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 * On boot up, the ring buffer is set to the minimum size, so that
49 * we do not waste memory on systems that are not using tracing.
51 static int ring_buffer_expanded;
54 * We need to change this state when a selftest is running.
55 * A selftest will lurk into the ring-buffer to count the
56 * entries inserted during the selftest although some concurrent
57 * insertions into the ring-buffer such as trace_printk could occurred
58 * at the same time, giving false positive or negative results.
60 static bool __read_mostly tracing_selftest_running;
63 * If a tracer is running, we do not want to run SELFTEST.
65 static bool __read_mostly tracing_selftest_disabled;
67 /* For tracers that don't implement custom flags */
68 static struct tracer_opt dummy_tracer_opt[] = {
72 static struct tracer_flags dummy_tracer_flags = {
74 .opts = dummy_tracer_opt
77 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
83 * Kill all tracing for good (never come back).
84 * It is initialized to 1 but will turn to zero if the initialization
85 * of the tracer is successful. But that is the only place that sets
88 static int tracing_disabled = 1;
90 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
92 static inline void ftrace_disable_cpu(void)
95 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
98 static inline void ftrace_enable_cpu(void)
100 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
104 static cpumask_var_t __read_mostly tracing_buffer_mask;
106 /* Define which cpu buffers are currently read in trace_pipe */
107 static cpumask_var_t tracing_reader_cpumask;
109 #define for_each_tracing_cpu(cpu) \
110 for_each_cpu(cpu, tracing_buffer_mask)
113 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
115 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
116 * is set, then ftrace_dump is called. This will output the contents
117 * of the ftrace buffers to the console. This is very useful for
118 * capturing traces that lead to crashes and outputing it to a
121 * It is default off, but you can enable it with either specifying
122 * "ftrace_dump_on_oops" in the kernel command line, or setting
123 * /proc/sys/kernel/ftrace_dump_on_oops to true.
125 int ftrace_dump_on_oops;
127 static int tracing_set_tracer(const char *buf);
129 #define BOOTUP_TRACER_SIZE 100
130 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
131 static char *default_bootup_tracer;
133 static int __init set_ftrace(char *str)
135 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
136 default_bootup_tracer = bootup_tracer_buf;
137 /* We are using ftrace early, expand it */
138 ring_buffer_expanded = 1;
141 __setup("ftrace=", set_ftrace);
143 static int __init set_ftrace_dump_on_oops(char *str)
145 ftrace_dump_on_oops = 1;
148 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
151 ns2usecs(cycle_t nsec)
159 * The global_trace is the descriptor that holds the tracing
160 * buffers for the live tracing. For each CPU, it contains
161 * a link list of pages that will store trace entries. The
162 * page descriptor of the pages in the memory is used to hold
163 * the link list by linking the lru item in the page descriptor
164 * to each of the pages in the buffer per CPU.
166 * For each active CPU there is a data field that holds the
167 * pages for the buffer for that CPU. Each CPU has the same number
168 * of pages allocated for its buffer.
170 static struct trace_array global_trace;
172 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
174 cycle_t ftrace_now(int cpu)
178 /* Early boot up does not have a buffer yet */
179 if (!global_trace.buffer)
180 return trace_clock_local();
182 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
183 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
189 * The max_tr is used to snapshot the global_trace when a maximum
190 * latency is reached. Some tracers will use this to store a maximum
191 * trace while it continues examining live traces.
193 * The buffers for the max_tr are set up the same as the global_trace.
194 * When a snapshot is taken, the link list of the max_tr is swapped
195 * with the link list of the global_trace and the buffers are reset for
196 * the global_trace so the tracing can continue.
198 static struct trace_array max_tr;
200 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
202 /* tracer_enabled is used to toggle activation of a tracer */
203 static int tracer_enabled = 1;
206 * tracing_is_enabled - return tracer_enabled status
208 * This function is used by other tracers to know the status
209 * of the tracer_enabled flag. Tracers may use this function
210 * to know if it should enable their features when starting
211 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
213 int tracing_is_enabled(void)
215 return tracer_enabled;
219 * trace_buf_size is the size in bytes that is allocated
220 * for a buffer. Note, the number of bytes is always rounded
223 * This number is purposely set to a low number of 16384.
224 * If the dump on oops happens, it will be much appreciated
225 * to not have to wait for all that output. Anyway this can be
226 * boot time and run time configurable.
228 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
230 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
232 /* trace_types holds a link list of available tracers. */
233 static struct tracer *trace_types __read_mostly;
235 /* current_trace points to the tracer that is currently active */
236 static struct tracer *current_trace __read_mostly;
239 * max_tracer_type_len is used to simplify the allocating of
240 * buffers to read userspace tracer names. We keep track of
241 * the longest tracer name registered.
243 static int max_tracer_type_len;
246 * trace_types_lock is used to protect the trace_types list.
247 * This lock is also used to keep user access serialized.
248 * Accesses from userspace will grab this lock while userspace
249 * activities happen inside the kernel.
251 static DEFINE_MUTEX(trace_types_lock);
253 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
254 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
256 /* trace_flags holds trace_options default values */
257 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
258 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME;
261 * trace_wake_up - wake up tasks waiting for trace input
263 * Simply wakes up any task that is blocked on the trace_wait
264 * queue. These is used with trace_poll for tasks polling the trace.
266 void trace_wake_up(void)
269 * The runqueue_is_locked() can fail, but this is the best we
272 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
273 wake_up(&trace_wait);
276 static int __init set_buf_size(char *str)
278 unsigned long buf_size;
283 ret = strict_strtoul(str, 0, &buf_size);
284 /* nr_entries can not be zero */
285 if (ret < 0 || buf_size == 0)
287 trace_buf_size = buf_size;
290 __setup("trace_buf_size=", set_buf_size);
292 unsigned long nsecs_to_usecs(unsigned long nsecs)
297 /* These must match the bit postions in trace_iterator_flags */
298 static const char *trace_options[] = {
324 * ftrace_max_lock is used to protect the swapping of buffers
325 * when taking a max snapshot. The buffers themselves are
326 * protected by per_cpu spinlocks. But the action of the swap
327 * needs its own lock.
329 * This is defined as a raw_spinlock_t in order to help
330 * with performance when lockdep debugging is enabled.
332 static raw_spinlock_t ftrace_max_lock =
333 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
336 * Copy the new maximum trace into the separate maximum-trace
337 * structure. (this way the maximum trace is permanently saved,
338 * for later retrieval via /debugfs/tracing/latency_trace)
341 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
343 struct trace_array_cpu *data = tr->data[cpu];
346 max_tr.time_start = data->preempt_timestamp;
348 data = max_tr.data[cpu];
349 data->saved_latency = tracing_max_latency;
351 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
352 data->pid = tsk->pid;
353 data->uid = task_uid(tsk);
354 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
355 data->policy = tsk->policy;
356 data->rt_priority = tsk->rt_priority;
358 /* record this tasks comm */
359 tracing_record_cmdline(tsk);
362 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
370 if (s->len <= s->readpos)
373 len = s->len - s->readpos;
376 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
386 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
391 if (s->len <= s->readpos)
394 len = s->len - s->readpos;
397 ret = memcpy(buf, s->buffer + s->readpos, cnt);
406 trace_print_seq(struct seq_file *m, struct trace_seq *s)
408 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
411 seq_puts(m, s->buffer);
417 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
419 * @tsk: the task with the latency
420 * @cpu: The cpu that initiated the trace.
422 * Flip the buffers between the @tr and the max_tr and record information
423 * about which task was the cause of this latency.
426 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
428 struct ring_buffer *buf = tr->buffer;
430 WARN_ON_ONCE(!irqs_disabled());
431 __raw_spin_lock(&ftrace_max_lock);
433 tr->buffer = max_tr.buffer;
436 ftrace_disable_cpu();
437 ring_buffer_reset(tr->buffer);
440 __update_max_tr(tr, tsk, cpu);
441 __raw_spin_unlock(&ftrace_max_lock);
445 * update_max_tr_single - only copy one trace over, and reset the rest
447 * @tsk - task with the latency
448 * @cpu - the cpu of the buffer to copy.
450 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
453 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
457 WARN_ON_ONCE(!irqs_disabled());
458 __raw_spin_lock(&ftrace_max_lock);
460 ftrace_disable_cpu();
462 ring_buffer_reset(max_tr.buffer);
463 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
467 WARN_ON_ONCE(ret && ret != -EAGAIN);
469 __update_max_tr(tr, tsk, cpu);
470 __raw_spin_unlock(&ftrace_max_lock);
474 * register_tracer - register a tracer with the ftrace system.
475 * @type - the plugin for the tracer
477 * Register a new plugin tracer.
479 int register_tracer(struct tracer *type)
480 __releases(kernel_lock)
481 __acquires(kernel_lock)
488 pr_info("Tracer must have a name\n");
493 * When this gets called we hold the BKL which means that
494 * preemption is disabled. Various trace selftests however
495 * need to disable and enable preemption for successful tests.
496 * So we drop the BKL here and grab it after the tests again.
499 mutex_lock(&trace_types_lock);
501 tracing_selftest_running = true;
503 for (t = trace_types; t; t = t->next) {
504 if (strcmp(type->name, t->name) == 0) {
506 pr_info("Trace %s already registered\n",
514 type->set_flag = &dummy_set_flag;
516 type->flags = &dummy_tracer_flags;
518 if (!type->flags->opts)
519 type->flags->opts = dummy_tracer_opt;
520 if (!type->wait_pipe)
521 type->wait_pipe = default_wait_pipe;
524 #ifdef CONFIG_FTRACE_STARTUP_TEST
525 if (type->selftest && !tracing_selftest_disabled) {
526 struct tracer *saved_tracer = current_trace;
527 struct trace_array *tr = &global_trace;
531 * Run a selftest on this tracer.
532 * Here we reset the trace buffer, and set the current
533 * tracer to be this tracer. The tracer can then run some
534 * internal tracing to verify that everything is in order.
535 * If we fail, we do not register this tracer.
537 for_each_tracing_cpu(i)
538 tracing_reset(tr, i);
540 current_trace = type;
541 /* the test is responsible for initializing and enabling */
542 pr_info("Testing tracer %s: ", type->name);
543 ret = type->selftest(type, tr);
544 /* the test is responsible for resetting too */
545 current_trace = saved_tracer;
547 printk(KERN_CONT "FAILED!\n");
550 /* Only reset on passing, to avoid touching corrupted buffers */
551 for_each_tracing_cpu(i)
552 tracing_reset(tr, i);
554 printk(KERN_CONT "PASSED\n");
558 type->next = trace_types;
560 len = strlen(type->name);
561 if (len > max_tracer_type_len)
562 max_tracer_type_len = len;
565 tracing_selftest_running = false;
566 mutex_unlock(&trace_types_lock);
568 if (ret || !default_bootup_tracer)
571 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
574 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
575 /* Do we want this tracer to start on bootup? */
576 tracing_set_tracer(type->name);
577 default_bootup_tracer = NULL;
578 /* disable other selftests, since this will break it. */
579 tracing_selftest_disabled = 1;
580 #ifdef CONFIG_FTRACE_STARTUP_TEST
581 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
590 void unregister_tracer(struct tracer *type)
595 mutex_lock(&trace_types_lock);
596 for (t = &trace_types; *t; t = &(*t)->next) {
600 pr_info("Trace %s not registered\n", type->name);
606 if (type == current_trace && tracer_enabled) {
609 if (current_trace->stop)
610 current_trace->stop(&global_trace);
611 current_trace = &nop_trace;
614 if (strlen(type->name) != max_tracer_type_len)
617 max_tracer_type_len = 0;
618 for (t = &trace_types; *t; t = &(*t)->next) {
619 len = strlen((*t)->name);
620 if (len > max_tracer_type_len)
621 max_tracer_type_len = len;
624 mutex_unlock(&trace_types_lock);
627 void tracing_reset(struct trace_array *tr, int cpu)
629 ftrace_disable_cpu();
630 ring_buffer_reset_cpu(tr->buffer, cpu);
634 void tracing_reset_online_cpus(struct trace_array *tr)
638 tr->time_start = ftrace_now(tr->cpu);
640 for_each_online_cpu(cpu)
641 tracing_reset(tr, cpu);
644 #define SAVED_CMDLINES 128
645 #define NO_CMDLINE_MAP UINT_MAX
646 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
647 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
648 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
649 static int cmdline_idx;
650 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
652 /* temporary disable recording */
653 static atomic_t trace_record_cmdline_disabled __read_mostly;
655 static void trace_init_cmdlines(void)
657 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
658 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
662 static int trace_stop_count;
663 static DEFINE_SPINLOCK(tracing_start_lock);
666 * ftrace_off_permanent - disable all ftrace code permanently
668 * This should only be called when a serious anomally has
669 * been detected. This will turn off the function tracing,
670 * ring buffers, and other tracing utilites. It takes no
671 * locks and can be called from any context.
673 void ftrace_off_permanent(void)
675 tracing_disabled = 1;
677 tracing_off_permanent();
681 * tracing_start - quick start of the tracer
683 * If tracing is enabled but was stopped by tracing_stop,
684 * this will start the tracer back up.
686 void tracing_start(void)
688 struct ring_buffer *buffer;
691 if (tracing_disabled)
694 spin_lock_irqsave(&tracing_start_lock, flags);
695 if (--trace_stop_count) {
696 if (trace_stop_count < 0) {
697 /* Someone screwed up their debugging */
699 trace_stop_count = 0;
705 buffer = global_trace.buffer;
707 ring_buffer_record_enable(buffer);
709 buffer = max_tr.buffer;
711 ring_buffer_record_enable(buffer);
715 spin_unlock_irqrestore(&tracing_start_lock, flags);
719 * tracing_stop - quick stop of the tracer
721 * Light weight way to stop tracing. Use in conjunction with
724 void tracing_stop(void)
726 struct ring_buffer *buffer;
730 spin_lock_irqsave(&tracing_start_lock, flags);
731 if (trace_stop_count++)
734 buffer = global_trace.buffer;
736 ring_buffer_record_disable(buffer);
738 buffer = max_tr.buffer;
740 ring_buffer_record_disable(buffer);
743 spin_unlock_irqrestore(&tracing_start_lock, flags);
746 void trace_stop_cmdline_recording(void);
748 static void trace_save_cmdline(struct task_struct *tsk)
752 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
756 * It's not the end of the world if we don't get
757 * the lock, but we also don't want to spin
758 * nor do we want to disable interrupts,
759 * so if we miss here, then better luck next time.
761 if (!__raw_spin_trylock(&trace_cmdline_lock))
764 idx = map_pid_to_cmdline[tsk->pid];
765 if (idx == NO_CMDLINE_MAP) {
766 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
769 * Check whether the cmdline buffer at idx has a pid
770 * mapped. We are going to overwrite that entry so we
771 * need to clear the map_pid_to_cmdline. Otherwise we
772 * would read the new comm for the old pid.
774 pid = map_cmdline_to_pid[idx];
775 if (pid != NO_CMDLINE_MAP)
776 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
778 map_cmdline_to_pid[idx] = tsk->pid;
779 map_pid_to_cmdline[tsk->pid] = idx;
784 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
786 __raw_spin_unlock(&trace_cmdline_lock);
789 void trace_find_cmdline(int pid, char comm[])
794 strcpy(comm, "<idle>");
798 if (pid > PID_MAX_DEFAULT) {
799 strcpy(comm, "<...>");
803 __raw_spin_lock(&trace_cmdline_lock);
804 map = map_pid_to_cmdline[pid];
805 if (map != NO_CMDLINE_MAP)
806 strcpy(comm, saved_cmdlines[map]);
808 strcpy(comm, "<...>");
810 __raw_spin_unlock(&trace_cmdline_lock);
813 void tracing_record_cmdline(struct task_struct *tsk)
815 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
819 trace_save_cmdline(tsk);
823 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
826 struct task_struct *tsk = current;
828 entry->preempt_count = pc & 0xff;
829 entry->pid = (tsk) ? tsk->pid : 0;
830 entry->tgid = (tsk) ? tsk->tgid : 0;
832 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
833 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
835 TRACE_FLAG_IRQS_NOSUPPORT |
837 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
838 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
839 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
842 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
845 unsigned long flags, int pc)
847 struct ring_buffer_event *event;
849 event = ring_buffer_lock_reserve(tr->buffer, len);
851 struct trace_entry *ent = ring_buffer_event_data(event);
853 tracing_generic_entry_update(ent, flags, pc);
859 static void ftrace_trace_stack(struct trace_array *tr,
860 unsigned long flags, int skip, int pc);
861 static void ftrace_trace_userstack(struct trace_array *tr,
862 unsigned long flags, int pc);
864 void trace_buffer_unlock_commit(struct trace_array *tr,
865 struct ring_buffer_event *event,
866 unsigned long flags, int pc)
868 ring_buffer_unlock_commit(tr->buffer, event);
870 ftrace_trace_stack(tr, flags, 6, pc);
871 ftrace_trace_userstack(tr, flags, pc);
875 struct ring_buffer_event *
876 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
877 unsigned long flags, int pc)
879 return trace_buffer_lock_reserve(&global_trace,
880 type, len, flags, pc);
883 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
884 unsigned long flags, int pc)
886 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
890 trace_function(struct trace_array *tr,
891 unsigned long ip, unsigned long parent_ip, unsigned long flags,
894 struct ring_buffer_event *event;
895 struct ftrace_entry *entry;
897 /* If we are reading the ring buffer, don't trace */
898 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
901 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
905 entry = ring_buffer_event_data(event);
907 entry->parent_ip = parent_ip;
908 ring_buffer_unlock_commit(tr->buffer, event);
911 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
912 static void __trace_graph_entry(struct trace_array *tr,
913 struct ftrace_graph_ent *trace,
917 struct ring_buffer_event *event;
918 struct ftrace_graph_ent_entry *entry;
920 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
923 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
924 sizeof(*entry), flags, pc);
927 entry = ring_buffer_event_data(event);
928 entry->graph_ent = *trace;
929 ring_buffer_unlock_commit(global_trace.buffer, event);
932 static void __trace_graph_return(struct trace_array *tr,
933 struct ftrace_graph_ret *trace,
937 struct ring_buffer_event *event;
938 struct ftrace_graph_ret_entry *entry;
940 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
943 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
944 sizeof(*entry), flags, pc);
947 entry = ring_buffer_event_data(event);
949 ring_buffer_unlock_commit(global_trace.buffer, event);
954 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
955 unsigned long ip, unsigned long parent_ip, unsigned long flags,
958 if (likely(!atomic_read(&data->disabled)))
959 trace_function(tr, ip, parent_ip, flags, pc);
962 static void __ftrace_trace_stack(struct trace_array *tr,
966 #ifdef CONFIG_STACKTRACE
967 struct ring_buffer_event *event;
968 struct stack_entry *entry;
969 struct stack_trace trace;
971 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
972 sizeof(*entry), flags, pc);
975 entry = ring_buffer_event_data(event);
976 memset(&entry->caller, 0, sizeof(entry->caller));
978 trace.nr_entries = 0;
979 trace.max_entries = FTRACE_STACK_ENTRIES;
981 trace.entries = entry->caller;
983 save_stack_trace(&trace);
984 ring_buffer_unlock_commit(tr->buffer, event);
988 static void ftrace_trace_stack(struct trace_array *tr,
992 if (!(trace_flags & TRACE_ITER_STACKTRACE))
995 __ftrace_trace_stack(tr, flags, skip, pc);
998 void __trace_stack(struct trace_array *tr,
1002 __ftrace_trace_stack(tr, flags, skip, pc);
1005 static void ftrace_trace_userstack(struct trace_array *tr,
1006 unsigned long flags, int pc)
1008 #ifdef CONFIG_STACKTRACE
1009 struct ring_buffer_event *event;
1010 struct userstack_entry *entry;
1011 struct stack_trace trace;
1013 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1016 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1017 sizeof(*entry), flags, pc);
1020 entry = ring_buffer_event_data(event);
1022 memset(&entry->caller, 0, sizeof(entry->caller));
1024 trace.nr_entries = 0;
1025 trace.max_entries = FTRACE_STACK_ENTRIES;
1027 trace.entries = entry->caller;
1029 save_stack_trace_user(&trace);
1030 ring_buffer_unlock_commit(tr->buffer, event);
1035 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1037 ftrace_trace_userstack(tr, flags, preempt_count());
1042 ftrace_trace_special(void *__tr,
1043 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1046 struct ring_buffer_event *event;
1047 struct trace_array *tr = __tr;
1048 struct special_entry *entry;
1050 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1051 sizeof(*entry), 0, pc);
1054 entry = ring_buffer_event_data(event);
1058 trace_buffer_unlock_commit(tr, event, 0, pc);
1062 __trace_special(void *__tr, void *__data,
1063 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1065 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1069 tracing_sched_switch_trace(struct trace_array *tr,
1070 struct task_struct *prev,
1071 struct task_struct *next,
1072 unsigned long flags, int pc)
1074 struct ring_buffer_event *event;
1075 struct ctx_switch_entry *entry;
1077 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1078 sizeof(*entry), flags, pc);
1081 entry = ring_buffer_event_data(event);
1082 entry->prev_pid = prev->pid;
1083 entry->prev_prio = prev->prio;
1084 entry->prev_state = prev->state;
1085 entry->next_pid = next->pid;
1086 entry->next_prio = next->prio;
1087 entry->next_state = next->state;
1088 entry->next_cpu = task_cpu(next);
1089 trace_buffer_unlock_commit(tr, event, flags, pc);
1093 tracing_sched_wakeup_trace(struct trace_array *tr,
1094 struct task_struct *wakee,
1095 struct task_struct *curr,
1096 unsigned long flags, int pc)
1098 struct ring_buffer_event *event;
1099 struct ctx_switch_entry *entry;
1101 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1102 sizeof(*entry), flags, pc);
1105 entry = ring_buffer_event_data(event);
1106 entry->prev_pid = curr->pid;
1107 entry->prev_prio = curr->prio;
1108 entry->prev_state = curr->state;
1109 entry->next_pid = wakee->pid;
1110 entry->next_prio = wakee->prio;
1111 entry->next_state = wakee->state;
1112 entry->next_cpu = task_cpu(wakee);
1114 ring_buffer_unlock_commit(tr->buffer, event);
1115 ftrace_trace_stack(tr, flags, 6, pc);
1116 ftrace_trace_userstack(tr, flags, pc);
1120 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1122 struct trace_array *tr = &global_trace;
1123 struct trace_array_cpu *data;
1124 unsigned long flags;
1128 if (tracing_disabled)
1131 pc = preempt_count();
1132 local_irq_save(flags);
1133 cpu = raw_smp_processor_id();
1134 data = tr->data[cpu];
1136 if (likely(atomic_inc_return(&data->disabled) == 1))
1137 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1139 atomic_dec(&data->disabled);
1140 local_irq_restore(flags);
1143 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1144 int trace_graph_entry(struct ftrace_graph_ent *trace)
1146 struct trace_array *tr = &global_trace;
1147 struct trace_array_cpu *data;
1148 unsigned long flags;
1153 if (!ftrace_trace_task(current))
1156 if (!ftrace_graph_addr(trace->func))
1159 local_irq_save(flags);
1160 cpu = raw_smp_processor_id();
1161 data = tr->data[cpu];
1162 disabled = atomic_inc_return(&data->disabled);
1163 if (likely(disabled == 1)) {
1164 pc = preempt_count();
1165 __trace_graph_entry(tr, trace, flags, pc);
1167 /* Only do the atomic if it is not already set */
1168 if (!test_tsk_trace_graph(current))
1169 set_tsk_trace_graph(current);
1170 atomic_dec(&data->disabled);
1171 local_irq_restore(flags);
1176 void trace_graph_return(struct ftrace_graph_ret *trace)
1178 struct trace_array *tr = &global_trace;
1179 struct trace_array_cpu *data;
1180 unsigned long flags;
1185 local_irq_save(flags);
1186 cpu = raw_smp_processor_id();
1187 data = tr->data[cpu];
1188 disabled = atomic_inc_return(&data->disabled);
1189 if (likely(disabled == 1)) {
1190 pc = preempt_count();
1191 __trace_graph_return(tr, trace, flags, pc);
1194 clear_tsk_trace_graph(current);
1195 atomic_dec(&data->disabled);
1196 local_irq_restore(flags);
1198 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1202 * trace_vbprintk - write binary msg to tracing buffer
1205 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1207 static raw_spinlock_t trace_buf_lock =
1208 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1209 static u32 trace_buf[TRACE_BUF_SIZE];
1211 struct ring_buffer_event *event;
1212 struct trace_array *tr = &global_trace;
1213 struct trace_array_cpu *data;
1214 struct bprint_entry *entry;
1215 unsigned long flags;
1217 int cpu, len = 0, size, pc;
1219 if (unlikely(tracing_selftest_running || tracing_disabled))
1222 /* Don't pollute graph traces with trace_vprintk internals */
1223 pause_graph_tracing();
1225 pc = preempt_count();
1226 resched = ftrace_preempt_disable();
1227 cpu = raw_smp_processor_id();
1228 data = tr->data[cpu];
1230 if (unlikely(atomic_read(&data->disabled)))
1233 /* Lockdep uses trace_printk for lock tracing */
1234 local_irq_save(flags);
1235 __raw_spin_lock(&trace_buf_lock);
1236 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1238 if (len > TRACE_BUF_SIZE || len < 0)
1241 size = sizeof(*entry) + sizeof(u32) * len;
1242 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1245 entry = ring_buffer_event_data(event);
1249 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1250 ring_buffer_unlock_commit(tr->buffer, event);
1253 __raw_spin_unlock(&trace_buf_lock);
1254 local_irq_restore(flags);
1257 ftrace_preempt_enable(resched);
1258 unpause_graph_tracing();
1262 EXPORT_SYMBOL_GPL(trace_vbprintk);
1264 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1266 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1267 static char trace_buf[TRACE_BUF_SIZE];
1269 struct ring_buffer_event *event;
1270 struct trace_array *tr = &global_trace;
1271 struct trace_array_cpu *data;
1272 int cpu, len = 0, size, pc;
1273 struct print_entry *entry;
1274 unsigned long irq_flags;
1276 if (tracing_disabled || tracing_selftest_running)
1279 pc = preempt_count();
1280 preempt_disable_notrace();
1281 cpu = raw_smp_processor_id();
1282 data = tr->data[cpu];
1284 if (unlikely(atomic_read(&data->disabled)))
1287 pause_graph_tracing();
1288 raw_local_irq_save(irq_flags);
1289 __raw_spin_lock(&trace_buf_lock);
1290 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1292 len = min(len, TRACE_BUF_SIZE-1);
1295 size = sizeof(*entry) + len + 1;
1296 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1299 entry = ring_buffer_event_data(event);
1302 memcpy(&entry->buf, trace_buf, len);
1303 entry->buf[len] = 0;
1304 ring_buffer_unlock_commit(tr->buffer, event);
1307 __raw_spin_unlock(&trace_buf_lock);
1308 raw_local_irq_restore(irq_flags);
1309 unpause_graph_tracing();
1311 preempt_enable_notrace();
1315 EXPORT_SYMBOL_GPL(trace_vprintk);
1317 enum trace_file_type {
1318 TRACE_FILE_LAT_FMT = 1,
1319 TRACE_FILE_ANNOTATE = 2,
1322 static void trace_iterator_increment(struct trace_iterator *iter)
1324 /* Don't allow ftrace to trace into the ring buffers */
1325 ftrace_disable_cpu();
1328 if (iter->buffer_iter[iter->cpu])
1329 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1331 ftrace_enable_cpu();
1334 static struct trace_entry *
1335 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1337 struct ring_buffer_event *event;
1338 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1340 /* Don't allow ftrace to trace into the ring buffers */
1341 ftrace_disable_cpu();
1344 event = ring_buffer_iter_peek(buf_iter, ts);
1346 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1348 ftrace_enable_cpu();
1350 return event ? ring_buffer_event_data(event) : NULL;
1353 static struct trace_entry *
1354 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1356 struct ring_buffer *buffer = iter->tr->buffer;
1357 struct trace_entry *ent, *next = NULL;
1358 int cpu_file = iter->cpu_file;
1359 u64 next_ts = 0, ts;
1364 * If we are in a per_cpu trace file, don't bother by iterating over
1365 * all cpu and peek directly.
1367 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1368 if (ring_buffer_empty_cpu(buffer, cpu_file))
1370 ent = peek_next_entry(iter, cpu_file, ent_ts);
1372 *ent_cpu = cpu_file;
1377 for_each_tracing_cpu(cpu) {
1379 if (ring_buffer_empty_cpu(buffer, cpu))
1382 ent = peek_next_entry(iter, cpu, &ts);
1385 * Pick the entry with the smallest timestamp:
1387 if (ent && (!next || ts < next_ts)) {
1395 *ent_cpu = next_cpu;
1403 /* Find the next real entry, without updating the iterator itself */
1404 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1405 int *ent_cpu, u64 *ent_ts)
1407 return __find_next_entry(iter, ent_cpu, ent_ts);
1410 /* Find the next real entry, and increment the iterator to the next entry */
1411 static void *find_next_entry_inc(struct trace_iterator *iter)
1413 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1416 trace_iterator_increment(iter);
1418 return iter->ent ? iter : NULL;
1421 static void trace_consume(struct trace_iterator *iter)
1423 /* Don't allow ftrace to trace into the ring buffers */
1424 ftrace_disable_cpu();
1425 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1426 ftrace_enable_cpu();
1429 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1431 struct trace_iterator *iter = m->private;
1437 /* can't go backwards */
1442 ent = find_next_entry_inc(iter);
1446 while (ent && iter->idx < i)
1447 ent = find_next_entry_inc(iter);
1455 * No necessary locking here. The worst thing which can
1456 * happen is loosing events consumed at the same time
1457 * by a trace_pipe reader.
1458 * Other than that, we don't risk to crash the ring buffer
1459 * because it serializes the readers.
1461 * The current tracer is copied to avoid a global locking
1464 static void *s_start(struct seq_file *m, loff_t *pos)
1466 struct trace_iterator *iter = m->private;
1467 static struct tracer *old_tracer;
1468 int cpu_file = iter->cpu_file;
1473 /* copy the tracer to avoid using a global lock all around */
1474 mutex_lock(&trace_types_lock);
1475 if (unlikely(old_tracer != current_trace && current_trace)) {
1476 old_tracer = current_trace;
1477 *iter->trace = *current_trace;
1479 mutex_unlock(&trace_types_lock);
1481 atomic_inc(&trace_record_cmdline_disabled);
1483 if (*pos != iter->pos) {
1488 ftrace_disable_cpu();
1490 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1491 for_each_tracing_cpu(cpu)
1492 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1494 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1497 ftrace_enable_cpu();
1499 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1504 p = s_next(m, p, &l);
1510 static void s_stop(struct seq_file *m, void *p)
1512 atomic_dec(&trace_record_cmdline_disabled);
1515 static void print_lat_help_header(struct seq_file *m)
1517 seq_puts(m, "# _------=> CPU# \n");
1518 seq_puts(m, "# / _-----=> irqs-off \n");
1519 seq_puts(m, "# | / _----=> need-resched \n");
1520 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1521 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1522 seq_puts(m, "# |||| / \n");
1523 seq_puts(m, "# ||||| delay \n");
1524 seq_puts(m, "# cmd pid ||||| time | caller \n");
1525 seq_puts(m, "# \\ / ||||| \\ | / \n");
1528 static void print_func_help_header(struct seq_file *m)
1530 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1531 seq_puts(m, "# | | | | |\n");
1536 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1538 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1539 struct trace_array *tr = iter->tr;
1540 struct trace_array_cpu *data = tr->data[tr->cpu];
1541 struct tracer *type = current_trace;
1542 unsigned long total;
1543 unsigned long entries;
1544 const char *name = "preemption";
1549 entries = ring_buffer_entries(iter->tr->buffer);
1551 ring_buffer_overruns(iter->tr->buffer);
1553 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1555 seq_puts(m, "# -----------------------------------"
1556 "---------------------------------\n");
1557 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1558 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1559 nsecs_to_usecs(data->saved_latency),
1563 #if defined(CONFIG_PREEMPT_NONE)
1565 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1567 #elif defined(CONFIG_PREEMPT)
1572 /* These are reserved for later use */
1575 seq_printf(m, " #P:%d)\n", num_online_cpus());
1579 seq_puts(m, "# -----------------\n");
1580 seq_printf(m, "# | task: %.16s-%d "
1581 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1582 data->comm, data->pid, data->uid, data->nice,
1583 data->policy, data->rt_priority);
1584 seq_puts(m, "# -----------------\n");
1586 if (data->critical_start) {
1587 seq_puts(m, "# => started at: ");
1588 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1589 trace_print_seq(m, &iter->seq);
1590 seq_puts(m, "\n# => ended at: ");
1591 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1592 trace_print_seq(m, &iter->seq);
1599 static void test_cpu_buff_start(struct trace_iterator *iter)
1601 struct trace_seq *s = &iter->seq;
1603 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1606 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1609 if (cpumask_test_cpu(iter->cpu, iter->started))
1612 cpumask_set_cpu(iter->cpu, iter->started);
1613 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1616 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1618 struct trace_seq *s = &iter->seq;
1619 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1620 struct trace_entry *entry;
1621 struct trace_event *event;
1625 test_cpu_buff_start(iter);
1627 event = ftrace_find_event(entry->type);
1629 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1630 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1631 if (!trace_print_lat_context(iter))
1634 if (!trace_print_context(iter))
1640 return event->trace(iter, sym_flags);
1642 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1645 return TRACE_TYPE_HANDLED;
1647 return TRACE_TYPE_PARTIAL_LINE;
1650 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1652 struct trace_seq *s = &iter->seq;
1653 struct trace_entry *entry;
1654 struct trace_event *event;
1658 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1659 if (!trace_seq_printf(s, "%d %d %llu ",
1660 entry->pid, iter->cpu, iter->ts))
1664 event = ftrace_find_event(entry->type);
1666 return event->raw(iter, 0);
1668 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1671 return TRACE_TYPE_HANDLED;
1673 return TRACE_TYPE_PARTIAL_LINE;
1676 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1678 struct trace_seq *s = &iter->seq;
1679 unsigned char newline = '\n';
1680 struct trace_entry *entry;
1681 struct trace_event *event;
1685 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1686 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1687 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1688 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1691 event = ftrace_find_event(entry->type);
1693 enum print_line_t ret = event->hex(iter, 0);
1694 if (ret != TRACE_TYPE_HANDLED)
1698 SEQ_PUT_FIELD_RET(s, newline);
1700 return TRACE_TYPE_HANDLED;
1703 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1705 struct trace_seq *s = &iter->seq;
1706 struct trace_entry *entry;
1707 struct trace_event *event;
1711 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1712 SEQ_PUT_FIELD_RET(s, entry->pid);
1713 SEQ_PUT_FIELD_RET(s, iter->cpu);
1714 SEQ_PUT_FIELD_RET(s, iter->ts);
1717 event = ftrace_find_event(entry->type);
1718 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1721 static int trace_empty(struct trace_iterator *iter)
1725 /* If we are looking at one CPU buffer, only check that one */
1726 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1727 cpu = iter->cpu_file;
1728 if (iter->buffer_iter[cpu]) {
1729 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1732 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1738 for_each_tracing_cpu(cpu) {
1739 if (iter->buffer_iter[cpu]) {
1740 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1743 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1751 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1753 enum print_line_t ret;
1755 if (iter->trace && iter->trace->print_line) {
1756 ret = iter->trace->print_line(iter);
1757 if (ret != TRACE_TYPE_UNHANDLED)
1761 if (iter->ent->type == TRACE_BPRINT &&
1762 trace_flags & TRACE_ITER_PRINTK &&
1763 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1764 return trace_print_bprintk_msg_only(iter);
1766 if (iter->ent->type == TRACE_PRINT &&
1767 trace_flags & TRACE_ITER_PRINTK &&
1768 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1769 return trace_print_printk_msg_only(iter);
1771 if (trace_flags & TRACE_ITER_BIN)
1772 return print_bin_fmt(iter);
1774 if (trace_flags & TRACE_ITER_HEX)
1775 return print_hex_fmt(iter);
1777 if (trace_flags & TRACE_ITER_RAW)
1778 return print_raw_fmt(iter);
1780 return print_trace_fmt(iter);
1783 static int s_show(struct seq_file *m, void *v)
1785 struct trace_iterator *iter = v;
1787 if (iter->ent == NULL) {
1789 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1792 if (iter->trace && iter->trace->print_header)
1793 iter->trace->print_header(m);
1794 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1795 /* print nothing if the buffers are empty */
1796 if (trace_empty(iter))
1798 print_trace_header(m, iter);
1799 if (!(trace_flags & TRACE_ITER_VERBOSE))
1800 print_lat_help_header(m);
1802 if (!(trace_flags & TRACE_ITER_VERBOSE))
1803 print_func_help_header(m);
1806 print_trace_line(iter);
1807 trace_print_seq(m, &iter->seq);
1813 static struct seq_operations tracer_seq_ops = {
1820 static struct trace_iterator *
1821 __tracing_open(struct inode *inode, struct file *file)
1823 long cpu_file = (long) inode->i_private;
1824 void *fail_ret = ERR_PTR(-ENOMEM);
1825 struct trace_iterator *iter;
1829 if (tracing_disabled)
1830 return ERR_PTR(-ENODEV);
1832 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1834 return ERR_PTR(-ENOMEM);
1837 * We make a copy of the current tracer to avoid concurrent
1838 * changes on it while we are reading.
1840 mutex_lock(&trace_types_lock);
1841 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1846 *iter->trace = *current_trace;
1848 if (current_trace && current_trace->print_max)
1851 iter->tr = &global_trace;
1853 mutex_init(&iter->mutex);
1854 iter->cpu_file = cpu_file;
1856 /* Notify the tracer early; before we stop tracing. */
1857 if (iter->trace && iter->trace->open)
1858 iter->trace->open(iter);
1860 /* Annotate start of buffers if we had overruns */
1861 if (ring_buffer_overruns(iter->tr->buffer))
1862 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1864 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1865 for_each_tracing_cpu(cpu) {
1867 iter->buffer_iter[cpu] =
1868 ring_buffer_read_start(iter->tr->buffer, cpu);
1871 cpu = iter->cpu_file;
1872 iter->buffer_iter[cpu] =
1873 ring_buffer_read_start(iter->tr->buffer, cpu);
1876 /* TODO stop tracer */
1877 ret = seq_open(file, &tracer_seq_ops);
1879 fail_ret = ERR_PTR(ret);
1883 m = file->private_data;
1886 /* stop the trace while dumping */
1889 mutex_unlock(&trace_types_lock);
1894 for_each_tracing_cpu(cpu) {
1895 if (iter->buffer_iter[cpu])
1896 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1899 mutex_unlock(&trace_types_lock);
1906 int tracing_open_generic(struct inode *inode, struct file *filp)
1908 if (tracing_disabled)
1911 filp->private_data = inode->i_private;
1915 static int tracing_release(struct inode *inode, struct file *file)
1917 struct seq_file *m = (struct seq_file *)file->private_data;
1918 struct trace_iterator *iter;
1921 if (!(file->f_mode & FMODE_READ))
1926 mutex_lock(&trace_types_lock);
1927 for_each_tracing_cpu(cpu) {
1928 if (iter->buffer_iter[cpu])
1929 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1932 if (iter->trace && iter->trace->close)
1933 iter->trace->close(iter);
1935 /* reenable tracing if it was previously enabled */
1937 mutex_unlock(&trace_types_lock);
1939 seq_release(inode, file);
1940 mutex_destroy(&iter->mutex);
1946 static int tracing_open(struct inode *inode, struct file *file)
1948 struct trace_iterator *iter;
1951 /* If this file was open for write, then erase contents */
1952 if ((file->f_mode & FMODE_WRITE) &&
1953 !(file->f_flags & O_APPEND)) {
1954 long cpu = (long) inode->i_private;
1956 if (cpu == TRACE_PIPE_ALL_CPU)
1957 tracing_reset_online_cpus(&global_trace);
1959 tracing_reset(&global_trace, cpu);
1962 if (file->f_mode & FMODE_READ) {
1963 iter = __tracing_open(inode, file);
1965 ret = PTR_ERR(iter);
1966 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1967 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1973 t_next(struct seq_file *m, void *v, loff_t *pos)
1975 struct tracer *t = m->private;
1987 static void *t_start(struct seq_file *m, loff_t *pos)
1989 struct tracer *t = m->private;
1992 mutex_lock(&trace_types_lock);
1993 for (; t && l < *pos; t = t_next(m, t, &l))
1999 static void t_stop(struct seq_file *m, void *p)
2001 mutex_unlock(&trace_types_lock);
2004 static int t_show(struct seq_file *m, void *v)
2006 struct tracer *t = v;
2011 seq_printf(m, "%s", t->name);
2020 static struct seq_operations show_traces_seq_ops = {
2027 static int show_traces_open(struct inode *inode, struct file *file)
2031 if (tracing_disabled)
2034 ret = seq_open(file, &show_traces_seq_ops);
2036 struct seq_file *m = file->private_data;
2037 m->private = trace_types;
2044 tracing_write_stub(struct file *filp, const char __user *ubuf,
2045 size_t count, loff_t *ppos)
2050 static const struct file_operations tracing_fops = {
2051 .open = tracing_open,
2053 .write = tracing_write_stub,
2054 .llseek = seq_lseek,
2055 .release = tracing_release,
2058 static const struct file_operations show_traces_fops = {
2059 .open = show_traces_open,
2061 .release = seq_release,
2065 * Only trace on a CPU if the bitmask is set:
2067 static cpumask_var_t tracing_cpumask;
2070 * The tracer itself will not take this lock, but still we want
2071 * to provide a consistent cpumask to user-space:
2073 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2076 * Temporary storage for the character representation of the
2077 * CPU bitmask (and one more byte for the newline):
2079 static char mask_str[NR_CPUS + 1];
2082 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2083 size_t count, loff_t *ppos)
2087 mutex_lock(&tracing_cpumask_update_lock);
2089 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2090 if (count - len < 2) {
2094 len += sprintf(mask_str + len, "\n");
2095 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2098 mutex_unlock(&tracing_cpumask_update_lock);
2104 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2105 size_t count, loff_t *ppos)
2108 cpumask_var_t tracing_cpumask_new;
2110 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2113 mutex_lock(&tracing_cpumask_update_lock);
2114 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2118 local_irq_disable();
2119 __raw_spin_lock(&ftrace_max_lock);
2120 for_each_tracing_cpu(cpu) {
2122 * Increase/decrease the disabled counter if we are
2123 * about to flip a bit in the cpumask:
2125 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2126 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2127 atomic_inc(&global_trace.data[cpu]->disabled);
2129 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2130 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2131 atomic_dec(&global_trace.data[cpu]->disabled);
2134 __raw_spin_unlock(&ftrace_max_lock);
2137 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2139 mutex_unlock(&tracing_cpumask_update_lock);
2140 free_cpumask_var(tracing_cpumask_new);
2145 mutex_unlock(&tracing_cpumask_update_lock);
2146 free_cpumask_var(tracing_cpumask);
2151 static const struct file_operations tracing_cpumask_fops = {
2152 .open = tracing_open_generic,
2153 .read = tracing_cpumask_read,
2154 .write = tracing_cpumask_write,
2158 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2159 size_t cnt, loff_t *ppos)
2161 struct tracer_opt *trace_opts;
2169 /* calculate max size */
2170 for (i = 0; trace_options[i]; i++) {
2171 len += strlen(trace_options[i]);
2172 len += 3; /* "no" and newline */
2175 mutex_lock(&trace_types_lock);
2176 tracer_flags = current_trace->flags->val;
2177 trace_opts = current_trace->flags->opts;
2180 * Increase the size with names of options specific
2181 * of the current tracer.
2183 for (i = 0; trace_opts[i].name; i++) {
2184 len += strlen(trace_opts[i].name);
2185 len += 3; /* "no" and newline */
2188 /* +2 for \n and \0 */
2189 buf = kmalloc(len + 2, GFP_KERNEL);
2191 mutex_unlock(&trace_types_lock);
2195 for (i = 0; trace_options[i]; i++) {
2196 if (trace_flags & (1 << i))
2197 r += sprintf(buf + r, "%s\n", trace_options[i]);
2199 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2202 for (i = 0; trace_opts[i].name; i++) {
2203 if (tracer_flags & trace_opts[i].bit)
2204 r += sprintf(buf + r, "%s\n",
2205 trace_opts[i].name);
2207 r += sprintf(buf + r, "no%s\n",
2208 trace_opts[i].name);
2210 mutex_unlock(&trace_types_lock);
2212 WARN_ON(r >= len + 2);
2214 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2220 /* Try to assign a tracer specific option */
2221 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2223 struct tracer_flags *trace_flags = trace->flags;
2224 struct tracer_opt *opts = NULL;
2228 for (i = 0; trace_flags->opts[i].name; i++) {
2229 opts = &trace_flags->opts[i];
2230 len = strlen(opts->name);
2232 if (strncmp(cmp, opts->name, len) == 0) {
2233 ret = trace->set_flag(trace_flags->val,
2239 if (!trace_flags->opts[i].name)
2242 /* Refused to handle */
2247 trace_flags->val &= ~opts->bit;
2249 trace_flags->val |= opts->bit;
2254 static void set_tracer_flags(unsigned int mask, int enabled)
2256 /* do nothing if flag is already set */
2257 if (!!(trace_flags & mask) == !!enabled)
2261 trace_flags |= mask;
2263 trace_flags &= ~mask;
2265 if (mask == TRACE_ITER_GLOBAL_CLK) {
2269 func = trace_clock_global;
2271 func = trace_clock_local;
2273 mutex_lock(&trace_types_lock);
2274 ring_buffer_set_clock(global_trace.buffer, func);
2277 ring_buffer_set_clock(max_tr.buffer, func);
2278 mutex_unlock(&trace_types_lock);
2283 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2284 size_t cnt, loff_t *ppos)
2292 if (cnt >= sizeof(buf))
2295 if (copy_from_user(&buf, ubuf, cnt))
2300 if (strncmp(buf, "no", 2) == 0) {
2305 for (i = 0; trace_options[i]; i++) {
2306 int len = strlen(trace_options[i]);
2308 if (strncmp(cmp, trace_options[i], len) == 0) {
2309 set_tracer_flags(1 << i, !neg);
2314 /* If no option could be set, test the specific tracer options */
2315 if (!trace_options[i]) {
2316 mutex_lock(&trace_types_lock);
2317 ret = set_tracer_option(current_trace, cmp, neg);
2318 mutex_unlock(&trace_types_lock);
2328 static const struct file_operations tracing_iter_fops = {
2329 .open = tracing_open_generic,
2330 .read = tracing_trace_options_read,
2331 .write = tracing_trace_options_write,
2334 static const char readme_msg[] =
2335 "tracing mini-HOWTO:\n\n"
2337 "# mount -t debugfs nodev /debug\n\n"
2338 "# cat /debug/tracing/available_tracers\n"
2339 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2340 "# cat /debug/tracing/current_tracer\n"
2342 "# echo sched_switch > /debug/tracing/current_tracer\n"
2343 "# cat /debug/tracing/current_tracer\n"
2345 "# cat /debug/tracing/trace_options\n"
2346 "noprint-parent nosym-offset nosym-addr noverbose\n"
2347 "# echo print-parent > /debug/tracing/trace_options\n"
2348 "# echo 1 > /debug/tracing/tracing_enabled\n"
2349 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2350 "echo 0 > /debug/tracing/tracing_enabled\n"
2354 tracing_readme_read(struct file *filp, char __user *ubuf,
2355 size_t cnt, loff_t *ppos)
2357 return simple_read_from_buffer(ubuf, cnt, ppos,
2358 readme_msg, strlen(readme_msg));
2361 static const struct file_operations tracing_readme_fops = {
2362 .open = tracing_open_generic,
2363 .read = tracing_readme_read,
2367 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2368 size_t cnt, loff_t *ppos)
2373 r = sprintf(buf, "%u\n", tracer_enabled);
2374 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2378 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2379 size_t cnt, loff_t *ppos)
2381 struct trace_array *tr = filp->private_data;
2386 if (cnt >= sizeof(buf))
2389 if (copy_from_user(&buf, ubuf, cnt))
2394 ret = strict_strtoul(buf, 10, &val);
2400 mutex_lock(&trace_types_lock);
2401 if (tracer_enabled ^ val) {
2404 if (current_trace->start)
2405 current_trace->start(tr);
2410 if (current_trace->stop)
2411 current_trace->stop(tr);
2414 mutex_unlock(&trace_types_lock);
2422 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2423 size_t cnt, loff_t *ppos)
2425 char buf[max_tracer_type_len+2];
2428 mutex_lock(&trace_types_lock);
2430 r = sprintf(buf, "%s\n", current_trace->name);
2432 r = sprintf(buf, "\n");
2433 mutex_unlock(&trace_types_lock);
2435 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2438 int tracer_init(struct tracer *t, struct trace_array *tr)
2440 tracing_reset_online_cpus(tr);
2444 static int tracing_resize_ring_buffer(unsigned long size)
2449 * If kernel or user changes the size of the ring buffer
2450 * we use the size that was given, and we can forget about
2451 * expanding it later.
2453 ring_buffer_expanded = 1;
2455 ret = ring_buffer_resize(global_trace.buffer, size);
2459 ret = ring_buffer_resize(max_tr.buffer, size);
2463 r = ring_buffer_resize(global_trace.buffer,
2464 global_trace.entries);
2467 * AARGH! We are left with different
2468 * size max buffer!!!!
2469 * The max buffer is our "snapshot" buffer.
2470 * When a tracer needs a snapshot (one of the
2471 * latency tracers), it swaps the max buffer
2472 * with the saved snap shot. We succeeded to
2473 * update the size of the main buffer, but failed to
2474 * update the size of the max buffer. But when we tried
2475 * to reset the main buffer to the original size, we
2476 * failed there too. This is very unlikely to
2477 * happen, but if it does, warn and kill all
2481 tracing_disabled = 1;
2486 global_trace.entries = size;
2492 * tracing_update_buffers - used by tracing facility to expand ring buffers
2494 * To save on memory when the tracing is never used on a system with it
2495 * configured in. The ring buffers are set to a minimum size. But once
2496 * a user starts to use the tracing facility, then they need to grow
2497 * to their default size.
2499 * This function is to be called when a tracer is about to be used.
2501 int tracing_update_buffers(void)
2505 mutex_lock(&trace_types_lock);
2506 if (!ring_buffer_expanded)
2507 ret = tracing_resize_ring_buffer(trace_buf_size);
2508 mutex_unlock(&trace_types_lock);
2513 struct trace_option_dentry;
2515 static struct trace_option_dentry *
2516 create_trace_option_files(struct tracer *tracer);
2519 destroy_trace_option_files(struct trace_option_dentry *topts);
2521 static int tracing_set_tracer(const char *buf)
2523 static struct trace_option_dentry *topts;
2524 struct trace_array *tr = &global_trace;
2528 mutex_lock(&trace_types_lock);
2530 if (!ring_buffer_expanded) {
2531 ret = tracing_resize_ring_buffer(trace_buf_size);
2537 for (t = trace_types; t; t = t->next) {
2538 if (strcmp(t->name, buf) == 0)
2545 if (t == current_trace)
2548 trace_branch_disable();
2549 if (current_trace && current_trace->reset)
2550 current_trace->reset(tr);
2552 destroy_trace_option_files(topts);
2556 topts = create_trace_option_files(current_trace);
2559 ret = tracer_init(t, tr);
2564 trace_branch_enable(tr);
2566 mutex_unlock(&trace_types_lock);
2572 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2573 size_t cnt, loff_t *ppos)
2575 char buf[max_tracer_type_len+1];
2582 if (cnt > max_tracer_type_len)
2583 cnt = max_tracer_type_len;
2585 if (copy_from_user(&buf, ubuf, cnt))
2590 /* strip ending whitespace. */
2591 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2594 err = tracing_set_tracer(buf);
2604 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2605 size_t cnt, loff_t *ppos)
2607 unsigned long *ptr = filp->private_data;
2611 r = snprintf(buf, sizeof(buf), "%ld\n",
2612 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2613 if (r > sizeof(buf))
2615 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2619 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2620 size_t cnt, loff_t *ppos)
2622 unsigned long *ptr = filp->private_data;
2627 if (cnt >= sizeof(buf))
2630 if (copy_from_user(&buf, ubuf, cnt))
2635 ret = strict_strtoul(buf, 10, &val);
2644 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2646 long cpu_file = (long) inode->i_private;
2647 struct trace_iterator *iter;
2650 if (tracing_disabled)
2653 mutex_lock(&trace_types_lock);
2655 /* We only allow one reader per cpu */
2656 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2657 if (!cpumask_empty(tracing_reader_cpumask)) {
2661 cpumask_setall(tracing_reader_cpumask);
2663 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2664 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2671 /* create a buffer to store the information to pass to userspace */
2672 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2679 * We make a copy of the current tracer to avoid concurrent
2680 * changes on it while we are reading.
2682 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2688 *iter->trace = *current_trace;
2690 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2695 /* trace pipe does not show start of buffer */
2696 cpumask_setall(iter->started);
2698 iter->cpu_file = cpu_file;
2699 iter->tr = &global_trace;
2700 mutex_init(&iter->mutex);
2701 filp->private_data = iter;
2703 if (iter->trace->pipe_open)
2704 iter->trace->pipe_open(iter);
2707 mutex_unlock(&trace_types_lock);
2713 mutex_unlock(&trace_types_lock);
2717 static int tracing_release_pipe(struct inode *inode, struct file *file)
2719 struct trace_iterator *iter = file->private_data;
2721 mutex_lock(&trace_types_lock);
2723 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2724 cpumask_clear(tracing_reader_cpumask);
2726 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2728 mutex_unlock(&trace_types_lock);
2730 free_cpumask_var(iter->started);
2731 mutex_destroy(&iter->mutex);
2739 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2741 struct trace_iterator *iter = filp->private_data;
2743 if (trace_flags & TRACE_ITER_BLOCK) {
2745 * Always select as readable when in blocking mode
2747 return POLLIN | POLLRDNORM;
2749 if (!trace_empty(iter))
2750 return POLLIN | POLLRDNORM;
2751 poll_wait(filp, &trace_wait, poll_table);
2752 if (!trace_empty(iter))
2753 return POLLIN | POLLRDNORM;
2760 void default_wait_pipe(struct trace_iterator *iter)
2764 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2766 if (trace_empty(iter))
2769 finish_wait(&trace_wait, &wait);
2773 * This is a make-shift waitqueue.
2774 * A tracer might use this callback on some rare cases:
2776 * 1) the current tracer might hold the runqueue lock when it wakes up
2777 * a reader, hence a deadlock (sched, function, and function graph tracers)
2778 * 2) the function tracers, trace all functions, we don't want
2779 * the overhead of calling wake_up and friends
2780 * (and tracing them too)
2782 * Anyway, this is really very primitive wakeup.
2784 void poll_wait_pipe(struct trace_iterator *iter)
2786 set_current_state(TASK_INTERRUPTIBLE);
2787 /* sleep for 100 msecs, and try again. */
2788 schedule_timeout(HZ / 10);
2791 /* Must be called with trace_types_lock mutex held. */
2792 static int tracing_wait_pipe(struct file *filp)
2794 struct trace_iterator *iter = filp->private_data;
2796 while (trace_empty(iter)) {
2798 if ((filp->f_flags & O_NONBLOCK)) {
2802 mutex_unlock(&iter->mutex);
2804 iter->trace->wait_pipe(iter);
2806 mutex_lock(&iter->mutex);
2808 if (signal_pending(current))
2812 * We block until we read something and tracing is disabled.
2813 * We still block if tracing is disabled, but we have never
2814 * read anything. This allows a user to cat this file, and
2815 * then enable tracing. But after we have read something,
2816 * we give an EOF when tracing is again disabled.
2818 * iter->pos will be 0 if we haven't read anything.
2820 if (!tracer_enabled && iter->pos)
2831 tracing_read_pipe(struct file *filp, char __user *ubuf,
2832 size_t cnt, loff_t *ppos)
2834 struct trace_iterator *iter = filp->private_data;
2835 static struct tracer *old_tracer;
2838 /* return any leftover data */
2839 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2843 trace_seq_init(&iter->seq);
2845 /* copy the tracer to avoid using a global lock all around */
2846 mutex_lock(&trace_types_lock);
2847 if (unlikely(old_tracer != current_trace && current_trace)) {
2848 old_tracer = current_trace;
2849 *iter->trace = *current_trace;
2851 mutex_unlock(&trace_types_lock);
2854 * Avoid more than one consumer on a single file descriptor
2855 * This is just a matter of traces coherency, the ring buffer itself
2858 mutex_lock(&iter->mutex);
2859 if (iter->trace->read) {
2860 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2866 sret = tracing_wait_pipe(filp);
2870 /* stop when tracing is finished */
2871 if (trace_empty(iter)) {
2876 if (cnt >= PAGE_SIZE)
2877 cnt = PAGE_SIZE - 1;
2879 /* reset all but tr, trace, and overruns */
2880 memset(&iter->seq, 0,
2881 sizeof(struct trace_iterator) -
2882 offsetof(struct trace_iterator, seq));
2885 while (find_next_entry_inc(iter) != NULL) {
2886 enum print_line_t ret;
2887 int len = iter->seq.len;
2889 ret = print_trace_line(iter);
2890 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2891 /* don't print partial lines */
2892 iter->seq.len = len;
2895 if (ret != TRACE_TYPE_NO_CONSUME)
2896 trace_consume(iter);
2898 if (iter->seq.len >= cnt)
2902 /* Now copy what we have to the user */
2903 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2904 if (iter->seq.readpos >= iter->seq.len)
2905 trace_seq_init(&iter->seq);
2908 * If there was nothing to send to user, inspite of consuming trace
2909 * entries, go back to wait for more entries.
2915 mutex_unlock(&iter->mutex);
2920 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2921 struct pipe_buffer *buf)
2923 __free_page(buf->page);
2926 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2929 __free_page(spd->pages[idx]);
2932 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2934 .map = generic_pipe_buf_map,
2935 .unmap = generic_pipe_buf_unmap,
2936 .confirm = generic_pipe_buf_confirm,
2937 .release = tracing_pipe_buf_release,
2938 .steal = generic_pipe_buf_steal,
2939 .get = generic_pipe_buf_get,
2943 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2948 /* Seq buffer is page-sized, exactly what we need. */
2950 count = iter->seq.len;
2951 ret = print_trace_line(iter);
2952 count = iter->seq.len - count;
2955 iter->seq.len -= count;
2958 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2959 iter->seq.len -= count;
2963 trace_consume(iter);
2965 if (!find_next_entry_inc(iter)) {
2975 static ssize_t tracing_splice_read_pipe(struct file *filp,
2977 struct pipe_inode_info *pipe,
2981 struct page *pages[PIPE_BUFFERS];
2982 struct partial_page partial[PIPE_BUFFERS];
2983 struct trace_iterator *iter = filp->private_data;
2984 struct splice_pipe_desc spd = {
2987 .nr_pages = 0, /* This gets updated below. */
2989 .ops = &tracing_pipe_buf_ops,
2990 .spd_release = tracing_spd_release_pipe,
2992 static struct tracer *old_tracer;
2997 /* copy the tracer to avoid using a global lock all around */
2998 mutex_lock(&trace_types_lock);
2999 if (unlikely(old_tracer != current_trace && current_trace)) {
3000 old_tracer = current_trace;
3001 *iter->trace = *current_trace;
3003 mutex_unlock(&trace_types_lock);
3005 mutex_lock(&iter->mutex);
3007 if (iter->trace->splice_read) {
3008 ret = iter->trace->splice_read(iter, filp,
3009 ppos, pipe, len, flags);
3014 ret = tracing_wait_pipe(filp);
3018 if (!iter->ent && !find_next_entry_inc(iter)) {
3023 /* Fill as many pages as possible. */
3024 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3025 pages[i] = alloc_page(GFP_KERNEL);
3029 rem = tracing_fill_pipe_page(rem, iter);
3031 /* Copy the data into the page, so we can start over. */
3032 ret = trace_seq_to_buffer(&iter->seq,
3033 page_address(pages[i]),
3036 __free_page(pages[i]);
3039 partial[i].offset = 0;
3040 partial[i].len = iter->seq.len;
3042 trace_seq_init(&iter->seq);
3045 mutex_unlock(&iter->mutex);
3049 return splice_to_pipe(pipe, &spd);
3052 mutex_unlock(&iter->mutex);
3058 tracing_entries_read(struct file *filp, char __user *ubuf,
3059 size_t cnt, loff_t *ppos)
3061 struct trace_array *tr = filp->private_data;
3065 mutex_lock(&trace_types_lock);
3066 if (!ring_buffer_expanded)
3067 r = sprintf(buf, "%lu (expanded: %lu)\n",
3069 trace_buf_size >> 10);
3071 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3072 mutex_unlock(&trace_types_lock);
3074 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3078 tracing_entries_write(struct file *filp, const char __user *ubuf,
3079 size_t cnt, loff_t *ppos)
3085 if (cnt >= sizeof(buf))
3088 if (copy_from_user(&buf, ubuf, cnt))
3093 ret = strict_strtoul(buf, 10, &val);
3097 /* must have at least 1 entry */
3101 mutex_lock(&trace_types_lock);
3105 /* disable all cpu buffers */
3106 for_each_tracing_cpu(cpu) {
3107 if (global_trace.data[cpu])
3108 atomic_inc(&global_trace.data[cpu]->disabled);
3109 if (max_tr.data[cpu])
3110 atomic_inc(&max_tr.data[cpu]->disabled);
3113 /* value is in KB */
3116 if (val != global_trace.entries) {
3117 ret = tracing_resize_ring_buffer(val);
3126 /* If check pages failed, return ENOMEM */
3127 if (tracing_disabled)
3130 for_each_tracing_cpu(cpu) {
3131 if (global_trace.data[cpu])
3132 atomic_dec(&global_trace.data[cpu]->disabled);
3133 if (max_tr.data[cpu])
3134 atomic_dec(&max_tr.data[cpu]->disabled);
3138 max_tr.entries = global_trace.entries;
3139 mutex_unlock(&trace_types_lock);
3144 static int mark_printk(const char *fmt, ...)
3148 va_start(args, fmt);
3149 ret = trace_vprintk(0, fmt, args);
3155 tracing_mark_write(struct file *filp, const char __user *ubuf,
3156 size_t cnt, loff_t *fpos)
3161 if (tracing_disabled)
3164 if (cnt > TRACE_BUF_SIZE)
3165 cnt = TRACE_BUF_SIZE;
3167 buf = kmalloc(cnt + 1, GFP_KERNEL);
3171 if (copy_from_user(buf, ubuf, cnt)) {
3176 /* Cut from the first nil or newline. */
3178 end = strchr(buf, '\n');
3182 cnt = mark_printk("%s\n", buf);
3189 static const struct file_operations tracing_max_lat_fops = {
3190 .open = tracing_open_generic,
3191 .read = tracing_max_lat_read,
3192 .write = tracing_max_lat_write,
3195 static const struct file_operations tracing_ctrl_fops = {
3196 .open = tracing_open_generic,
3197 .read = tracing_ctrl_read,
3198 .write = tracing_ctrl_write,
3201 static const struct file_operations set_tracer_fops = {
3202 .open = tracing_open_generic,
3203 .read = tracing_set_trace_read,
3204 .write = tracing_set_trace_write,
3207 static const struct file_operations tracing_pipe_fops = {
3208 .open = tracing_open_pipe,
3209 .poll = tracing_poll_pipe,
3210 .read = tracing_read_pipe,
3211 .splice_read = tracing_splice_read_pipe,
3212 .release = tracing_release_pipe,
3215 static const struct file_operations tracing_entries_fops = {
3216 .open = tracing_open_generic,
3217 .read = tracing_entries_read,
3218 .write = tracing_entries_write,
3221 static const struct file_operations tracing_mark_fops = {
3222 .open = tracing_open_generic,
3223 .write = tracing_mark_write,
3226 struct ftrace_buffer_info {
3227 struct trace_array *tr;
3233 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3235 int cpu = (int)(long)inode->i_private;
3236 struct ftrace_buffer_info *info;
3238 if (tracing_disabled)
3241 info = kzalloc(sizeof(*info), GFP_KERNEL);
3245 info->tr = &global_trace;
3247 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3248 /* Force reading ring buffer for first read */
3249 info->read = (unsigned int)-1;
3253 filp->private_data = info;
3263 tracing_buffers_read(struct file *filp, char __user *ubuf,
3264 size_t count, loff_t *ppos)
3266 struct ftrace_buffer_info *info = filp->private_data;
3274 /* Do we have previous read data to read? */
3275 if (info->read < PAGE_SIZE)
3280 ret = ring_buffer_read_page(info->tr->buffer,
3287 pos = ring_buffer_page_len(info->spare);
3289 if (pos < PAGE_SIZE)
3290 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3293 size = PAGE_SIZE - info->read;
3297 ret = copy_to_user(ubuf, info->spare + info->read, size);
3308 static int tracing_buffers_release(struct inode *inode, struct file *file)
3310 struct ftrace_buffer_info *info = file->private_data;
3312 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3319 struct ring_buffer *buffer;
3324 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3325 struct pipe_buffer *buf)
3327 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3332 ring_buffer_free_read_page(ref->buffer, ref->page);
3337 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3338 struct pipe_buffer *buf)
3343 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3344 struct pipe_buffer *buf)
3346 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3351 /* Pipe buffer operations for a buffer. */
3352 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3354 .map = generic_pipe_buf_map,
3355 .unmap = generic_pipe_buf_unmap,
3356 .confirm = generic_pipe_buf_confirm,
3357 .release = buffer_pipe_buf_release,
3358 .steal = buffer_pipe_buf_steal,
3359 .get = buffer_pipe_buf_get,
3363 * Callback from splice_to_pipe(), if we need to release some pages
3364 * at the end of the spd in case we error'ed out in filling the pipe.
3366 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3368 struct buffer_ref *ref =
3369 (struct buffer_ref *)spd->partial[i].private;
3374 ring_buffer_free_read_page(ref->buffer, ref->page);
3376 spd->partial[i].private = 0;
3380 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3381 struct pipe_inode_info *pipe, size_t len,
3384 struct ftrace_buffer_info *info = file->private_data;
3385 struct partial_page partial[PIPE_BUFFERS];
3386 struct page *pages[PIPE_BUFFERS];
3387 struct splice_pipe_desc spd = {
3391 .ops = &buffer_pipe_buf_ops,
3392 .spd_release = buffer_spd_release,
3394 struct buffer_ref *ref;
3399 * We can't seek on a buffer input
3401 if (unlikely(*ppos))
3405 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3409 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3413 ref->buffer = info->tr->buffer;
3414 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3420 r = ring_buffer_read_page(ref->buffer, &ref->page,
3423 ring_buffer_free_read_page(ref->buffer,
3430 * zero out any left over data, this is going to
3433 size = ring_buffer_page_len(ref->page);
3434 if (size < PAGE_SIZE)
3435 memset(ref->page + size, 0, PAGE_SIZE - size);
3437 page = virt_to_page(ref->page);
3439 spd.pages[i] = page;
3440 spd.partial[i].len = PAGE_SIZE;
3441 spd.partial[i].offset = 0;
3442 spd.partial[i].private = (unsigned long)ref;
3448 /* did we read anything? */
3449 if (!spd.nr_pages) {
3450 if (flags & SPLICE_F_NONBLOCK)
3458 ret = splice_to_pipe(pipe, &spd);
3463 static const struct file_operations tracing_buffers_fops = {
3464 .open = tracing_buffers_open,
3465 .read = tracing_buffers_read,
3466 .release = tracing_buffers_release,
3467 .splice_read = tracing_buffers_splice_read,
3468 .llseek = no_llseek,
3471 #ifdef CONFIG_DYNAMIC_FTRACE
3473 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3479 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3480 size_t cnt, loff_t *ppos)
3482 static char ftrace_dyn_info_buffer[1024];
3483 static DEFINE_MUTEX(dyn_info_mutex);
3484 unsigned long *p = filp->private_data;
3485 char *buf = ftrace_dyn_info_buffer;
3486 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3489 mutex_lock(&dyn_info_mutex);
3490 r = sprintf(buf, "%ld ", *p);
3492 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3495 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3497 mutex_unlock(&dyn_info_mutex);
3502 static const struct file_operations tracing_dyn_info_fops = {
3503 .open = tracing_open_generic,
3504 .read = tracing_read_dyn_info,
3508 static struct dentry *d_tracer;
3510 struct dentry *tracing_init_dentry(void)
3517 if (!debugfs_initialized())
3520 d_tracer = debugfs_create_dir("tracing", NULL);
3522 if (!d_tracer && !once) {
3524 pr_warning("Could not create debugfs directory 'tracing'\n");
3531 static struct dentry *d_percpu;
3533 struct dentry *tracing_dentry_percpu(void)
3536 struct dentry *d_tracer;
3541 d_tracer = tracing_init_dentry();
3546 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3548 if (!d_percpu && !once) {
3550 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3557 static void tracing_init_debugfs_percpu(long cpu)
3559 struct dentry *d_percpu = tracing_dentry_percpu();
3560 struct dentry *entry, *d_cpu;
3561 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3564 if (cpu > 999 || cpu < 0)
3567 sprintf(cpu_dir, "cpu%ld", cpu);
3568 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3570 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3574 /* per cpu trace_pipe */
3575 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
3576 (void *) cpu, &tracing_pipe_fops);
3578 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3581 entry = debugfs_create_file("trace", 0644, d_cpu,
3582 (void *) cpu, &tracing_fops);
3584 pr_warning("Could not create debugfs 'trace' entry\n");
3586 entry = debugfs_create_file("trace_pipe_raw", 0444, d_cpu,
3587 (void *) cpu, &tracing_buffers_fops);
3589 pr_warning("Could not create debugfs 'trace_pipe_raw' entry\n");
3592 #ifdef CONFIG_FTRACE_SELFTEST
3593 /* Let selftest have access to static functions in this file */
3594 #include "trace_selftest.c"
3597 struct trace_option_dentry {
3598 struct tracer_opt *opt;
3599 struct tracer_flags *flags;
3600 struct dentry *entry;
3604 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3607 struct trace_option_dentry *topt = filp->private_data;
3610 if (topt->flags->val & topt->opt->bit)
3615 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3619 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3622 struct trace_option_dentry *topt = filp->private_data;
3627 if (cnt >= sizeof(buf))
3630 if (copy_from_user(&buf, ubuf, cnt))
3635 ret = strict_strtoul(buf, 10, &val);
3642 /* do nothing if already cleared */
3643 if (!(topt->flags->val & topt->opt->bit))
3646 mutex_lock(&trace_types_lock);
3647 if (current_trace->set_flag)
3648 ret = current_trace->set_flag(topt->flags->val,
3650 mutex_unlock(&trace_types_lock);
3653 topt->flags->val &= ~topt->opt->bit;
3656 /* do nothing if already set */
3657 if (topt->flags->val & topt->opt->bit)
3660 mutex_lock(&trace_types_lock);
3661 if (current_trace->set_flag)
3662 ret = current_trace->set_flag(topt->flags->val,
3664 mutex_unlock(&trace_types_lock);
3667 topt->flags->val |= topt->opt->bit;
3680 static const struct file_operations trace_options_fops = {
3681 .open = tracing_open_generic,
3682 .read = trace_options_read,
3683 .write = trace_options_write,
3687 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3690 long index = (long)filp->private_data;
3693 if (trace_flags & (1 << index))
3698 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3702 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3705 long index = (long)filp->private_data;
3710 if (cnt >= sizeof(buf))
3713 if (copy_from_user(&buf, ubuf, cnt))
3718 ret = strict_strtoul(buf, 10, &val);
3724 trace_flags &= ~(1 << index);
3727 trace_flags |= 1 << index;
3739 static const struct file_operations trace_options_core_fops = {
3740 .open = tracing_open_generic,
3741 .read = trace_options_core_read,
3742 .write = trace_options_core_write,
3745 static struct dentry *trace_options_init_dentry(void)
3747 struct dentry *d_tracer;
3748 static struct dentry *t_options;
3753 d_tracer = tracing_init_dentry();
3757 t_options = debugfs_create_dir("options", d_tracer);
3759 pr_warning("Could not create debugfs directory 'options'\n");
3767 create_trace_option_file(struct trace_option_dentry *topt,
3768 struct tracer_flags *flags,
3769 struct tracer_opt *opt)
3771 struct dentry *t_options;
3772 struct dentry *entry;
3774 t_options = trace_options_init_dentry();
3778 topt->flags = flags;
3781 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3782 &trace_options_fops);
3784 topt->entry = entry;
3788 static struct trace_option_dentry *
3789 create_trace_option_files(struct tracer *tracer)
3791 struct trace_option_dentry *topts;
3792 struct tracer_flags *flags;
3793 struct tracer_opt *opts;
3799 flags = tracer->flags;
3801 if (!flags || !flags->opts)
3806 for (cnt = 0; opts[cnt].name; cnt++)
3809 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3813 for (cnt = 0; opts[cnt].name; cnt++)
3814 create_trace_option_file(&topts[cnt], flags,
3821 destroy_trace_option_files(struct trace_option_dentry *topts)
3828 for (cnt = 0; topts[cnt].opt; cnt++) {
3829 if (topts[cnt].entry)
3830 debugfs_remove(topts[cnt].entry);
3836 static struct dentry *
3837 create_trace_option_core_file(const char *option, long index)
3839 struct dentry *t_options;
3840 struct dentry *entry;
3842 t_options = trace_options_init_dentry();
3846 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3847 &trace_options_core_fops);
3852 static __init void create_trace_options_dir(void)
3854 struct dentry *t_options;
3855 struct dentry *entry;
3858 t_options = trace_options_init_dentry();
3862 for (i = 0; trace_options[i]; i++) {
3863 entry = create_trace_option_core_file(trace_options[i], i);
3865 pr_warning("Could not create debugfs %s entry\n",
3870 static __init int tracer_init_debugfs(void)
3872 struct dentry *d_tracer;
3873 struct dentry *entry;
3876 d_tracer = tracing_init_dentry();
3878 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3879 &global_trace, &tracing_ctrl_fops);
3881 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3883 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3884 NULL, &tracing_iter_fops);
3886 pr_warning("Could not create debugfs 'trace_options' entry\n");
3888 create_trace_options_dir();
3890 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3891 NULL, &tracing_cpumask_fops);
3893 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3895 entry = debugfs_create_file("trace", 0644, d_tracer,
3896 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3898 pr_warning("Could not create debugfs 'trace' entry\n");
3900 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3901 &global_trace, &show_traces_fops);
3903 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3905 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3906 &global_trace, &set_tracer_fops);
3908 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3910 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3911 &tracing_max_latency,
3912 &tracing_max_lat_fops);
3914 pr_warning("Could not create debugfs "
3915 "'tracing_max_latency' entry\n");
3917 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3918 &tracing_thresh, &tracing_max_lat_fops);
3920 pr_warning("Could not create debugfs "
3921 "'tracing_thresh' entry\n");
3922 entry = debugfs_create_file("README", 0644, d_tracer,
3923 NULL, &tracing_readme_fops);
3925 pr_warning("Could not create debugfs 'README' entry\n");
3927 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3928 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3930 pr_warning("Could not create debugfs "
3931 "'trace_pipe' entry\n");
3933 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3934 &global_trace, &tracing_entries_fops);
3936 pr_warning("Could not create debugfs "
3937 "'buffer_size_kb' entry\n");
3939 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3940 NULL, &tracing_mark_fops);
3942 pr_warning("Could not create debugfs "
3943 "'trace_marker' entry\n");
3945 #ifdef CONFIG_DYNAMIC_FTRACE
3946 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3947 &ftrace_update_tot_cnt,
3948 &tracing_dyn_info_fops);
3950 pr_warning("Could not create debugfs "
3951 "'dyn_ftrace_total_info' entry\n");
3953 #ifdef CONFIG_SYSPROF_TRACER
3954 init_tracer_sysprof_debugfs(d_tracer);
3957 for_each_tracing_cpu(cpu)
3958 tracing_init_debugfs_percpu(cpu);
3963 static int trace_panic_handler(struct notifier_block *this,
3964 unsigned long event, void *unused)
3966 if (ftrace_dump_on_oops)
3971 static struct notifier_block trace_panic_notifier = {
3972 .notifier_call = trace_panic_handler,
3974 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3977 static int trace_die_handler(struct notifier_block *self,
3983 if (ftrace_dump_on_oops)
3992 static struct notifier_block trace_die_notifier = {
3993 .notifier_call = trace_die_handler,
3998 * printk is set to max of 1024, we really don't need it that big.
3999 * Nothing should be printing 1000 characters anyway.
4001 #define TRACE_MAX_PRINT 1000
4004 * Define here KERN_TRACE so that we have one place to modify
4005 * it if we decide to change what log level the ftrace dump
4008 #define KERN_TRACE KERN_EMERG
4011 trace_printk_seq(struct trace_seq *s)
4013 /* Probably should print a warning here. */
4017 /* should be zero ended, but we are paranoid. */
4018 s->buffer[s->len] = 0;
4020 printk(KERN_TRACE "%s", s->buffer);
4025 static void __ftrace_dump(bool disable_tracing)
4027 static DEFINE_SPINLOCK(ftrace_dump_lock);
4028 /* use static because iter can be a bit big for the stack */
4029 static struct trace_iterator iter;
4030 unsigned int old_userobj;
4031 static int dump_ran;
4032 unsigned long flags;
4036 spin_lock_irqsave(&ftrace_dump_lock, flags);
4044 if (disable_tracing)
4047 for_each_tracing_cpu(cpu) {
4048 atomic_inc(&global_trace.data[cpu]->disabled);
4051 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4053 /* don't look at user memory in panic mode */
4054 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4056 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4058 /* Simulate the iterator */
4059 iter.tr = &global_trace;
4060 iter.trace = current_trace;
4061 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4064 * We need to stop all tracing on all CPUS to read the
4065 * the next buffer. This is a bit expensive, but is
4066 * not done often. We fill all what we can read,
4067 * and then release the locks again.
4070 while (!trace_empty(&iter)) {
4073 printk(KERN_TRACE "---------------------------------\n");
4077 /* reset all but tr, trace, and overruns */
4078 memset(&iter.seq, 0,
4079 sizeof(struct trace_iterator) -
4080 offsetof(struct trace_iterator, seq));
4081 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4084 if (find_next_entry_inc(&iter) != NULL) {
4085 print_trace_line(&iter);
4086 trace_consume(&iter);
4089 trace_printk_seq(&iter.seq);
4093 printk(KERN_TRACE " (ftrace buffer empty)\n");
4095 printk(KERN_TRACE "---------------------------------\n");
4097 /* Re-enable tracing if requested */
4098 if (!disable_tracing) {
4099 trace_flags |= old_userobj;
4101 for_each_tracing_cpu(cpu) {
4102 atomic_dec(&global_trace.data[cpu]->disabled);
4108 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
4111 /* By default: disable tracing after the dump */
4112 void ftrace_dump(void)
4114 __ftrace_dump(true);
4117 __init static int tracer_alloc_buffers(void)
4119 struct trace_array_cpu *data;
4124 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4127 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4128 goto out_free_buffer_mask;
4130 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4131 goto out_free_tracing_cpumask;
4133 /* To save memory, keep the ring buffer size to its minimum */
4134 if (ring_buffer_expanded)
4135 ring_buf_size = trace_buf_size;
4139 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4140 cpumask_copy(tracing_cpumask, cpu_all_mask);
4141 cpumask_clear(tracing_reader_cpumask);
4143 /* TODO: make the number of buffers hot pluggable with CPUS */
4144 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4145 TRACE_BUFFER_FLAGS);
4146 if (!global_trace.buffer) {
4147 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4149 goto out_free_cpumask;
4151 global_trace.entries = ring_buffer_size(global_trace.buffer);
4154 #ifdef CONFIG_TRACER_MAX_TRACE
4155 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4156 TRACE_BUFFER_FLAGS);
4157 if (!max_tr.buffer) {
4158 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4160 ring_buffer_free(global_trace.buffer);
4161 goto out_free_cpumask;
4163 max_tr.entries = ring_buffer_size(max_tr.buffer);
4164 WARN_ON(max_tr.entries != global_trace.entries);
4167 /* Allocate the first page for all buffers */
4168 for_each_tracing_cpu(i) {
4169 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4170 max_tr.data[i] = &per_cpu(max_data, i);
4173 trace_init_cmdlines();
4175 register_tracer(&nop_trace);
4176 current_trace = &nop_trace;
4177 #ifdef CONFIG_BOOT_TRACER
4178 register_tracer(&boot_tracer);
4180 /* All seems OK, enable tracing */
4181 tracing_disabled = 0;
4183 atomic_notifier_chain_register(&panic_notifier_list,
4184 &trace_panic_notifier);
4186 register_die_notifier(&trace_die_notifier);
4191 free_cpumask_var(tracing_reader_cpumask);
4192 out_free_tracing_cpumask:
4193 free_cpumask_var(tracing_cpumask);
4194 out_free_buffer_mask:
4195 free_cpumask_var(tracing_buffer_mask);
4200 __init static int clear_boot_tracer(void)
4203 * The default tracer at boot buffer is an init section.
4204 * This function is called in lateinit. If we did not
4205 * find the boot tracer, then clear it out, to prevent
4206 * later registration from accessing the buffer that is
4207 * about to be freed.
4209 if (!default_bootup_tracer)
4212 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4213 default_bootup_tracer);
4214 default_bootup_tracer = NULL;
4219 early_initcall(tracer_alloc_buffers);
4220 fs_initcall(tracer_init_debugfs);
4221 late_initcall(clear_boot_tracer);