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