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 int filter_current_check_discard(struct ftrace_event_call *call, void *rec,
175 struct ring_buffer_event *event)
177 return filter_check_discard(call, rec, global_trace.buffer, event);
180 cycle_t ftrace_now(int cpu)
184 /* Early boot up does not have a buffer yet */
185 if (!global_trace.buffer)
186 return trace_clock_local();
188 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
189 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
195 * The max_tr is used to snapshot the global_trace when a maximum
196 * latency is reached. Some tracers will use this to store a maximum
197 * trace while it continues examining live traces.
199 * The buffers for the max_tr are set up the same as the global_trace.
200 * When a snapshot is taken, the link list of the max_tr is swapped
201 * with the link list of the global_trace and the buffers are reset for
202 * the global_trace so the tracing can continue.
204 static struct trace_array max_tr;
206 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
208 /* tracer_enabled is used to toggle activation of a tracer */
209 static int tracer_enabled = 1;
212 * tracing_is_enabled - return tracer_enabled status
214 * This function is used by other tracers to know the status
215 * of the tracer_enabled flag. Tracers may use this function
216 * to know if it should enable their features when starting
217 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
219 int tracing_is_enabled(void)
221 return tracer_enabled;
225 * trace_buf_size is the size in bytes that is allocated
226 * for a buffer. Note, the number of bytes is always rounded
229 * This number is purposely set to a low number of 16384.
230 * If the dump on oops happens, it will be much appreciated
231 * to not have to wait for all that output. Anyway this can be
232 * boot time and run time configurable.
234 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
236 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
238 /* trace_types holds a link list of available tracers. */
239 static struct tracer *trace_types __read_mostly;
241 /* current_trace points to the tracer that is currently active */
242 static struct tracer *current_trace __read_mostly;
245 * max_tracer_type_len is used to simplify the allocating of
246 * buffers to read userspace tracer names. We keep track of
247 * the longest tracer name registered.
249 static int max_tracer_type_len;
252 * trace_types_lock is used to protect the trace_types list.
253 * This lock is also used to keep user access serialized.
254 * Accesses from userspace will grab this lock while userspace
255 * activities happen inside the kernel.
257 static DEFINE_MUTEX(trace_types_lock);
259 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
260 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
262 /* trace_flags holds trace_options default values */
263 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
264 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
265 TRACE_ITER_GRAPH_TIME;
268 * trace_wake_up - wake up tasks waiting for trace input
270 * Simply wakes up any task that is blocked on the trace_wait
271 * queue. These is used with trace_poll for tasks polling the trace.
273 void trace_wake_up(void)
276 * The runqueue_is_locked() can fail, but this is the best we
279 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
280 wake_up(&trace_wait);
283 static int __init set_buf_size(char *str)
285 unsigned long buf_size;
290 ret = strict_strtoul(str, 0, &buf_size);
291 /* nr_entries can not be zero */
292 if (ret < 0 || buf_size == 0)
294 trace_buf_size = buf_size;
297 __setup("trace_buf_size=", set_buf_size);
299 unsigned long nsecs_to_usecs(unsigned long nsecs)
304 /* These must match the bit postions in trace_iterator_flags */
305 static const char *trace_options[] = {
332 * ftrace_max_lock is used to protect the swapping of buffers
333 * when taking a max snapshot. The buffers themselves are
334 * protected by per_cpu spinlocks. But the action of the swap
335 * needs its own lock.
337 * This is defined as a raw_spinlock_t in order to help
338 * with performance when lockdep debugging is enabled.
340 static raw_spinlock_t ftrace_max_lock =
341 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
344 * Copy the new maximum trace into the separate maximum-trace
345 * structure. (this way the maximum trace is permanently saved,
346 * for later retrieval via /debugfs/tracing/latency_trace)
349 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
351 struct trace_array_cpu *data = tr->data[cpu];
354 max_tr.time_start = data->preempt_timestamp;
356 data = max_tr.data[cpu];
357 data->saved_latency = tracing_max_latency;
359 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
360 data->pid = tsk->pid;
361 data->uid = task_uid(tsk);
362 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
363 data->policy = tsk->policy;
364 data->rt_priority = tsk->rt_priority;
366 /* record this tasks comm */
367 tracing_record_cmdline(tsk);
370 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
378 if (s->len <= s->readpos)
381 len = s->len - s->readpos;
384 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
394 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
399 if (s->len <= s->readpos)
402 len = s->len - s->readpos;
405 ret = memcpy(buf, s->buffer + s->readpos, cnt);
414 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
416 * @tsk: the task with the latency
417 * @cpu: The cpu that initiated the trace.
419 * Flip the buffers between the @tr and the max_tr and record information
420 * about which task was the cause of this latency.
423 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
425 struct ring_buffer *buf = tr->buffer;
427 WARN_ON_ONCE(!irqs_disabled());
428 __raw_spin_lock(&ftrace_max_lock);
430 tr->buffer = max_tr.buffer;
433 ftrace_disable_cpu();
434 ring_buffer_reset(tr->buffer);
437 __update_max_tr(tr, tsk, cpu);
438 __raw_spin_unlock(&ftrace_max_lock);
442 * update_max_tr_single - only copy one trace over, and reset the rest
444 * @tsk - task with the latency
445 * @cpu - the cpu of the buffer to copy.
447 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
450 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
454 WARN_ON_ONCE(!irqs_disabled());
455 __raw_spin_lock(&ftrace_max_lock);
457 ftrace_disable_cpu();
459 ring_buffer_reset(max_tr.buffer);
460 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
464 WARN_ON_ONCE(ret && ret != -EAGAIN);
466 __update_max_tr(tr, tsk, cpu);
467 __raw_spin_unlock(&ftrace_max_lock);
471 * register_tracer - register a tracer with the ftrace system.
472 * @type - the plugin for the tracer
474 * Register a new plugin tracer.
476 int register_tracer(struct tracer *type)
477 __releases(kernel_lock)
478 __acquires(kernel_lock)
485 pr_info("Tracer must have a name\n");
490 * When this gets called we hold the BKL which means that
491 * preemption is disabled. Various trace selftests however
492 * need to disable and enable preemption for successful tests.
493 * So we drop the BKL here and grab it after the tests again.
496 mutex_lock(&trace_types_lock);
498 tracing_selftest_running = true;
500 for (t = trace_types; t; t = t->next) {
501 if (strcmp(type->name, t->name) == 0) {
503 pr_info("Trace %s already registered\n",
511 type->set_flag = &dummy_set_flag;
513 type->flags = &dummy_tracer_flags;
515 if (!type->flags->opts)
516 type->flags->opts = dummy_tracer_opt;
517 if (!type->wait_pipe)
518 type->wait_pipe = default_wait_pipe;
521 #ifdef CONFIG_FTRACE_STARTUP_TEST
522 if (type->selftest && !tracing_selftest_disabled) {
523 struct tracer *saved_tracer = current_trace;
524 struct trace_array *tr = &global_trace;
528 * Run a selftest on this tracer.
529 * Here we reset the trace buffer, and set the current
530 * tracer to be this tracer. The tracer can then run some
531 * internal tracing to verify that everything is in order.
532 * If we fail, we do not register this tracer.
534 for_each_tracing_cpu(i)
535 tracing_reset(tr, i);
537 current_trace = type;
538 /* the test is responsible for initializing and enabling */
539 pr_info("Testing tracer %s: ", type->name);
540 ret = type->selftest(type, tr);
541 /* the test is responsible for resetting too */
542 current_trace = saved_tracer;
544 printk(KERN_CONT "FAILED!\n");
547 /* Only reset on passing, to avoid touching corrupted buffers */
548 for_each_tracing_cpu(i)
549 tracing_reset(tr, i);
551 printk(KERN_CONT "PASSED\n");
555 type->next = trace_types;
557 len = strlen(type->name);
558 if (len > max_tracer_type_len)
559 max_tracer_type_len = len;
562 tracing_selftest_running = false;
563 mutex_unlock(&trace_types_lock);
565 if (ret || !default_bootup_tracer)
568 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
571 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
572 /* Do we want this tracer to start on bootup? */
573 tracing_set_tracer(type->name);
574 default_bootup_tracer = NULL;
575 /* disable other selftests, since this will break it. */
576 tracing_selftest_disabled = 1;
577 #ifdef CONFIG_FTRACE_STARTUP_TEST
578 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
587 void unregister_tracer(struct tracer *type)
592 mutex_lock(&trace_types_lock);
593 for (t = &trace_types; *t; t = &(*t)->next) {
597 pr_info("Trace %s not registered\n", type->name);
603 if (type == current_trace && tracer_enabled) {
606 if (current_trace->stop)
607 current_trace->stop(&global_trace);
608 current_trace = &nop_trace;
611 if (strlen(type->name) != max_tracer_type_len)
614 max_tracer_type_len = 0;
615 for (t = &trace_types; *t; t = &(*t)->next) {
616 len = strlen((*t)->name);
617 if (len > max_tracer_type_len)
618 max_tracer_type_len = len;
621 mutex_unlock(&trace_types_lock);
624 void tracing_reset(struct trace_array *tr, int cpu)
626 ftrace_disable_cpu();
627 ring_buffer_reset_cpu(tr->buffer, cpu);
631 void tracing_reset_online_cpus(struct trace_array *tr)
635 tr->time_start = ftrace_now(tr->cpu);
637 for_each_online_cpu(cpu)
638 tracing_reset(tr, cpu);
641 #define SAVED_CMDLINES 128
642 #define NO_CMDLINE_MAP UINT_MAX
643 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
644 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
645 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
646 static int cmdline_idx;
647 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
649 /* temporary disable recording */
650 static atomic_t trace_record_cmdline_disabled __read_mostly;
652 static void trace_init_cmdlines(void)
654 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
655 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
659 static int trace_stop_count;
660 static DEFINE_SPINLOCK(tracing_start_lock);
663 * ftrace_off_permanent - disable all ftrace code permanently
665 * This should only be called when a serious anomally has
666 * been detected. This will turn off the function tracing,
667 * ring buffers, and other tracing utilites. It takes no
668 * locks and can be called from any context.
670 void ftrace_off_permanent(void)
672 tracing_disabled = 1;
674 tracing_off_permanent();
678 * tracing_start - quick start of the tracer
680 * If tracing is enabled but was stopped by tracing_stop,
681 * this will start the tracer back up.
683 void tracing_start(void)
685 struct ring_buffer *buffer;
688 if (tracing_disabled)
691 spin_lock_irqsave(&tracing_start_lock, flags);
692 if (--trace_stop_count) {
693 if (trace_stop_count < 0) {
694 /* Someone screwed up their debugging */
696 trace_stop_count = 0;
702 buffer = global_trace.buffer;
704 ring_buffer_record_enable(buffer);
706 buffer = max_tr.buffer;
708 ring_buffer_record_enable(buffer);
712 spin_unlock_irqrestore(&tracing_start_lock, flags);
716 * tracing_stop - quick stop of the tracer
718 * Light weight way to stop tracing. Use in conjunction with
721 void tracing_stop(void)
723 struct ring_buffer *buffer;
727 spin_lock_irqsave(&tracing_start_lock, flags);
728 if (trace_stop_count++)
731 buffer = global_trace.buffer;
733 ring_buffer_record_disable(buffer);
735 buffer = max_tr.buffer;
737 ring_buffer_record_disable(buffer);
740 spin_unlock_irqrestore(&tracing_start_lock, flags);
743 void trace_stop_cmdline_recording(void);
745 static void trace_save_cmdline(struct task_struct *tsk)
749 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
753 * It's not the end of the world if we don't get
754 * the lock, but we also don't want to spin
755 * nor do we want to disable interrupts,
756 * so if we miss here, then better luck next time.
758 if (!__raw_spin_trylock(&trace_cmdline_lock))
761 idx = map_pid_to_cmdline[tsk->pid];
762 if (idx == NO_CMDLINE_MAP) {
763 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
766 * Check whether the cmdline buffer at idx has a pid
767 * mapped. We are going to overwrite that entry so we
768 * need to clear the map_pid_to_cmdline. Otherwise we
769 * would read the new comm for the old pid.
771 pid = map_cmdline_to_pid[idx];
772 if (pid != NO_CMDLINE_MAP)
773 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
775 map_cmdline_to_pid[idx] = tsk->pid;
776 map_pid_to_cmdline[tsk->pid] = idx;
781 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
783 __raw_spin_unlock(&trace_cmdline_lock);
786 void trace_find_cmdline(int pid, char comm[])
791 strcpy(comm, "<idle>");
795 if (pid > PID_MAX_DEFAULT) {
796 strcpy(comm, "<...>");
800 __raw_spin_lock(&trace_cmdline_lock);
801 map = map_pid_to_cmdline[pid];
802 if (map != NO_CMDLINE_MAP)
803 strcpy(comm, saved_cmdlines[map]);
805 strcpy(comm, "<...>");
807 __raw_spin_unlock(&trace_cmdline_lock);
810 void tracing_record_cmdline(struct task_struct *tsk)
812 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
816 trace_save_cmdline(tsk);
820 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
823 struct task_struct *tsk = current;
825 entry->preempt_count = pc & 0xff;
826 entry->pid = (tsk) ? tsk->pid : 0;
827 entry->tgid = (tsk) ? tsk->tgid : 0;
829 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
830 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
832 TRACE_FLAG_IRQS_NOSUPPORT |
834 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
835 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
836 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
839 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
842 unsigned long flags, int pc)
844 struct ring_buffer_event *event;
846 event = ring_buffer_lock_reserve(tr->buffer, len);
848 struct trace_entry *ent = ring_buffer_event_data(event);
850 tracing_generic_entry_update(ent, flags, pc);
856 static void ftrace_trace_stack(struct trace_array *tr,
857 unsigned long flags, int skip, int pc);
858 static void ftrace_trace_userstack(struct trace_array *tr,
859 unsigned long flags, int pc);
861 static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
862 struct ring_buffer_event *event,
863 unsigned long flags, int pc,
866 ring_buffer_unlock_commit(tr->buffer, event);
868 ftrace_trace_stack(tr, flags, 6, pc);
869 ftrace_trace_userstack(tr, flags, pc);
875 void trace_buffer_unlock_commit(struct trace_array *tr,
876 struct ring_buffer_event *event,
877 unsigned long flags, int pc)
879 __trace_buffer_unlock_commit(tr, event, flags, pc, 1);
882 struct ring_buffer_event *
883 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
884 unsigned long flags, int pc)
886 return trace_buffer_lock_reserve(&global_trace,
887 type, len, flags, pc);
890 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
891 unsigned long flags, int pc)
893 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
896 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
897 unsigned long flags, int pc)
899 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
902 void trace_current_buffer_discard_commit(struct ring_buffer_event *event)
904 ring_buffer_discard_commit(global_trace.buffer, event);
908 trace_function(struct trace_array *tr,
909 unsigned long ip, unsigned long parent_ip, unsigned long flags,
912 struct ftrace_event_call *call = &event_function;
913 struct ring_buffer_event *event;
914 struct ftrace_entry *entry;
916 /* If we are reading the ring buffer, don't trace */
917 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
920 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
924 entry = ring_buffer_event_data(event);
926 entry->parent_ip = parent_ip;
928 if (!filter_check_discard(call, entry, tr->buffer, event))
929 ring_buffer_unlock_commit(tr->buffer, event);
932 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
933 static int __trace_graph_entry(struct trace_array *tr,
934 struct ftrace_graph_ent *trace,
938 struct ftrace_event_call *call = &event_funcgraph_entry;
939 struct ring_buffer_event *event;
940 struct ftrace_graph_ent_entry *entry;
942 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
945 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
946 sizeof(*entry), flags, pc);
949 entry = ring_buffer_event_data(event);
950 entry->graph_ent = *trace;
951 if (!filter_current_check_discard(call, entry, event))
952 ring_buffer_unlock_commit(global_trace.buffer, event);
957 static void __trace_graph_return(struct trace_array *tr,
958 struct ftrace_graph_ret *trace,
962 struct ftrace_event_call *call = &event_funcgraph_exit;
963 struct ring_buffer_event *event;
964 struct ftrace_graph_ret_entry *entry;
966 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
969 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
970 sizeof(*entry), flags, pc);
973 entry = ring_buffer_event_data(event);
975 if (!filter_current_check_discard(call, entry, event))
976 ring_buffer_unlock_commit(global_trace.buffer, event);
981 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
982 unsigned long ip, unsigned long parent_ip, unsigned long flags,
985 if (likely(!atomic_read(&data->disabled)))
986 trace_function(tr, ip, parent_ip, flags, pc);
989 static void __ftrace_trace_stack(struct trace_array *tr,
993 #ifdef CONFIG_STACKTRACE
994 struct ftrace_event_call *call = &event_kernel_stack;
995 struct ring_buffer_event *event;
996 struct stack_entry *entry;
997 struct stack_trace trace;
999 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
1000 sizeof(*entry), flags, pc);
1003 entry = ring_buffer_event_data(event);
1004 memset(&entry->caller, 0, sizeof(entry->caller));
1006 trace.nr_entries = 0;
1007 trace.max_entries = FTRACE_STACK_ENTRIES;
1009 trace.entries = entry->caller;
1011 save_stack_trace(&trace);
1012 if (!filter_check_discard(call, entry, tr->buffer, event))
1013 ring_buffer_unlock_commit(tr->buffer, event);
1017 static void ftrace_trace_stack(struct trace_array *tr,
1018 unsigned long flags,
1021 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1024 __ftrace_trace_stack(tr, flags, skip, pc);
1027 void __trace_stack(struct trace_array *tr,
1028 unsigned long flags,
1031 __ftrace_trace_stack(tr, flags, skip, pc);
1034 static void ftrace_trace_userstack(struct trace_array *tr,
1035 unsigned long flags, int pc)
1037 #ifdef CONFIG_STACKTRACE
1038 struct ftrace_event_call *call = &event_user_stack;
1039 struct ring_buffer_event *event;
1040 struct userstack_entry *entry;
1041 struct stack_trace trace;
1043 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1046 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1047 sizeof(*entry), flags, pc);
1050 entry = ring_buffer_event_data(event);
1052 memset(&entry->caller, 0, sizeof(entry->caller));
1054 trace.nr_entries = 0;
1055 trace.max_entries = FTRACE_STACK_ENTRIES;
1057 trace.entries = entry->caller;
1059 save_stack_trace_user(&trace);
1060 if (!filter_check_discard(call, entry, tr->buffer, event))
1061 ring_buffer_unlock_commit(tr->buffer, event);
1066 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1068 ftrace_trace_userstack(tr, flags, preempt_count());
1073 ftrace_trace_special(void *__tr,
1074 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1077 struct ring_buffer_event *event;
1078 struct trace_array *tr = __tr;
1079 struct special_entry *entry;
1081 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1082 sizeof(*entry), 0, pc);
1085 entry = ring_buffer_event_data(event);
1089 trace_buffer_unlock_commit(tr, event, 0, pc);
1093 __trace_special(void *__tr, void *__data,
1094 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1096 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1100 tracing_sched_switch_trace(struct trace_array *tr,
1101 struct task_struct *prev,
1102 struct task_struct *next,
1103 unsigned long flags, int pc)
1105 struct ftrace_event_call *call = &event_context_switch;
1106 struct ring_buffer_event *event;
1107 struct ctx_switch_entry *entry;
1109 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1110 sizeof(*entry), flags, pc);
1113 entry = ring_buffer_event_data(event);
1114 entry->prev_pid = prev->pid;
1115 entry->prev_prio = prev->prio;
1116 entry->prev_state = prev->state;
1117 entry->next_pid = next->pid;
1118 entry->next_prio = next->prio;
1119 entry->next_state = next->state;
1120 entry->next_cpu = task_cpu(next);
1122 if (!filter_check_discard(call, entry, tr->buffer, event))
1123 trace_buffer_unlock_commit(tr, event, flags, pc);
1127 tracing_sched_wakeup_trace(struct trace_array *tr,
1128 struct task_struct *wakee,
1129 struct task_struct *curr,
1130 unsigned long flags, int pc)
1132 struct ftrace_event_call *call = &event_wakeup;
1133 struct ring_buffer_event *event;
1134 struct ctx_switch_entry *entry;
1136 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1137 sizeof(*entry), flags, pc);
1140 entry = ring_buffer_event_data(event);
1141 entry->prev_pid = curr->pid;
1142 entry->prev_prio = curr->prio;
1143 entry->prev_state = curr->state;
1144 entry->next_pid = wakee->pid;
1145 entry->next_prio = wakee->prio;
1146 entry->next_state = wakee->state;
1147 entry->next_cpu = task_cpu(wakee);
1149 if (!filter_check_discard(call, entry, tr->buffer, event))
1150 ring_buffer_unlock_commit(tr->buffer, event);
1151 ftrace_trace_stack(tr, flags, 6, pc);
1152 ftrace_trace_userstack(tr, flags, pc);
1156 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1158 struct trace_array *tr = &global_trace;
1159 struct trace_array_cpu *data;
1160 unsigned long flags;
1164 if (tracing_disabled)
1167 pc = preempt_count();
1168 local_irq_save(flags);
1169 cpu = raw_smp_processor_id();
1170 data = tr->data[cpu];
1172 if (likely(atomic_inc_return(&data->disabled) == 1))
1173 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1175 atomic_dec(&data->disabled);
1176 local_irq_restore(flags);
1179 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1180 int trace_graph_entry(struct ftrace_graph_ent *trace)
1182 struct trace_array *tr = &global_trace;
1183 struct trace_array_cpu *data;
1184 unsigned long flags;
1190 if (!ftrace_trace_task(current))
1193 if (!ftrace_graph_addr(trace->func))
1196 local_irq_save(flags);
1197 cpu = raw_smp_processor_id();
1198 data = tr->data[cpu];
1199 disabled = atomic_inc_return(&data->disabled);
1200 if (likely(disabled == 1)) {
1201 pc = preempt_count();
1202 ret = __trace_graph_entry(tr, trace, flags, pc);
1206 /* Only do the atomic if it is not already set */
1207 if (!test_tsk_trace_graph(current))
1208 set_tsk_trace_graph(current);
1210 atomic_dec(&data->disabled);
1211 local_irq_restore(flags);
1216 void trace_graph_return(struct ftrace_graph_ret *trace)
1218 struct trace_array *tr = &global_trace;
1219 struct trace_array_cpu *data;
1220 unsigned long flags;
1225 local_irq_save(flags);
1226 cpu = raw_smp_processor_id();
1227 data = tr->data[cpu];
1228 disabled = atomic_inc_return(&data->disabled);
1229 if (likely(disabled == 1)) {
1230 pc = preempt_count();
1231 __trace_graph_return(tr, trace, flags, pc);
1234 clear_tsk_trace_graph(current);
1235 atomic_dec(&data->disabled);
1236 local_irq_restore(flags);
1238 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1242 * trace_vbprintk - write binary msg to tracing buffer
1245 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1247 static raw_spinlock_t trace_buf_lock =
1248 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1249 static u32 trace_buf[TRACE_BUF_SIZE];
1251 struct ftrace_event_call *call = &event_bprint;
1252 struct ring_buffer_event *event;
1253 struct trace_array *tr = &global_trace;
1254 struct trace_array_cpu *data;
1255 struct bprint_entry *entry;
1256 unsigned long flags;
1258 int cpu, len = 0, size, pc;
1260 if (unlikely(tracing_selftest_running || tracing_disabled))
1263 /* Don't pollute graph traces with trace_vprintk internals */
1264 pause_graph_tracing();
1266 pc = preempt_count();
1267 resched = ftrace_preempt_disable();
1268 cpu = raw_smp_processor_id();
1269 data = tr->data[cpu];
1271 if (unlikely(atomic_read(&data->disabled)))
1274 /* Lockdep uses trace_printk for lock tracing */
1275 local_irq_save(flags);
1276 __raw_spin_lock(&trace_buf_lock);
1277 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1279 if (len > TRACE_BUF_SIZE || len < 0)
1282 size = sizeof(*entry) + sizeof(u32) * len;
1283 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1286 entry = ring_buffer_event_data(event);
1290 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1291 if (!filter_check_discard(call, entry, tr->buffer, event))
1292 ring_buffer_unlock_commit(tr->buffer, event);
1295 __raw_spin_unlock(&trace_buf_lock);
1296 local_irq_restore(flags);
1299 ftrace_preempt_enable(resched);
1300 unpause_graph_tracing();
1304 EXPORT_SYMBOL_GPL(trace_vbprintk);
1306 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1308 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1309 static char trace_buf[TRACE_BUF_SIZE];
1311 struct ftrace_event_call *call = &event_print;
1312 struct ring_buffer_event *event;
1313 struct trace_array *tr = &global_trace;
1314 struct trace_array_cpu *data;
1315 int cpu, len = 0, size, pc;
1316 struct print_entry *entry;
1317 unsigned long irq_flags;
1319 if (tracing_disabled || tracing_selftest_running)
1322 pc = preempt_count();
1323 preempt_disable_notrace();
1324 cpu = raw_smp_processor_id();
1325 data = tr->data[cpu];
1327 if (unlikely(atomic_read(&data->disabled)))
1330 pause_graph_tracing();
1331 raw_local_irq_save(irq_flags);
1332 __raw_spin_lock(&trace_buf_lock);
1333 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1335 len = min(len, TRACE_BUF_SIZE-1);
1338 size = sizeof(*entry) + len + 1;
1339 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1342 entry = ring_buffer_event_data(event);
1345 memcpy(&entry->buf, trace_buf, len);
1346 entry->buf[len] = 0;
1347 if (!filter_check_discard(call, entry, tr->buffer, event))
1348 ring_buffer_unlock_commit(tr->buffer, event);
1351 __raw_spin_unlock(&trace_buf_lock);
1352 raw_local_irq_restore(irq_flags);
1353 unpause_graph_tracing();
1355 preempt_enable_notrace();
1359 EXPORT_SYMBOL_GPL(trace_vprintk);
1361 enum trace_file_type {
1362 TRACE_FILE_LAT_FMT = 1,
1363 TRACE_FILE_ANNOTATE = 2,
1366 static void trace_iterator_increment(struct trace_iterator *iter)
1368 /* Don't allow ftrace to trace into the ring buffers */
1369 ftrace_disable_cpu();
1372 if (iter->buffer_iter[iter->cpu])
1373 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1375 ftrace_enable_cpu();
1378 static struct trace_entry *
1379 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1381 struct ring_buffer_event *event;
1382 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1384 /* Don't allow ftrace to trace into the ring buffers */
1385 ftrace_disable_cpu();
1388 event = ring_buffer_iter_peek(buf_iter, ts);
1390 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1392 ftrace_enable_cpu();
1394 return event ? ring_buffer_event_data(event) : NULL;
1397 static struct trace_entry *
1398 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1400 struct ring_buffer *buffer = iter->tr->buffer;
1401 struct trace_entry *ent, *next = NULL;
1402 int cpu_file = iter->cpu_file;
1403 u64 next_ts = 0, ts;
1408 * If we are in a per_cpu trace file, don't bother by iterating over
1409 * all cpu and peek directly.
1411 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1412 if (ring_buffer_empty_cpu(buffer, cpu_file))
1414 ent = peek_next_entry(iter, cpu_file, ent_ts);
1416 *ent_cpu = cpu_file;
1421 for_each_tracing_cpu(cpu) {
1423 if (ring_buffer_empty_cpu(buffer, cpu))
1426 ent = peek_next_entry(iter, cpu, &ts);
1429 * Pick the entry with the smallest timestamp:
1431 if (ent && (!next || ts < next_ts)) {
1439 *ent_cpu = next_cpu;
1447 /* Find the next real entry, without updating the iterator itself */
1448 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1449 int *ent_cpu, u64 *ent_ts)
1451 return __find_next_entry(iter, ent_cpu, ent_ts);
1454 /* Find the next real entry, and increment the iterator to the next entry */
1455 static void *find_next_entry_inc(struct trace_iterator *iter)
1457 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1460 trace_iterator_increment(iter);
1462 return iter->ent ? iter : NULL;
1465 static void trace_consume(struct trace_iterator *iter)
1467 /* Don't allow ftrace to trace into the ring buffers */
1468 ftrace_disable_cpu();
1469 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1470 ftrace_enable_cpu();
1473 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1475 struct trace_iterator *iter = m->private;
1481 /* can't go backwards */
1486 ent = find_next_entry_inc(iter);
1490 while (ent && iter->idx < i)
1491 ent = find_next_entry_inc(iter);
1499 * No necessary locking here. The worst thing which can
1500 * happen is loosing events consumed at the same time
1501 * by a trace_pipe reader.
1502 * Other than that, we don't risk to crash the ring buffer
1503 * because it serializes the readers.
1505 * The current tracer is copied to avoid a global locking
1508 static void *s_start(struct seq_file *m, loff_t *pos)
1510 struct trace_iterator *iter = m->private;
1511 static struct tracer *old_tracer;
1512 int cpu_file = iter->cpu_file;
1517 /* copy the tracer to avoid using a global lock all around */
1518 mutex_lock(&trace_types_lock);
1519 if (unlikely(old_tracer != current_trace && current_trace)) {
1520 old_tracer = current_trace;
1521 *iter->trace = *current_trace;
1523 mutex_unlock(&trace_types_lock);
1525 atomic_inc(&trace_record_cmdline_disabled);
1527 if (*pos != iter->pos) {
1532 ftrace_disable_cpu();
1534 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1535 for_each_tracing_cpu(cpu)
1536 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1538 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1541 ftrace_enable_cpu();
1543 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1548 p = s_next(m, p, &l);
1554 static void s_stop(struct seq_file *m, void *p)
1556 atomic_dec(&trace_record_cmdline_disabled);
1559 static void print_lat_help_header(struct seq_file *m)
1561 seq_puts(m, "# _------=> CPU# \n");
1562 seq_puts(m, "# / _-----=> irqs-off \n");
1563 seq_puts(m, "# | / _----=> need-resched \n");
1564 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1565 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1566 seq_puts(m, "# |||| / \n");
1567 seq_puts(m, "# ||||| delay \n");
1568 seq_puts(m, "# cmd pid ||||| time | caller \n");
1569 seq_puts(m, "# \\ / ||||| \\ | / \n");
1572 static void print_func_help_header(struct seq_file *m)
1574 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1575 seq_puts(m, "# | | | | |\n");
1580 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1582 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1583 struct trace_array *tr = iter->tr;
1584 struct trace_array_cpu *data = tr->data[tr->cpu];
1585 struct tracer *type = current_trace;
1586 unsigned long total;
1587 unsigned long entries;
1588 const char *name = "preemption";
1593 entries = ring_buffer_entries(iter->tr->buffer);
1595 ring_buffer_overruns(iter->tr->buffer);
1597 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1599 seq_puts(m, "# -----------------------------------"
1600 "---------------------------------\n");
1601 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1602 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1603 nsecs_to_usecs(data->saved_latency),
1607 #if defined(CONFIG_PREEMPT_NONE)
1609 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1611 #elif defined(CONFIG_PREEMPT)
1616 /* These are reserved for later use */
1619 seq_printf(m, " #P:%d)\n", num_online_cpus());
1623 seq_puts(m, "# -----------------\n");
1624 seq_printf(m, "# | task: %.16s-%d "
1625 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1626 data->comm, data->pid, data->uid, data->nice,
1627 data->policy, data->rt_priority);
1628 seq_puts(m, "# -----------------\n");
1630 if (data->critical_start) {
1631 seq_puts(m, "# => started at: ");
1632 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1633 trace_print_seq(m, &iter->seq);
1634 seq_puts(m, "\n# => ended at: ");
1635 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1636 trace_print_seq(m, &iter->seq);
1643 static void test_cpu_buff_start(struct trace_iterator *iter)
1645 struct trace_seq *s = &iter->seq;
1647 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1650 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1653 if (cpumask_test_cpu(iter->cpu, iter->started))
1656 cpumask_set_cpu(iter->cpu, iter->started);
1658 /* Don't print started cpu buffer for the first entry of the trace */
1660 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1664 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1666 struct trace_seq *s = &iter->seq;
1667 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1668 struct trace_entry *entry;
1669 struct trace_event *event;
1673 test_cpu_buff_start(iter);
1675 event = ftrace_find_event(entry->type);
1677 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1678 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1679 if (!trace_print_lat_context(iter))
1682 if (!trace_print_context(iter))
1688 return event->trace(iter, sym_flags);
1690 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1693 return TRACE_TYPE_HANDLED;
1695 return TRACE_TYPE_PARTIAL_LINE;
1698 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1700 struct trace_seq *s = &iter->seq;
1701 struct trace_entry *entry;
1702 struct trace_event *event;
1706 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1707 if (!trace_seq_printf(s, "%d %d %llu ",
1708 entry->pid, iter->cpu, iter->ts))
1712 event = ftrace_find_event(entry->type);
1714 return event->raw(iter, 0);
1716 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1719 return TRACE_TYPE_HANDLED;
1721 return TRACE_TYPE_PARTIAL_LINE;
1724 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1726 struct trace_seq *s = &iter->seq;
1727 unsigned char newline = '\n';
1728 struct trace_entry *entry;
1729 struct trace_event *event;
1733 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1734 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1735 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1736 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1739 event = ftrace_find_event(entry->type);
1741 enum print_line_t ret = event->hex(iter, 0);
1742 if (ret != TRACE_TYPE_HANDLED)
1746 SEQ_PUT_FIELD_RET(s, newline);
1748 return TRACE_TYPE_HANDLED;
1751 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1753 struct trace_seq *s = &iter->seq;
1754 struct trace_entry *entry;
1755 struct trace_event *event;
1759 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1760 SEQ_PUT_FIELD_RET(s, entry->pid);
1761 SEQ_PUT_FIELD_RET(s, iter->cpu);
1762 SEQ_PUT_FIELD_RET(s, iter->ts);
1765 event = ftrace_find_event(entry->type);
1766 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1769 static int trace_empty(struct trace_iterator *iter)
1773 /* If we are looking at one CPU buffer, only check that one */
1774 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1775 cpu = iter->cpu_file;
1776 if (iter->buffer_iter[cpu]) {
1777 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1780 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1786 for_each_tracing_cpu(cpu) {
1787 if (iter->buffer_iter[cpu]) {
1788 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1791 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1799 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1801 enum print_line_t ret;
1803 if (iter->trace && iter->trace->print_line) {
1804 ret = iter->trace->print_line(iter);
1805 if (ret != TRACE_TYPE_UNHANDLED)
1809 if (iter->ent->type == TRACE_BPRINT &&
1810 trace_flags & TRACE_ITER_PRINTK &&
1811 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1812 return trace_print_bprintk_msg_only(iter);
1814 if (iter->ent->type == TRACE_PRINT &&
1815 trace_flags & TRACE_ITER_PRINTK &&
1816 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1817 return trace_print_printk_msg_only(iter);
1819 if (trace_flags & TRACE_ITER_BIN)
1820 return print_bin_fmt(iter);
1822 if (trace_flags & TRACE_ITER_HEX)
1823 return print_hex_fmt(iter);
1825 if (trace_flags & TRACE_ITER_RAW)
1826 return print_raw_fmt(iter);
1828 return print_trace_fmt(iter);
1831 static int s_show(struct seq_file *m, void *v)
1833 struct trace_iterator *iter = v;
1835 if (iter->ent == NULL) {
1837 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1840 if (iter->trace && iter->trace->print_header)
1841 iter->trace->print_header(m);
1842 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1843 /* print nothing if the buffers are empty */
1844 if (trace_empty(iter))
1846 print_trace_header(m, iter);
1847 if (!(trace_flags & TRACE_ITER_VERBOSE))
1848 print_lat_help_header(m);
1850 if (!(trace_flags & TRACE_ITER_VERBOSE))
1851 print_func_help_header(m);
1854 print_trace_line(iter);
1855 trace_print_seq(m, &iter->seq);
1861 static struct seq_operations tracer_seq_ops = {
1868 static struct trace_iterator *
1869 __tracing_open(struct inode *inode, struct file *file)
1871 long cpu_file = (long) inode->i_private;
1872 void *fail_ret = ERR_PTR(-ENOMEM);
1873 struct trace_iterator *iter;
1877 if (tracing_disabled)
1878 return ERR_PTR(-ENODEV);
1880 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1882 return ERR_PTR(-ENOMEM);
1885 * We make a copy of the current tracer to avoid concurrent
1886 * changes on it while we are reading.
1888 mutex_lock(&trace_types_lock);
1889 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1894 *iter->trace = *current_trace;
1896 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1899 cpumask_clear(iter->started);
1901 if (current_trace && current_trace->print_max)
1904 iter->tr = &global_trace;
1906 mutex_init(&iter->mutex);
1907 iter->cpu_file = cpu_file;
1909 /* Notify the tracer early; before we stop tracing. */
1910 if (iter->trace && iter->trace->open)
1911 iter->trace->open(iter);
1913 /* Annotate start of buffers if we had overruns */
1914 if (ring_buffer_overruns(iter->tr->buffer))
1915 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1917 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1918 for_each_tracing_cpu(cpu) {
1920 iter->buffer_iter[cpu] =
1921 ring_buffer_read_start(iter->tr->buffer, cpu);
1924 cpu = iter->cpu_file;
1925 iter->buffer_iter[cpu] =
1926 ring_buffer_read_start(iter->tr->buffer, cpu);
1929 /* TODO stop tracer */
1930 ret = seq_open(file, &tracer_seq_ops);
1932 fail_ret = ERR_PTR(ret);
1936 m = file->private_data;
1939 /* stop the trace while dumping */
1942 mutex_unlock(&trace_types_lock);
1947 for_each_tracing_cpu(cpu) {
1948 if (iter->buffer_iter[cpu])
1949 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1951 free_cpumask_var(iter->started);
1953 mutex_unlock(&trace_types_lock);
1960 int tracing_open_generic(struct inode *inode, struct file *filp)
1962 if (tracing_disabled)
1965 filp->private_data = inode->i_private;
1969 static 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;
1975 if (!(file->f_mode & FMODE_READ))
1980 mutex_lock(&trace_types_lock);
1981 for_each_tracing_cpu(cpu) {
1982 if (iter->buffer_iter[cpu])
1983 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1986 if (iter->trace && iter->trace->close)
1987 iter->trace->close(iter);
1989 /* reenable tracing if it was previously enabled */
1991 mutex_unlock(&trace_types_lock);
1993 seq_release(inode, file);
1994 mutex_destroy(&iter->mutex);
1995 free_cpumask_var(iter->started);
2001 static int tracing_open(struct inode *inode, struct file *file)
2003 struct trace_iterator *iter;
2006 /* If this file was open for write, then erase contents */
2007 if ((file->f_mode & FMODE_WRITE) &&
2008 !(file->f_flags & O_APPEND)) {
2009 long cpu = (long) inode->i_private;
2011 if (cpu == TRACE_PIPE_ALL_CPU)
2012 tracing_reset_online_cpus(&global_trace);
2014 tracing_reset(&global_trace, cpu);
2017 if (file->f_mode & FMODE_READ) {
2018 iter = __tracing_open(inode, file);
2020 ret = PTR_ERR(iter);
2021 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2022 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2028 t_next(struct seq_file *m, void *v, loff_t *pos)
2030 struct tracer *t = m->private;
2042 static void *t_start(struct seq_file *m, loff_t *pos)
2044 struct tracer *t = m->private;
2047 mutex_lock(&trace_types_lock);
2048 for (; t && l < *pos; t = t_next(m, t, &l))
2054 static void t_stop(struct seq_file *m, void *p)
2056 mutex_unlock(&trace_types_lock);
2059 static int t_show(struct seq_file *m, void *v)
2061 struct tracer *t = v;
2066 seq_printf(m, "%s", t->name);
2075 static struct seq_operations show_traces_seq_ops = {
2082 static int show_traces_open(struct inode *inode, struct file *file)
2086 if (tracing_disabled)
2089 ret = seq_open(file, &show_traces_seq_ops);
2091 struct seq_file *m = file->private_data;
2092 m->private = trace_types;
2099 tracing_write_stub(struct file *filp, const char __user *ubuf,
2100 size_t count, loff_t *ppos)
2105 static const struct file_operations tracing_fops = {
2106 .open = tracing_open,
2108 .write = tracing_write_stub,
2109 .llseek = seq_lseek,
2110 .release = tracing_release,
2113 static const struct file_operations show_traces_fops = {
2114 .open = show_traces_open,
2116 .release = seq_release,
2120 * Only trace on a CPU if the bitmask is set:
2122 static cpumask_var_t tracing_cpumask;
2125 * The tracer itself will not take this lock, but still we want
2126 * to provide a consistent cpumask to user-space:
2128 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2131 * Temporary storage for the character representation of the
2132 * CPU bitmask (and one more byte for the newline):
2134 static char mask_str[NR_CPUS + 1];
2137 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2138 size_t count, loff_t *ppos)
2142 mutex_lock(&tracing_cpumask_update_lock);
2144 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2145 if (count - len < 2) {
2149 len += sprintf(mask_str + len, "\n");
2150 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2153 mutex_unlock(&tracing_cpumask_update_lock);
2159 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2160 size_t count, loff_t *ppos)
2163 cpumask_var_t tracing_cpumask_new;
2165 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2168 mutex_lock(&tracing_cpumask_update_lock);
2169 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2173 local_irq_disable();
2174 __raw_spin_lock(&ftrace_max_lock);
2175 for_each_tracing_cpu(cpu) {
2177 * Increase/decrease the disabled counter if we are
2178 * about to flip a bit in the cpumask:
2180 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2181 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2182 atomic_inc(&global_trace.data[cpu]->disabled);
2184 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2185 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2186 atomic_dec(&global_trace.data[cpu]->disabled);
2189 __raw_spin_unlock(&ftrace_max_lock);
2192 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2194 mutex_unlock(&tracing_cpumask_update_lock);
2195 free_cpumask_var(tracing_cpumask_new);
2200 mutex_unlock(&tracing_cpumask_update_lock);
2201 free_cpumask_var(tracing_cpumask);
2206 static const struct file_operations tracing_cpumask_fops = {
2207 .open = tracing_open_generic,
2208 .read = tracing_cpumask_read,
2209 .write = tracing_cpumask_write,
2213 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2214 size_t cnt, loff_t *ppos)
2216 struct tracer_opt *trace_opts;
2224 /* calculate max size */
2225 for (i = 0; trace_options[i]; i++) {
2226 len += strlen(trace_options[i]);
2227 len += 3; /* "no" and newline */
2230 mutex_lock(&trace_types_lock);
2231 tracer_flags = current_trace->flags->val;
2232 trace_opts = current_trace->flags->opts;
2235 * Increase the size with names of options specific
2236 * of the current tracer.
2238 for (i = 0; trace_opts[i].name; i++) {
2239 len += strlen(trace_opts[i].name);
2240 len += 3; /* "no" and newline */
2243 /* +2 for \n and \0 */
2244 buf = kmalloc(len + 2, GFP_KERNEL);
2246 mutex_unlock(&trace_types_lock);
2250 for (i = 0; trace_options[i]; i++) {
2251 if (trace_flags & (1 << i))
2252 r += sprintf(buf + r, "%s\n", trace_options[i]);
2254 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2257 for (i = 0; trace_opts[i].name; i++) {
2258 if (tracer_flags & trace_opts[i].bit)
2259 r += sprintf(buf + r, "%s\n",
2260 trace_opts[i].name);
2262 r += sprintf(buf + r, "no%s\n",
2263 trace_opts[i].name);
2265 mutex_unlock(&trace_types_lock);
2267 WARN_ON(r >= len + 2);
2269 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2275 /* Try to assign a tracer specific option */
2276 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2278 struct tracer_flags *trace_flags = trace->flags;
2279 struct tracer_opt *opts = NULL;
2283 for (i = 0; trace_flags->opts[i].name; i++) {
2284 opts = &trace_flags->opts[i];
2285 len = strlen(opts->name);
2287 if (strncmp(cmp, opts->name, len) == 0) {
2288 ret = trace->set_flag(trace_flags->val,
2294 if (!trace_flags->opts[i].name)
2297 /* Refused to handle */
2302 trace_flags->val &= ~opts->bit;
2304 trace_flags->val |= opts->bit;
2309 static void set_tracer_flags(unsigned int mask, int enabled)
2311 /* do nothing if flag is already set */
2312 if (!!(trace_flags & mask) == !!enabled)
2316 trace_flags |= mask;
2318 trace_flags &= ~mask;
2320 if (mask == TRACE_ITER_GLOBAL_CLK) {
2324 func = trace_clock_global;
2326 func = trace_clock_local;
2328 mutex_lock(&trace_types_lock);
2329 ring_buffer_set_clock(global_trace.buffer, func);
2332 ring_buffer_set_clock(max_tr.buffer, func);
2333 mutex_unlock(&trace_types_lock);
2338 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2339 size_t cnt, loff_t *ppos)
2347 if (cnt >= sizeof(buf))
2350 if (copy_from_user(&buf, ubuf, cnt))
2355 if (strncmp(buf, "no", 2) == 0) {
2360 for (i = 0; trace_options[i]; i++) {
2361 int len = strlen(trace_options[i]);
2363 if (strncmp(cmp, trace_options[i], len) == 0) {
2364 set_tracer_flags(1 << i, !neg);
2369 /* If no option could be set, test the specific tracer options */
2370 if (!trace_options[i]) {
2371 mutex_lock(&trace_types_lock);
2372 ret = set_tracer_option(current_trace, cmp, neg);
2373 mutex_unlock(&trace_types_lock);
2383 static const struct file_operations tracing_iter_fops = {
2384 .open = tracing_open_generic,
2385 .read = tracing_trace_options_read,
2386 .write = tracing_trace_options_write,
2389 static const char readme_msg[] =
2390 "tracing mini-HOWTO:\n\n"
2392 "# mount -t debugfs nodev /debug\n\n"
2393 "# cat /debug/tracing/available_tracers\n"
2394 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2395 "# cat /debug/tracing/current_tracer\n"
2397 "# echo sched_switch > /debug/tracing/current_tracer\n"
2398 "# cat /debug/tracing/current_tracer\n"
2400 "# cat /debug/tracing/trace_options\n"
2401 "noprint-parent nosym-offset nosym-addr noverbose\n"
2402 "# echo print-parent > /debug/tracing/trace_options\n"
2403 "# echo 1 > /debug/tracing/tracing_enabled\n"
2404 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2405 "echo 0 > /debug/tracing/tracing_enabled\n"
2409 tracing_readme_read(struct file *filp, char __user *ubuf,
2410 size_t cnt, loff_t *ppos)
2412 return simple_read_from_buffer(ubuf, cnt, ppos,
2413 readme_msg, strlen(readme_msg));
2416 static const struct file_operations tracing_readme_fops = {
2417 .open = tracing_open_generic,
2418 .read = tracing_readme_read,
2422 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2423 size_t cnt, loff_t *ppos)
2428 r = sprintf(buf, "%u\n", tracer_enabled);
2429 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2433 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2434 size_t cnt, loff_t *ppos)
2436 struct trace_array *tr = filp->private_data;
2441 if (cnt >= sizeof(buf))
2444 if (copy_from_user(&buf, ubuf, cnt))
2449 ret = strict_strtoul(buf, 10, &val);
2455 mutex_lock(&trace_types_lock);
2456 if (tracer_enabled ^ val) {
2459 if (current_trace->start)
2460 current_trace->start(tr);
2465 if (current_trace->stop)
2466 current_trace->stop(tr);
2469 mutex_unlock(&trace_types_lock);
2477 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2478 size_t cnt, loff_t *ppos)
2480 char buf[max_tracer_type_len+2];
2483 mutex_lock(&trace_types_lock);
2485 r = sprintf(buf, "%s\n", current_trace->name);
2487 r = sprintf(buf, "\n");
2488 mutex_unlock(&trace_types_lock);
2490 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2493 int tracer_init(struct tracer *t, struct trace_array *tr)
2495 tracing_reset_online_cpus(tr);
2499 static int tracing_resize_ring_buffer(unsigned long size)
2504 * If kernel or user changes the size of the ring buffer
2505 * we use the size that was given, and we can forget about
2506 * expanding it later.
2508 ring_buffer_expanded = 1;
2510 ret = ring_buffer_resize(global_trace.buffer, size);
2514 ret = ring_buffer_resize(max_tr.buffer, size);
2518 r = ring_buffer_resize(global_trace.buffer,
2519 global_trace.entries);
2522 * AARGH! We are left with different
2523 * size max buffer!!!!
2524 * The max buffer is our "snapshot" buffer.
2525 * When a tracer needs a snapshot (one of the
2526 * latency tracers), it swaps the max buffer
2527 * with the saved snap shot. We succeeded to
2528 * update the size of the main buffer, but failed to
2529 * update the size of the max buffer. But when we tried
2530 * to reset the main buffer to the original size, we
2531 * failed there too. This is very unlikely to
2532 * happen, but if it does, warn and kill all
2536 tracing_disabled = 1;
2541 global_trace.entries = size;
2547 * tracing_update_buffers - used by tracing facility to expand ring buffers
2549 * To save on memory when the tracing is never used on a system with it
2550 * configured in. The ring buffers are set to a minimum size. But once
2551 * a user starts to use the tracing facility, then they need to grow
2552 * to their default size.
2554 * This function is to be called when a tracer is about to be used.
2556 int tracing_update_buffers(void)
2560 mutex_lock(&trace_types_lock);
2561 if (!ring_buffer_expanded)
2562 ret = tracing_resize_ring_buffer(trace_buf_size);
2563 mutex_unlock(&trace_types_lock);
2568 struct trace_option_dentry;
2570 static struct trace_option_dentry *
2571 create_trace_option_files(struct tracer *tracer);
2574 destroy_trace_option_files(struct trace_option_dentry *topts);
2576 static int tracing_set_tracer(const char *buf)
2578 static struct trace_option_dentry *topts;
2579 struct trace_array *tr = &global_trace;
2583 mutex_lock(&trace_types_lock);
2585 if (!ring_buffer_expanded) {
2586 ret = tracing_resize_ring_buffer(trace_buf_size);
2592 for (t = trace_types; t; t = t->next) {
2593 if (strcmp(t->name, buf) == 0)
2600 if (t == current_trace)
2603 trace_branch_disable();
2604 if (current_trace && current_trace->reset)
2605 current_trace->reset(tr);
2607 destroy_trace_option_files(topts);
2611 topts = create_trace_option_files(current_trace);
2614 ret = tracer_init(t, tr);
2619 trace_branch_enable(tr);
2621 mutex_unlock(&trace_types_lock);
2627 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2628 size_t cnt, loff_t *ppos)
2630 char buf[max_tracer_type_len+1];
2637 if (cnt > max_tracer_type_len)
2638 cnt = max_tracer_type_len;
2640 if (copy_from_user(&buf, ubuf, cnt))
2645 /* strip ending whitespace. */
2646 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2649 err = tracing_set_tracer(buf);
2659 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2660 size_t cnt, loff_t *ppos)
2662 unsigned long *ptr = filp->private_data;
2666 r = snprintf(buf, sizeof(buf), "%ld\n",
2667 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2668 if (r > sizeof(buf))
2670 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2674 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2675 size_t cnt, loff_t *ppos)
2677 unsigned long *ptr = filp->private_data;
2682 if (cnt >= sizeof(buf))
2685 if (copy_from_user(&buf, ubuf, cnt))
2690 ret = strict_strtoul(buf, 10, &val);
2699 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2701 long cpu_file = (long) inode->i_private;
2702 struct trace_iterator *iter;
2705 if (tracing_disabled)
2708 mutex_lock(&trace_types_lock);
2710 /* We only allow one reader per cpu */
2711 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2712 if (!cpumask_empty(tracing_reader_cpumask)) {
2716 cpumask_setall(tracing_reader_cpumask);
2718 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2719 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2726 /* create a buffer to store the information to pass to userspace */
2727 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2734 * We make a copy of the current tracer to avoid concurrent
2735 * changes on it while we are reading.
2737 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2743 *iter->trace = *current_trace;
2745 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2750 /* trace pipe does not show start of buffer */
2751 cpumask_setall(iter->started);
2753 iter->cpu_file = cpu_file;
2754 iter->tr = &global_trace;
2755 mutex_init(&iter->mutex);
2756 filp->private_data = iter;
2758 if (iter->trace->pipe_open)
2759 iter->trace->pipe_open(iter);
2762 mutex_unlock(&trace_types_lock);
2768 mutex_unlock(&trace_types_lock);
2772 static int tracing_release_pipe(struct inode *inode, struct file *file)
2774 struct trace_iterator *iter = file->private_data;
2776 mutex_lock(&trace_types_lock);
2778 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2779 cpumask_clear(tracing_reader_cpumask);
2781 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2783 mutex_unlock(&trace_types_lock);
2785 free_cpumask_var(iter->started);
2786 mutex_destroy(&iter->mutex);
2794 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2796 struct trace_iterator *iter = filp->private_data;
2798 if (trace_flags & TRACE_ITER_BLOCK) {
2800 * Always select as readable when in blocking mode
2802 return POLLIN | POLLRDNORM;
2804 if (!trace_empty(iter))
2805 return POLLIN | POLLRDNORM;
2806 poll_wait(filp, &trace_wait, poll_table);
2807 if (!trace_empty(iter))
2808 return POLLIN | POLLRDNORM;
2815 void default_wait_pipe(struct trace_iterator *iter)
2819 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2821 if (trace_empty(iter))
2824 finish_wait(&trace_wait, &wait);
2828 * This is a make-shift waitqueue.
2829 * A tracer might use this callback on some rare cases:
2831 * 1) the current tracer might hold the runqueue lock when it wakes up
2832 * a reader, hence a deadlock (sched, function, and function graph tracers)
2833 * 2) the function tracers, trace all functions, we don't want
2834 * the overhead of calling wake_up and friends
2835 * (and tracing them too)
2837 * Anyway, this is really very primitive wakeup.
2839 void poll_wait_pipe(struct trace_iterator *iter)
2841 set_current_state(TASK_INTERRUPTIBLE);
2842 /* sleep for 100 msecs, and try again. */
2843 schedule_timeout(HZ / 10);
2846 /* Must be called with trace_types_lock mutex held. */
2847 static int tracing_wait_pipe(struct file *filp)
2849 struct trace_iterator *iter = filp->private_data;
2851 while (trace_empty(iter)) {
2853 if ((filp->f_flags & O_NONBLOCK)) {
2857 mutex_unlock(&iter->mutex);
2859 iter->trace->wait_pipe(iter);
2861 mutex_lock(&iter->mutex);
2863 if (signal_pending(current))
2867 * We block until we read something and tracing is disabled.
2868 * We still block if tracing is disabled, but we have never
2869 * read anything. This allows a user to cat this file, and
2870 * then enable tracing. But after we have read something,
2871 * we give an EOF when tracing is again disabled.
2873 * iter->pos will be 0 if we haven't read anything.
2875 if (!tracer_enabled && iter->pos)
2886 tracing_read_pipe(struct file *filp, char __user *ubuf,
2887 size_t cnt, loff_t *ppos)
2889 struct trace_iterator *iter = filp->private_data;
2890 static struct tracer *old_tracer;
2893 /* return any leftover data */
2894 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2898 trace_seq_init(&iter->seq);
2900 /* copy the tracer to avoid using a global lock all around */
2901 mutex_lock(&trace_types_lock);
2902 if (unlikely(old_tracer != current_trace && current_trace)) {
2903 old_tracer = current_trace;
2904 *iter->trace = *current_trace;
2906 mutex_unlock(&trace_types_lock);
2909 * Avoid more than one consumer on a single file descriptor
2910 * This is just a matter of traces coherency, the ring buffer itself
2913 mutex_lock(&iter->mutex);
2914 if (iter->trace->read) {
2915 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2921 sret = tracing_wait_pipe(filp);
2925 /* stop when tracing is finished */
2926 if (trace_empty(iter)) {
2931 if (cnt >= PAGE_SIZE)
2932 cnt = PAGE_SIZE - 1;
2934 /* reset all but tr, trace, and overruns */
2935 memset(&iter->seq, 0,
2936 sizeof(struct trace_iterator) -
2937 offsetof(struct trace_iterator, seq));
2940 while (find_next_entry_inc(iter) != NULL) {
2941 enum print_line_t ret;
2942 int len = iter->seq.len;
2944 ret = print_trace_line(iter);
2945 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2946 /* don't print partial lines */
2947 iter->seq.len = len;
2950 if (ret != TRACE_TYPE_NO_CONSUME)
2951 trace_consume(iter);
2953 if (iter->seq.len >= cnt)
2957 /* Now copy what we have to the user */
2958 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2959 if (iter->seq.readpos >= iter->seq.len)
2960 trace_seq_init(&iter->seq);
2963 * If there was nothing to send to user, inspite of consuming trace
2964 * entries, go back to wait for more entries.
2970 mutex_unlock(&iter->mutex);
2975 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2976 struct pipe_buffer *buf)
2978 __free_page(buf->page);
2981 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2984 __free_page(spd->pages[idx]);
2987 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2989 .map = generic_pipe_buf_map,
2990 .unmap = generic_pipe_buf_unmap,
2991 .confirm = generic_pipe_buf_confirm,
2992 .release = tracing_pipe_buf_release,
2993 .steal = generic_pipe_buf_steal,
2994 .get = generic_pipe_buf_get,
2998 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3003 /* Seq buffer is page-sized, exactly what we need. */
3005 count = iter->seq.len;
3006 ret = print_trace_line(iter);
3007 count = iter->seq.len - count;
3010 iter->seq.len -= count;
3013 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3014 iter->seq.len -= count;
3018 trace_consume(iter);
3020 if (!find_next_entry_inc(iter)) {
3030 static ssize_t tracing_splice_read_pipe(struct file *filp,
3032 struct pipe_inode_info *pipe,
3036 struct page *pages[PIPE_BUFFERS];
3037 struct partial_page partial[PIPE_BUFFERS];
3038 struct trace_iterator *iter = filp->private_data;
3039 struct splice_pipe_desc spd = {
3042 .nr_pages = 0, /* This gets updated below. */
3044 .ops = &tracing_pipe_buf_ops,
3045 .spd_release = tracing_spd_release_pipe,
3047 static struct tracer *old_tracer;
3052 /* copy the tracer to avoid using a global lock all around */
3053 mutex_lock(&trace_types_lock);
3054 if (unlikely(old_tracer != current_trace && current_trace)) {
3055 old_tracer = current_trace;
3056 *iter->trace = *current_trace;
3058 mutex_unlock(&trace_types_lock);
3060 mutex_lock(&iter->mutex);
3062 if (iter->trace->splice_read) {
3063 ret = iter->trace->splice_read(iter, filp,
3064 ppos, pipe, len, flags);
3069 ret = tracing_wait_pipe(filp);
3073 if (!iter->ent && !find_next_entry_inc(iter)) {
3078 /* Fill as many pages as possible. */
3079 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3080 pages[i] = alloc_page(GFP_KERNEL);
3084 rem = tracing_fill_pipe_page(rem, iter);
3086 /* Copy the data into the page, so we can start over. */
3087 ret = trace_seq_to_buffer(&iter->seq,
3088 page_address(pages[i]),
3091 __free_page(pages[i]);
3094 partial[i].offset = 0;
3095 partial[i].len = iter->seq.len;
3097 trace_seq_init(&iter->seq);
3100 mutex_unlock(&iter->mutex);
3104 return splice_to_pipe(pipe, &spd);
3107 mutex_unlock(&iter->mutex);
3113 tracing_entries_read(struct file *filp, char __user *ubuf,
3114 size_t cnt, loff_t *ppos)
3116 struct trace_array *tr = filp->private_data;
3120 mutex_lock(&trace_types_lock);
3121 if (!ring_buffer_expanded)
3122 r = sprintf(buf, "%lu (expanded: %lu)\n",
3124 trace_buf_size >> 10);
3126 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3127 mutex_unlock(&trace_types_lock);
3129 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3133 tracing_entries_write(struct file *filp, const char __user *ubuf,
3134 size_t cnt, loff_t *ppos)
3140 if (cnt >= sizeof(buf))
3143 if (copy_from_user(&buf, ubuf, cnt))
3148 ret = strict_strtoul(buf, 10, &val);
3152 /* must have at least 1 entry */
3156 mutex_lock(&trace_types_lock);
3160 /* disable all cpu buffers */
3161 for_each_tracing_cpu(cpu) {
3162 if (global_trace.data[cpu])
3163 atomic_inc(&global_trace.data[cpu]->disabled);
3164 if (max_tr.data[cpu])
3165 atomic_inc(&max_tr.data[cpu]->disabled);
3168 /* value is in KB */
3171 if (val != global_trace.entries) {
3172 ret = tracing_resize_ring_buffer(val);
3181 /* If check pages failed, return ENOMEM */
3182 if (tracing_disabled)
3185 for_each_tracing_cpu(cpu) {
3186 if (global_trace.data[cpu])
3187 atomic_dec(&global_trace.data[cpu]->disabled);
3188 if (max_tr.data[cpu])
3189 atomic_dec(&max_tr.data[cpu]->disabled);
3193 max_tr.entries = global_trace.entries;
3194 mutex_unlock(&trace_types_lock);
3199 static int mark_printk(const char *fmt, ...)
3203 va_start(args, fmt);
3204 ret = trace_vprintk(0, fmt, args);
3210 tracing_mark_write(struct file *filp, const char __user *ubuf,
3211 size_t cnt, loff_t *fpos)
3216 if (tracing_disabled)
3219 if (cnt > TRACE_BUF_SIZE)
3220 cnt = TRACE_BUF_SIZE;
3222 buf = kmalloc(cnt + 1, GFP_KERNEL);
3226 if (copy_from_user(buf, ubuf, cnt)) {
3231 /* Cut from the first nil or newline. */
3233 end = strchr(buf, '\n');
3237 cnt = mark_printk("%s\n", buf);
3244 static const struct file_operations tracing_max_lat_fops = {
3245 .open = tracing_open_generic,
3246 .read = tracing_max_lat_read,
3247 .write = tracing_max_lat_write,
3250 static const struct file_operations tracing_ctrl_fops = {
3251 .open = tracing_open_generic,
3252 .read = tracing_ctrl_read,
3253 .write = tracing_ctrl_write,
3256 static const struct file_operations set_tracer_fops = {
3257 .open = tracing_open_generic,
3258 .read = tracing_set_trace_read,
3259 .write = tracing_set_trace_write,
3262 static const struct file_operations tracing_pipe_fops = {
3263 .open = tracing_open_pipe,
3264 .poll = tracing_poll_pipe,
3265 .read = tracing_read_pipe,
3266 .splice_read = tracing_splice_read_pipe,
3267 .release = tracing_release_pipe,
3270 static const struct file_operations tracing_entries_fops = {
3271 .open = tracing_open_generic,
3272 .read = tracing_entries_read,
3273 .write = tracing_entries_write,
3276 static const struct file_operations tracing_mark_fops = {
3277 .open = tracing_open_generic,
3278 .write = tracing_mark_write,
3281 struct ftrace_buffer_info {
3282 struct trace_array *tr;
3288 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3290 int cpu = (int)(long)inode->i_private;
3291 struct ftrace_buffer_info *info;
3293 if (tracing_disabled)
3296 info = kzalloc(sizeof(*info), GFP_KERNEL);
3300 info->tr = &global_trace;
3303 /* Force reading ring buffer for first read */
3304 info->read = (unsigned int)-1;
3306 filp->private_data = info;
3308 return nonseekable_open(inode, filp);
3312 tracing_buffers_read(struct file *filp, char __user *ubuf,
3313 size_t count, loff_t *ppos)
3315 struct ftrace_buffer_info *info = filp->private_data;
3324 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3328 /* Do we have previous read data to read? */
3329 if (info->read < PAGE_SIZE)
3334 ret = ring_buffer_read_page(info->tr->buffer,
3341 pos = ring_buffer_page_len(info->spare);
3343 if (pos < PAGE_SIZE)
3344 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3347 size = PAGE_SIZE - info->read;
3351 ret = copy_to_user(ubuf, info->spare + info->read, size);
3362 static int tracing_buffers_release(struct inode *inode, struct file *file)
3364 struct ftrace_buffer_info *info = file->private_data;
3367 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3374 struct ring_buffer *buffer;
3379 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3380 struct pipe_buffer *buf)
3382 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3387 ring_buffer_free_read_page(ref->buffer, ref->page);
3392 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3393 struct pipe_buffer *buf)
3398 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3399 struct pipe_buffer *buf)
3401 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3406 /* Pipe buffer operations for a buffer. */
3407 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3409 .map = generic_pipe_buf_map,
3410 .unmap = generic_pipe_buf_unmap,
3411 .confirm = generic_pipe_buf_confirm,
3412 .release = buffer_pipe_buf_release,
3413 .steal = buffer_pipe_buf_steal,
3414 .get = buffer_pipe_buf_get,
3418 * Callback from splice_to_pipe(), if we need to release some pages
3419 * at the end of the spd in case we error'ed out in filling the pipe.
3421 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3423 struct buffer_ref *ref =
3424 (struct buffer_ref *)spd->partial[i].private;
3429 ring_buffer_free_read_page(ref->buffer, ref->page);
3431 spd->partial[i].private = 0;
3435 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3436 struct pipe_inode_info *pipe, size_t len,
3439 struct ftrace_buffer_info *info = file->private_data;
3440 struct partial_page partial[PIPE_BUFFERS];
3441 struct page *pages[PIPE_BUFFERS];
3442 struct splice_pipe_desc spd = {
3446 .ops = &buffer_pipe_buf_ops,
3447 .spd_release = buffer_spd_release,
3449 struct buffer_ref *ref;
3453 if (*ppos & (PAGE_SIZE - 1)) {
3454 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3458 if (len & (PAGE_SIZE - 1)) {
3459 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3460 if (len < PAGE_SIZE)
3465 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= PAGE_SIZE) {
3469 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3473 ref->buffer = info->tr->buffer;
3474 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3480 r = ring_buffer_read_page(ref->buffer, &ref->page,
3483 ring_buffer_free_read_page(ref->buffer,
3490 * zero out any left over data, this is going to
3493 size = ring_buffer_page_len(ref->page);
3494 if (size < PAGE_SIZE)
3495 memset(ref->page + size, 0, PAGE_SIZE - size);
3497 page = virt_to_page(ref->page);
3499 spd.pages[i] = page;
3500 spd.partial[i].len = PAGE_SIZE;
3501 spd.partial[i].offset = 0;
3502 spd.partial[i].private = (unsigned long)ref;
3509 /* did we read anything? */
3510 if (!spd.nr_pages) {
3511 if (flags & SPLICE_F_NONBLOCK)
3519 ret = splice_to_pipe(pipe, &spd);
3524 static const struct file_operations tracing_buffers_fops = {
3525 .open = tracing_buffers_open,
3526 .read = tracing_buffers_read,
3527 .release = tracing_buffers_release,
3528 .splice_read = tracing_buffers_splice_read,
3529 .llseek = no_llseek,
3532 #ifdef CONFIG_DYNAMIC_FTRACE
3534 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3540 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3541 size_t cnt, loff_t *ppos)
3543 static char ftrace_dyn_info_buffer[1024];
3544 static DEFINE_MUTEX(dyn_info_mutex);
3545 unsigned long *p = filp->private_data;
3546 char *buf = ftrace_dyn_info_buffer;
3547 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3550 mutex_lock(&dyn_info_mutex);
3551 r = sprintf(buf, "%ld ", *p);
3553 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3556 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3558 mutex_unlock(&dyn_info_mutex);
3563 static const struct file_operations tracing_dyn_info_fops = {
3564 .open = tracing_open_generic,
3565 .read = tracing_read_dyn_info,
3569 static struct dentry *d_tracer;
3571 struct dentry *tracing_init_dentry(void)
3578 if (!debugfs_initialized())
3581 d_tracer = debugfs_create_dir("tracing", NULL);
3583 if (!d_tracer && !once) {
3585 pr_warning("Could not create debugfs directory 'tracing'\n");
3592 static struct dentry *d_percpu;
3594 struct dentry *tracing_dentry_percpu(void)
3597 struct dentry *d_tracer;
3602 d_tracer = tracing_init_dentry();
3607 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3609 if (!d_percpu && !once) {
3611 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3618 static void tracing_init_debugfs_percpu(long cpu)
3620 struct dentry *d_percpu = tracing_dentry_percpu();
3621 struct dentry *d_cpu;
3622 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3625 if (cpu > 999 || cpu < 0)
3628 sprintf(cpu_dir, "cpu%ld", cpu);
3629 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3631 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3635 /* per cpu trace_pipe */
3636 trace_create_file("trace_pipe", 0444, d_cpu,
3637 (void *) cpu, &tracing_pipe_fops);
3640 trace_create_file("trace", 0644, d_cpu,
3641 (void *) cpu, &tracing_fops);
3643 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3644 (void *) cpu, &tracing_buffers_fops);
3647 #ifdef CONFIG_FTRACE_SELFTEST
3648 /* Let selftest have access to static functions in this file */
3649 #include "trace_selftest.c"
3652 struct trace_option_dentry {
3653 struct tracer_opt *opt;
3654 struct tracer_flags *flags;
3655 struct dentry *entry;
3659 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3662 struct trace_option_dentry *topt = filp->private_data;
3665 if (topt->flags->val & topt->opt->bit)
3670 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3674 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3677 struct trace_option_dentry *topt = filp->private_data;
3682 if (cnt >= sizeof(buf))
3685 if (copy_from_user(&buf, ubuf, cnt))
3690 ret = strict_strtoul(buf, 10, &val);
3697 /* do nothing if already cleared */
3698 if (!(topt->flags->val & topt->opt->bit))
3701 mutex_lock(&trace_types_lock);
3702 if (current_trace->set_flag)
3703 ret = current_trace->set_flag(topt->flags->val,
3705 mutex_unlock(&trace_types_lock);
3708 topt->flags->val &= ~topt->opt->bit;
3711 /* do nothing if already set */
3712 if (topt->flags->val & topt->opt->bit)
3715 mutex_lock(&trace_types_lock);
3716 if (current_trace->set_flag)
3717 ret = current_trace->set_flag(topt->flags->val,
3719 mutex_unlock(&trace_types_lock);
3722 topt->flags->val |= topt->opt->bit;
3735 static const struct file_operations trace_options_fops = {
3736 .open = tracing_open_generic,
3737 .read = trace_options_read,
3738 .write = trace_options_write,
3742 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3745 long index = (long)filp->private_data;
3748 if (trace_flags & (1 << index))
3753 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3757 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3760 long index = (long)filp->private_data;
3765 if (cnt >= sizeof(buf))
3768 if (copy_from_user(&buf, ubuf, cnt))
3773 ret = strict_strtoul(buf, 10, &val);
3779 trace_flags &= ~(1 << index);
3782 trace_flags |= 1 << index;
3794 static const struct file_operations trace_options_core_fops = {
3795 .open = tracing_open_generic,
3796 .read = trace_options_core_read,
3797 .write = trace_options_core_write,
3800 struct dentry *trace_create_file(const char *name,
3802 struct dentry *parent,
3804 const struct file_operations *fops)
3808 ret = debugfs_create_file(name, mode, parent, data, fops);
3810 pr_warning("Could not create debugfs '%s' entry\n", name);
3816 static struct dentry *trace_options_init_dentry(void)
3818 struct dentry *d_tracer;
3819 static struct dentry *t_options;
3824 d_tracer = tracing_init_dentry();
3828 t_options = debugfs_create_dir("options", d_tracer);
3830 pr_warning("Could not create debugfs directory 'options'\n");
3838 create_trace_option_file(struct trace_option_dentry *topt,
3839 struct tracer_flags *flags,
3840 struct tracer_opt *opt)
3842 struct dentry *t_options;
3844 t_options = trace_options_init_dentry();
3848 topt->flags = flags;
3851 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
3852 &trace_options_fops);
3856 static struct trace_option_dentry *
3857 create_trace_option_files(struct tracer *tracer)
3859 struct trace_option_dentry *topts;
3860 struct tracer_flags *flags;
3861 struct tracer_opt *opts;
3867 flags = tracer->flags;
3869 if (!flags || !flags->opts)
3874 for (cnt = 0; opts[cnt].name; cnt++)
3877 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3881 for (cnt = 0; opts[cnt].name; cnt++)
3882 create_trace_option_file(&topts[cnt], flags,
3889 destroy_trace_option_files(struct trace_option_dentry *topts)
3896 for (cnt = 0; topts[cnt].opt; cnt++) {
3897 if (topts[cnt].entry)
3898 debugfs_remove(topts[cnt].entry);
3904 static struct dentry *
3905 create_trace_option_core_file(const char *option, long index)
3907 struct dentry *t_options;
3909 t_options = trace_options_init_dentry();
3913 return trace_create_file(option, 0644, t_options, (void *)index,
3914 &trace_options_core_fops);
3917 static __init void create_trace_options_dir(void)
3919 struct dentry *t_options;
3922 t_options = trace_options_init_dentry();
3926 for (i = 0; trace_options[i]; i++)
3927 create_trace_option_core_file(trace_options[i], i);
3930 static __init int tracer_init_debugfs(void)
3932 struct dentry *d_tracer;
3935 d_tracer = tracing_init_dentry();
3937 trace_create_file("tracing_enabled", 0644, d_tracer,
3938 &global_trace, &tracing_ctrl_fops);
3940 trace_create_file("trace_options", 0644, d_tracer,
3941 NULL, &tracing_iter_fops);
3943 trace_create_file("tracing_cpumask", 0644, d_tracer,
3944 NULL, &tracing_cpumask_fops);
3946 trace_create_file("trace", 0644, d_tracer,
3947 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3949 trace_create_file("available_tracers", 0444, d_tracer,
3950 &global_trace, &show_traces_fops);
3952 trace_create_file("current_tracer", 0444, d_tracer,
3953 &global_trace, &set_tracer_fops);
3955 trace_create_file("tracing_max_latency", 0644, d_tracer,
3956 &tracing_max_latency, &tracing_max_lat_fops);
3958 trace_create_file("tracing_thresh", 0644, d_tracer,
3959 &tracing_thresh, &tracing_max_lat_fops);
3961 trace_create_file("README", 0644, d_tracer,
3962 NULL, &tracing_readme_fops);
3964 trace_create_file("trace_pipe", 0444, d_tracer,
3965 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3967 trace_create_file("buffer_size_kb", 0644, d_tracer,
3968 &global_trace, &tracing_entries_fops);
3970 trace_create_file("trace_marker", 0220, d_tracer,
3971 NULL, &tracing_mark_fops);
3973 #ifdef CONFIG_DYNAMIC_FTRACE
3974 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3975 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
3977 #ifdef CONFIG_SYSPROF_TRACER
3978 init_tracer_sysprof_debugfs(d_tracer);
3981 create_trace_options_dir();
3983 for_each_tracing_cpu(cpu)
3984 tracing_init_debugfs_percpu(cpu);
3989 static int trace_panic_handler(struct notifier_block *this,
3990 unsigned long event, void *unused)
3992 if (ftrace_dump_on_oops)
3997 static struct notifier_block trace_panic_notifier = {
3998 .notifier_call = trace_panic_handler,
4000 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4003 static int trace_die_handler(struct notifier_block *self,
4009 if (ftrace_dump_on_oops)
4018 static struct notifier_block trace_die_notifier = {
4019 .notifier_call = trace_die_handler,
4024 * printk is set to max of 1024, we really don't need it that big.
4025 * Nothing should be printing 1000 characters anyway.
4027 #define TRACE_MAX_PRINT 1000
4030 * Define here KERN_TRACE so that we have one place to modify
4031 * it if we decide to change what log level the ftrace dump
4034 #define KERN_TRACE KERN_EMERG
4037 trace_printk_seq(struct trace_seq *s)
4039 /* Probably should print a warning here. */
4043 /* should be zero ended, but we are paranoid. */
4044 s->buffer[s->len] = 0;
4046 printk(KERN_TRACE "%s", s->buffer);
4051 static void __ftrace_dump(bool disable_tracing)
4053 static DEFINE_SPINLOCK(ftrace_dump_lock);
4054 /* use static because iter can be a bit big for the stack */
4055 static struct trace_iterator iter;
4056 unsigned int old_userobj;
4057 static int dump_ran;
4058 unsigned long flags;
4062 spin_lock_irqsave(&ftrace_dump_lock, flags);
4070 if (disable_tracing)
4073 for_each_tracing_cpu(cpu) {
4074 atomic_inc(&global_trace.data[cpu]->disabled);
4077 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4079 /* don't look at user memory in panic mode */
4080 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4082 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4084 /* Simulate the iterator */
4085 iter.tr = &global_trace;
4086 iter.trace = current_trace;
4087 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4090 * We need to stop all tracing on all CPUS to read the
4091 * the next buffer. This is a bit expensive, but is
4092 * not done often. We fill all what we can read,
4093 * and then release the locks again.
4096 while (!trace_empty(&iter)) {
4099 printk(KERN_TRACE "---------------------------------\n");
4103 /* reset all but tr, trace, and overruns */
4104 memset(&iter.seq, 0,
4105 sizeof(struct trace_iterator) -
4106 offsetof(struct trace_iterator, seq));
4107 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4110 if (find_next_entry_inc(&iter) != NULL) {
4111 print_trace_line(&iter);
4112 trace_consume(&iter);
4115 trace_printk_seq(&iter.seq);
4119 printk(KERN_TRACE " (ftrace buffer empty)\n");
4121 printk(KERN_TRACE "---------------------------------\n");
4123 /* Re-enable tracing if requested */
4124 if (!disable_tracing) {
4125 trace_flags |= old_userobj;
4127 for_each_tracing_cpu(cpu) {
4128 atomic_dec(&global_trace.data[cpu]->disabled);
4134 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
4137 /* By default: disable tracing after the dump */
4138 void ftrace_dump(void)
4140 __ftrace_dump(true);
4143 __init static int tracer_alloc_buffers(void)
4145 struct trace_array_cpu *data;
4150 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4153 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4154 goto out_free_buffer_mask;
4156 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4157 goto out_free_tracing_cpumask;
4159 /* To save memory, keep the ring buffer size to its minimum */
4160 if (ring_buffer_expanded)
4161 ring_buf_size = trace_buf_size;
4165 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4166 cpumask_copy(tracing_cpumask, cpu_all_mask);
4167 cpumask_clear(tracing_reader_cpumask);
4169 /* TODO: make the number of buffers hot pluggable with CPUS */
4170 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4171 TRACE_BUFFER_FLAGS);
4172 if (!global_trace.buffer) {
4173 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4175 goto out_free_cpumask;
4177 global_trace.entries = ring_buffer_size(global_trace.buffer);
4180 #ifdef CONFIG_TRACER_MAX_TRACE
4181 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4182 TRACE_BUFFER_FLAGS);
4183 if (!max_tr.buffer) {
4184 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4186 ring_buffer_free(global_trace.buffer);
4187 goto out_free_cpumask;
4189 max_tr.entries = ring_buffer_size(max_tr.buffer);
4190 WARN_ON(max_tr.entries != global_trace.entries);
4193 /* Allocate the first page for all buffers */
4194 for_each_tracing_cpu(i) {
4195 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4196 max_tr.data[i] = &per_cpu(max_data, i);
4199 trace_init_cmdlines();
4201 register_tracer(&nop_trace);
4202 current_trace = &nop_trace;
4203 #ifdef CONFIG_BOOT_TRACER
4204 register_tracer(&boot_tracer);
4206 /* All seems OK, enable tracing */
4207 tracing_disabled = 0;
4209 atomic_notifier_chain_register(&panic_notifier_list,
4210 &trace_panic_notifier);
4212 register_die_notifier(&trace_die_notifier);
4217 free_cpumask_var(tracing_reader_cpumask);
4218 out_free_tracing_cpumask:
4219 free_cpumask_var(tracing_cpumask);
4220 out_free_buffer_mask:
4221 free_cpumask_var(tracing_buffer_mask);
4226 __init static int clear_boot_tracer(void)
4229 * The default tracer at boot buffer is an init section.
4230 * This function is called in lateinit. If we did not
4231 * find the boot tracer, then clear it out, to prevent
4232 * later registration from accessing the buffer that is
4233 * about to be freed.
4235 if (!default_bootup_tracer)
4238 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4239 default_bootup_tracer);
4240 default_bootup_tracer = NULL;
4245 early_initcall(tracer_alloc_buffers);
4246 fs_initcall(tracer_init_debugfs);
4247 late_initcall(clear_boot_tracer);