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