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