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