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