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