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