]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/trace/trace.c
ftrace: trace curr/next tasks
[net-next-2.6.git] / kernel / trace / trace.c
CommitLineData
bc0c38d1
SR
1/*
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>
4c11d7ae 18#include <linux/pagemap.h>
bc0c38d1
SR
19#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>
2a2cc8f7 27#include <linux/poll.h>
bc0c38d1
SR
28#include <linux/gfp.h>
29#include <linux/fs.h>
30
86387f7e
IM
31#include <linux/stacktrace.h>
32
bc0c38d1
SR
33#include "trace.h"
34
35unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
36unsigned long __read_mostly tracing_thresh;
37
60a11774
SR
38static int tracing_disabled = 1;
39
e309b41d 40static long
bc0c38d1
SR
41ns2usecs(cycle_t nsec)
42{
43 nsec += 500;
44 do_div(nsec, 1000);
45 return nsec;
46}
47
e309b41d 48cycle_t ftrace_now(int cpu)
750ed1a4 49{
0fd9e0da 50 return cpu_clock(cpu);
750ed1a4
IM
51}
52
bc0c38d1
SR
53static 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
26994ead 61static int tracer_enabled = 1;
57422797 62static unsigned long trace_nr_entries = 65536UL;
bc0c38d1
SR
63
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);
4e655519
IM
69static 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}
bc0c38d1 81
4c11d7ae
SR
82#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
83
bc0c38d1
SR
84static 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
57f50be1
SR
93unsigned long nsecs_to_usecs(unsigned long nsecs)
94{
95 return nsecs / 1000;
96}
97
bc0c38d1
SR
98enum trace_type {
99 __TRACE_FIRST_TYPE = 0,
100
101 TRACE_FN,
102 TRACE_CTX,
57422797 103 TRACE_WAKE,
86387f7e 104 TRACE_STACK,
f0a920d5 105 TRACE_SPECIAL,
bc0c38d1
SR
106
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
bc0c38d1
SR
117#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",
f9896bf3 126 "raw",
5e3ca0ec 127 "hex",
cb0f12aa 128 "bin",
2a2cc8f7 129 "block",
86387f7e 130 "stacktrace",
bc0c38d1
SR
131 NULL
132};
133
4c11d7ae 134static DEFINE_SPINLOCK(ftrace_max_lock);
bc0c38d1
SR
135
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 */
e309b41d 141static void
bc0c38d1
SR
142__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
c7aafc54
IM
163void 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
e309b41d 190static int
214023c3
SR
191trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
192{
193 int len = (PAGE_SIZE - 1) - s->len;
194 va_list ap;
b3806b43 195 int ret;
214023c3
SR
196
197 if (!len)
198 return 0;
199
200 va_start(ap, fmt);
b3806b43 201 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
214023c3
SR
202 va_end(ap);
203
b3806b43
SR
204 /* If we can't write it all, don't bother writing anything */
205 if (ret > len)
206 return 0;
207
208 s->len += ret;
214023c3
SR
209
210 return len;
211}
212
e309b41d 213static int
214023c3
SR
214trace_seq_puts(struct trace_seq *s, const char *str)
215{
216 int len = strlen(str);
217
218 if (len > ((PAGE_SIZE - 1) - s->len))
b3806b43 219 return 0;
214023c3
SR
220
221 memcpy(s->buffer + s->len, str, len);
222 s->len += len;
223
224 return len;
225}
226
e309b41d 227static int
214023c3
SR
228trace_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
e309b41d 238static int
cb0f12aa
IM
239trace_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
5e3ca0ec
IM
250#define HEX_CHARS 17
251
e309b41d 252static int
5e3ca0ec
IM
253trace_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
e309b41d 291static void
214023c3
SR
292trace_seq_reset(struct trace_seq *s)
293{
294 s->len = 0;
295}
296
e309b41d 297static void
214023c3
SR
298trace_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
e309b41d 308static void
c7aafc54
IM
309flip_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
93a588f4 315 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
c7aafc54 316 sizeof(struct trace_array_cpu) -
93a588f4 317 offsetof(struct trace_array_cpu, trace_head_idx));
c7aafc54
IM
318
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
e309b41d 329void
bc0c38d1
SR
330update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
331{
332 struct trace_array_cpu *data;
bc0c38d1
SR
333 int i;
334
4c11d7ae
SR
335 WARN_ON_ONCE(!irqs_disabled());
336 spin_lock(&ftrace_max_lock);
bc0c38d1
SR
337 /* clear out all the previous traces */
338 for_each_possible_cpu(i) {
339 data = tr->data[i];
c7aafc54 340 flip_trace(max_tr.data[i], data);
89b2f978 341 tracing_reset(data);
bc0c38d1
SR
342 }
343
344 __update_max_tr(tr, tsk, cpu);
4c11d7ae 345 spin_unlock(&ftrace_max_lock);
bc0c38d1
SR
346}
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 */
e309b41d 354void
bc0c38d1
SR
355update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
356{
357 struct trace_array_cpu *data = tr->data[cpu];
bc0c38d1
SR
358 int i;
359
4c11d7ae
SR
360 WARN_ON_ONCE(!irqs_disabled());
361 spin_lock(&ftrace_max_lock);
bc0c38d1
SR
362 for_each_possible_cpu(i)
363 tracing_reset(max_tr.data[i]);
364
c7aafc54 365 flip_trace(max_tr.data[cpu], data);
89b2f978 366 tracing_reset(data);
bc0c38d1
SR
367
368 __update_max_tr(tr, tsk, cpu);
4c11d7ae 369 spin_unlock(&ftrace_max_lock);
bc0c38d1
SR
370}
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
60a11774
SR
394#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) {
60a11774 409 data = tr->data[i];
c7aafc54
IM
410 if (!head_page(data))
411 continue;
60a11774
SR
412 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 }
1d4db00a
SR
426 /* 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 }
60a11774
SR
433 printk(KERN_CONT "PASSED\n");
434 }
435#endif
436
bc0c38d1
SR
437 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;
60a11774 442
bc0c38d1
SR
443 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
e309b41d 477void tracing_reset(struct trace_array_cpu *data)
bc0c38d1
SR
478{
479 data->trace_idx = 0;
93a588f4
SR
480 data->trace_head = data->trace_tail = head_page(data);
481 data->trace_head_idx = 0;
482 data->trace_tail_idx = 0;
bc0c38d1
SR
483}
484
bc0c38d1
SR
485#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
e309b41d 500void trace_stop_cmdline_recording(void);
bc0c38d1 501
e309b41d 502static void trace_save_cmdline(struct task_struct *tsk)
bc0c38d1
SR
503{
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
e309b41d 537static char *trace_find_cmdline(int pid)
bc0c38d1
SR
538{
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
e309b41d 558void tracing_record_cmdline(struct task_struct *tsk)
bc0c38d1
SR
559{
560 if (atomic_read(&trace_record_cmdline_disabled))
561 return;
562
563 trace_save_cmdline(tsk);
564}
565
e309b41d 566static inline struct list_head *
93a588f4
SR
567trace_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
e309b41d 580static inline void *
93a588f4
SR
581trace_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
e309b41d 594static inline struct trace_entry *
c7aafc54 595tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
bc0c38d1
SR
596{
597 unsigned long idx, idx_next;
598 struct trace_entry *entry;
599
4c11d7ae 600 data->trace_idx++;
93a588f4 601 idx = data->trace_head_idx;
bc0c38d1
SR
602 idx_next = idx + 1;
603
c7aafc54
IM
604 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
605
93a588f4 606 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
4c11d7ae
SR
607
608 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
93a588f4 609 data->trace_head = trace_next_page(data, data->trace_head);
bc0c38d1
SR
610 idx_next = 0;
611 }
612
93a588f4
SR
613 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;
bc0c38d1
SR
625
626 return entry;
627}
628
e309b41d 629static inline void
c7aafc54 630tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
bc0c38d1
SR
631{
632 struct task_struct *tsk = current;
633 unsigned long pc;
634
635 pc = preempt_count();
636
c7aafc54
IM
637 entry->preempt_count = pc & 0xff;
638 entry->pid = tsk->pid;
750ed1a4 639 entry->t = ftrace_now(raw_smp_processor_id());
bc0c38d1
SR
640 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
e309b41d 646void
6fb44b71
SR
647trace_function(struct trace_array *tr, struct trace_array_cpu *data,
648 unsigned long ip, unsigned long parent_ip, unsigned long flags)
bc0c38d1
SR
649{
650 struct trace_entry *entry;
dcb6308f 651 unsigned long irq_flags;
bc0c38d1 652
dcb6308f 653 spin_lock_irqsave(&data->lock, irq_flags);
c7aafc54 654 entry = tracing_get_trace_entry(tr, data);
bc0c38d1 655 tracing_generic_entry_update(entry, flags);
c7aafc54
IM
656 entry->type = TRACE_FN;
657 entry->fn.ip = ip;
658 entry->fn.parent_ip = parent_ip;
dcb6308f 659 spin_unlock_irqrestore(&data->lock, irq_flags);
bc0c38d1
SR
660}
661
e309b41d 662void
2e0f5761
IM
663ftrace(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)))
6fb44b71 667 trace_function(tr, data, ip, parent_ip, flags);
2e0f5761
IM
668}
669
4e655519
IM
670#ifdef CONFIG_CONTEXT_SWITCH_TRACER
671
e309b41d 672void
4e655519
IM
673__trace_special(void *__tr, void *__data,
674 unsigned long arg1, unsigned long arg2, unsigned long arg3)
f0a920d5 675{
4e655519
IM
676 struct trace_array_cpu *data = __data;
677 struct trace_array *tr = __tr;
f0a920d5 678 struct trace_entry *entry;
dcb6308f 679 unsigned long irq_flags;
f0a920d5 680
dcb6308f 681 spin_lock_irqsave(&data->lock, irq_flags);
f0a920d5
IM
682 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;
dcb6308f 688 spin_unlock_irqrestore(&data->lock, irq_flags);
86387f7e
IM
689}
690
4e655519
IM
691#endif
692
86387f7e
IM
693void __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);
f0a920d5
IM
716}
717
e309b41d 718void
bc0c38d1
SR
719tracing_sched_switch_trace(struct trace_array *tr,
720 struct trace_array_cpu *data,
86387f7e
IM
721 struct task_struct *prev,
722 struct task_struct *next,
bc0c38d1
SR
723 unsigned long flags)
724{
725 struct trace_entry *entry;
dcb6308f 726 unsigned long irq_flags;
bc0c38d1 727
dcb6308f 728 spin_lock_irqsave(&data->lock, irq_flags);
c7aafc54 729 entry = tracing_get_trace_entry(tr, data);
bc0c38d1
SR
730 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;
86387f7e 737 __trace_stack(tr, data, flags, 4);
dcb6308f 738 spin_unlock_irqrestore(&data->lock, irq_flags);
bc0c38d1
SR
739}
740
57422797
IM
741void
742tracing_sched_wakeup_trace(struct trace_array *tr,
743 struct trace_array_cpu *data,
86387f7e
IM
744 struct task_struct *wakee,
745 struct task_struct *curr,
57422797
IM
746 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;
86387f7e 760 __trace_stack(tr, data, flags, 5);
57422797 761 spin_unlock_irqrestore(&data->lock, irq_flags);
57422797
IM
762}
763
2e0f5761 764#ifdef CONFIG_FTRACE
e309b41d 765static void
2e0f5761
IM
766function_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))
6fb44b71 783 trace_function(tr, data, ip, parent_ip, flags);
2e0f5761
IM
784
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
e309b41d 794void tracing_start_function_trace(void)
2e0f5761
IM
795{
796 register_ftrace_function(&trace_ops);
797}
798
e309b41d 799void tracing_stop_function_trace(void)
2e0f5761
IM
800{
801 unregister_ftrace_function(&trace_ops);
802}
803#endif
804
bc0c38d1
SR
805enum trace_file_type {
806 TRACE_FILE_LAT_FMT = 1,
807};
808
809static struct trace_entry *
4c11d7ae
SR
810trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
811 struct trace_iterator *iter, int cpu)
bc0c38d1 812{
4c11d7ae
SR
813 struct page *page;
814 struct trace_entry *array;
bc0c38d1 815
4c11d7ae 816 if (iter->next_idx[cpu] >= tr->entries ||
b3806b43
SR
817 iter->next_idx[cpu] >= data->trace_idx ||
818 (data->trace_head == data->trace_tail &&
819 data->trace_head_idx == data->trace_tail_idx))
bc0c38d1
SR
820 return NULL;
821
4c11d7ae 822 if (!iter->next_page[cpu]) {
93a588f4
SR
823 /* 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;
4c11d7ae 828 }
bc0c38d1 829
4c11d7ae 830 page = list_entry(iter->next_page[cpu], struct page, lru);
c7aafc54
IM
831 BUG_ON(&data->trace_pages == &page->lru);
832
4c11d7ae
SR
833 array = page_address(page);
834
93a588f4 835 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
4c11d7ae 836 return &array[iter->next_page_idx[cpu]];
bc0c38d1
SR
837}
838
e309b41d 839static struct trace_entry *
bc0c38d1
SR
840find_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) {
c7aafc54 848 if (!head_page(tr->data[cpu]))
bc0c38d1 849 continue;
4c11d7ae 850 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
cdd31cd2
IM
851 /*
852 * Pick the entry with the smallest timestamp:
853 */
854 if (ent && (!next || ent->t < next->t)) {
bc0c38d1
SR
855 next = ent;
856 next_cpu = cpu;
857 }
858 }
859
860 if (ent_cpu)
861 *ent_cpu = next_cpu;
862
863 return next;
864}
865
e309b41d 866static void trace_iterator_increment(struct trace_iterator *iter)
bc0c38d1 867{
b3806b43
SR
868 iter->idx++;
869 iter->next_idx[iter->cpu]++;
870 iter->next_page_idx[iter->cpu]++;
8c523a9d 871
b3806b43
SR
872 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
873 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
bc0c38d1 874
b3806b43
SR
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}
bc0c38d1 880
e309b41d 881static void trace_consume(struct trace_iterator *iter)
b3806b43
SR
882{
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 }
4e3c3333 890
b3806b43
SR
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;
b3806b43
SR
895}
896
e309b41d 897static void *find_next_entry_inc(struct trace_iterator *iter)
b3806b43
SR
898{
899 struct trace_entry *next;
900 int next_cpu = -1;
901
902 next = find_next_entry(iter, &next_cpu);
93a588f4 903
4e3c3333
IM
904 iter->prev_ent = iter->ent;
905 iter->prev_cpu = iter->cpu;
906
bc0c38d1
SR
907 iter->ent = next;
908 iter->cpu = next_cpu;
909
b3806b43
SR
910 if (next)
911 trace_iterator_increment(iter);
912
bc0c38d1
SR
913 return next ? iter : NULL;
914}
915
e309b41d 916static void *s_next(struct seq_file *m, void *v, loff_t *pos)
bc0c38d1
SR
917{
918 struct trace_iterator *iter = m->private;
bc0c38d1
SR
919 void *last_ent = iter->ent;
920 int i = (int)*pos;
4e3c3333 921 void *ent;
bc0c38d1
SR
922
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;
4e3c3333
IM
967 iter->prev_ent = NULL;
968 iter->prev_cpu = -1;
bc0c38d1 969
4c11d7ae 970 for_each_possible_cpu(i) {
bc0c38d1 971 iter->next_idx[i] = 0;
4c11d7ae
SR
972 iter->next_page[i] = NULL;
973 }
bc0c38d1
SR
974
975 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
976 ;
977
978 } else {
4c11d7ae 979 l = *pos - 1;
bc0c38d1
SR
980 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
b3806b43 999static int
214023c3 1000seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
bc0c38d1
SR
1001{
1002#ifdef CONFIG_KALLSYMS
1003 char str[KSYM_SYMBOL_LEN];
1004
1005 kallsyms_lookup(address, NULL, NULL, NULL, str);
1006
b3806b43 1007 return trace_seq_printf(s, fmt, str);
bc0c38d1 1008#endif
b3806b43 1009 return 1;
bc0c38d1
SR
1010}
1011
b3806b43 1012static int
214023c3
SR
1013seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1014 unsigned long address)
bc0c38d1
SR
1015{
1016#ifdef CONFIG_KALLSYMS
1017 char str[KSYM_SYMBOL_LEN];
1018
1019 sprint_symbol(str, address);
b3806b43 1020 return trace_seq_printf(s, fmt, str);
bc0c38d1 1021#endif
b3806b43 1022 return 1;
bc0c38d1
SR
1023}
1024
1025#ifndef CONFIG_64BIT
1026# define IP_FMT "%08lx"
1027#else
1028# define IP_FMT "%016lx"
1029#endif
1030
e309b41d 1031static int
214023c3 1032seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
bc0c38d1 1033{
b3806b43
SR
1034 int ret;
1035
1036 if (!ip)
1037 return trace_seq_printf(s, "0");
bc0c38d1
SR
1038
1039 if (sym_flags & TRACE_ITER_SYM_OFFSET)
b3806b43 1040 ret = seq_print_sym_offset(s, "%s", ip);
bc0c38d1 1041 else
b3806b43
SR
1042 ret = seq_print_sym_short(s, "%s", ip);
1043
1044 if (!ret)
1045 return 0;
bc0c38d1
SR
1046
1047 if (sym_flags & TRACE_ITER_SYM_ADDR)
b3806b43
SR
1048 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1049 return ret;
bc0c38d1
SR
1050}
1051
e309b41d 1052static void print_lat_help_header(struct seq_file *m)
bc0c38d1
SR
1053{
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
e309b41d 1065static void print_func_help_header(struct seq_file *m)
bc0c38d1
SR
1066{
1067 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1068 seq_puts(m, "# | | | | |\n");
1069}
1070
1071
e309b41d 1072static void
bc0c38d1
SR
1073print_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;
4c11d7ae
SR
1079 unsigned long total = 0;
1080 unsigned long entries = 0;
bc0c38d1
SR
1081 int cpu;
1082 const char *name = "preemption";
1083
1084 if (type)
1085 name = type->name;
1086
1087 for_each_possible_cpu(cpu) {
c7aafc54 1088 if (head_page(tr->data[cpu])) {
4c11d7ae
SR
1089 total += tr->data[cpu]->trace_idx;
1090 if (tr->data[cpu]->trace_idx > tr->entries)
bc0c38d1 1091 entries += tr->entries;
4c11d7ae 1092 else
bc0c38d1
SR
1093 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",
57f50be1 1103 nsecs_to_usecs(data->saved_latency),
bc0c38d1 1104 entries,
4c11d7ae 1105 total,
bc0c38d1
SR
1106 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: ");
214023c3
SR
1132 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1133 trace_print_seq(m, &iter->seq);
bc0c38d1 1134 seq_puts(m, "\n => ended at: ");
214023c3
SR
1135 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1136 trace_print_seq(m, &iter->seq);
bc0c38d1
SR
1137 seq_puts(m, "\n");
1138 }
1139
1140 seq_puts(m, "\n");
1141}
1142
e309b41d 1143static void
214023c3 1144lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
bc0c38d1
SR
1145{
1146 int hardirq, softirq;
1147 char *comm;
1148
1149 comm = trace_find_cmdline(entry->pid);
1150
214023c3
SR
1151 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' : '.'));
bc0c38d1
SR
1156
1157 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1158 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1159 if (hardirq && softirq)
214023c3 1160 trace_seq_putc(s, 'H');
bc0c38d1
SR
1161 else {
1162 if (hardirq)
214023c3 1163 trace_seq_putc(s, 'h');
bc0c38d1
SR
1164 else {
1165 if (softirq)
214023c3 1166 trace_seq_putc(s, 's');
bc0c38d1 1167 else
214023c3 1168 trace_seq_putc(s, '.');
bc0c38d1
SR
1169 }
1170 }
1171
1172 if (entry->preempt_count)
214023c3 1173 trace_seq_printf(s, "%x", entry->preempt_count);
bc0c38d1 1174 else
214023c3 1175 trace_seq_puts(s, ".");
bc0c38d1
SR
1176}
1177
1178unsigned long preempt_mark_thresh = 100;
1179
e309b41d 1180static void
214023c3 1181lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
bc0c38d1
SR
1182 unsigned long rel_usecs)
1183{
214023c3 1184 trace_seq_printf(s, " %4lldus", abs_usecs);
bc0c38d1 1185 if (rel_usecs > preempt_mark_thresh)
214023c3 1186 trace_seq_puts(s, "!: ");
bc0c38d1 1187 else if (rel_usecs > 1)
214023c3 1188 trace_seq_puts(s, "+: ");
bc0c38d1 1189 else
214023c3 1190 trace_seq_puts(s, " : ");
bc0c38d1
SR
1191}
1192
1193static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1194
e309b41d 1195static int
214023c3 1196print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
bc0c38d1 1197{
214023c3 1198 struct trace_seq *s = &iter->seq;
bc0c38d1
SR
1199 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;
86387f7e 1207 int i;
bc0c38d1
SR
1208
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);
214023c3
SR
1216 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);
bc0c38d1 1225 } else {
86387f7e
IM
1226 if (entry->type != TRACE_STACK) {
1227 lat_print_generic(s, entry, cpu);
1228 lat_print_timestamp(s, abs_usecs, rel_usecs);
1229 }
bc0c38d1
SR
1230 }
1231 switch (entry->type) {
1232 case TRACE_FN:
214023c3
SR
1233 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");
bc0c38d1
SR
1237 break;
1238 case TRACE_CTX:
57422797 1239 case TRACE_WAKE:
bc0c38d1
SR
1240 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);
57422797 1243 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d %s\n",
214023c3
SR
1244 entry->ctx.prev_pid,
1245 entry->ctx.prev_prio,
57422797 1246 S, entry->type == TRACE_CTX ? "==>" : " +",
214023c3
SR
1247 entry->ctx.next_pid,
1248 entry->ctx.next_prio,
1249 comm);
bc0c38d1 1250 break;
f0a920d5 1251 case TRACE_SPECIAL:
4e655519 1252 trace_seq_printf(s, " %ld %ld %ld\n",
f0a920d5
IM
1253 entry->special.arg1,
1254 entry->special.arg2,
1255 entry->special.arg3);
1256 break;
86387f7e
IM
1257 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;
89b2f978 1265 default:
214023c3 1266 trace_seq_printf(s, "Unknown type %d\n", entry->type);
bc0c38d1 1267 }
f9896bf3 1268 return 1;
bc0c38d1
SR
1269}
1270
e309b41d 1271static int print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 1272{
214023c3 1273 struct trace_seq *s = &iter->seq;
bc0c38d1 1274 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 1275 struct trace_entry *entry;
bc0c38d1
SR
1276 unsigned long usec_rem;
1277 unsigned long long t;
1278 unsigned long secs;
1279 char *comm;
b3806b43 1280 int ret;
86387f7e
IM
1281 int S;
1282 int i;
bc0c38d1 1283
4e3c3333
IM
1284 entry = iter->ent;
1285
bc0c38d1
SR
1286 comm = trace_find_cmdline(iter->ent->pid);
1287
cdd31cd2 1288 t = ns2usecs(entry->t);
bc0c38d1
SR
1289 usec_rem = do_div(t, 1000000ULL);
1290 secs = (unsigned long)t;
1291
86387f7e
IM
1292 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 }
bc0c38d1
SR
1303
1304 switch (entry->type) {
1305 case TRACE_FN:
b3806b43
SR
1306 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1307 if (!ret)
1308 return 0;
bc0c38d1
SR
1309 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1310 entry->fn.parent_ip) {
b3806b43
SR
1311 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;
bc0c38d1 1318 }
b3806b43
SR
1319 ret = trace_seq_printf(s, "\n");
1320 if (!ret)
1321 return 0;
bc0c38d1
SR
1322 break;
1323 case TRACE_CTX:
57422797 1324 case TRACE_WAKE:
bc0c38d1
SR
1325 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1326 state_to_char[entry->ctx.prev_state] : 'X';
57422797 1327 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d\n",
b3806b43
SR
1328 entry->ctx.prev_pid,
1329 entry->ctx.prev_prio,
1330 S,
57422797 1331 entry->type == TRACE_CTX ? "==>" : " +",
b3806b43
SR
1332 entry->ctx.next_pid,
1333 entry->ctx.next_prio);
1334 if (!ret)
1335 return 0;
bc0c38d1 1336 break;
f0a920d5 1337 case TRACE_SPECIAL:
4e655519 1338 ret = trace_seq_printf(s, " %ld %ld %ld\n",
f0a920d5
IM
1339 entry->special.arg1,
1340 entry->special.arg2,
1341 entry->special.arg3);
1342 if (!ret)
1343 return 0;
1344 break;
86387f7e
IM
1345 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;
bc0c38d1 1361 }
b3806b43 1362 return 1;
bc0c38d1
SR
1363}
1364
e309b41d 1365static int print_raw_fmt(struct trace_iterator *iter)
f9896bf3
IM
1366{
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:
57422797 1387 case TRACE_WAKE:
f9896bf3
IM
1388 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1389 state_to_char[entry->ctx.prev_state] : 'X';
57422797
IM
1390 if (entry->type == TRACE_WAKE)
1391 S = '+';
f9896bf3
IM
1392 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;
f0a920d5 1401 case TRACE_SPECIAL:
86387f7e 1402 case TRACE_STACK:
4e655519 1403 ret = trace_seq_printf(s, " %ld %ld %ld\n",
f0a920d5
IM
1404 entry->special.arg1,
1405 entry->special.arg2,
1406 entry->special.arg3);
1407 if (!ret)
1408 return 0;
1409 break;
f9896bf3
IM
1410 }
1411 return 1;
1412}
1413
cb0f12aa
IM
1414#define SEQ_PUT_FIELD_RET(s, x) \
1415do { \
1416 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1417 return 0; \
1418} while (0)
1419
5e3ca0ec
IM
1420#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
e309b41d 1426static int print_hex_fmt(struct trace_iterator *iter)
5e3ca0ec
IM
1427{
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:
57422797 1445 case TRACE_WAKE:
5e3ca0ec
IM
1446 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1447 state_to_char[entry->ctx.prev_state] : 'X';
57422797
IM
1448 if (entry->type == TRACE_WAKE)
1449 S = '+';
5e3ca0ec
IM
1450 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:
86387f7e 1458 case TRACE_STACK:
5e3ca0ec
IM
1459 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
e309b41d 1469static int print_bin_fmt(struct trace_iterator *iter)
cb0f12aa
IM
1470{
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;
f0a920d5 1492 case TRACE_SPECIAL:
86387f7e 1493 case TRACE_STACK:
f0a920d5
IM
1494 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;
cb0f12aa
IM
1498 }
1499 return 1;
1500}
1501
bc0c38d1
SR
1502static 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
b3806b43
SR
1510 if (head_page(data) && data->trace_idx &&
1511 (data->trace_tail != data->trace_head ||
1512 data->trace_tail_idx != data->trace_head_idx))
bc0c38d1
SR
1513 return 0;
1514 }
1515 return 1;
1516}
1517
f9896bf3
IM
1518static int print_trace_line(struct trace_iterator *iter)
1519{
cb0f12aa
IM
1520 if (trace_flags & TRACE_ITER_BIN)
1521 return print_bin_fmt(iter);
1522
5e3ca0ec
IM
1523 if (trace_flags & TRACE_ITER_HEX)
1524 return print_hex_fmt(iter);
1525
f9896bf3
IM
1526 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
bc0c38d1
SR
1535static 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 {
f9896bf3 1556 print_trace_line(iter);
214023c3 1557 trace_print_seq(m, &iter->seq);
bc0c38d1
SR
1558 }
1559
1560 return 0;
1561}
1562
1563static struct seq_operations tracer_seq_ops = {
4bf39a94
IM
1564 .start = s_start,
1565 .next = s_next,
1566 .stop = s_stop,
1567 .show = s_show,
bc0c38d1
SR
1568};
1569
e309b41d 1570static struct trace_iterator *
bc0c38d1
SR
1571__tracing_open(struct inode *inode, struct file *file, int *ret)
1572{
1573 struct trace_iterator *iter;
1574
60a11774
SR
1575 if (tracing_disabled) {
1576 *ret = -ENODEV;
1577 return NULL;
1578 }
1579
bc0c38d1
SR
1580 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{
60a11774
SR
1618 if (tracing_disabled)
1619 return -ENODEV;
1620
bc0c38d1
SR
1621 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
e309b41d 1667static void *
bc0c38d1
SR
1668t_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 = {
4bf39a94
IM
1716 .start = t_start,
1717 .next = t_next,
1718 .stop = t_stop,
1719 .show = t_show,
bc0c38d1
SR
1720};
1721
1722static int show_traces_open(struct inode *inode, struct file *file)
1723{
1724 int ret;
1725
60a11774
SR
1726 if (tracing_disabled)
1727 return -ENODEV;
1728
bc0c38d1
SR
1729 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 = {
4bf39a94
IM
1739 .open = tracing_open,
1740 .read = seq_read,
1741 .llseek = seq_lseek,
1742 .release = tracing_release,
bc0c38d1
SR
1743};
1744
1745static struct file_operations tracing_lt_fops = {
4bf39a94
IM
1746 .open = tracing_lt_open,
1747 .read = seq_read,
1748 .llseek = seq_lseek,
1749 .release = tracing_release,
bc0c38d1
SR
1750};
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
7bd2f24c
IM
1841static 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
bc0c38d1
SR
1873static 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);
4e3c3333 1882 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
1883}
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
4bf39a94 1938 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
1939}
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;
4bf39a94 1997 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
1998}
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
b3806b43
SR
2023static 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
2a2cc8f7
SSP
2060static 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
b3806b43
SR
2082/*
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;
b3806b43
SR
2092 static int start;
2093 unsigned long flags;
25770467 2094#ifdef CONFIG_FTRACE
2e0f5761 2095 int ftrace_save;
25770467 2096#endif
b3806b43
SR
2097 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)) {
2a2cc8f7
SSP
2120 if (!(trace_flags & TRACE_ITER_BLOCK))
2121 return -EWOULDBLOCK;
b3806b43
SR
2122 /*
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);
25770467 2177#ifdef CONFIG_FTRACE
2e0f5761
IM
2178 ftrace_save = ftrace_enabled;
2179 ftrace_enabled = 0;
25770467 2180#endif
2e0f5761 2181 smp_wmb();
b3806b43
SR
2182 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);
b3806b43
SR
2189 cpu_set(cpu, mask);
2190 }
2191
2e0f5761
IM
2192 for_each_cpu_mask(cpu, mask) {
2193 data = iter->tr->data[cpu];
2194 spin_lock(&data->lock);
2195 }
2196
088b1e42
SR
2197 while (find_next_entry_inc(iter) != NULL) {
2198 int len = iter->seq.len;
2199
f9896bf3 2200 ret = print_trace_line(iter);
088b1e42
SR
2201 if (!ret) {
2202 /* don't print partial lines */
2203 iter->seq.len = len;
b3806b43 2204 break;
088b1e42 2205 }
b3806b43
SR
2206
2207 trace_consume(iter);
2208
2209 if (iter->seq.len >= cnt)
2210 break;
b3806b43
SR
2211 }
2212
d4c5a2f5 2213 for_each_cpu_mask(cpu, mask) {
b3806b43 2214 data = iter->tr->data[cpu];
b3806b43 2215 spin_unlock(&data->lock);
2e0f5761
IM
2216 }
2217
2218 for_each_cpu_mask(cpu, mask) {
2219 data = iter->tr->data[cpu];
b3806b43
SR
2220 atomic_dec(&data->disabled);
2221 }
25770467 2222#ifdef CONFIG_FTRACE
2e0f5761 2223 ftrace_enabled = ftrace_save;
25770467 2224#endif
b3806b43
SR
2225 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
bc0c38d1 2245static struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
2246 .open = tracing_open_generic,
2247 .read = tracing_max_lat_read,
2248 .write = tracing_max_lat_write,
bc0c38d1
SR
2249};
2250
2251static struct file_operations tracing_ctrl_fops = {
4bf39a94
IM
2252 .open = tracing_open_generic,
2253 .read = tracing_ctrl_read,
2254 .write = tracing_ctrl_write,
bc0c38d1
SR
2255};
2256
2257static struct file_operations set_tracer_fops = {
4bf39a94
IM
2258 .open = tracing_open_generic,
2259 .read = tracing_set_trace_read,
2260 .write = tracing_set_trace_write,
bc0c38d1
SR
2261};
2262
b3806b43 2263static struct file_operations tracing_pipe_fops = {
4bf39a94 2264 .open = tracing_open_pipe,
2a2cc8f7 2265 .poll = tracing_poll_pipe,
4bf39a94
IM
2266 .read = tracing_read_pipe,
2267 .release = tracing_release_pipe,
b3806b43
SR
2268};
2269
bc0c38d1
SR
2270#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);
4bf39a94
IM
2281
2282 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2283}
2284
2285static struct file_operations tracing_read_long_fops = {
4bf39a94
IM
2286 .open = tracing_open_generic,
2287 .read = tracing_read_long,
bc0c38d1
SR
2288};
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
60a11774
SR
2311#ifdef CONFIG_FTRACE_SELFTEST
2312/* Let selftest have access to static functions in this file */
2313#include "trace_selftest.c"
2314#endif
2315
bc0c38d1
SR
2316static __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");
7bd2f24c
IM
2365 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
b3806b43
SR
2370 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");
bc0c38d1
SR
2375
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{
4bf39a94 2389 .name = "none",
bc0c38d1
SR
2390};
2391
4c11d7ae 2392static int trace_alloc_page(void)
bc0c38d1 2393{
4c11d7ae 2394 struct trace_array_cpu *data;
4c11d7ae
SR
2395 struct page *page, *tmp;
2396 LIST_HEAD(pages);
c7aafc54 2397 void *array;
4c11d7ae
SR
2398 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];
b3806b43 2428 spin_lock_init(&data->lock);
d4c5a2f5 2429 lockdep_set_class(&data->lock, &data->lock_key);
4c11d7ae 2430 page = list_entry(pages.next, struct page, lru);
c7aafc54 2431 list_del_init(&page->lru);
4c11d7ae
SR
2432 list_add_tail(&page->lru, &data->trace_pages);
2433 ClearPageLRU(page);
2434
2435#ifdef CONFIG_TRACER_MAX_TRACE
2436 data = max_tr.data[i];
b3806b43 2437 spin_lock_init(&data->lock);
d4c5a2f5 2438 lockdep_set_class(&data->lock, &data->lock_key);
4c11d7ae 2439 page = list_entry(pages.next, struct page, lru);
c7aafc54 2440 list_del_init(&page->lru);
4c11d7ae
SR
2441 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) {
c7aafc54 2451 list_del_init(&page->lru);
4c11d7ae
SR
2452 __free_page(page);
2453 }
2454 return -ENOMEM;
bc0c38d1
SR
2455}
2456
2457__init static int tracer_alloc_buffers(void)
2458{
4c11d7ae
SR
2459 struct trace_array_cpu *data;
2460 void *array;
2461 struct page *page;
2462 int pages = 0;
60a11774 2463 int ret = -ENOMEM;
bc0c38d1
SR
2464 int i;
2465
26994ead
SR
2466 global_trace.ctrl = tracer_enabled;
2467
4c11d7ae 2468 /* Allocate the first page for all buffers */
bc0c38d1 2469 for_each_possible_cpu(i) {
4c11d7ae 2470 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
bc0c38d1
SR
2471 max_tr.data[i] = &per_cpu(max_data, i);
2472
4c11d7ae 2473 array = (void *)__get_free_page(GFP_KERNEL);
bc0c38d1 2474 if (array == NULL) {
4c11d7ae
SR
2475 printk(KERN_ERR "tracer: failed to allocate page"
2476 "for trace buffer!\n");
bc0c38d1
SR
2477 goto free_buffers;
2478 }
4c11d7ae
SR
2479
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);
bc0c38d1
SR
2486
2487/* Only allocate if we are actually using the max trace */
2488#ifdef CONFIG_TRACER_MAX_TRACE
4c11d7ae 2489 array = (void *)__get_free_page(GFP_KERNEL);
bc0c38d1 2490 if (array == NULL) {
4c11d7ae
SR
2491 printk(KERN_ERR "tracer: failed to allocate page"
2492 "for trace buffer!\n");
bc0c38d1
SR
2493 goto free_buffers;
2494 }
4c11d7ae
SR
2495
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);
bc0c38d1
SR
2500#endif
2501 }
2502
2503 /*
2504 * Since we allocate by orders of pages, we may be able to
2505 * round up a bit.
2506 */
4c11d7ae 2507 global_trace.entries = ENTRIES_PER_PAGE;
4c11d7ae
SR
2508 pages++;
2509
2510 while (global_trace.entries < trace_nr_entries) {
2511 if (trace_alloc_page())
2512 break;
2513 pages++;
2514 }
89b2f978 2515 max_tr.entries = global_trace.entries;
bc0c38d1 2516
4c11d7ae
SR
2517 pr_info("tracer: %d pages allocated for %ld",
2518 pages, trace_nr_entries);
bc0c38d1
SR
2519 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
60a11774
SR
2529 /* All seems OK, enable tracing */
2530 tracing_disabled = 0;
2531
bc0c38d1
SR
2532 return 0;
2533
2534 free_buffers:
2535 for (i-- ; i >= 0; i--) {
4c11d7ae 2536 struct page *page, *tmp;
bc0c38d1
SR
2537 struct trace_array_cpu *data = global_trace.data[i];
2538
c7aafc54 2539 if (data) {
4c11d7ae
SR
2540 list_for_each_entry_safe(page, tmp,
2541 &data->trace_pages, lru) {
c7aafc54 2542 list_del_init(&page->lru);
4c11d7ae
SR
2543 __free_page(page);
2544 }
bc0c38d1
SR
2545 }
2546
2547#ifdef CONFIG_TRACER_MAX_TRACE
2548 data = max_tr.data[i];
c7aafc54 2549 if (data) {
4c11d7ae
SR
2550 list_for_each_entry_safe(page, tmp,
2551 &data->trace_pages, lru) {
c7aafc54 2552 list_del_init(&page->lru);
4c11d7ae
SR
2553 __free_page(page);
2554 }
bc0c38d1
SR
2555 }
2556#endif
2557 }
60a11774 2558 return ret;
bc0c38d1 2559}
60a11774 2560fs_initcall(tracer_alloc_buffers);