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;
556 * Run a selftest on this tracer.
557 * Here we reset the trace buffer, and set the current
558 * tracer to be this tracer. The tracer can then run some
559 * internal tracing to verify that everything is in order.
560 * If we fail, we do not register this tracer.
562 for_each_tracing_cpu(i)
563 tracing_reset(tr, i);
565 current_trace = type;
566 /* the test is responsible for initializing and enabling */
567 pr_info("Testing tracer %s: ", type->name);
568 ret = type->selftest(type, tr);
569 /* the test is responsible for resetting too */
570 current_trace = saved_tracer;
572 printk(KERN_CONT "FAILED!\n");
575 /* Only reset on passing, to avoid touching corrupted buffers */
576 for_each_tracing_cpu(i)
577 tracing_reset(tr, i);
579 printk(KERN_CONT "PASSED\n");
583 type->next = trace_types;
585 len = strlen(type->name);
586 if (len > max_tracer_type_len)
587 max_tracer_type_len = len;
590 tracing_selftest_running = false;
591 mutex_unlock(&trace_types_lock);
593 if (ret || !default_bootup_tracer)
596 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
599 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
600 /* Do we want this tracer to start on bootup? */
601 tracing_set_tracer(type->name);
602 default_bootup_tracer = NULL;
603 /* disable other selftests, since this will break it. */
604 tracing_selftest_disabled = 1;
605 #ifdef CONFIG_FTRACE_STARTUP_TEST
606 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
615 void unregister_tracer(struct tracer *type)
620 mutex_lock(&trace_types_lock);
621 for (t = &trace_types; *t; t = &(*t)->next) {
625 pr_info("Trace %s not registered\n", type->name);
631 if (type == current_trace && tracer_enabled) {
634 if (current_trace->stop)
635 current_trace->stop(&global_trace);
636 current_trace = &nop_trace;
639 if (strlen(type->name) != max_tracer_type_len)
642 max_tracer_type_len = 0;
643 for (t = &trace_types; *t; t = &(*t)->next) {
644 len = strlen((*t)->name);
645 if (len > max_tracer_type_len)
646 max_tracer_type_len = len;
649 mutex_unlock(&trace_types_lock);
652 void tracing_reset(struct trace_array *tr, int cpu)
654 ftrace_disable_cpu();
655 ring_buffer_reset_cpu(tr->buffer, cpu);
659 void tracing_reset_online_cpus(struct trace_array *tr)
663 tr->time_start = ftrace_now(tr->cpu);
665 for_each_online_cpu(cpu)
666 tracing_reset(tr, cpu);
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);
1373 * No necessary locking here. The worst thing which can
1374 * happen is loosing events consumed at the same time
1375 * by a trace_pipe reader.
1376 * Other than that, we don't risk to crash the ring buffer
1377 * because it serializes the readers.
1379 * The current tracer is copied to avoid a global locking
1382 static void *s_start(struct seq_file *m, loff_t *pos)
1384 struct trace_iterator *iter = m->private;
1385 static struct tracer *old_tracer;
1386 int cpu_file = iter->cpu_file;
1391 /* copy the tracer to avoid using a global lock all around */
1392 mutex_lock(&trace_types_lock);
1393 if (unlikely(old_tracer != current_trace && current_trace)) {
1394 old_tracer = current_trace;
1395 *iter->trace = *current_trace;
1397 mutex_unlock(&trace_types_lock);
1399 atomic_inc(&trace_record_cmdline_disabled);
1401 if (*pos != iter->pos) {
1406 ftrace_disable_cpu();
1408 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1409 for_each_tracing_cpu(cpu)
1410 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1412 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1415 ftrace_enable_cpu();
1417 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1422 p = s_next(m, p, &l);
1425 trace_event_read_lock();
1429 static void s_stop(struct seq_file *m, void *p)
1431 atomic_dec(&trace_record_cmdline_disabled);
1432 trace_event_read_unlock();
1435 static void print_lat_help_header(struct seq_file *m)
1437 seq_puts(m, "# _------=> CPU# \n");
1438 seq_puts(m, "# / _-----=> irqs-off \n");
1439 seq_puts(m, "# | / _----=> need-resched \n");
1440 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1441 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1442 seq_puts(m, "# |||| / \n");
1443 seq_puts(m, "# ||||| delay \n");
1444 seq_puts(m, "# cmd pid ||||| time | caller \n");
1445 seq_puts(m, "# \\ / ||||| \\ | / \n");
1448 static void print_func_help_header(struct seq_file *m)
1450 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1451 seq_puts(m, "# | | | | |\n");
1456 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1458 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1459 struct trace_array *tr = iter->tr;
1460 struct trace_array_cpu *data = tr->data[tr->cpu];
1461 struct tracer *type = current_trace;
1462 unsigned long total;
1463 unsigned long entries;
1464 const char *name = "preemption";
1469 entries = ring_buffer_entries(iter->tr->buffer);
1471 ring_buffer_overruns(iter->tr->buffer);
1473 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1475 seq_puts(m, "# -----------------------------------"
1476 "---------------------------------\n");
1477 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1478 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1479 nsecs_to_usecs(data->saved_latency),
1483 #if defined(CONFIG_PREEMPT_NONE)
1485 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1487 #elif defined(CONFIG_PREEMPT)
1492 /* These are reserved for later use */
1495 seq_printf(m, " #P:%d)\n", num_online_cpus());
1499 seq_puts(m, "# -----------------\n");
1500 seq_printf(m, "# | task: %.16s-%d "
1501 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1502 data->comm, data->pid, data->uid, data->nice,
1503 data->policy, data->rt_priority);
1504 seq_puts(m, "# -----------------\n");
1506 if (data->critical_start) {
1507 seq_puts(m, "# => started at: ");
1508 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1509 trace_print_seq(m, &iter->seq);
1510 seq_puts(m, "\n# => ended at: ");
1511 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1512 trace_print_seq(m, &iter->seq);
1513 seq_puts(m, "\n#\n");
1519 static void test_cpu_buff_start(struct trace_iterator *iter)
1521 struct trace_seq *s = &iter->seq;
1523 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1526 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1529 if (cpumask_test_cpu(iter->cpu, iter->started))
1532 cpumask_set_cpu(iter->cpu, iter->started);
1534 /* Don't print started cpu buffer for the first entry of the trace */
1536 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1540 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1542 struct trace_seq *s = &iter->seq;
1543 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1544 struct trace_entry *entry;
1545 struct trace_event *event;
1549 test_cpu_buff_start(iter);
1551 event = ftrace_find_event(entry->type);
1553 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1554 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1555 if (!trace_print_lat_context(iter))
1558 if (!trace_print_context(iter))
1564 return event->trace(iter, sym_flags);
1566 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1569 return TRACE_TYPE_HANDLED;
1571 return TRACE_TYPE_PARTIAL_LINE;
1574 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1576 struct trace_seq *s = &iter->seq;
1577 struct trace_entry *entry;
1578 struct trace_event *event;
1582 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1583 if (!trace_seq_printf(s, "%d %d %llu ",
1584 entry->pid, iter->cpu, iter->ts))
1588 event = ftrace_find_event(entry->type);
1590 return event->raw(iter, 0);
1592 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1595 return TRACE_TYPE_HANDLED;
1597 return TRACE_TYPE_PARTIAL_LINE;
1600 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1602 struct trace_seq *s = &iter->seq;
1603 unsigned char newline = '\n';
1604 struct trace_entry *entry;
1605 struct trace_event *event;
1609 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1610 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1611 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1612 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1615 event = ftrace_find_event(entry->type);
1617 enum print_line_t ret = event->hex(iter, 0);
1618 if (ret != TRACE_TYPE_HANDLED)
1622 SEQ_PUT_FIELD_RET(s, newline);
1624 return TRACE_TYPE_HANDLED;
1627 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1629 struct trace_seq *s = &iter->seq;
1630 struct trace_entry *entry;
1631 struct trace_event *event;
1635 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1636 SEQ_PUT_FIELD_RET(s, entry->pid);
1637 SEQ_PUT_FIELD_RET(s, iter->cpu);
1638 SEQ_PUT_FIELD_RET(s, iter->ts);
1641 event = ftrace_find_event(entry->type);
1642 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1645 static int trace_empty(struct trace_iterator *iter)
1649 /* If we are looking at one CPU buffer, only check that one */
1650 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1651 cpu = iter->cpu_file;
1652 if (iter->buffer_iter[cpu]) {
1653 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1656 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1662 for_each_tracing_cpu(cpu) {
1663 if (iter->buffer_iter[cpu]) {
1664 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1667 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1675 /* Called with trace_event_read_lock() held. */
1676 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1678 enum print_line_t ret;
1680 if (iter->trace && iter->trace->print_line) {
1681 ret = iter->trace->print_line(iter);
1682 if (ret != TRACE_TYPE_UNHANDLED)
1686 if (iter->ent->type == TRACE_BPRINT &&
1687 trace_flags & TRACE_ITER_PRINTK &&
1688 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1689 return trace_print_bprintk_msg_only(iter);
1691 if (iter->ent->type == TRACE_PRINT &&
1692 trace_flags & TRACE_ITER_PRINTK &&
1693 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1694 return trace_print_printk_msg_only(iter);
1696 if (trace_flags & TRACE_ITER_BIN)
1697 return print_bin_fmt(iter);
1699 if (trace_flags & TRACE_ITER_HEX)
1700 return print_hex_fmt(iter);
1702 if (trace_flags & TRACE_ITER_RAW)
1703 return print_raw_fmt(iter);
1705 return print_trace_fmt(iter);
1708 static int s_show(struct seq_file *m, void *v)
1710 struct trace_iterator *iter = v;
1712 if (iter->ent == NULL) {
1714 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1717 if (iter->trace && iter->trace->print_header)
1718 iter->trace->print_header(m);
1719 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1720 /* print nothing if the buffers are empty */
1721 if (trace_empty(iter))
1723 print_trace_header(m, iter);
1724 if (!(trace_flags & TRACE_ITER_VERBOSE))
1725 print_lat_help_header(m);
1727 if (!(trace_flags & TRACE_ITER_VERBOSE))
1728 print_func_help_header(m);
1731 print_trace_line(iter);
1732 trace_print_seq(m, &iter->seq);
1738 static struct seq_operations tracer_seq_ops = {
1745 static struct trace_iterator *
1746 __tracing_open(struct inode *inode, struct file *file)
1748 long cpu_file = (long) inode->i_private;
1749 void *fail_ret = ERR_PTR(-ENOMEM);
1750 struct trace_iterator *iter;
1754 if (tracing_disabled)
1755 return ERR_PTR(-ENODEV);
1757 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1759 return ERR_PTR(-ENOMEM);
1762 * We make a copy of the current tracer to avoid concurrent
1763 * changes on it while we are reading.
1765 mutex_lock(&trace_types_lock);
1766 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1771 *iter->trace = *current_trace;
1773 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1776 cpumask_clear(iter->started);
1778 if (current_trace && current_trace->print_max)
1781 iter->tr = &global_trace;
1783 mutex_init(&iter->mutex);
1784 iter->cpu_file = cpu_file;
1786 /* Notify the tracer early; before we stop tracing. */
1787 if (iter->trace && iter->trace->open)
1788 iter->trace->open(iter);
1790 /* Annotate start of buffers if we had overruns */
1791 if (ring_buffer_overruns(iter->tr->buffer))
1792 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1794 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1795 for_each_tracing_cpu(cpu) {
1797 iter->buffer_iter[cpu] =
1798 ring_buffer_read_start(iter->tr->buffer, cpu);
1801 cpu = iter->cpu_file;
1802 iter->buffer_iter[cpu] =
1803 ring_buffer_read_start(iter->tr->buffer, cpu);
1806 /* TODO stop tracer */
1807 ret = seq_open(file, &tracer_seq_ops);
1809 fail_ret = ERR_PTR(ret);
1813 m = file->private_data;
1816 /* stop the trace while dumping */
1819 mutex_unlock(&trace_types_lock);
1824 for_each_tracing_cpu(cpu) {
1825 if (iter->buffer_iter[cpu])
1826 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1828 free_cpumask_var(iter->started);
1830 mutex_unlock(&trace_types_lock);
1837 int tracing_open_generic(struct inode *inode, struct file *filp)
1839 if (tracing_disabled)
1842 filp->private_data = inode->i_private;
1846 static int tracing_release(struct inode *inode, struct file *file)
1848 struct seq_file *m = (struct seq_file *)file->private_data;
1849 struct trace_iterator *iter;
1852 if (!(file->f_mode & FMODE_READ))
1857 mutex_lock(&trace_types_lock);
1858 for_each_tracing_cpu(cpu) {
1859 if (iter->buffer_iter[cpu])
1860 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1863 if (iter->trace && iter->trace->close)
1864 iter->trace->close(iter);
1866 /* reenable tracing if it was previously enabled */
1868 mutex_unlock(&trace_types_lock);
1870 seq_release(inode, file);
1871 mutex_destroy(&iter->mutex);
1872 free_cpumask_var(iter->started);
1878 static int tracing_open(struct inode *inode, struct file *file)
1880 struct trace_iterator *iter;
1883 /* If this file was open for write, then erase contents */
1884 if ((file->f_mode & FMODE_WRITE) &&
1885 (file->f_flags & O_TRUNC)) {
1886 long cpu = (long) inode->i_private;
1888 if (cpu == TRACE_PIPE_ALL_CPU)
1889 tracing_reset_online_cpus(&global_trace);
1891 tracing_reset(&global_trace, cpu);
1894 if (file->f_mode & FMODE_READ) {
1895 iter = __tracing_open(inode, file);
1897 ret = PTR_ERR(iter);
1898 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1899 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1905 t_next(struct seq_file *m, void *v, loff_t *pos)
1907 struct tracer *t = v;
1917 static void *t_start(struct seq_file *m, loff_t *pos)
1922 mutex_lock(&trace_types_lock);
1923 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
1929 static void t_stop(struct seq_file *m, void *p)
1931 mutex_unlock(&trace_types_lock);
1934 static int t_show(struct seq_file *m, void *v)
1936 struct tracer *t = v;
1941 seq_printf(m, "%s", t->name);
1950 static struct seq_operations show_traces_seq_ops = {
1957 static int show_traces_open(struct inode *inode, struct file *file)
1959 if (tracing_disabled)
1962 return seq_open(file, &show_traces_seq_ops);
1966 tracing_write_stub(struct file *filp, const char __user *ubuf,
1967 size_t count, loff_t *ppos)
1972 static const struct file_operations tracing_fops = {
1973 .open = tracing_open,
1975 .write = tracing_write_stub,
1976 .llseek = seq_lseek,
1977 .release = tracing_release,
1980 static const struct file_operations show_traces_fops = {
1981 .open = show_traces_open,
1983 .release = seq_release,
1987 * Only trace on a CPU if the bitmask is set:
1989 static cpumask_var_t tracing_cpumask;
1992 * The tracer itself will not take this lock, but still we want
1993 * to provide a consistent cpumask to user-space:
1995 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1998 * Temporary storage for the character representation of the
1999 * CPU bitmask (and one more byte for the newline):
2001 static char mask_str[NR_CPUS + 1];
2004 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2005 size_t count, loff_t *ppos)
2009 mutex_lock(&tracing_cpumask_update_lock);
2011 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2012 if (count - len < 2) {
2016 len += sprintf(mask_str + len, "\n");
2017 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2020 mutex_unlock(&tracing_cpumask_update_lock);
2026 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2027 size_t count, loff_t *ppos)
2030 cpumask_var_t tracing_cpumask_new;
2032 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2035 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2039 mutex_lock(&tracing_cpumask_update_lock);
2041 local_irq_disable();
2042 __raw_spin_lock(&ftrace_max_lock);
2043 for_each_tracing_cpu(cpu) {
2045 * Increase/decrease the disabled counter if we are
2046 * about to flip a bit in the cpumask:
2048 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2049 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2050 atomic_inc(&global_trace.data[cpu]->disabled);
2052 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2053 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2054 atomic_dec(&global_trace.data[cpu]->disabled);
2057 __raw_spin_unlock(&ftrace_max_lock);
2060 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2062 mutex_unlock(&tracing_cpumask_update_lock);
2063 free_cpumask_var(tracing_cpumask_new);
2068 free_cpumask_var(tracing_cpumask_new);
2073 static const struct file_operations tracing_cpumask_fops = {
2074 .open = tracing_open_generic,
2075 .read = tracing_cpumask_read,
2076 .write = tracing_cpumask_write,
2080 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2081 size_t cnt, loff_t *ppos)
2083 struct tracer_opt *trace_opts;
2091 /* calculate max size */
2092 for (i = 0; trace_options[i]; i++) {
2093 len += strlen(trace_options[i]);
2094 len += 3; /* "no" and newline */
2097 mutex_lock(&trace_types_lock);
2098 tracer_flags = current_trace->flags->val;
2099 trace_opts = current_trace->flags->opts;
2102 * Increase the size with names of options specific
2103 * of the current tracer.
2105 for (i = 0; trace_opts[i].name; i++) {
2106 len += strlen(trace_opts[i].name);
2107 len += 3; /* "no" and newline */
2111 buf = kmalloc(len + 1, GFP_KERNEL);
2113 mutex_unlock(&trace_types_lock);
2117 for (i = 0; trace_options[i]; i++) {
2118 if (trace_flags & (1 << i))
2119 r += sprintf(buf + r, "%s\n", trace_options[i]);
2121 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2124 for (i = 0; trace_opts[i].name; i++) {
2125 if (tracer_flags & trace_opts[i].bit)
2126 r += sprintf(buf + r, "%s\n",
2127 trace_opts[i].name);
2129 r += sprintf(buf + r, "no%s\n",
2130 trace_opts[i].name);
2132 mutex_unlock(&trace_types_lock);
2134 WARN_ON(r >= len + 1);
2136 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2142 /* Try to assign a tracer specific option */
2143 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2145 struct tracer_flags *tracer_flags = trace->flags;
2146 struct tracer_opt *opts = NULL;
2150 for (i = 0; tracer_flags->opts[i].name; i++) {
2151 opts = &tracer_flags->opts[i];
2152 len = strlen(opts->name);
2154 if (strncmp(cmp, opts->name, len) == 0) {
2155 ret = trace->set_flag(tracer_flags->val,
2161 if (!tracer_flags->opts[i].name)
2164 /* Refused to handle */
2169 tracer_flags->val &= ~opts->bit;
2171 tracer_flags->val |= opts->bit;
2176 static void set_tracer_flags(unsigned int mask, int enabled)
2178 /* do nothing if flag is already set */
2179 if (!!(trace_flags & mask) == !!enabled)
2183 trace_flags |= mask;
2185 trace_flags &= ~mask;
2189 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2190 size_t cnt, loff_t *ppos)
2198 if (cnt >= sizeof(buf))
2201 if (copy_from_user(&buf, ubuf, cnt))
2206 if (strncmp(buf, "no", 2) == 0) {
2211 for (i = 0; trace_options[i]; i++) {
2212 int len = strlen(trace_options[i]);
2214 if (strncmp(cmp, trace_options[i], len) == 0) {
2215 set_tracer_flags(1 << i, !neg);
2220 /* If no option could be set, test the specific tracer options */
2221 if (!trace_options[i]) {
2222 mutex_lock(&trace_types_lock);
2223 ret = set_tracer_option(current_trace, cmp, neg);
2224 mutex_unlock(&trace_types_lock);
2234 static const struct file_operations tracing_iter_fops = {
2235 .open = tracing_open_generic,
2236 .read = tracing_trace_options_read,
2237 .write = tracing_trace_options_write,
2240 static const char readme_msg[] =
2241 "tracing mini-HOWTO:\n\n"
2242 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2243 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2244 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2245 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2247 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2248 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2250 "# cat /sys/kernel/debug/tracing/trace_options\n"
2251 "noprint-parent nosym-offset nosym-addr noverbose\n"
2252 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2253 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2254 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2255 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2259 tracing_readme_read(struct file *filp, char __user *ubuf,
2260 size_t cnt, loff_t *ppos)
2262 return simple_read_from_buffer(ubuf, cnt, ppos,
2263 readme_msg, strlen(readme_msg));
2266 static const struct file_operations tracing_readme_fops = {
2267 .open = tracing_open_generic,
2268 .read = tracing_readme_read,
2272 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2273 size_t cnt, loff_t *ppos)
2282 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2286 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2294 for (i = 0; i < SAVED_CMDLINES; i++) {
2297 pid = map_cmdline_to_pid[i];
2298 if (pid == -1 || pid == NO_CMDLINE_MAP)
2301 trace_find_cmdline(pid, buf_comm);
2302 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2307 len = simple_read_from_buffer(ubuf, cnt, ppos,
2316 static const struct file_operations tracing_saved_cmdlines_fops = {
2317 .open = tracing_open_generic,
2318 .read = tracing_saved_cmdlines_read,
2322 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2323 size_t cnt, loff_t *ppos)
2328 r = sprintf(buf, "%u\n", tracer_enabled);
2329 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2333 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2334 size_t cnt, loff_t *ppos)
2336 struct trace_array *tr = filp->private_data;
2341 if (cnt >= sizeof(buf))
2344 if (copy_from_user(&buf, ubuf, cnt))
2349 ret = strict_strtoul(buf, 10, &val);
2355 mutex_lock(&trace_types_lock);
2356 if (tracer_enabled ^ val) {
2359 if (current_trace->start)
2360 current_trace->start(tr);
2365 if (current_trace->stop)
2366 current_trace->stop(tr);
2369 mutex_unlock(&trace_types_lock);
2377 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2378 size_t cnt, loff_t *ppos)
2380 char buf[max_tracer_type_len+2];
2383 mutex_lock(&trace_types_lock);
2385 r = sprintf(buf, "%s\n", current_trace->name);
2387 r = sprintf(buf, "\n");
2388 mutex_unlock(&trace_types_lock);
2390 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2393 int tracer_init(struct tracer *t, struct trace_array *tr)
2395 tracing_reset_online_cpus(tr);
2399 static int tracing_resize_ring_buffer(unsigned long size)
2404 * If kernel or user changes the size of the ring buffer
2405 * we use the size that was given, and we can forget about
2406 * expanding it later.
2408 ring_buffer_expanded = 1;
2410 ret = ring_buffer_resize(global_trace.buffer, size);
2414 ret = ring_buffer_resize(max_tr.buffer, size);
2418 r = ring_buffer_resize(global_trace.buffer,
2419 global_trace.entries);
2422 * AARGH! We are left with different
2423 * size max buffer!!!!
2424 * The max buffer is our "snapshot" buffer.
2425 * When a tracer needs a snapshot (one of the
2426 * latency tracers), it swaps the max buffer
2427 * with the saved snap shot. We succeeded to
2428 * update the size of the main buffer, but failed to
2429 * update the size of the max buffer. But when we tried
2430 * to reset the main buffer to the original size, we
2431 * failed there too. This is very unlikely to
2432 * happen, but if it does, warn and kill all
2436 tracing_disabled = 1;
2441 global_trace.entries = size;
2447 * tracing_update_buffers - used by tracing facility to expand ring buffers
2449 * To save on memory when the tracing is never used on a system with it
2450 * configured in. The ring buffers are set to a minimum size. But once
2451 * a user starts to use the tracing facility, then they need to grow
2452 * to their default size.
2454 * This function is to be called when a tracer is about to be used.
2456 int tracing_update_buffers(void)
2460 mutex_lock(&trace_types_lock);
2461 if (!ring_buffer_expanded)
2462 ret = tracing_resize_ring_buffer(trace_buf_size);
2463 mutex_unlock(&trace_types_lock);
2468 struct trace_option_dentry;
2470 static struct trace_option_dentry *
2471 create_trace_option_files(struct tracer *tracer);
2474 destroy_trace_option_files(struct trace_option_dentry *topts);
2476 static int tracing_set_tracer(const char *buf)
2478 static struct trace_option_dentry *topts;
2479 struct trace_array *tr = &global_trace;
2483 mutex_lock(&trace_types_lock);
2485 if (!ring_buffer_expanded) {
2486 ret = tracing_resize_ring_buffer(trace_buf_size);
2492 for (t = trace_types; t; t = t->next) {
2493 if (strcmp(t->name, buf) == 0)
2500 if (t == current_trace)
2503 trace_branch_disable();
2504 if (current_trace && current_trace->reset)
2505 current_trace->reset(tr);
2507 destroy_trace_option_files(topts);
2511 topts = create_trace_option_files(current_trace);
2514 ret = tracer_init(t, tr);
2519 trace_branch_enable(tr);
2521 mutex_unlock(&trace_types_lock);
2527 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2528 size_t cnt, loff_t *ppos)
2530 char buf[max_tracer_type_len+1];
2537 if (cnt > max_tracer_type_len)
2538 cnt = max_tracer_type_len;
2540 if (copy_from_user(&buf, ubuf, cnt))
2545 /* strip ending whitespace. */
2546 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2549 err = tracing_set_tracer(buf);
2559 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2560 size_t cnt, loff_t *ppos)
2562 unsigned long *ptr = filp->private_data;
2566 r = snprintf(buf, sizeof(buf), "%ld\n",
2567 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2568 if (r > sizeof(buf))
2570 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2574 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2575 size_t cnt, loff_t *ppos)
2577 unsigned long *ptr = filp->private_data;
2582 if (cnt >= sizeof(buf))
2585 if (copy_from_user(&buf, ubuf, cnt))
2590 ret = strict_strtoul(buf, 10, &val);
2599 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2601 long cpu_file = (long) inode->i_private;
2602 struct trace_iterator *iter;
2605 if (tracing_disabled)
2608 mutex_lock(&trace_types_lock);
2610 /* We only allow one reader per cpu */
2611 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2612 if (!cpumask_empty(tracing_reader_cpumask)) {
2616 cpumask_setall(tracing_reader_cpumask);
2618 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2619 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2626 /* create a buffer to store the information to pass to userspace */
2627 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2634 * We make a copy of the current tracer to avoid concurrent
2635 * changes on it while we are reading.
2637 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2643 *iter->trace = *current_trace;
2645 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2650 /* trace pipe does not show start of buffer */
2651 cpumask_setall(iter->started);
2653 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2654 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2656 iter->cpu_file = cpu_file;
2657 iter->tr = &global_trace;
2658 mutex_init(&iter->mutex);
2659 filp->private_data = iter;
2661 if (iter->trace->pipe_open)
2662 iter->trace->pipe_open(iter);
2665 mutex_unlock(&trace_types_lock);
2671 mutex_unlock(&trace_types_lock);
2675 static int tracing_release_pipe(struct inode *inode, struct file *file)
2677 struct trace_iterator *iter = file->private_data;
2679 mutex_lock(&trace_types_lock);
2681 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2682 cpumask_clear(tracing_reader_cpumask);
2684 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2686 mutex_unlock(&trace_types_lock);
2688 free_cpumask_var(iter->started);
2689 mutex_destroy(&iter->mutex);
2697 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2699 struct trace_iterator *iter = filp->private_data;
2701 if (trace_flags & TRACE_ITER_BLOCK) {
2703 * Always select as readable when in blocking mode
2705 return POLLIN | POLLRDNORM;
2707 if (!trace_empty(iter))
2708 return POLLIN | POLLRDNORM;
2709 poll_wait(filp, &trace_wait, poll_table);
2710 if (!trace_empty(iter))
2711 return POLLIN | POLLRDNORM;
2718 void default_wait_pipe(struct trace_iterator *iter)
2722 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2724 if (trace_empty(iter))
2727 finish_wait(&trace_wait, &wait);
2731 * This is a make-shift waitqueue.
2732 * A tracer might use this callback on some rare cases:
2734 * 1) the current tracer might hold the runqueue lock when it wakes up
2735 * a reader, hence a deadlock (sched, function, and function graph tracers)
2736 * 2) the function tracers, trace all functions, we don't want
2737 * the overhead of calling wake_up and friends
2738 * (and tracing them too)
2740 * Anyway, this is really very primitive wakeup.
2742 void poll_wait_pipe(struct trace_iterator *iter)
2744 set_current_state(TASK_INTERRUPTIBLE);
2745 /* sleep for 100 msecs, and try again. */
2746 schedule_timeout(HZ / 10);
2749 /* Must be called with trace_types_lock mutex held. */
2750 static int tracing_wait_pipe(struct file *filp)
2752 struct trace_iterator *iter = filp->private_data;
2754 while (trace_empty(iter)) {
2756 if ((filp->f_flags & O_NONBLOCK)) {
2760 mutex_unlock(&iter->mutex);
2762 iter->trace->wait_pipe(iter);
2764 mutex_lock(&iter->mutex);
2766 if (signal_pending(current))
2770 * We block until we read something and tracing is disabled.
2771 * We still block if tracing is disabled, but we have never
2772 * read anything. This allows a user to cat this file, and
2773 * then enable tracing. But after we have read something,
2774 * we give an EOF when tracing is again disabled.
2776 * iter->pos will be 0 if we haven't read anything.
2778 if (!tracer_enabled && iter->pos)
2789 tracing_read_pipe(struct file *filp, char __user *ubuf,
2790 size_t cnt, loff_t *ppos)
2792 struct trace_iterator *iter = filp->private_data;
2793 static struct tracer *old_tracer;
2796 /* return any leftover data */
2797 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2801 trace_seq_init(&iter->seq);
2803 /* copy the tracer to avoid using a global lock all around */
2804 mutex_lock(&trace_types_lock);
2805 if (unlikely(old_tracer != current_trace && current_trace)) {
2806 old_tracer = current_trace;
2807 *iter->trace = *current_trace;
2809 mutex_unlock(&trace_types_lock);
2812 * Avoid more than one consumer on a single file descriptor
2813 * This is just a matter of traces coherency, the ring buffer itself
2816 mutex_lock(&iter->mutex);
2817 if (iter->trace->read) {
2818 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2824 sret = tracing_wait_pipe(filp);
2828 /* stop when tracing is finished */
2829 if (trace_empty(iter)) {
2834 if (cnt >= PAGE_SIZE)
2835 cnt = PAGE_SIZE - 1;
2837 /* reset all but tr, trace, and overruns */
2838 memset(&iter->seq, 0,
2839 sizeof(struct trace_iterator) -
2840 offsetof(struct trace_iterator, seq));
2843 trace_event_read_lock();
2844 while (find_next_entry_inc(iter) != NULL) {
2845 enum print_line_t ret;
2846 int len = iter->seq.len;
2848 ret = print_trace_line(iter);
2849 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2850 /* don't print partial lines */
2851 iter->seq.len = len;
2854 if (ret != TRACE_TYPE_NO_CONSUME)
2855 trace_consume(iter);
2857 if (iter->seq.len >= cnt)
2860 trace_event_read_unlock();
2862 /* Now copy what we have to the user */
2863 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2864 if (iter->seq.readpos >= iter->seq.len)
2865 trace_seq_init(&iter->seq);
2868 * If there was nothing to send to user, inspite of consuming trace
2869 * entries, go back to wait for more entries.
2875 mutex_unlock(&iter->mutex);
2880 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2881 struct pipe_buffer *buf)
2883 __free_page(buf->page);
2886 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2889 __free_page(spd->pages[idx]);
2892 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2894 .map = generic_pipe_buf_map,
2895 .unmap = generic_pipe_buf_unmap,
2896 .confirm = generic_pipe_buf_confirm,
2897 .release = tracing_pipe_buf_release,
2898 .steal = generic_pipe_buf_steal,
2899 .get = generic_pipe_buf_get,
2903 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2908 /* Seq buffer is page-sized, exactly what we need. */
2910 count = iter->seq.len;
2911 ret = print_trace_line(iter);
2912 count = iter->seq.len - count;
2915 iter->seq.len -= count;
2918 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2919 iter->seq.len -= count;
2923 if (ret != TRACE_TYPE_NO_CONSUME)
2924 trace_consume(iter);
2926 if (!find_next_entry_inc(iter)) {
2936 static ssize_t tracing_splice_read_pipe(struct file *filp,
2938 struct pipe_inode_info *pipe,
2942 struct page *pages[PIPE_BUFFERS];
2943 struct partial_page partial[PIPE_BUFFERS];
2944 struct trace_iterator *iter = filp->private_data;
2945 struct splice_pipe_desc spd = {
2948 .nr_pages = 0, /* This gets updated below. */
2950 .ops = &tracing_pipe_buf_ops,
2951 .spd_release = tracing_spd_release_pipe,
2953 static struct tracer *old_tracer;
2958 /* copy the tracer to avoid using a global lock all around */
2959 mutex_lock(&trace_types_lock);
2960 if (unlikely(old_tracer != current_trace && current_trace)) {
2961 old_tracer = current_trace;
2962 *iter->trace = *current_trace;
2964 mutex_unlock(&trace_types_lock);
2966 mutex_lock(&iter->mutex);
2968 if (iter->trace->splice_read) {
2969 ret = iter->trace->splice_read(iter, filp,
2970 ppos, pipe, len, flags);
2975 ret = tracing_wait_pipe(filp);
2979 if (!iter->ent && !find_next_entry_inc(iter)) {
2984 trace_event_read_lock();
2986 /* Fill as many pages as possible. */
2987 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2988 pages[i] = alloc_page(GFP_KERNEL);
2992 rem = tracing_fill_pipe_page(rem, iter);
2994 /* Copy the data into the page, so we can start over. */
2995 ret = trace_seq_to_buffer(&iter->seq,
2996 page_address(pages[i]),
2999 __free_page(pages[i]);
3002 partial[i].offset = 0;
3003 partial[i].len = iter->seq.len;
3005 trace_seq_init(&iter->seq);
3008 trace_event_read_unlock();
3009 mutex_unlock(&iter->mutex);
3013 return splice_to_pipe(pipe, &spd);
3016 mutex_unlock(&iter->mutex);
3022 tracing_entries_read(struct file *filp, char __user *ubuf,
3023 size_t cnt, loff_t *ppos)
3025 struct trace_array *tr = filp->private_data;
3029 mutex_lock(&trace_types_lock);
3030 if (!ring_buffer_expanded)
3031 r = sprintf(buf, "%lu (expanded: %lu)\n",
3033 trace_buf_size >> 10);
3035 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3036 mutex_unlock(&trace_types_lock);
3038 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3042 tracing_entries_write(struct file *filp, const char __user *ubuf,
3043 size_t cnt, loff_t *ppos)
3049 if (cnt >= sizeof(buf))
3052 if (copy_from_user(&buf, ubuf, cnt))
3057 ret = strict_strtoul(buf, 10, &val);
3061 /* must have at least 1 entry */
3065 mutex_lock(&trace_types_lock);
3069 /* disable all cpu buffers */
3070 for_each_tracing_cpu(cpu) {
3071 if (global_trace.data[cpu])
3072 atomic_inc(&global_trace.data[cpu]->disabled);
3073 if (max_tr.data[cpu])
3074 atomic_inc(&max_tr.data[cpu]->disabled);
3077 /* value is in KB */
3080 if (val != global_trace.entries) {
3081 ret = tracing_resize_ring_buffer(val);
3090 /* If check pages failed, return ENOMEM */
3091 if (tracing_disabled)
3094 for_each_tracing_cpu(cpu) {
3095 if (global_trace.data[cpu])
3096 atomic_dec(&global_trace.data[cpu]->disabled);
3097 if (max_tr.data[cpu])
3098 atomic_dec(&max_tr.data[cpu]->disabled);
3102 max_tr.entries = global_trace.entries;
3103 mutex_unlock(&trace_types_lock);
3108 static int mark_printk(const char *fmt, ...)
3112 va_start(args, fmt);
3113 ret = trace_vprintk(0, fmt, args);
3119 tracing_mark_write(struct file *filp, const char __user *ubuf,
3120 size_t cnt, loff_t *fpos)
3125 if (tracing_disabled)
3128 if (cnt > TRACE_BUF_SIZE)
3129 cnt = TRACE_BUF_SIZE;
3131 buf = kmalloc(cnt + 1, GFP_KERNEL);
3135 if (copy_from_user(buf, ubuf, cnt)) {
3140 /* Cut from the first nil or newline. */
3142 end = strchr(buf, '\n');
3146 cnt = mark_printk("%s\n", buf);
3153 static ssize_t tracing_clock_read(struct file *filp, char __user *ubuf,
3154 size_t cnt, loff_t *ppos)
3160 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3161 bufiter += snprintf(buf + bufiter, sizeof(buf) - bufiter,
3162 "%s%s%s%s", i ? " " : "",
3163 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3164 i == trace_clock_id ? "]" : "");
3165 bufiter += snprintf(buf + bufiter, sizeof(buf) - bufiter, "\n");
3167 return simple_read_from_buffer(ubuf, cnt, ppos, buf, bufiter);
3170 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3171 size_t cnt, loff_t *fpos)
3174 const char *clockstr;
3177 if (cnt >= sizeof(buf))
3180 if (copy_from_user(&buf, ubuf, cnt))
3185 clockstr = strstrip(buf);
3187 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3188 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3191 if (i == ARRAY_SIZE(trace_clocks))
3196 mutex_lock(&trace_types_lock);
3198 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3200 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3202 mutex_unlock(&trace_types_lock);
3209 static const struct file_operations tracing_max_lat_fops = {
3210 .open = tracing_open_generic,
3211 .read = tracing_max_lat_read,
3212 .write = tracing_max_lat_write,
3215 static const struct file_operations tracing_ctrl_fops = {
3216 .open = tracing_open_generic,
3217 .read = tracing_ctrl_read,
3218 .write = tracing_ctrl_write,
3221 static const struct file_operations set_tracer_fops = {
3222 .open = tracing_open_generic,
3223 .read = tracing_set_trace_read,
3224 .write = tracing_set_trace_write,
3227 static const struct file_operations tracing_pipe_fops = {
3228 .open = tracing_open_pipe,
3229 .poll = tracing_poll_pipe,
3230 .read = tracing_read_pipe,
3231 .splice_read = tracing_splice_read_pipe,
3232 .release = tracing_release_pipe,
3235 static const struct file_operations tracing_entries_fops = {
3236 .open = tracing_open_generic,
3237 .read = tracing_entries_read,
3238 .write = tracing_entries_write,
3241 static const struct file_operations tracing_mark_fops = {
3242 .open = tracing_open_generic,
3243 .write = tracing_mark_write,
3246 static const struct file_operations trace_clock_fops = {
3247 .open = tracing_open_generic,
3248 .read = tracing_clock_read,
3249 .write = tracing_clock_write,
3252 struct ftrace_buffer_info {
3253 struct trace_array *tr;
3259 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3261 int cpu = (int)(long)inode->i_private;
3262 struct ftrace_buffer_info *info;
3264 if (tracing_disabled)
3267 info = kzalloc(sizeof(*info), GFP_KERNEL);
3271 info->tr = &global_trace;
3274 /* Force reading ring buffer for first read */
3275 info->read = (unsigned int)-1;
3277 filp->private_data = info;
3279 return nonseekable_open(inode, filp);
3283 tracing_buffers_read(struct file *filp, char __user *ubuf,
3284 size_t count, loff_t *ppos)
3286 struct ftrace_buffer_info *info = filp->private_data;
3295 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3299 /* Do we have previous read data to read? */
3300 if (info->read < PAGE_SIZE)
3305 ret = ring_buffer_read_page(info->tr->buffer,
3312 pos = ring_buffer_page_len(info->spare);
3314 if (pos < PAGE_SIZE)
3315 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3318 size = PAGE_SIZE - info->read;
3322 ret = copy_to_user(ubuf, info->spare + info->read, size);
3333 static int tracing_buffers_release(struct inode *inode, struct file *file)
3335 struct ftrace_buffer_info *info = file->private_data;
3338 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3345 struct ring_buffer *buffer;
3350 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3351 struct pipe_buffer *buf)
3353 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3358 ring_buffer_free_read_page(ref->buffer, ref->page);
3363 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3364 struct pipe_buffer *buf)
3369 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3370 struct pipe_buffer *buf)
3372 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3377 /* Pipe buffer operations for a buffer. */
3378 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3380 .map = generic_pipe_buf_map,
3381 .unmap = generic_pipe_buf_unmap,
3382 .confirm = generic_pipe_buf_confirm,
3383 .release = buffer_pipe_buf_release,
3384 .steal = buffer_pipe_buf_steal,
3385 .get = buffer_pipe_buf_get,
3389 * Callback from splice_to_pipe(), if we need to release some pages
3390 * at the end of the spd in case we error'ed out in filling the pipe.
3392 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3394 struct buffer_ref *ref =
3395 (struct buffer_ref *)spd->partial[i].private;
3400 ring_buffer_free_read_page(ref->buffer, ref->page);
3402 spd->partial[i].private = 0;
3406 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3407 struct pipe_inode_info *pipe, size_t len,
3410 struct ftrace_buffer_info *info = file->private_data;
3411 struct partial_page partial[PIPE_BUFFERS];
3412 struct page *pages[PIPE_BUFFERS];
3413 struct splice_pipe_desc spd = {
3417 .ops = &buffer_pipe_buf_ops,
3418 .spd_release = buffer_spd_release,
3420 struct buffer_ref *ref;
3421 int entries, size, i;
3424 if (*ppos & (PAGE_SIZE - 1)) {
3425 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3429 if (len & (PAGE_SIZE - 1)) {
3430 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3431 if (len < PAGE_SIZE)
3436 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3438 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
3442 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3447 ref->buffer = info->tr->buffer;
3448 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3454 r = ring_buffer_read_page(ref->buffer, &ref->page,
3457 ring_buffer_free_read_page(ref->buffer,
3464 * zero out any left over data, this is going to
3467 size = ring_buffer_page_len(ref->page);
3468 if (size < PAGE_SIZE)
3469 memset(ref->page + size, 0, PAGE_SIZE - size);
3471 page = virt_to_page(ref->page);
3473 spd.pages[i] = page;
3474 spd.partial[i].len = PAGE_SIZE;
3475 spd.partial[i].offset = 0;
3476 spd.partial[i].private = (unsigned long)ref;
3480 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3485 /* did we read anything? */
3486 if (!spd.nr_pages) {
3487 if (flags & SPLICE_F_NONBLOCK)
3495 ret = splice_to_pipe(pipe, &spd);
3500 static const struct file_operations tracing_buffers_fops = {
3501 .open = tracing_buffers_open,
3502 .read = tracing_buffers_read,
3503 .release = tracing_buffers_release,
3504 .splice_read = tracing_buffers_splice_read,
3505 .llseek = no_llseek,
3509 tracing_stats_read(struct file *filp, char __user *ubuf,
3510 size_t count, loff_t *ppos)
3512 unsigned long cpu = (unsigned long)filp->private_data;
3513 struct trace_array *tr = &global_trace;
3514 struct trace_seq *s;
3517 s = kmalloc(sizeof(*s), GFP_KERNEL);
3523 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3524 trace_seq_printf(s, "entries: %ld\n", cnt);
3526 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3527 trace_seq_printf(s, "overrun: %ld\n", cnt);
3529 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3530 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3532 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3539 static const struct file_operations tracing_stats_fops = {
3540 .open = tracing_open_generic,
3541 .read = tracing_stats_read,
3544 #ifdef CONFIG_DYNAMIC_FTRACE
3546 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3552 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3553 size_t cnt, loff_t *ppos)
3555 static char ftrace_dyn_info_buffer[1024];
3556 static DEFINE_MUTEX(dyn_info_mutex);
3557 unsigned long *p = filp->private_data;
3558 char *buf = ftrace_dyn_info_buffer;
3559 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3562 mutex_lock(&dyn_info_mutex);
3563 r = sprintf(buf, "%ld ", *p);
3565 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3568 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3570 mutex_unlock(&dyn_info_mutex);
3575 static const struct file_operations tracing_dyn_info_fops = {
3576 .open = tracing_open_generic,
3577 .read = tracing_read_dyn_info,
3581 static struct dentry *d_tracer;
3583 struct dentry *tracing_init_dentry(void)
3590 if (!debugfs_initialized())
3593 d_tracer = debugfs_create_dir("tracing", NULL);
3595 if (!d_tracer && !once) {
3597 pr_warning("Could not create debugfs directory 'tracing'\n");
3604 static struct dentry *d_percpu;
3606 struct dentry *tracing_dentry_percpu(void)
3609 struct dentry *d_tracer;
3614 d_tracer = tracing_init_dentry();
3619 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3621 if (!d_percpu && !once) {
3623 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3630 static void tracing_init_debugfs_percpu(long cpu)
3632 struct dentry *d_percpu = tracing_dentry_percpu();
3633 struct dentry *d_cpu;
3634 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3637 if (cpu > 999 || cpu < 0)
3640 sprintf(cpu_dir, "cpu%ld", cpu);
3641 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3643 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3647 /* per cpu trace_pipe */
3648 trace_create_file("trace_pipe", 0444, d_cpu,
3649 (void *) cpu, &tracing_pipe_fops);
3652 trace_create_file("trace", 0644, d_cpu,
3653 (void *) cpu, &tracing_fops);
3655 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3656 (void *) cpu, &tracing_buffers_fops);
3658 trace_create_file("stats", 0444, d_cpu,
3659 (void *) cpu, &tracing_stats_fops);
3662 #ifdef CONFIG_FTRACE_SELFTEST
3663 /* Let selftest have access to static functions in this file */
3664 #include "trace_selftest.c"
3667 struct trace_option_dentry {
3668 struct tracer_opt *opt;
3669 struct tracer_flags *flags;
3670 struct dentry *entry;
3674 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3677 struct trace_option_dentry *topt = filp->private_data;
3680 if (topt->flags->val & topt->opt->bit)
3685 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3689 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3692 struct trace_option_dentry *topt = filp->private_data;
3697 if (cnt >= sizeof(buf))
3700 if (copy_from_user(&buf, ubuf, cnt))
3705 ret = strict_strtoul(buf, 10, &val);
3712 /* do nothing if already cleared */
3713 if (!(topt->flags->val & topt->opt->bit))
3716 mutex_lock(&trace_types_lock);
3717 if (current_trace->set_flag)
3718 ret = current_trace->set_flag(topt->flags->val,
3720 mutex_unlock(&trace_types_lock);
3723 topt->flags->val &= ~topt->opt->bit;
3726 /* do nothing if already set */
3727 if (topt->flags->val & topt->opt->bit)
3730 mutex_lock(&trace_types_lock);
3731 if (current_trace->set_flag)
3732 ret = current_trace->set_flag(topt->flags->val,
3734 mutex_unlock(&trace_types_lock);
3737 topt->flags->val |= topt->opt->bit;
3750 static const struct file_operations trace_options_fops = {
3751 .open = tracing_open_generic,
3752 .read = trace_options_read,
3753 .write = trace_options_write,
3757 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3760 long index = (long)filp->private_data;
3763 if (trace_flags & (1 << index))
3768 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3772 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3775 long index = (long)filp->private_data;
3780 if (cnt >= sizeof(buf))
3783 if (copy_from_user(&buf, ubuf, cnt))
3788 ret = strict_strtoul(buf, 10, &val);
3794 trace_flags &= ~(1 << index);
3797 trace_flags |= 1 << index;
3809 static const struct file_operations trace_options_core_fops = {
3810 .open = tracing_open_generic,
3811 .read = trace_options_core_read,
3812 .write = trace_options_core_write,
3815 struct dentry *trace_create_file(const char *name,
3817 struct dentry *parent,
3819 const struct file_operations *fops)
3823 ret = debugfs_create_file(name, mode, parent, data, fops);
3825 pr_warning("Could not create debugfs '%s' entry\n", name);
3831 static struct dentry *trace_options_init_dentry(void)
3833 struct dentry *d_tracer;
3834 static struct dentry *t_options;
3839 d_tracer = tracing_init_dentry();
3843 t_options = debugfs_create_dir("options", d_tracer);
3845 pr_warning("Could not create debugfs directory 'options'\n");
3853 create_trace_option_file(struct trace_option_dentry *topt,
3854 struct tracer_flags *flags,
3855 struct tracer_opt *opt)
3857 struct dentry *t_options;
3859 t_options = trace_options_init_dentry();
3863 topt->flags = flags;
3866 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
3867 &trace_options_fops);
3871 static struct trace_option_dentry *
3872 create_trace_option_files(struct tracer *tracer)
3874 struct trace_option_dentry *topts;
3875 struct tracer_flags *flags;
3876 struct tracer_opt *opts;
3882 flags = tracer->flags;
3884 if (!flags || !flags->opts)
3889 for (cnt = 0; opts[cnt].name; cnt++)
3892 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3896 for (cnt = 0; opts[cnt].name; cnt++)
3897 create_trace_option_file(&topts[cnt], flags,
3904 destroy_trace_option_files(struct trace_option_dentry *topts)
3911 for (cnt = 0; topts[cnt].opt; cnt++) {
3912 if (topts[cnt].entry)
3913 debugfs_remove(topts[cnt].entry);
3919 static struct dentry *
3920 create_trace_option_core_file(const char *option, long index)
3922 struct dentry *t_options;
3924 t_options = trace_options_init_dentry();
3928 return trace_create_file(option, 0644, t_options, (void *)index,
3929 &trace_options_core_fops);
3932 static __init void create_trace_options_dir(void)
3934 struct dentry *t_options;
3937 t_options = trace_options_init_dentry();
3941 for (i = 0; trace_options[i]; i++)
3942 create_trace_option_core_file(trace_options[i], i);
3945 static __init int tracer_init_debugfs(void)
3947 struct dentry *d_tracer;
3950 d_tracer = tracing_init_dentry();
3952 trace_create_file("tracing_enabled", 0644, d_tracer,
3953 &global_trace, &tracing_ctrl_fops);
3955 trace_create_file("trace_options", 0644, d_tracer,
3956 NULL, &tracing_iter_fops);
3958 trace_create_file("tracing_cpumask", 0644, d_tracer,
3959 NULL, &tracing_cpumask_fops);
3961 trace_create_file("trace", 0644, d_tracer,
3962 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3964 trace_create_file("available_tracers", 0444, d_tracer,
3965 &global_trace, &show_traces_fops);
3967 trace_create_file("current_tracer", 0644, d_tracer,
3968 &global_trace, &set_tracer_fops);
3970 #ifdef CONFIG_TRACER_MAX_TRACE
3971 trace_create_file("tracing_max_latency", 0644, d_tracer,
3972 &tracing_max_latency, &tracing_max_lat_fops);
3974 trace_create_file("tracing_thresh", 0644, d_tracer,
3975 &tracing_thresh, &tracing_max_lat_fops);
3978 trace_create_file("README", 0444, d_tracer,
3979 NULL, &tracing_readme_fops);
3981 trace_create_file("trace_pipe", 0444, d_tracer,
3982 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3984 trace_create_file("buffer_size_kb", 0644, d_tracer,
3985 &global_trace, &tracing_entries_fops);
3987 trace_create_file("trace_marker", 0220, d_tracer,
3988 NULL, &tracing_mark_fops);
3990 trace_create_file("saved_cmdlines", 0444, d_tracer,
3991 NULL, &tracing_saved_cmdlines_fops);
3993 trace_create_file("trace_clock", 0644, d_tracer, NULL,
3996 #ifdef CONFIG_DYNAMIC_FTRACE
3997 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3998 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4000 #ifdef CONFIG_SYSPROF_TRACER
4001 init_tracer_sysprof_debugfs(d_tracer);
4004 create_trace_options_dir();
4006 for_each_tracing_cpu(cpu)
4007 tracing_init_debugfs_percpu(cpu);
4012 static int trace_panic_handler(struct notifier_block *this,
4013 unsigned long event, void *unused)
4015 if (ftrace_dump_on_oops)
4020 static struct notifier_block trace_panic_notifier = {
4021 .notifier_call = trace_panic_handler,
4023 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4026 static int trace_die_handler(struct notifier_block *self,
4032 if (ftrace_dump_on_oops)
4041 static struct notifier_block trace_die_notifier = {
4042 .notifier_call = trace_die_handler,
4047 * printk is set to max of 1024, we really don't need it that big.
4048 * Nothing should be printing 1000 characters anyway.
4050 #define TRACE_MAX_PRINT 1000
4053 * Define here KERN_TRACE so that we have one place to modify
4054 * it if we decide to change what log level the ftrace dump
4057 #define KERN_TRACE KERN_EMERG
4060 trace_printk_seq(struct trace_seq *s)
4062 /* Probably should print a warning here. */
4066 /* should be zero ended, but we are paranoid. */
4067 s->buffer[s->len] = 0;
4069 printk(KERN_TRACE "%s", s->buffer);
4074 static void __ftrace_dump(bool disable_tracing)
4076 static raw_spinlock_t ftrace_dump_lock =
4077 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
4078 /* use static because iter can be a bit big for the stack */
4079 static struct trace_iterator iter;
4080 unsigned int old_userobj;
4081 static int dump_ran;
4082 unsigned long flags;
4086 local_irq_save(flags);
4087 __raw_spin_lock(&ftrace_dump_lock);
4095 if (disable_tracing)
4098 for_each_tracing_cpu(cpu) {
4099 atomic_inc(&global_trace.data[cpu]->disabled);
4102 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4104 /* don't look at user memory in panic mode */
4105 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4107 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4109 /* Simulate the iterator */
4110 iter.tr = &global_trace;
4111 iter.trace = current_trace;
4112 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4115 * We need to stop all tracing on all CPUS to read the
4116 * the next buffer. This is a bit expensive, but is
4117 * not done often. We fill all what we can read,
4118 * and then release the locks again.
4121 while (!trace_empty(&iter)) {
4124 printk(KERN_TRACE "---------------------------------\n");
4128 /* reset all but tr, trace, and overruns */
4129 memset(&iter.seq, 0,
4130 sizeof(struct trace_iterator) -
4131 offsetof(struct trace_iterator, seq));
4132 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4135 if (find_next_entry_inc(&iter) != NULL) {
4138 ret = print_trace_line(&iter);
4139 if (ret != TRACE_TYPE_NO_CONSUME)
4140 trace_consume(&iter);
4143 trace_printk_seq(&iter.seq);
4147 printk(KERN_TRACE " (ftrace buffer empty)\n");
4149 printk(KERN_TRACE "---------------------------------\n");
4151 /* Re-enable tracing if requested */
4152 if (!disable_tracing) {
4153 trace_flags |= old_userobj;
4155 for_each_tracing_cpu(cpu) {
4156 atomic_dec(&global_trace.data[cpu]->disabled);
4162 __raw_spin_unlock(&ftrace_dump_lock);
4163 local_irq_restore(flags);
4166 /* By default: disable tracing after the dump */
4167 void ftrace_dump(void)
4169 __ftrace_dump(true);
4172 __init static int tracer_alloc_buffers(void)
4178 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4181 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4182 goto out_free_buffer_mask;
4184 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4185 goto out_free_tracing_cpumask;
4187 /* To save memory, keep the ring buffer size to its minimum */
4188 if (ring_buffer_expanded)
4189 ring_buf_size = trace_buf_size;
4193 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4194 cpumask_copy(tracing_cpumask, cpu_all_mask);
4195 cpumask_clear(tracing_reader_cpumask);
4197 /* TODO: make the number of buffers hot pluggable with CPUS */
4198 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4199 TRACE_BUFFER_FLAGS);
4200 if (!global_trace.buffer) {
4201 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4203 goto out_free_cpumask;
4205 global_trace.entries = ring_buffer_size(global_trace.buffer);
4208 #ifdef CONFIG_TRACER_MAX_TRACE
4209 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4210 TRACE_BUFFER_FLAGS);
4211 if (!max_tr.buffer) {
4212 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4214 ring_buffer_free(global_trace.buffer);
4215 goto out_free_cpumask;
4217 max_tr.entries = ring_buffer_size(max_tr.buffer);
4218 WARN_ON(max_tr.entries != global_trace.entries);
4221 /* Allocate the first page for all buffers */
4222 for_each_tracing_cpu(i) {
4223 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4224 max_tr.data[i] = &per_cpu(max_data, i);
4227 trace_init_cmdlines();
4229 register_tracer(&nop_trace);
4230 current_trace = &nop_trace;
4231 #ifdef CONFIG_BOOT_TRACER
4232 register_tracer(&boot_tracer);
4234 /* All seems OK, enable tracing */
4235 tracing_disabled = 0;
4237 atomic_notifier_chain_register(&panic_notifier_list,
4238 &trace_panic_notifier);
4240 register_die_notifier(&trace_die_notifier);
4245 free_cpumask_var(tracing_reader_cpumask);
4246 out_free_tracing_cpumask:
4247 free_cpumask_var(tracing_cpumask);
4248 out_free_buffer_mask:
4249 free_cpumask_var(tracing_buffer_mask);
4254 __init static int clear_boot_tracer(void)
4257 * The default tracer at boot buffer is an init section.
4258 * This function is called in lateinit. If we did not
4259 * find the boot tracer, then clear it out, to prevent
4260 * later registration from accessing the buffer that is
4261 * about to be freed.
4263 if (!default_bootup_tracer)
4266 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4267 default_bootup_tracer);
4268 default_bootup_tracer = NULL;
4273 early_initcall(tracer_alloc_buffers);
4274 fs_initcall(tracer_init_debugfs);
4275 late_initcall(clear_boot_tracer);