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