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;
48 * We need to change this state when a selftest is running.
49 * A selftest will lurk into the ring-buffer to count the
50 * entries inserted during the selftest although some concurrent
51 * insertions into the ring-buffer such as ftrace_printk could occurred
52 * at the same time, giving false positive or negative results.
54 static bool __read_mostly tracing_selftest_running;
56 /* For tracers that don't implement custom flags */
57 static struct tracer_opt dummy_tracer_opt[] = {
61 static struct tracer_flags dummy_tracer_flags = {
63 .opts = dummy_tracer_opt
66 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
72 * Kill all tracing for good (never come back).
73 * It is initialized to 1 but will turn to zero if the initialization
74 * of the tracer is successful. But that is the only place that sets
77 int tracing_disabled = 1;
79 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
81 static inline void ftrace_disable_cpu(void)
84 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
87 static inline void ftrace_enable_cpu(void)
89 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
93 static cpumask_t __read_mostly tracing_buffer_mask;
95 #define for_each_tracing_cpu(cpu) \
96 for_each_cpu_mask(cpu, tracing_buffer_mask)
99 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
101 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
102 * is set, then ftrace_dump is called. This will output the contents
103 * of the ftrace buffers to the console. This is very useful for
104 * capturing traces that lead to crashes and outputing it to a
107 * It is default off, but you can enable it with either specifying
108 * "ftrace_dump_on_oops" in the kernel command line, or setting
109 * /proc/sys/kernel/ftrace_dump_on_oops to true.
111 int ftrace_dump_on_oops;
113 static int tracing_set_tracer(char *buf);
115 static int __init set_ftrace(char *str)
117 tracing_set_tracer(str);
120 __setup("ftrace", set_ftrace);
122 static int __init set_ftrace_dump_on_oops(char *str)
124 ftrace_dump_on_oops = 1;
127 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
130 ns2usecs(cycle_t nsec)
137 cycle_t ftrace_now(int cpu)
139 u64 ts = ring_buffer_time_stamp(cpu);
140 ring_buffer_normalize_time_stamp(cpu, &ts);
145 * The global_trace is the descriptor that holds the tracing
146 * buffers for the live tracing. For each CPU, it contains
147 * a link list of pages that will store trace entries. The
148 * page descriptor of the pages in the memory is used to hold
149 * the link list by linking the lru item in the page descriptor
150 * to each of the pages in the buffer per CPU.
152 * For each active CPU there is a data field that holds the
153 * pages for the buffer for that CPU. Each CPU has the same number
154 * of pages allocated for its buffer.
156 static struct trace_array global_trace;
158 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
161 * The max_tr is used to snapshot the global_trace when a maximum
162 * latency is reached. Some tracers will use this to store a maximum
163 * trace while it continues examining live traces.
165 * The buffers for the max_tr are set up the same as the global_trace.
166 * When a snapshot is taken, the link list of the max_tr is swapped
167 * with the link list of the global_trace and the buffers are reset for
168 * the global_trace so the tracing can continue.
170 static struct trace_array max_tr;
172 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
174 /* tracer_enabled is used to toggle activation of a tracer */
175 static int tracer_enabled = 1;
178 * tracing_is_enabled - return tracer_enabled status
180 * This function is used by other tracers to know the status
181 * of the tracer_enabled flag. Tracers may use this function
182 * to know if it should enable their features when starting
183 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
185 int tracing_is_enabled(void)
187 return tracer_enabled;
190 /* function tracing enabled */
191 int ftrace_function_enabled;
194 * trace_buf_size is the size in bytes that is allocated
195 * for a buffer. Note, the number of bytes is always rounded
198 * This number is purposely set to a low number of 16384.
199 * If the dump on oops happens, it will be much appreciated
200 * to not have to wait for all that output. Anyway this can be
201 * boot time and run time configurable.
203 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
205 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
207 /* trace_types holds a link list of available tracers. */
208 static struct tracer *trace_types __read_mostly;
210 /* current_trace points to the tracer that is currently active */
211 static struct tracer *current_trace __read_mostly;
214 * max_tracer_type_len is used to simplify the allocating of
215 * buffers to read userspace tracer names. We keep track of
216 * the longest tracer name registered.
218 static int max_tracer_type_len;
221 * trace_types_lock is used to protect the trace_types list.
222 * This lock is also used to keep user access serialized.
223 * Accesses from userspace will grab this lock while userspace
224 * activities happen inside the kernel.
226 static DEFINE_MUTEX(trace_types_lock);
228 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
229 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
231 /* trace_flags holds trace_options default values */
232 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
236 * trace_wake_up - wake up tasks waiting for trace input
238 * Simply wakes up any task that is blocked on the trace_wait
239 * queue. These is used with trace_poll for tasks polling the trace.
241 void trace_wake_up(void)
244 * The runqueue_is_locked() can fail, but this is the best we
247 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
248 wake_up(&trace_wait);
251 static int __init set_buf_size(char *str)
253 unsigned long buf_size;
258 ret = strict_strtoul(str, 0, &buf_size);
259 /* nr_entries can not be zero */
260 if (ret < 0 || buf_size == 0)
262 trace_buf_size = buf_size;
265 __setup("trace_buf_size=", set_buf_size);
267 unsigned long nsecs_to_usecs(unsigned long nsecs)
272 /* These must match the bit postions in trace_iterator_flags */
273 static const char *trace_options[] = {
294 * ftrace_max_lock is used to protect the swapping of buffers
295 * when taking a max snapshot. The buffers themselves are
296 * protected by per_cpu spinlocks. But the action of the swap
297 * needs its own lock.
299 * This is defined as a raw_spinlock_t in order to help
300 * with performance when lockdep debugging is enabled.
302 static raw_spinlock_t ftrace_max_lock =
303 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
306 * Copy the new maximum trace into the separate maximum-trace
307 * structure. (this way the maximum trace is permanently saved,
308 * for later retrieval via /debugfs/tracing/latency_trace)
311 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
313 struct trace_array_cpu *data = tr->data[cpu];
316 max_tr.time_start = data->preempt_timestamp;
318 data = max_tr.data[cpu];
319 data->saved_latency = tracing_max_latency;
321 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
322 data->pid = tsk->pid;
323 data->uid = tsk->uid;
324 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
325 data->policy = tsk->policy;
326 data->rt_priority = tsk->rt_priority;
328 /* record this tasks comm */
329 tracing_record_cmdline(current);
333 * trace_seq_printf - sequence printing of trace information
334 * @s: trace sequence descriptor
335 * @fmt: printf format string
337 * The tracer may use either sequence operations or its own
338 * copy to user routines. To simplify formating of a trace
339 * trace_seq_printf is used to store strings into a special
340 * buffer (@s). Then the output may be either used by
341 * the sequencer or pulled into another buffer.
344 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
346 int len = (PAGE_SIZE - 1) - s->len;
354 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
357 /* If we can't write it all, don't bother writing anything */
367 * trace_seq_puts - trace sequence printing of simple string
368 * @s: trace sequence descriptor
369 * @str: simple string to record
371 * The tracer may use either the sequence operations or its own
372 * copy to user routines. This function records a simple string
373 * into a special buffer (@s) for later retrieval by a sequencer
374 * or other mechanism.
377 trace_seq_puts(struct trace_seq *s, const char *str)
379 int len = strlen(str);
381 if (len > ((PAGE_SIZE - 1) - s->len))
384 memcpy(s->buffer + s->len, str, len);
391 trace_seq_putc(struct trace_seq *s, unsigned char c)
393 if (s->len >= (PAGE_SIZE - 1))
396 s->buffer[s->len++] = c;
402 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
404 if (len > ((PAGE_SIZE - 1) - s->len))
407 memcpy(s->buffer + s->len, mem, len);
413 #define MAX_MEMHEX_BYTES 8
414 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
417 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
419 unsigned char hex[HEX_CHARS];
420 unsigned char *data = mem;
424 for (i = 0, j = 0; i < len; i++) {
426 for (i = len-1, j = 0; i >= 0; i--) {
428 hex[j++] = hex_asc_hi(data[i]);
429 hex[j++] = hex_asc_lo(data[i]);
433 return trace_seq_putmem(s, hex, j);
437 trace_seq_path(struct trace_seq *s, struct path *path)
441 if (s->len >= (PAGE_SIZE - 1))
443 p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
445 p = mangle_path(s->buffer + s->len, p, "\n");
447 s->len = p - s->buffer;
451 s->buffer[s->len++] = '?';
459 trace_seq_reset(struct trace_seq *s)
465 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
470 if (s->len <= s->readpos)
473 len = s->len - s->readpos;
476 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
485 trace_print_seq(struct seq_file *m, struct trace_seq *s)
487 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
490 seq_puts(m, s->buffer);
496 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
498 * @tsk: the task with the latency
499 * @cpu: The cpu that initiated the trace.
501 * Flip the buffers between the @tr and the max_tr and record information
502 * about which task was the cause of this latency.
505 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
507 struct ring_buffer *buf = tr->buffer;
509 WARN_ON_ONCE(!irqs_disabled());
510 __raw_spin_lock(&ftrace_max_lock);
512 tr->buffer = max_tr.buffer;
515 ftrace_disable_cpu();
516 ring_buffer_reset(tr->buffer);
519 __update_max_tr(tr, tsk, cpu);
520 __raw_spin_unlock(&ftrace_max_lock);
524 * update_max_tr_single - only copy one trace over, and reset the rest
526 * @tsk - task with the latency
527 * @cpu - the cpu of the buffer to copy.
529 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
532 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
536 WARN_ON_ONCE(!irqs_disabled());
537 __raw_spin_lock(&ftrace_max_lock);
539 ftrace_disable_cpu();
541 ring_buffer_reset(max_tr.buffer);
542 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
548 __update_max_tr(tr, tsk, cpu);
549 __raw_spin_unlock(&ftrace_max_lock);
553 * register_tracer - register a tracer with the ftrace system.
554 * @type - the plugin for the tracer
556 * Register a new plugin tracer.
558 int register_tracer(struct tracer *type)
565 pr_info("Tracer must have a name\n");
570 * When this gets called we hold the BKL which means that
571 * preemption is disabled. Various trace selftests however
572 * need to disable and enable preemption for successful tests.
573 * So we drop the BKL here and grab it after the tests again.
576 mutex_lock(&trace_types_lock);
578 tracing_selftest_running = true;
580 for (t = trace_types; t; t = t->next) {
581 if (strcmp(type->name, t->name) == 0) {
583 pr_info("Trace %s already registered\n",
591 type->set_flag = &dummy_set_flag;
593 type->flags = &dummy_tracer_flags;
595 if (!type->flags->opts)
596 type->flags->opts = dummy_tracer_opt;
598 #ifdef CONFIG_FTRACE_STARTUP_TEST
599 if (type->selftest) {
600 struct tracer *saved_tracer = current_trace;
601 struct trace_array *tr = &global_trace;
605 * Run a selftest on this tracer.
606 * Here we reset the trace buffer, and set the current
607 * tracer to be this tracer. The tracer can then run some
608 * internal tracing to verify that everything is in order.
609 * If we fail, we do not register this tracer.
611 for_each_tracing_cpu(i)
612 tracing_reset(tr, i);
614 current_trace = type;
615 /* the test is responsible for initializing and enabling */
616 pr_info("Testing tracer %s: ", type->name);
617 ret = type->selftest(type, tr);
618 /* the test is responsible for resetting too */
619 current_trace = saved_tracer;
621 printk(KERN_CONT "FAILED!\n");
624 /* Only reset on passing, to avoid touching corrupted buffers */
625 for_each_tracing_cpu(i)
626 tracing_reset(tr, i);
628 printk(KERN_CONT "PASSED\n");
632 type->next = trace_types;
634 len = strlen(type->name);
635 if (len > max_tracer_type_len)
636 max_tracer_type_len = len;
639 tracing_selftest_running = false;
640 mutex_unlock(&trace_types_lock);
646 void unregister_tracer(struct tracer *type)
651 mutex_lock(&trace_types_lock);
652 for (t = &trace_types; *t; t = &(*t)->next) {
656 pr_info("Trace %s not registered\n", type->name);
661 if (strlen(type->name) != max_tracer_type_len)
664 max_tracer_type_len = 0;
665 for (t = &trace_types; *t; t = &(*t)->next) {
666 len = strlen((*t)->name);
667 if (len > max_tracer_type_len)
668 max_tracer_type_len = len;
671 mutex_unlock(&trace_types_lock);
674 void tracing_reset(struct trace_array *tr, int cpu)
676 ftrace_disable_cpu();
677 ring_buffer_reset_cpu(tr->buffer, cpu);
681 #define SAVED_CMDLINES 128
682 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
683 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
684 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
685 static int cmdline_idx;
686 static DEFINE_SPINLOCK(trace_cmdline_lock);
688 /* temporary disable recording */
689 atomic_t trace_record_cmdline_disabled __read_mostly;
691 static void trace_init_cmdlines(void)
693 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
694 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
698 static int trace_stop_count;
699 static DEFINE_SPINLOCK(tracing_start_lock);
702 * ftrace_off_permanent - disable all ftrace code permanently
704 * This should only be called when a serious anomally has
705 * been detected. This will turn off the function tracing,
706 * ring buffers, and other tracing utilites. It takes no
707 * locks and can be called from any context.
709 void ftrace_off_permanent(void)
711 tracing_disabled = 1;
713 tracing_off_permanent();
717 * tracing_start - quick start of the tracer
719 * If tracing is enabled but was stopped by tracing_stop,
720 * this will start the tracer back up.
722 void tracing_start(void)
724 struct ring_buffer *buffer;
727 if (tracing_disabled)
730 spin_lock_irqsave(&tracing_start_lock, flags);
731 if (--trace_stop_count)
734 if (trace_stop_count < 0) {
735 /* Someone screwed up their debugging */
737 trace_stop_count = 0;
742 buffer = global_trace.buffer;
744 ring_buffer_record_enable(buffer);
746 buffer = max_tr.buffer;
748 ring_buffer_record_enable(buffer);
752 spin_unlock_irqrestore(&tracing_start_lock, flags);
756 * tracing_stop - quick stop of the tracer
758 * Light weight way to stop tracing. Use in conjunction with
761 void tracing_stop(void)
763 struct ring_buffer *buffer;
767 spin_lock_irqsave(&tracing_start_lock, flags);
768 if (trace_stop_count++)
771 buffer = global_trace.buffer;
773 ring_buffer_record_disable(buffer);
775 buffer = max_tr.buffer;
777 ring_buffer_record_disable(buffer);
780 spin_unlock_irqrestore(&tracing_start_lock, flags);
783 void trace_stop_cmdline_recording(void);
785 static void trace_save_cmdline(struct task_struct *tsk)
790 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
794 * It's not the end of the world if we don't get
795 * the lock, but we also don't want to spin
796 * nor do we want to disable interrupts,
797 * so if we miss here, then better luck next time.
799 if (!spin_trylock(&trace_cmdline_lock))
802 idx = map_pid_to_cmdline[tsk->pid];
803 if (idx >= SAVED_CMDLINES) {
804 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
806 map = map_cmdline_to_pid[idx];
807 if (map <= PID_MAX_DEFAULT)
808 map_pid_to_cmdline[map] = (unsigned)-1;
810 map_pid_to_cmdline[tsk->pid] = idx;
815 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
817 spin_unlock(&trace_cmdline_lock);
820 char *trace_find_cmdline(int pid)
822 char *cmdline = "<...>";
828 if (pid > PID_MAX_DEFAULT)
831 map = map_pid_to_cmdline[pid];
832 if (map >= SAVED_CMDLINES)
835 cmdline = saved_cmdlines[map];
841 void tracing_record_cmdline(struct task_struct *tsk)
843 if (atomic_read(&trace_record_cmdline_disabled))
846 trace_save_cmdline(tsk);
850 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
853 struct task_struct *tsk = current;
855 entry->preempt_count = pc & 0xff;
856 entry->pid = (tsk) ? tsk->pid : 0;
857 entry->tgid = (tsk) ? tsk->tgid : 0;
859 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
860 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
862 TRACE_FLAG_IRQS_NOSUPPORT |
864 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
865 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
866 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
870 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
871 unsigned long ip, unsigned long parent_ip, unsigned long flags,
874 struct ring_buffer_event *event;
875 struct ftrace_entry *entry;
876 unsigned long irq_flags;
878 /* If we are reading the ring buffer, don't trace */
879 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
882 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
886 entry = ring_buffer_event_data(event);
887 tracing_generic_entry_update(&entry->ent, flags, pc);
888 entry->ent.type = TRACE_FN;
890 entry->parent_ip = parent_ip;
891 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
894 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
895 static void __trace_graph_entry(struct trace_array *tr,
896 struct trace_array_cpu *data,
897 struct ftrace_graph_ent *trace,
901 struct ring_buffer_event *event;
902 struct ftrace_graph_ent_entry *entry;
903 unsigned long irq_flags;
905 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
908 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
912 entry = ring_buffer_event_data(event);
913 tracing_generic_entry_update(&entry->ent, flags, pc);
914 entry->ent.type = TRACE_GRAPH_ENT;
915 entry->graph_ent = *trace;
916 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
919 static void __trace_graph_return(struct trace_array *tr,
920 struct trace_array_cpu *data,
921 struct ftrace_graph_ret *trace,
925 struct ring_buffer_event *event;
926 struct ftrace_graph_ret_entry *entry;
927 unsigned long irq_flags;
929 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
932 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
936 entry = ring_buffer_event_data(event);
937 tracing_generic_entry_update(&entry->ent, flags, pc);
938 entry->ent.type = TRACE_GRAPH_RET;
940 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
945 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
946 unsigned long ip, unsigned long parent_ip, unsigned long flags,
949 if (likely(!atomic_read(&data->disabled)))
950 trace_function(tr, data, ip, parent_ip, flags, pc);
953 static void ftrace_trace_stack(struct trace_array *tr,
954 struct trace_array_cpu *data,
958 #ifdef CONFIG_STACKTRACE
959 struct ring_buffer_event *event;
960 struct stack_entry *entry;
961 struct stack_trace trace;
962 unsigned long irq_flags;
964 if (!(trace_flags & TRACE_ITER_STACKTRACE))
967 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
971 entry = ring_buffer_event_data(event);
972 tracing_generic_entry_update(&entry->ent, flags, pc);
973 entry->ent.type = TRACE_STACK;
975 memset(&entry->caller, 0, sizeof(entry->caller));
977 trace.nr_entries = 0;
978 trace.max_entries = FTRACE_STACK_ENTRIES;
980 trace.entries = entry->caller;
982 save_stack_trace(&trace);
983 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
987 void __trace_stack(struct trace_array *tr,
988 struct trace_array_cpu *data,
992 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
995 static void ftrace_trace_userstack(struct trace_array *tr,
996 struct trace_array_cpu *data,
997 unsigned long flags, int pc)
999 #ifdef CONFIG_STACKTRACE
1000 struct ring_buffer_event *event;
1001 struct userstack_entry *entry;
1002 struct stack_trace trace;
1003 unsigned long irq_flags;
1005 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1008 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1012 entry = ring_buffer_event_data(event);
1013 tracing_generic_entry_update(&entry->ent, flags, pc);
1014 entry->ent.type = TRACE_USER_STACK;
1016 memset(&entry->caller, 0, sizeof(entry->caller));
1018 trace.nr_entries = 0;
1019 trace.max_entries = FTRACE_STACK_ENTRIES;
1021 trace.entries = entry->caller;
1023 save_stack_trace_user(&trace);
1024 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1028 void __trace_userstack(struct trace_array *tr,
1029 struct trace_array_cpu *data,
1030 unsigned long flags)
1032 ftrace_trace_userstack(tr, data, flags, preempt_count());
1036 ftrace_trace_special(void *__tr, void *__data,
1037 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1040 struct ring_buffer_event *event;
1041 struct trace_array_cpu *data = __data;
1042 struct trace_array *tr = __tr;
1043 struct special_entry *entry;
1044 unsigned long irq_flags;
1046 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1050 entry = ring_buffer_event_data(event);
1051 tracing_generic_entry_update(&entry->ent, 0, pc);
1052 entry->ent.type = TRACE_SPECIAL;
1056 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1057 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
1058 ftrace_trace_userstack(tr, data, irq_flags, pc);
1064 __trace_special(void *__tr, void *__data,
1065 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1067 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
1071 tracing_sched_switch_trace(struct trace_array *tr,
1072 struct trace_array_cpu *data,
1073 struct task_struct *prev,
1074 struct task_struct *next,
1075 unsigned long flags, int pc)
1077 struct ring_buffer_event *event;
1078 struct ctx_switch_entry *entry;
1079 unsigned long irq_flags;
1081 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1085 entry = ring_buffer_event_data(event);
1086 tracing_generic_entry_update(&entry->ent, flags, pc);
1087 entry->ent.type = TRACE_CTX;
1088 entry->prev_pid = prev->pid;
1089 entry->prev_prio = prev->prio;
1090 entry->prev_state = prev->state;
1091 entry->next_pid = next->pid;
1092 entry->next_prio = next->prio;
1093 entry->next_state = next->state;
1094 entry->next_cpu = task_cpu(next);
1095 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1096 ftrace_trace_stack(tr, data, flags, 5, pc);
1097 ftrace_trace_userstack(tr, data, flags, pc);
1101 tracing_sched_wakeup_trace(struct trace_array *tr,
1102 struct trace_array_cpu *data,
1103 struct task_struct *wakee,
1104 struct task_struct *curr,
1105 unsigned long flags, int pc)
1107 struct ring_buffer_event *event;
1108 struct ctx_switch_entry *entry;
1109 unsigned long irq_flags;
1111 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1115 entry = ring_buffer_event_data(event);
1116 tracing_generic_entry_update(&entry->ent, flags, pc);
1117 entry->ent.type = TRACE_WAKE;
1118 entry->prev_pid = curr->pid;
1119 entry->prev_prio = curr->prio;
1120 entry->prev_state = curr->state;
1121 entry->next_pid = wakee->pid;
1122 entry->next_prio = wakee->prio;
1123 entry->next_state = wakee->state;
1124 entry->next_cpu = task_cpu(wakee);
1125 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1126 ftrace_trace_stack(tr, data, flags, 6, pc);
1127 ftrace_trace_userstack(tr, data, flags, pc);
1133 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1135 struct trace_array *tr = &global_trace;
1136 struct trace_array_cpu *data;
1137 unsigned long flags;
1141 if (tracing_disabled)
1144 pc = preempt_count();
1145 local_irq_save(flags);
1146 cpu = raw_smp_processor_id();
1147 data = tr->data[cpu];
1149 if (likely(atomic_inc_return(&data->disabled) == 1))
1150 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1152 atomic_dec(&data->disabled);
1153 local_irq_restore(flags);
1156 #ifdef CONFIG_FUNCTION_TRACER
1158 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
1160 struct trace_array *tr = &global_trace;
1161 struct trace_array_cpu *data;
1162 unsigned long flags;
1167 if (unlikely(!ftrace_function_enabled))
1170 pc = preempt_count();
1171 resched = ftrace_preempt_disable();
1172 local_save_flags(flags);
1173 cpu = raw_smp_processor_id();
1174 data = tr->data[cpu];
1175 disabled = atomic_inc_return(&data->disabled);
1177 if (likely(disabled == 1))
1178 trace_function(tr, data, ip, parent_ip, flags, pc);
1180 atomic_dec(&data->disabled);
1181 ftrace_preempt_enable(resched);
1185 function_trace_call(unsigned long ip, unsigned long parent_ip)
1187 struct trace_array *tr = &global_trace;
1188 struct trace_array_cpu *data;
1189 unsigned long flags;
1194 if (unlikely(!ftrace_function_enabled))
1198 * Need to use raw, since this must be called before the
1199 * recursive protection is performed.
1201 local_irq_save(flags);
1202 cpu = raw_smp_processor_id();
1203 data = tr->data[cpu];
1204 disabled = atomic_inc_return(&data->disabled);
1206 if (likely(disabled == 1)) {
1207 pc = preempt_count();
1208 trace_function(tr, data, ip, parent_ip, flags, pc);
1211 atomic_dec(&data->disabled);
1212 local_irq_restore(flags);
1215 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1216 int trace_graph_entry(struct ftrace_graph_ent *trace)
1218 struct trace_array *tr = &global_trace;
1219 struct trace_array_cpu *data;
1220 unsigned long flags;
1225 if (!ftrace_trace_task(current))
1228 if (!ftrace_graph_addr(trace->func))
1231 local_irq_save(flags);
1232 cpu = raw_smp_processor_id();
1233 data = tr->data[cpu];
1234 disabled = atomic_inc_return(&data->disabled);
1235 if (likely(disabled == 1)) {
1236 pc = preempt_count();
1237 __trace_graph_entry(tr, data, trace, flags, pc);
1239 /* Only do the atomic if it is not already set */
1240 if (!test_tsk_trace_graph(current))
1241 set_tsk_trace_graph(current);
1242 atomic_dec(&data->disabled);
1243 local_irq_restore(flags);
1248 void trace_graph_return(struct ftrace_graph_ret *trace)
1250 struct trace_array *tr = &global_trace;
1251 struct trace_array_cpu *data;
1252 unsigned long flags;
1257 local_irq_save(flags);
1258 cpu = raw_smp_processor_id();
1259 data = tr->data[cpu];
1260 disabled = atomic_inc_return(&data->disabled);
1261 if (likely(disabled == 1)) {
1262 pc = preempt_count();
1263 __trace_graph_return(tr, data, trace, flags, pc);
1266 clear_tsk_trace_graph(current);
1267 atomic_dec(&data->disabled);
1268 local_irq_restore(flags);
1270 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1272 static struct ftrace_ops trace_ops __read_mostly =
1274 .func = function_trace_call,
1277 void tracing_start_function_trace(void)
1279 ftrace_function_enabled = 0;
1281 if (trace_flags & TRACE_ITER_PREEMPTONLY)
1282 trace_ops.func = function_trace_call_preempt_only;
1284 trace_ops.func = function_trace_call;
1286 register_ftrace_function(&trace_ops);
1287 ftrace_function_enabled = 1;
1290 void tracing_stop_function_trace(void)
1292 ftrace_function_enabled = 0;
1293 unregister_ftrace_function(&trace_ops);
1297 enum trace_file_type {
1298 TRACE_FILE_LAT_FMT = 1,
1299 TRACE_FILE_ANNOTATE = 2,
1302 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1304 /* Don't allow ftrace to trace into the ring buffers */
1305 ftrace_disable_cpu();
1308 if (iter->buffer_iter[iter->cpu])
1309 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1311 ftrace_enable_cpu();
1314 static struct trace_entry *
1315 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1317 struct ring_buffer_event *event;
1318 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1320 /* Don't allow ftrace to trace into the ring buffers */
1321 ftrace_disable_cpu();
1324 event = ring_buffer_iter_peek(buf_iter, ts);
1326 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1328 ftrace_enable_cpu();
1330 return event ? ring_buffer_event_data(event) : NULL;
1333 static struct trace_entry *
1334 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1336 struct ring_buffer *buffer = iter->tr->buffer;
1337 struct trace_entry *ent, *next = NULL;
1338 u64 next_ts = 0, ts;
1342 for_each_tracing_cpu(cpu) {
1344 if (ring_buffer_empty_cpu(buffer, cpu))
1347 ent = peek_next_entry(iter, cpu, &ts);
1350 * Pick the entry with the smallest timestamp:
1352 if (ent && (!next || ts < next_ts)) {
1360 *ent_cpu = next_cpu;
1368 /* Find the next real entry, without updating the iterator itself */
1369 static struct trace_entry *
1370 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1372 return __find_next_entry(iter, ent_cpu, ent_ts);
1375 /* Find the next real entry, and increment the iterator to the next entry */
1376 static void *find_next_entry_inc(struct trace_iterator *iter)
1378 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1381 trace_iterator_increment(iter, iter->cpu);
1383 return iter->ent ? iter : NULL;
1386 static void trace_consume(struct trace_iterator *iter)
1388 /* Don't allow ftrace to trace into the ring buffers */
1389 ftrace_disable_cpu();
1390 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1391 ftrace_enable_cpu();
1394 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1396 struct trace_iterator *iter = m->private;
1402 /* can't go backwards */
1407 ent = find_next_entry_inc(iter);
1411 while (ent && iter->idx < i)
1412 ent = find_next_entry_inc(iter);
1419 static void *s_start(struct seq_file *m, loff_t *pos)
1421 struct trace_iterator *iter = m->private;
1426 mutex_lock(&trace_types_lock);
1428 if (!current_trace || current_trace != iter->trace) {
1429 mutex_unlock(&trace_types_lock);
1433 atomic_inc(&trace_record_cmdline_disabled);
1435 if (*pos != iter->pos) {
1440 ftrace_disable_cpu();
1442 for_each_tracing_cpu(cpu) {
1443 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1446 ftrace_enable_cpu();
1448 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1453 p = s_next(m, p, &l);
1459 static void s_stop(struct seq_file *m, void *p)
1461 atomic_dec(&trace_record_cmdline_disabled);
1462 mutex_unlock(&trace_types_lock);
1465 #ifdef CONFIG_KRETPROBES
1466 static inline const char *kretprobed(const char *name)
1468 static const char tramp_name[] = "kretprobe_trampoline";
1469 int size = sizeof(tramp_name);
1471 if (strncmp(tramp_name, name, size) == 0)
1472 return "[unknown/kretprobe'd]";
1476 static inline const char *kretprobed(const char *name)
1480 #endif /* CONFIG_KRETPROBES */
1483 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1485 #ifdef CONFIG_KALLSYMS
1486 char str[KSYM_SYMBOL_LEN];
1489 kallsyms_lookup(address, NULL, NULL, NULL, str);
1491 name = kretprobed(str);
1493 return trace_seq_printf(s, fmt, name);
1499 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1500 unsigned long address)
1502 #ifdef CONFIG_KALLSYMS
1503 char str[KSYM_SYMBOL_LEN];
1506 sprint_symbol(str, address);
1507 name = kretprobed(str);
1509 return trace_seq_printf(s, fmt, name);
1514 #ifndef CONFIG_64BIT
1515 # define IP_FMT "%08lx"
1517 # define IP_FMT "%016lx"
1521 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1526 return trace_seq_printf(s, "0");
1528 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1529 ret = seq_print_sym_offset(s, "%s", ip);
1531 ret = seq_print_sym_short(s, "%s", ip);
1536 if (sym_flags & TRACE_ITER_SYM_ADDR)
1537 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1541 static inline int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
1542 unsigned long ip, unsigned long sym_flags)
1544 struct file *file = NULL;
1545 unsigned long vmstart = 0;
1549 const struct vm_area_struct *vma;
1551 down_read(&mm->mmap_sem);
1552 vma = find_vma(mm, ip);
1554 file = vma->vm_file;
1555 vmstart = vma->vm_start;
1558 ret = trace_seq_path(s, &file->f_path);
1560 ret = trace_seq_printf(s, "[+0x%lx]", ip - vmstart);
1562 up_read(&mm->mmap_sem);
1564 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
1565 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1570 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
1571 unsigned long sym_flags)
1573 struct mm_struct *mm = NULL;
1577 if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
1578 struct task_struct *task;
1580 * we do the lookup on the thread group leader,
1581 * since individual threads might have already quit!
1584 task = find_task_by_vpid(entry->ent.tgid);
1586 mm = get_task_mm(task);
1590 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1591 unsigned long ip = entry->caller[i];
1593 if (ip == ULONG_MAX || !ret)
1596 ret = trace_seq_puts(s, " <- ");
1599 ret = trace_seq_puts(s, "??");
1605 ret = seq_print_user_ip(s, mm, ip, sym_flags);
1613 static void print_lat_help_header(struct seq_file *m)
1615 seq_puts(m, "# _------=> CPU# \n");
1616 seq_puts(m, "# / _-----=> irqs-off \n");
1617 seq_puts(m, "# | / _----=> need-resched \n");
1618 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1619 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1620 seq_puts(m, "# |||| / \n");
1621 seq_puts(m, "# ||||| delay \n");
1622 seq_puts(m, "# cmd pid ||||| time | caller \n");
1623 seq_puts(m, "# \\ / ||||| \\ | / \n");
1626 static void print_func_help_header(struct seq_file *m)
1628 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1629 seq_puts(m, "# | | | | |\n");
1634 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1636 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1637 struct trace_array *tr = iter->tr;
1638 struct trace_array_cpu *data = tr->data[tr->cpu];
1639 struct tracer *type = current_trace;
1640 unsigned long total;
1641 unsigned long entries;
1642 const char *name = "preemption";
1647 entries = ring_buffer_entries(iter->tr->buffer);
1649 ring_buffer_overruns(iter->tr->buffer);
1651 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1653 seq_puts(m, "-----------------------------------"
1654 "---------------------------------\n");
1655 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1656 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1657 nsecs_to_usecs(data->saved_latency),
1661 #if defined(CONFIG_PREEMPT_NONE)
1663 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1665 #elif defined(CONFIG_PREEMPT)
1670 /* These are reserved for later use */
1673 seq_printf(m, " #P:%d)\n", num_online_cpus());
1677 seq_puts(m, " -----------------\n");
1678 seq_printf(m, " | task: %.16s-%d "
1679 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1680 data->comm, data->pid, data->uid, data->nice,
1681 data->policy, data->rt_priority);
1682 seq_puts(m, " -----------------\n");
1684 if (data->critical_start) {
1685 seq_puts(m, " => started at: ");
1686 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1687 trace_print_seq(m, &iter->seq);
1688 seq_puts(m, "\n => ended at: ");
1689 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1690 trace_print_seq(m, &iter->seq);
1698 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1700 int hardirq, softirq;
1703 comm = trace_find_cmdline(entry->pid);
1705 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1706 trace_seq_printf(s, "%3d", cpu);
1707 trace_seq_printf(s, "%c%c",
1708 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1709 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1710 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1712 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1713 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1714 if (hardirq && softirq) {
1715 trace_seq_putc(s, 'H');
1718 trace_seq_putc(s, 'h');
1721 trace_seq_putc(s, 's');
1723 trace_seq_putc(s, '.');
1727 if (entry->preempt_count)
1728 trace_seq_printf(s, "%x", entry->preempt_count);
1730 trace_seq_puts(s, ".");
1733 unsigned long preempt_mark_thresh = 100;
1736 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1737 unsigned long rel_usecs)
1739 trace_seq_printf(s, " %4lldus", abs_usecs);
1740 if (rel_usecs > preempt_mark_thresh)
1741 trace_seq_puts(s, "!: ");
1742 else if (rel_usecs > 1)
1743 trace_seq_puts(s, "+: ");
1745 trace_seq_puts(s, " : ");
1748 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1751 * The message is supposed to contain an ending newline.
1752 * If the printing stops prematurely, try to add a newline of our own.
1754 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1756 struct trace_entry *ent;
1757 struct trace_field_cont *cont;
1760 ent = peek_next_entry(iter, iter->cpu, NULL);
1761 if (!ent || ent->type != TRACE_CONT) {
1762 trace_seq_putc(s, '\n');
1767 cont = (struct trace_field_cont *)ent;
1769 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1771 ftrace_disable_cpu();
1773 if (iter->buffer_iter[iter->cpu])
1774 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1776 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1778 ftrace_enable_cpu();
1780 ent = peek_next_entry(iter, iter->cpu, NULL);
1781 } while (ent && ent->type == TRACE_CONT);
1784 trace_seq_putc(s, '\n');
1787 static void test_cpu_buff_start(struct trace_iterator *iter)
1789 struct trace_seq *s = &iter->seq;
1791 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1794 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1797 if (cpu_isset(iter->cpu, iter->started))
1800 cpu_set(iter->cpu, iter->started);
1801 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1804 static enum print_line_t
1805 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1807 struct trace_seq *s = &iter->seq;
1808 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1809 struct trace_entry *next_entry;
1810 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1811 struct trace_entry *entry = iter->ent;
1812 unsigned long abs_usecs;
1813 unsigned long rel_usecs;
1820 if (entry->type == TRACE_CONT)
1821 return TRACE_TYPE_HANDLED;
1823 test_cpu_buff_start(iter);
1825 next_entry = find_next_entry(iter, NULL, &next_ts);
1828 rel_usecs = ns2usecs(next_ts - iter->ts);
1829 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1832 comm = trace_find_cmdline(entry->pid);
1833 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1834 " %ld.%03ldms (+%ld.%03ldms): ",
1836 entry->pid, cpu, entry->flags,
1837 entry->preempt_count, trace_idx,
1840 abs_usecs % 1000, rel_usecs/1000,
1843 lat_print_generic(s, entry, cpu);
1844 lat_print_timestamp(s, abs_usecs, rel_usecs);
1846 switch (entry->type) {
1848 struct ftrace_entry *field;
1850 trace_assign_type(field, entry);
1852 seq_print_ip_sym(s, field->ip, sym_flags);
1853 trace_seq_puts(s, " (");
1854 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1855 trace_seq_puts(s, ")\n");
1860 struct ctx_switch_entry *field;
1862 trace_assign_type(field, entry);
1864 T = field->next_state < sizeof(state_to_char) ?
1865 state_to_char[field->next_state] : 'X';
1867 state = field->prev_state ?
1868 __ffs(field->prev_state) + 1 : 0;
1869 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1870 comm = trace_find_cmdline(field->next_pid);
1871 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1874 S, entry->type == TRACE_CTX ? "==>" : " +",
1881 case TRACE_SPECIAL: {
1882 struct special_entry *field;
1884 trace_assign_type(field, entry);
1886 trace_seq_printf(s, "# %ld %ld %ld\n",
1893 struct stack_entry *field;
1895 trace_assign_type(field, entry);
1897 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1899 trace_seq_puts(s, " <= ");
1900 seq_print_ip_sym(s, field->caller[i], sym_flags);
1902 trace_seq_puts(s, "\n");
1906 struct print_entry *field;
1908 trace_assign_type(field, entry);
1910 seq_print_ip_sym(s, field->ip, sym_flags);
1911 trace_seq_printf(s, ": %s", field->buf);
1912 if (entry->flags & TRACE_FLAG_CONT)
1913 trace_seq_print_cont(s, iter);
1916 case TRACE_BRANCH: {
1917 struct trace_branch *field;
1919 trace_assign_type(field, entry);
1921 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1922 field->correct ? " ok " : " MISS ",
1928 case TRACE_USER_STACK: {
1929 struct userstack_entry *field;
1931 trace_assign_type(field, entry);
1933 seq_print_userip_objs(field, s, sym_flags);
1934 trace_seq_putc(s, '\n');
1938 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1940 return TRACE_TYPE_HANDLED;
1943 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1945 struct trace_seq *s = &iter->seq;
1946 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1947 struct trace_entry *entry;
1948 unsigned long usec_rem;
1949 unsigned long long t;
1958 if (entry->type == TRACE_CONT)
1959 return TRACE_TYPE_HANDLED;
1961 test_cpu_buff_start(iter);
1963 comm = trace_find_cmdline(iter->ent->pid);
1965 t = ns2usecs(iter->ts);
1966 usec_rem = do_div(t, 1000000ULL);
1967 secs = (unsigned long)t;
1969 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1971 return TRACE_TYPE_PARTIAL_LINE;
1972 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1974 return TRACE_TYPE_PARTIAL_LINE;
1975 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1977 return TRACE_TYPE_PARTIAL_LINE;
1979 switch (entry->type) {
1981 struct ftrace_entry *field;
1983 trace_assign_type(field, entry);
1985 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1987 return TRACE_TYPE_PARTIAL_LINE;
1988 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1990 ret = trace_seq_printf(s, " <-");
1992 return TRACE_TYPE_PARTIAL_LINE;
1993 ret = seq_print_ip_sym(s,
1997 return TRACE_TYPE_PARTIAL_LINE;
1999 ret = trace_seq_printf(s, "\n");
2001 return TRACE_TYPE_PARTIAL_LINE;
2006 struct ctx_switch_entry *field;
2008 trace_assign_type(field, entry);
2010 S = field->prev_state < sizeof(state_to_char) ?
2011 state_to_char[field->prev_state] : 'X';
2012 T = field->next_state < sizeof(state_to_char) ?
2013 state_to_char[field->next_state] : 'X';
2014 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
2018 entry->type == TRACE_CTX ? "==>" : " +",
2024 return TRACE_TYPE_PARTIAL_LINE;
2027 case TRACE_SPECIAL: {
2028 struct special_entry *field;
2030 trace_assign_type(field, entry);
2032 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
2037 return TRACE_TYPE_PARTIAL_LINE;
2041 struct stack_entry *field;
2043 trace_assign_type(field, entry);
2045 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
2047 ret = trace_seq_puts(s, " <= ");
2049 return TRACE_TYPE_PARTIAL_LINE;
2051 ret = seq_print_ip_sym(s, field->caller[i],
2054 return TRACE_TYPE_PARTIAL_LINE;
2056 ret = trace_seq_puts(s, "\n");
2058 return TRACE_TYPE_PARTIAL_LINE;
2062 struct print_entry *field;
2064 trace_assign_type(field, entry);
2066 seq_print_ip_sym(s, field->ip, sym_flags);
2067 trace_seq_printf(s, ": %s", field->buf);
2068 if (entry->flags & TRACE_FLAG_CONT)
2069 trace_seq_print_cont(s, iter);
2072 case TRACE_GRAPH_RET: {
2073 return print_graph_function(iter);
2075 case TRACE_GRAPH_ENT: {
2076 return print_graph_function(iter);
2078 case TRACE_BRANCH: {
2079 struct trace_branch *field;
2081 trace_assign_type(field, entry);
2083 trace_seq_printf(s, "[%s] %s:%s:%d\n",
2084 field->correct ? " ok " : " MISS ",
2090 case TRACE_USER_STACK: {
2091 struct userstack_entry *field;
2093 trace_assign_type(field, entry);
2095 ret = seq_print_userip_objs(field, s, sym_flags);
2097 return TRACE_TYPE_PARTIAL_LINE;
2098 ret = trace_seq_putc(s, '\n');
2100 return TRACE_TYPE_PARTIAL_LINE;
2104 return TRACE_TYPE_HANDLED;
2107 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2109 struct trace_seq *s = &iter->seq;
2110 struct trace_entry *entry;
2116 if (entry->type == TRACE_CONT)
2117 return TRACE_TYPE_HANDLED;
2119 ret = trace_seq_printf(s, "%d %d %llu ",
2120 entry->pid, iter->cpu, iter->ts);
2122 return TRACE_TYPE_PARTIAL_LINE;
2124 switch (entry->type) {
2126 struct ftrace_entry *field;
2128 trace_assign_type(field, entry);
2130 ret = trace_seq_printf(s, "%x %x\n",
2134 return TRACE_TYPE_PARTIAL_LINE;
2139 struct ctx_switch_entry *field;
2141 trace_assign_type(field, entry);
2143 S = field->prev_state < sizeof(state_to_char) ?
2144 state_to_char[field->prev_state] : 'X';
2145 T = field->next_state < sizeof(state_to_char) ?
2146 state_to_char[field->next_state] : 'X';
2147 if (entry->type == TRACE_WAKE)
2149 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
2158 return TRACE_TYPE_PARTIAL_LINE;
2162 case TRACE_USER_STACK:
2164 struct special_entry *field;
2166 trace_assign_type(field, entry);
2168 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
2173 return TRACE_TYPE_PARTIAL_LINE;
2177 struct print_entry *field;
2179 trace_assign_type(field, entry);
2181 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
2182 if (entry->flags & TRACE_FLAG_CONT)
2183 trace_seq_print_cont(s, iter);
2187 return TRACE_TYPE_HANDLED;
2190 #define SEQ_PUT_FIELD_RET(s, x) \
2192 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
2196 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
2198 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
2199 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
2203 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2205 struct trace_seq *s = &iter->seq;
2206 unsigned char newline = '\n';
2207 struct trace_entry *entry;
2212 if (entry->type == TRACE_CONT)
2213 return TRACE_TYPE_HANDLED;
2215 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2216 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2217 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2219 switch (entry->type) {
2221 struct ftrace_entry *field;
2223 trace_assign_type(field, entry);
2225 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
2226 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
2231 struct ctx_switch_entry *field;
2233 trace_assign_type(field, entry);
2235 S = field->prev_state < sizeof(state_to_char) ?
2236 state_to_char[field->prev_state] : 'X';
2237 T = field->next_state < sizeof(state_to_char) ?
2238 state_to_char[field->next_state] : 'X';
2239 if (entry->type == TRACE_WAKE)
2241 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
2242 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
2243 SEQ_PUT_HEX_FIELD_RET(s, S);
2244 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
2245 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
2246 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
2247 SEQ_PUT_HEX_FIELD_RET(s, T);
2251 case TRACE_USER_STACK:
2253 struct special_entry *field;
2255 trace_assign_type(field, entry);
2257 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
2258 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
2259 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
2263 SEQ_PUT_FIELD_RET(s, newline);
2265 return TRACE_TYPE_HANDLED;
2268 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2270 struct trace_seq *s = &iter->seq;
2271 struct trace_entry *entry;
2275 if (entry->type == TRACE_CONT)
2276 return TRACE_TYPE_HANDLED;
2278 SEQ_PUT_FIELD_RET(s, entry->pid);
2279 SEQ_PUT_FIELD_RET(s, entry->cpu);
2280 SEQ_PUT_FIELD_RET(s, iter->ts);
2282 switch (entry->type) {
2284 struct ftrace_entry *field;
2286 trace_assign_type(field, entry);
2288 SEQ_PUT_FIELD_RET(s, field->ip);
2289 SEQ_PUT_FIELD_RET(s, field->parent_ip);
2293 struct ctx_switch_entry *field;
2295 trace_assign_type(field, entry);
2297 SEQ_PUT_FIELD_RET(s, field->prev_pid);
2298 SEQ_PUT_FIELD_RET(s, field->prev_prio);
2299 SEQ_PUT_FIELD_RET(s, field->prev_state);
2300 SEQ_PUT_FIELD_RET(s, field->next_pid);
2301 SEQ_PUT_FIELD_RET(s, field->next_prio);
2302 SEQ_PUT_FIELD_RET(s, field->next_state);
2306 case TRACE_USER_STACK:
2308 struct special_entry *field;
2310 trace_assign_type(field, entry);
2312 SEQ_PUT_FIELD_RET(s, field->arg1);
2313 SEQ_PUT_FIELD_RET(s, field->arg2);
2314 SEQ_PUT_FIELD_RET(s, field->arg3);
2321 static int trace_empty(struct trace_iterator *iter)
2325 for_each_tracing_cpu(cpu) {
2326 if (iter->buffer_iter[cpu]) {
2327 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2330 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2338 static enum print_line_t print_trace_line(struct trace_iterator *iter)
2340 enum print_line_t ret;
2342 if (iter->trace && iter->trace->print_line) {
2343 ret = iter->trace->print_line(iter);
2344 if (ret != TRACE_TYPE_UNHANDLED)
2348 if (trace_flags & TRACE_ITER_BIN)
2349 return print_bin_fmt(iter);
2351 if (trace_flags & TRACE_ITER_HEX)
2352 return print_hex_fmt(iter);
2354 if (trace_flags & TRACE_ITER_RAW)
2355 return print_raw_fmt(iter);
2357 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2358 return print_lat_fmt(iter, iter->idx, iter->cpu);
2360 return print_trace_fmt(iter);
2363 static int s_show(struct seq_file *m, void *v)
2365 struct trace_iterator *iter = v;
2367 if (iter->ent == NULL) {
2369 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2372 if (iter->trace && iter->trace->print_header)
2373 iter->trace->print_header(m);
2374 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2375 /* print nothing if the buffers are empty */
2376 if (trace_empty(iter))
2378 print_trace_header(m, iter);
2379 if (!(trace_flags & TRACE_ITER_VERBOSE))
2380 print_lat_help_header(m);
2382 if (!(trace_flags & TRACE_ITER_VERBOSE))
2383 print_func_help_header(m);
2386 print_trace_line(iter);
2387 trace_print_seq(m, &iter->seq);
2393 static struct seq_operations tracer_seq_ops = {
2400 static struct trace_iterator *
2401 __tracing_open(struct inode *inode, struct file *file, int *ret)
2403 struct trace_iterator *iter;
2407 if (tracing_disabled) {
2412 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2418 mutex_lock(&trace_types_lock);
2419 if (current_trace && current_trace->print_max)
2422 iter->tr = inode->i_private;
2423 iter->trace = current_trace;
2426 /* Notify the tracer early; before we stop tracing. */
2427 if (iter->trace && iter->trace->open)
2428 iter->trace->open(iter);
2430 /* Annotate start of buffers if we had overruns */
2431 if (ring_buffer_overruns(iter->tr->buffer))
2432 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2435 for_each_tracing_cpu(cpu) {
2437 iter->buffer_iter[cpu] =
2438 ring_buffer_read_start(iter->tr->buffer, cpu);
2440 if (!iter->buffer_iter[cpu])
2444 /* TODO stop tracer */
2445 *ret = seq_open(file, &tracer_seq_ops);
2449 m = file->private_data;
2452 /* stop the trace while dumping */
2455 mutex_unlock(&trace_types_lock);
2461 for_each_tracing_cpu(cpu) {
2462 if (iter->buffer_iter[cpu])
2463 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2465 mutex_unlock(&trace_types_lock);
2468 return ERR_PTR(-ENOMEM);
2471 int tracing_open_generic(struct inode *inode, struct file *filp)
2473 if (tracing_disabled)
2476 filp->private_data = inode->i_private;
2480 int tracing_release(struct inode *inode, struct file *file)
2482 struct seq_file *m = (struct seq_file *)file->private_data;
2483 struct trace_iterator *iter = m->private;
2486 mutex_lock(&trace_types_lock);
2487 for_each_tracing_cpu(cpu) {
2488 if (iter->buffer_iter[cpu])
2489 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2492 if (iter->trace && iter->trace->close)
2493 iter->trace->close(iter);
2495 /* reenable tracing if it was previously enabled */
2497 mutex_unlock(&trace_types_lock);
2499 seq_release(inode, file);
2504 static int tracing_open(struct inode *inode, struct file *file)
2508 __tracing_open(inode, file, &ret);
2513 static int tracing_lt_open(struct inode *inode, struct file *file)
2515 struct trace_iterator *iter;
2518 iter = __tracing_open(inode, file, &ret);
2521 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2528 t_next(struct seq_file *m, void *v, loff_t *pos)
2530 struct tracer *t = m->private;
2542 static void *t_start(struct seq_file *m, loff_t *pos)
2544 struct tracer *t = m->private;
2547 mutex_lock(&trace_types_lock);
2548 for (; t && l < *pos; t = t_next(m, t, &l))
2554 static void t_stop(struct seq_file *m, void *p)
2556 mutex_unlock(&trace_types_lock);
2559 static int t_show(struct seq_file *m, void *v)
2561 struct tracer *t = v;
2566 seq_printf(m, "%s", t->name);
2575 static struct seq_operations show_traces_seq_ops = {
2582 static int show_traces_open(struct inode *inode, struct file *file)
2586 if (tracing_disabled)
2589 ret = seq_open(file, &show_traces_seq_ops);
2591 struct seq_file *m = file->private_data;
2592 m->private = trace_types;
2598 static struct file_operations tracing_fops = {
2599 .open = tracing_open,
2601 .llseek = seq_lseek,
2602 .release = tracing_release,
2605 static struct file_operations tracing_lt_fops = {
2606 .open = tracing_lt_open,
2608 .llseek = seq_lseek,
2609 .release = tracing_release,
2612 static struct file_operations show_traces_fops = {
2613 .open = show_traces_open,
2615 .release = seq_release,
2619 * Only trace on a CPU if the bitmask is set:
2621 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2624 * When tracing/tracing_cpu_mask is modified then this holds
2625 * the new bitmask we are about to install:
2627 static cpumask_t tracing_cpumask_new;
2630 * The tracer itself will not take this lock, but still we want
2631 * to provide a consistent cpumask to user-space:
2633 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2636 * Temporary storage for the character representation of the
2637 * CPU bitmask (and one more byte for the newline):
2639 static char mask_str[NR_CPUS + 1];
2642 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2643 size_t count, loff_t *ppos)
2647 mutex_lock(&tracing_cpumask_update_lock);
2649 len = cpumask_scnprintf(mask_str, count, &tracing_cpumask);
2650 if (count - len < 2) {
2654 len += sprintf(mask_str + len, "\n");
2655 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2658 mutex_unlock(&tracing_cpumask_update_lock);
2664 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2665 size_t count, loff_t *ppos)
2669 mutex_lock(&tracing_cpumask_update_lock);
2670 err = cpumask_parse_user(ubuf, count, &tracing_cpumask_new);
2674 local_irq_disable();
2675 __raw_spin_lock(&ftrace_max_lock);
2676 for_each_tracing_cpu(cpu) {
2678 * Increase/decrease the disabled counter if we are
2679 * about to flip a bit in the cpumask:
2681 if (cpu_isset(cpu, tracing_cpumask) &&
2682 !cpu_isset(cpu, tracing_cpumask_new)) {
2683 atomic_inc(&global_trace.data[cpu]->disabled);
2685 if (!cpu_isset(cpu, tracing_cpumask) &&
2686 cpu_isset(cpu, tracing_cpumask_new)) {
2687 atomic_dec(&global_trace.data[cpu]->disabled);
2690 __raw_spin_unlock(&ftrace_max_lock);
2693 tracing_cpumask = tracing_cpumask_new;
2695 mutex_unlock(&tracing_cpumask_update_lock);
2700 mutex_unlock(&tracing_cpumask_update_lock);
2705 static struct file_operations tracing_cpumask_fops = {
2706 .open = tracing_open_generic,
2707 .read = tracing_cpumask_read,
2708 .write = tracing_cpumask_write,
2712 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2713 size_t cnt, loff_t *ppos)
2719 u32 tracer_flags = current_trace->flags->val;
2720 struct tracer_opt *trace_opts = current_trace->flags->opts;
2723 /* calulate max size */
2724 for (i = 0; trace_options[i]; i++) {
2725 len += strlen(trace_options[i]);
2726 len += 3; /* "no" and space */
2730 * Increase the size with names of options specific
2731 * of the current tracer.
2733 for (i = 0; trace_opts[i].name; i++) {
2734 len += strlen(trace_opts[i].name);
2735 len += 3; /* "no" and space */
2738 /* +2 for \n and \0 */
2739 buf = kmalloc(len + 2, GFP_KERNEL);
2743 for (i = 0; trace_options[i]; i++) {
2744 if (trace_flags & (1 << i))
2745 r += sprintf(buf + r, "%s ", trace_options[i]);
2747 r += sprintf(buf + r, "no%s ", trace_options[i]);
2750 for (i = 0; trace_opts[i].name; i++) {
2751 if (tracer_flags & trace_opts[i].bit)
2752 r += sprintf(buf + r, "%s ",
2753 trace_opts[i].name);
2755 r += sprintf(buf + r, "no%s ",
2756 trace_opts[i].name);
2759 r += sprintf(buf + r, "\n");
2760 WARN_ON(r >= len + 2);
2762 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2769 /* Try to assign a tracer specific option */
2770 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2772 struct tracer_flags *trace_flags = trace->flags;
2773 struct tracer_opt *opts = NULL;
2777 for (i = 0; trace_flags->opts[i].name; i++) {
2778 opts = &trace_flags->opts[i];
2779 len = strlen(opts->name);
2781 if (strncmp(cmp, opts->name, len) == 0) {
2782 ret = trace->set_flag(trace_flags->val,
2788 if (!trace_flags->opts[i].name)
2791 /* Refused to handle */
2796 trace_flags->val &= ~opts->bit;
2798 trace_flags->val |= opts->bit;
2804 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2805 size_t cnt, loff_t *ppos)
2813 if (cnt >= sizeof(buf))
2816 if (copy_from_user(&buf, ubuf, cnt))
2821 if (strncmp(buf, "no", 2) == 0) {
2826 for (i = 0; trace_options[i]; i++) {
2827 int len = strlen(trace_options[i]);
2829 if (strncmp(cmp, trace_options[i], len) == 0) {
2831 trace_flags &= ~(1 << i);
2833 trace_flags |= (1 << i);
2838 /* If no option could be set, test the specific tracer options */
2839 if (!trace_options[i]) {
2840 ret = set_tracer_option(current_trace, cmp, neg);
2850 static struct file_operations tracing_iter_fops = {
2851 .open = tracing_open_generic,
2852 .read = tracing_trace_options_read,
2853 .write = tracing_trace_options_write,
2856 static const char readme_msg[] =
2857 "tracing mini-HOWTO:\n\n"
2859 "# mount -t debugfs nodev /debug\n\n"
2860 "# cat /debug/tracing/available_tracers\n"
2861 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2862 "# cat /debug/tracing/current_tracer\n"
2864 "# echo sched_switch > /debug/tracing/current_tracer\n"
2865 "# cat /debug/tracing/current_tracer\n"
2867 "# cat /debug/tracing/trace_options\n"
2868 "noprint-parent nosym-offset nosym-addr noverbose\n"
2869 "# echo print-parent > /debug/tracing/trace_options\n"
2870 "# echo 1 > /debug/tracing/tracing_enabled\n"
2871 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2872 "echo 0 > /debug/tracing/tracing_enabled\n"
2876 tracing_readme_read(struct file *filp, char __user *ubuf,
2877 size_t cnt, loff_t *ppos)
2879 return simple_read_from_buffer(ubuf, cnt, ppos,
2880 readme_msg, strlen(readme_msg));
2883 static struct file_operations tracing_readme_fops = {
2884 .open = tracing_open_generic,
2885 .read = tracing_readme_read,
2889 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2890 size_t cnt, loff_t *ppos)
2895 r = sprintf(buf, "%u\n", tracer_enabled);
2896 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2900 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2901 size_t cnt, loff_t *ppos)
2903 struct trace_array *tr = filp->private_data;
2908 if (cnt >= sizeof(buf))
2911 if (copy_from_user(&buf, ubuf, cnt))
2916 ret = strict_strtoul(buf, 10, &val);
2922 mutex_lock(&trace_types_lock);
2923 if (tracer_enabled ^ val) {
2926 if (current_trace->start)
2927 current_trace->start(tr);
2932 if (current_trace->stop)
2933 current_trace->stop(tr);
2936 mutex_unlock(&trace_types_lock);
2944 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2945 size_t cnt, loff_t *ppos)
2947 char buf[max_tracer_type_len+2];
2950 mutex_lock(&trace_types_lock);
2952 r = sprintf(buf, "%s\n", current_trace->name);
2954 r = sprintf(buf, "\n");
2955 mutex_unlock(&trace_types_lock);
2957 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2960 static int tracing_set_tracer(char *buf)
2962 struct trace_array *tr = &global_trace;
2966 mutex_lock(&trace_types_lock);
2967 for (t = trace_types; t; t = t->next) {
2968 if (strcmp(t->name, buf) == 0)
2975 if (t == current_trace)
2978 trace_branch_disable();
2979 if (current_trace && current_trace->reset)
2980 current_trace->reset(tr);
2989 trace_branch_enable(tr);
2991 mutex_unlock(&trace_types_lock);
2997 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2998 size_t cnt, loff_t *ppos)
3000 char buf[max_tracer_type_len+1];
3007 if (cnt > max_tracer_type_len)
3008 cnt = max_tracer_type_len;
3010 if (copy_from_user(&buf, ubuf, cnt))
3015 /* strip ending whitespace. */
3016 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3019 err = tracing_set_tracer(buf);
3029 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3030 size_t cnt, loff_t *ppos)
3032 unsigned long *ptr = filp->private_data;
3036 r = snprintf(buf, sizeof(buf), "%ld\n",
3037 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3038 if (r > sizeof(buf))
3040 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3044 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3045 size_t cnt, loff_t *ppos)
3047 long *ptr = filp->private_data;
3052 if (cnt >= sizeof(buf))
3055 if (copy_from_user(&buf, ubuf, cnt))
3060 ret = strict_strtoul(buf, 10, &val);
3069 static atomic_t tracing_reader;
3071 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3073 struct trace_iterator *iter;
3075 if (tracing_disabled)
3078 /* We only allow for reader of the pipe */
3079 if (atomic_inc_return(&tracing_reader) != 1) {
3080 atomic_dec(&tracing_reader);
3084 /* create a buffer to store the information to pass to userspace */
3085 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3089 mutex_lock(&trace_types_lock);
3091 /* trace pipe does not show start of buffer */
3092 cpus_setall(iter->started);
3094 iter->tr = &global_trace;
3095 iter->trace = current_trace;
3096 filp->private_data = iter;
3098 if (iter->trace->pipe_open)
3099 iter->trace->pipe_open(iter);
3100 mutex_unlock(&trace_types_lock);
3105 static int tracing_release_pipe(struct inode *inode, struct file *file)
3107 struct trace_iterator *iter = file->private_data;
3110 atomic_dec(&tracing_reader);
3116 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3118 struct trace_iterator *iter = filp->private_data;
3120 if (trace_flags & TRACE_ITER_BLOCK) {
3122 * Always select as readable when in blocking mode
3124 return POLLIN | POLLRDNORM;
3126 if (!trace_empty(iter))
3127 return POLLIN | POLLRDNORM;
3128 poll_wait(filp, &trace_wait, poll_table);
3129 if (!trace_empty(iter))
3130 return POLLIN | POLLRDNORM;
3140 tracing_read_pipe(struct file *filp, char __user *ubuf,
3141 size_t cnt, loff_t *ppos)
3143 struct trace_iterator *iter = filp->private_data;
3146 /* return any leftover data */
3147 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3151 trace_seq_reset(&iter->seq);
3153 mutex_lock(&trace_types_lock);
3154 if (iter->trace->read) {
3155 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3162 while (trace_empty(iter)) {
3164 if ((filp->f_flags & O_NONBLOCK)) {
3170 * This is a make-shift waitqueue. The reason we don't use
3171 * an actual wait queue is because:
3172 * 1) we only ever have one waiter
3173 * 2) the tracing, traces all functions, we don't want
3174 * the overhead of calling wake_up and friends
3175 * (and tracing them too)
3176 * Anyway, this is really very primitive wakeup.
3178 set_current_state(TASK_INTERRUPTIBLE);
3179 iter->tr->waiter = current;
3181 mutex_unlock(&trace_types_lock);
3183 /* sleep for 100 msecs, and try again. */
3184 schedule_timeout(HZ/10);
3186 mutex_lock(&trace_types_lock);
3188 iter->tr->waiter = NULL;
3190 if (signal_pending(current)) {
3195 if (iter->trace != current_trace)
3199 * We block until we read something and tracing is disabled.
3200 * We still block if tracing is disabled, but we have never
3201 * read anything. This allows a user to cat this file, and
3202 * then enable tracing. But after we have read something,
3203 * we give an EOF when tracing is again disabled.
3205 * iter->pos will be 0 if we haven't read anything.
3207 if (!tracer_enabled && iter->pos)
3213 /* stop when tracing is finished */
3214 if (trace_empty(iter))
3217 if (cnt >= PAGE_SIZE)
3218 cnt = PAGE_SIZE - 1;
3220 /* reset all but tr, trace, and overruns */
3221 memset(&iter->seq, 0,
3222 sizeof(struct trace_iterator) -
3223 offsetof(struct trace_iterator, seq));
3226 while (find_next_entry_inc(iter) != NULL) {
3227 enum print_line_t ret;
3228 int len = iter->seq.len;
3230 ret = print_trace_line(iter);
3231 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3232 /* don't print partial lines */
3233 iter->seq.len = len;
3237 trace_consume(iter);
3239 if (iter->seq.len >= cnt)
3243 /* Now copy what we have to the user */
3244 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3245 if (iter->seq.readpos >= iter->seq.len)
3246 trace_seq_reset(&iter->seq);
3249 * If there was nothing to send to user, inspite of consuming trace
3250 * entries, go back to wait for more entries.
3256 mutex_unlock(&trace_types_lock);
3262 tracing_entries_read(struct file *filp, char __user *ubuf,
3263 size_t cnt, loff_t *ppos)
3265 struct trace_array *tr = filp->private_data;
3269 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3270 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3274 tracing_entries_write(struct file *filp, const char __user *ubuf,
3275 size_t cnt, loff_t *ppos)
3281 if (cnt >= sizeof(buf))
3284 if (copy_from_user(&buf, ubuf, cnt))
3289 ret = strict_strtoul(buf, 10, &val);
3293 /* must have at least 1 entry */
3297 mutex_lock(&trace_types_lock);
3301 /* disable all cpu buffers */
3302 for_each_tracing_cpu(cpu) {
3303 if (global_trace.data[cpu])
3304 atomic_inc(&global_trace.data[cpu]->disabled);
3305 if (max_tr.data[cpu])
3306 atomic_inc(&max_tr.data[cpu]->disabled);
3309 /* value is in KB */
3312 if (val != global_trace.entries) {
3313 ret = ring_buffer_resize(global_trace.buffer, val);
3319 ret = ring_buffer_resize(max_tr.buffer, val);
3323 r = ring_buffer_resize(global_trace.buffer,
3324 global_trace.entries);
3326 /* AARGH! We are left with different
3327 * size max buffer!!!! */
3329 tracing_disabled = 1;
3334 global_trace.entries = val;
3339 /* If check pages failed, return ENOMEM */
3340 if (tracing_disabled)
3343 for_each_tracing_cpu(cpu) {
3344 if (global_trace.data[cpu])
3345 atomic_dec(&global_trace.data[cpu]->disabled);
3346 if (max_tr.data[cpu])
3347 atomic_dec(&max_tr.data[cpu]->disabled);
3351 max_tr.entries = global_trace.entries;
3352 mutex_unlock(&trace_types_lock);
3357 static int mark_printk(const char *fmt, ...)
3361 va_start(args, fmt);
3362 ret = trace_vprintk(0, -1, fmt, args);
3368 tracing_mark_write(struct file *filp, const char __user *ubuf,
3369 size_t cnt, loff_t *fpos)
3374 if (tracing_disabled)
3377 if (cnt > TRACE_BUF_SIZE)
3378 cnt = TRACE_BUF_SIZE;
3380 buf = kmalloc(cnt + 1, GFP_KERNEL);
3384 if (copy_from_user(buf, ubuf, cnt)) {
3389 /* Cut from the first nil or newline. */
3391 end = strchr(buf, '\n');
3395 cnt = mark_printk("%s\n", buf);
3402 static struct file_operations tracing_max_lat_fops = {
3403 .open = tracing_open_generic,
3404 .read = tracing_max_lat_read,
3405 .write = tracing_max_lat_write,
3408 static struct file_operations tracing_ctrl_fops = {
3409 .open = tracing_open_generic,
3410 .read = tracing_ctrl_read,
3411 .write = tracing_ctrl_write,
3414 static struct file_operations set_tracer_fops = {
3415 .open = tracing_open_generic,
3416 .read = tracing_set_trace_read,
3417 .write = tracing_set_trace_write,
3420 static struct file_operations tracing_pipe_fops = {
3421 .open = tracing_open_pipe,
3422 .poll = tracing_poll_pipe,
3423 .read = tracing_read_pipe,
3424 .release = tracing_release_pipe,
3427 static struct file_operations tracing_entries_fops = {
3428 .open = tracing_open_generic,
3429 .read = tracing_entries_read,
3430 .write = tracing_entries_write,
3433 static struct file_operations tracing_mark_fops = {
3434 .open = tracing_open_generic,
3435 .write = tracing_mark_write,
3438 #ifdef CONFIG_DYNAMIC_FTRACE
3440 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3446 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3447 size_t cnt, loff_t *ppos)
3449 static char ftrace_dyn_info_buffer[1024];
3450 static DEFINE_MUTEX(dyn_info_mutex);
3451 unsigned long *p = filp->private_data;
3452 char *buf = ftrace_dyn_info_buffer;
3453 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3456 mutex_lock(&dyn_info_mutex);
3457 r = sprintf(buf, "%ld ", *p);
3459 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3462 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3464 mutex_unlock(&dyn_info_mutex);
3469 static struct file_operations tracing_dyn_info_fops = {
3470 .open = tracing_open_generic,
3471 .read = tracing_read_dyn_info,
3475 static struct dentry *d_tracer;
3477 struct dentry *tracing_init_dentry(void)
3484 d_tracer = debugfs_create_dir("tracing", NULL);
3486 if (!d_tracer && !once) {
3488 pr_warning("Could not create debugfs directory 'tracing'\n");
3495 #ifdef CONFIG_FTRACE_SELFTEST
3496 /* Let selftest have access to static functions in this file */
3497 #include "trace_selftest.c"
3500 static __init int tracer_init_debugfs(void)
3502 struct dentry *d_tracer;
3503 struct dentry *entry;
3505 d_tracer = tracing_init_dentry();
3507 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3508 &global_trace, &tracing_ctrl_fops);
3510 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3512 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3513 NULL, &tracing_iter_fops);
3515 pr_warning("Could not create debugfs 'trace_options' entry\n");
3517 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3518 NULL, &tracing_cpumask_fops);
3520 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3522 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3523 &global_trace, &tracing_lt_fops);
3525 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3527 entry = debugfs_create_file("trace", 0444, d_tracer,
3528 &global_trace, &tracing_fops);
3530 pr_warning("Could not create debugfs 'trace' entry\n");
3532 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3533 &global_trace, &show_traces_fops);
3535 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3537 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3538 &global_trace, &set_tracer_fops);
3540 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3542 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3543 &tracing_max_latency,
3544 &tracing_max_lat_fops);
3546 pr_warning("Could not create debugfs "
3547 "'tracing_max_latency' entry\n");
3549 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3550 &tracing_thresh, &tracing_max_lat_fops);
3552 pr_warning("Could not create debugfs "
3553 "'tracing_thresh' entry\n");
3554 entry = debugfs_create_file("README", 0644, d_tracer,
3555 NULL, &tracing_readme_fops);
3557 pr_warning("Could not create debugfs 'README' entry\n");
3559 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3560 NULL, &tracing_pipe_fops);
3562 pr_warning("Could not create debugfs "
3563 "'trace_pipe' entry\n");
3565 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3566 &global_trace, &tracing_entries_fops);
3568 pr_warning("Could not create debugfs "
3569 "'buffer_size_kb' entry\n");
3571 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3572 NULL, &tracing_mark_fops);
3574 pr_warning("Could not create debugfs "
3575 "'trace_marker' entry\n");
3577 #ifdef CONFIG_DYNAMIC_FTRACE
3578 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3579 &ftrace_update_tot_cnt,
3580 &tracing_dyn_info_fops);
3582 pr_warning("Could not create debugfs "
3583 "'dyn_ftrace_total_info' entry\n");
3585 #ifdef CONFIG_SYSPROF_TRACER
3586 init_tracer_sysprof_debugfs(d_tracer);
3591 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
3593 static DEFINE_SPINLOCK(trace_buf_lock);
3594 static char trace_buf[TRACE_BUF_SIZE];
3596 struct ring_buffer_event *event;
3597 struct trace_array *tr = &global_trace;
3598 struct trace_array_cpu *data;
3599 int cpu, len = 0, size, pc;
3600 struct print_entry *entry;
3601 unsigned long irq_flags;
3603 if (tracing_disabled || tracing_selftest_running)
3606 pc = preempt_count();
3607 preempt_disable_notrace();
3608 cpu = raw_smp_processor_id();
3609 data = tr->data[cpu];
3611 if (unlikely(atomic_read(&data->disabled)))
3614 pause_graph_tracing();
3615 spin_lock_irqsave(&trace_buf_lock, irq_flags);
3616 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3618 len = min(len, TRACE_BUF_SIZE-1);
3621 size = sizeof(*entry) + len + 1;
3622 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3625 entry = ring_buffer_event_data(event);
3626 tracing_generic_entry_update(&entry->ent, irq_flags, pc);
3627 entry->ent.type = TRACE_PRINT;
3629 entry->depth = depth;
3631 memcpy(&entry->buf, trace_buf, len);
3632 entry->buf[len] = 0;
3633 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3636 spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
3637 unpause_graph_tracing();
3639 preempt_enable_notrace();
3643 EXPORT_SYMBOL_GPL(trace_vprintk);
3645 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3650 if (!(trace_flags & TRACE_ITER_PRINTK))
3654 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3658 EXPORT_SYMBOL_GPL(__ftrace_printk);
3660 static int trace_panic_handler(struct notifier_block *this,
3661 unsigned long event, void *unused)
3663 if (ftrace_dump_on_oops)
3668 static struct notifier_block trace_panic_notifier = {
3669 .notifier_call = trace_panic_handler,
3671 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3674 static int trace_die_handler(struct notifier_block *self,
3680 if (ftrace_dump_on_oops)
3689 static struct notifier_block trace_die_notifier = {
3690 .notifier_call = trace_die_handler,
3695 * printk is set to max of 1024, we really don't need it that big.
3696 * Nothing should be printing 1000 characters anyway.
3698 #define TRACE_MAX_PRINT 1000
3701 * Define here KERN_TRACE so that we have one place to modify
3702 * it if we decide to change what log level the ftrace dump
3705 #define KERN_TRACE KERN_INFO
3708 trace_printk_seq(struct trace_seq *s)
3710 /* Probably should print a warning here. */
3714 /* should be zero ended, but we are paranoid. */
3715 s->buffer[s->len] = 0;
3717 printk(KERN_TRACE "%s", s->buffer);
3722 void ftrace_dump(void)
3724 static DEFINE_SPINLOCK(ftrace_dump_lock);
3725 /* use static because iter can be a bit big for the stack */
3726 static struct trace_iterator iter;
3727 static cpumask_t mask;
3728 static int dump_ran;
3729 unsigned long flags;
3733 spin_lock_irqsave(&ftrace_dump_lock, flags);
3739 /* No turning back! */
3742 for_each_tracing_cpu(cpu) {
3743 atomic_inc(&global_trace.data[cpu]->disabled);
3746 /* don't look at user memory in panic mode */
3747 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3749 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3751 iter.tr = &global_trace;
3752 iter.trace = current_trace;
3755 * We need to stop all tracing on all CPUS to read the
3756 * the next buffer. This is a bit expensive, but is
3757 * not done often. We fill all what we can read,
3758 * and then release the locks again.
3763 while (!trace_empty(&iter)) {
3766 printk(KERN_TRACE "---------------------------------\n");
3770 /* reset all but tr, trace, and overruns */
3771 memset(&iter.seq, 0,
3772 sizeof(struct trace_iterator) -
3773 offsetof(struct trace_iterator, seq));
3774 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3777 if (find_next_entry_inc(&iter) != NULL) {
3778 print_trace_line(&iter);
3779 trace_consume(&iter);
3782 trace_printk_seq(&iter.seq);
3786 printk(KERN_TRACE " (ftrace buffer empty)\n");
3788 printk(KERN_TRACE "---------------------------------\n");
3791 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3794 __init static int tracer_alloc_buffers(void)
3796 struct trace_array_cpu *data;
3799 /* TODO: make the number of buffers hot pluggable with CPUS */
3800 tracing_buffer_mask = cpu_possible_map;
3802 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3803 TRACE_BUFFER_FLAGS);
3804 if (!global_trace.buffer) {
3805 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3809 global_trace.entries = ring_buffer_size(global_trace.buffer);
3811 #ifdef CONFIG_TRACER_MAX_TRACE
3812 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3813 TRACE_BUFFER_FLAGS);
3814 if (!max_tr.buffer) {
3815 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3817 ring_buffer_free(global_trace.buffer);
3820 max_tr.entries = ring_buffer_size(max_tr.buffer);
3821 WARN_ON(max_tr.entries != global_trace.entries);
3824 /* Allocate the first page for all buffers */
3825 for_each_tracing_cpu(i) {
3826 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3827 max_tr.data[i] = &per_cpu(max_data, i);
3830 trace_init_cmdlines();
3832 register_tracer(&nop_trace);
3833 #ifdef CONFIG_BOOT_TRACER
3834 register_tracer(&boot_tracer);
3835 current_trace = &boot_tracer;
3836 current_trace->init(&global_trace);
3838 current_trace = &nop_trace;
3841 /* All seems OK, enable tracing */
3842 tracing_disabled = 0;
3844 atomic_notifier_chain_register(&panic_notifier_list,
3845 &trace_panic_notifier);
3847 register_die_notifier(&trace_die_notifier);
3851 early_initcall(tracer_alloc_buffers);
3852 fs_initcall(tracer_init_debugfs);