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 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
682 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
684 TRACE_FLAG_IRQS_NOSUPPORT |
686 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
687 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
688 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
692 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
693 unsigned long ip, unsigned long parent_ip, unsigned long flags,
696 struct ring_buffer_event *event;
697 struct ftrace_entry *entry;
698 unsigned long irq_flags;
700 /* If we are reading the ring buffer, don't trace */
701 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
704 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
708 entry = ring_buffer_event_data(event);
709 tracing_generic_entry_update(&entry->ent, flags, pc);
710 entry->ent.type = TRACE_FN;
712 entry->parent_ip = parent_ip;
713 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
717 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
718 unsigned long ip, unsigned long parent_ip, unsigned long flags,
721 if (likely(!atomic_read(&data->disabled)))
722 trace_function(tr, data, ip, parent_ip, flags, pc);
725 static void ftrace_trace_stack(struct trace_array *tr,
726 struct trace_array_cpu *data,
730 struct ring_buffer_event *event;
731 struct stack_entry *entry;
732 struct stack_trace trace;
733 unsigned long irq_flags;
735 if (!(trace_flags & TRACE_ITER_STACKTRACE))
738 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
742 entry = ring_buffer_event_data(event);
743 tracing_generic_entry_update(&entry->ent, flags, pc);
744 entry->ent.type = TRACE_STACK;
746 memset(&entry->caller, 0, sizeof(entry->caller));
748 trace.nr_entries = 0;
749 trace.max_entries = FTRACE_STACK_ENTRIES;
751 trace.entries = entry->caller;
753 save_stack_trace(&trace);
754 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
757 void __trace_stack(struct trace_array *tr,
758 struct trace_array_cpu *data,
762 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
766 ftrace_trace_special(void *__tr, void *__data,
767 unsigned long arg1, unsigned long arg2, unsigned long arg3,
770 struct ring_buffer_event *event;
771 struct trace_array_cpu *data = __data;
772 struct trace_array *tr = __tr;
773 struct special_entry *entry;
774 unsigned long irq_flags;
776 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
780 entry = ring_buffer_event_data(event);
781 tracing_generic_entry_update(&entry->ent, 0, pc);
782 entry->ent.type = TRACE_SPECIAL;
786 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
787 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
793 __trace_special(void *__tr, void *__data,
794 unsigned long arg1, unsigned long arg2, unsigned long arg3)
796 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
800 tracing_sched_switch_trace(struct trace_array *tr,
801 struct trace_array_cpu *data,
802 struct task_struct *prev,
803 struct task_struct *next,
804 unsigned long flags, int pc)
806 struct ring_buffer_event *event;
807 struct ctx_switch_entry *entry;
808 unsigned long irq_flags;
810 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
814 entry = ring_buffer_event_data(event);
815 tracing_generic_entry_update(&entry->ent, flags, pc);
816 entry->ent.type = TRACE_CTX;
817 entry->prev_pid = prev->pid;
818 entry->prev_prio = prev->prio;
819 entry->prev_state = prev->state;
820 entry->next_pid = next->pid;
821 entry->next_prio = next->prio;
822 entry->next_state = next->state;
823 entry->next_cpu = task_cpu(next);
824 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
825 ftrace_trace_stack(tr, data, flags, 5, pc);
829 tracing_sched_wakeup_trace(struct trace_array *tr,
830 struct trace_array_cpu *data,
831 struct task_struct *wakee,
832 struct task_struct *curr,
833 unsigned long flags, int pc)
835 struct ring_buffer_event *event;
836 struct ctx_switch_entry *entry;
837 unsigned long irq_flags;
839 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
843 entry = ring_buffer_event_data(event);
844 tracing_generic_entry_update(&entry->ent, flags, pc);
845 entry->ent.type = TRACE_WAKE;
846 entry->prev_pid = curr->pid;
847 entry->prev_prio = curr->prio;
848 entry->prev_state = curr->state;
849 entry->next_pid = wakee->pid;
850 entry->next_prio = wakee->prio;
851 entry->next_state = wakee->state;
852 entry->next_cpu = task_cpu(wakee);
853 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
854 ftrace_trace_stack(tr, data, flags, 6, pc);
860 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
862 struct trace_array *tr = &global_trace;
863 struct trace_array_cpu *data;
867 if (tracing_disabled || !tr->ctrl)
870 pc = preempt_count();
871 preempt_disable_notrace();
872 cpu = raw_smp_processor_id();
873 data = tr->data[cpu];
875 if (likely(!atomic_read(&data->disabled)))
876 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
878 preempt_enable_notrace();
881 #ifdef CONFIG_FUNCTION_TRACER
883 function_trace_call(unsigned long ip, unsigned long parent_ip)
885 struct trace_array *tr = &global_trace;
886 struct trace_array_cpu *data;
892 if (unlikely(!ftrace_function_enabled))
895 pc = preempt_count();
896 resched = need_resched();
897 preempt_disable_notrace();
898 local_save_flags(flags);
899 cpu = raw_smp_processor_id();
900 data = tr->data[cpu];
901 disabled = atomic_inc_return(&data->disabled);
903 if (likely(disabled == 1))
904 trace_function(tr, data, ip, parent_ip, flags, pc);
906 atomic_dec(&data->disabled);
908 preempt_enable_no_resched_notrace();
910 preempt_enable_notrace();
913 static struct ftrace_ops trace_ops __read_mostly =
915 .func = function_trace_call,
918 void tracing_start_function_trace(void)
920 ftrace_function_enabled = 0;
921 register_ftrace_function(&trace_ops);
923 ftrace_function_enabled = 1;
926 void tracing_stop_function_trace(void)
928 ftrace_function_enabled = 0;
929 unregister_ftrace_function(&trace_ops);
933 enum trace_file_type {
934 TRACE_FILE_LAT_FMT = 1,
937 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
939 /* Don't allow ftrace to trace into the ring buffers */
940 ftrace_disable_cpu();
943 if (iter->buffer_iter[iter->cpu])
944 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
949 static struct trace_entry *
950 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
952 struct ring_buffer_event *event;
953 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
955 /* Don't allow ftrace to trace into the ring buffers */
956 ftrace_disable_cpu();
959 event = ring_buffer_iter_peek(buf_iter, ts);
961 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
965 return event ? ring_buffer_event_data(event) : NULL;
968 static struct trace_entry *
969 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
971 struct ring_buffer *buffer = iter->tr->buffer;
972 struct trace_entry *ent, *next = NULL;
977 for_each_tracing_cpu(cpu) {
979 if (ring_buffer_empty_cpu(buffer, cpu))
982 ent = peek_next_entry(iter, cpu, &ts);
985 * Pick the entry with the smallest timestamp:
987 if (ent && (!next || ts < next_ts)) {
1003 /* Find the next real entry, without updating the iterator itself */
1004 static struct trace_entry *
1005 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1007 return __find_next_entry(iter, ent_cpu, ent_ts);
1010 /* Find the next real entry, and increment the iterator to the next entry */
1011 static void *find_next_entry_inc(struct trace_iterator *iter)
1013 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1016 trace_iterator_increment(iter, iter->cpu);
1018 return iter->ent ? iter : NULL;
1021 static void trace_consume(struct trace_iterator *iter)
1023 /* Don't allow ftrace to trace into the ring buffers */
1024 ftrace_disable_cpu();
1025 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1026 ftrace_enable_cpu();
1029 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1031 struct trace_iterator *iter = m->private;
1037 /* can't go backwards */
1042 ent = find_next_entry_inc(iter);
1046 while (ent && iter->idx < i)
1047 ent = find_next_entry_inc(iter);
1054 static void *s_start(struct seq_file *m, loff_t *pos)
1056 struct trace_iterator *iter = m->private;
1061 mutex_lock(&trace_types_lock);
1063 if (!current_trace || current_trace != iter->trace) {
1064 mutex_unlock(&trace_types_lock);
1068 atomic_inc(&trace_record_cmdline_disabled);
1070 /* let the tracer grab locks here if needed */
1071 if (current_trace->start)
1072 current_trace->start(iter);
1074 if (*pos != iter->pos) {
1079 ftrace_disable_cpu();
1081 for_each_tracing_cpu(cpu) {
1082 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1085 ftrace_enable_cpu();
1087 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1092 p = s_next(m, p, &l);
1098 static void s_stop(struct seq_file *m, void *p)
1100 struct trace_iterator *iter = m->private;
1102 atomic_dec(&trace_record_cmdline_disabled);
1104 /* let the tracer release locks here if needed */
1105 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1106 iter->trace->stop(iter);
1108 mutex_unlock(&trace_types_lock);
1111 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1113 #ifdef CONFIG_KRETPROBES
1114 static inline int kretprobed(unsigned long addr)
1116 return addr == (unsigned long)kretprobe_trampoline;
1119 static inline int kretprobed(unsigned long addr)
1123 #endif /* CONFIG_KRETPROBES */
1126 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1128 #ifdef CONFIG_KALLSYMS
1129 char str[KSYM_SYMBOL_LEN];
1131 kallsyms_lookup(address, NULL, NULL, NULL, str);
1133 return trace_seq_printf(s, fmt, str);
1139 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1140 unsigned long address)
1142 #ifdef CONFIG_KALLSYMS
1143 char str[KSYM_SYMBOL_LEN];
1145 sprint_symbol(str, address);
1146 return trace_seq_printf(s, fmt, str);
1151 #ifndef CONFIG_64BIT
1152 # define IP_FMT "%08lx"
1154 # define IP_FMT "%016lx"
1158 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1163 return trace_seq_printf(s, "0");
1165 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1166 ret = seq_print_sym_offset(s, "%s", ip);
1168 ret = seq_print_sym_short(s, "%s", ip);
1173 if (sym_flags & TRACE_ITER_SYM_ADDR)
1174 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1178 static void print_lat_help_header(struct seq_file *m)
1180 seq_puts(m, "# _------=> CPU# \n");
1181 seq_puts(m, "# / _-----=> irqs-off \n");
1182 seq_puts(m, "# | / _----=> need-resched \n");
1183 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1184 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1185 seq_puts(m, "# |||| / \n");
1186 seq_puts(m, "# ||||| delay \n");
1187 seq_puts(m, "# cmd pid ||||| time | caller \n");
1188 seq_puts(m, "# \\ / ||||| \\ | / \n");
1191 static void print_func_help_header(struct seq_file *m)
1193 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1194 seq_puts(m, "# | | | | |\n");
1199 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1201 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1202 struct trace_array *tr = iter->tr;
1203 struct trace_array_cpu *data = tr->data[tr->cpu];
1204 struct tracer *type = current_trace;
1205 unsigned long total;
1206 unsigned long entries;
1207 const char *name = "preemption";
1212 entries = ring_buffer_entries(iter->tr->buffer);
1214 ring_buffer_overruns(iter->tr->buffer);
1216 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1218 seq_puts(m, "-----------------------------------"
1219 "---------------------------------\n");
1220 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1221 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1222 nsecs_to_usecs(data->saved_latency),
1226 #if defined(CONFIG_PREEMPT_NONE)
1228 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1230 #elif defined(CONFIG_PREEMPT)
1235 /* These are reserved for later use */
1238 seq_printf(m, " #P:%d)\n", num_online_cpus());
1242 seq_puts(m, " -----------------\n");
1243 seq_printf(m, " | task: %.16s-%d "
1244 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1245 data->comm, data->pid, data->uid, data->nice,
1246 data->policy, data->rt_priority);
1247 seq_puts(m, " -----------------\n");
1249 if (data->critical_start) {
1250 seq_puts(m, " => started at: ");
1251 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1252 trace_print_seq(m, &iter->seq);
1253 seq_puts(m, "\n => ended at: ");
1254 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1255 trace_print_seq(m, &iter->seq);
1263 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1265 int hardirq, softirq;
1268 comm = trace_find_cmdline(entry->pid);
1270 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1271 trace_seq_printf(s, "%3d", cpu);
1272 trace_seq_printf(s, "%c%c",
1273 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1274 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1275 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1277 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1278 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1279 if (hardirq && softirq) {
1280 trace_seq_putc(s, 'H');
1283 trace_seq_putc(s, 'h');
1286 trace_seq_putc(s, 's');
1288 trace_seq_putc(s, '.');
1292 if (entry->preempt_count)
1293 trace_seq_printf(s, "%x", entry->preempt_count);
1295 trace_seq_puts(s, ".");
1298 unsigned long preempt_mark_thresh = 100;
1301 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1302 unsigned long rel_usecs)
1304 trace_seq_printf(s, " %4lldus", abs_usecs);
1305 if (rel_usecs > preempt_mark_thresh)
1306 trace_seq_puts(s, "!: ");
1307 else if (rel_usecs > 1)
1308 trace_seq_puts(s, "+: ");
1310 trace_seq_puts(s, " : ");
1313 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1316 * The message is supposed to contain an ending newline.
1317 * If the printing stops prematurely, try to add a newline of our own.
1319 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1321 struct trace_entry *ent;
1322 struct trace_field_cont *cont;
1325 ent = peek_next_entry(iter, iter->cpu, NULL);
1326 if (!ent || ent->type != TRACE_CONT) {
1327 trace_seq_putc(s, '\n');
1332 cont = (struct trace_field_cont *)ent;
1334 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1336 ftrace_disable_cpu();
1338 if (iter->buffer_iter[iter->cpu])
1339 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1341 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1343 ftrace_enable_cpu();
1345 ent = peek_next_entry(iter, iter->cpu, NULL);
1346 } while (ent && ent->type == TRACE_CONT);
1349 trace_seq_putc(s, '\n');
1352 static enum print_line_t
1353 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1355 struct trace_seq *s = &iter->seq;
1356 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1357 struct trace_entry *next_entry;
1358 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1359 struct trace_entry *entry = iter->ent;
1360 unsigned long abs_usecs;
1361 unsigned long rel_usecs;
1368 if (entry->type == TRACE_CONT)
1369 return TRACE_TYPE_HANDLED;
1371 next_entry = find_next_entry(iter, NULL, &next_ts);
1374 rel_usecs = ns2usecs(next_ts - iter->ts);
1375 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1378 comm = trace_find_cmdline(entry->pid);
1379 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1380 " %ld.%03ldms (+%ld.%03ldms): ",
1382 entry->pid, cpu, entry->flags,
1383 entry->preempt_count, trace_idx,
1386 abs_usecs % 1000, rel_usecs/1000,
1389 lat_print_generic(s, entry, cpu);
1390 lat_print_timestamp(s, abs_usecs, rel_usecs);
1392 switch (entry->type) {
1394 struct ftrace_entry *field;
1396 trace_assign_type(field, entry);
1398 seq_print_ip_sym(s, field->ip, sym_flags);
1399 trace_seq_puts(s, " (");
1400 if (kretprobed(field->parent_ip))
1401 trace_seq_puts(s, KRETPROBE_MSG);
1403 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1404 trace_seq_puts(s, ")\n");
1409 struct ctx_switch_entry *field;
1411 trace_assign_type(field, entry);
1413 T = field->next_state < sizeof(state_to_char) ?
1414 state_to_char[field->next_state] : 'X';
1416 state = field->prev_state ?
1417 __ffs(field->prev_state) + 1 : 0;
1418 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1419 comm = trace_find_cmdline(field->next_pid);
1420 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1423 S, entry->type == TRACE_CTX ? "==>" : " +",
1430 case TRACE_SPECIAL: {
1431 struct special_entry *field;
1433 trace_assign_type(field, entry);
1435 trace_seq_printf(s, "# %ld %ld %ld\n",
1442 struct stack_entry *field;
1444 trace_assign_type(field, entry);
1446 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1448 trace_seq_puts(s, " <= ");
1449 seq_print_ip_sym(s, field->caller[i], sym_flags);
1451 trace_seq_puts(s, "\n");
1455 struct print_entry *field;
1457 trace_assign_type(field, entry);
1459 seq_print_ip_sym(s, field->ip, sym_flags);
1460 trace_seq_printf(s, ": %s", field->buf);
1461 if (entry->flags & TRACE_FLAG_CONT)
1462 trace_seq_print_cont(s, iter);
1466 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1468 return TRACE_TYPE_HANDLED;
1471 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1473 struct trace_seq *s = &iter->seq;
1474 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1475 struct trace_entry *entry;
1476 unsigned long usec_rem;
1477 unsigned long long t;
1486 if (entry->type == TRACE_CONT)
1487 return TRACE_TYPE_HANDLED;
1489 comm = trace_find_cmdline(iter->ent->pid);
1491 t = ns2usecs(iter->ts);
1492 usec_rem = do_div(t, 1000000ULL);
1493 secs = (unsigned long)t;
1495 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1497 return TRACE_TYPE_PARTIAL_LINE;
1498 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1500 return TRACE_TYPE_PARTIAL_LINE;
1501 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1503 return TRACE_TYPE_PARTIAL_LINE;
1505 switch (entry->type) {
1507 struct ftrace_entry *field;
1509 trace_assign_type(field, entry);
1511 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1513 return TRACE_TYPE_PARTIAL_LINE;
1514 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1516 ret = trace_seq_printf(s, " <-");
1518 return TRACE_TYPE_PARTIAL_LINE;
1519 if (kretprobed(field->parent_ip))
1520 ret = trace_seq_puts(s, KRETPROBE_MSG);
1522 ret = seq_print_ip_sym(s,
1526 return TRACE_TYPE_PARTIAL_LINE;
1528 ret = trace_seq_printf(s, "\n");
1530 return TRACE_TYPE_PARTIAL_LINE;
1535 struct ctx_switch_entry *field;
1537 trace_assign_type(field, entry);
1539 S = field->prev_state < sizeof(state_to_char) ?
1540 state_to_char[field->prev_state] : 'X';
1541 T = field->next_state < sizeof(state_to_char) ?
1542 state_to_char[field->next_state] : 'X';
1543 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1547 entry->type == TRACE_CTX ? "==>" : " +",
1553 return TRACE_TYPE_PARTIAL_LINE;
1556 case TRACE_SPECIAL: {
1557 struct special_entry *field;
1559 trace_assign_type(field, entry);
1561 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1566 return TRACE_TYPE_PARTIAL_LINE;
1570 struct stack_entry *field;
1572 trace_assign_type(field, entry);
1574 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1576 ret = trace_seq_puts(s, " <= ");
1578 return TRACE_TYPE_PARTIAL_LINE;
1580 ret = seq_print_ip_sym(s, field->caller[i],
1583 return TRACE_TYPE_PARTIAL_LINE;
1585 ret = trace_seq_puts(s, "\n");
1587 return TRACE_TYPE_PARTIAL_LINE;
1591 struct print_entry *field;
1593 trace_assign_type(field, entry);
1595 seq_print_ip_sym(s, field->ip, sym_flags);
1596 trace_seq_printf(s, ": %s", field->buf);
1597 if (entry->flags & TRACE_FLAG_CONT)
1598 trace_seq_print_cont(s, iter);
1602 return TRACE_TYPE_HANDLED;
1605 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1607 struct trace_seq *s = &iter->seq;
1608 struct trace_entry *entry;
1614 if (entry->type == TRACE_CONT)
1615 return TRACE_TYPE_HANDLED;
1617 ret = trace_seq_printf(s, "%d %d %llu ",
1618 entry->pid, iter->cpu, iter->ts);
1620 return TRACE_TYPE_PARTIAL_LINE;
1622 switch (entry->type) {
1624 struct ftrace_entry *field;
1626 trace_assign_type(field, entry);
1628 ret = trace_seq_printf(s, "%x %x\n",
1632 return TRACE_TYPE_PARTIAL_LINE;
1637 struct ctx_switch_entry *field;
1639 trace_assign_type(field, entry);
1641 S = field->prev_state < sizeof(state_to_char) ?
1642 state_to_char[field->prev_state] : 'X';
1643 T = field->next_state < sizeof(state_to_char) ?
1644 state_to_char[field->next_state] : 'X';
1645 if (entry->type == TRACE_WAKE)
1647 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
1656 return TRACE_TYPE_PARTIAL_LINE;
1661 struct special_entry *field;
1663 trace_assign_type(field, entry);
1665 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1670 return TRACE_TYPE_PARTIAL_LINE;
1674 struct print_entry *field;
1676 trace_assign_type(field, entry);
1678 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
1679 if (entry->flags & TRACE_FLAG_CONT)
1680 trace_seq_print_cont(s, iter);
1684 return TRACE_TYPE_HANDLED;
1687 #define SEQ_PUT_FIELD_RET(s, x) \
1689 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1693 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1695 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
1696 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1700 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1702 struct trace_seq *s = &iter->seq;
1703 unsigned char newline = '\n';
1704 struct trace_entry *entry;
1709 if (entry->type == TRACE_CONT)
1710 return TRACE_TYPE_HANDLED;
1712 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1713 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1714 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1716 switch (entry->type) {
1718 struct ftrace_entry *field;
1720 trace_assign_type(field, entry);
1722 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1723 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
1728 struct ctx_switch_entry *field;
1730 trace_assign_type(field, entry);
1732 S = field->prev_state < sizeof(state_to_char) ?
1733 state_to_char[field->prev_state] : 'X';
1734 T = field->next_state < sizeof(state_to_char) ?
1735 state_to_char[field->next_state] : 'X';
1736 if (entry->type == TRACE_WAKE)
1738 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1739 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
1740 SEQ_PUT_HEX_FIELD_RET(s, S);
1741 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1742 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1743 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
1744 SEQ_PUT_HEX_FIELD_RET(s, T);
1749 struct special_entry *field;
1751 trace_assign_type(field, entry);
1753 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
1754 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
1755 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
1759 SEQ_PUT_FIELD_RET(s, newline);
1761 return TRACE_TYPE_HANDLED;
1764 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1766 struct trace_seq *s = &iter->seq;
1767 struct trace_entry *entry;
1771 if (entry->type == TRACE_CONT)
1772 return TRACE_TYPE_HANDLED;
1774 SEQ_PUT_FIELD_RET(s, entry->pid);
1775 SEQ_PUT_FIELD_RET(s, iter->cpu);
1776 SEQ_PUT_FIELD_RET(s, iter->ts);
1778 switch (entry->type) {
1780 struct ftrace_entry *field;
1782 trace_assign_type(field, entry);
1784 SEQ_PUT_FIELD_RET(s, field->ip);
1785 SEQ_PUT_FIELD_RET(s, field->parent_ip);
1789 struct ctx_switch_entry *field;
1791 trace_assign_type(field, entry);
1793 SEQ_PUT_FIELD_RET(s, field->prev_pid);
1794 SEQ_PUT_FIELD_RET(s, field->prev_prio);
1795 SEQ_PUT_FIELD_RET(s, field->prev_state);
1796 SEQ_PUT_FIELD_RET(s, field->next_pid);
1797 SEQ_PUT_FIELD_RET(s, field->next_prio);
1798 SEQ_PUT_FIELD_RET(s, field->next_state);
1803 struct special_entry *field;
1805 trace_assign_type(field, entry);
1807 SEQ_PUT_FIELD_RET(s, field->arg1);
1808 SEQ_PUT_FIELD_RET(s, field->arg2);
1809 SEQ_PUT_FIELD_RET(s, field->arg3);
1816 static int trace_empty(struct trace_iterator *iter)
1820 for_each_tracing_cpu(cpu) {
1821 if (iter->buffer_iter[cpu]) {
1822 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1825 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1833 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1835 enum print_line_t ret;
1837 if (iter->trace && iter->trace->print_line) {
1838 ret = iter->trace->print_line(iter);
1839 if (ret != TRACE_TYPE_UNHANDLED)
1843 if (trace_flags & TRACE_ITER_BIN)
1844 return print_bin_fmt(iter);
1846 if (trace_flags & TRACE_ITER_HEX)
1847 return print_hex_fmt(iter);
1849 if (trace_flags & TRACE_ITER_RAW)
1850 return print_raw_fmt(iter);
1852 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1853 return print_lat_fmt(iter, iter->idx, iter->cpu);
1855 return print_trace_fmt(iter);
1858 static int s_show(struct seq_file *m, void *v)
1860 struct trace_iterator *iter = v;
1862 if (iter->ent == NULL) {
1864 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1867 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1868 /* print nothing if the buffers are empty */
1869 if (trace_empty(iter))
1871 print_trace_header(m, iter);
1872 if (!(trace_flags & TRACE_ITER_VERBOSE))
1873 print_lat_help_header(m);
1875 if (!(trace_flags & TRACE_ITER_VERBOSE))
1876 print_func_help_header(m);
1879 print_trace_line(iter);
1880 trace_print_seq(m, &iter->seq);
1886 static struct seq_operations tracer_seq_ops = {
1893 static struct trace_iterator *
1894 __tracing_open(struct inode *inode, struct file *file, int *ret)
1896 struct trace_iterator *iter;
1900 if (tracing_disabled) {
1905 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1911 mutex_lock(&trace_types_lock);
1912 if (current_trace && current_trace->print_max)
1915 iter->tr = inode->i_private;
1916 iter->trace = current_trace;
1919 for_each_tracing_cpu(cpu) {
1921 iter->buffer_iter[cpu] =
1922 ring_buffer_read_start(iter->tr->buffer, cpu);
1924 if (!iter->buffer_iter[cpu])
1928 /* TODO stop tracer */
1929 *ret = seq_open(file, &tracer_seq_ops);
1933 m = file->private_data;
1936 /* stop the trace while dumping */
1937 if (iter->tr->ctrl) {
1939 ftrace_function_enabled = 0;
1942 if (iter->trace && iter->trace->open)
1943 iter->trace->open(iter);
1945 mutex_unlock(&trace_types_lock);
1951 for_each_tracing_cpu(cpu) {
1952 if (iter->buffer_iter[cpu])
1953 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1955 mutex_unlock(&trace_types_lock);
1957 return ERR_PTR(-ENOMEM);
1960 int tracing_open_generic(struct inode *inode, struct file *filp)
1962 if (tracing_disabled)
1965 filp->private_data = inode->i_private;
1969 int tracing_release(struct inode *inode, struct file *file)
1971 struct seq_file *m = (struct seq_file *)file->private_data;
1972 struct trace_iterator *iter = m->private;
1975 mutex_lock(&trace_types_lock);
1976 for_each_tracing_cpu(cpu) {
1977 if (iter->buffer_iter[cpu])
1978 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1981 if (iter->trace && iter->trace->close)
1982 iter->trace->close(iter);
1984 /* reenable tracing if it was previously enabled */
1985 if (iter->tr->ctrl) {
1988 * It is safe to enable function tracing even if it
1991 ftrace_function_enabled = 1;
1993 mutex_unlock(&trace_types_lock);
1995 seq_release(inode, file);
2000 static int tracing_open(struct inode *inode, struct file *file)
2004 __tracing_open(inode, file, &ret);
2009 static int tracing_lt_open(struct inode *inode, struct file *file)
2011 struct trace_iterator *iter;
2014 iter = __tracing_open(inode, file, &ret);
2017 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2024 t_next(struct seq_file *m, void *v, loff_t *pos)
2026 struct tracer *t = m->private;
2038 static void *t_start(struct seq_file *m, loff_t *pos)
2040 struct tracer *t = m->private;
2043 mutex_lock(&trace_types_lock);
2044 for (; t && l < *pos; t = t_next(m, t, &l))
2050 static void t_stop(struct seq_file *m, void *p)
2052 mutex_unlock(&trace_types_lock);
2055 static int t_show(struct seq_file *m, void *v)
2057 struct tracer *t = v;
2062 seq_printf(m, "%s", t->name);
2071 static struct seq_operations show_traces_seq_ops = {
2078 static int show_traces_open(struct inode *inode, struct file *file)
2082 if (tracing_disabled)
2085 ret = seq_open(file, &show_traces_seq_ops);
2087 struct seq_file *m = file->private_data;
2088 m->private = trace_types;
2094 static struct file_operations tracing_fops = {
2095 .open = tracing_open,
2097 .llseek = seq_lseek,
2098 .release = tracing_release,
2101 static struct file_operations tracing_lt_fops = {
2102 .open = tracing_lt_open,
2104 .llseek = seq_lseek,
2105 .release = tracing_release,
2108 static struct file_operations show_traces_fops = {
2109 .open = show_traces_open,
2111 .release = seq_release,
2115 * Only trace on a CPU if the bitmask is set:
2117 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2120 * When tracing/tracing_cpu_mask is modified then this holds
2121 * the new bitmask we are about to install:
2123 static cpumask_t tracing_cpumask_new;
2126 * The tracer itself will not take this lock, but still we want
2127 * to provide a consistent cpumask to user-space:
2129 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2132 * Temporary storage for the character representation of the
2133 * CPU bitmask (and one more byte for the newline):
2135 static char mask_str[NR_CPUS + 1];
2138 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2139 size_t count, loff_t *ppos)
2143 mutex_lock(&tracing_cpumask_update_lock);
2145 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2146 if (count - len < 2) {
2150 len += sprintf(mask_str + len, "\n");
2151 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2154 mutex_unlock(&tracing_cpumask_update_lock);
2160 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2161 size_t count, loff_t *ppos)
2165 mutex_lock(&tracing_cpumask_update_lock);
2166 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2170 raw_local_irq_disable();
2171 __raw_spin_lock(&ftrace_max_lock);
2172 for_each_tracing_cpu(cpu) {
2174 * Increase/decrease the disabled counter if we are
2175 * about to flip a bit in the cpumask:
2177 if (cpu_isset(cpu, tracing_cpumask) &&
2178 !cpu_isset(cpu, tracing_cpumask_new)) {
2179 atomic_inc(&global_trace.data[cpu]->disabled);
2181 if (!cpu_isset(cpu, tracing_cpumask) &&
2182 cpu_isset(cpu, tracing_cpumask_new)) {
2183 atomic_dec(&global_trace.data[cpu]->disabled);
2186 __raw_spin_unlock(&ftrace_max_lock);
2187 raw_local_irq_enable();
2189 tracing_cpumask = tracing_cpumask_new;
2191 mutex_unlock(&tracing_cpumask_update_lock);
2196 mutex_unlock(&tracing_cpumask_update_lock);
2201 static struct file_operations tracing_cpumask_fops = {
2202 .open = tracing_open_generic,
2203 .read = tracing_cpumask_read,
2204 .write = tracing_cpumask_write,
2208 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2209 size_t cnt, loff_t *ppos)
2216 /* calulate max size */
2217 for (i = 0; trace_options[i]; i++) {
2218 len += strlen(trace_options[i]);
2219 len += 3; /* "no" and space */
2222 /* +2 for \n and \0 */
2223 buf = kmalloc(len + 2, GFP_KERNEL);
2227 for (i = 0; trace_options[i]; i++) {
2228 if (trace_flags & (1 << i))
2229 r += sprintf(buf + r, "%s ", trace_options[i]);
2231 r += sprintf(buf + r, "no%s ", trace_options[i]);
2234 r += sprintf(buf + r, "\n");
2235 WARN_ON(r >= len + 2);
2237 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2245 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2246 size_t cnt, loff_t *ppos)
2253 if (cnt >= sizeof(buf))
2256 if (copy_from_user(&buf, ubuf, cnt))
2261 if (strncmp(buf, "no", 2) == 0) {
2266 for (i = 0; trace_options[i]; i++) {
2267 int len = strlen(trace_options[i]);
2269 if (strncmp(cmp, trace_options[i], len) == 0) {
2271 trace_flags &= ~(1 << i);
2273 trace_flags |= (1 << i);
2278 * If no option could be set, return an error:
2280 if (!trace_options[i])
2288 static struct file_operations tracing_iter_fops = {
2289 .open = tracing_open_generic,
2290 .read = tracing_iter_ctrl_read,
2291 .write = tracing_iter_ctrl_write,
2294 static const char readme_msg[] =
2295 "tracing mini-HOWTO:\n\n"
2297 "# mount -t debugfs nodev /debug\n\n"
2298 "# cat /debug/tracing/available_tracers\n"
2299 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2300 "# cat /debug/tracing/current_tracer\n"
2302 "# echo sched_switch > /debug/tracing/current_tracer\n"
2303 "# cat /debug/tracing/current_tracer\n"
2305 "# cat /debug/tracing/iter_ctrl\n"
2306 "noprint-parent nosym-offset nosym-addr noverbose\n"
2307 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2308 "# echo 1 > /debug/tracing/tracing_enabled\n"
2309 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2310 "echo 0 > /debug/tracing/tracing_enabled\n"
2314 tracing_readme_read(struct file *filp, char __user *ubuf,
2315 size_t cnt, loff_t *ppos)
2317 return simple_read_from_buffer(ubuf, cnt, ppos,
2318 readme_msg, strlen(readme_msg));
2321 static struct file_operations tracing_readme_fops = {
2322 .open = tracing_open_generic,
2323 .read = tracing_readme_read,
2327 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2328 size_t cnt, loff_t *ppos)
2330 struct trace_array *tr = filp->private_data;
2334 r = sprintf(buf, "%ld\n", tr->ctrl);
2335 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2339 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2340 size_t cnt, loff_t *ppos)
2342 struct trace_array *tr = filp->private_data;
2347 if (cnt >= sizeof(buf))
2350 if (copy_from_user(&buf, ubuf, cnt))
2355 ret = strict_strtoul(buf, 10, &val);
2361 mutex_lock(&trace_types_lock);
2362 if (tr->ctrl ^ val) {
2370 if (current_trace && current_trace->ctrl_update)
2371 current_trace->ctrl_update(tr);
2373 mutex_unlock(&trace_types_lock);
2381 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2382 size_t cnt, loff_t *ppos)
2384 char buf[max_tracer_type_len+2];
2387 mutex_lock(&trace_types_lock);
2389 r = sprintf(buf, "%s\n", current_trace->name);
2391 r = sprintf(buf, "\n");
2392 mutex_unlock(&trace_types_lock);
2394 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2398 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2399 size_t cnt, loff_t *ppos)
2401 struct trace_array *tr = &global_trace;
2403 char buf[max_tracer_type_len+1];
2409 if (cnt > max_tracer_type_len)
2410 cnt = max_tracer_type_len;
2412 if (copy_from_user(&buf, ubuf, cnt))
2417 /* strip ending whitespace. */
2418 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2421 mutex_lock(&trace_types_lock);
2422 for (t = trace_types; t; t = t->next) {
2423 if (strcmp(t->name, buf) == 0)
2430 if (t == current_trace)
2433 if (current_trace && current_trace->reset)
2434 current_trace->reset(tr);
2441 mutex_unlock(&trace_types_lock);
2450 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2451 size_t cnt, loff_t *ppos)
2453 unsigned long *ptr = filp->private_data;
2457 r = snprintf(buf, sizeof(buf), "%ld\n",
2458 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2459 if (r > sizeof(buf))
2461 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2465 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2466 size_t cnt, loff_t *ppos)
2468 long *ptr = filp->private_data;
2473 if (cnt >= sizeof(buf))
2476 if (copy_from_user(&buf, ubuf, cnt))
2481 ret = strict_strtoul(buf, 10, &val);
2490 static atomic_t tracing_reader;
2492 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2494 struct trace_iterator *iter;
2496 if (tracing_disabled)
2499 /* We only allow for reader of the pipe */
2500 if (atomic_inc_return(&tracing_reader) != 1) {
2501 atomic_dec(&tracing_reader);
2505 /* create a buffer to store the information to pass to userspace */
2506 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2510 mutex_lock(&trace_types_lock);
2511 iter->tr = &global_trace;
2512 iter->trace = current_trace;
2513 filp->private_data = iter;
2515 if (iter->trace->pipe_open)
2516 iter->trace->pipe_open(iter);
2517 mutex_unlock(&trace_types_lock);
2522 static int tracing_release_pipe(struct inode *inode, struct file *file)
2524 struct trace_iterator *iter = file->private_data;
2527 atomic_dec(&tracing_reader);
2533 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2535 struct trace_iterator *iter = filp->private_data;
2537 if (trace_flags & TRACE_ITER_BLOCK) {
2539 * Always select as readable when in blocking mode
2541 return POLLIN | POLLRDNORM;
2543 if (!trace_empty(iter))
2544 return POLLIN | POLLRDNORM;
2545 poll_wait(filp, &trace_wait, poll_table);
2546 if (!trace_empty(iter))
2547 return POLLIN | POLLRDNORM;
2557 tracing_read_pipe(struct file *filp, char __user *ubuf,
2558 size_t cnt, loff_t *ppos)
2560 struct trace_iterator *iter = filp->private_data;
2563 /* return any leftover data */
2564 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2568 trace_seq_reset(&iter->seq);
2570 mutex_lock(&trace_types_lock);
2571 if (iter->trace->read) {
2572 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2579 while (trace_empty(iter)) {
2581 if ((filp->f_flags & O_NONBLOCK)) {
2587 * This is a make-shift waitqueue. The reason we don't use
2588 * an actual wait queue is because:
2589 * 1) we only ever have one waiter
2590 * 2) the tracing, traces all functions, we don't want
2591 * the overhead of calling wake_up and friends
2592 * (and tracing them too)
2593 * Anyway, this is really very primitive wakeup.
2595 set_current_state(TASK_INTERRUPTIBLE);
2596 iter->tr->waiter = current;
2598 mutex_unlock(&trace_types_lock);
2600 /* sleep for 100 msecs, and try again. */
2601 schedule_timeout(HZ/10);
2603 mutex_lock(&trace_types_lock);
2605 iter->tr->waiter = NULL;
2607 if (signal_pending(current)) {
2612 if (iter->trace != current_trace)
2616 * We block until we read something and tracing is disabled.
2617 * We still block if tracing is disabled, but we have never
2618 * read anything. This allows a user to cat this file, and
2619 * then enable tracing. But after we have read something,
2620 * we give an EOF when tracing is again disabled.
2622 * iter->pos will be 0 if we haven't read anything.
2624 if (!tracer_enabled && iter->pos)
2630 /* stop when tracing is finished */
2631 if (trace_empty(iter))
2634 if (cnt >= PAGE_SIZE)
2635 cnt = PAGE_SIZE - 1;
2637 /* reset all but tr, trace, and overruns */
2638 memset(&iter->seq, 0,
2639 sizeof(struct trace_iterator) -
2640 offsetof(struct trace_iterator, seq));
2643 while (find_next_entry_inc(iter) != NULL) {
2644 enum print_line_t ret;
2645 int len = iter->seq.len;
2647 ret = print_trace_line(iter);
2648 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2649 /* don't print partial lines */
2650 iter->seq.len = len;
2654 trace_consume(iter);
2656 if (iter->seq.len >= cnt)
2660 /* Now copy what we have to the user */
2661 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2662 if (iter->seq.readpos >= iter->seq.len)
2663 trace_seq_reset(&iter->seq);
2666 * If there was nothing to send to user, inspite of consuming trace
2667 * entries, go back to wait for more entries.
2673 mutex_unlock(&trace_types_lock);
2679 tracing_entries_read(struct file *filp, char __user *ubuf,
2680 size_t cnt, loff_t *ppos)
2682 struct trace_array *tr = filp->private_data;
2686 r = sprintf(buf, "%lu\n", tr->entries);
2687 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2691 tracing_entries_write(struct file *filp, const char __user *ubuf,
2692 size_t cnt, loff_t *ppos)
2697 struct trace_array *tr = filp->private_data;
2699 if (cnt >= sizeof(buf))
2702 if (copy_from_user(&buf, ubuf, cnt))
2707 ret = strict_strtoul(buf, 10, &val);
2711 /* must have at least 1 entry */
2715 mutex_lock(&trace_types_lock);
2719 pr_info("ftrace: please disable tracing"
2720 " before modifying buffer size\n");
2724 if (val != global_trace.entries) {
2725 ret = ring_buffer_resize(global_trace.buffer, val);
2731 ret = ring_buffer_resize(max_tr.buffer, val);
2735 r = ring_buffer_resize(global_trace.buffer,
2736 global_trace.entries);
2738 /* AARGH! We are left with different
2739 * size max buffer!!!! */
2741 tracing_disabled = 1;
2746 global_trace.entries = val;
2751 /* If check pages failed, return ENOMEM */
2752 if (tracing_disabled)
2755 max_tr.entries = global_trace.entries;
2756 mutex_unlock(&trace_types_lock);
2761 static int mark_printk(const char *fmt, ...)
2765 va_start(args, fmt);
2766 ret = trace_vprintk(0, fmt, args);
2772 tracing_mark_write(struct file *filp, const char __user *ubuf,
2773 size_t cnt, loff_t *fpos)
2777 struct trace_array *tr = &global_trace;
2779 if (!tr->ctrl || tracing_disabled)
2782 if (cnt > TRACE_BUF_SIZE)
2783 cnt = TRACE_BUF_SIZE;
2785 buf = kmalloc(cnt + 1, GFP_KERNEL);
2789 if (copy_from_user(buf, ubuf, cnt)) {
2794 /* Cut from the first nil or newline. */
2796 end = strchr(buf, '\n');
2800 cnt = mark_printk("%s\n", buf);
2807 static struct file_operations tracing_max_lat_fops = {
2808 .open = tracing_open_generic,
2809 .read = tracing_max_lat_read,
2810 .write = tracing_max_lat_write,
2813 static struct file_operations tracing_ctrl_fops = {
2814 .open = tracing_open_generic,
2815 .read = tracing_ctrl_read,
2816 .write = tracing_ctrl_write,
2819 static struct file_operations set_tracer_fops = {
2820 .open = tracing_open_generic,
2821 .read = tracing_set_trace_read,
2822 .write = tracing_set_trace_write,
2825 static struct file_operations tracing_pipe_fops = {
2826 .open = tracing_open_pipe,
2827 .poll = tracing_poll_pipe,
2828 .read = tracing_read_pipe,
2829 .release = tracing_release_pipe,
2832 static struct file_operations tracing_entries_fops = {
2833 .open = tracing_open_generic,
2834 .read = tracing_entries_read,
2835 .write = tracing_entries_write,
2838 static struct file_operations tracing_mark_fops = {
2839 .open = tracing_open_generic,
2840 .write = tracing_mark_write,
2843 #ifdef CONFIG_DYNAMIC_FTRACE
2846 tracing_read_long(struct file *filp, char __user *ubuf,
2847 size_t cnt, loff_t *ppos)
2849 unsigned long *p = filp->private_data;
2853 r = sprintf(buf, "%ld\n", *p);
2855 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2858 static struct file_operations tracing_read_long_fops = {
2859 .open = tracing_open_generic,
2860 .read = tracing_read_long,
2864 static struct dentry *d_tracer;
2866 struct dentry *tracing_init_dentry(void)
2873 d_tracer = debugfs_create_dir("tracing", NULL);
2875 if (!d_tracer && !once) {
2877 pr_warning("Could not create debugfs directory 'tracing'\n");
2884 #ifdef CONFIG_FTRACE_SELFTEST
2885 /* Let selftest have access to static functions in this file */
2886 #include "trace_selftest.c"
2889 static __init int tracer_init_debugfs(void)
2891 struct dentry *d_tracer;
2892 struct dentry *entry;
2894 d_tracer = tracing_init_dentry();
2896 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2897 &global_trace, &tracing_ctrl_fops);
2899 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2901 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2902 NULL, &tracing_iter_fops);
2904 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2906 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2907 NULL, &tracing_cpumask_fops);
2909 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2911 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2912 &global_trace, &tracing_lt_fops);
2914 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2916 entry = debugfs_create_file("trace", 0444, d_tracer,
2917 &global_trace, &tracing_fops);
2919 pr_warning("Could not create debugfs 'trace' entry\n");
2921 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2922 &global_trace, &show_traces_fops);
2924 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2926 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2927 &global_trace, &set_tracer_fops);
2929 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2931 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2932 &tracing_max_latency,
2933 &tracing_max_lat_fops);
2935 pr_warning("Could not create debugfs "
2936 "'tracing_max_latency' entry\n");
2938 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2939 &tracing_thresh, &tracing_max_lat_fops);
2941 pr_warning("Could not create debugfs "
2942 "'tracing_thresh' entry\n");
2943 entry = debugfs_create_file("README", 0644, d_tracer,
2944 NULL, &tracing_readme_fops);
2946 pr_warning("Could not create debugfs 'README' entry\n");
2948 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2949 NULL, &tracing_pipe_fops);
2951 pr_warning("Could not create debugfs "
2952 "'trace_pipe' entry\n");
2954 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2955 &global_trace, &tracing_entries_fops);
2957 pr_warning("Could not create debugfs "
2958 "'trace_entries' entry\n");
2960 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2961 NULL, &tracing_mark_fops);
2963 pr_warning("Could not create debugfs "
2964 "'trace_marker' entry\n");
2966 #ifdef CONFIG_DYNAMIC_FTRACE
2967 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2968 &ftrace_update_tot_cnt,
2969 &tracing_read_long_fops);
2971 pr_warning("Could not create debugfs "
2972 "'dyn_ftrace_total_info' entry\n");
2974 #ifdef CONFIG_SYSPROF_TRACER
2975 init_tracer_sysprof_debugfs(d_tracer);
2980 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2982 static DEFINE_SPINLOCK(trace_buf_lock);
2983 static char trace_buf[TRACE_BUF_SIZE];
2985 struct ring_buffer_event *event;
2986 struct trace_array *tr = &global_trace;
2987 struct trace_array_cpu *data;
2988 struct print_entry *entry;
2989 unsigned long flags, irq_flags;
2990 int cpu, len = 0, size, pc;
2992 if (!tr->ctrl || tracing_disabled)
2995 pc = preempt_count();
2996 preempt_disable_notrace();
2997 cpu = raw_smp_processor_id();
2998 data = tr->data[cpu];
3000 if (unlikely(atomic_read(&data->disabled)))
3003 spin_lock_irqsave(&trace_buf_lock, flags);
3004 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3006 len = min(len, TRACE_BUF_SIZE-1);
3009 size = sizeof(*entry) + len + 1;
3010 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3013 entry = ring_buffer_event_data(event);
3014 tracing_generic_entry_update(&entry->ent, flags, pc);
3015 entry->ent.type = TRACE_PRINT;
3018 memcpy(&entry->buf, trace_buf, len);
3019 entry->buf[len] = 0;
3020 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3023 spin_unlock_irqrestore(&trace_buf_lock, flags);
3026 preempt_enable_notrace();
3030 EXPORT_SYMBOL_GPL(trace_vprintk);
3032 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3037 if (!(trace_flags & TRACE_ITER_PRINTK))
3041 ret = trace_vprintk(ip, fmt, ap);
3045 EXPORT_SYMBOL_GPL(__ftrace_printk);
3047 static int trace_panic_handler(struct notifier_block *this,
3048 unsigned long event, void *unused)
3050 if (ftrace_dump_on_oops)
3055 static struct notifier_block trace_panic_notifier = {
3056 .notifier_call = trace_panic_handler,
3058 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3061 static int trace_die_handler(struct notifier_block *self,
3067 if (ftrace_dump_on_oops)
3076 static struct notifier_block trace_die_notifier = {
3077 .notifier_call = trace_die_handler,
3082 * printk is set to max of 1024, we really don't need it that big.
3083 * Nothing should be printing 1000 characters anyway.
3085 #define TRACE_MAX_PRINT 1000
3088 * Define here KERN_TRACE so that we have one place to modify
3089 * it if we decide to change what log level the ftrace dump
3092 #define KERN_TRACE KERN_INFO
3095 trace_printk_seq(struct trace_seq *s)
3097 /* Probably should print a warning here. */
3101 /* should be zero ended, but we are paranoid. */
3102 s->buffer[s->len] = 0;
3104 printk(KERN_TRACE "%s", s->buffer);
3109 void ftrace_dump(void)
3111 static DEFINE_SPINLOCK(ftrace_dump_lock);
3112 /* use static because iter can be a bit big for the stack */
3113 static struct trace_iterator iter;
3114 static cpumask_t mask;
3115 static int dump_ran;
3116 unsigned long flags;
3120 spin_lock_irqsave(&ftrace_dump_lock, flags);
3126 /* No turning back! */
3129 for_each_tracing_cpu(cpu) {
3130 atomic_inc(&global_trace.data[cpu]->disabled);
3133 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3135 iter.tr = &global_trace;
3136 iter.trace = current_trace;
3139 * We need to stop all tracing on all CPUS to read the
3140 * the next buffer. This is a bit expensive, but is
3141 * not done often. We fill all what we can read,
3142 * and then release the locks again.
3147 while (!trace_empty(&iter)) {
3150 printk(KERN_TRACE "---------------------------------\n");
3154 /* reset all but tr, trace, and overruns */
3155 memset(&iter.seq, 0,
3156 sizeof(struct trace_iterator) -
3157 offsetof(struct trace_iterator, seq));
3158 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3161 if (find_next_entry_inc(&iter) != NULL) {
3162 print_trace_line(&iter);
3163 trace_consume(&iter);
3166 trace_printk_seq(&iter.seq);
3170 printk(KERN_TRACE " (ftrace buffer empty)\n");
3172 printk(KERN_TRACE "---------------------------------\n");
3175 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3178 __init static int tracer_alloc_buffers(void)
3180 struct trace_array_cpu *data;
3183 /* TODO: make the number of buffers hot pluggable with CPUS */
3184 tracing_buffer_mask = cpu_possible_map;
3186 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3187 TRACE_BUFFER_FLAGS);
3188 if (!global_trace.buffer) {
3189 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3193 global_trace.entries = ring_buffer_size(global_trace.buffer);
3195 #ifdef CONFIG_TRACER_MAX_TRACE
3196 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3197 TRACE_BUFFER_FLAGS);
3198 if (!max_tr.buffer) {
3199 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3201 ring_buffer_free(global_trace.buffer);
3204 max_tr.entries = ring_buffer_size(max_tr.buffer);
3205 WARN_ON(max_tr.entries != global_trace.entries);
3208 /* Allocate the first page for all buffers */
3209 for_each_tracing_cpu(i) {
3210 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3211 max_tr.data[i] = &per_cpu(max_data, i);
3214 trace_init_cmdlines();
3216 register_tracer(&nop_trace);
3217 #ifdef CONFIG_BOOT_TRACER
3218 register_tracer(&boot_tracer);
3219 current_trace = &boot_tracer;
3220 current_trace->init(&global_trace);
3222 current_trace = &nop_trace;
3225 /* All seems OK, enable tracing */
3226 global_trace.ctrl = tracer_enabled;
3227 tracing_disabled = 0;
3229 atomic_notifier_chain_register(&panic_notifier_list,
3230 &trace_panic_notifier);
3232 register_die_notifier(&trace_die_notifier);
3236 early_initcall(tracer_alloc_buffers);
3237 fs_initcall(tracer_init_debugfs);