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