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 void trace_find_cmdline(int pid, char comm[])
778 strcpy(comm, "<idle>");
782 if (pid > PID_MAX_DEFAULT) {
783 strcpy(comm, "<...>");
787 __raw_spin_lock(&trace_cmdline_lock);
788 map = map_pid_to_cmdline[pid];
789 if (map >= SAVED_CMDLINES)
792 strcpy(comm, saved_cmdlines[map]);
795 __raw_spin_unlock(&trace_cmdline_lock);
798 void tracing_record_cmdline(struct task_struct *tsk)
800 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
803 trace_save_cmdline(tsk);
807 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
810 struct task_struct *tsk = current;
812 entry->preempt_count = pc & 0xff;
813 entry->pid = (tsk) ? tsk->pid : 0;
814 entry->tgid = (tsk) ? tsk->tgid : 0;
816 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
817 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
819 TRACE_FLAG_IRQS_NOSUPPORT |
821 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
822 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
823 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
826 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
829 unsigned long flags, int pc)
831 struct ring_buffer_event *event;
833 event = ring_buffer_lock_reserve(tr->buffer, len);
835 struct trace_entry *ent = ring_buffer_event_data(event);
837 tracing_generic_entry_update(ent, flags, pc);
843 static void ftrace_trace_stack(struct trace_array *tr,
844 unsigned long flags, int skip, int pc);
845 static void ftrace_trace_userstack(struct trace_array *tr,
846 unsigned long flags, int pc);
848 void trace_buffer_unlock_commit(struct trace_array *tr,
849 struct ring_buffer_event *event,
850 unsigned long flags, int pc)
852 ring_buffer_unlock_commit(tr->buffer, event);
854 ftrace_trace_stack(tr, flags, 6, pc);
855 ftrace_trace_userstack(tr, flags, pc);
859 struct ring_buffer_event *
860 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
861 unsigned long flags, int pc)
863 return trace_buffer_lock_reserve(&global_trace,
864 type, len, flags, pc);
867 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
868 unsigned long flags, int pc)
870 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
874 trace_function(struct trace_array *tr,
875 unsigned long ip, unsigned long parent_ip, unsigned long flags,
878 struct ring_buffer_event *event;
879 struct ftrace_entry *entry;
881 /* If we are reading the ring buffer, don't trace */
882 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
885 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
889 entry = ring_buffer_event_data(event);
891 entry->parent_ip = parent_ip;
892 ring_buffer_unlock_commit(tr->buffer, event);
895 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
896 static void __trace_graph_entry(struct trace_array *tr,
897 struct ftrace_graph_ent *trace,
901 struct ring_buffer_event *event;
902 struct ftrace_graph_ent_entry *entry;
904 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
907 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
908 sizeof(*entry), flags, pc);
911 entry = ring_buffer_event_data(event);
912 entry->graph_ent = *trace;
913 ring_buffer_unlock_commit(global_trace.buffer, event);
916 static void __trace_graph_return(struct trace_array *tr,
917 struct ftrace_graph_ret *trace,
921 struct ring_buffer_event *event;
922 struct ftrace_graph_ret_entry *entry;
924 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
927 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
928 sizeof(*entry), flags, pc);
931 entry = ring_buffer_event_data(event);
933 ring_buffer_unlock_commit(global_trace.buffer, event);
938 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
939 unsigned long ip, unsigned long parent_ip, unsigned long flags,
942 if (likely(!atomic_read(&data->disabled)))
943 trace_function(tr, ip, parent_ip, flags, pc);
946 static void __ftrace_trace_stack(struct trace_array *tr,
950 #ifdef CONFIG_STACKTRACE
951 struct ring_buffer_event *event;
952 struct stack_entry *entry;
953 struct stack_trace trace;
955 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
956 sizeof(*entry), flags, pc);
959 entry = ring_buffer_event_data(event);
960 memset(&entry->caller, 0, sizeof(entry->caller));
962 trace.nr_entries = 0;
963 trace.max_entries = FTRACE_STACK_ENTRIES;
965 trace.entries = entry->caller;
967 save_stack_trace(&trace);
968 ring_buffer_unlock_commit(tr->buffer, event);
972 static void ftrace_trace_stack(struct trace_array *tr,
976 if (!(trace_flags & TRACE_ITER_STACKTRACE))
979 __ftrace_trace_stack(tr, flags, skip, pc);
982 void __trace_stack(struct trace_array *tr,
986 __ftrace_trace_stack(tr, flags, skip, pc);
989 static void ftrace_trace_userstack(struct trace_array *tr,
990 unsigned long flags, int pc)
992 #ifdef CONFIG_STACKTRACE
993 struct ring_buffer_event *event;
994 struct userstack_entry *entry;
995 struct stack_trace trace;
997 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1000 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1001 sizeof(*entry), flags, pc);
1004 entry = ring_buffer_event_data(event);
1006 memset(&entry->caller, 0, sizeof(entry->caller));
1008 trace.nr_entries = 0;
1009 trace.max_entries = FTRACE_STACK_ENTRIES;
1011 trace.entries = entry->caller;
1013 save_stack_trace_user(&trace);
1014 ring_buffer_unlock_commit(tr->buffer, event);
1019 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1021 ftrace_trace_userstack(tr, flags, preempt_count());
1026 ftrace_trace_special(void *__tr,
1027 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1030 struct ring_buffer_event *event;
1031 struct trace_array *tr = __tr;
1032 struct special_entry *entry;
1034 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1035 sizeof(*entry), 0, pc);
1038 entry = ring_buffer_event_data(event);
1042 trace_buffer_unlock_commit(tr, event, 0, pc);
1046 __trace_special(void *__tr, void *__data,
1047 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1049 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1053 tracing_sched_switch_trace(struct trace_array *tr,
1054 struct task_struct *prev,
1055 struct task_struct *next,
1056 unsigned long flags, int pc)
1058 struct ring_buffer_event *event;
1059 struct ctx_switch_entry *entry;
1061 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1062 sizeof(*entry), flags, pc);
1065 entry = ring_buffer_event_data(event);
1066 entry->prev_pid = prev->pid;
1067 entry->prev_prio = prev->prio;
1068 entry->prev_state = prev->state;
1069 entry->next_pid = next->pid;
1070 entry->next_prio = next->prio;
1071 entry->next_state = next->state;
1072 entry->next_cpu = task_cpu(next);
1073 trace_buffer_unlock_commit(tr, event, flags, pc);
1077 tracing_sched_wakeup_trace(struct trace_array *tr,
1078 struct task_struct *wakee,
1079 struct task_struct *curr,
1080 unsigned long flags, int pc)
1082 struct ring_buffer_event *event;
1083 struct ctx_switch_entry *entry;
1085 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1086 sizeof(*entry), flags, pc);
1089 entry = ring_buffer_event_data(event);
1090 entry->prev_pid = curr->pid;
1091 entry->prev_prio = curr->prio;
1092 entry->prev_state = curr->state;
1093 entry->next_pid = wakee->pid;
1094 entry->next_prio = wakee->prio;
1095 entry->next_state = wakee->state;
1096 entry->next_cpu = task_cpu(wakee);
1098 ring_buffer_unlock_commit(tr->buffer, event);
1099 ftrace_trace_stack(tr, flags, 6, pc);
1100 ftrace_trace_userstack(tr, flags, pc);
1104 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1106 struct trace_array *tr = &global_trace;
1107 struct trace_array_cpu *data;
1108 unsigned long flags;
1112 if (tracing_disabled)
1115 pc = preempt_count();
1116 local_irq_save(flags);
1117 cpu = raw_smp_processor_id();
1118 data = tr->data[cpu];
1120 if (likely(atomic_inc_return(&data->disabled) == 1))
1121 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1123 atomic_dec(&data->disabled);
1124 local_irq_restore(flags);
1127 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1128 int trace_graph_entry(struct ftrace_graph_ent *trace)
1130 struct trace_array *tr = &global_trace;
1131 struct trace_array_cpu *data;
1132 unsigned long flags;
1137 if (!ftrace_trace_task(current))
1140 if (!ftrace_graph_addr(trace->func))
1143 local_irq_save(flags);
1144 cpu = raw_smp_processor_id();
1145 data = tr->data[cpu];
1146 disabled = atomic_inc_return(&data->disabled);
1147 if (likely(disabled == 1)) {
1148 pc = preempt_count();
1149 __trace_graph_entry(tr, trace, flags, pc);
1151 /* Only do the atomic if it is not already set */
1152 if (!test_tsk_trace_graph(current))
1153 set_tsk_trace_graph(current);
1154 atomic_dec(&data->disabled);
1155 local_irq_restore(flags);
1160 void trace_graph_return(struct ftrace_graph_ret *trace)
1162 struct trace_array *tr = &global_trace;
1163 struct trace_array_cpu *data;
1164 unsigned long flags;
1169 local_irq_save(flags);
1170 cpu = raw_smp_processor_id();
1171 data = tr->data[cpu];
1172 disabled = atomic_inc_return(&data->disabled);
1173 if (likely(disabled == 1)) {
1174 pc = preempt_count();
1175 __trace_graph_return(tr, trace, flags, pc);
1178 clear_tsk_trace_graph(current);
1179 atomic_dec(&data->disabled);
1180 local_irq_restore(flags);
1182 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1186 * trace_vbprintk - write binary msg to tracing buffer
1189 int trace_vbprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1191 static raw_spinlock_t trace_buf_lock =
1192 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1193 static u32 trace_buf[TRACE_BUF_SIZE];
1195 struct ring_buffer_event *event;
1196 struct trace_array *tr = &global_trace;
1197 struct trace_array_cpu *data;
1198 struct bprint_entry *entry;
1199 unsigned long flags;
1201 int cpu, len = 0, size, pc;
1203 if (unlikely(tracing_selftest_running || tracing_disabled))
1206 /* Don't pollute graph traces with trace_vprintk internals */
1207 pause_graph_tracing();
1209 pc = preempt_count();
1210 resched = ftrace_preempt_disable();
1211 cpu = raw_smp_processor_id();
1212 data = tr->data[cpu];
1214 if (unlikely(atomic_read(&data->disabled)))
1217 /* Lockdep uses trace_printk for lock tracing */
1218 local_irq_save(flags);
1219 __raw_spin_lock(&trace_buf_lock);
1220 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1222 if (len > TRACE_BUF_SIZE || len < 0)
1225 size = sizeof(*entry) + sizeof(u32) * len;
1226 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1229 entry = ring_buffer_event_data(event);
1231 entry->depth = depth;
1234 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1235 ring_buffer_unlock_commit(tr->buffer, event);
1238 __raw_spin_unlock(&trace_buf_lock);
1239 local_irq_restore(flags);
1242 ftrace_preempt_enable(resched);
1243 unpause_graph_tracing();
1247 EXPORT_SYMBOL_GPL(trace_vbprintk);
1249 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1251 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1252 static char trace_buf[TRACE_BUF_SIZE];
1254 struct ring_buffer_event *event;
1255 struct trace_array *tr = &global_trace;
1256 struct trace_array_cpu *data;
1257 int cpu, len = 0, size, pc;
1258 struct print_entry *entry;
1259 unsigned long irq_flags;
1261 if (tracing_disabled || tracing_selftest_running)
1264 pc = preempt_count();
1265 preempt_disable_notrace();
1266 cpu = raw_smp_processor_id();
1267 data = tr->data[cpu];
1269 if (unlikely(atomic_read(&data->disabled)))
1272 pause_graph_tracing();
1273 raw_local_irq_save(irq_flags);
1274 __raw_spin_lock(&trace_buf_lock);
1275 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1277 len = min(len, TRACE_BUF_SIZE-1);
1280 size = sizeof(*entry) + len + 1;
1281 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1284 entry = ring_buffer_event_data(event);
1286 entry->depth = depth;
1288 memcpy(&entry->buf, trace_buf, len);
1289 entry->buf[len] = 0;
1290 ring_buffer_unlock_commit(tr->buffer, event);
1293 __raw_spin_unlock(&trace_buf_lock);
1294 raw_local_irq_restore(irq_flags);
1295 unpause_graph_tracing();
1297 preempt_enable_notrace();
1301 EXPORT_SYMBOL_GPL(trace_vprintk);
1303 enum trace_file_type {
1304 TRACE_FILE_LAT_FMT = 1,
1305 TRACE_FILE_ANNOTATE = 2,
1308 static void trace_iterator_increment(struct trace_iterator *iter)
1310 /* Don't allow ftrace to trace into the ring buffers */
1311 ftrace_disable_cpu();
1314 if (iter->buffer_iter[iter->cpu])
1315 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1317 ftrace_enable_cpu();
1320 static struct trace_entry *
1321 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1323 struct ring_buffer_event *event;
1324 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1326 /* Don't allow ftrace to trace into the ring buffers */
1327 ftrace_disable_cpu();
1330 event = ring_buffer_iter_peek(buf_iter, ts);
1332 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1334 ftrace_enable_cpu();
1336 return event ? ring_buffer_event_data(event) : NULL;
1339 static struct trace_entry *
1340 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1342 struct ring_buffer *buffer = iter->tr->buffer;
1343 struct trace_entry *ent, *next = NULL;
1344 int cpu_file = iter->cpu_file;
1345 u64 next_ts = 0, ts;
1350 * If we are in a per_cpu trace file, don't bother by iterating over
1351 * all cpu and peek directly.
1353 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1354 if (ring_buffer_empty_cpu(buffer, cpu_file))
1356 ent = peek_next_entry(iter, cpu_file, ent_ts);
1358 *ent_cpu = cpu_file;
1363 for_each_tracing_cpu(cpu) {
1365 if (ring_buffer_empty_cpu(buffer, cpu))
1368 ent = peek_next_entry(iter, cpu, &ts);
1371 * Pick the entry with the smallest timestamp:
1373 if (ent && (!next || ts < next_ts)) {
1381 *ent_cpu = next_cpu;
1389 /* Find the next real entry, without updating the iterator itself */
1390 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1391 int *ent_cpu, u64 *ent_ts)
1393 return __find_next_entry(iter, ent_cpu, ent_ts);
1396 /* Find the next real entry, and increment the iterator to the next entry */
1397 static void *find_next_entry_inc(struct trace_iterator *iter)
1399 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1402 trace_iterator_increment(iter);
1404 return iter->ent ? iter : NULL;
1407 static void trace_consume(struct trace_iterator *iter)
1409 /* Don't allow ftrace to trace into the ring buffers */
1410 ftrace_disable_cpu();
1411 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1412 ftrace_enable_cpu();
1415 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1417 struct trace_iterator *iter = m->private;
1423 /* can't go backwards */
1428 ent = find_next_entry_inc(iter);
1432 while (ent && iter->idx < i)
1433 ent = find_next_entry_inc(iter);
1441 * No necessary locking here. The worst thing which can
1442 * happen is loosing events consumed at the same time
1443 * by a trace_pipe reader.
1444 * Other than that, we don't risk to crash the ring buffer
1445 * because it serializes the readers.
1447 * The current tracer is copied to avoid a global locking
1450 static void *s_start(struct seq_file *m, loff_t *pos)
1452 struct trace_iterator *iter = m->private;
1453 static struct tracer *old_tracer;
1454 int cpu_file = iter->cpu_file;
1459 /* copy the tracer to avoid using a global lock all around */
1460 mutex_lock(&trace_types_lock);
1461 if (unlikely(old_tracer != current_trace && current_trace)) {
1462 old_tracer = current_trace;
1463 *iter->trace = *current_trace;
1465 mutex_unlock(&trace_types_lock);
1467 atomic_inc(&trace_record_cmdline_disabled);
1469 if (*pos != iter->pos) {
1474 ftrace_disable_cpu();
1476 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1477 for_each_tracing_cpu(cpu)
1478 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1480 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1483 ftrace_enable_cpu();
1485 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1490 p = s_next(m, p, &l);
1496 static void s_stop(struct seq_file *m, void *p)
1498 atomic_dec(&trace_record_cmdline_disabled);
1501 static void print_lat_help_header(struct seq_file *m)
1503 seq_puts(m, "# _------=> CPU# \n");
1504 seq_puts(m, "# / _-----=> irqs-off \n");
1505 seq_puts(m, "# | / _----=> need-resched \n");
1506 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1507 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1508 seq_puts(m, "# |||| / \n");
1509 seq_puts(m, "# ||||| delay \n");
1510 seq_puts(m, "# cmd pid ||||| time | caller \n");
1511 seq_puts(m, "# \\ / ||||| \\ | / \n");
1514 static void print_func_help_header(struct seq_file *m)
1516 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1517 seq_puts(m, "# | | | | |\n");
1522 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1524 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1525 struct trace_array *tr = iter->tr;
1526 struct trace_array_cpu *data = tr->data[tr->cpu];
1527 struct tracer *type = current_trace;
1528 unsigned long total;
1529 unsigned long entries;
1530 const char *name = "preemption";
1535 entries = ring_buffer_entries(iter->tr->buffer);
1537 ring_buffer_overruns(iter->tr->buffer);
1539 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1541 seq_puts(m, "# -----------------------------------"
1542 "---------------------------------\n");
1543 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1544 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1545 nsecs_to_usecs(data->saved_latency),
1549 #if defined(CONFIG_PREEMPT_NONE)
1551 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1553 #elif defined(CONFIG_PREEMPT)
1558 /* These are reserved for later use */
1561 seq_printf(m, " #P:%d)\n", num_online_cpus());
1565 seq_puts(m, "# -----------------\n");
1566 seq_printf(m, "# | task: %.16s-%d "
1567 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1568 data->comm, data->pid, data->uid, data->nice,
1569 data->policy, data->rt_priority);
1570 seq_puts(m, "# -----------------\n");
1572 if (data->critical_start) {
1573 seq_puts(m, "# => started at: ");
1574 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1575 trace_print_seq(m, &iter->seq);
1576 seq_puts(m, "\n# => ended at: ");
1577 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1578 trace_print_seq(m, &iter->seq);
1585 static void test_cpu_buff_start(struct trace_iterator *iter)
1587 struct trace_seq *s = &iter->seq;
1589 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1592 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1595 if (cpumask_test_cpu(iter->cpu, iter->started))
1598 cpumask_set_cpu(iter->cpu, iter->started);
1599 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1602 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1604 struct trace_seq *s = &iter->seq;
1605 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1606 struct trace_entry *entry;
1607 struct trace_event *event;
1611 test_cpu_buff_start(iter);
1613 event = ftrace_find_event(entry->type);
1615 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1616 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1617 if (!trace_print_lat_context(iter))
1620 if (!trace_print_context(iter))
1626 return event->trace(iter, sym_flags);
1628 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1631 return TRACE_TYPE_HANDLED;
1633 return TRACE_TYPE_PARTIAL_LINE;
1636 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1638 struct trace_seq *s = &iter->seq;
1639 struct trace_entry *entry;
1640 struct trace_event *event;
1644 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1645 if (!trace_seq_printf(s, "%d %d %llu ",
1646 entry->pid, iter->cpu, iter->ts))
1650 event = ftrace_find_event(entry->type);
1652 return event->raw(iter, 0);
1654 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1657 return TRACE_TYPE_HANDLED;
1659 return TRACE_TYPE_PARTIAL_LINE;
1662 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1664 struct trace_seq *s = &iter->seq;
1665 unsigned char newline = '\n';
1666 struct trace_entry *entry;
1667 struct trace_event *event;
1671 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1672 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1673 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1674 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1677 event = ftrace_find_event(entry->type);
1679 enum print_line_t ret = event->hex(iter, 0);
1680 if (ret != TRACE_TYPE_HANDLED)
1684 SEQ_PUT_FIELD_RET(s, newline);
1686 return TRACE_TYPE_HANDLED;
1689 static enum print_line_t print_bprintk_msg_only(struct trace_iterator *iter)
1691 struct trace_seq *s = &iter->seq;
1692 struct trace_entry *entry = iter->ent;
1693 struct bprint_entry *field;
1696 trace_assign_type(field, entry);
1698 ret = trace_seq_bprintf(s, field->fmt, field->buf);
1700 return TRACE_TYPE_PARTIAL_LINE;
1702 return TRACE_TYPE_HANDLED;
1705 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1707 struct trace_seq *s = &iter->seq;
1708 struct trace_entry *entry = iter->ent;
1709 struct print_entry *field;
1712 trace_assign_type(field, entry);
1714 ret = trace_seq_printf(s, "%s", field->buf);
1716 return TRACE_TYPE_PARTIAL_LINE;
1718 return TRACE_TYPE_HANDLED;
1721 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1723 struct trace_seq *s = &iter->seq;
1724 struct trace_entry *entry;
1725 struct trace_event *event;
1729 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1730 SEQ_PUT_FIELD_RET(s, entry->pid);
1731 SEQ_PUT_FIELD_RET(s, iter->cpu);
1732 SEQ_PUT_FIELD_RET(s, iter->ts);
1735 event = ftrace_find_event(entry->type);
1736 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1739 static int trace_empty(struct trace_iterator *iter)
1743 /* If we are looking at one CPU buffer, only check that one */
1744 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1745 cpu = iter->cpu_file;
1746 if (iter->buffer_iter[cpu]) {
1747 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1750 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1756 for_each_tracing_cpu(cpu) {
1757 if (iter->buffer_iter[cpu]) {
1758 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1761 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1769 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1771 enum print_line_t ret;
1773 if (iter->trace && iter->trace->print_line) {
1774 ret = iter->trace->print_line(iter);
1775 if (ret != TRACE_TYPE_UNHANDLED)
1779 if (iter->ent->type == TRACE_BPRINT &&
1780 trace_flags & TRACE_ITER_PRINTK &&
1781 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1782 return print_bprintk_msg_only(iter);
1784 if (iter->ent->type == TRACE_PRINT &&
1785 trace_flags & TRACE_ITER_PRINTK &&
1786 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1787 return print_printk_msg_only(iter);
1789 if (trace_flags & TRACE_ITER_BIN)
1790 return print_bin_fmt(iter);
1792 if (trace_flags & TRACE_ITER_HEX)
1793 return print_hex_fmt(iter);
1795 if (trace_flags & TRACE_ITER_RAW)
1796 return print_raw_fmt(iter);
1798 return print_trace_fmt(iter);
1801 static int s_show(struct seq_file *m, void *v)
1803 struct trace_iterator *iter = v;
1805 if (iter->ent == NULL) {
1807 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1810 if (iter->trace && iter->trace->print_header)
1811 iter->trace->print_header(m);
1812 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1813 /* print nothing if the buffers are empty */
1814 if (trace_empty(iter))
1816 print_trace_header(m, iter);
1817 if (!(trace_flags & TRACE_ITER_VERBOSE))
1818 print_lat_help_header(m);
1820 if (!(trace_flags & TRACE_ITER_VERBOSE))
1821 print_func_help_header(m);
1824 print_trace_line(iter);
1825 trace_print_seq(m, &iter->seq);
1831 static struct seq_operations tracer_seq_ops = {
1838 static struct trace_iterator *
1839 __tracing_open(struct inode *inode, struct file *file)
1841 long cpu_file = (long) inode->i_private;
1842 void *fail_ret = ERR_PTR(-ENOMEM);
1843 struct trace_iterator *iter;
1847 if (tracing_disabled)
1848 return ERR_PTR(-ENODEV);
1850 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1852 return ERR_PTR(-ENOMEM);
1855 * We make a copy of the current tracer to avoid concurrent
1856 * changes on it while we are reading.
1858 mutex_lock(&trace_types_lock);
1859 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1864 *iter->trace = *current_trace;
1866 if (current_trace && current_trace->print_max)
1869 iter->tr = &global_trace;
1871 mutex_init(&iter->mutex);
1872 iter->cpu_file = cpu_file;
1874 /* Notify the tracer early; before we stop tracing. */
1875 if (iter->trace && iter->trace->open)
1876 iter->trace->open(iter);
1878 /* Annotate start of buffers if we had overruns */
1879 if (ring_buffer_overruns(iter->tr->buffer))
1880 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1882 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1883 for_each_tracing_cpu(cpu) {
1885 iter->buffer_iter[cpu] =
1886 ring_buffer_read_start(iter->tr->buffer, cpu);
1889 cpu = iter->cpu_file;
1890 iter->buffer_iter[cpu] =
1891 ring_buffer_read_start(iter->tr->buffer, cpu);
1894 /* TODO stop tracer */
1895 ret = seq_open(file, &tracer_seq_ops);
1897 fail_ret = ERR_PTR(ret);
1901 m = file->private_data;
1904 /* stop the trace while dumping */
1907 mutex_unlock(&trace_types_lock);
1912 for_each_tracing_cpu(cpu) {
1913 if (iter->buffer_iter[cpu])
1914 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1917 mutex_unlock(&trace_types_lock);
1924 int tracing_open_generic(struct inode *inode, struct file *filp)
1926 if (tracing_disabled)
1929 filp->private_data = inode->i_private;
1933 static int tracing_release(struct inode *inode, struct file *file)
1935 struct seq_file *m = (struct seq_file *)file->private_data;
1936 struct trace_iterator *iter = m->private;
1939 mutex_lock(&trace_types_lock);
1940 for_each_tracing_cpu(cpu) {
1941 if (iter->buffer_iter[cpu])
1942 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1945 if (iter->trace && iter->trace->close)
1946 iter->trace->close(iter);
1948 /* reenable tracing if it was previously enabled */
1950 mutex_unlock(&trace_types_lock);
1952 seq_release(inode, file);
1953 mutex_destroy(&iter->mutex);
1959 static int tracing_open(struct inode *inode, struct file *file)
1961 struct trace_iterator *iter;
1964 iter = __tracing_open(inode, file);
1966 ret = PTR_ERR(iter);
1967 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1968 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1974 t_next(struct seq_file *m, void *v, loff_t *pos)
1976 struct tracer *t = m->private;
1988 static void *t_start(struct seq_file *m, loff_t *pos)
1990 struct tracer *t = m->private;
1993 mutex_lock(&trace_types_lock);
1994 for (; t && l < *pos; t = t_next(m, t, &l))
2000 static void t_stop(struct seq_file *m, void *p)
2002 mutex_unlock(&trace_types_lock);
2005 static int t_show(struct seq_file *m, void *v)
2007 struct tracer *t = v;
2012 seq_printf(m, "%s", t->name);
2021 static struct seq_operations show_traces_seq_ops = {
2028 static int show_traces_open(struct inode *inode, struct file *file)
2032 if (tracing_disabled)
2035 ret = seq_open(file, &show_traces_seq_ops);
2037 struct seq_file *m = file->private_data;
2038 m->private = trace_types;
2044 static const struct file_operations tracing_fops = {
2045 .open = tracing_open,
2047 .llseek = seq_lseek,
2048 .release = tracing_release,
2051 static const struct file_operations show_traces_fops = {
2052 .open = show_traces_open,
2054 .release = seq_release,
2058 * Only trace on a CPU if the bitmask is set:
2060 static cpumask_var_t tracing_cpumask;
2063 * The tracer itself will not take this lock, but still we want
2064 * to provide a consistent cpumask to user-space:
2066 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2069 * Temporary storage for the character representation of the
2070 * CPU bitmask (and one more byte for the newline):
2072 static char mask_str[NR_CPUS + 1];
2075 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2076 size_t count, loff_t *ppos)
2080 mutex_lock(&tracing_cpumask_update_lock);
2082 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2083 if (count - len < 2) {
2087 len += sprintf(mask_str + len, "\n");
2088 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2091 mutex_unlock(&tracing_cpumask_update_lock);
2097 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2098 size_t count, loff_t *ppos)
2101 cpumask_var_t tracing_cpumask_new;
2103 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2106 mutex_lock(&tracing_cpumask_update_lock);
2107 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2111 local_irq_disable();
2112 __raw_spin_lock(&ftrace_max_lock);
2113 for_each_tracing_cpu(cpu) {
2115 * Increase/decrease the disabled counter if we are
2116 * about to flip a bit in the cpumask:
2118 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2119 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2120 atomic_inc(&global_trace.data[cpu]->disabled);
2122 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2123 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2124 atomic_dec(&global_trace.data[cpu]->disabled);
2127 __raw_spin_unlock(&ftrace_max_lock);
2130 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2132 mutex_unlock(&tracing_cpumask_update_lock);
2133 free_cpumask_var(tracing_cpumask_new);
2138 mutex_unlock(&tracing_cpumask_update_lock);
2139 free_cpumask_var(tracing_cpumask);
2144 static const struct file_operations tracing_cpumask_fops = {
2145 .open = tracing_open_generic,
2146 .read = tracing_cpumask_read,
2147 .write = tracing_cpumask_write,
2151 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2152 size_t cnt, loff_t *ppos)
2154 struct tracer_opt *trace_opts;
2162 /* calculate max size */
2163 for (i = 0; trace_options[i]; i++) {
2164 len += strlen(trace_options[i]);
2165 len += 3; /* "no" and newline */
2168 mutex_lock(&trace_types_lock);
2169 tracer_flags = current_trace->flags->val;
2170 trace_opts = current_trace->flags->opts;
2173 * Increase the size with names of options specific
2174 * of the current tracer.
2176 for (i = 0; trace_opts[i].name; i++) {
2177 len += strlen(trace_opts[i].name);
2178 len += 3; /* "no" and newline */
2181 /* +2 for \n and \0 */
2182 buf = kmalloc(len + 2, GFP_KERNEL);
2184 mutex_unlock(&trace_types_lock);
2188 for (i = 0; trace_options[i]; i++) {
2189 if (trace_flags & (1 << i))
2190 r += sprintf(buf + r, "%s\n", trace_options[i]);
2192 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2195 for (i = 0; trace_opts[i].name; i++) {
2196 if (tracer_flags & trace_opts[i].bit)
2197 r += sprintf(buf + r, "%s\n",
2198 trace_opts[i].name);
2200 r += sprintf(buf + r, "no%s\n",
2201 trace_opts[i].name);
2203 mutex_unlock(&trace_types_lock);
2205 WARN_ON(r >= len + 2);
2207 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2213 /* Try to assign a tracer specific option */
2214 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2216 struct tracer_flags *trace_flags = trace->flags;
2217 struct tracer_opt *opts = NULL;
2221 for (i = 0; trace_flags->opts[i].name; i++) {
2222 opts = &trace_flags->opts[i];
2223 len = strlen(opts->name);
2225 if (strncmp(cmp, opts->name, len) == 0) {
2226 ret = trace->set_flag(trace_flags->val,
2232 if (!trace_flags->opts[i].name)
2235 /* Refused to handle */
2240 trace_flags->val &= ~opts->bit;
2242 trace_flags->val |= opts->bit;
2248 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2249 size_t cnt, loff_t *ppos)
2257 if (cnt >= sizeof(buf))
2260 if (copy_from_user(&buf, ubuf, cnt))
2265 if (strncmp(buf, "no", 2) == 0) {
2270 for (i = 0; trace_options[i]; i++) {
2271 int len = strlen(trace_options[i]);
2273 if (strncmp(cmp, trace_options[i], len) == 0) {
2275 trace_flags &= ~(1 << i);
2277 trace_flags |= (1 << i);
2282 /* If no option could be set, test the specific tracer options */
2283 if (!trace_options[i]) {
2284 mutex_lock(&trace_types_lock);
2285 ret = set_tracer_option(current_trace, cmp, neg);
2286 mutex_unlock(&trace_types_lock);
2296 static const struct file_operations tracing_iter_fops = {
2297 .open = tracing_open_generic,
2298 .read = tracing_trace_options_read,
2299 .write = tracing_trace_options_write,
2302 static const char readme_msg[] =
2303 "tracing mini-HOWTO:\n\n"
2305 "# mount -t debugfs nodev /debug\n\n"
2306 "# cat /debug/tracing/available_tracers\n"
2307 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2308 "# cat /debug/tracing/current_tracer\n"
2310 "# echo sched_switch > /debug/tracing/current_tracer\n"
2311 "# cat /debug/tracing/current_tracer\n"
2313 "# cat /debug/tracing/trace_options\n"
2314 "noprint-parent nosym-offset nosym-addr noverbose\n"
2315 "# echo print-parent > /debug/tracing/trace_options\n"
2316 "# echo 1 > /debug/tracing/tracing_enabled\n"
2317 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2318 "echo 0 > /debug/tracing/tracing_enabled\n"
2322 tracing_readme_read(struct file *filp, char __user *ubuf,
2323 size_t cnt, loff_t *ppos)
2325 return simple_read_from_buffer(ubuf, cnt, ppos,
2326 readme_msg, strlen(readme_msg));
2329 static const struct file_operations tracing_readme_fops = {
2330 .open = tracing_open_generic,
2331 .read = tracing_readme_read,
2335 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2336 size_t cnt, loff_t *ppos)
2341 r = sprintf(buf, "%u\n", tracer_enabled);
2342 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2346 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2347 size_t cnt, loff_t *ppos)
2349 struct trace_array *tr = filp->private_data;
2354 if (cnt >= sizeof(buf))
2357 if (copy_from_user(&buf, ubuf, cnt))
2362 ret = strict_strtoul(buf, 10, &val);
2368 mutex_lock(&trace_types_lock);
2369 if (tracer_enabled ^ val) {
2372 if (current_trace->start)
2373 current_trace->start(tr);
2378 if (current_trace->stop)
2379 current_trace->stop(tr);
2382 mutex_unlock(&trace_types_lock);
2390 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2391 size_t cnt, loff_t *ppos)
2393 char buf[max_tracer_type_len+2];
2396 mutex_lock(&trace_types_lock);
2398 r = sprintf(buf, "%s\n", current_trace->name);
2400 r = sprintf(buf, "\n");
2401 mutex_unlock(&trace_types_lock);
2403 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2406 int tracer_init(struct tracer *t, struct trace_array *tr)
2408 tracing_reset_online_cpus(tr);
2412 static int tracing_resize_ring_buffer(unsigned long size)
2417 * If kernel or user changes the size of the ring buffer
2418 * we use the size that was given, and we can forget about
2419 * expanding it later.
2421 ring_buffer_expanded = 1;
2423 ret = ring_buffer_resize(global_trace.buffer, size);
2427 ret = ring_buffer_resize(max_tr.buffer, size);
2431 r = ring_buffer_resize(global_trace.buffer,
2432 global_trace.entries);
2435 * AARGH! We are left with different
2436 * size max buffer!!!!
2437 * The max buffer is our "snapshot" buffer.
2438 * When a tracer needs a snapshot (one of the
2439 * latency tracers), it swaps the max buffer
2440 * with the saved snap shot. We succeeded to
2441 * update the size of the main buffer, but failed to
2442 * update the size of the max buffer. But when we tried
2443 * to reset the main buffer to the original size, we
2444 * failed there too. This is very unlikely to
2445 * happen, but if it does, warn and kill all
2449 tracing_disabled = 1;
2454 global_trace.entries = size;
2460 * tracing_update_buffers - used by tracing facility to expand ring buffers
2462 * To save on memory when the tracing is never used on a system with it
2463 * configured in. The ring buffers are set to a minimum size. But once
2464 * a user starts to use the tracing facility, then they need to grow
2465 * to their default size.
2467 * This function is to be called when a tracer is about to be used.
2469 int tracing_update_buffers(void)
2473 mutex_lock(&trace_types_lock);
2474 if (!ring_buffer_expanded)
2475 ret = tracing_resize_ring_buffer(trace_buf_size);
2476 mutex_unlock(&trace_types_lock);
2481 struct trace_option_dentry;
2483 static struct trace_option_dentry *
2484 create_trace_option_files(struct tracer *tracer);
2487 destroy_trace_option_files(struct trace_option_dentry *topts);
2489 static int tracing_set_tracer(const char *buf)
2491 static struct trace_option_dentry *topts;
2492 struct trace_array *tr = &global_trace;
2496 mutex_lock(&trace_types_lock);
2498 if (!ring_buffer_expanded) {
2499 ret = tracing_resize_ring_buffer(trace_buf_size);
2505 for (t = trace_types; t; t = t->next) {
2506 if (strcmp(t->name, buf) == 0)
2513 if (t == current_trace)
2516 trace_branch_disable();
2517 if (current_trace && current_trace->reset)
2518 current_trace->reset(tr);
2520 destroy_trace_option_files(topts);
2524 topts = create_trace_option_files(current_trace);
2527 ret = tracer_init(t, tr);
2532 trace_branch_enable(tr);
2534 mutex_unlock(&trace_types_lock);
2540 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2541 size_t cnt, loff_t *ppos)
2543 char buf[max_tracer_type_len+1];
2550 if (cnt > max_tracer_type_len)
2551 cnt = max_tracer_type_len;
2553 if (copy_from_user(&buf, ubuf, cnt))
2558 /* strip ending whitespace. */
2559 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2562 err = tracing_set_tracer(buf);
2572 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2573 size_t cnt, loff_t *ppos)
2575 unsigned long *ptr = filp->private_data;
2579 r = snprintf(buf, sizeof(buf), "%ld\n",
2580 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2581 if (r > sizeof(buf))
2583 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2587 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2588 size_t cnt, loff_t *ppos)
2590 unsigned long *ptr = filp->private_data;
2595 if (cnt >= sizeof(buf))
2598 if (copy_from_user(&buf, ubuf, cnt))
2603 ret = strict_strtoul(buf, 10, &val);
2612 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2614 long cpu_file = (long) inode->i_private;
2615 struct trace_iterator *iter;
2618 if (tracing_disabled)
2621 mutex_lock(&trace_types_lock);
2623 /* We only allow one reader per cpu */
2624 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2625 if (!cpumask_empty(tracing_reader_cpumask)) {
2629 cpumask_setall(tracing_reader_cpumask);
2631 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2632 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2639 /* create a buffer to store the information to pass to userspace */
2640 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2647 * We make a copy of the current tracer to avoid concurrent
2648 * changes on it while we are reading.
2650 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2656 *iter->trace = *current_trace;
2658 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2663 /* trace pipe does not show start of buffer */
2664 cpumask_setall(iter->started);
2666 iter->cpu_file = cpu_file;
2667 iter->tr = &global_trace;
2668 mutex_init(&iter->mutex);
2669 filp->private_data = iter;
2671 if (iter->trace->pipe_open)
2672 iter->trace->pipe_open(iter);
2675 mutex_unlock(&trace_types_lock);
2681 mutex_unlock(&trace_types_lock);
2685 static int tracing_release_pipe(struct inode *inode, struct file *file)
2687 struct trace_iterator *iter = file->private_data;
2689 mutex_lock(&trace_types_lock);
2691 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2692 cpumask_clear(tracing_reader_cpumask);
2694 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2696 mutex_unlock(&trace_types_lock);
2698 free_cpumask_var(iter->started);
2699 mutex_destroy(&iter->mutex);
2707 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2709 struct trace_iterator *iter = filp->private_data;
2711 if (trace_flags & TRACE_ITER_BLOCK) {
2713 * Always select as readable when in blocking mode
2715 return POLLIN | POLLRDNORM;
2717 if (!trace_empty(iter))
2718 return POLLIN | POLLRDNORM;
2719 poll_wait(filp, &trace_wait, poll_table);
2720 if (!trace_empty(iter))
2721 return POLLIN | POLLRDNORM;
2728 void default_wait_pipe(struct trace_iterator *iter)
2732 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2734 if (trace_empty(iter))
2737 finish_wait(&trace_wait, &wait);
2741 * This is a make-shift waitqueue.
2742 * A tracer might use this callback on some rare cases:
2744 * 1) the current tracer might hold the runqueue lock when it wakes up
2745 * a reader, hence a deadlock (sched, function, and function graph tracers)
2746 * 2) the function tracers, trace all functions, we don't want
2747 * the overhead of calling wake_up and friends
2748 * (and tracing them too)
2750 * Anyway, this is really very primitive wakeup.
2752 void poll_wait_pipe(struct trace_iterator *iter)
2754 set_current_state(TASK_INTERRUPTIBLE);
2755 /* sleep for 100 msecs, and try again. */
2756 schedule_timeout(HZ / 10);
2759 /* Must be called with trace_types_lock mutex held. */
2760 static int tracing_wait_pipe(struct file *filp)
2762 struct trace_iterator *iter = filp->private_data;
2764 while (trace_empty(iter)) {
2766 if ((filp->f_flags & O_NONBLOCK)) {
2770 mutex_unlock(&iter->mutex);
2772 iter->trace->wait_pipe(iter);
2774 mutex_lock(&iter->mutex);
2776 if (signal_pending(current))
2780 * We block until we read something and tracing is disabled.
2781 * We still block if tracing is disabled, but we have never
2782 * read anything. This allows a user to cat this file, and
2783 * then enable tracing. But after we have read something,
2784 * we give an EOF when tracing is again disabled.
2786 * iter->pos will be 0 if we haven't read anything.
2788 if (!tracer_enabled && iter->pos)
2799 tracing_read_pipe(struct file *filp, char __user *ubuf,
2800 size_t cnt, loff_t *ppos)
2802 struct trace_iterator *iter = filp->private_data;
2803 static struct tracer *old_tracer;
2806 /* return any leftover data */
2807 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2811 trace_seq_init(&iter->seq);
2813 /* copy the tracer to avoid using a global lock all around */
2814 mutex_lock(&trace_types_lock);
2815 if (unlikely(old_tracer != current_trace && current_trace)) {
2816 old_tracer = current_trace;
2817 *iter->trace = *current_trace;
2819 mutex_unlock(&trace_types_lock);
2822 * Avoid more than one consumer on a single file descriptor
2823 * This is just a matter of traces coherency, the ring buffer itself
2826 mutex_lock(&iter->mutex);
2827 if (iter->trace->read) {
2828 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2834 sret = tracing_wait_pipe(filp);
2838 /* stop when tracing is finished */
2839 if (trace_empty(iter)) {
2844 if (cnt >= PAGE_SIZE)
2845 cnt = PAGE_SIZE - 1;
2847 /* reset all but tr, trace, and overruns */
2848 memset(&iter->seq, 0,
2849 sizeof(struct trace_iterator) -
2850 offsetof(struct trace_iterator, seq));
2853 while (find_next_entry_inc(iter) != NULL) {
2854 enum print_line_t ret;
2855 int len = iter->seq.len;
2857 ret = print_trace_line(iter);
2858 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2859 /* don't print partial lines */
2860 iter->seq.len = len;
2863 if (ret != TRACE_TYPE_NO_CONSUME)
2864 trace_consume(iter);
2866 if (iter->seq.len >= cnt)
2870 /* Now copy what we have to the user */
2871 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2872 if (iter->seq.readpos >= iter->seq.len)
2873 trace_seq_init(&iter->seq);
2876 * If there was nothing to send to user, inspite of consuming trace
2877 * entries, go back to wait for more entries.
2883 mutex_unlock(&iter->mutex);
2888 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2889 struct pipe_buffer *buf)
2891 __free_page(buf->page);
2894 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2897 __free_page(spd->pages[idx]);
2900 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2902 .map = generic_pipe_buf_map,
2903 .unmap = generic_pipe_buf_unmap,
2904 .confirm = generic_pipe_buf_confirm,
2905 .release = tracing_pipe_buf_release,
2906 .steal = generic_pipe_buf_steal,
2907 .get = generic_pipe_buf_get,
2911 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2916 /* Seq buffer is page-sized, exactly what we need. */
2918 count = iter->seq.len;
2919 ret = print_trace_line(iter);
2920 count = iter->seq.len - count;
2923 iter->seq.len -= count;
2926 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2927 iter->seq.len -= count;
2931 trace_consume(iter);
2933 if (!find_next_entry_inc(iter)) {
2943 static ssize_t tracing_splice_read_pipe(struct file *filp,
2945 struct pipe_inode_info *pipe,
2949 struct page *pages[PIPE_BUFFERS];
2950 struct partial_page partial[PIPE_BUFFERS];
2951 struct trace_iterator *iter = filp->private_data;
2952 struct splice_pipe_desc spd = {
2955 .nr_pages = 0, /* This gets updated below. */
2957 .ops = &tracing_pipe_buf_ops,
2958 .spd_release = tracing_spd_release_pipe,
2960 static struct tracer *old_tracer;
2965 /* copy the tracer to avoid using a global lock all around */
2966 mutex_lock(&trace_types_lock);
2967 if (unlikely(old_tracer != current_trace && current_trace)) {
2968 old_tracer = current_trace;
2969 *iter->trace = *current_trace;
2971 mutex_unlock(&trace_types_lock);
2973 mutex_lock(&iter->mutex);
2975 if (iter->trace->splice_read) {
2976 ret = iter->trace->splice_read(iter, filp,
2977 ppos, pipe, len, flags);
2982 ret = tracing_wait_pipe(filp);
2986 if (!iter->ent && !find_next_entry_inc(iter)) {
2991 /* Fill as many pages as possible. */
2992 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2993 pages[i] = alloc_page(GFP_KERNEL);
2997 rem = tracing_fill_pipe_page(rem, iter);
2999 /* Copy the data into the page, so we can start over. */
3000 ret = trace_seq_to_buffer(&iter->seq,
3001 page_address(pages[i]),
3004 __free_page(pages[i]);
3007 partial[i].offset = 0;
3008 partial[i].len = iter->seq.len;
3010 trace_seq_init(&iter->seq);
3013 mutex_unlock(&iter->mutex);
3017 return splice_to_pipe(pipe, &spd);
3020 mutex_unlock(&iter->mutex);
3026 tracing_entries_read(struct file *filp, char __user *ubuf,
3027 size_t cnt, loff_t *ppos)
3029 struct trace_array *tr = filp->private_data;
3033 mutex_lock(&trace_types_lock);
3034 if (!ring_buffer_expanded)
3035 r = sprintf(buf, "%lu (expanded: %lu)\n",
3037 trace_buf_size >> 10);
3039 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3040 mutex_unlock(&trace_types_lock);
3042 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3046 tracing_entries_write(struct file *filp, const char __user *ubuf,
3047 size_t cnt, loff_t *ppos)
3053 if (cnt >= sizeof(buf))
3056 if (copy_from_user(&buf, ubuf, cnt))
3061 ret = strict_strtoul(buf, 10, &val);
3065 /* must have at least 1 entry */
3069 mutex_lock(&trace_types_lock);
3073 /* disable all cpu buffers */
3074 for_each_tracing_cpu(cpu) {
3075 if (global_trace.data[cpu])
3076 atomic_inc(&global_trace.data[cpu]->disabled);
3077 if (max_tr.data[cpu])
3078 atomic_inc(&max_tr.data[cpu]->disabled);
3081 /* value is in KB */
3084 if (val != global_trace.entries) {
3085 ret = tracing_resize_ring_buffer(val);
3094 /* If check pages failed, return ENOMEM */
3095 if (tracing_disabled)
3098 for_each_tracing_cpu(cpu) {
3099 if (global_trace.data[cpu])
3100 atomic_dec(&global_trace.data[cpu]->disabled);
3101 if (max_tr.data[cpu])
3102 atomic_dec(&max_tr.data[cpu]->disabled);
3106 max_tr.entries = global_trace.entries;
3107 mutex_unlock(&trace_types_lock);
3112 static int mark_printk(const char *fmt, ...)
3116 va_start(args, fmt);
3117 ret = trace_vprintk(0, -1, fmt, args);
3123 tracing_mark_write(struct file *filp, const char __user *ubuf,
3124 size_t cnt, loff_t *fpos)
3129 if (tracing_disabled)
3132 if (cnt > TRACE_BUF_SIZE)
3133 cnt = TRACE_BUF_SIZE;
3135 buf = kmalloc(cnt + 1, GFP_KERNEL);
3139 if (copy_from_user(buf, ubuf, cnt)) {
3144 /* Cut from the first nil or newline. */
3146 end = strchr(buf, '\n');
3150 cnt = mark_printk("%s\n", buf);
3157 static const struct file_operations tracing_max_lat_fops = {
3158 .open = tracing_open_generic,
3159 .read = tracing_max_lat_read,
3160 .write = tracing_max_lat_write,
3163 static const struct file_operations tracing_ctrl_fops = {
3164 .open = tracing_open_generic,
3165 .read = tracing_ctrl_read,
3166 .write = tracing_ctrl_write,
3169 static const struct file_operations set_tracer_fops = {
3170 .open = tracing_open_generic,
3171 .read = tracing_set_trace_read,
3172 .write = tracing_set_trace_write,
3175 static const struct file_operations tracing_pipe_fops = {
3176 .open = tracing_open_pipe,
3177 .poll = tracing_poll_pipe,
3178 .read = tracing_read_pipe,
3179 .splice_read = tracing_splice_read_pipe,
3180 .release = tracing_release_pipe,
3183 static const struct file_operations tracing_entries_fops = {
3184 .open = tracing_open_generic,
3185 .read = tracing_entries_read,
3186 .write = tracing_entries_write,
3189 static const struct file_operations tracing_mark_fops = {
3190 .open = tracing_open_generic,
3191 .write = tracing_mark_write,
3194 struct ftrace_buffer_info {
3195 struct trace_array *tr;
3201 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3203 int cpu = (int)(long)inode->i_private;
3204 struct ftrace_buffer_info *info;
3206 if (tracing_disabled)
3209 info = kzalloc(sizeof(*info), GFP_KERNEL);
3213 info->tr = &global_trace;
3215 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3216 /* Force reading ring buffer for first read */
3217 info->read = (unsigned int)-1;
3221 filp->private_data = info;
3231 tracing_buffers_read(struct file *filp, char __user *ubuf,
3232 size_t count, loff_t *ppos)
3234 struct ftrace_buffer_info *info = filp->private_data;
3242 /* Do we have previous read data to read? */
3243 if (info->read < PAGE_SIZE)
3248 ret = ring_buffer_read_page(info->tr->buffer,
3255 pos = ring_buffer_page_len(info->spare);
3257 if (pos < PAGE_SIZE)
3258 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3261 size = PAGE_SIZE - info->read;
3265 ret = copy_to_user(ubuf, info->spare + info->read, size);
3276 static int tracing_buffers_release(struct inode *inode, struct file *file)
3278 struct ftrace_buffer_info *info = file->private_data;
3280 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3287 struct ring_buffer *buffer;
3292 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3293 struct pipe_buffer *buf)
3295 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3300 ring_buffer_free_read_page(ref->buffer, ref->page);
3305 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3306 struct pipe_buffer *buf)
3311 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3312 struct pipe_buffer *buf)
3314 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3319 /* Pipe buffer operations for a buffer. */
3320 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3322 .map = generic_pipe_buf_map,
3323 .unmap = generic_pipe_buf_unmap,
3324 .confirm = generic_pipe_buf_confirm,
3325 .release = buffer_pipe_buf_release,
3326 .steal = buffer_pipe_buf_steal,
3327 .get = buffer_pipe_buf_get,
3331 * Callback from splice_to_pipe(), if we need to release some pages
3332 * at the end of the spd in case we error'ed out in filling the pipe.
3334 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3336 struct buffer_ref *ref =
3337 (struct buffer_ref *)spd->partial[i].private;
3342 ring_buffer_free_read_page(ref->buffer, ref->page);
3344 spd->partial[i].private = 0;
3348 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3349 struct pipe_inode_info *pipe, size_t len,
3352 struct ftrace_buffer_info *info = file->private_data;
3353 struct partial_page partial[PIPE_BUFFERS];
3354 struct page *pages[PIPE_BUFFERS];
3355 struct splice_pipe_desc spd = {
3359 .ops = &buffer_pipe_buf_ops,
3360 .spd_release = buffer_spd_release,
3362 struct buffer_ref *ref;
3367 * We can't seek on a buffer input
3369 if (unlikely(*ppos))
3373 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3377 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3381 ref->buffer = info->tr->buffer;
3382 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3388 r = ring_buffer_read_page(ref->buffer, &ref->page,
3391 ring_buffer_free_read_page(ref->buffer,
3398 * zero out any left over data, this is going to
3401 size = ring_buffer_page_len(ref->page);
3402 if (size < PAGE_SIZE)
3403 memset(ref->page + size, 0, PAGE_SIZE - size);
3405 page = virt_to_page(ref->page);
3407 spd.pages[i] = page;
3408 spd.partial[i].len = PAGE_SIZE;
3409 spd.partial[i].offset = 0;
3410 spd.partial[i].private = (unsigned long)ref;
3416 /* did we read anything? */
3417 if (!spd.nr_pages) {
3418 if (flags & SPLICE_F_NONBLOCK)
3426 ret = splice_to_pipe(pipe, &spd);
3431 static const struct file_operations tracing_buffers_fops = {
3432 .open = tracing_buffers_open,
3433 .read = tracing_buffers_read,
3434 .release = tracing_buffers_release,
3435 .splice_read = tracing_buffers_splice_read,
3436 .llseek = no_llseek,
3439 #ifdef CONFIG_DYNAMIC_FTRACE
3441 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3447 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3448 size_t cnt, loff_t *ppos)
3450 static char ftrace_dyn_info_buffer[1024];
3451 static DEFINE_MUTEX(dyn_info_mutex);
3452 unsigned long *p = filp->private_data;
3453 char *buf = ftrace_dyn_info_buffer;
3454 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3457 mutex_lock(&dyn_info_mutex);
3458 r = sprintf(buf, "%ld ", *p);
3460 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3463 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3465 mutex_unlock(&dyn_info_mutex);
3470 static const struct file_operations tracing_dyn_info_fops = {
3471 .open = tracing_open_generic,
3472 .read = tracing_read_dyn_info,
3476 static struct dentry *d_tracer;
3478 struct dentry *tracing_init_dentry(void)
3485 d_tracer = debugfs_create_dir("tracing", NULL);
3487 if (!d_tracer && !once) {
3489 pr_warning("Could not create debugfs directory 'tracing'\n");
3496 static struct dentry *d_percpu;
3498 struct dentry *tracing_dentry_percpu(void)
3501 struct dentry *d_tracer;
3506 d_tracer = tracing_init_dentry();
3511 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3513 if (!d_percpu && !once) {
3515 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3522 static void tracing_init_debugfs_percpu(long cpu)
3524 struct dentry *d_percpu = tracing_dentry_percpu();
3525 struct dentry *entry, *d_cpu;
3526 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3529 if (cpu > 999 || cpu < 0)
3532 sprintf(cpu_dir, "cpu%ld", cpu);
3533 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3535 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3539 /* per cpu trace_pipe */
3540 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
3541 (void *) cpu, &tracing_pipe_fops);
3543 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3546 entry = debugfs_create_file("trace", 0444, d_cpu,
3547 (void *) cpu, &tracing_fops);
3549 pr_warning("Could not create debugfs 'trace' entry\n");
3551 entry = debugfs_create_file("trace_pipe_raw", 0444, d_cpu,
3552 (void *) cpu, &tracing_buffers_fops);
3554 pr_warning("Could not create debugfs 'trace_pipe_raw' entry\n");
3557 #ifdef CONFIG_FTRACE_SELFTEST
3558 /* Let selftest have access to static functions in this file */
3559 #include "trace_selftest.c"
3562 struct trace_option_dentry {
3563 struct tracer_opt *opt;
3564 struct tracer_flags *flags;
3565 struct dentry *entry;
3569 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3572 struct trace_option_dentry *topt = filp->private_data;
3575 if (topt->flags->val & topt->opt->bit)
3580 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3584 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3587 struct trace_option_dentry *topt = filp->private_data;
3592 if (cnt >= sizeof(buf))
3595 if (copy_from_user(&buf, ubuf, cnt))
3600 ret = strict_strtoul(buf, 10, &val);
3607 /* do nothing if already cleared */
3608 if (!(topt->flags->val & topt->opt->bit))
3611 mutex_lock(&trace_types_lock);
3612 if (current_trace->set_flag)
3613 ret = current_trace->set_flag(topt->flags->val,
3615 mutex_unlock(&trace_types_lock);
3618 topt->flags->val &= ~topt->opt->bit;
3621 /* do nothing if already set */
3622 if (topt->flags->val & topt->opt->bit)
3625 mutex_lock(&trace_types_lock);
3626 if (current_trace->set_flag)
3627 ret = current_trace->set_flag(topt->flags->val,
3629 mutex_unlock(&trace_types_lock);
3632 topt->flags->val |= topt->opt->bit;
3645 static const struct file_operations trace_options_fops = {
3646 .open = tracing_open_generic,
3647 .read = trace_options_read,
3648 .write = trace_options_write,
3652 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3655 long index = (long)filp->private_data;
3658 if (trace_flags & (1 << index))
3663 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3667 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3670 long index = (long)filp->private_data;
3675 if (cnt >= sizeof(buf))
3678 if (copy_from_user(&buf, ubuf, cnt))
3683 ret = strict_strtoul(buf, 10, &val);
3689 trace_flags &= ~(1 << index);
3692 trace_flags |= 1 << index;
3704 static const struct file_operations trace_options_core_fops = {
3705 .open = tracing_open_generic,
3706 .read = trace_options_core_read,
3707 .write = trace_options_core_write,
3710 static struct dentry *trace_options_init_dentry(void)
3712 struct dentry *d_tracer;
3713 static struct dentry *t_options;
3718 d_tracer = tracing_init_dentry();
3722 t_options = debugfs_create_dir("options", d_tracer);
3724 pr_warning("Could not create debugfs directory 'options'\n");
3732 create_trace_option_file(struct trace_option_dentry *topt,
3733 struct tracer_flags *flags,
3734 struct tracer_opt *opt)
3736 struct dentry *t_options;
3737 struct dentry *entry;
3739 t_options = trace_options_init_dentry();
3743 topt->flags = flags;
3746 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3747 &trace_options_fops);
3749 topt->entry = entry;
3753 static struct trace_option_dentry *
3754 create_trace_option_files(struct tracer *tracer)
3756 struct trace_option_dentry *topts;
3757 struct tracer_flags *flags;
3758 struct tracer_opt *opts;
3764 flags = tracer->flags;
3766 if (!flags || !flags->opts)
3771 for (cnt = 0; opts[cnt].name; cnt++)
3774 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3778 for (cnt = 0; opts[cnt].name; cnt++)
3779 create_trace_option_file(&topts[cnt], flags,
3786 destroy_trace_option_files(struct trace_option_dentry *topts)
3793 for (cnt = 0; topts[cnt].opt; cnt++) {
3794 if (topts[cnt].entry)
3795 debugfs_remove(topts[cnt].entry);
3801 static struct dentry *
3802 create_trace_option_core_file(const char *option, long index)
3804 struct dentry *t_options;
3805 struct dentry *entry;
3807 t_options = trace_options_init_dentry();
3811 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3812 &trace_options_core_fops);
3817 static __init void create_trace_options_dir(void)
3819 struct dentry *t_options;
3820 struct dentry *entry;
3823 t_options = trace_options_init_dentry();
3827 for (i = 0; trace_options[i]; i++) {
3828 entry = create_trace_option_core_file(trace_options[i], i);
3830 pr_warning("Could not create debugfs %s entry\n",
3835 static __init int tracer_init_debugfs(void)
3837 struct dentry *d_tracer;
3838 struct dentry *entry;
3841 d_tracer = tracing_init_dentry();
3843 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3844 &global_trace, &tracing_ctrl_fops);
3846 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3848 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3849 NULL, &tracing_iter_fops);
3851 pr_warning("Could not create debugfs 'trace_options' entry\n");
3853 create_trace_options_dir();
3855 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3856 NULL, &tracing_cpumask_fops);
3858 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3860 entry = debugfs_create_file("trace", 0444, d_tracer,
3861 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3863 pr_warning("Could not create debugfs 'trace' entry\n");
3865 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3866 &global_trace, &show_traces_fops);
3868 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3870 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3871 &global_trace, &set_tracer_fops);
3873 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3875 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3876 &tracing_max_latency,
3877 &tracing_max_lat_fops);
3879 pr_warning("Could not create debugfs "
3880 "'tracing_max_latency' entry\n");
3882 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3883 &tracing_thresh, &tracing_max_lat_fops);
3885 pr_warning("Could not create debugfs "
3886 "'tracing_thresh' entry\n");
3887 entry = debugfs_create_file("README", 0644, d_tracer,
3888 NULL, &tracing_readme_fops);
3890 pr_warning("Could not create debugfs 'README' entry\n");
3892 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3893 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3895 pr_warning("Could not create debugfs "
3896 "'trace_pipe' entry\n");
3898 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3899 &global_trace, &tracing_entries_fops);
3901 pr_warning("Could not create debugfs "
3902 "'buffer_size_kb' entry\n");
3904 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3905 NULL, &tracing_mark_fops);
3907 pr_warning("Could not create debugfs "
3908 "'trace_marker' entry\n");
3910 #ifdef CONFIG_DYNAMIC_FTRACE
3911 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3912 &ftrace_update_tot_cnt,
3913 &tracing_dyn_info_fops);
3915 pr_warning("Could not create debugfs "
3916 "'dyn_ftrace_total_info' entry\n");
3918 #ifdef CONFIG_SYSPROF_TRACER
3919 init_tracer_sysprof_debugfs(d_tracer);
3922 for_each_tracing_cpu(cpu)
3923 tracing_init_debugfs_percpu(cpu);
3928 static int trace_panic_handler(struct notifier_block *this,
3929 unsigned long event, void *unused)
3931 if (ftrace_dump_on_oops)
3936 static struct notifier_block trace_panic_notifier = {
3937 .notifier_call = trace_panic_handler,
3939 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3942 static int trace_die_handler(struct notifier_block *self,
3948 if (ftrace_dump_on_oops)
3957 static struct notifier_block trace_die_notifier = {
3958 .notifier_call = trace_die_handler,
3963 * printk is set to max of 1024, we really don't need it that big.
3964 * Nothing should be printing 1000 characters anyway.
3966 #define TRACE_MAX_PRINT 1000
3969 * Define here KERN_TRACE so that we have one place to modify
3970 * it if we decide to change what log level the ftrace dump
3973 #define KERN_TRACE KERN_EMERG
3976 trace_printk_seq(struct trace_seq *s)
3978 /* Probably should print a warning here. */
3982 /* should be zero ended, but we are paranoid. */
3983 s->buffer[s->len] = 0;
3985 printk(KERN_TRACE "%s", s->buffer);
3990 void ftrace_dump(void)
3992 static DEFINE_SPINLOCK(ftrace_dump_lock);
3993 /* use static because iter can be a bit big for the stack */
3994 static struct trace_iterator iter;
3995 static int dump_ran;
3996 unsigned long flags;
4000 spin_lock_irqsave(&ftrace_dump_lock, flags);
4006 /* No turning back! */
4010 for_each_tracing_cpu(cpu) {
4011 atomic_inc(&global_trace.data[cpu]->disabled);
4014 /* don't look at user memory in panic mode */
4015 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4017 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4019 /* Simulate the iterator */
4020 iter.tr = &global_trace;
4021 iter.trace = current_trace;
4022 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4025 * We need to stop all tracing on all CPUS to read the
4026 * the next buffer. This is a bit expensive, but is
4027 * not done often. We fill all what we can read,
4028 * and then release the locks again.
4031 while (!trace_empty(&iter)) {
4034 printk(KERN_TRACE "---------------------------------\n");
4038 /* reset all but tr, trace, and overruns */
4039 memset(&iter.seq, 0,
4040 sizeof(struct trace_iterator) -
4041 offsetof(struct trace_iterator, seq));
4042 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4045 if (find_next_entry_inc(&iter) != NULL) {
4046 print_trace_line(&iter);
4047 trace_consume(&iter);
4050 trace_printk_seq(&iter.seq);
4054 printk(KERN_TRACE " (ftrace buffer empty)\n");
4056 printk(KERN_TRACE "---------------------------------\n");
4059 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
4062 __init static int tracer_alloc_buffers(void)
4064 struct trace_array_cpu *data;
4069 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4072 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4073 goto out_free_buffer_mask;
4075 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4076 goto out_free_tracing_cpumask;
4078 /* To save memory, keep the ring buffer size to its minimum */
4079 if (ring_buffer_expanded)
4080 ring_buf_size = trace_buf_size;
4084 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4085 cpumask_copy(tracing_cpumask, cpu_all_mask);
4086 cpumask_clear(tracing_reader_cpumask);
4088 /* TODO: make the number of buffers hot pluggable with CPUS */
4089 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4090 TRACE_BUFFER_FLAGS);
4091 if (!global_trace.buffer) {
4092 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4094 goto out_free_cpumask;
4096 global_trace.entries = ring_buffer_size(global_trace.buffer);
4099 #ifdef CONFIG_TRACER_MAX_TRACE
4100 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4101 TRACE_BUFFER_FLAGS);
4102 if (!max_tr.buffer) {
4103 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4105 ring_buffer_free(global_trace.buffer);
4106 goto out_free_cpumask;
4108 max_tr.entries = ring_buffer_size(max_tr.buffer);
4109 WARN_ON(max_tr.entries != global_trace.entries);
4112 /* Allocate the first page for all buffers */
4113 for_each_tracing_cpu(i) {
4114 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4115 max_tr.data[i] = &per_cpu(max_data, i);
4118 trace_init_cmdlines();
4120 register_tracer(&nop_trace);
4121 current_trace = &nop_trace;
4122 #ifdef CONFIG_BOOT_TRACER
4123 register_tracer(&boot_tracer);
4125 /* All seems OK, enable tracing */
4126 tracing_disabled = 0;
4128 atomic_notifier_chain_register(&panic_notifier_list,
4129 &trace_panic_notifier);
4131 register_die_notifier(&trace_die_notifier);
4136 free_cpumask_var(tracing_reader_cpumask);
4137 out_free_tracing_cpumask:
4138 free_cpumask_var(tracing_cpumask);
4139 out_free_buffer_mask:
4140 free_cpumask_var(tracing_buffer_mask);
4145 __init static int clear_boot_tracer(void)
4148 * The default tracer at boot buffer is an init section.
4149 * This function is called in lateinit. If we did not
4150 * find the boot tracer, then clear it out, to prevent
4151 * later registration from accessing the buffer that is
4152 * about to be freed.
4154 if (!default_bootup_tracer)
4157 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4158 default_bootup_tracer);
4159 default_bootup_tracer = NULL;
4164 early_initcall(tracer_alloc_buffers);
4165 fs_initcall(tracer_init_debugfs);
4166 late_initcall(clear_boot_tracer);