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)
352 if (s->len <= s->readpos)
355 len = s->len - s->readpos;
358 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
368 ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
373 if (s->len <= s->readpos)
376 len = s->len - s->readpos;
379 ret = memcpy(buf, s->buffer + s->readpos, cnt);
388 trace_print_seq(struct seq_file *m, struct trace_seq *s)
390 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
393 seq_puts(m, s->buffer);
399 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
401 * @tsk: the task with the latency
402 * @cpu: The cpu that initiated the trace.
404 * Flip the buffers between the @tr and the max_tr and record information
405 * about which task was the cause of this latency.
408 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
410 struct ring_buffer *buf = tr->buffer;
412 WARN_ON_ONCE(!irqs_disabled());
413 __raw_spin_lock(&ftrace_max_lock);
415 tr->buffer = max_tr.buffer;
418 ftrace_disable_cpu();
419 ring_buffer_reset(tr->buffer);
422 __update_max_tr(tr, tsk, cpu);
423 __raw_spin_unlock(&ftrace_max_lock);
427 * update_max_tr_single - only copy one trace over, and reset the rest
429 * @tsk - task with the latency
430 * @cpu - the cpu of the buffer to copy.
432 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
435 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
439 WARN_ON_ONCE(!irqs_disabled());
440 __raw_spin_lock(&ftrace_max_lock);
442 ftrace_disable_cpu();
444 ring_buffer_reset(max_tr.buffer);
445 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
449 WARN_ON_ONCE(ret && ret != -EAGAIN);
451 __update_max_tr(tr, tsk, cpu);
452 __raw_spin_unlock(&ftrace_max_lock);
456 * register_tracer - register a tracer with the ftrace system.
457 * @type - the plugin for the tracer
459 * Register a new plugin tracer.
461 int register_tracer(struct tracer *type)
462 __releases(kernel_lock)
463 __acquires(kernel_lock)
470 pr_info("Tracer must have a name\n");
475 * When this gets called we hold the BKL which means that
476 * preemption is disabled. Various trace selftests however
477 * need to disable and enable preemption for successful tests.
478 * So we drop the BKL here and grab it after the tests again.
481 mutex_lock(&trace_types_lock);
483 tracing_selftest_running = true;
485 for (t = trace_types; t; t = t->next) {
486 if (strcmp(type->name, t->name) == 0) {
488 pr_info("Trace %s already registered\n",
496 type->set_flag = &dummy_set_flag;
498 type->flags = &dummy_tracer_flags;
500 if (!type->flags->opts)
501 type->flags->opts = dummy_tracer_opt;
502 if (!type->wait_pipe)
503 type->wait_pipe = default_wait_pipe;
506 #ifdef CONFIG_FTRACE_STARTUP_TEST
507 if (type->selftest && !tracing_selftest_disabled) {
508 struct tracer *saved_tracer = current_trace;
509 struct trace_array *tr = &global_trace;
513 * Run a selftest on this tracer.
514 * Here we reset the trace buffer, and set the current
515 * tracer to be this tracer. The tracer can then run some
516 * internal tracing to verify that everything is in order.
517 * If we fail, we do not register this tracer.
519 for_each_tracing_cpu(i)
520 tracing_reset(tr, i);
522 current_trace = type;
523 /* the test is responsible for initializing and enabling */
524 pr_info("Testing tracer %s: ", type->name);
525 ret = type->selftest(type, tr);
526 /* the test is responsible for resetting too */
527 current_trace = saved_tracer;
529 printk(KERN_CONT "FAILED!\n");
532 /* Only reset on passing, to avoid touching corrupted buffers */
533 for_each_tracing_cpu(i)
534 tracing_reset(tr, i);
536 printk(KERN_CONT "PASSED\n");
540 type->next = trace_types;
542 len = strlen(type->name);
543 if (len > max_tracer_type_len)
544 max_tracer_type_len = len;
547 tracing_selftest_running = false;
548 mutex_unlock(&trace_types_lock);
550 if (ret || !default_bootup_tracer)
553 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
556 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
557 /* Do we want this tracer to start on bootup? */
558 tracing_set_tracer(type->name);
559 default_bootup_tracer = NULL;
560 /* disable other selftests, since this will break it. */
561 tracing_selftest_disabled = 1;
562 #ifdef CONFIG_FTRACE_STARTUP_TEST
563 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
572 void unregister_tracer(struct tracer *type)
577 mutex_lock(&trace_types_lock);
578 for (t = &trace_types; *t; t = &(*t)->next) {
582 pr_info("Trace %s not registered\n", type->name);
588 if (type == current_trace && tracer_enabled) {
591 if (current_trace->stop)
592 current_trace->stop(&global_trace);
593 current_trace = &nop_trace;
596 if (strlen(type->name) != max_tracer_type_len)
599 max_tracer_type_len = 0;
600 for (t = &trace_types; *t; t = &(*t)->next) {
601 len = strlen((*t)->name);
602 if (len > max_tracer_type_len)
603 max_tracer_type_len = len;
606 mutex_unlock(&trace_types_lock);
609 void tracing_reset(struct trace_array *tr, int cpu)
611 ftrace_disable_cpu();
612 ring_buffer_reset_cpu(tr->buffer, cpu);
616 void tracing_reset_online_cpus(struct trace_array *tr)
620 tr->time_start = ftrace_now(tr->cpu);
622 for_each_online_cpu(cpu)
623 tracing_reset(tr, cpu);
626 #define SAVED_CMDLINES 128
627 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
628 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
629 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
630 static int cmdline_idx;
631 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
633 /* temporary disable recording */
634 static atomic_t trace_record_cmdline_disabled __read_mostly;
636 static void trace_init_cmdlines(void)
638 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
639 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
643 static int trace_stop_count;
644 static DEFINE_SPINLOCK(tracing_start_lock);
647 * ftrace_off_permanent - disable all ftrace code permanently
649 * This should only be called when a serious anomally has
650 * been detected. This will turn off the function tracing,
651 * ring buffers, and other tracing utilites. It takes no
652 * locks and can be called from any context.
654 void ftrace_off_permanent(void)
656 tracing_disabled = 1;
658 tracing_off_permanent();
662 * tracing_start - quick start of the tracer
664 * If tracing is enabled but was stopped by tracing_stop,
665 * this will start the tracer back up.
667 void tracing_start(void)
669 struct ring_buffer *buffer;
672 if (tracing_disabled)
675 spin_lock_irqsave(&tracing_start_lock, flags);
676 if (--trace_stop_count) {
677 if (trace_stop_count < 0) {
678 /* Someone screwed up their debugging */
680 trace_stop_count = 0;
686 buffer = global_trace.buffer;
688 ring_buffer_record_enable(buffer);
690 buffer = max_tr.buffer;
692 ring_buffer_record_enable(buffer);
696 spin_unlock_irqrestore(&tracing_start_lock, flags);
700 * tracing_stop - quick stop of the tracer
702 * Light weight way to stop tracing. Use in conjunction with
705 void tracing_stop(void)
707 struct ring_buffer *buffer;
711 spin_lock_irqsave(&tracing_start_lock, flags);
712 if (trace_stop_count++)
715 buffer = global_trace.buffer;
717 ring_buffer_record_disable(buffer);
719 buffer = max_tr.buffer;
721 ring_buffer_record_disable(buffer);
724 spin_unlock_irqrestore(&tracing_start_lock, flags);
727 void trace_stop_cmdline_recording(void);
729 static void trace_save_cmdline(struct task_struct *tsk)
734 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
738 * It's not the end of the world if we don't get
739 * the lock, but we also don't want to spin
740 * nor do we want to disable interrupts,
741 * so if we miss here, then better luck next time.
743 if (!__raw_spin_trylock(&trace_cmdline_lock))
746 idx = map_pid_to_cmdline[tsk->pid];
747 if (idx >= SAVED_CMDLINES) {
748 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
750 map = map_cmdline_to_pid[idx];
751 if (map <= PID_MAX_DEFAULT)
752 map_pid_to_cmdline[map] = (unsigned)-1;
754 map_pid_to_cmdline[tsk->pid] = idx;
759 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
761 __raw_spin_unlock(&trace_cmdline_lock);
764 char *trace_find_cmdline(int pid)
766 char *cmdline = "<...>";
772 if (pid > PID_MAX_DEFAULT)
775 map = map_pid_to_cmdline[pid];
776 if (map >= SAVED_CMDLINES)
779 cmdline = saved_cmdlines[map];
785 void tracing_record_cmdline(struct task_struct *tsk)
787 if (atomic_read(&trace_record_cmdline_disabled))
790 trace_save_cmdline(tsk);
794 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
797 struct task_struct *tsk = current;
799 entry->preempt_count = pc & 0xff;
800 entry->pid = (tsk) ? tsk->pid : 0;
801 entry->tgid = (tsk) ? tsk->tgid : 0;
803 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
804 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
806 TRACE_FLAG_IRQS_NOSUPPORT |
808 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
809 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
810 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
813 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
816 unsigned long flags, int pc)
818 struct ring_buffer_event *event;
820 event = ring_buffer_lock_reserve(tr->buffer, len);
822 struct trace_entry *ent = ring_buffer_event_data(event);
824 tracing_generic_entry_update(ent, flags, pc);
830 static void ftrace_trace_stack(struct trace_array *tr,
831 unsigned long flags, int skip, int pc);
832 static void ftrace_trace_userstack(struct trace_array *tr,
833 unsigned long flags, int pc);
835 void trace_buffer_unlock_commit(struct trace_array *tr,
836 struct ring_buffer_event *event,
837 unsigned long flags, int pc)
839 ring_buffer_unlock_commit(tr->buffer, event);
841 ftrace_trace_stack(tr, flags, 6, pc);
842 ftrace_trace_userstack(tr, flags, pc);
846 struct ring_buffer_event *
847 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
848 unsigned long flags, int pc)
850 return trace_buffer_lock_reserve(&global_trace,
851 type, len, flags, pc);
854 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
855 unsigned long flags, int pc)
857 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
861 trace_function(struct trace_array *tr,
862 unsigned long ip, unsigned long parent_ip, unsigned long flags,
865 struct ring_buffer_event *event;
866 struct ftrace_entry *entry;
868 /* If we are reading the ring buffer, don't trace */
869 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
872 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
876 entry = ring_buffer_event_data(event);
878 entry->parent_ip = parent_ip;
879 ring_buffer_unlock_commit(tr->buffer, event);
882 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
883 static void __trace_graph_entry(struct trace_array *tr,
884 struct ftrace_graph_ent *trace,
888 struct ring_buffer_event *event;
889 struct ftrace_graph_ent_entry *entry;
891 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
894 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
895 sizeof(*entry), flags, pc);
898 entry = ring_buffer_event_data(event);
899 entry->graph_ent = *trace;
900 ring_buffer_unlock_commit(global_trace.buffer, event);
903 static void __trace_graph_return(struct trace_array *tr,
904 struct ftrace_graph_ret *trace,
908 struct ring_buffer_event *event;
909 struct ftrace_graph_ret_entry *entry;
911 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
914 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
915 sizeof(*entry), flags, pc);
918 entry = ring_buffer_event_data(event);
920 ring_buffer_unlock_commit(global_trace.buffer, event);
925 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
926 unsigned long ip, unsigned long parent_ip, unsigned long flags,
929 if (likely(!atomic_read(&data->disabled)))
930 trace_function(tr, ip, parent_ip, flags, pc);
933 static void __ftrace_trace_stack(struct trace_array *tr,
937 #ifdef CONFIG_STACKTRACE
938 struct ring_buffer_event *event;
939 struct stack_entry *entry;
940 struct stack_trace trace;
942 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
943 sizeof(*entry), flags, pc);
946 entry = ring_buffer_event_data(event);
947 memset(&entry->caller, 0, sizeof(entry->caller));
949 trace.nr_entries = 0;
950 trace.max_entries = FTRACE_STACK_ENTRIES;
952 trace.entries = entry->caller;
954 save_stack_trace(&trace);
955 ring_buffer_unlock_commit(tr->buffer, event);
959 static void ftrace_trace_stack(struct trace_array *tr,
963 if (!(trace_flags & TRACE_ITER_STACKTRACE))
966 __ftrace_trace_stack(tr, flags, skip, pc);
969 void __trace_stack(struct trace_array *tr,
973 __ftrace_trace_stack(tr, flags, skip, pc);
976 static void ftrace_trace_userstack(struct trace_array *tr,
977 unsigned long flags, int pc)
979 #ifdef CONFIG_STACKTRACE
980 struct ring_buffer_event *event;
981 struct userstack_entry *entry;
982 struct stack_trace trace;
984 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
987 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
988 sizeof(*entry), flags, pc);
991 entry = ring_buffer_event_data(event);
993 memset(&entry->caller, 0, sizeof(entry->caller));
995 trace.nr_entries = 0;
996 trace.max_entries = FTRACE_STACK_ENTRIES;
998 trace.entries = entry->caller;
1000 save_stack_trace_user(&trace);
1001 ring_buffer_unlock_commit(tr->buffer, event);
1006 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1008 ftrace_trace_userstack(tr, flags, preempt_count());
1013 ftrace_trace_special(void *__tr,
1014 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1017 struct ring_buffer_event *event;
1018 struct trace_array *tr = __tr;
1019 struct special_entry *entry;
1021 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1022 sizeof(*entry), 0, pc);
1025 entry = ring_buffer_event_data(event);
1029 trace_buffer_unlock_commit(tr, event, 0, pc);
1033 __trace_special(void *__tr, void *__data,
1034 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1036 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1040 tracing_sched_switch_trace(struct trace_array *tr,
1041 struct task_struct *prev,
1042 struct task_struct *next,
1043 unsigned long flags, int pc)
1045 struct ring_buffer_event *event;
1046 struct ctx_switch_entry *entry;
1048 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1049 sizeof(*entry), flags, pc);
1052 entry = ring_buffer_event_data(event);
1053 entry->prev_pid = prev->pid;
1054 entry->prev_prio = prev->prio;
1055 entry->prev_state = prev->state;
1056 entry->next_pid = next->pid;
1057 entry->next_prio = next->prio;
1058 entry->next_state = next->state;
1059 entry->next_cpu = task_cpu(next);
1060 trace_buffer_unlock_commit(tr, event, flags, pc);
1064 tracing_sched_wakeup_trace(struct trace_array *tr,
1065 struct task_struct *wakee,
1066 struct task_struct *curr,
1067 unsigned long flags, int pc)
1069 struct ring_buffer_event *event;
1070 struct ctx_switch_entry *entry;
1072 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1073 sizeof(*entry), flags, pc);
1076 entry = ring_buffer_event_data(event);
1077 entry->prev_pid = curr->pid;
1078 entry->prev_prio = curr->prio;
1079 entry->prev_state = curr->state;
1080 entry->next_pid = wakee->pid;
1081 entry->next_prio = wakee->prio;
1082 entry->next_state = wakee->state;
1083 entry->next_cpu = task_cpu(wakee);
1085 ring_buffer_unlock_commit(tr->buffer, event);
1086 ftrace_trace_stack(tr, flags, 6, pc);
1087 ftrace_trace_userstack(tr, flags, pc);
1091 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1093 struct trace_array *tr = &global_trace;
1094 struct trace_array_cpu *data;
1095 unsigned long flags;
1099 if (tracing_disabled)
1102 pc = preempt_count();
1103 local_irq_save(flags);
1104 cpu = raw_smp_processor_id();
1105 data = tr->data[cpu];
1107 if (likely(atomic_inc_return(&data->disabled) == 1))
1108 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1110 atomic_dec(&data->disabled);
1111 local_irq_restore(flags);
1114 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1115 int trace_graph_entry(struct ftrace_graph_ent *trace)
1117 struct trace_array *tr = &global_trace;
1118 struct trace_array_cpu *data;
1119 unsigned long flags;
1124 if (!ftrace_trace_task(current))
1127 if (!ftrace_graph_addr(trace->func))
1130 local_irq_save(flags);
1131 cpu = raw_smp_processor_id();
1132 data = tr->data[cpu];
1133 disabled = atomic_inc_return(&data->disabled);
1134 if (likely(disabled == 1)) {
1135 pc = preempt_count();
1136 __trace_graph_entry(tr, trace, flags, pc);
1138 /* Only do the atomic if it is not already set */
1139 if (!test_tsk_trace_graph(current))
1140 set_tsk_trace_graph(current);
1141 atomic_dec(&data->disabled);
1142 local_irq_restore(flags);
1147 void trace_graph_return(struct ftrace_graph_ret *trace)
1149 struct trace_array *tr = &global_trace;
1150 struct trace_array_cpu *data;
1151 unsigned long flags;
1156 local_irq_save(flags);
1157 cpu = raw_smp_processor_id();
1158 data = tr->data[cpu];
1159 disabled = atomic_inc_return(&data->disabled);
1160 if (likely(disabled == 1)) {
1161 pc = preempt_count();
1162 __trace_graph_return(tr, trace, flags, pc);
1165 clear_tsk_trace_graph(current);
1166 atomic_dec(&data->disabled);
1167 local_irq_restore(flags);
1169 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1171 enum trace_file_type {
1172 TRACE_FILE_LAT_FMT = 1,
1173 TRACE_FILE_ANNOTATE = 2,
1176 static void trace_iterator_increment(struct trace_iterator *iter)
1178 /* Don't allow ftrace to trace into the ring buffers */
1179 ftrace_disable_cpu();
1182 if (iter->buffer_iter[iter->cpu])
1183 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1185 ftrace_enable_cpu();
1188 static struct trace_entry *
1189 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1191 struct ring_buffer_event *event;
1192 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1194 /* Don't allow ftrace to trace into the ring buffers */
1195 ftrace_disable_cpu();
1198 event = ring_buffer_iter_peek(buf_iter, ts);
1200 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1202 ftrace_enable_cpu();
1204 return event ? ring_buffer_event_data(event) : NULL;
1207 static struct trace_entry *
1208 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1210 struct ring_buffer *buffer = iter->tr->buffer;
1211 struct trace_entry *ent, *next = NULL;
1212 int cpu_file = iter->cpu_file;
1213 u64 next_ts = 0, ts;
1218 * If we are in a per_cpu trace file, don't bother by iterating over
1219 * all cpu and peek directly.
1221 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1222 if (ring_buffer_empty_cpu(buffer, cpu_file))
1224 ent = peek_next_entry(iter, cpu_file, ent_ts);
1226 *ent_cpu = cpu_file;
1231 for_each_tracing_cpu(cpu) {
1233 if (ring_buffer_empty_cpu(buffer, cpu))
1236 ent = peek_next_entry(iter, cpu, &ts);
1239 * Pick the entry with the smallest timestamp:
1241 if (ent && (!next || ts < next_ts)) {
1249 *ent_cpu = next_cpu;
1257 /* Find the next real entry, without updating the iterator itself */
1258 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1259 int *ent_cpu, u64 *ent_ts)
1261 return __find_next_entry(iter, ent_cpu, ent_ts);
1264 /* Find the next real entry, and increment the iterator to the next entry */
1265 static void *find_next_entry_inc(struct trace_iterator *iter)
1267 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1270 trace_iterator_increment(iter);
1272 return iter->ent ? iter : NULL;
1275 static void trace_consume(struct trace_iterator *iter)
1277 /* Don't allow ftrace to trace into the ring buffers */
1278 ftrace_disable_cpu();
1279 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1280 ftrace_enable_cpu();
1283 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1285 struct trace_iterator *iter = m->private;
1291 /* can't go backwards */
1296 ent = find_next_entry_inc(iter);
1300 while (ent && iter->idx < i)
1301 ent = find_next_entry_inc(iter);
1309 * No necessary locking here. The worst thing which can
1310 * happen is loosing events consumed at the same time
1311 * by a trace_pipe reader.
1312 * Other than that, we don't risk to crash the ring buffer
1313 * because it serializes the readers.
1315 * The current tracer is copied to avoid a global locking
1318 static void *s_start(struct seq_file *m, loff_t *pos)
1320 struct trace_iterator *iter = m->private;
1321 static struct tracer *old_tracer;
1322 int cpu_file = iter->cpu_file;
1327 /* copy the tracer to avoid using a global lock all around */
1328 mutex_lock(&trace_types_lock);
1329 if (unlikely(old_tracer != current_trace && current_trace)) {
1330 old_tracer = current_trace;
1331 *iter->trace = *current_trace;
1333 mutex_unlock(&trace_types_lock);
1335 atomic_inc(&trace_record_cmdline_disabled);
1337 if (*pos != iter->pos) {
1342 ftrace_disable_cpu();
1344 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1345 for_each_tracing_cpu(cpu)
1346 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1348 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1351 ftrace_enable_cpu();
1353 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1358 p = s_next(m, p, &l);
1364 static void s_stop(struct seq_file *m, void *p)
1366 atomic_dec(&trace_record_cmdline_disabled);
1369 static void print_lat_help_header(struct seq_file *m)
1371 seq_puts(m, "# _------=> CPU# \n");
1372 seq_puts(m, "# / _-----=> irqs-off \n");
1373 seq_puts(m, "# | / _----=> need-resched \n");
1374 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1375 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1376 seq_puts(m, "# |||| / \n");
1377 seq_puts(m, "# ||||| delay \n");
1378 seq_puts(m, "# cmd pid ||||| time | caller \n");
1379 seq_puts(m, "# \\ / ||||| \\ | / \n");
1382 static void print_func_help_header(struct seq_file *m)
1384 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1385 seq_puts(m, "# | | | | |\n");
1390 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1392 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1393 struct trace_array *tr = iter->tr;
1394 struct trace_array_cpu *data = tr->data[tr->cpu];
1395 struct tracer *type = current_trace;
1396 unsigned long total;
1397 unsigned long entries;
1398 const char *name = "preemption";
1403 entries = ring_buffer_entries(iter->tr->buffer);
1405 ring_buffer_overruns(iter->tr->buffer);
1407 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1409 seq_puts(m, "-----------------------------------"
1410 "---------------------------------\n");
1411 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1412 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1413 nsecs_to_usecs(data->saved_latency),
1417 #if defined(CONFIG_PREEMPT_NONE)
1419 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1421 #elif defined(CONFIG_PREEMPT)
1426 /* These are reserved for later use */
1429 seq_printf(m, " #P:%d)\n", num_online_cpus());
1433 seq_puts(m, " -----------------\n");
1434 seq_printf(m, " | task: %.16s-%d "
1435 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1436 data->comm, data->pid, data->uid, data->nice,
1437 data->policy, data->rt_priority);
1438 seq_puts(m, " -----------------\n");
1440 if (data->critical_start) {
1441 seq_puts(m, " => started at: ");
1442 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1443 trace_print_seq(m, &iter->seq);
1444 seq_puts(m, "\n => ended at: ");
1445 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1446 trace_print_seq(m, &iter->seq);
1453 static void test_cpu_buff_start(struct trace_iterator *iter)
1455 struct trace_seq *s = &iter->seq;
1457 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1460 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1463 if (cpumask_test_cpu(iter->cpu, iter->started))
1466 cpumask_set_cpu(iter->cpu, iter->started);
1467 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1470 static enum print_line_t print_lat_fmt(struct trace_iterator *iter)
1472 struct trace_seq *s = &iter->seq;
1473 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1474 struct trace_event *event;
1475 struct trace_entry *entry = iter->ent;
1477 test_cpu_buff_start(iter);
1479 event = ftrace_find_event(entry->type);
1481 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1482 if (!trace_print_lat_context(iter))
1487 return event->latency_trace(iter, sym_flags);
1489 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1492 return TRACE_TYPE_HANDLED;
1494 return TRACE_TYPE_PARTIAL_LINE;
1497 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1499 struct trace_seq *s = &iter->seq;
1500 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1501 struct trace_entry *entry;
1502 struct trace_event *event;
1506 test_cpu_buff_start(iter);
1508 event = ftrace_find_event(entry->type);
1510 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1511 if (!trace_print_context(iter))
1516 return event->trace(iter, sym_flags);
1518 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1521 return TRACE_TYPE_HANDLED;
1523 return TRACE_TYPE_PARTIAL_LINE;
1526 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1528 struct trace_seq *s = &iter->seq;
1529 struct trace_entry *entry;
1530 struct trace_event *event;
1534 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1535 if (!trace_seq_printf(s, "%d %d %llu ",
1536 entry->pid, iter->cpu, iter->ts))
1540 event = ftrace_find_event(entry->type);
1542 return event->raw(iter, 0);
1544 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1547 return TRACE_TYPE_HANDLED;
1549 return TRACE_TYPE_PARTIAL_LINE;
1552 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1554 struct trace_seq *s = &iter->seq;
1555 unsigned char newline = '\n';
1556 struct trace_entry *entry;
1557 struct trace_event *event;
1561 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1562 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1563 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1564 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1567 event = ftrace_find_event(entry->type);
1569 enum print_line_t ret = event->hex(iter, 0);
1570 if (ret != TRACE_TYPE_HANDLED)
1574 SEQ_PUT_FIELD_RET(s, newline);
1576 return TRACE_TYPE_HANDLED;
1579 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1581 struct trace_seq *s = &iter->seq;
1582 struct trace_entry *entry = iter->ent;
1583 struct print_entry *field;
1586 trace_assign_type(field, entry);
1588 ret = trace_seq_printf(s, "%s", field->buf);
1590 return TRACE_TYPE_PARTIAL_LINE;
1592 return TRACE_TYPE_HANDLED;
1595 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1597 struct trace_seq *s = &iter->seq;
1598 struct trace_entry *entry;
1599 struct trace_event *event;
1603 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1604 SEQ_PUT_FIELD_RET(s, entry->pid);
1605 SEQ_PUT_FIELD_RET(s, iter->cpu);
1606 SEQ_PUT_FIELD_RET(s, iter->ts);
1609 event = ftrace_find_event(entry->type);
1610 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1613 static int trace_empty(struct trace_iterator *iter)
1617 for_each_tracing_cpu(cpu) {
1618 if (iter->buffer_iter[cpu]) {
1619 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1622 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1630 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1632 enum print_line_t ret;
1634 if (iter->trace && iter->trace->print_line) {
1635 ret = iter->trace->print_line(iter);
1636 if (ret != TRACE_TYPE_UNHANDLED)
1640 if (iter->ent->type == TRACE_PRINT &&
1641 trace_flags & TRACE_ITER_PRINTK &&
1642 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1643 return print_printk_msg_only(iter);
1645 if (trace_flags & TRACE_ITER_BIN)
1646 return print_bin_fmt(iter);
1648 if (trace_flags & TRACE_ITER_HEX)
1649 return print_hex_fmt(iter);
1651 if (trace_flags & TRACE_ITER_RAW)
1652 return print_raw_fmt(iter);
1654 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1655 return print_lat_fmt(iter);
1657 return print_trace_fmt(iter);
1660 static int s_show(struct seq_file *m, void *v)
1662 struct trace_iterator *iter = v;
1664 if (iter->ent == NULL) {
1666 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1669 if (iter->trace && iter->trace->print_header)
1670 iter->trace->print_header(m);
1671 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1672 /* print nothing if the buffers are empty */
1673 if (trace_empty(iter))
1675 print_trace_header(m, iter);
1676 if (!(trace_flags & TRACE_ITER_VERBOSE))
1677 print_lat_help_header(m);
1679 if (!(trace_flags & TRACE_ITER_VERBOSE))
1680 print_func_help_header(m);
1683 print_trace_line(iter);
1684 trace_print_seq(m, &iter->seq);
1690 static struct seq_operations tracer_seq_ops = {
1697 static struct trace_iterator *
1698 __tracing_open(struct inode *inode, struct file *file)
1700 long cpu_file = (long) inode->i_private;
1701 void *fail_ret = ERR_PTR(-ENOMEM);
1702 struct trace_iterator *iter;
1706 if (tracing_disabled)
1707 return ERR_PTR(-ENODEV);
1709 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1711 return ERR_PTR(-ENOMEM);
1714 * We make a copy of the current tracer to avoid concurrent
1715 * changes on it while we are reading.
1717 mutex_lock(&trace_types_lock);
1718 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1723 *iter->trace = *current_trace;
1725 if (current_trace && current_trace->print_max)
1728 iter->tr = &global_trace;
1730 mutex_init(&iter->mutex);
1731 iter->cpu_file = cpu_file;
1733 /* Notify the tracer early; before we stop tracing. */
1734 if (iter->trace && iter->trace->open)
1735 iter->trace->open(iter);
1737 /* Annotate start of buffers if we had overruns */
1738 if (ring_buffer_overruns(iter->tr->buffer))
1739 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1741 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1742 for_each_tracing_cpu(cpu) {
1744 iter->buffer_iter[cpu] =
1745 ring_buffer_read_start(iter->tr->buffer, cpu);
1747 if (!iter->buffer_iter[cpu])
1751 cpu = iter->cpu_file;
1752 iter->buffer_iter[cpu] =
1753 ring_buffer_read_start(iter->tr->buffer, cpu);
1755 if (!iter->buffer_iter[cpu])
1759 /* TODO stop tracer */
1760 ret = seq_open(file, &tracer_seq_ops);
1762 fail_ret = ERR_PTR(ret);
1766 m = file->private_data;
1769 /* stop the trace while dumping */
1772 mutex_unlock(&trace_types_lock);
1777 for_each_tracing_cpu(cpu) {
1778 if (iter->buffer_iter[cpu])
1779 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1782 mutex_unlock(&trace_types_lock);
1789 int tracing_open_generic(struct inode *inode, struct file *filp)
1791 if (tracing_disabled)
1794 filp->private_data = inode->i_private;
1798 static int tracing_release(struct inode *inode, struct file *file)
1800 struct seq_file *m = (struct seq_file *)file->private_data;
1801 struct trace_iterator *iter = m->private;
1804 mutex_lock(&trace_types_lock);
1805 for_each_tracing_cpu(cpu) {
1806 if (iter->buffer_iter[cpu])
1807 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1810 if (iter->trace && iter->trace->close)
1811 iter->trace->close(iter);
1813 /* reenable tracing if it was previously enabled */
1815 mutex_unlock(&trace_types_lock);
1817 seq_release(inode, file);
1818 mutex_destroy(&iter->mutex);
1824 static int tracing_open(struct inode *inode, struct file *file)
1826 struct trace_iterator *iter;
1829 iter = __tracing_open(inode, file);
1831 ret = PTR_ERR(iter);
1836 static int tracing_lt_open(struct inode *inode, struct file *file)
1838 struct trace_iterator *iter;
1841 iter = __tracing_open(inode, file);
1844 ret = PTR_ERR(iter);
1846 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1853 t_next(struct seq_file *m, void *v, loff_t *pos)
1855 struct tracer *t = m->private;
1867 static void *t_start(struct seq_file *m, loff_t *pos)
1869 struct tracer *t = m->private;
1872 mutex_lock(&trace_types_lock);
1873 for (; t && l < *pos; t = t_next(m, t, &l))
1879 static void t_stop(struct seq_file *m, void *p)
1881 mutex_unlock(&trace_types_lock);
1884 static int t_show(struct seq_file *m, void *v)
1886 struct tracer *t = v;
1891 seq_printf(m, "%s", t->name);
1900 static struct seq_operations show_traces_seq_ops = {
1907 static int show_traces_open(struct inode *inode, struct file *file)
1911 if (tracing_disabled)
1914 ret = seq_open(file, &show_traces_seq_ops);
1916 struct seq_file *m = file->private_data;
1917 m->private = trace_types;
1923 static struct file_operations tracing_fops = {
1924 .open = tracing_open,
1926 .llseek = seq_lseek,
1927 .release = tracing_release,
1930 static struct file_operations tracing_lt_fops = {
1931 .open = tracing_lt_open,
1933 .llseek = seq_lseek,
1934 .release = tracing_release,
1937 static struct file_operations show_traces_fops = {
1938 .open = show_traces_open,
1940 .release = seq_release,
1944 * Only trace on a CPU if the bitmask is set:
1946 static cpumask_var_t tracing_cpumask;
1949 * The tracer itself will not take this lock, but still we want
1950 * to provide a consistent cpumask to user-space:
1952 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1955 * Temporary storage for the character representation of the
1956 * CPU bitmask (and one more byte for the newline):
1958 static char mask_str[NR_CPUS + 1];
1961 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1962 size_t count, loff_t *ppos)
1966 mutex_lock(&tracing_cpumask_update_lock);
1968 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1969 if (count - len < 2) {
1973 len += sprintf(mask_str + len, "\n");
1974 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1977 mutex_unlock(&tracing_cpumask_update_lock);
1983 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1984 size_t count, loff_t *ppos)
1987 cpumask_var_t tracing_cpumask_new;
1989 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1992 mutex_lock(&tracing_cpumask_update_lock);
1993 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1997 local_irq_disable();
1998 __raw_spin_lock(&ftrace_max_lock);
1999 for_each_tracing_cpu(cpu) {
2001 * Increase/decrease the disabled counter if we are
2002 * about to flip a bit in the cpumask:
2004 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2005 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2006 atomic_inc(&global_trace.data[cpu]->disabled);
2008 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2009 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2010 atomic_dec(&global_trace.data[cpu]->disabled);
2013 __raw_spin_unlock(&ftrace_max_lock);
2016 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2018 mutex_unlock(&tracing_cpumask_update_lock);
2019 free_cpumask_var(tracing_cpumask_new);
2024 mutex_unlock(&tracing_cpumask_update_lock);
2025 free_cpumask_var(tracing_cpumask);
2030 static struct file_operations tracing_cpumask_fops = {
2031 .open = tracing_open_generic,
2032 .read = tracing_cpumask_read,
2033 .write = tracing_cpumask_write,
2037 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2038 size_t cnt, loff_t *ppos)
2040 struct tracer_opt *trace_opts;
2048 /* calculate max size */
2049 for (i = 0; trace_options[i]; i++) {
2050 len += strlen(trace_options[i]);
2051 len += 3; /* "no" and newline */
2054 mutex_lock(&trace_types_lock);
2055 tracer_flags = current_trace->flags->val;
2056 trace_opts = current_trace->flags->opts;
2059 * Increase the size with names of options specific
2060 * of the current tracer.
2062 for (i = 0; trace_opts[i].name; i++) {
2063 len += strlen(trace_opts[i].name);
2064 len += 3; /* "no" and newline */
2067 /* +2 for \n and \0 */
2068 buf = kmalloc(len + 2, GFP_KERNEL);
2070 mutex_unlock(&trace_types_lock);
2074 for (i = 0; trace_options[i]; i++) {
2075 if (trace_flags & (1 << i))
2076 r += sprintf(buf + r, "%s\n", trace_options[i]);
2078 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2081 for (i = 0; trace_opts[i].name; i++) {
2082 if (tracer_flags & trace_opts[i].bit)
2083 r += sprintf(buf + r, "%s\n",
2084 trace_opts[i].name);
2086 r += sprintf(buf + r, "no%s\n",
2087 trace_opts[i].name);
2089 mutex_unlock(&trace_types_lock);
2091 WARN_ON(r >= len + 2);
2093 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2099 /* Try to assign a tracer specific option */
2100 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2102 struct tracer_flags *trace_flags = trace->flags;
2103 struct tracer_opt *opts = NULL;
2107 for (i = 0; trace_flags->opts[i].name; i++) {
2108 opts = &trace_flags->opts[i];
2109 len = strlen(opts->name);
2111 if (strncmp(cmp, opts->name, len) == 0) {
2112 ret = trace->set_flag(trace_flags->val,
2118 if (!trace_flags->opts[i].name)
2121 /* Refused to handle */
2126 trace_flags->val &= ~opts->bit;
2128 trace_flags->val |= opts->bit;
2134 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2135 size_t cnt, loff_t *ppos)
2143 if (cnt >= sizeof(buf))
2146 if (copy_from_user(&buf, ubuf, cnt))
2151 if (strncmp(buf, "no", 2) == 0) {
2156 for (i = 0; trace_options[i]; i++) {
2157 int len = strlen(trace_options[i]);
2159 if (strncmp(cmp, trace_options[i], len) == 0) {
2161 trace_flags &= ~(1 << i);
2163 trace_flags |= (1 << i);
2168 /* If no option could be set, test the specific tracer options */
2169 if (!trace_options[i]) {
2170 mutex_lock(&trace_types_lock);
2171 ret = set_tracer_option(current_trace, cmp, neg);
2172 mutex_unlock(&trace_types_lock);
2182 static struct file_operations tracing_iter_fops = {
2183 .open = tracing_open_generic,
2184 .read = tracing_trace_options_read,
2185 .write = tracing_trace_options_write,
2188 static const char readme_msg[] =
2189 "tracing mini-HOWTO:\n\n"
2191 "# mount -t debugfs nodev /debug\n\n"
2192 "# cat /debug/tracing/available_tracers\n"
2193 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2194 "# cat /debug/tracing/current_tracer\n"
2196 "# echo sched_switch > /debug/tracing/current_tracer\n"
2197 "# cat /debug/tracing/current_tracer\n"
2199 "# cat /debug/tracing/trace_options\n"
2200 "noprint-parent nosym-offset nosym-addr noverbose\n"
2201 "# echo print-parent > /debug/tracing/trace_options\n"
2202 "# echo 1 > /debug/tracing/tracing_enabled\n"
2203 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2204 "echo 0 > /debug/tracing/tracing_enabled\n"
2208 tracing_readme_read(struct file *filp, char __user *ubuf,
2209 size_t cnt, loff_t *ppos)
2211 return simple_read_from_buffer(ubuf, cnt, ppos,
2212 readme_msg, strlen(readme_msg));
2215 static struct file_operations tracing_readme_fops = {
2216 .open = tracing_open_generic,
2217 .read = tracing_readme_read,
2221 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2222 size_t cnt, loff_t *ppos)
2227 r = sprintf(buf, "%u\n", tracer_enabled);
2228 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2232 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2233 size_t cnt, loff_t *ppos)
2235 struct trace_array *tr = filp->private_data;
2240 if (cnt >= sizeof(buf))
2243 if (copy_from_user(&buf, ubuf, cnt))
2248 ret = strict_strtoul(buf, 10, &val);
2254 mutex_lock(&trace_types_lock);
2255 if (tracer_enabled ^ val) {
2258 if (current_trace->start)
2259 current_trace->start(tr);
2264 if (current_trace->stop)
2265 current_trace->stop(tr);
2268 mutex_unlock(&trace_types_lock);
2276 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2277 size_t cnt, loff_t *ppos)
2279 char buf[max_tracer_type_len+2];
2282 mutex_lock(&trace_types_lock);
2284 r = sprintf(buf, "%s\n", current_trace->name);
2286 r = sprintf(buf, "\n");
2287 mutex_unlock(&trace_types_lock);
2289 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2292 int tracer_init(struct tracer *t, struct trace_array *tr)
2294 tracing_reset_online_cpus(tr);
2298 struct trace_option_dentry;
2300 static struct trace_option_dentry *
2301 create_trace_option_files(struct tracer *tracer);
2304 destroy_trace_option_files(struct trace_option_dentry *topts);
2306 static int tracing_set_tracer(const char *buf)
2308 static struct trace_option_dentry *topts;
2309 struct trace_array *tr = &global_trace;
2313 mutex_lock(&trace_types_lock);
2314 for (t = trace_types; t; t = t->next) {
2315 if (strcmp(t->name, buf) == 0)
2322 if (t == current_trace)
2325 trace_branch_disable();
2326 if (current_trace && current_trace->reset)
2327 current_trace->reset(tr);
2329 destroy_trace_option_files(topts);
2333 topts = create_trace_option_files(current_trace);
2336 ret = tracer_init(t, tr);
2341 trace_branch_enable(tr);
2343 mutex_unlock(&trace_types_lock);
2349 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2350 size_t cnt, loff_t *ppos)
2352 char buf[max_tracer_type_len+1];
2359 if (cnt > max_tracer_type_len)
2360 cnt = max_tracer_type_len;
2362 if (copy_from_user(&buf, ubuf, cnt))
2367 /* strip ending whitespace. */
2368 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2371 err = tracing_set_tracer(buf);
2381 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2382 size_t cnt, loff_t *ppos)
2384 unsigned long *ptr = filp->private_data;
2388 r = snprintf(buf, sizeof(buf), "%ld\n",
2389 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2390 if (r > sizeof(buf))
2392 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2396 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2397 size_t cnt, loff_t *ppos)
2399 unsigned long *ptr = filp->private_data;
2404 if (cnt >= sizeof(buf))
2407 if (copy_from_user(&buf, ubuf, cnt))
2412 ret = strict_strtoul(buf, 10, &val);
2421 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2423 long cpu_file = (long) inode->i_private;
2424 struct trace_iterator *iter;
2427 if (tracing_disabled)
2430 mutex_lock(&trace_types_lock);
2432 /* We only allow one reader per cpu */
2433 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2434 if (!cpumask_empty(tracing_reader_cpumask)) {
2438 cpumask_setall(tracing_reader_cpumask);
2440 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2441 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2448 /* create a buffer to store the information to pass to userspace */
2449 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2456 * We make a copy of the current tracer to avoid concurrent
2457 * changes on it while we are reading.
2459 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2465 *iter->trace = *current_trace;
2467 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2472 /* trace pipe does not show start of buffer */
2473 cpumask_setall(iter->started);
2475 iter->cpu_file = cpu_file;
2476 iter->tr = &global_trace;
2477 mutex_init(&iter->mutex);
2478 filp->private_data = iter;
2480 if (iter->trace->pipe_open)
2481 iter->trace->pipe_open(iter);
2484 mutex_unlock(&trace_types_lock);
2490 mutex_unlock(&trace_types_lock);
2494 static int tracing_release_pipe(struct inode *inode, struct file *file)
2496 struct trace_iterator *iter = file->private_data;
2498 mutex_lock(&trace_types_lock);
2500 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2501 cpumask_clear(tracing_reader_cpumask);
2503 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2505 mutex_unlock(&trace_types_lock);
2507 free_cpumask_var(iter->started);
2508 mutex_destroy(&iter->mutex);
2516 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2518 struct trace_iterator *iter = filp->private_data;
2520 if (trace_flags & TRACE_ITER_BLOCK) {
2522 * Always select as readable when in blocking mode
2524 return POLLIN | POLLRDNORM;
2526 if (!trace_empty(iter))
2527 return POLLIN | POLLRDNORM;
2528 poll_wait(filp, &trace_wait, poll_table);
2529 if (!trace_empty(iter))
2530 return POLLIN | POLLRDNORM;
2537 void default_wait_pipe(struct trace_iterator *iter)
2541 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2543 if (trace_empty(iter))
2546 finish_wait(&trace_wait, &wait);
2550 * This is a make-shift waitqueue.
2551 * A tracer might use this callback on some rare cases:
2553 * 1) the current tracer might hold the runqueue lock when it wakes up
2554 * a reader, hence a deadlock (sched, function, and function graph tracers)
2555 * 2) the function tracers, trace all functions, we don't want
2556 * the overhead of calling wake_up and friends
2557 * (and tracing them too)
2559 * Anyway, this is really very primitive wakeup.
2561 void poll_wait_pipe(struct trace_iterator *iter)
2563 set_current_state(TASK_INTERRUPTIBLE);
2564 /* sleep for 100 msecs, and try again. */
2565 schedule_timeout(HZ / 10);
2568 /* Must be called with trace_types_lock mutex held. */
2569 static int tracing_wait_pipe(struct file *filp)
2571 struct trace_iterator *iter = filp->private_data;
2573 while (trace_empty(iter)) {
2575 if ((filp->f_flags & O_NONBLOCK)) {
2579 mutex_unlock(&iter->mutex);
2581 iter->trace->wait_pipe(iter);
2583 mutex_lock(&iter->mutex);
2585 if (signal_pending(current))
2589 * We block until we read something and tracing is disabled.
2590 * We still block if tracing is disabled, but we have never
2591 * read anything. This allows a user to cat this file, and
2592 * then enable tracing. But after we have read something,
2593 * we give an EOF when tracing is again disabled.
2595 * iter->pos will be 0 if we haven't read anything.
2597 if (!tracer_enabled && iter->pos)
2608 tracing_read_pipe(struct file *filp, char __user *ubuf,
2609 size_t cnt, loff_t *ppos)
2611 struct trace_iterator *iter = filp->private_data;
2612 static struct tracer *old_tracer;
2615 /* return any leftover data */
2616 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2620 trace_seq_init(&iter->seq);
2622 /* copy the tracer to avoid using a global lock all around */
2623 mutex_lock(&trace_types_lock);
2624 if (unlikely(old_tracer != current_trace && current_trace)) {
2625 old_tracer = current_trace;
2626 *iter->trace = *current_trace;
2628 mutex_unlock(&trace_types_lock);
2631 * Avoid more than one consumer on a single file descriptor
2632 * This is just a matter of traces coherency, the ring buffer itself
2635 mutex_lock(&iter->mutex);
2636 if (iter->trace->read) {
2637 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2643 sret = tracing_wait_pipe(filp);
2647 /* stop when tracing is finished */
2648 if (trace_empty(iter)) {
2653 if (cnt >= PAGE_SIZE)
2654 cnt = PAGE_SIZE - 1;
2656 /* reset all but tr, trace, and overruns */
2657 memset(&iter->seq, 0,
2658 sizeof(struct trace_iterator) -
2659 offsetof(struct trace_iterator, seq));
2662 while (find_next_entry_inc(iter) != NULL) {
2663 enum print_line_t ret;
2664 int len = iter->seq.len;
2666 ret = print_trace_line(iter);
2667 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2668 /* don't print partial lines */
2669 iter->seq.len = len;
2672 if (ret != TRACE_TYPE_NO_CONSUME)
2673 trace_consume(iter);
2675 if (iter->seq.len >= cnt)
2679 /* Now copy what we have to the user */
2680 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2681 if (iter->seq.readpos >= iter->seq.len)
2682 trace_seq_init(&iter->seq);
2685 * If there was nothing to send to user, inspite of consuming trace
2686 * entries, go back to wait for more entries.
2692 mutex_unlock(&iter->mutex);
2697 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2698 struct pipe_buffer *buf)
2700 __free_page(buf->page);
2703 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2706 __free_page(spd->pages[idx]);
2709 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2711 .map = generic_pipe_buf_map,
2712 .unmap = generic_pipe_buf_unmap,
2713 .confirm = generic_pipe_buf_confirm,
2714 .release = tracing_pipe_buf_release,
2715 .steal = generic_pipe_buf_steal,
2716 .get = generic_pipe_buf_get,
2720 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2725 /* Seq buffer is page-sized, exactly what we need. */
2727 count = iter->seq.len;
2728 ret = print_trace_line(iter);
2729 count = iter->seq.len - count;
2732 iter->seq.len -= count;
2735 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2736 iter->seq.len -= count;
2740 trace_consume(iter);
2742 if (!find_next_entry_inc(iter)) {
2752 static ssize_t tracing_splice_read_pipe(struct file *filp,
2754 struct pipe_inode_info *pipe,
2758 struct page *pages[PIPE_BUFFERS];
2759 struct partial_page partial[PIPE_BUFFERS];
2760 struct trace_iterator *iter = filp->private_data;
2761 struct splice_pipe_desc spd = {
2764 .nr_pages = 0, /* This gets updated below. */
2766 .ops = &tracing_pipe_buf_ops,
2767 .spd_release = tracing_spd_release_pipe,
2769 static struct tracer *old_tracer;
2774 /* copy the tracer to avoid using a global lock all around */
2775 mutex_lock(&trace_types_lock);
2776 if (unlikely(old_tracer != current_trace && current_trace)) {
2777 old_tracer = current_trace;
2778 *iter->trace = *current_trace;
2780 mutex_unlock(&trace_types_lock);
2782 mutex_lock(&iter->mutex);
2784 if (iter->trace->splice_read) {
2785 ret = iter->trace->splice_read(iter, filp,
2786 ppos, pipe, len, flags);
2791 ret = tracing_wait_pipe(filp);
2795 if (!iter->ent && !find_next_entry_inc(iter)) {
2800 /* Fill as many pages as possible. */
2801 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2802 pages[i] = alloc_page(GFP_KERNEL);
2806 rem = tracing_fill_pipe_page(rem, iter);
2808 /* Copy the data into the page, so we can start over. */
2809 ret = trace_seq_to_buffer(&iter->seq,
2810 page_address(pages[i]),
2813 __free_page(pages[i]);
2816 partial[i].offset = 0;
2817 partial[i].len = iter->seq.len;
2819 trace_seq_init(&iter->seq);
2822 mutex_unlock(&iter->mutex);
2826 return splice_to_pipe(pipe, &spd);
2829 mutex_unlock(&iter->mutex);
2835 tracing_entries_read(struct file *filp, char __user *ubuf,
2836 size_t cnt, loff_t *ppos)
2838 struct trace_array *tr = filp->private_data;
2842 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2843 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2847 tracing_entries_write(struct file *filp, const char __user *ubuf,
2848 size_t cnt, loff_t *ppos)
2854 if (cnt >= sizeof(buf))
2857 if (copy_from_user(&buf, ubuf, cnt))
2862 ret = strict_strtoul(buf, 10, &val);
2866 /* must have at least 1 entry */
2870 mutex_lock(&trace_types_lock);
2874 /* disable all cpu buffers */
2875 for_each_tracing_cpu(cpu) {
2876 if (global_trace.data[cpu])
2877 atomic_inc(&global_trace.data[cpu]->disabled);
2878 if (max_tr.data[cpu])
2879 atomic_inc(&max_tr.data[cpu]->disabled);
2882 /* value is in KB */
2885 if (val != global_trace.entries) {
2886 ret = ring_buffer_resize(global_trace.buffer, val);
2892 ret = ring_buffer_resize(max_tr.buffer, val);
2896 r = ring_buffer_resize(global_trace.buffer,
2897 global_trace.entries);
2899 /* AARGH! We are left with different
2900 * size max buffer!!!! */
2902 tracing_disabled = 1;
2907 global_trace.entries = val;
2912 /* If check pages failed, return ENOMEM */
2913 if (tracing_disabled)
2916 for_each_tracing_cpu(cpu) {
2917 if (global_trace.data[cpu])
2918 atomic_dec(&global_trace.data[cpu]->disabled);
2919 if (max_tr.data[cpu])
2920 atomic_dec(&max_tr.data[cpu]->disabled);
2924 max_tr.entries = global_trace.entries;
2925 mutex_unlock(&trace_types_lock);
2930 static int mark_printk(const char *fmt, ...)
2934 va_start(args, fmt);
2935 ret = trace_vprintk(0, -1, fmt, args);
2941 tracing_mark_write(struct file *filp, const char __user *ubuf,
2942 size_t cnt, loff_t *fpos)
2947 if (tracing_disabled)
2950 if (cnt > TRACE_BUF_SIZE)
2951 cnt = TRACE_BUF_SIZE;
2953 buf = kmalloc(cnt + 1, GFP_KERNEL);
2957 if (copy_from_user(buf, ubuf, cnt)) {
2962 /* Cut from the first nil or newline. */
2964 end = strchr(buf, '\n');
2968 cnt = mark_printk("%s\n", buf);
2975 static struct file_operations tracing_max_lat_fops = {
2976 .open = tracing_open_generic,
2977 .read = tracing_max_lat_read,
2978 .write = tracing_max_lat_write,
2981 static struct file_operations tracing_ctrl_fops = {
2982 .open = tracing_open_generic,
2983 .read = tracing_ctrl_read,
2984 .write = tracing_ctrl_write,
2987 static struct file_operations set_tracer_fops = {
2988 .open = tracing_open_generic,
2989 .read = tracing_set_trace_read,
2990 .write = tracing_set_trace_write,
2993 static struct file_operations tracing_pipe_fops = {
2994 .open = tracing_open_pipe,
2995 .poll = tracing_poll_pipe,
2996 .read = tracing_read_pipe,
2997 .splice_read = tracing_splice_read_pipe,
2998 .release = tracing_release_pipe,
3001 static struct file_operations tracing_entries_fops = {
3002 .open = tracing_open_generic,
3003 .read = tracing_entries_read,
3004 .write = tracing_entries_write,
3007 static struct file_operations tracing_mark_fops = {
3008 .open = tracing_open_generic,
3009 .write = tracing_mark_write,
3012 struct ftrace_buffer_info {
3013 struct trace_array *tr;
3019 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3021 int cpu = (int)(long)inode->i_private;
3022 struct ftrace_buffer_info *info;
3024 if (tracing_disabled)
3027 info = kzalloc(sizeof(*info), GFP_KERNEL);
3031 info->tr = &global_trace;
3033 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3034 /* Force reading ring buffer for first read */
3035 info->read = (unsigned int)-1;
3039 filp->private_data = info;
3049 tracing_buffers_read(struct file *filp, char __user *ubuf,
3050 size_t count, loff_t *ppos)
3052 struct ftrace_buffer_info *info = filp->private_data;
3060 /* Do we have previous read data to read? */
3061 if (info->read < PAGE_SIZE)
3066 ret = ring_buffer_read_page(info->tr->buffer,
3073 pos = ring_buffer_page_len(info->spare);
3075 if (pos < PAGE_SIZE)
3076 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3079 size = PAGE_SIZE - info->read;
3083 ret = copy_to_user(ubuf, info->spare + info->read, size);
3094 static int tracing_buffers_release(struct inode *inode, struct file *file)
3096 struct ftrace_buffer_info *info = file->private_data;
3098 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3105 struct ring_buffer *buffer;
3110 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3111 struct pipe_buffer *buf)
3113 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3118 ring_buffer_free_read_page(ref->buffer, ref->page);
3123 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3124 struct pipe_buffer *buf)
3129 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3130 struct pipe_buffer *buf)
3132 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3137 /* Pipe buffer operations for a buffer. */
3138 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3140 .map = generic_pipe_buf_map,
3141 .unmap = generic_pipe_buf_unmap,
3142 .confirm = generic_pipe_buf_confirm,
3143 .release = buffer_pipe_buf_release,
3144 .steal = buffer_pipe_buf_steal,
3145 .get = buffer_pipe_buf_get,
3149 * Callback from splice_to_pipe(), if we need to release some pages
3150 * at the end of the spd in case we error'ed out in filling the pipe.
3152 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3154 struct buffer_ref *ref =
3155 (struct buffer_ref *)spd->partial[i].private;
3160 ring_buffer_free_read_page(ref->buffer, ref->page);
3162 spd->partial[i].private = 0;
3166 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3167 struct pipe_inode_info *pipe, size_t len,
3170 struct ftrace_buffer_info *info = file->private_data;
3171 struct partial_page partial[PIPE_BUFFERS];
3172 struct page *pages[PIPE_BUFFERS];
3173 struct splice_pipe_desc spd = {
3177 .ops = &buffer_pipe_buf_ops,
3178 .spd_release = buffer_spd_release,
3180 struct buffer_ref *ref;
3185 * We can't seek on a buffer input
3187 if (unlikely(*ppos))
3191 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3195 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3199 ref->buffer = info->tr->buffer;
3200 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3206 r = ring_buffer_read_page(ref->buffer, &ref->page,
3209 ring_buffer_free_read_page(ref->buffer,
3216 * zero out any left over data, this is going to
3219 size = ring_buffer_page_len(ref->page);
3220 if (size < PAGE_SIZE)
3221 memset(ref->page + size, 0, PAGE_SIZE - size);
3223 page = virt_to_page(ref->page);
3225 spd.pages[i] = page;
3226 spd.partial[i].len = PAGE_SIZE;
3227 spd.partial[i].offset = 0;
3228 spd.partial[i].private = (unsigned long)ref;
3234 /* did we read anything? */
3235 if (!spd.nr_pages) {
3236 if (flags & SPLICE_F_NONBLOCK)
3244 ret = splice_to_pipe(pipe, &spd);
3249 static const struct file_operations tracing_buffers_fops = {
3250 .open = tracing_buffers_open,
3251 .read = tracing_buffers_read,
3252 .release = tracing_buffers_release,
3253 .splice_read = tracing_buffers_splice_read,
3254 .llseek = no_llseek,
3257 #ifdef CONFIG_DYNAMIC_FTRACE
3259 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3265 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3266 size_t cnt, loff_t *ppos)
3268 static char ftrace_dyn_info_buffer[1024];
3269 static DEFINE_MUTEX(dyn_info_mutex);
3270 unsigned long *p = filp->private_data;
3271 char *buf = ftrace_dyn_info_buffer;
3272 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3275 mutex_lock(&dyn_info_mutex);
3276 r = sprintf(buf, "%ld ", *p);
3278 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3281 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3283 mutex_unlock(&dyn_info_mutex);
3288 static struct file_operations tracing_dyn_info_fops = {
3289 .open = tracing_open_generic,
3290 .read = tracing_read_dyn_info,
3294 static struct dentry *d_tracer;
3296 struct dentry *tracing_init_dentry(void)
3303 d_tracer = debugfs_create_dir("tracing", NULL);
3305 if (!d_tracer && !once) {
3307 pr_warning("Could not create debugfs directory 'tracing'\n");
3314 static struct dentry *d_percpu;
3316 struct dentry *tracing_dentry_percpu(void)
3319 struct dentry *d_tracer;
3324 d_tracer = tracing_init_dentry();
3329 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3331 if (!d_percpu && !once) {
3333 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3340 static void tracing_init_debugfs_percpu(long cpu)
3342 struct dentry *d_percpu = tracing_dentry_percpu();
3343 struct dentry *entry, *d_cpu;
3344 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3347 if (cpu > 999 || cpu < 0)
3350 sprintf(cpu_dir, "cpu%ld", cpu);
3351 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3353 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3357 /* per cpu trace_pipe */
3358 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
3359 (void *) cpu, &tracing_pipe_fops);
3361 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3364 entry = debugfs_create_file("trace", 0444, d_cpu,
3365 (void *) cpu, &tracing_fops);
3367 pr_warning("Could not create debugfs 'trace' entry\n");
3370 #ifdef CONFIG_FTRACE_SELFTEST
3371 /* Let selftest have access to static functions in this file */
3372 #include "trace_selftest.c"
3375 struct trace_option_dentry {
3376 struct tracer_opt *opt;
3377 struct tracer_flags *flags;
3378 struct dentry *entry;
3382 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3385 struct trace_option_dentry *topt = filp->private_data;
3388 if (topt->flags->val & topt->opt->bit)
3393 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3397 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3400 struct trace_option_dentry *topt = filp->private_data;
3405 if (cnt >= sizeof(buf))
3408 if (copy_from_user(&buf, ubuf, cnt))
3413 ret = strict_strtoul(buf, 10, &val);
3420 /* do nothing if already cleared */
3421 if (!(topt->flags->val & topt->opt->bit))
3424 mutex_lock(&trace_types_lock);
3425 if (current_trace->set_flag)
3426 ret = current_trace->set_flag(topt->flags->val,
3428 mutex_unlock(&trace_types_lock);
3431 topt->flags->val &= ~topt->opt->bit;
3434 /* do nothing if already set */
3435 if (topt->flags->val & topt->opt->bit)
3438 mutex_lock(&trace_types_lock);
3439 if (current_trace->set_flag)
3440 ret = current_trace->set_flag(topt->flags->val,
3442 mutex_unlock(&trace_types_lock);
3445 topt->flags->val |= topt->opt->bit;
3458 static const struct file_operations trace_options_fops = {
3459 .open = tracing_open_generic,
3460 .read = trace_options_read,
3461 .write = trace_options_write,
3465 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3468 long index = (long)filp->private_data;
3471 if (trace_flags & (1 << index))
3476 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3480 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3483 long index = (long)filp->private_data;
3488 if (cnt >= sizeof(buf))
3491 if (copy_from_user(&buf, ubuf, cnt))
3496 ret = strict_strtoul(buf, 10, &val);
3502 trace_flags &= ~(1 << index);
3505 trace_flags |= 1 << index;
3517 static const struct file_operations trace_options_core_fops = {
3518 .open = tracing_open_generic,
3519 .read = trace_options_core_read,
3520 .write = trace_options_core_write,
3523 static struct dentry *trace_options_init_dentry(void)
3525 struct dentry *d_tracer;
3526 static struct dentry *t_options;
3531 d_tracer = tracing_init_dentry();
3535 t_options = debugfs_create_dir("options", d_tracer);
3537 pr_warning("Could not create debugfs directory 'options'\n");
3545 create_trace_option_file(struct trace_option_dentry *topt,
3546 struct tracer_flags *flags,
3547 struct tracer_opt *opt)
3549 struct dentry *t_options;
3550 struct dentry *entry;
3552 t_options = trace_options_init_dentry();
3556 topt->flags = flags;
3559 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3560 &trace_options_fops);
3562 topt->entry = entry;
3566 static struct trace_option_dentry *
3567 create_trace_option_files(struct tracer *tracer)
3569 struct trace_option_dentry *topts;
3570 struct tracer_flags *flags;
3571 struct tracer_opt *opts;
3577 flags = tracer->flags;
3579 if (!flags || !flags->opts)
3584 for (cnt = 0; opts[cnt].name; cnt++)
3587 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3591 for (cnt = 0; opts[cnt].name; cnt++)
3592 create_trace_option_file(&topts[cnt], flags,
3599 destroy_trace_option_files(struct trace_option_dentry *topts)
3606 for (cnt = 0; topts[cnt].opt; cnt++) {
3607 if (topts[cnt].entry)
3608 debugfs_remove(topts[cnt].entry);
3614 static struct dentry *
3615 create_trace_option_core_file(const char *option, long index)
3617 struct dentry *t_options;
3618 struct dentry *entry;
3620 t_options = trace_options_init_dentry();
3624 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3625 &trace_options_core_fops);
3630 static __init void create_trace_options_dir(void)
3632 struct dentry *t_options;
3633 struct dentry *entry;
3636 t_options = trace_options_init_dentry();
3640 for (i = 0; trace_options[i]; i++) {
3641 entry = create_trace_option_core_file(trace_options[i], i);
3643 pr_warning("Could not create debugfs %s entry\n",
3648 static __init int tracer_init_debugfs(void)
3650 struct dentry *d_tracer;
3651 struct dentry *buffers;
3652 struct dentry *entry;
3655 d_tracer = tracing_init_dentry();
3657 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3658 &global_trace, &tracing_ctrl_fops);
3660 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3662 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3663 NULL, &tracing_iter_fops);
3665 pr_warning("Could not create debugfs 'trace_options' entry\n");
3667 create_trace_options_dir();
3669 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3670 NULL, &tracing_cpumask_fops);
3672 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3674 entry = debugfs_create_file("trace", 0444, d_tracer,
3675 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3677 pr_warning("Could not create debugfs 'trace' entry\n");
3679 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3680 &global_trace, &show_traces_fops);
3682 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3684 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3685 &global_trace, &set_tracer_fops);
3687 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3689 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3690 &tracing_max_latency,
3691 &tracing_max_lat_fops);
3693 pr_warning("Could not create debugfs "
3694 "'tracing_max_latency' entry\n");
3696 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3697 &tracing_thresh, &tracing_max_lat_fops);
3699 pr_warning("Could not create debugfs "
3700 "'tracing_thresh' entry\n");
3701 entry = debugfs_create_file("README", 0644, d_tracer,
3702 NULL, &tracing_readme_fops);
3704 pr_warning("Could not create debugfs 'README' entry\n");
3706 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3707 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3709 pr_warning("Could not create debugfs "
3710 "'trace_pipe' entry\n");
3712 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3713 &global_trace, &tracing_entries_fops);
3715 pr_warning("Could not create debugfs "
3716 "'buffer_size_kb' entry\n");
3718 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3719 NULL, &tracing_mark_fops);
3721 pr_warning("Could not create debugfs "
3722 "'trace_marker' entry\n");
3724 buffers = debugfs_create_dir("binary_buffers", d_tracer);
3727 pr_warning("Could not create buffers directory\n");
3732 for_each_tracing_cpu(cpu) {
3733 sprintf(buf, "%d", cpu);
3735 entry = debugfs_create_file(buf, 0444, buffers,
3737 &tracing_buffers_fops);
3739 pr_warning("Could not create debugfs buffers "
3740 "'%s' entry\n", buf);
3744 #ifdef CONFIG_DYNAMIC_FTRACE
3745 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3746 &ftrace_update_tot_cnt,
3747 &tracing_dyn_info_fops);
3749 pr_warning("Could not create debugfs "
3750 "'dyn_ftrace_total_info' entry\n");
3752 #ifdef CONFIG_SYSPROF_TRACER
3753 init_tracer_sysprof_debugfs(d_tracer);
3756 for_each_tracing_cpu(cpu)
3757 tracing_init_debugfs_percpu(cpu);
3762 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
3764 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
3765 static char trace_buf[TRACE_BUF_SIZE];
3767 struct ring_buffer_event *event;
3768 struct trace_array *tr = &global_trace;
3769 struct trace_array_cpu *data;
3770 int cpu, len = 0, size, pc;
3771 struct print_entry *entry;
3772 unsigned long irq_flags;
3774 if (tracing_disabled || tracing_selftest_running)
3777 pc = preempt_count();
3778 preempt_disable_notrace();
3779 cpu = raw_smp_processor_id();
3780 data = tr->data[cpu];
3782 if (unlikely(atomic_read(&data->disabled)))
3785 pause_graph_tracing();
3786 raw_local_irq_save(irq_flags);
3787 __raw_spin_lock(&trace_buf_lock);
3788 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3790 len = min(len, TRACE_BUF_SIZE-1);
3793 size = sizeof(*entry) + len + 1;
3794 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
3797 entry = ring_buffer_event_data(event);
3799 entry->depth = depth;
3801 memcpy(&entry->buf, trace_buf, len);
3802 entry->buf[len] = 0;
3803 ring_buffer_unlock_commit(tr->buffer, event);
3806 __raw_spin_unlock(&trace_buf_lock);
3807 raw_local_irq_restore(irq_flags);
3808 unpause_graph_tracing();
3810 preempt_enable_notrace();
3814 EXPORT_SYMBOL_GPL(trace_vprintk);
3816 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3821 if (!(trace_flags & TRACE_ITER_PRINTK))
3825 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3829 EXPORT_SYMBOL_GPL(__ftrace_printk);
3831 int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
3833 if (!(trace_flags & TRACE_ITER_PRINTK))
3836 return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3838 EXPORT_SYMBOL_GPL(__ftrace_vprintk);
3840 static int trace_panic_handler(struct notifier_block *this,
3841 unsigned long event, void *unused)
3843 if (ftrace_dump_on_oops)
3848 static struct notifier_block trace_panic_notifier = {
3849 .notifier_call = trace_panic_handler,
3851 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3854 static int trace_die_handler(struct notifier_block *self,
3860 if (ftrace_dump_on_oops)
3869 static struct notifier_block trace_die_notifier = {
3870 .notifier_call = trace_die_handler,
3875 * printk is set to max of 1024, we really don't need it that big.
3876 * Nothing should be printing 1000 characters anyway.
3878 #define TRACE_MAX_PRINT 1000
3881 * Define here KERN_TRACE so that we have one place to modify
3882 * it if we decide to change what log level the ftrace dump
3885 #define KERN_TRACE KERN_EMERG
3888 trace_printk_seq(struct trace_seq *s)
3890 /* Probably should print a warning here. */
3894 /* should be zero ended, but we are paranoid. */
3895 s->buffer[s->len] = 0;
3897 printk(KERN_TRACE "%s", s->buffer);
3902 void ftrace_dump(void)
3904 static DEFINE_SPINLOCK(ftrace_dump_lock);
3905 /* use static because iter can be a bit big for the stack */
3906 static struct trace_iterator iter;
3907 static int dump_ran;
3908 unsigned long flags;
3912 spin_lock_irqsave(&ftrace_dump_lock, flags);
3918 /* No turning back! */
3922 for_each_tracing_cpu(cpu) {
3923 atomic_inc(&global_trace.data[cpu]->disabled);
3926 /* don't look at user memory in panic mode */
3927 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3929 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3931 /* Simulate the iterator */
3932 iter.tr = &global_trace;
3933 iter.trace = current_trace;
3934 iter.cpu_file = TRACE_PIPE_ALL_CPU;
3937 * We need to stop all tracing on all CPUS to read the
3938 * the next buffer. This is a bit expensive, but is
3939 * not done often. We fill all what we can read,
3940 * and then release the locks again.
3943 while (!trace_empty(&iter)) {
3946 printk(KERN_TRACE "---------------------------------\n");
3950 /* reset all but tr, trace, and overruns */
3951 memset(&iter.seq, 0,
3952 sizeof(struct trace_iterator) -
3953 offsetof(struct trace_iterator, seq));
3954 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3957 if (find_next_entry_inc(&iter) != NULL) {
3958 print_trace_line(&iter);
3959 trace_consume(&iter);
3962 trace_printk_seq(&iter.seq);
3966 printk(KERN_TRACE " (ftrace buffer empty)\n");
3968 printk(KERN_TRACE "---------------------------------\n");
3971 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3974 __init static int tracer_alloc_buffers(void)
3976 struct trace_array_cpu *data;
3980 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3983 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3984 goto out_free_buffer_mask;
3986 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
3987 goto out_free_tracing_cpumask;
3989 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3990 cpumask_copy(tracing_cpumask, cpu_all_mask);
3991 cpumask_clear(tracing_reader_cpumask);
3993 /* TODO: make the number of buffers hot pluggable with CPUS */
3994 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3995 TRACE_BUFFER_FLAGS);
3996 if (!global_trace.buffer) {
3997 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3999 goto out_free_cpumask;
4001 global_trace.entries = ring_buffer_size(global_trace.buffer);
4004 #ifdef CONFIG_TRACER_MAX_TRACE
4005 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
4006 TRACE_BUFFER_FLAGS);
4007 if (!max_tr.buffer) {
4008 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4010 ring_buffer_free(global_trace.buffer);
4011 goto out_free_cpumask;
4013 max_tr.entries = ring_buffer_size(max_tr.buffer);
4014 WARN_ON(max_tr.entries != global_trace.entries);
4017 /* Allocate the first page for all buffers */
4018 for_each_tracing_cpu(i) {
4019 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4020 max_tr.data[i] = &per_cpu(max_data, i);
4023 trace_init_cmdlines();
4025 register_tracer(&nop_trace);
4026 current_trace = &nop_trace;
4027 #ifdef CONFIG_BOOT_TRACER
4028 register_tracer(&boot_tracer);
4030 /* All seems OK, enable tracing */
4031 tracing_disabled = 0;
4033 atomic_notifier_chain_register(&panic_notifier_list,
4034 &trace_panic_notifier);
4036 register_die_notifier(&trace_die_notifier);
4040 free_cpumask_var(tracing_reader_cpumask);
4041 out_free_tracing_cpumask:
4042 free_cpumask_var(tracing_cpumask);
4043 out_free_buffer_mask:
4044 free_cpumask_var(tracing_buffer_mask);
4049 __init static int clear_boot_tracer(void)
4052 * The default tracer at boot buffer is an init section.
4053 * This function is called in lateinit. If we did not
4054 * find the boot tracer, then clear it out, to prevent
4055 * later registration from accessing the buffer that is
4056 * about to be freed.
4058 if (!default_bootup_tracer)
4061 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4062 default_bootup_tracer);
4063 default_bootup_tracer = NULL;
4068 early_initcall(tracer_alloc_buffers);
4069 fs_initcall(tracer_init_debugfs);
4070 late_initcall(clear_boot_tracer);