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