]> Pileus Git - ~andy/linux/blob - kernel/trace/trace.c
tracing: Remove an unneeded check in trace_seq_buffer()
[~andy/linux] / 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 <generated/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/string.h>
34 #include <linux/rwsem.h>
35 #include <linux/slab.h>
36 #include <linux/ctype.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
39 #include <linux/nmi.h>
40 #include <linux/fs.h>
41
42 #include "trace.h"
43 #include "trace_output.h"
44
45 /*
46  * On boot up, the ring buffer is set to the minimum size, so that
47  * we do not waste memory on systems that are not using tracing.
48  */
49 int ring_buffer_expanded;
50
51 /*
52  * We need to change this state when a selftest is running.
53  * A selftest will lurk into the ring-buffer to count the
54  * entries inserted during the selftest although some concurrent
55  * insertions into the ring-buffer such as trace_printk could occurred
56  * at the same time, giving false positive or negative results.
57  */
58 static bool __read_mostly tracing_selftest_running;
59
60 /*
61  * If a tracer is running, we do not want to run SELFTEST.
62  */
63 bool __read_mostly tracing_selftest_disabled;
64
65 /* For tracers that don't implement custom flags */
66 static struct tracer_opt dummy_tracer_opt[] = {
67         { }
68 };
69
70 static struct tracer_flags dummy_tracer_flags = {
71         .val = 0,
72         .opts = dummy_tracer_opt
73 };
74
75 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
76 {
77         return 0;
78 }
79
80 /*
81  * Kill all tracing for good (never come back).
82  * It is initialized to 1 but will turn to zero if the initialization
83  * of the tracer is successful. But that is the only place that sets
84  * this back to zero.
85  */
86 static int tracing_disabled = 1;
87
88 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
89
90 static inline void ftrace_disable_cpu(void)
91 {
92         preempt_disable();
93         __this_cpu_inc(ftrace_cpu_disabled);
94 }
95
96 static inline void ftrace_enable_cpu(void)
97 {
98         __this_cpu_dec(ftrace_cpu_disabled);
99         preempt_enable();
100 }
101
102 cpumask_var_t __read_mostly     tracing_buffer_mask;
103
104 /*
105  * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
106  *
107  * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
108  * is set, then ftrace_dump is called. This will output the contents
109  * of the ftrace buffers to the console.  This is very useful for
110  * capturing traces that lead to crashes and outputing it to a
111  * serial console.
112  *
113  * It is default off, but you can enable it with either specifying
114  * "ftrace_dump_on_oops" in the kernel command line, or setting
115  * /proc/sys/kernel/ftrace_dump_on_oops
116  * Set 1 if you want to dump buffers of all CPUs
117  * Set 2 if you want to dump the buffer of the CPU that triggered oops
118  */
119
120 enum ftrace_dump_mode ftrace_dump_on_oops;
121
122 static int tracing_set_tracer(const char *buf);
123
124 #define MAX_TRACER_SIZE         100
125 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
126 static char *default_bootup_tracer;
127
128 static int __init set_cmdline_ftrace(char *str)
129 {
130         strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
131         default_bootup_tracer = bootup_tracer_buf;
132         /* We are using ftrace early, expand it */
133         ring_buffer_expanded = 1;
134         return 1;
135 }
136 __setup("ftrace=", set_cmdline_ftrace);
137
138 static int __init set_ftrace_dump_on_oops(char *str)
139 {
140         if (*str++ != '=' || !*str) {
141                 ftrace_dump_on_oops = DUMP_ALL;
142                 return 1;
143         }
144
145         if (!strcmp("orig_cpu", str)) {
146                 ftrace_dump_on_oops = DUMP_ORIG;
147                 return 1;
148         }
149
150         return 0;
151 }
152 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
153
154 unsigned long long ns2usecs(cycle_t nsec)
155 {
156         nsec += 500;
157         do_div(nsec, 1000);
158         return nsec;
159 }
160
161 /*
162  * The global_trace is the descriptor that holds the tracing
163  * buffers for the live tracing. For each CPU, it contains
164  * a link list of pages that will store trace entries. The
165  * page descriptor of the pages in the memory is used to hold
166  * the link list by linking the lru item in the page descriptor
167  * to each of the pages in the buffer per CPU.
168  *
169  * For each active CPU there is a data field that holds the
170  * pages for the buffer for that CPU. Each CPU has the same number
171  * of pages allocated for its buffer.
172  */
173 static struct trace_array       global_trace;
174
175 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
176
177 int filter_current_check_discard(struct ring_buffer *buffer,
178                                  struct ftrace_event_call *call, void *rec,
179                                  struct ring_buffer_event *event)
180 {
181         return filter_check_discard(call, rec, buffer, event);
182 }
183 EXPORT_SYMBOL_GPL(filter_current_check_discard);
184
185 cycle_t ftrace_now(int cpu)
186 {
187         u64 ts;
188
189         /* Early boot up does not have a buffer yet */
190         if (!global_trace.buffer)
191                 return trace_clock_local();
192
193         ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
194         ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
195
196         return ts;
197 }
198
199 /*
200  * The max_tr is used to snapshot the global_trace when a maximum
201  * latency is reached. Some tracers will use this to store a maximum
202  * trace while it continues examining live traces.
203  *
204  * The buffers for the max_tr are set up the same as the global_trace.
205  * When a snapshot is taken, the link list of the max_tr is swapped
206  * with the link list of the global_trace and the buffers are reset for
207  * the global_trace so the tracing can continue.
208  */
209 static struct trace_array       max_tr;
210
211 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
212
213 /* tracer_enabled is used to toggle activation of a tracer */
214 static int                      tracer_enabled = 1;
215
216 /**
217  * tracing_is_enabled - return tracer_enabled status
218  *
219  * This function is used by other tracers to know the status
220  * of the tracer_enabled flag.  Tracers may use this function
221  * to know if it should enable their features when starting
222  * up. See irqsoff tracer for an example (start_irqsoff_tracer).
223  */
224 int tracing_is_enabled(void)
225 {
226         return tracer_enabled;
227 }
228
229 /*
230  * trace_buf_size is the size in bytes that is allocated
231  * for a buffer. Note, the number of bytes is always rounded
232  * to page size.
233  *
234  * This number is purposely set to a low number of 16384.
235  * If the dump on oops happens, it will be much appreciated
236  * to not have to wait for all that output. Anyway this can be
237  * boot time and run time configurable.
238  */
239 #define TRACE_BUF_SIZE_DEFAULT  1441792UL /* 16384 * 88 (sizeof(entry)) */
240
241 static unsigned long            trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
242
243 /* trace_types holds a link list of available tracers. */
244 static struct tracer            *trace_types __read_mostly;
245
246 /* current_trace points to the tracer that is currently active */
247 static struct tracer            *current_trace __read_mostly;
248
249 /*
250  * trace_types_lock is used to protect the trace_types list.
251  */
252 static DEFINE_MUTEX(trace_types_lock);
253
254 /*
255  * serialize the access of the ring buffer
256  *
257  * ring buffer serializes readers, but it is low level protection.
258  * The validity of the events (which returns by ring_buffer_peek() ..etc)
259  * are not protected by ring buffer.
260  *
261  * The content of events may become garbage if we allow other process consumes
262  * these events concurrently:
263  *   A) the page of the consumed events may become a normal page
264  *      (not reader page) in ring buffer, and this page will be rewrited
265  *      by events producer.
266  *   B) The page of the consumed events may become a page for splice_read,
267  *      and this page will be returned to system.
268  *
269  * These primitives allow multi process access to different cpu ring buffer
270  * concurrently.
271  *
272  * These primitives don't distinguish read-only and read-consume access.
273  * Multi read-only access are also serialized.
274  */
275
276 #ifdef CONFIG_SMP
277 static DECLARE_RWSEM(all_cpu_access_lock);
278 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
279
280 static inline void trace_access_lock(int cpu)
281 {
282         if (cpu == TRACE_PIPE_ALL_CPU) {
283                 /* gain it for accessing the whole ring buffer. */
284                 down_write(&all_cpu_access_lock);
285         } else {
286                 /* gain it for accessing a cpu ring buffer. */
287
288                 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
289                 down_read(&all_cpu_access_lock);
290
291                 /* Secondly block other access to this @cpu ring buffer. */
292                 mutex_lock(&per_cpu(cpu_access_lock, cpu));
293         }
294 }
295
296 static inline void trace_access_unlock(int cpu)
297 {
298         if (cpu == TRACE_PIPE_ALL_CPU) {
299                 up_write(&all_cpu_access_lock);
300         } else {
301                 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
302                 up_read(&all_cpu_access_lock);
303         }
304 }
305
306 static inline void trace_access_lock_init(void)
307 {
308         int cpu;
309
310         for_each_possible_cpu(cpu)
311                 mutex_init(&per_cpu(cpu_access_lock, cpu));
312 }
313
314 #else
315
316 static DEFINE_MUTEX(access_lock);
317
318 static inline void trace_access_lock(int cpu)
319 {
320         (void)cpu;
321         mutex_lock(&access_lock);
322 }
323
324 static inline void trace_access_unlock(int cpu)
325 {
326         (void)cpu;
327         mutex_unlock(&access_lock);
328 }
329
330 static inline void trace_access_lock_init(void)
331 {
332 }
333
334 #endif
335
336 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
337 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
338
339 /* trace_flags holds trace_options default values */
340 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
341         TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
342         TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
343         TRACE_ITER_IRQ_INFO;
344
345 static int trace_stop_count;
346 static DEFINE_RAW_SPINLOCK(tracing_start_lock);
347
348 static void wakeup_work_handler(struct work_struct *work)
349 {
350         wake_up(&trace_wait);
351 }
352
353 static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler);
354
355 /**
356  * tracing_on - enable tracing buffers
357  *
358  * This function enables tracing buffers that may have been
359  * disabled with tracing_off.
360  */
361 void tracing_on(void)
362 {
363         if (global_trace.buffer)
364                 ring_buffer_record_on(global_trace.buffer);
365         /*
366          * This flag is only looked at when buffers haven't been
367          * allocated yet. We don't really care about the race
368          * between setting this flag and actually turning
369          * on the buffer.
370          */
371         global_trace.buffer_disabled = 0;
372 }
373 EXPORT_SYMBOL_GPL(tracing_on);
374
375 /**
376  * tracing_off - turn off tracing buffers
377  *
378  * This function stops the tracing buffers from recording data.
379  * It does not disable any overhead the tracers themselves may
380  * be causing. This function simply causes all recording to
381  * the ring buffers to fail.
382  */
383 void tracing_off(void)
384 {
385         if (global_trace.buffer)
386                 ring_buffer_record_on(global_trace.buffer);
387         /*
388          * This flag is only looked at when buffers haven't been
389          * allocated yet. We don't really care about the race
390          * between setting this flag and actually turning
391          * on the buffer.
392          */
393         global_trace.buffer_disabled = 1;
394 }
395 EXPORT_SYMBOL_GPL(tracing_off);
396
397 /**
398  * tracing_is_on - show state of ring buffers enabled
399  */
400 int tracing_is_on(void)
401 {
402         if (global_trace.buffer)
403                 return ring_buffer_record_is_on(global_trace.buffer);
404         return !global_trace.buffer_disabled;
405 }
406 EXPORT_SYMBOL_GPL(tracing_is_on);
407
408 /**
409  * trace_wake_up - wake up tasks waiting for trace input
410  *
411  * Schedules a delayed work to wake up any task that is blocked on the
412  * trace_wait queue. These is used with trace_poll for tasks polling the
413  * trace.
414  */
415 void trace_wake_up(void)
416 {
417         const unsigned long delay = msecs_to_jiffies(2);
418
419         if (trace_flags & TRACE_ITER_BLOCK)
420                 return;
421         schedule_delayed_work(&wakeup_work, delay);
422 }
423
424 static int __init set_buf_size(char *str)
425 {
426         unsigned long buf_size;
427
428         if (!str)
429                 return 0;
430         buf_size = memparse(str, &str);
431         /* nr_entries can not be zero */
432         if (buf_size == 0)
433                 return 0;
434         trace_buf_size = buf_size;
435         return 1;
436 }
437 __setup("trace_buf_size=", set_buf_size);
438
439 static int __init set_tracing_thresh(char *str)
440 {
441         unsigned long threshhold;
442         int ret;
443
444         if (!str)
445                 return 0;
446         ret = strict_strtoul(str, 0, &threshhold);
447         if (ret < 0)
448                 return 0;
449         tracing_thresh = threshhold * 1000;
450         return 1;
451 }
452 __setup("tracing_thresh=", set_tracing_thresh);
453
454 unsigned long nsecs_to_usecs(unsigned long nsecs)
455 {
456         return nsecs / 1000;
457 }
458
459 /* These must match the bit postions in trace_iterator_flags */
460 static const char *trace_options[] = {
461         "print-parent",
462         "sym-offset",
463         "sym-addr",
464         "verbose",
465         "raw",
466         "hex",
467         "bin",
468         "block",
469         "stacktrace",
470         "trace_printk",
471         "ftrace_preempt",
472         "branch",
473         "annotate",
474         "userstacktrace",
475         "sym-userobj",
476         "printk-msg-only",
477         "context-info",
478         "latency-format",
479         "sleep-time",
480         "graph-time",
481         "record-cmd",
482         "overwrite",
483         "disable_on_free",
484         "irq-info",
485         NULL
486 };
487
488 static struct {
489         u64 (*func)(void);
490         const char *name;
491 } trace_clocks[] = {
492         { trace_clock_local,    "local" },
493         { trace_clock_global,   "global" },
494         { trace_clock_counter,  "counter" },
495 };
496
497 int trace_clock_id;
498
499 /*
500  * trace_parser_get_init - gets the buffer for trace parser
501  */
502 int trace_parser_get_init(struct trace_parser *parser, int size)
503 {
504         memset(parser, 0, sizeof(*parser));
505
506         parser->buffer = kmalloc(size, GFP_KERNEL);
507         if (!parser->buffer)
508                 return 1;
509
510         parser->size = size;
511         return 0;
512 }
513
514 /*
515  * trace_parser_put - frees the buffer for trace parser
516  */
517 void trace_parser_put(struct trace_parser *parser)
518 {
519         kfree(parser->buffer);
520 }
521
522 /*
523  * trace_get_user - reads the user input string separated by  space
524  * (matched by isspace(ch))
525  *
526  * For each string found the 'struct trace_parser' is updated,
527  * and the function returns.
528  *
529  * Returns number of bytes read.
530  *
531  * See kernel/trace/trace.h for 'struct trace_parser' details.
532  */
533 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
534         size_t cnt, loff_t *ppos)
535 {
536         char ch;
537         size_t read = 0;
538         ssize_t ret;
539
540         if (!*ppos)
541                 trace_parser_clear(parser);
542
543         ret = get_user(ch, ubuf++);
544         if (ret)
545                 goto out;
546
547         read++;
548         cnt--;
549
550         /*
551          * The parser is not finished with the last write,
552          * continue reading the user input without skipping spaces.
553          */
554         if (!parser->cont) {
555                 /* skip white space */
556                 while (cnt && isspace(ch)) {
557                         ret = get_user(ch, ubuf++);
558                         if (ret)
559                                 goto out;
560                         read++;
561                         cnt--;
562                 }
563
564                 /* only spaces were written */
565                 if (isspace(ch)) {
566                         *ppos += read;
567                         ret = read;
568                         goto out;
569                 }
570
571                 parser->idx = 0;
572         }
573
574         /* read the non-space input */
575         while (cnt && !isspace(ch)) {
576                 if (parser->idx < parser->size - 1)
577                         parser->buffer[parser->idx++] = ch;
578                 else {
579                         ret = -EINVAL;
580                         goto out;
581                 }
582                 ret = get_user(ch, ubuf++);
583                 if (ret)
584                         goto out;
585                 read++;
586                 cnt--;
587         }
588
589         /* We either got finished input or we have to wait for another call. */
590         if (isspace(ch)) {
591                 parser->buffer[parser->idx] = 0;
592                 parser->cont = false;
593         } else {
594                 parser->cont = true;
595                 parser->buffer[parser->idx++] = ch;
596         }
597
598         *ppos += read;
599         ret = read;
600
601 out:
602         return ret;
603 }
604
605 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
606 {
607         int len;
608         int ret;
609
610         if (!cnt)
611                 return 0;
612
613         if (s->len <= s->readpos)
614                 return -EBUSY;
615
616         len = s->len - s->readpos;
617         if (cnt > len)
618                 cnt = len;
619         ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
620         if (ret == cnt)
621                 return -EFAULT;
622
623         cnt -= ret;
624
625         s->readpos += cnt;
626         return cnt;
627 }
628
629 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
630 {
631         int len;
632
633         if (s->len <= s->readpos)
634                 return -EBUSY;
635
636         len = s->len - s->readpos;
637         if (cnt > len)
638                 cnt = len;
639         memcpy(buf, s->buffer + s->readpos, cnt);
640
641         s->readpos += cnt;
642         return cnt;
643 }
644
645 /*
646  * ftrace_max_lock is used to protect the swapping of buffers
647  * when taking a max snapshot. The buffers themselves are
648  * protected by per_cpu spinlocks. But the action of the swap
649  * needs its own lock.
650  *
651  * This is defined as a arch_spinlock_t in order to help
652  * with performance when lockdep debugging is enabled.
653  *
654  * It is also used in other places outside the update_max_tr
655  * so it needs to be defined outside of the
656  * CONFIG_TRACER_MAX_TRACE.
657  */
658 static arch_spinlock_t ftrace_max_lock =
659         (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
660
661 unsigned long __read_mostly     tracing_thresh;
662
663 #ifdef CONFIG_TRACER_MAX_TRACE
664 unsigned long __read_mostly     tracing_max_latency;
665
666 /*
667  * Copy the new maximum trace into the separate maximum-trace
668  * structure. (this way the maximum trace is permanently saved,
669  * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
670  */
671 static void
672 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
673 {
674         struct trace_array_cpu *data = tr->data[cpu];
675         struct trace_array_cpu *max_data;
676
677         max_tr.cpu = cpu;
678         max_tr.time_start = data->preempt_timestamp;
679
680         max_data = max_tr.data[cpu];
681         max_data->saved_latency = tracing_max_latency;
682         max_data->critical_start = data->critical_start;
683         max_data->critical_end = data->critical_end;
684
685         memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
686         max_data->pid = tsk->pid;
687         max_data->uid = task_uid(tsk);
688         max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
689         max_data->policy = tsk->policy;
690         max_data->rt_priority = tsk->rt_priority;
691
692         /* record this tasks comm */
693         tracing_record_cmdline(tsk);
694 }
695
696 /**
697  * update_max_tr - snapshot all trace buffers from global_trace to max_tr
698  * @tr: tracer
699  * @tsk: the task with the latency
700  * @cpu: The cpu that initiated the trace.
701  *
702  * Flip the buffers between the @tr and the max_tr and record information
703  * about which task was the cause of this latency.
704  */
705 void
706 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
707 {
708         struct ring_buffer *buf = tr->buffer;
709
710         if (trace_stop_count)
711                 return;
712
713         WARN_ON_ONCE(!irqs_disabled());
714         if (!current_trace->use_max_tr) {
715                 WARN_ON_ONCE(1);
716                 return;
717         }
718         arch_spin_lock(&ftrace_max_lock);
719
720         tr->buffer = max_tr.buffer;
721         max_tr.buffer = buf;
722
723         __update_max_tr(tr, tsk, cpu);
724         arch_spin_unlock(&ftrace_max_lock);
725 }
726
727 /**
728  * update_max_tr_single - only copy one trace over, and reset the rest
729  * @tr - tracer
730  * @tsk - task with the latency
731  * @cpu - the cpu of the buffer to copy.
732  *
733  * Flip the trace of a single CPU buffer between the @tr and the max_tr.
734  */
735 void
736 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
737 {
738         int ret;
739
740         if (trace_stop_count)
741                 return;
742
743         WARN_ON_ONCE(!irqs_disabled());
744         if (!current_trace->use_max_tr) {
745                 WARN_ON_ONCE(1);
746                 return;
747         }
748
749         arch_spin_lock(&ftrace_max_lock);
750
751         ftrace_disable_cpu();
752
753         ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
754
755         if (ret == -EBUSY) {
756                 /*
757                  * We failed to swap the buffer due to a commit taking
758                  * place on this CPU. We fail to record, but we reset
759                  * the max trace buffer (no one writes directly to it)
760                  * and flag that it failed.
761                  */
762                 trace_array_printk(&max_tr, _THIS_IP_,
763                         "Failed to swap buffers due to commit in progress\n");
764         }
765
766         ftrace_enable_cpu();
767
768         WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
769
770         __update_max_tr(tr, tsk, cpu);
771         arch_spin_unlock(&ftrace_max_lock);
772 }
773 #endif /* CONFIG_TRACER_MAX_TRACE */
774
775 /**
776  * register_tracer - register a tracer with the ftrace system.
777  * @type - the plugin for the tracer
778  *
779  * Register a new plugin tracer.
780  */
781 int register_tracer(struct tracer *type)
782 __releases(kernel_lock)
783 __acquires(kernel_lock)
784 {
785         struct tracer *t;
786         int ret = 0;
787
788         if (!type->name) {
789                 pr_info("Tracer must have a name\n");
790                 return -1;
791         }
792
793         if (strlen(type->name) >= MAX_TRACER_SIZE) {
794                 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
795                 return -1;
796         }
797
798         mutex_lock(&trace_types_lock);
799
800         tracing_selftest_running = true;
801
802         for (t = trace_types; t; t = t->next) {
803                 if (strcmp(type->name, t->name) == 0) {
804                         /* already found */
805                         pr_info("Tracer %s already registered\n",
806                                 type->name);
807                         ret = -1;
808                         goto out;
809                 }
810         }
811
812         if (!type->set_flag)
813                 type->set_flag = &dummy_set_flag;
814         if (!type->flags)
815                 type->flags = &dummy_tracer_flags;
816         else
817                 if (!type->flags->opts)
818                         type->flags->opts = dummy_tracer_opt;
819         if (!type->wait_pipe)
820                 type->wait_pipe = default_wait_pipe;
821
822
823 #ifdef CONFIG_FTRACE_STARTUP_TEST
824         if (type->selftest && !tracing_selftest_disabled) {
825                 struct tracer *saved_tracer = current_trace;
826                 struct trace_array *tr = &global_trace;
827
828                 /*
829                  * Run a selftest on this tracer.
830                  * Here we reset the trace buffer, and set the current
831                  * tracer to be this tracer. The tracer can then run some
832                  * internal tracing to verify that everything is in order.
833                  * If we fail, we do not register this tracer.
834                  */
835                 tracing_reset_online_cpus(tr);
836
837                 current_trace = type;
838
839                 /* If we expanded the buffers, make sure the max is expanded too */
840                 if (ring_buffer_expanded && type->use_max_tr)
841                         ring_buffer_resize(max_tr.buffer, trace_buf_size);
842
843                 /* the test is responsible for initializing and enabling */
844                 pr_info("Testing tracer %s: ", type->name);
845                 ret = type->selftest(type, tr);
846                 /* the test is responsible for resetting too */
847                 current_trace = saved_tracer;
848                 if (ret) {
849                         printk(KERN_CONT "FAILED!\n");
850                         goto out;
851                 }
852                 /* Only reset on passing, to avoid touching corrupted buffers */
853                 tracing_reset_online_cpus(tr);
854
855                 /* Shrink the max buffer again */
856                 if (ring_buffer_expanded && type->use_max_tr)
857                         ring_buffer_resize(max_tr.buffer, 1);
858
859                 printk(KERN_CONT "PASSED\n");
860         }
861 #endif
862
863         type->next = trace_types;
864         trace_types = type;
865
866  out:
867         tracing_selftest_running = false;
868         mutex_unlock(&trace_types_lock);
869
870         if (ret || !default_bootup_tracer)
871                 goto out_unlock;
872
873         if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
874                 goto out_unlock;
875
876         printk(KERN_INFO "Starting tracer '%s'\n", type->name);
877         /* Do we want this tracer to start on bootup? */
878         tracing_set_tracer(type->name);
879         default_bootup_tracer = NULL;
880         /* disable other selftests, since this will break it. */
881         tracing_selftest_disabled = 1;
882 #ifdef CONFIG_FTRACE_STARTUP_TEST
883         printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
884                type->name);
885 #endif
886
887  out_unlock:
888         return ret;
889 }
890
891 void unregister_tracer(struct tracer *type)
892 {
893         struct tracer **t;
894
895         mutex_lock(&trace_types_lock);
896         for (t = &trace_types; *t; t = &(*t)->next) {
897                 if (*t == type)
898                         goto found;
899         }
900         pr_info("Tracer %s not registered\n", type->name);
901         goto out;
902
903  found:
904         *t = (*t)->next;
905
906         if (type == current_trace && tracer_enabled) {
907                 tracer_enabled = 0;
908                 tracing_stop();
909                 if (current_trace->stop)
910                         current_trace->stop(&global_trace);
911                 current_trace = &nop_trace;
912         }
913 out:
914         mutex_unlock(&trace_types_lock);
915 }
916
917 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
918 {
919         ftrace_disable_cpu();
920         ring_buffer_reset_cpu(buffer, cpu);
921         ftrace_enable_cpu();
922 }
923
924 void tracing_reset(struct trace_array *tr, int cpu)
925 {
926         struct ring_buffer *buffer = tr->buffer;
927
928         ring_buffer_record_disable(buffer);
929
930         /* Make sure all commits have finished */
931         synchronize_sched();
932         __tracing_reset(buffer, cpu);
933
934         ring_buffer_record_enable(buffer);
935 }
936
937 void tracing_reset_online_cpus(struct trace_array *tr)
938 {
939         struct ring_buffer *buffer = tr->buffer;
940         int cpu;
941
942         ring_buffer_record_disable(buffer);
943
944         /* Make sure all commits have finished */
945         synchronize_sched();
946
947         tr->time_start = ftrace_now(tr->cpu);
948
949         for_each_online_cpu(cpu)
950                 __tracing_reset(buffer, cpu);
951
952         ring_buffer_record_enable(buffer);
953 }
954
955 void tracing_reset_current(int cpu)
956 {
957         tracing_reset(&global_trace, cpu);
958 }
959
960 void tracing_reset_current_online_cpus(void)
961 {
962         tracing_reset_online_cpus(&global_trace);
963 }
964
965 #define SAVED_CMDLINES 128
966 #define NO_CMDLINE_MAP UINT_MAX
967 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
968 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
969 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
970 static int cmdline_idx;
971 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
972
973 /* temporary disable recording */
974 static atomic_t trace_record_cmdline_disabled __read_mostly;
975
976 static void trace_init_cmdlines(void)
977 {
978         memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
979         memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
980         cmdline_idx = 0;
981 }
982
983 int is_tracing_stopped(void)
984 {
985         return trace_stop_count;
986 }
987
988 /**
989  * ftrace_off_permanent - disable all ftrace code permanently
990  *
991  * This should only be called when a serious anomally has
992  * been detected.  This will turn off the function tracing,
993  * ring buffers, and other tracing utilites. It takes no
994  * locks and can be called from any context.
995  */
996 void ftrace_off_permanent(void)
997 {
998         tracing_disabled = 1;
999         ftrace_stop();
1000         tracing_off_permanent();
1001 }
1002
1003 /**
1004  * tracing_start - quick start of the tracer
1005  *
1006  * If tracing is enabled but was stopped by tracing_stop,
1007  * this will start the tracer back up.
1008  */
1009 void tracing_start(void)
1010 {
1011         struct ring_buffer *buffer;
1012         unsigned long flags;
1013
1014         if (tracing_disabled)
1015                 return;
1016
1017         raw_spin_lock_irqsave(&tracing_start_lock, flags);
1018         if (--trace_stop_count) {
1019                 if (trace_stop_count < 0) {
1020                         /* Someone screwed up their debugging */
1021                         WARN_ON_ONCE(1);
1022                         trace_stop_count = 0;
1023                 }
1024                 goto out;
1025         }
1026
1027         /* Prevent the buffers from switching */
1028         arch_spin_lock(&ftrace_max_lock);
1029
1030         buffer = global_trace.buffer;
1031         if (buffer)
1032                 ring_buffer_record_enable(buffer);
1033
1034         buffer = max_tr.buffer;
1035         if (buffer)
1036                 ring_buffer_record_enable(buffer);
1037
1038         arch_spin_unlock(&ftrace_max_lock);
1039
1040         ftrace_start();
1041  out:
1042         raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1043 }
1044
1045 /**
1046  * tracing_stop - quick stop of the tracer
1047  *
1048  * Light weight way to stop tracing. Use in conjunction with
1049  * tracing_start.
1050  */
1051 void tracing_stop(void)
1052 {
1053         struct ring_buffer *buffer;
1054         unsigned long flags;
1055
1056         ftrace_stop();
1057         raw_spin_lock_irqsave(&tracing_start_lock, flags);
1058         if (trace_stop_count++)
1059                 goto out;
1060
1061         /* Prevent the buffers from switching */
1062         arch_spin_lock(&ftrace_max_lock);
1063
1064         buffer = global_trace.buffer;
1065         if (buffer)
1066                 ring_buffer_record_disable(buffer);
1067
1068         buffer = max_tr.buffer;
1069         if (buffer)
1070                 ring_buffer_record_disable(buffer);
1071
1072         arch_spin_unlock(&ftrace_max_lock);
1073
1074  out:
1075         raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1076 }
1077
1078 void trace_stop_cmdline_recording(void);
1079
1080 static void trace_save_cmdline(struct task_struct *tsk)
1081 {
1082         unsigned pid, idx;
1083
1084         if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1085                 return;
1086
1087         /*
1088          * It's not the end of the world if we don't get
1089          * the lock, but we also don't want to spin
1090          * nor do we want to disable interrupts,
1091          * so if we miss here, then better luck next time.
1092          */
1093         if (!arch_spin_trylock(&trace_cmdline_lock))
1094                 return;
1095
1096         idx = map_pid_to_cmdline[tsk->pid];
1097         if (idx == NO_CMDLINE_MAP) {
1098                 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1099
1100                 /*
1101                  * Check whether the cmdline buffer at idx has a pid
1102                  * mapped. We are going to overwrite that entry so we
1103                  * need to clear the map_pid_to_cmdline. Otherwise we
1104                  * would read the new comm for the old pid.
1105                  */
1106                 pid = map_cmdline_to_pid[idx];
1107                 if (pid != NO_CMDLINE_MAP)
1108                         map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1109
1110                 map_cmdline_to_pid[idx] = tsk->pid;
1111                 map_pid_to_cmdline[tsk->pid] = idx;
1112
1113                 cmdline_idx = idx;
1114         }
1115
1116         memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1117
1118         arch_spin_unlock(&trace_cmdline_lock);
1119 }
1120
1121 void trace_find_cmdline(int pid, char comm[])
1122 {
1123         unsigned map;
1124
1125         if (!pid) {
1126                 strcpy(comm, "<idle>");
1127                 return;
1128         }
1129
1130         if (WARN_ON_ONCE(pid < 0)) {
1131                 strcpy(comm, "<XXX>");
1132                 return;
1133         }
1134
1135         if (pid > PID_MAX_DEFAULT) {
1136                 strcpy(comm, "<...>");
1137                 return;
1138         }
1139
1140         preempt_disable();
1141         arch_spin_lock(&trace_cmdline_lock);
1142         map = map_pid_to_cmdline[pid];
1143         if (map != NO_CMDLINE_MAP)
1144                 strcpy(comm, saved_cmdlines[map]);
1145         else
1146                 strcpy(comm, "<...>");
1147
1148         arch_spin_unlock(&trace_cmdline_lock);
1149         preempt_enable();
1150 }
1151
1152 void tracing_record_cmdline(struct task_struct *tsk)
1153 {
1154         if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1155             !tracing_is_on())
1156                 return;
1157
1158         trace_save_cmdline(tsk);
1159 }
1160
1161 void
1162 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1163                              int pc)
1164 {
1165         struct task_struct *tsk = current;
1166
1167         entry->preempt_count            = pc & 0xff;
1168         entry->pid                      = (tsk) ? tsk->pid : 0;
1169         entry->padding                  = 0;
1170         entry->flags =
1171 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1172                 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1173 #else
1174                 TRACE_FLAG_IRQS_NOSUPPORT |
1175 #endif
1176                 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1177                 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1178                 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1179 }
1180 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1181
1182 struct ring_buffer_event *
1183 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1184                           int type,
1185                           unsigned long len,
1186                           unsigned long flags, int pc)
1187 {
1188         struct ring_buffer_event *event;
1189
1190         event = ring_buffer_lock_reserve(buffer, len);
1191         if (event != NULL) {
1192                 struct trace_entry *ent = ring_buffer_event_data(event);
1193
1194                 tracing_generic_entry_update(ent, flags, pc);
1195                 ent->type = type;
1196         }
1197
1198         return event;
1199 }
1200
1201 static inline void
1202 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1203                              struct ring_buffer_event *event,
1204                              unsigned long flags, int pc,
1205                              int wake)
1206 {
1207         ring_buffer_unlock_commit(buffer, event);
1208
1209         ftrace_trace_stack(buffer, flags, 6, pc);
1210         ftrace_trace_userstack(buffer, flags, pc);
1211
1212         if (wake)
1213                 trace_wake_up();
1214 }
1215
1216 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1217                                 struct ring_buffer_event *event,
1218                                 unsigned long flags, int pc)
1219 {
1220         __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1221 }
1222
1223 struct ring_buffer_event *
1224 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1225                                   int type, unsigned long len,
1226                                   unsigned long flags, int pc)
1227 {
1228         *current_rb = global_trace.buffer;
1229         return trace_buffer_lock_reserve(*current_rb,
1230                                          type, len, flags, pc);
1231 }
1232 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1233
1234 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1235                                         struct ring_buffer_event *event,
1236                                         unsigned long flags, int pc)
1237 {
1238         __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1239 }
1240 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1241
1242 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1243                                        struct ring_buffer_event *event,
1244                                        unsigned long flags, int pc)
1245 {
1246         __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1247 }
1248 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1249
1250 void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1251                                             struct ring_buffer_event *event,
1252                                             unsigned long flags, int pc,
1253                                             struct pt_regs *regs)
1254 {
1255         ring_buffer_unlock_commit(buffer, event);
1256
1257         ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1258         ftrace_trace_userstack(buffer, flags, pc);
1259 }
1260 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs);
1261
1262 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1263                                          struct ring_buffer_event *event)
1264 {
1265         ring_buffer_discard_commit(buffer, event);
1266 }
1267 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1268
1269 void
1270 trace_function(struct trace_array *tr,
1271                unsigned long ip, unsigned long parent_ip, unsigned long flags,
1272                int pc)
1273 {
1274         struct ftrace_event_call *call = &event_function;
1275         struct ring_buffer *buffer = tr->buffer;
1276         struct ring_buffer_event *event;
1277         struct ftrace_entry *entry;
1278
1279         /* If we are reading the ring buffer, don't trace */
1280         if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1281                 return;
1282
1283         event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1284                                           flags, pc);
1285         if (!event)
1286                 return;
1287         entry   = ring_buffer_event_data(event);
1288         entry->ip                       = ip;
1289         entry->parent_ip                = parent_ip;
1290
1291         if (!filter_check_discard(call, entry, buffer, event))
1292                 ring_buffer_unlock_commit(buffer, event);
1293 }
1294
1295 void
1296 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1297        unsigned long ip, unsigned long parent_ip, unsigned long flags,
1298        int pc)
1299 {
1300         if (likely(!atomic_read(&data->disabled)))
1301                 trace_function(tr, ip, parent_ip, flags, pc);
1302 }
1303
1304 #ifdef CONFIG_STACKTRACE
1305
1306 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1307 struct ftrace_stack {
1308         unsigned long           calls[FTRACE_STACK_MAX_ENTRIES];
1309 };
1310
1311 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1312 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1313
1314 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1315                                  unsigned long flags,
1316                                  int skip, int pc, struct pt_regs *regs)
1317 {
1318         struct ftrace_event_call *call = &event_kernel_stack;
1319         struct ring_buffer_event *event;
1320         struct stack_entry *entry;
1321         struct stack_trace trace;
1322         int use_stack;
1323         int size = FTRACE_STACK_ENTRIES;
1324
1325         trace.nr_entries        = 0;
1326         trace.skip              = skip;
1327
1328         /*
1329          * Since events can happen in NMIs there's no safe way to
1330          * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1331          * or NMI comes in, it will just have to use the default
1332          * FTRACE_STACK_SIZE.
1333          */
1334         preempt_disable_notrace();
1335
1336         use_stack = ++__get_cpu_var(ftrace_stack_reserve);
1337         /*
1338          * We don't need any atomic variables, just a barrier.
1339          * If an interrupt comes in, we don't care, because it would
1340          * have exited and put the counter back to what we want.
1341          * We just need a barrier to keep gcc from moving things
1342          * around.
1343          */
1344         barrier();
1345         if (use_stack == 1) {
1346                 trace.entries           = &__get_cpu_var(ftrace_stack).calls[0];
1347                 trace.max_entries       = FTRACE_STACK_MAX_ENTRIES;
1348
1349                 if (regs)
1350                         save_stack_trace_regs(regs, &trace);
1351                 else
1352                         save_stack_trace(&trace);
1353
1354                 if (trace.nr_entries > size)
1355                         size = trace.nr_entries;
1356         } else
1357                 /* From now on, use_stack is a boolean */
1358                 use_stack = 0;
1359
1360         size *= sizeof(unsigned long);
1361
1362         event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1363                                           sizeof(*entry) + size, flags, pc);
1364         if (!event)
1365                 goto out;
1366         entry = ring_buffer_event_data(event);
1367
1368         memset(&entry->caller, 0, size);
1369
1370         if (use_stack)
1371                 memcpy(&entry->caller, trace.entries,
1372                        trace.nr_entries * sizeof(unsigned long));
1373         else {
1374                 trace.max_entries       = FTRACE_STACK_ENTRIES;
1375                 trace.entries           = entry->caller;
1376                 if (regs)
1377                         save_stack_trace_regs(regs, &trace);
1378                 else
1379                         save_stack_trace(&trace);
1380         }
1381
1382         entry->size = trace.nr_entries;
1383
1384         if (!filter_check_discard(call, entry, buffer, event))
1385                 ring_buffer_unlock_commit(buffer, event);
1386
1387  out:
1388         /* Again, don't let gcc optimize things here */
1389         barrier();
1390         __get_cpu_var(ftrace_stack_reserve)--;
1391         preempt_enable_notrace();
1392
1393 }
1394
1395 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1396                              int skip, int pc, struct pt_regs *regs)
1397 {
1398         if (!(trace_flags & TRACE_ITER_STACKTRACE))
1399                 return;
1400
1401         __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1402 }
1403
1404 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1405                         int skip, int pc)
1406 {
1407         if (!(trace_flags & TRACE_ITER_STACKTRACE))
1408                 return;
1409
1410         __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1411 }
1412
1413 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1414                    int pc)
1415 {
1416         __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
1417 }
1418
1419 /**
1420  * trace_dump_stack - record a stack back trace in the trace buffer
1421  */
1422 void trace_dump_stack(void)
1423 {
1424         unsigned long flags;
1425
1426         if (tracing_disabled || tracing_selftest_running)
1427                 return;
1428
1429         local_save_flags(flags);
1430
1431         /* skipping 3 traces, seems to get us at the caller of this function */
1432         __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
1433 }
1434
1435 static DEFINE_PER_CPU(int, user_stack_count);
1436
1437 void
1438 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1439 {
1440         struct ftrace_event_call *call = &event_user_stack;
1441         struct ring_buffer_event *event;
1442         struct userstack_entry *entry;
1443         struct stack_trace trace;
1444
1445         if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1446                 return;
1447
1448         /*
1449          * NMIs can not handle page faults, even with fix ups.
1450          * The save user stack can (and often does) fault.
1451          */
1452         if (unlikely(in_nmi()))
1453                 return;
1454
1455         /*
1456          * prevent recursion, since the user stack tracing may
1457          * trigger other kernel events.
1458          */
1459         preempt_disable();
1460         if (__this_cpu_read(user_stack_count))
1461                 goto out;
1462
1463         __this_cpu_inc(user_stack_count);
1464
1465         event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1466                                           sizeof(*entry), flags, pc);
1467         if (!event)
1468                 goto out_drop_count;
1469         entry   = ring_buffer_event_data(event);
1470
1471         entry->tgid             = current->tgid;
1472         memset(&entry->caller, 0, sizeof(entry->caller));
1473
1474         trace.nr_entries        = 0;
1475         trace.max_entries       = FTRACE_STACK_ENTRIES;
1476         trace.skip              = 0;
1477         trace.entries           = entry->caller;
1478
1479         save_stack_trace_user(&trace);
1480         if (!filter_check_discard(call, entry, buffer, event))
1481                 ring_buffer_unlock_commit(buffer, event);
1482
1483  out_drop_count:
1484         __this_cpu_dec(user_stack_count);
1485  out:
1486         preempt_enable();
1487 }
1488
1489 #ifdef UNUSED
1490 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1491 {
1492         ftrace_trace_userstack(tr, flags, preempt_count());
1493 }
1494 #endif /* UNUSED */
1495
1496 #endif /* CONFIG_STACKTRACE */
1497
1498 /* created for use with alloc_percpu */
1499 struct trace_buffer_struct {
1500         char buffer[TRACE_BUF_SIZE];
1501 };
1502
1503 static struct trace_buffer_struct *trace_percpu_buffer;
1504 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1505 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1506 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1507
1508 /*
1509  * The buffer used is dependent on the context. There is a per cpu
1510  * buffer for normal context, softirq contex, hard irq context and
1511  * for NMI context. Thise allows for lockless recording.
1512  *
1513  * Note, if the buffers failed to be allocated, then this returns NULL
1514  */
1515 static char *get_trace_buf(void)
1516 {
1517         struct trace_buffer_struct *percpu_buffer;
1518         struct trace_buffer_struct *buffer;
1519
1520         /*
1521          * If we have allocated per cpu buffers, then we do not
1522          * need to do any locking.
1523          */
1524         if (in_nmi())
1525                 percpu_buffer = trace_percpu_nmi_buffer;
1526         else if (in_irq())
1527                 percpu_buffer = trace_percpu_irq_buffer;
1528         else if (in_softirq())
1529                 percpu_buffer = trace_percpu_sirq_buffer;
1530         else
1531                 percpu_buffer = trace_percpu_buffer;
1532
1533         if (!percpu_buffer)
1534                 return NULL;
1535
1536         buffer = per_cpu_ptr(percpu_buffer, smp_processor_id());
1537
1538         return buffer->buffer;
1539 }
1540
1541 static int alloc_percpu_trace_buffer(void)
1542 {
1543         struct trace_buffer_struct *buffers;
1544         struct trace_buffer_struct *sirq_buffers;
1545         struct trace_buffer_struct *irq_buffers;
1546         struct trace_buffer_struct *nmi_buffers;
1547
1548         buffers = alloc_percpu(struct trace_buffer_struct);
1549         if (!buffers)
1550                 goto err_warn;
1551
1552         sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1553         if (!sirq_buffers)
1554                 goto err_sirq;
1555
1556         irq_buffers = alloc_percpu(struct trace_buffer_struct);
1557         if (!irq_buffers)
1558                 goto err_irq;
1559
1560         nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1561         if (!nmi_buffers)
1562                 goto err_nmi;
1563
1564         trace_percpu_buffer = buffers;
1565         trace_percpu_sirq_buffer = sirq_buffers;
1566         trace_percpu_irq_buffer = irq_buffers;
1567         trace_percpu_nmi_buffer = nmi_buffers;
1568
1569         return 0;
1570
1571  err_nmi:
1572         free_percpu(irq_buffers);
1573  err_irq:
1574         free_percpu(sirq_buffers);
1575  err_sirq:
1576         free_percpu(buffers);
1577  err_warn:
1578         WARN(1, "Could not allocate percpu trace_printk buffer");
1579         return -ENOMEM;
1580 }
1581
1582 void trace_printk_init_buffers(void)
1583 {
1584         static int buffers_allocated;
1585
1586         if (buffers_allocated)
1587                 return;
1588
1589         if (alloc_percpu_trace_buffer())
1590                 return;
1591
1592         pr_info("ftrace: Allocated trace_printk buffers\n");
1593
1594         buffers_allocated = 1;
1595 }
1596
1597 /**
1598  * trace_vbprintk - write binary msg to tracing buffer
1599  *
1600  */
1601 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1602 {
1603         struct ftrace_event_call *call = &event_bprint;
1604         struct ring_buffer_event *event;
1605         struct ring_buffer *buffer;
1606         struct trace_array *tr = &global_trace;
1607         struct bprint_entry *entry;
1608         unsigned long flags;
1609         char *tbuffer;
1610         int len = 0, size, pc;
1611
1612         if (unlikely(tracing_selftest_running || tracing_disabled))
1613                 return 0;
1614
1615         /* Don't pollute graph traces with trace_vprintk internals */
1616         pause_graph_tracing();
1617
1618         pc = preempt_count();
1619         preempt_disable_notrace();
1620
1621         tbuffer = get_trace_buf();
1622         if (!tbuffer) {
1623                 len = 0;
1624                 goto out;
1625         }
1626
1627         len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1628
1629         if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1630                 goto out;
1631
1632         local_save_flags(flags);
1633         size = sizeof(*entry) + sizeof(u32) * len;
1634         buffer = tr->buffer;
1635         event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1636                                           flags, pc);
1637         if (!event)
1638                 goto out;
1639         entry = ring_buffer_event_data(event);
1640         entry->ip                       = ip;
1641         entry->fmt                      = fmt;
1642
1643         memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1644         if (!filter_check_discard(call, entry, buffer, event)) {
1645                 ring_buffer_unlock_commit(buffer, event);
1646                 ftrace_trace_stack(buffer, flags, 6, pc);
1647         }
1648
1649 out:
1650         preempt_enable_notrace();
1651         unpause_graph_tracing();
1652
1653         return len;
1654 }
1655 EXPORT_SYMBOL_GPL(trace_vbprintk);
1656
1657 int trace_array_printk(struct trace_array *tr,
1658                        unsigned long ip, const char *fmt, ...)
1659 {
1660         int ret;
1661         va_list ap;
1662
1663         if (!(trace_flags & TRACE_ITER_PRINTK))
1664                 return 0;
1665
1666         va_start(ap, fmt);
1667         ret = trace_array_vprintk(tr, ip, fmt, ap);
1668         va_end(ap);
1669         return ret;
1670 }
1671
1672 int trace_array_vprintk(struct trace_array *tr,
1673                         unsigned long ip, const char *fmt, va_list args)
1674 {
1675         struct ftrace_event_call *call = &event_print;
1676         struct ring_buffer_event *event;
1677         struct ring_buffer *buffer;
1678         int len = 0, size, pc;
1679         struct print_entry *entry;
1680         unsigned long flags;
1681         char *tbuffer;
1682
1683         if (tracing_disabled || tracing_selftest_running)
1684                 return 0;
1685
1686         /* Don't pollute graph traces with trace_vprintk internals */
1687         pause_graph_tracing();
1688
1689         pc = preempt_count();
1690         preempt_disable_notrace();
1691
1692
1693         tbuffer = get_trace_buf();
1694         if (!tbuffer) {
1695                 len = 0;
1696                 goto out;
1697         }
1698
1699         len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1700         if (len > TRACE_BUF_SIZE)
1701                 goto out;
1702
1703         local_save_flags(flags);
1704         size = sizeof(*entry) + len + 1;
1705         buffer = tr->buffer;
1706         event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1707                                           flags, pc);
1708         if (!event)
1709                 goto out;
1710         entry = ring_buffer_event_data(event);
1711         entry->ip = ip;
1712
1713         memcpy(&entry->buf, tbuffer, len);
1714         entry->buf[len] = '\0';
1715         if (!filter_check_discard(call, entry, buffer, event)) {
1716                 ring_buffer_unlock_commit(buffer, event);
1717                 ftrace_trace_stack(buffer, flags, 6, pc);
1718         }
1719  out:
1720         preempt_enable_notrace();
1721         unpause_graph_tracing();
1722
1723         return len;
1724 }
1725
1726 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1727 {
1728         return trace_array_vprintk(&global_trace, ip, fmt, args);
1729 }
1730 EXPORT_SYMBOL_GPL(trace_vprintk);
1731
1732 static void trace_iterator_increment(struct trace_iterator *iter)
1733 {
1734         /* Don't allow ftrace to trace into the ring buffers */
1735         ftrace_disable_cpu();
1736
1737         iter->idx++;
1738         if (iter->buffer_iter[iter->cpu])
1739                 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1740
1741         ftrace_enable_cpu();
1742 }
1743
1744 static struct trace_entry *
1745 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1746                 unsigned long *lost_events)
1747 {
1748         struct ring_buffer_event *event;
1749         struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1750
1751         /* Don't allow ftrace to trace into the ring buffers */
1752         ftrace_disable_cpu();
1753
1754         if (buf_iter)
1755                 event = ring_buffer_iter_peek(buf_iter, ts);
1756         else
1757                 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1758                                          lost_events);
1759
1760         ftrace_enable_cpu();
1761
1762         if (event) {
1763                 iter->ent_size = ring_buffer_event_length(event);
1764                 return ring_buffer_event_data(event);
1765         }
1766         iter->ent_size = 0;
1767         return NULL;
1768 }
1769
1770 static struct trace_entry *
1771 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1772                   unsigned long *missing_events, u64 *ent_ts)
1773 {
1774         struct ring_buffer *buffer = iter->tr->buffer;
1775         struct trace_entry *ent, *next = NULL;
1776         unsigned long lost_events = 0, next_lost = 0;
1777         int cpu_file = iter->cpu_file;
1778         u64 next_ts = 0, ts;
1779         int next_cpu = -1;
1780         int next_size = 0;
1781         int cpu;
1782
1783         /*
1784          * If we are in a per_cpu trace file, don't bother by iterating over
1785          * all cpu and peek directly.
1786          */
1787         if (cpu_file > TRACE_PIPE_ALL_CPU) {
1788                 if (ring_buffer_empty_cpu(buffer, cpu_file))
1789                         return NULL;
1790                 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1791                 if (ent_cpu)
1792                         *ent_cpu = cpu_file;
1793
1794                 return ent;
1795         }
1796
1797         for_each_tracing_cpu(cpu) {
1798
1799                 if (ring_buffer_empty_cpu(buffer, cpu))
1800                         continue;
1801
1802                 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1803
1804                 /*
1805                  * Pick the entry with the smallest timestamp:
1806                  */
1807                 if (ent && (!next || ts < next_ts)) {
1808                         next = ent;
1809                         next_cpu = cpu;
1810                         next_ts = ts;
1811                         next_lost = lost_events;
1812                         next_size = iter->ent_size;
1813                 }
1814         }
1815
1816         iter->ent_size = next_size;
1817
1818         if (ent_cpu)
1819                 *ent_cpu = next_cpu;
1820
1821         if (ent_ts)
1822                 *ent_ts = next_ts;
1823
1824         if (missing_events)
1825                 *missing_events = next_lost;
1826
1827         return next;
1828 }
1829
1830 /* Find the next real entry, without updating the iterator itself */
1831 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1832                                           int *ent_cpu, u64 *ent_ts)
1833 {
1834         return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1835 }
1836
1837 /* Find the next real entry, and increment the iterator to the next entry */
1838 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1839 {
1840         iter->ent = __find_next_entry(iter, &iter->cpu,
1841                                       &iter->lost_events, &iter->ts);
1842
1843         if (iter->ent)
1844                 trace_iterator_increment(iter);
1845
1846         return iter->ent ? iter : NULL;
1847 }
1848
1849 static void trace_consume(struct trace_iterator *iter)
1850 {
1851         /* Don't allow ftrace to trace into the ring buffers */
1852         ftrace_disable_cpu();
1853         ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1854                             &iter->lost_events);
1855         ftrace_enable_cpu();
1856 }
1857
1858 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1859 {
1860         struct trace_iterator *iter = m->private;
1861         int i = (int)*pos;
1862         void *ent;
1863
1864         WARN_ON_ONCE(iter->leftover);
1865
1866         (*pos)++;
1867
1868         /* can't go backwards */
1869         if (iter->idx > i)
1870                 return NULL;
1871
1872         if (iter->idx < 0)
1873                 ent = trace_find_next_entry_inc(iter);
1874         else
1875                 ent = iter;
1876
1877         while (ent && iter->idx < i)
1878                 ent = trace_find_next_entry_inc(iter);
1879
1880         iter->pos = *pos;
1881
1882         return ent;
1883 }
1884
1885 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1886 {
1887         struct trace_array *tr = iter->tr;
1888         struct ring_buffer_event *event;
1889         struct ring_buffer_iter *buf_iter;
1890         unsigned long entries = 0;
1891         u64 ts;
1892
1893         tr->data[cpu]->skipped_entries = 0;
1894
1895         if (!iter->buffer_iter[cpu])
1896                 return;
1897
1898         buf_iter = iter->buffer_iter[cpu];
1899         ring_buffer_iter_reset(buf_iter);
1900
1901         /*
1902          * We could have the case with the max latency tracers
1903          * that a reset never took place on a cpu. This is evident
1904          * by the timestamp being before the start of the buffer.
1905          */
1906         while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1907                 if (ts >= iter->tr->time_start)
1908                         break;
1909                 entries++;
1910                 ring_buffer_read(buf_iter, NULL);
1911         }
1912
1913         tr->data[cpu]->skipped_entries = entries;
1914 }
1915
1916 /*
1917  * The current tracer is copied to avoid a global locking
1918  * all around.
1919  */
1920 static void *s_start(struct seq_file *m, loff_t *pos)
1921 {
1922         struct trace_iterator *iter = m->private;
1923         static struct tracer *old_tracer;
1924         int cpu_file = iter->cpu_file;
1925         void *p = NULL;
1926         loff_t l = 0;
1927         int cpu;
1928
1929         /* copy the tracer to avoid using a global lock all around */
1930         mutex_lock(&trace_types_lock);
1931         if (unlikely(old_tracer != current_trace && current_trace)) {
1932                 old_tracer = current_trace;
1933                 *iter->trace = *current_trace;
1934         }
1935         mutex_unlock(&trace_types_lock);
1936
1937         atomic_inc(&trace_record_cmdline_disabled);
1938
1939         if (*pos != iter->pos) {
1940                 iter->ent = NULL;
1941                 iter->cpu = 0;
1942                 iter->idx = -1;
1943
1944                 ftrace_disable_cpu();
1945
1946                 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1947                         for_each_tracing_cpu(cpu)
1948                                 tracing_iter_reset(iter, cpu);
1949                 } else
1950                         tracing_iter_reset(iter, cpu_file);
1951
1952                 ftrace_enable_cpu();
1953
1954                 iter->leftover = 0;
1955                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1956                         ;
1957
1958         } else {
1959                 /*
1960                  * If we overflowed the seq_file before, then we want
1961                  * to just reuse the trace_seq buffer again.
1962                  */
1963                 if (iter->leftover)
1964                         p = iter;
1965                 else {
1966                         l = *pos - 1;
1967                         p = s_next(m, p, &l);
1968                 }
1969         }
1970
1971         trace_event_read_lock();
1972         trace_access_lock(cpu_file);
1973         return p;
1974 }
1975
1976 static void s_stop(struct seq_file *m, void *p)
1977 {
1978         struct trace_iterator *iter = m->private;
1979
1980         atomic_dec(&trace_record_cmdline_disabled);
1981         trace_access_unlock(iter->cpu_file);
1982         trace_event_read_unlock();
1983 }
1984
1985 static void
1986 get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *entries)
1987 {
1988         unsigned long count;
1989         int cpu;
1990
1991         *total = 0;
1992         *entries = 0;
1993
1994         for_each_tracing_cpu(cpu) {
1995                 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1996                 /*
1997                  * If this buffer has skipped entries, then we hold all
1998                  * entries for the trace and we need to ignore the
1999                  * ones before the time stamp.
2000                  */
2001                 if (tr->data[cpu]->skipped_entries) {
2002                         count -= tr->data[cpu]->skipped_entries;
2003                         /* total is the same as the entries */
2004                         *total += count;
2005                 } else
2006                         *total += count +
2007                                 ring_buffer_overrun_cpu(tr->buffer, cpu);
2008                 *entries += count;
2009         }
2010 }
2011
2012 static void print_lat_help_header(struct seq_file *m)
2013 {
2014         seq_puts(m, "#                  _------=> CPU#            \n");
2015         seq_puts(m, "#                 / _-----=> irqs-off        \n");
2016         seq_puts(m, "#                | / _----=> need-resched    \n");
2017         seq_puts(m, "#                || / _---=> hardirq/softirq \n");
2018         seq_puts(m, "#                ||| / _--=> preempt-depth   \n");
2019         seq_puts(m, "#                |||| /     delay             \n");
2020         seq_puts(m, "#  cmd     pid   ||||| time  |   caller      \n");
2021         seq_puts(m, "#     \\   /      |||||  \\    |   /           \n");
2022 }
2023
2024 static void print_event_info(struct trace_array *tr, struct seq_file *m)
2025 {
2026         unsigned long total;
2027         unsigned long entries;
2028
2029         get_total_entries(tr, &total, &entries);
2030         seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu   #P:%d\n",
2031                    entries, total, num_online_cpus());
2032         seq_puts(m, "#\n");
2033 }
2034
2035 static void print_func_help_header(struct trace_array *tr, struct seq_file *m)
2036 {
2037         print_event_info(tr, m);
2038         seq_puts(m, "#           TASK-PID   CPU#      TIMESTAMP  FUNCTION\n");
2039         seq_puts(m, "#              | |       |          |         |\n");
2040 }
2041
2042 static void print_func_help_header_irq(struct trace_array *tr, struct seq_file *m)
2043 {
2044         print_event_info(tr, m);
2045         seq_puts(m, "#                              _-----=> irqs-off\n");
2046         seq_puts(m, "#                             / _----=> need-resched\n");
2047         seq_puts(m, "#                            | / _---=> hardirq/softirq\n");
2048         seq_puts(m, "#                            || / _--=> preempt-depth\n");
2049         seq_puts(m, "#                            ||| /     delay\n");
2050         seq_puts(m, "#           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION\n");
2051         seq_puts(m, "#              | |       |   ||||       |         |\n");
2052 }
2053
2054 void
2055 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2056 {
2057         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2058         struct trace_array *tr = iter->tr;
2059         struct trace_array_cpu *data = tr->data[tr->cpu];
2060         struct tracer *type = current_trace;
2061         unsigned long entries;
2062         unsigned long total;
2063         const char *name = "preemption";
2064
2065         if (type)
2066                 name = type->name;
2067
2068         get_total_entries(tr, &total, &entries);
2069
2070         seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2071                    name, UTS_RELEASE);
2072         seq_puts(m, "# -----------------------------------"
2073                  "---------------------------------\n");
2074         seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2075                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2076                    nsecs_to_usecs(data->saved_latency),
2077                    entries,
2078                    total,
2079                    tr->cpu,
2080 #if defined(CONFIG_PREEMPT_NONE)
2081                    "server",
2082 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2083                    "desktop",
2084 #elif defined(CONFIG_PREEMPT)
2085                    "preempt",
2086 #else
2087                    "unknown",
2088 #endif
2089                    /* These are reserved for later use */
2090                    0, 0, 0, 0);
2091 #ifdef CONFIG_SMP
2092         seq_printf(m, " #P:%d)\n", num_online_cpus());
2093 #else
2094         seq_puts(m, ")\n");
2095 #endif
2096         seq_puts(m, "#    -----------------\n");
2097         seq_printf(m, "#    | task: %.16s-%d "
2098                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2099                    data->comm, data->pid, data->uid, data->nice,
2100                    data->policy, data->rt_priority);
2101         seq_puts(m, "#    -----------------\n");
2102
2103         if (data->critical_start) {
2104                 seq_puts(m, "#  => started at: ");
2105                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2106                 trace_print_seq(m, &iter->seq);
2107                 seq_puts(m, "\n#  => ended at:   ");
2108                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2109                 trace_print_seq(m, &iter->seq);
2110                 seq_puts(m, "\n#\n");
2111         }
2112
2113         seq_puts(m, "#\n");
2114 }
2115
2116 static void test_cpu_buff_start(struct trace_iterator *iter)
2117 {
2118         struct trace_seq *s = &iter->seq;
2119
2120         if (!(trace_flags & TRACE_ITER_ANNOTATE))
2121                 return;
2122
2123         if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2124                 return;
2125
2126         if (cpumask_test_cpu(iter->cpu, iter->started))
2127                 return;
2128
2129         if (iter->tr->data[iter->cpu]->skipped_entries)
2130                 return;
2131
2132         cpumask_set_cpu(iter->cpu, iter->started);
2133
2134         /* Don't print started cpu buffer for the first entry of the trace */
2135         if (iter->idx > 1)
2136                 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2137                                 iter->cpu);
2138 }
2139
2140 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2141 {
2142         struct trace_seq *s = &iter->seq;
2143         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2144         struct trace_entry *entry;
2145         struct trace_event *event;
2146
2147         entry = iter->ent;
2148
2149         test_cpu_buff_start(iter);
2150
2151         event = ftrace_find_event(entry->type);
2152
2153         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2154                 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2155                         if (!trace_print_lat_context(iter))
2156                                 goto partial;
2157                 } else {
2158                         if (!trace_print_context(iter))
2159                                 goto partial;
2160                 }
2161         }
2162
2163         if (event)
2164                 return event->funcs->trace(iter, sym_flags, event);
2165
2166         if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2167                 goto partial;
2168
2169         return TRACE_TYPE_HANDLED;
2170 partial:
2171         return TRACE_TYPE_PARTIAL_LINE;
2172 }
2173
2174 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2175 {
2176         struct trace_seq *s = &iter->seq;
2177         struct trace_entry *entry;
2178         struct trace_event *event;
2179
2180         entry = iter->ent;
2181
2182         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2183                 if (!trace_seq_printf(s, "%d %d %llu ",
2184                                       entry->pid, iter->cpu, iter->ts))
2185                         goto partial;
2186         }
2187
2188         event = ftrace_find_event(entry->type);
2189         if (event)
2190                 return event->funcs->raw(iter, 0, event);
2191
2192         if (!trace_seq_printf(s, "%d ?\n", entry->type))
2193                 goto partial;
2194
2195         return TRACE_TYPE_HANDLED;
2196 partial:
2197         return TRACE_TYPE_PARTIAL_LINE;
2198 }
2199
2200 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2201 {
2202         struct trace_seq *s = &iter->seq;
2203         unsigned char newline = '\n';
2204         struct trace_entry *entry;
2205         struct trace_event *event;
2206
2207         entry = iter->ent;
2208
2209         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2210                 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2211                 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2212                 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2213         }
2214
2215         event = ftrace_find_event(entry->type);
2216         if (event) {
2217                 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2218                 if (ret != TRACE_TYPE_HANDLED)
2219                         return ret;
2220         }
2221
2222         SEQ_PUT_FIELD_RET(s, newline);
2223
2224         return TRACE_TYPE_HANDLED;
2225 }
2226
2227 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2228 {
2229         struct trace_seq *s = &iter->seq;
2230         struct trace_entry *entry;
2231         struct trace_event *event;
2232
2233         entry = iter->ent;
2234
2235         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2236                 SEQ_PUT_FIELD_RET(s, entry->pid);
2237                 SEQ_PUT_FIELD_RET(s, iter->cpu);
2238                 SEQ_PUT_FIELD_RET(s, iter->ts);
2239         }
2240
2241         event = ftrace_find_event(entry->type);
2242         return event ? event->funcs->binary(iter, 0, event) :
2243                 TRACE_TYPE_HANDLED;
2244 }
2245
2246 int trace_empty(struct trace_iterator *iter)
2247 {
2248         int cpu;
2249
2250         /* If we are looking at one CPU buffer, only check that one */
2251         if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2252                 cpu = iter->cpu_file;
2253                 if (iter->buffer_iter[cpu]) {
2254                         if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2255                                 return 0;
2256                 } else {
2257                         if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2258                                 return 0;
2259                 }
2260                 return 1;
2261         }
2262
2263         for_each_tracing_cpu(cpu) {
2264                 if (iter->buffer_iter[cpu]) {
2265                         if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2266                                 return 0;
2267                 } else {
2268                         if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2269                                 return 0;
2270                 }
2271         }
2272
2273         return 1;
2274 }
2275
2276 /*  Called with trace_event_read_lock() held. */
2277 enum print_line_t print_trace_line(struct trace_iterator *iter)
2278 {
2279         enum print_line_t ret;
2280
2281         if (iter->lost_events &&
2282             !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2283                                  iter->cpu, iter->lost_events))
2284                 return TRACE_TYPE_PARTIAL_LINE;
2285
2286         if (iter->trace && iter->trace->print_line) {
2287                 ret = iter->trace->print_line(iter);
2288                 if (ret != TRACE_TYPE_UNHANDLED)
2289                         return ret;
2290         }
2291
2292         if (iter->ent->type == TRACE_BPRINT &&
2293                         trace_flags & TRACE_ITER_PRINTK &&
2294                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2295                 return trace_print_bprintk_msg_only(iter);
2296
2297         if (iter->ent->type == TRACE_PRINT &&
2298                         trace_flags & TRACE_ITER_PRINTK &&
2299                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2300                 return trace_print_printk_msg_only(iter);
2301
2302         if (trace_flags & TRACE_ITER_BIN)
2303                 return print_bin_fmt(iter);
2304
2305         if (trace_flags & TRACE_ITER_HEX)
2306                 return print_hex_fmt(iter);
2307
2308         if (trace_flags & TRACE_ITER_RAW)
2309                 return print_raw_fmt(iter);
2310
2311         return print_trace_fmt(iter);
2312 }
2313
2314 void trace_latency_header(struct seq_file *m)
2315 {
2316         struct trace_iterator *iter = m->private;
2317
2318         /* print nothing if the buffers are empty */
2319         if (trace_empty(iter))
2320                 return;
2321
2322         if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2323                 print_trace_header(m, iter);
2324
2325         if (!(trace_flags & TRACE_ITER_VERBOSE))
2326                 print_lat_help_header(m);
2327 }
2328
2329 void trace_default_header(struct seq_file *m)
2330 {
2331         struct trace_iterator *iter = m->private;
2332
2333         if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2334                 return;
2335
2336         if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2337                 /* print nothing if the buffers are empty */
2338                 if (trace_empty(iter))
2339                         return;
2340                 print_trace_header(m, iter);
2341                 if (!(trace_flags & TRACE_ITER_VERBOSE))
2342                         print_lat_help_header(m);
2343         } else {
2344                 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2345                         if (trace_flags & TRACE_ITER_IRQ_INFO)
2346                                 print_func_help_header_irq(iter->tr, m);
2347                         else
2348                                 print_func_help_header(iter->tr, m);
2349                 }
2350         }
2351 }
2352
2353 static void test_ftrace_alive(struct seq_file *m)
2354 {
2355         if (!ftrace_is_dead())
2356                 return;
2357         seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2358         seq_printf(m, "#          MAY BE MISSING FUNCTION EVENTS\n");
2359 }
2360
2361 static int s_show(struct seq_file *m, void *v)
2362 {
2363         struct trace_iterator *iter = v;
2364         int ret;
2365
2366         if (iter->ent == NULL) {
2367                 if (iter->tr) {
2368                         seq_printf(m, "# tracer: %s\n", iter->trace->name);
2369                         seq_puts(m, "#\n");
2370                         test_ftrace_alive(m);
2371                 }
2372                 if (iter->trace && iter->trace->print_header)
2373                         iter->trace->print_header(m);
2374                 else
2375                         trace_default_header(m);
2376
2377         } else if (iter->leftover) {
2378                 /*
2379                  * If we filled the seq_file buffer earlier, we
2380                  * want to just show it now.
2381                  */
2382                 ret = trace_print_seq(m, &iter->seq);
2383
2384                 /* ret should this time be zero, but you never know */
2385                 iter->leftover = ret;
2386
2387         } else {
2388                 print_trace_line(iter);
2389                 ret = trace_print_seq(m, &iter->seq);
2390                 /*
2391                  * If we overflow the seq_file buffer, then it will
2392                  * ask us for this data again at start up.
2393                  * Use that instead.
2394                  *  ret is 0 if seq_file write succeeded.
2395                  *        -1 otherwise.
2396                  */
2397                 iter->leftover = ret;
2398         }
2399
2400         return 0;
2401 }
2402
2403 static const struct seq_operations tracer_seq_ops = {
2404         .start          = s_start,
2405         .next           = s_next,
2406         .stop           = s_stop,
2407         .show           = s_show,
2408 };
2409
2410 static struct trace_iterator *
2411 __tracing_open(struct inode *inode, struct file *file)
2412 {
2413         long cpu_file = (long) inode->i_private;
2414         void *fail_ret = ERR_PTR(-ENOMEM);
2415         struct trace_iterator *iter;
2416         struct seq_file *m;
2417         int cpu, ret;
2418
2419         if (tracing_disabled)
2420                 return ERR_PTR(-ENODEV);
2421
2422         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2423         if (!iter)
2424                 return ERR_PTR(-ENOMEM);
2425
2426         /*
2427          * We make a copy of the current tracer to avoid concurrent
2428          * changes on it while we are reading.
2429          */
2430         mutex_lock(&trace_types_lock);
2431         iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2432         if (!iter->trace)
2433                 goto fail;
2434
2435         if (current_trace)
2436                 *iter->trace = *current_trace;
2437
2438         if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2439                 goto fail;
2440
2441         if (current_trace && current_trace->print_max)
2442                 iter->tr = &max_tr;
2443         else
2444                 iter->tr = &global_trace;
2445         iter->pos = -1;
2446         mutex_init(&iter->mutex);
2447         iter->cpu_file = cpu_file;
2448
2449         /* Notify the tracer early; before we stop tracing. */
2450         if (iter->trace && iter->trace->open)
2451                 iter->trace->open(iter);
2452
2453         /* Annotate start of buffers if we had overruns */
2454         if (ring_buffer_overruns(iter->tr->buffer))
2455                 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2456
2457         /* stop the trace while dumping */
2458         tracing_stop();
2459
2460         if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2461                 for_each_tracing_cpu(cpu) {
2462                         iter->buffer_iter[cpu] =
2463                                 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2464                 }
2465                 ring_buffer_read_prepare_sync();
2466                 for_each_tracing_cpu(cpu) {
2467                         ring_buffer_read_start(iter->buffer_iter[cpu]);
2468                         tracing_iter_reset(iter, cpu);
2469                 }
2470         } else {
2471                 cpu = iter->cpu_file;
2472                 iter->buffer_iter[cpu] =
2473                         ring_buffer_read_prepare(iter->tr->buffer, cpu);
2474                 ring_buffer_read_prepare_sync();
2475                 ring_buffer_read_start(iter->buffer_iter[cpu]);
2476                 tracing_iter_reset(iter, cpu);
2477         }
2478
2479         ret = seq_open(file, &tracer_seq_ops);
2480         if (ret < 0) {
2481                 fail_ret = ERR_PTR(ret);
2482                 goto fail_buffer;
2483         }
2484
2485         m = file->private_data;
2486         m->private = iter;
2487
2488         mutex_unlock(&trace_types_lock);
2489
2490         return iter;
2491
2492  fail_buffer:
2493         for_each_tracing_cpu(cpu) {
2494                 if (iter->buffer_iter[cpu])
2495                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
2496         }
2497         free_cpumask_var(iter->started);
2498         tracing_start();
2499  fail:
2500         mutex_unlock(&trace_types_lock);
2501         kfree(iter->trace);
2502         kfree(iter);
2503
2504         return fail_ret;
2505 }
2506
2507 int tracing_open_generic(struct inode *inode, struct file *filp)
2508 {
2509         if (tracing_disabled)
2510                 return -ENODEV;
2511
2512         filp->private_data = inode->i_private;
2513         return 0;
2514 }
2515
2516 static int tracing_release(struct inode *inode, struct file *file)
2517 {
2518         struct seq_file *m = file->private_data;
2519         struct trace_iterator *iter;
2520         int cpu;
2521
2522         if (!(file->f_mode & FMODE_READ))
2523                 return 0;
2524
2525         iter = m->private;
2526
2527         mutex_lock(&trace_types_lock);
2528         for_each_tracing_cpu(cpu) {
2529                 if (iter->buffer_iter[cpu])
2530                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
2531         }
2532
2533         if (iter->trace && iter->trace->close)
2534                 iter->trace->close(iter);
2535
2536         /* reenable tracing if it was previously enabled */
2537         tracing_start();
2538         mutex_unlock(&trace_types_lock);
2539
2540         seq_release(inode, file);
2541         mutex_destroy(&iter->mutex);
2542         free_cpumask_var(iter->started);
2543         kfree(iter->trace);
2544         kfree(iter);
2545         return 0;
2546 }
2547
2548 static int tracing_open(struct inode *inode, struct file *file)
2549 {
2550         struct trace_iterator *iter;
2551         int ret = 0;
2552
2553         /* If this file was open for write, then erase contents */
2554         if ((file->f_mode & FMODE_WRITE) &&
2555             (file->f_flags & O_TRUNC)) {
2556                 long cpu = (long) inode->i_private;
2557
2558                 if (cpu == TRACE_PIPE_ALL_CPU)
2559                         tracing_reset_online_cpus(&global_trace);
2560                 else
2561                         tracing_reset(&global_trace, cpu);
2562         }
2563
2564         if (file->f_mode & FMODE_READ) {
2565                 iter = __tracing_open(inode, file);
2566                 if (IS_ERR(iter))
2567                         ret = PTR_ERR(iter);
2568                 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2569                         iter->iter_flags |= TRACE_FILE_LAT_FMT;
2570         }
2571         return ret;
2572 }
2573
2574 static void *
2575 t_next(struct seq_file *m, void *v, loff_t *pos)
2576 {
2577         struct tracer *t = v;
2578
2579         (*pos)++;
2580
2581         if (t)
2582                 t = t->next;
2583
2584         return t;
2585 }
2586
2587 static void *t_start(struct seq_file *m, loff_t *pos)
2588 {
2589         struct tracer *t;
2590         loff_t l = 0;
2591
2592         mutex_lock(&trace_types_lock);
2593         for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2594                 ;
2595
2596         return t;
2597 }
2598
2599 static void t_stop(struct seq_file *m, void *p)
2600 {
2601         mutex_unlock(&trace_types_lock);
2602 }
2603
2604 static int t_show(struct seq_file *m, void *v)
2605 {
2606         struct tracer *t = v;
2607
2608         if (!t)
2609                 return 0;
2610
2611         seq_printf(m, "%s", t->name);
2612         if (t->next)
2613                 seq_putc(m, ' ');
2614         else
2615                 seq_putc(m, '\n');
2616
2617         return 0;
2618 }
2619
2620 static const struct seq_operations show_traces_seq_ops = {
2621         .start          = t_start,
2622         .next           = t_next,
2623         .stop           = t_stop,
2624         .show           = t_show,
2625 };
2626
2627 static int show_traces_open(struct inode *inode, struct file *file)
2628 {
2629         if (tracing_disabled)
2630                 return -ENODEV;
2631
2632         return seq_open(file, &show_traces_seq_ops);
2633 }
2634
2635 static ssize_t
2636 tracing_write_stub(struct file *filp, const char __user *ubuf,
2637                    size_t count, loff_t *ppos)
2638 {
2639         return count;
2640 }
2641
2642 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2643 {
2644         if (file->f_mode & FMODE_READ)
2645                 return seq_lseek(file, offset, origin);
2646         else
2647                 return 0;
2648 }
2649
2650 static const struct file_operations tracing_fops = {
2651         .open           = tracing_open,
2652         .read           = seq_read,
2653         .write          = tracing_write_stub,
2654         .llseek         = tracing_seek,
2655         .release        = tracing_release,
2656 };
2657
2658 static const struct file_operations show_traces_fops = {
2659         .open           = show_traces_open,
2660         .read           = seq_read,
2661         .release        = seq_release,
2662         .llseek         = seq_lseek,
2663 };
2664
2665 /*
2666  * Only trace on a CPU if the bitmask is set:
2667  */
2668 static cpumask_var_t tracing_cpumask;
2669
2670 /*
2671  * The tracer itself will not take this lock, but still we want
2672  * to provide a consistent cpumask to user-space:
2673  */
2674 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2675
2676 /*
2677  * Temporary storage for the character representation of the
2678  * CPU bitmask (and one more byte for the newline):
2679  */
2680 static char mask_str[NR_CPUS + 1];
2681
2682 static ssize_t
2683 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2684                      size_t count, loff_t *ppos)
2685 {
2686         int len;
2687
2688         mutex_lock(&tracing_cpumask_update_lock);
2689
2690         len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2691         if (count - len < 2) {
2692                 count = -EINVAL;
2693                 goto out_err;
2694         }
2695         len += sprintf(mask_str + len, "\n");
2696         count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2697
2698 out_err:
2699         mutex_unlock(&tracing_cpumask_update_lock);
2700
2701         return count;
2702 }
2703
2704 static ssize_t
2705 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2706                       size_t count, loff_t *ppos)
2707 {
2708         int err, cpu;
2709         cpumask_var_t tracing_cpumask_new;
2710
2711         if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2712                 return -ENOMEM;
2713
2714         err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2715         if (err)
2716                 goto err_unlock;
2717
2718         mutex_lock(&tracing_cpumask_update_lock);
2719
2720         local_irq_disable();
2721         arch_spin_lock(&ftrace_max_lock);
2722         for_each_tracing_cpu(cpu) {
2723                 /*
2724                  * Increase/decrease the disabled counter if we are
2725                  * about to flip a bit in the cpumask:
2726                  */
2727                 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2728                                 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2729                         atomic_inc(&global_trace.data[cpu]->disabled);
2730                 }
2731                 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2732                                 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2733                         atomic_dec(&global_trace.data[cpu]->disabled);
2734                 }
2735         }
2736         arch_spin_unlock(&ftrace_max_lock);
2737         local_irq_enable();
2738
2739         cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2740
2741         mutex_unlock(&tracing_cpumask_update_lock);
2742         free_cpumask_var(tracing_cpumask_new);
2743
2744         return count;
2745
2746 err_unlock:
2747         free_cpumask_var(tracing_cpumask_new);
2748
2749         return err;
2750 }
2751
2752 static const struct file_operations tracing_cpumask_fops = {
2753         .open           = tracing_open_generic,
2754         .read           = tracing_cpumask_read,
2755         .write          = tracing_cpumask_write,
2756         .llseek         = generic_file_llseek,
2757 };
2758
2759 static int tracing_trace_options_show(struct seq_file *m, void *v)
2760 {
2761         struct tracer_opt *trace_opts;
2762         u32 tracer_flags;
2763         int i;
2764
2765         mutex_lock(&trace_types_lock);
2766         tracer_flags = current_trace->flags->val;
2767         trace_opts = current_trace->flags->opts;
2768
2769         for (i = 0; trace_options[i]; i++) {
2770                 if (trace_flags & (1 << i))
2771                         seq_printf(m, "%s\n", trace_options[i]);
2772                 else
2773                         seq_printf(m, "no%s\n", trace_options[i]);
2774         }
2775
2776         for (i = 0; trace_opts[i].name; i++) {
2777                 if (tracer_flags & trace_opts[i].bit)
2778                         seq_printf(m, "%s\n", trace_opts[i].name);
2779                 else
2780                         seq_printf(m, "no%s\n", trace_opts[i].name);
2781         }
2782         mutex_unlock(&trace_types_lock);
2783
2784         return 0;
2785 }
2786
2787 static int __set_tracer_option(struct tracer *trace,
2788                                struct tracer_flags *tracer_flags,
2789                                struct tracer_opt *opts, int neg)
2790 {
2791         int ret;
2792
2793         ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2794         if (ret)
2795                 return ret;
2796
2797         if (neg)
2798                 tracer_flags->val &= ~opts->bit;
2799         else
2800                 tracer_flags->val |= opts->bit;
2801         return 0;
2802 }
2803
2804 /* Try to assign a tracer specific option */
2805 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2806 {
2807         struct tracer_flags *tracer_flags = trace->flags;
2808         struct tracer_opt *opts = NULL;
2809         int i;
2810
2811         for (i = 0; tracer_flags->opts[i].name; i++) {
2812                 opts = &tracer_flags->opts[i];
2813
2814                 if (strcmp(cmp, opts->name) == 0)
2815                         return __set_tracer_option(trace, trace->flags,
2816                                                    opts, neg);
2817         }
2818
2819         return -EINVAL;
2820 }
2821
2822 static void set_tracer_flags(unsigned int mask, int enabled)
2823 {
2824         /* do nothing if flag is already set */
2825         if (!!(trace_flags & mask) == !!enabled)
2826                 return;
2827
2828         if (enabled)
2829                 trace_flags |= mask;
2830         else
2831                 trace_flags &= ~mask;
2832
2833         if (mask == TRACE_ITER_RECORD_CMD)
2834                 trace_event_enable_cmd_record(enabled);
2835
2836         if (mask == TRACE_ITER_OVERWRITE)
2837                 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2838 }
2839
2840 static ssize_t
2841 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2842                         size_t cnt, loff_t *ppos)
2843 {
2844         char buf[64];
2845         char *cmp;
2846         int neg = 0;
2847         int ret;
2848         int i;
2849
2850         if (cnt >= sizeof(buf))
2851                 return -EINVAL;
2852
2853         if (copy_from_user(&buf, ubuf, cnt))
2854                 return -EFAULT;
2855
2856         buf[cnt] = 0;
2857         cmp = strstrip(buf);
2858
2859         if (strncmp(cmp, "no", 2) == 0) {
2860                 neg = 1;
2861                 cmp += 2;
2862         }
2863
2864         for (i = 0; trace_options[i]; i++) {
2865                 if (strcmp(cmp, trace_options[i]) == 0) {
2866                         set_tracer_flags(1 << i, !neg);
2867                         break;
2868                 }
2869         }
2870
2871         /* If no option could be set, test the specific tracer options */
2872         if (!trace_options[i]) {
2873                 mutex_lock(&trace_types_lock);
2874                 ret = set_tracer_option(current_trace, cmp, neg);
2875                 mutex_unlock(&trace_types_lock);
2876                 if (ret)
2877                         return ret;
2878         }
2879
2880         *ppos += cnt;
2881
2882         return cnt;
2883 }
2884
2885 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2886 {
2887         if (tracing_disabled)
2888                 return -ENODEV;
2889         return single_open(file, tracing_trace_options_show, NULL);
2890 }
2891
2892 static const struct file_operations tracing_iter_fops = {
2893         .open           = tracing_trace_options_open,
2894         .read           = seq_read,
2895         .llseek         = seq_lseek,
2896         .release        = single_release,
2897         .write          = tracing_trace_options_write,
2898 };
2899
2900 static const char readme_msg[] =
2901         "tracing mini-HOWTO:\n\n"
2902         "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2903         "# cat /sys/kernel/debug/tracing/available_tracers\n"
2904         "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
2905         "# cat /sys/kernel/debug/tracing/current_tracer\n"
2906         "nop\n"
2907         "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
2908         "# cat /sys/kernel/debug/tracing/current_tracer\n"
2909         "wakeup\n"
2910         "# cat /sys/kernel/debug/tracing/trace_options\n"
2911         "noprint-parent nosym-offset nosym-addr noverbose\n"
2912         "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2913         "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
2914         "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2915         "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
2916 ;
2917
2918 static ssize_t
2919 tracing_readme_read(struct file *filp, char __user *ubuf,
2920                        size_t cnt, loff_t *ppos)
2921 {
2922         return simple_read_from_buffer(ubuf, cnt, ppos,
2923                                         readme_msg, strlen(readme_msg));
2924 }
2925
2926 static const struct file_operations tracing_readme_fops = {
2927         .open           = tracing_open_generic,
2928         .read           = tracing_readme_read,
2929         .llseek         = generic_file_llseek,
2930 };
2931
2932 static ssize_t
2933 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2934                                 size_t cnt, loff_t *ppos)
2935 {
2936         char *buf_comm;
2937         char *file_buf;
2938         char *buf;
2939         int len = 0;
2940         int pid;
2941         int i;
2942
2943         file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2944         if (!file_buf)
2945                 return -ENOMEM;
2946
2947         buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2948         if (!buf_comm) {
2949                 kfree(file_buf);
2950                 return -ENOMEM;
2951         }
2952
2953         buf = file_buf;
2954
2955         for (i = 0; i < SAVED_CMDLINES; i++) {
2956                 int r;
2957
2958                 pid = map_cmdline_to_pid[i];
2959                 if (pid == -1 || pid == NO_CMDLINE_MAP)
2960                         continue;
2961
2962                 trace_find_cmdline(pid, buf_comm);
2963                 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2964                 buf += r;
2965                 len += r;
2966         }
2967
2968         len = simple_read_from_buffer(ubuf, cnt, ppos,
2969                                       file_buf, len);
2970
2971         kfree(file_buf);
2972         kfree(buf_comm);
2973
2974         return len;
2975 }
2976
2977 static const struct file_operations tracing_saved_cmdlines_fops = {
2978     .open       = tracing_open_generic,
2979     .read       = tracing_saved_cmdlines_read,
2980     .llseek     = generic_file_llseek,
2981 };
2982
2983 static ssize_t
2984 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2985                   size_t cnt, loff_t *ppos)
2986 {
2987         char buf[64];
2988         int r;
2989
2990         r = sprintf(buf, "%u\n", tracer_enabled);
2991         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2992 }
2993
2994 static ssize_t
2995 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2996                    size_t cnt, loff_t *ppos)
2997 {
2998         struct trace_array *tr = filp->private_data;
2999         unsigned long val;
3000         int ret;
3001
3002         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3003         if (ret)
3004                 return ret;
3005
3006         val = !!val;
3007
3008         mutex_lock(&trace_types_lock);
3009         if (tracer_enabled ^ val) {
3010
3011                 /* Only need to warn if this is used to change the state */
3012                 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
3013
3014                 if (val) {
3015                         tracer_enabled = 1;
3016                         if (current_trace->start)
3017                                 current_trace->start(tr);
3018                         tracing_start();
3019                 } else {
3020                         tracer_enabled = 0;
3021                         tracing_stop();
3022                         if (current_trace->stop)
3023                                 current_trace->stop(tr);
3024                 }
3025         }
3026         mutex_unlock(&trace_types_lock);
3027
3028         *ppos += cnt;
3029
3030         return cnt;
3031 }
3032
3033 static ssize_t
3034 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3035                        size_t cnt, loff_t *ppos)
3036 {
3037         char buf[MAX_TRACER_SIZE+2];
3038         int r;
3039
3040         mutex_lock(&trace_types_lock);
3041         if (current_trace)
3042                 r = sprintf(buf, "%s\n", current_trace->name);
3043         else
3044                 r = sprintf(buf, "\n");
3045         mutex_unlock(&trace_types_lock);
3046
3047         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3048 }
3049
3050 int tracer_init(struct tracer *t, struct trace_array *tr)
3051 {
3052         tracing_reset_online_cpus(tr);
3053         return t->init(tr);
3054 }
3055
3056 static int __tracing_resize_ring_buffer(unsigned long size)
3057 {
3058         int ret;
3059
3060         /*
3061          * If kernel or user changes the size of the ring buffer
3062          * we use the size that was given, and we can forget about
3063          * expanding it later.
3064          */
3065         ring_buffer_expanded = 1;
3066
3067         ret = ring_buffer_resize(global_trace.buffer, size);
3068         if (ret < 0)
3069                 return ret;
3070
3071         if (!current_trace->use_max_tr)
3072                 goto out;
3073
3074         ret = ring_buffer_resize(max_tr.buffer, size);
3075         if (ret < 0) {
3076                 int r;
3077
3078                 r = ring_buffer_resize(global_trace.buffer,
3079                                        global_trace.entries);
3080                 if (r < 0) {
3081                         /*
3082                          * AARGH! We are left with different
3083                          * size max buffer!!!!
3084                          * The max buffer is our "snapshot" buffer.
3085                          * When a tracer needs a snapshot (one of the
3086                          * latency tracers), it swaps the max buffer
3087                          * with the saved snap shot. We succeeded to
3088                          * update the size of the main buffer, but failed to
3089                          * update the size of the max buffer. But when we tried
3090                          * to reset the main buffer to the original size, we
3091                          * failed there too. This is very unlikely to
3092                          * happen, but if it does, warn and kill all
3093                          * tracing.
3094                          */
3095                         WARN_ON(1);
3096                         tracing_disabled = 1;
3097                 }
3098                 return ret;
3099         }
3100
3101         max_tr.entries = size;
3102  out:
3103         global_trace.entries = size;
3104
3105         return ret;
3106 }
3107
3108 static ssize_t tracing_resize_ring_buffer(unsigned long size)
3109 {
3110         int cpu, ret = size;
3111
3112         mutex_lock(&trace_types_lock);
3113
3114         tracing_stop();
3115
3116         /* disable all cpu buffers */
3117         for_each_tracing_cpu(cpu) {
3118                 if (global_trace.data[cpu])
3119                         atomic_inc(&global_trace.data[cpu]->disabled);
3120                 if (max_tr.data[cpu])
3121                         atomic_inc(&max_tr.data[cpu]->disabled);
3122         }
3123
3124         if (size != global_trace.entries)
3125                 ret = __tracing_resize_ring_buffer(size);
3126
3127         if (ret < 0)
3128                 ret = -ENOMEM;
3129
3130         for_each_tracing_cpu(cpu) {
3131                 if (global_trace.data[cpu])
3132                         atomic_dec(&global_trace.data[cpu]->disabled);
3133                 if (max_tr.data[cpu])
3134                         atomic_dec(&max_tr.data[cpu]->disabled);
3135         }
3136
3137         tracing_start();
3138         mutex_unlock(&trace_types_lock);
3139
3140         return ret;
3141 }
3142
3143
3144 /**
3145  * tracing_update_buffers - used by tracing facility to expand ring buffers
3146  *
3147  * To save on memory when the tracing is never used on a system with it
3148  * configured in. The ring buffers are set to a minimum size. But once
3149  * a user starts to use the tracing facility, then they need to grow
3150  * to their default size.
3151  *
3152  * This function is to be called when a tracer is about to be used.
3153  */
3154 int tracing_update_buffers(void)
3155 {
3156         int ret = 0;
3157
3158         mutex_lock(&trace_types_lock);
3159         if (!ring_buffer_expanded)
3160                 ret = __tracing_resize_ring_buffer(trace_buf_size);
3161         mutex_unlock(&trace_types_lock);
3162
3163         return ret;
3164 }
3165
3166 struct trace_option_dentry;
3167
3168 static struct trace_option_dentry *
3169 create_trace_option_files(struct tracer *tracer);
3170
3171 static void
3172 destroy_trace_option_files(struct trace_option_dentry *topts);
3173
3174 static int tracing_set_tracer(const char *buf)
3175 {
3176         static struct trace_option_dentry *topts;
3177         struct trace_array *tr = &global_trace;
3178         struct tracer *t;
3179         int ret = 0;
3180
3181         mutex_lock(&trace_types_lock);
3182
3183         if (!ring_buffer_expanded) {
3184                 ret = __tracing_resize_ring_buffer(trace_buf_size);
3185                 if (ret < 0)
3186                         goto out;
3187                 ret = 0;
3188         }
3189
3190         for (t = trace_types; t; t = t->next) {
3191                 if (strcmp(t->name, buf) == 0)
3192                         break;
3193         }
3194         if (!t) {
3195                 ret = -EINVAL;
3196                 goto out;
3197         }
3198         if (t == current_trace)
3199                 goto out;
3200
3201         trace_branch_disable();
3202         if (current_trace && current_trace->reset)
3203                 current_trace->reset(tr);
3204         if (current_trace && current_trace->use_max_tr) {
3205                 /*
3206                  * We don't free the ring buffer. instead, resize it because
3207                  * The max_tr ring buffer has some state (e.g. ring->clock) and
3208                  * we want preserve it.
3209                  */
3210                 ring_buffer_resize(max_tr.buffer, 1);
3211                 max_tr.entries = 1;
3212         }
3213         destroy_trace_option_files(topts);
3214
3215         current_trace = t;
3216
3217         topts = create_trace_option_files(current_trace);
3218         if (current_trace->use_max_tr) {
3219                 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
3220                 if (ret < 0)
3221                         goto out;
3222                 max_tr.entries = global_trace.entries;
3223         }
3224
3225         if (t->init) {
3226                 ret = tracer_init(t, tr);
3227                 if (ret)
3228                         goto out;
3229         }
3230
3231         trace_branch_enable(tr);
3232  out:
3233         mutex_unlock(&trace_types_lock);
3234
3235         return ret;
3236 }
3237
3238 static ssize_t
3239 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3240                         size_t cnt, loff_t *ppos)
3241 {
3242         char buf[MAX_TRACER_SIZE+1];
3243         int i;
3244         size_t ret;
3245         int err;
3246
3247         ret = cnt;
3248
3249         if (cnt > MAX_TRACER_SIZE)
3250                 cnt = MAX_TRACER_SIZE;
3251
3252         if (copy_from_user(&buf, ubuf, cnt))
3253                 return -EFAULT;
3254
3255         buf[cnt] = 0;
3256
3257         /* strip ending whitespace. */
3258         for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3259                 buf[i] = 0;
3260
3261         err = tracing_set_tracer(buf);
3262         if (err)
3263                 return err;
3264
3265         *ppos += ret;
3266
3267         return ret;
3268 }
3269
3270 static ssize_t
3271 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3272                      size_t cnt, loff_t *ppos)
3273 {
3274         unsigned long *ptr = filp->private_data;
3275         char buf[64];
3276         int r;
3277
3278         r = snprintf(buf, sizeof(buf), "%ld\n",
3279                      *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3280         if (r > sizeof(buf))
3281                 r = sizeof(buf);
3282         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3283 }
3284
3285 static ssize_t
3286 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3287                       size_t cnt, loff_t *ppos)
3288 {
3289         unsigned long *ptr = filp->private_data;
3290         unsigned long val;
3291         int ret;
3292
3293         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3294         if (ret)
3295                 return ret;
3296
3297         *ptr = val * 1000;
3298
3299         return cnt;
3300 }
3301
3302 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3303 {
3304         long cpu_file = (long) inode->i_private;
3305         struct trace_iterator *iter;
3306         int ret = 0;
3307
3308         if (tracing_disabled)
3309                 return -ENODEV;
3310
3311         mutex_lock(&trace_types_lock);
3312
3313         /* create a buffer to store the information to pass to userspace */
3314         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3315         if (!iter) {
3316                 ret = -ENOMEM;
3317                 goto out;
3318         }
3319
3320         /*
3321          * We make a copy of the current tracer to avoid concurrent
3322          * changes on it while we are reading.
3323          */
3324         iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3325         if (!iter->trace) {
3326                 ret = -ENOMEM;
3327                 goto fail;
3328         }
3329         if (current_trace)
3330                 *iter->trace = *current_trace;
3331
3332         if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3333                 ret = -ENOMEM;
3334                 goto fail;
3335         }
3336
3337         /* trace pipe does not show start of buffer */
3338         cpumask_setall(iter->started);
3339
3340         if (trace_flags & TRACE_ITER_LATENCY_FMT)
3341                 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3342
3343         iter->cpu_file = cpu_file;
3344         iter->tr = &global_trace;
3345         mutex_init(&iter->mutex);
3346         filp->private_data = iter;
3347
3348         if (iter->trace->pipe_open)
3349                 iter->trace->pipe_open(iter);
3350
3351         nonseekable_open(inode, filp);
3352 out:
3353         mutex_unlock(&trace_types_lock);
3354         return ret;
3355
3356 fail:
3357         kfree(iter->trace);
3358         kfree(iter);
3359         mutex_unlock(&trace_types_lock);
3360         return ret;
3361 }
3362
3363 static int tracing_release_pipe(struct inode *inode, struct file *file)
3364 {
3365         struct trace_iterator *iter = file->private_data;
3366
3367         mutex_lock(&trace_types_lock);
3368
3369         if (iter->trace->pipe_close)
3370                 iter->trace->pipe_close(iter);
3371
3372         mutex_unlock(&trace_types_lock);
3373
3374         free_cpumask_var(iter->started);
3375         mutex_destroy(&iter->mutex);
3376         kfree(iter->trace);
3377         kfree(iter);
3378
3379         return 0;
3380 }
3381
3382 static unsigned int
3383 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3384 {
3385         struct trace_iterator *iter = filp->private_data;
3386
3387         if (trace_flags & TRACE_ITER_BLOCK) {
3388                 /*
3389                  * Always select as readable when in blocking mode
3390                  */
3391                 return POLLIN | POLLRDNORM;
3392         } else {
3393                 if (!trace_empty(iter))
3394                         return POLLIN | POLLRDNORM;
3395                 poll_wait(filp, &trace_wait, poll_table);
3396                 if (!trace_empty(iter))
3397                         return POLLIN | POLLRDNORM;
3398
3399                 return 0;
3400         }
3401 }
3402
3403
3404 void default_wait_pipe(struct trace_iterator *iter)
3405 {
3406         DEFINE_WAIT(wait);
3407
3408         prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3409
3410         if (trace_empty(iter))
3411                 schedule();
3412
3413         finish_wait(&trace_wait, &wait);
3414 }
3415
3416 /*
3417  * This is a make-shift waitqueue.
3418  * A tracer might use this callback on some rare cases:
3419  *
3420  *  1) the current tracer might hold the runqueue lock when it wakes up
3421  *     a reader, hence a deadlock (sched, function, and function graph tracers)
3422  *  2) the function tracers, trace all functions, we don't want
3423  *     the overhead of calling wake_up and friends
3424  *     (and tracing them too)
3425  *
3426  *     Anyway, this is really very primitive wakeup.
3427  */
3428 void poll_wait_pipe(struct trace_iterator *iter)
3429 {
3430         set_current_state(TASK_INTERRUPTIBLE);
3431         /* sleep for 100 msecs, and try again. */
3432         schedule_timeout(HZ / 10);
3433 }
3434
3435 /* Must be called with trace_types_lock mutex held. */
3436 static int tracing_wait_pipe(struct file *filp)
3437 {
3438         struct trace_iterator *iter = filp->private_data;
3439
3440         while (trace_empty(iter)) {
3441
3442                 if ((filp->f_flags & O_NONBLOCK)) {
3443                         return -EAGAIN;
3444                 }
3445
3446                 mutex_unlock(&iter->mutex);
3447
3448                 iter->trace->wait_pipe(iter);
3449
3450                 mutex_lock(&iter->mutex);
3451
3452                 if (signal_pending(current))
3453                         return -EINTR;
3454
3455                 /*
3456                  * We block until we read something and tracing is disabled.
3457                  * We still block if tracing is disabled, but we have never
3458                  * read anything. This allows a user to cat this file, and
3459                  * then enable tracing. But after we have read something,
3460                  * we give an EOF when tracing is again disabled.
3461                  *
3462                  * iter->pos will be 0 if we haven't read anything.
3463                  */
3464                 if (!tracer_enabled && iter->pos)
3465                         break;
3466         }
3467
3468         return 1;
3469 }
3470
3471 /*
3472  * Consumer reader.
3473  */
3474 static ssize_t
3475 tracing_read_pipe(struct file *filp, char __user *ubuf,
3476                   size_t cnt, loff_t *ppos)
3477 {
3478         struct trace_iterator *iter = filp->private_data;
3479         static struct tracer *old_tracer;
3480         ssize_t sret;
3481
3482         /* return any leftover data */
3483         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3484         if (sret != -EBUSY)
3485                 return sret;
3486
3487         trace_seq_init(&iter->seq);
3488
3489         /* copy the tracer to avoid using a global lock all around */
3490         mutex_lock(&trace_types_lock);
3491         if (unlikely(old_tracer != current_trace && current_trace)) {
3492                 old_tracer = current_trace;
3493                 *iter->trace = *current_trace;
3494         }
3495         mutex_unlock(&trace_types_lock);
3496
3497         /*
3498          * Avoid more than one consumer on a single file descriptor
3499          * This is just a matter of traces coherency, the ring buffer itself
3500          * is protected.
3501          */
3502         mutex_lock(&iter->mutex);
3503         if (iter->trace->read) {
3504                 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3505                 if (sret)
3506                         goto out;
3507         }
3508
3509 waitagain:
3510         sret = tracing_wait_pipe(filp);
3511         if (sret <= 0)
3512                 goto out;
3513
3514         /* stop when tracing is finished */
3515         if (trace_empty(iter)) {
3516                 sret = 0;
3517                 goto out;
3518         }
3519
3520         if (cnt >= PAGE_SIZE)
3521                 cnt = PAGE_SIZE - 1;
3522
3523         /* reset all but tr, trace, and overruns */
3524         memset(&iter->seq, 0,
3525                sizeof(struct trace_iterator) -
3526                offsetof(struct trace_iterator, seq));
3527         iter->pos = -1;
3528
3529         trace_event_read_lock();
3530         trace_access_lock(iter->cpu_file);
3531         while (trace_find_next_entry_inc(iter) != NULL) {
3532                 enum print_line_t ret;
3533                 int len = iter->seq.len;
3534
3535                 ret = print_trace_line(iter);
3536                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3537                         /* don't print partial lines */
3538                         iter->seq.len = len;
3539                         break;
3540                 }
3541                 if (ret != TRACE_TYPE_NO_CONSUME)
3542                         trace_consume(iter);
3543
3544                 if (iter->seq.len >= cnt)
3545                         break;
3546
3547                 /*
3548                  * Setting the full flag means we reached the trace_seq buffer
3549                  * size and we should leave by partial output condition above.
3550                  * One of the trace_seq_* functions is not used properly.
3551                  */
3552                 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3553                           iter->ent->type);
3554         }
3555         trace_access_unlock(iter->cpu_file);
3556         trace_event_read_unlock();
3557
3558         /* Now copy what we have to the user */
3559         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3560         if (iter->seq.readpos >= iter->seq.len)
3561                 trace_seq_init(&iter->seq);
3562
3563         /*
3564          * If there was nothing to send to user, in spite of consuming trace
3565          * entries, go back to wait for more entries.
3566          */
3567         if (sret == -EBUSY)
3568                 goto waitagain;
3569
3570 out:
3571         mutex_unlock(&iter->mutex);
3572
3573         return sret;
3574 }
3575
3576 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3577                                      struct pipe_buffer *buf)
3578 {
3579         __free_page(buf->page);
3580 }
3581
3582 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3583                                      unsigned int idx)
3584 {
3585         __free_page(spd->pages[idx]);
3586 }
3587
3588 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3589         .can_merge              = 0,
3590         .map                    = generic_pipe_buf_map,
3591         .unmap                  = generic_pipe_buf_unmap,
3592         .confirm                = generic_pipe_buf_confirm,
3593         .release                = tracing_pipe_buf_release,
3594         .steal                  = generic_pipe_buf_steal,
3595         .get                    = generic_pipe_buf_get,
3596 };
3597
3598 static size_t
3599 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3600 {
3601         size_t count;
3602         int ret;
3603
3604         /* Seq buffer is page-sized, exactly what we need. */
3605         for (;;) {
3606                 count = iter->seq.len;
3607                 ret = print_trace_line(iter);
3608                 count = iter->seq.len - count;
3609                 if (rem < count) {
3610                         rem = 0;
3611                         iter->seq.len -= count;
3612                         break;
3613                 }
3614                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3615                         iter->seq.len -= count;
3616                         break;
3617                 }
3618
3619                 if (ret != TRACE_TYPE_NO_CONSUME)
3620                         trace_consume(iter);
3621                 rem -= count;
3622                 if (!trace_find_next_entry_inc(iter))   {
3623                         rem = 0;
3624                         iter->ent = NULL;
3625                         break;
3626                 }
3627         }
3628
3629         return rem;
3630 }
3631
3632 static ssize_t tracing_splice_read_pipe(struct file *filp,
3633                                         loff_t *ppos,
3634                                         struct pipe_inode_info *pipe,
3635                                         size_t len,
3636                                         unsigned int flags)
3637 {
3638         struct page *pages_def[PIPE_DEF_BUFFERS];
3639         struct partial_page partial_def[PIPE_DEF_BUFFERS];
3640         struct trace_iterator *iter = filp->private_data;
3641         struct splice_pipe_desc spd = {
3642                 .pages          = pages_def,
3643                 .partial        = partial_def,
3644                 .nr_pages       = 0, /* This gets updated below. */
3645                 .flags          = flags,
3646                 .ops            = &tracing_pipe_buf_ops,
3647                 .spd_release    = tracing_spd_release_pipe,
3648         };
3649         static struct tracer *old_tracer;
3650         ssize_t ret;
3651         size_t rem;
3652         unsigned int i;
3653
3654         if (splice_grow_spd(pipe, &spd))
3655                 return -ENOMEM;
3656
3657         /* copy the tracer to avoid using a global lock all around */
3658         mutex_lock(&trace_types_lock);
3659         if (unlikely(old_tracer != current_trace && current_trace)) {
3660                 old_tracer = current_trace;
3661                 *iter->trace = *current_trace;
3662         }
3663         mutex_unlock(&trace_types_lock);
3664
3665         mutex_lock(&iter->mutex);
3666
3667         if (iter->trace->splice_read) {
3668                 ret = iter->trace->splice_read(iter, filp,
3669                                                ppos, pipe, len, flags);
3670                 if (ret)
3671                         goto out_err;
3672         }
3673
3674         ret = tracing_wait_pipe(filp);
3675         if (ret <= 0)
3676                 goto out_err;
3677
3678         if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3679                 ret = -EFAULT;
3680                 goto out_err;
3681         }
3682
3683         trace_event_read_lock();
3684         trace_access_lock(iter->cpu_file);
3685
3686         /* Fill as many pages as possible. */
3687         for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3688                 spd.pages[i] = alloc_page(GFP_KERNEL);
3689                 if (!spd.pages[i])
3690                         break;
3691
3692                 rem = tracing_fill_pipe_page(rem, iter);
3693
3694                 /* Copy the data into the page, so we can start over. */
3695                 ret = trace_seq_to_buffer(&iter->seq,
3696                                           page_address(spd.pages[i]),
3697                                           iter->seq.len);
3698                 if (ret < 0) {
3699                         __free_page(spd.pages[i]);
3700                         break;
3701                 }
3702                 spd.partial[i].offset = 0;
3703                 spd.partial[i].len = iter->seq.len;
3704
3705                 trace_seq_init(&iter->seq);
3706         }
3707
3708         trace_access_unlock(iter->cpu_file);
3709         trace_event_read_unlock();
3710         mutex_unlock(&iter->mutex);
3711
3712         spd.nr_pages = i;
3713
3714         ret = splice_to_pipe(pipe, &spd);
3715 out:
3716         splice_shrink_spd(pipe, &spd);
3717         return ret;
3718
3719 out_err:
3720         mutex_unlock(&iter->mutex);
3721         goto out;
3722 }
3723
3724 static ssize_t
3725 tracing_entries_read(struct file *filp, char __user *ubuf,
3726                      size_t cnt, loff_t *ppos)
3727 {
3728         struct trace_array *tr = filp->private_data;
3729         char buf[96];
3730         int r;
3731
3732         mutex_lock(&trace_types_lock);
3733         if (!ring_buffer_expanded)
3734                 r = sprintf(buf, "%lu (expanded: %lu)\n",
3735                             tr->entries >> 10,
3736                             trace_buf_size >> 10);
3737         else
3738                 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3739         mutex_unlock(&trace_types_lock);
3740
3741         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3742 }
3743
3744 static ssize_t
3745 tracing_entries_write(struct file *filp, const char __user *ubuf,
3746                       size_t cnt, loff_t *ppos)
3747 {
3748         unsigned long val;
3749         int ret;
3750
3751         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3752         if (ret)
3753                 return ret;
3754
3755         /* must have at least 1 entry */
3756         if (!val)
3757                 return -EINVAL;
3758
3759         /* value is in KB */
3760         val <<= 10;
3761
3762         ret = tracing_resize_ring_buffer(val);
3763         if (ret < 0)
3764                 return ret;
3765
3766         *ppos += cnt;
3767
3768         return cnt;
3769 }
3770
3771 static ssize_t
3772 tracing_total_entries_read(struct file *filp, char __user *ubuf,
3773                                 size_t cnt, loff_t *ppos)
3774 {
3775         struct trace_array *tr = filp->private_data;
3776         char buf[64];
3777         int r, cpu;
3778         unsigned long size = 0, expanded_size = 0;
3779
3780         mutex_lock(&trace_types_lock);
3781         for_each_tracing_cpu(cpu) {
3782                 size += tr->entries >> 10;
3783                 if (!ring_buffer_expanded)
3784                         expanded_size += trace_buf_size >> 10;
3785         }
3786         if (ring_buffer_expanded)
3787                 r = sprintf(buf, "%lu\n", size);
3788         else
3789                 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3790         mutex_unlock(&trace_types_lock);
3791
3792         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3793 }
3794
3795 static ssize_t
3796 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3797                           size_t cnt, loff_t *ppos)
3798 {
3799         /*
3800          * There is no need to read what the user has written, this function
3801          * is just to make sure that there is no error when "echo" is used
3802          */
3803
3804         *ppos += cnt;
3805
3806         return cnt;
3807 }
3808
3809 static int
3810 tracing_free_buffer_release(struct inode *inode, struct file *filp)
3811 {
3812         /* disable tracing ? */
3813         if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3814                 tracing_off();
3815         /* resize the ring buffer to 0 */
3816         tracing_resize_ring_buffer(0);
3817
3818         return 0;
3819 }
3820
3821 static ssize_t
3822 tracing_mark_write(struct file *filp, const char __user *ubuf,
3823                                         size_t cnt, loff_t *fpos)
3824 {
3825         unsigned long addr = (unsigned long)ubuf;
3826         struct ring_buffer_event *event;
3827         struct ring_buffer *buffer;
3828         struct print_entry *entry;
3829         unsigned long irq_flags;
3830         struct page *pages[2];
3831         int nr_pages = 1;
3832         ssize_t written;
3833         void *page1;
3834         void *page2;
3835         int offset;
3836         int size;
3837         int len;
3838         int ret;
3839
3840         if (tracing_disabled)
3841                 return -EINVAL;
3842
3843         if (cnt > TRACE_BUF_SIZE)
3844                 cnt = TRACE_BUF_SIZE;
3845
3846         /*
3847          * Userspace is injecting traces into the kernel trace buffer.
3848          * We want to be as non intrusive as possible.
3849          * To do so, we do not want to allocate any special buffers
3850          * or take any locks, but instead write the userspace data
3851          * straight into the ring buffer.
3852          *
3853          * First we need to pin the userspace buffer into memory,
3854          * which, most likely it is, because it just referenced it.
3855          * But there's no guarantee that it is. By using get_user_pages_fast()
3856          * and kmap_atomic/kunmap_atomic() we can get access to the
3857          * pages directly. We then write the data directly into the
3858          * ring buffer.
3859          */
3860         BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
3861
3862         /* check if we cross pages */
3863         if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
3864                 nr_pages = 2;
3865
3866         offset = addr & (PAGE_SIZE - 1);
3867         addr &= PAGE_MASK;
3868
3869         ret = get_user_pages_fast(addr, nr_pages, 0, pages);
3870         if (ret < nr_pages) {
3871                 while (--ret >= 0)
3872                         put_page(pages[ret]);
3873                 written = -EFAULT;
3874                 goto out;
3875         }
3876
3877         page1 = kmap_atomic(pages[0]);
3878         if (nr_pages == 2)
3879                 page2 = kmap_atomic(pages[1]);
3880
3881         local_save_flags(irq_flags);
3882         size = sizeof(*entry) + cnt + 2; /* possible \n added */
3883         buffer = global_trace.buffer;
3884         event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3885                                           irq_flags, preempt_count());
3886         if (!event) {
3887                 /* Ring buffer disabled, return as if not open for write */
3888                 written = -EBADF;
3889                 goto out_unlock;
3890         }
3891
3892         entry = ring_buffer_event_data(event);
3893         entry->ip = _THIS_IP_;
3894
3895         if (nr_pages == 2) {
3896                 len = PAGE_SIZE - offset;
3897                 memcpy(&entry->buf, page1 + offset, len);
3898                 memcpy(&entry->buf[len], page2, cnt - len);
3899         } else
3900                 memcpy(&entry->buf, page1 + offset, cnt);
3901
3902         if (entry->buf[cnt - 1] != '\n') {
3903                 entry->buf[cnt] = '\n';
3904                 entry->buf[cnt + 1] = '\0';
3905         } else
3906                 entry->buf[cnt] = '\0';
3907
3908         ring_buffer_unlock_commit(buffer, event);
3909
3910         written = cnt;
3911
3912         *fpos += written;
3913
3914  out_unlock:
3915         if (nr_pages == 2)
3916                 kunmap_atomic(page2);
3917         kunmap_atomic(page1);
3918         while (nr_pages > 0)
3919                 put_page(pages[--nr_pages]);
3920  out:
3921         return written;
3922 }
3923
3924 static int tracing_clock_show(struct seq_file *m, void *v)
3925 {
3926         int i;
3927
3928         for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3929                 seq_printf(m,
3930                         "%s%s%s%s", i ? " " : "",
3931                         i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3932                         i == trace_clock_id ? "]" : "");
3933         seq_putc(m, '\n');
3934
3935         return 0;
3936 }
3937
3938 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3939                                    size_t cnt, loff_t *fpos)
3940 {
3941         char buf[64];
3942         const char *clockstr;
3943         int i;
3944
3945         if (cnt >= sizeof(buf))
3946                 return -EINVAL;
3947
3948         if (copy_from_user(&buf, ubuf, cnt))
3949                 return -EFAULT;
3950
3951         buf[cnt] = 0;
3952
3953         clockstr = strstrip(buf);
3954
3955         for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3956                 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3957                         break;
3958         }
3959         if (i == ARRAY_SIZE(trace_clocks))
3960                 return -EINVAL;
3961
3962         trace_clock_id = i;
3963
3964         mutex_lock(&trace_types_lock);
3965
3966         ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3967         if (max_tr.buffer)
3968                 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3969
3970         mutex_unlock(&trace_types_lock);
3971
3972         *fpos += cnt;
3973
3974         return cnt;
3975 }
3976
3977 static int tracing_clock_open(struct inode *inode, struct file *file)
3978 {
3979         if (tracing_disabled)
3980                 return -ENODEV;
3981         return single_open(file, tracing_clock_show, NULL);
3982 }
3983
3984 static const struct file_operations tracing_max_lat_fops = {
3985         .open           = tracing_open_generic,
3986         .read           = tracing_max_lat_read,
3987         .write          = tracing_max_lat_write,
3988         .llseek         = generic_file_llseek,
3989 };
3990
3991 static const struct file_operations tracing_ctrl_fops = {
3992         .open           = tracing_open_generic,
3993         .read           = tracing_ctrl_read,
3994         .write          = tracing_ctrl_write,
3995         .llseek         = generic_file_llseek,
3996 };
3997
3998 static const struct file_operations set_tracer_fops = {
3999         .open           = tracing_open_generic,
4000         .read           = tracing_set_trace_read,
4001         .write          = tracing_set_trace_write,
4002         .llseek         = generic_file_llseek,
4003 };
4004
4005 static const struct file_operations tracing_pipe_fops = {
4006         .open           = tracing_open_pipe,
4007         .poll           = tracing_poll_pipe,
4008         .read           = tracing_read_pipe,
4009         .splice_read    = tracing_splice_read_pipe,
4010         .release        = tracing_release_pipe,
4011         .llseek         = no_llseek,
4012 };
4013
4014 static const struct file_operations tracing_entries_fops = {
4015         .open           = tracing_open_generic,
4016         .read           = tracing_entries_read,
4017         .write          = tracing_entries_write,
4018         .llseek         = generic_file_llseek,
4019 };
4020
4021 static const struct file_operations tracing_total_entries_fops = {
4022         .open           = tracing_open_generic,
4023         .read           = tracing_total_entries_read,
4024         .llseek         = generic_file_llseek,
4025 };
4026
4027 static const struct file_operations tracing_free_buffer_fops = {
4028         .write          = tracing_free_buffer_write,
4029         .release        = tracing_free_buffer_release,
4030 };
4031
4032 static const struct file_operations tracing_mark_fops = {
4033         .open           = tracing_open_generic,
4034         .write          = tracing_mark_write,
4035         .llseek         = generic_file_llseek,
4036 };
4037
4038 static const struct file_operations trace_clock_fops = {
4039         .open           = tracing_clock_open,
4040         .read           = seq_read,
4041         .llseek         = seq_lseek,
4042         .release        = single_release,
4043         .write          = tracing_clock_write,
4044 };
4045
4046 struct ftrace_buffer_info {
4047         struct trace_array      *tr;
4048         void                    *spare;
4049         int                     cpu;
4050         unsigned int            read;
4051 };
4052
4053 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4054 {
4055         int cpu = (int)(long)inode->i_private;
4056         struct ftrace_buffer_info *info;
4057
4058         if (tracing_disabled)
4059                 return -ENODEV;
4060
4061         info = kzalloc(sizeof(*info), GFP_KERNEL);
4062         if (!info)
4063                 return -ENOMEM;
4064
4065         info->tr        = &global_trace;
4066         info->cpu       = cpu;
4067         info->spare     = NULL;
4068         /* Force reading ring buffer for first read */
4069         info->read      = (unsigned int)-1;
4070
4071         filp->private_data = info;
4072
4073         return nonseekable_open(inode, filp);
4074 }
4075
4076 static ssize_t
4077 tracing_buffers_read(struct file *filp, char __user *ubuf,
4078                      size_t count, loff_t *ppos)
4079 {
4080         struct ftrace_buffer_info *info = filp->private_data;
4081         ssize_t ret;
4082         size_t size;
4083
4084         if (!count)
4085                 return 0;
4086
4087         if (!info->spare)
4088                 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
4089         if (!info->spare)
4090                 return -ENOMEM;
4091
4092         /* Do we have previous read data to read? */
4093         if (info->read < PAGE_SIZE)
4094                 goto read;
4095
4096         trace_access_lock(info->cpu);
4097         ret = ring_buffer_read_page(info->tr->buffer,
4098                                     &info->spare,
4099                                     count,
4100                                     info->cpu, 0);
4101         trace_access_unlock(info->cpu);
4102         if (ret < 0)
4103                 return 0;
4104
4105         info->read = 0;
4106
4107 read:
4108         size = PAGE_SIZE - info->read;
4109         if (size > count)
4110                 size = count;
4111
4112         ret = copy_to_user(ubuf, info->spare + info->read, size);
4113         if (ret == size)
4114                 return -EFAULT;
4115         size -= ret;
4116
4117         *ppos += size;
4118         info->read += size;
4119
4120         return size;
4121 }
4122
4123 static int tracing_buffers_release(struct inode *inode, struct file *file)
4124 {
4125         struct ftrace_buffer_info *info = file->private_data;
4126
4127         if (info->spare)
4128                 ring_buffer_free_read_page(info->tr->buffer, info->spare);
4129         kfree(info);
4130
4131         return 0;
4132 }
4133
4134 struct buffer_ref {
4135         struct ring_buffer      *buffer;
4136         void                    *page;
4137         int                     ref;
4138 };
4139
4140 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4141                                     struct pipe_buffer *buf)
4142 {
4143         struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4144
4145         if (--ref->ref)
4146                 return;
4147
4148         ring_buffer_free_read_page(ref->buffer, ref->page);
4149         kfree(ref);
4150         buf->private = 0;
4151 }
4152
4153 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
4154                                  struct pipe_buffer *buf)
4155 {
4156         return 1;
4157 }
4158
4159 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4160                                 struct pipe_buffer *buf)
4161 {
4162         struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4163
4164         ref->ref++;
4165 }
4166
4167 /* Pipe buffer operations for a buffer. */
4168 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4169         .can_merge              = 0,
4170         .map                    = generic_pipe_buf_map,
4171         .unmap                  = generic_pipe_buf_unmap,
4172         .confirm                = generic_pipe_buf_confirm,
4173         .release                = buffer_pipe_buf_release,
4174         .steal                  = buffer_pipe_buf_steal,
4175         .get                    = buffer_pipe_buf_get,
4176 };
4177
4178 /*
4179  * Callback from splice_to_pipe(), if we need to release some pages
4180  * at the end of the spd in case we error'ed out in filling the pipe.
4181  */
4182 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4183 {
4184         struct buffer_ref *ref =
4185                 (struct buffer_ref *)spd->partial[i].private;
4186
4187         if (--ref->ref)
4188                 return;
4189
4190         ring_buffer_free_read_page(ref->buffer, ref->page);
4191         kfree(ref);
4192         spd->partial[i].private = 0;
4193 }
4194
4195 static ssize_t
4196 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4197                             struct pipe_inode_info *pipe, size_t len,
4198                             unsigned int flags)
4199 {
4200         struct ftrace_buffer_info *info = file->private_data;
4201         struct partial_page partial_def[PIPE_DEF_BUFFERS];
4202         struct page *pages_def[PIPE_DEF_BUFFERS];
4203         struct splice_pipe_desc spd = {
4204                 .pages          = pages_def,
4205                 .partial        = partial_def,
4206                 .flags          = flags,
4207                 .ops            = &buffer_pipe_buf_ops,
4208                 .spd_release    = buffer_spd_release,
4209         };
4210         struct buffer_ref *ref;
4211         int entries, size, i;
4212         size_t ret;
4213
4214         if (splice_grow_spd(pipe, &spd))
4215                 return -ENOMEM;
4216
4217         if (*ppos & (PAGE_SIZE - 1)) {
4218                 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
4219                 ret = -EINVAL;
4220                 goto out;
4221         }
4222
4223         if (len & (PAGE_SIZE - 1)) {
4224                 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
4225                 if (len < PAGE_SIZE) {
4226                         ret = -EINVAL;
4227                         goto out;
4228                 }
4229                 len &= PAGE_MASK;
4230         }
4231
4232         trace_access_lock(info->cpu);
4233         entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4234
4235         for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4236                 struct page *page;
4237                 int r;
4238
4239                 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4240                 if (!ref)
4241                         break;
4242
4243                 ref->ref = 1;
4244                 ref->buffer = info->tr->buffer;
4245                 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
4246                 if (!ref->page) {
4247                         kfree(ref);
4248                         break;
4249                 }
4250
4251                 r = ring_buffer_read_page(ref->buffer, &ref->page,
4252                                           len, info->cpu, 1);
4253                 if (r < 0) {
4254                         ring_buffer_free_read_page(ref->buffer, ref->page);
4255                         kfree(ref);
4256                         break;
4257                 }
4258
4259                 /*
4260                  * zero out any left over data, this is going to
4261                  * user land.
4262                  */
4263                 size = ring_buffer_page_len(ref->page);
4264                 if (size < PAGE_SIZE)
4265                         memset(ref->page + size, 0, PAGE_SIZE - size);
4266
4267                 page = virt_to_page(ref->page);
4268
4269                 spd.pages[i] = page;
4270                 spd.partial[i].len = PAGE_SIZE;
4271                 spd.partial[i].offset = 0;
4272                 spd.partial[i].private = (unsigned long)ref;
4273                 spd.nr_pages++;
4274                 *ppos += PAGE_SIZE;
4275
4276                 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4277         }
4278
4279         trace_access_unlock(info->cpu);
4280         spd.nr_pages = i;
4281
4282         /* did we read anything? */
4283         if (!spd.nr_pages) {
4284                 if (flags & SPLICE_F_NONBLOCK)
4285                         ret = -EAGAIN;
4286                 else
4287                         ret = 0;
4288                 /* TODO: block */
4289                 goto out;
4290         }
4291
4292         ret = splice_to_pipe(pipe, &spd);
4293         splice_shrink_spd(pipe, &spd);
4294 out:
4295         return ret;
4296 }
4297
4298 static const struct file_operations tracing_buffers_fops = {
4299         .open           = tracing_buffers_open,
4300         .read           = tracing_buffers_read,
4301         .release        = tracing_buffers_release,
4302         .splice_read    = tracing_buffers_splice_read,
4303         .llseek         = no_llseek,
4304 };
4305
4306 static ssize_t
4307 tracing_stats_read(struct file *filp, char __user *ubuf,
4308                    size_t count, loff_t *ppos)
4309 {
4310         unsigned long cpu = (unsigned long)filp->private_data;
4311         struct trace_array *tr = &global_trace;
4312         struct trace_seq *s;
4313         unsigned long cnt;
4314         unsigned long long t;
4315         unsigned long usec_rem;
4316
4317         s = kmalloc(sizeof(*s), GFP_KERNEL);
4318         if (!s)
4319                 return -ENOMEM;
4320
4321         trace_seq_init(s);
4322
4323         cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4324         trace_seq_printf(s, "entries: %ld\n", cnt);
4325
4326         cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4327         trace_seq_printf(s, "overrun: %ld\n", cnt);
4328
4329         cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4330         trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4331
4332         cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4333         trace_seq_printf(s, "bytes: %ld\n", cnt);
4334
4335         t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4336         usec_rem = do_div(t, USEC_PER_SEC);
4337         trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", t, usec_rem);
4338
4339         t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4340         usec_rem = do_div(t, USEC_PER_SEC);
4341         trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4342
4343         count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4344
4345         kfree(s);
4346
4347         return count;
4348 }
4349
4350 static const struct file_operations tracing_stats_fops = {
4351         .open           = tracing_open_generic,
4352         .read           = tracing_stats_read,
4353         .llseek         = generic_file_llseek,
4354 };
4355
4356 #ifdef CONFIG_DYNAMIC_FTRACE
4357
4358 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4359 {
4360         return 0;
4361 }
4362
4363 static ssize_t
4364 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
4365                   size_t cnt, loff_t *ppos)
4366 {
4367         static char ftrace_dyn_info_buffer[1024];
4368         static DEFINE_MUTEX(dyn_info_mutex);
4369         unsigned long *p = filp->private_data;
4370         char *buf = ftrace_dyn_info_buffer;
4371         int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
4372         int r;
4373
4374         mutex_lock(&dyn_info_mutex);
4375         r = sprintf(buf, "%ld ", *p);
4376
4377         r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
4378         buf[r++] = '\n';
4379
4380         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4381
4382         mutex_unlock(&dyn_info_mutex);
4383
4384         return r;
4385 }
4386
4387 static const struct file_operations tracing_dyn_info_fops = {
4388         .open           = tracing_open_generic,
4389         .read           = tracing_read_dyn_info,
4390         .llseek         = generic_file_llseek,
4391 };
4392 #endif
4393
4394 static struct dentry *d_tracer;
4395
4396 struct dentry *tracing_init_dentry(void)
4397 {
4398         static int once;
4399
4400         if (d_tracer)
4401                 return d_tracer;
4402
4403         if (!debugfs_initialized())
4404                 return NULL;
4405
4406         d_tracer = debugfs_create_dir("tracing", NULL);
4407
4408         if (!d_tracer && !once) {
4409                 once = 1;
4410                 pr_warning("Could not create debugfs directory 'tracing'\n");
4411                 return NULL;
4412         }
4413
4414         return d_tracer;
4415 }
4416
4417 static struct dentry *d_percpu;
4418
4419 struct dentry *tracing_dentry_percpu(void)
4420 {
4421         static int once;
4422         struct dentry *d_tracer;
4423
4424         if (d_percpu)
4425                 return d_percpu;
4426
4427         d_tracer = tracing_init_dentry();
4428
4429         if (!d_tracer)
4430                 return NULL;
4431
4432         d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4433
4434         if (!d_percpu && !once) {
4435                 once = 1;
4436                 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4437                 return NULL;
4438         }
4439
4440         return d_percpu;
4441 }
4442
4443 static void tracing_init_debugfs_percpu(long cpu)
4444 {
4445         struct dentry *d_percpu = tracing_dentry_percpu();
4446         struct dentry *d_cpu;
4447         char cpu_dir[30]; /* 30 characters should be more than enough */
4448
4449         snprintf(cpu_dir, 30, "cpu%ld", cpu);
4450         d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4451         if (!d_cpu) {
4452                 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4453                 return;
4454         }
4455
4456         /* per cpu trace_pipe */
4457         trace_create_file("trace_pipe", 0444, d_cpu,
4458                         (void *) cpu, &tracing_pipe_fops);
4459
4460         /* per cpu trace */
4461         trace_create_file("trace", 0644, d_cpu,
4462                         (void *) cpu, &tracing_fops);
4463
4464         trace_create_file("trace_pipe_raw", 0444, d_cpu,
4465                         (void *) cpu, &tracing_buffers_fops);
4466
4467         trace_create_file("stats", 0444, d_cpu,
4468                         (void *) cpu, &tracing_stats_fops);
4469 }
4470
4471 #ifdef CONFIG_FTRACE_SELFTEST
4472 /* Let selftest have access to static functions in this file */
4473 #include "trace_selftest.c"
4474 #endif
4475
4476 struct trace_option_dentry {
4477         struct tracer_opt               *opt;
4478         struct tracer_flags             *flags;
4479         struct dentry                   *entry;
4480 };
4481
4482 static ssize_t
4483 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4484                         loff_t *ppos)
4485 {
4486         struct trace_option_dentry *topt = filp->private_data;
4487         char *buf;
4488
4489         if (topt->flags->val & topt->opt->bit)
4490                 buf = "1\n";
4491         else
4492                 buf = "0\n";
4493
4494         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4495 }
4496
4497 static ssize_t
4498 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4499                          loff_t *ppos)
4500 {
4501         struct trace_option_dentry *topt = filp->private_data;
4502         unsigned long val;
4503         int ret;
4504
4505         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4506         if (ret)
4507                 return ret;
4508
4509         if (val != 0 && val != 1)
4510                 return -EINVAL;
4511
4512         if (!!(topt->flags->val & topt->opt->bit) != val) {
4513                 mutex_lock(&trace_types_lock);
4514                 ret = __set_tracer_option(current_trace, topt->flags,
4515                                           topt->opt, !val);
4516                 mutex_unlock(&trace_types_lock);
4517                 if (ret)
4518                         return ret;
4519         }
4520
4521         *ppos += cnt;
4522
4523         return cnt;
4524 }
4525
4526
4527 static const struct file_operations trace_options_fops = {
4528         .open = tracing_open_generic,
4529         .read = trace_options_read,
4530         .write = trace_options_write,
4531         .llseek = generic_file_llseek,
4532 };
4533
4534 static ssize_t
4535 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4536                         loff_t *ppos)
4537 {
4538         long index = (long)filp->private_data;
4539         char *buf;
4540
4541         if (trace_flags & (1 << index))
4542                 buf = "1\n";
4543         else
4544                 buf = "0\n";
4545
4546         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4547 }
4548
4549 static ssize_t
4550 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4551                          loff_t *ppos)
4552 {
4553         long index = (long)filp->private_data;
4554         unsigned long val;
4555         int ret;
4556
4557         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4558         if (ret)
4559                 return ret;
4560
4561         if (val != 0 && val != 1)
4562                 return -EINVAL;
4563         set_tracer_flags(1 << index, val);
4564
4565         *ppos += cnt;
4566
4567         return cnt;
4568 }
4569
4570 static const struct file_operations trace_options_core_fops = {
4571         .open = tracing_open_generic,
4572         .read = trace_options_core_read,
4573         .write = trace_options_core_write,
4574         .llseek = generic_file_llseek,
4575 };
4576
4577 struct dentry *trace_create_file(const char *name,
4578                                  umode_t mode,
4579                                  struct dentry *parent,
4580                                  void *data,
4581                                  const struct file_operations *fops)
4582 {
4583         struct dentry *ret;
4584
4585         ret = debugfs_create_file(name, mode, parent, data, fops);
4586         if (!ret)
4587                 pr_warning("Could not create debugfs '%s' entry\n", name);
4588
4589         return ret;
4590 }
4591
4592
4593 static struct dentry *trace_options_init_dentry(void)
4594 {
4595         struct dentry *d_tracer;
4596         static struct dentry *t_options;
4597
4598         if (t_options)
4599                 return t_options;
4600
4601         d_tracer = tracing_init_dentry();
4602         if (!d_tracer)
4603                 return NULL;
4604
4605         t_options = debugfs_create_dir("options", d_tracer);
4606         if (!t_options) {
4607                 pr_warning("Could not create debugfs directory 'options'\n");
4608                 return NULL;
4609         }
4610
4611         return t_options;
4612 }
4613
4614 static void
4615 create_trace_option_file(struct trace_option_dentry *topt,
4616                          struct tracer_flags *flags,
4617                          struct tracer_opt *opt)
4618 {
4619         struct dentry *t_options;
4620
4621         t_options = trace_options_init_dentry();
4622         if (!t_options)
4623                 return;
4624
4625         topt->flags = flags;
4626         topt->opt = opt;
4627
4628         topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4629                                     &trace_options_fops);
4630
4631 }
4632
4633 static struct trace_option_dentry *
4634 create_trace_option_files(struct tracer *tracer)
4635 {
4636         struct trace_option_dentry *topts;
4637         struct tracer_flags *flags;
4638         struct tracer_opt *opts;
4639         int cnt;
4640
4641         if (!tracer)
4642                 return NULL;
4643
4644         flags = tracer->flags;
4645
4646         if (!flags || !flags->opts)
4647                 return NULL;
4648
4649         opts = flags->opts;
4650
4651         for (cnt = 0; opts[cnt].name; cnt++)
4652                 ;
4653
4654         topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4655         if (!topts)
4656                 return NULL;
4657
4658         for (cnt = 0; opts[cnt].name; cnt++)
4659                 create_trace_option_file(&topts[cnt], flags,
4660                                          &opts[cnt]);
4661
4662         return topts;
4663 }
4664
4665 static void
4666 destroy_trace_option_files(struct trace_option_dentry *topts)
4667 {
4668         int cnt;
4669
4670         if (!topts)
4671                 return;
4672
4673         for (cnt = 0; topts[cnt].opt; cnt++) {
4674                 if (topts[cnt].entry)
4675                         debugfs_remove(topts[cnt].entry);
4676         }
4677
4678         kfree(topts);
4679 }
4680
4681 static struct dentry *
4682 create_trace_option_core_file(const char *option, long index)
4683 {
4684         struct dentry *t_options;
4685
4686         t_options = trace_options_init_dentry();
4687         if (!t_options)
4688                 return NULL;
4689
4690         return trace_create_file(option, 0644, t_options, (void *)index,
4691                                     &trace_options_core_fops);
4692 }
4693
4694 static __init void create_trace_options_dir(void)
4695 {
4696         struct dentry *t_options;
4697         int i;
4698
4699         t_options = trace_options_init_dentry();
4700         if (!t_options)
4701                 return;
4702
4703         for (i = 0; trace_options[i]; i++)
4704                 create_trace_option_core_file(trace_options[i], i);
4705 }
4706
4707 static ssize_t
4708 rb_simple_read(struct file *filp, char __user *ubuf,
4709                size_t cnt, loff_t *ppos)
4710 {
4711         struct ring_buffer *buffer = filp->private_data;
4712         char buf[64];
4713         int r;
4714
4715         if (buffer)
4716                 r = ring_buffer_record_is_on(buffer);
4717         else
4718                 r = 0;
4719
4720         r = sprintf(buf, "%d\n", r);
4721
4722         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4723 }
4724
4725 static ssize_t
4726 rb_simple_write(struct file *filp, const char __user *ubuf,
4727                 size_t cnt, loff_t *ppos)
4728 {
4729         struct ring_buffer *buffer = filp->private_data;
4730         unsigned long val;
4731         int ret;
4732
4733         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4734         if (ret)
4735                 return ret;
4736
4737         if (buffer) {
4738                 if (val)
4739                         ring_buffer_record_on(buffer);
4740                 else
4741                         ring_buffer_record_off(buffer);
4742         }
4743
4744         (*ppos)++;
4745
4746         return cnt;
4747 }
4748
4749 static const struct file_operations rb_simple_fops = {
4750         .open           = tracing_open_generic,
4751         .read           = rb_simple_read,
4752         .write          = rb_simple_write,
4753         .llseek         = default_llseek,
4754 };
4755
4756 static __init int tracer_init_debugfs(void)
4757 {
4758         struct dentry *d_tracer;
4759         int cpu;
4760
4761         trace_access_lock_init();
4762
4763         d_tracer = tracing_init_dentry();
4764
4765         trace_create_file("tracing_enabled", 0644, d_tracer,
4766                         &global_trace, &tracing_ctrl_fops);
4767
4768         trace_create_file("trace_options", 0644, d_tracer,
4769                         NULL, &tracing_iter_fops);
4770
4771         trace_create_file("tracing_cpumask", 0644, d_tracer,
4772                         NULL, &tracing_cpumask_fops);
4773
4774         trace_create_file("trace", 0644, d_tracer,
4775                         (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4776
4777         trace_create_file("available_tracers", 0444, d_tracer,
4778                         &global_trace, &show_traces_fops);
4779
4780         trace_create_file("current_tracer", 0644, d_tracer,
4781                         &global_trace, &set_tracer_fops);
4782
4783 #ifdef CONFIG_TRACER_MAX_TRACE
4784         trace_create_file("tracing_max_latency", 0644, d_tracer,
4785                         &tracing_max_latency, &tracing_max_lat_fops);
4786 #endif
4787
4788         trace_create_file("tracing_thresh", 0644, d_tracer,
4789                         &tracing_thresh, &tracing_max_lat_fops);
4790
4791         trace_create_file("README", 0444, d_tracer,
4792                         NULL, &tracing_readme_fops);
4793
4794         trace_create_file("trace_pipe", 0444, d_tracer,
4795                         (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4796
4797         trace_create_file("buffer_size_kb", 0644, d_tracer,
4798                         &global_trace, &tracing_entries_fops);
4799
4800         trace_create_file("buffer_total_size_kb", 0444, d_tracer,
4801                         &global_trace, &tracing_total_entries_fops);
4802
4803         trace_create_file("free_buffer", 0644, d_tracer,
4804                         &global_trace, &tracing_free_buffer_fops);
4805
4806         trace_create_file("trace_marker", 0220, d_tracer,
4807                         NULL, &tracing_mark_fops);
4808
4809         trace_create_file("saved_cmdlines", 0444, d_tracer,
4810                         NULL, &tracing_saved_cmdlines_fops);
4811
4812         trace_create_file("trace_clock", 0644, d_tracer, NULL,
4813                           &trace_clock_fops);
4814
4815         trace_create_file("tracing_on", 0644, d_tracer,
4816                             global_trace.buffer, &rb_simple_fops);
4817
4818 #ifdef CONFIG_DYNAMIC_FTRACE
4819         trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4820                         &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4821 #endif
4822
4823         create_trace_options_dir();
4824
4825         for_each_tracing_cpu(cpu)
4826                 tracing_init_debugfs_percpu(cpu);
4827
4828         return 0;
4829 }
4830
4831 static int trace_panic_handler(struct notifier_block *this,
4832                                unsigned long event, void *unused)
4833 {
4834         if (ftrace_dump_on_oops)
4835                 ftrace_dump(ftrace_dump_on_oops);
4836         return NOTIFY_OK;
4837 }
4838
4839 static struct notifier_block trace_panic_notifier = {
4840         .notifier_call  = trace_panic_handler,
4841         .next           = NULL,
4842         .priority       = 150   /* priority: INT_MAX >= x >= 0 */
4843 };
4844
4845 static int trace_die_handler(struct notifier_block *self,
4846                              unsigned long val,
4847                              void *data)
4848 {
4849         switch (val) {
4850         case DIE_OOPS:
4851                 if (ftrace_dump_on_oops)
4852                         ftrace_dump(ftrace_dump_on_oops);
4853                 break;
4854         default:
4855                 break;
4856         }
4857         return NOTIFY_OK;
4858 }
4859
4860 static struct notifier_block trace_die_notifier = {
4861         .notifier_call = trace_die_handler,
4862         .priority = 200
4863 };
4864
4865 /*
4866  * printk is set to max of 1024, we really don't need it that big.
4867  * Nothing should be printing 1000 characters anyway.
4868  */
4869 #define TRACE_MAX_PRINT         1000
4870
4871 /*
4872  * Define here KERN_TRACE so that we have one place to modify
4873  * it if we decide to change what log level the ftrace dump
4874  * should be at.
4875  */
4876 #define KERN_TRACE              KERN_EMERG
4877
4878 void
4879 trace_printk_seq(struct trace_seq *s)
4880 {
4881         /* Probably should print a warning here. */
4882         if (s->len >= 1000)
4883                 s->len = 1000;
4884
4885         /* should be zero ended, but we are paranoid. */
4886         s->buffer[s->len] = 0;
4887
4888         printk(KERN_TRACE "%s", s->buffer);
4889
4890         trace_seq_init(s);
4891 }
4892
4893 void trace_init_global_iter(struct trace_iterator *iter)
4894 {
4895         iter->tr = &global_trace;
4896         iter->trace = current_trace;
4897         iter->cpu_file = TRACE_PIPE_ALL_CPU;
4898 }
4899
4900 static void
4901 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
4902 {
4903         static arch_spinlock_t ftrace_dump_lock =
4904                 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
4905         /* use static because iter can be a bit big for the stack */
4906         static struct trace_iterator iter;
4907         unsigned int old_userobj;
4908         static int dump_ran;
4909         unsigned long flags;
4910         int cnt = 0, cpu;
4911
4912         /* only one dump */
4913         local_irq_save(flags);
4914         arch_spin_lock(&ftrace_dump_lock);
4915         if (dump_ran)
4916                 goto out;
4917
4918         dump_ran = 1;
4919
4920         tracing_off();
4921
4922         /* Did function tracer already get disabled? */
4923         if (ftrace_is_dead()) {
4924                 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
4925                 printk("#          MAY BE MISSING FUNCTION EVENTS\n");
4926         }
4927
4928         if (disable_tracing)
4929                 ftrace_kill();
4930
4931         trace_init_global_iter(&iter);
4932
4933         for_each_tracing_cpu(cpu) {
4934                 atomic_inc(&iter.tr->data[cpu]->disabled);
4935         }
4936
4937         old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4938
4939         /* don't look at user memory in panic mode */
4940         trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4941
4942         /* Simulate the iterator */
4943         iter.tr = &global_trace;
4944         iter.trace = current_trace;
4945
4946         switch (oops_dump_mode) {
4947         case DUMP_ALL:
4948                 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4949                 break;
4950         case DUMP_ORIG:
4951                 iter.cpu_file = raw_smp_processor_id();
4952                 break;
4953         case DUMP_NONE:
4954                 goto out_enable;
4955         default:
4956                 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4957                 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4958         }
4959
4960         printk(KERN_TRACE "Dumping ftrace buffer:\n");
4961
4962         /*
4963          * We need to stop all tracing on all CPUS to read the
4964          * the next buffer. This is a bit expensive, but is
4965          * not done often. We fill all what we can read,
4966          * and then release the locks again.
4967          */
4968
4969         while (!trace_empty(&iter)) {
4970
4971                 if (!cnt)
4972                         printk(KERN_TRACE "---------------------------------\n");
4973
4974                 cnt++;
4975
4976                 /* reset all but tr, trace, and overruns */
4977                 memset(&iter.seq, 0,
4978                        sizeof(struct trace_iterator) -
4979                        offsetof(struct trace_iterator, seq));
4980                 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4981                 iter.pos = -1;
4982
4983                 if (trace_find_next_entry_inc(&iter) != NULL) {
4984                         int ret;
4985
4986                         ret = print_trace_line(&iter);
4987                         if (ret != TRACE_TYPE_NO_CONSUME)
4988                                 trace_consume(&iter);
4989                 }
4990                 touch_nmi_watchdog();
4991
4992                 trace_printk_seq(&iter.seq);
4993         }
4994
4995         if (!cnt)
4996                 printk(KERN_TRACE "   (ftrace buffer empty)\n");
4997         else
4998                 printk(KERN_TRACE "---------------------------------\n");
4999
5000  out_enable:
5001         /* Re-enable tracing if requested */
5002         if (!disable_tracing) {
5003                 trace_flags |= old_userobj;
5004
5005                 for_each_tracing_cpu(cpu) {
5006                         atomic_dec(&iter.tr->data[cpu]->disabled);
5007                 }
5008                 tracing_on();
5009         }
5010
5011  out:
5012         arch_spin_unlock(&ftrace_dump_lock);
5013         local_irq_restore(flags);
5014 }
5015
5016 /* By default: disable tracing after the dump */
5017 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
5018 {
5019         __ftrace_dump(true, oops_dump_mode);
5020 }
5021 EXPORT_SYMBOL_GPL(ftrace_dump);
5022
5023 __init static int tracer_alloc_buffers(void)
5024 {
5025         int ring_buf_size;
5026         enum ring_buffer_flags rb_flags;
5027         int i;
5028         int ret = -ENOMEM;
5029
5030
5031         if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
5032                 goto out;
5033
5034         if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
5035                 goto out_free_buffer_mask;
5036
5037         /* Only allocate trace_printk buffers if a trace_printk exists */
5038         if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
5039                 trace_printk_init_buffers();
5040
5041         /* To save memory, keep the ring buffer size to its minimum */
5042         if (ring_buffer_expanded)
5043                 ring_buf_size = trace_buf_size;
5044         else
5045                 ring_buf_size = 1;
5046
5047         rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5048
5049         cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
5050         cpumask_copy(tracing_cpumask, cpu_all_mask);
5051
5052         /* TODO: make the number of buffers hot pluggable with CPUS */
5053         global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
5054         if (!global_trace.buffer) {
5055                 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
5056                 WARN_ON(1);
5057                 goto out_free_cpumask;
5058         }
5059         global_trace.entries = ring_buffer_size(global_trace.buffer);
5060         if (global_trace.buffer_disabled)
5061                 tracing_off();
5062
5063
5064 #ifdef CONFIG_TRACER_MAX_TRACE
5065         max_tr.buffer = ring_buffer_alloc(1, rb_flags);
5066         if (!max_tr.buffer) {
5067                 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
5068                 WARN_ON(1);
5069                 ring_buffer_free(global_trace.buffer);
5070                 goto out_free_cpumask;
5071         }
5072         max_tr.entries = 1;
5073 #endif
5074
5075         /* Allocate the first page for all buffers */
5076         for_each_tracing_cpu(i) {
5077                 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
5078                 max_tr.data[i] = &per_cpu(max_tr_data, i);
5079         }
5080
5081         trace_init_cmdlines();
5082
5083         register_tracer(&nop_trace);
5084         current_trace = &nop_trace;
5085         /* All seems OK, enable tracing */
5086         tracing_disabled = 0;
5087
5088         atomic_notifier_chain_register(&panic_notifier_list,
5089                                        &trace_panic_notifier);
5090
5091         register_die_notifier(&trace_die_notifier);
5092
5093         return 0;
5094
5095 out_free_cpumask:
5096         free_cpumask_var(tracing_cpumask);
5097 out_free_buffer_mask:
5098         free_cpumask_var(tracing_buffer_mask);
5099 out:
5100         return ret;
5101 }
5102
5103 __init static int clear_boot_tracer(void)
5104 {
5105         /*
5106          * The default tracer at boot buffer is an init section.
5107          * This function is called in lateinit. If we did not
5108          * find the boot tracer, then clear it out, to prevent
5109          * later registration from accessing the buffer that is
5110          * about to be freed.
5111          */
5112         if (!default_bootup_tracer)
5113                 return 0;
5114
5115         printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
5116                default_bootup_tracer);
5117         default_bootup_tracer = NULL;
5118
5119         return 0;
5120 }
5121
5122 early_initcall(tracer_alloc_buffers);
5123 fs_initcall(tracer_init_debugfs);
5124 late_initcall(clear_boot_tracer);