Merge branches 'tracing/ftrace', 'tracing/function-graph-tracer' and 'tracing/urgent...
[pandora-kernel.git] / kernel / trace / trace.c
1 /*
2  * ring buffer based function tracer
3  *
4  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6  *
7  * Originally taken from the RT patch by:
8  *    Arnaldo Carvalho de Melo <acme@redhat.com>
9  *
10  * Based on code from the latency_tracer, that is:
11  *  Copyright (C) 2004-2006 Ingo Molnar
12  *  Copyright (C) 2004 William Lee Irwin III
13  */
14 #include <linux/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  * ftrace_off_permanent - disable all ftrace code permanently
690  *
691  * This should only be called when a serious anomally has
692  * been detected.  This will turn off the function tracing,
693  * ring buffers, and other tracing utilites. It takes no
694  * locks and can be called from any context.
695  */
696 void ftrace_off_permanent(void)
697 {
698         tracing_disabled = 1;
699         ftrace_stop();
700         tracing_off_permanent();
701 }
702
703 /**
704  * tracing_start - quick start of the tracer
705  *
706  * If tracing is enabled but was stopped by tracing_stop,
707  * this will start the tracer back up.
708  */
709 void tracing_start(void)
710 {
711         struct ring_buffer *buffer;
712         unsigned long flags;
713
714         if (tracing_disabled)
715                 return;
716
717         spin_lock_irqsave(&tracing_start_lock, flags);
718         if (--trace_stop_count)
719                 goto out;
720
721         if (trace_stop_count < 0) {
722                 /* Someone screwed up their debugging */
723                 WARN_ON_ONCE(1);
724                 trace_stop_count = 0;
725                 goto out;
726         }
727
728
729         buffer = global_trace.buffer;
730         if (buffer)
731                 ring_buffer_record_enable(buffer);
732
733         buffer = max_tr.buffer;
734         if (buffer)
735                 ring_buffer_record_enable(buffer);
736
737         ftrace_start();
738  out:
739         spin_unlock_irqrestore(&tracing_start_lock, flags);
740 }
741
742 /**
743  * tracing_stop - quick stop of the tracer
744  *
745  * Light weight way to stop tracing. Use in conjunction with
746  * tracing_start.
747  */
748 void tracing_stop(void)
749 {
750         struct ring_buffer *buffer;
751         unsigned long flags;
752
753         ftrace_stop();
754         spin_lock_irqsave(&tracing_start_lock, flags);
755         if (trace_stop_count++)
756                 goto out;
757
758         buffer = global_trace.buffer;
759         if (buffer)
760                 ring_buffer_record_disable(buffer);
761
762         buffer = max_tr.buffer;
763         if (buffer)
764                 ring_buffer_record_disable(buffer);
765
766  out:
767         spin_unlock_irqrestore(&tracing_start_lock, flags);
768 }
769
770 void trace_stop_cmdline_recording(void);
771
772 static void trace_save_cmdline(struct task_struct *tsk)
773 {
774         unsigned map;
775         unsigned idx;
776
777         if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
778                 return;
779
780         /*
781          * It's not the end of the world if we don't get
782          * the lock, but we also don't want to spin
783          * nor do we want to disable interrupts,
784          * so if we miss here, then better luck next time.
785          */
786         if (!spin_trylock(&trace_cmdline_lock))
787                 return;
788
789         idx = map_pid_to_cmdline[tsk->pid];
790         if (idx >= SAVED_CMDLINES) {
791                 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
792
793                 map = map_cmdline_to_pid[idx];
794                 if (map <= PID_MAX_DEFAULT)
795                         map_pid_to_cmdline[map] = (unsigned)-1;
796
797                 map_pid_to_cmdline[tsk->pid] = idx;
798
799                 cmdline_idx = idx;
800         }
801
802         memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
803
804         spin_unlock(&trace_cmdline_lock);
805 }
806
807 char *trace_find_cmdline(int pid)
808 {
809         char *cmdline = "<...>";
810         unsigned map;
811
812         if (!pid)
813                 return "<idle>";
814
815         if (pid > PID_MAX_DEFAULT)
816                 goto out;
817
818         map = map_pid_to_cmdline[pid];
819         if (map >= SAVED_CMDLINES)
820                 goto out;
821
822         cmdline = saved_cmdlines[map];
823
824  out:
825         return cmdline;
826 }
827
828 void tracing_record_cmdline(struct task_struct *tsk)
829 {
830         if (atomic_read(&trace_record_cmdline_disabled))
831                 return;
832
833         trace_save_cmdline(tsk);
834 }
835
836 void
837 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
838                              int pc)
839 {
840         struct task_struct *tsk = current;
841
842         entry->preempt_count            = pc & 0xff;
843         entry->pid                      = (tsk) ? tsk->pid : 0;
844         entry->tgid                     = (tsk) ? tsk->tgid : 0;
845         entry->flags =
846 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
847                 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
848 #else
849                 TRACE_FLAG_IRQS_NOSUPPORT |
850 #endif
851                 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
852                 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
853                 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
854 }
855
856 void
857 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
858                unsigned long ip, unsigned long parent_ip, unsigned long flags,
859                int pc)
860 {
861         struct ring_buffer_event *event;
862         struct ftrace_entry *entry;
863         unsigned long irq_flags;
864
865         /* If we are reading the ring buffer, don't trace */
866         if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
867                 return;
868
869         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
870                                          &irq_flags);
871         if (!event)
872                 return;
873         entry   = ring_buffer_event_data(event);
874         tracing_generic_entry_update(&entry->ent, flags, pc);
875         entry->ent.type                 = TRACE_FN;
876         entry->ip                       = ip;
877         entry->parent_ip                = parent_ip;
878         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
879 }
880
881 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
882 static void __trace_graph_entry(struct trace_array *tr,
883                                 struct trace_array_cpu *data,
884                                 struct ftrace_graph_ent *trace,
885                                 unsigned long flags,
886                                 int pc)
887 {
888         struct ring_buffer_event *event;
889         struct ftrace_graph_ent_entry *entry;
890         unsigned long irq_flags;
891
892         if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
893                 return;
894
895         event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
896                                          &irq_flags);
897         if (!event)
898                 return;
899         entry   = ring_buffer_event_data(event);
900         tracing_generic_entry_update(&entry->ent, flags, pc);
901         entry->ent.type                 = TRACE_GRAPH_ENT;
902         entry->graph_ent                        = *trace;
903         ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
904 }
905
906 static void __trace_graph_return(struct trace_array *tr,
907                                 struct trace_array_cpu *data,
908                                 struct ftrace_graph_ret *trace,
909                                 unsigned long flags,
910                                 int pc)
911 {
912         struct ring_buffer_event *event;
913         struct ftrace_graph_ret_entry *entry;
914         unsigned long irq_flags;
915
916         if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
917                 return;
918
919         event = ring_buffer_lock_reserve(global_trace.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_GRAPH_RET;
926         entry->ret                              = *trace;
927         ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
928 }
929 #endif
930
931 void
932 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
933        unsigned long ip, unsigned long parent_ip, unsigned long flags,
934        int pc)
935 {
936         if (likely(!atomic_read(&data->disabled)))
937                 trace_function(tr, data, ip, parent_ip, flags, pc);
938 }
939
940 static void ftrace_trace_stack(struct trace_array *tr,
941                                struct trace_array_cpu *data,
942                                unsigned long flags,
943                                int skip, int pc)
944 {
945 #ifdef CONFIG_STACKTRACE
946         struct ring_buffer_event *event;
947         struct stack_entry *entry;
948         struct stack_trace trace;
949         unsigned long irq_flags;
950
951         if (!(trace_flags & TRACE_ITER_STACKTRACE))
952                 return;
953
954         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
955                                          &irq_flags);
956         if (!event)
957                 return;
958         entry   = ring_buffer_event_data(event);
959         tracing_generic_entry_update(&entry->ent, flags, pc);
960         entry->ent.type         = TRACE_STACK;
961
962         memset(&entry->caller, 0, sizeof(entry->caller));
963
964         trace.nr_entries        = 0;
965         trace.max_entries       = FTRACE_STACK_ENTRIES;
966         trace.skip              = skip;
967         trace.entries           = entry->caller;
968
969         save_stack_trace(&trace);
970         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
971 #endif
972 }
973
974 void __trace_stack(struct trace_array *tr,
975                    struct trace_array_cpu *data,
976                    unsigned long flags,
977                    int skip)
978 {
979         ftrace_trace_stack(tr, data, flags, skip, preempt_count());
980 }
981
982 static void ftrace_trace_userstack(struct trace_array *tr,
983                    struct trace_array_cpu *data,
984                    unsigned long flags, int pc)
985 {
986 #ifdef CONFIG_STACKTRACE
987         struct ring_buffer_event *event;
988         struct userstack_entry *entry;
989         struct stack_trace trace;
990         unsigned long irq_flags;
991
992         if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
993                 return;
994
995         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
996                                          &irq_flags);
997         if (!event)
998                 return;
999         entry   = ring_buffer_event_data(event);
1000         tracing_generic_entry_update(&entry->ent, flags, pc);
1001         entry->ent.type         = TRACE_USER_STACK;
1002
1003         memset(&entry->caller, 0, sizeof(entry->caller));
1004
1005         trace.nr_entries        = 0;
1006         trace.max_entries       = FTRACE_STACK_ENTRIES;
1007         trace.skip              = 0;
1008         trace.entries           = entry->caller;
1009
1010         save_stack_trace_user(&trace);
1011         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1012 #endif
1013 }
1014
1015 void __trace_userstack(struct trace_array *tr,
1016                    struct trace_array_cpu *data,
1017                    unsigned long flags)
1018 {
1019         ftrace_trace_userstack(tr, data, flags, preempt_count());
1020 }
1021
1022 static void
1023 ftrace_trace_special(void *__tr, void *__data,
1024                      unsigned long arg1, unsigned long arg2, unsigned long arg3,
1025                      int pc)
1026 {
1027         struct ring_buffer_event *event;
1028         struct trace_array_cpu *data = __data;
1029         struct trace_array *tr = __tr;
1030         struct special_entry *entry;
1031         unsigned long irq_flags;
1032
1033         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1034                                          &irq_flags);
1035         if (!event)
1036                 return;
1037         entry   = ring_buffer_event_data(event);
1038         tracing_generic_entry_update(&entry->ent, 0, pc);
1039         entry->ent.type                 = TRACE_SPECIAL;
1040         entry->arg1                     = arg1;
1041         entry->arg2                     = arg2;
1042         entry->arg3                     = arg3;
1043         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1044         ftrace_trace_stack(tr, data, irq_flags, 4, pc);
1045         ftrace_trace_userstack(tr, data, irq_flags, pc);
1046
1047         trace_wake_up();
1048 }
1049
1050 void
1051 __trace_special(void *__tr, void *__data,
1052                 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1053 {
1054         ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
1055 }
1056
1057 void
1058 tracing_sched_switch_trace(struct trace_array *tr,
1059                            struct trace_array_cpu *data,
1060                            struct task_struct *prev,
1061                            struct task_struct *next,
1062                            unsigned long flags, int pc)
1063 {
1064         struct ring_buffer_event *event;
1065         struct ctx_switch_entry *entry;
1066         unsigned long irq_flags;
1067
1068         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1069                                            &irq_flags);
1070         if (!event)
1071                 return;
1072         entry   = ring_buffer_event_data(event);
1073         tracing_generic_entry_update(&entry->ent, flags, pc);
1074         entry->ent.type                 = TRACE_CTX;
1075         entry->prev_pid                 = prev->pid;
1076         entry->prev_prio                = prev->prio;
1077         entry->prev_state               = prev->state;
1078         entry->next_pid                 = next->pid;
1079         entry->next_prio                = next->prio;
1080         entry->next_state               = next->state;
1081         entry->next_cpu = task_cpu(next);
1082         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1083         ftrace_trace_stack(tr, data, flags, 5, pc);
1084         ftrace_trace_userstack(tr, data, flags, pc);
1085 }
1086
1087 void
1088 tracing_sched_wakeup_trace(struct trace_array *tr,
1089                            struct trace_array_cpu *data,
1090                            struct task_struct *wakee,
1091                            struct task_struct *curr,
1092                            unsigned long flags, int pc)
1093 {
1094         struct ring_buffer_event *event;
1095         struct ctx_switch_entry *entry;
1096         unsigned long irq_flags;
1097
1098         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1099                                            &irq_flags);
1100         if (!event)
1101                 return;
1102         entry   = ring_buffer_event_data(event);
1103         tracing_generic_entry_update(&entry->ent, flags, pc);
1104         entry->ent.type                 = TRACE_WAKE;
1105         entry->prev_pid                 = curr->pid;
1106         entry->prev_prio                = curr->prio;
1107         entry->prev_state               = curr->state;
1108         entry->next_pid                 = wakee->pid;
1109         entry->next_prio                = wakee->prio;
1110         entry->next_state               = wakee->state;
1111         entry->next_cpu                 = task_cpu(wakee);
1112         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1113         ftrace_trace_stack(tr, data, flags, 6, pc);
1114         ftrace_trace_userstack(tr, data, flags, pc);
1115
1116         trace_wake_up();
1117 }
1118
1119 void
1120 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1121 {
1122         struct trace_array *tr = &global_trace;
1123         struct trace_array_cpu *data;
1124         unsigned long flags;
1125         int cpu;
1126         int pc;
1127
1128         if (tracing_disabled)
1129                 return;
1130
1131         pc = preempt_count();
1132         local_irq_save(flags);
1133         cpu = raw_smp_processor_id();
1134         data = tr->data[cpu];
1135
1136         if (likely(atomic_inc_return(&data->disabled) == 1))
1137                 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1138
1139         atomic_dec(&data->disabled);
1140         local_irq_restore(flags);
1141 }
1142
1143 #ifdef CONFIG_FUNCTION_TRACER
1144 static void
1145 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
1146 {
1147         struct trace_array *tr = &global_trace;
1148         struct trace_array_cpu *data;
1149         unsigned long flags;
1150         long disabled;
1151         int cpu, resched;
1152         int pc;
1153
1154         if (unlikely(!ftrace_function_enabled))
1155                 return;
1156
1157         pc = preempt_count();
1158         resched = ftrace_preempt_disable();
1159         local_save_flags(flags);
1160         cpu = raw_smp_processor_id();
1161         data = tr->data[cpu];
1162         disabled = atomic_inc_return(&data->disabled);
1163
1164         if (likely(disabled == 1))
1165                 trace_function(tr, data, ip, parent_ip, flags, pc);
1166
1167         atomic_dec(&data->disabled);
1168         ftrace_preempt_enable(resched);
1169 }
1170
1171 static void
1172 function_trace_call(unsigned long ip, unsigned long parent_ip)
1173 {
1174         struct trace_array *tr = &global_trace;
1175         struct trace_array_cpu *data;
1176         unsigned long flags;
1177         long disabled;
1178         int cpu;
1179         int pc;
1180
1181         if (unlikely(!ftrace_function_enabled))
1182                 return;
1183
1184         /*
1185          * Need to use raw, since this must be called before the
1186          * recursive protection is performed.
1187          */
1188         local_irq_save(flags);
1189         cpu = raw_smp_processor_id();
1190         data = tr->data[cpu];
1191         disabled = atomic_inc_return(&data->disabled);
1192
1193         if (likely(disabled == 1)) {
1194                 pc = preempt_count();
1195                 trace_function(tr, data, ip, parent_ip, flags, pc);
1196         }
1197
1198         atomic_dec(&data->disabled);
1199         local_irq_restore(flags);
1200 }
1201
1202 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1203 int trace_graph_entry(struct ftrace_graph_ent *trace)
1204 {
1205         struct trace_array *tr = &global_trace;
1206         struct trace_array_cpu *data;
1207         unsigned long flags;
1208         long disabled;
1209         int cpu;
1210         int pc;
1211
1212         if (!ftrace_trace_task(current))
1213                 return 0;
1214
1215         if (!ftrace_graph_addr(trace->func))
1216                 return 0;
1217
1218         local_irq_save(flags);
1219         cpu = raw_smp_processor_id();
1220         data = tr->data[cpu];
1221         disabled = atomic_inc_return(&data->disabled);
1222         if (likely(disabled == 1)) {
1223                 pc = preempt_count();
1224                 __trace_graph_entry(tr, data, trace, flags, pc);
1225         }
1226         /* Only do the atomic if it is not already set */
1227         if (!test_tsk_trace_graph(current))
1228                 set_tsk_trace_graph(current);
1229         atomic_dec(&data->disabled);
1230         local_irq_restore(flags);
1231
1232         return 1;
1233 }
1234
1235 void trace_graph_return(struct ftrace_graph_ret *trace)
1236 {
1237         struct trace_array *tr = &global_trace;
1238         struct trace_array_cpu *data;
1239         unsigned long flags;
1240         long disabled;
1241         int cpu;
1242         int pc;
1243
1244         local_irq_save(flags);
1245         cpu = raw_smp_processor_id();
1246         data = tr->data[cpu];
1247         disabled = atomic_inc_return(&data->disabled);
1248         if (likely(disabled == 1)) {
1249                 pc = preempt_count();
1250                 __trace_graph_return(tr, data, trace, flags, pc);
1251         }
1252         if (!trace->depth)
1253                 clear_tsk_trace_graph(current);
1254         atomic_dec(&data->disabled);
1255         local_irq_restore(flags);
1256 }
1257 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1258
1259 static struct ftrace_ops trace_ops __read_mostly =
1260 {
1261         .func = function_trace_call,
1262 };
1263
1264 void tracing_start_function_trace(void)
1265 {
1266         ftrace_function_enabled = 0;
1267
1268         if (trace_flags & TRACE_ITER_PREEMPTONLY)
1269                 trace_ops.func = function_trace_call_preempt_only;
1270         else
1271                 trace_ops.func = function_trace_call;
1272
1273         register_ftrace_function(&trace_ops);
1274         ftrace_function_enabled = 1;
1275 }
1276
1277 void tracing_stop_function_trace(void)
1278 {
1279         ftrace_function_enabled = 0;
1280         unregister_ftrace_function(&trace_ops);
1281 }
1282 #endif
1283
1284 enum trace_file_type {
1285         TRACE_FILE_LAT_FMT      = 1,
1286         TRACE_FILE_ANNOTATE     = 2,
1287 };
1288
1289 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1290 {
1291         /* Don't allow ftrace to trace into the ring buffers */
1292         ftrace_disable_cpu();
1293
1294         iter->idx++;
1295         if (iter->buffer_iter[iter->cpu])
1296                 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1297
1298         ftrace_enable_cpu();
1299 }
1300
1301 static struct trace_entry *
1302 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1303 {
1304         struct ring_buffer_event *event;
1305         struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1306
1307         /* Don't allow ftrace to trace into the ring buffers */
1308         ftrace_disable_cpu();
1309
1310         if (buf_iter)
1311                 event = ring_buffer_iter_peek(buf_iter, ts);
1312         else
1313                 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1314
1315         ftrace_enable_cpu();
1316
1317         return event ? ring_buffer_event_data(event) : NULL;
1318 }
1319
1320 static struct trace_entry *
1321 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1322 {
1323         struct ring_buffer *buffer = iter->tr->buffer;
1324         struct trace_entry *ent, *next = NULL;
1325         u64 next_ts = 0, ts;
1326         int next_cpu = -1;
1327         int cpu;
1328
1329         for_each_tracing_cpu(cpu) {
1330
1331                 if (ring_buffer_empty_cpu(buffer, cpu))
1332                         continue;
1333
1334                 ent = peek_next_entry(iter, cpu, &ts);
1335
1336                 /*
1337                  * Pick the entry with the smallest timestamp:
1338                  */
1339                 if (ent && (!next || ts < next_ts)) {
1340                         next = ent;
1341                         next_cpu = cpu;
1342                         next_ts = ts;
1343                 }
1344         }
1345
1346         if (ent_cpu)
1347                 *ent_cpu = next_cpu;
1348
1349         if (ent_ts)
1350                 *ent_ts = next_ts;
1351
1352         return next;
1353 }
1354
1355 /* Find the next real entry, without updating the iterator itself */
1356 static struct trace_entry *
1357 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1358 {
1359         return __find_next_entry(iter, ent_cpu, ent_ts);
1360 }
1361
1362 /* Find the next real entry, and increment the iterator to the next entry */
1363 static void *find_next_entry_inc(struct trace_iterator *iter)
1364 {
1365         iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1366
1367         if (iter->ent)
1368                 trace_iterator_increment(iter, iter->cpu);
1369
1370         return iter->ent ? iter : NULL;
1371 }
1372
1373 static void trace_consume(struct trace_iterator *iter)
1374 {
1375         /* Don't allow ftrace to trace into the ring buffers */
1376         ftrace_disable_cpu();
1377         ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1378         ftrace_enable_cpu();
1379 }
1380
1381 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1382 {
1383         struct trace_iterator *iter = m->private;
1384         int i = (int)*pos;
1385         void *ent;
1386
1387         (*pos)++;
1388
1389         /* can't go backwards */
1390         if (iter->idx > i)
1391                 return NULL;
1392
1393         if (iter->idx < 0)
1394                 ent = find_next_entry_inc(iter);
1395         else
1396                 ent = iter;
1397
1398         while (ent && iter->idx < i)
1399                 ent = find_next_entry_inc(iter);
1400
1401         iter->pos = *pos;
1402
1403         return ent;
1404 }
1405
1406 static void *s_start(struct seq_file *m, loff_t *pos)
1407 {
1408         struct trace_iterator *iter = m->private;
1409         void *p = NULL;
1410         loff_t l = 0;
1411         int cpu;
1412
1413         mutex_lock(&trace_types_lock);
1414
1415         if (!current_trace || current_trace != iter->trace) {
1416                 mutex_unlock(&trace_types_lock);
1417                 return NULL;
1418         }
1419
1420         atomic_inc(&trace_record_cmdline_disabled);
1421
1422         if (*pos != iter->pos) {
1423                 iter->ent = NULL;
1424                 iter->cpu = 0;
1425                 iter->idx = -1;
1426
1427                 ftrace_disable_cpu();
1428
1429                 for_each_tracing_cpu(cpu) {
1430                         ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1431                 }
1432
1433                 ftrace_enable_cpu();
1434
1435                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1436                         ;
1437
1438         } else {
1439                 l = *pos - 1;
1440                 p = s_next(m, p, &l);
1441         }
1442
1443         return p;
1444 }
1445
1446 static void s_stop(struct seq_file *m, void *p)
1447 {
1448         atomic_dec(&trace_record_cmdline_disabled);
1449         mutex_unlock(&trace_types_lock);
1450 }
1451
1452 #ifdef CONFIG_KRETPROBES
1453 static inline const char *kretprobed(const char *name)
1454 {
1455         static const char tramp_name[] = "kretprobe_trampoline";
1456         int size = sizeof(tramp_name);
1457
1458         if (strncmp(tramp_name, name, size) == 0)
1459                 return "[unknown/kretprobe'd]";
1460         return name;
1461 }
1462 #else
1463 static inline const char *kretprobed(const char *name)
1464 {
1465         return name;
1466 }
1467 #endif /* CONFIG_KRETPROBES */
1468
1469 static int
1470 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1471 {
1472 #ifdef CONFIG_KALLSYMS
1473         char str[KSYM_SYMBOL_LEN];
1474         const char *name;
1475
1476         kallsyms_lookup(address, NULL, NULL, NULL, str);
1477
1478         name = kretprobed(str);
1479
1480         return trace_seq_printf(s, fmt, name);
1481 #endif
1482         return 1;
1483 }
1484
1485 static int
1486 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1487                      unsigned long address)
1488 {
1489 #ifdef CONFIG_KALLSYMS
1490         char str[KSYM_SYMBOL_LEN];
1491         const char *name;
1492
1493         sprint_symbol(str, address);
1494         name = kretprobed(str);
1495
1496         return trace_seq_printf(s, fmt, name);
1497 #endif
1498         return 1;
1499 }
1500
1501 #ifndef CONFIG_64BIT
1502 # define IP_FMT "%08lx"
1503 #else
1504 # define IP_FMT "%016lx"
1505 #endif
1506
1507 int
1508 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1509 {
1510         int ret;
1511
1512         if (!ip)
1513                 return trace_seq_printf(s, "0");
1514
1515         if (sym_flags & TRACE_ITER_SYM_OFFSET)
1516                 ret = seq_print_sym_offset(s, "%s", ip);
1517         else
1518                 ret = seq_print_sym_short(s, "%s", ip);
1519
1520         if (!ret)
1521                 return 0;
1522
1523         if (sym_flags & TRACE_ITER_SYM_ADDR)
1524                 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1525         return ret;
1526 }
1527
1528 static inline int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
1529                                     unsigned long ip, unsigned long sym_flags)
1530 {
1531         struct file *file = NULL;
1532         unsigned long vmstart = 0;
1533         int ret = 1;
1534
1535         if (mm) {
1536                 const struct vm_area_struct *vma;
1537
1538                 down_read(&mm->mmap_sem);
1539                 vma = find_vma(mm, ip);
1540                 if (vma) {
1541                         file = vma->vm_file;
1542                         vmstart = vma->vm_start;
1543                 }
1544                 if (file) {
1545                         ret = trace_seq_path(s, &file->f_path);
1546                         if (ret)
1547                                 ret = trace_seq_printf(s, "[+0x%lx]", ip - vmstart);
1548                 }
1549                 up_read(&mm->mmap_sem);
1550         }
1551         if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
1552                 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1553         return ret;
1554 }
1555
1556 static int
1557 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
1558                       unsigned long sym_flags)
1559 {
1560         struct mm_struct *mm = NULL;
1561         int ret = 1;
1562         unsigned int i;
1563
1564         if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
1565                 struct task_struct *task;
1566                 /*
1567                  * we do the lookup on the thread group leader,
1568                  * since individual threads might have already quit!
1569                  */
1570                 rcu_read_lock();
1571                 task = find_task_by_vpid(entry->ent.tgid);
1572                 if (task)
1573                         mm = get_task_mm(task);
1574                 rcu_read_unlock();
1575         }
1576
1577         for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1578                 unsigned long ip = entry->caller[i];
1579
1580                 if (ip == ULONG_MAX || !ret)
1581                         break;
1582                 if (i && ret)
1583                         ret = trace_seq_puts(s, " <- ");
1584                 if (!ip) {
1585                         if (ret)
1586                                 ret = trace_seq_puts(s, "??");
1587                         continue;
1588                 }
1589                 if (!ret)
1590                         break;
1591                 if (ret)
1592                         ret = seq_print_user_ip(s, mm, ip, sym_flags);
1593         }
1594
1595         if (mm)
1596                 mmput(mm);
1597         return ret;
1598 }
1599
1600 static void print_lat_help_header(struct seq_file *m)
1601 {
1602         seq_puts(m, "#                  _------=> CPU#            \n");
1603         seq_puts(m, "#                 / _-----=> irqs-off        \n");
1604         seq_puts(m, "#                | / _----=> need-resched    \n");
1605         seq_puts(m, "#                || / _---=> hardirq/softirq \n");
1606         seq_puts(m, "#                ||| / _--=> preempt-depth   \n");
1607         seq_puts(m, "#                |||| /                      \n");
1608         seq_puts(m, "#                |||||     delay             \n");
1609         seq_puts(m, "#  cmd     pid   ||||| time  |   caller      \n");
1610         seq_puts(m, "#     \\   /      |||||   \\   |   /           \n");
1611 }
1612
1613 static void print_func_help_header(struct seq_file *m)
1614 {
1615         seq_puts(m, "#           TASK-PID    CPU#    TIMESTAMP  FUNCTION\n");
1616         seq_puts(m, "#              | |       |          |         |\n");
1617 }
1618
1619
1620 static void
1621 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1622 {
1623         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1624         struct trace_array *tr = iter->tr;
1625         struct trace_array_cpu *data = tr->data[tr->cpu];
1626         struct tracer *type = current_trace;
1627         unsigned long total;
1628         unsigned long entries;
1629         const char *name = "preemption";
1630
1631         if (type)
1632                 name = type->name;
1633
1634         entries = ring_buffer_entries(iter->tr->buffer);
1635         total = entries +
1636                 ring_buffer_overruns(iter->tr->buffer);
1637
1638         seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1639                    name, UTS_RELEASE);
1640         seq_puts(m, "-----------------------------------"
1641                  "---------------------------------\n");
1642         seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1643                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1644                    nsecs_to_usecs(data->saved_latency),
1645                    entries,
1646                    total,
1647                    tr->cpu,
1648 #if defined(CONFIG_PREEMPT_NONE)
1649                    "server",
1650 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1651                    "desktop",
1652 #elif defined(CONFIG_PREEMPT)
1653                    "preempt",
1654 #else
1655                    "unknown",
1656 #endif
1657                    /* These are reserved for later use */
1658                    0, 0, 0, 0);
1659 #ifdef CONFIG_SMP
1660         seq_printf(m, " #P:%d)\n", num_online_cpus());
1661 #else
1662         seq_puts(m, ")\n");
1663 #endif
1664         seq_puts(m, "    -----------------\n");
1665         seq_printf(m, "    | task: %.16s-%d "
1666                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1667                    data->comm, data->pid, data->uid, data->nice,
1668                    data->policy, data->rt_priority);
1669         seq_puts(m, "    -----------------\n");
1670
1671         if (data->critical_start) {
1672                 seq_puts(m, " => started at: ");
1673                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1674                 trace_print_seq(m, &iter->seq);
1675                 seq_puts(m, "\n => ended at:   ");
1676                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1677                 trace_print_seq(m, &iter->seq);
1678                 seq_puts(m, "\n");
1679         }
1680
1681         seq_puts(m, "\n");
1682 }
1683
1684 static void
1685 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1686 {
1687         int hardirq, softirq;
1688         char *comm;
1689
1690         comm = trace_find_cmdline(entry->pid);
1691
1692         trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1693         trace_seq_printf(s, "%3d", cpu);
1694         trace_seq_printf(s, "%c%c",
1695                         (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1696                          (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1697                         ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1698
1699         hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1700         softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1701         if (hardirq && softirq) {
1702                 trace_seq_putc(s, 'H');
1703         } else {
1704                 if (hardirq) {
1705                         trace_seq_putc(s, 'h');
1706                 } else {
1707                         if (softirq)
1708                                 trace_seq_putc(s, 's');
1709                         else
1710                                 trace_seq_putc(s, '.');
1711                 }
1712         }
1713
1714         if (entry->preempt_count)
1715                 trace_seq_printf(s, "%x", entry->preempt_count);
1716         else
1717                 trace_seq_puts(s, ".");
1718 }
1719
1720 unsigned long preempt_mark_thresh = 100;
1721
1722 static void
1723 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1724                     unsigned long rel_usecs)
1725 {
1726         trace_seq_printf(s, " %4lldus", abs_usecs);
1727         if (rel_usecs > preempt_mark_thresh)
1728                 trace_seq_puts(s, "!: ");
1729         else if (rel_usecs > 1)
1730                 trace_seq_puts(s, "+: ");
1731         else
1732                 trace_seq_puts(s, " : ");
1733 }
1734
1735 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1736
1737 /*
1738  * The message is supposed to contain an ending newline.
1739  * If the printing stops prematurely, try to add a newline of our own.
1740  */
1741 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1742 {
1743         struct trace_entry *ent;
1744         struct trace_field_cont *cont;
1745         bool ok = true;
1746
1747         ent = peek_next_entry(iter, iter->cpu, NULL);
1748         if (!ent || ent->type != TRACE_CONT) {
1749                 trace_seq_putc(s, '\n');
1750                 return;
1751         }
1752
1753         do {
1754                 cont = (struct trace_field_cont *)ent;
1755                 if (ok)
1756                         ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1757
1758                 ftrace_disable_cpu();
1759
1760                 if (iter->buffer_iter[iter->cpu])
1761                         ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1762                 else
1763                         ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1764
1765                 ftrace_enable_cpu();
1766
1767                 ent = peek_next_entry(iter, iter->cpu, NULL);
1768         } while (ent && ent->type == TRACE_CONT);
1769
1770         if (!ok)
1771                 trace_seq_putc(s, '\n');
1772 }
1773
1774 static void test_cpu_buff_start(struct trace_iterator *iter)
1775 {
1776         struct trace_seq *s = &iter->seq;
1777
1778         if (!(trace_flags & TRACE_ITER_ANNOTATE))
1779                 return;
1780
1781         if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1782                 return;
1783
1784         if (cpu_isset(iter->cpu, iter->started))
1785                 return;
1786
1787         cpu_set(iter->cpu, iter->started);
1788         trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1789 }
1790
1791 static enum print_line_t
1792 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1793 {
1794         struct trace_seq *s = &iter->seq;
1795         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1796         struct trace_entry *next_entry;
1797         unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1798         struct trace_entry *entry = iter->ent;
1799         unsigned long abs_usecs;
1800         unsigned long rel_usecs;
1801         u64 next_ts;
1802         char *comm;
1803         int S, T;
1804         int i;
1805         unsigned state;
1806
1807         if (entry->type == TRACE_CONT)
1808                 return TRACE_TYPE_HANDLED;
1809
1810         test_cpu_buff_start(iter);
1811
1812         next_entry = find_next_entry(iter, NULL, &next_ts);
1813         if (!next_entry)
1814                 next_ts = iter->ts;
1815         rel_usecs = ns2usecs(next_ts - iter->ts);
1816         abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1817
1818         if (verbose) {
1819                 comm = trace_find_cmdline(entry->pid);
1820                 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1821                                  " %ld.%03ldms (+%ld.%03ldms): ",
1822                                  comm,
1823                                  entry->pid, cpu, entry->flags,
1824                                  entry->preempt_count, trace_idx,
1825                                  ns2usecs(iter->ts),
1826                                  abs_usecs/1000,
1827                                  abs_usecs % 1000, rel_usecs/1000,
1828                                  rel_usecs % 1000);
1829         } else {
1830                 lat_print_generic(s, entry, cpu);
1831                 lat_print_timestamp(s, abs_usecs, rel_usecs);
1832         }
1833         switch (entry->type) {
1834         case TRACE_FN: {
1835                 struct ftrace_entry *field;
1836
1837                 trace_assign_type(field, entry);
1838
1839                 seq_print_ip_sym(s, field->ip, sym_flags);
1840                 trace_seq_puts(s, " (");
1841                 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1842                 trace_seq_puts(s, ")\n");
1843                 break;
1844         }
1845         case TRACE_CTX:
1846         case TRACE_WAKE: {
1847                 struct ctx_switch_entry *field;
1848
1849                 trace_assign_type(field, entry);
1850
1851                 T = field->next_state < sizeof(state_to_char) ?
1852                         state_to_char[field->next_state] : 'X';
1853
1854                 state = field->prev_state ?
1855                         __ffs(field->prev_state) + 1 : 0;
1856                 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1857                 comm = trace_find_cmdline(field->next_pid);
1858                 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1859                                  field->prev_pid,
1860                                  field->prev_prio,
1861                                  S, entry->type == TRACE_CTX ? "==>" : "  +",
1862                                  field->next_cpu,
1863                                  field->next_pid,
1864                                  field->next_prio,
1865                                  T, comm);
1866                 break;
1867         }
1868         case TRACE_SPECIAL: {
1869                 struct special_entry *field;
1870
1871                 trace_assign_type(field, entry);
1872
1873                 trace_seq_printf(s, "# %ld %ld %ld\n",
1874                                  field->arg1,
1875                                  field->arg2,
1876                                  field->arg3);
1877                 break;
1878         }
1879         case TRACE_STACK: {
1880                 struct stack_entry *field;
1881
1882                 trace_assign_type(field, entry);
1883
1884                 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1885                         if (i)
1886                                 trace_seq_puts(s, " <= ");
1887                         seq_print_ip_sym(s, field->caller[i], sym_flags);
1888                 }
1889                 trace_seq_puts(s, "\n");
1890                 break;
1891         }
1892         case TRACE_PRINT: {
1893                 struct print_entry *field;
1894
1895                 trace_assign_type(field, entry);
1896
1897                 seq_print_ip_sym(s, field->ip, sym_flags);
1898                 trace_seq_printf(s, ": %s", field->buf);
1899                 if (entry->flags & TRACE_FLAG_CONT)
1900                         trace_seq_print_cont(s, iter);
1901                 break;
1902         }
1903         case TRACE_BRANCH: {
1904                 struct trace_branch *field;
1905
1906                 trace_assign_type(field, entry);
1907
1908                 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1909                                  field->correct ? "  ok  " : " MISS ",
1910                                  field->func,
1911                                  field->file,
1912                                  field->line);
1913                 break;
1914         }
1915         case TRACE_USER_STACK: {
1916                 struct userstack_entry *field;
1917
1918                 trace_assign_type(field, entry);
1919
1920                 seq_print_userip_objs(field, s, sym_flags);
1921                 trace_seq_putc(s, '\n');
1922                 break;
1923         }
1924         default:
1925                 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1926         }
1927         return TRACE_TYPE_HANDLED;
1928 }
1929
1930 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1931 {
1932         struct trace_seq *s = &iter->seq;
1933         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1934         struct trace_entry *entry;
1935         unsigned long usec_rem;
1936         unsigned long long t;
1937         unsigned long secs;
1938         char *comm;
1939         int ret;
1940         int S, T;
1941         int i;
1942
1943         entry = iter->ent;
1944
1945         if (entry->type == TRACE_CONT)
1946                 return TRACE_TYPE_HANDLED;
1947
1948         test_cpu_buff_start(iter);
1949
1950         comm = trace_find_cmdline(iter->ent->pid);
1951
1952         t = ns2usecs(iter->ts);
1953         usec_rem = do_div(t, 1000000ULL);
1954         secs = (unsigned long)t;
1955
1956         ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1957         if (!ret)
1958                 return TRACE_TYPE_PARTIAL_LINE;
1959         ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1960         if (!ret)
1961                 return TRACE_TYPE_PARTIAL_LINE;
1962         ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1963         if (!ret)
1964                 return TRACE_TYPE_PARTIAL_LINE;
1965
1966         switch (entry->type) {
1967         case TRACE_FN: {
1968                 struct ftrace_entry *field;
1969
1970                 trace_assign_type(field, entry);
1971
1972                 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1973                 if (!ret)
1974                         return TRACE_TYPE_PARTIAL_LINE;
1975                 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1976                                                 field->parent_ip) {
1977                         ret = trace_seq_printf(s, " <-");
1978                         if (!ret)
1979                                 return TRACE_TYPE_PARTIAL_LINE;
1980                         ret = seq_print_ip_sym(s,
1981                                                field->parent_ip,
1982                                                sym_flags);
1983                         if (!ret)
1984                                 return TRACE_TYPE_PARTIAL_LINE;
1985                 }
1986                 ret = trace_seq_printf(s, "\n");
1987                 if (!ret)
1988                         return TRACE_TYPE_PARTIAL_LINE;
1989                 break;
1990         }
1991         case TRACE_CTX:
1992         case TRACE_WAKE: {
1993                 struct ctx_switch_entry *field;
1994
1995                 trace_assign_type(field, entry);
1996
1997                 S = field->prev_state < sizeof(state_to_char) ?
1998                         state_to_char[field->prev_state] : 'X';
1999                 T = field->next_state < sizeof(state_to_char) ?
2000                         state_to_char[field->next_state] : 'X';
2001                 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
2002                                        field->prev_pid,
2003                                        field->prev_prio,
2004                                        S,
2005                                        entry->type == TRACE_CTX ? "==>" : "  +",
2006                                        field->next_cpu,
2007                                        field->next_pid,
2008                                        field->next_prio,
2009                                        T);
2010                 if (!ret)
2011                         return TRACE_TYPE_PARTIAL_LINE;
2012                 break;
2013         }
2014         case TRACE_SPECIAL: {
2015                 struct special_entry *field;
2016
2017                 trace_assign_type(field, entry);
2018
2019                 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
2020                                  field->arg1,
2021                                  field->arg2,
2022                                  field->arg3);
2023                 if (!ret)
2024                         return TRACE_TYPE_PARTIAL_LINE;
2025                 break;
2026         }
2027         case TRACE_STACK: {
2028                 struct stack_entry *field;
2029
2030                 trace_assign_type(field, entry);
2031
2032                 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
2033                         if (i) {
2034                                 ret = trace_seq_puts(s, " <= ");
2035                                 if (!ret)
2036                                         return TRACE_TYPE_PARTIAL_LINE;
2037                         }
2038                         ret = seq_print_ip_sym(s, field->caller[i],
2039                                                sym_flags);
2040                         if (!ret)
2041                                 return TRACE_TYPE_PARTIAL_LINE;
2042                 }
2043                 ret = trace_seq_puts(s, "\n");
2044                 if (!ret)
2045                         return TRACE_TYPE_PARTIAL_LINE;
2046                 break;
2047         }
2048         case TRACE_PRINT: {
2049                 struct print_entry *field;
2050
2051                 trace_assign_type(field, entry);
2052
2053                 seq_print_ip_sym(s, field->ip, sym_flags);
2054                 trace_seq_printf(s, ": %s", field->buf);
2055                 if (entry->flags & TRACE_FLAG_CONT)
2056                         trace_seq_print_cont(s, iter);
2057                 break;
2058         }
2059         case TRACE_GRAPH_RET: {
2060                 return print_graph_function(iter);
2061         }
2062         case TRACE_GRAPH_ENT: {
2063                 return print_graph_function(iter);
2064         }
2065         case TRACE_BRANCH: {
2066                 struct trace_branch *field;
2067
2068                 trace_assign_type(field, entry);
2069
2070                 trace_seq_printf(s, "[%s] %s:%s:%d\n",
2071                                  field->correct ? "  ok  " : " MISS ",
2072                                  field->func,
2073                                  field->file,
2074                                  field->line);
2075                 break;
2076         }
2077         case TRACE_USER_STACK: {
2078                 struct userstack_entry *field;
2079
2080                 trace_assign_type(field, entry);
2081
2082                 ret = seq_print_userip_objs(field, s, sym_flags);
2083                 if (!ret)
2084                         return TRACE_TYPE_PARTIAL_LINE;
2085                 ret = trace_seq_putc(s, '\n');
2086                 if (!ret)
2087                         return TRACE_TYPE_PARTIAL_LINE;
2088                 break;
2089         }
2090         }
2091         return TRACE_TYPE_HANDLED;
2092 }
2093
2094 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2095 {
2096         struct trace_seq *s = &iter->seq;
2097         struct trace_entry *entry;
2098         int ret;
2099         int S, T;
2100
2101         entry = iter->ent;
2102
2103         if (entry->type == TRACE_CONT)
2104                 return TRACE_TYPE_HANDLED;
2105
2106         ret = trace_seq_printf(s, "%d %d %llu ",
2107                 entry->pid, iter->cpu, iter->ts);
2108         if (!ret)
2109                 return TRACE_TYPE_PARTIAL_LINE;
2110
2111         switch (entry->type) {
2112         case TRACE_FN: {
2113                 struct ftrace_entry *field;
2114
2115                 trace_assign_type(field, entry);
2116
2117                 ret = trace_seq_printf(s, "%x %x\n",
2118                                         field->ip,
2119                                         field->parent_ip);
2120                 if (!ret)
2121                         return TRACE_TYPE_PARTIAL_LINE;
2122                 break;
2123         }
2124         case TRACE_CTX:
2125         case TRACE_WAKE: {
2126                 struct ctx_switch_entry *field;
2127
2128                 trace_assign_type(field, entry);
2129
2130                 S = field->prev_state < sizeof(state_to_char) ?
2131                         state_to_char[field->prev_state] : 'X';
2132                 T = field->next_state < sizeof(state_to_char) ?
2133                         state_to_char[field->next_state] : 'X';
2134                 if (entry->type == TRACE_WAKE)
2135                         S = '+';
2136                 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
2137                                        field->prev_pid,
2138                                        field->prev_prio,
2139                                        S,
2140                                        field->next_cpu,
2141                                        field->next_pid,
2142                                        field->next_prio,
2143                                        T);
2144                 if (!ret)
2145                         return TRACE_TYPE_PARTIAL_LINE;
2146                 break;
2147         }
2148         case TRACE_SPECIAL:
2149         case TRACE_USER_STACK:
2150         case TRACE_STACK: {
2151                 struct special_entry *field;
2152
2153                 trace_assign_type(field, entry);
2154
2155                 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
2156                                  field->arg1,
2157                                  field->arg2,
2158                                  field->arg3);
2159                 if (!ret)
2160                         return TRACE_TYPE_PARTIAL_LINE;
2161                 break;
2162         }
2163         case TRACE_PRINT: {
2164                 struct print_entry *field;
2165
2166                 trace_assign_type(field, entry);
2167
2168                 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
2169                 if (entry->flags & TRACE_FLAG_CONT)
2170                         trace_seq_print_cont(s, iter);
2171                 break;
2172         }
2173         }
2174         return TRACE_TYPE_HANDLED;
2175 }
2176
2177 #define SEQ_PUT_FIELD_RET(s, x)                         \
2178 do {                                                    \
2179         if (!trace_seq_putmem(s, &(x), sizeof(x)))      \
2180                 return 0;                               \
2181 } while (0)
2182
2183 #define SEQ_PUT_HEX_FIELD_RET(s, x)                     \
2184 do {                                                    \
2185         BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES);     \
2186         if (!trace_seq_putmem_hex(s, &(x), sizeof(x)))  \
2187                 return 0;                               \
2188 } while (0)
2189
2190 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2191 {
2192         struct trace_seq *s = &iter->seq;
2193         unsigned char newline = '\n';
2194         struct trace_entry *entry;
2195         int S, T;
2196
2197         entry = iter->ent;
2198
2199         if (entry->type == TRACE_CONT)
2200                 return TRACE_TYPE_HANDLED;
2201
2202         SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2203         SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2204         SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2205
2206         switch (entry->type) {
2207         case TRACE_FN: {
2208                 struct ftrace_entry *field;
2209
2210                 trace_assign_type(field, entry);
2211
2212                 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
2213                 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
2214                 break;
2215         }
2216         case TRACE_CTX:
2217         case TRACE_WAKE: {
2218                 struct ctx_switch_entry *field;
2219
2220                 trace_assign_type(field, entry);
2221
2222                 S = field->prev_state < sizeof(state_to_char) ?
2223                         state_to_char[field->prev_state] : 'X';
2224                 T = field->next_state < sizeof(state_to_char) ?
2225                         state_to_char[field->next_state] : 'X';
2226                 if (entry->type == TRACE_WAKE)
2227                         S = '+';
2228                 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
2229                 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
2230                 SEQ_PUT_HEX_FIELD_RET(s, S);
2231                 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
2232                 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
2233                 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
2234                 SEQ_PUT_HEX_FIELD_RET(s, T);
2235                 break;
2236         }
2237         case TRACE_SPECIAL:
2238         case TRACE_USER_STACK:
2239         case TRACE_STACK: {
2240                 struct special_entry *field;
2241
2242                 trace_assign_type(field, entry);
2243
2244                 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
2245                 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
2246                 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
2247                 break;
2248         }
2249         }
2250         SEQ_PUT_FIELD_RET(s, newline);
2251
2252         return TRACE_TYPE_HANDLED;
2253 }
2254
2255 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2256 {
2257         struct trace_seq *s = &iter->seq;
2258         struct trace_entry *entry;
2259
2260         entry = iter->ent;
2261
2262         if (entry->type == TRACE_CONT)
2263                 return TRACE_TYPE_HANDLED;
2264
2265         SEQ_PUT_FIELD_RET(s, entry->pid);
2266         SEQ_PUT_FIELD_RET(s, entry->cpu);
2267         SEQ_PUT_FIELD_RET(s, iter->ts);
2268
2269         switch (entry->type) {
2270         case TRACE_FN: {
2271                 struct ftrace_entry *field;
2272
2273                 trace_assign_type(field, entry);
2274
2275                 SEQ_PUT_FIELD_RET(s, field->ip);
2276                 SEQ_PUT_FIELD_RET(s, field->parent_ip);
2277                 break;
2278         }
2279         case TRACE_CTX: {
2280                 struct ctx_switch_entry *field;
2281
2282                 trace_assign_type(field, entry);
2283
2284                 SEQ_PUT_FIELD_RET(s, field->prev_pid);
2285                 SEQ_PUT_FIELD_RET(s, field->prev_prio);
2286                 SEQ_PUT_FIELD_RET(s, field->prev_state);
2287                 SEQ_PUT_FIELD_RET(s, field->next_pid);
2288                 SEQ_PUT_FIELD_RET(s, field->next_prio);
2289                 SEQ_PUT_FIELD_RET(s, field->next_state);
2290                 break;
2291         }
2292         case TRACE_SPECIAL:
2293         case TRACE_USER_STACK:
2294         case TRACE_STACK: {
2295                 struct special_entry *field;
2296
2297                 trace_assign_type(field, entry);
2298
2299                 SEQ_PUT_FIELD_RET(s, field->arg1);
2300                 SEQ_PUT_FIELD_RET(s, field->arg2);
2301                 SEQ_PUT_FIELD_RET(s, field->arg3);
2302                 break;
2303         }
2304         }
2305         return 1;
2306 }
2307
2308 static int trace_empty(struct trace_iterator *iter)
2309 {
2310         int cpu;
2311
2312         for_each_tracing_cpu(cpu) {
2313                 if (iter->buffer_iter[cpu]) {
2314                         if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2315                                 return 0;
2316                 } else {
2317                         if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2318                                 return 0;
2319                 }
2320         }
2321
2322         return 1;
2323 }
2324
2325 static enum print_line_t print_trace_line(struct trace_iterator *iter)
2326 {
2327         enum print_line_t ret;
2328
2329         if (iter->trace && iter->trace->print_line) {
2330                 ret = iter->trace->print_line(iter);
2331                 if (ret != TRACE_TYPE_UNHANDLED)
2332                         return ret;
2333         }
2334
2335         if (trace_flags & TRACE_ITER_BIN)
2336                 return print_bin_fmt(iter);
2337
2338         if (trace_flags & TRACE_ITER_HEX)
2339                 return print_hex_fmt(iter);
2340
2341         if (trace_flags & TRACE_ITER_RAW)
2342                 return print_raw_fmt(iter);
2343
2344         if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2345                 return print_lat_fmt(iter, iter->idx, iter->cpu);
2346
2347         return print_trace_fmt(iter);
2348 }
2349
2350 static int s_show(struct seq_file *m, void *v)
2351 {
2352         struct trace_iterator *iter = v;
2353
2354         if (iter->ent == NULL) {
2355                 if (iter->tr) {
2356                         seq_printf(m, "# tracer: %s\n", iter->trace->name);
2357                         seq_puts(m, "#\n");
2358                 }
2359                 if (iter->trace && iter->trace->print_header)
2360                         iter->trace->print_header(m);
2361                 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2362                         /* print nothing if the buffers are empty */
2363                         if (trace_empty(iter))
2364                                 return 0;
2365                         print_trace_header(m, iter);
2366                         if (!(trace_flags & TRACE_ITER_VERBOSE))
2367                                 print_lat_help_header(m);
2368                 } else {
2369                         if (!(trace_flags & TRACE_ITER_VERBOSE))
2370                                 print_func_help_header(m);
2371                 }
2372         } else {
2373                 print_trace_line(iter);
2374                 trace_print_seq(m, &iter->seq);
2375         }
2376
2377         return 0;
2378 }
2379
2380 static struct seq_operations tracer_seq_ops = {
2381         .start          = s_start,
2382         .next           = s_next,
2383         .stop           = s_stop,
2384         .show           = s_show,
2385 };
2386
2387 static struct trace_iterator *
2388 __tracing_open(struct inode *inode, struct file *file, int *ret)
2389 {
2390         struct trace_iterator *iter;
2391         struct seq_file *m;
2392         int cpu;
2393
2394         if (tracing_disabled) {
2395                 *ret = -ENODEV;
2396                 return NULL;
2397         }
2398
2399         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2400         if (!iter) {
2401                 *ret = -ENOMEM;
2402                 goto out;
2403         }
2404
2405         mutex_lock(&trace_types_lock);
2406         if (current_trace && current_trace->print_max)
2407                 iter->tr = &max_tr;
2408         else
2409                 iter->tr = inode->i_private;
2410         iter->trace = current_trace;
2411         iter->pos = -1;
2412
2413         /* Notify the tracer early; before we stop tracing. */
2414         if (iter->trace && iter->trace->open)
2415                         iter->trace->open(iter);
2416
2417         /* Annotate start of buffers if we had overruns */
2418         if (ring_buffer_overruns(iter->tr->buffer))
2419                 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2420
2421
2422         for_each_tracing_cpu(cpu) {
2423
2424                 iter->buffer_iter[cpu] =
2425                         ring_buffer_read_start(iter->tr->buffer, cpu);
2426
2427                 if (!iter->buffer_iter[cpu])
2428                         goto fail_buffer;
2429         }
2430
2431         /* TODO stop tracer */
2432         *ret = seq_open(file, &tracer_seq_ops);
2433         if (*ret)
2434                 goto fail_buffer;
2435
2436         m = file->private_data;
2437         m->private = iter;
2438
2439         /* stop the trace while dumping */
2440         tracing_stop();
2441
2442         mutex_unlock(&trace_types_lock);
2443
2444  out:
2445         return iter;
2446
2447  fail_buffer:
2448         for_each_tracing_cpu(cpu) {
2449                 if (iter->buffer_iter[cpu])
2450                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
2451         }
2452         mutex_unlock(&trace_types_lock);
2453         kfree(iter);
2454
2455         return ERR_PTR(-ENOMEM);
2456 }
2457
2458 int tracing_open_generic(struct inode *inode, struct file *filp)
2459 {
2460         if (tracing_disabled)
2461                 return -ENODEV;
2462
2463         filp->private_data = inode->i_private;
2464         return 0;
2465 }
2466
2467 int tracing_release(struct inode *inode, struct file *file)
2468 {
2469         struct seq_file *m = (struct seq_file *)file->private_data;
2470         struct trace_iterator *iter = m->private;
2471         int cpu;
2472
2473         mutex_lock(&trace_types_lock);
2474         for_each_tracing_cpu(cpu) {
2475                 if (iter->buffer_iter[cpu])
2476                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
2477         }
2478
2479         if (iter->trace && iter->trace->close)
2480                 iter->trace->close(iter);
2481
2482         /* reenable tracing if it was previously enabled */
2483         tracing_start();
2484         mutex_unlock(&trace_types_lock);
2485
2486         seq_release(inode, file);
2487         kfree(iter);
2488         return 0;
2489 }
2490
2491 static int tracing_open(struct inode *inode, struct file *file)
2492 {
2493         int ret;
2494
2495         __tracing_open(inode, file, &ret);
2496
2497         return ret;
2498 }
2499
2500 static int tracing_lt_open(struct inode *inode, struct file *file)
2501 {
2502         struct trace_iterator *iter;
2503         int ret;
2504
2505         iter = __tracing_open(inode, file, &ret);
2506
2507         if (!ret)
2508                 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2509
2510         return ret;
2511 }
2512
2513
2514 static void *
2515 t_next(struct seq_file *m, void *v, loff_t *pos)
2516 {
2517         struct tracer *t = m->private;
2518
2519         (*pos)++;
2520
2521         if (t)
2522                 t = t->next;
2523
2524         m->private = t;
2525
2526         return t;
2527 }
2528
2529 static void *t_start(struct seq_file *m, loff_t *pos)
2530 {
2531         struct tracer *t = m->private;
2532         loff_t l = 0;
2533
2534         mutex_lock(&trace_types_lock);
2535         for (; t && l < *pos; t = t_next(m, t, &l))
2536                 ;
2537
2538         return t;
2539 }
2540
2541 static void t_stop(struct seq_file *m, void *p)
2542 {
2543         mutex_unlock(&trace_types_lock);
2544 }
2545
2546 static int t_show(struct seq_file *m, void *v)
2547 {
2548         struct tracer *t = v;
2549
2550         if (!t)
2551                 return 0;
2552
2553         seq_printf(m, "%s", t->name);
2554         if (t->next)
2555                 seq_putc(m, ' ');
2556         else
2557                 seq_putc(m, '\n');
2558
2559         return 0;
2560 }
2561
2562 static struct seq_operations show_traces_seq_ops = {
2563         .start          = t_start,
2564         .next           = t_next,
2565         .stop           = t_stop,
2566         .show           = t_show,
2567 };
2568
2569 static int show_traces_open(struct inode *inode, struct file *file)
2570 {
2571         int ret;
2572
2573         if (tracing_disabled)
2574                 return -ENODEV;
2575
2576         ret = seq_open(file, &show_traces_seq_ops);
2577         if (!ret) {
2578                 struct seq_file *m = file->private_data;
2579                 m->private = trace_types;
2580         }
2581
2582         return ret;
2583 }
2584
2585 static struct file_operations tracing_fops = {
2586         .open           = tracing_open,
2587         .read           = seq_read,
2588         .llseek         = seq_lseek,
2589         .release        = tracing_release,
2590 };
2591
2592 static struct file_operations tracing_lt_fops = {
2593         .open           = tracing_lt_open,
2594         .read           = seq_read,
2595         .llseek         = seq_lseek,
2596         .release        = tracing_release,
2597 };
2598
2599 static struct file_operations show_traces_fops = {
2600         .open           = show_traces_open,
2601         .read           = seq_read,
2602         .release        = seq_release,
2603 };
2604
2605 /*
2606  * Only trace on a CPU if the bitmask is set:
2607  */
2608 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2609
2610 /*
2611  * When tracing/tracing_cpu_mask is modified then this holds
2612  * the new bitmask we are about to install:
2613  */
2614 static cpumask_t tracing_cpumask_new;
2615
2616 /*
2617  * The tracer itself will not take this lock, but still we want
2618  * to provide a consistent cpumask to user-space:
2619  */
2620 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2621
2622 /*
2623  * Temporary storage for the character representation of the
2624  * CPU bitmask (and one more byte for the newline):
2625  */
2626 static char mask_str[NR_CPUS + 1];
2627
2628 static ssize_t
2629 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2630                      size_t count, loff_t *ppos)
2631 {
2632         int len;
2633
2634         mutex_lock(&tracing_cpumask_update_lock);
2635
2636         len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2637         if (count - len < 2) {
2638                 count = -EINVAL;
2639                 goto out_err;
2640         }
2641         len += sprintf(mask_str + len, "\n");
2642         count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2643
2644 out_err:
2645         mutex_unlock(&tracing_cpumask_update_lock);
2646
2647         return count;
2648 }
2649
2650 static ssize_t
2651 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2652                       size_t count, loff_t *ppos)
2653 {
2654         int err, cpu;
2655
2656         mutex_lock(&tracing_cpumask_update_lock);
2657         err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2658         if (err)
2659                 goto err_unlock;
2660
2661         local_irq_disable();
2662         __raw_spin_lock(&ftrace_max_lock);
2663         for_each_tracing_cpu(cpu) {
2664                 /*
2665                  * Increase/decrease the disabled counter if we are
2666                  * about to flip a bit in the cpumask:
2667                  */
2668                 if (cpu_isset(cpu, tracing_cpumask) &&
2669                                 !cpu_isset(cpu, tracing_cpumask_new)) {
2670                         atomic_inc(&global_trace.data[cpu]->disabled);
2671                 }
2672                 if (!cpu_isset(cpu, tracing_cpumask) &&
2673                                 cpu_isset(cpu, tracing_cpumask_new)) {
2674                         atomic_dec(&global_trace.data[cpu]->disabled);
2675                 }
2676         }
2677         __raw_spin_unlock(&ftrace_max_lock);
2678         local_irq_enable();
2679
2680         tracing_cpumask = tracing_cpumask_new;
2681
2682         mutex_unlock(&tracing_cpumask_update_lock);
2683
2684         return count;
2685
2686 err_unlock:
2687         mutex_unlock(&tracing_cpumask_update_lock);
2688
2689         return err;
2690 }
2691
2692 static struct file_operations tracing_cpumask_fops = {
2693         .open           = tracing_open_generic,
2694         .read           = tracing_cpumask_read,
2695         .write          = tracing_cpumask_write,
2696 };
2697
2698 static ssize_t
2699 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2700                        size_t cnt, loff_t *ppos)
2701 {
2702         int i;
2703         char *buf;
2704         int r = 0;
2705         int len = 0;
2706         u32 tracer_flags = current_trace->flags->val;
2707         struct tracer_opt *trace_opts = current_trace->flags->opts;
2708
2709
2710         /* calulate max size */
2711         for (i = 0; trace_options[i]; i++) {
2712                 len += strlen(trace_options[i]);
2713                 len += 3; /* "no" and space */
2714         }
2715
2716         /*
2717          * Increase the size with names of options specific
2718          * of the current tracer.
2719          */
2720         for (i = 0; trace_opts[i].name; i++) {
2721                 len += strlen(trace_opts[i].name);
2722                 len += 3; /* "no" and space */
2723         }
2724
2725         /* +2 for \n and \0 */
2726         buf = kmalloc(len + 2, GFP_KERNEL);
2727         if (!buf)
2728                 return -ENOMEM;
2729
2730         for (i = 0; trace_options[i]; i++) {
2731                 if (trace_flags & (1 << i))
2732                         r += sprintf(buf + r, "%s ", trace_options[i]);
2733                 else
2734                         r += sprintf(buf + r, "no%s ", trace_options[i]);
2735         }
2736
2737         for (i = 0; trace_opts[i].name; i++) {
2738                 if (tracer_flags & trace_opts[i].bit)
2739                         r += sprintf(buf + r, "%s ",
2740                                 trace_opts[i].name);
2741                 else
2742                         r += sprintf(buf + r, "no%s ",
2743                                 trace_opts[i].name);
2744         }
2745
2746         r += sprintf(buf + r, "\n");
2747         WARN_ON(r >= len + 2);
2748
2749         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2750
2751         kfree(buf);
2752
2753         return r;
2754 }
2755
2756 /* Try to assign a tracer specific option */
2757 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2758 {
2759         struct tracer_flags *trace_flags = trace->flags;
2760         struct tracer_opt *opts = NULL;
2761         int ret = 0, i = 0;
2762         int len;
2763
2764         for (i = 0; trace_flags->opts[i].name; i++) {
2765                 opts = &trace_flags->opts[i];
2766                 len = strlen(opts->name);
2767
2768                 if (strncmp(cmp, opts->name, len) == 0) {
2769                         ret = trace->set_flag(trace_flags->val,
2770                                 opts->bit, !neg);
2771                         break;
2772                 }
2773         }
2774         /* Not found */
2775         if (!trace_flags->opts[i].name)
2776                 return -EINVAL;
2777
2778         /* Refused to handle */
2779         if (ret)
2780                 return ret;
2781
2782         if (neg)
2783                 trace_flags->val &= ~opts->bit;
2784         else
2785                 trace_flags->val |= opts->bit;
2786
2787         return 0;
2788 }
2789
2790 static ssize_t
2791 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2792                         size_t cnt, loff_t *ppos)
2793 {
2794         char buf[64];
2795         char *cmp = buf;
2796         int neg = 0;
2797         int ret;
2798         int i;
2799
2800         if (cnt >= sizeof(buf))
2801                 return -EINVAL;
2802
2803         if (copy_from_user(&buf, ubuf, cnt))
2804                 return -EFAULT;
2805
2806         buf[cnt] = 0;
2807
2808         if (strncmp(buf, "no", 2) == 0) {
2809                 neg = 1;
2810                 cmp += 2;
2811         }
2812
2813         for (i = 0; trace_options[i]; i++) {
2814                 int len = strlen(trace_options[i]);
2815
2816                 if (strncmp(cmp, trace_options[i], len) == 0) {
2817                         if (neg)
2818                                 trace_flags &= ~(1 << i);
2819                         else
2820                                 trace_flags |= (1 << i);
2821                         break;
2822                 }
2823         }
2824
2825         /* If no option could be set, test the specific tracer options */
2826         if (!trace_options[i]) {
2827                 ret = set_tracer_option(current_trace, cmp, neg);
2828                 if (ret)
2829                         return ret;
2830         }
2831
2832         filp->f_pos += cnt;
2833
2834         return cnt;
2835 }
2836
2837 static struct file_operations tracing_iter_fops = {
2838         .open           = tracing_open_generic,
2839         .read           = tracing_trace_options_read,
2840         .write          = tracing_trace_options_write,
2841 };
2842
2843 static const char readme_msg[] =
2844         "tracing mini-HOWTO:\n\n"
2845         "# mkdir /debug\n"
2846         "# mount -t debugfs nodev /debug\n\n"
2847         "# cat /debug/tracing/available_tracers\n"
2848         "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2849         "# cat /debug/tracing/current_tracer\n"
2850         "none\n"
2851         "# echo sched_switch > /debug/tracing/current_tracer\n"
2852         "# cat /debug/tracing/current_tracer\n"
2853         "sched_switch\n"
2854         "# cat /debug/tracing/trace_options\n"
2855         "noprint-parent nosym-offset nosym-addr noverbose\n"
2856         "# echo print-parent > /debug/tracing/trace_options\n"
2857         "# echo 1 > /debug/tracing/tracing_enabled\n"
2858         "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2859         "echo 0 > /debug/tracing/tracing_enabled\n"
2860 ;
2861
2862 static ssize_t
2863 tracing_readme_read(struct file *filp, char __user *ubuf,
2864                        size_t cnt, loff_t *ppos)
2865 {
2866         return simple_read_from_buffer(ubuf, cnt, ppos,
2867                                         readme_msg, strlen(readme_msg));
2868 }
2869
2870 static struct file_operations tracing_readme_fops = {
2871         .open           = tracing_open_generic,
2872         .read           = tracing_readme_read,
2873 };
2874
2875 static ssize_t
2876 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2877                   size_t cnt, loff_t *ppos)
2878 {
2879         char buf[64];
2880         int r;
2881
2882         r = sprintf(buf, "%u\n", tracer_enabled);
2883         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2884 }
2885
2886 static ssize_t
2887 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2888                    size_t cnt, loff_t *ppos)
2889 {
2890         struct trace_array *tr = filp->private_data;
2891         char buf[64];
2892         long val;
2893         int ret;
2894
2895         if (cnt >= sizeof(buf))
2896                 return -EINVAL;
2897
2898         if (copy_from_user(&buf, ubuf, cnt))
2899                 return -EFAULT;
2900
2901         buf[cnt] = 0;
2902
2903         ret = strict_strtoul(buf, 10, &val);
2904         if (ret < 0)
2905                 return ret;
2906
2907         val = !!val;
2908
2909         mutex_lock(&trace_types_lock);
2910         if (tracer_enabled ^ val) {
2911                 if (val) {
2912                         tracer_enabled = 1;
2913                         if (current_trace->start)
2914                                 current_trace->start(tr);
2915                         tracing_start();
2916                 } else {
2917                         tracer_enabled = 0;
2918                         tracing_stop();
2919                         if (current_trace->stop)
2920                                 current_trace->stop(tr);
2921                 }
2922         }
2923         mutex_unlock(&trace_types_lock);
2924
2925         filp->f_pos += cnt;
2926
2927         return cnt;
2928 }
2929
2930 static ssize_t
2931 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2932                        size_t cnt, loff_t *ppos)
2933 {
2934         char buf[max_tracer_type_len+2];
2935         int r;
2936
2937         mutex_lock(&trace_types_lock);
2938         if (current_trace)
2939                 r = sprintf(buf, "%s\n", current_trace->name);
2940         else
2941                 r = sprintf(buf, "\n");
2942         mutex_unlock(&trace_types_lock);
2943
2944         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2945 }
2946
2947 static int tracing_set_tracer(char *buf)
2948 {
2949         struct trace_array *tr = &global_trace;
2950         struct tracer *t;
2951         int ret = 0;
2952
2953         mutex_lock(&trace_types_lock);
2954         for (t = trace_types; t; t = t->next) {
2955                 if (strcmp(t->name, buf) == 0)
2956                         break;
2957         }
2958         if (!t) {
2959                 ret = -EINVAL;
2960                 goto out;
2961         }
2962         if (t == current_trace)
2963                 goto out;
2964
2965         trace_branch_disable();
2966         if (current_trace && current_trace->reset)
2967                 current_trace->reset(tr);
2968
2969         current_trace = t;
2970         if (t->init) {
2971                 ret = t->init(tr);
2972                 if (ret)
2973                         goto out;
2974         }
2975
2976         trace_branch_enable(tr);
2977  out:
2978         mutex_unlock(&trace_types_lock);
2979
2980         return ret;
2981 }
2982
2983 static ssize_t
2984 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2985                         size_t cnt, loff_t *ppos)
2986 {
2987         char buf[max_tracer_type_len+1];
2988         int i;
2989         size_t ret;
2990         int err;
2991
2992         ret = cnt;
2993
2994         if (cnt > max_tracer_type_len)
2995                 cnt = max_tracer_type_len;
2996
2997         if (copy_from_user(&buf, ubuf, cnt))
2998                 return -EFAULT;
2999
3000         buf[cnt] = 0;
3001
3002         /* strip ending whitespace. */
3003         for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3004                 buf[i] = 0;
3005
3006         err = tracing_set_tracer(buf);
3007         if (err)
3008                 return err;
3009
3010         filp->f_pos += ret;
3011
3012         return ret;
3013 }
3014
3015 static ssize_t
3016 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3017                      size_t cnt, loff_t *ppos)
3018 {
3019         unsigned long *ptr = filp->private_data;
3020         char buf[64];
3021         int r;
3022
3023         r = snprintf(buf, sizeof(buf), "%ld\n",
3024                      *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3025         if (r > sizeof(buf))
3026                 r = sizeof(buf);
3027         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3028 }
3029
3030 static ssize_t
3031 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3032                       size_t cnt, loff_t *ppos)
3033 {
3034         long *ptr = filp->private_data;
3035         char buf[64];
3036         long val;
3037         int ret;
3038
3039         if (cnt >= sizeof(buf))
3040                 return -EINVAL;
3041
3042         if (copy_from_user(&buf, ubuf, cnt))
3043                 return -EFAULT;
3044
3045         buf[cnt] = 0;
3046
3047         ret = strict_strtoul(buf, 10, &val);
3048         if (ret < 0)
3049                 return ret;
3050
3051         *ptr = val * 1000;
3052
3053         return cnt;
3054 }
3055
3056 static atomic_t tracing_reader;
3057
3058 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3059 {
3060         struct trace_iterator *iter;
3061
3062         if (tracing_disabled)
3063                 return -ENODEV;
3064
3065         /* We only allow for reader of the pipe */
3066         if (atomic_inc_return(&tracing_reader) != 1) {
3067                 atomic_dec(&tracing_reader);
3068                 return -EBUSY;
3069         }
3070
3071         /* create a buffer to store the information to pass to userspace */
3072         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3073         if (!iter)
3074                 return -ENOMEM;
3075
3076         mutex_lock(&trace_types_lock);
3077
3078         /* trace pipe does not show start of buffer */
3079         cpus_setall(iter->started);
3080
3081         iter->tr = &global_trace;
3082         iter->trace = current_trace;
3083         filp->private_data = iter;
3084
3085         if (iter->trace->pipe_open)
3086                 iter->trace->pipe_open(iter);
3087         mutex_unlock(&trace_types_lock);
3088
3089         return 0;
3090 }
3091
3092 static int tracing_release_pipe(struct inode *inode, struct file *file)
3093 {
3094         struct trace_iterator *iter = file->private_data;
3095
3096         kfree(iter);
3097         atomic_dec(&tracing_reader);
3098
3099         return 0;
3100 }
3101
3102 static unsigned int
3103 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3104 {
3105         struct trace_iterator *iter = filp->private_data;
3106
3107         if (trace_flags & TRACE_ITER_BLOCK) {
3108                 /*
3109                  * Always select as readable when in blocking mode
3110                  */
3111                 return POLLIN | POLLRDNORM;
3112         } else {
3113                 if (!trace_empty(iter))
3114                         return POLLIN | POLLRDNORM;
3115                 poll_wait(filp, &trace_wait, poll_table);
3116                 if (!trace_empty(iter))
3117                         return POLLIN | POLLRDNORM;
3118
3119                 return 0;
3120         }
3121 }
3122
3123 /*
3124  * Consumer reader.
3125  */
3126 static ssize_t
3127 tracing_read_pipe(struct file *filp, char __user *ubuf,
3128                   size_t cnt, loff_t *ppos)
3129 {
3130         struct trace_iterator *iter = filp->private_data;
3131         ssize_t sret;
3132
3133         /* return any leftover data */
3134         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3135         if (sret != -EBUSY)
3136                 return sret;
3137
3138         trace_seq_reset(&iter->seq);
3139
3140         mutex_lock(&trace_types_lock);
3141         if (iter->trace->read) {
3142                 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3143                 if (sret)
3144                         goto out;
3145         }
3146
3147 waitagain:
3148         sret = 0;
3149         while (trace_empty(iter)) {
3150
3151                 if ((filp->f_flags & O_NONBLOCK)) {
3152                         sret = -EAGAIN;
3153                         goto out;
3154                 }
3155
3156                 /*
3157                  * This is a make-shift waitqueue. The reason we don't use
3158                  * an actual wait queue is because:
3159                  *  1) we only ever have one waiter
3160                  *  2) the tracing, traces all functions, we don't want
3161                  *     the overhead of calling wake_up and friends
3162                  *     (and tracing them too)
3163                  *     Anyway, this is really very primitive wakeup.
3164                  */
3165                 set_current_state(TASK_INTERRUPTIBLE);
3166                 iter->tr->waiter = current;
3167
3168                 mutex_unlock(&trace_types_lock);
3169
3170                 /* sleep for 100 msecs, and try again. */
3171                 schedule_timeout(HZ/10);
3172
3173                 mutex_lock(&trace_types_lock);
3174
3175                 iter->tr->waiter = NULL;
3176
3177                 if (signal_pending(current)) {
3178                         sret = -EINTR;
3179                         goto out;
3180                 }
3181
3182                 if (iter->trace != current_trace)
3183                         goto out;
3184
3185                 /*
3186                  * We block until we read something and tracing is disabled.
3187                  * We still block if tracing is disabled, but we have never
3188                  * read anything. This allows a user to cat this file, and
3189                  * then enable tracing. But after we have read something,
3190                  * we give an EOF when tracing is again disabled.
3191                  *
3192                  * iter->pos will be 0 if we haven't read anything.
3193                  */
3194                 if (!tracer_enabled && iter->pos)
3195                         break;
3196
3197                 continue;
3198         }
3199
3200         /* stop when tracing is finished */
3201         if (trace_empty(iter))
3202                 goto out;
3203
3204         if (cnt >= PAGE_SIZE)
3205                 cnt = PAGE_SIZE - 1;
3206
3207         /* reset all but tr, trace, and overruns */
3208         memset(&iter->seq, 0,
3209                sizeof(struct trace_iterator) -
3210                offsetof(struct trace_iterator, seq));
3211         iter->pos = -1;
3212
3213         while (find_next_entry_inc(iter) != NULL) {
3214                 enum print_line_t ret;
3215                 int len = iter->seq.len;
3216
3217                 ret = print_trace_line(iter);
3218                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3219                         /* don't print partial lines */
3220                         iter->seq.len = len;
3221                         break;
3222                 }
3223
3224                 trace_consume(iter);
3225
3226                 if (iter->seq.len >= cnt)
3227                         break;
3228         }
3229
3230         /* Now copy what we have to the user */
3231         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3232         if (iter->seq.readpos >= iter->seq.len)
3233                 trace_seq_reset(&iter->seq);
3234
3235         /*
3236          * If there was nothing to send to user, inspite of consuming trace
3237          * entries, go back to wait for more entries.
3238          */
3239         if (sret == -EBUSY)
3240                 goto waitagain;
3241
3242 out:
3243         mutex_unlock(&trace_types_lock);
3244
3245         return sret;
3246 }
3247
3248 static ssize_t
3249 tracing_entries_read(struct file *filp, char __user *ubuf,
3250                      size_t cnt, loff_t *ppos)
3251 {
3252         struct trace_array *tr = filp->private_data;
3253         char buf[64];
3254         int r;
3255
3256         r = sprintf(buf, "%lu\n", tr->entries >> 10);
3257         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3258 }
3259
3260 static ssize_t
3261 tracing_entries_write(struct file *filp, const char __user *ubuf,
3262                       size_t cnt, loff_t *ppos)
3263 {
3264         unsigned long val;
3265         char buf[64];
3266         int ret, cpu;
3267
3268         if (cnt >= sizeof(buf))
3269                 return -EINVAL;
3270
3271         if (copy_from_user(&buf, ubuf, cnt))
3272                 return -EFAULT;
3273
3274         buf[cnt] = 0;
3275
3276         ret = strict_strtoul(buf, 10, &val);
3277         if (ret < 0)
3278                 return ret;
3279
3280         /* must have at least 1 entry */
3281         if (!val)
3282                 return -EINVAL;
3283
3284         mutex_lock(&trace_types_lock);
3285
3286         tracing_stop();
3287
3288         /* disable all cpu buffers */
3289         for_each_tracing_cpu(cpu) {
3290                 if (global_trace.data[cpu])
3291                         atomic_inc(&global_trace.data[cpu]->disabled);
3292                 if (max_tr.data[cpu])
3293                         atomic_inc(&max_tr.data[cpu]->disabled);
3294         }
3295
3296         /* value is in KB */
3297         val <<= 10;
3298
3299         if (val != global_trace.entries) {
3300                 ret = ring_buffer_resize(global_trace.buffer, val);
3301                 if (ret < 0) {
3302                         cnt = ret;
3303                         goto out;
3304                 }
3305
3306                 ret = ring_buffer_resize(max_tr.buffer, val);
3307                 if (ret < 0) {
3308                         int r;
3309                         cnt = ret;
3310                         r = ring_buffer_resize(global_trace.buffer,
3311                                                global_trace.entries);
3312                         if (r < 0) {
3313                                 /* AARGH! We are left with different
3314                                  * size max buffer!!!! */
3315                                 WARN_ON(1);
3316                                 tracing_disabled = 1;
3317                         }
3318                         goto out;
3319                 }
3320
3321                 global_trace.entries = val;
3322         }
3323
3324         filp->f_pos += cnt;
3325
3326         /* If check pages failed, return ENOMEM */
3327         if (tracing_disabled)
3328                 cnt = -ENOMEM;
3329  out:
3330         for_each_tracing_cpu(cpu) {
3331                 if (global_trace.data[cpu])
3332                         atomic_dec(&global_trace.data[cpu]->disabled);
3333                 if (max_tr.data[cpu])
3334                         atomic_dec(&max_tr.data[cpu]->disabled);
3335         }
3336
3337         tracing_start();
3338         max_tr.entries = global_trace.entries;
3339         mutex_unlock(&trace_types_lock);
3340
3341         return cnt;
3342 }
3343
3344 static int mark_printk(const char *fmt, ...)
3345 {
3346         int ret;
3347         va_list args;
3348         va_start(args, fmt);
3349         ret = trace_vprintk(0, -1, fmt, args);
3350         va_end(args);
3351         return ret;
3352 }
3353
3354 static ssize_t
3355 tracing_mark_write(struct file *filp, const char __user *ubuf,
3356                                         size_t cnt, loff_t *fpos)
3357 {
3358         char *buf;
3359         char *end;
3360
3361         if (tracing_disabled)
3362                 return -EINVAL;
3363
3364         if (cnt > TRACE_BUF_SIZE)
3365                 cnt = TRACE_BUF_SIZE;
3366
3367         buf = kmalloc(cnt + 1, GFP_KERNEL);
3368         if (buf == NULL)
3369                 return -ENOMEM;
3370
3371         if (copy_from_user(buf, ubuf, cnt)) {
3372                 kfree(buf);
3373                 return -EFAULT;
3374         }
3375
3376         /* Cut from the first nil or newline. */
3377         buf[cnt] = '\0';
3378         end = strchr(buf, '\n');
3379         if (end)
3380                 *end = '\0';
3381
3382         cnt = mark_printk("%s\n", buf);
3383         kfree(buf);
3384         *fpos += cnt;
3385
3386         return cnt;
3387 }
3388
3389 static struct file_operations tracing_max_lat_fops = {
3390         .open           = tracing_open_generic,
3391         .read           = tracing_max_lat_read,
3392         .write          = tracing_max_lat_write,
3393 };
3394
3395 static struct file_operations tracing_ctrl_fops = {
3396         .open           = tracing_open_generic,
3397         .read           = tracing_ctrl_read,
3398         .write          = tracing_ctrl_write,
3399 };
3400
3401 static struct file_operations set_tracer_fops = {
3402         .open           = tracing_open_generic,
3403         .read           = tracing_set_trace_read,
3404         .write          = tracing_set_trace_write,
3405 };
3406
3407 static struct file_operations tracing_pipe_fops = {
3408         .open           = tracing_open_pipe,
3409         .poll           = tracing_poll_pipe,
3410         .read           = tracing_read_pipe,
3411         .release        = tracing_release_pipe,
3412 };
3413
3414 static struct file_operations tracing_entries_fops = {
3415         .open           = tracing_open_generic,
3416         .read           = tracing_entries_read,
3417         .write          = tracing_entries_write,
3418 };
3419
3420 static struct file_operations tracing_mark_fops = {
3421         .open           = tracing_open_generic,
3422         .write          = tracing_mark_write,
3423 };
3424
3425 #ifdef CONFIG_DYNAMIC_FTRACE
3426
3427 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3428 {
3429         return 0;
3430 }
3431
3432 static ssize_t
3433 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3434                   size_t cnt, loff_t *ppos)
3435 {
3436         static char ftrace_dyn_info_buffer[1024];
3437         static DEFINE_MUTEX(dyn_info_mutex);
3438         unsigned long *p = filp->private_data;
3439         char *buf = ftrace_dyn_info_buffer;
3440         int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3441         int r;
3442
3443         mutex_lock(&dyn_info_mutex);
3444         r = sprintf(buf, "%ld ", *p);
3445
3446         r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3447         buf[r++] = '\n';
3448
3449         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3450
3451         mutex_unlock(&dyn_info_mutex);
3452
3453         return r;
3454 }
3455
3456 static struct file_operations tracing_dyn_info_fops = {
3457         .open           = tracing_open_generic,
3458         .read           = tracing_read_dyn_info,
3459 };
3460 #endif
3461
3462 static struct dentry *d_tracer;
3463
3464 struct dentry *tracing_init_dentry(void)
3465 {
3466         static int once;
3467
3468         if (d_tracer)
3469                 return d_tracer;
3470
3471         d_tracer = debugfs_create_dir("tracing", NULL);
3472
3473         if (!d_tracer && !once) {
3474                 once = 1;
3475                 pr_warning("Could not create debugfs directory 'tracing'\n");
3476                 return NULL;
3477         }
3478
3479         return d_tracer;
3480 }
3481
3482 #ifdef CONFIG_FTRACE_SELFTEST
3483 /* Let selftest have access to static functions in this file */
3484 #include "trace_selftest.c"
3485 #endif
3486
3487 static __init int tracer_init_debugfs(void)
3488 {
3489         struct dentry *d_tracer;
3490         struct dentry *entry;
3491
3492         d_tracer = tracing_init_dentry();
3493
3494         entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3495                                     &global_trace, &tracing_ctrl_fops);
3496         if (!entry)
3497                 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3498
3499         entry = debugfs_create_file("trace_options", 0644, d_tracer,
3500                                     NULL, &tracing_iter_fops);
3501         if (!entry)
3502                 pr_warning("Could not create debugfs 'trace_options' entry\n");
3503
3504         entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3505                                     NULL, &tracing_cpumask_fops);
3506         if (!entry)
3507                 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3508
3509         entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3510                                     &global_trace, &tracing_lt_fops);
3511         if (!entry)
3512                 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3513
3514         entry = debugfs_create_file("trace", 0444, d_tracer,
3515                                     &global_trace, &tracing_fops);
3516         if (!entry)
3517                 pr_warning("Could not create debugfs 'trace' entry\n");
3518
3519         entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3520                                     &global_trace, &show_traces_fops);
3521         if (!entry)
3522                 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3523
3524         entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3525                                     &global_trace, &set_tracer_fops);
3526         if (!entry)
3527                 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3528
3529         entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3530                                     &tracing_max_latency,
3531                                     &tracing_max_lat_fops);
3532         if (!entry)
3533                 pr_warning("Could not create debugfs "
3534                            "'tracing_max_latency' entry\n");
3535
3536         entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3537                                     &tracing_thresh, &tracing_max_lat_fops);
3538         if (!entry)
3539                 pr_warning("Could not create debugfs "
3540                            "'tracing_thresh' entry\n");
3541         entry = debugfs_create_file("README", 0644, d_tracer,
3542                                     NULL, &tracing_readme_fops);
3543         if (!entry)
3544                 pr_warning("Could not create debugfs 'README' entry\n");
3545
3546         entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3547                                     NULL, &tracing_pipe_fops);
3548         if (!entry)
3549                 pr_warning("Could not create debugfs "
3550                            "'trace_pipe' entry\n");
3551
3552         entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3553                                     &global_trace, &tracing_entries_fops);
3554         if (!entry)
3555                 pr_warning("Could not create debugfs "
3556                            "'buffer_size_kb' entry\n");
3557
3558         entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3559                                     NULL, &tracing_mark_fops);
3560         if (!entry)
3561                 pr_warning("Could not create debugfs "
3562                            "'trace_marker' entry\n");
3563
3564 #ifdef CONFIG_DYNAMIC_FTRACE
3565         entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3566                                     &ftrace_update_tot_cnt,
3567                                     &tracing_dyn_info_fops);
3568         if (!entry)
3569                 pr_warning("Could not create debugfs "
3570                            "'dyn_ftrace_total_info' entry\n");
3571 #endif
3572 #ifdef CONFIG_SYSPROF_TRACER
3573         init_tracer_sysprof_debugfs(d_tracer);
3574 #endif
3575         return 0;
3576 }
3577
3578 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
3579 {
3580         /*
3581          * Raw Spinlock because a normal spinlock would be traced here
3582          * and append an irrelevant couple spin_lock_irqsave/
3583          * spin_unlock_irqrestore traced by ftrace around this
3584          * TRACE_PRINTK trace.
3585          */
3586         static raw_spinlock_t trace_buf_lock =
3587                                 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3588         static char trace_buf[TRACE_BUF_SIZE];
3589
3590         struct ring_buffer_event *event;
3591         struct trace_array *tr = &global_trace;
3592         struct trace_array_cpu *data;
3593         struct print_entry *entry;
3594         unsigned long flags, irq_flags;
3595         int cpu, len = 0, size, pc;
3596
3597         if (tracing_disabled)
3598                 return 0;
3599
3600         pc = preempt_count();
3601         preempt_disable_notrace();
3602         cpu = raw_smp_processor_id();
3603         data = tr->data[cpu];
3604
3605         if (unlikely(atomic_read(&data->disabled)))
3606                 goto out;
3607
3608         local_irq_save(flags);
3609         __raw_spin_lock(&trace_buf_lock);
3610         len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3611
3612         len = min(len, TRACE_BUF_SIZE-1);
3613         trace_buf[len] = 0;
3614
3615         size = sizeof(*entry) + len + 1;
3616         event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3617         if (!event)
3618                 goto out_unlock;
3619         entry = ring_buffer_event_data(event);
3620         tracing_generic_entry_update(&entry->ent, flags, pc);
3621         entry->ent.type                 = TRACE_PRINT;
3622         entry->ip                       = ip;
3623         entry->depth                    = depth;
3624
3625         memcpy(&entry->buf, trace_buf, len);
3626         entry->buf[len] = 0;
3627         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3628
3629  out_unlock:
3630         __raw_spin_unlock(&trace_buf_lock);
3631         local_irq_restore(flags);
3632
3633  out:
3634         preempt_enable_notrace();
3635
3636         return len;
3637 }
3638 EXPORT_SYMBOL_GPL(trace_vprintk);
3639
3640 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3641 {
3642         int ret;
3643         va_list ap;
3644
3645         if (!(trace_flags & TRACE_ITER_PRINTK))
3646                 return 0;
3647
3648         va_start(ap, fmt);
3649
3650 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3651         ret = trace_vprintk(ip, current->curr_ret_stack, fmt, ap);
3652 #else
3653         ret = trace_vprintk(ip, -1, fmt, ap);
3654 #endif
3655
3656         va_end(ap);
3657         return ret;
3658 }
3659 EXPORT_SYMBOL_GPL(__ftrace_printk);
3660
3661 static int trace_panic_handler(struct notifier_block *this,
3662                                unsigned long event, void *unused)
3663 {
3664         if (ftrace_dump_on_oops)
3665                 ftrace_dump();
3666         return NOTIFY_OK;
3667 }
3668
3669 static struct notifier_block trace_panic_notifier = {
3670         .notifier_call  = trace_panic_handler,
3671         .next           = NULL,
3672         .priority       = 150   /* priority: INT_MAX >= x >= 0 */
3673 };
3674
3675 static int trace_die_handler(struct notifier_block *self,
3676                              unsigned long val,
3677                              void *data)
3678 {
3679         switch (val) {
3680         case DIE_OOPS:
3681                 if (ftrace_dump_on_oops)
3682                         ftrace_dump();
3683                 break;
3684         default:
3685                 break;
3686         }
3687         return NOTIFY_OK;
3688 }
3689
3690 static struct notifier_block trace_die_notifier = {
3691         .notifier_call = trace_die_handler,
3692         .priority = 200
3693 };
3694
3695 /*
3696  * printk is set to max of 1024, we really don't need it that big.
3697  * Nothing should be printing 1000 characters anyway.
3698  */
3699 #define TRACE_MAX_PRINT         1000
3700
3701 /*
3702  * Define here KERN_TRACE so that we have one place to modify
3703  * it if we decide to change what log level the ftrace dump
3704  * should be at.
3705  */
3706 #define KERN_TRACE              KERN_INFO
3707
3708 static void
3709 trace_printk_seq(struct trace_seq *s)
3710 {
3711         /* Probably should print a warning here. */
3712         if (s->len >= 1000)
3713                 s->len = 1000;
3714
3715         /* should be zero ended, but we are paranoid. */
3716         s->buffer[s->len] = 0;
3717
3718         printk(KERN_TRACE "%s", s->buffer);
3719
3720         trace_seq_reset(s);
3721 }
3722
3723 void ftrace_dump(void)
3724 {
3725         static DEFINE_SPINLOCK(ftrace_dump_lock);
3726         /* use static because iter can be a bit big for the stack */
3727         static struct trace_iterator iter;
3728         static cpumask_t mask;
3729         static int dump_ran;
3730         unsigned long flags;
3731         int cnt = 0, cpu;
3732
3733         /* only one dump */
3734         spin_lock_irqsave(&ftrace_dump_lock, flags);
3735         if (dump_ran)
3736                 goto out;
3737
3738         dump_ran = 1;
3739
3740         /* No turning back! */
3741         ftrace_kill();
3742
3743         for_each_tracing_cpu(cpu) {
3744                 atomic_inc(&global_trace.data[cpu]->disabled);
3745         }
3746
3747         /* don't look at user memory in panic mode */
3748         trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3749
3750         printk(KERN_TRACE "Dumping ftrace buffer:\n");
3751
3752         iter.tr = &global_trace;
3753         iter.trace = current_trace;
3754
3755         /*
3756          * We need to stop all tracing on all CPUS to read the
3757          * the next buffer. This is a bit expensive, but is
3758          * not done often. We fill all what we can read,
3759          * and then release the locks again.
3760          */
3761
3762         cpus_clear(mask);
3763
3764         while (!trace_empty(&iter)) {
3765
3766                 if (!cnt)
3767                         printk(KERN_TRACE "---------------------------------\n");
3768
3769                 cnt++;
3770
3771                 /* reset all but tr, trace, and overruns */
3772                 memset(&iter.seq, 0,
3773                        sizeof(struct trace_iterator) -
3774                        offsetof(struct trace_iterator, seq));
3775                 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3776                 iter.pos = -1;
3777
3778                 if (find_next_entry_inc(&iter) != NULL) {
3779                         print_trace_line(&iter);
3780                         trace_consume(&iter);
3781                 }
3782
3783                 trace_printk_seq(&iter.seq);
3784         }
3785
3786         if (!cnt)
3787                 printk(KERN_TRACE "   (ftrace buffer empty)\n");
3788         else
3789                 printk(KERN_TRACE "---------------------------------\n");
3790
3791  out:
3792         spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3793 }
3794
3795 __init static int tracer_alloc_buffers(void)
3796 {
3797         struct trace_array_cpu *data;
3798         int i;
3799
3800         /* TODO: make the number of buffers hot pluggable with CPUS */
3801         tracing_buffer_mask = cpu_possible_map;
3802
3803         global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3804                                                    TRACE_BUFFER_FLAGS);
3805         if (!global_trace.buffer) {
3806                 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3807                 WARN_ON(1);
3808                 return 0;
3809         }
3810         global_trace.entries = ring_buffer_size(global_trace.buffer);
3811
3812 #ifdef CONFIG_TRACER_MAX_TRACE
3813         max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3814                                              TRACE_BUFFER_FLAGS);
3815         if (!max_tr.buffer) {
3816                 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3817                 WARN_ON(1);
3818                 ring_buffer_free(global_trace.buffer);
3819                 return 0;
3820         }
3821         max_tr.entries = ring_buffer_size(max_tr.buffer);
3822         WARN_ON(max_tr.entries != global_trace.entries);
3823 #endif
3824
3825         /* Allocate the first page for all buffers */
3826         for_each_tracing_cpu(i) {
3827                 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3828                 max_tr.data[i] = &per_cpu(max_data, i);
3829         }
3830
3831         trace_init_cmdlines();
3832
3833         register_tracer(&nop_trace);
3834 #ifdef CONFIG_BOOT_TRACER
3835         register_tracer(&boot_tracer);
3836         current_trace = &boot_tracer;
3837         current_trace->init(&global_trace);
3838 #else
3839         current_trace = &nop_trace;
3840 #endif
3841
3842         /* All seems OK, enable tracing */
3843         tracing_disabled = 0;
3844
3845         atomic_notifier_chain_register(&panic_notifier_list,
3846                                        &trace_panic_notifier);
3847
3848         register_die_notifier(&trace_die_notifier);
3849
3850         return 0;
3851 }
3852 early_initcall(tracer_alloc_buffers);
3853 fs_initcall(tracer_init_debugfs);