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 Nadia Yvette Chambers
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/irq_work.h>
23 #include <linux/debugfs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/kprobes.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/rwsem.h>
36 #include <linux/slab.h>
37 #include <linux/ctype.h>
38 #include <linux/init.h>
39 #include <linux/poll.h>
40 #include <linux/nmi.h>
42 #include <linux/sched/rt.h>
45 #include "trace_output.h"
48 * On boot up, the ring buffer is set to the minimum size, so that
49 * we do not waste memory on systems that are not using tracing.
51 int ring_buffer_expanded;
54 * We need to change this state when a selftest is running.
55 * A selftest will lurk into the ring-buffer to count the
56 * entries inserted during the selftest although some concurrent
57 * insertions into the ring-buffer such as trace_printk could occurred
58 * at the same time, giving false positive or negative results.
60 static bool __read_mostly tracing_selftest_running;
63 * If a tracer is running, we do not want to run SELFTEST.
65 bool __read_mostly tracing_selftest_disabled;
67 /* For tracers that don't implement custom flags */
68 static struct tracer_opt dummy_tracer_opt[] = {
72 static struct tracer_flags dummy_tracer_flags = {
74 .opts = dummy_tracer_opt
77 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
83 * To prevent the comm cache from being overwritten when no
84 * tracing is active, only save the comm when a trace event
87 static DEFINE_PER_CPU(bool, trace_cmdline_save);
90 * When a reader is waiting for data, then this variable is
93 static bool trace_wakeup_needed;
95 static struct irq_work trace_work_wakeup;
98 * Kill all tracing for good (never come back).
99 * It is initialized to 1 but will turn to zero if the initialization
100 * of the tracer is successful. But that is the only place that sets
103 static int tracing_disabled = 1;
105 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
107 cpumask_var_t __read_mostly tracing_buffer_mask;
110 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
112 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
113 * is set, then ftrace_dump is called. This will output the contents
114 * of the ftrace buffers to the console. This is very useful for
115 * capturing traces that lead to crashes and outputing it to a
118 * It is default off, but you can enable it with either specifying
119 * "ftrace_dump_on_oops" in the kernel command line, or setting
120 * /proc/sys/kernel/ftrace_dump_on_oops
121 * Set 1 if you want to dump buffers of all CPUs
122 * Set 2 if you want to dump the buffer of the CPU that triggered oops
125 enum ftrace_dump_mode ftrace_dump_on_oops;
127 static int tracing_set_tracer(const char *buf);
129 #define MAX_TRACER_SIZE 100
130 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
131 static char *default_bootup_tracer;
133 static int __init set_cmdline_ftrace(char *str)
135 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
136 default_bootup_tracer = bootup_tracer_buf;
137 /* We are using ftrace early, expand it */
138 ring_buffer_expanded = 1;
141 __setup("ftrace=", set_cmdline_ftrace);
143 static int __init set_ftrace_dump_on_oops(char *str)
145 if (*str++ != '=' || !*str) {
146 ftrace_dump_on_oops = DUMP_ALL;
150 if (!strcmp("orig_cpu", str)) {
151 ftrace_dump_on_oops = DUMP_ORIG;
157 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
160 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
161 static char *trace_boot_options __initdata;
163 static int __init set_trace_boot_options(char *str)
165 strncpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
166 trace_boot_options = trace_boot_options_buf;
169 __setup("trace_options=", set_trace_boot_options);
171 unsigned long long ns2usecs(cycle_t nsec)
179 * The global_trace is the descriptor that holds the tracing
180 * buffers for the live tracing. For each CPU, it contains
181 * a link list of pages that will store trace entries. The
182 * page descriptor of the pages in the memory is used to hold
183 * the link list by linking the lru item in the page descriptor
184 * to each of the pages in the buffer per CPU.
186 * For each active CPU there is a data field that holds the
187 * pages for the buffer for that CPU. Each CPU has the same number
188 * of pages allocated for its buffer.
190 static struct trace_array global_trace;
192 LIST_HEAD(ftrace_trace_arrays);
194 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
196 int filter_current_check_discard(struct ring_buffer *buffer,
197 struct ftrace_event_call *call, void *rec,
198 struct ring_buffer_event *event)
200 return filter_check_discard(call, rec, buffer, event);
202 EXPORT_SYMBOL_GPL(filter_current_check_discard);
204 cycle_t ftrace_now(int cpu)
208 /* Early boot up does not have a buffer yet */
209 if (!global_trace.buffer)
210 return trace_clock_local();
212 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
213 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
219 * The max_tr is used to snapshot the global_trace when a maximum
220 * latency is reached. Some tracers will use this to store a maximum
221 * trace while it continues examining live traces.
223 * The buffers for the max_tr are set up the same as the global_trace.
224 * When a snapshot is taken, the link list of the max_tr is swapped
225 * with the link list of the global_trace and the buffers are reset for
226 * the global_trace so the tracing can continue.
228 static struct trace_array max_tr;
230 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
232 int tracing_is_enabled(void)
234 return tracing_is_on();
238 * trace_buf_size is the size in bytes that is allocated
239 * for a buffer. Note, the number of bytes is always rounded
242 * This number is purposely set to a low number of 16384.
243 * If the dump on oops happens, it will be much appreciated
244 * to not have to wait for all that output. Anyway this can be
245 * boot time and run time configurable.
247 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
249 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
251 /* trace_types holds a link list of available tracers. */
252 static struct tracer *trace_types __read_mostly;
254 /* current_trace points to the tracer that is currently active */
255 static struct tracer *current_trace __read_mostly = &nop_trace;
258 * trace_types_lock is used to protect the trace_types list.
260 static DEFINE_MUTEX(trace_types_lock);
263 * serialize the access of the ring buffer
265 * ring buffer serializes readers, but it is low level protection.
266 * The validity of the events (which returns by ring_buffer_peek() ..etc)
267 * are not protected by ring buffer.
269 * The content of events may become garbage if we allow other process consumes
270 * these events concurrently:
271 * A) the page of the consumed events may become a normal page
272 * (not reader page) in ring buffer, and this page will be rewrited
273 * by events producer.
274 * B) The page of the consumed events may become a page for splice_read,
275 * and this page will be returned to system.
277 * These primitives allow multi process access to different cpu ring buffer
280 * These primitives don't distinguish read-only and read-consume access.
281 * Multi read-only access are also serialized.
285 static DECLARE_RWSEM(all_cpu_access_lock);
286 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
288 static inline void trace_access_lock(int cpu)
290 if (cpu == RING_BUFFER_ALL_CPUS) {
291 /* gain it for accessing the whole ring buffer. */
292 down_write(&all_cpu_access_lock);
294 /* gain it for accessing a cpu ring buffer. */
296 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
297 down_read(&all_cpu_access_lock);
299 /* Secondly block other access to this @cpu ring buffer. */
300 mutex_lock(&per_cpu(cpu_access_lock, cpu));
304 static inline void trace_access_unlock(int cpu)
306 if (cpu == RING_BUFFER_ALL_CPUS) {
307 up_write(&all_cpu_access_lock);
309 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
310 up_read(&all_cpu_access_lock);
314 static inline void trace_access_lock_init(void)
318 for_each_possible_cpu(cpu)
319 mutex_init(&per_cpu(cpu_access_lock, cpu));
324 static DEFINE_MUTEX(access_lock);
326 static inline void trace_access_lock(int cpu)
329 mutex_lock(&access_lock);
332 static inline void trace_access_unlock(int cpu)
335 mutex_unlock(&access_lock);
338 static inline void trace_access_lock_init(void)
344 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
345 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
347 /* trace_flags holds trace_options default values */
348 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
349 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
350 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
351 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS;
353 static int trace_stop_count;
354 static DEFINE_RAW_SPINLOCK(tracing_start_lock);
357 * trace_wake_up - wake up tasks waiting for trace input
359 * Schedules a delayed work to wake up any task that is blocked on the
360 * trace_wait queue. These is used with trace_poll for tasks polling the
363 static void trace_wake_up(struct irq_work *work)
365 wake_up_all(&trace_wait);
370 * tracing_on - enable tracing buffers
372 * This function enables tracing buffers that may have been
373 * disabled with tracing_off.
375 void tracing_on(void)
377 if (global_trace.buffer)
378 ring_buffer_record_on(global_trace.buffer);
380 * This flag is only looked at when buffers haven't been
381 * allocated yet. We don't really care about the race
382 * between setting this flag and actually turning
385 global_trace.buffer_disabled = 0;
387 EXPORT_SYMBOL_GPL(tracing_on);
390 * tracing_off - turn off tracing buffers
392 * This function stops the tracing buffers from recording data.
393 * It does not disable any overhead the tracers themselves may
394 * be causing. This function simply causes all recording to
395 * the ring buffers to fail.
397 void tracing_off(void)
399 if (global_trace.buffer)
400 ring_buffer_record_off(global_trace.buffer);
402 * This flag is only looked at when buffers haven't been
403 * allocated yet. We don't really care about the race
404 * between setting this flag and actually turning
407 global_trace.buffer_disabled = 1;
409 EXPORT_SYMBOL_GPL(tracing_off);
412 * tracing_is_on - show state of ring buffers enabled
414 int tracing_is_on(void)
416 if (global_trace.buffer)
417 return ring_buffer_record_is_on(global_trace.buffer);
418 return !global_trace.buffer_disabled;
420 EXPORT_SYMBOL_GPL(tracing_is_on);
422 static int __init set_buf_size(char *str)
424 unsigned long buf_size;
428 buf_size = memparse(str, &str);
429 /* nr_entries can not be zero */
432 trace_buf_size = buf_size;
435 __setup("trace_buf_size=", set_buf_size);
437 static int __init set_tracing_thresh(char *str)
439 unsigned long threshold;
444 ret = kstrtoul(str, 0, &threshold);
447 tracing_thresh = threshold * 1000;
450 __setup("tracing_thresh=", set_tracing_thresh);
452 unsigned long nsecs_to_usecs(unsigned long nsecs)
457 /* These must match the bit postions in trace_iterator_flags */
458 static const char *trace_options[] = {
490 int in_ns; /* is this clock in nanoseconds? */
492 { trace_clock_local, "local", 1 },
493 { trace_clock_global, "global", 1 },
494 { trace_clock_counter, "counter", 0 },
501 * trace_parser_get_init - gets the buffer for trace parser
503 int trace_parser_get_init(struct trace_parser *parser, int size)
505 memset(parser, 0, sizeof(*parser));
507 parser->buffer = kmalloc(size, GFP_KERNEL);
516 * trace_parser_put - frees the buffer for trace parser
518 void trace_parser_put(struct trace_parser *parser)
520 kfree(parser->buffer);
524 * trace_get_user - reads the user input string separated by space
525 * (matched by isspace(ch))
527 * For each string found the 'struct trace_parser' is updated,
528 * and the function returns.
530 * Returns number of bytes read.
532 * See kernel/trace/trace.h for 'struct trace_parser' details.
534 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
535 size_t cnt, loff_t *ppos)
542 trace_parser_clear(parser);
544 ret = get_user(ch, ubuf++);
552 * The parser is not finished with the last write,
553 * continue reading the user input without skipping spaces.
556 /* skip white space */
557 while (cnt && isspace(ch)) {
558 ret = get_user(ch, ubuf++);
565 /* only spaces were written */
575 /* read the non-space input */
576 while (cnt && !isspace(ch)) {
577 if (parser->idx < parser->size - 1)
578 parser->buffer[parser->idx++] = ch;
583 ret = get_user(ch, ubuf++);
590 /* We either got finished input or we have to wait for another call. */
592 parser->buffer[parser->idx] = 0;
593 parser->cont = false;
596 parser->buffer[parser->idx++] = ch;
606 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
614 if (s->len <= s->readpos)
617 len = s->len - s->readpos;
620 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
630 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
634 if (s->len <= s->readpos)
637 len = s->len - s->readpos;
640 memcpy(buf, s->buffer + s->readpos, cnt);
647 * ftrace_max_lock is used to protect the swapping of buffers
648 * when taking a max snapshot. The buffers themselves are
649 * protected by per_cpu spinlocks. But the action of the swap
650 * needs its own lock.
652 * This is defined as a arch_spinlock_t in order to help
653 * with performance when lockdep debugging is enabled.
655 * It is also used in other places outside the update_max_tr
656 * so it needs to be defined outside of the
657 * CONFIG_TRACER_MAX_TRACE.
659 static arch_spinlock_t ftrace_max_lock =
660 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
662 unsigned long __read_mostly tracing_thresh;
664 #ifdef CONFIG_TRACER_MAX_TRACE
665 unsigned long __read_mostly tracing_max_latency;
668 * Copy the new maximum trace into the separate maximum-trace
669 * structure. (this way the maximum trace is permanently saved,
670 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
673 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
675 struct trace_array_cpu *data = tr->data[cpu];
676 struct trace_array_cpu *max_data;
679 max_tr.time_start = data->preempt_timestamp;
681 max_data = max_tr.data[cpu];
682 max_data->saved_latency = tracing_max_latency;
683 max_data->critical_start = data->critical_start;
684 max_data->critical_end = data->critical_end;
686 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
687 max_data->pid = tsk->pid;
688 max_data->uid = task_uid(tsk);
689 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
690 max_data->policy = tsk->policy;
691 max_data->rt_priority = tsk->rt_priority;
693 /* record this tasks comm */
694 tracing_record_cmdline(tsk);
698 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
700 * @tsk: the task with the latency
701 * @cpu: The cpu that initiated the trace.
703 * Flip the buffers between the @tr and the max_tr and record information
704 * about which task was the cause of this latency.
707 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
709 struct ring_buffer *buf;
711 if (trace_stop_count)
714 WARN_ON_ONCE(!irqs_disabled());
716 if (!current_trace->allocated_snapshot) {
717 /* Only the nop tracer should hit this when disabling */
718 WARN_ON_ONCE(current_trace != &nop_trace);
722 arch_spin_lock(&ftrace_max_lock);
725 tr->buffer = max_tr.buffer;
728 __update_max_tr(tr, tsk, cpu);
729 arch_spin_unlock(&ftrace_max_lock);
733 * update_max_tr_single - only copy one trace over, and reset the rest
735 * @tsk - task with the latency
736 * @cpu - the cpu of the buffer to copy.
738 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
741 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
745 if (trace_stop_count)
748 WARN_ON_ONCE(!irqs_disabled());
749 if (WARN_ON_ONCE(!current_trace->allocated_snapshot))
752 arch_spin_lock(&ftrace_max_lock);
754 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
758 * We failed to swap the buffer due to a commit taking
759 * place on this CPU. We fail to record, but we reset
760 * the max trace buffer (no one writes directly to it)
761 * and flag that it failed.
763 trace_array_printk(&max_tr, _THIS_IP_,
764 "Failed to swap buffers due to commit in progress\n");
767 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
769 __update_max_tr(tr, tsk, cpu);
770 arch_spin_unlock(&ftrace_max_lock);
772 #endif /* CONFIG_TRACER_MAX_TRACE */
774 static void default_wait_pipe(struct trace_iterator *iter)
778 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
781 * The events can happen in critical sections where
782 * checking a work queue can cause deadlocks.
783 * After adding a task to the queue, this flag is set
784 * only to notify events to try to wake up the queue
787 * We don't clear it even if the buffer is no longer
788 * empty. The flag only causes the next event to run
789 * irq_work to do the work queue wake up. The worse
790 * that can happen if we race with !trace_empty() is that
791 * an event will cause an irq_work to try to wake up
794 * There's no reason to protect this flag either, as
795 * the work queue and irq_work logic will do the necessary
796 * synchronization for the wake ups. The only thing
797 * that is necessary is that the wake up happens after
798 * a task has been queued. It's OK for spurious wake ups.
800 trace_wakeup_needed = true;
802 if (trace_empty(iter))
805 finish_wait(&trace_wait, &wait);
809 * register_tracer - register a tracer with the ftrace system.
810 * @type - the plugin for the tracer
812 * Register a new plugin tracer.
814 int register_tracer(struct tracer *type)
820 pr_info("Tracer must have a name\n");
824 if (strlen(type->name) >= MAX_TRACER_SIZE) {
825 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
829 mutex_lock(&trace_types_lock);
831 tracing_selftest_running = true;
833 for (t = trace_types; t; t = t->next) {
834 if (strcmp(type->name, t->name) == 0) {
836 pr_info("Tracer %s already registered\n",
844 type->set_flag = &dummy_set_flag;
846 type->flags = &dummy_tracer_flags;
848 if (!type->flags->opts)
849 type->flags->opts = dummy_tracer_opt;
850 if (!type->wait_pipe)
851 type->wait_pipe = default_wait_pipe;
854 #ifdef CONFIG_FTRACE_STARTUP_TEST
855 if (type->selftest && !tracing_selftest_disabled) {
856 struct tracer *saved_tracer = current_trace;
857 struct trace_array *tr = &global_trace;
860 * Run a selftest on this tracer.
861 * Here we reset the trace buffer, and set the current
862 * tracer to be this tracer. The tracer can then run some
863 * internal tracing to verify that everything is in order.
864 * If we fail, we do not register this tracer.
866 tracing_reset_online_cpus(tr);
868 current_trace = type;
870 if (type->use_max_tr) {
871 /* If we expanded the buffers, make sure the max is expanded too */
872 if (ring_buffer_expanded)
873 ring_buffer_resize(max_tr.buffer, trace_buf_size,
874 RING_BUFFER_ALL_CPUS);
875 type->allocated_snapshot = true;
878 /* the test is responsible for initializing and enabling */
879 pr_info("Testing tracer %s: ", type->name);
880 ret = type->selftest(type, tr);
881 /* the test is responsible for resetting too */
882 current_trace = saved_tracer;
884 printk(KERN_CONT "FAILED!\n");
885 /* Add the warning after printing 'FAILED' */
889 /* Only reset on passing, to avoid touching corrupted buffers */
890 tracing_reset_online_cpus(tr);
892 if (type->use_max_tr) {
893 type->allocated_snapshot = false;
895 /* Shrink the max buffer again */
896 if (ring_buffer_expanded)
897 ring_buffer_resize(max_tr.buffer, 1,
898 RING_BUFFER_ALL_CPUS);
901 printk(KERN_CONT "PASSED\n");
905 type->next = trace_types;
909 tracing_selftest_running = false;
910 mutex_unlock(&trace_types_lock);
912 if (ret || !default_bootup_tracer)
915 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
918 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
919 /* Do we want this tracer to start on bootup? */
920 tracing_set_tracer(type->name);
921 default_bootup_tracer = NULL;
922 /* disable other selftests, since this will break it. */
923 tracing_selftest_disabled = 1;
924 #ifdef CONFIG_FTRACE_STARTUP_TEST
925 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
933 void tracing_reset(struct trace_array *tr, int cpu)
935 struct ring_buffer *buffer = tr->buffer;
940 ring_buffer_record_disable(buffer);
942 /* Make sure all commits have finished */
944 ring_buffer_reset_cpu(buffer, cpu);
946 ring_buffer_record_enable(buffer);
949 void tracing_reset_online_cpus(struct trace_array *tr)
951 struct ring_buffer *buffer = tr->buffer;
957 ring_buffer_record_disable(buffer);
959 /* Make sure all commits have finished */
962 tr->time_start = ftrace_now(tr->cpu);
964 for_each_online_cpu(cpu)
965 ring_buffer_reset_cpu(buffer, cpu);
967 ring_buffer_record_enable(buffer);
970 void tracing_reset_current(int cpu)
972 tracing_reset(&global_trace, cpu);
975 void tracing_reset_current_online_cpus(void)
977 tracing_reset_online_cpus(&global_trace);
980 #define SAVED_CMDLINES 128
981 #define NO_CMDLINE_MAP UINT_MAX
982 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
983 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
984 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
985 static int cmdline_idx;
986 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
988 /* temporary disable recording */
989 static atomic_t trace_record_cmdline_disabled __read_mostly;
991 static void trace_init_cmdlines(void)
993 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
994 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
998 int is_tracing_stopped(void)
1000 return trace_stop_count;
1004 * ftrace_off_permanent - disable all ftrace code permanently
1006 * This should only be called when a serious anomally has
1007 * been detected. This will turn off the function tracing,
1008 * ring buffers, and other tracing utilites. It takes no
1009 * locks and can be called from any context.
1011 void ftrace_off_permanent(void)
1013 tracing_disabled = 1;
1015 tracing_off_permanent();
1019 * tracing_start - quick start of the tracer
1021 * If tracing is enabled but was stopped by tracing_stop,
1022 * this will start the tracer back up.
1024 void tracing_start(void)
1026 struct ring_buffer *buffer;
1027 unsigned long flags;
1029 if (tracing_disabled)
1032 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1033 if (--trace_stop_count) {
1034 if (trace_stop_count < 0) {
1035 /* Someone screwed up their debugging */
1037 trace_stop_count = 0;
1042 /* Prevent the buffers from switching */
1043 arch_spin_lock(&ftrace_max_lock);
1045 buffer = global_trace.buffer;
1047 ring_buffer_record_enable(buffer);
1049 buffer = max_tr.buffer;
1051 ring_buffer_record_enable(buffer);
1053 arch_spin_unlock(&ftrace_max_lock);
1057 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1061 * tracing_stop - quick stop of the tracer
1063 * Light weight way to stop tracing. Use in conjunction with
1066 void tracing_stop(void)
1068 struct ring_buffer *buffer;
1069 unsigned long flags;
1072 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1073 if (trace_stop_count++)
1076 /* Prevent the buffers from switching */
1077 arch_spin_lock(&ftrace_max_lock);
1079 buffer = global_trace.buffer;
1081 ring_buffer_record_disable(buffer);
1083 buffer = max_tr.buffer;
1085 ring_buffer_record_disable(buffer);
1087 arch_spin_unlock(&ftrace_max_lock);
1090 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1093 void trace_stop_cmdline_recording(void);
1095 static void trace_save_cmdline(struct task_struct *tsk)
1099 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1103 * It's not the end of the world if we don't get
1104 * the lock, but we also don't want to spin
1105 * nor do we want to disable interrupts,
1106 * so if we miss here, then better luck next time.
1108 if (!arch_spin_trylock(&trace_cmdline_lock))
1111 idx = map_pid_to_cmdline[tsk->pid];
1112 if (idx == NO_CMDLINE_MAP) {
1113 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1116 * Check whether the cmdline buffer at idx has a pid
1117 * mapped. We are going to overwrite that entry so we
1118 * need to clear the map_pid_to_cmdline. Otherwise we
1119 * would read the new comm for the old pid.
1121 pid = map_cmdline_to_pid[idx];
1122 if (pid != NO_CMDLINE_MAP)
1123 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1125 map_cmdline_to_pid[idx] = tsk->pid;
1126 map_pid_to_cmdline[tsk->pid] = idx;
1131 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1133 arch_spin_unlock(&trace_cmdline_lock);
1136 void trace_find_cmdline(int pid, char comm[])
1141 strcpy(comm, "<idle>");
1145 if (WARN_ON_ONCE(pid < 0)) {
1146 strcpy(comm, "<XXX>");
1150 if (pid > PID_MAX_DEFAULT) {
1151 strcpy(comm, "<...>");
1156 arch_spin_lock(&trace_cmdline_lock);
1157 map = map_pid_to_cmdline[pid];
1158 if (map != NO_CMDLINE_MAP)
1159 strcpy(comm, saved_cmdlines[map]);
1161 strcpy(comm, "<...>");
1163 arch_spin_unlock(&trace_cmdline_lock);
1167 void tracing_record_cmdline(struct task_struct *tsk)
1169 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1172 if (!__this_cpu_read(trace_cmdline_save))
1175 __this_cpu_write(trace_cmdline_save, false);
1177 trace_save_cmdline(tsk);
1181 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1184 struct task_struct *tsk = current;
1186 entry->preempt_count = pc & 0xff;
1187 entry->pid = (tsk) ? tsk->pid : 0;
1189 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1190 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1192 TRACE_FLAG_IRQS_NOSUPPORT |
1194 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1195 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1196 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1198 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1200 struct ring_buffer_event *
1201 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1204 unsigned long flags, int pc)
1206 struct ring_buffer_event *event;
1208 event = ring_buffer_lock_reserve(buffer, len);
1209 if (event != NULL) {
1210 struct trace_entry *ent = ring_buffer_event_data(event);
1212 tracing_generic_entry_update(ent, flags, pc);
1220 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1222 __this_cpu_write(trace_cmdline_save, true);
1223 if (trace_wakeup_needed) {
1224 trace_wakeup_needed = false;
1225 /* irq_work_queue() supplies it's own memory barriers */
1226 irq_work_queue(&trace_work_wakeup);
1228 ring_buffer_unlock_commit(buffer, event);
1232 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1233 struct ring_buffer_event *event,
1234 unsigned long flags, int pc)
1236 __buffer_unlock_commit(buffer, event);
1238 ftrace_trace_stack(buffer, flags, 6, pc);
1239 ftrace_trace_userstack(buffer, flags, pc);
1242 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1243 struct ring_buffer_event *event,
1244 unsigned long flags, int pc)
1246 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1248 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1250 struct ring_buffer_event *
1251 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1252 int type, unsigned long len,
1253 unsigned long flags, int pc)
1255 *current_rb = global_trace.buffer;
1256 return trace_buffer_lock_reserve(*current_rb,
1257 type, len, flags, pc);
1259 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1261 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1262 struct ring_buffer_event *event,
1263 unsigned long flags, int pc)
1265 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1267 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1269 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1270 struct ring_buffer_event *event,
1271 unsigned long flags, int pc,
1272 struct pt_regs *regs)
1274 __buffer_unlock_commit(buffer, event);
1276 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1277 ftrace_trace_userstack(buffer, flags, pc);
1279 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1281 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1282 struct ring_buffer_event *event)
1284 ring_buffer_discard_commit(buffer, event);
1286 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1289 trace_function(struct trace_array *tr,
1290 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1293 struct ftrace_event_call *call = &event_function;
1294 struct ring_buffer *buffer = tr->buffer;
1295 struct ring_buffer_event *event;
1296 struct ftrace_entry *entry;
1298 /* If we are reading the ring buffer, don't trace */
1299 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1302 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1306 entry = ring_buffer_event_data(event);
1308 entry->parent_ip = parent_ip;
1310 if (!filter_check_discard(call, entry, buffer, event))
1311 __buffer_unlock_commit(buffer, event);
1315 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1316 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1319 if (likely(!atomic_read(&data->disabled)))
1320 trace_function(tr, ip, parent_ip, flags, pc);
1323 #ifdef CONFIG_STACKTRACE
1325 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1326 struct ftrace_stack {
1327 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1330 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1331 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1333 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1334 unsigned long flags,
1335 int skip, int pc, struct pt_regs *regs)
1337 struct ftrace_event_call *call = &event_kernel_stack;
1338 struct ring_buffer_event *event;
1339 struct stack_entry *entry;
1340 struct stack_trace trace;
1342 int size = FTRACE_STACK_ENTRIES;
1344 trace.nr_entries = 0;
1348 * Since events can happen in NMIs there's no safe way to
1349 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1350 * or NMI comes in, it will just have to use the default
1351 * FTRACE_STACK_SIZE.
1353 preempt_disable_notrace();
1355 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1357 * We don't need any atomic variables, just a barrier.
1358 * If an interrupt comes in, we don't care, because it would
1359 * have exited and put the counter back to what we want.
1360 * We just need a barrier to keep gcc from moving things
1364 if (use_stack == 1) {
1365 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1366 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1369 save_stack_trace_regs(regs, &trace);
1371 save_stack_trace(&trace);
1373 if (trace.nr_entries > size)
1374 size = trace.nr_entries;
1376 /* From now on, use_stack is a boolean */
1379 size *= sizeof(unsigned long);
1381 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1382 sizeof(*entry) + size, flags, pc);
1385 entry = ring_buffer_event_data(event);
1387 memset(&entry->caller, 0, size);
1390 memcpy(&entry->caller, trace.entries,
1391 trace.nr_entries * sizeof(unsigned long));
1393 trace.max_entries = FTRACE_STACK_ENTRIES;
1394 trace.entries = entry->caller;
1396 save_stack_trace_regs(regs, &trace);
1398 save_stack_trace(&trace);
1401 entry->size = trace.nr_entries;
1403 if (!filter_check_discard(call, entry, buffer, event))
1404 __buffer_unlock_commit(buffer, event);
1407 /* Again, don't let gcc optimize things here */
1409 __this_cpu_dec(ftrace_stack_reserve);
1410 preempt_enable_notrace();
1414 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1415 int skip, int pc, struct pt_regs *regs)
1417 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1420 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1423 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1426 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1429 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1432 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1435 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
1439 * trace_dump_stack - record a stack back trace in the trace buffer
1441 void trace_dump_stack(void)
1443 unsigned long flags;
1445 if (tracing_disabled || tracing_selftest_running)
1448 local_save_flags(flags);
1450 /* skipping 3 traces, seems to get us at the caller of this function */
1451 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
1454 static DEFINE_PER_CPU(int, user_stack_count);
1457 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1459 struct ftrace_event_call *call = &event_user_stack;
1460 struct ring_buffer_event *event;
1461 struct userstack_entry *entry;
1462 struct stack_trace trace;
1464 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1468 * NMIs can not handle page faults, even with fix ups.
1469 * The save user stack can (and often does) fault.
1471 if (unlikely(in_nmi()))
1475 * prevent recursion, since the user stack tracing may
1476 * trigger other kernel events.
1479 if (__this_cpu_read(user_stack_count))
1482 __this_cpu_inc(user_stack_count);
1484 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1485 sizeof(*entry), flags, pc);
1487 goto out_drop_count;
1488 entry = ring_buffer_event_data(event);
1490 entry->tgid = current->tgid;
1491 memset(&entry->caller, 0, sizeof(entry->caller));
1493 trace.nr_entries = 0;
1494 trace.max_entries = FTRACE_STACK_ENTRIES;
1496 trace.entries = entry->caller;
1498 save_stack_trace_user(&trace);
1499 if (!filter_check_discard(call, entry, buffer, event))
1500 __buffer_unlock_commit(buffer, event);
1503 __this_cpu_dec(user_stack_count);
1509 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1511 ftrace_trace_userstack(tr, flags, preempt_count());
1515 #endif /* CONFIG_STACKTRACE */
1517 /* created for use with alloc_percpu */
1518 struct trace_buffer_struct {
1519 char buffer[TRACE_BUF_SIZE];
1522 static struct trace_buffer_struct *trace_percpu_buffer;
1523 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1524 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1525 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1528 * The buffer used is dependent on the context. There is a per cpu
1529 * buffer for normal context, softirq contex, hard irq context and
1530 * for NMI context. Thise allows for lockless recording.
1532 * Note, if the buffers failed to be allocated, then this returns NULL
1534 static char *get_trace_buf(void)
1536 struct trace_buffer_struct *percpu_buffer;
1539 * If we have allocated per cpu buffers, then we do not
1540 * need to do any locking.
1543 percpu_buffer = trace_percpu_nmi_buffer;
1545 percpu_buffer = trace_percpu_irq_buffer;
1546 else if (in_softirq())
1547 percpu_buffer = trace_percpu_sirq_buffer;
1549 percpu_buffer = trace_percpu_buffer;
1554 return this_cpu_ptr(&percpu_buffer->buffer[0]);
1557 static int alloc_percpu_trace_buffer(void)
1559 struct trace_buffer_struct *buffers;
1560 struct trace_buffer_struct *sirq_buffers;
1561 struct trace_buffer_struct *irq_buffers;
1562 struct trace_buffer_struct *nmi_buffers;
1564 buffers = alloc_percpu(struct trace_buffer_struct);
1568 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1572 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1576 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1580 trace_percpu_buffer = buffers;
1581 trace_percpu_sirq_buffer = sirq_buffers;
1582 trace_percpu_irq_buffer = irq_buffers;
1583 trace_percpu_nmi_buffer = nmi_buffers;
1588 free_percpu(irq_buffers);
1590 free_percpu(sirq_buffers);
1592 free_percpu(buffers);
1594 WARN(1, "Could not allocate percpu trace_printk buffer");
1598 static int buffers_allocated;
1600 void trace_printk_init_buffers(void)
1602 if (buffers_allocated)
1605 if (alloc_percpu_trace_buffer())
1608 pr_info("ftrace: Allocated trace_printk buffers\n");
1610 /* Expand the buffers to set size */
1611 tracing_update_buffers();
1613 buffers_allocated = 1;
1616 * trace_printk_init_buffers() can be called by modules.
1617 * If that happens, then we need to start cmdline recording
1618 * directly here. If the global_trace.buffer is already
1619 * allocated here, then this was called by module code.
1621 if (global_trace.buffer)
1622 tracing_start_cmdline_record();
1625 void trace_printk_start_comm(void)
1627 /* Start tracing comms if trace printk is set */
1628 if (!buffers_allocated)
1630 tracing_start_cmdline_record();
1633 static void trace_printk_start_stop_comm(int enabled)
1635 if (!buffers_allocated)
1639 tracing_start_cmdline_record();
1641 tracing_stop_cmdline_record();
1645 * trace_vbprintk - write binary msg to tracing buffer
1648 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1650 struct ftrace_event_call *call = &event_bprint;
1651 struct ring_buffer_event *event;
1652 struct ring_buffer *buffer;
1653 struct trace_array *tr = &global_trace;
1654 struct bprint_entry *entry;
1655 unsigned long flags;
1657 int len = 0, size, pc;
1659 if (unlikely(tracing_selftest_running || tracing_disabled))
1662 /* Don't pollute graph traces with trace_vprintk internals */
1663 pause_graph_tracing();
1665 pc = preempt_count();
1666 preempt_disable_notrace();
1668 tbuffer = get_trace_buf();
1674 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1676 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1679 local_save_flags(flags);
1680 size = sizeof(*entry) + sizeof(u32) * len;
1681 buffer = tr->buffer;
1682 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1686 entry = ring_buffer_event_data(event);
1690 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1691 if (!filter_check_discard(call, entry, buffer, event)) {
1692 __buffer_unlock_commit(buffer, event);
1693 ftrace_trace_stack(buffer, flags, 6, pc);
1697 preempt_enable_notrace();
1698 unpause_graph_tracing();
1702 EXPORT_SYMBOL_GPL(trace_vbprintk);
1704 int trace_array_printk(struct trace_array *tr,
1705 unsigned long ip, const char *fmt, ...)
1710 if (!(trace_flags & TRACE_ITER_PRINTK))
1714 ret = trace_array_vprintk(tr, ip, fmt, ap);
1719 int trace_array_vprintk(struct trace_array *tr,
1720 unsigned long ip, const char *fmt, va_list args)
1722 struct ftrace_event_call *call = &event_print;
1723 struct ring_buffer_event *event;
1724 struct ring_buffer *buffer;
1725 int len = 0, size, pc;
1726 struct print_entry *entry;
1727 unsigned long flags;
1730 if (tracing_disabled || tracing_selftest_running)
1733 /* Don't pollute graph traces with trace_vprintk internals */
1734 pause_graph_tracing();
1736 pc = preempt_count();
1737 preempt_disable_notrace();
1740 tbuffer = get_trace_buf();
1746 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1747 if (len > TRACE_BUF_SIZE)
1750 local_save_flags(flags);
1751 size = sizeof(*entry) + len + 1;
1752 buffer = tr->buffer;
1753 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1757 entry = ring_buffer_event_data(event);
1760 memcpy(&entry->buf, tbuffer, len);
1761 entry->buf[len] = '\0';
1762 if (!filter_check_discard(call, entry, buffer, event)) {
1763 __buffer_unlock_commit(buffer, event);
1764 ftrace_trace_stack(buffer, flags, 6, pc);
1767 preempt_enable_notrace();
1768 unpause_graph_tracing();
1773 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1775 return trace_array_vprintk(&global_trace, ip, fmt, args);
1777 EXPORT_SYMBOL_GPL(trace_vprintk);
1779 static void trace_iterator_increment(struct trace_iterator *iter)
1781 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
1785 ring_buffer_read(buf_iter, NULL);
1788 static struct trace_entry *
1789 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1790 unsigned long *lost_events)
1792 struct ring_buffer_event *event;
1793 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
1796 event = ring_buffer_iter_peek(buf_iter, ts);
1798 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1802 iter->ent_size = ring_buffer_event_length(event);
1803 return ring_buffer_event_data(event);
1809 static struct trace_entry *
1810 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1811 unsigned long *missing_events, u64 *ent_ts)
1813 struct ring_buffer *buffer = iter->tr->buffer;
1814 struct trace_entry *ent, *next = NULL;
1815 unsigned long lost_events = 0, next_lost = 0;
1816 int cpu_file = iter->cpu_file;
1817 u64 next_ts = 0, ts;
1823 * If we are in a per_cpu trace file, don't bother by iterating over
1824 * all cpu and peek directly.
1826 if (cpu_file > RING_BUFFER_ALL_CPUS) {
1827 if (ring_buffer_empty_cpu(buffer, cpu_file))
1829 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1831 *ent_cpu = cpu_file;
1836 for_each_tracing_cpu(cpu) {
1838 if (ring_buffer_empty_cpu(buffer, cpu))
1841 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1844 * Pick the entry with the smallest timestamp:
1846 if (ent && (!next || ts < next_ts)) {
1850 next_lost = lost_events;
1851 next_size = iter->ent_size;
1855 iter->ent_size = next_size;
1858 *ent_cpu = next_cpu;
1864 *missing_events = next_lost;
1869 /* Find the next real entry, without updating the iterator itself */
1870 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1871 int *ent_cpu, u64 *ent_ts)
1873 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1876 /* Find the next real entry, and increment the iterator to the next entry */
1877 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1879 iter->ent = __find_next_entry(iter, &iter->cpu,
1880 &iter->lost_events, &iter->ts);
1883 trace_iterator_increment(iter);
1885 return iter->ent ? iter : NULL;
1888 static void trace_consume(struct trace_iterator *iter)
1890 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1891 &iter->lost_events);
1894 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1896 struct trace_iterator *iter = m->private;
1900 WARN_ON_ONCE(iter->leftover);
1904 /* can't go backwards */
1909 ent = trace_find_next_entry_inc(iter);
1913 while (ent && iter->idx < i)
1914 ent = trace_find_next_entry_inc(iter);
1921 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1923 struct trace_array *tr = iter->tr;
1924 struct ring_buffer_event *event;
1925 struct ring_buffer_iter *buf_iter;
1926 unsigned long entries = 0;
1929 tr->data[cpu]->skipped_entries = 0;
1931 buf_iter = trace_buffer_iter(iter, cpu);
1935 ring_buffer_iter_reset(buf_iter);
1938 * We could have the case with the max latency tracers
1939 * that a reset never took place on a cpu. This is evident
1940 * by the timestamp being before the start of the buffer.
1942 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1943 if (ts >= iter->tr->time_start)
1946 ring_buffer_read(buf_iter, NULL);
1949 tr->data[cpu]->skipped_entries = entries;
1953 * The current tracer is copied to avoid a global locking
1956 static void *s_start(struct seq_file *m, loff_t *pos)
1958 struct trace_iterator *iter = m->private;
1959 int cpu_file = iter->cpu_file;
1965 * copy the tracer to avoid using a global lock all around.
1966 * iter->trace is a copy of current_trace, the pointer to the
1967 * name may be used instead of a strcmp(), as iter->trace->name
1968 * will point to the same string as current_trace->name.
1970 mutex_lock(&trace_types_lock);
1971 if (unlikely(current_trace && iter->trace->name != current_trace->name))
1972 *iter->trace = *current_trace;
1973 mutex_unlock(&trace_types_lock);
1975 if (iter->snapshot && iter->trace->use_max_tr)
1976 return ERR_PTR(-EBUSY);
1978 if (!iter->snapshot)
1979 atomic_inc(&trace_record_cmdline_disabled);
1981 if (*pos != iter->pos) {
1986 if (cpu_file == RING_BUFFER_ALL_CPUS) {
1987 for_each_tracing_cpu(cpu)
1988 tracing_iter_reset(iter, cpu);
1990 tracing_iter_reset(iter, cpu_file);
1993 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1998 * If we overflowed the seq_file before, then we want
1999 * to just reuse the trace_seq buffer again.
2005 p = s_next(m, p, &l);
2009 trace_event_read_lock();
2010 trace_access_lock(cpu_file);
2014 static void s_stop(struct seq_file *m, void *p)
2016 struct trace_iterator *iter = m->private;
2018 if (iter->snapshot && iter->trace->use_max_tr)
2021 if (!iter->snapshot)
2022 atomic_dec(&trace_record_cmdline_disabled);
2023 trace_access_unlock(iter->cpu_file);
2024 trace_event_read_unlock();
2028 get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *entries)
2030 unsigned long count;
2036 for_each_tracing_cpu(cpu) {
2037 count = ring_buffer_entries_cpu(tr->buffer, cpu);
2039 * If this buffer has skipped entries, then we hold all
2040 * entries for the trace and we need to ignore the
2041 * ones before the time stamp.
2043 if (tr->data[cpu]->skipped_entries) {
2044 count -= tr->data[cpu]->skipped_entries;
2045 /* total is the same as the entries */
2049 ring_buffer_overrun_cpu(tr->buffer, cpu);
2054 static void print_lat_help_header(struct seq_file *m)
2056 seq_puts(m, "# _------=> CPU# \n");
2057 seq_puts(m, "# / _-----=> irqs-off \n");
2058 seq_puts(m, "# | / _----=> need-resched \n");
2059 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2060 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2061 seq_puts(m, "# |||| / delay \n");
2062 seq_puts(m, "# cmd pid ||||| time | caller \n");
2063 seq_puts(m, "# \\ / ||||| \\ | / \n");
2066 static void print_event_info(struct trace_array *tr, struct seq_file *m)
2068 unsigned long total;
2069 unsigned long entries;
2071 get_total_entries(tr, &total, &entries);
2072 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2073 entries, total, num_online_cpus());
2077 static void print_func_help_header(struct trace_array *tr, struct seq_file *m)
2079 print_event_info(tr, m);
2080 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2081 seq_puts(m, "# | | | | |\n");
2084 static void print_func_help_header_irq(struct trace_array *tr, struct seq_file *m)
2086 print_event_info(tr, m);
2087 seq_puts(m, "# _-----=> irqs-off\n");
2088 seq_puts(m, "# / _----=> need-resched\n");
2089 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2090 seq_puts(m, "# || / _--=> preempt-depth\n");
2091 seq_puts(m, "# ||| / delay\n");
2092 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2093 seq_puts(m, "# | | | |||| | |\n");
2097 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2099 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2100 struct trace_array *tr = iter->tr;
2101 struct trace_array_cpu *data = tr->data[tr->cpu];
2102 struct tracer *type = current_trace;
2103 unsigned long entries;
2104 unsigned long total;
2105 const char *name = "preemption";
2109 get_total_entries(tr, &total, &entries);
2111 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2113 seq_puts(m, "# -----------------------------------"
2114 "---------------------------------\n");
2115 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2116 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2117 nsecs_to_usecs(data->saved_latency),
2121 #if defined(CONFIG_PREEMPT_NONE)
2123 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2125 #elif defined(CONFIG_PREEMPT)
2130 /* These are reserved for later use */
2133 seq_printf(m, " #P:%d)\n", num_online_cpus());
2137 seq_puts(m, "# -----------------\n");
2138 seq_printf(m, "# | task: %.16s-%d "
2139 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2140 data->comm, data->pid,
2141 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2142 data->policy, data->rt_priority);
2143 seq_puts(m, "# -----------------\n");
2145 if (data->critical_start) {
2146 seq_puts(m, "# => started at: ");
2147 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2148 trace_print_seq(m, &iter->seq);
2149 seq_puts(m, "\n# => ended at: ");
2150 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2151 trace_print_seq(m, &iter->seq);
2152 seq_puts(m, "\n#\n");
2158 static void test_cpu_buff_start(struct trace_iterator *iter)
2160 struct trace_seq *s = &iter->seq;
2162 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2165 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2168 if (cpumask_test_cpu(iter->cpu, iter->started))
2171 if (iter->tr->data[iter->cpu]->skipped_entries)
2174 cpumask_set_cpu(iter->cpu, iter->started);
2176 /* Don't print started cpu buffer for the first entry of the trace */
2178 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2182 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2184 struct trace_seq *s = &iter->seq;
2185 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2186 struct trace_entry *entry;
2187 struct trace_event *event;
2191 test_cpu_buff_start(iter);
2193 event = ftrace_find_event(entry->type);
2195 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2196 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2197 if (!trace_print_lat_context(iter))
2200 if (!trace_print_context(iter))
2206 return event->funcs->trace(iter, sym_flags, event);
2208 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2211 return TRACE_TYPE_HANDLED;
2213 return TRACE_TYPE_PARTIAL_LINE;
2216 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2218 struct trace_seq *s = &iter->seq;
2219 struct trace_entry *entry;
2220 struct trace_event *event;
2224 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2225 if (!trace_seq_printf(s, "%d %d %llu ",
2226 entry->pid, iter->cpu, iter->ts))
2230 event = ftrace_find_event(entry->type);
2232 return event->funcs->raw(iter, 0, event);
2234 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2237 return TRACE_TYPE_HANDLED;
2239 return TRACE_TYPE_PARTIAL_LINE;
2242 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2244 struct trace_seq *s = &iter->seq;
2245 unsigned char newline = '\n';
2246 struct trace_entry *entry;
2247 struct trace_event *event;
2251 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2252 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2253 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2254 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2257 event = ftrace_find_event(entry->type);
2259 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2260 if (ret != TRACE_TYPE_HANDLED)
2264 SEQ_PUT_FIELD_RET(s, newline);
2266 return TRACE_TYPE_HANDLED;
2269 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2271 struct trace_seq *s = &iter->seq;
2272 struct trace_entry *entry;
2273 struct trace_event *event;
2277 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2278 SEQ_PUT_FIELD_RET(s, entry->pid);
2279 SEQ_PUT_FIELD_RET(s, iter->cpu);
2280 SEQ_PUT_FIELD_RET(s, iter->ts);
2283 event = ftrace_find_event(entry->type);
2284 return event ? event->funcs->binary(iter, 0, event) :
2288 int trace_empty(struct trace_iterator *iter)
2290 struct ring_buffer_iter *buf_iter;
2293 /* If we are looking at one CPU buffer, only check that one */
2294 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2295 cpu = iter->cpu_file;
2296 buf_iter = trace_buffer_iter(iter, cpu);
2298 if (!ring_buffer_iter_empty(buf_iter))
2301 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2307 for_each_tracing_cpu(cpu) {
2308 buf_iter = trace_buffer_iter(iter, cpu);
2310 if (!ring_buffer_iter_empty(buf_iter))
2313 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2321 /* Called with trace_event_read_lock() held. */
2322 enum print_line_t print_trace_line(struct trace_iterator *iter)
2324 enum print_line_t ret;
2326 if (iter->lost_events &&
2327 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2328 iter->cpu, iter->lost_events))
2329 return TRACE_TYPE_PARTIAL_LINE;
2331 if (iter->trace && iter->trace->print_line) {
2332 ret = iter->trace->print_line(iter);
2333 if (ret != TRACE_TYPE_UNHANDLED)
2337 if (iter->ent->type == TRACE_BPRINT &&
2338 trace_flags & TRACE_ITER_PRINTK &&
2339 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2340 return trace_print_bprintk_msg_only(iter);
2342 if (iter->ent->type == TRACE_PRINT &&
2343 trace_flags & TRACE_ITER_PRINTK &&
2344 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2345 return trace_print_printk_msg_only(iter);
2347 if (trace_flags & TRACE_ITER_BIN)
2348 return print_bin_fmt(iter);
2350 if (trace_flags & TRACE_ITER_HEX)
2351 return print_hex_fmt(iter);
2353 if (trace_flags & TRACE_ITER_RAW)
2354 return print_raw_fmt(iter);
2356 return print_trace_fmt(iter);
2359 void trace_latency_header(struct seq_file *m)
2361 struct trace_iterator *iter = m->private;
2363 /* print nothing if the buffers are empty */
2364 if (trace_empty(iter))
2367 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2368 print_trace_header(m, iter);
2370 if (!(trace_flags & TRACE_ITER_VERBOSE))
2371 print_lat_help_header(m);
2374 void trace_default_header(struct seq_file *m)
2376 struct trace_iterator *iter = m->private;
2378 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2381 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2382 /* print nothing if the buffers are empty */
2383 if (trace_empty(iter))
2385 print_trace_header(m, iter);
2386 if (!(trace_flags & TRACE_ITER_VERBOSE))
2387 print_lat_help_header(m);
2389 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2390 if (trace_flags & TRACE_ITER_IRQ_INFO)
2391 print_func_help_header_irq(iter->tr, m);
2393 print_func_help_header(iter->tr, m);
2398 static void test_ftrace_alive(struct seq_file *m)
2400 if (!ftrace_is_dead())
2402 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2403 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2406 #ifdef CONFIG_TRACER_MAX_TRACE
2407 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2409 if (iter->trace->allocated_snapshot)
2410 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2412 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2414 seq_printf(m, "# Snapshot commands:\n");
2415 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2416 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2417 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2418 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2419 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2420 seq_printf(m, "# is not a '0' or '1')\n");
2423 /* Should never be called */
2424 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2427 static int s_show(struct seq_file *m, void *v)
2429 struct trace_iterator *iter = v;
2432 if (iter->ent == NULL) {
2434 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2436 test_ftrace_alive(m);
2438 if (iter->snapshot && trace_empty(iter))
2439 print_snapshot_help(m, iter);
2440 else if (iter->trace && iter->trace->print_header)
2441 iter->trace->print_header(m);
2443 trace_default_header(m);
2445 } else if (iter->leftover) {
2447 * If we filled the seq_file buffer earlier, we
2448 * want to just show it now.
2450 ret = trace_print_seq(m, &iter->seq);
2452 /* ret should this time be zero, but you never know */
2453 iter->leftover = ret;
2456 print_trace_line(iter);
2457 ret = trace_print_seq(m, &iter->seq);
2459 * If we overflow the seq_file buffer, then it will
2460 * ask us for this data again at start up.
2462 * ret is 0 if seq_file write succeeded.
2465 iter->leftover = ret;
2471 static const struct seq_operations tracer_seq_ops = {
2478 static struct trace_iterator *
2479 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2481 long cpu_file = (long) inode->i_private;
2482 struct trace_iterator *iter;
2485 if (tracing_disabled)
2486 return ERR_PTR(-ENODEV);
2488 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2490 return ERR_PTR(-ENOMEM);
2492 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2494 if (!iter->buffer_iter)
2498 * We make a copy of the current tracer to avoid concurrent
2499 * changes on it while we are reading.
2501 mutex_lock(&trace_types_lock);
2502 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2506 *iter->trace = *current_trace;
2508 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2511 if (current_trace->print_max || snapshot)
2514 iter->tr = &global_trace;
2515 iter->snapshot = snapshot;
2517 mutex_init(&iter->mutex);
2518 iter->cpu_file = cpu_file;
2520 /* Notify the tracer early; before we stop tracing. */
2521 if (iter->trace && iter->trace->open)
2522 iter->trace->open(iter);
2524 /* Annotate start of buffers if we had overruns */
2525 if (ring_buffer_overruns(iter->tr->buffer))
2526 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2528 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2529 if (trace_clocks[trace_clock_id].in_ns)
2530 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2532 /* stop the trace while dumping if we are not opening "snapshot" */
2533 if (!iter->snapshot)
2536 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
2537 for_each_tracing_cpu(cpu) {
2538 iter->buffer_iter[cpu] =
2539 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2541 ring_buffer_read_prepare_sync();
2542 for_each_tracing_cpu(cpu) {
2543 ring_buffer_read_start(iter->buffer_iter[cpu]);
2544 tracing_iter_reset(iter, cpu);
2547 cpu = iter->cpu_file;
2548 iter->buffer_iter[cpu] =
2549 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2550 ring_buffer_read_prepare_sync();
2551 ring_buffer_read_start(iter->buffer_iter[cpu]);
2552 tracing_iter_reset(iter, cpu);
2555 mutex_unlock(&trace_types_lock);
2560 mutex_unlock(&trace_types_lock);
2562 kfree(iter->buffer_iter);
2564 seq_release_private(inode, file);
2565 return ERR_PTR(-ENOMEM);
2568 int tracing_open_generic(struct inode *inode, struct file *filp)
2570 if (tracing_disabled)
2573 filp->private_data = inode->i_private;
2577 static int tracing_release(struct inode *inode, struct file *file)
2579 struct seq_file *m = file->private_data;
2580 struct trace_iterator *iter;
2583 if (!(file->f_mode & FMODE_READ))
2588 mutex_lock(&trace_types_lock);
2589 for_each_tracing_cpu(cpu) {
2590 if (iter->buffer_iter[cpu])
2591 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2594 if (iter->trace && iter->trace->close)
2595 iter->trace->close(iter);
2597 if (!iter->snapshot)
2598 /* reenable tracing if it was previously enabled */
2600 mutex_unlock(&trace_types_lock);
2602 mutex_destroy(&iter->mutex);
2603 free_cpumask_var(iter->started);
2605 kfree(iter->buffer_iter);
2606 seq_release_private(inode, file);
2610 static int tracing_open(struct inode *inode, struct file *file)
2612 struct trace_iterator *iter;
2615 /* If this file was open for write, then erase contents */
2616 if ((file->f_mode & FMODE_WRITE) &&
2617 (file->f_flags & O_TRUNC)) {
2618 long cpu = (long) inode->i_private;
2620 if (cpu == RING_BUFFER_ALL_CPUS)
2621 tracing_reset_online_cpus(&global_trace);
2623 tracing_reset(&global_trace, cpu);
2626 if (file->f_mode & FMODE_READ) {
2627 iter = __tracing_open(inode, file, false);
2629 ret = PTR_ERR(iter);
2630 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2631 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2637 t_next(struct seq_file *m, void *v, loff_t *pos)
2639 struct tracer *t = v;
2649 static void *t_start(struct seq_file *m, loff_t *pos)
2654 mutex_lock(&trace_types_lock);
2655 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2661 static void t_stop(struct seq_file *m, void *p)
2663 mutex_unlock(&trace_types_lock);
2666 static int t_show(struct seq_file *m, void *v)
2668 struct tracer *t = v;
2673 seq_printf(m, "%s", t->name);
2682 static const struct seq_operations show_traces_seq_ops = {
2689 static int show_traces_open(struct inode *inode, struct file *file)
2691 if (tracing_disabled)
2694 return seq_open(file, &show_traces_seq_ops);
2698 tracing_write_stub(struct file *filp, const char __user *ubuf,
2699 size_t count, loff_t *ppos)
2704 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2706 if (file->f_mode & FMODE_READ)
2707 return seq_lseek(file, offset, origin);
2712 static const struct file_operations tracing_fops = {
2713 .open = tracing_open,
2715 .write = tracing_write_stub,
2716 .llseek = tracing_seek,
2717 .release = tracing_release,
2720 static const struct file_operations show_traces_fops = {
2721 .open = show_traces_open,
2723 .release = seq_release,
2724 .llseek = seq_lseek,
2728 * Only trace on a CPU if the bitmask is set:
2730 static cpumask_var_t tracing_cpumask;
2733 * The tracer itself will not take this lock, but still we want
2734 * to provide a consistent cpumask to user-space:
2736 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2739 * Temporary storage for the character representation of the
2740 * CPU bitmask (and one more byte for the newline):
2742 static char mask_str[NR_CPUS + 1];
2745 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2746 size_t count, loff_t *ppos)
2750 mutex_lock(&tracing_cpumask_update_lock);
2752 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2753 if (count - len < 2) {
2757 len += sprintf(mask_str + len, "\n");
2758 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2761 mutex_unlock(&tracing_cpumask_update_lock);
2767 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2768 size_t count, loff_t *ppos)
2771 cpumask_var_t tracing_cpumask_new;
2773 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2776 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2780 mutex_lock(&tracing_cpumask_update_lock);
2782 local_irq_disable();
2783 arch_spin_lock(&ftrace_max_lock);
2784 for_each_tracing_cpu(cpu) {
2786 * Increase/decrease the disabled counter if we are
2787 * about to flip a bit in the cpumask:
2789 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2790 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2791 atomic_inc(&global_trace.data[cpu]->disabled);
2792 ring_buffer_record_disable_cpu(global_trace.buffer, cpu);
2794 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2795 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2796 atomic_dec(&global_trace.data[cpu]->disabled);
2797 ring_buffer_record_enable_cpu(global_trace.buffer, cpu);
2800 arch_spin_unlock(&ftrace_max_lock);
2803 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2805 mutex_unlock(&tracing_cpumask_update_lock);
2806 free_cpumask_var(tracing_cpumask_new);
2811 free_cpumask_var(tracing_cpumask_new);
2816 static const struct file_operations tracing_cpumask_fops = {
2817 .open = tracing_open_generic,
2818 .read = tracing_cpumask_read,
2819 .write = tracing_cpumask_write,
2820 .llseek = generic_file_llseek,
2823 static int tracing_trace_options_show(struct seq_file *m, void *v)
2825 struct tracer_opt *trace_opts;
2829 mutex_lock(&trace_types_lock);
2830 tracer_flags = current_trace->flags->val;
2831 trace_opts = current_trace->flags->opts;
2833 for (i = 0; trace_options[i]; i++) {
2834 if (trace_flags & (1 << i))
2835 seq_printf(m, "%s\n", trace_options[i]);
2837 seq_printf(m, "no%s\n", trace_options[i]);
2840 for (i = 0; trace_opts[i].name; i++) {
2841 if (tracer_flags & trace_opts[i].bit)
2842 seq_printf(m, "%s\n", trace_opts[i].name);
2844 seq_printf(m, "no%s\n", trace_opts[i].name);
2846 mutex_unlock(&trace_types_lock);
2851 static int __set_tracer_option(struct tracer *trace,
2852 struct tracer_flags *tracer_flags,
2853 struct tracer_opt *opts, int neg)
2857 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2862 tracer_flags->val &= ~opts->bit;
2864 tracer_flags->val |= opts->bit;
2868 /* Try to assign a tracer specific option */
2869 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2871 struct tracer_flags *tracer_flags = trace->flags;
2872 struct tracer_opt *opts = NULL;
2875 for (i = 0; tracer_flags->opts[i].name; i++) {
2876 opts = &tracer_flags->opts[i];
2878 if (strcmp(cmp, opts->name) == 0)
2879 return __set_tracer_option(trace, trace->flags,
2886 /* Some tracers require overwrite to stay enabled */
2887 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
2889 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
2895 int set_tracer_flag(unsigned int mask, int enabled)
2897 /* do nothing if flag is already set */
2898 if (!!(trace_flags & mask) == !!enabled)
2901 /* Give the tracer a chance to approve the change */
2902 if (current_trace->flag_changed)
2903 if (current_trace->flag_changed(current_trace, mask, !!enabled))
2907 trace_flags |= mask;
2909 trace_flags &= ~mask;
2911 if (mask == TRACE_ITER_RECORD_CMD)
2912 trace_event_enable_cmd_record(enabled);
2914 if (mask == TRACE_ITER_OVERWRITE) {
2915 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2916 #ifdef CONFIG_TRACER_MAX_TRACE
2917 ring_buffer_change_overwrite(max_tr.buffer, enabled);
2921 if (mask == TRACE_ITER_PRINTK)
2922 trace_printk_start_stop_comm(enabled);
2927 static int trace_set_options(char *option)
2934 cmp = strstrip(option);
2936 if (strncmp(cmp, "no", 2) == 0) {
2941 mutex_lock(&trace_types_lock);
2943 for (i = 0; trace_options[i]; i++) {
2944 if (strcmp(cmp, trace_options[i]) == 0) {
2945 ret = set_tracer_flag(1 << i, !neg);
2950 /* If no option could be set, test the specific tracer options */
2951 if (!trace_options[i])
2952 ret = set_tracer_option(current_trace, cmp, neg);
2954 mutex_unlock(&trace_types_lock);
2960 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2961 size_t cnt, loff_t *ppos)
2966 if (cnt >= sizeof(buf))
2969 if (copy_from_user(&buf, ubuf, cnt))
2974 ret = trace_set_options(buf);
2983 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2985 if (tracing_disabled)
2987 return single_open(file, tracing_trace_options_show, NULL);
2990 static const struct file_operations tracing_iter_fops = {
2991 .open = tracing_trace_options_open,
2993 .llseek = seq_lseek,
2994 .release = single_release,
2995 .write = tracing_trace_options_write,
2998 static const char readme_msg[] =
2999 "tracing mini-HOWTO:\n\n"
3000 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
3001 "# cat /sys/kernel/debug/tracing/available_tracers\n"
3002 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
3003 "# cat /sys/kernel/debug/tracing/current_tracer\n"
3005 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
3006 "# cat /sys/kernel/debug/tracing/current_tracer\n"
3008 "# cat /sys/kernel/debug/tracing/trace_options\n"
3009 "noprint-parent nosym-offset nosym-addr noverbose\n"
3010 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
3011 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
3012 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
3013 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
3017 tracing_readme_read(struct file *filp, char __user *ubuf,
3018 size_t cnt, loff_t *ppos)
3020 return simple_read_from_buffer(ubuf, cnt, ppos,
3021 readme_msg, strlen(readme_msg));
3024 static const struct file_operations tracing_readme_fops = {
3025 .open = tracing_open_generic,
3026 .read = tracing_readme_read,
3027 .llseek = generic_file_llseek,
3031 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3032 size_t cnt, loff_t *ppos)
3041 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3045 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3053 for (i = 0; i < SAVED_CMDLINES; i++) {
3056 pid = map_cmdline_to_pid[i];
3057 if (pid == -1 || pid == NO_CMDLINE_MAP)
3060 trace_find_cmdline(pid, buf_comm);
3061 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3066 len = simple_read_from_buffer(ubuf, cnt, ppos,
3075 static const struct file_operations tracing_saved_cmdlines_fops = {
3076 .open = tracing_open_generic,
3077 .read = tracing_saved_cmdlines_read,
3078 .llseek = generic_file_llseek,
3082 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3083 size_t cnt, loff_t *ppos)
3085 char buf[MAX_TRACER_SIZE+2];
3088 mutex_lock(&trace_types_lock);
3089 r = sprintf(buf, "%s\n", current_trace->name);
3090 mutex_unlock(&trace_types_lock);
3092 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3095 int tracer_init(struct tracer *t, struct trace_array *tr)
3097 tracing_reset_online_cpus(tr);
3101 static void set_buffer_entries(struct trace_array *tr, unsigned long val)
3104 for_each_tracing_cpu(cpu)
3105 tr->data[cpu]->entries = val;
3108 /* resize @tr's buffer to the size of @size_tr's entries */
3109 static int resize_buffer_duplicate_size(struct trace_array *tr,
3110 struct trace_array *size_tr, int cpu_id)
3114 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3115 for_each_tracing_cpu(cpu) {
3116 ret = ring_buffer_resize(tr->buffer,
3117 size_tr->data[cpu]->entries, cpu);
3120 tr->data[cpu]->entries = size_tr->data[cpu]->entries;
3123 ret = ring_buffer_resize(tr->buffer,
3124 size_tr->data[cpu_id]->entries, cpu_id);
3126 tr->data[cpu_id]->entries =
3127 size_tr->data[cpu_id]->entries;
3133 static int __tracing_resize_ring_buffer(unsigned long size, int cpu)
3138 * If kernel or user changes the size of the ring buffer
3139 * we use the size that was given, and we can forget about
3140 * expanding it later.
3142 ring_buffer_expanded = 1;
3144 /* May be called before buffers are initialized */
3145 if (!global_trace.buffer)
3148 ret = ring_buffer_resize(global_trace.buffer, size, cpu);
3152 if (!current_trace->use_max_tr)
3155 ret = ring_buffer_resize(max_tr.buffer, size, cpu);
3157 int r = resize_buffer_duplicate_size(&global_trace,
3158 &global_trace, cpu);
3161 * AARGH! We are left with different
3162 * size max buffer!!!!
3163 * The max buffer is our "snapshot" buffer.
3164 * When a tracer needs a snapshot (one of the
3165 * latency tracers), it swaps the max buffer
3166 * with the saved snap shot. We succeeded to
3167 * update the size of the main buffer, but failed to
3168 * update the size of the max buffer. But when we tried
3169 * to reset the main buffer to the original size, we
3170 * failed there too. This is very unlikely to
3171 * happen, but if it does, warn and kill all
3175 tracing_disabled = 1;
3180 if (cpu == RING_BUFFER_ALL_CPUS)
3181 set_buffer_entries(&max_tr, size);
3183 max_tr.data[cpu]->entries = size;
3186 if (cpu == RING_BUFFER_ALL_CPUS)
3187 set_buffer_entries(&global_trace, size);
3189 global_trace.data[cpu]->entries = size;
3194 static ssize_t tracing_resize_ring_buffer(unsigned long size, int cpu_id)
3198 mutex_lock(&trace_types_lock);
3200 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3201 /* make sure, this cpu is enabled in the mask */
3202 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3208 ret = __tracing_resize_ring_buffer(size, cpu_id);
3213 mutex_unlock(&trace_types_lock);
3220 * tracing_update_buffers - used by tracing facility to expand ring buffers
3222 * To save on memory when the tracing is never used on a system with it
3223 * configured in. The ring buffers are set to a minimum size. But once
3224 * a user starts to use the tracing facility, then they need to grow
3225 * to their default size.
3227 * This function is to be called when a tracer is about to be used.
3229 int tracing_update_buffers(void)
3233 mutex_lock(&trace_types_lock);
3234 if (!ring_buffer_expanded)
3235 ret = __tracing_resize_ring_buffer(trace_buf_size,
3236 RING_BUFFER_ALL_CPUS);
3237 mutex_unlock(&trace_types_lock);
3242 struct trace_option_dentry;
3244 static struct trace_option_dentry *
3245 create_trace_option_files(struct tracer *tracer);
3248 destroy_trace_option_files(struct trace_option_dentry *topts);
3250 static int tracing_set_tracer(const char *buf)
3252 static struct trace_option_dentry *topts;
3253 struct trace_array *tr = &global_trace;
3258 mutex_lock(&trace_types_lock);
3260 if (!ring_buffer_expanded) {
3261 ret = __tracing_resize_ring_buffer(trace_buf_size,
3262 RING_BUFFER_ALL_CPUS);
3268 for (t = trace_types; t; t = t->next) {
3269 if (strcmp(t->name, buf) == 0)
3276 if (t == current_trace)
3279 trace_branch_disable();
3281 current_trace->enabled = false;
3283 if (current_trace->reset)
3284 current_trace->reset(tr);
3286 had_max_tr = current_trace->allocated_snapshot;
3287 current_trace = &nop_trace;
3289 if (had_max_tr && !t->use_max_tr) {
3291 * We need to make sure that the update_max_tr sees that
3292 * current_trace changed to nop_trace to keep it from
3293 * swapping the buffers after we resize it.
3294 * The update_max_tr is called from interrupts disabled
3295 * so a synchronized_sched() is sufficient.
3297 synchronize_sched();
3299 * We don't free the ring buffer. instead, resize it because
3300 * The max_tr ring buffer has some state (e.g. ring->clock) and
3301 * we want preserve it.
3303 ring_buffer_resize(max_tr.buffer, 1, RING_BUFFER_ALL_CPUS);
3304 set_buffer_entries(&max_tr, 1);
3305 tracing_reset_online_cpus(&max_tr);
3306 current_trace->allocated_snapshot = false;
3308 destroy_trace_option_files(topts);
3310 topts = create_trace_option_files(t);
3311 if (t->use_max_tr && !had_max_tr) {
3312 /* we need to make per cpu buffer sizes equivalent */
3313 ret = resize_buffer_duplicate_size(&max_tr, &global_trace,
3314 RING_BUFFER_ALL_CPUS);
3317 t->allocated_snapshot = true;
3321 ret = tracer_init(t, tr);
3327 current_trace->enabled = true;
3328 trace_branch_enable(tr);
3330 mutex_unlock(&trace_types_lock);
3336 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3337 size_t cnt, loff_t *ppos)
3339 char buf[MAX_TRACER_SIZE+1];
3346 if (cnt > MAX_TRACER_SIZE)
3347 cnt = MAX_TRACER_SIZE;
3349 if (copy_from_user(&buf, ubuf, cnt))
3354 /* strip ending whitespace. */
3355 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3358 err = tracing_set_tracer(buf);
3368 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3369 size_t cnt, loff_t *ppos)
3371 unsigned long *ptr = filp->private_data;
3375 r = snprintf(buf, sizeof(buf), "%ld\n",
3376 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3377 if (r > sizeof(buf))
3379 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3383 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3384 size_t cnt, loff_t *ppos)
3386 unsigned long *ptr = filp->private_data;
3390 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3399 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3401 long cpu_file = (long) inode->i_private;
3402 struct trace_iterator *iter;
3405 if (tracing_disabled)
3408 mutex_lock(&trace_types_lock);
3410 /* create a buffer to store the information to pass to userspace */
3411 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3418 * We make a copy of the current tracer to avoid concurrent
3419 * changes on it while we are reading.
3421 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3426 *iter->trace = *current_trace;
3428 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3433 /* trace pipe does not show start of buffer */
3434 cpumask_setall(iter->started);
3436 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3437 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3439 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3440 if (trace_clocks[trace_clock_id].in_ns)
3441 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3443 iter->cpu_file = cpu_file;
3444 iter->tr = &global_trace;
3445 mutex_init(&iter->mutex);
3446 filp->private_data = iter;
3448 if (iter->trace->pipe_open)
3449 iter->trace->pipe_open(iter);
3451 nonseekable_open(inode, filp);
3453 mutex_unlock(&trace_types_lock);
3459 mutex_unlock(&trace_types_lock);
3463 static int tracing_release_pipe(struct inode *inode, struct file *file)
3465 struct trace_iterator *iter = file->private_data;
3467 mutex_lock(&trace_types_lock);
3469 if (iter->trace->pipe_close)
3470 iter->trace->pipe_close(iter);
3472 mutex_unlock(&trace_types_lock);
3474 free_cpumask_var(iter->started);
3475 mutex_destroy(&iter->mutex);
3483 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3485 struct trace_iterator *iter = filp->private_data;
3487 if (trace_flags & TRACE_ITER_BLOCK) {
3489 * Always select as readable when in blocking mode
3491 return POLLIN | POLLRDNORM;
3493 if (!trace_empty(iter))
3494 return POLLIN | POLLRDNORM;
3495 poll_wait(filp, &trace_wait, poll_table);
3496 if (!trace_empty(iter))
3497 return POLLIN | POLLRDNORM;
3504 * This is a make-shift waitqueue.
3505 * A tracer might use this callback on some rare cases:
3507 * 1) the current tracer might hold the runqueue lock when it wakes up
3508 * a reader, hence a deadlock (sched, function, and function graph tracers)
3509 * 2) the function tracers, trace all functions, we don't want
3510 * the overhead of calling wake_up and friends
3511 * (and tracing them too)
3513 * Anyway, this is really very primitive wakeup.
3515 void poll_wait_pipe(struct trace_iterator *iter)
3517 set_current_state(TASK_INTERRUPTIBLE);
3518 /* sleep for 100 msecs, and try again. */
3519 schedule_timeout(HZ / 10);
3522 /* Must be called with trace_types_lock mutex held. */
3523 static int tracing_wait_pipe(struct file *filp)
3525 struct trace_iterator *iter = filp->private_data;
3527 while (trace_empty(iter)) {
3529 if ((filp->f_flags & O_NONBLOCK)) {
3533 mutex_unlock(&iter->mutex);
3535 iter->trace->wait_pipe(iter);
3537 mutex_lock(&iter->mutex);
3539 if (signal_pending(current))
3543 * We block until we read something and tracing is disabled.
3544 * We still block if tracing is disabled, but we have never
3545 * read anything. This allows a user to cat this file, and
3546 * then enable tracing. But after we have read something,
3547 * we give an EOF when tracing is again disabled.
3549 * iter->pos will be 0 if we haven't read anything.
3551 if (!tracing_is_enabled() && iter->pos)
3562 tracing_read_pipe(struct file *filp, char __user *ubuf,
3563 size_t cnt, loff_t *ppos)
3565 struct trace_iterator *iter = filp->private_data;
3568 /* return any leftover data */
3569 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3573 trace_seq_init(&iter->seq);
3575 /* copy the tracer to avoid using a global lock all around */
3576 mutex_lock(&trace_types_lock);
3577 if (unlikely(iter->trace->name != current_trace->name))
3578 *iter->trace = *current_trace;
3579 mutex_unlock(&trace_types_lock);
3582 * Avoid more than one consumer on a single file descriptor
3583 * This is just a matter of traces coherency, the ring buffer itself
3586 mutex_lock(&iter->mutex);
3587 if (iter->trace->read) {
3588 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3594 sret = tracing_wait_pipe(filp);
3598 /* stop when tracing is finished */
3599 if (trace_empty(iter)) {
3604 if (cnt >= PAGE_SIZE)
3605 cnt = PAGE_SIZE - 1;
3607 /* reset all but tr, trace, and overruns */
3608 memset(&iter->seq, 0,
3609 sizeof(struct trace_iterator) -
3610 offsetof(struct trace_iterator, seq));
3613 trace_event_read_lock();
3614 trace_access_lock(iter->cpu_file);
3615 while (trace_find_next_entry_inc(iter) != NULL) {
3616 enum print_line_t ret;
3617 int len = iter->seq.len;
3619 ret = print_trace_line(iter);
3620 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3621 /* don't print partial lines */
3622 iter->seq.len = len;
3625 if (ret != TRACE_TYPE_NO_CONSUME)
3626 trace_consume(iter);
3628 if (iter->seq.len >= cnt)
3632 * Setting the full flag means we reached the trace_seq buffer
3633 * size and we should leave by partial output condition above.
3634 * One of the trace_seq_* functions is not used properly.
3636 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3639 trace_access_unlock(iter->cpu_file);
3640 trace_event_read_unlock();
3642 /* Now copy what we have to the user */
3643 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3644 if (iter->seq.readpos >= iter->seq.len)
3645 trace_seq_init(&iter->seq);
3648 * If there was nothing to send to user, in spite of consuming trace
3649 * entries, go back to wait for more entries.
3655 mutex_unlock(&iter->mutex);
3660 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3661 struct pipe_buffer *buf)
3663 __free_page(buf->page);
3666 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3669 __free_page(spd->pages[idx]);
3672 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3674 .map = generic_pipe_buf_map,
3675 .unmap = generic_pipe_buf_unmap,
3676 .confirm = generic_pipe_buf_confirm,
3677 .release = tracing_pipe_buf_release,
3678 .steal = generic_pipe_buf_steal,
3679 .get = generic_pipe_buf_get,
3683 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3688 /* Seq buffer is page-sized, exactly what we need. */
3690 count = iter->seq.len;
3691 ret = print_trace_line(iter);
3692 count = iter->seq.len - count;
3695 iter->seq.len -= count;
3698 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3699 iter->seq.len -= count;
3703 if (ret != TRACE_TYPE_NO_CONSUME)
3704 trace_consume(iter);
3706 if (!trace_find_next_entry_inc(iter)) {
3716 static ssize_t tracing_splice_read_pipe(struct file *filp,
3718 struct pipe_inode_info *pipe,
3722 struct page *pages_def[PIPE_DEF_BUFFERS];
3723 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3724 struct trace_iterator *iter = filp->private_data;
3725 struct splice_pipe_desc spd = {
3727 .partial = partial_def,
3728 .nr_pages = 0, /* This gets updated below. */
3729 .nr_pages_max = PIPE_DEF_BUFFERS,
3731 .ops = &tracing_pipe_buf_ops,
3732 .spd_release = tracing_spd_release_pipe,
3738 if (splice_grow_spd(pipe, &spd))
3741 /* copy the tracer to avoid using a global lock all around */
3742 mutex_lock(&trace_types_lock);
3743 if (unlikely(iter->trace->name != current_trace->name))
3744 *iter->trace = *current_trace;
3745 mutex_unlock(&trace_types_lock);
3747 mutex_lock(&iter->mutex);
3749 if (iter->trace->splice_read) {
3750 ret = iter->trace->splice_read(iter, filp,
3751 ppos, pipe, len, flags);
3756 ret = tracing_wait_pipe(filp);
3760 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3765 trace_event_read_lock();
3766 trace_access_lock(iter->cpu_file);
3768 /* Fill as many pages as possible. */
3769 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3770 spd.pages[i] = alloc_page(GFP_KERNEL);
3774 rem = tracing_fill_pipe_page(rem, iter);
3776 /* Copy the data into the page, so we can start over. */
3777 ret = trace_seq_to_buffer(&iter->seq,
3778 page_address(spd.pages[i]),
3781 __free_page(spd.pages[i]);
3784 spd.partial[i].offset = 0;
3785 spd.partial[i].len = iter->seq.len;
3787 trace_seq_init(&iter->seq);
3790 trace_access_unlock(iter->cpu_file);
3791 trace_event_read_unlock();
3792 mutex_unlock(&iter->mutex);
3796 ret = splice_to_pipe(pipe, &spd);
3798 splice_shrink_spd(&spd);
3802 mutex_unlock(&iter->mutex);
3806 struct ftrace_entries_info {
3807 struct trace_array *tr;
3811 static int tracing_entries_open(struct inode *inode, struct file *filp)
3813 struct ftrace_entries_info *info;
3815 if (tracing_disabled)
3818 info = kzalloc(sizeof(*info), GFP_KERNEL);
3822 info->tr = &global_trace;
3823 info->cpu = (unsigned long)inode->i_private;
3825 filp->private_data = info;
3831 tracing_entries_read(struct file *filp, char __user *ubuf,
3832 size_t cnt, loff_t *ppos)
3834 struct ftrace_entries_info *info = filp->private_data;
3835 struct trace_array *tr = info->tr;
3840 mutex_lock(&trace_types_lock);
3842 if (info->cpu == RING_BUFFER_ALL_CPUS) {
3843 int cpu, buf_size_same;
3848 /* check if all cpu sizes are same */
3849 for_each_tracing_cpu(cpu) {
3850 /* fill in the size from first enabled cpu */
3852 size = tr->data[cpu]->entries;
3853 if (size != tr->data[cpu]->entries) {
3859 if (buf_size_same) {
3860 if (!ring_buffer_expanded)
3861 r = sprintf(buf, "%lu (expanded: %lu)\n",
3863 trace_buf_size >> 10);
3865 r = sprintf(buf, "%lu\n", size >> 10);
3867 r = sprintf(buf, "X\n");
3869 r = sprintf(buf, "%lu\n", tr->data[info->cpu]->entries >> 10);
3871 mutex_unlock(&trace_types_lock);
3873 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3878 tracing_entries_write(struct file *filp, const char __user *ubuf,
3879 size_t cnt, loff_t *ppos)
3881 struct ftrace_entries_info *info = filp->private_data;
3885 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3889 /* must have at least 1 entry */
3893 /* value is in KB */
3896 ret = tracing_resize_ring_buffer(val, info->cpu);
3906 tracing_entries_release(struct inode *inode, struct file *filp)
3908 struct ftrace_entries_info *info = filp->private_data;
3916 tracing_total_entries_read(struct file *filp, char __user *ubuf,
3917 size_t cnt, loff_t *ppos)
3919 struct trace_array *tr = filp->private_data;
3922 unsigned long size = 0, expanded_size = 0;
3924 mutex_lock(&trace_types_lock);
3925 for_each_tracing_cpu(cpu) {
3926 size += tr->data[cpu]->entries >> 10;
3927 if (!ring_buffer_expanded)
3928 expanded_size += trace_buf_size >> 10;
3930 if (ring_buffer_expanded)
3931 r = sprintf(buf, "%lu\n", size);
3933 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3934 mutex_unlock(&trace_types_lock);
3936 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3940 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3941 size_t cnt, loff_t *ppos)
3944 * There is no need to read what the user has written, this function
3945 * is just to make sure that there is no error when "echo" is used
3954 tracing_free_buffer_release(struct inode *inode, struct file *filp)
3956 /* disable tracing ? */
3957 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3959 /* resize the ring buffer to 0 */
3960 tracing_resize_ring_buffer(0, RING_BUFFER_ALL_CPUS);
3966 tracing_mark_write(struct file *filp, const char __user *ubuf,
3967 size_t cnt, loff_t *fpos)
3969 unsigned long addr = (unsigned long)ubuf;
3970 struct ring_buffer_event *event;
3971 struct ring_buffer *buffer;
3972 struct print_entry *entry;
3973 unsigned long irq_flags;
3974 struct page *pages[2];
3984 if (tracing_disabled)
3987 if (!(trace_flags & TRACE_ITER_MARKERS))
3990 if (cnt > TRACE_BUF_SIZE)
3991 cnt = TRACE_BUF_SIZE;
3994 * Userspace is injecting traces into the kernel trace buffer.
3995 * We want to be as non intrusive as possible.
3996 * To do so, we do not want to allocate any special buffers
3997 * or take any locks, but instead write the userspace data
3998 * straight into the ring buffer.
4000 * First we need to pin the userspace buffer into memory,
4001 * which, most likely it is, because it just referenced it.
4002 * But there's no guarantee that it is. By using get_user_pages_fast()
4003 * and kmap_atomic/kunmap_atomic() we can get access to the
4004 * pages directly. We then write the data directly into the
4007 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4009 /* check if we cross pages */
4010 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4013 offset = addr & (PAGE_SIZE - 1);
4016 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4017 if (ret < nr_pages) {
4019 put_page(pages[ret]);
4024 for (i = 0; i < nr_pages; i++)
4025 map_page[i] = kmap_atomic(pages[i]);
4027 local_save_flags(irq_flags);
4028 size = sizeof(*entry) + cnt + 2; /* possible \n added */
4029 buffer = global_trace.buffer;
4030 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4031 irq_flags, preempt_count());
4033 /* Ring buffer disabled, return as if not open for write */
4038 entry = ring_buffer_event_data(event);
4039 entry->ip = _THIS_IP_;
4041 if (nr_pages == 2) {
4042 len = PAGE_SIZE - offset;
4043 memcpy(&entry->buf, map_page[0] + offset, len);
4044 memcpy(&entry->buf[len], map_page[1], cnt - len);
4046 memcpy(&entry->buf, map_page[0] + offset, cnt);
4048 if (entry->buf[cnt - 1] != '\n') {
4049 entry->buf[cnt] = '\n';
4050 entry->buf[cnt + 1] = '\0';
4052 entry->buf[cnt] = '\0';
4054 __buffer_unlock_commit(buffer, event);
4061 for (i = 0; i < nr_pages; i++){
4062 kunmap_atomic(map_page[i]);
4069 static int tracing_clock_show(struct seq_file *m, void *v)
4073 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4075 "%s%s%s%s", i ? " " : "",
4076 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
4077 i == trace_clock_id ? "]" : "");
4083 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4084 size_t cnt, loff_t *fpos)
4087 const char *clockstr;
4090 if (cnt >= sizeof(buf))
4093 if (copy_from_user(&buf, ubuf, cnt))
4098 clockstr = strstrip(buf);
4100 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4101 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4104 if (i == ARRAY_SIZE(trace_clocks))
4109 mutex_lock(&trace_types_lock);
4111 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
4113 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
4116 * New clock may not be consistent with the previous clock.
4117 * Reset the buffer so that it doesn't have incomparable timestamps.
4119 tracing_reset_online_cpus(&global_trace);
4120 tracing_reset_online_cpus(&max_tr);
4122 mutex_unlock(&trace_types_lock);
4129 static int tracing_clock_open(struct inode *inode, struct file *file)
4131 if (tracing_disabled)
4133 return single_open(file, tracing_clock_show, NULL);
4136 #ifdef CONFIG_TRACER_SNAPSHOT
4137 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4139 struct trace_iterator *iter;
4142 if (file->f_mode & FMODE_READ) {
4143 iter = __tracing_open(inode, file, true);
4145 ret = PTR_ERR(iter);
4151 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4157 ret = tracing_update_buffers();
4161 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4165 mutex_lock(&trace_types_lock);
4167 if (current_trace->use_max_tr) {
4174 if (current_trace->allocated_snapshot) {
4175 /* free spare buffer */
4176 ring_buffer_resize(max_tr.buffer, 1,
4177 RING_BUFFER_ALL_CPUS);
4178 set_buffer_entries(&max_tr, 1);
4179 tracing_reset_online_cpus(&max_tr);
4180 current_trace->allocated_snapshot = false;
4184 if (!current_trace->allocated_snapshot) {
4185 /* allocate spare buffer */
4186 ret = resize_buffer_duplicate_size(&max_tr,
4187 &global_trace, RING_BUFFER_ALL_CPUS);
4190 current_trace->allocated_snapshot = true;
4193 local_irq_disable();
4194 /* Now, we're going to swap */
4195 update_max_tr(&global_trace, current, smp_processor_id());
4199 if (current_trace->allocated_snapshot)
4200 tracing_reset_online_cpus(&max_tr);
4209 mutex_unlock(&trace_types_lock);
4212 #endif /* CONFIG_TRACER_SNAPSHOT */
4215 static const struct file_operations tracing_max_lat_fops = {
4216 .open = tracing_open_generic,
4217 .read = tracing_max_lat_read,
4218 .write = tracing_max_lat_write,
4219 .llseek = generic_file_llseek,
4222 static const struct file_operations set_tracer_fops = {
4223 .open = tracing_open_generic,
4224 .read = tracing_set_trace_read,
4225 .write = tracing_set_trace_write,
4226 .llseek = generic_file_llseek,
4229 static const struct file_operations tracing_pipe_fops = {
4230 .open = tracing_open_pipe,
4231 .poll = tracing_poll_pipe,
4232 .read = tracing_read_pipe,
4233 .splice_read = tracing_splice_read_pipe,
4234 .release = tracing_release_pipe,
4235 .llseek = no_llseek,
4238 static const struct file_operations tracing_entries_fops = {
4239 .open = tracing_entries_open,
4240 .read = tracing_entries_read,
4241 .write = tracing_entries_write,
4242 .release = tracing_entries_release,
4243 .llseek = generic_file_llseek,
4246 static const struct file_operations tracing_total_entries_fops = {
4247 .open = tracing_open_generic,
4248 .read = tracing_total_entries_read,
4249 .llseek = generic_file_llseek,
4252 static const struct file_operations tracing_free_buffer_fops = {
4253 .write = tracing_free_buffer_write,
4254 .release = tracing_free_buffer_release,
4257 static const struct file_operations tracing_mark_fops = {
4258 .open = tracing_open_generic,
4259 .write = tracing_mark_write,
4260 .llseek = generic_file_llseek,
4263 static const struct file_operations trace_clock_fops = {
4264 .open = tracing_clock_open,
4266 .llseek = seq_lseek,
4267 .release = single_release,
4268 .write = tracing_clock_write,
4271 #ifdef CONFIG_TRACER_SNAPSHOT
4272 static const struct file_operations snapshot_fops = {
4273 .open = tracing_snapshot_open,
4275 .write = tracing_snapshot_write,
4276 .llseek = tracing_seek,
4277 .release = tracing_release,
4279 #endif /* CONFIG_TRACER_SNAPSHOT */
4281 struct ftrace_buffer_info {
4282 struct trace_array *tr;
4288 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4290 int cpu = (int)(long)inode->i_private;
4291 struct ftrace_buffer_info *info;
4293 if (tracing_disabled)
4296 info = kzalloc(sizeof(*info), GFP_KERNEL);
4300 info->tr = &global_trace;
4303 /* Force reading ring buffer for first read */
4304 info->read = (unsigned int)-1;
4306 filp->private_data = info;
4308 return nonseekable_open(inode, filp);
4312 tracing_buffers_read(struct file *filp, char __user *ubuf,
4313 size_t count, loff_t *ppos)
4315 struct ftrace_buffer_info *info = filp->private_data;
4323 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
4327 /* Do we have previous read data to read? */
4328 if (info->read < PAGE_SIZE)
4331 trace_access_lock(info->cpu);
4332 ret = ring_buffer_read_page(info->tr->buffer,
4336 trace_access_unlock(info->cpu);
4343 size = PAGE_SIZE - info->read;
4347 ret = copy_to_user(ubuf, info->spare + info->read, size);
4358 static int tracing_buffers_release(struct inode *inode, struct file *file)
4360 struct ftrace_buffer_info *info = file->private_data;
4363 ring_buffer_free_read_page(info->tr->buffer, info->spare);
4370 struct ring_buffer *buffer;
4375 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4376 struct pipe_buffer *buf)
4378 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4383 ring_buffer_free_read_page(ref->buffer, ref->page);
4388 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4389 struct pipe_buffer *buf)
4391 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4396 /* Pipe buffer operations for a buffer. */
4397 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4399 .map = generic_pipe_buf_map,
4400 .unmap = generic_pipe_buf_unmap,
4401 .confirm = generic_pipe_buf_confirm,
4402 .release = buffer_pipe_buf_release,
4403 .steal = generic_pipe_buf_steal,
4404 .get = buffer_pipe_buf_get,
4408 * Callback from splice_to_pipe(), if we need to release some pages
4409 * at the end of the spd in case we error'ed out in filling the pipe.
4411 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4413 struct buffer_ref *ref =
4414 (struct buffer_ref *)spd->partial[i].private;
4419 ring_buffer_free_read_page(ref->buffer, ref->page);
4421 spd->partial[i].private = 0;
4425 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4426 struct pipe_inode_info *pipe, size_t len,
4429 struct ftrace_buffer_info *info = file->private_data;
4430 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4431 struct page *pages_def[PIPE_DEF_BUFFERS];
4432 struct splice_pipe_desc spd = {
4434 .partial = partial_def,
4435 .nr_pages_max = PIPE_DEF_BUFFERS,
4437 .ops = &buffer_pipe_buf_ops,
4438 .spd_release = buffer_spd_release,
4440 struct buffer_ref *ref;
4441 int entries, size, i;
4444 if (splice_grow_spd(pipe, &spd))
4447 if (*ppos & (PAGE_SIZE - 1)) {
4452 if (len & (PAGE_SIZE - 1)) {
4453 if (len < PAGE_SIZE) {
4460 trace_access_lock(info->cpu);
4461 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4463 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4467 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4472 ref->buffer = info->tr->buffer;
4473 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
4479 r = ring_buffer_read_page(ref->buffer, &ref->page,
4482 ring_buffer_free_read_page(ref->buffer, ref->page);
4488 * zero out any left over data, this is going to
4491 size = ring_buffer_page_len(ref->page);
4492 if (size < PAGE_SIZE)
4493 memset(ref->page + size, 0, PAGE_SIZE - size);
4495 page = virt_to_page(ref->page);
4497 spd.pages[i] = page;
4498 spd.partial[i].len = PAGE_SIZE;
4499 spd.partial[i].offset = 0;
4500 spd.partial[i].private = (unsigned long)ref;
4504 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4507 trace_access_unlock(info->cpu);
4510 /* did we read anything? */
4511 if (!spd.nr_pages) {
4512 if (flags & SPLICE_F_NONBLOCK)
4520 ret = splice_to_pipe(pipe, &spd);
4521 splice_shrink_spd(&spd);
4526 static const struct file_operations tracing_buffers_fops = {
4527 .open = tracing_buffers_open,
4528 .read = tracing_buffers_read,
4529 .release = tracing_buffers_release,
4530 .splice_read = tracing_buffers_splice_read,
4531 .llseek = no_llseek,
4535 tracing_stats_read(struct file *filp, char __user *ubuf,
4536 size_t count, loff_t *ppos)
4538 unsigned long cpu = (unsigned long)filp->private_data;
4539 struct trace_array *tr = &global_trace;
4540 struct trace_seq *s;
4542 unsigned long long t;
4543 unsigned long usec_rem;
4545 s = kmalloc(sizeof(*s), GFP_KERNEL);
4551 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4552 trace_seq_printf(s, "entries: %ld\n", cnt);
4554 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4555 trace_seq_printf(s, "overrun: %ld\n", cnt);
4557 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4558 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4560 cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4561 trace_seq_printf(s, "bytes: %ld\n", cnt);
4563 if (trace_clocks[trace_clock_id].in_ns) {
4564 /* local or global for trace_clock */
4565 t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4566 usec_rem = do_div(t, USEC_PER_SEC);
4567 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
4570 t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4571 usec_rem = do_div(t, USEC_PER_SEC);
4572 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4574 /* counter or tsc mode for trace_clock */
4575 trace_seq_printf(s, "oldest event ts: %llu\n",
4576 ring_buffer_oldest_event_ts(tr->buffer, cpu));
4578 trace_seq_printf(s, "now ts: %llu\n",
4579 ring_buffer_time_stamp(tr->buffer, cpu));
4582 cnt = ring_buffer_dropped_events_cpu(tr->buffer, cpu);
4583 trace_seq_printf(s, "dropped events: %ld\n", cnt);
4585 cnt = ring_buffer_read_events_cpu(tr->buffer, cpu);
4586 trace_seq_printf(s, "read events: %ld\n", cnt);
4588 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4595 static const struct file_operations tracing_stats_fops = {
4596 .open = tracing_open_generic,
4597 .read = tracing_stats_read,
4598 .llseek = generic_file_llseek,
4601 #ifdef CONFIG_DYNAMIC_FTRACE
4603 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4609 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
4610 size_t cnt, loff_t *ppos)
4612 static char ftrace_dyn_info_buffer[1024];
4613 static DEFINE_MUTEX(dyn_info_mutex);
4614 unsigned long *p = filp->private_data;
4615 char *buf = ftrace_dyn_info_buffer;
4616 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
4619 mutex_lock(&dyn_info_mutex);
4620 r = sprintf(buf, "%ld ", *p);
4622 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
4625 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4627 mutex_unlock(&dyn_info_mutex);
4632 static const struct file_operations tracing_dyn_info_fops = {
4633 .open = tracing_open_generic,
4634 .read = tracing_read_dyn_info,
4635 .llseek = generic_file_llseek,
4639 static struct dentry *d_tracer;
4641 struct dentry *tracing_init_dentry(void)
4648 if (!debugfs_initialized())
4651 d_tracer = debugfs_create_dir("tracing", NULL);
4653 if (!d_tracer && !once) {
4655 pr_warning("Could not create debugfs directory 'tracing'\n");
4662 static struct dentry *d_percpu;
4664 static struct dentry *tracing_dentry_percpu(void)
4667 struct dentry *d_tracer;
4672 d_tracer = tracing_init_dentry();
4677 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4679 if (!d_percpu && !once) {
4681 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4688 static void tracing_init_debugfs_percpu(long cpu)
4690 struct dentry *d_percpu = tracing_dentry_percpu();
4691 struct dentry *d_cpu;
4692 char cpu_dir[30]; /* 30 characters should be more than enough */
4697 snprintf(cpu_dir, 30, "cpu%ld", cpu);
4698 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4700 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4704 /* per cpu trace_pipe */
4705 trace_create_file("trace_pipe", 0444, d_cpu,
4706 (void *) cpu, &tracing_pipe_fops);
4709 trace_create_file("trace", 0644, d_cpu,
4710 (void *) cpu, &tracing_fops);
4712 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4713 (void *) cpu, &tracing_buffers_fops);
4715 trace_create_file("stats", 0444, d_cpu,
4716 (void *) cpu, &tracing_stats_fops);
4718 trace_create_file("buffer_size_kb", 0444, d_cpu,
4719 (void *) cpu, &tracing_entries_fops);
4722 #ifdef CONFIG_FTRACE_SELFTEST
4723 /* Let selftest have access to static functions in this file */
4724 #include "trace_selftest.c"
4727 struct trace_option_dentry {
4728 struct tracer_opt *opt;
4729 struct tracer_flags *flags;
4730 struct dentry *entry;
4734 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4737 struct trace_option_dentry *topt = filp->private_data;
4740 if (topt->flags->val & topt->opt->bit)
4745 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4749 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4752 struct trace_option_dentry *topt = filp->private_data;
4756 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4760 if (val != 0 && val != 1)
4763 if (!!(topt->flags->val & topt->opt->bit) != val) {
4764 mutex_lock(&trace_types_lock);
4765 ret = __set_tracer_option(current_trace, topt->flags,
4767 mutex_unlock(&trace_types_lock);
4778 static const struct file_operations trace_options_fops = {
4779 .open = tracing_open_generic,
4780 .read = trace_options_read,
4781 .write = trace_options_write,
4782 .llseek = generic_file_llseek,
4786 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4789 long index = (long)filp->private_data;
4792 if (trace_flags & (1 << index))
4797 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4801 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4804 long index = (long)filp->private_data;
4808 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4812 if (val != 0 && val != 1)
4815 mutex_lock(&trace_types_lock);
4816 ret = set_tracer_flag(1 << index, val);
4817 mutex_unlock(&trace_types_lock);
4827 static const struct file_operations trace_options_core_fops = {
4828 .open = tracing_open_generic,
4829 .read = trace_options_core_read,
4830 .write = trace_options_core_write,
4831 .llseek = generic_file_llseek,
4834 struct dentry *trace_create_file(const char *name,
4836 struct dentry *parent,
4838 const struct file_operations *fops)
4842 ret = debugfs_create_file(name, mode, parent, data, fops);
4844 pr_warning("Could not create debugfs '%s' entry\n", name);
4850 static struct dentry *trace_options_init_dentry(void)
4852 struct dentry *d_tracer;
4853 static struct dentry *t_options;
4858 d_tracer = tracing_init_dentry();
4862 t_options = debugfs_create_dir("options", d_tracer);
4864 pr_warning("Could not create debugfs directory 'options'\n");
4872 create_trace_option_file(struct trace_option_dentry *topt,
4873 struct tracer_flags *flags,
4874 struct tracer_opt *opt)
4876 struct dentry *t_options;
4878 t_options = trace_options_init_dentry();
4882 topt->flags = flags;
4885 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4886 &trace_options_fops);
4890 static struct trace_option_dentry *
4891 create_trace_option_files(struct tracer *tracer)
4893 struct trace_option_dentry *topts;
4894 struct tracer_flags *flags;
4895 struct tracer_opt *opts;
4901 flags = tracer->flags;
4903 if (!flags || !flags->opts)
4908 for (cnt = 0; opts[cnt].name; cnt++)
4911 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4915 for (cnt = 0; opts[cnt].name; cnt++)
4916 create_trace_option_file(&topts[cnt], flags,
4923 destroy_trace_option_files(struct trace_option_dentry *topts)
4930 for (cnt = 0; topts[cnt].opt; cnt++) {
4931 if (topts[cnt].entry)
4932 debugfs_remove(topts[cnt].entry);
4938 static struct dentry *
4939 create_trace_option_core_file(const char *option, long index)
4941 struct dentry *t_options;
4943 t_options = trace_options_init_dentry();
4947 return trace_create_file(option, 0644, t_options, (void *)index,
4948 &trace_options_core_fops);
4951 static __init void create_trace_options_dir(void)
4953 struct dentry *t_options;
4956 t_options = trace_options_init_dentry();
4960 for (i = 0; trace_options[i]; i++)
4961 create_trace_option_core_file(trace_options[i], i);
4965 rb_simple_read(struct file *filp, char __user *ubuf,
4966 size_t cnt, loff_t *ppos)
4968 struct trace_array *tr = filp->private_data;
4969 struct ring_buffer *buffer = tr->buffer;
4974 r = ring_buffer_record_is_on(buffer);
4978 r = sprintf(buf, "%d\n", r);
4980 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4984 rb_simple_write(struct file *filp, const char __user *ubuf,
4985 size_t cnt, loff_t *ppos)
4987 struct trace_array *tr = filp->private_data;
4988 struct ring_buffer *buffer = tr->buffer;
4992 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4997 mutex_lock(&trace_types_lock);
4999 ring_buffer_record_on(buffer);
5000 if (current_trace->start)
5001 current_trace->start(tr);
5003 ring_buffer_record_off(buffer);
5004 if (current_trace->stop)
5005 current_trace->stop(tr);
5007 mutex_unlock(&trace_types_lock);
5015 static const struct file_operations rb_simple_fops = {
5016 .open = tracing_open_generic,
5017 .read = rb_simple_read,
5018 .write = rb_simple_write,
5019 .llseek = default_llseek,
5022 static __init int tracer_init_debugfs(void)
5024 struct dentry *d_tracer;
5027 trace_access_lock_init();
5029 d_tracer = tracing_init_dentry();
5031 trace_create_file("trace_options", 0644, d_tracer,
5032 NULL, &tracing_iter_fops);
5034 trace_create_file("tracing_cpumask", 0644, d_tracer,
5035 NULL, &tracing_cpumask_fops);
5037 trace_create_file("trace", 0644, d_tracer,
5038 (void *) RING_BUFFER_ALL_CPUS, &tracing_fops);
5040 trace_create_file("available_tracers", 0444, d_tracer,
5041 &global_trace, &show_traces_fops);
5043 trace_create_file("current_tracer", 0644, d_tracer,
5044 &global_trace, &set_tracer_fops);
5046 #ifdef CONFIG_TRACER_MAX_TRACE
5047 trace_create_file("tracing_max_latency", 0644, d_tracer,
5048 &tracing_max_latency, &tracing_max_lat_fops);
5051 trace_create_file("tracing_thresh", 0644, d_tracer,
5052 &tracing_thresh, &tracing_max_lat_fops);
5054 trace_create_file("README", 0444, d_tracer,
5055 NULL, &tracing_readme_fops);
5057 trace_create_file("trace_pipe", 0444, d_tracer,
5058 (void *) RING_BUFFER_ALL_CPUS, &tracing_pipe_fops);
5060 trace_create_file("buffer_size_kb", 0644, d_tracer,
5061 (void *) RING_BUFFER_ALL_CPUS, &tracing_entries_fops);
5063 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
5064 &global_trace, &tracing_total_entries_fops);
5066 trace_create_file("free_buffer", 0644, d_tracer,
5067 &global_trace, &tracing_free_buffer_fops);
5069 trace_create_file("trace_marker", 0220, d_tracer,
5070 NULL, &tracing_mark_fops);
5072 trace_create_file("saved_cmdlines", 0444, d_tracer,
5073 NULL, &tracing_saved_cmdlines_fops);
5075 trace_create_file("trace_clock", 0644, d_tracer, NULL,
5078 trace_create_file("tracing_on", 0644, d_tracer,
5079 &global_trace, &rb_simple_fops);
5081 #ifdef CONFIG_DYNAMIC_FTRACE
5082 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
5083 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
5086 #ifdef CONFIG_TRACER_SNAPSHOT
5087 trace_create_file("snapshot", 0644, d_tracer,
5088 (void *) RING_BUFFER_ALL_CPUS, &snapshot_fops);
5091 create_trace_options_dir();
5093 for_each_tracing_cpu(cpu)
5094 tracing_init_debugfs_percpu(cpu);
5099 static int trace_panic_handler(struct notifier_block *this,
5100 unsigned long event, void *unused)
5102 if (ftrace_dump_on_oops)
5103 ftrace_dump(ftrace_dump_on_oops);
5107 static struct notifier_block trace_panic_notifier = {
5108 .notifier_call = trace_panic_handler,
5110 .priority = 150 /* priority: INT_MAX >= x >= 0 */
5113 static int trace_die_handler(struct notifier_block *self,
5119 if (ftrace_dump_on_oops)
5120 ftrace_dump(ftrace_dump_on_oops);
5128 static struct notifier_block trace_die_notifier = {
5129 .notifier_call = trace_die_handler,
5134 * printk is set to max of 1024, we really don't need it that big.
5135 * Nothing should be printing 1000 characters anyway.
5137 #define TRACE_MAX_PRINT 1000
5140 * Define here KERN_TRACE so that we have one place to modify
5141 * it if we decide to change what log level the ftrace dump
5144 #define KERN_TRACE KERN_EMERG
5147 trace_printk_seq(struct trace_seq *s)
5149 /* Probably should print a warning here. */
5153 /* should be zero ended, but we are paranoid. */
5154 s->buffer[s->len] = 0;
5156 printk(KERN_TRACE "%s", s->buffer);
5161 void trace_init_global_iter(struct trace_iterator *iter)
5163 iter->tr = &global_trace;
5164 iter->trace = current_trace;
5165 iter->cpu_file = RING_BUFFER_ALL_CPUS;
5169 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
5171 static arch_spinlock_t ftrace_dump_lock =
5172 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
5173 /* use static because iter can be a bit big for the stack */
5174 static struct trace_iterator iter;
5175 unsigned int old_userobj;
5176 static int dump_ran;
5177 unsigned long flags;
5181 local_irq_save(flags);
5182 arch_spin_lock(&ftrace_dump_lock);
5190 /* Did function tracer already get disabled? */
5191 if (ftrace_is_dead()) {
5192 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
5193 printk("# MAY BE MISSING FUNCTION EVENTS\n");
5196 if (disable_tracing)
5199 /* Simulate the iterator */
5200 trace_init_global_iter(&iter);
5202 for_each_tracing_cpu(cpu) {
5203 atomic_inc(&iter.tr->data[cpu]->disabled);
5206 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
5208 /* don't look at user memory in panic mode */
5209 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
5211 switch (oops_dump_mode) {
5213 iter.cpu_file = RING_BUFFER_ALL_CPUS;
5216 iter.cpu_file = raw_smp_processor_id();
5221 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
5222 iter.cpu_file = RING_BUFFER_ALL_CPUS;
5225 printk(KERN_TRACE "Dumping ftrace buffer:\n");
5228 * We need to stop all tracing on all CPUS to read the
5229 * the next buffer. This is a bit expensive, but is
5230 * not done often. We fill all what we can read,
5231 * and then release the locks again.
5234 while (!trace_empty(&iter)) {
5237 printk(KERN_TRACE "---------------------------------\n");
5241 /* reset all but tr, trace, and overruns */
5242 memset(&iter.seq, 0,
5243 sizeof(struct trace_iterator) -
5244 offsetof(struct trace_iterator, seq));
5245 iter.iter_flags |= TRACE_FILE_LAT_FMT;
5248 if (trace_find_next_entry_inc(&iter) != NULL) {
5251 ret = print_trace_line(&iter);
5252 if (ret != TRACE_TYPE_NO_CONSUME)
5253 trace_consume(&iter);
5255 touch_nmi_watchdog();
5257 trace_printk_seq(&iter.seq);
5261 printk(KERN_TRACE " (ftrace buffer empty)\n");
5263 printk(KERN_TRACE "---------------------------------\n");
5266 /* Re-enable tracing if requested */
5267 if (!disable_tracing) {
5268 trace_flags |= old_userobj;
5270 for_each_tracing_cpu(cpu) {
5271 atomic_dec(&iter.tr->data[cpu]->disabled);
5277 arch_spin_unlock(&ftrace_dump_lock);
5278 local_irq_restore(flags);
5281 /* By default: disable tracing after the dump */
5282 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
5284 __ftrace_dump(true, oops_dump_mode);
5286 EXPORT_SYMBOL_GPL(ftrace_dump);
5288 __init static int tracer_alloc_buffers(void)
5291 enum ring_buffer_flags rb_flags;
5296 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
5299 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
5300 goto out_free_buffer_mask;
5302 /* Only allocate trace_printk buffers if a trace_printk exists */
5303 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
5304 /* Must be called before global_trace.buffer is allocated */
5305 trace_printk_init_buffers();
5307 /* To save memory, keep the ring buffer size to its minimum */
5308 if (ring_buffer_expanded)
5309 ring_buf_size = trace_buf_size;
5313 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5315 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
5316 cpumask_copy(tracing_cpumask, cpu_all_mask);
5318 /* TODO: make the number of buffers hot pluggable with CPUS */
5319 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
5320 if (!global_trace.buffer) {
5321 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
5323 goto out_free_cpumask;
5325 if (global_trace.buffer_disabled)
5329 #ifdef CONFIG_TRACER_MAX_TRACE
5330 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
5331 if (!max_tr.buffer) {
5332 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
5334 ring_buffer_free(global_trace.buffer);
5335 goto out_free_cpumask;
5339 /* Allocate the first page for all buffers */
5340 for_each_tracing_cpu(i) {
5341 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
5342 max_tr.data[i] = &per_cpu(max_tr_data, i);
5345 set_buffer_entries(&global_trace,
5346 ring_buffer_size(global_trace.buffer, 0));
5347 #ifdef CONFIG_TRACER_MAX_TRACE
5348 set_buffer_entries(&max_tr, 1);
5351 trace_init_cmdlines();
5352 init_irq_work(&trace_work_wakeup, trace_wake_up);
5354 register_tracer(&nop_trace);
5356 /* All seems OK, enable tracing */
5357 tracing_disabled = 0;
5359 atomic_notifier_chain_register(&panic_notifier_list,
5360 &trace_panic_notifier);
5362 register_die_notifier(&trace_die_notifier);
5364 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
5366 INIT_LIST_HEAD(&global_trace.systems);
5367 INIT_LIST_HEAD(&global_trace.events);
5368 list_add(&global_trace.list, &ftrace_trace_arrays);
5370 while (trace_boot_options) {
5373 option = strsep(&trace_boot_options, ",");
5374 trace_set_options(option);
5380 free_cpumask_var(tracing_cpumask);
5381 out_free_buffer_mask:
5382 free_cpumask_var(tracing_buffer_mask);
5387 __init static int clear_boot_tracer(void)
5390 * The default tracer at boot buffer is an init section.
5391 * This function is called in lateinit. If we did not
5392 * find the boot tracer, then clear it out, to prevent
5393 * later registration from accessing the buffer that is
5394 * about to be freed.
5396 if (!default_bootup_tracer)
5399 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
5400 default_bootup_tracer);
5401 default_bootup_tracer = NULL;
5406 early_initcall(tracer_alloc_buffers);
5407 fs_initcall(tracer_init_debugfs);
5408 late_initcall(clear_boot_tracer);