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);
179 EXPORT_SYMBOL_GPL(filter_current_check_discard);
181 cycle_t ftrace_now(int cpu)
185 /* Early boot up does not have a buffer yet */
186 if (!global_trace.buffer)
187 return trace_clock_local();
189 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
190 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
196 * The max_tr is used to snapshot the global_trace when a maximum
197 * latency is reached. Some tracers will use this to store a maximum
198 * trace while it continues examining live traces.
200 * The buffers for the max_tr are set up the same as the global_trace.
201 * When a snapshot is taken, the link list of the max_tr is swapped
202 * with the link list of the global_trace and the buffers are reset for
203 * the global_trace so the tracing can continue.
205 static struct trace_array max_tr;
207 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
209 /* tracer_enabled is used to toggle activation of a tracer */
210 static int tracer_enabled = 1;
213 * tracing_is_enabled - return tracer_enabled status
215 * This function is used by other tracers to know the status
216 * of the tracer_enabled flag. Tracers may use this function
217 * to know if it should enable their features when starting
218 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
220 int tracing_is_enabled(void)
222 return tracer_enabled;
226 * trace_buf_size is the size in bytes that is allocated
227 * for a buffer. Note, the number of bytes is always rounded
230 * This number is purposely set to a low number of 16384.
231 * If the dump on oops happens, it will be much appreciated
232 * to not have to wait for all that output. Anyway this can be
233 * boot time and run time configurable.
235 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
237 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
239 /* trace_types holds a link list of available tracers. */
240 static struct tracer *trace_types __read_mostly;
242 /* current_trace points to the tracer that is currently active */
243 static struct tracer *current_trace __read_mostly;
246 * max_tracer_type_len is used to simplify the allocating of
247 * buffers to read userspace tracer names. We keep track of
248 * the longest tracer name registered.
250 static int max_tracer_type_len;
253 * trace_types_lock is used to protect the trace_types list.
254 * This lock is also used to keep user access serialized.
255 * Accesses from userspace will grab this lock while userspace
256 * activities happen inside the kernel.
258 static DEFINE_MUTEX(trace_types_lock);
260 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
261 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
263 /* trace_flags holds trace_options default values */
264 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
265 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
266 TRACE_ITER_GRAPH_TIME;
269 * trace_wake_up - wake up tasks waiting for trace input
271 * Simply wakes up any task that is blocked on the trace_wait
272 * queue. These is used with trace_poll for tasks polling the trace.
274 void trace_wake_up(void)
277 * The runqueue_is_locked() can fail, but this is the best we
280 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
281 wake_up(&trace_wait);
284 static int __init set_buf_size(char *str)
286 unsigned long buf_size;
290 buf_size = memparse(str, &str);
291 /* nr_entries can not be zero */
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 /sys/kernel/debug/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 void tracing_reset_current(int cpu)
643 tracing_reset(&global_trace, cpu);
646 void tracing_reset_current_online_cpus(void)
648 tracing_reset_online_cpus(&global_trace);
651 #define SAVED_CMDLINES 128
652 #define NO_CMDLINE_MAP UINT_MAX
653 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
654 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
655 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
656 static int cmdline_idx;
657 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
659 /* temporary disable recording */
660 static atomic_t trace_record_cmdline_disabled __read_mostly;
662 static void trace_init_cmdlines(void)
664 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
665 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
669 static int trace_stop_count;
670 static DEFINE_SPINLOCK(tracing_start_lock);
673 * ftrace_off_permanent - disable all ftrace code permanently
675 * This should only be called when a serious anomally has
676 * been detected. This will turn off the function tracing,
677 * ring buffers, and other tracing utilites. It takes no
678 * locks and can be called from any context.
680 void ftrace_off_permanent(void)
682 tracing_disabled = 1;
684 tracing_off_permanent();
688 * tracing_start - quick start of the tracer
690 * If tracing is enabled but was stopped by tracing_stop,
691 * this will start the tracer back up.
693 void tracing_start(void)
695 struct ring_buffer *buffer;
698 if (tracing_disabled)
701 spin_lock_irqsave(&tracing_start_lock, flags);
702 if (--trace_stop_count) {
703 if (trace_stop_count < 0) {
704 /* Someone screwed up their debugging */
706 trace_stop_count = 0;
712 buffer = global_trace.buffer;
714 ring_buffer_record_enable(buffer);
716 buffer = max_tr.buffer;
718 ring_buffer_record_enable(buffer);
722 spin_unlock_irqrestore(&tracing_start_lock, flags);
726 * tracing_stop - quick stop of the tracer
728 * Light weight way to stop tracing. Use in conjunction with
731 void tracing_stop(void)
733 struct ring_buffer *buffer;
737 spin_lock_irqsave(&tracing_start_lock, flags);
738 if (trace_stop_count++)
741 buffer = global_trace.buffer;
743 ring_buffer_record_disable(buffer);
745 buffer = max_tr.buffer;
747 ring_buffer_record_disable(buffer);
750 spin_unlock_irqrestore(&tracing_start_lock, flags);
753 void trace_stop_cmdline_recording(void);
755 static void trace_save_cmdline(struct task_struct *tsk)
759 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
763 * It's not the end of the world if we don't get
764 * the lock, but we also don't want to spin
765 * nor do we want to disable interrupts,
766 * so if we miss here, then better luck next time.
768 if (!__raw_spin_trylock(&trace_cmdline_lock))
771 idx = map_pid_to_cmdline[tsk->pid];
772 if (idx == NO_CMDLINE_MAP) {
773 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
776 * Check whether the cmdline buffer at idx has a pid
777 * mapped. We are going to overwrite that entry so we
778 * need to clear the map_pid_to_cmdline. Otherwise we
779 * would read the new comm for the old pid.
781 pid = map_cmdline_to_pid[idx];
782 if (pid != NO_CMDLINE_MAP)
783 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
785 map_cmdline_to_pid[idx] = tsk->pid;
786 map_pid_to_cmdline[tsk->pid] = idx;
791 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
793 __raw_spin_unlock(&trace_cmdline_lock);
796 void trace_find_cmdline(int pid, char comm[])
801 strcpy(comm, "<idle>");
805 if (pid > PID_MAX_DEFAULT) {
806 strcpy(comm, "<...>");
811 __raw_spin_lock(&trace_cmdline_lock);
812 map = map_pid_to_cmdline[pid];
813 if (map != NO_CMDLINE_MAP)
814 strcpy(comm, saved_cmdlines[map]);
816 strcpy(comm, "<...>");
818 __raw_spin_unlock(&trace_cmdline_lock);
822 void tracing_record_cmdline(struct task_struct *tsk)
824 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
828 trace_save_cmdline(tsk);
832 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
835 struct task_struct *tsk = current;
837 entry->preempt_count = pc & 0xff;
838 entry->pid = (tsk) ? tsk->pid : 0;
839 entry->tgid = (tsk) ? tsk->tgid : 0;
841 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
842 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
844 TRACE_FLAG_IRQS_NOSUPPORT |
846 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
847 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
848 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
851 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
854 unsigned long flags, int pc)
856 struct ring_buffer_event *event;
858 event = ring_buffer_lock_reserve(tr->buffer, len);
860 struct trace_entry *ent = ring_buffer_event_data(event);
862 tracing_generic_entry_update(ent, flags, pc);
868 static void ftrace_trace_stack(struct trace_array *tr,
869 unsigned long flags, int skip, int pc);
870 static void ftrace_trace_userstack(struct trace_array *tr,
871 unsigned long flags, int pc);
873 static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
874 struct ring_buffer_event *event,
875 unsigned long flags, int pc,
878 ring_buffer_unlock_commit(tr->buffer, event);
880 ftrace_trace_stack(tr, flags, 6, pc);
881 ftrace_trace_userstack(tr, flags, pc);
887 void trace_buffer_unlock_commit(struct trace_array *tr,
888 struct ring_buffer_event *event,
889 unsigned long flags, int pc)
891 __trace_buffer_unlock_commit(tr, event, flags, pc, 1);
894 struct ring_buffer_event *
895 trace_current_buffer_lock_reserve(int type, unsigned long len,
896 unsigned long flags, int pc)
898 return trace_buffer_lock_reserve(&global_trace,
899 type, len, flags, pc);
901 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
903 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
904 unsigned long flags, int pc)
906 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
908 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
910 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
911 unsigned long flags, int pc)
913 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
915 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
917 void trace_current_buffer_discard_commit(struct ring_buffer_event *event)
919 ring_buffer_discard_commit(global_trace.buffer, event);
921 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
924 trace_function(struct trace_array *tr,
925 unsigned long ip, unsigned long parent_ip, unsigned long flags,
928 struct ftrace_event_call *call = &event_function;
929 struct ring_buffer_event *event;
930 struct ftrace_entry *entry;
932 /* If we are reading the ring buffer, don't trace */
933 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
936 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
940 entry = ring_buffer_event_data(event);
942 entry->parent_ip = parent_ip;
944 if (!filter_check_discard(call, entry, tr->buffer, event))
945 ring_buffer_unlock_commit(tr->buffer, event);
948 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
949 static int __trace_graph_entry(struct trace_array *tr,
950 struct ftrace_graph_ent *trace,
954 struct ftrace_event_call *call = &event_funcgraph_entry;
955 struct ring_buffer_event *event;
956 struct ftrace_graph_ent_entry *entry;
958 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
961 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
962 sizeof(*entry), flags, pc);
965 entry = ring_buffer_event_data(event);
966 entry->graph_ent = *trace;
967 if (!filter_current_check_discard(call, entry, event))
968 ring_buffer_unlock_commit(global_trace.buffer, event);
973 static void __trace_graph_return(struct trace_array *tr,
974 struct ftrace_graph_ret *trace,
978 struct ftrace_event_call *call = &event_funcgraph_exit;
979 struct ring_buffer_event *event;
980 struct ftrace_graph_ret_entry *entry;
982 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
985 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
986 sizeof(*entry), flags, pc);
989 entry = ring_buffer_event_data(event);
991 if (!filter_current_check_discard(call, entry, event))
992 ring_buffer_unlock_commit(global_trace.buffer, event);
997 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
998 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1001 if (likely(!atomic_read(&data->disabled)))
1002 trace_function(tr, ip, parent_ip, flags, pc);
1005 static void __ftrace_trace_stack(struct trace_array *tr,
1006 unsigned long flags,
1009 #ifdef CONFIG_STACKTRACE
1010 struct ftrace_event_call *call = &event_kernel_stack;
1011 struct ring_buffer_event *event;
1012 struct stack_entry *entry;
1013 struct stack_trace trace;
1015 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
1016 sizeof(*entry), flags, pc);
1019 entry = ring_buffer_event_data(event);
1020 memset(&entry->caller, 0, sizeof(entry->caller));
1022 trace.nr_entries = 0;
1023 trace.max_entries = FTRACE_STACK_ENTRIES;
1025 trace.entries = entry->caller;
1027 save_stack_trace(&trace);
1028 if (!filter_check_discard(call, entry, tr->buffer, event))
1029 ring_buffer_unlock_commit(tr->buffer, event);
1033 static void ftrace_trace_stack(struct trace_array *tr,
1034 unsigned long flags,
1037 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1040 __ftrace_trace_stack(tr, flags, skip, pc);
1043 void __trace_stack(struct trace_array *tr,
1044 unsigned long flags,
1047 __ftrace_trace_stack(tr, flags, skip, pc);
1050 static void ftrace_trace_userstack(struct trace_array *tr,
1051 unsigned long flags, int pc)
1053 #ifdef CONFIG_STACKTRACE
1054 struct ftrace_event_call *call = &event_user_stack;
1055 struct ring_buffer_event *event;
1056 struct userstack_entry *entry;
1057 struct stack_trace trace;
1059 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1062 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1063 sizeof(*entry), flags, pc);
1066 entry = ring_buffer_event_data(event);
1068 memset(&entry->caller, 0, sizeof(entry->caller));
1070 trace.nr_entries = 0;
1071 trace.max_entries = FTRACE_STACK_ENTRIES;
1073 trace.entries = entry->caller;
1075 save_stack_trace_user(&trace);
1076 if (!filter_check_discard(call, entry, tr->buffer, event))
1077 ring_buffer_unlock_commit(tr->buffer, event);
1082 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1084 ftrace_trace_userstack(tr, flags, preempt_count());
1089 ftrace_trace_special(void *__tr,
1090 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1093 struct ring_buffer_event *event;
1094 struct trace_array *tr = __tr;
1095 struct special_entry *entry;
1097 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1098 sizeof(*entry), 0, pc);
1101 entry = ring_buffer_event_data(event);
1105 trace_buffer_unlock_commit(tr, event, 0, pc);
1109 __trace_special(void *__tr, void *__data,
1110 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1112 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1116 tracing_sched_switch_trace(struct trace_array *tr,
1117 struct task_struct *prev,
1118 struct task_struct *next,
1119 unsigned long flags, int pc)
1121 struct ftrace_event_call *call = &event_context_switch;
1122 struct ring_buffer_event *event;
1123 struct ctx_switch_entry *entry;
1125 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1126 sizeof(*entry), flags, pc);
1129 entry = ring_buffer_event_data(event);
1130 entry->prev_pid = prev->pid;
1131 entry->prev_prio = prev->prio;
1132 entry->prev_state = prev->state;
1133 entry->next_pid = next->pid;
1134 entry->next_prio = next->prio;
1135 entry->next_state = next->state;
1136 entry->next_cpu = task_cpu(next);
1138 if (!filter_check_discard(call, entry, tr->buffer, event))
1139 trace_buffer_unlock_commit(tr, event, flags, pc);
1143 tracing_sched_wakeup_trace(struct trace_array *tr,
1144 struct task_struct *wakee,
1145 struct task_struct *curr,
1146 unsigned long flags, int pc)
1148 struct ftrace_event_call *call = &event_wakeup;
1149 struct ring_buffer_event *event;
1150 struct ctx_switch_entry *entry;
1152 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1153 sizeof(*entry), flags, pc);
1156 entry = ring_buffer_event_data(event);
1157 entry->prev_pid = curr->pid;
1158 entry->prev_prio = curr->prio;
1159 entry->prev_state = curr->state;
1160 entry->next_pid = wakee->pid;
1161 entry->next_prio = wakee->prio;
1162 entry->next_state = wakee->state;
1163 entry->next_cpu = task_cpu(wakee);
1165 if (!filter_check_discard(call, entry, tr->buffer, event))
1166 ring_buffer_unlock_commit(tr->buffer, event);
1167 ftrace_trace_stack(tr, flags, 6, pc);
1168 ftrace_trace_userstack(tr, flags, pc);
1172 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1174 struct trace_array *tr = &global_trace;
1175 struct trace_array_cpu *data;
1176 unsigned long flags;
1180 if (tracing_disabled)
1183 pc = preempt_count();
1184 local_irq_save(flags);
1185 cpu = raw_smp_processor_id();
1186 data = tr->data[cpu];
1188 if (likely(atomic_inc_return(&data->disabled) == 1))
1189 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1191 atomic_dec(&data->disabled);
1192 local_irq_restore(flags);
1195 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1196 int trace_graph_entry(struct ftrace_graph_ent *trace)
1198 struct trace_array *tr = &global_trace;
1199 struct trace_array_cpu *data;
1200 unsigned long flags;
1206 if (!ftrace_trace_task(current))
1209 if (!ftrace_graph_addr(trace->func))
1212 local_irq_save(flags);
1213 cpu = raw_smp_processor_id();
1214 data = tr->data[cpu];
1215 disabled = atomic_inc_return(&data->disabled);
1216 if (likely(disabled == 1)) {
1217 pc = preempt_count();
1218 ret = __trace_graph_entry(tr, trace, flags, pc);
1222 /* Only do the atomic if it is not already set */
1223 if (!test_tsk_trace_graph(current))
1224 set_tsk_trace_graph(current);
1226 atomic_dec(&data->disabled);
1227 local_irq_restore(flags);
1232 void trace_graph_return(struct ftrace_graph_ret *trace)
1234 struct trace_array *tr = &global_trace;
1235 struct trace_array_cpu *data;
1236 unsigned long flags;
1241 local_irq_save(flags);
1242 cpu = raw_smp_processor_id();
1243 data = tr->data[cpu];
1244 disabled = atomic_inc_return(&data->disabled);
1245 if (likely(disabled == 1)) {
1246 pc = preempt_count();
1247 __trace_graph_return(tr, trace, flags, pc);
1250 clear_tsk_trace_graph(current);
1251 atomic_dec(&data->disabled);
1252 local_irq_restore(flags);
1254 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1258 * trace_vbprintk - write binary msg to tracing buffer
1261 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1263 static raw_spinlock_t trace_buf_lock =
1264 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1265 static u32 trace_buf[TRACE_BUF_SIZE];
1267 struct ftrace_event_call *call = &event_bprint;
1268 struct ring_buffer_event *event;
1269 struct trace_array *tr = &global_trace;
1270 struct trace_array_cpu *data;
1271 struct bprint_entry *entry;
1272 unsigned long flags;
1275 int cpu, len = 0, size, pc;
1277 if (unlikely(tracing_selftest_running || tracing_disabled))
1280 /* Don't pollute graph traces with trace_vprintk internals */
1281 pause_graph_tracing();
1283 pc = preempt_count();
1284 resched = ftrace_preempt_disable();
1285 cpu = raw_smp_processor_id();
1286 data = tr->data[cpu];
1288 disable = atomic_inc_return(&data->disabled);
1289 if (unlikely(disable != 1))
1292 /* Lockdep uses trace_printk for lock tracing */
1293 local_irq_save(flags);
1294 __raw_spin_lock(&trace_buf_lock);
1295 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1297 if (len > TRACE_BUF_SIZE || len < 0)
1300 size = sizeof(*entry) + sizeof(u32) * len;
1301 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1304 entry = ring_buffer_event_data(event);
1308 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1309 if (!filter_check_discard(call, entry, tr->buffer, event))
1310 ring_buffer_unlock_commit(tr->buffer, event);
1313 __raw_spin_unlock(&trace_buf_lock);
1314 local_irq_restore(flags);
1317 atomic_dec_return(&data->disabled);
1318 ftrace_preempt_enable(resched);
1319 unpause_graph_tracing();
1323 EXPORT_SYMBOL_GPL(trace_vbprintk);
1325 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1327 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1328 static char trace_buf[TRACE_BUF_SIZE];
1330 struct ftrace_event_call *call = &event_print;
1331 struct ring_buffer_event *event;
1332 struct trace_array *tr = &global_trace;
1333 struct trace_array_cpu *data;
1334 int cpu, len = 0, size, pc;
1335 struct print_entry *entry;
1336 unsigned long irq_flags;
1339 if (tracing_disabled || tracing_selftest_running)
1342 pc = preempt_count();
1343 preempt_disable_notrace();
1344 cpu = raw_smp_processor_id();
1345 data = tr->data[cpu];
1347 disable = atomic_inc_return(&data->disabled);
1348 if (unlikely(disable != 1))
1351 pause_graph_tracing();
1352 raw_local_irq_save(irq_flags);
1353 __raw_spin_lock(&trace_buf_lock);
1354 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1356 len = min(len, TRACE_BUF_SIZE-1);
1359 size = sizeof(*entry) + len + 1;
1360 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1363 entry = ring_buffer_event_data(event);
1366 memcpy(&entry->buf, trace_buf, len);
1367 entry->buf[len] = 0;
1368 if (!filter_check_discard(call, entry, tr->buffer, event))
1369 ring_buffer_unlock_commit(tr->buffer, event);
1372 __raw_spin_unlock(&trace_buf_lock);
1373 raw_local_irq_restore(irq_flags);
1374 unpause_graph_tracing();
1376 atomic_dec_return(&data->disabled);
1377 preempt_enable_notrace();
1381 EXPORT_SYMBOL_GPL(trace_vprintk);
1383 enum trace_file_type {
1384 TRACE_FILE_LAT_FMT = 1,
1385 TRACE_FILE_ANNOTATE = 2,
1388 static void trace_iterator_increment(struct trace_iterator *iter)
1390 /* Don't allow ftrace to trace into the ring buffers */
1391 ftrace_disable_cpu();
1394 if (iter->buffer_iter[iter->cpu])
1395 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1397 ftrace_enable_cpu();
1400 static struct trace_entry *
1401 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1403 struct ring_buffer_event *event;
1404 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1406 /* Don't allow ftrace to trace into the ring buffers */
1407 ftrace_disable_cpu();
1410 event = ring_buffer_iter_peek(buf_iter, ts);
1412 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1414 ftrace_enable_cpu();
1416 return event ? ring_buffer_event_data(event) : NULL;
1419 static struct trace_entry *
1420 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1422 struct ring_buffer *buffer = iter->tr->buffer;
1423 struct trace_entry *ent, *next = NULL;
1424 int cpu_file = iter->cpu_file;
1425 u64 next_ts = 0, ts;
1430 * If we are in a per_cpu trace file, don't bother by iterating over
1431 * all cpu and peek directly.
1433 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1434 if (ring_buffer_empty_cpu(buffer, cpu_file))
1436 ent = peek_next_entry(iter, cpu_file, ent_ts);
1438 *ent_cpu = cpu_file;
1443 for_each_tracing_cpu(cpu) {
1445 if (ring_buffer_empty_cpu(buffer, cpu))
1448 ent = peek_next_entry(iter, cpu, &ts);
1451 * Pick the entry with the smallest timestamp:
1453 if (ent && (!next || ts < next_ts)) {
1461 *ent_cpu = next_cpu;
1469 /* Find the next real entry, without updating the iterator itself */
1470 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1471 int *ent_cpu, u64 *ent_ts)
1473 return __find_next_entry(iter, ent_cpu, ent_ts);
1476 /* Find the next real entry, and increment the iterator to the next entry */
1477 static void *find_next_entry_inc(struct trace_iterator *iter)
1479 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1482 trace_iterator_increment(iter);
1484 return iter->ent ? iter : NULL;
1487 static void trace_consume(struct trace_iterator *iter)
1489 /* Don't allow ftrace to trace into the ring buffers */
1490 ftrace_disable_cpu();
1491 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1492 ftrace_enable_cpu();
1495 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1497 struct trace_iterator *iter = m->private;
1503 /* can't go backwards */
1508 ent = find_next_entry_inc(iter);
1512 while (ent && iter->idx < i)
1513 ent = find_next_entry_inc(iter);
1521 * No necessary locking here. The worst thing which can
1522 * happen is loosing events consumed at the same time
1523 * by a trace_pipe reader.
1524 * Other than that, we don't risk to crash the ring buffer
1525 * because it serializes the readers.
1527 * The current tracer is copied to avoid a global locking
1530 static void *s_start(struct seq_file *m, loff_t *pos)
1532 struct trace_iterator *iter = m->private;
1533 static struct tracer *old_tracer;
1534 int cpu_file = iter->cpu_file;
1539 /* copy the tracer to avoid using a global lock all around */
1540 mutex_lock(&trace_types_lock);
1541 if (unlikely(old_tracer != current_trace && current_trace)) {
1542 old_tracer = current_trace;
1543 *iter->trace = *current_trace;
1545 mutex_unlock(&trace_types_lock);
1547 atomic_inc(&trace_record_cmdline_disabled);
1549 if (*pos != iter->pos) {
1554 ftrace_disable_cpu();
1556 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1557 for_each_tracing_cpu(cpu)
1558 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1560 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1563 ftrace_enable_cpu();
1565 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1570 p = s_next(m, p, &l);
1573 trace_event_read_lock();
1577 static void s_stop(struct seq_file *m, void *p)
1579 atomic_dec(&trace_record_cmdline_disabled);
1580 trace_event_read_unlock();
1583 static void print_lat_help_header(struct seq_file *m)
1585 seq_puts(m, "# _------=> CPU# \n");
1586 seq_puts(m, "# / _-----=> irqs-off \n");
1587 seq_puts(m, "# | / _----=> need-resched \n");
1588 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1589 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1590 seq_puts(m, "# |||| / \n");
1591 seq_puts(m, "# ||||| delay \n");
1592 seq_puts(m, "# cmd pid ||||| time | caller \n");
1593 seq_puts(m, "# \\ / ||||| \\ | / \n");
1596 static void print_func_help_header(struct seq_file *m)
1598 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1599 seq_puts(m, "# | | | | |\n");
1604 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1606 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1607 struct trace_array *tr = iter->tr;
1608 struct trace_array_cpu *data = tr->data[tr->cpu];
1609 struct tracer *type = current_trace;
1610 unsigned long total;
1611 unsigned long entries;
1612 const char *name = "preemption";
1617 entries = ring_buffer_entries(iter->tr->buffer);
1619 ring_buffer_overruns(iter->tr->buffer);
1621 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1623 seq_puts(m, "# -----------------------------------"
1624 "---------------------------------\n");
1625 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1626 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1627 nsecs_to_usecs(data->saved_latency),
1631 #if defined(CONFIG_PREEMPT_NONE)
1633 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1635 #elif defined(CONFIG_PREEMPT)
1640 /* These are reserved for later use */
1643 seq_printf(m, " #P:%d)\n", num_online_cpus());
1647 seq_puts(m, "# -----------------\n");
1648 seq_printf(m, "# | task: %.16s-%d "
1649 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1650 data->comm, data->pid, data->uid, data->nice,
1651 data->policy, data->rt_priority);
1652 seq_puts(m, "# -----------------\n");
1654 if (data->critical_start) {
1655 seq_puts(m, "# => started at: ");
1656 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1657 trace_print_seq(m, &iter->seq);
1658 seq_puts(m, "\n# => ended at: ");
1659 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1660 trace_print_seq(m, &iter->seq);
1667 static void test_cpu_buff_start(struct trace_iterator *iter)
1669 struct trace_seq *s = &iter->seq;
1671 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1674 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1677 if (cpumask_test_cpu(iter->cpu, iter->started))
1680 cpumask_set_cpu(iter->cpu, iter->started);
1682 /* Don't print started cpu buffer for the first entry of the trace */
1684 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1688 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1690 struct trace_seq *s = &iter->seq;
1691 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1692 struct trace_entry *entry;
1693 struct trace_event *event;
1697 test_cpu_buff_start(iter);
1699 event = ftrace_find_event(entry->type);
1701 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1702 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1703 if (!trace_print_lat_context(iter))
1706 if (!trace_print_context(iter))
1712 return event->trace(iter, sym_flags);
1714 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1717 return TRACE_TYPE_HANDLED;
1719 return TRACE_TYPE_PARTIAL_LINE;
1722 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1724 struct trace_seq *s = &iter->seq;
1725 struct trace_entry *entry;
1726 struct trace_event *event;
1730 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1731 if (!trace_seq_printf(s, "%d %d %llu ",
1732 entry->pid, iter->cpu, iter->ts))
1736 event = ftrace_find_event(entry->type);
1738 return event->raw(iter, 0);
1740 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1743 return TRACE_TYPE_HANDLED;
1745 return TRACE_TYPE_PARTIAL_LINE;
1748 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1750 struct trace_seq *s = &iter->seq;
1751 unsigned char newline = '\n';
1752 struct trace_entry *entry;
1753 struct trace_event *event;
1757 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1758 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1759 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1760 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1763 event = ftrace_find_event(entry->type);
1765 enum print_line_t ret = event->hex(iter, 0);
1766 if (ret != TRACE_TYPE_HANDLED)
1770 SEQ_PUT_FIELD_RET(s, newline);
1772 return TRACE_TYPE_HANDLED;
1775 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1777 struct trace_seq *s = &iter->seq;
1778 struct trace_entry *entry;
1779 struct trace_event *event;
1783 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1784 SEQ_PUT_FIELD_RET(s, entry->pid);
1785 SEQ_PUT_FIELD_RET(s, iter->cpu);
1786 SEQ_PUT_FIELD_RET(s, iter->ts);
1789 event = ftrace_find_event(entry->type);
1790 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1793 static int trace_empty(struct trace_iterator *iter)
1797 /* If we are looking at one CPU buffer, only check that one */
1798 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1799 cpu = iter->cpu_file;
1800 if (iter->buffer_iter[cpu]) {
1801 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1804 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1810 for_each_tracing_cpu(cpu) {
1811 if (iter->buffer_iter[cpu]) {
1812 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1815 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1823 /* Called with trace_event_read_lock() held. */
1824 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1826 enum print_line_t ret;
1828 if (iter->trace && iter->trace->print_line) {
1829 ret = iter->trace->print_line(iter);
1830 if (ret != TRACE_TYPE_UNHANDLED)
1834 if (iter->ent->type == TRACE_BPRINT &&
1835 trace_flags & TRACE_ITER_PRINTK &&
1836 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1837 return trace_print_bprintk_msg_only(iter);
1839 if (iter->ent->type == TRACE_PRINT &&
1840 trace_flags & TRACE_ITER_PRINTK &&
1841 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1842 return trace_print_printk_msg_only(iter);
1844 if (trace_flags & TRACE_ITER_BIN)
1845 return print_bin_fmt(iter);
1847 if (trace_flags & TRACE_ITER_HEX)
1848 return print_hex_fmt(iter);
1850 if (trace_flags & TRACE_ITER_RAW)
1851 return print_raw_fmt(iter);
1853 return print_trace_fmt(iter);
1856 static int s_show(struct seq_file *m, void *v)
1858 struct trace_iterator *iter = v;
1860 if (iter->ent == NULL) {
1862 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1865 if (iter->trace && iter->trace->print_header)
1866 iter->trace->print_header(m);
1867 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1868 /* print nothing if the buffers are empty */
1869 if (trace_empty(iter))
1871 print_trace_header(m, iter);
1872 if (!(trace_flags & TRACE_ITER_VERBOSE))
1873 print_lat_help_header(m);
1875 if (!(trace_flags & TRACE_ITER_VERBOSE))
1876 print_func_help_header(m);
1879 print_trace_line(iter);
1880 trace_print_seq(m, &iter->seq);
1886 static struct seq_operations tracer_seq_ops = {
1893 static struct trace_iterator *
1894 __tracing_open(struct inode *inode, struct file *file)
1896 long cpu_file = (long) inode->i_private;
1897 void *fail_ret = ERR_PTR(-ENOMEM);
1898 struct trace_iterator *iter;
1902 if (tracing_disabled)
1903 return ERR_PTR(-ENODEV);
1905 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1907 return ERR_PTR(-ENOMEM);
1910 * We make a copy of the current tracer to avoid concurrent
1911 * changes on it while we are reading.
1913 mutex_lock(&trace_types_lock);
1914 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1919 *iter->trace = *current_trace;
1921 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1924 cpumask_clear(iter->started);
1926 if (current_trace && current_trace->print_max)
1929 iter->tr = &global_trace;
1931 mutex_init(&iter->mutex);
1932 iter->cpu_file = cpu_file;
1934 /* Notify the tracer early; before we stop tracing. */
1935 if (iter->trace && iter->trace->open)
1936 iter->trace->open(iter);
1938 /* Annotate start of buffers if we had overruns */
1939 if (ring_buffer_overruns(iter->tr->buffer))
1940 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1942 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1943 for_each_tracing_cpu(cpu) {
1945 iter->buffer_iter[cpu] =
1946 ring_buffer_read_start(iter->tr->buffer, cpu);
1949 cpu = iter->cpu_file;
1950 iter->buffer_iter[cpu] =
1951 ring_buffer_read_start(iter->tr->buffer, cpu);
1954 /* TODO stop tracer */
1955 ret = seq_open(file, &tracer_seq_ops);
1957 fail_ret = ERR_PTR(ret);
1961 m = file->private_data;
1964 /* stop the trace while dumping */
1967 mutex_unlock(&trace_types_lock);
1972 for_each_tracing_cpu(cpu) {
1973 if (iter->buffer_iter[cpu])
1974 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1976 free_cpumask_var(iter->started);
1978 mutex_unlock(&trace_types_lock);
1985 int tracing_open_generic(struct inode *inode, struct file *filp)
1987 if (tracing_disabled)
1990 filp->private_data = inode->i_private;
1994 static int tracing_release(struct inode *inode, struct file *file)
1996 struct seq_file *m = (struct seq_file *)file->private_data;
1997 struct trace_iterator *iter;
2000 if (!(file->f_mode & FMODE_READ))
2005 mutex_lock(&trace_types_lock);
2006 for_each_tracing_cpu(cpu) {
2007 if (iter->buffer_iter[cpu])
2008 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2011 if (iter->trace && iter->trace->close)
2012 iter->trace->close(iter);
2014 /* reenable tracing if it was previously enabled */
2016 mutex_unlock(&trace_types_lock);
2018 seq_release(inode, file);
2019 mutex_destroy(&iter->mutex);
2020 free_cpumask_var(iter->started);
2026 static int tracing_open(struct inode *inode, struct file *file)
2028 struct trace_iterator *iter;
2031 /* If this file was open for write, then erase contents */
2032 if ((file->f_mode & FMODE_WRITE) &&
2033 !(file->f_flags & O_APPEND)) {
2034 long cpu = (long) inode->i_private;
2036 if (cpu == TRACE_PIPE_ALL_CPU)
2037 tracing_reset_online_cpus(&global_trace);
2039 tracing_reset(&global_trace, cpu);
2042 if (file->f_mode & FMODE_READ) {
2043 iter = __tracing_open(inode, file);
2045 ret = PTR_ERR(iter);
2046 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2047 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2053 t_next(struct seq_file *m, void *v, loff_t *pos)
2055 struct tracer *t = v;
2065 static void *t_start(struct seq_file *m, loff_t *pos)
2070 mutex_lock(&trace_types_lock);
2071 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2077 static void t_stop(struct seq_file *m, void *p)
2079 mutex_unlock(&trace_types_lock);
2082 static int t_show(struct seq_file *m, void *v)
2084 struct tracer *t = v;
2089 seq_printf(m, "%s", t->name);
2098 static struct seq_operations show_traces_seq_ops = {
2105 static int show_traces_open(struct inode *inode, struct file *file)
2107 if (tracing_disabled)
2110 return seq_open(file, &show_traces_seq_ops);
2114 tracing_write_stub(struct file *filp, const char __user *ubuf,
2115 size_t count, loff_t *ppos)
2120 static const struct file_operations tracing_fops = {
2121 .open = tracing_open,
2123 .write = tracing_write_stub,
2124 .llseek = seq_lseek,
2125 .release = tracing_release,
2128 static const struct file_operations show_traces_fops = {
2129 .open = show_traces_open,
2131 .release = seq_release,
2135 * Only trace on a CPU if the bitmask is set:
2137 static cpumask_var_t tracing_cpumask;
2140 * The tracer itself will not take this lock, but still we want
2141 * to provide a consistent cpumask to user-space:
2143 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2146 * Temporary storage for the character representation of the
2147 * CPU bitmask (and one more byte for the newline):
2149 static char mask_str[NR_CPUS + 1];
2152 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2153 size_t count, loff_t *ppos)
2157 mutex_lock(&tracing_cpumask_update_lock);
2159 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2160 if (count - len < 2) {
2164 len += sprintf(mask_str + len, "\n");
2165 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2168 mutex_unlock(&tracing_cpumask_update_lock);
2174 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2175 size_t count, loff_t *ppos)
2178 cpumask_var_t tracing_cpumask_new;
2180 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2183 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2187 mutex_lock(&tracing_cpumask_update_lock);
2189 local_irq_disable();
2190 __raw_spin_lock(&ftrace_max_lock);
2191 for_each_tracing_cpu(cpu) {
2193 * Increase/decrease the disabled counter if we are
2194 * about to flip a bit in the cpumask:
2196 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2197 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2198 atomic_inc(&global_trace.data[cpu]->disabled);
2200 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2201 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2202 atomic_dec(&global_trace.data[cpu]->disabled);
2205 __raw_spin_unlock(&ftrace_max_lock);
2208 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2210 mutex_unlock(&tracing_cpumask_update_lock);
2211 free_cpumask_var(tracing_cpumask_new);
2216 free_cpumask_var(tracing_cpumask_new);
2221 static const struct file_operations tracing_cpumask_fops = {
2222 .open = tracing_open_generic,
2223 .read = tracing_cpumask_read,
2224 .write = tracing_cpumask_write,
2228 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2229 size_t cnt, loff_t *ppos)
2231 struct tracer_opt *trace_opts;
2239 /* calculate max size */
2240 for (i = 0; trace_options[i]; i++) {
2241 len += strlen(trace_options[i]);
2242 len += 3; /* "no" and newline */
2245 mutex_lock(&trace_types_lock);
2246 tracer_flags = current_trace->flags->val;
2247 trace_opts = current_trace->flags->opts;
2250 * Increase the size with names of options specific
2251 * of the current tracer.
2253 for (i = 0; trace_opts[i].name; i++) {
2254 len += strlen(trace_opts[i].name);
2255 len += 3; /* "no" and newline */
2258 /* +2 for \n and \0 */
2259 buf = kmalloc(len + 2, GFP_KERNEL);
2261 mutex_unlock(&trace_types_lock);
2265 for (i = 0; trace_options[i]; i++) {
2266 if (trace_flags & (1 << i))
2267 r += sprintf(buf + r, "%s\n", trace_options[i]);
2269 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2272 for (i = 0; trace_opts[i].name; i++) {
2273 if (tracer_flags & trace_opts[i].bit)
2274 r += sprintf(buf + r, "%s\n",
2275 trace_opts[i].name);
2277 r += sprintf(buf + r, "no%s\n",
2278 trace_opts[i].name);
2280 mutex_unlock(&trace_types_lock);
2282 WARN_ON(r >= len + 2);
2284 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2290 /* Try to assign a tracer specific option */
2291 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2293 struct tracer_flags *trace_flags = trace->flags;
2294 struct tracer_opt *opts = NULL;
2298 for (i = 0; trace_flags->opts[i].name; i++) {
2299 opts = &trace_flags->opts[i];
2300 len = strlen(opts->name);
2302 if (strncmp(cmp, opts->name, len) == 0) {
2303 ret = trace->set_flag(trace_flags->val,
2309 if (!trace_flags->opts[i].name)
2312 /* Refused to handle */
2317 trace_flags->val &= ~opts->bit;
2319 trace_flags->val |= opts->bit;
2324 static void set_tracer_flags(unsigned int mask, int enabled)
2326 /* do nothing if flag is already set */
2327 if (!!(trace_flags & mask) == !!enabled)
2331 trace_flags |= mask;
2333 trace_flags &= ~mask;
2335 if (mask == TRACE_ITER_GLOBAL_CLK) {
2339 func = trace_clock_global;
2341 func = trace_clock_local;
2343 mutex_lock(&trace_types_lock);
2344 ring_buffer_set_clock(global_trace.buffer, func);
2347 ring_buffer_set_clock(max_tr.buffer, func);
2348 mutex_unlock(&trace_types_lock);
2353 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2354 size_t cnt, loff_t *ppos)
2362 if (cnt >= sizeof(buf))
2365 if (copy_from_user(&buf, ubuf, cnt))
2370 if (strncmp(buf, "no", 2) == 0) {
2375 for (i = 0; trace_options[i]; i++) {
2376 int len = strlen(trace_options[i]);
2378 if (strncmp(cmp, trace_options[i], len) == 0) {
2379 set_tracer_flags(1 << i, !neg);
2384 /* If no option could be set, test the specific tracer options */
2385 if (!trace_options[i]) {
2386 mutex_lock(&trace_types_lock);
2387 ret = set_tracer_option(current_trace, cmp, neg);
2388 mutex_unlock(&trace_types_lock);
2398 static const struct file_operations tracing_iter_fops = {
2399 .open = tracing_open_generic,
2400 .read = tracing_trace_options_read,
2401 .write = tracing_trace_options_write,
2404 static const char readme_msg[] =
2405 "tracing mini-HOWTO:\n\n"
2406 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2407 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2408 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2409 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2411 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2412 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2414 "# cat /sys/kernel/debug/tracing/trace_options\n"
2415 "noprint-parent nosym-offset nosym-addr noverbose\n"
2416 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2417 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2418 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2419 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2423 tracing_readme_read(struct file *filp, char __user *ubuf,
2424 size_t cnt, loff_t *ppos)
2426 return simple_read_from_buffer(ubuf, cnt, ppos,
2427 readme_msg, strlen(readme_msg));
2430 static const struct file_operations tracing_readme_fops = {
2431 .open = tracing_open_generic,
2432 .read = tracing_readme_read,
2436 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2437 size_t cnt, loff_t *ppos)
2446 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2450 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2458 for (i = 0; i < SAVED_CMDLINES; i++) {
2461 pid = map_cmdline_to_pid[i];
2462 if (pid == -1 || pid == NO_CMDLINE_MAP)
2465 trace_find_cmdline(pid, buf_comm);
2466 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2471 len = simple_read_from_buffer(ubuf, cnt, ppos,
2480 static const struct file_operations tracing_saved_cmdlines_fops = {
2481 .open = tracing_open_generic,
2482 .read = tracing_saved_cmdlines_read,
2486 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2487 size_t cnt, loff_t *ppos)
2492 r = sprintf(buf, "%u\n", tracer_enabled);
2493 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2497 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2498 size_t cnt, loff_t *ppos)
2500 struct trace_array *tr = filp->private_data;
2505 if (cnt >= sizeof(buf))
2508 if (copy_from_user(&buf, ubuf, cnt))
2513 ret = strict_strtoul(buf, 10, &val);
2519 mutex_lock(&trace_types_lock);
2520 if (tracer_enabled ^ val) {
2523 if (current_trace->start)
2524 current_trace->start(tr);
2529 if (current_trace->stop)
2530 current_trace->stop(tr);
2533 mutex_unlock(&trace_types_lock);
2541 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2542 size_t cnt, loff_t *ppos)
2544 char buf[max_tracer_type_len+2];
2547 mutex_lock(&trace_types_lock);
2549 r = sprintf(buf, "%s\n", current_trace->name);
2551 r = sprintf(buf, "\n");
2552 mutex_unlock(&trace_types_lock);
2554 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2557 int tracer_init(struct tracer *t, struct trace_array *tr)
2559 tracing_reset_online_cpus(tr);
2563 static int tracing_resize_ring_buffer(unsigned long size)
2568 * If kernel or user changes the size of the ring buffer
2569 * we use the size that was given, and we can forget about
2570 * expanding it later.
2572 ring_buffer_expanded = 1;
2574 ret = ring_buffer_resize(global_trace.buffer, size);
2578 ret = ring_buffer_resize(max_tr.buffer, size);
2582 r = ring_buffer_resize(global_trace.buffer,
2583 global_trace.entries);
2586 * AARGH! We are left with different
2587 * size max buffer!!!!
2588 * The max buffer is our "snapshot" buffer.
2589 * When a tracer needs a snapshot (one of the
2590 * latency tracers), it swaps the max buffer
2591 * with the saved snap shot. We succeeded to
2592 * update the size of the main buffer, but failed to
2593 * update the size of the max buffer. But when we tried
2594 * to reset the main buffer to the original size, we
2595 * failed there too. This is very unlikely to
2596 * happen, but if it does, warn and kill all
2600 tracing_disabled = 1;
2605 global_trace.entries = size;
2611 * tracing_update_buffers - used by tracing facility to expand ring buffers
2613 * To save on memory when the tracing is never used on a system with it
2614 * configured in. The ring buffers are set to a minimum size. But once
2615 * a user starts to use the tracing facility, then they need to grow
2616 * to their default size.
2618 * This function is to be called when a tracer is about to be used.
2620 int tracing_update_buffers(void)
2624 mutex_lock(&trace_types_lock);
2625 if (!ring_buffer_expanded)
2626 ret = tracing_resize_ring_buffer(trace_buf_size);
2627 mutex_unlock(&trace_types_lock);
2632 struct trace_option_dentry;
2634 static struct trace_option_dentry *
2635 create_trace_option_files(struct tracer *tracer);
2638 destroy_trace_option_files(struct trace_option_dentry *topts);
2640 static int tracing_set_tracer(const char *buf)
2642 static struct trace_option_dentry *topts;
2643 struct trace_array *tr = &global_trace;
2647 mutex_lock(&trace_types_lock);
2649 if (!ring_buffer_expanded) {
2650 ret = tracing_resize_ring_buffer(trace_buf_size);
2656 for (t = trace_types; t; t = t->next) {
2657 if (strcmp(t->name, buf) == 0)
2664 if (t == current_trace)
2667 trace_branch_disable();
2668 if (current_trace && current_trace->reset)
2669 current_trace->reset(tr);
2671 destroy_trace_option_files(topts);
2675 topts = create_trace_option_files(current_trace);
2678 ret = tracer_init(t, tr);
2683 trace_branch_enable(tr);
2685 mutex_unlock(&trace_types_lock);
2691 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2692 size_t cnt, loff_t *ppos)
2694 char buf[max_tracer_type_len+1];
2701 if (cnt > max_tracer_type_len)
2702 cnt = max_tracer_type_len;
2704 if (copy_from_user(&buf, ubuf, cnt))
2709 /* strip ending whitespace. */
2710 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2713 err = tracing_set_tracer(buf);
2723 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2724 size_t cnt, loff_t *ppos)
2726 unsigned long *ptr = filp->private_data;
2730 r = snprintf(buf, sizeof(buf), "%ld\n",
2731 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2732 if (r > sizeof(buf))
2734 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2738 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2739 size_t cnt, loff_t *ppos)
2741 unsigned long *ptr = filp->private_data;
2746 if (cnt >= sizeof(buf))
2749 if (copy_from_user(&buf, ubuf, cnt))
2754 ret = strict_strtoul(buf, 10, &val);
2763 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2765 long cpu_file = (long) inode->i_private;
2766 struct trace_iterator *iter;
2769 if (tracing_disabled)
2772 mutex_lock(&trace_types_lock);
2774 /* We only allow one reader per cpu */
2775 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2776 if (!cpumask_empty(tracing_reader_cpumask)) {
2780 cpumask_setall(tracing_reader_cpumask);
2782 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2783 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2790 /* create a buffer to store the information to pass to userspace */
2791 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2798 * We make a copy of the current tracer to avoid concurrent
2799 * changes on it while we are reading.
2801 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2807 *iter->trace = *current_trace;
2809 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2814 /* trace pipe does not show start of buffer */
2815 cpumask_setall(iter->started);
2817 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2818 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2820 iter->cpu_file = cpu_file;
2821 iter->tr = &global_trace;
2822 mutex_init(&iter->mutex);
2823 filp->private_data = iter;
2825 if (iter->trace->pipe_open)
2826 iter->trace->pipe_open(iter);
2829 mutex_unlock(&trace_types_lock);
2835 mutex_unlock(&trace_types_lock);
2839 static int tracing_release_pipe(struct inode *inode, struct file *file)
2841 struct trace_iterator *iter = file->private_data;
2843 mutex_lock(&trace_types_lock);
2845 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2846 cpumask_clear(tracing_reader_cpumask);
2848 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2850 mutex_unlock(&trace_types_lock);
2852 free_cpumask_var(iter->started);
2853 mutex_destroy(&iter->mutex);
2861 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2863 struct trace_iterator *iter = filp->private_data;
2865 if (trace_flags & TRACE_ITER_BLOCK) {
2867 * Always select as readable when in blocking mode
2869 return POLLIN | POLLRDNORM;
2871 if (!trace_empty(iter))
2872 return POLLIN | POLLRDNORM;
2873 poll_wait(filp, &trace_wait, poll_table);
2874 if (!trace_empty(iter))
2875 return POLLIN | POLLRDNORM;
2882 void default_wait_pipe(struct trace_iterator *iter)
2886 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2888 if (trace_empty(iter))
2891 finish_wait(&trace_wait, &wait);
2895 * This is a make-shift waitqueue.
2896 * A tracer might use this callback on some rare cases:
2898 * 1) the current tracer might hold the runqueue lock when it wakes up
2899 * a reader, hence a deadlock (sched, function, and function graph tracers)
2900 * 2) the function tracers, trace all functions, we don't want
2901 * the overhead of calling wake_up and friends
2902 * (and tracing them too)
2904 * Anyway, this is really very primitive wakeup.
2906 void poll_wait_pipe(struct trace_iterator *iter)
2908 set_current_state(TASK_INTERRUPTIBLE);
2909 /* sleep for 100 msecs, and try again. */
2910 schedule_timeout(HZ / 10);
2913 /* Must be called with trace_types_lock mutex held. */
2914 static int tracing_wait_pipe(struct file *filp)
2916 struct trace_iterator *iter = filp->private_data;
2918 while (trace_empty(iter)) {
2920 if ((filp->f_flags & O_NONBLOCK)) {
2924 mutex_unlock(&iter->mutex);
2926 iter->trace->wait_pipe(iter);
2928 mutex_lock(&iter->mutex);
2930 if (signal_pending(current))
2934 * We block until we read something and tracing is disabled.
2935 * We still block if tracing is disabled, but we have never
2936 * read anything. This allows a user to cat this file, and
2937 * then enable tracing. But after we have read something,
2938 * we give an EOF when tracing is again disabled.
2940 * iter->pos will be 0 if we haven't read anything.
2942 if (!tracer_enabled && iter->pos)
2953 tracing_read_pipe(struct file *filp, char __user *ubuf,
2954 size_t cnt, loff_t *ppos)
2956 struct trace_iterator *iter = filp->private_data;
2957 static struct tracer *old_tracer;
2960 /* return any leftover data */
2961 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2965 trace_seq_init(&iter->seq);
2967 /* copy the tracer to avoid using a global lock all around */
2968 mutex_lock(&trace_types_lock);
2969 if (unlikely(old_tracer != current_trace && current_trace)) {
2970 old_tracer = current_trace;
2971 *iter->trace = *current_trace;
2973 mutex_unlock(&trace_types_lock);
2976 * Avoid more than one consumer on a single file descriptor
2977 * This is just a matter of traces coherency, the ring buffer itself
2980 mutex_lock(&iter->mutex);
2981 if (iter->trace->read) {
2982 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2988 sret = tracing_wait_pipe(filp);
2992 /* stop when tracing is finished */
2993 if (trace_empty(iter)) {
2998 if (cnt >= PAGE_SIZE)
2999 cnt = PAGE_SIZE - 1;
3001 /* reset all but tr, trace, and overruns */
3002 memset(&iter->seq, 0,
3003 sizeof(struct trace_iterator) -
3004 offsetof(struct trace_iterator, seq));
3007 trace_event_read_lock();
3008 while (find_next_entry_inc(iter) != NULL) {
3009 enum print_line_t ret;
3010 int len = iter->seq.len;
3012 ret = print_trace_line(iter);
3013 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3014 /* don't print partial lines */
3015 iter->seq.len = len;
3018 if (ret != TRACE_TYPE_NO_CONSUME)
3019 trace_consume(iter);
3021 if (iter->seq.len >= cnt)
3024 trace_event_read_unlock();
3026 /* Now copy what we have to the user */
3027 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3028 if (iter->seq.readpos >= iter->seq.len)
3029 trace_seq_init(&iter->seq);
3032 * If there was nothing to send to user, inspite of consuming trace
3033 * entries, go back to wait for more entries.
3039 mutex_unlock(&iter->mutex);
3044 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3045 struct pipe_buffer *buf)
3047 __free_page(buf->page);
3050 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3053 __free_page(spd->pages[idx]);
3056 static struct pipe_buf_operations tracing_pipe_buf_ops = {
3058 .map = generic_pipe_buf_map,
3059 .unmap = generic_pipe_buf_unmap,
3060 .confirm = generic_pipe_buf_confirm,
3061 .release = tracing_pipe_buf_release,
3062 .steal = generic_pipe_buf_steal,
3063 .get = generic_pipe_buf_get,
3067 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3072 /* Seq buffer is page-sized, exactly what we need. */
3074 count = iter->seq.len;
3075 ret = print_trace_line(iter);
3076 count = iter->seq.len - count;
3079 iter->seq.len -= count;
3082 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3083 iter->seq.len -= count;
3087 trace_consume(iter);
3089 if (!find_next_entry_inc(iter)) {
3099 static ssize_t tracing_splice_read_pipe(struct file *filp,
3101 struct pipe_inode_info *pipe,
3105 struct page *pages[PIPE_BUFFERS];
3106 struct partial_page partial[PIPE_BUFFERS];
3107 struct trace_iterator *iter = filp->private_data;
3108 struct splice_pipe_desc spd = {
3111 .nr_pages = 0, /* This gets updated below. */
3113 .ops = &tracing_pipe_buf_ops,
3114 .spd_release = tracing_spd_release_pipe,
3116 static struct tracer *old_tracer;
3121 /* copy the tracer to avoid using a global lock all around */
3122 mutex_lock(&trace_types_lock);
3123 if (unlikely(old_tracer != current_trace && current_trace)) {
3124 old_tracer = current_trace;
3125 *iter->trace = *current_trace;
3127 mutex_unlock(&trace_types_lock);
3129 mutex_lock(&iter->mutex);
3131 if (iter->trace->splice_read) {
3132 ret = iter->trace->splice_read(iter, filp,
3133 ppos, pipe, len, flags);
3138 ret = tracing_wait_pipe(filp);
3142 if (!iter->ent && !find_next_entry_inc(iter)) {
3147 trace_event_read_lock();
3149 /* Fill as many pages as possible. */
3150 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3151 pages[i] = alloc_page(GFP_KERNEL);
3155 rem = tracing_fill_pipe_page(rem, iter);
3157 /* Copy the data into the page, so we can start over. */
3158 ret = trace_seq_to_buffer(&iter->seq,
3159 page_address(pages[i]),
3162 __free_page(pages[i]);
3165 partial[i].offset = 0;
3166 partial[i].len = iter->seq.len;
3168 trace_seq_init(&iter->seq);
3171 trace_event_read_unlock();
3172 mutex_unlock(&iter->mutex);
3176 return splice_to_pipe(pipe, &spd);
3179 mutex_unlock(&iter->mutex);
3185 tracing_entries_read(struct file *filp, char __user *ubuf,
3186 size_t cnt, loff_t *ppos)
3188 struct trace_array *tr = filp->private_data;
3192 mutex_lock(&trace_types_lock);
3193 if (!ring_buffer_expanded)
3194 r = sprintf(buf, "%lu (expanded: %lu)\n",
3196 trace_buf_size >> 10);
3198 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3199 mutex_unlock(&trace_types_lock);
3201 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3205 tracing_entries_write(struct file *filp, const char __user *ubuf,
3206 size_t cnt, loff_t *ppos)
3212 if (cnt >= sizeof(buf))
3215 if (copy_from_user(&buf, ubuf, cnt))
3220 ret = strict_strtoul(buf, 10, &val);
3224 /* must have at least 1 entry */
3228 mutex_lock(&trace_types_lock);
3232 /* disable all cpu buffers */
3233 for_each_tracing_cpu(cpu) {
3234 if (global_trace.data[cpu])
3235 atomic_inc(&global_trace.data[cpu]->disabled);
3236 if (max_tr.data[cpu])
3237 atomic_inc(&max_tr.data[cpu]->disabled);
3240 /* value is in KB */
3243 if (val != global_trace.entries) {
3244 ret = tracing_resize_ring_buffer(val);
3253 /* If check pages failed, return ENOMEM */
3254 if (tracing_disabled)
3257 for_each_tracing_cpu(cpu) {
3258 if (global_trace.data[cpu])
3259 atomic_dec(&global_trace.data[cpu]->disabled);
3260 if (max_tr.data[cpu])
3261 atomic_dec(&max_tr.data[cpu]->disabled);
3265 max_tr.entries = global_trace.entries;
3266 mutex_unlock(&trace_types_lock);
3271 static int mark_printk(const char *fmt, ...)
3275 va_start(args, fmt);
3276 ret = trace_vprintk(0, fmt, args);
3282 tracing_mark_write(struct file *filp, const char __user *ubuf,
3283 size_t cnt, loff_t *fpos)
3288 if (tracing_disabled)
3291 if (cnt > TRACE_BUF_SIZE)
3292 cnt = TRACE_BUF_SIZE;
3294 buf = kmalloc(cnt + 1, GFP_KERNEL);
3298 if (copy_from_user(buf, ubuf, cnt)) {
3303 /* Cut from the first nil or newline. */
3305 end = strchr(buf, '\n');
3309 cnt = mark_printk("%s\n", buf);
3316 static const struct file_operations tracing_max_lat_fops = {
3317 .open = tracing_open_generic,
3318 .read = tracing_max_lat_read,
3319 .write = tracing_max_lat_write,
3322 static const struct file_operations tracing_ctrl_fops = {
3323 .open = tracing_open_generic,
3324 .read = tracing_ctrl_read,
3325 .write = tracing_ctrl_write,
3328 static const struct file_operations set_tracer_fops = {
3329 .open = tracing_open_generic,
3330 .read = tracing_set_trace_read,
3331 .write = tracing_set_trace_write,
3334 static const struct file_operations tracing_pipe_fops = {
3335 .open = tracing_open_pipe,
3336 .poll = tracing_poll_pipe,
3337 .read = tracing_read_pipe,
3338 .splice_read = tracing_splice_read_pipe,
3339 .release = tracing_release_pipe,
3342 static const struct file_operations tracing_entries_fops = {
3343 .open = tracing_open_generic,
3344 .read = tracing_entries_read,
3345 .write = tracing_entries_write,
3348 static const struct file_operations tracing_mark_fops = {
3349 .open = tracing_open_generic,
3350 .write = tracing_mark_write,
3353 struct ftrace_buffer_info {
3354 struct trace_array *tr;
3360 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3362 int cpu = (int)(long)inode->i_private;
3363 struct ftrace_buffer_info *info;
3365 if (tracing_disabled)
3368 info = kzalloc(sizeof(*info), GFP_KERNEL);
3372 info->tr = &global_trace;
3375 /* Force reading ring buffer for first read */
3376 info->read = (unsigned int)-1;
3378 filp->private_data = info;
3380 return nonseekable_open(inode, filp);
3384 tracing_buffers_read(struct file *filp, char __user *ubuf,
3385 size_t count, loff_t *ppos)
3387 struct ftrace_buffer_info *info = filp->private_data;
3396 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3400 /* Do we have previous read data to read? */
3401 if (info->read < PAGE_SIZE)
3406 ret = ring_buffer_read_page(info->tr->buffer,
3413 pos = ring_buffer_page_len(info->spare);
3415 if (pos < PAGE_SIZE)
3416 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3419 size = PAGE_SIZE - info->read;
3423 ret = copy_to_user(ubuf, info->spare + info->read, size);
3434 static int tracing_buffers_release(struct inode *inode, struct file *file)
3436 struct ftrace_buffer_info *info = file->private_data;
3439 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3446 struct ring_buffer *buffer;
3451 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3452 struct pipe_buffer *buf)
3454 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3459 ring_buffer_free_read_page(ref->buffer, ref->page);
3464 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3465 struct pipe_buffer *buf)
3470 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3471 struct pipe_buffer *buf)
3473 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3478 /* Pipe buffer operations for a buffer. */
3479 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3481 .map = generic_pipe_buf_map,
3482 .unmap = generic_pipe_buf_unmap,
3483 .confirm = generic_pipe_buf_confirm,
3484 .release = buffer_pipe_buf_release,
3485 .steal = buffer_pipe_buf_steal,
3486 .get = buffer_pipe_buf_get,
3490 * Callback from splice_to_pipe(), if we need to release some pages
3491 * at the end of the spd in case we error'ed out in filling the pipe.
3493 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3495 struct buffer_ref *ref =
3496 (struct buffer_ref *)spd->partial[i].private;
3501 ring_buffer_free_read_page(ref->buffer, ref->page);
3503 spd->partial[i].private = 0;
3507 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3508 struct pipe_inode_info *pipe, size_t len,
3511 struct ftrace_buffer_info *info = file->private_data;
3512 struct partial_page partial[PIPE_BUFFERS];
3513 struct page *pages[PIPE_BUFFERS];
3514 struct splice_pipe_desc spd = {
3518 .ops = &buffer_pipe_buf_ops,
3519 .spd_release = buffer_spd_release,
3521 struct buffer_ref *ref;
3522 int entries, size, i;
3525 if (*ppos & (PAGE_SIZE - 1)) {
3526 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3530 if (len & (PAGE_SIZE - 1)) {
3531 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3532 if (len < PAGE_SIZE)
3537 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3539 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
3543 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3548 ref->buffer = info->tr->buffer;
3549 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3555 r = ring_buffer_read_page(ref->buffer, &ref->page,
3558 ring_buffer_free_read_page(ref->buffer,
3565 * zero out any left over data, this is going to
3568 size = ring_buffer_page_len(ref->page);
3569 if (size < PAGE_SIZE)
3570 memset(ref->page + size, 0, PAGE_SIZE - size);
3572 page = virt_to_page(ref->page);
3574 spd.pages[i] = page;
3575 spd.partial[i].len = PAGE_SIZE;
3576 spd.partial[i].offset = 0;
3577 spd.partial[i].private = (unsigned long)ref;
3581 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3586 /* did we read anything? */
3587 if (!spd.nr_pages) {
3588 if (flags & SPLICE_F_NONBLOCK)
3596 ret = splice_to_pipe(pipe, &spd);
3601 static const struct file_operations tracing_buffers_fops = {
3602 .open = tracing_buffers_open,
3603 .read = tracing_buffers_read,
3604 .release = tracing_buffers_release,
3605 .splice_read = tracing_buffers_splice_read,
3606 .llseek = no_llseek,
3610 tracing_stats_read(struct file *filp, char __user *ubuf,
3611 size_t count, loff_t *ppos)
3613 unsigned long cpu = (unsigned long)filp->private_data;
3614 struct trace_array *tr = &global_trace;
3615 struct trace_seq *s;
3618 s = kmalloc(sizeof(*s), GFP_KERNEL);
3624 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3625 trace_seq_printf(s, "entries: %ld\n", cnt);
3627 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3628 trace_seq_printf(s, "overrun: %ld\n", cnt);
3630 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3631 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3633 cnt = ring_buffer_nmi_dropped_cpu(tr->buffer, cpu);
3634 trace_seq_printf(s, "nmi dropped: %ld\n", cnt);
3636 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3643 static const struct file_operations tracing_stats_fops = {
3644 .open = tracing_open_generic,
3645 .read = tracing_stats_read,
3648 #ifdef CONFIG_DYNAMIC_FTRACE
3650 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3656 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3657 size_t cnt, loff_t *ppos)
3659 static char ftrace_dyn_info_buffer[1024];
3660 static DEFINE_MUTEX(dyn_info_mutex);
3661 unsigned long *p = filp->private_data;
3662 char *buf = ftrace_dyn_info_buffer;
3663 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3666 mutex_lock(&dyn_info_mutex);
3667 r = sprintf(buf, "%ld ", *p);
3669 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3672 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3674 mutex_unlock(&dyn_info_mutex);
3679 static const struct file_operations tracing_dyn_info_fops = {
3680 .open = tracing_open_generic,
3681 .read = tracing_read_dyn_info,
3685 static struct dentry *d_tracer;
3687 struct dentry *tracing_init_dentry(void)
3694 if (!debugfs_initialized())
3697 d_tracer = debugfs_create_dir("tracing", NULL);
3699 if (!d_tracer && !once) {
3701 pr_warning("Could not create debugfs directory 'tracing'\n");
3708 static struct dentry *d_percpu;
3710 struct dentry *tracing_dentry_percpu(void)
3713 struct dentry *d_tracer;
3718 d_tracer = tracing_init_dentry();
3723 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3725 if (!d_percpu && !once) {
3727 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3734 static void tracing_init_debugfs_percpu(long cpu)
3736 struct dentry *d_percpu = tracing_dentry_percpu();
3737 struct dentry *d_cpu;
3738 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3741 if (cpu > 999 || cpu < 0)
3744 sprintf(cpu_dir, "cpu%ld", cpu);
3745 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3747 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3751 /* per cpu trace_pipe */
3752 trace_create_file("trace_pipe", 0444, d_cpu,
3753 (void *) cpu, &tracing_pipe_fops);
3756 trace_create_file("trace", 0644, d_cpu,
3757 (void *) cpu, &tracing_fops);
3759 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3760 (void *) cpu, &tracing_buffers_fops);
3762 trace_create_file("stats", 0444, d_cpu,
3763 (void *) cpu, &tracing_stats_fops);
3766 #ifdef CONFIG_FTRACE_SELFTEST
3767 /* Let selftest have access to static functions in this file */
3768 #include "trace_selftest.c"
3771 struct trace_option_dentry {
3772 struct tracer_opt *opt;
3773 struct tracer_flags *flags;
3774 struct dentry *entry;
3778 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3781 struct trace_option_dentry *topt = filp->private_data;
3784 if (topt->flags->val & topt->opt->bit)
3789 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3793 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3796 struct trace_option_dentry *topt = filp->private_data;
3801 if (cnt >= sizeof(buf))
3804 if (copy_from_user(&buf, ubuf, cnt))
3809 ret = strict_strtoul(buf, 10, &val);
3816 /* do nothing if already cleared */
3817 if (!(topt->flags->val & topt->opt->bit))
3820 mutex_lock(&trace_types_lock);
3821 if (current_trace->set_flag)
3822 ret = current_trace->set_flag(topt->flags->val,
3824 mutex_unlock(&trace_types_lock);
3827 topt->flags->val &= ~topt->opt->bit;
3830 /* do nothing if already set */
3831 if (topt->flags->val & topt->opt->bit)
3834 mutex_lock(&trace_types_lock);
3835 if (current_trace->set_flag)
3836 ret = current_trace->set_flag(topt->flags->val,
3838 mutex_unlock(&trace_types_lock);
3841 topt->flags->val |= topt->opt->bit;
3854 static const struct file_operations trace_options_fops = {
3855 .open = tracing_open_generic,
3856 .read = trace_options_read,
3857 .write = trace_options_write,
3861 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3864 long index = (long)filp->private_data;
3867 if (trace_flags & (1 << index))
3872 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3876 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3879 long index = (long)filp->private_data;
3884 if (cnt >= sizeof(buf))
3887 if (copy_from_user(&buf, ubuf, cnt))
3892 ret = strict_strtoul(buf, 10, &val);
3898 trace_flags &= ~(1 << index);
3901 trace_flags |= 1 << index;
3913 static const struct file_operations trace_options_core_fops = {
3914 .open = tracing_open_generic,
3915 .read = trace_options_core_read,
3916 .write = trace_options_core_write,
3919 struct dentry *trace_create_file(const char *name,
3921 struct dentry *parent,
3923 const struct file_operations *fops)
3927 ret = debugfs_create_file(name, mode, parent, data, fops);
3929 pr_warning("Could not create debugfs '%s' entry\n", name);
3935 static struct dentry *trace_options_init_dentry(void)
3937 struct dentry *d_tracer;
3938 static struct dentry *t_options;
3943 d_tracer = tracing_init_dentry();
3947 t_options = debugfs_create_dir("options", d_tracer);
3949 pr_warning("Could not create debugfs directory 'options'\n");
3957 create_trace_option_file(struct trace_option_dentry *topt,
3958 struct tracer_flags *flags,
3959 struct tracer_opt *opt)
3961 struct dentry *t_options;
3963 t_options = trace_options_init_dentry();
3967 topt->flags = flags;
3970 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
3971 &trace_options_fops);
3975 static struct trace_option_dentry *
3976 create_trace_option_files(struct tracer *tracer)
3978 struct trace_option_dentry *topts;
3979 struct tracer_flags *flags;
3980 struct tracer_opt *opts;
3986 flags = tracer->flags;
3988 if (!flags || !flags->opts)
3993 for (cnt = 0; opts[cnt].name; cnt++)
3996 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4000 for (cnt = 0; opts[cnt].name; cnt++)
4001 create_trace_option_file(&topts[cnt], flags,
4008 destroy_trace_option_files(struct trace_option_dentry *topts)
4015 for (cnt = 0; topts[cnt].opt; cnt++) {
4016 if (topts[cnt].entry)
4017 debugfs_remove(topts[cnt].entry);
4023 static struct dentry *
4024 create_trace_option_core_file(const char *option, long index)
4026 struct dentry *t_options;
4028 t_options = trace_options_init_dentry();
4032 return trace_create_file(option, 0644, t_options, (void *)index,
4033 &trace_options_core_fops);
4036 static __init void create_trace_options_dir(void)
4038 struct dentry *t_options;
4041 t_options = trace_options_init_dentry();
4045 for (i = 0; trace_options[i]; i++)
4046 create_trace_option_core_file(trace_options[i], i);
4049 static __init int tracer_init_debugfs(void)
4051 struct dentry *d_tracer;
4054 d_tracer = tracing_init_dentry();
4056 trace_create_file("tracing_enabled", 0644, d_tracer,
4057 &global_trace, &tracing_ctrl_fops);
4059 trace_create_file("trace_options", 0644, d_tracer,
4060 NULL, &tracing_iter_fops);
4062 trace_create_file("tracing_cpumask", 0644, d_tracer,
4063 NULL, &tracing_cpumask_fops);
4065 trace_create_file("trace", 0644, d_tracer,
4066 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4068 trace_create_file("available_tracers", 0444, d_tracer,
4069 &global_trace, &show_traces_fops);
4071 trace_create_file("current_tracer", 0644, d_tracer,
4072 &global_trace, &set_tracer_fops);
4074 trace_create_file("tracing_max_latency", 0644, d_tracer,
4075 &tracing_max_latency, &tracing_max_lat_fops);
4077 trace_create_file("tracing_thresh", 0644, d_tracer,
4078 &tracing_thresh, &tracing_max_lat_fops);
4080 trace_create_file("README", 0444, d_tracer,
4081 NULL, &tracing_readme_fops);
4083 trace_create_file("trace_pipe", 0444, d_tracer,
4084 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4086 trace_create_file("buffer_size_kb", 0644, d_tracer,
4087 &global_trace, &tracing_entries_fops);
4089 trace_create_file("trace_marker", 0220, d_tracer,
4090 NULL, &tracing_mark_fops);
4092 trace_create_file("saved_cmdlines", 0444, d_tracer,
4093 NULL, &tracing_saved_cmdlines_fops);
4095 #ifdef CONFIG_DYNAMIC_FTRACE
4096 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4097 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4099 #ifdef CONFIG_SYSPROF_TRACER
4100 init_tracer_sysprof_debugfs(d_tracer);
4103 create_trace_options_dir();
4105 for_each_tracing_cpu(cpu)
4106 tracing_init_debugfs_percpu(cpu);
4111 static int trace_panic_handler(struct notifier_block *this,
4112 unsigned long event, void *unused)
4114 if (ftrace_dump_on_oops)
4119 static struct notifier_block trace_panic_notifier = {
4120 .notifier_call = trace_panic_handler,
4122 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4125 static int trace_die_handler(struct notifier_block *self,
4131 if (ftrace_dump_on_oops)
4140 static struct notifier_block trace_die_notifier = {
4141 .notifier_call = trace_die_handler,
4146 * printk is set to max of 1024, we really don't need it that big.
4147 * Nothing should be printing 1000 characters anyway.
4149 #define TRACE_MAX_PRINT 1000
4152 * Define here KERN_TRACE so that we have one place to modify
4153 * it if we decide to change what log level the ftrace dump
4156 #define KERN_TRACE KERN_EMERG
4159 trace_printk_seq(struct trace_seq *s)
4161 /* Probably should print a warning here. */
4165 /* should be zero ended, but we are paranoid. */
4166 s->buffer[s->len] = 0;
4168 printk(KERN_TRACE "%s", s->buffer);
4173 static void __ftrace_dump(bool disable_tracing)
4175 static raw_spinlock_t ftrace_dump_lock =
4176 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
4177 /* use static because iter can be a bit big for the stack */
4178 static struct trace_iterator iter;
4179 unsigned int old_userobj;
4180 static int dump_ran;
4181 unsigned long flags;
4185 local_irq_save(flags);
4186 __raw_spin_lock(&ftrace_dump_lock);
4194 if (disable_tracing)
4197 for_each_tracing_cpu(cpu) {
4198 atomic_inc(&global_trace.data[cpu]->disabled);
4201 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4203 /* don't look at user memory in panic mode */
4204 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4206 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4208 /* Simulate the iterator */
4209 iter.tr = &global_trace;
4210 iter.trace = current_trace;
4211 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4214 * We need to stop all tracing on all CPUS to read the
4215 * the next buffer. This is a bit expensive, but is
4216 * not done often. We fill all what we can read,
4217 * and then release the locks again.
4220 while (!trace_empty(&iter)) {
4223 printk(KERN_TRACE "---------------------------------\n");
4227 /* reset all but tr, trace, and overruns */
4228 memset(&iter.seq, 0,
4229 sizeof(struct trace_iterator) -
4230 offsetof(struct trace_iterator, seq));
4231 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4234 if (find_next_entry_inc(&iter) != NULL) {
4235 print_trace_line(&iter);
4236 trace_consume(&iter);
4239 trace_printk_seq(&iter.seq);
4243 printk(KERN_TRACE " (ftrace buffer empty)\n");
4245 printk(KERN_TRACE "---------------------------------\n");
4247 /* Re-enable tracing if requested */
4248 if (!disable_tracing) {
4249 trace_flags |= old_userobj;
4251 for_each_tracing_cpu(cpu) {
4252 atomic_dec(&global_trace.data[cpu]->disabled);
4258 __raw_spin_unlock(&ftrace_dump_lock);
4259 local_irq_restore(flags);
4262 /* By default: disable tracing after the dump */
4263 void ftrace_dump(void)
4265 __ftrace_dump(true);
4268 __init static int tracer_alloc_buffers(void)
4270 struct trace_array_cpu *data;
4275 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4278 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4279 goto out_free_buffer_mask;
4281 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4282 goto out_free_tracing_cpumask;
4284 /* To save memory, keep the ring buffer size to its minimum */
4285 if (ring_buffer_expanded)
4286 ring_buf_size = trace_buf_size;
4290 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4291 cpumask_copy(tracing_cpumask, cpu_all_mask);
4292 cpumask_clear(tracing_reader_cpumask);
4294 /* TODO: make the number of buffers hot pluggable with CPUS */
4295 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4296 TRACE_BUFFER_FLAGS);
4297 if (!global_trace.buffer) {
4298 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4300 goto out_free_cpumask;
4302 global_trace.entries = ring_buffer_size(global_trace.buffer);
4305 #ifdef CONFIG_TRACER_MAX_TRACE
4306 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4307 TRACE_BUFFER_FLAGS);
4308 if (!max_tr.buffer) {
4309 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4311 ring_buffer_free(global_trace.buffer);
4312 goto out_free_cpumask;
4314 max_tr.entries = ring_buffer_size(max_tr.buffer);
4315 WARN_ON(max_tr.entries != global_trace.entries);
4318 /* Allocate the first page for all buffers */
4319 for_each_tracing_cpu(i) {
4320 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4321 max_tr.data[i] = &per_cpu(max_data, i);
4324 trace_init_cmdlines();
4326 register_tracer(&nop_trace);
4327 current_trace = &nop_trace;
4328 #ifdef CONFIG_BOOT_TRACER
4329 register_tracer(&boot_tracer);
4331 /* All seems OK, enable tracing */
4332 tracing_disabled = 0;
4334 atomic_notifier_chain_register(&panic_notifier_list,
4335 &trace_panic_notifier);
4337 register_die_notifier(&trace_die_notifier);
4342 free_cpumask_var(tracing_reader_cpumask);
4343 out_free_tracing_cpumask:
4344 free_cpumask_var(tracing_cpumask);
4345 out_free_buffer_mask:
4346 free_cpumask_var(tracing_buffer_mask);
4351 __init static int clear_boot_tracer(void)
4354 * The default tracer at boot buffer is an init section.
4355 * This function is called in lateinit. If we did not
4356 * find the boot tracer, then clear it out, to prevent
4357 * later registration from accessing the buffer that is
4358 * about to be freed.
4360 if (!default_bootup_tracer)
4363 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4364 default_bootup_tracer);
4365 default_bootup_tracer = NULL;
4370 early_initcall(tracer_alloc_buffers);
4371 fs_initcall(tracer_init_debugfs);
4372 late_initcall(clear_boot_tracer);