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