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 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
48 static inline void ftrace_disable_cpu(void)
51 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
54 static inline void ftrace_enable_cpu(void)
56 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
60 static cpumask_t __read_mostly tracing_buffer_mask;
62 #define for_each_tracing_cpu(cpu) \
63 for_each_cpu_mask(cpu, tracing_buffer_mask)
65 static int tracing_disabled = 1;
68 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
70 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
71 * is set, then ftrace_dump is called. This will output the contents
72 * of the ftrace buffers to the console. This is very useful for
73 * capturing traces that lead to crashes and outputing it to a
76 * It is default off, but you can enable it with either specifying
77 * "ftrace_dump_on_oops" in the kernel command line, or setting
78 * /proc/sys/kernel/ftrace_dump_on_oops to true.
80 int ftrace_dump_on_oops;
82 static int __init set_ftrace_dump_on_oops(char *str)
84 ftrace_dump_on_oops = 1;
87 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
90 ns2usecs(cycle_t nsec)
97 cycle_t ftrace_now(int cpu)
99 u64 ts = ring_buffer_time_stamp(cpu);
100 ring_buffer_normalize_time_stamp(cpu, &ts);
105 * The global_trace is the descriptor that holds the tracing
106 * buffers for the live tracing. For each CPU, it contains
107 * a link list of pages that will store trace entries. The
108 * page descriptor of the pages in the memory is used to hold
109 * the link list by linking the lru item in the page descriptor
110 * to each of the pages in the buffer per CPU.
112 * For each active CPU there is a data field that holds the
113 * pages for the buffer for that CPU. Each CPU has the same number
114 * of pages allocated for its buffer.
116 static struct trace_array global_trace;
118 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
121 * The max_tr is used to snapshot the global_trace when a maximum
122 * latency is reached. Some tracers will use this to store a maximum
123 * trace while it continues examining live traces.
125 * The buffers for the max_tr are set up the same as the global_trace.
126 * When a snapshot is taken, the link list of the max_tr is swapped
127 * with the link list of the global_trace and the buffers are reset for
128 * the global_trace so the tracing can continue.
130 static struct trace_array max_tr;
132 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
134 /* tracer_enabled is used to toggle activation of a tracer */
135 static int tracer_enabled = 1;
137 /* function tracing enabled */
138 int ftrace_function_enabled;
141 * trace_buf_size is the size in bytes that is allocated
142 * for a buffer. Note, the number of bytes is always rounded
145 * This number is purposely set to a low number of 16384.
146 * If the dump on oops happens, it will be much appreciated
147 * to not have to wait for all that output. Anyway this can be
148 * boot time and run time configurable.
150 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
152 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
154 /* trace_types holds a link list of available tracers. */
155 static struct tracer *trace_types __read_mostly;
157 /* current_trace points to the tracer that is currently active */
158 static struct tracer *current_trace __read_mostly;
161 * max_tracer_type_len is used to simplify the allocating of
162 * buffers to read userspace tracer names. We keep track of
163 * the longest tracer name registered.
165 static int max_tracer_type_len;
168 * trace_types_lock is used to protect the trace_types list.
169 * This lock is also used to keep user access serialized.
170 * Accesses from userspace will grab this lock while userspace
171 * activities happen inside the kernel.
173 static DEFINE_MUTEX(trace_types_lock);
175 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
176 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
178 /* trace_flags holds iter_ctrl options */
179 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
182 * trace_wake_up - wake up tasks waiting for trace input
184 * Simply wakes up any task that is blocked on the trace_wait
185 * queue. These is used with trace_poll for tasks polling the trace.
187 void trace_wake_up(void)
190 * The runqueue_is_locked() can fail, but this is the best we
193 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
194 wake_up(&trace_wait);
197 static int __init set_buf_size(char *str)
199 unsigned long buf_size;
204 ret = strict_strtoul(str, 0, &buf_size);
205 /* nr_entries can not be zero */
206 if (ret < 0 || buf_size == 0)
208 trace_buf_size = buf_size;
211 __setup("trace_buf_size=", set_buf_size);
213 unsigned long nsecs_to_usecs(unsigned long nsecs)
219 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
220 * control the output of kernel symbols.
222 #define TRACE_ITER_SYM_MASK \
223 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
225 /* These must match the bit postions in trace_iterator_flags */
226 static const char *trace_options[] = {
242 * ftrace_max_lock is used to protect the swapping of buffers
243 * when taking a max snapshot. The buffers themselves are
244 * protected by per_cpu spinlocks. But the action of the swap
245 * needs its own lock.
247 * This is defined as a raw_spinlock_t in order to help
248 * with performance when lockdep debugging is enabled.
250 static raw_spinlock_t ftrace_max_lock =
251 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
254 * Copy the new maximum trace into the separate maximum-trace
255 * structure. (this way the maximum trace is permanently saved,
256 * for later retrieval via /debugfs/tracing/latency_trace)
259 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
261 struct trace_array_cpu *data = tr->data[cpu];
264 max_tr.time_start = data->preempt_timestamp;
266 data = max_tr.data[cpu];
267 data->saved_latency = tracing_max_latency;
269 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
270 data->pid = tsk->pid;
271 data->uid = tsk->uid;
272 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
273 data->policy = tsk->policy;
274 data->rt_priority = tsk->rt_priority;
276 /* record this tasks comm */
277 tracing_record_cmdline(current);
281 * trace_seq_printf - sequence printing of trace information
282 * @s: trace sequence descriptor
283 * @fmt: printf format string
285 * The tracer may use either sequence operations or its own
286 * copy to user routines. To simplify formating of a trace
287 * trace_seq_printf is used to store strings into a special
288 * buffer (@s). Then the output may be either used by
289 * the sequencer or pulled into another buffer.
292 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
294 int len = (PAGE_SIZE - 1) - s->len;
302 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
305 /* If we can't write it all, don't bother writing anything */
315 * trace_seq_puts - trace sequence printing of simple string
316 * @s: trace sequence descriptor
317 * @str: simple string to record
319 * The tracer may use either the sequence operations or its own
320 * copy to user routines. This function records a simple string
321 * into a special buffer (@s) for later retrieval by a sequencer
322 * or other mechanism.
325 trace_seq_puts(struct trace_seq *s, const char *str)
327 int len = strlen(str);
329 if (len > ((PAGE_SIZE - 1) - s->len))
332 memcpy(s->buffer + s->len, str, len);
339 trace_seq_putc(struct trace_seq *s, unsigned char c)
341 if (s->len >= (PAGE_SIZE - 1))
344 s->buffer[s->len++] = c;
350 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
352 if (len > ((PAGE_SIZE - 1) - s->len))
355 memcpy(s->buffer + s->len, mem, len);
361 #define MAX_MEMHEX_BYTES 8
362 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
365 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
367 unsigned char hex[HEX_CHARS];
368 unsigned char *data = mem;
372 for (i = 0, j = 0; i < len; i++) {
374 for (i = len-1, j = 0; i >= 0; i--) {
376 hex[j++] = hex_asc_hi(data[i]);
377 hex[j++] = hex_asc_lo(data[i]);
381 return trace_seq_putmem(s, hex, j);
385 trace_seq_reset(struct trace_seq *s)
391 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
396 if (s->len <= s->readpos)
399 len = s->len - s->readpos;
402 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
411 trace_print_seq(struct seq_file *m, struct trace_seq *s)
413 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
416 seq_puts(m, s->buffer);
422 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
424 * @tsk: the task with the latency
425 * @cpu: The cpu that initiated the trace.
427 * Flip the buffers between the @tr and the max_tr and record information
428 * about which task was the cause of this latency.
431 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
433 struct ring_buffer *buf = tr->buffer;
435 WARN_ON_ONCE(!irqs_disabled());
436 __raw_spin_lock(&ftrace_max_lock);
438 tr->buffer = max_tr.buffer;
441 ftrace_disable_cpu();
442 ring_buffer_reset(tr->buffer);
445 __update_max_tr(tr, tsk, cpu);
446 __raw_spin_unlock(&ftrace_max_lock);
450 * update_max_tr_single - only copy one trace over, and reset the rest
452 * @tsk - task with the latency
453 * @cpu - the cpu of the buffer to copy.
455 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
458 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
462 WARN_ON_ONCE(!irqs_disabled());
463 __raw_spin_lock(&ftrace_max_lock);
465 ftrace_disable_cpu();
467 ring_buffer_reset(max_tr.buffer);
468 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
474 __update_max_tr(tr, tsk, cpu);
475 __raw_spin_unlock(&ftrace_max_lock);
479 * register_tracer - register a tracer with the ftrace system.
480 * @type - the plugin for the tracer
482 * Register a new plugin tracer.
484 int register_tracer(struct tracer *type)
491 pr_info("Tracer must have a name\n");
495 mutex_lock(&trace_types_lock);
496 for (t = trace_types; t; t = t->next) {
497 if (strcmp(type->name, t->name) == 0) {
499 pr_info("Trace %s already registered\n",
506 #ifdef CONFIG_FTRACE_STARTUP_TEST
507 if (type->selftest) {
508 struct tracer *saved_tracer = current_trace;
509 struct trace_array *tr = &global_trace;
510 int saved_ctrl = tr->ctrl;
513 * Run a selftest on this tracer.
514 * Here we reset the trace buffer, and set the current
515 * tracer to be this tracer. The tracer can then run some
516 * internal tracing to verify that everything is in order.
517 * If we fail, we do not register this tracer.
519 for_each_tracing_cpu(i) {
520 tracing_reset(tr, i);
522 current_trace = type;
524 /* the test is responsible for initializing and enabling */
525 pr_info("Testing tracer %s: ", type->name);
526 ret = type->selftest(type, tr);
527 /* the test is responsible for resetting too */
528 current_trace = saved_tracer;
529 tr->ctrl = saved_ctrl;
531 printk(KERN_CONT "FAILED!\n");
534 /* Only reset on passing, to avoid touching corrupted buffers */
535 for_each_tracing_cpu(i) {
536 tracing_reset(tr, i);
538 printk(KERN_CONT "PASSED\n");
542 type->next = trace_types;
544 len = strlen(type->name);
545 if (len > max_tracer_type_len)
546 max_tracer_type_len = len;
549 mutex_unlock(&trace_types_lock);
554 void unregister_tracer(struct tracer *type)
559 mutex_lock(&trace_types_lock);
560 for (t = &trace_types; *t; t = &(*t)->next) {
564 pr_info("Trace %s not registered\n", type->name);
569 if (strlen(type->name) != max_tracer_type_len)
572 max_tracer_type_len = 0;
573 for (t = &trace_types; *t; t = &(*t)->next) {
574 len = strlen((*t)->name);
575 if (len > max_tracer_type_len)
576 max_tracer_type_len = len;
579 mutex_unlock(&trace_types_lock);
582 void tracing_reset(struct trace_array *tr, int cpu)
584 ftrace_disable_cpu();
585 ring_buffer_reset_cpu(tr->buffer, cpu);
589 #define SAVED_CMDLINES 128
590 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
591 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
592 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
593 static int cmdline_idx;
594 static DEFINE_SPINLOCK(trace_cmdline_lock);
596 /* temporary disable recording */
597 atomic_t trace_record_cmdline_disabled __read_mostly;
599 static void trace_init_cmdlines(void)
601 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
602 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
606 void trace_stop_cmdline_recording(void);
608 static void trace_save_cmdline(struct task_struct *tsk)
613 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
617 * It's not the end of the world if we don't get
618 * the lock, but we also don't want to spin
619 * nor do we want to disable interrupts,
620 * so if we miss here, then better luck next time.
622 if (!spin_trylock(&trace_cmdline_lock))
625 idx = map_pid_to_cmdline[tsk->pid];
626 if (idx >= SAVED_CMDLINES) {
627 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
629 map = map_cmdline_to_pid[idx];
630 if (map <= PID_MAX_DEFAULT)
631 map_pid_to_cmdline[map] = (unsigned)-1;
633 map_pid_to_cmdline[tsk->pid] = idx;
638 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
640 spin_unlock(&trace_cmdline_lock);
643 static char *trace_find_cmdline(int pid)
645 char *cmdline = "<...>";
651 if (pid > PID_MAX_DEFAULT)
654 map = map_pid_to_cmdline[pid];
655 if (map >= SAVED_CMDLINES)
658 cmdline = saved_cmdlines[map];
664 void tracing_record_cmdline(struct task_struct *tsk)
666 if (atomic_read(&trace_record_cmdline_disabled))
669 trace_save_cmdline(tsk);
673 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
676 struct task_struct *tsk = current;
678 entry->preempt_count = pc & 0xff;
679 entry->pid = (tsk) ? tsk->pid : 0;
681 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
682 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
683 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
684 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
688 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
689 unsigned long ip, unsigned long parent_ip, unsigned long flags,
692 struct ring_buffer_event *event;
693 struct ftrace_entry *entry;
694 unsigned long irq_flags;
696 /* If we are reading the ring buffer, don't trace */
697 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
700 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
704 entry = ring_buffer_event_data(event);
705 tracing_generic_entry_update(&entry->ent, flags, pc);
706 entry->ent.type = TRACE_FN;
708 entry->parent_ip = parent_ip;
709 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
713 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
714 unsigned long ip, unsigned long parent_ip, unsigned long flags,
717 if (likely(!atomic_read(&data->disabled)))
718 trace_function(tr, data, ip, parent_ip, flags, pc);
721 static void ftrace_trace_stack(struct trace_array *tr,
722 struct trace_array_cpu *data,
726 struct ring_buffer_event *event;
727 struct stack_entry *entry;
728 struct stack_trace trace;
729 unsigned long irq_flags;
731 if (!(trace_flags & TRACE_ITER_STACKTRACE))
734 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
738 entry = ring_buffer_event_data(event);
739 tracing_generic_entry_update(&entry->ent, flags, pc);
740 entry->ent.type = TRACE_STACK;
742 memset(&entry->caller, 0, sizeof(entry->caller));
744 trace.nr_entries = 0;
745 trace.max_entries = FTRACE_STACK_ENTRIES;
747 trace.entries = entry->caller;
749 save_stack_trace(&trace);
750 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
753 void __trace_stack(struct trace_array *tr,
754 struct trace_array_cpu *data,
758 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
762 ftrace_trace_special(void *__tr, void *__data,
763 unsigned long arg1, unsigned long arg2, unsigned long arg3,
766 struct ring_buffer_event *event;
767 struct trace_array_cpu *data = __data;
768 struct trace_array *tr = __tr;
769 struct special_entry *entry;
770 unsigned long irq_flags;
772 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
776 entry = ring_buffer_event_data(event);
777 tracing_generic_entry_update(&entry->ent, 0, pc);
778 entry->ent.type = TRACE_SPECIAL;
782 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
783 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
789 __trace_special(void *__tr, void *__data,
790 unsigned long arg1, unsigned long arg2, unsigned long arg3)
792 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
796 tracing_sched_switch_trace(struct trace_array *tr,
797 struct trace_array_cpu *data,
798 struct task_struct *prev,
799 struct task_struct *next,
800 unsigned long flags, int pc)
802 struct ring_buffer_event *event;
803 struct ctx_switch_entry *entry;
804 unsigned long irq_flags;
806 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
810 entry = ring_buffer_event_data(event);
811 tracing_generic_entry_update(&entry->ent, flags, pc);
812 entry->ent.type = TRACE_CTX;
813 entry->prev_pid = prev->pid;
814 entry->prev_prio = prev->prio;
815 entry->prev_state = prev->state;
816 entry->next_pid = next->pid;
817 entry->next_prio = next->prio;
818 entry->next_state = next->state;
819 entry->next_cpu = task_cpu(next);
820 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
821 ftrace_trace_stack(tr, data, flags, 5, pc);
825 tracing_sched_wakeup_trace(struct trace_array *tr,
826 struct trace_array_cpu *data,
827 struct task_struct *wakee,
828 struct task_struct *curr,
829 unsigned long flags, int pc)
831 struct ring_buffer_event *event;
832 struct ctx_switch_entry *entry;
833 unsigned long irq_flags;
835 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
839 entry = ring_buffer_event_data(event);
840 tracing_generic_entry_update(&entry->ent, flags, pc);
841 entry->ent.type = TRACE_WAKE;
842 entry->prev_pid = curr->pid;
843 entry->prev_prio = curr->prio;
844 entry->prev_state = curr->state;
845 entry->next_pid = wakee->pid;
846 entry->next_prio = wakee->prio;
847 entry->next_state = wakee->state;
848 entry->next_cpu = task_cpu(wakee);
849 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
850 ftrace_trace_stack(tr, data, flags, 6, pc);
856 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
858 struct trace_array *tr = &global_trace;
859 struct trace_array_cpu *data;
863 if (tracing_disabled || !tr->ctrl)
866 pc = preempt_count();
867 preempt_disable_notrace();
868 cpu = raw_smp_processor_id();
869 data = tr->data[cpu];
871 if (likely(!atomic_read(&data->disabled)))
872 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
874 preempt_enable_notrace();
877 #ifdef CONFIG_FUNCTION_TRACER
879 function_trace_call(unsigned long ip, unsigned long parent_ip)
881 struct trace_array *tr = &global_trace;
882 struct trace_array_cpu *data;
888 if (unlikely(!ftrace_function_enabled))
891 pc = preempt_count();
892 resched = need_resched();
893 preempt_disable_notrace();
894 local_save_flags(flags);
895 cpu = raw_smp_processor_id();
896 data = tr->data[cpu];
897 disabled = atomic_inc_return(&data->disabled);
899 if (likely(disabled == 1))
900 trace_function(tr, data, ip, parent_ip, flags, pc);
902 atomic_dec(&data->disabled);
904 preempt_enable_no_resched_notrace();
906 preempt_enable_notrace();
909 static struct ftrace_ops trace_ops __read_mostly =
911 .func = function_trace_call,
914 void tracing_start_function_trace(void)
916 ftrace_function_enabled = 0;
917 register_ftrace_function(&trace_ops);
919 ftrace_function_enabled = 1;
922 void tracing_stop_function_trace(void)
924 ftrace_function_enabled = 0;
925 unregister_ftrace_function(&trace_ops);
929 enum trace_file_type {
930 TRACE_FILE_LAT_FMT = 1,
933 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
935 /* Don't allow ftrace to trace into the ring buffers */
936 ftrace_disable_cpu();
939 if (iter->buffer_iter[iter->cpu])
940 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
945 static struct trace_entry *
946 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
948 struct ring_buffer_event *event;
949 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
951 /* Don't allow ftrace to trace into the ring buffers */
952 ftrace_disable_cpu();
955 event = ring_buffer_iter_peek(buf_iter, ts);
957 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
961 return event ? ring_buffer_event_data(event) : NULL;
964 static struct trace_entry *
965 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
967 struct ring_buffer *buffer = iter->tr->buffer;
968 struct trace_entry *ent, *next = NULL;
973 for_each_tracing_cpu(cpu) {
975 if (ring_buffer_empty_cpu(buffer, cpu))
978 ent = peek_next_entry(iter, cpu, &ts);
981 * Pick the entry with the smallest timestamp:
983 if (ent && (!next || ts < next_ts)) {
999 /* Find the next real entry, without updating the iterator itself */
1000 static struct trace_entry *
1001 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1003 return __find_next_entry(iter, ent_cpu, ent_ts);
1006 /* Find the next real entry, and increment the iterator to the next entry */
1007 static void *find_next_entry_inc(struct trace_iterator *iter)
1009 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1012 trace_iterator_increment(iter, iter->cpu);
1014 return iter->ent ? iter : NULL;
1017 static void trace_consume(struct trace_iterator *iter)
1019 /* Don't allow ftrace to trace into the ring buffers */
1020 ftrace_disable_cpu();
1021 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1022 ftrace_enable_cpu();
1025 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1027 struct trace_iterator *iter = m->private;
1033 /* can't go backwards */
1038 ent = find_next_entry_inc(iter);
1042 while (ent && iter->idx < i)
1043 ent = find_next_entry_inc(iter);
1050 static void *s_start(struct seq_file *m, loff_t *pos)
1052 struct trace_iterator *iter = m->private;
1057 mutex_lock(&trace_types_lock);
1059 if (!current_trace || current_trace != iter->trace) {
1060 mutex_unlock(&trace_types_lock);
1064 atomic_inc(&trace_record_cmdline_disabled);
1066 /* let the tracer grab locks here if needed */
1067 if (current_trace->start)
1068 current_trace->start(iter);
1070 if (*pos != iter->pos) {
1075 ftrace_disable_cpu();
1077 for_each_tracing_cpu(cpu) {
1078 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1081 ftrace_enable_cpu();
1083 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1088 p = s_next(m, p, &l);
1094 static void s_stop(struct seq_file *m, void *p)
1096 struct trace_iterator *iter = m->private;
1098 atomic_dec(&trace_record_cmdline_disabled);
1100 /* let the tracer release locks here if needed */
1101 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1102 iter->trace->stop(iter);
1104 mutex_unlock(&trace_types_lock);
1107 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1109 #ifdef CONFIG_KRETPROBES
1110 static inline int kretprobed(unsigned long addr)
1112 return addr == (unsigned long)kretprobe_trampoline;
1115 static inline int kretprobed(unsigned long addr)
1119 #endif /* CONFIG_KRETPROBES */
1122 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1124 #ifdef CONFIG_KALLSYMS
1125 char str[KSYM_SYMBOL_LEN];
1127 kallsyms_lookup(address, NULL, NULL, NULL, str);
1129 return trace_seq_printf(s, fmt, str);
1135 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1136 unsigned long address)
1138 #ifdef CONFIG_KALLSYMS
1139 char str[KSYM_SYMBOL_LEN];
1141 sprint_symbol(str, address);
1142 return trace_seq_printf(s, fmt, str);
1147 #ifndef CONFIG_64BIT
1148 # define IP_FMT "%08lx"
1150 # define IP_FMT "%016lx"
1154 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1159 return trace_seq_printf(s, "0");
1161 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1162 ret = seq_print_sym_offset(s, "%s", ip);
1164 ret = seq_print_sym_short(s, "%s", ip);
1169 if (sym_flags & TRACE_ITER_SYM_ADDR)
1170 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1174 static void print_lat_help_header(struct seq_file *m)
1176 seq_puts(m, "# _------=> CPU# \n");
1177 seq_puts(m, "# / _-----=> irqs-off \n");
1178 seq_puts(m, "# | / _----=> need-resched \n");
1179 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1180 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1181 seq_puts(m, "# |||| / \n");
1182 seq_puts(m, "# ||||| delay \n");
1183 seq_puts(m, "# cmd pid ||||| time | caller \n");
1184 seq_puts(m, "# \\ / ||||| \\ | / \n");
1187 static void print_func_help_header(struct seq_file *m)
1189 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1190 seq_puts(m, "# | | | | |\n");
1195 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1197 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1198 struct trace_array *tr = iter->tr;
1199 struct trace_array_cpu *data = tr->data[tr->cpu];
1200 struct tracer *type = current_trace;
1201 unsigned long total;
1202 unsigned long entries;
1203 const char *name = "preemption";
1208 entries = ring_buffer_entries(iter->tr->buffer);
1210 ring_buffer_overruns(iter->tr->buffer);
1212 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1214 seq_puts(m, "-----------------------------------"
1215 "---------------------------------\n");
1216 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1217 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1218 nsecs_to_usecs(data->saved_latency),
1222 #if defined(CONFIG_PREEMPT_NONE)
1224 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1226 #elif defined(CONFIG_PREEMPT)
1231 /* These are reserved for later use */
1234 seq_printf(m, " #P:%d)\n", num_online_cpus());
1238 seq_puts(m, " -----------------\n");
1239 seq_printf(m, " | task: %.16s-%d "
1240 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1241 data->comm, data->pid, data->uid, data->nice,
1242 data->policy, data->rt_priority);
1243 seq_puts(m, " -----------------\n");
1245 if (data->critical_start) {
1246 seq_puts(m, " => started at: ");
1247 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1248 trace_print_seq(m, &iter->seq);
1249 seq_puts(m, "\n => ended at: ");
1250 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1251 trace_print_seq(m, &iter->seq);
1259 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1261 int hardirq, softirq;
1264 comm = trace_find_cmdline(entry->pid);
1266 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1267 trace_seq_printf(s, "%3d", cpu);
1268 trace_seq_printf(s, "%c%c",
1269 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1270 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1272 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1273 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1274 if (hardirq && softirq) {
1275 trace_seq_putc(s, 'H');
1278 trace_seq_putc(s, 'h');
1281 trace_seq_putc(s, 's');
1283 trace_seq_putc(s, '.');
1287 if (entry->preempt_count)
1288 trace_seq_printf(s, "%x", entry->preempt_count);
1290 trace_seq_puts(s, ".");
1293 unsigned long preempt_mark_thresh = 100;
1296 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1297 unsigned long rel_usecs)
1299 trace_seq_printf(s, " %4lldus", abs_usecs);
1300 if (rel_usecs > preempt_mark_thresh)
1301 trace_seq_puts(s, "!: ");
1302 else if (rel_usecs > 1)
1303 trace_seq_puts(s, "+: ");
1305 trace_seq_puts(s, " : ");
1308 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1311 * The message is supposed to contain an ending newline.
1312 * If the printing stops prematurely, try to add a newline of our own.
1314 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1316 struct trace_entry *ent;
1317 struct trace_field_cont *cont;
1320 ent = peek_next_entry(iter, iter->cpu, NULL);
1321 if (!ent || ent->type != TRACE_CONT) {
1322 trace_seq_putc(s, '\n');
1327 cont = (struct trace_field_cont *)ent;
1329 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1331 ftrace_disable_cpu();
1333 if (iter->buffer_iter[iter->cpu])
1334 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1336 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1338 ftrace_enable_cpu();
1340 ent = peek_next_entry(iter, iter->cpu, NULL);
1341 } while (ent && ent->type == TRACE_CONT);
1344 trace_seq_putc(s, '\n');
1347 static enum print_line_t
1348 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1350 struct trace_seq *s = &iter->seq;
1351 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1352 struct trace_entry *next_entry;
1353 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1354 struct trace_entry *entry = iter->ent;
1355 unsigned long abs_usecs;
1356 unsigned long rel_usecs;
1363 if (entry->type == TRACE_CONT)
1364 return TRACE_TYPE_HANDLED;
1366 next_entry = find_next_entry(iter, NULL, &next_ts);
1369 rel_usecs = ns2usecs(next_ts - iter->ts);
1370 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1373 comm = trace_find_cmdline(entry->pid);
1374 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1375 " %ld.%03ldms (+%ld.%03ldms): ",
1377 entry->pid, cpu, entry->flags,
1378 entry->preempt_count, trace_idx,
1381 abs_usecs % 1000, rel_usecs/1000,
1384 lat_print_generic(s, entry, cpu);
1385 lat_print_timestamp(s, abs_usecs, rel_usecs);
1387 switch (entry->type) {
1389 struct ftrace_entry *field;
1391 trace_assign_type(field, entry);
1393 seq_print_ip_sym(s, field->ip, sym_flags);
1394 trace_seq_puts(s, " (");
1395 if (kretprobed(field->parent_ip))
1396 trace_seq_puts(s, KRETPROBE_MSG);
1398 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1399 trace_seq_puts(s, ")\n");
1404 struct ctx_switch_entry *field;
1406 trace_assign_type(field, entry);
1408 T = field->next_state < sizeof(state_to_char) ?
1409 state_to_char[field->next_state] : 'X';
1411 state = field->prev_state ?
1412 __ffs(field->prev_state) + 1 : 0;
1413 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1414 comm = trace_find_cmdline(field->next_pid);
1415 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1418 S, entry->type == TRACE_CTX ? "==>" : " +",
1425 case TRACE_SPECIAL: {
1426 struct special_entry *field;
1428 trace_assign_type(field, entry);
1430 trace_seq_printf(s, "# %ld %ld %ld\n",
1437 struct stack_entry *field;
1439 trace_assign_type(field, entry);
1441 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1443 trace_seq_puts(s, " <= ");
1444 seq_print_ip_sym(s, field->caller[i], sym_flags);
1446 trace_seq_puts(s, "\n");
1450 struct print_entry *field;
1452 trace_assign_type(field, entry);
1454 seq_print_ip_sym(s, field->ip, sym_flags);
1455 trace_seq_printf(s, ": %s", field->buf);
1456 if (entry->flags & TRACE_FLAG_CONT)
1457 trace_seq_print_cont(s, iter);
1461 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1463 return TRACE_TYPE_HANDLED;
1466 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1468 struct trace_seq *s = &iter->seq;
1469 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1470 struct trace_entry *entry;
1471 unsigned long usec_rem;
1472 unsigned long long t;
1481 if (entry->type == TRACE_CONT)
1482 return TRACE_TYPE_HANDLED;
1484 comm = trace_find_cmdline(iter->ent->pid);
1486 t = ns2usecs(iter->ts);
1487 usec_rem = do_div(t, 1000000ULL);
1488 secs = (unsigned long)t;
1490 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1492 return TRACE_TYPE_PARTIAL_LINE;
1493 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1495 return TRACE_TYPE_PARTIAL_LINE;
1496 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1498 return TRACE_TYPE_PARTIAL_LINE;
1500 switch (entry->type) {
1502 struct ftrace_entry *field;
1504 trace_assign_type(field, entry);
1506 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1508 return TRACE_TYPE_PARTIAL_LINE;
1509 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1511 ret = trace_seq_printf(s, " <-");
1513 return TRACE_TYPE_PARTIAL_LINE;
1514 if (kretprobed(field->parent_ip))
1515 ret = trace_seq_puts(s, KRETPROBE_MSG);
1517 ret = seq_print_ip_sym(s,
1521 return TRACE_TYPE_PARTIAL_LINE;
1523 ret = trace_seq_printf(s, "\n");
1525 return TRACE_TYPE_PARTIAL_LINE;
1530 struct ctx_switch_entry *field;
1532 trace_assign_type(field, entry);
1534 S = field->prev_state < sizeof(state_to_char) ?
1535 state_to_char[field->prev_state] : 'X';
1536 T = field->next_state < sizeof(state_to_char) ?
1537 state_to_char[field->next_state] : 'X';
1538 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1542 entry->type == TRACE_CTX ? "==>" : " +",
1548 return TRACE_TYPE_PARTIAL_LINE;
1551 case TRACE_SPECIAL: {
1552 struct special_entry *field;
1554 trace_assign_type(field, entry);
1556 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1561 return TRACE_TYPE_PARTIAL_LINE;
1565 struct stack_entry *field;
1567 trace_assign_type(field, entry);
1569 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1571 ret = trace_seq_puts(s, " <= ");
1573 return TRACE_TYPE_PARTIAL_LINE;
1575 ret = seq_print_ip_sym(s, field->caller[i],
1578 return TRACE_TYPE_PARTIAL_LINE;
1580 ret = trace_seq_puts(s, "\n");
1582 return TRACE_TYPE_PARTIAL_LINE;
1586 struct print_entry *field;
1588 trace_assign_type(field, entry);
1590 seq_print_ip_sym(s, field->ip, sym_flags);
1591 trace_seq_printf(s, ": %s", field->buf);
1592 if (entry->flags & TRACE_FLAG_CONT)
1593 trace_seq_print_cont(s, iter);
1597 return TRACE_TYPE_HANDLED;
1600 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1602 struct trace_seq *s = &iter->seq;
1603 struct trace_entry *entry;
1609 if (entry->type == TRACE_CONT)
1610 return TRACE_TYPE_HANDLED;
1612 ret = trace_seq_printf(s, "%d %d %llu ",
1613 entry->pid, iter->cpu, iter->ts);
1615 return TRACE_TYPE_PARTIAL_LINE;
1617 switch (entry->type) {
1619 struct ftrace_entry *field;
1621 trace_assign_type(field, entry);
1623 ret = trace_seq_printf(s, "%x %x\n",
1627 return TRACE_TYPE_PARTIAL_LINE;
1632 struct ctx_switch_entry *field;
1634 trace_assign_type(field, entry);
1636 S = field->prev_state < sizeof(state_to_char) ?
1637 state_to_char[field->prev_state] : 'X';
1638 T = field->next_state < sizeof(state_to_char) ?
1639 state_to_char[field->next_state] : 'X';
1640 if (entry->type == TRACE_WAKE)
1642 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
1651 return TRACE_TYPE_PARTIAL_LINE;
1656 struct special_entry *field;
1658 trace_assign_type(field, entry);
1660 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1665 return TRACE_TYPE_PARTIAL_LINE;
1669 struct print_entry *field;
1671 trace_assign_type(field, entry);
1673 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
1674 if (entry->flags & TRACE_FLAG_CONT)
1675 trace_seq_print_cont(s, iter);
1679 return TRACE_TYPE_HANDLED;
1682 #define SEQ_PUT_FIELD_RET(s, x) \
1684 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1688 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1690 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
1691 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1695 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1697 struct trace_seq *s = &iter->seq;
1698 unsigned char newline = '\n';
1699 struct trace_entry *entry;
1704 if (entry->type == TRACE_CONT)
1705 return TRACE_TYPE_HANDLED;
1707 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1708 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1709 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1711 switch (entry->type) {
1713 struct ftrace_entry *field;
1715 trace_assign_type(field, entry);
1717 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1718 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
1723 struct ctx_switch_entry *field;
1725 trace_assign_type(field, entry);
1727 S = field->prev_state < sizeof(state_to_char) ?
1728 state_to_char[field->prev_state] : 'X';
1729 T = field->next_state < sizeof(state_to_char) ?
1730 state_to_char[field->next_state] : 'X';
1731 if (entry->type == TRACE_WAKE)
1733 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1734 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
1735 SEQ_PUT_HEX_FIELD_RET(s, S);
1736 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1737 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1738 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
1739 SEQ_PUT_HEX_FIELD_RET(s, T);
1744 struct special_entry *field;
1746 trace_assign_type(field, entry);
1748 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
1749 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
1750 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
1754 SEQ_PUT_FIELD_RET(s, newline);
1756 return TRACE_TYPE_HANDLED;
1759 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1761 struct trace_seq *s = &iter->seq;
1762 struct trace_entry *entry;
1766 if (entry->type == TRACE_CONT)
1767 return TRACE_TYPE_HANDLED;
1769 SEQ_PUT_FIELD_RET(s, entry->pid);
1770 SEQ_PUT_FIELD_RET(s, iter->cpu);
1771 SEQ_PUT_FIELD_RET(s, iter->ts);
1773 switch (entry->type) {
1775 struct ftrace_entry *field;
1777 trace_assign_type(field, entry);
1779 SEQ_PUT_FIELD_RET(s, field->ip);
1780 SEQ_PUT_FIELD_RET(s, field->parent_ip);
1784 struct ctx_switch_entry *field;
1786 trace_assign_type(field, entry);
1788 SEQ_PUT_FIELD_RET(s, field->prev_pid);
1789 SEQ_PUT_FIELD_RET(s, field->prev_prio);
1790 SEQ_PUT_FIELD_RET(s, field->prev_state);
1791 SEQ_PUT_FIELD_RET(s, field->next_pid);
1792 SEQ_PUT_FIELD_RET(s, field->next_prio);
1793 SEQ_PUT_FIELD_RET(s, field->next_state);
1798 struct special_entry *field;
1800 trace_assign_type(field, entry);
1802 SEQ_PUT_FIELD_RET(s, field->arg1);
1803 SEQ_PUT_FIELD_RET(s, field->arg2);
1804 SEQ_PUT_FIELD_RET(s, field->arg3);
1811 static int trace_empty(struct trace_iterator *iter)
1815 for_each_tracing_cpu(cpu) {
1816 if (iter->buffer_iter[cpu]) {
1817 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1820 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1828 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1830 enum print_line_t ret;
1832 if (iter->trace && iter->trace->print_line) {
1833 ret = iter->trace->print_line(iter);
1834 if (ret != TRACE_TYPE_UNHANDLED)
1838 if (trace_flags & TRACE_ITER_BIN)
1839 return print_bin_fmt(iter);
1841 if (trace_flags & TRACE_ITER_HEX)
1842 return print_hex_fmt(iter);
1844 if (trace_flags & TRACE_ITER_RAW)
1845 return print_raw_fmt(iter);
1847 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1848 return print_lat_fmt(iter, iter->idx, iter->cpu);
1850 return print_trace_fmt(iter);
1853 static int s_show(struct seq_file *m, void *v)
1855 struct trace_iterator *iter = v;
1857 if (iter->ent == NULL) {
1859 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1862 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1863 /* print nothing if the buffers are empty */
1864 if (trace_empty(iter))
1866 print_trace_header(m, iter);
1867 if (!(trace_flags & TRACE_ITER_VERBOSE))
1868 print_lat_help_header(m);
1870 if (!(trace_flags & TRACE_ITER_VERBOSE))
1871 print_func_help_header(m);
1874 print_trace_line(iter);
1875 trace_print_seq(m, &iter->seq);
1881 static struct seq_operations tracer_seq_ops = {
1888 static struct trace_iterator *
1889 __tracing_open(struct inode *inode, struct file *file, int *ret)
1891 struct trace_iterator *iter;
1895 if (tracing_disabled) {
1900 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1906 mutex_lock(&trace_types_lock);
1907 if (current_trace && current_trace->print_max)
1910 iter->tr = inode->i_private;
1911 iter->trace = current_trace;
1914 for_each_tracing_cpu(cpu) {
1916 iter->buffer_iter[cpu] =
1917 ring_buffer_read_start(iter->tr->buffer, cpu);
1919 if (!iter->buffer_iter[cpu])
1923 /* TODO stop tracer */
1924 *ret = seq_open(file, &tracer_seq_ops);
1928 m = file->private_data;
1931 /* stop the trace while dumping */
1932 if (iter->tr->ctrl) {
1934 ftrace_function_enabled = 0;
1937 if (iter->trace && iter->trace->open)
1938 iter->trace->open(iter);
1940 mutex_unlock(&trace_types_lock);
1946 for_each_tracing_cpu(cpu) {
1947 if (iter->buffer_iter[cpu])
1948 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1950 mutex_unlock(&trace_types_lock);
1952 return ERR_PTR(-ENOMEM);
1955 int tracing_open_generic(struct inode *inode, struct file *filp)
1957 if (tracing_disabled)
1960 filp->private_data = inode->i_private;
1964 int tracing_release(struct inode *inode, struct file *file)
1966 struct seq_file *m = (struct seq_file *)file->private_data;
1967 struct trace_iterator *iter = m->private;
1970 mutex_lock(&trace_types_lock);
1971 for_each_tracing_cpu(cpu) {
1972 if (iter->buffer_iter[cpu])
1973 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1976 if (iter->trace && iter->trace->close)
1977 iter->trace->close(iter);
1979 /* reenable tracing if it was previously enabled */
1980 if (iter->tr->ctrl) {
1983 * It is safe to enable function tracing even if it
1986 ftrace_function_enabled = 1;
1988 mutex_unlock(&trace_types_lock);
1990 seq_release(inode, file);
1995 static int tracing_open(struct inode *inode, struct file *file)
1999 __tracing_open(inode, file, &ret);
2004 static int tracing_lt_open(struct inode *inode, struct file *file)
2006 struct trace_iterator *iter;
2009 iter = __tracing_open(inode, file, &ret);
2012 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2019 t_next(struct seq_file *m, void *v, loff_t *pos)
2021 struct tracer *t = m->private;
2033 static void *t_start(struct seq_file *m, loff_t *pos)
2035 struct tracer *t = m->private;
2038 mutex_lock(&trace_types_lock);
2039 for (; t && l < *pos; t = t_next(m, t, &l))
2045 static void t_stop(struct seq_file *m, void *p)
2047 mutex_unlock(&trace_types_lock);
2050 static int t_show(struct seq_file *m, void *v)
2052 struct tracer *t = v;
2057 seq_printf(m, "%s", t->name);
2066 static struct seq_operations show_traces_seq_ops = {
2073 static int show_traces_open(struct inode *inode, struct file *file)
2077 if (tracing_disabled)
2080 ret = seq_open(file, &show_traces_seq_ops);
2082 struct seq_file *m = file->private_data;
2083 m->private = trace_types;
2089 static struct file_operations tracing_fops = {
2090 .open = tracing_open,
2092 .llseek = seq_lseek,
2093 .release = tracing_release,
2096 static struct file_operations tracing_lt_fops = {
2097 .open = tracing_lt_open,
2099 .llseek = seq_lseek,
2100 .release = tracing_release,
2103 static struct file_operations show_traces_fops = {
2104 .open = show_traces_open,
2106 .release = seq_release,
2110 * Only trace on a CPU if the bitmask is set:
2112 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2115 * When tracing/tracing_cpu_mask is modified then this holds
2116 * the new bitmask we are about to install:
2118 static cpumask_t tracing_cpumask_new;
2121 * The tracer itself will not take this lock, but still we want
2122 * to provide a consistent cpumask to user-space:
2124 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2127 * Temporary storage for the character representation of the
2128 * CPU bitmask (and one more byte for the newline):
2130 static char mask_str[NR_CPUS + 1];
2133 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2134 size_t count, loff_t *ppos)
2138 mutex_lock(&tracing_cpumask_update_lock);
2140 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2141 if (count - len < 2) {
2145 len += sprintf(mask_str + len, "\n");
2146 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2149 mutex_unlock(&tracing_cpumask_update_lock);
2155 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2156 size_t count, loff_t *ppos)
2160 mutex_lock(&tracing_cpumask_update_lock);
2161 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2165 raw_local_irq_disable();
2166 __raw_spin_lock(&ftrace_max_lock);
2167 for_each_tracing_cpu(cpu) {
2169 * Increase/decrease the disabled counter if we are
2170 * about to flip a bit in the cpumask:
2172 if (cpu_isset(cpu, tracing_cpumask) &&
2173 !cpu_isset(cpu, tracing_cpumask_new)) {
2174 atomic_inc(&global_trace.data[cpu]->disabled);
2176 if (!cpu_isset(cpu, tracing_cpumask) &&
2177 cpu_isset(cpu, tracing_cpumask_new)) {
2178 atomic_dec(&global_trace.data[cpu]->disabled);
2181 __raw_spin_unlock(&ftrace_max_lock);
2182 raw_local_irq_enable();
2184 tracing_cpumask = tracing_cpumask_new;
2186 mutex_unlock(&tracing_cpumask_update_lock);
2191 mutex_unlock(&tracing_cpumask_update_lock);
2196 static struct file_operations tracing_cpumask_fops = {
2197 .open = tracing_open_generic,
2198 .read = tracing_cpumask_read,
2199 .write = tracing_cpumask_write,
2203 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2204 size_t cnt, loff_t *ppos)
2211 /* calulate max size */
2212 for (i = 0; trace_options[i]; i++) {
2213 len += strlen(trace_options[i]);
2214 len += 3; /* "no" and space */
2217 /* +2 for \n and \0 */
2218 buf = kmalloc(len + 2, GFP_KERNEL);
2222 for (i = 0; trace_options[i]; i++) {
2223 if (trace_flags & (1 << i))
2224 r += sprintf(buf + r, "%s ", trace_options[i]);
2226 r += sprintf(buf + r, "no%s ", trace_options[i]);
2229 r += sprintf(buf + r, "\n");
2230 WARN_ON(r >= len + 2);
2232 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2240 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2241 size_t cnt, loff_t *ppos)
2248 if (cnt >= sizeof(buf))
2251 if (copy_from_user(&buf, ubuf, cnt))
2256 if (strncmp(buf, "no", 2) == 0) {
2261 for (i = 0; trace_options[i]; i++) {
2262 int len = strlen(trace_options[i]);
2264 if (strncmp(cmp, trace_options[i], len) == 0) {
2266 trace_flags &= ~(1 << i);
2268 trace_flags |= (1 << i);
2273 * If no option could be set, return an error:
2275 if (!trace_options[i])
2283 static struct file_operations tracing_iter_fops = {
2284 .open = tracing_open_generic,
2285 .read = tracing_iter_ctrl_read,
2286 .write = tracing_iter_ctrl_write,
2289 static const char readme_msg[] =
2290 "tracing mini-HOWTO:\n\n"
2292 "# mount -t debugfs nodev /debug\n\n"
2293 "# cat /debug/tracing/available_tracers\n"
2294 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2295 "# cat /debug/tracing/current_tracer\n"
2297 "# echo sched_switch > /debug/tracing/current_tracer\n"
2298 "# cat /debug/tracing/current_tracer\n"
2300 "# cat /debug/tracing/iter_ctrl\n"
2301 "noprint-parent nosym-offset nosym-addr noverbose\n"
2302 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2303 "# echo 1 > /debug/tracing/tracing_enabled\n"
2304 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2305 "echo 0 > /debug/tracing/tracing_enabled\n"
2309 tracing_readme_read(struct file *filp, char __user *ubuf,
2310 size_t cnt, loff_t *ppos)
2312 return simple_read_from_buffer(ubuf, cnt, ppos,
2313 readme_msg, strlen(readme_msg));
2316 static struct file_operations tracing_readme_fops = {
2317 .open = tracing_open_generic,
2318 .read = tracing_readme_read,
2322 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2323 size_t cnt, loff_t *ppos)
2325 struct trace_array *tr = filp->private_data;
2329 r = sprintf(buf, "%ld\n", tr->ctrl);
2330 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2334 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2335 size_t cnt, loff_t *ppos)
2337 struct trace_array *tr = filp->private_data;
2342 if (cnt >= sizeof(buf))
2345 if (copy_from_user(&buf, ubuf, cnt))
2350 ret = strict_strtoul(buf, 10, &val);
2356 mutex_lock(&trace_types_lock);
2357 if (tr->ctrl ^ val) {
2365 if (current_trace && current_trace->ctrl_update)
2366 current_trace->ctrl_update(tr);
2368 mutex_unlock(&trace_types_lock);
2376 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2377 size_t cnt, loff_t *ppos)
2379 char buf[max_tracer_type_len+2];
2382 mutex_lock(&trace_types_lock);
2384 r = sprintf(buf, "%s\n", current_trace->name);
2386 r = sprintf(buf, "\n");
2387 mutex_unlock(&trace_types_lock);
2389 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2393 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2394 size_t cnt, loff_t *ppos)
2396 struct trace_array *tr = &global_trace;
2398 char buf[max_tracer_type_len+1];
2404 if (cnt > max_tracer_type_len)
2405 cnt = max_tracer_type_len;
2407 if (copy_from_user(&buf, ubuf, cnt))
2412 /* strip ending whitespace. */
2413 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2416 mutex_lock(&trace_types_lock);
2417 for (t = trace_types; t; t = t->next) {
2418 if (strcmp(t->name, buf) == 0)
2425 if (t == current_trace)
2428 if (current_trace && current_trace->reset)
2429 current_trace->reset(tr);
2436 mutex_unlock(&trace_types_lock);
2445 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2446 size_t cnt, loff_t *ppos)
2448 unsigned long *ptr = filp->private_data;
2452 r = snprintf(buf, sizeof(buf), "%ld\n",
2453 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2454 if (r > sizeof(buf))
2456 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2460 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2461 size_t cnt, loff_t *ppos)
2463 long *ptr = filp->private_data;
2468 if (cnt >= sizeof(buf))
2471 if (copy_from_user(&buf, ubuf, cnt))
2476 ret = strict_strtoul(buf, 10, &val);
2485 static atomic_t tracing_reader;
2487 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2489 struct trace_iterator *iter;
2491 if (tracing_disabled)
2494 /* We only allow for reader of the pipe */
2495 if (atomic_inc_return(&tracing_reader) != 1) {
2496 atomic_dec(&tracing_reader);
2500 /* create a buffer to store the information to pass to userspace */
2501 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2505 mutex_lock(&trace_types_lock);
2506 iter->tr = &global_trace;
2507 iter->trace = current_trace;
2508 filp->private_data = iter;
2510 if (iter->trace->pipe_open)
2511 iter->trace->pipe_open(iter);
2512 mutex_unlock(&trace_types_lock);
2517 static int tracing_release_pipe(struct inode *inode, struct file *file)
2519 struct trace_iterator *iter = file->private_data;
2522 atomic_dec(&tracing_reader);
2528 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2530 struct trace_iterator *iter = filp->private_data;
2532 if (trace_flags & TRACE_ITER_BLOCK) {
2534 * Always select as readable when in blocking mode
2536 return POLLIN | POLLRDNORM;
2538 if (!trace_empty(iter))
2539 return POLLIN | POLLRDNORM;
2540 poll_wait(filp, &trace_wait, poll_table);
2541 if (!trace_empty(iter))
2542 return POLLIN | POLLRDNORM;
2552 tracing_read_pipe(struct file *filp, char __user *ubuf,
2553 size_t cnt, loff_t *ppos)
2555 struct trace_iterator *iter = filp->private_data;
2558 /* return any leftover data */
2559 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2563 trace_seq_reset(&iter->seq);
2565 mutex_lock(&trace_types_lock);
2566 if (iter->trace->read) {
2567 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2574 while (trace_empty(iter)) {
2576 if ((filp->f_flags & O_NONBLOCK)) {
2582 * This is a make-shift waitqueue. The reason we don't use
2583 * an actual wait queue is because:
2584 * 1) we only ever have one waiter
2585 * 2) the tracing, traces all functions, we don't want
2586 * the overhead of calling wake_up and friends
2587 * (and tracing them too)
2588 * Anyway, this is really very primitive wakeup.
2590 set_current_state(TASK_INTERRUPTIBLE);
2591 iter->tr->waiter = current;
2593 mutex_unlock(&trace_types_lock);
2595 /* sleep for 100 msecs, and try again. */
2596 schedule_timeout(HZ/10);
2598 mutex_lock(&trace_types_lock);
2600 iter->tr->waiter = NULL;
2602 if (signal_pending(current)) {
2607 if (iter->trace != current_trace)
2611 * We block until we read something and tracing is disabled.
2612 * We still block if tracing is disabled, but we have never
2613 * read anything. This allows a user to cat this file, and
2614 * then enable tracing. But after we have read something,
2615 * we give an EOF when tracing is again disabled.
2617 * iter->pos will be 0 if we haven't read anything.
2619 if (!tracer_enabled && iter->pos)
2625 /* stop when tracing is finished */
2626 if (trace_empty(iter))
2629 if (cnt >= PAGE_SIZE)
2630 cnt = PAGE_SIZE - 1;
2632 /* reset all but tr, trace, and overruns */
2633 memset(&iter->seq, 0,
2634 sizeof(struct trace_iterator) -
2635 offsetof(struct trace_iterator, seq));
2638 while (find_next_entry_inc(iter) != NULL) {
2639 enum print_line_t ret;
2640 int len = iter->seq.len;
2642 ret = print_trace_line(iter);
2643 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2644 /* don't print partial lines */
2645 iter->seq.len = len;
2649 trace_consume(iter);
2651 if (iter->seq.len >= cnt)
2655 /* Now copy what we have to the user */
2656 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2657 if (iter->seq.readpos >= iter->seq.len)
2658 trace_seq_reset(&iter->seq);
2661 * If there was nothing to send to user, inspite of consuming trace
2662 * entries, go back to wait for more entries.
2668 mutex_unlock(&trace_types_lock);
2674 tracing_entries_read(struct file *filp, char __user *ubuf,
2675 size_t cnt, loff_t *ppos)
2677 struct trace_array *tr = filp->private_data;
2681 r = sprintf(buf, "%lu\n", tr->entries);
2682 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2686 tracing_entries_write(struct file *filp, const char __user *ubuf,
2687 size_t cnt, loff_t *ppos)
2692 struct trace_array *tr = filp->private_data;
2694 if (cnt >= sizeof(buf))
2697 if (copy_from_user(&buf, ubuf, cnt))
2702 ret = strict_strtoul(buf, 10, &val);
2706 /* must have at least 1 entry */
2710 mutex_lock(&trace_types_lock);
2714 pr_info("ftrace: please disable tracing"
2715 " before modifying buffer size\n");
2719 if (val != global_trace.entries) {
2720 ret = ring_buffer_resize(global_trace.buffer, val);
2726 ret = ring_buffer_resize(max_tr.buffer, val);
2730 r = ring_buffer_resize(global_trace.buffer,
2731 global_trace.entries);
2733 /* AARGH! We are left with different
2734 * size max buffer!!!! */
2736 tracing_disabled = 1;
2741 global_trace.entries = val;
2746 /* If check pages failed, return ENOMEM */
2747 if (tracing_disabled)
2750 max_tr.entries = global_trace.entries;
2751 mutex_unlock(&trace_types_lock);
2756 static int mark_printk(const char *fmt, ...)
2760 va_start(args, fmt);
2761 ret = trace_vprintk(0, fmt, args);
2767 tracing_mark_write(struct file *filp, const char __user *ubuf,
2768 size_t cnt, loff_t *fpos)
2772 struct trace_array *tr = &global_trace;
2774 if (!tr->ctrl || tracing_disabled)
2777 if (cnt > TRACE_BUF_SIZE)
2778 cnt = TRACE_BUF_SIZE;
2780 buf = kmalloc(cnt + 1, GFP_KERNEL);
2784 if (copy_from_user(buf, ubuf, cnt)) {
2789 /* Cut from the first nil or newline. */
2791 end = strchr(buf, '\n');
2795 cnt = mark_printk("%s\n", buf);
2802 static struct file_operations tracing_max_lat_fops = {
2803 .open = tracing_open_generic,
2804 .read = tracing_max_lat_read,
2805 .write = tracing_max_lat_write,
2808 static struct file_operations tracing_ctrl_fops = {
2809 .open = tracing_open_generic,
2810 .read = tracing_ctrl_read,
2811 .write = tracing_ctrl_write,
2814 static struct file_operations set_tracer_fops = {
2815 .open = tracing_open_generic,
2816 .read = tracing_set_trace_read,
2817 .write = tracing_set_trace_write,
2820 static struct file_operations tracing_pipe_fops = {
2821 .open = tracing_open_pipe,
2822 .poll = tracing_poll_pipe,
2823 .read = tracing_read_pipe,
2824 .release = tracing_release_pipe,
2827 static struct file_operations tracing_entries_fops = {
2828 .open = tracing_open_generic,
2829 .read = tracing_entries_read,
2830 .write = tracing_entries_write,
2833 static struct file_operations tracing_mark_fops = {
2834 .open = tracing_open_generic,
2835 .write = tracing_mark_write,
2838 #ifdef CONFIG_DYNAMIC_FTRACE
2841 tracing_read_long(struct file *filp, char __user *ubuf,
2842 size_t cnt, loff_t *ppos)
2844 unsigned long *p = filp->private_data;
2848 r = sprintf(buf, "%ld\n", *p);
2850 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2853 static struct file_operations tracing_read_long_fops = {
2854 .open = tracing_open_generic,
2855 .read = tracing_read_long,
2859 static struct dentry *d_tracer;
2861 struct dentry *tracing_init_dentry(void)
2868 d_tracer = debugfs_create_dir("tracing", NULL);
2870 if (!d_tracer && !once) {
2872 pr_warning("Could not create debugfs directory 'tracing'\n");
2879 #ifdef CONFIG_FTRACE_SELFTEST
2880 /* Let selftest have access to static functions in this file */
2881 #include "trace_selftest.c"
2884 static __init int tracer_init_debugfs(void)
2886 struct dentry *d_tracer;
2887 struct dentry *entry;
2889 d_tracer = tracing_init_dentry();
2891 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2892 &global_trace, &tracing_ctrl_fops);
2894 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2896 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2897 NULL, &tracing_iter_fops);
2899 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2901 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2902 NULL, &tracing_cpumask_fops);
2904 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2906 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2907 &global_trace, &tracing_lt_fops);
2909 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2911 entry = debugfs_create_file("trace", 0444, d_tracer,
2912 &global_trace, &tracing_fops);
2914 pr_warning("Could not create debugfs 'trace' entry\n");
2916 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2917 &global_trace, &show_traces_fops);
2919 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2921 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2922 &global_trace, &set_tracer_fops);
2924 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2926 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2927 &tracing_max_latency,
2928 &tracing_max_lat_fops);
2930 pr_warning("Could not create debugfs "
2931 "'tracing_max_latency' entry\n");
2933 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2934 &tracing_thresh, &tracing_max_lat_fops);
2936 pr_warning("Could not create debugfs "
2937 "'tracing_thresh' entry\n");
2938 entry = debugfs_create_file("README", 0644, d_tracer,
2939 NULL, &tracing_readme_fops);
2941 pr_warning("Could not create debugfs 'README' entry\n");
2943 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2944 NULL, &tracing_pipe_fops);
2946 pr_warning("Could not create debugfs "
2947 "'trace_pipe' entry\n");
2949 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2950 &global_trace, &tracing_entries_fops);
2952 pr_warning("Could not create debugfs "
2953 "'trace_entries' entry\n");
2955 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2956 NULL, &tracing_mark_fops);
2958 pr_warning("Could not create debugfs "
2959 "'trace_marker' entry\n");
2961 #ifdef CONFIG_DYNAMIC_FTRACE
2962 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2963 &ftrace_update_tot_cnt,
2964 &tracing_read_long_fops);
2966 pr_warning("Could not create debugfs "
2967 "'dyn_ftrace_total_info' entry\n");
2969 #ifdef CONFIG_SYSPROF_TRACER
2970 init_tracer_sysprof_debugfs(d_tracer);
2975 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2977 static DEFINE_SPINLOCK(trace_buf_lock);
2978 static char trace_buf[TRACE_BUF_SIZE];
2980 struct ring_buffer_event *event;
2981 struct trace_array *tr = &global_trace;
2982 struct trace_array_cpu *data;
2983 struct print_entry *entry;
2984 unsigned long flags, irq_flags;
2985 int cpu, len = 0, size, pc;
2987 if (!tr->ctrl || tracing_disabled)
2990 pc = preempt_count();
2991 preempt_disable_notrace();
2992 cpu = raw_smp_processor_id();
2993 data = tr->data[cpu];
2995 if (unlikely(atomic_read(&data->disabled)))
2998 spin_lock_irqsave(&trace_buf_lock, flags);
2999 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3001 len = min(len, TRACE_BUF_SIZE-1);
3004 size = sizeof(*entry) + len + 1;
3005 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3008 entry = ring_buffer_event_data(event);
3009 tracing_generic_entry_update(&entry->ent, flags, pc);
3010 entry->ent.type = TRACE_PRINT;
3013 memcpy(&entry->buf, trace_buf, len);
3014 entry->buf[len] = 0;
3015 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3018 spin_unlock_irqrestore(&trace_buf_lock, flags);
3021 preempt_enable_notrace();
3025 EXPORT_SYMBOL_GPL(trace_vprintk);
3027 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3032 if (!(trace_flags & TRACE_ITER_PRINTK))
3036 ret = trace_vprintk(ip, fmt, ap);
3040 EXPORT_SYMBOL_GPL(__ftrace_printk);
3042 static int trace_panic_handler(struct notifier_block *this,
3043 unsigned long event, void *unused)
3045 if (ftrace_dump_on_oops)
3050 static struct notifier_block trace_panic_notifier = {
3051 .notifier_call = trace_panic_handler,
3053 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3056 static int trace_die_handler(struct notifier_block *self,
3062 if (ftrace_dump_on_oops)
3071 static struct notifier_block trace_die_notifier = {
3072 .notifier_call = trace_die_handler,
3077 * printk is set to max of 1024, we really don't need it that big.
3078 * Nothing should be printing 1000 characters anyway.
3080 #define TRACE_MAX_PRINT 1000
3083 * Define here KERN_TRACE so that we have one place to modify
3084 * it if we decide to change what log level the ftrace dump
3087 #define KERN_TRACE KERN_INFO
3090 trace_printk_seq(struct trace_seq *s)
3092 /* Probably should print a warning here. */
3096 /* should be zero ended, but we are paranoid. */
3097 s->buffer[s->len] = 0;
3099 printk(KERN_TRACE "%s", s->buffer);
3104 void ftrace_dump(void)
3106 static DEFINE_SPINLOCK(ftrace_dump_lock);
3107 /* use static because iter can be a bit big for the stack */
3108 static struct trace_iterator iter;
3109 static cpumask_t mask;
3110 static int dump_ran;
3111 unsigned long flags;
3115 spin_lock_irqsave(&ftrace_dump_lock, flags);
3121 /* No turning back! */
3124 for_each_tracing_cpu(cpu) {
3125 atomic_inc(&global_trace.data[cpu]->disabled);
3128 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3130 iter.tr = &global_trace;
3131 iter.trace = current_trace;
3134 * We need to stop all tracing on all CPUS to read the
3135 * the next buffer. This is a bit expensive, but is
3136 * not done often. We fill all what we can read,
3137 * and then release the locks again.
3142 while (!trace_empty(&iter)) {
3145 printk(KERN_TRACE "---------------------------------\n");
3149 /* reset all but tr, trace, and overruns */
3150 memset(&iter.seq, 0,
3151 sizeof(struct trace_iterator) -
3152 offsetof(struct trace_iterator, seq));
3153 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3156 if (find_next_entry_inc(&iter) != NULL) {
3157 print_trace_line(&iter);
3158 trace_consume(&iter);
3161 trace_printk_seq(&iter.seq);
3165 printk(KERN_TRACE " (ftrace buffer empty)\n");
3167 printk(KERN_TRACE "---------------------------------\n");
3170 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3173 __init static int tracer_alloc_buffers(void)
3175 struct trace_array_cpu *data;
3178 /* TODO: make the number of buffers hot pluggable with CPUS */
3179 tracing_buffer_mask = cpu_possible_map;
3181 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3182 TRACE_BUFFER_FLAGS);
3183 if (!global_trace.buffer) {
3184 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3188 global_trace.entries = ring_buffer_size(global_trace.buffer);
3190 #ifdef CONFIG_TRACER_MAX_TRACE
3191 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3192 TRACE_BUFFER_FLAGS);
3193 if (!max_tr.buffer) {
3194 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3196 ring_buffer_free(global_trace.buffer);
3199 max_tr.entries = ring_buffer_size(max_tr.buffer);
3200 WARN_ON(max_tr.entries != global_trace.entries);
3203 /* Allocate the first page for all buffers */
3204 for_each_tracing_cpu(i) {
3205 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3206 max_tr.data[i] = &per_cpu(max_data, i);
3209 trace_init_cmdlines();
3211 register_tracer(&nop_trace);
3212 #ifdef CONFIG_BOOT_TRACER
3213 register_tracer(&boot_tracer);
3214 current_trace = &boot_tracer;
3215 current_trace->init(&global_trace);
3217 current_trace = &nop_trace;
3220 /* All seems OK, enable tracing */
3221 global_trace.ctrl = tracer_enabled;
3222 tracing_disabled = 0;
3224 atomic_notifier_chain_register(&panic_notifier_list,
3225 &trace_panic_notifier);
3227 register_die_notifier(&trace_die_notifier);
3231 early_initcall(tracer_alloc_buffers);
3232 fs_initcall(tracer_init_debugfs);