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