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