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