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/ctype.h>
34 #include <linux/init.h>
35 #include <linux/poll.h>
36 #include <linux/gfp.h>
40 #include "trace_output.h"
42 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
44 unsigned long __read_mostly tracing_max_latency;
45 unsigned long __read_mostly tracing_thresh;
48 * We need to change this state when a selftest is running.
49 * A selftest will lurk into the ring-buffer to count the
50 * entries inserted during the selftest although some concurrent
51 * insertions into the ring-buffer such as ftrace_printk could occurred
52 * at the same time, giving false positive or negative results.
54 static bool __read_mostly tracing_selftest_running;
57 * If a tracer is running, we do not want to run SELFTEST.
59 static bool __read_mostly tracing_selftest_disabled;
61 /* For tracers that don't implement custom flags */
62 static struct tracer_opt dummy_tracer_opt[] = {
66 static struct tracer_flags dummy_tracer_flags = {
68 .opts = dummy_tracer_opt
71 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
77 * Kill all tracing for good (never come back).
78 * It is initialized to 1 but will turn to zero if the initialization
79 * of the tracer is successful. But that is the only place that sets
82 static int tracing_disabled = 1;
84 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
86 static inline void ftrace_disable_cpu(void)
89 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
92 static inline void ftrace_enable_cpu(void)
94 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
98 static cpumask_var_t __read_mostly tracing_buffer_mask;
100 /* Define which cpu buffers are currently read in trace_pipe */
101 static cpumask_var_t tracing_reader_cpumask;
103 #define for_each_tracing_cpu(cpu) \
104 for_each_cpu(cpu, tracing_buffer_mask)
107 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
109 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
110 * is set, then ftrace_dump is called. This will output the contents
111 * of the ftrace buffers to the console. This is very useful for
112 * capturing traces that lead to crashes and outputing it to a
115 * It is default off, but you can enable it with either specifying
116 * "ftrace_dump_on_oops" in the kernel command line, or setting
117 * /proc/sys/kernel/ftrace_dump_on_oops to true.
119 int ftrace_dump_on_oops;
121 static int tracing_set_tracer(const char *buf);
123 #define BOOTUP_TRACER_SIZE 100
124 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
125 static char *default_bootup_tracer;
127 static int __init set_ftrace(char *str)
129 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
130 default_bootup_tracer = bootup_tracer_buf;
133 __setup("ftrace=", set_ftrace);
135 static int __init set_ftrace_dump_on_oops(char *str)
137 ftrace_dump_on_oops = 1;
140 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
143 ns2usecs(cycle_t nsec)
150 cycle_t ftrace_now(int cpu)
152 u64 ts = ring_buffer_time_stamp(cpu);
153 ring_buffer_normalize_time_stamp(cpu, &ts);
158 * The global_trace is the descriptor that holds the tracing
159 * buffers for the live tracing. For each CPU, it contains
160 * a link list of pages that will store trace entries. The
161 * page descriptor of the pages in the memory is used to hold
162 * the link list by linking the lru item in the page descriptor
163 * to each of the pages in the buffer per CPU.
165 * For each active CPU there is a data field that holds the
166 * pages for the buffer for that CPU. Each CPU has the same number
167 * of pages allocated for its buffer.
169 static struct trace_array global_trace;
171 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
174 * The max_tr is used to snapshot the global_trace when a maximum
175 * latency is reached. Some tracers will use this to store a maximum
176 * trace while it continues examining live traces.
178 * The buffers for the max_tr are set up the same as the global_trace.
179 * When a snapshot is taken, the link list of the max_tr is swapped
180 * with the link list of the global_trace and the buffers are reset for
181 * the global_trace so the tracing can continue.
183 static struct trace_array max_tr;
185 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
187 /* tracer_enabled is used to toggle activation of a tracer */
188 static int tracer_enabled = 1;
191 * tracing_is_enabled - return tracer_enabled status
193 * This function is used by other tracers to know the status
194 * of the tracer_enabled flag. Tracers may use this function
195 * to know if it should enable their features when starting
196 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
198 int tracing_is_enabled(void)
200 return tracer_enabled;
204 * trace_buf_size is the size in bytes that is allocated
205 * for a buffer. Note, the number of bytes is always rounded
208 * This number is purposely set to a low number of 16384.
209 * If the dump on oops happens, it will be much appreciated
210 * to not have to wait for all that output. Anyway this can be
211 * boot time and run time configurable.
213 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
215 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
217 /* trace_types holds a link list of available tracers. */
218 static struct tracer *trace_types __read_mostly;
220 /* current_trace points to the tracer that is currently active */
221 static struct tracer *current_trace __read_mostly;
224 * max_tracer_type_len is used to simplify the allocating of
225 * buffers to read userspace tracer names. We keep track of
226 * the longest tracer name registered.
228 static int max_tracer_type_len;
231 * trace_types_lock is used to protect the trace_types list.
232 * This lock is also used to keep user access serialized.
233 * Accesses from userspace will grab this lock while userspace
234 * activities happen inside the kernel.
236 static DEFINE_MUTEX(trace_types_lock);
238 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
239 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
241 /* trace_flags holds trace_options default values */
242 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
243 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
246 * trace_wake_up - wake up tasks waiting for trace input
248 * Simply wakes up any task that is blocked on the trace_wait
249 * queue. These is used with trace_poll for tasks polling the trace.
251 void trace_wake_up(void)
254 * The runqueue_is_locked() can fail, but this is the best we
257 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
258 wake_up(&trace_wait);
261 static int __init set_buf_size(char *str)
263 unsigned long buf_size;
268 ret = strict_strtoul(str, 0, &buf_size);
269 /* nr_entries can not be zero */
270 if (ret < 0 || buf_size == 0)
272 trace_buf_size = buf_size;
275 __setup("trace_buf_size=", set_buf_size);
277 unsigned long nsecs_to_usecs(unsigned long nsecs)
282 /* These must match the bit postions in trace_iterator_flags */
283 static const char *trace_options[] = {
306 * ftrace_max_lock is used to protect the swapping of buffers
307 * when taking a max snapshot. The buffers themselves are
308 * protected by per_cpu spinlocks. But the action of the swap
309 * needs its own lock.
311 * This is defined as a raw_spinlock_t in order to help
312 * with performance when lockdep debugging is enabled.
314 static raw_spinlock_t ftrace_max_lock =
315 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
318 * Copy the new maximum trace into the separate maximum-trace
319 * structure. (this way the maximum trace is permanently saved,
320 * for later retrieval via /debugfs/tracing/latency_trace)
323 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
325 struct trace_array_cpu *data = tr->data[cpu];
328 max_tr.time_start = data->preempt_timestamp;
330 data = max_tr.data[cpu];
331 data->saved_latency = tracing_max_latency;
333 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
334 data->pid = tsk->pid;
335 data->uid = task_uid(tsk);
336 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
337 data->policy = tsk->policy;
338 data->rt_priority = tsk->rt_priority;
340 /* record this tasks comm */
341 tracing_record_cmdline(tsk);
344 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
349 if (s->len <= s->readpos)
352 len = s->len - s->readpos;
355 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
363 ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
368 if (s->len <= s->readpos)
371 len = s->len - s->readpos;
374 ret = memcpy(buf, s->buffer + s->readpos, cnt);
383 trace_print_seq(struct seq_file *m, struct trace_seq *s)
385 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
388 seq_puts(m, s->buffer);
394 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
396 * @tsk: the task with the latency
397 * @cpu: The cpu that initiated the trace.
399 * Flip the buffers between the @tr and the max_tr and record information
400 * about which task was the cause of this latency.
403 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
405 struct ring_buffer *buf = tr->buffer;
407 WARN_ON_ONCE(!irqs_disabled());
408 __raw_spin_lock(&ftrace_max_lock);
410 tr->buffer = max_tr.buffer;
413 ftrace_disable_cpu();
414 ring_buffer_reset(tr->buffer);
417 __update_max_tr(tr, tsk, cpu);
418 __raw_spin_unlock(&ftrace_max_lock);
422 * update_max_tr_single - only copy one trace over, and reset the rest
424 * @tsk - task with the latency
425 * @cpu - the cpu of the buffer to copy.
427 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
430 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
434 WARN_ON_ONCE(!irqs_disabled());
435 __raw_spin_lock(&ftrace_max_lock);
437 ftrace_disable_cpu();
439 ring_buffer_reset(max_tr.buffer);
440 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
444 WARN_ON_ONCE(ret && ret != -EAGAIN);
446 __update_max_tr(tr, tsk, cpu);
447 __raw_spin_unlock(&ftrace_max_lock);
451 * register_tracer - register a tracer with the ftrace system.
452 * @type - the plugin for the tracer
454 * Register a new plugin tracer.
456 int register_tracer(struct tracer *type)
457 __releases(kernel_lock)
458 __acquires(kernel_lock)
465 pr_info("Tracer must have a name\n");
470 * When this gets called we hold the BKL which means that
471 * preemption is disabled. Various trace selftests however
472 * need to disable and enable preemption for successful tests.
473 * So we drop the BKL here and grab it after the tests again.
476 mutex_lock(&trace_types_lock);
478 tracing_selftest_running = true;
480 for (t = trace_types; t; t = t->next) {
481 if (strcmp(type->name, t->name) == 0) {
483 pr_info("Trace %s already registered\n",
491 type->set_flag = &dummy_set_flag;
493 type->flags = &dummy_tracer_flags;
495 if (!type->flags->opts)
496 type->flags->opts = dummy_tracer_opt;
497 if (!type->wait_pipe)
498 type->wait_pipe = default_wait_pipe;
501 #ifdef CONFIG_FTRACE_STARTUP_TEST
502 if (type->selftest && !tracing_selftest_disabled) {
503 struct tracer *saved_tracer = current_trace;
504 struct trace_array *tr = &global_trace;
508 * Run a selftest on this tracer.
509 * Here we reset the trace buffer, and set the current
510 * tracer to be this tracer. The tracer can then run some
511 * internal tracing to verify that everything is in order.
512 * If we fail, we do not register this tracer.
514 for_each_tracing_cpu(i)
515 tracing_reset(tr, i);
517 current_trace = type;
518 /* the test is responsible for initializing and enabling */
519 pr_info("Testing tracer %s: ", type->name);
520 ret = type->selftest(type, tr);
521 /* the test is responsible for resetting too */
522 current_trace = saved_tracer;
524 printk(KERN_CONT "FAILED!\n");
527 /* Only reset on passing, to avoid touching corrupted buffers */
528 for_each_tracing_cpu(i)
529 tracing_reset(tr, i);
531 printk(KERN_CONT "PASSED\n");
535 type->next = trace_types;
537 len = strlen(type->name);
538 if (len > max_tracer_type_len)
539 max_tracer_type_len = len;
542 tracing_selftest_running = false;
543 mutex_unlock(&trace_types_lock);
545 if (ret || !default_bootup_tracer)
548 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
551 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
552 /* Do we want this tracer to start on bootup? */
553 tracing_set_tracer(type->name);
554 default_bootup_tracer = NULL;
555 /* disable other selftests, since this will break it. */
556 tracing_selftest_disabled = 1;
557 #ifdef CONFIG_FTRACE_STARTUP_TEST
558 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
567 void unregister_tracer(struct tracer *type)
572 mutex_lock(&trace_types_lock);
573 for (t = &trace_types; *t; t = &(*t)->next) {
577 pr_info("Trace %s not registered\n", type->name);
583 if (type == current_trace && tracer_enabled) {
586 if (current_trace->stop)
587 current_trace->stop(&global_trace);
588 current_trace = &nop_trace;
591 if (strlen(type->name) != max_tracer_type_len)
594 max_tracer_type_len = 0;
595 for (t = &trace_types; *t; t = &(*t)->next) {
596 len = strlen((*t)->name);
597 if (len > max_tracer_type_len)
598 max_tracer_type_len = len;
601 mutex_unlock(&trace_types_lock);
604 void tracing_reset(struct trace_array *tr, int cpu)
606 ftrace_disable_cpu();
607 ring_buffer_reset_cpu(tr->buffer, cpu);
611 void tracing_reset_online_cpus(struct trace_array *tr)
615 tr->time_start = ftrace_now(tr->cpu);
617 for_each_online_cpu(cpu)
618 tracing_reset(tr, cpu);
621 #define SAVED_CMDLINES 128
622 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
623 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
624 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
625 static int cmdline_idx;
626 static DEFINE_SPINLOCK(trace_cmdline_lock);
628 /* temporary disable recording */
629 static atomic_t trace_record_cmdline_disabled __read_mostly;
631 static void trace_init_cmdlines(void)
633 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
634 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
638 static int trace_stop_count;
639 static DEFINE_SPINLOCK(tracing_start_lock);
642 * ftrace_off_permanent - disable all ftrace code permanently
644 * This should only be called when a serious anomally has
645 * been detected. This will turn off the function tracing,
646 * ring buffers, and other tracing utilites. It takes no
647 * locks and can be called from any context.
649 void ftrace_off_permanent(void)
651 tracing_disabled = 1;
653 tracing_off_permanent();
657 * tracing_start - quick start of the tracer
659 * If tracing is enabled but was stopped by tracing_stop,
660 * this will start the tracer back up.
662 void tracing_start(void)
664 struct ring_buffer *buffer;
667 if (tracing_disabled)
670 spin_lock_irqsave(&tracing_start_lock, flags);
671 if (--trace_stop_count) {
672 if (trace_stop_count < 0) {
673 /* Someone screwed up their debugging */
675 trace_stop_count = 0;
681 buffer = global_trace.buffer;
683 ring_buffer_record_enable(buffer);
685 buffer = max_tr.buffer;
687 ring_buffer_record_enable(buffer);
691 spin_unlock_irqrestore(&tracing_start_lock, flags);
695 * tracing_stop - quick stop of the tracer
697 * Light weight way to stop tracing. Use in conjunction with
700 void tracing_stop(void)
702 struct ring_buffer *buffer;
706 spin_lock_irqsave(&tracing_start_lock, flags);
707 if (trace_stop_count++)
710 buffer = global_trace.buffer;
712 ring_buffer_record_disable(buffer);
714 buffer = max_tr.buffer;
716 ring_buffer_record_disable(buffer);
719 spin_unlock_irqrestore(&tracing_start_lock, flags);
722 void trace_stop_cmdline_recording(void);
724 static void trace_save_cmdline(struct task_struct *tsk)
729 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
733 * It's not the end of the world if we don't get
734 * the lock, but we also don't want to spin
735 * nor do we want to disable interrupts,
736 * so if we miss here, then better luck next time.
738 if (!spin_trylock(&trace_cmdline_lock))
741 idx = map_pid_to_cmdline[tsk->pid];
742 if (idx >= SAVED_CMDLINES) {
743 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
745 map = map_cmdline_to_pid[idx];
746 if (map <= PID_MAX_DEFAULT)
747 map_pid_to_cmdline[map] = (unsigned)-1;
749 map_pid_to_cmdline[tsk->pid] = idx;
754 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
756 spin_unlock(&trace_cmdline_lock);
759 char *trace_find_cmdline(int pid)
761 char *cmdline = "<...>";
767 if (pid > PID_MAX_DEFAULT)
770 map = map_pid_to_cmdline[pid];
771 if (map >= SAVED_CMDLINES)
774 cmdline = saved_cmdlines[map];
780 void tracing_record_cmdline(struct task_struct *tsk)
782 if (atomic_read(&trace_record_cmdline_disabled))
785 trace_save_cmdline(tsk);
789 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
792 struct task_struct *tsk = current;
794 entry->preempt_count = pc & 0xff;
795 entry->pid = (tsk) ? tsk->pid : 0;
796 entry->tgid = (tsk) ? tsk->tgid : 0;
798 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
799 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
801 TRACE_FLAG_IRQS_NOSUPPORT |
803 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
804 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
805 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
808 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
811 unsigned long flags, int pc)
813 struct ring_buffer_event *event;
815 event = ring_buffer_lock_reserve(tr->buffer, len);
817 struct trace_entry *ent = ring_buffer_event_data(event);
819 tracing_generic_entry_update(ent, flags, pc);
825 static void ftrace_trace_stack(struct trace_array *tr,
826 unsigned long flags, int skip, int pc);
827 static void ftrace_trace_userstack(struct trace_array *tr,
828 unsigned long flags, int pc);
830 void trace_buffer_unlock_commit(struct trace_array *tr,
831 struct ring_buffer_event *event,
832 unsigned long flags, int pc)
834 ring_buffer_unlock_commit(tr->buffer, event);
836 ftrace_trace_stack(tr, flags, 6, pc);
837 ftrace_trace_userstack(tr, flags, pc);
841 struct ring_buffer_event *
842 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
843 unsigned long flags, int pc)
845 return trace_buffer_lock_reserve(&global_trace,
846 type, len, flags, pc);
849 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
850 unsigned long flags, int pc)
852 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
856 trace_function(struct trace_array *tr,
857 unsigned long ip, unsigned long parent_ip, unsigned long flags,
860 struct ring_buffer_event *event;
861 struct ftrace_entry *entry;
863 /* If we are reading the ring buffer, don't trace */
864 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
867 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
871 entry = ring_buffer_event_data(event);
873 entry->parent_ip = parent_ip;
874 ring_buffer_unlock_commit(tr->buffer, event);
877 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
878 static void __trace_graph_entry(struct trace_array *tr,
879 struct ftrace_graph_ent *trace,
883 struct ring_buffer_event *event;
884 struct ftrace_graph_ent_entry *entry;
886 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
889 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
890 sizeof(*entry), flags, pc);
893 entry = ring_buffer_event_data(event);
894 entry->graph_ent = *trace;
895 ring_buffer_unlock_commit(global_trace.buffer, event);
898 static void __trace_graph_return(struct trace_array *tr,
899 struct ftrace_graph_ret *trace,
903 struct ring_buffer_event *event;
904 struct ftrace_graph_ret_entry *entry;
906 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
909 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
910 sizeof(*entry), flags, pc);
913 entry = ring_buffer_event_data(event);
915 ring_buffer_unlock_commit(global_trace.buffer, event);
920 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
921 unsigned long ip, unsigned long parent_ip, unsigned long flags,
924 if (likely(!atomic_read(&data->disabled)))
925 trace_function(tr, ip, parent_ip, flags, pc);
928 static void __ftrace_trace_stack(struct trace_array *tr,
932 #ifdef CONFIG_STACKTRACE
933 struct ring_buffer_event *event;
934 struct stack_entry *entry;
935 struct stack_trace trace;
937 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
938 sizeof(*entry), flags, pc);
941 entry = ring_buffer_event_data(event);
942 memset(&entry->caller, 0, sizeof(entry->caller));
944 trace.nr_entries = 0;
945 trace.max_entries = FTRACE_STACK_ENTRIES;
947 trace.entries = entry->caller;
949 save_stack_trace(&trace);
950 ring_buffer_unlock_commit(tr->buffer, event);
954 static void ftrace_trace_stack(struct trace_array *tr,
958 if (!(trace_flags & TRACE_ITER_STACKTRACE))
961 __ftrace_trace_stack(tr, flags, skip, pc);
964 void __trace_stack(struct trace_array *tr,
968 __ftrace_trace_stack(tr, flags, skip, pc);
971 static void ftrace_trace_userstack(struct trace_array *tr,
972 unsigned long flags, int pc)
974 #ifdef CONFIG_STACKTRACE
975 struct ring_buffer_event *event;
976 struct userstack_entry *entry;
977 struct stack_trace trace;
979 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
982 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
983 sizeof(*entry), flags, pc);
986 entry = ring_buffer_event_data(event);
988 memset(&entry->caller, 0, sizeof(entry->caller));
990 trace.nr_entries = 0;
991 trace.max_entries = FTRACE_STACK_ENTRIES;
993 trace.entries = entry->caller;
995 save_stack_trace_user(&trace);
996 ring_buffer_unlock_commit(tr->buffer, event);
1001 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1003 ftrace_trace_userstack(tr, flags, preempt_count());
1008 ftrace_trace_special(void *__tr,
1009 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1012 struct ring_buffer_event *event;
1013 struct trace_array *tr = __tr;
1014 struct special_entry *entry;
1016 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1017 sizeof(*entry), 0, pc);
1020 entry = ring_buffer_event_data(event);
1024 trace_buffer_unlock_commit(tr, event, 0, pc);
1028 __trace_special(void *__tr, void *__data,
1029 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1031 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1035 tracing_sched_switch_trace(struct trace_array *tr,
1036 struct task_struct *prev,
1037 struct task_struct *next,
1038 unsigned long flags, int pc)
1040 struct ring_buffer_event *event;
1041 struct ctx_switch_entry *entry;
1043 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1044 sizeof(*entry), flags, pc);
1047 entry = ring_buffer_event_data(event);
1048 entry->prev_pid = prev->pid;
1049 entry->prev_prio = prev->prio;
1050 entry->prev_state = prev->state;
1051 entry->next_pid = next->pid;
1052 entry->next_prio = next->prio;
1053 entry->next_state = next->state;
1054 entry->next_cpu = task_cpu(next);
1055 trace_buffer_unlock_commit(tr, event, flags, pc);
1059 tracing_sched_wakeup_trace(struct trace_array *tr,
1060 struct task_struct *wakee,
1061 struct task_struct *curr,
1062 unsigned long flags, int pc)
1064 struct ring_buffer_event *event;
1065 struct ctx_switch_entry *entry;
1067 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1068 sizeof(*entry), flags, pc);
1071 entry = ring_buffer_event_data(event);
1072 entry->prev_pid = curr->pid;
1073 entry->prev_prio = curr->prio;
1074 entry->prev_state = curr->state;
1075 entry->next_pid = wakee->pid;
1076 entry->next_prio = wakee->prio;
1077 entry->next_state = wakee->state;
1078 entry->next_cpu = task_cpu(wakee);
1080 ring_buffer_unlock_commit(tr->buffer, event);
1081 ftrace_trace_stack(tr, flags, 6, pc);
1082 ftrace_trace_userstack(tr, flags, pc);
1086 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1088 struct trace_array *tr = &global_trace;
1089 struct trace_array_cpu *data;
1090 unsigned long flags;
1094 if (tracing_disabled)
1097 pc = preempt_count();
1098 local_irq_save(flags);
1099 cpu = raw_smp_processor_id();
1100 data = tr->data[cpu];
1102 if (likely(atomic_inc_return(&data->disabled) == 1))
1103 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1105 atomic_dec(&data->disabled);
1106 local_irq_restore(flags);
1109 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1110 int trace_graph_entry(struct ftrace_graph_ent *trace)
1112 struct trace_array *tr = &global_trace;
1113 struct trace_array_cpu *data;
1114 unsigned long flags;
1119 if (!ftrace_trace_task(current))
1122 if (!ftrace_graph_addr(trace->func))
1125 local_irq_save(flags);
1126 cpu = raw_smp_processor_id();
1127 data = tr->data[cpu];
1128 disabled = atomic_inc_return(&data->disabled);
1129 if (likely(disabled == 1)) {
1130 pc = preempt_count();
1131 __trace_graph_entry(tr, trace, flags, pc);
1133 /* Only do the atomic if it is not already set */
1134 if (!test_tsk_trace_graph(current))
1135 set_tsk_trace_graph(current);
1136 atomic_dec(&data->disabled);
1137 local_irq_restore(flags);
1142 void trace_graph_return(struct ftrace_graph_ret *trace)
1144 struct trace_array *tr = &global_trace;
1145 struct trace_array_cpu *data;
1146 unsigned long flags;
1151 local_irq_save(flags);
1152 cpu = raw_smp_processor_id();
1153 data = tr->data[cpu];
1154 disabled = atomic_inc_return(&data->disabled);
1155 if (likely(disabled == 1)) {
1156 pc = preempt_count();
1157 __trace_graph_return(tr, trace, flags, pc);
1160 clear_tsk_trace_graph(current);
1161 atomic_dec(&data->disabled);
1162 local_irq_restore(flags);
1164 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1166 enum trace_file_type {
1167 TRACE_FILE_LAT_FMT = 1,
1168 TRACE_FILE_ANNOTATE = 2,
1171 static void trace_iterator_increment(struct trace_iterator *iter)
1173 /* Don't allow ftrace to trace into the ring buffers */
1174 ftrace_disable_cpu();
1177 if (iter->buffer_iter[iter->cpu])
1178 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1180 ftrace_enable_cpu();
1183 static struct trace_entry *
1184 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1186 struct ring_buffer_event *event;
1187 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1189 /* Don't allow ftrace to trace into the ring buffers */
1190 ftrace_disable_cpu();
1193 event = ring_buffer_iter_peek(buf_iter, ts);
1195 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1197 ftrace_enable_cpu();
1199 return event ? ring_buffer_event_data(event) : NULL;
1202 static struct trace_entry *
1203 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1205 struct ring_buffer *buffer = iter->tr->buffer;
1206 struct trace_entry *ent, *next = NULL;
1207 int cpu_file = iter->cpu_file;
1208 u64 next_ts = 0, ts;
1213 * If we are in a per_cpu trace file, don't bother by iterating over
1214 * all cpu and peek directly.
1216 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1217 if (ring_buffer_empty_cpu(buffer, cpu_file))
1219 ent = peek_next_entry(iter, cpu_file, ent_ts);
1221 *ent_cpu = cpu_file;
1226 for_each_tracing_cpu(cpu) {
1228 if (ring_buffer_empty_cpu(buffer, cpu))
1231 ent = peek_next_entry(iter, cpu, &ts);
1234 * Pick the entry with the smallest timestamp:
1236 if (ent && (!next || ts < next_ts)) {
1244 *ent_cpu = next_cpu;
1252 /* Find the next real entry, without updating the iterator itself */
1253 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1254 int *ent_cpu, u64 *ent_ts)
1256 return __find_next_entry(iter, ent_cpu, ent_ts);
1259 /* Find the next real entry, and increment the iterator to the next entry */
1260 static void *find_next_entry_inc(struct trace_iterator *iter)
1262 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1265 trace_iterator_increment(iter);
1267 return iter->ent ? iter : NULL;
1270 static void trace_consume(struct trace_iterator *iter)
1272 /* Don't allow ftrace to trace into the ring buffers */
1273 ftrace_disable_cpu();
1274 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1275 ftrace_enable_cpu();
1278 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1280 struct trace_iterator *iter = m->private;
1286 /* can't go backwards */
1291 ent = find_next_entry_inc(iter);
1295 while (ent && iter->idx < i)
1296 ent = find_next_entry_inc(iter);
1304 * No necessary locking here. The worst thing which can
1305 * happen is loosing events consumed at the same time
1306 * by a trace_pipe reader.
1307 * Other than that, we don't risk to crash the ring buffer
1308 * because it serializes the readers.
1310 * The current tracer is copied to avoid a global locking
1313 static void *s_start(struct seq_file *m, loff_t *pos)
1315 struct trace_iterator *iter = m->private;
1316 static struct tracer *old_tracer;
1317 int cpu_file = iter->cpu_file;
1322 /* copy the tracer to avoid using a global lock all around */
1323 mutex_lock(&trace_types_lock);
1324 if (unlikely(old_tracer != current_trace && current_trace)) {
1325 old_tracer = current_trace;
1326 *iter->trace = *current_trace;
1328 mutex_unlock(&trace_types_lock);
1330 atomic_inc(&trace_record_cmdline_disabled);
1332 if (*pos != iter->pos) {
1337 ftrace_disable_cpu();
1339 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1340 for_each_tracing_cpu(cpu)
1341 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1343 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1346 ftrace_enable_cpu();
1348 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1353 p = s_next(m, p, &l);
1359 static void s_stop(struct seq_file *m, void *p)
1361 atomic_dec(&trace_record_cmdline_disabled);
1364 static void print_lat_help_header(struct seq_file *m)
1366 seq_puts(m, "# _------=> CPU# \n");
1367 seq_puts(m, "# / _-----=> irqs-off \n");
1368 seq_puts(m, "# | / _----=> need-resched \n");
1369 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1370 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1371 seq_puts(m, "# |||| / \n");
1372 seq_puts(m, "# ||||| delay \n");
1373 seq_puts(m, "# cmd pid ||||| time | caller \n");
1374 seq_puts(m, "# \\ / ||||| \\ | / \n");
1377 static void print_func_help_header(struct seq_file *m)
1379 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1380 seq_puts(m, "# | | | | |\n");
1385 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1387 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1388 struct trace_array *tr = iter->tr;
1389 struct trace_array_cpu *data = tr->data[tr->cpu];
1390 struct tracer *type = current_trace;
1391 unsigned long total;
1392 unsigned long entries;
1393 const char *name = "preemption";
1398 entries = ring_buffer_entries(iter->tr->buffer);
1400 ring_buffer_overruns(iter->tr->buffer);
1402 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1404 seq_puts(m, "-----------------------------------"
1405 "---------------------------------\n");
1406 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1407 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1408 nsecs_to_usecs(data->saved_latency),
1412 #if defined(CONFIG_PREEMPT_NONE)
1414 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1416 #elif defined(CONFIG_PREEMPT)
1421 /* These are reserved for later use */
1424 seq_printf(m, " #P:%d)\n", num_online_cpus());
1428 seq_puts(m, " -----------------\n");
1429 seq_printf(m, " | task: %.16s-%d "
1430 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1431 data->comm, data->pid, data->uid, data->nice,
1432 data->policy, data->rt_priority);
1433 seq_puts(m, " -----------------\n");
1435 if (data->critical_start) {
1436 seq_puts(m, " => started at: ");
1437 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1438 trace_print_seq(m, &iter->seq);
1439 seq_puts(m, "\n => ended at: ");
1440 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1441 trace_print_seq(m, &iter->seq);
1448 static void test_cpu_buff_start(struct trace_iterator *iter)
1450 struct trace_seq *s = &iter->seq;
1452 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1455 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1458 if (cpumask_test_cpu(iter->cpu, iter->started))
1461 cpumask_set_cpu(iter->cpu, iter->started);
1462 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1465 static enum print_line_t print_lat_fmt(struct trace_iterator *iter)
1467 struct trace_seq *s = &iter->seq;
1468 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1469 struct trace_event *event;
1470 struct trace_entry *entry = iter->ent;
1472 test_cpu_buff_start(iter);
1474 event = ftrace_find_event(entry->type);
1476 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1477 if (!trace_print_lat_context(iter))
1482 return event->latency_trace(iter, sym_flags);
1484 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1487 return TRACE_TYPE_HANDLED;
1489 return TRACE_TYPE_PARTIAL_LINE;
1492 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1494 struct trace_seq *s = &iter->seq;
1495 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1496 struct trace_entry *entry;
1497 struct trace_event *event;
1501 test_cpu_buff_start(iter);
1503 event = ftrace_find_event(entry->type);
1505 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1506 if (!trace_print_context(iter))
1511 return event->trace(iter, sym_flags);
1513 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1516 return TRACE_TYPE_HANDLED;
1518 return TRACE_TYPE_PARTIAL_LINE;
1521 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1523 struct trace_seq *s = &iter->seq;
1524 struct trace_entry *entry;
1525 struct trace_event *event;
1529 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1530 if (!trace_seq_printf(s, "%d %d %llu ",
1531 entry->pid, iter->cpu, iter->ts))
1535 event = ftrace_find_event(entry->type);
1537 return event->raw(iter, 0);
1539 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1542 return TRACE_TYPE_HANDLED;
1544 return TRACE_TYPE_PARTIAL_LINE;
1547 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1549 struct trace_seq *s = &iter->seq;
1550 unsigned char newline = '\n';
1551 struct trace_entry *entry;
1552 struct trace_event *event;
1556 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1557 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1558 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1559 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1562 event = ftrace_find_event(entry->type);
1564 enum print_line_t ret = event->hex(iter, 0);
1565 if (ret != TRACE_TYPE_HANDLED)
1569 SEQ_PUT_FIELD_RET(s, newline);
1571 return TRACE_TYPE_HANDLED;
1574 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1576 struct trace_seq *s = &iter->seq;
1577 struct trace_entry *entry = iter->ent;
1578 struct print_entry *field;
1581 trace_assign_type(field, entry);
1583 ret = trace_seq_printf(s, "%s", field->buf);
1585 return TRACE_TYPE_PARTIAL_LINE;
1587 return TRACE_TYPE_HANDLED;
1590 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1592 struct trace_seq *s = &iter->seq;
1593 struct trace_entry *entry;
1594 struct trace_event *event;
1598 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1599 SEQ_PUT_FIELD_RET(s, entry->pid);
1600 SEQ_PUT_FIELD_RET(s, iter->cpu);
1601 SEQ_PUT_FIELD_RET(s, iter->ts);
1604 event = ftrace_find_event(entry->type);
1605 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1608 static int trace_empty(struct trace_iterator *iter)
1612 for_each_tracing_cpu(cpu) {
1613 if (iter->buffer_iter[cpu]) {
1614 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1617 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1625 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1627 enum print_line_t ret;
1629 if (iter->trace && iter->trace->print_line) {
1630 ret = iter->trace->print_line(iter);
1631 if (ret != TRACE_TYPE_UNHANDLED)
1635 if (iter->ent->type == TRACE_PRINT &&
1636 trace_flags & TRACE_ITER_PRINTK &&
1637 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1638 return print_printk_msg_only(iter);
1640 if (trace_flags & TRACE_ITER_BIN)
1641 return print_bin_fmt(iter);
1643 if (trace_flags & TRACE_ITER_HEX)
1644 return print_hex_fmt(iter);
1646 if (trace_flags & TRACE_ITER_RAW)
1647 return print_raw_fmt(iter);
1649 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1650 return print_lat_fmt(iter);
1652 return print_trace_fmt(iter);
1655 static int s_show(struct seq_file *m, void *v)
1657 struct trace_iterator *iter = v;
1659 if (iter->ent == NULL) {
1661 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1664 if (iter->trace && iter->trace->print_header)
1665 iter->trace->print_header(m);
1666 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1667 /* print nothing if the buffers are empty */
1668 if (trace_empty(iter))
1670 print_trace_header(m, iter);
1671 if (!(trace_flags & TRACE_ITER_VERBOSE))
1672 print_lat_help_header(m);
1674 if (!(trace_flags & TRACE_ITER_VERBOSE))
1675 print_func_help_header(m);
1678 print_trace_line(iter);
1679 trace_print_seq(m, &iter->seq);
1685 static struct seq_operations tracer_seq_ops = {
1692 static struct trace_iterator *
1693 __tracing_open(struct inode *inode, struct file *file)
1695 long cpu_file = (long) inode->i_private;
1696 void *fail_ret = ERR_PTR(-ENOMEM);
1697 struct trace_iterator *iter;
1701 if (tracing_disabled)
1702 return ERR_PTR(-ENODEV);
1704 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1706 return ERR_PTR(-ENOMEM);
1709 * We make a copy of the current tracer to avoid concurrent
1710 * changes on it while we are reading.
1712 mutex_lock(&trace_types_lock);
1713 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1718 *iter->trace = *current_trace;
1720 if (current_trace && current_trace->print_max)
1723 iter->tr = &global_trace;
1725 mutex_init(&iter->mutex);
1726 iter->cpu_file = cpu_file;
1728 /* Notify the tracer early; before we stop tracing. */
1729 if (iter->trace && iter->trace->open)
1730 iter->trace->open(iter);
1732 /* Annotate start of buffers if we had overruns */
1733 if (ring_buffer_overruns(iter->tr->buffer))
1734 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1736 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1737 for_each_tracing_cpu(cpu) {
1739 iter->buffer_iter[cpu] =
1740 ring_buffer_read_start(iter->tr->buffer, cpu);
1742 if (!iter->buffer_iter[cpu])
1746 cpu = iter->cpu_file;
1747 iter->buffer_iter[cpu] =
1748 ring_buffer_read_start(iter->tr->buffer, cpu);
1750 if (!iter->buffer_iter[cpu])
1754 /* TODO stop tracer */
1755 ret = seq_open(file, &tracer_seq_ops);
1757 fail_ret = ERR_PTR(ret);
1761 m = file->private_data;
1764 /* stop the trace while dumping */
1767 mutex_unlock(&trace_types_lock);
1772 for_each_tracing_cpu(cpu) {
1773 if (iter->buffer_iter[cpu])
1774 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1777 mutex_unlock(&trace_types_lock);
1784 int tracing_open_generic(struct inode *inode, struct file *filp)
1786 if (tracing_disabled)
1789 filp->private_data = inode->i_private;
1793 static int tracing_release(struct inode *inode, struct file *file)
1795 struct seq_file *m = (struct seq_file *)file->private_data;
1796 struct trace_iterator *iter = m->private;
1799 mutex_lock(&trace_types_lock);
1800 for_each_tracing_cpu(cpu) {
1801 if (iter->buffer_iter[cpu])
1802 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1805 if (iter->trace && iter->trace->close)
1806 iter->trace->close(iter);
1808 /* reenable tracing if it was previously enabled */
1810 mutex_unlock(&trace_types_lock);
1812 seq_release(inode, file);
1813 mutex_destroy(&iter->mutex);
1819 static int tracing_open(struct inode *inode, struct file *file)
1821 struct trace_iterator *iter;
1824 iter = __tracing_open(inode, file);
1826 ret = PTR_ERR(iter);
1831 static int tracing_lt_open(struct inode *inode, struct file *file)
1833 struct trace_iterator *iter;
1836 iter = __tracing_open(inode, file);
1839 ret = PTR_ERR(iter);
1841 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1848 t_next(struct seq_file *m, void *v, loff_t *pos)
1850 struct tracer *t = m->private;
1862 static void *t_start(struct seq_file *m, loff_t *pos)
1864 struct tracer *t = m->private;
1867 mutex_lock(&trace_types_lock);
1868 for (; t && l < *pos; t = t_next(m, t, &l))
1874 static void t_stop(struct seq_file *m, void *p)
1876 mutex_unlock(&trace_types_lock);
1879 static int t_show(struct seq_file *m, void *v)
1881 struct tracer *t = v;
1886 seq_printf(m, "%s", t->name);
1895 static struct seq_operations show_traces_seq_ops = {
1902 static int show_traces_open(struct inode *inode, struct file *file)
1906 if (tracing_disabled)
1909 ret = seq_open(file, &show_traces_seq_ops);
1911 struct seq_file *m = file->private_data;
1912 m->private = trace_types;
1918 static struct file_operations tracing_fops = {
1919 .open = tracing_open,
1921 .llseek = seq_lseek,
1922 .release = tracing_release,
1925 static struct file_operations tracing_lt_fops = {
1926 .open = tracing_lt_open,
1928 .llseek = seq_lseek,
1929 .release = tracing_release,
1932 static struct file_operations show_traces_fops = {
1933 .open = show_traces_open,
1935 .release = seq_release,
1939 * Only trace on a CPU if the bitmask is set:
1941 static cpumask_var_t tracing_cpumask;
1944 * The tracer itself will not take this lock, but still we want
1945 * to provide a consistent cpumask to user-space:
1947 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1950 * Temporary storage for the character representation of the
1951 * CPU bitmask (and one more byte for the newline):
1953 static char mask_str[NR_CPUS + 1];
1956 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1957 size_t count, loff_t *ppos)
1961 mutex_lock(&tracing_cpumask_update_lock);
1963 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1964 if (count - len < 2) {
1968 len += sprintf(mask_str + len, "\n");
1969 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1972 mutex_unlock(&tracing_cpumask_update_lock);
1978 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1979 size_t count, loff_t *ppos)
1982 cpumask_var_t tracing_cpumask_new;
1984 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1987 mutex_lock(&tracing_cpumask_update_lock);
1988 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1992 local_irq_disable();
1993 __raw_spin_lock(&ftrace_max_lock);
1994 for_each_tracing_cpu(cpu) {
1996 * Increase/decrease the disabled counter if we are
1997 * about to flip a bit in the cpumask:
1999 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2000 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2001 atomic_inc(&global_trace.data[cpu]->disabled);
2003 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2004 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2005 atomic_dec(&global_trace.data[cpu]->disabled);
2008 __raw_spin_unlock(&ftrace_max_lock);
2011 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2013 mutex_unlock(&tracing_cpumask_update_lock);
2014 free_cpumask_var(tracing_cpumask_new);
2019 mutex_unlock(&tracing_cpumask_update_lock);
2020 free_cpumask_var(tracing_cpumask);
2025 static struct file_operations tracing_cpumask_fops = {
2026 .open = tracing_open_generic,
2027 .read = tracing_cpumask_read,
2028 .write = tracing_cpumask_write,
2032 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2033 size_t cnt, loff_t *ppos)
2035 struct tracer_opt *trace_opts;
2043 /* calculate max size */
2044 for (i = 0; trace_options[i]; i++) {
2045 len += strlen(trace_options[i]);
2046 len += 3; /* "no" and newline */
2049 mutex_lock(&trace_types_lock);
2050 tracer_flags = current_trace->flags->val;
2051 trace_opts = current_trace->flags->opts;
2054 * Increase the size with names of options specific
2055 * of the current tracer.
2057 for (i = 0; trace_opts[i].name; i++) {
2058 len += strlen(trace_opts[i].name);
2059 len += 3; /* "no" and newline */
2062 /* +2 for \n and \0 */
2063 buf = kmalloc(len + 2, GFP_KERNEL);
2065 mutex_unlock(&trace_types_lock);
2069 for (i = 0; trace_options[i]; i++) {
2070 if (trace_flags & (1 << i))
2071 r += sprintf(buf + r, "%s\n", trace_options[i]);
2073 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2076 for (i = 0; trace_opts[i].name; i++) {
2077 if (tracer_flags & trace_opts[i].bit)
2078 r += sprintf(buf + r, "%s\n",
2079 trace_opts[i].name);
2081 r += sprintf(buf + r, "no%s\n",
2082 trace_opts[i].name);
2084 mutex_unlock(&trace_types_lock);
2086 WARN_ON(r >= len + 2);
2088 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2094 /* Try to assign a tracer specific option */
2095 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2097 struct tracer_flags *trace_flags = trace->flags;
2098 struct tracer_opt *opts = NULL;
2102 for (i = 0; trace_flags->opts[i].name; i++) {
2103 opts = &trace_flags->opts[i];
2104 len = strlen(opts->name);
2106 if (strncmp(cmp, opts->name, len) == 0) {
2107 ret = trace->set_flag(trace_flags->val,
2113 if (!trace_flags->opts[i].name)
2116 /* Refused to handle */
2121 trace_flags->val &= ~opts->bit;
2123 trace_flags->val |= opts->bit;
2129 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2130 size_t cnt, loff_t *ppos)
2138 if (cnt >= sizeof(buf))
2141 if (copy_from_user(&buf, ubuf, cnt))
2146 if (strncmp(buf, "no", 2) == 0) {
2151 for (i = 0; trace_options[i]; i++) {
2152 int len = strlen(trace_options[i]);
2154 if (strncmp(cmp, trace_options[i], len) == 0) {
2156 trace_flags &= ~(1 << i);
2158 trace_flags |= (1 << i);
2163 /* If no option could be set, test the specific tracer options */
2164 if (!trace_options[i]) {
2165 mutex_lock(&trace_types_lock);
2166 ret = set_tracer_option(current_trace, cmp, neg);
2167 mutex_unlock(&trace_types_lock);
2177 static struct file_operations tracing_iter_fops = {
2178 .open = tracing_open_generic,
2179 .read = tracing_trace_options_read,
2180 .write = tracing_trace_options_write,
2183 static const char readme_msg[] =
2184 "tracing mini-HOWTO:\n\n"
2186 "# mount -t debugfs nodev /debug\n\n"
2187 "# cat /debug/tracing/available_tracers\n"
2188 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2189 "# cat /debug/tracing/current_tracer\n"
2191 "# echo sched_switch > /debug/tracing/current_tracer\n"
2192 "# cat /debug/tracing/current_tracer\n"
2194 "# cat /debug/tracing/trace_options\n"
2195 "noprint-parent nosym-offset nosym-addr noverbose\n"
2196 "# echo print-parent > /debug/tracing/trace_options\n"
2197 "# echo 1 > /debug/tracing/tracing_enabled\n"
2198 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2199 "echo 0 > /debug/tracing/tracing_enabled\n"
2203 tracing_readme_read(struct file *filp, char __user *ubuf,
2204 size_t cnt, loff_t *ppos)
2206 return simple_read_from_buffer(ubuf, cnt, ppos,
2207 readme_msg, strlen(readme_msg));
2210 static struct file_operations tracing_readme_fops = {
2211 .open = tracing_open_generic,
2212 .read = tracing_readme_read,
2216 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2217 size_t cnt, loff_t *ppos)
2222 r = sprintf(buf, "%u\n", tracer_enabled);
2223 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2227 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2228 size_t cnt, loff_t *ppos)
2230 struct trace_array *tr = filp->private_data;
2235 if (cnt >= sizeof(buf))
2238 if (copy_from_user(&buf, ubuf, cnt))
2243 ret = strict_strtoul(buf, 10, &val);
2249 mutex_lock(&trace_types_lock);
2250 if (tracer_enabled ^ val) {
2253 if (current_trace->start)
2254 current_trace->start(tr);
2259 if (current_trace->stop)
2260 current_trace->stop(tr);
2263 mutex_unlock(&trace_types_lock);
2271 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2272 size_t cnt, loff_t *ppos)
2274 char buf[max_tracer_type_len+2];
2277 mutex_lock(&trace_types_lock);
2279 r = sprintf(buf, "%s\n", current_trace->name);
2281 r = sprintf(buf, "\n");
2282 mutex_unlock(&trace_types_lock);
2284 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2287 int tracer_init(struct tracer *t, struct trace_array *tr)
2289 tracing_reset_online_cpus(tr);
2293 struct trace_option_dentry;
2295 static struct trace_option_dentry *
2296 create_trace_option_files(struct tracer *tracer);
2299 destroy_trace_option_files(struct trace_option_dentry *topts);
2301 static int tracing_set_tracer(const char *buf)
2303 static struct trace_option_dentry *topts;
2304 struct trace_array *tr = &global_trace;
2308 mutex_lock(&trace_types_lock);
2309 for (t = trace_types; t; t = t->next) {
2310 if (strcmp(t->name, buf) == 0)
2317 if (t == current_trace)
2320 trace_branch_disable();
2321 if (current_trace && current_trace->reset)
2322 current_trace->reset(tr);
2324 destroy_trace_option_files(topts);
2328 topts = create_trace_option_files(current_trace);
2331 ret = tracer_init(t, tr);
2336 trace_branch_enable(tr);
2338 mutex_unlock(&trace_types_lock);
2344 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2345 size_t cnt, loff_t *ppos)
2347 char buf[max_tracer_type_len+1];
2354 if (cnt > max_tracer_type_len)
2355 cnt = max_tracer_type_len;
2357 if (copy_from_user(&buf, ubuf, cnt))
2362 /* strip ending whitespace. */
2363 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2366 err = tracing_set_tracer(buf);
2376 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2377 size_t cnt, loff_t *ppos)
2379 unsigned long *ptr = filp->private_data;
2383 r = snprintf(buf, sizeof(buf), "%ld\n",
2384 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2385 if (r > sizeof(buf))
2387 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2391 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2392 size_t cnt, loff_t *ppos)
2394 unsigned long *ptr = filp->private_data;
2399 if (cnt >= sizeof(buf))
2402 if (copy_from_user(&buf, ubuf, cnt))
2407 ret = strict_strtoul(buf, 10, &val);
2416 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2418 long cpu_file = (long) inode->i_private;
2419 struct trace_iterator *iter;
2422 if (tracing_disabled)
2425 mutex_lock(&trace_types_lock);
2427 /* We only allow one reader per cpu */
2428 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2429 if (!cpumask_empty(tracing_reader_cpumask)) {
2433 cpumask_setall(tracing_reader_cpumask);
2435 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2436 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2443 /* create a buffer to store the information to pass to userspace */
2444 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2451 * We make a copy of the current tracer to avoid concurrent
2452 * changes on it while we are reading.
2454 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2460 *iter->trace = *current_trace;
2462 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2467 /* trace pipe does not show start of buffer */
2468 cpumask_setall(iter->started);
2470 iter->cpu_file = cpu_file;
2471 iter->tr = &global_trace;
2472 mutex_init(&iter->mutex);
2473 filp->private_data = iter;
2475 if (iter->trace->pipe_open)
2476 iter->trace->pipe_open(iter);
2479 mutex_unlock(&trace_types_lock);
2485 mutex_unlock(&trace_types_lock);
2489 static int tracing_release_pipe(struct inode *inode, struct file *file)
2491 struct trace_iterator *iter = file->private_data;
2493 mutex_lock(&trace_types_lock);
2495 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2496 cpumask_clear(tracing_reader_cpumask);
2498 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2500 mutex_unlock(&trace_types_lock);
2502 free_cpumask_var(iter->started);
2503 mutex_destroy(&iter->mutex);
2511 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2513 struct trace_iterator *iter = filp->private_data;
2515 if (trace_flags & TRACE_ITER_BLOCK) {
2517 * Always select as readable when in blocking mode
2519 return POLLIN | POLLRDNORM;
2521 if (!trace_empty(iter))
2522 return POLLIN | POLLRDNORM;
2523 poll_wait(filp, &trace_wait, poll_table);
2524 if (!trace_empty(iter))
2525 return POLLIN | POLLRDNORM;
2532 void default_wait_pipe(struct trace_iterator *iter)
2536 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2538 if (trace_empty(iter))
2541 finish_wait(&trace_wait, &wait);
2545 * This is a make-shift waitqueue.
2546 * A tracer might use this callback on some rare cases:
2548 * 1) the current tracer might hold the runqueue lock when it wakes up
2549 * a reader, hence a deadlock (sched, function, and function graph tracers)
2550 * 2) the function tracers, trace all functions, we don't want
2551 * the overhead of calling wake_up and friends
2552 * (and tracing them too)
2554 * Anyway, this is really very primitive wakeup.
2556 void poll_wait_pipe(struct trace_iterator *iter)
2558 set_current_state(TASK_INTERRUPTIBLE);
2559 /* sleep for 100 msecs, and try again. */
2560 schedule_timeout(HZ / 10);
2563 /* Must be called with trace_types_lock mutex held. */
2564 static int tracing_wait_pipe(struct file *filp)
2566 struct trace_iterator *iter = filp->private_data;
2568 while (trace_empty(iter)) {
2570 if ((filp->f_flags & O_NONBLOCK)) {
2574 mutex_unlock(&iter->mutex);
2576 iter->trace->wait_pipe(iter);
2578 mutex_lock(&iter->mutex);
2580 if (signal_pending(current))
2584 * We block until we read something and tracing is disabled.
2585 * We still block if tracing is disabled, but we have never
2586 * read anything. This allows a user to cat this file, and
2587 * then enable tracing. But after we have read something,
2588 * we give an EOF when tracing is again disabled.
2590 * iter->pos will be 0 if we haven't read anything.
2592 if (!tracer_enabled && iter->pos)
2603 tracing_read_pipe(struct file *filp, char __user *ubuf,
2604 size_t cnt, loff_t *ppos)
2606 struct trace_iterator *iter = filp->private_data;
2607 static struct tracer *old_tracer;
2610 /* return any leftover data */
2611 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2615 trace_seq_init(&iter->seq);
2617 /* copy the tracer to avoid using a global lock all around */
2618 mutex_lock(&trace_types_lock);
2619 if (unlikely(old_tracer != current_trace && current_trace)) {
2620 old_tracer = current_trace;
2621 *iter->trace = *current_trace;
2623 mutex_unlock(&trace_types_lock);
2626 * Avoid more than one consumer on a single file descriptor
2627 * This is just a matter of traces coherency, the ring buffer itself
2630 mutex_lock(&iter->mutex);
2631 if (iter->trace->read) {
2632 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2638 sret = tracing_wait_pipe(filp);
2642 /* stop when tracing is finished */
2643 if (trace_empty(iter)) {
2648 if (cnt >= PAGE_SIZE)
2649 cnt = PAGE_SIZE - 1;
2651 /* reset all but tr, trace, and overruns */
2652 memset(&iter->seq, 0,
2653 sizeof(struct trace_iterator) -
2654 offsetof(struct trace_iterator, seq));
2657 while (find_next_entry_inc(iter) != NULL) {
2658 enum print_line_t ret;
2659 int len = iter->seq.len;
2661 ret = print_trace_line(iter);
2662 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2663 /* don't print partial lines */
2664 iter->seq.len = len;
2667 if (ret != TRACE_TYPE_NO_CONSUME)
2668 trace_consume(iter);
2670 if (iter->seq.len >= cnt)
2674 /* Now copy what we have to the user */
2675 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2676 if (iter->seq.readpos >= iter->seq.len)
2677 trace_seq_init(&iter->seq);
2680 * If there was nothing to send to user, inspite of consuming trace
2681 * entries, go back to wait for more entries.
2687 mutex_unlock(&iter->mutex);
2692 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2693 struct pipe_buffer *buf)
2695 __free_page(buf->page);
2698 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2701 __free_page(spd->pages[idx]);
2704 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2706 .map = generic_pipe_buf_map,
2707 .unmap = generic_pipe_buf_unmap,
2708 .confirm = generic_pipe_buf_confirm,
2709 .release = tracing_pipe_buf_release,
2710 .steal = generic_pipe_buf_steal,
2711 .get = generic_pipe_buf_get,
2715 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2720 /* Seq buffer is page-sized, exactly what we need. */
2722 count = iter->seq.len;
2723 ret = print_trace_line(iter);
2724 count = iter->seq.len - count;
2727 iter->seq.len -= count;
2730 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2731 iter->seq.len -= count;
2735 trace_consume(iter);
2737 if (!find_next_entry_inc(iter)) {
2747 static ssize_t tracing_splice_read_pipe(struct file *filp,
2749 struct pipe_inode_info *pipe,
2753 struct page *pages[PIPE_BUFFERS];
2754 struct partial_page partial[PIPE_BUFFERS];
2755 struct trace_iterator *iter = filp->private_data;
2756 struct splice_pipe_desc spd = {
2759 .nr_pages = 0, /* This gets updated below. */
2761 .ops = &tracing_pipe_buf_ops,
2762 .spd_release = tracing_spd_release_pipe,
2764 static struct tracer *old_tracer;
2769 /* copy the tracer to avoid using a global lock all around */
2770 mutex_lock(&trace_types_lock);
2771 if (unlikely(old_tracer != current_trace && current_trace)) {
2772 old_tracer = current_trace;
2773 *iter->trace = *current_trace;
2775 mutex_unlock(&trace_types_lock);
2777 mutex_lock(&iter->mutex);
2779 if (iter->trace->splice_read) {
2780 ret = iter->trace->splice_read(iter, filp,
2781 ppos, pipe, len, flags);
2786 ret = tracing_wait_pipe(filp);
2790 if (!iter->ent && !find_next_entry_inc(iter)) {
2795 /* Fill as many pages as possible. */
2796 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2797 pages[i] = alloc_page(GFP_KERNEL);
2801 rem = tracing_fill_pipe_page(rem, iter);
2803 /* Copy the data into the page, so we can start over. */
2804 ret = trace_seq_to_buffer(&iter->seq,
2805 page_address(pages[i]),
2808 __free_page(pages[i]);
2811 partial[i].offset = 0;
2812 partial[i].len = iter->seq.len;
2814 trace_seq_init(&iter->seq);
2817 mutex_unlock(&iter->mutex);
2821 return splice_to_pipe(pipe, &spd);
2824 mutex_unlock(&iter->mutex);
2830 tracing_entries_read(struct file *filp, char __user *ubuf,
2831 size_t cnt, loff_t *ppos)
2833 struct trace_array *tr = filp->private_data;
2837 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2838 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2842 tracing_entries_write(struct file *filp, const char __user *ubuf,
2843 size_t cnt, loff_t *ppos)
2849 if (cnt >= sizeof(buf))
2852 if (copy_from_user(&buf, ubuf, cnt))
2857 ret = strict_strtoul(buf, 10, &val);
2861 /* must have at least 1 entry */
2865 mutex_lock(&trace_types_lock);
2869 /* disable all cpu buffers */
2870 for_each_tracing_cpu(cpu) {
2871 if (global_trace.data[cpu])
2872 atomic_inc(&global_trace.data[cpu]->disabled);
2873 if (max_tr.data[cpu])
2874 atomic_inc(&max_tr.data[cpu]->disabled);
2877 /* value is in KB */
2880 if (val != global_trace.entries) {
2881 ret = ring_buffer_resize(global_trace.buffer, val);
2887 ret = ring_buffer_resize(max_tr.buffer, val);
2891 r = ring_buffer_resize(global_trace.buffer,
2892 global_trace.entries);
2894 /* AARGH! We are left with different
2895 * size max buffer!!!! */
2897 tracing_disabled = 1;
2902 global_trace.entries = val;
2907 /* If check pages failed, return ENOMEM */
2908 if (tracing_disabled)
2911 for_each_tracing_cpu(cpu) {
2912 if (global_trace.data[cpu])
2913 atomic_dec(&global_trace.data[cpu]->disabled);
2914 if (max_tr.data[cpu])
2915 atomic_dec(&max_tr.data[cpu]->disabled);
2919 max_tr.entries = global_trace.entries;
2920 mutex_unlock(&trace_types_lock);
2925 static int mark_printk(const char *fmt, ...)
2929 va_start(args, fmt);
2930 ret = trace_vprintk(0, -1, fmt, args);
2936 tracing_mark_write(struct file *filp, const char __user *ubuf,
2937 size_t cnt, loff_t *fpos)
2942 if (tracing_disabled)
2945 if (cnt > TRACE_BUF_SIZE)
2946 cnt = TRACE_BUF_SIZE;
2948 buf = kmalloc(cnt + 1, GFP_KERNEL);
2952 if (copy_from_user(buf, ubuf, cnt)) {
2957 /* Cut from the first nil or newline. */
2959 end = strchr(buf, '\n');
2963 cnt = mark_printk("%s\n", buf);
2970 static struct file_operations tracing_max_lat_fops = {
2971 .open = tracing_open_generic,
2972 .read = tracing_max_lat_read,
2973 .write = tracing_max_lat_write,
2976 static struct file_operations tracing_ctrl_fops = {
2977 .open = tracing_open_generic,
2978 .read = tracing_ctrl_read,
2979 .write = tracing_ctrl_write,
2982 static struct file_operations set_tracer_fops = {
2983 .open = tracing_open_generic,
2984 .read = tracing_set_trace_read,
2985 .write = tracing_set_trace_write,
2988 static struct file_operations tracing_pipe_fops = {
2989 .open = tracing_open_pipe,
2990 .poll = tracing_poll_pipe,
2991 .read = tracing_read_pipe,
2992 .splice_read = tracing_splice_read_pipe,
2993 .release = tracing_release_pipe,
2996 static struct file_operations tracing_entries_fops = {
2997 .open = tracing_open_generic,
2998 .read = tracing_entries_read,
2999 .write = tracing_entries_write,
3002 static struct file_operations tracing_mark_fops = {
3003 .open = tracing_open_generic,
3004 .write = tracing_mark_write,
3007 struct ftrace_buffer_info {
3008 struct trace_array *tr;
3014 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3016 int cpu = (int)(long)inode->i_private;
3017 struct ftrace_buffer_info *info;
3019 if (tracing_disabled)
3022 info = kzalloc(sizeof(*info), GFP_KERNEL);
3026 info->tr = &global_trace;
3028 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3029 /* Force reading ring buffer for first read */
3030 info->read = (unsigned int)-1;
3034 filp->private_data = info;
3044 tracing_buffers_read(struct file *filp, char __user *ubuf,
3045 size_t count, loff_t *ppos)
3047 struct ftrace_buffer_info *info = filp->private_data;
3052 /* Do we have previous read data to read? */
3053 if (info->read < PAGE_SIZE)
3058 ret = ring_buffer_read_page(info->tr->buffer,
3065 pos = ring_buffer_page_len(info->spare);
3067 if (pos < PAGE_SIZE)
3068 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3071 size = PAGE_SIZE - info->read;
3075 ret = copy_to_user(ubuf, info->spare + info->read, size);
3084 static int tracing_buffers_release(struct inode *inode, struct file *file)
3086 struct ftrace_buffer_info *info = file->private_data;
3088 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3095 struct ring_buffer *buffer;
3100 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3101 struct pipe_buffer *buf)
3103 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3108 ring_buffer_free_read_page(ref->buffer, ref->page);
3113 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3114 struct pipe_buffer *buf)
3119 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3120 struct pipe_buffer *buf)
3122 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3127 /* Pipe buffer operations for a buffer. */
3128 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3130 .map = generic_pipe_buf_map,
3131 .unmap = generic_pipe_buf_unmap,
3132 .confirm = generic_pipe_buf_confirm,
3133 .release = buffer_pipe_buf_release,
3134 .steal = buffer_pipe_buf_steal,
3135 .get = buffer_pipe_buf_get,
3139 * Callback from splice_to_pipe(), if we need to release some pages
3140 * at the end of the spd in case we error'ed out in filling the pipe.
3142 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3144 struct buffer_ref *ref =
3145 (struct buffer_ref *)spd->partial[i].private;
3150 ring_buffer_free_read_page(ref->buffer, ref->page);
3152 spd->partial[i].private = 0;
3156 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3157 struct pipe_inode_info *pipe, size_t len,
3160 struct ftrace_buffer_info *info = file->private_data;
3161 struct partial_page partial[PIPE_BUFFERS];
3162 struct page *pages[PIPE_BUFFERS];
3163 struct splice_pipe_desc spd = {
3167 .ops = &buffer_pipe_buf_ops,
3168 .spd_release = buffer_spd_release,
3170 struct buffer_ref *ref;
3175 * We can't seek on a buffer input
3177 if (unlikely(*ppos))
3181 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3185 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3189 ref->buffer = info->tr->buffer;
3190 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3196 r = ring_buffer_read_page(ref->buffer, &ref->page,
3199 ring_buffer_free_read_page(ref->buffer,
3206 * zero out any left over data, this is going to
3209 size = ring_buffer_page_len(ref->page);
3210 if (size < PAGE_SIZE)
3211 memset(ref->page + size, 0, PAGE_SIZE - size);
3213 page = virt_to_page(ref->page);
3215 spd.pages[i] = page;
3216 spd.partial[i].len = PAGE_SIZE;
3217 spd.partial[i].offset = 0;
3218 spd.partial[i].private = (unsigned long)ref;
3224 /* did we read anything? */
3225 if (!spd.nr_pages) {
3226 if (flags & SPLICE_F_NONBLOCK)
3234 ret = splice_to_pipe(pipe, &spd);
3239 static const struct file_operations tracing_buffers_fops = {
3240 .open = tracing_buffers_open,
3241 .read = tracing_buffers_read,
3242 .release = tracing_buffers_release,
3243 .splice_read = tracing_buffers_splice_read,
3244 .llseek = no_llseek,
3247 #ifdef CONFIG_DYNAMIC_FTRACE
3249 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3255 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3256 size_t cnt, loff_t *ppos)
3258 static char ftrace_dyn_info_buffer[1024];
3259 static DEFINE_MUTEX(dyn_info_mutex);
3260 unsigned long *p = filp->private_data;
3261 char *buf = ftrace_dyn_info_buffer;
3262 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3265 mutex_lock(&dyn_info_mutex);
3266 r = sprintf(buf, "%ld ", *p);
3268 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3271 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3273 mutex_unlock(&dyn_info_mutex);
3278 static struct file_operations tracing_dyn_info_fops = {
3279 .open = tracing_open_generic,
3280 .read = tracing_read_dyn_info,
3284 static struct dentry *d_tracer;
3286 struct dentry *tracing_init_dentry(void)
3293 d_tracer = debugfs_create_dir("tracing", NULL);
3295 if (!d_tracer && !once) {
3297 pr_warning("Could not create debugfs directory 'tracing'\n");
3304 static struct dentry *d_percpu;
3306 struct dentry *tracing_dentry_percpu(void)
3309 struct dentry *d_tracer;
3314 d_tracer = tracing_init_dentry();
3319 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3321 if (!d_percpu && !once) {
3323 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3330 static void tracing_init_debugfs_percpu(long cpu)
3332 struct dentry *d_percpu = tracing_dentry_percpu();
3333 struct dentry *entry, *d_cpu;
3334 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3337 if (cpu > 999 || cpu < 0)
3340 sprintf(cpu_dir, "cpu%ld", cpu);
3341 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3343 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3347 /* per cpu trace_pipe */
3348 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
3349 (void *) cpu, &tracing_pipe_fops);
3351 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3354 entry = debugfs_create_file("trace", 0444, d_cpu,
3355 (void *) cpu, &tracing_fops);
3357 pr_warning("Could not create debugfs 'trace' entry\n");
3360 #ifdef CONFIG_FTRACE_SELFTEST
3361 /* Let selftest have access to static functions in this file */
3362 #include "trace_selftest.c"
3365 struct trace_option_dentry {
3366 struct tracer_opt *opt;
3367 struct tracer_flags *flags;
3368 struct dentry *entry;
3372 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3375 struct trace_option_dentry *topt = filp->private_data;
3378 if (topt->flags->val & topt->opt->bit)
3383 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3387 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3390 struct trace_option_dentry *topt = filp->private_data;
3395 if (cnt >= sizeof(buf))
3398 if (copy_from_user(&buf, ubuf, cnt))
3403 ret = strict_strtoul(buf, 10, &val);
3410 /* do nothing if already cleared */
3411 if (!(topt->flags->val & topt->opt->bit))
3414 mutex_lock(&trace_types_lock);
3415 if (current_trace->set_flag)
3416 ret = current_trace->set_flag(topt->flags->val,
3418 mutex_unlock(&trace_types_lock);
3421 topt->flags->val &= ~topt->opt->bit;
3424 /* do nothing if already set */
3425 if (topt->flags->val & topt->opt->bit)
3428 mutex_lock(&trace_types_lock);
3429 if (current_trace->set_flag)
3430 ret = current_trace->set_flag(topt->flags->val,
3432 mutex_unlock(&trace_types_lock);
3435 topt->flags->val |= topt->opt->bit;
3448 static const struct file_operations trace_options_fops = {
3449 .open = tracing_open_generic,
3450 .read = trace_options_read,
3451 .write = trace_options_write,
3455 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3458 long index = (long)filp->private_data;
3461 if (trace_flags & (1 << index))
3466 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3470 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3473 long index = (long)filp->private_data;
3478 if (cnt >= sizeof(buf))
3481 if (copy_from_user(&buf, ubuf, cnt))
3486 ret = strict_strtoul(buf, 10, &val);
3492 trace_flags &= ~(1 << index);
3495 trace_flags |= 1 << index;
3507 static const struct file_operations trace_options_core_fops = {
3508 .open = tracing_open_generic,
3509 .read = trace_options_core_read,
3510 .write = trace_options_core_write,
3513 static struct dentry *trace_options_init_dentry(void)
3515 struct dentry *d_tracer;
3516 static struct dentry *t_options;
3521 d_tracer = tracing_init_dentry();
3525 t_options = debugfs_create_dir("options", d_tracer);
3527 pr_warning("Could not create debugfs directory 'options'\n");
3535 create_trace_option_file(struct trace_option_dentry *topt,
3536 struct tracer_flags *flags,
3537 struct tracer_opt *opt)
3539 struct dentry *t_options;
3540 struct dentry *entry;
3542 t_options = trace_options_init_dentry();
3546 topt->flags = flags;
3549 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3550 &trace_options_fops);
3552 topt->entry = entry;
3556 static struct trace_option_dentry *
3557 create_trace_option_files(struct tracer *tracer)
3559 struct trace_option_dentry *topts;
3560 struct tracer_flags *flags;
3561 struct tracer_opt *opts;
3567 flags = tracer->flags;
3569 if (!flags || !flags->opts)
3574 for (cnt = 0; opts[cnt].name; cnt++)
3577 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3581 for (cnt = 0; opts[cnt].name; cnt++)
3582 create_trace_option_file(&topts[cnt], flags,
3589 destroy_trace_option_files(struct trace_option_dentry *topts)
3596 for (cnt = 0; topts[cnt].opt; cnt++) {
3597 if (topts[cnt].entry)
3598 debugfs_remove(topts[cnt].entry);
3604 static struct dentry *
3605 create_trace_option_core_file(const char *option, long index)
3607 struct dentry *t_options;
3608 struct dentry *entry;
3610 t_options = trace_options_init_dentry();
3614 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3615 &trace_options_core_fops);
3620 static __init void create_trace_options_dir(void)
3622 struct dentry *t_options;
3623 struct dentry *entry;
3626 t_options = trace_options_init_dentry();
3630 for (i = 0; trace_options[i]; i++) {
3631 entry = create_trace_option_core_file(trace_options[i], i);
3633 pr_warning("Could not create debugfs %s entry\n",
3638 static __init int tracer_init_debugfs(void)
3640 struct dentry *d_tracer;
3641 struct dentry *buffers;
3642 struct dentry *entry;
3645 d_tracer = tracing_init_dentry();
3647 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3648 &global_trace, &tracing_ctrl_fops);
3650 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3652 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3653 NULL, &tracing_iter_fops);
3655 pr_warning("Could not create debugfs 'trace_options' entry\n");
3657 create_trace_options_dir();
3659 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3660 NULL, &tracing_cpumask_fops);
3662 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3664 entry = debugfs_create_file("trace", 0444, d_tracer,
3665 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3667 pr_warning("Could not create debugfs 'trace' entry\n");
3669 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3670 &global_trace, &show_traces_fops);
3672 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3674 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3675 &global_trace, &set_tracer_fops);
3677 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3679 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3680 &tracing_max_latency,
3681 &tracing_max_lat_fops);
3683 pr_warning("Could not create debugfs "
3684 "'tracing_max_latency' entry\n");
3686 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3687 &tracing_thresh, &tracing_max_lat_fops);
3689 pr_warning("Could not create debugfs "
3690 "'tracing_thresh' entry\n");
3691 entry = debugfs_create_file("README", 0644, d_tracer,
3692 NULL, &tracing_readme_fops);
3694 pr_warning("Could not create debugfs 'README' entry\n");
3696 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3697 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3699 pr_warning("Could not create debugfs "
3700 "'trace_pipe' entry\n");
3702 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3703 &global_trace, &tracing_entries_fops);
3705 pr_warning("Could not create debugfs "
3706 "'buffer_size_kb' entry\n");
3708 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3709 NULL, &tracing_mark_fops);
3711 pr_warning("Could not create debugfs "
3712 "'trace_marker' entry\n");
3714 buffers = debugfs_create_dir("binary_buffers", d_tracer);
3717 pr_warning("Could not create buffers directory\n");
3722 for_each_tracing_cpu(cpu) {
3723 sprintf(buf, "%d", cpu);
3725 entry = debugfs_create_file(buf, 0444, buffers,
3727 &tracing_buffers_fops);
3729 pr_warning("Could not create debugfs buffers "
3730 "'%s' entry\n", buf);
3734 #ifdef CONFIG_DYNAMIC_FTRACE
3735 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3736 &ftrace_update_tot_cnt,
3737 &tracing_dyn_info_fops);
3739 pr_warning("Could not create debugfs "
3740 "'dyn_ftrace_total_info' entry\n");
3742 #ifdef CONFIG_SYSPROF_TRACER
3743 init_tracer_sysprof_debugfs(d_tracer);
3746 for_each_tracing_cpu(cpu)
3747 tracing_init_debugfs_percpu(cpu);
3752 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
3754 static DEFINE_SPINLOCK(trace_buf_lock);
3755 static char trace_buf[TRACE_BUF_SIZE];
3757 struct ring_buffer_event *event;
3758 struct trace_array *tr = &global_trace;
3759 struct trace_array_cpu *data;
3760 int cpu, len = 0, size, pc;
3761 struct print_entry *entry;
3762 unsigned long irq_flags;
3764 if (tracing_disabled || tracing_selftest_running)
3767 pc = preempt_count();
3768 preempt_disable_notrace();
3769 cpu = raw_smp_processor_id();
3770 data = tr->data[cpu];
3772 if (unlikely(atomic_read(&data->disabled)))
3775 pause_graph_tracing();
3776 spin_lock_irqsave(&trace_buf_lock, irq_flags);
3777 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3779 len = min(len, TRACE_BUF_SIZE-1);
3782 size = sizeof(*entry) + len + 1;
3783 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
3786 entry = ring_buffer_event_data(event);
3788 entry->depth = depth;
3790 memcpy(&entry->buf, trace_buf, len);
3791 entry->buf[len] = 0;
3792 ring_buffer_unlock_commit(tr->buffer, event);
3795 spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
3796 unpause_graph_tracing();
3798 preempt_enable_notrace();
3802 EXPORT_SYMBOL_GPL(trace_vprintk);
3804 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3809 if (!(trace_flags & TRACE_ITER_PRINTK))
3813 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3817 EXPORT_SYMBOL_GPL(__ftrace_printk);
3819 int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
3821 if (!(trace_flags & TRACE_ITER_PRINTK))
3824 return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3826 EXPORT_SYMBOL_GPL(__ftrace_vprintk);
3828 static int trace_panic_handler(struct notifier_block *this,
3829 unsigned long event, void *unused)
3831 if (ftrace_dump_on_oops)
3836 static struct notifier_block trace_panic_notifier = {
3837 .notifier_call = trace_panic_handler,
3839 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3842 static int trace_die_handler(struct notifier_block *self,
3848 if (ftrace_dump_on_oops)
3857 static struct notifier_block trace_die_notifier = {
3858 .notifier_call = trace_die_handler,
3863 * printk is set to max of 1024, we really don't need it that big.
3864 * Nothing should be printing 1000 characters anyway.
3866 #define TRACE_MAX_PRINT 1000
3869 * Define here KERN_TRACE so that we have one place to modify
3870 * it if we decide to change what log level the ftrace dump
3873 #define KERN_TRACE KERN_EMERG
3876 trace_printk_seq(struct trace_seq *s)
3878 /* Probably should print a warning here. */
3882 /* should be zero ended, but we are paranoid. */
3883 s->buffer[s->len] = 0;
3885 printk(KERN_TRACE "%s", s->buffer);
3890 void ftrace_dump(void)
3892 static DEFINE_SPINLOCK(ftrace_dump_lock);
3893 /* use static because iter can be a bit big for the stack */
3894 static struct trace_iterator iter;
3895 static int dump_ran;
3896 unsigned long flags;
3900 spin_lock_irqsave(&ftrace_dump_lock, flags);
3906 /* No turning back! */
3910 for_each_tracing_cpu(cpu) {
3911 atomic_inc(&global_trace.data[cpu]->disabled);
3914 /* don't look at user memory in panic mode */
3915 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3917 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3919 iter.tr = &global_trace;
3920 iter.trace = current_trace;
3923 * We need to stop all tracing on all CPUS to read the
3924 * the next buffer. This is a bit expensive, but is
3925 * not done often. We fill all what we can read,
3926 * and then release the locks again.
3929 while (!trace_empty(&iter)) {
3932 printk(KERN_TRACE "---------------------------------\n");
3936 /* reset all but tr, trace, and overruns */
3937 memset(&iter.seq, 0,
3938 sizeof(struct trace_iterator) -
3939 offsetof(struct trace_iterator, seq));
3940 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3943 if (find_next_entry_inc(&iter) != NULL) {
3944 print_trace_line(&iter);
3945 trace_consume(&iter);
3948 trace_printk_seq(&iter.seq);
3952 printk(KERN_TRACE " (ftrace buffer empty)\n");
3954 printk(KERN_TRACE "---------------------------------\n");
3957 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3960 __init static int tracer_alloc_buffers(void)
3962 struct trace_array_cpu *data;
3966 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3969 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3970 goto out_free_buffer_mask;
3972 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
3973 goto out_free_tracing_cpumask;
3975 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3976 cpumask_copy(tracing_cpumask, cpu_all_mask);
3977 cpumask_clear(tracing_reader_cpumask);
3979 /* TODO: make the number of buffers hot pluggable with CPUS */
3980 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3981 TRACE_BUFFER_FLAGS);
3982 if (!global_trace.buffer) {
3983 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3985 goto out_free_cpumask;
3987 global_trace.entries = ring_buffer_size(global_trace.buffer);
3990 #ifdef CONFIG_TRACER_MAX_TRACE
3991 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3992 TRACE_BUFFER_FLAGS);
3993 if (!max_tr.buffer) {
3994 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3996 ring_buffer_free(global_trace.buffer);
3997 goto out_free_cpumask;
3999 max_tr.entries = ring_buffer_size(max_tr.buffer);
4000 WARN_ON(max_tr.entries != global_trace.entries);
4003 /* Allocate the first page for all buffers */
4004 for_each_tracing_cpu(i) {
4005 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4006 max_tr.data[i] = &per_cpu(max_data, i);
4009 trace_init_cmdlines();
4011 register_tracer(&nop_trace);
4012 current_trace = &nop_trace;
4013 #ifdef CONFIG_BOOT_TRACER
4014 register_tracer(&boot_tracer);
4016 /* All seems OK, enable tracing */
4017 tracing_disabled = 0;
4019 atomic_notifier_chain_register(&panic_notifier_list,
4020 &trace_panic_notifier);
4022 register_die_notifier(&trace_die_notifier);
4026 free_cpumask_var(tracing_reader_cpumask);
4027 out_free_tracing_cpumask:
4028 free_cpumask_var(tracing_cpumask);
4029 out_free_buffer_mask:
4030 free_cpumask_var(tracing_buffer_mask);
4035 __init static int clear_boot_tracer(void)
4038 * The default tracer at boot buffer is an init section.
4039 * This function is called in lateinit. If we did not
4040 * find the boot tracer, then clear it out, to prevent
4041 * later registration from accessing the buffer that is
4042 * about to be freed.
4044 if (!default_bootup_tracer)
4047 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4048 default_bootup_tracer);
4049 default_bootup_tracer = NULL;
4054 early_initcall(tracer_alloc_buffers);
4055 fs_initcall(tracer_init_debugfs);
4056 late_initcall(clear_boot_tracer);