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