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 <generated/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/rwsem.h>
35 #include <linux/slab.h>
36 #include <linux/ctype.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
39 #include <linux/nmi.h>
43 #include "trace_output.h"
46 * On boot up, the ring buffer is set to the minimum size, so that
47 * we do not waste memory on systems that are not using tracing.
49 int ring_buffer_expanded;
52 * We need to change this state when a selftest is running.
53 * A selftest will lurk into the ring-buffer to count the
54 * entries inserted during the selftest although some concurrent
55 * insertions into the ring-buffer such as trace_printk could occurred
56 * at the same time, giving false positive or negative results.
58 static bool __read_mostly tracing_selftest_running;
61 * If a tracer is running, we do not want to run SELFTEST.
63 bool __read_mostly tracing_selftest_disabled;
65 /* For tracers that don't implement custom flags */
66 static struct tracer_opt dummy_tracer_opt[] = {
70 static struct tracer_flags dummy_tracer_flags = {
72 .opts = dummy_tracer_opt
75 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
81 * Kill all tracing for good (never come back).
82 * It is initialized to 1 but will turn to zero if the initialization
83 * of the tracer is successful. But that is the only place that sets
86 static int tracing_disabled = 1;
88 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
90 static inline void ftrace_disable_cpu(void)
93 __this_cpu_inc(ftrace_cpu_disabled);
96 static inline void ftrace_enable_cpu(void)
98 __this_cpu_dec(ftrace_cpu_disabled);
102 cpumask_var_t __read_mostly tracing_buffer_mask;
105 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
107 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
108 * is set, then ftrace_dump is called. This will output the contents
109 * of the ftrace buffers to the console. This is very useful for
110 * capturing traces that lead to crashes and outputing it to a
113 * It is default off, but you can enable it with either specifying
114 * "ftrace_dump_on_oops" in the kernel command line, or setting
115 * /proc/sys/kernel/ftrace_dump_on_oops
116 * Set 1 if you want to dump buffers of all CPUs
117 * Set 2 if you want to dump the buffer of the CPU that triggered oops
120 enum ftrace_dump_mode ftrace_dump_on_oops;
122 static int tracing_set_tracer(const char *buf);
124 #define MAX_TRACER_SIZE 100
125 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
126 static char *default_bootup_tracer;
128 static int __init set_cmdline_ftrace(char *str)
130 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
131 default_bootup_tracer = bootup_tracer_buf;
132 /* We are using ftrace early, expand it */
133 ring_buffer_expanded = 1;
136 __setup("ftrace=", set_cmdline_ftrace);
138 static int __init set_ftrace_dump_on_oops(char *str)
140 if (*str++ != '=' || !*str) {
141 ftrace_dump_on_oops = DUMP_ALL;
145 if (!strcmp("orig_cpu", str)) {
146 ftrace_dump_on_oops = DUMP_ORIG;
152 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
154 unsigned long long ns2usecs(cycle_t nsec)
162 * The global_trace is the descriptor that holds the tracing
163 * buffers for the live tracing. For each CPU, it contains
164 * a link list of pages that will store trace entries. The
165 * page descriptor of the pages in the memory is used to hold
166 * the link list by linking the lru item in the page descriptor
167 * to each of the pages in the buffer per CPU.
169 * For each active CPU there is a data field that holds the
170 * pages for the buffer for that CPU. Each CPU has the same number
171 * of pages allocated for its buffer.
173 static struct trace_array global_trace;
175 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
177 int filter_current_check_discard(struct ring_buffer *buffer,
178 struct ftrace_event_call *call, void *rec,
179 struct ring_buffer_event *event)
181 return filter_check_discard(call, rec, buffer, event);
183 EXPORT_SYMBOL_GPL(filter_current_check_discard);
185 cycle_t ftrace_now(int cpu)
189 /* Early boot up does not have a buffer yet */
190 if (!global_trace.buffer)
191 return trace_clock_local();
193 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
194 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
200 * The max_tr is used to snapshot the global_trace when a maximum
201 * latency is reached. Some tracers will use this to store a maximum
202 * trace while it continues examining live traces.
204 * The buffers for the max_tr are set up the same as the global_trace.
205 * When a snapshot is taken, the link list of the max_tr is swapped
206 * with the link list of the global_trace and the buffers are reset for
207 * the global_trace so the tracing can continue.
209 static struct trace_array max_tr;
211 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
213 /* tracer_enabled is used to toggle activation of a tracer */
214 static int tracer_enabled = 1;
217 * tracing_is_enabled - return tracer_enabled status
219 * This function is used by other tracers to know the status
220 * of the tracer_enabled flag. Tracers may use this function
221 * to know if it should enable their features when starting
222 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
224 int tracing_is_enabled(void)
226 return tracer_enabled;
230 * trace_buf_size is the size in bytes that is allocated
231 * for a buffer. Note, the number of bytes is always rounded
234 * This number is purposely set to a low number of 16384.
235 * If the dump on oops happens, it will be much appreciated
236 * to not have to wait for all that output. Anyway this can be
237 * boot time and run time configurable.
239 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
241 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
243 /* trace_types holds a link list of available tracers. */
244 static struct tracer *trace_types __read_mostly;
246 /* current_trace points to the tracer that is currently active */
247 static struct tracer *current_trace __read_mostly;
250 * trace_types_lock is used to protect the trace_types list.
252 static DEFINE_MUTEX(trace_types_lock);
255 * serialize the access of the ring buffer
257 * ring buffer serializes readers, but it is low level protection.
258 * The validity of the events (which returns by ring_buffer_peek() ..etc)
259 * are not protected by ring buffer.
261 * The content of events may become garbage if we allow other process consumes
262 * these events concurrently:
263 * A) the page of the consumed events may become a normal page
264 * (not reader page) in ring buffer, and this page will be rewrited
265 * by events producer.
266 * B) The page of the consumed events may become a page for splice_read,
267 * and this page will be returned to system.
269 * These primitives allow multi process access to different cpu ring buffer
272 * These primitives don't distinguish read-only and read-consume access.
273 * Multi read-only access are also serialized.
277 static DECLARE_RWSEM(all_cpu_access_lock);
278 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
280 static inline void trace_access_lock(int cpu)
282 if (cpu == TRACE_PIPE_ALL_CPU) {
283 /* gain it for accessing the whole ring buffer. */
284 down_write(&all_cpu_access_lock);
286 /* gain it for accessing a cpu ring buffer. */
288 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
289 down_read(&all_cpu_access_lock);
291 /* Secondly block other access to this @cpu ring buffer. */
292 mutex_lock(&per_cpu(cpu_access_lock, cpu));
296 static inline void trace_access_unlock(int cpu)
298 if (cpu == TRACE_PIPE_ALL_CPU) {
299 up_write(&all_cpu_access_lock);
301 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
302 up_read(&all_cpu_access_lock);
306 static inline void trace_access_lock_init(void)
310 for_each_possible_cpu(cpu)
311 mutex_init(&per_cpu(cpu_access_lock, cpu));
316 static DEFINE_MUTEX(access_lock);
318 static inline void trace_access_lock(int cpu)
321 mutex_lock(&access_lock);
324 static inline void trace_access_unlock(int cpu)
327 mutex_unlock(&access_lock);
330 static inline void trace_access_lock_init(void)
336 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
337 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
339 /* trace_flags holds trace_options default values */
340 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
341 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
342 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
345 static int trace_stop_count;
346 static DEFINE_RAW_SPINLOCK(tracing_start_lock);
348 static void wakeup_work_handler(struct work_struct *work)
350 wake_up(&trace_wait);
353 static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler);
356 * tracing_on - enable tracing buffers
358 * This function enables tracing buffers that may have been
359 * disabled with tracing_off.
361 void tracing_on(void)
363 if (global_trace.buffer)
364 ring_buffer_record_on(global_trace.buffer);
366 * This flag is only looked at when buffers haven't been
367 * allocated yet. We don't really care about the race
368 * between setting this flag and actually turning
371 global_trace.buffer_disabled = 0;
373 EXPORT_SYMBOL_GPL(tracing_on);
376 * tracing_off - turn off tracing buffers
378 * This function stops the tracing buffers from recording data.
379 * It does not disable any overhead the tracers themselves may
380 * be causing. This function simply causes all recording to
381 * the ring buffers to fail.
383 void tracing_off(void)
385 if (global_trace.buffer)
386 ring_buffer_record_on(global_trace.buffer);
388 * This flag is only looked at when buffers haven't been
389 * allocated yet. We don't really care about the race
390 * between setting this flag and actually turning
393 global_trace.buffer_disabled = 1;
395 EXPORT_SYMBOL_GPL(tracing_off);
398 * tracing_is_on - show state of ring buffers enabled
400 int tracing_is_on(void)
402 if (global_trace.buffer)
403 return ring_buffer_record_is_on(global_trace.buffer);
404 return !global_trace.buffer_disabled;
406 EXPORT_SYMBOL_GPL(tracing_is_on);
409 * trace_wake_up - wake up tasks waiting for trace input
411 * Schedules a delayed work to wake up any task that is blocked on the
412 * trace_wait queue. These is used with trace_poll for tasks polling the
415 void trace_wake_up(void)
417 const unsigned long delay = msecs_to_jiffies(2);
419 if (trace_flags & TRACE_ITER_BLOCK)
421 schedule_delayed_work(&wakeup_work, delay);
424 static int __init set_buf_size(char *str)
426 unsigned long buf_size;
430 buf_size = memparse(str, &str);
431 /* nr_entries can not be zero */
434 trace_buf_size = buf_size;
437 __setup("trace_buf_size=", set_buf_size);
439 static int __init set_tracing_thresh(char *str)
441 unsigned long threshhold;
446 ret = strict_strtoul(str, 0, &threshhold);
449 tracing_thresh = threshhold * 1000;
452 __setup("tracing_thresh=", set_tracing_thresh);
454 unsigned long nsecs_to_usecs(unsigned long nsecs)
459 /* These must match the bit postions in trace_iterator_flags */
460 static const char *trace_options[] = {
492 { trace_clock_local, "local" },
493 { trace_clock_global, "global" },
494 { trace_clock_counter, "counter" },
500 * trace_parser_get_init - gets the buffer for trace parser
502 int trace_parser_get_init(struct trace_parser *parser, int size)
504 memset(parser, 0, sizeof(*parser));
506 parser->buffer = kmalloc(size, GFP_KERNEL);
515 * trace_parser_put - frees the buffer for trace parser
517 void trace_parser_put(struct trace_parser *parser)
519 kfree(parser->buffer);
523 * trace_get_user - reads the user input string separated by space
524 * (matched by isspace(ch))
526 * For each string found the 'struct trace_parser' is updated,
527 * and the function returns.
529 * Returns number of bytes read.
531 * See kernel/trace/trace.h for 'struct trace_parser' details.
533 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
534 size_t cnt, loff_t *ppos)
541 trace_parser_clear(parser);
543 ret = get_user(ch, ubuf++);
551 * The parser is not finished with the last write,
552 * continue reading the user input without skipping spaces.
555 /* skip white space */
556 while (cnt && isspace(ch)) {
557 ret = get_user(ch, ubuf++);
564 /* only spaces were written */
574 /* read the non-space input */
575 while (cnt && !isspace(ch)) {
576 if (parser->idx < parser->size - 1)
577 parser->buffer[parser->idx++] = ch;
582 ret = get_user(ch, ubuf++);
589 /* We either got finished input or we have to wait for another call. */
591 parser->buffer[parser->idx] = 0;
592 parser->cont = false;
595 parser->buffer[parser->idx++] = ch;
605 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
613 if (s->len <= s->readpos)
616 len = s->len - s->readpos;
619 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
629 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
633 if (s->len <= s->readpos)
636 len = s->len - s->readpos;
639 memcpy(buf, s->buffer + s->readpos, cnt);
646 * ftrace_max_lock is used to protect the swapping of buffers
647 * when taking a max snapshot. The buffers themselves are
648 * protected by per_cpu spinlocks. But the action of the swap
649 * needs its own lock.
651 * This is defined as a arch_spinlock_t in order to help
652 * with performance when lockdep debugging is enabled.
654 * It is also used in other places outside the update_max_tr
655 * so it needs to be defined outside of the
656 * CONFIG_TRACER_MAX_TRACE.
658 static arch_spinlock_t ftrace_max_lock =
659 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
661 unsigned long __read_mostly tracing_thresh;
663 #ifdef CONFIG_TRACER_MAX_TRACE
664 unsigned long __read_mostly tracing_max_latency;
667 * Copy the new maximum trace into the separate maximum-trace
668 * structure. (this way the maximum trace is permanently saved,
669 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
672 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
674 struct trace_array_cpu *data = tr->data[cpu];
675 struct trace_array_cpu *max_data;
678 max_tr.time_start = data->preempt_timestamp;
680 max_data = max_tr.data[cpu];
681 max_data->saved_latency = tracing_max_latency;
682 max_data->critical_start = data->critical_start;
683 max_data->critical_end = data->critical_end;
685 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
686 max_data->pid = tsk->pid;
687 max_data->uid = task_uid(tsk);
688 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
689 max_data->policy = tsk->policy;
690 max_data->rt_priority = tsk->rt_priority;
692 /* record this tasks comm */
693 tracing_record_cmdline(tsk);
697 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
699 * @tsk: the task with the latency
700 * @cpu: The cpu that initiated the trace.
702 * Flip the buffers between the @tr and the max_tr and record information
703 * about which task was the cause of this latency.
706 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
708 struct ring_buffer *buf = tr->buffer;
710 if (trace_stop_count)
713 WARN_ON_ONCE(!irqs_disabled());
714 if (!current_trace->use_max_tr) {
718 arch_spin_lock(&ftrace_max_lock);
720 tr->buffer = max_tr.buffer;
723 __update_max_tr(tr, tsk, cpu);
724 arch_spin_unlock(&ftrace_max_lock);
728 * update_max_tr_single - only copy one trace over, and reset the rest
730 * @tsk - task with the latency
731 * @cpu - the cpu of the buffer to copy.
733 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
736 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
740 if (trace_stop_count)
743 WARN_ON_ONCE(!irqs_disabled());
744 if (!current_trace->use_max_tr) {
749 arch_spin_lock(&ftrace_max_lock);
751 ftrace_disable_cpu();
753 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
757 * We failed to swap the buffer due to a commit taking
758 * place on this CPU. We fail to record, but we reset
759 * the max trace buffer (no one writes directly to it)
760 * and flag that it failed.
762 trace_array_printk(&max_tr, _THIS_IP_,
763 "Failed to swap buffers due to commit in progress\n");
768 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
770 __update_max_tr(tr, tsk, cpu);
771 arch_spin_unlock(&ftrace_max_lock);
773 #endif /* CONFIG_TRACER_MAX_TRACE */
776 * register_tracer - register a tracer with the ftrace system.
777 * @type - the plugin for the tracer
779 * Register a new plugin tracer.
781 int register_tracer(struct tracer *type)
782 __releases(kernel_lock)
783 __acquires(kernel_lock)
789 pr_info("Tracer must have a name\n");
793 if (strlen(type->name) >= MAX_TRACER_SIZE) {
794 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
798 mutex_lock(&trace_types_lock);
800 tracing_selftest_running = true;
802 for (t = trace_types; t; t = t->next) {
803 if (strcmp(type->name, t->name) == 0) {
805 pr_info("Tracer %s already registered\n",
813 type->set_flag = &dummy_set_flag;
815 type->flags = &dummy_tracer_flags;
817 if (!type->flags->opts)
818 type->flags->opts = dummy_tracer_opt;
819 if (!type->wait_pipe)
820 type->wait_pipe = default_wait_pipe;
823 #ifdef CONFIG_FTRACE_STARTUP_TEST
824 if (type->selftest && !tracing_selftest_disabled) {
825 struct tracer *saved_tracer = current_trace;
826 struct trace_array *tr = &global_trace;
829 * Run a selftest on this tracer.
830 * Here we reset the trace buffer, and set the current
831 * tracer to be this tracer. The tracer can then run some
832 * internal tracing to verify that everything is in order.
833 * If we fail, we do not register this tracer.
835 tracing_reset_online_cpus(tr);
837 current_trace = type;
839 /* If we expanded the buffers, make sure the max is expanded too */
840 if (ring_buffer_expanded && type->use_max_tr)
841 ring_buffer_resize(max_tr.buffer, trace_buf_size,
842 RING_BUFFER_ALL_CPUS);
844 /* the test is responsible for initializing and enabling */
845 pr_info("Testing tracer %s: ", type->name);
846 ret = type->selftest(type, tr);
847 /* the test is responsible for resetting too */
848 current_trace = saved_tracer;
850 printk(KERN_CONT "FAILED!\n");
853 /* Only reset on passing, to avoid touching corrupted buffers */
854 tracing_reset_online_cpus(tr);
856 /* Shrink the max buffer again */
857 if (ring_buffer_expanded && type->use_max_tr)
858 ring_buffer_resize(max_tr.buffer, 1,
859 RING_BUFFER_ALL_CPUS);
861 printk(KERN_CONT "PASSED\n");
865 type->next = trace_types;
869 tracing_selftest_running = false;
870 mutex_unlock(&trace_types_lock);
872 if (ret || !default_bootup_tracer)
875 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
878 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
879 /* Do we want this tracer to start on bootup? */
880 tracing_set_tracer(type->name);
881 default_bootup_tracer = NULL;
882 /* disable other selftests, since this will break it. */
883 tracing_selftest_disabled = 1;
884 #ifdef CONFIG_FTRACE_STARTUP_TEST
885 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
893 void unregister_tracer(struct tracer *type)
897 mutex_lock(&trace_types_lock);
898 for (t = &trace_types; *t; t = &(*t)->next) {
902 pr_info("Tracer %s not registered\n", type->name);
908 if (type == current_trace && tracer_enabled) {
911 if (current_trace->stop)
912 current_trace->stop(&global_trace);
913 current_trace = &nop_trace;
916 mutex_unlock(&trace_types_lock);
919 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
921 ftrace_disable_cpu();
922 ring_buffer_reset_cpu(buffer, cpu);
926 void tracing_reset(struct trace_array *tr, int cpu)
928 struct ring_buffer *buffer = tr->buffer;
930 ring_buffer_record_disable(buffer);
932 /* Make sure all commits have finished */
934 __tracing_reset(buffer, cpu);
936 ring_buffer_record_enable(buffer);
939 void tracing_reset_online_cpus(struct trace_array *tr)
941 struct ring_buffer *buffer = tr->buffer;
944 ring_buffer_record_disable(buffer);
946 /* Make sure all commits have finished */
949 tr->time_start = ftrace_now(tr->cpu);
951 for_each_online_cpu(cpu)
952 __tracing_reset(buffer, cpu);
954 ring_buffer_record_enable(buffer);
957 void tracing_reset_current(int cpu)
959 tracing_reset(&global_trace, cpu);
962 void tracing_reset_current_online_cpus(void)
964 tracing_reset_online_cpus(&global_trace);
967 #define SAVED_CMDLINES 128
968 #define NO_CMDLINE_MAP UINT_MAX
969 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
970 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
971 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
972 static int cmdline_idx;
973 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
975 /* temporary disable recording */
976 static atomic_t trace_record_cmdline_disabled __read_mostly;
978 static void trace_init_cmdlines(void)
980 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
981 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
985 int is_tracing_stopped(void)
987 return trace_stop_count;
991 * ftrace_off_permanent - disable all ftrace code permanently
993 * This should only be called when a serious anomally has
994 * been detected. This will turn off the function tracing,
995 * ring buffers, and other tracing utilites. It takes no
996 * locks and can be called from any context.
998 void ftrace_off_permanent(void)
1000 tracing_disabled = 1;
1002 tracing_off_permanent();
1006 * tracing_start - quick start of the tracer
1008 * If tracing is enabled but was stopped by tracing_stop,
1009 * this will start the tracer back up.
1011 void tracing_start(void)
1013 struct ring_buffer *buffer;
1014 unsigned long flags;
1016 if (tracing_disabled)
1019 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1020 if (--trace_stop_count) {
1021 if (trace_stop_count < 0) {
1022 /* Someone screwed up their debugging */
1024 trace_stop_count = 0;
1029 /* Prevent the buffers from switching */
1030 arch_spin_lock(&ftrace_max_lock);
1032 buffer = global_trace.buffer;
1034 ring_buffer_record_enable(buffer);
1036 buffer = max_tr.buffer;
1038 ring_buffer_record_enable(buffer);
1040 arch_spin_unlock(&ftrace_max_lock);
1044 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1048 * tracing_stop - quick stop of the tracer
1050 * Light weight way to stop tracing. Use in conjunction with
1053 void tracing_stop(void)
1055 struct ring_buffer *buffer;
1056 unsigned long flags;
1059 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1060 if (trace_stop_count++)
1063 /* Prevent the buffers from switching */
1064 arch_spin_lock(&ftrace_max_lock);
1066 buffer = global_trace.buffer;
1068 ring_buffer_record_disable(buffer);
1070 buffer = max_tr.buffer;
1072 ring_buffer_record_disable(buffer);
1074 arch_spin_unlock(&ftrace_max_lock);
1077 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1080 void trace_stop_cmdline_recording(void);
1082 static void trace_save_cmdline(struct task_struct *tsk)
1086 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1090 * It's not the end of the world if we don't get
1091 * the lock, but we also don't want to spin
1092 * nor do we want to disable interrupts,
1093 * so if we miss here, then better luck next time.
1095 if (!arch_spin_trylock(&trace_cmdline_lock))
1098 idx = map_pid_to_cmdline[tsk->pid];
1099 if (idx == NO_CMDLINE_MAP) {
1100 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1103 * Check whether the cmdline buffer at idx has a pid
1104 * mapped. We are going to overwrite that entry so we
1105 * need to clear the map_pid_to_cmdline. Otherwise we
1106 * would read the new comm for the old pid.
1108 pid = map_cmdline_to_pid[idx];
1109 if (pid != NO_CMDLINE_MAP)
1110 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1112 map_cmdline_to_pid[idx] = tsk->pid;
1113 map_pid_to_cmdline[tsk->pid] = idx;
1118 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1120 arch_spin_unlock(&trace_cmdline_lock);
1123 void trace_find_cmdline(int pid, char comm[])
1128 strcpy(comm, "<idle>");
1132 if (WARN_ON_ONCE(pid < 0)) {
1133 strcpy(comm, "<XXX>");
1137 if (pid > PID_MAX_DEFAULT) {
1138 strcpy(comm, "<...>");
1143 arch_spin_lock(&trace_cmdline_lock);
1144 map = map_pid_to_cmdline[pid];
1145 if (map != NO_CMDLINE_MAP)
1146 strcpy(comm, saved_cmdlines[map]);
1148 strcpy(comm, "<...>");
1150 arch_spin_unlock(&trace_cmdline_lock);
1154 void tracing_record_cmdline(struct task_struct *tsk)
1156 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1160 trace_save_cmdline(tsk);
1164 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1167 struct task_struct *tsk = current;
1169 entry->preempt_count = pc & 0xff;
1170 entry->pid = (tsk) ? tsk->pid : 0;
1173 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1174 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1176 TRACE_FLAG_IRQS_NOSUPPORT |
1178 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1179 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1180 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1182 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1184 struct ring_buffer_event *
1185 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1188 unsigned long flags, int pc)
1190 struct ring_buffer_event *event;
1192 event = ring_buffer_lock_reserve(buffer, len);
1193 if (event != NULL) {
1194 struct trace_entry *ent = ring_buffer_event_data(event);
1196 tracing_generic_entry_update(ent, flags, pc);
1204 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1205 struct ring_buffer_event *event,
1206 unsigned long flags, int pc,
1209 ring_buffer_unlock_commit(buffer, event);
1211 ftrace_trace_stack(buffer, flags, 6, pc);
1212 ftrace_trace_userstack(buffer, flags, pc);
1218 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1219 struct ring_buffer_event *event,
1220 unsigned long flags, int pc)
1222 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1225 struct ring_buffer_event *
1226 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1227 int type, unsigned long len,
1228 unsigned long flags, int pc)
1230 *current_rb = global_trace.buffer;
1231 return trace_buffer_lock_reserve(*current_rb,
1232 type, len, flags, pc);
1234 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1236 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1237 struct ring_buffer_event *event,
1238 unsigned long flags, int pc)
1240 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1242 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1244 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1245 struct ring_buffer_event *event,
1246 unsigned long flags, int pc)
1248 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1250 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1252 void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1253 struct ring_buffer_event *event,
1254 unsigned long flags, int pc,
1255 struct pt_regs *regs)
1257 ring_buffer_unlock_commit(buffer, event);
1259 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1260 ftrace_trace_userstack(buffer, flags, pc);
1262 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs);
1264 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1265 struct ring_buffer_event *event)
1267 ring_buffer_discard_commit(buffer, event);
1269 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1272 trace_function(struct trace_array *tr,
1273 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1276 struct ftrace_event_call *call = &event_function;
1277 struct ring_buffer *buffer = tr->buffer;
1278 struct ring_buffer_event *event;
1279 struct ftrace_entry *entry;
1281 /* If we are reading the ring buffer, don't trace */
1282 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1285 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1289 entry = ring_buffer_event_data(event);
1291 entry->parent_ip = parent_ip;
1293 if (!filter_check_discard(call, entry, buffer, event))
1294 ring_buffer_unlock_commit(buffer, event);
1298 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1299 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1302 if (likely(!atomic_read(&data->disabled)))
1303 trace_function(tr, ip, parent_ip, flags, pc);
1306 #ifdef CONFIG_STACKTRACE
1308 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1309 struct ftrace_stack {
1310 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1313 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1314 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1316 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1317 unsigned long flags,
1318 int skip, int pc, struct pt_regs *regs)
1320 struct ftrace_event_call *call = &event_kernel_stack;
1321 struct ring_buffer_event *event;
1322 struct stack_entry *entry;
1323 struct stack_trace trace;
1325 int size = FTRACE_STACK_ENTRIES;
1327 trace.nr_entries = 0;
1331 * Since events can happen in NMIs there's no safe way to
1332 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1333 * or NMI comes in, it will just have to use the default
1334 * FTRACE_STACK_SIZE.
1336 preempt_disable_notrace();
1338 use_stack = ++__get_cpu_var(ftrace_stack_reserve);
1340 * We don't need any atomic variables, just a barrier.
1341 * If an interrupt comes in, we don't care, because it would
1342 * have exited and put the counter back to what we want.
1343 * We just need a barrier to keep gcc from moving things
1347 if (use_stack == 1) {
1348 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1349 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1352 save_stack_trace_regs(regs, &trace);
1354 save_stack_trace(&trace);
1356 if (trace.nr_entries > size)
1357 size = trace.nr_entries;
1359 /* From now on, use_stack is a boolean */
1362 size *= sizeof(unsigned long);
1364 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1365 sizeof(*entry) + size, flags, pc);
1368 entry = ring_buffer_event_data(event);
1370 memset(&entry->caller, 0, size);
1373 memcpy(&entry->caller, trace.entries,
1374 trace.nr_entries * sizeof(unsigned long));
1376 trace.max_entries = FTRACE_STACK_ENTRIES;
1377 trace.entries = entry->caller;
1379 save_stack_trace_regs(regs, &trace);
1381 save_stack_trace(&trace);
1384 entry->size = trace.nr_entries;
1386 if (!filter_check_discard(call, entry, buffer, event))
1387 ring_buffer_unlock_commit(buffer, event);
1390 /* Again, don't let gcc optimize things here */
1392 __get_cpu_var(ftrace_stack_reserve)--;
1393 preempt_enable_notrace();
1397 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1398 int skip, int pc, struct pt_regs *regs)
1400 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1403 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1406 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1409 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1412 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1415 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1418 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
1422 * trace_dump_stack - record a stack back trace in the trace buffer
1424 void trace_dump_stack(void)
1426 unsigned long flags;
1428 if (tracing_disabled || tracing_selftest_running)
1431 local_save_flags(flags);
1433 /* skipping 3 traces, seems to get us at the caller of this function */
1434 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
1437 static DEFINE_PER_CPU(int, user_stack_count);
1440 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1442 struct ftrace_event_call *call = &event_user_stack;
1443 struct ring_buffer_event *event;
1444 struct userstack_entry *entry;
1445 struct stack_trace trace;
1447 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1451 * NMIs can not handle page faults, even with fix ups.
1452 * The save user stack can (and often does) fault.
1454 if (unlikely(in_nmi()))
1458 * prevent recursion, since the user stack tracing may
1459 * trigger other kernel events.
1462 if (__this_cpu_read(user_stack_count))
1465 __this_cpu_inc(user_stack_count);
1467 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1468 sizeof(*entry), flags, pc);
1470 goto out_drop_count;
1471 entry = ring_buffer_event_data(event);
1473 entry->tgid = current->tgid;
1474 memset(&entry->caller, 0, sizeof(entry->caller));
1476 trace.nr_entries = 0;
1477 trace.max_entries = FTRACE_STACK_ENTRIES;
1479 trace.entries = entry->caller;
1481 save_stack_trace_user(&trace);
1482 if (!filter_check_discard(call, entry, buffer, event))
1483 ring_buffer_unlock_commit(buffer, event);
1486 __this_cpu_dec(user_stack_count);
1492 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1494 ftrace_trace_userstack(tr, flags, preempt_count());
1498 #endif /* CONFIG_STACKTRACE */
1500 /* created for use with alloc_percpu */
1501 struct trace_buffer_struct {
1502 char buffer[TRACE_BUF_SIZE];
1505 static struct trace_buffer_struct *trace_percpu_buffer;
1506 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1507 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1508 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1511 * The buffer used is dependent on the context. There is a per cpu
1512 * buffer for normal context, softirq contex, hard irq context and
1513 * for NMI context. Thise allows for lockless recording.
1515 * Note, if the buffers failed to be allocated, then this returns NULL
1517 static char *get_trace_buf(void)
1519 struct trace_buffer_struct *percpu_buffer;
1520 struct trace_buffer_struct *buffer;
1523 * If we have allocated per cpu buffers, then we do not
1524 * need to do any locking.
1527 percpu_buffer = trace_percpu_nmi_buffer;
1529 percpu_buffer = trace_percpu_irq_buffer;
1530 else if (in_softirq())
1531 percpu_buffer = trace_percpu_sirq_buffer;
1533 percpu_buffer = trace_percpu_buffer;
1538 buffer = per_cpu_ptr(percpu_buffer, smp_processor_id());
1540 return buffer->buffer;
1543 static int alloc_percpu_trace_buffer(void)
1545 struct trace_buffer_struct *buffers;
1546 struct trace_buffer_struct *sirq_buffers;
1547 struct trace_buffer_struct *irq_buffers;
1548 struct trace_buffer_struct *nmi_buffers;
1550 buffers = alloc_percpu(struct trace_buffer_struct);
1554 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1558 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1562 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1566 trace_percpu_buffer = buffers;
1567 trace_percpu_sirq_buffer = sirq_buffers;
1568 trace_percpu_irq_buffer = irq_buffers;
1569 trace_percpu_nmi_buffer = nmi_buffers;
1574 free_percpu(irq_buffers);
1576 free_percpu(sirq_buffers);
1578 free_percpu(buffers);
1580 WARN(1, "Could not allocate percpu trace_printk buffer");
1584 void trace_printk_init_buffers(void)
1586 static int buffers_allocated;
1588 if (buffers_allocated)
1591 if (alloc_percpu_trace_buffer())
1594 pr_info("ftrace: Allocated trace_printk buffers\n");
1596 buffers_allocated = 1;
1600 * trace_vbprintk - write binary msg to tracing buffer
1603 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1605 struct ftrace_event_call *call = &event_bprint;
1606 struct ring_buffer_event *event;
1607 struct ring_buffer *buffer;
1608 struct trace_array *tr = &global_trace;
1609 struct bprint_entry *entry;
1610 unsigned long flags;
1612 int len = 0, size, pc;
1614 if (unlikely(tracing_selftest_running || tracing_disabled))
1617 /* Don't pollute graph traces with trace_vprintk internals */
1618 pause_graph_tracing();
1620 pc = preempt_count();
1621 preempt_disable_notrace();
1623 tbuffer = get_trace_buf();
1629 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1631 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1634 local_save_flags(flags);
1635 size = sizeof(*entry) + sizeof(u32) * len;
1636 buffer = tr->buffer;
1637 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1641 entry = ring_buffer_event_data(event);
1645 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1646 if (!filter_check_discard(call, entry, buffer, event)) {
1647 ring_buffer_unlock_commit(buffer, event);
1648 ftrace_trace_stack(buffer, flags, 6, pc);
1652 preempt_enable_notrace();
1653 unpause_graph_tracing();
1657 EXPORT_SYMBOL_GPL(trace_vbprintk);
1659 int trace_array_printk(struct trace_array *tr,
1660 unsigned long ip, const char *fmt, ...)
1665 if (!(trace_flags & TRACE_ITER_PRINTK))
1669 ret = trace_array_vprintk(tr, ip, fmt, ap);
1674 int trace_array_vprintk(struct trace_array *tr,
1675 unsigned long ip, const char *fmt, va_list args)
1677 struct ftrace_event_call *call = &event_print;
1678 struct ring_buffer_event *event;
1679 struct ring_buffer *buffer;
1680 int len = 0, size, pc;
1681 struct print_entry *entry;
1682 unsigned long flags;
1685 if (tracing_disabled || tracing_selftest_running)
1688 /* Don't pollute graph traces with trace_vprintk internals */
1689 pause_graph_tracing();
1691 pc = preempt_count();
1692 preempt_disable_notrace();
1695 tbuffer = get_trace_buf();
1701 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1702 if (len > TRACE_BUF_SIZE)
1705 local_save_flags(flags);
1706 size = sizeof(*entry) + len + 1;
1707 buffer = tr->buffer;
1708 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1712 entry = ring_buffer_event_data(event);
1715 memcpy(&entry->buf, tbuffer, len);
1716 entry->buf[len] = '\0';
1717 if (!filter_check_discard(call, entry, buffer, event)) {
1718 ring_buffer_unlock_commit(buffer, event);
1719 ftrace_trace_stack(buffer, flags, 6, pc);
1722 preempt_enable_notrace();
1723 unpause_graph_tracing();
1728 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1730 return trace_array_vprintk(&global_trace, ip, fmt, args);
1732 EXPORT_SYMBOL_GPL(trace_vprintk);
1734 static void trace_iterator_increment(struct trace_iterator *iter)
1736 /* Don't allow ftrace to trace into the ring buffers */
1737 ftrace_disable_cpu();
1740 if (iter->buffer_iter[iter->cpu])
1741 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1743 ftrace_enable_cpu();
1746 static struct trace_entry *
1747 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1748 unsigned long *lost_events)
1750 struct ring_buffer_event *event;
1751 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1753 /* Don't allow ftrace to trace into the ring buffers */
1754 ftrace_disable_cpu();
1757 event = ring_buffer_iter_peek(buf_iter, ts);
1759 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1762 ftrace_enable_cpu();
1765 iter->ent_size = ring_buffer_event_length(event);
1766 return ring_buffer_event_data(event);
1772 static struct trace_entry *
1773 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1774 unsigned long *missing_events, u64 *ent_ts)
1776 struct ring_buffer *buffer = iter->tr->buffer;
1777 struct trace_entry *ent, *next = NULL;
1778 unsigned long lost_events = 0, next_lost = 0;
1779 int cpu_file = iter->cpu_file;
1780 u64 next_ts = 0, ts;
1786 * If we are in a per_cpu trace file, don't bother by iterating over
1787 * all cpu and peek directly.
1789 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1790 if (ring_buffer_empty_cpu(buffer, cpu_file))
1792 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1794 *ent_cpu = cpu_file;
1799 for_each_tracing_cpu(cpu) {
1801 if (ring_buffer_empty_cpu(buffer, cpu))
1804 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1807 * Pick the entry with the smallest timestamp:
1809 if (ent && (!next || ts < next_ts)) {
1813 next_lost = lost_events;
1814 next_size = iter->ent_size;
1818 iter->ent_size = next_size;
1821 *ent_cpu = next_cpu;
1827 *missing_events = next_lost;
1832 /* Find the next real entry, without updating the iterator itself */
1833 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1834 int *ent_cpu, u64 *ent_ts)
1836 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1839 /* Find the next real entry, and increment the iterator to the next entry */
1840 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1842 iter->ent = __find_next_entry(iter, &iter->cpu,
1843 &iter->lost_events, &iter->ts);
1846 trace_iterator_increment(iter);
1848 return iter->ent ? iter : NULL;
1851 static void trace_consume(struct trace_iterator *iter)
1853 /* Don't allow ftrace to trace into the ring buffers */
1854 ftrace_disable_cpu();
1855 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1856 &iter->lost_events);
1857 ftrace_enable_cpu();
1860 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1862 struct trace_iterator *iter = m->private;
1866 WARN_ON_ONCE(iter->leftover);
1870 /* can't go backwards */
1875 ent = trace_find_next_entry_inc(iter);
1879 while (ent && iter->idx < i)
1880 ent = trace_find_next_entry_inc(iter);
1887 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1889 struct trace_array *tr = iter->tr;
1890 struct ring_buffer_event *event;
1891 struct ring_buffer_iter *buf_iter;
1892 unsigned long entries = 0;
1895 tr->data[cpu]->skipped_entries = 0;
1897 if (!iter->buffer_iter[cpu])
1900 buf_iter = iter->buffer_iter[cpu];
1901 ring_buffer_iter_reset(buf_iter);
1904 * We could have the case with the max latency tracers
1905 * that a reset never took place on a cpu. This is evident
1906 * by the timestamp being before the start of the buffer.
1908 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1909 if (ts >= iter->tr->time_start)
1912 ring_buffer_read(buf_iter, NULL);
1915 tr->data[cpu]->skipped_entries = entries;
1919 * The current tracer is copied to avoid a global locking
1922 static void *s_start(struct seq_file *m, loff_t *pos)
1924 struct trace_iterator *iter = m->private;
1925 static struct tracer *old_tracer;
1926 int cpu_file = iter->cpu_file;
1931 /* copy the tracer to avoid using a global lock all around */
1932 mutex_lock(&trace_types_lock);
1933 if (unlikely(old_tracer != current_trace && current_trace)) {
1934 old_tracer = current_trace;
1935 *iter->trace = *current_trace;
1937 mutex_unlock(&trace_types_lock);
1939 atomic_inc(&trace_record_cmdline_disabled);
1941 if (*pos != iter->pos) {
1946 ftrace_disable_cpu();
1948 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1949 for_each_tracing_cpu(cpu)
1950 tracing_iter_reset(iter, cpu);
1952 tracing_iter_reset(iter, cpu_file);
1954 ftrace_enable_cpu();
1957 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1962 * If we overflowed the seq_file before, then we want
1963 * to just reuse the trace_seq buffer again.
1969 p = s_next(m, p, &l);
1973 trace_event_read_lock();
1974 trace_access_lock(cpu_file);
1978 static void s_stop(struct seq_file *m, void *p)
1980 struct trace_iterator *iter = m->private;
1982 atomic_dec(&trace_record_cmdline_disabled);
1983 trace_access_unlock(iter->cpu_file);
1984 trace_event_read_unlock();
1988 get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *entries)
1990 unsigned long count;
1996 for_each_tracing_cpu(cpu) {
1997 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1999 * If this buffer has skipped entries, then we hold all
2000 * entries for the trace and we need to ignore the
2001 * ones before the time stamp.
2003 if (tr->data[cpu]->skipped_entries) {
2004 count -= tr->data[cpu]->skipped_entries;
2005 /* total is the same as the entries */
2009 ring_buffer_overrun_cpu(tr->buffer, cpu);
2014 static void print_lat_help_header(struct seq_file *m)
2016 seq_puts(m, "# _------=> CPU# \n");
2017 seq_puts(m, "# / _-----=> irqs-off \n");
2018 seq_puts(m, "# | / _----=> need-resched \n");
2019 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2020 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2021 seq_puts(m, "# |||| / delay \n");
2022 seq_puts(m, "# cmd pid ||||| time | caller \n");
2023 seq_puts(m, "# \\ / ||||| \\ | / \n");
2026 static void print_event_info(struct trace_array *tr, struct seq_file *m)
2028 unsigned long total;
2029 unsigned long entries;
2031 get_total_entries(tr, &total, &entries);
2032 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2033 entries, total, num_online_cpus());
2037 static void print_func_help_header(struct trace_array *tr, struct seq_file *m)
2039 print_event_info(tr, m);
2040 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2041 seq_puts(m, "# | | | | |\n");
2044 static void print_func_help_header_irq(struct trace_array *tr, struct seq_file *m)
2046 print_event_info(tr, m);
2047 seq_puts(m, "# _-----=> irqs-off\n");
2048 seq_puts(m, "# / _----=> need-resched\n");
2049 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2050 seq_puts(m, "# || / _--=> preempt-depth\n");
2051 seq_puts(m, "# ||| / delay\n");
2052 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2053 seq_puts(m, "# | | | |||| | |\n");
2057 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2059 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2060 struct trace_array *tr = iter->tr;
2061 struct trace_array_cpu *data = tr->data[tr->cpu];
2062 struct tracer *type = current_trace;
2063 unsigned long entries;
2064 unsigned long total;
2065 const char *name = "preemption";
2070 get_total_entries(tr, &total, &entries);
2072 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2074 seq_puts(m, "# -----------------------------------"
2075 "---------------------------------\n");
2076 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2077 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2078 nsecs_to_usecs(data->saved_latency),
2082 #if defined(CONFIG_PREEMPT_NONE)
2084 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2086 #elif defined(CONFIG_PREEMPT)
2091 /* These are reserved for later use */
2094 seq_printf(m, " #P:%d)\n", num_online_cpus());
2098 seq_puts(m, "# -----------------\n");
2099 seq_printf(m, "# | task: %.16s-%d "
2100 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2101 data->comm, data->pid, data->uid, data->nice,
2102 data->policy, data->rt_priority);
2103 seq_puts(m, "# -----------------\n");
2105 if (data->critical_start) {
2106 seq_puts(m, "# => started at: ");
2107 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2108 trace_print_seq(m, &iter->seq);
2109 seq_puts(m, "\n# => ended at: ");
2110 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2111 trace_print_seq(m, &iter->seq);
2112 seq_puts(m, "\n#\n");
2118 static void test_cpu_buff_start(struct trace_iterator *iter)
2120 struct trace_seq *s = &iter->seq;
2122 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2125 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2128 if (cpumask_test_cpu(iter->cpu, iter->started))
2131 if (iter->tr->data[iter->cpu]->skipped_entries)
2134 cpumask_set_cpu(iter->cpu, iter->started);
2136 /* Don't print started cpu buffer for the first entry of the trace */
2138 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2142 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2144 struct trace_seq *s = &iter->seq;
2145 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2146 struct trace_entry *entry;
2147 struct trace_event *event;
2151 test_cpu_buff_start(iter);
2153 event = ftrace_find_event(entry->type);
2155 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2156 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2157 if (!trace_print_lat_context(iter))
2160 if (!trace_print_context(iter))
2166 return event->funcs->trace(iter, sym_flags, event);
2168 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2171 return TRACE_TYPE_HANDLED;
2173 return TRACE_TYPE_PARTIAL_LINE;
2176 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2178 struct trace_seq *s = &iter->seq;
2179 struct trace_entry *entry;
2180 struct trace_event *event;
2184 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2185 if (!trace_seq_printf(s, "%d %d %llu ",
2186 entry->pid, iter->cpu, iter->ts))
2190 event = ftrace_find_event(entry->type);
2192 return event->funcs->raw(iter, 0, event);
2194 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2197 return TRACE_TYPE_HANDLED;
2199 return TRACE_TYPE_PARTIAL_LINE;
2202 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2204 struct trace_seq *s = &iter->seq;
2205 unsigned char newline = '\n';
2206 struct trace_entry *entry;
2207 struct trace_event *event;
2211 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2212 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2213 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2214 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2217 event = ftrace_find_event(entry->type);
2219 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2220 if (ret != TRACE_TYPE_HANDLED)
2224 SEQ_PUT_FIELD_RET(s, newline);
2226 return TRACE_TYPE_HANDLED;
2229 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2231 struct trace_seq *s = &iter->seq;
2232 struct trace_entry *entry;
2233 struct trace_event *event;
2237 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2238 SEQ_PUT_FIELD_RET(s, entry->pid);
2239 SEQ_PUT_FIELD_RET(s, iter->cpu);
2240 SEQ_PUT_FIELD_RET(s, iter->ts);
2243 event = ftrace_find_event(entry->type);
2244 return event ? event->funcs->binary(iter, 0, event) :
2248 int trace_empty(struct trace_iterator *iter)
2252 /* If we are looking at one CPU buffer, only check that one */
2253 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2254 cpu = iter->cpu_file;
2255 if (iter->buffer_iter[cpu]) {
2256 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2259 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2265 for_each_tracing_cpu(cpu) {
2266 if (iter->buffer_iter[cpu]) {
2267 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2270 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2278 /* Called with trace_event_read_lock() held. */
2279 enum print_line_t print_trace_line(struct trace_iterator *iter)
2281 enum print_line_t ret;
2283 if (iter->lost_events &&
2284 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2285 iter->cpu, iter->lost_events))
2286 return TRACE_TYPE_PARTIAL_LINE;
2288 if (iter->trace && iter->trace->print_line) {
2289 ret = iter->trace->print_line(iter);
2290 if (ret != TRACE_TYPE_UNHANDLED)
2294 if (iter->ent->type == TRACE_BPRINT &&
2295 trace_flags & TRACE_ITER_PRINTK &&
2296 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2297 return trace_print_bprintk_msg_only(iter);
2299 if (iter->ent->type == TRACE_PRINT &&
2300 trace_flags & TRACE_ITER_PRINTK &&
2301 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2302 return trace_print_printk_msg_only(iter);
2304 if (trace_flags & TRACE_ITER_BIN)
2305 return print_bin_fmt(iter);
2307 if (trace_flags & TRACE_ITER_HEX)
2308 return print_hex_fmt(iter);
2310 if (trace_flags & TRACE_ITER_RAW)
2311 return print_raw_fmt(iter);
2313 return print_trace_fmt(iter);
2316 void trace_latency_header(struct seq_file *m)
2318 struct trace_iterator *iter = m->private;
2320 /* print nothing if the buffers are empty */
2321 if (trace_empty(iter))
2324 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2325 print_trace_header(m, iter);
2327 if (!(trace_flags & TRACE_ITER_VERBOSE))
2328 print_lat_help_header(m);
2331 void trace_default_header(struct seq_file *m)
2333 struct trace_iterator *iter = m->private;
2335 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2338 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2339 /* print nothing if the buffers are empty */
2340 if (trace_empty(iter))
2342 print_trace_header(m, iter);
2343 if (!(trace_flags & TRACE_ITER_VERBOSE))
2344 print_lat_help_header(m);
2346 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2347 if (trace_flags & TRACE_ITER_IRQ_INFO)
2348 print_func_help_header_irq(iter->tr, m);
2350 print_func_help_header(iter->tr, m);
2355 static void test_ftrace_alive(struct seq_file *m)
2357 if (!ftrace_is_dead())
2359 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2360 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2363 static int s_show(struct seq_file *m, void *v)
2365 struct trace_iterator *iter = v;
2368 if (iter->ent == NULL) {
2370 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2372 test_ftrace_alive(m);
2374 if (iter->trace && iter->trace->print_header)
2375 iter->trace->print_header(m);
2377 trace_default_header(m);
2379 } else if (iter->leftover) {
2381 * If we filled the seq_file buffer earlier, we
2382 * want to just show it now.
2384 ret = trace_print_seq(m, &iter->seq);
2386 /* ret should this time be zero, but you never know */
2387 iter->leftover = ret;
2390 print_trace_line(iter);
2391 ret = trace_print_seq(m, &iter->seq);
2393 * If we overflow the seq_file buffer, then it will
2394 * ask us for this data again at start up.
2396 * ret is 0 if seq_file write succeeded.
2399 iter->leftover = ret;
2405 static const struct seq_operations tracer_seq_ops = {
2412 static struct trace_iterator *
2413 __tracing_open(struct inode *inode, struct file *file)
2415 long cpu_file = (long) inode->i_private;
2416 void *fail_ret = ERR_PTR(-ENOMEM);
2417 struct trace_iterator *iter;
2421 if (tracing_disabled)
2422 return ERR_PTR(-ENODEV);
2424 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2426 return ERR_PTR(-ENOMEM);
2429 * We make a copy of the current tracer to avoid concurrent
2430 * changes on it while we are reading.
2432 mutex_lock(&trace_types_lock);
2433 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2438 *iter->trace = *current_trace;
2440 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2443 if (current_trace && current_trace->print_max)
2446 iter->tr = &global_trace;
2448 mutex_init(&iter->mutex);
2449 iter->cpu_file = cpu_file;
2451 /* Notify the tracer early; before we stop tracing. */
2452 if (iter->trace && iter->trace->open)
2453 iter->trace->open(iter);
2455 /* Annotate start of buffers if we had overruns */
2456 if (ring_buffer_overruns(iter->tr->buffer))
2457 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2459 /* stop the trace while dumping */
2462 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2463 for_each_tracing_cpu(cpu) {
2464 iter->buffer_iter[cpu] =
2465 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2467 ring_buffer_read_prepare_sync();
2468 for_each_tracing_cpu(cpu) {
2469 ring_buffer_read_start(iter->buffer_iter[cpu]);
2470 tracing_iter_reset(iter, cpu);
2473 cpu = iter->cpu_file;
2474 iter->buffer_iter[cpu] =
2475 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2476 ring_buffer_read_prepare_sync();
2477 ring_buffer_read_start(iter->buffer_iter[cpu]);
2478 tracing_iter_reset(iter, cpu);
2481 ret = seq_open(file, &tracer_seq_ops);
2483 fail_ret = ERR_PTR(ret);
2487 m = file->private_data;
2490 mutex_unlock(&trace_types_lock);
2495 for_each_tracing_cpu(cpu) {
2496 if (iter->buffer_iter[cpu])
2497 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2499 free_cpumask_var(iter->started);
2502 mutex_unlock(&trace_types_lock);
2509 int tracing_open_generic(struct inode *inode, struct file *filp)
2511 if (tracing_disabled)
2514 filp->private_data = inode->i_private;
2518 static int tracing_release(struct inode *inode, struct file *file)
2520 struct seq_file *m = file->private_data;
2521 struct trace_iterator *iter;
2524 if (!(file->f_mode & FMODE_READ))
2529 mutex_lock(&trace_types_lock);
2530 for_each_tracing_cpu(cpu) {
2531 if (iter->buffer_iter[cpu])
2532 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2535 if (iter->trace && iter->trace->close)
2536 iter->trace->close(iter);
2538 /* reenable tracing if it was previously enabled */
2540 mutex_unlock(&trace_types_lock);
2542 seq_release(inode, file);
2543 mutex_destroy(&iter->mutex);
2544 free_cpumask_var(iter->started);
2550 static int tracing_open(struct inode *inode, struct file *file)
2552 struct trace_iterator *iter;
2555 /* If this file was open for write, then erase contents */
2556 if ((file->f_mode & FMODE_WRITE) &&
2557 (file->f_flags & O_TRUNC)) {
2558 long cpu = (long) inode->i_private;
2560 if (cpu == TRACE_PIPE_ALL_CPU)
2561 tracing_reset_online_cpus(&global_trace);
2563 tracing_reset(&global_trace, cpu);
2566 if (file->f_mode & FMODE_READ) {
2567 iter = __tracing_open(inode, file);
2569 ret = PTR_ERR(iter);
2570 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2571 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2577 t_next(struct seq_file *m, void *v, loff_t *pos)
2579 struct tracer *t = v;
2589 static void *t_start(struct seq_file *m, loff_t *pos)
2594 mutex_lock(&trace_types_lock);
2595 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2601 static void t_stop(struct seq_file *m, void *p)
2603 mutex_unlock(&trace_types_lock);
2606 static int t_show(struct seq_file *m, void *v)
2608 struct tracer *t = v;
2613 seq_printf(m, "%s", t->name);
2622 static const struct seq_operations show_traces_seq_ops = {
2629 static int show_traces_open(struct inode *inode, struct file *file)
2631 if (tracing_disabled)
2634 return seq_open(file, &show_traces_seq_ops);
2638 tracing_write_stub(struct file *filp, const char __user *ubuf,
2639 size_t count, loff_t *ppos)
2644 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2646 if (file->f_mode & FMODE_READ)
2647 return seq_lseek(file, offset, origin);
2652 static const struct file_operations tracing_fops = {
2653 .open = tracing_open,
2655 .write = tracing_write_stub,
2656 .llseek = tracing_seek,
2657 .release = tracing_release,
2660 static const struct file_operations show_traces_fops = {
2661 .open = show_traces_open,
2663 .release = seq_release,
2664 .llseek = seq_lseek,
2668 * Only trace on a CPU if the bitmask is set:
2670 static cpumask_var_t tracing_cpumask;
2673 * The tracer itself will not take this lock, but still we want
2674 * to provide a consistent cpumask to user-space:
2676 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2679 * Temporary storage for the character representation of the
2680 * CPU bitmask (and one more byte for the newline):
2682 static char mask_str[NR_CPUS + 1];
2685 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2686 size_t count, loff_t *ppos)
2690 mutex_lock(&tracing_cpumask_update_lock);
2692 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2693 if (count - len < 2) {
2697 len += sprintf(mask_str + len, "\n");
2698 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2701 mutex_unlock(&tracing_cpumask_update_lock);
2707 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2708 size_t count, loff_t *ppos)
2711 cpumask_var_t tracing_cpumask_new;
2713 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2716 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2720 mutex_lock(&tracing_cpumask_update_lock);
2722 local_irq_disable();
2723 arch_spin_lock(&ftrace_max_lock);
2724 for_each_tracing_cpu(cpu) {
2726 * Increase/decrease the disabled counter if we are
2727 * about to flip a bit in the cpumask:
2729 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2730 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2731 atomic_inc(&global_trace.data[cpu]->disabled);
2733 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2734 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2735 atomic_dec(&global_trace.data[cpu]->disabled);
2738 arch_spin_unlock(&ftrace_max_lock);
2741 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2743 mutex_unlock(&tracing_cpumask_update_lock);
2744 free_cpumask_var(tracing_cpumask_new);
2749 free_cpumask_var(tracing_cpumask_new);
2754 static const struct file_operations tracing_cpumask_fops = {
2755 .open = tracing_open_generic,
2756 .read = tracing_cpumask_read,
2757 .write = tracing_cpumask_write,
2758 .llseek = generic_file_llseek,
2761 static int tracing_trace_options_show(struct seq_file *m, void *v)
2763 struct tracer_opt *trace_opts;
2767 mutex_lock(&trace_types_lock);
2768 tracer_flags = current_trace->flags->val;
2769 trace_opts = current_trace->flags->opts;
2771 for (i = 0; trace_options[i]; i++) {
2772 if (trace_flags & (1 << i))
2773 seq_printf(m, "%s\n", trace_options[i]);
2775 seq_printf(m, "no%s\n", trace_options[i]);
2778 for (i = 0; trace_opts[i].name; i++) {
2779 if (tracer_flags & trace_opts[i].bit)
2780 seq_printf(m, "%s\n", trace_opts[i].name);
2782 seq_printf(m, "no%s\n", trace_opts[i].name);
2784 mutex_unlock(&trace_types_lock);
2789 static int __set_tracer_option(struct tracer *trace,
2790 struct tracer_flags *tracer_flags,
2791 struct tracer_opt *opts, int neg)
2795 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2800 tracer_flags->val &= ~opts->bit;
2802 tracer_flags->val |= opts->bit;
2806 /* Try to assign a tracer specific option */
2807 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2809 struct tracer_flags *tracer_flags = trace->flags;
2810 struct tracer_opt *opts = NULL;
2813 for (i = 0; tracer_flags->opts[i].name; i++) {
2814 opts = &tracer_flags->opts[i];
2816 if (strcmp(cmp, opts->name) == 0)
2817 return __set_tracer_option(trace, trace->flags,
2824 static void set_tracer_flags(unsigned int mask, int enabled)
2826 /* do nothing if flag is already set */
2827 if (!!(trace_flags & mask) == !!enabled)
2831 trace_flags |= mask;
2833 trace_flags &= ~mask;
2835 if (mask == TRACE_ITER_RECORD_CMD)
2836 trace_event_enable_cmd_record(enabled);
2838 if (mask == TRACE_ITER_OVERWRITE)
2839 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2843 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2844 size_t cnt, loff_t *ppos)
2852 if (cnt >= sizeof(buf))
2855 if (copy_from_user(&buf, ubuf, cnt))
2859 cmp = strstrip(buf);
2861 if (strncmp(cmp, "no", 2) == 0) {
2866 for (i = 0; trace_options[i]; i++) {
2867 if (strcmp(cmp, trace_options[i]) == 0) {
2868 set_tracer_flags(1 << i, !neg);
2873 /* If no option could be set, test the specific tracer options */
2874 if (!trace_options[i]) {
2875 mutex_lock(&trace_types_lock);
2876 ret = set_tracer_option(current_trace, cmp, neg);
2877 mutex_unlock(&trace_types_lock);
2887 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2889 if (tracing_disabled)
2891 return single_open(file, tracing_trace_options_show, NULL);
2894 static const struct file_operations tracing_iter_fops = {
2895 .open = tracing_trace_options_open,
2897 .llseek = seq_lseek,
2898 .release = single_release,
2899 .write = tracing_trace_options_write,
2902 static const char readme_msg[] =
2903 "tracing mini-HOWTO:\n\n"
2904 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2905 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2906 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
2907 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2909 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
2910 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2912 "# cat /sys/kernel/debug/tracing/trace_options\n"
2913 "noprint-parent nosym-offset nosym-addr noverbose\n"
2914 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2915 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
2916 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2917 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
2921 tracing_readme_read(struct file *filp, char __user *ubuf,
2922 size_t cnt, loff_t *ppos)
2924 return simple_read_from_buffer(ubuf, cnt, ppos,
2925 readme_msg, strlen(readme_msg));
2928 static const struct file_operations tracing_readme_fops = {
2929 .open = tracing_open_generic,
2930 .read = tracing_readme_read,
2931 .llseek = generic_file_llseek,
2935 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2936 size_t cnt, loff_t *ppos)
2945 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2949 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2957 for (i = 0; i < SAVED_CMDLINES; i++) {
2960 pid = map_cmdline_to_pid[i];
2961 if (pid == -1 || pid == NO_CMDLINE_MAP)
2964 trace_find_cmdline(pid, buf_comm);
2965 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2970 len = simple_read_from_buffer(ubuf, cnt, ppos,
2979 static const struct file_operations tracing_saved_cmdlines_fops = {
2980 .open = tracing_open_generic,
2981 .read = tracing_saved_cmdlines_read,
2982 .llseek = generic_file_llseek,
2986 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2987 size_t cnt, loff_t *ppos)
2992 r = sprintf(buf, "%u\n", tracer_enabled);
2993 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2997 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2998 size_t cnt, loff_t *ppos)
3000 struct trace_array *tr = filp->private_data;
3004 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3010 mutex_lock(&trace_types_lock);
3011 if (tracer_enabled ^ val) {
3013 /* Only need to warn if this is used to change the state */
3014 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
3018 if (current_trace->start)
3019 current_trace->start(tr);
3024 if (current_trace->stop)
3025 current_trace->stop(tr);
3028 mutex_unlock(&trace_types_lock);
3036 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3037 size_t cnt, loff_t *ppos)
3039 char buf[MAX_TRACER_SIZE+2];
3042 mutex_lock(&trace_types_lock);
3044 r = sprintf(buf, "%s\n", current_trace->name);
3046 r = sprintf(buf, "\n");
3047 mutex_unlock(&trace_types_lock);
3049 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3052 int tracer_init(struct tracer *t, struct trace_array *tr)
3054 tracing_reset_online_cpus(tr);
3058 static void set_buffer_entries(struct trace_array *tr, unsigned long val)
3061 for_each_tracing_cpu(cpu)
3062 tr->data[cpu]->entries = val;
3065 static int __tracing_resize_ring_buffer(unsigned long size, int cpu)
3070 * If kernel or user changes the size of the ring buffer
3071 * we use the size that was given, and we can forget about
3072 * expanding it later.
3074 ring_buffer_expanded = 1;
3076 ret = ring_buffer_resize(global_trace.buffer, size, cpu);
3080 if (!current_trace->use_max_tr)
3083 ret = ring_buffer_resize(max_tr.buffer, size, cpu);
3087 if (cpu == RING_BUFFER_ALL_CPUS) {
3089 for_each_tracing_cpu(i) {
3090 r = ring_buffer_resize(global_trace.buffer,
3091 global_trace.data[i]->entries,
3097 r = ring_buffer_resize(global_trace.buffer,
3098 global_trace.data[cpu]->entries,
3104 * AARGH! We are left with different
3105 * size max buffer!!!!
3106 * The max buffer is our "snapshot" buffer.
3107 * When a tracer needs a snapshot (one of the
3108 * latency tracers), it swaps the max buffer
3109 * with the saved snap shot. We succeeded to
3110 * update the size of the main buffer, but failed to
3111 * update the size of the max buffer. But when we tried
3112 * to reset the main buffer to the original size, we
3113 * failed there too. This is very unlikely to
3114 * happen, but if it does, warn and kill all
3118 tracing_disabled = 1;
3123 if (cpu == RING_BUFFER_ALL_CPUS)
3124 set_buffer_entries(&max_tr, size);
3126 max_tr.data[cpu]->entries = size;
3129 if (cpu == RING_BUFFER_ALL_CPUS)
3130 set_buffer_entries(&global_trace, size);
3132 global_trace.data[cpu]->entries = size;
3137 static ssize_t tracing_resize_ring_buffer(unsigned long size, int cpu_id)
3139 int cpu, ret = size;
3141 mutex_lock(&trace_types_lock);
3145 /* disable all cpu buffers */
3146 for_each_tracing_cpu(cpu) {
3147 if (global_trace.data[cpu])
3148 atomic_inc(&global_trace.data[cpu]->disabled);
3149 if (max_tr.data[cpu])
3150 atomic_inc(&max_tr.data[cpu]->disabled);
3153 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3154 /* make sure, this cpu is enabled in the mask */
3155 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3161 ret = __tracing_resize_ring_buffer(size, cpu_id);
3166 for_each_tracing_cpu(cpu) {
3167 if (global_trace.data[cpu])
3168 atomic_dec(&global_trace.data[cpu]->disabled);
3169 if (max_tr.data[cpu])
3170 atomic_dec(&max_tr.data[cpu]->disabled);
3174 mutex_unlock(&trace_types_lock);
3181 * tracing_update_buffers - used by tracing facility to expand ring buffers
3183 * To save on memory when the tracing is never used on a system with it
3184 * configured in. The ring buffers are set to a minimum size. But once
3185 * a user starts to use the tracing facility, then they need to grow
3186 * to their default size.
3188 * This function is to be called when a tracer is about to be used.
3190 int tracing_update_buffers(void)
3194 mutex_lock(&trace_types_lock);
3195 if (!ring_buffer_expanded)
3196 ret = __tracing_resize_ring_buffer(trace_buf_size,
3197 RING_BUFFER_ALL_CPUS);
3198 mutex_unlock(&trace_types_lock);
3203 struct trace_option_dentry;
3205 static struct trace_option_dentry *
3206 create_trace_option_files(struct tracer *tracer);
3209 destroy_trace_option_files(struct trace_option_dentry *topts);
3211 static int tracing_set_tracer(const char *buf)
3213 static struct trace_option_dentry *topts;
3214 struct trace_array *tr = &global_trace;
3218 mutex_lock(&trace_types_lock);
3220 if (!ring_buffer_expanded) {
3221 ret = __tracing_resize_ring_buffer(trace_buf_size,
3222 RING_BUFFER_ALL_CPUS);
3228 for (t = trace_types; t; t = t->next) {
3229 if (strcmp(t->name, buf) == 0)
3236 if (t == current_trace)
3239 trace_branch_disable();
3240 if (current_trace && current_trace->reset)
3241 current_trace->reset(tr);
3242 if (current_trace && current_trace->use_max_tr) {
3244 * We don't free the ring buffer. instead, resize it because
3245 * The max_tr ring buffer has some state (e.g. ring->clock) and
3246 * we want preserve it.
3248 ring_buffer_resize(max_tr.buffer, 1, RING_BUFFER_ALL_CPUS);
3249 set_buffer_entries(&max_tr, 1);
3251 destroy_trace_option_files(topts);
3255 topts = create_trace_option_files(current_trace);
3256 if (current_trace->use_max_tr) {
3258 /* we need to make per cpu buffer sizes equivalent */
3259 for_each_tracing_cpu(cpu) {
3260 ret = ring_buffer_resize(max_tr.buffer,
3261 global_trace.data[cpu]->entries,
3265 max_tr.data[cpu]->entries =
3266 global_trace.data[cpu]->entries;
3271 ret = tracer_init(t, tr);
3276 trace_branch_enable(tr);
3278 mutex_unlock(&trace_types_lock);
3284 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3285 size_t cnt, loff_t *ppos)
3287 char buf[MAX_TRACER_SIZE+1];
3294 if (cnt > MAX_TRACER_SIZE)
3295 cnt = MAX_TRACER_SIZE;
3297 if (copy_from_user(&buf, ubuf, cnt))
3302 /* strip ending whitespace. */
3303 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3306 err = tracing_set_tracer(buf);
3316 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3317 size_t cnt, loff_t *ppos)
3319 unsigned long *ptr = filp->private_data;
3323 r = snprintf(buf, sizeof(buf), "%ld\n",
3324 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3325 if (r > sizeof(buf))
3327 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3331 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3332 size_t cnt, loff_t *ppos)
3334 unsigned long *ptr = filp->private_data;
3338 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3347 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3349 long cpu_file = (long) inode->i_private;
3350 struct trace_iterator *iter;
3353 if (tracing_disabled)
3356 mutex_lock(&trace_types_lock);
3358 /* create a buffer to store the information to pass to userspace */
3359 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3366 * We make a copy of the current tracer to avoid concurrent
3367 * changes on it while we are reading.
3369 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3375 *iter->trace = *current_trace;
3377 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3382 /* trace pipe does not show start of buffer */
3383 cpumask_setall(iter->started);
3385 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3386 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3388 iter->cpu_file = cpu_file;
3389 iter->tr = &global_trace;
3390 mutex_init(&iter->mutex);
3391 filp->private_data = iter;
3393 if (iter->trace->pipe_open)
3394 iter->trace->pipe_open(iter);
3396 nonseekable_open(inode, filp);
3398 mutex_unlock(&trace_types_lock);
3404 mutex_unlock(&trace_types_lock);
3408 static int tracing_release_pipe(struct inode *inode, struct file *file)
3410 struct trace_iterator *iter = file->private_data;
3412 mutex_lock(&trace_types_lock);
3414 if (iter->trace->pipe_close)
3415 iter->trace->pipe_close(iter);
3417 mutex_unlock(&trace_types_lock);
3419 free_cpumask_var(iter->started);
3420 mutex_destroy(&iter->mutex);
3428 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3430 struct trace_iterator *iter = filp->private_data;
3432 if (trace_flags & TRACE_ITER_BLOCK) {
3434 * Always select as readable when in blocking mode
3436 return POLLIN | POLLRDNORM;
3438 if (!trace_empty(iter))
3439 return POLLIN | POLLRDNORM;
3440 poll_wait(filp, &trace_wait, poll_table);
3441 if (!trace_empty(iter))
3442 return POLLIN | POLLRDNORM;
3449 void default_wait_pipe(struct trace_iterator *iter)
3453 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3455 if (trace_empty(iter))
3458 finish_wait(&trace_wait, &wait);
3462 * This is a make-shift waitqueue.
3463 * A tracer might use this callback on some rare cases:
3465 * 1) the current tracer might hold the runqueue lock when it wakes up
3466 * a reader, hence a deadlock (sched, function, and function graph tracers)
3467 * 2) the function tracers, trace all functions, we don't want
3468 * the overhead of calling wake_up and friends
3469 * (and tracing them too)
3471 * Anyway, this is really very primitive wakeup.
3473 void poll_wait_pipe(struct trace_iterator *iter)
3475 set_current_state(TASK_INTERRUPTIBLE);
3476 /* sleep for 100 msecs, and try again. */
3477 schedule_timeout(HZ / 10);
3480 /* Must be called with trace_types_lock mutex held. */
3481 static int tracing_wait_pipe(struct file *filp)
3483 struct trace_iterator *iter = filp->private_data;
3485 while (trace_empty(iter)) {
3487 if ((filp->f_flags & O_NONBLOCK)) {
3491 mutex_unlock(&iter->mutex);
3493 iter->trace->wait_pipe(iter);
3495 mutex_lock(&iter->mutex);
3497 if (signal_pending(current))
3501 * We block until we read something and tracing is disabled.
3502 * We still block if tracing is disabled, but we have never
3503 * read anything. This allows a user to cat this file, and
3504 * then enable tracing. But after we have read something,
3505 * we give an EOF when tracing is again disabled.
3507 * iter->pos will be 0 if we haven't read anything.
3509 if (!tracer_enabled && iter->pos)
3520 tracing_read_pipe(struct file *filp, char __user *ubuf,
3521 size_t cnt, loff_t *ppos)
3523 struct trace_iterator *iter = filp->private_data;
3524 static struct tracer *old_tracer;
3527 /* return any leftover data */
3528 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3532 trace_seq_init(&iter->seq);
3534 /* copy the tracer to avoid using a global lock all around */
3535 mutex_lock(&trace_types_lock);
3536 if (unlikely(old_tracer != current_trace && current_trace)) {
3537 old_tracer = current_trace;
3538 *iter->trace = *current_trace;
3540 mutex_unlock(&trace_types_lock);
3543 * Avoid more than one consumer on a single file descriptor
3544 * This is just a matter of traces coherency, the ring buffer itself
3547 mutex_lock(&iter->mutex);
3548 if (iter->trace->read) {
3549 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3555 sret = tracing_wait_pipe(filp);
3559 /* stop when tracing is finished */
3560 if (trace_empty(iter)) {
3565 if (cnt >= PAGE_SIZE)
3566 cnt = PAGE_SIZE - 1;
3568 /* reset all but tr, trace, and overruns */
3569 memset(&iter->seq, 0,
3570 sizeof(struct trace_iterator) -
3571 offsetof(struct trace_iterator, seq));
3574 trace_event_read_lock();
3575 trace_access_lock(iter->cpu_file);
3576 while (trace_find_next_entry_inc(iter) != NULL) {
3577 enum print_line_t ret;
3578 int len = iter->seq.len;
3580 ret = print_trace_line(iter);
3581 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3582 /* don't print partial lines */
3583 iter->seq.len = len;
3586 if (ret != TRACE_TYPE_NO_CONSUME)
3587 trace_consume(iter);
3589 if (iter->seq.len >= cnt)
3593 * Setting the full flag means we reached the trace_seq buffer
3594 * size and we should leave by partial output condition above.
3595 * One of the trace_seq_* functions is not used properly.
3597 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3600 trace_access_unlock(iter->cpu_file);
3601 trace_event_read_unlock();
3603 /* Now copy what we have to the user */
3604 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3605 if (iter->seq.readpos >= iter->seq.len)
3606 trace_seq_init(&iter->seq);
3609 * If there was nothing to send to user, in spite of consuming trace
3610 * entries, go back to wait for more entries.
3616 mutex_unlock(&iter->mutex);
3621 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3622 struct pipe_buffer *buf)
3624 __free_page(buf->page);
3627 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3630 __free_page(spd->pages[idx]);
3633 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3635 .map = generic_pipe_buf_map,
3636 .unmap = generic_pipe_buf_unmap,
3637 .confirm = generic_pipe_buf_confirm,
3638 .release = tracing_pipe_buf_release,
3639 .steal = generic_pipe_buf_steal,
3640 .get = generic_pipe_buf_get,
3644 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3649 /* Seq buffer is page-sized, exactly what we need. */
3651 count = iter->seq.len;
3652 ret = print_trace_line(iter);
3653 count = iter->seq.len - count;
3656 iter->seq.len -= count;
3659 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3660 iter->seq.len -= count;
3664 if (ret != TRACE_TYPE_NO_CONSUME)
3665 trace_consume(iter);
3667 if (!trace_find_next_entry_inc(iter)) {
3677 static ssize_t tracing_splice_read_pipe(struct file *filp,
3679 struct pipe_inode_info *pipe,
3683 struct page *pages_def[PIPE_DEF_BUFFERS];
3684 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3685 struct trace_iterator *iter = filp->private_data;
3686 struct splice_pipe_desc spd = {
3688 .partial = partial_def,
3689 .nr_pages = 0, /* This gets updated below. */
3691 .ops = &tracing_pipe_buf_ops,
3692 .spd_release = tracing_spd_release_pipe,
3694 static struct tracer *old_tracer;
3699 if (splice_grow_spd(pipe, &spd))
3702 /* copy the tracer to avoid using a global lock all around */
3703 mutex_lock(&trace_types_lock);
3704 if (unlikely(old_tracer != current_trace && current_trace)) {
3705 old_tracer = current_trace;
3706 *iter->trace = *current_trace;
3708 mutex_unlock(&trace_types_lock);
3710 mutex_lock(&iter->mutex);
3712 if (iter->trace->splice_read) {
3713 ret = iter->trace->splice_read(iter, filp,
3714 ppos, pipe, len, flags);
3719 ret = tracing_wait_pipe(filp);
3723 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3728 trace_event_read_lock();
3729 trace_access_lock(iter->cpu_file);
3731 /* Fill as many pages as possible. */
3732 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3733 spd.pages[i] = alloc_page(GFP_KERNEL);
3737 rem = tracing_fill_pipe_page(rem, iter);
3739 /* Copy the data into the page, so we can start over. */
3740 ret = trace_seq_to_buffer(&iter->seq,
3741 page_address(spd.pages[i]),
3744 __free_page(spd.pages[i]);
3747 spd.partial[i].offset = 0;
3748 spd.partial[i].len = iter->seq.len;
3750 trace_seq_init(&iter->seq);
3753 trace_access_unlock(iter->cpu_file);
3754 trace_event_read_unlock();
3755 mutex_unlock(&iter->mutex);
3759 ret = splice_to_pipe(pipe, &spd);
3761 splice_shrink_spd(pipe, &spd);
3765 mutex_unlock(&iter->mutex);
3769 struct ftrace_entries_info {
3770 struct trace_array *tr;
3774 static int tracing_entries_open(struct inode *inode, struct file *filp)
3776 struct ftrace_entries_info *info;
3778 if (tracing_disabled)
3781 info = kzalloc(sizeof(*info), GFP_KERNEL);
3785 info->tr = &global_trace;
3786 info->cpu = (unsigned long)inode->i_private;
3788 filp->private_data = info;
3794 tracing_entries_read(struct file *filp, char __user *ubuf,
3795 size_t cnt, loff_t *ppos)
3797 struct ftrace_entries_info *info = filp->private_data;
3798 struct trace_array *tr = info->tr;
3803 mutex_lock(&trace_types_lock);
3805 if (info->cpu == RING_BUFFER_ALL_CPUS) {
3806 int cpu, buf_size_same;
3811 /* check if all cpu sizes are same */
3812 for_each_tracing_cpu(cpu) {
3813 /* fill in the size from first enabled cpu */
3815 size = tr->data[cpu]->entries;
3816 if (size != tr->data[cpu]->entries) {
3822 if (buf_size_same) {
3823 if (!ring_buffer_expanded)
3824 r = sprintf(buf, "%lu (expanded: %lu)\n",
3826 trace_buf_size >> 10);
3828 r = sprintf(buf, "%lu\n", size >> 10);
3830 r = sprintf(buf, "X\n");
3832 r = sprintf(buf, "%lu\n", tr->data[info->cpu]->entries >> 10);
3834 mutex_unlock(&trace_types_lock);
3836 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3841 tracing_entries_write(struct file *filp, const char __user *ubuf,
3842 size_t cnt, loff_t *ppos)
3844 struct ftrace_entries_info *info = filp->private_data;
3848 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3852 /* must have at least 1 entry */
3856 /* value is in KB */
3859 ret = tracing_resize_ring_buffer(val, info->cpu);
3869 tracing_entries_release(struct inode *inode, struct file *filp)
3871 struct ftrace_entries_info *info = filp->private_data;
3879 tracing_total_entries_read(struct file *filp, char __user *ubuf,
3880 size_t cnt, loff_t *ppos)
3882 struct trace_array *tr = filp->private_data;
3885 unsigned long size = 0, expanded_size = 0;
3887 mutex_lock(&trace_types_lock);
3888 for_each_tracing_cpu(cpu) {
3889 size += tr->data[cpu]->entries >> 10;
3890 if (!ring_buffer_expanded)
3891 expanded_size += trace_buf_size >> 10;
3893 if (ring_buffer_expanded)
3894 r = sprintf(buf, "%lu\n", size);
3896 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3897 mutex_unlock(&trace_types_lock);
3899 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3903 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3904 size_t cnt, loff_t *ppos)
3907 * There is no need to read what the user has written, this function
3908 * is just to make sure that there is no error when "echo" is used
3917 tracing_free_buffer_release(struct inode *inode, struct file *filp)
3919 /* disable tracing ? */
3920 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3922 /* resize the ring buffer to 0 */
3923 tracing_resize_ring_buffer(0, RING_BUFFER_ALL_CPUS);
3929 tracing_mark_write(struct file *filp, const char __user *ubuf,
3930 size_t cnt, loff_t *fpos)
3932 unsigned long addr = (unsigned long)ubuf;
3933 struct ring_buffer_event *event;
3934 struct ring_buffer *buffer;
3935 struct print_entry *entry;
3936 unsigned long irq_flags;
3937 struct page *pages[2];
3947 if (tracing_disabled)
3950 if (cnt > TRACE_BUF_SIZE)
3951 cnt = TRACE_BUF_SIZE;
3954 * Userspace is injecting traces into the kernel trace buffer.
3955 * We want to be as non intrusive as possible.
3956 * To do so, we do not want to allocate any special buffers
3957 * or take any locks, but instead write the userspace data
3958 * straight into the ring buffer.
3960 * First we need to pin the userspace buffer into memory,
3961 * which, most likely it is, because it just referenced it.
3962 * But there's no guarantee that it is. By using get_user_pages_fast()
3963 * and kmap_atomic/kunmap_atomic() we can get access to the
3964 * pages directly. We then write the data directly into the
3967 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
3969 /* check if we cross pages */
3970 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
3973 offset = addr & (PAGE_SIZE - 1);
3976 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
3977 if (ret < nr_pages) {
3979 put_page(pages[ret]);
3984 page1 = kmap_atomic(pages[0]);
3986 page2 = kmap_atomic(pages[1]);
3988 local_save_flags(irq_flags);
3989 size = sizeof(*entry) + cnt + 2; /* possible \n added */
3990 buffer = global_trace.buffer;
3991 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3992 irq_flags, preempt_count());
3994 /* Ring buffer disabled, return as if not open for write */
3999 entry = ring_buffer_event_data(event);
4000 entry->ip = _THIS_IP_;
4002 if (nr_pages == 2) {
4003 len = PAGE_SIZE - offset;
4004 memcpy(&entry->buf, page1 + offset, len);
4005 memcpy(&entry->buf[len], page2, cnt - len);
4007 memcpy(&entry->buf, page1 + offset, cnt);
4009 if (entry->buf[cnt - 1] != '\n') {
4010 entry->buf[cnt] = '\n';
4011 entry->buf[cnt + 1] = '\0';
4013 entry->buf[cnt] = '\0';
4015 ring_buffer_unlock_commit(buffer, event);
4023 kunmap_atomic(page2);
4024 kunmap_atomic(page1);
4025 while (nr_pages > 0)
4026 put_page(pages[--nr_pages]);
4031 static int tracing_clock_show(struct seq_file *m, void *v)
4035 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4037 "%s%s%s%s", i ? " " : "",
4038 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
4039 i == trace_clock_id ? "]" : "");
4045 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4046 size_t cnt, loff_t *fpos)
4049 const char *clockstr;
4052 if (cnt >= sizeof(buf))
4055 if (copy_from_user(&buf, ubuf, cnt))
4060 clockstr = strstrip(buf);
4062 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4063 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4066 if (i == ARRAY_SIZE(trace_clocks))
4071 mutex_lock(&trace_types_lock);
4073 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
4075 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
4077 mutex_unlock(&trace_types_lock);
4084 static int tracing_clock_open(struct inode *inode, struct file *file)
4086 if (tracing_disabled)
4088 return single_open(file, tracing_clock_show, NULL);
4091 static const struct file_operations tracing_max_lat_fops = {
4092 .open = tracing_open_generic,
4093 .read = tracing_max_lat_read,
4094 .write = tracing_max_lat_write,
4095 .llseek = generic_file_llseek,
4098 static const struct file_operations tracing_ctrl_fops = {
4099 .open = tracing_open_generic,
4100 .read = tracing_ctrl_read,
4101 .write = tracing_ctrl_write,
4102 .llseek = generic_file_llseek,
4105 static const struct file_operations set_tracer_fops = {
4106 .open = tracing_open_generic,
4107 .read = tracing_set_trace_read,
4108 .write = tracing_set_trace_write,
4109 .llseek = generic_file_llseek,
4112 static const struct file_operations tracing_pipe_fops = {
4113 .open = tracing_open_pipe,
4114 .poll = tracing_poll_pipe,
4115 .read = tracing_read_pipe,
4116 .splice_read = tracing_splice_read_pipe,
4117 .release = tracing_release_pipe,
4118 .llseek = no_llseek,
4121 static const struct file_operations tracing_entries_fops = {
4122 .open = tracing_entries_open,
4123 .read = tracing_entries_read,
4124 .write = tracing_entries_write,
4125 .release = tracing_entries_release,
4126 .llseek = generic_file_llseek,
4129 static const struct file_operations tracing_total_entries_fops = {
4130 .open = tracing_open_generic,
4131 .read = tracing_total_entries_read,
4132 .llseek = generic_file_llseek,
4135 static const struct file_operations tracing_free_buffer_fops = {
4136 .write = tracing_free_buffer_write,
4137 .release = tracing_free_buffer_release,
4140 static const struct file_operations tracing_mark_fops = {
4141 .open = tracing_open_generic,
4142 .write = tracing_mark_write,
4143 .llseek = generic_file_llseek,
4146 static const struct file_operations trace_clock_fops = {
4147 .open = tracing_clock_open,
4149 .llseek = seq_lseek,
4150 .release = single_release,
4151 .write = tracing_clock_write,
4154 struct ftrace_buffer_info {
4155 struct trace_array *tr;
4161 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4163 int cpu = (int)(long)inode->i_private;
4164 struct ftrace_buffer_info *info;
4166 if (tracing_disabled)
4169 info = kzalloc(sizeof(*info), GFP_KERNEL);
4173 info->tr = &global_trace;
4176 /* Force reading ring buffer for first read */
4177 info->read = (unsigned int)-1;
4179 filp->private_data = info;
4181 return nonseekable_open(inode, filp);
4185 tracing_buffers_read(struct file *filp, char __user *ubuf,
4186 size_t count, loff_t *ppos)
4188 struct ftrace_buffer_info *info = filp->private_data;
4196 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
4200 /* Do we have previous read data to read? */
4201 if (info->read < PAGE_SIZE)
4204 trace_access_lock(info->cpu);
4205 ret = ring_buffer_read_page(info->tr->buffer,
4209 trace_access_unlock(info->cpu);
4216 size = PAGE_SIZE - info->read;
4220 ret = copy_to_user(ubuf, info->spare + info->read, size);
4231 static int tracing_buffers_release(struct inode *inode, struct file *file)
4233 struct ftrace_buffer_info *info = file->private_data;
4236 ring_buffer_free_read_page(info->tr->buffer, info->spare);
4243 struct ring_buffer *buffer;
4248 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4249 struct pipe_buffer *buf)
4251 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4256 ring_buffer_free_read_page(ref->buffer, ref->page);
4261 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
4262 struct pipe_buffer *buf)
4267 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4268 struct pipe_buffer *buf)
4270 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4275 /* Pipe buffer operations for a buffer. */
4276 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4278 .map = generic_pipe_buf_map,
4279 .unmap = generic_pipe_buf_unmap,
4280 .confirm = generic_pipe_buf_confirm,
4281 .release = buffer_pipe_buf_release,
4282 .steal = buffer_pipe_buf_steal,
4283 .get = buffer_pipe_buf_get,
4287 * Callback from splice_to_pipe(), if we need to release some pages
4288 * at the end of the spd in case we error'ed out in filling the pipe.
4290 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4292 struct buffer_ref *ref =
4293 (struct buffer_ref *)spd->partial[i].private;
4298 ring_buffer_free_read_page(ref->buffer, ref->page);
4300 spd->partial[i].private = 0;
4304 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4305 struct pipe_inode_info *pipe, size_t len,
4308 struct ftrace_buffer_info *info = file->private_data;
4309 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4310 struct page *pages_def[PIPE_DEF_BUFFERS];
4311 struct splice_pipe_desc spd = {
4313 .partial = partial_def,
4315 .ops = &buffer_pipe_buf_ops,
4316 .spd_release = buffer_spd_release,
4318 struct buffer_ref *ref;
4319 int entries, size, i;
4322 if (splice_grow_spd(pipe, &spd))
4325 if (*ppos & (PAGE_SIZE - 1)) {
4326 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
4331 if (len & (PAGE_SIZE - 1)) {
4332 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
4333 if (len < PAGE_SIZE) {
4340 trace_access_lock(info->cpu);
4341 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4343 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4347 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4352 ref->buffer = info->tr->buffer;
4353 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
4359 r = ring_buffer_read_page(ref->buffer, &ref->page,
4362 ring_buffer_free_read_page(ref->buffer, ref->page);
4368 * zero out any left over data, this is going to
4371 size = ring_buffer_page_len(ref->page);
4372 if (size < PAGE_SIZE)
4373 memset(ref->page + size, 0, PAGE_SIZE - size);
4375 page = virt_to_page(ref->page);
4377 spd.pages[i] = page;
4378 spd.partial[i].len = PAGE_SIZE;
4379 spd.partial[i].offset = 0;
4380 spd.partial[i].private = (unsigned long)ref;
4384 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4387 trace_access_unlock(info->cpu);
4390 /* did we read anything? */
4391 if (!spd.nr_pages) {
4392 if (flags & SPLICE_F_NONBLOCK)
4400 ret = splice_to_pipe(pipe, &spd);
4401 splice_shrink_spd(pipe, &spd);
4406 static const struct file_operations tracing_buffers_fops = {
4407 .open = tracing_buffers_open,
4408 .read = tracing_buffers_read,
4409 .release = tracing_buffers_release,
4410 .splice_read = tracing_buffers_splice_read,
4411 .llseek = no_llseek,
4415 tracing_stats_read(struct file *filp, char __user *ubuf,
4416 size_t count, loff_t *ppos)
4418 unsigned long cpu = (unsigned long)filp->private_data;
4419 struct trace_array *tr = &global_trace;
4420 struct trace_seq *s;
4422 unsigned long long t;
4423 unsigned long usec_rem;
4425 s = kmalloc(sizeof(*s), GFP_KERNEL);
4431 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4432 trace_seq_printf(s, "entries: %ld\n", cnt);
4434 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4435 trace_seq_printf(s, "overrun: %ld\n", cnt);
4437 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4438 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4440 cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4441 trace_seq_printf(s, "bytes: %ld\n", cnt);
4443 t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4444 usec_rem = do_div(t, USEC_PER_SEC);
4445 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", t, usec_rem);
4447 t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4448 usec_rem = do_div(t, USEC_PER_SEC);
4449 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4451 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4458 static const struct file_operations tracing_stats_fops = {
4459 .open = tracing_open_generic,
4460 .read = tracing_stats_read,
4461 .llseek = generic_file_llseek,
4464 #ifdef CONFIG_DYNAMIC_FTRACE
4466 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4472 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
4473 size_t cnt, loff_t *ppos)
4475 static char ftrace_dyn_info_buffer[1024];
4476 static DEFINE_MUTEX(dyn_info_mutex);
4477 unsigned long *p = filp->private_data;
4478 char *buf = ftrace_dyn_info_buffer;
4479 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
4482 mutex_lock(&dyn_info_mutex);
4483 r = sprintf(buf, "%ld ", *p);
4485 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
4488 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4490 mutex_unlock(&dyn_info_mutex);
4495 static const struct file_operations tracing_dyn_info_fops = {
4496 .open = tracing_open_generic,
4497 .read = tracing_read_dyn_info,
4498 .llseek = generic_file_llseek,
4502 static struct dentry *d_tracer;
4504 struct dentry *tracing_init_dentry(void)
4511 if (!debugfs_initialized())
4514 d_tracer = debugfs_create_dir("tracing", NULL);
4516 if (!d_tracer && !once) {
4518 pr_warning("Could not create debugfs directory 'tracing'\n");
4525 static struct dentry *d_percpu;
4527 struct dentry *tracing_dentry_percpu(void)
4530 struct dentry *d_tracer;
4535 d_tracer = tracing_init_dentry();
4540 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4542 if (!d_percpu && !once) {
4544 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4551 static void tracing_init_debugfs_percpu(long cpu)
4553 struct dentry *d_percpu = tracing_dentry_percpu();
4554 struct dentry *d_cpu;
4555 char cpu_dir[30]; /* 30 characters should be more than enough */
4557 snprintf(cpu_dir, 30, "cpu%ld", cpu);
4558 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4560 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4564 /* per cpu trace_pipe */
4565 trace_create_file("trace_pipe", 0444, d_cpu,
4566 (void *) cpu, &tracing_pipe_fops);
4569 trace_create_file("trace", 0644, d_cpu,
4570 (void *) cpu, &tracing_fops);
4572 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4573 (void *) cpu, &tracing_buffers_fops);
4575 trace_create_file("stats", 0444, d_cpu,
4576 (void *) cpu, &tracing_stats_fops);
4578 trace_create_file("buffer_size_kb", 0444, d_cpu,
4579 (void *) cpu, &tracing_entries_fops);
4582 #ifdef CONFIG_FTRACE_SELFTEST
4583 /* Let selftest have access to static functions in this file */
4584 #include "trace_selftest.c"
4587 struct trace_option_dentry {
4588 struct tracer_opt *opt;
4589 struct tracer_flags *flags;
4590 struct dentry *entry;
4594 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4597 struct trace_option_dentry *topt = filp->private_data;
4600 if (topt->flags->val & topt->opt->bit)
4605 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4609 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4612 struct trace_option_dentry *topt = filp->private_data;
4616 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4620 if (val != 0 && val != 1)
4623 if (!!(topt->flags->val & topt->opt->bit) != val) {
4624 mutex_lock(&trace_types_lock);
4625 ret = __set_tracer_option(current_trace, topt->flags,
4627 mutex_unlock(&trace_types_lock);
4638 static const struct file_operations trace_options_fops = {
4639 .open = tracing_open_generic,
4640 .read = trace_options_read,
4641 .write = trace_options_write,
4642 .llseek = generic_file_llseek,
4646 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4649 long index = (long)filp->private_data;
4652 if (trace_flags & (1 << index))
4657 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4661 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4664 long index = (long)filp->private_data;
4668 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4672 if (val != 0 && val != 1)
4674 set_tracer_flags(1 << index, val);
4681 static const struct file_operations trace_options_core_fops = {
4682 .open = tracing_open_generic,
4683 .read = trace_options_core_read,
4684 .write = trace_options_core_write,
4685 .llseek = generic_file_llseek,
4688 struct dentry *trace_create_file(const char *name,
4690 struct dentry *parent,
4692 const struct file_operations *fops)
4696 ret = debugfs_create_file(name, mode, parent, data, fops);
4698 pr_warning("Could not create debugfs '%s' entry\n", name);
4704 static struct dentry *trace_options_init_dentry(void)
4706 struct dentry *d_tracer;
4707 static struct dentry *t_options;
4712 d_tracer = tracing_init_dentry();
4716 t_options = debugfs_create_dir("options", d_tracer);
4718 pr_warning("Could not create debugfs directory 'options'\n");
4726 create_trace_option_file(struct trace_option_dentry *topt,
4727 struct tracer_flags *flags,
4728 struct tracer_opt *opt)
4730 struct dentry *t_options;
4732 t_options = trace_options_init_dentry();
4736 topt->flags = flags;
4739 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4740 &trace_options_fops);
4744 static struct trace_option_dentry *
4745 create_trace_option_files(struct tracer *tracer)
4747 struct trace_option_dentry *topts;
4748 struct tracer_flags *flags;
4749 struct tracer_opt *opts;
4755 flags = tracer->flags;
4757 if (!flags || !flags->opts)
4762 for (cnt = 0; opts[cnt].name; cnt++)
4765 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4769 for (cnt = 0; opts[cnt].name; cnt++)
4770 create_trace_option_file(&topts[cnt], flags,
4777 destroy_trace_option_files(struct trace_option_dentry *topts)
4784 for (cnt = 0; topts[cnt].opt; cnt++) {
4785 if (topts[cnt].entry)
4786 debugfs_remove(topts[cnt].entry);
4792 static struct dentry *
4793 create_trace_option_core_file(const char *option, long index)
4795 struct dentry *t_options;
4797 t_options = trace_options_init_dentry();
4801 return trace_create_file(option, 0644, t_options, (void *)index,
4802 &trace_options_core_fops);
4805 static __init void create_trace_options_dir(void)
4807 struct dentry *t_options;
4810 t_options = trace_options_init_dentry();
4814 for (i = 0; trace_options[i]; i++)
4815 create_trace_option_core_file(trace_options[i], i);
4819 rb_simple_read(struct file *filp, char __user *ubuf,
4820 size_t cnt, loff_t *ppos)
4822 struct ring_buffer *buffer = filp->private_data;
4827 r = ring_buffer_record_is_on(buffer);
4831 r = sprintf(buf, "%d\n", r);
4833 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4837 rb_simple_write(struct file *filp, const char __user *ubuf,
4838 size_t cnt, loff_t *ppos)
4840 struct ring_buffer *buffer = filp->private_data;
4844 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4850 ring_buffer_record_on(buffer);
4852 ring_buffer_record_off(buffer);
4860 static const struct file_operations rb_simple_fops = {
4861 .open = tracing_open_generic,
4862 .read = rb_simple_read,
4863 .write = rb_simple_write,
4864 .llseek = default_llseek,
4867 static __init int tracer_init_debugfs(void)
4869 struct dentry *d_tracer;
4872 trace_access_lock_init();
4874 d_tracer = tracing_init_dentry();
4876 trace_create_file("tracing_enabled", 0644, d_tracer,
4877 &global_trace, &tracing_ctrl_fops);
4879 trace_create_file("trace_options", 0644, d_tracer,
4880 NULL, &tracing_iter_fops);
4882 trace_create_file("tracing_cpumask", 0644, d_tracer,
4883 NULL, &tracing_cpumask_fops);
4885 trace_create_file("trace", 0644, d_tracer,
4886 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4888 trace_create_file("available_tracers", 0444, d_tracer,
4889 &global_trace, &show_traces_fops);
4891 trace_create_file("current_tracer", 0644, d_tracer,
4892 &global_trace, &set_tracer_fops);
4894 #ifdef CONFIG_TRACER_MAX_TRACE
4895 trace_create_file("tracing_max_latency", 0644, d_tracer,
4896 &tracing_max_latency, &tracing_max_lat_fops);
4899 trace_create_file("tracing_thresh", 0644, d_tracer,
4900 &tracing_thresh, &tracing_max_lat_fops);
4902 trace_create_file("README", 0444, d_tracer,
4903 NULL, &tracing_readme_fops);
4905 trace_create_file("trace_pipe", 0444, d_tracer,
4906 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4908 trace_create_file("buffer_size_kb", 0644, d_tracer,
4909 (void *) RING_BUFFER_ALL_CPUS, &tracing_entries_fops);
4911 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
4912 &global_trace, &tracing_total_entries_fops);
4914 trace_create_file("free_buffer", 0644, d_tracer,
4915 &global_trace, &tracing_free_buffer_fops);
4917 trace_create_file("trace_marker", 0220, d_tracer,
4918 NULL, &tracing_mark_fops);
4920 trace_create_file("saved_cmdlines", 0444, d_tracer,
4921 NULL, &tracing_saved_cmdlines_fops);
4923 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4926 trace_create_file("tracing_on", 0644, d_tracer,
4927 global_trace.buffer, &rb_simple_fops);
4929 #ifdef CONFIG_DYNAMIC_FTRACE
4930 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4931 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4934 create_trace_options_dir();
4936 for_each_tracing_cpu(cpu)
4937 tracing_init_debugfs_percpu(cpu);
4942 static int trace_panic_handler(struct notifier_block *this,
4943 unsigned long event, void *unused)
4945 if (ftrace_dump_on_oops)
4946 ftrace_dump(ftrace_dump_on_oops);
4950 static struct notifier_block trace_panic_notifier = {
4951 .notifier_call = trace_panic_handler,
4953 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4956 static int trace_die_handler(struct notifier_block *self,
4962 if (ftrace_dump_on_oops)
4963 ftrace_dump(ftrace_dump_on_oops);
4971 static struct notifier_block trace_die_notifier = {
4972 .notifier_call = trace_die_handler,
4977 * printk is set to max of 1024, we really don't need it that big.
4978 * Nothing should be printing 1000 characters anyway.
4980 #define TRACE_MAX_PRINT 1000
4983 * Define here KERN_TRACE so that we have one place to modify
4984 * it if we decide to change what log level the ftrace dump
4987 #define KERN_TRACE KERN_EMERG
4990 trace_printk_seq(struct trace_seq *s)
4992 /* Probably should print a warning here. */
4996 /* should be zero ended, but we are paranoid. */
4997 s->buffer[s->len] = 0;
4999 printk(KERN_TRACE "%s", s->buffer);
5004 void trace_init_global_iter(struct trace_iterator *iter)
5006 iter->tr = &global_trace;
5007 iter->trace = current_trace;
5008 iter->cpu_file = TRACE_PIPE_ALL_CPU;
5012 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
5014 static arch_spinlock_t ftrace_dump_lock =
5015 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
5016 /* use static because iter can be a bit big for the stack */
5017 static struct trace_iterator iter;
5018 unsigned int old_userobj;
5019 static int dump_ran;
5020 unsigned long flags;
5024 local_irq_save(flags);
5025 arch_spin_lock(&ftrace_dump_lock);
5033 /* Did function tracer already get disabled? */
5034 if (ftrace_is_dead()) {
5035 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
5036 printk("# MAY BE MISSING FUNCTION EVENTS\n");
5039 if (disable_tracing)
5042 trace_init_global_iter(&iter);
5044 for_each_tracing_cpu(cpu) {
5045 atomic_inc(&iter.tr->data[cpu]->disabled);
5048 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
5050 /* don't look at user memory in panic mode */
5051 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
5053 /* Simulate the iterator */
5054 iter.tr = &global_trace;
5055 iter.trace = current_trace;
5057 switch (oops_dump_mode) {
5059 iter.cpu_file = TRACE_PIPE_ALL_CPU;
5062 iter.cpu_file = raw_smp_processor_id();
5067 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
5068 iter.cpu_file = TRACE_PIPE_ALL_CPU;
5071 printk(KERN_TRACE "Dumping ftrace buffer:\n");
5074 * We need to stop all tracing on all CPUS to read the
5075 * the next buffer. This is a bit expensive, but is
5076 * not done often. We fill all what we can read,
5077 * and then release the locks again.
5080 while (!trace_empty(&iter)) {
5083 printk(KERN_TRACE "---------------------------------\n");
5087 /* reset all but tr, trace, and overruns */
5088 memset(&iter.seq, 0,
5089 sizeof(struct trace_iterator) -
5090 offsetof(struct trace_iterator, seq));
5091 iter.iter_flags |= TRACE_FILE_LAT_FMT;
5094 if (trace_find_next_entry_inc(&iter) != NULL) {
5097 ret = print_trace_line(&iter);
5098 if (ret != TRACE_TYPE_NO_CONSUME)
5099 trace_consume(&iter);
5101 touch_nmi_watchdog();
5103 trace_printk_seq(&iter.seq);
5107 printk(KERN_TRACE " (ftrace buffer empty)\n");
5109 printk(KERN_TRACE "---------------------------------\n");
5112 /* Re-enable tracing if requested */
5113 if (!disable_tracing) {
5114 trace_flags |= old_userobj;
5116 for_each_tracing_cpu(cpu) {
5117 atomic_dec(&iter.tr->data[cpu]->disabled);
5123 arch_spin_unlock(&ftrace_dump_lock);
5124 local_irq_restore(flags);
5127 /* By default: disable tracing after the dump */
5128 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
5130 __ftrace_dump(true, oops_dump_mode);
5132 EXPORT_SYMBOL_GPL(ftrace_dump);
5134 __init static int tracer_alloc_buffers(void)
5137 enum ring_buffer_flags rb_flags;
5142 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
5145 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
5146 goto out_free_buffer_mask;
5148 /* Only allocate trace_printk buffers if a trace_printk exists */
5149 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
5150 trace_printk_init_buffers();
5152 /* To save memory, keep the ring buffer size to its minimum */
5153 if (ring_buffer_expanded)
5154 ring_buf_size = trace_buf_size;
5158 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5160 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
5161 cpumask_copy(tracing_cpumask, cpu_all_mask);
5163 /* TODO: make the number of buffers hot pluggable with CPUS */
5164 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
5165 if (!global_trace.buffer) {
5166 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
5168 goto out_free_cpumask;
5170 if (global_trace.buffer_disabled)
5174 #ifdef CONFIG_TRACER_MAX_TRACE
5175 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
5176 if (!max_tr.buffer) {
5177 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
5179 ring_buffer_free(global_trace.buffer);
5180 goto out_free_cpumask;
5184 /* Allocate the first page for all buffers */
5185 for_each_tracing_cpu(i) {
5186 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
5187 max_tr.data[i] = &per_cpu(max_tr_data, i);
5190 set_buffer_entries(&global_trace, ring_buf_size);
5191 #ifdef CONFIG_TRACER_MAX_TRACE
5192 set_buffer_entries(&max_tr, 1);
5195 trace_init_cmdlines();
5197 register_tracer(&nop_trace);
5198 current_trace = &nop_trace;
5199 /* All seems OK, enable tracing */
5200 tracing_disabled = 0;
5202 atomic_notifier_chain_register(&panic_notifier_list,
5203 &trace_panic_notifier);
5205 register_die_notifier(&trace_die_notifier);
5210 free_cpumask_var(tracing_cpumask);
5211 out_free_buffer_mask:
5212 free_cpumask_var(tracing_buffer_mask);
5217 __init static int clear_boot_tracer(void)
5220 * The default tracer at boot buffer is an init section.
5221 * This function is called in lateinit. If we did not
5222 * find the boot tracer, then clear it out, to prevent
5223 * later registration from accessing the buffer that is
5224 * about to be freed.
5226 if (!default_bootup_tracer)
5229 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
5230 default_bootup_tracer);
5231 default_bootup_tracer = NULL;
5236 early_initcall(tracer_alloc_buffers);
5237 fs_initcall(tracer_init_debugfs);
5238 late_initcall(clear_boot_tracer);