blob: 65173b14b914502e6c03ca086bbc86b3b7d07275 [file] [log] [blame]
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001/*
2 * ring buffer based function tracer
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
13 */
14#include <linux/utsrelease.h>
15#include <linux/kallsyms.h>
16#include <linux/seq_file.h>
17#include <linux/debugfs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020018#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020019#include <linux/hardirq.h>
20#include <linux/linkage.h>
21#include <linux/uaccess.h>
22#include <linux/ftrace.h>
23#include <linux/module.h>
24#include <linux/percpu.h>
25#include <linux/ctype.h>
26#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020027#include <linux/poll.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020028#include <linux/gfp.h>
29#include <linux/fs.h>
30
Ingo Molnar86387f72008-05-12 21:20:51 +020031#include <linux/stacktrace.h>
32
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020033#include "trace.h"
34
35unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
36unsigned long __read_mostly tracing_thresh;
37
Steven Rostedt60a11772008-05-12 21:20:44 +020038static int tracing_disabled = 1;
39
Ingo Molnare309b412008-05-12 21:20:51 +020040static long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020041ns2usecs(cycle_t nsec)
42{
43 nsec += 500;
44 do_div(nsec, 1000);
45 return nsec;
46}
47
Ingo Molnare309b412008-05-12 21:20:51 +020048cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +020049{
Ingo Molnar0fd9e0d2008-05-12 21:20:48 +020050 return cpu_clock(cpu);
Ingo Molnar750ed1a2008-05-12 21:20:46 +020051}
52
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020053static struct trace_array global_trace;
54
55static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
56
57static struct trace_array max_tr;
58
59static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
60
Steven Rostedt26994ea2008-05-12 21:20:48 +020061static int tracer_enabled = 1;
Ingo Molnar57422792008-05-12 21:20:51 +020062static unsigned long trace_nr_entries = 65536UL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020063
64static struct tracer *trace_types __read_mostly;
65static struct tracer *current_trace __read_mostly;
66static int max_tracer_type_len;
67
68static DEFINE_MUTEX(trace_types_lock);
Ingo Molnar4e655512008-05-12 21:20:52 +020069static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
70
71unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
72
73/*
74 * FIXME: where should this be called?
75 */
76void trace_wake_up(void)
77{
78 if (!(trace_flags & TRACE_ITER_BLOCK))
79 wake_up(&trace_wait);
80}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020081
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020082#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
83
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020084static int __init set_nr_entries(char *str)
85{
86 if (!str)
87 return 0;
88 trace_nr_entries = simple_strtoul(str, &str, 0);
89 return 1;
90}
91__setup("trace_entries=", set_nr_entries);
92
Steven Rostedt57f50be2008-05-12 21:20:44 +020093unsigned long nsecs_to_usecs(unsigned long nsecs)
94{
95 return nsecs / 1000;
96}
97
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020098enum trace_type {
99 __TRACE_FIRST_TYPE = 0,
100
101 TRACE_FN,
102 TRACE_CTX,
Ingo Molnar57422792008-05-12 21:20:51 +0200103 TRACE_WAKE,
Ingo Molnar86387f72008-05-12 21:20:51 +0200104 TRACE_STACK,
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200105 TRACE_SPECIAL,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200106
107 __TRACE_LAST_TYPE
108};
109
110enum trace_flag_type {
111 TRACE_FLAG_IRQS_OFF = 0x01,
112 TRACE_FLAG_NEED_RESCHED = 0x02,
113 TRACE_FLAG_HARDIRQ = 0x04,
114 TRACE_FLAG_SOFTIRQ = 0x08,
115};
116
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200117#define TRACE_ITER_SYM_MASK \
118 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
119
120/* These must match the bit postions above */
121static const char *trace_options[] = {
122 "print-parent",
123 "sym-offset",
124 "sym-addr",
125 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200126 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200127 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200128 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200129 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200130 "stacktrace",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200131 NULL
132};
133
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200134static DEFINE_SPINLOCK(ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200135
136/*
137 * Copy the new maximum trace into the separate maximum-trace
138 * structure. (this way the maximum trace is permanently saved,
139 * for later retrieval via /debugfs/tracing/latency_trace)
140 */
Ingo Molnare309b412008-05-12 21:20:51 +0200141static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200142__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
143{
144 struct trace_array_cpu *data = tr->data[cpu];
145
146 max_tr.cpu = cpu;
147 max_tr.time_start = data->preempt_timestamp;
148
149 data = max_tr.data[cpu];
150 data->saved_latency = tracing_max_latency;
151
152 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
153 data->pid = tsk->pid;
154 data->uid = tsk->uid;
155 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
156 data->policy = tsk->policy;
157 data->rt_priority = tsk->rt_priority;
158
159 /* record this tasks comm */
160 tracing_record_cmdline(current);
161}
162
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200163void check_pages(struct trace_array_cpu *data)
164{
165 struct page *page, *tmp;
166
167 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
168 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
169
170 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
171 BUG_ON(page->lru.next->prev != &page->lru);
172 BUG_ON(page->lru.prev->next != &page->lru);
173 }
174}
175
176void *head_page(struct trace_array_cpu *data)
177{
178 struct page *page;
179
180 check_pages(data);
181 if (list_empty(&data->trace_pages))
182 return NULL;
183
184 page = list_entry(data->trace_pages.next, struct page, lru);
185 BUG_ON(&page->lru == &data->trace_pages);
186
187 return page_address(page);
188}
189
Ingo Molnare309b412008-05-12 21:20:51 +0200190static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200191trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
192{
193 int len = (PAGE_SIZE - 1) - s->len;
194 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200195 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200196
197 if (!len)
198 return 0;
199
200 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200201 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200202 va_end(ap);
203
Steven Rostedtb3806b42008-05-12 21:20:46 +0200204 /* If we can't write it all, don't bother writing anything */
205 if (ret > len)
206 return 0;
207
208 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200209
210 return len;
211}
212
Ingo Molnare309b412008-05-12 21:20:51 +0200213static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200214trace_seq_puts(struct trace_seq *s, const char *str)
215{
216 int len = strlen(str);
217
218 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200219 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200220
221 memcpy(s->buffer + s->len, str, len);
222 s->len += len;
223
224 return len;
225}
226
Ingo Molnare309b412008-05-12 21:20:51 +0200227static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200228trace_seq_putc(struct trace_seq *s, unsigned char c)
229{
230 if (s->len >= (PAGE_SIZE - 1))
231 return 0;
232
233 s->buffer[s->len++] = c;
234
235 return 1;
236}
237
Ingo Molnare309b412008-05-12 21:20:51 +0200238static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200239trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
240{
241 if (len > ((PAGE_SIZE - 1) - s->len))
242 return 0;
243
244 memcpy(s->buffer + s->len, mem, len);
245 s->len += len;
246
247 return len;
248}
249
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200250#define HEX_CHARS 17
251
Ingo Molnare309b412008-05-12 21:20:51 +0200252static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200253trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
254{
255 unsigned char hex[HEX_CHARS];
256 unsigned char *data;
257 unsigned char byte;
258 int i, j;
259
260 BUG_ON(len >= HEX_CHARS);
261
262 data = mem;
263
264#ifdef __BIG_ENDIAN
265 for (i = 0, j = 0; i < len; i++) {
266#else
267 for (i = len-1, j = 0; i >= 0; i--) {
268#endif
269 byte = data[i];
270
271 hex[j] = byte & 0x0f;
272 if (hex[j] >= 10)
273 hex[j] += 'a' - 10;
274 else
275 hex[j] += '0';
276 j++;
277
278 hex[j] = byte >> 4;
279 if (hex[j] >= 10)
280 hex[j] += 'a' - 10;
281 else
282 hex[j] += '0';
283 j++;
284 }
285 hex[j] = ' ';
286 j++;
287
288 return trace_seq_putmem(s, hex, j);
289}
290
Ingo Molnare309b412008-05-12 21:20:51 +0200291static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200292trace_seq_reset(struct trace_seq *s)
293{
294 s->len = 0;
295}
296
Ingo Molnare309b412008-05-12 21:20:51 +0200297static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200298trace_print_seq(struct seq_file *m, struct trace_seq *s)
299{
300 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
301
302 s->buffer[len] = 0;
303 seq_puts(m, s->buffer);
304
305 trace_seq_reset(s);
306}
307
Ingo Molnare309b412008-05-12 21:20:51 +0200308static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200309flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
310{
311 struct list_head flip_pages;
312
313 INIT_LIST_HEAD(&flip_pages);
314
Steven Rostedt93a588f2008-05-12 21:20:45 +0200315 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200316 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200317 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200318
319 check_pages(tr1);
320 check_pages(tr2);
321 list_splice_init(&tr1->trace_pages, &flip_pages);
322 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
323 list_splice_init(&flip_pages, &tr2->trace_pages);
324 BUG_ON(!list_empty(&flip_pages));
325 check_pages(tr1);
326 check_pages(tr2);
327}
328
Ingo Molnare309b412008-05-12 21:20:51 +0200329void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200330update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
331{
332 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200333 int i;
334
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200335 WARN_ON_ONCE(!irqs_disabled());
336 spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200337 /* clear out all the previous traces */
338 for_each_possible_cpu(i) {
339 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200340 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200341 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200342 }
343
344 __update_max_tr(tr, tsk, cpu);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200345 spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200346}
347
348/**
349 * update_max_tr_single - only copy one trace over, and reset the rest
350 * @tr - tracer
351 * @tsk - task with the latency
352 * @cpu - the cpu of the buffer to copy.
353 */
Ingo Molnare309b412008-05-12 21:20:51 +0200354void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200355update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
356{
357 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200358 int i;
359
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200360 WARN_ON_ONCE(!irqs_disabled());
361 spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200362 for_each_possible_cpu(i)
363 tracing_reset(max_tr.data[i]);
364
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200365 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200366 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200367
368 __update_max_tr(tr, tsk, cpu);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200369 spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200370}
371
372int register_tracer(struct tracer *type)
373{
374 struct tracer *t;
375 int len;
376 int ret = 0;
377
378 if (!type->name) {
379 pr_info("Tracer must have a name\n");
380 return -1;
381 }
382
383 mutex_lock(&trace_types_lock);
384 for (t = trace_types; t; t = t->next) {
385 if (strcmp(type->name, t->name) == 0) {
386 /* already found */
387 pr_info("Trace %s already registered\n",
388 type->name);
389 ret = -1;
390 goto out;
391 }
392 }
393
Steven Rostedt60a11772008-05-12 21:20:44 +0200394#ifdef CONFIG_FTRACE_STARTUP_TEST
395 if (type->selftest) {
396 struct tracer *saved_tracer = current_trace;
397 struct trace_array_cpu *data;
398 struct trace_array *tr = &global_trace;
399 int saved_ctrl = tr->ctrl;
400 int i;
401 /*
402 * Run a selftest on this tracer.
403 * Here we reset the trace buffer, and set the current
404 * tracer to be this tracer. The tracer can then run some
405 * internal tracing to verify that everything is in order.
406 * If we fail, we do not register this tracer.
407 */
408 for_each_possible_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200409 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200410 if (!head_page(data))
411 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200412 tracing_reset(data);
413 }
414 current_trace = type;
415 tr->ctrl = 0;
416 /* the test is responsible for initializing and enabling */
417 pr_info("Testing tracer %s: ", type->name);
418 ret = type->selftest(type, tr);
419 /* the test is responsible for resetting too */
420 current_trace = saved_tracer;
421 tr->ctrl = saved_ctrl;
422 if (ret) {
423 printk(KERN_CONT "FAILED!\n");
424 goto out;
425 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200426 /* Only reset on passing, to avoid touching corrupted buffers */
427 for_each_possible_cpu(i) {
428 data = tr->data[i];
429 if (!head_page(data))
430 continue;
431 tracing_reset(data);
432 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200433 printk(KERN_CONT "PASSED\n");
434 }
435#endif
436
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200437 type->next = trace_types;
438 trace_types = type;
439 len = strlen(type->name);
440 if (len > max_tracer_type_len)
441 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200442
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200443 out:
444 mutex_unlock(&trace_types_lock);
445
446 return ret;
447}
448
449void unregister_tracer(struct tracer *type)
450{
451 struct tracer **t;
452 int len;
453
454 mutex_lock(&trace_types_lock);
455 for (t = &trace_types; *t; t = &(*t)->next) {
456 if (*t == type)
457 goto found;
458 }
459 pr_info("Trace %s not registered\n", type->name);
460 goto out;
461
462 found:
463 *t = (*t)->next;
464 if (strlen(type->name) != max_tracer_type_len)
465 goto out;
466
467 max_tracer_type_len = 0;
468 for (t = &trace_types; *t; t = &(*t)->next) {
469 len = strlen((*t)->name);
470 if (len > max_tracer_type_len)
471 max_tracer_type_len = len;
472 }
473 out:
474 mutex_unlock(&trace_types_lock);
475}
476
Ingo Molnare309b412008-05-12 21:20:51 +0200477void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200478{
479 data->trace_idx = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200480 data->trace_head = data->trace_tail = head_page(data);
481 data->trace_head_idx = 0;
482 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200483}
484
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200485#define SAVED_CMDLINES 128
486static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
487static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
488static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
489static int cmdline_idx;
490static DEFINE_SPINLOCK(trace_cmdline_lock);
491atomic_t trace_record_cmdline_disabled;
492
493static void trace_init_cmdlines(void)
494{
495 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
496 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
497 cmdline_idx = 0;
498}
499
Ingo Molnare309b412008-05-12 21:20:51 +0200500void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200501
Ingo Molnare309b412008-05-12 21:20:51 +0200502static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200503{
504 unsigned map;
505 unsigned idx;
506
507 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
508 return;
509
510 /*
511 * It's not the end of the world if we don't get
512 * the lock, but we also don't want to spin
513 * nor do we want to disable interrupts,
514 * so if we miss here, then better luck next time.
515 */
516 if (!spin_trylock(&trace_cmdline_lock))
517 return;
518
519 idx = map_pid_to_cmdline[tsk->pid];
520 if (idx >= SAVED_CMDLINES) {
521 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
522
523 map = map_cmdline_to_pid[idx];
524 if (map <= PID_MAX_DEFAULT)
525 map_pid_to_cmdline[map] = (unsigned)-1;
526
527 map_pid_to_cmdline[tsk->pid] = idx;
528
529 cmdline_idx = idx;
530 }
531
532 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
533
534 spin_unlock(&trace_cmdline_lock);
535}
536
Ingo Molnare309b412008-05-12 21:20:51 +0200537static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200538{
539 char *cmdline = "<...>";
540 unsigned map;
541
542 if (!pid)
543 return "<idle>";
544
545 if (pid > PID_MAX_DEFAULT)
546 goto out;
547
548 map = map_pid_to_cmdline[pid];
549 if (map >= SAVED_CMDLINES)
550 goto out;
551
552 cmdline = saved_cmdlines[map];
553
554 out:
555 return cmdline;
556}
557
Ingo Molnare309b412008-05-12 21:20:51 +0200558void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200559{
560 if (atomic_read(&trace_record_cmdline_disabled))
561 return;
562
563 trace_save_cmdline(tsk);
564}
565
Ingo Molnare309b412008-05-12 21:20:51 +0200566static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200567trace_next_list(struct trace_array_cpu *data, struct list_head *next)
568{
569 /*
570 * Roundrobin - but skip the head (which is not a real page):
571 */
572 next = next->next;
573 if (unlikely(next == &data->trace_pages))
574 next = next->next;
575 BUG_ON(next == &data->trace_pages);
576
577 return next;
578}
579
Ingo Molnare309b412008-05-12 21:20:51 +0200580static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200581trace_next_page(struct trace_array_cpu *data, void *addr)
582{
583 struct list_head *next;
584 struct page *page;
585
586 page = virt_to_page(addr);
587
588 next = trace_next_list(data, &page->lru);
589 page = list_entry(next, struct page, lru);
590
591 return page_address(page);
592}
593
Ingo Molnare309b412008-05-12 21:20:51 +0200594static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200595tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200596{
597 unsigned long idx, idx_next;
598 struct trace_entry *entry;
599
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200600 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200601 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200602 idx_next = idx + 1;
603
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200604 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
605
Steven Rostedt93a588f2008-05-12 21:20:45 +0200606 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200607
608 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200609 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200610 idx_next = 0;
611 }
612
Steven Rostedt93a588f2008-05-12 21:20:45 +0200613 if (data->trace_head == data->trace_tail &&
614 idx_next == data->trace_tail_idx) {
615 /* overrun */
616 data->trace_tail_idx++;
617 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
618 data->trace_tail =
619 trace_next_page(data, data->trace_tail);
620 data->trace_tail_idx = 0;
621 }
622 }
623
624 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200625
626 return entry;
627}
628
Ingo Molnare309b412008-05-12 21:20:51 +0200629static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200630tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200631{
632 struct task_struct *tsk = current;
633 unsigned long pc;
634
635 pc = preempt_count();
636
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200637 entry->preempt_count = pc & 0xff;
638 entry->pid = tsk->pid;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200639 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200640 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
641 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
642 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
643 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
644}
645
Ingo Molnare309b412008-05-12 21:20:51 +0200646void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200647trace_function(struct trace_array *tr, struct trace_array_cpu *data,
648 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200649{
650 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200651 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200652
Ingo Molnardcb63082008-05-12 21:20:48 +0200653 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200654 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200655 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200656 entry->type = TRACE_FN;
657 entry->fn.ip = ip;
658 entry->fn.parent_ip = parent_ip;
Ingo Molnardcb63082008-05-12 21:20:48 +0200659 spin_unlock_irqrestore(&data->lock, irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200660}
661
Ingo Molnare309b412008-05-12 21:20:51 +0200662void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200663ftrace(struct trace_array *tr, struct trace_array_cpu *data,
664 unsigned long ip, unsigned long parent_ip, unsigned long flags)
665{
666 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200667 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200668}
669
Ingo Molnar4e655512008-05-12 21:20:52 +0200670#ifdef CONFIG_CONTEXT_SWITCH_TRACER
671
Ingo Molnare309b412008-05-12 21:20:51 +0200672void
Ingo Molnar4e655512008-05-12 21:20:52 +0200673__trace_special(void *__tr, void *__data,
674 unsigned long arg1, unsigned long arg2, unsigned long arg3)
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200675{
Ingo Molnar4e655512008-05-12 21:20:52 +0200676 struct trace_array_cpu *data = __data;
677 struct trace_array *tr = __tr;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200678 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200679 unsigned long irq_flags;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200680
Ingo Molnardcb63082008-05-12 21:20:48 +0200681 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200682 entry = tracing_get_trace_entry(tr, data);
683 tracing_generic_entry_update(entry, 0);
684 entry->type = TRACE_SPECIAL;
685 entry->special.arg1 = arg1;
686 entry->special.arg2 = arg2;
687 entry->special.arg3 = arg3;
Ingo Molnardcb63082008-05-12 21:20:48 +0200688 spin_unlock_irqrestore(&data->lock, irq_flags);
Ingo Molnar86387f72008-05-12 21:20:51 +0200689}
690
Ingo Molnar4e655512008-05-12 21:20:52 +0200691#endif
692
Ingo Molnar86387f72008-05-12 21:20:51 +0200693void __trace_stack(struct trace_array *tr,
694 struct trace_array_cpu *data,
695 unsigned long flags,
696 int skip)
697{
698 struct trace_entry *entry;
699 struct stack_trace trace;
700
701 if (!(trace_flags & TRACE_ITER_STACKTRACE))
702 return;
703
704 entry = tracing_get_trace_entry(tr, data);
705 tracing_generic_entry_update(entry, flags);
706 entry->type = TRACE_STACK;
707
708 memset(&entry->stack, 0, sizeof(entry->stack));
709
710 trace.nr_entries = 0;
711 trace.max_entries = FTRACE_STACK_ENTRIES;
712 trace.skip = skip;
713 trace.entries = entry->stack.caller;
714
715 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200716}
717
Ingo Molnare309b412008-05-12 21:20:51 +0200718void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200719tracing_sched_switch_trace(struct trace_array *tr,
720 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200721 struct task_struct *prev,
722 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200723 unsigned long flags)
724{
725 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200726 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200727
Ingo Molnardcb63082008-05-12 21:20:48 +0200728 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200729 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200730 tracing_generic_entry_update(entry, flags);
731 entry->type = TRACE_CTX;
732 entry->ctx.prev_pid = prev->pid;
733 entry->ctx.prev_prio = prev->prio;
734 entry->ctx.prev_state = prev->state;
735 entry->ctx.next_pid = next->pid;
736 entry->ctx.next_prio = next->prio;
Ingo Molnar86387f72008-05-12 21:20:51 +0200737 __trace_stack(tr, data, flags, 4);
Ingo Molnardcb63082008-05-12 21:20:48 +0200738 spin_unlock_irqrestore(&data->lock, irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200739}
740
Ingo Molnar57422792008-05-12 21:20:51 +0200741void
742tracing_sched_wakeup_trace(struct trace_array *tr,
743 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200744 struct task_struct *wakee,
745 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200746 unsigned long flags)
747{
748 struct trace_entry *entry;
749 unsigned long irq_flags;
750
751 spin_lock_irqsave(&data->lock, irq_flags);
752 entry = tracing_get_trace_entry(tr, data);
753 tracing_generic_entry_update(entry, flags);
754 entry->type = TRACE_WAKE;
755 entry->ctx.prev_pid = curr->pid;
756 entry->ctx.prev_prio = curr->prio;
757 entry->ctx.prev_state = curr->state;
758 entry->ctx.next_pid = wakee->pid;
759 entry->ctx.next_prio = wakee->prio;
Ingo Molnar86387f72008-05-12 21:20:51 +0200760 __trace_stack(tr, data, flags, 5);
Ingo Molnar57422792008-05-12 21:20:51 +0200761 spin_unlock_irqrestore(&data->lock, irq_flags);
Ingo Molnar57422792008-05-12 21:20:51 +0200762}
763
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200764#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200765static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200766function_trace_call(unsigned long ip, unsigned long parent_ip)
767{
768 struct trace_array *tr = &global_trace;
769 struct trace_array_cpu *data;
770 unsigned long flags;
771 long disabled;
772 int cpu;
773
774 if (unlikely(!tracer_enabled))
775 return;
776
777 local_irq_save(flags);
778 cpu = raw_smp_processor_id();
779 data = tr->data[cpu];
780 disabled = atomic_inc_return(&data->disabled);
781
782 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200783 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200784
785 atomic_dec(&data->disabled);
786 local_irq_restore(flags);
787}
788
789static struct ftrace_ops trace_ops __read_mostly =
790{
791 .func = function_trace_call,
792};
793
Ingo Molnare309b412008-05-12 21:20:51 +0200794void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200795{
796 register_ftrace_function(&trace_ops);
797}
798
Ingo Molnare309b412008-05-12 21:20:51 +0200799void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200800{
801 unregister_ftrace_function(&trace_ops);
802}
803#endif
804
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200805enum trace_file_type {
806 TRACE_FILE_LAT_FMT = 1,
807};
808
809static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200810trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
811 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200812{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200813 struct page *page;
814 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200815
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200816 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200817 iter->next_idx[cpu] >= data->trace_idx ||
818 (data->trace_head == data->trace_tail &&
819 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200820 return NULL;
821
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200822 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200823 /* Initialize the iterator for this cpu trace buffer */
824 WARN_ON(!data->trace_tail);
825 page = virt_to_page(data->trace_tail);
826 iter->next_page[cpu] = &page->lru;
827 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200828 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200829
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200830 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200831 BUG_ON(&data->trace_pages == &page->lru);
832
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200833 array = page_address(page);
834
Steven Rostedt93a588f2008-05-12 21:20:45 +0200835 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200836 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200837}
838
Ingo Molnare309b412008-05-12 21:20:51 +0200839static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200840find_next_entry(struct trace_iterator *iter, int *ent_cpu)
841{
842 struct trace_array *tr = iter->tr;
843 struct trace_entry *ent, *next = NULL;
844 int next_cpu = -1;
845 int cpu;
846
847 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200848 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200849 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200850 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +0200851 /*
852 * Pick the entry with the smallest timestamp:
853 */
854 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200855 next = ent;
856 next_cpu = cpu;
857 }
858 }
859
860 if (ent_cpu)
861 *ent_cpu = next_cpu;
862
863 return next;
864}
865
Ingo Molnare309b412008-05-12 21:20:51 +0200866static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200867{
868 iter->idx++;
869 iter->next_idx[iter->cpu]++;
870 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +0200871
Steven Rostedtb3806b42008-05-12 21:20:46 +0200872 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
873 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
874
875 iter->next_page_idx[iter->cpu] = 0;
876 iter->next_page[iter->cpu] =
877 trace_next_list(data, iter->next_page[iter->cpu]);
878 }
879}
880
Ingo Molnare309b412008-05-12 21:20:51 +0200881static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200882{
883 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
884
885 data->trace_tail_idx++;
886 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
887 data->trace_tail = trace_next_page(data, data->trace_tail);
888 data->trace_tail_idx = 0;
889 }
890
891 /* Check if we empty it, then reset the index */
892 if (data->trace_head == data->trace_tail &&
893 data->trace_head_idx == data->trace_tail_idx)
894 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200895}
896
Ingo Molnare309b412008-05-12 21:20:51 +0200897static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200898{
899 struct trace_entry *next;
900 int next_cpu = -1;
901
902 next = find_next_entry(iter, &next_cpu);
903
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200904 iter->prev_ent = iter->ent;
905 iter->prev_cpu = iter->cpu;
906
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200907 iter->ent = next;
908 iter->cpu = next_cpu;
909
Steven Rostedtb3806b42008-05-12 21:20:46 +0200910 if (next)
911 trace_iterator_increment(iter);
912
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200913 return next ? iter : NULL;
914}
915
Ingo Molnare309b412008-05-12 21:20:51 +0200916static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200917{
918 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200919 void *last_ent = iter->ent;
920 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200921 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200922
923 (*pos)++;
924
925 /* can't go backwards */
926 if (iter->idx > i)
927 return NULL;
928
929 if (iter->idx < 0)
930 ent = find_next_entry_inc(iter);
931 else
932 ent = iter;
933
934 while (ent && iter->idx < i)
935 ent = find_next_entry_inc(iter);
936
937 iter->pos = *pos;
938
939 if (last_ent && !ent)
940 seq_puts(m, "\n\nvim:ft=help\n");
941
942 return ent;
943}
944
945static void *s_start(struct seq_file *m, loff_t *pos)
946{
947 struct trace_iterator *iter = m->private;
948 void *p = NULL;
949 loff_t l = 0;
950 int i;
951
952 mutex_lock(&trace_types_lock);
953
954 if (!current_trace || current_trace != iter->trace)
955 return NULL;
956
957 atomic_inc(&trace_record_cmdline_disabled);
958
959 /* let the tracer grab locks here if needed */
960 if (current_trace->start)
961 current_trace->start(iter);
962
963 if (*pos != iter->pos) {
964 iter->ent = NULL;
965 iter->cpu = 0;
966 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200967 iter->prev_ent = NULL;
968 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200969
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200970 for_each_possible_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200971 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200972 iter->next_page[i] = NULL;
973 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200974
975 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
976 ;
977
978 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200979 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200980 p = s_next(m, p, &l);
981 }
982
983 return p;
984}
985
986static void s_stop(struct seq_file *m, void *p)
987{
988 struct trace_iterator *iter = m->private;
989
990 atomic_dec(&trace_record_cmdline_disabled);
991
992 /* let the tracer release locks here if needed */
993 if (current_trace && current_trace == iter->trace && iter->trace->stop)
994 iter->trace->stop(iter);
995
996 mutex_unlock(&trace_types_lock);
997}
998
Steven Rostedtb3806b42008-05-12 21:20:46 +0200999static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001000seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001001{
1002#ifdef CONFIG_KALLSYMS
1003 char str[KSYM_SYMBOL_LEN];
1004
1005 kallsyms_lookup(address, NULL, NULL, NULL, str);
1006
Steven Rostedtb3806b42008-05-12 21:20:46 +02001007 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001008#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001009 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001010}
1011
Steven Rostedtb3806b42008-05-12 21:20:46 +02001012static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001013seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1014 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001015{
1016#ifdef CONFIG_KALLSYMS
1017 char str[KSYM_SYMBOL_LEN];
1018
1019 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001020 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001021#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001022 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001023}
1024
1025#ifndef CONFIG_64BIT
1026# define IP_FMT "%08lx"
1027#else
1028# define IP_FMT "%016lx"
1029#endif
1030
Ingo Molnare309b412008-05-12 21:20:51 +02001031static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001032seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001033{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001034 int ret;
1035
1036 if (!ip)
1037 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001038
1039 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001040 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001041 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001042 ret = seq_print_sym_short(s, "%s", ip);
1043
1044 if (!ret)
1045 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001046
1047 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001048 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1049 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001050}
1051
Ingo Molnare309b412008-05-12 21:20:51 +02001052static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001053{
1054 seq_puts(m, "# _------=> CPU# \n");
1055 seq_puts(m, "# / _-----=> irqs-off \n");
1056 seq_puts(m, "# | / _----=> need-resched \n");
1057 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1058 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1059 seq_puts(m, "# |||| / \n");
1060 seq_puts(m, "# ||||| delay \n");
1061 seq_puts(m, "# cmd pid ||||| time | caller \n");
1062 seq_puts(m, "# \\ / ||||| \\ | / \n");
1063}
1064
Ingo Molnare309b412008-05-12 21:20:51 +02001065static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001066{
1067 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1068 seq_puts(m, "# | | | | |\n");
1069}
1070
1071
Ingo Molnare309b412008-05-12 21:20:51 +02001072static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001073print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1074{
1075 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1076 struct trace_array *tr = iter->tr;
1077 struct trace_array_cpu *data = tr->data[tr->cpu];
1078 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001079 unsigned long total = 0;
1080 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001081 int cpu;
1082 const char *name = "preemption";
1083
1084 if (type)
1085 name = type->name;
1086
1087 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001088 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001089 total += tr->data[cpu]->trace_idx;
1090 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001091 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001092 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001093 entries += tr->data[cpu]->trace_idx;
1094 }
1095 }
1096
1097 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1098 name, UTS_RELEASE);
1099 seq_puts(m, "-----------------------------------"
1100 "---------------------------------\n");
1101 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1102 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001103 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001104 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001105 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001106 tr->cpu,
1107#if defined(CONFIG_PREEMPT_NONE)
1108 "server",
1109#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1110 "desktop",
1111#elif defined(CONFIG_PREEMPT_DESKTOP)
1112 "preempt",
1113#else
1114 "unknown",
1115#endif
1116 /* These are reserved for later use */
1117 0, 0, 0, 0);
1118#ifdef CONFIG_SMP
1119 seq_printf(m, " #P:%d)\n", num_online_cpus());
1120#else
1121 seq_puts(m, ")\n");
1122#endif
1123 seq_puts(m, " -----------------\n");
1124 seq_printf(m, " | task: %.16s-%d "
1125 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1126 data->comm, data->pid, data->uid, data->nice,
1127 data->policy, data->rt_priority);
1128 seq_puts(m, " -----------------\n");
1129
1130 if (data->critical_start) {
1131 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001132 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1133 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001134 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001135 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1136 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001137 seq_puts(m, "\n");
1138 }
1139
1140 seq_puts(m, "\n");
1141}
1142
Ingo Molnare309b412008-05-12 21:20:51 +02001143static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001144lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001145{
1146 int hardirq, softirq;
1147 char *comm;
1148
1149 comm = trace_find_cmdline(entry->pid);
1150
Steven Rostedt214023c2008-05-12 21:20:46 +02001151 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1152 trace_seq_printf(s, "%d", cpu);
1153 trace_seq_printf(s, "%c%c",
1154 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1155 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001156
1157 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1158 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1159 if (hardirq && softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001160 trace_seq_putc(s, 'H');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001161 else {
1162 if (hardirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001163 trace_seq_putc(s, 'h');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001164 else {
1165 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001166 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001167 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001168 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001169 }
1170 }
1171
1172 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001173 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001174 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001175 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001176}
1177
1178unsigned long preempt_mark_thresh = 100;
1179
Ingo Molnare309b412008-05-12 21:20:51 +02001180static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001181lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001182 unsigned long rel_usecs)
1183{
Steven Rostedt214023c2008-05-12 21:20:46 +02001184 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001185 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001186 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001187 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001188 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001189 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001190 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001191}
1192
1193static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1194
Ingo Molnare309b412008-05-12 21:20:51 +02001195static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001196print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001197{
Steven Rostedt214023c2008-05-12 21:20:46 +02001198 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001199 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1200 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1201 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1202 struct trace_entry *entry = iter->ent;
1203 unsigned long abs_usecs;
1204 unsigned long rel_usecs;
1205 char *comm;
1206 int S;
Ingo Molnar86387f72008-05-12 21:20:51 +02001207 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001208
1209 if (!next_entry)
1210 next_entry = entry;
1211 rel_usecs = ns2usecs(next_entry->t - entry->t);
1212 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1213
1214 if (verbose) {
1215 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001216 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1217 " %ld.%03ldms (+%ld.%03ldms): ",
1218 comm,
1219 entry->pid, cpu, entry->flags,
1220 entry->preempt_count, trace_idx,
1221 ns2usecs(entry->t),
1222 abs_usecs/1000,
1223 abs_usecs % 1000, rel_usecs/1000,
1224 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001225 } else {
Ingo Molnar86387f72008-05-12 21:20:51 +02001226 if (entry->type != TRACE_STACK) {
1227 lat_print_generic(s, entry, cpu);
1228 lat_print_timestamp(s, abs_usecs, rel_usecs);
1229 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001230 }
1231 switch (entry->type) {
1232 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001233 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1234 trace_seq_puts(s, " (");
1235 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1236 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001237 break;
1238 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001239 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001240 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1241 state_to_char[entry->ctx.prev_state] : 'X';
1242 comm = trace_find_cmdline(entry->ctx.next_pid);
Ingo Molnar57422792008-05-12 21:20:51 +02001243 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001244 entry->ctx.prev_pid,
1245 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001246 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001247 entry->ctx.next_pid,
1248 entry->ctx.next_prio,
1249 comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001250 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001251 case TRACE_SPECIAL:
Ingo Molnar4e655512008-05-12 21:20:52 +02001252 trace_seq_printf(s, " %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001253 entry->special.arg1,
1254 entry->special.arg2,
1255 entry->special.arg3);
1256 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001257 case TRACE_STACK:
1258 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1259 if (i)
1260 trace_seq_puts(s, " <= ");
1261 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1262 }
1263 trace_seq_puts(s, "\n");
1264 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001265 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001266 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001267 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001268 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001269}
1270
Ingo Molnare309b412008-05-12 21:20:51 +02001271static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001272{
Steven Rostedt214023c2008-05-12 21:20:46 +02001273 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001274 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001275 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001276 unsigned long usec_rem;
1277 unsigned long long t;
1278 unsigned long secs;
1279 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001280 int ret;
Ingo Molnar86387f72008-05-12 21:20:51 +02001281 int S;
1282 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001283
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001284 entry = iter->ent;
1285
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001286 comm = trace_find_cmdline(iter->ent->pid);
1287
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001288 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001289 usec_rem = do_div(t, 1000000ULL);
1290 secs = (unsigned long)t;
1291
Ingo Molnar86387f72008-05-12 21:20:51 +02001292 if (entry->type != TRACE_STACK) {
1293 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1294 if (!ret)
1295 return 0;
1296 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1297 if (!ret)
1298 return 0;
1299 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1300 if (!ret)
1301 return 0;
1302 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001303
1304 switch (entry->type) {
1305 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001306 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1307 if (!ret)
1308 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001309 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1310 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001311 ret = trace_seq_printf(s, " <-");
1312 if (!ret)
1313 return 0;
1314 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1315 sym_flags);
1316 if (!ret)
1317 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001318 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001319 ret = trace_seq_printf(s, "\n");
1320 if (!ret)
1321 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001322 break;
1323 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001324 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001325 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1326 state_to_char[entry->ctx.prev_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001327 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001328 entry->ctx.prev_pid,
1329 entry->ctx.prev_prio,
1330 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001331 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001332 entry->ctx.next_pid,
1333 entry->ctx.next_prio);
1334 if (!ret)
1335 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001336 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001337 case TRACE_SPECIAL:
Ingo Molnar4e655512008-05-12 21:20:52 +02001338 ret = trace_seq_printf(s, " %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001339 entry->special.arg1,
1340 entry->special.arg2,
1341 entry->special.arg3);
1342 if (!ret)
1343 return 0;
1344 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001345 case TRACE_STACK:
1346 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1347 if (i) {
1348 ret = trace_seq_puts(s, " <= ");
1349 if (!ret)
1350 return 0;
1351 }
1352 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1353 sym_flags);
1354 if (!ret)
1355 return 0;
1356 }
1357 ret = trace_seq_puts(s, "\n");
1358 if (!ret)
1359 return 0;
1360 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001361 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001362 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001363}
1364
Ingo Molnare309b412008-05-12 21:20:51 +02001365static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001366{
1367 struct trace_seq *s = &iter->seq;
1368 struct trace_entry *entry;
1369 int ret;
1370 int S;
1371
1372 entry = iter->ent;
1373
1374 ret = trace_seq_printf(s, "%d %d %llu ",
1375 entry->pid, iter->cpu, entry->t);
1376 if (!ret)
1377 return 0;
1378
1379 switch (entry->type) {
1380 case TRACE_FN:
1381 ret = trace_seq_printf(s, "%x %x\n",
1382 entry->fn.ip, entry->fn.parent_ip);
1383 if (!ret)
1384 return 0;
1385 break;
1386 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001387 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001388 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1389 state_to_char[entry->ctx.prev_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001390 if (entry->type == TRACE_WAKE)
1391 S = '+';
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001392 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1393 entry->ctx.prev_pid,
1394 entry->ctx.prev_prio,
1395 S,
1396 entry->ctx.next_pid,
1397 entry->ctx.next_prio);
1398 if (!ret)
1399 return 0;
1400 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001401 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001402 case TRACE_STACK:
Ingo Molnar4e655512008-05-12 21:20:52 +02001403 ret = trace_seq_printf(s, " %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001404 entry->special.arg1,
1405 entry->special.arg2,
1406 entry->special.arg3);
1407 if (!ret)
1408 return 0;
1409 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001410 }
1411 return 1;
1412}
1413
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001414#define SEQ_PUT_FIELD_RET(s, x) \
1415do { \
1416 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1417 return 0; \
1418} while (0)
1419
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001420#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1421do { \
1422 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1423 return 0; \
1424} while (0)
1425
Ingo Molnare309b412008-05-12 21:20:51 +02001426static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001427{
1428 struct trace_seq *s = &iter->seq;
1429 unsigned char newline = '\n';
1430 struct trace_entry *entry;
1431 int S;
1432
1433 entry = iter->ent;
1434
1435 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1436 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1437 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1438
1439 switch (entry->type) {
1440 case TRACE_FN:
1441 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1442 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1443 break;
1444 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001445 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001446 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1447 state_to_char[entry->ctx.prev_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001448 if (entry->type == TRACE_WAKE)
1449 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001450 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1451 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1452 SEQ_PUT_HEX_FIELD_RET(s, S);
1453 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1454 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1455 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1456 break;
1457 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001458 case TRACE_STACK:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001459 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1460 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1461 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1462 break;
1463 }
1464 SEQ_PUT_FIELD_RET(s, newline);
1465
1466 return 1;
1467}
1468
Ingo Molnare309b412008-05-12 21:20:51 +02001469static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001470{
1471 struct trace_seq *s = &iter->seq;
1472 struct trace_entry *entry;
1473
1474 entry = iter->ent;
1475
1476 SEQ_PUT_FIELD_RET(s, entry->pid);
1477 SEQ_PUT_FIELD_RET(s, entry->cpu);
1478 SEQ_PUT_FIELD_RET(s, entry->t);
1479
1480 switch (entry->type) {
1481 case TRACE_FN:
1482 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1483 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1484 break;
1485 case TRACE_CTX:
1486 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1487 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1488 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1489 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1490 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1491 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001492 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001493 case TRACE_STACK:
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001494 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1495 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1496 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1497 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001498 }
1499 return 1;
1500}
1501
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001502static int trace_empty(struct trace_iterator *iter)
1503{
1504 struct trace_array_cpu *data;
1505 int cpu;
1506
1507 for_each_possible_cpu(cpu) {
1508 data = iter->tr->data[cpu];
1509
Steven Rostedtb3806b42008-05-12 21:20:46 +02001510 if (head_page(data) && data->trace_idx &&
1511 (data->trace_tail != data->trace_head ||
1512 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001513 return 0;
1514 }
1515 return 1;
1516}
1517
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001518static int print_trace_line(struct trace_iterator *iter)
1519{
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001520 if (trace_flags & TRACE_ITER_BIN)
1521 return print_bin_fmt(iter);
1522
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001523 if (trace_flags & TRACE_ITER_HEX)
1524 return print_hex_fmt(iter);
1525
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001526 if (trace_flags & TRACE_ITER_RAW)
1527 return print_raw_fmt(iter);
1528
1529 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1530 return print_lat_fmt(iter, iter->idx, iter->cpu);
1531
1532 return print_trace_fmt(iter);
1533}
1534
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001535static int s_show(struct seq_file *m, void *v)
1536{
1537 struct trace_iterator *iter = v;
1538
1539 if (iter->ent == NULL) {
1540 if (iter->tr) {
1541 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1542 seq_puts(m, "#\n");
1543 }
1544 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1545 /* print nothing if the buffers are empty */
1546 if (trace_empty(iter))
1547 return 0;
1548 print_trace_header(m, iter);
1549 if (!(trace_flags & TRACE_ITER_VERBOSE))
1550 print_lat_help_header(m);
1551 } else {
1552 if (!(trace_flags & TRACE_ITER_VERBOSE))
1553 print_func_help_header(m);
1554 }
1555 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001556 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001557 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001558 }
1559
1560 return 0;
1561}
1562
1563static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001564 .start = s_start,
1565 .next = s_next,
1566 .stop = s_stop,
1567 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001568};
1569
Ingo Molnare309b412008-05-12 21:20:51 +02001570static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001571__tracing_open(struct inode *inode, struct file *file, int *ret)
1572{
1573 struct trace_iterator *iter;
1574
Steven Rostedt60a11772008-05-12 21:20:44 +02001575 if (tracing_disabled) {
1576 *ret = -ENODEV;
1577 return NULL;
1578 }
1579
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001580 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1581 if (!iter) {
1582 *ret = -ENOMEM;
1583 goto out;
1584 }
1585
1586 mutex_lock(&trace_types_lock);
1587 if (current_trace && current_trace->print_max)
1588 iter->tr = &max_tr;
1589 else
1590 iter->tr = inode->i_private;
1591 iter->trace = current_trace;
1592 iter->pos = -1;
1593
1594 /* TODO stop tracer */
1595 *ret = seq_open(file, &tracer_seq_ops);
1596 if (!*ret) {
1597 struct seq_file *m = file->private_data;
1598 m->private = iter;
1599
1600 /* stop the trace while dumping */
1601 if (iter->tr->ctrl)
1602 tracer_enabled = 0;
1603
1604 if (iter->trace && iter->trace->open)
1605 iter->trace->open(iter);
1606 } else {
1607 kfree(iter);
1608 iter = NULL;
1609 }
1610 mutex_unlock(&trace_types_lock);
1611
1612 out:
1613 return iter;
1614}
1615
1616int tracing_open_generic(struct inode *inode, struct file *filp)
1617{
Steven Rostedt60a11772008-05-12 21:20:44 +02001618 if (tracing_disabled)
1619 return -ENODEV;
1620
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001621 filp->private_data = inode->i_private;
1622 return 0;
1623}
1624
1625int tracing_release(struct inode *inode, struct file *file)
1626{
1627 struct seq_file *m = (struct seq_file *)file->private_data;
1628 struct trace_iterator *iter = m->private;
1629
1630 mutex_lock(&trace_types_lock);
1631 if (iter->trace && iter->trace->close)
1632 iter->trace->close(iter);
1633
1634 /* reenable tracing if it was previously enabled */
1635 if (iter->tr->ctrl)
1636 tracer_enabled = 1;
1637 mutex_unlock(&trace_types_lock);
1638
1639 seq_release(inode, file);
1640 kfree(iter);
1641 return 0;
1642}
1643
1644static int tracing_open(struct inode *inode, struct file *file)
1645{
1646 int ret;
1647
1648 __tracing_open(inode, file, &ret);
1649
1650 return ret;
1651}
1652
1653static int tracing_lt_open(struct inode *inode, struct file *file)
1654{
1655 struct trace_iterator *iter;
1656 int ret;
1657
1658 iter = __tracing_open(inode, file, &ret);
1659
1660 if (!ret)
1661 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1662
1663 return ret;
1664}
1665
1666
Ingo Molnare309b412008-05-12 21:20:51 +02001667static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001668t_next(struct seq_file *m, void *v, loff_t *pos)
1669{
1670 struct tracer *t = m->private;
1671
1672 (*pos)++;
1673
1674 if (t)
1675 t = t->next;
1676
1677 m->private = t;
1678
1679 return t;
1680}
1681
1682static void *t_start(struct seq_file *m, loff_t *pos)
1683{
1684 struct tracer *t = m->private;
1685 loff_t l = 0;
1686
1687 mutex_lock(&trace_types_lock);
1688 for (; t && l < *pos; t = t_next(m, t, &l))
1689 ;
1690
1691 return t;
1692}
1693
1694static void t_stop(struct seq_file *m, void *p)
1695{
1696 mutex_unlock(&trace_types_lock);
1697}
1698
1699static int t_show(struct seq_file *m, void *v)
1700{
1701 struct tracer *t = v;
1702
1703 if (!t)
1704 return 0;
1705
1706 seq_printf(m, "%s", t->name);
1707 if (t->next)
1708 seq_putc(m, ' ');
1709 else
1710 seq_putc(m, '\n');
1711
1712 return 0;
1713}
1714
1715static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001716 .start = t_start,
1717 .next = t_next,
1718 .stop = t_stop,
1719 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001720};
1721
1722static int show_traces_open(struct inode *inode, struct file *file)
1723{
1724 int ret;
1725
Steven Rostedt60a11772008-05-12 21:20:44 +02001726 if (tracing_disabled)
1727 return -ENODEV;
1728
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001729 ret = seq_open(file, &show_traces_seq_ops);
1730 if (!ret) {
1731 struct seq_file *m = file->private_data;
1732 m->private = trace_types;
1733 }
1734
1735 return ret;
1736}
1737
1738static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001739 .open = tracing_open,
1740 .read = seq_read,
1741 .llseek = seq_lseek,
1742 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001743};
1744
1745static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001746 .open = tracing_lt_open,
1747 .read = seq_read,
1748 .llseek = seq_lseek,
1749 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001750};
1751
1752static struct file_operations show_traces_fops = {
1753 .open = show_traces_open,
1754 .read = seq_read,
1755 .release = seq_release,
1756};
1757
1758static ssize_t
1759tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1760 size_t cnt, loff_t *ppos)
1761{
1762 char *buf;
1763 int r = 0;
1764 int len = 0;
1765 int i;
1766
1767 /* calulate max size */
1768 for (i = 0; trace_options[i]; i++) {
1769 len += strlen(trace_options[i]);
1770 len += 3; /* "no" and space */
1771 }
1772
1773 /* +2 for \n and \0 */
1774 buf = kmalloc(len + 2, GFP_KERNEL);
1775 if (!buf)
1776 return -ENOMEM;
1777
1778 for (i = 0; trace_options[i]; i++) {
1779 if (trace_flags & (1 << i))
1780 r += sprintf(buf + r, "%s ", trace_options[i]);
1781 else
1782 r += sprintf(buf + r, "no%s ", trace_options[i]);
1783 }
1784
1785 r += sprintf(buf + r, "\n");
1786 WARN_ON(r >= len + 2);
1787
1788 r = simple_read_from_buffer(ubuf, cnt, ppos,
1789 buf, r);
1790
1791 kfree(buf);
1792
1793 return r;
1794}
1795
1796static ssize_t
1797tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1798 size_t cnt, loff_t *ppos)
1799{
1800 char buf[64];
1801 char *cmp = buf;
1802 int neg = 0;
1803 int i;
1804
1805 if (cnt > 63)
1806 cnt = 63;
1807
1808 if (copy_from_user(&buf, ubuf, cnt))
1809 return -EFAULT;
1810
1811 buf[cnt] = 0;
1812
1813 if (strncmp(buf, "no", 2) == 0) {
1814 neg = 1;
1815 cmp += 2;
1816 }
1817
1818 for (i = 0; trace_options[i]; i++) {
1819 int len = strlen(trace_options[i]);
1820
1821 if (strncmp(cmp, trace_options[i], len) == 0) {
1822 if (neg)
1823 trace_flags &= ~(1 << i);
1824 else
1825 trace_flags |= (1 << i);
1826 break;
1827 }
1828 }
1829
1830 filp->f_pos += cnt;
1831
1832 return cnt;
1833}
1834
1835static struct file_operations tracing_iter_fops = {
1836 .open = tracing_open_generic,
1837 .read = tracing_iter_ctrl_read,
1838 .write = tracing_iter_ctrl_write,
1839};
1840
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001841static const char readme_msg[] =
1842 "tracing mini-HOWTO:\n\n"
1843 "# mkdir /debug\n"
1844 "# mount -t debugfs nodev /debug\n\n"
1845 "# cat /debug/tracing/available_tracers\n"
1846 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1847 "# cat /debug/tracing/current_tracer\n"
1848 "none\n"
1849 "# echo sched_switch > /debug/tracing/current_tracer\n"
1850 "# cat /debug/tracing/current_tracer\n"
1851 "sched_switch\n"
1852 "# cat /debug/tracing/iter_ctrl\n"
1853 "noprint-parent nosym-offset nosym-addr noverbose\n"
1854 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1855 "# echo 1 > /debug/tracing/tracing_enabled\n"
1856 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1857 "echo 0 > /debug/tracing/tracing_enabled\n"
1858;
1859
1860static ssize_t
1861tracing_readme_read(struct file *filp, char __user *ubuf,
1862 size_t cnt, loff_t *ppos)
1863{
1864 return simple_read_from_buffer(ubuf, cnt, ppos,
1865 readme_msg, strlen(readme_msg));
1866}
1867
1868static struct file_operations tracing_readme_fops = {
1869 .open = tracing_open_generic,
1870 .read = tracing_readme_read,
1871};
1872
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001873static ssize_t
1874tracing_ctrl_read(struct file *filp, char __user *ubuf,
1875 size_t cnt, loff_t *ppos)
1876{
1877 struct trace_array *tr = filp->private_data;
1878 char buf[64];
1879 int r;
1880
1881 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001882 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001883}
1884
1885static ssize_t
1886tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1887 size_t cnt, loff_t *ppos)
1888{
1889 struct trace_array *tr = filp->private_data;
1890 long val;
1891 char buf[64];
1892
1893 if (cnt > 63)
1894 cnt = 63;
1895
1896 if (copy_from_user(&buf, ubuf, cnt))
1897 return -EFAULT;
1898
1899 buf[cnt] = 0;
1900
1901 val = simple_strtoul(buf, NULL, 10);
1902
1903 val = !!val;
1904
1905 mutex_lock(&trace_types_lock);
1906 if (tr->ctrl ^ val) {
1907 if (val)
1908 tracer_enabled = 1;
1909 else
1910 tracer_enabled = 0;
1911
1912 tr->ctrl = val;
1913
1914 if (current_trace && current_trace->ctrl_update)
1915 current_trace->ctrl_update(tr);
1916 }
1917 mutex_unlock(&trace_types_lock);
1918
1919 filp->f_pos += cnt;
1920
1921 return cnt;
1922}
1923
1924static ssize_t
1925tracing_set_trace_read(struct file *filp, char __user *ubuf,
1926 size_t cnt, loff_t *ppos)
1927{
1928 char buf[max_tracer_type_len+2];
1929 int r;
1930
1931 mutex_lock(&trace_types_lock);
1932 if (current_trace)
1933 r = sprintf(buf, "%s\n", current_trace->name);
1934 else
1935 r = sprintf(buf, "\n");
1936 mutex_unlock(&trace_types_lock);
1937
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001938 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001939}
1940
1941static ssize_t
1942tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1943 size_t cnt, loff_t *ppos)
1944{
1945 struct trace_array *tr = &global_trace;
1946 struct tracer *t;
1947 char buf[max_tracer_type_len+1];
1948 int i;
1949
1950 if (cnt > max_tracer_type_len)
1951 cnt = max_tracer_type_len;
1952
1953 if (copy_from_user(&buf, ubuf, cnt))
1954 return -EFAULT;
1955
1956 buf[cnt] = 0;
1957
1958 /* strip ending whitespace. */
1959 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1960 buf[i] = 0;
1961
1962 mutex_lock(&trace_types_lock);
1963 for (t = trace_types; t; t = t->next) {
1964 if (strcmp(t->name, buf) == 0)
1965 break;
1966 }
1967 if (!t || t == current_trace)
1968 goto out;
1969
1970 if (current_trace && current_trace->reset)
1971 current_trace->reset(tr);
1972
1973 current_trace = t;
1974 if (t->init)
1975 t->init(tr);
1976
1977 out:
1978 mutex_unlock(&trace_types_lock);
1979
1980 filp->f_pos += cnt;
1981
1982 return cnt;
1983}
1984
1985static ssize_t
1986tracing_max_lat_read(struct file *filp, char __user *ubuf,
1987 size_t cnt, loff_t *ppos)
1988{
1989 unsigned long *ptr = filp->private_data;
1990 char buf[64];
1991 int r;
1992
1993 r = snprintf(buf, 64, "%ld\n",
1994 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1995 if (r > 64)
1996 r = 64;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001997 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001998}
1999
2000static ssize_t
2001tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2002 size_t cnt, loff_t *ppos)
2003{
2004 long *ptr = filp->private_data;
2005 long val;
2006 char buf[64];
2007
2008 if (cnt > 63)
2009 cnt = 63;
2010
2011 if (copy_from_user(&buf, ubuf, cnt))
2012 return -EFAULT;
2013
2014 buf[cnt] = 0;
2015
2016 val = simple_strtoul(buf, NULL, 10);
2017
2018 *ptr = val * 1000;
2019
2020 return cnt;
2021}
2022
Steven Rostedtb3806b42008-05-12 21:20:46 +02002023static atomic_t tracing_reader;
2024
2025static int tracing_open_pipe(struct inode *inode, struct file *filp)
2026{
2027 struct trace_iterator *iter;
2028
2029 if (tracing_disabled)
2030 return -ENODEV;
2031
2032 /* We only allow for reader of the pipe */
2033 if (atomic_inc_return(&tracing_reader) != 1) {
2034 atomic_dec(&tracing_reader);
2035 return -EBUSY;
2036 }
2037
2038 /* create a buffer to store the information to pass to userspace */
2039 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2040 if (!iter)
2041 return -ENOMEM;
2042
2043 iter->tr = &global_trace;
2044
2045 filp->private_data = iter;
2046
2047 return 0;
2048}
2049
2050static int tracing_release_pipe(struct inode *inode, struct file *file)
2051{
2052 struct trace_iterator *iter = file->private_data;
2053
2054 kfree(iter);
2055 atomic_dec(&tracing_reader);
2056
2057 return 0;
2058}
2059
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002060static unsigned int
2061tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2062{
2063 struct trace_iterator *iter = filp->private_data;
2064
2065 if (trace_flags & TRACE_ITER_BLOCK) {
2066 /*
2067 * Always select as readable when in blocking mode
2068 */
2069 return POLLIN | POLLRDNORM;
2070 }
2071 else {
2072 if (!trace_empty(iter))
2073 return POLLIN | POLLRDNORM;
2074 poll_wait(filp, &trace_wait, poll_table);
2075 if (!trace_empty(iter))
2076 return POLLIN | POLLRDNORM;
2077
2078 return 0;
2079 }
2080}
2081
Steven Rostedtb3806b42008-05-12 21:20:46 +02002082/*
2083 * Consumer reader.
2084 */
2085static ssize_t
2086tracing_read_pipe(struct file *filp, char __user *ubuf,
2087 size_t cnt, loff_t *ppos)
2088{
2089 struct trace_iterator *iter = filp->private_data;
2090 struct trace_array_cpu *data;
2091 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002092 static int start;
2093 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002094#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002095 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002096#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002097 int read = 0;
2098 int cpu;
2099 int len;
2100 int ret;
2101
2102 /* return any leftover data */
2103 if (iter->seq.len > start) {
2104 len = iter->seq.len - start;
2105 if (cnt > len)
2106 cnt = len;
2107 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2108 if (ret)
2109 cnt = -EFAULT;
2110
2111 start += len;
2112
2113 return cnt;
2114 }
2115
2116 trace_seq_reset(&iter->seq);
2117 start = 0;
2118
2119 while (trace_empty(iter)) {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002120 if (!(trace_flags & TRACE_ITER_BLOCK))
2121 return -EWOULDBLOCK;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002122 /*
2123 * This is a make-shift waitqueue. The reason we don't use
2124 * an actual wait queue is because:
2125 * 1) we only ever have one waiter
2126 * 2) the tracing, traces all functions, we don't want
2127 * the overhead of calling wake_up and friends
2128 * (and tracing them too)
2129 * Anyway, this is really very primitive wakeup.
2130 */
2131 set_current_state(TASK_INTERRUPTIBLE);
2132 iter->tr->waiter = current;
2133
2134 /* sleep for one second, and try again. */
2135 schedule_timeout(HZ);
2136
2137 iter->tr->waiter = NULL;
2138
2139 if (signal_pending(current))
2140 return -EINTR;
2141
2142 /*
2143 * We block until we read something and tracing is disabled.
2144 * We still block if tracing is disabled, but we have never
2145 * read anything. This allows a user to cat this file, and
2146 * then enable tracing. But after we have read something,
2147 * we give an EOF when tracing is again disabled.
2148 *
2149 * iter->pos will be 0 if we haven't read anything.
2150 */
2151 if (!tracer_enabled && iter->pos)
2152 break;
2153
2154 continue;
2155 }
2156
2157 /* stop when tracing is finished */
2158 if (trace_empty(iter))
2159 return 0;
2160
2161 if (cnt >= PAGE_SIZE)
2162 cnt = PAGE_SIZE - 1;
2163
2164 memset(iter, 0, sizeof(*iter));
2165 iter->tr = &global_trace;
2166 iter->pos = -1;
2167
2168 /*
2169 * We need to stop all tracing on all CPUS to read the
2170 * the next buffer. This is a bit expensive, but is
2171 * not done often. We fill all what we can read,
2172 * and then release the locks again.
2173 */
2174
2175 cpus_clear(mask);
2176 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002177#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002178 ftrace_save = ftrace_enabled;
2179 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002180#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002181 smp_wmb();
Steven Rostedtb3806b42008-05-12 21:20:46 +02002182 for_each_possible_cpu(cpu) {
2183 data = iter->tr->data[cpu];
2184
2185 if (!head_page(data) || !data->trace_idx)
2186 continue;
2187
2188 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002189 cpu_set(cpu, mask);
2190 }
2191
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002192 for_each_cpu_mask(cpu, mask) {
2193 data = iter->tr->data[cpu];
2194 spin_lock(&data->lock);
2195 }
2196
Steven Rostedt088b1e422008-05-12 21:20:48 +02002197 while (find_next_entry_inc(iter) != NULL) {
2198 int len = iter->seq.len;
2199
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002200 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002201 if (!ret) {
2202 /* don't print partial lines */
2203 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002204 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002205 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002206
2207 trace_consume(iter);
2208
2209 if (iter->seq.len >= cnt)
2210 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002211 }
2212
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002213 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002214 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002215 spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002216 }
2217
2218 for_each_cpu_mask(cpu, mask) {
2219 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002220 atomic_dec(&data->disabled);
2221 }
Ingo Molnar25770462008-05-12 21:20:49 +02002222#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002223 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002224#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002225 local_irq_restore(flags);
2226
2227 /* Now copy what we have to the user */
2228 read = iter->seq.len;
2229 if (read > cnt)
2230 read = cnt;
2231
2232 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2233
2234 if (read < iter->seq.len)
2235 start = read;
2236 else
2237 trace_seq_reset(&iter->seq);
2238
2239 if (ret)
2240 read = -EFAULT;
2241
2242 return read;
2243}
2244
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002245static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002246 .open = tracing_open_generic,
2247 .read = tracing_max_lat_read,
2248 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002249};
2250
2251static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002252 .open = tracing_open_generic,
2253 .read = tracing_ctrl_read,
2254 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002255};
2256
2257static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002258 .open = tracing_open_generic,
2259 .read = tracing_set_trace_read,
2260 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002261};
2262
Steven Rostedtb3806b42008-05-12 21:20:46 +02002263static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002264 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002265 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002266 .read = tracing_read_pipe,
2267 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002268};
2269
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002270#ifdef CONFIG_DYNAMIC_FTRACE
2271
2272static ssize_t
2273tracing_read_long(struct file *filp, char __user *ubuf,
2274 size_t cnt, loff_t *ppos)
2275{
2276 unsigned long *p = filp->private_data;
2277 char buf[64];
2278 int r;
2279
2280 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002281
2282 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002283}
2284
2285static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002286 .open = tracing_open_generic,
2287 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002288};
2289#endif
2290
2291static struct dentry *d_tracer;
2292
2293struct dentry *tracing_init_dentry(void)
2294{
2295 static int once;
2296
2297 if (d_tracer)
2298 return d_tracer;
2299
2300 d_tracer = debugfs_create_dir("tracing", NULL);
2301
2302 if (!d_tracer && !once) {
2303 once = 1;
2304 pr_warning("Could not create debugfs directory 'tracing'\n");
2305 return NULL;
2306 }
2307
2308 return d_tracer;
2309}
2310
Steven Rostedt60a11772008-05-12 21:20:44 +02002311#ifdef CONFIG_FTRACE_SELFTEST
2312/* Let selftest have access to static functions in this file */
2313#include "trace_selftest.c"
2314#endif
2315
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002316static __init void tracer_init_debugfs(void)
2317{
2318 struct dentry *d_tracer;
2319 struct dentry *entry;
2320
2321 d_tracer = tracing_init_dentry();
2322
2323 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2324 &global_trace, &tracing_ctrl_fops);
2325 if (!entry)
2326 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2327
2328 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2329 NULL, &tracing_iter_fops);
2330 if (!entry)
2331 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2332
2333 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2334 &global_trace, &tracing_lt_fops);
2335 if (!entry)
2336 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2337
2338 entry = debugfs_create_file("trace", 0444, d_tracer,
2339 &global_trace, &tracing_fops);
2340 if (!entry)
2341 pr_warning("Could not create debugfs 'trace' entry\n");
2342
2343 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2344 &global_trace, &show_traces_fops);
2345 if (!entry)
2346 pr_warning("Could not create debugfs 'trace' entry\n");
2347
2348 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2349 &global_trace, &set_tracer_fops);
2350 if (!entry)
2351 pr_warning("Could not create debugfs 'trace' entry\n");
2352
2353 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2354 &tracing_max_latency,
2355 &tracing_max_lat_fops);
2356 if (!entry)
2357 pr_warning("Could not create debugfs "
2358 "'tracing_max_latency' entry\n");
2359
2360 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2361 &tracing_thresh, &tracing_max_lat_fops);
2362 if (!entry)
2363 pr_warning("Could not create debugfs "
2364 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002365 entry = debugfs_create_file("README", 0644, d_tracer,
2366 NULL, &tracing_readme_fops);
2367 if (!entry)
2368 pr_warning("Could not create debugfs 'README' entry\n");
2369
Steven Rostedtb3806b42008-05-12 21:20:46 +02002370 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2371 NULL, &tracing_pipe_fops);
2372 if (!entry)
2373 pr_warning("Could not create debugfs "
2374 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002375
2376#ifdef CONFIG_DYNAMIC_FTRACE
2377 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2378 &ftrace_update_tot_cnt,
2379 &tracing_read_long_fops);
2380 if (!entry)
2381 pr_warning("Could not create debugfs "
2382 "'dyn_ftrace_total_info' entry\n");
2383#endif
2384}
2385
2386/* dummy trace to disable tracing */
2387static struct tracer no_tracer __read_mostly =
2388{
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002389 .name = "none",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002390};
2391
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002392static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002393{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002394 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002395 struct page *page, *tmp;
2396 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002397 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002398 int i;
2399
2400 /* first allocate a page for each CPU */
2401 for_each_possible_cpu(i) {
2402 array = (void *)__get_free_page(GFP_KERNEL);
2403 if (array == NULL) {
2404 printk(KERN_ERR "tracer: failed to allocate page"
2405 "for trace buffer!\n");
2406 goto free_pages;
2407 }
2408
2409 page = virt_to_page(array);
2410 list_add(&page->lru, &pages);
2411
2412/* Only allocate if we are actually using the max trace */
2413#ifdef CONFIG_TRACER_MAX_TRACE
2414 array = (void *)__get_free_page(GFP_KERNEL);
2415 if (array == NULL) {
2416 printk(KERN_ERR "tracer: failed to allocate page"
2417 "for trace buffer!\n");
2418 goto free_pages;
2419 }
2420 page = virt_to_page(array);
2421 list_add(&page->lru, &pages);
2422#endif
2423 }
2424
2425 /* Now that we successfully allocate a page per CPU, add them */
2426 for_each_possible_cpu(i) {
2427 data = global_trace.data[i];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002428 spin_lock_init(&data->lock);
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002429 lockdep_set_class(&data->lock, &data->lock_key);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002430 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002431 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002432 list_add_tail(&page->lru, &data->trace_pages);
2433 ClearPageLRU(page);
2434
2435#ifdef CONFIG_TRACER_MAX_TRACE
2436 data = max_tr.data[i];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002437 spin_lock_init(&data->lock);
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002438 lockdep_set_class(&data->lock, &data->lock_key);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002439 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002440 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002441 list_add_tail(&page->lru, &data->trace_pages);
2442 SetPageLRU(page);
2443#endif
2444 }
2445 global_trace.entries += ENTRIES_PER_PAGE;
2446
2447 return 0;
2448
2449 free_pages:
2450 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002451 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002452 __free_page(page);
2453 }
2454 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002455}
2456
2457__init static int tracer_alloc_buffers(void)
2458{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002459 struct trace_array_cpu *data;
2460 void *array;
2461 struct page *page;
2462 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002463 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002464 int i;
2465
Steven Rostedt26994ea2008-05-12 21:20:48 +02002466 global_trace.ctrl = tracer_enabled;
2467
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002468 /* Allocate the first page for all buffers */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002469 for_each_possible_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002470 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002471 max_tr.data[i] = &per_cpu(max_data, i);
2472
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002473 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002474 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002475 printk(KERN_ERR "tracer: failed to allocate page"
2476 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002477 goto free_buffers;
2478 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002479
2480 /* set the array to the list */
2481 INIT_LIST_HEAD(&data->trace_pages);
2482 page = virt_to_page(array);
2483 list_add(&page->lru, &data->trace_pages);
2484 /* use the LRU flag to differentiate the two buffers */
2485 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002486
2487/* Only allocate if we are actually using the max trace */
2488#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002489 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002490 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002491 printk(KERN_ERR "tracer: failed to allocate page"
2492 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002493 goto free_buffers;
2494 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002495
2496 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2497 page = virt_to_page(array);
2498 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2499 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002500#endif
2501 }
2502
2503 /*
2504 * Since we allocate by orders of pages, we may be able to
2505 * round up a bit.
2506 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002507 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002508 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002509
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002510 while (global_trace.entries < trace_nr_entries) {
2511 if (trace_alloc_page())
2512 break;
2513 pages++;
2514 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002515 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002516
2517 pr_info("tracer: %d pages allocated for %ld",
2518 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002519 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2520 pr_info(" actual entries %ld\n", global_trace.entries);
2521
2522 tracer_init_debugfs();
2523
2524 trace_init_cmdlines();
2525
2526 register_tracer(&no_tracer);
2527 current_trace = &no_tracer;
2528
Steven Rostedt60a11772008-05-12 21:20:44 +02002529 /* All seems OK, enable tracing */
2530 tracing_disabled = 0;
2531
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002532 return 0;
2533
2534 free_buffers:
2535 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002536 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002537 struct trace_array_cpu *data = global_trace.data[i];
2538
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002539 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002540 list_for_each_entry_safe(page, tmp,
2541 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002542 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002543 __free_page(page);
2544 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002545 }
2546
2547#ifdef CONFIG_TRACER_MAX_TRACE
2548 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002549 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002550 list_for_each_entry_safe(page, tmp,
2551 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002552 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002553 __free_page(page);
2554 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002555 }
2556#endif
2557 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002558 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002559}
Steven Rostedt60a11772008-05-12 21:20:44 +02002560fs_initcall(tracer_alloc_buffers);