2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/notifier.h>
18 #include <linux/debugfs.h>
19 #include <linux/pagemap.h>
20 #include <linux/hardirq.h>
21 #include <linux/linkage.h>
22 #include <linux/uaccess.h>
23 #include <linux/ftrace.h>
24 #include <linux/module.h>
25 #include <linux/percpu.h>
26 #include <linux/kdebug.h>
27 #include <linux/ctype.h>
28 #include <linux/init.h>
29 #include <linux/poll.h>
30 #include <linux/gfp.h>
32 #include <linux/kprobes.h>
33 #include <linux/writeback.h>
35 #include <linux/stacktrace.h>
36 #include <linux/ring_buffer.h>
37 #include <linux/irqflags.h>
41 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
43 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
44 unsigned long __read_mostly tracing_thresh;
46 /* For tracers that don't implement custom flags */
47 static struct tracer_opt dummy_tracer_opt[] = {
51 static struct tracer_flags dummy_tracer_flags = {
53 .opts = dummy_tracer_opt
56 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
62 * Kill all tracing for good (never come back).
63 * It is initialized to 1 but will turn to zero if the initialization
64 * of the tracer is successful. But that is the only place that sets
67 int tracing_disabled = 1;
69 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
71 static inline void ftrace_disable_cpu(void)
74 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
77 static inline void ftrace_enable_cpu(void)
79 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
83 static cpumask_t __read_mostly tracing_buffer_mask;
85 #define for_each_tracing_cpu(cpu) \
86 for_each_cpu_mask(cpu, tracing_buffer_mask)
89 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
91 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
92 * is set, then ftrace_dump is called. This will output the contents
93 * of the ftrace buffers to the console. This is very useful for
94 * capturing traces that lead to crashes and outputing it to a
97 * It is default off, but you can enable it with either specifying
98 * "ftrace_dump_on_oops" in the kernel command line, or setting
99 * /proc/sys/kernel/ftrace_dump_on_oops to true.
101 int ftrace_dump_on_oops;
103 static int tracing_set_tracer(char *buf);
105 static int __init set_ftrace(char *str)
107 tracing_set_tracer(str);
110 __setup("ftrace", set_ftrace);
112 static int __init set_ftrace_dump_on_oops(char *str)
114 ftrace_dump_on_oops = 1;
117 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
120 ns2usecs(cycle_t nsec)
127 cycle_t ftrace_now(int cpu)
129 u64 ts = ring_buffer_time_stamp(cpu);
130 ring_buffer_normalize_time_stamp(cpu, &ts);
135 * The global_trace is the descriptor that holds the tracing
136 * buffers for the live tracing. For each CPU, it contains
137 * a link list of pages that will store trace entries. The
138 * page descriptor of the pages in the memory is used to hold
139 * the link list by linking the lru item in the page descriptor
140 * to each of the pages in the buffer per CPU.
142 * For each active CPU there is a data field that holds the
143 * pages for the buffer for that CPU. Each CPU has the same number
144 * of pages allocated for its buffer.
146 static struct trace_array global_trace;
148 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
151 * The max_tr is used to snapshot the global_trace when a maximum
152 * latency is reached. Some tracers will use this to store a maximum
153 * trace while it continues examining live traces.
155 * The buffers for the max_tr are set up the same as the global_trace.
156 * When a snapshot is taken, the link list of the max_tr is swapped
157 * with the link list of the global_trace and the buffers are reset for
158 * the global_trace so the tracing can continue.
160 static struct trace_array max_tr;
162 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
164 /* tracer_enabled is used to toggle activation of a tracer */
165 static int tracer_enabled = 1;
168 * tracing_is_enabled - return tracer_enabled status
170 * This function is used by other tracers to know the status
171 * of the tracer_enabled flag. Tracers may use this function
172 * to know if it should enable their features when starting
173 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
175 int tracing_is_enabled(void)
177 return tracer_enabled;
180 /* function tracing enabled */
181 int ftrace_function_enabled;
184 * trace_buf_size is the size in bytes that is allocated
185 * for a buffer. Note, the number of bytes is always rounded
188 * This number is purposely set to a low number of 16384.
189 * If the dump on oops happens, it will be much appreciated
190 * to not have to wait for all that output. Anyway this can be
191 * boot time and run time configurable.
193 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
195 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
197 /* trace_types holds a link list of available tracers. */
198 static struct tracer *trace_types __read_mostly;
200 /* current_trace points to the tracer that is currently active */
201 static struct tracer *current_trace __read_mostly;
204 * max_tracer_type_len is used to simplify the allocating of
205 * buffers to read userspace tracer names. We keep track of
206 * the longest tracer name registered.
208 static int max_tracer_type_len;
211 * trace_types_lock is used to protect the trace_types list.
212 * This lock is also used to keep user access serialized.
213 * Accesses from userspace will grab this lock while userspace
214 * activities happen inside the kernel.
216 static DEFINE_MUTEX(trace_types_lock);
218 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
219 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
221 /* trace_flags holds trace_options default values */
222 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
226 * trace_wake_up - wake up tasks waiting for trace input
228 * Simply wakes up any task that is blocked on the trace_wait
229 * queue. These is used with trace_poll for tasks polling the trace.
231 void trace_wake_up(void)
234 * The runqueue_is_locked() can fail, but this is the best we
237 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
238 wake_up(&trace_wait);
241 static int __init set_buf_size(char *str)
243 unsigned long buf_size;
248 ret = strict_strtoul(str, 0, &buf_size);
249 /* nr_entries can not be zero */
250 if (ret < 0 || buf_size == 0)
252 trace_buf_size = buf_size;
255 __setup("trace_buf_size=", set_buf_size);
257 unsigned long nsecs_to_usecs(unsigned long nsecs)
262 /* These must match the bit postions in trace_iterator_flags */
263 static const char *trace_options[] = {
283 * ftrace_max_lock is used to protect the swapping of buffers
284 * when taking a max snapshot. The buffers themselves are
285 * protected by per_cpu spinlocks. But the action of the swap
286 * needs its own lock.
288 * This is defined as a raw_spinlock_t in order to help
289 * with performance when lockdep debugging is enabled.
291 static raw_spinlock_t ftrace_max_lock =
292 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
295 * Copy the new maximum trace into the separate maximum-trace
296 * structure. (this way the maximum trace is permanently saved,
297 * for later retrieval via /debugfs/tracing/latency_trace)
300 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
302 struct trace_array_cpu *data = tr->data[cpu];
305 max_tr.time_start = data->preempt_timestamp;
307 data = max_tr.data[cpu];
308 data->saved_latency = tracing_max_latency;
310 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
311 data->pid = tsk->pid;
312 data->uid = tsk->uid;
313 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
314 data->policy = tsk->policy;
315 data->rt_priority = tsk->rt_priority;
317 /* record this tasks comm */
318 tracing_record_cmdline(current);
322 * trace_seq_printf - sequence printing of trace information
323 * @s: trace sequence descriptor
324 * @fmt: printf format string
326 * The tracer may use either sequence operations or its own
327 * copy to user routines. To simplify formating of a trace
328 * trace_seq_printf is used to store strings into a special
329 * buffer (@s). Then the output may be either used by
330 * the sequencer or pulled into another buffer.
333 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
335 int len = (PAGE_SIZE - 1) - s->len;
343 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
346 /* If we can't write it all, don't bother writing anything */
356 * trace_seq_puts - trace sequence printing of simple string
357 * @s: trace sequence descriptor
358 * @str: simple string to record
360 * The tracer may use either the sequence operations or its own
361 * copy to user routines. This function records a simple string
362 * into a special buffer (@s) for later retrieval by a sequencer
363 * or other mechanism.
366 trace_seq_puts(struct trace_seq *s, const char *str)
368 int len = strlen(str);
370 if (len > ((PAGE_SIZE - 1) - s->len))
373 memcpy(s->buffer + s->len, str, len);
380 trace_seq_putc(struct trace_seq *s, unsigned char c)
382 if (s->len >= (PAGE_SIZE - 1))
385 s->buffer[s->len++] = c;
391 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
393 if (len > ((PAGE_SIZE - 1) - s->len))
396 memcpy(s->buffer + s->len, mem, len);
402 #define MAX_MEMHEX_BYTES 8
403 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
406 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
408 unsigned char hex[HEX_CHARS];
409 unsigned char *data = mem;
413 for (i = 0, j = 0; i < len; i++) {
415 for (i = len-1, j = 0; i >= 0; i--) {
417 hex[j++] = hex_asc_hi(data[i]);
418 hex[j++] = hex_asc_lo(data[i]);
422 return trace_seq_putmem(s, hex, j);
426 trace_seq_reset(struct trace_seq *s)
432 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
437 if (s->len <= s->readpos)
440 len = s->len - s->readpos;
443 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
452 trace_print_seq(struct seq_file *m, struct trace_seq *s)
454 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
457 seq_puts(m, s->buffer);
463 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
465 * @tsk: the task with the latency
466 * @cpu: The cpu that initiated the trace.
468 * Flip the buffers between the @tr and the max_tr and record information
469 * about which task was the cause of this latency.
472 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
474 struct ring_buffer *buf = tr->buffer;
476 WARN_ON_ONCE(!irqs_disabled());
477 __raw_spin_lock(&ftrace_max_lock);
479 tr->buffer = max_tr.buffer;
482 ftrace_disable_cpu();
483 ring_buffer_reset(tr->buffer);
486 __update_max_tr(tr, tsk, cpu);
487 __raw_spin_unlock(&ftrace_max_lock);
491 * update_max_tr_single - only copy one trace over, and reset the rest
493 * @tsk - task with the latency
494 * @cpu - the cpu of the buffer to copy.
496 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
499 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
503 WARN_ON_ONCE(!irqs_disabled());
504 __raw_spin_lock(&ftrace_max_lock);
506 ftrace_disable_cpu();
508 ring_buffer_reset(max_tr.buffer);
509 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
515 __update_max_tr(tr, tsk, cpu);
516 __raw_spin_unlock(&ftrace_max_lock);
520 * register_tracer - register a tracer with the ftrace system.
521 * @type - the plugin for the tracer
523 * Register a new plugin tracer.
525 int register_tracer(struct tracer *type)
532 pr_info("Tracer must have a name\n");
537 * When this gets called we hold the BKL which means that
538 * preemption is disabled. Various trace selftests however
539 * need to disable and enable preemption for successful tests.
540 * So we drop the BKL here and grab it after the tests again.
543 mutex_lock(&trace_types_lock);
545 for (t = trace_types; t; t = t->next) {
546 if (strcmp(type->name, t->name) == 0) {
548 pr_info("Trace %s already registered\n",
556 type->set_flag = &dummy_set_flag;
558 type->flags = &dummy_tracer_flags;
560 if (!type->flags->opts)
561 type->flags->opts = dummy_tracer_opt;
563 #ifdef CONFIG_FTRACE_STARTUP_TEST
564 if (type->selftest) {
565 struct tracer *saved_tracer = current_trace;
566 struct trace_array *tr = &global_trace;
569 * Run a selftest on this tracer.
570 * Here we reset the trace buffer, and set the current
571 * tracer to be this tracer. The tracer can then run some
572 * internal tracing to verify that everything is in order.
573 * If we fail, we do not register this tracer.
575 for_each_tracing_cpu(i)
576 tracing_reset(tr, i);
578 current_trace = type;
579 /* the test is responsible for initializing and enabling */
580 pr_info("Testing tracer %s: ", type->name);
581 ret = type->selftest(type, tr);
582 /* the test is responsible for resetting too */
583 current_trace = saved_tracer;
585 printk(KERN_CONT "FAILED!\n");
588 /* Only reset on passing, to avoid touching corrupted buffers */
589 for_each_tracing_cpu(i)
590 tracing_reset(tr, i);
592 printk(KERN_CONT "PASSED\n");
596 type->next = trace_types;
598 len = strlen(type->name);
599 if (len > max_tracer_type_len)
600 max_tracer_type_len = len;
603 mutex_unlock(&trace_types_lock);
609 void unregister_tracer(struct tracer *type)
614 mutex_lock(&trace_types_lock);
615 for (t = &trace_types; *t; t = &(*t)->next) {
619 pr_info("Trace %s not registered\n", type->name);
624 if (strlen(type->name) != max_tracer_type_len)
627 max_tracer_type_len = 0;
628 for (t = &trace_types; *t; t = &(*t)->next) {
629 len = strlen((*t)->name);
630 if (len > max_tracer_type_len)
631 max_tracer_type_len = len;
634 mutex_unlock(&trace_types_lock);
637 void tracing_reset(struct trace_array *tr, int cpu)
639 ftrace_disable_cpu();
640 ring_buffer_reset_cpu(tr->buffer, cpu);
644 #define SAVED_CMDLINES 128
645 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
646 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
647 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
648 static int cmdline_idx;
649 static DEFINE_SPINLOCK(trace_cmdline_lock);
651 /* temporary disable recording */
652 atomic_t trace_record_cmdline_disabled __read_mostly;
654 static void trace_init_cmdlines(void)
656 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
657 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
661 static int trace_stop_count;
662 static DEFINE_SPINLOCK(tracing_start_lock);
665 * tracing_start - quick start of the tracer
667 * If tracing is enabled but was stopped by tracing_stop,
668 * this will start the tracer back up.
670 void tracing_start(void)
672 struct ring_buffer *buffer;
675 if (tracing_disabled)
678 spin_lock_irqsave(&tracing_start_lock, flags);
679 if (--trace_stop_count)
682 if (trace_stop_count < 0) {
683 /* Someone screwed up their debugging */
685 trace_stop_count = 0;
690 buffer = global_trace.buffer;
692 ring_buffer_record_enable(buffer);
694 buffer = max_tr.buffer;
696 ring_buffer_record_enable(buffer);
700 spin_unlock_irqrestore(&tracing_start_lock, flags);
704 * tracing_stop - quick stop of the tracer
706 * Light weight way to stop tracing. Use in conjunction with
709 void tracing_stop(void)
711 struct ring_buffer *buffer;
715 spin_lock_irqsave(&tracing_start_lock, flags);
716 if (trace_stop_count++)
719 buffer = global_trace.buffer;
721 ring_buffer_record_disable(buffer);
723 buffer = max_tr.buffer;
725 ring_buffer_record_disable(buffer);
728 spin_unlock_irqrestore(&tracing_start_lock, flags);
731 void trace_stop_cmdline_recording(void);
733 static void trace_save_cmdline(struct task_struct *tsk)
738 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
742 * It's not the end of the world if we don't get
743 * the lock, but we also don't want to spin
744 * nor do we want to disable interrupts,
745 * so if we miss here, then better luck next time.
747 if (!spin_trylock(&trace_cmdline_lock))
750 idx = map_pid_to_cmdline[tsk->pid];
751 if (idx >= SAVED_CMDLINES) {
752 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
754 map = map_cmdline_to_pid[idx];
755 if (map <= PID_MAX_DEFAULT)
756 map_pid_to_cmdline[map] = (unsigned)-1;
758 map_pid_to_cmdline[tsk->pid] = idx;
763 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
765 spin_unlock(&trace_cmdline_lock);
768 static char *trace_find_cmdline(int pid)
770 char *cmdline = "<...>";
776 if (pid > PID_MAX_DEFAULT)
779 map = map_pid_to_cmdline[pid];
780 if (map >= SAVED_CMDLINES)
783 cmdline = saved_cmdlines[map];
789 void tracing_record_cmdline(struct task_struct *tsk)
791 if (atomic_read(&trace_record_cmdline_disabled))
794 trace_save_cmdline(tsk);
798 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
801 struct task_struct *tsk = current;
803 entry->preempt_count = pc & 0xff;
804 entry->pid = (tsk) ? tsk->pid : 0;
806 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
807 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
809 TRACE_FLAG_IRQS_NOSUPPORT |
811 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
812 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
813 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
817 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
818 unsigned long ip, unsigned long parent_ip, unsigned long flags,
821 struct ring_buffer_event *event;
822 struct ftrace_entry *entry;
823 unsigned long irq_flags;
825 /* If we are reading the ring buffer, don't trace */
826 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
829 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
833 entry = ring_buffer_event_data(event);
834 tracing_generic_entry_update(&entry->ent, flags, pc);
835 entry->ent.type = TRACE_FN;
837 entry->parent_ip = parent_ip;
838 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
841 #ifdef CONFIG_FUNCTION_RET_TRACER
842 static void __trace_function_return(struct trace_array *tr,
843 struct trace_array_cpu *data,
844 struct ftrace_retfunc *trace,
848 struct ring_buffer_event *event;
849 struct ftrace_ret_entry *entry;
850 unsigned long irq_flags;
852 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
855 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
859 entry = ring_buffer_event_data(event);
860 tracing_generic_entry_update(&entry->ent, flags, pc);
861 entry->ent.type = TRACE_FN_RET;
862 entry->ip = trace->func;
863 entry->parent_ip = trace->ret;
864 entry->rettime = trace->rettime;
865 entry->calltime = trace->calltime;
866 entry->overrun = trace->overrun;
867 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
872 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
873 unsigned long ip, unsigned long parent_ip, unsigned long flags,
876 if (likely(!atomic_read(&data->disabled)))
877 trace_function(tr, data, ip, parent_ip, flags, pc);
880 static void ftrace_trace_stack(struct trace_array *tr,
881 struct trace_array_cpu *data,
885 #ifdef CONFIG_STACKTRACE
886 struct ring_buffer_event *event;
887 struct stack_entry *entry;
888 struct stack_trace trace;
889 unsigned long irq_flags;
891 if (!(trace_flags & TRACE_ITER_STACKTRACE))
894 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
898 entry = ring_buffer_event_data(event);
899 tracing_generic_entry_update(&entry->ent, flags, pc);
900 entry->ent.type = TRACE_STACK;
902 memset(&entry->caller, 0, sizeof(entry->caller));
904 trace.nr_entries = 0;
905 trace.max_entries = FTRACE_STACK_ENTRIES;
907 trace.entries = entry->caller;
909 save_stack_trace(&trace);
910 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
914 void __trace_stack(struct trace_array *tr,
915 struct trace_array_cpu *data,
919 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
922 static void ftrace_trace_userstack(struct trace_array *tr,
923 struct trace_array_cpu *data,
924 unsigned long flags, int pc)
926 struct userstack_entry *entry;
927 struct stack_trace trace;
928 struct ring_buffer_event *event;
929 unsigned long irq_flags;
931 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
934 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
938 entry = ring_buffer_event_data(event);
939 tracing_generic_entry_update(&entry->ent, flags, pc);
940 entry->ent.type = TRACE_USER_STACK;
942 memset(&entry->caller, 0, sizeof(entry->caller));
944 trace.nr_entries = 0;
945 trace.max_entries = FTRACE_STACK_ENTRIES;
947 trace.entries = entry->caller;
949 save_stack_trace_user(&trace);
950 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
953 void __trace_userstack(struct trace_array *tr,
954 struct trace_array_cpu *data,
957 ftrace_trace_userstack(tr, data, flags, preempt_count());
961 ftrace_trace_special(void *__tr, void *__data,
962 unsigned long arg1, unsigned long arg2, unsigned long arg3,
965 struct ring_buffer_event *event;
966 struct trace_array_cpu *data = __data;
967 struct trace_array *tr = __tr;
968 struct special_entry *entry;
969 unsigned long irq_flags;
971 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
975 entry = ring_buffer_event_data(event);
976 tracing_generic_entry_update(&entry->ent, 0, pc);
977 entry->ent.type = TRACE_SPECIAL;
981 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
982 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
983 ftrace_trace_userstack(tr, data, irq_flags, pc);
989 __trace_special(void *__tr, void *__data,
990 unsigned long arg1, unsigned long arg2, unsigned long arg3)
992 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
996 tracing_sched_switch_trace(struct trace_array *tr,
997 struct trace_array_cpu *data,
998 struct task_struct *prev,
999 struct task_struct *next,
1000 unsigned long flags, int pc)
1002 struct ring_buffer_event *event;
1003 struct ctx_switch_entry *entry;
1004 unsigned long irq_flags;
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_CTX;
1013 entry->prev_pid = prev->pid;
1014 entry->prev_prio = prev->prio;
1015 entry->prev_state = prev->state;
1016 entry->next_pid = next->pid;
1017 entry->next_prio = next->prio;
1018 entry->next_state = next->state;
1019 entry->next_cpu = task_cpu(next);
1020 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1021 ftrace_trace_stack(tr, data, flags, 5, pc);
1022 ftrace_trace_userstack(tr, data, flags, pc);
1026 tracing_sched_wakeup_trace(struct trace_array *tr,
1027 struct trace_array_cpu *data,
1028 struct task_struct *wakee,
1029 struct task_struct *curr,
1030 unsigned long flags, int pc)
1032 struct ring_buffer_event *event;
1033 struct ctx_switch_entry *entry;
1034 unsigned long irq_flags;
1036 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1040 entry = ring_buffer_event_data(event);
1041 tracing_generic_entry_update(&entry->ent, flags, pc);
1042 entry->ent.type = TRACE_WAKE;
1043 entry->prev_pid = curr->pid;
1044 entry->prev_prio = curr->prio;
1045 entry->prev_state = curr->state;
1046 entry->next_pid = wakee->pid;
1047 entry->next_prio = wakee->prio;
1048 entry->next_state = wakee->state;
1049 entry->next_cpu = task_cpu(wakee);
1050 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1051 ftrace_trace_stack(tr, data, flags, 6, pc);
1052 ftrace_trace_userstack(tr, data, flags, pc);
1058 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1060 struct trace_array *tr = &global_trace;
1061 struct trace_array_cpu *data;
1062 unsigned long flags;
1066 if (tracing_disabled)
1069 pc = preempt_count();
1070 local_irq_save(flags);
1071 cpu = raw_smp_processor_id();
1072 data = tr->data[cpu];
1074 if (likely(atomic_inc_return(&data->disabled) == 1))
1075 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1077 atomic_dec(&data->disabled);
1078 local_irq_restore(flags);
1081 #ifdef CONFIG_FUNCTION_TRACER
1083 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
1085 struct trace_array *tr = &global_trace;
1086 struct trace_array_cpu *data;
1087 unsigned long flags;
1092 if (unlikely(!ftrace_function_enabled))
1095 pc = preempt_count();
1096 resched = ftrace_preempt_disable();
1097 local_save_flags(flags);
1098 cpu = raw_smp_processor_id();
1099 data = tr->data[cpu];
1100 disabled = atomic_inc_return(&data->disabled);
1102 if (likely(disabled == 1))
1103 trace_function(tr, data, ip, parent_ip, flags, pc);
1105 atomic_dec(&data->disabled);
1106 ftrace_preempt_enable(resched);
1110 function_trace_call(unsigned long ip, unsigned long parent_ip)
1112 struct trace_array *tr = &global_trace;
1113 struct trace_array_cpu *data;
1114 unsigned long flags;
1119 if (unlikely(!ftrace_function_enabled))
1123 * Need to use raw, since this must be called before the
1124 * recursive protection is performed.
1126 local_irq_save(flags);
1127 cpu = raw_smp_processor_id();
1128 data = tr->data[cpu];
1129 disabled = atomic_inc_return(&data->disabled);
1131 if (likely(disabled == 1)) {
1132 pc = preempt_count();
1133 trace_function(tr, data, ip, parent_ip, flags, pc);
1136 atomic_dec(&data->disabled);
1137 local_irq_restore(flags);
1140 #ifdef CONFIG_FUNCTION_RET_TRACER
1141 void trace_function_return(struct ftrace_retfunc *trace)
1143 struct trace_array *tr = &global_trace;
1144 struct trace_array_cpu *data;
1145 unsigned long flags;
1150 raw_local_irq_save(flags);
1151 cpu = raw_smp_processor_id();
1152 data = tr->data[cpu];
1153 disabled = atomic_inc_return(&data->disabled);
1154 if (likely(disabled == 1)) {
1155 pc = preempt_count();
1156 __trace_function_return(tr, data, trace, flags, pc);
1158 atomic_dec(&data->disabled);
1159 raw_local_irq_restore(flags);
1161 #endif /* CONFIG_FUNCTION_RET_TRACER */
1163 static struct ftrace_ops trace_ops __read_mostly =
1165 .func = function_trace_call,
1168 void tracing_start_function_trace(void)
1170 ftrace_function_enabled = 0;
1172 if (trace_flags & TRACE_ITER_PREEMPTONLY)
1173 trace_ops.func = function_trace_call_preempt_only;
1175 trace_ops.func = function_trace_call;
1177 register_ftrace_function(&trace_ops);
1178 ftrace_function_enabled = 1;
1181 void tracing_stop_function_trace(void)
1183 ftrace_function_enabled = 0;
1184 unregister_ftrace_function(&trace_ops);
1188 enum trace_file_type {
1189 TRACE_FILE_LAT_FMT = 1,
1190 TRACE_FILE_ANNOTATE = 2,
1193 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1195 /* Don't allow ftrace to trace into the ring buffers */
1196 ftrace_disable_cpu();
1199 if (iter->buffer_iter[iter->cpu])
1200 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1202 ftrace_enable_cpu();
1205 static struct trace_entry *
1206 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1208 struct ring_buffer_event *event;
1209 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1211 /* Don't allow ftrace to trace into the ring buffers */
1212 ftrace_disable_cpu();
1215 event = ring_buffer_iter_peek(buf_iter, ts);
1217 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1219 ftrace_enable_cpu();
1221 return event ? ring_buffer_event_data(event) : NULL;
1224 static struct trace_entry *
1225 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1227 struct ring_buffer *buffer = iter->tr->buffer;
1228 struct trace_entry *ent, *next = NULL;
1229 u64 next_ts = 0, ts;
1233 for_each_tracing_cpu(cpu) {
1235 if (ring_buffer_empty_cpu(buffer, cpu))
1238 ent = peek_next_entry(iter, cpu, &ts);
1241 * Pick the entry with the smallest timestamp:
1243 if (ent && (!next || ts < next_ts)) {
1251 *ent_cpu = next_cpu;
1259 /* Find the next real entry, without updating the iterator itself */
1260 static struct trace_entry *
1261 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1263 return __find_next_entry(iter, ent_cpu, ent_ts);
1266 /* Find the next real entry, and increment the iterator to the next entry */
1267 static void *find_next_entry_inc(struct trace_iterator *iter)
1269 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1272 trace_iterator_increment(iter, iter->cpu);
1274 return iter->ent ? iter : NULL;
1277 static void trace_consume(struct trace_iterator *iter)
1279 /* Don't allow ftrace to trace into the ring buffers */
1280 ftrace_disable_cpu();
1281 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1282 ftrace_enable_cpu();
1285 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1287 struct trace_iterator *iter = m->private;
1293 /* can't go backwards */
1298 ent = find_next_entry_inc(iter);
1302 while (ent && iter->idx < i)
1303 ent = find_next_entry_inc(iter);
1310 static void *s_start(struct seq_file *m, loff_t *pos)
1312 struct trace_iterator *iter = m->private;
1317 mutex_lock(&trace_types_lock);
1319 if (!current_trace || current_trace != iter->trace) {
1320 mutex_unlock(&trace_types_lock);
1324 atomic_inc(&trace_record_cmdline_disabled);
1326 if (*pos != iter->pos) {
1331 ftrace_disable_cpu();
1333 for_each_tracing_cpu(cpu) {
1334 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1337 ftrace_enable_cpu();
1339 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1344 p = s_next(m, p, &l);
1350 static void s_stop(struct seq_file *m, void *p)
1352 atomic_dec(&trace_record_cmdline_disabled);
1353 mutex_unlock(&trace_types_lock);
1356 #ifdef CONFIG_KRETPROBES
1357 static inline const char *kretprobed(const char *name)
1359 static const char tramp_name[] = "kretprobe_trampoline";
1360 int size = sizeof(tramp_name);
1362 if (strncmp(tramp_name, name, size) == 0)
1363 return "[unknown/kretprobe'd]";
1367 static inline const char *kretprobed(const char *name)
1371 #endif /* CONFIG_KRETPROBES */
1374 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1376 #ifdef CONFIG_KALLSYMS
1377 char str[KSYM_SYMBOL_LEN];
1380 kallsyms_lookup(address, NULL, NULL, NULL, str);
1382 name = kretprobed(str);
1384 return trace_seq_printf(s, fmt, name);
1390 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1391 unsigned long address)
1393 #ifdef CONFIG_KALLSYMS
1394 char str[KSYM_SYMBOL_LEN];
1397 sprint_symbol(str, address);
1398 name = kretprobed(str);
1400 return trace_seq_printf(s, fmt, name);
1405 #ifndef CONFIG_64BIT
1406 # define IP_FMT "%08lx"
1408 # define IP_FMT "%016lx"
1412 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1417 return trace_seq_printf(s, "0");
1419 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1420 ret = seq_print_sym_offset(s, "%s", ip);
1422 ret = seq_print_sym_short(s, "%s", ip);
1427 if (sym_flags & TRACE_ITER_SYM_ADDR)
1428 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1433 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
1434 unsigned long sym_flags)
1439 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1440 unsigned long ip = entry->caller[i];
1442 if (ip == ULONG_MAX || !ret)
1445 ret = trace_seq_puts(s, " <- ");
1447 ret = trace_seq_puts(s, "??");
1450 if (ret /*&& (sym_flags & TRACE_ITER_SYM_ADDR)*/)
1451 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1457 static void print_lat_help_header(struct seq_file *m)
1459 seq_puts(m, "# _------=> CPU# \n");
1460 seq_puts(m, "# / _-----=> irqs-off \n");
1461 seq_puts(m, "# | / _----=> need-resched \n");
1462 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1463 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1464 seq_puts(m, "# |||| / \n");
1465 seq_puts(m, "# ||||| delay \n");
1466 seq_puts(m, "# cmd pid ||||| time | caller \n");
1467 seq_puts(m, "# \\ / ||||| \\ | / \n");
1470 static void print_func_help_header(struct seq_file *m)
1472 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1473 seq_puts(m, "# | | | | |\n");
1478 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1480 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1481 struct trace_array *tr = iter->tr;
1482 struct trace_array_cpu *data = tr->data[tr->cpu];
1483 struct tracer *type = current_trace;
1484 unsigned long total;
1485 unsigned long entries;
1486 const char *name = "preemption";
1491 entries = ring_buffer_entries(iter->tr->buffer);
1493 ring_buffer_overruns(iter->tr->buffer);
1495 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1497 seq_puts(m, "-----------------------------------"
1498 "---------------------------------\n");
1499 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1500 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1501 nsecs_to_usecs(data->saved_latency),
1505 #if defined(CONFIG_PREEMPT_NONE)
1507 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1509 #elif defined(CONFIG_PREEMPT)
1514 /* These are reserved for later use */
1517 seq_printf(m, " #P:%d)\n", num_online_cpus());
1521 seq_puts(m, " -----------------\n");
1522 seq_printf(m, " | task: %.16s-%d "
1523 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1524 data->comm, data->pid, data->uid, data->nice,
1525 data->policy, data->rt_priority);
1526 seq_puts(m, " -----------------\n");
1528 if (data->critical_start) {
1529 seq_puts(m, " => started at: ");
1530 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1531 trace_print_seq(m, &iter->seq);
1532 seq_puts(m, "\n => ended at: ");
1533 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1534 trace_print_seq(m, &iter->seq);
1542 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1544 int hardirq, softirq;
1547 comm = trace_find_cmdline(entry->pid);
1549 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1550 trace_seq_printf(s, "%3d", cpu);
1551 trace_seq_printf(s, "%c%c",
1552 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1553 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1554 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1556 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1557 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1558 if (hardirq && softirq) {
1559 trace_seq_putc(s, 'H');
1562 trace_seq_putc(s, 'h');
1565 trace_seq_putc(s, 's');
1567 trace_seq_putc(s, '.');
1571 if (entry->preempt_count)
1572 trace_seq_printf(s, "%x", entry->preempt_count);
1574 trace_seq_puts(s, ".");
1577 unsigned long preempt_mark_thresh = 100;
1580 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1581 unsigned long rel_usecs)
1583 trace_seq_printf(s, " %4lldus", abs_usecs);
1584 if (rel_usecs > preempt_mark_thresh)
1585 trace_seq_puts(s, "!: ");
1586 else if (rel_usecs > 1)
1587 trace_seq_puts(s, "+: ");
1589 trace_seq_puts(s, " : ");
1592 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1595 * The message is supposed to contain an ending newline.
1596 * If the printing stops prematurely, try to add a newline of our own.
1598 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1600 struct trace_entry *ent;
1601 struct trace_field_cont *cont;
1604 ent = peek_next_entry(iter, iter->cpu, NULL);
1605 if (!ent || ent->type != TRACE_CONT) {
1606 trace_seq_putc(s, '\n');
1611 cont = (struct trace_field_cont *)ent;
1613 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1615 ftrace_disable_cpu();
1617 if (iter->buffer_iter[iter->cpu])
1618 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1620 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1622 ftrace_enable_cpu();
1624 ent = peek_next_entry(iter, iter->cpu, NULL);
1625 } while (ent && ent->type == TRACE_CONT);
1628 trace_seq_putc(s, '\n');
1631 static void test_cpu_buff_start(struct trace_iterator *iter)
1633 struct trace_seq *s = &iter->seq;
1635 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1638 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1641 if (cpu_isset(iter->cpu, iter->started))
1644 cpu_set(iter->cpu, iter->started);
1645 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1648 static enum print_line_t
1649 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1651 struct trace_seq *s = &iter->seq;
1652 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1653 struct trace_entry *next_entry;
1654 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1655 struct trace_entry *entry = iter->ent;
1656 unsigned long abs_usecs;
1657 unsigned long rel_usecs;
1664 if (entry->type == TRACE_CONT)
1665 return TRACE_TYPE_HANDLED;
1667 test_cpu_buff_start(iter);
1669 next_entry = find_next_entry(iter, NULL, &next_ts);
1672 rel_usecs = ns2usecs(next_ts - iter->ts);
1673 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1676 comm = trace_find_cmdline(entry->pid);
1677 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1678 " %ld.%03ldms (+%ld.%03ldms): ",
1680 entry->pid, cpu, entry->flags,
1681 entry->preempt_count, trace_idx,
1684 abs_usecs % 1000, rel_usecs/1000,
1687 lat_print_generic(s, entry, cpu);
1688 lat_print_timestamp(s, abs_usecs, rel_usecs);
1690 switch (entry->type) {
1692 struct ftrace_entry *field;
1694 trace_assign_type(field, entry);
1696 seq_print_ip_sym(s, field->ip, sym_flags);
1697 trace_seq_puts(s, " (");
1698 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1699 trace_seq_puts(s, ")\n");
1704 struct ctx_switch_entry *field;
1706 trace_assign_type(field, entry);
1708 T = field->next_state < sizeof(state_to_char) ?
1709 state_to_char[field->next_state] : 'X';
1711 state = field->prev_state ?
1712 __ffs(field->prev_state) + 1 : 0;
1713 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1714 comm = trace_find_cmdline(field->next_pid);
1715 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1718 S, entry->type == TRACE_CTX ? "==>" : " +",
1725 case TRACE_SPECIAL: {
1726 struct special_entry *field;
1728 trace_assign_type(field, entry);
1730 trace_seq_printf(s, "# %ld %ld %ld\n",
1737 struct stack_entry *field;
1739 trace_assign_type(field, entry);
1741 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1743 trace_seq_puts(s, " <= ");
1744 seq_print_ip_sym(s, field->caller[i], sym_flags);
1746 trace_seq_puts(s, "\n");
1750 struct print_entry *field;
1752 trace_assign_type(field, entry);
1754 seq_print_ip_sym(s, field->ip, sym_flags);
1755 trace_seq_printf(s, ": %s", field->buf);
1756 if (entry->flags & TRACE_FLAG_CONT)
1757 trace_seq_print_cont(s, iter);
1760 case TRACE_BRANCH: {
1761 struct trace_branch *field;
1763 trace_assign_type(field, entry);
1765 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1766 field->correct ? " ok " : " MISS ",
1772 case TRACE_USER_STACK: {
1773 struct userstack_entry *field;
1775 trace_assign_type(field, entry);
1777 seq_print_userip_objs(field, s, sym_flags);
1778 if (entry->flags & TRACE_FLAG_CONT)
1779 trace_seq_print_cont(s, iter);
1783 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1785 return TRACE_TYPE_HANDLED;
1788 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1790 struct trace_seq *s = &iter->seq;
1791 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1792 struct trace_entry *entry;
1793 unsigned long usec_rem;
1794 unsigned long long t;
1803 if (entry->type == TRACE_CONT)
1804 return TRACE_TYPE_HANDLED;
1806 test_cpu_buff_start(iter);
1808 comm = trace_find_cmdline(iter->ent->pid);
1810 t = ns2usecs(iter->ts);
1811 usec_rem = do_div(t, 1000000ULL);
1812 secs = (unsigned long)t;
1814 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1816 return TRACE_TYPE_PARTIAL_LINE;
1817 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1819 return TRACE_TYPE_PARTIAL_LINE;
1820 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1822 return TRACE_TYPE_PARTIAL_LINE;
1824 switch (entry->type) {
1826 struct ftrace_entry *field;
1828 trace_assign_type(field, entry);
1830 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1832 return TRACE_TYPE_PARTIAL_LINE;
1833 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1835 ret = trace_seq_printf(s, " <-");
1837 return TRACE_TYPE_PARTIAL_LINE;
1838 ret = seq_print_ip_sym(s,
1842 return TRACE_TYPE_PARTIAL_LINE;
1844 ret = trace_seq_printf(s, "\n");
1846 return TRACE_TYPE_PARTIAL_LINE;
1851 struct ctx_switch_entry *field;
1853 trace_assign_type(field, entry);
1855 S = field->prev_state < sizeof(state_to_char) ?
1856 state_to_char[field->prev_state] : 'X';
1857 T = field->next_state < sizeof(state_to_char) ?
1858 state_to_char[field->next_state] : 'X';
1859 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1863 entry->type == TRACE_CTX ? "==>" : " +",
1869 return TRACE_TYPE_PARTIAL_LINE;
1872 case TRACE_SPECIAL: {
1873 struct special_entry *field;
1875 trace_assign_type(field, entry);
1877 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1882 return TRACE_TYPE_PARTIAL_LINE;
1886 struct stack_entry *field;
1888 trace_assign_type(field, entry);
1890 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1892 ret = trace_seq_puts(s, " <= ");
1894 return TRACE_TYPE_PARTIAL_LINE;
1896 ret = seq_print_ip_sym(s, field->caller[i],
1899 return TRACE_TYPE_PARTIAL_LINE;
1901 ret = trace_seq_puts(s, "\n");
1903 return TRACE_TYPE_PARTIAL_LINE;
1907 struct print_entry *field;
1909 trace_assign_type(field, entry);
1911 seq_print_ip_sym(s, field->ip, sym_flags);
1912 trace_seq_printf(s, ": %s", field->buf);
1913 if (entry->flags & TRACE_FLAG_CONT)
1914 trace_seq_print_cont(s, iter);
1917 case TRACE_FN_RET: {
1918 return print_return_function(iter);
1921 case TRACE_BRANCH: {
1922 struct trace_branch *field;
1924 trace_assign_type(field, entry);
1926 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1927 field->correct ? " ok " : " MISS ",
1933 case TRACE_USER_STACK: {
1934 struct userstack_entry *field;
1936 trace_assign_type(field, entry);
1938 ret = seq_print_userip_objs(field, s, sym_flags);
1940 return TRACE_TYPE_PARTIAL_LINE;
1941 ret = trace_seq_putc(s, '\n');
1943 return TRACE_TYPE_PARTIAL_LINE;
1947 return TRACE_TYPE_HANDLED;
1950 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1952 struct trace_seq *s = &iter->seq;
1953 struct trace_entry *entry;
1959 if (entry->type == TRACE_CONT)
1960 return TRACE_TYPE_HANDLED;
1962 ret = trace_seq_printf(s, "%d %d %llu ",
1963 entry->pid, iter->cpu, iter->ts);
1965 return TRACE_TYPE_PARTIAL_LINE;
1967 switch (entry->type) {
1969 struct ftrace_entry *field;
1971 trace_assign_type(field, entry);
1973 ret = trace_seq_printf(s, "%x %x\n",
1977 return TRACE_TYPE_PARTIAL_LINE;
1982 struct ctx_switch_entry *field;
1984 trace_assign_type(field, entry);
1986 S = field->prev_state < sizeof(state_to_char) ?
1987 state_to_char[field->prev_state] : 'X';
1988 T = field->next_state < sizeof(state_to_char) ?
1989 state_to_char[field->next_state] : 'X';
1990 if (entry->type == TRACE_WAKE)
1992 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
2001 return TRACE_TYPE_PARTIAL_LINE;
2005 case TRACE_USER_STACK:
2007 struct special_entry *field;
2009 trace_assign_type(field, entry);
2011 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
2016 return TRACE_TYPE_PARTIAL_LINE;
2020 struct print_entry *field;
2022 trace_assign_type(field, entry);
2024 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
2025 if (entry->flags & TRACE_FLAG_CONT)
2026 trace_seq_print_cont(s, iter);
2030 return TRACE_TYPE_HANDLED;
2033 #define SEQ_PUT_FIELD_RET(s, x) \
2035 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
2039 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
2041 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
2042 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
2046 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2048 struct trace_seq *s = &iter->seq;
2049 unsigned char newline = '\n';
2050 struct trace_entry *entry;
2055 if (entry->type == TRACE_CONT)
2056 return TRACE_TYPE_HANDLED;
2058 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2059 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2060 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2062 switch (entry->type) {
2064 struct ftrace_entry *field;
2066 trace_assign_type(field, entry);
2068 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
2069 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
2074 struct ctx_switch_entry *field;
2076 trace_assign_type(field, entry);
2078 S = field->prev_state < sizeof(state_to_char) ?
2079 state_to_char[field->prev_state] : 'X';
2080 T = field->next_state < sizeof(state_to_char) ?
2081 state_to_char[field->next_state] : 'X';
2082 if (entry->type == TRACE_WAKE)
2084 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
2085 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
2086 SEQ_PUT_HEX_FIELD_RET(s, S);
2087 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
2088 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
2089 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
2090 SEQ_PUT_HEX_FIELD_RET(s, T);
2094 case TRACE_USER_STACK:
2096 struct special_entry *field;
2098 trace_assign_type(field, entry);
2100 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
2101 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
2102 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
2106 SEQ_PUT_FIELD_RET(s, newline);
2108 return TRACE_TYPE_HANDLED;
2111 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2113 struct trace_seq *s = &iter->seq;
2114 struct trace_entry *entry;
2118 if (entry->type == TRACE_CONT)
2119 return TRACE_TYPE_HANDLED;
2121 SEQ_PUT_FIELD_RET(s, entry->pid);
2122 SEQ_PUT_FIELD_RET(s, entry->cpu);
2123 SEQ_PUT_FIELD_RET(s, iter->ts);
2125 switch (entry->type) {
2127 struct ftrace_entry *field;
2129 trace_assign_type(field, entry);
2131 SEQ_PUT_FIELD_RET(s, field->ip);
2132 SEQ_PUT_FIELD_RET(s, field->parent_ip);
2136 struct ctx_switch_entry *field;
2138 trace_assign_type(field, entry);
2140 SEQ_PUT_FIELD_RET(s, field->prev_pid);
2141 SEQ_PUT_FIELD_RET(s, field->prev_prio);
2142 SEQ_PUT_FIELD_RET(s, field->prev_state);
2143 SEQ_PUT_FIELD_RET(s, field->next_pid);
2144 SEQ_PUT_FIELD_RET(s, field->next_prio);
2145 SEQ_PUT_FIELD_RET(s, field->next_state);
2149 case TRACE_USER_STACK:
2151 struct special_entry *field;
2153 trace_assign_type(field, entry);
2155 SEQ_PUT_FIELD_RET(s, field->arg1);
2156 SEQ_PUT_FIELD_RET(s, field->arg2);
2157 SEQ_PUT_FIELD_RET(s, field->arg3);
2164 static int trace_empty(struct trace_iterator *iter)
2168 for_each_tracing_cpu(cpu) {
2169 if (iter->buffer_iter[cpu]) {
2170 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2173 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2181 static enum print_line_t print_trace_line(struct trace_iterator *iter)
2183 enum print_line_t ret;
2185 if (iter->trace && iter->trace->print_line) {
2186 ret = iter->trace->print_line(iter);
2187 if (ret != TRACE_TYPE_UNHANDLED)
2191 if (trace_flags & TRACE_ITER_BIN)
2192 return print_bin_fmt(iter);
2194 if (trace_flags & TRACE_ITER_HEX)
2195 return print_hex_fmt(iter);
2197 if (trace_flags & TRACE_ITER_RAW)
2198 return print_raw_fmt(iter);
2200 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2201 return print_lat_fmt(iter, iter->idx, iter->cpu);
2203 return print_trace_fmt(iter);
2206 static int s_show(struct seq_file *m, void *v)
2208 struct trace_iterator *iter = v;
2210 if (iter->ent == NULL) {
2212 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2215 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2216 /* print nothing if the buffers are empty */
2217 if (trace_empty(iter))
2219 print_trace_header(m, iter);
2220 if (!(trace_flags & TRACE_ITER_VERBOSE))
2221 print_lat_help_header(m);
2223 if (!(trace_flags & TRACE_ITER_VERBOSE))
2224 print_func_help_header(m);
2227 print_trace_line(iter);
2228 trace_print_seq(m, &iter->seq);
2234 static struct seq_operations tracer_seq_ops = {
2241 static struct trace_iterator *
2242 __tracing_open(struct inode *inode, struct file *file, int *ret)
2244 struct trace_iterator *iter;
2248 if (tracing_disabled) {
2253 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2259 mutex_lock(&trace_types_lock);
2260 if (current_trace && current_trace->print_max)
2263 iter->tr = inode->i_private;
2264 iter->trace = current_trace;
2267 /* Annotate start of buffers if we had overruns */
2268 if (ring_buffer_overruns(iter->tr->buffer))
2269 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2272 for_each_tracing_cpu(cpu) {
2274 iter->buffer_iter[cpu] =
2275 ring_buffer_read_start(iter->tr->buffer, cpu);
2277 if (!iter->buffer_iter[cpu])
2281 /* TODO stop tracer */
2282 *ret = seq_open(file, &tracer_seq_ops);
2286 m = file->private_data;
2289 /* stop the trace while dumping */
2292 if (iter->trace && iter->trace->open)
2293 iter->trace->open(iter);
2295 mutex_unlock(&trace_types_lock);
2301 for_each_tracing_cpu(cpu) {
2302 if (iter->buffer_iter[cpu])
2303 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2305 mutex_unlock(&trace_types_lock);
2308 return ERR_PTR(-ENOMEM);
2311 int tracing_open_generic(struct inode *inode, struct file *filp)
2313 if (tracing_disabled)
2316 filp->private_data = inode->i_private;
2320 int tracing_release(struct inode *inode, struct file *file)
2322 struct seq_file *m = (struct seq_file *)file->private_data;
2323 struct trace_iterator *iter = m->private;
2326 mutex_lock(&trace_types_lock);
2327 for_each_tracing_cpu(cpu) {
2328 if (iter->buffer_iter[cpu])
2329 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2332 if (iter->trace && iter->trace->close)
2333 iter->trace->close(iter);
2335 /* reenable tracing if it was previously enabled */
2337 mutex_unlock(&trace_types_lock);
2339 seq_release(inode, file);
2344 static int tracing_open(struct inode *inode, struct file *file)
2348 __tracing_open(inode, file, &ret);
2353 static int tracing_lt_open(struct inode *inode, struct file *file)
2355 struct trace_iterator *iter;
2358 iter = __tracing_open(inode, file, &ret);
2361 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2368 t_next(struct seq_file *m, void *v, loff_t *pos)
2370 struct tracer *t = m->private;
2382 static void *t_start(struct seq_file *m, loff_t *pos)
2384 struct tracer *t = m->private;
2387 mutex_lock(&trace_types_lock);
2388 for (; t && l < *pos; t = t_next(m, t, &l))
2394 static void t_stop(struct seq_file *m, void *p)
2396 mutex_unlock(&trace_types_lock);
2399 static int t_show(struct seq_file *m, void *v)
2401 struct tracer *t = v;
2406 seq_printf(m, "%s", t->name);
2415 static struct seq_operations show_traces_seq_ops = {
2422 static int show_traces_open(struct inode *inode, struct file *file)
2426 if (tracing_disabled)
2429 ret = seq_open(file, &show_traces_seq_ops);
2431 struct seq_file *m = file->private_data;
2432 m->private = trace_types;
2438 static struct file_operations tracing_fops = {
2439 .open = tracing_open,
2441 .llseek = seq_lseek,
2442 .release = tracing_release,
2445 static struct file_operations tracing_lt_fops = {
2446 .open = tracing_lt_open,
2448 .llseek = seq_lseek,
2449 .release = tracing_release,
2452 static struct file_operations show_traces_fops = {
2453 .open = show_traces_open,
2455 .release = seq_release,
2459 * Only trace on a CPU if the bitmask is set:
2461 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2464 * When tracing/tracing_cpu_mask is modified then this holds
2465 * the new bitmask we are about to install:
2467 static cpumask_t tracing_cpumask_new;
2470 * The tracer itself will not take this lock, but still we want
2471 * to provide a consistent cpumask to user-space:
2473 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2476 * Temporary storage for the character representation of the
2477 * CPU bitmask (and one more byte for the newline):
2479 static char mask_str[NR_CPUS + 1];
2482 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2483 size_t count, loff_t *ppos)
2487 mutex_lock(&tracing_cpumask_update_lock);
2489 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2490 if (count - len < 2) {
2494 len += sprintf(mask_str + len, "\n");
2495 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2498 mutex_unlock(&tracing_cpumask_update_lock);
2504 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2505 size_t count, loff_t *ppos)
2509 mutex_lock(&tracing_cpumask_update_lock);
2510 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2514 raw_local_irq_disable();
2515 __raw_spin_lock(&ftrace_max_lock);
2516 for_each_tracing_cpu(cpu) {
2518 * Increase/decrease the disabled counter if we are
2519 * about to flip a bit in the cpumask:
2521 if (cpu_isset(cpu, tracing_cpumask) &&
2522 !cpu_isset(cpu, tracing_cpumask_new)) {
2523 atomic_inc(&global_trace.data[cpu]->disabled);
2525 if (!cpu_isset(cpu, tracing_cpumask) &&
2526 cpu_isset(cpu, tracing_cpumask_new)) {
2527 atomic_dec(&global_trace.data[cpu]->disabled);
2530 __raw_spin_unlock(&ftrace_max_lock);
2531 raw_local_irq_enable();
2533 tracing_cpumask = tracing_cpumask_new;
2535 mutex_unlock(&tracing_cpumask_update_lock);
2540 mutex_unlock(&tracing_cpumask_update_lock);
2545 static struct file_operations tracing_cpumask_fops = {
2546 .open = tracing_open_generic,
2547 .read = tracing_cpumask_read,
2548 .write = tracing_cpumask_write,
2552 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2553 size_t cnt, loff_t *ppos)
2559 u32 tracer_flags = current_trace->flags->val;
2560 struct tracer_opt *trace_opts = current_trace->flags->opts;
2563 /* calulate max size */
2564 for (i = 0; trace_options[i]; i++) {
2565 len += strlen(trace_options[i]);
2566 len += 3; /* "no" and space */
2570 * Increase the size with names of options specific
2571 * of the current tracer.
2573 for (i = 0; trace_opts[i].name; i++) {
2574 len += strlen(trace_opts[i].name);
2575 len += 3; /* "no" and space */
2578 /* +2 for \n and \0 */
2579 buf = kmalloc(len + 2, GFP_KERNEL);
2583 for (i = 0; trace_options[i]; i++) {
2584 if (trace_flags & (1 << i))
2585 r += sprintf(buf + r, "%s ", trace_options[i]);
2587 r += sprintf(buf + r, "no%s ", trace_options[i]);
2590 for (i = 0; trace_opts[i].name; i++) {
2591 if (tracer_flags & trace_opts[i].bit)
2592 r += sprintf(buf + r, "%s ",
2593 trace_opts[i].name);
2595 r += sprintf(buf + r, "no%s ",
2596 trace_opts[i].name);
2599 r += sprintf(buf + r, "\n");
2600 WARN_ON(r >= len + 2);
2602 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2609 /* Try to assign a tracer specific option */
2610 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2612 struct tracer_flags *trace_flags = trace->flags;
2613 struct tracer_opt *opts = NULL;
2617 for (i = 0; trace_flags->opts[i].name; i++) {
2618 opts = &trace_flags->opts[i];
2619 len = strlen(opts->name);
2621 if (strncmp(cmp, opts->name, len) == 0) {
2622 ret = trace->set_flag(trace_flags->val,
2628 if (!trace_flags->opts[i].name)
2631 /* Refused to handle */
2636 trace_flags->val &= ~opts->bit;
2638 trace_flags->val |= opts->bit;
2644 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2645 size_t cnt, loff_t *ppos)
2653 if (cnt >= sizeof(buf))
2656 if (copy_from_user(&buf, ubuf, cnt))
2661 if (strncmp(buf, "no", 2) == 0) {
2666 for (i = 0; trace_options[i]; i++) {
2667 int len = strlen(trace_options[i]);
2669 if (strncmp(cmp, trace_options[i], len) == 0) {
2671 trace_flags &= ~(1 << i);
2673 trace_flags |= (1 << i);
2678 /* If no option could be set, test the specific tracer options */
2679 if (!trace_options[i]) {
2680 ret = set_tracer_option(current_trace, cmp, neg);
2690 static struct file_operations tracing_iter_fops = {
2691 .open = tracing_open_generic,
2692 .read = tracing_trace_options_read,
2693 .write = tracing_trace_options_write,
2696 static const char readme_msg[] =
2697 "tracing mini-HOWTO:\n\n"
2699 "# mount -t debugfs nodev /debug\n\n"
2700 "# cat /debug/tracing/available_tracers\n"
2701 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2702 "# cat /debug/tracing/current_tracer\n"
2704 "# echo sched_switch > /debug/tracing/current_tracer\n"
2705 "# cat /debug/tracing/current_tracer\n"
2707 "# cat /debug/tracing/trace_options\n"
2708 "noprint-parent nosym-offset nosym-addr noverbose\n"
2709 "# echo print-parent > /debug/tracing/trace_options\n"
2710 "# echo 1 > /debug/tracing/tracing_enabled\n"
2711 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2712 "echo 0 > /debug/tracing/tracing_enabled\n"
2716 tracing_readme_read(struct file *filp, char __user *ubuf,
2717 size_t cnt, loff_t *ppos)
2719 return simple_read_from_buffer(ubuf, cnt, ppos,
2720 readme_msg, strlen(readme_msg));
2723 static struct file_operations tracing_readme_fops = {
2724 .open = tracing_open_generic,
2725 .read = tracing_readme_read,
2729 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2730 size_t cnt, loff_t *ppos)
2735 r = sprintf(buf, "%u\n", tracer_enabled);
2736 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2740 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2741 size_t cnt, loff_t *ppos)
2743 struct trace_array *tr = filp->private_data;
2748 if (cnt >= sizeof(buf))
2751 if (copy_from_user(&buf, ubuf, cnt))
2756 ret = strict_strtoul(buf, 10, &val);
2762 mutex_lock(&trace_types_lock);
2763 if (tracer_enabled ^ val) {
2766 if (current_trace->start)
2767 current_trace->start(tr);
2772 if (current_trace->stop)
2773 current_trace->stop(tr);
2776 mutex_unlock(&trace_types_lock);
2784 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2785 size_t cnt, loff_t *ppos)
2787 char buf[max_tracer_type_len+2];
2790 mutex_lock(&trace_types_lock);
2792 r = sprintf(buf, "%s\n", current_trace->name);
2794 r = sprintf(buf, "\n");
2795 mutex_unlock(&trace_types_lock);
2797 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2800 static int tracing_set_tracer(char *buf)
2802 struct trace_array *tr = &global_trace;
2806 mutex_lock(&trace_types_lock);
2807 for (t = trace_types; t; t = t->next) {
2808 if (strcmp(t->name, buf) == 0)
2815 if (t == current_trace)
2818 trace_branch_disable();
2819 if (current_trace && current_trace->reset)
2820 current_trace->reset(tr);
2829 trace_branch_enable(tr);
2831 mutex_unlock(&trace_types_lock);
2837 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2838 size_t cnt, loff_t *ppos)
2840 char buf[max_tracer_type_len+1];
2847 if (cnt > max_tracer_type_len)
2848 cnt = max_tracer_type_len;
2850 if (copy_from_user(&buf, ubuf, cnt))
2855 /* strip ending whitespace. */
2856 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2859 err = tracing_set_tracer(buf);
2869 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2870 size_t cnt, loff_t *ppos)
2872 unsigned long *ptr = filp->private_data;
2876 r = snprintf(buf, sizeof(buf), "%ld\n",
2877 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2878 if (r > sizeof(buf))
2880 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2884 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2885 size_t cnt, loff_t *ppos)
2887 long *ptr = filp->private_data;
2892 if (cnt >= sizeof(buf))
2895 if (copy_from_user(&buf, ubuf, cnt))
2900 ret = strict_strtoul(buf, 10, &val);
2909 static atomic_t tracing_reader;
2911 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2913 struct trace_iterator *iter;
2915 if (tracing_disabled)
2918 /* We only allow for reader of the pipe */
2919 if (atomic_inc_return(&tracing_reader) != 1) {
2920 atomic_dec(&tracing_reader);
2924 /* create a buffer to store the information to pass to userspace */
2925 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2929 mutex_lock(&trace_types_lock);
2931 /* trace pipe does not show start of buffer */
2932 cpus_setall(iter->started);
2934 iter->tr = &global_trace;
2935 iter->trace = current_trace;
2936 filp->private_data = iter;
2938 if (iter->trace->pipe_open)
2939 iter->trace->pipe_open(iter);
2940 mutex_unlock(&trace_types_lock);
2945 static int tracing_release_pipe(struct inode *inode, struct file *file)
2947 struct trace_iterator *iter = file->private_data;
2950 atomic_dec(&tracing_reader);
2956 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2958 struct trace_iterator *iter = filp->private_data;
2960 if (trace_flags & TRACE_ITER_BLOCK) {
2962 * Always select as readable when in blocking mode
2964 return POLLIN | POLLRDNORM;
2966 if (!trace_empty(iter))
2967 return POLLIN | POLLRDNORM;
2968 poll_wait(filp, &trace_wait, poll_table);
2969 if (!trace_empty(iter))
2970 return POLLIN | POLLRDNORM;
2980 tracing_read_pipe(struct file *filp, char __user *ubuf,
2981 size_t cnt, loff_t *ppos)
2983 struct trace_iterator *iter = filp->private_data;
2986 /* return any leftover data */
2987 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2991 trace_seq_reset(&iter->seq);
2993 mutex_lock(&trace_types_lock);
2994 if (iter->trace->read) {
2995 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3002 while (trace_empty(iter)) {
3004 if ((filp->f_flags & O_NONBLOCK)) {
3010 * This is a make-shift waitqueue. The reason we don't use
3011 * an actual wait queue is because:
3012 * 1) we only ever have one waiter
3013 * 2) the tracing, traces all functions, we don't want
3014 * the overhead of calling wake_up and friends
3015 * (and tracing them too)
3016 * Anyway, this is really very primitive wakeup.
3018 set_current_state(TASK_INTERRUPTIBLE);
3019 iter->tr->waiter = current;
3021 mutex_unlock(&trace_types_lock);
3023 /* sleep for 100 msecs, and try again. */
3024 schedule_timeout(HZ/10);
3026 mutex_lock(&trace_types_lock);
3028 iter->tr->waiter = NULL;
3030 if (signal_pending(current)) {
3035 if (iter->trace != current_trace)
3039 * We block until we read something and tracing is disabled.
3040 * We still block if tracing is disabled, but we have never
3041 * read anything. This allows a user to cat this file, and
3042 * then enable tracing. But after we have read something,
3043 * we give an EOF when tracing is again disabled.
3045 * iter->pos will be 0 if we haven't read anything.
3047 if (!tracer_enabled && iter->pos)
3053 /* stop when tracing is finished */
3054 if (trace_empty(iter))
3057 if (cnt >= PAGE_SIZE)
3058 cnt = PAGE_SIZE - 1;
3060 /* reset all but tr, trace, and overruns */
3061 memset(&iter->seq, 0,
3062 sizeof(struct trace_iterator) -
3063 offsetof(struct trace_iterator, seq));
3066 while (find_next_entry_inc(iter) != NULL) {
3067 enum print_line_t ret;
3068 int len = iter->seq.len;
3070 ret = print_trace_line(iter);
3071 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3072 /* don't print partial lines */
3073 iter->seq.len = len;
3077 trace_consume(iter);
3079 if (iter->seq.len >= cnt)
3083 /* Now copy what we have to the user */
3084 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3085 if (iter->seq.readpos >= iter->seq.len)
3086 trace_seq_reset(&iter->seq);
3089 * If there was nothing to send to user, inspite of consuming trace
3090 * entries, go back to wait for more entries.
3096 mutex_unlock(&trace_types_lock);
3102 tracing_entries_read(struct file *filp, char __user *ubuf,
3103 size_t cnt, loff_t *ppos)
3105 struct trace_array *tr = filp->private_data;
3109 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3110 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3114 tracing_entries_write(struct file *filp, const char __user *ubuf,
3115 size_t cnt, loff_t *ppos)
3121 if (cnt >= sizeof(buf))
3124 if (copy_from_user(&buf, ubuf, cnt))
3129 ret = strict_strtoul(buf, 10, &val);
3133 /* must have at least 1 entry */
3137 mutex_lock(&trace_types_lock);
3141 /* disable all cpu buffers */
3142 for_each_tracing_cpu(cpu) {
3143 if (global_trace.data[cpu])
3144 atomic_inc(&global_trace.data[cpu]->disabled);
3145 if (max_tr.data[cpu])
3146 atomic_inc(&max_tr.data[cpu]->disabled);
3149 /* value is in KB */
3152 if (val != global_trace.entries) {
3153 ret = ring_buffer_resize(global_trace.buffer, val);
3159 ret = ring_buffer_resize(max_tr.buffer, val);
3163 r = ring_buffer_resize(global_trace.buffer,
3164 global_trace.entries);
3166 /* AARGH! We are left with different
3167 * size max buffer!!!! */
3169 tracing_disabled = 1;
3174 global_trace.entries = val;
3179 /* If check pages failed, return ENOMEM */
3180 if (tracing_disabled)
3183 for_each_tracing_cpu(cpu) {
3184 if (global_trace.data[cpu])
3185 atomic_dec(&global_trace.data[cpu]->disabled);
3186 if (max_tr.data[cpu])
3187 atomic_dec(&max_tr.data[cpu]->disabled);
3191 max_tr.entries = global_trace.entries;
3192 mutex_unlock(&trace_types_lock);
3197 static int mark_printk(const char *fmt, ...)
3201 va_start(args, fmt);
3202 ret = trace_vprintk(0, fmt, args);
3208 tracing_mark_write(struct file *filp, const char __user *ubuf,
3209 size_t cnt, loff_t *fpos)
3214 if (tracing_disabled)
3217 if (cnt > TRACE_BUF_SIZE)
3218 cnt = TRACE_BUF_SIZE;
3220 buf = kmalloc(cnt + 1, GFP_KERNEL);
3224 if (copy_from_user(buf, ubuf, cnt)) {
3229 /* Cut from the first nil or newline. */
3231 end = strchr(buf, '\n');
3235 cnt = mark_printk("%s\n", buf);
3242 static struct file_operations tracing_max_lat_fops = {
3243 .open = tracing_open_generic,
3244 .read = tracing_max_lat_read,
3245 .write = tracing_max_lat_write,
3248 static struct file_operations tracing_ctrl_fops = {
3249 .open = tracing_open_generic,
3250 .read = tracing_ctrl_read,
3251 .write = tracing_ctrl_write,
3254 static struct file_operations set_tracer_fops = {
3255 .open = tracing_open_generic,
3256 .read = tracing_set_trace_read,
3257 .write = tracing_set_trace_write,
3260 static struct file_operations tracing_pipe_fops = {
3261 .open = tracing_open_pipe,
3262 .poll = tracing_poll_pipe,
3263 .read = tracing_read_pipe,
3264 .release = tracing_release_pipe,
3267 static struct file_operations tracing_entries_fops = {
3268 .open = tracing_open_generic,
3269 .read = tracing_entries_read,
3270 .write = tracing_entries_write,
3273 static struct file_operations tracing_mark_fops = {
3274 .open = tracing_open_generic,
3275 .write = tracing_mark_write,
3278 #ifdef CONFIG_DYNAMIC_FTRACE
3280 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3286 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3287 size_t cnt, loff_t *ppos)
3289 static char ftrace_dyn_info_buffer[1024];
3290 static DEFINE_MUTEX(dyn_info_mutex);
3291 unsigned long *p = filp->private_data;
3292 char *buf = ftrace_dyn_info_buffer;
3293 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3296 mutex_lock(&dyn_info_mutex);
3297 r = sprintf(buf, "%ld ", *p);
3299 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3302 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3304 mutex_unlock(&dyn_info_mutex);
3309 static struct file_operations tracing_dyn_info_fops = {
3310 .open = tracing_open_generic,
3311 .read = tracing_read_dyn_info,
3315 static struct dentry *d_tracer;
3317 struct dentry *tracing_init_dentry(void)
3324 d_tracer = debugfs_create_dir("tracing", NULL);
3326 if (!d_tracer && !once) {
3328 pr_warning("Could not create debugfs directory 'tracing'\n");
3335 #ifdef CONFIG_FTRACE_SELFTEST
3336 /* Let selftest have access to static functions in this file */
3337 #include "trace_selftest.c"
3340 static __init int tracer_init_debugfs(void)
3342 struct dentry *d_tracer;
3343 struct dentry *entry;
3345 d_tracer = tracing_init_dentry();
3347 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3348 &global_trace, &tracing_ctrl_fops);
3350 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3352 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3353 NULL, &tracing_iter_fops);
3355 pr_warning("Could not create debugfs 'trace_options' entry\n");
3357 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3358 NULL, &tracing_cpumask_fops);
3360 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3362 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3363 &global_trace, &tracing_lt_fops);
3365 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3367 entry = debugfs_create_file("trace", 0444, d_tracer,
3368 &global_trace, &tracing_fops);
3370 pr_warning("Could not create debugfs 'trace' entry\n");
3372 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3373 &global_trace, &show_traces_fops);
3375 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3377 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3378 &global_trace, &set_tracer_fops);
3380 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3382 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3383 &tracing_max_latency,
3384 &tracing_max_lat_fops);
3386 pr_warning("Could not create debugfs "
3387 "'tracing_max_latency' entry\n");
3389 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3390 &tracing_thresh, &tracing_max_lat_fops);
3392 pr_warning("Could not create debugfs "
3393 "'tracing_thresh' entry\n");
3394 entry = debugfs_create_file("README", 0644, d_tracer,
3395 NULL, &tracing_readme_fops);
3397 pr_warning("Could not create debugfs 'README' entry\n");
3399 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3400 NULL, &tracing_pipe_fops);
3402 pr_warning("Could not create debugfs "
3403 "'trace_pipe' entry\n");
3405 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3406 &global_trace, &tracing_entries_fops);
3408 pr_warning("Could not create debugfs "
3409 "'buffer_size_kb' entry\n");
3411 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3412 NULL, &tracing_mark_fops);
3414 pr_warning("Could not create debugfs "
3415 "'trace_marker' entry\n");
3417 #ifdef CONFIG_DYNAMIC_FTRACE
3418 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3419 &ftrace_update_tot_cnt,
3420 &tracing_dyn_info_fops);
3422 pr_warning("Could not create debugfs "
3423 "'dyn_ftrace_total_info' entry\n");
3425 #ifdef CONFIG_SYSPROF_TRACER
3426 init_tracer_sysprof_debugfs(d_tracer);
3431 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3433 static DEFINE_SPINLOCK(trace_buf_lock);
3434 static char trace_buf[TRACE_BUF_SIZE];
3436 struct ring_buffer_event *event;
3437 struct trace_array *tr = &global_trace;
3438 struct trace_array_cpu *data;
3439 struct print_entry *entry;
3440 unsigned long flags, irq_flags;
3441 int cpu, len = 0, size, pc;
3443 if (tracing_disabled)
3446 pc = preempt_count();
3447 preempt_disable_notrace();
3448 cpu = raw_smp_processor_id();
3449 data = tr->data[cpu];
3451 if (unlikely(atomic_read(&data->disabled)))
3454 spin_lock_irqsave(&trace_buf_lock, flags);
3455 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3457 len = min(len, TRACE_BUF_SIZE-1);
3460 size = sizeof(*entry) + len + 1;
3461 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3464 entry = ring_buffer_event_data(event);
3465 tracing_generic_entry_update(&entry->ent, flags, pc);
3466 entry->ent.type = TRACE_PRINT;
3469 memcpy(&entry->buf, trace_buf, len);
3470 entry->buf[len] = 0;
3471 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3474 spin_unlock_irqrestore(&trace_buf_lock, flags);
3477 preempt_enable_notrace();
3481 EXPORT_SYMBOL_GPL(trace_vprintk);
3483 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3488 if (!(trace_flags & TRACE_ITER_PRINTK))
3492 ret = trace_vprintk(ip, fmt, ap);
3496 EXPORT_SYMBOL_GPL(__ftrace_printk);
3498 static int trace_panic_handler(struct notifier_block *this,
3499 unsigned long event, void *unused)
3501 if (ftrace_dump_on_oops)
3506 static struct notifier_block trace_panic_notifier = {
3507 .notifier_call = trace_panic_handler,
3509 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3512 static int trace_die_handler(struct notifier_block *self,
3518 if (ftrace_dump_on_oops)
3527 static struct notifier_block trace_die_notifier = {
3528 .notifier_call = trace_die_handler,
3533 * printk is set to max of 1024, we really don't need it that big.
3534 * Nothing should be printing 1000 characters anyway.
3536 #define TRACE_MAX_PRINT 1000
3539 * Define here KERN_TRACE so that we have one place to modify
3540 * it if we decide to change what log level the ftrace dump
3543 #define KERN_TRACE KERN_INFO
3546 trace_printk_seq(struct trace_seq *s)
3548 /* Probably should print a warning here. */
3552 /* should be zero ended, but we are paranoid. */
3553 s->buffer[s->len] = 0;
3555 printk(KERN_TRACE "%s", s->buffer);
3560 void ftrace_dump(void)
3562 static DEFINE_SPINLOCK(ftrace_dump_lock);
3563 /* use static because iter can be a bit big for the stack */
3564 static struct trace_iterator iter;
3565 static cpumask_t mask;
3566 static int dump_ran;
3567 unsigned long flags;
3571 spin_lock_irqsave(&ftrace_dump_lock, flags);
3577 /* No turning back! */
3580 for_each_tracing_cpu(cpu) {
3581 atomic_inc(&global_trace.data[cpu]->disabled);
3584 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3586 iter.tr = &global_trace;
3587 iter.trace = current_trace;
3590 * We need to stop all tracing on all CPUS to read the
3591 * the next buffer. This is a bit expensive, but is
3592 * not done often. We fill all what we can read,
3593 * and then release the locks again.
3598 while (!trace_empty(&iter)) {
3601 printk(KERN_TRACE "---------------------------------\n");
3605 /* reset all but tr, trace, and overruns */
3606 memset(&iter.seq, 0,
3607 sizeof(struct trace_iterator) -
3608 offsetof(struct trace_iterator, seq));
3609 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3612 if (find_next_entry_inc(&iter) != NULL) {
3613 print_trace_line(&iter);
3614 trace_consume(&iter);
3617 trace_printk_seq(&iter.seq);
3621 printk(KERN_TRACE " (ftrace buffer empty)\n");
3623 printk(KERN_TRACE "---------------------------------\n");
3626 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3629 __init static int tracer_alloc_buffers(void)
3631 struct trace_array_cpu *data;
3634 /* TODO: make the number of buffers hot pluggable with CPUS */
3635 tracing_buffer_mask = cpu_possible_map;
3637 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3638 TRACE_BUFFER_FLAGS);
3639 if (!global_trace.buffer) {
3640 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3644 global_trace.entries = ring_buffer_size(global_trace.buffer);
3646 #ifdef CONFIG_TRACER_MAX_TRACE
3647 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3648 TRACE_BUFFER_FLAGS);
3649 if (!max_tr.buffer) {
3650 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3652 ring_buffer_free(global_trace.buffer);
3655 max_tr.entries = ring_buffer_size(max_tr.buffer);
3656 WARN_ON(max_tr.entries != global_trace.entries);
3659 /* Allocate the first page for all buffers */
3660 for_each_tracing_cpu(i) {
3661 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3662 max_tr.data[i] = &per_cpu(max_data, i);
3665 trace_init_cmdlines();
3667 register_tracer(&nop_trace);
3668 #ifdef CONFIG_BOOT_TRACER
3669 register_tracer(&boot_tracer);
3670 current_trace = &boot_tracer;
3671 current_trace->init(&global_trace);
3673 current_trace = &nop_trace;
3676 /* All seems OK, enable tracing */
3677 tracing_disabled = 0;
3679 atomic_notifier_chain_register(&panic_notifier_list,
3680 &trace_panic_notifier);
3682 register_die_notifier(&trace_die_notifier);
3686 early_initcall(tracer_alloc_buffers);
3687 fs_initcall(tracer_init_debugfs);