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)
158 cycle_t ftrace_now(int cpu)
160 u64 ts = ring_buffer_time_stamp(cpu);
161 ring_buffer_normalize_time_stamp(cpu, &ts);
166 * The global_trace is the descriptor that holds the tracing
167 * buffers for the live tracing. For each CPU, it contains
168 * a link list of pages that will store trace entries. The
169 * page descriptor of the pages in the memory is used to hold
170 * the link list by linking the lru item in the page descriptor
171 * to each of the pages in the buffer per CPU.
173 * For each active CPU there is a data field that holds the
174 * pages for the buffer for that CPU. Each CPU has the same number
175 * of pages allocated for its buffer.
177 static struct trace_array global_trace;
179 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
182 * The max_tr is used to snapshot the global_trace when a maximum
183 * latency is reached. Some tracers will use this to store a maximum
184 * trace while it continues examining live traces.
186 * The buffers for the max_tr are set up the same as the global_trace.
187 * When a snapshot is taken, the link list of the max_tr is swapped
188 * with the link list of the global_trace and the buffers are reset for
189 * the global_trace so the tracing can continue.
191 static struct trace_array max_tr;
193 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
195 /* tracer_enabled is used to toggle activation of a tracer */
196 static int tracer_enabled = 1;
199 * tracing_is_enabled - return tracer_enabled status
201 * This function is used by other tracers to know the status
202 * of the tracer_enabled flag. Tracers may use this function
203 * to know if it should enable their features when starting
204 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
206 int tracing_is_enabled(void)
208 return tracer_enabled;
212 * trace_buf_size is the size in bytes that is allocated
213 * for a buffer. Note, the number of bytes is always rounded
216 * This number is purposely set to a low number of 16384.
217 * If the dump on oops happens, it will be much appreciated
218 * to not have to wait for all that output. Anyway this can be
219 * boot time and run time configurable.
221 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
223 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
225 /* trace_types holds a link list of available tracers. */
226 static struct tracer *trace_types __read_mostly;
228 /* current_trace points to the tracer that is currently active */
229 static struct tracer *current_trace __read_mostly;
232 * max_tracer_type_len is used to simplify the allocating of
233 * buffers to read userspace tracer names. We keep track of
234 * the longest tracer name registered.
236 static int max_tracer_type_len;
239 * trace_types_lock is used to protect the trace_types list.
240 * This lock is also used to keep user access serialized.
241 * Accesses from userspace will grab this lock while userspace
242 * activities happen inside the kernel.
244 static DEFINE_MUTEX(trace_types_lock);
246 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
247 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
249 /* trace_flags holds trace_options default values */
250 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
251 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
254 * trace_wake_up - wake up tasks waiting for trace input
256 * Simply wakes up any task that is blocked on the trace_wait
257 * queue. These is used with trace_poll for tasks polling the trace.
259 void trace_wake_up(void)
262 * The runqueue_is_locked() can fail, but this is the best we
265 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
266 wake_up(&trace_wait);
269 static int __init set_buf_size(char *str)
271 unsigned long buf_size;
276 ret = strict_strtoul(str, 0, &buf_size);
277 /* nr_entries can not be zero */
278 if (ret < 0 || buf_size == 0)
280 trace_buf_size = buf_size;
283 __setup("trace_buf_size=", set_buf_size);
285 unsigned long nsecs_to_usecs(unsigned long nsecs)
290 /* These must match the bit postions in trace_iterator_flags */
291 static const char *trace_options[] = {
315 * ftrace_max_lock is used to protect the swapping of buffers
316 * when taking a max snapshot. The buffers themselves are
317 * protected by per_cpu spinlocks. But the action of the swap
318 * needs its own lock.
320 * This is defined as a raw_spinlock_t in order to help
321 * with performance when lockdep debugging is enabled.
323 static raw_spinlock_t ftrace_max_lock =
324 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
327 * Copy the new maximum trace into the separate maximum-trace
328 * structure. (this way the maximum trace is permanently saved,
329 * for later retrieval via /debugfs/tracing/latency_trace)
332 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
334 struct trace_array_cpu *data = tr->data[cpu];
337 max_tr.time_start = data->preempt_timestamp;
339 data = max_tr.data[cpu];
340 data->saved_latency = tracing_max_latency;
342 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
343 data->pid = tsk->pid;
344 data->uid = task_uid(tsk);
345 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
346 data->policy = tsk->policy;
347 data->rt_priority = tsk->rt_priority;
349 /* record this tasks comm */
350 tracing_record_cmdline(tsk);
353 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
361 if (s->len <= s->readpos)
364 len = s->len - s->readpos;
367 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
377 ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
382 if (s->len <= s->readpos)
385 len = s->len - s->readpos;
388 ret = memcpy(buf, s->buffer + s->readpos, cnt);
397 trace_print_seq(struct seq_file *m, struct trace_seq *s)
399 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
402 seq_puts(m, s->buffer);
408 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
410 * @tsk: the task with the latency
411 * @cpu: The cpu that initiated the trace.
413 * Flip the buffers between the @tr and the max_tr and record information
414 * about which task was the cause of this latency.
417 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
419 struct ring_buffer *buf = tr->buffer;
421 WARN_ON_ONCE(!irqs_disabled());
422 __raw_spin_lock(&ftrace_max_lock);
424 tr->buffer = max_tr.buffer;
427 ftrace_disable_cpu();
428 ring_buffer_reset(tr->buffer);
431 __update_max_tr(tr, tsk, cpu);
432 __raw_spin_unlock(&ftrace_max_lock);
436 * update_max_tr_single - only copy one trace over, and reset the rest
438 * @tsk - task with the latency
439 * @cpu - the cpu of the buffer to copy.
441 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
444 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
448 WARN_ON_ONCE(!irqs_disabled());
449 __raw_spin_lock(&ftrace_max_lock);
451 ftrace_disable_cpu();
453 ring_buffer_reset(max_tr.buffer);
454 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
458 WARN_ON_ONCE(ret && ret != -EAGAIN);
460 __update_max_tr(tr, tsk, cpu);
461 __raw_spin_unlock(&ftrace_max_lock);
465 * register_tracer - register a tracer with the ftrace system.
466 * @type - the plugin for the tracer
468 * Register a new plugin tracer.
470 int register_tracer(struct tracer *type)
471 __releases(kernel_lock)
472 __acquires(kernel_lock)
479 pr_info("Tracer must have a name\n");
484 * When this gets called we hold the BKL which means that
485 * preemption is disabled. Various trace selftests however
486 * need to disable and enable preemption for successful tests.
487 * So we drop the BKL here and grab it after the tests again.
490 mutex_lock(&trace_types_lock);
492 tracing_selftest_running = true;
494 for (t = trace_types; t; t = t->next) {
495 if (strcmp(type->name, t->name) == 0) {
497 pr_info("Trace %s already registered\n",
505 type->set_flag = &dummy_set_flag;
507 type->flags = &dummy_tracer_flags;
509 if (!type->flags->opts)
510 type->flags->opts = dummy_tracer_opt;
511 if (!type->wait_pipe)
512 type->wait_pipe = default_wait_pipe;
515 #ifdef CONFIG_FTRACE_STARTUP_TEST
516 if (type->selftest && !tracing_selftest_disabled) {
517 struct tracer *saved_tracer = current_trace;
518 struct trace_array *tr = &global_trace;
522 * Run a selftest on this tracer.
523 * Here we reset the trace buffer, and set the current
524 * tracer to be this tracer. The tracer can then run some
525 * internal tracing to verify that everything is in order.
526 * If we fail, we do not register this tracer.
528 for_each_tracing_cpu(i)
529 tracing_reset(tr, i);
531 current_trace = type;
532 /* the test is responsible for initializing and enabling */
533 pr_info("Testing tracer %s: ", type->name);
534 ret = type->selftest(type, tr);
535 /* the test is responsible for resetting too */
536 current_trace = saved_tracer;
538 printk(KERN_CONT "FAILED!\n");
541 /* Only reset on passing, to avoid touching corrupted buffers */
542 for_each_tracing_cpu(i)
543 tracing_reset(tr, i);
545 printk(KERN_CONT "PASSED\n");
549 type->next = trace_types;
551 len = strlen(type->name);
552 if (len > max_tracer_type_len)
553 max_tracer_type_len = len;
556 tracing_selftest_running = false;
557 mutex_unlock(&trace_types_lock);
559 if (ret || !default_bootup_tracer)
562 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
565 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
566 /* Do we want this tracer to start on bootup? */
567 tracing_set_tracer(type->name);
568 default_bootup_tracer = NULL;
569 /* disable other selftests, since this will break it. */
570 tracing_selftest_disabled = 1;
571 #ifdef CONFIG_FTRACE_STARTUP_TEST
572 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
581 void unregister_tracer(struct tracer *type)
586 mutex_lock(&trace_types_lock);
587 for (t = &trace_types; *t; t = &(*t)->next) {
591 pr_info("Trace %s not registered\n", type->name);
597 if (type == current_trace && tracer_enabled) {
600 if (current_trace->stop)
601 current_trace->stop(&global_trace);
602 current_trace = &nop_trace;
605 if (strlen(type->name) != max_tracer_type_len)
608 max_tracer_type_len = 0;
609 for (t = &trace_types; *t; t = &(*t)->next) {
610 len = strlen((*t)->name);
611 if (len > max_tracer_type_len)
612 max_tracer_type_len = len;
615 mutex_unlock(&trace_types_lock);
618 void tracing_reset(struct trace_array *tr, int cpu)
620 ftrace_disable_cpu();
621 ring_buffer_reset_cpu(tr->buffer, cpu);
625 void tracing_reset_online_cpus(struct trace_array *tr)
629 tr->time_start = ftrace_now(tr->cpu);
631 for_each_online_cpu(cpu)
632 tracing_reset(tr, cpu);
635 #define SAVED_CMDLINES 128
636 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
637 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
638 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
639 static int cmdline_idx;
640 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
642 /* temporary disable recording */
643 static atomic_t trace_record_cmdline_disabled __read_mostly;
645 static void trace_init_cmdlines(void)
647 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
648 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
652 static int trace_stop_count;
653 static DEFINE_SPINLOCK(tracing_start_lock);
656 * ftrace_off_permanent - disable all ftrace code permanently
658 * This should only be called when a serious anomally has
659 * been detected. This will turn off the function tracing,
660 * ring buffers, and other tracing utilites. It takes no
661 * locks and can be called from any context.
663 void ftrace_off_permanent(void)
665 tracing_disabled = 1;
667 tracing_off_permanent();
671 * tracing_start - quick start of the tracer
673 * If tracing is enabled but was stopped by tracing_stop,
674 * this will start the tracer back up.
676 void tracing_start(void)
678 struct ring_buffer *buffer;
681 if (tracing_disabled)
684 spin_lock_irqsave(&tracing_start_lock, flags);
685 if (--trace_stop_count) {
686 if (trace_stop_count < 0) {
687 /* Someone screwed up their debugging */
689 trace_stop_count = 0;
695 buffer = global_trace.buffer;
697 ring_buffer_record_enable(buffer);
699 buffer = max_tr.buffer;
701 ring_buffer_record_enable(buffer);
705 spin_unlock_irqrestore(&tracing_start_lock, flags);
709 * tracing_stop - quick stop of the tracer
711 * Light weight way to stop tracing. Use in conjunction with
714 void tracing_stop(void)
716 struct ring_buffer *buffer;
720 spin_lock_irqsave(&tracing_start_lock, flags);
721 if (trace_stop_count++)
724 buffer = global_trace.buffer;
726 ring_buffer_record_disable(buffer);
728 buffer = max_tr.buffer;
730 ring_buffer_record_disable(buffer);
733 spin_unlock_irqrestore(&tracing_start_lock, flags);
736 void trace_stop_cmdline_recording(void);
738 static void trace_save_cmdline(struct task_struct *tsk)
743 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
747 * It's not the end of the world if we don't get
748 * the lock, but we also don't want to spin
749 * nor do we want to disable interrupts,
750 * so if we miss here, then better luck next time.
752 if (!__raw_spin_trylock(&trace_cmdline_lock))
755 idx = map_pid_to_cmdline[tsk->pid];
756 if (idx >= SAVED_CMDLINES) {
757 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
759 map = map_cmdline_to_pid[idx];
760 if (map <= PID_MAX_DEFAULT)
761 map_pid_to_cmdline[map] = (unsigned)-1;
763 map_pid_to_cmdline[tsk->pid] = idx;
768 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
770 __raw_spin_unlock(&trace_cmdline_lock);
773 char *trace_find_cmdline(int pid)
775 char *cmdline = "<...>";
781 if (pid > PID_MAX_DEFAULT)
784 map = map_pid_to_cmdline[pid];
785 if (map >= SAVED_CMDLINES)
788 cmdline = saved_cmdlines[map];
794 void tracing_record_cmdline(struct task_struct *tsk)
796 if (atomic_read(&trace_record_cmdline_disabled))
799 trace_save_cmdline(tsk);
803 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
806 struct task_struct *tsk = current;
808 entry->preempt_count = pc & 0xff;
809 entry->pid = (tsk) ? tsk->pid : 0;
810 entry->tgid = (tsk) ? tsk->tgid : 0;
812 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
813 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
815 TRACE_FLAG_IRQS_NOSUPPORT |
817 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
818 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
819 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
822 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
825 unsigned long flags, int pc)
827 struct ring_buffer_event *event;
829 event = ring_buffer_lock_reserve(tr->buffer, len);
831 struct trace_entry *ent = ring_buffer_event_data(event);
833 tracing_generic_entry_update(ent, flags, pc);
839 static void ftrace_trace_stack(struct trace_array *tr,
840 unsigned long flags, int skip, int pc);
841 static void ftrace_trace_userstack(struct trace_array *tr,
842 unsigned long flags, int pc);
844 void trace_buffer_unlock_commit(struct trace_array *tr,
845 struct ring_buffer_event *event,
846 unsigned long flags, int pc)
848 ring_buffer_unlock_commit(tr->buffer, event);
850 ftrace_trace_stack(tr, flags, 6, pc);
851 ftrace_trace_userstack(tr, flags, pc);
855 struct ring_buffer_event *
856 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
857 unsigned long flags, int pc)
859 return trace_buffer_lock_reserve(&global_trace,
860 type, len, flags, pc);
863 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
864 unsigned long flags, int pc)
866 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
870 trace_function(struct trace_array *tr,
871 unsigned long ip, unsigned long parent_ip, unsigned long flags,
874 struct ring_buffer_event *event;
875 struct ftrace_entry *entry;
877 /* If we are reading the ring buffer, don't trace */
878 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
881 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
885 entry = ring_buffer_event_data(event);
887 entry->parent_ip = parent_ip;
888 ring_buffer_unlock_commit(tr->buffer, event);
891 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
892 static void __trace_graph_entry(struct trace_array *tr,
893 struct ftrace_graph_ent *trace,
897 struct ring_buffer_event *event;
898 struct ftrace_graph_ent_entry *entry;
900 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
903 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
904 sizeof(*entry), flags, pc);
907 entry = ring_buffer_event_data(event);
908 entry->graph_ent = *trace;
909 ring_buffer_unlock_commit(global_trace.buffer, event);
912 static void __trace_graph_return(struct trace_array *tr,
913 struct ftrace_graph_ret *trace,
917 struct ring_buffer_event *event;
918 struct ftrace_graph_ret_entry *entry;
920 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
923 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
924 sizeof(*entry), flags, pc);
927 entry = ring_buffer_event_data(event);
929 ring_buffer_unlock_commit(global_trace.buffer, event);
934 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
935 unsigned long ip, unsigned long parent_ip, unsigned long flags,
938 if (likely(!atomic_read(&data->disabled)))
939 trace_function(tr, ip, parent_ip, flags, pc);
942 static void __ftrace_trace_stack(struct trace_array *tr,
946 #ifdef CONFIG_STACKTRACE
947 struct ring_buffer_event *event;
948 struct stack_entry *entry;
949 struct stack_trace trace;
951 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
952 sizeof(*entry), flags, pc);
955 entry = ring_buffer_event_data(event);
956 memset(&entry->caller, 0, sizeof(entry->caller));
958 trace.nr_entries = 0;
959 trace.max_entries = FTRACE_STACK_ENTRIES;
961 trace.entries = entry->caller;
963 save_stack_trace(&trace);
964 ring_buffer_unlock_commit(tr->buffer, event);
968 static void ftrace_trace_stack(struct trace_array *tr,
972 if (!(trace_flags & TRACE_ITER_STACKTRACE))
975 __ftrace_trace_stack(tr, flags, skip, pc);
978 void __trace_stack(struct trace_array *tr,
982 __ftrace_trace_stack(tr, flags, skip, pc);
985 static void ftrace_trace_userstack(struct trace_array *tr,
986 unsigned long flags, int pc)
988 #ifdef CONFIG_STACKTRACE
989 struct ring_buffer_event *event;
990 struct userstack_entry *entry;
991 struct stack_trace trace;
993 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
996 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
997 sizeof(*entry), flags, pc);
1000 entry = ring_buffer_event_data(event);
1002 memset(&entry->caller, 0, sizeof(entry->caller));
1004 trace.nr_entries = 0;
1005 trace.max_entries = FTRACE_STACK_ENTRIES;
1007 trace.entries = entry->caller;
1009 save_stack_trace_user(&trace);
1010 ring_buffer_unlock_commit(tr->buffer, event);
1015 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1017 ftrace_trace_userstack(tr, flags, preempt_count());
1022 ftrace_trace_special(void *__tr,
1023 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1026 struct ring_buffer_event *event;
1027 struct trace_array *tr = __tr;
1028 struct special_entry *entry;
1030 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1031 sizeof(*entry), 0, pc);
1034 entry = ring_buffer_event_data(event);
1038 trace_buffer_unlock_commit(tr, event, 0, pc);
1042 __trace_special(void *__tr, void *__data,
1043 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1045 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1049 tracing_sched_switch_trace(struct trace_array *tr,
1050 struct task_struct *prev,
1051 struct task_struct *next,
1052 unsigned long flags, int pc)
1054 struct ring_buffer_event *event;
1055 struct ctx_switch_entry *entry;
1057 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1058 sizeof(*entry), flags, pc);
1061 entry = ring_buffer_event_data(event);
1062 entry->prev_pid = prev->pid;
1063 entry->prev_prio = prev->prio;
1064 entry->prev_state = prev->state;
1065 entry->next_pid = next->pid;
1066 entry->next_prio = next->prio;
1067 entry->next_state = next->state;
1068 entry->next_cpu = task_cpu(next);
1069 trace_buffer_unlock_commit(tr, event, flags, pc);
1073 tracing_sched_wakeup_trace(struct trace_array *tr,
1074 struct task_struct *wakee,
1075 struct task_struct *curr,
1076 unsigned long flags, int pc)
1078 struct ring_buffer_event *event;
1079 struct ctx_switch_entry *entry;
1081 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1082 sizeof(*entry), flags, pc);
1085 entry = ring_buffer_event_data(event);
1086 entry->prev_pid = curr->pid;
1087 entry->prev_prio = curr->prio;
1088 entry->prev_state = curr->state;
1089 entry->next_pid = wakee->pid;
1090 entry->next_prio = wakee->prio;
1091 entry->next_state = wakee->state;
1092 entry->next_cpu = task_cpu(wakee);
1094 ring_buffer_unlock_commit(tr->buffer, event);
1095 ftrace_trace_stack(tr, flags, 6, pc);
1096 ftrace_trace_userstack(tr, flags, pc);
1100 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1102 struct trace_array *tr = &global_trace;
1103 struct trace_array_cpu *data;
1104 unsigned long flags;
1108 if (tracing_disabled)
1111 pc = preempt_count();
1112 local_irq_save(flags);
1113 cpu = raw_smp_processor_id();
1114 data = tr->data[cpu];
1116 if (likely(atomic_inc_return(&data->disabled) == 1))
1117 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1119 atomic_dec(&data->disabled);
1120 local_irq_restore(flags);
1123 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1124 int trace_graph_entry(struct ftrace_graph_ent *trace)
1126 struct trace_array *tr = &global_trace;
1127 struct trace_array_cpu *data;
1128 unsigned long flags;
1133 if (!ftrace_trace_task(current))
1136 if (!ftrace_graph_addr(trace->func))
1139 local_irq_save(flags);
1140 cpu = raw_smp_processor_id();
1141 data = tr->data[cpu];
1142 disabled = atomic_inc_return(&data->disabled);
1143 if (likely(disabled == 1)) {
1144 pc = preempt_count();
1145 __trace_graph_entry(tr, trace, flags, pc);
1147 /* Only do the atomic if it is not already set */
1148 if (!test_tsk_trace_graph(current))
1149 set_tsk_trace_graph(current);
1150 atomic_dec(&data->disabled);
1151 local_irq_restore(flags);
1156 void trace_graph_return(struct ftrace_graph_ret *trace)
1158 struct trace_array *tr = &global_trace;
1159 struct trace_array_cpu *data;
1160 unsigned long flags;
1165 local_irq_save(flags);
1166 cpu = raw_smp_processor_id();
1167 data = tr->data[cpu];
1168 disabled = atomic_inc_return(&data->disabled);
1169 if (likely(disabled == 1)) {
1170 pc = preempt_count();
1171 __trace_graph_return(tr, trace, flags, pc);
1174 clear_tsk_trace_graph(current);
1175 atomic_dec(&data->disabled);
1176 local_irq_restore(flags);
1178 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1182 * trace_vbprintk - write binary msg to tracing buffer
1185 int trace_vbprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1187 static raw_spinlock_t trace_buf_lock =
1188 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1189 static u32 trace_buf[TRACE_BUF_SIZE];
1191 struct ring_buffer_event *event;
1192 struct trace_array *tr = &global_trace;
1193 struct trace_array_cpu *data;
1194 struct bprint_entry *entry;
1195 unsigned long flags;
1197 int cpu, len = 0, size, pc;
1199 if (unlikely(tracing_selftest_running || tracing_disabled))
1202 /* Don't pollute graph traces with trace_vprintk internals */
1203 pause_graph_tracing();
1205 pc = preempt_count();
1206 resched = ftrace_preempt_disable();
1207 cpu = raw_smp_processor_id();
1208 data = tr->data[cpu];
1210 if (unlikely(atomic_read(&data->disabled)))
1213 /* Lockdep uses trace_printk for lock tracing */
1214 local_irq_save(flags);
1215 __raw_spin_lock(&trace_buf_lock);
1216 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1218 if (len > TRACE_BUF_SIZE || len < 0)
1221 size = sizeof(*entry) + sizeof(u32) * len;
1222 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1225 entry = ring_buffer_event_data(event);
1227 entry->depth = depth;
1230 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1231 ring_buffer_unlock_commit(tr->buffer, event);
1234 __raw_spin_unlock(&trace_buf_lock);
1235 local_irq_restore(flags);
1238 ftrace_preempt_enable(resched);
1239 unpause_graph_tracing();
1243 EXPORT_SYMBOL_GPL(trace_vbprintk);
1245 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1247 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1248 static char trace_buf[TRACE_BUF_SIZE];
1250 struct ring_buffer_event *event;
1251 struct trace_array *tr = &global_trace;
1252 struct trace_array_cpu *data;
1253 int cpu, len = 0, size, pc;
1254 struct print_entry *entry;
1255 unsigned long irq_flags;
1257 if (tracing_disabled || tracing_selftest_running)
1260 pc = preempt_count();
1261 preempt_disable_notrace();
1262 cpu = raw_smp_processor_id();
1263 data = tr->data[cpu];
1265 if (unlikely(atomic_read(&data->disabled)))
1268 pause_graph_tracing();
1269 raw_local_irq_save(irq_flags);
1270 __raw_spin_lock(&trace_buf_lock);
1271 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1273 len = min(len, TRACE_BUF_SIZE-1);
1276 size = sizeof(*entry) + len + 1;
1277 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1280 entry = ring_buffer_event_data(event);
1282 entry->depth = depth;
1284 memcpy(&entry->buf, trace_buf, len);
1285 entry->buf[len] = 0;
1286 ring_buffer_unlock_commit(tr->buffer, event);
1289 __raw_spin_unlock(&trace_buf_lock);
1290 raw_local_irq_restore(irq_flags);
1291 unpause_graph_tracing();
1293 preempt_enable_notrace();
1297 EXPORT_SYMBOL_GPL(trace_vprintk);
1299 enum trace_file_type {
1300 TRACE_FILE_LAT_FMT = 1,
1301 TRACE_FILE_ANNOTATE = 2,
1304 static void trace_iterator_increment(struct trace_iterator *iter)
1306 /* Don't allow ftrace to trace into the ring buffers */
1307 ftrace_disable_cpu();
1310 if (iter->buffer_iter[iter->cpu])
1311 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1313 ftrace_enable_cpu();
1316 static struct trace_entry *
1317 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1319 struct ring_buffer_event *event;
1320 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1322 /* Don't allow ftrace to trace into the ring buffers */
1323 ftrace_disable_cpu();
1326 event = ring_buffer_iter_peek(buf_iter, ts);
1328 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1330 ftrace_enable_cpu();
1332 return event ? ring_buffer_event_data(event) : NULL;
1335 static struct trace_entry *
1336 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1338 struct ring_buffer *buffer = iter->tr->buffer;
1339 struct trace_entry *ent, *next = NULL;
1340 int cpu_file = iter->cpu_file;
1341 u64 next_ts = 0, ts;
1346 * If we are in a per_cpu trace file, don't bother by iterating over
1347 * all cpu and peek directly.
1349 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1350 if (ring_buffer_empty_cpu(buffer, cpu_file))
1352 ent = peek_next_entry(iter, cpu_file, ent_ts);
1354 *ent_cpu = cpu_file;
1359 for_each_tracing_cpu(cpu) {
1361 if (ring_buffer_empty_cpu(buffer, cpu))
1364 ent = peek_next_entry(iter, cpu, &ts);
1367 * Pick the entry with the smallest timestamp:
1369 if (ent && (!next || ts < next_ts)) {
1377 *ent_cpu = next_cpu;
1385 /* Find the next real entry, without updating the iterator itself */
1386 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1387 int *ent_cpu, u64 *ent_ts)
1389 return __find_next_entry(iter, ent_cpu, ent_ts);
1392 /* Find the next real entry, and increment the iterator to the next entry */
1393 static void *find_next_entry_inc(struct trace_iterator *iter)
1395 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1398 trace_iterator_increment(iter);
1400 return iter->ent ? iter : NULL;
1403 static void trace_consume(struct trace_iterator *iter)
1405 /* Don't allow ftrace to trace into the ring buffers */
1406 ftrace_disable_cpu();
1407 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1408 ftrace_enable_cpu();
1411 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1413 struct trace_iterator *iter = m->private;
1419 /* can't go backwards */
1424 ent = find_next_entry_inc(iter);
1428 while (ent && iter->idx < i)
1429 ent = find_next_entry_inc(iter);
1437 * No necessary locking here. The worst thing which can
1438 * happen is loosing events consumed at the same time
1439 * by a trace_pipe reader.
1440 * Other than that, we don't risk to crash the ring buffer
1441 * because it serializes the readers.
1443 * The current tracer is copied to avoid a global locking
1446 static void *s_start(struct seq_file *m, loff_t *pos)
1448 struct trace_iterator *iter = m->private;
1449 static struct tracer *old_tracer;
1450 int cpu_file = iter->cpu_file;
1455 /* copy the tracer to avoid using a global lock all around */
1456 mutex_lock(&trace_types_lock);
1457 if (unlikely(old_tracer != current_trace && current_trace)) {
1458 old_tracer = current_trace;
1459 *iter->trace = *current_trace;
1461 mutex_unlock(&trace_types_lock);
1463 atomic_inc(&trace_record_cmdline_disabled);
1465 if (*pos != iter->pos) {
1470 ftrace_disable_cpu();
1472 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1473 for_each_tracing_cpu(cpu)
1474 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1476 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1479 ftrace_enable_cpu();
1481 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1486 p = s_next(m, p, &l);
1492 static void s_stop(struct seq_file *m, void *p)
1494 atomic_dec(&trace_record_cmdline_disabled);
1497 static void print_lat_help_header(struct seq_file *m)
1499 seq_puts(m, "# _------=> CPU# \n");
1500 seq_puts(m, "# / _-----=> irqs-off \n");
1501 seq_puts(m, "# | / _----=> need-resched \n");
1502 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1503 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1504 seq_puts(m, "# |||| / \n");
1505 seq_puts(m, "# ||||| delay \n");
1506 seq_puts(m, "# cmd pid ||||| time | caller \n");
1507 seq_puts(m, "# \\ / ||||| \\ | / \n");
1510 static void print_func_help_header(struct seq_file *m)
1512 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1513 seq_puts(m, "# | | | | |\n");
1518 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1520 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1521 struct trace_array *tr = iter->tr;
1522 struct trace_array_cpu *data = tr->data[tr->cpu];
1523 struct tracer *type = current_trace;
1524 unsigned long total;
1525 unsigned long entries;
1526 const char *name = "preemption";
1531 entries = ring_buffer_entries(iter->tr->buffer);
1533 ring_buffer_overruns(iter->tr->buffer);
1535 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1537 seq_puts(m, "# -----------------------------------"
1538 "---------------------------------\n");
1539 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1540 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1541 nsecs_to_usecs(data->saved_latency),
1545 #if defined(CONFIG_PREEMPT_NONE)
1547 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1549 #elif defined(CONFIG_PREEMPT)
1554 /* These are reserved for later use */
1557 seq_printf(m, " #P:%d)\n", num_online_cpus());
1561 seq_puts(m, "# -----------------\n");
1562 seq_printf(m, "# | task: %.16s-%d "
1563 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1564 data->comm, data->pid, data->uid, data->nice,
1565 data->policy, data->rt_priority);
1566 seq_puts(m, "# -----------------\n");
1568 if (data->critical_start) {
1569 seq_puts(m, "# => started at: ");
1570 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1571 trace_print_seq(m, &iter->seq);
1572 seq_puts(m, "\n# => ended at: ");
1573 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1574 trace_print_seq(m, &iter->seq);
1581 static void test_cpu_buff_start(struct trace_iterator *iter)
1583 struct trace_seq *s = &iter->seq;
1585 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1588 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1591 if (cpumask_test_cpu(iter->cpu, iter->started))
1594 cpumask_set_cpu(iter->cpu, iter->started);
1595 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1598 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1600 struct trace_seq *s = &iter->seq;
1601 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1602 struct trace_entry *entry;
1603 struct trace_event *event;
1607 test_cpu_buff_start(iter);
1609 event = ftrace_find_event(entry->type);
1611 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1612 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1613 if (!trace_print_lat_context(iter))
1616 if (!trace_print_context(iter))
1622 return event->trace(iter, sym_flags);
1624 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1627 return TRACE_TYPE_HANDLED;
1629 return TRACE_TYPE_PARTIAL_LINE;
1632 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1634 struct trace_seq *s = &iter->seq;
1635 struct trace_entry *entry;
1636 struct trace_event *event;
1640 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1641 if (!trace_seq_printf(s, "%d %d %llu ",
1642 entry->pid, iter->cpu, iter->ts))
1646 event = ftrace_find_event(entry->type);
1648 return event->raw(iter, 0);
1650 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1653 return TRACE_TYPE_HANDLED;
1655 return TRACE_TYPE_PARTIAL_LINE;
1658 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1660 struct trace_seq *s = &iter->seq;
1661 unsigned char newline = '\n';
1662 struct trace_entry *entry;
1663 struct trace_event *event;
1667 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1668 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1669 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1670 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1673 event = ftrace_find_event(entry->type);
1675 enum print_line_t ret = event->hex(iter, 0);
1676 if (ret != TRACE_TYPE_HANDLED)
1680 SEQ_PUT_FIELD_RET(s, newline);
1682 return TRACE_TYPE_HANDLED;
1685 static enum print_line_t print_bprintk_msg_only(struct trace_iterator *iter)
1687 struct trace_seq *s = &iter->seq;
1688 struct trace_entry *entry = iter->ent;
1689 struct bprint_entry *field;
1692 trace_assign_type(field, entry);
1694 ret = trace_seq_bprintf(s, field->fmt, field->buf);
1696 return TRACE_TYPE_PARTIAL_LINE;
1698 return TRACE_TYPE_HANDLED;
1701 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1703 struct trace_seq *s = &iter->seq;
1704 struct trace_entry *entry = iter->ent;
1705 struct print_entry *field;
1708 trace_assign_type(field, entry);
1710 ret = trace_seq_printf(s, "%s", field->buf);
1712 return TRACE_TYPE_PARTIAL_LINE;
1714 return TRACE_TYPE_HANDLED;
1717 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1719 struct trace_seq *s = &iter->seq;
1720 struct trace_entry *entry;
1721 struct trace_event *event;
1725 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1726 SEQ_PUT_FIELD_RET(s, entry->pid);
1727 SEQ_PUT_FIELD_RET(s, iter->cpu);
1728 SEQ_PUT_FIELD_RET(s, iter->ts);
1731 event = ftrace_find_event(entry->type);
1732 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1735 static int trace_empty(struct trace_iterator *iter)
1739 /* If we are looking at one CPU buffer, only check that one */
1740 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1741 cpu = iter->cpu_file;
1742 if (iter->buffer_iter[cpu]) {
1743 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1746 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1752 for_each_tracing_cpu(cpu) {
1753 if (iter->buffer_iter[cpu]) {
1754 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1757 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1765 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1767 enum print_line_t ret;
1769 if (iter->trace && iter->trace->print_line) {
1770 ret = iter->trace->print_line(iter);
1771 if (ret != TRACE_TYPE_UNHANDLED)
1775 if (iter->ent->type == TRACE_BPRINT &&
1776 trace_flags & TRACE_ITER_PRINTK &&
1777 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1778 return print_bprintk_msg_only(iter);
1780 if (iter->ent->type == TRACE_PRINT &&
1781 trace_flags & TRACE_ITER_PRINTK &&
1782 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1783 return print_printk_msg_only(iter);
1785 if (trace_flags & TRACE_ITER_BIN)
1786 return print_bin_fmt(iter);
1788 if (trace_flags & TRACE_ITER_HEX)
1789 return print_hex_fmt(iter);
1791 if (trace_flags & TRACE_ITER_RAW)
1792 return print_raw_fmt(iter);
1794 return print_trace_fmt(iter);
1797 static int s_show(struct seq_file *m, void *v)
1799 struct trace_iterator *iter = v;
1801 if (iter->ent == NULL) {
1803 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1806 if (iter->trace && iter->trace->print_header)
1807 iter->trace->print_header(m);
1808 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1809 /* print nothing if the buffers are empty */
1810 if (trace_empty(iter))
1812 print_trace_header(m, iter);
1813 if (!(trace_flags & TRACE_ITER_VERBOSE))
1814 print_lat_help_header(m);
1816 if (!(trace_flags & TRACE_ITER_VERBOSE))
1817 print_func_help_header(m);
1820 print_trace_line(iter);
1821 trace_print_seq(m, &iter->seq);
1827 static struct seq_operations tracer_seq_ops = {
1834 static struct trace_iterator *
1835 __tracing_open(struct inode *inode, struct file *file)
1837 long cpu_file = (long) inode->i_private;
1838 void *fail_ret = ERR_PTR(-ENOMEM);
1839 struct trace_iterator *iter;
1843 if (tracing_disabled)
1844 return ERR_PTR(-ENODEV);
1846 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1848 return ERR_PTR(-ENOMEM);
1851 * We make a copy of the current tracer to avoid concurrent
1852 * changes on it while we are reading.
1854 mutex_lock(&trace_types_lock);
1855 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1860 *iter->trace = *current_trace;
1862 if (current_trace && current_trace->print_max)
1865 iter->tr = &global_trace;
1867 mutex_init(&iter->mutex);
1868 iter->cpu_file = cpu_file;
1870 /* Notify the tracer early; before we stop tracing. */
1871 if (iter->trace && iter->trace->open)
1872 iter->trace->open(iter);
1874 /* Annotate start of buffers if we had overruns */
1875 if (ring_buffer_overruns(iter->tr->buffer))
1876 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1878 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1879 for_each_tracing_cpu(cpu) {
1881 iter->buffer_iter[cpu] =
1882 ring_buffer_read_start(iter->tr->buffer, cpu);
1885 cpu = iter->cpu_file;
1886 iter->buffer_iter[cpu] =
1887 ring_buffer_read_start(iter->tr->buffer, cpu);
1890 /* TODO stop tracer */
1891 ret = seq_open(file, &tracer_seq_ops);
1893 fail_ret = ERR_PTR(ret);
1897 m = file->private_data;
1900 /* stop the trace while dumping */
1903 mutex_unlock(&trace_types_lock);
1908 for_each_tracing_cpu(cpu) {
1909 if (iter->buffer_iter[cpu])
1910 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1913 mutex_unlock(&trace_types_lock);
1920 int tracing_open_generic(struct inode *inode, struct file *filp)
1922 if (tracing_disabled)
1925 filp->private_data = inode->i_private;
1929 static int tracing_release(struct inode *inode, struct file *file)
1931 struct seq_file *m = (struct seq_file *)file->private_data;
1932 struct trace_iterator *iter = m->private;
1935 mutex_lock(&trace_types_lock);
1936 for_each_tracing_cpu(cpu) {
1937 if (iter->buffer_iter[cpu])
1938 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1941 if (iter->trace && iter->trace->close)
1942 iter->trace->close(iter);
1944 /* reenable tracing if it was previously enabled */
1946 mutex_unlock(&trace_types_lock);
1948 seq_release(inode, file);
1949 mutex_destroy(&iter->mutex);
1955 static int tracing_open(struct inode *inode, struct file *file)
1957 struct trace_iterator *iter;
1960 iter = __tracing_open(inode, file);
1962 ret = PTR_ERR(iter);
1963 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1964 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1970 t_next(struct seq_file *m, void *v, loff_t *pos)
1972 struct tracer *t = m->private;
1984 static void *t_start(struct seq_file *m, loff_t *pos)
1986 struct tracer *t = m->private;
1989 mutex_lock(&trace_types_lock);
1990 for (; t && l < *pos; t = t_next(m, t, &l))
1996 static void t_stop(struct seq_file *m, void *p)
1998 mutex_unlock(&trace_types_lock);
2001 static int t_show(struct seq_file *m, void *v)
2003 struct tracer *t = v;
2008 seq_printf(m, "%s", t->name);
2017 static struct seq_operations show_traces_seq_ops = {
2024 static int show_traces_open(struct inode *inode, struct file *file)
2028 if (tracing_disabled)
2031 ret = seq_open(file, &show_traces_seq_ops);
2033 struct seq_file *m = file->private_data;
2034 m->private = trace_types;
2040 static const struct file_operations tracing_fops = {
2041 .open = tracing_open,
2043 .llseek = seq_lseek,
2044 .release = tracing_release,
2047 static const struct file_operations show_traces_fops = {
2048 .open = show_traces_open,
2050 .release = seq_release,
2054 * Only trace on a CPU if the bitmask is set:
2056 static cpumask_var_t tracing_cpumask;
2059 * The tracer itself will not take this lock, but still we want
2060 * to provide a consistent cpumask to user-space:
2062 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2065 * Temporary storage for the character representation of the
2066 * CPU bitmask (and one more byte for the newline):
2068 static char mask_str[NR_CPUS + 1];
2071 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2072 size_t count, loff_t *ppos)
2076 mutex_lock(&tracing_cpumask_update_lock);
2078 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2079 if (count - len < 2) {
2083 len += sprintf(mask_str + len, "\n");
2084 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2087 mutex_unlock(&tracing_cpumask_update_lock);
2093 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2094 size_t count, loff_t *ppos)
2097 cpumask_var_t tracing_cpumask_new;
2099 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2102 mutex_lock(&tracing_cpumask_update_lock);
2103 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2107 local_irq_disable();
2108 __raw_spin_lock(&ftrace_max_lock);
2109 for_each_tracing_cpu(cpu) {
2111 * Increase/decrease the disabled counter if we are
2112 * about to flip a bit in the cpumask:
2114 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2115 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2116 atomic_inc(&global_trace.data[cpu]->disabled);
2118 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2119 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2120 atomic_dec(&global_trace.data[cpu]->disabled);
2123 __raw_spin_unlock(&ftrace_max_lock);
2126 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2128 mutex_unlock(&tracing_cpumask_update_lock);
2129 free_cpumask_var(tracing_cpumask_new);
2134 mutex_unlock(&tracing_cpumask_update_lock);
2135 free_cpumask_var(tracing_cpumask);
2140 static const struct file_operations tracing_cpumask_fops = {
2141 .open = tracing_open_generic,
2142 .read = tracing_cpumask_read,
2143 .write = tracing_cpumask_write,
2147 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2148 size_t cnt, loff_t *ppos)
2150 struct tracer_opt *trace_opts;
2158 /* calculate max size */
2159 for (i = 0; trace_options[i]; i++) {
2160 len += strlen(trace_options[i]);
2161 len += 3; /* "no" and newline */
2164 mutex_lock(&trace_types_lock);
2165 tracer_flags = current_trace->flags->val;
2166 trace_opts = current_trace->flags->opts;
2169 * Increase the size with names of options specific
2170 * of the current tracer.
2172 for (i = 0; trace_opts[i].name; i++) {
2173 len += strlen(trace_opts[i].name);
2174 len += 3; /* "no" and newline */
2177 /* +2 for \n and \0 */
2178 buf = kmalloc(len + 2, GFP_KERNEL);
2180 mutex_unlock(&trace_types_lock);
2184 for (i = 0; trace_options[i]; i++) {
2185 if (trace_flags & (1 << i))
2186 r += sprintf(buf + r, "%s\n", trace_options[i]);
2188 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2191 for (i = 0; trace_opts[i].name; i++) {
2192 if (tracer_flags & trace_opts[i].bit)
2193 r += sprintf(buf + r, "%s\n",
2194 trace_opts[i].name);
2196 r += sprintf(buf + r, "no%s\n",
2197 trace_opts[i].name);
2199 mutex_unlock(&trace_types_lock);
2201 WARN_ON(r >= len + 2);
2203 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2209 /* Try to assign a tracer specific option */
2210 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2212 struct tracer_flags *trace_flags = trace->flags;
2213 struct tracer_opt *opts = NULL;
2217 for (i = 0; trace_flags->opts[i].name; i++) {
2218 opts = &trace_flags->opts[i];
2219 len = strlen(opts->name);
2221 if (strncmp(cmp, opts->name, len) == 0) {
2222 ret = trace->set_flag(trace_flags->val,
2228 if (!trace_flags->opts[i].name)
2231 /* Refused to handle */
2236 trace_flags->val &= ~opts->bit;
2238 trace_flags->val |= opts->bit;
2244 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2245 size_t cnt, loff_t *ppos)
2253 if (cnt >= sizeof(buf))
2256 if (copy_from_user(&buf, ubuf, cnt))
2261 if (strncmp(buf, "no", 2) == 0) {
2266 for (i = 0; trace_options[i]; i++) {
2267 int len = strlen(trace_options[i]);
2269 if (strncmp(cmp, trace_options[i], len) == 0) {
2271 trace_flags &= ~(1 << i);
2273 trace_flags |= (1 << i);
2278 /* If no option could be set, test the specific tracer options */
2279 if (!trace_options[i]) {
2280 mutex_lock(&trace_types_lock);
2281 ret = set_tracer_option(current_trace, cmp, neg);
2282 mutex_unlock(&trace_types_lock);
2292 static const struct file_operations tracing_iter_fops = {
2293 .open = tracing_open_generic,
2294 .read = tracing_trace_options_read,
2295 .write = tracing_trace_options_write,
2298 static const char readme_msg[] =
2299 "tracing mini-HOWTO:\n\n"
2301 "# mount -t debugfs nodev /debug\n\n"
2302 "# cat /debug/tracing/available_tracers\n"
2303 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2304 "# cat /debug/tracing/current_tracer\n"
2306 "# echo sched_switch > /debug/tracing/current_tracer\n"
2307 "# cat /debug/tracing/current_tracer\n"
2309 "# cat /debug/tracing/trace_options\n"
2310 "noprint-parent nosym-offset nosym-addr noverbose\n"
2311 "# echo print-parent > /debug/tracing/trace_options\n"
2312 "# echo 1 > /debug/tracing/tracing_enabled\n"
2313 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2314 "echo 0 > /debug/tracing/tracing_enabled\n"
2318 tracing_readme_read(struct file *filp, char __user *ubuf,
2319 size_t cnt, loff_t *ppos)
2321 return simple_read_from_buffer(ubuf, cnt, ppos,
2322 readme_msg, strlen(readme_msg));
2325 static const struct file_operations tracing_readme_fops = {
2326 .open = tracing_open_generic,
2327 .read = tracing_readme_read,
2331 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2332 size_t cnt, loff_t *ppos)
2337 r = sprintf(buf, "%u\n", tracer_enabled);
2338 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2342 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2343 size_t cnt, loff_t *ppos)
2345 struct trace_array *tr = filp->private_data;
2350 if (cnt >= sizeof(buf))
2353 if (copy_from_user(&buf, ubuf, cnt))
2358 ret = strict_strtoul(buf, 10, &val);
2364 mutex_lock(&trace_types_lock);
2365 if (tracer_enabled ^ val) {
2368 if (current_trace->start)
2369 current_trace->start(tr);
2374 if (current_trace->stop)
2375 current_trace->stop(tr);
2378 mutex_unlock(&trace_types_lock);
2386 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2387 size_t cnt, loff_t *ppos)
2389 char buf[max_tracer_type_len+2];
2392 mutex_lock(&trace_types_lock);
2394 r = sprintf(buf, "%s\n", current_trace->name);
2396 r = sprintf(buf, "\n");
2397 mutex_unlock(&trace_types_lock);
2399 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2402 int tracer_init(struct tracer *t, struct trace_array *tr)
2404 tracing_reset_online_cpus(tr);
2408 static int tracing_resize_ring_buffer(unsigned long size)
2413 * If kernel or user changes the size of the ring buffer
2414 * we use the size that was given, and we can forget about
2415 * expanding it later.
2417 ring_buffer_expanded = 1;
2419 ret = ring_buffer_resize(global_trace.buffer, size);
2423 ret = ring_buffer_resize(max_tr.buffer, size);
2427 r = ring_buffer_resize(global_trace.buffer,
2428 global_trace.entries);
2431 * AARGH! We are left with different
2432 * size max buffer!!!!
2433 * The max buffer is our "snapshot" buffer.
2434 * When a tracer needs a snapshot (one of the
2435 * latency tracers), it swaps the max buffer
2436 * with the saved snap shot. We succeeded to
2437 * update the size of the main buffer, but failed to
2438 * update the size of the max buffer. But when we tried
2439 * to reset the main buffer to the original size, we
2440 * failed there too. This is very unlikely to
2441 * happen, but if it does, warn and kill all
2445 tracing_disabled = 1;
2450 global_trace.entries = size;
2456 * tracing_update_buffers - used by tracing facility to expand ring buffers
2458 * To save on memory when the tracing is never used on a system with it
2459 * configured in. The ring buffers are set to a minimum size. But once
2460 * a user starts to use the tracing facility, then they need to grow
2461 * to their default size.
2463 * This function is to be called when a tracer is about to be used.
2465 int tracing_update_buffers(void)
2469 mutex_lock(&trace_types_lock);
2470 if (!ring_buffer_expanded)
2471 ret = tracing_resize_ring_buffer(trace_buf_size);
2472 mutex_unlock(&trace_types_lock);
2477 struct trace_option_dentry;
2479 static struct trace_option_dentry *
2480 create_trace_option_files(struct tracer *tracer);
2483 destroy_trace_option_files(struct trace_option_dentry *topts);
2485 static int tracing_set_tracer(const char *buf)
2487 static struct trace_option_dentry *topts;
2488 struct trace_array *tr = &global_trace;
2492 mutex_lock(&trace_types_lock);
2494 if (!ring_buffer_expanded) {
2495 ret = tracing_resize_ring_buffer(trace_buf_size);
2501 for (t = trace_types; t; t = t->next) {
2502 if (strcmp(t->name, buf) == 0)
2509 if (t == current_trace)
2512 trace_branch_disable();
2513 if (current_trace && current_trace->reset)
2514 current_trace->reset(tr);
2516 destroy_trace_option_files(topts);
2520 topts = create_trace_option_files(current_trace);
2523 ret = tracer_init(t, tr);
2528 trace_branch_enable(tr);
2530 mutex_unlock(&trace_types_lock);
2536 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2537 size_t cnt, loff_t *ppos)
2539 char buf[max_tracer_type_len+1];
2546 if (cnt > max_tracer_type_len)
2547 cnt = max_tracer_type_len;
2549 if (copy_from_user(&buf, ubuf, cnt))
2554 /* strip ending whitespace. */
2555 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2558 err = tracing_set_tracer(buf);
2568 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2569 size_t cnt, loff_t *ppos)
2571 unsigned long *ptr = filp->private_data;
2575 r = snprintf(buf, sizeof(buf), "%ld\n",
2576 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2577 if (r > sizeof(buf))
2579 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2583 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2584 size_t cnt, loff_t *ppos)
2586 unsigned long *ptr = filp->private_data;
2591 if (cnt >= sizeof(buf))
2594 if (copy_from_user(&buf, ubuf, cnt))
2599 ret = strict_strtoul(buf, 10, &val);
2608 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2610 long cpu_file = (long) inode->i_private;
2611 struct trace_iterator *iter;
2614 if (tracing_disabled)
2617 mutex_lock(&trace_types_lock);
2619 /* We only allow one reader per cpu */
2620 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2621 if (!cpumask_empty(tracing_reader_cpumask)) {
2625 cpumask_setall(tracing_reader_cpumask);
2627 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2628 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2635 /* create a buffer to store the information to pass to userspace */
2636 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2643 * We make a copy of the current tracer to avoid concurrent
2644 * changes on it while we are reading.
2646 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2652 *iter->trace = *current_trace;
2654 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2659 /* trace pipe does not show start of buffer */
2660 cpumask_setall(iter->started);
2662 iter->cpu_file = cpu_file;
2663 iter->tr = &global_trace;
2664 mutex_init(&iter->mutex);
2665 filp->private_data = iter;
2667 if (iter->trace->pipe_open)
2668 iter->trace->pipe_open(iter);
2671 mutex_unlock(&trace_types_lock);
2677 mutex_unlock(&trace_types_lock);
2681 static int tracing_release_pipe(struct inode *inode, struct file *file)
2683 struct trace_iterator *iter = file->private_data;
2685 mutex_lock(&trace_types_lock);
2687 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2688 cpumask_clear(tracing_reader_cpumask);
2690 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2692 mutex_unlock(&trace_types_lock);
2694 free_cpumask_var(iter->started);
2695 mutex_destroy(&iter->mutex);
2703 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2705 struct trace_iterator *iter = filp->private_data;
2707 if (trace_flags & TRACE_ITER_BLOCK) {
2709 * Always select as readable when in blocking mode
2711 return POLLIN | POLLRDNORM;
2713 if (!trace_empty(iter))
2714 return POLLIN | POLLRDNORM;
2715 poll_wait(filp, &trace_wait, poll_table);
2716 if (!trace_empty(iter))
2717 return POLLIN | POLLRDNORM;
2724 void default_wait_pipe(struct trace_iterator *iter)
2728 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2730 if (trace_empty(iter))
2733 finish_wait(&trace_wait, &wait);
2737 * This is a make-shift waitqueue.
2738 * A tracer might use this callback on some rare cases:
2740 * 1) the current tracer might hold the runqueue lock when it wakes up
2741 * a reader, hence a deadlock (sched, function, and function graph tracers)
2742 * 2) the function tracers, trace all functions, we don't want
2743 * the overhead of calling wake_up and friends
2744 * (and tracing them too)
2746 * Anyway, this is really very primitive wakeup.
2748 void poll_wait_pipe(struct trace_iterator *iter)
2750 set_current_state(TASK_INTERRUPTIBLE);
2751 /* sleep for 100 msecs, and try again. */
2752 schedule_timeout(HZ / 10);
2755 /* Must be called with trace_types_lock mutex held. */
2756 static int tracing_wait_pipe(struct file *filp)
2758 struct trace_iterator *iter = filp->private_data;
2760 while (trace_empty(iter)) {
2762 if ((filp->f_flags & O_NONBLOCK)) {
2766 mutex_unlock(&iter->mutex);
2768 iter->trace->wait_pipe(iter);
2770 mutex_lock(&iter->mutex);
2772 if (signal_pending(current))
2776 * We block until we read something and tracing is disabled.
2777 * We still block if tracing is disabled, but we have never
2778 * read anything. This allows a user to cat this file, and
2779 * then enable tracing. But after we have read something,
2780 * we give an EOF when tracing is again disabled.
2782 * iter->pos will be 0 if we haven't read anything.
2784 if (!tracer_enabled && iter->pos)
2795 tracing_read_pipe(struct file *filp, char __user *ubuf,
2796 size_t cnt, loff_t *ppos)
2798 struct trace_iterator *iter = filp->private_data;
2799 static struct tracer *old_tracer;
2802 /* return any leftover data */
2803 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2807 trace_seq_init(&iter->seq);
2809 /* copy the tracer to avoid using a global lock all around */
2810 mutex_lock(&trace_types_lock);
2811 if (unlikely(old_tracer != current_trace && current_trace)) {
2812 old_tracer = current_trace;
2813 *iter->trace = *current_trace;
2815 mutex_unlock(&trace_types_lock);
2818 * Avoid more than one consumer on a single file descriptor
2819 * This is just a matter of traces coherency, the ring buffer itself
2822 mutex_lock(&iter->mutex);
2823 if (iter->trace->read) {
2824 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2830 sret = tracing_wait_pipe(filp);
2834 /* stop when tracing is finished */
2835 if (trace_empty(iter)) {
2840 if (cnt >= PAGE_SIZE)
2841 cnt = PAGE_SIZE - 1;
2843 /* reset all but tr, trace, and overruns */
2844 memset(&iter->seq, 0,
2845 sizeof(struct trace_iterator) -
2846 offsetof(struct trace_iterator, seq));
2849 while (find_next_entry_inc(iter) != NULL) {
2850 enum print_line_t ret;
2851 int len = iter->seq.len;
2853 ret = print_trace_line(iter);
2854 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2855 /* don't print partial lines */
2856 iter->seq.len = len;
2859 if (ret != TRACE_TYPE_NO_CONSUME)
2860 trace_consume(iter);
2862 if (iter->seq.len >= cnt)
2866 /* Now copy what we have to the user */
2867 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2868 if (iter->seq.readpos >= iter->seq.len)
2869 trace_seq_init(&iter->seq);
2872 * If there was nothing to send to user, inspite of consuming trace
2873 * entries, go back to wait for more entries.
2879 mutex_unlock(&iter->mutex);
2884 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2885 struct pipe_buffer *buf)
2887 __free_page(buf->page);
2890 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2893 __free_page(spd->pages[idx]);
2896 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2898 .map = generic_pipe_buf_map,
2899 .unmap = generic_pipe_buf_unmap,
2900 .confirm = generic_pipe_buf_confirm,
2901 .release = tracing_pipe_buf_release,
2902 .steal = generic_pipe_buf_steal,
2903 .get = generic_pipe_buf_get,
2907 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2912 /* Seq buffer is page-sized, exactly what we need. */
2914 count = iter->seq.len;
2915 ret = print_trace_line(iter);
2916 count = iter->seq.len - count;
2919 iter->seq.len -= count;
2922 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2923 iter->seq.len -= count;
2927 trace_consume(iter);
2929 if (!find_next_entry_inc(iter)) {
2939 static ssize_t tracing_splice_read_pipe(struct file *filp,
2941 struct pipe_inode_info *pipe,
2945 struct page *pages[PIPE_BUFFERS];
2946 struct partial_page partial[PIPE_BUFFERS];
2947 struct trace_iterator *iter = filp->private_data;
2948 struct splice_pipe_desc spd = {
2951 .nr_pages = 0, /* This gets updated below. */
2953 .ops = &tracing_pipe_buf_ops,
2954 .spd_release = tracing_spd_release_pipe,
2956 static struct tracer *old_tracer;
2961 /* copy the tracer to avoid using a global lock all around */
2962 mutex_lock(&trace_types_lock);
2963 if (unlikely(old_tracer != current_trace && current_trace)) {
2964 old_tracer = current_trace;
2965 *iter->trace = *current_trace;
2967 mutex_unlock(&trace_types_lock);
2969 mutex_lock(&iter->mutex);
2971 if (iter->trace->splice_read) {
2972 ret = iter->trace->splice_read(iter, filp,
2973 ppos, pipe, len, flags);
2978 ret = tracing_wait_pipe(filp);
2982 if (!iter->ent && !find_next_entry_inc(iter)) {
2987 /* Fill as many pages as possible. */
2988 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2989 pages[i] = alloc_page(GFP_KERNEL);
2993 rem = tracing_fill_pipe_page(rem, iter);
2995 /* Copy the data into the page, so we can start over. */
2996 ret = trace_seq_to_buffer(&iter->seq,
2997 page_address(pages[i]),
3000 __free_page(pages[i]);
3003 partial[i].offset = 0;
3004 partial[i].len = iter->seq.len;
3006 trace_seq_init(&iter->seq);
3009 mutex_unlock(&iter->mutex);
3013 return splice_to_pipe(pipe, &spd);
3016 mutex_unlock(&iter->mutex);
3022 tracing_entries_read(struct file *filp, char __user *ubuf,
3023 size_t cnt, loff_t *ppos)
3025 struct trace_array *tr = filp->private_data;
3029 mutex_lock(&trace_types_lock);
3030 if (!ring_buffer_expanded)
3031 r = sprintf(buf, "%lu (expanded: %lu)\n",
3033 trace_buf_size >> 10);
3035 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3036 mutex_unlock(&trace_types_lock);
3038 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3042 tracing_entries_write(struct file *filp, const char __user *ubuf,
3043 size_t cnt, loff_t *ppos)
3049 if (cnt >= sizeof(buf))
3052 if (copy_from_user(&buf, ubuf, cnt))
3057 ret = strict_strtoul(buf, 10, &val);
3061 /* must have at least 1 entry */
3065 mutex_lock(&trace_types_lock);
3069 /* disable all cpu buffers */
3070 for_each_tracing_cpu(cpu) {
3071 if (global_trace.data[cpu])
3072 atomic_inc(&global_trace.data[cpu]->disabled);
3073 if (max_tr.data[cpu])
3074 atomic_inc(&max_tr.data[cpu]->disabled);
3077 /* value is in KB */
3080 if (val != global_trace.entries) {
3081 ret = tracing_resize_ring_buffer(val);
3090 /* If check pages failed, return ENOMEM */
3091 if (tracing_disabled)
3094 for_each_tracing_cpu(cpu) {
3095 if (global_trace.data[cpu])
3096 atomic_dec(&global_trace.data[cpu]->disabled);
3097 if (max_tr.data[cpu])
3098 atomic_dec(&max_tr.data[cpu]->disabled);
3102 max_tr.entries = global_trace.entries;
3103 mutex_unlock(&trace_types_lock);
3108 static int mark_printk(const char *fmt, ...)
3112 va_start(args, fmt);
3113 ret = trace_vprintk(0, -1, fmt, args);
3119 tracing_mark_write(struct file *filp, const char __user *ubuf,
3120 size_t cnt, loff_t *fpos)
3125 if (tracing_disabled)
3128 if (cnt > TRACE_BUF_SIZE)
3129 cnt = TRACE_BUF_SIZE;
3131 buf = kmalloc(cnt + 1, GFP_KERNEL);
3135 if (copy_from_user(buf, ubuf, cnt)) {
3140 /* Cut from the first nil or newline. */
3142 end = strchr(buf, '\n');
3146 cnt = mark_printk("%s\n", buf);
3153 static const struct file_operations tracing_max_lat_fops = {
3154 .open = tracing_open_generic,
3155 .read = tracing_max_lat_read,
3156 .write = tracing_max_lat_write,
3159 static const struct file_operations tracing_ctrl_fops = {
3160 .open = tracing_open_generic,
3161 .read = tracing_ctrl_read,
3162 .write = tracing_ctrl_write,
3165 static const struct file_operations set_tracer_fops = {
3166 .open = tracing_open_generic,
3167 .read = tracing_set_trace_read,
3168 .write = tracing_set_trace_write,
3171 static const struct file_operations tracing_pipe_fops = {
3172 .open = tracing_open_pipe,
3173 .poll = tracing_poll_pipe,
3174 .read = tracing_read_pipe,
3175 .splice_read = tracing_splice_read_pipe,
3176 .release = tracing_release_pipe,
3179 static const struct file_operations tracing_entries_fops = {
3180 .open = tracing_open_generic,
3181 .read = tracing_entries_read,
3182 .write = tracing_entries_write,
3185 static const struct file_operations tracing_mark_fops = {
3186 .open = tracing_open_generic,
3187 .write = tracing_mark_write,
3190 struct ftrace_buffer_info {
3191 struct trace_array *tr;
3197 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3199 int cpu = (int)(long)inode->i_private;
3200 struct ftrace_buffer_info *info;
3202 if (tracing_disabled)
3205 info = kzalloc(sizeof(*info), GFP_KERNEL);
3209 info->tr = &global_trace;
3211 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3212 /* Force reading ring buffer for first read */
3213 info->read = (unsigned int)-1;
3217 filp->private_data = info;
3227 tracing_buffers_read(struct file *filp, char __user *ubuf,
3228 size_t count, loff_t *ppos)
3230 struct ftrace_buffer_info *info = filp->private_data;
3238 /* Do we have previous read data to read? */
3239 if (info->read < PAGE_SIZE)
3244 ret = ring_buffer_read_page(info->tr->buffer,
3251 pos = ring_buffer_page_len(info->spare);
3253 if (pos < PAGE_SIZE)
3254 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3257 size = PAGE_SIZE - info->read;
3261 ret = copy_to_user(ubuf, info->spare + info->read, size);
3272 static int tracing_buffers_release(struct inode *inode, struct file *file)
3274 struct ftrace_buffer_info *info = file->private_data;
3276 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3283 struct ring_buffer *buffer;
3288 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3289 struct pipe_buffer *buf)
3291 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3296 ring_buffer_free_read_page(ref->buffer, ref->page);
3301 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3302 struct pipe_buffer *buf)
3307 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3308 struct pipe_buffer *buf)
3310 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3315 /* Pipe buffer operations for a buffer. */
3316 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3318 .map = generic_pipe_buf_map,
3319 .unmap = generic_pipe_buf_unmap,
3320 .confirm = generic_pipe_buf_confirm,
3321 .release = buffer_pipe_buf_release,
3322 .steal = buffer_pipe_buf_steal,
3323 .get = buffer_pipe_buf_get,
3327 * Callback from splice_to_pipe(), if we need to release some pages
3328 * at the end of the spd in case we error'ed out in filling the pipe.
3330 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3332 struct buffer_ref *ref =
3333 (struct buffer_ref *)spd->partial[i].private;
3338 ring_buffer_free_read_page(ref->buffer, ref->page);
3340 spd->partial[i].private = 0;
3344 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3345 struct pipe_inode_info *pipe, size_t len,
3348 struct ftrace_buffer_info *info = file->private_data;
3349 struct partial_page partial[PIPE_BUFFERS];
3350 struct page *pages[PIPE_BUFFERS];
3351 struct splice_pipe_desc spd = {
3355 .ops = &buffer_pipe_buf_ops,
3356 .spd_release = buffer_spd_release,
3358 struct buffer_ref *ref;
3363 * We can't seek on a buffer input
3365 if (unlikely(*ppos))
3369 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3373 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3377 ref->buffer = info->tr->buffer;
3378 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3384 r = ring_buffer_read_page(ref->buffer, &ref->page,
3387 ring_buffer_free_read_page(ref->buffer,
3394 * zero out any left over data, this is going to
3397 size = ring_buffer_page_len(ref->page);
3398 if (size < PAGE_SIZE)
3399 memset(ref->page + size, 0, PAGE_SIZE - size);
3401 page = virt_to_page(ref->page);
3403 spd.pages[i] = page;
3404 spd.partial[i].len = PAGE_SIZE;
3405 spd.partial[i].offset = 0;
3406 spd.partial[i].private = (unsigned long)ref;
3412 /* did we read anything? */
3413 if (!spd.nr_pages) {
3414 if (flags & SPLICE_F_NONBLOCK)
3422 ret = splice_to_pipe(pipe, &spd);
3427 static const struct file_operations tracing_buffers_fops = {
3428 .open = tracing_buffers_open,
3429 .read = tracing_buffers_read,
3430 .release = tracing_buffers_release,
3431 .splice_read = tracing_buffers_splice_read,
3432 .llseek = no_llseek,
3435 #ifdef CONFIG_DYNAMIC_FTRACE
3437 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3443 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3444 size_t cnt, loff_t *ppos)
3446 static char ftrace_dyn_info_buffer[1024];
3447 static DEFINE_MUTEX(dyn_info_mutex);
3448 unsigned long *p = filp->private_data;
3449 char *buf = ftrace_dyn_info_buffer;
3450 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3453 mutex_lock(&dyn_info_mutex);
3454 r = sprintf(buf, "%ld ", *p);
3456 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3459 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3461 mutex_unlock(&dyn_info_mutex);
3466 static const struct file_operations tracing_dyn_info_fops = {
3467 .open = tracing_open_generic,
3468 .read = tracing_read_dyn_info,
3472 static struct dentry *d_tracer;
3474 struct dentry *tracing_init_dentry(void)
3481 d_tracer = debugfs_create_dir("tracing", NULL);
3483 if (!d_tracer && !once) {
3485 pr_warning("Could not create debugfs directory 'tracing'\n");
3492 static struct dentry *d_percpu;
3494 struct dentry *tracing_dentry_percpu(void)
3497 struct dentry *d_tracer;
3502 d_tracer = tracing_init_dentry();
3507 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3509 if (!d_percpu && !once) {
3511 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3518 static void tracing_init_debugfs_percpu(long cpu)
3520 struct dentry *d_percpu = tracing_dentry_percpu();
3521 struct dentry *entry, *d_cpu;
3522 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3525 if (cpu > 999 || cpu < 0)
3528 sprintf(cpu_dir, "cpu%ld", cpu);
3529 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3531 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3535 /* per cpu trace_pipe */
3536 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
3537 (void *) cpu, &tracing_pipe_fops);
3539 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3542 entry = debugfs_create_file("trace", 0444, d_cpu,
3543 (void *) cpu, &tracing_fops);
3545 pr_warning("Could not create debugfs 'trace' entry\n");
3547 entry = debugfs_create_file("trace_pipe_raw", 0444, d_cpu,
3548 (void *) cpu, &tracing_buffers_fops);
3550 pr_warning("Could not create debugfs 'trace_pipe_raw' entry\n");
3553 #ifdef CONFIG_FTRACE_SELFTEST
3554 /* Let selftest have access to static functions in this file */
3555 #include "trace_selftest.c"
3558 struct trace_option_dentry {
3559 struct tracer_opt *opt;
3560 struct tracer_flags *flags;
3561 struct dentry *entry;
3565 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3568 struct trace_option_dentry *topt = filp->private_data;
3571 if (topt->flags->val & topt->opt->bit)
3576 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3580 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3583 struct trace_option_dentry *topt = filp->private_data;
3588 if (cnt >= sizeof(buf))
3591 if (copy_from_user(&buf, ubuf, cnt))
3596 ret = strict_strtoul(buf, 10, &val);
3603 /* do nothing if already cleared */
3604 if (!(topt->flags->val & topt->opt->bit))
3607 mutex_lock(&trace_types_lock);
3608 if (current_trace->set_flag)
3609 ret = current_trace->set_flag(topt->flags->val,
3611 mutex_unlock(&trace_types_lock);
3614 topt->flags->val &= ~topt->opt->bit;
3617 /* do nothing if already set */
3618 if (topt->flags->val & topt->opt->bit)
3621 mutex_lock(&trace_types_lock);
3622 if (current_trace->set_flag)
3623 ret = current_trace->set_flag(topt->flags->val,
3625 mutex_unlock(&trace_types_lock);
3628 topt->flags->val |= topt->opt->bit;
3641 static const struct file_operations trace_options_fops = {
3642 .open = tracing_open_generic,
3643 .read = trace_options_read,
3644 .write = trace_options_write,
3648 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3651 long index = (long)filp->private_data;
3654 if (trace_flags & (1 << index))
3659 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3663 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3666 long index = (long)filp->private_data;
3671 if (cnt >= sizeof(buf))
3674 if (copy_from_user(&buf, ubuf, cnt))
3679 ret = strict_strtoul(buf, 10, &val);
3685 trace_flags &= ~(1 << index);
3688 trace_flags |= 1 << index;
3700 static const struct file_operations trace_options_core_fops = {
3701 .open = tracing_open_generic,
3702 .read = trace_options_core_read,
3703 .write = trace_options_core_write,
3706 static struct dentry *trace_options_init_dentry(void)
3708 struct dentry *d_tracer;
3709 static struct dentry *t_options;
3714 d_tracer = tracing_init_dentry();
3718 t_options = debugfs_create_dir("options", d_tracer);
3720 pr_warning("Could not create debugfs directory 'options'\n");
3728 create_trace_option_file(struct trace_option_dentry *topt,
3729 struct tracer_flags *flags,
3730 struct tracer_opt *opt)
3732 struct dentry *t_options;
3733 struct dentry *entry;
3735 t_options = trace_options_init_dentry();
3739 topt->flags = flags;
3742 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3743 &trace_options_fops);
3745 topt->entry = entry;
3749 static struct trace_option_dentry *
3750 create_trace_option_files(struct tracer *tracer)
3752 struct trace_option_dentry *topts;
3753 struct tracer_flags *flags;
3754 struct tracer_opt *opts;
3760 flags = tracer->flags;
3762 if (!flags || !flags->opts)
3767 for (cnt = 0; opts[cnt].name; cnt++)
3770 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3774 for (cnt = 0; opts[cnt].name; cnt++)
3775 create_trace_option_file(&topts[cnt], flags,
3782 destroy_trace_option_files(struct trace_option_dentry *topts)
3789 for (cnt = 0; topts[cnt].opt; cnt++) {
3790 if (topts[cnt].entry)
3791 debugfs_remove(topts[cnt].entry);
3797 static struct dentry *
3798 create_trace_option_core_file(const char *option, long index)
3800 struct dentry *t_options;
3801 struct dentry *entry;
3803 t_options = trace_options_init_dentry();
3807 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3808 &trace_options_core_fops);
3813 static __init void create_trace_options_dir(void)
3815 struct dentry *t_options;
3816 struct dentry *entry;
3819 t_options = trace_options_init_dentry();
3823 for (i = 0; trace_options[i]; i++) {
3824 entry = create_trace_option_core_file(trace_options[i], i);
3826 pr_warning("Could not create debugfs %s entry\n",
3831 static __init int tracer_init_debugfs(void)
3833 struct dentry *d_tracer;
3834 struct dentry *entry;
3837 d_tracer = tracing_init_dentry();
3839 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3840 &global_trace, &tracing_ctrl_fops);
3842 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3844 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3845 NULL, &tracing_iter_fops);
3847 pr_warning("Could not create debugfs 'trace_options' entry\n");
3849 create_trace_options_dir();
3851 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3852 NULL, &tracing_cpumask_fops);
3854 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3856 entry = debugfs_create_file("trace", 0444, d_tracer,
3857 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3859 pr_warning("Could not create debugfs 'trace' entry\n");
3861 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3862 &global_trace, &show_traces_fops);
3864 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3866 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3867 &global_trace, &set_tracer_fops);
3869 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3871 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3872 &tracing_max_latency,
3873 &tracing_max_lat_fops);
3875 pr_warning("Could not create debugfs "
3876 "'tracing_max_latency' entry\n");
3878 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3879 &tracing_thresh, &tracing_max_lat_fops);
3881 pr_warning("Could not create debugfs "
3882 "'tracing_thresh' entry\n");
3883 entry = debugfs_create_file("README", 0644, d_tracer,
3884 NULL, &tracing_readme_fops);
3886 pr_warning("Could not create debugfs 'README' entry\n");
3888 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3889 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3891 pr_warning("Could not create debugfs "
3892 "'trace_pipe' entry\n");
3894 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3895 &global_trace, &tracing_entries_fops);
3897 pr_warning("Could not create debugfs "
3898 "'buffer_size_kb' entry\n");
3900 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3901 NULL, &tracing_mark_fops);
3903 pr_warning("Could not create debugfs "
3904 "'trace_marker' entry\n");
3906 #ifdef CONFIG_DYNAMIC_FTRACE
3907 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3908 &ftrace_update_tot_cnt,
3909 &tracing_dyn_info_fops);
3911 pr_warning("Could not create debugfs "
3912 "'dyn_ftrace_total_info' entry\n");
3914 #ifdef CONFIG_SYSPROF_TRACER
3915 init_tracer_sysprof_debugfs(d_tracer);
3918 for_each_tracing_cpu(cpu)
3919 tracing_init_debugfs_percpu(cpu);
3924 static int trace_panic_handler(struct notifier_block *this,
3925 unsigned long event, void *unused)
3927 if (ftrace_dump_on_oops)
3932 static struct notifier_block trace_panic_notifier = {
3933 .notifier_call = trace_panic_handler,
3935 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3938 static int trace_die_handler(struct notifier_block *self,
3944 if (ftrace_dump_on_oops)
3953 static struct notifier_block trace_die_notifier = {
3954 .notifier_call = trace_die_handler,
3959 * printk is set to max of 1024, we really don't need it that big.
3960 * Nothing should be printing 1000 characters anyway.
3962 #define TRACE_MAX_PRINT 1000
3965 * Define here KERN_TRACE so that we have one place to modify
3966 * it if we decide to change what log level the ftrace dump
3969 #define KERN_TRACE KERN_EMERG
3972 trace_printk_seq(struct trace_seq *s)
3974 /* Probably should print a warning here. */
3978 /* should be zero ended, but we are paranoid. */
3979 s->buffer[s->len] = 0;
3981 printk(KERN_TRACE "%s", s->buffer);
3986 void ftrace_dump(void)
3988 static DEFINE_SPINLOCK(ftrace_dump_lock);
3989 /* use static because iter can be a bit big for the stack */
3990 static struct trace_iterator iter;
3991 static int dump_ran;
3992 unsigned long flags;
3996 spin_lock_irqsave(&ftrace_dump_lock, flags);
4002 /* No turning back! */
4006 for_each_tracing_cpu(cpu) {
4007 atomic_inc(&global_trace.data[cpu]->disabled);
4010 /* don't look at user memory in panic mode */
4011 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4013 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4015 /* Simulate the iterator */
4016 iter.tr = &global_trace;
4017 iter.trace = current_trace;
4018 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4021 * We need to stop all tracing on all CPUS to read the
4022 * the next buffer. This is a bit expensive, but is
4023 * not done often. We fill all what we can read,
4024 * and then release the locks again.
4027 while (!trace_empty(&iter)) {
4030 printk(KERN_TRACE "---------------------------------\n");
4034 /* reset all but tr, trace, and overruns */
4035 memset(&iter.seq, 0,
4036 sizeof(struct trace_iterator) -
4037 offsetof(struct trace_iterator, seq));
4038 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4041 if (find_next_entry_inc(&iter) != NULL) {
4042 print_trace_line(&iter);
4043 trace_consume(&iter);
4046 trace_printk_seq(&iter.seq);
4050 printk(KERN_TRACE " (ftrace buffer empty)\n");
4052 printk(KERN_TRACE "---------------------------------\n");
4055 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
4058 __init static int tracer_alloc_buffers(void)
4060 struct trace_array_cpu *data;
4065 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4068 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4069 goto out_free_buffer_mask;
4071 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4072 goto out_free_tracing_cpumask;
4074 /* To save memory, keep the ring buffer size to its minimum */
4075 if (ring_buffer_expanded)
4076 ring_buf_size = trace_buf_size;
4080 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4081 cpumask_copy(tracing_cpumask, cpu_all_mask);
4082 cpumask_clear(tracing_reader_cpumask);
4084 /* TODO: make the number of buffers hot pluggable with CPUS */
4085 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4086 TRACE_BUFFER_FLAGS);
4087 if (!global_trace.buffer) {
4088 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4090 goto out_free_cpumask;
4092 global_trace.entries = ring_buffer_size(global_trace.buffer);
4095 #ifdef CONFIG_TRACER_MAX_TRACE
4096 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4097 TRACE_BUFFER_FLAGS);
4098 if (!max_tr.buffer) {
4099 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4101 ring_buffer_free(global_trace.buffer);
4102 goto out_free_cpumask;
4104 max_tr.entries = ring_buffer_size(max_tr.buffer);
4105 WARN_ON(max_tr.entries != global_trace.entries);
4108 /* Allocate the first page for all buffers */
4109 for_each_tracing_cpu(i) {
4110 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4111 max_tr.data[i] = &per_cpu(max_data, i);
4114 trace_init_cmdlines();
4116 register_tracer(&nop_trace);
4117 current_trace = &nop_trace;
4118 #ifdef CONFIG_BOOT_TRACER
4119 register_tracer(&boot_tracer);
4121 /* All seems OK, enable tracing */
4122 tracing_disabled = 0;
4124 atomic_notifier_chain_register(&panic_notifier_list,
4125 &trace_panic_notifier);
4127 register_die_notifier(&trace_die_notifier);
4132 free_cpumask_var(tracing_reader_cpumask);
4133 out_free_tracing_cpumask:
4134 free_cpumask_var(tracing_cpumask);
4135 out_free_buffer_mask:
4136 free_cpumask_var(tracing_buffer_mask);
4141 __init static int clear_boot_tracer(void)
4144 * The default tracer at boot buffer is an init section.
4145 * This function is called in lateinit. If we did not
4146 * find the boot tracer, then clear it out, to prevent
4147 * later registration from accessing the buffer that is
4148 * about to be freed.
4150 if (!default_bootup_tracer)
4153 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4154 default_bootup_tracer);
4155 default_bootup_tracer = NULL;
4160 early_initcall(tracer_alloc_buffers);
4161 fs_initcall(tracer_init_debugfs);
4162 late_initcall(clear_boot_tracer);