2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
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
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>
32 #include <linux/kprobes.h>
33 #include <linux/writeback.h>
35 #include <linux/stacktrace.h>
36 #include <linux/ring_buffer.h>
37 #include <linux/irqflags.h>
41 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
43 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
44 unsigned long __read_mostly tracing_thresh;
46 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
48 static inline void ftrace_disable_cpu(void)
51 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
54 static inline void ftrace_enable_cpu(void)
56 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
60 static cpumask_t __read_mostly tracing_buffer_mask;
62 #define for_each_tracing_cpu(cpu) \
63 for_each_cpu_mask(cpu, tracing_buffer_mask)
65 static int tracing_disabled = 1;
68 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
70 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
71 * is set, then ftrace_dump is called. This will output the contents
72 * of the ftrace buffers to the console. This is very useful for
73 * capturing traces that lead to crashes and outputing it to a
76 * It is default off, but you can enable it with either specifying
77 * "ftrace_dump_on_oops" in the kernel command line, or setting
78 * /proc/sys/kernel/ftrace_dump_on_oops to true.
80 int ftrace_dump_on_oops;
82 static int tracing_set_tracer(char *buf);
84 static int __init set_ftrace(char *str)
86 tracing_set_tracer(str);
89 __setup("ftrace", set_ftrace);
91 static int __init set_ftrace_dump_on_oops(char *str)
93 ftrace_dump_on_oops = 1;
96 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
99 ns2usecs(cycle_t nsec)
106 cycle_t ftrace_now(int cpu)
108 u64 ts = ring_buffer_time_stamp(cpu);
109 ring_buffer_normalize_time_stamp(cpu, &ts);
114 * The global_trace is the descriptor that holds the tracing
115 * buffers for the live tracing. For each CPU, it contains
116 * a link list of pages that will store trace entries. The
117 * page descriptor of the pages in the memory is used to hold
118 * the link list by linking the lru item in the page descriptor
119 * to each of the pages in the buffer per CPU.
121 * For each active CPU there is a data field that holds the
122 * pages for the buffer for that CPU. Each CPU has the same number
123 * of pages allocated for its buffer.
125 static struct trace_array global_trace;
127 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
130 * The max_tr is used to snapshot the global_trace when a maximum
131 * latency is reached. Some tracers will use this to store a maximum
132 * trace while it continues examining live traces.
134 * The buffers for the max_tr are set up the same as the global_trace.
135 * When a snapshot is taken, the link list of the max_tr is swapped
136 * with the link list of the global_trace and the buffers are reset for
137 * the global_trace so the tracing can continue.
139 static struct trace_array max_tr;
141 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
143 /* tracer_enabled is used to toggle activation of a tracer */
144 static int tracer_enabled = 1;
146 /* function tracing enabled */
147 int ftrace_function_enabled;
150 * trace_buf_size is the size in bytes that is allocated
151 * for a buffer. Note, the number of bytes is always rounded
154 * This number is purposely set to a low number of 16384.
155 * If the dump on oops happens, it will be much appreciated
156 * to not have to wait for all that output. Anyway this can be
157 * boot time and run time configurable.
159 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
161 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
163 /* trace_types holds a link list of available tracers. */
164 static struct tracer *trace_types __read_mostly;
166 /* current_trace points to the tracer that is currently active */
167 static struct tracer *current_trace __read_mostly;
170 * max_tracer_type_len is used to simplify the allocating of
171 * buffers to read userspace tracer names. We keep track of
172 * the longest tracer name registered.
174 static int max_tracer_type_len;
177 * trace_types_lock is used to protect the trace_types list.
178 * This lock is also used to keep user access serialized.
179 * Accesses from userspace will grab this lock while userspace
180 * activities happen inside the kernel.
182 static DEFINE_MUTEX(trace_types_lock);
184 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
185 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
187 /* trace_flags holds iter_ctrl options */
188 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
191 * trace_wake_up - wake up tasks waiting for trace input
193 * Simply wakes up any task that is blocked on the trace_wait
194 * queue. These is used with trace_poll for tasks polling the trace.
196 void trace_wake_up(void)
199 * The runqueue_is_locked() can fail, but this is the best we
202 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
203 wake_up(&trace_wait);
206 static int __init set_buf_size(char *str)
208 unsigned long buf_size;
213 ret = strict_strtoul(str, 0, &buf_size);
214 /* nr_entries can not be zero */
215 if (ret < 0 || buf_size == 0)
217 trace_buf_size = buf_size;
220 __setup("trace_buf_size=", set_buf_size);
222 unsigned long nsecs_to_usecs(unsigned long nsecs)
228 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
229 * control the output of kernel symbols.
231 #define TRACE_ITER_SYM_MASK \
232 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
234 /* These must match the bit postions in trace_iterator_flags */
235 static const char *trace_options[] = {
251 * ftrace_max_lock is used to protect the swapping of buffers
252 * when taking a max snapshot. The buffers themselves are
253 * protected by per_cpu spinlocks. But the action of the swap
254 * needs its own lock.
256 * This is defined as a raw_spinlock_t in order to help
257 * with performance when lockdep debugging is enabled.
259 static raw_spinlock_t ftrace_max_lock =
260 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
263 * Copy the new maximum trace into the separate maximum-trace
264 * structure. (this way the maximum trace is permanently saved,
265 * for later retrieval via /debugfs/tracing/latency_trace)
268 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
270 struct trace_array_cpu *data = tr->data[cpu];
273 max_tr.time_start = data->preempt_timestamp;
275 data = max_tr.data[cpu];
276 data->saved_latency = tracing_max_latency;
278 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
279 data->pid = tsk->pid;
280 data->uid = tsk->uid;
281 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
282 data->policy = tsk->policy;
283 data->rt_priority = tsk->rt_priority;
285 /* record this tasks comm */
286 tracing_record_cmdline(current);
290 * trace_seq_printf - sequence printing of trace information
291 * @s: trace sequence descriptor
292 * @fmt: printf format string
294 * The tracer may use either sequence operations or its own
295 * copy to user routines. To simplify formating of a trace
296 * trace_seq_printf is used to store strings into a special
297 * buffer (@s). Then the output may be either used by
298 * the sequencer or pulled into another buffer.
301 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
303 int len = (PAGE_SIZE - 1) - s->len;
311 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
314 /* If we can't write it all, don't bother writing anything */
324 * trace_seq_puts - trace sequence printing of simple string
325 * @s: trace sequence descriptor
326 * @str: simple string to record
328 * The tracer may use either the sequence operations or its own
329 * copy to user routines. This function records a simple string
330 * into a special buffer (@s) for later retrieval by a sequencer
331 * or other mechanism.
334 trace_seq_puts(struct trace_seq *s, const char *str)
336 int len = strlen(str);
338 if (len > ((PAGE_SIZE - 1) - s->len))
341 memcpy(s->buffer + s->len, str, len);
348 trace_seq_putc(struct trace_seq *s, unsigned char c)
350 if (s->len >= (PAGE_SIZE - 1))
353 s->buffer[s->len++] = c;
359 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
361 if (len > ((PAGE_SIZE - 1) - s->len))
364 memcpy(s->buffer + s->len, mem, len);
370 #define MAX_MEMHEX_BYTES 8
371 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
374 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
376 unsigned char hex[HEX_CHARS];
377 unsigned char *data = mem;
381 for (i = 0, j = 0; i < len; i++) {
383 for (i = len-1, j = 0; i >= 0; i--) {
385 hex[j++] = hex_asc_hi(data[i]);
386 hex[j++] = hex_asc_lo(data[i]);
390 return trace_seq_putmem(s, hex, j);
394 trace_seq_reset(struct trace_seq *s)
400 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
405 if (s->len <= s->readpos)
408 len = s->len - s->readpos;
411 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
420 trace_print_seq(struct seq_file *m, struct trace_seq *s)
422 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
425 seq_puts(m, s->buffer);
431 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
433 * @tsk: the task with the latency
434 * @cpu: The cpu that initiated the trace.
436 * Flip the buffers between the @tr and the max_tr and record information
437 * about which task was the cause of this latency.
440 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
442 struct ring_buffer *buf = tr->buffer;
444 WARN_ON_ONCE(!irqs_disabled());
445 __raw_spin_lock(&ftrace_max_lock);
447 tr->buffer = max_tr.buffer;
450 ftrace_disable_cpu();
451 ring_buffer_reset(tr->buffer);
454 __update_max_tr(tr, tsk, cpu);
455 __raw_spin_unlock(&ftrace_max_lock);
459 * update_max_tr_single - only copy one trace over, and reset the rest
461 * @tsk - task with the latency
462 * @cpu - the cpu of the buffer to copy.
464 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
467 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
471 WARN_ON_ONCE(!irqs_disabled());
472 __raw_spin_lock(&ftrace_max_lock);
474 ftrace_disable_cpu();
476 ring_buffer_reset(max_tr.buffer);
477 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
483 __update_max_tr(tr, tsk, cpu);
484 __raw_spin_unlock(&ftrace_max_lock);
488 * register_tracer - register a tracer with the ftrace system.
489 * @type - the plugin for the tracer
491 * Register a new plugin tracer.
493 int register_tracer(struct tracer *type)
500 pr_info("Tracer must have a name\n");
504 mutex_lock(&trace_types_lock);
505 for (t = trace_types; t; t = t->next) {
506 if (strcmp(type->name, t->name) == 0) {
508 pr_info("Trace %s already registered\n",
515 #ifdef CONFIG_FTRACE_STARTUP_TEST
516 if (type->selftest) {
517 struct tracer *saved_tracer = current_trace;
518 struct trace_array *tr = &global_trace;
519 int saved_ctrl = tr->ctrl;
522 * Run a selftest on this tracer.
523 * Here we reset the trace buffer, and set the current
524 * tracer to be this tracer. The tracer can then run some
525 * internal tracing to verify that everything is in order.
526 * If we fail, we do not register this tracer.
528 for_each_tracing_cpu(i) {
529 tracing_reset(tr, i);
531 current_trace = type;
533 /* the test is responsible for initializing and enabling */
534 pr_info("Testing tracer %s: ", type->name);
535 ret = type->selftest(type, tr);
536 /* the test is responsible for resetting too */
537 current_trace = saved_tracer;
538 tr->ctrl = saved_ctrl;
540 printk(KERN_CONT "FAILED!\n");
543 /* Only reset on passing, to avoid touching corrupted buffers */
544 for_each_tracing_cpu(i) {
545 tracing_reset(tr, i);
547 printk(KERN_CONT "PASSED\n");
551 type->next = trace_types;
553 len = strlen(type->name);
554 if (len > max_tracer_type_len)
555 max_tracer_type_len = len;
558 mutex_unlock(&trace_types_lock);
563 void unregister_tracer(struct tracer *type)
568 mutex_lock(&trace_types_lock);
569 for (t = &trace_types; *t; t = &(*t)->next) {
573 pr_info("Trace %s not registered\n", type->name);
578 if (strlen(type->name) != max_tracer_type_len)
581 max_tracer_type_len = 0;
582 for (t = &trace_types; *t; t = &(*t)->next) {
583 len = strlen((*t)->name);
584 if (len > max_tracer_type_len)
585 max_tracer_type_len = len;
588 mutex_unlock(&trace_types_lock);
591 void tracing_reset(struct trace_array *tr, int cpu)
593 ftrace_disable_cpu();
594 ring_buffer_reset_cpu(tr->buffer, cpu);
598 #define SAVED_CMDLINES 128
599 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
600 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
601 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
602 static int cmdline_idx;
603 static DEFINE_SPINLOCK(trace_cmdline_lock);
605 /* temporary disable recording */
606 atomic_t trace_record_cmdline_disabled __read_mostly;
608 static void trace_init_cmdlines(void)
610 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
611 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
615 void trace_stop_cmdline_recording(void);
617 static void trace_save_cmdline(struct task_struct *tsk)
622 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
626 * It's not the end of the world if we don't get
627 * the lock, but we also don't want to spin
628 * nor do we want to disable interrupts,
629 * so if we miss here, then better luck next time.
631 if (!spin_trylock(&trace_cmdline_lock))
634 idx = map_pid_to_cmdline[tsk->pid];
635 if (idx >= SAVED_CMDLINES) {
636 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
638 map = map_cmdline_to_pid[idx];
639 if (map <= PID_MAX_DEFAULT)
640 map_pid_to_cmdline[map] = (unsigned)-1;
642 map_pid_to_cmdline[tsk->pid] = idx;
647 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
649 spin_unlock(&trace_cmdline_lock);
652 static char *trace_find_cmdline(int pid)
654 char *cmdline = "<...>";
660 if (pid > PID_MAX_DEFAULT)
663 map = map_pid_to_cmdline[pid];
664 if (map >= SAVED_CMDLINES)
667 cmdline = saved_cmdlines[map];
673 void tracing_record_cmdline(struct task_struct *tsk)
675 if (atomic_read(&trace_record_cmdline_disabled))
678 trace_save_cmdline(tsk);
682 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
685 struct task_struct *tsk = current;
687 entry->preempt_count = pc & 0xff;
688 entry->pid = (tsk) ? tsk->pid : 0;
690 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
691 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
693 TRACE_FLAG_IRQS_NOSUPPORT |
695 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
696 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
697 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
701 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
702 unsigned long ip, unsigned long parent_ip, unsigned long flags,
705 struct ring_buffer_event *event;
706 struct ftrace_entry *entry;
707 unsigned long irq_flags;
709 /* If we are reading the ring buffer, don't trace */
710 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
713 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
717 entry = ring_buffer_event_data(event);
718 tracing_generic_entry_update(&entry->ent, flags, pc);
719 entry->ent.type = TRACE_FN;
721 entry->parent_ip = parent_ip;
722 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
726 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
727 unsigned long ip, unsigned long parent_ip, unsigned long flags,
730 if (likely(!atomic_read(&data->disabled)))
731 trace_function(tr, data, ip, parent_ip, flags, pc);
734 static void ftrace_trace_stack(struct trace_array *tr,
735 struct trace_array_cpu *data,
739 #ifdef CONFIG_STACKTRACE
740 struct ring_buffer_event *event;
741 struct stack_entry *entry;
742 struct stack_trace trace;
743 unsigned long irq_flags;
745 if (!(trace_flags & TRACE_ITER_STACKTRACE))
748 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
752 entry = ring_buffer_event_data(event);
753 tracing_generic_entry_update(&entry->ent, flags, pc);
754 entry->ent.type = TRACE_STACK;
756 memset(&entry->caller, 0, sizeof(entry->caller));
758 trace.nr_entries = 0;
759 trace.max_entries = FTRACE_STACK_ENTRIES;
761 trace.entries = entry->caller;
763 save_stack_trace(&trace);
764 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
768 void __trace_stack(struct trace_array *tr,
769 struct trace_array_cpu *data,
773 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
777 ftrace_trace_special(void *__tr, void *__data,
778 unsigned long arg1, unsigned long arg2, unsigned long arg3,
781 struct ring_buffer_event *event;
782 struct trace_array_cpu *data = __data;
783 struct trace_array *tr = __tr;
784 struct special_entry *entry;
785 unsigned long irq_flags;
787 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
791 entry = ring_buffer_event_data(event);
792 tracing_generic_entry_update(&entry->ent, 0, pc);
793 entry->ent.type = TRACE_SPECIAL;
797 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
798 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
804 __trace_special(void *__tr, void *__data,
805 unsigned long arg1, unsigned long arg2, unsigned long arg3)
807 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
811 tracing_sched_switch_trace(struct trace_array *tr,
812 struct trace_array_cpu *data,
813 struct task_struct *prev,
814 struct task_struct *next,
815 unsigned long flags, int pc)
817 struct ring_buffer_event *event;
818 struct ctx_switch_entry *entry;
819 unsigned long irq_flags;
821 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
825 entry = ring_buffer_event_data(event);
826 tracing_generic_entry_update(&entry->ent, flags, pc);
827 entry->ent.type = TRACE_CTX;
828 entry->prev_pid = prev->pid;
829 entry->prev_prio = prev->prio;
830 entry->prev_state = prev->state;
831 entry->next_pid = next->pid;
832 entry->next_prio = next->prio;
833 entry->next_state = next->state;
834 entry->next_cpu = task_cpu(next);
835 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
836 ftrace_trace_stack(tr, data, flags, 5, pc);
840 tracing_sched_wakeup_trace(struct trace_array *tr,
841 struct trace_array_cpu *data,
842 struct task_struct *wakee,
843 struct task_struct *curr,
844 unsigned long flags, int pc)
846 struct ring_buffer_event *event;
847 struct ctx_switch_entry *entry;
848 unsigned long irq_flags;
850 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
854 entry = ring_buffer_event_data(event);
855 tracing_generic_entry_update(&entry->ent, flags, pc);
856 entry->ent.type = TRACE_WAKE;
857 entry->prev_pid = curr->pid;
858 entry->prev_prio = curr->prio;
859 entry->prev_state = curr->state;
860 entry->next_pid = wakee->pid;
861 entry->next_prio = wakee->prio;
862 entry->next_state = wakee->state;
863 entry->next_cpu = task_cpu(wakee);
864 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
865 ftrace_trace_stack(tr, data, flags, 6, pc);
871 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
873 struct trace_array *tr = &global_trace;
874 struct trace_array_cpu *data;
878 if (tracing_disabled || !tr->ctrl)
881 pc = preempt_count();
882 preempt_disable_notrace();
883 cpu = raw_smp_processor_id();
884 data = tr->data[cpu];
886 if (likely(!atomic_read(&data->disabled)))
887 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
889 preempt_enable_notrace();
892 #ifdef CONFIG_FUNCTION_TRACER
894 function_trace_call(unsigned long ip, unsigned long parent_ip)
896 struct trace_array *tr = &global_trace;
897 struct trace_array_cpu *data;
903 if (unlikely(!ftrace_function_enabled))
906 pc = preempt_count();
907 resched = need_resched();
908 preempt_disable_notrace();
909 local_save_flags(flags);
910 cpu = raw_smp_processor_id();
911 data = tr->data[cpu];
912 disabled = atomic_inc_return(&data->disabled);
914 if (likely(disabled == 1))
915 trace_function(tr, data, ip, parent_ip, flags, pc);
917 atomic_dec(&data->disabled);
919 preempt_enable_no_resched_notrace();
921 preempt_enable_notrace();
924 static struct ftrace_ops trace_ops __read_mostly =
926 .func = function_trace_call,
929 void tracing_start_function_trace(void)
931 ftrace_function_enabled = 0;
932 register_ftrace_function(&trace_ops);
934 ftrace_function_enabled = 1;
937 void tracing_stop_function_trace(void)
939 ftrace_function_enabled = 0;
940 unregister_ftrace_function(&trace_ops);
944 enum trace_file_type {
945 TRACE_FILE_LAT_FMT = 1,
948 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
950 /* Don't allow ftrace to trace into the ring buffers */
951 ftrace_disable_cpu();
954 if (iter->buffer_iter[iter->cpu])
955 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
960 static struct trace_entry *
961 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
963 struct ring_buffer_event *event;
964 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
966 /* Don't allow ftrace to trace into the ring buffers */
967 ftrace_disable_cpu();
970 event = ring_buffer_iter_peek(buf_iter, ts);
972 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
976 return event ? ring_buffer_event_data(event) : NULL;
979 static struct trace_entry *
980 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
982 struct ring_buffer *buffer = iter->tr->buffer;
983 struct trace_entry *ent, *next = NULL;
988 for_each_tracing_cpu(cpu) {
990 if (ring_buffer_empty_cpu(buffer, cpu))
993 ent = peek_next_entry(iter, cpu, &ts);
996 * Pick the entry with the smallest timestamp:
998 if (ent && (!next || ts < next_ts)) {
1006 *ent_cpu = next_cpu;
1014 /* Find the next real entry, without updating the iterator itself */
1015 static struct trace_entry *
1016 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1018 return __find_next_entry(iter, ent_cpu, ent_ts);
1021 /* Find the next real entry, and increment the iterator to the next entry */
1022 static void *find_next_entry_inc(struct trace_iterator *iter)
1024 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1027 trace_iterator_increment(iter, iter->cpu);
1029 return iter->ent ? iter : NULL;
1032 static void trace_consume(struct trace_iterator *iter)
1034 /* Don't allow ftrace to trace into the ring buffers */
1035 ftrace_disable_cpu();
1036 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1037 ftrace_enable_cpu();
1040 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1042 struct trace_iterator *iter = m->private;
1048 /* can't go backwards */
1053 ent = find_next_entry_inc(iter);
1057 while (ent && iter->idx < i)
1058 ent = find_next_entry_inc(iter);
1065 static void *s_start(struct seq_file *m, loff_t *pos)
1067 struct trace_iterator *iter = m->private;
1072 mutex_lock(&trace_types_lock);
1074 if (!current_trace || current_trace != iter->trace) {
1075 mutex_unlock(&trace_types_lock);
1079 atomic_inc(&trace_record_cmdline_disabled);
1081 /* let the tracer grab locks here if needed */
1082 if (current_trace->start)
1083 current_trace->start(iter);
1085 if (*pos != iter->pos) {
1090 ftrace_disable_cpu();
1092 for_each_tracing_cpu(cpu) {
1093 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1096 ftrace_enable_cpu();
1098 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1103 p = s_next(m, p, &l);
1109 static void s_stop(struct seq_file *m, void *p)
1111 struct trace_iterator *iter = m->private;
1113 atomic_dec(&trace_record_cmdline_disabled);
1115 /* let the tracer release locks here if needed */
1116 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1117 iter->trace->stop(iter);
1119 mutex_unlock(&trace_types_lock);
1122 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1124 #ifdef CONFIG_KRETPROBES
1125 static inline int kretprobed(unsigned long addr)
1127 return addr == (unsigned long)kretprobe_trampoline;
1130 static inline int kretprobed(unsigned long addr)
1134 #endif /* CONFIG_KRETPROBES */
1137 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1139 #ifdef CONFIG_KALLSYMS
1140 char str[KSYM_SYMBOL_LEN];
1142 kallsyms_lookup(address, NULL, NULL, NULL, str);
1144 return trace_seq_printf(s, fmt, str);
1150 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1151 unsigned long address)
1153 #ifdef CONFIG_KALLSYMS
1154 char str[KSYM_SYMBOL_LEN];
1156 sprint_symbol(str, address);
1157 return trace_seq_printf(s, fmt, str);
1162 #ifndef CONFIG_64BIT
1163 # define IP_FMT "%08lx"
1165 # define IP_FMT "%016lx"
1169 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1174 return trace_seq_printf(s, "0");
1176 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1177 ret = seq_print_sym_offset(s, "%s", ip);
1179 ret = seq_print_sym_short(s, "%s", ip);
1184 if (sym_flags & TRACE_ITER_SYM_ADDR)
1185 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1189 static void print_lat_help_header(struct seq_file *m)
1191 seq_puts(m, "# _------=> CPU# \n");
1192 seq_puts(m, "# / _-----=> irqs-off \n");
1193 seq_puts(m, "# | / _----=> need-resched \n");
1194 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1195 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1196 seq_puts(m, "# |||| / \n");
1197 seq_puts(m, "# ||||| delay \n");
1198 seq_puts(m, "# cmd pid ||||| time | caller \n");
1199 seq_puts(m, "# \\ / ||||| \\ | / \n");
1202 static void print_func_help_header(struct seq_file *m)
1204 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1205 seq_puts(m, "# | | | | |\n");
1210 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1212 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1213 struct trace_array *tr = iter->tr;
1214 struct trace_array_cpu *data = tr->data[tr->cpu];
1215 struct tracer *type = current_trace;
1216 unsigned long total;
1217 unsigned long entries;
1218 const char *name = "preemption";
1223 entries = ring_buffer_entries(iter->tr->buffer);
1225 ring_buffer_overruns(iter->tr->buffer);
1227 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1229 seq_puts(m, "-----------------------------------"
1230 "---------------------------------\n");
1231 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1232 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1233 nsecs_to_usecs(data->saved_latency),
1237 #if defined(CONFIG_PREEMPT_NONE)
1239 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1241 #elif defined(CONFIG_PREEMPT)
1246 /* These are reserved for later use */
1249 seq_printf(m, " #P:%d)\n", num_online_cpus());
1253 seq_puts(m, " -----------------\n");
1254 seq_printf(m, " | task: %.16s-%d "
1255 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1256 data->comm, data->pid, data->uid, data->nice,
1257 data->policy, data->rt_priority);
1258 seq_puts(m, " -----------------\n");
1260 if (data->critical_start) {
1261 seq_puts(m, " => started at: ");
1262 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1263 trace_print_seq(m, &iter->seq);
1264 seq_puts(m, "\n => ended at: ");
1265 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1266 trace_print_seq(m, &iter->seq);
1274 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1276 int hardirq, softirq;
1279 comm = trace_find_cmdline(entry->pid);
1281 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1282 trace_seq_printf(s, "%3d", cpu);
1283 trace_seq_printf(s, "%c%c",
1284 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1285 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1286 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1288 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1289 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1290 if (hardirq && softirq) {
1291 trace_seq_putc(s, 'H');
1294 trace_seq_putc(s, 'h');
1297 trace_seq_putc(s, 's');
1299 trace_seq_putc(s, '.');
1303 if (entry->preempt_count)
1304 trace_seq_printf(s, "%x", entry->preempt_count);
1306 trace_seq_puts(s, ".");
1309 unsigned long preempt_mark_thresh = 100;
1312 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1313 unsigned long rel_usecs)
1315 trace_seq_printf(s, " %4lldus", abs_usecs);
1316 if (rel_usecs > preempt_mark_thresh)
1317 trace_seq_puts(s, "!: ");
1318 else if (rel_usecs > 1)
1319 trace_seq_puts(s, "+: ");
1321 trace_seq_puts(s, " : ");
1324 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1327 * The message is supposed to contain an ending newline.
1328 * If the printing stops prematurely, try to add a newline of our own.
1330 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1332 struct trace_entry *ent;
1333 struct trace_field_cont *cont;
1336 ent = peek_next_entry(iter, iter->cpu, NULL);
1337 if (!ent || ent->type != TRACE_CONT) {
1338 trace_seq_putc(s, '\n');
1343 cont = (struct trace_field_cont *)ent;
1345 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1347 ftrace_disable_cpu();
1349 if (iter->buffer_iter[iter->cpu])
1350 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1352 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1354 ftrace_enable_cpu();
1356 ent = peek_next_entry(iter, iter->cpu, NULL);
1357 } while (ent && ent->type == TRACE_CONT);
1360 trace_seq_putc(s, '\n');
1363 static enum print_line_t
1364 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1366 struct trace_seq *s = &iter->seq;
1367 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1368 struct trace_entry *next_entry;
1369 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1370 struct trace_entry *entry = iter->ent;
1371 unsigned long abs_usecs;
1372 unsigned long rel_usecs;
1379 if (entry->type == TRACE_CONT)
1380 return TRACE_TYPE_HANDLED;
1382 next_entry = find_next_entry(iter, NULL, &next_ts);
1385 rel_usecs = ns2usecs(next_ts - iter->ts);
1386 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1389 comm = trace_find_cmdline(entry->pid);
1390 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1391 " %ld.%03ldms (+%ld.%03ldms): ",
1393 entry->pid, cpu, entry->flags,
1394 entry->preempt_count, trace_idx,
1397 abs_usecs % 1000, rel_usecs/1000,
1400 lat_print_generic(s, entry, cpu);
1401 lat_print_timestamp(s, abs_usecs, rel_usecs);
1403 switch (entry->type) {
1405 struct ftrace_entry *field;
1407 trace_assign_type(field, entry);
1409 seq_print_ip_sym(s, field->ip, sym_flags);
1410 trace_seq_puts(s, " (");
1411 if (kretprobed(field->parent_ip))
1412 trace_seq_puts(s, KRETPROBE_MSG);
1414 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1415 trace_seq_puts(s, ")\n");
1420 struct ctx_switch_entry *field;
1422 trace_assign_type(field, entry);
1424 T = field->next_state < sizeof(state_to_char) ?
1425 state_to_char[field->next_state] : 'X';
1427 state = field->prev_state ?
1428 __ffs(field->prev_state) + 1 : 0;
1429 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1430 comm = trace_find_cmdline(field->next_pid);
1431 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1434 S, entry->type == TRACE_CTX ? "==>" : " +",
1441 case TRACE_SPECIAL: {
1442 struct special_entry *field;
1444 trace_assign_type(field, entry);
1446 trace_seq_printf(s, "# %ld %ld %ld\n",
1453 struct stack_entry *field;
1455 trace_assign_type(field, entry);
1457 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1459 trace_seq_puts(s, " <= ");
1460 seq_print_ip_sym(s, field->caller[i], sym_flags);
1462 trace_seq_puts(s, "\n");
1466 struct print_entry *field;
1468 trace_assign_type(field, entry);
1470 seq_print_ip_sym(s, field->ip, sym_flags);
1471 trace_seq_printf(s, ": %s", field->buf);
1472 if (entry->flags & TRACE_FLAG_CONT)
1473 trace_seq_print_cont(s, iter);
1477 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1479 return TRACE_TYPE_HANDLED;
1482 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1484 struct trace_seq *s = &iter->seq;
1485 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1486 struct trace_entry *entry;
1487 unsigned long usec_rem;
1488 unsigned long long t;
1497 if (entry->type == TRACE_CONT)
1498 return TRACE_TYPE_HANDLED;
1500 comm = trace_find_cmdline(iter->ent->pid);
1502 t = ns2usecs(iter->ts);
1503 usec_rem = do_div(t, 1000000ULL);
1504 secs = (unsigned long)t;
1506 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1508 return TRACE_TYPE_PARTIAL_LINE;
1509 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1511 return TRACE_TYPE_PARTIAL_LINE;
1512 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1514 return TRACE_TYPE_PARTIAL_LINE;
1516 switch (entry->type) {
1518 struct ftrace_entry *field;
1520 trace_assign_type(field, entry);
1522 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1524 return TRACE_TYPE_PARTIAL_LINE;
1525 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1527 ret = trace_seq_printf(s, " <-");
1529 return TRACE_TYPE_PARTIAL_LINE;
1530 if (kretprobed(field->parent_ip))
1531 ret = trace_seq_puts(s, KRETPROBE_MSG);
1533 ret = seq_print_ip_sym(s,
1537 return TRACE_TYPE_PARTIAL_LINE;
1539 ret = trace_seq_printf(s, "\n");
1541 return TRACE_TYPE_PARTIAL_LINE;
1546 struct ctx_switch_entry *field;
1548 trace_assign_type(field, entry);
1550 S = field->prev_state < sizeof(state_to_char) ?
1551 state_to_char[field->prev_state] : 'X';
1552 T = field->next_state < sizeof(state_to_char) ?
1553 state_to_char[field->next_state] : 'X';
1554 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1558 entry->type == TRACE_CTX ? "==>" : " +",
1564 return TRACE_TYPE_PARTIAL_LINE;
1567 case TRACE_SPECIAL: {
1568 struct special_entry *field;
1570 trace_assign_type(field, entry);
1572 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1577 return TRACE_TYPE_PARTIAL_LINE;
1581 struct stack_entry *field;
1583 trace_assign_type(field, entry);
1585 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1587 ret = trace_seq_puts(s, " <= ");
1589 return TRACE_TYPE_PARTIAL_LINE;
1591 ret = seq_print_ip_sym(s, field->caller[i],
1594 return TRACE_TYPE_PARTIAL_LINE;
1596 ret = trace_seq_puts(s, "\n");
1598 return TRACE_TYPE_PARTIAL_LINE;
1602 struct print_entry *field;
1604 trace_assign_type(field, entry);
1606 seq_print_ip_sym(s, field->ip, sym_flags);
1607 trace_seq_printf(s, ": %s", field->buf);
1608 if (entry->flags & TRACE_FLAG_CONT)
1609 trace_seq_print_cont(s, iter);
1613 return TRACE_TYPE_HANDLED;
1616 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1618 struct trace_seq *s = &iter->seq;
1619 struct trace_entry *entry;
1625 if (entry->type == TRACE_CONT)
1626 return TRACE_TYPE_HANDLED;
1628 ret = trace_seq_printf(s, "%d %d %llu ",
1629 entry->pid, iter->cpu, iter->ts);
1631 return TRACE_TYPE_PARTIAL_LINE;
1633 switch (entry->type) {
1635 struct ftrace_entry *field;
1637 trace_assign_type(field, entry);
1639 ret = trace_seq_printf(s, "%x %x\n",
1643 return TRACE_TYPE_PARTIAL_LINE;
1648 struct ctx_switch_entry *field;
1650 trace_assign_type(field, entry);
1652 S = field->prev_state < sizeof(state_to_char) ?
1653 state_to_char[field->prev_state] : 'X';
1654 T = field->next_state < sizeof(state_to_char) ?
1655 state_to_char[field->next_state] : 'X';
1656 if (entry->type == TRACE_WAKE)
1658 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
1667 return TRACE_TYPE_PARTIAL_LINE;
1672 struct special_entry *field;
1674 trace_assign_type(field, entry);
1676 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1681 return TRACE_TYPE_PARTIAL_LINE;
1685 struct print_entry *field;
1687 trace_assign_type(field, entry);
1689 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
1690 if (entry->flags & TRACE_FLAG_CONT)
1691 trace_seq_print_cont(s, iter);
1695 return TRACE_TYPE_HANDLED;
1698 #define SEQ_PUT_FIELD_RET(s, x) \
1700 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1704 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1706 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
1707 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1711 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1713 struct trace_seq *s = &iter->seq;
1714 unsigned char newline = '\n';
1715 struct trace_entry *entry;
1720 if (entry->type == TRACE_CONT)
1721 return TRACE_TYPE_HANDLED;
1723 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1724 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1725 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1727 switch (entry->type) {
1729 struct ftrace_entry *field;
1731 trace_assign_type(field, entry);
1733 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1734 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
1739 struct ctx_switch_entry *field;
1741 trace_assign_type(field, entry);
1743 S = field->prev_state < sizeof(state_to_char) ?
1744 state_to_char[field->prev_state] : 'X';
1745 T = field->next_state < sizeof(state_to_char) ?
1746 state_to_char[field->next_state] : 'X';
1747 if (entry->type == TRACE_WAKE)
1749 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1750 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
1751 SEQ_PUT_HEX_FIELD_RET(s, S);
1752 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1753 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1754 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
1755 SEQ_PUT_HEX_FIELD_RET(s, T);
1760 struct special_entry *field;
1762 trace_assign_type(field, entry);
1764 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
1765 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
1766 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
1770 SEQ_PUT_FIELD_RET(s, newline);
1772 return TRACE_TYPE_HANDLED;
1775 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1777 struct trace_seq *s = &iter->seq;
1778 struct trace_entry *entry;
1782 if (entry->type == TRACE_CONT)
1783 return TRACE_TYPE_HANDLED;
1785 SEQ_PUT_FIELD_RET(s, entry->pid);
1786 SEQ_PUT_FIELD_RET(s, iter->cpu);
1787 SEQ_PUT_FIELD_RET(s, iter->ts);
1789 switch (entry->type) {
1791 struct ftrace_entry *field;
1793 trace_assign_type(field, entry);
1795 SEQ_PUT_FIELD_RET(s, field->ip);
1796 SEQ_PUT_FIELD_RET(s, field->parent_ip);
1800 struct ctx_switch_entry *field;
1802 trace_assign_type(field, entry);
1804 SEQ_PUT_FIELD_RET(s, field->prev_pid);
1805 SEQ_PUT_FIELD_RET(s, field->prev_prio);
1806 SEQ_PUT_FIELD_RET(s, field->prev_state);
1807 SEQ_PUT_FIELD_RET(s, field->next_pid);
1808 SEQ_PUT_FIELD_RET(s, field->next_prio);
1809 SEQ_PUT_FIELD_RET(s, field->next_state);
1814 struct special_entry *field;
1816 trace_assign_type(field, entry);
1818 SEQ_PUT_FIELD_RET(s, field->arg1);
1819 SEQ_PUT_FIELD_RET(s, field->arg2);
1820 SEQ_PUT_FIELD_RET(s, field->arg3);
1827 static int trace_empty(struct trace_iterator *iter)
1831 for_each_tracing_cpu(cpu) {
1832 if (iter->buffer_iter[cpu]) {
1833 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1836 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1844 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1846 enum print_line_t ret;
1848 if (iter->trace && iter->trace->print_line) {
1849 ret = iter->trace->print_line(iter);
1850 if (ret != TRACE_TYPE_UNHANDLED)
1854 if (trace_flags & TRACE_ITER_BIN)
1855 return print_bin_fmt(iter);
1857 if (trace_flags & TRACE_ITER_HEX)
1858 return print_hex_fmt(iter);
1860 if (trace_flags & TRACE_ITER_RAW)
1861 return print_raw_fmt(iter);
1863 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1864 return print_lat_fmt(iter, iter->idx, iter->cpu);
1866 return print_trace_fmt(iter);
1869 static int s_show(struct seq_file *m, void *v)
1871 struct trace_iterator *iter = v;
1873 if (iter->ent == NULL) {
1875 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1878 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1879 /* print nothing if the buffers are empty */
1880 if (trace_empty(iter))
1882 print_trace_header(m, iter);
1883 if (!(trace_flags & TRACE_ITER_VERBOSE))
1884 print_lat_help_header(m);
1886 if (!(trace_flags & TRACE_ITER_VERBOSE))
1887 print_func_help_header(m);
1890 print_trace_line(iter);
1891 trace_print_seq(m, &iter->seq);
1897 static struct seq_operations tracer_seq_ops = {
1904 static struct trace_iterator *
1905 __tracing_open(struct inode *inode, struct file *file, int *ret)
1907 struct trace_iterator *iter;
1911 if (tracing_disabled) {
1916 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1922 mutex_lock(&trace_types_lock);
1923 if (current_trace && current_trace->print_max)
1926 iter->tr = inode->i_private;
1927 iter->trace = current_trace;
1930 for_each_tracing_cpu(cpu) {
1932 iter->buffer_iter[cpu] =
1933 ring_buffer_read_start(iter->tr->buffer, cpu);
1935 if (!iter->buffer_iter[cpu])
1939 /* TODO stop tracer */
1940 *ret = seq_open(file, &tracer_seq_ops);
1944 m = file->private_data;
1947 /* stop the trace while dumping */
1948 if (iter->tr->ctrl) {
1950 ftrace_function_enabled = 0;
1953 if (iter->trace && iter->trace->open)
1954 iter->trace->open(iter);
1956 mutex_unlock(&trace_types_lock);
1962 for_each_tracing_cpu(cpu) {
1963 if (iter->buffer_iter[cpu])
1964 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1966 mutex_unlock(&trace_types_lock);
1968 return ERR_PTR(-ENOMEM);
1971 int tracing_open_generic(struct inode *inode, struct file *filp)
1973 if (tracing_disabled)
1976 filp->private_data = inode->i_private;
1980 int tracing_release(struct inode *inode, struct file *file)
1982 struct seq_file *m = (struct seq_file *)file->private_data;
1983 struct trace_iterator *iter = m->private;
1986 mutex_lock(&trace_types_lock);
1987 for_each_tracing_cpu(cpu) {
1988 if (iter->buffer_iter[cpu])
1989 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1992 if (iter->trace && iter->trace->close)
1993 iter->trace->close(iter);
1995 /* reenable tracing if it was previously enabled */
1996 if (iter->tr->ctrl) {
1999 * It is safe to enable function tracing even if it
2002 ftrace_function_enabled = 1;
2004 mutex_unlock(&trace_types_lock);
2006 seq_release(inode, file);
2011 static int tracing_open(struct inode *inode, struct file *file)
2015 __tracing_open(inode, file, &ret);
2020 static int tracing_lt_open(struct inode *inode, struct file *file)
2022 struct trace_iterator *iter;
2025 iter = __tracing_open(inode, file, &ret);
2028 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2035 t_next(struct seq_file *m, void *v, loff_t *pos)
2037 struct tracer *t = m->private;
2049 static void *t_start(struct seq_file *m, loff_t *pos)
2051 struct tracer *t = m->private;
2054 mutex_lock(&trace_types_lock);
2055 for (; t && l < *pos; t = t_next(m, t, &l))
2061 static void t_stop(struct seq_file *m, void *p)
2063 mutex_unlock(&trace_types_lock);
2066 static int t_show(struct seq_file *m, void *v)
2068 struct tracer *t = v;
2073 seq_printf(m, "%s", t->name);
2082 static struct seq_operations show_traces_seq_ops = {
2089 static int show_traces_open(struct inode *inode, struct file *file)
2093 if (tracing_disabled)
2096 ret = seq_open(file, &show_traces_seq_ops);
2098 struct seq_file *m = file->private_data;
2099 m->private = trace_types;
2105 static struct file_operations tracing_fops = {
2106 .open = tracing_open,
2108 .llseek = seq_lseek,
2109 .release = tracing_release,
2112 static struct file_operations tracing_lt_fops = {
2113 .open = tracing_lt_open,
2115 .llseek = seq_lseek,
2116 .release = tracing_release,
2119 static struct file_operations show_traces_fops = {
2120 .open = show_traces_open,
2122 .release = seq_release,
2126 * Only trace on a CPU if the bitmask is set:
2128 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2131 * When tracing/tracing_cpu_mask is modified then this holds
2132 * the new bitmask we are about to install:
2134 static cpumask_t tracing_cpumask_new;
2137 * The tracer itself will not take this lock, but still we want
2138 * to provide a consistent cpumask to user-space:
2140 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2143 * Temporary storage for the character representation of the
2144 * CPU bitmask (and one more byte for the newline):
2146 static char mask_str[NR_CPUS + 1];
2149 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2150 size_t count, loff_t *ppos)
2154 mutex_lock(&tracing_cpumask_update_lock);
2156 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2157 if (count - len < 2) {
2161 len += sprintf(mask_str + len, "\n");
2162 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2165 mutex_unlock(&tracing_cpumask_update_lock);
2171 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2172 size_t count, loff_t *ppos)
2176 mutex_lock(&tracing_cpumask_update_lock);
2177 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2181 raw_local_irq_disable();
2182 __raw_spin_lock(&ftrace_max_lock);
2183 for_each_tracing_cpu(cpu) {
2185 * Increase/decrease the disabled counter if we are
2186 * about to flip a bit in the cpumask:
2188 if (cpu_isset(cpu, tracing_cpumask) &&
2189 !cpu_isset(cpu, tracing_cpumask_new)) {
2190 atomic_inc(&global_trace.data[cpu]->disabled);
2192 if (!cpu_isset(cpu, tracing_cpumask) &&
2193 cpu_isset(cpu, tracing_cpumask_new)) {
2194 atomic_dec(&global_trace.data[cpu]->disabled);
2197 __raw_spin_unlock(&ftrace_max_lock);
2198 raw_local_irq_enable();
2200 tracing_cpumask = tracing_cpumask_new;
2202 mutex_unlock(&tracing_cpumask_update_lock);
2207 mutex_unlock(&tracing_cpumask_update_lock);
2212 static struct file_operations tracing_cpumask_fops = {
2213 .open = tracing_open_generic,
2214 .read = tracing_cpumask_read,
2215 .write = tracing_cpumask_write,
2219 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2220 size_t cnt, loff_t *ppos)
2227 /* calulate max size */
2228 for (i = 0; trace_options[i]; i++) {
2229 len += strlen(trace_options[i]);
2230 len += 3; /* "no" and space */
2233 /* +2 for \n and \0 */
2234 buf = kmalloc(len + 2, GFP_KERNEL);
2238 for (i = 0; trace_options[i]; i++) {
2239 if (trace_flags & (1 << i))
2240 r += sprintf(buf + r, "%s ", trace_options[i]);
2242 r += sprintf(buf + r, "no%s ", trace_options[i]);
2245 r += sprintf(buf + r, "\n");
2246 WARN_ON(r >= len + 2);
2248 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2256 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2257 size_t cnt, loff_t *ppos)
2264 if (cnt >= sizeof(buf))
2267 if (copy_from_user(&buf, ubuf, cnt))
2272 if (strncmp(buf, "no", 2) == 0) {
2277 for (i = 0; trace_options[i]; i++) {
2278 int len = strlen(trace_options[i]);
2280 if (strncmp(cmp, trace_options[i], len) == 0) {
2282 trace_flags &= ~(1 << i);
2284 trace_flags |= (1 << i);
2289 * If no option could be set, return an error:
2291 if (!trace_options[i])
2299 static struct file_operations tracing_iter_fops = {
2300 .open = tracing_open_generic,
2301 .read = tracing_iter_ctrl_read,
2302 .write = tracing_iter_ctrl_write,
2305 static const char readme_msg[] =
2306 "tracing mini-HOWTO:\n\n"
2308 "# mount -t debugfs nodev /debug\n\n"
2309 "# cat /debug/tracing/available_tracers\n"
2310 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2311 "# cat /debug/tracing/current_tracer\n"
2313 "# echo sched_switch > /debug/tracing/current_tracer\n"
2314 "# cat /debug/tracing/current_tracer\n"
2316 "# cat /debug/tracing/iter_ctrl\n"
2317 "noprint-parent nosym-offset nosym-addr noverbose\n"
2318 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2319 "# echo 1 > /debug/tracing/tracing_enabled\n"
2320 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2321 "echo 0 > /debug/tracing/tracing_enabled\n"
2325 tracing_readme_read(struct file *filp, char __user *ubuf,
2326 size_t cnt, loff_t *ppos)
2328 return simple_read_from_buffer(ubuf, cnt, ppos,
2329 readme_msg, strlen(readme_msg));
2332 static struct file_operations tracing_readme_fops = {
2333 .open = tracing_open_generic,
2334 .read = tracing_readme_read,
2338 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2339 size_t cnt, loff_t *ppos)
2341 struct trace_array *tr = filp->private_data;
2345 r = sprintf(buf, "%ld\n", tr->ctrl);
2346 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2350 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2351 size_t cnt, loff_t *ppos)
2353 struct trace_array *tr = filp->private_data;
2358 if (cnt >= sizeof(buf))
2361 if (copy_from_user(&buf, ubuf, cnt))
2366 ret = strict_strtoul(buf, 10, &val);
2372 mutex_lock(&trace_types_lock);
2373 if (tr->ctrl ^ val) {
2381 if (current_trace && current_trace->ctrl_update)
2382 current_trace->ctrl_update(tr);
2384 mutex_unlock(&trace_types_lock);
2392 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2393 size_t cnt, loff_t *ppos)
2395 char buf[max_tracer_type_len+2];
2398 mutex_lock(&trace_types_lock);
2400 r = sprintf(buf, "%s\n", current_trace->name);
2402 r = sprintf(buf, "\n");
2403 mutex_unlock(&trace_types_lock);
2405 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2408 static int tracing_set_tracer(char *buf)
2410 struct trace_array *tr = &global_trace;
2414 mutex_lock(&trace_types_lock);
2415 for (t = trace_types; t; t = t->next) {
2416 if (strcmp(t->name, buf) == 0)
2423 if (t == current_trace)
2426 if (current_trace && current_trace->reset)
2427 current_trace->reset(tr);
2434 mutex_unlock(&trace_types_lock);
2440 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2441 size_t cnt, loff_t *ppos)
2443 char buf[max_tracer_type_len+1];
2447 if (cnt > max_tracer_type_len)
2448 cnt = max_tracer_type_len;
2450 if (copy_from_user(&buf, ubuf, cnt))
2455 /* strip ending whitespace. */
2456 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2459 ret = tracing_set_tracer(buf);
2470 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2471 size_t cnt, loff_t *ppos)
2473 unsigned long *ptr = filp->private_data;
2477 r = snprintf(buf, sizeof(buf), "%ld\n",
2478 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2479 if (r > sizeof(buf))
2481 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2485 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2486 size_t cnt, loff_t *ppos)
2488 long *ptr = filp->private_data;
2493 if (cnt >= sizeof(buf))
2496 if (copy_from_user(&buf, ubuf, cnt))
2501 ret = strict_strtoul(buf, 10, &val);
2510 static atomic_t tracing_reader;
2512 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2514 struct trace_iterator *iter;
2516 if (tracing_disabled)
2519 /* We only allow for reader of the pipe */
2520 if (atomic_inc_return(&tracing_reader) != 1) {
2521 atomic_dec(&tracing_reader);
2525 /* create a buffer to store the information to pass to userspace */
2526 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2530 mutex_lock(&trace_types_lock);
2531 iter->tr = &global_trace;
2532 iter->trace = current_trace;
2533 filp->private_data = iter;
2535 if (iter->trace->pipe_open)
2536 iter->trace->pipe_open(iter);
2537 mutex_unlock(&trace_types_lock);
2542 static int tracing_release_pipe(struct inode *inode, struct file *file)
2544 struct trace_iterator *iter = file->private_data;
2547 atomic_dec(&tracing_reader);
2553 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2555 struct trace_iterator *iter = filp->private_data;
2557 if (trace_flags & TRACE_ITER_BLOCK) {
2559 * Always select as readable when in blocking mode
2561 return POLLIN | POLLRDNORM;
2563 if (!trace_empty(iter))
2564 return POLLIN | POLLRDNORM;
2565 poll_wait(filp, &trace_wait, poll_table);
2566 if (!trace_empty(iter))
2567 return POLLIN | POLLRDNORM;
2577 tracing_read_pipe(struct file *filp, char __user *ubuf,
2578 size_t cnt, loff_t *ppos)
2580 struct trace_iterator *iter = filp->private_data;
2583 /* return any leftover data */
2584 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2588 trace_seq_reset(&iter->seq);
2590 mutex_lock(&trace_types_lock);
2591 if (iter->trace->read) {
2592 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2599 while (trace_empty(iter)) {
2601 if ((filp->f_flags & O_NONBLOCK)) {
2607 * This is a make-shift waitqueue. The reason we don't use
2608 * an actual wait queue is because:
2609 * 1) we only ever have one waiter
2610 * 2) the tracing, traces all functions, we don't want
2611 * the overhead of calling wake_up and friends
2612 * (and tracing them too)
2613 * Anyway, this is really very primitive wakeup.
2615 set_current_state(TASK_INTERRUPTIBLE);
2616 iter->tr->waiter = current;
2618 mutex_unlock(&trace_types_lock);
2620 /* sleep for 100 msecs, and try again. */
2621 schedule_timeout(HZ/10);
2623 mutex_lock(&trace_types_lock);
2625 iter->tr->waiter = NULL;
2627 if (signal_pending(current)) {
2632 if (iter->trace != current_trace)
2636 * We block until we read something and tracing is disabled.
2637 * We still block if tracing is disabled, but we have never
2638 * read anything. This allows a user to cat this file, and
2639 * then enable tracing. But after we have read something,
2640 * we give an EOF when tracing is again disabled.
2642 * iter->pos will be 0 if we haven't read anything.
2644 if (!tracer_enabled && iter->pos)
2650 /* stop when tracing is finished */
2651 if (trace_empty(iter))
2654 if (cnt >= PAGE_SIZE)
2655 cnt = PAGE_SIZE - 1;
2657 /* reset all but tr, trace, and overruns */
2658 memset(&iter->seq, 0,
2659 sizeof(struct trace_iterator) -
2660 offsetof(struct trace_iterator, seq));
2663 while (find_next_entry_inc(iter) != NULL) {
2664 enum print_line_t ret;
2665 int len = iter->seq.len;
2667 ret = print_trace_line(iter);
2668 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2669 /* don't print partial lines */
2670 iter->seq.len = len;
2674 trace_consume(iter);
2676 if (iter->seq.len >= cnt)
2680 /* Now copy what we have to the user */
2681 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2682 if (iter->seq.readpos >= iter->seq.len)
2683 trace_seq_reset(&iter->seq);
2686 * If there was nothing to send to user, inspite of consuming trace
2687 * entries, go back to wait for more entries.
2693 mutex_unlock(&trace_types_lock);
2699 tracing_entries_read(struct file *filp, char __user *ubuf,
2700 size_t cnt, loff_t *ppos)
2702 struct trace_array *tr = filp->private_data;
2706 r = sprintf(buf, "%lu\n", tr->entries);
2707 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2711 tracing_entries_write(struct file *filp, const char __user *ubuf,
2712 size_t cnt, loff_t *ppos)
2717 struct trace_array *tr = filp->private_data;
2719 if (cnt >= sizeof(buf))
2722 if (copy_from_user(&buf, ubuf, cnt))
2727 ret = strict_strtoul(buf, 10, &val);
2731 /* must have at least 1 entry */
2735 mutex_lock(&trace_types_lock);
2739 pr_info("ftrace: please disable tracing"
2740 " before modifying buffer size\n");
2744 if (val != global_trace.entries) {
2745 ret = ring_buffer_resize(global_trace.buffer, val);
2751 ret = ring_buffer_resize(max_tr.buffer, val);
2755 r = ring_buffer_resize(global_trace.buffer,
2756 global_trace.entries);
2758 /* AARGH! We are left with different
2759 * size max buffer!!!! */
2761 tracing_disabled = 1;
2766 global_trace.entries = val;
2771 /* If check pages failed, return ENOMEM */
2772 if (tracing_disabled)
2775 max_tr.entries = global_trace.entries;
2776 mutex_unlock(&trace_types_lock);
2781 static int mark_printk(const char *fmt, ...)
2785 va_start(args, fmt);
2786 ret = trace_vprintk(0, fmt, args);
2792 tracing_mark_write(struct file *filp, const char __user *ubuf,
2793 size_t cnt, loff_t *fpos)
2797 struct trace_array *tr = &global_trace;
2799 if (!tr->ctrl || tracing_disabled)
2802 if (cnt > TRACE_BUF_SIZE)
2803 cnt = TRACE_BUF_SIZE;
2805 buf = kmalloc(cnt + 1, GFP_KERNEL);
2809 if (copy_from_user(buf, ubuf, cnt)) {
2814 /* Cut from the first nil or newline. */
2816 end = strchr(buf, '\n');
2820 cnt = mark_printk("%s\n", buf);
2827 static struct file_operations tracing_max_lat_fops = {
2828 .open = tracing_open_generic,
2829 .read = tracing_max_lat_read,
2830 .write = tracing_max_lat_write,
2833 static struct file_operations tracing_ctrl_fops = {
2834 .open = tracing_open_generic,
2835 .read = tracing_ctrl_read,
2836 .write = tracing_ctrl_write,
2839 static struct file_operations set_tracer_fops = {
2840 .open = tracing_open_generic,
2841 .read = tracing_set_trace_read,
2842 .write = tracing_set_trace_write,
2845 static struct file_operations tracing_pipe_fops = {
2846 .open = tracing_open_pipe,
2847 .poll = tracing_poll_pipe,
2848 .read = tracing_read_pipe,
2849 .release = tracing_release_pipe,
2852 static struct file_operations tracing_entries_fops = {
2853 .open = tracing_open_generic,
2854 .read = tracing_entries_read,
2855 .write = tracing_entries_write,
2858 static struct file_operations tracing_mark_fops = {
2859 .open = tracing_open_generic,
2860 .write = tracing_mark_write,
2863 #ifdef CONFIG_DYNAMIC_FTRACE
2865 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
2871 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
2872 size_t cnt, loff_t *ppos)
2874 static char ftrace_dyn_info_buffer[1024];
2875 static DEFINE_MUTEX(dyn_info_mutex);
2876 unsigned long *p = filp->private_data;
2877 char *buf = ftrace_dyn_info_buffer;
2878 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
2881 mutex_lock(&dyn_info_mutex);
2882 r = sprintf(buf, "%ld ", *p);
2884 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
2887 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2889 mutex_unlock(&dyn_info_mutex);
2894 static struct file_operations tracing_dyn_info_fops = {
2895 .open = tracing_open_generic,
2896 .read = tracing_read_dyn_info,
2900 static struct dentry *d_tracer;
2902 struct dentry *tracing_init_dentry(void)
2909 d_tracer = debugfs_create_dir("tracing", NULL);
2911 if (!d_tracer && !once) {
2913 pr_warning("Could not create debugfs directory 'tracing'\n");
2920 #ifdef CONFIG_FTRACE_SELFTEST
2921 /* Let selftest have access to static functions in this file */
2922 #include "trace_selftest.c"
2925 static __init int tracer_init_debugfs(void)
2927 struct dentry *d_tracer;
2928 struct dentry *entry;
2930 d_tracer = tracing_init_dentry();
2932 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2933 &global_trace, &tracing_ctrl_fops);
2935 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2937 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2938 NULL, &tracing_iter_fops);
2940 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2942 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2943 NULL, &tracing_cpumask_fops);
2945 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2947 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2948 &global_trace, &tracing_lt_fops);
2950 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2952 entry = debugfs_create_file("trace", 0444, d_tracer,
2953 &global_trace, &tracing_fops);
2955 pr_warning("Could not create debugfs 'trace' entry\n");
2957 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2958 &global_trace, &show_traces_fops);
2960 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2962 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2963 &global_trace, &set_tracer_fops);
2965 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2967 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2968 &tracing_max_latency,
2969 &tracing_max_lat_fops);
2971 pr_warning("Could not create debugfs "
2972 "'tracing_max_latency' entry\n");
2974 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2975 &tracing_thresh, &tracing_max_lat_fops);
2977 pr_warning("Could not create debugfs "
2978 "'tracing_thresh' entry\n");
2979 entry = debugfs_create_file("README", 0644, d_tracer,
2980 NULL, &tracing_readme_fops);
2982 pr_warning("Could not create debugfs 'README' entry\n");
2984 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2985 NULL, &tracing_pipe_fops);
2987 pr_warning("Could not create debugfs "
2988 "'trace_pipe' entry\n");
2990 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2991 &global_trace, &tracing_entries_fops);
2993 pr_warning("Could not create debugfs "
2994 "'trace_entries' entry\n");
2996 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2997 NULL, &tracing_mark_fops);
2999 pr_warning("Could not create debugfs "
3000 "'trace_marker' entry\n");
3002 #ifdef CONFIG_DYNAMIC_FTRACE
3003 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3004 &ftrace_update_tot_cnt,
3005 &tracing_dyn_info_fops);
3007 pr_warning("Could not create debugfs "
3008 "'dyn_ftrace_total_info' entry\n");
3010 #ifdef CONFIG_SYSPROF_TRACER
3011 init_tracer_sysprof_debugfs(d_tracer);
3016 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3018 static DEFINE_SPINLOCK(trace_buf_lock);
3019 static char trace_buf[TRACE_BUF_SIZE];
3021 struct ring_buffer_event *event;
3022 struct trace_array *tr = &global_trace;
3023 struct trace_array_cpu *data;
3024 struct print_entry *entry;
3025 unsigned long flags, irq_flags;
3026 int cpu, len = 0, size, pc;
3028 if (!tr->ctrl || tracing_disabled)
3031 pc = preempt_count();
3032 preempt_disable_notrace();
3033 cpu = raw_smp_processor_id();
3034 data = tr->data[cpu];
3036 if (unlikely(atomic_read(&data->disabled)))
3039 spin_lock_irqsave(&trace_buf_lock, flags);
3040 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3042 len = min(len, TRACE_BUF_SIZE-1);
3045 size = sizeof(*entry) + len + 1;
3046 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3049 entry = ring_buffer_event_data(event);
3050 tracing_generic_entry_update(&entry->ent, flags, pc);
3051 entry->ent.type = TRACE_PRINT;
3054 memcpy(&entry->buf, trace_buf, len);
3055 entry->buf[len] = 0;
3056 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3059 spin_unlock_irqrestore(&trace_buf_lock, flags);
3062 preempt_enable_notrace();
3066 EXPORT_SYMBOL_GPL(trace_vprintk);
3068 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3073 if (!(trace_flags & TRACE_ITER_PRINTK))
3077 ret = trace_vprintk(ip, fmt, ap);
3081 EXPORT_SYMBOL_GPL(__ftrace_printk);
3083 static int trace_panic_handler(struct notifier_block *this,
3084 unsigned long event, void *unused)
3086 if (ftrace_dump_on_oops)
3091 static struct notifier_block trace_panic_notifier = {
3092 .notifier_call = trace_panic_handler,
3094 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3097 static int trace_die_handler(struct notifier_block *self,
3103 if (ftrace_dump_on_oops)
3112 static struct notifier_block trace_die_notifier = {
3113 .notifier_call = trace_die_handler,
3118 * printk is set to max of 1024, we really don't need it that big.
3119 * Nothing should be printing 1000 characters anyway.
3121 #define TRACE_MAX_PRINT 1000
3124 * Define here KERN_TRACE so that we have one place to modify
3125 * it if we decide to change what log level the ftrace dump
3128 #define KERN_TRACE KERN_INFO
3131 trace_printk_seq(struct trace_seq *s)
3133 /* Probably should print a warning here. */
3137 /* should be zero ended, but we are paranoid. */
3138 s->buffer[s->len] = 0;
3140 printk(KERN_TRACE "%s", s->buffer);
3145 void ftrace_dump(void)
3147 static DEFINE_SPINLOCK(ftrace_dump_lock);
3148 /* use static because iter can be a bit big for the stack */
3149 static struct trace_iterator iter;
3150 static cpumask_t mask;
3151 static int dump_ran;
3152 unsigned long flags;
3156 spin_lock_irqsave(&ftrace_dump_lock, flags);
3162 /* No turning back! */
3165 for_each_tracing_cpu(cpu) {
3166 atomic_inc(&global_trace.data[cpu]->disabled);
3169 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3171 iter.tr = &global_trace;
3172 iter.trace = current_trace;
3175 * We need to stop all tracing on all CPUS to read the
3176 * the next buffer. This is a bit expensive, but is
3177 * not done often. We fill all what we can read,
3178 * and then release the locks again.
3183 while (!trace_empty(&iter)) {
3186 printk(KERN_TRACE "---------------------------------\n");
3190 /* reset all but tr, trace, and overruns */
3191 memset(&iter.seq, 0,
3192 sizeof(struct trace_iterator) -
3193 offsetof(struct trace_iterator, seq));
3194 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3197 if (find_next_entry_inc(&iter) != NULL) {
3198 print_trace_line(&iter);
3199 trace_consume(&iter);
3202 trace_printk_seq(&iter.seq);
3206 printk(KERN_TRACE " (ftrace buffer empty)\n");
3208 printk(KERN_TRACE "---------------------------------\n");
3211 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3214 __init static int tracer_alloc_buffers(void)
3216 struct trace_array_cpu *data;
3219 /* TODO: make the number of buffers hot pluggable with CPUS */
3220 tracing_buffer_mask = cpu_possible_map;
3222 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3223 TRACE_BUFFER_FLAGS);
3224 if (!global_trace.buffer) {
3225 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3229 global_trace.entries = ring_buffer_size(global_trace.buffer);
3231 #ifdef CONFIG_TRACER_MAX_TRACE
3232 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3233 TRACE_BUFFER_FLAGS);
3234 if (!max_tr.buffer) {
3235 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3237 ring_buffer_free(global_trace.buffer);
3240 max_tr.entries = ring_buffer_size(max_tr.buffer);
3241 WARN_ON(max_tr.entries != global_trace.entries);
3244 /* Allocate the first page for all buffers */
3245 for_each_tracing_cpu(i) {
3246 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3247 max_tr.data[i] = &per_cpu(max_data, i);
3250 trace_init_cmdlines();
3252 register_tracer(&nop_trace);
3253 #ifdef CONFIG_BOOT_TRACER
3254 register_tracer(&boot_tracer);
3255 current_trace = &boot_tracer;
3256 current_trace->init(&global_trace);
3258 current_trace = &nop_trace;
3261 /* All seems OK, enable tracing */
3262 global_trace.ctrl = tracer_enabled;
3263 tracing_disabled = 0;
3265 atomic_notifier_chain_register(&panic_notifier_list,
3266 &trace_panic_notifier);
3268 register_die_notifier(&trace_die_notifier);
3272 early_initcall(tracer_alloc_buffers);
3273 fs_initcall(tracer_init_debugfs);