tracing/ftrace: don't insert TRACE_PRINT during selftests
[linux-2.6] / kernel / trace / trace.c
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/notifier.h>
18 #include <linux/debugfs.h>
19 #include <linux/pagemap.h>
20 #include <linux/hardirq.h>
21 #include <linux/linkage.h>
22 #include <linux/uaccess.h>
23 #include <linux/ftrace.h>
24 #include <linux/module.h>
25 #include <linux/percpu.h>
26 #include <linux/kdebug.h>
27 #include <linux/ctype.h>
28 #include <linux/init.h>
29 #include <linux/poll.h>
30 #include <linux/gfp.h>
31 #include <linux/fs.h>
32 #include <linux/kprobes.h>
33 #include <linux/seq_file.h>
34 #include <linux/writeback.h>
35
36 #include <linux/stacktrace.h>
37 #include <linux/ring_buffer.h>
38 #include <linux/irqflags.h>
39
40 #include "trace.h"
41
42 #define TRACE_BUFFER_FLAGS      (RB_FL_OVERWRITE)
43
44 unsigned long __read_mostly     tracing_max_latency = (cycle_t)ULONG_MAX;
45 unsigned long __read_mostly     tracing_thresh;
46
47 /* We need to change this state when a selftest is running.
48  * A selftest will lurk into the ring-buffer to count the
49  * entries inserted during the selftest although some concurrent
50  * insertions into the ring-buffer such as ftrace_printk could occurred
51  * at the same time, giving false positive or negative results.
52  */
53 static atomic_t tracing_selftest_running = ATOMIC_INIT(0);
54
55 /* For tracers that don't implement custom flags */
56 static struct tracer_opt dummy_tracer_opt[] = {
57         { }
58 };
59
60 static struct tracer_flags dummy_tracer_flags = {
61         .val = 0,
62         .opts = dummy_tracer_opt
63 };
64
65 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
66 {
67         return 0;
68 }
69
70 /*
71  * Kill all tracing for good (never come back).
72  * It is initialized to 1 but will turn to zero if the initialization
73  * of the tracer is successful. But that is the only place that sets
74  * this back to zero.
75  */
76 int tracing_disabled = 1;
77
78 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
79
80 static inline void ftrace_disable_cpu(void)
81 {
82         preempt_disable();
83         local_inc(&__get_cpu_var(ftrace_cpu_disabled));
84 }
85
86 static inline void ftrace_enable_cpu(void)
87 {
88         local_dec(&__get_cpu_var(ftrace_cpu_disabled));
89         preempt_enable();
90 }
91
92 static cpumask_t __read_mostly          tracing_buffer_mask;
93
94 #define for_each_tracing_cpu(cpu)       \
95         for_each_cpu_mask(cpu, tracing_buffer_mask)
96
97 /*
98  * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
99  *
100  * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
101  * is set, then ftrace_dump is called. This will output the contents
102  * of the ftrace buffers to the console.  This is very useful for
103  * capturing traces that lead to crashes and outputing it to a
104  * serial console.
105  *
106  * It is default off, but you can enable it with either specifying
107  * "ftrace_dump_on_oops" in the kernel command line, or setting
108  * /proc/sys/kernel/ftrace_dump_on_oops to true.
109  */
110 int ftrace_dump_on_oops;
111
112 static int tracing_set_tracer(char *buf);
113
114 static int __init set_ftrace(char *str)
115 {
116         tracing_set_tracer(str);
117         return 1;
118 }
119 __setup("ftrace", set_ftrace);
120
121 static int __init set_ftrace_dump_on_oops(char *str)
122 {
123         ftrace_dump_on_oops = 1;
124         return 1;
125 }
126 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
127
128 long
129 ns2usecs(cycle_t nsec)
130 {
131         nsec += 500;
132         do_div(nsec, 1000);
133         return nsec;
134 }
135
136 cycle_t ftrace_now(int cpu)
137 {
138         u64 ts = ring_buffer_time_stamp(cpu);
139         ring_buffer_normalize_time_stamp(cpu, &ts);
140         return ts;
141 }
142
143 /*
144  * The global_trace is the descriptor that holds the tracing
145  * buffers for the live tracing. For each CPU, it contains
146  * a link list of pages that will store trace entries. The
147  * page descriptor of the pages in the memory is used to hold
148  * the link list by linking the lru item in the page descriptor
149  * to each of the pages in the buffer per CPU.
150  *
151  * For each active CPU there is a data field that holds the
152  * pages for the buffer for that CPU. Each CPU has the same number
153  * of pages allocated for its buffer.
154  */
155 static struct trace_array       global_trace;
156
157 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
158
159 /*
160  * The max_tr is used to snapshot the global_trace when a maximum
161  * latency is reached. Some tracers will use this to store a maximum
162  * trace while it continues examining live traces.
163  *
164  * The buffers for the max_tr are set up the same as the global_trace.
165  * When a snapshot is taken, the link list of the max_tr is swapped
166  * with the link list of the global_trace and the buffers are reset for
167  * the global_trace so the tracing can continue.
168  */
169 static struct trace_array       max_tr;
170
171 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
172
173 /* tracer_enabled is used to toggle activation of a tracer */
174 static int                      tracer_enabled = 1;
175
176 /**
177  * tracing_is_enabled - return tracer_enabled status
178  *
179  * This function is used by other tracers to know the status
180  * of the tracer_enabled flag.  Tracers may use this function
181  * to know if it should enable their features when starting
182  * up. See irqsoff tracer for an example (start_irqsoff_tracer).
183  */
184 int tracing_is_enabled(void)
185 {
186         return tracer_enabled;
187 }
188
189 /* function tracing enabled */
190 int                             ftrace_function_enabled;
191
192 /*
193  * trace_buf_size is the size in bytes that is allocated
194  * for a buffer. Note, the number of bytes is always rounded
195  * to page size.
196  *
197  * This number is purposely set to a low number of 16384.
198  * If the dump on oops happens, it will be much appreciated
199  * to not have to wait for all that output. Anyway this can be
200  * boot time and run time configurable.
201  */
202 #define TRACE_BUF_SIZE_DEFAULT  1441792UL /* 16384 * 88 (sizeof(entry)) */
203
204 static unsigned long            trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
205
206 /* trace_types holds a link list of available tracers. */
207 static struct tracer            *trace_types __read_mostly;
208
209 /* current_trace points to the tracer that is currently active */
210 static struct tracer            *current_trace __read_mostly;
211
212 /*
213  * max_tracer_type_len is used to simplify the allocating of
214  * buffers to read userspace tracer names. We keep track of
215  * the longest tracer name registered.
216  */
217 static int                      max_tracer_type_len;
218
219 /*
220  * trace_types_lock is used to protect the trace_types list.
221  * This lock is also used to keep user access serialized.
222  * Accesses from userspace will grab this lock while userspace
223  * activities happen inside the kernel.
224  */
225 static DEFINE_MUTEX(trace_types_lock);
226
227 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
228 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
229
230 /* trace_flags holds trace_options default values */
231 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
232         TRACE_ITER_ANNOTATE;
233
234 /**
235  * trace_wake_up - wake up tasks waiting for trace input
236  *
237  * Simply wakes up any task that is blocked on the trace_wait
238  * queue. These is used with trace_poll for tasks polling the trace.
239  */
240 void trace_wake_up(void)
241 {
242         /*
243          * The runqueue_is_locked() can fail, but this is the best we
244          * have for now:
245          */
246         if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
247                 wake_up(&trace_wait);
248 }
249
250 static int __init set_buf_size(char *str)
251 {
252         unsigned long buf_size;
253         int ret;
254
255         if (!str)
256                 return 0;
257         ret = strict_strtoul(str, 0, &buf_size);
258         /* nr_entries can not be zero */
259         if (ret < 0 || buf_size == 0)
260                 return 0;
261         trace_buf_size = buf_size;
262         return 1;
263 }
264 __setup("trace_buf_size=", set_buf_size);
265
266 unsigned long nsecs_to_usecs(unsigned long nsecs)
267 {
268         return nsecs / 1000;
269 }
270
271 /* These must match the bit postions in trace_iterator_flags */
272 static const char *trace_options[] = {
273         "print-parent",
274         "sym-offset",
275         "sym-addr",
276         "verbose",
277         "raw",
278         "hex",
279         "bin",
280         "block",
281         "stacktrace",
282         "sched-tree",
283         "ftrace_printk",
284         "ftrace_preempt",
285         "branch",
286         "annotate",
287         "userstacktrace",
288         "sym-userobj",
289         NULL
290 };
291
292 /*
293  * ftrace_max_lock is used to protect the swapping of buffers
294  * when taking a max snapshot. The buffers themselves are
295  * protected by per_cpu spinlocks. But the action of the swap
296  * needs its own lock.
297  *
298  * This is defined as a raw_spinlock_t in order to help
299  * with performance when lockdep debugging is enabled.
300  */
301 static raw_spinlock_t ftrace_max_lock =
302         (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
303
304 /*
305  * Copy the new maximum trace into the separate maximum-trace
306  * structure. (this way the maximum trace is permanently saved,
307  * for later retrieval via /debugfs/tracing/latency_trace)
308  */
309 static void
310 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
311 {
312         struct trace_array_cpu *data = tr->data[cpu];
313
314         max_tr.cpu = cpu;
315         max_tr.time_start = data->preempt_timestamp;
316
317         data = max_tr.data[cpu];
318         data->saved_latency = tracing_max_latency;
319
320         memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
321         data->pid = tsk->pid;
322         data->uid = tsk->uid;
323         data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
324         data->policy = tsk->policy;
325         data->rt_priority = tsk->rt_priority;
326
327         /* record this tasks comm */
328         tracing_record_cmdline(current);
329 }
330
331 /**
332  * trace_seq_printf - sequence printing of trace information
333  * @s: trace sequence descriptor
334  * @fmt: printf format string
335  *
336  * The tracer may use either sequence operations or its own
337  * copy to user routines. To simplify formating of a trace
338  * trace_seq_printf is used to store strings into a special
339  * buffer (@s). Then the output may be either used by
340  * the sequencer or pulled into another buffer.
341  */
342 int
343 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
344 {
345         int len = (PAGE_SIZE - 1) - s->len;
346         va_list ap;
347         int ret;
348
349         if (!len)
350                 return 0;
351
352         va_start(ap, fmt);
353         ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
354         va_end(ap);
355
356         /* If we can't write it all, don't bother writing anything */
357         if (ret >= len)
358                 return 0;
359
360         s->len += ret;
361
362         return len;
363 }
364
365 /**
366  * trace_seq_puts - trace sequence printing of simple string
367  * @s: trace sequence descriptor
368  * @str: simple string to record
369  *
370  * The tracer may use either the sequence operations or its own
371  * copy to user routines. This function records a simple string
372  * into a special buffer (@s) for later retrieval by a sequencer
373  * or other mechanism.
374  */
375 static int
376 trace_seq_puts(struct trace_seq *s, const char *str)
377 {
378         int len = strlen(str);
379
380         if (len > ((PAGE_SIZE - 1) - s->len))
381                 return 0;
382
383         memcpy(s->buffer + s->len, str, len);
384         s->len += len;
385
386         return len;
387 }
388
389 static int
390 trace_seq_putc(struct trace_seq *s, unsigned char c)
391 {
392         if (s->len >= (PAGE_SIZE - 1))
393                 return 0;
394
395         s->buffer[s->len++] = c;
396
397         return 1;
398 }
399
400 static int
401 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
402 {
403         if (len > ((PAGE_SIZE - 1) - s->len))
404                 return 0;
405
406         memcpy(s->buffer + s->len, mem, len);
407         s->len += len;
408
409         return len;
410 }
411
412 #define MAX_MEMHEX_BYTES        8
413 #define HEX_CHARS               (MAX_MEMHEX_BYTES*2 + 1)
414
415 static int
416 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
417 {
418         unsigned char hex[HEX_CHARS];
419         unsigned char *data = mem;
420         int i, j;
421
422 #ifdef __BIG_ENDIAN
423         for (i = 0, j = 0; i < len; i++) {
424 #else
425         for (i = len-1, j = 0; i >= 0; i--) {
426 #endif
427                 hex[j++] = hex_asc_hi(data[i]);
428                 hex[j++] = hex_asc_lo(data[i]);
429         }
430         hex[j++] = ' ';
431
432         return trace_seq_putmem(s, hex, j);
433 }
434
435 static int
436 trace_seq_path(struct trace_seq *s, struct path *path)
437 {
438         unsigned char *p;
439
440         if (s->len >= (PAGE_SIZE - 1))
441                 return 0;
442         p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
443         if (!IS_ERR(p)) {
444                 p = mangle_path(s->buffer + s->len, p, "\n");
445                 if (p) {
446                         s->len = p - s->buffer;
447                         return 1;
448                 }
449         } else {
450                 s->buffer[s->len++] = '?';
451                 return 1;
452         }
453
454         return 0;
455 }
456
457 static void
458 trace_seq_reset(struct trace_seq *s)
459 {
460         s->len = 0;
461         s->readpos = 0;
462 }
463
464 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
465 {
466         int len;
467         int ret;
468
469         if (s->len <= s->readpos)
470                 return -EBUSY;
471
472         len = s->len - s->readpos;
473         if (cnt > len)
474                 cnt = len;
475         ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
476         if (ret)
477                 return -EFAULT;
478
479         s->readpos += len;
480         return cnt;
481 }
482
483 static void
484 trace_print_seq(struct seq_file *m, struct trace_seq *s)
485 {
486         int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
487
488         s->buffer[len] = 0;
489         seq_puts(m, s->buffer);
490
491         trace_seq_reset(s);
492 }
493
494 /**
495  * update_max_tr - snapshot all trace buffers from global_trace to max_tr
496  * @tr: tracer
497  * @tsk: the task with the latency
498  * @cpu: The cpu that initiated the trace.
499  *
500  * Flip the buffers between the @tr and the max_tr and record information
501  * about which task was the cause of this latency.
502  */
503 void
504 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
505 {
506         struct ring_buffer *buf = tr->buffer;
507
508         WARN_ON_ONCE(!irqs_disabled());
509         __raw_spin_lock(&ftrace_max_lock);
510
511         tr->buffer = max_tr.buffer;
512         max_tr.buffer = buf;
513
514         ftrace_disable_cpu();
515         ring_buffer_reset(tr->buffer);
516         ftrace_enable_cpu();
517
518         __update_max_tr(tr, tsk, cpu);
519         __raw_spin_unlock(&ftrace_max_lock);
520 }
521
522 /**
523  * update_max_tr_single - only copy one trace over, and reset the rest
524  * @tr - tracer
525  * @tsk - task with the latency
526  * @cpu - the cpu of the buffer to copy.
527  *
528  * Flip the trace of a single CPU buffer between the @tr and the max_tr.
529  */
530 void
531 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
532 {
533         int ret;
534
535         WARN_ON_ONCE(!irqs_disabled());
536         __raw_spin_lock(&ftrace_max_lock);
537
538         ftrace_disable_cpu();
539
540         ring_buffer_reset(max_tr.buffer);
541         ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
542
543         ftrace_enable_cpu();
544
545         WARN_ON_ONCE(ret);
546
547         __update_max_tr(tr, tsk, cpu);
548         __raw_spin_unlock(&ftrace_max_lock);
549 }
550
551 /**
552  * register_tracer - register a tracer with the ftrace system.
553  * @type - the plugin for the tracer
554  *
555  * Register a new plugin tracer.
556  */
557 int register_tracer(struct tracer *type)
558 {
559         struct tracer *t;
560         int len;
561         int ret = 0;
562
563         if (!type->name) {
564                 pr_info("Tracer must have a name\n");
565                 return -1;
566         }
567
568         /*
569          * When this gets called we hold the BKL which means that
570          * preemption is disabled. Various trace selftests however
571          * need to disable and enable preemption for successful tests.
572          * So we drop the BKL here and grab it after the tests again.
573          */
574         unlock_kernel();
575         mutex_lock(&trace_types_lock);
576
577         for (t = trace_types; t; t = t->next) {
578                 if (strcmp(type->name, t->name) == 0) {
579                         /* already found */
580                         pr_info("Trace %s already registered\n",
581                                 type->name);
582                         ret = -1;
583                         goto out;
584                 }
585         }
586
587         if (!type->set_flag)
588                 type->set_flag = &dummy_set_flag;
589         if (!type->flags)
590                 type->flags = &dummy_tracer_flags;
591         else
592                 if (!type->flags->opts)
593                         type->flags->opts = dummy_tracer_opt;
594
595 #ifdef CONFIG_FTRACE_STARTUP_TEST
596         if (type->selftest) {
597                 struct tracer *saved_tracer = current_trace;
598                 struct trace_array *tr = &global_trace;
599                 int i;
600
601                 atomic_set(&tracing_selftest_running, 1);
602                 /*
603                  * Run a selftest on this tracer.
604                  * Here we reset the trace buffer, and set the current
605                  * tracer to be this tracer. The tracer can then run some
606                  * internal tracing to verify that everything is in order.
607                  * If we fail, we do not register this tracer.
608                  */
609                 for_each_tracing_cpu(i)
610                         tracing_reset(tr, i);
611
612                 current_trace = type;
613                 /* the test is responsible for initializing and enabling */
614                 pr_info("Testing tracer %s: ", type->name);
615                 ret = type->selftest(type, tr);
616                 atomic_set(&tracing_selftest_running, 0);
617                 /* the test is responsible for resetting too */
618                 current_trace = saved_tracer;
619                 if (ret) {
620                         printk(KERN_CONT "FAILED!\n");
621                         goto out;
622                 }
623                 /* Only reset on passing, to avoid touching corrupted buffers */
624                 for_each_tracing_cpu(i)
625                         tracing_reset(tr, i);
626
627                 printk(KERN_CONT "PASSED\n");
628         }
629 #endif
630
631         type->next = trace_types;
632         trace_types = type;
633         len = strlen(type->name);
634         if (len > max_tracer_type_len)
635                 max_tracer_type_len = len;
636
637  out:
638         mutex_unlock(&trace_types_lock);
639         lock_kernel();
640
641         return ret;
642 }
643
644 void unregister_tracer(struct tracer *type)
645 {
646         struct tracer **t;
647         int len;
648
649         mutex_lock(&trace_types_lock);
650         for (t = &trace_types; *t; t = &(*t)->next) {
651                 if (*t == type)
652                         goto found;
653         }
654         pr_info("Trace %s not registered\n", type->name);
655         goto out;
656
657  found:
658         *t = (*t)->next;
659         if (strlen(type->name) != max_tracer_type_len)
660                 goto out;
661
662         max_tracer_type_len = 0;
663         for (t = &trace_types; *t; t = &(*t)->next) {
664                 len = strlen((*t)->name);
665                 if (len > max_tracer_type_len)
666                         max_tracer_type_len = len;
667         }
668  out:
669         mutex_unlock(&trace_types_lock);
670 }
671
672 void tracing_reset(struct trace_array *tr, int cpu)
673 {
674         ftrace_disable_cpu();
675         ring_buffer_reset_cpu(tr->buffer, cpu);
676         ftrace_enable_cpu();
677 }
678
679 #define SAVED_CMDLINES 128
680 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
681 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
682 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
683 static int cmdline_idx;
684 static DEFINE_SPINLOCK(trace_cmdline_lock);
685
686 /* temporary disable recording */
687 atomic_t trace_record_cmdline_disabled __read_mostly;
688
689 static void trace_init_cmdlines(void)
690 {
691         memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
692         memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
693         cmdline_idx = 0;
694 }
695
696 static int trace_stop_count;
697 static DEFINE_SPINLOCK(tracing_start_lock);
698
699 /**
700  * ftrace_off_permanent - disable all ftrace code permanently
701  *
702  * This should only be called when a serious anomally has
703  * been detected.  This will turn off the function tracing,
704  * ring buffers, and other tracing utilites. It takes no
705  * locks and can be called from any context.
706  */
707 void ftrace_off_permanent(void)
708 {
709         tracing_disabled = 1;
710         ftrace_stop();
711         tracing_off_permanent();
712 }
713
714 /**
715  * tracing_start - quick start of the tracer
716  *
717  * If tracing is enabled but was stopped by tracing_stop,
718  * this will start the tracer back up.
719  */
720 void tracing_start(void)
721 {
722         struct ring_buffer *buffer;
723         unsigned long flags;
724
725         if (tracing_disabled)
726                 return;
727
728         spin_lock_irqsave(&tracing_start_lock, flags);
729         if (--trace_stop_count)
730                 goto out;
731
732         if (trace_stop_count < 0) {
733                 /* Someone screwed up their debugging */
734                 WARN_ON_ONCE(1);
735                 trace_stop_count = 0;
736                 goto out;
737         }
738
739
740         buffer = global_trace.buffer;
741         if (buffer)
742                 ring_buffer_record_enable(buffer);
743
744         buffer = max_tr.buffer;
745         if (buffer)
746                 ring_buffer_record_enable(buffer);
747
748         ftrace_start();
749  out:
750         spin_unlock_irqrestore(&tracing_start_lock, flags);
751 }
752
753 /**
754  * tracing_stop - quick stop of the tracer
755  *
756  * Light weight way to stop tracing. Use in conjunction with
757  * tracing_start.
758  */
759 void tracing_stop(void)
760 {
761         struct ring_buffer *buffer;
762         unsigned long flags;
763
764         ftrace_stop();
765         spin_lock_irqsave(&tracing_start_lock, flags);
766         if (trace_stop_count++)
767                 goto out;
768
769         buffer = global_trace.buffer;
770         if (buffer)
771                 ring_buffer_record_disable(buffer);
772
773         buffer = max_tr.buffer;
774         if (buffer)
775                 ring_buffer_record_disable(buffer);
776
777  out:
778         spin_unlock_irqrestore(&tracing_start_lock, flags);
779 }
780
781 void trace_stop_cmdline_recording(void);
782
783 static void trace_save_cmdline(struct task_struct *tsk)
784 {
785         unsigned map;
786         unsigned idx;
787
788         if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
789                 return;
790
791         /*
792          * It's not the end of the world if we don't get
793          * the lock, but we also don't want to spin
794          * nor do we want to disable interrupts,
795          * so if we miss here, then better luck next time.
796          */
797         if (!spin_trylock(&trace_cmdline_lock))
798                 return;
799
800         idx = map_pid_to_cmdline[tsk->pid];
801         if (idx >= SAVED_CMDLINES) {
802                 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
803
804                 map = map_cmdline_to_pid[idx];
805                 if (map <= PID_MAX_DEFAULT)
806                         map_pid_to_cmdline[map] = (unsigned)-1;
807
808                 map_pid_to_cmdline[tsk->pid] = idx;
809
810                 cmdline_idx = idx;
811         }
812
813         memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
814
815         spin_unlock(&trace_cmdline_lock);
816 }
817
818 char *trace_find_cmdline(int pid)
819 {
820         char *cmdline = "<...>";
821         unsigned map;
822
823         if (!pid)
824                 return "<idle>";
825
826         if (pid > PID_MAX_DEFAULT)
827                 goto out;
828
829         map = map_pid_to_cmdline[pid];
830         if (map >= SAVED_CMDLINES)
831                 goto out;
832
833         cmdline = saved_cmdlines[map];
834
835  out:
836         return cmdline;
837 }
838
839 void tracing_record_cmdline(struct task_struct *tsk)
840 {
841         if (atomic_read(&trace_record_cmdline_disabled))
842                 return;
843
844         trace_save_cmdline(tsk);
845 }
846
847 void
848 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
849                              int pc)
850 {
851         struct task_struct *tsk = current;
852
853         entry->preempt_count            = pc & 0xff;
854         entry->pid                      = (tsk) ? tsk->pid : 0;
855         entry->tgid                     = (tsk) ? tsk->tgid : 0;
856         entry->flags =
857 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
858                 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
859 #else
860                 TRACE_FLAG_IRQS_NOSUPPORT |
861 #endif
862                 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
863                 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
864                 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
865 }
866
867 void
868 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
869                unsigned long ip, unsigned long parent_ip, unsigned long flags,
870                int pc)
871 {
872         struct ring_buffer_event *event;
873         struct ftrace_entry *entry;
874         unsigned long irq_flags;
875
876         /* If we are reading the ring buffer, don't trace */
877         if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
878                 return;
879
880         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
881                                          &irq_flags);
882         if (!event)
883                 return;
884         entry   = ring_buffer_event_data(event);
885         tracing_generic_entry_update(&entry->ent, flags, pc);
886         entry->ent.type                 = TRACE_FN;
887         entry->ip                       = ip;
888         entry->parent_ip                = parent_ip;
889         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
890 }
891
892 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
893 static void __trace_graph_entry(struct trace_array *tr,
894                                 struct trace_array_cpu *data,
895                                 struct ftrace_graph_ent *trace,
896                                 unsigned long flags,
897                                 int pc)
898 {
899         struct ring_buffer_event *event;
900         struct ftrace_graph_ent_entry *entry;
901         unsigned long irq_flags;
902
903         if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
904                 return;
905
906         event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
907                                          &irq_flags);
908         if (!event)
909                 return;
910         entry   = ring_buffer_event_data(event);
911         tracing_generic_entry_update(&entry->ent, flags, pc);
912         entry->ent.type                 = TRACE_GRAPH_ENT;
913         entry->graph_ent                        = *trace;
914         ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
915 }
916
917 static void __trace_graph_return(struct trace_array *tr,
918                                 struct trace_array_cpu *data,
919                                 struct ftrace_graph_ret *trace,
920                                 unsigned long flags,
921                                 int pc)
922 {
923         struct ring_buffer_event *event;
924         struct ftrace_graph_ret_entry *entry;
925         unsigned long irq_flags;
926
927         if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
928                 return;
929
930         event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
931                                          &irq_flags);
932         if (!event)
933                 return;
934         entry   = ring_buffer_event_data(event);
935         tracing_generic_entry_update(&entry->ent, flags, pc);
936         entry->ent.type                 = TRACE_GRAPH_RET;
937         entry->ret                              = *trace;
938         ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
939 }
940 #endif
941
942 void
943 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
944        unsigned long ip, unsigned long parent_ip, unsigned long flags,
945        int pc)
946 {
947         if (likely(!atomic_read(&data->disabled)))
948                 trace_function(tr, data, ip, parent_ip, flags, pc);
949 }
950
951 static void ftrace_trace_stack(struct trace_array *tr,
952                                struct trace_array_cpu *data,
953                                unsigned long flags,
954                                int skip, int pc)
955 {
956 #ifdef CONFIG_STACKTRACE
957         struct ring_buffer_event *event;
958         struct stack_entry *entry;
959         struct stack_trace trace;
960         unsigned long irq_flags;
961
962         if (!(trace_flags & TRACE_ITER_STACKTRACE))
963                 return;
964
965         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
966                                          &irq_flags);
967         if (!event)
968                 return;
969         entry   = ring_buffer_event_data(event);
970         tracing_generic_entry_update(&entry->ent, flags, pc);
971         entry->ent.type         = TRACE_STACK;
972
973         memset(&entry->caller, 0, sizeof(entry->caller));
974
975         trace.nr_entries        = 0;
976         trace.max_entries       = FTRACE_STACK_ENTRIES;
977         trace.skip              = skip;
978         trace.entries           = entry->caller;
979
980         save_stack_trace(&trace);
981         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
982 #endif
983 }
984
985 void __trace_stack(struct trace_array *tr,
986                    struct trace_array_cpu *data,
987                    unsigned long flags,
988                    int skip)
989 {
990         ftrace_trace_stack(tr, data, flags, skip, preempt_count());
991 }
992
993 static void ftrace_trace_userstack(struct trace_array *tr,
994                    struct trace_array_cpu *data,
995                    unsigned long flags, int pc)
996 {
997 #ifdef CONFIG_STACKTRACE
998         struct ring_buffer_event *event;
999         struct userstack_entry *entry;
1000         struct stack_trace trace;
1001         unsigned long irq_flags;
1002
1003         if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1004                 return;
1005
1006         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1007                                          &irq_flags);
1008         if (!event)
1009                 return;
1010         entry   = ring_buffer_event_data(event);
1011         tracing_generic_entry_update(&entry->ent, flags, pc);
1012         entry->ent.type         = TRACE_USER_STACK;
1013
1014         memset(&entry->caller, 0, sizeof(entry->caller));
1015
1016         trace.nr_entries        = 0;
1017         trace.max_entries       = FTRACE_STACK_ENTRIES;
1018         trace.skip              = 0;
1019         trace.entries           = entry->caller;
1020
1021         save_stack_trace_user(&trace);
1022         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1023 #endif
1024 }
1025
1026 void __trace_userstack(struct trace_array *tr,
1027                    struct trace_array_cpu *data,
1028                    unsigned long flags)
1029 {
1030         ftrace_trace_userstack(tr, data, flags, preempt_count());
1031 }
1032
1033 static void
1034 ftrace_trace_special(void *__tr, void *__data,
1035                      unsigned long arg1, unsigned long arg2, unsigned long arg3,
1036                      int pc)
1037 {
1038         struct ring_buffer_event *event;
1039         struct trace_array_cpu *data = __data;
1040         struct trace_array *tr = __tr;
1041         struct special_entry *entry;
1042         unsigned long irq_flags;
1043
1044         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1045                                          &irq_flags);
1046         if (!event)
1047                 return;
1048         entry   = ring_buffer_event_data(event);
1049         tracing_generic_entry_update(&entry->ent, 0, pc);
1050         entry->ent.type                 = TRACE_SPECIAL;
1051         entry->arg1                     = arg1;
1052         entry->arg2                     = arg2;
1053         entry->arg3                     = arg3;
1054         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1055         ftrace_trace_stack(tr, data, irq_flags, 4, pc);
1056         ftrace_trace_userstack(tr, data, irq_flags, pc);
1057
1058         trace_wake_up();
1059 }
1060
1061 void
1062 __trace_special(void *__tr, void *__data,
1063                 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1064 {
1065         ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
1066 }
1067
1068 void
1069 tracing_sched_switch_trace(struct trace_array *tr,
1070                            struct trace_array_cpu *data,
1071                            struct task_struct *prev,
1072                            struct task_struct *next,
1073                            unsigned long flags, int pc)
1074 {
1075         struct ring_buffer_event *event;
1076         struct ctx_switch_entry *entry;
1077         unsigned long irq_flags;
1078
1079         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1080                                            &irq_flags);
1081         if (!event)
1082                 return;
1083         entry   = ring_buffer_event_data(event);
1084         tracing_generic_entry_update(&entry->ent, flags, pc);
1085         entry->ent.type                 = TRACE_CTX;
1086         entry->prev_pid                 = prev->pid;
1087         entry->prev_prio                = prev->prio;
1088         entry->prev_state               = prev->state;
1089         entry->next_pid                 = next->pid;
1090         entry->next_prio                = next->prio;
1091         entry->next_state               = next->state;
1092         entry->next_cpu = task_cpu(next);
1093         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1094         ftrace_trace_stack(tr, data, flags, 5, pc);
1095         ftrace_trace_userstack(tr, data, flags, pc);
1096 }
1097
1098 void
1099 tracing_sched_wakeup_trace(struct trace_array *tr,
1100                            struct trace_array_cpu *data,
1101                            struct task_struct *wakee,
1102                            struct task_struct *curr,
1103                            unsigned long flags, int pc)
1104 {
1105         struct ring_buffer_event *event;
1106         struct ctx_switch_entry *entry;
1107         unsigned long irq_flags;
1108
1109         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1110                                            &irq_flags);
1111         if (!event)
1112                 return;
1113         entry   = ring_buffer_event_data(event);
1114         tracing_generic_entry_update(&entry->ent, flags, pc);
1115         entry->ent.type                 = TRACE_WAKE;
1116         entry->prev_pid                 = curr->pid;
1117         entry->prev_prio                = curr->prio;
1118         entry->prev_state               = curr->state;
1119         entry->next_pid                 = wakee->pid;
1120         entry->next_prio                = wakee->prio;
1121         entry->next_state               = wakee->state;
1122         entry->next_cpu                 = task_cpu(wakee);
1123         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1124         ftrace_trace_stack(tr, data, flags, 6, pc);
1125         ftrace_trace_userstack(tr, data, flags, pc);
1126
1127         trace_wake_up();
1128 }
1129
1130 void
1131 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1132 {
1133         struct trace_array *tr = &global_trace;
1134         struct trace_array_cpu *data;
1135         unsigned long flags;
1136         int cpu;
1137         int pc;
1138
1139         if (tracing_disabled)
1140                 return;
1141
1142         pc = preempt_count();
1143         local_irq_save(flags);
1144         cpu = raw_smp_processor_id();
1145         data = tr->data[cpu];
1146
1147         if (likely(atomic_inc_return(&data->disabled) == 1))
1148                 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1149
1150         atomic_dec(&data->disabled);
1151         local_irq_restore(flags);
1152 }
1153
1154 #ifdef CONFIG_FUNCTION_TRACER
1155 static void
1156 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
1157 {
1158         struct trace_array *tr = &global_trace;
1159         struct trace_array_cpu *data;
1160         unsigned long flags;
1161         long disabled;
1162         int cpu, resched;
1163         int pc;
1164
1165         if (unlikely(!ftrace_function_enabled))
1166                 return;
1167
1168         pc = preempt_count();
1169         resched = ftrace_preempt_disable();
1170         local_save_flags(flags);
1171         cpu = raw_smp_processor_id();
1172         data = tr->data[cpu];
1173         disabled = atomic_inc_return(&data->disabled);
1174
1175         if (likely(disabled == 1))
1176                 trace_function(tr, data, ip, parent_ip, flags, pc);
1177
1178         atomic_dec(&data->disabled);
1179         ftrace_preempt_enable(resched);
1180 }
1181
1182 static void
1183 function_trace_call(unsigned long ip, unsigned long parent_ip)
1184 {
1185         struct trace_array *tr = &global_trace;
1186         struct trace_array_cpu *data;
1187         unsigned long flags;
1188         long disabled;
1189         int cpu;
1190         int pc;
1191
1192         if (unlikely(!ftrace_function_enabled))
1193                 return;
1194
1195         /*
1196          * Need to use raw, since this must be called before the
1197          * recursive protection is performed.
1198          */
1199         local_irq_save(flags);
1200         cpu = raw_smp_processor_id();
1201         data = tr->data[cpu];
1202         disabled = atomic_inc_return(&data->disabled);
1203
1204         if (likely(disabled == 1)) {
1205                 pc = preempt_count();
1206                 trace_function(tr, data, ip, parent_ip, flags, pc);
1207         }
1208
1209         atomic_dec(&data->disabled);
1210         local_irq_restore(flags);
1211 }
1212
1213 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1214 int trace_graph_entry(struct ftrace_graph_ent *trace)
1215 {
1216         struct trace_array *tr = &global_trace;
1217         struct trace_array_cpu *data;
1218         unsigned long flags;
1219         long disabled;
1220         int cpu;
1221         int pc;
1222
1223         if (!ftrace_trace_task(current))
1224                 return 0;
1225
1226         if (!ftrace_graph_addr(trace->func))
1227                 return 0;
1228
1229         local_irq_save(flags);
1230         cpu = raw_smp_processor_id();
1231         data = tr->data[cpu];
1232         disabled = atomic_inc_return(&data->disabled);
1233         if (likely(disabled == 1)) {
1234                 pc = preempt_count();
1235                 __trace_graph_entry(tr, data, trace, flags, pc);
1236         }
1237         /* Only do the atomic if it is not already set */
1238         if (!test_tsk_trace_graph(current))
1239                 set_tsk_trace_graph(current);
1240         atomic_dec(&data->disabled);
1241         local_irq_restore(flags);
1242
1243         return 1;
1244 }
1245
1246 void trace_graph_return(struct ftrace_graph_ret *trace)
1247 {
1248         struct trace_array *tr = &global_trace;
1249         struct trace_array_cpu *data;
1250         unsigned long flags;
1251         long disabled;
1252         int cpu;
1253         int pc;
1254
1255         local_irq_save(flags);
1256         cpu = raw_smp_processor_id();
1257         data = tr->data[cpu];
1258         disabled = atomic_inc_return(&data->disabled);
1259         if (likely(disabled == 1)) {
1260                 pc = preempt_count();
1261                 __trace_graph_return(tr, data, trace, flags, pc);
1262         }
1263         if (!trace->depth)
1264                 clear_tsk_trace_graph(current);
1265         atomic_dec(&data->disabled);
1266         local_irq_restore(flags);
1267 }
1268 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1269
1270 static struct ftrace_ops trace_ops __read_mostly =
1271 {
1272         .func = function_trace_call,
1273 };
1274
1275 void tracing_start_function_trace(void)
1276 {
1277         ftrace_function_enabled = 0;
1278
1279         if (trace_flags & TRACE_ITER_PREEMPTONLY)
1280                 trace_ops.func = function_trace_call_preempt_only;
1281         else
1282                 trace_ops.func = function_trace_call;
1283
1284         register_ftrace_function(&trace_ops);
1285         ftrace_function_enabled = 1;
1286 }
1287
1288 void tracing_stop_function_trace(void)
1289 {
1290         ftrace_function_enabled = 0;
1291         unregister_ftrace_function(&trace_ops);
1292 }
1293 #endif
1294
1295 enum trace_file_type {
1296         TRACE_FILE_LAT_FMT      = 1,
1297         TRACE_FILE_ANNOTATE     = 2,
1298 };
1299
1300 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1301 {
1302         /* Don't allow ftrace to trace into the ring buffers */
1303         ftrace_disable_cpu();
1304
1305         iter->idx++;
1306         if (iter->buffer_iter[iter->cpu])
1307                 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1308
1309         ftrace_enable_cpu();
1310 }
1311
1312 static struct trace_entry *
1313 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1314 {
1315         struct ring_buffer_event *event;
1316         struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1317
1318         /* Don't allow ftrace to trace into the ring buffers */
1319         ftrace_disable_cpu();
1320
1321         if (buf_iter)
1322                 event = ring_buffer_iter_peek(buf_iter, ts);
1323         else
1324                 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1325
1326         ftrace_enable_cpu();
1327
1328         return event ? ring_buffer_event_data(event) : NULL;
1329 }
1330
1331 static struct trace_entry *
1332 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1333 {
1334         struct ring_buffer *buffer = iter->tr->buffer;
1335         struct trace_entry *ent, *next = NULL;
1336         u64 next_ts = 0, ts;
1337         int next_cpu = -1;
1338         int cpu;
1339
1340         for_each_tracing_cpu(cpu) {
1341
1342                 if (ring_buffer_empty_cpu(buffer, cpu))
1343                         continue;
1344
1345                 ent = peek_next_entry(iter, cpu, &ts);
1346
1347                 /*
1348                  * Pick the entry with the smallest timestamp:
1349                  */
1350                 if (ent && (!next || ts < next_ts)) {
1351                         next = ent;
1352                         next_cpu = cpu;
1353                         next_ts = ts;
1354                 }
1355         }
1356
1357         if (ent_cpu)
1358                 *ent_cpu = next_cpu;
1359
1360         if (ent_ts)
1361                 *ent_ts = next_ts;
1362
1363         return next;
1364 }
1365
1366 /* Find the next real entry, without updating the iterator itself */
1367 static struct trace_entry *
1368 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1369 {
1370         return __find_next_entry(iter, ent_cpu, ent_ts);
1371 }
1372
1373 /* Find the next real entry, and increment the iterator to the next entry */
1374 static void *find_next_entry_inc(struct trace_iterator *iter)
1375 {
1376         iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1377
1378         if (iter->ent)
1379                 trace_iterator_increment(iter, iter->cpu);
1380
1381         return iter->ent ? iter : NULL;
1382 }
1383
1384 static void trace_consume(struct trace_iterator *iter)
1385 {
1386         /* Don't allow ftrace to trace into the ring buffers */
1387         ftrace_disable_cpu();
1388         ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1389         ftrace_enable_cpu();
1390 }
1391
1392 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1393 {
1394         struct trace_iterator *iter = m->private;
1395         int i = (int)*pos;
1396         void *ent;
1397
1398         (*pos)++;
1399
1400         /* can't go backwards */
1401         if (iter->idx > i)
1402                 return NULL;
1403
1404         if (iter->idx < 0)
1405                 ent = find_next_entry_inc(iter);
1406         else
1407                 ent = iter;
1408
1409         while (ent && iter->idx < i)
1410                 ent = find_next_entry_inc(iter);
1411
1412         iter->pos = *pos;
1413
1414         return ent;
1415 }
1416
1417 static void *s_start(struct seq_file *m, loff_t *pos)
1418 {
1419         struct trace_iterator *iter = m->private;
1420         void *p = NULL;
1421         loff_t l = 0;
1422         int cpu;
1423
1424         mutex_lock(&trace_types_lock);
1425
1426         if (!current_trace || current_trace != iter->trace) {
1427                 mutex_unlock(&trace_types_lock);
1428                 return NULL;
1429         }
1430
1431         atomic_inc(&trace_record_cmdline_disabled);
1432
1433         if (*pos != iter->pos) {
1434                 iter->ent = NULL;
1435                 iter->cpu = 0;
1436                 iter->idx = -1;
1437
1438                 ftrace_disable_cpu();
1439
1440                 for_each_tracing_cpu(cpu) {
1441                         ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1442                 }
1443
1444                 ftrace_enable_cpu();
1445
1446                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1447                         ;
1448
1449         } else {
1450                 l = *pos - 1;
1451                 p = s_next(m, p, &l);
1452         }
1453
1454         return p;
1455 }
1456
1457 static void s_stop(struct seq_file *m, void *p)
1458 {
1459         atomic_dec(&trace_record_cmdline_disabled);
1460         mutex_unlock(&trace_types_lock);
1461 }
1462
1463 #ifdef CONFIG_KRETPROBES
1464 static inline const char *kretprobed(const char *name)
1465 {
1466         static const char tramp_name[] = "kretprobe_trampoline";
1467         int size = sizeof(tramp_name);
1468
1469         if (strncmp(tramp_name, name, size) == 0)
1470                 return "[unknown/kretprobe'd]";
1471         return name;
1472 }
1473 #else
1474 static inline const char *kretprobed(const char *name)
1475 {
1476         return name;
1477 }
1478 #endif /* CONFIG_KRETPROBES */
1479
1480 static int
1481 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1482 {
1483 #ifdef CONFIG_KALLSYMS
1484         char str[KSYM_SYMBOL_LEN];
1485         const char *name;
1486
1487         kallsyms_lookup(address, NULL, NULL, NULL, str);
1488
1489         name = kretprobed(str);
1490
1491         return trace_seq_printf(s, fmt, name);
1492 #endif
1493         return 1;
1494 }
1495
1496 static int
1497 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1498                      unsigned long address)
1499 {
1500 #ifdef CONFIG_KALLSYMS
1501         char str[KSYM_SYMBOL_LEN];
1502         const char *name;
1503
1504         sprint_symbol(str, address);
1505         name = kretprobed(str);
1506
1507         return trace_seq_printf(s, fmt, name);
1508 #endif
1509         return 1;
1510 }
1511
1512 #ifndef CONFIG_64BIT
1513 # define IP_FMT "%08lx"
1514 #else
1515 # define IP_FMT "%016lx"
1516 #endif
1517
1518 int
1519 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1520 {
1521         int ret;
1522
1523         if (!ip)
1524                 return trace_seq_printf(s, "0");
1525
1526         if (sym_flags & TRACE_ITER_SYM_OFFSET)
1527                 ret = seq_print_sym_offset(s, "%s", ip);
1528         else
1529                 ret = seq_print_sym_short(s, "%s", ip);
1530
1531         if (!ret)
1532                 return 0;
1533
1534         if (sym_flags & TRACE_ITER_SYM_ADDR)
1535                 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1536         return ret;
1537 }
1538
1539 static inline int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
1540                                     unsigned long ip, unsigned long sym_flags)
1541 {
1542         struct file *file = NULL;
1543         unsigned long vmstart = 0;
1544         int ret = 1;
1545
1546         if (mm) {
1547                 const struct vm_area_struct *vma;
1548
1549                 down_read(&mm->mmap_sem);
1550                 vma = find_vma(mm, ip);
1551                 if (vma) {
1552                         file = vma->vm_file;
1553                         vmstart = vma->vm_start;
1554                 }
1555                 if (file) {
1556                         ret = trace_seq_path(s, &file->f_path);
1557                         if (ret)
1558                                 ret = trace_seq_printf(s, "[+0x%lx]", ip - vmstart);
1559                 }
1560                 up_read(&mm->mmap_sem);
1561         }
1562         if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
1563                 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1564         return ret;
1565 }
1566
1567 static int
1568 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
1569                       unsigned long sym_flags)
1570 {
1571         struct mm_struct *mm = NULL;
1572         int ret = 1;
1573         unsigned int i;
1574
1575         if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
1576                 struct task_struct *task;
1577                 /*
1578                  * we do the lookup on the thread group leader,
1579                  * since individual threads might have already quit!
1580                  */
1581                 rcu_read_lock();
1582                 task = find_task_by_vpid(entry->ent.tgid);
1583                 if (task)
1584                         mm = get_task_mm(task);
1585                 rcu_read_unlock();
1586         }
1587
1588         for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1589                 unsigned long ip = entry->caller[i];
1590
1591                 if (ip == ULONG_MAX || !ret)
1592                         break;
1593                 if (i && ret)
1594                         ret = trace_seq_puts(s, " <- ");
1595                 if (!ip) {
1596                         if (ret)
1597                                 ret = trace_seq_puts(s, "??");
1598                         continue;
1599                 }
1600                 if (!ret)
1601                         break;
1602                 if (ret)
1603                         ret = seq_print_user_ip(s, mm, ip, sym_flags);
1604         }
1605
1606         if (mm)
1607                 mmput(mm);
1608         return ret;
1609 }
1610
1611 static void print_lat_help_header(struct seq_file *m)
1612 {
1613         seq_puts(m, "#                  _------=> CPU#            \n");
1614         seq_puts(m, "#                 / _-----=> irqs-off        \n");
1615         seq_puts(m, "#                | / _----=> need-resched    \n");
1616         seq_puts(m, "#                || / _---=> hardirq/softirq \n");
1617         seq_puts(m, "#                ||| / _--=> preempt-depth   \n");
1618         seq_puts(m, "#                |||| /                      \n");
1619         seq_puts(m, "#                |||||     delay             \n");
1620         seq_puts(m, "#  cmd     pid   ||||| time  |   caller      \n");
1621         seq_puts(m, "#     \\   /      |||||   \\   |   /           \n");
1622 }
1623
1624 static void print_func_help_header(struct seq_file *m)
1625 {
1626         seq_puts(m, "#           TASK-PID    CPU#    TIMESTAMP  FUNCTION\n");
1627         seq_puts(m, "#              | |       |          |         |\n");
1628 }
1629
1630
1631 static void
1632 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1633 {
1634         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1635         struct trace_array *tr = iter->tr;
1636         struct trace_array_cpu *data = tr->data[tr->cpu];
1637         struct tracer *type = current_trace;
1638         unsigned long total;
1639         unsigned long entries;
1640         const char *name = "preemption";
1641
1642         if (type)
1643                 name = type->name;
1644
1645         entries = ring_buffer_entries(iter->tr->buffer);
1646         total = entries +
1647                 ring_buffer_overruns(iter->tr->buffer);
1648
1649         seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1650                    name, UTS_RELEASE);
1651         seq_puts(m, "-----------------------------------"
1652                  "---------------------------------\n");
1653         seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1654                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1655                    nsecs_to_usecs(data->saved_latency),
1656                    entries,
1657                    total,
1658                    tr->cpu,
1659 #if defined(CONFIG_PREEMPT_NONE)
1660                    "server",
1661 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1662                    "desktop",
1663 #elif defined(CONFIG_PREEMPT)
1664                    "preempt",
1665 #else
1666                    "unknown",
1667 #endif
1668                    /* These are reserved for later use */
1669                    0, 0, 0, 0);
1670 #ifdef CONFIG_SMP
1671         seq_printf(m, " #P:%d)\n", num_online_cpus());
1672 #else
1673         seq_puts(m, ")\n");
1674 #endif
1675         seq_puts(m, "    -----------------\n");
1676         seq_printf(m, "    | task: %.16s-%d "
1677                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1678                    data->comm, data->pid, data->uid, data->nice,
1679                    data->policy, data->rt_priority);
1680         seq_puts(m, "    -----------------\n");
1681
1682         if (data->critical_start) {
1683                 seq_puts(m, " => started at: ");
1684                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1685                 trace_print_seq(m, &iter->seq);
1686                 seq_puts(m, "\n => ended at:   ");
1687                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1688                 trace_print_seq(m, &iter->seq);
1689                 seq_puts(m, "\n");
1690         }
1691
1692         seq_puts(m, "\n");
1693 }
1694
1695 static void
1696 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1697 {
1698         int hardirq, softirq;
1699         char *comm;
1700
1701         comm = trace_find_cmdline(entry->pid);
1702
1703         trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1704         trace_seq_printf(s, "%3d", cpu);
1705         trace_seq_printf(s, "%c%c",
1706                         (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1707                          (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1708                         ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1709
1710         hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1711         softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1712         if (hardirq && softirq) {
1713                 trace_seq_putc(s, 'H');
1714         } else {
1715                 if (hardirq) {
1716                         trace_seq_putc(s, 'h');
1717                 } else {
1718                         if (softirq)
1719                                 trace_seq_putc(s, 's');
1720                         else
1721                                 trace_seq_putc(s, '.');
1722                 }
1723         }
1724
1725         if (entry->preempt_count)
1726                 trace_seq_printf(s, "%x", entry->preempt_count);
1727         else
1728                 trace_seq_puts(s, ".");
1729 }
1730
1731 unsigned long preempt_mark_thresh = 100;
1732
1733 static void
1734 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1735                     unsigned long rel_usecs)
1736 {
1737         trace_seq_printf(s, " %4lldus", abs_usecs);
1738         if (rel_usecs > preempt_mark_thresh)
1739                 trace_seq_puts(s, "!: ");
1740         else if (rel_usecs > 1)
1741                 trace_seq_puts(s, "+: ");
1742         else
1743                 trace_seq_puts(s, " : ");
1744 }
1745
1746 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1747
1748 /*
1749  * The message is supposed to contain an ending newline.
1750  * If the printing stops prematurely, try to add a newline of our own.
1751  */
1752 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1753 {
1754         struct trace_entry *ent;
1755         struct trace_field_cont *cont;
1756         bool ok = true;
1757
1758         ent = peek_next_entry(iter, iter->cpu, NULL);
1759         if (!ent || ent->type != TRACE_CONT) {
1760                 trace_seq_putc(s, '\n');
1761                 return;
1762         }
1763
1764         do {
1765                 cont = (struct trace_field_cont *)ent;
1766                 if (ok)
1767                         ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1768
1769                 ftrace_disable_cpu();
1770
1771                 if (iter->buffer_iter[iter->cpu])
1772                         ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1773                 else
1774                         ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1775
1776                 ftrace_enable_cpu();
1777
1778                 ent = peek_next_entry(iter, iter->cpu, NULL);
1779         } while (ent && ent->type == TRACE_CONT);
1780
1781         if (!ok)
1782                 trace_seq_putc(s, '\n');
1783 }
1784
1785 static void test_cpu_buff_start(struct trace_iterator *iter)
1786 {
1787         struct trace_seq *s = &iter->seq;
1788
1789         if (!(trace_flags & TRACE_ITER_ANNOTATE))
1790                 return;
1791
1792         if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1793                 return;
1794
1795         if (cpu_isset(iter->cpu, iter->started))
1796                 return;
1797
1798         cpu_set(iter->cpu, iter->started);
1799         trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1800 }
1801
1802 static enum print_line_t
1803 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1804 {
1805         struct trace_seq *s = &iter->seq;
1806         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1807         struct trace_entry *next_entry;
1808         unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1809         struct trace_entry *entry = iter->ent;
1810         unsigned long abs_usecs;
1811         unsigned long rel_usecs;
1812         u64 next_ts;
1813         char *comm;
1814         int S, T;
1815         int i;
1816         unsigned state;
1817
1818         if (entry->type == TRACE_CONT)
1819                 return TRACE_TYPE_HANDLED;
1820
1821         test_cpu_buff_start(iter);
1822
1823         next_entry = find_next_entry(iter, NULL, &next_ts);
1824         if (!next_entry)
1825                 next_ts = iter->ts;
1826         rel_usecs = ns2usecs(next_ts - iter->ts);
1827         abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1828
1829         if (verbose) {
1830                 comm = trace_find_cmdline(entry->pid);
1831                 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1832                                  " %ld.%03ldms (+%ld.%03ldms): ",
1833                                  comm,
1834                                  entry->pid, cpu, entry->flags,
1835                                  entry->preempt_count, trace_idx,
1836                                  ns2usecs(iter->ts),
1837                                  abs_usecs/1000,
1838                                  abs_usecs % 1000, rel_usecs/1000,
1839                                  rel_usecs % 1000);
1840         } else {
1841                 lat_print_generic(s, entry, cpu);
1842                 lat_print_timestamp(s, abs_usecs, rel_usecs);
1843         }
1844         switch (entry->type) {
1845         case TRACE_FN: {
1846                 struct ftrace_entry *field;
1847
1848                 trace_assign_type(field, entry);
1849
1850                 seq_print_ip_sym(s, field->ip, sym_flags);
1851                 trace_seq_puts(s, " (");
1852                 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1853                 trace_seq_puts(s, ")\n");
1854                 break;
1855         }
1856         case TRACE_CTX:
1857         case TRACE_WAKE: {
1858                 struct ctx_switch_entry *field;
1859
1860                 trace_assign_type(field, entry);
1861
1862                 T = field->next_state < sizeof(state_to_char) ?
1863                         state_to_char[field->next_state] : 'X';
1864
1865                 state = field->prev_state ?
1866                         __ffs(field->prev_state) + 1 : 0;
1867                 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1868                 comm = trace_find_cmdline(field->next_pid);
1869                 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1870                                  field->prev_pid,
1871                                  field->prev_prio,
1872                                  S, entry->type == TRACE_CTX ? "==>" : "  +",
1873                                  field->next_cpu,
1874                                  field->next_pid,
1875                                  field->next_prio,
1876                                  T, comm);
1877                 break;
1878         }
1879         case TRACE_SPECIAL: {
1880                 struct special_entry *field;
1881
1882                 trace_assign_type(field, entry);
1883
1884                 trace_seq_printf(s, "# %ld %ld %ld\n",
1885                                  field->arg1,
1886                                  field->arg2,
1887                                  field->arg3);
1888                 break;
1889         }
1890         case TRACE_STACK: {
1891                 struct stack_entry *field;
1892
1893                 trace_assign_type(field, entry);
1894
1895                 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1896                         if (i)
1897                                 trace_seq_puts(s, " <= ");
1898                         seq_print_ip_sym(s, field->caller[i], sym_flags);
1899                 }
1900                 trace_seq_puts(s, "\n");
1901                 break;
1902         }
1903         case TRACE_PRINT: {
1904                 struct print_entry *field;
1905
1906                 trace_assign_type(field, entry);
1907
1908                 seq_print_ip_sym(s, field->ip, sym_flags);
1909                 trace_seq_printf(s, ": %s", field->buf);
1910                 if (entry->flags & TRACE_FLAG_CONT)
1911                         trace_seq_print_cont(s, iter);
1912                 break;
1913         }
1914         case TRACE_BRANCH: {
1915                 struct trace_branch *field;
1916
1917                 trace_assign_type(field, entry);
1918
1919                 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1920                                  field->correct ? "  ok  " : " MISS ",
1921                                  field->func,
1922                                  field->file,
1923                                  field->line);
1924                 break;
1925         }
1926         case TRACE_USER_STACK: {
1927                 struct userstack_entry *field;
1928
1929                 trace_assign_type(field, entry);
1930
1931                 seq_print_userip_objs(field, s, sym_flags);
1932                 trace_seq_putc(s, '\n');
1933                 break;
1934         }
1935         default:
1936                 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1937         }
1938         return TRACE_TYPE_HANDLED;
1939 }
1940
1941 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1942 {
1943         struct trace_seq *s = &iter->seq;
1944         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1945         struct trace_entry *entry;
1946         unsigned long usec_rem;
1947         unsigned long long t;
1948         unsigned long secs;
1949         char *comm;
1950         int ret;
1951         int S, T;
1952         int i;
1953
1954         entry = iter->ent;
1955
1956         if (entry->type == TRACE_CONT)
1957                 return TRACE_TYPE_HANDLED;
1958
1959         test_cpu_buff_start(iter);
1960
1961         comm = trace_find_cmdline(iter->ent->pid);
1962
1963         t = ns2usecs(iter->ts);
1964         usec_rem = do_div(t, 1000000ULL);
1965         secs = (unsigned long)t;
1966
1967         ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1968         if (!ret)
1969                 return TRACE_TYPE_PARTIAL_LINE;
1970         ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1971         if (!ret)
1972                 return TRACE_TYPE_PARTIAL_LINE;
1973         ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1974         if (!ret)
1975                 return TRACE_TYPE_PARTIAL_LINE;
1976
1977         switch (entry->type) {
1978         case TRACE_FN: {
1979                 struct ftrace_entry *field;
1980
1981                 trace_assign_type(field, entry);
1982
1983                 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1984                 if (!ret)
1985                         return TRACE_TYPE_PARTIAL_LINE;
1986                 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1987                                                 field->parent_ip) {
1988                         ret = trace_seq_printf(s, " <-");
1989                         if (!ret)
1990                                 return TRACE_TYPE_PARTIAL_LINE;
1991                         ret = seq_print_ip_sym(s,
1992                                                field->parent_ip,
1993                                                sym_flags);
1994                         if (!ret)
1995                                 return TRACE_TYPE_PARTIAL_LINE;
1996                 }
1997                 ret = trace_seq_printf(s, "\n");
1998                 if (!ret)
1999                         return TRACE_TYPE_PARTIAL_LINE;
2000                 break;
2001         }
2002         case TRACE_CTX:
2003         case TRACE_WAKE: {
2004                 struct ctx_switch_entry *field;
2005
2006                 trace_assign_type(field, entry);
2007
2008                 S = field->prev_state < sizeof(state_to_char) ?
2009                         state_to_char[field->prev_state] : 'X';
2010                 T = field->next_state < sizeof(state_to_char) ?
2011                         state_to_char[field->next_state] : 'X';
2012                 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
2013                                        field->prev_pid,
2014                                        field->prev_prio,
2015                                        S,
2016                                        entry->type == TRACE_CTX ? "==>" : "  +",
2017                                        field->next_cpu,
2018                                        field->next_pid,
2019                                        field->next_prio,
2020                                        T);
2021                 if (!ret)
2022                         return TRACE_TYPE_PARTIAL_LINE;
2023                 break;
2024         }
2025         case TRACE_SPECIAL: {
2026                 struct special_entry *field;
2027
2028                 trace_assign_type(field, entry);
2029
2030                 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
2031                                  field->arg1,
2032                                  field->arg2,
2033                                  field->arg3);
2034                 if (!ret)
2035                         return TRACE_TYPE_PARTIAL_LINE;
2036                 break;
2037         }
2038         case TRACE_STACK: {
2039                 struct stack_entry *field;
2040
2041                 trace_assign_type(field, entry);
2042
2043                 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
2044                         if (i) {
2045                                 ret = trace_seq_puts(s, " <= ");
2046                                 if (!ret)
2047                                         return TRACE_TYPE_PARTIAL_LINE;
2048                         }
2049                         ret = seq_print_ip_sym(s, field->caller[i],
2050                                                sym_flags);
2051                         if (!ret)
2052                                 return TRACE_TYPE_PARTIAL_LINE;
2053                 }
2054                 ret = trace_seq_puts(s, "\n");
2055                 if (!ret)
2056                         return TRACE_TYPE_PARTIAL_LINE;
2057                 break;
2058         }
2059         case TRACE_PRINT: {
2060                 struct print_entry *field;
2061
2062                 trace_assign_type(field, entry);
2063
2064                 seq_print_ip_sym(s, field->ip, sym_flags);
2065                 trace_seq_printf(s, ": %s", field->buf);
2066                 if (entry->flags & TRACE_FLAG_CONT)
2067                         trace_seq_print_cont(s, iter);
2068                 break;
2069         }
2070         case TRACE_GRAPH_RET: {
2071                 return print_graph_function(iter);
2072         }
2073         case TRACE_GRAPH_ENT: {
2074                 return print_graph_function(iter);
2075         }
2076         case TRACE_BRANCH: {
2077                 struct trace_branch *field;
2078
2079                 trace_assign_type(field, entry);
2080
2081                 trace_seq_printf(s, "[%s] %s:%s:%d\n",
2082                                  field->correct ? "  ok  " : " MISS ",
2083                                  field->func,
2084                                  field->file,
2085                                  field->line);
2086                 break;
2087         }
2088         case TRACE_USER_STACK: {
2089                 struct userstack_entry *field;
2090
2091                 trace_assign_type(field, entry);
2092
2093                 ret = seq_print_userip_objs(field, s, sym_flags);
2094                 if (!ret)
2095                         return TRACE_TYPE_PARTIAL_LINE;
2096                 ret = trace_seq_putc(s, '\n');
2097                 if (!ret)
2098                         return TRACE_TYPE_PARTIAL_LINE;
2099                 break;
2100         }
2101         }
2102         return TRACE_TYPE_HANDLED;
2103 }
2104
2105 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2106 {
2107         struct trace_seq *s = &iter->seq;
2108         struct trace_entry *entry;
2109         int ret;
2110         int S, T;
2111
2112         entry = iter->ent;
2113
2114         if (entry->type == TRACE_CONT)
2115                 return TRACE_TYPE_HANDLED;
2116
2117         ret = trace_seq_printf(s, "%d %d %llu ",
2118                 entry->pid, iter->cpu, iter->ts);
2119         if (!ret)
2120                 return TRACE_TYPE_PARTIAL_LINE;
2121
2122         switch (entry->type) {
2123         case TRACE_FN: {
2124                 struct ftrace_entry *field;
2125
2126                 trace_assign_type(field, entry);
2127
2128                 ret = trace_seq_printf(s, "%x %x\n",
2129                                         field->ip,
2130                                         field->parent_ip);
2131                 if (!ret)
2132                         return TRACE_TYPE_PARTIAL_LINE;
2133                 break;
2134         }
2135         case TRACE_CTX:
2136         case TRACE_WAKE: {
2137                 struct ctx_switch_entry *field;
2138
2139                 trace_assign_type(field, entry);
2140
2141                 S = field->prev_state < sizeof(state_to_char) ?
2142                         state_to_char[field->prev_state] : 'X';
2143                 T = field->next_state < sizeof(state_to_char) ?
2144                         state_to_char[field->next_state] : 'X';
2145                 if (entry->type == TRACE_WAKE)
2146                         S = '+';
2147                 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
2148                                        field->prev_pid,
2149                                        field->prev_prio,
2150                                        S,
2151                                        field->next_cpu,
2152                                        field->next_pid,
2153                                        field->next_prio,
2154                                        T);
2155                 if (!ret)
2156                         return TRACE_TYPE_PARTIAL_LINE;
2157                 break;
2158         }
2159         case TRACE_SPECIAL:
2160         case TRACE_USER_STACK:
2161         case TRACE_STACK: {
2162                 struct special_entry *field;
2163
2164                 trace_assign_type(field, entry);
2165
2166                 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
2167                                  field->arg1,
2168                                  field->arg2,
2169                                  field->arg3);
2170                 if (!ret)
2171                         return TRACE_TYPE_PARTIAL_LINE;
2172                 break;
2173         }
2174         case TRACE_PRINT: {
2175                 struct print_entry *field;
2176
2177                 trace_assign_type(field, entry);
2178
2179                 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
2180                 if (entry->flags & TRACE_FLAG_CONT)
2181                         trace_seq_print_cont(s, iter);
2182                 break;
2183         }
2184         }
2185         return TRACE_TYPE_HANDLED;
2186 }
2187
2188 #define SEQ_PUT_FIELD_RET(s, x)                         \
2189 do {                                                    \
2190         if (!trace_seq_putmem(s, &(x), sizeof(x)))      \
2191                 return 0;                               \
2192 } while (0)
2193
2194 #define SEQ_PUT_HEX_FIELD_RET(s, x)                     \
2195 do {                                                    \
2196         BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES);     \
2197         if (!trace_seq_putmem_hex(s, &(x), sizeof(x)))  \
2198                 return 0;                               \
2199 } while (0)
2200
2201 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2202 {
2203         struct trace_seq *s = &iter->seq;
2204         unsigned char newline = '\n';
2205         struct trace_entry *entry;
2206         int S, T;
2207
2208         entry = iter->ent;
2209
2210         if (entry->type == TRACE_CONT)
2211                 return TRACE_TYPE_HANDLED;
2212
2213         SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2214         SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2215         SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2216
2217         switch (entry->type) {
2218         case TRACE_FN: {
2219                 struct ftrace_entry *field;
2220
2221                 trace_assign_type(field, entry);
2222
2223                 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
2224                 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
2225                 break;
2226         }
2227         case TRACE_CTX:
2228         case TRACE_WAKE: {
2229                 struct ctx_switch_entry *field;
2230
2231                 trace_assign_type(field, entry);
2232
2233                 S = field->prev_state < sizeof(state_to_char) ?
2234                         state_to_char[field->prev_state] : 'X';
2235                 T = field->next_state < sizeof(state_to_char) ?
2236                         state_to_char[field->next_state] : 'X';
2237                 if (entry->type == TRACE_WAKE)
2238                         S = '+';
2239                 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
2240                 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
2241                 SEQ_PUT_HEX_FIELD_RET(s, S);
2242                 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
2243                 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
2244                 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
2245                 SEQ_PUT_HEX_FIELD_RET(s, T);
2246                 break;
2247         }
2248         case TRACE_SPECIAL:
2249         case TRACE_USER_STACK:
2250         case TRACE_STACK: {
2251                 struct special_entry *field;
2252
2253                 trace_assign_type(field, entry);
2254
2255                 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
2256                 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
2257                 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
2258                 break;
2259         }
2260         }
2261         SEQ_PUT_FIELD_RET(s, newline);
2262
2263         return TRACE_TYPE_HANDLED;
2264 }
2265
2266 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2267 {
2268         struct trace_seq *s = &iter->seq;
2269         struct trace_entry *entry;
2270
2271         entry = iter->ent;
2272
2273         if (entry->type == TRACE_CONT)
2274                 return TRACE_TYPE_HANDLED;
2275
2276         SEQ_PUT_FIELD_RET(s, entry->pid);
2277         SEQ_PUT_FIELD_RET(s, entry->cpu);
2278         SEQ_PUT_FIELD_RET(s, iter->ts);
2279
2280         switch (entry->type) {
2281         case TRACE_FN: {
2282                 struct ftrace_entry *field;
2283
2284                 trace_assign_type(field, entry);
2285
2286                 SEQ_PUT_FIELD_RET(s, field->ip);
2287                 SEQ_PUT_FIELD_RET(s, field->parent_ip);
2288                 break;
2289         }
2290         case TRACE_CTX: {
2291                 struct ctx_switch_entry *field;
2292
2293                 trace_assign_type(field, entry);
2294
2295                 SEQ_PUT_FIELD_RET(s, field->prev_pid);
2296                 SEQ_PUT_FIELD_RET(s, field->prev_prio);
2297                 SEQ_PUT_FIELD_RET(s, field->prev_state);
2298                 SEQ_PUT_FIELD_RET(s, field->next_pid);
2299                 SEQ_PUT_FIELD_RET(s, field->next_prio);
2300                 SEQ_PUT_FIELD_RET(s, field->next_state);
2301                 break;
2302         }
2303         case TRACE_SPECIAL:
2304         case TRACE_USER_STACK:
2305         case TRACE_STACK: {
2306                 struct special_entry *field;
2307
2308                 trace_assign_type(field, entry);
2309
2310                 SEQ_PUT_FIELD_RET(s, field->arg1);
2311                 SEQ_PUT_FIELD_RET(s, field->arg2);
2312                 SEQ_PUT_FIELD_RET(s, field->arg3);
2313                 break;
2314         }
2315         }
2316         return 1;
2317 }
2318
2319 static int trace_empty(struct trace_iterator *iter)
2320 {
2321         int cpu;
2322
2323         for_each_tracing_cpu(cpu) {
2324                 if (iter->buffer_iter[cpu]) {
2325                         if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2326                                 return 0;
2327                 } else {
2328                         if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2329                                 return 0;
2330                 }
2331         }
2332
2333         return 1;
2334 }
2335
2336 static enum print_line_t print_trace_line(struct trace_iterator *iter)
2337 {
2338         enum print_line_t ret;
2339
2340         if (iter->trace && iter->trace->print_line) {
2341                 ret = iter->trace->print_line(iter);
2342                 if (ret != TRACE_TYPE_UNHANDLED)
2343                         return ret;
2344         }
2345
2346         if (trace_flags & TRACE_ITER_BIN)
2347                 return print_bin_fmt(iter);
2348
2349         if (trace_flags & TRACE_ITER_HEX)
2350                 return print_hex_fmt(iter);
2351
2352         if (trace_flags & TRACE_ITER_RAW)
2353                 return print_raw_fmt(iter);
2354
2355         if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2356                 return print_lat_fmt(iter, iter->idx, iter->cpu);
2357
2358         return print_trace_fmt(iter);
2359 }
2360
2361 static int s_show(struct seq_file *m, void *v)
2362 {
2363         struct trace_iterator *iter = v;
2364
2365         if (iter->ent == NULL) {
2366                 if (iter->tr) {
2367                         seq_printf(m, "# tracer: %s\n", iter->trace->name);
2368                         seq_puts(m, "#\n");
2369                 }
2370                 if (iter->trace && iter->trace->print_header)
2371                         iter->trace->print_header(m);
2372                 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2373                         /* print nothing if the buffers are empty */
2374                         if (trace_empty(iter))
2375                                 return 0;
2376                         print_trace_header(m, iter);
2377                         if (!(trace_flags & TRACE_ITER_VERBOSE))
2378                                 print_lat_help_header(m);
2379                 } else {
2380                         if (!(trace_flags & TRACE_ITER_VERBOSE))
2381                                 print_func_help_header(m);
2382                 }
2383         } else {
2384                 print_trace_line(iter);
2385                 trace_print_seq(m, &iter->seq);
2386         }
2387
2388         return 0;
2389 }
2390
2391 static struct seq_operations tracer_seq_ops = {
2392         .start          = s_start,
2393         .next           = s_next,
2394         .stop           = s_stop,
2395         .show           = s_show,
2396 };
2397
2398 static struct trace_iterator *
2399 __tracing_open(struct inode *inode, struct file *file, int *ret)
2400 {
2401         struct trace_iterator *iter;
2402         struct seq_file *m;
2403         int cpu;
2404
2405         if (tracing_disabled) {
2406                 *ret = -ENODEV;
2407                 return NULL;
2408         }
2409
2410         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2411         if (!iter) {
2412                 *ret = -ENOMEM;
2413                 goto out;
2414         }
2415
2416         mutex_lock(&trace_types_lock);
2417         if (current_trace && current_trace->print_max)
2418                 iter->tr = &max_tr;
2419         else
2420                 iter->tr = inode->i_private;
2421         iter->trace = current_trace;
2422         iter->pos = -1;
2423
2424         /* Notify the tracer early; before we stop tracing. */
2425         if (iter->trace && iter->trace->open)
2426                         iter->trace->open(iter);
2427
2428         /* Annotate start of buffers if we had overruns */
2429         if (ring_buffer_overruns(iter->tr->buffer))
2430                 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2431
2432
2433         for_each_tracing_cpu(cpu) {
2434
2435                 iter->buffer_iter[cpu] =
2436                         ring_buffer_read_start(iter->tr->buffer, cpu);
2437
2438                 if (!iter->buffer_iter[cpu])
2439                         goto fail_buffer;
2440         }
2441
2442         /* TODO stop tracer */
2443         *ret = seq_open(file, &tracer_seq_ops);
2444         if (*ret)
2445                 goto fail_buffer;
2446
2447         m = file->private_data;
2448         m->private = iter;
2449
2450         /* stop the trace while dumping */
2451         tracing_stop();
2452
2453         mutex_unlock(&trace_types_lock);
2454
2455  out:
2456         return iter;
2457
2458  fail_buffer:
2459         for_each_tracing_cpu(cpu) {
2460                 if (iter->buffer_iter[cpu])
2461                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
2462         }
2463         mutex_unlock(&trace_types_lock);
2464         kfree(iter);
2465
2466         return ERR_PTR(-ENOMEM);
2467 }
2468
2469 int tracing_open_generic(struct inode *inode, struct file *filp)
2470 {
2471         if (tracing_disabled)
2472                 return -ENODEV;
2473
2474         filp->private_data = inode->i_private;
2475         return 0;
2476 }
2477
2478 int tracing_release(struct inode *inode, struct file *file)
2479 {
2480         struct seq_file *m = (struct seq_file *)file->private_data;
2481         struct trace_iterator *iter = m->private;
2482         int cpu;
2483
2484         mutex_lock(&trace_types_lock);
2485         for_each_tracing_cpu(cpu) {
2486                 if (iter->buffer_iter[cpu])
2487                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
2488         }
2489
2490         if (iter->trace && iter->trace->close)
2491                 iter->trace->close(iter);
2492
2493         /* reenable tracing if it was previously enabled */
2494         tracing_start();
2495         mutex_unlock(&trace_types_lock);
2496
2497         seq_release(inode, file);
2498         kfree(iter);
2499         return 0;
2500 }
2501
2502 static int tracing_open(struct inode *inode, struct file *file)
2503 {
2504         int ret;
2505
2506         __tracing_open(inode, file, &ret);
2507
2508         return ret;
2509 }
2510
2511 static int tracing_lt_open(struct inode *inode, struct file *file)
2512 {
2513         struct trace_iterator *iter;
2514         int ret;
2515
2516         iter = __tracing_open(inode, file, &ret);
2517
2518         if (!ret)
2519                 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2520
2521         return ret;
2522 }
2523
2524
2525 static void *
2526 t_next(struct seq_file *m, void *v, loff_t *pos)
2527 {
2528         struct tracer *t = m->private;
2529
2530         (*pos)++;
2531
2532         if (t)
2533                 t = t->next;
2534
2535         m->private = t;
2536
2537         return t;
2538 }
2539
2540 static void *t_start(struct seq_file *m, loff_t *pos)
2541 {
2542         struct tracer *t = m->private;
2543         loff_t l = 0;
2544
2545         mutex_lock(&trace_types_lock);
2546         for (; t && l < *pos; t = t_next(m, t, &l))
2547                 ;
2548
2549         return t;
2550 }
2551
2552 static void t_stop(struct seq_file *m, void *p)
2553 {
2554         mutex_unlock(&trace_types_lock);
2555 }
2556
2557 static int t_show(struct seq_file *m, void *v)
2558 {
2559         struct tracer *t = v;
2560
2561         if (!t)
2562                 return 0;
2563
2564         seq_printf(m, "%s", t->name);
2565         if (t->next)
2566                 seq_putc(m, ' ');
2567         else
2568                 seq_putc(m, '\n');
2569
2570         return 0;
2571 }
2572
2573 static struct seq_operations show_traces_seq_ops = {
2574         .start          = t_start,
2575         .next           = t_next,
2576         .stop           = t_stop,
2577         .show           = t_show,
2578 };
2579
2580 static int show_traces_open(struct inode *inode, struct file *file)
2581 {
2582         int ret;
2583
2584         if (tracing_disabled)
2585                 return -ENODEV;
2586
2587         ret = seq_open(file, &show_traces_seq_ops);
2588         if (!ret) {
2589                 struct seq_file *m = file->private_data;
2590                 m->private = trace_types;
2591         }
2592
2593         return ret;
2594 }
2595
2596 static struct file_operations tracing_fops = {
2597         .open           = tracing_open,
2598         .read           = seq_read,
2599         .llseek         = seq_lseek,
2600         .release        = tracing_release,
2601 };
2602
2603 static struct file_operations tracing_lt_fops = {
2604         .open           = tracing_lt_open,
2605         .read           = seq_read,
2606         .llseek         = seq_lseek,
2607         .release        = tracing_release,
2608 };
2609
2610 static struct file_operations show_traces_fops = {
2611         .open           = show_traces_open,
2612         .read           = seq_read,
2613         .release        = seq_release,
2614 };
2615
2616 /*
2617  * Only trace on a CPU if the bitmask is set:
2618  */
2619 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2620
2621 /*
2622  * When tracing/tracing_cpu_mask is modified then this holds
2623  * the new bitmask we are about to install:
2624  */
2625 static cpumask_t tracing_cpumask_new;
2626
2627 /*
2628  * The tracer itself will not take this lock, but still we want
2629  * to provide a consistent cpumask to user-space:
2630  */
2631 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2632
2633 /*
2634  * Temporary storage for the character representation of the
2635  * CPU bitmask (and one more byte for the newline):
2636  */
2637 static char mask_str[NR_CPUS + 1];
2638
2639 static ssize_t
2640 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2641                      size_t count, loff_t *ppos)
2642 {
2643         int len;
2644
2645         mutex_lock(&tracing_cpumask_update_lock);
2646
2647         len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2648         if (count - len < 2) {
2649                 count = -EINVAL;
2650                 goto out_err;
2651         }
2652         len += sprintf(mask_str + len, "\n");
2653         count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2654
2655 out_err:
2656         mutex_unlock(&tracing_cpumask_update_lock);
2657
2658         return count;
2659 }
2660
2661 static ssize_t
2662 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2663                       size_t count, loff_t *ppos)
2664 {
2665         int err, cpu;
2666
2667         mutex_lock(&tracing_cpumask_update_lock);
2668         err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2669         if (err)
2670                 goto err_unlock;
2671
2672         local_irq_disable();
2673         __raw_spin_lock(&ftrace_max_lock);
2674         for_each_tracing_cpu(cpu) {
2675                 /*
2676                  * Increase/decrease the disabled counter if we are
2677                  * about to flip a bit in the cpumask:
2678                  */
2679                 if (cpu_isset(cpu, tracing_cpumask) &&
2680                                 !cpu_isset(cpu, tracing_cpumask_new)) {
2681                         atomic_inc(&global_trace.data[cpu]->disabled);
2682                 }
2683                 if (!cpu_isset(cpu, tracing_cpumask) &&
2684                                 cpu_isset(cpu, tracing_cpumask_new)) {
2685                         atomic_dec(&global_trace.data[cpu]->disabled);
2686                 }
2687         }
2688         __raw_spin_unlock(&ftrace_max_lock);
2689         local_irq_enable();
2690
2691         tracing_cpumask = tracing_cpumask_new;
2692
2693         mutex_unlock(&tracing_cpumask_update_lock);
2694
2695         return count;
2696
2697 err_unlock:
2698         mutex_unlock(&tracing_cpumask_update_lock);
2699
2700         return err;
2701 }
2702
2703 static struct file_operations tracing_cpumask_fops = {
2704         .open           = tracing_open_generic,
2705         .read           = tracing_cpumask_read,
2706         .write          = tracing_cpumask_write,
2707 };
2708
2709 static ssize_t
2710 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2711                        size_t cnt, loff_t *ppos)
2712 {
2713         int i;
2714         char *buf;
2715         int r = 0;
2716         int len = 0;
2717         u32 tracer_flags = current_trace->flags->val;
2718         struct tracer_opt *trace_opts = current_trace->flags->opts;
2719
2720
2721         /* calulate max size */
2722         for (i = 0; trace_options[i]; i++) {
2723                 len += strlen(trace_options[i]);
2724                 len += 3; /* "no" and space */
2725         }
2726
2727         /*
2728          * Increase the size with names of options specific
2729          * of the current tracer.
2730          */
2731         for (i = 0; trace_opts[i].name; i++) {
2732                 len += strlen(trace_opts[i].name);
2733                 len += 3; /* "no" and space */
2734         }
2735
2736         /* +2 for \n and \0 */
2737         buf = kmalloc(len + 2, GFP_KERNEL);
2738         if (!buf)
2739                 return -ENOMEM;
2740
2741         for (i = 0; trace_options[i]; i++) {
2742                 if (trace_flags & (1 << i))
2743                         r += sprintf(buf + r, "%s ", trace_options[i]);
2744                 else
2745                         r += sprintf(buf + r, "no%s ", trace_options[i]);
2746         }
2747
2748         for (i = 0; trace_opts[i].name; i++) {
2749                 if (tracer_flags & trace_opts[i].bit)
2750                         r += sprintf(buf + r, "%s ",
2751                                 trace_opts[i].name);
2752                 else
2753                         r += sprintf(buf + r, "no%s ",
2754                                 trace_opts[i].name);
2755         }
2756
2757         r += sprintf(buf + r, "\n");
2758         WARN_ON(r >= len + 2);
2759
2760         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2761
2762         kfree(buf);
2763
2764         return r;
2765 }
2766
2767 /* Try to assign a tracer specific option */
2768 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2769 {
2770         struct tracer_flags *trace_flags = trace->flags;
2771         struct tracer_opt *opts = NULL;
2772         int ret = 0, i = 0;
2773         int len;
2774
2775         for (i = 0; trace_flags->opts[i].name; i++) {
2776                 opts = &trace_flags->opts[i];
2777                 len = strlen(opts->name);
2778
2779                 if (strncmp(cmp, opts->name, len) == 0) {
2780                         ret = trace->set_flag(trace_flags->val,
2781                                 opts->bit, !neg);
2782                         break;
2783                 }
2784         }
2785         /* Not found */
2786         if (!trace_flags->opts[i].name)
2787                 return -EINVAL;
2788
2789         /* Refused to handle */
2790         if (ret)
2791                 return ret;
2792
2793         if (neg)
2794                 trace_flags->val &= ~opts->bit;
2795         else
2796                 trace_flags->val |= opts->bit;
2797
2798         return 0;
2799 }
2800
2801 static ssize_t
2802 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2803                         size_t cnt, loff_t *ppos)
2804 {
2805         char buf[64];
2806         char *cmp = buf;
2807         int neg = 0;
2808         int ret;
2809         int i;
2810
2811         if (cnt >= sizeof(buf))
2812                 return -EINVAL;
2813
2814         if (copy_from_user(&buf, ubuf, cnt))
2815                 return -EFAULT;
2816
2817         buf[cnt] = 0;
2818
2819         if (strncmp(buf, "no", 2) == 0) {
2820                 neg = 1;
2821                 cmp += 2;
2822         }
2823
2824         for (i = 0; trace_options[i]; i++) {
2825                 int len = strlen(trace_options[i]);
2826
2827                 if (strncmp(cmp, trace_options[i], len) == 0) {
2828                         if (neg)
2829                                 trace_flags &= ~(1 << i);
2830                         else
2831                                 trace_flags |= (1 << i);
2832                         break;
2833                 }
2834         }
2835
2836         /* If no option could be set, test the specific tracer options */
2837         if (!trace_options[i]) {
2838                 ret = set_tracer_option(current_trace, cmp, neg);
2839                 if (ret)
2840                         return ret;
2841         }
2842
2843         filp->f_pos += cnt;
2844
2845         return cnt;
2846 }
2847
2848 static struct file_operations tracing_iter_fops = {
2849         .open           = tracing_open_generic,
2850         .read           = tracing_trace_options_read,
2851         .write          = tracing_trace_options_write,
2852 };
2853
2854 static const char readme_msg[] =
2855         "tracing mini-HOWTO:\n\n"
2856         "# mkdir /debug\n"
2857         "# mount -t debugfs nodev /debug\n\n"
2858         "# cat /debug/tracing/available_tracers\n"
2859         "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2860         "# cat /debug/tracing/current_tracer\n"
2861         "none\n"
2862         "# echo sched_switch > /debug/tracing/current_tracer\n"
2863         "# cat /debug/tracing/current_tracer\n"
2864         "sched_switch\n"
2865         "# cat /debug/tracing/trace_options\n"
2866         "noprint-parent nosym-offset nosym-addr noverbose\n"
2867         "# echo print-parent > /debug/tracing/trace_options\n"
2868         "# echo 1 > /debug/tracing/tracing_enabled\n"
2869         "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2870         "echo 0 > /debug/tracing/tracing_enabled\n"
2871 ;
2872
2873 static ssize_t
2874 tracing_readme_read(struct file *filp, char __user *ubuf,
2875                        size_t cnt, loff_t *ppos)
2876 {
2877         return simple_read_from_buffer(ubuf, cnt, ppos,
2878                                         readme_msg, strlen(readme_msg));
2879 }
2880
2881 static struct file_operations tracing_readme_fops = {
2882         .open           = tracing_open_generic,
2883         .read           = tracing_readme_read,
2884 };
2885
2886 static ssize_t
2887 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2888                   size_t cnt, loff_t *ppos)
2889 {
2890         char buf[64];
2891         int r;
2892
2893         r = sprintf(buf, "%u\n", tracer_enabled);
2894         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2895 }
2896
2897 static ssize_t
2898 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2899                    size_t cnt, loff_t *ppos)
2900 {
2901         struct trace_array *tr = filp->private_data;
2902         char buf[64];
2903         long val;
2904         int ret;
2905
2906         if (cnt >= sizeof(buf))
2907                 return -EINVAL;
2908
2909         if (copy_from_user(&buf, ubuf, cnt))
2910                 return -EFAULT;
2911
2912         buf[cnt] = 0;
2913
2914         ret = strict_strtoul(buf, 10, &val);
2915         if (ret < 0)
2916                 return ret;
2917
2918         val = !!val;
2919
2920         mutex_lock(&trace_types_lock);
2921         if (tracer_enabled ^ val) {
2922                 if (val) {
2923                         tracer_enabled = 1;
2924                         if (current_trace->start)
2925                                 current_trace->start(tr);
2926                         tracing_start();
2927                 } else {
2928                         tracer_enabled = 0;
2929                         tracing_stop();
2930                         if (current_trace->stop)
2931                                 current_trace->stop(tr);
2932                 }
2933         }
2934         mutex_unlock(&trace_types_lock);
2935
2936         filp->f_pos += cnt;
2937
2938         return cnt;
2939 }
2940
2941 static ssize_t
2942 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2943                        size_t cnt, loff_t *ppos)
2944 {
2945         char buf[max_tracer_type_len+2];
2946         int r;
2947
2948         mutex_lock(&trace_types_lock);
2949         if (current_trace)
2950                 r = sprintf(buf, "%s\n", current_trace->name);
2951         else
2952                 r = sprintf(buf, "\n");
2953         mutex_unlock(&trace_types_lock);
2954
2955         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2956 }
2957
2958 static int tracing_set_tracer(char *buf)
2959 {
2960         struct trace_array *tr = &global_trace;
2961         struct tracer *t;
2962         int ret = 0;
2963
2964         mutex_lock(&trace_types_lock);
2965         for (t = trace_types; t; t = t->next) {
2966                 if (strcmp(t->name, buf) == 0)
2967                         break;
2968         }
2969         if (!t) {
2970                 ret = -EINVAL;
2971                 goto out;
2972         }
2973         if (t == current_trace)
2974                 goto out;
2975
2976         trace_branch_disable();
2977         if (current_trace && current_trace->reset)
2978                 current_trace->reset(tr);
2979
2980         current_trace = t;
2981         if (t->init) {
2982                 ret = t->init(tr);
2983                 if (ret)
2984                         goto out;
2985         }
2986
2987         trace_branch_enable(tr);
2988  out:
2989         mutex_unlock(&trace_types_lock);
2990
2991         return ret;
2992 }
2993
2994 static ssize_t
2995 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2996                         size_t cnt, loff_t *ppos)
2997 {
2998         char buf[max_tracer_type_len+1];
2999         int i;
3000         size_t ret;
3001         int err;
3002
3003         ret = cnt;
3004
3005         if (cnt > max_tracer_type_len)
3006                 cnt = max_tracer_type_len;
3007
3008         if (copy_from_user(&buf, ubuf, cnt))
3009                 return -EFAULT;
3010
3011         buf[cnt] = 0;
3012
3013         /* strip ending whitespace. */
3014         for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3015                 buf[i] = 0;
3016
3017         err = tracing_set_tracer(buf);
3018         if (err)
3019                 return err;
3020
3021         filp->f_pos += ret;
3022
3023         return ret;
3024 }
3025
3026 static ssize_t
3027 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3028                      size_t cnt, loff_t *ppos)
3029 {
3030         unsigned long *ptr = filp->private_data;
3031         char buf[64];
3032         int r;
3033
3034         r = snprintf(buf, sizeof(buf), "%ld\n",
3035                      *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3036         if (r > sizeof(buf))
3037                 r = sizeof(buf);
3038         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3039 }
3040
3041 static ssize_t
3042 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3043                       size_t cnt, loff_t *ppos)
3044 {
3045         long *ptr = filp->private_data;
3046         char buf[64];
3047         long val;
3048         int ret;
3049
3050         if (cnt >= sizeof(buf))
3051                 return -EINVAL;
3052
3053         if (copy_from_user(&buf, ubuf, cnt))
3054                 return -EFAULT;
3055
3056         buf[cnt] = 0;
3057
3058         ret = strict_strtoul(buf, 10, &val);
3059         if (ret < 0)
3060                 return ret;
3061
3062         *ptr = val * 1000;
3063
3064         return cnt;
3065 }
3066
3067 static atomic_t tracing_reader;
3068
3069 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3070 {
3071         struct trace_iterator *iter;
3072
3073         if (tracing_disabled)
3074                 return -ENODEV;
3075
3076         /* We only allow for reader of the pipe */
3077         if (atomic_inc_return(&tracing_reader) != 1) {
3078                 atomic_dec(&tracing_reader);
3079                 return -EBUSY;
3080         }
3081
3082         /* create a buffer to store the information to pass to userspace */
3083         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3084         if (!iter)
3085                 return -ENOMEM;
3086
3087         mutex_lock(&trace_types_lock);
3088
3089         /* trace pipe does not show start of buffer */
3090         cpus_setall(iter->started);
3091
3092         iter->tr = &global_trace;
3093         iter->trace = current_trace;
3094         filp->private_data = iter;
3095
3096         if (iter->trace->pipe_open)
3097                 iter->trace->pipe_open(iter);
3098         mutex_unlock(&trace_types_lock);
3099
3100         return 0;
3101 }
3102
3103 static int tracing_release_pipe(struct inode *inode, struct file *file)
3104 {
3105         struct trace_iterator *iter = file->private_data;
3106
3107         kfree(iter);
3108         atomic_dec(&tracing_reader);
3109
3110         return 0;
3111 }
3112
3113 static unsigned int
3114 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3115 {
3116         struct trace_iterator *iter = filp->private_data;
3117
3118         if (trace_flags & TRACE_ITER_BLOCK) {
3119                 /*
3120                  * Always select as readable when in blocking mode
3121                  */
3122                 return POLLIN | POLLRDNORM;
3123         } else {
3124                 if (!trace_empty(iter))
3125                         return POLLIN | POLLRDNORM;
3126                 poll_wait(filp, &trace_wait, poll_table);
3127                 if (!trace_empty(iter))
3128                         return POLLIN | POLLRDNORM;
3129
3130                 return 0;
3131         }
3132 }
3133
3134 /*
3135  * Consumer reader.
3136  */
3137 static ssize_t
3138 tracing_read_pipe(struct file *filp, char __user *ubuf,
3139                   size_t cnt, loff_t *ppos)
3140 {
3141         struct trace_iterator *iter = filp->private_data;
3142         ssize_t sret;
3143
3144         /* return any leftover data */
3145         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3146         if (sret != -EBUSY)
3147                 return sret;
3148
3149         trace_seq_reset(&iter->seq);
3150
3151         mutex_lock(&trace_types_lock);
3152         if (iter->trace->read) {
3153                 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3154                 if (sret)
3155                         goto out;
3156         }
3157
3158 waitagain:
3159         sret = 0;
3160         while (trace_empty(iter)) {
3161
3162                 if ((filp->f_flags & O_NONBLOCK)) {
3163                         sret = -EAGAIN;
3164                         goto out;
3165                 }
3166
3167                 /*
3168                  * This is a make-shift waitqueue. The reason we don't use
3169                  * an actual wait queue is because:
3170                  *  1) we only ever have one waiter
3171                  *  2) the tracing, traces all functions, we don't want
3172                  *     the overhead of calling wake_up and friends
3173                  *     (and tracing them too)
3174                  *     Anyway, this is really very primitive wakeup.
3175                  */
3176                 set_current_state(TASK_INTERRUPTIBLE);
3177                 iter->tr->waiter = current;
3178
3179                 mutex_unlock(&trace_types_lock);
3180
3181                 /* sleep for 100 msecs, and try again. */
3182                 schedule_timeout(HZ/10);
3183
3184                 mutex_lock(&trace_types_lock);
3185
3186                 iter->tr->waiter = NULL;
3187
3188                 if (signal_pending(current)) {
3189                         sret = -EINTR;
3190                         goto out;
3191                 }
3192
3193                 if (iter->trace != current_trace)
3194                         goto out;
3195
3196                 /*
3197                  * We block until we read something and tracing is disabled.
3198                  * We still block if tracing is disabled, but we have never
3199                  * read anything. This allows a user to cat this file, and
3200                  * then enable tracing. But after we have read something,
3201                  * we give an EOF when tracing is again disabled.
3202                  *
3203                  * iter->pos will be 0 if we haven't read anything.
3204                  */
3205                 if (!tracer_enabled && iter->pos)
3206                         break;
3207
3208                 continue;
3209         }
3210
3211         /* stop when tracing is finished */
3212         if (trace_empty(iter))
3213                 goto out;
3214
3215         if (cnt >= PAGE_SIZE)
3216                 cnt = PAGE_SIZE - 1;
3217
3218         /* reset all but tr, trace, and overruns */
3219         memset(&iter->seq, 0,
3220                sizeof(struct trace_iterator) -
3221                offsetof(struct trace_iterator, seq));
3222         iter->pos = -1;
3223
3224         while (find_next_entry_inc(iter) != NULL) {
3225                 enum print_line_t ret;
3226                 int len = iter->seq.len;
3227
3228                 ret = print_trace_line(iter);
3229                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3230                         /* don't print partial lines */
3231                         iter->seq.len = len;
3232                         break;
3233                 }
3234
3235                 trace_consume(iter);
3236
3237                 if (iter->seq.len >= cnt)
3238                         break;
3239         }
3240
3241         /* Now copy what we have to the user */
3242         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3243         if (iter->seq.readpos >= iter->seq.len)
3244                 trace_seq_reset(&iter->seq);
3245
3246         /*
3247          * If there was nothing to send to user, inspite of consuming trace
3248          * entries, go back to wait for more entries.
3249          */
3250         if (sret == -EBUSY)
3251                 goto waitagain;
3252
3253 out:
3254         mutex_unlock(&trace_types_lock);
3255
3256         return sret;
3257 }
3258
3259 static ssize_t
3260 tracing_entries_read(struct file *filp, char __user *ubuf,
3261                      size_t cnt, loff_t *ppos)
3262 {
3263         struct trace_array *tr = filp->private_data;
3264         char buf[64];
3265         int r;
3266
3267         r = sprintf(buf, "%lu\n", tr->entries >> 10);
3268         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3269 }
3270
3271 static ssize_t
3272 tracing_entries_write(struct file *filp, const char __user *ubuf,
3273                       size_t cnt, loff_t *ppos)
3274 {
3275         unsigned long val;
3276         char buf[64];
3277         int ret, cpu;
3278
3279         if (cnt >= sizeof(buf))
3280                 return -EINVAL;
3281
3282         if (copy_from_user(&buf, ubuf, cnt))
3283                 return -EFAULT;
3284
3285         buf[cnt] = 0;
3286
3287         ret = strict_strtoul(buf, 10, &val);
3288         if (ret < 0)
3289                 return ret;
3290
3291         /* must have at least 1 entry */
3292         if (!val)
3293                 return -EINVAL;
3294
3295         mutex_lock(&trace_types_lock);
3296
3297         tracing_stop();
3298
3299         /* disable all cpu buffers */
3300         for_each_tracing_cpu(cpu) {
3301                 if (global_trace.data[cpu])
3302                         atomic_inc(&global_trace.data[cpu]->disabled);
3303                 if (max_tr.data[cpu])
3304                         atomic_inc(&max_tr.data[cpu]->disabled);
3305         }
3306
3307         /* value is in KB */
3308         val <<= 10;
3309
3310         if (val != global_trace.entries) {
3311                 ret = ring_buffer_resize(global_trace.buffer, val);
3312                 if (ret < 0) {
3313                         cnt = ret;
3314                         goto out;
3315                 }
3316
3317                 ret = ring_buffer_resize(max_tr.buffer, val);
3318                 if (ret < 0) {
3319                         int r;
3320                         cnt = ret;
3321                         r = ring_buffer_resize(global_trace.buffer,
3322                                                global_trace.entries);
3323                         if (r < 0) {
3324                                 /* AARGH! We are left with different
3325                                  * size max buffer!!!! */
3326                                 WARN_ON(1);
3327                                 tracing_disabled = 1;
3328                         }
3329                         goto out;
3330                 }
3331
3332                 global_trace.entries = val;
3333         }
3334
3335         filp->f_pos += cnt;
3336
3337         /* If check pages failed, return ENOMEM */
3338         if (tracing_disabled)
3339                 cnt = -ENOMEM;
3340  out:
3341         for_each_tracing_cpu(cpu) {
3342                 if (global_trace.data[cpu])
3343                         atomic_dec(&global_trace.data[cpu]->disabled);
3344                 if (max_tr.data[cpu])
3345                         atomic_dec(&max_tr.data[cpu]->disabled);
3346         }
3347
3348         tracing_start();
3349         max_tr.entries = global_trace.entries;
3350         mutex_unlock(&trace_types_lock);
3351
3352         return cnt;
3353 }
3354
3355 static int mark_printk(const char *fmt, ...)
3356 {
3357         int ret;
3358         va_list args;
3359         va_start(args, fmt);
3360         ret = trace_vprintk(0, -1, fmt, args);
3361         va_end(args);
3362         return ret;
3363 }
3364
3365 static ssize_t
3366 tracing_mark_write(struct file *filp, const char __user *ubuf,
3367                                         size_t cnt, loff_t *fpos)
3368 {
3369         char *buf;
3370         char *end;
3371
3372         if (tracing_disabled)
3373                 return -EINVAL;
3374
3375         if (cnt > TRACE_BUF_SIZE)
3376                 cnt = TRACE_BUF_SIZE;
3377
3378         buf = kmalloc(cnt + 1, GFP_KERNEL);
3379         if (buf == NULL)
3380                 return -ENOMEM;
3381
3382         if (copy_from_user(buf, ubuf, cnt)) {
3383                 kfree(buf);
3384                 return -EFAULT;
3385         }
3386
3387         /* Cut from the first nil or newline. */
3388         buf[cnt] = '\0';
3389         end = strchr(buf, '\n');
3390         if (end)
3391                 *end = '\0';
3392
3393         cnt = mark_printk("%s\n", buf);
3394         kfree(buf);
3395         *fpos += cnt;
3396
3397         return cnt;
3398 }
3399
3400 static struct file_operations tracing_max_lat_fops = {
3401         .open           = tracing_open_generic,
3402         .read           = tracing_max_lat_read,
3403         .write          = tracing_max_lat_write,
3404 };
3405
3406 static struct file_operations tracing_ctrl_fops = {
3407         .open           = tracing_open_generic,
3408         .read           = tracing_ctrl_read,
3409         .write          = tracing_ctrl_write,
3410 };
3411
3412 static struct file_operations set_tracer_fops = {
3413         .open           = tracing_open_generic,
3414         .read           = tracing_set_trace_read,
3415         .write          = tracing_set_trace_write,
3416 };
3417
3418 static struct file_operations tracing_pipe_fops = {
3419         .open           = tracing_open_pipe,
3420         .poll           = tracing_poll_pipe,
3421         .read           = tracing_read_pipe,
3422         .release        = tracing_release_pipe,
3423 };
3424
3425 static struct file_operations tracing_entries_fops = {
3426         .open           = tracing_open_generic,
3427         .read           = tracing_entries_read,
3428         .write          = tracing_entries_write,
3429 };
3430
3431 static struct file_operations tracing_mark_fops = {
3432         .open           = tracing_open_generic,
3433         .write          = tracing_mark_write,
3434 };
3435
3436 #ifdef CONFIG_DYNAMIC_FTRACE
3437
3438 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3439 {
3440         return 0;
3441 }
3442
3443 static ssize_t
3444 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3445                   size_t cnt, loff_t *ppos)
3446 {
3447         static char ftrace_dyn_info_buffer[1024];
3448         static DEFINE_MUTEX(dyn_info_mutex);
3449         unsigned long *p = filp->private_data;
3450         char *buf = ftrace_dyn_info_buffer;
3451         int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3452         int r;
3453
3454         mutex_lock(&dyn_info_mutex);
3455         r = sprintf(buf, "%ld ", *p);
3456
3457         r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3458         buf[r++] = '\n';
3459
3460         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3461
3462         mutex_unlock(&dyn_info_mutex);
3463
3464         return r;
3465 }
3466
3467 static struct file_operations tracing_dyn_info_fops = {
3468         .open           = tracing_open_generic,
3469         .read           = tracing_read_dyn_info,
3470 };
3471 #endif
3472
3473 static struct dentry *d_tracer;
3474
3475 struct dentry *tracing_init_dentry(void)
3476 {
3477         static int once;
3478
3479         if (d_tracer)
3480                 return d_tracer;
3481
3482         d_tracer = debugfs_create_dir("tracing", NULL);
3483
3484         if (!d_tracer && !once) {
3485                 once = 1;
3486                 pr_warning("Could not create debugfs directory 'tracing'\n");
3487                 return NULL;
3488         }
3489
3490         return d_tracer;
3491 }
3492
3493 #ifdef CONFIG_FTRACE_SELFTEST
3494 /* Let selftest have access to static functions in this file */
3495 #include "trace_selftest.c"
3496 #endif
3497
3498 static __init int tracer_init_debugfs(void)
3499 {
3500         struct dentry *d_tracer;
3501         struct dentry *entry;
3502
3503         d_tracer = tracing_init_dentry();
3504
3505         entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3506                                     &global_trace, &tracing_ctrl_fops);
3507         if (!entry)
3508                 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3509
3510         entry = debugfs_create_file("trace_options", 0644, d_tracer,
3511                                     NULL, &tracing_iter_fops);
3512         if (!entry)
3513                 pr_warning("Could not create debugfs 'trace_options' entry\n");
3514
3515         entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3516                                     NULL, &tracing_cpumask_fops);
3517         if (!entry)
3518                 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3519
3520         entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3521                                     &global_trace, &tracing_lt_fops);
3522         if (!entry)
3523                 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3524
3525         entry = debugfs_create_file("trace", 0444, d_tracer,
3526                                     &global_trace, &tracing_fops);
3527         if (!entry)
3528                 pr_warning("Could not create debugfs 'trace' entry\n");
3529
3530         entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3531                                     &global_trace, &show_traces_fops);
3532         if (!entry)
3533                 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3534
3535         entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3536                                     &global_trace, &set_tracer_fops);
3537         if (!entry)
3538                 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3539
3540         entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3541                                     &tracing_max_latency,
3542                                     &tracing_max_lat_fops);
3543         if (!entry)
3544                 pr_warning("Could not create debugfs "
3545                            "'tracing_max_latency' entry\n");
3546
3547         entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3548                                     &tracing_thresh, &tracing_max_lat_fops);
3549         if (!entry)
3550                 pr_warning("Could not create debugfs "
3551                            "'tracing_thresh' entry\n");
3552         entry = debugfs_create_file("README", 0644, d_tracer,
3553                                     NULL, &tracing_readme_fops);
3554         if (!entry)
3555                 pr_warning("Could not create debugfs 'README' entry\n");
3556
3557         entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3558                                     NULL, &tracing_pipe_fops);
3559         if (!entry)
3560                 pr_warning("Could not create debugfs "
3561                            "'trace_pipe' entry\n");
3562
3563         entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3564                                     &global_trace, &tracing_entries_fops);
3565         if (!entry)
3566                 pr_warning("Could not create debugfs "
3567                            "'buffer_size_kb' entry\n");
3568
3569         entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3570                                     NULL, &tracing_mark_fops);
3571         if (!entry)
3572                 pr_warning("Could not create debugfs "
3573                            "'trace_marker' entry\n");
3574
3575 #ifdef CONFIG_DYNAMIC_FTRACE
3576         entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3577                                     &ftrace_update_tot_cnt,
3578                                     &tracing_dyn_info_fops);
3579         if (!entry)
3580                 pr_warning("Could not create debugfs "
3581                            "'dyn_ftrace_total_info' entry\n");
3582 #endif
3583 #ifdef CONFIG_SYSPROF_TRACER
3584         init_tracer_sysprof_debugfs(d_tracer);
3585 #endif
3586         return 0;
3587 }
3588
3589 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
3590 {
3591         /*
3592          * Raw Spinlock because a normal spinlock would be traced here
3593          * and append an irrelevant couple spin_lock_irqsave/
3594          * spin_unlock_irqrestore traced by ftrace around this
3595          * TRACE_PRINTK trace.
3596          */
3597         static raw_spinlock_t trace_buf_lock =
3598                                 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3599         static char trace_buf[TRACE_BUF_SIZE];
3600
3601         struct ring_buffer_event *event;
3602         struct trace_array *tr = &global_trace;
3603         struct trace_array_cpu *data;
3604         struct print_entry *entry;
3605         unsigned long flags, irq_flags;
3606         int cpu, len = 0, size, pc;
3607
3608         if (tracing_disabled || atomic_read(&tracing_selftest_running))
3609                 return 0;
3610
3611         pc = preempt_count();
3612         preempt_disable_notrace();
3613         cpu = raw_smp_processor_id();
3614         data = tr->data[cpu];
3615
3616         if (unlikely(atomic_read(&data->disabled)))
3617                 goto out;
3618
3619         local_irq_save(flags);
3620         __raw_spin_lock(&trace_buf_lock);
3621         len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3622
3623         len = min(len, TRACE_BUF_SIZE-1);
3624         trace_buf[len] = 0;
3625
3626         size = sizeof(*entry) + len + 1;
3627         event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3628         if (!event)
3629                 goto out_unlock;
3630         entry = ring_buffer_event_data(event);
3631         tracing_generic_entry_update(&entry->ent, flags, pc);
3632         entry->ent.type                 = TRACE_PRINT;
3633         entry->ip                       = ip;
3634         entry->depth                    = depth;
3635
3636         memcpy(&entry->buf, trace_buf, len);
3637         entry->buf[len] = 0;
3638         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3639
3640  out_unlock:
3641         __raw_spin_unlock(&trace_buf_lock);
3642         local_irq_restore(flags);
3643
3644  out:
3645         preempt_enable_notrace();
3646
3647         return len;
3648 }
3649 EXPORT_SYMBOL_GPL(trace_vprintk);
3650
3651 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3652 {
3653         int ret;
3654         va_list ap;
3655
3656         if (!(trace_flags & TRACE_ITER_PRINTK))
3657                 return 0;
3658
3659         va_start(ap, fmt);
3660
3661 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3662         ret = trace_vprintk(ip, current->curr_ret_stack, fmt, ap);
3663 #else
3664         ret = trace_vprintk(ip, -1, fmt, ap);
3665 #endif
3666
3667         va_end(ap);
3668         return ret;
3669 }
3670 EXPORT_SYMBOL_GPL(__ftrace_printk);
3671
3672 static int trace_panic_handler(struct notifier_block *this,
3673                                unsigned long event, void *unused)
3674 {
3675         if (ftrace_dump_on_oops)
3676                 ftrace_dump();
3677         return NOTIFY_OK;
3678 }
3679
3680 static struct notifier_block trace_panic_notifier = {
3681         .notifier_call  = trace_panic_handler,
3682         .next           = NULL,
3683         .priority       = 150   /* priority: INT_MAX >= x >= 0 */
3684 };
3685
3686 static int trace_die_handler(struct notifier_block *self,
3687                              unsigned long val,
3688                              void *data)
3689 {
3690         switch (val) {
3691         case DIE_OOPS:
3692                 if (ftrace_dump_on_oops)
3693                         ftrace_dump();
3694                 break;
3695         default:
3696                 break;
3697         }
3698         return NOTIFY_OK;
3699 }
3700
3701 static struct notifier_block trace_die_notifier = {
3702         .notifier_call = trace_die_handler,
3703         .priority = 200
3704 };
3705
3706 /*
3707  * printk is set to max of 1024, we really don't need it that big.
3708  * Nothing should be printing 1000 characters anyway.
3709  */
3710 #define TRACE_MAX_PRINT         1000
3711
3712 /*
3713  * Define here KERN_TRACE so that we have one place to modify
3714  * it if we decide to change what log level the ftrace dump
3715  * should be at.
3716  */
3717 #define KERN_TRACE              KERN_INFO
3718
3719 static void
3720 trace_printk_seq(struct trace_seq *s)
3721 {
3722         /* Probably should print a warning here. */
3723         if (s->len >= 1000)
3724                 s->len = 1000;
3725
3726         /* should be zero ended, but we are paranoid. */
3727         s->buffer[s->len] = 0;
3728
3729         printk(KERN_TRACE "%s", s->buffer);
3730
3731         trace_seq_reset(s);
3732 }
3733
3734 void ftrace_dump(void)
3735 {
3736         static DEFINE_SPINLOCK(ftrace_dump_lock);
3737         /* use static because iter can be a bit big for the stack */
3738         static struct trace_iterator iter;
3739         static cpumask_t mask;
3740         static int dump_ran;
3741         unsigned long flags;
3742         int cnt = 0, cpu;
3743
3744         /* only one dump */
3745         spin_lock_irqsave(&ftrace_dump_lock, flags);
3746         if (dump_ran)
3747                 goto out;
3748
3749         dump_ran = 1;
3750
3751         /* No turning back! */
3752         ftrace_kill();
3753
3754         for_each_tracing_cpu(cpu) {
3755                 atomic_inc(&global_trace.data[cpu]->disabled);
3756         }
3757
3758         /* don't look at user memory in panic mode */
3759         trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3760
3761         printk(KERN_TRACE "Dumping ftrace buffer:\n");
3762
3763         iter.tr = &global_trace;
3764         iter.trace = current_trace;
3765
3766         /*
3767          * We need to stop all tracing on all CPUS to read the
3768          * the next buffer. This is a bit expensive, but is
3769          * not done often. We fill all what we can read,
3770          * and then release the locks again.
3771          */
3772
3773         cpus_clear(mask);
3774
3775         while (!trace_empty(&iter)) {
3776
3777                 if (!cnt)
3778                         printk(KERN_TRACE "---------------------------------\n");
3779
3780                 cnt++;
3781
3782                 /* reset all but tr, trace, and overruns */
3783                 memset(&iter.seq, 0,
3784                        sizeof(struct trace_iterator) -
3785                        offsetof(struct trace_iterator, seq));
3786                 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3787                 iter.pos = -1;
3788
3789                 if (find_next_entry_inc(&iter) != NULL) {
3790                         print_trace_line(&iter);
3791                         trace_consume(&iter);
3792                 }
3793
3794                 trace_printk_seq(&iter.seq);
3795         }
3796
3797         if (!cnt)
3798                 printk(KERN_TRACE "   (ftrace buffer empty)\n");
3799         else
3800                 printk(KERN_TRACE "---------------------------------\n");
3801
3802  out:
3803         spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3804 }
3805
3806 __init static int tracer_alloc_buffers(void)
3807 {
3808         struct trace_array_cpu *data;
3809         int i;
3810
3811         /* TODO: make the number of buffers hot pluggable with CPUS */
3812         tracing_buffer_mask = cpu_possible_map;
3813
3814         global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3815                                                    TRACE_BUFFER_FLAGS);
3816         if (!global_trace.buffer) {
3817                 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3818                 WARN_ON(1);
3819                 return 0;
3820         }
3821         global_trace.entries = ring_buffer_size(global_trace.buffer);
3822
3823 #ifdef CONFIG_TRACER_MAX_TRACE
3824         max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3825                                              TRACE_BUFFER_FLAGS);
3826         if (!max_tr.buffer) {
3827                 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3828                 WARN_ON(1);
3829                 ring_buffer_free(global_trace.buffer);
3830                 return 0;
3831         }
3832         max_tr.entries = ring_buffer_size(max_tr.buffer);
3833         WARN_ON(max_tr.entries != global_trace.entries);
3834 #endif
3835
3836         /* Allocate the first page for all buffers */
3837         for_each_tracing_cpu(i) {
3838                 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3839                 max_tr.data[i] = &per_cpu(max_data, i);
3840         }
3841
3842         trace_init_cmdlines();
3843
3844         register_tracer(&nop_trace);
3845 #ifdef CONFIG_BOOT_TRACER
3846         register_tracer(&boot_tracer);
3847         current_trace = &boot_tracer;
3848         current_trace->init(&global_trace);
3849 #else
3850         current_trace = &nop_trace;
3851 #endif
3852
3853         /* All seems OK, enable tracing */
3854         tracing_disabled = 0;
3855
3856         atomic_notifier_chain_register(&panic_notifier_list,
3857                                        &trace_panic_notifier);
3858
3859         register_die_notifier(&trace_die_notifier);
3860
3861         return 0;
3862 }
3863 early_initcall(tracer_alloc_buffers);
3864 fs_initcall(tracer_init_debugfs);