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 int 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 int __init set_cmdline_ftrace(char *str)
126 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
127 default_bootup_tracer = bootup_tracer_buf;
128 /* We are using ftrace early, expand it */
129 ring_buffer_expanded = 1;
132 __setup("ftrace=", set_cmdline_ftrace);
134 static int __init set_ftrace_dump_on_oops(char *str)
136 if (*str++ != '=' || !*str) {
137 ftrace_dump_on_oops = DUMP_ALL;
141 if (!strcmp("orig_cpu", str)) {
142 ftrace_dump_on_oops = DUMP_ORIG;
148 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
151 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
152 static char *trace_boot_options __initdata;
154 static int __init set_trace_boot_options(char *str)
156 strncpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
157 trace_boot_options = trace_boot_options_buf;
160 __setup("trace_options=", set_trace_boot_options);
162 unsigned long long ns2usecs(cycle_t nsec)
170 * The global_trace is the descriptor that holds the tracing
171 * buffers for the live tracing. For each CPU, it contains
172 * a link list of pages that will store trace entries. The
173 * page descriptor of the pages in the memory is used to hold
174 * the link list by linking the lru item in the page descriptor
175 * to each of the pages in the buffer per CPU.
177 * For each active CPU there is a data field that holds the
178 * pages for the buffer for that CPU. Each CPU has the same number
179 * of pages allocated for its buffer.
181 static struct trace_array global_trace;
183 LIST_HEAD(ftrace_trace_arrays);
185 int filter_current_check_discard(struct ring_buffer *buffer,
186 struct ftrace_event_call *call, void *rec,
187 struct ring_buffer_event *event)
189 return filter_check_discard(call, rec, buffer, event);
191 EXPORT_SYMBOL_GPL(filter_current_check_discard);
193 cycle_t ftrace_now(int cpu)
197 /* Early boot up does not have a buffer yet */
198 if (!global_trace.trace_buffer.buffer)
199 return trace_clock_local();
201 ts = ring_buffer_time_stamp(global_trace.trace_buffer.buffer, cpu);
202 ring_buffer_normalize_time_stamp(global_trace.trace_buffer.buffer, cpu, &ts);
207 int tracing_is_enabled(void)
209 return tracing_is_on();
213 * trace_buf_size is the size in bytes that is allocated
214 * for a buffer. Note, the number of bytes is always rounded
217 * This number is purposely set to a low number of 16384.
218 * If the dump on oops happens, it will be much appreciated
219 * to not have to wait for all that output. Anyway this can be
220 * boot time and run time configurable.
222 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
224 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
226 /* trace_types holds a link list of available tracers. */
227 static struct tracer *trace_types __read_mostly;
230 * trace_types_lock is used to protect the trace_types list.
232 static DEFINE_MUTEX(trace_types_lock);
235 * serialize the access of the ring buffer
237 * ring buffer serializes readers, but it is low level protection.
238 * The validity of the events (which returns by ring_buffer_peek() ..etc)
239 * are not protected by ring buffer.
241 * The content of events may become garbage if we allow other process consumes
242 * these events concurrently:
243 * A) the page of the consumed events may become a normal page
244 * (not reader page) in ring buffer, and this page will be rewrited
245 * by events producer.
246 * B) The page of the consumed events may become a page for splice_read,
247 * and this page will be returned to system.
249 * These primitives allow multi process access to different cpu ring buffer
252 * These primitives don't distinguish read-only and read-consume access.
253 * Multi read-only access are also serialized.
257 static DECLARE_RWSEM(all_cpu_access_lock);
258 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
260 static inline void trace_access_lock(int cpu)
262 if (cpu == RING_BUFFER_ALL_CPUS) {
263 /* gain it for accessing the whole ring buffer. */
264 down_write(&all_cpu_access_lock);
266 /* gain it for accessing a cpu ring buffer. */
268 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
269 down_read(&all_cpu_access_lock);
271 /* Secondly block other access to this @cpu ring buffer. */
272 mutex_lock(&per_cpu(cpu_access_lock, cpu));
276 static inline void trace_access_unlock(int cpu)
278 if (cpu == RING_BUFFER_ALL_CPUS) {
279 up_write(&all_cpu_access_lock);
281 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
282 up_read(&all_cpu_access_lock);
286 static inline void trace_access_lock_init(void)
290 for_each_possible_cpu(cpu)
291 mutex_init(&per_cpu(cpu_access_lock, cpu));
296 static DEFINE_MUTEX(access_lock);
298 static inline void trace_access_lock(int cpu)
301 mutex_lock(&access_lock);
304 static inline void trace_access_unlock(int cpu)
307 mutex_unlock(&access_lock);
310 static inline void trace_access_lock_init(void)
316 /* trace_flags holds trace_options default values */
317 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
318 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
319 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
320 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS;
323 * tracing_on - enable tracing buffers
325 * This function enables tracing buffers that may have been
326 * disabled with tracing_off.
328 void tracing_on(void)
330 if (global_trace.trace_buffer.buffer)
331 ring_buffer_record_on(global_trace.trace_buffer.buffer);
333 * This flag is only looked at when buffers haven't been
334 * allocated yet. We don't really care about the race
335 * between setting this flag and actually turning
338 global_trace.buffer_disabled = 0;
340 EXPORT_SYMBOL_GPL(tracing_on);
342 #ifdef CONFIG_TRACER_SNAPSHOT
344 * trace_snapshot - take a snapshot of the current buffer.
346 * This causes a swap between the snapshot buffer and the current live
347 * tracing buffer. You can use this to take snapshots of the live
348 * trace when some condition is triggered, but continue to trace.
350 * Note, make sure to allocate the snapshot with either
351 * a tracing_snapshot_alloc(), or by doing it manually
352 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
354 * If the snapshot buffer is not allocated, it will stop tracing.
355 * Basically making a permanent snapshot.
357 void tracing_snapshot(void)
359 struct trace_array *tr = &global_trace;
360 struct tracer *tracer = tr->current_trace;
363 if (!tr->allocated_snapshot) {
364 trace_printk("*** SNAPSHOT NOT ALLOCATED ***\n");
365 trace_printk("*** stopping trace here! ***\n");
370 /* Note, snapshot can not be used when the tracer uses it */
371 if (tracer->use_max_tr) {
372 trace_printk("*** LATENCY TRACER ACTIVE ***\n");
373 trace_printk("*** Can not use snapshot (sorry) ***\n");
377 local_irq_save(flags);
378 update_max_tr(tr, current, smp_processor_id());
379 local_irq_restore(flags);
382 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
383 struct trace_buffer *size_buf, int cpu_id);
386 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
388 * This is similar to trace_snapshot(), but it will allocate the
389 * snapshot buffer if it isn't already allocated. Use this only
390 * where it is safe to sleep, as the allocation may sleep.
392 * This causes a swap between the snapshot buffer and the current live
393 * tracing buffer. You can use this to take snapshots of the live
394 * trace when some condition is triggered, but continue to trace.
396 void tracing_snapshot_alloc(void)
398 struct trace_array *tr = &global_trace;
401 if (!tr->allocated_snapshot) {
403 /* allocate spare buffer */
404 ret = resize_buffer_duplicate_size(&tr->max_buffer,
405 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
406 if (WARN_ON(ret < 0))
409 tr->allocated_snapshot = true;
415 void tracing_snapshot(void)
417 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
419 void tracing_snapshot_alloc(void)
424 #endif /* CONFIG_TRACER_SNAPSHOT */
427 * tracing_off - turn off tracing buffers
429 * This function stops the tracing buffers from recording data.
430 * It does not disable any overhead the tracers themselves may
431 * be causing. This function simply causes all recording to
432 * the ring buffers to fail.
434 void tracing_off(void)
436 if (global_trace.trace_buffer.buffer)
437 ring_buffer_record_off(global_trace.trace_buffer.buffer);
439 * This flag is only looked at when buffers haven't been
440 * allocated yet. We don't really care about the race
441 * between setting this flag and actually turning
444 global_trace.buffer_disabled = 1;
446 EXPORT_SYMBOL_GPL(tracing_off);
449 * tracing_is_on - show state of ring buffers enabled
451 int tracing_is_on(void)
453 if (global_trace.trace_buffer.buffer)
454 return ring_buffer_record_is_on(global_trace.trace_buffer.buffer);
455 return !global_trace.buffer_disabled;
457 EXPORT_SYMBOL_GPL(tracing_is_on);
459 static int __init set_buf_size(char *str)
461 unsigned long buf_size;
465 buf_size = memparse(str, &str);
466 /* nr_entries can not be zero */
469 trace_buf_size = buf_size;
472 __setup("trace_buf_size=", set_buf_size);
474 static int __init set_tracing_thresh(char *str)
476 unsigned long threshold;
481 ret = kstrtoul(str, 0, &threshold);
484 tracing_thresh = threshold * 1000;
487 __setup("tracing_thresh=", set_tracing_thresh);
489 unsigned long nsecs_to_usecs(unsigned long nsecs)
494 /* These must match the bit postions in trace_iterator_flags */
495 static const char *trace_options[] = {
527 int in_ns; /* is this clock in nanoseconds? */
529 { trace_clock_local, "local", 1 },
530 { trace_clock_global, "global", 1 },
531 { trace_clock_counter, "counter", 0 },
538 * trace_parser_get_init - gets the buffer for trace parser
540 int trace_parser_get_init(struct trace_parser *parser, int size)
542 memset(parser, 0, sizeof(*parser));
544 parser->buffer = kmalloc(size, GFP_KERNEL);
553 * trace_parser_put - frees the buffer for trace parser
555 void trace_parser_put(struct trace_parser *parser)
557 kfree(parser->buffer);
561 * trace_get_user - reads the user input string separated by space
562 * (matched by isspace(ch))
564 * For each string found the 'struct trace_parser' is updated,
565 * and the function returns.
567 * Returns number of bytes read.
569 * See kernel/trace/trace.h for 'struct trace_parser' details.
571 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
572 size_t cnt, loff_t *ppos)
579 trace_parser_clear(parser);
581 ret = get_user(ch, ubuf++);
589 * The parser is not finished with the last write,
590 * continue reading the user input without skipping spaces.
593 /* skip white space */
594 while (cnt && isspace(ch)) {
595 ret = get_user(ch, ubuf++);
602 /* only spaces were written */
612 /* read the non-space input */
613 while (cnt && !isspace(ch)) {
614 if (parser->idx < parser->size - 1)
615 parser->buffer[parser->idx++] = ch;
620 ret = get_user(ch, ubuf++);
627 /* We either got finished input or we have to wait for another call. */
629 parser->buffer[parser->idx] = 0;
630 parser->cont = false;
633 parser->buffer[parser->idx++] = ch;
643 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
651 if (s->len <= s->readpos)
654 len = s->len - s->readpos;
657 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
667 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
671 if (s->len <= s->readpos)
674 len = s->len - s->readpos;
677 memcpy(buf, s->buffer + s->readpos, cnt);
684 * ftrace_max_lock is used to protect the swapping of buffers
685 * when taking a max snapshot. The buffers themselves are
686 * protected by per_cpu spinlocks. But the action of the swap
687 * needs its own lock.
689 * This is defined as a arch_spinlock_t in order to help
690 * with performance when lockdep debugging is enabled.
692 * It is also used in other places outside the update_max_tr
693 * so it needs to be defined outside of the
694 * CONFIG_TRACER_MAX_TRACE.
696 static arch_spinlock_t ftrace_max_lock =
697 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
699 unsigned long __read_mostly tracing_thresh;
701 #ifdef CONFIG_TRACER_MAX_TRACE
702 unsigned long __read_mostly tracing_max_latency;
705 * Copy the new maximum trace into the separate maximum-trace
706 * structure. (this way the maximum trace is permanently saved,
707 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
710 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
712 struct trace_buffer *trace_buf = &tr->trace_buffer;
713 struct trace_buffer *max_buf = &tr->max_buffer;
714 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
715 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
718 max_buf->time_start = data->preempt_timestamp;
720 max_data->saved_latency = tracing_max_latency;
721 max_data->critical_start = data->critical_start;
722 max_data->critical_end = data->critical_end;
724 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
725 max_data->pid = tsk->pid;
726 max_data->uid = task_uid(tsk);
727 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
728 max_data->policy = tsk->policy;
729 max_data->rt_priority = tsk->rt_priority;
731 /* record this tasks comm */
732 tracing_record_cmdline(tsk);
736 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
738 * @tsk: the task with the latency
739 * @cpu: The cpu that initiated the trace.
741 * Flip the buffers between the @tr and the max_tr and record information
742 * about which task was the cause of this latency.
745 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
747 struct ring_buffer *buf;
752 WARN_ON_ONCE(!irqs_disabled());
754 if (!tr->allocated_snapshot) {
755 /* Only the nop tracer should hit this when disabling */
756 WARN_ON_ONCE(tr->current_trace != &nop_trace);
760 arch_spin_lock(&ftrace_max_lock);
762 buf = tr->trace_buffer.buffer;
763 tr->trace_buffer.buffer = tr->max_buffer.buffer;
764 tr->max_buffer.buffer = buf;
766 __update_max_tr(tr, tsk, cpu);
767 arch_spin_unlock(&ftrace_max_lock);
771 * update_max_tr_single - only copy one trace over, and reset the rest
773 * @tsk - task with the latency
774 * @cpu - the cpu of the buffer to copy.
776 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
779 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
786 WARN_ON_ONCE(!irqs_disabled());
787 if (WARN_ON_ONCE(!tr->allocated_snapshot))
790 arch_spin_lock(&ftrace_max_lock);
792 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
796 * We failed to swap the buffer due to a commit taking
797 * place on this CPU. We fail to record, but we reset
798 * the max trace buffer (no one writes directly to it)
799 * and flag that it failed.
801 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
802 "Failed to swap buffers due to commit in progress\n");
805 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
807 __update_max_tr(tr, tsk, cpu);
808 arch_spin_unlock(&ftrace_max_lock);
810 #endif /* CONFIG_TRACER_MAX_TRACE */
812 static void default_wait_pipe(struct trace_iterator *iter)
814 /* Iterators are static, they should be filled or empty */
815 if (trace_buffer_iter(iter, iter->cpu_file))
818 ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
822 * register_tracer - register a tracer with the ftrace system.
823 * @type - the plugin for the tracer
825 * Register a new plugin tracer.
827 int register_tracer(struct tracer *type)
833 pr_info("Tracer must have a name\n");
837 if (strlen(type->name) >= MAX_TRACER_SIZE) {
838 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
842 mutex_lock(&trace_types_lock);
844 tracing_selftest_running = true;
846 for (t = trace_types; t; t = t->next) {
847 if (strcmp(type->name, t->name) == 0) {
849 pr_info("Tracer %s already registered\n",
857 type->set_flag = &dummy_set_flag;
859 type->flags = &dummy_tracer_flags;
861 if (!type->flags->opts)
862 type->flags->opts = dummy_tracer_opt;
863 if (!type->wait_pipe)
864 type->wait_pipe = default_wait_pipe;
867 #ifdef CONFIG_FTRACE_STARTUP_TEST
868 if (type->selftest && !tracing_selftest_disabled) {
869 struct trace_array *tr = &global_trace;
870 struct tracer *saved_tracer = tr->current_trace;
873 * Run a selftest on this tracer.
874 * Here we reset the trace buffer, and set the current
875 * tracer to be this tracer. The tracer can then run some
876 * internal tracing to verify that everything is in order.
877 * If we fail, we do not register this tracer.
879 tracing_reset_online_cpus(&tr->trace_buffer);
881 tr->current_trace = type;
883 #ifdef CONFIG_TRACER_MAX_TRACE
884 if (type->use_max_tr) {
885 /* If we expanded the buffers, make sure the max is expanded too */
886 if (ring_buffer_expanded)
887 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
888 RING_BUFFER_ALL_CPUS);
889 tr->allocated_snapshot = true;
893 /* the test is responsible for initializing and enabling */
894 pr_info("Testing tracer %s: ", type->name);
895 ret = type->selftest(type, tr);
896 /* the test is responsible for resetting too */
897 tr->current_trace = saved_tracer;
899 printk(KERN_CONT "FAILED!\n");
900 /* Add the warning after printing 'FAILED' */
904 /* Only reset on passing, to avoid touching corrupted buffers */
905 tracing_reset_online_cpus(&tr->trace_buffer);
907 #ifdef CONFIG_TRACER_MAX_TRACE
908 if (type->use_max_tr) {
909 tr->allocated_snapshot = false;
911 /* Shrink the max buffer again */
912 if (ring_buffer_expanded)
913 ring_buffer_resize(tr->max_buffer.buffer, 1,
914 RING_BUFFER_ALL_CPUS);
918 printk(KERN_CONT "PASSED\n");
922 type->next = trace_types;
926 tracing_selftest_running = false;
927 mutex_unlock(&trace_types_lock);
929 if (ret || !default_bootup_tracer)
932 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
935 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
936 /* Do we want this tracer to start on bootup? */
937 tracing_set_tracer(type->name);
938 default_bootup_tracer = NULL;
939 /* disable other selftests, since this will break it. */
940 tracing_selftest_disabled = 1;
941 #ifdef CONFIG_FTRACE_STARTUP_TEST
942 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
950 void tracing_reset(struct trace_buffer *buf, int cpu)
952 struct ring_buffer *buffer = buf->buffer;
957 ring_buffer_record_disable(buffer);
959 /* Make sure all commits have finished */
961 ring_buffer_reset_cpu(buffer, cpu);
963 ring_buffer_record_enable(buffer);
966 void tracing_reset_online_cpus(struct trace_buffer *buf)
968 struct ring_buffer *buffer = buf->buffer;
974 ring_buffer_record_disable(buffer);
976 /* Make sure all commits have finished */
979 buf->time_start = ftrace_now(buf->cpu);
981 for_each_online_cpu(cpu)
982 ring_buffer_reset_cpu(buffer, cpu);
984 ring_buffer_record_enable(buffer);
987 void tracing_reset_current(int cpu)
989 tracing_reset(&global_trace.trace_buffer, cpu);
992 void tracing_reset_all_online_cpus(void)
994 struct trace_array *tr;
996 mutex_lock(&trace_types_lock);
997 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
998 tracing_reset_online_cpus(&tr->trace_buffer);
999 #ifdef CONFIG_TRACER_MAX_TRACE
1000 tracing_reset_online_cpus(&tr->max_buffer);
1003 mutex_unlock(&trace_types_lock);
1006 #define SAVED_CMDLINES 128
1007 #define NO_CMDLINE_MAP UINT_MAX
1008 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1009 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1010 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1011 static int cmdline_idx;
1012 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1014 /* temporary disable recording */
1015 static atomic_t trace_record_cmdline_disabled __read_mostly;
1017 static void trace_init_cmdlines(void)
1019 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1020 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
1024 int is_tracing_stopped(void)
1026 return global_trace.stop_count;
1030 * ftrace_off_permanent - disable all ftrace code permanently
1032 * This should only be called when a serious anomally has
1033 * been detected. This will turn off the function tracing,
1034 * ring buffers, and other tracing utilites. It takes no
1035 * locks and can be called from any context.
1037 void ftrace_off_permanent(void)
1039 tracing_disabled = 1;
1041 tracing_off_permanent();
1045 * tracing_start - quick start of the tracer
1047 * If tracing is enabled but was stopped by tracing_stop,
1048 * this will start the tracer back up.
1050 void tracing_start(void)
1052 struct ring_buffer *buffer;
1053 unsigned long flags;
1055 if (tracing_disabled)
1058 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1059 if (--global_trace.stop_count) {
1060 if (global_trace.stop_count < 0) {
1061 /* Someone screwed up their debugging */
1063 global_trace.stop_count = 0;
1068 /* Prevent the buffers from switching */
1069 arch_spin_lock(&ftrace_max_lock);
1071 buffer = global_trace.trace_buffer.buffer;
1073 ring_buffer_record_enable(buffer);
1075 #ifdef CONFIG_TRACER_MAX_TRACE
1076 buffer = global_trace.max_buffer.buffer;
1078 ring_buffer_record_enable(buffer);
1081 arch_spin_unlock(&ftrace_max_lock);
1085 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1088 static void tracing_start_tr(struct trace_array *tr)
1090 struct ring_buffer *buffer;
1091 unsigned long flags;
1093 if (tracing_disabled)
1096 /* If global, we need to also start the max tracer */
1097 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1098 return tracing_start();
1100 raw_spin_lock_irqsave(&tr->start_lock, flags);
1102 if (--tr->stop_count) {
1103 if (tr->stop_count < 0) {
1104 /* Someone screwed up their debugging */
1111 buffer = tr->trace_buffer.buffer;
1113 ring_buffer_record_enable(buffer);
1116 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1120 * tracing_stop - quick stop of the tracer
1122 * Light weight way to stop tracing. Use in conjunction with
1125 void tracing_stop(void)
1127 struct ring_buffer *buffer;
1128 unsigned long flags;
1131 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1132 if (global_trace.stop_count++)
1135 /* Prevent the buffers from switching */
1136 arch_spin_lock(&ftrace_max_lock);
1138 buffer = global_trace.trace_buffer.buffer;
1140 ring_buffer_record_disable(buffer);
1142 #ifdef CONFIG_TRACER_MAX_TRACE
1143 buffer = global_trace.max_buffer.buffer;
1145 ring_buffer_record_disable(buffer);
1148 arch_spin_unlock(&ftrace_max_lock);
1151 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1154 static void tracing_stop_tr(struct trace_array *tr)
1156 struct ring_buffer *buffer;
1157 unsigned long flags;
1159 /* If global, we need to also stop the max tracer */
1160 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1161 return tracing_stop();
1163 raw_spin_lock_irqsave(&tr->start_lock, flags);
1164 if (tr->stop_count++)
1167 buffer = tr->trace_buffer.buffer;
1169 ring_buffer_record_disable(buffer);
1172 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1175 void trace_stop_cmdline_recording(void);
1177 static void trace_save_cmdline(struct task_struct *tsk)
1181 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1185 * It's not the end of the world if we don't get
1186 * the lock, but we also don't want to spin
1187 * nor do we want to disable interrupts,
1188 * so if we miss here, then better luck next time.
1190 if (!arch_spin_trylock(&trace_cmdline_lock))
1193 idx = map_pid_to_cmdline[tsk->pid];
1194 if (idx == NO_CMDLINE_MAP) {
1195 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1198 * Check whether the cmdline buffer at idx has a pid
1199 * mapped. We are going to overwrite that entry so we
1200 * need to clear the map_pid_to_cmdline. Otherwise we
1201 * would read the new comm for the old pid.
1203 pid = map_cmdline_to_pid[idx];
1204 if (pid != NO_CMDLINE_MAP)
1205 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1207 map_cmdline_to_pid[idx] = tsk->pid;
1208 map_pid_to_cmdline[tsk->pid] = idx;
1213 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1215 arch_spin_unlock(&trace_cmdline_lock);
1218 void trace_find_cmdline(int pid, char comm[])
1223 strcpy(comm, "<idle>");
1227 if (WARN_ON_ONCE(pid < 0)) {
1228 strcpy(comm, "<XXX>");
1232 if (pid > PID_MAX_DEFAULT) {
1233 strcpy(comm, "<...>");
1238 arch_spin_lock(&trace_cmdline_lock);
1239 map = map_pid_to_cmdline[pid];
1240 if (map != NO_CMDLINE_MAP)
1241 strcpy(comm, saved_cmdlines[map]);
1243 strcpy(comm, "<...>");
1245 arch_spin_unlock(&trace_cmdline_lock);
1249 void tracing_record_cmdline(struct task_struct *tsk)
1251 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1254 if (!__this_cpu_read(trace_cmdline_save))
1257 __this_cpu_write(trace_cmdline_save, false);
1259 trace_save_cmdline(tsk);
1263 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1266 struct task_struct *tsk = current;
1268 entry->preempt_count = pc & 0xff;
1269 entry->pid = (tsk) ? tsk->pid : 0;
1271 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1272 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1274 TRACE_FLAG_IRQS_NOSUPPORT |
1276 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1277 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1278 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1280 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1282 struct ring_buffer_event *
1283 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1286 unsigned long flags, int pc)
1288 struct ring_buffer_event *event;
1290 event = ring_buffer_lock_reserve(buffer, len);
1291 if (event != NULL) {
1292 struct trace_entry *ent = ring_buffer_event_data(event);
1294 tracing_generic_entry_update(ent, flags, pc);
1302 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1304 __this_cpu_write(trace_cmdline_save, true);
1305 ring_buffer_unlock_commit(buffer, event);
1309 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1310 struct ring_buffer_event *event,
1311 unsigned long flags, int pc)
1313 __buffer_unlock_commit(buffer, event);
1315 ftrace_trace_stack(buffer, flags, 6, pc);
1316 ftrace_trace_userstack(buffer, flags, pc);
1319 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1320 struct ring_buffer_event *event,
1321 unsigned long flags, int pc)
1323 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1325 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1327 struct ring_buffer_event *
1328 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1329 struct ftrace_event_file *ftrace_file,
1330 int type, unsigned long len,
1331 unsigned long flags, int pc)
1333 *current_rb = ftrace_file->tr->trace_buffer.buffer;
1334 return trace_buffer_lock_reserve(*current_rb,
1335 type, len, flags, pc);
1337 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1339 struct ring_buffer_event *
1340 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1341 int type, unsigned long len,
1342 unsigned long flags, int pc)
1344 *current_rb = global_trace.trace_buffer.buffer;
1345 return trace_buffer_lock_reserve(*current_rb,
1346 type, len, flags, pc);
1348 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1350 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1351 struct ring_buffer_event *event,
1352 unsigned long flags, int pc)
1354 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1356 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1358 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1359 struct ring_buffer_event *event,
1360 unsigned long flags, int pc,
1361 struct pt_regs *regs)
1363 __buffer_unlock_commit(buffer, event);
1365 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1366 ftrace_trace_userstack(buffer, flags, pc);
1368 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1370 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1371 struct ring_buffer_event *event)
1373 ring_buffer_discard_commit(buffer, event);
1375 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1378 trace_function(struct trace_array *tr,
1379 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1382 struct ftrace_event_call *call = &event_function;
1383 struct ring_buffer *buffer = tr->trace_buffer.buffer;
1384 struct ring_buffer_event *event;
1385 struct ftrace_entry *entry;
1387 /* If we are reading the ring buffer, don't trace */
1388 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1391 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1395 entry = ring_buffer_event_data(event);
1397 entry->parent_ip = parent_ip;
1399 if (!filter_check_discard(call, entry, buffer, event))
1400 __buffer_unlock_commit(buffer, event);
1404 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1405 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1408 if (likely(!atomic_read(&data->disabled)))
1409 trace_function(tr, ip, parent_ip, flags, pc);
1412 #ifdef CONFIG_STACKTRACE
1414 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1415 struct ftrace_stack {
1416 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1419 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1420 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1422 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1423 unsigned long flags,
1424 int skip, int pc, struct pt_regs *regs)
1426 struct ftrace_event_call *call = &event_kernel_stack;
1427 struct ring_buffer_event *event;
1428 struct stack_entry *entry;
1429 struct stack_trace trace;
1431 int size = FTRACE_STACK_ENTRIES;
1433 trace.nr_entries = 0;
1437 * Since events can happen in NMIs there's no safe way to
1438 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1439 * or NMI comes in, it will just have to use the default
1440 * FTRACE_STACK_SIZE.
1442 preempt_disable_notrace();
1444 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1446 * We don't need any atomic variables, just a barrier.
1447 * If an interrupt comes in, we don't care, because it would
1448 * have exited and put the counter back to what we want.
1449 * We just need a barrier to keep gcc from moving things
1453 if (use_stack == 1) {
1454 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1455 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1458 save_stack_trace_regs(regs, &trace);
1460 save_stack_trace(&trace);
1462 if (trace.nr_entries > size)
1463 size = trace.nr_entries;
1465 /* From now on, use_stack is a boolean */
1468 size *= sizeof(unsigned long);
1470 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1471 sizeof(*entry) + size, flags, pc);
1474 entry = ring_buffer_event_data(event);
1476 memset(&entry->caller, 0, size);
1479 memcpy(&entry->caller, trace.entries,
1480 trace.nr_entries * sizeof(unsigned long));
1482 trace.max_entries = FTRACE_STACK_ENTRIES;
1483 trace.entries = entry->caller;
1485 save_stack_trace_regs(regs, &trace);
1487 save_stack_trace(&trace);
1490 entry->size = trace.nr_entries;
1492 if (!filter_check_discard(call, entry, buffer, event))
1493 __buffer_unlock_commit(buffer, event);
1496 /* Again, don't let gcc optimize things here */
1498 __this_cpu_dec(ftrace_stack_reserve);
1499 preempt_enable_notrace();
1503 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1504 int skip, int pc, struct pt_regs *regs)
1506 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1509 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1512 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1515 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1518 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1521 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1524 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
1528 * trace_dump_stack - record a stack back trace in the trace buffer
1530 void trace_dump_stack(void)
1532 unsigned long flags;
1534 if (tracing_disabled || tracing_selftest_running)
1537 local_save_flags(flags);
1539 /* skipping 3 traces, seems to get us at the caller of this function */
1540 __ftrace_trace_stack(global_trace.trace_buffer.buffer, flags, 3,
1541 preempt_count(), NULL);
1544 static DEFINE_PER_CPU(int, user_stack_count);
1547 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1549 struct ftrace_event_call *call = &event_user_stack;
1550 struct ring_buffer_event *event;
1551 struct userstack_entry *entry;
1552 struct stack_trace trace;
1554 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1558 * NMIs can not handle page faults, even with fix ups.
1559 * The save user stack can (and often does) fault.
1561 if (unlikely(in_nmi()))
1565 * prevent recursion, since the user stack tracing may
1566 * trigger other kernel events.
1569 if (__this_cpu_read(user_stack_count))
1572 __this_cpu_inc(user_stack_count);
1574 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1575 sizeof(*entry), flags, pc);
1577 goto out_drop_count;
1578 entry = ring_buffer_event_data(event);
1580 entry->tgid = current->tgid;
1581 memset(&entry->caller, 0, sizeof(entry->caller));
1583 trace.nr_entries = 0;
1584 trace.max_entries = FTRACE_STACK_ENTRIES;
1586 trace.entries = entry->caller;
1588 save_stack_trace_user(&trace);
1589 if (!filter_check_discard(call, entry, buffer, event))
1590 __buffer_unlock_commit(buffer, event);
1593 __this_cpu_dec(user_stack_count);
1599 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1601 ftrace_trace_userstack(tr, flags, preempt_count());
1605 #endif /* CONFIG_STACKTRACE */
1607 /* created for use with alloc_percpu */
1608 struct trace_buffer_struct {
1609 char buffer[TRACE_BUF_SIZE];
1612 static struct trace_buffer_struct *trace_percpu_buffer;
1613 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1614 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1615 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1618 * The buffer used is dependent on the context. There is a per cpu
1619 * buffer for normal context, softirq contex, hard irq context and
1620 * for NMI context. Thise allows for lockless recording.
1622 * Note, if the buffers failed to be allocated, then this returns NULL
1624 static char *get_trace_buf(void)
1626 struct trace_buffer_struct *percpu_buffer;
1629 * If we have allocated per cpu buffers, then we do not
1630 * need to do any locking.
1633 percpu_buffer = trace_percpu_nmi_buffer;
1635 percpu_buffer = trace_percpu_irq_buffer;
1636 else if (in_softirq())
1637 percpu_buffer = trace_percpu_sirq_buffer;
1639 percpu_buffer = trace_percpu_buffer;
1644 return this_cpu_ptr(&percpu_buffer->buffer[0]);
1647 static int alloc_percpu_trace_buffer(void)
1649 struct trace_buffer_struct *buffers;
1650 struct trace_buffer_struct *sirq_buffers;
1651 struct trace_buffer_struct *irq_buffers;
1652 struct trace_buffer_struct *nmi_buffers;
1654 buffers = alloc_percpu(struct trace_buffer_struct);
1658 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1662 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1666 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1670 trace_percpu_buffer = buffers;
1671 trace_percpu_sirq_buffer = sirq_buffers;
1672 trace_percpu_irq_buffer = irq_buffers;
1673 trace_percpu_nmi_buffer = nmi_buffers;
1678 free_percpu(irq_buffers);
1680 free_percpu(sirq_buffers);
1682 free_percpu(buffers);
1684 WARN(1, "Could not allocate percpu trace_printk buffer");
1688 static int buffers_allocated;
1690 void trace_printk_init_buffers(void)
1692 if (buffers_allocated)
1695 if (alloc_percpu_trace_buffer())
1698 pr_info("ftrace: Allocated trace_printk buffers\n");
1700 /* Expand the buffers to set size */
1701 tracing_update_buffers();
1703 buffers_allocated = 1;
1706 * trace_printk_init_buffers() can be called by modules.
1707 * If that happens, then we need to start cmdline recording
1708 * directly here. If the global_trace.buffer is already
1709 * allocated here, then this was called by module code.
1711 if (global_trace.trace_buffer.buffer)
1712 tracing_start_cmdline_record();
1715 void trace_printk_start_comm(void)
1717 /* Start tracing comms if trace printk is set */
1718 if (!buffers_allocated)
1720 tracing_start_cmdline_record();
1723 static void trace_printk_start_stop_comm(int enabled)
1725 if (!buffers_allocated)
1729 tracing_start_cmdline_record();
1731 tracing_stop_cmdline_record();
1735 * trace_vbprintk - write binary msg to tracing buffer
1738 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1740 struct ftrace_event_call *call = &event_bprint;
1741 struct ring_buffer_event *event;
1742 struct ring_buffer *buffer;
1743 struct trace_array *tr = &global_trace;
1744 struct bprint_entry *entry;
1745 unsigned long flags;
1747 int len = 0, size, pc;
1749 if (unlikely(tracing_selftest_running || tracing_disabled))
1752 /* Don't pollute graph traces with trace_vprintk internals */
1753 pause_graph_tracing();
1755 pc = preempt_count();
1756 preempt_disable_notrace();
1758 tbuffer = get_trace_buf();
1764 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1766 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1769 local_save_flags(flags);
1770 size = sizeof(*entry) + sizeof(u32) * len;
1771 buffer = tr->trace_buffer.buffer;
1772 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1776 entry = ring_buffer_event_data(event);
1780 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1781 if (!filter_check_discard(call, entry, buffer, event)) {
1782 __buffer_unlock_commit(buffer, event);
1783 ftrace_trace_stack(buffer, flags, 6, pc);
1787 preempt_enable_notrace();
1788 unpause_graph_tracing();
1792 EXPORT_SYMBOL_GPL(trace_vbprintk);
1795 __trace_array_vprintk(struct ring_buffer *buffer,
1796 unsigned long ip, const char *fmt, va_list args)
1798 struct ftrace_event_call *call = &event_print;
1799 struct ring_buffer_event *event;
1800 int len = 0, size, pc;
1801 struct print_entry *entry;
1802 unsigned long flags;
1805 if (tracing_disabled || tracing_selftest_running)
1808 /* Don't pollute graph traces with trace_vprintk internals */
1809 pause_graph_tracing();
1811 pc = preempt_count();
1812 preempt_disable_notrace();
1815 tbuffer = get_trace_buf();
1821 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1822 if (len > TRACE_BUF_SIZE)
1825 local_save_flags(flags);
1826 size = sizeof(*entry) + len + 1;
1827 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1831 entry = ring_buffer_event_data(event);
1834 memcpy(&entry->buf, tbuffer, len);
1835 entry->buf[len] = '\0';
1836 if (!filter_check_discard(call, entry, buffer, event)) {
1837 __buffer_unlock_commit(buffer, event);
1838 ftrace_trace_stack(buffer, flags, 6, pc);
1841 preempt_enable_notrace();
1842 unpause_graph_tracing();
1847 int trace_array_vprintk(struct trace_array *tr,
1848 unsigned long ip, const char *fmt, va_list args)
1850 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
1853 int trace_array_printk(struct trace_array *tr,
1854 unsigned long ip, const char *fmt, ...)
1859 if (!(trace_flags & TRACE_ITER_PRINTK))
1863 ret = trace_array_vprintk(tr, ip, fmt, ap);
1868 int trace_array_printk_buf(struct ring_buffer *buffer,
1869 unsigned long ip, const char *fmt, ...)
1874 if (!(trace_flags & TRACE_ITER_PRINTK))
1878 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
1883 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1885 return trace_array_vprintk(&global_trace, ip, fmt, args);
1887 EXPORT_SYMBOL_GPL(trace_vprintk);
1889 static void trace_iterator_increment(struct trace_iterator *iter)
1891 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
1895 ring_buffer_read(buf_iter, NULL);
1898 static struct trace_entry *
1899 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1900 unsigned long *lost_events)
1902 struct ring_buffer_event *event;
1903 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
1906 event = ring_buffer_iter_peek(buf_iter, ts);
1908 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
1912 iter->ent_size = ring_buffer_event_length(event);
1913 return ring_buffer_event_data(event);
1919 static struct trace_entry *
1920 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1921 unsigned long *missing_events, u64 *ent_ts)
1923 struct ring_buffer *buffer = iter->trace_buffer->buffer;
1924 struct trace_entry *ent, *next = NULL;
1925 unsigned long lost_events = 0, next_lost = 0;
1926 int cpu_file = iter->cpu_file;
1927 u64 next_ts = 0, ts;
1933 * If we are in a per_cpu trace file, don't bother by iterating over
1934 * all cpu and peek directly.
1936 if (cpu_file > RING_BUFFER_ALL_CPUS) {
1937 if (ring_buffer_empty_cpu(buffer, cpu_file))
1939 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1941 *ent_cpu = cpu_file;
1946 for_each_tracing_cpu(cpu) {
1948 if (ring_buffer_empty_cpu(buffer, cpu))
1951 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1954 * Pick the entry with the smallest timestamp:
1956 if (ent && (!next || ts < next_ts)) {
1960 next_lost = lost_events;
1961 next_size = iter->ent_size;
1965 iter->ent_size = next_size;
1968 *ent_cpu = next_cpu;
1974 *missing_events = next_lost;
1979 /* Find the next real entry, without updating the iterator itself */
1980 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1981 int *ent_cpu, u64 *ent_ts)
1983 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1986 /* Find the next real entry, and increment the iterator to the next entry */
1987 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1989 iter->ent = __find_next_entry(iter, &iter->cpu,
1990 &iter->lost_events, &iter->ts);
1993 trace_iterator_increment(iter);
1995 return iter->ent ? iter : NULL;
1998 static void trace_consume(struct trace_iterator *iter)
2000 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
2001 &iter->lost_events);
2004 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
2006 struct trace_iterator *iter = m->private;
2010 WARN_ON_ONCE(iter->leftover);
2014 /* can't go backwards */
2019 ent = trace_find_next_entry_inc(iter);
2023 while (ent && iter->idx < i)
2024 ent = trace_find_next_entry_inc(iter);
2031 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2033 struct ring_buffer_event *event;
2034 struct ring_buffer_iter *buf_iter;
2035 unsigned long entries = 0;
2038 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2040 buf_iter = trace_buffer_iter(iter, cpu);
2044 ring_buffer_iter_reset(buf_iter);
2047 * We could have the case with the max latency tracers
2048 * that a reset never took place on a cpu. This is evident
2049 * by the timestamp being before the start of the buffer.
2051 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
2052 if (ts >= iter->trace_buffer->time_start)
2055 ring_buffer_read(buf_iter, NULL);
2058 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2062 * The current tracer is copied to avoid a global locking
2065 static void *s_start(struct seq_file *m, loff_t *pos)
2067 struct trace_iterator *iter = m->private;
2068 struct trace_array *tr = iter->tr;
2069 int cpu_file = iter->cpu_file;
2075 * copy the tracer to avoid using a global lock all around.
2076 * iter->trace is a copy of current_trace, the pointer to the
2077 * name may be used instead of a strcmp(), as iter->trace->name
2078 * will point to the same string as current_trace->name.
2080 mutex_lock(&trace_types_lock);
2081 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2082 *iter->trace = *tr->current_trace;
2083 mutex_unlock(&trace_types_lock);
2085 #ifdef CONFIG_TRACER_MAX_TRACE
2086 if (iter->snapshot && iter->trace->use_max_tr)
2087 return ERR_PTR(-EBUSY);
2090 if (!iter->snapshot)
2091 atomic_inc(&trace_record_cmdline_disabled);
2093 if (*pos != iter->pos) {
2098 if (cpu_file == RING_BUFFER_ALL_CPUS) {
2099 for_each_tracing_cpu(cpu)
2100 tracing_iter_reset(iter, cpu);
2102 tracing_iter_reset(iter, cpu_file);
2105 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2110 * If we overflowed the seq_file before, then we want
2111 * to just reuse the trace_seq buffer again.
2117 p = s_next(m, p, &l);
2121 trace_event_read_lock();
2122 trace_access_lock(cpu_file);
2126 static void s_stop(struct seq_file *m, void *p)
2128 struct trace_iterator *iter = m->private;
2130 #ifdef CONFIG_TRACER_MAX_TRACE
2131 if (iter->snapshot && iter->trace->use_max_tr)
2135 if (!iter->snapshot)
2136 atomic_dec(&trace_record_cmdline_disabled);
2138 trace_access_unlock(iter->cpu_file);
2139 trace_event_read_unlock();
2143 get_total_entries(struct trace_buffer *buf,
2144 unsigned long *total, unsigned long *entries)
2146 unsigned long count;
2152 for_each_tracing_cpu(cpu) {
2153 count = ring_buffer_entries_cpu(buf->buffer, cpu);
2155 * If this buffer has skipped entries, then we hold all
2156 * entries for the trace and we need to ignore the
2157 * ones before the time stamp.
2159 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2160 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
2161 /* total is the same as the entries */
2165 ring_buffer_overrun_cpu(buf->buffer, cpu);
2170 static void print_lat_help_header(struct seq_file *m)
2172 seq_puts(m, "# _------=> CPU# \n");
2173 seq_puts(m, "# / _-----=> irqs-off \n");
2174 seq_puts(m, "# | / _----=> need-resched \n");
2175 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2176 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2177 seq_puts(m, "# |||| / delay \n");
2178 seq_puts(m, "# cmd pid ||||| time | caller \n");
2179 seq_puts(m, "# \\ / ||||| \\ | / \n");
2182 static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
2184 unsigned long total;
2185 unsigned long entries;
2187 get_total_entries(buf, &total, &entries);
2188 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2189 entries, total, num_online_cpus());
2193 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
2195 print_event_info(buf, m);
2196 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2197 seq_puts(m, "# | | | | |\n");
2200 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
2202 print_event_info(buf, m);
2203 seq_puts(m, "# _-----=> irqs-off\n");
2204 seq_puts(m, "# / _----=> need-resched\n");
2205 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2206 seq_puts(m, "# || / _--=> preempt-depth\n");
2207 seq_puts(m, "# ||| / delay\n");
2208 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2209 seq_puts(m, "# | | | |||| | |\n");
2213 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2215 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2216 struct trace_buffer *buf = iter->trace_buffer;
2217 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2218 struct tracer *type = iter->trace;
2219 unsigned long entries;
2220 unsigned long total;
2221 const char *name = "preemption";
2225 get_total_entries(buf, &total, &entries);
2227 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2229 seq_puts(m, "# -----------------------------------"
2230 "---------------------------------\n");
2231 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2232 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2233 nsecs_to_usecs(data->saved_latency),
2237 #if defined(CONFIG_PREEMPT_NONE)
2239 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2241 #elif defined(CONFIG_PREEMPT)
2246 /* These are reserved for later use */
2249 seq_printf(m, " #P:%d)\n", num_online_cpus());
2253 seq_puts(m, "# -----------------\n");
2254 seq_printf(m, "# | task: %.16s-%d "
2255 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2256 data->comm, data->pid,
2257 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2258 data->policy, data->rt_priority);
2259 seq_puts(m, "# -----------------\n");
2261 if (data->critical_start) {
2262 seq_puts(m, "# => started at: ");
2263 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2264 trace_print_seq(m, &iter->seq);
2265 seq_puts(m, "\n# => ended at: ");
2266 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2267 trace_print_seq(m, &iter->seq);
2268 seq_puts(m, "\n#\n");
2274 static void test_cpu_buff_start(struct trace_iterator *iter)
2276 struct trace_seq *s = &iter->seq;
2278 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2281 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2284 if (cpumask_test_cpu(iter->cpu, iter->started))
2287 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2290 cpumask_set_cpu(iter->cpu, iter->started);
2292 /* Don't print started cpu buffer for the first entry of the trace */
2294 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2298 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2300 struct trace_seq *s = &iter->seq;
2301 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2302 struct trace_entry *entry;
2303 struct trace_event *event;
2307 test_cpu_buff_start(iter);
2309 event = ftrace_find_event(entry->type);
2311 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2312 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2313 if (!trace_print_lat_context(iter))
2316 if (!trace_print_context(iter))
2322 return event->funcs->trace(iter, sym_flags, event);
2324 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2327 return TRACE_TYPE_HANDLED;
2329 return TRACE_TYPE_PARTIAL_LINE;
2332 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2334 struct trace_seq *s = &iter->seq;
2335 struct trace_entry *entry;
2336 struct trace_event *event;
2340 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2341 if (!trace_seq_printf(s, "%d %d %llu ",
2342 entry->pid, iter->cpu, iter->ts))
2346 event = ftrace_find_event(entry->type);
2348 return event->funcs->raw(iter, 0, event);
2350 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2353 return TRACE_TYPE_HANDLED;
2355 return TRACE_TYPE_PARTIAL_LINE;
2358 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2360 struct trace_seq *s = &iter->seq;
2361 unsigned char newline = '\n';
2362 struct trace_entry *entry;
2363 struct trace_event *event;
2367 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2368 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2369 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2370 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2373 event = ftrace_find_event(entry->type);
2375 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2376 if (ret != TRACE_TYPE_HANDLED)
2380 SEQ_PUT_FIELD_RET(s, newline);
2382 return TRACE_TYPE_HANDLED;
2385 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2387 struct trace_seq *s = &iter->seq;
2388 struct trace_entry *entry;
2389 struct trace_event *event;
2393 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2394 SEQ_PUT_FIELD_RET(s, entry->pid);
2395 SEQ_PUT_FIELD_RET(s, iter->cpu);
2396 SEQ_PUT_FIELD_RET(s, iter->ts);
2399 event = ftrace_find_event(entry->type);
2400 return event ? event->funcs->binary(iter, 0, event) :
2404 int trace_empty(struct trace_iterator *iter)
2406 struct ring_buffer_iter *buf_iter;
2409 /* If we are looking at one CPU buffer, only check that one */
2410 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2411 cpu = iter->cpu_file;
2412 buf_iter = trace_buffer_iter(iter, cpu);
2414 if (!ring_buffer_iter_empty(buf_iter))
2417 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2423 for_each_tracing_cpu(cpu) {
2424 buf_iter = trace_buffer_iter(iter, cpu);
2426 if (!ring_buffer_iter_empty(buf_iter))
2429 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2437 /* Called with trace_event_read_lock() held. */
2438 enum print_line_t print_trace_line(struct trace_iterator *iter)
2440 enum print_line_t ret;
2442 if (iter->lost_events &&
2443 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2444 iter->cpu, iter->lost_events))
2445 return TRACE_TYPE_PARTIAL_LINE;
2447 if (iter->trace && iter->trace->print_line) {
2448 ret = iter->trace->print_line(iter);
2449 if (ret != TRACE_TYPE_UNHANDLED)
2453 if (iter->ent->type == TRACE_BPRINT &&
2454 trace_flags & TRACE_ITER_PRINTK &&
2455 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2456 return trace_print_bprintk_msg_only(iter);
2458 if (iter->ent->type == TRACE_PRINT &&
2459 trace_flags & TRACE_ITER_PRINTK &&
2460 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2461 return trace_print_printk_msg_only(iter);
2463 if (trace_flags & TRACE_ITER_BIN)
2464 return print_bin_fmt(iter);
2466 if (trace_flags & TRACE_ITER_HEX)
2467 return print_hex_fmt(iter);
2469 if (trace_flags & TRACE_ITER_RAW)
2470 return print_raw_fmt(iter);
2472 return print_trace_fmt(iter);
2475 void trace_latency_header(struct seq_file *m)
2477 struct trace_iterator *iter = m->private;
2479 /* print nothing if the buffers are empty */
2480 if (trace_empty(iter))
2483 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2484 print_trace_header(m, iter);
2486 if (!(trace_flags & TRACE_ITER_VERBOSE))
2487 print_lat_help_header(m);
2490 void trace_default_header(struct seq_file *m)
2492 struct trace_iterator *iter = m->private;
2494 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2497 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2498 /* print nothing if the buffers are empty */
2499 if (trace_empty(iter))
2501 print_trace_header(m, iter);
2502 if (!(trace_flags & TRACE_ITER_VERBOSE))
2503 print_lat_help_header(m);
2505 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2506 if (trace_flags & TRACE_ITER_IRQ_INFO)
2507 print_func_help_header_irq(iter->trace_buffer, m);
2509 print_func_help_header(iter->trace_buffer, m);
2514 static void test_ftrace_alive(struct seq_file *m)
2516 if (!ftrace_is_dead())
2518 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2519 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2522 #ifdef CONFIG_TRACER_MAX_TRACE
2523 static void show_snapshot_main_help(struct seq_file *m)
2525 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2526 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2527 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2528 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2529 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2530 seq_printf(m, "# is not a '0' or '1')\n");
2533 static void show_snapshot_percpu_help(struct seq_file *m)
2535 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2536 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2537 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2538 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2540 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2541 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2543 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2544 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2545 seq_printf(m, "# is not a '0' or '1')\n");
2548 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2550 if (iter->tr->allocated_snapshot)
2551 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2553 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2555 seq_printf(m, "# Snapshot commands:\n");
2556 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2557 show_snapshot_main_help(m);
2559 show_snapshot_percpu_help(m);
2562 /* Should never be called */
2563 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2566 static int s_show(struct seq_file *m, void *v)
2568 struct trace_iterator *iter = v;
2571 if (iter->ent == NULL) {
2573 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2575 test_ftrace_alive(m);
2577 if (iter->snapshot && trace_empty(iter))
2578 print_snapshot_help(m, iter);
2579 else if (iter->trace && iter->trace->print_header)
2580 iter->trace->print_header(m);
2582 trace_default_header(m);
2584 } else if (iter->leftover) {
2586 * If we filled the seq_file buffer earlier, we
2587 * want to just show it now.
2589 ret = trace_print_seq(m, &iter->seq);
2591 /* ret should this time be zero, but you never know */
2592 iter->leftover = ret;
2595 print_trace_line(iter);
2596 ret = trace_print_seq(m, &iter->seq);
2598 * If we overflow the seq_file buffer, then it will
2599 * ask us for this data again at start up.
2601 * ret is 0 if seq_file write succeeded.
2604 iter->leftover = ret;
2610 static const struct seq_operations tracer_seq_ops = {
2617 static struct trace_iterator *
2618 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2620 struct trace_cpu *tc = inode->i_private;
2621 struct trace_array *tr = tc->tr;
2622 struct trace_iterator *iter;
2625 if (tracing_disabled)
2626 return ERR_PTR(-ENODEV);
2628 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2630 return ERR_PTR(-ENOMEM);
2632 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2634 if (!iter->buffer_iter)
2638 * We make a copy of the current tracer to avoid concurrent
2639 * changes on it while we are reading.
2641 mutex_lock(&trace_types_lock);
2642 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2646 *iter->trace = *tr->current_trace;
2648 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2653 #ifdef CONFIG_TRACER_MAX_TRACE
2654 /* Currently only the top directory has a snapshot */
2655 if (tr->current_trace->print_max || snapshot)
2656 iter->trace_buffer = &tr->max_buffer;
2659 iter->trace_buffer = &tr->trace_buffer;
2660 iter->snapshot = snapshot;
2662 mutex_init(&iter->mutex);
2663 iter->cpu_file = tc->cpu;
2665 /* Notify the tracer early; before we stop tracing. */
2666 if (iter->trace && iter->trace->open)
2667 iter->trace->open(iter);
2669 /* Annotate start of buffers if we had overruns */
2670 if (ring_buffer_overruns(iter->trace_buffer->buffer))
2671 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2673 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2674 if (trace_clocks[trace_clock_id].in_ns)
2675 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2677 /* stop the trace while dumping if we are not opening "snapshot" */
2678 if (!iter->snapshot)
2679 tracing_stop_tr(tr);
2681 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
2682 for_each_tracing_cpu(cpu) {
2683 iter->buffer_iter[cpu] =
2684 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2686 ring_buffer_read_prepare_sync();
2687 for_each_tracing_cpu(cpu) {
2688 ring_buffer_read_start(iter->buffer_iter[cpu]);
2689 tracing_iter_reset(iter, cpu);
2692 cpu = iter->cpu_file;
2693 iter->buffer_iter[cpu] =
2694 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2695 ring_buffer_read_prepare_sync();
2696 ring_buffer_read_start(iter->buffer_iter[cpu]);
2697 tracing_iter_reset(iter, cpu);
2702 mutex_unlock(&trace_types_lock);
2707 mutex_unlock(&trace_types_lock);
2709 kfree(iter->buffer_iter);
2711 seq_release_private(inode, file);
2712 return ERR_PTR(-ENOMEM);
2715 int tracing_open_generic(struct inode *inode, struct file *filp)
2717 if (tracing_disabled)
2720 filp->private_data = inode->i_private;
2724 static int tracing_release(struct inode *inode, struct file *file)
2726 struct seq_file *m = file->private_data;
2727 struct trace_iterator *iter;
2728 struct trace_array *tr;
2731 if (!(file->f_mode & FMODE_READ))
2737 mutex_lock(&trace_types_lock);
2742 for_each_tracing_cpu(cpu) {
2743 if (iter->buffer_iter[cpu])
2744 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2747 if (iter->trace && iter->trace->close)
2748 iter->trace->close(iter);
2750 if (!iter->snapshot)
2751 /* reenable tracing if it was previously enabled */
2752 tracing_start_tr(tr);
2753 mutex_unlock(&trace_types_lock);
2755 mutex_destroy(&iter->mutex);
2756 free_cpumask_var(iter->started);
2758 kfree(iter->buffer_iter);
2759 seq_release_private(inode, file);
2763 static int tracing_open(struct inode *inode, struct file *file)
2765 struct trace_iterator *iter;
2768 /* If this file was open for write, then erase contents */
2769 if ((file->f_mode & FMODE_WRITE) &&
2770 (file->f_flags & O_TRUNC)) {
2771 struct trace_cpu *tc = inode->i_private;
2772 struct trace_array *tr = tc->tr;
2774 if (tc->cpu == RING_BUFFER_ALL_CPUS)
2775 tracing_reset_online_cpus(&tr->trace_buffer);
2777 tracing_reset(&tr->trace_buffer, tc->cpu);
2780 if (file->f_mode & FMODE_READ) {
2781 iter = __tracing_open(inode, file, false);
2783 ret = PTR_ERR(iter);
2784 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2785 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2791 t_next(struct seq_file *m, void *v, loff_t *pos)
2793 struct tracer *t = v;
2803 static void *t_start(struct seq_file *m, loff_t *pos)
2808 mutex_lock(&trace_types_lock);
2809 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2815 static void t_stop(struct seq_file *m, void *p)
2817 mutex_unlock(&trace_types_lock);
2820 static int t_show(struct seq_file *m, void *v)
2822 struct tracer *t = v;
2827 seq_printf(m, "%s", t->name);
2836 static const struct seq_operations show_traces_seq_ops = {
2843 static int show_traces_open(struct inode *inode, struct file *file)
2845 if (tracing_disabled)
2848 return seq_open(file, &show_traces_seq_ops);
2852 tracing_write_stub(struct file *filp, const char __user *ubuf,
2853 size_t count, loff_t *ppos)
2858 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2860 if (file->f_mode & FMODE_READ)
2861 return seq_lseek(file, offset, origin);
2866 static const struct file_operations tracing_fops = {
2867 .open = tracing_open,
2869 .write = tracing_write_stub,
2870 .llseek = tracing_seek,
2871 .release = tracing_release,
2874 static const struct file_operations show_traces_fops = {
2875 .open = show_traces_open,
2877 .release = seq_release,
2878 .llseek = seq_lseek,
2882 * Only trace on a CPU if the bitmask is set:
2884 static cpumask_var_t tracing_cpumask;
2887 * The tracer itself will not take this lock, but still we want
2888 * to provide a consistent cpumask to user-space:
2890 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2893 * Temporary storage for the character representation of the
2894 * CPU bitmask (and one more byte for the newline):
2896 static char mask_str[NR_CPUS + 1];
2899 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2900 size_t count, loff_t *ppos)
2904 mutex_lock(&tracing_cpumask_update_lock);
2906 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2907 if (count - len < 2) {
2911 len += sprintf(mask_str + len, "\n");
2912 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2915 mutex_unlock(&tracing_cpumask_update_lock);
2921 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2922 size_t count, loff_t *ppos)
2924 struct trace_array *tr = filp->private_data;
2925 cpumask_var_t tracing_cpumask_new;
2928 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2931 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2935 mutex_lock(&tracing_cpumask_update_lock);
2937 local_irq_disable();
2938 arch_spin_lock(&ftrace_max_lock);
2939 for_each_tracing_cpu(cpu) {
2941 * Increase/decrease the disabled counter if we are
2942 * about to flip a bit in the cpumask:
2944 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2945 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2946 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
2947 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
2949 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2950 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2951 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
2952 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
2955 arch_spin_unlock(&ftrace_max_lock);
2958 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2960 mutex_unlock(&tracing_cpumask_update_lock);
2961 free_cpumask_var(tracing_cpumask_new);
2966 free_cpumask_var(tracing_cpumask_new);
2971 static const struct file_operations tracing_cpumask_fops = {
2972 .open = tracing_open_generic,
2973 .read = tracing_cpumask_read,
2974 .write = tracing_cpumask_write,
2975 .llseek = generic_file_llseek,
2978 static int tracing_trace_options_show(struct seq_file *m, void *v)
2980 struct tracer_opt *trace_opts;
2981 struct trace_array *tr = m->private;
2985 mutex_lock(&trace_types_lock);
2986 tracer_flags = tr->current_trace->flags->val;
2987 trace_opts = tr->current_trace->flags->opts;
2989 for (i = 0; trace_options[i]; i++) {
2990 if (trace_flags & (1 << i))
2991 seq_printf(m, "%s\n", trace_options[i]);
2993 seq_printf(m, "no%s\n", trace_options[i]);
2996 for (i = 0; trace_opts[i].name; i++) {
2997 if (tracer_flags & trace_opts[i].bit)
2998 seq_printf(m, "%s\n", trace_opts[i].name);
3000 seq_printf(m, "no%s\n", trace_opts[i].name);
3002 mutex_unlock(&trace_types_lock);
3007 static int __set_tracer_option(struct tracer *trace,
3008 struct tracer_flags *tracer_flags,
3009 struct tracer_opt *opts, int neg)
3013 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
3018 tracer_flags->val &= ~opts->bit;
3020 tracer_flags->val |= opts->bit;
3024 /* Try to assign a tracer specific option */
3025 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3027 struct tracer_flags *tracer_flags = trace->flags;
3028 struct tracer_opt *opts = NULL;
3031 for (i = 0; tracer_flags->opts[i].name; i++) {
3032 opts = &tracer_flags->opts[i];
3034 if (strcmp(cmp, opts->name) == 0)
3035 return __set_tracer_option(trace, trace->flags,
3042 /* Some tracers require overwrite to stay enabled */
3043 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3045 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3051 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
3053 /* do nothing if flag is already set */
3054 if (!!(trace_flags & mask) == !!enabled)
3057 /* Give the tracer a chance to approve the change */
3058 if (tr->current_trace->flag_changed)
3059 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
3063 trace_flags |= mask;
3065 trace_flags &= ~mask;
3067 if (mask == TRACE_ITER_RECORD_CMD)
3068 trace_event_enable_cmd_record(enabled);
3070 if (mask == TRACE_ITER_OVERWRITE) {
3071 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
3072 #ifdef CONFIG_TRACER_MAX_TRACE
3073 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
3077 if (mask == TRACE_ITER_PRINTK)
3078 trace_printk_start_stop_comm(enabled);
3083 static int trace_set_options(struct trace_array *tr, char *option)
3090 cmp = strstrip(option);
3092 if (strncmp(cmp, "no", 2) == 0) {
3097 mutex_lock(&trace_types_lock);
3099 for (i = 0; trace_options[i]; i++) {
3100 if (strcmp(cmp, trace_options[i]) == 0) {
3101 ret = set_tracer_flag(tr, 1 << i, !neg);
3106 /* If no option could be set, test the specific tracer options */
3107 if (!trace_options[i])
3108 ret = set_tracer_option(tr->current_trace, cmp, neg);
3110 mutex_unlock(&trace_types_lock);
3116 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3117 size_t cnt, loff_t *ppos)
3119 struct seq_file *m = filp->private_data;
3120 struct trace_array *tr = m->private;
3124 if (cnt >= sizeof(buf))
3127 if (copy_from_user(&buf, ubuf, cnt))
3132 ret = trace_set_options(tr, buf);
3141 static int tracing_trace_options_open(struct inode *inode, struct file *file)
3143 if (tracing_disabled)
3146 return single_open(file, tracing_trace_options_show, inode->i_private);
3149 static const struct file_operations tracing_iter_fops = {
3150 .open = tracing_trace_options_open,
3152 .llseek = seq_lseek,
3153 .release = single_release,
3154 .write = tracing_trace_options_write,
3157 static const char readme_msg[] =
3158 "tracing mini-HOWTO:\n\n"
3159 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
3160 "# cat /sys/kernel/debug/tracing/available_tracers\n"
3161 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
3162 "# cat /sys/kernel/debug/tracing/current_tracer\n"
3164 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
3165 "# cat /sys/kernel/debug/tracing/current_tracer\n"
3167 "# cat /sys/kernel/debug/tracing/trace_options\n"
3168 "noprint-parent nosym-offset nosym-addr noverbose\n"
3169 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
3170 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
3171 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
3172 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
3176 tracing_readme_read(struct file *filp, char __user *ubuf,
3177 size_t cnt, loff_t *ppos)
3179 return simple_read_from_buffer(ubuf, cnt, ppos,
3180 readme_msg, strlen(readme_msg));
3183 static const struct file_operations tracing_readme_fops = {
3184 .open = tracing_open_generic,
3185 .read = tracing_readme_read,
3186 .llseek = generic_file_llseek,
3190 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3191 size_t cnt, loff_t *ppos)
3200 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3204 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3212 for (i = 0; i < SAVED_CMDLINES; i++) {
3215 pid = map_cmdline_to_pid[i];
3216 if (pid == -1 || pid == NO_CMDLINE_MAP)
3219 trace_find_cmdline(pid, buf_comm);
3220 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3225 len = simple_read_from_buffer(ubuf, cnt, ppos,
3234 static const struct file_operations tracing_saved_cmdlines_fops = {
3235 .open = tracing_open_generic,
3236 .read = tracing_saved_cmdlines_read,
3237 .llseek = generic_file_llseek,
3241 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3242 size_t cnt, loff_t *ppos)
3244 struct trace_array *tr = filp->private_data;
3245 char buf[MAX_TRACER_SIZE+2];
3248 mutex_lock(&trace_types_lock);
3249 r = sprintf(buf, "%s\n", tr->current_trace->name);
3250 mutex_unlock(&trace_types_lock);
3252 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3255 int tracer_init(struct tracer *t, struct trace_array *tr)
3257 tracing_reset_online_cpus(&tr->trace_buffer);
3261 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
3265 for_each_tracing_cpu(cpu)
3266 per_cpu_ptr(buf->data, cpu)->entries = val;
3269 #ifdef CONFIG_TRACER_MAX_TRACE
3270 /* resize @tr's buffer to the size of @size_tr's entries */
3271 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3272 struct trace_buffer *size_buf, int cpu_id)
3276 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3277 for_each_tracing_cpu(cpu) {
3278 ret = ring_buffer_resize(trace_buf->buffer,
3279 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
3282 per_cpu_ptr(trace_buf->data, cpu)->entries =
3283 per_cpu_ptr(size_buf->data, cpu)->entries;
3286 ret = ring_buffer_resize(trace_buf->buffer,
3287 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
3289 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3290 per_cpu_ptr(size_buf->data, cpu_id)->entries;
3295 #endif /* CONFIG_TRACER_MAX_TRACE */
3297 static int __tracing_resize_ring_buffer(struct trace_array *tr,
3298 unsigned long size, int cpu)
3303 * If kernel or user changes the size of the ring buffer
3304 * we use the size that was given, and we can forget about
3305 * expanding it later.
3307 ring_buffer_expanded = 1;
3309 /* May be called before buffers are initialized */
3310 if (!tr->trace_buffer.buffer)
3313 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
3317 #ifdef CONFIG_TRACER_MAX_TRACE
3318 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3319 !tr->current_trace->use_max_tr)
3322 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
3324 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3325 &tr->trace_buffer, cpu);
3328 * AARGH! We are left with different
3329 * size max buffer!!!!
3330 * The max buffer is our "snapshot" buffer.
3331 * When a tracer needs a snapshot (one of the
3332 * latency tracers), it swaps the max buffer
3333 * with the saved snap shot. We succeeded to
3334 * update the size of the main buffer, but failed to
3335 * update the size of the max buffer. But when we tried
3336 * to reset the main buffer to the original size, we
3337 * failed there too. This is very unlikely to
3338 * happen, but if it does, warn and kill all
3342 tracing_disabled = 1;
3347 if (cpu == RING_BUFFER_ALL_CPUS)
3348 set_buffer_entries(&tr->max_buffer, size);
3350 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
3353 #endif /* CONFIG_TRACER_MAX_TRACE */
3355 if (cpu == RING_BUFFER_ALL_CPUS)
3356 set_buffer_entries(&tr->trace_buffer, size);
3358 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
3363 static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3364 unsigned long size, int cpu_id)
3368 mutex_lock(&trace_types_lock);
3370 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3371 /* make sure, this cpu is enabled in the mask */
3372 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3378 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
3383 mutex_unlock(&trace_types_lock);
3390 * tracing_update_buffers - used by tracing facility to expand ring buffers
3392 * To save on memory when the tracing is never used on a system with it
3393 * configured in. The ring buffers are set to a minimum size. But once
3394 * a user starts to use the tracing facility, then they need to grow
3395 * to their default size.
3397 * This function is to be called when a tracer is about to be used.
3399 int tracing_update_buffers(void)
3403 mutex_lock(&trace_types_lock);
3404 if (!ring_buffer_expanded)
3405 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
3406 RING_BUFFER_ALL_CPUS);
3407 mutex_unlock(&trace_types_lock);
3412 struct trace_option_dentry;
3414 static struct trace_option_dentry *
3415 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
3418 destroy_trace_option_files(struct trace_option_dentry *topts);
3420 static int tracing_set_tracer(const char *buf)
3422 static struct trace_option_dentry *topts;
3423 struct trace_array *tr = &global_trace;
3425 #ifdef CONFIG_TRACER_MAX_TRACE
3430 mutex_lock(&trace_types_lock);
3432 if (!ring_buffer_expanded) {
3433 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
3434 RING_BUFFER_ALL_CPUS);
3440 for (t = trace_types; t; t = t->next) {
3441 if (strcmp(t->name, buf) == 0)
3448 if (t == tr->current_trace)
3451 trace_branch_disable();
3453 tr->current_trace->enabled = false;
3455 if (tr->current_trace->reset)
3456 tr->current_trace->reset(tr);
3458 /* Current trace needs to be nop_trace before synchronize_sched */
3459 tr->current_trace = &nop_trace;
3461 #ifdef CONFIG_TRACER_MAX_TRACE
3462 had_max_tr = tr->allocated_snapshot;
3464 if (had_max_tr && !t->use_max_tr) {
3466 * We need to make sure that the update_max_tr sees that
3467 * current_trace changed to nop_trace to keep it from
3468 * swapping the buffers after we resize it.
3469 * The update_max_tr is called from interrupts disabled
3470 * so a synchronized_sched() is sufficient.
3472 synchronize_sched();
3474 * We don't free the ring buffer. instead, resize it because
3475 * The max_tr ring buffer has some state (e.g. ring->clock) and
3476 * we want preserve it.
3478 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
3479 set_buffer_entries(&tr->max_buffer, 1);
3480 tracing_reset_online_cpus(&tr->max_buffer);
3481 tr->allocated_snapshot = false;
3484 destroy_trace_option_files(topts);
3486 topts = create_trace_option_files(tr, t);
3488 #ifdef CONFIG_TRACER_MAX_TRACE
3489 if (t->use_max_tr && !had_max_tr) {
3490 /* we need to make per cpu buffer sizes equivalent */
3491 ret = resize_buffer_duplicate_size(&tr->max_buffer, &tr->trace_buffer,
3492 RING_BUFFER_ALL_CPUS);
3495 tr->allocated_snapshot = true;
3500 ret = tracer_init(t, tr);
3505 tr->current_trace = t;
3506 tr->current_trace->enabled = true;
3507 trace_branch_enable(tr);
3509 mutex_unlock(&trace_types_lock);
3515 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3516 size_t cnt, loff_t *ppos)
3518 char buf[MAX_TRACER_SIZE+1];
3525 if (cnt > MAX_TRACER_SIZE)
3526 cnt = MAX_TRACER_SIZE;
3528 if (copy_from_user(&buf, ubuf, cnt))
3533 /* strip ending whitespace. */
3534 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3537 err = tracing_set_tracer(buf);
3547 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3548 size_t cnt, loff_t *ppos)
3550 unsigned long *ptr = filp->private_data;
3554 r = snprintf(buf, sizeof(buf), "%ld\n",
3555 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3556 if (r > sizeof(buf))
3558 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3562 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3563 size_t cnt, loff_t *ppos)
3565 unsigned long *ptr = filp->private_data;
3569 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3578 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3580 struct trace_cpu *tc = inode->i_private;
3581 struct trace_array *tr = tc->tr;
3582 struct trace_iterator *iter;
3585 if (tracing_disabled)
3588 mutex_lock(&trace_types_lock);
3590 /* create a buffer to store the information to pass to userspace */
3591 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3598 * We make a copy of the current tracer to avoid concurrent
3599 * changes on it while we are reading.
3601 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3606 *iter->trace = *tr->current_trace;
3608 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3613 /* trace pipe does not show start of buffer */
3614 cpumask_setall(iter->started);
3616 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3617 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3619 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3620 if (trace_clocks[trace_clock_id].in_ns)
3621 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3623 iter->cpu_file = tc->cpu;
3625 iter->trace_buffer = &tc->tr->trace_buffer;
3626 mutex_init(&iter->mutex);
3627 filp->private_data = iter;
3629 if (iter->trace->pipe_open)
3630 iter->trace->pipe_open(iter);
3632 nonseekable_open(inode, filp);
3634 mutex_unlock(&trace_types_lock);
3640 mutex_unlock(&trace_types_lock);
3644 static int tracing_release_pipe(struct inode *inode, struct file *file)
3646 struct trace_iterator *iter = file->private_data;
3648 mutex_lock(&trace_types_lock);
3650 if (iter->trace->pipe_close)
3651 iter->trace->pipe_close(iter);
3653 mutex_unlock(&trace_types_lock);
3655 free_cpumask_var(iter->started);
3656 mutex_destroy(&iter->mutex);
3664 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
3666 /* Iterators are static, they should be filled or empty */
3667 if (trace_buffer_iter(iter, iter->cpu_file))
3668 return POLLIN | POLLRDNORM;
3670 if (trace_flags & TRACE_ITER_BLOCK)
3672 * Always select as readable when in blocking mode
3674 return POLLIN | POLLRDNORM;
3676 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
3681 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3683 struct trace_iterator *iter = filp->private_data;
3685 return trace_poll(iter, filp, poll_table);
3689 * This is a make-shift waitqueue.
3690 * A tracer might use this callback on some rare cases:
3692 * 1) the current tracer might hold the runqueue lock when it wakes up
3693 * a reader, hence a deadlock (sched, function, and function graph tracers)
3694 * 2) the function tracers, trace all functions, we don't want
3695 * the overhead of calling wake_up and friends
3696 * (and tracing them too)
3698 * Anyway, this is really very primitive wakeup.
3700 void poll_wait_pipe(struct trace_iterator *iter)
3702 set_current_state(TASK_INTERRUPTIBLE);
3703 /* sleep for 100 msecs, and try again. */
3704 schedule_timeout(HZ / 10);
3707 /* Must be called with trace_types_lock mutex held. */
3708 static int tracing_wait_pipe(struct file *filp)
3710 struct trace_iterator *iter = filp->private_data;
3712 while (trace_empty(iter)) {
3714 if ((filp->f_flags & O_NONBLOCK)) {
3718 mutex_unlock(&iter->mutex);
3720 iter->trace->wait_pipe(iter);
3722 mutex_lock(&iter->mutex);
3724 if (signal_pending(current))
3728 * We block until we read something and tracing is disabled.
3729 * We still block if tracing is disabled, but we have never
3730 * read anything. This allows a user to cat this file, and
3731 * then enable tracing. But after we have read something,
3732 * we give an EOF when tracing is again disabled.
3734 * iter->pos will be 0 if we haven't read anything.
3736 if (!tracing_is_enabled() && iter->pos)
3747 tracing_read_pipe(struct file *filp, char __user *ubuf,
3748 size_t cnt, loff_t *ppos)
3750 struct trace_iterator *iter = filp->private_data;
3751 struct trace_array *tr = iter->tr;
3754 /* return any leftover data */
3755 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3759 trace_seq_init(&iter->seq);
3761 /* copy the tracer to avoid using a global lock all around */
3762 mutex_lock(&trace_types_lock);
3763 if (unlikely(iter->trace->name != tr->current_trace->name))
3764 *iter->trace = *tr->current_trace;
3765 mutex_unlock(&trace_types_lock);
3768 * Avoid more than one consumer on a single file descriptor
3769 * This is just a matter of traces coherency, the ring buffer itself
3772 mutex_lock(&iter->mutex);
3773 if (iter->trace->read) {
3774 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3780 sret = tracing_wait_pipe(filp);
3784 /* stop when tracing is finished */
3785 if (trace_empty(iter)) {
3790 if (cnt >= PAGE_SIZE)
3791 cnt = PAGE_SIZE - 1;
3793 /* reset all but tr, trace, and overruns */
3794 memset(&iter->seq, 0,
3795 sizeof(struct trace_iterator) -
3796 offsetof(struct trace_iterator, seq));
3799 trace_event_read_lock();
3800 trace_access_lock(iter->cpu_file);
3801 while (trace_find_next_entry_inc(iter) != NULL) {
3802 enum print_line_t ret;
3803 int len = iter->seq.len;
3805 ret = print_trace_line(iter);
3806 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3807 /* don't print partial lines */
3808 iter->seq.len = len;
3811 if (ret != TRACE_TYPE_NO_CONSUME)
3812 trace_consume(iter);
3814 if (iter->seq.len >= cnt)
3818 * Setting the full flag means we reached the trace_seq buffer
3819 * size and we should leave by partial output condition above.
3820 * One of the trace_seq_* functions is not used properly.
3822 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3825 trace_access_unlock(iter->cpu_file);
3826 trace_event_read_unlock();
3828 /* Now copy what we have to the user */
3829 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3830 if (iter->seq.readpos >= iter->seq.len)
3831 trace_seq_init(&iter->seq);
3834 * If there was nothing to send to user, in spite of consuming trace
3835 * entries, go back to wait for more entries.
3841 mutex_unlock(&iter->mutex);
3846 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3847 struct pipe_buffer *buf)
3849 __free_page(buf->page);
3852 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3855 __free_page(spd->pages[idx]);
3858 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3860 .map = generic_pipe_buf_map,
3861 .unmap = generic_pipe_buf_unmap,
3862 .confirm = generic_pipe_buf_confirm,
3863 .release = tracing_pipe_buf_release,
3864 .steal = generic_pipe_buf_steal,
3865 .get = generic_pipe_buf_get,
3869 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3874 /* Seq buffer is page-sized, exactly what we need. */
3876 count = iter->seq.len;
3877 ret = print_trace_line(iter);
3878 count = iter->seq.len - count;
3881 iter->seq.len -= count;
3884 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3885 iter->seq.len -= count;
3889 if (ret != TRACE_TYPE_NO_CONSUME)
3890 trace_consume(iter);
3892 if (!trace_find_next_entry_inc(iter)) {
3902 static ssize_t tracing_splice_read_pipe(struct file *filp,
3904 struct pipe_inode_info *pipe,
3908 struct page *pages_def[PIPE_DEF_BUFFERS];
3909 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3910 struct trace_iterator *iter = filp->private_data;
3911 struct splice_pipe_desc spd = {
3913 .partial = partial_def,
3914 .nr_pages = 0, /* This gets updated below. */
3915 .nr_pages_max = PIPE_DEF_BUFFERS,
3917 .ops = &tracing_pipe_buf_ops,
3918 .spd_release = tracing_spd_release_pipe,
3920 struct trace_array *tr = iter->tr;
3925 if (splice_grow_spd(pipe, &spd))
3928 /* copy the tracer to avoid using a global lock all around */
3929 mutex_lock(&trace_types_lock);
3930 if (unlikely(iter->trace->name != tr->current_trace->name))
3931 *iter->trace = *tr->current_trace;
3932 mutex_unlock(&trace_types_lock);
3934 mutex_lock(&iter->mutex);
3936 if (iter->trace->splice_read) {
3937 ret = iter->trace->splice_read(iter, filp,
3938 ppos, pipe, len, flags);
3943 ret = tracing_wait_pipe(filp);
3947 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3952 trace_event_read_lock();
3953 trace_access_lock(iter->cpu_file);
3955 /* Fill as many pages as possible. */
3956 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3957 spd.pages[i] = alloc_page(GFP_KERNEL);
3961 rem = tracing_fill_pipe_page(rem, iter);
3963 /* Copy the data into the page, so we can start over. */
3964 ret = trace_seq_to_buffer(&iter->seq,
3965 page_address(spd.pages[i]),
3968 __free_page(spd.pages[i]);
3971 spd.partial[i].offset = 0;
3972 spd.partial[i].len = iter->seq.len;
3974 trace_seq_init(&iter->seq);
3977 trace_access_unlock(iter->cpu_file);
3978 trace_event_read_unlock();
3979 mutex_unlock(&iter->mutex);
3983 ret = splice_to_pipe(pipe, &spd);
3985 splice_shrink_spd(&spd);
3989 mutex_unlock(&iter->mutex);
3994 tracing_entries_read(struct file *filp, char __user *ubuf,
3995 size_t cnt, loff_t *ppos)
3997 struct trace_cpu *tc = filp->private_data;
3998 struct trace_array *tr = tc->tr;
4003 mutex_lock(&trace_types_lock);
4005 if (tc->cpu == RING_BUFFER_ALL_CPUS) {
4006 int cpu, buf_size_same;
4011 /* check if all cpu sizes are same */
4012 for_each_tracing_cpu(cpu) {
4013 /* fill in the size from first enabled cpu */
4015 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4016 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
4022 if (buf_size_same) {
4023 if (!ring_buffer_expanded)
4024 r = sprintf(buf, "%lu (expanded: %lu)\n",
4026 trace_buf_size >> 10);
4028 r = sprintf(buf, "%lu\n", size >> 10);
4030 r = sprintf(buf, "X\n");
4032 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, tc->cpu)->entries >> 10);
4034 mutex_unlock(&trace_types_lock);
4036 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4041 tracing_entries_write(struct file *filp, const char __user *ubuf,
4042 size_t cnt, loff_t *ppos)
4044 struct trace_cpu *tc = filp->private_data;
4048 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4052 /* must have at least 1 entry */
4056 /* value is in KB */
4059 ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
4069 tracing_total_entries_read(struct file *filp, char __user *ubuf,
4070 size_t cnt, loff_t *ppos)
4072 struct trace_array *tr = filp->private_data;
4075 unsigned long size = 0, expanded_size = 0;
4077 mutex_lock(&trace_types_lock);
4078 for_each_tracing_cpu(cpu) {
4079 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
4080 if (!ring_buffer_expanded)
4081 expanded_size += trace_buf_size >> 10;
4083 if (ring_buffer_expanded)
4084 r = sprintf(buf, "%lu\n", size);
4086 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4087 mutex_unlock(&trace_types_lock);
4089 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4093 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4094 size_t cnt, loff_t *ppos)
4097 * There is no need to read what the user has written, this function
4098 * is just to make sure that there is no error when "echo" is used
4107 tracing_free_buffer_release(struct inode *inode, struct file *filp)
4109 struct trace_array *tr = inode->i_private;
4111 /* disable tracing ? */
4112 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4114 /* resize the ring buffer to 0 */
4115 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4121 tracing_mark_write(struct file *filp, const char __user *ubuf,
4122 size_t cnt, loff_t *fpos)
4124 unsigned long addr = (unsigned long)ubuf;
4125 struct ring_buffer_event *event;
4126 struct ring_buffer *buffer;
4127 struct print_entry *entry;
4128 unsigned long irq_flags;
4129 struct page *pages[2];
4139 if (tracing_disabled)
4142 if (!(trace_flags & TRACE_ITER_MARKERS))
4145 if (cnt > TRACE_BUF_SIZE)
4146 cnt = TRACE_BUF_SIZE;
4149 * Userspace is injecting traces into the kernel trace buffer.
4150 * We want to be as non intrusive as possible.
4151 * To do so, we do not want to allocate any special buffers
4152 * or take any locks, but instead write the userspace data
4153 * straight into the ring buffer.
4155 * First we need to pin the userspace buffer into memory,
4156 * which, most likely it is, because it just referenced it.
4157 * But there's no guarantee that it is. By using get_user_pages_fast()
4158 * and kmap_atomic/kunmap_atomic() we can get access to the
4159 * pages directly. We then write the data directly into the
4162 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4164 /* check if we cross pages */
4165 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4168 offset = addr & (PAGE_SIZE - 1);
4171 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4172 if (ret < nr_pages) {
4174 put_page(pages[ret]);
4179 for (i = 0; i < nr_pages; i++)
4180 map_page[i] = kmap_atomic(pages[i]);
4182 local_save_flags(irq_flags);
4183 size = sizeof(*entry) + cnt + 2; /* possible \n added */
4184 buffer = global_trace.trace_buffer.buffer;
4185 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4186 irq_flags, preempt_count());
4188 /* Ring buffer disabled, return as if not open for write */
4193 entry = ring_buffer_event_data(event);
4194 entry->ip = _THIS_IP_;
4196 if (nr_pages == 2) {
4197 len = PAGE_SIZE - offset;
4198 memcpy(&entry->buf, map_page[0] + offset, len);
4199 memcpy(&entry->buf[len], map_page[1], cnt - len);
4201 memcpy(&entry->buf, map_page[0] + offset, cnt);
4203 if (entry->buf[cnt - 1] != '\n') {
4204 entry->buf[cnt] = '\n';
4205 entry->buf[cnt + 1] = '\0';
4207 entry->buf[cnt] = '\0';
4209 __buffer_unlock_commit(buffer, event);
4216 for (i = 0; i < nr_pages; i++){
4217 kunmap_atomic(map_page[i]);
4224 static int tracing_clock_show(struct seq_file *m, void *v)
4226 struct trace_array *tr = m->private;
4229 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4231 "%s%s%s%s", i ? " " : "",
4232 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4233 i == tr->clock_id ? "]" : "");
4239 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4240 size_t cnt, loff_t *fpos)
4242 struct seq_file *m = filp->private_data;
4243 struct trace_array *tr = m->private;
4245 const char *clockstr;
4248 if (cnt >= sizeof(buf))
4251 if (copy_from_user(&buf, ubuf, cnt))
4256 clockstr = strstrip(buf);
4258 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4259 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4262 if (i == ARRAY_SIZE(trace_clocks))
4265 mutex_lock(&trace_types_lock);
4269 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
4272 * New clock may not be consistent with the previous clock.
4273 * Reset the buffer so that it doesn't have incomparable timestamps.
4275 tracing_reset_online_cpus(&global_trace.trace_buffer);
4277 #ifdef CONFIG_TRACER_MAX_TRACE
4278 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4279 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4280 tracing_reset_online_cpus(&global_trace.max_buffer);
4283 mutex_unlock(&trace_types_lock);
4290 static int tracing_clock_open(struct inode *inode, struct file *file)
4292 if (tracing_disabled)
4295 return single_open(file, tracing_clock_show, inode->i_private);
4298 struct ftrace_buffer_info {
4299 struct trace_iterator iter;
4304 #ifdef CONFIG_TRACER_SNAPSHOT
4305 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4307 struct trace_cpu *tc = inode->i_private;
4308 struct trace_iterator *iter;
4312 if (file->f_mode & FMODE_READ) {
4313 iter = __tracing_open(inode, file, true);
4315 ret = PTR_ERR(iter);
4317 /* Writes still need the seq_file to hold the private data */
4318 m = kzalloc(sizeof(*m), GFP_KERNEL);
4321 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4327 iter->trace_buffer = &tc->tr->max_buffer;
4328 iter->cpu_file = tc->cpu;
4330 file->private_data = m;
4337 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4340 struct seq_file *m = filp->private_data;
4341 struct trace_iterator *iter = m->private;
4342 struct trace_array *tr = iter->tr;
4346 ret = tracing_update_buffers();
4350 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4354 mutex_lock(&trace_types_lock);
4356 if (tr->current_trace->use_max_tr) {
4363 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4367 if (tr->allocated_snapshot) {
4368 /* free spare buffer */
4369 ring_buffer_resize(tr->max_buffer.buffer, 1,
4370 RING_BUFFER_ALL_CPUS);
4371 set_buffer_entries(&tr->max_buffer, 1);
4372 tracing_reset_online_cpus(&tr->max_buffer);
4373 tr->allocated_snapshot = false;
4377 /* Only allow per-cpu swap if the ring buffer supports it */
4378 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4379 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4384 if (!tr->allocated_snapshot) {
4385 /* allocate spare buffer */
4386 ret = resize_buffer_duplicate_size(&tr->max_buffer,
4387 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
4390 tr->allocated_snapshot = true;
4392 local_irq_disable();
4393 /* Now, we're going to swap */
4394 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4395 update_max_tr(tr, current, smp_processor_id());
4397 update_max_tr_single(tr, current, iter->cpu_file);
4401 if (tr->allocated_snapshot) {
4402 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4403 tracing_reset_online_cpus(&tr->max_buffer);
4405 tracing_reset(&tr->max_buffer, iter->cpu_file);
4415 mutex_unlock(&trace_types_lock);
4419 static int tracing_snapshot_release(struct inode *inode, struct file *file)
4421 struct seq_file *m = file->private_data;
4423 if (file->f_mode & FMODE_READ)
4424 return tracing_release(inode, file);
4426 /* If write only, the seq_file is just a stub */
4434 static int tracing_buffers_open(struct inode *inode, struct file *filp);
4435 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
4436 size_t count, loff_t *ppos);
4437 static int tracing_buffers_release(struct inode *inode, struct file *file);
4438 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4439 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
4441 static int snapshot_raw_open(struct inode *inode, struct file *filp)
4443 struct ftrace_buffer_info *info;
4446 ret = tracing_buffers_open(inode, filp);
4450 info = filp->private_data;
4452 if (info->iter.trace->use_max_tr) {
4453 tracing_buffers_release(inode, filp);
4457 info->iter.snapshot = true;
4458 info->iter.trace_buffer = &info->iter.tr->max_buffer;
4463 #endif /* CONFIG_TRACER_SNAPSHOT */
4466 static const struct file_operations tracing_max_lat_fops = {
4467 .open = tracing_open_generic,
4468 .read = tracing_max_lat_read,
4469 .write = tracing_max_lat_write,
4470 .llseek = generic_file_llseek,
4473 static const struct file_operations set_tracer_fops = {
4474 .open = tracing_open_generic,
4475 .read = tracing_set_trace_read,
4476 .write = tracing_set_trace_write,
4477 .llseek = generic_file_llseek,
4480 static const struct file_operations tracing_pipe_fops = {
4481 .open = tracing_open_pipe,
4482 .poll = tracing_poll_pipe,
4483 .read = tracing_read_pipe,
4484 .splice_read = tracing_splice_read_pipe,
4485 .release = tracing_release_pipe,
4486 .llseek = no_llseek,
4489 static const struct file_operations tracing_entries_fops = {
4490 .open = tracing_open_generic,
4491 .read = tracing_entries_read,
4492 .write = tracing_entries_write,
4493 .llseek = generic_file_llseek,
4496 static const struct file_operations tracing_total_entries_fops = {
4497 .open = tracing_open_generic,
4498 .read = tracing_total_entries_read,
4499 .llseek = generic_file_llseek,
4502 static const struct file_operations tracing_free_buffer_fops = {
4503 .write = tracing_free_buffer_write,
4504 .release = tracing_free_buffer_release,
4507 static const struct file_operations tracing_mark_fops = {
4508 .open = tracing_open_generic,
4509 .write = tracing_mark_write,
4510 .llseek = generic_file_llseek,
4513 static const struct file_operations trace_clock_fops = {
4514 .open = tracing_clock_open,
4516 .llseek = seq_lseek,
4517 .release = single_release,
4518 .write = tracing_clock_write,
4521 #ifdef CONFIG_TRACER_SNAPSHOT
4522 static const struct file_operations snapshot_fops = {
4523 .open = tracing_snapshot_open,
4525 .write = tracing_snapshot_write,
4526 .llseek = tracing_seek,
4527 .release = tracing_snapshot_release,
4530 static const struct file_operations snapshot_raw_fops = {
4531 .open = snapshot_raw_open,
4532 .read = tracing_buffers_read,
4533 .release = tracing_buffers_release,
4534 .splice_read = tracing_buffers_splice_read,
4535 .llseek = no_llseek,
4538 #endif /* CONFIG_TRACER_SNAPSHOT */
4540 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4542 struct trace_cpu *tc = inode->i_private;
4543 struct trace_array *tr = tc->tr;
4544 struct ftrace_buffer_info *info;
4546 if (tracing_disabled)
4549 info = kzalloc(sizeof(*info), GFP_KERNEL);
4553 mutex_lock(&trace_types_lock);
4558 info->iter.cpu_file = tc->cpu;
4559 info->iter.trace = tr->current_trace;
4560 info->iter.trace_buffer = &tr->trace_buffer;
4562 /* Force reading ring buffer for first read */
4563 info->read = (unsigned int)-1;
4565 filp->private_data = info;
4567 mutex_unlock(&trace_types_lock);
4569 return nonseekable_open(inode, filp);
4573 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
4575 struct ftrace_buffer_info *info = filp->private_data;
4576 struct trace_iterator *iter = &info->iter;
4578 return trace_poll(iter, filp, poll_table);
4582 tracing_buffers_read(struct file *filp, char __user *ubuf,
4583 size_t count, loff_t *ppos)
4585 struct ftrace_buffer_info *info = filp->private_data;
4586 struct trace_iterator *iter = &info->iter;
4593 mutex_lock(&trace_types_lock);
4595 #ifdef CONFIG_TRACER_MAX_TRACE
4596 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4603 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
4609 /* Do we have previous read data to read? */
4610 if (info->read < PAGE_SIZE)
4614 trace_access_lock(iter->cpu_file);
4615 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
4619 trace_access_unlock(iter->cpu_file);
4622 if (trace_empty(iter)) {
4623 if ((filp->f_flags & O_NONBLOCK)) {
4627 mutex_unlock(&trace_types_lock);
4628 iter->trace->wait_pipe(iter);
4629 mutex_lock(&trace_types_lock);
4630 if (signal_pending(current)) {
4642 size = PAGE_SIZE - info->read;
4646 ret = copy_to_user(ubuf, info->spare + info->read, size);
4657 mutex_unlock(&trace_types_lock);
4662 static int tracing_buffers_release(struct inode *inode, struct file *file)
4664 struct ftrace_buffer_info *info = file->private_data;
4665 struct trace_iterator *iter = &info->iter;
4667 mutex_lock(&trace_types_lock);
4669 WARN_ON(!iter->tr->ref);
4673 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
4676 mutex_unlock(&trace_types_lock);
4682 struct ring_buffer *buffer;
4687 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4688 struct pipe_buffer *buf)
4690 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4695 ring_buffer_free_read_page(ref->buffer, ref->page);
4700 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4701 struct pipe_buffer *buf)
4703 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4708 /* Pipe buffer operations for a buffer. */
4709 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4711 .map = generic_pipe_buf_map,
4712 .unmap = generic_pipe_buf_unmap,
4713 .confirm = generic_pipe_buf_confirm,
4714 .release = buffer_pipe_buf_release,
4715 .steal = generic_pipe_buf_steal,
4716 .get = buffer_pipe_buf_get,
4720 * Callback from splice_to_pipe(), if we need to release some pages
4721 * at the end of the spd in case we error'ed out in filling the pipe.
4723 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4725 struct buffer_ref *ref =
4726 (struct buffer_ref *)spd->partial[i].private;
4731 ring_buffer_free_read_page(ref->buffer, ref->page);
4733 spd->partial[i].private = 0;
4737 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4738 struct pipe_inode_info *pipe, size_t len,
4741 struct ftrace_buffer_info *info = file->private_data;
4742 struct trace_iterator *iter = &info->iter;
4743 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4744 struct page *pages_def[PIPE_DEF_BUFFERS];
4745 struct splice_pipe_desc spd = {
4747 .partial = partial_def,
4748 .nr_pages_max = PIPE_DEF_BUFFERS,
4750 .ops = &buffer_pipe_buf_ops,
4751 .spd_release = buffer_spd_release,
4753 struct buffer_ref *ref;
4754 int entries, size, i;
4757 mutex_lock(&trace_types_lock);
4759 #ifdef CONFIG_TRACER_MAX_TRACE
4760 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4766 if (splice_grow_spd(pipe, &spd)) {
4771 if (*ppos & (PAGE_SIZE - 1)) {
4776 if (len & (PAGE_SIZE - 1)) {
4777 if (len < PAGE_SIZE) {
4785 trace_access_lock(iter->cpu_file);
4786 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
4788 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4792 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4797 ref->buffer = iter->trace_buffer->buffer;
4798 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
4804 r = ring_buffer_read_page(ref->buffer, &ref->page,
4805 len, iter->cpu_file, 1);
4807 ring_buffer_free_read_page(ref->buffer, ref->page);
4813 * zero out any left over data, this is going to
4816 size = ring_buffer_page_len(ref->page);
4817 if (size < PAGE_SIZE)
4818 memset(ref->page + size, 0, PAGE_SIZE - size);
4820 page = virt_to_page(ref->page);
4822 spd.pages[i] = page;
4823 spd.partial[i].len = PAGE_SIZE;
4824 spd.partial[i].offset = 0;
4825 spd.partial[i].private = (unsigned long)ref;
4829 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
4832 trace_access_unlock(iter->cpu_file);
4835 /* did we read anything? */
4836 if (!spd.nr_pages) {
4837 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
4841 mutex_unlock(&trace_types_lock);
4842 iter->trace->wait_pipe(iter);
4843 mutex_lock(&trace_types_lock);
4844 if (signal_pending(current)) {
4851 ret = splice_to_pipe(pipe, &spd);
4852 splice_shrink_spd(&spd);
4854 mutex_unlock(&trace_types_lock);
4859 static const struct file_operations tracing_buffers_fops = {
4860 .open = tracing_buffers_open,
4861 .read = tracing_buffers_read,
4862 .poll = tracing_buffers_poll,
4863 .release = tracing_buffers_release,
4864 .splice_read = tracing_buffers_splice_read,
4865 .llseek = no_llseek,
4869 tracing_stats_read(struct file *filp, char __user *ubuf,
4870 size_t count, loff_t *ppos)
4872 struct trace_cpu *tc = filp->private_data;
4873 struct trace_array *tr = tc->tr;
4874 struct trace_buffer *trace_buf = &tr->trace_buffer;
4875 struct trace_seq *s;
4877 unsigned long long t;
4878 unsigned long usec_rem;
4881 s = kmalloc(sizeof(*s), GFP_KERNEL);
4887 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
4888 trace_seq_printf(s, "entries: %ld\n", cnt);
4890 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
4891 trace_seq_printf(s, "overrun: %ld\n", cnt);
4893 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
4894 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4896 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
4897 trace_seq_printf(s, "bytes: %ld\n", cnt);
4899 if (trace_clocks[trace_clock_id].in_ns) {
4900 /* local or global for trace_clock */
4901 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
4902 usec_rem = do_div(t, USEC_PER_SEC);
4903 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
4906 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
4907 usec_rem = do_div(t, USEC_PER_SEC);
4908 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4910 /* counter or tsc mode for trace_clock */
4911 trace_seq_printf(s, "oldest event ts: %llu\n",
4912 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
4914 trace_seq_printf(s, "now ts: %llu\n",
4915 ring_buffer_time_stamp(trace_buf->buffer, cpu));
4918 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
4919 trace_seq_printf(s, "dropped events: %ld\n", cnt);
4921 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
4922 trace_seq_printf(s, "read events: %ld\n", cnt);
4924 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4931 static const struct file_operations tracing_stats_fops = {
4932 .open = tracing_open_generic,
4933 .read = tracing_stats_read,
4934 .llseek = generic_file_llseek,
4937 #ifdef CONFIG_DYNAMIC_FTRACE
4939 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4945 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
4946 size_t cnt, loff_t *ppos)
4948 static char ftrace_dyn_info_buffer[1024];
4949 static DEFINE_MUTEX(dyn_info_mutex);
4950 unsigned long *p = filp->private_data;
4951 char *buf = ftrace_dyn_info_buffer;
4952 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
4955 mutex_lock(&dyn_info_mutex);
4956 r = sprintf(buf, "%ld ", *p);
4958 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
4961 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4963 mutex_unlock(&dyn_info_mutex);
4968 static const struct file_operations tracing_dyn_info_fops = {
4969 .open = tracing_open_generic,
4970 .read = tracing_read_dyn_info,
4971 .llseek = generic_file_llseek,
4975 struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
4982 if (!debugfs_initialized())
4985 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
4986 tr->dir = debugfs_create_dir("tracing", NULL);
4988 if (!tr->dir && !once) {
4990 pr_warning("Could not create debugfs directory 'tracing'\n");
4997 struct dentry *tracing_init_dentry(void)
4999 return tracing_init_dentry_tr(&global_trace);
5002 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
5004 struct dentry *d_tracer;
5007 return tr->percpu_dir;
5009 d_tracer = tracing_init_dentry_tr(tr);
5013 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
5015 WARN_ONCE(!tr->percpu_dir,
5016 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
5018 return tr->percpu_dir;
5022 tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
5024 struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
5025 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5026 struct dentry *d_cpu;
5027 char cpu_dir[30]; /* 30 characters should be more than enough */
5032 snprintf(cpu_dir, 30, "cpu%ld", cpu);
5033 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5035 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5039 /* per cpu trace_pipe */
5040 trace_create_file("trace_pipe", 0444, d_cpu,
5041 (void *)&data->trace_cpu, &tracing_pipe_fops);
5044 trace_create_file("trace", 0644, d_cpu,
5045 (void *)&data->trace_cpu, &tracing_fops);
5047 trace_create_file("trace_pipe_raw", 0444, d_cpu,
5048 (void *)&data->trace_cpu, &tracing_buffers_fops);
5050 trace_create_file("stats", 0444, d_cpu,
5051 (void *)&data->trace_cpu, &tracing_stats_fops);
5053 trace_create_file("buffer_size_kb", 0444, d_cpu,
5054 (void *)&data->trace_cpu, &tracing_entries_fops);
5056 #ifdef CONFIG_TRACER_SNAPSHOT
5057 trace_create_file("snapshot", 0644, d_cpu,
5058 (void *)&data->trace_cpu, &snapshot_fops);
5060 trace_create_file("snapshot_raw", 0444, d_cpu,
5061 (void *)&data->trace_cpu, &snapshot_raw_fops);
5065 #ifdef CONFIG_FTRACE_SELFTEST
5066 /* Let selftest have access to static functions in this file */
5067 #include "trace_selftest.c"
5070 struct trace_option_dentry {
5071 struct tracer_opt *opt;
5072 struct tracer_flags *flags;
5073 struct trace_array *tr;
5074 struct dentry *entry;
5078 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5081 struct trace_option_dentry *topt = filp->private_data;
5084 if (topt->flags->val & topt->opt->bit)
5089 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5093 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5096 struct trace_option_dentry *topt = filp->private_data;
5100 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5104 if (val != 0 && val != 1)
5107 if (!!(topt->flags->val & topt->opt->bit) != val) {
5108 mutex_lock(&trace_types_lock);
5109 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
5111 mutex_unlock(&trace_types_lock);
5122 static const struct file_operations trace_options_fops = {
5123 .open = tracing_open_generic,
5124 .read = trace_options_read,
5125 .write = trace_options_write,
5126 .llseek = generic_file_llseek,
5130 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5133 long index = (long)filp->private_data;
5136 if (trace_flags & (1 << index))
5141 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5145 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5148 struct trace_array *tr = &global_trace;
5149 long index = (long)filp->private_data;
5153 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5157 if (val != 0 && val != 1)
5160 mutex_lock(&trace_types_lock);
5161 ret = set_tracer_flag(tr, 1 << index, val);
5162 mutex_unlock(&trace_types_lock);
5172 static const struct file_operations trace_options_core_fops = {
5173 .open = tracing_open_generic,
5174 .read = trace_options_core_read,
5175 .write = trace_options_core_write,
5176 .llseek = generic_file_llseek,
5179 struct dentry *trace_create_file(const char *name,
5181 struct dentry *parent,
5183 const struct file_operations *fops)
5187 ret = debugfs_create_file(name, mode, parent, data, fops);
5189 pr_warning("Could not create debugfs '%s' entry\n", name);
5195 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
5197 struct dentry *d_tracer;
5202 d_tracer = tracing_init_dentry_tr(tr);
5206 tr->options = debugfs_create_dir("options", d_tracer);
5208 pr_warning("Could not create debugfs directory 'options'\n");
5216 create_trace_option_file(struct trace_array *tr,
5217 struct trace_option_dentry *topt,
5218 struct tracer_flags *flags,
5219 struct tracer_opt *opt)
5221 struct dentry *t_options;
5223 t_options = trace_options_init_dentry(tr);
5227 topt->flags = flags;
5231 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
5232 &trace_options_fops);
5236 static struct trace_option_dentry *
5237 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
5239 struct trace_option_dentry *topts;
5240 struct tracer_flags *flags;
5241 struct tracer_opt *opts;
5247 flags = tracer->flags;
5249 if (!flags || !flags->opts)
5254 for (cnt = 0; opts[cnt].name; cnt++)
5257 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
5261 for (cnt = 0; opts[cnt].name; cnt++)
5262 create_trace_option_file(tr, &topts[cnt], flags,
5269 destroy_trace_option_files(struct trace_option_dentry *topts)
5276 for (cnt = 0; topts[cnt].opt; cnt++) {
5277 if (topts[cnt].entry)
5278 debugfs_remove(topts[cnt].entry);
5284 static struct dentry *
5285 create_trace_option_core_file(struct trace_array *tr,
5286 const char *option, long index)
5288 struct dentry *t_options;
5290 t_options = trace_options_init_dentry(tr);
5294 return trace_create_file(option, 0644, t_options, (void *)index,
5295 &trace_options_core_fops);
5298 static __init void create_trace_options_dir(struct trace_array *tr)
5300 struct dentry *t_options;
5303 t_options = trace_options_init_dentry(tr);
5307 for (i = 0; trace_options[i]; i++)
5308 create_trace_option_core_file(tr, trace_options[i], i);
5312 rb_simple_read(struct file *filp, char __user *ubuf,
5313 size_t cnt, loff_t *ppos)
5315 struct trace_array *tr = filp->private_data;
5316 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5321 r = ring_buffer_record_is_on(buffer);
5325 r = sprintf(buf, "%d\n", r);
5327 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5331 rb_simple_write(struct file *filp, const char __user *ubuf,
5332 size_t cnt, loff_t *ppos)
5334 struct trace_array *tr = filp->private_data;
5335 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5339 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5344 mutex_lock(&trace_types_lock);
5346 ring_buffer_record_on(buffer);
5347 if (tr->current_trace->start)
5348 tr->current_trace->start(tr);
5350 ring_buffer_record_off(buffer);
5351 if (tr->current_trace->stop)
5352 tr->current_trace->stop(tr);
5354 mutex_unlock(&trace_types_lock);
5362 static const struct file_operations rb_simple_fops = {
5363 .open = tracing_open_generic,
5364 .read = rb_simple_read,
5365 .write = rb_simple_write,
5366 .llseek = default_llseek,
5369 struct dentry *trace_instance_dir;
5372 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5374 static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
5378 for_each_tracing_cpu(cpu) {
5379 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
5380 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
5381 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
5385 static int allocate_trace_buffers(struct trace_array *tr, int size)
5387 enum ring_buffer_flags rb_flags;
5389 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5391 tr->trace_buffer.buffer = ring_buffer_alloc(size, rb_flags);
5392 if (!tr->trace_buffer.buffer)
5395 tr->trace_buffer.data = alloc_percpu(struct trace_array_cpu);
5396 if (!tr->trace_buffer.data)
5399 init_trace_buffers(tr, &tr->trace_buffer);
5401 /* Allocate the first page for all buffers */
5402 set_buffer_entries(&tr->trace_buffer,
5403 ring_buffer_size(tr->trace_buffer.buffer, 0));
5405 #ifdef CONFIG_TRACER_MAX_TRACE
5407 tr->max_buffer.buffer = ring_buffer_alloc(1, rb_flags);
5408 if (!tr->max_buffer.buffer)
5411 tr->max_buffer.data = alloc_percpu(struct trace_array_cpu);
5412 if (!tr->max_buffer.data)
5415 init_trace_buffers(tr, &tr->max_buffer);
5417 set_buffer_entries(&tr->max_buffer, 1);
5422 if (tr->trace_buffer.buffer)
5423 ring_buffer_free(tr->trace_buffer.buffer);
5424 free_percpu(tr->trace_buffer.data);
5426 #ifdef CONFIG_TRACER_MAX_TRACE
5427 if (tr->max_buffer.buffer)
5428 ring_buffer_free(tr->max_buffer.buffer);
5429 free_percpu(tr->max_buffer.data);
5434 static int new_instance_create(const char *name)
5436 struct trace_array *tr;
5439 mutex_lock(&trace_types_lock);
5442 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5443 if (tr->name && strcmp(tr->name, name) == 0)
5448 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
5452 tr->name = kstrdup(name, GFP_KERNEL);
5456 raw_spin_lock_init(&tr->start_lock);
5458 tr->current_trace = &nop_trace;
5460 INIT_LIST_HEAD(&tr->systems);
5461 INIT_LIST_HEAD(&tr->events);
5463 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
5466 /* Holder for file callbacks */
5467 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5468 tr->trace_cpu.tr = tr;
5470 tr->dir = debugfs_create_dir(name, trace_instance_dir);
5474 ret = event_trace_add_tracer(tr->dir, tr);
5478 init_tracer_debugfs(tr, tr->dir);
5480 list_add(&tr->list, &ftrace_trace_arrays);
5482 mutex_unlock(&trace_types_lock);
5487 if (tr->trace_buffer.buffer)
5488 ring_buffer_free(tr->trace_buffer.buffer);
5493 mutex_unlock(&trace_types_lock);
5499 static int instance_delete(const char *name)
5501 struct trace_array *tr;
5505 mutex_lock(&trace_types_lock);
5508 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5509 if (tr->name && strcmp(tr->name, name) == 0) {
5521 list_del(&tr->list);
5523 event_trace_del_tracer(tr);
5524 debugfs_remove_recursive(tr->dir);
5525 free_percpu(tr->trace_buffer.data);
5526 ring_buffer_free(tr->trace_buffer.buffer);
5534 mutex_unlock(&trace_types_lock);
5539 static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
5541 struct dentry *parent;
5544 /* Paranoid: Make sure the parent is the "instances" directory */
5545 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5546 if (WARN_ON_ONCE(parent != trace_instance_dir))
5550 * The inode mutex is locked, but debugfs_create_dir() will also
5551 * take the mutex. As the instances directory can not be destroyed
5552 * or changed in any other way, it is safe to unlock it, and
5553 * let the dentry try. If two users try to make the same dir at
5554 * the same time, then the new_instance_create() will determine the
5557 mutex_unlock(&inode->i_mutex);
5559 ret = new_instance_create(dentry->d_iname);
5561 mutex_lock(&inode->i_mutex);
5566 static int instance_rmdir(struct inode *inode, struct dentry *dentry)
5568 struct dentry *parent;
5571 /* Paranoid: Make sure the parent is the "instances" directory */
5572 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5573 if (WARN_ON_ONCE(parent != trace_instance_dir))
5576 /* The caller did a dget() on dentry */
5577 mutex_unlock(&dentry->d_inode->i_mutex);
5580 * The inode mutex is locked, but debugfs_create_dir() will also
5581 * take the mutex. As the instances directory can not be destroyed
5582 * or changed in any other way, it is safe to unlock it, and
5583 * let the dentry try. If two users try to make the same dir at
5584 * the same time, then the instance_delete() will determine the
5587 mutex_unlock(&inode->i_mutex);
5589 ret = instance_delete(dentry->d_iname);
5591 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
5592 mutex_lock(&dentry->d_inode->i_mutex);
5597 static const struct inode_operations instance_dir_inode_operations = {
5598 .lookup = simple_lookup,
5599 .mkdir = instance_mkdir,
5600 .rmdir = instance_rmdir,
5603 static __init void create_trace_instances(struct dentry *d_tracer)
5605 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
5606 if (WARN_ON(!trace_instance_dir))
5609 /* Hijack the dir inode operations, to allow mkdir */
5610 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
5614 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
5618 trace_create_file("trace_options", 0644, d_tracer,
5619 tr, &tracing_iter_fops);
5621 trace_create_file("trace", 0644, d_tracer,
5622 (void *)&tr->trace_cpu, &tracing_fops);
5624 trace_create_file("trace_pipe", 0444, d_tracer,
5625 (void *)&tr->trace_cpu, &tracing_pipe_fops);
5627 trace_create_file("buffer_size_kb", 0644, d_tracer,
5628 (void *)&tr->trace_cpu, &tracing_entries_fops);
5630 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
5631 tr, &tracing_total_entries_fops);
5633 trace_create_file("free_buffer", 0644, d_tracer,
5634 tr, &tracing_free_buffer_fops);
5636 trace_create_file("trace_marker", 0220, d_tracer,
5637 tr, &tracing_mark_fops);
5639 trace_create_file("trace_clock", 0644, d_tracer, tr,
5642 trace_create_file("tracing_on", 0644, d_tracer,
5643 tr, &rb_simple_fops);
5645 #ifdef CONFIG_TRACER_SNAPSHOT
5646 trace_create_file("snapshot", 0644, d_tracer,
5647 (void *)&tr->trace_cpu, &snapshot_fops);
5650 for_each_tracing_cpu(cpu)
5651 tracing_init_debugfs_percpu(tr, cpu);
5655 static __init int tracer_init_debugfs(void)
5657 struct dentry *d_tracer;
5659 trace_access_lock_init();
5661 d_tracer = tracing_init_dentry();
5663 init_tracer_debugfs(&global_trace, d_tracer);
5665 trace_create_file("tracing_cpumask", 0644, d_tracer,
5666 &global_trace, &tracing_cpumask_fops);
5668 trace_create_file("available_tracers", 0444, d_tracer,
5669 &global_trace, &show_traces_fops);
5671 trace_create_file("current_tracer", 0644, d_tracer,
5672 &global_trace, &set_tracer_fops);
5674 #ifdef CONFIG_TRACER_MAX_TRACE
5675 trace_create_file("tracing_max_latency", 0644, d_tracer,
5676 &tracing_max_latency, &tracing_max_lat_fops);
5679 trace_create_file("tracing_thresh", 0644, d_tracer,
5680 &tracing_thresh, &tracing_max_lat_fops);
5682 trace_create_file("README", 0444, d_tracer,
5683 NULL, &tracing_readme_fops);
5685 trace_create_file("saved_cmdlines", 0444, d_tracer,
5686 NULL, &tracing_saved_cmdlines_fops);
5688 #ifdef CONFIG_DYNAMIC_FTRACE
5689 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
5690 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
5693 create_trace_instances(d_tracer);
5695 create_trace_options_dir(&global_trace);
5700 static int trace_panic_handler(struct notifier_block *this,
5701 unsigned long event, void *unused)
5703 if (ftrace_dump_on_oops)
5704 ftrace_dump(ftrace_dump_on_oops);
5708 static struct notifier_block trace_panic_notifier = {
5709 .notifier_call = trace_panic_handler,
5711 .priority = 150 /* priority: INT_MAX >= x >= 0 */
5714 static int trace_die_handler(struct notifier_block *self,
5720 if (ftrace_dump_on_oops)
5721 ftrace_dump(ftrace_dump_on_oops);
5729 static struct notifier_block trace_die_notifier = {
5730 .notifier_call = trace_die_handler,
5735 * printk is set to max of 1024, we really don't need it that big.
5736 * Nothing should be printing 1000 characters anyway.
5738 #define TRACE_MAX_PRINT 1000
5741 * Define here KERN_TRACE so that we have one place to modify
5742 * it if we decide to change what log level the ftrace dump
5745 #define KERN_TRACE KERN_EMERG
5748 trace_printk_seq(struct trace_seq *s)
5750 /* Probably should print a warning here. */
5754 /* should be zero ended, but we are paranoid. */
5755 s->buffer[s->len] = 0;
5757 printk(KERN_TRACE "%s", s->buffer);
5762 void trace_init_global_iter(struct trace_iterator *iter)
5764 iter->tr = &global_trace;
5765 iter->trace = iter->tr->current_trace;
5766 iter->cpu_file = RING_BUFFER_ALL_CPUS;
5767 iter->trace_buffer = &global_trace.trace_buffer;
5771 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
5773 static arch_spinlock_t ftrace_dump_lock =
5774 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
5775 /* use static because iter can be a bit big for the stack */
5776 static struct trace_iterator iter;
5777 unsigned int old_userobj;
5778 static int dump_ran;
5779 unsigned long flags;
5783 local_irq_save(flags);
5784 arch_spin_lock(&ftrace_dump_lock);
5792 /* Did function tracer already get disabled? */
5793 if (ftrace_is_dead()) {
5794 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
5795 printk("# MAY BE MISSING FUNCTION EVENTS\n");
5798 if (disable_tracing)
5801 /* Simulate the iterator */
5802 trace_init_global_iter(&iter);
5804 for_each_tracing_cpu(cpu) {
5805 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
5808 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
5810 /* don't look at user memory in panic mode */
5811 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
5813 switch (oops_dump_mode) {
5815 iter.cpu_file = RING_BUFFER_ALL_CPUS;
5818 iter.cpu_file = raw_smp_processor_id();
5823 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
5824 iter.cpu_file = RING_BUFFER_ALL_CPUS;
5827 printk(KERN_TRACE "Dumping ftrace buffer:\n");
5830 * We need to stop all tracing on all CPUS to read the
5831 * the next buffer. This is a bit expensive, but is
5832 * not done often. We fill all what we can read,
5833 * and then release the locks again.
5836 while (!trace_empty(&iter)) {
5839 printk(KERN_TRACE "---------------------------------\n");
5843 /* reset all but tr, trace, and overruns */
5844 memset(&iter.seq, 0,
5845 sizeof(struct trace_iterator) -
5846 offsetof(struct trace_iterator, seq));
5847 iter.iter_flags |= TRACE_FILE_LAT_FMT;
5850 if (trace_find_next_entry_inc(&iter) != NULL) {
5853 ret = print_trace_line(&iter);
5854 if (ret != TRACE_TYPE_NO_CONSUME)
5855 trace_consume(&iter);
5857 touch_nmi_watchdog();
5859 trace_printk_seq(&iter.seq);
5863 printk(KERN_TRACE " (ftrace buffer empty)\n");
5865 printk(KERN_TRACE "---------------------------------\n");
5868 /* Re-enable tracing if requested */
5869 if (!disable_tracing) {
5870 trace_flags |= old_userobj;
5872 for_each_tracing_cpu(cpu) {
5873 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
5879 arch_spin_unlock(&ftrace_dump_lock);
5880 local_irq_restore(flags);
5883 /* By default: disable tracing after the dump */
5884 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
5886 __ftrace_dump(true, oops_dump_mode);
5888 EXPORT_SYMBOL_GPL(ftrace_dump);
5890 __init static int tracer_alloc_buffers(void)
5896 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
5899 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
5900 goto out_free_buffer_mask;
5902 /* Only allocate trace_printk buffers if a trace_printk exists */
5903 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
5904 /* Must be called before global_trace.buffer is allocated */
5905 trace_printk_init_buffers();
5907 /* To save memory, keep the ring buffer size to its minimum */
5908 if (ring_buffer_expanded)
5909 ring_buf_size = trace_buf_size;
5913 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
5914 cpumask_copy(tracing_cpumask, cpu_all_mask);
5916 raw_spin_lock_init(&global_trace.start_lock);
5918 /* TODO: make the number of buffers hot pluggable with CPUS */
5919 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
5920 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
5922 goto out_free_cpumask;
5925 if (global_trace.buffer_disabled)
5928 trace_init_cmdlines();
5930 register_tracer(&nop_trace);
5932 global_trace.current_trace = &nop_trace;
5934 /* All seems OK, enable tracing */
5935 tracing_disabled = 0;
5937 atomic_notifier_chain_register(&panic_notifier_list,
5938 &trace_panic_notifier);
5940 register_die_notifier(&trace_die_notifier);
5942 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
5944 /* Holder for file callbacks */
5945 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5946 global_trace.trace_cpu.tr = &global_trace;
5948 INIT_LIST_HEAD(&global_trace.systems);
5949 INIT_LIST_HEAD(&global_trace.events);
5950 list_add(&global_trace.list, &ftrace_trace_arrays);
5952 while (trace_boot_options) {
5955 option = strsep(&trace_boot_options, ",");
5956 trace_set_options(&global_trace, option);
5962 free_percpu(global_trace.trace_buffer.data);
5963 #ifdef CONFIG_TRACER_MAX_TRACE
5964 free_percpu(global_trace.max_buffer.data);
5966 free_cpumask_var(tracing_cpumask);
5967 out_free_buffer_mask:
5968 free_cpumask_var(tracing_buffer_mask);
5973 __init static int clear_boot_tracer(void)
5976 * The default tracer at boot buffer is an init section.
5977 * This function is called in lateinit. If we did not
5978 * find the boot tracer, then clear it out, to prevent
5979 * later registration from accessing the buffer that is
5980 * about to be freed.
5982 if (!default_bootup_tracer)
5985 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
5986 default_bootup_tracer);
5987 default_bootup_tracer = NULL;
5992 early_initcall(tracer_alloc_buffers);
5993 fs_initcall(tracer_init_debugfs);
5994 late_initcall(clear_boot_tracer);