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