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 * To prevent the comm cache from being overwritten when no
82 * tracing is active, only save the comm when a trace event
85 static DEFINE_PER_CPU(bool, trace_cmdline_save);
88 * Kill all tracing for good (never come back).
89 * It is initialized to 1 but will turn to zero if the initialization
90 * of the tracer is successful. But that is the only place that sets
93 static int tracing_disabled = 1;
95 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
97 cpumask_var_t __read_mostly tracing_buffer_mask;
100 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
102 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
103 * is set, then ftrace_dump is called. This will output the contents
104 * of the ftrace buffers to the console. This is very useful for
105 * capturing traces that lead to crashes and outputing it to a
108 * It is default off, but you can enable it with either specifying
109 * "ftrace_dump_on_oops" in the kernel command line, or setting
110 * /proc/sys/kernel/ftrace_dump_on_oops
111 * Set 1 if you want to dump buffers of all CPUs
112 * Set 2 if you want to dump the buffer of the CPU that triggered oops
115 enum ftrace_dump_mode ftrace_dump_on_oops;
117 static int tracing_set_tracer(const char *buf);
119 #define MAX_TRACER_SIZE 100
120 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
121 static char *default_bootup_tracer;
123 static int __init set_cmdline_ftrace(char *str)
125 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
126 default_bootup_tracer = bootup_tracer_buf;
127 /* We are using ftrace early, expand it */
128 ring_buffer_expanded = 1;
131 __setup("ftrace=", set_cmdline_ftrace);
133 static int __init set_ftrace_dump_on_oops(char *str)
135 if (*str++ != '=' || !*str) {
136 ftrace_dump_on_oops = DUMP_ALL;
140 if (!strcmp("orig_cpu", str)) {
141 ftrace_dump_on_oops = DUMP_ORIG;
147 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
149 unsigned long long ns2usecs(cycle_t nsec)
157 * The global_trace is the descriptor that holds the tracing
158 * buffers for the live tracing. For each CPU, it contains
159 * a link list of pages that will store trace entries. The
160 * page descriptor of the pages in the memory is used to hold
161 * the link list by linking the lru item in the page descriptor
162 * to each of the pages in the buffer per CPU.
164 * For each active CPU there is a data field that holds the
165 * pages for the buffer for that CPU. Each CPU has the same number
166 * of pages allocated for its buffer.
168 static struct trace_array global_trace;
170 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
172 int filter_current_check_discard(struct ring_buffer *buffer,
173 struct ftrace_event_call *call, void *rec,
174 struct ring_buffer_event *event)
176 return filter_check_discard(call, rec, buffer, event);
178 EXPORT_SYMBOL_GPL(filter_current_check_discard);
180 cycle_t ftrace_now(int cpu)
184 /* Early boot up does not have a buffer yet */
185 if (!global_trace.buffer)
186 return trace_clock_local();
188 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
189 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
195 * The max_tr is used to snapshot the global_trace when a maximum
196 * latency is reached. Some tracers will use this to store a maximum
197 * trace while it continues examining live traces.
199 * The buffers for the max_tr are set up the same as the global_trace.
200 * When a snapshot is taken, the link list of the max_tr is swapped
201 * with the link list of the global_trace and the buffers are reset for
202 * the global_trace so the tracing can continue.
204 static struct trace_array max_tr;
206 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
208 /* tracer_enabled is used to toggle activation of a tracer */
209 static int tracer_enabled = 1;
212 * tracing_is_enabled - return tracer_enabled status
214 * This function is used by other tracers to know the status
215 * of the tracer_enabled flag. Tracers may use this function
216 * to know if it should enable their features when starting
217 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
219 int tracing_is_enabled(void)
221 return tracer_enabled;
225 * trace_buf_size is the size in bytes that is allocated
226 * for a buffer. Note, the number of bytes is always rounded
229 * This number is purposely set to a low number of 16384.
230 * If the dump on oops happens, it will be much appreciated
231 * to not have to wait for all that output. Anyway this can be
232 * boot time and run time configurable.
234 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
236 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
238 /* trace_types holds a link list of available tracers. */
239 static struct tracer *trace_types __read_mostly;
241 /* current_trace points to the tracer that is currently active */
242 static struct tracer *current_trace __read_mostly;
245 * trace_types_lock is used to protect the trace_types list.
247 static DEFINE_MUTEX(trace_types_lock);
250 * serialize the access of the ring buffer
252 * ring buffer serializes readers, but it is low level protection.
253 * The validity of the events (which returns by ring_buffer_peek() ..etc)
254 * are not protected by ring buffer.
256 * The content of events may become garbage if we allow other process consumes
257 * these events concurrently:
258 * A) the page of the consumed events may become a normal page
259 * (not reader page) in ring buffer, and this page will be rewrited
260 * by events producer.
261 * B) The page of the consumed events may become a page for splice_read,
262 * and this page will be returned to system.
264 * These primitives allow multi process access to different cpu ring buffer
267 * These primitives don't distinguish read-only and read-consume access.
268 * Multi read-only access are also serialized.
272 static DECLARE_RWSEM(all_cpu_access_lock);
273 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
275 static inline void trace_access_lock(int cpu)
277 if (cpu == TRACE_PIPE_ALL_CPU) {
278 /* gain it for accessing the whole ring buffer. */
279 down_write(&all_cpu_access_lock);
281 /* gain it for accessing a cpu ring buffer. */
283 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
284 down_read(&all_cpu_access_lock);
286 /* Secondly block other access to this @cpu ring buffer. */
287 mutex_lock(&per_cpu(cpu_access_lock, cpu));
291 static inline void trace_access_unlock(int cpu)
293 if (cpu == TRACE_PIPE_ALL_CPU) {
294 up_write(&all_cpu_access_lock);
296 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
297 up_read(&all_cpu_access_lock);
301 static inline void trace_access_lock_init(void)
305 for_each_possible_cpu(cpu)
306 mutex_init(&per_cpu(cpu_access_lock, cpu));
311 static DEFINE_MUTEX(access_lock);
313 static inline void trace_access_lock(int cpu)
316 mutex_lock(&access_lock);
319 static inline void trace_access_unlock(int cpu)
322 mutex_unlock(&access_lock);
325 static inline void trace_access_lock_init(void)
331 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
332 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
334 /* trace_flags holds trace_options default values */
335 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
336 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
337 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
338 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS;
340 static int trace_stop_count;
341 static DEFINE_RAW_SPINLOCK(tracing_start_lock);
343 static void wakeup_work_handler(struct work_struct *work)
345 wake_up(&trace_wait);
348 static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler);
351 * tracing_on - enable tracing buffers
353 * This function enables tracing buffers that may have been
354 * disabled with tracing_off.
356 void tracing_on(void)
358 if (global_trace.buffer)
359 ring_buffer_record_on(global_trace.buffer);
361 * This flag is only looked at when buffers haven't been
362 * allocated yet. We don't really care about the race
363 * between setting this flag and actually turning
366 global_trace.buffer_disabled = 0;
368 EXPORT_SYMBOL_GPL(tracing_on);
371 * tracing_off - turn off tracing buffers
373 * This function stops the tracing buffers from recording data.
374 * It does not disable any overhead the tracers themselves may
375 * be causing. This function simply causes all recording to
376 * the ring buffers to fail.
378 void tracing_off(void)
380 if (global_trace.buffer)
381 ring_buffer_record_off(global_trace.buffer);
383 * This flag is only looked at when buffers haven't been
384 * allocated yet. We don't really care about the race
385 * between setting this flag and actually turning
388 global_trace.buffer_disabled = 1;
390 EXPORT_SYMBOL_GPL(tracing_off);
393 * tracing_is_on - show state of ring buffers enabled
395 int tracing_is_on(void)
397 if (global_trace.buffer)
398 return ring_buffer_record_is_on(global_trace.buffer);
399 return !global_trace.buffer_disabled;
401 EXPORT_SYMBOL_GPL(tracing_is_on);
404 * trace_wake_up - wake up tasks waiting for trace input
406 * Schedules a delayed work to wake up any task that is blocked on the
407 * trace_wait queue. These is used with trace_poll for tasks polling the
410 void trace_wake_up(void)
412 const unsigned long delay = msecs_to_jiffies(2);
414 if (trace_flags & TRACE_ITER_BLOCK)
416 schedule_delayed_work(&wakeup_work, delay);
419 static int __init set_buf_size(char *str)
421 unsigned long buf_size;
425 buf_size = memparse(str, &str);
426 /* nr_entries can not be zero */
429 trace_buf_size = buf_size;
432 __setup("trace_buf_size=", set_buf_size);
434 static int __init set_tracing_thresh(char *str)
436 unsigned long threshold;
441 ret = kstrtoul(str, 0, &threshold);
444 tracing_thresh = threshold * 1000;
447 __setup("tracing_thresh=", set_tracing_thresh);
449 unsigned long nsecs_to_usecs(unsigned long nsecs)
454 /* These must match the bit postions in trace_iterator_flags */
455 static const char *trace_options[] = {
488 { trace_clock_local, "local" },
489 { trace_clock_global, "global" },
490 { trace_clock_counter, "counter" },
496 * trace_parser_get_init - gets the buffer for trace parser
498 int trace_parser_get_init(struct trace_parser *parser, int size)
500 memset(parser, 0, sizeof(*parser));
502 parser->buffer = kmalloc(size, GFP_KERNEL);
511 * trace_parser_put - frees the buffer for trace parser
513 void trace_parser_put(struct trace_parser *parser)
515 kfree(parser->buffer);
519 * trace_get_user - reads the user input string separated by space
520 * (matched by isspace(ch))
522 * For each string found the 'struct trace_parser' is updated,
523 * and the function returns.
525 * Returns number of bytes read.
527 * See kernel/trace/trace.h for 'struct trace_parser' details.
529 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
530 size_t cnt, loff_t *ppos)
537 trace_parser_clear(parser);
539 ret = get_user(ch, ubuf++);
547 * The parser is not finished with the last write,
548 * continue reading the user input without skipping spaces.
551 /* skip white space */
552 while (cnt && isspace(ch)) {
553 ret = get_user(ch, ubuf++);
560 /* only spaces were written */
570 /* read the non-space input */
571 while (cnt && !isspace(ch)) {
572 if (parser->idx < parser->size - 1)
573 parser->buffer[parser->idx++] = ch;
578 ret = get_user(ch, ubuf++);
585 /* We either got finished input or we have to wait for another call. */
587 parser->buffer[parser->idx] = 0;
588 parser->cont = false;
591 parser->buffer[parser->idx++] = ch;
601 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
609 if (s->len <= s->readpos)
612 len = s->len - s->readpos;
615 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
625 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
629 if (s->len <= s->readpos)
632 len = s->len - s->readpos;
635 memcpy(buf, s->buffer + s->readpos, cnt);
642 * ftrace_max_lock is used to protect the swapping of buffers
643 * when taking a max snapshot. The buffers themselves are
644 * protected by per_cpu spinlocks. But the action of the swap
645 * needs its own lock.
647 * This is defined as a arch_spinlock_t in order to help
648 * with performance when lockdep debugging is enabled.
650 * It is also used in other places outside the update_max_tr
651 * so it needs to be defined outside of the
652 * CONFIG_TRACER_MAX_TRACE.
654 static arch_spinlock_t ftrace_max_lock =
655 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
657 unsigned long __read_mostly tracing_thresh;
659 #ifdef CONFIG_TRACER_MAX_TRACE
660 unsigned long __read_mostly tracing_max_latency;
663 * Copy the new maximum trace into the separate maximum-trace
664 * structure. (this way the maximum trace is permanently saved,
665 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
668 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
670 struct trace_array_cpu *data = tr->data[cpu];
671 struct trace_array_cpu *max_data;
674 max_tr.time_start = data->preempt_timestamp;
676 max_data = max_tr.data[cpu];
677 max_data->saved_latency = tracing_max_latency;
678 max_data->critical_start = data->critical_start;
679 max_data->critical_end = data->critical_end;
681 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
682 max_data->pid = tsk->pid;
683 max_data->uid = task_uid(tsk);
684 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
685 max_data->policy = tsk->policy;
686 max_data->rt_priority = tsk->rt_priority;
688 /* record this tasks comm */
689 tracing_record_cmdline(tsk);
693 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
695 * @tsk: the task with the latency
696 * @cpu: The cpu that initiated the trace.
698 * Flip the buffers between the @tr and the max_tr and record information
699 * about which task was the cause of this latency.
702 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
704 struct ring_buffer *buf = tr->buffer;
706 if (trace_stop_count)
709 WARN_ON_ONCE(!irqs_disabled());
710 if (!current_trace->use_max_tr) {
714 arch_spin_lock(&ftrace_max_lock);
716 tr->buffer = max_tr.buffer;
719 __update_max_tr(tr, tsk, cpu);
720 arch_spin_unlock(&ftrace_max_lock);
724 * update_max_tr_single - only copy one trace over, and reset the rest
726 * @tsk - task with the latency
727 * @cpu - the cpu of the buffer to copy.
729 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
732 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
736 if (trace_stop_count)
739 WARN_ON_ONCE(!irqs_disabled());
740 if (!current_trace->use_max_tr) {
745 arch_spin_lock(&ftrace_max_lock);
747 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
751 * We failed to swap the buffer due to a commit taking
752 * place on this CPU. We fail to record, but we reset
753 * the max trace buffer (no one writes directly to it)
754 * and flag that it failed.
756 trace_array_printk(&max_tr, _THIS_IP_,
757 "Failed to swap buffers due to commit in progress\n");
760 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
762 __update_max_tr(tr, tsk, cpu);
763 arch_spin_unlock(&ftrace_max_lock);
765 #endif /* CONFIG_TRACER_MAX_TRACE */
768 * register_tracer - register a tracer with the ftrace system.
769 * @type - the plugin for the tracer
771 * Register a new plugin tracer.
773 int register_tracer(struct tracer *type)
779 pr_info("Tracer must have a name\n");
783 if (strlen(type->name) >= MAX_TRACER_SIZE) {
784 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
788 mutex_lock(&trace_types_lock);
790 tracing_selftest_running = true;
792 for (t = trace_types; t; t = t->next) {
793 if (strcmp(type->name, t->name) == 0) {
795 pr_info("Tracer %s already registered\n",
803 type->set_flag = &dummy_set_flag;
805 type->flags = &dummy_tracer_flags;
807 if (!type->flags->opts)
808 type->flags->opts = dummy_tracer_opt;
809 if (!type->wait_pipe)
810 type->wait_pipe = default_wait_pipe;
813 #ifdef CONFIG_FTRACE_STARTUP_TEST
814 if (type->selftest && !tracing_selftest_disabled) {
815 struct tracer *saved_tracer = current_trace;
816 struct trace_array *tr = &global_trace;
819 * Run a selftest on this tracer.
820 * Here we reset the trace buffer, and set the current
821 * tracer to be this tracer. The tracer can then run some
822 * internal tracing to verify that everything is in order.
823 * If we fail, we do not register this tracer.
825 tracing_reset_online_cpus(tr);
827 current_trace = type;
829 /* If we expanded the buffers, make sure the max is expanded too */
830 if (ring_buffer_expanded && type->use_max_tr)
831 ring_buffer_resize(max_tr.buffer, trace_buf_size,
832 RING_BUFFER_ALL_CPUS);
834 /* the test is responsible for initializing and enabling */
835 pr_info("Testing tracer %s: ", type->name);
836 ret = type->selftest(type, tr);
837 /* the test is responsible for resetting too */
838 current_trace = saved_tracer;
840 printk(KERN_CONT "FAILED!\n");
841 /* Add the warning after printing 'FAILED' */
845 /* Only reset on passing, to avoid touching corrupted buffers */
846 tracing_reset_online_cpus(tr);
848 /* Shrink the max buffer again */
849 if (ring_buffer_expanded && type->use_max_tr)
850 ring_buffer_resize(max_tr.buffer, 1,
851 RING_BUFFER_ALL_CPUS);
853 printk(KERN_CONT "PASSED\n");
857 type->next = trace_types;
861 tracing_selftest_running = false;
862 mutex_unlock(&trace_types_lock);
864 if (ret || !default_bootup_tracer)
867 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
870 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
871 /* Do we want this tracer to start on bootup? */
872 tracing_set_tracer(type->name);
873 default_bootup_tracer = NULL;
874 /* disable other selftests, since this will break it. */
875 tracing_selftest_disabled = 1;
876 #ifdef CONFIG_FTRACE_STARTUP_TEST
877 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
885 void unregister_tracer(struct tracer *type)
889 mutex_lock(&trace_types_lock);
890 for (t = &trace_types; *t; t = &(*t)->next) {
894 pr_info("Tracer %s not registered\n", type->name);
900 if (type == current_trace && tracer_enabled) {
903 if (current_trace->stop)
904 current_trace->stop(&global_trace);
905 current_trace = &nop_trace;
908 mutex_unlock(&trace_types_lock);
911 void tracing_reset(struct trace_array *tr, int cpu)
913 struct ring_buffer *buffer = tr->buffer;
915 ring_buffer_record_disable(buffer);
917 /* Make sure all commits have finished */
919 ring_buffer_reset_cpu(buffer, cpu);
921 ring_buffer_record_enable(buffer);
924 void tracing_reset_online_cpus(struct trace_array *tr)
926 struct ring_buffer *buffer = tr->buffer;
929 ring_buffer_record_disable(buffer);
931 /* Make sure all commits have finished */
934 tr->time_start = ftrace_now(tr->cpu);
936 for_each_online_cpu(cpu)
937 ring_buffer_reset_cpu(buffer, cpu);
939 ring_buffer_record_enable(buffer);
942 void tracing_reset_current(int cpu)
944 tracing_reset(&global_trace, cpu);
947 void tracing_reset_current_online_cpus(void)
949 tracing_reset_online_cpus(&global_trace);
952 #define SAVED_CMDLINES 128
953 #define NO_CMDLINE_MAP UINT_MAX
954 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
955 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
956 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
957 static int cmdline_idx;
958 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
960 /* temporary disable recording */
961 static atomic_t trace_record_cmdline_disabled __read_mostly;
963 static void trace_init_cmdlines(void)
965 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
966 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
970 int is_tracing_stopped(void)
972 return trace_stop_count;
976 * ftrace_off_permanent - disable all ftrace code permanently
978 * This should only be called when a serious anomally has
979 * been detected. This will turn off the function tracing,
980 * ring buffers, and other tracing utilites. It takes no
981 * locks and can be called from any context.
983 void ftrace_off_permanent(void)
985 tracing_disabled = 1;
987 tracing_off_permanent();
991 * tracing_start - quick start of the tracer
993 * If tracing is enabled but was stopped by tracing_stop,
994 * this will start the tracer back up.
996 void tracing_start(void)
998 struct ring_buffer *buffer;
1001 if (tracing_disabled)
1004 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1005 if (--trace_stop_count) {
1006 if (trace_stop_count < 0) {
1007 /* Someone screwed up their debugging */
1009 trace_stop_count = 0;
1014 /* Prevent the buffers from switching */
1015 arch_spin_lock(&ftrace_max_lock);
1017 buffer = global_trace.buffer;
1019 ring_buffer_record_enable(buffer);
1021 buffer = max_tr.buffer;
1023 ring_buffer_record_enable(buffer);
1025 arch_spin_unlock(&ftrace_max_lock);
1029 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1033 * tracing_stop - quick stop of the tracer
1035 * Light weight way to stop tracing. Use in conjunction with
1038 void tracing_stop(void)
1040 struct ring_buffer *buffer;
1041 unsigned long flags;
1044 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1045 if (trace_stop_count++)
1048 /* Prevent the buffers from switching */
1049 arch_spin_lock(&ftrace_max_lock);
1051 buffer = global_trace.buffer;
1053 ring_buffer_record_disable(buffer);
1055 buffer = max_tr.buffer;
1057 ring_buffer_record_disable(buffer);
1059 arch_spin_unlock(&ftrace_max_lock);
1062 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1065 void trace_stop_cmdline_recording(void);
1067 static void trace_save_cmdline(struct task_struct *tsk)
1071 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1075 * It's not the end of the world if we don't get
1076 * the lock, but we also don't want to spin
1077 * nor do we want to disable interrupts,
1078 * so if we miss here, then better luck next time.
1080 if (!arch_spin_trylock(&trace_cmdline_lock))
1083 idx = map_pid_to_cmdline[tsk->pid];
1084 if (idx == NO_CMDLINE_MAP) {
1085 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1088 * Check whether the cmdline buffer at idx has a pid
1089 * mapped. We are going to overwrite that entry so we
1090 * need to clear the map_pid_to_cmdline. Otherwise we
1091 * would read the new comm for the old pid.
1093 pid = map_cmdline_to_pid[idx];
1094 if (pid != NO_CMDLINE_MAP)
1095 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1097 map_cmdline_to_pid[idx] = tsk->pid;
1098 map_pid_to_cmdline[tsk->pid] = idx;
1103 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1105 arch_spin_unlock(&trace_cmdline_lock);
1108 void trace_find_cmdline(int pid, char comm[])
1113 strcpy(comm, "<idle>");
1117 if (WARN_ON_ONCE(pid < 0)) {
1118 strcpy(comm, "<XXX>");
1122 if (pid > PID_MAX_DEFAULT) {
1123 strcpy(comm, "<...>");
1128 arch_spin_lock(&trace_cmdline_lock);
1129 map = map_pid_to_cmdline[pid];
1130 if (map != NO_CMDLINE_MAP)
1131 strcpy(comm, saved_cmdlines[map]);
1133 strcpy(comm, "<...>");
1135 arch_spin_unlock(&trace_cmdline_lock);
1139 void tracing_record_cmdline(struct task_struct *tsk)
1141 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1145 if (!__this_cpu_read(trace_cmdline_save))
1148 __this_cpu_write(trace_cmdline_save, false);
1150 trace_save_cmdline(tsk);
1154 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1157 struct task_struct *tsk = current;
1159 entry->preempt_count = pc & 0xff;
1160 entry->pid = (tsk) ? tsk->pid : 0;
1163 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1164 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1166 TRACE_FLAG_IRQS_NOSUPPORT |
1168 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1169 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1170 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1172 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1174 struct ring_buffer_event *
1175 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1178 unsigned long flags, int pc)
1180 struct ring_buffer_event *event;
1182 event = ring_buffer_lock_reserve(buffer, len);
1183 if (event != NULL) {
1184 struct trace_entry *ent = ring_buffer_event_data(event);
1186 tracing_generic_entry_update(ent, flags, pc);
1194 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1196 __this_cpu_write(trace_cmdline_save, true);
1197 ring_buffer_unlock_commit(buffer, event);
1201 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1202 struct ring_buffer_event *event,
1203 unsigned long flags, int pc,
1206 __buffer_unlock_commit(buffer, event);
1208 ftrace_trace_stack(buffer, flags, 6, pc);
1209 ftrace_trace_userstack(buffer, flags, pc);
1215 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1216 struct ring_buffer_event *event,
1217 unsigned long flags, int pc)
1219 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1222 struct ring_buffer_event *
1223 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1224 int type, unsigned long len,
1225 unsigned long flags, int pc)
1227 *current_rb = global_trace.buffer;
1228 return trace_buffer_lock_reserve(*current_rb,
1229 type, len, flags, pc);
1231 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1233 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1234 struct ring_buffer_event *event,
1235 unsigned long flags, int pc)
1237 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1239 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1241 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1242 struct ring_buffer_event *event,
1243 unsigned long flags, int pc)
1245 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1247 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1249 void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1250 struct ring_buffer_event *event,
1251 unsigned long flags, int pc,
1252 struct pt_regs *regs)
1254 __buffer_unlock_commit(buffer, event);
1256 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1257 ftrace_trace_userstack(buffer, flags, pc);
1259 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs);
1261 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1262 struct ring_buffer_event *event)
1264 ring_buffer_discard_commit(buffer, event);
1266 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1269 trace_function(struct trace_array *tr,
1270 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1273 struct ftrace_event_call *call = &event_function;
1274 struct ring_buffer *buffer = tr->buffer;
1275 struct ring_buffer_event *event;
1276 struct ftrace_entry *entry;
1278 /* If we are reading the ring buffer, don't trace */
1279 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1282 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1286 entry = ring_buffer_event_data(event);
1288 entry->parent_ip = parent_ip;
1290 if (!filter_check_discard(call, entry, buffer, event))
1291 __buffer_unlock_commit(buffer, event);
1295 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1296 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1299 if (likely(!atomic_read(&data->disabled)))
1300 trace_function(tr, ip, parent_ip, flags, pc);
1303 #ifdef CONFIG_STACKTRACE
1305 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1306 struct ftrace_stack {
1307 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1310 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1311 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1313 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1314 unsigned long flags,
1315 int skip, int pc, struct pt_regs *regs)
1317 struct ftrace_event_call *call = &event_kernel_stack;
1318 struct ring_buffer_event *event;
1319 struct stack_entry *entry;
1320 struct stack_trace trace;
1322 int size = FTRACE_STACK_ENTRIES;
1324 trace.nr_entries = 0;
1328 * Since events can happen in NMIs there's no safe way to
1329 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1330 * or NMI comes in, it will just have to use the default
1331 * FTRACE_STACK_SIZE.
1333 preempt_disable_notrace();
1335 use_stack = ++__get_cpu_var(ftrace_stack_reserve);
1337 * We don't need any atomic variables, just a barrier.
1338 * If an interrupt comes in, we don't care, because it would
1339 * have exited and put the counter back to what we want.
1340 * We just need a barrier to keep gcc from moving things
1344 if (use_stack == 1) {
1345 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1346 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1349 save_stack_trace_regs(regs, &trace);
1351 save_stack_trace(&trace);
1353 if (trace.nr_entries > size)
1354 size = trace.nr_entries;
1356 /* From now on, use_stack is a boolean */
1359 size *= sizeof(unsigned long);
1361 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1362 sizeof(*entry) + size, flags, pc);
1365 entry = ring_buffer_event_data(event);
1367 memset(&entry->caller, 0, size);
1370 memcpy(&entry->caller, trace.entries,
1371 trace.nr_entries * sizeof(unsigned long));
1373 trace.max_entries = FTRACE_STACK_ENTRIES;
1374 trace.entries = entry->caller;
1376 save_stack_trace_regs(regs, &trace);
1378 save_stack_trace(&trace);
1381 entry->size = trace.nr_entries;
1383 if (!filter_check_discard(call, entry, buffer, event))
1384 __buffer_unlock_commit(buffer, event);
1387 /* Again, don't let gcc optimize things here */
1389 __get_cpu_var(ftrace_stack_reserve)--;
1390 preempt_enable_notrace();
1394 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1395 int skip, int pc, struct pt_regs *regs)
1397 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1400 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1403 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1406 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1409 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1412 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1415 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
1419 * trace_dump_stack - record a stack back trace in the trace buffer
1421 void trace_dump_stack(void)
1423 unsigned long flags;
1425 if (tracing_disabled || tracing_selftest_running)
1428 local_save_flags(flags);
1430 /* skipping 3 traces, seems to get us at the caller of this function */
1431 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
1434 static DEFINE_PER_CPU(int, user_stack_count);
1437 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1439 struct ftrace_event_call *call = &event_user_stack;
1440 struct ring_buffer_event *event;
1441 struct userstack_entry *entry;
1442 struct stack_trace trace;
1444 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1448 * NMIs can not handle page faults, even with fix ups.
1449 * The save user stack can (and often does) fault.
1451 if (unlikely(in_nmi()))
1455 * prevent recursion, since the user stack tracing may
1456 * trigger other kernel events.
1459 if (__this_cpu_read(user_stack_count))
1462 __this_cpu_inc(user_stack_count);
1464 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1465 sizeof(*entry), flags, pc);
1467 goto out_drop_count;
1468 entry = ring_buffer_event_data(event);
1470 entry->tgid = current->tgid;
1471 memset(&entry->caller, 0, sizeof(entry->caller));
1473 trace.nr_entries = 0;
1474 trace.max_entries = FTRACE_STACK_ENTRIES;
1476 trace.entries = entry->caller;
1478 save_stack_trace_user(&trace);
1479 if (!filter_check_discard(call, entry, buffer, event))
1480 __buffer_unlock_commit(buffer, event);
1483 __this_cpu_dec(user_stack_count);
1489 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1491 ftrace_trace_userstack(tr, flags, preempt_count());
1495 #endif /* CONFIG_STACKTRACE */
1497 /* created for use with alloc_percpu */
1498 struct trace_buffer_struct {
1499 char buffer[TRACE_BUF_SIZE];
1502 static struct trace_buffer_struct *trace_percpu_buffer;
1503 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1504 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1505 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1508 * The buffer used is dependent on the context. There is a per cpu
1509 * buffer for normal context, softirq contex, hard irq context and
1510 * for NMI context. Thise allows for lockless recording.
1512 * Note, if the buffers failed to be allocated, then this returns NULL
1514 static char *get_trace_buf(void)
1516 struct trace_buffer_struct *percpu_buffer;
1517 struct trace_buffer_struct *buffer;
1520 * If we have allocated per cpu buffers, then we do not
1521 * need to do any locking.
1524 percpu_buffer = trace_percpu_nmi_buffer;
1526 percpu_buffer = trace_percpu_irq_buffer;
1527 else if (in_softirq())
1528 percpu_buffer = trace_percpu_sirq_buffer;
1530 percpu_buffer = trace_percpu_buffer;
1535 buffer = per_cpu_ptr(percpu_buffer, smp_processor_id());
1537 return buffer->buffer;
1540 static int alloc_percpu_trace_buffer(void)
1542 struct trace_buffer_struct *buffers;
1543 struct trace_buffer_struct *sirq_buffers;
1544 struct trace_buffer_struct *irq_buffers;
1545 struct trace_buffer_struct *nmi_buffers;
1547 buffers = alloc_percpu(struct trace_buffer_struct);
1551 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1555 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1559 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1563 trace_percpu_buffer = buffers;
1564 trace_percpu_sirq_buffer = sirq_buffers;
1565 trace_percpu_irq_buffer = irq_buffers;
1566 trace_percpu_nmi_buffer = nmi_buffers;
1571 free_percpu(irq_buffers);
1573 free_percpu(sirq_buffers);
1575 free_percpu(buffers);
1577 WARN(1, "Could not allocate percpu trace_printk buffer");
1581 static int buffers_allocated;
1583 void trace_printk_init_buffers(void)
1585 if (buffers_allocated)
1588 if (alloc_percpu_trace_buffer())
1591 pr_info("ftrace: Allocated trace_printk buffers\n");
1593 /* Expand the buffers to set size */
1594 tracing_update_buffers();
1596 buffers_allocated = 1;
1599 * trace_printk_init_buffers() can be called by modules.
1600 * If that happens, then we need to start cmdline recording
1601 * directly here. If the global_trace.buffer is already
1602 * allocated here, then this was called by module code.
1604 if (global_trace.buffer)
1605 tracing_start_cmdline_record();
1608 void trace_printk_start_comm(void)
1610 /* Start tracing comms if trace printk is set */
1611 if (!buffers_allocated)
1613 tracing_start_cmdline_record();
1616 static void trace_printk_start_stop_comm(int enabled)
1618 if (!buffers_allocated)
1622 tracing_start_cmdline_record();
1624 tracing_stop_cmdline_record();
1628 * trace_vbprintk - write binary msg to tracing buffer
1631 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1633 struct ftrace_event_call *call = &event_bprint;
1634 struct ring_buffer_event *event;
1635 struct ring_buffer *buffer;
1636 struct trace_array *tr = &global_trace;
1637 struct bprint_entry *entry;
1638 unsigned long flags;
1640 int len = 0, size, pc;
1642 if (unlikely(tracing_selftest_running || tracing_disabled))
1645 /* Don't pollute graph traces with trace_vprintk internals */
1646 pause_graph_tracing();
1648 pc = preempt_count();
1649 preempt_disable_notrace();
1651 tbuffer = get_trace_buf();
1657 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1659 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1662 local_save_flags(flags);
1663 size = sizeof(*entry) + sizeof(u32) * len;
1664 buffer = tr->buffer;
1665 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1669 entry = ring_buffer_event_data(event);
1673 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1674 if (!filter_check_discard(call, entry, buffer, event)) {
1675 __buffer_unlock_commit(buffer, event);
1676 ftrace_trace_stack(buffer, flags, 6, pc);
1680 preempt_enable_notrace();
1681 unpause_graph_tracing();
1685 EXPORT_SYMBOL_GPL(trace_vbprintk);
1687 int trace_array_printk(struct trace_array *tr,
1688 unsigned long ip, const char *fmt, ...)
1693 if (!(trace_flags & TRACE_ITER_PRINTK))
1697 ret = trace_array_vprintk(tr, ip, fmt, ap);
1702 int trace_array_vprintk(struct trace_array *tr,
1703 unsigned long ip, const char *fmt, va_list args)
1705 struct ftrace_event_call *call = &event_print;
1706 struct ring_buffer_event *event;
1707 struct ring_buffer *buffer;
1708 int len = 0, size, pc;
1709 struct print_entry *entry;
1710 unsigned long flags;
1713 if (tracing_disabled || tracing_selftest_running)
1716 /* Don't pollute graph traces with trace_vprintk internals */
1717 pause_graph_tracing();
1719 pc = preempt_count();
1720 preempt_disable_notrace();
1723 tbuffer = get_trace_buf();
1729 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1730 if (len > TRACE_BUF_SIZE)
1733 local_save_flags(flags);
1734 size = sizeof(*entry) + len + 1;
1735 buffer = tr->buffer;
1736 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1740 entry = ring_buffer_event_data(event);
1743 memcpy(&entry->buf, tbuffer, len);
1744 entry->buf[len] = '\0';
1745 if (!filter_check_discard(call, entry, buffer, event)) {
1746 __buffer_unlock_commit(buffer, event);
1747 ftrace_trace_stack(buffer, flags, 6, pc);
1750 preempt_enable_notrace();
1751 unpause_graph_tracing();
1756 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1758 return trace_array_vprintk(&global_trace, ip, fmt, args);
1760 EXPORT_SYMBOL_GPL(trace_vprintk);
1762 static void trace_iterator_increment(struct trace_iterator *iter)
1764 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
1768 ring_buffer_read(buf_iter, NULL);
1771 static struct trace_entry *
1772 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1773 unsigned long *lost_events)
1775 struct ring_buffer_event *event;
1776 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
1779 event = ring_buffer_iter_peek(buf_iter, ts);
1781 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1785 iter->ent_size = ring_buffer_event_length(event);
1786 return ring_buffer_event_data(event);
1792 static struct trace_entry *
1793 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1794 unsigned long *missing_events, u64 *ent_ts)
1796 struct ring_buffer *buffer = iter->tr->buffer;
1797 struct trace_entry *ent, *next = NULL;
1798 unsigned long lost_events = 0, next_lost = 0;
1799 int cpu_file = iter->cpu_file;
1800 u64 next_ts = 0, ts;
1806 * If we are in a per_cpu trace file, don't bother by iterating over
1807 * all cpu and peek directly.
1809 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1810 if (ring_buffer_empty_cpu(buffer, cpu_file))
1812 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1814 *ent_cpu = cpu_file;
1819 for_each_tracing_cpu(cpu) {
1821 if (ring_buffer_empty_cpu(buffer, cpu))
1824 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1827 * Pick the entry with the smallest timestamp:
1829 if (ent && (!next || ts < next_ts)) {
1833 next_lost = lost_events;
1834 next_size = iter->ent_size;
1838 iter->ent_size = next_size;
1841 *ent_cpu = next_cpu;
1847 *missing_events = next_lost;
1852 /* Find the next real entry, without updating the iterator itself */
1853 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1854 int *ent_cpu, u64 *ent_ts)
1856 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1859 /* Find the next real entry, and increment the iterator to the next entry */
1860 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1862 iter->ent = __find_next_entry(iter, &iter->cpu,
1863 &iter->lost_events, &iter->ts);
1866 trace_iterator_increment(iter);
1868 return iter->ent ? iter : NULL;
1871 static void trace_consume(struct trace_iterator *iter)
1873 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1874 &iter->lost_events);
1877 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1879 struct trace_iterator *iter = m->private;
1883 WARN_ON_ONCE(iter->leftover);
1887 /* can't go backwards */
1892 ent = trace_find_next_entry_inc(iter);
1896 while (ent && iter->idx < i)
1897 ent = trace_find_next_entry_inc(iter);
1904 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1906 struct trace_array *tr = iter->tr;
1907 struct ring_buffer_event *event;
1908 struct ring_buffer_iter *buf_iter;
1909 unsigned long entries = 0;
1912 tr->data[cpu]->skipped_entries = 0;
1914 buf_iter = trace_buffer_iter(iter, cpu);
1918 ring_buffer_iter_reset(buf_iter);
1921 * We could have the case with the max latency tracers
1922 * that a reset never took place on a cpu. This is evident
1923 * by the timestamp being before the start of the buffer.
1925 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1926 if (ts >= iter->tr->time_start)
1929 ring_buffer_read(buf_iter, NULL);
1932 tr->data[cpu]->skipped_entries = entries;
1936 * The current tracer is copied to avoid a global locking
1939 static void *s_start(struct seq_file *m, loff_t *pos)
1941 struct trace_iterator *iter = m->private;
1942 static struct tracer *old_tracer;
1943 int cpu_file = iter->cpu_file;
1948 /* copy the tracer to avoid using a global lock all around */
1949 mutex_lock(&trace_types_lock);
1950 if (unlikely(old_tracer != current_trace && current_trace)) {
1951 old_tracer = current_trace;
1952 *iter->trace = *current_trace;
1954 mutex_unlock(&trace_types_lock);
1956 atomic_inc(&trace_record_cmdline_disabled);
1958 if (*pos != iter->pos) {
1963 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1964 for_each_tracing_cpu(cpu)
1965 tracing_iter_reset(iter, cpu);
1967 tracing_iter_reset(iter, cpu_file);
1970 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1975 * If we overflowed the seq_file before, then we want
1976 * to just reuse the trace_seq buffer again.
1982 p = s_next(m, p, &l);
1986 trace_event_read_lock();
1987 trace_access_lock(cpu_file);
1991 static void s_stop(struct seq_file *m, void *p)
1993 struct trace_iterator *iter = m->private;
1995 atomic_dec(&trace_record_cmdline_disabled);
1996 trace_access_unlock(iter->cpu_file);
1997 trace_event_read_unlock();
2001 get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *entries)
2003 unsigned long count;
2009 for_each_tracing_cpu(cpu) {
2010 count = ring_buffer_entries_cpu(tr->buffer, cpu);
2012 * If this buffer has skipped entries, then we hold all
2013 * entries for the trace and we need to ignore the
2014 * ones before the time stamp.
2016 if (tr->data[cpu]->skipped_entries) {
2017 count -= tr->data[cpu]->skipped_entries;
2018 /* total is the same as the entries */
2022 ring_buffer_overrun_cpu(tr->buffer, cpu);
2027 static void print_lat_help_header(struct seq_file *m)
2029 seq_puts(m, "# _------=> CPU# \n");
2030 seq_puts(m, "# / _-----=> irqs-off \n");
2031 seq_puts(m, "# | / _----=> need-resched \n");
2032 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2033 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2034 seq_puts(m, "# |||| / delay \n");
2035 seq_puts(m, "# cmd pid ||||| time | caller \n");
2036 seq_puts(m, "# \\ / ||||| \\ | / \n");
2039 static void print_event_info(struct trace_array *tr, struct seq_file *m)
2041 unsigned long total;
2042 unsigned long entries;
2044 get_total_entries(tr, &total, &entries);
2045 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2046 entries, total, num_online_cpus());
2050 static void print_func_help_header(struct trace_array *tr, struct seq_file *m)
2052 print_event_info(tr, m);
2053 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2054 seq_puts(m, "# | | | | |\n");
2057 static void print_func_help_header_irq(struct trace_array *tr, struct seq_file *m)
2059 print_event_info(tr, m);
2060 seq_puts(m, "# _-----=> irqs-off\n");
2061 seq_puts(m, "# / _----=> need-resched\n");
2062 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2063 seq_puts(m, "# || / _--=> preempt-depth\n");
2064 seq_puts(m, "# ||| / delay\n");
2065 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2066 seq_puts(m, "# | | | |||| | |\n");
2070 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2072 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2073 struct trace_array *tr = iter->tr;
2074 struct trace_array_cpu *data = tr->data[tr->cpu];
2075 struct tracer *type = current_trace;
2076 unsigned long entries;
2077 unsigned long total;
2078 const char *name = "preemption";
2083 get_total_entries(tr, &total, &entries);
2085 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2087 seq_puts(m, "# -----------------------------------"
2088 "---------------------------------\n");
2089 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2090 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2091 nsecs_to_usecs(data->saved_latency),
2095 #if defined(CONFIG_PREEMPT_NONE)
2097 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2099 #elif defined(CONFIG_PREEMPT)
2104 /* These are reserved for later use */
2107 seq_printf(m, " #P:%d)\n", num_online_cpus());
2111 seq_puts(m, "# -----------------\n");
2112 seq_printf(m, "# | task: %.16s-%d "
2113 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2114 data->comm, data->pid,
2115 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2116 data->policy, data->rt_priority);
2117 seq_puts(m, "# -----------------\n");
2119 if (data->critical_start) {
2120 seq_puts(m, "# => started at: ");
2121 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2122 trace_print_seq(m, &iter->seq);
2123 seq_puts(m, "\n# => ended at: ");
2124 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2125 trace_print_seq(m, &iter->seq);
2126 seq_puts(m, "\n#\n");
2132 static void test_cpu_buff_start(struct trace_iterator *iter)
2134 struct trace_seq *s = &iter->seq;
2136 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2139 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2142 if (cpumask_test_cpu(iter->cpu, iter->started))
2145 if (iter->tr->data[iter->cpu]->skipped_entries)
2148 cpumask_set_cpu(iter->cpu, iter->started);
2150 /* Don't print started cpu buffer for the first entry of the trace */
2152 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2156 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2158 struct trace_seq *s = &iter->seq;
2159 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2160 struct trace_entry *entry;
2161 struct trace_event *event;
2165 test_cpu_buff_start(iter);
2167 event = ftrace_find_event(entry->type);
2169 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2170 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2171 if (!trace_print_lat_context(iter))
2174 if (!trace_print_context(iter))
2180 return event->funcs->trace(iter, sym_flags, event);
2182 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2185 return TRACE_TYPE_HANDLED;
2187 return TRACE_TYPE_PARTIAL_LINE;
2190 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2192 struct trace_seq *s = &iter->seq;
2193 struct trace_entry *entry;
2194 struct trace_event *event;
2198 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2199 if (!trace_seq_printf(s, "%d %d %llu ",
2200 entry->pid, iter->cpu, iter->ts))
2204 event = ftrace_find_event(entry->type);
2206 return event->funcs->raw(iter, 0, event);
2208 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2211 return TRACE_TYPE_HANDLED;
2213 return TRACE_TYPE_PARTIAL_LINE;
2216 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2218 struct trace_seq *s = &iter->seq;
2219 unsigned char newline = '\n';
2220 struct trace_entry *entry;
2221 struct trace_event *event;
2225 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2226 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2227 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2228 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2231 event = ftrace_find_event(entry->type);
2233 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2234 if (ret != TRACE_TYPE_HANDLED)
2238 SEQ_PUT_FIELD_RET(s, newline);
2240 return TRACE_TYPE_HANDLED;
2243 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2245 struct trace_seq *s = &iter->seq;
2246 struct trace_entry *entry;
2247 struct trace_event *event;
2251 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2252 SEQ_PUT_FIELD_RET(s, entry->pid);
2253 SEQ_PUT_FIELD_RET(s, iter->cpu);
2254 SEQ_PUT_FIELD_RET(s, iter->ts);
2257 event = ftrace_find_event(entry->type);
2258 return event ? event->funcs->binary(iter, 0, event) :
2262 int trace_empty(struct trace_iterator *iter)
2264 struct ring_buffer_iter *buf_iter;
2267 /* If we are looking at one CPU buffer, only check that one */
2268 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2269 cpu = iter->cpu_file;
2270 buf_iter = trace_buffer_iter(iter, cpu);
2272 if (!ring_buffer_iter_empty(buf_iter))
2275 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2281 for_each_tracing_cpu(cpu) {
2282 buf_iter = trace_buffer_iter(iter, cpu);
2284 if (!ring_buffer_iter_empty(buf_iter))
2287 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2295 /* Called with trace_event_read_lock() held. */
2296 enum print_line_t print_trace_line(struct trace_iterator *iter)
2298 enum print_line_t ret;
2300 if (iter->lost_events &&
2301 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2302 iter->cpu, iter->lost_events))
2303 return TRACE_TYPE_PARTIAL_LINE;
2305 if (iter->trace && iter->trace->print_line) {
2306 ret = iter->trace->print_line(iter);
2307 if (ret != TRACE_TYPE_UNHANDLED)
2311 if (iter->ent->type == TRACE_BPRINT &&
2312 trace_flags & TRACE_ITER_PRINTK &&
2313 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2314 return trace_print_bprintk_msg_only(iter);
2316 if (iter->ent->type == TRACE_PRINT &&
2317 trace_flags & TRACE_ITER_PRINTK &&
2318 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2319 return trace_print_printk_msg_only(iter);
2321 if (trace_flags & TRACE_ITER_BIN)
2322 return print_bin_fmt(iter);
2324 if (trace_flags & TRACE_ITER_HEX)
2325 return print_hex_fmt(iter);
2327 if (trace_flags & TRACE_ITER_RAW)
2328 return print_raw_fmt(iter);
2330 return print_trace_fmt(iter);
2333 void trace_latency_header(struct seq_file *m)
2335 struct trace_iterator *iter = m->private;
2337 /* print nothing if the buffers are empty */
2338 if (trace_empty(iter))
2341 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2342 print_trace_header(m, iter);
2344 if (!(trace_flags & TRACE_ITER_VERBOSE))
2345 print_lat_help_header(m);
2348 void trace_default_header(struct seq_file *m)
2350 struct trace_iterator *iter = m->private;
2352 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2355 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2356 /* print nothing if the buffers are empty */
2357 if (trace_empty(iter))
2359 print_trace_header(m, iter);
2360 if (!(trace_flags & TRACE_ITER_VERBOSE))
2361 print_lat_help_header(m);
2363 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2364 if (trace_flags & TRACE_ITER_IRQ_INFO)
2365 print_func_help_header_irq(iter->tr, m);
2367 print_func_help_header(iter->tr, m);
2372 static void test_ftrace_alive(struct seq_file *m)
2374 if (!ftrace_is_dead())
2376 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2377 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2380 static int s_show(struct seq_file *m, void *v)
2382 struct trace_iterator *iter = v;
2385 if (iter->ent == NULL) {
2387 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2389 test_ftrace_alive(m);
2391 if (iter->trace && iter->trace->print_header)
2392 iter->trace->print_header(m);
2394 trace_default_header(m);
2396 } else if (iter->leftover) {
2398 * If we filled the seq_file buffer earlier, we
2399 * want to just show it now.
2401 ret = trace_print_seq(m, &iter->seq);
2403 /* ret should this time be zero, but you never know */
2404 iter->leftover = ret;
2407 print_trace_line(iter);
2408 ret = trace_print_seq(m, &iter->seq);
2410 * If we overflow the seq_file buffer, then it will
2411 * ask us for this data again at start up.
2413 * ret is 0 if seq_file write succeeded.
2416 iter->leftover = ret;
2422 static const struct seq_operations tracer_seq_ops = {
2429 static struct trace_iterator *
2430 __tracing_open(struct inode *inode, struct file *file)
2432 long cpu_file = (long) inode->i_private;
2433 struct trace_iterator *iter;
2436 if (tracing_disabled)
2437 return ERR_PTR(-ENODEV);
2439 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2441 return ERR_PTR(-ENOMEM);
2443 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2445 if (!iter->buffer_iter)
2449 * We make a copy of the current tracer to avoid concurrent
2450 * changes on it while we are reading.
2452 mutex_lock(&trace_types_lock);
2453 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2458 *iter->trace = *current_trace;
2460 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2463 if (current_trace && current_trace->print_max)
2466 iter->tr = &global_trace;
2468 mutex_init(&iter->mutex);
2469 iter->cpu_file = cpu_file;
2471 /* Notify the tracer early; before we stop tracing. */
2472 if (iter->trace && iter->trace->open)
2473 iter->trace->open(iter);
2475 /* Annotate start of buffers if we had overruns */
2476 if (ring_buffer_overruns(iter->tr->buffer))
2477 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2479 /* stop the trace while dumping */
2482 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2483 for_each_tracing_cpu(cpu) {
2484 iter->buffer_iter[cpu] =
2485 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2487 ring_buffer_read_prepare_sync();
2488 for_each_tracing_cpu(cpu) {
2489 ring_buffer_read_start(iter->buffer_iter[cpu]);
2490 tracing_iter_reset(iter, cpu);
2493 cpu = iter->cpu_file;
2494 iter->buffer_iter[cpu] =
2495 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2496 ring_buffer_read_prepare_sync();
2497 ring_buffer_read_start(iter->buffer_iter[cpu]);
2498 tracing_iter_reset(iter, cpu);
2501 mutex_unlock(&trace_types_lock);
2506 mutex_unlock(&trace_types_lock);
2508 kfree(iter->buffer_iter);
2510 seq_release_private(inode, file);
2511 return ERR_PTR(-ENOMEM);
2514 int tracing_open_generic(struct inode *inode, struct file *filp)
2516 if (tracing_disabled)
2519 filp->private_data = inode->i_private;
2523 static int tracing_release(struct inode *inode, struct file *file)
2525 struct seq_file *m = file->private_data;
2526 struct trace_iterator *iter;
2529 if (!(file->f_mode & FMODE_READ))
2534 mutex_lock(&trace_types_lock);
2535 for_each_tracing_cpu(cpu) {
2536 if (iter->buffer_iter[cpu])
2537 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2540 if (iter->trace && iter->trace->close)
2541 iter->trace->close(iter);
2543 /* reenable tracing if it was previously enabled */
2545 mutex_unlock(&trace_types_lock);
2547 mutex_destroy(&iter->mutex);
2548 free_cpumask_var(iter->started);
2550 kfree(iter->buffer_iter);
2551 seq_release_private(inode, file);
2555 static int tracing_open(struct inode *inode, struct file *file)
2557 struct trace_iterator *iter;
2560 /* If this file was open for write, then erase contents */
2561 if ((file->f_mode & FMODE_WRITE) &&
2562 (file->f_flags & O_TRUNC)) {
2563 long cpu = (long) inode->i_private;
2565 if (cpu == TRACE_PIPE_ALL_CPU)
2566 tracing_reset_online_cpus(&global_trace);
2568 tracing_reset(&global_trace, cpu);
2571 if (file->f_mode & FMODE_READ) {
2572 iter = __tracing_open(inode, file);
2574 ret = PTR_ERR(iter);
2575 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2576 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2582 t_next(struct seq_file *m, void *v, loff_t *pos)
2584 struct tracer *t = v;
2594 static void *t_start(struct seq_file *m, loff_t *pos)
2599 mutex_lock(&trace_types_lock);
2600 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2606 static void t_stop(struct seq_file *m, void *p)
2608 mutex_unlock(&trace_types_lock);
2611 static int t_show(struct seq_file *m, void *v)
2613 struct tracer *t = v;
2618 seq_printf(m, "%s", t->name);
2627 static const struct seq_operations show_traces_seq_ops = {
2634 static int show_traces_open(struct inode *inode, struct file *file)
2636 if (tracing_disabled)
2639 return seq_open(file, &show_traces_seq_ops);
2643 tracing_write_stub(struct file *filp, const char __user *ubuf,
2644 size_t count, loff_t *ppos)
2649 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2651 if (file->f_mode & FMODE_READ)
2652 return seq_lseek(file, offset, origin);
2657 static const struct file_operations tracing_fops = {
2658 .open = tracing_open,
2660 .write = tracing_write_stub,
2661 .llseek = tracing_seek,
2662 .release = tracing_release,
2665 static const struct file_operations show_traces_fops = {
2666 .open = show_traces_open,
2668 .release = seq_release,
2669 .llseek = seq_lseek,
2673 * Only trace on a CPU if the bitmask is set:
2675 static cpumask_var_t tracing_cpumask;
2678 * The tracer itself will not take this lock, but still we want
2679 * to provide a consistent cpumask to user-space:
2681 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2684 * Temporary storage for the character representation of the
2685 * CPU bitmask (and one more byte for the newline):
2687 static char mask_str[NR_CPUS + 1];
2690 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2691 size_t count, loff_t *ppos)
2695 mutex_lock(&tracing_cpumask_update_lock);
2697 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2698 if (count - len < 2) {
2702 len += sprintf(mask_str + len, "\n");
2703 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2706 mutex_unlock(&tracing_cpumask_update_lock);
2712 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2713 size_t count, loff_t *ppos)
2716 cpumask_var_t tracing_cpumask_new;
2718 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2721 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2725 mutex_lock(&tracing_cpumask_update_lock);
2727 local_irq_disable();
2728 arch_spin_lock(&ftrace_max_lock);
2729 for_each_tracing_cpu(cpu) {
2731 * Increase/decrease the disabled counter if we are
2732 * about to flip a bit in the cpumask:
2734 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2735 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2736 atomic_inc(&global_trace.data[cpu]->disabled);
2737 ring_buffer_record_disable_cpu(global_trace.buffer, cpu);
2739 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2740 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2741 atomic_dec(&global_trace.data[cpu]->disabled);
2742 ring_buffer_record_enable_cpu(global_trace.buffer, cpu);
2745 arch_spin_unlock(&ftrace_max_lock);
2748 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2750 mutex_unlock(&tracing_cpumask_update_lock);
2751 free_cpumask_var(tracing_cpumask_new);
2756 free_cpumask_var(tracing_cpumask_new);
2761 static const struct file_operations tracing_cpumask_fops = {
2762 .open = tracing_open_generic,
2763 .read = tracing_cpumask_read,
2764 .write = tracing_cpumask_write,
2765 .llseek = generic_file_llseek,
2768 static int tracing_trace_options_show(struct seq_file *m, void *v)
2770 struct tracer_opt *trace_opts;
2774 mutex_lock(&trace_types_lock);
2775 tracer_flags = current_trace->flags->val;
2776 trace_opts = current_trace->flags->opts;
2778 for (i = 0; trace_options[i]; i++) {
2779 if (trace_flags & (1 << i))
2780 seq_printf(m, "%s\n", trace_options[i]);
2782 seq_printf(m, "no%s\n", trace_options[i]);
2785 for (i = 0; trace_opts[i].name; i++) {
2786 if (tracer_flags & trace_opts[i].bit)
2787 seq_printf(m, "%s\n", trace_opts[i].name);
2789 seq_printf(m, "no%s\n", trace_opts[i].name);
2791 mutex_unlock(&trace_types_lock);
2796 static int __set_tracer_option(struct tracer *trace,
2797 struct tracer_flags *tracer_flags,
2798 struct tracer_opt *opts, int neg)
2802 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2807 tracer_flags->val &= ~opts->bit;
2809 tracer_flags->val |= opts->bit;
2813 /* Try to assign a tracer specific option */
2814 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2816 struct tracer_flags *tracer_flags = trace->flags;
2817 struct tracer_opt *opts = NULL;
2820 for (i = 0; tracer_flags->opts[i].name; i++) {
2821 opts = &tracer_flags->opts[i];
2823 if (strcmp(cmp, opts->name) == 0)
2824 return __set_tracer_option(trace, trace->flags,
2831 static void set_tracer_flags(unsigned int mask, int enabled)
2833 /* do nothing if flag is already set */
2834 if (!!(trace_flags & mask) == !!enabled)
2838 trace_flags |= mask;
2840 trace_flags &= ~mask;
2842 if (mask == TRACE_ITER_RECORD_CMD)
2843 trace_event_enable_cmd_record(enabled);
2845 if (mask == TRACE_ITER_OVERWRITE)
2846 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2848 if (mask == TRACE_ITER_PRINTK)
2849 trace_printk_start_stop_comm(enabled);
2853 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2854 size_t cnt, loff_t *ppos)
2862 if (cnt >= sizeof(buf))
2865 if (copy_from_user(&buf, ubuf, cnt))
2869 cmp = strstrip(buf);
2871 if (strncmp(cmp, "no", 2) == 0) {
2876 for (i = 0; trace_options[i]; i++) {
2877 if (strcmp(cmp, trace_options[i]) == 0) {
2878 set_tracer_flags(1 << i, !neg);
2883 /* If no option could be set, test the specific tracer options */
2884 if (!trace_options[i]) {
2885 mutex_lock(&trace_types_lock);
2886 ret = set_tracer_option(current_trace, cmp, neg);
2887 mutex_unlock(&trace_types_lock);
2897 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2899 if (tracing_disabled)
2901 return single_open(file, tracing_trace_options_show, NULL);
2904 static const struct file_operations tracing_iter_fops = {
2905 .open = tracing_trace_options_open,
2907 .llseek = seq_lseek,
2908 .release = single_release,
2909 .write = tracing_trace_options_write,
2912 static const char readme_msg[] =
2913 "tracing mini-HOWTO:\n\n"
2914 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2915 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2916 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
2917 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2919 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
2920 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2922 "# cat /sys/kernel/debug/tracing/trace_options\n"
2923 "noprint-parent nosym-offset nosym-addr noverbose\n"
2924 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2925 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
2926 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2927 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
2931 tracing_readme_read(struct file *filp, char __user *ubuf,
2932 size_t cnt, loff_t *ppos)
2934 return simple_read_from_buffer(ubuf, cnt, ppos,
2935 readme_msg, strlen(readme_msg));
2938 static const struct file_operations tracing_readme_fops = {
2939 .open = tracing_open_generic,
2940 .read = tracing_readme_read,
2941 .llseek = generic_file_llseek,
2945 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2946 size_t cnt, loff_t *ppos)
2955 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2959 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2967 for (i = 0; i < SAVED_CMDLINES; i++) {
2970 pid = map_cmdline_to_pid[i];
2971 if (pid == -1 || pid == NO_CMDLINE_MAP)
2974 trace_find_cmdline(pid, buf_comm);
2975 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2980 len = simple_read_from_buffer(ubuf, cnt, ppos,
2989 static const struct file_operations tracing_saved_cmdlines_fops = {
2990 .open = tracing_open_generic,
2991 .read = tracing_saved_cmdlines_read,
2992 .llseek = generic_file_llseek,
2996 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2997 size_t cnt, loff_t *ppos)
3002 r = sprintf(buf, "%u\n", tracer_enabled);
3003 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3007 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
3008 size_t cnt, loff_t *ppos)
3010 struct trace_array *tr = filp->private_data;
3014 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3020 mutex_lock(&trace_types_lock);
3021 if (tracer_enabled ^ val) {
3023 /* Only need to warn if this is used to change the state */
3024 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
3028 if (current_trace->start)
3029 current_trace->start(tr);
3034 if (current_trace->stop)
3035 current_trace->stop(tr);
3038 mutex_unlock(&trace_types_lock);
3046 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3047 size_t cnt, loff_t *ppos)
3049 char buf[MAX_TRACER_SIZE+2];
3052 mutex_lock(&trace_types_lock);
3054 r = sprintf(buf, "%s\n", current_trace->name);
3056 r = sprintf(buf, "\n");
3057 mutex_unlock(&trace_types_lock);
3059 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3062 int tracer_init(struct tracer *t, struct trace_array *tr)
3064 tracing_reset_online_cpus(tr);
3068 static void set_buffer_entries(struct trace_array *tr, unsigned long val)
3071 for_each_tracing_cpu(cpu)
3072 tr->data[cpu]->entries = val;
3075 static int __tracing_resize_ring_buffer(unsigned long size, int cpu)
3080 * If kernel or user changes the size of the ring buffer
3081 * we use the size that was given, and we can forget about
3082 * expanding it later.
3084 ring_buffer_expanded = 1;
3086 /* May be called before buffers are initialized */
3087 if (!global_trace.buffer)
3090 ret = ring_buffer_resize(global_trace.buffer, size, cpu);
3094 if (!current_trace->use_max_tr)
3097 ret = ring_buffer_resize(max_tr.buffer, size, cpu);
3101 if (cpu == RING_BUFFER_ALL_CPUS) {
3103 for_each_tracing_cpu(i) {
3104 r = ring_buffer_resize(global_trace.buffer,
3105 global_trace.data[i]->entries,
3111 r = ring_buffer_resize(global_trace.buffer,
3112 global_trace.data[cpu]->entries,
3118 * AARGH! We are left with different
3119 * size max buffer!!!!
3120 * The max buffer is our "snapshot" buffer.
3121 * When a tracer needs a snapshot (one of the
3122 * latency tracers), it swaps the max buffer
3123 * with the saved snap shot. We succeeded to
3124 * update the size of the main buffer, but failed to
3125 * update the size of the max buffer. But when we tried
3126 * to reset the main buffer to the original size, we
3127 * failed there too. This is very unlikely to
3128 * happen, but if it does, warn and kill all
3132 tracing_disabled = 1;
3137 if (cpu == RING_BUFFER_ALL_CPUS)
3138 set_buffer_entries(&max_tr, size);
3140 max_tr.data[cpu]->entries = size;
3143 if (cpu == RING_BUFFER_ALL_CPUS)
3144 set_buffer_entries(&global_trace, size);
3146 global_trace.data[cpu]->entries = size;
3151 static ssize_t tracing_resize_ring_buffer(unsigned long size, int cpu_id)
3155 mutex_lock(&trace_types_lock);
3157 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3158 /* make sure, this cpu is enabled in the mask */
3159 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3165 ret = __tracing_resize_ring_buffer(size, cpu_id);
3170 mutex_unlock(&trace_types_lock);
3177 * tracing_update_buffers - used by tracing facility to expand ring buffers
3179 * To save on memory when the tracing is never used on a system with it
3180 * configured in. The ring buffers are set to a minimum size. But once
3181 * a user starts to use the tracing facility, then they need to grow
3182 * to their default size.
3184 * This function is to be called when a tracer is about to be used.
3186 int tracing_update_buffers(void)
3190 mutex_lock(&trace_types_lock);
3191 if (!ring_buffer_expanded)
3192 ret = __tracing_resize_ring_buffer(trace_buf_size,
3193 RING_BUFFER_ALL_CPUS);
3194 mutex_unlock(&trace_types_lock);
3199 struct trace_option_dentry;
3201 static struct trace_option_dentry *
3202 create_trace_option_files(struct tracer *tracer);
3205 destroy_trace_option_files(struct trace_option_dentry *topts);
3207 static int tracing_set_tracer(const char *buf)
3209 static struct trace_option_dentry *topts;
3210 struct trace_array *tr = &global_trace;
3214 mutex_lock(&trace_types_lock);
3216 if (!ring_buffer_expanded) {
3217 ret = __tracing_resize_ring_buffer(trace_buf_size,
3218 RING_BUFFER_ALL_CPUS);
3224 for (t = trace_types; t; t = t->next) {
3225 if (strcmp(t->name, buf) == 0)
3232 if (t == current_trace)
3235 trace_branch_disable();
3236 if (current_trace && current_trace->reset)
3237 current_trace->reset(tr);
3238 if (current_trace && current_trace->use_max_tr) {
3240 * We don't free the ring buffer. instead, resize it because
3241 * The max_tr ring buffer has some state (e.g. ring->clock) and
3242 * we want preserve it.
3244 ring_buffer_resize(max_tr.buffer, 1, RING_BUFFER_ALL_CPUS);
3245 set_buffer_entries(&max_tr, 1);
3247 destroy_trace_option_files(topts);
3249 current_trace = &nop_trace;
3251 topts = create_trace_option_files(t);
3252 if (t->use_max_tr) {
3254 /* we need to make per cpu buffer sizes equivalent */
3255 for_each_tracing_cpu(cpu) {
3256 ret = ring_buffer_resize(max_tr.buffer,
3257 global_trace.data[cpu]->entries,
3261 max_tr.data[cpu]->entries =
3262 global_trace.data[cpu]->entries;
3267 ret = tracer_init(t, tr);
3273 trace_branch_enable(tr);
3275 mutex_unlock(&trace_types_lock);
3281 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3282 size_t cnt, loff_t *ppos)
3284 char buf[MAX_TRACER_SIZE+1];
3291 if (cnt > MAX_TRACER_SIZE)
3292 cnt = MAX_TRACER_SIZE;
3294 if (copy_from_user(&buf, ubuf, cnt))
3299 /* strip ending whitespace. */
3300 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3303 err = tracing_set_tracer(buf);
3313 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3314 size_t cnt, loff_t *ppos)
3316 unsigned long *ptr = filp->private_data;
3320 r = snprintf(buf, sizeof(buf), "%ld\n",
3321 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3322 if (r > sizeof(buf))
3324 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3328 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3329 size_t cnt, loff_t *ppos)
3331 unsigned long *ptr = filp->private_data;
3335 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3344 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3346 long cpu_file = (long) inode->i_private;
3347 struct trace_iterator *iter;
3350 if (tracing_disabled)
3353 mutex_lock(&trace_types_lock);
3355 /* create a buffer to store the information to pass to userspace */
3356 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3363 * We make a copy of the current tracer to avoid concurrent
3364 * changes on it while we are reading.
3366 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3372 *iter->trace = *current_trace;
3374 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3379 /* trace pipe does not show start of buffer */
3380 cpumask_setall(iter->started);
3382 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3383 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3385 iter->cpu_file = cpu_file;
3386 iter->tr = &global_trace;
3387 mutex_init(&iter->mutex);
3388 filp->private_data = iter;
3390 if (iter->trace->pipe_open)
3391 iter->trace->pipe_open(iter);
3393 nonseekable_open(inode, filp);
3395 mutex_unlock(&trace_types_lock);
3401 mutex_unlock(&trace_types_lock);
3405 static int tracing_release_pipe(struct inode *inode, struct file *file)
3407 struct trace_iterator *iter = file->private_data;
3409 mutex_lock(&trace_types_lock);
3411 if (iter->trace->pipe_close)
3412 iter->trace->pipe_close(iter);
3414 mutex_unlock(&trace_types_lock);
3416 free_cpumask_var(iter->started);
3417 mutex_destroy(&iter->mutex);
3425 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3427 struct trace_iterator *iter = filp->private_data;
3429 if (trace_flags & TRACE_ITER_BLOCK) {
3431 * Always select as readable when in blocking mode
3433 return POLLIN | POLLRDNORM;
3435 if (!trace_empty(iter))
3436 return POLLIN | POLLRDNORM;
3437 poll_wait(filp, &trace_wait, poll_table);
3438 if (!trace_empty(iter))
3439 return POLLIN | POLLRDNORM;
3446 void default_wait_pipe(struct trace_iterator *iter)
3450 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3452 if (trace_empty(iter))
3455 finish_wait(&trace_wait, &wait);
3459 * This is a make-shift waitqueue.
3460 * A tracer might use this callback on some rare cases:
3462 * 1) the current tracer might hold the runqueue lock when it wakes up
3463 * a reader, hence a deadlock (sched, function, and function graph tracers)
3464 * 2) the function tracers, trace all functions, we don't want
3465 * the overhead of calling wake_up and friends
3466 * (and tracing them too)
3468 * Anyway, this is really very primitive wakeup.
3470 void poll_wait_pipe(struct trace_iterator *iter)
3472 set_current_state(TASK_INTERRUPTIBLE);
3473 /* sleep for 100 msecs, and try again. */
3474 schedule_timeout(HZ / 10);
3477 /* Must be called with trace_types_lock mutex held. */
3478 static int tracing_wait_pipe(struct file *filp)
3480 struct trace_iterator *iter = filp->private_data;
3482 while (trace_empty(iter)) {
3484 if ((filp->f_flags & O_NONBLOCK)) {
3488 mutex_unlock(&iter->mutex);
3490 iter->trace->wait_pipe(iter);
3492 mutex_lock(&iter->mutex);
3494 if (signal_pending(current))
3498 * We block until we read something and tracing is disabled.
3499 * We still block if tracing is disabled, but we have never
3500 * read anything. This allows a user to cat this file, and
3501 * then enable tracing. But after we have read something,
3502 * we give an EOF when tracing is again disabled.
3504 * iter->pos will be 0 if we haven't read anything.
3506 if (!tracer_enabled && iter->pos)
3517 tracing_read_pipe(struct file *filp, char __user *ubuf,
3518 size_t cnt, loff_t *ppos)
3520 struct trace_iterator *iter = filp->private_data;
3521 static struct tracer *old_tracer;
3524 /* return any leftover data */
3525 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3529 trace_seq_init(&iter->seq);
3531 /* copy the tracer to avoid using a global lock all around */
3532 mutex_lock(&trace_types_lock);
3533 if (unlikely(old_tracer != current_trace && current_trace)) {
3534 old_tracer = current_trace;
3535 *iter->trace = *current_trace;
3537 mutex_unlock(&trace_types_lock);
3540 * Avoid more than one consumer on a single file descriptor
3541 * This is just a matter of traces coherency, the ring buffer itself
3544 mutex_lock(&iter->mutex);
3545 if (iter->trace->read) {
3546 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3552 sret = tracing_wait_pipe(filp);
3556 /* stop when tracing is finished */
3557 if (trace_empty(iter)) {
3562 if (cnt >= PAGE_SIZE)
3563 cnt = PAGE_SIZE - 1;
3565 /* reset all but tr, trace, and overruns */
3566 memset(&iter->seq, 0,
3567 sizeof(struct trace_iterator) -
3568 offsetof(struct trace_iterator, seq));
3571 trace_event_read_lock();
3572 trace_access_lock(iter->cpu_file);
3573 while (trace_find_next_entry_inc(iter) != NULL) {
3574 enum print_line_t ret;
3575 int len = iter->seq.len;
3577 ret = print_trace_line(iter);
3578 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3579 /* don't print partial lines */
3580 iter->seq.len = len;
3583 if (ret != TRACE_TYPE_NO_CONSUME)
3584 trace_consume(iter);
3586 if (iter->seq.len >= cnt)
3590 * Setting the full flag means we reached the trace_seq buffer
3591 * size and we should leave by partial output condition above.
3592 * One of the trace_seq_* functions is not used properly.
3594 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3597 trace_access_unlock(iter->cpu_file);
3598 trace_event_read_unlock();
3600 /* Now copy what we have to the user */
3601 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3602 if (iter->seq.readpos >= iter->seq.len)
3603 trace_seq_init(&iter->seq);
3606 * If there was nothing to send to user, in spite of consuming trace
3607 * entries, go back to wait for more entries.
3613 mutex_unlock(&iter->mutex);
3618 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3619 struct pipe_buffer *buf)
3621 __free_page(buf->page);
3624 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3627 __free_page(spd->pages[idx]);
3630 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3632 .map = generic_pipe_buf_map,
3633 .unmap = generic_pipe_buf_unmap,
3634 .confirm = generic_pipe_buf_confirm,
3635 .release = tracing_pipe_buf_release,
3636 .steal = generic_pipe_buf_steal,
3637 .get = generic_pipe_buf_get,
3641 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3646 /* Seq buffer is page-sized, exactly what we need. */
3648 count = iter->seq.len;
3649 ret = print_trace_line(iter);
3650 count = iter->seq.len - count;
3653 iter->seq.len -= count;
3656 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3657 iter->seq.len -= count;
3661 if (ret != TRACE_TYPE_NO_CONSUME)
3662 trace_consume(iter);
3664 if (!trace_find_next_entry_inc(iter)) {
3674 static ssize_t tracing_splice_read_pipe(struct file *filp,
3676 struct pipe_inode_info *pipe,
3680 struct page *pages_def[PIPE_DEF_BUFFERS];
3681 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3682 struct trace_iterator *iter = filp->private_data;
3683 struct splice_pipe_desc spd = {
3685 .partial = partial_def,
3686 .nr_pages = 0, /* This gets updated below. */
3687 .nr_pages_max = PIPE_DEF_BUFFERS,
3689 .ops = &tracing_pipe_buf_ops,
3690 .spd_release = tracing_spd_release_pipe,
3692 static struct tracer *old_tracer;
3697 if (splice_grow_spd(pipe, &spd))
3700 /* copy the tracer to avoid using a global lock all around */
3701 mutex_lock(&trace_types_lock);
3702 if (unlikely(old_tracer != current_trace && current_trace)) {
3703 old_tracer = current_trace;
3704 *iter->trace = *current_trace;
3706 mutex_unlock(&trace_types_lock);
3708 mutex_lock(&iter->mutex);
3710 if (iter->trace->splice_read) {
3711 ret = iter->trace->splice_read(iter, filp,
3712 ppos, pipe, len, flags);
3717 ret = tracing_wait_pipe(filp);
3721 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3726 trace_event_read_lock();
3727 trace_access_lock(iter->cpu_file);
3729 /* Fill as many pages as possible. */
3730 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3731 spd.pages[i] = alloc_page(GFP_KERNEL);
3735 rem = tracing_fill_pipe_page(rem, iter);
3737 /* Copy the data into the page, so we can start over. */
3738 ret = trace_seq_to_buffer(&iter->seq,
3739 page_address(spd.pages[i]),
3742 __free_page(spd.pages[i]);
3745 spd.partial[i].offset = 0;
3746 spd.partial[i].len = iter->seq.len;
3748 trace_seq_init(&iter->seq);
3751 trace_access_unlock(iter->cpu_file);
3752 trace_event_read_unlock();
3753 mutex_unlock(&iter->mutex);
3757 ret = splice_to_pipe(pipe, &spd);
3759 splice_shrink_spd(&spd);
3763 mutex_unlock(&iter->mutex);
3767 struct ftrace_entries_info {
3768 struct trace_array *tr;
3772 static int tracing_entries_open(struct inode *inode, struct file *filp)
3774 struct ftrace_entries_info *info;
3776 if (tracing_disabled)
3779 info = kzalloc(sizeof(*info), GFP_KERNEL);
3783 info->tr = &global_trace;
3784 info->cpu = (unsigned long)inode->i_private;
3786 filp->private_data = info;
3792 tracing_entries_read(struct file *filp, char __user *ubuf,
3793 size_t cnt, loff_t *ppos)
3795 struct ftrace_entries_info *info = filp->private_data;
3796 struct trace_array *tr = info->tr;
3801 mutex_lock(&trace_types_lock);
3803 if (info->cpu == RING_BUFFER_ALL_CPUS) {
3804 int cpu, buf_size_same;
3809 /* check if all cpu sizes are same */
3810 for_each_tracing_cpu(cpu) {
3811 /* fill in the size from first enabled cpu */
3813 size = tr->data[cpu]->entries;
3814 if (size != tr->data[cpu]->entries) {
3820 if (buf_size_same) {
3821 if (!ring_buffer_expanded)
3822 r = sprintf(buf, "%lu (expanded: %lu)\n",
3824 trace_buf_size >> 10);
3826 r = sprintf(buf, "%lu\n", size >> 10);
3828 r = sprintf(buf, "X\n");
3830 r = sprintf(buf, "%lu\n", tr->data[info->cpu]->entries >> 10);
3832 mutex_unlock(&trace_types_lock);
3834 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3839 tracing_entries_write(struct file *filp, const char __user *ubuf,
3840 size_t cnt, loff_t *ppos)
3842 struct ftrace_entries_info *info = filp->private_data;
3846 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3850 /* must have at least 1 entry */
3854 /* value is in KB */
3857 ret = tracing_resize_ring_buffer(val, info->cpu);
3867 tracing_entries_release(struct inode *inode, struct file *filp)
3869 struct ftrace_entries_info *info = filp->private_data;
3877 tracing_total_entries_read(struct file *filp, char __user *ubuf,
3878 size_t cnt, loff_t *ppos)
3880 struct trace_array *tr = filp->private_data;
3883 unsigned long size = 0, expanded_size = 0;
3885 mutex_lock(&trace_types_lock);
3886 for_each_tracing_cpu(cpu) {
3887 size += tr->data[cpu]->entries >> 10;
3888 if (!ring_buffer_expanded)
3889 expanded_size += trace_buf_size >> 10;
3891 if (ring_buffer_expanded)
3892 r = sprintf(buf, "%lu\n", size);
3894 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3895 mutex_unlock(&trace_types_lock);
3897 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3901 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3902 size_t cnt, loff_t *ppos)
3905 * There is no need to read what the user has written, this function
3906 * is just to make sure that there is no error when "echo" is used
3915 tracing_free_buffer_release(struct inode *inode, struct file *filp)
3917 /* disable tracing ? */
3918 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3920 /* resize the ring buffer to 0 */
3921 tracing_resize_ring_buffer(0, RING_BUFFER_ALL_CPUS);
3927 tracing_mark_write(struct file *filp, const char __user *ubuf,
3928 size_t cnt, loff_t *fpos)
3930 unsigned long addr = (unsigned long)ubuf;
3931 struct ring_buffer_event *event;
3932 struct ring_buffer *buffer;
3933 struct print_entry *entry;
3934 unsigned long irq_flags;
3935 struct page *pages[2];
3945 if (tracing_disabled)
3948 if (!(trace_flags & TRACE_ITER_MARKERS))
3951 if (cnt > TRACE_BUF_SIZE)
3952 cnt = TRACE_BUF_SIZE;
3955 * Userspace is injecting traces into the kernel trace buffer.
3956 * We want to be as non intrusive as possible.
3957 * To do so, we do not want to allocate any special buffers
3958 * or take any locks, but instead write the userspace data
3959 * straight into the ring buffer.
3961 * First we need to pin the userspace buffer into memory,
3962 * which, most likely it is, because it just referenced it.
3963 * But there's no guarantee that it is. By using get_user_pages_fast()
3964 * and kmap_atomic/kunmap_atomic() we can get access to the
3965 * pages directly. We then write the data directly into the
3968 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
3970 /* check if we cross pages */
3971 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
3974 offset = addr & (PAGE_SIZE - 1);
3977 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
3978 if (ret < nr_pages) {
3980 put_page(pages[ret]);
3985 for (i = 0; i < nr_pages; i++)
3986 map_page[i] = kmap_atomic(pages[i]);
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, map_page[0] + offset, len);
4005 memcpy(&entry->buf[len], map_page[1], cnt - len);
4007 memcpy(&entry->buf, map_page[0] + 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 __buffer_unlock_commit(buffer, event);
4022 for (i = 0; i < nr_pages; i++){
4023 kunmap_atomic(map_page[i]);
4030 static int tracing_clock_show(struct seq_file *m, void *v)
4034 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4036 "%s%s%s%s", i ? " " : "",
4037 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
4038 i == trace_clock_id ? "]" : "");
4044 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4045 size_t cnt, loff_t *fpos)
4048 const char *clockstr;
4051 if (cnt >= sizeof(buf))
4054 if (copy_from_user(&buf, ubuf, cnt))
4059 clockstr = strstrip(buf);
4061 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4062 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4065 if (i == ARRAY_SIZE(trace_clocks))
4070 mutex_lock(&trace_types_lock);
4072 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
4074 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
4076 mutex_unlock(&trace_types_lock);
4083 static int tracing_clock_open(struct inode *inode, struct file *file)
4085 if (tracing_disabled)
4087 return single_open(file, tracing_clock_show, NULL);
4090 static const struct file_operations tracing_max_lat_fops = {
4091 .open = tracing_open_generic,
4092 .read = tracing_max_lat_read,
4093 .write = tracing_max_lat_write,
4094 .llseek = generic_file_llseek,
4097 static const struct file_operations tracing_ctrl_fops = {
4098 .open = tracing_open_generic,
4099 .read = tracing_ctrl_read,
4100 .write = tracing_ctrl_write,
4101 .llseek = generic_file_llseek,
4104 static const struct file_operations set_tracer_fops = {
4105 .open = tracing_open_generic,
4106 .read = tracing_set_trace_read,
4107 .write = tracing_set_trace_write,
4108 .llseek = generic_file_llseek,
4111 static const struct file_operations tracing_pipe_fops = {
4112 .open = tracing_open_pipe,
4113 .poll = tracing_poll_pipe,
4114 .read = tracing_read_pipe,
4115 .splice_read = tracing_splice_read_pipe,
4116 .release = tracing_release_pipe,
4117 .llseek = no_llseek,
4120 static const struct file_operations tracing_entries_fops = {
4121 .open = tracing_entries_open,
4122 .read = tracing_entries_read,
4123 .write = tracing_entries_write,
4124 .release = tracing_entries_release,
4125 .llseek = generic_file_llseek,
4128 static const struct file_operations tracing_total_entries_fops = {
4129 .open = tracing_open_generic,
4130 .read = tracing_total_entries_read,
4131 .llseek = generic_file_llseek,
4134 static const struct file_operations tracing_free_buffer_fops = {
4135 .write = tracing_free_buffer_write,
4136 .release = tracing_free_buffer_release,
4139 static const struct file_operations tracing_mark_fops = {
4140 .open = tracing_open_generic,
4141 .write = tracing_mark_write,
4142 .llseek = generic_file_llseek,
4145 static const struct file_operations trace_clock_fops = {
4146 .open = tracing_clock_open,
4148 .llseek = seq_lseek,
4149 .release = single_release,
4150 .write = tracing_clock_write,
4153 struct ftrace_buffer_info {
4154 struct trace_array *tr;
4160 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4162 int cpu = (int)(long)inode->i_private;
4163 struct ftrace_buffer_info *info;
4165 if (tracing_disabled)
4168 info = kzalloc(sizeof(*info), GFP_KERNEL);
4172 info->tr = &global_trace;
4175 /* Force reading ring buffer for first read */
4176 info->read = (unsigned int)-1;
4178 filp->private_data = info;
4180 return nonseekable_open(inode, filp);
4184 tracing_buffers_read(struct file *filp, char __user *ubuf,
4185 size_t count, loff_t *ppos)
4187 struct ftrace_buffer_info *info = filp->private_data;
4195 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
4199 /* Do we have previous read data to read? */
4200 if (info->read < PAGE_SIZE)
4203 trace_access_lock(info->cpu);
4204 ret = ring_buffer_read_page(info->tr->buffer,
4208 trace_access_unlock(info->cpu);
4215 size = PAGE_SIZE - info->read;
4219 ret = copy_to_user(ubuf, info->spare + info->read, size);
4230 static int tracing_buffers_release(struct inode *inode, struct file *file)
4232 struct ftrace_buffer_info *info = file->private_data;
4235 ring_buffer_free_read_page(info->tr->buffer, info->spare);
4242 struct ring_buffer *buffer;
4247 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4248 struct pipe_buffer *buf)
4250 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4255 ring_buffer_free_read_page(ref->buffer, ref->page);
4260 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4261 struct pipe_buffer *buf)
4263 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4268 /* Pipe buffer operations for a buffer. */
4269 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4271 .map = generic_pipe_buf_map,
4272 .unmap = generic_pipe_buf_unmap,
4273 .confirm = generic_pipe_buf_confirm,
4274 .release = buffer_pipe_buf_release,
4275 .steal = generic_pipe_buf_steal,
4276 .get = buffer_pipe_buf_get,
4280 * Callback from splice_to_pipe(), if we need to release some pages
4281 * at the end of the spd in case we error'ed out in filling the pipe.
4283 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4285 struct buffer_ref *ref =
4286 (struct buffer_ref *)spd->partial[i].private;
4291 ring_buffer_free_read_page(ref->buffer, ref->page);
4293 spd->partial[i].private = 0;
4297 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4298 struct pipe_inode_info *pipe, size_t len,
4301 struct ftrace_buffer_info *info = file->private_data;
4302 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4303 struct page *pages_def[PIPE_DEF_BUFFERS];
4304 struct splice_pipe_desc spd = {
4306 .partial = partial_def,
4307 .nr_pages_max = PIPE_DEF_BUFFERS,
4309 .ops = &buffer_pipe_buf_ops,
4310 .spd_release = buffer_spd_release,
4312 struct buffer_ref *ref;
4313 int entries, size, i;
4316 if (splice_grow_spd(pipe, &spd))
4319 if (*ppos & (PAGE_SIZE - 1)) {
4320 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
4325 if (len & (PAGE_SIZE - 1)) {
4326 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
4327 if (len < PAGE_SIZE) {
4334 trace_access_lock(info->cpu);
4335 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4337 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4341 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4346 ref->buffer = info->tr->buffer;
4347 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
4353 r = ring_buffer_read_page(ref->buffer, &ref->page,
4356 ring_buffer_free_read_page(ref->buffer, ref->page);
4362 * zero out any left over data, this is going to
4365 size = ring_buffer_page_len(ref->page);
4366 if (size < PAGE_SIZE)
4367 memset(ref->page + size, 0, PAGE_SIZE - size);
4369 page = virt_to_page(ref->page);
4371 spd.pages[i] = page;
4372 spd.partial[i].len = PAGE_SIZE;
4373 spd.partial[i].offset = 0;
4374 spd.partial[i].private = (unsigned long)ref;
4378 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4381 trace_access_unlock(info->cpu);
4384 /* did we read anything? */
4385 if (!spd.nr_pages) {
4386 if (flags & SPLICE_F_NONBLOCK)
4394 ret = splice_to_pipe(pipe, &spd);
4395 splice_shrink_spd(&spd);
4400 static const struct file_operations tracing_buffers_fops = {
4401 .open = tracing_buffers_open,
4402 .read = tracing_buffers_read,
4403 .release = tracing_buffers_release,
4404 .splice_read = tracing_buffers_splice_read,
4405 .llseek = no_llseek,
4409 tracing_stats_read(struct file *filp, char __user *ubuf,
4410 size_t count, loff_t *ppos)
4412 unsigned long cpu = (unsigned long)filp->private_data;
4413 struct trace_array *tr = &global_trace;
4414 struct trace_seq *s;
4416 unsigned long long t;
4417 unsigned long usec_rem;
4419 s = kmalloc(sizeof(*s), GFP_KERNEL);
4425 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4426 trace_seq_printf(s, "entries: %ld\n", cnt);
4428 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4429 trace_seq_printf(s, "overrun: %ld\n", cnt);
4431 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4432 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4434 cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4435 trace_seq_printf(s, "bytes: %ld\n", cnt);
4437 t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4438 usec_rem = do_div(t, USEC_PER_SEC);
4439 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", t, usec_rem);
4441 t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4442 usec_rem = do_div(t, USEC_PER_SEC);
4443 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4445 cnt = ring_buffer_dropped_events_cpu(tr->buffer, cpu);
4446 trace_seq_printf(s, "dropped events: %ld\n", cnt);
4448 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4455 static const struct file_operations tracing_stats_fops = {
4456 .open = tracing_open_generic,
4457 .read = tracing_stats_read,
4458 .llseek = generic_file_llseek,
4461 #ifdef CONFIG_DYNAMIC_FTRACE
4463 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4469 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
4470 size_t cnt, loff_t *ppos)
4472 static char ftrace_dyn_info_buffer[1024];
4473 static DEFINE_MUTEX(dyn_info_mutex);
4474 unsigned long *p = filp->private_data;
4475 char *buf = ftrace_dyn_info_buffer;
4476 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
4479 mutex_lock(&dyn_info_mutex);
4480 r = sprintf(buf, "%ld ", *p);
4482 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
4485 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4487 mutex_unlock(&dyn_info_mutex);
4492 static const struct file_operations tracing_dyn_info_fops = {
4493 .open = tracing_open_generic,
4494 .read = tracing_read_dyn_info,
4495 .llseek = generic_file_llseek,
4499 static struct dentry *d_tracer;
4501 struct dentry *tracing_init_dentry(void)
4508 if (!debugfs_initialized())
4511 d_tracer = debugfs_create_dir("tracing", NULL);
4513 if (!d_tracer && !once) {
4515 pr_warning("Could not create debugfs directory 'tracing'\n");
4522 static struct dentry *d_percpu;
4524 struct dentry *tracing_dentry_percpu(void)
4527 struct dentry *d_tracer;
4532 d_tracer = tracing_init_dentry();
4537 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4539 if (!d_percpu && !once) {
4541 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4548 static void tracing_init_debugfs_percpu(long cpu)
4550 struct dentry *d_percpu = tracing_dentry_percpu();
4551 struct dentry *d_cpu;
4552 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 trace_array *tr = filp->private_data;
4823 struct ring_buffer *buffer = tr->buffer;
4828 r = ring_buffer_record_is_on(buffer);
4832 r = sprintf(buf, "%d\n", r);
4834 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4838 rb_simple_write(struct file *filp, const char __user *ubuf,
4839 size_t cnt, loff_t *ppos)
4841 struct trace_array *tr = filp->private_data;
4842 struct ring_buffer *buffer = tr->buffer;
4846 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4852 ring_buffer_record_on(buffer);
4854 ring_buffer_record_off(buffer);
4862 static const struct file_operations rb_simple_fops = {
4863 .open = tracing_open_generic,
4864 .read = rb_simple_read,
4865 .write = rb_simple_write,
4866 .llseek = default_llseek,
4869 static __init int tracer_init_debugfs(void)
4871 struct dentry *d_tracer;
4874 trace_access_lock_init();
4876 d_tracer = tracing_init_dentry();
4878 trace_create_file("tracing_enabled", 0644, d_tracer,
4879 &global_trace, &tracing_ctrl_fops);
4881 trace_create_file("trace_options", 0644, d_tracer,
4882 NULL, &tracing_iter_fops);
4884 trace_create_file("tracing_cpumask", 0644, d_tracer,
4885 NULL, &tracing_cpumask_fops);
4887 trace_create_file("trace", 0644, d_tracer,
4888 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4890 trace_create_file("available_tracers", 0444, d_tracer,
4891 &global_trace, &show_traces_fops);
4893 trace_create_file("current_tracer", 0644, d_tracer,
4894 &global_trace, &set_tracer_fops);
4896 #ifdef CONFIG_TRACER_MAX_TRACE
4897 trace_create_file("tracing_max_latency", 0644, d_tracer,
4898 &tracing_max_latency, &tracing_max_lat_fops);
4901 trace_create_file("tracing_thresh", 0644, d_tracer,
4902 &tracing_thresh, &tracing_max_lat_fops);
4904 trace_create_file("README", 0444, d_tracer,
4905 NULL, &tracing_readme_fops);
4907 trace_create_file("trace_pipe", 0444, d_tracer,
4908 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4910 trace_create_file("buffer_size_kb", 0644, d_tracer,
4911 (void *) RING_BUFFER_ALL_CPUS, &tracing_entries_fops);
4913 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
4914 &global_trace, &tracing_total_entries_fops);
4916 trace_create_file("free_buffer", 0644, d_tracer,
4917 &global_trace, &tracing_free_buffer_fops);
4919 trace_create_file("trace_marker", 0220, d_tracer,
4920 NULL, &tracing_mark_fops);
4922 trace_create_file("saved_cmdlines", 0444, d_tracer,
4923 NULL, &tracing_saved_cmdlines_fops);
4925 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4928 trace_create_file("tracing_on", 0644, d_tracer,
4929 &global_trace, &rb_simple_fops);
4931 #ifdef CONFIG_DYNAMIC_FTRACE
4932 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4933 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4936 create_trace_options_dir();
4938 for_each_tracing_cpu(cpu)
4939 tracing_init_debugfs_percpu(cpu);
4944 static int trace_panic_handler(struct notifier_block *this,
4945 unsigned long event, void *unused)
4947 if (ftrace_dump_on_oops)
4948 ftrace_dump(ftrace_dump_on_oops);
4952 static struct notifier_block trace_panic_notifier = {
4953 .notifier_call = trace_panic_handler,
4955 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4958 static int trace_die_handler(struct notifier_block *self,
4964 if (ftrace_dump_on_oops)
4965 ftrace_dump(ftrace_dump_on_oops);
4973 static struct notifier_block trace_die_notifier = {
4974 .notifier_call = trace_die_handler,
4979 * printk is set to max of 1024, we really don't need it that big.
4980 * Nothing should be printing 1000 characters anyway.
4982 #define TRACE_MAX_PRINT 1000
4985 * Define here KERN_TRACE so that we have one place to modify
4986 * it if we decide to change what log level the ftrace dump
4989 #define KERN_TRACE KERN_EMERG
4992 trace_printk_seq(struct trace_seq *s)
4994 /* Probably should print a warning here. */
4998 /* should be zero ended, but we are paranoid. */
4999 s->buffer[s->len] = 0;
5001 printk(KERN_TRACE "%s", s->buffer);
5006 void trace_init_global_iter(struct trace_iterator *iter)
5008 iter->tr = &global_trace;
5009 iter->trace = current_trace;
5010 iter->cpu_file = TRACE_PIPE_ALL_CPU;
5014 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
5016 static arch_spinlock_t ftrace_dump_lock =
5017 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
5018 /* use static because iter can be a bit big for the stack */
5019 static struct trace_iterator iter;
5020 unsigned int old_userobj;
5021 static int dump_ran;
5022 unsigned long flags;
5026 local_irq_save(flags);
5027 arch_spin_lock(&ftrace_dump_lock);
5035 /* Did function tracer already get disabled? */
5036 if (ftrace_is_dead()) {
5037 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
5038 printk("# MAY BE MISSING FUNCTION EVENTS\n");
5041 if (disable_tracing)
5044 trace_init_global_iter(&iter);
5046 for_each_tracing_cpu(cpu) {
5047 atomic_inc(&iter.tr->data[cpu]->disabled);
5050 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
5052 /* don't look at user memory in panic mode */
5053 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
5055 /* Simulate the iterator */
5056 iter.tr = &global_trace;
5057 iter.trace = current_trace;
5059 switch (oops_dump_mode) {
5061 iter.cpu_file = TRACE_PIPE_ALL_CPU;
5064 iter.cpu_file = raw_smp_processor_id();
5069 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
5070 iter.cpu_file = TRACE_PIPE_ALL_CPU;
5073 printk(KERN_TRACE "Dumping ftrace buffer:\n");
5076 * We need to stop all tracing on all CPUS to read the
5077 * the next buffer. This is a bit expensive, but is
5078 * not done often. We fill all what we can read,
5079 * and then release the locks again.
5082 while (!trace_empty(&iter)) {
5085 printk(KERN_TRACE "---------------------------------\n");
5089 /* reset all but tr, trace, and overruns */
5090 memset(&iter.seq, 0,
5091 sizeof(struct trace_iterator) -
5092 offsetof(struct trace_iterator, seq));
5093 iter.iter_flags |= TRACE_FILE_LAT_FMT;
5096 if (trace_find_next_entry_inc(&iter) != NULL) {
5099 ret = print_trace_line(&iter);
5100 if (ret != TRACE_TYPE_NO_CONSUME)
5101 trace_consume(&iter);
5103 touch_nmi_watchdog();
5105 trace_printk_seq(&iter.seq);
5109 printk(KERN_TRACE " (ftrace buffer empty)\n");
5111 printk(KERN_TRACE "---------------------------------\n");
5114 /* Re-enable tracing if requested */
5115 if (!disable_tracing) {
5116 trace_flags |= old_userobj;
5118 for_each_tracing_cpu(cpu) {
5119 atomic_dec(&iter.tr->data[cpu]->disabled);
5125 arch_spin_unlock(&ftrace_dump_lock);
5126 local_irq_restore(flags);
5129 /* By default: disable tracing after the dump */
5130 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
5132 __ftrace_dump(true, oops_dump_mode);
5134 EXPORT_SYMBOL_GPL(ftrace_dump);
5136 __init static int tracer_alloc_buffers(void)
5139 enum ring_buffer_flags rb_flags;
5144 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
5147 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
5148 goto out_free_buffer_mask;
5150 /* Only allocate trace_printk buffers if a trace_printk exists */
5151 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
5152 /* Must be called before global_trace.buffer is allocated */
5153 trace_printk_init_buffers();
5155 /* To save memory, keep the ring buffer size to its minimum */
5156 if (ring_buffer_expanded)
5157 ring_buf_size = trace_buf_size;
5161 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5163 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
5164 cpumask_copy(tracing_cpumask, cpu_all_mask);
5166 /* TODO: make the number of buffers hot pluggable with CPUS */
5167 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
5168 if (!global_trace.buffer) {
5169 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
5171 goto out_free_cpumask;
5173 if (global_trace.buffer_disabled)
5177 #ifdef CONFIG_TRACER_MAX_TRACE
5178 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
5179 if (!max_tr.buffer) {
5180 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
5182 ring_buffer_free(global_trace.buffer);
5183 goto out_free_cpumask;
5187 /* Allocate the first page for all buffers */
5188 for_each_tracing_cpu(i) {
5189 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
5190 max_tr.data[i] = &per_cpu(max_tr_data, i);
5193 set_buffer_entries(&global_trace,
5194 ring_buffer_size(global_trace.buffer, 0));
5195 #ifdef CONFIG_TRACER_MAX_TRACE
5196 set_buffer_entries(&max_tr, 1);
5199 trace_init_cmdlines();
5201 register_tracer(&nop_trace);
5202 current_trace = &nop_trace;
5203 /* All seems OK, enable tracing */
5204 tracing_disabled = 0;
5206 atomic_notifier_chain_register(&panic_notifier_list,
5207 &trace_panic_notifier);
5209 register_die_notifier(&trace_die_notifier);
5214 free_cpumask_var(tracing_cpumask);
5215 out_free_buffer_mask:
5216 free_cpumask_var(tracing_buffer_mask);
5221 __init static int clear_boot_tracer(void)
5224 * The default tracer at boot buffer is an init section.
5225 * This function is called in lateinit. If we did not
5226 * find the boot tracer, then clear it out, to prevent
5227 * later registration from accessing the buffer that is
5228 * about to be freed.
5230 if (!default_bootup_tracer)
5233 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
5234 default_bootup_tracer);
5235 default_bootup_tracer = NULL;
5240 early_initcall(tracer_alloc_buffers);
5241 fs_initcall(tracer_init_debugfs);
5242 late_initcall(clear_boot_tracer);