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