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/smp_lock.h>
21 #include <linux/notifier.h>
22 #include <linux/irqflags.h>
23 #include <linux/debugfs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/kprobes.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/ctype.h>
36 #include <linux/init.h>
37 #include <linux/poll.h>
38 #include <linux/gfp.h>
42 #include "trace_output.h"
44 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
47 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
50 int ring_buffer_expanded;
53 * We need to change this state when a selftest is running.
54 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
56 * insertions into the ring-buffer such as trace_printk could occurred
57 * at the same time, giving false positive or negative results.
59 static bool __read_mostly tracing_selftest_running;
62 * If a tracer is running, we do not want to run SELFTEST.
64 bool __read_mostly tracing_selftest_disabled;
66 /* For tracers that don't implement custom flags */
67 static struct tracer_opt dummy_tracer_opt[] = {
71 static struct tracer_flags dummy_tracer_flags = {
73 .opts = dummy_tracer_opt
76 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
82 * Kill all tracing for good (never come back).
83 * It is initialized to 1 but will turn to zero if the initialization
84 * of the tracer is successful. But that is the only place that sets
87 static int tracing_disabled = 1;
89 DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
91 static inline void ftrace_disable_cpu(void)
94 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
97 static inline void ftrace_enable_cpu(void)
99 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
103 static cpumask_var_t __read_mostly tracing_buffer_mask;
105 /* Define which cpu buffers are currently read in trace_pipe */
106 static cpumask_var_t tracing_reader_cpumask;
108 #define for_each_tracing_cpu(cpu) \
109 for_each_cpu(cpu, tracing_buffer_mask)
112 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
114 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
115 * is set, then ftrace_dump is called. This will output the contents
116 * of the ftrace buffers to the console. This is very useful for
117 * capturing traces that lead to crashes and outputing it to a
120 * It is default off, but you can enable it with either specifying
121 * "ftrace_dump_on_oops" in the kernel command line, or setting
122 * /proc/sys/kernel/ftrace_dump_on_oops to true.
124 int ftrace_dump_on_oops;
126 static int tracing_set_tracer(const char *buf);
128 #define BOOTUP_TRACER_SIZE 100
129 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
130 static char *default_bootup_tracer;
132 static int __init set_ftrace(char *str)
134 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
135 default_bootup_tracer = bootup_tracer_buf;
136 /* We are using ftrace early, expand it */
137 ring_buffer_expanded = 1;
140 __setup("ftrace=", set_ftrace);
142 static int __init set_ftrace_dump_on_oops(char *str)
144 ftrace_dump_on_oops = 1;
147 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
149 unsigned long long ns2usecs(cycle_t nsec)
157 * The global_trace is the descriptor that holds the tracing
158 * buffers for the live tracing. For each CPU, it contains
159 * a link list of pages that will store trace entries. The
160 * page descriptor of the pages in the memory is used to hold
161 * the link list by linking the lru item in the page descriptor
162 * to each of the pages in the buffer per CPU.
164 * For each active CPU there is a data field that holds the
165 * pages for the buffer for that CPU. Each CPU has the same number
166 * of pages allocated for its buffer.
168 static struct trace_array global_trace;
170 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
172 int filter_current_check_discard(struct ftrace_event_call *call, void *rec,
173 struct ring_buffer_event *event)
175 return filter_check_discard(call, rec, global_trace.buffer, event);
177 EXPORT_SYMBOL_GPL(filter_current_check_discard);
179 cycle_t ftrace_now(int cpu)
183 /* Early boot up does not have a buffer yet */
184 if (!global_trace.buffer)
185 return trace_clock_local();
187 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
188 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
194 * The max_tr is used to snapshot the global_trace when a maximum
195 * latency is reached. Some tracers will use this to store a maximum
196 * trace while it continues examining live traces.
198 * The buffers for the max_tr are set up the same as the global_trace.
199 * When a snapshot is taken, the link list of the max_tr is swapped
200 * with the link list of the global_trace and the buffers are reset for
201 * the global_trace so the tracing can continue.
203 static struct trace_array max_tr;
205 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
207 /* tracer_enabled is used to toggle activation of a tracer */
208 static int tracer_enabled = 1;
211 * tracing_is_enabled - return tracer_enabled status
213 * This function is used by other tracers to know the status
214 * of the tracer_enabled flag. Tracers may use this function
215 * to know if it should enable their features when starting
216 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
218 int tracing_is_enabled(void)
220 return tracer_enabled;
224 * trace_buf_size is the size in bytes that is allocated
225 * for a buffer. Note, the number of bytes is always rounded
228 * This number is purposely set to a low number of 16384.
229 * If the dump on oops happens, it will be much appreciated
230 * to not have to wait for all that output. Anyway this can be
231 * boot time and run time configurable.
233 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
235 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
237 /* trace_types holds a link list of available tracers. */
238 static struct tracer *trace_types __read_mostly;
240 /* current_trace points to the tracer that is currently active */
241 static struct tracer *current_trace __read_mostly;
244 * max_tracer_type_len is used to simplify the allocating of
245 * buffers to read userspace tracer names. We keep track of
246 * the longest tracer name registered.
248 static int max_tracer_type_len;
251 * trace_types_lock is used to protect the trace_types list.
252 * This lock is also used to keep user access serialized.
253 * Accesses from userspace will grab this lock while userspace
254 * activities happen inside the kernel.
256 static DEFINE_MUTEX(trace_types_lock);
258 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
259 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
261 /* trace_flags holds trace_options default values */
262 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
263 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
264 TRACE_ITER_GRAPH_TIME;
266 static int trace_stop_count;
267 static DEFINE_SPINLOCK(tracing_start_lock);
270 * trace_wake_up - wake up tasks waiting for trace input
272 * Simply wakes up any task that is blocked on the trace_wait
273 * queue. These is used with trace_poll for tasks polling the trace.
275 void trace_wake_up(void)
278 * The runqueue_is_locked() can fail, but this is the best we
281 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
282 wake_up(&trace_wait);
285 static int __init set_buf_size(char *str)
287 unsigned long buf_size;
291 buf_size = memparse(str, &str);
292 /* nr_entries can not be zero */
295 trace_buf_size = buf_size;
298 __setup("trace_buf_size=", set_buf_size);
300 unsigned long nsecs_to_usecs(unsigned long nsecs)
305 /* These must match the bit postions in trace_iterator_flags */
306 static const char *trace_options[] = {
335 { trace_clock_local, "local" },
336 { trace_clock_global, "global" },
341 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
349 if (s->len <= s->readpos)
352 len = s->len - s->readpos;
355 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
365 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
370 if (s->len <= s->readpos)
373 len = s->len - s->readpos;
376 ret = memcpy(buf, s->buffer + s->readpos, cnt);
385 * ftrace_max_lock is used to protect the swapping of buffers
386 * when taking a max snapshot. The buffers themselves are
387 * protected by per_cpu spinlocks. But the action of the swap
388 * needs its own lock.
390 * This is defined as a raw_spinlock_t in order to help
391 * with performance when lockdep debugging is enabled.
393 * It is also used in other places outside the update_max_tr
394 * so it needs to be defined outside of the
395 * CONFIG_TRACER_MAX_TRACE.
397 static raw_spinlock_t ftrace_max_lock =
398 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
400 #ifdef CONFIG_TRACER_MAX_TRACE
401 unsigned long __read_mostly tracing_max_latency;
402 unsigned long __read_mostly tracing_thresh;
405 * Copy the new maximum trace into the separate maximum-trace
406 * structure. (this way the maximum trace is permanently saved,
407 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
410 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
412 struct trace_array_cpu *data = tr->data[cpu];
413 struct trace_array_cpu *max_data = tr->data[cpu];
416 max_tr.time_start = data->preempt_timestamp;
418 max_data = max_tr.data[cpu];
419 max_data->saved_latency = tracing_max_latency;
420 max_data->critical_start = data->critical_start;
421 max_data->critical_end = data->critical_end;
423 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
424 max_data->pid = tsk->pid;
425 max_data->uid = task_uid(tsk);
426 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
427 max_data->policy = tsk->policy;
428 max_data->rt_priority = tsk->rt_priority;
430 /* record this tasks comm */
431 tracing_record_cmdline(tsk);
435 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
437 * @tsk: the task with the latency
438 * @cpu: The cpu that initiated the trace.
440 * Flip the buffers between the @tr and the max_tr and record information
441 * about which task was the cause of this latency.
444 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
446 struct ring_buffer *buf = tr->buffer;
448 if (trace_stop_count)
451 WARN_ON_ONCE(!irqs_disabled());
452 __raw_spin_lock(&ftrace_max_lock);
454 tr->buffer = max_tr.buffer;
457 ftrace_disable_cpu();
458 ring_buffer_reset(tr->buffer);
461 __update_max_tr(tr, tsk, cpu);
462 __raw_spin_unlock(&ftrace_max_lock);
466 * update_max_tr_single - only copy one trace over, and reset the rest
468 * @tsk - task with the latency
469 * @cpu - the cpu of the buffer to copy.
471 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
474 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
478 if (trace_stop_count)
481 WARN_ON_ONCE(!irqs_disabled());
482 __raw_spin_lock(&ftrace_max_lock);
484 ftrace_disable_cpu();
486 ring_buffer_reset(max_tr.buffer);
487 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
491 WARN_ON_ONCE(ret && ret != -EAGAIN);
493 __update_max_tr(tr, tsk, cpu);
494 __raw_spin_unlock(&ftrace_max_lock);
496 #endif /* CONFIG_TRACER_MAX_TRACE */
499 * register_tracer - register a tracer with the ftrace system.
500 * @type - the plugin for the tracer
502 * Register a new plugin tracer.
504 int register_tracer(struct tracer *type)
505 __releases(kernel_lock)
506 __acquires(kernel_lock)
513 pr_info("Tracer must have a name\n");
518 * When this gets called we hold the BKL which means that
519 * preemption is disabled. Various trace selftests however
520 * need to disable and enable preemption for successful tests.
521 * So we drop the BKL here and grab it after the tests again.
524 mutex_lock(&trace_types_lock);
526 tracing_selftest_running = true;
528 for (t = trace_types; t; t = t->next) {
529 if (strcmp(type->name, t->name) == 0) {
531 pr_info("Trace %s already registered\n",
539 type->set_flag = &dummy_set_flag;
541 type->flags = &dummy_tracer_flags;
543 if (!type->flags->opts)
544 type->flags->opts = dummy_tracer_opt;
545 if (!type->wait_pipe)
546 type->wait_pipe = default_wait_pipe;
549 #ifdef CONFIG_FTRACE_STARTUP_TEST
550 if (type->selftest && !tracing_selftest_disabled) {
551 struct tracer *saved_tracer = current_trace;
552 struct trace_array *tr = &global_trace;
555 * Run a selftest on this tracer.
556 * Here we reset the trace buffer, and set the current
557 * tracer to be this tracer. The tracer can then run some
558 * internal tracing to verify that everything is in order.
559 * If we fail, we do not register this tracer.
561 tracing_reset_online_cpus(tr);
563 current_trace = type;
564 /* the test is responsible for initializing and enabling */
565 pr_info("Testing tracer %s: ", type->name);
566 ret = type->selftest(type, tr);
567 /* the test is responsible for resetting too */
568 current_trace = saved_tracer;
570 printk(KERN_CONT "FAILED!\n");
573 /* Only reset on passing, to avoid touching corrupted buffers */
574 tracing_reset_online_cpus(tr);
576 printk(KERN_CONT "PASSED\n");
580 type->next = trace_types;
582 len = strlen(type->name);
583 if (len > max_tracer_type_len)
584 max_tracer_type_len = len;
587 tracing_selftest_running = false;
588 mutex_unlock(&trace_types_lock);
590 if (ret || !default_bootup_tracer)
593 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
596 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
597 /* Do we want this tracer to start on bootup? */
598 tracing_set_tracer(type->name);
599 default_bootup_tracer = NULL;
600 /* disable other selftests, since this will break it. */
601 tracing_selftest_disabled = 1;
602 #ifdef CONFIG_FTRACE_STARTUP_TEST
603 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
612 void unregister_tracer(struct tracer *type)
617 mutex_lock(&trace_types_lock);
618 for (t = &trace_types; *t; t = &(*t)->next) {
622 pr_info("Trace %s not registered\n", type->name);
628 if (type == current_trace && tracer_enabled) {
631 if (current_trace->stop)
632 current_trace->stop(&global_trace);
633 current_trace = &nop_trace;
636 if (strlen(type->name) != max_tracer_type_len)
639 max_tracer_type_len = 0;
640 for (t = &trace_types; *t; t = &(*t)->next) {
641 len = strlen((*t)->name);
642 if (len > max_tracer_type_len)
643 max_tracer_type_len = len;
646 mutex_unlock(&trace_types_lock);
649 void tracing_reset(struct trace_array *tr, int cpu)
651 ftrace_disable_cpu();
652 ring_buffer_reset_cpu(tr->buffer, cpu);
656 void tracing_reset_online_cpus(struct trace_array *tr)
658 struct ring_buffer *buffer = tr->buffer;
661 ring_buffer_record_disable(buffer);
663 /* Make sure all commits have finished */
666 tr->time_start = ftrace_now(tr->cpu);
668 for_each_online_cpu(cpu)
669 tracing_reset(tr, cpu);
671 ring_buffer_record_enable(buffer);
674 void tracing_reset_current(int cpu)
676 tracing_reset(&global_trace, cpu);
679 void tracing_reset_current_online_cpus(void)
681 tracing_reset_online_cpus(&global_trace);
684 #define SAVED_CMDLINES 128
685 #define NO_CMDLINE_MAP UINT_MAX
686 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
687 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
688 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
689 static int cmdline_idx;
690 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
692 /* temporary disable recording */
693 static atomic_t trace_record_cmdline_disabled __read_mostly;
695 static void trace_init_cmdlines(void)
697 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
698 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
703 * ftrace_off_permanent - disable all ftrace code permanently
705 * This should only be called when a serious anomally has
706 * been detected. This will turn off the function tracing,
707 * ring buffers, and other tracing utilites. It takes no
708 * locks and can be called from any context.
710 void ftrace_off_permanent(void)
712 tracing_disabled = 1;
714 tracing_off_permanent();
718 * tracing_start - quick start of the tracer
720 * If tracing is enabled but was stopped by tracing_stop,
721 * this will start the tracer back up.
723 void tracing_start(void)
725 struct ring_buffer *buffer;
728 if (tracing_disabled)
731 spin_lock_irqsave(&tracing_start_lock, flags);
732 if (--trace_stop_count) {
733 if (trace_stop_count < 0) {
734 /* Someone screwed up their debugging */
736 trace_stop_count = 0;
742 buffer = global_trace.buffer;
744 ring_buffer_record_enable(buffer);
746 buffer = max_tr.buffer;
748 ring_buffer_record_enable(buffer);
752 spin_unlock_irqrestore(&tracing_start_lock, flags);
756 * tracing_stop - quick stop of the tracer
758 * Light weight way to stop tracing. Use in conjunction with
761 void tracing_stop(void)
763 struct ring_buffer *buffer;
767 spin_lock_irqsave(&tracing_start_lock, flags);
768 if (trace_stop_count++)
771 buffer = global_trace.buffer;
773 ring_buffer_record_disable(buffer);
775 buffer = max_tr.buffer;
777 ring_buffer_record_disable(buffer);
780 spin_unlock_irqrestore(&tracing_start_lock, flags);
783 void trace_stop_cmdline_recording(void);
785 static void trace_save_cmdline(struct task_struct *tsk)
789 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
793 * It's not the end of the world if we don't get
794 * the lock, but we also don't want to spin
795 * nor do we want to disable interrupts,
796 * so if we miss here, then better luck next time.
798 if (!__raw_spin_trylock(&trace_cmdline_lock))
801 idx = map_pid_to_cmdline[tsk->pid];
802 if (idx == NO_CMDLINE_MAP) {
803 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
806 * Check whether the cmdline buffer at idx has a pid
807 * mapped. We are going to overwrite that entry so we
808 * need to clear the map_pid_to_cmdline. Otherwise we
809 * would read the new comm for the old pid.
811 pid = map_cmdline_to_pid[idx];
812 if (pid != NO_CMDLINE_MAP)
813 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
815 map_cmdline_to_pid[idx] = tsk->pid;
816 map_pid_to_cmdline[tsk->pid] = idx;
821 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
823 __raw_spin_unlock(&trace_cmdline_lock);
826 void trace_find_cmdline(int pid, char comm[])
831 strcpy(comm, "<idle>");
835 if (pid > PID_MAX_DEFAULT) {
836 strcpy(comm, "<...>");
841 __raw_spin_lock(&trace_cmdline_lock);
842 map = map_pid_to_cmdline[pid];
843 if (map != NO_CMDLINE_MAP)
844 strcpy(comm, saved_cmdlines[map]);
846 strcpy(comm, "<...>");
848 __raw_spin_unlock(&trace_cmdline_lock);
852 void tracing_record_cmdline(struct task_struct *tsk)
854 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
858 trace_save_cmdline(tsk);
862 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
865 struct task_struct *tsk = current;
867 entry->preempt_count = pc & 0xff;
868 entry->pid = (tsk) ? tsk->pid : 0;
869 entry->tgid = (tsk) ? tsk->tgid : 0;
871 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
872 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
874 TRACE_FLAG_IRQS_NOSUPPORT |
876 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
877 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
878 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
880 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
882 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
885 unsigned long flags, int pc)
887 struct ring_buffer_event *event;
889 event = ring_buffer_lock_reserve(tr->buffer, len);
891 struct trace_entry *ent = ring_buffer_event_data(event);
893 tracing_generic_entry_update(ent, flags, pc);
900 static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
901 struct ring_buffer_event *event,
902 unsigned long flags, int pc,
905 ring_buffer_unlock_commit(tr->buffer, event);
907 ftrace_trace_stack(tr, flags, 6, pc);
908 ftrace_trace_userstack(tr, flags, pc);
914 void trace_buffer_unlock_commit(struct trace_array *tr,
915 struct ring_buffer_event *event,
916 unsigned long flags, int pc)
918 __trace_buffer_unlock_commit(tr, event, flags, pc, 1);
921 struct ring_buffer_event *
922 trace_current_buffer_lock_reserve(int type, unsigned long len,
923 unsigned long flags, int pc)
925 return trace_buffer_lock_reserve(&global_trace,
926 type, len, flags, pc);
928 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
930 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
931 unsigned long flags, int pc)
933 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
935 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
937 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
938 unsigned long flags, int pc)
940 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
942 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
944 void trace_current_buffer_discard_commit(struct ring_buffer_event *event)
946 ring_buffer_discard_commit(global_trace.buffer, event);
948 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
951 trace_function(struct trace_array *tr,
952 unsigned long ip, unsigned long parent_ip, unsigned long flags,
955 struct ftrace_event_call *call = &event_function;
956 struct ring_buffer_event *event;
957 struct ftrace_entry *entry;
959 /* If we are reading the ring buffer, don't trace */
960 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
963 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
967 entry = ring_buffer_event_data(event);
969 entry->parent_ip = parent_ip;
971 if (!filter_check_discard(call, entry, tr->buffer, event))
972 ring_buffer_unlock_commit(tr->buffer, event);
976 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
977 unsigned long ip, unsigned long parent_ip, unsigned long flags,
980 if (likely(!atomic_read(&data->disabled)))
981 trace_function(tr, ip, parent_ip, flags, pc);
984 #ifdef CONFIG_STACKTRACE
985 static void __ftrace_trace_stack(struct trace_array *tr,
989 struct ftrace_event_call *call = &event_kernel_stack;
990 struct ring_buffer_event *event;
991 struct stack_entry *entry;
992 struct stack_trace trace;
994 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
995 sizeof(*entry), flags, pc);
998 entry = ring_buffer_event_data(event);
999 memset(&entry->caller, 0, sizeof(entry->caller));
1001 trace.nr_entries = 0;
1002 trace.max_entries = FTRACE_STACK_ENTRIES;
1004 trace.entries = entry->caller;
1006 save_stack_trace(&trace);
1007 if (!filter_check_discard(call, entry, tr->buffer, event))
1008 ring_buffer_unlock_commit(tr->buffer, event);
1011 void ftrace_trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1014 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1017 __ftrace_trace_stack(tr, flags, skip, pc);
1020 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1023 __ftrace_trace_stack(tr, flags, skip, pc);
1026 void ftrace_trace_userstack(struct trace_array *tr, unsigned long flags, int pc)
1028 struct ftrace_event_call *call = &event_user_stack;
1029 struct ring_buffer_event *event;
1030 struct userstack_entry *entry;
1031 struct stack_trace trace;
1033 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1036 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1037 sizeof(*entry), flags, pc);
1040 entry = ring_buffer_event_data(event);
1042 memset(&entry->caller, 0, sizeof(entry->caller));
1044 trace.nr_entries = 0;
1045 trace.max_entries = FTRACE_STACK_ENTRIES;
1047 trace.entries = entry->caller;
1049 save_stack_trace_user(&trace);
1050 if (!filter_check_discard(call, entry, tr->buffer, event))
1051 ring_buffer_unlock_commit(tr->buffer, event);
1055 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1057 ftrace_trace_userstack(tr, flags, preempt_count());
1061 #endif /* CONFIG_STACKTRACE */
1064 ftrace_trace_special(void *__tr,
1065 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1068 struct ring_buffer_event *event;
1069 struct trace_array *tr = __tr;
1070 struct special_entry *entry;
1072 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1073 sizeof(*entry), 0, pc);
1076 entry = ring_buffer_event_data(event);
1080 trace_buffer_unlock_commit(tr, event, 0, pc);
1084 __trace_special(void *__tr, void *__data,
1085 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1087 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1091 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1093 struct trace_array *tr = &global_trace;
1094 struct trace_array_cpu *data;
1095 unsigned long flags;
1099 if (tracing_disabled)
1102 pc = preempt_count();
1103 local_irq_save(flags);
1104 cpu = raw_smp_processor_id();
1105 data = tr->data[cpu];
1107 if (likely(atomic_inc_return(&data->disabled) == 1))
1108 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1110 atomic_dec(&data->disabled);
1111 local_irq_restore(flags);
1115 * trace_vbprintk - write binary msg to tracing buffer
1118 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1120 static raw_spinlock_t trace_buf_lock =
1121 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1122 static u32 trace_buf[TRACE_BUF_SIZE];
1124 struct ftrace_event_call *call = &event_bprint;
1125 struct ring_buffer_event *event;
1126 struct trace_array *tr = &global_trace;
1127 struct trace_array_cpu *data;
1128 struct bprint_entry *entry;
1129 unsigned long flags;
1132 int cpu, len = 0, size, pc;
1134 if (unlikely(tracing_selftest_running || tracing_disabled))
1137 /* Don't pollute graph traces with trace_vprintk internals */
1138 pause_graph_tracing();
1140 pc = preempt_count();
1141 resched = ftrace_preempt_disable();
1142 cpu = raw_smp_processor_id();
1143 data = tr->data[cpu];
1145 disable = atomic_inc_return(&data->disabled);
1146 if (unlikely(disable != 1))
1149 /* Lockdep uses trace_printk for lock tracing */
1150 local_irq_save(flags);
1151 __raw_spin_lock(&trace_buf_lock);
1152 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1154 if (len > TRACE_BUF_SIZE || len < 0)
1157 size = sizeof(*entry) + sizeof(u32) * len;
1158 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1161 entry = ring_buffer_event_data(event);
1165 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1166 if (!filter_check_discard(call, entry, tr->buffer, event))
1167 ring_buffer_unlock_commit(tr->buffer, event);
1170 __raw_spin_unlock(&trace_buf_lock);
1171 local_irq_restore(flags);
1174 atomic_dec_return(&data->disabled);
1175 ftrace_preempt_enable(resched);
1176 unpause_graph_tracing();
1180 EXPORT_SYMBOL_GPL(trace_vbprintk);
1182 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1184 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1185 static char trace_buf[TRACE_BUF_SIZE];
1187 struct ftrace_event_call *call = &event_print;
1188 struct ring_buffer_event *event;
1189 struct trace_array *tr = &global_trace;
1190 struct trace_array_cpu *data;
1191 int cpu, len = 0, size, pc;
1192 struct print_entry *entry;
1193 unsigned long irq_flags;
1196 if (tracing_disabled || tracing_selftest_running)
1199 pc = preempt_count();
1200 preempt_disable_notrace();
1201 cpu = raw_smp_processor_id();
1202 data = tr->data[cpu];
1204 disable = atomic_inc_return(&data->disabled);
1205 if (unlikely(disable != 1))
1208 pause_graph_tracing();
1209 raw_local_irq_save(irq_flags);
1210 __raw_spin_lock(&trace_buf_lock);
1211 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1213 len = min(len, TRACE_BUF_SIZE-1);
1216 size = sizeof(*entry) + len + 1;
1217 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1220 entry = ring_buffer_event_data(event);
1223 memcpy(&entry->buf, trace_buf, len);
1224 entry->buf[len] = 0;
1225 if (!filter_check_discard(call, entry, tr->buffer, event))
1226 ring_buffer_unlock_commit(tr->buffer, event);
1229 __raw_spin_unlock(&trace_buf_lock);
1230 raw_local_irq_restore(irq_flags);
1231 unpause_graph_tracing();
1233 atomic_dec_return(&data->disabled);
1234 preempt_enable_notrace();
1238 EXPORT_SYMBOL_GPL(trace_vprintk);
1240 enum trace_file_type {
1241 TRACE_FILE_LAT_FMT = 1,
1242 TRACE_FILE_ANNOTATE = 2,
1245 static void trace_iterator_increment(struct trace_iterator *iter)
1247 /* Don't allow ftrace to trace into the ring buffers */
1248 ftrace_disable_cpu();
1251 if (iter->buffer_iter[iter->cpu])
1252 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1254 ftrace_enable_cpu();
1257 static struct trace_entry *
1258 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1260 struct ring_buffer_event *event;
1261 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1263 /* Don't allow ftrace to trace into the ring buffers */
1264 ftrace_disable_cpu();
1267 event = ring_buffer_iter_peek(buf_iter, ts);
1269 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1271 ftrace_enable_cpu();
1273 return event ? ring_buffer_event_data(event) : NULL;
1276 static struct trace_entry *
1277 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1279 struct ring_buffer *buffer = iter->tr->buffer;
1280 struct trace_entry *ent, *next = NULL;
1281 int cpu_file = iter->cpu_file;
1282 u64 next_ts = 0, ts;
1287 * If we are in a per_cpu trace file, don't bother by iterating over
1288 * all cpu and peek directly.
1290 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1291 if (ring_buffer_empty_cpu(buffer, cpu_file))
1293 ent = peek_next_entry(iter, cpu_file, ent_ts);
1295 *ent_cpu = cpu_file;
1300 for_each_tracing_cpu(cpu) {
1302 if (ring_buffer_empty_cpu(buffer, cpu))
1305 ent = peek_next_entry(iter, cpu, &ts);
1308 * Pick the entry with the smallest timestamp:
1310 if (ent && (!next || ts < next_ts)) {
1318 *ent_cpu = next_cpu;
1326 /* Find the next real entry, without updating the iterator itself */
1327 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1328 int *ent_cpu, u64 *ent_ts)
1330 return __find_next_entry(iter, ent_cpu, ent_ts);
1333 /* Find the next real entry, and increment the iterator to the next entry */
1334 static void *find_next_entry_inc(struct trace_iterator *iter)
1336 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1339 trace_iterator_increment(iter);
1341 return iter->ent ? iter : NULL;
1344 static void trace_consume(struct trace_iterator *iter)
1346 /* Don't allow ftrace to trace into the ring buffers */
1347 ftrace_disable_cpu();
1348 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1349 ftrace_enable_cpu();
1352 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1354 struct trace_iterator *iter = m->private;
1360 /* can't go backwards */
1365 ent = find_next_entry_inc(iter);
1369 while (ent && iter->idx < i)
1370 ent = find_next_entry_inc(iter);
1378 * No necessary locking here. The worst thing which can
1379 * happen is loosing events consumed at the same time
1380 * by a trace_pipe reader.
1381 * Other than that, we don't risk to crash the ring buffer
1382 * because it serializes the readers.
1384 * The current tracer is copied to avoid a global locking
1387 static void *s_start(struct seq_file *m, loff_t *pos)
1389 struct trace_iterator *iter = m->private;
1390 static struct tracer *old_tracer;
1391 int cpu_file = iter->cpu_file;
1396 /* copy the tracer to avoid using a global lock all around */
1397 mutex_lock(&trace_types_lock);
1398 if (unlikely(old_tracer != current_trace && current_trace)) {
1399 old_tracer = current_trace;
1400 *iter->trace = *current_trace;
1402 mutex_unlock(&trace_types_lock);
1404 atomic_inc(&trace_record_cmdline_disabled);
1406 if (*pos != iter->pos) {
1411 ftrace_disable_cpu();
1413 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1414 for_each_tracing_cpu(cpu)
1415 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1417 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1420 ftrace_enable_cpu();
1422 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1427 p = s_next(m, p, &l);
1430 trace_event_read_lock();
1434 static void s_stop(struct seq_file *m, void *p)
1436 atomic_dec(&trace_record_cmdline_disabled);
1437 trace_event_read_unlock();
1440 static void print_lat_help_header(struct seq_file *m)
1442 seq_puts(m, "# _------=> CPU# \n");
1443 seq_puts(m, "# / _-----=> irqs-off \n");
1444 seq_puts(m, "# | / _----=> need-resched \n");
1445 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1446 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1447 seq_puts(m, "# |||| / \n");
1448 seq_puts(m, "# ||||| delay \n");
1449 seq_puts(m, "# cmd pid ||||| time | caller \n");
1450 seq_puts(m, "# \\ / ||||| \\ | / \n");
1453 static void print_func_help_header(struct seq_file *m)
1455 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1456 seq_puts(m, "# | | | | |\n");
1461 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1463 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1464 struct trace_array *tr = iter->tr;
1465 struct trace_array_cpu *data = tr->data[tr->cpu];
1466 struct tracer *type = current_trace;
1467 unsigned long total;
1468 unsigned long entries;
1469 const char *name = "preemption";
1474 entries = ring_buffer_entries(iter->tr->buffer);
1476 ring_buffer_overruns(iter->tr->buffer);
1478 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1480 seq_puts(m, "# -----------------------------------"
1481 "---------------------------------\n");
1482 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1483 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1484 nsecs_to_usecs(data->saved_latency),
1488 #if defined(CONFIG_PREEMPT_NONE)
1490 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1492 #elif defined(CONFIG_PREEMPT)
1497 /* These are reserved for later use */
1500 seq_printf(m, " #P:%d)\n", num_online_cpus());
1504 seq_puts(m, "# -----------------\n");
1505 seq_printf(m, "# | task: %.16s-%d "
1506 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1507 data->comm, data->pid, data->uid, data->nice,
1508 data->policy, data->rt_priority);
1509 seq_puts(m, "# -----------------\n");
1511 if (data->critical_start) {
1512 seq_puts(m, "# => started at: ");
1513 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1514 trace_print_seq(m, &iter->seq);
1515 seq_puts(m, "\n# => ended at: ");
1516 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1517 trace_print_seq(m, &iter->seq);
1518 seq_puts(m, "\n#\n");
1524 static void test_cpu_buff_start(struct trace_iterator *iter)
1526 struct trace_seq *s = &iter->seq;
1528 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1531 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1534 if (cpumask_test_cpu(iter->cpu, iter->started))
1537 cpumask_set_cpu(iter->cpu, iter->started);
1539 /* Don't print started cpu buffer for the first entry of the trace */
1541 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1545 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1547 struct trace_seq *s = &iter->seq;
1548 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1549 struct trace_entry *entry;
1550 struct trace_event *event;
1554 test_cpu_buff_start(iter);
1556 event = ftrace_find_event(entry->type);
1558 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1559 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1560 if (!trace_print_lat_context(iter))
1563 if (!trace_print_context(iter))
1569 return event->trace(iter, sym_flags);
1571 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1574 return TRACE_TYPE_HANDLED;
1576 return TRACE_TYPE_PARTIAL_LINE;
1579 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1581 struct trace_seq *s = &iter->seq;
1582 struct trace_entry *entry;
1583 struct trace_event *event;
1587 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1588 if (!trace_seq_printf(s, "%d %d %llu ",
1589 entry->pid, iter->cpu, iter->ts))
1593 event = ftrace_find_event(entry->type);
1595 return event->raw(iter, 0);
1597 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1600 return TRACE_TYPE_HANDLED;
1602 return TRACE_TYPE_PARTIAL_LINE;
1605 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1607 struct trace_seq *s = &iter->seq;
1608 unsigned char newline = '\n';
1609 struct trace_entry *entry;
1610 struct trace_event *event;
1614 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1615 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1616 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1617 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1620 event = ftrace_find_event(entry->type);
1622 enum print_line_t ret = event->hex(iter, 0);
1623 if (ret != TRACE_TYPE_HANDLED)
1627 SEQ_PUT_FIELD_RET(s, newline);
1629 return TRACE_TYPE_HANDLED;
1632 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1634 struct trace_seq *s = &iter->seq;
1635 struct trace_entry *entry;
1636 struct trace_event *event;
1640 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1641 SEQ_PUT_FIELD_RET(s, entry->pid);
1642 SEQ_PUT_FIELD_RET(s, iter->cpu);
1643 SEQ_PUT_FIELD_RET(s, iter->ts);
1646 event = ftrace_find_event(entry->type);
1647 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1650 static int trace_empty(struct trace_iterator *iter)
1654 /* If we are looking at one CPU buffer, only check that one */
1655 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1656 cpu = iter->cpu_file;
1657 if (iter->buffer_iter[cpu]) {
1658 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1661 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1667 for_each_tracing_cpu(cpu) {
1668 if (iter->buffer_iter[cpu]) {
1669 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1672 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1680 /* Called with trace_event_read_lock() held. */
1681 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1683 enum print_line_t ret;
1685 if (iter->trace && iter->trace->print_line) {
1686 ret = iter->trace->print_line(iter);
1687 if (ret != TRACE_TYPE_UNHANDLED)
1691 if (iter->ent->type == TRACE_BPRINT &&
1692 trace_flags & TRACE_ITER_PRINTK &&
1693 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1694 return trace_print_bprintk_msg_only(iter);
1696 if (iter->ent->type == TRACE_PRINT &&
1697 trace_flags & TRACE_ITER_PRINTK &&
1698 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1699 return trace_print_printk_msg_only(iter);
1701 if (trace_flags & TRACE_ITER_BIN)
1702 return print_bin_fmt(iter);
1704 if (trace_flags & TRACE_ITER_HEX)
1705 return print_hex_fmt(iter);
1707 if (trace_flags & TRACE_ITER_RAW)
1708 return print_raw_fmt(iter);
1710 return print_trace_fmt(iter);
1713 static int s_show(struct seq_file *m, void *v)
1715 struct trace_iterator *iter = v;
1717 if (iter->ent == NULL) {
1719 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1722 if (iter->trace && iter->trace->print_header)
1723 iter->trace->print_header(m);
1724 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1725 /* print nothing if the buffers are empty */
1726 if (trace_empty(iter))
1728 print_trace_header(m, iter);
1729 if (!(trace_flags & TRACE_ITER_VERBOSE))
1730 print_lat_help_header(m);
1732 if (!(trace_flags & TRACE_ITER_VERBOSE))
1733 print_func_help_header(m);
1736 print_trace_line(iter);
1737 trace_print_seq(m, &iter->seq);
1743 static struct seq_operations tracer_seq_ops = {
1750 static struct trace_iterator *
1751 __tracing_open(struct inode *inode, struct file *file)
1753 long cpu_file = (long) inode->i_private;
1754 void *fail_ret = ERR_PTR(-ENOMEM);
1755 struct trace_iterator *iter;
1759 if (tracing_disabled)
1760 return ERR_PTR(-ENODEV);
1762 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1764 return ERR_PTR(-ENOMEM);
1767 * We make a copy of the current tracer to avoid concurrent
1768 * changes on it while we are reading.
1770 mutex_lock(&trace_types_lock);
1771 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1776 *iter->trace = *current_trace;
1778 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1781 cpumask_clear(iter->started);
1783 if (current_trace && current_trace->print_max)
1786 iter->tr = &global_trace;
1788 mutex_init(&iter->mutex);
1789 iter->cpu_file = cpu_file;
1791 /* Notify the tracer early; before we stop tracing. */
1792 if (iter->trace && iter->trace->open)
1793 iter->trace->open(iter);
1795 /* Annotate start of buffers if we had overruns */
1796 if (ring_buffer_overruns(iter->tr->buffer))
1797 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1799 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1800 for_each_tracing_cpu(cpu) {
1802 iter->buffer_iter[cpu] =
1803 ring_buffer_read_start(iter->tr->buffer, cpu);
1806 cpu = iter->cpu_file;
1807 iter->buffer_iter[cpu] =
1808 ring_buffer_read_start(iter->tr->buffer, cpu);
1811 /* TODO stop tracer */
1812 ret = seq_open(file, &tracer_seq_ops);
1814 fail_ret = ERR_PTR(ret);
1818 m = file->private_data;
1821 /* stop the trace while dumping */
1824 mutex_unlock(&trace_types_lock);
1829 for_each_tracing_cpu(cpu) {
1830 if (iter->buffer_iter[cpu])
1831 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1833 free_cpumask_var(iter->started);
1835 mutex_unlock(&trace_types_lock);
1842 int tracing_open_generic(struct inode *inode, struct file *filp)
1844 if (tracing_disabled)
1847 filp->private_data = inode->i_private;
1851 static int tracing_release(struct inode *inode, struct file *file)
1853 struct seq_file *m = (struct seq_file *)file->private_data;
1854 struct trace_iterator *iter;
1857 if (!(file->f_mode & FMODE_READ))
1862 mutex_lock(&trace_types_lock);
1863 for_each_tracing_cpu(cpu) {
1864 if (iter->buffer_iter[cpu])
1865 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1868 if (iter->trace && iter->trace->close)
1869 iter->trace->close(iter);
1871 /* reenable tracing if it was previously enabled */
1873 mutex_unlock(&trace_types_lock);
1875 seq_release(inode, file);
1876 mutex_destroy(&iter->mutex);
1877 free_cpumask_var(iter->started);
1883 static int tracing_open(struct inode *inode, struct file *file)
1885 struct trace_iterator *iter;
1888 /* If this file was open for write, then erase contents */
1889 if ((file->f_mode & FMODE_WRITE) &&
1890 (file->f_flags & O_TRUNC)) {
1891 long cpu = (long) inode->i_private;
1893 if (cpu == TRACE_PIPE_ALL_CPU)
1894 tracing_reset_online_cpus(&global_trace);
1896 tracing_reset(&global_trace, cpu);
1899 if (file->f_mode & FMODE_READ) {
1900 iter = __tracing_open(inode, file);
1902 ret = PTR_ERR(iter);
1903 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1904 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1910 t_next(struct seq_file *m, void *v, loff_t *pos)
1912 struct tracer *t = v;
1922 static void *t_start(struct seq_file *m, loff_t *pos)
1927 mutex_lock(&trace_types_lock);
1928 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
1934 static void t_stop(struct seq_file *m, void *p)
1936 mutex_unlock(&trace_types_lock);
1939 static int t_show(struct seq_file *m, void *v)
1941 struct tracer *t = v;
1946 seq_printf(m, "%s", t->name);
1955 static struct seq_operations show_traces_seq_ops = {
1962 static int show_traces_open(struct inode *inode, struct file *file)
1964 if (tracing_disabled)
1967 return seq_open(file, &show_traces_seq_ops);
1971 tracing_write_stub(struct file *filp, const char __user *ubuf,
1972 size_t count, loff_t *ppos)
1977 static const struct file_operations tracing_fops = {
1978 .open = tracing_open,
1980 .write = tracing_write_stub,
1981 .llseek = seq_lseek,
1982 .release = tracing_release,
1985 static const struct file_operations show_traces_fops = {
1986 .open = show_traces_open,
1988 .release = seq_release,
1992 * Only trace on a CPU if the bitmask is set:
1994 static cpumask_var_t tracing_cpumask;
1997 * The tracer itself will not take this lock, but still we want
1998 * to provide a consistent cpumask to user-space:
2000 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2003 * Temporary storage for the character representation of the
2004 * CPU bitmask (and one more byte for the newline):
2006 static char mask_str[NR_CPUS + 1];
2009 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2010 size_t count, loff_t *ppos)
2014 mutex_lock(&tracing_cpumask_update_lock);
2016 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2017 if (count - len < 2) {
2021 len += sprintf(mask_str + len, "\n");
2022 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2025 mutex_unlock(&tracing_cpumask_update_lock);
2031 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2032 size_t count, loff_t *ppos)
2035 cpumask_var_t tracing_cpumask_new;
2037 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2040 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2044 mutex_lock(&tracing_cpumask_update_lock);
2046 local_irq_disable();
2047 __raw_spin_lock(&ftrace_max_lock);
2048 for_each_tracing_cpu(cpu) {
2050 * Increase/decrease the disabled counter if we are
2051 * about to flip a bit in the cpumask:
2053 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2054 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2055 atomic_inc(&global_trace.data[cpu]->disabled);
2057 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2058 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2059 atomic_dec(&global_trace.data[cpu]->disabled);
2062 __raw_spin_unlock(&ftrace_max_lock);
2065 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2067 mutex_unlock(&tracing_cpumask_update_lock);
2068 free_cpumask_var(tracing_cpumask_new);
2073 free_cpumask_var(tracing_cpumask_new);
2078 static const struct file_operations tracing_cpumask_fops = {
2079 .open = tracing_open_generic,
2080 .read = tracing_cpumask_read,
2081 .write = tracing_cpumask_write,
2085 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2086 size_t cnt, loff_t *ppos)
2088 struct tracer_opt *trace_opts;
2096 /* calculate max size */
2097 for (i = 0; trace_options[i]; i++) {
2098 len += strlen(trace_options[i]);
2099 len += 3; /* "no" and newline */
2102 mutex_lock(&trace_types_lock);
2103 tracer_flags = current_trace->flags->val;
2104 trace_opts = current_trace->flags->opts;
2107 * Increase the size with names of options specific
2108 * of the current tracer.
2110 for (i = 0; trace_opts[i].name; i++) {
2111 len += strlen(trace_opts[i].name);
2112 len += 3; /* "no" and newline */
2116 buf = kmalloc(len + 1, GFP_KERNEL);
2118 mutex_unlock(&trace_types_lock);
2122 for (i = 0; trace_options[i]; i++) {
2123 if (trace_flags & (1 << i))
2124 r += sprintf(buf + r, "%s\n", trace_options[i]);
2126 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2129 for (i = 0; trace_opts[i].name; i++) {
2130 if (tracer_flags & trace_opts[i].bit)
2131 r += sprintf(buf + r, "%s\n",
2132 trace_opts[i].name);
2134 r += sprintf(buf + r, "no%s\n",
2135 trace_opts[i].name);
2137 mutex_unlock(&trace_types_lock);
2139 WARN_ON(r >= len + 1);
2141 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2147 /* Try to assign a tracer specific option */
2148 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2150 struct tracer_flags *tracer_flags = trace->flags;
2151 struct tracer_opt *opts = NULL;
2155 for (i = 0; tracer_flags->opts[i].name; i++) {
2156 opts = &tracer_flags->opts[i];
2157 len = strlen(opts->name);
2159 if (strncmp(cmp, opts->name, len) == 0) {
2160 ret = trace->set_flag(tracer_flags->val,
2166 if (!tracer_flags->opts[i].name)
2169 /* Refused to handle */
2174 tracer_flags->val &= ~opts->bit;
2176 tracer_flags->val |= opts->bit;
2181 static void set_tracer_flags(unsigned int mask, int enabled)
2183 /* do nothing if flag is already set */
2184 if (!!(trace_flags & mask) == !!enabled)
2188 trace_flags |= mask;
2190 trace_flags &= ~mask;
2194 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2195 size_t cnt, loff_t *ppos)
2203 if (cnt >= sizeof(buf))
2206 if (copy_from_user(&buf, ubuf, cnt))
2211 if (strncmp(buf, "no", 2) == 0) {
2216 for (i = 0; trace_options[i]; i++) {
2217 int len = strlen(trace_options[i]);
2219 if (strncmp(cmp, trace_options[i], len) == 0) {
2220 set_tracer_flags(1 << i, !neg);
2225 /* If no option could be set, test the specific tracer options */
2226 if (!trace_options[i]) {
2227 mutex_lock(&trace_types_lock);
2228 ret = set_tracer_option(current_trace, cmp, neg);
2229 mutex_unlock(&trace_types_lock);
2239 static const struct file_operations tracing_iter_fops = {
2240 .open = tracing_open_generic,
2241 .read = tracing_trace_options_read,
2242 .write = tracing_trace_options_write,
2245 static const char readme_msg[] =
2246 "tracing mini-HOWTO:\n\n"
2247 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2248 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2249 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2250 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2252 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2253 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2255 "# cat /sys/kernel/debug/tracing/trace_options\n"
2256 "noprint-parent nosym-offset nosym-addr noverbose\n"
2257 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2258 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2259 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2260 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2264 tracing_readme_read(struct file *filp, char __user *ubuf,
2265 size_t cnt, loff_t *ppos)
2267 return simple_read_from_buffer(ubuf, cnt, ppos,
2268 readme_msg, strlen(readme_msg));
2271 static const struct file_operations tracing_readme_fops = {
2272 .open = tracing_open_generic,
2273 .read = tracing_readme_read,
2277 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2278 size_t cnt, loff_t *ppos)
2287 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2291 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2299 for (i = 0; i < SAVED_CMDLINES; i++) {
2302 pid = map_cmdline_to_pid[i];
2303 if (pid == -1 || pid == NO_CMDLINE_MAP)
2306 trace_find_cmdline(pid, buf_comm);
2307 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2312 len = simple_read_from_buffer(ubuf, cnt, ppos,
2321 static const struct file_operations tracing_saved_cmdlines_fops = {
2322 .open = tracing_open_generic,
2323 .read = tracing_saved_cmdlines_read,
2327 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2328 size_t cnt, loff_t *ppos)
2333 r = sprintf(buf, "%u\n", tracer_enabled);
2334 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2338 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2339 size_t cnt, loff_t *ppos)
2341 struct trace_array *tr = filp->private_data;
2346 if (cnt >= sizeof(buf))
2349 if (copy_from_user(&buf, ubuf, cnt))
2354 ret = strict_strtoul(buf, 10, &val);
2360 mutex_lock(&trace_types_lock);
2361 if (tracer_enabled ^ val) {
2364 if (current_trace->start)
2365 current_trace->start(tr);
2370 if (current_trace->stop)
2371 current_trace->stop(tr);
2374 mutex_unlock(&trace_types_lock);
2382 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2383 size_t cnt, loff_t *ppos)
2385 char buf[max_tracer_type_len+2];
2388 mutex_lock(&trace_types_lock);
2390 r = sprintf(buf, "%s\n", current_trace->name);
2392 r = sprintf(buf, "\n");
2393 mutex_unlock(&trace_types_lock);
2395 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2398 int tracer_init(struct tracer *t, struct trace_array *tr)
2400 tracing_reset_online_cpus(tr);
2404 static int tracing_resize_ring_buffer(unsigned long size)
2409 * If kernel or user changes the size of the ring buffer
2410 * we use the size that was given, and we can forget about
2411 * expanding it later.
2413 ring_buffer_expanded = 1;
2415 ret = ring_buffer_resize(global_trace.buffer, size);
2419 ret = ring_buffer_resize(max_tr.buffer, size);
2423 r = ring_buffer_resize(global_trace.buffer,
2424 global_trace.entries);
2427 * AARGH! We are left with different
2428 * size max buffer!!!!
2429 * The max buffer is our "snapshot" buffer.
2430 * When a tracer needs a snapshot (one of the
2431 * latency tracers), it swaps the max buffer
2432 * with the saved snap shot. We succeeded to
2433 * update the size of the main buffer, but failed to
2434 * update the size of the max buffer. But when we tried
2435 * to reset the main buffer to the original size, we
2436 * failed there too. This is very unlikely to
2437 * happen, but if it does, warn and kill all
2441 tracing_disabled = 1;
2446 global_trace.entries = size;
2452 * tracing_update_buffers - used by tracing facility to expand ring buffers
2454 * To save on memory when the tracing is never used on a system with it
2455 * configured in. The ring buffers are set to a minimum size. But once
2456 * a user starts to use the tracing facility, then they need to grow
2457 * to their default size.
2459 * This function is to be called when a tracer is about to be used.
2461 int tracing_update_buffers(void)
2465 mutex_lock(&trace_types_lock);
2466 if (!ring_buffer_expanded)
2467 ret = tracing_resize_ring_buffer(trace_buf_size);
2468 mutex_unlock(&trace_types_lock);
2473 struct trace_option_dentry;
2475 static struct trace_option_dentry *
2476 create_trace_option_files(struct tracer *tracer);
2479 destroy_trace_option_files(struct trace_option_dentry *topts);
2481 static int tracing_set_tracer(const char *buf)
2483 static struct trace_option_dentry *topts;
2484 struct trace_array *tr = &global_trace;
2488 mutex_lock(&trace_types_lock);
2490 if (!ring_buffer_expanded) {
2491 ret = tracing_resize_ring_buffer(trace_buf_size);
2497 for (t = trace_types; t; t = t->next) {
2498 if (strcmp(t->name, buf) == 0)
2505 if (t == current_trace)
2508 trace_branch_disable();
2509 if (current_trace && current_trace->reset)
2510 current_trace->reset(tr);
2512 destroy_trace_option_files(topts);
2516 topts = create_trace_option_files(current_trace);
2519 ret = tracer_init(t, tr);
2524 trace_branch_enable(tr);
2526 mutex_unlock(&trace_types_lock);
2532 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2533 size_t cnt, loff_t *ppos)
2535 char buf[max_tracer_type_len+1];
2542 if (cnt > max_tracer_type_len)
2543 cnt = max_tracer_type_len;
2545 if (copy_from_user(&buf, ubuf, cnt))
2550 /* strip ending whitespace. */
2551 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2554 err = tracing_set_tracer(buf);
2564 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2565 size_t cnt, loff_t *ppos)
2567 unsigned long *ptr = filp->private_data;
2571 r = snprintf(buf, sizeof(buf), "%ld\n",
2572 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2573 if (r > sizeof(buf))
2575 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2579 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2580 size_t cnt, loff_t *ppos)
2582 unsigned long *ptr = filp->private_data;
2587 if (cnt >= sizeof(buf))
2590 if (copy_from_user(&buf, ubuf, cnt))
2595 ret = strict_strtoul(buf, 10, &val);
2604 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2606 long cpu_file = (long) inode->i_private;
2607 struct trace_iterator *iter;
2610 if (tracing_disabled)
2613 mutex_lock(&trace_types_lock);
2615 /* We only allow one reader per cpu */
2616 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2617 if (!cpumask_empty(tracing_reader_cpumask)) {
2621 cpumask_setall(tracing_reader_cpumask);
2623 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2624 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2631 /* create a buffer to store the information to pass to userspace */
2632 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2639 * We make a copy of the current tracer to avoid concurrent
2640 * changes on it while we are reading.
2642 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2648 *iter->trace = *current_trace;
2650 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2655 /* trace pipe does not show start of buffer */
2656 cpumask_setall(iter->started);
2658 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2659 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2661 iter->cpu_file = cpu_file;
2662 iter->tr = &global_trace;
2663 mutex_init(&iter->mutex);
2664 filp->private_data = iter;
2666 if (iter->trace->pipe_open)
2667 iter->trace->pipe_open(iter);
2670 mutex_unlock(&trace_types_lock);
2676 mutex_unlock(&trace_types_lock);
2680 static int tracing_release_pipe(struct inode *inode, struct file *file)
2682 struct trace_iterator *iter = file->private_data;
2684 mutex_lock(&trace_types_lock);
2686 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2687 cpumask_clear(tracing_reader_cpumask);
2689 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2691 mutex_unlock(&trace_types_lock);
2693 free_cpumask_var(iter->started);
2694 mutex_destroy(&iter->mutex);
2702 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2704 struct trace_iterator *iter = filp->private_data;
2706 if (trace_flags & TRACE_ITER_BLOCK) {
2708 * Always select as readable when in blocking mode
2710 return POLLIN | POLLRDNORM;
2712 if (!trace_empty(iter))
2713 return POLLIN | POLLRDNORM;
2714 poll_wait(filp, &trace_wait, poll_table);
2715 if (!trace_empty(iter))
2716 return POLLIN | POLLRDNORM;
2723 void default_wait_pipe(struct trace_iterator *iter)
2727 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2729 if (trace_empty(iter))
2732 finish_wait(&trace_wait, &wait);
2736 * This is a make-shift waitqueue.
2737 * A tracer might use this callback on some rare cases:
2739 * 1) the current tracer might hold the runqueue lock when it wakes up
2740 * a reader, hence a deadlock (sched, function, and function graph tracers)
2741 * 2) the function tracers, trace all functions, we don't want
2742 * the overhead of calling wake_up and friends
2743 * (and tracing them too)
2745 * Anyway, this is really very primitive wakeup.
2747 void poll_wait_pipe(struct trace_iterator *iter)
2749 set_current_state(TASK_INTERRUPTIBLE);
2750 /* sleep for 100 msecs, and try again. */
2751 schedule_timeout(HZ / 10);
2754 /* Must be called with trace_types_lock mutex held. */
2755 static int tracing_wait_pipe(struct file *filp)
2757 struct trace_iterator *iter = filp->private_data;
2759 while (trace_empty(iter)) {
2761 if ((filp->f_flags & O_NONBLOCK)) {
2765 mutex_unlock(&iter->mutex);
2767 iter->trace->wait_pipe(iter);
2769 mutex_lock(&iter->mutex);
2771 if (signal_pending(current))
2775 * We block until we read something and tracing is disabled.
2776 * We still block if tracing is disabled, but we have never
2777 * read anything. This allows a user to cat this file, and
2778 * then enable tracing. But after we have read something,
2779 * we give an EOF when tracing is again disabled.
2781 * iter->pos will be 0 if we haven't read anything.
2783 if (!tracer_enabled && iter->pos)
2794 tracing_read_pipe(struct file *filp, char __user *ubuf,
2795 size_t cnt, loff_t *ppos)
2797 struct trace_iterator *iter = filp->private_data;
2798 static struct tracer *old_tracer;
2801 /* return any leftover data */
2802 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2806 trace_seq_init(&iter->seq);
2808 /* copy the tracer to avoid using a global lock all around */
2809 mutex_lock(&trace_types_lock);
2810 if (unlikely(old_tracer != current_trace && current_trace)) {
2811 old_tracer = current_trace;
2812 *iter->trace = *current_trace;
2814 mutex_unlock(&trace_types_lock);
2817 * Avoid more than one consumer on a single file descriptor
2818 * This is just a matter of traces coherency, the ring buffer itself
2821 mutex_lock(&iter->mutex);
2822 if (iter->trace->read) {
2823 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2829 sret = tracing_wait_pipe(filp);
2833 /* stop when tracing is finished */
2834 if (trace_empty(iter)) {
2839 if (cnt >= PAGE_SIZE)
2840 cnt = PAGE_SIZE - 1;
2842 /* reset all but tr, trace, and overruns */
2843 memset(&iter->seq, 0,
2844 sizeof(struct trace_iterator) -
2845 offsetof(struct trace_iterator, seq));
2848 trace_event_read_lock();
2849 while (find_next_entry_inc(iter) != NULL) {
2850 enum print_line_t ret;
2851 int len = iter->seq.len;
2853 ret = print_trace_line(iter);
2854 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2855 /* don't print partial lines */
2856 iter->seq.len = len;
2859 if (ret != TRACE_TYPE_NO_CONSUME)
2860 trace_consume(iter);
2862 if (iter->seq.len >= cnt)
2865 trace_event_read_unlock();
2867 /* Now copy what we have to the user */
2868 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2869 if (iter->seq.readpos >= iter->seq.len)
2870 trace_seq_init(&iter->seq);
2873 * If there was nothing to send to user, inspite of consuming trace
2874 * entries, go back to wait for more entries.
2880 mutex_unlock(&iter->mutex);
2885 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2886 struct pipe_buffer *buf)
2888 __free_page(buf->page);
2891 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2894 __free_page(spd->pages[idx]);
2897 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2899 .map = generic_pipe_buf_map,
2900 .unmap = generic_pipe_buf_unmap,
2901 .confirm = generic_pipe_buf_confirm,
2902 .release = tracing_pipe_buf_release,
2903 .steal = generic_pipe_buf_steal,
2904 .get = generic_pipe_buf_get,
2908 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2913 /* Seq buffer is page-sized, exactly what we need. */
2915 count = iter->seq.len;
2916 ret = print_trace_line(iter);
2917 count = iter->seq.len - count;
2920 iter->seq.len -= count;
2923 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2924 iter->seq.len -= count;
2928 if (ret != TRACE_TYPE_NO_CONSUME)
2929 trace_consume(iter);
2931 if (!find_next_entry_inc(iter)) {
2941 static ssize_t tracing_splice_read_pipe(struct file *filp,
2943 struct pipe_inode_info *pipe,
2947 struct page *pages[PIPE_BUFFERS];
2948 struct partial_page partial[PIPE_BUFFERS];
2949 struct trace_iterator *iter = filp->private_data;
2950 struct splice_pipe_desc spd = {
2953 .nr_pages = 0, /* This gets updated below. */
2955 .ops = &tracing_pipe_buf_ops,
2956 .spd_release = tracing_spd_release_pipe,
2958 static struct tracer *old_tracer;
2963 /* copy the tracer to avoid using a global lock all around */
2964 mutex_lock(&trace_types_lock);
2965 if (unlikely(old_tracer != current_trace && current_trace)) {
2966 old_tracer = current_trace;
2967 *iter->trace = *current_trace;
2969 mutex_unlock(&trace_types_lock);
2971 mutex_lock(&iter->mutex);
2973 if (iter->trace->splice_read) {
2974 ret = iter->trace->splice_read(iter, filp,
2975 ppos, pipe, len, flags);
2980 ret = tracing_wait_pipe(filp);
2984 if (!iter->ent && !find_next_entry_inc(iter)) {
2989 trace_event_read_lock();
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 trace_event_read_unlock();
3014 mutex_unlock(&iter->mutex);
3018 return splice_to_pipe(pipe, &spd);
3021 mutex_unlock(&iter->mutex);
3027 tracing_entries_read(struct file *filp, char __user *ubuf,
3028 size_t cnt, loff_t *ppos)
3030 struct trace_array *tr = filp->private_data;
3034 mutex_lock(&trace_types_lock);
3035 if (!ring_buffer_expanded)
3036 r = sprintf(buf, "%lu (expanded: %lu)\n",
3038 trace_buf_size >> 10);
3040 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3041 mutex_unlock(&trace_types_lock);
3043 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3047 tracing_entries_write(struct file *filp, const char __user *ubuf,
3048 size_t cnt, loff_t *ppos)
3054 if (cnt >= sizeof(buf))
3057 if (copy_from_user(&buf, ubuf, cnt))
3062 ret = strict_strtoul(buf, 10, &val);
3066 /* must have at least 1 entry */
3070 mutex_lock(&trace_types_lock);
3074 /* disable all cpu buffers */
3075 for_each_tracing_cpu(cpu) {
3076 if (global_trace.data[cpu])
3077 atomic_inc(&global_trace.data[cpu]->disabled);
3078 if (max_tr.data[cpu])
3079 atomic_inc(&max_tr.data[cpu]->disabled);
3082 /* value is in KB */
3085 if (val != global_trace.entries) {
3086 ret = tracing_resize_ring_buffer(val);
3095 /* If check pages failed, return ENOMEM */
3096 if (tracing_disabled)
3099 for_each_tracing_cpu(cpu) {
3100 if (global_trace.data[cpu])
3101 atomic_dec(&global_trace.data[cpu]->disabled);
3102 if (max_tr.data[cpu])
3103 atomic_dec(&max_tr.data[cpu]->disabled);
3107 max_tr.entries = global_trace.entries;
3108 mutex_unlock(&trace_types_lock);
3113 static int mark_printk(const char *fmt, ...)
3117 va_start(args, fmt);
3118 ret = trace_vprintk(0, fmt, args);
3124 tracing_mark_write(struct file *filp, const char __user *ubuf,
3125 size_t cnt, loff_t *fpos)
3130 if (tracing_disabled)
3133 if (cnt > TRACE_BUF_SIZE)
3134 cnt = TRACE_BUF_SIZE;
3136 buf = kmalloc(cnt + 1, GFP_KERNEL);
3140 if (copy_from_user(buf, ubuf, cnt)) {
3145 /* Cut from the first nil or newline. */
3147 end = strchr(buf, '\n');
3151 cnt = mark_printk("%s\n", buf);
3158 static ssize_t tracing_clock_read(struct file *filp, char __user *ubuf,
3159 size_t cnt, loff_t *ppos)
3165 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3166 bufiter += snprintf(buf + bufiter, sizeof(buf) - bufiter,
3167 "%s%s%s%s", i ? " " : "",
3168 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3169 i == trace_clock_id ? "]" : "");
3170 bufiter += snprintf(buf + bufiter, sizeof(buf) - bufiter, "\n");
3172 return simple_read_from_buffer(ubuf, cnt, ppos, buf, bufiter);
3175 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3176 size_t cnt, loff_t *fpos)
3179 const char *clockstr;
3182 if (cnt >= sizeof(buf))
3185 if (copy_from_user(&buf, ubuf, cnt))
3190 clockstr = strstrip(buf);
3192 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3193 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3196 if (i == ARRAY_SIZE(trace_clocks))
3201 mutex_lock(&trace_types_lock);
3203 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3205 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3207 mutex_unlock(&trace_types_lock);
3214 static const struct file_operations tracing_max_lat_fops = {
3215 .open = tracing_open_generic,
3216 .read = tracing_max_lat_read,
3217 .write = tracing_max_lat_write,
3220 static const struct file_operations tracing_ctrl_fops = {
3221 .open = tracing_open_generic,
3222 .read = tracing_ctrl_read,
3223 .write = tracing_ctrl_write,
3226 static const struct file_operations set_tracer_fops = {
3227 .open = tracing_open_generic,
3228 .read = tracing_set_trace_read,
3229 .write = tracing_set_trace_write,
3232 static const struct file_operations tracing_pipe_fops = {
3233 .open = tracing_open_pipe,
3234 .poll = tracing_poll_pipe,
3235 .read = tracing_read_pipe,
3236 .splice_read = tracing_splice_read_pipe,
3237 .release = tracing_release_pipe,
3240 static const struct file_operations tracing_entries_fops = {
3241 .open = tracing_open_generic,
3242 .read = tracing_entries_read,
3243 .write = tracing_entries_write,
3246 static const struct file_operations tracing_mark_fops = {
3247 .open = tracing_open_generic,
3248 .write = tracing_mark_write,
3251 static const struct file_operations trace_clock_fops = {
3252 .open = tracing_open_generic,
3253 .read = tracing_clock_read,
3254 .write = tracing_clock_write,
3257 struct ftrace_buffer_info {
3258 struct trace_array *tr;
3264 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3266 int cpu = (int)(long)inode->i_private;
3267 struct ftrace_buffer_info *info;
3269 if (tracing_disabled)
3272 info = kzalloc(sizeof(*info), GFP_KERNEL);
3276 info->tr = &global_trace;
3279 /* Force reading ring buffer for first read */
3280 info->read = (unsigned int)-1;
3282 filp->private_data = info;
3284 return nonseekable_open(inode, filp);
3288 tracing_buffers_read(struct file *filp, char __user *ubuf,
3289 size_t count, loff_t *ppos)
3291 struct ftrace_buffer_info *info = filp->private_data;
3300 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3304 /* Do we have previous read data to read? */
3305 if (info->read < PAGE_SIZE)
3310 ret = ring_buffer_read_page(info->tr->buffer,
3317 pos = ring_buffer_page_len(info->spare);
3319 if (pos < PAGE_SIZE)
3320 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3323 size = PAGE_SIZE - info->read;
3327 ret = copy_to_user(ubuf, info->spare + info->read, size);
3338 static int tracing_buffers_release(struct inode *inode, struct file *file)
3340 struct ftrace_buffer_info *info = file->private_data;
3343 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3350 struct ring_buffer *buffer;
3355 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3356 struct pipe_buffer *buf)
3358 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3363 ring_buffer_free_read_page(ref->buffer, ref->page);
3368 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3369 struct pipe_buffer *buf)
3374 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3375 struct pipe_buffer *buf)
3377 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3382 /* Pipe buffer operations for a buffer. */
3383 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3385 .map = generic_pipe_buf_map,
3386 .unmap = generic_pipe_buf_unmap,
3387 .confirm = generic_pipe_buf_confirm,
3388 .release = buffer_pipe_buf_release,
3389 .steal = buffer_pipe_buf_steal,
3390 .get = buffer_pipe_buf_get,
3394 * Callback from splice_to_pipe(), if we need to release some pages
3395 * at the end of the spd in case we error'ed out in filling the pipe.
3397 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3399 struct buffer_ref *ref =
3400 (struct buffer_ref *)spd->partial[i].private;
3405 ring_buffer_free_read_page(ref->buffer, ref->page);
3407 spd->partial[i].private = 0;
3411 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3412 struct pipe_inode_info *pipe, size_t len,
3415 struct ftrace_buffer_info *info = file->private_data;
3416 struct partial_page partial[PIPE_BUFFERS];
3417 struct page *pages[PIPE_BUFFERS];
3418 struct splice_pipe_desc spd = {
3422 .ops = &buffer_pipe_buf_ops,
3423 .spd_release = buffer_spd_release,
3425 struct buffer_ref *ref;
3426 int entries, size, i;
3429 if (*ppos & (PAGE_SIZE - 1)) {
3430 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3434 if (len & (PAGE_SIZE - 1)) {
3435 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3436 if (len < PAGE_SIZE)
3441 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3443 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
3447 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3452 ref->buffer = info->tr->buffer;
3453 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3459 r = ring_buffer_read_page(ref->buffer, &ref->page,
3462 ring_buffer_free_read_page(ref->buffer,
3469 * zero out any left over data, this is going to
3472 size = ring_buffer_page_len(ref->page);
3473 if (size < PAGE_SIZE)
3474 memset(ref->page + size, 0, PAGE_SIZE - size);
3476 page = virt_to_page(ref->page);
3478 spd.pages[i] = page;
3479 spd.partial[i].len = PAGE_SIZE;
3480 spd.partial[i].offset = 0;
3481 spd.partial[i].private = (unsigned long)ref;
3485 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3490 /* did we read anything? */
3491 if (!spd.nr_pages) {
3492 if (flags & SPLICE_F_NONBLOCK)
3500 ret = splice_to_pipe(pipe, &spd);
3505 static const struct file_operations tracing_buffers_fops = {
3506 .open = tracing_buffers_open,
3507 .read = tracing_buffers_read,
3508 .release = tracing_buffers_release,
3509 .splice_read = tracing_buffers_splice_read,
3510 .llseek = no_llseek,
3514 tracing_stats_read(struct file *filp, char __user *ubuf,
3515 size_t count, loff_t *ppos)
3517 unsigned long cpu = (unsigned long)filp->private_data;
3518 struct trace_array *tr = &global_trace;
3519 struct trace_seq *s;
3522 s = kmalloc(sizeof(*s), GFP_KERNEL);
3528 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3529 trace_seq_printf(s, "entries: %ld\n", cnt);
3531 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3532 trace_seq_printf(s, "overrun: %ld\n", cnt);
3534 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3535 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3537 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3544 static const struct file_operations tracing_stats_fops = {
3545 .open = tracing_open_generic,
3546 .read = tracing_stats_read,
3549 #ifdef CONFIG_DYNAMIC_FTRACE
3551 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3557 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3558 size_t cnt, loff_t *ppos)
3560 static char ftrace_dyn_info_buffer[1024];
3561 static DEFINE_MUTEX(dyn_info_mutex);
3562 unsigned long *p = filp->private_data;
3563 char *buf = ftrace_dyn_info_buffer;
3564 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3567 mutex_lock(&dyn_info_mutex);
3568 r = sprintf(buf, "%ld ", *p);
3570 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3573 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3575 mutex_unlock(&dyn_info_mutex);
3580 static const struct file_operations tracing_dyn_info_fops = {
3581 .open = tracing_open_generic,
3582 .read = tracing_read_dyn_info,
3586 static struct dentry *d_tracer;
3588 struct dentry *tracing_init_dentry(void)
3595 if (!debugfs_initialized())
3598 d_tracer = debugfs_create_dir("tracing", NULL);
3600 if (!d_tracer && !once) {
3602 pr_warning("Could not create debugfs directory 'tracing'\n");
3609 static struct dentry *d_percpu;
3611 struct dentry *tracing_dentry_percpu(void)
3614 struct dentry *d_tracer;
3619 d_tracer = tracing_init_dentry();
3624 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3626 if (!d_percpu && !once) {
3628 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3635 static void tracing_init_debugfs_percpu(long cpu)
3637 struct dentry *d_percpu = tracing_dentry_percpu();
3638 struct dentry *d_cpu;
3639 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3642 if (cpu > 999 || cpu < 0)
3645 sprintf(cpu_dir, "cpu%ld", cpu);
3646 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3648 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3652 /* per cpu trace_pipe */
3653 trace_create_file("trace_pipe", 0444, d_cpu,
3654 (void *) cpu, &tracing_pipe_fops);
3657 trace_create_file("trace", 0644, d_cpu,
3658 (void *) cpu, &tracing_fops);
3660 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3661 (void *) cpu, &tracing_buffers_fops);
3663 trace_create_file("stats", 0444, d_cpu,
3664 (void *) cpu, &tracing_stats_fops);
3667 #ifdef CONFIG_FTRACE_SELFTEST
3668 /* Let selftest have access to static functions in this file */
3669 #include "trace_selftest.c"
3672 struct trace_option_dentry {
3673 struct tracer_opt *opt;
3674 struct tracer_flags *flags;
3675 struct dentry *entry;
3679 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3682 struct trace_option_dentry *topt = filp->private_data;
3685 if (topt->flags->val & topt->opt->bit)
3690 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3694 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3697 struct trace_option_dentry *topt = filp->private_data;
3702 if (cnt >= sizeof(buf))
3705 if (copy_from_user(&buf, ubuf, cnt))
3710 ret = strict_strtoul(buf, 10, &val);
3717 /* do nothing if already cleared */
3718 if (!(topt->flags->val & topt->opt->bit))
3721 mutex_lock(&trace_types_lock);
3722 if (current_trace->set_flag)
3723 ret = current_trace->set_flag(topt->flags->val,
3725 mutex_unlock(&trace_types_lock);
3728 topt->flags->val &= ~topt->opt->bit;
3731 /* do nothing if already set */
3732 if (topt->flags->val & topt->opt->bit)
3735 mutex_lock(&trace_types_lock);
3736 if (current_trace->set_flag)
3737 ret = current_trace->set_flag(topt->flags->val,
3739 mutex_unlock(&trace_types_lock);
3742 topt->flags->val |= topt->opt->bit;
3755 static const struct file_operations trace_options_fops = {
3756 .open = tracing_open_generic,
3757 .read = trace_options_read,
3758 .write = trace_options_write,
3762 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3765 long index = (long)filp->private_data;
3768 if (trace_flags & (1 << index))
3773 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3777 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3780 long index = (long)filp->private_data;
3785 if (cnt >= sizeof(buf))
3788 if (copy_from_user(&buf, ubuf, cnt))
3793 ret = strict_strtoul(buf, 10, &val);
3799 trace_flags &= ~(1 << index);
3802 trace_flags |= 1 << index;
3814 static const struct file_operations trace_options_core_fops = {
3815 .open = tracing_open_generic,
3816 .read = trace_options_core_read,
3817 .write = trace_options_core_write,
3820 struct dentry *trace_create_file(const char *name,
3822 struct dentry *parent,
3824 const struct file_operations *fops)
3828 ret = debugfs_create_file(name, mode, parent, data, fops);
3830 pr_warning("Could not create debugfs '%s' entry\n", name);
3836 static struct dentry *trace_options_init_dentry(void)
3838 struct dentry *d_tracer;
3839 static struct dentry *t_options;
3844 d_tracer = tracing_init_dentry();
3848 t_options = debugfs_create_dir("options", d_tracer);
3850 pr_warning("Could not create debugfs directory 'options'\n");
3858 create_trace_option_file(struct trace_option_dentry *topt,
3859 struct tracer_flags *flags,
3860 struct tracer_opt *opt)
3862 struct dentry *t_options;
3864 t_options = trace_options_init_dentry();
3868 topt->flags = flags;
3871 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
3872 &trace_options_fops);
3876 static struct trace_option_dentry *
3877 create_trace_option_files(struct tracer *tracer)
3879 struct trace_option_dentry *topts;
3880 struct tracer_flags *flags;
3881 struct tracer_opt *opts;
3887 flags = tracer->flags;
3889 if (!flags || !flags->opts)
3894 for (cnt = 0; opts[cnt].name; cnt++)
3897 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3901 for (cnt = 0; opts[cnt].name; cnt++)
3902 create_trace_option_file(&topts[cnt], flags,
3909 destroy_trace_option_files(struct trace_option_dentry *topts)
3916 for (cnt = 0; topts[cnt].opt; cnt++) {
3917 if (topts[cnt].entry)
3918 debugfs_remove(topts[cnt].entry);
3924 static struct dentry *
3925 create_trace_option_core_file(const char *option, long index)
3927 struct dentry *t_options;
3929 t_options = trace_options_init_dentry();
3933 return trace_create_file(option, 0644, t_options, (void *)index,
3934 &trace_options_core_fops);
3937 static __init void create_trace_options_dir(void)
3939 struct dentry *t_options;
3942 t_options = trace_options_init_dentry();
3946 for (i = 0; trace_options[i]; i++)
3947 create_trace_option_core_file(trace_options[i], i);
3950 static __init int tracer_init_debugfs(void)
3952 struct dentry *d_tracer;
3955 d_tracer = tracing_init_dentry();
3957 trace_create_file("tracing_enabled", 0644, d_tracer,
3958 &global_trace, &tracing_ctrl_fops);
3960 trace_create_file("trace_options", 0644, d_tracer,
3961 NULL, &tracing_iter_fops);
3963 trace_create_file("tracing_cpumask", 0644, d_tracer,
3964 NULL, &tracing_cpumask_fops);
3966 trace_create_file("trace", 0644, d_tracer,
3967 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3969 trace_create_file("available_tracers", 0444, d_tracer,
3970 &global_trace, &show_traces_fops);
3972 trace_create_file("current_tracer", 0644, d_tracer,
3973 &global_trace, &set_tracer_fops);
3975 #ifdef CONFIG_TRACER_MAX_TRACE
3976 trace_create_file("tracing_max_latency", 0644, d_tracer,
3977 &tracing_max_latency, &tracing_max_lat_fops);
3979 trace_create_file("tracing_thresh", 0644, d_tracer,
3980 &tracing_thresh, &tracing_max_lat_fops);
3983 trace_create_file("README", 0444, d_tracer,
3984 NULL, &tracing_readme_fops);
3986 trace_create_file("trace_pipe", 0444, d_tracer,
3987 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3989 trace_create_file("buffer_size_kb", 0644, d_tracer,
3990 &global_trace, &tracing_entries_fops);
3992 trace_create_file("trace_marker", 0220, d_tracer,
3993 NULL, &tracing_mark_fops);
3995 trace_create_file("saved_cmdlines", 0444, d_tracer,
3996 NULL, &tracing_saved_cmdlines_fops);
3998 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4001 #ifdef CONFIG_DYNAMIC_FTRACE
4002 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4003 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4005 #ifdef CONFIG_SYSPROF_TRACER
4006 init_tracer_sysprof_debugfs(d_tracer);
4009 create_trace_options_dir();
4011 for_each_tracing_cpu(cpu)
4012 tracing_init_debugfs_percpu(cpu);
4017 static int trace_panic_handler(struct notifier_block *this,
4018 unsigned long event, void *unused)
4020 if (ftrace_dump_on_oops)
4025 static struct notifier_block trace_panic_notifier = {
4026 .notifier_call = trace_panic_handler,
4028 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4031 static int trace_die_handler(struct notifier_block *self,
4037 if (ftrace_dump_on_oops)
4046 static struct notifier_block trace_die_notifier = {
4047 .notifier_call = trace_die_handler,
4052 * printk is set to max of 1024, we really don't need it that big.
4053 * Nothing should be printing 1000 characters anyway.
4055 #define TRACE_MAX_PRINT 1000
4058 * Define here KERN_TRACE so that we have one place to modify
4059 * it if we decide to change what log level the ftrace dump
4062 #define KERN_TRACE KERN_EMERG
4065 trace_printk_seq(struct trace_seq *s)
4067 /* Probably should print a warning here. */
4071 /* should be zero ended, but we are paranoid. */
4072 s->buffer[s->len] = 0;
4074 printk(KERN_TRACE "%s", s->buffer);
4079 static void __ftrace_dump(bool disable_tracing)
4081 static raw_spinlock_t ftrace_dump_lock =
4082 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
4083 /* use static because iter can be a bit big for the stack */
4084 static struct trace_iterator iter;
4085 unsigned int old_userobj;
4086 static int dump_ran;
4087 unsigned long flags;
4091 local_irq_save(flags);
4092 __raw_spin_lock(&ftrace_dump_lock);
4100 if (disable_tracing)
4103 for_each_tracing_cpu(cpu) {
4104 atomic_inc(&global_trace.data[cpu]->disabled);
4107 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4109 /* don't look at user memory in panic mode */
4110 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4112 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4114 /* Simulate the iterator */
4115 iter.tr = &global_trace;
4116 iter.trace = current_trace;
4117 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4120 * We need to stop all tracing on all CPUS to read the
4121 * the next buffer. This is a bit expensive, but is
4122 * not done often. We fill all what we can read,
4123 * and then release the locks again.
4126 while (!trace_empty(&iter)) {
4129 printk(KERN_TRACE "---------------------------------\n");
4133 /* reset all but tr, trace, and overruns */
4134 memset(&iter.seq, 0,
4135 sizeof(struct trace_iterator) -
4136 offsetof(struct trace_iterator, seq));
4137 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4140 if (find_next_entry_inc(&iter) != NULL) {
4143 ret = print_trace_line(&iter);
4144 if (ret != TRACE_TYPE_NO_CONSUME)
4145 trace_consume(&iter);
4148 trace_printk_seq(&iter.seq);
4152 printk(KERN_TRACE " (ftrace buffer empty)\n");
4154 printk(KERN_TRACE "---------------------------------\n");
4156 /* Re-enable tracing if requested */
4157 if (!disable_tracing) {
4158 trace_flags |= old_userobj;
4160 for_each_tracing_cpu(cpu) {
4161 atomic_dec(&global_trace.data[cpu]->disabled);
4167 __raw_spin_unlock(&ftrace_dump_lock);
4168 local_irq_restore(flags);
4171 /* By default: disable tracing after the dump */
4172 void ftrace_dump(void)
4174 __ftrace_dump(true);
4177 __init static int tracer_alloc_buffers(void)
4183 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4186 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4187 goto out_free_buffer_mask;
4189 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4190 goto out_free_tracing_cpumask;
4192 /* To save memory, keep the ring buffer size to its minimum */
4193 if (ring_buffer_expanded)
4194 ring_buf_size = trace_buf_size;
4198 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4199 cpumask_copy(tracing_cpumask, cpu_all_mask);
4200 cpumask_clear(tracing_reader_cpumask);
4202 /* TODO: make the number of buffers hot pluggable with CPUS */
4203 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4204 TRACE_BUFFER_FLAGS);
4205 if (!global_trace.buffer) {
4206 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4208 goto out_free_cpumask;
4210 global_trace.entries = ring_buffer_size(global_trace.buffer);
4213 #ifdef CONFIG_TRACER_MAX_TRACE
4214 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4215 TRACE_BUFFER_FLAGS);
4216 if (!max_tr.buffer) {
4217 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4219 ring_buffer_free(global_trace.buffer);
4220 goto out_free_cpumask;
4222 max_tr.entries = ring_buffer_size(max_tr.buffer);
4223 WARN_ON(max_tr.entries != global_trace.entries);
4226 /* Allocate the first page for all buffers */
4227 for_each_tracing_cpu(i) {
4228 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4229 max_tr.data[i] = &per_cpu(max_data, i);
4232 trace_init_cmdlines();
4234 register_tracer(&nop_trace);
4235 current_trace = &nop_trace;
4236 #ifdef CONFIG_BOOT_TRACER
4237 register_tracer(&boot_tracer);
4239 /* All seems OK, enable tracing */
4240 tracing_disabled = 0;
4242 atomic_notifier_chain_register(&panic_notifier_list,
4243 &trace_panic_notifier);
4245 register_die_notifier(&trace_die_notifier);
4250 free_cpumask_var(tracing_reader_cpumask);
4251 out_free_tracing_cpumask:
4252 free_cpumask_var(tracing_cpumask);
4253 out_free_buffer_mask:
4254 free_cpumask_var(tracing_buffer_mask);
4259 __init static int clear_boot_tracer(void)
4262 * The default tracer at boot buffer is an init section.
4263 * This function is called in lateinit. If we did not
4264 * find the boot tracer, then clear it out, to prevent
4265 * later registration from accessing the buffer that is
4266 * about to be freed.
4268 if (!default_bootup_tracer)
4271 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4272 default_bootup_tracer);
4273 default_bootup_tracer = NULL;
4278 early_initcall(tracer_alloc_buffers);
4279 fs_initcall(tracer_init_debugfs);
4280 late_initcall(clear_boot_tracer);