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