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/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/rwsem.h>
35 #include <linux/slab.h>
36 #include <linux/ctype.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
39 #include <linux/nmi.h>
41 #include <linux/sched/rt.h>
44 #include "trace_output.h"
47 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
50 bool ring_buffer_expanded;
53 * We need to change this state when a selftest is running.
54 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
56 * insertions into the ring-buffer such as trace_printk could occurred
57 * at the same time, giving false positive or negative results.
59 static bool __read_mostly tracing_selftest_running;
62 * If a tracer is running, we do not want to run SELFTEST.
64 bool __read_mostly tracing_selftest_disabled;
66 /* For tracers that don't implement custom flags */
67 static struct tracer_opt dummy_tracer_opt[] = {
71 static struct tracer_flags dummy_tracer_flags = {
73 .opts = dummy_tracer_opt
76 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
82 * To prevent the comm cache from being overwritten when no
83 * tracing is active, only save the comm when a trace event
86 static DEFINE_PER_CPU(bool, trace_cmdline_save);
89 * Kill all tracing for good (never come back).
90 * It is initialized to 1 but will turn to zero if the initialization
91 * of the tracer is successful. But that is the only place that sets
94 static int tracing_disabled = 1;
96 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
98 cpumask_var_t __read_mostly tracing_buffer_mask;
101 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
103 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
104 * is set, then ftrace_dump is called. This will output the contents
105 * of the ftrace buffers to the console. This is very useful for
106 * capturing traces that lead to crashes and outputing it to a
109 * It is default off, but you can enable it with either specifying
110 * "ftrace_dump_on_oops" in the kernel command line, or setting
111 * /proc/sys/kernel/ftrace_dump_on_oops
112 * Set 1 if you want to dump buffers of all CPUs
113 * Set 2 if you want to dump the buffer of the CPU that triggered oops
116 enum ftrace_dump_mode ftrace_dump_on_oops;
118 static int tracing_set_tracer(const char *buf);
120 #define MAX_TRACER_SIZE 100
121 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
122 static char *default_bootup_tracer;
124 static bool allocate_snapshot;
126 static int __init set_cmdline_ftrace(char *str)
128 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
129 default_bootup_tracer = bootup_tracer_buf;
130 /* We are using ftrace early, expand it */
131 ring_buffer_expanded = true;
134 __setup("ftrace=", set_cmdline_ftrace);
136 static int __init set_ftrace_dump_on_oops(char *str)
138 if (*str++ != '=' || !*str) {
139 ftrace_dump_on_oops = DUMP_ALL;
143 if (!strcmp("orig_cpu", str)) {
144 ftrace_dump_on_oops = DUMP_ORIG;
150 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
152 static int __init alloc_snapshot(char *str)
154 allocate_snapshot = true;
155 /* We also need the main ring buffer expanded */
156 ring_buffer_expanded = true;
159 __setup("alloc_snapshot", alloc_snapshot);
162 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
163 static char *trace_boot_options __initdata;
165 static int __init set_trace_boot_options(char *str)
167 strncpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
168 trace_boot_options = trace_boot_options_buf;
171 __setup("trace_options=", set_trace_boot_options);
173 unsigned long long ns2usecs(cycle_t nsec)
181 * The global_trace is the descriptor that holds the tracing
182 * buffers for the live tracing. For each CPU, it contains
183 * a link list of pages that will store trace entries. The
184 * page descriptor of the pages in the memory is used to hold
185 * the link list by linking the lru item in the page descriptor
186 * to each of the pages in the buffer per CPU.
188 * For each active CPU there is a data field that holds the
189 * pages for the buffer for that CPU. Each CPU has the same number
190 * of pages allocated for its buffer.
192 static struct trace_array global_trace;
194 LIST_HEAD(ftrace_trace_arrays);
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.trace_buffer.buffer)
210 return trace_clock_local();
212 ts = ring_buffer_time_stamp(global_trace.trace_buffer.buffer, cpu);
213 ring_buffer_normalize_time_stamp(global_trace.trace_buffer.buffer, cpu, &ts);
218 int tracing_is_enabled(void)
220 return tracing_is_on();
224 * trace_buf_size is the size in bytes that is allocated
225 * for a buffer. Note, the number of bytes is always rounded
228 * This number is purposely set to a low number of 16384.
229 * If the dump on oops happens, it will be much appreciated
230 * to not have to wait for all that output. Anyway this can be
231 * boot time and run time configurable.
233 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
235 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
237 /* trace_types holds a link list of available tracers. */
238 static struct tracer *trace_types __read_mostly;
241 * trace_types_lock is used to protect the trace_types list.
243 static DEFINE_MUTEX(trace_types_lock);
246 * serialize the access of the ring buffer
248 * ring buffer serializes readers, but it is low level protection.
249 * The validity of the events (which returns by ring_buffer_peek() ..etc)
250 * are not protected by ring buffer.
252 * The content of events may become garbage if we allow other process consumes
253 * these events concurrently:
254 * A) the page of the consumed events may become a normal page
255 * (not reader page) in ring buffer, and this page will be rewrited
256 * by events producer.
257 * B) The page of the consumed events may become a page for splice_read,
258 * and this page will be returned to system.
260 * These primitives allow multi process access to different cpu ring buffer
263 * These primitives don't distinguish read-only and read-consume access.
264 * Multi read-only access are also serialized.
268 static DECLARE_RWSEM(all_cpu_access_lock);
269 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
271 static inline void trace_access_lock(int cpu)
273 if (cpu == RING_BUFFER_ALL_CPUS) {
274 /* gain it for accessing the whole ring buffer. */
275 down_write(&all_cpu_access_lock);
277 /* gain it for accessing a cpu ring buffer. */
279 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
280 down_read(&all_cpu_access_lock);
282 /* Secondly block other access to this @cpu ring buffer. */
283 mutex_lock(&per_cpu(cpu_access_lock, cpu));
287 static inline void trace_access_unlock(int cpu)
289 if (cpu == RING_BUFFER_ALL_CPUS) {
290 up_write(&all_cpu_access_lock);
292 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
293 up_read(&all_cpu_access_lock);
297 static inline void trace_access_lock_init(void)
301 for_each_possible_cpu(cpu)
302 mutex_init(&per_cpu(cpu_access_lock, cpu));
307 static DEFINE_MUTEX(access_lock);
309 static inline void trace_access_lock(int cpu)
312 mutex_lock(&access_lock);
315 static inline void trace_access_unlock(int cpu)
318 mutex_unlock(&access_lock);
321 static inline void trace_access_lock_init(void)
327 /* trace_flags holds trace_options default values */
328 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
329 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
330 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
331 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS;
334 * tracing_on - enable tracing buffers
336 * This function enables tracing buffers that may have been
337 * disabled with tracing_off.
339 void tracing_on(void)
341 if (global_trace.trace_buffer.buffer)
342 ring_buffer_record_on(global_trace.trace_buffer.buffer);
344 * This flag is only looked at when buffers haven't been
345 * allocated yet. We don't really care about the race
346 * between setting this flag and actually turning
349 global_trace.buffer_disabled = 0;
351 EXPORT_SYMBOL_GPL(tracing_on);
353 #ifdef CONFIG_TRACER_SNAPSHOT
355 * trace_snapshot - take a snapshot of the current buffer.
357 * This causes a swap between the snapshot buffer and the current live
358 * tracing buffer. You can use this to take snapshots of the live
359 * trace when some condition is triggered, but continue to trace.
361 * Note, make sure to allocate the snapshot with either
362 * a tracing_snapshot_alloc(), or by doing it manually
363 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
365 * If the snapshot buffer is not allocated, it will stop tracing.
366 * Basically making a permanent snapshot.
368 void tracing_snapshot(void)
370 struct trace_array *tr = &global_trace;
371 struct tracer *tracer = tr->current_trace;
374 if (!tr->allocated_snapshot) {
375 trace_printk("*** SNAPSHOT NOT ALLOCATED ***\n");
376 trace_printk("*** stopping trace here! ***\n");
381 /* Note, snapshot can not be used when the tracer uses it */
382 if (tracer->use_max_tr) {
383 trace_printk("*** LATENCY TRACER ACTIVE ***\n");
384 trace_printk("*** Can not use snapshot (sorry) ***\n");
388 local_irq_save(flags);
389 update_max_tr(tr, current, smp_processor_id());
390 local_irq_restore(flags);
393 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
394 struct trace_buffer *size_buf, int cpu_id);
397 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
399 * This is similar to trace_snapshot(), but it will allocate the
400 * snapshot buffer if it isn't already allocated. Use this only
401 * where it is safe to sleep, as the allocation may sleep.
403 * This causes a swap between the snapshot buffer and the current live
404 * tracing buffer. You can use this to take snapshots of the live
405 * trace when some condition is triggered, but continue to trace.
407 void tracing_snapshot_alloc(void)
409 struct trace_array *tr = &global_trace;
412 if (!tr->allocated_snapshot) {
414 /* allocate spare buffer */
415 ret = resize_buffer_duplicate_size(&tr->max_buffer,
416 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
417 if (WARN_ON(ret < 0))
420 tr->allocated_snapshot = true;
426 void tracing_snapshot(void)
428 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
430 void tracing_snapshot_alloc(void)
435 #endif /* CONFIG_TRACER_SNAPSHOT */
438 * tracing_off - turn off tracing buffers
440 * This function stops the tracing buffers from recording data.
441 * It does not disable any overhead the tracers themselves may
442 * be causing. This function simply causes all recording to
443 * the ring buffers to fail.
445 void tracing_off(void)
447 if (global_trace.trace_buffer.buffer)
448 ring_buffer_record_off(global_trace.trace_buffer.buffer);
450 * This flag is only looked at when buffers haven't been
451 * allocated yet. We don't really care about the race
452 * between setting this flag and actually turning
455 global_trace.buffer_disabled = 1;
457 EXPORT_SYMBOL_GPL(tracing_off);
460 * tracing_is_on - show state of ring buffers enabled
462 int tracing_is_on(void)
464 if (global_trace.trace_buffer.buffer)
465 return ring_buffer_record_is_on(global_trace.trace_buffer.buffer);
466 return !global_trace.buffer_disabled;
468 EXPORT_SYMBOL_GPL(tracing_is_on);
470 static int __init set_buf_size(char *str)
472 unsigned long buf_size;
476 buf_size = memparse(str, &str);
477 /* nr_entries can not be zero */
480 trace_buf_size = buf_size;
483 __setup("trace_buf_size=", set_buf_size);
485 static int __init set_tracing_thresh(char *str)
487 unsigned long threshold;
492 ret = kstrtoul(str, 0, &threshold);
495 tracing_thresh = threshold * 1000;
498 __setup("tracing_thresh=", set_tracing_thresh);
500 unsigned long nsecs_to_usecs(unsigned long nsecs)
505 /* These must match the bit postions in trace_iterator_flags */
506 static const char *trace_options[] = {
538 int in_ns; /* is this clock in nanoseconds? */
540 { trace_clock_local, "local", 1 },
541 { trace_clock_global, "global", 1 },
542 { trace_clock_counter, "counter", 0 },
549 * trace_parser_get_init - gets the buffer for trace parser
551 int trace_parser_get_init(struct trace_parser *parser, int size)
553 memset(parser, 0, sizeof(*parser));
555 parser->buffer = kmalloc(size, GFP_KERNEL);
564 * trace_parser_put - frees the buffer for trace parser
566 void trace_parser_put(struct trace_parser *parser)
568 kfree(parser->buffer);
572 * trace_get_user - reads the user input string separated by space
573 * (matched by isspace(ch))
575 * For each string found the 'struct trace_parser' is updated,
576 * and the function returns.
578 * Returns number of bytes read.
580 * See kernel/trace/trace.h for 'struct trace_parser' details.
582 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
583 size_t cnt, loff_t *ppos)
590 trace_parser_clear(parser);
592 ret = get_user(ch, ubuf++);
600 * The parser is not finished with the last write,
601 * continue reading the user input without skipping spaces.
604 /* skip white space */
605 while (cnt && isspace(ch)) {
606 ret = get_user(ch, ubuf++);
613 /* only spaces were written */
623 /* read the non-space input */
624 while (cnt && !isspace(ch)) {
625 if (parser->idx < parser->size - 1)
626 parser->buffer[parser->idx++] = ch;
631 ret = get_user(ch, ubuf++);
638 /* We either got finished input or we have to wait for another call. */
640 parser->buffer[parser->idx] = 0;
641 parser->cont = false;
644 parser->buffer[parser->idx++] = ch;
654 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
662 if (s->len <= s->readpos)
665 len = s->len - s->readpos;
668 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
678 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
682 if (s->len <= s->readpos)
685 len = s->len - s->readpos;
688 memcpy(buf, s->buffer + s->readpos, cnt);
695 * ftrace_max_lock is used to protect the swapping of buffers
696 * when taking a max snapshot. The buffers themselves are
697 * protected by per_cpu spinlocks. But the action of the swap
698 * needs its own lock.
700 * This is defined as a arch_spinlock_t in order to help
701 * with performance when lockdep debugging is enabled.
703 * It is also used in other places outside the update_max_tr
704 * so it needs to be defined outside of the
705 * CONFIG_TRACER_MAX_TRACE.
707 static arch_spinlock_t ftrace_max_lock =
708 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
710 unsigned long __read_mostly tracing_thresh;
712 #ifdef CONFIG_TRACER_MAX_TRACE
713 unsigned long __read_mostly tracing_max_latency;
716 * Copy the new maximum trace into the separate maximum-trace
717 * structure. (this way the maximum trace is permanently saved,
718 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
721 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
723 struct trace_buffer *trace_buf = &tr->trace_buffer;
724 struct trace_buffer *max_buf = &tr->max_buffer;
725 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
726 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
729 max_buf->time_start = data->preempt_timestamp;
731 max_data->saved_latency = tracing_max_latency;
732 max_data->critical_start = data->critical_start;
733 max_data->critical_end = data->critical_end;
735 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
736 max_data->pid = tsk->pid;
737 max_data->uid = task_uid(tsk);
738 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
739 max_data->policy = tsk->policy;
740 max_data->rt_priority = tsk->rt_priority;
742 /* record this tasks comm */
743 tracing_record_cmdline(tsk);
747 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
749 * @tsk: the task with the latency
750 * @cpu: The cpu that initiated the trace.
752 * Flip the buffers between the @tr and the max_tr and record information
753 * about which task was the cause of this latency.
756 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
758 struct ring_buffer *buf;
763 WARN_ON_ONCE(!irqs_disabled());
765 if (!tr->allocated_snapshot) {
766 /* Only the nop tracer should hit this when disabling */
767 WARN_ON_ONCE(tr->current_trace != &nop_trace);
771 arch_spin_lock(&ftrace_max_lock);
773 buf = tr->trace_buffer.buffer;
774 tr->trace_buffer.buffer = tr->max_buffer.buffer;
775 tr->max_buffer.buffer = buf;
777 __update_max_tr(tr, tsk, cpu);
778 arch_spin_unlock(&ftrace_max_lock);
782 * update_max_tr_single - only copy one trace over, and reset the rest
784 * @tsk - task with the latency
785 * @cpu - the cpu of the buffer to copy.
787 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
790 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
797 WARN_ON_ONCE(!irqs_disabled());
798 if (WARN_ON_ONCE(!tr->allocated_snapshot))
801 arch_spin_lock(&ftrace_max_lock);
803 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
807 * We failed to swap the buffer due to a commit taking
808 * place on this CPU. We fail to record, but we reset
809 * the max trace buffer (no one writes directly to it)
810 * and flag that it failed.
812 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
813 "Failed to swap buffers due to commit in progress\n");
816 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
818 __update_max_tr(tr, tsk, cpu);
819 arch_spin_unlock(&ftrace_max_lock);
821 #endif /* CONFIG_TRACER_MAX_TRACE */
823 static void default_wait_pipe(struct trace_iterator *iter)
825 /* Iterators are static, they should be filled or empty */
826 if (trace_buffer_iter(iter, iter->cpu_file))
829 ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
832 #ifdef CONFIG_FTRACE_STARTUP_TEST
833 static int run_tracer_selftest(struct tracer *type)
835 struct trace_array *tr = &global_trace;
836 struct tracer *saved_tracer = tr->current_trace;
839 if (!type->selftest || tracing_selftest_disabled)
843 * Run a selftest on this tracer.
844 * Here we reset the trace buffer, and set the current
845 * tracer to be this tracer. The tracer can then run some
846 * internal tracing to verify that everything is in order.
847 * If we fail, we do not register this tracer.
849 tracing_reset_online_cpus(&tr->trace_buffer);
851 tr->current_trace = type;
853 #ifdef CONFIG_TRACER_MAX_TRACE
854 if (type->use_max_tr) {
855 /* If we expanded the buffers, make sure the max is expanded too */
856 if (ring_buffer_expanded)
857 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
858 RING_BUFFER_ALL_CPUS);
859 tr->allocated_snapshot = true;
863 /* the test is responsible for initializing and enabling */
864 pr_info("Testing tracer %s: ", type->name);
865 ret = type->selftest(type, tr);
866 /* the test is responsible for resetting too */
867 tr->current_trace = saved_tracer;
869 printk(KERN_CONT "FAILED!\n");
870 /* Add the warning after printing 'FAILED' */
874 /* Only reset on passing, to avoid touching corrupted buffers */
875 tracing_reset_online_cpus(&tr->trace_buffer);
877 #ifdef CONFIG_TRACER_MAX_TRACE
878 if (type->use_max_tr) {
879 tr->allocated_snapshot = false;
881 /* Shrink the max buffer again */
882 if (ring_buffer_expanded)
883 ring_buffer_resize(tr->max_buffer.buffer, 1,
884 RING_BUFFER_ALL_CPUS);
888 printk(KERN_CONT "PASSED\n");
892 static inline int run_tracer_selftest(struct tracer *type)
896 #endif /* CONFIG_FTRACE_STARTUP_TEST */
899 * register_tracer - register a tracer with the ftrace system.
900 * @type - the plugin for the tracer
902 * Register a new plugin tracer.
904 int register_tracer(struct tracer *type)
910 pr_info("Tracer must have a name\n");
914 if (strlen(type->name) >= MAX_TRACER_SIZE) {
915 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
919 mutex_lock(&trace_types_lock);
921 tracing_selftest_running = true;
923 for (t = trace_types; t; t = t->next) {
924 if (strcmp(type->name, t->name) == 0) {
926 pr_info("Tracer %s already registered\n",
934 type->set_flag = &dummy_set_flag;
936 type->flags = &dummy_tracer_flags;
938 if (!type->flags->opts)
939 type->flags->opts = dummy_tracer_opt;
940 if (!type->wait_pipe)
941 type->wait_pipe = default_wait_pipe;
943 ret = run_tracer_selftest(type);
947 type->next = trace_types;
951 tracing_selftest_running = false;
952 mutex_unlock(&trace_types_lock);
954 if (ret || !default_bootup_tracer)
957 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
960 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
961 /* Do we want this tracer to start on bootup? */
962 tracing_set_tracer(type->name);
963 default_bootup_tracer = NULL;
964 /* disable other selftests, since this will break it. */
965 tracing_selftest_disabled = true;
966 #ifdef CONFIG_FTRACE_STARTUP_TEST
967 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
975 void tracing_reset(struct trace_buffer *buf, int cpu)
977 struct ring_buffer *buffer = buf->buffer;
982 ring_buffer_record_disable(buffer);
984 /* Make sure all commits have finished */
986 ring_buffer_reset_cpu(buffer, cpu);
988 ring_buffer_record_enable(buffer);
991 void tracing_reset_online_cpus(struct trace_buffer *buf)
993 struct ring_buffer *buffer = buf->buffer;
999 ring_buffer_record_disable(buffer);
1001 /* Make sure all commits have finished */
1002 synchronize_sched();
1004 buf->time_start = ftrace_now(buf->cpu);
1006 for_each_online_cpu(cpu)
1007 ring_buffer_reset_cpu(buffer, cpu);
1009 ring_buffer_record_enable(buffer);
1012 void tracing_reset_current(int cpu)
1014 tracing_reset(&global_trace.trace_buffer, cpu);
1017 void tracing_reset_all_online_cpus(void)
1019 struct trace_array *tr;
1021 mutex_lock(&trace_types_lock);
1022 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1023 tracing_reset_online_cpus(&tr->trace_buffer);
1024 #ifdef CONFIG_TRACER_MAX_TRACE
1025 tracing_reset_online_cpus(&tr->max_buffer);
1028 mutex_unlock(&trace_types_lock);
1031 #define SAVED_CMDLINES 128
1032 #define NO_CMDLINE_MAP UINT_MAX
1033 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1034 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1035 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1036 static int cmdline_idx;
1037 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1039 /* temporary disable recording */
1040 static atomic_t trace_record_cmdline_disabled __read_mostly;
1042 static void trace_init_cmdlines(void)
1044 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1045 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
1049 int is_tracing_stopped(void)
1051 return global_trace.stop_count;
1055 * ftrace_off_permanent - disable all ftrace code permanently
1057 * This should only be called when a serious anomally has
1058 * been detected. This will turn off the function tracing,
1059 * ring buffers, and other tracing utilites. It takes no
1060 * locks and can be called from any context.
1062 void ftrace_off_permanent(void)
1064 tracing_disabled = 1;
1066 tracing_off_permanent();
1070 * tracing_start - quick start of the tracer
1072 * If tracing is enabled but was stopped by tracing_stop,
1073 * this will start the tracer back up.
1075 void tracing_start(void)
1077 struct ring_buffer *buffer;
1078 unsigned long flags;
1080 if (tracing_disabled)
1083 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1084 if (--global_trace.stop_count) {
1085 if (global_trace.stop_count < 0) {
1086 /* Someone screwed up their debugging */
1088 global_trace.stop_count = 0;
1093 /* Prevent the buffers from switching */
1094 arch_spin_lock(&ftrace_max_lock);
1096 buffer = global_trace.trace_buffer.buffer;
1098 ring_buffer_record_enable(buffer);
1100 #ifdef CONFIG_TRACER_MAX_TRACE
1101 buffer = global_trace.max_buffer.buffer;
1103 ring_buffer_record_enable(buffer);
1106 arch_spin_unlock(&ftrace_max_lock);
1110 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1113 static void tracing_start_tr(struct trace_array *tr)
1115 struct ring_buffer *buffer;
1116 unsigned long flags;
1118 if (tracing_disabled)
1121 /* If global, we need to also start the max tracer */
1122 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1123 return tracing_start();
1125 raw_spin_lock_irqsave(&tr->start_lock, flags);
1127 if (--tr->stop_count) {
1128 if (tr->stop_count < 0) {
1129 /* Someone screwed up their debugging */
1136 buffer = tr->trace_buffer.buffer;
1138 ring_buffer_record_enable(buffer);
1141 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1145 * tracing_stop - quick stop of the tracer
1147 * Light weight way to stop tracing. Use in conjunction with
1150 void tracing_stop(void)
1152 struct ring_buffer *buffer;
1153 unsigned long flags;
1156 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1157 if (global_trace.stop_count++)
1160 /* Prevent the buffers from switching */
1161 arch_spin_lock(&ftrace_max_lock);
1163 buffer = global_trace.trace_buffer.buffer;
1165 ring_buffer_record_disable(buffer);
1167 #ifdef CONFIG_TRACER_MAX_TRACE
1168 buffer = global_trace.max_buffer.buffer;
1170 ring_buffer_record_disable(buffer);
1173 arch_spin_unlock(&ftrace_max_lock);
1176 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1179 static void tracing_stop_tr(struct trace_array *tr)
1181 struct ring_buffer *buffer;
1182 unsigned long flags;
1184 /* If global, we need to also stop the max tracer */
1185 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1186 return tracing_stop();
1188 raw_spin_lock_irqsave(&tr->start_lock, flags);
1189 if (tr->stop_count++)
1192 buffer = tr->trace_buffer.buffer;
1194 ring_buffer_record_disable(buffer);
1197 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1200 void trace_stop_cmdline_recording(void);
1202 static void trace_save_cmdline(struct task_struct *tsk)
1206 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1210 * It's not the end of the world if we don't get
1211 * the lock, but we also don't want to spin
1212 * nor do we want to disable interrupts,
1213 * so if we miss here, then better luck next time.
1215 if (!arch_spin_trylock(&trace_cmdline_lock))
1218 idx = map_pid_to_cmdline[tsk->pid];
1219 if (idx == NO_CMDLINE_MAP) {
1220 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1223 * Check whether the cmdline buffer at idx has a pid
1224 * mapped. We are going to overwrite that entry so we
1225 * need to clear the map_pid_to_cmdline. Otherwise we
1226 * would read the new comm for the old pid.
1228 pid = map_cmdline_to_pid[idx];
1229 if (pid != NO_CMDLINE_MAP)
1230 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1232 map_cmdline_to_pid[idx] = tsk->pid;
1233 map_pid_to_cmdline[tsk->pid] = idx;
1238 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1240 arch_spin_unlock(&trace_cmdline_lock);
1243 void trace_find_cmdline(int pid, char comm[])
1248 strcpy(comm, "<idle>");
1252 if (WARN_ON_ONCE(pid < 0)) {
1253 strcpy(comm, "<XXX>");
1257 if (pid > PID_MAX_DEFAULT) {
1258 strcpy(comm, "<...>");
1263 arch_spin_lock(&trace_cmdline_lock);
1264 map = map_pid_to_cmdline[pid];
1265 if (map != NO_CMDLINE_MAP)
1266 strcpy(comm, saved_cmdlines[map]);
1268 strcpy(comm, "<...>");
1270 arch_spin_unlock(&trace_cmdline_lock);
1274 void tracing_record_cmdline(struct task_struct *tsk)
1276 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1279 if (!__this_cpu_read(trace_cmdline_save))
1282 __this_cpu_write(trace_cmdline_save, false);
1284 trace_save_cmdline(tsk);
1288 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1291 struct task_struct *tsk = current;
1293 entry->preempt_count = pc & 0xff;
1294 entry->pid = (tsk) ? tsk->pid : 0;
1296 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1297 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1299 TRACE_FLAG_IRQS_NOSUPPORT |
1301 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1302 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1303 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1305 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1307 struct ring_buffer_event *
1308 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1311 unsigned long flags, int pc)
1313 struct ring_buffer_event *event;
1315 event = ring_buffer_lock_reserve(buffer, len);
1316 if (event != NULL) {
1317 struct trace_entry *ent = ring_buffer_event_data(event);
1319 tracing_generic_entry_update(ent, flags, pc);
1327 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1329 __this_cpu_write(trace_cmdline_save, true);
1330 ring_buffer_unlock_commit(buffer, event);
1334 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1335 struct ring_buffer_event *event,
1336 unsigned long flags, int pc)
1338 __buffer_unlock_commit(buffer, event);
1340 ftrace_trace_stack(buffer, flags, 6, pc);
1341 ftrace_trace_userstack(buffer, flags, pc);
1344 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1345 struct ring_buffer_event *event,
1346 unsigned long flags, int pc)
1348 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1350 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1352 struct ring_buffer_event *
1353 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1354 struct ftrace_event_file *ftrace_file,
1355 int type, unsigned long len,
1356 unsigned long flags, int pc)
1358 *current_rb = ftrace_file->tr->trace_buffer.buffer;
1359 return trace_buffer_lock_reserve(*current_rb,
1360 type, len, flags, pc);
1362 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1364 struct ring_buffer_event *
1365 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1366 int type, unsigned long len,
1367 unsigned long flags, int pc)
1369 *current_rb = global_trace.trace_buffer.buffer;
1370 return trace_buffer_lock_reserve(*current_rb,
1371 type, len, flags, pc);
1373 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1375 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1376 struct ring_buffer_event *event,
1377 unsigned long flags, int pc)
1379 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1381 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1383 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1384 struct ring_buffer_event *event,
1385 unsigned long flags, int pc,
1386 struct pt_regs *regs)
1388 __buffer_unlock_commit(buffer, event);
1390 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1391 ftrace_trace_userstack(buffer, flags, pc);
1393 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1395 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1396 struct ring_buffer_event *event)
1398 ring_buffer_discard_commit(buffer, event);
1400 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1403 trace_function(struct trace_array *tr,
1404 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1407 struct ftrace_event_call *call = &event_function;
1408 struct ring_buffer *buffer = tr->trace_buffer.buffer;
1409 struct ring_buffer_event *event;
1410 struct ftrace_entry *entry;
1412 /* If we are reading the ring buffer, don't trace */
1413 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1416 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1420 entry = ring_buffer_event_data(event);
1422 entry->parent_ip = parent_ip;
1424 if (!filter_check_discard(call, entry, buffer, event))
1425 __buffer_unlock_commit(buffer, event);
1429 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1430 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1433 if (likely(!atomic_read(&data->disabled)))
1434 trace_function(tr, ip, parent_ip, flags, pc);
1437 #ifdef CONFIG_STACKTRACE
1439 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1440 struct ftrace_stack {
1441 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1444 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1445 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1447 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1448 unsigned long flags,
1449 int skip, int pc, struct pt_regs *regs)
1451 struct ftrace_event_call *call = &event_kernel_stack;
1452 struct ring_buffer_event *event;
1453 struct stack_entry *entry;
1454 struct stack_trace trace;
1456 int size = FTRACE_STACK_ENTRIES;
1458 trace.nr_entries = 0;
1462 * Since events can happen in NMIs there's no safe way to
1463 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1464 * or NMI comes in, it will just have to use the default
1465 * FTRACE_STACK_SIZE.
1467 preempt_disable_notrace();
1469 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1471 * We don't need any atomic variables, just a barrier.
1472 * If an interrupt comes in, we don't care, because it would
1473 * have exited and put the counter back to what we want.
1474 * We just need a barrier to keep gcc from moving things
1478 if (use_stack == 1) {
1479 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1480 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1483 save_stack_trace_regs(regs, &trace);
1485 save_stack_trace(&trace);
1487 if (trace.nr_entries > size)
1488 size = trace.nr_entries;
1490 /* From now on, use_stack is a boolean */
1493 size *= sizeof(unsigned long);
1495 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1496 sizeof(*entry) + size, flags, pc);
1499 entry = ring_buffer_event_data(event);
1501 memset(&entry->caller, 0, size);
1504 memcpy(&entry->caller, trace.entries,
1505 trace.nr_entries * sizeof(unsigned long));
1507 trace.max_entries = FTRACE_STACK_ENTRIES;
1508 trace.entries = entry->caller;
1510 save_stack_trace_regs(regs, &trace);
1512 save_stack_trace(&trace);
1515 entry->size = trace.nr_entries;
1517 if (!filter_check_discard(call, entry, buffer, event))
1518 __buffer_unlock_commit(buffer, event);
1521 /* Again, don't let gcc optimize things here */
1523 __this_cpu_dec(ftrace_stack_reserve);
1524 preempt_enable_notrace();
1528 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1529 int skip, int pc, struct pt_regs *regs)
1531 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1534 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1537 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1540 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1543 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1546 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1549 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
1553 * trace_dump_stack - record a stack back trace in the trace buffer
1555 void trace_dump_stack(void)
1557 unsigned long flags;
1559 if (tracing_disabled || tracing_selftest_running)
1562 local_save_flags(flags);
1564 /* skipping 3 traces, seems to get us at the caller of this function */
1565 __ftrace_trace_stack(global_trace.trace_buffer.buffer, flags, 3,
1566 preempt_count(), NULL);
1569 static DEFINE_PER_CPU(int, user_stack_count);
1572 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1574 struct ftrace_event_call *call = &event_user_stack;
1575 struct ring_buffer_event *event;
1576 struct userstack_entry *entry;
1577 struct stack_trace trace;
1579 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1583 * NMIs can not handle page faults, even with fix ups.
1584 * The save user stack can (and often does) fault.
1586 if (unlikely(in_nmi()))
1590 * prevent recursion, since the user stack tracing may
1591 * trigger other kernel events.
1594 if (__this_cpu_read(user_stack_count))
1597 __this_cpu_inc(user_stack_count);
1599 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1600 sizeof(*entry), flags, pc);
1602 goto out_drop_count;
1603 entry = ring_buffer_event_data(event);
1605 entry->tgid = current->tgid;
1606 memset(&entry->caller, 0, sizeof(entry->caller));
1608 trace.nr_entries = 0;
1609 trace.max_entries = FTRACE_STACK_ENTRIES;
1611 trace.entries = entry->caller;
1613 save_stack_trace_user(&trace);
1614 if (!filter_check_discard(call, entry, buffer, event))
1615 __buffer_unlock_commit(buffer, event);
1618 __this_cpu_dec(user_stack_count);
1624 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1626 ftrace_trace_userstack(tr, flags, preempt_count());
1630 #endif /* CONFIG_STACKTRACE */
1632 /* created for use with alloc_percpu */
1633 struct trace_buffer_struct {
1634 char buffer[TRACE_BUF_SIZE];
1637 static struct trace_buffer_struct *trace_percpu_buffer;
1638 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1639 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1640 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1643 * The buffer used is dependent on the context. There is a per cpu
1644 * buffer for normal context, softirq contex, hard irq context and
1645 * for NMI context. Thise allows for lockless recording.
1647 * Note, if the buffers failed to be allocated, then this returns NULL
1649 static char *get_trace_buf(void)
1651 struct trace_buffer_struct *percpu_buffer;
1654 * If we have allocated per cpu buffers, then we do not
1655 * need to do any locking.
1658 percpu_buffer = trace_percpu_nmi_buffer;
1660 percpu_buffer = trace_percpu_irq_buffer;
1661 else if (in_softirq())
1662 percpu_buffer = trace_percpu_sirq_buffer;
1664 percpu_buffer = trace_percpu_buffer;
1669 return this_cpu_ptr(&percpu_buffer->buffer[0]);
1672 static int alloc_percpu_trace_buffer(void)
1674 struct trace_buffer_struct *buffers;
1675 struct trace_buffer_struct *sirq_buffers;
1676 struct trace_buffer_struct *irq_buffers;
1677 struct trace_buffer_struct *nmi_buffers;
1679 buffers = alloc_percpu(struct trace_buffer_struct);
1683 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1687 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1691 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1695 trace_percpu_buffer = buffers;
1696 trace_percpu_sirq_buffer = sirq_buffers;
1697 trace_percpu_irq_buffer = irq_buffers;
1698 trace_percpu_nmi_buffer = nmi_buffers;
1703 free_percpu(irq_buffers);
1705 free_percpu(sirq_buffers);
1707 free_percpu(buffers);
1709 WARN(1, "Could not allocate percpu trace_printk buffer");
1713 static int buffers_allocated;
1715 void trace_printk_init_buffers(void)
1717 if (buffers_allocated)
1720 if (alloc_percpu_trace_buffer())
1723 pr_info("ftrace: Allocated trace_printk buffers\n");
1725 /* Expand the buffers to set size */
1726 tracing_update_buffers();
1728 buffers_allocated = 1;
1731 * trace_printk_init_buffers() can be called by modules.
1732 * If that happens, then we need to start cmdline recording
1733 * directly here. If the global_trace.buffer is already
1734 * allocated here, then this was called by module code.
1736 if (global_trace.trace_buffer.buffer)
1737 tracing_start_cmdline_record();
1740 void trace_printk_start_comm(void)
1742 /* Start tracing comms if trace printk is set */
1743 if (!buffers_allocated)
1745 tracing_start_cmdline_record();
1748 static void trace_printk_start_stop_comm(int enabled)
1750 if (!buffers_allocated)
1754 tracing_start_cmdline_record();
1756 tracing_stop_cmdline_record();
1760 * trace_vbprintk - write binary msg to tracing buffer
1763 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1765 struct ftrace_event_call *call = &event_bprint;
1766 struct ring_buffer_event *event;
1767 struct ring_buffer *buffer;
1768 struct trace_array *tr = &global_trace;
1769 struct bprint_entry *entry;
1770 unsigned long flags;
1772 int len = 0, size, pc;
1774 if (unlikely(tracing_selftest_running || tracing_disabled))
1777 /* Don't pollute graph traces with trace_vprintk internals */
1778 pause_graph_tracing();
1780 pc = preempt_count();
1781 preempt_disable_notrace();
1783 tbuffer = get_trace_buf();
1789 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1791 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1794 local_save_flags(flags);
1795 size = sizeof(*entry) + sizeof(u32) * len;
1796 buffer = tr->trace_buffer.buffer;
1797 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1801 entry = ring_buffer_event_data(event);
1805 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1806 if (!filter_check_discard(call, entry, buffer, event)) {
1807 __buffer_unlock_commit(buffer, event);
1808 ftrace_trace_stack(buffer, flags, 6, pc);
1812 preempt_enable_notrace();
1813 unpause_graph_tracing();
1817 EXPORT_SYMBOL_GPL(trace_vbprintk);
1820 __trace_array_vprintk(struct ring_buffer *buffer,
1821 unsigned long ip, const char *fmt, va_list args)
1823 struct ftrace_event_call *call = &event_print;
1824 struct ring_buffer_event *event;
1825 int len = 0, size, pc;
1826 struct print_entry *entry;
1827 unsigned long flags;
1830 if (tracing_disabled || tracing_selftest_running)
1833 /* Don't pollute graph traces with trace_vprintk internals */
1834 pause_graph_tracing();
1836 pc = preempt_count();
1837 preempt_disable_notrace();
1840 tbuffer = get_trace_buf();
1846 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1847 if (len > TRACE_BUF_SIZE)
1850 local_save_flags(flags);
1851 size = sizeof(*entry) + len + 1;
1852 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1856 entry = ring_buffer_event_data(event);
1859 memcpy(&entry->buf, tbuffer, len);
1860 entry->buf[len] = '\0';
1861 if (!filter_check_discard(call, entry, buffer, event)) {
1862 __buffer_unlock_commit(buffer, event);
1863 ftrace_trace_stack(buffer, flags, 6, pc);
1866 preempt_enable_notrace();
1867 unpause_graph_tracing();
1872 int trace_array_vprintk(struct trace_array *tr,
1873 unsigned long ip, const char *fmt, va_list args)
1875 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
1878 int trace_array_printk(struct trace_array *tr,
1879 unsigned long ip, const char *fmt, ...)
1884 if (!(trace_flags & TRACE_ITER_PRINTK))
1888 ret = trace_array_vprintk(tr, ip, fmt, ap);
1893 int trace_array_printk_buf(struct ring_buffer *buffer,
1894 unsigned long ip, const char *fmt, ...)
1899 if (!(trace_flags & TRACE_ITER_PRINTK))
1903 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
1908 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1910 return trace_array_vprintk(&global_trace, ip, fmt, args);
1912 EXPORT_SYMBOL_GPL(trace_vprintk);
1914 static void trace_iterator_increment(struct trace_iterator *iter)
1916 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
1920 ring_buffer_read(buf_iter, NULL);
1923 static struct trace_entry *
1924 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1925 unsigned long *lost_events)
1927 struct ring_buffer_event *event;
1928 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
1931 event = ring_buffer_iter_peek(buf_iter, ts);
1933 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
1937 iter->ent_size = ring_buffer_event_length(event);
1938 return ring_buffer_event_data(event);
1944 static struct trace_entry *
1945 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1946 unsigned long *missing_events, u64 *ent_ts)
1948 struct ring_buffer *buffer = iter->trace_buffer->buffer;
1949 struct trace_entry *ent, *next = NULL;
1950 unsigned long lost_events = 0, next_lost = 0;
1951 int cpu_file = iter->cpu_file;
1952 u64 next_ts = 0, ts;
1958 * If we are in a per_cpu trace file, don't bother by iterating over
1959 * all cpu and peek directly.
1961 if (cpu_file > RING_BUFFER_ALL_CPUS) {
1962 if (ring_buffer_empty_cpu(buffer, cpu_file))
1964 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1966 *ent_cpu = cpu_file;
1971 for_each_tracing_cpu(cpu) {
1973 if (ring_buffer_empty_cpu(buffer, cpu))
1976 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1979 * Pick the entry with the smallest timestamp:
1981 if (ent && (!next || ts < next_ts)) {
1985 next_lost = lost_events;
1986 next_size = iter->ent_size;
1990 iter->ent_size = next_size;
1993 *ent_cpu = next_cpu;
1999 *missing_events = next_lost;
2004 /* Find the next real entry, without updating the iterator itself */
2005 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2006 int *ent_cpu, u64 *ent_ts)
2008 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
2011 /* Find the next real entry, and increment the iterator to the next entry */
2012 void *trace_find_next_entry_inc(struct trace_iterator *iter)
2014 iter->ent = __find_next_entry(iter, &iter->cpu,
2015 &iter->lost_events, &iter->ts);
2018 trace_iterator_increment(iter);
2020 return iter->ent ? iter : NULL;
2023 static void trace_consume(struct trace_iterator *iter)
2025 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
2026 &iter->lost_events);
2029 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
2031 struct trace_iterator *iter = m->private;
2035 WARN_ON_ONCE(iter->leftover);
2039 /* can't go backwards */
2044 ent = trace_find_next_entry_inc(iter);
2048 while (ent && iter->idx < i)
2049 ent = trace_find_next_entry_inc(iter);
2056 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2058 struct ring_buffer_event *event;
2059 struct ring_buffer_iter *buf_iter;
2060 unsigned long entries = 0;
2063 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2065 buf_iter = trace_buffer_iter(iter, cpu);
2069 ring_buffer_iter_reset(buf_iter);
2072 * We could have the case with the max latency tracers
2073 * that a reset never took place on a cpu. This is evident
2074 * by the timestamp being before the start of the buffer.
2076 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
2077 if (ts >= iter->trace_buffer->time_start)
2080 ring_buffer_read(buf_iter, NULL);
2083 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2087 * The current tracer is copied to avoid a global locking
2090 static void *s_start(struct seq_file *m, loff_t *pos)
2092 struct trace_iterator *iter = m->private;
2093 struct trace_array *tr = iter->tr;
2094 int cpu_file = iter->cpu_file;
2100 * copy the tracer to avoid using a global lock all around.
2101 * iter->trace is a copy of current_trace, the pointer to the
2102 * name may be used instead of a strcmp(), as iter->trace->name
2103 * will point to the same string as current_trace->name.
2105 mutex_lock(&trace_types_lock);
2106 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2107 *iter->trace = *tr->current_trace;
2108 mutex_unlock(&trace_types_lock);
2110 #ifdef CONFIG_TRACER_MAX_TRACE
2111 if (iter->snapshot && iter->trace->use_max_tr)
2112 return ERR_PTR(-EBUSY);
2115 if (!iter->snapshot)
2116 atomic_inc(&trace_record_cmdline_disabled);
2118 if (*pos != iter->pos) {
2123 if (cpu_file == RING_BUFFER_ALL_CPUS) {
2124 for_each_tracing_cpu(cpu)
2125 tracing_iter_reset(iter, cpu);
2127 tracing_iter_reset(iter, cpu_file);
2130 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2135 * If we overflowed the seq_file before, then we want
2136 * to just reuse the trace_seq buffer again.
2142 p = s_next(m, p, &l);
2146 trace_event_read_lock();
2147 trace_access_lock(cpu_file);
2151 static void s_stop(struct seq_file *m, void *p)
2153 struct trace_iterator *iter = m->private;
2155 #ifdef CONFIG_TRACER_MAX_TRACE
2156 if (iter->snapshot && iter->trace->use_max_tr)
2160 if (!iter->snapshot)
2161 atomic_dec(&trace_record_cmdline_disabled);
2163 trace_access_unlock(iter->cpu_file);
2164 trace_event_read_unlock();
2168 get_total_entries(struct trace_buffer *buf,
2169 unsigned long *total, unsigned long *entries)
2171 unsigned long count;
2177 for_each_tracing_cpu(cpu) {
2178 count = ring_buffer_entries_cpu(buf->buffer, cpu);
2180 * If this buffer has skipped entries, then we hold all
2181 * entries for the trace and we need to ignore the
2182 * ones before the time stamp.
2184 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2185 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
2186 /* total is the same as the entries */
2190 ring_buffer_overrun_cpu(buf->buffer, cpu);
2195 static void print_lat_help_header(struct seq_file *m)
2197 seq_puts(m, "# _------=> CPU# \n");
2198 seq_puts(m, "# / _-----=> irqs-off \n");
2199 seq_puts(m, "# | / _----=> need-resched \n");
2200 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2201 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2202 seq_puts(m, "# |||| / delay \n");
2203 seq_puts(m, "# cmd pid ||||| time | caller \n");
2204 seq_puts(m, "# \\ / ||||| \\ | / \n");
2207 static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
2209 unsigned long total;
2210 unsigned long entries;
2212 get_total_entries(buf, &total, &entries);
2213 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2214 entries, total, num_online_cpus());
2218 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
2220 print_event_info(buf, m);
2221 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2222 seq_puts(m, "# | | | | |\n");
2225 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
2227 print_event_info(buf, m);
2228 seq_puts(m, "# _-----=> irqs-off\n");
2229 seq_puts(m, "# / _----=> need-resched\n");
2230 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2231 seq_puts(m, "# || / _--=> preempt-depth\n");
2232 seq_puts(m, "# ||| / delay\n");
2233 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2234 seq_puts(m, "# | | | |||| | |\n");
2238 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2240 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2241 struct trace_buffer *buf = iter->trace_buffer;
2242 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2243 struct tracer *type = iter->trace;
2244 unsigned long entries;
2245 unsigned long total;
2246 const char *name = "preemption";
2250 get_total_entries(buf, &total, &entries);
2252 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2254 seq_puts(m, "# -----------------------------------"
2255 "---------------------------------\n");
2256 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2257 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2258 nsecs_to_usecs(data->saved_latency),
2262 #if defined(CONFIG_PREEMPT_NONE)
2264 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2266 #elif defined(CONFIG_PREEMPT)
2271 /* These are reserved for later use */
2274 seq_printf(m, " #P:%d)\n", num_online_cpus());
2278 seq_puts(m, "# -----------------\n");
2279 seq_printf(m, "# | task: %.16s-%d "
2280 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2281 data->comm, data->pid,
2282 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2283 data->policy, data->rt_priority);
2284 seq_puts(m, "# -----------------\n");
2286 if (data->critical_start) {
2287 seq_puts(m, "# => started at: ");
2288 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2289 trace_print_seq(m, &iter->seq);
2290 seq_puts(m, "\n# => ended at: ");
2291 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2292 trace_print_seq(m, &iter->seq);
2293 seq_puts(m, "\n#\n");
2299 static void test_cpu_buff_start(struct trace_iterator *iter)
2301 struct trace_seq *s = &iter->seq;
2303 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2306 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2309 if (cpumask_test_cpu(iter->cpu, iter->started))
2312 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2315 cpumask_set_cpu(iter->cpu, iter->started);
2317 /* Don't print started cpu buffer for the first entry of the trace */
2319 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2323 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2325 struct trace_seq *s = &iter->seq;
2326 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2327 struct trace_entry *entry;
2328 struct trace_event *event;
2332 test_cpu_buff_start(iter);
2334 event = ftrace_find_event(entry->type);
2336 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2337 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2338 if (!trace_print_lat_context(iter))
2341 if (!trace_print_context(iter))
2347 return event->funcs->trace(iter, sym_flags, event);
2349 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2352 return TRACE_TYPE_HANDLED;
2354 return TRACE_TYPE_PARTIAL_LINE;
2357 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2359 struct trace_seq *s = &iter->seq;
2360 struct trace_entry *entry;
2361 struct trace_event *event;
2365 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2366 if (!trace_seq_printf(s, "%d %d %llu ",
2367 entry->pid, iter->cpu, iter->ts))
2371 event = ftrace_find_event(entry->type);
2373 return event->funcs->raw(iter, 0, event);
2375 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2378 return TRACE_TYPE_HANDLED;
2380 return TRACE_TYPE_PARTIAL_LINE;
2383 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2385 struct trace_seq *s = &iter->seq;
2386 unsigned char newline = '\n';
2387 struct trace_entry *entry;
2388 struct trace_event *event;
2392 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2393 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2394 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2395 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2398 event = ftrace_find_event(entry->type);
2400 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2401 if (ret != TRACE_TYPE_HANDLED)
2405 SEQ_PUT_FIELD_RET(s, newline);
2407 return TRACE_TYPE_HANDLED;
2410 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2412 struct trace_seq *s = &iter->seq;
2413 struct trace_entry *entry;
2414 struct trace_event *event;
2418 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2419 SEQ_PUT_FIELD_RET(s, entry->pid);
2420 SEQ_PUT_FIELD_RET(s, iter->cpu);
2421 SEQ_PUT_FIELD_RET(s, iter->ts);
2424 event = ftrace_find_event(entry->type);
2425 return event ? event->funcs->binary(iter, 0, event) :
2429 int trace_empty(struct trace_iterator *iter)
2431 struct ring_buffer_iter *buf_iter;
2434 /* If we are looking at one CPU buffer, only check that one */
2435 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2436 cpu = iter->cpu_file;
2437 buf_iter = trace_buffer_iter(iter, cpu);
2439 if (!ring_buffer_iter_empty(buf_iter))
2442 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2448 for_each_tracing_cpu(cpu) {
2449 buf_iter = trace_buffer_iter(iter, cpu);
2451 if (!ring_buffer_iter_empty(buf_iter))
2454 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2462 /* Called with trace_event_read_lock() held. */
2463 enum print_line_t print_trace_line(struct trace_iterator *iter)
2465 enum print_line_t ret;
2467 if (iter->lost_events &&
2468 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2469 iter->cpu, iter->lost_events))
2470 return TRACE_TYPE_PARTIAL_LINE;
2472 if (iter->trace && iter->trace->print_line) {
2473 ret = iter->trace->print_line(iter);
2474 if (ret != TRACE_TYPE_UNHANDLED)
2478 if (iter->ent->type == TRACE_BPRINT &&
2479 trace_flags & TRACE_ITER_PRINTK &&
2480 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2481 return trace_print_bprintk_msg_only(iter);
2483 if (iter->ent->type == TRACE_PRINT &&
2484 trace_flags & TRACE_ITER_PRINTK &&
2485 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2486 return trace_print_printk_msg_only(iter);
2488 if (trace_flags & TRACE_ITER_BIN)
2489 return print_bin_fmt(iter);
2491 if (trace_flags & TRACE_ITER_HEX)
2492 return print_hex_fmt(iter);
2494 if (trace_flags & TRACE_ITER_RAW)
2495 return print_raw_fmt(iter);
2497 return print_trace_fmt(iter);
2500 void trace_latency_header(struct seq_file *m)
2502 struct trace_iterator *iter = m->private;
2504 /* print nothing if the buffers are empty */
2505 if (trace_empty(iter))
2508 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2509 print_trace_header(m, iter);
2511 if (!(trace_flags & TRACE_ITER_VERBOSE))
2512 print_lat_help_header(m);
2515 void trace_default_header(struct seq_file *m)
2517 struct trace_iterator *iter = m->private;
2519 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2522 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2523 /* print nothing if the buffers are empty */
2524 if (trace_empty(iter))
2526 print_trace_header(m, iter);
2527 if (!(trace_flags & TRACE_ITER_VERBOSE))
2528 print_lat_help_header(m);
2530 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2531 if (trace_flags & TRACE_ITER_IRQ_INFO)
2532 print_func_help_header_irq(iter->trace_buffer, m);
2534 print_func_help_header(iter->trace_buffer, m);
2539 static void test_ftrace_alive(struct seq_file *m)
2541 if (!ftrace_is_dead())
2543 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2544 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2547 #ifdef CONFIG_TRACER_MAX_TRACE
2548 static void show_snapshot_main_help(struct seq_file *m)
2550 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2551 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2552 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2553 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2554 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2555 seq_printf(m, "# is not a '0' or '1')\n");
2558 static void show_snapshot_percpu_help(struct seq_file *m)
2560 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2561 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2562 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2563 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2565 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2566 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2568 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2569 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2570 seq_printf(m, "# is not a '0' or '1')\n");
2573 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2575 if (iter->tr->allocated_snapshot)
2576 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2578 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2580 seq_printf(m, "# Snapshot commands:\n");
2581 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2582 show_snapshot_main_help(m);
2584 show_snapshot_percpu_help(m);
2587 /* Should never be called */
2588 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2591 static int s_show(struct seq_file *m, void *v)
2593 struct trace_iterator *iter = v;
2596 if (iter->ent == NULL) {
2598 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2600 test_ftrace_alive(m);
2602 if (iter->snapshot && trace_empty(iter))
2603 print_snapshot_help(m, iter);
2604 else if (iter->trace && iter->trace->print_header)
2605 iter->trace->print_header(m);
2607 trace_default_header(m);
2609 } else if (iter->leftover) {
2611 * If we filled the seq_file buffer earlier, we
2612 * want to just show it now.
2614 ret = trace_print_seq(m, &iter->seq);
2616 /* ret should this time be zero, but you never know */
2617 iter->leftover = ret;
2620 print_trace_line(iter);
2621 ret = trace_print_seq(m, &iter->seq);
2623 * If we overflow the seq_file buffer, then it will
2624 * ask us for this data again at start up.
2626 * ret is 0 if seq_file write succeeded.
2629 iter->leftover = ret;
2635 static const struct seq_operations tracer_seq_ops = {
2642 static struct trace_iterator *
2643 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2645 struct trace_cpu *tc = inode->i_private;
2646 struct trace_array *tr = tc->tr;
2647 struct trace_iterator *iter;
2650 if (tracing_disabled)
2651 return ERR_PTR(-ENODEV);
2653 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2655 return ERR_PTR(-ENOMEM);
2657 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2659 if (!iter->buffer_iter)
2663 * We make a copy of the current tracer to avoid concurrent
2664 * changes on it while we are reading.
2666 mutex_lock(&trace_types_lock);
2667 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2671 *iter->trace = *tr->current_trace;
2673 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2678 #ifdef CONFIG_TRACER_MAX_TRACE
2679 /* Currently only the top directory has a snapshot */
2680 if (tr->current_trace->print_max || snapshot)
2681 iter->trace_buffer = &tr->max_buffer;
2684 iter->trace_buffer = &tr->trace_buffer;
2685 iter->snapshot = snapshot;
2687 mutex_init(&iter->mutex);
2688 iter->cpu_file = tc->cpu;
2690 /* Notify the tracer early; before we stop tracing. */
2691 if (iter->trace && iter->trace->open)
2692 iter->trace->open(iter);
2694 /* Annotate start of buffers if we had overruns */
2695 if (ring_buffer_overruns(iter->trace_buffer->buffer))
2696 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2698 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2699 if (trace_clocks[trace_clock_id].in_ns)
2700 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2702 /* stop the trace while dumping if we are not opening "snapshot" */
2703 if (!iter->snapshot)
2704 tracing_stop_tr(tr);
2706 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
2707 for_each_tracing_cpu(cpu) {
2708 iter->buffer_iter[cpu] =
2709 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2711 ring_buffer_read_prepare_sync();
2712 for_each_tracing_cpu(cpu) {
2713 ring_buffer_read_start(iter->buffer_iter[cpu]);
2714 tracing_iter_reset(iter, cpu);
2717 cpu = iter->cpu_file;
2718 iter->buffer_iter[cpu] =
2719 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2720 ring_buffer_read_prepare_sync();
2721 ring_buffer_read_start(iter->buffer_iter[cpu]);
2722 tracing_iter_reset(iter, cpu);
2727 mutex_unlock(&trace_types_lock);
2732 mutex_unlock(&trace_types_lock);
2734 kfree(iter->buffer_iter);
2736 seq_release_private(inode, file);
2737 return ERR_PTR(-ENOMEM);
2740 int tracing_open_generic(struct inode *inode, struct file *filp)
2742 if (tracing_disabled)
2745 filp->private_data = inode->i_private;
2749 static int tracing_release(struct inode *inode, struct file *file)
2751 struct seq_file *m = file->private_data;
2752 struct trace_iterator *iter;
2753 struct trace_array *tr;
2756 if (!(file->f_mode & FMODE_READ))
2762 mutex_lock(&trace_types_lock);
2767 for_each_tracing_cpu(cpu) {
2768 if (iter->buffer_iter[cpu])
2769 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2772 if (iter->trace && iter->trace->close)
2773 iter->trace->close(iter);
2775 if (!iter->snapshot)
2776 /* reenable tracing if it was previously enabled */
2777 tracing_start_tr(tr);
2778 mutex_unlock(&trace_types_lock);
2780 mutex_destroy(&iter->mutex);
2781 free_cpumask_var(iter->started);
2783 kfree(iter->buffer_iter);
2784 seq_release_private(inode, file);
2788 static int tracing_open(struct inode *inode, struct file *file)
2790 struct trace_iterator *iter;
2793 /* If this file was open for write, then erase contents */
2794 if ((file->f_mode & FMODE_WRITE) &&
2795 (file->f_flags & O_TRUNC)) {
2796 struct trace_cpu *tc = inode->i_private;
2797 struct trace_array *tr = tc->tr;
2799 if (tc->cpu == RING_BUFFER_ALL_CPUS)
2800 tracing_reset_online_cpus(&tr->trace_buffer);
2802 tracing_reset(&tr->trace_buffer, tc->cpu);
2805 if (file->f_mode & FMODE_READ) {
2806 iter = __tracing_open(inode, file, false);
2808 ret = PTR_ERR(iter);
2809 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2810 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2816 t_next(struct seq_file *m, void *v, loff_t *pos)
2818 struct tracer *t = v;
2828 static void *t_start(struct seq_file *m, loff_t *pos)
2833 mutex_lock(&trace_types_lock);
2834 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2840 static void t_stop(struct seq_file *m, void *p)
2842 mutex_unlock(&trace_types_lock);
2845 static int t_show(struct seq_file *m, void *v)
2847 struct tracer *t = v;
2852 seq_printf(m, "%s", t->name);
2861 static const struct seq_operations show_traces_seq_ops = {
2868 static int show_traces_open(struct inode *inode, struct file *file)
2870 if (tracing_disabled)
2873 return seq_open(file, &show_traces_seq_ops);
2877 tracing_write_stub(struct file *filp, const char __user *ubuf,
2878 size_t count, loff_t *ppos)
2883 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2885 if (file->f_mode & FMODE_READ)
2886 return seq_lseek(file, offset, origin);
2891 static const struct file_operations tracing_fops = {
2892 .open = tracing_open,
2894 .write = tracing_write_stub,
2895 .llseek = tracing_seek,
2896 .release = tracing_release,
2899 static const struct file_operations show_traces_fops = {
2900 .open = show_traces_open,
2902 .release = seq_release,
2903 .llseek = seq_lseek,
2907 * Only trace on a CPU if the bitmask is set:
2909 static cpumask_var_t tracing_cpumask;
2912 * The tracer itself will not take this lock, but still we want
2913 * to provide a consistent cpumask to user-space:
2915 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2918 * Temporary storage for the character representation of the
2919 * CPU bitmask (and one more byte for the newline):
2921 static char mask_str[NR_CPUS + 1];
2924 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2925 size_t count, loff_t *ppos)
2929 mutex_lock(&tracing_cpumask_update_lock);
2931 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2932 if (count - len < 2) {
2936 len += sprintf(mask_str + len, "\n");
2937 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2940 mutex_unlock(&tracing_cpumask_update_lock);
2946 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2947 size_t count, loff_t *ppos)
2949 struct trace_array *tr = filp->private_data;
2950 cpumask_var_t tracing_cpumask_new;
2953 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2956 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2960 mutex_lock(&tracing_cpumask_update_lock);
2962 local_irq_disable();
2963 arch_spin_lock(&ftrace_max_lock);
2964 for_each_tracing_cpu(cpu) {
2966 * Increase/decrease the disabled counter if we are
2967 * about to flip a bit in the cpumask:
2969 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2970 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2971 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
2972 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
2974 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2975 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2976 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
2977 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
2980 arch_spin_unlock(&ftrace_max_lock);
2983 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2985 mutex_unlock(&tracing_cpumask_update_lock);
2986 free_cpumask_var(tracing_cpumask_new);
2991 free_cpumask_var(tracing_cpumask_new);
2996 static const struct file_operations tracing_cpumask_fops = {
2997 .open = tracing_open_generic,
2998 .read = tracing_cpumask_read,
2999 .write = tracing_cpumask_write,
3000 .llseek = generic_file_llseek,
3003 static int tracing_trace_options_show(struct seq_file *m, void *v)
3005 struct tracer_opt *trace_opts;
3006 struct trace_array *tr = m->private;
3010 mutex_lock(&trace_types_lock);
3011 tracer_flags = tr->current_trace->flags->val;
3012 trace_opts = tr->current_trace->flags->opts;
3014 for (i = 0; trace_options[i]; i++) {
3015 if (trace_flags & (1 << i))
3016 seq_printf(m, "%s\n", trace_options[i]);
3018 seq_printf(m, "no%s\n", trace_options[i]);
3021 for (i = 0; trace_opts[i].name; i++) {
3022 if (tracer_flags & trace_opts[i].bit)
3023 seq_printf(m, "%s\n", trace_opts[i].name);
3025 seq_printf(m, "no%s\n", trace_opts[i].name);
3027 mutex_unlock(&trace_types_lock);
3032 static int __set_tracer_option(struct tracer *trace,
3033 struct tracer_flags *tracer_flags,
3034 struct tracer_opt *opts, int neg)
3038 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
3043 tracer_flags->val &= ~opts->bit;
3045 tracer_flags->val |= opts->bit;
3049 /* Try to assign a tracer specific option */
3050 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3052 struct tracer_flags *tracer_flags = trace->flags;
3053 struct tracer_opt *opts = NULL;
3056 for (i = 0; tracer_flags->opts[i].name; i++) {
3057 opts = &tracer_flags->opts[i];
3059 if (strcmp(cmp, opts->name) == 0)
3060 return __set_tracer_option(trace, trace->flags,
3067 /* Some tracers require overwrite to stay enabled */
3068 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3070 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3076 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
3078 /* do nothing if flag is already set */
3079 if (!!(trace_flags & mask) == !!enabled)
3082 /* Give the tracer a chance to approve the change */
3083 if (tr->current_trace->flag_changed)
3084 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
3088 trace_flags |= mask;
3090 trace_flags &= ~mask;
3092 if (mask == TRACE_ITER_RECORD_CMD)
3093 trace_event_enable_cmd_record(enabled);
3095 if (mask == TRACE_ITER_OVERWRITE) {
3096 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
3097 #ifdef CONFIG_TRACER_MAX_TRACE
3098 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
3102 if (mask == TRACE_ITER_PRINTK)
3103 trace_printk_start_stop_comm(enabled);
3108 static int trace_set_options(struct trace_array *tr, char *option)
3115 cmp = strstrip(option);
3117 if (strncmp(cmp, "no", 2) == 0) {
3122 mutex_lock(&trace_types_lock);
3124 for (i = 0; trace_options[i]; i++) {
3125 if (strcmp(cmp, trace_options[i]) == 0) {
3126 ret = set_tracer_flag(tr, 1 << i, !neg);
3131 /* If no option could be set, test the specific tracer options */
3132 if (!trace_options[i])
3133 ret = set_tracer_option(tr->current_trace, cmp, neg);
3135 mutex_unlock(&trace_types_lock);
3141 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3142 size_t cnt, loff_t *ppos)
3144 struct seq_file *m = filp->private_data;
3145 struct trace_array *tr = m->private;
3149 if (cnt >= sizeof(buf))
3152 if (copy_from_user(&buf, ubuf, cnt))
3157 ret = trace_set_options(tr, buf);
3166 static int tracing_trace_options_open(struct inode *inode, struct file *file)
3168 if (tracing_disabled)
3171 return single_open(file, tracing_trace_options_show, inode->i_private);
3174 static const struct file_operations tracing_iter_fops = {
3175 .open = tracing_trace_options_open,
3177 .llseek = seq_lseek,
3178 .release = single_release,
3179 .write = tracing_trace_options_write,
3182 static const char readme_msg[] =
3183 "tracing mini-HOWTO:\n\n"
3184 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
3185 "# cat /sys/kernel/debug/tracing/available_tracers\n"
3186 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
3187 "# cat /sys/kernel/debug/tracing/current_tracer\n"
3189 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
3190 "# cat /sys/kernel/debug/tracing/current_tracer\n"
3192 "# cat /sys/kernel/debug/tracing/trace_options\n"
3193 "noprint-parent nosym-offset nosym-addr noverbose\n"
3194 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
3195 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
3196 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
3197 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
3201 tracing_readme_read(struct file *filp, char __user *ubuf,
3202 size_t cnt, loff_t *ppos)
3204 return simple_read_from_buffer(ubuf, cnt, ppos,
3205 readme_msg, strlen(readme_msg));
3208 static const struct file_operations tracing_readme_fops = {
3209 .open = tracing_open_generic,
3210 .read = tracing_readme_read,
3211 .llseek = generic_file_llseek,
3215 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3216 size_t cnt, loff_t *ppos)
3225 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3229 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3237 for (i = 0; i < SAVED_CMDLINES; i++) {
3240 pid = map_cmdline_to_pid[i];
3241 if (pid == -1 || pid == NO_CMDLINE_MAP)
3244 trace_find_cmdline(pid, buf_comm);
3245 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3250 len = simple_read_from_buffer(ubuf, cnt, ppos,
3259 static const struct file_operations tracing_saved_cmdlines_fops = {
3260 .open = tracing_open_generic,
3261 .read = tracing_saved_cmdlines_read,
3262 .llseek = generic_file_llseek,
3266 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3267 size_t cnt, loff_t *ppos)
3269 struct trace_array *tr = filp->private_data;
3270 char buf[MAX_TRACER_SIZE+2];
3273 mutex_lock(&trace_types_lock);
3274 r = sprintf(buf, "%s\n", tr->current_trace->name);
3275 mutex_unlock(&trace_types_lock);
3277 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3280 int tracer_init(struct tracer *t, struct trace_array *tr)
3282 tracing_reset_online_cpus(&tr->trace_buffer);
3286 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
3290 for_each_tracing_cpu(cpu)
3291 per_cpu_ptr(buf->data, cpu)->entries = val;
3294 #ifdef CONFIG_TRACER_MAX_TRACE
3295 /* resize @tr's buffer to the size of @size_tr's entries */
3296 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3297 struct trace_buffer *size_buf, int cpu_id)
3301 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3302 for_each_tracing_cpu(cpu) {
3303 ret = ring_buffer_resize(trace_buf->buffer,
3304 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
3307 per_cpu_ptr(trace_buf->data, cpu)->entries =
3308 per_cpu_ptr(size_buf->data, cpu)->entries;
3311 ret = ring_buffer_resize(trace_buf->buffer,
3312 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
3314 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3315 per_cpu_ptr(size_buf->data, cpu_id)->entries;
3320 #endif /* CONFIG_TRACER_MAX_TRACE */
3322 static int __tracing_resize_ring_buffer(struct trace_array *tr,
3323 unsigned long size, int cpu)
3328 * If kernel or user changes the size of the ring buffer
3329 * we use the size that was given, and we can forget about
3330 * expanding it later.
3332 ring_buffer_expanded = true;
3334 /* May be called before buffers are initialized */
3335 if (!tr->trace_buffer.buffer)
3338 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
3342 #ifdef CONFIG_TRACER_MAX_TRACE
3343 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3344 !tr->current_trace->use_max_tr)
3347 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
3349 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3350 &tr->trace_buffer, cpu);
3353 * AARGH! We are left with different
3354 * size max buffer!!!!
3355 * The max buffer is our "snapshot" buffer.
3356 * When a tracer needs a snapshot (one of the
3357 * latency tracers), it swaps the max buffer
3358 * with the saved snap shot. We succeeded to
3359 * update the size of the main buffer, but failed to
3360 * update the size of the max buffer. But when we tried
3361 * to reset the main buffer to the original size, we
3362 * failed there too. This is very unlikely to
3363 * happen, but if it does, warn and kill all
3367 tracing_disabled = 1;
3372 if (cpu == RING_BUFFER_ALL_CPUS)
3373 set_buffer_entries(&tr->max_buffer, size);
3375 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
3378 #endif /* CONFIG_TRACER_MAX_TRACE */
3380 if (cpu == RING_BUFFER_ALL_CPUS)
3381 set_buffer_entries(&tr->trace_buffer, size);
3383 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
3388 static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3389 unsigned long size, int cpu_id)
3393 mutex_lock(&trace_types_lock);
3395 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3396 /* make sure, this cpu is enabled in the mask */
3397 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3403 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
3408 mutex_unlock(&trace_types_lock);
3415 * tracing_update_buffers - used by tracing facility to expand ring buffers
3417 * To save on memory when the tracing is never used on a system with it
3418 * configured in. The ring buffers are set to a minimum size. But once
3419 * a user starts to use the tracing facility, then they need to grow
3420 * to their default size.
3422 * This function is to be called when a tracer is about to be used.
3424 int tracing_update_buffers(void)
3428 mutex_lock(&trace_types_lock);
3429 if (!ring_buffer_expanded)
3430 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
3431 RING_BUFFER_ALL_CPUS);
3432 mutex_unlock(&trace_types_lock);
3437 struct trace_option_dentry;
3439 static struct trace_option_dentry *
3440 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
3443 destroy_trace_option_files(struct trace_option_dentry *topts);
3445 static int tracing_set_tracer(const char *buf)
3447 static struct trace_option_dentry *topts;
3448 struct trace_array *tr = &global_trace;
3450 #ifdef CONFIG_TRACER_MAX_TRACE
3455 mutex_lock(&trace_types_lock);
3457 if (!ring_buffer_expanded) {
3458 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
3459 RING_BUFFER_ALL_CPUS);
3465 for (t = trace_types; t; t = t->next) {
3466 if (strcmp(t->name, buf) == 0)
3473 if (t == tr->current_trace)
3476 trace_branch_disable();
3478 tr->current_trace->enabled = false;
3480 if (tr->current_trace->reset)
3481 tr->current_trace->reset(tr);
3483 /* Current trace needs to be nop_trace before synchronize_sched */
3484 tr->current_trace = &nop_trace;
3486 #ifdef CONFIG_TRACER_MAX_TRACE
3487 had_max_tr = tr->allocated_snapshot;
3489 if (had_max_tr && !t->use_max_tr) {
3491 * We need to make sure that the update_max_tr sees that
3492 * current_trace changed to nop_trace to keep it from
3493 * swapping the buffers after we resize it.
3494 * The update_max_tr is called from interrupts disabled
3495 * so a synchronized_sched() is sufficient.
3497 synchronize_sched();
3499 * We don't free the ring buffer. instead, resize it because
3500 * The max_tr ring buffer has some state (e.g. ring->clock) and
3501 * we want preserve it.
3503 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
3504 set_buffer_entries(&tr->max_buffer, 1);
3505 tracing_reset_online_cpus(&tr->max_buffer);
3506 tr->allocated_snapshot = false;
3509 destroy_trace_option_files(topts);
3511 topts = create_trace_option_files(tr, t);
3513 #ifdef CONFIG_TRACER_MAX_TRACE
3514 if (t->use_max_tr && !had_max_tr) {
3515 /* we need to make per cpu buffer sizes equivalent */
3516 ret = resize_buffer_duplicate_size(&tr->max_buffer, &tr->trace_buffer,
3517 RING_BUFFER_ALL_CPUS);
3520 tr->allocated_snapshot = true;
3525 ret = tracer_init(t, tr);
3530 tr->current_trace = t;
3531 tr->current_trace->enabled = true;
3532 trace_branch_enable(tr);
3534 mutex_unlock(&trace_types_lock);
3540 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3541 size_t cnt, loff_t *ppos)
3543 char buf[MAX_TRACER_SIZE+1];
3550 if (cnt > MAX_TRACER_SIZE)
3551 cnt = MAX_TRACER_SIZE;
3553 if (copy_from_user(&buf, ubuf, cnt))
3558 /* strip ending whitespace. */
3559 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3562 err = tracing_set_tracer(buf);
3572 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3573 size_t cnt, loff_t *ppos)
3575 unsigned long *ptr = filp->private_data;
3579 r = snprintf(buf, sizeof(buf), "%ld\n",
3580 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3581 if (r > sizeof(buf))
3583 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3587 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3588 size_t cnt, loff_t *ppos)
3590 unsigned long *ptr = filp->private_data;
3594 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3603 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3605 struct trace_cpu *tc = inode->i_private;
3606 struct trace_array *tr = tc->tr;
3607 struct trace_iterator *iter;
3610 if (tracing_disabled)
3613 mutex_lock(&trace_types_lock);
3615 /* create a buffer to store the information to pass to userspace */
3616 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3623 * We make a copy of the current tracer to avoid concurrent
3624 * changes on it while we are reading.
3626 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3631 *iter->trace = *tr->current_trace;
3633 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3638 /* trace pipe does not show start of buffer */
3639 cpumask_setall(iter->started);
3641 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3642 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3644 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3645 if (trace_clocks[trace_clock_id].in_ns)
3646 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3648 iter->cpu_file = tc->cpu;
3650 iter->trace_buffer = &tc->tr->trace_buffer;
3651 mutex_init(&iter->mutex);
3652 filp->private_data = iter;
3654 if (iter->trace->pipe_open)
3655 iter->trace->pipe_open(iter);
3657 nonseekable_open(inode, filp);
3659 mutex_unlock(&trace_types_lock);
3665 mutex_unlock(&trace_types_lock);
3669 static int tracing_release_pipe(struct inode *inode, struct file *file)
3671 struct trace_iterator *iter = file->private_data;
3673 mutex_lock(&trace_types_lock);
3675 if (iter->trace->pipe_close)
3676 iter->trace->pipe_close(iter);
3678 mutex_unlock(&trace_types_lock);
3680 free_cpumask_var(iter->started);
3681 mutex_destroy(&iter->mutex);
3689 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
3691 /* Iterators are static, they should be filled or empty */
3692 if (trace_buffer_iter(iter, iter->cpu_file))
3693 return POLLIN | POLLRDNORM;
3695 if (trace_flags & TRACE_ITER_BLOCK)
3697 * Always select as readable when in blocking mode
3699 return POLLIN | POLLRDNORM;
3701 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
3706 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3708 struct trace_iterator *iter = filp->private_data;
3710 return trace_poll(iter, filp, poll_table);
3714 * This is a make-shift waitqueue.
3715 * A tracer might use this callback on some rare cases:
3717 * 1) the current tracer might hold the runqueue lock when it wakes up
3718 * a reader, hence a deadlock (sched, function, and function graph tracers)
3719 * 2) the function tracers, trace all functions, we don't want
3720 * the overhead of calling wake_up and friends
3721 * (and tracing them too)
3723 * Anyway, this is really very primitive wakeup.
3725 void poll_wait_pipe(struct trace_iterator *iter)
3727 set_current_state(TASK_INTERRUPTIBLE);
3728 /* sleep for 100 msecs, and try again. */
3729 schedule_timeout(HZ / 10);
3732 /* Must be called with trace_types_lock mutex held. */
3733 static int tracing_wait_pipe(struct file *filp)
3735 struct trace_iterator *iter = filp->private_data;
3737 while (trace_empty(iter)) {
3739 if ((filp->f_flags & O_NONBLOCK)) {
3743 mutex_unlock(&iter->mutex);
3745 iter->trace->wait_pipe(iter);
3747 mutex_lock(&iter->mutex);
3749 if (signal_pending(current))
3753 * We block until we read something and tracing is disabled.
3754 * We still block if tracing is disabled, but we have never
3755 * read anything. This allows a user to cat this file, and
3756 * then enable tracing. But after we have read something,
3757 * we give an EOF when tracing is again disabled.
3759 * iter->pos will be 0 if we haven't read anything.
3761 if (!tracing_is_enabled() && iter->pos)
3772 tracing_read_pipe(struct file *filp, char __user *ubuf,
3773 size_t cnt, loff_t *ppos)
3775 struct trace_iterator *iter = filp->private_data;
3776 struct trace_array *tr = iter->tr;
3779 /* return any leftover data */
3780 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3784 trace_seq_init(&iter->seq);
3786 /* copy the tracer to avoid using a global lock all around */
3787 mutex_lock(&trace_types_lock);
3788 if (unlikely(iter->trace->name != tr->current_trace->name))
3789 *iter->trace = *tr->current_trace;
3790 mutex_unlock(&trace_types_lock);
3793 * Avoid more than one consumer on a single file descriptor
3794 * This is just a matter of traces coherency, the ring buffer itself
3797 mutex_lock(&iter->mutex);
3798 if (iter->trace->read) {
3799 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3805 sret = tracing_wait_pipe(filp);
3809 /* stop when tracing is finished */
3810 if (trace_empty(iter)) {
3815 if (cnt >= PAGE_SIZE)
3816 cnt = PAGE_SIZE - 1;
3818 /* reset all but tr, trace, and overruns */
3819 memset(&iter->seq, 0,
3820 sizeof(struct trace_iterator) -
3821 offsetof(struct trace_iterator, seq));
3824 trace_event_read_lock();
3825 trace_access_lock(iter->cpu_file);
3826 while (trace_find_next_entry_inc(iter) != NULL) {
3827 enum print_line_t ret;
3828 int len = iter->seq.len;
3830 ret = print_trace_line(iter);
3831 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3832 /* don't print partial lines */
3833 iter->seq.len = len;
3836 if (ret != TRACE_TYPE_NO_CONSUME)
3837 trace_consume(iter);
3839 if (iter->seq.len >= cnt)
3843 * Setting the full flag means we reached the trace_seq buffer
3844 * size and we should leave by partial output condition above.
3845 * One of the trace_seq_* functions is not used properly.
3847 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3850 trace_access_unlock(iter->cpu_file);
3851 trace_event_read_unlock();
3853 /* Now copy what we have to the user */
3854 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3855 if (iter->seq.readpos >= iter->seq.len)
3856 trace_seq_init(&iter->seq);
3859 * If there was nothing to send to user, in spite of consuming trace
3860 * entries, go back to wait for more entries.
3866 mutex_unlock(&iter->mutex);
3871 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3872 struct pipe_buffer *buf)
3874 __free_page(buf->page);
3877 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3880 __free_page(spd->pages[idx]);
3883 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3885 .map = generic_pipe_buf_map,
3886 .unmap = generic_pipe_buf_unmap,
3887 .confirm = generic_pipe_buf_confirm,
3888 .release = tracing_pipe_buf_release,
3889 .steal = generic_pipe_buf_steal,
3890 .get = generic_pipe_buf_get,
3894 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3899 /* Seq buffer is page-sized, exactly what we need. */
3901 count = iter->seq.len;
3902 ret = print_trace_line(iter);
3903 count = iter->seq.len - count;
3906 iter->seq.len -= count;
3909 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3910 iter->seq.len -= count;
3914 if (ret != TRACE_TYPE_NO_CONSUME)
3915 trace_consume(iter);
3917 if (!trace_find_next_entry_inc(iter)) {
3927 static ssize_t tracing_splice_read_pipe(struct file *filp,
3929 struct pipe_inode_info *pipe,
3933 struct page *pages_def[PIPE_DEF_BUFFERS];
3934 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3935 struct trace_iterator *iter = filp->private_data;
3936 struct splice_pipe_desc spd = {
3938 .partial = partial_def,
3939 .nr_pages = 0, /* This gets updated below. */
3940 .nr_pages_max = PIPE_DEF_BUFFERS,
3942 .ops = &tracing_pipe_buf_ops,
3943 .spd_release = tracing_spd_release_pipe,
3945 struct trace_array *tr = iter->tr;
3950 if (splice_grow_spd(pipe, &spd))
3953 /* copy the tracer to avoid using a global lock all around */
3954 mutex_lock(&trace_types_lock);
3955 if (unlikely(iter->trace->name != tr->current_trace->name))
3956 *iter->trace = *tr->current_trace;
3957 mutex_unlock(&trace_types_lock);
3959 mutex_lock(&iter->mutex);
3961 if (iter->trace->splice_read) {
3962 ret = iter->trace->splice_read(iter, filp,
3963 ppos, pipe, len, flags);
3968 ret = tracing_wait_pipe(filp);
3972 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3977 trace_event_read_lock();
3978 trace_access_lock(iter->cpu_file);
3980 /* Fill as many pages as possible. */
3981 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3982 spd.pages[i] = alloc_page(GFP_KERNEL);
3986 rem = tracing_fill_pipe_page(rem, iter);
3988 /* Copy the data into the page, so we can start over. */
3989 ret = trace_seq_to_buffer(&iter->seq,
3990 page_address(spd.pages[i]),
3993 __free_page(spd.pages[i]);
3996 spd.partial[i].offset = 0;
3997 spd.partial[i].len = iter->seq.len;
3999 trace_seq_init(&iter->seq);
4002 trace_access_unlock(iter->cpu_file);
4003 trace_event_read_unlock();
4004 mutex_unlock(&iter->mutex);
4008 ret = splice_to_pipe(pipe, &spd);
4010 splice_shrink_spd(&spd);
4014 mutex_unlock(&iter->mutex);
4019 tracing_entries_read(struct file *filp, char __user *ubuf,
4020 size_t cnt, loff_t *ppos)
4022 struct trace_cpu *tc = filp->private_data;
4023 struct trace_array *tr = tc->tr;
4028 mutex_lock(&trace_types_lock);
4030 if (tc->cpu == RING_BUFFER_ALL_CPUS) {
4031 int cpu, buf_size_same;
4036 /* check if all cpu sizes are same */
4037 for_each_tracing_cpu(cpu) {
4038 /* fill in the size from first enabled cpu */
4040 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4041 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
4047 if (buf_size_same) {
4048 if (!ring_buffer_expanded)
4049 r = sprintf(buf, "%lu (expanded: %lu)\n",
4051 trace_buf_size >> 10);
4053 r = sprintf(buf, "%lu\n", size >> 10);
4055 r = sprintf(buf, "X\n");
4057 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, tc->cpu)->entries >> 10);
4059 mutex_unlock(&trace_types_lock);
4061 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4066 tracing_entries_write(struct file *filp, const char __user *ubuf,
4067 size_t cnt, loff_t *ppos)
4069 struct trace_cpu *tc = filp->private_data;
4073 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4077 /* must have at least 1 entry */
4081 /* value is in KB */
4084 ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
4094 tracing_total_entries_read(struct file *filp, char __user *ubuf,
4095 size_t cnt, loff_t *ppos)
4097 struct trace_array *tr = filp->private_data;
4100 unsigned long size = 0, expanded_size = 0;
4102 mutex_lock(&trace_types_lock);
4103 for_each_tracing_cpu(cpu) {
4104 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
4105 if (!ring_buffer_expanded)
4106 expanded_size += trace_buf_size >> 10;
4108 if (ring_buffer_expanded)
4109 r = sprintf(buf, "%lu\n", size);
4111 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4112 mutex_unlock(&trace_types_lock);
4114 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4118 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4119 size_t cnt, loff_t *ppos)
4122 * There is no need to read what the user has written, this function
4123 * is just to make sure that there is no error when "echo" is used
4132 tracing_free_buffer_release(struct inode *inode, struct file *filp)
4134 struct trace_array *tr = inode->i_private;
4136 /* disable tracing ? */
4137 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4139 /* resize the ring buffer to 0 */
4140 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4146 tracing_mark_write(struct file *filp, const char __user *ubuf,
4147 size_t cnt, loff_t *fpos)
4149 unsigned long addr = (unsigned long)ubuf;
4150 struct ring_buffer_event *event;
4151 struct ring_buffer *buffer;
4152 struct print_entry *entry;
4153 unsigned long irq_flags;
4154 struct page *pages[2];
4164 if (tracing_disabled)
4167 if (!(trace_flags & TRACE_ITER_MARKERS))
4170 if (cnt > TRACE_BUF_SIZE)
4171 cnt = TRACE_BUF_SIZE;
4174 * Userspace is injecting traces into the kernel trace buffer.
4175 * We want to be as non intrusive as possible.
4176 * To do so, we do not want to allocate any special buffers
4177 * or take any locks, but instead write the userspace data
4178 * straight into the ring buffer.
4180 * First we need to pin the userspace buffer into memory,
4181 * which, most likely it is, because it just referenced it.
4182 * But there's no guarantee that it is. By using get_user_pages_fast()
4183 * and kmap_atomic/kunmap_atomic() we can get access to the
4184 * pages directly. We then write the data directly into the
4187 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4189 /* check if we cross pages */
4190 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4193 offset = addr & (PAGE_SIZE - 1);
4196 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4197 if (ret < nr_pages) {
4199 put_page(pages[ret]);
4204 for (i = 0; i < nr_pages; i++)
4205 map_page[i] = kmap_atomic(pages[i]);
4207 local_save_flags(irq_flags);
4208 size = sizeof(*entry) + cnt + 2; /* possible \n added */
4209 buffer = global_trace.trace_buffer.buffer;
4210 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4211 irq_flags, preempt_count());
4213 /* Ring buffer disabled, return as if not open for write */
4218 entry = ring_buffer_event_data(event);
4219 entry->ip = _THIS_IP_;
4221 if (nr_pages == 2) {
4222 len = PAGE_SIZE - offset;
4223 memcpy(&entry->buf, map_page[0] + offset, len);
4224 memcpy(&entry->buf[len], map_page[1], cnt - len);
4226 memcpy(&entry->buf, map_page[0] + offset, cnt);
4228 if (entry->buf[cnt - 1] != '\n') {
4229 entry->buf[cnt] = '\n';
4230 entry->buf[cnt + 1] = '\0';
4232 entry->buf[cnt] = '\0';
4234 __buffer_unlock_commit(buffer, event);
4241 for (i = 0; i < nr_pages; i++){
4242 kunmap_atomic(map_page[i]);
4249 static int tracing_clock_show(struct seq_file *m, void *v)
4251 struct trace_array *tr = m->private;
4254 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4256 "%s%s%s%s", i ? " " : "",
4257 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4258 i == tr->clock_id ? "]" : "");
4264 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4265 size_t cnt, loff_t *fpos)
4267 struct seq_file *m = filp->private_data;
4268 struct trace_array *tr = m->private;
4270 const char *clockstr;
4273 if (cnt >= sizeof(buf))
4276 if (copy_from_user(&buf, ubuf, cnt))
4281 clockstr = strstrip(buf);
4283 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4284 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4287 if (i == ARRAY_SIZE(trace_clocks))
4290 mutex_lock(&trace_types_lock);
4294 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
4297 * New clock may not be consistent with the previous clock.
4298 * Reset the buffer so that it doesn't have incomparable timestamps.
4300 tracing_reset_online_cpus(&global_trace.trace_buffer);
4302 #ifdef CONFIG_TRACER_MAX_TRACE
4303 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4304 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4305 tracing_reset_online_cpus(&global_trace.max_buffer);
4308 mutex_unlock(&trace_types_lock);
4315 static int tracing_clock_open(struct inode *inode, struct file *file)
4317 if (tracing_disabled)
4320 return single_open(file, tracing_clock_show, inode->i_private);
4323 struct ftrace_buffer_info {
4324 struct trace_iterator iter;
4329 #ifdef CONFIG_TRACER_SNAPSHOT
4330 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4332 struct trace_cpu *tc = inode->i_private;
4333 struct trace_iterator *iter;
4337 if (file->f_mode & FMODE_READ) {
4338 iter = __tracing_open(inode, file, true);
4340 ret = PTR_ERR(iter);
4342 /* Writes still need the seq_file to hold the private data */
4343 m = kzalloc(sizeof(*m), GFP_KERNEL);
4346 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4352 iter->trace_buffer = &tc->tr->max_buffer;
4353 iter->cpu_file = tc->cpu;
4355 file->private_data = m;
4362 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4365 struct seq_file *m = filp->private_data;
4366 struct trace_iterator *iter = m->private;
4367 struct trace_array *tr = iter->tr;
4371 ret = tracing_update_buffers();
4375 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4379 mutex_lock(&trace_types_lock);
4381 if (tr->current_trace->use_max_tr) {
4388 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4392 if (tr->allocated_snapshot) {
4393 /* free spare buffer */
4394 ring_buffer_resize(tr->max_buffer.buffer, 1,
4395 RING_BUFFER_ALL_CPUS);
4396 set_buffer_entries(&tr->max_buffer, 1);
4397 tracing_reset_online_cpus(&tr->max_buffer);
4398 tr->allocated_snapshot = false;
4402 /* Only allow per-cpu swap if the ring buffer supports it */
4403 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4404 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4409 if (!tr->allocated_snapshot) {
4410 /* allocate spare buffer */
4411 ret = resize_buffer_duplicate_size(&tr->max_buffer,
4412 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
4415 tr->allocated_snapshot = true;
4417 local_irq_disable();
4418 /* Now, we're going to swap */
4419 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4420 update_max_tr(tr, current, smp_processor_id());
4422 update_max_tr_single(tr, current, iter->cpu_file);
4426 if (tr->allocated_snapshot) {
4427 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4428 tracing_reset_online_cpus(&tr->max_buffer);
4430 tracing_reset(&tr->max_buffer, iter->cpu_file);
4440 mutex_unlock(&trace_types_lock);
4444 static int tracing_snapshot_release(struct inode *inode, struct file *file)
4446 struct seq_file *m = file->private_data;
4448 if (file->f_mode & FMODE_READ)
4449 return tracing_release(inode, file);
4451 /* If write only, the seq_file is just a stub */
4459 static int tracing_buffers_open(struct inode *inode, struct file *filp);
4460 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
4461 size_t count, loff_t *ppos);
4462 static int tracing_buffers_release(struct inode *inode, struct file *file);
4463 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4464 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
4466 static int snapshot_raw_open(struct inode *inode, struct file *filp)
4468 struct ftrace_buffer_info *info;
4471 ret = tracing_buffers_open(inode, filp);
4475 info = filp->private_data;
4477 if (info->iter.trace->use_max_tr) {
4478 tracing_buffers_release(inode, filp);
4482 info->iter.snapshot = true;
4483 info->iter.trace_buffer = &info->iter.tr->max_buffer;
4488 #endif /* CONFIG_TRACER_SNAPSHOT */
4491 static const struct file_operations tracing_max_lat_fops = {
4492 .open = tracing_open_generic,
4493 .read = tracing_max_lat_read,
4494 .write = tracing_max_lat_write,
4495 .llseek = generic_file_llseek,
4498 static const struct file_operations set_tracer_fops = {
4499 .open = tracing_open_generic,
4500 .read = tracing_set_trace_read,
4501 .write = tracing_set_trace_write,
4502 .llseek = generic_file_llseek,
4505 static const struct file_operations tracing_pipe_fops = {
4506 .open = tracing_open_pipe,
4507 .poll = tracing_poll_pipe,
4508 .read = tracing_read_pipe,
4509 .splice_read = tracing_splice_read_pipe,
4510 .release = tracing_release_pipe,
4511 .llseek = no_llseek,
4514 static const struct file_operations tracing_entries_fops = {
4515 .open = tracing_open_generic,
4516 .read = tracing_entries_read,
4517 .write = tracing_entries_write,
4518 .llseek = generic_file_llseek,
4521 static const struct file_operations tracing_total_entries_fops = {
4522 .open = tracing_open_generic,
4523 .read = tracing_total_entries_read,
4524 .llseek = generic_file_llseek,
4527 static const struct file_operations tracing_free_buffer_fops = {
4528 .write = tracing_free_buffer_write,
4529 .release = tracing_free_buffer_release,
4532 static const struct file_operations tracing_mark_fops = {
4533 .open = tracing_open_generic,
4534 .write = tracing_mark_write,
4535 .llseek = generic_file_llseek,
4538 static const struct file_operations trace_clock_fops = {
4539 .open = tracing_clock_open,
4541 .llseek = seq_lseek,
4542 .release = single_release,
4543 .write = tracing_clock_write,
4546 #ifdef CONFIG_TRACER_SNAPSHOT
4547 static const struct file_operations snapshot_fops = {
4548 .open = tracing_snapshot_open,
4550 .write = tracing_snapshot_write,
4551 .llseek = tracing_seek,
4552 .release = tracing_snapshot_release,
4555 static const struct file_operations snapshot_raw_fops = {
4556 .open = snapshot_raw_open,
4557 .read = tracing_buffers_read,
4558 .release = tracing_buffers_release,
4559 .splice_read = tracing_buffers_splice_read,
4560 .llseek = no_llseek,
4563 #endif /* CONFIG_TRACER_SNAPSHOT */
4565 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4567 struct trace_cpu *tc = inode->i_private;
4568 struct trace_array *tr = tc->tr;
4569 struct ftrace_buffer_info *info;
4571 if (tracing_disabled)
4574 info = kzalloc(sizeof(*info), GFP_KERNEL);
4578 mutex_lock(&trace_types_lock);
4583 info->iter.cpu_file = tc->cpu;
4584 info->iter.trace = tr->current_trace;
4585 info->iter.trace_buffer = &tr->trace_buffer;
4587 /* Force reading ring buffer for first read */
4588 info->read = (unsigned int)-1;
4590 filp->private_data = info;
4592 mutex_unlock(&trace_types_lock);
4594 return nonseekable_open(inode, filp);
4598 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
4600 struct ftrace_buffer_info *info = filp->private_data;
4601 struct trace_iterator *iter = &info->iter;
4603 return trace_poll(iter, filp, poll_table);
4607 tracing_buffers_read(struct file *filp, char __user *ubuf,
4608 size_t count, loff_t *ppos)
4610 struct ftrace_buffer_info *info = filp->private_data;
4611 struct trace_iterator *iter = &info->iter;
4618 mutex_lock(&trace_types_lock);
4620 #ifdef CONFIG_TRACER_MAX_TRACE
4621 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4628 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
4634 /* Do we have previous read data to read? */
4635 if (info->read < PAGE_SIZE)
4639 trace_access_lock(iter->cpu_file);
4640 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
4644 trace_access_unlock(iter->cpu_file);
4647 if (trace_empty(iter)) {
4648 if ((filp->f_flags & O_NONBLOCK)) {
4652 mutex_unlock(&trace_types_lock);
4653 iter->trace->wait_pipe(iter);
4654 mutex_lock(&trace_types_lock);
4655 if (signal_pending(current)) {
4667 size = PAGE_SIZE - info->read;
4671 ret = copy_to_user(ubuf, info->spare + info->read, size);
4682 mutex_unlock(&trace_types_lock);
4687 static int tracing_buffers_release(struct inode *inode, struct file *file)
4689 struct ftrace_buffer_info *info = file->private_data;
4690 struct trace_iterator *iter = &info->iter;
4692 mutex_lock(&trace_types_lock);
4694 WARN_ON(!iter->tr->ref);
4698 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
4701 mutex_unlock(&trace_types_lock);
4707 struct ring_buffer *buffer;
4712 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4713 struct pipe_buffer *buf)
4715 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4720 ring_buffer_free_read_page(ref->buffer, ref->page);
4725 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4726 struct pipe_buffer *buf)
4728 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4733 /* Pipe buffer operations for a buffer. */
4734 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4736 .map = generic_pipe_buf_map,
4737 .unmap = generic_pipe_buf_unmap,
4738 .confirm = generic_pipe_buf_confirm,
4739 .release = buffer_pipe_buf_release,
4740 .steal = generic_pipe_buf_steal,
4741 .get = buffer_pipe_buf_get,
4745 * Callback from splice_to_pipe(), if we need to release some pages
4746 * at the end of the spd in case we error'ed out in filling the pipe.
4748 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4750 struct buffer_ref *ref =
4751 (struct buffer_ref *)spd->partial[i].private;
4756 ring_buffer_free_read_page(ref->buffer, ref->page);
4758 spd->partial[i].private = 0;
4762 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4763 struct pipe_inode_info *pipe, size_t len,
4766 struct ftrace_buffer_info *info = file->private_data;
4767 struct trace_iterator *iter = &info->iter;
4768 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4769 struct page *pages_def[PIPE_DEF_BUFFERS];
4770 struct splice_pipe_desc spd = {
4772 .partial = partial_def,
4773 .nr_pages_max = PIPE_DEF_BUFFERS,
4775 .ops = &buffer_pipe_buf_ops,
4776 .spd_release = buffer_spd_release,
4778 struct buffer_ref *ref;
4779 int entries, size, i;
4782 mutex_lock(&trace_types_lock);
4784 #ifdef CONFIG_TRACER_MAX_TRACE
4785 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4791 if (splice_grow_spd(pipe, &spd)) {
4796 if (*ppos & (PAGE_SIZE - 1)) {
4801 if (len & (PAGE_SIZE - 1)) {
4802 if (len < PAGE_SIZE) {
4810 trace_access_lock(iter->cpu_file);
4811 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
4813 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4817 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4822 ref->buffer = iter->trace_buffer->buffer;
4823 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
4829 r = ring_buffer_read_page(ref->buffer, &ref->page,
4830 len, iter->cpu_file, 1);
4832 ring_buffer_free_read_page(ref->buffer, ref->page);
4838 * zero out any left over data, this is going to
4841 size = ring_buffer_page_len(ref->page);
4842 if (size < PAGE_SIZE)
4843 memset(ref->page + size, 0, PAGE_SIZE - size);
4845 page = virt_to_page(ref->page);
4847 spd.pages[i] = page;
4848 spd.partial[i].len = PAGE_SIZE;
4849 spd.partial[i].offset = 0;
4850 spd.partial[i].private = (unsigned long)ref;
4854 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
4857 trace_access_unlock(iter->cpu_file);
4860 /* did we read anything? */
4861 if (!spd.nr_pages) {
4862 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
4866 mutex_unlock(&trace_types_lock);
4867 iter->trace->wait_pipe(iter);
4868 mutex_lock(&trace_types_lock);
4869 if (signal_pending(current)) {
4876 ret = splice_to_pipe(pipe, &spd);
4877 splice_shrink_spd(&spd);
4879 mutex_unlock(&trace_types_lock);
4884 static const struct file_operations tracing_buffers_fops = {
4885 .open = tracing_buffers_open,
4886 .read = tracing_buffers_read,
4887 .poll = tracing_buffers_poll,
4888 .release = tracing_buffers_release,
4889 .splice_read = tracing_buffers_splice_read,
4890 .llseek = no_llseek,
4894 tracing_stats_read(struct file *filp, char __user *ubuf,
4895 size_t count, loff_t *ppos)
4897 struct trace_cpu *tc = filp->private_data;
4898 struct trace_array *tr = tc->tr;
4899 struct trace_buffer *trace_buf = &tr->trace_buffer;
4900 struct trace_seq *s;
4902 unsigned long long t;
4903 unsigned long usec_rem;
4906 s = kmalloc(sizeof(*s), GFP_KERNEL);
4912 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
4913 trace_seq_printf(s, "entries: %ld\n", cnt);
4915 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
4916 trace_seq_printf(s, "overrun: %ld\n", cnt);
4918 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
4919 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4921 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
4922 trace_seq_printf(s, "bytes: %ld\n", cnt);
4924 if (trace_clocks[trace_clock_id].in_ns) {
4925 /* local or global for trace_clock */
4926 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
4927 usec_rem = do_div(t, USEC_PER_SEC);
4928 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
4931 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
4932 usec_rem = do_div(t, USEC_PER_SEC);
4933 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4935 /* counter or tsc mode for trace_clock */
4936 trace_seq_printf(s, "oldest event ts: %llu\n",
4937 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
4939 trace_seq_printf(s, "now ts: %llu\n",
4940 ring_buffer_time_stamp(trace_buf->buffer, cpu));
4943 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
4944 trace_seq_printf(s, "dropped events: %ld\n", cnt);
4946 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
4947 trace_seq_printf(s, "read events: %ld\n", cnt);
4949 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4956 static const struct file_operations tracing_stats_fops = {
4957 .open = tracing_open_generic,
4958 .read = tracing_stats_read,
4959 .llseek = generic_file_llseek,
4962 #ifdef CONFIG_DYNAMIC_FTRACE
4964 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4970 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
4971 size_t cnt, loff_t *ppos)
4973 static char ftrace_dyn_info_buffer[1024];
4974 static DEFINE_MUTEX(dyn_info_mutex);
4975 unsigned long *p = filp->private_data;
4976 char *buf = ftrace_dyn_info_buffer;
4977 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
4980 mutex_lock(&dyn_info_mutex);
4981 r = sprintf(buf, "%ld ", *p);
4983 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
4986 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4988 mutex_unlock(&dyn_info_mutex);
4993 static const struct file_operations tracing_dyn_info_fops = {
4994 .open = tracing_open_generic,
4995 .read = tracing_read_dyn_info,
4996 .llseek = generic_file_llseek,
5000 struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
5007 if (!debugfs_initialized())
5010 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5011 tr->dir = debugfs_create_dir("tracing", NULL);
5013 if (!tr->dir && !once) {
5015 pr_warning("Could not create debugfs directory 'tracing'\n");
5022 struct dentry *tracing_init_dentry(void)
5024 return tracing_init_dentry_tr(&global_trace);
5027 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
5029 struct dentry *d_tracer;
5032 return tr->percpu_dir;
5034 d_tracer = tracing_init_dentry_tr(tr);
5038 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
5040 WARN_ONCE(!tr->percpu_dir,
5041 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
5043 return tr->percpu_dir;
5047 tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
5049 struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
5050 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5051 struct dentry *d_cpu;
5052 char cpu_dir[30]; /* 30 characters should be more than enough */
5057 snprintf(cpu_dir, 30, "cpu%ld", cpu);
5058 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5060 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5064 /* per cpu trace_pipe */
5065 trace_create_file("trace_pipe", 0444, d_cpu,
5066 (void *)&data->trace_cpu, &tracing_pipe_fops);
5069 trace_create_file("trace", 0644, d_cpu,
5070 (void *)&data->trace_cpu, &tracing_fops);
5072 trace_create_file("trace_pipe_raw", 0444, d_cpu,
5073 (void *)&data->trace_cpu, &tracing_buffers_fops);
5075 trace_create_file("stats", 0444, d_cpu,
5076 (void *)&data->trace_cpu, &tracing_stats_fops);
5078 trace_create_file("buffer_size_kb", 0444, d_cpu,
5079 (void *)&data->trace_cpu, &tracing_entries_fops);
5081 #ifdef CONFIG_TRACER_SNAPSHOT
5082 trace_create_file("snapshot", 0644, d_cpu,
5083 (void *)&data->trace_cpu, &snapshot_fops);
5085 trace_create_file("snapshot_raw", 0444, d_cpu,
5086 (void *)&data->trace_cpu, &snapshot_raw_fops);
5090 #ifdef CONFIG_FTRACE_SELFTEST
5091 /* Let selftest have access to static functions in this file */
5092 #include "trace_selftest.c"
5095 struct trace_option_dentry {
5096 struct tracer_opt *opt;
5097 struct tracer_flags *flags;
5098 struct trace_array *tr;
5099 struct dentry *entry;
5103 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5106 struct trace_option_dentry *topt = filp->private_data;
5109 if (topt->flags->val & topt->opt->bit)
5114 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5118 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5121 struct trace_option_dentry *topt = filp->private_data;
5125 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5129 if (val != 0 && val != 1)
5132 if (!!(topt->flags->val & topt->opt->bit) != val) {
5133 mutex_lock(&trace_types_lock);
5134 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
5136 mutex_unlock(&trace_types_lock);
5147 static const struct file_operations trace_options_fops = {
5148 .open = tracing_open_generic,
5149 .read = trace_options_read,
5150 .write = trace_options_write,
5151 .llseek = generic_file_llseek,
5155 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5158 long index = (long)filp->private_data;
5161 if (trace_flags & (1 << index))
5166 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5170 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5173 struct trace_array *tr = &global_trace;
5174 long index = (long)filp->private_data;
5178 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5182 if (val != 0 && val != 1)
5185 mutex_lock(&trace_types_lock);
5186 ret = set_tracer_flag(tr, 1 << index, val);
5187 mutex_unlock(&trace_types_lock);
5197 static const struct file_operations trace_options_core_fops = {
5198 .open = tracing_open_generic,
5199 .read = trace_options_core_read,
5200 .write = trace_options_core_write,
5201 .llseek = generic_file_llseek,
5204 struct dentry *trace_create_file(const char *name,
5206 struct dentry *parent,
5208 const struct file_operations *fops)
5212 ret = debugfs_create_file(name, mode, parent, data, fops);
5214 pr_warning("Could not create debugfs '%s' entry\n", name);
5220 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
5222 struct dentry *d_tracer;
5227 d_tracer = tracing_init_dentry_tr(tr);
5231 tr->options = debugfs_create_dir("options", d_tracer);
5233 pr_warning("Could not create debugfs directory 'options'\n");
5241 create_trace_option_file(struct trace_array *tr,
5242 struct trace_option_dentry *topt,
5243 struct tracer_flags *flags,
5244 struct tracer_opt *opt)
5246 struct dentry *t_options;
5248 t_options = trace_options_init_dentry(tr);
5252 topt->flags = flags;
5256 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
5257 &trace_options_fops);
5261 static struct trace_option_dentry *
5262 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
5264 struct trace_option_dentry *topts;
5265 struct tracer_flags *flags;
5266 struct tracer_opt *opts;
5272 flags = tracer->flags;
5274 if (!flags || !flags->opts)
5279 for (cnt = 0; opts[cnt].name; cnt++)
5282 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
5286 for (cnt = 0; opts[cnt].name; cnt++)
5287 create_trace_option_file(tr, &topts[cnt], flags,
5294 destroy_trace_option_files(struct trace_option_dentry *topts)
5301 for (cnt = 0; topts[cnt].opt; cnt++) {
5302 if (topts[cnt].entry)
5303 debugfs_remove(topts[cnt].entry);
5309 static struct dentry *
5310 create_trace_option_core_file(struct trace_array *tr,
5311 const char *option, long index)
5313 struct dentry *t_options;
5315 t_options = trace_options_init_dentry(tr);
5319 return trace_create_file(option, 0644, t_options, (void *)index,
5320 &trace_options_core_fops);
5323 static __init void create_trace_options_dir(struct trace_array *tr)
5325 struct dentry *t_options;
5328 t_options = trace_options_init_dentry(tr);
5332 for (i = 0; trace_options[i]; i++)
5333 create_trace_option_core_file(tr, trace_options[i], i);
5337 rb_simple_read(struct file *filp, char __user *ubuf,
5338 size_t cnt, loff_t *ppos)
5340 struct trace_array *tr = filp->private_data;
5341 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5346 r = ring_buffer_record_is_on(buffer);
5350 r = sprintf(buf, "%d\n", r);
5352 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5356 rb_simple_write(struct file *filp, const char __user *ubuf,
5357 size_t cnt, loff_t *ppos)
5359 struct trace_array *tr = filp->private_data;
5360 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5364 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5369 mutex_lock(&trace_types_lock);
5371 ring_buffer_record_on(buffer);
5372 if (tr->current_trace->start)
5373 tr->current_trace->start(tr);
5375 ring_buffer_record_off(buffer);
5376 if (tr->current_trace->stop)
5377 tr->current_trace->stop(tr);
5379 mutex_unlock(&trace_types_lock);
5387 static const struct file_operations rb_simple_fops = {
5388 .open = tracing_open_generic,
5389 .read = rb_simple_read,
5390 .write = rb_simple_write,
5391 .llseek = default_llseek,
5394 struct dentry *trace_instance_dir;
5397 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5399 static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
5403 for_each_tracing_cpu(cpu) {
5404 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
5405 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
5406 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
5411 allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
5413 enum ring_buffer_flags rb_flags;
5415 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5417 buf->buffer = ring_buffer_alloc(size, rb_flags);
5421 buf->data = alloc_percpu(struct trace_array_cpu);
5423 ring_buffer_free(buf->buffer);
5427 init_trace_buffers(tr, buf);
5429 /* Allocate the first page for all buffers */
5430 set_buffer_entries(&tr->trace_buffer,
5431 ring_buffer_size(tr->trace_buffer.buffer, 0));
5436 static int allocate_trace_buffers(struct trace_array *tr, int size)
5440 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
5444 #ifdef CONFIG_TRACER_MAX_TRACE
5445 ret = allocate_trace_buffer(tr, &tr->max_buffer,
5446 allocate_snapshot ? size : 1);
5448 ring_buffer_free(tr->trace_buffer.buffer);
5449 free_percpu(tr->trace_buffer.data);
5452 tr->allocated_snapshot = allocate_snapshot;
5455 * Only the top level trace array gets its snapshot allocated
5456 * from the kernel command line.
5458 allocate_snapshot = false;
5463 static int new_instance_create(const char *name)
5465 struct trace_array *tr;
5468 mutex_lock(&trace_types_lock);
5471 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5472 if (tr->name && strcmp(tr->name, name) == 0)
5477 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
5481 tr->name = kstrdup(name, GFP_KERNEL);
5485 raw_spin_lock_init(&tr->start_lock);
5487 tr->current_trace = &nop_trace;
5489 INIT_LIST_HEAD(&tr->systems);
5490 INIT_LIST_HEAD(&tr->events);
5492 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
5495 /* Holder for file callbacks */
5496 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5497 tr->trace_cpu.tr = tr;
5499 tr->dir = debugfs_create_dir(name, trace_instance_dir);
5503 ret = event_trace_add_tracer(tr->dir, tr);
5507 init_tracer_debugfs(tr, tr->dir);
5509 list_add(&tr->list, &ftrace_trace_arrays);
5511 mutex_unlock(&trace_types_lock);
5516 if (tr->trace_buffer.buffer)
5517 ring_buffer_free(tr->trace_buffer.buffer);
5522 mutex_unlock(&trace_types_lock);
5528 static int instance_delete(const char *name)
5530 struct trace_array *tr;
5534 mutex_lock(&trace_types_lock);
5537 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5538 if (tr->name && strcmp(tr->name, name) == 0) {
5550 list_del(&tr->list);
5552 event_trace_del_tracer(tr);
5553 debugfs_remove_recursive(tr->dir);
5554 free_percpu(tr->trace_buffer.data);
5555 ring_buffer_free(tr->trace_buffer.buffer);
5563 mutex_unlock(&trace_types_lock);
5568 static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
5570 struct dentry *parent;
5573 /* Paranoid: Make sure the parent is the "instances" directory */
5574 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5575 if (WARN_ON_ONCE(parent != trace_instance_dir))
5579 * The inode mutex is locked, but debugfs_create_dir() will also
5580 * take the mutex. As the instances directory can not be destroyed
5581 * or changed in any other way, it is safe to unlock it, and
5582 * let the dentry try. If two users try to make the same dir at
5583 * the same time, then the new_instance_create() will determine the
5586 mutex_unlock(&inode->i_mutex);
5588 ret = new_instance_create(dentry->d_iname);
5590 mutex_lock(&inode->i_mutex);
5595 static int instance_rmdir(struct inode *inode, struct dentry *dentry)
5597 struct dentry *parent;
5600 /* Paranoid: Make sure the parent is the "instances" directory */
5601 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5602 if (WARN_ON_ONCE(parent != trace_instance_dir))
5605 /* The caller did a dget() on dentry */
5606 mutex_unlock(&dentry->d_inode->i_mutex);
5609 * The inode mutex is locked, but debugfs_create_dir() will also
5610 * take the mutex. As the instances directory can not be destroyed
5611 * or changed in any other way, it is safe to unlock it, and
5612 * let the dentry try. If two users try to make the same dir at
5613 * the same time, then the instance_delete() will determine the
5616 mutex_unlock(&inode->i_mutex);
5618 ret = instance_delete(dentry->d_iname);
5620 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
5621 mutex_lock(&dentry->d_inode->i_mutex);
5626 static const struct inode_operations instance_dir_inode_operations = {
5627 .lookup = simple_lookup,
5628 .mkdir = instance_mkdir,
5629 .rmdir = instance_rmdir,
5632 static __init void create_trace_instances(struct dentry *d_tracer)
5634 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
5635 if (WARN_ON(!trace_instance_dir))
5638 /* Hijack the dir inode operations, to allow mkdir */
5639 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
5643 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
5647 trace_create_file("trace_options", 0644, d_tracer,
5648 tr, &tracing_iter_fops);
5650 trace_create_file("trace", 0644, d_tracer,
5651 (void *)&tr->trace_cpu, &tracing_fops);
5653 trace_create_file("trace_pipe", 0444, d_tracer,
5654 (void *)&tr->trace_cpu, &tracing_pipe_fops);
5656 trace_create_file("buffer_size_kb", 0644, d_tracer,
5657 (void *)&tr->trace_cpu, &tracing_entries_fops);
5659 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
5660 tr, &tracing_total_entries_fops);
5662 trace_create_file("free_buffer", 0644, d_tracer,
5663 tr, &tracing_free_buffer_fops);
5665 trace_create_file("trace_marker", 0220, d_tracer,
5666 tr, &tracing_mark_fops);
5668 trace_create_file("trace_clock", 0644, d_tracer, tr,
5671 trace_create_file("tracing_on", 0644, d_tracer,
5672 tr, &rb_simple_fops);
5674 #ifdef CONFIG_TRACER_SNAPSHOT
5675 trace_create_file("snapshot", 0644, d_tracer,
5676 (void *)&tr->trace_cpu, &snapshot_fops);
5679 for_each_tracing_cpu(cpu)
5680 tracing_init_debugfs_percpu(tr, cpu);
5684 static __init int tracer_init_debugfs(void)
5686 struct dentry *d_tracer;
5688 trace_access_lock_init();
5690 d_tracer = tracing_init_dentry();
5692 init_tracer_debugfs(&global_trace, d_tracer);
5694 trace_create_file("tracing_cpumask", 0644, d_tracer,
5695 &global_trace, &tracing_cpumask_fops);
5697 trace_create_file("available_tracers", 0444, d_tracer,
5698 &global_trace, &show_traces_fops);
5700 trace_create_file("current_tracer", 0644, d_tracer,
5701 &global_trace, &set_tracer_fops);
5703 #ifdef CONFIG_TRACER_MAX_TRACE
5704 trace_create_file("tracing_max_latency", 0644, d_tracer,
5705 &tracing_max_latency, &tracing_max_lat_fops);
5708 trace_create_file("tracing_thresh", 0644, d_tracer,
5709 &tracing_thresh, &tracing_max_lat_fops);
5711 trace_create_file("README", 0444, d_tracer,
5712 NULL, &tracing_readme_fops);
5714 trace_create_file("saved_cmdlines", 0444, d_tracer,
5715 NULL, &tracing_saved_cmdlines_fops);
5717 #ifdef CONFIG_DYNAMIC_FTRACE
5718 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
5719 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
5722 create_trace_instances(d_tracer);
5724 create_trace_options_dir(&global_trace);
5729 static int trace_panic_handler(struct notifier_block *this,
5730 unsigned long event, void *unused)
5732 if (ftrace_dump_on_oops)
5733 ftrace_dump(ftrace_dump_on_oops);
5737 static struct notifier_block trace_panic_notifier = {
5738 .notifier_call = trace_panic_handler,
5740 .priority = 150 /* priority: INT_MAX >= x >= 0 */
5743 static int trace_die_handler(struct notifier_block *self,
5749 if (ftrace_dump_on_oops)
5750 ftrace_dump(ftrace_dump_on_oops);
5758 static struct notifier_block trace_die_notifier = {
5759 .notifier_call = trace_die_handler,
5764 * printk is set to max of 1024, we really don't need it that big.
5765 * Nothing should be printing 1000 characters anyway.
5767 #define TRACE_MAX_PRINT 1000
5770 * Define here KERN_TRACE so that we have one place to modify
5771 * it if we decide to change what log level the ftrace dump
5774 #define KERN_TRACE KERN_EMERG
5777 trace_printk_seq(struct trace_seq *s)
5779 /* Probably should print a warning here. */
5783 /* should be zero ended, but we are paranoid. */
5784 s->buffer[s->len] = 0;
5786 printk(KERN_TRACE "%s", s->buffer);
5791 void trace_init_global_iter(struct trace_iterator *iter)
5793 iter->tr = &global_trace;
5794 iter->trace = iter->tr->current_trace;
5795 iter->cpu_file = RING_BUFFER_ALL_CPUS;
5796 iter->trace_buffer = &global_trace.trace_buffer;
5800 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
5802 static arch_spinlock_t ftrace_dump_lock =
5803 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
5804 /* use static because iter can be a bit big for the stack */
5805 static struct trace_iterator iter;
5806 unsigned int old_userobj;
5807 static int dump_ran;
5808 unsigned long flags;
5812 local_irq_save(flags);
5813 arch_spin_lock(&ftrace_dump_lock);
5821 /* Did function tracer already get disabled? */
5822 if (ftrace_is_dead()) {
5823 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
5824 printk("# MAY BE MISSING FUNCTION EVENTS\n");
5827 if (disable_tracing)
5830 /* Simulate the iterator */
5831 trace_init_global_iter(&iter);
5833 for_each_tracing_cpu(cpu) {
5834 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
5837 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
5839 /* don't look at user memory in panic mode */
5840 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
5842 switch (oops_dump_mode) {
5844 iter.cpu_file = RING_BUFFER_ALL_CPUS;
5847 iter.cpu_file = raw_smp_processor_id();
5852 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
5853 iter.cpu_file = RING_BUFFER_ALL_CPUS;
5856 printk(KERN_TRACE "Dumping ftrace buffer:\n");
5859 * We need to stop all tracing on all CPUS to read the
5860 * the next buffer. This is a bit expensive, but is
5861 * not done often. We fill all what we can read,
5862 * and then release the locks again.
5865 while (!trace_empty(&iter)) {
5868 printk(KERN_TRACE "---------------------------------\n");
5872 /* reset all but tr, trace, and overruns */
5873 memset(&iter.seq, 0,
5874 sizeof(struct trace_iterator) -
5875 offsetof(struct trace_iterator, seq));
5876 iter.iter_flags |= TRACE_FILE_LAT_FMT;
5879 if (trace_find_next_entry_inc(&iter) != NULL) {
5882 ret = print_trace_line(&iter);
5883 if (ret != TRACE_TYPE_NO_CONSUME)
5884 trace_consume(&iter);
5886 touch_nmi_watchdog();
5888 trace_printk_seq(&iter.seq);
5892 printk(KERN_TRACE " (ftrace buffer empty)\n");
5894 printk(KERN_TRACE "---------------------------------\n");
5897 /* Re-enable tracing if requested */
5898 if (!disable_tracing) {
5899 trace_flags |= old_userobj;
5901 for_each_tracing_cpu(cpu) {
5902 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
5908 arch_spin_unlock(&ftrace_dump_lock);
5909 local_irq_restore(flags);
5912 /* By default: disable tracing after the dump */
5913 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
5915 __ftrace_dump(true, oops_dump_mode);
5917 EXPORT_SYMBOL_GPL(ftrace_dump);
5919 __init static int tracer_alloc_buffers(void)
5925 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
5928 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
5929 goto out_free_buffer_mask;
5931 /* Only allocate trace_printk buffers if a trace_printk exists */
5932 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
5933 /* Must be called before global_trace.buffer is allocated */
5934 trace_printk_init_buffers();
5936 /* To save memory, keep the ring buffer size to its minimum */
5937 if (ring_buffer_expanded)
5938 ring_buf_size = trace_buf_size;
5942 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
5943 cpumask_copy(tracing_cpumask, cpu_all_mask);
5945 raw_spin_lock_init(&global_trace.start_lock);
5947 /* TODO: make the number of buffers hot pluggable with CPUS */
5948 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
5949 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
5951 goto out_free_cpumask;
5954 if (global_trace.buffer_disabled)
5957 trace_init_cmdlines();
5959 register_tracer(&nop_trace);
5961 global_trace.current_trace = &nop_trace;
5963 /* All seems OK, enable tracing */
5964 tracing_disabled = 0;
5966 atomic_notifier_chain_register(&panic_notifier_list,
5967 &trace_panic_notifier);
5969 register_die_notifier(&trace_die_notifier);
5971 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
5973 /* Holder for file callbacks */
5974 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5975 global_trace.trace_cpu.tr = &global_trace;
5977 INIT_LIST_HEAD(&global_trace.systems);
5978 INIT_LIST_HEAD(&global_trace.events);
5979 list_add(&global_trace.list, &ftrace_trace_arrays);
5981 while (trace_boot_options) {
5984 option = strsep(&trace_boot_options, ",");
5985 trace_set_options(&global_trace, option);
5991 free_percpu(global_trace.trace_buffer.data);
5992 #ifdef CONFIG_TRACER_MAX_TRACE
5993 free_percpu(global_trace.max_buffer.data);
5995 free_cpumask_var(tracing_cpumask);
5996 out_free_buffer_mask:
5997 free_cpumask_var(tracing_buffer_mask);
6002 __init static int clear_boot_tracer(void)
6005 * The default tracer at boot buffer is an init section.
6006 * This function is called in lateinit. If we did not
6007 * find the boot tracer, then clear it out, to prevent
6008 * later registration from accessing the buffer that is
6009 * about to be freed.
6011 if (!default_bootup_tracer)
6014 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6015 default_bootup_tracer);
6016 default_bootup_tracer = NULL;
6021 early_initcall(tracer_alloc_buffers);
6022 fs_initcall(tracer_init_debugfs);
6023 late_initcall(clear_boot_tracer);