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;
48 * Kill all tracing for good (never come back).
49 * It is initialized to 1 but will turn to zero if the initialization
50 * of the tracer is successful. But that is the only place that sets
53 int tracing_disabled = 1;
55 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
57 static inline void ftrace_disable_cpu(void)
60 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
63 static inline void ftrace_enable_cpu(void)
65 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
69 static cpumask_t __read_mostly tracing_buffer_mask;
71 #define for_each_tracing_cpu(cpu) \
72 for_each_cpu_mask(cpu, tracing_buffer_mask)
75 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
77 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
78 * is set, then ftrace_dump is called. This will output the contents
79 * of the ftrace buffers to the console. This is very useful for
80 * capturing traces that lead to crashes and outputing it to a
83 * It is default off, but you can enable it with either specifying
84 * "ftrace_dump_on_oops" in the kernel command line, or setting
85 * /proc/sys/kernel/ftrace_dump_on_oops to true.
87 int ftrace_dump_on_oops;
89 static int tracing_set_tracer(char *buf);
91 static int __init set_ftrace(char *str)
93 tracing_set_tracer(str);
96 __setup("ftrace", set_ftrace);
98 static int __init set_ftrace_dump_on_oops(char *str)
100 ftrace_dump_on_oops = 1;
103 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
106 ns2usecs(cycle_t nsec)
113 cycle_t ftrace_now(int cpu)
115 u64 ts = ring_buffer_time_stamp(cpu);
116 ring_buffer_normalize_time_stamp(cpu, &ts);
121 * The global_trace is the descriptor that holds the tracing
122 * buffers for the live tracing. For each CPU, it contains
123 * a link list of pages that will store trace entries. The
124 * page descriptor of the pages in the memory is used to hold
125 * the link list by linking the lru item in the page descriptor
126 * to each of the pages in the buffer per CPU.
128 * For each active CPU there is a data field that holds the
129 * pages for the buffer for that CPU. Each CPU has the same number
130 * of pages allocated for its buffer.
132 static struct trace_array global_trace;
134 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
137 * The max_tr is used to snapshot the global_trace when a maximum
138 * latency is reached. Some tracers will use this to store a maximum
139 * trace while it continues examining live traces.
141 * The buffers for the max_tr are set up the same as the global_trace.
142 * When a snapshot is taken, the link list of the max_tr is swapped
143 * with the link list of the global_trace and the buffers are reset for
144 * the global_trace so the tracing can continue.
146 static struct trace_array max_tr;
148 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
150 /* tracer_enabled is used to toggle activation of a tracer */
151 static int tracer_enabled = 1;
153 /* function tracing enabled */
154 int ftrace_function_enabled;
157 * trace_buf_size is the size in bytes that is allocated
158 * for a buffer. Note, the number of bytes is always rounded
161 * This number is purposely set to a low number of 16384.
162 * If the dump on oops happens, it will be much appreciated
163 * to not have to wait for all that output. Anyway this can be
164 * boot time and run time configurable.
166 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
168 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
170 /* trace_types holds a link list of available tracers. */
171 static struct tracer *trace_types __read_mostly;
173 /* current_trace points to the tracer that is currently active */
174 static struct tracer *current_trace __read_mostly;
177 * max_tracer_type_len is used to simplify the allocating of
178 * buffers to read userspace tracer names. We keep track of
179 * the longest tracer name registered.
181 static int max_tracer_type_len;
184 * trace_types_lock is used to protect the trace_types list.
185 * This lock is also used to keep user access serialized.
186 * Accesses from userspace will grab this lock while userspace
187 * activities happen inside the kernel.
189 static DEFINE_MUTEX(trace_types_lock);
191 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
192 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
194 /* trace_flags holds iter_ctrl options */
195 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
198 * trace_wake_up - wake up tasks waiting for trace input
200 * Simply wakes up any task that is blocked on the trace_wait
201 * queue. These is used with trace_poll for tasks polling the trace.
203 void trace_wake_up(void)
206 * The runqueue_is_locked() can fail, but this is the best we
209 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
210 wake_up(&trace_wait);
213 static int __init set_buf_size(char *str)
215 unsigned long buf_size;
220 ret = strict_strtoul(str, 0, &buf_size);
221 /* nr_entries can not be zero */
222 if (ret < 0 || buf_size == 0)
224 trace_buf_size = buf_size;
227 __setup("trace_buf_size=", set_buf_size);
229 unsigned long nsecs_to_usecs(unsigned long nsecs)
235 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
236 * control the output of kernel symbols.
238 #define TRACE_ITER_SYM_MASK \
239 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
241 /* These must match the bit postions in trace_iterator_flags */
242 static const char *trace_options[] = {
259 * ftrace_max_lock is used to protect the swapping of buffers
260 * when taking a max snapshot. The buffers themselves are
261 * protected by per_cpu spinlocks. But the action of the swap
262 * needs its own lock.
264 * This is defined as a raw_spinlock_t in order to help
265 * with performance when lockdep debugging is enabled.
267 static raw_spinlock_t ftrace_max_lock =
268 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
271 * Copy the new maximum trace into the separate maximum-trace
272 * structure. (this way the maximum trace is permanently saved,
273 * for later retrieval via /debugfs/tracing/latency_trace)
276 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
278 struct trace_array_cpu *data = tr->data[cpu];
281 max_tr.time_start = data->preempt_timestamp;
283 data = max_tr.data[cpu];
284 data->saved_latency = tracing_max_latency;
286 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
287 data->pid = tsk->pid;
288 data->uid = tsk->uid;
289 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
290 data->policy = tsk->policy;
291 data->rt_priority = tsk->rt_priority;
293 /* record this tasks comm */
294 tracing_record_cmdline(current);
298 * trace_seq_printf - sequence printing of trace information
299 * @s: trace sequence descriptor
300 * @fmt: printf format string
302 * The tracer may use either sequence operations or its own
303 * copy to user routines. To simplify formating of a trace
304 * trace_seq_printf is used to store strings into a special
305 * buffer (@s). Then the output may be either used by
306 * the sequencer or pulled into another buffer.
309 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
311 int len = (PAGE_SIZE - 1) - s->len;
319 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
322 /* If we can't write it all, don't bother writing anything */
332 * trace_seq_puts - trace sequence printing of simple string
333 * @s: trace sequence descriptor
334 * @str: simple string to record
336 * The tracer may use either the sequence operations or its own
337 * copy to user routines. This function records a simple string
338 * into a special buffer (@s) for later retrieval by a sequencer
339 * or other mechanism.
342 trace_seq_puts(struct trace_seq *s, const char *str)
344 int len = strlen(str);
346 if (len > ((PAGE_SIZE - 1) - s->len))
349 memcpy(s->buffer + s->len, str, len);
356 trace_seq_putc(struct trace_seq *s, unsigned char c)
358 if (s->len >= (PAGE_SIZE - 1))
361 s->buffer[s->len++] = c;
367 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
369 if (len > ((PAGE_SIZE - 1) - s->len))
372 memcpy(s->buffer + s->len, mem, len);
378 #define MAX_MEMHEX_BYTES 8
379 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
382 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
384 unsigned char hex[HEX_CHARS];
385 unsigned char *data = mem;
389 for (i = 0, j = 0; i < len; i++) {
391 for (i = len-1, j = 0; i >= 0; i--) {
393 hex[j++] = hex_asc_hi(data[i]);
394 hex[j++] = hex_asc_lo(data[i]);
398 return trace_seq_putmem(s, hex, j);
402 trace_seq_reset(struct trace_seq *s)
408 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
413 if (s->len <= s->readpos)
416 len = s->len - s->readpos;
419 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
428 trace_print_seq(struct seq_file *m, struct trace_seq *s)
430 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
433 seq_puts(m, s->buffer);
439 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
441 * @tsk: the task with the latency
442 * @cpu: The cpu that initiated the trace.
444 * Flip the buffers between the @tr and the max_tr and record information
445 * about which task was the cause of this latency.
448 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
450 struct ring_buffer *buf = tr->buffer;
452 WARN_ON_ONCE(!irqs_disabled());
453 __raw_spin_lock(&ftrace_max_lock);
455 tr->buffer = max_tr.buffer;
458 ftrace_disable_cpu();
459 ring_buffer_reset(tr->buffer);
462 __update_max_tr(tr, tsk, cpu);
463 __raw_spin_unlock(&ftrace_max_lock);
467 * update_max_tr_single - only copy one trace over, and reset the rest
469 * @tsk - task with the latency
470 * @cpu - the cpu of the buffer to copy.
472 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
475 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
479 WARN_ON_ONCE(!irqs_disabled());
480 __raw_spin_lock(&ftrace_max_lock);
482 ftrace_disable_cpu();
484 ring_buffer_reset(max_tr.buffer);
485 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
491 __update_max_tr(tr, tsk, cpu);
492 __raw_spin_unlock(&ftrace_max_lock);
496 * register_tracer - register a tracer with the ftrace system.
497 * @type - the plugin for the tracer
499 * Register a new plugin tracer.
501 int register_tracer(struct tracer *type)
508 pr_info("Tracer must have a name\n");
512 mutex_lock(&trace_types_lock);
513 for (t = trace_types; t; t = t->next) {
514 if (strcmp(type->name, t->name) == 0) {
516 pr_info("Trace %s already registered\n",
523 #ifdef CONFIG_FTRACE_STARTUP_TEST
524 if (type->selftest) {
525 struct tracer *saved_tracer = current_trace;
526 struct trace_array *tr = &global_trace;
527 int saved_ctrl = tr->ctrl;
530 * Run a selftest on this tracer.
531 * Here we reset the trace buffer, and set the current
532 * tracer to be this tracer. The tracer can then run some
533 * internal tracing to verify that everything is in order.
534 * If we fail, we do not register this tracer.
536 for_each_tracing_cpu(i) {
537 tracing_reset(tr, i);
539 current_trace = type;
541 /* the test is responsible for initializing and enabling */
542 pr_info("Testing tracer %s: ", type->name);
543 ret = type->selftest(type, tr);
544 /* the test is responsible for resetting too */
545 current_trace = saved_tracer;
546 tr->ctrl = saved_ctrl;
548 printk(KERN_CONT "FAILED!\n");
551 /* Only reset on passing, to avoid touching corrupted buffers */
552 for_each_tracing_cpu(i) {
553 tracing_reset(tr, i);
555 printk(KERN_CONT "PASSED\n");
559 type->next = trace_types;
561 len = strlen(type->name);
562 if (len > max_tracer_type_len)
563 max_tracer_type_len = len;
566 mutex_unlock(&trace_types_lock);
571 void unregister_tracer(struct tracer *type)
576 mutex_lock(&trace_types_lock);
577 for (t = &trace_types; *t; t = &(*t)->next) {
581 pr_info("Trace %s not registered\n", type->name);
586 if (strlen(type->name) != max_tracer_type_len)
589 max_tracer_type_len = 0;
590 for (t = &trace_types; *t; t = &(*t)->next) {
591 len = strlen((*t)->name);
592 if (len > max_tracer_type_len)
593 max_tracer_type_len = len;
596 mutex_unlock(&trace_types_lock);
599 void tracing_reset(struct trace_array *tr, int cpu)
601 ftrace_disable_cpu();
602 ring_buffer_reset_cpu(tr->buffer, cpu);
606 #define SAVED_CMDLINES 128
607 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
608 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
609 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
610 static int cmdline_idx;
611 static DEFINE_SPINLOCK(trace_cmdline_lock);
613 /* temporary disable recording */
614 atomic_t trace_record_cmdline_disabled __read_mostly;
616 static void trace_init_cmdlines(void)
618 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
619 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
623 static int trace_stop_count;
624 static DEFINE_SPINLOCK(tracing_start_lock);
627 * tracing_start - quick start of the tracer
629 * If tracing is enabled but was stopped by tracing_stop,
630 * this will start the tracer back up.
632 void tracing_start(void)
634 struct ring_buffer *buffer;
637 if (tracing_disabled)
640 spin_lock_irqsave(&tracing_start_lock, flags);
641 if (--trace_stop_count)
644 if (trace_stop_count < 0) {
645 /* Someone screwed up their debugging */
647 trace_stop_count = 0;
652 buffer = global_trace.buffer;
654 ring_buffer_record_enable(buffer);
656 buffer = max_tr.buffer;
658 ring_buffer_record_enable(buffer);
662 spin_unlock_irqrestore(&tracing_start_lock, flags);
666 * tracing_stop - quick stop of the tracer
668 * Light weight way to stop tracing. Use in conjunction with
671 void tracing_stop(void)
673 struct ring_buffer *buffer;
677 spin_lock_irqsave(&tracing_start_lock, flags);
678 if (trace_stop_count++)
681 buffer = global_trace.buffer;
683 ring_buffer_record_disable(buffer);
685 buffer = max_tr.buffer;
687 ring_buffer_record_disable(buffer);
690 spin_unlock_irqrestore(&tracing_start_lock, flags);
693 void trace_stop_cmdline_recording(void);
695 static void trace_save_cmdline(struct task_struct *tsk)
700 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
704 * It's not the end of the world if we don't get
705 * the lock, but we also don't want to spin
706 * nor do we want to disable interrupts,
707 * so if we miss here, then better luck next time.
709 if (!spin_trylock(&trace_cmdline_lock))
712 idx = map_pid_to_cmdline[tsk->pid];
713 if (idx >= SAVED_CMDLINES) {
714 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
716 map = map_cmdline_to_pid[idx];
717 if (map <= PID_MAX_DEFAULT)
718 map_pid_to_cmdline[map] = (unsigned)-1;
720 map_pid_to_cmdline[tsk->pid] = idx;
725 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
727 spin_unlock(&trace_cmdline_lock);
730 static char *trace_find_cmdline(int pid)
732 char *cmdline = "<...>";
738 if (pid > PID_MAX_DEFAULT)
741 map = map_pid_to_cmdline[pid];
742 if (map >= SAVED_CMDLINES)
745 cmdline = saved_cmdlines[map];
751 void tracing_record_cmdline(struct task_struct *tsk)
753 if (atomic_read(&trace_record_cmdline_disabled))
756 trace_save_cmdline(tsk);
760 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
763 struct task_struct *tsk = current;
765 entry->preempt_count = pc & 0xff;
766 entry->pid = (tsk) ? tsk->pid : 0;
768 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
769 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
771 TRACE_FLAG_IRQS_NOSUPPORT |
773 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
774 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
775 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
779 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
780 unsigned long ip, unsigned long parent_ip, unsigned long flags,
783 struct ring_buffer_event *event;
784 struct ftrace_entry *entry;
785 unsigned long irq_flags;
787 /* If we are reading the ring buffer, don't trace */
788 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
791 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
795 entry = ring_buffer_event_data(event);
796 tracing_generic_entry_update(&entry->ent, flags, pc);
797 entry->ent.type = TRACE_FN;
799 entry->parent_ip = parent_ip;
800 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
804 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
805 unsigned long ip, unsigned long parent_ip, unsigned long flags,
808 if (likely(!atomic_read(&data->disabled)))
809 trace_function(tr, data, ip, parent_ip, flags, pc);
812 static void ftrace_trace_stack(struct trace_array *tr,
813 struct trace_array_cpu *data,
817 #ifdef CONFIG_STACKTRACE
818 struct ring_buffer_event *event;
819 struct stack_entry *entry;
820 struct stack_trace trace;
821 unsigned long irq_flags;
823 if (!(trace_flags & TRACE_ITER_STACKTRACE))
826 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
830 entry = ring_buffer_event_data(event);
831 tracing_generic_entry_update(&entry->ent, flags, pc);
832 entry->ent.type = TRACE_STACK;
834 memset(&entry->caller, 0, sizeof(entry->caller));
836 trace.nr_entries = 0;
837 trace.max_entries = FTRACE_STACK_ENTRIES;
839 trace.entries = entry->caller;
841 save_stack_trace(&trace);
842 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
846 void __trace_stack(struct trace_array *tr,
847 struct trace_array_cpu *data,
851 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
855 ftrace_trace_special(void *__tr, void *__data,
856 unsigned long arg1, unsigned long arg2, unsigned long arg3,
859 struct ring_buffer_event *event;
860 struct trace_array_cpu *data = __data;
861 struct trace_array *tr = __tr;
862 struct special_entry *entry;
863 unsigned long irq_flags;
865 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
869 entry = ring_buffer_event_data(event);
870 tracing_generic_entry_update(&entry->ent, 0, pc);
871 entry->ent.type = TRACE_SPECIAL;
875 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
876 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
882 __trace_special(void *__tr, void *__data,
883 unsigned long arg1, unsigned long arg2, unsigned long arg3)
885 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
889 tracing_sched_switch_trace(struct trace_array *tr,
890 struct trace_array_cpu *data,
891 struct task_struct *prev,
892 struct task_struct *next,
893 unsigned long flags, int pc)
895 struct ring_buffer_event *event;
896 struct ctx_switch_entry *entry;
897 unsigned long irq_flags;
899 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
903 entry = ring_buffer_event_data(event);
904 tracing_generic_entry_update(&entry->ent, flags, pc);
905 entry->ent.type = TRACE_CTX;
906 entry->prev_pid = prev->pid;
907 entry->prev_prio = prev->prio;
908 entry->prev_state = prev->state;
909 entry->next_pid = next->pid;
910 entry->next_prio = next->prio;
911 entry->next_state = next->state;
912 entry->next_cpu = task_cpu(next);
913 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
914 ftrace_trace_stack(tr, data, flags, 5, pc);
918 tracing_sched_wakeup_trace(struct trace_array *tr,
919 struct trace_array_cpu *data,
920 struct task_struct *wakee,
921 struct task_struct *curr,
922 unsigned long flags, int pc)
924 struct ring_buffer_event *event;
925 struct ctx_switch_entry *entry;
926 unsigned long irq_flags;
928 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
932 entry = ring_buffer_event_data(event);
933 tracing_generic_entry_update(&entry->ent, flags, pc);
934 entry->ent.type = TRACE_WAKE;
935 entry->prev_pid = curr->pid;
936 entry->prev_prio = curr->prio;
937 entry->prev_state = curr->state;
938 entry->next_pid = wakee->pid;
939 entry->next_prio = wakee->prio;
940 entry->next_state = wakee->state;
941 entry->next_cpu = task_cpu(wakee);
942 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
943 ftrace_trace_stack(tr, data, flags, 6, pc);
949 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
951 struct trace_array *tr = &global_trace;
952 struct trace_array_cpu *data;
956 if (tracing_disabled || !tr->ctrl)
959 pc = preempt_count();
960 preempt_disable_notrace();
961 cpu = raw_smp_processor_id();
962 data = tr->data[cpu];
964 if (likely(!atomic_read(&data->disabled)))
965 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
967 preempt_enable_notrace();
970 #ifdef CONFIG_FUNCTION_TRACER
972 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
974 struct trace_array *tr = &global_trace;
975 struct trace_array_cpu *data;
981 if (unlikely(!ftrace_function_enabled))
984 pc = preempt_count();
985 resched = ftrace_preempt_disable();
986 local_save_flags(flags);
987 cpu = raw_smp_processor_id();
988 data = tr->data[cpu];
989 disabled = atomic_inc_return(&data->disabled);
991 if (likely(disabled == 1))
992 trace_function(tr, data, ip, parent_ip, flags, pc);
994 atomic_dec(&data->disabled);
995 ftrace_preempt_enable(resched);
999 function_trace_call(unsigned long ip, unsigned long parent_ip)
1001 struct trace_array *tr = &global_trace;
1002 struct trace_array_cpu *data;
1003 unsigned long flags;
1008 if (unlikely(!ftrace_function_enabled))
1012 * Need to use raw, since this must be called before the
1013 * recursive protection is performed.
1015 raw_local_irq_save(flags);
1016 cpu = raw_smp_processor_id();
1017 data = tr->data[cpu];
1018 disabled = atomic_inc_return(&data->disabled);
1020 if (likely(disabled == 1)) {
1021 pc = preempt_count();
1022 trace_function(tr, data, ip, parent_ip, flags, pc);
1025 atomic_dec(&data->disabled);
1026 raw_local_irq_restore(flags);
1029 static struct ftrace_ops trace_ops __read_mostly =
1031 .func = function_trace_call,
1034 void tracing_start_function_trace(void)
1036 ftrace_function_enabled = 0;
1038 if (trace_flags & TRACE_ITER_PREEMPTONLY)
1039 trace_ops.func = function_trace_call_preempt_only;
1041 trace_ops.func = function_trace_call;
1043 register_ftrace_function(&trace_ops);
1045 ftrace_function_enabled = 1;
1048 void tracing_stop_function_trace(void)
1050 ftrace_function_enabled = 0;
1051 unregister_ftrace_function(&trace_ops);
1055 enum trace_file_type {
1056 TRACE_FILE_LAT_FMT = 1,
1059 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1061 /* Don't allow ftrace to trace into the ring buffers */
1062 ftrace_disable_cpu();
1065 if (iter->buffer_iter[iter->cpu])
1066 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1068 ftrace_enable_cpu();
1071 static struct trace_entry *
1072 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1074 struct ring_buffer_event *event;
1075 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1077 /* Don't allow ftrace to trace into the ring buffers */
1078 ftrace_disable_cpu();
1081 event = ring_buffer_iter_peek(buf_iter, ts);
1083 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1085 ftrace_enable_cpu();
1087 return event ? ring_buffer_event_data(event) : NULL;
1090 static struct trace_entry *
1091 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1093 struct ring_buffer *buffer = iter->tr->buffer;
1094 struct trace_entry *ent, *next = NULL;
1095 u64 next_ts = 0, ts;
1099 for_each_tracing_cpu(cpu) {
1101 if (ring_buffer_empty_cpu(buffer, cpu))
1104 ent = peek_next_entry(iter, cpu, &ts);
1107 * Pick the entry with the smallest timestamp:
1109 if (ent && (!next || ts < next_ts)) {
1117 *ent_cpu = next_cpu;
1125 /* Find the next real entry, without updating the iterator itself */
1126 static struct trace_entry *
1127 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1129 return __find_next_entry(iter, ent_cpu, ent_ts);
1132 /* Find the next real entry, and increment the iterator to the next entry */
1133 static void *find_next_entry_inc(struct trace_iterator *iter)
1135 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1138 trace_iterator_increment(iter, iter->cpu);
1140 return iter->ent ? iter : NULL;
1143 static void trace_consume(struct trace_iterator *iter)
1145 /* Don't allow ftrace to trace into the ring buffers */
1146 ftrace_disable_cpu();
1147 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1148 ftrace_enable_cpu();
1151 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1153 struct trace_iterator *iter = m->private;
1159 /* can't go backwards */
1164 ent = find_next_entry_inc(iter);
1168 while (ent && iter->idx < i)
1169 ent = find_next_entry_inc(iter);
1176 static void *s_start(struct seq_file *m, loff_t *pos)
1178 struct trace_iterator *iter = m->private;
1183 mutex_lock(&trace_types_lock);
1185 if (!current_trace || current_trace != iter->trace) {
1186 mutex_unlock(&trace_types_lock);
1190 atomic_inc(&trace_record_cmdline_disabled);
1192 /* let the tracer grab locks here if needed */
1193 if (current_trace->start)
1194 current_trace->start(iter);
1196 if (*pos != iter->pos) {
1201 ftrace_disable_cpu();
1203 for_each_tracing_cpu(cpu) {
1204 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1207 ftrace_enable_cpu();
1209 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1214 p = s_next(m, p, &l);
1220 static void s_stop(struct seq_file *m, void *p)
1222 struct trace_iterator *iter = m->private;
1224 atomic_dec(&trace_record_cmdline_disabled);
1226 /* let the tracer release locks here if needed */
1227 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1228 iter->trace->stop(iter);
1230 mutex_unlock(&trace_types_lock);
1233 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1235 #ifdef CONFIG_KRETPROBES
1236 static inline int kretprobed(unsigned long addr)
1238 return addr == (unsigned long)kretprobe_trampoline;
1241 static inline int kretprobed(unsigned long addr)
1245 #endif /* CONFIG_KRETPROBES */
1248 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1250 #ifdef CONFIG_KALLSYMS
1251 char str[KSYM_SYMBOL_LEN];
1253 kallsyms_lookup(address, NULL, NULL, NULL, str);
1255 return trace_seq_printf(s, fmt, str);
1261 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1262 unsigned long address)
1264 #ifdef CONFIG_KALLSYMS
1265 char str[KSYM_SYMBOL_LEN];
1267 sprint_symbol(str, address);
1268 return trace_seq_printf(s, fmt, str);
1273 #ifndef CONFIG_64BIT
1274 # define IP_FMT "%08lx"
1276 # define IP_FMT "%016lx"
1280 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1285 return trace_seq_printf(s, "0");
1287 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1288 ret = seq_print_sym_offset(s, "%s", ip);
1290 ret = seq_print_sym_short(s, "%s", ip);
1295 if (sym_flags & TRACE_ITER_SYM_ADDR)
1296 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1300 static void print_lat_help_header(struct seq_file *m)
1302 seq_puts(m, "# _------=> CPU# \n");
1303 seq_puts(m, "# / _-----=> irqs-off \n");
1304 seq_puts(m, "# | / _----=> need-resched \n");
1305 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1306 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1307 seq_puts(m, "# |||| / \n");
1308 seq_puts(m, "# ||||| delay \n");
1309 seq_puts(m, "# cmd pid ||||| time | caller \n");
1310 seq_puts(m, "# \\ / ||||| \\ | / \n");
1313 static void print_func_help_header(struct seq_file *m)
1315 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1316 seq_puts(m, "# | | | | |\n");
1321 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1323 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1324 struct trace_array *tr = iter->tr;
1325 struct trace_array_cpu *data = tr->data[tr->cpu];
1326 struct tracer *type = current_trace;
1327 unsigned long total;
1328 unsigned long entries;
1329 const char *name = "preemption";
1334 entries = ring_buffer_entries(iter->tr->buffer);
1336 ring_buffer_overruns(iter->tr->buffer);
1338 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1340 seq_puts(m, "-----------------------------------"
1341 "---------------------------------\n");
1342 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1343 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1344 nsecs_to_usecs(data->saved_latency),
1348 #if defined(CONFIG_PREEMPT_NONE)
1350 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1352 #elif defined(CONFIG_PREEMPT)
1357 /* These are reserved for later use */
1360 seq_printf(m, " #P:%d)\n", num_online_cpus());
1364 seq_puts(m, " -----------------\n");
1365 seq_printf(m, " | task: %.16s-%d "
1366 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1367 data->comm, data->pid, data->uid, data->nice,
1368 data->policy, data->rt_priority);
1369 seq_puts(m, " -----------------\n");
1371 if (data->critical_start) {
1372 seq_puts(m, " => started at: ");
1373 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1374 trace_print_seq(m, &iter->seq);
1375 seq_puts(m, "\n => ended at: ");
1376 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1377 trace_print_seq(m, &iter->seq);
1385 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1387 int hardirq, softirq;
1390 comm = trace_find_cmdline(entry->pid);
1392 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1393 trace_seq_printf(s, "%3d", cpu);
1394 trace_seq_printf(s, "%c%c",
1395 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1396 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1397 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1399 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1400 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1401 if (hardirq && softirq) {
1402 trace_seq_putc(s, 'H');
1405 trace_seq_putc(s, 'h');
1408 trace_seq_putc(s, 's');
1410 trace_seq_putc(s, '.');
1414 if (entry->preempt_count)
1415 trace_seq_printf(s, "%x", entry->preempt_count);
1417 trace_seq_puts(s, ".");
1420 unsigned long preempt_mark_thresh = 100;
1423 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1424 unsigned long rel_usecs)
1426 trace_seq_printf(s, " %4lldus", abs_usecs);
1427 if (rel_usecs > preempt_mark_thresh)
1428 trace_seq_puts(s, "!: ");
1429 else if (rel_usecs > 1)
1430 trace_seq_puts(s, "+: ");
1432 trace_seq_puts(s, " : ");
1435 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1438 * The message is supposed to contain an ending newline.
1439 * If the printing stops prematurely, try to add a newline of our own.
1441 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1443 struct trace_entry *ent;
1444 struct trace_field_cont *cont;
1447 ent = peek_next_entry(iter, iter->cpu, NULL);
1448 if (!ent || ent->type != TRACE_CONT) {
1449 trace_seq_putc(s, '\n');
1454 cont = (struct trace_field_cont *)ent;
1456 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1458 ftrace_disable_cpu();
1460 if (iter->buffer_iter[iter->cpu])
1461 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1463 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1465 ftrace_enable_cpu();
1467 ent = peek_next_entry(iter, iter->cpu, NULL);
1468 } while (ent && ent->type == TRACE_CONT);
1471 trace_seq_putc(s, '\n');
1474 static enum print_line_t
1475 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1477 struct trace_seq *s = &iter->seq;
1478 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1479 struct trace_entry *next_entry;
1480 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1481 struct trace_entry *entry = iter->ent;
1482 unsigned long abs_usecs;
1483 unsigned long rel_usecs;
1490 if (entry->type == TRACE_CONT)
1491 return TRACE_TYPE_HANDLED;
1493 next_entry = find_next_entry(iter, NULL, &next_ts);
1496 rel_usecs = ns2usecs(next_ts - iter->ts);
1497 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1500 comm = trace_find_cmdline(entry->pid);
1501 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1502 " %ld.%03ldms (+%ld.%03ldms): ",
1504 entry->pid, cpu, entry->flags,
1505 entry->preempt_count, trace_idx,
1508 abs_usecs % 1000, rel_usecs/1000,
1511 lat_print_generic(s, entry, cpu);
1512 lat_print_timestamp(s, abs_usecs, rel_usecs);
1514 switch (entry->type) {
1516 struct ftrace_entry *field;
1518 trace_assign_type(field, entry);
1520 seq_print_ip_sym(s, field->ip, sym_flags);
1521 trace_seq_puts(s, " (");
1522 if (kretprobed(field->parent_ip))
1523 trace_seq_puts(s, KRETPROBE_MSG);
1525 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1526 trace_seq_puts(s, ")\n");
1531 struct ctx_switch_entry *field;
1533 trace_assign_type(field, entry);
1535 T = field->next_state < sizeof(state_to_char) ?
1536 state_to_char[field->next_state] : 'X';
1538 state = field->prev_state ?
1539 __ffs(field->prev_state) + 1 : 0;
1540 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1541 comm = trace_find_cmdline(field->next_pid);
1542 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1545 S, entry->type == TRACE_CTX ? "==>" : " +",
1552 case TRACE_SPECIAL: {
1553 struct special_entry *field;
1555 trace_assign_type(field, entry);
1557 trace_seq_printf(s, "# %ld %ld %ld\n",
1564 struct stack_entry *field;
1566 trace_assign_type(field, entry);
1568 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1570 trace_seq_puts(s, " <= ");
1571 seq_print_ip_sym(s, field->caller[i], sym_flags);
1573 trace_seq_puts(s, "\n");
1577 struct print_entry *field;
1579 trace_assign_type(field, entry);
1581 seq_print_ip_sym(s, field->ip, sym_flags);
1582 trace_seq_printf(s, ": %s", field->buf);
1583 if (entry->flags & TRACE_FLAG_CONT)
1584 trace_seq_print_cont(s, iter);
1588 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1590 return TRACE_TYPE_HANDLED;
1593 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1595 struct trace_seq *s = &iter->seq;
1596 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1597 struct trace_entry *entry;
1598 unsigned long usec_rem;
1599 unsigned long long t;
1608 if (entry->type == TRACE_CONT)
1609 return TRACE_TYPE_HANDLED;
1611 comm = trace_find_cmdline(iter->ent->pid);
1613 t = ns2usecs(iter->ts);
1614 usec_rem = do_div(t, 1000000ULL);
1615 secs = (unsigned long)t;
1617 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1619 return TRACE_TYPE_PARTIAL_LINE;
1620 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1622 return TRACE_TYPE_PARTIAL_LINE;
1623 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1625 return TRACE_TYPE_PARTIAL_LINE;
1627 switch (entry->type) {
1629 struct ftrace_entry *field;
1631 trace_assign_type(field, entry);
1633 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1635 return TRACE_TYPE_PARTIAL_LINE;
1636 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1638 ret = trace_seq_printf(s, " <-");
1640 return TRACE_TYPE_PARTIAL_LINE;
1641 if (kretprobed(field->parent_ip))
1642 ret = trace_seq_puts(s, KRETPROBE_MSG);
1644 ret = seq_print_ip_sym(s,
1648 return TRACE_TYPE_PARTIAL_LINE;
1650 ret = trace_seq_printf(s, "\n");
1652 return TRACE_TYPE_PARTIAL_LINE;
1657 struct ctx_switch_entry *field;
1659 trace_assign_type(field, entry);
1661 S = field->prev_state < sizeof(state_to_char) ?
1662 state_to_char[field->prev_state] : 'X';
1663 T = field->next_state < sizeof(state_to_char) ?
1664 state_to_char[field->next_state] : 'X';
1665 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1669 entry->type == TRACE_CTX ? "==>" : " +",
1675 return TRACE_TYPE_PARTIAL_LINE;
1678 case TRACE_SPECIAL: {
1679 struct special_entry *field;
1681 trace_assign_type(field, entry);
1683 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1688 return TRACE_TYPE_PARTIAL_LINE;
1692 struct stack_entry *field;
1694 trace_assign_type(field, entry);
1696 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1698 ret = trace_seq_puts(s, " <= ");
1700 return TRACE_TYPE_PARTIAL_LINE;
1702 ret = seq_print_ip_sym(s, field->caller[i],
1705 return TRACE_TYPE_PARTIAL_LINE;
1707 ret = trace_seq_puts(s, "\n");
1709 return TRACE_TYPE_PARTIAL_LINE;
1713 struct print_entry *field;
1715 trace_assign_type(field, entry);
1717 seq_print_ip_sym(s, field->ip, sym_flags);
1718 trace_seq_printf(s, ": %s", field->buf);
1719 if (entry->flags & TRACE_FLAG_CONT)
1720 trace_seq_print_cont(s, iter);
1724 return TRACE_TYPE_HANDLED;
1727 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1729 struct trace_seq *s = &iter->seq;
1730 struct trace_entry *entry;
1736 if (entry->type == TRACE_CONT)
1737 return TRACE_TYPE_HANDLED;
1739 ret = trace_seq_printf(s, "%d %d %llu ",
1740 entry->pid, iter->cpu, iter->ts);
1742 return TRACE_TYPE_PARTIAL_LINE;
1744 switch (entry->type) {
1746 struct ftrace_entry *field;
1748 trace_assign_type(field, entry);
1750 ret = trace_seq_printf(s, "%x %x\n",
1754 return TRACE_TYPE_PARTIAL_LINE;
1759 struct ctx_switch_entry *field;
1761 trace_assign_type(field, entry);
1763 S = field->prev_state < sizeof(state_to_char) ?
1764 state_to_char[field->prev_state] : 'X';
1765 T = field->next_state < sizeof(state_to_char) ?
1766 state_to_char[field->next_state] : 'X';
1767 if (entry->type == TRACE_WAKE)
1769 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
1778 return TRACE_TYPE_PARTIAL_LINE;
1783 struct special_entry *field;
1785 trace_assign_type(field, entry);
1787 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1792 return TRACE_TYPE_PARTIAL_LINE;
1796 struct print_entry *field;
1798 trace_assign_type(field, entry);
1800 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
1801 if (entry->flags & TRACE_FLAG_CONT)
1802 trace_seq_print_cont(s, iter);
1806 return TRACE_TYPE_HANDLED;
1809 #define SEQ_PUT_FIELD_RET(s, x) \
1811 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1815 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1817 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
1818 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1822 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1824 struct trace_seq *s = &iter->seq;
1825 unsigned char newline = '\n';
1826 struct trace_entry *entry;
1831 if (entry->type == TRACE_CONT)
1832 return TRACE_TYPE_HANDLED;
1834 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1835 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1836 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1838 switch (entry->type) {
1840 struct ftrace_entry *field;
1842 trace_assign_type(field, entry);
1844 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1845 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
1850 struct ctx_switch_entry *field;
1852 trace_assign_type(field, entry);
1854 S = field->prev_state < sizeof(state_to_char) ?
1855 state_to_char[field->prev_state] : 'X';
1856 T = field->next_state < sizeof(state_to_char) ?
1857 state_to_char[field->next_state] : 'X';
1858 if (entry->type == TRACE_WAKE)
1860 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1861 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
1862 SEQ_PUT_HEX_FIELD_RET(s, S);
1863 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1864 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1865 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
1866 SEQ_PUT_HEX_FIELD_RET(s, T);
1871 struct special_entry *field;
1873 trace_assign_type(field, entry);
1875 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
1876 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
1877 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
1881 SEQ_PUT_FIELD_RET(s, newline);
1883 return TRACE_TYPE_HANDLED;
1886 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1888 struct trace_seq *s = &iter->seq;
1889 struct trace_entry *entry;
1893 if (entry->type == TRACE_CONT)
1894 return TRACE_TYPE_HANDLED;
1896 SEQ_PUT_FIELD_RET(s, entry->pid);
1897 SEQ_PUT_FIELD_RET(s, iter->cpu);
1898 SEQ_PUT_FIELD_RET(s, iter->ts);
1900 switch (entry->type) {
1902 struct ftrace_entry *field;
1904 trace_assign_type(field, entry);
1906 SEQ_PUT_FIELD_RET(s, field->ip);
1907 SEQ_PUT_FIELD_RET(s, field->parent_ip);
1911 struct ctx_switch_entry *field;
1913 trace_assign_type(field, entry);
1915 SEQ_PUT_FIELD_RET(s, field->prev_pid);
1916 SEQ_PUT_FIELD_RET(s, field->prev_prio);
1917 SEQ_PUT_FIELD_RET(s, field->prev_state);
1918 SEQ_PUT_FIELD_RET(s, field->next_pid);
1919 SEQ_PUT_FIELD_RET(s, field->next_prio);
1920 SEQ_PUT_FIELD_RET(s, field->next_state);
1925 struct special_entry *field;
1927 trace_assign_type(field, entry);
1929 SEQ_PUT_FIELD_RET(s, field->arg1);
1930 SEQ_PUT_FIELD_RET(s, field->arg2);
1931 SEQ_PUT_FIELD_RET(s, field->arg3);
1938 static int trace_empty(struct trace_iterator *iter)
1942 for_each_tracing_cpu(cpu) {
1943 if (iter->buffer_iter[cpu]) {
1944 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1947 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1955 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1957 enum print_line_t ret;
1959 if (iter->trace && iter->trace->print_line) {
1960 ret = iter->trace->print_line(iter);
1961 if (ret != TRACE_TYPE_UNHANDLED)
1965 if (trace_flags & TRACE_ITER_BIN)
1966 return print_bin_fmt(iter);
1968 if (trace_flags & TRACE_ITER_HEX)
1969 return print_hex_fmt(iter);
1971 if (trace_flags & TRACE_ITER_RAW)
1972 return print_raw_fmt(iter);
1974 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1975 return print_lat_fmt(iter, iter->idx, iter->cpu);
1977 return print_trace_fmt(iter);
1980 static int s_show(struct seq_file *m, void *v)
1982 struct trace_iterator *iter = v;
1984 if (iter->ent == NULL) {
1986 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1989 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1990 /* print nothing if the buffers are empty */
1991 if (trace_empty(iter))
1993 print_trace_header(m, iter);
1994 if (!(trace_flags & TRACE_ITER_VERBOSE))
1995 print_lat_help_header(m);
1997 if (!(trace_flags & TRACE_ITER_VERBOSE))
1998 print_func_help_header(m);
2001 print_trace_line(iter);
2002 trace_print_seq(m, &iter->seq);
2008 static struct seq_operations tracer_seq_ops = {
2015 static struct trace_iterator *
2016 __tracing_open(struct inode *inode, struct file *file, int *ret)
2018 struct trace_iterator *iter;
2022 if (tracing_disabled) {
2027 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2033 mutex_lock(&trace_types_lock);
2034 if (current_trace && current_trace->print_max)
2037 iter->tr = inode->i_private;
2038 iter->trace = current_trace;
2041 for_each_tracing_cpu(cpu) {
2043 iter->buffer_iter[cpu] =
2044 ring_buffer_read_start(iter->tr->buffer, cpu);
2046 if (!iter->buffer_iter[cpu])
2050 /* TODO stop tracer */
2051 *ret = seq_open(file, &tracer_seq_ops);
2055 m = file->private_data;
2058 /* stop the trace while dumping */
2059 if (iter->tr->ctrl) {
2061 ftrace_function_enabled = 0;
2064 if (iter->trace && iter->trace->open)
2065 iter->trace->open(iter);
2067 mutex_unlock(&trace_types_lock);
2073 for_each_tracing_cpu(cpu) {
2074 if (iter->buffer_iter[cpu])
2075 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2077 mutex_unlock(&trace_types_lock);
2079 return ERR_PTR(-ENOMEM);
2082 int tracing_open_generic(struct inode *inode, struct file *filp)
2084 if (tracing_disabled)
2087 filp->private_data = inode->i_private;
2091 int tracing_release(struct inode *inode, struct file *file)
2093 struct seq_file *m = (struct seq_file *)file->private_data;
2094 struct trace_iterator *iter = m->private;
2097 mutex_lock(&trace_types_lock);
2098 for_each_tracing_cpu(cpu) {
2099 if (iter->buffer_iter[cpu])
2100 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2103 if (iter->trace && iter->trace->close)
2104 iter->trace->close(iter);
2106 /* reenable tracing if it was previously enabled */
2107 if (iter->tr->ctrl) {
2110 * It is safe to enable function tracing even if it
2113 ftrace_function_enabled = 1;
2115 mutex_unlock(&trace_types_lock);
2117 seq_release(inode, file);
2122 static int tracing_open(struct inode *inode, struct file *file)
2126 __tracing_open(inode, file, &ret);
2131 static int tracing_lt_open(struct inode *inode, struct file *file)
2133 struct trace_iterator *iter;
2136 iter = __tracing_open(inode, file, &ret);
2139 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2146 t_next(struct seq_file *m, void *v, loff_t *pos)
2148 struct tracer *t = m->private;
2160 static void *t_start(struct seq_file *m, loff_t *pos)
2162 struct tracer *t = m->private;
2165 mutex_lock(&trace_types_lock);
2166 for (; t && l < *pos; t = t_next(m, t, &l))
2172 static void t_stop(struct seq_file *m, void *p)
2174 mutex_unlock(&trace_types_lock);
2177 static int t_show(struct seq_file *m, void *v)
2179 struct tracer *t = v;
2184 seq_printf(m, "%s", t->name);
2193 static struct seq_operations show_traces_seq_ops = {
2200 static int show_traces_open(struct inode *inode, struct file *file)
2204 if (tracing_disabled)
2207 ret = seq_open(file, &show_traces_seq_ops);
2209 struct seq_file *m = file->private_data;
2210 m->private = trace_types;
2216 static struct file_operations tracing_fops = {
2217 .open = tracing_open,
2219 .llseek = seq_lseek,
2220 .release = tracing_release,
2223 static struct file_operations tracing_lt_fops = {
2224 .open = tracing_lt_open,
2226 .llseek = seq_lseek,
2227 .release = tracing_release,
2230 static struct file_operations show_traces_fops = {
2231 .open = show_traces_open,
2233 .release = seq_release,
2237 * Only trace on a CPU if the bitmask is set:
2239 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2242 * When tracing/tracing_cpu_mask is modified then this holds
2243 * the new bitmask we are about to install:
2245 static cpumask_t tracing_cpumask_new;
2248 * The tracer itself will not take this lock, but still we want
2249 * to provide a consistent cpumask to user-space:
2251 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2254 * Temporary storage for the character representation of the
2255 * CPU bitmask (and one more byte for the newline):
2257 static char mask_str[NR_CPUS + 1];
2260 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2261 size_t count, loff_t *ppos)
2265 mutex_lock(&tracing_cpumask_update_lock);
2267 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2268 if (count - len < 2) {
2272 len += sprintf(mask_str + len, "\n");
2273 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2276 mutex_unlock(&tracing_cpumask_update_lock);
2282 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2283 size_t count, loff_t *ppos)
2287 mutex_lock(&tracing_cpumask_update_lock);
2288 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2292 raw_local_irq_disable();
2293 __raw_spin_lock(&ftrace_max_lock);
2294 for_each_tracing_cpu(cpu) {
2296 * Increase/decrease the disabled counter if we are
2297 * about to flip a bit in the cpumask:
2299 if (cpu_isset(cpu, tracing_cpumask) &&
2300 !cpu_isset(cpu, tracing_cpumask_new)) {
2301 atomic_inc(&global_trace.data[cpu]->disabled);
2303 if (!cpu_isset(cpu, tracing_cpumask) &&
2304 cpu_isset(cpu, tracing_cpumask_new)) {
2305 atomic_dec(&global_trace.data[cpu]->disabled);
2308 __raw_spin_unlock(&ftrace_max_lock);
2309 raw_local_irq_enable();
2311 tracing_cpumask = tracing_cpumask_new;
2313 mutex_unlock(&tracing_cpumask_update_lock);
2318 mutex_unlock(&tracing_cpumask_update_lock);
2323 static struct file_operations tracing_cpumask_fops = {
2324 .open = tracing_open_generic,
2325 .read = tracing_cpumask_read,
2326 .write = tracing_cpumask_write,
2330 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2331 size_t cnt, loff_t *ppos)
2338 /* calulate max size */
2339 for (i = 0; trace_options[i]; i++) {
2340 len += strlen(trace_options[i]);
2341 len += 3; /* "no" and space */
2344 /* +2 for \n and \0 */
2345 buf = kmalloc(len + 2, GFP_KERNEL);
2349 for (i = 0; trace_options[i]; i++) {
2350 if (trace_flags & (1 << i))
2351 r += sprintf(buf + r, "%s ", trace_options[i]);
2353 r += sprintf(buf + r, "no%s ", trace_options[i]);
2356 r += sprintf(buf + r, "\n");
2357 WARN_ON(r >= len + 2);
2359 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2367 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2368 size_t cnt, loff_t *ppos)
2375 if (cnt >= sizeof(buf))
2378 if (copy_from_user(&buf, ubuf, cnt))
2383 if (strncmp(buf, "no", 2) == 0) {
2388 for (i = 0; trace_options[i]; i++) {
2389 int len = strlen(trace_options[i]);
2391 if (strncmp(cmp, trace_options[i], len) == 0) {
2393 trace_flags &= ~(1 << i);
2395 trace_flags |= (1 << i);
2400 * If no option could be set, return an error:
2402 if (!trace_options[i])
2410 static struct file_operations tracing_iter_fops = {
2411 .open = tracing_open_generic,
2412 .read = tracing_iter_ctrl_read,
2413 .write = tracing_iter_ctrl_write,
2416 static const char readme_msg[] =
2417 "tracing mini-HOWTO:\n\n"
2419 "# mount -t debugfs nodev /debug\n\n"
2420 "# cat /debug/tracing/available_tracers\n"
2421 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2422 "# cat /debug/tracing/current_tracer\n"
2424 "# echo sched_switch > /debug/tracing/current_tracer\n"
2425 "# cat /debug/tracing/current_tracer\n"
2427 "# cat /debug/tracing/iter_ctrl\n"
2428 "noprint-parent nosym-offset nosym-addr noverbose\n"
2429 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2430 "# echo 1 > /debug/tracing/tracing_enabled\n"
2431 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2432 "echo 0 > /debug/tracing/tracing_enabled\n"
2436 tracing_readme_read(struct file *filp, char __user *ubuf,
2437 size_t cnt, loff_t *ppos)
2439 return simple_read_from_buffer(ubuf, cnt, ppos,
2440 readme_msg, strlen(readme_msg));
2443 static struct file_operations tracing_readme_fops = {
2444 .open = tracing_open_generic,
2445 .read = tracing_readme_read,
2449 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2450 size_t cnt, loff_t *ppos)
2452 struct trace_array *tr = filp->private_data;
2456 r = sprintf(buf, "%ld\n", tr->ctrl);
2457 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2461 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2462 size_t cnt, loff_t *ppos)
2464 struct trace_array *tr = filp->private_data;
2469 if (cnt >= sizeof(buf))
2472 if (copy_from_user(&buf, ubuf, cnt))
2477 ret = strict_strtoul(buf, 10, &val);
2483 mutex_lock(&trace_types_lock);
2484 if (tr->ctrl ^ val) {
2492 if (current_trace && current_trace->ctrl_update)
2493 current_trace->ctrl_update(tr);
2495 mutex_unlock(&trace_types_lock);
2503 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2504 size_t cnt, loff_t *ppos)
2506 char buf[max_tracer_type_len+2];
2509 mutex_lock(&trace_types_lock);
2511 r = sprintf(buf, "%s\n", current_trace->name);
2513 r = sprintf(buf, "\n");
2514 mutex_unlock(&trace_types_lock);
2516 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2519 static int tracing_set_tracer(char *buf)
2521 struct trace_array *tr = &global_trace;
2525 mutex_lock(&trace_types_lock);
2526 for (t = trace_types; t; t = t->next) {
2527 if (strcmp(t->name, buf) == 0)
2534 if (t == current_trace)
2537 if (current_trace && current_trace->reset)
2538 current_trace->reset(tr);
2545 mutex_unlock(&trace_types_lock);
2551 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2552 size_t cnt, loff_t *ppos)
2554 char buf[max_tracer_type_len+1];
2558 if (cnt > max_tracer_type_len)
2559 cnt = max_tracer_type_len;
2561 if (copy_from_user(&buf, ubuf, cnt))
2566 /* strip ending whitespace. */
2567 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2570 ret = tracing_set_tracer(buf);
2581 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2582 size_t cnt, loff_t *ppos)
2584 unsigned long *ptr = filp->private_data;
2588 r = snprintf(buf, sizeof(buf), "%ld\n",
2589 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2590 if (r > sizeof(buf))
2592 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2596 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2597 size_t cnt, loff_t *ppos)
2599 long *ptr = filp->private_data;
2604 if (cnt >= sizeof(buf))
2607 if (copy_from_user(&buf, ubuf, cnt))
2612 ret = strict_strtoul(buf, 10, &val);
2621 static atomic_t tracing_reader;
2623 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2625 struct trace_iterator *iter;
2627 if (tracing_disabled)
2630 /* We only allow for reader of the pipe */
2631 if (atomic_inc_return(&tracing_reader) != 1) {
2632 atomic_dec(&tracing_reader);
2636 /* create a buffer to store the information to pass to userspace */
2637 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2641 mutex_lock(&trace_types_lock);
2642 iter->tr = &global_trace;
2643 iter->trace = current_trace;
2644 filp->private_data = iter;
2646 if (iter->trace->pipe_open)
2647 iter->trace->pipe_open(iter);
2648 mutex_unlock(&trace_types_lock);
2653 static int tracing_release_pipe(struct inode *inode, struct file *file)
2655 struct trace_iterator *iter = file->private_data;
2658 atomic_dec(&tracing_reader);
2664 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2666 struct trace_iterator *iter = filp->private_data;
2668 if (trace_flags & TRACE_ITER_BLOCK) {
2670 * Always select as readable when in blocking mode
2672 return POLLIN | POLLRDNORM;
2674 if (!trace_empty(iter))
2675 return POLLIN | POLLRDNORM;
2676 poll_wait(filp, &trace_wait, poll_table);
2677 if (!trace_empty(iter))
2678 return POLLIN | POLLRDNORM;
2688 tracing_read_pipe(struct file *filp, char __user *ubuf,
2689 size_t cnt, loff_t *ppos)
2691 struct trace_iterator *iter = filp->private_data;
2694 /* return any leftover data */
2695 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2699 trace_seq_reset(&iter->seq);
2701 mutex_lock(&trace_types_lock);
2702 if (iter->trace->read) {
2703 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2710 while (trace_empty(iter)) {
2712 if ((filp->f_flags & O_NONBLOCK)) {
2718 * This is a make-shift waitqueue. The reason we don't use
2719 * an actual wait queue is because:
2720 * 1) we only ever have one waiter
2721 * 2) the tracing, traces all functions, we don't want
2722 * the overhead of calling wake_up and friends
2723 * (and tracing them too)
2724 * Anyway, this is really very primitive wakeup.
2726 set_current_state(TASK_INTERRUPTIBLE);
2727 iter->tr->waiter = current;
2729 mutex_unlock(&trace_types_lock);
2731 /* sleep for 100 msecs, and try again. */
2732 schedule_timeout(HZ/10);
2734 mutex_lock(&trace_types_lock);
2736 iter->tr->waiter = NULL;
2738 if (signal_pending(current)) {
2743 if (iter->trace != current_trace)
2747 * We block until we read something and tracing is disabled.
2748 * We still block if tracing is disabled, but we have never
2749 * read anything. This allows a user to cat this file, and
2750 * then enable tracing. But after we have read something,
2751 * we give an EOF when tracing is again disabled.
2753 * iter->pos will be 0 if we haven't read anything.
2755 if (!tracer_enabled && iter->pos)
2761 /* stop when tracing is finished */
2762 if (trace_empty(iter))
2765 if (cnt >= PAGE_SIZE)
2766 cnt = PAGE_SIZE - 1;
2768 /* reset all but tr, trace, and overruns */
2769 memset(&iter->seq, 0,
2770 sizeof(struct trace_iterator) -
2771 offsetof(struct trace_iterator, seq));
2774 while (find_next_entry_inc(iter) != NULL) {
2775 enum print_line_t ret;
2776 int len = iter->seq.len;
2778 ret = print_trace_line(iter);
2779 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2780 /* don't print partial lines */
2781 iter->seq.len = len;
2785 trace_consume(iter);
2787 if (iter->seq.len >= cnt)
2791 /* Now copy what we have to the user */
2792 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2793 if (iter->seq.readpos >= iter->seq.len)
2794 trace_seq_reset(&iter->seq);
2797 * If there was nothing to send to user, inspite of consuming trace
2798 * entries, go back to wait for more entries.
2804 mutex_unlock(&trace_types_lock);
2810 tracing_entries_read(struct file *filp, char __user *ubuf,
2811 size_t cnt, loff_t *ppos)
2813 struct trace_array *tr = filp->private_data;
2817 r = sprintf(buf, "%lu\n", tr->entries);
2818 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2822 tracing_entries_write(struct file *filp, const char __user *ubuf,
2823 size_t cnt, loff_t *ppos)
2828 struct trace_array *tr = filp->private_data;
2830 if (cnt >= sizeof(buf))
2833 if (copy_from_user(&buf, ubuf, cnt))
2838 ret = strict_strtoul(buf, 10, &val);
2842 /* must have at least 1 entry */
2846 mutex_lock(&trace_types_lock);
2850 pr_info("ftrace: please disable tracing"
2851 " before modifying buffer size\n");
2855 if (val != global_trace.entries) {
2856 ret = ring_buffer_resize(global_trace.buffer, val);
2862 ret = ring_buffer_resize(max_tr.buffer, val);
2866 r = ring_buffer_resize(global_trace.buffer,
2867 global_trace.entries);
2869 /* AARGH! We are left with different
2870 * size max buffer!!!! */
2872 tracing_disabled = 1;
2877 global_trace.entries = val;
2882 /* If check pages failed, return ENOMEM */
2883 if (tracing_disabled)
2886 max_tr.entries = global_trace.entries;
2887 mutex_unlock(&trace_types_lock);
2892 static int mark_printk(const char *fmt, ...)
2896 va_start(args, fmt);
2897 ret = trace_vprintk(0, fmt, args);
2903 tracing_mark_write(struct file *filp, const char __user *ubuf,
2904 size_t cnt, loff_t *fpos)
2908 struct trace_array *tr = &global_trace;
2910 if (!tr->ctrl || tracing_disabled)
2913 if (cnt > TRACE_BUF_SIZE)
2914 cnt = TRACE_BUF_SIZE;
2916 buf = kmalloc(cnt + 1, GFP_KERNEL);
2920 if (copy_from_user(buf, ubuf, cnt)) {
2925 /* Cut from the first nil or newline. */
2927 end = strchr(buf, '\n');
2931 cnt = mark_printk("%s\n", buf);
2938 static struct file_operations tracing_max_lat_fops = {
2939 .open = tracing_open_generic,
2940 .read = tracing_max_lat_read,
2941 .write = tracing_max_lat_write,
2944 static struct file_operations tracing_ctrl_fops = {
2945 .open = tracing_open_generic,
2946 .read = tracing_ctrl_read,
2947 .write = tracing_ctrl_write,
2950 static struct file_operations set_tracer_fops = {
2951 .open = tracing_open_generic,
2952 .read = tracing_set_trace_read,
2953 .write = tracing_set_trace_write,
2956 static struct file_operations tracing_pipe_fops = {
2957 .open = tracing_open_pipe,
2958 .poll = tracing_poll_pipe,
2959 .read = tracing_read_pipe,
2960 .release = tracing_release_pipe,
2963 static struct file_operations tracing_entries_fops = {
2964 .open = tracing_open_generic,
2965 .read = tracing_entries_read,
2966 .write = tracing_entries_write,
2969 static struct file_operations tracing_mark_fops = {
2970 .open = tracing_open_generic,
2971 .write = tracing_mark_write,
2974 #ifdef CONFIG_DYNAMIC_FTRACE
2976 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
2982 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
2983 size_t cnt, loff_t *ppos)
2985 static char ftrace_dyn_info_buffer[1024];
2986 static DEFINE_MUTEX(dyn_info_mutex);
2987 unsigned long *p = filp->private_data;
2988 char *buf = ftrace_dyn_info_buffer;
2989 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
2992 mutex_lock(&dyn_info_mutex);
2993 r = sprintf(buf, "%ld ", *p);
2995 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
2998 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3000 mutex_unlock(&dyn_info_mutex);
3005 static struct file_operations tracing_dyn_info_fops = {
3006 .open = tracing_open_generic,
3007 .read = tracing_read_dyn_info,
3011 static struct dentry *d_tracer;
3013 struct dentry *tracing_init_dentry(void)
3020 d_tracer = debugfs_create_dir("tracing", NULL);
3022 if (!d_tracer && !once) {
3024 pr_warning("Could not create debugfs directory 'tracing'\n");
3031 #ifdef CONFIG_FTRACE_SELFTEST
3032 /* Let selftest have access to static functions in this file */
3033 #include "trace_selftest.c"
3036 static __init int tracer_init_debugfs(void)
3038 struct dentry *d_tracer;
3039 struct dentry *entry;
3041 d_tracer = tracing_init_dentry();
3043 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3044 &global_trace, &tracing_ctrl_fops);
3046 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3048 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
3049 NULL, &tracing_iter_fops);
3051 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
3053 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3054 NULL, &tracing_cpumask_fops);
3056 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3058 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3059 &global_trace, &tracing_lt_fops);
3061 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3063 entry = debugfs_create_file("trace", 0444, d_tracer,
3064 &global_trace, &tracing_fops);
3066 pr_warning("Could not create debugfs 'trace' entry\n");
3068 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3069 &global_trace, &show_traces_fops);
3071 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3073 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3074 &global_trace, &set_tracer_fops);
3076 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3078 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3079 &tracing_max_latency,
3080 &tracing_max_lat_fops);
3082 pr_warning("Could not create debugfs "
3083 "'tracing_max_latency' entry\n");
3085 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3086 &tracing_thresh, &tracing_max_lat_fops);
3088 pr_warning("Could not create debugfs "
3089 "'tracing_thresh' entry\n");
3090 entry = debugfs_create_file("README", 0644, d_tracer,
3091 NULL, &tracing_readme_fops);
3093 pr_warning("Could not create debugfs 'README' entry\n");
3095 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3096 NULL, &tracing_pipe_fops);
3098 pr_warning("Could not create debugfs "
3099 "'trace_pipe' entry\n");
3101 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
3102 &global_trace, &tracing_entries_fops);
3104 pr_warning("Could not create debugfs "
3105 "'trace_entries' entry\n");
3107 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3108 NULL, &tracing_mark_fops);
3110 pr_warning("Could not create debugfs "
3111 "'trace_marker' entry\n");
3113 #ifdef CONFIG_DYNAMIC_FTRACE
3114 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3115 &ftrace_update_tot_cnt,
3116 &tracing_dyn_info_fops);
3118 pr_warning("Could not create debugfs "
3119 "'dyn_ftrace_total_info' entry\n");
3121 #ifdef CONFIG_SYSPROF_TRACER
3122 init_tracer_sysprof_debugfs(d_tracer);
3127 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3129 static DEFINE_SPINLOCK(trace_buf_lock);
3130 static char trace_buf[TRACE_BUF_SIZE];
3132 struct ring_buffer_event *event;
3133 struct trace_array *tr = &global_trace;
3134 struct trace_array_cpu *data;
3135 struct print_entry *entry;
3136 unsigned long flags, irq_flags;
3137 int cpu, len = 0, size, pc;
3139 if (!tr->ctrl || tracing_disabled)
3142 pc = preempt_count();
3143 preempt_disable_notrace();
3144 cpu = raw_smp_processor_id();
3145 data = tr->data[cpu];
3147 if (unlikely(atomic_read(&data->disabled)))
3150 spin_lock_irqsave(&trace_buf_lock, flags);
3151 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3153 len = min(len, TRACE_BUF_SIZE-1);
3156 size = sizeof(*entry) + len + 1;
3157 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3160 entry = ring_buffer_event_data(event);
3161 tracing_generic_entry_update(&entry->ent, flags, pc);
3162 entry->ent.type = TRACE_PRINT;
3165 memcpy(&entry->buf, trace_buf, len);
3166 entry->buf[len] = 0;
3167 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3170 spin_unlock_irqrestore(&trace_buf_lock, flags);
3173 preempt_enable_notrace();
3177 EXPORT_SYMBOL_GPL(trace_vprintk);
3179 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3184 if (!(trace_flags & TRACE_ITER_PRINTK))
3188 ret = trace_vprintk(ip, fmt, ap);
3192 EXPORT_SYMBOL_GPL(__ftrace_printk);
3194 static int trace_panic_handler(struct notifier_block *this,
3195 unsigned long event, void *unused)
3197 if (ftrace_dump_on_oops)
3202 static struct notifier_block trace_panic_notifier = {
3203 .notifier_call = trace_panic_handler,
3205 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3208 static int trace_die_handler(struct notifier_block *self,
3214 if (ftrace_dump_on_oops)
3223 static struct notifier_block trace_die_notifier = {
3224 .notifier_call = trace_die_handler,
3229 * printk is set to max of 1024, we really don't need it that big.
3230 * Nothing should be printing 1000 characters anyway.
3232 #define TRACE_MAX_PRINT 1000
3235 * Define here KERN_TRACE so that we have one place to modify
3236 * it if we decide to change what log level the ftrace dump
3239 #define KERN_TRACE KERN_INFO
3242 trace_printk_seq(struct trace_seq *s)
3244 /* Probably should print a warning here. */
3248 /* should be zero ended, but we are paranoid. */
3249 s->buffer[s->len] = 0;
3251 printk(KERN_TRACE "%s", s->buffer);
3256 void ftrace_dump(void)
3258 static DEFINE_SPINLOCK(ftrace_dump_lock);
3259 /* use static because iter can be a bit big for the stack */
3260 static struct trace_iterator iter;
3261 static cpumask_t mask;
3262 static int dump_ran;
3263 unsigned long flags;
3267 spin_lock_irqsave(&ftrace_dump_lock, flags);
3273 /* No turning back! */
3276 for_each_tracing_cpu(cpu) {
3277 atomic_inc(&global_trace.data[cpu]->disabled);
3280 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3282 iter.tr = &global_trace;
3283 iter.trace = current_trace;
3286 * We need to stop all tracing on all CPUS to read the
3287 * the next buffer. This is a bit expensive, but is
3288 * not done often. We fill all what we can read,
3289 * and then release the locks again.
3294 while (!trace_empty(&iter)) {
3297 printk(KERN_TRACE "---------------------------------\n");
3301 /* reset all but tr, trace, and overruns */
3302 memset(&iter.seq, 0,
3303 sizeof(struct trace_iterator) -
3304 offsetof(struct trace_iterator, seq));
3305 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3308 if (find_next_entry_inc(&iter) != NULL) {
3309 print_trace_line(&iter);
3310 trace_consume(&iter);
3313 trace_printk_seq(&iter.seq);
3317 printk(KERN_TRACE " (ftrace buffer empty)\n");
3319 printk(KERN_TRACE "---------------------------------\n");
3322 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3325 __init static int tracer_alloc_buffers(void)
3327 struct trace_array_cpu *data;
3330 /* TODO: make the number of buffers hot pluggable with CPUS */
3331 tracing_buffer_mask = cpu_possible_map;
3333 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3334 TRACE_BUFFER_FLAGS);
3335 if (!global_trace.buffer) {
3336 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3340 global_trace.entries = ring_buffer_size(global_trace.buffer);
3342 #ifdef CONFIG_TRACER_MAX_TRACE
3343 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3344 TRACE_BUFFER_FLAGS);
3345 if (!max_tr.buffer) {
3346 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3348 ring_buffer_free(global_trace.buffer);
3351 max_tr.entries = ring_buffer_size(max_tr.buffer);
3352 WARN_ON(max_tr.entries != global_trace.entries);
3355 /* Allocate the first page for all buffers */
3356 for_each_tracing_cpu(i) {
3357 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3358 max_tr.data[i] = &per_cpu(max_data, i);
3361 trace_init_cmdlines();
3363 register_tracer(&nop_trace);
3364 #ifdef CONFIG_BOOT_TRACER
3365 /* We don't want to launch sched_switch tracer yet */
3366 global_trace.ctrl = 0;
3367 register_tracer(&boot_tracer);
3368 current_trace = &boot_tracer;
3369 current_trace->init(&global_trace);
3371 current_trace = &nop_trace;
3374 /* All seems OK, enable tracing */
3375 global_trace.ctrl = tracer_enabled;
3376 tracing_disabled = 0;
3378 atomic_notifier_chain_register(&panic_notifier_list,
3379 &trace_panic_notifier);
3381 register_die_notifier(&trace_die_notifier);
3385 early_initcall(tracer_alloc_buffers);
3386 fs_initcall(tracer_init_debugfs);