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 static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
865 struct ring_buffer_event *event,
866 unsigned long flags, int pc,
869 ring_buffer_unlock_commit(tr->buffer, event);
871 ftrace_trace_stack(tr, flags, 6, pc);
872 ftrace_trace_userstack(tr, flags, pc);
878 void trace_buffer_unlock_commit(struct trace_array *tr,
879 struct ring_buffer_event *event,
880 unsigned long flags, int pc)
882 __trace_buffer_unlock_commit(tr, event, flags, pc, 1);
885 struct ring_buffer_event *
886 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
887 unsigned long flags, int pc)
889 return trace_buffer_lock_reserve(&global_trace,
890 type, len, flags, pc);
893 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
894 unsigned long flags, int pc)
896 return __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
899 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
900 unsigned long flags, int pc)
902 return __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
906 trace_function(struct trace_array *tr,
907 unsigned long ip, unsigned long parent_ip, unsigned long flags,
910 struct ring_buffer_event *event;
911 struct ftrace_entry *entry;
913 /* If we are reading the ring buffer, don't trace */
914 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
917 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
921 entry = ring_buffer_event_data(event);
923 entry->parent_ip = parent_ip;
924 ring_buffer_unlock_commit(tr->buffer, event);
927 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
928 static int __trace_graph_entry(struct trace_array *tr,
929 struct ftrace_graph_ent *trace,
933 struct ring_buffer_event *event;
934 struct ftrace_graph_ent_entry *entry;
936 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
939 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
940 sizeof(*entry), flags, pc);
943 entry = ring_buffer_event_data(event);
944 entry->graph_ent = *trace;
945 ring_buffer_unlock_commit(global_trace.buffer, event);
950 static void __trace_graph_return(struct trace_array *tr,
951 struct ftrace_graph_ret *trace,
955 struct ring_buffer_event *event;
956 struct ftrace_graph_ret_entry *entry;
958 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
961 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
962 sizeof(*entry), flags, pc);
965 entry = ring_buffer_event_data(event);
967 ring_buffer_unlock_commit(global_trace.buffer, event);
972 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
973 unsigned long ip, unsigned long parent_ip, unsigned long flags,
976 if (likely(!atomic_read(&data->disabled)))
977 trace_function(tr, ip, parent_ip, flags, pc);
980 static void __ftrace_trace_stack(struct trace_array *tr,
984 #ifdef CONFIG_STACKTRACE
985 struct ring_buffer_event *event;
986 struct stack_entry *entry;
987 struct stack_trace trace;
989 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
990 sizeof(*entry), flags, pc);
993 entry = ring_buffer_event_data(event);
994 memset(&entry->caller, 0, sizeof(entry->caller));
996 trace.nr_entries = 0;
997 trace.max_entries = FTRACE_STACK_ENTRIES;
999 trace.entries = entry->caller;
1001 save_stack_trace(&trace);
1002 ring_buffer_unlock_commit(tr->buffer, event);
1006 static void ftrace_trace_stack(struct trace_array *tr,
1007 unsigned long flags,
1010 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1013 __ftrace_trace_stack(tr, flags, skip, pc);
1016 void __trace_stack(struct trace_array *tr,
1017 unsigned long flags,
1020 __ftrace_trace_stack(tr, flags, skip, pc);
1023 static void ftrace_trace_userstack(struct trace_array *tr,
1024 unsigned long flags, int pc)
1026 #ifdef CONFIG_STACKTRACE
1027 struct ring_buffer_event *event;
1028 struct userstack_entry *entry;
1029 struct stack_trace trace;
1031 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1034 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1035 sizeof(*entry), flags, pc);
1038 entry = ring_buffer_event_data(event);
1040 memset(&entry->caller, 0, sizeof(entry->caller));
1042 trace.nr_entries = 0;
1043 trace.max_entries = FTRACE_STACK_ENTRIES;
1045 trace.entries = entry->caller;
1047 save_stack_trace_user(&trace);
1048 ring_buffer_unlock_commit(tr->buffer, event);
1053 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1055 ftrace_trace_userstack(tr, flags, preempt_count());
1060 ftrace_trace_special(void *__tr,
1061 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1064 struct ring_buffer_event *event;
1065 struct trace_array *tr = __tr;
1066 struct special_entry *entry;
1068 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1069 sizeof(*entry), 0, pc);
1072 entry = ring_buffer_event_data(event);
1076 trace_buffer_unlock_commit(tr, event, 0, pc);
1080 __trace_special(void *__tr, void *__data,
1081 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1083 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1087 tracing_sched_switch_trace(struct trace_array *tr,
1088 struct task_struct *prev,
1089 struct task_struct *next,
1090 unsigned long flags, int pc)
1092 struct ring_buffer_event *event;
1093 struct ctx_switch_entry *entry;
1095 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1096 sizeof(*entry), flags, pc);
1099 entry = ring_buffer_event_data(event);
1100 entry->prev_pid = prev->pid;
1101 entry->prev_prio = prev->prio;
1102 entry->prev_state = prev->state;
1103 entry->next_pid = next->pid;
1104 entry->next_prio = next->prio;
1105 entry->next_state = next->state;
1106 entry->next_cpu = task_cpu(next);
1107 trace_buffer_unlock_commit(tr, event, flags, pc);
1111 tracing_sched_wakeup_trace(struct trace_array *tr,
1112 struct task_struct *wakee,
1113 struct task_struct *curr,
1114 unsigned long flags, int pc)
1116 struct ring_buffer_event *event;
1117 struct ctx_switch_entry *entry;
1119 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1120 sizeof(*entry), flags, pc);
1123 entry = ring_buffer_event_data(event);
1124 entry->prev_pid = curr->pid;
1125 entry->prev_prio = curr->prio;
1126 entry->prev_state = curr->state;
1127 entry->next_pid = wakee->pid;
1128 entry->next_prio = wakee->prio;
1129 entry->next_state = wakee->state;
1130 entry->next_cpu = task_cpu(wakee);
1132 ring_buffer_unlock_commit(tr->buffer, event);
1133 ftrace_trace_stack(tr, flags, 6, pc);
1134 ftrace_trace_userstack(tr, flags, pc);
1138 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1140 struct trace_array *tr = &global_trace;
1141 struct trace_array_cpu *data;
1142 unsigned long flags;
1146 if (tracing_disabled)
1149 pc = preempt_count();
1150 local_irq_save(flags);
1151 cpu = raw_smp_processor_id();
1152 data = tr->data[cpu];
1154 if (likely(atomic_inc_return(&data->disabled) == 1))
1155 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1157 atomic_dec(&data->disabled);
1158 local_irq_restore(flags);
1161 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1162 int trace_graph_entry(struct ftrace_graph_ent *trace)
1164 struct trace_array *tr = &global_trace;
1165 struct trace_array_cpu *data;
1166 unsigned long flags;
1172 if (!ftrace_trace_task(current))
1175 if (!ftrace_graph_addr(trace->func))
1178 local_irq_save(flags);
1179 cpu = raw_smp_processor_id();
1180 data = tr->data[cpu];
1181 disabled = atomic_inc_return(&data->disabled);
1182 if (likely(disabled == 1)) {
1183 pc = preempt_count();
1184 ret = __trace_graph_entry(tr, trace, flags, pc);
1188 /* Only do the atomic if it is not already set */
1189 if (!test_tsk_trace_graph(current))
1190 set_tsk_trace_graph(current);
1192 atomic_dec(&data->disabled);
1193 local_irq_restore(flags);
1198 void trace_graph_return(struct ftrace_graph_ret *trace)
1200 struct trace_array *tr = &global_trace;
1201 struct trace_array_cpu *data;
1202 unsigned long flags;
1207 local_irq_save(flags);
1208 cpu = raw_smp_processor_id();
1209 data = tr->data[cpu];
1210 disabled = atomic_inc_return(&data->disabled);
1211 if (likely(disabled == 1)) {
1212 pc = preempt_count();
1213 __trace_graph_return(tr, trace, flags, pc);
1216 clear_tsk_trace_graph(current);
1217 atomic_dec(&data->disabled);
1218 local_irq_restore(flags);
1220 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1224 * trace_vbprintk - write binary msg to tracing buffer
1227 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1229 static raw_spinlock_t trace_buf_lock =
1230 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1231 static u32 trace_buf[TRACE_BUF_SIZE];
1233 struct ring_buffer_event *event;
1234 struct trace_array *tr = &global_trace;
1235 struct trace_array_cpu *data;
1236 struct bprint_entry *entry;
1237 unsigned long flags;
1239 int cpu, len = 0, size, pc;
1241 if (unlikely(tracing_selftest_running || tracing_disabled))
1244 /* Don't pollute graph traces with trace_vprintk internals */
1245 pause_graph_tracing();
1247 pc = preempt_count();
1248 resched = ftrace_preempt_disable();
1249 cpu = raw_smp_processor_id();
1250 data = tr->data[cpu];
1252 if (unlikely(atomic_read(&data->disabled)))
1255 /* Lockdep uses trace_printk for lock tracing */
1256 local_irq_save(flags);
1257 __raw_spin_lock(&trace_buf_lock);
1258 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1260 if (len > TRACE_BUF_SIZE || len < 0)
1263 size = sizeof(*entry) + sizeof(u32) * len;
1264 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1267 entry = ring_buffer_event_data(event);
1271 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1272 ring_buffer_unlock_commit(tr->buffer, event);
1275 __raw_spin_unlock(&trace_buf_lock);
1276 local_irq_restore(flags);
1279 ftrace_preempt_enable(resched);
1280 unpause_graph_tracing();
1284 EXPORT_SYMBOL_GPL(trace_vbprintk);
1286 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1288 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1289 static char trace_buf[TRACE_BUF_SIZE];
1291 struct ring_buffer_event *event;
1292 struct trace_array *tr = &global_trace;
1293 struct trace_array_cpu *data;
1294 int cpu, len = 0, size, pc;
1295 struct print_entry *entry;
1296 unsigned long irq_flags;
1298 if (tracing_disabled || tracing_selftest_running)
1301 pc = preempt_count();
1302 preempt_disable_notrace();
1303 cpu = raw_smp_processor_id();
1304 data = tr->data[cpu];
1306 if (unlikely(atomic_read(&data->disabled)))
1309 pause_graph_tracing();
1310 raw_local_irq_save(irq_flags);
1311 __raw_spin_lock(&trace_buf_lock);
1312 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1314 len = min(len, TRACE_BUF_SIZE-1);
1317 size = sizeof(*entry) + len + 1;
1318 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1321 entry = ring_buffer_event_data(event);
1324 memcpy(&entry->buf, trace_buf, len);
1325 entry->buf[len] = 0;
1326 ring_buffer_unlock_commit(tr->buffer, event);
1329 __raw_spin_unlock(&trace_buf_lock);
1330 raw_local_irq_restore(irq_flags);
1331 unpause_graph_tracing();
1333 preempt_enable_notrace();
1337 EXPORT_SYMBOL_GPL(trace_vprintk);
1339 enum trace_file_type {
1340 TRACE_FILE_LAT_FMT = 1,
1341 TRACE_FILE_ANNOTATE = 2,
1344 static void trace_iterator_increment(struct trace_iterator *iter)
1346 /* Don't allow ftrace to trace into the ring buffers */
1347 ftrace_disable_cpu();
1350 if (iter->buffer_iter[iter->cpu])
1351 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1353 ftrace_enable_cpu();
1356 static struct trace_entry *
1357 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1359 struct ring_buffer_event *event;
1360 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1362 /* Don't allow ftrace to trace into the ring buffers */
1363 ftrace_disable_cpu();
1366 event = ring_buffer_iter_peek(buf_iter, ts);
1368 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1370 ftrace_enable_cpu();
1372 return event ? ring_buffer_event_data(event) : NULL;
1375 static struct trace_entry *
1376 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1378 struct ring_buffer *buffer = iter->tr->buffer;
1379 struct trace_entry *ent, *next = NULL;
1380 int cpu_file = iter->cpu_file;
1381 u64 next_ts = 0, ts;
1386 * If we are in a per_cpu trace file, don't bother by iterating over
1387 * all cpu and peek directly.
1389 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1390 if (ring_buffer_empty_cpu(buffer, cpu_file))
1392 ent = peek_next_entry(iter, cpu_file, ent_ts);
1394 *ent_cpu = cpu_file;
1399 for_each_tracing_cpu(cpu) {
1401 if (ring_buffer_empty_cpu(buffer, cpu))
1404 ent = peek_next_entry(iter, cpu, &ts);
1407 * Pick the entry with the smallest timestamp:
1409 if (ent && (!next || ts < next_ts)) {
1417 *ent_cpu = next_cpu;
1425 /* Find the next real entry, without updating the iterator itself */
1426 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1427 int *ent_cpu, u64 *ent_ts)
1429 return __find_next_entry(iter, ent_cpu, ent_ts);
1432 /* Find the next real entry, and increment the iterator to the next entry */
1433 static void *find_next_entry_inc(struct trace_iterator *iter)
1435 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1438 trace_iterator_increment(iter);
1440 return iter->ent ? iter : NULL;
1443 static void trace_consume(struct trace_iterator *iter)
1445 /* Don't allow ftrace to trace into the ring buffers */
1446 ftrace_disable_cpu();
1447 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1448 ftrace_enable_cpu();
1451 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1453 struct trace_iterator *iter = m->private;
1459 /* can't go backwards */
1464 ent = find_next_entry_inc(iter);
1468 while (ent && iter->idx < i)
1469 ent = find_next_entry_inc(iter);
1477 * No necessary locking here. The worst thing which can
1478 * happen is loosing events consumed at the same time
1479 * by a trace_pipe reader.
1480 * Other than that, we don't risk to crash the ring buffer
1481 * because it serializes the readers.
1483 * The current tracer is copied to avoid a global locking
1486 static void *s_start(struct seq_file *m, loff_t *pos)
1488 struct trace_iterator *iter = m->private;
1489 static struct tracer *old_tracer;
1490 int cpu_file = iter->cpu_file;
1495 /* copy the tracer to avoid using a global lock all around */
1496 mutex_lock(&trace_types_lock);
1497 if (unlikely(old_tracer != current_trace && current_trace)) {
1498 old_tracer = current_trace;
1499 *iter->trace = *current_trace;
1501 mutex_unlock(&trace_types_lock);
1503 atomic_inc(&trace_record_cmdline_disabled);
1505 if (*pos != iter->pos) {
1510 ftrace_disable_cpu();
1512 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1513 for_each_tracing_cpu(cpu)
1514 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1516 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1519 ftrace_enable_cpu();
1521 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1526 p = s_next(m, p, &l);
1532 static void s_stop(struct seq_file *m, void *p)
1534 atomic_dec(&trace_record_cmdline_disabled);
1537 static void print_lat_help_header(struct seq_file *m)
1539 seq_puts(m, "# _------=> CPU# \n");
1540 seq_puts(m, "# / _-----=> irqs-off \n");
1541 seq_puts(m, "# | / _----=> need-resched \n");
1542 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1543 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1544 seq_puts(m, "# |||| / \n");
1545 seq_puts(m, "# ||||| delay \n");
1546 seq_puts(m, "# cmd pid ||||| time | caller \n");
1547 seq_puts(m, "# \\ / ||||| \\ | / \n");
1550 static void print_func_help_header(struct seq_file *m)
1552 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1553 seq_puts(m, "# | | | | |\n");
1558 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1560 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1561 struct trace_array *tr = iter->tr;
1562 struct trace_array_cpu *data = tr->data[tr->cpu];
1563 struct tracer *type = current_trace;
1564 unsigned long total;
1565 unsigned long entries;
1566 const char *name = "preemption";
1571 entries = ring_buffer_entries(iter->tr->buffer);
1573 ring_buffer_overruns(iter->tr->buffer);
1575 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1577 seq_puts(m, "# -----------------------------------"
1578 "---------------------------------\n");
1579 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1580 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1581 nsecs_to_usecs(data->saved_latency),
1585 #if defined(CONFIG_PREEMPT_NONE)
1587 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1589 #elif defined(CONFIG_PREEMPT)
1594 /* These are reserved for later use */
1597 seq_printf(m, " #P:%d)\n", num_online_cpus());
1601 seq_puts(m, "# -----------------\n");
1602 seq_printf(m, "# | task: %.16s-%d "
1603 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1604 data->comm, data->pid, data->uid, data->nice,
1605 data->policy, data->rt_priority);
1606 seq_puts(m, "# -----------------\n");
1608 if (data->critical_start) {
1609 seq_puts(m, "# => started at: ");
1610 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1611 trace_print_seq(m, &iter->seq);
1612 seq_puts(m, "\n# => ended at: ");
1613 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1614 trace_print_seq(m, &iter->seq);
1621 static void test_cpu_buff_start(struct trace_iterator *iter)
1623 struct trace_seq *s = &iter->seq;
1625 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1628 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1631 if (cpumask_test_cpu(iter->cpu, iter->started))
1634 cpumask_set_cpu(iter->cpu, iter->started);
1635 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1638 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1640 struct trace_seq *s = &iter->seq;
1641 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1642 struct trace_entry *entry;
1643 struct trace_event *event;
1647 test_cpu_buff_start(iter);
1649 event = ftrace_find_event(entry->type);
1651 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1652 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1653 if (!trace_print_lat_context(iter))
1656 if (!trace_print_context(iter))
1662 return event->trace(iter, sym_flags);
1664 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1667 return TRACE_TYPE_HANDLED;
1669 return TRACE_TYPE_PARTIAL_LINE;
1672 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1674 struct trace_seq *s = &iter->seq;
1675 struct trace_entry *entry;
1676 struct trace_event *event;
1680 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1681 if (!trace_seq_printf(s, "%d %d %llu ",
1682 entry->pid, iter->cpu, iter->ts))
1686 event = ftrace_find_event(entry->type);
1688 return event->raw(iter, 0);
1690 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1693 return TRACE_TYPE_HANDLED;
1695 return TRACE_TYPE_PARTIAL_LINE;
1698 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1700 struct trace_seq *s = &iter->seq;
1701 unsigned char newline = '\n';
1702 struct trace_entry *entry;
1703 struct trace_event *event;
1707 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1708 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1709 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1710 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1713 event = ftrace_find_event(entry->type);
1715 enum print_line_t ret = event->hex(iter, 0);
1716 if (ret != TRACE_TYPE_HANDLED)
1720 SEQ_PUT_FIELD_RET(s, newline);
1722 return TRACE_TYPE_HANDLED;
1725 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1727 struct trace_seq *s = &iter->seq;
1728 struct trace_entry *entry;
1729 struct trace_event *event;
1733 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1734 SEQ_PUT_FIELD_RET(s, entry->pid);
1735 SEQ_PUT_FIELD_RET(s, iter->cpu);
1736 SEQ_PUT_FIELD_RET(s, iter->ts);
1739 event = ftrace_find_event(entry->type);
1740 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1743 static int trace_empty(struct trace_iterator *iter)
1747 /* If we are looking at one CPU buffer, only check that one */
1748 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1749 cpu = iter->cpu_file;
1750 if (iter->buffer_iter[cpu]) {
1751 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1754 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1760 for_each_tracing_cpu(cpu) {
1761 if (iter->buffer_iter[cpu]) {
1762 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1765 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1773 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1775 enum print_line_t ret;
1777 if (iter->trace && iter->trace->print_line) {
1778 ret = iter->trace->print_line(iter);
1779 if (ret != TRACE_TYPE_UNHANDLED)
1783 if (iter->ent->type == TRACE_BPRINT &&
1784 trace_flags & TRACE_ITER_PRINTK &&
1785 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1786 return trace_print_bprintk_msg_only(iter);
1788 if (iter->ent->type == TRACE_PRINT &&
1789 trace_flags & TRACE_ITER_PRINTK &&
1790 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1791 return trace_print_printk_msg_only(iter);
1793 if (trace_flags & TRACE_ITER_BIN)
1794 return print_bin_fmt(iter);
1796 if (trace_flags & TRACE_ITER_HEX)
1797 return print_hex_fmt(iter);
1799 if (trace_flags & TRACE_ITER_RAW)
1800 return print_raw_fmt(iter);
1802 return print_trace_fmt(iter);
1805 static int s_show(struct seq_file *m, void *v)
1807 struct trace_iterator *iter = v;
1809 if (iter->ent == NULL) {
1811 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1814 if (iter->trace && iter->trace->print_header)
1815 iter->trace->print_header(m);
1816 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1817 /* print nothing if the buffers are empty */
1818 if (trace_empty(iter))
1820 print_trace_header(m, iter);
1821 if (!(trace_flags & TRACE_ITER_VERBOSE))
1822 print_lat_help_header(m);
1824 if (!(trace_flags & TRACE_ITER_VERBOSE))
1825 print_func_help_header(m);
1828 print_trace_line(iter);
1829 trace_print_seq(m, &iter->seq);
1835 static struct seq_operations tracer_seq_ops = {
1842 static struct trace_iterator *
1843 __tracing_open(struct inode *inode, struct file *file)
1845 long cpu_file = (long) inode->i_private;
1846 void *fail_ret = ERR_PTR(-ENOMEM);
1847 struct trace_iterator *iter;
1851 if (tracing_disabled)
1852 return ERR_PTR(-ENODEV);
1854 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1856 return ERR_PTR(-ENOMEM);
1859 * We make a copy of the current tracer to avoid concurrent
1860 * changes on it while we are reading.
1862 mutex_lock(&trace_types_lock);
1863 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1868 *iter->trace = *current_trace;
1870 if (current_trace && current_trace->print_max)
1873 iter->tr = &global_trace;
1875 mutex_init(&iter->mutex);
1876 iter->cpu_file = cpu_file;
1878 /* Notify the tracer early; before we stop tracing. */
1879 if (iter->trace && iter->trace->open)
1880 iter->trace->open(iter);
1882 /* Annotate start of buffers if we had overruns */
1883 if (ring_buffer_overruns(iter->tr->buffer))
1884 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1886 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1887 for_each_tracing_cpu(cpu) {
1889 iter->buffer_iter[cpu] =
1890 ring_buffer_read_start(iter->tr->buffer, cpu);
1893 cpu = iter->cpu_file;
1894 iter->buffer_iter[cpu] =
1895 ring_buffer_read_start(iter->tr->buffer, cpu);
1898 /* TODO stop tracer */
1899 ret = seq_open(file, &tracer_seq_ops);
1901 fail_ret = ERR_PTR(ret);
1905 m = file->private_data;
1908 /* stop the trace while dumping */
1911 mutex_unlock(&trace_types_lock);
1916 for_each_tracing_cpu(cpu) {
1917 if (iter->buffer_iter[cpu])
1918 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1921 mutex_unlock(&trace_types_lock);
1928 int tracing_open_generic(struct inode *inode, struct file *filp)
1930 if (tracing_disabled)
1933 filp->private_data = inode->i_private;
1937 static int tracing_release(struct inode *inode, struct file *file)
1939 struct seq_file *m = (struct seq_file *)file->private_data;
1940 struct trace_iterator *iter;
1943 if (!(file->f_mode & FMODE_READ))
1948 mutex_lock(&trace_types_lock);
1949 for_each_tracing_cpu(cpu) {
1950 if (iter->buffer_iter[cpu])
1951 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1954 if (iter->trace && iter->trace->close)
1955 iter->trace->close(iter);
1957 /* reenable tracing if it was previously enabled */
1959 mutex_unlock(&trace_types_lock);
1961 seq_release(inode, file);
1962 mutex_destroy(&iter->mutex);
1968 static int tracing_open(struct inode *inode, struct file *file)
1970 struct trace_iterator *iter;
1973 /* If this file was open for write, then erase contents */
1974 if ((file->f_mode & FMODE_WRITE) &&
1975 !(file->f_flags & O_APPEND)) {
1976 long cpu = (long) inode->i_private;
1978 if (cpu == TRACE_PIPE_ALL_CPU)
1979 tracing_reset_online_cpus(&global_trace);
1981 tracing_reset(&global_trace, cpu);
1984 if (file->f_mode & FMODE_READ) {
1985 iter = __tracing_open(inode, file);
1987 ret = PTR_ERR(iter);
1988 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1989 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1995 t_next(struct seq_file *m, void *v, loff_t *pos)
1997 struct tracer *t = m->private;
2009 static void *t_start(struct seq_file *m, loff_t *pos)
2011 struct tracer *t = m->private;
2014 mutex_lock(&trace_types_lock);
2015 for (; t && l < *pos; t = t_next(m, t, &l))
2021 static void t_stop(struct seq_file *m, void *p)
2023 mutex_unlock(&trace_types_lock);
2026 static int t_show(struct seq_file *m, void *v)
2028 struct tracer *t = v;
2033 seq_printf(m, "%s", t->name);
2042 static struct seq_operations show_traces_seq_ops = {
2049 static int show_traces_open(struct inode *inode, struct file *file)
2053 if (tracing_disabled)
2056 ret = seq_open(file, &show_traces_seq_ops);
2058 struct seq_file *m = file->private_data;
2059 m->private = trace_types;
2066 tracing_write_stub(struct file *filp, const char __user *ubuf,
2067 size_t count, loff_t *ppos)
2072 static const struct file_operations tracing_fops = {
2073 .open = tracing_open,
2075 .write = tracing_write_stub,
2076 .llseek = seq_lseek,
2077 .release = tracing_release,
2080 static const struct file_operations show_traces_fops = {
2081 .open = show_traces_open,
2083 .release = seq_release,
2087 * Only trace on a CPU if the bitmask is set:
2089 static cpumask_var_t tracing_cpumask;
2092 * The tracer itself will not take this lock, but still we want
2093 * to provide a consistent cpumask to user-space:
2095 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2098 * Temporary storage for the character representation of the
2099 * CPU bitmask (and one more byte for the newline):
2101 static char mask_str[NR_CPUS + 1];
2104 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2105 size_t count, loff_t *ppos)
2109 mutex_lock(&tracing_cpumask_update_lock);
2111 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2112 if (count - len < 2) {
2116 len += sprintf(mask_str + len, "\n");
2117 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2120 mutex_unlock(&tracing_cpumask_update_lock);
2126 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2127 size_t count, loff_t *ppos)
2130 cpumask_var_t tracing_cpumask_new;
2132 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2135 mutex_lock(&tracing_cpumask_update_lock);
2136 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2140 local_irq_disable();
2141 __raw_spin_lock(&ftrace_max_lock);
2142 for_each_tracing_cpu(cpu) {
2144 * Increase/decrease the disabled counter if we are
2145 * about to flip a bit in the cpumask:
2147 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2148 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2149 atomic_inc(&global_trace.data[cpu]->disabled);
2151 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2152 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2153 atomic_dec(&global_trace.data[cpu]->disabled);
2156 __raw_spin_unlock(&ftrace_max_lock);
2159 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2161 mutex_unlock(&tracing_cpumask_update_lock);
2162 free_cpumask_var(tracing_cpumask_new);
2167 mutex_unlock(&tracing_cpumask_update_lock);
2168 free_cpumask_var(tracing_cpumask);
2173 static const struct file_operations tracing_cpumask_fops = {
2174 .open = tracing_open_generic,
2175 .read = tracing_cpumask_read,
2176 .write = tracing_cpumask_write,
2180 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2181 size_t cnt, loff_t *ppos)
2183 struct tracer_opt *trace_opts;
2191 /* calculate max size */
2192 for (i = 0; trace_options[i]; i++) {
2193 len += strlen(trace_options[i]);
2194 len += 3; /* "no" and newline */
2197 mutex_lock(&trace_types_lock);
2198 tracer_flags = current_trace->flags->val;
2199 trace_opts = current_trace->flags->opts;
2202 * Increase the size with names of options specific
2203 * of the current tracer.
2205 for (i = 0; trace_opts[i].name; i++) {
2206 len += strlen(trace_opts[i].name);
2207 len += 3; /* "no" and newline */
2210 /* +2 for \n and \0 */
2211 buf = kmalloc(len + 2, GFP_KERNEL);
2213 mutex_unlock(&trace_types_lock);
2217 for (i = 0; trace_options[i]; i++) {
2218 if (trace_flags & (1 << i))
2219 r += sprintf(buf + r, "%s\n", trace_options[i]);
2221 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2224 for (i = 0; trace_opts[i].name; i++) {
2225 if (tracer_flags & trace_opts[i].bit)
2226 r += sprintf(buf + r, "%s\n",
2227 trace_opts[i].name);
2229 r += sprintf(buf + r, "no%s\n",
2230 trace_opts[i].name);
2232 mutex_unlock(&trace_types_lock);
2234 WARN_ON(r >= len + 2);
2236 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2242 /* Try to assign a tracer specific option */
2243 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2245 struct tracer_flags *trace_flags = trace->flags;
2246 struct tracer_opt *opts = NULL;
2250 for (i = 0; trace_flags->opts[i].name; i++) {
2251 opts = &trace_flags->opts[i];
2252 len = strlen(opts->name);
2254 if (strncmp(cmp, opts->name, len) == 0) {
2255 ret = trace->set_flag(trace_flags->val,
2261 if (!trace_flags->opts[i].name)
2264 /* Refused to handle */
2269 trace_flags->val &= ~opts->bit;
2271 trace_flags->val |= opts->bit;
2276 static void set_tracer_flags(unsigned int mask, int enabled)
2278 /* do nothing if flag is already set */
2279 if (!!(trace_flags & mask) == !!enabled)
2283 trace_flags |= mask;
2285 trace_flags &= ~mask;
2287 if (mask == TRACE_ITER_GLOBAL_CLK) {
2291 func = trace_clock_global;
2293 func = trace_clock_local;
2295 mutex_lock(&trace_types_lock);
2296 ring_buffer_set_clock(global_trace.buffer, func);
2299 ring_buffer_set_clock(max_tr.buffer, func);
2300 mutex_unlock(&trace_types_lock);
2305 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2306 size_t cnt, loff_t *ppos)
2314 if (cnt >= sizeof(buf))
2317 if (copy_from_user(&buf, ubuf, cnt))
2322 if (strncmp(buf, "no", 2) == 0) {
2327 for (i = 0; trace_options[i]; i++) {
2328 int len = strlen(trace_options[i]);
2330 if (strncmp(cmp, trace_options[i], len) == 0) {
2331 set_tracer_flags(1 << i, !neg);
2336 /* If no option could be set, test the specific tracer options */
2337 if (!trace_options[i]) {
2338 mutex_lock(&trace_types_lock);
2339 ret = set_tracer_option(current_trace, cmp, neg);
2340 mutex_unlock(&trace_types_lock);
2350 static const struct file_operations tracing_iter_fops = {
2351 .open = tracing_open_generic,
2352 .read = tracing_trace_options_read,
2353 .write = tracing_trace_options_write,
2356 static const char readme_msg[] =
2357 "tracing mini-HOWTO:\n\n"
2359 "# mount -t debugfs nodev /debug\n\n"
2360 "# cat /debug/tracing/available_tracers\n"
2361 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2362 "# cat /debug/tracing/current_tracer\n"
2364 "# echo sched_switch > /debug/tracing/current_tracer\n"
2365 "# cat /debug/tracing/current_tracer\n"
2367 "# cat /debug/tracing/trace_options\n"
2368 "noprint-parent nosym-offset nosym-addr noverbose\n"
2369 "# echo print-parent > /debug/tracing/trace_options\n"
2370 "# echo 1 > /debug/tracing/tracing_enabled\n"
2371 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2372 "echo 0 > /debug/tracing/tracing_enabled\n"
2376 tracing_readme_read(struct file *filp, char __user *ubuf,
2377 size_t cnt, loff_t *ppos)
2379 return simple_read_from_buffer(ubuf, cnt, ppos,
2380 readme_msg, strlen(readme_msg));
2383 static const struct file_operations tracing_readme_fops = {
2384 .open = tracing_open_generic,
2385 .read = tracing_readme_read,
2389 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2390 size_t cnt, loff_t *ppos)
2395 r = sprintf(buf, "%u\n", tracer_enabled);
2396 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2400 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2401 size_t cnt, loff_t *ppos)
2403 struct trace_array *tr = filp->private_data;
2408 if (cnt >= sizeof(buf))
2411 if (copy_from_user(&buf, ubuf, cnt))
2416 ret = strict_strtoul(buf, 10, &val);
2422 mutex_lock(&trace_types_lock);
2423 if (tracer_enabled ^ val) {
2426 if (current_trace->start)
2427 current_trace->start(tr);
2432 if (current_trace->stop)
2433 current_trace->stop(tr);
2436 mutex_unlock(&trace_types_lock);
2444 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2445 size_t cnt, loff_t *ppos)
2447 char buf[max_tracer_type_len+2];
2450 mutex_lock(&trace_types_lock);
2452 r = sprintf(buf, "%s\n", current_trace->name);
2454 r = sprintf(buf, "\n");
2455 mutex_unlock(&trace_types_lock);
2457 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2460 int tracer_init(struct tracer *t, struct trace_array *tr)
2462 tracing_reset_online_cpus(tr);
2466 static int tracing_resize_ring_buffer(unsigned long size)
2471 * If kernel or user changes the size of the ring buffer
2472 * we use the size that was given, and we can forget about
2473 * expanding it later.
2475 ring_buffer_expanded = 1;
2477 ret = ring_buffer_resize(global_trace.buffer, size);
2481 ret = ring_buffer_resize(max_tr.buffer, size);
2485 r = ring_buffer_resize(global_trace.buffer,
2486 global_trace.entries);
2489 * AARGH! We are left with different
2490 * size max buffer!!!!
2491 * The max buffer is our "snapshot" buffer.
2492 * When a tracer needs a snapshot (one of the
2493 * latency tracers), it swaps the max buffer
2494 * with the saved snap shot. We succeeded to
2495 * update the size of the main buffer, but failed to
2496 * update the size of the max buffer. But when we tried
2497 * to reset the main buffer to the original size, we
2498 * failed there too. This is very unlikely to
2499 * happen, but if it does, warn and kill all
2503 tracing_disabled = 1;
2508 global_trace.entries = size;
2514 * tracing_update_buffers - used by tracing facility to expand ring buffers
2516 * To save on memory when the tracing is never used on a system with it
2517 * configured in. The ring buffers are set to a minimum size. But once
2518 * a user starts to use the tracing facility, then they need to grow
2519 * to their default size.
2521 * This function is to be called when a tracer is about to be used.
2523 int tracing_update_buffers(void)
2527 mutex_lock(&trace_types_lock);
2528 if (!ring_buffer_expanded)
2529 ret = tracing_resize_ring_buffer(trace_buf_size);
2530 mutex_unlock(&trace_types_lock);
2535 struct trace_option_dentry;
2537 static struct trace_option_dentry *
2538 create_trace_option_files(struct tracer *tracer);
2541 destroy_trace_option_files(struct trace_option_dentry *topts);
2543 static int tracing_set_tracer(const char *buf)
2545 static struct trace_option_dentry *topts;
2546 struct trace_array *tr = &global_trace;
2550 mutex_lock(&trace_types_lock);
2552 if (!ring_buffer_expanded) {
2553 ret = tracing_resize_ring_buffer(trace_buf_size);
2559 for (t = trace_types; t; t = t->next) {
2560 if (strcmp(t->name, buf) == 0)
2567 if (t == current_trace)
2570 trace_branch_disable();
2571 if (current_trace && current_trace->reset)
2572 current_trace->reset(tr);
2574 destroy_trace_option_files(topts);
2578 topts = create_trace_option_files(current_trace);
2581 ret = tracer_init(t, tr);
2586 trace_branch_enable(tr);
2588 mutex_unlock(&trace_types_lock);
2594 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2595 size_t cnt, loff_t *ppos)
2597 char buf[max_tracer_type_len+1];
2604 if (cnt > max_tracer_type_len)
2605 cnt = max_tracer_type_len;
2607 if (copy_from_user(&buf, ubuf, cnt))
2612 /* strip ending whitespace. */
2613 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2616 err = tracing_set_tracer(buf);
2626 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2627 size_t cnt, loff_t *ppos)
2629 unsigned long *ptr = filp->private_data;
2633 r = snprintf(buf, sizeof(buf), "%ld\n",
2634 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2635 if (r > sizeof(buf))
2637 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2641 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2642 size_t cnt, loff_t *ppos)
2644 unsigned long *ptr = filp->private_data;
2649 if (cnt >= sizeof(buf))
2652 if (copy_from_user(&buf, ubuf, cnt))
2657 ret = strict_strtoul(buf, 10, &val);
2666 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2668 long cpu_file = (long) inode->i_private;
2669 struct trace_iterator *iter;
2672 if (tracing_disabled)
2675 mutex_lock(&trace_types_lock);
2677 /* We only allow one reader per cpu */
2678 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2679 if (!cpumask_empty(tracing_reader_cpumask)) {
2683 cpumask_setall(tracing_reader_cpumask);
2685 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2686 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2693 /* create a buffer to store the information to pass to userspace */
2694 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2701 * We make a copy of the current tracer to avoid concurrent
2702 * changes on it while we are reading.
2704 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2710 *iter->trace = *current_trace;
2712 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2717 /* trace pipe does not show start of buffer */
2718 cpumask_setall(iter->started);
2720 iter->cpu_file = cpu_file;
2721 iter->tr = &global_trace;
2722 mutex_init(&iter->mutex);
2723 filp->private_data = iter;
2725 if (iter->trace->pipe_open)
2726 iter->trace->pipe_open(iter);
2729 mutex_unlock(&trace_types_lock);
2735 mutex_unlock(&trace_types_lock);
2739 static int tracing_release_pipe(struct inode *inode, struct file *file)
2741 struct trace_iterator *iter = file->private_data;
2743 mutex_lock(&trace_types_lock);
2745 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2746 cpumask_clear(tracing_reader_cpumask);
2748 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2750 mutex_unlock(&trace_types_lock);
2752 free_cpumask_var(iter->started);
2753 mutex_destroy(&iter->mutex);
2761 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2763 struct trace_iterator *iter = filp->private_data;
2765 if (trace_flags & TRACE_ITER_BLOCK) {
2767 * Always select as readable when in blocking mode
2769 return POLLIN | POLLRDNORM;
2771 if (!trace_empty(iter))
2772 return POLLIN | POLLRDNORM;
2773 poll_wait(filp, &trace_wait, poll_table);
2774 if (!trace_empty(iter))
2775 return POLLIN | POLLRDNORM;
2782 void default_wait_pipe(struct trace_iterator *iter)
2786 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2788 if (trace_empty(iter))
2791 finish_wait(&trace_wait, &wait);
2795 * This is a make-shift waitqueue.
2796 * A tracer might use this callback on some rare cases:
2798 * 1) the current tracer might hold the runqueue lock when it wakes up
2799 * a reader, hence a deadlock (sched, function, and function graph tracers)
2800 * 2) the function tracers, trace all functions, we don't want
2801 * the overhead of calling wake_up and friends
2802 * (and tracing them too)
2804 * Anyway, this is really very primitive wakeup.
2806 void poll_wait_pipe(struct trace_iterator *iter)
2808 set_current_state(TASK_INTERRUPTIBLE);
2809 /* sleep for 100 msecs, and try again. */
2810 schedule_timeout(HZ / 10);
2813 /* Must be called with trace_types_lock mutex held. */
2814 static int tracing_wait_pipe(struct file *filp)
2816 struct trace_iterator *iter = filp->private_data;
2818 while (trace_empty(iter)) {
2820 if ((filp->f_flags & O_NONBLOCK)) {
2824 mutex_unlock(&iter->mutex);
2826 iter->trace->wait_pipe(iter);
2828 mutex_lock(&iter->mutex);
2830 if (signal_pending(current))
2834 * We block until we read something and tracing is disabled.
2835 * We still block if tracing is disabled, but we have never
2836 * read anything. This allows a user to cat this file, and
2837 * then enable tracing. But after we have read something,
2838 * we give an EOF when tracing is again disabled.
2840 * iter->pos will be 0 if we haven't read anything.
2842 if (!tracer_enabled && iter->pos)
2853 tracing_read_pipe(struct file *filp, char __user *ubuf,
2854 size_t cnt, loff_t *ppos)
2856 struct trace_iterator *iter = filp->private_data;
2857 static struct tracer *old_tracer;
2860 /* return any leftover data */
2861 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2865 trace_seq_init(&iter->seq);
2867 /* copy the tracer to avoid using a global lock all around */
2868 mutex_lock(&trace_types_lock);
2869 if (unlikely(old_tracer != current_trace && current_trace)) {
2870 old_tracer = current_trace;
2871 *iter->trace = *current_trace;
2873 mutex_unlock(&trace_types_lock);
2876 * Avoid more than one consumer on a single file descriptor
2877 * This is just a matter of traces coherency, the ring buffer itself
2880 mutex_lock(&iter->mutex);
2881 if (iter->trace->read) {
2882 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2888 sret = tracing_wait_pipe(filp);
2892 /* stop when tracing is finished */
2893 if (trace_empty(iter)) {
2898 if (cnt >= PAGE_SIZE)
2899 cnt = PAGE_SIZE - 1;
2901 /* reset all but tr, trace, and overruns */
2902 memset(&iter->seq, 0,
2903 sizeof(struct trace_iterator) -
2904 offsetof(struct trace_iterator, seq));
2907 while (find_next_entry_inc(iter) != NULL) {
2908 enum print_line_t ret;
2909 int len = iter->seq.len;
2911 ret = print_trace_line(iter);
2912 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2913 /* don't print partial lines */
2914 iter->seq.len = len;
2917 if (ret != TRACE_TYPE_NO_CONSUME)
2918 trace_consume(iter);
2920 if (iter->seq.len >= cnt)
2924 /* Now copy what we have to the user */
2925 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2926 if (iter->seq.readpos >= iter->seq.len)
2927 trace_seq_init(&iter->seq);
2930 * If there was nothing to send to user, inspite of consuming trace
2931 * entries, go back to wait for more entries.
2937 mutex_unlock(&iter->mutex);
2942 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2943 struct pipe_buffer *buf)
2945 __free_page(buf->page);
2948 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2951 __free_page(spd->pages[idx]);
2954 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2956 .map = generic_pipe_buf_map,
2957 .unmap = generic_pipe_buf_unmap,
2958 .confirm = generic_pipe_buf_confirm,
2959 .release = tracing_pipe_buf_release,
2960 .steal = generic_pipe_buf_steal,
2961 .get = generic_pipe_buf_get,
2965 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2970 /* Seq buffer is page-sized, exactly what we need. */
2972 count = iter->seq.len;
2973 ret = print_trace_line(iter);
2974 count = iter->seq.len - count;
2977 iter->seq.len -= count;
2980 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2981 iter->seq.len -= count;
2985 trace_consume(iter);
2987 if (!find_next_entry_inc(iter)) {
2997 static ssize_t tracing_splice_read_pipe(struct file *filp,
2999 struct pipe_inode_info *pipe,
3003 struct page *pages[PIPE_BUFFERS];
3004 struct partial_page partial[PIPE_BUFFERS];
3005 struct trace_iterator *iter = filp->private_data;
3006 struct splice_pipe_desc spd = {
3009 .nr_pages = 0, /* This gets updated below. */
3011 .ops = &tracing_pipe_buf_ops,
3012 .spd_release = tracing_spd_release_pipe,
3014 static struct tracer *old_tracer;
3019 /* copy the tracer to avoid using a global lock all around */
3020 mutex_lock(&trace_types_lock);
3021 if (unlikely(old_tracer != current_trace && current_trace)) {
3022 old_tracer = current_trace;
3023 *iter->trace = *current_trace;
3025 mutex_unlock(&trace_types_lock);
3027 mutex_lock(&iter->mutex);
3029 if (iter->trace->splice_read) {
3030 ret = iter->trace->splice_read(iter, filp,
3031 ppos, pipe, len, flags);
3036 ret = tracing_wait_pipe(filp);
3040 if (!iter->ent && !find_next_entry_inc(iter)) {
3045 /* Fill as many pages as possible. */
3046 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3047 pages[i] = alloc_page(GFP_KERNEL);
3051 rem = tracing_fill_pipe_page(rem, iter);
3053 /* Copy the data into the page, so we can start over. */
3054 ret = trace_seq_to_buffer(&iter->seq,
3055 page_address(pages[i]),
3058 __free_page(pages[i]);
3061 partial[i].offset = 0;
3062 partial[i].len = iter->seq.len;
3064 trace_seq_init(&iter->seq);
3067 mutex_unlock(&iter->mutex);
3071 return splice_to_pipe(pipe, &spd);
3074 mutex_unlock(&iter->mutex);
3080 tracing_entries_read(struct file *filp, char __user *ubuf,
3081 size_t cnt, loff_t *ppos)
3083 struct trace_array *tr = filp->private_data;
3087 mutex_lock(&trace_types_lock);
3088 if (!ring_buffer_expanded)
3089 r = sprintf(buf, "%lu (expanded: %lu)\n",
3091 trace_buf_size >> 10);
3093 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3094 mutex_unlock(&trace_types_lock);
3096 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3100 tracing_entries_write(struct file *filp, const char __user *ubuf,
3101 size_t cnt, loff_t *ppos)
3107 if (cnt >= sizeof(buf))
3110 if (copy_from_user(&buf, ubuf, cnt))
3115 ret = strict_strtoul(buf, 10, &val);
3119 /* must have at least 1 entry */
3123 mutex_lock(&trace_types_lock);
3127 /* disable all cpu buffers */
3128 for_each_tracing_cpu(cpu) {
3129 if (global_trace.data[cpu])
3130 atomic_inc(&global_trace.data[cpu]->disabled);
3131 if (max_tr.data[cpu])
3132 atomic_inc(&max_tr.data[cpu]->disabled);
3135 /* value is in KB */
3138 if (val != global_trace.entries) {
3139 ret = tracing_resize_ring_buffer(val);
3148 /* If check pages failed, return ENOMEM */
3149 if (tracing_disabled)
3152 for_each_tracing_cpu(cpu) {
3153 if (global_trace.data[cpu])
3154 atomic_dec(&global_trace.data[cpu]->disabled);
3155 if (max_tr.data[cpu])
3156 atomic_dec(&max_tr.data[cpu]->disabled);
3160 max_tr.entries = global_trace.entries;
3161 mutex_unlock(&trace_types_lock);
3166 static int mark_printk(const char *fmt, ...)
3170 va_start(args, fmt);
3171 ret = trace_vprintk(0, fmt, args);
3177 tracing_mark_write(struct file *filp, const char __user *ubuf,
3178 size_t cnt, loff_t *fpos)
3183 if (tracing_disabled)
3186 if (cnt > TRACE_BUF_SIZE)
3187 cnt = TRACE_BUF_SIZE;
3189 buf = kmalloc(cnt + 1, GFP_KERNEL);
3193 if (copy_from_user(buf, ubuf, cnt)) {
3198 /* Cut from the first nil or newline. */
3200 end = strchr(buf, '\n');
3204 cnt = mark_printk("%s\n", buf);
3211 static const struct file_operations tracing_max_lat_fops = {
3212 .open = tracing_open_generic,
3213 .read = tracing_max_lat_read,
3214 .write = tracing_max_lat_write,
3217 static const struct file_operations tracing_ctrl_fops = {
3218 .open = tracing_open_generic,
3219 .read = tracing_ctrl_read,
3220 .write = tracing_ctrl_write,
3223 static const struct file_operations set_tracer_fops = {
3224 .open = tracing_open_generic,
3225 .read = tracing_set_trace_read,
3226 .write = tracing_set_trace_write,
3229 static const struct file_operations tracing_pipe_fops = {
3230 .open = tracing_open_pipe,
3231 .poll = tracing_poll_pipe,
3232 .read = tracing_read_pipe,
3233 .splice_read = tracing_splice_read_pipe,
3234 .release = tracing_release_pipe,
3237 static const struct file_operations tracing_entries_fops = {
3238 .open = tracing_open_generic,
3239 .read = tracing_entries_read,
3240 .write = tracing_entries_write,
3243 static const struct file_operations tracing_mark_fops = {
3244 .open = tracing_open_generic,
3245 .write = tracing_mark_write,
3248 struct ftrace_buffer_info {
3249 struct trace_array *tr;
3255 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3257 int cpu = (int)(long)inode->i_private;
3258 struct ftrace_buffer_info *info;
3260 if (tracing_disabled)
3263 info = kzalloc(sizeof(*info), GFP_KERNEL);
3267 info->tr = &global_trace;
3269 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3270 /* Force reading ring buffer for first read */
3271 info->read = (unsigned int)-1;
3275 filp->private_data = info;
3285 tracing_buffers_read(struct file *filp, char __user *ubuf,
3286 size_t count, loff_t *ppos)
3288 struct ftrace_buffer_info *info = filp->private_data;
3296 /* Do we have previous read data to read? */
3297 if (info->read < PAGE_SIZE)
3302 ret = ring_buffer_read_page(info->tr->buffer,
3309 pos = ring_buffer_page_len(info->spare);
3311 if (pos < PAGE_SIZE)
3312 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3315 size = PAGE_SIZE - info->read;
3319 ret = copy_to_user(ubuf, info->spare + info->read, size);
3330 static int tracing_buffers_release(struct inode *inode, struct file *file)
3332 struct ftrace_buffer_info *info = file->private_data;
3334 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3341 struct ring_buffer *buffer;
3346 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3347 struct pipe_buffer *buf)
3349 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3354 ring_buffer_free_read_page(ref->buffer, ref->page);
3359 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3360 struct pipe_buffer *buf)
3365 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3366 struct pipe_buffer *buf)
3368 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3373 /* Pipe buffer operations for a buffer. */
3374 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3376 .map = generic_pipe_buf_map,
3377 .unmap = generic_pipe_buf_unmap,
3378 .confirm = generic_pipe_buf_confirm,
3379 .release = buffer_pipe_buf_release,
3380 .steal = buffer_pipe_buf_steal,
3381 .get = buffer_pipe_buf_get,
3385 * Callback from splice_to_pipe(), if we need to release some pages
3386 * at the end of the spd in case we error'ed out in filling the pipe.
3388 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3390 struct buffer_ref *ref =
3391 (struct buffer_ref *)spd->partial[i].private;
3396 ring_buffer_free_read_page(ref->buffer, ref->page);
3398 spd->partial[i].private = 0;
3402 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3403 struct pipe_inode_info *pipe, size_t len,
3406 struct ftrace_buffer_info *info = file->private_data;
3407 struct partial_page partial[PIPE_BUFFERS];
3408 struct page *pages[PIPE_BUFFERS];
3409 struct splice_pipe_desc spd = {
3413 .ops = &buffer_pipe_buf_ops,
3414 .spd_release = buffer_spd_release,
3416 struct buffer_ref *ref;
3421 * We can't seek on a buffer input
3423 if (unlikely(*ppos))
3427 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3431 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3435 ref->buffer = info->tr->buffer;
3436 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3442 r = ring_buffer_read_page(ref->buffer, &ref->page,
3445 ring_buffer_free_read_page(ref->buffer,
3452 * zero out any left over data, this is going to
3455 size = ring_buffer_page_len(ref->page);
3456 if (size < PAGE_SIZE)
3457 memset(ref->page + size, 0, PAGE_SIZE - size);
3459 page = virt_to_page(ref->page);
3461 spd.pages[i] = page;
3462 spd.partial[i].len = PAGE_SIZE;
3463 spd.partial[i].offset = 0;
3464 spd.partial[i].private = (unsigned long)ref;
3470 /* did we read anything? */
3471 if (!spd.nr_pages) {
3472 if (flags & SPLICE_F_NONBLOCK)
3480 ret = splice_to_pipe(pipe, &spd);
3485 static const struct file_operations tracing_buffers_fops = {
3486 .open = tracing_buffers_open,
3487 .read = tracing_buffers_read,
3488 .release = tracing_buffers_release,
3489 .splice_read = tracing_buffers_splice_read,
3490 .llseek = no_llseek,
3493 #ifdef CONFIG_DYNAMIC_FTRACE
3495 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3501 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3502 size_t cnt, loff_t *ppos)
3504 static char ftrace_dyn_info_buffer[1024];
3505 static DEFINE_MUTEX(dyn_info_mutex);
3506 unsigned long *p = filp->private_data;
3507 char *buf = ftrace_dyn_info_buffer;
3508 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3511 mutex_lock(&dyn_info_mutex);
3512 r = sprintf(buf, "%ld ", *p);
3514 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3517 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3519 mutex_unlock(&dyn_info_mutex);
3524 static const struct file_operations tracing_dyn_info_fops = {
3525 .open = tracing_open_generic,
3526 .read = tracing_read_dyn_info,
3530 static struct dentry *d_tracer;
3532 struct dentry *tracing_init_dentry(void)
3539 if (!debugfs_initialized())
3542 d_tracer = debugfs_create_dir("tracing", NULL);
3544 if (!d_tracer && !once) {
3546 pr_warning("Could not create debugfs directory 'tracing'\n");
3553 static struct dentry *d_percpu;
3555 struct dentry *tracing_dentry_percpu(void)
3558 struct dentry *d_tracer;
3563 d_tracer = tracing_init_dentry();
3568 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3570 if (!d_percpu && !once) {
3572 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3579 static void tracing_init_debugfs_percpu(long cpu)
3581 struct dentry *d_percpu = tracing_dentry_percpu();
3582 struct dentry *entry, *d_cpu;
3583 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3586 if (cpu > 999 || cpu < 0)
3589 sprintf(cpu_dir, "cpu%ld", cpu);
3590 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3592 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3596 /* per cpu trace_pipe */
3597 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
3598 (void *) cpu, &tracing_pipe_fops);
3600 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3603 entry = debugfs_create_file("trace", 0644, d_cpu,
3604 (void *) cpu, &tracing_fops);
3606 pr_warning("Could not create debugfs 'trace' entry\n");
3608 entry = debugfs_create_file("trace_pipe_raw", 0444, d_cpu,
3609 (void *) cpu, &tracing_buffers_fops);
3611 pr_warning("Could not create debugfs 'trace_pipe_raw' entry\n");
3614 #ifdef CONFIG_FTRACE_SELFTEST
3615 /* Let selftest have access to static functions in this file */
3616 #include "trace_selftest.c"
3619 struct trace_option_dentry {
3620 struct tracer_opt *opt;
3621 struct tracer_flags *flags;
3622 struct dentry *entry;
3626 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3629 struct trace_option_dentry *topt = filp->private_data;
3632 if (topt->flags->val & topt->opt->bit)
3637 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3641 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3644 struct trace_option_dentry *topt = filp->private_data;
3649 if (cnt >= sizeof(buf))
3652 if (copy_from_user(&buf, ubuf, cnt))
3657 ret = strict_strtoul(buf, 10, &val);
3664 /* do nothing if already cleared */
3665 if (!(topt->flags->val & topt->opt->bit))
3668 mutex_lock(&trace_types_lock);
3669 if (current_trace->set_flag)
3670 ret = current_trace->set_flag(topt->flags->val,
3672 mutex_unlock(&trace_types_lock);
3675 topt->flags->val &= ~topt->opt->bit;
3678 /* do nothing if already set */
3679 if (topt->flags->val & topt->opt->bit)
3682 mutex_lock(&trace_types_lock);
3683 if (current_trace->set_flag)
3684 ret = current_trace->set_flag(topt->flags->val,
3686 mutex_unlock(&trace_types_lock);
3689 topt->flags->val |= topt->opt->bit;
3702 static const struct file_operations trace_options_fops = {
3703 .open = tracing_open_generic,
3704 .read = trace_options_read,
3705 .write = trace_options_write,
3709 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3712 long index = (long)filp->private_data;
3715 if (trace_flags & (1 << index))
3720 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3724 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3727 long index = (long)filp->private_data;
3732 if (cnt >= sizeof(buf))
3735 if (copy_from_user(&buf, ubuf, cnt))
3740 ret = strict_strtoul(buf, 10, &val);
3746 trace_flags &= ~(1 << index);
3749 trace_flags |= 1 << index;
3761 static const struct file_operations trace_options_core_fops = {
3762 .open = tracing_open_generic,
3763 .read = trace_options_core_read,
3764 .write = trace_options_core_write,
3767 static struct dentry *trace_options_init_dentry(void)
3769 struct dentry *d_tracer;
3770 static struct dentry *t_options;
3775 d_tracer = tracing_init_dentry();
3779 t_options = debugfs_create_dir("options", d_tracer);
3781 pr_warning("Could not create debugfs directory 'options'\n");
3789 create_trace_option_file(struct trace_option_dentry *topt,
3790 struct tracer_flags *flags,
3791 struct tracer_opt *opt)
3793 struct dentry *t_options;
3794 struct dentry *entry;
3796 t_options = trace_options_init_dentry();
3800 topt->flags = flags;
3803 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3804 &trace_options_fops);
3806 topt->entry = entry;
3810 static struct trace_option_dentry *
3811 create_trace_option_files(struct tracer *tracer)
3813 struct trace_option_dentry *topts;
3814 struct tracer_flags *flags;
3815 struct tracer_opt *opts;
3821 flags = tracer->flags;
3823 if (!flags || !flags->opts)
3828 for (cnt = 0; opts[cnt].name; cnt++)
3831 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3835 for (cnt = 0; opts[cnt].name; cnt++)
3836 create_trace_option_file(&topts[cnt], flags,
3843 destroy_trace_option_files(struct trace_option_dentry *topts)
3850 for (cnt = 0; topts[cnt].opt; cnt++) {
3851 if (topts[cnt].entry)
3852 debugfs_remove(topts[cnt].entry);
3858 static struct dentry *
3859 create_trace_option_core_file(const char *option, long index)
3861 struct dentry *t_options;
3862 struct dentry *entry;
3864 t_options = trace_options_init_dentry();
3868 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3869 &trace_options_core_fops);
3874 static __init void create_trace_options_dir(void)
3876 struct dentry *t_options;
3877 struct dentry *entry;
3880 t_options = trace_options_init_dentry();
3884 for (i = 0; trace_options[i]; i++) {
3885 entry = create_trace_option_core_file(trace_options[i], i);
3887 pr_warning("Could not create debugfs %s entry\n",
3892 static __init int tracer_init_debugfs(void)
3894 struct dentry *d_tracer;
3895 struct dentry *entry;
3898 d_tracer = tracing_init_dentry();
3900 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3901 &global_trace, &tracing_ctrl_fops);
3903 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3905 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3906 NULL, &tracing_iter_fops);
3908 pr_warning("Could not create debugfs 'trace_options' entry\n");
3910 create_trace_options_dir();
3912 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3913 NULL, &tracing_cpumask_fops);
3915 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3917 entry = debugfs_create_file("trace", 0644, d_tracer,
3918 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3920 pr_warning("Could not create debugfs 'trace' entry\n");
3922 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3923 &global_trace, &show_traces_fops);
3925 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3927 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3928 &global_trace, &set_tracer_fops);
3930 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3932 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3933 &tracing_max_latency,
3934 &tracing_max_lat_fops);
3936 pr_warning("Could not create debugfs "
3937 "'tracing_max_latency' entry\n");
3939 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3940 &tracing_thresh, &tracing_max_lat_fops);
3942 pr_warning("Could not create debugfs "
3943 "'tracing_thresh' entry\n");
3944 entry = debugfs_create_file("README", 0644, d_tracer,
3945 NULL, &tracing_readme_fops);
3947 pr_warning("Could not create debugfs 'README' entry\n");
3949 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3950 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3952 pr_warning("Could not create debugfs "
3953 "'trace_pipe' entry\n");
3955 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3956 &global_trace, &tracing_entries_fops);
3958 pr_warning("Could not create debugfs "
3959 "'buffer_size_kb' entry\n");
3961 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3962 NULL, &tracing_mark_fops);
3964 pr_warning("Could not create debugfs "
3965 "'trace_marker' entry\n");
3967 #ifdef CONFIG_DYNAMIC_FTRACE
3968 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3969 &ftrace_update_tot_cnt,
3970 &tracing_dyn_info_fops);
3972 pr_warning("Could not create debugfs "
3973 "'dyn_ftrace_total_info' entry\n");
3975 #ifdef CONFIG_SYSPROF_TRACER
3976 init_tracer_sysprof_debugfs(d_tracer);
3979 for_each_tracing_cpu(cpu)
3980 tracing_init_debugfs_percpu(cpu);
3985 static int trace_panic_handler(struct notifier_block *this,
3986 unsigned long event, void *unused)
3988 if (ftrace_dump_on_oops)
3993 static struct notifier_block trace_panic_notifier = {
3994 .notifier_call = trace_panic_handler,
3996 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3999 static int trace_die_handler(struct notifier_block *self,
4005 if (ftrace_dump_on_oops)
4014 static struct notifier_block trace_die_notifier = {
4015 .notifier_call = trace_die_handler,
4020 * printk is set to max of 1024, we really don't need it that big.
4021 * Nothing should be printing 1000 characters anyway.
4023 #define TRACE_MAX_PRINT 1000
4026 * Define here KERN_TRACE so that we have one place to modify
4027 * it if we decide to change what log level the ftrace dump
4030 #define KERN_TRACE KERN_EMERG
4033 trace_printk_seq(struct trace_seq *s)
4035 /* Probably should print a warning here. */
4039 /* should be zero ended, but we are paranoid. */
4040 s->buffer[s->len] = 0;
4042 printk(KERN_TRACE "%s", s->buffer);
4047 static void __ftrace_dump(bool disable_tracing)
4049 static DEFINE_SPINLOCK(ftrace_dump_lock);
4050 /* use static because iter can be a bit big for the stack */
4051 static struct trace_iterator iter;
4052 unsigned int old_userobj;
4053 static int dump_ran;
4054 unsigned long flags;
4058 spin_lock_irqsave(&ftrace_dump_lock, flags);
4066 if (disable_tracing)
4069 for_each_tracing_cpu(cpu) {
4070 atomic_inc(&global_trace.data[cpu]->disabled);
4073 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4075 /* don't look at user memory in panic mode */
4076 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4078 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4080 /* Simulate the iterator */
4081 iter.tr = &global_trace;
4082 iter.trace = current_trace;
4083 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4086 * We need to stop all tracing on all CPUS to read the
4087 * the next buffer. This is a bit expensive, but is
4088 * not done often. We fill all what we can read,
4089 * and then release the locks again.
4092 while (!trace_empty(&iter)) {
4095 printk(KERN_TRACE "---------------------------------\n");
4099 /* reset all but tr, trace, and overruns */
4100 memset(&iter.seq, 0,
4101 sizeof(struct trace_iterator) -
4102 offsetof(struct trace_iterator, seq));
4103 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4106 if (find_next_entry_inc(&iter) != NULL) {
4107 print_trace_line(&iter);
4108 trace_consume(&iter);
4111 trace_printk_seq(&iter.seq);
4115 printk(KERN_TRACE " (ftrace buffer empty)\n");
4117 printk(KERN_TRACE "---------------------------------\n");
4119 /* Re-enable tracing if requested */
4120 if (!disable_tracing) {
4121 trace_flags |= old_userobj;
4123 for_each_tracing_cpu(cpu) {
4124 atomic_dec(&global_trace.data[cpu]->disabled);
4130 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
4133 /* By default: disable tracing after the dump */
4134 void ftrace_dump(void)
4136 __ftrace_dump(true);
4139 __init static int tracer_alloc_buffers(void)
4141 struct trace_array_cpu *data;
4146 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4149 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4150 goto out_free_buffer_mask;
4152 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4153 goto out_free_tracing_cpumask;
4155 /* To save memory, keep the ring buffer size to its minimum */
4156 if (ring_buffer_expanded)
4157 ring_buf_size = trace_buf_size;
4161 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4162 cpumask_copy(tracing_cpumask, cpu_all_mask);
4163 cpumask_clear(tracing_reader_cpumask);
4165 /* TODO: make the number of buffers hot pluggable with CPUS */
4166 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4167 TRACE_BUFFER_FLAGS);
4168 if (!global_trace.buffer) {
4169 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4171 goto out_free_cpumask;
4173 global_trace.entries = ring_buffer_size(global_trace.buffer);
4176 #ifdef CONFIG_TRACER_MAX_TRACE
4177 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4178 TRACE_BUFFER_FLAGS);
4179 if (!max_tr.buffer) {
4180 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4182 ring_buffer_free(global_trace.buffer);
4183 goto out_free_cpumask;
4185 max_tr.entries = ring_buffer_size(max_tr.buffer);
4186 WARN_ON(max_tr.entries != global_trace.entries);
4189 /* Allocate the first page for all buffers */
4190 for_each_tracing_cpu(i) {
4191 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4192 max_tr.data[i] = &per_cpu(max_data, i);
4195 trace_init_cmdlines();
4197 register_tracer(&nop_trace);
4198 current_trace = &nop_trace;
4199 #ifdef CONFIG_BOOT_TRACER
4200 register_tracer(&boot_tracer);
4202 /* All seems OK, enable tracing */
4203 tracing_disabled = 0;
4205 atomic_notifier_chain_register(&panic_notifier_list,
4206 &trace_panic_notifier);
4208 register_die_notifier(&trace_die_notifier);
4213 free_cpumask_var(tracing_reader_cpumask);
4214 out_free_tracing_cpumask:
4215 free_cpumask_var(tracing_cpumask);
4216 out_free_buffer_mask:
4217 free_cpumask_var(tracing_buffer_mask);
4222 __init static int clear_boot_tracer(void)
4225 * The default tracer at boot buffer is an init section.
4226 * This function is called in lateinit. If we did not
4227 * find the boot tracer, then clear it out, to prevent
4228 * later registration from accessing the buffer that is
4229 * about to be freed.
4231 if (!default_bootup_tracer)
4234 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4235 default_bootup_tracer);
4236 default_bootup_tracer = NULL;
4241 early_initcall(tracer_alloc_buffers);
4242 fs_initcall(tracer_init_debugfs);
4243 late_initcall(clear_boot_tracer);