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