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/ring_buffer.h>
15 #include <linux/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/ctype.h>
35 #include <linux/init.h>
36 #include <linux/poll.h>
37 #include <linux/gfp.h>
41 #include "trace_output.h"
43 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
45 unsigned long __read_mostly tracing_max_latency;
46 unsigned long __read_mostly tracing_thresh;
49 * On boot up, the ring buffer is set to the minimum size, so that
50 * we do not waste memory on systems that are not using tracing.
52 static int ring_buffer_expanded;
55 * We need to change this state when a selftest is running.
56 * A selftest will lurk into the ring-buffer to count the
57 * entries inserted during the selftest although some concurrent
58 * insertions into the ring-buffer such as trace_printk could occurred
59 * at the same time, giving false positive or negative results.
61 static bool __read_mostly tracing_selftest_running;
64 * If a tracer is running, we do not want to run SELFTEST.
66 static bool __read_mostly tracing_selftest_disabled;
68 /* For tracers that don't implement custom flags */
69 static struct tracer_opt dummy_tracer_opt[] = {
73 static struct tracer_flags dummy_tracer_flags = {
75 .opts = dummy_tracer_opt
78 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
84 * Kill all tracing for good (never come back).
85 * It is initialized to 1 but will turn to zero if the initialization
86 * of the tracer is successful. But that is the only place that sets
89 static int tracing_disabled = 1;
91 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
93 static inline void ftrace_disable_cpu(void)
96 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
99 static inline void ftrace_enable_cpu(void)
101 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
105 static cpumask_var_t __read_mostly tracing_buffer_mask;
107 /* Define which cpu buffers are currently read in trace_pipe */
108 static cpumask_var_t tracing_reader_cpumask;
110 #define for_each_tracing_cpu(cpu) \
111 for_each_cpu(cpu, tracing_buffer_mask)
114 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
116 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
117 * is set, then ftrace_dump is called. This will output the contents
118 * of the ftrace buffers to the console. This is very useful for
119 * capturing traces that lead to crashes and outputing it to a
122 * It is default off, but you can enable it with either specifying
123 * "ftrace_dump_on_oops" in the kernel command line, or setting
124 * /proc/sys/kernel/ftrace_dump_on_oops to true.
126 int ftrace_dump_on_oops;
128 static int tracing_set_tracer(const char *buf);
130 #define BOOTUP_TRACER_SIZE 100
131 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
132 static char *default_bootup_tracer;
134 static int __init set_ftrace(char *str)
136 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
137 default_bootup_tracer = bootup_tracer_buf;
138 /* We are using ftrace early, expand it */
139 ring_buffer_expanded = 1;
142 __setup("ftrace=", set_ftrace);
144 static int __init set_ftrace_dump_on_oops(char *str)
146 ftrace_dump_on_oops = 1;
149 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
151 unsigned long long ns2usecs(cycle_t nsec)
159 * The global_trace is the descriptor that holds the tracing
160 * buffers for the live tracing. For each CPU, it contains
161 * a link list of pages that will store trace entries. The
162 * page descriptor of the pages in the memory is used to hold
163 * the link list by linking the lru item in the page descriptor
164 * to each of the pages in the buffer per CPU.
166 * For each active CPU there is a data field that holds the
167 * pages for the buffer for that CPU. Each CPU has the same number
168 * of pages allocated for its buffer.
170 static struct trace_array global_trace;
172 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
174 cycle_t ftrace_now(int cpu)
178 /* Early boot up does not have a buffer yet */
179 if (!global_trace.buffer)
180 return trace_clock_local();
182 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
183 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
189 * The max_tr is used to snapshot the global_trace when a maximum
190 * latency is reached. Some tracers will use this to store a maximum
191 * trace while it continues examining live traces.
193 * The buffers for the max_tr are set up the same as the global_trace.
194 * When a snapshot is taken, the link list of the max_tr is swapped
195 * with the link list of the global_trace and the buffers are reset for
196 * the global_trace so the tracing can continue.
198 static struct trace_array max_tr;
200 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
202 /* tracer_enabled is used to toggle activation of a tracer */
203 static int tracer_enabled = 1;
206 * tracing_is_enabled - return tracer_enabled status
208 * This function is used by other tracers to know the status
209 * of the tracer_enabled flag. Tracers may use this function
210 * to know if it should enable their features when starting
211 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
213 int tracing_is_enabled(void)
215 return tracer_enabled;
219 * trace_buf_size is the size in bytes that is allocated
220 * for a buffer. Note, the number of bytes is always rounded
223 * This number is purposely set to a low number of 16384.
224 * If the dump on oops happens, it will be much appreciated
225 * to not have to wait for all that output. Anyway this can be
226 * boot time and run time configurable.
228 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
230 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
232 /* trace_types holds a link list of available tracers. */
233 static struct tracer *trace_types __read_mostly;
235 /* current_trace points to the tracer that is currently active */
236 static struct tracer *current_trace __read_mostly;
239 * max_tracer_type_len is used to simplify the allocating of
240 * buffers to read userspace tracer names. We keep track of
241 * the longest tracer name registered.
243 static int max_tracer_type_len;
246 * trace_types_lock is used to protect the trace_types list.
247 * This lock is also used to keep user access serialized.
248 * Accesses from userspace will grab this lock while userspace
249 * activities happen inside the kernel.
251 static DEFINE_MUTEX(trace_types_lock);
253 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
254 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
256 /* trace_flags holds trace_options default values */
257 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
258 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
259 TRACE_ITER_GRAPH_TIME;
262 * trace_wake_up - wake up tasks waiting for trace input
264 * Simply wakes up any task that is blocked on the trace_wait
265 * queue. These is used with trace_poll for tasks polling the trace.
267 void trace_wake_up(void)
270 * The runqueue_is_locked() can fail, but this is the best we
273 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
274 wake_up(&trace_wait);
277 static int __init set_buf_size(char *str)
279 unsigned long buf_size;
284 ret = strict_strtoul(str, 0, &buf_size);
285 /* nr_entries can not be zero */
286 if (ret < 0 || buf_size == 0)
288 trace_buf_size = buf_size;
291 __setup("trace_buf_size=", set_buf_size);
293 unsigned long nsecs_to_usecs(unsigned long nsecs)
298 /* These must match the bit postions in trace_iterator_flags */
299 static const char *trace_options[] = {
326 * ftrace_max_lock is used to protect the swapping of buffers
327 * when taking a max snapshot. The buffers themselves are
328 * protected by per_cpu spinlocks. But the action of the swap
329 * needs its own lock.
331 * This is defined as a raw_spinlock_t in order to help
332 * with performance when lockdep debugging is enabled.
334 static raw_spinlock_t ftrace_max_lock =
335 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
338 * Copy the new maximum trace into the separate maximum-trace
339 * structure. (this way the maximum trace is permanently saved,
340 * for later retrieval via /debugfs/tracing/latency_trace)
343 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
345 struct trace_array_cpu *data = tr->data[cpu];
348 max_tr.time_start = data->preempt_timestamp;
350 data = max_tr.data[cpu];
351 data->saved_latency = tracing_max_latency;
353 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
354 data->pid = tsk->pid;
355 data->uid = task_uid(tsk);
356 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
357 data->policy = tsk->policy;
358 data->rt_priority = tsk->rt_priority;
360 /* record this tasks comm */
361 tracing_record_cmdline(tsk);
364 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
372 if (s->len <= s->readpos)
375 len = s->len - s->readpos;
378 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
388 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
393 if (s->len <= s->readpos)
396 len = s->len - s->readpos;
399 ret = memcpy(buf, s->buffer + s->readpos, cnt);
408 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
410 * @tsk: the task with the latency
411 * @cpu: The cpu that initiated the trace.
413 * Flip the buffers between the @tr and the max_tr and record information
414 * about which task was the cause of this latency.
417 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
419 struct ring_buffer *buf = tr->buffer;
421 WARN_ON_ONCE(!irqs_disabled());
422 __raw_spin_lock(&ftrace_max_lock);
424 tr->buffer = max_tr.buffer;
427 ftrace_disable_cpu();
428 ring_buffer_reset(tr->buffer);
431 __update_max_tr(tr, tsk, cpu);
432 __raw_spin_unlock(&ftrace_max_lock);
436 * update_max_tr_single - only copy one trace over, and reset the rest
438 * @tsk - task with the latency
439 * @cpu - the cpu of the buffer to copy.
441 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
444 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
448 WARN_ON_ONCE(!irqs_disabled());
449 __raw_spin_lock(&ftrace_max_lock);
451 ftrace_disable_cpu();
453 ring_buffer_reset(max_tr.buffer);
454 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
458 WARN_ON_ONCE(ret && ret != -EAGAIN);
460 __update_max_tr(tr, tsk, cpu);
461 __raw_spin_unlock(&ftrace_max_lock);
465 * register_tracer - register a tracer with the ftrace system.
466 * @type - the plugin for the tracer
468 * Register a new plugin tracer.
470 int register_tracer(struct tracer *type)
471 __releases(kernel_lock)
472 __acquires(kernel_lock)
479 pr_info("Tracer must have a name\n");
484 * When this gets called we hold the BKL which means that
485 * preemption is disabled. Various trace selftests however
486 * need to disable and enable preemption for successful tests.
487 * So we drop the BKL here and grab it after the tests again.
490 mutex_lock(&trace_types_lock);
492 tracing_selftest_running = true;
494 for (t = trace_types; t; t = t->next) {
495 if (strcmp(type->name, t->name) == 0) {
497 pr_info("Trace %s already registered\n",
505 type->set_flag = &dummy_set_flag;
507 type->flags = &dummy_tracer_flags;
509 if (!type->flags->opts)
510 type->flags->opts = dummy_tracer_opt;
511 if (!type->wait_pipe)
512 type->wait_pipe = default_wait_pipe;
515 #ifdef CONFIG_FTRACE_STARTUP_TEST
516 if (type->selftest && !tracing_selftest_disabled) {
517 struct tracer *saved_tracer = current_trace;
518 struct trace_array *tr = &global_trace;
522 * Run a selftest on this tracer.
523 * Here we reset the trace buffer, and set the current
524 * tracer to be this tracer. The tracer can then run some
525 * internal tracing to verify that everything is in order.
526 * If we fail, we do not register this tracer.
528 for_each_tracing_cpu(i)
529 tracing_reset(tr, i);
531 current_trace = type;
532 /* the test is responsible for initializing and enabling */
533 pr_info("Testing tracer %s: ", type->name);
534 ret = type->selftest(type, tr);
535 /* the test is responsible for resetting too */
536 current_trace = saved_tracer;
538 printk(KERN_CONT "FAILED!\n");
541 /* Only reset on passing, to avoid touching corrupted buffers */
542 for_each_tracing_cpu(i)
543 tracing_reset(tr, i);
545 printk(KERN_CONT "PASSED\n");
549 type->next = trace_types;
551 len = strlen(type->name);
552 if (len > max_tracer_type_len)
553 max_tracer_type_len = len;
556 tracing_selftest_running = false;
557 mutex_unlock(&trace_types_lock);
559 if (ret || !default_bootup_tracer)
562 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
565 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
566 /* Do we want this tracer to start on bootup? */
567 tracing_set_tracer(type->name);
568 default_bootup_tracer = NULL;
569 /* disable other selftests, since this will break it. */
570 tracing_selftest_disabled = 1;
571 #ifdef CONFIG_FTRACE_STARTUP_TEST
572 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
581 void unregister_tracer(struct tracer *type)
586 mutex_lock(&trace_types_lock);
587 for (t = &trace_types; *t; t = &(*t)->next) {
591 pr_info("Trace %s not registered\n", type->name);
597 if (type == current_trace && tracer_enabled) {
600 if (current_trace->stop)
601 current_trace->stop(&global_trace);
602 current_trace = &nop_trace;
605 if (strlen(type->name) != max_tracer_type_len)
608 max_tracer_type_len = 0;
609 for (t = &trace_types; *t; t = &(*t)->next) {
610 len = strlen((*t)->name);
611 if (len > max_tracer_type_len)
612 max_tracer_type_len = len;
615 mutex_unlock(&trace_types_lock);
618 void tracing_reset(struct trace_array *tr, int cpu)
620 ftrace_disable_cpu();
621 ring_buffer_reset_cpu(tr->buffer, cpu);
625 void tracing_reset_online_cpus(struct trace_array *tr)
629 tr->time_start = ftrace_now(tr->cpu);
631 for_each_online_cpu(cpu)
632 tracing_reset(tr, cpu);
635 #define SAVED_CMDLINES 128
636 #define NO_CMDLINE_MAP UINT_MAX
637 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
638 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
639 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
640 static int cmdline_idx;
641 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
643 /* temporary disable recording */
644 static atomic_t trace_record_cmdline_disabled __read_mostly;
646 static void trace_init_cmdlines(void)
648 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
649 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
653 static int trace_stop_count;
654 static DEFINE_SPINLOCK(tracing_start_lock);
657 * ftrace_off_permanent - disable all ftrace code permanently
659 * This should only be called when a serious anomally has
660 * been detected. This will turn off the function tracing,
661 * ring buffers, and other tracing utilites. It takes no
662 * locks and can be called from any context.
664 void ftrace_off_permanent(void)
666 tracing_disabled = 1;
668 tracing_off_permanent();
672 * tracing_start - quick start of the tracer
674 * If tracing is enabled but was stopped by tracing_stop,
675 * this will start the tracer back up.
677 void tracing_start(void)
679 struct ring_buffer *buffer;
682 if (tracing_disabled)
685 spin_lock_irqsave(&tracing_start_lock, flags);
686 if (--trace_stop_count) {
687 if (trace_stop_count < 0) {
688 /* Someone screwed up their debugging */
690 trace_stop_count = 0;
696 buffer = global_trace.buffer;
698 ring_buffer_record_enable(buffer);
700 buffer = max_tr.buffer;
702 ring_buffer_record_enable(buffer);
706 spin_unlock_irqrestore(&tracing_start_lock, flags);
710 * tracing_stop - quick stop of the tracer
712 * Light weight way to stop tracing. Use in conjunction with
715 void tracing_stop(void)
717 struct ring_buffer *buffer;
721 spin_lock_irqsave(&tracing_start_lock, flags);
722 if (trace_stop_count++)
725 buffer = global_trace.buffer;
727 ring_buffer_record_disable(buffer);
729 buffer = max_tr.buffer;
731 ring_buffer_record_disable(buffer);
734 spin_unlock_irqrestore(&tracing_start_lock, flags);
737 void trace_stop_cmdline_recording(void);
739 static void trace_save_cmdline(struct task_struct *tsk)
743 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
747 * It's not the end of the world if we don't get
748 * the lock, but we also don't want to spin
749 * nor do we want to disable interrupts,
750 * so if we miss here, then better luck next time.
752 if (!__raw_spin_trylock(&trace_cmdline_lock))
755 idx = map_pid_to_cmdline[tsk->pid];
756 if (idx == NO_CMDLINE_MAP) {
757 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
760 * Check whether the cmdline buffer at idx has a pid
761 * mapped. We are going to overwrite that entry so we
762 * need to clear the map_pid_to_cmdline. Otherwise we
763 * would read the new comm for the old pid.
765 pid = map_cmdline_to_pid[idx];
766 if (pid != NO_CMDLINE_MAP)
767 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
769 map_cmdline_to_pid[idx] = tsk->pid;
770 map_pid_to_cmdline[tsk->pid] = idx;
775 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
777 __raw_spin_unlock(&trace_cmdline_lock);
780 void trace_find_cmdline(int pid, char comm[])
785 strcpy(comm, "<idle>");
789 if (pid > PID_MAX_DEFAULT) {
790 strcpy(comm, "<...>");
794 __raw_spin_lock(&trace_cmdline_lock);
795 map = map_pid_to_cmdline[pid];
796 if (map != NO_CMDLINE_MAP)
797 strcpy(comm, saved_cmdlines[map]);
799 strcpy(comm, "<...>");
801 __raw_spin_unlock(&trace_cmdline_lock);
804 void tracing_record_cmdline(struct task_struct *tsk)
806 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
810 trace_save_cmdline(tsk);
814 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
817 struct task_struct *tsk = current;
819 entry->preempt_count = pc & 0xff;
820 entry->pid = (tsk) ? tsk->pid : 0;
821 entry->tgid = (tsk) ? tsk->tgid : 0;
823 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
824 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
826 TRACE_FLAG_IRQS_NOSUPPORT |
828 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
829 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
830 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
833 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
836 unsigned long flags, int pc)
838 struct ring_buffer_event *event;
840 event = ring_buffer_lock_reserve(tr->buffer, len);
842 struct trace_entry *ent = ring_buffer_event_data(event);
844 tracing_generic_entry_update(ent, flags, pc);
850 static void ftrace_trace_stack(struct trace_array *tr,
851 unsigned long flags, int skip, int pc);
852 static void ftrace_trace_userstack(struct trace_array *tr,
853 unsigned long flags, int pc);
855 static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
856 struct ring_buffer_event *event,
857 unsigned long flags, int pc,
860 ring_buffer_unlock_commit(tr->buffer, event);
862 ftrace_trace_stack(tr, flags, 6, pc);
863 ftrace_trace_userstack(tr, flags, pc);
869 void trace_buffer_unlock_commit(struct trace_array *tr,
870 struct ring_buffer_event *event,
871 unsigned long flags, int pc)
873 __trace_buffer_unlock_commit(tr, event, flags, pc, 1);
876 struct ring_buffer_event *
877 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
878 unsigned long flags, int pc)
880 return trace_buffer_lock_reserve(&global_trace,
881 type, len, flags, pc);
884 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
885 unsigned long flags, int pc)
887 return __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
890 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
891 unsigned long flags, int pc)
893 return __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
897 trace_function(struct trace_array *tr,
898 unsigned long ip, unsigned long parent_ip, unsigned long flags,
901 struct ftrace_event_call *call = &event_function;
902 struct ring_buffer_event *event;
903 struct ftrace_entry *entry;
905 /* If we are reading the ring buffer, don't trace */
906 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
909 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
913 entry = ring_buffer_event_data(event);
915 entry->parent_ip = parent_ip;
917 filter_check_discard(call, entry, event);
919 ring_buffer_unlock_commit(tr->buffer, event);
922 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
923 static int __trace_graph_entry(struct trace_array *tr,
924 struct ftrace_graph_ent *trace,
928 struct ftrace_event_call *call = &event_funcgraph_entry;
929 struct ring_buffer_event *event;
930 struct ftrace_graph_ent_entry *entry;
932 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
935 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
936 sizeof(*entry), flags, pc);
939 entry = ring_buffer_event_data(event);
940 entry->graph_ent = *trace;
941 filter_check_discard(call, entry, event);
942 ring_buffer_unlock_commit(global_trace.buffer, event);
947 static void __trace_graph_return(struct trace_array *tr,
948 struct ftrace_graph_ret *trace,
952 struct ftrace_event_call *call = &event_funcgraph_exit;
953 struct ring_buffer_event *event;
954 struct ftrace_graph_ret_entry *entry;
956 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
959 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
960 sizeof(*entry), flags, pc);
963 entry = ring_buffer_event_data(event);
965 filter_check_discard(call, entry, event);
966 ring_buffer_unlock_commit(global_trace.buffer, event);
971 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
972 unsigned long ip, unsigned long parent_ip, unsigned long flags,
975 if (likely(!atomic_read(&data->disabled)))
976 trace_function(tr, ip, parent_ip, flags, pc);
979 static void __ftrace_trace_stack(struct trace_array *tr,
983 #ifdef CONFIG_STACKTRACE
984 struct ftrace_event_call *call = &event_kernel_stack;
985 struct ring_buffer_event *event;
986 struct stack_entry *entry;
987 struct stack_trace trace;
989 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
990 sizeof(*entry), flags, pc);
993 entry = ring_buffer_event_data(event);
994 memset(&entry->caller, 0, sizeof(entry->caller));
996 trace.nr_entries = 0;
997 trace.max_entries = FTRACE_STACK_ENTRIES;
999 trace.entries = entry->caller;
1001 save_stack_trace(&trace);
1002 filter_check_discard(call, entry, event);
1003 ring_buffer_unlock_commit(tr->buffer, event);
1007 static void ftrace_trace_stack(struct trace_array *tr,
1008 unsigned long flags,
1011 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1014 __ftrace_trace_stack(tr, flags, skip, pc);
1017 void __trace_stack(struct trace_array *tr,
1018 unsigned long flags,
1021 __ftrace_trace_stack(tr, flags, skip, pc);
1024 static void ftrace_trace_userstack(struct trace_array *tr,
1025 unsigned long flags, int pc)
1027 #ifdef CONFIG_STACKTRACE
1028 struct ftrace_event_call *call = &event_user_stack;
1029 struct ring_buffer_event *event;
1030 struct userstack_entry *entry;
1031 struct stack_trace trace;
1033 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1036 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1037 sizeof(*entry), flags, pc);
1040 entry = ring_buffer_event_data(event);
1042 memset(&entry->caller, 0, sizeof(entry->caller));
1044 trace.nr_entries = 0;
1045 trace.max_entries = FTRACE_STACK_ENTRIES;
1047 trace.entries = entry->caller;
1049 save_stack_trace_user(&trace);
1050 filter_check_discard(call, entry, event);
1051 ring_buffer_unlock_commit(tr->buffer, event);
1056 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1058 ftrace_trace_userstack(tr, flags, preempt_count());
1063 ftrace_trace_special(void *__tr,
1064 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1067 struct ring_buffer_event *event;
1068 struct trace_array *tr = __tr;
1069 struct special_entry *entry;
1071 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1072 sizeof(*entry), 0, pc);
1075 entry = ring_buffer_event_data(event);
1079 trace_buffer_unlock_commit(tr, event, 0, pc);
1083 __trace_special(void *__tr, void *__data,
1084 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1086 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1090 tracing_sched_switch_trace(struct trace_array *tr,
1091 struct task_struct *prev,
1092 struct task_struct *next,
1093 unsigned long flags, int pc)
1095 struct ftrace_event_call *call = &event_context_switch;
1096 struct ring_buffer_event *event;
1097 struct ctx_switch_entry *entry;
1099 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1100 sizeof(*entry), flags, pc);
1103 entry = ring_buffer_event_data(event);
1104 entry->prev_pid = prev->pid;
1105 entry->prev_prio = prev->prio;
1106 entry->prev_state = prev->state;
1107 entry->next_pid = next->pid;
1108 entry->next_prio = next->prio;
1109 entry->next_state = next->state;
1110 entry->next_cpu = task_cpu(next);
1112 filter_check_discard(call, entry, event);
1114 trace_buffer_unlock_commit(tr, event, flags, pc);
1118 tracing_sched_wakeup_trace(struct trace_array *tr,
1119 struct task_struct *wakee,
1120 struct task_struct *curr,
1121 unsigned long flags, int pc)
1123 struct ftrace_event_call *call = &event_wakeup;
1124 struct ring_buffer_event *event;
1125 struct ctx_switch_entry *entry;
1127 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1128 sizeof(*entry), flags, pc);
1131 entry = ring_buffer_event_data(event);
1132 entry->prev_pid = curr->pid;
1133 entry->prev_prio = curr->prio;
1134 entry->prev_state = curr->state;
1135 entry->next_pid = wakee->pid;
1136 entry->next_prio = wakee->prio;
1137 entry->next_state = wakee->state;
1138 entry->next_cpu = task_cpu(wakee);
1140 filter_check_discard(call, entry, event);
1142 ring_buffer_unlock_commit(tr->buffer, event);
1143 ftrace_trace_stack(tr, flags, 6, pc);
1144 ftrace_trace_userstack(tr, flags, pc);
1148 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1150 struct trace_array *tr = &global_trace;
1151 struct trace_array_cpu *data;
1152 unsigned long flags;
1156 if (tracing_disabled)
1159 pc = preempt_count();
1160 local_irq_save(flags);
1161 cpu = raw_smp_processor_id();
1162 data = tr->data[cpu];
1164 if (likely(atomic_inc_return(&data->disabled) == 1))
1165 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1167 atomic_dec(&data->disabled);
1168 local_irq_restore(flags);
1171 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1172 int trace_graph_entry(struct ftrace_graph_ent *trace)
1174 struct trace_array *tr = &global_trace;
1175 struct trace_array_cpu *data;
1176 unsigned long flags;
1182 if (!ftrace_trace_task(current))
1185 if (!ftrace_graph_addr(trace->func))
1188 local_irq_save(flags);
1189 cpu = raw_smp_processor_id();
1190 data = tr->data[cpu];
1191 disabled = atomic_inc_return(&data->disabled);
1192 if (likely(disabled == 1)) {
1193 pc = preempt_count();
1194 ret = __trace_graph_entry(tr, trace, flags, pc);
1198 /* Only do the atomic if it is not already set */
1199 if (!test_tsk_trace_graph(current))
1200 set_tsk_trace_graph(current);
1202 atomic_dec(&data->disabled);
1203 local_irq_restore(flags);
1208 void trace_graph_return(struct ftrace_graph_ret *trace)
1210 struct trace_array *tr = &global_trace;
1211 struct trace_array_cpu *data;
1212 unsigned long flags;
1217 local_irq_save(flags);
1218 cpu = raw_smp_processor_id();
1219 data = tr->data[cpu];
1220 disabled = atomic_inc_return(&data->disabled);
1221 if (likely(disabled == 1)) {
1222 pc = preempt_count();
1223 __trace_graph_return(tr, trace, flags, pc);
1226 clear_tsk_trace_graph(current);
1227 atomic_dec(&data->disabled);
1228 local_irq_restore(flags);
1230 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1234 * trace_vbprintk - write binary msg to tracing buffer
1237 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1239 static raw_spinlock_t trace_buf_lock =
1240 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1241 static u32 trace_buf[TRACE_BUF_SIZE];
1243 struct ftrace_event_call *call = &event_bprint;
1244 struct ring_buffer_event *event;
1245 struct trace_array *tr = &global_trace;
1246 struct trace_array_cpu *data;
1247 struct bprint_entry *entry;
1248 unsigned long flags;
1250 int cpu, len = 0, size, pc;
1252 if (unlikely(tracing_selftest_running || tracing_disabled))
1255 /* Don't pollute graph traces with trace_vprintk internals */
1256 pause_graph_tracing();
1258 pc = preempt_count();
1259 resched = ftrace_preempt_disable();
1260 cpu = raw_smp_processor_id();
1261 data = tr->data[cpu];
1263 if (unlikely(atomic_read(&data->disabled)))
1266 /* Lockdep uses trace_printk for lock tracing */
1267 local_irq_save(flags);
1268 __raw_spin_lock(&trace_buf_lock);
1269 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1271 if (len > TRACE_BUF_SIZE || len < 0)
1274 size = sizeof(*entry) + sizeof(u32) * len;
1275 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1278 entry = ring_buffer_event_data(event);
1282 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1283 filter_check_discard(call, entry, event);
1284 ring_buffer_unlock_commit(tr->buffer, event);
1287 __raw_spin_unlock(&trace_buf_lock);
1288 local_irq_restore(flags);
1291 ftrace_preempt_enable(resched);
1292 unpause_graph_tracing();
1296 EXPORT_SYMBOL_GPL(trace_vbprintk);
1298 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1300 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1301 static char trace_buf[TRACE_BUF_SIZE];
1303 struct ftrace_event_call *call = &event_print;
1304 struct ring_buffer_event *event;
1305 struct trace_array *tr = &global_trace;
1306 struct trace_array_cpu *data;
1307 int cpu, len = 0, size, pc;
1308 struct print_entry *entry;
1309 unsigned long irq_flags;
1311 if (tracing_disabled || tracing_selftest_running)
1314 pc = preempt_count();
1315 preempt_disable_notrace();
1316 cpu = raw_smp_processor_id();
1317 data = tr->data[cpu];
1319 if (unlikely(atomic_read(&data->disabled)))
1322 pause_graph_tracing();
1323 raw_local_irq_save(irq_flags);
1324 __raw_spin_lock(&trace_buf_lock);
1325 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1327 len = min(len, TRACE_BUF_SIZE-1);
1330 size = sizeof(*entry) + len + 1;
1331 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1334 entry = ring_buffer_event_data(event);
1337 memcpy(&entry->buf, trace_buf, len);
1338 entry->buf[len] = 0;
1339 filter_check_discard(call, entry, event);
1340 ring_buffer_unlock_commit(tr->buffer, event);
1343 __raw_spin_unlock(&trace_buf_lock);
1344 raw_local_irq_restore(irq_flags);
1345 unpause_graph_tracing();
1347 preempt_enable_notrace();
1351 EXPORT_SYMBOL_GPL(trace_vprintk);
1353 enum trace_file_type {
1354 TRACE_FILE_LAT_FMT = 1,
1355 TRACE_FILE_ANNOTATE = 2,
1358 static void trace_iterator_increment(struct trace_iterator *iter)
1360 /* Don't allow ftrace to trace into the ring buffers */
1361 ftrace_disable_cpu();
1364 if (iter->buffer_iter[iter->cpu])
1365 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1367 ftrace_enable_cpu();
1370 static struct trace_entry *
1371 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1373 struct ring_buffer_event *event;
1374 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1376 /* Don't allow ftrace to trace into the ring buffers */
1377 ftrace_disable_cpu();
1380 event = ring_buffer_iter_peek(buf_iter, ts);
1382 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1384 ftrace_enable_cpu();
1386 return event ? ring_buffer_event_data(event) : NULL;
1389 static struct trace_entry *
1390 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1392 struct ring_buffer *buffer = iter->tr->buffer;
1393 struct trace_entry *ent, *next = NULL;
1394 int cpu_file = iter->cpu_file;
1395 u64 next_ts = 0, ts;
1400 * If we are in a per_cpu trace file, don't bother by iterating over
1401 * all cpu and peek directly.
1403 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1404 if (ring_buffer_empty_cpu(buffer, cpu_file))
1406 ent = peek_next_entry(iter, cpu_file, ent_ts);
1408 *ent_cpu = cpu_file;
1413 for_each_tracing_cpu(cpu) {
1415 if (ring_buffer_empty_cpu(buffer, cpu))
1418 ent = peek_next_entry(iter, cpu, &ts);
1421 * Pick the entry with the smallest timestamp:
1423 if (ent && (!next || ts < next_ts)) {
1431 *ent_cpu = next_cpu;
1439 /* Find the next real entry, without updating the iterator itself */
1440 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1441 int *ent_cpu, u64 *ent_ts)
1443 return __find_next_entry(iter, ent_cpu, ent_ts);
1446 /* Find the next real entry, and increment the iterator to the next entry */
1447 static void *find_next_entry_inc(struct trace_iterator *iter)
1449 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1452 trace_iterator_increment(iter);
1454 return iter->ent ? iter : NULL;
1457 static void trace_consume(struct trace_iterator *iter)
1459 /* Don't allow ftrace to trace into the ring buffers */
1460 ftrace_disable_cpu();
1461 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1462 ftrace_enable_cpu();
1465 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1467 struct trace_iterator *iter = m->private;
1473 /* can't go backwards */
1478 ent = find_next_entry_inc(iter);
1482 while (ent && iter->idx < i)
1483 ent = find_next_entry_inc(iter);
1491 * No necessary locking here. The worst thing which can
1492 * happen is loosing events consumed at the same time
1493 * by a trace_pipe reader.
1494 * Other than that, we don't risk to crash the ring buffer
1495 * because it serializes the readers.
1497 * The current tracer is copied to avoid a global locking
1500 static void *s_start(struct seq_file *m, loff_t *pos)
1502 struct trace_iterator *iter = m->private;
1503 static struct tracer *old_tracer;
1504 int cpu_file = iter->cpu_file;
1509 /* copy the tracer to avoid using a global lock all around */
1510 mutex_lock(&trace_types_lock);
1511 if (unlikely(old_tracer != current_trace && current_trace)) {
1512 old_tracer = current_trace;
1513 *iter->trace = *current_trace;
1515 mutex_unlock(&trace_types_lock);
1517 atomic_inc(&trace_record_cmdline_disabled);
1519 if (*pos != iter->pos) {
1524 ftrace_disable_cpu();
1526 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1527 for_each_tracing_cpu(cpu)
1528 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1530 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1533 ftrace_enable_cpu();
1535 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1540 p = s_next(m, p, &l);
1546 static void s_stop(struct seq_file *m, void *p)
1548 atomic_dec(&trace_record_cmdline_disabled);
1551 static void print_lat_help_header(struct seq_file *m)
1553 seq_puts(m, "# _------=> CPU# \n");
1554 seq_puts(m, "# / _-----=> irqs-off \n");
1555 seq_puts(m, "# | / _----=> need-resched \n");
1556 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1557 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1558 seq_puts(m, "# |||| / \n");
1559 seq_puts(m, "# ||||| delay \n");
1560 seq_puts(m, "# cmd pid ||||| time | caller \n");
1561 seq_puts(m, "# \\ / ||||| \\ | / \n");
1564 static void print_func_help_header(struct seq_file *m)
1566 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1567 seq_puts(m, "# | | | | |\n");
1572 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1574 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1575 struct trace_array *tr = iter->tr;
1576 struct trace_array_cpu *data = tr->data[tr->cpu];
1577 struct tracer *type = current_trace;
1578 unsigned long total;
1579 unsigned long entries;
1580 const char *name = "preemption";
1585 entries = ring_buffer_entries(iter->tr->buffer);
1587 ring_buffer_overruns(iter->tr->buffer);
1589 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1591 seq_puts(m, "# -----------------------------------"
1592 "---------------------------------\n");
1593 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1594 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1595 nsecs_to_usecs(data->saved_latency),
1599 #if defined(CONFIG_PREEMPT_NONE)
1601 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1603 #elif defined(CONFIG_PREEMPT)
1608 /* These are reserved for later use */
1611 seq_printf(m, " #P:%d)\n", num_online_cpus());
1615 seq_puts(m, "# -----------------\n");
1616 seq_printf(m, "# | task: %.16s-%d "
1617 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1618 data->comm, data->pid, data->uid, data->nice,
1619 data->policy, data->rt_priority);
1620 seq_puts(m, "# -----------------\n");
1622 if (data->critical_start) {
1623 seq_puts(m, "# => started at: ");
1624 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1625 trace_print_seq(m, &iter->seq);
1626 seq_puts(m, "\n# => ended at: ");
1627 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1628 trace_print_seq(m, &iter->seq);
1635 static void test_cpu_buff_start(struct trace_iterator *iter)
1637 struct trace_seq *s = &iter->seq;
1639 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1642 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1645 if (cpumask_test_cpu(iter->cpu, iter->started))
1648 cpumask_set_cpu(iter->cpu, iter->started);
1650 /* Don't print started cpu buffer for the first entry of the trace */
1652 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1656 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1658 struct trace_seq *s = &iter->seq;
1659 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1660 struct trace_entry *entry;
1661 struct trace_event *event;
1665 test_cpu_buff_start(iter);
1667 event = ftrace_find_event(entry->type);
1669 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1670 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1671 if (!trace_print_lat_context(iter))
1674 if (!trace_print_context(iter))
1680 return event->trace(iter, sym_flags);
1682 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1685 return TRACE_TYPE_HANDLED;
1687 return TRACE_TYPE_PARTIAL_LINE;
1690 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1692 struct trace_seq *s = &iter->seq;
1693 struct trace_entry *entry;
1694 struct trace_event *event;
1698 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1699 if (!trace_seq_printf(s, "%d %d %llu ",
1700 entry->pid, iter->cpu, iter->ts))
1704 event = ftrace_find_event(entry->type);
1706 return event->raw(iter, 0);
1708 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1711 return TRACE_TYPE_HANDLED;
1713 return TRACE_TYPE_PARTIAL_LINE;
1716 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1718 struct trace_seq *s = &iter->seq;
1719 unsigned char newline = '\n';
1720 struct trace_entry *entry;
1721 struct trace_event *event;
1725 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1726 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1727 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1728 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1731 event = ftrace_find_event(entry->type);
1733 enum print_line_t ret = event->hex(iter, 0);
1734 if (ret != TRACE_TYPE_HANDLED)
1738 SEQ_PUT_FIELD_RET(s, newline);
1740 return TRACE_TYPE_HANDLED;
1743 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1745 struct trace_seq *s = &iter->seq;
1746 struct trace_entry *entry;
1747 struct trace_event *event;
1751 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1752 SEQ_PUT_FIELD_RET(s, entry->pid);
1753 SEQ_PUT_FIELD_RET(s, iter->cpu);
1754 SEQ_PUT_FIELD_RET(s, iter->ts);
1757 event = ftrace_find_event(entry->type);
1758 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1761 static int trace_empty(struct trace_iterator *iter)
1765 /* If we are looking at one CPU buffer, only check that one */
1766 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1767 cpu = iter->cpu_file;
1768 if (iter->buffer_iter[cpu]) {
1769 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1772 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1778 for_each_tracing_cpu(cpu) {
1779 if (iter->buffer_iter[cpu]) {
1780 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1783 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1791 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1793 enum print_line_t ret;
1795 if (iter->trace && iter->trace->print_line) {
1796 ret = iter->trace->print_line(iter);
1797 if (ret != TRACE_TYPE_UNHANDLED)
1801 if (iter->ent->type == TRACE_BPRINT &&
1802 trace_flags & TRACE_ITER_PRINTK &&
1803 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1804 return trace_print_bprintk_msg_only(iter);
1806 if (iter->ent->type == TRACE_PRINT &&
1807 trace_flags & TRACE_ITER_PRINTK &&
1808 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1809 return trace_print_printk_msg_only(iter);
1811 if (trace_flags & TRACE_ITER_BIN)
1812 return print_bin_fmt(iter);
1814 if (trace_flags & TRACE_ITER_HEX)
1815 return print_hex_fmt(iter);
1817 if (trace_flags & TRACE_ITER_RAW)
1818 return print_raw_fmt(iter);
1820 return print_trace_fmt(iter);
1823 static int s_show(struct seq_file *m, void *v)
1825 struct trace_iterator *iter = v;
1827 if (iter->ent == NULL) {
1829 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1832 if (iter->trace && iter->trace->print_header)
1833 iter->trace->print_header(m);
1834 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1835 /* print nothing if the buffers are empty */
1836 if (trace_empty(iter))
1838 print_trace_header(m, iter);
1839 if (!(trace_flags & TRACE_ITER_VERBOSE))
1840 print_lat_help_header(m);
1842 if (!(trace_flags & TRACE_ITER_VERBOSE))
1843 print_func_help_header(m);
1846 print_trace_line(iter);
1847 trace_print_seq(m, &iter->seq);
1853 static struct seq_operations tracer_seq_ops = {
1860 static struct trace_iterator *
1861 __tracing_open(struct inode *inode, struct file *file)
1863 long cpu_file = (long) inode->i_private;
1864 void *fail_ret = ERR_PTR(-ENOMEM);
1865 struct trace_iterator *iter;
1869 if (tracing_disabled)
1870 return ERR_PTR(-ENODEV);
1872 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1874 return ERR_PTR(-ENOMEM);
1877 * We make a copy of the current tracer to avoid concurrent
1878 * changes on it while we are reading.
1880 mutex_lock(&trace_types_lock);
1881 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1886 *iter->trace = *current_trace;
1888 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1891 cpumask_clear(iter->started);
1893 if (current_trace && current_trace->print_max)
1896 iter->tr = &global_trace;
1898 mutex_init(&iter->mutex);
1899 iter->cpu_file = cpu_file;
1901 /* Notify the tracer early; before we stop tracing. */
1902 if (iter->trace && iter->trace->open)
1903 iter->trace->open(iter);
1905 /* Annotate start of buffers if we had overruns */
1906 if (ring_buffer_overruns(iter->tr->buffer))
1907 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1909 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1910 for_each_tracing_cpu(cpu) {
1912 iter->buffer_iter[cpu] =
1913 ring_buffer_read_start(iter->tr->buffer, cpu);
1916 cpu = iter->cpu_file;
1917 iter->buffer_iter[cpu] =
1918 ring_buffer_read_start(iter->tr->buffer, cpu);
1921 /* TODO stop tracer */
1922 ret = seq_open(file, &tracer_seq_ops);
1924 fail_ret = ERR_PTR(ret);
1928 m = file->private_data;
1931 /* stop the trace while dumping */
1934 mutex_unlock(&trace_types_lock);
1939 for_each_tracing_cpu(cpu) {
1940 if (iter->buffer_iter[cpu])
1941 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1943 free_cpumask_var(iter->started);
1945 mutex_unlock(&trace_types_lock);
1952 int tracing_open_generic(struct inode *inode, struct file *filp)
1954 if (tracing_disabled)
1957 filp->private_data = inode->i_private;
1961 static int tracing_release(struct inode *inode, struct file *file)
1963 struct seq_file *m = (struct seq_file *)file->private_data;
1964 struct trace_iterator *iter;
1967 if (!(file->f_mode & FMODE_READ))
1972 mutex_lock(&trace_types_lock);
1973 for_each_tracing_cpu(cpu) {
1974 if (iter->buffer_iter[cpu])
1975 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1978 if (iter->trace && iter->trace->close)
1979 iter->trace->close(iter);
1981 /* reenable tracing if it was previously enabled */
1983 mutex_unlock(&trace_types_lock);
1985 seq_release(inode, file);
1986 mutex_destroy(&iter->mutex);
1987 free_cpumask_var(iter->started);
1993 static int tracing_open(struct inode *inode, struct file *file)
1995 struct trace_iterator *iter;
1998 /* If this file was open for write, then erase contents */
1999 if ((file->f_mode & FMODE_WRITE) &&
2000 !(file->f_flags & O_APPEND)) {
2001 long cpu = (long) inode->i_private;
2003 if (cpu == TRACE_PIPE_ALL_CPU)
2004 tracing_reset_online_cpus(&global_trace);
2006 tracing_reset(&global_trace, cpu);
2009 if (file->f_mode & FMODE_READ) {
2010 iter = __tracing_open(inode, file);
2012 ret = PTR_ERR(iter);
2013 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2014 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2020 t_next(struct seq_file *m, void *v, loff_t *pos)
2022 struct tracer *t = m->private;
2034 static void *t_start(struct seq_file *m, loff_t *pos)
2036 struct tracer *t = m->private;
2039 mutex_lock(&trace_types_lock);
2040 for (; t && l < *pos; t = t_next(m, t, &l))
2046 static void t_stop(struct seq_file *m, void *p)
2048 mutex_unlock(&trace_types_lock);
2051 static int t_show(struct seq_file *m, void *v)
2053 struct tracer *t = v;
2058 seq_printf(m, "%s", t->name);
2067 static struct seq_operations show_traces_seq_ops = {
2074 static int show_traces_open(struct inode *inode, struct file *file)
2078 if (tracing_disabled)
2081 ret = seq_open(file, &show_traces_seq_ops);
2083 struct seq_file *m = file->private_data;
2084 m->private = trace_types;
2091 tracing_write_stub(struct file *filp, const char __user *ubuf,
2092 size_t count, loff_t *ppos)
2097 static const struct file_operations tracing_fops = {
2098 .open = tracing_open,
2100 .write = tracing_write_stub,
2101 .llseek = seq_lseek,
2102 .release = tracing_release,
2105 static const struct file_operations show_traces_fops = {
2106 .open = show_traces_open,
2108 .release = seq_release,
2112 * Only trace on a CPU if the bitmask is set:
2114 static cpumask_var_t tracing_cpumask;
2117 * The tracer itself will not take this lock, but still we want
2118 * to provide a consistent cpumask to user-space:
2120 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2123 * Temporary storage for the character representation of the
2124 * CPU bitmask (and one more byte for the newline):
2126 static char mask_str[NR_CPUS + 1];
2129 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2130 size_t count, loff_t *ppos)
2134 mutex_lock(&tracing_cpumask_update_lock);
2136 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2137 if (count - len < 2) {
2141 len += sprintf(mask_str + len, "\n");
2142 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2145 mutex_unlock(&tracing_cpumask_update_lock);
2151 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2152 size_t count, loff_t *ppos)
2155 cpumask_var_t tracing_cpumask_new;
2157 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2160 mutex_lock(&tracing_cpumask_update_lock);
2161 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2165 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 (cpumask_test_cpu(cpu, tracing_cpumask) &&
2173 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2174 atomic_inc(&global_trace.data[cpu]->disabled);
2176 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2177 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2178 atomic_dec(&global_trace.data[cpu]->disabled);
2181 __raw_spin_unlock(&ftrace_max_lock);
2184 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2186 mutex_unlock(&tracing_cpumask_update_lock);
2187 free_cpumask_var(tracing_cpumask_new);
2192 mutex_unlock(&tracing_cpumask_update_lock);
2193 free_cpumask_var(tracing_cpumask);
2198 static const struct file_operations tracing_cpumask_fops = {
2199 .open = tracing_open_generic,
2200 .read = tracing_cpumask_read,
2201 .write = tracing_cpumask_write,
2205 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2206 size_t cnt, loff_t *ppos)
2208 struct tracer_opt *trace_opts;
2216 /* calculate max size */
2217 for (i = 0; trace_options[i]; i++) {
2218 len += strlen(trace_options[i]);
2219 len += 3; /* "no" and newline */
2222 mutex_lock(&trace_types_lock);
2223 tracer_flags = current_trace->flags->val;
2224 trace_opts = current_trace->flags->opts;
2227 * Increase the size with names of options specific
2228 * of the current tracer.
2230 for (i = 0; trace_opts[i].name; i++) {
2231 len += strlen(trace_opts[i].name);
2232 len += 3; /* "no" and newline */
2235 /* +2 for \n and \0 */
2236 buf = kmalloc(len + 2, GFP_KERNEL);
2238 mutex_unlock(&trace_types_lock);
2242 for (i = 0; trace_options[i]; i++) {
2243 if (trace_flags & (1 << i))
2244 r += sprintf(buf + r, "%s\n", trace_options[i]);
2246 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2249 for (i = 0; trace_opts[i].name; i++) {
2250 if (tracer_flags & trace_opts[i].bit)
2251 r += sprintf(buf + r, "%s\n",
2252 trace_opts[i].name);
2254 r += sprintf(buf + r, "no%s\n",
2255 trace_opts[i].name);
2257 mutex_unlock(&trace_types_lock);
2259 WARN_ON(r >= len + 2);
2261 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2267 /* Try to assign a tracer specific option */
2268 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2270 struct tracer_flags *trace_flags = trace->flags;
2271 struct tracer_opt *opts = NULL;
2275 for (i = 0; trace_flags->opts[i].name; i++) {
2276 opts = &trace_flags->opts[i];
2277 len = strlen(opts->name);
2279 if (strncmp(cmp, opts->name, len) == 0) {
2280 ret = trace->set_flag(trace_flags->val,
2286 if (!trace_flags->opts[i].name)
2289 /* Refused to handle */
2294 trace_flags->val &= ~opts->bit;
2296 trace_flags->val |= opts->bit;
2301 static void set_tracer_flags(unsigned int mask, int enabled)
2303 /* do nothing if flag is already set */
2304 if (!!(trace_flags & mask) == !!enabled)
2308 trace_flags |= mask;
2310 trace_flags &= ~mask;
2312 if (mask == TRACE_ITER_GLOBAL_CLK) {
2316 func = trace_clock_global;
2318 func = trace_clock_local;
2320 mutex_lock(&trace_types_lock);
2321 ring_buffer_set_clock(global_trace.buffer, func);
2324 ring_buffer_set_clock(max_tr.buffer, func);
2325 mutex_unlock(&trace_types_lock);
2330 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2331 size_t cnt, loff_t *ppos)
2339 if (cnt >= sizeof(buf))
2342 if (copy_from_user(&buf, ubuf, cnt))
2347 if (strncmp(buf, "no", 2) == 0) {
2352 for (i = 0; trace_options[i]; i++) {
2353 int len = strlen(trace_options[i]);
2355 if (strncmp(cmp, trace_options[i], len) == 0) {
2356 set_tracer_flags(1 << i, !neg);
2361 /* If no option could be set, test the specific tracer options */
2362 if (!trace_options[i]) {
2363 mutex_lock(&trace_types_lock);
2364 ret = set_tracer_option(current_trace, cmp, neg);
2365 mutex_unlock(&trace_types_lock);
2375 static const struct file_operations tracing_iter_fops = {
2376 .open = tracing_open_generic,
2377 .read = tracing_trace_options_read,
2378 .write = tracing_trace_options_write,
2381 static const char readme_msg[] =
2382 "tracing mini-HOWTO:\n\n"
2384 "# mount -t debugfs nodev /debug\n\n"
2385 "# cat /debug/tracing/available_tracers\n"
2386 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2387 "# cat /debug/tracing/current_tracer\n"
2389 "# echo sched_switch > /debug/tracing/current_tracer\n"
2390 "# cat /debug/tracing/current_tracer\n"
2392 "# cat /debug/tracing/trace_options\n"
2393 "noprint-parent nosym-offset nosym-addr noverbose\n"
2394 "# echo print-parent > /debug/tracing/trace_options\n"
2395 "# echo 1 > /debug/tracing/tracing_enabled\n"
2396 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2397 "echo 0 > /debug/tracing/tracing_enabled\n"
2401 tracing_readme_read(struct file *filp, char __user *ubuf,
2402 size_t cnt, loff_t *ppos)
2404 return simple_read_from_buffer(ubuf, cnt, ppos,
2405 readme_msg, strlen(readme_msg));
2408 static const struct file_operations tracing_readme_fops = {
2409 .open = tracing_open_generic,
2410 .read = tracing_readme_read,
2414 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2415 size_t cnt, loff_t *ppos)
2420 r = sprintf(buf, "%u\n", tracer_enabled);
2421 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2425 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2426 size_t cnt, loff_t *ppos)
2428 struct trace_array *tr = filp->private_data;
2433 if (cnt >= sizeof(buf))
2436 if (copy_from_user(&buf, ubuf, cnt))
2441 ret = strict_strtoul(buf, 10, &val);
2447 mutex_lock(&trace_types_lock);
2448 if (tracer_enabled ^ val) {
2451 if (current_trace->start)
2452 current_trace->start(tr);
2457 if (current_trace->stop)
2458 current_trace->stop(tr);
2461 mutex_unlock(&trace_types_lock);
2469 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2470 size_t cnt, loff_t *ppos)
2472 char buf[max_tracer_type_len+2];
2475 mutex_lock(&trace_types_lock);
2477 r = sprintf(buf, "%s\n", current_trace->name);
2479 r = sprintf(buf, "\n");
2480 mutex_unlock(&trace_types_lock);
2482 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2485 int tracer_init(struct tracer *t, struct trace_array *tr)
2487 tracing_reset_online_cpus(tr);
2491 static int tracing_resize_ring_buffer(unsigned long size)
2496 * If kernel or user changes the size of the ring buffer
2497 * we use the size that was given, and we can forget about
2498 * expanding it later.
2500 ring_buffer_expanded = 1;
2502 ret = ring_buffer_resize(global_trace.buffer, size);
2506 ret = ring_buffer_resize(max_tr.buffer, size);
2510 r = ring_buffer_resize(global_trace.buffer,
2511 global_trace.entries);
2514 * AARGH! We are left with different
2515 * size max buffer!!!!
2516 * The max buffer is our "snapshot" buffer.
2517 * When a tracer needs a snapshot (one of the
2518 * latency tracers), it swaps the max buffer
2519 * with the saved snap shot. We succeeded to
2520 * update the size of the main buffer, but failed to
2521 * update the size of the max buffer. But when we tried
2522 * to reset the main buffer to the original size, we
2523 * failed there too. This is very unlikely to
2524 * happen, but if it does, warn and kill all
2528 tracing_disabled = 1;
2533 global_trace.entries = size;
2539 * tracing_update_buffers - used by tracing facility to expand ring buffers
2541 * To save on memory when the tracing is never used on a system with it
2542 * configured in. The ring buffers are set to a minimum size. But once
2543 * a user starts to use the tracing facility, then they need to grow
2544 * to their default size.
2546 * This function is to be called when a tracer is about to be used.
2548 int tracing_update_buffers(void)
2552 mutex_lock(&trace_types_lock);
2553 if (!ring_buffer_expanded)
2554 ret = tracing_resize_ring_buffer(trace_buf_size);
2555 mutex_unlock(&trace_types_lock);
2560 struct trace_option_dentry;
2562 static struct trace_option_dentry *
2563 create_trace_option_files(struct tracer *tracer);
2566 destroy_trace_option_files(struct trace_option_dentry *topts);
2568 static int tracing_set_tracer(const char *buf)
2570 static struct trace_option_dentry *topts;
2571 struct trace_array *tr = &global_trace;
2575 mutex_lock(&trace_types_lock);
2577 if (!ring_buffer_expanded) {
2578 ret = tracing_resize_ring_buffer(trace_buf_size);
2584 for (t = trace_types; t; t = t->next) {
2585 if (strcmp(t->name, buf) == 0)
2592 if (t == current_trace)
2595 trace_branch_disable();
2596 if (current_trace && current_trace->reset)
2597 current_trace->reset(tr);
2599 destroy_trace_option_files(topts);
2603 topts = create_trace_option_files(current_trace);
2606 ret = tracer_init(t, tr);
2611 trace_branch_enable(tr);
2613 mutex_unlock(&trace_types_lock);
2619 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2620 size_t cnt, loff_t *ppos)
2622 char buf[max_tracer_type_len+1];
2629 if (cnt > max_tracer_type_len)
2630 cnt = max_tracer_type_len;
2632 if (copy_from_user(&buf, ubuf, cnt))
2637 /* strip ending whitespace. */
2638 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2641 err = tracing_set_tracer(buf);
2651 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2652 size_t cnt, loff_t *ppos)
2654 unsigned long *ptr = filp->private_data;
2658 r = snprintf(buf, sizeof(buf), "%ld\n",
2659 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2660 if (r > sizeof(buf))
2662 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2666 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2667 size_t cnt, loff_t *ppos)
2669 unsigned long *ptr = filp->private_data;
2674 if (cnt >= sizeof(buf))
2677 if (copy_from_user(&buf, ubuf, cnt))
2682 ret = strict_strtoul(buf, 10, &val);
2691 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2693 long cpu_file = (long) inode->i_private;
2694 struct trace_iterator *iter;
2697 if (tracing_disabled)
2700 mutex_lock(&trace_types_lock);
2702 /* We only allow one reader per cpu */
2703 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2704 if (!cpumask_empty(tracing_reader_cpumask)) {
2708 cpumask_setall(tracing_reader_cpumask);
2710 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2711 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2718 /* create a buffer to store the information to pass to userspace */
2719 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2726 * We make a copy of the current tracer to avoid concurrent
2727 * changes on it while we are reading.
2729 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2735 *iter->trace = *current_trace;
2737 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2742 /* trace pipe does not show start of buffer */
2743 cpumask_setall(iter->started);
2745 iter->cpu_file = cpu_file;
2746 iter->tr = &global_trace;
2747 mutex_init(&iter->mutex);
2748 filp->private_data = iter;
2750 if (iter->trace->pipe_open)
2751 iter->trace->pipe_open(iter);
2754 mutex_unlock(&trace_types_lock);
2760 mutex_unlock(&trace_types_lock);
2764 static int tracing_release_pipe(struct inode *inode, struct file *file)
2766 struct trace_iterator *iter = file->private_data;
2768 mutex_lock(&trace_types_lock);
2770 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2771 cpumask_clear(tracing_reader_cpumask);
2773 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2775 mutex_unlock(&trace_types_lock);
2777 free_cpumask_var(iter->started);
2778 mutex_destroy(&iter->mutex);
2786 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2788 struct trace_iterator *iter = filp->private_data;
2790 if (trace_flags & TRACE_ITER_BLOCK) {
2792 * Always select as readable when in blocking mode
2794 return POLLIN | POLLRDNORM;
2796 if (!trace_empty(iter))
2797 return POLLIN | POLLRDNORM;
2798 poll_wait(filp, &trace_wait, poll_table);
2799 if (!trace_empty(iter))
2800 return POLLIN | POLLRDNORM;
2807 void default_wait_pipe(struct trace_iterator *iter)
2811 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2813 if (trace_empty(iter))
2816 finish_wait(&trace_wait, &wait);
2820 * This is a make-shift waitqueue.
2821 * A tracer might use this callback on some rare cases:
2823 * 1) the current tracer might hold the runqueue lock when it wakes up
2824 * a reader, hence a deadlock (sched, function, and function graph tracers)
2825 * 2) the function tracers, trace all functions, we don't want
2826 * the overhead of calling wake_up and friends
2827 * (and tracing them too)
2829 * Anyway, this is really very primitive wakeup.
2831 void poll_wait_pipe(struct trace_iterator *iter)
2833 set_current_state(TASK_INTERRUPTIBLE);
2834 /* sleep for 100 msecs, and try again. */
2835 schedule_timeout(HZ / 10);
2838 /* Must be called with trace_types_lock mutex held. */
2839 static int tracing_wait_pipe(struct file *filp)
2841 struct trace_iterator *iter = filp->private_data;
2843 while (trace_empty(iter)) {
2845 if ((filp->f_flags & O_NONBLOCK)) {
2849 mutex_unlock(&iter->mutex);
2851 iter->trace->wait_pipe(iter);
2853 mutex_lock(&iter->mutex);
2855 if (signal_pending(current))
2859 * We block until we read something and tracing is disabled.
2860 * We still block if tracing is disabled, but we have never
2861 * read anything. This allows a user to cat this file, and
2862 * then enable tracing. But after we have read something,
2863 * we give an EOF when tracing is again disabled.
2865 * iter->pos will be 0 if we haven't read anything.
2867 if (!tracer_enabled && iter->pos)
2878 tracing_read_pipe(struct file *filp, char __user *ubuf,
2879 size_t cnt, loff_t *ppos)
2881 struct trace_iterator *iter = filp->private_data;
2882 static struct tracer *old_tracer;
2885 /* return any leftover data */
2886 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2890 trace_seq_init(&iter->seq);
2892 /* copy the tracer to avoid using a global lock all around */
2893 mutex_lock(&trace_types_lock);
2894 if (unlikely(old_tracer != current_trace && current_trace)) {
2895 old_tracer = current_trace;
2896 *iter->trace = *current_trace;
2898 mutex_unlock(&trace_types_lock);
2901 * Avoid more than one consumer on a single file descriptor
2902 * This is just a matter of traces coherency, the ring buffer itself
2905 mutex_lock(&iter->mutex);
2906 if (iter->trace->read) {
2907 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2913 sret = tracing_wait_pipe(filp);
2917 /* stop when tracing is finished */
2918 if (trace_empty(iter)) {
2923 if (cnt >= PAGE_SIZE)
2924 cnt = PAGE_SIZE - 1;
2926 /* reset all but tr, trace, and overruns */
2927 memset(&iter->seq, 0,
2928 sizeof(struct trace_iterator) -
2929 offsetof(struct trace_iterator, seq));
2932 while (find_next_entry_inc(iter) != NULL) {
2933 enum print_line_t ret;
2934 int len = iter->seq.len;
2936 ret = print_trace_line(iter);
2937 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2938 /* don't print partial lines */
2939 iter->seq.len = len;
2942 if (ret != TRACE_TYPE_NO_CONSUME)
2943 trace_consume(iter);
2945 if (iter->seq.len >= cnt)
2949 /* Now copy what we have to the user */
2950 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2951 if (iter->seq.readpos >= iter->seq.len)
2952 trace_seq_init(&iter->seq);
2955 * If there was nothing to send to user, inspite of consuming trace
2956 * entries, go back to wait for more entries.
2962 mutex_unlock(&iter->mutex);
2967 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2968 struct pipe_buffer *buf)
2970 __free_page(buf->page);
2973 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2976 __free_page(spd->pages[idx]);
2979 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2981 .map = generic_pipe_buf_map,
2982 .unmap = generic_pipe_buf_unmap,
2983 .confirm = generic_pipe_buf_confirm,
2984 .release = tracing_pipe_buf_release,
2985 .steal = generic_pipe_buf_steal,
2986 .get = generic_pipe_buf_get,
2990 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2995 /* Seq buffer is page-sized, exactly what we need. */
2997 count = iter->seq.len;
2998 ret = print_trace_line(iter);
2999 count = iter->seq.len - count;
3002 iter->seq.len -= count;
3005 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3006 iter->seq.len -= count;
3010 trace_consume(iter);
3012 if (!find_next_entry_inc(iter)) {
3022 static ssize_t tracing_splice_read_pipe(struct file *filp,
3024 struct pipe_inode_info *pipe,
3028 struct page *pages[PIPE_BUFFERS];
3029 struct partial_page partial[PIPE_BUFFERS];
3030 struct trace_iterator *iter = filp->private_data;
3031 struct splice_pipe_desc spd = {
3034 .nr_pages = 0, /* This gets updated below. */
3036 .ops = &tracing_pipe_buf_ops,
3037 .spd_release = tracing_spd_release_pipe,
3039 static struct tracer *old_tracer;
3044 /* copy the tracer to avoid using a global lock all around */
3045 mutex_lock(&trace_types_lock);
3046 if (unlikely(old_tracer != current_trace && current_trace)) {
3047 old_tracer = current_trace;
3048 *iter->trace = *current_trace;
3050 mutex_unlock(&trace_types_lock);
3052 mutex_lock(&iter->mutex);
3054 if (iter->trace->splice_read) {
3055 ret = iter->trace->splice_read(iter, filp,
3056 ppos, pipe, len, flags);
3061 ret = tracing_wait_pipe(filp);
3065 if (!iter->ent && !find_next_entry_inc(iter)) {
3070 /* Fill as many pages as possible. */
3071 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3072 pages[i] = alloc_page(GFP_KERNEL);
3076 rem = tracing_fill_pipe_page(rem, iter);
3078 /* Copy the data into the page, so we can start over. */
3079 ret = trace_seq_to_buffer(&iter->seq,
3080 page_address(pages[i]),
3083 __free_page(pages[i]);
3086 partial[i].offset = 0;
3087 partial[i].len = iter->seq.len;
3089 trace_seq_init(&iter->seq);
3092 mutex_unlock(&iter->mutex);
3096 return splice_to_pipe(pipe, &spd);
3099 mutex_unlock(&iter->mutex);
3105 tracing_entries_read(struct file *filp, char __user *ubuf,
3106 size_t cnt, loff_t *ppos)
3108 struct trace_array *tr = filp->private_data;
3112 mutex_lock(&trace_types_lock);
3113 if (!ring_buffer_expanded)
3114 r = sprintf(buf, "%lu (expanded: %lu)\n",
3116 trace_buf_size >> 10);
3118 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3119 mutex_unlock(&trace_types_lock);
3121 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3125 tracing_entries_write(struct file *filp, const char __user *ubuf,
3126 size_t cnt, loff_t *ppos)
3132 if (cnt >= sizeof(buf))
3135 if (copy_from_user(&buf, ubuf, cnt))
3140 ret = strict_strtoul(buf, 10, &val);
3144 /* must have at least 1 entry */
3148 mutex_lock(&trace_types_lock);
3152 /* disable all cpu buffers */
3153 for_each_tracing_cpu(cpu) {
3154 if (global_trace.data[cpu])
3155 atomic_inc(&global_trace.data[cpu]->disabled);
3156 if (max_tr.data[cpu])
3157 atomic_inc(&max_tr.data[cpu]->disabled);
3160 /* value is in KB */
3163 if (val != global_trace.entries) {
3164 ret = tracing_resize_ring_buffer(val);
3173 /* If check pages failed, return ENOMEM */
3174 if (tracing_disabled)
3177 for_each_tracing_cpu(cpu) {
3178 if (global_trace.data[cpu])
3179 atomic_dec(&global_trace.data[cpu]->disabled);
3180 if (max_tr.data[cpu])
3181 atomic_dec(&max_tr.data[cpu]->disabled);
3185 max_tr.entries = global_trace.entries;
3186 mutex_unlock(&trace_types_lock);
3191 static int mark_printk(const char *fmt, ...)
3195 va_start(args, fmt);
3196 ret = trace_vprintk(0, fmt, args);
3202 tracing_mark_write(struct file *filp, const char __user *ubuf,
3203 size_t cnt, loff_t *fpos)
3208 if (tracing_disabled)
3211 if (cnt > TRACE_BUF_SIZE)
3212 cnt = TRACE_BUF_SIZE;
3214 buf = kmalloc(cnt + 1, GFP_KERNEL);
3218 if (copy_from_user(buf, ubuf, cnt)) {
3223 /* Cut from the first nil or newline. */
3225 end = strchr(buf, '\n');
3229 cnt = mark_printk("%s\n", buf);
3236 static const struct file_operations tracing_max_lat_fops = {
3237 .open = tracing_open_generic,
3238 .read = tracing_max_lat_read,
3239 .write = tracing_max_lat_write,
3242 static const struct file_operations tracing_ctrl_fops = {
3243 .open = tracing_open_generic,
3244 .read = tracing_ctrl_read,
3245 .write = tracing_ctrl_write,
3248 static const struct file_operations set_tracer_fops = {
3249 .open = tracing_open_generic,
3250 .read = tracing_set_trace_read,
3251 .write = tracing_set_trace_write,
3254 static const struct file_operations tracing_pipe_fops = {
3255 .open = tracing_open_pipe,
3256 .poll = tracing_poll_pipe,
3257 .read = tracing_read_pipe,
3258 .splice_read = tracing_splice_read_pipe,
3259 .release = tracing_release_pipe,
3262 static const struct file_operations tracing_entries_fops = {
3263 .open = tracing_open_generic,
3264 .read = tracing_entries_read,
3265 .write = tracing_entries_write,
3268 static const struct file_operations tracing_mark_fops = {
3269 .open = tracing_open_generic,
3270 .write = tracing_mark_write,
3273 struct ftrace_buffer_info {
3274 struct trace_array *tr;
3280 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3282 int cpu = (int)(long)inode->i_private;
3283 struct ftrace_buffer_info *info;
3285 if (tracing_disabled)
3288 info = kzalloc(sizeof(*info), GFP_KERNEL);
3292 info->tr = &global_trace;
3295 /* Force reading ring buffer for first read */
3296 info->read = (unsigned int)-1;
3298 filp->private_data = info;
3300 return nonseekable_open(inode, filp);
3304 tracing_buffers_read(struct file *filp, char __user *ubuf,
3305 size_t count, loff_t *ppos)
3307 struct ftrace_buffer_info *info = filp->private_data;
3316 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3320 /* Do we have previous read data to read? */
3321 if (info->read < PAGE_SIZE)
3326 ret = ring_buffer_read_page(info->tr->buffer,
3333 pos = ring_buffer_page_len(info->spare);
3335 if (pos < PAGE_SIZE)
3336 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3339 size = PAGE_SIZE - info->read;
3343 ret = copy_to_user(ubuf, info->spare + info->read, size);
3354 static int tracing_buffers_release(struct inode *inode, struct file *file)
3356 struct ftrace_buffer_info *info = file->private_data;
3359 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3366 struct ring_buffer *buffer;
3371 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3372 struct pipe_buffer *buf)
3374 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3379 ring_buffer_free_read_page(ref->buffer, ref->page);
3384 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3385 struct pipe_buffer *buf)
3390 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3391 struct pipe_buffer *buf)
3393 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3398 /* Pipe buffer operations for a buffer. */
3399 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3401 .map = generic_pipe_buf_map,
3402 .unmap = generic_pipe_buf_unmap,
3403 .confirm = generic_pipe_buf_confirm,
3404 .release = buffer_pipe_buf_release,
3405 .steal = buffer_pipe_buf_steal,
3406 .get = buffer_pipe_buf_get,
3410 * Callback from splice_to_pipe(), if we need to release some pages
3411 * at the end of the spd in case we error'ed out in filling the pipe.
3413 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3415 struct buffer_ref *ref =
3416 (struct buffer_ref *)spd->partial[i].private;
3421 ring_buffer_free_read_page(ref->buffer, ref->page);
3423 spd->partial[i].private = 0;
3427 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3428 struct pipe_inode_info *pipe, size_t len,
3431 struct ftrace_buffer_info *info = file->private_data;
3432 struct partial_page partial[PIPE_BUFFERS];
3433 struct page *pages[PIPE_BUFFERS];
3434 struct splice_pipe_desc spd = {
3438 .ops = &buffer_pipe_buf_ops,
3439 .spd_release = buffer_spd_release,
3441 struct buffer_ref *ref;
3445 if (*ppos & (PAGE_SIZE - 1)) {
3446 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3450 if (len & (PAGE_SIZE - 1)) {
3451 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3452 if (len < PAGE_SIZE)
3457 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= PAGE_SIZE) {
3461 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3465 ref->buffer = info->tr->buffer;
3466 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3472 r = ring_buffer_read_page(ref->buffer, &ref->page,
3475 ring_buffer_free_read_page(ref->buffer,
3482 * zero out any left over data, this is going to
3485 size = ring_buffer_page_len(ref->page);
3486 if (size < PAGE_SIZE)
3487 memset(ref->page + size, 0, PAGE_SIZE - size);
3489 page = virt_to_page(ref->page);
3491 spd.pages[i] = page;
3492 spd.partial[i].len = PAGE_SIZE;
3493 spd.partial[i].offset = 0;
3494 spd.partial[i].private = (unsigned long)ref;
3501 /* did we read anything? */
3502 if (!spd.nr_pages) {
3503 if (flags & SPLICE_F_NONBLOCK)
3511 ret = splice_to_pipe(pipe, &spd);
3516 static const struct file_operations tracing_buffers_fops = {
3517 .open = tracing_buffers_open,
3518 .read = tracing_buffers_read,
3519 .release = tracing_buffers_release,
3520 .splice_read = tracing_buffers_splice_read,
3521 .llseek = no_llseek,
3524 #ifdef CONFIG_DYNAMIC_FTRACE
3526 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3532 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3533 size_t cnt, loff_t *ppos)
3535 static char ftrace_dyn_info_buffer[1024];
3536 static DEFINE_MUTEX(dyn_info_mutex);
3537 unsigned long *p = filp->private_data;
3538 char *buf = ftrace_dyn_info_buffer;
3539 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3542 mutex_lock(&dyn_info_mutex);
3543 r = sprintf(buf, "%ld ", *p);
3545 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3548 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3550 mutex_unlock(&dyn_info_mutex);
3555 static const struct file_operations tracing_dyn_info_fops = {
3556 .open = tracing_open_generic,
3557 .read = tracing_read_dyn_info,
3561 static struct dentry *d_tracer;
3563 struct dentry *tracing_init_dentry(void)
3570 if (!debugfs_initialized())
3573 d_tracer = debugfs_create_dir("tracing", NULL);
3575 if (!d_tracer && !once) {
3577 pr_warning("Could not create debugfs directory 'tracing'\n");
3584 static struct dentry *d_percpu;
3586 struct dentry *tracing_dentry_percpu(void)
3589 struct dentry *d_tracer;
3594 d_tracer = tracing_init_dentry();
3599 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3601 if (!d_percpu && !once) {
3603 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3610 static void tracing_init_debugfs_percpu(long cpu)
3612 struct dentry *d_percpu = tracing_dentry_percpu();
3613 struct dentry *d_cpu;
3614 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3617 if (cpu > 999 || cpu < 0)
3620 sprintf(cpu_dir, "cpu%ld", cpu);
3621 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3623 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3627 /* per cpu trace_pipe */
3628 trace_create_file("trace_pipe", 0444, d_cpu,
3629 (void *) cpu, &tracing_pipe_fops);
3632 trace_create_file("trace", 0644, d_cpu,
3633 (void *) cpu, &tracing_fops);
3635 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3636 (void *) cpu, &tracing_buffers_fops);
3639 #ifdef CONFIG_FTRACE_SELFTEST
3640 /* Let selftest have access to static functions in this file */
3641 #include "trace_selftest.c"
3644 struct trace_option_dentry {
3645 struct tracer_opt *opt;
3646 struct tracer_flags *flags;
3647 struct dentry *entry;
3651 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3654 struct trace_option_dentry *topt = filp->private_data;
3657 if (topt->flags->val & topt->opt->bit)
3662 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3666 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3669 struct trace_option_dentry *topt = filp->private_data;
3674 if (cnt >= sizeof(buf))
3677 if (copy_from_user(&buf, ubuf, cnt))
3682 ret = strict_strtoul(buf, 10, &val);
3689 /* do nothing if already cleared */
3690 if (!(topt->flags->val & topt->opt->bit))
3693 mutex_lock(&trace_types_lock);
3694 if (current_trace->set_flag)
3695 ret = current_trace->set_flag(topt->flags->val,
3697 mutex_unlock(&trace_types_lock);
3700 topt->flags->val &= ~topt->opt->bit;
3703 /* do nothing if already set */
3704 if (topt->flags->val & topt->opt->bit)
3707 mutex_lock(&trace_types_lock);
3708 if (current_trace->set_flag)
3709 ret = current_trace->set_flag(topt->flags->val,
3711 mutex_unlock(&trace_types_lock);
3714 topt->flags->val |= topt->opt->bit;
3727 static const struct file_operations trace_options_fops = {
3728 .open = tracing_open_generic,
3729 .read = trace_options_read,
3730 .write = trace_options_write,
3734 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3737 long index = (long)filp->private_data;
3740 if (trace_flags & (1 << index))
3745 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3749 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3752 long index = (long)filp->private_data;
3757 if (cnt >= sizeof(buf))
3760 if (copy_from_user(&buf, ubuf, cnt))
3765 ret = strict_strtoul(buf, 10, &val);
3771 trace_flags &= ~(1 << index);
3774 trace_flags |= 1 << index;
3786 static const struct file_operations trace_options_core_fops = {
3787 .open = tracing_open_generic,
3788 .read = trace_options_core_read,
3789 .write = trace_options_core_write,
3792 struct dentry *trace_create_file(const char *name,
3794 struct dentry *parent,
3796 const struct file_operations *fops)
3800 ret = debugfs_create_file(name, mode, parent, data, fops);
3802 pr_warning("Could not create debugfs '%s' entry\n", name);
3808 static struct dentry *trace_options_init_dentry(void)
3810 struct dentry *d_tracer;
3811 static struct dentry *t_options;
3816 d_tracer = tracing_init_dentry();
3820 t_options = debugfs_create_dir("options", d_tracer);
3822 pr_warning("Could not create debugfs directory 'options'\n");
3830 create_trace_option_file(struct trace_option_dentry *topt,
3831 struct tracer_flags *flags,
3832 struct tracer_opt *opt)
3834 struct dentry *t_options;
3836 t_options = trace_options_init_dentry();
3840 topt->flags = flags;
3843 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
3844 &trace_options_fops);
3848 static struct trace_option_dentry *
3849 create_trace_option_files(struct tracer *tracer)
3851 struct trace_option_dentry *topts;
3852 struct tracer_flags *flags;
3853 struct tracer_opt *opts;
3859 flags = tracer->flags;
3861 if (!flags || !flags->opts)
3866 for (cnt = 0; opts[cnt].name; cnt++)
3869 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3873 for (cnt = 0; opts[cnt].name; cnt++)
3874 create_trace_option_file(&topts[cnt], flags,
3881 destroy_trace_option_files(struct trace_option_dentry *topts)
3888 for (cnt = 0; topts[cnt].opt; cnt++) {
3889 if (topts[cnt].entry)
3890 debugfs_remove(topts[cnt].entry);
3896 static struct dentry *
3897 create_trace_option_core_file(const char *option, long index)
3899 struct dentry *t_options;
3901 t_options = trace_options_init_dentry();
3905 return trace_create_file(option, 0644, t_options, (void *)index,
3906 &trace_options_core_fops);
3909 static __init void create_trace_options_dir(void)
3911 struct dentry *t_options;
3914 t_options = trace_options_init_dentry();
3918 for (i = 0; trace_options[i]; i++)
3919 create_trace_option_core_file(trace_options[i], i);
3922 static __init int tracer_init_debugfs(void)
3924 struct dentry *d_tracer;
3927 d_tracer = tracing_init_dentry();
3929 trace_create_file("tracing_enabled", 0644, d_tracer,
3930 &global_trace, &tracing_ctrl_fops);
3932 trace_create_file("trace_options", 0644, d_tracer,
3933 NULL, &tracing_iter_fops);
3935 trace_create_file("tracing_cpumask", 0644, d_tracer,
3936 NULL, &tracing_cpumask_fops);
3938 trace_create_file("trace", 0644, d_tracer,
3939 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3941 trace_create_file("available_tracers", 0444, d_tracer,
3942 &global_trace, &show_traces_fops);
3944 trace_create_file("current_tracer", 0444, d_tracer,
3945 &global_trace, &set_tracer_fops);
3947 trace_create_file("tracing_max_latency", 0644, d_tracer,
3948 &tracing_max_latency, &tracing_max_lat_fops);
3950 trace_create_file("tracing_thresh", 0644, d_tracer,
3951 &tracing_thresh, &tracing_max_lat_fops);
3953 trace_create_file("README", 0644, d_tracer,
3954 NULL, &tracing_readme_fops);
3956 trace_create_file("trace_pipe", 0444, d_tracer,
3957 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3959 trace_create_file("buffer_size_kb", 0644, d_tracer,
3960 &global_trace, &tracing_entries_fops);
3962 trace_create_file("trace_marker", 0220, d_tracer,
3963 NULL, &tracing_mark_fops);
3965 #ifdef CONFIG_DYNAMIC_FTRACE
3966 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3967 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
3969 #ifdef CONFIG_SYSPROF_TRACER
3970 init_tracer_sysprof_debugfs(d_tracer);
3973 create_trace_options_dir();
3975 for_each_tracing_cpu(cpu)
3976 tracing_init_debugfs_percpu(cpu);
3981 static int trace_panic_handler(struct notifier_block *this,
3982 unsigned long event, void *unused)
3984 if (ftrace_dump_on_oops)
3989 static struct notifier_block trace_panic_notifier = {
3990 .notifier_call = trace_panic_handler,
3992 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3995 static int trace_die_handler(struct notifier_block *self,
4001 if (ftrace_dump_on_oops)
4010 static struct notifier_block trace_die_notifier = {
4011 .notifier_call = trace_die_handler,
4016 * printk is set to max of 1024, we really don't need it that big.
4017 * Nothing should be printing 1000 characters anyway.
4019 #define TRACE_MAX_PRINT 1000
4022 * Define here KERN_TRACE so that we have one place to modify
4023 * it if we decide to change what log level the ftrace dump
4026 #define KERN_TRACE KERN_EMERG
4029 trace_printk_seq(struct trace_seq *s)
4031 /* Probably should print a warning here. */
4035 /* should be zero ended, but we are paranoid. */
4036 s->buffer[s->len] = 0;
4038 printk(KERN_TRACE "%s", s->buffer);
4043 static void __ftrace_dump(bool disable_tracing)
4045 static DEFINE_SPINLOCK(ftrace_dump_lock);
4046 /* use static because iter can be a bit big for the stack */
4047 static struct trace_iterator iter;
4048 unsigned int old_userobj;
4049 static int dump_ran;
4050 unsigned long flags;
4054 spin_lock_irqsave(&ftrace_dump_lock, flags);
4062 if (disable_tracing)
4065 for_each_tracing_cpu(cpu) {
4066 atomic_inc(&global_trace.data[cpu]->disabled);
4069 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4071 /* don't look at user memory in panic mode */
4072 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4074 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4076 /* Simulate the iterator */
4077 iter.tr = &global_trace;
4078 iter.trace = current_trace;
4079 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4082 * We need to stop all tracing on all CPUS to read the
4083 * the next buffer. This is a bit expensive, but is
4084 * not done often. We fill all what we can read,
4085 * and then release the locks again.
4088 while (!trace_empty(&iter)) {
4091 printk(KERN_TRACE "---------------------------------\n");
4095 /* reset all but tr, trace, and overruns */
4096 memset(&iter.seq, 0,
4097 sizeof(struct trace_iterator) -
4098 offsetof(struct trace_iterator, seq));
4099 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4102 if (find_next_entry_inc(&iter) != NULL) {
4103 print_trace_line(&iter);
4104 trace_consume(&iter);
4107 trace_printk_seq(&iter.seq);
4111 printk(KERN_TRACE " (ftrace buffer empty)\n");
4113 printk(KERN_TRACE "---------------------------------\n");
4115 /* Re-enable tracing if requested */
4116 if (!disable_tracing) {
4117 trace_flags |= old_userobj;
4119 for_each_tracing_cpu(cpu) {
4120 atomic_dec(&global_trace.data[cpu]->disabled);
4126 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
4129 /* By default: disable tracing after the dump */
4130 void ftrace_dump(void)
4132 __ftrace_dump(true);
4135 __init static int tracer_alloc_buffers(void)
4137 struct trace_array_cpu *data;
4142 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4145 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4146 goto out_free_buffer_mask;
4148 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4149 goto out_free_tracing_cpumask;
4151 /* To save memory, keep the ring buffer size to its minimum */
4152 if (ring_buffer_expanded)
4153 ring_buf_size = trace_buf_size;
4157 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4158 cpumask_copy(tracing_cpumask, cpu_all_mask);
4159 cpumask_clear(tracing_reader_cpumask);
4161 /* TODO: make the number of buffers hot pluggable with CPUS */
4162 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4163 TRACE_BUFFER_FLAGS);
4164 if (!global_trace.buffer) {
4165 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4167 goto out_free_cpumask;
4169 global_trace.entries = ring_buffer_size(global_trace.buffer);
4172 #ifdef CONFIG_TRACER_MAX_TRACE
4173 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4174 TRACE_BUFFER_FLAGS);
4175 if (!max_tr.buffer) {
4176 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4178 ring_buffer_free(global_trace.buffer);
4179 goto out_free_cpumask;
4181 max_tr.entries = ring_buffer_size(max_tr.buffer);
4182 WARN_ON(max_tr.entries != global_trace.entries);
4185 /* Allocate the first page for all buffers */
4186 for_each_tracing_cpu(i) {
4187 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4188 max_tr.data[i] = &per_cpu(max_data, i);
4191 trace_init_cmdlines();
4193 register_tracer(&nop_trace);
4194 current_trace = &nop_trace;
4195 #ifdef CONFIG_BOOT_TRACER
4196 register_tracer(&boot_tracer);
4198 /* All seems OK, enable tracing */
4199 tracing_disabled = 0;
4201 atomic_notifier_chain_register(&panic_notifier_list,
4202 &trace_panic_notifier);
4204 register_die_notifier(&trace_die_notifier);
4209 free_cpumask_var(tracing_reader_cpumask);
4210 out_free_tracing_cpumask:
4211 free_cpumask_var(tracing_cpumask);
4212 out_free_buffer_mask:
4213 free_cpumask_var(tracing_buffer_mask);
4218 __init static int clear_boot_tracer(void)
4221 * The default tracer at boot buffer is an init section.
4222 * This function is called in lateinit. If we did not
4223 * find the boot tracer, then clear it out, to prevent
4224 * later registration from accessing the buffer that is
4225 * about to be freed.
4227 if (!default_bootup_tracer)
4230 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4231 default_bootup_tracer);
4232 default_bootup_tracer = NULL;
4237 early_initcall(tracer_alloc_buffers);
4238 fs_initcall(tracer_init_debugfs);
4239 late_initcall(clear_boot_tracer);