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