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