2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/ring_buffer.h>
15 #include <linux/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/ctype.h>
34 #include <linux/init.h>
35 #include <linux/poll.h>
36 #include <linux/gfp.h>
40 #include "trace_output.h"
42 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
44 unsigned long __read_mostly tracing_max_latency;
45 unsigned long __read_mostly tracing_thresh;
48 * On boot up, the ring buffer is set to the minimum size, so that
49 * we do not waste memory on systems that are not using tracing.
51 static int ring_buffer_expanded;
54 * We need to change this state when a selftest is running.
55 * A selftest will lurk into the ring-buffer to count the
56 * entries inserted during the selftest although some concurrent
57 * insertions into the ring-buffer such as trace_printk could occurred
58 * at the same time, giving false positive or negative results.
60 static bool __read_mostly tracing_selftest_running;
63 * If a tracer is running, we do not want to run SELFTEST.
65 static bool __read_mostly tracing_selftest_disabled;
67 /* For tracers that don't implement custom flags */
68 static struct tracer_opt dummy_tracer_opt[] = {
72 static struct tracer_flags dummy_tracer_flags = {
74 .opts = dummy_tracer_opt
77 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
83 * Kill all tracing for good (never come back).
84 * It is initialized to 1 but will turn to zero if the initialization
85 * of the tracer is successful. But that is the only place that sets
88 static int tracing_disabled = 1;
90 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
92 static inline void ftrace_disable_cpu(void)
95 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
98 static inline void ftrace_enable_cpu(void)
100 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
104 static cpumask_var_t __read_mostly tracing_buffer_mask;
106 /* Define which cpu buffers are currently read in trace_pipe */
107 static cpumask_var_t tracing_reader_cpumask;
109 #define for_each_tracing_cpu(cpu) \
110 for_each_cpu(cpu, tracing_buffer_mask)
113 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
115 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
116 * is set, then ftrace_dump is called. This will output the contents
117 * of the ftrace buffers to the console. This is very useful for
118 * capturing traces that lead to crashes and outputing it to a
121 * It is default off, but you can enable it with either specifying
122 * "ftrace_dump_on_oops" in the kernel command line, or setting
123 * /proc/sys/kernel/ftrace_dump_on_oops to true.
125 int ftrace_dump_on_oops;
127 static int tracing_set_tracer(const char *buf);
129 #define BOOTUP_TRACER_SIZE 100
130 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
131 static char *default_bootup_tracer;
133 static int __init set_ftrace(char *str)
135 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
136 default_bootup_tracer = bootup_tracer_buf;
137 /* We are using ftrace early, expand it */
138 ring_buffer_expanded = 1;
141 __setup("ftrace=", set_ftrace);
143 static int __init set_ftrace_dump_on_oops(char *str)
145 ftrace_dump_on_oops = 1;
148 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
151 ns2usecs(cycle_t nsec)
159 * The global_trace is the descriptor that holds the tracing
160 * buffers for the live tracing. For each CPU, it contains
161 * a link list of pages that will store trace entries. The
162 * page descriptor of the pages in the memory is used to hold
163 * the link list by linking the lru item in the page descriptor
164 * to each of the pages in the buffer per CPU.
166 * For each active CPU there is a data field that holds the
167 * pages for the buffer for that CPU. Each CPU has the same number
168 * of pages allocated for its buffer.
170 static struct trace_array global_trace;
172 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
174 cycle_t ftrace_now(int cpu)
178 /* Early boot up does not have a buffer yet */
179 if (!global_trace.buffer)
180 return trace_clock_local();
182 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
183 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
189 * The max_tr is used to snapshot the global_trace when a maximum
190 * latency is reached. Some tracers will use this to store a maximum
191 * trace while it continues examining live traces.
193 * The buffers for the max_tr are set up the same as the global_trace.
194 * When a snapshot is taken, the link list of the max_tr is swapped
195 * with the link list of the global_trace and the buffers are reset for
196 * the global_trace so the tracing can continue.
198 static struct trace_array max_tr;
200 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
202 /* tracer_enabled is used to toggle activation of a tracer */
203 static int tracer_enabled = 1;
206 * tracing_is_enabled - return tracer_enabled status
208 * This function is used by other tracers to know the status
209 * of the tracer_enabled flag. Tracers may use this function
210 * to know if it should enable their features when starting
211 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
213 int tracing_is_enabled(void)
215 return tracer_enabled;
219 * trace_buf_size is the size in bytes that is allocated
220 * for a buffer. Note, the number of bytes is always rounded
223 * This number is purposely set to a low number of 16384.
224 * If the dump on oops happens, it will be much appreciated
225 * to not have to wait for all that output. Anyway this can be
226 * boot time and run time configurable.
228 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
230 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
232 /* trace_types holds a link list of available tracers. */
233 static struct tracer *trace_types __read_mostly;
235 /* current_trace points to the tracer that is currently active */
236 static struct tracer *current_trace __read_mostly;
239 * max_tracer_type_len is used to simplify the allocating of
240 * buffers to read userspace tracer names. We keep track of
241 * the longest tracer name registered.
243 static int max_tracer_type_len;
246 * trace_types_lock is used to protect the trace_types list.
247 * This lock is also used to keep user access serialized.
248 * Accesses from userspace will grab this lock while userspace
249 * activities happen inside the kernel.
251 static DEFINE_MUTEX(trace_types_lock);
253 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
254 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
256 /* trace_flags holds trace_options default values */
257 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
258 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
261 * trace_wake_up - wake up tasks waiting for trace input
263 * Simply wakes up any task that is blocked on the trace_wait
264 * queue. These is used with trace_poll for tasks polling the trace.
266 void trace_wake_up(void)
269 * The runqueue_is_locked() can fail, but this is the best we
272 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
273 wake_up(&trace_wait);
276 static int __init set_buf_size(char *str)
278 unsigned long buf_size;
283 ret = strict_strtoul(str, 0, &buf_size);
284 /* nr_entries can not be zero */
285 if (ret < 0 || buf_size == 0)
287 trace_buf_size = buf_size;
290 __setup("trace_buf_size=", set_buf_size);
292 unsigned long nsecs_to_usecs(unsigned long nsecs)
297 /* These must match the bit postions in trace_iterator_flags */
298 static const char *trace_options[] = {
323 * ftrace_max_lock is used to protect the swapping of buffers
324 * when taking a max snapshot. The buffers themselves are
325 * protected by per_cpu spinlocks. But the action of the swap
326 * needs its own lock.
328 * This is defined as a raw_spinlock_t in order to help
329 * with performance when lockdep debugging is enabled.
331 static raw_spinlock_t ftrace_max_lock =
332 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
335 * Copy the new maximum trace into the separate maximum-trace
336 * structure. (this way the maximum trace is permanently saved,
337 * for later retrieval via /debugfs/tracing/latency_trace)
340 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
342 struct trace_array_cpu *data = tr->data[cpu];
345 max_tr.time_start = data->preempt_timestamp;
347 data = max_tr.data[cpu];
348 data->saved_latency = tracing_max_latency;
350 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
351 data->pid = tsk->pid;
352 data->uid = task_uid(tsk);
353 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
354 data->policy = tsk->policy;
355 data->rt_priority = tsk->rt_priority;
357 /* record this tasks comm */
358 tracing_record_cmdline(tsk);
361 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
369 if (s->len <= s->readpos)
372 len = s->len - s->readpos;
375 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
385 ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
390 if (s->len <= s->readpos)
393 len = s->len - s->readpos;
396 ret = memcpy(buf, s->buffer + s->readpos, cnt);
405 trace_print_seq(struct seq_file *m, struct trace_seq *s)
407 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
410 seq_puts(m, s->buffer);
416 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
418 * @tsk: the task with the latency
419 * @cpu: The cpu that initiated the trace.
421 * Flip the buffers between the @tr and the max_tr and record information
422 * about which task was the cause of this latency.
425 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
427 struct ring_buffer *buf = tr->buffer;
429 WARN_ON_ONCE(!irqs_disabled());
430 __raw_spin_lock(&ftrace_max_lock);
432 tr->buffer = max_tr.buffer;
435 ftrace_disable_cpu();
436 ring_buffer_reset(tr->buffer);
439 __update_max_tr(tr, tsk, cpu);
440 __raw_spin_unlock(&ftrace_max_lock);
444 * update_max_tr_single - only copy one trace over, and reset the rest
446 * @tsk - task with the latency
447 * @cpu - the cpu of the buffer to copy.
449 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
452 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
456 WARN_ON_ONCE(!irqs_disabled());
457 __raw_spin_lock(&ftrace_max_lock);
459 ftrace_disable_cpu();
461 ring_buffer_reset(max_tr.buffer);
462 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
466 WARN_ON_ONCE(ret && ret != -EAGAIN);
468 __update_max_tr(tr, tsk, cpu);
469 __raw_spin_unlock(&ftrace_max_lock);
473 * register_tracer - register a tracer with the ftrace system.
474 * @type - the plugin for the tracer
476 * Register a new plugin tracer.
478 int register_tracer(struct tracer *type)
479 __releases(kernel_lock)
480 __acquires(kernel_lock)
487 pr_info("Tracer must have a name\n");
492 * When this gets called we hold the BKL which means that
493 * preemption is disabled. Various trace selftests however
494 * need to disable and enable preemption for successful tests.
495 * So we drop the BKL here and grab it after the tests again.
498 mutex_lock(&trace_types_lock);
500 tracing_selftest_running = true;
502 for (t = trace_types; t; t = t->next) {
503 if (strcmp(type->name, t->name) == 0) {
505 pr_info("Trace %s already registered\n",
513 type->set_flag = &dummy_set_flag;
515 type->flags = &dummy_tracer_flags;
517 if (!type->flags->opts)
518 type->flags->opts = dummy_tracer_opt;
519 if (!type->wait_pipe)
520 type->wait_pipe = default_wait_pipe;
523 #ifdef CONFIG_FTRACE_STARTUP_TEST
524 if (type->selftest && !tracing_selftest_disabled) {
525 struct tracer *saved_tracer = current_trace;
526 struct trace_array *tr = &global_trace;
530 * Run a selftest on this tracer.
531 * Here we reset the trace buffer, and set the current
532 * tracer to be this tracer. The tracer can then run some
533 * internal tracing to verify that everything is in order.
534 * If we fail, we do not register this tracer.
536 for_each_tracing_cpu(i)
537 tracing_reset(tr, i);
539 current_trace = type;
540 /* the test is responsible for initializing and enabling */
541 pr_info("Testing tracer %s: ", type->name);
542 ret = type->selftest(type, tr);
543 /* the test is responsible for resetting too */
544 current_trace = saved_tracer;
546 printk(KERN_CONT "FAILED!\n");
549 /* Only reset on passing, to avoid touching corrupted buffers */
550 for_each_tracing_cpu(i)
551 tracing_reset(tr, i);
553 printk(KERN_CONT "PASSED\n");
557 type->next = trace_types;
559 len = strlen(type->name);
560 if (len > max_tracer_type_len)
561 max_tracer_type_len = len;
564 tracing_selftest_running = false;
565 mutex_unlock(&trace_types_lock);
567 if (ret || !default_bootup_tracer)
570 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
573 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
574 /* Do we want this tracer to start on bootup? */
575 tracing_set_tracer(type->name);
576 default_bootup_tracer = NULL;
577 /* disable other selftests, since this will break it. */
578 tracing_selftest_disabled = 1;
579 #ifdef CONFIG_FTRACE_STARTUP_TEST
580 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
589 void unregister_tracer(struct tracer *type)
594 mutex_lock(&trace_types_lock);
595 for (t = &trace_types; *t; t = &(*t)->next) {
599 pr_info("Trace %s not registered\n", type->name);
605 if (type == current_trace && tracer_enabled) {
608 if (current_trace->stop)
609 current_trace->stop(&global_trace);
610 current_trace = &nop_trace;
613 if (strlen(type->name) != max_tracer_type_len)
616 max_tracer_type_len = 0;
617 for (t = &trace_types; *t; t = &(*t)->next) {
618 len = strlen((*t)->name);
619 if (len > max_tracer_type_len)
620 max_tracer_type_len = len;
623 mutex_unlock(&trace_types_lock);
626 void tracing_reset(struct trace_array *tr, int cpu)
628 ftrace_disable_cpu();
629 ring_buffer_reset_cpu(tr->buffer, cpu);
633 void tracing_reset_online_cpus(struct trace_array *tr)
637 tr->time_start = ftrace_now(tr->cpu);
639 for_each_online_cpu(cpu)
640 tracing_reset(tr, cpu);
643 #define SAVED_CMDLINES 128
644 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
645 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
646 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
647 static int cmdline_idx;
648 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
650 /* temporary disable recording */
651 static atomic_t trace_record_cmdline_disabled __read_mostly;
653 static void trace_init_cmdlines(void)
655 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
656 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
660 static int trace_stop_count;
661 static DEFINE_SPINLOCK(tracing_start_lock);
664 * ftrace_off_permanent - disable all ftrace code permanently
666 * This should only be called when a serious anomally has
667 * been detected. This will turn off the function tracing,
668 * ring buffers, and other tracing utilites. It takes no
669 * locks and can be called from any context.
671 void ftrace_off_permanent(void)
673 tracing_disabled = 1;
675 tracing_off_permanent();
679 * tracing_start - quick start of the tracer
681 * If tracing is enabled but was stopped by tracing_stop,
682 * this will start the tracer back up.
684 void tracing_start(void)
686 struct ring_buffer *buffer;
689 if (tracing_disabled)
692 spin_lock_irqsave(&tracing_start_lock, flags);
693 if (--trace_stop_count) {
694 if (trace_stop_count < 0) {
695 /* Someone screwed up their debugging */
697 trace_stop_count = 0;
703 buffer = global_trace.buffer;
705 ring_buffer_record_enable(buffer);
707 buffer = max_tr.buffer;
709 ring_buffer_record_enable(buffer);
713 spin_unlock_irqrestore(&tracing_start_lock, flags);
717 * tracing_stop - quick stop of the tracer
719 * Light weight way to stop tracing. Use in conjunction with
722 void tracing_stop(void)
724 struct ring_buffer *buffer;
728 spin_lock_irqsave(&tracing_start_lock, flags);
729 if (trace_stop_count++)
732 buffer = global_trace.buffer;
734 ring_buffer_record_disable(buffer);
736 buffer = max_tr.buffer;
738 ring_buffer_record_disable(buffer);
741 spin_unlock_irqrestore(&tracing_start_lock, flags);
744 void trace_stop_cmdline_recording(void);
746 static void trace_save_cmdline(struct task_struct *tsk)
751 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
755 * It's not the end of the world if we don't get
756 * the lock, but we also don't want to spin
757 * nor do we want to disable interrupts,
758 * so if we miss here, then better luck next time.
760 if (!__raw_spin_trylock(&trace_cmdline_lock))
763 idx = map_pid_to_cmdline[tsk->pid];
764 if (idx >= SAVED_CMDLINES) {
765 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
767 map = map_cmdline_to_pid[idx];
768 if (map <= PID_MAX_DEFAULT)
769 map_pid_to_cmdline[map] = (unsigned)-1;
771 map_pid_to_cmdline[tsk->pid] = idx;
776 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
778 __raw_spin_unlock(&trace_cmdline_lock);
781 void trace_find_cmdline(int pid, char comm[])
786 strcpy(comm, "<idle>");
790 if (pid > PID_MAX_DEFAULT) {
791 strcpy(comm, "<...>");
795 __raw_spin_lock(&trace_cmdline_lock);
796 map = map_pid_to_cmdline[pid];
797 if (map >= SAVED_CMDLINES)
800 strcpy(comm, saved_cmdlines[map]);
803 __raw_spin_unlock(&trace_cmdline_lock);
806 void tracing_record_cmdline(struct task_struct *tsk)
808 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
811 trace_save_cmdline(tsk);
815 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
818 struct task_struct *tsk = current;
820 entry->preempt_count = pc & 0xff;
821 entry->pid = (tsk) ? tsk->pid : 0;
822 entry->tgid = (tsk) ? tsk->tgid : 0;
824 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
825 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
827 TRACE_FLAG_IRQS_NOSUPPORT |
829 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
830 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
831 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
834 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
837 unsigned long flags, int pc)
839 struct ring_buffer_event *event;
841 event = ring_buffer_lock_reserve(tr->buffer, len);
843 struct trace_entry *ent = ring_buffer_event_data(event);
845 tracing_generic_entry_update(ent, flags, pc);
851 static void ftrace_trace_stack(struct trace_array *tr,
852 unsigned long flags, int skip, int pc);
853 static void ftrace_trace_userstack(struct trace_array *tr,
854 unsigned long flags, int pc);
856 void trace_buffer_unlock_commit(struct trace_array *tr,
857 struct ring_buffer_event *event,
858 unsigned long flags, int pc)
860 ring_buffer_unlock_commit(tr->buffer, event);
862 ftrace_trace_stack(tr, flags, 6, pc);
863 ftrace_trace_userstack(tr, flags, pc);
867 struct ring_buffer_event *
868 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
869 unsigned long flags, int pc)
871 return trace_buffer_lock_reserve(&global_trace,
872 type, len, flags, pc);
875 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
876 unsigned long flags, int pc)
878 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
882 trace_function(struct trace_array *tr,
883 unsigned long ip, unsigned long parent_ip, unsigned long flags,
886 struct ring_buffer_event *event;
887 struct ftrace_entry *entry;
889 /* If we are reading the ring buffer, don't trace */
890 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
893 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
897 entry = ring_buffer_event_data(event);
899 entry->parent_ip = parent_ip;
900 ring_buffer_unlock_commit(tr->buffer, event);
903 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
904 static void __trace_graph_entry(struct trace_array *tr,
905 struct ftrace_graph_ent *trace,
909 struct ring_buffer_event *event;
910 struct ftrace_graph_ent_entry *entry;
912 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
915 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
916 sizeof(*entry), flags, pc);
919 entry = ring_buffer_event_data(event);
920 entry->graph_ent = *trace;
921 ring_buffer_unlock_commit(global_trace.buffer, event);
924 static void __trace_graph_return(struct trace_array *tr,
925 struct ftrace_graph_ret *trace,
929 struct ring_buffer_event *event;
930 struct ftrace_graph_ret_entry *entry;
932 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
935 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
936 sizeof(*entry), flags, pc);
939 entry = ring_buffer_event_data(event);
941 ring_buffer_unlock_commit(global_trace.buffer, event);
946 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
947 unsigned long ip, unsigned long parent_ip, unsigned long flags,
950 if (likely(!atomic_read(&data->disabled)))
951 trace_function(tr, ip, parent_ip, flags, pc);
954 static void __ftrace_trace_stack(struct trace_array *tr,
958 #ifdef CONFIG_STACKTRACE
959 struct ring_buffer_event *event;
960 struct stack_entry *entry;
961 struct stack_trace trace;
963 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
964 sizeof(*entry), flags, pc);
967 entry = ring_buffer_event_data(event);
968 memset(&entry->caller, 0, sizeof(entry->caller));
970 trace.nr_entries = 0;
971 trace.max_entries = FTRACE_STACK_ENTRIES;
973 trace.entries = entry->caller;
975 save_stack_trace(&trace);
976 ring_buffer_unlock_commit(tr->buffer, event);
980 static void ftrace_trace_stack(struct trace_array *tr,
984 if (!(trace_flags & TRACE_ITER_STACKTRACE))
987 __ftrace_trace_stack(tr, flags, skip, pc);
990 void __trace_stack(struct trace_array *tr,
994 __ftrace_trace_stack(tr, flags, skip, pc);
997 static void ftrace_trace_userstack(struct trace_array *tr,
998 unsigned long flags, int pc)
1000 #ifdef CONFIG_STACKTRACE
1001 struct ring_buffer_event *event;
1002 struct userstack_entry *entry;
1003 struct stack_trace trace;
1005 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1008 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1009 sizeof(*entry), flags, pc);
1012 entry = ring_buffer_event_data(event);
1014 memset(&entry->caller, 0, sizeof(entry->caller));
1016 trace.nr_entries = 0;
1017 trace.max_entries = FTRACE_STACK_ENTRIES;
1019 trace.entries = entry->caller;
1021 save_stack_trace_user(&trace);
1022 ring_buffer_unlock_commit(tr->buffer, event);
1027 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1029 ftrace_trace_userstack(tr, flags, preempt_count());
1034 ftrace_trace_special(void *__tr,
1035 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1038 struct ring_buffer_event *event;
1039 struct trace_array *tr = __tr;
1040 struct special_entry *entry;
1042 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1043 sizeof(*entry), 0, pc);
1046 entry = ring_buffer_event_data(event);
1050 trace_buffer_unlock_commit(tr, event, 0, pc);
1054 __trace_special(void *__tr, void *__data,
1055 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1057 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1061 tracing_sched_switch_trace(struct trace_array *tr,
1062 struct task_struct *prev,
1063 struct task_struct *next,
1064 unsigned long flags, int pc)
1066 struct ring_buffer_event *event;
1067 struct ctx_switch_entry *entry;
1069 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1070 sizeof(*entry), flags, pc);
1073 entry = ring_buffer_event_data(event);
1074 entry->prev_pid = prev->pid;
1075 entry->prev_prio = prev->prio;
1076 entry->prev_state = prev->state;
1077 entry->next_pid = next->pid;
1078 entry->next_prio = next->prio;
1079 entry->next_state = next->state;
1080 entry->next_cpu = task_cpu(next);
1081 trace_buffer_unlock_commit(tr, event, flags, pc);
1085 tracing_sched_wakeup_trace(struct trace_array *tr,
1086 struct task_struct *wakee,
1087 struct task_struct *curr,
1088 unsigned long flags, int pc)
1090 struct ring_buffer_event *event;
1091 struct ctx_switch_entry *entry;
1093 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1094 sizeof(*entry), flags, pc);
1097 entry = ring_buffer_event_data(event);
1098 entry->prev_pid = curr->pid;
1099 entry->prev_prio = curr->prio;
1100 entry->prev_state = curr->state;
1101 entry->next_pid = wakee->pid;
1102 entry->next_prio = wakee->prio;
1103 entry->next_state = wakee->state;
1104 entry->next_cpu = task_cpu(wakee);
1106 ring_buffer_unlock_commit(tr->buffer, event);
1107 ftrace_trace_stack(tr, flags, 6, pc);
1108 ftrace_trace_userstack(tr, flags, pc);
1112 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1114 struct trace_array *tr = &global_trace;
1115 struct trace_array_cpu *data;
1116 unsigned long flags;
1120 if (tracing_disabled)
1123 pc = preempt_count();
1124 local_irq_save(flags);
1125 cpu = raw_smp_processor_id();
1126 data = tr->data[cpu];
1128 if (likely(atomic_inc_return(&data->disabled) == 1))
1129 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1131 atomic_dec(&data->disabled);
1132 local_irq_restore(flags);
1135 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1136 int trace_graph_entry(struct ftrace_graph_ent *trace)
1138 struct trace_array *tr = &global_trace;
1139 struct trace_array_cpu *data;
1140 unsigned long flags;
1145 if (!ftrace_trace_task(current))
1148 if (!ftrace_graph_addr(trace->func))
1151 local_irq_save(flags);
1152 cpu = raw_smp_processor_id();
1153 data = tr->data[cpu];
1154 disabled = atomic_inc_return(&data->disabled);
1155 if (likely(disabled == 1)) {
1156 pc = preempt_count();
1157 __trace_graph_entry(tr, trace, flags, pc);
1159 /* Only do the atomic if it is not already set */
1160 if (!test_tsk_trace_graph(current))
1161 set_tsk_trace_graph(current);
1162 atomic_dec(&data->disabled);
1163 local_irq_restore(flags);
1168 void trace_graph_return(struct ftrace_graph_ret *trace)
1170 struct trace_array *tr = &global_trace;
1171 struct trace_array_cpu *data;
1172 unsigned long flags;
1177 local_irq_save(flags);
1178 cpu = raw_smp_processor_id();
1179 data = tr->data[cpu];
1180 disabled = atomic_inc_return(&data->disabled);
1181 if (likely(disabled == 1)) {
1182 pc = preempt_count();
1183 __trace_graph_return(tr, trace, flags, pc);
1186 clear_tsk_trace_graph(current);
1187 atomic_dec(&data->disabled);
1188 local_irq_restore(flags);
1190 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1194 * trace_vbprintk - write binary msg to tracing buffer
1197 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1199 static raw_spinlock_t trace_buf_lock =
1200 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1201 static u32 trace_buf[TRACE_BUF_SIZE];
1203 struct ring_buffer_event *event;
1204 struct trace_array *tr = &global_trace;
1205 struct trace_array_cpu *data;
1206 struct bprint_entry *entry;
1207 unsigned long flags;
1209 int cpu, len = 0, size, pc;
1211 if (unlikely(tracing_selftest_running || tracing_disabled))
1214 /* Don't pollute graph traces with trace_vprintk internals */
1215 pause_graph_tracing();
1217 pc = preempt_count();
1218 resched = ftrace_preempt_disable();
1219 cpu = raw_smp_processor_id();
1220 data = tr->data[cpu];
1222 if (unlikely(atomic_read(&data->disabled)))
1225 /* Lockdep uses trace_printk for lock tracing */
1226 local_irq_save(flags);
1227 __raw_spin_lock(&trace_buf_lock);
1228 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1230 if (len > TRACE_BUF_SIZE || len < 0)
1233 size = sizeof(*entry) + sizeof(u32) * len;
1234 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1237 entry = ring_buffer_event_data(event);
1241 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1242 ring_buffer_unlock_commit(tr->buffer, event);
1245 __raw_spin_unlock(&trace_buf_lock);
1246 local_irq_restore(flags);
1249 ftrace_preempt_enable(resched);
1250 unpause_graph_tracing();
1254 EXPORT_SYMBOL_GPL(trace_vbprintk);
1256 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1258 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1259 static char trace_buf[TRACE_BUF_SIZE];
1261 struct ring_buffer_event *event;
1262 struct trace_array *tr = &global_trace;
1263 struct trace_array_cpu *data;
1264 int cpu, len = 0, size, pc;
1265 struct print_entry *entry;
1266 unsigned long irq_flags;
1268 if (tracing_disabled || tracing_selftest_running)
1271 pc = preempt_count();
1272 preempt_disable_notrace();
1273 cpu = raw_smp_processor_id();
1274 data = tr->data[cpu];
1276 if (unlikely(atomic_read(&data->disabled)))
1279 pause_graph_tracing();
1280 raw_local_irq_save(irq_flags);
1281 __raw_spin_lock(&trace_buf_lock);
1282 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1284 len = min(len, TRACE_BUF_SIZE-1);
1287 size = sizeof(*entry) + len + 1;
1288 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1291 entry = ring_buffer_event_data(event);
1294 memcpy(&entry->buf, trace_buf, len);
1295 entry->buf[len] = 0;
1296 ring_buffer_unlock_commit(tr->buffer, event);
1299 __raw_spin_unlock(&trace_buf_lock);
1300 raw_local_irq_restore(irq_flags);
1301 unpause_graph_tracing();
1303 preempt_enable_notrace();
1307 EXPORT_SYMBOL_GPL(trace_vprintk);
1309 enum trace_file_type {
1310 TRACE_FILE_LAT_FMT = 1,
1311 TRACE_FILE_ANNOTATE = 2,
1314 static void trace_iterator_increment(struct trace_iterator *iter)
1316 /* Don't allow ftrace to trace into the ring buffers */
1317 ftrace_disable_cpu();
1320 if (iter->buffer_iter[iter->cpu])
1321 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1323 ftrace_enable_cpu();
1326 static struct trace_entry *
1327 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1329 struct ring_buffer_event *event;
1330 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1332 /* Don't allow ftrace to trace into the ring buffers */
1333 ftrace_disable_cpu();
1336 event = ring_buffer_iter_peek(buf_iter, ts);
1338 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1340 ftrace_enable_cpu();
1342 return event ? ring_buffer_event_data(event) : NULL;
1345 static struct trace_entry *
1346 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1348 struct ring_buffer *buffer = iter->tr->buffer;
1349 struct trace_entry *ent, *next = NULL;
1350 int cpu_file = iter->cpu_file;
1351 u64 next_ts = 0, ts;
1356 * If we are in a per_cpu trace file, don't bother by iterating over
1357 * all cpu and peek directly.
1359 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1360 if (ring_buffer_empty_cpu(buffer, cpu_file))
1362 ent = peek_next_entry(iter, cpu_file, ent_ts);
1364 *ent_cpu = cpu_file;
1369 for_each_tracing_cpu(cpu) {
1371 if (ring_buffer_empty_cpu(buffer, cpu))
1374 ent = peek_next_entry(iter, cpu, &ts);
1377 * Pick the entry with the smallest timestamp:
1379 if (ent && (!next || ts < next_ts)) {
1387 *ent_cpu = next_cpu;
1395 /* Find the next real entry, without updating the iterator itself */
1396 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1397 int *ent_cpu, u64 *ent_ts)
1399 return __find_next_entry(iter, ent_cpu, ent_ts);
1402 /* Find the next real entry, and increment the iterator to the next entry */
1403 static void *find_next_entry_inc(struct trace_iterator *iter)
1405 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1408 trace_iterator_increment(iter);
1410 return iter->ent ? iter : NULL;
1413 static void trace_consume(struct trace_iterator *iter)
1415 /* Don't allow ftrace to trace into the ring buffers */
1416 ftrace_disable_cpu();
1417 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1418 ftrace_enable_cpu();
1421 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1423 struct trace_iterator *iter = m->private;
1429 /* can't go backwards */
1434 ent = find_next_entry_inc(iter);
1438 while (ent && iter->idx < i)
1439 ent = find_next_entry_inc(iter);
1447 * No necessary locking here. The worst thing which can
1448 * happen is loosing events consumed at the same time
1449 * by a trace_pipe reader.
1450 * Other than that, we don't risk to crash the ring buffer
1451 * because it serializes the readers.
1453 * The current tracer is copied to avoid a global locking
1456 static void *s_start(struct seq_file *m, loff_t *pos)
1458 struct trace_iterator *iter = m->private;
1459 static struct tracer *old_tracer;
1460 int cpu_file = iter->cpu_file;
1465 /* copy the tracer to avoid using a global lock all around */
1466 mutex_lock(&trace_types_lock);
1467 if (unlikely(old_tracer != current_trace && current_trace)) {
1468 old_tracer = current_trace;
1469 *iter->trace = *current_trace;
1471 mutex_unlock(&trace_types_lock);
1473 atomic_inc(&trace_record_cmdline_disabled);
1475 if (*pos != iter->pos) {
1480 ftrace_disable_cpu();
1482 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1483 for_each_tracing_cpu(cpu)
1484 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1486 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1489 ftrace_enable_cpu();
1491 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1496 p = s_next(m, p, &l);
1502 static void s_stop(struct seq_file *m, void *p)
1504 atomic_dec(&trace_record_cmdline_disabled);
1507 static void print_lat_help_header(struct seq_file *m)
1509 seq_puts(m, "# _------=> CPU# \n");
1510 seq_puts(m, "# / _-----=> irqs-off \n");
1511 seq_puts(m, "# | / _----=> need-resched \n");
1512 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1513 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1514 seq_puts(m, "# |||| / \n");
1515 seq_puts(m, "# ||||| delay \n");
1516 seq_puts(m, "# cmd pid ||||| time | caller \n");
1517 seq_puts(m, "# \\ / ||||| \\ | / \n");
1520 static void print_func_help_header(struct seq_file *m)
1522 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1523 seq_puts(m, "# | | | | |\n");
1528 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1530 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1531 struct trace_array *tr = iter->tr;
1532 struct trace_array_cpu *data = tr->data[tr->cpu];
1533 struct tracer *type = current_trace;
1534 unsigned long total;
1535 unsigned long entries;
1536 const char *name = "preemption";
1541 entries = ring_buffer_entries(iter->tr->buffer);
1543 ring_buffer_overruns(iter->tr->buffer);
1545 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1547 seq_puts(m, "# -----------------------------------"
1548 "---------------------------------\n");
1549 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1550 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1551 nsecs_to_usecs(data->saved_latency),
1555 #if defined(CONFIG_PREEMPT_NONE)
1557 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1559 #elif defined(CONFIG_PREEMPT)
1564 /* These are reserved for later use */
1567 seq_printf(m, " #P:%d)\n", num_online_cpus());
1571 seq_puts(m, "# -----------------\n");
1572 seq_printf(m, "# | task: %.16s-%d "
1573 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1574 data->comm, data->pid, data->uid, data->nice,
1575 data->policy, data->rt_priority);
1576 seq_puts(m, "# -----------------\n");
1578 if (data->critical_start) {
1579 seq_puts(m, "# => started at: ");
1580 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1581 trace_print_seq(m, &iter->seq);
1582 seq_puts(m, "\n# => ended at: ");
1583 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1584 trace_print_seq(m, &iter->seq);
1591 static void test_cpu_buff_start(struct trace_iterator *iter)
1593 struct trace_seq *s = &iter->seq;
1595 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1598 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1601 if (cpumask_test_cpu(iter->cpu, iter->started))
1604 cpumask_set_cpu(iter->cpu, iter->started);
1605 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1608 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1610 struct trace_seq *s = &iter->seq;
1611 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1612 struct trace_entry *entry;
1613 struct trace_event *event;
1617 test_cpu_buff_start(iter);
1619 event = ftrace_find_event(entry->type);
1621 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1622 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1623 if (!trace_print_lat_context(iter))
1626 if (!trace_print_context(iter))
1632 return event->trace(iter, sym_flags);
1634 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1637 return TRACE_TYPE_HANDLED;
1639 return TRACE_TYPE_PARTIAL_LINE;
1642 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1644 struct trace_seq *s = &iter->seq;
1645 struct trace_entry *entry;
1646 struct trace_event *event;
1650 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1651 if (!trace_seq_printf(s, "%d %d %llu ",
1652 entry->pid, iter->cpu, iter->ts))
1656 event = ftrace_find_event(entry->type);
1658 return event->raw(iter, 0);
1660 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1663 return TRACE_TYPE_HANDLED;
1665 return TRACE_TYPE_PARTIAL_LINE;
1668 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1670 struct trace_seq *s = &iter->seq;
1671 unsigned char newline = '\n';
1672 struct trace_entry *entry;
1673 struct trace_event *event;
1677 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1678 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1679 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1680 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1683 event = ftrace_find_event(entry->type);
1685 enum print_line_t ret = event->hex(iter, 0);
1686 if (ret != TRACE_TYPE_HANDLED)
1690 SEQ_PUT_FIELD_RET(s, newline);
1692 return TRACE_TYPE_HANDLED;
1695 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1697 struct trace_seq *s = &iter->seq;
1698 struct trace_entry *entry;
1699 struct trace_event *event;
1703 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1704 SEQ_PUT_FIELD_RET(s, entry->pid);
1705 SEQ_PUT_FIELD_RET(s, iter->cpu);
1706 SEQ_PUT_FIELD_RET(s, iter->ts);
1709 event = ftrace_find_event(entry->type);
1710 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1713 static int trace_empty(struct trace_iterator *iter)
1717 /* If we are looking at one CPU buffer, only check that one */
1718 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1719 cpu = iter->cpu_file;
1720 if (iter->buffer_iter[cpu]) {
1721 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1724 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1730 for_each_tracing_cpu(cpu) {
1731 if (iter->buffer_iter[cpu]) {
1732 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1735 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1743 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1745 enum print_line_t ret;
1747 if (iter->trace && iter->trace->print_line) {
1748 ret = iter->trace->print_line(iter);
1749 if (ret != TRACE_TYPE_UNHANDLED)
1753 if (iter->ent->type == TRACE_BPRINT &&
1754 trace_flags & TRACE_ITER_PRINTK &&
1755 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1756 return trace_print_bprintk_msg_only(iter);
1758 if (iter->ent->type == TRACE_PRINT &&
1759 trace_flags & TRACE_ITER_PRINTK &&
1760 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1761 return trace_print_printk_msg_only(iter);
1763 if (trace_flags & TRACE_ITER_BIN)
1764 return print_bin_fmt(iter);
1766 if (trace_flags & TRACE_ITER_HEX)
1767 return print_hex_fmt(iter);
1769 if (trace_flags & TRACE_ITER_RAW)
1770 return print_raw_fmt(iter);
1772 return print_trace_fmt(iter);
1775 static int s_show(struct seq_file *m, void *v)
1777 struct trace_iterator *iter = v;
1779 if (iter->ent == NULL) {
1781 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1784 if (iter->trace && iter->trace->print_header)
1785 iter->trace->print_header(m);
1786 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1787 /* print nothing if the buffers are empty */
1788 if (trace_empty(iter))
1790 print_trace_header(m, iter);
1791 if (!(trace_flags & TRACE_ITER_VERBOSE))
1792 print_lat_help_header(m);
1794 if (!(trace_flags & TRACE_ITER_VERBOSE))
1795 print_func_help_header(m);
1798 print_trace_line(iter);
1799 trace_print_seq(m, &iter->seq);
1805 static struct seq_operations tracer_seq_ops = {
1812 static struct trace_iterator *
1813 __tracing_open(struct inode *inode, struct file *file)
1815 long cpu_file = (long) inode->i_private;
1816 void *fail_ret = ERR_PTR(-ENOMEM);
1817 struct trace_iterator *iter;
1821 if (tracing_disabled)
1822 return ERR_PTR(-ENODEV);
1824 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1826 return ERR_PTR(-ENOMEM);
1829 * We make a copy of the current tracer to avoid concurrent
1830 * changes on it while we are reading.
1832 mutex_lock(&trace_types_lock);
1833 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1838 *iter->trace = *current_trace;
1840 if (current_trace && current_trace->print_max)
1843 iter->tr = &global_trace;
1845 mutex_init(&iter->mutex);
1846 iter->cpu_file = cpu_file;
1848 /* Notify the tracer early; before we stop tracing. */
1849 if (iter->trace && iter->trace->open)
1850 iter->trace->open(iter);
1852 /* Annotate start of buffers if we had overruns */
1853 if (ring_buffer_overruns(iter->tr->buffer))
1854 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1856 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1857 for_each_tracing_cpu(cpu) {
1859 iter->buffer_iter[cpu] =
1860 ring_buffer_read_start(iter->tr->buffer, cpu);
1863 cpu = iter->cpu_file;
1864 iter->buffer_iter[cpu] =
1865 ring_buffer_read_start(iter->tr->buffer, cpu);
1868 /* TODO stop tracer */
1869 ret = seq_open(file, &tracer_seq_ops);
1871 fail_ret = ERR_PTR(ret);
1875 m = file->private_data;
1878 /* stop the trace while dumping */
1881 mutex_unlock(&trace_types_lock);
1886 for_each_tracing_cpu(cpu) {
1887 if (iter->buffer_iter[cpu])
1888 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1891 mutex_unlock(&trace_types_lock);
1898 int tracing_open_generic(struct inode *inode, struct file *filp)
1900 if (tracing_disabled)
1903 filp->private_data = inode->i_private;
1907 static int tracing_release(struct inode *inode, struct file *file)
1909 struct seq_file *m = (struct seq_file *)file->private_data;
1910 struct trace_iterator *iter;
1913 if (!(file->f_mode & FMODE_READ))
1918 mutex_lock(&trace_types_lock);
1919 for_each_tracing_cpu(cpu) {
1920 if (iter->buffer_iter[cpu])
1921 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1924 if (iter->trace && iter->trace->close)
1925 iter->trace->close(iter);
1927 /* reenable tracing if it was previously enabled */
1929 mutex_unlock(&trace_types_lock);
1931 seq_release(inode, file);
1932 mutex_destroy(&iter->mutex);
1938 static int tracing_open(struct inode *inode, struct file *file)
1940 struct trace_iterator *iter;
1943 /* If this file was open for write, then erase contents */
1944 if ((file->f_mode & FMODE_WRITE) &&
1945 !(file->f_flags & O_APPEND)) {
1946 long cpu = (long) inode->i_private;
1948 if (cpu == TRACE_PIPE_ALL_CPU)
1949 tracing_reset_online_cpus(&global_trace);
1951 tracing_reset(&global_trace, cpu);
1954 if (file->f_mode & FMODE_READ) {
1955 iter = __tracing_open(inode, file);
1957 ret = PTR_ERR(iter);
1958 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1959 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1965 t_next(struct seq_file *m, void *v, loff_t *pos)
1967 struct tracer *t = m->private;
1979 static void *t_start(struct seq_file *m, loff_t *pos)
1981 struct tracer *t = m->private;
1984 mutex_lock(&trace_types_lock);
1985 for (; t && l < *pos; t = t_next(m, t, &l))
1991 static void t_stop(struct seq_file *m, void *p)
1993 mutex_unlock(&trace_types_lock);
1996 static int t_show(struct seq_file *m, void *v)
1998 struct tracer *t = v;
2003 seq_printf(m, "%s", t->name);
2012 static struct seq_operations show_traces_seq_ops = {
2019 static int show_traces_open(struct inode *inode, struct file *file)
2023 if (tracing_disabled)
2026 ret = seq_open(file, &show_traces_seq_ops);
2028 struct seq_file *m = file->private_data;
2029 m->private = trace_types;
2036 tracing_write_stub(struct file *filp, const char __user *ubuf,
2037 size_t count, loff_t *ppos)
2042 static const struct file_operations tracing_fops = {
2043 .open = tracing_open,
2045 .write = tracing_write_stub,
2046 .llseek = seq_lseek,
2047 .release = tracing_release,
2050 static const struct file_operations show_traces_fops = {
2051 .open = show_traces_open,
2053 .release = seq_release,
2057 * Only trace on a CPU if the bitmask is set:
2059 static cpumask_var_t tracing_cpumask;
2062 * The tracer itself will not take this lock, but still we want
2063 * to provide a consistent cpumask to user-space:
2065 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2068 * Temporary storage for the character representation of the
2069 * CPU bitmask (and one more byte for the newline):
2071 static char mask_str[NR_CPUS + 1];
2074 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2075 size_t count, loff_t *ppos)
2079 mutex_lock(&tracing_cpumask_update_lock);
2081 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2082 if (count - len < 2) {
2086 len += sprintf(mask_str + len, "\n");
2087 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2090 mutex_unlock(&tracing_cpumask_update_lock);
2096 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2097 size_t count, loff_t *ppos)
2100 cpumask_var_t tracing_cpumask_new;
2102 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2105 mutex_lock(&tracing_cpumask_update_lock);
2106 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2110 local_irq_disable();
2111 __raw_spin_lock(&ftrace_max_lock);
2112 for_each_tracing_cpu(cpu) {
2114 * Increase/decrease the disabled counter if we are
2115 * about to flip a bit in the cpumask:
2117 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2118 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2119 atomic_inc(&global_trace.data[cpu]->disabled);
2121 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2122 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2123 atomic_dec(&global_trace.data[cpu]->disabled);
2126 __raw_spin_unlock(&ftrace_max_lock);
2129 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2131 mutex_unlock(&tracing_cpumask_update_lock);
2132 free_cpumask_var(tracing_cpumask_new);
2137 mutex_unlock(&tracing_cpumask_update_lock);
2138 free_cpumask_var(tracing_cpumask);
2143 static const struct file_operations tracing_cpumask_fops = {
2144 .open = tracing_open_generic,
2145 .read = tracing_cpumask_read,
2146 .write = tracing_cpumask_write,
2150 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2151 size_t cnt, loff_t *ppos)
2153 struct tracer_opt *trace_opts;
2161 /* calculate max size */
2162 for (i = 0; trace_options[i]; i++) {
2163 len += strlen(trace_options[i]);
2164 len += 3; /* "no" and newline */
2167 mutex_lock(&trace_types_lock);
2168 tracer_flags = current_trace->flags->val;
2169 trace_opts = current_trace->flags->opts;
2172 * Increase the size with names of options specific
2173 * of the current tracer.
2175 for (i = 0; trace_opts[i].name; i++) {
2176 len += strlen(trace_opts[i].name);
2177 len += 3; /* "no" and newline */
2180 /* +2 for \n and \0 */
2181 buf = kmalloc(len + 2, GFP_KERNEL);
2183 mutex_unlock(&trace_types_lock);
2187 for (i = 0; trace_options[i]; i++) {
2188 if (trace_flags & (1 << i))
2189 r += sprintf(buf + r, "%s\n", trace_options[i]);
2191 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2194 for (i = 0; trace_opts[i].name; i++) {
2195 if (tracer_flags & trace_opts[i].bit)
2196 r += sprintf(buf + r, "%s\n",
2197 trace_opts[i].name);
2199 r += sprintf(buf + r, "no%s\n",
2200 trace_opts[i].name);
2202 mutex_unlock(&trace_types_lock);
2204 WARN_ON(r >= len + 2);
2206 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2212 /* Try to assign a tracer specific option */
2213 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2215 struct tracer_flags *trace_flags = trace->flags;
2216 struct tracer_opt *opts = NULL;
2220 for (i = 0; trace_flags->opts[i].name; i++) {
2221 opts = &trace_flags->opts[i];
2222 len = strlen(opts->name);
2224 if (strncmp(cmp, opts->name, len) == 0) {
2225 ret = trace->set_flag(trace_flags->val,
2231 if (!trace_flags->opts[i].name)
2234 /* Refused to handle */
2239 trace_flags->val &= ~opts->bit;
2241 trace_flags->val |= opts->bit;
2246 static void set_tracer_flags(unsigned int mask, int enabled)
2248 /* do nothing if flag is already set */
2249 if (!!(trace_flags & mask) == !!enabled)
2253 trace_flags |= mask;
2255 trace_flags &= ~mask;
2257 if (mask == TRACE_ITER_GLOBAL_CLK) {
2261 func = trace_clock_global;
2263 func = trace_clock_local;
2265 mutex_lock(&trace_types_lock);
2266 ring_buffer_set_clock(global_trace.buffer, func);
2269 ring_buffer_set_clock(max_tr.buffer, func);
2270 mutex_unlock(&trace_types_lock);
2275 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2276 size_t cnt, loff_t *ppos)
2284 if (cnt >= sizeof(buf))
2287 if (copy_from_user(&buf, ubuf, cnt))
2292 if (strncmp(buf, "no", 2) == 0) {
2297 for (i = 0; trace_options[i]; i++) {
2298 int len = strlen(trace_options[i]);
2300 if (strncmp(cmp, trace_options[i], len) == 0) {
2301 set_tracer_flags(1 << i, !neg);
2306 /* If no option could be set, test the specific tracer options */
2307 if (!trace_options[i]) {
2308 mutex_lock(&trace_types_lock);
2309 ret = set_tracer_option(current_trace, cmp, neg);
2310 mutex_unlock(&trace_types_lock);
2320 static const struct file_operations tracing_iter_fops = {
2321 .open = tracing_open_generic,
2322 .read = tracing_trace_options_read,
2323 .write = tracing_trace_options_write,
2326 static const char readme_msg[] =
2327 "tracing mini-HOWTO:\n\n"
2329 "# mount -t debugfs nodev /debug\n\n"
2330 "# cat /debug/tracing/available_tracers\n"
2331 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2332 "# cat /debug/tracing/current_tracer\n"
2334 "# echo sched_switch > /debug/tracing/current_tracer\n"
2335 "# cat /debug/tracing/current_tracer\n"
2337 "# cat /debug/tracing/trace_options\n"
2338 "noprint-parent nosym-offset nosym-addr noverbose\n"
2339 "# echo print-parent > /debug/tracing/trace_options\n"
2340 "# echo 1 > /debug/tracing/tracing_enabled\n"
2341 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2342 "echo 0 > /debug/tracing/tracing_enabled\n"
2346 tracing_readme_read(struct file *filp, char __user *ubuf,
2347 size_t cnt, loff_t *ppos)
2349 return simple_read_from_buffer(ubuf, cnt, ppos,
2350 readme_msg, strlen(readme_msg));
2353 static const struct file_operations tracing_readme_fops = {
2354 .open = tracing_open_generic,
2355 .read = tracing_readme_read,
2359 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2360 size_t cnt, loff_t *ppos)
2365 r = sprintf(buf, "%u\n", tracer_enabled);
2366 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2370 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2371 size_t cnt, loff_t *ppos)
2373 struct trace_array *tr = filp->private_data;
2378 if (cnt >= sizeof(buf))
2381 if (copy_from_user(&buf, ubuf, cnt))
2386 ret = strict_strtoul(buf, 10, &val);
2392 mutex_lock(&trace_types_lock);
2393 if (tracer_enabled ^ val) {
2396 if (current_trace->start)
2397 current_trace->start(tr);
2402 if (current_trace->stop)
2403 current_trace->stop(tr);
2406 mutex_unlock(&trace_types_lock);
2414 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2415 size_t cnt, loff_t *ppos)
2417 char buf[max_tracer_type_len+2];
2420 mutex_lock(&trace_types_lock);
2422 r = sprintf(buf, "%s\n", current_trace->name);
2424 r = sprintf(buf, "\n");
2425 mutex_unlock(&trace_types_lock);
2427 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2430 int tracer_init(struct tracer *t, struct trace_array *tr)
2432 tracing_reset_online_cpus(tr);
2436 static int tracing_resize_ring_buffer(unsigned long size)
2441 * If kernel or user changes the size of the ring buffer
2442 * we use the size that was given, and we can forget about
2443 * expanding it later.
2445 ring_buffer_expanded = 1;
2447 ret = ring_buffer_resize(global_trace.buffer, size);
2451 ret = ring_buffer_resize(max_tr.buffer, size);
2455 r = ring_buffer_resize(global_trace.buffer,
2456 global_trace.entries);
2459 * AARGH! We are left with different
2460 * size max buffer!!!!
2461 * The max buffer is our "snapshot" buffer.
2462 * When a tracer needs a snapshot (one of the
2463 * latency tracers), it swaps the max buffer
2464 * with the saved snap shot. We succeeded to
2465 * update the size of the main buffer, but failed to
2466 * update the size of the max buffer. But when we tried
2467 * to reset the main buffer to the original size, we
2468 * failed there too. This is very unlikely to
2469 * happen, but if it does, warn and kill all
2473 tracing_disabled = 1;
2478 global_trace.entries = size;
2484 * tracing_update_buffers - used by tracing facility to expand ring buffers
2486 * To save on memory when the tracing is never used on a system with it
2487 * configured in. The ring buffers are set to a minimum size. But once
2488 * a user starts to use the tracing facility, then they need to grow
2489 * to their default size.
2491 * This function is to be called when a tracer is about to be used.
2493 int tracing_update_buffers(void)
2497 mutex_lock(&trace_types_lock);
2498 if (!ring_buffer_expanded)
2499 ret = tracing_resize_ring_buffer(trace_buf_size);
2500 mutex_unlock(&trace_types_lock);
2505 struct trace_option_dentry;
2507 static struct trace_option_dentry *
2508 create_trace_option_files(struct tracer *tracer);
2511 destroy_trace_option_files(struct trace_option_dentry *topts);
2513 static int tracing_set_tracer(const char *buf)
2515 static struct trace_option_dentry *topts;
2516 struct trace_array *tr = &global_trace;
2520 mutex_lock(&trace_types_lock);
2522 if (!ring_buffer_expanded) {
2523 ret = tracing_resize_ring_buffer(trace_buf_size);
2529 for (t = trace_types; t; t = t->next) {
2530 if (strcmp(t->name, buf) == 0)
2537 if (t == current_trace)
2540 trace_branch_disable();
2541 if (current_trace && current_trace->reset)
2542 current_trace->reset(tr);
2544 destroy_trace_option_files(topts);
2548 topts = create_trace_option_files(current_trace);
2551 ret = tracer_init(t, tr);
2556 trace_branch_enable(tr);
2558 mutex_unlock(&trace_types_lock);
2564 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2565 size_t cnt, loff_t *ppos)
2567 char buf[max_tracer_type_len+1];
2574 if (cnt > max_tracer_type_len)
2575 cnt = max_tracer_type_len;
2577 if (copy_from_user(&buf, ubuf, cnt))
2582 /* strip ending whitespace. */
2583 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2586 err = tracing_set_tracer(buf);
2596 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2597 size_t cnt, loff_t *ppos)
2599 unsigned long *ptr = filp->private_data;
2603 r = snprintf(buf, sizeof(buf), "%ld\n",
2604 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2605 if (r > sizeof(buf))
2607 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2611 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2612 size_t cnt, loff_t *ppos)
2614 unsigned long *ptr = filp->private_data;
2619 if (cnt >= sizeof(buf))
2622 if (copy_from_user(&buf, ubuf, cnt))
2627 ret = strict_strtoul(buf, 10, &val);
2636 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2638 long cpu_file = (long) inode->i_private;
2639 struct trace_iterator *iter;
2642 if (tracing_disabled)
2645 mutex_lock(&trace_types_lock);
2647 /* We only allow one reader per cpu */
2648 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2649 if (!cpumask_empty(tracing_reader_cpumask)) {
2653 cpumask_setall(tracing_reader_cpumask);
2655 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2656 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2663 /* create a buffer to store the information to pass to userspace */
2664 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2671 * We make a copy of the current tracer to avoid concurrent
2672 * changes on it while we are reading.
2674 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2680 *iter->trace = *current_trace;
2682 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2687 /* trace pipe does not show start of buffer */
2688 cpumask_setall(iter->started);
2690 iter->cpu_file = cpu_file;
2691 iter->tr = &global_trace;
2692 mutex_init(&iter->mutex);
2693 filp->private_data = iter;
2695 if (iter->trace->pipe_open)
2696 iter->trace->pipe_open(iter);
2699 mutex_unlock(&trace_types_lock);
2705 mutex_unlock(&trace_types_lock);
2709 static int tracing_release_pipe(struct inode *inode, struct file *file)
2711 struct trace_iterator *iter = file->private_data;
2713 mutex_lock(&trace_types_lock);
2715 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2716 cpumask_clear(tracing_reader_cpumask);
2718 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2720 mutex_unlock(&trace_types_lock);
2722 free_cpumask_var(iter->started);
2723 mutex_destroy(&iter->mutex);
2731 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2733 struct trace_iterator *iter = filp->private_data;
2735 if (trace_flags & TRACE_ITER_BLOCK) {
2737 * Always select as readable when in blocking mode
2739 return POLLIN | POLLRDNORM;
2741 if (!trace_empty(iter))
2742 return POLLIN | POLLRDNORM;
2743 poll_wait(filp, &trace_wait, poll_table);
2744 if (!trace_empty(iter))
2745 return POLLIN | POLLRDNORM;
2752 void default_wait_pipe(struct trace_iterator *iter)
2756 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2758 if (trace_empty(iter))
2761 finish_wait(&trace_wait, &wait);
2765 * This is a make-shift waitqueue.
2766 * A tracer might use this callback on some rare cases:
2768 * 1) the current tracer might hold the runqueue lock when it wakes up
2769 * a reader, hence a deadlock (sched, function, and function graph tracers)
2770 * 2) the function tracers, trace all functions, we don't want
2771 * the overhead of calling wake_up and friends
2772 * (and tracing them too)
2774 * Anyway, this is really very primitive wakeup.
2776 void poll_wait_pipe(struct trace_iterator *iter)
2778 set_current_state(TASK_INTERRUPTIBLE);
2779 /* sleep for 100 msecs, and try again. */
2780 schedule_timeout(HZ / 10);
2783 /* Must be called with trace_types_lock mutex held. */
2784 static int tracing_wait_pipe(struct file *filp)
2786 struct trace_iterator *iter = filp->private_data;
2788 while (trace_empty(iter)) {
2790 if ((filp->f_flags & O_NONBLOCK)) {
2794 mutex_unlock(&iter->mutex);
2796 iter->trace->wait_pipe(iter);
2798 mutex_lock(&iter->mutex);
2800 if (signal_pending(current))
2804 * We block until we read something and tracing is disabled.
2805 * We still block if tracing is disabled, but we have never
2806 * read anything. This allows a user to cat this file, and
2807 * then enable tracing. But after we have read something,
2808 * we give an EOF when tracing is again disabled.
2810 * iter->pos will be 0 if we haven't read anything.
2812 if (!tracer_enabled && iter->pos)
2823 tracing_read_pipe(struct file *filp, char __user *ubuf,
2824 size_t cnt, loff_t *ppos)
2826 struct trace_iterator *iter = filp->private_data;
2827 static struct tracer *old_tracer;
2830 /* return any leftover data */
2831 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2835 trace_seq_init(&iter->seq);
2837 /* copy the tracer to avoid using a global lock all around */
2838 mutex_lock(&trace_types_lock);
2839 if (unlikely(old_tracer != current_trace && current_trace)) {
2840 old_tracer = current_trace;
2841 *iter->trace = *current_trace;
2843 mutex_unlock(&trace_types_lock);
2846 * Avoid more than one consumer on a single file descriptor
2847 * This is just a matter of traces coherency, the ring buffer itself
2850 mutex_lock(&iter->mutex);
2851 if (iter->trace->read) {
2852 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2858 sret = tracing_wait_pipe(filp);
2862 /* stop when tracing is finished */
2863 if (trace_empty(iter)) {
2868 if (cnt >= PAGE_SIZE)
2869 cnt = PAGE_SIZE - 1;
2871 /* reset all but tr, trace, and overruns */
2872 memset(&iter->seq, 0,
2873 sizeof(struct trace_iterator) -
2874 offsetof(struct trace_iterator, seq));
2877 while (find_next_entry_inc(iter) != NULL) {
2878 enum print_line_t ret;
2879 int len = iter->seq.len;
2881 ret = print_trace_line(iter);
2882 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2883 /* don't print partial lines */
2884 iter->seq.len = len;
2887 if (ret != TRACE_TYPE_NO_CONSUME)
2888 trace_consume(iter);
2890 if (iter->seq.len >= cnt)
2894 /* Now copy what we have to the user */
2895 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2896 if (iter->seq.readpos >= iter->seq.len)
2897 trace_seq_init(&iter->seq);
2900 * If there was nothing to send to user, inspite of consuming trace
2901 * entries, go back to wait for more entries.
2907 mutex_unlock(&iter->mutex);
2912 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2913 struct pipe_buffer *buf)
2915 __free_page(buf->page);
2918 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2921 __free_page(spd->pages[idx]);
2924 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2926 .map = generic_pipe_buf_map,
2927 .unmap = generic_pipe_buf_unmap,
2928 .confirm = generic_pipe_buf_confirm,
2929 .release = tracing_pipe_buf_release,
2930 .steal = generic_pipe_buf_steal,
2931 .get = generic_pipe_buf_get,
2935 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2940 /* Seq buffer is page-sized, exactly what we need. */
2942 count = iter->seq.len;
2943 ret = print_trace_line(iter);
2944 count = iter->seq.len - count;
2947 iter->seq.len -= count;
2950 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2951 iter->seq.len -= count;
2955 trace_consume(iter);
2957 if (!find_next_entry_inc(iter)) {
2967 static ssize_t tracing_splice_read_pipe(struct file *filp,
2969 struct pipe_inode_info *pipe,
2973 struct page *pages[PIPE_BUFFERS];
2974 struct partial_page partial[PIPE_BUFFERS];
2975 struct trace_iterator *iter = filp->private_data;
2976 struct splice_pipe_desc spd = {
2979 .nr_pages = 0, /* This gets updated below. */
2981 .ops = &tracing_pipe_buf_ops,
2982 .spd_release = tracing_spd_release_pipe,
2984 static struct tracer *old_tracer;
2989 /* copy the tracer to avoid using a global lock all around */
2990 mutex_lock(&trace_types_lock);
2991 if (unlikely(old_tracer != current_trace && current_trace)) {
2992 old_tracer = current_trace;
2993 *iter->trace = *current_trace;
2995 mutex_unlock(&trace_types_lock);
2997 mutex_lock(&iter->mutex);
2999 if (iter->trace->splice_read) {
3000 ret = iter->trace->splice_read(iter, filp,
3001 ppos, pipe, len, flags);
3006 ret = tracing_wait_pipe(filp);
3010 if (!iter->ent && !find_next_entry_inc(iter)) {
3015 /* Fill as many pages as possible. */
3016 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3017 pages[i] = alloc_page(GFP_KERNEL);
3021 rem = tracing_fill_pipe_page(rem, iter);
3023 /* Copy the data into the page, so we can start over. */
3024 ret = trace_seq_to_buffer(&iter->seq,
3025 page_address(pages[i]),
3028 __free_page(pages[i]);
3031 partial[i].offset = 0;
3032 partial[i].len = iter->seq.len;
3034 trace_seq_init(&iter->seq);
3037 mutex_unlock(&iter->mutex);
3041 return splice_to_pipe(pipe, &spd);
3044 mutex_unlock(&iter->mutex);
3050 tracing_entries_read(struct file *filp, char __user *ubuf,
3051 size_t cnt, loff_t *ppos)
3053 struct trace_array *tr = filp->private_data;
3057 mutex_lock(&trace_types_lock);
3058 if (!ring_buffer_expanded)
3059 r = sprintf(buf, "%lu (expanded: %lu)\n",
3061 trace_buf_size >> 10);
3063 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3064 mutex_unlock(&trace_types_lock);
3066 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3070 tracing_entries_write(struct file *filp, const char __user *ubuf,
3071 size_t cnt, loff_t *ppos)
3077 if (cnt >= sizeof(buf))
3080 if (copy_from_user(&buf, ubuf, cnt))
3085 ret = strict_strtoul(buf, 10, &val);
3089 /* must have at least 1 entry */
3093 mutex_lock(&trace_types_lock);
3097 /* disable all cpu buffers */
3098 for_each_tracing_cpu(cpu) {
3099 if (global_trace.data[cpu])
3100 atomic_inc(&global_trace.data[cpu]->disabled);
3101 if (max_tr.data[cpu])
3102 atomic_inc(&max_tr.data[cpu]->disabled);
3105 /* value is in KB */
3108 if (val != global_trace.entries) {
3109 ret = tracing_resize_ring_buffer(val);
3118 /* If check pages failed, return ENOMEM */
3119 if (tracing_disabled)
3122 for_each_tracing_cpu(cpu) {
3123 if (global_trace.data[cpu])
3124 atomic_dec(&global_trace.data[cpu]->disabled);
3125 if (max_tr.data[cpu])
3126 atomic_dec(&max_tr.data[cpu]->disabled);
3130 max_tr.entries = global_trace.entries;
3131 mutex_unlock(&trace_types_lock);
3136 static int mark_printk(const char *fmt, ...)
3140 va_start(args, fmt);
3141 ret = trace_vprintk(0, fmt, args);
3147 tracing_mark_write(struct file *filp, const char __user *ubuf,
3148 size_t cnt, loff_t *fpos)
3153 if (tracing_disabled)
3156 if (cnt > TRACE_BUF_SIZE)
3157 cnt = TRACE_BUF_SIZE;
3159 buf = kmalloc(cnt + 1, GFP_KERNEL);
3163 if (copy_from_user(buf, ubuf, cnt)) {
3168 /* Cut from the first nil or newline. */
3170 end = strchr(buf, '\n');
3174 cnt = mark_printk("%s\n", buf);
3181 static const struct file_operations tracing_max_lat_fops = {
3182 .open = tracing_open_generic,
3183 .read = tracing_max_lat_read,
3184 .write = tracing_max_lat_write,
3187 static const struct file_operations tracing_ctrl_fops = {
3188 .open = tracing_open_generic,
3189 .read = tracing_ctrl_read,
3190 .write = tracing_ctrl_write,
3193 static const struct file_operations set_tracer_fops = {
3194 .open = tracing_open_generic,
3195 .read = tracing_set_trace_read,
3196 .write = tracing_set_trace_write,
3199 static const struct file_operations tracing_pipe_fops = {
3200 .open = tracing_open_pipe,
3201 .poll = tracing_poll_pipe,
3202 .read = tracing_read_pipe,
3203 .splice_read = tracing_splice_read_pipe,
3204 .release = tracing_release_pipe,
3207 static const struct file_operations tracing_entries_fops = {
3208 .open = tracing_open_generic,
3209 .read = tracing_entries_read,
3210 .write = tracing_entries_write,
3213 static const struct file_operations tracing_mark_fops = {
3214 .open = tracing_open_generic,
3215 .write = tracing_mark_write,
3218 struct ftrace_buffer_info {
3219 struct trace_array *tr;
3225 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3227 int cpu = (int)(long)inode->i_private;
3228 struct ftrace_buffer_info *info;
3230 if (tracing_disabled)
3233 info = kzalloc(sizeof(*info), GFP_KERNEL);
3237 info->tr = &global_trace;
3239 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3240 /* Force reading ring buffer for first read */
3241 info->read = (unsigned int)-1;
3245 filp->private_data = info;
3255 tracing_buffers_read(struct file *filp, char __user *ubuf,
3256 size_t count, loff_t *ppos)
3258 struct ftrace_buffer_info *info = filp->private_data;
3266 /* Do we have previous read data to read? */
3267 if (info->read < PAGE_SIZE)
3272 ret = ring_buffer_read_page(info->tr->buffer,
3279 pos = ring_buffer_page_len(info->spare);
3281 if (pos < PAGE_SIZE)
3282 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3285 size = PAGE_SIZE - info->read;
3289 ret = copy_to_user(ubuf, info->spare + info->read, size);
3300 static int tracing_buffers_release(struct inode *inode, struct file *file)
3302 struct ftrace_buffer_info *info = file->private_data;
3304 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3311 struct ring_buffer *buffer;
3316 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3317 struct pipe_buffer *buf)
3319 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3324 ring_buffer_free_read_page(ref->buffer, ref->page);
3329 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3330 struct pipe_buffer *buf)
3335 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3336 struct pipe_buffer *buf)
3338 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3343 /* Pipe buffer operations for a buffer. */
3344 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3346 .map = generic_pipe_buf_map,
3347 .unmap = generic_pipe_buf_unmap,
3348 .confirm = generic_pipe_buf_confirm,
3349 .release = buffer_pipe_buf_release,
3350 .steal = buffer_pipe_buf_steal,
3351 .get = buffer_pipe_buf_get,
3355 * Callback from splice_to_pipe(), if we need to release some pages
3356 * at the end of the spd in case we error'ed out in filling the pipe.
3358 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3360 struct buffer_ref *ref =
3361 (struct buffer_ref *)spd->partial[i].private;
3366 ring_buffer_free_read_page(ref->buffer, ref->page);
3368 spd->partial[i].private = 0;
3372 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3373 struct pipe_inode_info *pipe, size_t len,
3376 struct ftrace_buffer_info *info = file->private_data;
3377 struct partial_page partial[PIPE_BUFFERS];
3378 struct page *pages[PIPE_BUFFERS];
3379 struct splice_pipe_desc spd = {
3383 .ops = &buffer_pipe_buf_ops,
3384 .spd_release = buffer_spd_release,
3386 struct buffer_ref *ref;
3391 * We can't seek on a buffer input
3393 if (unlikely(*ppos))
3397 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3401 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3405 ref->buffer = info->tr->buffer;
3406 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3412 r = ring_buffer_read_page(ref->buffer, &ref->page,
3415 ring_buffer_free_read_page(ref->buffer,
3422 * zero out any left over data, this is going to
3425 size = ring_buffer_page_len(ref->page);
3426 if (size < PAGE_SIZE)
3427 memset(ref->page + size, 0, PAGE_SIZE - size);
3429 page = virt_to_page(ref->page);
3431 spd.pages[i] = page;
3432 spd.partial[i].len = PAGE_SIZE;
3433 spd.partial[i].offset = 0;
3434 spd.partial[i].private = (unsigned long)ref;
3440 /* did we read anything? */
3441 if (!spd.nr_pages) {
3442 if (flags & SPLICE_F_NONBLOCK)
3450 ret = splice_to_pipe(pipe, &spd);
3455 static const struct file_operations tracing_buffers_fops = {
3456 .open = tracing_buffers_open,
3457 .read = tracing_buffers_read,
3458 .release = tracing_buffers_release,
3459 .splice_read = tracing_buffers_splice_read,
3460 .llseek = no_llseek,
3463 #ifdef CONFIG_DYNAMIC_FTRACE
3465 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3471 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3472 size_t cnt, loff_t *ppos)
3474 static char ftrace_dyn_info_buffer[1024];
3475 static DEFINE_MUTEX(dyn_info_mutex);
3476 unsigned long *p = filp->private_data;
3477 char *buf = ftrace_dyn_info_buffer;
3478 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3481 mutex_lock(&dyn_info_mutex);
3482 r = sprintf(buf, "%ld ", *p);
3484 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3487 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3489 mutex_unlock(&dyn_info_mutex);
3494 static const struct file_operations tracing_dyn_info_fops = {
3495 .open = tracing_open_generic,
3496 .read = tracing_read_dyn_info,
3500 static struct dentry *d_tracer;
3502 struct dentry *tracing_init_dentry(void)
3509 d_tracer = debugfs_create_dir("tracing", NULL);
3511 if (!d_tracer && !once) {
3513 pr_warning("Could not create debugfs directory 'tracing'\n");
3520 static struct dentry *d_percpu;
3522 struct dentry *tracing_dentry_percpu(void)
3525 struct dentry *d_tracer;
3530 d_tracer = tracing_init_dentry();
3535 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3537 if (!d_percpu && !once) {
3539 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3546 static void tracing_init_debugfs_percpu(long cpu)
3548 struct dentry *d_percpu = tracing_dentry_percpu();
3549 struct dentry *entry, *d_cpu;
3550 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3553 if (cpu > 999 || cpu < 0)
3556 sprintf(cpu_dir, "cpu%ld", cpu);
3557 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3559 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3563 /* per cpu trace_pipe */
3564 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
3565 (void *) cpu, &tracing_pipe_fops);
3567 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3570 entry = debugfs_create_file("trace", 0644, d_cpu,
3571 (void *) cpu, &tracing_fops);
3573 pr_warning("Could not create debugfs 'trace' entry\n");
3575 entry = debugfs_create_file("trace_pipe_raw", 0444, d_cpu,
3576 (void *) cpu, &tracing_buffers_fops);
3578 pr_warning("Could not create debugfs 'trace_pipe_raw' entry\n");
3581 #ifdef CONFIG_FTRACE_SELFTEST
3582 /* Let selftest have access to static functions in this file */
3583 #include "trace_selftest.c"
3586 struct trace_option_dentry {
3587 struct tracer_opt *opt;
3588 struct tracer_flags *flags;
3589 struct dentry *entry;
3593 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3596 struct trace_option_dentry *topt = filp->private_data;
3599 if (topt->flags->val & topt->opt->bit)
3604 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3608 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3611 struct trace_option_dentry *topt = filp->private_data;
3616 if (cnt >= sizeof(buf))
3619 if (copy_from_user(&buf, ubuf, cnt))
3624 ret = strict_strtoul(buf, 10, &val);
3631 /* do nothing if already cleared */
3632 if (!(topt->flags->val & topt->opt->bit))
3635 mutex_lock(&trace_types_lock);
3636 if (current_trace->set_flag)
3637 ret = current_trace->set_flag(topt->flags->val,
3639 mutex_unlock(&trace_types_lock);
3642 topt->flags->val &= ~topt->opt->bit;
3645 /* do nothing if already set */
3646 if (topt->flags->val & topt->opt->bit)
3649 mutex_lock(&trace_types_lock);
3650 if (current_trace->set_flag)
3651 ret = current_trace->set_flag(topt->flags->val,
3653 mutex_unlock(&trace_types_lock);
3656 topt->flags->val |= topt->opt->bit;
3669 static const struct file_operations trace_options_fops = {
3670 .open = tracing_open_generic,
3671 .read = trace_options_read,
3672 .write = trace_options_write,
3676 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3679 long index = (long)filp->private_data;
3682 if (trace_flags & (1 << index))
3687 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3691 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3694 long index = (long)filp->private_data;
3699 if (cnt >= sizeof(buf))
3702 if (copy_from_user(&buf, ubuf, cnt))
3707 ret = strict_strtoul(buf, 10, &val);
3713 trace_flags &= ~(1 << index);
3716 trace_flags |= 1 << index;
3728 static const struct file_operations trace_options_core_fops = {
3729 .open = tracing_open_generic,
3730 .read = trace_options_core_read,
3731 .write = trace_options_core_write,
3734 static struct dentry *trace_options_init_dentry(void)
3736 struct dentry *d_tracer;
3737 static struct dentry *t_options;
3742 d_tracer = tracing_init_dentry();
3746 t_options = debugfs_create_dir("options", d_tracer);
3748 pr_warning("Could not create debugfs directory 'options'\n");
3756 create_trace_option_file(struct trace_option_dentry *topt,
3757 struct tracer_flags *flags,
3758 struct tracer_opt *opt)
3760 struct dentry *t_options;
3761 struct dentry *entry;
3763 t_options = trace_options_init_dentry();
3767 topt->flags = flags;
3770 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3771 &trace_options_fops);
3773 topt->entry = entry;
3777 static struct trace_option_dentry *
3778 create_trace_option_files(struct tracer *tracer)
3780 struct trace_option_dentry *topts;
3781 struct tracer_flags *flags;
3782 struct tracer_opt *opts;
3788 flags = tracer->flags;
3790 if (!flags || !flags->opts)
3795 for (cnt = 0; opts[cnt].name; cnt++)
3798 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3802 for (cnt = 0; opts[cnt].name; cnt++)
3803 create_trace_option_file(&topts[cnt], flags,
3810 destroy_trace_option_files(struct trace_option_dentry *topts)
3817 for (cnt = 0; topts[cnt].opt; cnt++) {
3818 if (topts[cnt].entry)
3819 debugfs_remove(topts[cnt].entry);
3825 static struct dentry *
3826 create_trace_option_core_file(const char *option, long index)
3828 struct dentry *t_options;
3829 struct dentry *entry;
3831 t_options = trace_options_init_dentry();
3835 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3836 &trace_options_core_fops);
3841 static __init void create_trace_options_dir(void)
3843 struct dentry *t_options;
3844 struct dentry *entry;
3847 t_options = trace_options_init_dentry();
3851 for (i = 0; trace_options[i]; i++) {
3852 entry = create_trace_option_core_file(trace_options[i], i);
3854 pr_warning("Could not create debugfs %s entry\n",
3859 static __init int tracer_init_debugfs(void)
3861 struct dentry *d_tracer;
3862 struct dentry *entry;
3865 d_tracer = tracing_init_dentry();
3867 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3868 &global_trace, &tracing_ctrl_fops);
3870 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3872 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3873 NULL, &tracing_iter_fops);
3875 pr_warning("Could not create debugfs 'trace_options' entry\n");
3877 create_trace_options_dir();
3879 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3880 NULL, &tracing_cpumask_fops);
3882 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3884 entry = debugfs_create_file("trace", 0644, d_tracer,
3885 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3887 pr_warning("Could not create debugfs 'trace' entry\n");
3889 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3890 &global_trace, &show_traces_fops);
3892 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3894 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3895 &global_trace, &set_tracer_fops);
3897 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3899 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3900 &tracing_max_latency,
3901 &tracing_max_lat_fops);
3903 pr_warning("Could not create debugfs "
3904 "'tracing_max_latency' entry\n");
3906 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3907 &tracing_thresh, &tracing_max_lat_fops);
3909 pr_warning("Could not create debugfs "
3910 "'tracing_thresh' entry\n");
3911 entry = debugfs_create_file("README", 0644, d_tracer,
3912 NULL, &tracing_readme_fops);
3914 pr_warning("Could not create debugfs 'README' entry\n");
3916 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3917 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3919 pr_warning("Could not create debugfs "
3920 "'trace_pipe' entry\n");
3922 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3923 &global_trace, &tracing_entries_fops);
3925 pr_warning("Could not create debugfs "
3926 "'buffer_size_kb' entry\n");
3928 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3929 NULL, &tracing_mark_fops);
3931 pr_warning("Could not create debugfs "
3932 "'trace_marker' entry\n");
3934 #ifdef CONFIG_DYNAMIC_FTRACE
3935 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3936 &ftrace_update_tot_cnt,
3937 &tracing_dyn_info_fops);
3939 pr_warning("Could not create debugfs "
3940 "'dyn_ftrace_total_info' entry\n");
3942 #ifdef CONFIG_SYSPROF_TRACER
3943 init_tracer_sysprof_debugfs(d_tracer);
3946 for_each_tracing_cpu(cpu)
3947 tracing_init_debugfs_percpu(cpu);
3952 static int trace_panic_handler(struct notifier_block *this,
3953 unsigned long event, void *unused)
3955 if (ftrace_dump_on_oops)
3960 static struct notifier_block trace_panic_notifier = {
3961 .notifier_call = trace_panic_handler,
3963 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3966 static int trace_die_handler(struct notifier_block *self,
3972 if (ftrace_dump_on_oops)
3981 static struct notifier_block trace_die_notifier = {
3982 .notifier_call = trace_die_handler,
3987 * printk is set to max of 1024, we really don't need it that big.
3988 * Nothing should be printing 1000 characters anyway.
3990 #define TRACE_MAX_PRINT 1000
3993 * Define here KERN_TRACE so that we have one place to modify
3994 * it if we decide to change what log level the ftrace dump
3997 #define KERN_TRACE KERN_EMERG
4000 trace_printk_seq(struct trace_seq *s)
4002 /* Probably should print a warning here. */
4006 /* should be zero ended, but we are paranoid. */
4007 s->buffer[s->len] = 0;
4009 printk(KERN_TRACE "%s", s->buffer);
4014 void ftrace_dump(void)
4016 static DEFINE_SPINLOCK(ftrace_dump_lock);
4017 /* use static because iter can be a bit big for the stack */
4018 static struct trace_iterator iter;
4019 static int dump_ran;
4020 unsigned long flags;
4024 spin_lock_irqsave(&ftrace_dump_lock, flags);
4030 /* No turning back! */
4034 for_each_tracing_cpu(cpu) {
4035 atomic_inc(&global_trace.data[cpu]->disabled);
4038 /* don't look at user memory in panic mode */
4039 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4041 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4043 /* Simulate the iterator */
4044 iter.tr = &global_trace;
4045 iter.trace = current_trace;
4046 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4049 * We need to stop all tracing on all CPUS to read the
4050 * the next buffer. This is a bit expensive, but is
4051 * not done often. We fill all what we can read,
4052 * and then release the locks again.
4055 while (!trace_empty(&iter)) {
4058 printk(KERN_TRACE "---------------------------------\n");
4062 /* reset all but tr, trace, and overruns */
4063 memset(&iter.seq, 0,
4064 sizeof(struct trace_iterator) -
4065 offsetof(struct trace_iterator, seq));
4066 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4069 if (find_next_entry_inc(&iter) != NULL) {
4070 print_trace_line(&iter);
4071 trace_consume(&iter);
4074 trace_printk_seq(&iter.seq);
4078 printk(KERN_TRACE " (ftrace buffer empty)\n");
4080 printk(KERN_TRACE "---------------------------------\n");
4083 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
4086 __init static int tracer_alloc_buffers(void)
4088 struct trace_array_cpu *data;
4093 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4096 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4097 goto out_free_buffer_mask;
4099 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4100 goto out_free_tracing_cpumask;
4102 /* To save memory, keep the ring buffer size to its minimum */
4103 if (ring_buffer_expanded)
4104 ring_buf_size = trace_buf_size;
4108 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4109 cpumask_copy(tracing_cpumask, cpu_all_mask);
4110 cpumask_clear(tracing_reader_cpumask);
4112 /* TODO: make the number of buffers hot pluggable with CPUS */
4113 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4114 TRACE_BUFFER_FLAGS);
4115 if (!global_trace.buffer) {
4116 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4118 goto out_free_cpumask;
4120 global_trace.entries = ring_buffer_size(global_trace.buffer);
4123 #ifdef CONFIG_TRACER_MAX_TRACE
4124 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4125 TRACE_BUFFER_FLAGS);
4126 if (!max_tr.buffer) {
4127 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4129 ring_buffer_free(global_trace.buffer);
4130 goto out_free_cpumask;
4132 max_tr.entries = ring_buffer_size(max_tr.buffer);
4133 WARN_ON(max_tr.entries != global_trace.entries);
4136 /* Allocate the first page for all buffers */
4137 for_each_tracing_cpu(i) {
4138 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4139 max_tr.data[i] = &per_cpu(max_data, i);
4142 trace_init_cmdlines();
4144 register_tracer(&nop_trace);
4145 current_trace = &nop_trace;
4146 #ifdef CONFIG_BOOT_TRACER
4147 register_tracer(&boot_tracer);
4149 /* All seems OK, enable tracing */
4150 tracing_disabled = 0;
4152 atomic_notifier_chain_register(&panic_notifier_list,
4153 &trace_panic_notifier);
4155 register_die_notifier(&trace_die_notifier);
4160 free_cpumask_var(tracing_reader_cpumask);
4161 out_free_tracing_cpumask:
4162 free_cpumask_var(tracing_cpumask);
4163 out_free_buffer_mask:
4164 free_cpumask_var(tracing_buffer_mask);
4169 __init static int clear_boot_tracer(void)
4172 * The default tracer at boot buffer is an init section.
4173 * This function is called in lateinit. If we did not
4174 * find the boot tracer, then clear it out, to prevent
4175 * later registration from accessing the buffer that is
4176 * about to be freed.
4178 if (!default_bootup_tracer)
4181 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4182 default_bootup_tracer);
4183 default_bootup_tracer = NULL;
4188 early_initcall(tracer_alloc_buffers);
4189 fs_initcall(tracer_init_debugfs);
4190 late_initcall(clear_boot_tracer);