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 /* We need to change this state when a selftest is running.
48 * A selftest will lurk into the ring-buffer to count the
49 * entries inserted during the selftest although some concurrent
50 * insertions into the ring-buffer such as ftrace_printk could occurred
51 * at the same time, giving false positive or negative results.
53 static atomic_t tracing_selftest_running = ATOMIC_INIT(0);
55 /* For tracers that don't implement custom flags */
56 static struct tracer_opt dummy_tracer_opt[] = {
60 static struct tracer_flags dummy_tracer_flags = {
62 .opts = dummy_tracer_opt
65 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
71 * Kill all tracing for good (never come back).
72 * It is initialized to 1 but will turn to zero if the initialization
73 * of the tracer is successful. But that is the only place that sets
76 int tracing_disabled = 1;
78 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
80 static inline void ftrace_disable_cpu(void)
83 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
86 static inline void ftrace_enable_cpu(void)
88 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
92 static cpumask_t __read_mostly tracing_buffer_mask;
94 #define for_each_tracing_cpu(cpu) \
95 for_each_cpu_mask(cpu, tracing_buffer_mask)
98 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
100 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
101 * is set, then ftrace_dump is called. This will output the contents
102 * of the ftrace buffers to the console. This is very useful for
103 * capturing traces that lead to crashes and outputing it to a
106 * It is default off, but you can enable it with either specifying
107 * "ftrace_dump_on_oops" in the kernel command line, or setting
108 * /proc/sys/kernel/ftrace_dump_on_oops to true.
110 int ftrace_dump_on_oops;
112 static int tracing_set_tracer(char *buf);
114 static int __init set_ftrace(char *str)
116 tracing_set_tracer(str);
119 __setup("ftrace", set_ftrace);
121 static int __init set_ftrace_dump_on_oops(char *str)
123 ftrace_dump_on_oops = 1;
126 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
129 ns2usecs(cycle_t nsec)
136 cycle_t ftrace_now(int cpu)
138 u64 ts = ring_buffer_time_stamp(cpu);
139 ring_buffer_normalize_time_stamp(cpu, &ts);
144 * The global_trace is the descriptor that holds the tracing
145 * buffers for the live tracing. For each CPU, it contains
146 * a link list of pages that will store trace entries. The
147 * page descriptor of the pages in the memory is used to hold
148 * the link list by linking the lru item in the page descriptor
149 * to each of the pages in the buffer per CPU.
151 * For each active CPU there is a data field that holds the
152 * pages for the buffer for that CPU. Each CPU has the same number
153 * of pages allocated for its buffer.
155 static struct trace_array global_trace;
157 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
160 * The max_tr is used to snapshot the global_trace when a maximum
161 * latency is reached. Some tracers will use this to store a maximum
162 * trace while it continues examining live traces.
164 * The buffers for the max_tr are set up the same as the global_trace.
165 * When a snapshot is taken, the link list of the max_tr is swapped
166 * with the link list of the global_trace and the buffers are reset for
167 * the global_trace so the tracing can continue.
169 static struct trace_array max_tr;
171 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
173 /* tracer_enabled is used to toggle activation of a tracer */
174 static int tracer_enabled = 1;
177 * tracing_is_enabled - return tracer_enabled status
179 * This function is used by other tracers to know the status
180 * of the tracer_enabled flag. Tracers may use this function
181 * to know if it should enable their features when starting
182 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
184 int tracing_is_enabled(void)
186 return tracer_enabled;
189 /* function tracing enabled */
190 int ftrace_function_enabled;
193 * trace_buf_size is the size in bytes that is allocated
194 * for a buffer. Note, the number of bytes is always rounded
197 * This number is purposely set to a low number of 16384.
198 * If the dump on oops happens, it will be much appreciated
199 * to not have to wait for all that output. Anyway this can be
200 * boot time and run time configurable.
202 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
204 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
206 /* trace_types holds a link list of available tracers. */
207 static struct tracer *trace_types __read_mostly;
209 /* current_trace points to the tracer that is currently active */
210 static struct tracer *current_trace __read_mostly;
213 * max_tracer_type_len is used to simplify the allocating of
214 * buffers to read userspace tracer names. We keep track of
215 * the longest tracer name registered.
217 static int max_tracer_type_len;
220 * trace_types_lock is used to protect the trace_types list.
221 * This lock is also used to keep user access serialized.
222 * Accesses from userspace will grab this lock while userspace
223 * activities happen inside the kernel.
225 static DEFINE_MUTEX(trace_types_lock);
227 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
228 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
230 /* trace_flags holds trace_options default values */
231 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
235 * trace_wake_up - wake up tasks waiting for trace input
237 * Simply wakes up any task that is blocked on the trace_wait
238 * queue. These is used with trace_poll for tasks polling the trace.
240 void trace_wake_up(void)
243 * The runqueue_is_locked() can fail, but this is the best we
246 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
247 wake_up(&trace_wait);
250 static int __init set_buf_size(char *str)
252 unsigned long buf_size;
257 ret = strict_strtoul(str, 0, &buf_size);
258 /* nr_entries can not be zero */
259 if (ret < 0 || buf_size == 0)
261 trace_buf_size = buf_size;
264 __setup("trace_buf_size=", set_buf_size);
266 unsigned long nsecs_to_usecs(unsigned long nsecs)
271 /* These must match the bit postions in trace_iterator_flags */
272 static const char *trace_options[] = {
293 * ftrace_max_lock is used to protect the swapping of buffers
294 * when taking a max snapshot. The buffers themselves are
295 * protected by per_cpu spinlocks. But the action of the swap
296 * needs its own lock.
298 * This is defined as a raw_spinlock_t in order to help
299 * with performance when lockdep debugging is enabled.
301 static raw_spinlock_t ftrace_max_lock =
302 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
305 * Copy the new maximum trace into the separate maximum-trace
306 * structure. (this way the maximum trace is permanently saved,
307 * for later retrieval via /debugfs/tracing/latency_trace)
310 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
312 struct trace_array_cpu *data = tr->data[cpu];
315 max_tr.time_start = data->preempt_timestamp;
317 data = max_tr.data[cpu];
318 data->saved_latency = tracing_max_latency;
320 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
321 data->pid = tsk->pid;
322 data->uid = tsk->uid;
323 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
324 data->policy = tsk->policy;
325 data->rt_priority = tsk->rt_priority;
327 /* record this tasks comm */
328 tracing_record_cmdline(current);
332 * trace_seq_printf - sequence printing of trace information
333 * @s: trace sequence descriptor
334 * @fmt: printf format string
336 * The tracer may use either sequence operations or its own
337 * copy to user routines. To simplify formating of a trace
338 * trace_seq_printf is used to store strings into a special
339 * buffer (@s). Then the output may be either used by
340 * the sequencer or pulled into another buffer.
343 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
345 int len = (PAGE_SIZE - 1) - s->len;
353 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
356 /* If we can't write it all, don't bother writing anything */
366 * trace_seq_puts - trace sequence printing of simple string
367 * @s: trace sequence descriptor
368 * @str: simple string to record
370 * The tracer may use either the sequence operations or its own
371 * copy to user routines. This function records a simple string
372 * into a special buffer (@s) for later retrieval by a sequencer
373 * or other mechanism.
376 trace_seq_puts(struct trace_seq *s, const char *str)
378 int len = strlen(str);
380 if (len > ((PAGE_SIZE - 1) - s->len))
383 memcpy(s->buffer + s->len, str, len);
390 trace_seq_putc(struct trace_seq *s, unsigned char c)
392 if (s->len >= (PAGE_SIZE - 1))
395 s->buffer[s->len++] = c;
401 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
403 if (len > ((PAGE_SIZE - 1) - s->len))
406 memcpy(s->buffer + s->len, mem, len);
412 #define MAX_MEMHEX_BYTES 8
413 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
416 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
418 unsigned char hex[HEX_CHARS];
419 unsigned char *data = mem;
423 for (i = 0, j = 0; i < len; i++) {
425 for (i = len-1, j = 0; i >= 0; i--) {
427 hex[j++] = hex_asc_hi(data[i]);
428 hex[j++] = hex_asc_lo(data[i]);
432 return trace_seq_putmem(s, hex, j);
436 trace_seq_path(struct trace_seq *s, struct path *path)
440 if (s->len >= (PAGE_SIZE - 1))
442 p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
444 p = mangle_path(s->buffer + s->len, p, "\n");
446 s->len = p - s->buffer;
450 s->buffer[s->len++] = '?';
458 trace_seq_reset(struct trace_seq *s)
464 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
469 if (s->len <= s->readpos)
472 len = s->len - s->readpos;
475 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
484 trace_print_seq(struct seq_file *m, struct trace_seq *s)
486 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
489 seq_puts(m, s->buffer);
495 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
497 * @tsk: the task with the latency
498 * @cpu: The cpu that initiated the trace.
500 * Flip the buffers between the @tr and the max_tr and record information
501 * about which task was the cause of this latency.
504 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
506 struct ring_buffer *buf = tr->buffer;
508 WARN_ON_ONCE(!irqs_disabled());
509 __raw_spin_lock(&ftrace_max_lock);
511 tr->buffer = max_tr.buffer;
514 ftrace_disable_cpu();
515 ring_buffer_reset(tr->buffer);
518 __update_max_tr(tr, tsk, cpu);
519 __raw_spin_unlock(&ftrace_max_lock);
523 * update_max_tr_single - only copy one trace over, and reset the rest
525 * @tsk - task with the latency
526 * @cpu - the cpu of the buffer to copy.
528 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
531 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
535 WARN_ON_ONCE(!irqs_disabled());
536 __raw_spin_lock(&ftrace_max_lock);
538 ftrace_disable_cpu();
540 ring_buffer_reset(max_tr.buffer);
541 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
547 __update_max_tr(tr, tsk, cpu);
548 __raw_spin_unlock(&ftrace_max_lock);
552 * register_tracer - register a tracer with the ftrace system.
553 * @type - the plugin for the tracer
555 * Register a new plugin tracer.
557 int register_tracer(struct tracer *type)
564 pr_info("Tracer must have a name\n");
569 * When this gets called we hold the BKL which means that
570 * preemption is disabled. Various trace selftests however
571 * need to disable and enable preemption for successful tests.
572 * So we drop the BKL here and grab it after the tests again.
575 mutex_lock(&trace_types_lock);
577 for (t = trace_types; t; t = t->next) {
578 if (strcmp(type->name, t->name) == 0) {
580 pr_info("Trace %s already registered\n",
588 type->set_flag = &dummy_set_flag;
590 type->flags = &dummy_tracer_flags;
592 if (!type->flags->opts)
593 type->flags->opts = dummy_tracer_opt;
595 #ifdef CONFIG_FTRACE_STARTUP_TEST
596 if (type->selftest) {
597 struct tracer *saved_tracer = current_trace;
598 struct trace_array *tr = &global_trace;
601 atomic_set(&tracing_selftest_running, 1);
603 * Run a selftest on this tracer.
604 * Here we reset the trace buffer, and set the current
605 * tracer to be this tracer. The tracer can then run some
606 * internal tracing to verify that everything is in order.
607 * If we fail, we do not register this tracer.
609 for_each_tracing_cpu(i)
610 tracing_reset(tr, i);
612 current_trace = type;
613 /* the test is responsible for initializing and enabling */
614 pr_info("Testing tracer %s: ", type->name);
615 ret = type->selftest(type, tr);
616 atomic_set(&tracing_selftest_running, 0);
617 /* the test is responsible for resetting too */
618 current_trace = saved_tracer;
620 printk(KERN_CONT "FAILED!\n");
623 /* Only reset on passing, to avoid touching corrupted buffers */
624 for_each_tracing_cpu(i)
625 tracing_reset(tr, i);
627 printk(KERN_CONT "PASSED\n");
631 type->next = trace_types;
633 len = strlen(type->name);
634 if (len > max_tracer_type_len)
635 max_tracer_type_len = len;
638 mutex_unlock(&trace_types_lock);
644 void unregister_tracer(struct tracer *type)
649 mutex_lock(&trace_types_lock);
650 for (t = &trace_types; *t; t = &(*t)->next) {
654 pr_info("Trace %s not registered\n", type->name);
659 if (strlen(type->name) != max_tracer_type_len)
662 max_tracer_type_len = 0;
663 for (t = &trace_types; *t; t = &(*t)->next) {
664 len = strlen((*t)->name);
665 if (len > max_tracer_type_len)
666 max_tracer_type_len = len;
669 mutex_unlock(&trace_types_lock);
672 void tracing_reset(struct trace_array *tr, int cpu)
674 ftrace_disable_cpu();
675 ring_buffer_reset_cpu(tr->buffer, cpu);
679 #define SAVED_CMDLINES 128
680 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
681 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
682 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
683 static int cmdline_idx;
684 static DEFINE_SPINLOCK(trace_cmdline_lock);
686 /* temporary disable recording */
687 atomic_t trace_record_cmdline_disabled __read_mostly;
689 static void trace_init_cmdlines(void)
691 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
692 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
696 static int trace_stop_count;
697 static DEFINE_SPINLOCK(tracing_start_lock);
700 * ftrace_off_permanent - disable all ftrace code permanently
702 * This should only be called when a serious anomally has
703 * been detected. This will turn off the function tracing,
704 * ring buffers, and other tracing utilites. It takes no
705 * locks and can be called from any context.
707 void ftrace_off_permanent(void)
709 tracing_disabled = 1;
711 tracing_off_permanent();
715 * tracing_start - quick start of the tracer
717 * If tracing is enabled but was stopped by tracing_stop,
718 * this will start the tracer back up.
720 void tracing_start(void)
722 struct ring_buffer *buffer;
725 if (tracing_disabled)
728 spin_lock_irqsave(&tracing_start_lock, flags);
729 if (--trace_stop_count)
732 if (trace_stop_count < 0) {
733 /* Someone screwed up their debugging */
735 trace_stop_count = 0;
740 buffer = global_trace.buffer;
742 ring_buffer_record_enable(buffer);
744 buffer = max_tr.buffer;
746 ring_buffer_record_enable(buffer);
750 spin_unlock_irqrestore(&tracing_start_lock, flags);
754 * tracing_stop - quick stop of the tracer
756 * Light weight way to stop tracing. Use in conjunction with
759 void tracing_stop(void)
761 struct ring_buffer *buffer;
765 spin_lock_irqsave(&tracing_start_lock, flags);
766 if (trace_stop_count++)
769 buffer = global_trace.buffer;
771 ring_buffer_record_disable(buffer);
773 buffer = max_tr.buffer;
775 ring_buffer_record_disable(buffer);
778 spin_unlock_irqrestore(&tracing_start_lock, flags);
781 void trace_stop_cmdline_recording(void);
783 static void trace_save_cmdline(struct task_struct *tsk)
788 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
792 * It's not the end of the world if we don't get
793 * the lock, but we also don't want to spin
794 * nor do we want to disable interrupts,
795 * so if we miss here, then better luck next time.
797 if (!spin_trylock(&trace_cmdline_lock))
800 idx = map_pid_to_cmdline[tsk->pid];
801 if (idx >= SAVED_CMDLINES) {
802 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
804 map = map_cmdline_to_pid[idx];
805 if (map <= PID_MAX_DEFAULT)
806 map_pid_to_cmdline[map] = (unsigned)-1;
808 map_pid_to_cmdline[tsk->pid] = idx;
813 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
815 spin_unlock(&trace_cmdline_lock);
818 char *trace_find_cmdline(int pid)
820 char *cmdline = "<...>";
826 if (pid > PID_MAX_DEFAULT)
829 map = map_pid_to_cmdline[pid];
830 if (map >= SAVED_CMDLINES)
833 cmdline = saved_cmdlines[map];
839 void tracing_record_cmdline(struct task_struct *tsk)
841 if (atomic_read(&trace_record_cmdline_disabled))
844 trace_save_cmdline(tsk);
848 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
851 struct task_struct *tsk = current;
853 entry->preempt_count = pc & 0xff;
854 entry->pid = (tsk) ? tsk->pid : 0;
855 entry->tgid = (tsk) ? tsk->tgid : 0;
857 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
858 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
860 TRACE_FLAG_IRQS_NOSUPPORT |
862 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
863 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
864 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
868 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
869 unsigned long ip, unsigned long parent_ip, unsigned long flags,
872 struct ring_buffer_event *event;
873 struct ftrace_entry *entry;
874 unsigned long irq_flags;
876 /* If we are reading the ring buffer, don't trace */
877 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
880 event = ring_buffer_lock_reserve(tr->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;
888 entry->parent_ip = parent_ip;
889 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
892 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
893 static void __trace_graph_entry(struct trace_array *tr,
894 struct trace_array_cpu *data,
895 struct ftrace_graph_ent *trace,
899 struct ring_buffer_event *event;
900 struct ftrace_graph_ent_entry *entry;
901 unsigned long irq_flags;
903 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
906 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
910 entry = ring_buffer_event_data(event);
911 tracing_generic_entry_update(&entry->ent, flags, pc);
912 entry->ent.type = TRACE_GRAPH_ENT;
913 entry->graph_ent = *trace;
914 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
917 static void __trace_graph_return(struct trace_array *tr,
918 struct trace_array_cpu *data,
919 struct ftrace_graph_ret *trace,
923 struct ring_buffer_event *event;
924 struct ftrace_graph_ret_entry *entry;
925 unsigned long irq_flags;
927 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
930 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
934 entry = ring_buffer_event_data(event);
935 tracing_generic_entry_update(&entry->ent, flags, pc);
936 entry->ent.type = TRACE_GRAPH_RET;
938 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
943 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
944 unsigned long ip, unsigned long parent_ip, unsigned long flags,
947 if (likely(!atomic_read(&data->disabled)))
948 trace_function(tr, data, ip, parent_ip, flags, pc);
951 static void ftrace_trace_stack(struct trace_array *tr,
952 struct trace_array_cpu *data,
956 #ifdef CONFIG_STACKTRACE
957 struct ring_buffer_event *event;
958 struct stack_entry *entry;
959 struct stack_trace trace;
960 unsigned long irq_flags;
962 if (!(trace_flags & TRACE_ITER_STACKTRACE))
965 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
969 entry = ring_buffer_event_data(event);
970 tracing_generic_entry_update(&entry->ent, flags, pc);
971 entry->ent.type = TRACE_STACK;
973 memset(&entry->caller, 0, sizeof(entry->caller));
975 trace.nr_entries = 0;
976 trace.max_entries = FTRACE_STACK_ENTRIES;
978 trace.entries = entry->caller;
980 save_stack_trace(&trace);
981 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
985 void __trace_stack(struct trace_array *tr,
986 struct trace_array_cpu *data,
990 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
993 static void ftrace_trace_userstack(struct trace_array *tr,
994 struct trace_array_cpu *data,
995 unsigned long flags, int pc)
997 #ifdef CONFIG_STACKTRACE
998 struct ring_buffer_event *event;
999 struct userstack_entry *entry;
1000 struct stack_trace trace;
1001 unsigned long irq_flags;
1003 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1006 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1010 entry = ring_buffer_event_data(event);
1011 tracing_generic_entry_update(&entry->ent, flags, pc);
1012 entry->ent.type = TRACE_USER_STACK;
1014 memset(&entry->caller, 0, sizeof(entry->caller));
1016 trace.nr_entries = 0;
1017 trace.max_entries = FTRACE_STACK_ENTRIES;
1019 trace.entries = entry->caller;
1021 save_stack_trace_user(&trace);
1022 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1026 void __trace_userstack(struct trace_array *tr,
1027 struct trace_array_cpu *data,
1028 unsigned long flags)
1030 ftrace_trace_userstack(tr, data, flags, preempt_count());
1034 ftrace_trace_special(void *__tr, void *__data,
1035 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1038 struct ring_buffer_event *event;
1039 struct trace_array_cpu *data = __data;
1040 struct trace_array *tr = __tr;
1041 struct special_entry *entry;
1042 unsigned long irq_flags;
1044 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1048 entry = ring_buffer_event_data(event);
1049 tracing_generic_entry_update(&entry->ent, 0, pc);
1050 entry->ent.type = TRACE_SPECIAL;
1054 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1055 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
1056 ftrace_trace_userstack(tr, data, irq_flags, pc);
1062 __trace_special(void *__tr, void *__data,
1063 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1065 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
1069 tracing_sched_switch_trace(struct trace_array *tr,
1070 struct trace_array_cpu *data,
1071 struct task_struct *prev,
1072 struct task_struct *next,
1073 unsigned long flags, int pc)
1075 struct ring_buffer_event *event;
1076 struct ctx_switch_entry *entry;
1077 unsigned long irq_flags;
1079 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1083 entry = ring_buffer_event_data(event);
1084 tracing_generic_entry_update(&entry->ent, flags, pc);
1085 entry->ent.type = TRACE_CTX;
1086 entry->prev_pid = prev->pid;
1087 entry->prev_prio = prev->prio;
1088 entry->prev_state = prev->state;
1089 entry->next_pid = next->pid;
1090 entry->next_prio = next->prio;
1091 entry->next_state = next->state;
1092 entry->next_cpu = task_cpu(next);
1093 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1094 ftrace_trace_stack(tr, data, flags, 5, pc);
1095 ftrace_trace_userstack(tr, data, flags, pc);
1099 tracing_sched_wakeup_trace(struct trace_array *tr,
1100 struct trace_array_cpu *data,
1101 struct task_struct *wakee,
1102 struct task_struct *curr,
1103 unsigned long flags, int pc)
1105 struct ring_buffer_event *event;
1106 struct ctx_switch_entry *entry;
1107 unsigned long irq_flags;
1109 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1113 entry = ring_buffer_event_data(event);
1114 tracing_generic_entry_update(&entry->ent, flags, pc);
1115 entry->ent.type = TRACE_WAKE;
1116 entry->prev_pid = curr->pid;
1117 entry->prev_prio = curr->prio;
1118 entry->prev_state = curr->state;
1119 entry->next_pid = wakee->pid;
1120 entry->next_prio = wakee->prio;
1121 entry->next_state = wakee->state;
1122 entry->next_cpu = task_cpu(wakee);
1123 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1124 ftrace_trace_stack(tr, data, flags, 6, pc);
1125 ftrace_trace_userstack(tr, data, flags, pc);
1131 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1133 struct trace_array *tr = &global_trace;
1134 struct trace_array_cpu *data;
1135 unsigned long flags;
1139 if (tracing_disabled)
1142 pc = preempt_count();
1143 local_irq_save(flags);
1144 cpu = raw_smp_processor_id();
1145 data = tr->data[cpu];
1147 if (likely(atomic_inc_return(&data->disabled) == 1))
1148 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1150 atomic_dec(&data->disabled);
1151 local_irq_restore(flags);
1154 #ifdef CONFIG_FUNCTION_TRACER
1156 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
1158 struct trace_array *tr = &global_trace;
1159 struct trace_array_cpu *data;
1160 unsigned long flags;
1165 if (unlikely(!ftrace_function_enabled))
1168 pc = preempt_count();
1169 resched = ftrace_preempt_disable();
1170 local_save_flags(flags);
1171 cpu = raw_smp_processor_id();
1172 data = tr->data[cpu];
1173 disabled = atomic_inc_return(&data->disabled);
1175 if (likely(disabled == 1))
1176 trace_function(tr, data, ip, parent_ip, flags, pc);
1178 atomic_dec(&data->disabled);
1179 ftrace_preempt_enable(resched);
1183 function_trace_call(unsigned long ip, unsigned long parent_ip)
1185 struct trace_array *tr = &global_trace;
1186 struct trace_array_cpu *data;
1187 unsigned long flags;
1192 if (unlikely(!ftrace_function_enabled))
1196 * Need to use raw, since this must be called before the
1197 * recursive protection is performed.
1199 local_irq_save(flags);
1200 cpu = raw_smp_processor_id();
1201 data = tr->data[cpu];
1202 disabled = atomic_inc_return(&data->disabled);
1204 if (likely(disabled == 1)) {
1205 pc = preempt_count();
1206 trace_function(tr, data, ip, parent_ip, flags, pc);
1209 atomic_dec(&data->disabled);
1210 local_irq_restore(flags);
1213 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1214 int trace_graph_entry(struct ftrace_graph_ent *trace)
1216 struct trace_array *tr = &global_trace;
1217 struct trace_array_cpu *data;
1218 unsigned long flags;
1223 if (!ftrace_trace_task(current))
1226 if (!ftrace_graph_addr(trace->func))
1229 local_irq_save(flags);
1230 cpu = raw_smp_processor_id();
1231 data = tr->data[cpu];
1232 disabled = atomic_inc_return(&data->disabled);
1233 if (likely(disabled == 1)) {
1234 pc = preempt_count();
1235 __trace_graph_entry(tr, data, trace, flags, pc);
1237 /* Only do the atomic if it is not already set */
1238 if (!test_tsk_trace_graph(current))
1239 set_tsk_trace_graph(current);
1240 atomic_dec(&data->disabled);
1241 local_irq_restore(flags);
1246 void trace_graph_return(struct ftrace_graph_ret *trace)
1248 struct trace_array *tr = &global_trace;
1249 struct trace_array_cpu *data;
1250 unsigned long flags;
1255 local_irq_save(flags);
1256 cpu = raw_smp_processor_id();
1257 data = tr->data[cpu];
1258 disabled = atomic_inc_return(&data->disabled);
1259 if (likely(disabled == 1)) {
1260 pc = preempt_count();
1261 __trace_graph_return(tr, data, trace, flags, pc);
1264 clear_tsk_trace_graph(current);
1265 atomic_dec(&data->disabled);
1266 local_irq_restore(flags);
1268 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1270 static struct ftrace_ops trace_ops __read_mostly =
1272 .func = function_trace_call,
1275 void tracing_start_function_trace(void)
1277 ftrace_function_enabled = 0;
1279 if (trace_flags & TRACE_ITER_PREEMPTONLY)
1280 trace_ops.func = function_trace_call_preempt_only;
1282 trace_ops.func = function_trace_call;
1284 register_ftrace_function(&trace_ops);
1285 ftrace_function_enabled = 1;
1288 void tracing_stop_function_trace(void)
1290 ftrace_function_enabled = 0;
1291 unregister_ftrace_function(&trace_ops);
1295 enum trace_file_type {
1296 TRACE_FILE_LAT_FMT = 1,
1297 TRACE_FILE_ANNOTATE = 2,
1300 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1302 /* Don't allow ftrace to trace into the ring buffers */
1303 ftrace_disable_cpu();
1306 if (iter->buffer_iter[iter->cpu])
1307 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1309 ftrace_enable_cpu();
1312 static struct trace_entry *
1313 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1315 struct ring_buffer_event *event;
1316 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1318 /* Don't allow ftrace to trace into the ring buffers */
1319 ftrace_disable_cpu();
1322 event = ring_buffer_iter_peek(buf_iter, ts);
1324 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1326 ftrace_enable_cpu();
1328 return event ? ring_buffer_event_data(event) : NULL;
1331 static struct trace_entry *
1332 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1334 struct ring_buffer *buffer = iter->tr->buffer;
1335 struct trace_entry *ent, *next = NULL;
1336 u64 next_ts = 0, ts;
1340 for_each_tracing_cpu(cpu) {
1342 if (ring_buffer_empty_cpu(buffer, cpu))
1345 ent = peek_next_entry(iter, cpu, &ts);
1348 * Pick the entry with the smallest timestamp:
1350 if (ent && (!next || ts < next_ts)) {
1358 *ent_cpu = next_cpu;
1366 /* Find the next real entry, without updating the iterator itself */
1367 static struct trace_entry *
1368 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1370 return __find_next_entry(iter, ent_cpu, ent_ts);
1373 /* Find the next real entry, and increment the iterator to the next entry */
1374 static void *find_next_entry_inc(struct trace_iterator *iter)
1376 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1379 trace_iterator_increment(iter, iter->cpu);
1381 return iter->ent ? iter : NULL;
1384 static void trace_consume(struct trace_iterator *iter)
1386 /* Don't allow ftrace to trace into the ring buffers */
1387 ftrace_disable_cpu();
1388 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1389 ftrace_enable_cpu();
1392 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1394 struct trace_iterator *iter = m->private;
1400 /* can't go backwards */
1405 ent = find_next_entry_inc(iter);
1409 while (ent && iter->idx < i)
1410 ent = find_next_entry_inc(iter);
1417 static void *s_start(struct seq_file *m, loff_t *pos)
1419 struct trace_iterator *iter = m->private;
1424 mutex_lock(&trace_types_lock);
1426 if (!current_trace || current_trace != iter->trace) {
1427 mutex_unlock(&trace_types_lock);
1431 atomic_inc(&trace_record_cmdline_disabled);
1433 if (*pos != iter->pos) {
1438 ftrace_disable_cpu();
1440 for_each_tracing_cpu(cpu) {
1441 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1444 ftrace_enable_cpu();
1446 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1451 p = s_next(m, p, &l);
1457 static void s_stop(struct seq_file *m, void *p)
1459 atomic_dec(&trace_record_cmdline_disabled);
1460 mutex_unlock(&trace_types_lock);
1463 #ifdef CONFIG_KRETPROBES
1464 static inline const char *kretprobed(const char *name)
1466 static const char tramp_name[] = "kretprobe_trampoline";
1467 int size = sizeof(tramp_name);
1469 if (strncmp(tramp_name, name, size) == 0)
1470 return "[unknown/kretprobe'd]";
1474 static inline const char *kretprobed(const char *name)
1478 #endif /* CONFIG_KRETPROBES */
1481 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1483 #ifdef CONFIG_KALLSYMS
1484 char str[KSYM_SYMBOL_LEN];
1487 kallsyms_lookup(address, NULL, NULL, NULL, str);
1489 name = kretprobed(str);
1491 return trace_seq_printf(s, fmt, name);
1497 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1498 unsigned long address)
1500 #ifdef CONFIG_KALLSYMS
1501 char str[KSYM_SYMBOL_LEN];
1504 sprint_symbol(str, address);
1505 name = kretprobed(str);
1507 return trace_seq_printf(s, fmt, name);
1512 #ifndef CONFIG_64BIT
1513 # define IP_FMT "%08lx"
1515 # define IP_FMT "%016lx"
1519 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1524 return trace_seq_printf(s, "0");
1526 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1527 ret = seq_print_sym_offset(s, "%s", ip);
1529 ret = seq_print_sym_short(s, "%s", ip);
1534 if (sym_flags & TRACE_ITER_SYM_ADDR)
1535 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1539 static inline int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
1540 unsigned long ip, unsigned long sym_flags)
1542 struct file *file = NULL;
1543 unsigned long vmstart = 0;
1547 const struct vm_area_struct *vma;
1549 down_read(&mm->mmap_sem);
1550 vma = find_vma(mm, ip);
1552 file = vma->vm_file;
1553 vmstart = vma->vm_start;
1556 ret = trace_seq_path(s, &file->f_path);
1558 ret = trace_seq_printf(s, "[+0x%lx]", ip - vmstart);
1560 up_read(&mm->mmap_sem);
1562 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
1563 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1568 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
1569 unsigned long sym_flags)
1571 struct mm_struct *mm = NULL;
1575 if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
1576 struct task_struct *task;
1578 * we do the lookup on the thread group leader,
1579 * since individual threads might have already quit!
1582 task = find_task_by_vpid(entry->ent.tgid);
1584 mm = get_task_mm(task);
1588 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1589 unsigned long ip = entry->caller[i];
1591 if (ip == ULONG_MAX || !ret)
1594 ret = trace_seq_puts(s, " <- ");
1597 ret = trace_seq_puts(s, "??");
1603 ret = seq_print_user_ip(s, mm, ip, sym_flags);
1611 static void print_lat_help_header(struct seq_file *m)
1613 seq_puts(m, "# _------=> CPU# \n");
1614 seq_puts(m, "# / _-----=> irqs-off \n");
1615 seq_puts(m, "# | / _----=> need-resched \n");
1616 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1617 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1618 seq_puts(m, "# |||| / \n");
1619 seq_puts(m, "# ||||| delay \n");
1620 seq_puts(m, "# cmd pid ||||| time | caller \n");
1621 seq_puts(m, "# \\ / ||||| \\ | / \n");
1624 static void print_func_help_header(struct seq_file *m)
1626 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1627 seq_puts(m, "# | | | | |\n");
1632 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1634 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1635 struct trace_array *tr = iter->tr;
1636 struct trace_array_cpu *data = tr->data[tr->cpu];
1637 struct tracer *type = current_trace;
1638 unsigned long total;
1639 unsigned long entries;
1640 const char *name = "preemption";
1645 entries = ring_buffer_entries(iter->tr->buffer);
1647 ring_buffer_overruns(iter->tr->buffer);
1649 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1651 seq_puts(m, "-----------------------------------"
1652 "---------------------------------\n");
1653 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1654 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1655 nsecs_to_usecs(data->saved_latency),
1659 #if defined(CONFIG_PREEMPT_NONE)
1661 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1663 #elif defined(CONFIG_PREEMPT)
1668 /* These are reserved for later use */
1671 seq_printf(m, " #P:%d)\n", num_online_cpus());
1675 seq_puts(m, " -----------------\n");
1676 seq_printf(m, " | task: %.16s-%d "
1677 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1678 data->comm, data->pid, data->uid, data->nice,
1679 data->policy, data->rt_priority);
1680 seq_puts(m, " -----------------\n");
1682 if (data->critical_start) {
1683 seq_puts(m, " => started at: ");
1684 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1685 trace_print_seq(m, &iter->seq);
1686 seq_puts(m, "\n => ended at: ");
1687 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1688 trace_print_seq(m, &iter->seq);
1696 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1698 int hardirq, softirq;
1701 comm = trace_find_cmdline(entry->pid);
1703 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1704 trace_seq_printf(s, "%3d", cpu);
1705 trace_seq_printf(s, "%c%c",
1706 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1707 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1708 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1710 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1711 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1712 if (hardirq && softirq) {
1713 trace_seq_putc(s, 'H');
1716 trace_seq_putc(s, 'h');
1719 trace_seq_putc(s, 's');
1721 trace_seq_putc(s, '.');
1725 if (entry->preempt_count)
1726 trace_seq_printf(s, "%x", entry->preempt_count);
1728 trace_seq_puts(s, ".");
1731 unsigned long preempt_mark_thresh = 100;
1734 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1735 unsigned long rel_usecs)
1737 trace_seq_printf(s, " %4lldus", abs_usecs);
1738 if (rel_usecs > preempt_mark_thresh)
1739 trace_seq_puts(s, "!: ");
1740 else if (rel_usecs > 1)
1741 trace_seq_puts(s, "+: ");
1743 trace_seq_puts(s, " : ");
1746 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1749 * The message is supposed to contain an ending newline.
1750 * If the printing stops prematurely, try to add a newline of our own.
1752 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1754 struct trace_entry *ent;
1755 struct trace_field_cont *cont;
1758 ent = peek_next_entry(iter, iter->cpu, NULL);
1759 if (!ent || ent->type != TRACE_CONT) {
1760 trace_seq_putc(s, '\n');
1765 cont = (struct trace_field_cont *)ent;
1767 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1769 ftrace_disable_cpu();
1771 if (iter->buffer_iter[iter->cpu])
1772 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1774 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1776 ftrace_enable_cpu();
1778 ent = peek_next_entry(iter, iter->cpu, NULL);
1779 } while (ent && ent->type == TRACE_CONT);
1782 trace_seq_putc(s, '\n');
1785 static void test_cpu_buff_start(struct trace_iterator *iter)
1787 struct trace_seq *s = &iter->seq;
1789 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1792 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1795 if (cpu_isset(iter->cpu, iter->started))
1798 cpu_set(iter->cpu, iter->started);
1799 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1802 static enum print_line_t
1803 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1805 struct trace_seq *s = &iter->seq;
1806 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1807 struct trace_entry *next_entry;
1808 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1809 struct trace_entry *entry = iter->ent;
1810 unsigned long abs_usecs;
1811 unsigned long rel_usecs;
1818 if (entry->type == TRACE_CONT)
1819 return TRACE_TYPE_HANDLED;
1821 test_cpu_buff_start(iter);
1823 next_entry = find_next_entry(iter, NULL, &next_ts);
1826 rel_usecs = ns2usecs(next_ts - iter->ts);
1827 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1830 comm = trace_find_cmdline(entry->pid);
1831 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1832 " %ld.%03ldms (+%ld.%03ldms): ",
1834 entry->pid, cpu, entry->flags,
1835 entry->preempt_count, trace_idx,
1838 abs_usecs % 1000, rel_usecs/1000,
1841 lat_print_generic(s, entry, cpu);
1842 lat_print_timestamp(s, abs_usecs, rel_usecs);
1844 switch (entry->type) {
1846 struct ftrace_entry *field;
1848 trace_assign_type(field, entry);
1850 seq_print_ip_sym(s, field->ip, sym_flags);
1851 trace_seq_puts(s, " (");
1852 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1853 trace_seq_puts(s, ")\n");
1858 struct ctx_switch_entry *field;
1860 trace_assign_type(field, entry);
1862 T = field->next_state < sizeof(state_to_char) ?
1863 state_to_char[field->next_state] : 'X';
1865 state = field->prev_state ?
1866 __ffs(field->prev_state) + 1 : 0;
1867 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1868 comm = trace_find_cmdline(field->next_pid);
1869 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1872 S, entry->type == TRACE_CTX ? "==>" : " +",
1879 case TRACE_SPECIAL: {
1880 struct special_entry *field;
1882 trace_assign_type(field, entry);
1884 trace_seq_printf(s, "# %ld %ld %ld\n",
1891 struct stack_entry *field;
1893 trace_assign_type(field, entry);
1895 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1897 trace_seq_puts(s, " <= ");
1898 seq_print_ip_sym(s, field->caller[i], sym_flags);
1900 trace_seq_puts(s, "\n");
1904 struct print_entry *field;
1906 trace_assign_type(field, entry);
1908 seq_print_ip_sym(s, field->ip, sym_flags);
1909 trace_seq_printf(s, ": %s", field->buf);
1910 if (entry->flags & TRACE_FLAG_CONT)
1911 trace_seq_print_cont(s, iter);
1914 case TRACE_BRANCH: {
1915 struct trace_branch *field;
1917 trace_assign_type(field, entry);
1919 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1920 field->correct ? " ok " : " MISS ",
1926 case TRACE_USER_STACK: {
1927 struct userstack_entry *field;
1929 trace_assign_type(field, entry);
1931 seq_print_userip_objs(field, s, sym_flags);
1932 trace_seq_putc(s, '\n');
1936 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1938 return TRACE_TYPE_HANDLED;
1941 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1943 struct trace_seq *s = &iter->seq;
1944 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1945 struct trace_entry *entry;
1946 unsigned long usec_rem;
1947 unsigned long long t;
1956 if (entry->type == TRACE_CONT)
1957 return TRACE_TYPE_HANDLED;
1959 test_cpu_buff_start(iter);
1961 comm = trace_find_cmdline(iter->ent->pid);
1963 t = ns2usecs(iter->ts);
1964 usec_rem = do_div(t, 1000000ULL);
1965 secs = (unsigned long)t;
1967 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1969 return TRACE_TYPE_PARTIAL_LINE;
1970 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1972 return TRACE_TYPE_PARTIAL_LINE;
1973 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1975 return TRACE_TYPE_PARTIAL_LINE;
1977 switch (entry->type) {
1979 struct ftrace_entry *field;
1981 trace_assign_type(field, entry);
1983 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1985 return TRACE_TYPE_PARTIAL_LINE;
1986 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1988 ret = trace_seq_printf(s, " <-");
1990 return TRACE_TYPE_PARTIAL_LINE;
1991 ret = seq_print_ip_sym(s,
1995 return TRACE_TYPE_PARTIAL_LINE;
1997 ret = trace_seq_printf(s, "\n");
1999 return TRACE_TYPE_PARTIAL_LINE;
2004 struct ctx_switch_entry *field;
2006 trace_assign_type(field, entry);
2008 S = field->prev_state < sizeof(state_to_char) ?
2009 state_to_char[field->prev_state] : 'X';
2010 T = field->next_state < sizeof(state_to_char) ?
2011 state_to_char[field->next_state] : 'X';
2012 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
2016 entry->type == TRACE_CTX ? "==>" : " +",
2022 return TRACE_TYPE_PARTIAL_LINE;
2025 case TRACE_SPECIAL: {
2026 struct special_entry *field;
2028 trace_assign_type(field, entry);
2030 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
2035 return TRACE_TYPE_PARTIAL_LINE;
2039 struct stack_entry *field;
2041 trace_assign_type(field, entry);
2043 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
2045 ret = trace_seq_puts(s, " <= ");
2047 return TRACE_TYPE_PARTIAL_LINE;
2049 ret = seq_print_ip_sym(s, field->caller[i],
2052 return TRACE_TYPE_PARTIAL_LINE;
2054 ret = trace_seq_puts(s, "\n");
2056 return TRACE_TYPE_PARTIAL_LINE;
2060 struct print_entry *field;
2062 trace_assign_type(field, entry);
2064 seq_print_ip_sym(s, field->ip, sym_flags);
2065 trace_seq_printf(s, ": %s", field->buf);
2066 if (entry->flags & TRACE_FLAG_CONT)
2067 trace_seq_print_cont(s, iter);
2070 case TRACE_GRAPH_RET: {
2071 return print_graph_function(iter);
2073 case TRACE_GRAPH_ENT: {
2074 return print_graph_function(iter);
2076 case TRACE_BRANCH: {
2077 struct trace_branch *field;
2079 trace_assign_type(field, entry);
2081 trace_seq_printf(s, "[%s] %s:%s:%d\n",
2082 field->correct ? " ok " : " MISS ",
2088 case TRACE_USER_STACK: {
2089 struct userstack_entry *field;
2091 trace_assign_type(field, entry);
2093 ret = seq_print_userip_objs(field, s, sym_flags);
2095 return TRACE_TYPE_PARTIAL_LINE;
2096 ret = trace_seq_putc(s, '\n');
2098 return TRACE_TYPE_PARTIAL_LINE;
2102 return TRACE_TYPE_HANDLED;
2105 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2107 struct trace_seq *s = &iter->seq;
2108 struct trace_entry *entry;
2114 if (entry->type == TRACE_CONT)
2115 return TRACE_TYPE_HANDLED;
2117 ret = trace_seq_printf(s, "%d %d %llu ",
2118 entry->pid, iter->cpu, iter->ts);
2120 return TRACE_TYPE_PARTIAL_LINE;
2122 switch (entry->type) {
2124 struct ftrace_entry *field;
2126 trace_assign_type(field, entry);
2128 ret = trace_seq_printf(s, "%x %x\n",
2132 return TRACE_TYPE_PARTIAL_LINE;
2137 struct ctx_switch_entry *field;
2139 trace_assign_type(field, entry);
2141 S = field->prev_state < sizeof(state_to_char) ?
2142 state_to_char[field->prev_state] : 'X';
2143 T = field->next_state < sizeof(state_to_char) ?
2144 state_to_char[field->next_state] : 'X';
2145 if (entry->type == TRACE_WAKE)
2147 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
2156 return TRACE_TYPE_PARTIAL_LINE;
2160 case TRACE_USER_STACK:
2162 struct special_entry *field;
2164 trace_assign_type(field, entry);
2166 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
2171 return TRACE_TYPE_PARTIAL_LINE;
2175 struct print_entry *field;
2177 trace_assign_type(field, entry);
2179 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
2180 if (entry->flags & TRACE_FLAG_CONT)
2181 trace_seq_print_cont(s, iter);
2185 return TRACE_TYPE_HANDLED;
2188 #define SEQ_PUT_FIELD_RET(s, x) \
2190 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
2194 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
2196 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
2197 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
2201 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2203 struct trace_seq *s = &iter->seq;
2204 unsigned char newline = '\n';
2205 struct trace_entry *entry;
2210 if (entry->type == TRACE_CONT)
2211 return TRACE_TYPE_HANDLED;
2213 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2214 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2215 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2217 switch (entry->type) {
2219 struct ftrace_entry *field;
2221 trace_assign_type(field, entry);
2223 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
2224 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
2229 struct ctx_switch_entry *field;
2231 trace_assign_type(field, entry);
2233 S = field->prev_state < sizeof(state_to_char) ?
2234 state_to_char[field->prev_state] : 'X';
2235 T = field->next_state < sizeof(state_to_char) ?
2236 state_to_char[field->next_state] : 'X';
2237 if (entry->type == TRACE_WAKE)
2239 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
2240 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
2241 SEQ_PUT_HEX_FIELD_RET(s, S);
2242 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
2243 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
2244 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
2245 SEQ_PUT_HEX_FIELD_RET(s, T);
2249 case TRACE_USER_STACK:
2251 struct special_entry *field;
2253 trace_assign_type(field, entry);
2255 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
2256 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
2257 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
2261 SEQ_PUT_FIELD_RET(s, newline);
2263 return TRACE_TYPE_HANDLED;
2266 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2268 struct trace_seq *s = &iter->seq;
2269 struct trace_entry *entry;
2273 if (entry->type == TRACE_CONT)
2274 return TRACE_TYPE_HANDLED;
2276 SEQ_PUT_FIELD_RET(s, entry->pid);
2277 SEQ_PUT_FIELD_RET(s, entry->cpu);
2278 SEQ_PUT_FIELD_RET(s, iter->ts);
2280 switch (entry->type) {
2282 struct ftrace_entry *field;
2284 trace_assign_type(field, entry);
2286 SEQ_PUT_FIELD_RET(s, field->ip);
2287 SEQ_PUT_FIELD_RET(s, field->parent_ip);
2291 struct ctx_switch_entry *field;
2293 trace_assign_type(field, entry);
2295 SEQ_PUT_FIELD_RET(s, field->prev_pid);
2296 SEQ_PUT_FIELD_RET(s, field->prev_prio);
2297 SEQ_PUT_FIELD_RET(s, field->prev_state);
2298 SEQ_PUT_FIELD_RET(s, field->next_pid);
2299 SEQ_PUT_FIELD_RET(s, field->next_prio);
2300 SEQ_PUT_FIELD_RET(s, field->next_state);
2304 case TRACE_USER_STACK:
2306 struct special_entry *field;
2308 trace_assign_type(field, entry);
2310 SEQ_PUT_FIELD_RET(s, field->arg1);
2311 SEQ_PUT_FIELD_RET(s, field->arg2);
2312 SEQ_PUT_FIELD_RET(s, field->arg3);
2319 static int trace_empty(struct trace_iterator *iter)
2323 for_each_tracing_cpu(cpu) {
2324 if (iter->buffer_iter[cpu]) {
2325 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2328 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2336 static enum print_line_t print_trace_line(struct trace_iterator *iter)
2338 enum print_line_t ret;
2340 if (iter->trace && iter->trace->print_line) {
2341 ret = iter->trace->print_line(iter);
2342 if (ret != TRACE_TYPE_UNHANDLED)
2346 if (trace_flags & TRACE_ITER_BIN)
2347 return print_bin_fmt(iter);
2349 if (trace_flags & TRACE_ITER_HEX)
2350 return print_hex_fmt(iter);
2352 if (trace_flags & TRACE_ITER_RAW)
2353 return print_raw_fmt(iter);
2355 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2356 return print_lat_fmt(iter, iter->idx, iter->cpu);
2358 return print_trace_fmt(iter);
2361 static int s_show(struct seq_file *m, void *v)
2363 struct trace_iterator *iter = v;
2365 if (iter->ent == NULL) {
2367 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2370 if (iter->trace && iter->trace->print_header)
2371 iter->trace->print_header(m);
2372 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2373 /* print nothing if the buffers are empty */
2374 if (trace_empty(iter))
2376 print_trace_header(m, iter);
2377 if (!(trace_flags & TRACE_ITER_VERBOSE))
2378 print_lat_help_header(m);
2380 if (!(trace_flags & TRACE_ITER_VERBOSE))
2381 print_func_help_header(m);
2384 print_trace_line(iter);
2385 trace_print_seq(m, &iter->seq);
2391 static struct seq_operations tracer_seq_ops = {
2398 static struct trace_iterator *
2399 __tracing_open(struct inode *inode, struct file *file, int *ret)
2401 struct trace_iterator *iter;
2405 if (tracing_disabled) {
2410 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2416 mutex_lock(&trace_types_lock);
2417 if (current_trace && current_trace->print_max)
2420 iter->tr = inode->i_private;
2421 iter->trace = current_trace;
2424 /* Notify the tracer early; before we stop tracing. */
2425 if (iter->trace && iter->trace->open)
2426 iter->trace->open(iter);
2428 /* Annotate start of buffers if we had overruns */
2429 if (ring_buffer_overruns(iter->tr->buffer))
2430 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2433 for_each_tracing_cpu(cpu) {
2435 iter->buffer_iter[cpu] =
2436 ring_buffer_read_start(iter->tr->buffer, cpu);
2438 if (!iter->buffer_iter[cpu])
2442 /* TODO stop tracer */
2443 *ret = seq_open(file, &tracer_seq_ops);
2447 m = file->private_data;
2450 /* stop the trace while dumping */
2453 mutex_unlock(&trace_types_lock);
2459 for_each_tracing_cpu(cpu) {
2460 if (iter->buffer_iter[cpu])
2461 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2463 mutex_unlock(&trace_types_lock);
2466 return ERR_PTR(-ENOMEM);
2469 int tracing_open_generic(struct inode *inode, struct file *filp)
2471 if (tracing_disabled)
2474 filp->private_data = inode->i_private;
2478 int tracing_release(struct inode *inode, struct file *file)
2480 struct seq_file *m = (struct seq_file *)file->private_data;
2481 struct trace_iterator *iter = m->private;
2484 mutex_lock(&trace_types_lock);
2485 for_each_tracing_cpu(cpu) {
2486 if (iter->buffer_iter[cpu])
2487 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2490 if (iter->trace && iter->trace->close)
2491 iter->trace->close(iter);
2493 /* reenable tracing if it was previously enabled */
2495 mutex_unlock(&trace_types_lock);
2497 seq_release(inode, file);
2502 static int tracing_open(struct inode *inode, struct file *file)
2506 __tracing_open(inode, file, &ret);
2511 static int tracing_lt_open(struct inode *inode, struct file *file)
2513 struct trace_iterator *iter;
2516 iter = __tracing_open(inode, file, &ret);
2519 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2526 t_next(struct seq_file *m, void *v, loff_t *pos)
2528 struct tracer *t = m->private;
2540 static void *t_start(struct seq_file *m, loff_t *pos)
2542 struct tracer *t = m->private;
2545 mutex_lock(&trace_types_lock);
2546 for (; t && l < *pos; t = t_next(m, t, &l))
2552 static void t_stop(struct seq_file *m, void *p)
2554 mutex_unlock(&trace_types_lock);
2557 static int t_show(struct seq_file *m, void *v)
2559 struct tracer *t = v;
2564 seq_printf(m, "%s", t->name);
2573 static struct seq_operations show_traces_seq_ops = {
2580 static int show_traces_open(struct inode *inode, struct file *file)
2584 if (tracing_disabled)
2587 ret = seq_open(file, &show_traces_seq_ops);
2589 struct seq_file *m = file->private_data;
2590 m->private = trace_types;
2596 static struct file_operations tracing_fops = {
2597 .open = tracing_open,
2599 .llseek = seq_lseek,
2600 .release = tracing_release,
2603 static struct file_operations tracing_lt_fops = {
2604 .open = tracing_lt_open,
2606 .llseek = seq_lseek,
2607 .release = tracing_release,
2610 static struct file_operations show_traces_fops = {
2611 .open = show_traces_open,
2613 .release = seq_release,
2617 * Only trace on a CPU if the bitmask is set:
2619 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2622 * When tracing/tracing_cpu_mask is modified then this holds
2623 * the new bitmask we are about to install:
2625 static cpumask_t tracing_cpumask_new;
2628 * The tracer itself will not take this lock, but still we want
2629 * to provide a consistent cpumask to user-space:
2631 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2634 * Temporary storage for the character representation of the
2635 * CPU bitmask (and one more byte for the newline):
2637 static char mask_str[NR_CPUS + 1];
2640 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2641 size_t count, loff_t *ppos)
2645 mutex_lock(&tracing_cpumask_update_lock);
2647 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2648 if (count - len < 2) {
2652 len += sprintf(mask_str + len, "\n");
2653 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2656 mutex_unlock(&tracing_cpumask_update_lock);
2662 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2663 size_t count, loff_t *ppos)
2667 mutex_lock(&tracing_cpumask_update_lock);
2668 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2672 local_irq_disable();
2673 __raw_spin_lock(&ftrace_max_lock);
2674 for_each_tracing_cpu(cpu) {
2676 * Increase/decrease the disabled counter if we are
2677 * about to flip a bit in the cpumask:
2679 if (cpu_isset(cpu, tracing_cpumask) &&
2680 !cpu_isset(cpu, tracing_cpumask_new)) {
2681 atomic_inc(&global_trace.data[cpu]->disabled);
2683 if (!cpu_isset(cpu, tracing_cpumask) &&
2684 cpu_isset(cpu, tracing_cpumask_new)) {
2685 atomic_dec(&global_trace.data[cpu]->disabled);
2688 __raw_spin_unlock(&ftrace_max_lock);
2691 tracing_cpumask = tracing_cpumask_new;
2693 mutex_unlock(&tracing_cpumask_update_lock);
2698 mutex_unlock(&tracing_cpumask_update_lock);
2703 static struct file_operations tracing_cpumask_fops = {
2704 .open = tracing_open_generic,
2705 .read = tracing_cpumask_read,
2706 .write = tracing_cpumask_write,
2710 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2711 size_t cnt, loff_t *ppos)
2717 u32 tracer_flags = current_trace->flags->val;
2718 struct tracer_opt *trace_opts = current_trace->flags->opts;
2721 /* calulate max size */
2722 for (i = 0; trace_options[i]; i++) {
2723 len += strlen(trace_options[i]);
2724 len += 3; /* "no" and space */
2728 * Increase the size with names of options specific
2729 * of the current tracer.
2731 for (i = 0; trace_opts[i].name; i++) {
2732 len += strlen(trace_opts[i].name);
2733 len += 3; /* "no" and space */
2736 /* +2 for \n and \0 */
2737 buf = kmalloc(len + 2, GFP_KERNEL);
2741 for (i = 0; trace_options[i]; i++) {
2742 if (trace_flags & (1 << i))
2743 r += sprintf(buf + r, "%s ", trace_options[i]);
2745 r += sprintf(buf + r, "no%s ", trace_options[i]);
2748 for (i = 0; trace_opts[i].name; i++) {
2749 if (tracer_flags & trace_opts[i].bit)
2750 r += sprintf(buf + r, "%s ",
2751 trace_opts[i].name);
2753 r += sprintf(buf + r, "no%s ",
2754 trace_opts[i].name);
2757 r += sprintf(buf + r, "\n");
2758 WARN_ON(r >= len + 2);
2760 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2767 /* Try to assign a tracer specific option */
2768 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2770 struct tracer_flags *trace_flags = trace->flags;
2771 struct tracer_opt *opts = NULL;
2775 for (i = 0; trace_flags->opts[i].name; i++) {
2776 opts = &trace_flags->opts[i];
2777 len = strlen(opts->name);
2779 if (strncmp(cmp, opts->name, len) == 0) {
2780 ret = trace->set_flag(trace_flags->val,
2786 if (!trace_flags->opts[i].name)
2789 /* Refused to handle */
2794 trace_flags->val &= ~opts->bit;
2796 trace_flags->val |= opts->bit;
2802 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2803 size_t cnt, loff_t *ppos)
2811 if (cnt >= sizeof(buf))
2814 if (copy_from_user(&buf, ubuf, cnt))
2819 if (strncmp(buf, "no", 2) == 0) {
2824 for (i = 0; trace_options[i]; i++) {
2825 int len = strlen(trace_options[i]);
2827 if (strncmp(cmp, trace_options[i], len) == 0) {
2829 trace_flags &= ~(1 << i);
2831 trace_flags |= (1 << i);
2836 /* If no option could be set, test the specific tracer options */
2837 if (!trace_options[i]) {
2838 ret = set_tracer_option(current_trace, cmp, neg);
2848 static struct file_operations tracing_iter_fops = {
2849 .open = tracing_open_generic,
2850 .read = tracing_trace_options_read,
2851 .write = tracing_trace_options_write,
2854 static const char readme_msg[] =
2855 "tracing mini-HOWTO:\n\n"
2857 "# mount -t debugfs nodev /debug\n\n"
2858 "# cat /debug/tracing/available_tracers\n"
2859 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2860 "# cat /debug/tracing/current_tracer\n"
2862 "# echo sched_switch > /debug/tracing/current_tracer\n"
2863 "# cat /debug/tracing/current_tracer\n"
2865 "# cat /debug/tracing/trace_options\n"
2866 "noprint-parent nosym-offset nosym-addr noverbose\n"
2867 "# echo print-parent > /debug/tracing/trace_options\n"
2868 "# echo 1 > /debug/tracing/tracing_enabled\n"
2869 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2870 "echo 0 > /debug/tracing/tracing_enabled\n"
2874 tracing_readme_read(struct file *filp, char __user *ubuf,
2875 size_t cnt, loff_t *ppos)
2877 return simple_read_from_buffer(ubuf, cnt, ppos,
2878 readme_msg, strlen(readme_msg));
2881 static struct file_operations tracing_readme_fops = {
2882 .open = tracing_open_generic,
2883 .read = tracing_readme_read,
2887 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2888 size_t cnt, loff_t *ppos)
2893 r = sprintf(buf, "%u\n", tracer_enabled);
2894 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2898 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2899 size_t cnt, loff_t *ppos)
2901 struct trace_array *tr = filp->private_data;
2906 if (cnt >= sizeof(buf))
2909 if (copy_from_user(&buf, ubuf, cnt))
2914 ret = strict_strtoul(buf, 10, &val);
2920 mutex_lock(&trace_types_lock);
2921 if (tracer_enabled ^ val) {
2924 if (current_trace->start)
2925 current_trace->start(tr);
2930 if (current_trace->stop)
2931 current_trace->stop(tr);
2934 mutex_unlock(&trace_types_lock);
2942 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2943 size_t cnt, loff_t *ppos)
2945 char buf[max_tracer_type_len+2];
2948 mutex_lock(&trace_types_lock);
2950 r = sprintf(buf, "%s\n", current_trace->name);
2952 r = sprintf(buf, "\n");
2953 mutex_unlock(&trace_types_lock);
2955 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2958 static int tracing_set_tracer(char *buf)
2960 struct trace_array *tr = &global_trace;
2964 mutex_lock(&trace_types_lock);
2965 for (t = trace_types; t; t = t->next) {
2966 if (strcmp(t->name, buf) == 0)
2973 if (t == current_trace)
2976 trace_branch_disable();
2977 if (current_trace && current_trace->reset)
2978 current_trace->reset(tr);
2987 trace_branch_enable(tr);
2989 mutex_unlock(&trace_types_lock);
2995 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2996 size_t cnt, loff_t *ppos)
2998 char buf[max_tracer_type_len+1];
3005 if (cnt > max_tracer_type_len)
3006 cnt = max_tracer_type_len;
3008 if (copy_from_user(&buf, ubuf, cnt))
3013 /* strip ending whitespace. */
3014 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3017 err = tracing_set_tracer(buf);
3027 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3028 size_t cnt, loff_t *ppos)
3030 unsigned long *ptr = filp->private_data;
3034 r = snprintf(buf, sizeof(buf), "%ld\n",
3035 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3036 if (r > sizeof(buf))
3038 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3042 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3043 size_t cnt, loff_t *ppos)
3045 long *ptr = filp->private_data;
3050 if (cnt >= sizeof(buf))
3053 if (copy_from_user(&buf, ubuf, cnt))
3058 ret = strict_strtoul(buf, 10, &val);
3067 static atomic_t tracing_reader;
3069 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3071 struct trace_iterator *iter;
3073 if (tracing_disabled)
3076 /* We only allow for reader of the pipe */
3077 if (atomic_inc_return(&tracing_reader) != 1) {
3078 atomic_dec(&tracing_reader);
3082 /* create a buffer to store the information to pass to userspace */
3083 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3087 mutex_lock(&trace_types_lock);
3089 /* trace pipe does not show start of buffer */
3090 cpus_setall(iter->started);
3092 iter->tr = &global_trace;
3093 iter->trace = current_trace;
3094 filp->private_data = iter;
3096 if (iter->trace->pipe_open)
3097 iter->trace->pipe_open(iter);
3098 mutex_unlock(&trace_types_lock);
3103 static int tracing_release_pipe(struct inode *inode, struct file *file)
3105 struct trace_iterator *iter = file->private_data;
3108 atomic_dec(&tracing_reader);
3114 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3116 struct trace_iterator *iter = filp->private_data;
3118 if (trace_flags & TRACE_ITER_BLOCK) {
3120 * Always select as readable when in blocking mode
3122 return POLLIN | POLLRDNORM;
3124 if (!trace_empty(iter))
3125 return POLLIN | POLLRDNORM;
3126 poll_wait(filp, &trace_wait, poll_table);
3127 if (!trace_empty(iter))
3128 return POLLIN | POLLRDNORM;
3138 tracing_read_pipe(struct file *filp, char __user *ubuf,
3139 size_t cnt, loff_t *ppos)
3141 struct trace_iterator *iter = filp->private_data;
3144 /* return any leftover data */
3145 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3149 trace_seq_reset(&iter->seq);
3151 mutex_lock(&trace_types_lock);
3152 if (iter->trace->read) {
3153 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3160 while (trace_empty(iter)) {
3162 if ((filp->f_flags & O_NONBLOCK)) {
3168 * This is a make-shift waitqueue. The reason we don't use
3169 * an actual wait queue is because:
3170 * 1) we only ever have one waiter
3171 * 2) the tracing, traces all functions, we don't want
3172 * the overhead of calling wake_up and friends
3173 * (and tracing them too)
3174 * Anyway, this is really very primitive wakeup.
3176 set_current_state(TASK_INTERRUPTIBLE);
3177 iter->tr->waiter = current;
3179 mutex_unlock(&trace_types_lock);
3181 /* sleep for 100 msecs, and try again. */
3182 schedule_timeout(HZ/10);
3184 mutex_lock(&trace_types_lock);
3186 iter->tr->waiter = NULL;
3188 if (signal_pending(current)) {
3193 if (iter->trace != current_trace)
3197 * We block until we read something and tracing is disabled.
3198 * We still block if tracing is disabled, but we have never
3199 * read anything. This allows a user to cat this file, and
3200 * then enable tracing. But after we have read something,
3201 * we give an EOF when tracing is again disabled.
3203 * iter->pos will be 0 if we haven't read anything.
3205 if (!tracer_enabled && iter->pos)
3211 /* stop when tracing is finished */
3212 if (trace_empty(iter))
3215 if (cnt >= PAGE_SIZE)
3216 cnt = PAGE_SIZE - 1;
3218 /* reset all but tr, trace, and overruns */
3219 memset(&iter->seq, 0,
3220 sizeof(struct trace_iterator) -
3221 offsetof(struct trace_iterator, seq));
3224 while (find_next_entry_inc(iter) != NULL) {
3225 enum print_line_t ret;
3226 int len = iter->seq.len;
3228 ret = print_trace_line(iter);
3229 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3230 /* don't print partial lines */
3231 iter->seq.len = len;
3235 trace_consume(iter);
3237 if (iter->seq.len >= cnt)
3241 /* Now copy what we have to the user */
3242 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3243 if (iter->seq.readpos >= iter->seq.len)
3244 trace_seq_reset(&iter->seq);
3247 * If there was nothing to send to user, inspite of consuming trace
3248 * entries, go back to wait for more entries.
3254 mutex_unlock(&trace_types_lock);
3260 tracing_entries_read(struct file *filp, char __user *ubuf,
3261 size_t cnt, loff_t *ppos)
3263 struct trace_array *tr = filp->private_data;
3267 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3268 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3272 tracing_entries_write(struct file *filp, const char __user *ubuf,
3273 size_t cnt, loff_t *ppos)
3279 if (cnt >= sizeof(buf))
3282 if (copy_from_user(&buf, ubuf, cnt))
3287 ret = strict_strtoul(buf, 10, &val);
3291 /* must have at least 1 entry */
3295 mutex_lock(&trace_types_lock);
3299 /* disable all cpu buffers */
3300 for_each_tracing_cpu(cpu) {
3301 if (global_trace.data[cpu])
3302 atomic_inc(&global_trace.data[cpu]->disabled);
3303 if (max_tr.data[cpu])
3304 atomic_inc(&max_tr.data[cpu]->disabled);
3307 /* value is in KB */
3310 if (val != global_trace.entries) {
3311 ret = ring_buffer_resize(global_trace.buffer, val);
3317 ret = ring_buffer_resize(max_tr.buffer, val);
3321 r = ring_buffer_resize(global_trace.buffer,
3322 global_trace.entries);
3324 /* AARGH! We are left with different
3325 * size max buffer!!!! */
3327 tracing_disabled = 1;
3332 global_trace.entries = val;
3337 /* If check pages failed, return ENOMEM */
3338 if (tracing_disabled)
3341 for_each_tracing_cpu(cpu) {
3342 if (global_trace.data[cpu])
3343 atomic_dec(&global_trace.data[cpu]->disabled);
3344 if (max_tr.data[cpu])
3345 atomic_dec(&max_tr.data[cpu]->disabled);
3349 max_tr.entries = global_trace.entries;
3350 mutex_unlock(&trace_types_lock);
3355 static int mark_printk(const char *fmt, ...)
3359 va_start(args, fmt);
3360 ret = trace_vprintk(0, -1, fmt, args);
3366 tracing_mark_write(struct file *filp, const char __user *ubuf,
3367 size_t cnt, loff_t *fpos)
3372 if (tracing_disabled)
3375 if (cnt > TRACE_BUF_SIZE)
3376 cnt = TRACE_BUF_SIZE;
3378 buf = kmalloc(cnt + 1, GFP_KERNEL);
3382 if (copy_from_user(buf, ubuf, cnt)) {
3387 /* Cut from the first nil or newline. */
3389 end = strchr(buf, '\n');
3393 cnt = mark_printk("%s\n", buf);
3400 static struct file_operations tracing_max_lat_fops = {
3401 .open = tracing_open_generic,
3402 .read = tracing_max_lat_read,
3403 .write = tracing_max_lat_write,
3406 static struct file_operations tracing_ctrl_fops = {
3407 .open = tracing_open_generic,
3408 .read = tracing_ctrl_read,
3409 .write = tracing_ctrl_write,
3412 static struct file_operations set_tracer_fops = {
3413 .open = tracing_open_generic,
3414 .read = tracing_set_trace_read,
3415 .write = tracing_set_trace_write,
3418 static struct file_operations tracing_pipe_fops = {
3419 .open = tracing_open_pipe,
3420 .poll = tracing_poll_pipe,
3421 .read = tracing_read_pipe,
3422 .release = tracing_release_pipe,
3425 static struct file_operations tracing_entries_fops = {
3426 .open = tracing_open_generic,
3427 .read = tracing_entries_read,
3428 .write = tracing_entries_write,
3431 static struct file_operations tracing_mark_fops = {
3432 .open = tracing_open_generic,
3433 .write = tracing_mark_write,
3436 #ifdef CONFIG_DYNAMIC_FTRACE
3438 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3444 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3445 size_t cnt, loff_t *ppos)
3447 static char ftrace_dyn_info_buffer[1024];
3448 static DEFINE_MUTEX(dyn_info_mutex);
3449 unsigned long *p = filp->private_data;
3450 char *buf = ftrace_dyn_info_buffer;
3451 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3454 mutex_lock(&dyn_info_mutex);
3455 r = sprintf(buf, "%ld ", *p);
3457 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3460 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3462 mutex_unlock(&dyn_info_mutex);
3467 static struct file_operations tracing_dyn_info_fops = {
3468 .open = tracing_open_generic,
3469 .read = tracing_read_dyn_info,
3473 static struct dentry *d_tracer;
3475 struct dentry *tracing_init_dentry(void)
3482 d_tracer = debugfs_create_dir("tracing", NULL);
3484 if (!d_tracer && !once) {
3486 pr_warning("Could not create debugfs directory 'tracing'\n");
3493 #ifdef CONFIG_FTRACE_SELFTEST
3494 /* Let selftest have access to static functions in this file */
3495 #include "trace_selftest.c"
3498 static __init int tracer_init_debugfs(void)
3500 struct dentry *d_tracer;
3501 struct dentry *entry;
3503 d_tracer = tracing_init_dentry();
3505 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3506 &global_trace, &tracing_ctrl_fops);
3508 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3510 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3511 NULL, &tracing_iter_fops);
3513 pr_warning("Could not create debugfs 'trace_options' entry\n");
3515 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3516 NULL, &tracing_cpumask_fops);
3518 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3520 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3521 &global_trace, &tracing_lt_fops);
3523 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3525 entry = debugfs_create_file("trace", 0444, d_tracer,
3526 &global_trace, &tracing_fops);
3528 pr_warning("Could not create debugfs 'trace' entry\n");
3530 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3531 &global_trace, &show_traces_fops);
3533 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3535 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3536 &global_trace, &set_tracer_fops);
3538 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3540 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3541 &tracing_max_latency,
3542 &tracing_max_lat_fops);
3544 pr_warning("Could not create debugfs "
3545 "'tracing_max_latency' entry\n");
3547 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3548 &tracing_thresh, &tracing_max_lat_fops);
3550 pr_warning("Could not create debugfs "
3551 "'tracing_thresh' entry\n");
3552 entry = debugfs_create_file("README", 0644, d_tracer,
3553 NULL, &tracing_readme_fops);
3555 pr_warning("Could not create debugfs 'README' entry\n");
3557 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3558 NULL, &tracing_pipe_fops);
3560 pr_warning("Could not create debugfs "
3561 "'trace_pipe' entry\n");
3563 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3564 &global_trace, &tracing_entries_fops);
3566 pr_warning("Could not create debugfs "
3567 "'buffer_size_kb' entry\n");
3569 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3570 NULL, &tracing_mark_fops);
3572 pr_warning("Could not create debugfs "
3573 "'trace_marker' entry\n");
3575 #ifdef CONFIG_DYNAMIC_FTRACE
3576 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3577 &ftrace_update_tot_cnt,
3578 &tracing_dyn_info_fops);
3580 pr_warning("Could not create debugfs "
3581 "'dyn_ftrace_total_info' entry\n");
3583 #ifdef CONFIG_SYSPROF_TRACER
3584 init_tracer_sysprof_debugfs(d_tracer);
3589 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
3592 * Raw Spinlock because a normal spinlock would be traced here
3593 * and append an irrelevant couple spin_lock_irqsave/
3594 * spin_unlock_irqrestore traced by ftrace around this
3595 * TRACE_PRINTK trace.
3597 static raw_spinlock_t trace_buf_lock =
3598 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3599 static char trace_buf[TRACE_BUF_SIZE];
3601 struct ring_buffer_event *event;
3602 struct trace_array *tr = &global_trace;
3603 struct trace_array_cpu *data;
3604 struct print_entry *entry;
3605 unsigned long flags, irq_flags;
3606 int cpu, len = 0, size, pc;
3608 if (tracing_disabled || atomic_read(&tracing_selftest_running))
3611 pc = preempt_count();
3612 preempt_disable_notrace();
3613 cpu = raw_smp_processor_id();
3614 data = tr->data[cpu];
3616 if (unlikely(atomic_read(&data->disabled)))
3619 local_irq_save(flags);
3620 __raw_spin_lock(&trace_buf_lock);
3621 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3623 len = min(len, TRACE_BUF_SIZE-1);
3626 size = sizeof(*entry) + len + 1;
3627 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3630 entry = ring_buffer_event_data(event);
3631 tracing_generic_entry_update(&entry->ent, flags, pc);
3632 entry->ent.type = TRACE_PRINT;
3634 entry->depth = depth;
3636 memcpy(&entry->buf, trace_buf, len);
3637 entry->buf[len] = 0;
3638 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3641 __raw_spin_unlock(&trace_buf_lock);
3642 local_irq_restore(flags);
3645 preempt_enable_notrace();
3649 EXPORT_SYMBOL_GPL(trace_vprintk);
3651 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3656 if (!(trace_flags & TRACE_ITER_PRINTK))
3661 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3662 ret = trace_vprintk(ip, current->curr_ret_stack, fmt, ap);
3664 ret = trace_vprintk(ip, -1, fmt, ap);
3670 EXPORT_SYMBOL_GPL(__ftrace_printk);
3672 static int trace_panic_handler(struct notifier_block *this,
3673 unsigned long event, void *unused)
3675 if (ftrace_dump_on_oops)
3680 static struct notifier_block trace_panic_notifier = {
3681 .notifier_call = trace_panic_handler,
3683 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3686 static int trace_die_handler(struct notifier_block *self,
3692 if (ftrace_dump_on_oops)
3701 static struct notifier_block trace_die_notifier = {
3702 .notifier_call = trace_die_handler,
3707 * printk is set to max of 1024, we really don't need it that big.
3708 * Nothing should be printing 1000 characters anyway.
3710 #define TRACE_MAX_PRINT 1000
3713 * Define here KERN_TRACE so that we have one place to modify
3714 * it if we decide to change what log level the ftrace dump
3717 #define KERN_TRACE KERN_INFO
3720 trace_printk_seq(struct trace_seq *s)
3722 /* Probably should print a warning here. */
3726 /* should be zero ended, but we are paranoid. */
3727 s->buffer[s->len] = 0;
3729 printk(KERN_TRACE "%s", s->buffer);
3734 void ftrace_dump(void)
3736 static DEFINE_SPINLOCK(ftrace_dump_lock);
3737 /* use static because iter can be a bit big for the stack */
3738 static struct trace_iterator iter;
3739 static cpumask_t mask;
3740 static int dump_ran;
3741 unsigned long flags;
3745 spin_lock_irqsave(&ftrace_dump_lock, flags);
3751 /* No turning back! */
3754 for_each_tracing_cpu(cpu) {
3755 atomic_inc(&global_trace.data[cpu]->disabled);
3758 /* don't look at user memory in panic mode */
3759 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3761 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3763 iter.tr = &global_trace;
3764 iter.trace = current_trace;
3767 * We need to stop all tracing on all CPUS to read the
3768 * the next buffer. This is a bit expensive, but is
3769 * not done often. We fill all what we can read,
3770 * and then release the locks again.
3775 while (!trace_empty(&iter)) {
3778 printk(KERN_TRACE "---------------------------------\n");
3782 /* reset all but tr, trace, and overruns */
3783 memset(&iter.seq, 0,
3784 sizeof(struct trace_iterator) -
3785 offsetof(struct trace_iterator, seq));
3786 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3789 if (find_next_entry_inc(&iter) != NULL) {
3790 print_trace_line(&iter);
3791 trace_consume(&iter);
3794 trace_printk_seq(&iter.seq);
3798 printk(KERN_TRACE " (ftrace buffer empty)\n");
3800 printk(KERN_TRACE "---------------------------------\n");
3803 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3806 __init static int tracer_alloc_buffers(void)
3808 struct trace_array_cpu *data;
3811 /* TODO: make the number of buffers hot pluggable with CPUS */
3812 tracing_buffer_mask = cpu_possible_map;
3814 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3815 TRACE_BUFFER_FLAGS);
3816 if (!global_trace.buffer) {
3817 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3821 global_trace.entries = ring_buffer_size(global_trace.buffer);
3823 #ifdef CONFIG_TRACER_MAX_TRACE
3824 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3825 TRACE_BUFFER_FLAGS);
3826 if (!max_tr.buffer) {
3827 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3829 ring_buffer_free(global_trace.buffer);
3832 max_tr.entries = ring_buffer_size(max_tr.buffer);
3833 WARN_ON(max_tr.entries != global_trace.entries);
3836 /* Allocate the first page for all buffers */
3837 for_each_tracing_cpu(i) {
3838 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3839 max_tr.data[i] = &per_cpu(max_data, i);
3842 trace_init_cmdlines();
3844 register_tracer(&nop_trace);
3845 #ifdef CONFIG_BOOT_TRACER
3846 register_tracer(&boot_tracer);
3847 current_trace = &boot_tracer;
3848 current_trace->init(&global_trace);
3850 current_trace = &nop_trace;
3853 /* All seems OK, enable tracing */
3854 tracing_disabled = 0;
3856 atomic_notifier_chain_register(&panic_notifier_list,
3857 &trace_panic_notifier);
3859 register_die_notifier(&trace_die_notifier);
3863 early_initcall(tracer_alloc_buffers);
3864 fs_initcall(tracer_init_debugfs);