]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - kernel/trace/trace.c
tracing: do not return EFAULT if read copied anything
[linux-2.6-omap-h63xx.git] / 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/ring_buffer.h>
15 #include <linux/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/ctype.h>
34 #include <linux/init.h>
35 #include <linux/poll.h>
36 #include <linux/gfp.h>
37 #include <linux/fs.h>
38
39 #include "trace.h"
40 #include "trace_output.h"
41
42 #define TRACE_BUFFER_FLAGS      (RB_FL_OVERWRITE)
43
44 unsigned long __read_mostly     tracing_max_latency;
45 unsigned long __read_mostly     tracing_thresh;
46
47 /*
48  * We need to change this state when a selftest is running.
49  * A selftest will lurk into the ring-buffer to count the
50  * entries inserted during the selftest although some concurrent
51  * insertions into the ring-buffer such as ftrace_printk could occurred
52  * at the same time, giving false positive or negative results.
53  */
54 static bool __read_mostly tracing_selftest_running;
55
56 /*
57  * If a tracer is running, we do not want to run SELFTEST.
58  */
59 static bool __read_mostly tracing_selftest_disabled;
60
61 /* For tracers that don't implement custom flags */
62 static struct tracer_opt dummy_tracer_opt[] = {
63         { }
64 };
65
66 static struct tracer_flags dummy_tracer_flags = {
67         .val = 0,
68         .opts = dummy_tracer_opt
69 };
70
71 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
72 {
73         return 0;
74 }
75
76 /*
77  * Kill all tracing for good (never come back).
78  * It is initialized to 1 but will turn to zero if the initialization
79  * of the tracer is successful. But that is the only place that sets
80  * this back to zero.
81  */
82 static int tracing_disabled = 1;
83
84 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
85
86 static inline void ftrace_disable_cpu(void)
87 {
88         preempt_disable();
89         local_inc(&__get_cpu_var(ftrace_cpu_disabled));
90 }
91
92 static inline void ftrace_enable_cpu(void)
93 {
94         local_dec(&__get_cpu_var(ftrace_cpu_disabled));
95         preempt_enable();
96 }
97
98 static cpumask_var_t __read_mostly      tracing_buffer_mask;
99
100 /* Define which cpu buffers are currently read in trace_pipe */
101 static cpumask_var_t                    tracing_reader_cpumask;
102
103 #define for_each_tracing_cpu(cpu)       \
104         for_each_cpu(cpu, tracing_buffer_mask)
105
106 /*
107  * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
108  *
109  * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
110  * is set, then ftrace_dump is called. This will output the contents
111  * of the ftrace buffers to the console.  This is very useful for
112  * capturing traces that lead to crashes and outputing it to a
113  * serial console.
114  *
115  * It is default off, but you can enable it with either specifying
116  * "ftrace_dump_on_oops" in the kernel command line, or setting
117  * /proc/sys/kernel/ftrace_dump_on_oops to true.
118  */
119 int ftrace_dump_on_oops;
120
121 static int tracing_set_tracer(const char *buf);
122
123 #define BOOTUP_TRACER_SIZE              100
124 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
125 static char *default_bootup_tracer;
126
127 static int __init set_ftrace(char *str)
128 {
129         strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
130         default_bootup_tracer = bootup_tracer_buf;
131         return 1;
132 }
133 __setup("ftrace=", set_ftrace);
134
135 static int __init set_ftrace_dump_on_oops(char *str)
136 {
137         ftrace_dump_on_oops = 1;
138         return 1;
139 }
140 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
141
142 long
143 ns2usecs(cycle_t nsec)
144 {
145         nsec += 500;
146         do_div(nsec, 1000);
147         return nsec;
148 }
149
150 cycle_t ftrace_now(int cpu)
151 {
152         u64 ts = ring_buffer_time_stamp(cpu);
153         ring_buffer_normalize_time_stamp(cpu, &ts);
154         return ts;
155 }
156
157 /*
158  * The global_trace is the descriptor that holds the tracing
159  * buffers for the live tracing. For each CPU, it contains
160  * a link list of pages that will store trace entries. The
161  * page descriptor of the pages in the memory is used to hold
162  * the link list by linking the lru item in the page descriptor
163  * to each of the pages in the buffer per CPU.
164  *
165  * For each active CPU there is a data field that holds the
166  * pages for the buffer for that CPU. Each CPU has the same number
167  * of pages allocated for its buffer.
168  */
169 static struct trace_array       global_trace;
170
171 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
172
173 /*
174  * The max_tr is used to snapshot the global_trace when a maximum
175  * latency is reached. Some tracers will use this to store a maximum
176  * trace while it continues examining live traces.
177  *
178  * The buffers for the max_tr are set up the same as the global_trace.
179  * When a snapshot is taken, the link list of the max_tr is swapped
180  * with the link list of the global_trace and the buffers are reset for
181  * the global_trace so the tracing can continue.
182  */
183 static struct trace_array       max_tr;
184
185 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
186
187 /* tracer_enabled is used to toggle activation of a tracer */
188 static int                      tracer_enabled = 1;
189
190 /**
191  * tracing_is_enabled - return tracer_enabled status
192  *
193  * This function is used by other tracers to know the status
194  * of the tracer_enabled flag.  Tracers may use this function
195  * to know if it should enable their features when starting
196  * up. See irqsoff tracer for an example (start_irqsoff_tracer).
197  */
198 int tracing_is_enabled(void)
199 {
200         return tracer_enabled;
201 }
202
203 /*
204  * trace_buf_size is the size in bytes that is allocated
205  * for a buffer. Note, the number of bytes is always rounded
206  * to page size.
207  *
208  * This number is purposely set to a low number of 16384.
209  * If the dump on oops happens, it will be much appreciated
210  * to not have to wait for all that output. Anyway this can be
211  * boot time and run time configurable.
212  */
213 #define TRACE_BUF_SIZE_DEFAULT  1441792UL /* 16384 * 88 (sizeof(entry)) */
214
215 static unsigned long            trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
216
217 /* trace_types holds a link list of available tracers. */
218 static struct tracer            *trace_types __read_mostly;
219
220 /* current_trace points to the tracer that is currently active */
221 static struct tracer            *current_trace __read_mostly;
222
223 /*
224  * max_tracer_type_len is used to simplify the allocating of
225  * buffers to read userspace tracer names. We keep track of
226  * the longest tracer name registered.
227  */
228 static int                      max_tracer_type_len;
229
230 /*
231  * trace_types_lock is used to protect the trace_types list.
232  * This lock is also used to keep user access serialized.
233  * Accesses from userspace will grab this lock while userspace
234  * activities happen inside the kernel.
235  */
236 static DEFINE_MUTEX(trace_types_lock);
237
238 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
239 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
240
241 /* trace_flags holds trace_options default values */
242 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
243         TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
244
245 /**
246  * trace_wake_up - wake up tasks waiting for trace input
247  *
248  * Simply wakes up any task that is blocked on the trace_wait
249  * queue. These is used with trace_poll for tasks polling the trace.
250  */
251 void trace_wake_up(void)
252 {
253         /*
254          * The runqueue_is_locked() can fail, but this is the best we
255          * have for now:
256          */
257         if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
258                 wake_up(&trace_wait);
259 }
260
261 static int __init set_buf_size(char *str)
262 {
263         unsigned long buf_size;
264         int ret;
265
266         if (!str)
267                 return 0;
268         ret = strict_strtoul(str, 0, &buf_size);
269         /* nr_entries can not be zero */
270         if (ret < 0 || buf_size == 0)
271                 return 0;
272         trace_buf_size = buf_size;
273         return 1;
274 }
275 __setup("trace_buf_size=", set_buf_size);
276
277 unsigned long nsecs_to_usecs(unsigned long nsecs)
278 {
279         return nsecs / 1000;
280 }
281
282 /* These must match the bit postions in trace_iterator_flags */
283 static const char *trace_options[] = {
284         "print-parent",
285         "sym-offset",
286         "sym-addr",
287         "verbose",
288         "raw",
289         "hex",
290         "bin",
291         "block",
292         "stacktrace",
293         "sched-tree",
294         "ftrace_printk",
295         "ftrace_preempt",
296         "branch",
297         "annotate",
298         "userstacktrace",
299         "sym-userobj",
300         "printk-msg-only",
301         "context-info",
302         NULL
303 };
304
305 /*
306  * ftrace_max_lock is used to protect the swapping of buffers
307  * when taking a max snapshot. The buffers themselves are
308  * protected by per_cpu spinlocks. But the action of the swap
309  * needs its own lock.
310  *
311  * This is defined as a raw_spinlock_t in order to help
312  * with performance when lockdep debugging is enabled.
313  */
314 static raw_spinlock_t ftrace_max_lock =
315         (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
316
317 /*
318  * Copy the new maximum trace into the separate maximum-trace
319  * structure. (this way the maximum trace is permanently saved,
320  * for later retrieval via /debugfs/tracing/latency_trace)
321  */
322 static void
323 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
324 {
325         struct trace_array_cpu *data = tr->data[cpu];
326
327         max_tr.cpu = cpu;
328         max_tr.time_start = data->preempt_timestamp;
329
330         data = max_tr.data[cpu];
331         data->saved_latency = tracing_max_latency;
332
333         memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
334         data->pid = tsk->pid;
335         data->uid = task_uid(tsk);
336         data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
337         data->policy = tsk->policy;
338         data->rt_priority = tsk->rt_priority;
339
340         /* record this tasks comm */
341         tracing_record_cmdline(tsk);
342 }
343
344 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
345 {
346         int len;
347         int ret;
348
349         if (!cnt)
350                 return 0;
351
352         if (s->len <= s->readpos)
353                 return -EBUSY;
354
355         len = s->len - s->readpos;
356         if (cnt > len)
357                 cnt = len;
358         ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
359         if (ret == cnt)
360                 return -EFAULT;
361
362         cnt -= ret;
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_init(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 raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
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 (!__raw_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         __raw_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 struct ring_buffer_event *
847 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
848                                   unsigned long flags, int pc)
849 {
850         return trace_buffer_lock_reserve(&global_trace,
851                                          type, len, flags, pc);
852 }
853
854 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
855                                         unsigned long flags, int pc)
856 {
857         return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
858 }
859
860 void
861 trace_function(struct trace_array *tr,
862                unsigned long ip, unsigned long parent_ip, unsigned long flags,
863                int pc)
864 {
865         struct ring_buffer_event *event;
866         struct ftrace_entry *entry;
867
868         /* If we are reading the ring buffer, don't trace */
869         if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
870                 return;
871
872         event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
873                                           flags, pc);
874         if (!event)
875                 return;
876         entry   = ring_buffer_event_data(event);
877         entry->ip                       = ip;
878         entry->parent_ip                = parent_ip;
879         ring_buffer_unlock_commit(tr->buffer, event);
880 }
881
882 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
883 static void __trace_graph_entry(struct trace_array *tr,
884                                 struct ftrace_graph_ent *trace,
885                                 unsigned long flags,
886                                 int pc)
887 {
888         struct ring_buffer_event *event;
889         struct ftrace_graph_ent_entry *entry;
890
891         if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
892                 return;
893
894         event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
895                                           sizeof(*entry), flags, pc);
896         if (!event)
897                 return;
898         entry   = ring_buffer_event_data(event);
899         entry->graph_ent                        = *trace;
900         ring_buffer_unlock_commit(global_trace.buffer, event);
901 }
902
903 static void __trace_graph_return(struct trace_array *tr,
904                                 struct ftrace_graph_ret *trace,
905                                 unsigned long flags,
906                                 int pc)
907 {
908         struct ring_buffer_event *event;
909         struct ftrace_graph_ret_entry *entry;
910
911         if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
912                 return;
913
914         event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
915                                           sizeof(*entry), flags, pc);
916         if (!event)
917                 return;
918         entry   = ring_buffer_event_data(event);
919         entry->ret                              = *trace;
920         ring_buffer_unlock_commit(global_trace.buffer, event);
921 }
922 #endif
923
924 void
925 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
926        unsigned long ip, unsigned long parent_ip, unsigned long flags,
927        int pc)
928 {
929         if (likely(!atomic_read(&data->disabled)))
930                 trace_function(tr, ip, parent_ip, flags, pc);
931 }
932
933 static void __ftrace_trace_stack(struct trace_array *tr,
934                                  unsigned long flags,
935                                  int skip, int pc)
936 {
937 #ifdef CONFIG_STACKTRACE
938         struct ring_buffer_event *event;
939         struct stack_entry *entry;
940         struct stack_trace trace;
941
942         event = trace_buffer_lock_reserve(tr, TRACE_STACK,
943                                           sizeof(*entry), flags, pc);
944         if (!event)
945                 return;
946         entry   = ring_buffer_event_data(event);
947         memset(&entry->caller, 0, sizeof(entry->caller));
948
949         trace.nr_entries        = 0;
950         trace.max_entries       = FTRACE_STACK_ENTRIES;
951         trace.skip              = skip;
952         trace.entries           = entry->caller;
953
954         save_stack_trace(&trace);
955         ring_buffer_unlock_commit(tr->buffer, event);
956 #endif
957 }
958
959 static void ftrace_trace_stack(struct trace_array *tr,
960                                unsigned long flags,
961                                int skip, int pc)
962 {
963         if (!(trace_flags & TRACE_ITER_STACKTRACE))
964                 return;
965
966         __ftrace_trace_stack(tr, flags, skip, pc);
967 }
968
969 void __trace_stack(struct trace_array *tr,
970                    unsigned long flags,
971                    int skip, int pc)
972 {
973         __ftrace_trace_stack(tr, flags, skip, pc);
974 }
975
976 static void ftrace_trace_userstack(struct trace_array *tr,
977                                    unsigned long flags, int pc)
978 {
979 #ifdef CONFIG_STACKTRACE
980         struct ring_buffer_event *event;
981         struct userstack_entry *entry;
982         struct stack_trace trace;
983
984         if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
985                 return;
986
987         event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
988                                           sizeof(*entry), flags, pc);
989         if (!event)
990                 return;
991         entry   = ring_buffer_event_data(event);
992
993         memset(&entry->caller, 0, sizeof(entry->caller));
994
995         trace.nr_entries        = 0;
996         trace.max_entries       = FTRACE_STACK_ENTRIES;
997         trace.skip              = 0;
998         trace.entries           = entry->caller;
999
1000         save_stack_trace_user(&trace);
1001         ring_buffer_unlock_commit(tr->buffer, event);
1002 #endif
1003 }
1004
1005 #ifdef UNUSED
1006 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1007 {
1008         ftrace_trace_userstack(tr, flags, preempt_count());
1009 }
1010 #endif /* UNUSED */
1011
1012 static void
1013 ftrace_trace_special(void *__tr,
1014                      unsigned long arg1, unsigned long arg2, unsigned long arg3,
1015                      int pc)
1016 {
1017         struct ring_buffer_event *event;
1018         struct trace_array *tr = __tr;
1019         struct special_entry *entry;
1020
1021         event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1022                                           sizeof(*entry), 0, pc);
1023         if (!event)
1024                 return;
1025         entry   = ring_buffer_event_data(event);
1026         entry->arg1                     = arg1;
1027         entry->arg2                     = arg2;
1028         entry->arg3                     = arg3;
1029         trace_buffer_unlock_commit(tr, event, 0, pc);
1030 }
1031
1032 void
1033 __trace_special(void *__tr, void *__data,
1034                 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1035 {
1036         ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1037 }
1038
1039 void
1040 tracing_sched_switch_trace(struct trace_array *tr,
1041                            struct task_struct *prev,
1042                            struct task_struct *next,
1043                            unsigned long flags, int pc)
1044 {
1045         struct ring_buffer_event *event;
1046         struct ctx_switch_entry *entry;
1047
1048         event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1049                                           sizeof(*entry), flags, pc);
1050         if (!event)
1051                 return;
1052         entry   = ring_buffer_event_data(event);
1053         entry->prev_pid                 = prev->pid;
1054         entry->prev_prio                = prev->prio;
1055         entry->prev_state               = prev->state;
1056         entry->next_pid                 = next->pid;
1057         entry->next_prio                = next->prio;
1058         entry->next_state               = next->state;
1059         entry->next_cpu = task_cpu(next);
1060         trace_buffer_unlock_commit(tr, event, flags, pc);
1061 }
1062
1063 void
1064 tracing_sched_wakeup_trace(struct trace_array *tr,
1065                            struct task_struct *wakee,
1066                            struct task_struct *curr,
1067                            unsigned long flags, int pc)
1068 {
1069         struct ring_buffer_event *event;
1070         struct ctx_switch_entry *entry;
1071
1072         event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1073                                           sizeof(*entry), flags, pc);
1074         if (!event)
1075                 return;
1076         entry   = ring_buffer_event_data(event);
1077         entry->prev_pid                 = curr->pid;
1078         entry->prev_prio                = curr->prio;
1079         entry->prev_state               = curr->state;
1080         entry->next_pid                 = wakee->pid;
1081         entry->next_prio                = wakee->prio;
1082         entry->next_state               = wakee->state;
1083         entry->next_cpu                 = task_cpu(wakee);
1084
1085         ring_buffer_unlock_commit(tr->buffer, event);
1086         ftrace_trace_stack(tr, flags, 6, pc);
1087         ftrace_trace_userstack(tr, flags, pc);
1088 }
1089
1090 void
1091 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1092 {
1093         struct trace_array *tr = &global_trace;
1094         struct trace_array_cpu *data;
1095         unsigned long flags;
1096         int cpu;
1097         int pc;
1098
1099         if (tracing_disabled)
1100                 return;
1101
1102         pc = preempt_count();
1103         local_irq_save(flags);
1104         cpu = raw_smp_processor_id();
1105         data = tr->data[cpu];
1106
1107         if (likely(atomic_inc_return(&data->disabled) == 1))
1108                 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1109
1110         atomic_dec(&data->disabled);
1111         local_irq_restore(flags);
1112 }
1113
1114 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1115 int trace_graph_entry(struct ftrace_graph_ent *trace)
1116 {
1117         struct trace_array *tr = &global_trace;
1118         struct trace_array_cpu *data;
1119         unsigned long flags;
1120         long disabled;
1121         int cpu;
1122         int pc;
1123
1124         if (!ftrace_trace_task(current))
1125                 return 0;
1126
1127         if (!ftrace_graph_addr(trace->func))
1128                 return 0;
1129
1130         local_irq_save(flags);
1131         cpu = raw_smp_processor_id();
1132         data = tr->data[cpu];
1133         disabled = atomic_inc_return(&data->disabled);
1134         if (likely(disabled == 1)) {
1135                 pc = preempt_count();
1136                 __trace_graph_entry(tr, trace, flags, pc);
1137         }
1138         /* Only do the atomic if it is not already set */
1139         if (!test_tsk_trace_graph(current))
1140                 set_tsk_trace_graph(current);
1141         atomic_dec(&data->disabled);
1142         local_irq_restore(flags);
1143
1144         return 1;
1145 }
1146
1147 void trace_graph_return(struct ftrace_graph_ret *trace)
1148 {
1149         struct trace_array *tr = &global_trace;
1150         struct trace_array_cpu *data;
1151         unsigned long flags;
1152         long disabled;
1153         int cpu;
1154         int pc;
1155
1156         local_irq_save(flags);
1157         cpu = raw_smp_processor_id();
1158         data = tr->data[cpu];
1159         disabled = atomic_inc_return(&data->disabled);
1160         if (likely(disabled == 1)) {
1161                 pc = preempt_count();
1162                 __trace_graph_return(tr, trace, flags, pc);
1163         }
1164         if (!trace->depth)
1165                 clear_tsk_trace_graph(current);
1166         atomic_dec(&data->disabled);
1167         local_irq_restore(flags);
1168 }
1169 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1170
1171 enum trace_file_type {
1172         TRACE_FILE_LAT_FMT      = 1,
1173         TRACE_FILE_ANNOTATE     = 2,
1174 };
1175
1176 static void trace_iterator_increment(struct trace_iterator *iter)
1177 {
1178         /* Don't allow ftrace to trace into the ring buffers */
1179         ftrace_disable_cpu();
1180
1181         iter->idx++;
1182         if (iter->buffer_iter[iter->cpu])
1183                 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1184
1185         ftrace_enable_cpu();
1186 }
1187
1188 static struct trace_entry *
1189 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1190 {
1191         struct ring_buffer_event *event;
1192         struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1193
1194         /* Don't allow ftrace to trace into the ring buffers */
1195         ftrace_disable_cpu();
1196
1197         if (buf_iter)
1198                 event = ring_buffer_iter_peek(buf_iter, ts);
1199         else
1200                 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1201
1202         ftrace_enable_cpu();
1203
1204         return event ? ring_buffer_event_data(event) : NULL;
1205 }
1206
1207 static struct trace_entry *
1208 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1209 {
1210         struct ring_buffer *buffer = iter->tr->buffer;
1211         struct trace_entry *ent, *next = NULL;
1212         int cpu_file = iter->cpu_file;
1213         u64 next_ts = 0, ts;
1214         int next_cpu = -1;
1215         int cpu;
1216
1217         /*
1218          * If we are in a per_cpu trace file, don't bother by iterating over
1219          * all cpu and peek directly.
1220          */
1221         if (cpu_file > TRACE_PIPE_ALL_CPU) {
1222                 if (ring_buffer_empty_cpu(buffer, cpu_file))
1223                         return NULL;
1224                 ent = peek_next_entry(iter, cpu_file, ent_ts);
1225                 if (ent_cpu)
1226                         *ent_cpu = cpu_file;
1227
1228                 return ent;
1229         }
1230
1231         for_each_tracing_cpu(cpu) {
1232
1233                 if (ring_buffer_empty_cpu(buffer, cpu))
1234                         continue;
1235
1236                 ent = peek_next_entry(iter, cpu, &ts);
1237
1238                 /*
1239                  * Pick the entry with the smallest timestamp:
1240                  */
1241                 if (ent && (!next || ts < next_ts)) {
1242                         next = ent;
1243                         next_cpu = cpu;
1244                         next_ts = ts;
1245                 }
1246         }
1247
1248         if (ent_cpu)
1249                 *ent_cpu = next_cpu;
1250
1251         if (ent_ts)
1252                 *ent_ts = next_ts;
1253
1254         return next;
1255 }
1256
1257 /* Find the next real entry, without updating the iterator itself */
1258 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1259                                           int *ent_cpu, u64 *ent_ts)
1260 {
1261         return __find_next_entry(iter, ent_cpu, ent_ts);
1262 }
1263
1264 /* Find the next real entry, and increment the iterator to the next entry */
1265 static void *find_next_entry_inc(struct trace_iterator *iter)
1266 {
1267         iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1268
1269         if (iter->ent)
1270                 trace_iterator_increment(iter);
1271
1272         return iter->ent ? iter : NULL;
1273 }
1274
1275 static void trace_consume(struct trace_iterator *iter)
1276 {
1277         /* Don't allow ftrace to trace into the ring buffers */
1278         ftrace_disable_cpu();
1279         ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1280         ftrace_enable_cpu();
1281 }
1282
1283 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1284 {
1285         struct trace_iterator *iter = m->private;
1286         int i = (int)*pos;
1287         void *ent;
1288
1289         (*pos)++;
1290
1291         /* can't go backwards */
1292         if (iter->idx > i)
1293                 return NULL;
1294
1295         if (iter->idx < 0)
1296                 ent = find_next_entry_inc(iter);
1297         else
1298                 ent = iter;
1299
1300         while (ent && iter->idx < i)
1301                 ent = find_next_entry_inc(iter);
1302
1303         iter->pos = *pos;
1304
1305         return ent;
1306 }
1307
1308 /*
1309  * No necessary locking here. The worst thing which can
1310  * happen is loosing events consumed at the same time
1311  * by a trace_pipe reader.
1312  * Other than that, we don't risk to crash the ring buffer
1313  * because it serializes the readers.
1314  *
1315  * The current tracer is copied to avoid a global locking
1316  * all around.
1317  */
1318 static void *s_start(struct seq_file *m, loff_t *pos)
1319 {
1320         struct trace_iterator *iter = m->private;
1321         static struct tracer *old_tracer;
1322         int cpu_file = iter->cpu_file;
1323         void *p = NULL;
1324         loff_t l = 0;
1325         int cpu;
1326
1327         /* copy the tracer to avoid using a global lock all around */
1328         mutex_lock(&trace_types_lock);
1329         if (unlikely(old_tracer != current_trace && current_trace)) {
1330                 old_tracer = current_trace;
1331                 *iter->trace = *current_trace;
1332         }
1333         mutex_unlock(&trace_types_lock);
1334
1335         atomic_inc(&trace_record_cmdline_disabled);
1336
1337         if (*pos != iter->pos) {
1338                 iter->ent = NULL;
1339                 iter->cpu = 0;
1340                 iter->idx = -1;
1341
1342                 ftrace_disable_cpu();
1343
1344                 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1345                         for_each_tracing_cpu(cpu)
1346                                 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1347                 } else
1348                         ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1349
1350
1351                 ftrace_enable_cpu();
1352
1353                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1354                         ;
1355
1356         } else {
1357                 l = *pos - 1;
1358                 p = s_next(m, p, &l);
1359         }
1360
1361         return p;
1362 }
1363
1364 static void s_stop(struct seq_file *m, void *p)
1365 {
1366         atomic_dec(&trace_record_cmdline_disabled);
1367 }
1368
1369 static void print_lat_help_header(struct seq_file *m)
1370 {
1371         seq_puts(m, "#                  _------=> CPU#            \n");
1372         seq_puts(m, "#                 / _-----=> irqs-off        \n");
1373         seq_puts(m, "#                | / _----=> need-resched    \n");
1374         seq_puts(m, "#                || / _---=> hardirq/softirq \n");
1375         seq_puts(m, "#                ||| / _--=> preempt-depth   \n");
1376         seq_puts(m, "#                |||| /                      \n");
1377         seq_puts(m, "#                |||||     delay             \n");
1378         seq_puts(m, "#  cmd     pid   ||||| time  |   caller      \n");
1379         seq_puts(m, "#     \\   /      |||||   \\   |   /           \n");
1380 }
1381
1382 static void print_func_help_header(struct seq_file *m)
1383 {
1384         seq_puts(m, "#           TASK-PID    CPU#    TIMESTAMP  FUNCTION\n");
1385         seq_puts(m, "#              | |       |          |         |\n");
1386 }
1387
1388
1389 static void
1390 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1391 {
1392         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1393         struct trace_array *tr = iter->tr;
1394         struct trace_array_cpu *data = tr->data[tr->cpu];
1395         struct tracer *type = current_trace;
1396         unsigned long total;
1397         unsigned long entries;
1398         const char *name = "preemption";
1399
1400         if (type)
1401                 name = type->name;
1402
1403         entries = ring_buffer_entries(iter->tr->buffer);
1404         total = entries +
1405                 ring_buffer_overruns(iter->tr->buffer);
1406
1407         seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1408                    name, UTS_RELEASE);
1409         seq_puts(m, "-----------------------------------"
1410                  "---------------------------------\n");
1411         seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1412                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1413                    nsecs_to_usecs(data->saved_latency),
1414                    entries,
1415                    total,
1416                    tr->cpu,
1417 #if defined(CONFIG_PREEMPT_NONE)
1418                    "server",
1419 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1420                    "desktop",
1421 #elif defined(CONFIG_PREEMPT)
1422                    "preempt",
1423 #else
1424                    "unknown",
1425 #endif
1426                    /* These are reserved for later use */
1427                    0, 0, 0, 0);
1428 #ifdef CONFIG_SMP
1429         seq_printf(m, " #P:%d)\n", num_online_cpus());
1430 #else
1431         seq_puts(m, ")\n");
1432 #endif
1433         seq_puts(m, "    -----------------\n");
1434         seq_printf(m, "    | task: %.16s-%d "
1435                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1436                    data->comm, data->pid, data->uid, data->nice,
1437                    data->policy, data->rt_priority);
1438         seq_puts(m, "    -----------------\n");
1439
1440         if (data->critical_start) {
1441                 seq_puts(m, " => started at: ");
1442                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1443                 trace_print_seq(m, &iter->seq);
1444                 seq_puts(m, "\n => ended at:   ");
1445                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1446                 trace_print_seq(m, &iter->seq);
1447                 seq_puts(m, "\n");
1448         }
1449
1450         seq_puts(m, "\n");
1451 }
1452
1453 static void test_cpu_buff_start(struct trace_iterator *iter)
1454 {
1455         struct trace_seq *s = &iter->seq;
1456
1457         if (!(trace_flags & TRACE_ITER_ANNOTATE))
1458                 return;
1459
1460         if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1461                 return;
1462
1463         if (cpumask_test_cpu(iter->cpu, iter->started))
1464                 return;
1465
1466         cpumask_set_cpu(iter->cpu, iter->started);
1467         trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1468 }
1469
1470 static enum print_line_t print_lat_fmt(struct trace_iterator *iter)
1471 {
1472         struct trace_seq *s = &iter->seq;
1473         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1474         struct trace_event *event;
1475         struct trace_entry *entry = iter->ent;
1476
1477         test_cpu_buff_start(iter);
1478
1479         event = ftrace_find_event(entry->type);
1480
1481         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1482                 if (!trace_print_lat_context(iter))
1483                         goto partial;
1484         }
1485
1486         if (event)
1487                 return event->latency_trace(iter, sym_flags);
1488
1489         if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1490                 goto partial;
1491
1492         return TRACE_TYPE_HANDLED;
1493 partial:
1494         return TRACE_TYPE_PARTIAL_LINE;
1495 }
1496
1497 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1498 {
1499         struct trace_seq *s = &iter->seq;
1500         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1501         struct trace_entry *entry;
1502         struct trace_event *event;
1503
1504         entry = iter->ent;
1505
1506         test_cpu_buff_start(iter);
1507
1508         event = ftrace_find_event(entry->type);
1509
1510         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1511                 if (!trace_print_context(iter))
1512                         goto partial;
1513         }
1514
1515         if (event)
1516                 return event->trace(iter, sym_flags);
1517
1518         if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1519                 goto partial;
1520
1521         return TRACE_TYPE_HANDLED;
1522 partial:
1523         return TRACE_TYPE_PARTIAL_LINE;
1524 }
1525
1526 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1527 {
1528         struct trace_seq *s = &iter->seq;
1529         struct trace_entry *entry;
1530         struct trace_event *event;
1531
1532         entry = iter->ent;
1533
1534         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1535                 if (!trace_seq_printf(s, "%d %d %llu ",
1536                                       entry->pid, iter->cpu, iter->ts))
1537                         goto partial;
1538         }
1539
1540         event = ftrace_find_event(entry->type);
1541         if (event)
1542                 return event->raw(iter, 0);
1543
1544         if (!trace_seq_printf(s, "%d ?\n", entry->type))
1545                 goto partial;
1546
1547         return TRACE_TYPE_HANDLED;
1548 partial:
1549         return TRACE_TYPE_PARTIAL_LINE;
1550 }
1551
1552 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1553 {
1554         struct trace_seq *s = &iter->seq;
1555         unsigned char newline = '\n';
1556         struct trace_entry *entry;
1557         struct trace_event *event;
1558
1559         entry = iter->ent;
1560
1561         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1562                 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1563                 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1564                 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1565         }
1566
1567         event = ftrace_find_event(entry->type);
1568         if (event) {
1569                 enum print_line_t ret = event->hex(iter, 0);
1570                 if (ret != TRACE_TYPE_HANDLED)
1571                         return ret;
1572         }
1573
1574         SEQ_PUT_FIELD_RET(s, newline);
1575
1576         return TRACE_TYPE_HANDLED;
1577 }
1578
1579 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1580 {
1581         struct trace_seq *s = &iter->seq;
1582         struct trace_entry *entry = iter->ent;
1583         struct print_entry *field;
1584         int ret;
1585
1586         trace_assign_type(field, entry);
1587
1588         ret = trace_seq_printf(s, "%s", field->buf);
1589         if (!ret)
1590                 return TRACE_TYPE_PARTIAL_LINE;
1591
1592         return TRACE_TYPE_HANDLED;
1593 }
1594
1595 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1596 {
1597         struct trace_seq *s = &iter->seq;
1598         struct trace_entry *entry;
1599         struct trace_event *event;
1600
1601         entry = iter->ent;
1602
1603         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1604                 SEQ_PUT_FIELD_RET(s, entry->pid);
1605                 SEQ_PUT_FIELD_RET(s, iter->cpu);
1606                 SEQ_PUT_FIELD_RET(s, iter->ts);
1607         }
1608
1609         event = ftrace_find_event(entry->type);
1610         return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1611 }
1612
1613 static int trace_empty(struct trace_iterator *iter)
1614 {
1615         int cpu;
1616
1617         for_each_tracing_cpu(cpu) {
1618                 if (iter->buffer_iter[cpu]) {
1619                         if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1620                                 return 0;
1621                 } else {
1622                         if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1623                                 return 0;
1624                 }
1625         }
1626
1627         return 1;
1628 }
1629
1630 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1631 {
1632         enum print_line_t ret;
1633
1634         if (iter->trace && iter->trace->print_line) {
1635                 ret = iter->trace->print_line(iter);
1636                 if (ret != TRACE_TYPE_UNHANDLED)
1637                         return ret;
1638         }
1639
1640         if (iter->ent->type == TRACE_PRINT &&
1641                         trace_flags & TRACE_ITER_PRINTK &&
1642                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1643                 return print_printk_msg_only(iter);
1644
1645         if (trace_flags & TRACE_ITER_BIN)
1646                 return print_bin_fmt(iter);
1647
1648         if (trace_flags & TRACE_ITER_HEX)
1649                 return print_hex_fmt(iter);
1650
1651         if (trace_flags & TRACE_ITER_RAW)
1652                 return print_raw_fmt(iter);
1653
1654         if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1655                 return print_lat_fmt(iter);
1656
1657         return print_trace_fmt(iter);
1658 }
1659
1660 static int s_show(struct seq_file *m, void *v)
1661 {
1662         struct trace_iterator *iter = v;
1663
1664         if (iter->ent == NULL) {
1665                 if (iter->tr) {
1666                         seq_printf(m, "# tracer: %s\n", iter->trace->name);
1667                         seq_puts(m, "#\n");
1668                 }
1669                 if (iter->trace && iter->trace->print_header)
1670                         iter->trace->print_header(m);
1671                 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1672                         /* print nothing if the buffers are empty */
1673                         if (trace_empty(iter))
1674                                 return 0;
1675                         print_trace_header(m, iter);
1676                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1677                                 print_lat_help_header(m);
1678                 } else {
1679                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1680                                 print_func_help_header(m);
1681                 }
1682         } else {
1683                 print_trace_line(iter);
1684                 trace_print_seq(m, &iter->seq);
1685         }
1686
1687         return 0;
1688 }
1689
1690 static struct seq_operations tracer_seq_ops = {
1691         .start          = s_start,
1692         .next           = s_next,
1693         .stop           = s_stop,
1694         .show           = s_show,
1695 };
1696
1697 static struct trace_iterator *
1698 __tracing_open(struct inode *inode, struct file *file)
1699 {
1700         long cpu_file = (long) inode->i_private;
1701         void *fail_ret = ERR_PTR(-ENOMEM);
1702         struct trace_iterator *iter;
1703         struct seq_file *m;
1704         int cpu, ret;
1705
1706         if (tracing_disabled)
1707                 return ERR_PTR(-ENODEV);
1708
1709         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1710         if (!iter)
1711                 return ERR_PTR(-ENOMEM);
1712
1713         /*
1714          * We make a copy of the current tracer to avoid concurrent
1715          * changes on it while we are reading.
1716          */
1717         mutex_lock(&trace_types_lock);
1718         iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1719         if (!iter->trace)
1720                 goto fail;
1721
1722         if (current_trace)
1723                 *iter->trace = *current_trace;
1724
1725         if (current_trace && current_trace->print_max)
1726                 iter->tr = &max_tr;
1727         else
1728                 iter->tr = &global_trace;
1729         iter->pos = -1;
1730         mutex_init(&iter->mutex);
1731         iter->cpu_file = cpu_file;
1732
1733         /* Notify the tracer early; before we stop tracing. */
1734         if (iter->trace && iter->trace->open)
1735                 iter->trace->open(iter);
1736
1737         /* Annotate start of buffers if we had overruns */
1738         if (ring_buffer_overruns(iter->tr->buffer))
1739                 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1740
1741         if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1742                 for_each_tracing_cpu(cpu) {
1743
1744                         iter->buffer_iter[cpu] =
1745                                 ring_buffer_read_start(iter->tr->buffer, cpu);
1746
1747                         if (!iter->buffer_iter[cpu])
1748                                 goto fail_buffer;
1749                 }
1750         } else {
1751                 cpu = iter->cpu_file;
1752                 iter->buffer_iter[cpu] =
1753                                 ring_buffer_read_start(iter->tr->buffer, cpu);
1754
1755                 if (!iter->buffer_iter[cpu])
1756                         goto fail;
1757         }
1758
1759         /* TODO stop tracer */
1760         ret = seq_open(file, &tracer_seq_ops);
1761         if (ret < 0) {
1762                 fail_ret = ERR_PTR(ret);
1763                 goto fail_buffer;
1764         }
1765
1766         m = file->private_data;
1767         m->private = iter;
1768
1769         /* stop the trace while dumping */
1770         tracing_stop();
1771
1772         mutex_unlock(&trace_types_lock);
1773
1774         return iter;
1775
1776  fail_buffer:
1777         for_each_tracing_cpu(cpu) {
1778                 if (iter->buffer_iter[cpu])
1779                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
1780         }
1781  fail:
1782         mutex_unlock(&trace_types_lock);
1783         kfree(iter->trace);
1784         kfree(iter);
1785
1786         return fail_ret;
1787 }
1788
1789 int tracing_open_generic(struct inode *inode, struct file *filp)
1790 {
1791         if (tracing_disabled)
1792                 return -ENODEV;
1793
1794         filp->private_data = inode->i_private;
1795         return 0;
1796 }
1797
1798 static int tracing_release(struct inode *inode, struct file *file)
1799 {
1800         struct seq_file *m = (struct seq_file *)file->private_data;
1801         struct trace_iterator *iter = m->private;
1802         int cpu;
1803
1804         mutex_lock(&trace_types_lock);
1805         for_each_tracing_cpu(cpu) {
1806                 if (iter->buffer_iter[cpu])
1807                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
1808         }
1809
1810         if (iter->trace && iter->trace->close)
1811                 iter->trace->close(iter);
1812
1813         /* reenable tracing if it was previously enabled */
1814         tracing_start();
1815         mutex_unlock(&trace_types_lock);
1816
1817         seq_release(inode, file);
1818         mutex_destroy(&iter->mutex);
1819         kfree(iter->trace);
1820         kfree(iter);
1821         return 0;
1822 }
1823
1824 static int tracing_open(struct inode *inode, struct file *file)
1825 {
1826         struct trace_iterator *iter;
1827         int ret = 0;
1828
1829         iter = __tracing_open(inode, file);
1830         if (IS_ERR(iter))
1831                 ret = PTR_ERR(iter);
1832
1833         return ret;
1834 }
1835
1836 static int tracing_lt_open(struct inode *inode, struct file *file)
1837 {
1838         struct trace_iterator *iter;
1839         int ret = 0;
1840
1841         iter = __tracing_open(inode, file);
1842
1843         if (IS_ERR(iter))
1844                 ret = PTR_ERR(iter);
1845         else
1846                 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1847
1848         return ret;
1849 }
1850
1851
1852 static void *
1853 t_next(struct seq_file *m, void *v, loff_t *pos)
1854 {
1855         struct tracer *t = m->private;
1856
1857         (*pos)++;
1858
1859         if (t)
1860                 t = t->next;
1861
1862         m->private = t;
1863
1864         return t;
1865 }
1866
1867 static void *t_start(struct seq_file *m, loff_t *pos)
1868 {
1869         struct tracer *t = m->private;
1870         loff_t l = 0;
1871
1872         mutex_lock(&trace_types_lock);
1873         for (; t && l < *pos; t = t_next(m, t, &l))
1874                 ;
1875
1876         return t;
1877 }
1878
1879 static void t_stop(struct seq_file *m, void *p)
1880 {
1881         mutex_unlock(&trace_types_lock);
1882 }
1883
1884 static int t_show(struct seq_file *m, void *v)
1885 {
1886         struct tracer *t = v;
1887
1888         if (!t)
1889                 return 0;
1890
1891         seq_printf(m, "%s", t->name);
1892         if (t->next)
1893                 seq_putc(m, ' ');
1894         else
1895                 seq_putc(m, '\n');
1896
1897         return 0;
1898 }
1899
1900 static struct seq_operations show_traces_seq_ops = {
1901         .start          = t_start,
1902         .next           = t_next,
1903         .stop           = t_stop,
1904         .show           = t_show,
1905 };
1906
1907 static int show_traces_open(struct inode *inode, struct file *file)
1908 {
1909         int ret;
1910
1911         if (tracing_disabled)
1912                 return -ENODEV;
1913
1914         ret = seq_open(file, &show_traces_seq_ops);
1915         if (!ret) {
1916                 struct seq_file *m = file->private_data;
1917                 m->private = trace_types;
1918         }
1919
1920         return ret;
1921 }
1922
1923 static struct file_operations tracing_fops = {
1924         .open           = tracing_open,
1925         .read           = seq_read,
1926         .llseek         = seq_lseek,
1927         .release        = tracing_release,
1928 };
1929
1930 static struct file_operations tracing_lt_fops = {
1931         .open           = tracing_lt_open,
1932         .read           = seq_read,
1933         .llseek         = seq_lseek,
1934         .release        = tracing_release,
1935 };
1936
1937 static struct file_operations show_traces_fops = {
1938         .open           = show_traces_open,
1939         .read           = seq_read,
1940         .release        = seq_release,
1941 };
1942
1943 /*
1944  * Only trace on a CPU if the bitmask is set:
1945  */
1946 static cpumask_var_t tracing_cpumask;
1947
1948 /*
1949  * The tracer itself will not take this lock, but still we want
1950  * to provide a consistent cpumask to user-space:
1951  */
1952 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1953
1954 /*
1955  * Temporary storage for the character representation of the
1956  * CPU bitmask (and one more byte for the newline):
1957  */
1958 static char mask_str[NR_CPUS + 1];
1959
1960 static ssize_t
1961 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1962                      size_t count, loff_t *ppos)
1963 {
1964         int len;
1965
1966         mutex_lock(&tracing_cpumask_update_lock);
1967
1968         len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1969         if (count - len < 2) {
1970                 count = -EINVAL;
1971                 goto out_err;
1972         }
1973         len += sprintf(mask_str + len, "\n");
1974         count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1975
1976 out_err:
1977         mutex_unlock(&tracing_cpumask_update_lock);
1978
1979         return count;
1980 }
1981
1982 static ssize_t
1983 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1984                       size_t count, loff_t *ppos)
1985 {
1986         int err, cpu;
1987         cpumask_var_t tracing_cpumask_new;
1988
1989         if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1990                 return -ENOMEM;
1991
1992         mutex_lock(&tracing_cpumask_update_lock);
1993         err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1994         if (err)
1995                 goto err_unlock;
1996
1997         local_irq_disable();
1998         __raw_spin_lock(&ftrace_max_lock);
1999         for_each_tracing_cpu(cpu) {
2000                 /*
2001                  * Increase/decrease the disabled counter if we are
2002                  * about to flip a bit in the cpumask:
2003                  */
2004                 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2005                                 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2006                         atomic_inc(&global_trace.data[cpu]->disabled);
2007                 }
2008                 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2009                                 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2010                         atomic_dec(&global_trace.data[cpu]->disabled);
2011                 }
2012         }
2013         __raw_spin_unlock(&ftrace_max_lock);
2014         local_irq_enable();
2015
2016         cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2017
2018         mutex_unlock(&tracing_cpumask_update_lock);
2019         free_cpumask_var(tracing_cpumask_new);
2020
2021         return count;
2022
2023 err_unlock:
2024         mutex_unlock(&tracing_cpumask_update_lock);
2025         free_cpumask_var(tracing_cpumask);
2026
2027         return err;
2028 }
2029
2030 static struct file_operations tracing_cpumask_fops = {
2031         .open           = tracing_open_generic,
2032         .read           = tracing_cpumask_read,
2033         .write          = tracing_cpumask_write,
2034 };
2035
2036 static ssize_t
2037 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2038                        size_t cnt, loff_t *ppos)
2039 {
2040         struct tracer_opt *trace_opts;
2041         u32 tracer_flags;
2042         int len = 0;
2043         char *buf;
2044         int r = 0;
2045         int i;
2046
2047
2048         /* calculate max size */
2049         for (i = 0; trace_options[i]; i++) {
2050                 len += strlen(trace_options[i]);
2051                 len += 3; /* "no" and newline */
2052         }
2053
2054         mutex_lock(&trace_types_lock);
2055         tracer_flags = current_trace->flags->val;
2056         trace_opts = current_trace->flags->opts;
2057
2058         /*
2059          * Increase the size with names of options specific
2060          * of the current tracer.
2061          */
2062         for (i = 0; trace_opts[i].name; i++) {
2063                 len += strlen(trace_opts[i].name);
2064                 len += 3; /* "no" and newline */
2065         }
2066
2067         /* +2 for \n and \0 */
2068         buf = kmalloc(len + 2, GFP_KERNEL);
2069         if (!buf) {
2070                 mutex_unlock(&trace_types_lock);
2071                 return -ENOMEM;
2072         }
2073
2074         for (i = 0; trace_options[i]; i++) {
2075                 if (trace_flags & (1 << i))
2076                         r += sprintf(buf + r, "%s\n", trace_options[i]);
2077                 else
2078                         r += sprintf(buf + r, "no%s\n", trace_options[i]);
2079         }
2080
2081         for (i = 0; trace_opts[i].name; i++) {
2082                 if (tracer_flags & trace_opts[i].bit)
2083                         r += sprintf(buf + r, "%s\n",
2084                                 trace_opts[i].name);
2085                 else
2086                         r += sprintf(buf + r, "no%s\n",
2087                                 trace_opts[i].name);
2088         }
2089         mutex_unlock(&trace_types_lock);
2090
2091         WARN_ON(r >= len + 2);
2092
2093         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2094
2095         kfree(buf);
2096         return r;
2097 }
2098
2099 /* Try to assign a tracer specific option */
2100 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2101 {
2102         struct tracer_flags *trace_flags = trace->flags;
2103         struct tracer_opt *opts = NULL;
2104         int ret = 0, i = 0;
2105         int len;
2106
2107         for (i = 0; trace_flags->opts[i].name; i++) {
2108                 opts = &trace_flags->opts[i];
2109                 len = strlen(opts->name);
2110
2111                 if (strncmp(cmp, opts->name, len) == 0) {
2112                         ret = trace->set_flag(trace_flags->val,
2113                                 opts->bit, !neg);
2114                         break;
2115                 }
2116         }
2117         /* Not found */
2118         if (!trace_flags->opts[i].name)
2119                 return -EINVAL;
2120
2121         /* Refused to handle */
2122         if (ret)
2123                 return ret;
2124
2125         if (neg)
2126                 trace_flags->val &= ~opts->bit;
2127         else
2128                 trace_flags->val |= opts->bit;
2129
2130         return 0;
2131 }
2132
2133 static ssize_t
2134 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2135                         size_t cnt, loff_t *ppos)
2136 {
2137         char buf[64];
2138         char *cmp = buf;
2139         int neg = 0;
2140         int ret;
2141         int i;
2142
2143         if (cnt >= sizeof(buf))
2144                 return -EINVAL;
2145
2146         if (copy_from_user(&buf, ubuf, cnt))
2147                 return -EFAULT;
2148
2149         buf[cnt] = 0;
2150
2151         if (strncmp(buf, "no", 2) == 0) {
2152                 neg = 1;
2153                 cmp += 2;
2154         }
2155
2156         for (i = 0; trace_options[i]; i++) {
2157                 int len = strlen(trace_options[i]);
2158
2159                 if (strncmp(cmp, trace_options[i], len) == 0) {
2160                         if (neg)
2161                                 trace_flags &= ~(1 << i);
2162                         else
2163                                 trace_flags |= (1 << i);
2164                         break;
2165                 }
2166         }
2167
2168         /* If no option could be set, test the specific tracer options */
2169         if (!trace_options[i]) {
2170                 mutex_lock(&trace_types_lock);
2171                 ret = set_tracer_option(current_trace, cmp, neg);
2172                 mutex_unlock(&trace_types_lock);
2173                 if (ret)
2174                         return ret;
2175         }
2176
2177         filp->f_pos += cnt;
2178
2179         return cnt;
2180 }
2181
2182 static struct file_operations tracing_iter_fops = {
2183         .open           = tracing_open_generic,
2184         .read           = tracing_trace_options_read,
2185         .write          = tracing_trace_options_write,
2186 };
2187
2188 static const char readme_msg[] =
2189         "tracing mini-HOWTO:\n\n"
2190         "# mkdir /debug\n"
2191         "# mount -t debugfs nodev /debug\n\n"
2192         "# cat /debug/tracing/available_tracers\n"
2193         "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2194         "# cat /debug/tracing/current_tracer\n"
2195         "none\n"
2196         "# echo sched_switch > /debug/tracing/current_tracer\n"
2197         "# cat /debug/tracing/current_tracer\n"
2198         "sched_switch\n"
2199         "# cat /debug/tracing/trace_options\n"
2200         "noprint-parent nosym-offset nosym-addr noverbose\n"
2201         "# echo print-parent > /debug/tracing/trace_options\n"
2202         "# echo 1 > /debug/tracing/tracing_enabled\n"
2203         "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2204         "echo 0 > /debug/tracing/tracing_enabled\n"
2205 ;
2206
2207 static ssize_t
2208 tracing_readme_read(struct file *filp, char __user *ubuf,
2209                        size_t cnt, loff_t *ppos)
2210 {
2211         return simple_read_from_buffer(ubuf, cnt, ppos,
2212                                         readme_msg, strlen(readme_msg));
2213 }
2214
2215 static struct file_operations tracing_readme_fops = {
2216         .open           = tracing_open_generic,
2217         .read           = tracing_readme_read,
2218 };
2219
2220 static ssize_t
2221 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2222                   size_t cnt, loff_t *ppos)
2223 {
2224         char buf[64];
2225         int r;
2226
2227         r = sprintf(buf, "%u\n", tracer_enabled);
2228         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2229 }
2230
2231 static ssize_t
2232 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2233                    size_t cnt, loff_t *ppos)
2234 {
2235         struct trace_array *tr = filp->private_data;
2236         char buf[64];
2237         unsigned long val;
2238         int ret;
2239
2240         if (cnt >= sizeof(buf))
2241                 return -EINVAL;
2242
2243         if (copy_from_user(&buf, ubuf, cnt))
2244                 return -EFAULT;
2245
2246         buf[cnt] = 0;
2247
2248         ret = strict_strtoul(buf, 10, &val);
2249         if (ret < 0)
2250                 return ret;
2251
2252         val = !!val;
2253
2254         mutex_lock(&trace_types_lock);
2255         if (tracer_enabled ^ val) {
2256                 if (val) {
2257                         tracer_enabled = 1;
2258                         if (current_trace->start)
2259                                 current_trace->start(tr);
2260                         tracing_start();
2261                 } else {
2262                         tracer_enabled = 0;
2263                         tracing_stop();
2264                         if (current_trace->stop)
2265                                 current_trace->stop(tr);
2266                 }
2267         }
2268         mutex_unlock(&trace_types_lock);
2269
2270         filp->f_pos += cnt;
2271
2272         return cnt;
2273 }
2274
2275 static ssize_t
2276 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2277                        size_t cnt, loff_t *ppos)
2278 {
2279         char buf[max_tracer_type_len+2];
2280         int r;
2281
2282         mutex_lock(&trace_types_lock);
2283         if (current_trace)
2284                 r = sprintf(buf, "%s\n", current_trace->name);
2285         else
2286                 r = sprintf(buf, "\n");
2287         mutex_unlock(&trace_types_lock);
2288
2289         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2290 }
2291
2292 int tracer_init(struct tracer *t, struct trace_array *tr)
2293 {
2294         tracing_reset_online_cpus(tr);
2295         return t->init(tr);
2296 }
2297
2298 struct trace_option_dentry;
2299
2300 static struct trace_option_dentry *
2301 create_trace_option_files(struct tracer *tracer);
2302
2303 static void
2304 destroy_trace_option_files(struct trace_option_dentry *topts);
2305
2306 static int tracing_set_tracer(const char *buf)
2307 {
2308         static struct trace_option_dentry *topts;
2309         struct trace_array *tr = &global_trace;
2310         struct tracer *t;
2311         int ret = 0;
2312
2313         mutex_lock(&trace_types_lock);
2314         for (t = trace_types; t; t = t->next) {
2315                 if (strcmp(t->name, buf) == 0)
2316                         break;
2317         }
2318         if (!t) {
2319                 ret = -EINVAL;
2320                 goto out;
2321         }
2322         if (t == current_trace)
2323                 goto out;
2324
2325         trace_branch_disable();
2326         if (current_trace && current_trace->reset)
2327                 current_trace->reset(tr);
2328
2329         destroy_trace_option_files(topts);
2330
2331         current_trace = t;
2332
2333         topts = create_trace_option_files(current_trace);
2334
2335         if (t->init) {
2336                 ret = tracer_init(t, tr);
2337                 if (ret)
2338                         goto out;
2339         }
2340
2341         trace_branch_enable(tr);
2342  out:
2343         mutex_unlock(&trace_types_lock);
2344
2345         return ret;
2346 }
2347
2348 static ssize_t
2349 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2350                         size_t cnt, loff_t *ppos)
2351 {
2352         char buf[max_tracer_type_len+1];
2353         int i;
2354         size_t ret;
2355         int err;
2356
2357         ret = cnt;
2358
2359         if (cnt > max_tracer_type_len)
2360                 cnt = max_tracer_type_len;
2361
2362         if (copy_from_user(&buf, ubuf, cnt))
2363                 return -EFAULT;
2364
2365         buf[cnt] = 0;
2366
2367         /* strip ending whitespace. */
2368         for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2369                 buf[i] = 0;
2370
2371         err = tracing_set_tracer(buf);
2372         if (err)
2373                 return err;
2374
2375         filp->f_pos += ret;
2376
2377         return ret;
2378 }
2379
2380 static ssize_t
2381 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2382                      size_t cnt, loff_t *ppos)
2383 {
2384         unsigned long *ptr = filp->private_data;
2385         char buf[64];
2386         int r;
2387
2388         r = snprintf(buf, sizeof(buf), "%ld\n",
2389                      *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2390         if (r > sizeof(buf))
2391                 r = sizeof(buf);
2392         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2393 }
2394
2395 static ssize_t
2396 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2397                       size_t cnt, loff_t *ppos)
2398 {
2399         unsigned long *ptr = filp->private_data;
2400         char buf[64];
2401         unsigned long val;
2402         int ret;
2403
2404         if (cnt >= sizeof(buf))
2405                 return -EINVAL;
2406
2407         if (copy_from_user(&buf, ubuf, cnt))
2408                 return -EFAULT;
2409
2410         buf[cnt] = 0;
2411
2412         ret = strict_strtoul(buf, 10, &val);
2413         if (ret < 0)
2414                 return ret;
2415
2416         *ptr = val * 1000;
2417
2418         return cnt;
2419 }
2420
2421 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2422 {
2423         long cpu_file = (long) inode->i_private;
2424         struct trace_iterator *iter;
2425         int ret = 0;
2426
2427         if (tracing_disabled)
2428                 return -ENODEV;
2429
2430         mutex_lock(&trace_types_lock);
2431
2432         /* We only allow one reader per cpu */
2433         if (cpu_file == TRACE_PIPE_ALL_CPU) {
2434                 if (!cpumask_empty(tracing_reader_cpumask)) {
2435                         ret = -EBUSY;
2436                         goto out;
2437                 }
2438                 cpumask_setall(tracing_reader_cpumask);
2439         } else {
2440                 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2441                         cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2442                 else {
2443                         ret = -EBUSY;
2444                         goto out;
2445                 }
2446         }
2447
2448         /* create a buffer to store the information to pass to userspace */
2449         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2450         if (!iter) {
2451                 ret = -ENOMEM;
2452                 goto out;
2453         }
2454
2455         /*
2456          * We make a copy of the current tracer to avoid concurrent
2457          * changes on it while we are reading.
2458          */
2459         iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2460         if (!iter->trace) {
2461                 ret = -ENOMEM;
2462                 goto fail;
2463         }
2464         if (current_trace)
2465                 *iter->trace = *current_trace;
2466
2467         if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2468                 ret = -ENOMEM;
2469                 goto fail;
2470         }
2471
2472         /* trace pipe does not show start of buffer */
2473         cpumask_setall(iter->started);
2474
2475         iter->cpu_file = cpu_file;
2476         iter->tr = &global_trace;
2477         mutex_init(&iter->mutex);
2478         filp->private_data = iter;
2479
2480         if (iter->trace->pipe_open)
2481                 iter->trace->pipe_open(iter);
2482
2483 out:
2484         mutex_unlock(&trace_types_lock);
2485         return ret;
2486
2487 fail:
2488         kfree(iter->trace);
2489         kfree(iter);
2490         mutex_unlock(&trace_types_lock);
2491         return ret;
2492 }
2493
2494 static int tracing_release_pipe(struct inode *inode, struct file *file)
2495 {
2496         struct trace_iterator *iter = file->private_data;
2497
2498         mutex_lock(&trace_types_lock);
2499
2500         if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2501                 cpumask_clear(tracing_reader_cpumask);
2502         else
2503                 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2504
2505         mutex_unlock(&trace_types_lock);
2506
2507         free_cpumask_var(iter->started);
2508         mutex_destroy(&iter->mutex);
2509         kfree(iter->trace);
2510         kfree(iter);
2511
2512         return 0;
2513 }
2514
2515 static unsigned int
2516 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2517 {
2518         struct trace_iterator *iter = filp->private_data;
2519
2520         if (trace_flags & TRACE_ITER_BLOCK) {
2521                 /*
2522                  * Always select as readable when in blocking mode
2523                  */
2524                 return POLLIN | POLLRDNORM;
2525         } else {
2526                 if (!trace_empty(iter))
2527                         return POLLIN | POLLRDNORM;
2528                 poll_wait(filp, &trace_wait, poll_table);
2529                 if (!trace_empty(iter))
2530                         return POLLIN | POLLRDNORM;
2531
2532                 return 0;
2533         }
2534 }
2535
2536
2537 void default_wait_pipe(struct trace_iterator *iter)
2538 {
2539         DEFINE_WAIT(wait);
2540
2541         prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2542
2543         if (trace_empty(iter))
2544                 schedule();
2545
2546         finish_wait(&trace_wait, &wait);
2547 }
2548
2549 /*
2550  * This is a make-shift waitqueue.
2551  * A tracer might use this callback on some rare cases:
2552  *
2553  *  1) the current tracer might hold the runqueue lock when it wakes up
2554  *     a reader, hence a deadlock (sched, function, and function graph tracers)
2555  *  2) the function tracers, trace all functions, we don't want
2556  *     the overhead of calling wake_up and friends
2557  *     (and tracing them too)
2558  *
2559  *     Anyway, this is really very primitive wakeup.
2560  */
2561 void poll_wait_pipe(struct trace_iterator *iter)
2562 {
2563         set_current_state(TASK_INTERRUPTIBLE);
2564         /* sleep for 100 msecs, and try again. */
2565         schedule_timeout(HZ / 10);
2566 }
2567
2568 /* Must be called with trace_types_lock mutex held. */
2569 static int tracing_wait_pipe(struct file *filp)
2570 {
2571         struct trace_iterator *iter = filp->private_data;
2572
2573         while (trace_empty(iter)) {
2574
2575                 if ((filp->f_flags & O_NONBLOCK)) {
2576                         return -EAGAIN;
2577                 }
2578
2579                 mutex_unlock(&iter->mutex);
2580
2581                 iter->trace->wait_pipe(iter);
2582
2583                 mutex_lock(&iter->mutex);
2584
2585                 if (signal_pending(current))
2586                         return -EINTR;
2587
2588                 /*
2589                  * We block until we read something and tracing is disabled.
2590                  * We still block if tracing is disabled, but we have never
2591                  * read anything. This allows a user to cat this file, and
2592                  * then enable tracing. But after we have read something,
2593                  * we give an EOF when tracing is again disabled.
2594                  *
2595                  * iter->pos will be 0 if we haven't read anything.
2596                  */
2597                 if (!tracer_enabled && iter->pos)
2598                         break;
2599         }
2600
2601         return 1;
2602 }
2603
2604 /*
2605  * Consumer reader.
2606  */
2607 static ssize_t
2608 tracing_read_pipe(struct file *filp, char __user *ubuf,
2609                   size_t cnt, loff_t *ppos)
2610 {
2611         struct trace_iterator *iter = filp->private_data;
2612         static struct tracer *old_tracer;
2613         ssize_t sret;
2614
2615         /* return any leftover data */
2616         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2617         if (sret != -EBUSY)
2618                 return sret;
2619
2620         trace_seq_init(&iter->seq);
2621
2622         /* copy the tracer to avoid using a global lock all around */
2623         mutex_lock(&trace_types_lock);
2624         if (unlikely(old_tracer != current_trace && current_trace)) {
2625                 old_tracer = current_trace;
2626                 *iter->trace = *current_trace;
2627         }
2628         mutex_unlock(&trace_types_lock);
2629
2630         /*
2631          * Avoid more than one consumer on a single file descriptor
2632          * This is just a matter of traces coherency, the ring buffer itself
2633          * is protected.
2634          */
2635         mutex_lock(&iter->mutex);
2636         if (iter->trace->read) {
2637                 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2638                 if (sret)
2639                         goto out;
2640         }
2641
2642 waitagain:
2643         sret = tracing_wait_pipe(filp);
2644         if (sret <= 0)
2645                 goto out;
2646
2647         /* stop when tracing is finished */
2648         if (trace_empty(iter)) {
2649                 sret = 0;
2650                 goto out;
2651         }
2652
2653         if (cnt >= PAGE_SIZE)
2654                 cnt = PAGE_SIZE - 1;
2655
2656         /* reset all but tr, trace, and overruns */
2657         memset(&iter->seq, 0,
2658                sizeof(struct trace_iterator) -
2659                offsetof(struct trace_iterator, seq));
2660         iter->pos = -1;
2661
2662         while (find_next_entry_inc(iter) != NULL) {
2663                 enum print_line_t ret;
2664                 int len = iter->seq.len;
2665
2666                 ret = print_trace_line(iter);
2667                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2668                         /* don't print partial lines */
2669                         iter->seq.len = len;
2670                         break;
2671                 }
2672                 if (ret != TRACE_TYPE_NO_CONSUME)
2673                         trace_consume(iter);
2674
2675                 if (iter->seq.len >= cnt)
2676                         break;
2677         }
2678
2679         /* Now copy what we have to the user */
2680         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2681         if (iter->seq.readpos >= iter->seq.len)
2682                 trace_seq_init(&iter->seq);
2683
2684         /*
2685          * If there was nothing to send to user, inspite of consuming trace
2686          * entries, go back to wait for more entries.
2687          */
2688         if (sret == -EBUSY)
2689                 goto waitagain;
2690
2691 out:
2692         mutex_unlock(&iter->mutex);
2693
2694         return sret;
2695 }
2696
2697 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2698                                      struct pipe_buffer *buf)
2699 {
2700         __free_page(buf->page);
2701 }
2702
2703 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2704                                      unsigned int idx)
2705 {
2706         __free_page(spd->pages[idx]);
2707 }
2708
2709 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2710         .can_merge              = 0,
2711         .map                    = generic_pipe_buf_map,
2712         .unmap                  = generic_pipe_buf_unmap,
2713         .confirm                = generic_pipe_buf_confirm,
2714         .release                = tracing_pipe_buf_release,
2715         .steal                  = generic_pipe_buf_steal,
2716         .get                    = generic_pipe_buf_get,
2717 };
2718
2719 static size_t
2720 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2721 {
2722         size_t count;
2723         int ret;
2724
2725         /* Seq buffer is page-sized, exactly what we need. */
2726         for (;;) {
2727                 count = iter->seq.len;
2728                 ret = print_trace_line(iter);
2729                 count = iter->seq.len - count;
2730                 if (rem < count) {
2731                         rem = 0;
2732                         iter->seq.len -= count;
2733                         break;
2734                 }
2735                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2736                         iter->seq.len -= count;
2737                         break;
2738                 }
2739
2740                 trace_consume(iter);
2741                 rem -= count;
2742                 if (!find_next_entry_inc(iter)) {
2743                         rem = 0;
2744                         iter->ent = NULL;
2745                         break;
2746                 }
2747         }
2748
2749         return rem;
2750 }
2751
2752 static ssize_t tracing_splice_read_pipe(struct file *filp,
2753                                         loff_t *ppos,
2754                                         struct pipe_inode_info *pipe,
2755                                         size_t len,
2756                                         unsigned int flags)
2757 {
2758         struct page *pages[PIPE_BUFFERS];
2759         struct partial_page partial[PIPE_BUFFERS];
2760         struct trace_iterator *iter = filp->private_data;
2761         struct splice_pipe_desc spd = {
2762                 .pages          = pages,
2763                 .partial        = partial,
2764                 .nr_pages       = 0, /* This gets updated below. */
2765                 .flags          = flags,
2766                 .ops            = &tracing_pipe_buf_ops,
2767                 .spd_release    = tracing_spd_release_pipe,
2768         };
2769         static struct tracer *old_tracer;
2770         ssize_t ret;
2771         size_t rem;
2772         unsigned int i;
2773
2774         /* copy the tracer to avoid using a global lock all around */
2775         mutex_lock(&trace_types_lock);
2776         if (unlikely(old_tracer != current_trace && current_trace)) {
2777                 old_tracer = current_trace;
2778                 *iter->trace = *current_trace;
2779         }
2780         mutex_unlock(&trace_types_lock);
2781
2782         mutex_lock(&iter->mutex);
2783
2784         if (iter->trace->splice_read) {
2785                 ret = iter->trace->splice_read(iter, filp,
2786                                                ppos, pipe, len, flags);
2787                 if (ret)
2788                         goto out_err;
2789         }
2790
2791         ret = tracing_wait_pipe(filp);
2792         if (ret <= 0)
2793                 goto out_err;
2794
2795         if (!iter->ent && !find_next_entry_inc(iter)) {
2796                 ret = -EFAULT;
2797                 goto out_err;
2798         }
2799
2800         /* Fill as many pages as possible. */
2801         for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2802                 pages[i] = alloc_page(GFP_KERNEL);
2803                 if (!pages[i])
2804                         break;
2805
2806                 rem = tracing_fill_pipe_page(rem, iter);
2807
2808                 /* Copy the data into the page, so we can start over. */
2809                 ret = trace_seq_to_buffer(&iter->seq,
2810                                           page_address(pages[i]),
2811                                           iter->seq.len);
2812                 if (ret < 0) {
2813                         __free_page(pages[i]);
2814                         break;
2815                 }
2816                 partial[i].offset = 0;
2817                 partial[i].len = iter->seq.len;
2818
2819                 trace_seq_init(&iter->seq);
2820         }
2821
2822         mutex_unlock(&iter->mutex);
2823
2824         spd.nr_pages = i;
2825
2826         return splice_to_pipe(pipe, &spd);
2827
2828 out_err:
2829         mutex_unlock(&iter->mutex);
2830
2831         return ret;
2832 }
2833
2834 static ssize_t
2835 tracing_entries_read(struct file *filp, char __user *ubuf,
2836                      size_t cnt, loff_t *ppos)
2837 {
2838         struct trace_array *tr = filp->private_data;
2839         char buf[64];
2840         int r;
2841
2842         r = sprintf(buf, "%lu\n", tr->entries >> 10);
2843         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2844 }
2845
2846 static ssize_t
2847 tracing_entries_write(struct file *filp, const char __user *ubuf,
2848                       size_t cnt, loff_t *ppos)
2849 {
2850         unsigned long val;
2851         char buf[64];
2852         int ret, cpu;
2853
2854         if (cnt >= sizeof(buf))
2855                 return -EINVAL;
2856
2857         if (copy_from_user(&buf, ubuf, cnt))
2858                 return -EFAULT;
2859
2860         buf[cnt] = 0;
2861
2862         ret = strict_strtoul(buf, 10, &val);
2863         if (ret < 0)
2864                 return ret;
2865
2866         /* must have at least 1 entry */
2867         if (!val)
2868                 return -EINVAL;
2869
2870         mutex_lock(&trace_types_lock);
2871
2872         tracing_stop();
2873
2874         /* disable all cpu buffers */
2875         for_each_tracing_cpu(cpu) {
2876                 if (global_trace.data[cpu])
2877                         atomic_inc(&global_trace.data[cpu]->disabled);
2878                 if (max_tr.data[cpu])
2879                         atomic_inc(&max_tr.data[cpu]->disabled);
2880         }
2881
2882         /* value is in KB */
2883         val <<= 10;
2884
2885         if (val != global_trace.entries) {
2886                 ret = ring_buffer_resize(global_trace.buffer, val);
2887                 if (ret < 0) {
2888                         cnt = ret;
2889                         goto out;
2890                 }
2891
2892                 ret = ring_buffer_resize(max_tr.buffer, val);
2893                 if (ret < 0) {
2894                         int r;
2895                         cnt = ret;
2896                         r = ring_buffer_resize(global_trace.buffer,
2897                                                global_trace.entries);
2898                         if (r < 0) {
2899                                 /* AARGH! We are left with different
2900                                  * size max buffer!!!! */
2901                                 WARN_ON(1);
2902                                 tracing_disabled = 1;
2903                         }
2904                         goto out;
2905                 }
2906
2907                 global_trace.entries = val;
2908         }
2909
2910         filp->f_pos += cnt;
2911
2912         /* If check pages failed, return ENOMEM */
2913         if (tracing_disabled)
2914                 cnt = -ENOMEM;
2915  out:
2916         for_each_tracing_cpu(cpu) {
2917                 if (global_trace.data[cpu])
2918                         atomic_dec(&global_trace.data[cpu]->disabled);
2919                 if (max_tr.data[cpu])
2920                         atomic_dec(&max_tr.data[cpu]->disabled);
2921         }
2922
2923         tracing_start();
2924         max_tr.entries = global_trace.entries;
2925         mutex_unlock(&trace_types_lock);
2926
2927         return cnt;
2928 }
2929
2930 static int mark_printk(const char *fmt, ...)
2931 {
2932         int ret;
2933         va_list args;
2934         va_start(args, fmt);
2935         ret = trace_vprintk(0, -1, fmt, args);
2936         va_end(args);
2937         return ret;
2938 }
2939
2940 static ssize_t
2941 tracing_mark_write(struct file *filp, const char __user *ubuf,
2942                                         size_t cnt, loff_t *fpos)
2943 {
2944         char *buf;
2945         char *end;
2946
2947         if (tracing_disabled)
2948                 return -EINVAL;
2949
2950         if (cnt > TRACE_BUF_SIZE)
2951                 cnt = TRACE_BUF_SIZE;
2952
2953         buf = kmalloc(cnt + 1, GFP_KERNEL);
2954         if (buf == NULL)
2955                 return -ENOMEM;
2956
2957         if (copy_from_user(buf, ubuf, cnt)) {
2958                 kfree(buf);
2959                 return -EFAULT;
2960         }
2961
2962         /* Cut from the first nil or newline. */
2963         buf[cnt] = '\0';
2964         end = strchr(buf, '\n');
2965         if (end)
2966                 *end = '\0';
2967
2968         cnt = mark_printk("%s\n", buf);
2969         kfree(buf);
2970         *fpos += cnt;
2971
2972         return cnt;
2973 }
2974
2975 static struct file_operations tracing_max_lat_fops = {
2976         .open           = tracing_open_generic,
2977         .read           = tracing_max_lat_read,
2978         .write          = tracing_max_lat_write,
2979 };
2980
2981 static struct file_operations tracing_ctrl_fops = {
2982         .open           = tracing_open_generic,
2983         .read           = tracing_ctrl_read,
2984         .write          = tracing_ctrl_write,
2985 };
2986
2987 static struct file_operations set_tracer_fops = {
2988         .open           = tracing_open_generic,
2989         .read           = tracing_set_trace_read,
2990         .write          = tracing_set_trace_write,
2991 };
2992
2993 static struct file_operations tracing_pipe_fops = {
2994         .open           = tracing_open_pipe,
2995         .poll           = tracing_poll_pipe,
2996         .read           = tracing_read_pipe,
2997         .splice_read    = tracing_splice_read_pipe,
2998         .release        = tracing_release_pipe,
2999 };
3000
3001 static struct file_operations tracing_entries_fops = {
3002         .open           = tracing_open_generic,
3003         .read           = tracing_entries_read,
3004         .write          = tracing_entries_write,
3005 };
3006
3007 static struct file_operations tracing_mark_fops = {
3008         .open           = tracing_open_generic,
3009         .write          = tracing_mark_write,
3010 };
3011
3012 struct ftrace_buffer_info {
3013         struct trace_array      *tr;
3014         void                    *spare;
3015         int                     cpu;
3016         unsigned int            read;
3017 };
3018
3019 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3020 {
3021         int cpu = (int)(long)inode->i_private;
3022         struct ftrace_buffer_info *info;
3023
3024         if (tracing_disabled)
3025                 return -ENODEV;
3026
3027         info = kzalloc(sizeof(*info), GFP_KERNEL);
3028         if (!info)
3029                 return -ENOMEM;
3030
3031         info->tr        = &global_trace;
3032         info->cpu       = cpu;
3033         info->spare     = ring_buffer_alloc_read_page(info->tr->buffer);
3034         /* Force reading ring buffer for first read */
3035         info->read      = (unsigned int)-1;
3036         if (!info->spare)
3037                 goto out;
3038
3039         filp->private_data = info;
3040
3041         return 0;
3042
3043  out:
3044         kfree(info);
3045         return -ENOMEM;
3046 }
3047
3048 static ssize_t
3049 tracing_buffers_read(struct file *filp, char __user *ubuf,
3050                      size_t count, loff_t *ppos)
3051 {
3052         struct ftrace_buffer_info *info = filp->private_data;
3053         unsigned int pos;
3054         ssize_t ret;
3055         size_t size;
3056
3057         if (!count)
3058                 return 0;
3059
3060         /* Do we have previous read data to read? */
3061         if (info->read < PAGE_SIZE)
3062                 goto read;
3063
3064         info->read = 0;
3065
3066         ret = ring_buffer_read_page(info->tr->buffer,
3067                                     &info->spare,
3068                                     count,
3069                                     info->cpu, 0);
3070         if (ret < 0)
3071                 return 0;
3072
3073         pos = ring_buffer_page_len(info->spare);
3074
3075         if (pos < PAGE_SIZE)
3076                 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3077
3078 read:
3079         size = PAGE_SIZE - info->read;
3080         if (size > count)
3081                 size = count;
3082
3083         ret = copy_to_user(ubuf, info->spare + info->read, size);
3084         if (ret == size)
3085                 return -EFAULT;
3086         size -= ret;
3087
3088         *ppos += size;
3089         info->read += size;
3090
3091         return size;
3092 }
3093
3094 static int tracing_buffers_release(struct inode *inode, struct file *file)
3095 {
3096         struct ftrace_buffer_info *info = file->private_data;
3097
3098         ring_buffer_free_read_page(info->tr->buffer, info->spare);
3099         kfree(info);
3100
3101         return 0;
3102 }
3103
3104 struct buffer_ref {
3105         struct ring_buffer      *buffer;
3106         void                    *page;
3107         int                     ref;
3108 };
3109
3110 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3111                                     struct pipe_buffer *buf)
3112 {
3113         struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3114
3115         if (--ref->ref)
3116                 return;
3117
3118         ring_buffer_free_read_page(ref->buffer, ref->page);
3119         kfree(ref);
3120         buf->private = 0;
3121 }
3122
3123 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3124                                  struct pipe_buffer *buf)
3125 {
3126         return 1;
3127 }
3128
3129 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3130                                 struct pipe_buffer *buf)
3131 {
3132         struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3133
3134         ref->ref++;
3135 }
3136
3137 /* Pipe buffer operations for a buffer. */
3138 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3139         .can_merge              = 0,
3140         .map                    = generic_pipe_buf_map,
3141         .unmap                  = generic_pipe_buf_unmap,
3142         .confirm                = generic_pipe_buf_confirm,
3143         .release                = buffer_pipe_buf_release,
3144         .steal                  = buffer_pipe_buf_steal,
3145         .get                    = buffer_pipe_buf_get,
3146 };
3147
3148 /*
3149  * Callback from splice_to_pipe(), if we need to release some pages
3150  * at the end of the spd in case we error'ed out in filling the pipe.
3151  */
3152 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3153 {
3154         struct buffer_ref *ref =
3155                 (struct buffer_ref *)spd->partial[i].private;
3156
3157         if (--ref->ref)
3158                 return;
3159
3160         ring_buffer_free_read_page(ref->buffer, ref->page);
3161         kfree(ref);
3162         spd->partial[i].private = 0;
3163 }
3164
3165 static ssize_t
3166 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3167                             struct pipe_inode_info *pipe, size_t len,
3168                             unsigned int flags)
3169 {
3170         struct ftrace_buffer_info *info = file->private_data;
3171         struct partial_page partial[PIPE_BUFFERS];
3172         struct page *pages[PIPE_BUFFERS];
3173         struct splice_pipe_desc spd = {
3174                 .pages          = pages,
3175                 .partial        = partial,
3176                 .flags          = flags,
3177                 .ops            = &buffer_pipe_buf_ops,
3178                 .spd_release    = buffer_spd_release,
3179         };
3180         struct buffer_ref *ref;
3181         int size, i;
3182         size_t ret;
3183
3184         /*
3185          * We can't seek on a buffer input
3186          */
3187         if (unlikely(*ppos))
3188                 return -ESPIPE;
3189
3190
3191         for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3192                 struct page *page;
3193                 int r;
3194
3195                 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3196                 if (!ref)
3197                         break;
3198
3199                 ref->buffer = info->tr->buffer;
3200                 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3201                 if (!ref->page) {
3202                         kfree(ref);
3203                         break;
3204                 }
3205
3206                 r = ring_buffer_read_page(ref->buffer, &ref->page,
3207                                           len, info->cpu, 0);
3208                 if (r < 0) {
3209                         ring_buffer_free_read_page(ref->buffer,
3210                                                    ref->page);
3211                         kfree(ref);
3212                         break;
3213                 }
3214
3215                 /*
3216                  * zero out any left over data, this is going to
3217                  * user land.
3218                  */
3219                 size = ring_buffer_page_len(ref->page);
3220                 if (size < PAGE_SIZE)
3221                         memset(ref->page + size, 0, PAGE_SIZE - size);
3222
3223                 page = virt_to_page(ref->page);
3224
3225                 spd.pages[i] = page;
3226                 spd.partial[i].len = PAGE_SIZE;
3227                 spd.partial[i].offset = 0;
3228                 spd.partial[i].private = (unsigned long)ref;
3229                 spd.nr_pages++;
3230         }
3231
3232         spd.nr_pages = i;
3233
3234         /* did we read anything? */
3235         if (!spd.nr_pages) {
3236                 if (flags & SPLICE_F_NONBLOCK)
3237                         ret = -EAGAIN;
3238                 else
3239                         ret = 0;
3240                 /* TODO: block */
3241                 return ret;
3242         }
3243
3244         ret = splice_to_pipe(pipe, &spd);
3245
3246         return ret;
3247 }
3248
3249 static const struct file_operations tracing_buffers_fops = {
3250         .open           = tracing_buffers_open,
3251         .read           = tracing_buffers_read,
3252         .release        = tracing_buffers_release,
3253         .splice_read    = tracing_buffers_splice_read,
3254         .llseek         = no_llseek,
3255 };
3256
3257 #ifdef CONFIG_DYNAMIC_FTRACE
3258
3259 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3260 {
3261         return 0;
3262 }
3263
3264 static ssize_t
3265 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3266                   size_t cnt, loff_t *ppos)
3267 {
3268         static char ftrace_dyn_info_buffer[1024];
3269         static DEFINE_MUTEX(dyn_info_mutex);
3270         unsigned long *p = filp->private_data;
3271         char *buf = ftrace_dyn_info_buffer;
3272         int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3273         int r;
3274
3275         mutex_lock(&dyn_info_mutex);
3276         r = sprintf(buf, "%ld ", *p);
3277
3278         r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3279         buf[r++] = '\n';
3280
3281         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3282
3283         mutex_unlock(&dyn_info_mutex);
3284
3285         return r;
3286 }
3287
3288 static struct file_operations tracing_dyn_info_fops = {
3289         .open           = tracing_open_generic,
3290         .read           = tracing_read_dyn_info,
3291 };
3292 #endif
3293
3294 static struct dentry *d_tracer;
3295
3296 struct dentry *tracing_init_dentry(void)
3297 {
3298         static int once;
3299
3300         if (d_tracer)
3301                 return d_tracer;
3302
3303         d_tracer = debugfs_create_dir("tracing", NULL);
3304
3305         if (!d_tracer && !once) {
3306                 once = 1;
3307                 pr_warning("Could not create debugfs directory 'tracing'\n");
3308                 return NULL;
3309         }
3310
3311         return d_tracer;
3312 }
3313
3314 static struct dentry *d_percpu;
3315
3316 struct dentry *tracing_dentry_percpu(void)
3317 {
3318         static int once;
3319         struct dentry *d_tracer;
3320
3321         if (d_percpu)
3322                 return d_percpu;
3323
3324         d_tracer = tracing_init_dentry();
3325
3326         if (!d_tracer)
3327                 return NULL;
3328
3329         d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3330
3331         if (!d_percpu && !once) {
3332                 once = 1;
3333                 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3334                 return NULL;
3335         }
3336
3337         return d_percpu;
3338 }
3339
3340 static void tracing_init_debugfs_percpu(long cpu)
3341 {
3342         struct dentry *d_percpu = tracing_dentry_percpu();
3343         struct dentry *entry, *d_cpu;
3344         /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3345         char cpu_dir[7];
3346
3347         if (cpu > 999 || cpu < 0)
3348                 return;
3349
3350         sprintf(cpu_dir, "cpu%ld", cpu);
3351         d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3352         if (!d_cpu) {
3353                 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3354                 return;
3355         }
3356
3357         /* per cpu trace_pipe */
3358         entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
3359                                 (void *) cpu, &tracing_pipe_fops);
3360         if (!entry)
3361                 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3362
3363         /* per cpu trace */
3364         entry = debugfs_create_file("trace", 0444, d_cpu,
3365                                 (void *) cpu, &tracing_fops);
3366         if (!entry)
3367                 pr_warning("Could not create debugfs 'trace' entry\n");
3368 }
3369
3370 #ifdef CONFIG_FTRACE_SELFTEST
3371 /* Let selftest have access to static functions in this file */
3372 #include "trace_selftest.c"
3373 #endif
3374
3375 struct trace_option_dentry {
3376         struct tracer_opt               *opt;
3377         struct tracer_flags             *flags;
3378         struct dentry                   *entry;
3379 };
3380
3381 static ssize_t
3382 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3383                         loff_t *ppos)
3384 {
3385         struct trace_option_dentry *topt = filp->private_data;
3386         char *buf;
3387
3388         if (topt->flags->val & topt->opt->bit)
3389                 buf = "1\n";
3390         else
3391                 buf = "0\n";
3392
3393         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3394 }
3395
3396 static ssize_t
3397 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3398                          loff_t *ppos)
3399 {
3400         struct trace_option_dentry *topt = filp->private_data;
3401         unsigned long val;
3402         char buf[64];
3403         int ret;
3404
3405         if (cnt >= sizeof(buf))
3406                 return -EINVAL;
3407
3408         if (copy_from_user(&buf, ubuf, cnt))
3409                 return -EFAULT;
3410
3411         buf[cnt] = 0;
3412
3413         ret = strict_strtoul(buf, 10, &val);
3414         if (ret < 0)
3415                 return ret;
3416
3417         ret = 0;
3418         switch (val) {
3419         case 0:
3420                 /* do nothing if already cleared */
3421                 if (!(topt->flags->val & topt->opt->bit))
3422                         break;
3423
3424                 mutex_lock(&trace_types_lock);
3425                 if (current_trace->set_flag)
3426                         ret = current_trace->set_flag(topt->flags->val,
3427                                                       topt->opt->bit, 0);
3428                 mutex_unlock(&trace_types_lock);
3429                 if (ret)
3430                         return ret;
3431                 topt->flags->val &= ~topt->opt->bit;
3432                 break;
3433         case 1:
3434                 /* do nothing if already set */
3435                 if (topt->flags->val & topt->opt->bit)
3436                         break;
3437
3438                 mutex_lock(&trace_types_lock);
3439                 if (current_trace->set_flag)
3440                         ret = current_trace->set_flag(topt->flags->val,
3441                                                       topt->opt->bit, 1);
3442                 mutex_unlock(&trace_types_lock);
3443                 if (ret)
3444                         return ret;
3445                 topt->flags->val |= topt->opt->bit;
3446                 break;
3447
3448         default:
3449                 return -EINVAL;
3450         }
3451
3452         *ppos += cnt;
3453
3454         return cnt;
3455 }
3456
3457
3458 static const struct file_operations trace_options_fops = {
3459         .open = tracing_open_generic,
3460         .read = trace_options_read,
3461         .write = trace_options_write,
3462 };
3463
3464 static ssize_t
3465 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3466                         loff_t *ppos)
3467 {
3468         long index = (long)filp->private_data;
3469         char *buf;
3470
3471         if (trace_flags & (1 << index))
3472                 buf = "1\n";
3473         else
3474                 buf = "0\n";
3475
3476         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3477 }
3478
3479 static ssize_t
3480 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3481                          loff_t *ppos)
3482 {
3483         long index = (long)filp->private_data;
3484         char buf[64];
3485         unsigned long val;
3486         int ret;
3487
3488         if (cnt >= sizeof(buf))
3489                 return -EINVAL;
3490
3491         if (copy_from_user(&buf, ubuf, cnt))
3492                 return -EFAULT;
3493
3494         buf[cnt] = 0;
3495
3496         ret = strict_strtoul(buf, 10, &val);
3497         if (ret < 0)
3498                 return ret;
3499
3500         switch (val) {
3501         case 0:
3502                 trace_flags &= ~(1 << index);
3503                 break;
3504         case 1:
3505                 trace_flags |= 1 << index;
3506                 break;
3507
3508         default:
3509                 return -EINVAL;
3510         }
3511
3512         *ppos += cnt;
3513
3514         return cnt;
3515 }
3516
3517 static const struct file_operations trace_options_core_fops = {
3518         .open = tracing_open_generic,
3519         .read = trace_options_core_read,
3520         .write = trace_options_core_write,
3521 };
3522
3523 static struct dentry *trace_options_init_dentry(void)
3524 {
3525         struct dentry *d_tracer;
3526         static struct dentry *t_options;
3527
3528         if (t_options)
3529                 return t_options;
3530
3531         d_tracer = tracing_init_dentry();
3532         if (!d_tracer)
3533                 return NULL;
3534
3535         t_options = debugfs_create_dir("options", d_tracer);
3536         if (!t_options) {
3537                 pr_warning("Could not create debugfs directory 'options'\n");
3538                 return NULL;
3539         }
3540
3541         return t_options;
3542 }
3543
3544 static void
3545 create_trace_option_file(struct trace_option_dentry *topt,
3546                          struct tracer_flags *flags,
3547                          struct tracer_opt *opt)
3548 {
3549         struct dentry *t_options;
3550         struct dentry *entry;
3551
3552         t_options = trace_options_init_dentry();
3553         if (!t_options)
3554                 return;
3555
3556         topt->flags = flags;
3557         topt->opt = opt;
3558
3559         entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3560                                     &trace_options_fops);
3561
3562         topt->entry = entry;
3563
3564 }
3565
3566 static struct trace_option_dentry *
3567 create_trace_option_files(struct tracer *tracer)
3568 {
3569         struct trace_option_dentry *topts;
3570         struct tracer_flags *flags;
3571         struct tracer_opt *opts;
3572         int cnt;
3573
3574         if (!tracer)
3575                 return NULL;
3576
3577         flags = tracer->flags;
3578
3579         if (!flags || !flags->opts)
3580                 return NULL;
3581
3582         opts = flags->opts;
3583
3584         for (cnt = 0; opts[cnt].name; cnt++)
3585                 ;
3586
3587         topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3588         if (!topts)
3589                 return NULL;
3590
3591         for (cnt = 0; opts[cnt].name; cnt++)
3592                 create_trace_option_file(&topts[cnt], flags,
3593                                          &opts[cnt]);
3594
3595         return topts;
3596 }
3597
3598 static void
3599 destroy_trace_option_files(struct trace_option_dentry *topts)
3600 {
3601         int cnt;
3602
3603         if (!topts)
3604                 return;
3605
3606         for (cnt = 0; topts[cnt].opt; cnt++) {
3607                 if (topts[cnt].entry)
3608                         debugfs_remove(topts[cnt].entry);
3609         }
3610
3611         kfree(topts);
3612 }
3613
3614 static struct dentry *
3615 create_trace_option_core_file(const char *option, long index)
3616 {
3617         struct dentry *t_options;
3618         struct dentry *entry;
3619
3620         t_options = trace_options_init_dentry();
3621         if (!t_options)
3622                 return NULL;
3623
3624         entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3625                                     &trace_options_core_fops);
3626
3627         return entry;
3628 }
3629
3630 static __init void create_trace_options_dir(void)
3631 {
3632         struct dentry *t_options;
3633         struct dentry *entry;
3634         int i;
3635
3636         t_options = trace_options_init_dentry();
3637         if (!t_options)
3638                 return;
3639
3640         for (i = 0; trace_options[i]; i++) {
3641                 entry = create_trace_option_core_file(trace_options[i], i);
3642                 if (!entry)
3643                         pr_warning("Could not create debugfs %s entry\n",
3644                                    trace_options[i]);
3645         }
3646 }
3647
3648 static __init int tracer_init_debugfs(void)
3649 {
3650         struct dentry *d_tracer;
3651         struct dentry *buffers;
3652         struct dentry *entry;
3653         int cpu;
3654
3655         d_tracer = tracing_init_dentry();
3656
3657         entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3658                                     &global_trace, &tracing_ctrl_fops);
3659         if (!entry)
3660                 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3661
3662         entry = debugfs_create_file("trace_options", 0644, d_tracer,
3663                                     NULL, &tracing_iter_fops);
3664         if (!entry)
3665                 pr_warning("Could not create debugfs 'trace_options' entry\n");
3666
3667         create_trace_options_dir();
3668
3669         entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3670                                     NULL, &tracing_cpumask_fops);
3671         if (!entry)
3672                 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3673
3674         entry = debugfs_create_file("trace", 0444, d_tracer,
3675                                  (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3676         if (!entry)
3677                 pr_warning("Could not create debugfs 'trace' entry\n");
3678
3679         entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3680                                     &global_trace, &show_traces_fops);
3681         if (!entry)
3682                 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3683
3684         entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3685                                     &global_trace, &set_tracer_fops);
3686         if (!entry)
3687                 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3688
3689         entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3690                                     &tracing_max_latency,
3691                                     &tracing_max_lat_fops);
3692         if (!entry)
3693                 pr_warning("Could not create debugfs "
3694                            "'tracing_max_latency' entry\n");
3695
3696         entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3697                                     &tracing_thresh, &tracing_max_lat_fops);
3698         if (!entry)
3699                 pr_warning("Could not create debugfs "
3700                            "'tracing_thresh' entry\n");
3701         entry = debugfs_create_file("README", 0644, d_tracer,
3702                                     NULL, &tracing_readme_fops);
3703         if (!entry)
3704                 pr_warning("Could not create debugfs 'README' entry\n");
3705
3706         entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3707                         (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3708         if (!entry)
3709                 pr_warning("Could not create debugfs "
3710                            "'trace_pipe' entry\n");
3711
3712         entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3713                                     &global_trace, &tracing_entries_fops);
3714         if (!entry)
3715                 pr_warning("Could not create debugfs "
3716                            "'buffer_size_kb' entry\n");
3717
3718         entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3719                                     NULL, &tracing_mark_fops);
3720         if (!entry)
3721                 pr_warning("Could not create debugfs "
3722                            "'trace_marker' entry\n");
3723
3724         buffers = debugfs_create_dir("binary_buffers", d_tracer);
3725
3726         if (!buffers)
3727                 pr_warning("Could not create buffers directory\n");
3728         else {
3729                 int cpu;
3730                 char buf[64];
3731
3732                 for_each_tracing_cpu(cpu) {
3733                         sprintf(buf, "%d", cpu);
3734
3735                         entry = debugfs_create_file(buf, 0444, buffers,
3736                                                     (void *)(long)cpu,
3737                                                     &tracing_buffers_fops);
3738                         if (!entry)
3739                                 pr_warning("Could not create debugfs buffers "
3740                                            "'%s' entry\n", buf);
3741                 }
3742         }
3743
3744 #ifdef CONFIG_DYNAMIC_FTRACE
3745         entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3746                                     &ftrace_update_tot_cnt,
3747                                     &tracing_dyn_info_fops);
3748         if (!entry)
3749                 pr_warning("Could not create debugfs "
3750                            "'dyn_ftrace_total_info' entry\n");
3751 #endif
3752 #ifdef CONFIG_SYSPROF_TRACER
3753         init_tracer_sysprof_debugfs(d_tracer);
3754 #endif
3755
3756         for_each_tracing_cpu(cpu)
3757                 tracing_init_debugfs_percpu(cpu);
3758
3759         return 0;
3760 }
3761
3762 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
3763 {
3764         static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
3765         static char trace_buf[TRACE_BUF_SIZE];
3766
3767         struct ring_buffer_event *event;
3768         struct trace_array *tr = &global_trace;
3769         struct trace_array_cpu *data;
3770         int cpu, len = 0, size, pc;
3771         struct print_entry *entry;
3772         unsigned long irq_flags;
3773
3774         if (tracing_disabled || tracing_selftest_running)
3775                 return 0;
3776
3777         pc = preempt_count();
3778         preempt_disable_notrace();
3779         cpu = raw_smp_processor_id();
3780         data = tr->data[cpu];
3781
3782         if (unlikely(atomic_read(&data->disabled)))
3783                 goto out;
3784
3785         pause_graph_tracing();
3786         raw_local_irq_save(irq_flags);
3787         __raw_spin_lock(&trace_buf_lock);
3788         len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3789
3790         len = min(len, TRACE_BUF_SIZE-1);
3791         trace_buf[len] = 0;
3792
3793         size = sizeof(*entry) + len + 1;
3794         event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
3795         if (!event)
3796                 goto out_unlock;
3797         entry = ring_buffer_event_data(event);
3798         entry->ip                       = ip;
3799         entry->depth                    = depth;
3800
3801         memcpy(&entry->buf, trace_buf, len);
3802         entry->buf[len] = 0;
3803         ring_buffer_unlock_commit(tr->buffer, event);
3804
3805  out_unlock:
3806         __raw_spin_unlock(&trace_buf_lock);
3807         raw_local_irq_restore(irq_flags);
3808         unpause_graph_tracing();
3809  out:
3810         preempt_enable_notrace();
3811
3812         return len;
3813 }
3814 EXPORT_SYMBOL_GPL(trace_vprintk);
3815
3816 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3817 {
3818         int ret;
3819         va_list ap;
3820
3821         if (!(trace_flags & TRACE_ITER_PRINTK))
3822                 return 0;
3823
3824         va_start(ap, fmt);
3825         ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3826         va_end(ap);
3827         return ret;
3828 }
3829 EXPORT_SYMBOL_GPL(__ftrace_printk);
3830
3831 int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
3832 {
3833         if (!(trace_flags & TRACE_ITER_PRINTK))
3834                 return 0;
3835
3836         return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3837 }
3838 EXPORT_SYMBOL_GPL(__ftrace_vprintk);
3839
3840 static int trace_panic_handler(struct notifier_block *this,
3841                                unsigned long event, void *unused)
3842 {
3843         if (ftrace_dump_on_oops)
3844                 ftrace_dump();
3845         return NOTIFY_OK;
3846 }
3847
3848 static struct notifier_block trace_panic_notifier = {
3849         .notifier_call  = trace_panic_handler,
3850         .next           = NULL,
3851         .priority       = 150   /* priority: INT_MAX >= x >= 0 */
3852 };
3853
3854 static int trace_die_handler(struct notifier_block *self,
3855                              unsigned long val,
3856                              void *data)
3857 {
3858         switch (val) {
3859         case DIE_OOPS:
3860                 if (ftrace_dump_on_oops)
3861                         ftrace_dump();
3862                 break;
3863         default:
3864                 break;
3865         }
3866         return NOTIFY_OK;
3867 }
3868
3869 static struct notifier_block trace_die_notifier = {
3870         .notifier_call = trace_die_handler,
3871         .priority = 200
3872 };
3873
3874 /*
3875  * printk is set to max of 1024, we really don't need it that big.
3876  * Nothing should be printing 1000 characters anyway.
3877  */
3878 #define TRACE_MAX_PRINT         1000
3879
3880 /*
3881  * Define here KERN_TRACE so that we have one place to modify
3882  * it if we decide to change what log level the ftrace dump
3883  * should be at.
3884  */
3885 #define KERN_TRACE              KERN_EMERG
3886
3887 static void
3888 trace_printk_seq(struct trace_seq *s)
3889 {
3890         /* Probably should print a warning here. */
3891         if (s->len >= 1000)
3892                 s->len = 1000;
3893
3894         /* should be zero ended, but we are paranoid. */
3895         s->buffer[s->len] = 0;
3896
3897         printk(KERN_TRACE "%s", s->buffer);
3898
3899         trace_seq_init(s);
3900 }
3901
3902 void ftrace_dump(void)
3903 {
3904         static DEFINE_SPINLOCK(ftrace_dump_lock);
3905         /* use static because iter can be a bit big for the stack */
3906         static struct trace_iterator iter;
3907         static int dump_ran;
3908         unsigned long flags;
3909         int cnt = 0, cpu;
3910
3911         /* only one dump */
3912         spin_lock_irqsave(&ftrace_dump_lock, flags);
3913         if (dump_ran)
3914                 goto out;
3915
3916         dump_ran = 1;
3917
3918         /* No turning back! */
3919         tracing_off();
3920         ftrace_kill();
3921
3922         for_each_tracing_cpu(cpu) {
3923                 atomic_inc(&global_trace.data[cpu]->disabled);
3924         }
3925
3926         /* don't look at user memory in panic mode */
3927         trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3928
3929         printk(KERN_TRACE "Dumping ftrace buffer:\n");
3930
3931         /* Simulate the iterator */
3932         iter.tr = &global_trace;
3933         iter.trace = current_trace;
3934         iter.cpu_file = TRACE_PIPE_ALL_CPU;
3935
3936         /*
3937          * We need to stop all tracing on all CPUS to read the
3938          * the next buffer. This is a bit expensive, but is
3939          * not done often. We fill all what we can read,
3940          * and then release the locks again.
3941          */
3942
3943         while (!trace_empty(&iter)) {
3944
3945                 if (!cnt)
3946                         printk(KERN_TRACE "---------------------------------\n");
3947
3948                 cnt++;
3949
3950                 /* reset all but tr, trace, and overruns */
3951                 memset(&iter.seq, 0,
3952                        sizeof(struct trace_iterator) -
3953                        offsetof(struct trace_iterator, seq));
3954                 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3955                 iter.pos = -1;
3956
3957                 if (find_next_entry_inc(&iter) != NULL) {
3958                         print_trace_line(&iter);
3959                         trace_consume(&iter);
3960                 }
3961
3962                 trace_printk_seq(&iter.seq);
3963         }
3964
3965         if (!cnt)
3966                 printk(KERN_TRACE "   (ftrace buffer empty)\n");
3967         else
3968                 printk(KERN_TRACE "---------------------------------\n");
3969
3970  out:
3971         spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3972 }
3973
3974 __init static int tracer_alloc_buffers(void)
3975 {
3976         struct trace_array_cpu *data;
3977         int i;
3978         int ret = -ENOMEM;
3979
3980         if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3981                 goto out;
3982
3983         if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3984                 goto out_free_buffer_mask;
3985
3986         if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
3987                 goto out_free_tracing_cpumask;
3988
3989         cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3990         cpumask_copy(tracing_cpumask, cpu_all_mask);
3991         cpumask_clear(tracing_reader_cpumask);
3992
3993         /* TODO: make the number of buffers hot pluggable with CPUS */
3994         global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3995                                                    TRACE_BUFFER_FLAGS);
3996         if (!global_trace.buffer) {
3997                 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3998                 WARN_ON(1);
3999                 goto out_free_cpumask;
4000         }
4001         global_trace.entries = ring_buffer_size(global_trace.buffer);
4002
4003
4004 #ifdef CONFIG_TRACER_MAX_TRACE
4005         max_tr.buffer = ring_buffer_alloc(trace_buf_size,
4006                                              TRACE_BUFFER_FLAGS);
4007         if (!max_tr.buffer) {
4008                 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4009                 WARN_ON(1);
4010                 ring_buffer_free(global_trace.buffer);
4011                 goto out_free_cpumask;
4012         }
4013         max_tr.entries = ring_buffer_size(max_tr.buffer);
4014         WARN_ON(max_tr.entries != global_trace.entries);
4015 #endif
4016
4017         /* Allocate the first page for all buffers */
4018         for_each_tracing_cpu(i) {
4019                 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4020                 max_tr.data[i] = &per_cpu(max_data, i);
4021         }
4022
4023         trace_init_cmdlines();
4024
4025         register_tracer(&nop_trace);
4026         current_trace = &nop_trace;
4027 #ifdef CONFIG_BOOT_TRACER
4028         register_tracer(&boot_tracer);
4029 #endif
4030         /* All seems OK, enable tracing */
4031         tracing_disabled = 0;
4032
4033         atomic_notifier_chain_register(&panic_notifier_list,
4034                                        &trace_panic_notifier);
4035
4036         register_die_notifier(&trace_die_notifier);
4037         ret = 0;
4038
4039 out_free_cpumask:
4040         free_cpumask_var(tracing_reader_cpumask);
4041 out_free_tracing_cpumask:
4042         free_cpumask_var(tracing_cpumask);
4043 out_free_buffer_mask:
4044         free_cpumask_var(tracing_buffer_mask);
4045 out:
4046         return ret;
4047 }
4048
4049 __init static int clear_boot_tracer(void)
4050 {
4051         /*
4052          * The default tracer at boot buffer is an init section.
4053          * This function is called in lateinit. If we did not
4054          * find the boot tracer, then clear it out, to prevent
4055          * later registration from accessing the buffer that is
4056          * about to be freed.
4057          */
4058         if (!default_bootup_tracer)
4059                 return 0;
4060
4061         printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4062                default_bootup_tracer);
4063         default_bootup_tracer = NULL;
4064
4065         return 0;
4066 }
4067
4068 early_initcall(tracer_alloc_buffers);
4069 fs_initcall(tracer_init_debugfs);
4070 late_initcall(clear_boot_tracer);