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