2 * ring buffer based function tracer
4 * Copyright (C) 2007-2012 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;
255 * trace_types_lock is used to protect the trace_types list.
257 static DEFINE_MUTEX(trace_types_lock);
260 * serialize the access of the ring buffer
262 * ring buffer serializes readers, but it is low level protection.
263 * The validity of the events (which returns by ring_buffer_peek() ..etc)
264 * are not protected by ring buffer.
266 * The content of events may become garbage if we allow other process consumes
267 * these events concurrently:
268 * A) the page of the consumed events may become a normal page
269 * (not reader page) in ring buffer, and this page will be rewrited
270 * by events producer.
271 * B) The page of the consumed events may become a page for splice_read,
272 * and this page will be returned to system.
274 * These primitives allow multi process access to different cpu ring buffer
277 * These primitives don't distinguish read-only and read-consume access.
278 * Multi read-only access are also serialized.
282 static DECLARE_RWSEM(all_cpu_access_lock);
283 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
285 static inline void trace_access_lock(int cpu)
287 if (cpu == RING_BUFFER_ALL_CPUS) {
288 /* gain it for accessing the whole ring buffer. */
289 down_write(&all_cpu_access_lock);
291 /* gain it for accessing a cpu ring buffer. */
293 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
294 down_read(&all_cpu_access_lock);
296 /* Secondly block other access to this @cpu ring buffer. */
297 mutex_lock(&per_cpu(cpu_access_lock, cpu));
301 static inline void trace_access_unlock(int cpu)
303 if (cpu == RING_BUFFER_ALL_CPUS) {
304 up_write(&all_cpu_access_lock);
306 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
307 up_read(&all_cpu_access_lock);
311 static inline void trace_access_lock_init(void)
315 for_each_possible_cpu(cpu)
316 mutex_init(&per_cpu(cpu_access_lock, cpu));
321 static DEFINE_MUTEX(access_lock);
323 static inline void trace_access_lock(int cpu)
326 mutex_lock(&access_lock);
329 static inline void trace_access_unlock(int cpu)
332 mutex_unlock(&access_lock);
335 static inline void trace_access_lock_init(void)
341 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
342 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
344 /* trace_flags holds trace_options default values */
345 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
346 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
347 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
348 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS;
351 * trace_wake_up - wake up tasks waiting for trace input
353 * Schedules a delayed work to wake up any task that is blocked on the
354 * trace_wait queue. These is used with trace_poll for tasks polling the
357 static void trace_wake_up(struct irq_work *work)
359 wake_up_all(&trace_wait);
364 * tracing_on - enable tracing buffers
366 * This function enables tracing buffers that may have been
367 * disabled with tracing_off.
369 void tracing_on(void)
371 if (global_trace.buffer)
372 ring_buffer_record_on(global_trace.buffer);
374 * This flag is only looked at when buffers haven't been
375 * allocated yet. We don't really care about the race
376 * between setting this flag and actually turning
379 global_trace.buffer_disabled = 0;
381 EXPORT_SYMBOL_GPL(tracing_on);
384 * tracing_off - turn off tracing buffers
386 * This function stops the tracing buffers from recording data.
387 * It does not disable any overhead the tracers themselves may
388 * be causing. This function simply causes all recording to
389 * the ring buffers to fail.
391 void tracing_off(void)
393 if (global_trace.buffer)
394 ring_buffer_record_off(global_trace.buffer);
396 * This flag is only looked at when buffers haven't been
397 * allocated yet. We don't really care about the race
398 * between setting this flag and actually turning
401 global_trace.buffer_disabled = 1;
403 EXPORT_SYMBOL_GPL(tracing_off);
406 * tracing_is_on - show state of ring buffers enabled
408 int tracing_is_on(void)
410 if (global_trace.buffer)
411 return ring_buffer_record_is_on(global_trace.buffer);
412 return !global_trace.buffer_disabled;
414 EXPORT_SYMBOL_GPL(tracing_is_on);
416 static int __init set_buf_size(char *str)
418 unsigned long buf_size;
422 buf_size = memparse(str, &str);
423 /* nr_entries can not be zero */
426 trace_buf_size = buf_size;
429 __setup("trace_buf_size=", set_buf_size);
431 static int __init set_tracing_thresh(char *str)
433 unsigned long threshold;
438 ret = kstrtoul(str, 0, &threshold);
441 tracing_thresh = threshold * 1000;
444 __setup("tracing_thresh=", set_tracing_thresh);
446 unsigned long nsecs_to_usecs(unsigned long nsecs)
451 /* These must match the bit postions in trace_iterator_flags */
452 static const char *trace_options[] = {
484 int in_ns; /* is this clock in nanoseconds? */
486 { trace_clock_local, "local", 1 },
487 { trace_clock_global, "global", 1 },
488 { trace_clock_counter, "counter", 0 },
495 * trace_parser_get_init - gets the buffer for trace parser
497 int trace_parser_get_init(struct trace_parser *parser, int size)
499 memset(parser, 0, sizeof(*parser));
501 parser->buffer = kmalloc(size, GFP_KERNEL);
510 * trace_parser_put - frees the buffer for trace parser
512 void trace_parser_put(struct trace_parser *parser)
514 kfree(parser->buffer);
518 * trace_get_user - reads the user input string separated by space
519 * (matched by isspace(ch))
521 * For each string found the 'struct trace_parser' is updated,
522 * and the function returns.
524 * Returns number of bytes read.
526 * See kernel/trace/trace.h for 'struct trace_parser' details.
528 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
529 size_t cnt, loff_t *ppos)
536 trace_parser_clear(parser);
538 ret = get_user(ch, ubuf++);
546 * The parser is not finished with the last write,
547 * continue reading the user input without skipping spaces.
550 /* skip white space */
551 while (cnt && isspace(ch)) {
552 ret = get_user(ch, ubuf++);
559 /* only spaces were written */
569 /* read the non-space input */
570 while (cnt && !isspace(ch)) {
571 if (parser->idx < parser->size - 1)
572 parser->buffer[parser->idx++] = ch;
577 ret = get_user(ch, ubuf++);
584 /* We either got finished input or we have to wait for another call. */
586 parser->buffer[parser->idx] = 0;
587 parser->cont = false;
590 parser->buffer[parser->idx++] = ch;
600 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
608 if (s->len <= s->readpos)
611 len = s->len - s->readpos;
614 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
624 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
628 if (s->len <= s->readpos)
631 len = s->len - s->readpos;
634 memcpy(buf, s->buffer + s->readpos, cnt);
641 * ftrace_max_lock is used to protect the swapping of buffers
642 * when taking a max snapshot. The buffers themselves are
643 * protected by per_cpu spinlocks. But the action of the swap
644 * needs its own lock.
646 * This is defined as a arch_spinlock_t in order to help
647 * with performance when lockdep debugging is enabled.
649 * It is also used in other places outside the update_max_tr
650 * so it needs to be defined outside of the
651 * CONFIG_TRACER_MAX_TRACE.
653 static arch_spinlock_t ftrace_max_lock =
654 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
656 unsigned long __read_mostly tracing_thresh;
658 #ifdef CONFIG_TRACER_MAX_TRACE
659 unsigned long __read_mostly tracing_max_latency;
662 * Copy the new maximum trace into the separate maximum-trace
663 * structure. (this way the maximum trace is permanently saved,
664 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
667 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
669 struct trace_array_cpu *data = tr->data[cpu];
670 struct trace_array_cpu *max_data;
673 max_tr.time_start = data->preempt_timestamp;
675 max_data = max_tr.data[cpu];
676 max_data->saved_latency = tracing_max_latency;
677 max_data->critical_start = data->critical_start;
678 max_data->critical_end = data->critical_end;
680 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
681 max_data->pid = tsk->pid;
682 max_data->uid = task_uid(tsk);
683 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
684 max_data->policy = tsk->policy;
685 max_data->rt_priority = tsk->rt_priority;
687 /* record this tasks comm */
688 tracing_record_cmdline(tsk);
692 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
694 * @tsk: the task with the latency
695 * @cpu: The cpu that initiated the trace.
697 * Flip the buffers between the @tr and the max_tr and record information
698 * about which task was the cause of this latency.
701 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
703 struct ring_buffer *buf;
708 WARN_ON_ONCE(!irqs_disabled());
710 if (!tr->current_trace->allocated_snapshot) {
711 /* Only the nop tracer should hit this when disabling */
712 WARN_ON_ONCE(tr->current_trace != &nop_trace);
716 arch_spin_lock(&ftrace_max_lock);
719 tr->buffer = max_tr.buffer;
722 __update_max_tr(tr, tsk, cpu);
723 arch_spin_unlock(&ftrace_max_lock);
727 * update_max_tr_single - only copy one trace over, and reset the rest
729 * @tsk - task with the latency
730 * @cpu - the cpu of the buffer to copy.
732 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
735 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
742 WARN_ON_ONCE(!irqs_disabled());
743 if (WARN_ON_ONCE(!tr->current_trace->allocated_snapshot))
746 arch_spin_lock(&ftrace_max_lock);
748 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
752 * We failed to swap the buffer due to a commit taking
753 * place on this CPU. We fail to record, but we reset
754 * the max trace buffer (no one writes directly to it)
755 * and flag that it failed.
757 trace_array_printk(&max_tr, _THIS_IP_,
758 "Failed to swap buffers due to commit in progress\n");
761 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
763 __update_max_tr(tr, tsk, cpu);
764 arch_spin_unlock(&ftrace_max_lock);
766 #endif /* CONFIG_TRACER_MAX_TRACE */
768 static void default_wait_pipe(struct trace_iterator *iter)
772 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
775 * The events can happen in critical sections where
776 * checking a work queue can cause deadlocks.
777 * After adding a task to the queue, this flag is set
778 * only to notify events to try to wake up the queue
781 * We don't clear it even if the buffer is no longer
782 * empty. The flag only causes the next event to run
783 * irq_work to do the work queue wake up. The worse
784 * that can happen if we race with !trace_empty() is that
785 * an event will cause an irq_work to try to wake up
788 * There's no reason to protect this flag either, as
789 * the work queue and irq_work logic will do the necessary
790 * synchronization for the wake ups. The only thing
791 * that is necessary is that the wake up happens after
792 * a task has been queued. It's OK for spurious wake ups.
794 trace_wakeup_needed = true;
796 if (trace_empty(iter))
799 finish_wait(&trace_wait, &wait);
803 * register_tracer - register a tracer with the ftrace system.
804 * @type - the plugin for the tracer
806 * Register a new plugin tracer.
808 int register_tracer(struct tracer *type)
814 pr_info("Tracer must have a name\n");
818 if (strlen(type->name) >= MAX_TRACER_SIZE) {
819 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
823 mutex_lock(&trace_types_lock);
825 tracing_selftest_running = true;
827 for (t = trace_types; t; t = t->next) {
828 if (strcmp(type->name, t->name) == 0) {
830 pr_info("Tracer %s already registered\n",
838 type->set_flag = &dummy_set_flag;
840 type->flags = &dummy_tracer_flags;
842 if (!type->flags->opts)
843 type->flags->opts = dummy_tracer_opt;
844 if (!type->wait_pipe)
845 type->wait_pipe = default_wait_pipe;
848 #ifdef CONFIG_FTRACE_STARTUP_TEST
849 if (type->selftest && !tracing_selftest_disabled) {
850 struct trace_array *tr = &global_trace;
851 struct tracer *saved_tracer = tr->current_trace;
854 * Run a selftest on this tracer.
855 * Here we reset the trace buffer, and set the current
856 * tracer to be this tracer. The tracer can then run some
857 * internal tracing to verify that everything is in order.
858 * If we fail, we do not register this tracer.
860 tracing_reset_online_cpus(tr);
862 tr->current_trace = type;
864 if (type->use_max_tr) {
865 /* If we expanded the buffers, make sure the max is expanded too */
866 if (ring_buffer_expanded)
867 ring_buffer_resize(max_tr.buffer, trace_buf_size,
868 RING_BUFFER_ALL_CPUS);
869 type->allocated_snapshot = true;
872 /* the test is responsible for initializing and enabling */
873 pr_info("Testing tracer %s: ", type->name);
874 ret = type->selftest(type, tr);
875 /* the test is responsible for resetting too */
876 tr->current_trace = saved_tracer;
878 printk(KERN_CONT "FAILED!\n");
879 /* Add the warning after printing 'FAILED' */
883 /* Only reset on passing, to avoid touching corrupted buffers */
884 tracing_reset_online_cpus(tr);
886 if (type->use_max_tr) {
887 type->allocated_snapshot = false;
889 /* Shrink the max buffer again */
890 if (ring_buffer_expanded)
891 ring_buffer_resize(max_tr.buffer, 1,
892 RING_BUFFER_ALL_CPUS);
895 printk(KERN_CONT "PASSED\n");
899 type->next = trace_types;
903 tracing_selftest_running = false;
904 mutex_unlock(&trace_types_lock);
906 if (ret || !default_bootup_tracer)
909 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
912 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
913 /* Do we want this tracer to start on bootup? */
914 tracing_set_tracer(type->name);
915 default_bootup_tracer = NULL;
916 /* disable other selftests, since this will break it. */
917 tracing_selftest_disabled = 1;
918 #ifdef CONFIG_FTRACE_STARTUP_TEST
919 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
927 void tracing_reset(struct trace_array *tr, int cpu)
929 struct ring_buffer *buffer = tr->buffer;
934 ring_buffer_record_disable(buffer);
936 /* Make sure all commits have finished */
938 ring_buffer_reset_cpu(buffer, cpu);
940 ring_buffer_record_enable(buffer);
943 void tracing_reset_online_cpus(struct trace_array *tr)
945 struct ring_buffer *buffer = tr->buffer;
951 ring_buffer_record_disable(buffer);
953 /* Make sure all commits have finished */
956 tr->time_start = ftrace_now(tr->cpu);
958 for_each_online_cpu(cpu)
959 ring_buffer_reset_cpu(buffer, cpu);
961 ring_buffer_record_enable(buffer);
964 void tracing_reset_current(int cpu)
966 tracing_reset(&global_trace, cpu);
969 void tracing_reset_current_online_cpus(void)
971 tracing_reset_online_cpus(&global_trace);
974 #define SAVED_CMDLINES 128
975 #define NO_CMDLINE_MAP UINT_MAX
976 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
977 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
978 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
979 static int cmdline_idx;
980 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
982 /* temporary disable recording */
983 static atomic_t trace_record_cmdline_disabled __read_mostly;
985 static void trace_init_cmdlines(void)
987 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
988 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
992 int is_tracing_stopped(void)
994 return global_trace.stop_count;
998 * ftrace_off_permanent - disable all ftrace code permanently
1000 * This should only be called when a serious anomally has
1001 * been detected. This will turn off the function tracing,
1002 * ring buffers, and other tracing utilites. It takes no
1003 * locks and can be called from any context.
1005 void ftrace_off_permanent(void)
1007 tracing_disabled = 1;
1009 tracing_off_permanent();
1013 * tracing_start - quick start of the tracer
1015 * If tracing is enabled but was stopped by tracing_stop,
1016 * this will start the tracer back up.
1018 void tracing_start(void)
1020 struct ring_buffer *buffer;
1021 unsigned long flags;
1023 if (tracing_disabled)
1026 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1027 if (--global_trace.stop_count) {
1028 if (global_trace.stop_count < 0) {
1029 /* Someone screwed up their debugging */
1031 global_trace.stop_count = 0;
1036 /* Prevent the buffers from switching */
1037 arch_spin_lock(&ftrace_max_lock);
1039 buffer = global_trace.buffer;
1041 ring_buffer_record_enable(buffer);
1043 buffer = max_tr.buffer;
1045 ring_buffer_record_enable(buffer);
1047 arch_spin_unlock(&ftrace_max_lock);
1051 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1054 static void tracing_start_tr(struct trace_array *tr)
1056 struct ring_buffer *buffer;
1057 unsigned long flags;
1059 if (tracing_disabled)
1062 /* If global, we need to also start the max tracer */
1063 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1064 return tracing_start();
1066 raw_spin_lock_irqsave(&tr->start_lock, flags);
1068 if (--tr->stop_count) {
1069 if (tr->stop_count < 0) {
1070 /* Someone screwed up their debugging */
1077 buffer = tr->buffer;
1079 ring_buffer_record_enable(buffer);
1082 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1086 * tracing_stop - quick stop of the tracer
1088 * Light weight way to stop tracing. Use in conjunction with
1091 void tracing_stop(void)
1093 struct ring_buffer *buffer;
1094 unsigned long flags;
1097 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1098 if (global_trace.stop_count++)
1101 /* Prevent the buffers from switching */
1102 arch_spin_lock(&ftrace_max_lock);
1104 buffer = global_trace.buffer;
1106 ring_buffer_record_disable(buffer);
1108 buffer = max_tr.buffer;
1110 ring_buffer_record_disable(buffer);
1112 arch_spin_unlock(&ftrace_max_lock);
1115 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1118 static void tracing_stop_tr(struct trace_array *tr)
1120 struct ring_buffer *buffer;
1121 unsigned long flags;
1123 /* If global, we need to also stop the max tracer */
1124 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1125 return tracing_stop();
1127 raw_spin_lock_irqsave(&tr->start_lock, flags);
1128 if (tr->stop_count++)
1131 buffer = tr->buffer;
1133 ring_buffer_record_disable(buffer);
1136 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1139 void trace_stop_cmdline_recording(void);
1141 static void trace_save_cmdline(struct task_struct *tsk)
1145 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1149 * It's not the end of the world if we don't get
1150 * the lock, but we also don't want to spin
1151 * nor do we want to disable interrupts,
1152 * so if we miss here, then better luck next time.
1154 if (!arch_spin_trylock(&trace_cmdline_lock))
1157 idx = map_pid_to_cmdline[tsk->pid];
1158 if (idx == NO_CMDLINE_MAP) {
1159 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1162 * Check whether the cmdline buffer at idx has a pid
1163 * mapped. We are going to overwrite that entry so we
1164 * need to clear the map_pid_to_cmdline. Otherwise we
1165 * would read the new comm for the old pid.
1167 pid = map_cmdline_to_pid[idx];
1168 if (pid != NO_CMDLINE_MAP)
1169 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1171 map_cmdline_to_pid[idx] = tsk->pid;
1172 map_pid_to_cmdline[tsk->pid] = idx;
1177 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1179 arch_spin_unlock(&trace_cmdline_lock);
1182 void trace_find_cmdline(int pid, char comm[])
1187 strcpy(comm, "<idle>");
1191 if (WARN_ON_ONCE(pid < 0)) {
1192 strcpy(comm, "<XXX>");
1196 if (pid > PID_MAX_DEFAULT) {
1197 strcpy(comm, "<...>");
1202 arch_spin_lock(&trace_cmdline_lock);
1203 map = map_pid_to_cmdline[pid];
1204 if (map != NO_CMDLINE_MAP)
1205 strcpy(comm, saved_cmdlines[map]);
1207 strcpy(comm, "<...>");
1209 arch_spin_unlock(&trace_cmdline_lock);
1213 void tracing_record_cmdline(struct task_struct *tsk)
1215 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1218 if (!__this_cpu_read(trace_cmdline_save))
1221 __this_cpu_write(trace_cmdline_save, false);
1223 trace_save_cmdline(tsk);
1227 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1230 struct task_struct *tsk = current;
1232 entry->preempt_count = pc & 0xff;
1233 entry->pid = (tsk) ? tsk->pid : 0;
1235 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1236 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1238 TRACE_FLAG_IRQS_NOSUPPORT |
1240 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1241 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1242 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1244 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1246 struct ring_buffer_event *
1247 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1250 unsigned long flags, int pc)
1252 struct ring_buffer_event *event;
1254 event = ring_buffer_lock_reserve(buffer, len);
1255 if (event != NULL) {
1256 struct trace_entry *ent = ring_buffer_event_data(event);
1258 tracing_generic_entry_update(ent, flags, pc);
1266 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1268 __this_cpu_write(trace_cmdline_save, true);
1269 if (trace_wakeup_needed) {
1270 trace_wakeup_needed = false;
1271 /* irq_work_queue() supplies it's own memory barriers */
1272 irq_work_queue(&trace_work_wakeup);
1274 ring_buffer_unlock_commit(buffer, event);
1278 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1279 struct ring_buffer_event *event,
1280 unsigned long flags, int pc)
1282 __buffer_unlock_commit(buffer, event);
1284 ftrace_trace_stack(buffer, flags, 6, pc);
1285 ftrace_trace_userstack(buffer, flags, pc);
1288 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1289 struct ring_buffer_event *event,
1290 unsigned long flags, int pc)
1292 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1294 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1296 struct ring_buffer_event *
1297 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1298 struct ftrace_event_file *ftrace_file,
1299 int type, unsigned long len,
1300 unsigned long flags, int pc)
1302 *current_rb = ftrace_file->tr->buffer;
1303 return trace_buffer_lock_reserve(*current_rb,
1304 type, len, flags, pc);
1306 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1308 struct ring_buffer_event *
1309 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1310 int type, unsigned long len,
1311 unsigned long flags, int pc)
1313 *current_rb = global_trace.buffer;
1314 return trace_buffer_lock_reserve(*current_rb,
1315 type, len, flags, pc);
1317 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1319 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1320 struct ring_buffer_event *event,
1321 unsigned long flags, int pc)
1323 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1325 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1327 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1328 struct ring_buffer_event *event,
1329 unsigned long flags, int pc,
1330 struct pt_regs *regs)
1332 __buffer_unlock_commit(buffer, event);
1334 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1335 ftrace_trace_userstack(buffer, flags, pc);
1337 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1339 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1340 struct ring_buffer_event *event)
1342 ring_buffer_discard_commit(buffer, event);
1344 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1347 trace_function(struct trace_array *tr,
1348 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1351 struct ftrace_event_call *call = &event_function;
1352 struct ring_buffer *buffer = tr->buffer;
1353 struct ring_buffer_event *event;
1354 struct ftrace_entry *entry;
1356 /* If we are reading the ring buffer, don't trace */
1357 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1360 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1364 entry = ring_buffer_event_data(event);
1366 entry->parent_ip = parent_ip;
1368 if (!filter_check_discard(call, entry, buffer, event))
1369 __buffer_unlock_commit(buffer, event);
1373 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1374 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1377 if (likely(!atomic_read(&data->disabled)))
1378 trace_function(tr, ip, parent_ip, flags, pc);
1381 #ifdef CONFIG_STACKTRACE
1383 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1384 struct ftrace_stack {
1385 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1388 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1389 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1391 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1392 unsigned long flags,
1393 int skip, int pc, struct pt_regs *regs)
1395 struct ftrace_event_call *call = &event_kernel_stack;
1396 struct ring_buffer_event *event;
1397 struct stack_entry *entry;
1398 struct stack_trace trace;
1400 int size = FTRACE_STACK_ENTRIES;
1402 trace.nr_entries = 0;
1406 * Since events can happen in NMIs there's no safe way to
1407 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1408 * or NMI comes in, it will just have to use the default
1409 * FTRACE_STACK_SIZE.
1411 preempt_disable_notrace();
1413 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1415 * We don't need any atomic variables, just a barrier.
1416 * If an interrupt comes in, we don't care, because it would
1417 * have exited and put the counter back to what we want.
1418 * We just need a barrier to keep gcc from moving things
1422 if (use_stack == 1) {
1423 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1424 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1427 save_stack_trace_regs(regs, &trace);
1429 save_stack_trace(&trace);
1431 if (trace.nr_entries > size)
1432 size = trace.nr_entries;
1434 /* From now on, use_stack is a boolean */
1437 size *= sizeof(unsigned long);
1439 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1440 sizeof(*entry) + size, flags, pc);
1443 entry = ring_buffer_event_data(event);
1445 memset(&entry->caller, 0, size);
1448 memcpy(&entry->caller, trace.entries,
1449 trace.nr_entries * sizeof(unsigned long));
1451 trace.max_entries = FTRACE_STACK_ENTRIES;
1452 trace.entries = entry->caller;
1454 save_stack_trace_regs(regs, &trace);
1456 save_stack_trace(&trace);
1459 entry->size = trace.nr_entries;
1461 if (!filter_check_discard(call, entry, buffer, event))
1462 __buffer_unlock_commit(buffer, event);
1465 /* Again, don't let gcc optimize things here */
1467 __this_cpu_dec(ftrace_stack_reserve);
1468 preempt_enable_notrace();
1472 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1473 int skip, int pc, struct pt_regs *regs)
1475 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1478 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1481 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1484 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1487 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1490 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1493 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
1497 * trace_dump_stack - record a stack back trace in the trace buffer
1499 void trace_dump_stack(void)
1501 unsigned long flags;
1503 if (tracing_disabled || tracing_selftest_running)
1506 local_save_flags(flags);
1508 /* skipping 3 traces, seems to get us at the caller of this function */
1509 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
1512 static DEFINE_PER_CPU(int, user_stack_count);
1515 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1517 struct ftrace_event_call *call = &event_user_stack;
1518 struct ring_buffer_event *event;
1519 struct userstack_entry *entry;
1520 struct stack_trace trace;
1522 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1526 * NMIs can not handle page faults, even with fix ups.
1527 * The save user stack can (and often does) fault.
1529 if (unlikely(in_nmi()))
1533 * prevent recursion, since the user stack tracing may
1534 * trigger other kernel events.
1537 if (__this_cpu_read(user_stack_count))
1540 __this_cpu_inc(user_stack_count);
1542 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1543 sizeof(*entry), flags, pc);
1545 goto out_drop_count;
1546 entry = ring_buffer_event_data(event);
1548 entry->tgid = current->tgid;
1549 memset(&entry->caller, 0, sizeof(entry->caller));
1551 trace.nr_entries = 0;
1552 trace.max_entries = FTRACE_STACK_ENTRIES;
1554 trace.entries = entry->caller;
1556 save_stack_trace_user(&trace);
1557 if (!filter_check_discard(call, entry, buffer, event))
1558 __buffer_unlock_commit(buffer, event);
1561 __this_cpu_dec(user_stack_count);
1567 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1569 ftrace_trace_userstack(tr, flags, preempt_count());
1573 #endif /* CONFIG_STACKTRACE */
1575 /* created for use with alloc_percpu */
1576 struct trace_buffer_struct {
1577 char buffer[TRACE_BUF_SIZE];
1580 static struct trace_buffer_struct *trace_percpu_buffer;
1581 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1582 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1583 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1586 * The buffer used is dependent on the context. There is a per cpu
1587 * buffer for normal context, softirq contex, hard irq context and
1588 * for NMI context. Thise allows for lockless recording.
1590 * Note, if the buffers failed to be allocated, then this returns NULL
1592 static char *get_trace_buf(void)
1594 struct trace_buffer_struct *percpu_buffer;
1597 * If we have allocated per cpu buffers, then we do not
1598 * need to do any locking.
1601 percpu_buffer = trace_percpu_nmi_buffer;
1603 percpu_buffer = trace_percpu_irq_buffer;
1604 else if (in_softirq())
1605 percpu_buffer = trace_percpu_sirq_buffer;
1607 percpu_buffer = trace_percpu_buffer;
1612 return this_cpu_ptr(&percpu_buffer->buffer[0]);
1615 static int alloc_percpu_trace_buffer(void)
1617 struct trace_buffer_struct *buffers;
1618 struct trace_buffer_struct *sirq_buffers;
1619 struct trace_buffer_struct *irq_buffers;
1620 struct trace_buffer_struct *nmi_buffers;
1622 buffers = alloc_percpu(struct trace_buffer_struct);
1626 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1630 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1634 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1638 trace_percpu_buffer = buffers;
1639 trace_percpu_sirq_buffer = sirq_buffers;
1640 trace_percpu_irq_buffer = irq_buffers;
1641 trace_percpu_nmi_buffer = nmi_buffers;
1646 free_percpu(irq_buffers);
1648 free_percpu(sirq_buffers);
1650 free_percpu(buffers);
1652 WARN(1, "Could not allocate percpu trace_printk buffer");
1656 static int buffers_allocated;
1658 void trace_printk_init_buffers(void)
1660 if (buffers_allocated)
1663 if (alloc_percpu_trace_buffer())
1666 pr_info("ftrace: Allocated trace_printk buffers\n");
1668 /* Expand the buffers to set size */
1669 tracing_update_buffers();
1671 buffers_allocated = 1;
1674 * trace_printk_init_buffers() can be called by modules.
1675 * If that happens, then we need to start cmdline recording
1676 * directly here. If the global_trace.buffer is already
1677 * allocated here, then this was called by module code.
1679 if (global_trace.buffer)
1680 tracing_start_cmdline_record();
1683 void trace_printk_start_comm(void)
1685 /* Start tracing comms if trace printk is set */
1686 if (!buffers_allocated)
1688 tracing_start_cmdline_record();
1691 static void trace_printk_start_stop_comm(int enabled)
1693 if (!buffers_allocated)
1697 tracing_start_cmdline_record();
1699 tracing_stop_cmdline_record();
1703 * trace_vbprintk - write binary msg to tracing buffer
1706 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1708 struct ftrace_event_call *call = &event_bprint;
1709 struct ring_buffer_event *event;
1710 struct ring_buffer *buffer;
1711 struct trace_array *tr = &global_trace;
1712 struct bprint_entry *entry;
1713 unsigned long flags;
1715 int len = 0, size, pc;
1717 if (unlikely(tracing_selftest_running || tracing_disabled))
1720 /* Don't pollute graph traces with trace_vprintk internals */
1721 pause_graph_tracing();
1723 pc = preempt_count();
1724 preempt_disable_notrace();
1726 tbuffer = get_trace_buf();
1732 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1734 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1737 local_save_flags(flags);
1738 size = sizeof(*entry) + sizeof(u32) * len;
1739 buffer = tr->buffer;
1740 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1744 entry = ring_buffer_event_data(event);
1748 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1749 if (!filter_check_discard(call, entry, buffer, event)) {
1750 __buffer_unlock_commit(buffer, event);
1751 ftrace_trace_stack(buffer, flags, 6, pc);
1755 preempt_enable_notrace();
1756 unpause_graph_tracing();
1760 EXPORT_SYMBOL_GPL(trace_vbprintk);
1762 int trace_array_printk(struct trace_array *tr,
1763 unsigned long ip, const char *fmt, ...)
1768 if (!(trace_flags & TRACE_ITER_PRINTK))
1772 ret = trace_array_vprintk(tr, ip, fmt, ap);
1777 int trace_array_vprintk(struct trace_array *tr,
1778 unsigned long ip, const char *fmt, va_list args)
1780 struct ftrace_event_call *call = &event_print;
1781 struct ring_buffer_event *event;
1782 struct ring_buffer *buffer;
1783 int len = 0, size, pc;
1784 struct print_entry *entry;
1785 unsigned long flags;
1788 if (tracing_disabled || tracing_selftest_running)
1791 /* Don't pollute graph traces with trace_vprintk internals */
1792 pause_graph_tracing();
1794 pc = preempt_count();
1795 preempt_disable_notrace();
1798 tbuffer = get_trace_buf();
1804 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1805 if (len > TRACE_BUF_SIZE)
1808 local_save_flags(flags);
1809 size = sizeof(*entry) + len + 1;
1810 buffer = tr->buffer;
1811 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1815 entry = ring_buffer_event_data(event);
1818 memcpy(&entry->buf, tbuffer, len);
1819 entry->buf[len] = '\0';
1820 if (!filter_check_discard(call, entry, buffer, event)) {
1821 __buffer_unlock_commit(buffer, event);
1822 ftrace_trace_stack(buffer, flags, 6, pc);
1825 preempt_enable_notrace();
1826 unpause_graph_tracing();
1831 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1833 return trace_array_vprintk(&global_trace, ip, fmt, args);
1835 EXPORT_SYMBOL_GPL(trace_vprintk);
1837 static void trace_iterator_increment(struct trace_iterator *iter)
1839 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
1843 ring_buffer_read(buf_iter, NULL);
1846 static struct trace_entry *
1847 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1848 unsigned long *lost_events)
1850 struct ring_buffer_event *event;
1851 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
1854 event = ring_buffer_iter_peek(buf_iter, ts);
1856 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1860 iter->ent_size = ring_buffer_event_length(event);
1861 return ring_buffer_event_data(event);
1867 static struct trace_entry *
1868 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1869 unsigned long *missing_events, u64 *ent_ts)
1871 struct ring_buffer *buffer = iter->tr->buffer;
1872 struct trace_entry *ent, *next = NULL;
1873 unsigned long lost_events = 0, next_lost = 0;
1874 int cpu_file = iter->cpu_file;
1875 u64 next_ts = 0, ts;
1881 * If we are in a per_cpu trace file, don't bother by iterating over
1882 * all cpu and peek directly.
1884 if (cpu_file > RING_BUFFER_ALL_CPUS) {
1885 if (ring_buffer_empty_cpu(buffer, cpu_file))
1887 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1889 *ent_cpu = cpu_file;
1894 for_each_tracing_cpu(cpu) {
1896 if (ring_buffer_empty_cpu(buffer, cpu))
1899 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1902 * Pick the entry with the smallest timestamp:
1904 if (ent && (!next || ts < next_ts)) {
1908 next_lost = lost_events;
1909 next_size = iter->ent_size;
1913 iter->ent_size = next_size;
1916 *ent_cpu = next_cpu;
1922 *missing_events = next_lost;
1927 /* Find the next real entry, without updating the iterator itself */
1928 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1929 int *ent_cpu, u64 *ent_ts)
1931 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1934 /* Find the next real entry, and increment the iterator to the next entry */
1935 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1937 iter->ent = __find_next_entry(iter, &iter->cpu,
1938 &iter->lost_events, &iter->ts);
1941 trace_iterator_increment(iter);
1943 return iter->ent ? iter : NULL;
1946 static void trace_consume(struct trace_iterator *iter)
1948 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1949 &iter->lost_events);
1952 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1954 struct trace_iterator *iter = m->private;
1958 WARN_ON_ONCE(iter->leftover);
1962 /* can't go backwards */
1967 ent = trace_find_next_entry_inc(iter);
1971 while (ent && iter->idx < i)
1972 ent = trace_find_next_entry_inc(iter);
1979 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1981 struct trace_array *tr = iter->tr;
1982 struct ring_buffer_event *event;
1983 struct ring_buffer_iter *buf_iter;
1984 unsigned long entries = 0;
1987 tr->data[cpu]->skipped_entries = 0;
1989 buf_iter = trace_buffer_iter(iter, cpu);
1993 ring_buffer_iter_reset(buf_iter);
1996 * We could have the case with the max latency tracers
1997 * that a reset never took place on a cpu. This is evident
1998 * by the timestamp being before the start of the buffer.
2000 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
2001 if (ts >= iter->tr->time_start)
2004 ring_buffer_read(buf_iter, NULL);
2007 tr->data[cpu]->skipped_entries = entries;
2011 * The current tracer is copied to avoid a global locking
2014 static void *s_start(struct seq_file *m, loff_t *pos)
2016 struct trace_iterator *iter = m->private;
2017 struct trace_array *tr = iter->tr;
2018 int cpu_file = iter->cpu_file;
2024 * copy the tracer to avoid using a global lock all around.
2025 * iter->trace is a copy of current_trace, the pointer to the
2026 * name may be used instead of a strcmp(), as iter->trace->name
2027 * will point to the same string as current_trace->name.
2029 mutex_lock(&trace_types_lock);
2030 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2031 *iter->trace = *tr->current_trace;
2032 mutex_unlock(&trace_types_lock);
2034 if (iter->snapshot && iter->trace->use_max_tr)
2035 return ERR_PTR(-EBUSY);
2037 if (!iter->snapshot)
2038 atomic_inc(&trace_record_cmdline_disabled);
2040 if (*pos != iter->pos) {
2045 if (cpu_file == RING_BUFFER_ALL_CPUS) {
2046 for_each_tracing_cpu(cpu)
2047 tracing_iter_reset(iter, cpu);
2049 tracing_iter_reset(iter, cpu_file);
2052 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2057 * If we overflowed the seq_file before, then we want
2058 * to just reuse the trace_seq buffer again.
2064 p = s_next(m, p, &l);
2068 trace_event_read_lock();
2069 trace_access_lock(cpu_file);
2073 static void s_stop(struct seq_file *m, void *p)
2075 struct trace_iterator *iter = m->private;
2077 if (iter->snapshot && iter->trace->use_max_tr)
2080 if (!iter->snapshot)
2081 atomic_dec(&trace_record_cmdline_disabled);
2082 trace_access_unlock(iter->cpu_file);
2083 trace_event_read_unlock();
2087 get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *entries)
2089 unsigned long count;
2095 for_each_tracing_cpu(cpu) {
2096 count = ring_buffer_entries_cpu(tr->buffer, cpu);
2098 * If this buffer has skipped entries, then we hold all
2099 * entries for the trace and we need to ignore the
2100 * ones before the time stamp.
2102 if (tr->data[cpu]->skipped_entries) {
2103 count -= tr->data[cpu]->skipped_entries;
2104 /* total is the same as the entries */
2108 ring_buffer_overrun_cpu(tr->buffer, cpu);
2113 static void print_lat_help_header(struct seq_file *m)
2115 seq_puts(m, "# _------=> CPU# \n");
2116 seq_puts(m, "# / _-----=> irqs-off \n");
2117 seq_puts(m, "# | / _----=> need-resched \n");
2118 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2119 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2120 seq_puts(m, "# |||| / delay \n");
2121 seq_puts(m, "# cmd pid ||||| time | caller \n");
2122 seq_puts(m, "# \\ / ||||| \\ | / \n");
2125 static void print_event_info(struct trace_array *tr, struct seq_file *m)
2127 unsigned long total;
2128 unsigned long entries;
2130 get_total_entries(tr, &total, &entries);
2131 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2132 entries, total, num_online_cpus());
2136 static void print_func_help_header(struct trace_array *tr, struct seq_file *m)
2138 print_event_info(tr, m);
2139 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2140 seq_puts(m, "# | | | | |\n");
2143 static void print_func_help_header_irq(struct trace_array *tr, struct seq_file *m)
2145 print_event_info(tr, m);
2146 seq_puts(m, "# _-----=> irqs-off\n");
2147 seq_puts(m, "# / _----=> need-resched\n");
2148 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2149 seq_puts(m, "# || / _--=> preempt-depth\n");
2150 seq_puts(m, "# ||| / delay\n");
2151 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2152 seq_puts(m, "# | | | |||| | |\n");
2156 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2158 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2159 struct trace_array *tr = iter->tr;
2160 struct trace_array_cpu *data = tr->data[tr->cpu];
2161 struct tracer *type = iter->trace;
2162 unsigned long entries;
2163 unsigned long total;
2164 const char *name = "preemption";
2168 get_total_entries(tr, &total, &entries);
2170 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2172 seq_puts(m, "# -----------------------------------"
2173 "---------------------------------\n");
2174 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2175 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2176 nsecs_to_usecs(data->saved_latency),
2180 #if defined(CONFIG_PREEMPT_NONE)
2182 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2184 #elif defined(CONFIG_PREEMPT)
2189 /* These are reserved for later use */
2192 seq_printf(m, " #P:%d)\n", num_online_cpus());
2196 seq_puts(m, "# -----------------\n");
2197 seq_printf(m, "# | task: %.16s-%d "
2198 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2199 data->comm, data->pid,
2200 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2201 data->policy, data->rt_priority);
2202 seq_puts(m, "# -----------------\n");
2204 if (data->critical_start) {
2205 seq_puts(m, "# => started at: ");
2206 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2207 trace_print_seq(m, &iter->seq);
2208 seq_puts(m, "\n# => ended at: ");
2209 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2210 trace_print_seq(m, &iter->seq);
2211 seq_puts(m, "\n#\n");
2217 static void test_cpu_buff_start(struct trace_iterator *iter)
2219 struct trace_seq *s = &iter->seq;
2221 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2224 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2227 if (cpumask_test_cpu(iter->cpu, iter->started))
2230 if (iter->tr->data[iter->cpu]->skipped_entries)
2233 cpumask_set_cpu(iter->cpu, iter->started);
2235 /* Don't print started cpu buffer for the first entry of the trace */
2237 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2241 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2243 struct trace_seq *s = &iter->seq;
2244 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2245 struct trace_entry *entry;
2246 struct trace_event *event;
2250 test_cpu_buff_start(iter);
2252 event = ftrace_find_event(entry->type);
2254 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2255 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2256 if (!trace_print_lat_context(iter))
2259 if (!trace_print_context(iter))
2265 return event->funcs->trace(iter, sym_flags, event);
2267 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2270 return TRACE_TYPE_HANDLED;
2272 return TRACE_TYPE_PARTIAL_LINE;
2275 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2277 struct trace_seq *s = &iter->seq;
2278 struct trace_entry *entry;
2279 struct trace_event *event;
2283 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2284 if (!trace_seq_printf(s, "%d %d %llu ",
2285 entry->pid, iter->cpu, iter->ts))
2289 event = ftrace_find_event(entry->type);
2291 return event->funcs->raw(iter, 0, event);
2293 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2296 return TRACE_TYPE_HANDLED;
2298 return TRACE_TYPE_PARTIAL_LINE;
2301 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2303 struct trace_seq *s = &iter->seq;
2304 unsigned char newline = '\n';
2305 struct trace_entry *entry;
2306 struct trace_event *event;
2310 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2311 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2312 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2313 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2316 event = ftrace_find_event(entry->type);
2318 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2319 if (ret != TRACE_TYPE_HANDLED)
2323 SEQ_PUT_FIELD_RET(s, newline);
2325 return TRACE_TYPE_HANDLED;
2328 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2330 struct trace_seq *s = &iter->seq;
2331 struct trace_entry *entry;
2332 struct trace_event *event;
2336 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2337 SEQ_PUT_FIELD_RET(s, entry->pid);
2338 SEQ_PUT_FIELD_RET(s, iter->cpu);
2339 SEQ_PUT_FIELD_RET(s, iter->ts);
2342 event = ftrace_find_event(entry->type);
2343 return event ? event->funcs->binary(iter, 0, event) :
2347 int trace_empty(struct trace_iterator *iter)
2349 struct ring_buffer_iter *buf_iter;
2352 /* If we are looking at one CPU buffer, only check that one */
2353 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2354 cpu = iter->cpu_file;
2355 buf_iter = trace_buffer_iter(iter, cpu);
2357 if (!ring_buffer_iter_empty(buf_iter))
2360 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2366 for_each_tracing_cpu(cpu) {
2367 buf_iter = trace_buffer_iter(iter, cpu);
2369 if (!ring_buffer_iter_empty(buf_iter))
2372 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2380 /* Called with trace_event_read_lock() held. */
2381 enum print_line_t print_trace_line(struct trace_iterator *iter)
2383 enum print_line_t ret;
2385 if (iter->lost_events &&
2386 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2387 iter->cpu, iter->lost_events))
2388 return TRACE_TYPE_PARTIAL_LINE;
2390 if (iter->trace && iter->trace->print_line) {
2391 ret = iter->trace->print_line(iter);
2392 if (ret != TRACE_TYPE_UNHANDLED)
2396 if (iter->ent->type == TRACE_BPRINT &&
2397 trace_flags & TRACE_ITER_PRINTK &&
2398 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2399 return trace_print_bprintk_msg_only(iter);
2401 if (iter->ent->type == TRACE_PRINT &&
2402 trace_flags & TRACE_ITER_PRINTK &&
2403 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2404 return trace_print_printk_msg_only(iter);
2406 if (trace_flags & TRACE_ITER_BIN)
2407 return print_bin_fmt(iter);
2409 if (trace_flags & TRACE_ITER_HEX)
2410 return print_hex_fmt(iter);
2412 if (trace_flags & TRACE_ITER_RAW)
2413 return print_raw_fmt(iter);
2415 return print_trace_fmt(iter);
2418 void trace_latency_header(struct seq_file *m)
2420 struct trace_iterator *iter = m->private;
2422 /* print nothing if the buffers are empty */
2423 if (trace_empty(iter))
2426 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2427 print_trace_header(m, iter);
2429 if (!(trace_flags & TRACE_ITER_VERBOSE))
2430 print_lat_help_header(m);
2433 void trace_default_header(struct seq_file *m)
2435 struct trace_iterator *iter = m->private;
2437 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2440 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2441 /* print nothing if the buffers are empty */
2442 if (trace_empty(iter))
2444 print_trace_header(m, iter);
2445 if (!(trace_flags & TRACE_ITER_VERBOSE))
2446 print_lat_help_header(m);
2448 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2449 if (trace_flags & TRACE_ITER_IRQ_INFO)
2450 print_func_help_header_irq(iter->tr, m);
2452 print_func_help_header(iter->tr, m);
2457 static void test_ftrace_alive(struct seq_file *m)
2459 if (!ftrace_is_dead())
2461 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2462 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2465 #ifdef CONFIG_TRACER_MAX_TRACE
2466 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2468 if (iter->trace->allocated_snapshot)
2469 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2471 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2473 seq_printf(m, "# Snapshot commands:\n");
2474 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2475 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2476 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2477 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2478 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2479 seq_printf(m, "# is not a '0' or '1')\n");
2482 /* Should never be called */
2483 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2486 static int s_show(struct seq_file *m, void *v)
2488 struct trace_iterator *iter = v;
2491 if (iter->ent == NULL) {
2493 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2495 test_ftrace_alive(m);
2497 if (iter->snapshot && trace_empty(iter))
2498 print_snapshot_help(m, iter);
2499 else if (iter->trace && iter->trace->print_header)
2500 iter->trace->print_header(m);
2502 trace_default_header(m);
2504 } else if (iter->leftover) {
2506 * If we filled the seq_file buffer earlier, we
2507 * want to just show it now.
2509 ret = trace_print_seq(m, &iter->seq);
2511 /* ret should this time be zero, but you never know */
2512 iter->leftover = ret;
2515 print_trace_line(iter);
2516 ret = trace_print_seq(m, &iter->seq);
2518 * If we overflow the seq_file buffer, then it will
2519 * ask us for this data again at start up.
2521 * ret is 0 if seq_file write succeeded.
2524 iter->leftover = ret;
2530 static const struct seq_operations tracer_seq_ops = {
2537 static struct trace_iterator *
2538 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2540 struct trace_cpu *tc = inode->i_private;
2541 struct trace_array *tr = tc->tr;
2542 struct trace_iterator *iter;
2545 if (tracing_disabled)
2546 return ERR_PTR(-ENODEV);
2548 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2550 return ERR_PTR(-ENOMEM);
2552 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2554 if (!iter->buffer_iter)
2558 * We make a copy of the current tracer to avoid concurrent
2559 * changes on it while we are reading.
2561 mutex_lock(&trace_types_lock);
2562 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2566 *iter->trace = *tr->current_trace;
2568 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2571 /* Currently only the top directory has a snapshot */
2572 if (tr->current_trace->print_max || snapshot)
2576 iter->snapshot = snapshot;
2578 mutex_init(&iter->mutex);
2579 iter->cpu_file = tc->cpu;
2581 /* Notify the tracer early; before we stop tracing. */
2582 if (iter->trace && iter->trace->open)
2583 iter->trace->open(iter);
2585 /* Annotate start of buffers if we had overruns */
2586 if (ring_buffer_overruns(iter->tr->buffer))
2587 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2589 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2590 if (trace_clocks[trace_clock_id].in_ns)
2591 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2593 /* stop the trace while dumping if we are not opening "snapshot" */
2594 if (!iter->snapshot)
2595 tracing_stop_tr(tr);
2597 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
2598 for_each_tracing_cpu(cpu) {
2599 iter->buffer_iter[cpu] =
2600 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2602 ring_buffer_read_prepare_sync();
2603 for_each_tracing_cpu(cpu) {
2604 ring_buffer_read_start(iter->buffer_iter[cpu]);
2605 tracing_iter_reset(iter, cpu);
2608 cpu = iter->cpu_file;
2609 iter->buffer_iter[cpu] =
2610 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2611 ring_buffer_read_prepare_sync();
2612 ring_buffer_read_start(iter->buffer_iter[cpu]);
2613 tracing_iter_reset(iter, cpu);
2616 mutex_unlock(&trace_types_lock);
2621 mutex_unlock(&trace_types_lock);
2623 kfree(iter->buffer_iter);
2625 seq_release_private(inode, file);
2626 return ERR_PTR(-ENOMEM);
2629 int tracing_open_generic(struct inode *inode, struct file *filp)
2631 if (tracing_disabled)
2634 filp->private_data = inode->i_private;
2638 static int tracing_release(struct inode *inode, struct file *file)
2640 struct seq_file *m = file->private_data;
2641 struct trace_iterator *iter;
2642 struct trace_array *tr;
2645 if (!(file->f_mode & FMODE_READ))
2650 /* Only the global tracer has a matching max_tr */
2651 if (iter->tr == &max_tr)
2656 mutex_lock(&trace_types_lock);
2657 for_each_tracing_cpu(cpu) {
2658 if (iter->buffer_iter[cpu])
2659 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2662 if (iter->trace && iter->trace->close)
2663 iter->trace->close(iter);
2665 if (!iter->snapshot)
2666 /* reenable tracing if it was previously enabled */
2667 tracing_start_tr(tr);
2668 mutex_unlock(&trace_types_lock);
2670 mutex_destroy(&iter->mutex);
2671 free_cpumask_var(iter->started);
2673 kfree(iter->buffer_iter);
2674 seq_release_private(inode, file);
2678 static int tracing_open(struct inode *inode, struct file *file)
2680 struct trace_iterator *iter;
2683 /* If this file was open for write, then erase contents */
2684 if ((file->f_mode & FMODE_WRITE) &&
2685 (file->f_flags & O_TRUNC)) {
2686 struct trace_cpu *tc = inode->i_private;
2687 struct trace_array *tr = tc->tr;
2689 if (tc->cpu == RING_BUFFER_ALL_CPUS)
2690 tracing_reset_online_cpus(tr);
2692 tracing_reset(tr, tc->cpu);
2695 if (file->f_mode & FMODE_READ) {
2696 iter = __tracing_open(inode, file, false);
2698 ret = PTR_ERR(iter);
2699 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2700 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2706 t_next(struct seq_file *m, void *v, loff_t *pos)
2708 struct tracer *t = v;
2718 static void *t_start(struct seq_file *m, loff_t *pos)
2723 mutex_lock(&trace_types_lock);
2724 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2730 static void t_stop(struct seq_file *m, void *p)
2732 mutex_unlock(&trace_types_lock);
2735 static int t_show(struct seq_file *m, void *v)
2737 struct tracer *t = v;
2742 seq_printf(m, "%s", t->name);
2751 static const struct seq_operations show_traces_seq_ops = {
2758 static int show_traces_open(struct inode *inode, struct file *file)
2760 if (tracing_disabled)
2763 return seq_open(file, &show_traces_seq_ops);
2767 tracing_write_stub(struct file *filp, const char __user *ubuf,
2768 size_t count, loff_t *ppos)
2773 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2775 if (file->f_mode & FMODE_READ)
2776 return seq_lseek(file, offset, origin);
2781 static const struct file_operations tracing_fops = {
2782 .open = tracing_open,
2784 .write = tracing_write_stub,
2785 .llseek = tracing_seek,
2786 .release = tracing_release,
2789 static const struct file_operations show_traces_fops = {
2790 .open = show_traces_open,
2792 .release = seq_release,
2793 .llseek = seq_lseek,
2797 * Only trace on a CPU if the bitmask is set:
2799 static cpumask_var_t tracing_cpumask;
2802 * The tracer itself will not take this lock, but still we want
2803 * to provide a consistent cpumask to user-space:
2805 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2808 * Temporary storage for the character representation of the
2809 * CPU bitmask (and one more byte for the newline):
2811 static char mask_str[NR_CPUS + 1];
2814 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2815 size_t count, loff_t *ppos)
2819 mutex_lock(&tracing_cpumask_update_lock);
2821 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2822 if (count - len < 2) {
2826 len += sprintf(mask_str + len, "\n");
2827 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2830 mutex_unlock(&tracing_cpumask_update_lock);
2836 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2837 size_t count, loff_t *ppos)
2839 struct trace_array *tr = filp->private_data;
2840 cpumask_var_t tracing_cpumask_new;
2843 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2846 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2850 mutex_lock(&tracing_cpumask_update_lock);
2852 local_irq_disable();
2853 arch_spin_lock(&ftrace_max_lock);
2854 for_each_tracing_cpu(cpu) {
2856 * Increase/decrease the disabled counter if we are
2857 * about to flip a bit in the cpumask:
2859 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2860 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2861 atomic_inc(&tr->data[cpu]->disabled);
2862 ring_buffer_record_disable_cpu(tr->buffer, cpu);
2864 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2865 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2866 atomic_dec(&tr->data[cpu]->disabled);
2867 ring_buffer_record_enable_cpu(tr->buffer, cpu);
2870 arch_spin_unlock(&ftrace_max_lock);
2873 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2875 mutex_unlock(&tracing_cpumask_update_lock);
2876 free_cpumask_var(tracing_cpumask_new);
2881 free_cpumask_var(tracing_cpumask_new);
2886 static const struct file_operations tracing_cpumask_fops = {
2887 .open = tracing_open_generic,
2888 .read = tracing_cpumask_read,
2889 .write = tracing_cpumask_write,
2890 .llseek = generic_file_llseek,
2893 static int tracing_trace_options_show(struct seq_file *m, void *v)
2895 struct tracer_opt *trace_opts;
2896 struct trace_array *tr = m->private;
2900 mutex_lock(&trace_types_lock);
2901 tracer_flags = tr->current_trace->flags->val;
2902 trace_opts = tr->current_trace->flags->opts;
2904 for (i = 0; trace_options[i]; i++) {
2905 if (trace_flags & (1 << i))
2906 seq_printf(m, "%s\n", trace_options[i]);
2908 seq_printf(m, "no%s\n", trace_options[i]);
2911 for (i = 0; trace_opts[i].name; i++) {
2912 if (tracer_flags & trace_opts[i].bit)
2913 seq_printf(m, "%s\n", trace_opts[i].name);
2915 seq_printf(m, "no%s\n", trace_opts[i].name);
2917 mutex_unlock(&trace_types_lock);
2922 static int __set_tracer_option(struct tracer *trace,
2923 struct tracer_flags *tracer_flags,
2924 struct tracer_opt *opts, int neg)
2928 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2933 tracer_flags->val &= ~opts->bit;
2935 tracer_flags->val |= opts->bit;
2939 /* Try to assign a tracer specific option */
2940 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2942 struct tracer_flags *tracer_flags = trace->flags;
2943 struct tracer_opt *opts = NULL;
2946 for (i = 0; tracer_flags->opts[i].name; i++) {
2947 opts = &tracer_flags->opts[i];
2949 if (strcmp(cmp, opts->name) == 0)
2950 return __set_tracer_option(trace, trace->flags,
2957 /* Some tracers require overwrite to stay enabled */
2958 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
2960 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
2966 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
2968 /* do nothing if flag is already set */
2969 if (!!(trace_flags & mask) == !!enabled)
2972 /* Give the tracer a chance to approve the change */
2973 if (tr->current_trace->flag_changed)
2974 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
2978 trace_flags |= mask;
2980 trace_flags &= ~mask;
2982 if (mask == TRACE_ITER_RECORD_CMD)
2983 trace_event_enable_cmd_record(enabled);
2985 if (mask == TRACE_ITER_OVERWRITE) {
2986 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2987 #ifdef CONFIG_TRACER_MAX_TRACE
2988 ring_buffer_change_overwrite(max_tr.buffer, enabled);
2992 if (mask == TRACE_ITER_PRINTK)
2993 trace_printk_start_stop_comm(enabled);
2998 static int trace_set_options(struct trace_array *tr, char *option)
3005 cmp = strstrip(option);
3007 if (strncmp(cmp, "no", 2) == 0) {
3012 mutex_lock(&trace_types_lock);
3014 for (i = 0; trace_options[i]; i++) {
3015 if (strcmp(cmp, trace_options[i]) == 0) {
3016 ret = set_tracer_flag(tr, 1 << i, !neg);
3021 /* If no option could be set, test the specific tracer options */
3022 if (!trace_options[i])
3023 ret = set_tracer_option(tr->current_trace, cmp, neg);
3025 mutex_unlock(&trace_types_lock);
3031 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3032 size_t cnt, loff_t *ppos)
3034 struct seq_file *m = filp->private_data;
3035 struct trace_array *tr = m->private;
3039 if (cnt >= sizeof(buf))
3042 if (copy_from_user(&buf, ubuf, cnt))
3047 ret = trace_set_options(tr, buf);
3056 static int tracing_trace_options_open(struct inode *inode, struct file *file)
3058 if (tracing_disabled)
3061 return single_open(file, tracing_trace_options_show, inode->i_private);
3064 static const struct file_operations tracing_iter_fops = {
3065 .open = tracing_trace_options_open,
3067 .llseek = seq_lseek,
3068 .release = single_release,
3069 .write = tracing_trace_options_write,
3072 static const char readme_msg[] =
3073 "tracing mini-HOWTO:\n\n"
3074 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
3075 "# cat /sys/kernel/debug/tracing/available_tracers\n"
3076 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
3077 "# cat /sys/kernel/debug/tracing/current_tracer\n"
3079 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
3080 "# cat /sys/kernel/debug/tracing/current_tracer\n"
3082 "# cat /sys/kernel/debug/tracing/trace_options\n"
3083 "noprint-parent nosym-offset nosym-addr noverbose\n"
3084 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
3085 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
3086 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
3087 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
3091 tracing_readme_read(struct file *filp, char __user *ubuf,
3092 size_t cnt, loff_t *ppos)
3094 return simple_read_from_buffer(ubuf, cnt, ppos,
3095 readme_msg, strlen(readme_msg));
3098 static const struct file_operations tracing_readme_fops = {
3099 .open = tracing_open_generic,
3100 .read = tracing_readme_read,
3101 .llseek = generic_file_llseek,
3105 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3106 size_t cnt, loff_t *ppos)
3115 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3119 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3127 for (i = 0; i < SAVED_CMDLINES; i++) {
3130 pid = map_cmdline_to_pid[i];
3131 if (pid == -1 || pid == NO_CMDLINE_MAP)
3134 trace_find_cmdline(pid, buf_comm);
3135 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3140 len = simple_read_from_buffer(ubuf, cnt, ppos,
3149 static const struct file_operations tracing_saved_cmdlines_fops = {
3150 .open = tracing_open_generic,
3151 .read = tracing_saved_cmdlines_read,
3152 .llseek = generic_file_llseek,
3156 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3157 size_t cnt, loff_t *ppos)
3159 struct trace_array *tr = filp->private_data;
3160 char buf[MAX_TRACER_SIZE+2];
3163 mutex_lock(&trace_types_lock);
3164 r = sprintf(buf, "%s\n", tr->current_trace->name);
3165 mutex_unlock(&trace_types_lock);
3167 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3170 int tracer_init(struct tracer *t, struct trace_array *tr)
3172 tracing_reset_online_cpus(tr);
3176 static void set_buffer_entries(struct trace_array *tr, unsigned long val)
3179 for_each_tracing_cpu(cpu)
3180 tr->data[cpu]->entries = val;
3183 /* resize @tr's buffer to the size of @size_tr's entries */
3184 static int resize_buffer_duplicate_size(struct trace_array *tr,
3185 struct trace_array *size_tr, int cpu_id)
3189 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3190 for_each_tracing_cpu(cpu) {
3191 ret = ring_buffer_resize(tr->buffer,
3192 size_tr->data[cpu]->entries, cpu);
3195 tr->data[cpu]->entries = size_tr->data[cpu]->entries;
3198 ret = ring_buffer_resize(tr->buffer,
3199 size_tr->data[cpu_id]->entries, cpu_id);
3201 tr->data[cpu_id]->entries =
3202 size_tr->data[cpu_id]->entries;
3208 static int __tracing_resize_ring_buffer(struct trace_array *tr,
3209 unsigned long size, int cpu)
3214 * If kernel or user changes the size of the ring buffer
3215 * we use the size that was given, and we can forget about
3216 * expanding it later.
3218 ring_buffer_expanded = 1;
3220 /* May be called before buffers are initialized */
3224 ret = ring_buffer_resize(tr->buffer, size, cpu);
3228 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3229 !tr->current_trace->use_max_tr)
3232 ret = ring_buffer_resize(max_tr.buffer, size, cpu);
3234 int r = resize_buffer_duplicate_size(tr, tr, cpu);
3237 * AARGH! We are left with different
3238 * size max buffer!!!!
3239 * The max buffer is our "snapshot" buffer.
3240 * When a tracer needs a snapshot (one of the
3241 * latency tracers), it swaps the max buffer
3242 * with the saved snap shot. We succeeded to
3243 * update the size of the main buffer, but failed to
3244 * update the size of the max buffer. But when we tried
3245 * to reset the main buffer to the original size, we
3246 * failed there too. This is very unlikely to
3247 * happen, but if it does, warn and kill all
3251 tracing_disabled = 1;
3256 if (cpu == RING_BUFFER_ALL_CPUS)
3257 set_buffer_entries(&max_tr, size);
3259 max_tr.data[cpu]->entries = size;
3262 if (cpu == RING_BUFFER_ALL_CPUS)
3263 set_buffer_entries(tr, size);
3265 tr->data[cpu]->entries = size;
3270 static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3271 unsigned long size, int cpu_id)
3275 mutex_lock(&trace_types_lock);
3277 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3278 /* make sure, this cpu is enabled in the mask */
3279 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3285 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
3290 mutex_unlock(&trace_types_lock);
3297 * tracing_update_buffers - used by tracing facility to expand ring buffers
3299 * To save on memory when the tracing is never used on a system with it
3300 * configured in. The ring buffers are set to a minimum size. But once
3301 * a user starts to use the tracing facility, then they need to grow
3302 * to their default size.
3304 * This function is to be called when a tracer is about to be used.
3306 int tracing_update_buffers(void)
3310 mutex_lock(&trace_types_lock);
3311 if (!ring_buffer_expanded)
3312 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
3313 RING_BUFFER_ALL_CPUS);
3314 mutex_unlock(&trace_types_lock);
3319 struct trace_option_dentry;
3321 static struct trace_option_dentry *
3322 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
3325 destroy_trace_option_files(struct trace_option_dentry *topts);
3327 static int tracing_set_tracer(const char *buf)
3329 static struct trace_option_dentry *topts;
3330 struct trace_array *tr = &global_trace;
3335 mutex_lock(&trace_types_lock);
3337 if (!ring_buffer_expanded) {
3338 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
3339 RING_BUFFER_ALL_CPUS);
3345 for (t = trace_types; t; t = t->next) {
3346 if (strcmp(t->name, buf) == 0)
3353 if (t == tr->current_trace)
3356 trace_branch_disable();
3358 tr->current_trace->enabled = false;
3360 if (tr->current_trace->reset)
3361 tr->current_trace->reset(tr);
3363 had_max_tr = tr->current_trace->allocated_snapshot;
3364 tr->current_trace = &nop_trace;
3366 if (had_max_tr && !t->use_max_tr) {
3368 * We need to make sure that the update_max_tr sees that
3369 * current_trace changed to nop_trace to keep it from
3370 * swapping the buffers after we resize it.
3371 * The update_max_tr is called from interrupts disabled
3372 * so a synchronized_sched() is sufficient.
3374 synchronize_sched();
3376 * We don't free the ring buffer. instead, resize it because
3377 * The max_tr ring buffer has some state (e.g. ring->clock) and
3378 * we want preserve it.
3380 ring_buffer_resize(max_tr.buffer, 1, RING_BUFFER_ALL_CPUS);
3381 set_buffer_entries(&max_tr, 1);
3382 tracing_reset_online_cpus(&max_tr);
3383 tr->current_trace->allocated_snapshot = false;
3385 destroy_trace_option_files(topts);
3387 topts = create_trace_option_files(tr, t);
3388 if (t->use_max_tr && !had_max_tr) {
3389 /* we need to make per cpu buffer sizes equivalent */
3390 ret = resize_buffer_duplicate_size(&max_tr, &global_trace,
3391 RING_BUFFER_ALL_CPUS);
3394 t->allocated_snapshot = true;
3398 ret = tracer_init(t, tr);
3403 tr->current_trace = t;
3404 tr->current_trace->enabled = true;
3405 trace_branch_enable(tr);
3407 mutex_unlock(&trace_types_lock);
3413 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3414 size_t cnt, loff_t *ppos)
3416 char buf[MAX_TRACER_SIZE+1];
3423 if (cnt > MAX_TRACER_SIZE)
3424 cnt = MAX_TRACER_SIZE;
3426 if (copy_from_user(&buf, ubuf, cnt))
3431 /* strip ending whitespace. */
3432 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3435 err = tracing_set_tracer(buf);
3445 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3446 size_t cnt, loff_t *ppos)
3448 unsigned long *ptr = filp->private_data;
3452 r = snprintf(buf, sizeof(buf), "%ld\n",
3453 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3454 if (r > sizeof(buf))
3456 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3460 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3461 size_t cnt, loff_t *ppos)
3463 unsigned long *ptr = filp->private_data;
3467 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3476 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3478 struct trace_cpu *tc = inode->i_private;
3479 struct trace_array *tr = tc->tr;
3480 struct trace_iterator *iter;
3483 if (tracing_disabled)
3486 mutex_lock(&trace_types_lock);
3488 /* create a buffer to store the information to pass to userspace */
3489 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3496 * We make a copy of the current tracer to avoid concurrent
3497 * changes on it while we are reading.
3499 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3504 *iter->trace = *tr->current_trace;
3506 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3511 /* trace pipe does not show start of buffer */
3512 cpumask_setall(iter->started);
3514 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3515 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3517 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3518 if (trace_clocks[trace_clock_id].in_ns)
3519 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3521 iter->cpu_file = tc->cpu;
3523 mutex_init(&iter->mutex);
3524 filp->private_data = iter;
3526 if (iter->trace->pipe_open)
3527 iter->trace->pipe_open(iter);
3529 nonseekable_open(inode, filp);
3531 mutex_unlock(&trace_types_lock);
3537 mutex_unlock(&trace_types_lock);
3541 static int tracing_release_pipe(struct inode *inode, struct file *file)
3543 struct trace_iterator *iter = file->private_data;
3545 mutex_lock(&trace_types_lock);
3547 if (iter->trace->pipe_close)
3548 iter->trace->pipe_close(iter);
3550 mutex_unlock(&trace_types_lock);
3552 free_cpumask_var(iter->started);
3553 mutex_destroy(&iter->mutex);
3561 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3563 struct trace_iterator *iter = filp->private_data;
3565 if (trace_flags & TRACE_ITER_BLOCK) {
3567 * Always select as readable when in blocking mode
3569 return POLLIN | POLLRDNORM;
3571 if (!trace_empty(iter))
3572 return POLLIN | POLLRDNORM;
3573 poll_wait(filp, &trace_wait, poll_table);
3574 if (!trace_empty(iter))
3575 return POLLIN | POLLRDNORM;
3582 * This is a make-shift waitqueue.
3583 * A tracer might use this callback on some rare cases:
3585 * 1) the current tracer might hold the runqueue lock when it wakes up
3586 * a reader, hence a deadlock (sched, function, and function graph tracers)
3587 * 2) the function tracers, trace all functions, we don't want
3588 * the overhead of calling wake_up and friends
3589 * (and tracing them too)
3591 * Anyway, this is really very primitive wakeup.
3593 void poll_wait_pipe(struct trace_iterator *iter)
3595 set_current_state(TASK_INTERRUPTIBLE);
3596 /* sleep for 100 msecs, and try again. */
3597 schedule_timeout(HZ / 10);
3600 /* Must be called with trace_types_lock mutex held. */
3601 static int tracing_wait_pipe(struct file *filp)
3603 struct trace_iterator *iter = filp->private_data;
3605 while (trace_empty(iter)) {
3607 if ((filp->f_flags & O_NONBLOCK)) {
3611 mutex_unlock(&iter->mutex);
3613 iter->trace->wait_pipe(iter);
3615 mutex_lock(&iter->mutex);
3617 if (signal_pending(current))
3621 * We block until we read something and tracing is disabled.
3622 * We still block if tracing is disabled, but we have never
3623 * read anything. This allows a user to cat this file, and
3624 * then enable tracing. But after we have read something,
3625 * we give an EOF when tracing is again disabled.
3627 * iter->pos will be 0 if we haven't read anything.
3629 if (!tracing_is_enabled() && iter->pos)
3640 tracing_read_pipe(struct file *filp, char __user *ubuf,
3641 size_t cnt, loff_t *ppos)
3643 struct trace_iterator *iter = filp->private_data;
3644 struct trace_array *tr = iter->tr;
3647 /* return any leftover data */
3648 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3652 trace_seq_init(&iter->seq);
3654 /* copy the tracer to avoid using a global lock all around */
3655 mutex_lock(&trace_types_lock);
3656 if (unlikely(iter->trace->name != tr->current_trace->name))
3657 *iter->trace = *tr->current_trace;
3658 mutex_unlock(&trace_types_lock);
3661 * Avoid more than one consumer on a single file descriptor
3662 * This is just a matter of traces coherency, the ring buffer itself
3665 mutex_lock(&iter->mutex);
3666 if (iter->trace->read) {
3667 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3673 sret = tracing_wait_pipe(filp);
3677 /* stop when tracing is finished */
3678 if (trace_empty(iter)) {
3683 if (cnt >= PAGE_SIZE)
3684 cnt = PAGE_SIZE - 1;
3686 /* reset all but tr, trace, and overruns */
3687 memset(&iter->seq, 0,
3688 sizeof(struct trace_iterator) -
3689 offsetof(struct trace_iterator, seq));
3692 trace_event_read_lock();
3693 trace_access_lock(iter->cpu_file);
3694 while (trace_find_next_entry_inc(iter) != NULL) {
3695 enum print_line_t ret;
3696 int len = iter->seq.len;
3698 ret = print_trace_line(iter);
3699 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3700 /* don't print partial lines */
3701 iter->seq.len = len;
3704 if (ret != TRACE_TYPE_NO_CONSUME)
3705 trace_consume(iter);
3707 if (iter->seq.len >= cnt)
3711 * Setting the full flag means we reached the trace_seq buffer
3712 * size and we should leave by partial output condition above.
3713 * One of the trace_seq_* functions is not used properly.
3715 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3718 trace_access_unlock(iter->cpu_file);
3719 trace_event_read_unlock();
3721 /* Now copy what we have to the user */
3722 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3723 if (iter->seq.readpos >= iter->seq.len)
3724 trace_seq_init(&iter->seq);
3727 * If there was nothing to send to user, in spite of consuming trace
3728 * entries, go back to wait for more entries.
3734 mutex_unlock(&iter->mutex);
3739 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3740 struct pipe_buffer *buf)
3742 __free_page(buf->page);
3745 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3748 __free_page(spd->pages[idx]);
3751 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3753 .map = generic_pipe_buf_map,
3754 .unmap = generic_pipe_buf_unmap,
3755 .confirm = generic_pipe_buf_confirm,
3756 .release = tracing_pipe_buf_release,
3757 .steal = generic_pipe_buf_steal,
3758 .get = generic_pipe_buf_get,
3762 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3767 /* Seq buffer is page-sized, exactly what we need. */
3769 count = iter->seq.len;
3770 ret = print_trace_line(iter);
3771 count = iter->seq.len - count;
3774 iter->seq.len -= count;
3777 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3778 iter->seq.len -= count;
3782 if (ret != TRACE_TYPE_NO_CONSUME)
3783 trace_consume(iter);
3785 if (!trace_find_next_entry_inc(iter)) {
3795 static ssize_t tracing_splice_read_pipe(struct file *filp,
3797 struct pipe_inode_info *pipe,
3801 struct page *pages_def[PIPE_DEF_BUFFERS];
3802 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3803 struct trace_iterator *iter = filp->private_data;
3804 struct splice_pipe_desc spd = {
3806 .partial = partial_def,
3807 .nr_pages = 0, /* This gets updated below. */
3808 .nr_pages_max = PIPE_DEF_BUFFERS,
3810 .ops = &tracing_pipe_buf_ops,
3811 .spd_release = tracing_spd_release_pipe,
3813 struct trace_array *tr = iter->tr;
3818 if (splice_grow_spd(pipe, &spd))
3821 /* copy the tracer to avoid using a global lock all around */
3822 mutex_lock(&trace_types_lock);
3823 if (unlikely(iter->trace->name != tr->current_trace->name))
3824 *iter->trace = *tr->current_trace;
3825 mutex_unlock(&trace_types_lock);
3827 mutex_lock(&iter->mutex);
3829 if (iter->trace->splice_read) {
3830 ret = iter->trace->splice_read(iter, filp,
3831 ppos, pipe, len, flags);
3836 ret = tracing_wait_pipe(filp);
3840 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3845 trace_event_read_lock();
3846 trace_access_lock(iter->cpu_file);
3848 /* Fill as many pages as possible. */
3849 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3850 spd.pages[i] = alloc_page(GFP_KERNEL);
3854 rem = tracing_fill_pipe_page(rem, iter);
3856 /* Copy the data into the page, so we can start over. */
3857 ret = trace_seq_to_buffer(&iter->seq,
3858 page_address(spd.pages[i]),
3861 __free_page(spd.pages[i]);
3864 spd.partial[i].offset = 0;
3865 spd.partial[i].len = iter->seq.len;
3867 trace_seq_init(&iter->seq);
3870 trace_access_unlock(iter->cpu_file);
3871 trace_event_read_unlock();
3872 mutex_unlock(&iter->mutex);
3876 ret = splice_to_pipe(pipe, &spd);
3878 splice_shrink_spd(&spd);
3882 mutex_unlock(&iter->mutex);
3887 tracing_entries_read(struct file *filp, char __user *ubuf,
3888 size_t cnt, loff_t *ppos)
3890 struct trace_cpu *tc = filp->private_data;
3891 struct trace_array *tr = tc->tr;
3896 mutex_lock(&trace_types_lock);
3898 if (tc->cpu == RING_BUFFER_ALL_CPUS) {
3899 int cpu, buf_size_same;
3904 /* check if all cpu sizes are same */
3905 for_each_tracing_cpu(cpu) {
3906 /* fill in the size from first enabled cpu */
3908 size = tr->data[cpu]->entries;
3909 if (size != tr->data[cpu]->entries) {
3915 if (buf_size_same) {
3916 if (!ring_buffer_expanded)
3917 r = sprintf(buf, "%lu (expanded: %lu)\n",
3919 trace_buf_size >> 10);
3921 r = sprintf(buf, "%lu\n", size >> 10);
3923 r = sprintf(buf, "X\n");
3925 r = sprintf(buf, "%lu\n", tr->data[tc->cpu]->entries >> 10);
3927 mutex_unlock(&trace_types_lock);
3929 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3934 tracing_entries_write(struct file *filp, const char __user *ubuf,
3935 size_t cnt, loff_t *ppos)
3937 struct trace_cpu *tc = filp->private_data;
3941 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3945 /* must have at least 1 entry */
3949 /* value is in KB */
3952 ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
3962 tracing_total_entries_read(struct file *filp, char __user *ubuf,
3963 size_t cnt, loff_t *ppos)
3965 struct trace_array *tr = filp->private_data;
3968 unsigned long size = 0, expanded_size = 0;
3970 mutex_lock(&trace_types_lock);
3971 for_each_tracing_cpu(cpu) {
3972 size += tr->data[cpu]->entries >> 10;
3973 if (!ring_buffer_expanded)
3974 expanded_size += trace_buf_size >> 10;
3976 if (ring_buffer_expanded)
3977 r = sprintf(buf, "%lu\n", size);
3979 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3980 mutex_unlock(&trace_types_lock);
3982 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3986 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3987 size_t cnt, loff_t *ppos)
3990 * There is no need to read what the user has written, this function
3991 * is just to make sure that there is no error when "echo" is used
4000 tracing_free_buffer_release(struct inode *inode, struct file *filp)
4002 struct trace_array *tr = inode->i_private;
4004 /* disable tracing ? */
4005 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4007 /* resize the ring buffer to 0 */
4008 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4014 tracing_mark_write(struct file *filp, const char __user *ubuf,
4015 size_t cnt, loff_t *fpos)
4017 unsigned long addr = (unsigned long)ubuf;
4018 struct ring_buffer_event *event;
4019 struct ring_buffer *buffer;
4020 struct print_entry *entry;
4021 unsigned long irq_flags;
4022 struct page *pages[2];
4032 if (tracing_disabled)
4035 if (!(trace_flags & TRACE_ITER_MARKERS))
4038 if (cnt > TRACE_BUF_SIZE)
4039 cnt = TRACE_BUF_SIZE;
4042 * Userspace is injecting traces into the kernel trace buffer.
4043 * We want to be as non intrusive as possible.
4044 * To do so, we do not want to allocate any special buffers
4045 * or take any locks, but instead write the userspace data
4046 * straight into the ring buffer.
4048 * First we need to pin the userspace buffer into memory,
4049 * which, most likely it is, because it just referenced it.
4050 * But there's no guarantee that it is. By using get_user_pages_fast()
4051 * and kmap_atomic/kunmap_atomic() we can get access to the
4052 * pages directly. We then write the data directly into the
4055 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4057 /* check if we cross pages */
4058 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4061 offset = addr & (PAGE_SIZE - 1);
4064 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4065 if (ret < nr_pages) {
4067 put_page(pages[ret]);
4072 for (i = 0; i < nr_pages; i++)
4073 map_page[i] = kmap_atomic(pages[i]);
4075 local_save_flags(irq_flags);
4076 size = sizeof(*entry) + cnt + 2; /* possible \n added */
4077 buffer = global_trace.buffer;
4078 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4079 irq_flags, preempt_count());
4081 /* Ring buffer disabled, return as if not open for write */
4086 entry = ring_buffer_event_data(event);
4087 entry->ip = _THIS_IP_;
4089 if (nr_pages == 2) {
4090 len = PAGE_SIZE - offset;
4091 memcpy(&entry->buf, map_page[0] + offset, len);
4092 memcpy(&entry->buf[len], map_page[1], cnt - len);
4094 memcpy(&entry->buf, map_page[0] + offset, cnt);
4096 if (entry->buf[cnt - 1] != '\n') {
4097 entry->buf[cnt] = '\n';
4098 entry->buf[cnt + 1] = '\0';
4100 entry->buf[cnt] = '\0';
4102 __buffer_unlock_commit(buffer, event);
4109 for (i = 0; i < nr_pages; i++){
4110 kunmap_atomic(map_page[i]);
4117 static int tracing_clock_show(struct seq_file *m, void *v)
4119 struct trace_array *tr = m->private;
4122 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4124 "%s%s%s%s", i ? " " : "",
4125 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4126 i == tr->clock_id ? "]" : "");
4132 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4133 size_t cnt, loff_t *fpos)
4135 struct seq_file *m = filp->private_data;
4136 struct trace_array *tr = m->private;
4138 const char *clockstr;
4141 if (cnt >= sizeof(buf))
4144 if (copy_from_user(&buf, ubuf, cnt))
4149 clockstr = strstrip(buf);
4151 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4152 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4155 if (i == ARRAY_SIZE(trace_clocks))
4158 mutex_lock(&trace_types_lock);
4162 ring_buffer_set_clock(tr->buffer, trace_clocks[i].func);
4163 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && max_tr.buffer)
4164 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
4167 * New clock may not be consistent with the previous clock.
4168 * Reset the buffer so that it doesn't have incomparable timestamps.
4170 tracing_reset_online_cpus(&global_trace);
4171 tracing_reset_online_cpus(&max_tr);
4173 mutex_unlock(&trace_types_lock);
4180 static int tracing_clock_open(struct inode *inode, struct file *file)
4182 if (tracing_disabled)
4185 return single_open(file, tracing_clock_show, inode->i_private);
4188 #ifdef CONFIG_TRACER_SNAPSHOT
4189 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4191 struct trace_cpu *tc = inode->i_private;
4192 struct trace_iterator *iter;
4196 if (file->f_mode & FMODE_READ) {
4197 iter = __tracing_open(inode, file, true);
4199 ret = PTR_ERR(iter);
4201 /* Writes still need the seq_file to hold the private data */
4202 m = kzalloc(sizeof(*m), GFP_KERNEL);
4205 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4212 file->private_data = m;
4219 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4222 struct seq_file *m = filp->private_data;
4223 struct trace_iterator *iter = m->private;
4224 struct trace_array *tr = iter->tr;
4228 ret = tracing_update_buffers();
4232 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4236 mutex_lock(&trace_types_lock);
4238 if (tr->current_trace->use_max_tr) {
4245 if (tr->current_trace->allocated_snapshot) {
4246 /* free spare buffer */
4247 ring_buffer_resize(max_tr.buffer, 1,
4248 RING_BUFFER_ALL_CPUS);
4249 set_buffer_entries(&max_tr, 1);
4250 tracing_reset_online_cpus(&max_tr);
4251 tr->current_trace->allocated_snapshot = false;
4255 if (!tr->current_trace->allocated_snapshot) {
4256 /* allocate spare buffer */
4257 ret = resize_buffer_duplicate_size(&max_tr,
4258 &global_trace, RING_BUFFER_ALL_CPUS);
4261 tr->current_trace->allocated_snapshot = true;
4264 local_irq_disable();
4265 /* Now, we're going to swap */
4266 update_max_tr(&global_trace, current, smp_processor_id());
4270 if (tr->current_trace->allocated_snapshot)
4271 tracing_reset_online_cpus(&max_tr);
4280 mutex_unlock(&trace_types_lock);
4284 static int tracing_snapshot_release(struct inode *inode, struct file *file)
4286 struct seq_file *m = file->private_data;
4288 if (file->f_mode & FMODE_READ)
4289 return tracing_release(inode, file);
4291 /* If write only, the seq_file is just a stub */
4299 #endif /* CONFIG_TRACER_SNAPSHOT */
4302 static const struct file_operations tracing_max_lat_fops = {
4303 .open = tracing_open_generic,
4304 .read = tracing_max_lat_read,
4305 .write = tracing_max_lat_write,
4306 .llseek = generic_file_llseek,
4309 static const struct file_operations set_tracer_fops = {
4310 .open = tracing_open_generic,
4311 .read = tracing_set_trace_read,
4312 .write = tracing_set_trace_write,
4313 .llseek = generic_file_llseek,
4316 static const struct file_operations tracing_pipe_fops = {
4317 .open = tracing_open_pipe,
4318 .poll = tracing_poll_pipe,
4319 .read = tracing_read_pipe,
4320 .splice_read = tracing_splice_read_pipe,
4321 .release = tracing_release_pipe,
4322 .llseek = no_llseek,
4325 static const struct file_operations tracing_entries_fops = {
4326 .open = tracing_open_generic,
4327 .read = tracing_entries_read,
4328 .write = tracing_entries_write,
4329 .llseek = generic_file_llseek,
4332 static const struct file_operations tracing_total_entries_fops = {
4333 .open = tracing_open_generic,
4334 .read = tracing_total_entries_read,
4335 .llseek = generic_file_llseek,
4338 static const struct file_operations tracing_free_buffer_fops = {
4339 .write = tracing_free_buffer_write,
4340 .release = tracing_free_buffer_release,
4343 static const struct file_operations tracing_mark_fops = {
4344 .open = tracing_open_generic,
4345 .write = tracing_mark_write,
4346 .llseek = generic_file_llseek,
4349 static const struct file_operations trace_clock_fops = {
4350 .open = tracing_clock_open,
4352 .llseek = seq_lseek,
4353 .release = single_release,
4354 .write = tracing_clock_write,
4357 #ifdef CONFIG_TRACER_SNAPSHOT
4358 static const struct file_operations snapshot_fops = {
4359 .open = tracing_snapshot_open,
4361 .write = tracing_snapshot_write,
4362 .llseek = tracing_seek,
4363 .release = tracing_snapshot_release,
4365 #endif /* CONFIG_TRACER_SNAPSHOT */
4367 struct ftrace_buffer_info {
4368 struct trace_array *tr;
4374 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4376 struct trace_cpu *tc = inode->i_private;
4377 struct trace_array *tr = tc->tr;
4378 struct ftrace_buffer_info *info;
4380 if (tracing_disabled)
4383 info = kzalloc(sizeof(*info), GFP_KERNEL);
4388 info->cpu = tc->cpu;
4390 /* Force reading ring buffer for first read */
4391 info->read = (unsigned int)-1;
4393 filp->private_data = info;
4395 return nonseekable_open(inode, filp);
4399 tracing_buffers_read(struct file *filp, char __user *ubuf,
4400 size_t count, loff_t *ppos)
4402 struct ftrace_buffer_info *info = filp->private_data;
4410 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
4414 /* Do we have previous read data to read? */
4415 if (info->read < PAGE_SIZE)
4418 trace_access_lock(info->cpu);
4419 ret = ring_buffer_read_page(info->tr->buffer,
4423 trace_access_unlock(info->cpu);
4430 size = PAGE_SIZE - info->read;
4434 ret = copy_to_user(ubuf, info->spare + info->read, size);
4445 static int tracing_buffers_release(struct inode *inode, struct file *file)
4447 struct ftrace_buffer_info *info = file->private_data;
4450 ring_buffer_free_read_page(info->tr->buffer, info->spare);
4457 struct ring_buffer *buffer;
4462 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4463 struct pipe_buffer *buf)
4465 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4470 ring_buffer_free_read_page(ref->buffer, ref->page);
4475 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4476 struct pipe_buffer *buf)
4478 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4483 /* Pipe buffer operations for a buffer. */
4484 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4486 .map = generic_pipe_buf_map,
4487 .unmap = generic_pipe_buf_unmap,
4488 .confirm = generic_pipe_buf_confirm,
4489 .release = buffer_pipe_buf_release,
4490 .steal = generic_pipe_buf_steal,
4491 .get = buffer_pipe_buf_get,
4495 * Callback from splice_to_pipe(), if we need to release some pages
4496 * at the end of the spd in case we error'ed out in filling the pipe.
4498 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4500 struct buffer_ref *ref =
4501 (struct buffer_ref *)spd->partial[i].private;
4506 ring_buffer_free_read_page(ref->buffer, ref->page);
4508 spd->partial[i].private = 0;
4512 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4513 struct pipe_inode_info *pipe, size_t len,
4516 struct ftrace_buffer_info *info = file->private_data;
4517 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4518 struct page *pages_def[PIPE_DEF_BUFFERS];
4519 struct splice_pipe_desc spd = {
4521 .partial = partial_def,
4522 .nr_pages_max = PIPE_DEF_BUFFERS,
4524 .ops = &buffer_pipe_buf_ops,
4525 .spd_release = buffer_spd_release,
4527 struct buffer_ref *ref;
4528 int entries, size, i;
4531 if (splice_grow_spd(pipe, &spd))
4534 if (*ppos & (PAGE_SIZE - 1)) {
4539 if (len & (PAGE_SIZE - 1)) {
4540 if (len < PAGE_SIZE) {
4547 trace_access_lock(info->cpu);
4548 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4550 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4554 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4559 ref->buffer = info->tr->buffer;
4560 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
4566 r = ring_buffer_read_page(ref->buffer, &ref->page,
4569 ring_buffer_free_read_page(ref->buffer, ref->page);
4575 * zero out any left over data, this is going to
4578 size = ring_buffer_page_len(ref->page);
4579 if (size < PAGE_SIZE)
4580 memset(ref->page + size, 0, PAGE_SIZE - size);
4582 page = virt_to_page(ref->page);
4584 spd.pages[i] = page;
4585 spd.partial[i].len = PAGE_SIZE;
4586 spd.partial[i].offset = 0;
4587 spd.partial[i].private = (unsigned long)ref;
4591 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4594 trace_access_unlock(info->cpu);
4597 /* did we read anything? */
4598 if (!spd.nr_pages) {
4599 if (flags & SPLICE_F_NONBLOCK)
4607 ret = splice_to_pipe(pipe, &spd);
4608 splice_shrink_spd(&spd);
4613 static const struct file_operations tracing_buffers_fops = {
4614 .open = tracing_buffers_open,
4615 .read = tracing_buffers_read,
4616 .release = tracing_buffers_release,
4617 .splice_read = tracing_buffers_splice_read,
4618 .llseek = no_llseek,
4622 tracing_stats_read(struct file *filp, char __user *ubuf,
4623 size_t count, loff_t *ppos)
4625 struct trace_cpu *tc = filp->private_data;
4626 struct trace_array *tr = tc->tr;
4627 struct trace_seq *s;
4629 unsigned long long t;
4630 unsigned long usec_rem;
4633 s = kmalloc(sizeof(*s), GFP_KERNEL);
4639 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4640 trace_seq_printf(s, "entries: %ld\n", cnt);
4642 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4643 trace_seq_printf(s, "overrun: %ld\n", cnt);
4645 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4646 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4648 cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4649 trace_seq_printf(s, "bytes: %ld\n", cnt);
4651 if (trace_clocks[trace_clock_id].in_ns) {
4652 /* local or global for trace_clock */
4653 t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4654 usec_rem = do_div(t, USEC_PER_SEC);
4655 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
4658 t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4659 usec_rem = do_div(t, USEC_PER_SEC);
4660 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4662 /* counter or tsc mode for trace_clock */
4663 trace_seq_printf(s, "oldest event ts: %llu\n",
4664 ring_buffer_oldest_event_ts(tr->buffer, cpu));
4666 trace_seq_printf(s, "now ts: %llu\n",
4667 ring_buffer_time_stamp(tr->buffer, cpu));
4670 cnt = ring_buffer_dropped_events_cpu(tr->buffer, cpu);
4671 trace_seq_printf(s, "dropped events: %ld\n", cnt);
4673 cnt = ring_buffer_read_events_cpu(tr->buffer, cpu);
4674 trace_seq_printf(s, "read events: %ld\n", cnt);
4676 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4683 static const struct file_operations tracing_stats_fops = {
4684 .open = tracing_open_generic,
4685 .read = tracing_stats_read,
4686 .llseek = generic_file_llseek,
4689 #ifdef CONFIG_DYNAMIC_FTRACE
4691 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4697 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
4698 size_t cnt, loff_t *ppos)
4700 static char ftrace_dyn_info_buffer[1024];
4701 static DEFINE_MUTEX(dyn_info_mutex);
4702 unsigned long *p = filp->private_data;
4703 char *buf = ftrace_dyn_info_buffer;
4704 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
4707 mutex_lock(&dyn_info_mutex);
4708 r = sprintf(buf, "%ld ", *p);
4710 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
4713 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4715 mutex_unlock(&dyn_info_mutex);
4720 static const struct file_operations tracing_dyn_info_fops = {
4721 .open = tracing_open_generic,
4722 .read = tracing_read_dyn_info,
4723 .llseek = generic_file_llseek,
4727 struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
4734 if (!debugfs_initialized())
4737 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
4738 tr->dir = debugfs_create_dir("tracing", NULL);
4740 if (!tr->dir && !once) {
4742 pr_warning("Could not create debugfs directory 'tracing'\n");
4749 struct dentry *tracing_init_dentry(void)
4751 return tracing_init_dentry_tr(&global_trace);
4754 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
4756 struct dentry *d_tracer;
4759 return tr->percpu_dir;
4761 d_tracer = tracing_init_dentry_tr(tr);
4765 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
4767 WARN_ONCE(!tr->percpu_dir,
4768 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
4770 return tr->percpu_dir;
4774 tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
4776 struct trace_array_cpu *data = tr->data[cpu];
4777 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
4778 struct dentry *d_cpu;
4779 char cpu_dir[30]; /* 30 characters should be more than enough */
4784 snprintf(cpu_dir, 30, "cpu%ld", cpu);
4785 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4787 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4791 /* per cpu trace_pipe */
4792 trace_create_file("trace_pipe", 0444, d_cpu,
4793 (void *)&data->trace_cpu, &tracing_pipe_fops);
4796 trace_create_file("trace", 0644, d_cpu,
4797 (void *)&data->trace_cpu, &tracing_fops);
4799 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4800 (void *)&data->trace_cpu, &tracing_buffers_fops);
4802 trace_create_file("stats", 0444, d_cpu,
4803 (void *)&data->trace_cpu, &tracing_stats_fops);
4805 trace_create_file("buffer_size_kb", 0444, d_cpu,
4806 (void *)&data->trace_cpu, &tracing_entries_fops);
4809 #ifdef CONFIG_FTRACE_SELFTEST
4810 /* Let selftest have access to static functions in this file */
4811 #include "trace_selftest.c"
4814 struct trace_option_dentry {
4815 struct tracer_opt *opt;
4816 struct tracer_flags *flags;
4817 struct trace_array *tr;
4818 struct dentry *entry;
4822 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4825 struct trace_option_dentry *topt = filp->private_data;
4828 if (topt->flags->val & topt->opt->bit)
4833 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4837 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4840 struct trace_option_dentry *topt = filp->private_data;
4844 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4848 if (val != 0 && val != 1)
4851 if (!!(topt->flags->val & topt->opt->bit) != val) {
4852 mutex_lock(&trace_types_lock);
4853 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
4855 mutex_unlock(&trace_types_lock);
4866 static const struct file_operations trace_options_fops = {
4867 .open = tracing_open_generic,
4868 .read = trace_options_read,
4869 .write = trace_options_write,
4870 .llseek = generic_file_llseek,
4874 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4877 long index = (long)filp->private_data;
4880 if (trace_flags & (1 << index))
4885 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4889 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4892 struct trace_array *tr = &global_trace;
4893 long index = (long)filp->private_data;
4897 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4901 if (val != 0 && val != 1)
4904 mutex_lock(&trace_types_lock);
4905 ret = set_tracer_flag(tr, 1 << index, val);
4906 mutex_unlock(&trace_types_lock);
4916 static const struct file_operations trace_options_core_fops = {
4917 .open = tracing_open_generic,
4918 .read = trace_options_core_read,
4919 .write = trace_options_core_write,
4920 .llseek = generic_file_llseek,
4923 struct dentry *trace_create_file(const char *name,
4925 struct dentry *parent,
4927 const struct file_operations *fops)
4931 ret = debugfs_create_file(name, mode, parent, data, fops);
4933 pr_warning("Could not create debugfs '%s' entry\n", name);
4939 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
4941 struct dentry *d_tracer;
4946 d_tracer = tracing_init_dentry_tr(tr);
4950 tr->options = debugfs_create_dir("options", d_tracer);
4952 pr_warning("Could not create debugfs directory 'options'\n");
4960 create_trace_option_file(struct trace_array *tr,
4961 struct trace_option_dentry *topt,
4962 struct tracer_flags *flags,
4963 struct tracer_opt *opt)
4965 struct dentry *t_options;
4967 t_options = trace_options_init_dentry(tr);
4971 topt->flags = flags;
4975 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4976 &trace_options_fops);
4980 static struct trace_option_dentry *
4981 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
4983 struct trace_option_dentry *topts;
4984 struct tracer_flags *flags;
4985 struct tracer_opt *opts;
4991 flags = tracer->flags;
4993 if (!flags || !flags->opts)
4998 for (cnt = 0; opts[cnt].name; cnt++)
5001 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
5005 for (cnt = 0; opts[cnt].name; cnt++)
5006 create_trace_option_file(tr, &topts[cnt], flags,
5013 destroy_trace_option_files(struct trace_option_dentry *topts)
5020 for (cnt = 0; topts[cnt].opt; cnt++) {
5021 if (topts[cnt].entry)
5022 debugfs_remove(topts[cnt].entry);
5028 static struct dentry *
5029 create_trace_option_core_file(struct trace_array *tr,
5030 const char *option, long index)
5032 struct dentry *t_options;
5034 t_options = trace_options_init_dentry(tr);
5038 return trace_create_file(option, 0644, t_options, (void *)index,
5039 &trace_options_core_fops);
5042 static __init void create_trace_options_dir(struct trace_array *tr)
5044 struct dentry *t_options;
5047 t_options = trace_options_init_dentry(tr);
5051 for (i = 0; trace_options[i]; i++)
5052 create_trace_option_core_file(tr, trace_options[i], i);
5056 rb_simple_read(struct file *filp, char __user *ubuf,
5057 size_t cnt, loff_t *ppos)
5059 struct trace_array *tr = filp->private_data;
5060 struct ring_buffer *buffer = tr->buffer;
5065 r = ring_buffer_record_is_on(buffer);
5069 r = sprintf(buf, "%d\n", r);
5071 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5075 rb_simple_write(struct file *filp, const char __user *ubuf,
5076 size_t cnt, loff_t *ppos)
5078 struct trace_array *tr = filp->private_data;
5079 struct ring_buffer *buffer = tr->buffer;
5083 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5088 mutex_lock(&trace_types_lock);
5090 ring_buffer_record_on(buffer);
5091 if (tr->current_trace->start)
5092 tr->current_trace->start(tr);
5094 ring_buffer_record_off(buffer);
5095 if (tr->current_trace->stop)
5096 tr->current_trace->stop(tr);
5098 mutex_unlock(&trace_types_lock);
5106 static const struct file_operations rb_simple_fops = {
5107 .open = tracing_open_generic,
5108 .read = rb_simple_read,
5109 .write = rb_simple_write,
5110 .llseek = default_llseek,
5114 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
5117 trace_create_file("trace_options", 0644, d_tracer,
5118 tr, &tracing_iter_fops);
5120 trace_create_file("trace", 0644, d_tracer,
5121 (void *)&tr->trace_cpu, &tracing_fops);
5123 trace_create_file("trace_pipe", 0444, d_tracer,
5124 (void *)&tr->trace_cpu, &tracing_pipe_fops);
5126 trace_create_file("buffer_size_kb", 0644, d_tracer,
5127 (void *)&tr->trace_cpu, &tracing_entries_fops);
5129 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
5130 tr, &tracing_total_entries_fops);
5132 trace_create_file("free_buffer", 0644, d_tracer,
5133 tr, &tracing_free_buffer_fops);
5135 trace_create_file("trace_marker", 0220, d_tracer,
5136 tr, &tracing_mark_fops);
5138 trace_create_file("trace_clock", 0644, d_tracer, tr,
5141 trace_create_file("tracing_on", 0644, d_tracer,
5142 tr, &rb_simple_fops);
5145 static __init int tracer_init_debugfs(void)
5147 struct dentry *d_tracer;
5150 trace_access_lock_init();
5152 d_tracer = tracing_init_dentry();
5154 init_tracer_debugfs(&global_trace, d_tracer);
5156 trace_create_file("tracing_cpumask", 0644, d_tracer,
5157 &global_trace, &tracing_cpumask_fops);
5159 trace_create_file("available_tracers", 0444, d_tracer,
5160 &global_trace, &show_traces_fops);
5162 trace_create_file("current_tracer", 0644, d_tracer,
5163 &global_trace, &set_tracer_fops);
5165 #ifdef CONFIG_TRACER_MAX_TRACE
5166 trace_create_file("tracing_max_latency", 0644, d_tracer,
5167 &tracing_max_latency, &tracing_max_lat_fops);
5170 trace_create_file("tracing_thresh", 0644, d_tracer,
5171 &tracing_thresh, &tracing_max_lat_fops);
5173 trace_create_file("README", 0444, d_tracer,
5174 NULL, &tracing_readme_fops);
5176 trace_create_file("saved_cmdlines", 0444, d_tracer,
5177 NULL, &tracing_saved_cmdlines_fops);
5179 #ifdef CONFIG_DYNAMIC_FTRACE
5180 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
5181 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
5184 #ifdef CONFIG_TRACER_SNAPSHOT
5185 trace_create_file("snapshot", 0644, d_tracer,
5186 (void *)&global_trace.trace_cpu, &snapshot_fops);
5189 create_trace_options_dir(&global_trace);
5191 for_each_tracing_cpu(cpu)
5192 tracing_init_debugfs_percpu(&global_trace, cpu);
5197 static int trace_panic_handler(struct notifier_block *this,
5198 unsigned long event, void *unused)
5200 if (ftrace_dump_on_oops)
5201 ftrace_dump(ftrace_dump_on_oops);
5205 static struct notifier_block trace_panic_notifier = {
5206 .notifier_call = trace_panic_handler,
5208 .priority = 150 /* priority: INT_MAX >= x >= 0 */
5211 static int trace_die_handler(struct notifier_block *self,
5217 if (ftrace_dump_on_oops)
5218 ftrace_dump(ftrace_dump_on_oops);
5226 static struct notifier_block trace_die_notifier = {
5227 .notifier_call = trace_die_handler,
5232 * printk is set to max of 1024, we really don't need it that big.
5233 * Nothing should be printing 1000 characters anyway.
5235 #define TRACE_MAX_PRINT 1000
5238 * Define here KERN_TRACE so that we have one place to modify
5239 * it if we decide to change what log level the ftrace dump
5242 #define KERN_TRACE KERN_EMERG
5245 trace_printk_seq(struct trace_seq *s)
5247 /* Probably should print a warning here. */
5251 /* should be zero ended, but we are paranoid. */
5252 s->buffer[s->len] = 0;
5254 printk(KERN_TRACE "%s", s->buffer);
5259 void trace_init_global_iter(struct trace_iterator *iter)
5261 iter->tr = &global_trace;
5262 iter->trace = iter->tr->current_trace;
5263 iter->cpu_file = RING_BUFFER_ALL_CPUS;
5267 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
5269 static arch_spinlock_t ftrace_dump_lock =
5270 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
5271 /* use static because iter can be a bit big for the stack */
5272 static struct trace_iterator iter;
5273 unsigned int old_userobj;
5274 static int dump_ran;
5275 unsigned long flags;
5279 local_irq_save(flags);
5280 arch_spin_lock(&ftrace_dump_lock);
5288 /* Did function tracer already get disabled? */
5289 if (ftrace_is_dead()) {
5290 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
5291 printk("# MAY BE MISSING FUNCTION EVENTS\n");
5294 if (disable_tracing)
5297 /* Simulate the iterator */
5298 trace_init_global_iter(&iter);
5300 for_each_tracing_cpu(cpu) {
5301 atomic_inc(&iter.tr->data[cpu]->disabled);
5304 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
5306 /* don't look at user memory in panic mode */
5307 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
5309 switch (oops_dump_mode) {
5311 iter.cpu_file = RING_BUFFER_ALL_CPUS;
5314 iter.cpu_file = raw_smp_processor_id();
5319 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
5320 iter.cpu_file = RING_BUFFER_ALL_CPUS;
5323 printk(KERN_TRACE "Dumping ftrace buffer:\n");
5326 * We need to stop all tracing on all CPUS to read the
5327 * the next buffer. This is a bit expensive, but is
5328 * not done often. We fill all what we can read,
5329 * and then release the locks again.
5332 while (!trace_empty(&iter)) {
5335 printk(KERN_TRACE "---------------------------------\n");
5339 /* reset all but tr, trace, and overruns */
5340 memset(&iter.seq, 0,
5341 sizeof(struct trace_iterator) -
5342 offsetof(struct trace_iterator, seq));
5343 iter.iter_flags |= TRACE_FILE_LAT_FMT;
5346 if (trace_find_next_entry_inc(&iter) != NULL) {
5349 ret = print_trace_line(&iter);
5350 if (ret != TRACE_TYPE_NO_CONSUME)
5351 trace_consume(&iter);
5353 touch_nmi_watchdog();
5355 trace_printk_seq(&iter.seq);
5359 printk(KERN_TRACE " (ftrace buffer empty)\n");
5361 printk(KERN_TRACE "---------------------------------\n");
5364 /* Re-enable tracing if requested */
5365 if (!disable_tracing) {
5366 trace_flags |= old_userobj;
5368 for_each_tracing_cpu(cpu) {
5369 atomic_dec(&iter.tr->data[cpu]->disabled);
5375 arch_spin_unlock(&ftrace_dump_lock);
5376 local_irq_restore(flags);
5379 /* By default: disable tracing after the dump */
5380 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
5382 __ftrace_dump(true, oops_dump_mode);
5384 EXPORT_SYMBOL_GPL(ftrace_dump);
5386 __init static int tracer_alloc_buffers(void)
5389 enum ring_buffer_flags rb_flags;
5394 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
5397 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
5398 goto out_free_buffer_mask;
5400 /* Only allocate trace_printk buffers if a trace_printk exists */
5401 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
5402 /* Must be called before global_trace.buffer is allocated */
5403 trace_printk_init_buffers();
5405 /* To save memory, keep the ring buffer size to its minimum */
5406 if (ring_buffer_expanded)
5407 ring_buf_size = trace_buf_size;
5411 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5413 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
5414 cpumask_copy(tracing_cpumask, cpu_all_mask);
5416 raw_spin_lock_init(&global_trace.start_lock);
5418 /* TODO: make the number of buffers hot pluggable with CPUS */
5419 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
5420 if (!global_trace.buffer) {
5421 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
5423 goto out_free_cpumask;
5425 if (global_trace.buffer_disabled)
5429 #ifdef CONFIG_TRACER_MAX_TRACE
5430 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
5431 raw_spin_lock_init(&max_tr.start_lock);
5432 if (!max_tr.buffer) {
5433 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
5435 ring_buffer_free(global_trace.buffer);
5436 goto out_free_cpumask;
5440 /* Allocate the first page for all buffers */
5441 for_each_tracing_cpu(i) {
5442 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
5443 global_trace.data[i]->trace_cpu.cpu = i;
5444 global_trace.data[i]->trace_cpu.tr = &global_trace;
5445 max_tr.data[i] = &per_cpu(max_tr_data, i);
5446 max_tr.data[i]->trace_cpu.cpu = i;
5447 max_tr.data[i]->trace_cpu.tr = &max_tr;
5450 set_buffer_entries(&global_trace,
5451 ring_buffer_size(global_trace.buffer, 0));
5452 #ifdef CONFIG_TRACER_MAX_TRACE
5453 set_buffer_entries(&max_tr, 1);
5456 trace_init_cmdlines();
5457 init_irq_work(&trace_work_wakeup, trace_wake_up);
5459 register_tracer(&nop_trace);
5461 global_trace.current_trace = &nop_trace;
5463 /* All seems OK, enable tracing */
5464 tracing_disabled = 0;
5466 atomic_notifier_chain_register(&panic_notifier_list,
5467 &trace_panic_notifier);
5469 register_die_notifier(&trace_die_notifier);
5471 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
5473 /* Holder for file callbacks */
5474 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5475 global_trace.trace_cpu.tr = &global_trace;
5477 INIT_LIST_HEAD(&global_trace.systems);
5478 INIT_LIST_HEAD(&global_trace.events);
5479 list_add(&global_trace.list, &ftrace_trace_arrays);
5481 while (trace_boot_options) {
5484 option = strsep(&trace_boot_options, ",");
5485 trace_set_options(&global_trace, option);
5491 free_cpumask_var(tracing_cpumask);
5492 out_free_buffer_mask:
5493 free_cpumask_var(tracing_buffer_mask);
5498 __init static int clear_boot_tracer(void)
5501 * The default tracer at boot buffer is an init section.
5502 * This function is called in lateinit. If we did not
5503 * find the boot tracer, then clear it out, to prevent
5504 * later registration from accessing the buffer that is
5505 * about to be freed.
5507 if (!default_bootup_tracer)
5510 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
5511 default_bootup_tracer);
5512 default_bootup_tracer = NULL;
5517 early_initcall(tracer_alloc_buffers);
5518 fs_initcall(tracer_init_debugfs);
5519 late_initcall(clear_boot_tracer);