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