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