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