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/seq_file.h>
34 #include <linux/writeback.h>
36 #include <linux/stacktrace.h>
37 #include <linux/ring_buffer.h>
38 #include <linux/irqflags.h>
42 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
44 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
45 unsigned long __read_mostly tracing_thresh;
47 /* For tracers that don't implement custom flags */
48 static struct tracer_opt dummy_tracer_opt[] = {
52 static struct tracer_flags dummy_tracer_flags = {
54 .opts = dummy_tracer_opt
57 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
63 * Kill all tracing for good (never come back).
64 * It is initialized to 1 but will turn to zero if the initialization
65 * of the tracer is successful. But that is the only place that sets
68 int tracing_disabled = 1;
70 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
72 static inline void ftrace_disable_cpu(void)
75 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
78 static inline void ftrace_enable_cpu(void)
80 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
84 static cpumask_t __read_mostly tracing_buffer_mask;
86 #define for_each_tracing_cpu(cpu) \
87 for_each_cpu_mask(cpu, tracing_buffer_mask)
90 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
92 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
93 * is set, then ftrace_dump is called. This will output the contents
94 * of the ftrace buffers to the console. This is very useful for
95 * capturing traces that lead to crashes and outputing it to a
98 * It is default off, but you can enable it with either specifying
99 * "ftrace_dump_on_oops" in the kernel command line, or setting
100 * /proc/sys/kernel/ftrace_dump_on_oops to true.
102 int ftrace_dump_on_oops;
104 static int tracing_set_tracer(char *buf);
106 static int __init set_ftrace(char *str)
108 tracing_set_tracer(str);
111 __setup("ftrace", set_ftrace);
113 static int __init set_ftrace_dump_on_oops(char *str)
115 ftrace_dump_on_oops = 1;
118 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
121 ns2usecs(cycle_t nsec)
128 cycle_t ftrace_now(int cpu)
130 u64 ts = ring_buffer_time_stamp(cpu);
131 ring_buffer_normalize_time_stamp(cpu, &ts);
136 * The global_trace is the descriptor that holds the tracing
137 * buffers for the live tracing. For each CPU, it contains
138 * a link list of pages that will store trace entries. The
139 * page descriptor of the pages in the memory is used to hold
140 * the link list by linking the lru item in the page descriptor
141 * to each of the pages in the buffer per CPU.
143 * For each active CPU there is a data field that holds the
144 * pages for the buffer for that CPU. Each CPU has the same number
145 * of pages allocated for its buffer.
147 static struct trace_array global_trace;
149 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
152 * The max_tr is used to snapshot the global_trace when a maximum
153 * latency is reached. Some tracers will use this to store a maximum
154 * trace while it continues examining live traces.
156 * The buffers for the max_tr are set up the same as the global_trace.
157 * When a snapshot is taken, the link list of the max_tr is swapped
158 * with the link list of the global_trace and the buffers are reset for
159 * the global_trace so the tracing can continue.
161 static struct trace_array max_tr;
163 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
165 /* tracer_enabled is used to toggle activation of a tracer */
166 static int tracer_enabled = 1;
169 * tracing_is_enabled - return tracer_enabled status
171 * This function is used by other tracers to know the status
172 * of the tracer_enabled flag. Tracers may use this function
173 * to know if it should enable their features when starting
174 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
176 int tracing_is_enabled(void)
178 return tracer_enabled;
181 /* function tracing enabled */
182 int ftrace_function_enabled;
185 * trace_buf_size is the size in bytes that is allocated
186 * for a buffer. Note, the number of bytes is always rounded
189 * This number is purposely set to a low number of 16384.
190 * If the dump on oops happens, it will be much appreciated
191 * to not have to wait for all that output. Anyway this can be
192 * boot time and run time configurable.
194 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
196 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
198 /* trace_types holds a link list of available tracers. */
199 static struct tracer *trace_types __read_mostly;
201 /* current_trace points to the tracer that is currently active */
202 static struct tracer *current_trace __read_mostly;
205 * max_tracer_type_len is used to simplify the allocating of
206 * buffers to read userspace tracer names. We keep track of
207 * the longest tracer name registered.
209 static int max_tracer_type_len;
212 * trace_types_lock is used to protect the trace_types list.
213 * This lock is also used to keep user access serialized.
214 * Accesses from userspace will grab this lock while userspace
215 * activities happen inside the kernel.
217 static DEFINE_MUTEX(trace_types_lock);
219 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
220 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
222 /* trace_flags holds trace_options default values */
223 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
227 * trace_wake_up - wake up tasks waiting for trace input
229 * Simply wakes up any task that is blocked on the trace_wait
230 * queue. These is used with trace_poll for tasks polling the trace.
232 void trace_wake_up(void)
235 * The runqueue_is_locked() can fail, but this is the best we
238 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
239 wake_up(&trace_wait);
242 static int __init set_buf_size(char *str)
244 unsigned long buf_size;
249 ret = strict_strtoul(str, 0, &buf_size);
250 /* nr_entries can not be zero */
251 if (ret < 0 || buf_size == 0)
253 trace_buf_size = buf_size;
256 __setup("trace_buf_size=", set_buf_size);
258 unsigned long nsecs_to_usecs(unsigned long nsecs)
263 /* These must match the bit postions in trace_iterator_flags */
264 static const char *trace_options[] = {
285 * ftrace_max_lock is used to protect the swapping of buffers
286 * when taking a max snapshot. The buffers themselves are
287 * protected by per_cpu spinlocks. But the action of the swap
288 * needs its own lock.
290 * This is defined as a raw_spinlock_t in order to help
291 * with performance when lockdep debugging is enabled.
293 static raw_spinlock_t ftrace_max_lock =
294 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
297 * Copy the new maximum trace into the separate maximum-trace
298 * structure. (this way the maximum trace is permanently saved,
299 * for later retrieval via /debugfs/tracing/latency_trace)
302 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
304 struct trace_array_cpu *data = tr->data[cpu];
307 max_tr.time_start = data->preempt_timestamp;
309 data = max_tr.data[cpu];
310 data->saved_latency = tracing_max_latency;
312 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
313 data->pid = tsk->pid;
314 data->uid = tsk->uid;
315 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
316 data->policy = tsk->policy;
317 data->rt_priority = tsk->rt_priority;
319 /* record this tasks comm */
320 tracing_record_cmdline(current);
324 * trace_seq_printf - sequence printing of trace information
325 * @s: trace sequence descriptor
326 * @fmt: printf format string
328 * The tracer may use either sequence operations or its own
329 * copy to user routines. To simplify formating of a trace
330 * trace_seq_printf is used to store strings into a special
331 * buffer (@s). Then the output may be either used by
332 * the sequencer or pulled into another buffer.
335 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
337 int len = (PAGE_SIZE - 1) - s->len;
345 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
348 /* If we can't write it all, don't bother writing anything */
358 * trace_seq_puts - trace sequence printing of simple string
359 * @s: trace sequence descriptor
360 * @str: simple string to record
362 * The tracer may use either the sequence operations or its own
363 * copy to user routines. This function records a simple string
364 * into a special buffer (@s) for later retrieval by a sequencer
365 * or other mechanism.
368 trace_seq_puts(struct trace_seq *s, const char *str)
370 int len = strlen(str);
372 if (len > ((PAGE_SIZE - 1) - s->len))
375 memcpy(s->buffer + s->len, str, len);
382 trace_seq_putc(struct trace_seq *s, unsigned char c)
384 if (s->len >= (PAGE_SIZE - 1))
387 s->buffer[s->len++] = c;
393 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
395 if (len > ((PAGE_SIZE - 1) - s->len))
398 memcpy(s->buffer + s->len, mem, len);
404 #define MAX_MEMHEX_BYTES 8
405 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
408 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
410 unsigned char hex[HEX_CHARS];
411 unsigned char *data = mem;
415 for (i = 0, j = 0; i < len; i++) {
417 for (i = len-1, j = 0; i >= 0; i--) {
419 hex[j++] = hex_asc_hi(data[i]);
420 hex[j++] = hex_asc_lo(data[i]);
424 return trace_seq_putmem(s, hex, j);
428 trace_seq_path(struct trace_seq *s, struct path *path)
432 if (s->len >= (PAGE_SIZE - 1))
434 p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
436 p = mangle_path(s->buffer + s->len, p, "\n");
438 s->len = p - s->buffer;
442 s->buffer[s->len++] = '?';
450 trace_seq_reset(struct trace_seq *s)
456 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
461 if (s->len <= s->readpos)
464 len = s->len - s->readpos;
467 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
476 trace_print_seq(struct seq_file *m, struct trace_seq *s)
478 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
481 seq_puts(m, s->buffer);
487 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
489 * @tsk: the task with the latency
490 * @cpu: The cpu that initiated the trace.
492 * Flip the buffers between the @tr and the max_tr and record information
493 * about which task was the cause of this latency.
496 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
498 struct ring_buffer *buf = tr->buffer;
500 WARN_ON_ONCE(!irqs_disabled());
501 __raw_spin_lock(&ftrace_max_lock);
503 tr->buffer = max_tr.buffer;
506 ftrace_disable_cpu();
507 ring_buffer_reset(tr->buffer);
510 __update_max_tr(tr, tsk, cpu);
511 __raw_spin_unlock(&ftrace_max_lock);
515 * update_max_tr_single - only copy one trace over, and reset the rest
517 * @tsk - task with the latency
518 * @cpu - the cpu of the buffer to copy.
520 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
523 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
527 WARN_ON_ONCE(!irqs_disabled());
528 __raw_spin_lock(&ftrace_max_lock);
530 ftrace_disable_cpu();
532 ring_buffer_reset(max_tr.buffer);
533 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
539 __update_max_tr(tr, tsk, cpu);
540 __raw_spin_unlock(&ftrace_max_lock);
544 * register_tracer - register a tracer with the ftrace system.
545 * @type - the plugin for the tracer
547 * Register a new plugin tracer.
549 int register_tracer(struct tracer *type)
556 pr_info("Tracer must have a name\n");
561 * When this gets called we hold the BKL which means that
562 * preemption is disabled. Various trace selftests however
563 * need to disable and enable preemption for successful tests.
564 * So we drop the BKL here and grab it after the tests again.
567 mutex_lock(&trace_types_lock);
569 for (t = trace_types; t; t = t->next) {
570 if (strcmp(type->name, t->name) == 0) {
572 pr_info("Trace %s already registered\n",
580 type->set_flag = &dummy_set_flag;
582 type->flags = &dummy_tracer_flags;
584 if (!type->flags->opts)
585 type->flags->opts = dummy_tracer_opt;
587 #ifdef CONFIG_FTRACE_STARTUP_TEST
588 if (type->selftest) {
589 struct tracer *saved_tracer = current_trace;
590 struct trace_array *tr = &global_trace;
593 * Run a selftest on this tracer.
594 * Here we reset the trace buffer, and set the current
595 * tracer to be this tracer. The tracer can then run some
596 * internal tracing to verify that everything is in order.
597 * If we fail, we do not register this tracer.
599 for_each_tracing_cpu(i)
600 tracing_reset(tr, i);
602 current_trace = type;
603 /* the test is responsible for initializing and enabling */
604 pr_info("Testing tracer %s: ", type->name);
605 ret = type->selftest(type, tr);
606 /* the test is responsible for resetting too */
607 current_trace = saved_tracer;
609 printk(KERN_CONT "FAILED!\n");
612 /* Only reset on passing, to avoid touching corrupted buffers */
613 for_each_tracing_cpu(i)
614 tracing_reset(tr, i);
616 printk(KERN_CONT "PASSED\n");
620 type->next = trace_types;
622 len = strlen(type->name);
623 if (len > max_tracer_type_len)
624 max_tracer_type_len = len;
627 mutex_unlock(&trace_types_lock);
633 void unregister_tracer(struct tracer *type)
638 mutex_lock(&trace_types_lock);
639 for (t = &trace_types; *t; t = &(*t)->next) {
643 pr_info("Trace %s not registered\n", type->name);
648 if (strlen(type->name) != max_tracer_type_len)
651 max_tracer_type_len = 0;
652 for (t = &trace_types; *t; t = &(*t)->next) {
653 len = strlen((*t)->name);
654 if (len > max_tracer_type_len)
655 max_tracer_type_len = len;
658 mutex_unlock(&trace_types_lock);
661 void tracing_reset(struct trace_array *tr, int cpu)
663 ftrace_disable_cpu();
664 ring_buffer_reset_cpu(tr->buffer, cpu);
668 #define SAVED_CMDLINES 128
669 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
670 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
671 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
672 static int cmdline_idx;
673 static DEFINE_SPINLOCK(trace_cmdline_lock);
675 /* temporary disable recording */
676 atomic_t trace_record_cmdline_disabled __read_mostly;
678 static void trace_init_cmdlines(void)
680 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
681 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
685 static int trace_stop_count;
686 static DEFINE_SPINLOCK(tracing_start_lock);
689 * tracing_start - quick start of the tracer
691 * If tracing is enabled but was stopped by tracing_stop,
692 * this will start the tracer back up.
694 void tracing_start(void)
696 struct ring_buffer *buffer;
699 if (tracing_disabled)
702 spin_lock_irqsave(&tracing_start_lock, flags);
703 if (--trace_stop_count)
706 if (trace_stop_count < 0) {
707 /* Someone screwed up their debugging */
709 trace_stop_count = 0;
714 buffer = global_trace.buffer;
716 ring_buffer_record_enable(buffer);
718 buffer = max_tr.buffer;
720 ring_buffer_record_enable(buffer);
724 spin_unlock_irqrestore(&tracing_start_lock, flags);
728 * tracing_stop - quick stop of the tracer
730 * Light weight way to stop tracing. Use in conjunction with
733 void tracing_stop(void)
735 struct ring_buffer *buffer;
739 spin_lock_irqsave(&tracing_start_lock, flags);
740 if (trace_stop_count++)
743 buffer = global_trace.buffer;
745 ring_buffer_record_disable(buffer);
747 buffer = max_tr.buffer;
749 ring_buffer_record_disable(buffer);
752 spin_unlock_irqrestore(&tracing_start_lock, flags);
755 void trace_stop_cmdline_recording(void);
757 static void trace_save_cmdline(struct task_struct *tsk)
762 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
766 * It's not the end of the world if we don't get
767 * the lock, but we also don't want to spin
768 * nor do we want to disable interrupts,
769 * so if we miss here, then better luck next time.
771 if (!spin_trylock(&trace_cmdline_lock))
774 idx = map_pid_to_cmdline[tsk->pid];
775 if (idx >= SAVED_CMDLINES) {
776 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
778 map = map_cmdline_to_pid[idx];
779 if (map <= PID_MAX_DEFAULT)
780 map_pid_to_cmdline[map] = (unsigned)-1;
782 map_pid_to_cmdline[tsk->pid] = idx;
787 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
789 spin_unlock(&trace_cmdline_lock);
792 static char *trace_find_cmdline(int pid)
794 char *cmdline = "<...>";
800 if (pid > PID_MAX_DEFAULT)
803 map = map_pid_to_cmdline[pid];
804 if (map >= SAVED_CMDLINES)
807 cmdline = saved_cmdlines[map];
813 void tracing_record_cmdline(struct task_struct *tsk)
815 if (atomic_read(&trace_record_cmdline_disabled))
818 trace_save_cmdline(tsk);
822 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
825 struct task_struct *tsk = current;
827 entry->preempt_count = pc & 0xff;
828 entry->pid = (tsk) ? tsk->pid : 0;
829 entry->tgid = (tsk) ? tsk->tgid : 0;
831 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
832 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
834 TRACE_FLAG_IRQS_NOSUPPORT |
836 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
837 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
838 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
842 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
843 unsigned long ip, unsigned long parent_ip, unsigned long flags,
846 struct ring_buffer_event *event;
847 struct ftrace_entry *entry;
848 unsigned long irq_flags;
850 /* If we are reading the ring buffer, don't trace */
851 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
854 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
858 entry = ring_buffer_event_data(event);
859 tracing_generic_entry_update(&entry->ent, flags, pc);
860 entry->ent.type = TRACE_FN;
862 entry->parent_ip = parent_ip;
863 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
866 #ifdef CONFIG_FUNCTION_RET_TRACER
867 static void __trace_function_return(struct trace_array *tr,
868 struct trace_array_cpu *data,
869 struct ftrace_retfunc *trace,
873 struct ring_buffer_event *event;
874 struct ftrace_ret_entry *entry;
875 unsigned long irq_flags;
877 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
880 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
884 entry = ring_buffer_event_data(event);
885 tracing_generic_entry_update(&entry->ent, flags, pc);
886 entry->ent.type = TRACE_FN_RET;
887 entry->ip = trace->func;
888 entry->parent_ip = trace->ret;
889 entry->rettime = trace->rettime;
890 entry->calltime = trace->calltime;
891 entry->overrun = trace->overrun;
892 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
897 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
898 unsigned long ip, unsigned long parent_ip, unsigned long flags,
901 if (likely(!atomic_read(&data->disabled)))
902 trace_function(tr, data, ip, parent_ip, flags, pc);
905 static void ftrace_trace_stack(struct trace_array *tr,
906 struct trace_array_cpu *data,
910 #ifdef CONFIG_STACKTRACE
911 struct ring_buffer_event *event;
912 struct stack_entry *entry;
913 struct stack_trace trace;
914 unsigned long irq_flags;
916 if (!(trace_flags & TRACE_ITER_STACKTRACE))
919 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
923 entry = ring_buffer_event_data(event);
924 tracing_generic_entry_update(&entry->ent, flags, pc);
925 entry->ent.type = TRACE_STACK;
927 memset(&entry->caller, 0, sizeof(entry->caller));
929 trace.nr_entries = 0;
930 trace.max_entries = FTRACE_STACK_ENTRIES;
932 trace.entries = entry->caller;
934 save_stack_trace(&trace);
935 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
939 void __trace_stack(struct trace_array *tr,
940 struct trace_array_cpu *data,
944 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
947 static void ftrace_trace_userstack(struct trace_array *tr,
948 struct trace_array_cpu *data,
949 unsigned long flags, int pc)
951 struct ring_buffer_event *event;
952 struct userstack_entry *entry;
953 struct stack_trace trace;
954 unsigned long irq_flags;
956 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
959 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
963 entry = ring_buffer_event_data(event);
964 tracing_generic_entry_update(&entry->ent, flags, pc);
965 entry->ent.type = TRACE_USER_STACK;
967 memset(&entry->caller, 0, sizeof(entry->caller));
969 trace.nr_entries = 0;
970 trace.max_entries = FTRACE_STACK_ENTRIES;
972 trace.entries = entry->caller;
974 save_stack_trace_user(&trace);
975 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
978 void __trace_userstack(struct trace_array *tr,
979 struct trace_array_cpu *data,
982 ftrace_trace_userstack(tr, data, flags, preempt_count());
986 ftrace_trace_special(void *__tr, void *__data,
987 unsigned long arg1, unsigned long arg2, unsigned long arg3,
990 struct ring_buffer_event *event;
991 struct trace_array_cpu *data = __data;
992 struct trace_array *tr = __tr;
993 struct special_entry *entry;
994 unsigned long irq_flags;
996 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1000 entry = ring_buffer_event_data(event);
1001 tracing_generic_entry_update(&entry->ent, 0, pc);
1002 entry->ent.type = TRACE_SPECIAL;
1006 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1007 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
1008 ftrace_trace_userstack(tr, data, irq_flags, pc);
1014 __trace_special(void *__tr, void *__data,
1015 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1017 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
1021 tracing_sched_switch_trace(struct trace_array *tr,
1022 struct trace_array_cpu *data,
1023 struct task_struct *prev,
1024 struct task_struct *next,
1025 unsigned long flags, int pc)
1027 struct ring_buffer_event *event;
1028 struct ctx_switch_entry *entry;
1029 unsigned long irq_flags;
1031 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1035 entry = ring_buffer_event_data(event);
1036 tracing_generic_entry_update(&entry->ent, flags, pc);
1037 entry->ent.type = TRACE_CTX;
1038 entry->prev_pid = prev->pid;
1039 entry->prev_prio = prev->prio;
1040 entry->prev_state = prev->state;
1041 entry->next_pid = next->pid;
1042 entry->next_prio = next->prio;
1043 entry->next_state = next->state;
1044 entry->next_cpu = task_cpu(next);
1045 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1046 ftrace_trace_stack(tr, data, flags, 5, pc);
1047 ftrace_trace_userstack(tr, data, flags, pc);
1051 tracing_sched_wakeup_trace(struct trace_array *tr,
1052 struct trace_array_cpu *data,
1053 struct task_struct *wakee,
1054 struct task_struct *curr,
1055 unsigned long flags, int pc)
1057 struct ring_buffer_event *event;
1058 struct ctx_switch_entry *entry;
1059 unsigned long irq_flags;
1061 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1065 entry = ring_buffer_event_data(event);
1066 tracing_generic_entry_update(&entry->ent, flags, pc);
1067 entry->ent.type = TRACE_WAKE;
1068 entry->prev_pid = curr->pid;
1069 entry->prev_prio = curr->prio;
1070 entry->prev_state = curr->state;
1071 entry->next_pid = wakee->pid;
1072 entry->next_prio = wakee->prio;
1073 entry->next_state = wakee->state;
1074 entry->next_cpu = task_cpu(wakee);
1075 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1076 ftrace_trace_stack(tr, data, flags, 6, pc);
1077 ftrace_trace_userstack(tr, data, flags, pc);
1083 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1085 struct trace_array *tr = &global_trace;
1086 struct trace_array_cpu *data;
1087 unsigned long flags;
1091 if (tracing_disabled)
1094 pc = preempt_count();
1095 local_irq_save(flags);
1096 cpu = raw_smp_processor_id();
1097 data = tr->data[cpu];
1099 if (likely(atomic_inc_return(&data->disabled) == 1))
1100 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1102 atomic_dec(&data->disabled);
1103 local_irq_restore(flags);
1106 #ifdef CONFIG_FUNCTION_TRACER
1108 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
1110 struct trace_array *tr = &global_trace;
1111 struct trace_array_cpu *data;
1112 unsigned long flags;
1117 if (unlikely(!ftrace_function_enabled))
1120 pc = preempt_count();
1121 resched = ftrace_preempt_disable();
1122 local_save_flags(flags);
1123 cpu = raw_smp_processor_id();
1124 data = tr->data[cpu];
1125 disabled = atomic_inc_return(&data->disabled);
1127 if (likely(disabled == 1))
1128 trace_function(tr, data, ip, parent_ip, flags, pc);
1130 atomic_dec(&data->disabled);
1131 ftrace_preempt_enable(resched);
1135 function_trace_call(unsigned long ip, unsigned long parent_ip)
1137 struct trace_array *tr = &global_trace;
1138 struct trace_array_cpu *data;
1139 unsigned long flags;
1144 if (unlikely(!ftrace_function_enabled))
1148 * Need to use raw, since this must be called before the
1149 * recursive protection is performed.
1151 local_irq_save(flags);
1152 cpu = raw_smp_processor_id();
1153 data = tr->data[cpu];
1154 disabled = atomic_inc_return(&data->disabled);
1156 if (likely(disabled == 1)) {
1157 pc = preempt_count();
1158 trace_function(tr, data, ip, parent_ip, flags, pc);
1161 atomic_dec(&data->disabled);
1162 local_irq_restore(flags);
1165 #ifdef CONFIG_FUNCTION_RET_TRACER
1166 void trace_function_return(struct ftrace_retfunc *trace)
1168 struct trace_array *tr = &global_trace;
1169 struct trace_array_cpu *data;
1170 unsigned long flags;
1175 raw_local_irq_save(flags);
1176 cpu = raw_smp_processor_id();
1177 data = tr->data[cpu];
1178 disabled = atomic_inc_return(&data->disabled);
1179 if (likely(disabled == 1)) {
1180 pc = preempt_count();
1181 __trace_function_return(tr, data, trace, flags, pc);
1183 atomic_dec(&data->disabled);
1184 raw_local_irq_restore(flags);
1186 #endif /* CONFIG_FUNCTION_RET_TRACER */
1188 static struct ftrace_ops trace_ops __read_mostly =
1190 .func = function_trace_call,
1193 void tracing_start_function_trace(void)
1195 ftrace_function_enabled = 0;
1197 if (trace_flags & TRACE_ITER_PREEMPTONLY)
1198 trace_ops.func = function_trace_call_preempt_only;
1200 trace_ops.func = function_trace_call;
1202 register_ftrace_function(&trace_ops);
1203 ftrace_function_enabled = 1;
1206 void tracing_stop_function_trace(void)
1208 ftrace_function_enabled = 0;
1209 unregister_ftrace_function(&trace_ops);
1213 enum trace_file_type {
1214 TRACE_FILE_LAT_FMT = 1,
1215 TRACE_FILE_ANNOTATE = 2,
1218 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1220 /* Don't allow ftrace to trace into the ring buffers */
1221 ftrace_disable_cpu();
1224 if (iter->buffer_iter[iter->cpu])
1225 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1227 ftrace_enable_cpu();
1230 static struct trace_entry *
1231 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1233 struct ring_buffer_event *event;
1234 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1236 /* Don't allow ftrace to trace into the ring buffers */
1237 ftrace_disable_cpu();
1240 event = ring_buffer_iter_peek(buf_iter, ts);
1242 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1244 ftrace_enable_cpu();
1246 return event ? ring_buffer_event_data(event) : NULL;
1249 static struct trace_entry *
1250 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1252 struct ring_buffer *buffer = iter->tr->buffer;
1253 struct trace_entry *ent, *next = NULL;
1254 u64 next_ts = 0, ts;
1258 for_each_tracing_cpu(cpu) {
1260 if (ring_buffer_empty_cpu(buffer, cpu))
1263 ent = peek_next_entry(iter, cpu, &ts);
1266 * Pick the entry with the smallest timestamp:
1268 if (ent && (!next || ts < next_ts)) {
1276 *ent_cpu = next_cpu;
1284 /* Find the next real entry, without updating the iterator itself */
1285 static struct trace_entry *
1286 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1288 return __find_next_entry(iter, ent_cpu, ent_ts);
1291 /* Find the next real entry, and increment the iterator to the next entry */
1292 static void *find_next_entry_inc(struct trace_iterator *iter)
1294 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1297 trace_iterator_increment(iter, iter->cpu);
1299 return iter->ent ? iter : NULL;
1302 static void trace_consume(struct trace_iterator *iter)
1304 /* Don't allow ftrace to trace into the ring buffers */
1305 ftrace_disable_cpu();
1306 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1307 ftrace_enable_cpu();
1310 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1312 struct trace_iterator *iter = m->private;
1318 /* can't go backwards */
1323 ent = find_next_entry_inc(iter);
1327 while (ent && iter->idx < i)
1328 ent = find_next_entry_inc(iter);
1335 static void *s_start(struct seq_file *m, loff_t *pos)
1337 struct trace_iterator *iter = m->private;
1342 mutex_lock(&trace_types_lock);
1344 if (!current_trace || current_trace != iter->trace) {
1345 mutex_unlock(&trace_types_lock);
1349 atomic_inc(&trace_record_cmdline_disabled);
1351 if (*pos != iter->pos) {
1356 ftrace_disable_cpu();
1358 for_each_tracing_cpu(cpu) {
1359 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1362 ftrace_enable_cpu();
1364 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1369 p = s_next(m, p, &l);
1375 static void s_stop(struct seq_file *m, void *p)
1377 atomic_dec(&trace_record_cmdline_disabled);
1378 mutex_unlock(&trace_types_lock);
1381 #ifdef CONFIG_KRETPROBES
1382 static inline const char *kretprobed(const char *name)
1384 static const char tramp_name[] = "kretprobe_trampoline";
1385 int size = sizeof(tramp_name);
1387 if (strncmp(tramp_name, name, size) == 0)
1388 return "[unknown/kretprobe'd]";
1392 static inline const char *kretprobed(const char *name)
1396 #endif /* CONFIG_KRETPROBES */
1399 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1401 #ifdef CONFIG_KALLSYMS
1402 char str[KSYM_SYMBOL_LEN];
1405 kallsyms_lookup(address, NULL, NULL, NULL, str);
1407 name = kretprobed(str);
1409 return trace_seq_printf(s, fmt, name);
1415 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1416 unsigned long address)
1418 #ifdef CONFIG_KALLSYMS
1419 char str[KSYM_SYMBOL_LEN];
1422 sprint_symbol(str, address);
1423 name = kretprobed(str);
1425 return trace_seq_printf(s, fmt, name);
1430 #ifndef CONFIG_64BIT
1431 # define IP_FMT "%08lx"
1433 # define IP_FMT "%016lx"
1437 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1442 return trace_seq_printf(s, "0");
1444 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1445 ret = seq_print_sym_offset(s, "%s", ip);
1447 ret = seq_print_sym_short(s, "%s", ip);
1452 if (sym_flags & TRACE_ITER_SYM_ADDR)
1453 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1457 static inline int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
1458 unsigned long ip, unsigned long sym_flags)
1460 struct file *file = NULL;
1461 unsigned long vmstart = 0;
1465 const struct vm_area_struct *vma;
1467 down_read(&mm->mmap_sem);
1468 vma = find_vma(mm, ip);
1470 file = vma->vm_file;
1471 vmstart = vma->vm_start;
1474 ret = trace_seq_path(s, &file->f_path);
1476 ret = trace_seq_printf(s, "[+0x%lx]", ip - vmstart);
1478 up_read(&mm->mmap_sem);
1480 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
1481 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1486 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
1487 unsigned long sym_flags)
1489 struct mm_struct *mm = NULL;
1493 if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
1494 struct task_struct *task;
1496 * we do the lookup on the thread group leader,
1497 * since individual threads might have already quit!
1500 task = find_task_by_vpid(entry->ent.tgid);
1502 mm = get_task_mm(task);
1506 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1507 unsigned long ip = entry->caller[i];
1509 if (ip == ULONG_MAX || !ret)
1512 ret = trace_seq_puts(s, " <- ");
1515 ret = trace_seq_puts(s, "??");
1521 ret = seq_print_user_ip(s, mm, ip, sym_flags);
1529 static void print_lat_help_header(struct seq_file *m)
1531 seq_puts(m, "# _------=> CPU# \n");
1532 seq_puts(m, "# / _-----=> irqs-off \n");
1533 seq_puts(m, "# | / _----=> need-resched \n");
1534 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1535 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1536 seq_puts(m, "# |||| / \n");
1537 seq_puts(m, "# ||||| delay \n");
1538 seq_puts(m, "# cmd pid ||||| time | caller \n");
1539 seq_puts(m, "# \\ / ||||| \\ | / \n");
1542 static void print_func_help_header(struct seq_file *m)
1544 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1545 seq_puts(m, "# | | | | |\n");
1550 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1552 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1553 struct trace_array *tr = iter->tr;
1554 struct trace_array_cpu *data = tr->data[tr->cpu];
1555 struct tracer *type = current_trace;
1556 unsigned long total;
1557 unsigned long entries;
1558 const char *name = "preemption";
1563 entries = ring_buffer_entries(iter->tr->buffer);
1565 ring_buffer_overruns(iter->tr->buffer);
1567 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1569 seq_puts(m, "-----------------------------------"
1570 "---------------------------------\n");
1571 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1572 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1573 nsecs_to_usecs(data->saved_latency),
1577 #if defined(CONFIG_PREEMPT_NONE)
1579 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1581 #elif defined(CONFIG_PREEMPT)
1586 /* These are reserved for later use */
1589 seq_printf(m, " #P:%d)\n", num_online_cpus());
1593 seq_puts(m, " -----------------\n");
1594 seq_printf(m, " | task: %.16s-%d "
1595 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1596 data->comm, data->pid, data->uid, data->nice,
1597 data->policy, data->rt_priority);
1598 seq_puts(m, " -----------------\n");
1600 if (data->critical_start) {
1601 seq_puts(m, " => started at: ");
1602 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1603 trace_print_seq(m, &iter->seq);
1604 seq_puts(m, "\n => ended at: ");
1605 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1606 trace_print_seq(m, &iter->seq);
1614 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1616 int hardirq, softirq;
1619 comm = trace_find_cmdline(entry->pid);
1621 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1622 trace_seq_printf(s, "%3d", cpu);
1623 trace_seq_printf(s, "%c%c",
1624 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1625 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1626 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1628 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1629 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1630 if (hardirq && softirq) {
1631 trace_seq_putc(s, 'H');
1634 trace_seq_putc(s, 'h');
1637 trace_seq_putc(s, 's');
1639 trace_seq_putc(s, '.');
1643 if (entry->preempt_count)
1644 trace_seq_printf(s, "%x", entry->preempt_count);
1646 trace_seq_puts(s, ".");
1649 unsigned long preempt_mark_thresh = 100;
1652 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1653 unsigned long rel_usecs)
1655 trace_seq_printf(s, " %4lldus", abs_usecs);
1656 if (rel_usecs > preempt_mark_thresh)
1657 trace_seq_puts(s, "!: ");
1658 else if (rel_usecs > 1)
1659 trace_seq_puts(s, "+: ");
1661 trace_seq_puts(s, " : ");
1664 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1667 * The message is supposed to contain an ending newline.
1668 * If the printing stops prematurely, try to add a newline of our own.
1670 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1672 struct trace_entry *ent;
1673 struct trace_field_cont *cont;
1676 ent = peek_next_entry(iter, iter->cpu, NULL);
1677 if (!ent || ent->type != TRACE_CONT) {
1678 trace_seq_putc(s, '\n');
1683 cont = (struct trace_field_cont *)ent;
1685 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1687 ftrace_disable_cpu();
1689 if (iter->buffer_iter[iter->cpu])
1690 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1692 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1694 ftrace_enable_cpu();
1696 ent = peek_next_entry(iter, iter->cpu, NULL);
1697 } while (ent && ent->type == TRACE_CONT);
1700 trace_seq_putc(s, '\n');
1703 static void test_cpu_buff_start(struct trace_iterator *iter)
1705 struct trace_seq *s = &iter->seq;
1707 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1710 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1713 if (cpu_isset(iter->cpu, iter->started))
1716 cpu_set(iter->cpu, iter->started);
1717 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1720 static enum print_line_t
1721 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1723 struct trace_seq *s = &iter->seq;
1724 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1725 struct trace_entry *next_entry;
1726 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1727 struct trace_entry *entry = iter->ent;
1728 unsigned long abs_usecs;
1729 unsigned long rel_usecs;
1736 if (entry->type == TRACE_CONT)
1737 return TRACE_TYPE_HANDLED;
1739 test_cpu_buff_start(iter);
1741 next_entry = find_next_entry(iter, NULL, &next_ts);
1744 rel_usecs = ns2usecs(next_ts - iter->ts);
1745 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1748 comm = trace_find_cmdline(entry->pid);
1749 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1750 " %ld.%03ldms (+%ld.%03ldms): ",
1752 entry->pid, cpu, entry->flags,
1753 entry->preempt_count, trace_idx,
1756 abs_usecs % 1000, rel_usecs/1000,
1759 lat_print_generic(s, entry, cpu);
1760 lat_print_timestamp(s, abs_usecs, rel_usecs);
1762 switch (entry->type) {
1764 struct ftrace_entry *field;
1766 trace_assign_type(field, entry);
1768 seq_print_ip_sym(s, field->ip, sym_flags);
1769 trace_seq_puts(s, " (");
1770 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1771 trace_seq_puts(s, ")\n");
1776 struct ctx_switch_entry *field;
1778 trace_assign_type(field, entry);
1780 T = field->next_state < sizeof(state_to_char) ?
1781 state_to_char[field->next_state] : 'X';
1783 state = field->prev_state ?
1784 __ffs(field->prev_state) + 1 : 0;
1785 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1786 comm = trace_find_cmdline(field->next_pid);
1787 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1790 S, entry->type == TRACE_CTX ? "==>" : " +",
1797 case TRACE_SPECIAL: {
1798 struct special_entry *field;
1800 trace_assign_type(field, entry);
1802 trace_seq_printf(s, "# %ld %ld %ld\n",
1809 struct stack_entry *field;
1811 trace_assign_type(field, entry);
1813 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1815 trace_seq_puts(s, " <= ");
1816 seq_print_ip_sym(s, field->caller[i], sym_flags);
1818 trace_seq_puts(s, "\n");
1822 struct print_entry *field;
1824 trace_assign_type(field, entry);
1826 seq_print_ip_sym(s, field->ip, sym_flags);
1827 trace_seq_printf(s, ": %s", field->buf);
1828 if (entry->flags & TRACE_FLAG_CONT)
1829 trace_seq_print_cont(s, iter);
1832 case TRACE_BRANCH: {
1833 struct trace_branch *field;
1835 trace_assign_type(field, entry);
1837 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1838 field->correct ? " ok " : " MISS ",
1844 case TRACE_USER_STACK: {
1845 struct userstack_entry *field;
1847 trace_assign_type(field, entry);
1849 seq_print_userip_objs(field, s, sym_flags);
1850 trace_seq_putc(s, '\n');
1854 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1856 return TRACE_TYPE_HANDLED;
1859 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1861 struct trace_seq *s = &iter->seq;
1862 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1863 struct trace_entry *entry;
1864 unsigned long usec_rem;
1865 unsigned long long t;
1874 if (entry->type == TRACE_CONT)
1875 return TRACE_TYPE_HANDLED;
1877 test_cpu_buff_start(iter);
1879 comm = trace_find_cmdline(iter->ent->pid);
1881 t = ns2usecs(iter->ts);
1882 usec_rem = do_div(t, 1000000ULL);
1883 secs = (unsigned long)t;
1885 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1887 return TRACE_TYPE_PARTIAL_LINE;
1888 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1890 return TRACE_TYPE_PARTIAL_LINE;
1891 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1893 return TRACE_TYPE_PARTIAL_LINE;
1895 switch (entry->type) {
1897 struct ftrace_entry *field;
1899 trace_assign_type(field, entry);
1901 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1903 return TRACE_TYPE_PARTIAL_LINE;
1904 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1906 ret = trace_seq_printf(s, " <-");
1908 return TRACE_TYPE_PARTIAL_LINE;
1909 ret = seq_print_ip_sym(s,
1913 return TRACE_TYPE_PARTIAL_LINE;
1915 ret = trace_seq_printf(s, "\n");
1917 return TRACE_TYPE_PARTIAL_LINE;
1922 struct ctx_switch_entry *field;
1924 trace_assign_type(field, entry);
1926 S = field->prev_state < sizeof(state_to_char) ?
1927 state_to_char[field->prev_state] : 'X';
1928 T = field->next_state < sizeof(state_to_char) ?
1929 state_to_char[field->next_state] : 'X';
1930 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1934 entry->type == TRACE_CTX ? "==>" : " +",
1940 return TRACE_TYPE_PARTIAL_LINE;
1943 case TRACE_SPECIAL: {
1944 struct special_entry *field;
1946 trace_assign_type(field, entry);
1948 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1953 return TRACE_TYPE_PARTIAL_LINE;
1957 struct stack_entry *field;
1959 trace_assign_type(field, entry);
1961 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1963 ret = trace_seq_puts(s, " <= ");
1965 return TRACE_TYPE_PARTIAL_LINE;
1967 ret = seq_print_ip_sym(s, field->caller[i],
1970 return TRACE_TYPE_PARTIAL_LINE;
1972 ret = trace_seq_puts(s, "\n");
1974 return TRACE_TYPE_PARTIAL_LINE;
1978 struct print_entry *field;
1980 trace_assign_type(field, entry);
1982 seq_print_ip_sym(s, field->ip, sym_flags);
1983 trace_seq_printf(s, ": %s", field->buf);
1984 if (entry->flags & TRACE_FLAG_CONT)
1985 trace_seq_print_cont(s, iter);
1988 case TRACE_FN_RET: {
1989 return print_return_function(iter);
1992 case TRACE_BRANCH: {
1993 struct trace_branch *field;
1995 trace_assign_type(field, entry);
1997 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1998 field->correct ? " ok " : " MISS ",
2004 case TRACE_USER_STACK: {
2005 struct userstack_entry *field;
2007 trace_assign_type(field, entry);
2009 ret = seq_print_userip_objs(field, s, sym_flags);
2011 return TRACE_TYPE_PARTIAL_LINE;
2012 ret = trace_seq_putc(s, '\n');
2014 return TRACE_TYPE_PARTIAL_LINE;
2018 return TRACE_TYPE_HANDLED;
2021 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2023 struct trace_seq *s = &iter->seq;
2024 struct trace_entry *entry;
2030 if (entry->type == TRACE_CONT)
2031 return TRACE_TYPE_HANDLED;
2033 ret = trace_seq_printf(s, "%d %d %llu ",
2034 entry->pid, iter->cpu, iter->ts);
2036 return TRACE_TYPE_PARTIAL_LINE;
2038 switch (entry->type) {
2040 struct ftrace_entry *field;
2042 trace_assign_type(field, entry);
2044 ret = trace_seq_printf(s, "%x %x\n",
2048 return TRACE_TYPE_PARTIAL_LINE;
2053 struct ctx_switch_entry *field;
2055 trace_assign_type(field, entry);
2057 S = field->prev_state < sizeof(state_to_char) ?
2058 state_to_char[field->prev_state] : 'X';
2059 T = field->next_state < sizeof(state_to_char) ?
2060 state_to_char[field->next_state] : 'X';
2061 if (entry->type == TRACE_WAKE)
2063 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
2072 return TRACE_TYPE_PARTIAL_LINE;
2076 case TRACE_USER_STACK:
2078 struct special_entry *field;
2080 trace_assign_type(field, entry);
2082 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
2087 return TRACE_TYPE_PARTIAL_LINE;
2091 struct print_entry *field;
2093 trace_assign_type(field, entry);
2095 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
2096 if (entry->flags & TRACE_FLAG_CONT)
2097 trace_seq_print_cont(s, iter);
2101 return TRACE_TYPE_HANDLED;
2104 #define SEQ_PUT_FIELD_RET(s, x) \
2106 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
2110 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
2112 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
2113 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
2117 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2119 struct trace_seq *s = &iter->seq;
2120 unsigned char newline = '\n';
2121 struct trace_entry *entry;
2126 if (entry->type == TRACE_CONT)
2127 return TRACE_TYPE_HANDLED;
2129 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2130 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2131 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2133 switch (entry->type) {
2135 struct ftrace_entry *field;
2137 trace_assign_type(field, entry);
2139 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
2140 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
2145 struct ctx_switch_entry *field;
2147 trace_assign_type(field, entry);
2149 S = field->prev_state < sizeof(state_to_char) ?
2150 state_to_char[field->prev_state] : 'X';
2151 T = field->next_state < sizeof(state_to_char) ?
2152 state_to_char[field->next_state] : 'X';
2153 if (entry->type == TRACE_WAKE)
2155 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
2156 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
2157 SEQ_PUT_HEX_FIELD_RET(s, S);
2158 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
2159 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
2160 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
2161 SEQ_PUT_HEX_FIELD_RET(s, T);
2165 case TRACE_USER_STACK:
2167 struct special_entry *field;
2169 trace_assign_type(field, entry);
2171 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
2172 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
2173 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
2177 SEQ_PUT_FIELD_RET(s, newline);
2179 return TRACE_TYPE_HANDLED;
2182 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2184 struct trace_seq *s = &iter->seq;
2185 struct trace_entry *entry;
2189 if (entry->type == TRACE_CONT)
2190 return TRACE_TYPE_HANDLED;
2192 SEQ_PUT_FIELD_RET(s, entry->pid);
2193 SEQ_PUT_FIELD_RET(s, entry->cpu);
2194 SEQ_PUT_FIELD_RET(s, iter->ts);
2196 switch (entry->type) {
2198 struct ftrace_entry *field;
2200 trace_assign_type(field, entry);
2202 SEQ_PUT_FIELD_RET(s, field->ip);
2203 SEQ_PUT_FIELD_RET(s, field->parent_ip);
2207 struct ctx_switch_entry *field;
2209 trace_assign_type(field, entry);
2211 SEQ_PUT_FIELD_RET(s, field->prev_pid);
2212 SEQ_PUT_FIELD_RET(s, field->prev_prio);
2213 SEQ_PUT_FIELD_RET(s, field->prev_state);
2214 SEQ_PUT_FIELD_RET(s, field->next_pid);
2215 SEQ_PUT_FIELD_RET(s, field->next_prio);
2216 SEQ_PUT_FIELD_RET(s, field->next_state);
2220 case TRACE_USER_STACK:
2222 struct special_entry *field;
2224 trace_assign_type(field, entry);
2226 SEQ_PUT_FIELD_RET(s, field->arg1);
2227 SEQ_PUT_FIELD_RET(s, field->arg2);
2228 SEQ_PUT_FIELD_RET(s, field->arg3);
2235 static int trace_empty(struct trace_iterator *iter)
2239 for_each_tracing_cpu(cpu) {
2240 if (iter->buffer_iter[cpu]) {
2241 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2244 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2252 static enum print_line_t print_trace_line(struct trace_iterator *iter)
2254 enum print_line_t ret;
2256 if (iter->trace && iter->trace->print_line) {
2257 ret = iter->trace->print_line(iter);
2258 if (ret != TRACE_TYPE_UNHANDLED)
2262 if (trace_flags & TRACE_ITER_BIN)
2263 return print_bin_fmt(iter);
2265 if (trace_flags & TRACE_ITER_HEX)
2266 return print_hex_fmt(iter);
2268 if (trace_flags & TRACE_ITER_RAW)
2269 return print_raw_fmt(iter);
2271 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2272 return print_lat_fmt(iter, iter->idx, iter->cpu);
2274 return print_trace_fmt(iter);
2277 static int s_show(struct seq_file *m, void *v)
2279 struct trace_iterator *iter = v;
2281 if (iter->ent == NULL) {
2283 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2286 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2287 /* print nothing if the buffers are empty */
2288 if (trace_empty(iter))
2290 print_trace_header(m, iter);
2291 if (!(trace_flags & TRACE_ITER_VERBOSE))
2292 print_lat_help_header(m);
2294 if (!(trace_flags & TRACE_ITER_VERBOSE))
2295 print_func_help_header(m);
2298 print_trace_line(iter);
2299 trace_print_seq(m, &iter->seq);
2305 static struct seq_operations tracer_seq_ops = {
2312 static struct trace_iterator *
2313 __tracing_open(struct inode *inode, struct file *file, int *ret)
2315 struct trace_iterator *iter;
2319 if (tracing_disabled) {
2324 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2330 mutex_lock(&trace_types_lock);
2331 if (current_trace && current_trace->print_max)
2334 iter->tr = inode->i_private;
2335 iter->trace = current_trace;
2338 /* Annotate start of buffers if we had overruns */
2339 if (ring_buffer_overruns(iter->tr->buffer))
2340 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2343 for_each_tracing_cpu(cpu) {
2345 iter->buffer_iter[cpu] =
2346 ring_buffer_read_start(iter->tr->buffer, cpu);
2348 if (!iter->buffer_iter[cpu])
2352 /* TODO stop tracer */
2353 *ret = seq_open(file, &tracer_seq_ops);
2357 m = file->private_data;
2360 /* stop the trace while dumping */
2363 if (iter->trace && iter->trace->open)
2364 iter->trace->open(iter);
2366 mutex_unlock(&trace_types_lock);
2372 for_each_tracing_cpu(cpu) {
2373 if (iter->buffer_iter[cpu])
2374 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2376 mutex_unlock(&trace_types_lock);
2379 return ERR_PTR(-ENOMEM);
2382 int tracing_open_generic(struct inode *inode, struct file *filp)
2384 if (tracing_disabled)
2387 filp->private_data = inode->i_private;
2391 int tracing_release(struct inode *inode, struct file *file)
2393 struct seq_file *m = (struct seq_file *)file->private_data;
2394 struct trace_iterator *iter = m->private;
2397 mutex_lock(&trace_types_lock);
2398 for_each_tracing_cpu(cpu) {
2399 if (iter->buffer_iter[cpu])
2400 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2403 if (iter->trace && iter->trace->close)
2404 iter->trace->close(iter);
2406 /* reenable tracing if it was previously enabled */
2408 mutex_unlock(&trace_types_lock);
2410 seq_release(inode, file);
2415 static int tracing_open(struct inode *inode, struct file *file)
2419 __tracing_open(inode, file, &ret);
2424 static int tracing_lt_open(struct inode *inode, struct file *file)
2426 struct trace_iterator *iter;
2429 iter = __tracing_open(inode, file, &ret);
2432 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2439 t_next(struct seq_file *m, void *v, loff_t *pos)
2441 struct tracer *t = m->private;
2453 static void *t_start(struct seq_file *m, loff_t *pos)
2455 struct tracer *t = m->private;
2458 mutex_lock(&trace_types_lock);
2459 for (; t && l < *pos; t = t_next(m, t, &l))
2465 static void t_stop(struct seq_file *m, void *p)
2467 mutex_unlock(&trace_types_lock);
2470 static int t_show(struct seq_file *m, void *v)
2472 struct tracer *t = v;
2477 seq_printf(m, "%s", t->name);
2486 static struct seq_operations show_traces_seq_ops = {
2493 static int show_traces_open(struct inode *inode, struct file *file)
2497 if (tracing_disabled)
2500 ret = seq_open(file, &show_traces_seq_ops);
2502 struct seq_file *m = file->private_data;
2503 m->private = trace_types;
2509 static struct file_operations tracing_fops = {
2510 .open = tracing_open,
2512 .llseek = seq_lseek,
2513 .release = tracing_release,
2516 static struct file_operations tracing_lt_fops = {
2517 .open = tracing_lt_open,
2519 .llseek = seq_lseek,
2520 .release = tracing_release,
2523 static struct file_operations show_traces_fops = {
2524 .open = show_traces_open,
2526 .release = seq_release,
2530 * Only trace on a CPU if the bitmask is set:
2532 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2535 * When tracing/tracing_cpu_mask is modified then this holds
2536 * the new bitmask we are about to install:
2538 static cpumask_t tracing_cpumask_new;
2541 * The tracer itself will not take this lock, but still we want
2542 * to provide a consistent cpumask to user-space:
2544 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2547 * Temporary storage for the character representation of the
2548 * CPU bitmask (and one more byte for the newline):
2550 static char mask_str[NR_CPUS + 1];
2553 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2554 size_t count, loff_t *ppos)
2558 mutex_lock(&tracing_cpumask_update_lock);
2560 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2561 if (count - len < 2) {
2565 len += sprintf(mask_str + len, "\n");
2566 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2569 mutex_unlock(&tracing_cpumask_update_lock);
2575 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2576 size_t count, loff_t *ppos)
2580 mutex_lock(&tracing_cpumask_update_lock);
2581 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2585 raw_local_irq_disable();
2586 __raw_spin_lock(&ftrace_max_lock);
2587 for_each_tracing_cpu(cpu) {
2589 * Increase/decrease the disabled counter if we are
2590 * about to flip a bit in the cpumask:
2592 if (cpu_isset(cpu, tracing_cpumask) &&
2593 !cpu_isset(cpu, tracing_cpumask_new)) {
2594 atomic_inc(&global_trace.data[cpu]->disabled);
2596 if (!cpu_isset(cpu, tracing_cpumask) &&
2597 cpu_isset(cpu, tracing_cpumask_new)) {
2598 atomic_dec(&global_trace.data[cpu]->disabled);
2601 __raw_spin_unlock(&ftrace_max_lock);
2602 raw_local_irq_enable();
2604 tracing_cpumask = tracing_cpumask_new;
2606 mutex_unlock(&tracing_cpumask_update_lock);
2611 mutex_unlock(&tracing_cpumask_update_lock);
2616 static struct file_operations tracing_cpumask_fops = {
2617 .open = tracing_open_generic,
2618 .read = tracing_cpumask_read,
2619 .write = tracing_cpumask_write,
2623 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2624 size_t cnt, loff_t *ppos)
2630 u32 tracer_flags = current_trace->flags->val;
2631 struct tracer_opt *trace_opts = current_trace->flags->opts;
2634 /* calulate max size */
2635 for (i = 0; trace_options[i]; i++) {
2636 len += strlen(trace_options[i]);
2637 len += 3; /* "no" and space */
2641 * Increase the size with names of options specific
2642 * of the current tracer.
2644 for (i = 0; trace_opts[i].name; i++) {
2645 len += strlen(trace_opts[i].name);
2646 len += 3; /* "no" and space */
2649 /* +2 for \n and \0 */
2650 buf = kmalloc(len + 2, GFP_KERNEL);
2654 for (i = 0; trace_options[i]; i++) {
2655 if (trace_flags & (1 << i))
2656 r += sprintf(buf + r, "%s ", trace_options[i]);
2658 r += sprintf(buf + r, "no%s ", trace_options[i]);
2661 for (i = 0; trace_opts[i].name; i++) {
2662 if (tracer_flags & trace_opts[i].bit)
2663 r += sprintf(buf + r, "%s ",
2664 trace_opts[i].name);
2666 r += sprintf(buf + r, "no%s ",
2667 trace_opts[i].name);
2670 r += sprintf(buf + r, "\n");
2671 WARN_ON(r >= len + 2);
2673 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2680 /* Try to assign a tracer specific option */
2681 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2683 struct tracer_flags *trace_flags = trace->flags;
2684 struct tracer_opt *opts = NULL;
2688 for (i = 0; trace_flags->opts[i].name; i++) {
2689 opts = &trace_flags->opts[i];
2690 len = strlen(opts->name);
2692 if (strncmp(cmp, opts->name, len) == 0) {
2693 ret = trace->set_flag(trace_flags->val,
2699 if (!trace_flags->opts[i].name)
2702 /* Refused to handle */
2707 trace_flags->val &= ~opts->bit;
2709 trace_flags->val |= opts->bit;
2715 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2716 size_t cnt, loff_t *ppos)
2724 if (cnt >= sizeof(buf))
2727 if (copy_from_user(&buf, ubuf, cnt))
2732 if (strncmp(buf, "no", 2) == 0) {
2737 for (i = 0; trace_options[i]; i++) {
2738 int len = strlen(trace_options[i]);
2740 if (strncmp(cmp, trace_options[i], len) == 0) {
2742 trace_flags &= ~(1 << i);
2744 trace_flags |= (1 << i);
2749 /* If no option could be set, test the specific tracer options */
2750 if (!trace_options[i]) {
2751 ret = set_tracer_option(current_trace, cmp, neg);
2761 static struct file_operations tracing_iter_fops = {
2762 .open = tracing_open_generic,
2763 .read = tracing_trace_options_read,
2764 .write = tracing_trace_options_write,
2767 static const char readme_msg[] =
2768 "tracing mini-HOWTO:\n\n"
2770 "# mount -t debugfs nodev /debug\n\n"
2771 "# cat /debug/tracing/available_tracers\n"
2772 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2773 "# cat /debug/tracing/current_tracer\n"
2775 "# echo sched_switch > /debug/tracing/current_tracer\n"
2776 "# cat /debug/tracing/current_tracer\n"
2778 "# cat /debug/tracing/trace_options\n"
2779 "noprint-parent nosym-offset nosym-addr noverbose\n"
2780 "# echo print-parent > /debug/tracing/trace_options\n"
2781 "# echo 1 > /debug/tracing/tracing_enabled\n"
2782 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2783 "echo 0 > /debug/tracing/tracing_enabled\n"
2787 tracing_readme_read(struct file *filp, char __user *ubuf,
2788 size_t cnt, loff_t *ppos)
2790 return simple_read_from_buffer(ubuf, cnt, ppos,
2791 readme_msg, strlen(readme_msg));
2794 static struct file_operations tracing_readme_fops = {
2795 .open = tracing_open_generic,
2796 .read = tracing_readme_read,
2800 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2801 size_t cnt, loff_t *ppos)
2806 r = sprintf(buf, "%u\n", tracer_enabled);
2807 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2811 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2812 size_t cnt, loff_t *ppos)
2814 struct trace_array *tr = filp->private_data;
2819 if (cnt >= sizeof(buf))
2822 if (copy_from_user(&buf, ubuf, cnt))
2827 ret = strict_strtoul(buf, 10, &val);
2833 mutex_lock(&trace_types_lock);
2834 if (tracer_enabled ^ val) {
2837 if (current_trace->start)
2838 current_trace->start(tr);
2843 if (current_trace->stop)
2844 current_trace->stop(tr);
2847 mutex_unlock(&trace_types_lock);
2855 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2856 size_t cnt, loff_t *ppos)
2858 char buf[max_tracer_type_len+2];
2861 mutex_lock(&trace_types_lock);
2863 r = sprintf(buf, "%s\n", current_trace->name);
2865 r = sprintf(buf, "\n");
2866 mutex_unlock(&trace_types_lock);
2868 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2871 static int tracing_set_tracer(char *buf)
2873 struct trace_array *tr = &global_trace;
2877 mutex_lock(&trace_types_lock);
2878 for (t = trace_types; t; t = t->next) {
2879 if (strcmp(t->name, buf) == 0)
2886 if (t == current_trace)
2889 trace_branch_disable();
2890 if (current_trace && current_trace->reset)
2891 current_trace->reset(tr);
2900 trace_branch_enable(tr);
2902 mutex_unlock(&trace_types_lock);
2908 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2909 size_t cnt, loff_t *ppos)
2911 char buf[max_tracer_type_len+1];
2918 if (cnt > max_tracer_type_len)
2919 cnt = max_tracer_type_len;
2921 if (copy_from_user(&buf, ubuf, cnt))
2926 /* strip ending whitespace. */
2927 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2930 err = tracing_set_tracer(buf);
2940 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2941 size_t cnt, loff_t *ppos)
2943 unsigned long *ptr = filp->private_data;
2947 r = snprintf(buf, sizeof(buf), "%ld\n",
2948 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2949 if (r > sizeof(buf))
2951 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2955 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2956 size_t cnt, loff_t *ppos)
2958 long *ptr = filp->private_data;
2963 if (cnt >= sizeof(buf))
2966 if (copy_from_user(&buf, ubuf, cnt))
2971 ret = strict_strtoul(buf, 10, &val);
2980 static atomic_t tracing_reader;
2982 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2984 struct trace_iterator *iter;
2986 if (tracing_disabled)
2989 /* We only allow for reader of the pipe */
2990 if (atomic_inc_return(&tracing_reader) != 1) {
2991 atomic_dec(&tracing_reader);
2995 /* create a buffer to store the information to pass to userspace */
2996 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3000 mutex_lock(&trace_types_lock);
3002 /* trace pipe does not show start of buffer */
3003 cpus_setall(iter->started);
3005 iter->tr = &global_trace;
3006 iter->trace = current_trace;
3007 filp->private_data = iter;
3009 if (iter->trace->pipe_open)
3010 iter->trace->pipe_open(iter);
3011 mutex_unlock(&trace_types_lock);
3016 static int tracing_release_pipe(struct inode *inode, struct file *file)
3018 struct trace_iterator *iter = file->private_data;
3021 atomic_dec(&tracing_reader);
3027 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3029 struct trace_iterator *iter = filp->private_data;
3031 if (trace_flags & TRACE_ITER_BLOCK) {
3033 * Always select as readable when in blocking mode
3035 return POLLIN | POLLRDNORM;
3037 if (!trace_empty(iter))
3038 return POLLIN | POLLRDNORM;
3039 poll_wait(filp, &trace_wait, poll_table);
3040 if (!trace_empty(iter))
3041 return POLLIN | POLLRDNORM;
3051 tracing_read_pipe(struct file *filp, char __user *ubuf,
3052 size_t cnt, loff_t *ppos)
3054 struct trace_iterator *iter = filp->private_data;
3057 /* return any leftover data */
3058 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3062 trace_seq_reset(&iter->seq);
3064 mutex_lock(&trace_types_lock);
3065 if (iter->trace->read) {
3066 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3073 while (trace_empty(iter)) {
3075 if ((filp->f_flags & O_NONBLOCK)) {
3081 * This is a make-shift waitqueue. The reason we don't use
3082 * an actual wait queue is because:
3083 * 1) we only ever have one waiter
3084 * 2) the tracing, traces all functions, we don't want
3085 * the overhead of calling wake_up and friends
3086 * (and tracing them too)
3087 * Anyway, this is really very primitive wakeup.
3089 set_current_state(TASK_INTERRUPTIBLE);
3090 iter->tr->waiter = current;
3092 mutex_unlock(&trace_types_lock);
3094 /* sleep for 100 msecs, and try again. */
3095 schedule_timeout(HZ/10);
3097 mutex_lock(&trace_types_lock);
3099 iter->tr->waiter = NULL;
3101 if (signal_pending(current)) {
3106 if (iter->trace != current_trace)
3110 * We block until we read something and tracing is disabled.
3111 * We still block if tracing is disabled, but we have never
3112 * read anything. This allows a user to cat this file, and
3113 * then enable tracing. But after we have read something,
3114 * we give an EOF when tracing is again disabled.
3116 * iter->pos will be 0 if we haven't read anything.
3118 if (!tracer_enabled && iter->pos)
3124 /* stop when tracing is finished */
3125 if (trace_empty(iter))
3128 if (cnt >= PAGE_SIZE)
3129 cnt = PAGE_SIZE - 1;
3131 /* reset all but tr, trace, and overruns */
3132 memset(&iter->seq, 0,
3133 sizeof(struct trace_iterator) -
3134 offsetof(struct trace_iterator, seq));
3137 while (find_next_entry_inc(iter) != NULL) {
3138 enum print_line_t ret;
3139 int len = iter->seq.len;
3141 ret = print_trace_line(iter);
3142 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3143 /* don't print partial lines */
3144 iter->seq.len = len;
3148 trace_consume(iter);
3150 if (iter->seq.len >= cnt)
3154 /* Now copy what we have to the user */
3155 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3156 if (iter->seq.readpos >= iter->seq.len)
3157 trace_seq_reset(&iter->seq);
3160 * If there was nothing to send to user, inspite of consuming trace
3161 * entries, go back to wait for more entries.
3167 mutex_unlock(&trace_types_lock);
3173 tracing_entries_read(struct file *filp, char __user *ubuf,
3174 size_t cnt, loff_t *ppos)
3176 struct trace_array *tr = filp->private_data;
3180 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3181 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3185 tracing_entries_write(struct file *filp, const char __user *ubuf,
3186 size_t cnt, loff_t *ppos)
3192 if (cnt >= sizeof(buf))
3195 if (copy_from_user(&buf, ubuf, cnt))
3200 ret = strict_strtoul(buf, 10, &val);
3204 /* must have at least 1 entry */
3208 mutex_lock(&trace_types_lock);
3212 /* disable all cpu buffers */
3213 for_each_tracing_cpu(cpu) {
3214 if (global_trace.data[cpu])
3215 atomic_inc(&global_trace.data[cpu]->disabled);
3216 if (max_tr.data[cpu])
3217 atomic_inc(&max_tr.data[cpu]->disabled);
3220 /* value is in KB */
3223 if (val != global_trace.entries) {
3224 ret = ring_buffer_resize(global_trace.buffer, val);
3230 ret = ring_buffer_resize(max_tr.buffer, val);
3234 r = ring_buffer_resize(global_trace.buffer,
3235 global_trace.entries);
3237 /* AARGH! We are left with different
3238 * size max buffer!!!! */
3240 tracing_disabled = 1;
3245 global_trace.entries = val;
3250 /* If check pages failed, return ENOMEM */
3251 if (tracing_disabled)
3254 for_each_tracing_cpu(cpu) {
3255 if (global_trace.data[cpu])
3256 atomic_dec(&global_trace.data[cpu]->disabled);
3257 if (max_tr.data[cpu])
3258 atomic_dec(&max_tr.data[cpu]->disabled);
3262 max_tr.entries = global_trace.entries;
3263 mutex_unlock(&trace_types_lock);
3268 static int mark_printk(const char *fmt, ...)
3272 va_start(args, fmt);
3273 ret = trace_vprintk(0, fmt, args);
3279 tracing_mark_write(struct file *filp, const char __user *ubuf,
3280 size_t cnt, loff_t *fpos)
3285 if (tracing_disabled)
3288 if (cnt > TRACE_BUF_SIZE)
3289 cnt = TRACE_BUF_SIZE;
3291 buf = kmalloc(cnt + 1, GFP_KERNEL);
3295 if (copy_from_user(buf, ubuf, cnt)) {
3300 /* Cut from the first nil or newline. */
3302 end = strchr(buf, '\n');
3306 cnt = mark_printk("%s\n", buf);
3313 static struct file_operations tracing_max_lat_fops = {
3314 .open = tracing_open_generic,
3315 .read = tracing_max_lat_read,
3316 .write = tracing_max_lat_write,
3319 static struct file_operations tracing_ctrl_fops = {
3320 .open = tracing_open_generic,
3321 .read = tracing_ctrl_read,
3322 .write = tracing_ctrl_write,
3325 static struct file_operations set_tracer_fops = {
3326 .open = tracing_open_generic,
3327 .read = tracing_set_trace_read,
3328 .write = tracing_set_trace_write,
3331 static struct file_operations tracing_pipe_fops = {
3332 .open = tracing_open_pipe,
3333 .poll = tracing_poll_pipe,
3334 .read = tracing_read_pipe,
3335 .release = tracing_release_pipe,
3338 static struct file_operations tracing_entries_fops = {
3339 .open = tracing_open_generic,
3340 .read = tracing_entries_read,
3341 .write = tracing_entries_write,
3344 static struct file_operations tracing_mark_fops = {
3345 .open = tracing_open_generic,
3346 .write = tracing_mark_write,
3349 #ifdef CONFIG_DYNAMIC_FTRACE
3351 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3357 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3358 size_t cnt, loff_t *ppos)
3360 static char ftrace_dyn_info_buffer[1024];
3361 static DEFINE_MUTEX(dyn_info_mutex);
3362 unsigned long *p = filp->private_data;
3363 char *buf = ftrace_dyn_info_buffer;
3364 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3367 mutex_lock(&dyn_info_mutex);
3368 r = sprintf(buf, "%ld ", *p);
3370 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3373 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3375 mutex_unlock(&dyn_info_mutex);
3380 static struct file_operations tracing_dyn_info_fops = {
3381 .open = tracing_open_generic,
3382 .read = tracing_read_dyn_info,
3386 static struct dentry *d_tracer;
3388 struct dentry *tracing_init_dentry(void)
3395 d_tracer = debugfs_create_dir("tracing", NULL);
3397 if (!d_tracer && !once) {
3399 pr_warning("Could not create debugfs directory 'tracing'\n");
3406 #ifdef CONFIG_FTRACE_SELFTEST
3407 /* Let selftest have access to static functions in this file */
3408 #include "trace_selftest.c"
3411 static __init int tracer_init_debugfs(void)
3413 struct dentry *d_tracer;
3414 struct dentry *entry;
3416 d_tracer = tracing_init_dentry();
3418 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3419 &global_trace, &tracing_ctrl_fops);
3421 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3423 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3424 NULL, &tracing_iter_fops);
3426 pr_warning("Could not create debugfs 'trace_options' entry\n");
3428 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3429 NULL, &tracing_cpumask_fops);
3431 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3433 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3434 &global_trace, &tracing_lt_fops);
3436 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3438 entry = debugfs_create_file("trace", 0444, d_tracer,
3439 &global_trace, &tracing_fops);
3441 pr_warning("Could not create debugfs 'trace' entry\n");
3443 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3444 &global_trace, &show_traces_fops);
3446 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3448 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3449 &global_trace, &set_tracer_fops);
3451 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3453 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3454 &tracing_max_latency,
3455 &tracing_max_lat_fops);
3457 pr_warning("Could not create debugfs "
3458 "'tracing_max_latency' entry\n");
3460 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3461 &tracing_thresh, &tracing_max_lat_fops);
3463 pr_warning("Could not create debugfs "
3464 "'tracing_thresh' entry\n");
3465 entry = debugfs_create_file("README", 0644, d_tracer,
3466 NULL, &tracing_readme_fops);
3468 pr_warning("Could not create debugfs 'README' entry\n");
3470 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3471 NULL, &tracing_pipe_fops);
3473 pr_warning("Could not create debugfs "
3474 "'trace_pipe' entry\n");
3476 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3477 &global_trace, &tracing_entries_fops);
3479 pr_warning("Could not create debugfs "
3480 "'buffer_size_kb' entry\n");
3482 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3483 NULL, &tracing_mark_fops);
3485 pr_warning("Could not create debugfs "
3486 "'trace_marker' entry\n");
3488 #ifdef CONFIG_DYNAMIC_FTRACE
3489 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3490 &ftrace_update_tot_cnt,
3491 &tracing_dyn_info_fops);
3493 pr_warning("Could not create debugfs "
3494 "'dyn_ftrace_total_info' entry\n");
3496 #ifdef CONFIG_SYSPROF_TRACER
3497 init_tracer_sysprof_debugfs(d_tracer);
3502 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3504 static DEFINE_SPINLOCK(trace_buf_lock);
3505 static char trace_buf[TRACE_BUF_SIZE];
3507 struct ring_buffer_event *event;
3508 struct trace_array *tr = &global_trace;
3509 struct trace_array_cpu *data;
3510 struct print_entry *entry;
3511 unsigned long flags, irq_flags;
3512 int cpu, len = 0, size, pc;
3514 if (tracing_disabled)
3517 pc = preempt_count();
3518 preempt_disable_notrace();
3519 cpu = raw_smp_processor_id();
3520 data = tr->data[cpu];
3522 if (unlikely(atomic_read(&data->disabled)))
3525 spin_lock_irqsave(&trace_buf_lock, flags);
3526 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3528 len = min(len, TRACE_BUF_SIZE-1);
3531 size = sizeof(*entry) + len + 1;
3532 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3535 entry = ring_buffer_event_data(event);
3536 tracing_generic_entry_update(&entry->ent, flags, pc);
3537 entry->ent.type = TRACE_PRINT;
3540 memcpy(&entry->buf, trace_buf, len);
3541 entry->buf[len] = 0;
3542 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3545 spin_unlock_irqrestore(&trace_buf_lock, flags);
3548 preempt_enable_notrace();
3552 EXPORT_SYMBOL_GPL(trace_vprintk);
3554 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3559 if (!(trace_flags & TRACE_ITER_PRINTK))
3563 ret = trace_vprintk(ip, fmt, ap);
3567 EXPORT_SYMBOL_GPL(__ftrace_printk);
3569 static int trace_panic_handler(struct notifier_block *this,
3570 unsigned long event, void *unused)
3572 if (ftrace_dump_on_oops)
3577 static struct notifier_block trace_panic_notifier = {
3578 .notifier_call = trace_panic_handler,
3580 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3583 static int trace_die_handler(struct notifier_block *self,
3589 if (ftrace_dump_on_oops)
3598 static struct notifier_block trace_die_notifier = {
3599 .notifier_call = trace_die_handler,
3604 * printk is set to max of 1024, we really don't need it that big.
3605 * Nothing should be printing 1000 characters anyway.
3607 #define TRACE_MAX_PRINT 1000
3610 * Define here KERN_TRACE so that we have one place to modify
3611 * it if we decide to change what log level the ftrace dump
3614 #define KERN_TRACE KERN_INFO
3617 trace_printk_seq(struct trace_seq *s)
3619 /* Probably should print a warning here. */
3623 /* should be zero ended, but we are paranoid. */
3624 s->buffer[s->len] = 0;
3626 printk(KERN_TRACE "%s", s->buffer);
3631 void ftrace_dump(void)
3633 static DEFINE_SPINLOCK(ftrace_dump_lock);
3634 /* use static because iter can be a bit big for the stack */
3635 static struct trace_iterator iter;
3636 static cpumask_t mask;
3637 static int dump_ran;
3638 unsigned long flags;
3642 spin_lock_irqsave(&ftrace_dump_lock, flags);
3648 /* No turning back! */
3651 for_each_tracing_cpu(cpu) {
3652 atomic_inc(&global_trace.data[cpu]->disabled);
3655 /* don't look at user memory in panic mode */
3656 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3658 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3660 iter.tr = &global_trace;
3661 iter.trace = current_trace;
3664 * We need to stop all tracing on all CPUS to read the
3665 * the next buffer. This is a bit expensive, but is
3666 * not done often. We fill all what we can read,
3667 * and then release the locks again.
3672 while (!trace_empty(&iter)) {
3675 printk(KERN_TRACE "---------------------------------\n");
3679 /* reset all but tr, trace, and overruns */
3680 memset(&iter.seq, 0,
3681 sizeof(struct trace_iterator) -
3682 offsetof(struct trace_iterator, seq));
3683 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3686 if (find_next_entry_inc(&iter) != NULL) {
3687 print_trace_line(&iter);
3688 trace_consume(&iter);
3691 trace_printk_seq(&iter.seq);
3695 printk(KERN_TRACE " (ftrace buffer empty)\n");
3697 printk(KERN_TRACE "---------------------------------\n");
3700 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3703 __init static int tracer_alloc_buffers(void)
3705 struct trace_array_cpu *data;
3708 /* TODO: make the number of buffers hot pluggable with CPUS */
3709 tracing_buffer_mask = cpu_possible_map;
3711 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3712 TRACE_BUFFER_FLAGS);
3713 if (!global_trace.buffer) {
3714 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3718 global_trace.entries = ring_buffer_size(global_trace.buffer);
3720 #ifdef CONFIG_TRACER_MAX_TRACE
3721 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3722 TRACE_BUFFER_FLAGS);
3723 if (!max_tr.buffer) {
3724 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3726 ring_buffer_free(global_trace.buffer);
3729 max_tr.entries = ring_buffer_size(max_tr.buffer);
3730 WARN_ON(max_tr.entries != global_trace.entries);
3733 /* Allocate the first page for all buffers */
3734 for_each_tracing_cpu(i) {
3735 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3736 max_tr.data[i] = &per_cpu(max_data, i);
3739 trace_init_cmdlines();
3741 register_tracer(&nop_trace);
3742 #ifdef CONFIG_BOOT_TRACER
3743 register_tracer(&boot_tracer);
3744 current_trace = &boot_tracer;
3745 current_trace->init(&global_trace);
3747 current_trace = &nop_trace;
3750 /* All seems OK, enable tracing */
3751 tracing_disabled = 0;
3753 atomic_notifier_chain_register(&panic_notifier_list,
3754 &trace_panic_notifier);
3756 register_die_notifier(&trace_die_notifier);
3760 early_initcall(tracer_alloc_buffers);
3761 fs_initcall(tracer_init_debugfs);