]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - kernel/trace/trace.c
ftrace: insert in the ftrace_preempt_disable()/enable() functions
[linux-2.6-omap-h63xx.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 #ifdef CONFIG_STACKTRACE
740         struct ring_buffer_event *event;
741         struct stack_entry *entry;
742         struct stack_trace trace;
743         unsigned long irq_flags;
744
745         if (!(trace_flags & TRACE_ITER_STACKTRACE))
746                 return;
747
748         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
749                                          &irq_flags);
750         if (!event)
751                 return;
752         entry   = ring_buffer_event_data(event);
753         tracing_generic_entry_update(&entry->ent, flags, pc);
754         entry->ent.type         = TRACE_STACK;
755
756         memset(&entry->caller, 0, sizeof(entry->caller));
757
758         trace.nr_entries        = 0;
759         trace.max_entries       = FTRACE_STACK_ENTRIES;
760         trace.skip              = skip;
761         trace.entries           = entry->caller;
762
763         save_stack_trace(&trace);
764         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
765 #endif
766 }
767
768 void __trace_stack(struct trace_array *tr,
769                    struct trace_array_cpu *data,
770                    unsigned long flags,
771                    int skip)
772 {
773         ftrace_trace_stack(tr, data, flags, skip, preempt_count());
774 }
775
776 static void
777 ftrace_trace_special(void *__tr, void *__data,
778                      unsigned long arg1, unsigned long arg2, unsigned long arg3,
779                      int pc)
780 {
781         struct ring_buffer_event *event;
782         struct trace_array_cpu *data = __data;
783         struct trace_array *tr = __tr;
784         struct special_entry *entry;
785         unsigned long irq_flags;
786
787         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
788                                          &irq_flags);
789         if (!event)
790                 return;
791         entry   = ring_buffer_event_data(event);
792         tracing_generic_entry_update(&entry->ent, 0, pc);
793         entry->ent.type                 = TRACE_SPECIAL;
794         entry->arg1                     = arg1;
795         entry->arg2                     = arg2;
796         entry->arg3                     = arg3;
797         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
798         ftrace_trace_stack(tr, data, irq_flags, 4, pc);
799
800         trace_wake_up();
801 }
802
803 void
804 __trace_special(void *__tr, void *__data,
805                 unsigned long arg1, unsigned long arg2, unsigned long arg3)
806 {
807         ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
808 }
809
810 void
811 tracing_sched_switch_trace(struct trace_array *tr,
812                            struct trace_array_cpu *data,
813                            struct task_struct *prev,
814                            struct task_struct *next,
815                            unsigned long flags, int pc)
816 {
817         struct ring_buffer_event *event;
818         struct ctx_switch_entry *entry;
819         unsigned long irq_flags;
820
821         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
822                                            &irq_flags);
823         if (!event)
824                 return;
825         entry   = ring_buffer_event_data(event);
826         tracing_generic_entry_update(&entry->ent, flags, pc);
827         entry->ent.type                 = TRACE_CTX;
828         entry->prev_pid                 = prev->pid;
829         entry->prev_prio                = prev->prio;
830         entry->prev_state               = prev->state;
831         entry->next_pid                 = next->pid;
832         entry->next_prio                = next->prio;
833         entry->next_state               = next->state;
834         entry->next_cpu = task_cpu(next);
835         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
836         ftrace_trace_stack(tr, data, flags, 5, pc);
837 }
838
839 void
840 tracing_sched_wakeup_trace(struct trace_array *tr,
841                            struct trace_array_cpu *data,
842                            struct task_struct *wakee,
843                            struct task_struct *curr,
844                            unsigned long flags, int pc)
845 {
846         struct ring_buffer_event *event;
847         struct ctx_switch_entry *entry;
848         unsigned long irq_flags;
849
850         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
851                                            &irq_flags);
852         if (!event)
853                 return;
854         entry   = ring_buffer_event_data(event);
855         tracing_generic_entry_update(&entry->ent, flags, pc);
856         entry->ent.type                 = TRACE_WAKE;
857         entry->prev_pid                 = curr->pid;
858         entry->prev_prio                = curr->prio;
859         entry->prev_state               = curr->state;
860         entry->next_pid                 = wakee->pid;
861         entry->next_prio                = wakee->prio;
862         entry->next_state               = wakee->state;
863         entry->next_cpu                 = task_cpu(wakee);
864         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
865         ftrace_trace_stack(tr, data, flags, 6, pc);
866
867         trace_wake_up();
868 }
869
870 void
871 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
872 {
873         struct trace_array *tr = &global_trace;
874         struct trace_array_cpu *data;
875         int cpu;
876         int pc;
877
878         if (tracing_disabled || !tr->ctrl)
879                 return;
880
881         pc = preempt_count();
882         preempt_disable_notrace();
883         cpu = raw_smp_processor_id();
884         data = tr->data[cpu];
885
886         if (likely(!atomic_read(&data->disabled)))
887                 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
888
889         preempt_enable_notrace();
890 }
891
892 #ifdef CONFIG_FUNCTION_TRACER
893 static void
894 function_trace_call(unsigned long ip, unsigned long parent_ip)
895 {
896         struct trace_array *tr = &global_trace;
897         struct trace_array_cpu *data;
898         unsigned long flags;
899         long disabled;
900         int cpu, resched;
901         int pc;
902
903         if (unlikely(!ftrace_function_enabled))
904                 return;
905
906         pc = preempt_count();
907         resched = ftrace_preempt_disable();
908         local_save_flags(flags);
909         cpu = raw_smp_processor_id();
910         data = tr->data[cpu];
911         disabled = atomic_inc_return(&data->disabled);
912
913         if (likely(disabled == 1))
914                 trace_function(tr, data, ip, parent_ip, flags, pc);
915
916         atomic_dec(&data->disabled);
917         ftrace_preempt_enable(resched);
918 }
919
920 static struct ftrace_ops trace_ops __read_mostly =
921 {
922         .func = function_trace_call,
923 };
924
925 void tracing_start_function_trace(void)
926 {
927         ftrace_function_enabled = 0;
928         register_ftrace_function(&trace_ops);
929         if (tracer_enabled)
930                 ftrace_function_enabled = 1;
931 }
932
933 void tracing_stop_function_trace(void)
934 {
935         ftrace_function_enabled = 0;
936         unregister_ftrace_function(&trace_ops);
937 }
938 #endif
939
940 enum trace_file_type {
941         TRACE_FILE_LAT_FMT      = 1,
942 };
943
944 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
945 {
946         /* Don't allow ftrace to trace into the ring buffers */
947         ftrace_disable_cpu();
948
949         iter->idx++;
950         if (iter->buffer_iter[iter->cpu])
951                 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
952
953         ftrace_enable_cpu();
954 }
955
956 static struct trace_entry *
957 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
958 {
959         struct ring_buffer_event *event;
960         struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
961
962         /* Don't allow ftrace to trace into the ring buffers */
963         ftrace_disable_cpu();
964
965         if (buf_iter)
966                 event = ring_buffer_iter_peek(buf_iter, ts);
967         else
968                 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
969
970         ftrace_enable_cpu();
971
972         return event ? ring_buffer_event_data(event) : NULL;
973 }
974
975 static struct trace_entry *
976 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
977 {
978         struct ring_buffer *buffer = iter->tr->buffer;
979         struct trace_entry *ent, *next = NULL;
980         u64 next_ts = 0, ts;
981         int next_cpu = -1;
982         int cpu;
983
984         for_each_tracing_cpu(cpu) {
985
986                 if (ring_buffer_empty_cpu(buffer, cpu))
987                         continue;
988
989                 ent = peek_next_entry(iter, cpu, &ts);
990
991                 /*
992                  * Pick the entry with the smallest timestamp:
993                  */
994                 if (ent && (!next || ts < next_ts)) {
995                         next = ent;
996                         next_cpu = cpu;
997                         next_ts = ts;
998                 }
999         }
1000
1001         if (ent_cpu)
1002                 *ent_cpu = next_cpu;
1003
1004         if (ent_ts)
1005                 *ent_ts = next_ts;
1006
1007         return next;
1008 }
1009
1010 /* Find the next real entry, without updating the iterator itself */
1011 static struct trace_entry *
1012 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1013 {
1014         return __find_next_entry(iter, ent_cpu, ent_ts);
1015 }
1016
1017 /* Find the next real entry, and increment the iterator to the next entry */
1018 static void *find_next_entry_inc(struct trace_iterator *iter)
1019 {
1020         iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1021
1022         if (iter->ent)
1023                 trace_iterator_increment(iter, iter->cpu);
1024
1025         return iter->ent ? iter : NULL;
1026 }
1027
1028 static void trace_consume(struct trace_iterator *iter)
1029 {
1030         /* Don't allow ftrace to trace into the ring buffers */
1031         ftrace_disable_cpu();
1032         ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1033         ftrace_enable_cpu();
1034 }
1035
1036 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1037 {
1038         struct trace_iterator *iter = m->private;
1039         int i = (int)*pos;
1040         void *ent;
1041
1042         (*pos)++;
1043
1044         /* can't go backwards */
1045         if (iter->idx > i)
1046                 return NULL;
1047
1048         if (iter->idx < 0)
1049                 ent = find_next_entry_inc(iter);
1050         else
1051                 ent = iter;
1052
1053         while (ent && iter->idx < i)
1054                 ent = find_next_entry_inc(iter);
1055
1056         iter->pos = *pos;
1057
1058         return ent;
1059 }
1060
1061 static void *s_start(struct seq_file *m, loff_t *pos)
1062 {
1063         struct trace_iterator *iter = m->private;
1064         void *p = NULL;
1065         loff_t l = 0;
1066         int cpu;
1067
1068         mutex_lock(&trace_types_lock);
1069
1070         if (!current_trace || current_trace != iter->trace) {
1071                 mutex_unlock(&trace_types_lock);
1072                 return NULL;
1073         }
1074
1075         atomic_inc(&trace_record_cmdline_disabled);
1076
1077         /* let the tracer grab locks here if needed */
1078         if (current_trace->start)
1079                 current_trace->start(iter);
1080
1081         if (*pos != iter->pos) {
1082                 iter->ent = NULL;
1083                 iter->cpu = 0;
1084                 iter->idx = -1;
1085
1086                 ftrace_disable_cpu();
1087
1088                 for_each_tracing_cpu(cpu) {
1089                         ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1090                 }
1091
1092                 ftrace_enable_cpu();
1093
1094                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1095                         ;
1096
1097         } else {
1098                 l = *pos - 1;
1099                 p = s_next(m, p, &l);
1100         }
1101
1102         return p;
1103 }
1104
1105 static void s_stop(struct seq_file *m, void *p)
1106 {
1107         struct trace_iterator *iter = m->private;
1108
1109         atomic_dec(&trace_record_cmdline_disabled);
1110
1111         /* let the tracer release locks here if needed */
1112         if (current_trace && current_trace == iter->trace && iter->trace->stop)
1113                 iter->trace->stop(iter);
1114
1115         mutex_unlock(&trace_types_lock);
1116 }
1117
1118 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1119
1120 #ifdef CONFIG_KRETPROBES
1121 static inline int kretprobed(unsigned long addr)
1122 {
1123         return addr == (unsigned long)kretprobe_trampoline;
1124 }
1125 #else
1126 static inline int kretprobed(unsigned long addr)
1127 {
1128         return 0;
1129 }
1130 #endif /* CONFIG_KRETPROBES */
1131
1132 static int
1133 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1134 {
1135 #ifdef CONFIG_KALLSYMS
1136         char str[KSYM_SYMBOL_LEN];
1137
1138         kallsyms_lookup(address, NULL, NULL, NULL, str);
1139
1140         return trace_seq_printf(s, fmt, str);
1141 #endif
1142         return 1;
1143 }
1144
1145 static int
1146 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1147                      unsigned long address)
1148 {
1149 #ifdef CONFIG_KALLSYMS
1150         char str[KSYM_SYMBOL_LEN];
1151
1152         sprint_symbol(str, address);
1153         return trace_seq_printf(s, fmt, str);
1154 #endif
1155         return 1;
1156 }
1157
1158 #ifndef CONFIG_64BIT
1159 # define IP_FMT "%08lx"
1160 #else
1161 # define IP_FMT "%016lx"
1162 #endif
1163
1164 static int
1165 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1166 {
1167         int ret;
1168
1169         if (!ip)
1170                 return trace_seq_printf(s, "0");
1171
1172         if (sym_flags & TRACE_ITER_SYM_OFFSET)
1173                 ret = seq_print_sym_offset(s, "%s", ip);
1174         else
1175                 ret = seq_print_sym_short(s, "%s", ip);
1176
1177         if (!ret)
1178                 return 0;
1179
1180         if (sym_flags & TRACE_ITER_SYM_ADDR)
1181                 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1182         return ret;
1183 }
1184
1185 static void print_lat_help_header(struct seq_file *m)
1186 {
1187         seq_puts(m, "#                  _------=> CPU#            \n");
1188         seq_puts(m, "#                 / _-----=> irqs-off        \n");
1189         seq_puts(m, "#                | / _----=> need-resched    \n");
1190         seq_puts(m, "#                || / _---=> hardirq/softirq \n");
1191         seq_puts(m, "#                ||| / _--=> preempt-depth   \n");
1192         seq_puts(m, "#                |||| /                      \n");
1193         seq_puts(m, "#                |||||     delay             \n");
1194         seq_puts(m, "#  cmd     pid   ||||| time  |   caller      \n");
1195         seq_puts(m, "#     \\   /      |||||   \\   |   /           \n");
1196 }
1197
1198 static void print_func_help_header(struct seq_file *m)
1199 {
1200         seq_puts(m, "#           TASK-PID    CPU#    TIMESTAMP  FUNCTION\n");
1201         seq_puts(m, "#              | |       |          |         |\n");
1202 }
1203
1204
1205 static void
1206 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1207 {
1208         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1209         struct trace_array *tr = iter->tr;
1210         struct trace_array_cpu *data = tr->data[tr->cpu];
1211         struct tracer *type = current_trace;
1212         unsigned long total;
1213         unsigned long entries;
1214         const char *name = "preemption";
1215
1216         if (type)
1217                 name = type->name;
1218
1219         entries = ring_buffer_entries(iter->tr->buffer);
1220         total = entries +
1221                 ring_buffer_overruns(iter->tr->buffer);
1222
1223         seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1224                    name, UTS_RELEASE);
1225         seq_puts(m, "-----------------------------------"
1226                  "---------------------------------\n");
1227         seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1228                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1229                    nsecs_to_usecs(data->saved_latency),
1230                    entries,
1231                    total,
1232                    tr->cpu,
1233 #if defined(CONFIG_PREEMPT_NONE)
1234                    "server",
1235 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1236                    "desktop",
1237 #elif defined(CONFIG_PREEMPT)
1238                    "preempt",
1239 #else
1240                    "unknown",
1241 #endif
1242                    /* These are reserved for later use */
1243                    0, 0, 0, 0);
1244 #ifdef CONFIG_SMP
1245         seq_printf(m, " #P:%d)\n", num_online_cpus());
1246 #else
1247         seq_puts(m, ")\n");
1248 #endif
1249         seq_puts(m, "    -----------------\n");
1250         seq_printf(m, "    | task: %.16s-%d "
1251                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1252                    data->comm, data->pid, data->uid, data->nice,
1253                    data->policy, data->rt_priority);
1254         seq_puts(m, "    -----------------\n");
1255
1256         if (data->critical_start) {
1257                 seq_puts(m, " => started at: ");
1258                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1259                 trace_print_seq(m, &iter->seq);
1260                 seq_puts(m, "\n => ended at:   ");
1261                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1262                 trace_print_seq(m, &iter->seq);
1263                 seq_puts(m, "\n");
1264         }
1265
1266         seq_puts(m, "\n");
1267 }
1268
1269 static void
1270 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1271 {
1272         int hardirq, softirq;
1273         char *comm;
1274
1275         comm = trace_find_cmdline(entry->pid);
1276
1277         trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1278         trace_seq_printf(s, "%3d", cpu);
1279         trace_seq_printf(s, "%c%c",
1280                         (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1281                          (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1282                         ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1283
1284         hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1285         softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1286         if (hardirq && softirq) {
1287                 trace_seq_putc(s, 'H');
1288         } else {
1289                 if (hardirq) {
1290                         trace_seq_putc(s, 'h');
1291                 } else {
1292                         if (softirq)
1293                                 trace_seq_putc(s, 's');
1294                         else
1295                                 trace_seq_putc(s, '.');
1296                 }
1297         }
1298
1299         if (entry->preempt_count)
1300                 trace_seq_printf(s, "%x", entry->preempt_count);
1301         else
1302                 trace_seq_puts(s, ".");
1303 }
1304
1305 unsigned long preempt_mark_thresh = 100;
1306
1307 static void
1308 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1309                     unsigned long rel_usecs)
1310 {
1311         trace_seq_printf(s, " %4lldus", abs_usecs);
1312         if (rel_usecs > preempt_mark_thresh)
1313                 trace_seq_puts(s, "!: ");
1314         else if (rel_usecs > 1)
1315                 trace_seq_puts(s, "+: ");
1316         else
1317                 trace_seq_puts(s, " : ");
1318 }
1319
1320 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1321
1322 /*
1323  * The message is supposed to contain an ending newline.
1324  * If the printing stops prematurely, try to add a newline of our own.
1325  */
1326 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1327 {
1328         struct trace_entry *ent;
1329         struct trace_field_cont *cont;
1330         bool ok = true;
1331
1332         ent = peek_next_entry(iter, iter->cpu, NULL);
1333         if (!ent || ent->type != TRACE_CONT) {
1334                 trace_seq_putc(s, '\n');
1335                 return;
1336         }
1337
1338         do {
1339                 cont = (struct trace_field_cont *)ent;
1340                 if (ok)
1341                         ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1342
1343                 ftrace_disable_cpu();
1344
1345                 if (iter->buffer_iter[iter->cpu])
1346                         ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1347                 else
1348                         ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1349
1350                 ftrace_enable_cpu();
1351
1352                 ent = peek_next_entry(iter, iter->cpu, NULL);
1353         } while (ent && ent->type == TRACE_CONT);
1354
1355         if (!ok)
1356                 trace_seq_putc(s, '\n');
1357 }
1358
1359 static enum print_line_t
1360 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1361 {
1362         struct trace_seq *s = &iter->seq;
1363         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1364         struct trace_entry *next_entry;
1365         unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1366         struct trace_entry *entry = iter->ent;
1367         unsigned long abs_usecs;
1368         unsigned long rel_usecs;
1369         u64 next_ts;
1370         char *comm;
1371         int S, T;
1372         int i;
1373         unsigned state;
1374
1375         if (entry->type == TRACE_CONT)
1376                 return TRACE_TYPE_HANDLED;
1377
1378         next_entry = find_next_entry(iter, NULL, &next_ts);
1379         if (!next_entry)
1380                 next_ts = iter->ts;
1381         rel_usecs = ns2usecs(next_ts - iter->ts);
1382         abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1383
1384         if (verbose) {
1385                 comm = trace_find_cmdline(entry->pid);
1386                 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1387                                  " %ld.%03ldms (+%ld.%03ldms): ",
1388                                  comm,
1389                                  entry->pid, cpu, entry->flags,
1390                                  entry->preempt_count, trace_idx,
1391                                  ns2usecs(iter->ts),
1392                                  abs_usecs/1000,
1393                                  abs_usecs % 1000, rel_usecs/1000,
1394                                  rel_usecs % 1000);
1395         } else {
1396                 lat_print_generic(s, entry, cpu);
1397                 lat_print_timestamp(s, abs_usecs, rel_usecs);
1398         }
1399         switch (entry->type) {
1400         case TRACE_FN: {
1401                 struct ftrace_entry *field;
1402
1403                 trace_assign_type(field, entry);
1404
1405                 seq_print_ip_sym(s, field->ip, sym_flags);
1406                 trace_seq_puts(s, " (");
1407                 if (kretprobed(field->parent_ip))
1408                         trace_seq_puts(s, KRETPROBE_MSG);
1409                 else
1410                         seq_print_ip_sym(s, field->parent_ip, sym_flags);
1411                 trace_seq_puts(s, ")\n");
1412                 break;
1413         }
1414         case TRACE_CTX:
1415         case TRACE_WAKE: {
1416                 struct ctx_switch_entry *field;
1417
1418                 trace_assign_type(field, entry);
1419
1420                 T = field->next_state < sizeof(state_to_char) ?
1421                         state_to_char[field->next_state] : 'X';
1422
1423                 state = field->prev_state ?
1424                         __ffs(field->prev_state) + 1 : 0;
1425                 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1426                 comm = trace_find_cmdline(field->next_pid);
1427                 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1428                                  field->prev_pid,
1429                                  field->prev_prio,
1430                                  S, entry->type == TRACE_CTX ? "==>" : "  +",
1431                                  field->next_cpu,
1432                                  field->next_pid,
1433                                  field->next_prio,
1434                                  T, comm);
1435                 break;
1436         }
1437         case TRACE_SPECIAL: {
1438                 struct special_entry *field;
1439
1440                 trace_assign_type(field, entry);
1441
1442                 trace_seq_printf(s, "# %ld %ld %ld\n",
1443                                  field->arg1,
1444                                  field->arg2,
1445                                  field->arg3);
1446                 break;
1447         }
1448         case TRACE_STACK: {
1449                 struct stack_entry *field;
1450
1451                 trace_assign_type(field, entry);
1452
1453                 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1454                         if (i)
1455                                 trace_seq_puts(s, " <= ");
1456                         seq_print_ip_sym(s, field->caller[i], sym_flags);
1457                 }
1458                 trace_seq_puts(s, "\n");
1459                 break;
1460         }
1461         case TRACE_PRINT: {
1462                 struct print_entry *field;
1463
1464                 trace_assign_type(field, entry);
1465
1466                 seq_print_ip_sym(s, field->ip, sym_flags);
1467                 trace_seq_printf(s, ": %s", field->buf);
1468                 if (entry->flags & TRACE_FLAG_CONT)
1469                         trace_seq_print_cont(s, iter);
1470                 break;
1471         }
1472         default:
1473                 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1474         }
1475         return TRACE_TYPE_HANDLED;
1476 }
1477
1478 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1479 {
1480         struct trace_seq *s = &iter->seq;
1481         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1482         struct trace_entry *entry;
1483         unsigned long usec_rem;
1484         unsigned long long t;
1485         unsigned long secs;
1486         char *comm;
1487         int ret;
1488         int S, T;
1489         int i;
1490
1491         entry = iter->ent;
1492
1493         if (entry->type == TRACE_CONT)
1494                 return TRACE_TYPE_HANDLED;
1495
1496         comm = trace_find_cmdline(iter->ent->pid);
1497
1498         t = ns2usecs(iter->ts);
1499         usec_rem = do_div(t, 1000000ULL);
1500         secs = (unsigned long)t;
1501
1502         ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1503         if (!ret)
1504                 return TRACE_TYPE_PARTIAL_LINE;
1505         ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1506         if (!ret)
1507                 return TRACE_TYPE_PARTIAL_LINE;
1508         ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1509         if (!ret)
1510                 return TRACE_TYPE_PARTIAL_LINE;
1511
1512         switch (entry->type) {
1513         case TRACE_FN: {
1514                 struct ftrace_entry *field;
1515
1516                 trace_assign_type(field, entry);
1517
1518                 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1519                 if (!ret)
1520                         return TRACE_TYPE_PARTIAL_LINE;
1521                 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1522                                                 field->parent_ip) {
1523                         ret = trace_seq_printf(s, " <-");
1524                         if (!ret)
1525                                 return TRACE_TYPE_PARTIAL_LINE;
1526                         if (kretprobed(field->parent_ip))
1527                                 ret = trace_seq_puts(s, KRETPROBE_MSG);
1528                         else
1529                                 ret = seq_print_ip_sym(s,
1530                                                        field->parent_ip,
1531                                                        sym_flags);
1532                         if (!ret)
1533                                 return TRACE_TYPE_PARTIAL_LINE;
1534                 }
1535                 ret = trace_seq_printf(s, "\n");
1536                 if (!ret)
1537                         return TRACE_TYPE_PARTIAL_LINE;
1538                 break;
1539         }
1540         case TRACE_CTX:
1541         case TRACE_WAKE: {
1542                 struct ctx_switch_entry *field;
1543
1544                 trace_assign_type(field, entry);
1545
1546                 S = field->prev_state < sizeof(state_to_char) ?
1547                         state_to_char[field->prev_state] : 'X';
1548                 T = field->next_state < sizeof(state_to_char) ?
1549                         state_to_char[field->next_state] : 'X';
1550                 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1551                                        field->prev_pid,
1552                                        field->prev_prio,
1553                                        S,
1554                                        entry->type == TRACE_CTX ? "==>" : "  +",
1555                                        field->next_cpu,
1556                                        field->next_pid,
1557                                        field->next_prio,
1558                                        T);
1559                 if (!ret)
1560                         return TRACE_TYPE_PARTIAL_LINE;
1561                 break;
1562         }
1563         case TRACE_SPECIAL: {
1564                 struct special_entry *field;
1565
1566                 trace_assign_type(field, entry);
1567
1568                 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1569                                  field->arg1,
1570                                  field->arg2,
1571                                  field->arg3);
1572                 if (!ret)
1573                         return TRACE_TYPE_PARTIAL_LINE;
1574                 break;
1575         }
1576         case TRACE_STACK: {
1577                 struct stack_entry *field;
1578
1579                 trace_assign_type(field, entry);
1580
1581                 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1582                         if (i) {
1583                                 ret = trace_seq_puts(s, " <= ");
1584                                 if (!ret)
1585                                         return TRACE_TYPE_PARTIAL_LINE;
1586                         }
1587                         ret = seq_print_ip_sym(s, field->caller[i],
1588                                                sym_flags);
1589                         if (!ret)
1590                                 return TRACE_TYPE_PARTIAL_LINE;
1591                 }
1592                 ret = trace_seq_puts(s, "\n");
1593                 if (!ret)
1594                         return TRACE_TYPE_PARTIAL_LINE;
1595                 break;
1596         }
1597         case TRACE_PRINT: {
1598                 struct print_entry *field;
1599
1600                 trace_assign_type(field, entry);
1601
1602                 seq_print_ip_sym(s, field->ip, sym_flags);
1603                 trace_seq_printf(s, ": %s", field->buf);
1604                 if (entry->flags & TRACE_FLAG_CONT)
1605                         trace_seq_print_cont(s, iter);
1606                 break;
1607         }
1608         }
1609         return TRACE_TYPE_HANDLED;
1610 }
1611
1612 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1613 {
1614         struct trace_seq *s = &iter->seq;
1615         struct trace_entry *entry;
1616         int ret;
1617         int S, T;
1618
1619         entry = iter->ent;
1620
1621         if (entry->type == TRACE_CONT)
1622                 return TRACE_TYPE_HANDLED;
1623
1624         ret = trace_seq_printf(s, "%d %d %llu ",
1625                 entry->pid, iter->cpu, iter->ts);
1626         if (!ret)
1627                 return TRACE_TYPE_PARTIAL_LINE;
1628
1629         switch (entry->type) {
1630         case TRACE_FN: {
1631                 struct ftrace_entry *field;
1632
1633                 trace_assign_type(field, entry);
1634
1635                 ret = trace_seq_printf(s, "%x %x\n",
1636                                         field->ip,
1637                                         field->parent_ip);
1638                 if (!ret)
1639                         return TRACE_TYPE_PARTIAL_LINE;
1640                 break;
1641         }
1642         case TRACE_CTX:
1643         case TRACE_WAKE: {
1644                 struct ctx_switch_entry *field;
1645
1646                 trace_assign_type(field, entry);
1647
1648                 S = field->prev_state < sizeof(state_to_char) ?
1649                         state_to_char[field->prev_state] : 'X';
1650                 T = field->next_state < sizeof(state_to_char) ?
1651                         state_to_char[field->next_state] : 'X';
1652                 if (entry->type == TRACE_WAKE)
1653                         S = '+';
1654                 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
1655                                        field->prev_pid,
1656                                        field->prev_prio,
1657                                        S,
1658                                        field->next_cpu,
1659                                        field->next_pid,
1660                                        field->next_prio,
1661                                        T);
1662                 if (!ret)
1663                         return TRACE_TYPE_PARTIAL_LINE;
1664                 break;
1665         }
1666         case TRACE_SPECIAL:
1667         case TRACE_STACK: {
1668                 struct special_entry *field;
1669
1670                 trace_assign_type(field, entry);
1671
1672                 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1673                                  field->arg1,
1674                                  field->arg2,
1675                                  field->arg3);
1676                 if (!ret)
1677                         return TRACE_TYPE_PARTIAL_LINE;
1678                 break;
1679         }
1680         case TRACE_PRINT: {
1681                 struct print_entry *field;
1682
1683                 trace_assign_type(field, entry);
1684
1685                 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
1686                 if (entry->flags & TRACE_FLAG_CONT)
1687                         trace_seq_print_cont(s, iter);
1688                 break;
1689         }
1690         }
1691         return TRACE_TYPE_HANDLED;
1692 }
1693
1694 #define SEQ_PUT_FIELD_RET(s, x)                         \
1695 do {                                                    \
1696         if (!trace_seq_putmem(s, &(x), sizeof(x)))      \
1697                 return 0;                               \
1698 } while (0)
1699
1700 #define SEQ_PUT_HEX_FIELD_RET(s, x)                     \
1701 do {                                                    \
1702         BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES);     \
1703         if (!trace_seq_putmem_hex(s, &(x), sizeof(x)))  \
1704                 return 0;                               \
1705 } while (0)
1706
1707 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1708 {
1709         struct trace_seq *s = &iter->seq;
1710         unsigned char newline = '\n';
1711         struct trace_entry *entry;
1712         int S, T;
1713
1714         entry = iter->ent;
1715
1716         if (entry->type == TRACE_CONT)
1717                 return TRACE_TYPE_HANDLED;
1718
1719         SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1720         SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1721         SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1722
1723         switch (entry->type) {
1724         case TRACE_FN: {
1725                 struct ftrace_entry *field;
1726
1727                 trace_assign_type(field, entry);
1728
1729                 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1730                 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
1731                 break;
1732         }
1733         case TRACE_CTX:
1734         case TRACE_WAKE: {
1735                 struct ctx_switch_entry *field;
1736
1737                 trace_assign_type(field, entry);
1738
1739                 S = field->prev_state < sizeof(state_to_char) ?
1740                         state_to_char[field->prev_state] : 'X';
1741                 T = field->next_state < sizeof(state_to_char) ?
1742                         state_to_char[field->next_state] : 'X';
1743                 if (entry->type == TRACE_WAKE)
1744                         S = '+';
1745                 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1746                 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
1747                 SEQ_PUT_HEX_FIELD_RET(s, S);
1748                 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1749                 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1750                 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
1751                 SEQ_PUT_HEX_FIELD_RET(s, T);
1752                 break;
1753         }
1754         case TRACE_SPECIAL:
1755         case TRACE_STACK: {
1756                 struct special_entry *field;
1757
1758                 trace_assign_type(field, entry);
1759
1760                 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
1761                 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
1762                 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
1763                 break;
1764         }
1765         }
1766         SEQ_PUT_FIELD_RET(s, newline);
1767
1768         return TRACE_TYPE_HANDLED;
1769 }
1770
1771 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1772 {
1773         struct trace_seq *s = &iter->seq;
1774         struct trace_entry *entry;
1775
1776         entry = iter->ent;
1777
1778         if (entry->type == TRACE_CONT)
1779                 return TRACE_TYPE_HANDLED;
1780
1781         SEQ_PUT_FIELD_RET(s, entry->pid);
1782         SEQ_PUT_FIELD_RET(s, iter->cpu);
1783         SEQ_PUT_FIELD_RET(s, iter->ts);
1784
1785         switch (entry->type) {
1786         case TRACE_FN: {
1787                 struct ftrace_entry *field;
1788
1789                 trace_assign_type(field, entry);
1790
1791                 SEQ_PUT_FIELD_RET(s, field->ip);
1792                 SEQ_PUT_FIELD_RET(s, field->parent_ip);
1793                 break;
1794         }
1795         case TRACE_CTX: {
1796                 struct ctx_switch_entry *field;
1797
1798                 trace_assign_type(field, entry);
1799
1800                 SEQ_PUT_FIELD_RET(s, field->prev_pid);
1801                 SEQ_PUT_FIELD_RET(s, field->prev_prio);
1802                 SEQ_PUT_FIELD_RET(s, field->prev_state);
1803                 SEQ_PUT_FIELD_RET(s, field->next_pid);
1804                 SEQ_PUT_FIELD_RET(s, field->next_prio);
1805                 SEQ_PUT_FIELD_RET(s, field->next_state);
1806                 break;
1807         }
1808         case TRACE_SPECIAL:
1809         case TRACE_STACK: {
1810                 struct special_entry *field;
1811
1812                 trace_assign_type(field, entry);
1813
1814                 SEQ_PUT_FIELD_RET(s, field->arg1);
1815                 SEQ_PUT_FIELD_RET(s, field->arg2);
1816                 SEQ_PUT_FIELD_RET(s, field->arg3);
1817                 break;
1818         }
1819         }
1820         return 1;
1821 }
1822
1823 static int trace_empty(struct trace_iterator *iter)
1824 {
1825         int cpu;
1826
1827         for_each_tracing_cpu(cpu) {
1828                 if (iter->buffer_iter[cpu]) {
1829                         if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1830                                 return 0;
1831                 } else {
1832                         if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1833                                 return 0;
1834                 }
1835         }
1836
1837         return 1;
1838 }
1839
1840 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1841 {
1842         enum print_line_t ret;
1843
1844         if (iter->trace && iter->trace->print_line) {
1845                 ret = iter->trace->print_line(iter);
1846                 if (ret != TRACE_TYPE_UNHANDLED)
1847                         return ret;
1848         }
1849
1850         if (trace_flags & TRACE_ITER_BIN)
1851                 return print_bin_fmt(iter);
1852
1853         if (trace_flags & TRACE_ITER_HEX)
1854                 return print_hex_fmt(iter);
1855
1856         if (trace_flags & TRACE_ITER_RAW)
1857                 return print_raw_fmt(iter);
1858
1859         if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1860                 return print_lat_fmt(iter, iter->idx, iter->cpu);
1861
1862         return print_trace_fmt(iter);
1863 }
1864
1865 static int s_show(struct seq_file *m, void *v)
1866 {
1867         struct trace_iterator *iter = v;
1868
1869         if (iter->ent == NULL) {
1870                 if (iter->tr) {
1871                         seq_printf(m, "# tracer: %s\n", iter->trace->name);
1872                         seq_puts(m, "#\n");
1873                 }
1874                 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1875                         /* print nothing if the buffers are empty */
1876                         if (trace_empty(iter))
1877                                 return 0;
1878                         print_trace_header(m, iter);
1879                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1880                                 print_lat_help_header(m);
1881                 } else {
1882                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1883                                 print_func_help_header(m);
1884                 }
1885         } else {
1886                 print_trace_line(iter);
1887                 trace_print_seq(m, &iter->seq);
1888         }
1889
1890         return 0;
1891 }
1892
1893 static struct seq_operations tracer_seq_ops = {
1894         .start          = s_start,
1895         .next           = s_next,
1896         .stop           = s_stop,
1897         .show           = s_show,
1898 };
1899
1900 static struct trace_iterator *
1901 __tracing_open(struct inode *inode, struct file *file, int *ret)
1902 {
1903         struct trace_iterator *iter;
1904         struct seq_file *m;
1905         int cpu;
1906
1907         if (tracing_disabled) {
1908                 *ret = -ENODEV;
1909                 return NULL;
1910         }
1911
1912         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1913         if (!iter) {
1914                 *ret = -ENOMEM;
1915                 goto out;
1916         }
1917
1918         mutex_lock(&trace_types_lock);
1919         if (current_trace && current_trace->print_max)
1920                 iter->tr = &max_tr;
1921         else
1922                 iter->tr = inode->i_private;
1923         iter->trace = current_trace;
1924         iter->pos = -1;
1925
1926         for_each_tracing_cpu(cpu) {
1927
1928                 iter->buffer_iter[cpu] =
1929                         ring_buffer_read_start(iter->tr->buffer, cpu);
1930
1931                 if (!iter->buffer_iter[cpu])
1932                         goto fail_buffer;
1933         }
1934
1935         /* TODO stop tracer */
1936         *ret = seq_open(file, &tracer_seq_ops);
1937         if (*ret)
1938                 goto fail_buffer;
1939
1940         m = file->private_data;
1941         m->private = iter;
1942
1943         /* stop the trace while dumping */
1944         if (iter->tr->ctrl) {
1945                 tracer_enabled = 0;
1946                 ftrace_function_enabled = 0;
1947         }
1948
1949         if (iter->trace && iter->trace->open)
1950                         iter->trace->open(iter);
1951
1952         mutex_unlock(&trace_types_lock);
1953
1954  out:
1955         return iter;
1956
1957  fail_buffer:
1958         for_each_tracing_cpu(cpu) {
1959                 if (iter->buffer_iter[cpu])
1960                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
1961         }
1962         mutex_unlock(&trace_types_lock);
1963
1964         return ERR_PTR(-ENOMEM);
1965 }
1966
1967 int tracing_open_generic(struct inode *inode, struct file *filp)
1968 {
1969         if (tracing_disabled)
1970                 return -ENODEV;
1971
1972         filp->private_data = inode->i_private;
1973         return 0;
1974 }
1975
1976 int tracing_release(struct inode *inode, struct file *file)
1977 {
1978         struct seq_file *m = (struct seq_file *)file->private_data;
1979         struct trace_iterator *iter = m->private;
1980         int cpu;
1981
1982         mutex_lock(&trace_types_lock);
1983         for_each_tracing_cpu(cpu) {
1984                 if (iter->buffer_iter[cpu])
1985                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
1986         }
1987
1988         if (iter->trace && iter->trace->close)
1989                 iter->trace->close(iter);
1990
1991         /* reenable tracing if it was previously enabled */
1992         if (iter->tr->ctrl) {
1993                 tracer_enabled = 1;
1994                 /*
1995                  * It is safe to enable function tracing even if it
1996                  * isn't used
1997                  */
1998                 ftrace_function_enabled = 1;
1999         }
2000         mutex_unlock(&trace_types_lock);
2001
2002         seq_release(inode, file);
2003         kfree(iter);
2004         return 0;
2005 }
2006
2007 static int tracing_open(struct inode *inode, struct file *file)
2008 {
2009         int ret;
2010
2011         __tracing_open(inode, file, &ret);
2012
2013         return ret;
2014 }
2015
2016 static int tracing_lt_open(struct inode *inode, struct file *file)
2017 {
2018         struct trace_iterator *iter;
2019         int ret;
2020
2021         iter = __tracing_open(inode, file, &ret);
2022
2023         if (!ret)
2024                 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2025
2026         return ret;
2027 }
2028
2029
2030 static void *
2031 t_next(struct seq_file *m, void *v, loff_t *pos)
2032 {
2033         struct tracer *t = m->private;
2034
2035         (*pos)++;
2036
2037         if (t)
2038                 t = t->next;
2039
2040         m->private = t;
2041
2042         return t;
2043 }
2044
2045 static void *t_start(struct seq_file *m, loff_t *pos)
2046 {
2047         struct tracer *t = m->private;
2048         loff_t l = 0;
2049
2050         mutex_lock(&trace_types_lock);
2051         for (; t && l < *pos; t = t_next(m, t, &l))
2052                 ;
2053
2054         return t;
2055 }
2056
2057 static void t_stop(struct seq_file *m, void *p)
2058 {
2059         mutex_unlock(&trace_types_lock);
2060 }
2061
2062 static int t_show(struct seq_file *m, void *v)
2063 {
2064         struct tracer *t = v;
2065
2066         if (!t)
2067                 return 0;
2068
2069         seq_printf(m, "%s", t->name);
2070         if (t->next)
2071                 seq_putc(m, ' ');
2072         else
2073                 seq_putc(m, '\n');
2074
2075         return 0;
2076 }
2077
2078 static struct seq_operations show_traces_seq_ops = {
2079         .start          = t_start,
2080         .next           = t_next,
2081         .stop           = t_stop,
2082         .show           = t_show,
2083 };
2084
2085 static int show_traces_open(struct inode *inode, struct file *file)
2086 {
2087         int ret;
2088
2089         if (tracing_disabled)
2090                 return -ENODEV;
2091
2092         ret = seq_open(file, &show_traces_seq_ops);
2093         if (!ret) {
2094                 struct seq_file *m = file->private_data;
2095                 m->private = trace_types;
2096         }
2097
2098         return ret;
2099 }
2100
2101 static struct file_operations tracing_fops = {
2102         .open           = tracing_open,
2103         .read           = seq_read,
2104         .llseek         = seq_lseek,
2105         .release        = tracing_release,
2106 };
2107
2108 static struct file_operations tracing_lt_fops = {
2109         .open           = tracing_lt_open,
2110         .read           = seq_read,
2111         .llseek         = seq_lseek,
2112         .release        = tracing_release,
2113 };
2114
2115 static struct file_operations show_traces_fops = {
2116         .open           = show_traces_open,
2117         .read           = seq_read,
2118         .release        = seq_release,
2119 };
2120
2121 /*
2122  * Only trace on a CPU if the bitmask is set:
2123  */
2124 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2125
2126 /*
2127  * When tracing/tracing_cpu_mask is modified then this holds
2128  * the new bitmask we are about to install:
2129  */
2130 static cpumask_t tracing_cpumask_new;
2131
2132 /*
2133  * The tracer itself will not take this lock, but still we want
2134  * to provide a consistent cpumask to user-space:
2135  */
2136 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2137
2138 /*
2139  * Temporary storage for the character representation of the
2140  * CPU bitmask (and one more byte for the newline):
2141  */
2142 static char mask_str[NR_CPUS + 1];
2143
2144 static ssize_t
2145 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2146                      size_t count, loff_t *ppos)
2147 {
2148         int len;
2149
2150         mutex_lock(&tracing_cpumask_update_lock);
2151
2152         len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2153         if (count - len < 2) {
2154                 count = -EINVAL;
2155                 goto out_err;
2156         }
2157         len += sprintf(mask_str + len, "\n");
2158         count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2159
2160 out_err:
2161         mutex_unlock(&tracing_cpumask_update_lock);
2162
2163         return count;
2164 }
2165
2166 static ssize_t
2167 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2168                       size_t count, loff_t *ppos)
2169 {
2170         int err, cpu;
2171
2172         mutex_lock(&tracing_cpumask_update_lock);
2173         err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2174         if (err)
2175                 goto err_unlock;
2176
2177         raw_local_irq_disable();
2178         __raw_spin_lock(&ftrace_max_lock);
2179         for_each_tracing_cpu(cpu) {
2180                 /*
2181                  * Increase/decrease the disabled counter if we are
2182                  * about to flip a bit in the cpumask:
2183                  */
2184                 if (cpu_isset(cpu, tracing_cpumask) &&
2185                                 !cpu_isset(cpu, tracing_cpumask_new)) {
2186                         atomic_inc(&global_trace.data[cpu]->disabled);
2187                 }
2188                 if (!cpu_isset(cpu, tracing_cpumask) &&
2189                                 cpu_isset(cpu, tracing_cpumask_new)) {
2190                         atomic_dec(&global_trace.data[cpu]->disabled);
2191                 }
2192         }
2193         __raw_spin_unlock(&ftrace_max_lock);
2194         raw_local_irq_enable();
2195
2196         tracing_cpumask = tracing_cpumask_new;
2197
2198         mutex_unlock(&tracing_cpumask_update_lock);
2199
2200         return count;
2201
2202 err_unlock:
2203         mutex_unlock(&tracing_cpumask_update_lock);
2204
2205         return err;
2206 }
2207
2208 static struct file_operations tracing_cpumask_fops = {
2209         .open           = tracing_open_generic,
2210         .read           = tracing_cpumask_read,
2211         .write          = tracing_cpumask_write,
2212 };
2213
2214 static ssize_t
2215 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2216                        size_t cnt, loff_t *ppos)
2217 {
2218         char *buf;
2219         int r = 0;
2220         int len = 0;
2221         int i;
2222
2223         /* calulate max size */
2224         for (i = 0; trace_options[i]; i++) {
2225                 len += strlen(trace_options[i]);
2226                 len += 3; /* "no" and space */
2227         }
2228
2229         /* +2 for \n and \0 */
2230         buf = kmalloc(len + 2, GFP_KERNEL);
2231         if (!buf)
2232                 return -ENOMEM;
2233
2234         for (i = 0; trace_options[i]; i++) {
2235                 if (trace_flags & (1 << i))
2236                         r += sprintf(buf + r, "%s ", trace_options[i]);
2237                 else
2238                         r += sprintf(buf + r, "no%s ", trace_options[i]);
2239         }
2240
2241         r += sprintf(buf + r, "\n");
2242         WARN_ON(r >= len + 2);
2243
2244         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2245
2246         kfree(buf);
2247
2248         return r;
2249 }
2250
2251 static ssize_t
2252 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2253                         size_t cnt, loff_t *ppos)
2254 {
2255         char buf[64];
2256         char *cmp = buf;
2257         int neg = 0;
2258         int i;
2259
2260         if (cnt >= sizeof(buf))
2261                 return -EINVAL;
2262
2263         if (copy_from_user(&buf, ubuf, cnt))
2264                 return -EFAULT;
2265
2266         buf[cnt] = 0;
2267
2268         if (strncmp(buf, "no", 2) == 0) {
2269                 neg = 1;
2270                 cmp += 2;
2271         }
2272
2273         for (i = 0; trace_options[i]; i++) {
2274                 int len = strlen(trace_options[i]);
2275
2276                 if (strncmp(cmp, trace_options[i], len) == 0) {
2277                         if (neg)
2278                                 trace_flags &= ~(1 << i);
2279                         else
2280                                 trace_flags |= (1 << i);
2281                         break;
2282                 }
2283         }
2284         /*
2285          * If no option could be set, return an error:
2286          */
2287         if (!trace_options[i])
2288                 return -EINVAL;
2289
2290         filp->f_pos += cnt;
2291
2292         return cnt;
2293 }
2294
2295 static struct file_operations tracing_iter_fops = {
2296         .open           = tracing_open_generic,
2297         .read           = tracing_iter_ctrl_read,
2298         .write          = tracing_iter_ctrl_write,
2299 };
2300
2301 static const char readme_msg[] =
2302         "tracing mini-HOWTO:\n\n"
2303         "# mkdir /debug\n"
2304         "# mount -t debugfs nodev /debug\n\n"
2305         "# cat /debug/tracing/available_tracers\n"
2306         "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2307         "# cat /debug/tracing/current_tracer\n"
2308         "none\n"
2309         "# echo sched_switch > /debug/tracing/current_tracer\n"
2310         "# cat /debug/tracing/current_tracer\n"
2311         "sched_switch\n"
2312         "# cat /debug/tracing/iter_ctrl\n"
2313         "noprint-parent nosym-offset nosym-addr noverbose\n"
2314         "# echo print-parent > /debug/tracing/iter_ctrl\n"
2315         "# echo 1 > /debug/tracing/tracing_enabled\n"
2316         "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2317         "echo 0 > /debug/tracing/tracing_enabled\n"
2318 ;
2319
2320 static ssize_t
2321 tracing_readme_read(struct file *filp, char __user *ubuf,
2322                        size_t cnt, loff_t *ppos)
2323 {
2324         return simple_read_from_buffer(ubuf, cnt, ppos,
2325                                         readme_msg, strlen(readme_msg));
2326 }
2327
2328 static struct file_operations tracing_readme_fops = {
2329         .open           = tracing_open_generic,
2330         .read           = tracing_readme_read,
2331 };
2332
2333 static ssize_t
2334 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2335                   size_t cnt, loff_t *ppos)
2336 {
2337         struct trace_array *tr = filp->private_data;
2338         char buf[64];
2339         int r;
2340
2341         r = sprintf(buf, "%ld\n", tr->ctrl);
2342         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2343 }
2344
2345 static ssize_t
2346 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2347                    size_t cnt, loff_t *ppos)
2348 {
2349         struct trace_array *tr = filp->private_data;
2350         char buf[64];
2351         long val;
2352         int ret;
2353
2354         if (cnt >= sizeof(buf))
2355                 return -EINVAL;
2356
2357         if (copy_from_user(&buf, ubuf, cnt))
2358                 return -EFAULT;
2359
2360         buf[cnt] = 0;
2361
2362         ret = strict_strtoul(buf, 10, &val);
2363         if (ret < 0)
2364                 return ret;
2365
2366         val = !!val;
2367
2368         mutex_lock(&trace_types_lock);
2369         if (tr->ctrl ^ val) {
2370                 if (val)
2371                         tracer_enabled = 1;
2372                 else
2373                         tracer_enabled = 0;
2374
2375                 tr->ctrl = val;
2376
2377                 if (current_trace && current_trace->ctrl_update)
2378                         current_trace->ctrl_update(tr);
2379         }
2380         mutex_unlock(&trace_types_lock);
2381
2382         filp->f_pos += cnt;
2383
2384         return cnt;
2385 }
2386
2387 static ssize_t
2388 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2389                        size_t cnt, loff_t *ppos)
2390 {
2391         char buf[max_tracer_type_len+2];
2392         int r;
2393
2394         mutex_lock(&trace_types_lock);
2395         if (current_trace)
2396                 r = sprintf(buf, "%s\n", current_trace->name);
2397         else
2398                 r = sprintf(buf, "\n");
2399         mutex_unlock(&trace_types_lock);
2400
2401         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2402 }
2403
2404 static int tracing_set_tracer(char *buf)
2405 {
2406         struct trace_array *tr = &global_trace;
2407         struct tracer *t;
2408         int ret = 0;
2409
2410         mutex_lock(&trace_types_lock);
2411         for (t = trace_types; t; t = t->next) {
2412                 if (strcmp(t->name, buf) == 0)
2413                         break;
2414         }
2415         if (!t) {
2416                 ret = -EINVAL;
2417                 goto out;
2418         }
2419         if (t == current_trace)
2420                 goto out;
2421
2422         if (current_trace && current_trace->reset)
2423                 current_trace->reset(tr);
2424
2425         current_trace = t;
2426         if (t->init)
2427                 t->init(tr);
2428
2429  out:
2430         mutex_unlock(&trace_types_lock);
2431
2432         return ret;
2433 }
2434
2435 static ssize_t
2436 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2437                         size_t cnt, loff_t *ppos)
2438 {
2439         char buf[max_tracer_type_len+1];
2440         int i;
2441         size_t ret;
2442
2443         if (cnt > max_tracer_type_len)
2444                 cnt = max_tracer_type_len;
2445
2446         if (copy_from_user(&buf, ubuf, cnt))
2447                 return -EFAULT;
2448
2449         buf[cnt] = 0;
2450
2451         /* strip ending whitespace. */
2452         for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2453                 buf[i] = 0;
2454
2455         ret = tracing_set_tracer(buf);
2456         if (!ret)
2457                 ret = cnt;
2458
2459         if (ret > 0)
2460                 filp->f_pos += ret;
2461
2462         return ret;
2463 }
2464
2465 static ssize_t
2466 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2467                      size_t cnt, loff_t *ppos)
2468 {
2469         unsigned long *ptr = filp->private_data;
2470         char buf[64];
2471         int r;
2472
2473         r = snprintf(buf, sizeof(buf), "%ld\n",
2474                      *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2475         if (r > sizeof(buf))
2476                 r = sizeof(buf);
2477         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2478 }
2479
2480 static ssize_t
2481 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2482                       size_t cnt, loff_t *ppos)
2483 {
2484         long *ptr = filp->private_data;
2485         char buf[64];
2486         long val;
2487         int ret;
2488
2489         if (cnt >= sizeof(buf))
2490                 return -EINVAL;
2491
2492         if (copy_from_user(&buf, ubuf, cnt))
2493                 return -EFAULT;
2494
2495         buf[cnt] = 0;
2496
2497         ret = strict_strtoul(buf, 10, &val);
2498         if (ret < 0)
2499                 return ret;
2500
2501         *ptr = val * 1000;
2502
2503         return cnt;
2504 }
2505
2506 static atomic_t tracing_reader;
2507
2508 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2509 {
2510         struct trace_iterator *iter;
2511
2512         if (tracing_disabled)
2513                 return -ENODEV;
2514
2515         /* We only allow for reader of the pipe */
2516         if (atomic_inc_return(&tracing_reader) != 1) {
2517                 atomic_dec(&tracing_reader);
2518                 return -EBUSY;
2519         }
2520
2521         /* create a buffer to store the information to pass to userspace */
2522         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2523         if (!iter)
2524                 return -ENOMEM;
2525
2526         mutex_lock(&trace_types_lock);
2527         iter->tr = &global_trace;
2528         iter->trace = current_trace;
2529         filp->private_data = iter;
2530
2531         if (iter->trace->pipe_open)
2532                 iter->trace->pipe_open(iter);
2533         mutex_unlock(&trace_types_lock);
2534
2535         return 0;
2536 }
2537
2538 static int tracing_release_pipe(struct inode *inode, struct file *file)
2539 {
2540         struct trace_iterator *iter = file->private_data;
2541
2542         kfree(iter);
2543         atomic_dec(&tracing_reader);
2544
2545         return 0;
2546 }
2547
2548 static unsigned int
2549 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2550 {
2551         struct trace_iterator *iter = filp->private_data;
2552
2553         if (trace_flags & TRACE_ITER_BLOCK) {
2554                 /*
2555                  * Always select as readable when in blocking mode
2556                  */
2557                 return POLLIN | POLLRDNORM;
2558         } else {
2559                 if (!trace_empty(iter))
2560                         return POLLIN | POLLRDNORM;
2561                 poll_wait(filp, &trace_wait, poll_table);
2562                 if (!trace_empty(iter))
2563                         return POLLIN | POLLRDNORM;
2564
2565                 return 0;
2566         }
2567 }
2568
2569 /*
2570  * Consumer reader.
2571  */
2572 static ssize_t
2573 tracing_read_pipe(struct file *filp, char __user *ubuf,
2574                   size_t cnt, loff_t *ppos)
2575 {
2576         struct trace_iterator *iter = filp->private_data;
2577         ssize_t sret;
2578
2579         /* return any leftover data */
2580         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2581         if (sret != -EBUSY)
2582                 return sret;
2583
2584         trace_seq_reset(&iter->seq);
2585
2586         mutex_lock(&trace_types_lock);
2587         if (iter->trace->read) {
2588                 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2589                 if (sret)
2590                         goto out;
2591         }
2592
2593 waitagain:
2594         sret = 0;
2595         while (trace_empty(iter)) {
2596
2597                 if ((filp->f_flags & O_NONBLOCK)) {
2598                         sret = -EAGAIN;
2599                         goto out;
2600                 }
2601
2602                 /*
2603                  * This is a make-shift waitqueue. The reason we don't use
2604                  * an actual wait queue is because:
2605                  *  1) we only ever have one waiter
2606                  *  2) the tracing, traces all functions, we don't want
2607                  *     the overhead of calling wake_up and friends
2608                  *     (and tracing them too)
2609                  *     Anyway, this is really very primitive wakeup.
2610                  */
2611                 set_current_state(TASK_INTERRUPTIBLE);
2612                 iter->tr->waiter = current;
2613
2614                 mutex_unlock(&trace_types_lock);
2615
2616                 /* sleep for 100 msecs, and try again. */
2617                 schedule_timeout(HZ/10);
2618
2619                 mutex_lock(&trace_types_lock);
2620
2621                 iter->tr->waiter = NULL;
2622
2623                 if (signal_pending(current)) {
2624                         sret = -EINTR;
2625                         goto out;
2626                 }
2627
2628                 if (iter->trace != current_trace)
2629                         goto out;
2630
2631                 /*
2632                  * We block until we read something and tracing is disabled.
2633                  * We still block if tracing is disabled, but we have never
2634                  * read anything. This allows a user to cat this file, and
2635                  * then enable tracing. But after we have read something,
2636                  * we give an EOF when tracing is again disabled.
2637                  *
2638                  * iter->pos will be 0 if we haven't read anything.
2639                  */
2640                 if (!tracer_enabled && iter->pos)
2641                         break;
2642
2643                 continue;
2644         }
2645
2646         /* stop when tracing is finished */
2647         if (trace_empty(iter))
2648                 goto out;
2649
2650         if (cnt >= PAGE_SIZE)
2651                 cnt = PAGE_SIZE - 1;
2652
2653         /* reset all but tr, trace, and overruns */
2654         memset(&iter->seq, 0,
2655                sizeof(struct trace_iterator) -
2656                offsetof(struct trace_iterator, seq));
2657         iter->pos = -1;
2658
2659         while (find_next_entry_inc(iter) != NULL) {
2660                 enum print_line_t ret;
2661                 int len = iter->seq.len;
2662
2663                 ret = print_trace_line(iter);
2664                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2665                         /* don't print partial lines */
2666                         iter->seq.len = len;
2667                         break;
2668                 }
2669
2670                 trace_consume(iter);
2671
2672                 if (iter->seq.len >= cnt)
2673                         break;
2674         }
2675
2676         /* Now copy what we have to the user */
2677         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2678         if (iter->seq.readpos >= iter->seq.len)
2679                 trace_seq_reset(&iter->seq);
2680
2681         /*
2682          * If there was nothing to send to user, inspite of consuming trace
2683          * entries, go back to wait for more entries.
2684          */
2685         if (sret == -EBUSY)
2686                 goto waitagain;
2687
2688 out:
2689         mutex_unlock(&trace_types_lock);
2690
2691         return sret;
2692 }
2693
2694 static ssize_t
2695 tracing_entries_read(struct file *filp, char __user *ubuf,
2696                      size_t cnt, loff_t *ppos)
2697 {
2698         struct trace_array *tr = filp->private_data;
2699         char buf[64];
2700         int r;
2701
2702         r = sprintf(buf, "%lu\n", tr->entries);
2703         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2704 }
2705
2706 static ssize_t
2707 tracing_entries_write(struct file *filp, const char __user *ubuf,
2708                       size_t cnt, loff_t *ppos)
2709 {
2710         unsigned long val;
2711         char buf[64];
2712         int ret;
2713         struct trace_array *tr = filp->private_data;
2714
2715         if (cnt >= sizeof(buf))
2716                 return -EINVAL;
2717
2718         if (copy_from_user(&buf, ubuf, cnt))
2719                 return -EFAULT;
2720
2721         buf[cnt] = 0;
2722
2723         ret = strict_strtoul(buf, 10, &val);
2724         if (ret < 0)
2725                 return ret;
2726
2727         /* must have at least 1 entry */
2728         if (!val)
2729                 return -EINVAL;
2730
2731         mutex_lock(&trace_types_lock);
2732
2733         if (tr->ctrl) {
2734                 cnt = -EBUSY;
2735                 pr_info("ftrace: please disable tracing"
2736                         " before modifying buffer size\n");
2737                 goto out;
2738         }
2739
2740         if (val != global_trace.entries) {
2741                 ret = ring_buffer_resize(global_trace.buffer, val);
2742                 if (ret < 0) {
2743                         cnt = ret;
2744                         goto out;
2745                 }
2746
2747                 ret = ring_buffer_resize(max_tr.buffer, val);
2748                 if (ret < 0) {
2749                         int r;
2750                         cnt = ret;
2751                         r = ring_buffer_resize(global_trace.buffer,
2752                                                global_trace.entries);
2753                         if (r < 0) {
2754                                 /* AARGH! We are left with different
2755                                  * size max buffer!!!! */
2756                                 WARN_ON(1);
2757                                 tracing_disabled = 1;
2758                         }
2759                         goto out;
2760                 }
2761
2762                 global_trace.entries = val;
2763         }
2764
2765         filp->f_pos += cnt;
2766
2767         /* If check pages failed, return ENOMEM */
2768         if (tracing_disabled)
2769                 cnt = -ENOMEM;
2770  out:
2771         max_tr.entries = global_trace.entries;
2772         mutex_unlock(&trace_types_lock);
2773
2774         return cnt;
2775 }
2776
2777 static int mark_printk(const char *fmt, ...)
2778 {
2779         int ret;
2780         va_list args;
2781         va_start(args, fmt);
2782         ret = trace_vprintk(0, fmt, args);
2783         va_end(args);
2784         return ret;
2785 }
2786
2787 static ssize_t
2788 tracing_mark_write(struct file *filp, const char __user *ubuf,
2789                                         size_t cnt, loff_t *fpos)
2790 {
2791         char *buf;
2792         char *end;
2793         struct trace_array *tr = &global_trace;
2794
2795         if (!tr->ctrl || tracing_disabled)
2796                 return -EINVAL;
2797
2798         if (cnt > TRACE_BUF_SIZE)
2799                 cnt = TRACE_BUF_SIZE;
2800
2801         buf = kmalloc(cnt + 1, GFP_KERNEL);
2802         if (buf == NULL)
2803                 return -ENOMEM;
2804
2805         if (copy_from_user(buf, ubuf, cnt)) {
2806                 kfree(buf);
2807                 return -EFAULT;
2808         }
2809
2810         /* Cut from the first nil or newline. */
2811         buf[cnt] = '\0';
2812         end = strchr(buf, '\n');
2813         if (end)
2814                 *end = '\0';
2815
2816         cnt = mark_printk("%s\n", buf);
2817         kfree(buf);
2818         *fpos += cnt;
2819
2820         return cnt;
2821 }
2822
2823 static struct file_operations tracing_max_lat_fops = {
2824         .open           = tracing_open_generic,
2825         .read           = tracing_max_lat_read,
2826         .write          = tracing_max_lat_write,
2827 };
2828
2829 static struct file_operations tracing_ctrl_fops = {
2830         .open           = tracing_open_generic,
2831         .read           = tracing_ctrl_read,
2832         .write          = tracing_ctrl_write,
2833 };
2834
2835 static struct file_operations set_tracer_fops = {
2836         .open           = tracing_open_generic,
2837         .read           = tracing_set_trace_read,
2838         .write          = tracing_set_trace_write,
2839 };
2840
2841 static struct file_operations tracing_pipe_fops = {
2842         .open           = tracing_open_pipe,
2843         .poll           = tracing_poll_pipe,
2844         .read           = tracing_read_pipe,
2845         .release        = tracing_release_pipe,
2846 };
2847
2848 static struct file_operations tracing_entries_fops = {
2849         .open           = tracing_open_generic,
2850         .read           = tracing_entries_read,
2851         .write          = tracing_entries_write,
2852 };
2853
2854 static struct file_operations tracing_mark_fops = {
2855         .open           = tracing_open_generic,
2856         .write          = tracing_mark_write,
2857 };
2858
2859 #ifdef CONFIG_DYNAMIC_FTRACE
2860
2861 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
2862 {
2863         return 0;
2864 }
2865
2866 static ssize_t
2867 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
2868                   size_t cnt, loff_t *ppos)
2869 {
2870         static char ftrace_dyn_info_buffer[1024];
2871         static DEFINE_MUTEX(dyn_info_mutex);
2872         unsigned long *p = filp->private_data;
2873         char *buf = ftrace_dyn_info_buffer;
2874         int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
2875         int r;
2876
2877         mutex_lock(&dyn_info_mutex);
2878         r = sprintf(buf, "%ld ", *p);
2879
2880         r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
2881         buf[r++] = '\n';
2882
2883         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2884
2885         mutex_unlock(&dyn_info_mutex);
2886
2887         return r;
2888 }
2889
2890 static struct file_operations tracing_dyn_info_fops = {
2891         .open           = tracing_open_generic,
2892         .read           = tracing_read_dyn_info,
2893 };
2894 #endif
2895
2896 static struct dentry *d_tracer;
2897
2898 struct dentry *tracing_init_dentry(void)
2899 {
2900         static int once;
2901
2902         if (d_tracer)
2903                 return d_tracer;
2904
2905         d_tracer = debugfs_create_dir("tracing", NULL);
2906
2907         if (!d_tracer && !once) {
2908                 once = 1;
2909                 pr_warning("Could not create debugfs directory 'tracing'\n");
2910                 return NULL;
2911         }
2912
2913         return d_tracer;
2914 }
2915
2916 #ifdef CONFIG_FTRACE_SELFTEST
2917 /* Let selftest have access to static functions in this file */
2918 #include "trace_selftest.c"
2919 #endif
2920
2921 static __init int tracer_init_debugfs(void)
2922 {
2923         struct dentry *d_tracer;
2924         struct dentry *entry;
2925
2926         d_tracer = tracing_init_dentry();
2927
2928         entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2929                                     &global_trace, &tracing_ctrl_fops);
2930         if (!entry)
2931                 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2932
2933         entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2934                                     NULL, &tracing_iter_fops);
2935         if (!entry)
2936                 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2937
2938         entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2939                                     NULL, &tracing_cpumask_fops);
2940         if (!entry)
2941                 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2942
2943         entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2944                                     &global_trace, &tracing_lt_fops);
2945         if (!entry)
2946                 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2947
2948         entry = debugfs_create_file("trace", 0444, d_tracer,
2949                                     &global_trace, &tracing_fops);
2950         if (!entry)
2951                 pr_warning("Could not create debugfs 'trace' entry\n");
2952
2953         entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2954                                     &global_trace, &show_traces_fops);
2955         if (!entry)
2956                 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2957
2958         entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2959                                     &global_trace, &set_tracer_fops);
2960         if (!entry)
2961                 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2962
2963         entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2964                                     &tracing_max_latency,
2965                                     &tracing_max_lat_fops);
2966         if (!entry)
2967                 pr_warning("Could not create debugfs "
2968                            "'tracing_max_latency' entry\n");
2969
2970         entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2971                                     &tracing_thresh, &tracing_max_lat_fops);
2972         if (!entry)
2973                 pr_warning("Could not create debugfs "
2974                            "'tracing_thresh' entry\n");
2975         entry = debugfs_create_file("README", 0644, d_tracer,
2976                                     NULL, &tracing_readme_fops);
2977         if (!entry)
2978                 pr_warning("Could not create debugfs 'README' entry\n");
2979
2980         entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2981                                     NULL, &tracing_pipe_fops);
2982         if (!entry)
2983                 pr_warning("Could not create debugfs "
2984                            "'trace_pipe' entry\n");
2985
2986         entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2987                                     &global_trace, &tracing_entries_fops);
2988         if (!entry)
2989                 pr_warning("Could not create debugfs "
2990                            "'trace_entries' entry\n");
2991
2992         entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2993                                     NULL, &tracing_mark_fops);
2994         if (!entry)
2995                 pr_warning("Could not create debugfs "
2996                            "'trace_marker' entry\n");
2997
2998 #ifdef CONFIG_DYNAMIC_FTRACE
2999         entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3000                                     &ftrace_update_tot_cnt,
3001                                     &tracing_dyn_info_fops);
3002         if (!entry)
3003                 pr_warning("Could not create debugfs "
3004                            "'dyn_ftrace_total_info' entry\n");
3005 #endif
3006 #ifdef CONFIG_SYSPROF_TRACER
3007         init_tracer_sysprof_debugfs(d_tracer);
3008 #endif
3009         return 0;
3010 }
3011
3012 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3013 {
3014         static DEFINE_SPINLOCK(trace_buf_lock);
3015         static char trace_buf[TRACE_BUF_SIZE];
3016
3017         struct ring_buffer_event *event;
3018         struct trace_array *tr = &global_trace;
3019         struct trace_array_cpu *data;
3020         struct print_entry *entry;
3021         unsigned long flags, irq_flags;
3022         int cpu, len = 0, size, pc;
3023
3024         if (!tr->ctrl || tracing_disabled)
3025                 return 0;
3026
3027         pc = preempt_count();
3028         preempt_disable_notrace();
3029         cpu = raw_smp_processor_id();
3030         data = tr->data[cpu];
3031
3032         if (unlikely(atomic_read(&data->disabled)))
3033                 goto out;
3034
3035         spin_lock_irqsave(&trace_buf_lock, flags);
3036         len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3037
3038         len = min(len, TRACE_BUF_SIZE-1);
3039         trace_buf[len] = 0;
3040
3041         size = sizeof(*entry) + len + 1;
3042         event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3043         if (!event)
3044                 goto out_unlock;
3045         entry = ring_buffer_event_data(event);
3046         tracing_generic_entry_update(&entry->ent, flags, pc);
3047         entry->ent.type                 = TRACE_PRINT;
3048         entry->ip                       = ip;
3049
3050         memcpy(&entry->buf, trace_buf, len);
3051         entry->buf[len] = 0;
3052         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3053
3054  out_unlock:
3055         spin_unlock_irqrestore(&trace_buf_lock, flags);
3056
3057  out:
3058         preempt_enable_notrace();
3059
3060         return len;
3061 }
3062 EXPORT_SYMBOL_GPL(trace_vprintk);
3063
3064 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3065 {
3066         int ret;
3067         va_list ap;
3068
3069         if (!(trace_flags & TRACE_ITER_PRINTK))
3070                 return 0;
3071
3072         va_start(ap, fmt);
3073         ret = trace_vprintk(ip, fmt, ap);
3074         va_end(ap);
3075         return ret;
3076 }
3077 EXPORT_SYMBOL_GPL(__ftrace_printk);
3078
3079 static int trace_panic_handler(struct notifier_block *this,
3080                                unsigned long event, void *unused)
3081 {
3082         if (ftrace_dump_on_oops)
3083                 ftrace_dump();
3084         return NOTIFY_OK;
3085 }
3086
3087 static struct notifier_block trace_panic_notifier = {
3088         .notifier_call  = trace_panic_handler,
3089         .next           = NULL,
3090         .priority       = 150   /* priority: INT_MAX >= x >= 0 */
3091 };
3092
3093 static int trace_die_handler(struct notifier_block *self,
3094                              unsigned long val,
3095                              void *data)
3096 {
3097         switch (val) {
3098         case DIE_OOPS:
3099                 if (ftrace_dump_on_oops)
3100                         ftrace_dump();
3101                 break;
3102         default:
3103                 break;
3104         }
3105         return NOTIFY_OK;
3106 }
3107
3108 static struct notifier_block trace_die_notifier = {
3109         .notifier_call = trace_die_handler,
3110         .priority = 200
3111 };
3112
3113 /*
3114  * printk is set to max of 1024, we really don't need it that big.
3115  * Nothing should be printing 1000 characters anyway.
3116  */
3117 #define TRACE_MAX_PRINT         1000
3118
3119 /*
3120  * Define here KERN_TRACE so that we have one place to modify
3121  * it if we decide to change what log level the ftrace dump
3122  * should be at.
3123  */
3124 #define KERN_TRACE              KERN_INFO
3125
3126 static void
3127 trace_printk_seq(struct trace_seq *s)
3128 {
3129         /* Probably should print a warning here. */
3130         if (s->len >= 1000)
3131                 s->len = 1000;
3132
3133         /* should be zero ended, but we are paranoid. */
3134         s->buffer[s->len] = 0;
3135
3136         printk(KERN_TRACE "%s", s->buffer);
3137
3138         trace_seq_reset(s);
3139 }
3140
3141 void ftrace_dump(void)
3142 {
3143         static DEFINE_SPINLOCK(ftrace_dump_lock);
3144         /* use static because iter can be a bit big for the stack */
3145         static struct trace_iterator iter;
3146         static cpumask_t mask;
3147         static int dump_ran;
3148         unsigned long flags;
3149         int cnt = 0, cpu;
3150
3151         /* only one dump */
3152         spin_lock_irqsave(&ftrace_dump_lock, flags);
3153         if (dump_ran)
3154                 goto out;
3155
3156         dump_ran = 1;
3157
3158         /* No turning back! */
3159         ftrace_kill();
3160
3161         for_each_tracing_cpu(cpu) {
3162                 atomic_inc(&global_trace.data[cpu]->disabled);
3163         }
3164
3165         printk(KERN_TRACE "Dumping ftrace buffer:\n");
3166
3167         iter.tr = &global_trace;
3168         iter.trace = current_trace;
3169
3170         /*
3171          * We need to stop all tracing on all CPUS to read the
3172          * the next buffer. This is a bit expensive, but is
3173          * not done often. We fill all what we can read,
3174          * and then release the locks again.
3175          */
3176
3177         cpus_clear(mask);
3178
3179         while (!trace_empty(&iter)) {
3180
3181                 if (!cnt)
3182                         printk(KERN_TRACE "---------------------------------\n");
3183
3184                 cnt++;
3185
3186                 /* reset all but tr, trace, and overruns */
3187                 memset(&iter.seq, 0,
3188                        sizeof(struct trace_iterator) -
3189                        offsetof(struct trace_iterator, seq));
3190                 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3191                 iter.pos = -1;
3192
3193                 if (find_next_entry_inc(&iter) != NULL) {
3194                         print_trace_line(&iter);
3195                         trace_consume(&iter);
3196                 }
3197
3198                 trace_printk_seq(&iter.seq);
3199         }
3200
3201         if (!cnt)
3202                 printk(KERN_TRACE "   (ftrace buffer empty)\n");
3203         else
3204                 printk(KERN_TRACE "---------------------------------\n");
3205
3206  out:
3207         spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3208 }
3209
3210 __init static int tracer_alloc_buffers(void)
3211 {
3212         struct trace_array_cpu *data;
3213         int i;
3214
3215         /* TODO: make the number of buffers hot pluggable with CPUS */
3216         tracing_buffer_mask = cpu_possible_map;
3217
3218         global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3219                                                    TRACE_BUFFER_FLAGS);
3220         if (!global_trace.buffer) {
3221                 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3222                 WARN_ON(1);
3223                 return 0;
3224         }
3225         global_trace.entries = ring_buffer_size(global_trace.buffer);
3226
3227 #ifdef CONFIG_TRACER_MAX_TRACE
3228         max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3229                                              TRACE_BUFFER_FLAGS);
3230         if (!max_tr.buffer) {
3231                 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3232                 WARN_ON(1);
3233                 ring_buffer_free(global_trace.buffer);
3234                 return 0;
3235         }
3236         max_tr.entries = ring_buffer_size(max_tr.buffer);
3237         WARN_ON(max_tr.entries != global_trace.entries);
3238 #endif
3239
3240         /* Allocate the first page for all buffers */
3241         for_each_tracing_cpu(i) {
3242                 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3243                 max_tr.data[i] = &per_cpu(max_data, i);
3244         }
3245
3246         trace_init_cmdlines();
3247
3248         register_tracer(&nop_trace);
3249 #ifdef CONFIG_BOOT_TRACER
3250         register_tracer(&boot_tracer);
3251         current_trace = &boot_tracer;
3252         current_trace->init(&global_trace);
3253 #else
3254         current_trace = &nop_trace;
3255 #endif
3256
3257         /* All seems OK, enable tracing */
3258         global_trace.ctrl = tracer_enabled;
3259         tracing_disabled = 0;
3260
3261         atomic_notifier_chain_register(&panic_notifier_list,
3262                                        &trace_panic_notifier);
3263
3264         register_die_notifier(&trace_die_notifier);
3265
3266         return 0;
3267 }
3268 early_initcall(tracer_alloc_buffers);
3269 fs_initcall(tracer_init_debugfs);