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 __update_max_tr(tr, tsk, cpu);
458 __raw_spin_unlock(&ftrace_max_lock);
462 * update_max_tr_single - only copy one trace over, and reset the rest
464 * @tsk - task with the latency
465 * @cpu - the cpu of the buffer to copy.
467 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
470 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
474 if (trace_stop_count)
477 WARN_ON_ONCE(!irqs_disabled());
478 __raw_spin_lock(&ftrace_max_lock);
480 ftrace_disable_cpu();
482 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
486 WARN_ON_ONCE(ret && ret != -EAGAIN);
488 __update_max_tr(tr, tsk, cpu);
489 __raw_spin_unlock(&ftrace_max_lock);
491 #endif /* CONFIG_TRACER_MAX_TRACE */
494 * register_tracer - register a tracer with the ftrace system.
495 * @type - the plugin for the tracer
497 * Register a new plugin tracer.
499 int register_tracer(struct tracer *type)
500 __releases(kernel_lock)
501 __acquires(kernel_lock)
508 pr_info("Tracer must have a name\n");
513 * When this gets called we hold the BKL which means that
514 * preemption is disabled. Various trace selftests however
515 * need to disable and enable preemption for successful tests.
516 * So we drop the BKL here and grab it after the tests again.
519 mutex_lock(&trace_types_lock);
521 tracing_selftest_running = true;
523 for (t = trace_types; t; t = t->next) {
524 if (strcmp(type->name, t->name) == 0) {
526 pr_info("Trace %s already registered\n",
534 type->set_flag = &dummy_set_flag;
536 type->flags = &dummy_tracer_flags;
538 if (!type->flags->opts)
539 type->flags->opts = dummy_tracer_opt;
540 if (!type->wait_pipe)
541 type->wait_pipe = default_wait_pipe;
544 #ifdef CONFIG_FTRACE_STARTUP_TEST
545 if (type->selftest && !tracing_selftest_disabled) {
546 struct tracer *saved_tracer = current_trace;
547 struct trace_array *tr = &global_trace;
550 * Run a selftest on this tracer.
551 * Here we reset the trace buffer, and set the current
552 * tracer to be this tracer. The tracer can then run some
553 * internal tracing to verify that everything is in order.
554 * If we fail, we do not register this tracer.
556 tracing_reset_online_cpus(tr);
558 current_trace = type;
559 /* the test is responsible for initializing and enabling */
560 pr_info("Testing tracer %s: ", type->name);
561 ret = type->selftest(type, tr);
562 /* the test is responsible for resetting too */
563 current_trace = saved_tracer;
565 printk(KERN_CONT "FAILED!\n");
568 /* Only reset on passing, to avoid touching corrupted buffers */
569 tracing_reset_online_cpus(tr);
571 printk(KERN_CONT "PASSED\n");
575 type->next = trace_types;
577 len = strlen(type->name);
578 if (len > max_tracer_type_len)
579 max_tracer_type_len = len;
582 tracing_selftest_running = false;
583 mutex_unlock(&trace_types_lock);
585 if (ret || !default_bootup_tracer)
588 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
591 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
592 /* Do we want this tracer to start on bootup? */
593 tracing_set_tracer(type->name);
594 default_bootup_tracer = NULL;
595 /* disable other selftests, since this will break it. */
596 tracing_selftest_disabled = 1;
597 #ifdef CONFIG_FTRACE_STARTUP_TEST
598 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
607 void unregister_tracer(struct tracer *type)
612 mutex_lock(&trace_types_lock);
613 for (t = &trace_types; *t; t = &(*t)->next) {
617 pr_info("Trace %s not registered\n", type->name);
623 if (type == current_trace && tracer_enabled) {
626 if (current_trace->stop)
627 current_trace->stop(&global_trace);
628 current_trace = &nop_trace;
631 if (strlen(type->name) != max_tracer_type_len)
634 max_tracer_type_len = 0;
635 for (t = &trace_types; *t; t = &(*t)->next) {
636 len = strlen((*t)->name);
637 if (len > max_tracer_type_len)
638 max_tracer_type_len = len;
641 mutex_unlock(&trace_types_lock);
644 void tracing_reset(struct trace_array *tr, int cpu)
646 ftrace_disable_cpu();
647 ring_buffer_reset_cpu(tr->buffer, cpu);
651 void tracing_reset_online_cpus(struct trace_array *tr)
653 struct ring_buffer *buffer = tr->buffer;
656 ring_buffer_record_disable(buffer);
658 /* Make sure all commits have finished */
661 tr->time_start = ftrace_now(tr->cpu);
663 for_each_online_cpu(cpu)
664 tracing_reset(tr, cpu);
666 ring_buffer_record_enable(buffer);
669 void tracing_reset_current(int cpu)
671 tracing_reset(&global_trace, cpu);
674 void tracing_reset_current_online_cpus(void)
676 tracing_reset_online_cpus(&global_trace);
679 #define SAVED_CMDLINES 128
680 #define NO_CMDLINE_MAP UINT_MAX
681 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
682 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
683 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
684 static int cmdline_idx;
685 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
687 /* temporary disable recording */
688 static atomic_t trace_record_cmdline_disabled __read_mostly;
690 static void trace_init_cmdlines(void)
692 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
693 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
698 * ftrace_off_permanent - disable all ftrace code permanently
700 * This should only be called when a serious anomally has
701 * been detected. This will turn off the function tracing,
702 * ring buffers, and other tracing utilites. It takes no
703 * locks and can be called from any context.
705 void ftrace_off_permanent(void)
707 tracing_disabled = 1;
709 tracing_off_permanent();
713 * tracing_start - quick start of the tracer
715 * If tracing is enabled but was stopped by tracing_stop,
716 * this will start the tracer back up.
718 void tracing_start(void)
720 struct ring_buffer *buffer;
723 if (tracing_disabled)
726 spin_lock_irqsave(&tracing_start_lock, flags);
727 if (--trace_stop_count) {
728 if (trace_stop_count < 0) {
729 /* Someone screwed up their debugging */
731 trace_stop_count = 0;
737 buffer = global_trace.buffer;
739 ring_buffer_record_enable(buffer);
741 buffer = max_tr.buffer;
743 ring_buffer_record_enable(buffer);
747 spin_unlock_irqrestore(&tracing_start_lock, flags);
751 * tracing_stop - quick stop of the tracer
753 * Light weight way to stop tracing. Use in conjunction with
756 void tracing_stop(void)
758 struct ring_buffer *buffer;
762 spin_lock_irqsave(&tracing_start_lock, flags);
763 if (trace_stop_count++)
766 buffer = global_trace.buffer;
768 ring_buffer_record_disable(buffer);
770 buffer = max_tr.buffer;
772 ring_buffer_record_disable(buffer);
775 spin_unlock_irqrestore(&tracing_start_lock, flags);
778 void trace_stop_cmdline_recording(void);
780 static void trace_save_cmdline(struct task_struct *tsk)
784 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
788 * It's not the end of the world if we don't get
789 * the lock, but we also don't want to spin
790 * nor do we want to disable interrupts,
791 * so if we miss here, then better luck next time.
793 if (!__raw_spin_trylock(&trace_cmdline_lock))
796 idx = map_pid_to_cmdline[tsk->pid];
797 if (idx == NO_CMDLINE_MAP) {
798 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
801 * Check whether the cmdline buffer at idx has a pid
802 * mapped. We are going to overwrite that entry so we
803 * need to clear the map_pid_to_cmdline. Otherwise we
804 * would read the new comm for the old pid.
806 pid = map_cmdline_to_pid[idx];
807 if (pid != NO_CMDLINE_MAP)
808 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
810 map_cmdline_to_pid[idx] = tsk->pid;
811 map_pid_to_cmdline[tsk->pid] = idx;
816 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
818 __raw_spin_unlock(&trace_cmdline_lock);
821 void trace_find_cmdline(int pid, char comm[])
826 strcpy(comm, "<idle>");
830 if (pid > PID_MAX_DEFAULT) {
831 strcpy(comm, "<...>");
836 __raw_spin_lock(&trace_cmdline_lock);
837 map = map_pid_to_cmdline[pid];
838 if (map != NO_CMDLINE_MAP)
839 strcpy(comm, saved_cmdlines[map]);
841 strcpy(comm, "<...>");
843 __raw_spin_unlock(&trace_cmdline_lock);
847 void tracing_record_cmdline(struct task_struct *tsk)
849 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
853 trace_save_cmdline(tsk);
857 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
860 struct task_struct *tsk = current;
862 entry->preempt_count = pc & 0xff;
863 entry->pid = (tsk) ? tsk->pid : 0;
864 entry->tgid = (tsk) ? tsk->tgid : 0;
866 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
867 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
869 TRACE_FLAG_IRQS_NOSUPPORT |
871 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
872 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
873 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
875 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
877 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
880 unsigned long flags, int pc)
882 struct ring_buffer_event *event;
884 event = ring_buffer_lock_reserve(tr->buffer, len);
886 struct trace_entry *ent = ring_buffer_event_data(event);
888 tracing_generic_entry_update(ent, flags, pc);
895 static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
896 struct ring_buffer_event *event,
897 unsigned long flags, int pc,
900 ring_buffer_unlock_commit(tr->buffer, event);
902 ftrace_trace_stack(tr, flags, 6, pc);
903 ftrace_trace_userstack(tr, flags, pc);
909 void trace_buffer_unlock_commit(struct trace_array *tr,
910 struct ring_buffer_event *event,
911 unsigned long flags, int pc)
913 __trace_buffer_unlock_commit(tr, event, flags, pc, 1);
916 struct ring_buffer_event *
917 trace_current_buffer_lock_reserve(int type, unsigned long len,
918 unsigned long flags, int pc)
920 return trace_buffer_lock_reserve(&global_trace,
921 type, len, flags, pc);
923 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
925 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
926 unsigned long flags, int pc)
928 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
930 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
932 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
933 unsigned long flags, int pc)
935 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
937 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
939 void trace_current_buffer_discard_commit(struct ring_buffer_event *event)
941 ring_buffer_discard_commit(global_trace.buffer, event);
943 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
946 trace_function(struct trace_array *tr,
947 unsigned long ip, unsigned long parent_ip, unsigned long flags,
950 struct ftrace_event_call *call = &event_function;
951 struct ring_buffer_event *event;
952 struct ftrace_entry *entry;
954 /* If we are reading the ring buffer, don't trace */
955 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
958 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
962 entry = ring_buffer_event_data(event);
964 entry->parent_ip = parent_ip;
966 if (!filter_check_discard(call, entry, tr->buffer, event))
967 ring_buffer_unlock_commit(tr->buffer, event);
971 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
972 unsigned long ip, unsigned long parent_ip, unsigned long flags,
975 if (likely(!atomic_read(&data->disabled)))
976 trace_function(tr, ip, parent_ip, flags, pc);
979 #ifdef CONFIG_STACKTRACE
980 static void __ftrace_trace_stack(struct trace_array *tr,
984 struct ftrace_event_call *call = &event_kernel_stack;
985 struct ring_buffer_event *event;
986 struct stack_entry *entry;
987 struct stack_trace trace;
989 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
990 sizeof(*entry), flags, pc);
993 entry = ring_buffer_event_data(event);
994 memset(&entry->caller, 0, sizeof(entry->caller));
996 trace.nr_entries = 0;
997 trace.max_entries = FTRACE_STACK_ENTRIES;
999 trace.entries = entry->caller;
1001 save_stack_trace(&trace);
1002 if (!filter_check_discard(call, entry, tr->buffer, event))
1003 ring_buffer_unlock_commit(tr->buffer, event);
1006 void ftrace_trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1009 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1012 __ftrace_trace_stack(tr, flags, skip, pc);
1015 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1018 __ftrace_trace_stack(tr, flags, skip, pc);
1021 void ftrace_trace_userstack(struct trace_array *tr, unsigned long flags, int pc)
1023 struct ftrace_event_call *call = &event_user_stack;
1024 struct ring_buffer_event *event;
1025 struct userstack_entry *entry;
1026 struct stack_trace trace;
1028 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1031 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1032 sizeof(*entry), flags, pc);
1035 entry = ring_buffer_event_data(event);
1037 memset(&entry->caller, 0, sizeof(entry->caller));
1039 trace.nr_entries = 0;
1040 trace.max_entries = FTRACE_STACK_ENTRIES;
1042 trace.entries = entry->caller;
1044 save_stack_trace_user(&trace);
1045 if (!filter_check_discard(call, entry, tr->buffer, event))
1046 ring_buffer_unlock_commit(tr->buffer, event);
1050 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1052 ftrace_trace_userstack(tr, flags, preempt_count());
1056 #endif /* CONFIG_STACKTRACE */
1059 ftrace_trace_special(void *__tr,
1060 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1063 struct ring_buffer_event *event;
1064 struct trace_array *tr = __tr;
1065 struct special_entry *entry;
1067 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1068 sizeof(*entry), 0, pc);
1071 entry = ring_buffer_event_data(event);
1075 trace_buffer_unlock_commit(tr, event, 0, pc);
1079 __trace_special(void *__tr, void *__data,
1080 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1082 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1086 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1088 struct trace_array *tr = &global_trace;
1089 struct trace_array_cpu *data;
1090 unsigned long flags;
1094 if (tracing_disabled)
1097 pc = preempt_count();
1098 local_irq_save(flags);
1099 cpu = raw_smp_processor_id();
1100 data = tr->data[cpu];
1102 if (likely(atomic_inc_return(&data->disabled) == 1))
1103 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1105 atomic_dec(&data->disabled);
1106 local_irq_restore(flags);
1110 * trace_vbprintk - write binary msg to tracing buffer
1113 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1115 static raw_spinlock_t trace_buf_lock =
1116 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1117 static u32 trace_buf[TRACE_BUF_SIZE];
1119 struct ftrace_event_call *call = &event_bprint;
1120 struct ring_buffer_event *event;
1121 struct trace_array *tr = &global_trace;
1122 struct trace_array_cpu *data;
1123 struct bprint_entry *entry;
1124 unsigned long flags;
1127 int cpu, len = 0, size, pc;
1129 if (unlikely(tracing_selftest_running || tracing_disabled))
1132 /* Don't pollute graph traces with trace_vprintk internals */
1133 pause_graph_tracing();
1135 pc = preempt_count();
1136 resched = ftrace_preempt_disable();
1137 cpu = raw_smp_processor_id();
1138 data = tr->data[cpu];
1140 disable = atomic_inc_return(&data->disabled);
1141 if (unlikely(disable != 1))
1144 /* Lockdep uses trace_printk for lock tracing */
1145 local_irq_save(flags);
1146 __raw_spin_lock(&trace_buf_lock);
1147 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1149 if (len > TRACE_BUF_SIZE || len < 0)
1152 size = sizeof(*entry) + sizeof(u32) * len;
1153 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1156 entry = ring_buffer_event_data(event);
1160 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1161 if (!filter_check_discard(call, entry, tr->buffer, event))
1162 ring_buffer_unlock_commit(tr->buffer, event);
1165 __raw_spin_unlock(&trace_buf_lock);
1166 local_irq_restore(flags);
1169 atomic_dec_return(&data->disabled);
1170 ftrace_preempt_enable(resched);
1171 unpause_graph_tracing();
1175 EXPORT_SYMBOL_GPL(trace_vbprintk);
1177 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1179 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1180 static char trace_buf[TRACE_BUF_SIZE];
1182 struct ftrace_event_call *call = &event_print;
1183 struct ring_buffer_event *event;
1184 struct trace_array *tr = &global_trace;
1185 struct trace_array_cpu *data;
1186 int cpu, len = 0, size, pc;
1187 struct print_entry *entry;
1188 unsigned long irq_flags;
1191 if (tracing_disabled || tracing_selftest_running)
1194 pc = preempt_count();
1195 preempt_disable_notrace();
1196 cpu = raw_smp_processor_id();
1197 data = tr->data[cpu];
1199 disable = atomic_inc_return(&data->disabled);
1200 if (unlikely(disable != 1))
1203 pause_graph_tracing();
1204 raw_local_irq_save(irq_flags);
1205 __raw_spin_lock(&trace_buf_lock);
1206 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1208 len = min(len, TRACE_BUF_SIZE-1);
1211 size = sizeof(*entry) + len + 1;
1212 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1215 entry = ring_buffer_event_data(event);
1218 memcpy(&entry->buf, trace_buf, len);
1219 entry->buf[len] = 0;
1220 if (!filter_check_discard(call, entry, tr->buffer, event))
1221 ring_buffer_unlock_commit(tr->buffer, event);
1224 __raw_spin_unlock(&trace_buf_lock);
1225 raw_local_irq_restore(irq_flags);
1226 unpause_graph_tracing();
1228 atomic_dec_return(&data->disabled);
1229 preempt_enable_notrace();
1233 EXPORT_SYMBOL_GPL(trace_vprintk);
1235 enum trace_file_type {
1236 TRACE_FILE_LAT_FMT = 1,
1237 TRACE_FILE_ANNOTATE = 2,
1240 static void trace_iterator_increment(struct trace_iterator *iter)
1242 /* Don't allow ftrace to trace into the ring buffers */
1243 ftrace_disable_cpu();
1246 if (iter->buffer_iter[iter->cpu])
1247 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1249 ftrace_enable_cpu();
1252 static struct trace_entry *
1253 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1255 struct ring_buffer_event *event;
1256 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1258 /* Don't allow ftrace to trace into the ring buffers */
1259 ftrace_disable_cpu();
1262 event = ring_buffer_iter_peek(buf_iter, ts);
1264 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1266 ftrace_enable_cpu();
1268 return event ? ring_buffer_event_data(event) : NULL;
1271 static struct trace_entry *
1272 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1274 struct ring_buffer *buffer = iter->tr->buffer;
1275 struct trace_entry *ent, *next = NULL;
1276 int cpu_file = iter->cpu_file;
1277 u64 next_ts = 0, ts;
1282 * If we are in a per_cpu trace file, don't bother by iterating over
1283 * all cpu and peek directly.
1285 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1286 if (ring_buffer_empty_cpu(buffer, cpu_file))
1288 ent = peek_next_entry(iter, cpu_file, ent_ts);
1290 *ent_cpu = cpu_file;
1295 for_each_tracing_cpu(cpu) {
1297 if (ring_buffer_empty_cpu(buffer, cpu))
1300 ent = peek_next_entry(iter, cpu, &ts);
1303 * Pick the entry with the smallest timestamp:
1305 if (ent && (!next || ts < next_ts)) {
1313 *ent_cpu = next_cpu;
1321 /* Find the next real entry, without updating the iterator itself */
1322 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1323 int *ent_cpu, u64 *ent_ts)
1325 return __find_next_entry(iter, ent_cpu, ent_ts);
1328 /* Find the next real entry, and increment the iterator to the next entry */
1329 static void *find_next_entry_inc(struct trace_iterator *iter)
1331 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1334 trace_iterator_increment(iter);
1336 return iter->ent ? iter : NULL;
1339 static void trace_consume(struct trace_iterator *iter)
1341 /* Don't allow ftrace to trace into the ring buffers */
1342 ftrace_disable_cpu();
1343 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1344 ftrace_enable_cpu();
1347 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1349 struct trace_iterator *iter = m->private;
1355 /* can't go backwards */
1360 ent = find_next_entry_inc(iter);
1364 while (ent && iter->idx < i)
1365 ent = find_next_entry_inc(iter);
1372 static void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1374 struct trace_array *tr = iter->tr;
1375 struct ring_buffer_event *event;
1376 struct ring_buffer_iter *buf_iter;
1377 unsigned long entries = 0;
1380 tr->data[cpu]->skipped_entries = 0;
1382 if (!iter->buffer_iter[cpu])
1385 buf_iter = iter->buffer_iter[cpu];
1386 ring_buffer_iter_reset(buf_iter);
1389 * We could have the case with the max latency tracers
1390 * that a reset never took place on a cpu. This is evident
1391 * by the timestamp being before the start of the buffer.
1393 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1394 if (ts >= iter->tr->time_start)
1397 ring_buffer_read(buf_iter, NULL);
1400 tr->data[cpu]->skipped_entries = entries;
1404 * No necessary locking here. The worst thing which can
1405 * happen is loosing events consumed at the same time
1406 * by a trace_pipe reader.
1407 * Other than that, we don't risk to crash the ring buffer
1408 * because it serializes the readers.
1410 * The current tracer is copied to avoid a global locking
1413 static void *s_start(struct seq_file *m, loff_t *pos)
1415 struct trace_iterator *iter = m->private;
1416 static struct tracer *old_tracer;
1417 int cpu_file = iter->cpu_file;
1422 /* copy the tracer to avoid using a global lock all around */
1423 mutex_lock(&trace_types_lock);
1424 if (unlikely(old_tracer != current_trace && current_trace)) {
1425 old_tracer = current_trace;
1426 *iter->trace = *current_trace;
1428 mutex_unlock(&trace_types_lock);
1430 atomic_inc(&trace_record_cmdline_disabled);
1432 if (*pos != iter->pos) {
1437 ftrace_disable_cpu();
1439 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1440 for_each_tracing_cpu(cpu)
1441 tracing_iter_reset(iter, cpu);
1443 tracing_iter_reset(iter, cpu_file);
1445 ftrace_enable_cpu();
1447 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1452 p = s_next(m, p, &l);
1455 trace_event_read_lock();
1459 static void s_stop(struct seq_file *m, void *p)
1461 atomic_dec(&trace_record_cmdline_disabled);
1462 trace_event_read_unlock();
1465 static void print_lat_help_header(struct seq_file *m)
1467 seq_puts(m, "# _------=> CPU# \n");
1468 seq_puts(m, "# / _-----=> irqs-off \n");
1469 seq_puts(m, "# | / _----=> need-resched \n");
1470 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1471 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1472 seq_puts(m, "# |||| / \n");
1473 seq_puts(m, "# ||||| delay \n");
1474 seq_puts(m, "# cmd pid ||||| time | caller \n");
1475 seq_puts(m, "# \\ / ||||| \\ | / \n");
1478 static void print_func_help_header(struct seq_file *m)
1480 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1481 seq_puts(m, "# | | | | |\n");
1486 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1488 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1489 struct trace_array *tr = iter->tr;
1490 struct trace_array_cpu *data = tr->data[tr->cpu];
1491 struct tracer *type = current_trace;
1492 unsigned long entries = 0;
1493 unsigned long total = 0;
1494 unsigned long count;
1495 const char *name = "preemption";
1502 for_each_tracing_cpu(cpu) {
1503 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1505 * If this buffer has skipped entries, then we hold all
1506 * entries for the trace and we need to ignore the
1507 * ones before the time stamp.
1509 if (tr->data[cpu]->skipped_entries) {
1510 count -= tr->data[cpu]->skipped_entries;
1511 /* total is the same as the entries */
1515 ring_buffer_overrun_cpu(tr->buffer, cpu);
1519 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1521 seq_puts(m, "# -----------------------------------"
1522 "---------------------------------\n");
1523 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1524 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1525 nsecs_to_usecs(data->saved_latency),
1529 #if defined(CONFIG_PREEMPT_NONE)
1531 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1533 #elif defined(CONFIG_PREEMPT)
1538 /* These are reserved for later use */
1541 seq_printf(m, " #P:%d)\n", num_online_cpus());
1545 seq_puts(m, "# -----------------\n");
1546 seq_printf(m, "# | task: %.16s-%d "
1547 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1548 data->comm, data->pid, data->uid, data->nice,
1549 data->policy, data->rt_priority);
1550 seq_puts(m, "# -----------------\n");
1552 if (data->critical_start) {
1553 seq_puts(m, "# => started at: ");
1554 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1555 trace_print_seq(m, &iter->seq);
1556 seq_puts(m, "\n# => ended at: ");
1557 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1558 trace_print_seq(m, &iter->seq);
1559 seq_puts(m, "\n#\n");
1565 static void test_cpu_buff_start(struct trace_iterator *iter)
1567 struct trace_seq *s = &iter->seq;
1569 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1572 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1575 if (cpumask_test_cpu(iter->cpu, iter->started))
1578 if (iter->tr->data[iter->cpu]->skipped_entries)
1581 cpumask_set_cpu(iter->cpu, iter->started);
1583 /* Don't print started cpu buffer for the first entry of the trace */
1585 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1589 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1591 struct trace_seq *s = &iter->seq;
1592 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1593 struct trace_entry *entry;
1594 struct trace_event *event;
1598 test_cpu_buff_start(iter);
1600 event = ftrace_find_event(entry->type);
1602 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1603 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1604 if (!trace_print_lat_context(iter))
1607 if (!trace_print_context(iter))
1613 return event->trace(iter, sym_flags);
1615 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1618 return TRACE_TYPE_HANDLED;
1620 return TRACE_TYPE_PARTIAL_LINE;
1623 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1625 struct trace_seq *s = &iter->seq;
1626 struct trace_entry *entry;
1627 struct trace_event *event;
1631 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1632 if (!trace_seq_printf(s, "%d %d %llu ",
1633 entry->pid, iter->cpu, iter->ts))
1637 event = ftrace_find_event(entry->type);
1639 return event->raw(iter, 0);
1641 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1644 return TRACE_TYPE_HANDLED;
1646 return TRACE_TYPE_PARTIAL_LINE;
1649 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1651 struct trace_seq *s = &iter->seq;
1652 unsigned char newline = '\n';
1653 struct trace_entry *entry;
1654 struct trace_event *event;
1658 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1659 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1660 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1661 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1664 event = ftrace_find_event(entry->type);
1666 enum print_line_t ret = event->hex(iter, 0);
1667 if (ret != TRACE_TYPE_HANDLED)
1671 SEQ_PUT_FIELD_RET(s, newline);
1673 return TRACE_TYPE_HANDLED;
1676 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1678 struct trace_seq *s = &iter->seq;
1679 struct trace_entry *entry;
1680 struct trace_event *event;
1684 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1685 SEQ_PUT_FIELD_RET(s, entry->pid);
1686 SEQ_PUT_FIELD_RET(s, iter->cpu);
1687 SEQ_PUT_FIELD_RET(s, iter->ts);
1690 event = ftrace_find_event(entry->type);
1691 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1694 static int trace_empty(struct trace_iterator *iter)
1698 /* If we are looking at one CPU buffer, only check that one */
1699 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1700 cpu = iter->cpu_file;
1701 if (iter->buffer_iter[cpu]) {
1702 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1705 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1711 for_each_tracing_cpu(cpu) {
1712 if (iter->buffer_iter[cpu]) {
1713 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1716 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1724 /* Called with trace_event_read_lock() held. */
1725 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1727 enum print_line_t ret;
1729 if (iter->trace && iter->trace->print_line) {
1730 ret = iter->trace->print_line(iter);
1731 if (ret != TRACE_TYPE_UNHANDLED)
1735 if (iter->ent->type == TRACE_BPRINT &&
1736 trace_flags & TRACE_ITER_PRINTK &&
1737 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1738 return trace_print_bprintk_msg_only(iter);
1740 if (iter->ent->type == TRACE_PRINT &&
1741 trace_flags & TRACE_ITER_PRINTK &&
1742 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1743 return trace_print_printk_msg_only(iter);
1745 if (trace_flags & TRACE_ITER_BIN)
1746 return print_bin_fmt(iter);
1748 if (trace_flags & TRACE_ITER_HEX)
1749 return print_hex_fmt(iter);
1751 if (trace_flags & TRACE_ITER_RAW)
1752 return print_raw_fmt(iter);
1754 return print_trace_fmt(iter);
1757 static int s_show(struct seq_file *m, void *v)
1759 struct trace_iterator *iter = v;
1761 if (iter->ent == NULL) {
1763 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1766 if (iter->trace && iter->trace->print_header)
1767 iter->trace->print_header(m);
1768 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1769 /* print nothing if the buffers are empty */
1770 if (trace_empty(iter))
1772 print_trace_header(m, iter);
1773 if (!(trace_flags & TRACE_ITER_VERBOSE))
1774 print_lat_help_header(m);
1776 if (!(trace_flags & TRACE_ITER_VERBOSE))
1777 print_func_help_header(m);
1780 print_trace_line(iter);
1781 trace_print_seq(m, &iter->seq);
1787 static struct seq_operations tracer_seq_ops = {
1794 static struct trace_iterator *
1795 __tracing_open(struct inode *inode, struct file *file)
1797 long cpu_file = (long) inode->i_private;
1798 void *fail_ret = ERR_PTR(-ENOMEM);
1799 struct trace_iterator *iter;
1803 if (tracing_disabled)
1804 return ERR_PTR(-ENODEV);
1806 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1808 return ERR_PTR(-ENOMEM);
1811 * We make a copy of the current tracer to avoid concurrent
1812 * changes on it while we are reading.
1814 mutex_lock(&trace_types_lock);
1815 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1820 *iter->trace = *current_trace;
1822 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1825 cpumask_clear(iter->started);
1827 if (current_trace && current_trace->print_max)
1830 iter->tr = &global_trace;
1832 mutex_init(&iter->mutex);
1833 iter->cpu_file = cpu_file;
1835 /* Notify the tracer early; before we stop tracing. */
1836 if (iter->trace && iter->trace->open)
1837 iter->trace->open(iter);
1839 /* Annotate start of buffers if we had overruns */
1840 if (ring_buffer_overruns(iter->tr->buffer))
1841 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1843 /* stop the trace while dumping */
1846 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1847 for_each_tracing_cpu(cpu) {
1849 iter->buffer_iter[cpu] =
1850 ring_buffer_read_start(iter->tr->buffer, cpu);
1851 tracing_iter_reset(iter, cpu);
1854 cpu = iter->cpu_file;
1855 iter->buffer_iter[cpu] =
1856 ring_buffer_read_start(iter->tr->buffer, cpu);
1857 tracing_iter_reset(iter, cpu);
1860 ret = seq_open(file, &tracer_seq_ops);
1862 fail_ret = ERR_PTR(ret);
1866 m = file->private_data;
1869 mutex_unlock(&trace_types_lock);
1874 for_each_tracing_cpu(cpu) {
1875 if (iter->buffer_iter[cpu])
1876 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1878 free_cpumask_var(iter->started);
1881 mutex_unlock(&trace_types_lock);
1888 int tracing_open_generic(struct inode *inode, struct file *filp)
1890 if (tracing_disabled)
1893 filp->private_data = inode->i_private;
1897 static int tracing_release(struct inode *inode, struct file *file)
1899 struct seq_file *m = (struct seq_file *)file->private_data;
1900 struct trace_iterator *iter;
1903 if (!(file->f_mode & FMODE_READ))
1908 mutex_lock(&trace_types_lock);
1909 for_each_tracing_cpu(cpu) {
1910 if (iter->buffer_iter[cpu])
1911 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1914 if (iter->trace && iter->trace->close)
1915 iter->trace->close(iter);
1917 /* reenable tracing if it was previously enabled */
1919 mutex_unlock(&trace_types_lock);
1921 seq_release(inode, file);
1922 mutex_destroy(&iter->mutex);
1923 free_cpumask_var(iter->started);
1929 static int tracing_open(struct inode *inode, struct file *file)
1931 struct trace_iterator *iter;
1934 /* If this file was open for write, then erase contents */
1935 if ((file->f_mode & FMODE_WRITE) &&
1936 (file->f_flags & O_TRUNC)) {
1937 long cpu = (long) inode->i_private;
1939 if (cpu == TRACE_PIPE_ALL_CPU)
1940 tracing_reset_online_cpus(&global_trace);
1942 tracing_reset(&global_trace, cpu);
1945 if (file->f_mode & FMODE_READ) {
1946 iter = __tracing_open(inode, file);
1948 ret = PTR_ERR(iter);
1949 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1950 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1956 t_next(struct seq_file *m, void *v, loff_t *pos)
1958 struct tracer *t = v;
1968 static void *t_start(struct seq_file *m, loff_t *pos)
1973 mutex_lock(&trace_types_lock);
1974 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
1980 static void t_stop(struct seq_file *m, void *p)
1982 mutex_unlock(&trace_types_lock);
1985 static int t_show(struct seq_file *m, void *v)
1987 struct tracer *t = v;
1992 seq_printf(m, "%s", t->name);
2001 static struct seq_operations show_traces_seq_ops = {
2008 static int show_traces_open(struct inode *inode, struct file *file)
2010 if (tracing_disabled)
2013 return seq_open(file, &show_traces_seq_ops);
2017 tracing_write_stub(struct file *filp, const char __user *ubuf,
2018 size_t count, loff_t *ppos)
2023 static const struct file_operations tracing_fops = {
2024 .open = tracing_open,
2026 .write = tracing_write_stub,
2027 .llseek = seq_lseek,
2028 .release = tracing_release,
2031 static const struct file_operations show_traces_fops = {
2032 .open = show_traces_open,
2034 .release = seq_release,
2038 * Only trace on a CPU if the bitmask is set:
2040 static cpumask_var_t tracing_cpumask;
2043 * The tracer itself will not take this lock, but still we want
2044 * to provide a consistent cpumask to user-space:
2046 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2049 * Temporary storage for the character representation of the
2050 * CPU bitmask (and one more byte for the newline):
2052 static char mask_str[NR_CPUS + 1];
2055 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2056 size_t count, loff_t *ppos)
2060 mutex_lock(&tracing_cpumask_update_lock);
2062 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2063 if (count - len < 2) {
2067 len += sprintf(mask_str + len, "\n");
2068 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2071 mutex_unlock(&tracing_cpumask_update_lock);
2077 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2078 size_t count, loff_t *ppos)
2081 cpumask_var_t tracing_cpumask_new;
2083 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2086 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2090 mutex_lock(&tracing_cpumask_update_lock);
2092 local_irq_disable();
2093 __raw_spin_lock(&ftrace_max_lock);
2094 for_each_tracing_cpu(cpu) {
2096 * Increase/decrease the disabled counter if we are
2097 * about to flip a bit in the cpumask:
2099 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2100 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2101 atomic_inc(&global_trace.data[cpu]->disabled);
2103 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2104 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2105 atomic_dec(&global_trace.data[cpu]->disabled);
2108 __raw_spin_unlock(&ftrace_max_lock);
2111 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2113 mutex_unlock(&tracing_cpumask_update_lock);
2114 free_cpumask_var(tracing_cpumask_new);
2119 free_cpumask_var(tracing_cpumask_new);
2124 static const struct file_operations tracing_cpumask_fops = {
2125 .open = tracing_open_generic,
2126 .read = tracing_cpumask_read,
2127 .write = tracing_cpumask_write,
2131 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2132 size_t cnt, loff_t *ppos)
2134 struct tracer_opt *trace_opts;
2142 /* calculate max size */
2143 for (i = 0; trace_options[i]; i++) {
2144 len += strlen(trace_options[i]);
2145 len += 3; /* "no" and newline */
2148 mutex_lock(&trace_types_lock);
2149 tracer_flags = current_trace->flags->val;
2150 trace_opts = current_trace->flags->opts;
2153 * Increase the size with names of options specific
2154 * of the current tracer.
2156 for (i = 0; trace_opts[i].name; i++) {
2157 len += strlen(trace_opts[i].name);
2158 len += 3; /* "no" and newline */
2162 buf = kmalloc(len + 1, GFP_KERNEL);
2164 mutex_unlock(&trace_types_lock);
2168 for (i = 0; trace_options[i]; i++) {
2169 if (trace_flags & (1 << i))
2170 r += sprintf(buf + r, "%s\n", trace_options[i]);
2172 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2175 for (i = 0; trace_opts[i].name; i++) {
2176 if (tracer_flags & trace_opts[i].bit)
2177 r += sprintf(buf + r, "%s\n",
2178 trace_opts[i].name);
2180 r += sprintf(buf + r, "no%s\n",
2181 trace_opts[i].name);
2183 mutex_unlock(&trace_types_lock);
2185 WARN_ON(r >= len + 1);
2187 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2193 /* Try to assign a tracer specific option */
2194 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2196 struct tracer_flags *tracer_flags = trace->flags;
2197 struct tracer_opt *opts = NULL;
2201 for (i = 0; tracer_flags->opts[i].name; i++) {
2202 opts = &tracer_flags->opts[i];
2203 len = strlen(opts->name);
2205 if (strncmp(cmp, opts->name, len) == 0) {
2206 ret = trace->set_flag(tracer_flags->val,
2212 if (!tracer_flags->opts[i].name)
2215 /* Refused to handle */
2220 tracer_flags->val &= ~opts->bit;
2222 tracer_flags->val |= opts->bit;
2227 static void set_tracer_flags(unsigned int mask, int enabled)
2229 /* do nothing if flag is already set */
2230 if (!!(trace_flags & mask) == !!enabled)
2234 trace_flags |= mask;
2236 trace_flags &= ~mask;
2240 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2241 size_t cnt, loff_t *ppos)
2249 if (cnt >= sizeof(buf))
2252 if (copy_from_user(&buf, ubuf, cnt))
2257 if (strncmp(buf, "no", 2) == 0) {
2262 for (i = 0; trace_options[i]; i++) {
2263 int len = strlen(trace_options[i]);
2265 if (strncmp(cmp, trace_options[i], len) == 0) {
2266 set_tracer_flags(1 << i, !neg);
2271 /* If no option could be set, test the specific tracer options */
2272 if (!trace_options[i]) {
2273 mutex_lock(&trace_types_lock);
2274 ret = set_tracer_option(current_trace, cmp, neg);
2275 mutex_unlock(&trace_types_lock);
2285 static const struct file_operations tracing_iter_fops = {
2286 .open = tracing_open_generic,
2287 .read = tracing_trace_options_read,
2288 .write = tracing_trace_options_write,
2291 static const char readme_msg[] =
2292 "tracing mini-HOWTO:\n\n"
2293 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2294 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2295 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2296 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2298 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2299 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2301 "# cat /sys/kernel/debug/tracing/trace_options\n"
2302 "noprint-parent nosym-offset nosym-addr noverbose\n"
2303 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2304 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2305 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2306 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2310 tracing_readme_read(struct file *filp, char __user *ubuf,
2311 size_t cnt, loff_t *ppos)
2313 return simple_read_from_buffer(ubuf, cnt, ppos,
2314 readme_msg, strlen(readme_msg));
2317 static const struct file_operations tracing_readme_fops = {
2318 .open = tracing_open_generic,
2319 .read = tracing_readme_read,
2323 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2324 size_t cnt, loff_t *ppos)
2333 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2337 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2345 for (i = 0; i < SAVED_CMDLINES; i++) {
2348 pid = map_cmdline_to_pid[i];
2349 if (pid == -1 || pid == NO_CMDLINE_MAP)
2352 trace_find_cmdline(pid, buf_comm);
2353 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2358 len = simple_read_from_buffer(ubuf, cnt, ppos,
2367 static const struct file_operations tracing_saved_cmdlines_fops = {
2368 .open = tracing_open_generic,
2369 .read = tracing_saved_cmdlines_read,
2373 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2374 size_t cnt, loff_t *ppos)
2379 r = sprintf(buf, "%u\n", tracer_enabled);
2380 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2384 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2385 size_t cnt, loff_t *ppos)
2387 struct trace_array *tr = filp->private_data;
2392 if (cnt >= sizeof(buf))
2395 if (copy_from_user(&buf, ubuf, cnt))
2400 ret = strict_strtoul(buf, 10, &val);
2406 mutex_lock(&trace_types_lock);
2407 if (tracer_enabled ^ val) {
2410 if (current_trace->start)
2411 current_trace->start(tr);
2416 if (current_trace->stop)
2417 current_trace->stop(tr);
2420 mutex_unlock(&trace_types_lock);
2428 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2429 size_t cnt, loff_t *ppos)
2431 char buf[max_tracer_type_len+2];
2434 mutex_lock(&trace_types_lock);
2436 r = sprintf(buf, "%s\n", current_trace->name);
2438 r = sprintf(buf, "\n");
2439 mutex_unlock(&trace_types_lock);
2441 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2444 int tracer_init(struct tracer *t, struct trace_array *tr)
2446 tracing_reset_online_cpus(tr);
2450 static int tracing_resize_ring_buffer(unsigned long size)
2455 * If kernel or user changes the size of the ring buffer
2456 * we use the size that was given, and we can forget about
2457 * expanding it later.
2459 ring_buffer_expanded = 1;
2461 ret = ring_buffer_resize(global_trace.buffer, size);
2465 ret = ring_buffer_resize(max_tr.buffer, size);
2469 r = ring_buffer_resize(global_trace.buffer,
2470 global_trace.entries);
2473 * AARGH! We are left with different
2474 * size max buffer!!!!
2475 * The max buffer is our "snapshot" buffer.
2476 * When a tracer needs a snapshot (one of the
2477 * latency tracers), it swaps the max buffer
2478 * with the saved snap shot. We succeeded to
2479 * update the size of the main buffer, but failed to
2480 * update the size of the max buffer. But when we tried
2481 * to reset the main buffer to the original size, we
2482 * failed there too. This is very unlikely to
2483 * happen, but if it does, warn and kill all
2487 tracing_disabled = 1;
2492 global_trace.entries = size;
2498 * tracing_update_buffers - used by tracing facility to expand ring buffers
2500 * To save on memory when the tracing is never used on a system with it
2501 * configured in. The ring buffers are set to a minimum size. But once
2502 * a user starts to use the tracing facility, then they need to grow
2503 * to their default size.
2505 * This function is to be called when a tracer is about to be used.
2507 int tracing_update_buffers(void)
2511 mutex_lock(&trace_types_lock);
2512 if (!ring_buffer_expanded)
2513 ret = tracing_resize_ring_buffer(trace_buf_size);
2514 mutex_unlock(&trace_types_lock);
2519 struct trace_option_dentry;
2521 static struct trace_option_dentry *
2522 create_trace_option_files(struct tracer *tracer);
2525 destroy_trace_option_files(struct trace_option_dentry *topts);
2527 static int tracing_set_tracer(const char *buf)
2529 static struct trace_option_dentry *topts;
2530 struct trace_array *tr = &global_trace;
2534 mutex_lock(&trace_types_lock);
2536 if (!ring_buffer_expanded) {
2537 ret = tracing_resize_ring_buffer(trace_buf_size);
2543 for (t = trace_types; t; t = t->next) {
2544 if (strcmp(t->name, buf) == 0)
2551 if (t == current_trace)
2554 trace_branch_disable();
2555 if (current_trace && current_trace->reset)
2556 current_trace->reset(tr);
2558 destroy_trace_option_files(topts);
2562 topts = create_trace_option_files(current_trace);
2565 ret = tracer_init(t, tr);
2570 trace_branch_enable(tr);
2572 mutex_unlock(&trace_types_lock);
2578 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2579 size_t cnt, loff_t *ppos)
2581 char buf[max_tracer_type_len+1];
2588 if (cnt > max_tracer_type_len)
2589 cnt = max_tracer_type_len;
2591 if (copy_from_user(&buf, ubuf, cnt))
2596 /* strip ending whitespace. */
2597 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2600 err = tracing_set_tracer(buf);
2610 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2611 size_t cnt, loff_t *ppos)
2613 unsigned long *ptr = filp->private_data;
2617 r = snprintf(buf, sizeof(buf), "%ld\n",
2618 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2619 if (r > sizeof(buf))
2621 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2625 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2626 size_t cnt, loff_t *ppos)
2628 unsigned long *ptr = filp->private_data;
2633 if (cnt >= sizeof(buf))
2636 if (copy_from_user(&buf, ubuf, cnt))
2641 ret = strict_strtoul(buf, 10, &val);
2650 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2652 long cpu_file = (long) inode->i_private;
2653 struct trace_iterator *iter;
2656 if (tracing_disabled)
2659 mutex_lock(&trace_types_lock);
2661 /* We only allow one reader per cpu */
2662 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2663 if (!cpumask_empty(tracing_reader_cpumask)) {
2667 cpumask_setall(tracing_reader_cpumask);
2669 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2670 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2677 /* create a buffer to store the information to pass to userspace */
2678 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2685 * We make a copy of the current tracer to avoid concurrent
2686 * changes on it while we are reading.
2688 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2694 *iter->trace = *current_trace;
2696 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2701 /* trace pipe does not show start of buffer */
2702 cpumask_setall(iter->started);
2704 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2705 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2707 iter->cpu_file = cpu_file;
2708 iter->tr = &global_trace;
2709 mutex_init(&iter->mutex);
2710 filp->private_data = iter;
2712 if (iter->trace->pipe_open)
2713 iter->trace->pipe_open(iter);
2716 mutex_unlock(&trace_types_lock);
2722 mutex_unlock(&trace_types_lock);
2726 static int tracing_release_pipe(struct inode *inode, struct file *file)
2728 struct trace_iterator *iter = file->private_data;
2730 mutex_lock(&trace_types_lock);
2732 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2733 cpumask_clear(tracing_reader_cpumask);
2735 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2737 mutex_unlock(&trace_types_lock);
2739 free_cpumask_var(iter->started);
2740 mutex_destroy(&iter->mutex);
2748 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2750 struct trace_iterator *iter = filp->private_data;
2752 if (trace_flags & TRACE_ITER_BLOCK) {
2754 * Always select as readable when in blocking mode
2756 return POLLIN | POLLRDNORM;
2758 if (!trace_empty(iter))
2759 return POLLIN | POLLRDNORM;
2760 poll_wait(filp, &trace_wait, poll_table);
2761 if (!trace_empty(iter))
2762 return POLLIN | POLLRDNORM;
2769 void default_wait_pipe(struct trace_iterator *iter)
2773 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2775 if (trace_empty(iter))
2778 finish_wait(&trace_wait, &wait);
2782 * This is a make-shift waitqueue.
2783 * A tracer might use this callback on some rare cases:
2785 * 1) the current tracer might hold the runqueue lock when it wakes up
2786 * a reader, hence a deadlock (sched, function, and function graph tracers)
2787 * 2) the function tracers, trace all functions, we don't want
2788 * the overhead of calling wake_up and friends
2789 * (and tracing them too)
2791 * Anyway, this is really very primitive wakeup.
2793 void poll_wait_pipe(struct trace_iterator *iter)
2795 set_current_state(TASK_INTERRUPTIBLE);
2796 /* sleep for 100 msecs, and try again. */
2797 schedule_timeout(HZ / 10);
2800 /* Must be called with trace_types_lock mutex held. */
2801 static int tracing_wait_pipe(struct file *filp)
2803 struct trace_iterator *iter = filp->private_data;
2805 while (trace_empty(iter)) {
2807 if ((filp->f_flags & O_NONBLOCK)) {
2811 mutex_unlock(&iter->mutex);
2813 iter->trace->wait_pipe(iter);
2815 mutex_lock(&iter->mutex);
2817 if (signal_pending(current))
2821 * We block until we read something and tracing is disabled.
2822 * We still block if tracing is disabled, but we have never
2823 * read anything. This allows a user to cat this file, and
2824 * then enable tracing. But after we have read something,
2825 * we give an EOF when tracing is again disabled.
2827 * iter->pos will be 0 if we haven't read anything.
2829 if (!tracer_enabled && iter->pos)
2840 tracing_read_pipe(struct file *filp, char __user *ubuf,
2841 size_t cnt, loff_t *ppos)
2843 struct trace_iterator *iter = filp->private_data;
2844 static struct tracer *old_tracer;
2847 /* return any leftover data */
2848 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2852 trace_seq_init(&iter->seq);
2854 /* copy the tracer to avoid using a global lock all around */
2855 mutex_lock(&trace_types_lock);
2856 if (unlikely(old_tracer != current_trace && current_trace)) {
2857 old_tracer = current_trace;
2858 *iter->trace = *current_trace;
2860 mutex_unlock(&trace_types_lock);
2863 * Avoid more than one consumer on a single file descriptor
2864 * This is just a matter of traces coherency, the ring buffer itself
2867 mutex_lock(&iter->mutex);
2868 if (iter->trace->read) {
2869 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2875 sret = tracing_wait_pipe(filp);
2879 /* stop when tracing is finished */
2880 if (trace_empty(iter)) {
2885 if (cnt >= PAGE_SIZE)
2886 cnt = PAGE_SIZE - 1;
2888 /* reset all but tr, trace, and overruns */
2889 memset(&iter->seq, 0,
2890 sizeof(struct trace_iterator) -
2891 offsetof(struct trace_iterator, seq));
2894 trace_event_read_lock();
2895 while (find_next_entry_inc(iter) != NULL) {
2896 enum print_line_t ret;
2897 int len = iter->seq.len;
2899 ret = print_trace_line(iter);
2900 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2901 /* don't print partial lines */
2902 iter->seq.len = len;
2905 if (ret != TRACE_TYPE_NO_CONSUME)
2906 trace_consume(iter);
2908 if (iter->seq.len >= cnt)
2911 trace_event_read_unlock();
2913 /* Now copy what we have to the user */
2914 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2915 if (iter->seq.readpos >= iter->seq.len)
2916 trace_seq_init(&iter->seq);
2919 * If there was nothing to send to user, inspite of consuming trace
2920 * entries, go back to wait for more entries.
2926 mutex_unlock(&iter->mutex);
2931 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2932 struct pipe_buffer *buf)
2934 __free_page(buf->page);
2937 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2940 __free_page(spd->pages[idx]);
2943 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2945 .map = generic_pipe_buf_map,
2946 .unmap = generic_pipe_buf_unmap,
2947 .confirm = generic_pipe_buf_confirm,
2948 .release = tracing_pipe_buf_release,
2949 .steal = generic_pipe_buf_steal,
2950 .get = generic_pipe_buf_get,
2954 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2959 /* Seq buffer is page-sized, exactly what we need. */
2961 count = iter->seq.len;
2962 ret = print_trace_line(iter);
2963 count = iter->seq.len - count;
2966 iter->seq.len -= count;
2969 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2970 iter->seq.len -= count;
2974 if (ret != TRACE_TYPE_NO_CONSUME)
2975 trace_consume(iter);
2977 if (!find_next_entry_inc(iter)) {
2987 static ssize_t tracing_splice_read_pipe(struct file *filp,
2989 struct pipe_inode_info *pipe,
2993 struct page *pages[PIPE_BUFFERS];
2994 struct partial_page partial[PIPE_BUFFERS];
2995 struct trace_iterator *iter = filp->private_data;
2996 struct splice_pipe_desc spd = {
2999 .nr_pages = 0, /* This gets updated below. */
3001 .ops = &tracing_pipe_buf_ops,
3002 .spd_release = tracing_spd_release_pipe,
3004 static struct tracer *old_tracer;
3009 /* copy the tracer to avoid using a global lock all around */
3010 mutex_lock(&trace_types_lock);
3011 if (unlikely(old_tracer != current_trace && current_trace)) {
3012 old_tracer = current_trace;
3013 *iter->trace = *current_trace;
3015 mutex_unlock(&trace_types_lock);
3017 mutex_lock(&iter->mutex);
3019 if (iter->trace->splice_read) {
3020 ret = iter->trace->splice_read(iter, filp,
3021 ppos, pipe, len, flags);
3026 ret = tracing_wait_pipe(filp);
3030 if (!iter->ent && !find_next_entry_inc(iter)) {
3035 trace_event_read_lock();
3037 /* Fill as many pages as possible. */
3038 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3039 pages[i] = alloc_page(GFP_KERNEL);
3043 rem = tracing_fill_pipe_page(rem, iter);
3045 /* Copy the data into the page, so we can start over. */
3046 ret = trace_seq_to_buffer(&iter->seq,
3047 page_address(pages[i]),
3050 __free_page(pages[i]);
3053 partial[i].offset = 0;
3054 partial[i].len = iter->seq.len;
3056 trace_seq_init(&iter->seq);
3059 trace_event_read_unlock();
3060 mutex_unlock(&iter->mutex);
3064 return splice_to_pipe(pipe, &spd);
3067 mutex_unlock(&iter->mutex);
3073 tracing_entries_read(struct file *filp, char __user *ubuf,
3074 size_t cnt, loff_t *ppos)
3076 struct trace_array *tr = filp->private_data;
3080 mutex_lock(&trace_types_lock);
3081 if (!ring_buffer_expanded)
3082 r = sprintf(buf, "%lu (expanded: %lu)\n",
3084 trace_buf_size >> 10);
3086 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3087 mutex_unlock(&trace_types_lock);
3089 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3093 tracing_entries_write(struct file *filp, const char __user *ubuf,
3094 size_t cnt, loff_t *ppos)
3100 if (cnt >= sizeof(buf))
3103 if (copy_from_user(&buf, ubuf, cnt))
3108 ret = strict_strtoul(buf, 10, &val);
3112 /* must have at least 1 entry */
3116 mutex_lock(&trace_types_lock);
3120 /* disable all cpu buffers */
3121 for_each_tracing_cpu(cpu) {
3122 if (global_trace.data[cpu])
3123 atomic_inc(&global_trace.data[cpu]->disabled);
3124 if (max_tr.data[cpu])
3125 atomic_inc(&max_tr.data[cpu]->disabled);
3128 /* value is in KB */
3131 if (val != global_trace.entries) {
3132 ret = tracing_resize_ring_buffer(val);
3141 /* If check pages failed, return ENOMEM */
3142 if (tracing_disabled)
3145 for_each_tracing_cpu(cpu) {
3146 if (global_trace.data[cpu])
3147 atomic_dec(&global_trace.data[cpu]->disabled);
3148 if (max_tr.data[cpu])
3149 atomic_dec(&max_tr.data[cpu]->disabled);
3153 max_tr.entries = global_trace.entries;
3154 mutex_unlock(&trace_types_lock);
3159 static int mark_printk(const char *fmt, ...)
3163 va_start(args, fmt);
3164 ret = trace_vprintk(0, fmt, args);
3170 tracing_mark_write(struct file *filp, const char __user *ubuf,
3171 size_t cnt, loff_t *fpos)
3176 if (tracing_disabled)
3179 if (cnt > TRACE_BUF_SIZE)
3180 cnt = TRACE_BUF_SIZE;
3182 buf = kmalloc(cnt + 1, GFP_KERNEL);
3186 if (copy_from_user(buf, ubuf, cnt)) {
3191 /* Cut from the first nil or newline. */
3193 end = strchr(buf, '\n');
3197 cnt = mark_printk("%s\n", buf);
3204 static ssize_t tracing_clock_read(struct file *filp, char __user *ubuf,
3205 size_t cnt, loff_t *ppos)
3211 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3212 bufiter += snprintf(buf + bufiter, sizeof(buf) - bufiter,
3213 "%s%s%s%s", i ? " " : "",
3214 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3215 i == trace_clock_id ? "]" : "");
3216 bufiter += snprintf(buf + bufiter, sizeof(buf) - bufiter, "\n");
3218 return simple_read_from_buffer(ubuf, cnt, ppos, buf, bufiter);
3221 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3222 size_t cnt, loff_t *fpos)
3225 const char *clockstr;
3228 if (cnt >= sizeof(buf))
3231 if (copy_from_user(&buf, ubuf, cnt))
3236 clockstr = strstrip(buf);
3238 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3239 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3242 if (i == ARRAY_SIZE(trace_clocks))
3247 mutex_lock(&trace_types_lock);
3249 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3251 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3253 mutex_unlock(&trace_types_lock);
3260 static const struct file_operations tracing_max_lat_fops = {
3261 .open = tracing_open_generic,
3262 .read = tracing_max_lat_read,
3263 .write = tracing_max_lat_write,
3266 static const struct file_operations tracing_ctrl_fops = {
3267 .open = tracing_open_generic,
3268 .read = tracing_ctrl_read,
3269 .write = tracing_ctrl_write,
3272 static const struct file_operations set_tracer_fops = {
3273 .open = tracing_open_generic,
3274 .read = tracing_set_trace_read,
3275 .write = tracing_set_trace_write,
3278 static const struct file_operations tracing_pipe_fops = {
3279 .open = tracing_open_pipe,
3280 .poll = tracing_poll_pipe,
3281 .read = tracing_read_pipe,
3282 .splice_read = tracing_splice_read_pipe,
3283 .release = tracing_release_pipe,
3286 static const struct file_operations tracing_entries_fops = {
3287 .open = tracing_open_generic,
3288 .read = tracing_entries_read,
3289 .write = tracing_entries_write,
3292 static const struct file_operations tracing_mark_fops = {
3293 .open = tracing_open_generic,
3294 .write = tracing_mark_write,
3297 static const struct file_operations trace_clock_fops = {
3298 .open = tracing_open_generic,
3299 .read = tracing_clock_read,
3300 .write = tracing_clock_write,
3303 struct ftrace_buffer_info {
3304 struct trace_array *tr;
3310 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3312 int cpu = (int)(long)inode->i_private;
3313 struct ftrace_buffer_info *info;
3315 if (tracing_disabled)
3318 info = kzalloc(sizeof(*info), GFP_KERNEL);
3322 info->tr = &global_trace;
3325 /* Force reading ring buffer for first read */
3326 info->read = (unsigned int)-1;
3328 filp->private_data = info;
3330 return nonseekable_open(inode, filp);
3334 tracing_buffers_read(struct file *filp, char __user *ubuf,
3335 size_t count, loff_t *ppos)
3337 struct ftrace_buffer_info *info = filp->private_data;
3346 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3350 /* Do we have previous read data to read? */
3351 if (info->read < PAGE_SIZE)
3356 ret = ring_buffer_read_page(info->tr->buffer,
3363 pos = ring_buffer_page_len(info->spare);
3365 if (pos < PAGE_SIZE)
3366 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3369 size = PAGE_SIZE - info->read;
3373 ret = copy_to_user(ubuf, info->spare + info->read, size);
3384 static int tracing_buffers_release(struct inode *inode, struct file *file)
3386 struct ftrace_buffer_info *info = file->private_data;
3389 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3396 struct ring_buffer *buffer;
3401 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3402 struct pipe_buffer *buf)
3404 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3409 ring_buffer_free_read_page(ref->buffer, ref->page);
3414 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3415 struct pipe_buffer *buf)
3420 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3421 struct pipe_buffer *buf)
3423 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3428 /* Pipe buffer operations for a buffer. */
3429 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3431 .map = generic_pipe_buf_map,
3432 .unmap = generic_pipe_buf_unmap,
3433 .confirm = generic_pipe_buf_confirm,
3434 .release = buffer_pipe_buf_release,
3435 .steal = buffer_pipe_buf_steal,
3436 .get = buffer_pipe_buf_get,
3440 * Callback from splice_to_pipe(), if we need to release some pages
3441 * at the end of the spd in case we error'ed out in filling the pipe.
3443 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3445 struct buffer_ref *ref =
3446 (struct buffer_ref *)spd->partial[i].private;
3451 ring_buffer_free_read_page(ref->buffer, ref->page);
3453 spd->partial[i].private = 0;
3457 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3458 struct pipe_inode_info *pipe, size_t len,
3461 struct ftrace_buffer_info *info = file->private_data;
3462 struct partial_page partial[PIPE_BUFFERS];
3463 struct page *pages[PIPE_BUFFERS];
3464 struct splice_pipe_desc spd = {
3468 .ops = &buffer_pipe_buf_ops,
3469 .spd_release = buffer_spd_release,
3471 struct buffer_ref *ref;
3472 int entries, size, i;
3475 if (*ppos & (PAGE_SIZE - 1)) {
3476 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3480 if (len & (PAGE_SIZE - 1)) {
3481 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3482 if (len < PAGE_SIZE)
3487 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3489 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
3493 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3498 ref->buffer = info->tr->buffer;
3499 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3505 r = ring_buffer_read_page(ref->buffer, &ref->page,
3508 ring_buffer_free_read_page(ref->buffer,
3515 * zero out any left over data, this is going to
3518 size = ring_buffer_page_len(ref->page);
3519 if (size < PAGE_SIZE)
3520 memset(ref->page + size, 0, PAGE_SIZE - size);
3522 page = virt_to_page(ref->page);
3524 spd.pages[i] = page;
3525 spd.partial[i].len = PAGE_SIZE;
3526 spd.partial[i].offset = 0;
3527 spd.partial[i].private = (unsigned long)ref;
3531 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3536 /* did we read anything? */
3537 if (!spd.nr_pages) {
3538 if (flags & SPLICE_F_NONBLOCK)
3546 ret = splice_to_pipe(pipe, &spd);
3551 static const struct file_operations tracing_buffers_fops = {
3552 .open = tracing_buffers_open,
3553 .read = tracing_buffers_read,
3554 .release = tracing_buffers_release,
3555 .splice_read = tracing_buffers_splice_read,
3556 .llseek = no_llseek,
3560 tracing_stats_read(struct file *filp, char __user *ubuf,
3561 size_t count, loff_t *ppos)
3563 unsigned long cpu = (unsigned long)filp->private_data;
3564 struct trace_array *tr = &global_trace;
3565 struct trace_seq *s;
3568 s = kmalloc(sizeof(*s), GFP_KERNEL);
3574 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3575 trace_seq_printf(s, "entries: %ld\n", cnt);
3577 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3578 trace_seq_printf(s, "overrun: %ld\n", cnt);
3580 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3581 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3583 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3590 static const struct file_operations tracing_stats_fops = {
3591 .open = tracing_open_generic,
3592 .read = tracing_stats_read,
3595 #ifdef CONFIG_DYNAMIC_FTRACE
3597 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3603 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3604 size_t cnt, loff_t *ppos)
3606 static char ftrace_dyn_info_buffer[1024];
3607 static DEFINE_MUTEX(dyn_info_mutex);
3608 unsigned long *p = filp->private_data;
3609 char *buf = ftrace_dyn_info_buffer;
3610 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3613 mutex_lock(&dyn_info_mutex);
3614 r = sprintf(buf, "%ld ", *p);
3616 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3619 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3621 mutex_unlock(&dyn_info_mutex);
3626 static const struct file_operations tracing_dyn_info_fops = {
3627 .open = tracing_open_generic,
3628 .read = tracing_read_dyn_info,
3632 static struct dentry *d_tracer;
3634 struct dentry *tracing_init_dentry(void)
3641 if (!debugfs_initialized())
3644 d_tracer = debugfs_create_dir("tracing", NULL);
3646 if (!d_tracer && !once) {
3648 pr_warning("Could not create debugfs directory 'tracing'\n");
3655 static struct dentry *d_percpu;
3657 struct dentry *tracing_dentry_percpu(void)
3660 struct dentry *d_tracer;
3665 d_tracer = tracing_init_dentry();
3670 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3672 if (!d_percpu && !once) {
3674 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3681 static void tracing_init_debugfs_percpu(long cpu)
3683 struct dentry *d_percpu = tracing_dentry_percpu();
3684 struct dentry *d_cpu;
3685 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3688 if (cpu > 999 || cpu < 0)
3691 sprintf(cpu_dir, "cpu%ld", cpu);
3692 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3694 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3698 /* per cpu trace_pipe */
3699 trace_create_file("trace_pipe", 0444, d_cpu,
3700 (void *) cpu, &tracing_pipe_fops);
3703 trace_create_file("trace", 0644, d_cpu,
3704 (void *) cpu, &tracing_fops);
3706 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3707 (void *) cpu, &tracing_buffers_fops);
3709 trace_create_file("stats", 0444, d_cpu,
3710 (void *) cpu, &tracing_stats_fops);
3713 #ifdef CONFIG_FTRACE_SELFTEST
3714 /* Let selftest have access to static functions in this file */
3715 #include "trace_selftest.c"
3718 struct trace_option_dentry {
3719 struct tracer_opt *opt;
3720 struct tracer_flags *flags;
3721 struct dentry *entry;
3725 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3728 struct trace_option_dentry *topt = filp->private_data;
3731 if (topt->flags->val & topt->opt->bit)
3736 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3740 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3743 struct trace_option_dentry *topt = filp->private_data;
3748 if (cnt >= sizeof(buf))
3751 if (copy_from_user(&buf, ubuf, cnt))
3756 ret = strict_strtoul(buf, 10, &val);
3763 /* do nothing if already cleared */
3764 if (!(topt->flags->val & topt->opt->bit))
3767 mutex_lock(&trace_types_lock);
3768 if (current_trace->set_flag)
3769 ret = current_trace->set_flag(topt->flags->val,
3771 mutex_unlock(&trace_types_lock);
3774 topt->flags->val &= ~topt->opt->bit;
3777 /* do nothing if already set */
3778 if (topt->flags->val & topt->opt->bit)
3781 mutex_lock(&trace_types_lock);
3782 if (current_trace->set_flag)
3783 ret = current_trace->set_flag(topt->flags->val,
3785 mutex_unlock(&trace_types_lock);
3788 topt->flags->val |= topt->opt->bit;
3801 static const struct file_operations trace_options_fops = {
3802 .open = tracing_open_generic,
3803 .read = trace_options_read,
3804 .write = trace_options_write,
3808 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3811 long index = (long)filp->private_data;
3814 if (trace_flags & (1 << index))
3819 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3823 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3826 long index = (long)filp->private_data;
3831 if (cnt >= sizeof(buf))
3834 if (copy_from_user(&buf, ubuf, cnt))
3839 ret = strict_strtoul(buf, 10, &val);
3845 trace_flags &= ~(1 << index);
3848 trace_flags |= 1 << index;
3860 static const struct file_operations trace_options_core_fops = {
3861 .open = tracing_open_generic,
3862 .read = trace_options_core_read,
3863 .write = trace_options_core_write,
3866 struct dentry *trace_create_file(const char *name,
3868 struct dentry *parent,
3870 const struct file_operations *fops)
3874 ret = debugfs_create_file(name, mode, parent, data, fops);
3876 pr_warning("Could not create debugfs '%s' entry\n", name);
3882 static struct dentry *trace_options_init_dentry(void)
3884 struct dentry *d_tracer;
3885 static struct dentry *t_options;
3890 d_tracer = tracing_init_dentry();
3894 t_options = debugfs_create_dir("options", d_tracer);
3896 pr_warning("Could not create debugfs directory 'options'\n");
3904 create_trace_option_file(struct trace_option_dentry *topt,
3905 struct tracer_flags *flags,
3906 struct tracer_opt *opt)
3908 struct dentry *t_options;
3910 t_options = trace_options_init_dentry();
3914 topt->flags = flags;
3917 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
3918 &trace_options_fops);
3922 static struct trace_option_dentry *
3923 create_trace_option_files(struct tracer *tracer)
3925 struct trace_option_dentry *topts;
3926 struct tracer_flags *flags;
3927 struct tracer_opt *opts;
3933 flags = tracer->flags;
3935 if (!flags || !flags->opts)
3940 for (cnt = 0; opts[cnt].name; cnt++)
3943 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3947 for (cnt = 0; opts[cnt].name; cnt++)
3948 create_trace_option_file(&topts[cnt], flags,
3955 destroy_trace_option_files(struct trace_option_dentry *topts)
3962 for (cnt = 0; topts[cnt].opt; cnt++) {
3963 if (topts[cnt].entry)
3964 debugfs_remove(topts[cnt].entry);
3970 static struct dentry *
3971 create_trace_option_core_file(const char *option, long index)
3973 struct dentry *t_options;
3975 t_options = trace_options_init_dentry();
3979 return trace_create_file(option, 0644, t_options, (void *)index,
3980 &trace_options_core_fops);
3983 static __init void create_trace_options_dir(void)
3985 struct dentry *t_options;
3988 t_options = trace_options_init_dentry();
3992 for (i = 0; trace_options[i]; i++)
3993 create_trace_option_core_file(trace_options[i], i);
3996 static __init int tracer_init_debugfs(void)
3998 struct dentry *d_tracer;
4001 d_tracer = tracing_init_dentry();
4003 trace_create_file("tracing_enabled", 0644, d_tracer,
4004 &global_trace, &tracing_ctrl_fops);
4006 trace_create_file("trace_options", 0644, d_tracer,
4007 NULL, &tracing_iter_fops);
4009 trace_create_file("tracing_cpumask", 0644, d_tracer,
4010 NULL, &tracing_cpumask_fops);
4012 trace_create_file("trace", 0644, d_tracer,
4013 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4015 trace_create_file("available_tracers", 0444, d_tracer,
4016 &global_trace, &show_traces_fops);
4018 trace_create_file("current_tracer", 0644, d_tracer,
4019 &global_trace, &set_tracer_fops);
4021 #ifdef CONFIG_TRACER_MAX_TRACE
4022 trace_create_file("tracing_max_latency", 0644, d_tracer,
4023 &tracing_max_latency, &tracing_max_lat_fops);
4025 trace_create_file("tracing_thresh", 0644, d_tracer,
4026 &tracing_thresh, &tracing_max_lat_fops);
4029 trace_create_file("README", 0444, d_tracer,
4030 NULL, &tracing_readme_fops);
4032 trace_create_file("trace_pipe", 0444, d_tracer,
4033 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4035 trace_create_file("buffer_size_kb", 0644, d_tracer,
4036 &global_trace, &tracing_entries_fops);
4038 trace_create_file("trace_marker", 0220, d_tracer,
4039 NULL, &tracing_mark_fops);
4041 trace_create_file("saved_cmdlines", 0444, d_tracer,
4042 NULL, &tracing_saved_cmdlines_fops);
4044 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4047 #ifdef CONFIG_DYNAMIC_FTRACE
4048 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4049 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4051 #ifdef CONFIG_SYSPROF_TRACER
4052 init_tracer_sysprof_debugfs(d_tracer);
4055 create_trace_options_dir();
4057 for_each_tracing_cpu(cpu)
4058 tracing_init_debugfs_percpu(cpu);
4063 static int trace_panic_handler(struct notifier_block *this,
4064 unsigned long event, void *unused)
4066 if (ftrace_dump_on_oops)
4071 static struct notifier_block trace_panic_notifier = {
4072 .notifier_call = trace_panic_handler,
4074 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4077 static int trace_die_handler(struct notifier_block *self,
4083 if (ftrace_dump_on_oops)
4092 static struct notifier_block trace_die_notifier = {
4093 .notifier_call = trace_die_handler,
4098 * printk is set to max of 1024, we really don't need it that big.
4099 * Nothing should be printing 1000 characters anyway.
4101 #define TRACE_MAX_PRINT 1000
4104 * Define here KERN_TRACE so that we have one place to modify
4105 * it if we decide to change what log level the ftrace dump
4108 #define KERN_TRACE KERN_EMERG
4111 trace_printk_seq(struct trace_seq *s)
4113 /* Probably should print a warning here. */
4117 /* should be zero ended, but we are paranoid. */
4118 s->buffer[s->len] = 0;
4120 printk(KERN_TRACE "%s", s->buffer);
4125 static void __ftrace_dump(bool disable_tracing)
4127 static raw_spinlock_t ftrace_dump_lock =
4128 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
4129 /* use static because iter can be a bit big for the stack */
4130 static struct trace_iterator iter;
4131 unsigned int old_userobj;
4132 static int dump_ran;
4133 unsigned long flags;
4137 local_irq_save(flags);
4138 __raw_spin_lock(&ftrace_dump_lock);
4146 if (disable_tracing)
4149 for_each_tracing_cpu(cpu) {
4150 atomic_inc(&global_trace.data[cpu]->disabled);
4153 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4155 /* don't look at user memory in panic mode */
4156 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4158 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4160 /* Simulate the iterator */
4161 iter.tr = &global_trace;
4162 iter.trace = current_trace;
4163 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4166 * We need to stop all tracing on all CPUS to read the
4167 * the next buffer. This is a bit expensive, but is
4168 * not done often. We fill all what we can read,
4169 * and then release the locks again.
4172 while (!trace_empty(&iter)) {
4175 printk(KERN_TRACE "---------------------------------\n");
4179 /* reset all but tr, trace, and overruns */
4180 memset(&iter.seq, 0,
4181 sizeof(struct trace_iterator) -
4182 offsetof(struct trace_iterator, seq));
4183 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4186 if (find_next_entry_inc(&iter) != NULL) {
4189 ret = print_trace_line(&iter);
4190 if (ret != TRACE_TYPE_NO_CONSUME)
4191 trace_consume(&iter);
4194 trace_printk_seq(&iter.seq);
4198 printk(KERN_TRACE " (ftrace buffer empty)\n");
4200 printk(KERN_TRACE "---------------------------------\n");
4202 /* Re-enable tracing if requested */
4203 if (!disable_tracing) {
4204 trace_flags |= old_userobj;
4206 for_each_tracing_cpu(cpu) {
4207 atomic_dec(&global_trace.data[cpu]->disabled);
4213 __raw_spin_unlock(&ftrace_dump_lock);
4214 local_irq_restore(flags);
4217 /* By default: disable tracing after the dump */
4218 void ftrace_dump(void)
4220 __ftrace_dump(true);
4223 __init static int tracer_alloc_buffers(void)
4229 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4232 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4233 goto out_free_buffer_mask;
4235 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4236 goto out_free_tracing_cpumask;
4238 /* To save memory, keep the ring buffer size to its minimum */
4239 if (ring_buffer_expanded)
4240 ring_buf_size = trace_buf_size;
4244 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4245 cpumask_copy(tracing_cpumask, cpu_all_mask);
4246 cpumask_clear(tracing_reader_cpumask);
4248 /* TODO: make the number of buffers hot pluggable with CPUS */
4249 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4250 TRACE_BUFFER_FLAGS);
4251 if (!global_trace.buffer) {
4252 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4254 goto out_free_cpumask;
4256 global_trace.entries = ring_buffer_size(global_trace.buffer);
4259 #ifdef CONFIG_TRACER_MAX_TRACE
4260 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4261 TRACE_BUFFER_FLAGS);
4262 if (!max_tr.buffer) {
4263 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4265 ring_buffer_free(global_trace.buffer);
4266 goto out_free_cpumask;
4268 max_tr.entries = ring_buffer_size(max_tr.buffer);
4269 WARN_ON(max_tr.entries != global_trace.entries);
4272 /* Allocate the first page for all buffers */
4273 for_each_tracing_cpu(i) {
4274 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4275 max_tr.data[i] = &per_cpu(max_data, i);
4278 trace_init_cmdlines();
4280 register_tracer(&nop_trace);
4281 current_trace = &nop_trace;
4282 #ifdef CONFIG_BOOT_TRACER
4283 register_tracer(&boot_tracer);
4285 /* All seems OK, enable tracing */
4286 tracing_disabled = 0;
4288 atomic_notifier_chain_register(&panic_notifier_list,
4289 &trace_panic_notifier);
4291 register_die_notifier(&trace_die_notifier);
4296 free_cpumask_var(tracing_reader_cpumask);
4297 out_free_tracing_cpumask:
4298 free_cpumask_var(tracing_cpumask);
4299 out_free_buffer_mask:
4300 free_cpumask_var(tracing_buffer_mask);
4305 __init static int clear_boot_tracer(void)
4308 * The default tracer at boot buffer is an init section.
4309 * This function is called in lateinit. If we did not
4310 * find the boot tracer, then clear it out, to prevent
4311 * later registration from accessing the buffer that is
4312 * about to be freed.
4314 if (!default_bootup_tracer)
4317 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4318 default_bootup_tracer);
4319 default_bootup_tracer = NULL;
4324 early_initcall(tracer_alloc_buffers);
4325 fs_initcall(tracer_init_debugfs);
4326 late_initcall(clear_boot_tracer);