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