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