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