2 * ring buffer based function tracer
4 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 Nadia Yvette Chambers
14 #include <linux/ring_buffer.h>
15 #include <generated/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/rwsem.h>
35 #include <linux/slab.h>
36 #include <linux/ctype.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
39 #include <linux/nmi.h>
41 #include <linux/sched/rt.h>
44 #include "trace_output.h"
47 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
50 bool ring_buffer_expanded;
53 * We need to change this state when a selftest is running.
54 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
56 * insertions into the ring-buffer such as trace_printk could occurred
57 * at the same time, giving false positive or negative results.
59 static bool __read_mostly tracing_selftest_running;
62 * If a tracer is running, we do not want to run SELFTEST.
64 bool __read_mostly tracing_selftest_disabled;
66 /* For tracers that don't implement custom flags */
67 static struct tracer_opt dummy_tracer_opt[] = {
71 static struct tracer_flags dummy_tracer_flags = {
73 .opts = dummy_tracer_opt
76 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
82 * To prevent the comm cache from being overwritten when no
83 * tracing is active, only save the comm when a trace event
86 static DEFINE_PER_CPU(bool, trace_cmdline_save);
89 * Kill all tracing for good (never come back).
90 * It is initialized to 1 but will turn to zero if the initialization
91 * of the tracer is successful. But that is the only place that sets
94 static int tracing_disabled = 1;
96 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
98 cpumask_var_t __read_mostly tracing_buffer_mask;
101 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
103 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
104 * is set, then ftrace_dump is called. This will output the contents
105 * of the ftrace buffers to the console. This is very useful for
106 * capturing traces that lead to crashes and outputing it to a
109 * It is default off, but you can enable it with either specifying
110 * "ftrace_dump_on_oops" in the kernel command line, or setting
111 * /proc/sys/kernel/ftrace_dump_on_oops
112 * Set 1 if you want to dump buffers of all CPUs
113 * Set 2 if you want to dump the buffer of the CPU that triggered oops
116 enum ftrace_dump_mode ftrace_dump_on_oops;
118 static int tracing_set_tracer(const char *buf);
120 #define MAX_TRACER_SIZE 100
121 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
122 static char *default_bootup_tracer;
124 static bool allocate_snapshot;
126 static int __init set_cmdline_ftrace(char *str)
128 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
129 default_bootup_tracer = bootup_tracer_buf;
130 /* We are using ftrace early, expand it */
131 ring_buffer_expanded = true;
134 __setup("ftrace=", set_cmdline_ftrace);
136 static int __init set_ftrace_dump_on_oops(char *str)
138 if (*str++ != '=' || !*str) {
139 ftrace_dump_on_oops = DUMP_ALL;
143 if (!strcmp("orig_cpu", str)) {
144 ftrace_dump_on_oops = DUMP_ORIG;
150 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
152 static int __init boot_alloc_snapshot(char *str)
154 allocate_snapshot = true;
155 /* We also need the main ring buffer expanded */
156 ring_buffer_expanded = true;
159 __setup("alloc_snapshot", boot_alloc_snapshot);
162 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
163 static char *trace_boot_options __initdata;
165 static int __init set_trace_boot_options(char *str)
167 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
168 trace_boot_options = trace_boot_options_buf;
171 __setup("trace_options=", set_trace_boot_options);
173 unsigned long long ns2usecs(cycle_t nsec)
181 * The global_trace is the descriptor that holds the tracing
182 * buffers for the live tracing. For each CPU, it contains
183 * a link list of pages that will store trace entries. The
184 * page descriptor of the pages in the memory is used to hold
185 * the link list by linking the lru item in the page descriptor
186 * to each of the pages in the buffer per CPU.
188 * For each active CPU there is a data field that holds the
189 * pages for the buffer for that CPU. Each CPU has the same number
190 * of pages allocated for its buffer.
192 static struct trace_array global_trace;
194 LIST_HEAD(ftrace_trace_arrays);
196 int filter_current_check_discard(struct ring_buffer *buffer,
197 struct ftrace_event_call *call, void *rec,
198 struct ring_buffer_event *event)
200 return filter_check_discard(call, rec, buffer, event);
202 EXPORT_SYMBOL_GPL(filter_current_check_discard);
204 cycle_t ftrace_now(int cpu)
208 /* Early boot up does not have a buffer yet */
209 if (!global_trace.trace_buffer.buffer)
210 return trace_clock_local();
212 ts = ring_buffer_time_stamp(global_trace.trace_buffer.buffer, cpu);
213 ring_buffer_normalize_time_stamp(global_trace.trace_buffer.buffer, cpu, &ts);
218 int tracing_is_enabled(void)
220 return tracing_is_on();
224 * trace_buf_size is the size in bytes that is allocated
225 * for a buffer. Note, the number of bytes is always rounded
228 * This number is purposely set to a low number of 16384.
229 * If the dump on oops happens, it will be much appreciated
230 * to not have to wait for all that output. Anyway this can be
231 * boot time and run time configurable.
233 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
235 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
237 /* trace_types holds a link list of available tracers. */
238 static struct tracer *trace_types __read_mostly;
241 * trace_types_lock is used to protect the trace_types list.
243 static DEFINE_MUTEX(trace_types_lock);
246 * serialize the access of the ring buffer
248 * ring buffer serializes readers, but it is low level protection.
249 * The validity of the events (which returns by ring_buffer_peek() ..etc)
250 * are not protected by ring buffer.
252 * The content of events may become garbage if we allow other process consumes
253 * these events concurrently:
254 * A) the page of the consumed events may become a normal page
255 * (not reader page) in ring buffer, and this page will be rewrited
256 * by events producer.
257 * B) The page of the consumed events may become a page for splice_read,
258 * and this page will be returned to system.
260 * These primitives allow multi process access to different cpu ring buffer
263 * These primitives don't distinguish read-only and read-consume access.
264 * Multi read-only access are also serialized.
268 static DECLARE_RWSEM(all_cpu_access_lock);
269 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
271 static inline void trace_access_lock(int cpu)
273 if (cpu == RING_BUFFER_ALL_CPUS) {
274 /* gain it for accessing the whole ring buffer. */
275 down_write(&all_cpu_access_lock);
277 /* gain it for accessing a cpu ring buffer. */
279 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
280 down_read(&all_cpu_access_lock);
282 /* Secondly block other access to this @cpu ring buffer. */
283 mutex_lock(&per_cpu(cpu_access_lock, cpu));
287 static inline void trace_access_unlock(int cpu)
289 if (cpu == RING_BUFFER_ALL_CPUS) {
290 up_write(&all_cpu_access_lock);
292 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
293 up_read(&all_cpu_access_lock);
297 static inline void trace_access_lock_init(void)
301 for_each_possible_cpu(cpu)
302 mutex_init(&per_cpu(cpu_access_lock, cpu));
307 static DEFINE_MUTEX(access_lock);
309 static inline void trace_access_lock(int cpu)
312 mutex_lock(&access_lock);
315 static inline void trace_access_unlock(int cpu)
318 mutex_unlock(&access_lock);
321 static inline void trace_access_lock_init(void)
327 /* trace_flags holds trace_options default values */
328 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
329 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
330 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
331 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
334 * tracing_on - enable tracing buffers
336 * This function enables tracing buffers that may have been
337 * disabled with tracing_off.
339 void tracing_on(void)
341 if (global_trace.trace_buffer.buffer)
342 ring_buffer_record_on(global_trace.trace_buffer.buffer);
344 * This flag is only looked at when buffers haven't been
345 * allocated yet. We don't really care about the race
346 * between setting this flag and actually turning
349 global_trace.buffer_disabled = 0;
351 EXPORT_SYMBOL_GPL(tracing_on);
354 * __trace_puts - write a constant string into the trace buffer.
355 * @ip: The address of the caller
356 * @str: The constant string to write
357 * @size: The size of the string.
359 int __trace_puts(unsigned long ip, const char *str, int size)
361 struct ring_buffer_event *event;
362 struct ring_buffer *buffer;
363 struct print_entry *entry;
364 unsigned long irq_flags;
367 alloc = sizeof(*entry) + size + 2; /* possible \n added */
369 local_save_flags(irq_flags);
370 buffer = global_trace.trace_buffer.buffer;
371 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
372 irq_flags, preempt_count());
376 entry = ring_buffer_event_data(event);
379 memcpy(&entry->buf, str, size);
381 /* Add a newline if necessary */
382 if (entry->buf[size - 1] != '\n') {
383 entry->buf[size] = '\n';
384 entry->buf[size + 1] = '\0';
386 entry->buf[size] = '\0';
388 __buffer_unlock_commit(buffer, event);
392 EXPORT_SYMBOL_GPL(__trace_puts);
395 * __trace_bputs - write the pointer to a constant string into trace buffer
396 * @ip: The address of the caller
397 * @str: The constant string to write to the buffer to
399 int __trace_bputs(unsigned long ip, const char *str)
401 struct ring_buffer_event *event;
402 struct ring_buffer *buffer;
403 struct bputs_entry *entry;
404 unsigned long irq_flags;
405 int size = sizeof(struct bputs_entry);
407 local_save_flags(irq_flags);
408 buffer = global_trace.trace_buffer.buffer;
409 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
410 irq_flags, preempt_count());
414 entry = ring_buffer_event_data(event);
418 __buffer_unlock_commit(buffer, event);
422 EXPORT_SYMBOL_GPL(__trace_bputs);
424 #ifdef CONFIG_TRACER_SNAPSHOT
426 * trace_snapshot - take a snapshot of the current buffer.
428 * This causes a swap between the snapshot buffer and the current live
429 * tracing buffer. You can use this to take snapshots of the live
430 * trace when some condition is triggered, but continue to trace.
432 * Note, make sure to allocate the snapshot with either
433 * a tracing_snapshot_alloc(), or by doing it manually
434 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
436 * If the snapshot buffer is not allocated, it will stop tracing.
437 * Basically making a permanent snapshot.
439 void tracing_snapshot(void)
441 struct trace_array *tr = &global_trace;
442 struct tracer *tracer = tr->current_trace;
446 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
447 internal_trace_puts("*** snapshot is being ignored ***\n");
451 if (!tr->allocated_snapshot) {
452 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
453 internal_trace_puts("*** stopping trace here! ***\n");
458 /* Note, snapshot can not be used when the tracer uses it */
459 if (tracer->use_max_tr) {
460 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
461 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
465 local_irq_save(flags);
466 update_max_tr(tr, current, smp_processor_id());
467 local_irq_restore(flags);
469 EXPORT_SYMBOL_GPL(tracing_snapshot);
471 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
472 struct trace_buffer *size_buf, int cpu_id);
473 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
475 static int alloc_snapshot(struct trace_array *tr)
479 if (!tr->allocated_snapshot) {
481 /* allocate spare buffer */
482 ret = resize_buffer_duplicate_size(&tr->max_buffer,
483 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
487 tr->allocated_snapshot = true;
493 void free_snapshot(struct trace_array *tr)
496 * We don't free the ring buffer. instead, resize it because
497 * The max_tr ring buffer has some state (e.g. ring->clock) and
498 * we want preserve it.
500 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
501 set_buffer_entries(&tr->max_buffer, 1);
502 tracing_reset_online_cpus(&tr->max_buffer);
503 tr->allocated_snapshot = false;
507 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
509 * This is similar to trace_snapshot(), but it will allocate the
510 * snapshot buffer if it isn't already allocated. Use this only
511 * where it is safe to sleep, as the allocation may sleep.
513 * This causes a swap between the snapshot buffer and the current live
514 * tracing buffer. You can use this to take snapshots of the live
515 * trace when some condition is triggered, but continue to trace.
517 void tracing_snapshot_alloc(void)
519 struct trace_array *tr = &global_trace;
522 ret = alloc_snapshot(tr);
523 if (WARN_ON(ret < 0))
528 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
530 void tracing_snapshot(void)
532 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
534 EXPORT_SYMBOL_GPL(tracing_snapshot);
535 void tracing_snapshot_alloc(void)
540 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
541 #endif /* CONFIG_TRACER_SNAPSHOT */
544 * tracing_off - turn off tracing buffers
546 * This function stops the tracing buffers from recording data.
547 * It does not disable any overhead the tracers themselves may
548 * be causing. This function simply causes all recording to
549 * the ring buffers to fail.
551 void tracing_off(void)
553 if (global_trace.trace_buffer.buffer)
554 ring_buffer_record_off(global_trace.trace_buffer.buffer);
556 * This flag is only looked at when buffers haven't been
557 * allocated yet. We don't really care about the race
558 * between setting this flag and actually turning
561 global_trace.buffer_disabled = 1;
563 EXPORT_SYMBOL_GPL(tracing_off);
566 * tracing_is_on - show state of ring buffers enabled
568 int tracing_is_on(void)
570 if (global_trace.trace_buffer.buffer)
571 return ring_buffer_record_is_on(global_trace.trace_buffer.buffer);
572 return !global_trace.buffer_disabled;
574 EXPORT_SYMBOL_GPL(tracing_is_on);
576 static int __init set_buf_size(char *str)
578 unsigned long buf_size;
582 buf_size = memparse(str, &str);
583 /* nr_entries can not be zero */
586 trace_buf_size = buf_size;
589 __setup("trace_buf_size=", set_buf_size);
591 static int __init set_tracing_thresh(char *str)
593 unsigned long threshold;
598 ret = kstrtoul(str, 0, &threshold);
601 tracing_thresh = threshold * 1000;
604 __setup("tracing_thresh=", set_tracing_thresh);
606 unsigned long nsecs_to_usecs(unsigned long nsecs)
611 /* These must match the bit postions in trace_iterator_flags */
612 static const char *trace_options[] = {
645 int in_ns; /* is this clock in nanoseconds? */
647 { trace_clock_local, "local", 1 },
648 { trace_clock_global, "global", 1 },
649 { trace_clock_counter, "counter", 0 },
650 { trace_clock_jiffies, "uptime", 1 },
651 { trace_clock, "perf", 1 },
658 * trace_parser_get_init - gets the buffer for trace parser
660 int trace_parser_get_init(struct trace_parser *parser, int size)
662 memset(parser, 0, sizeof(*parser));
664 parser->buffer = kmalloc(size, GFP_KERNEL);
673 * trace_parser_put - frees the buffer for trace parser
675 void trace_parser_put(struct trace_parser *parser)
677 kfree(parser->buffer);
681 * trace_get_user - reads the user input string separated by space
682 * (matched by isspace(ch))
684 * For each string found the 'struct trace_parser' is updated,
685 * and the function returns.
687 * Returns number of bytes read.
689 * See kernel/trace/trace.h for 'struct trace_parser' details.
691 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
692 size_t cnt, loff_t *ppos)
699 trace_parser_clear(parser);
701 ret = get_user(ch, ubuf++);
709 * The parser is not finished with the last write,
710 * continue reading the user input without skipping spaces.
713 /* skip white space */
714 while (cnt && isspace(ch)) {
715 ret = get_user(ch, ubuf++);
722 /* only spaces were written */
732 /* read the non-space input */
733 while (cnt && !isspace(ch)) {
734 if (parser->idx < parser->size - 1)
735 parser->buffer[parser->idx++] = ch;
740 ret = get_user(ch, ubuf++);
747 /* We either got finished input or we have to wait for another call. */
749 parser->buffer[parser->idx] = 0;
750 parser->cont = false;
753 parser->buffer[parser->idx++] = ch;
763 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
771 if (s->len <= s->readpos)
774 len = s->len - s->readpos;
777 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
787 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
791 if (s->len <= s->readpos)
794 len = s->len - s->readpos;
797 memcpy(buf, s->buffer + s->readpos, cnt);
804 * ftrace_max_lock is used to protect the swapping of buffers
805 * when taking a max snapshot. The buffers themselves are
806 * protected by per_cpu spinlocks. But the action of the swap
807 * needs its own lock.
809 * This is defined as a arch_spinlock_t in order to help
810 * with performance when lockdep debugging is enabled.
812 * It is also used in other places outside the update_max_tr
813 * so it needs to be defined outside of the
814 * CONFIG_TRACER_MAX_TRACE.
816 static arch_spinlock_t ftrace_max_lock =
817 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
819 unsigned long __read_mostly tracing_thresh;
821 #ifdef CONFIG_TRACER_MAX_TRACE
822 unsigned long __read_mostly tracing_max_latency;
825 * Copy the new maximum trace into the separate maximum-trace
826 * structure. (this way the maximum trace is permanently saved,
827 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
830 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
832 struct trace_buffer *trace_buf = &tr->trace_buffer;
833 struct trace_buffer *max_buf = &tr->max_buffer;
834 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
835 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
838 max_buf->time_start = data->preempt_timestamp;
840 max_data->saved_latency = tracing_max_latency;
841 max_data->critical_start = data->critical_start;
842 max_data->critical_end = data->critical_end;
844 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
845 max_data->pid = tsk->pid;
847 * If tsk == current, then use current_uid(), as that does not use
848 * RCU. The irq tracer can be called out of RCU scope.
851 max_data->uid = current_uid();
853 max_data->uid = task_uid(tsk);
855 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
856 max_data->policy = tsk->policy;
857 max_data->rt_priority = tsk->rt_priority;
859 /* record this tasks comm */
860 tracing_record_cmdline(tsk);
864 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
866 * @tsk: the task with the latency
867 * @cpu: The cpu that initiated the trace.
869 * Flip the buffers between the @tr and the max_tr and record information
870 * about which task was the cause of this latency.
873 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
875 struct ring_buffer *buf;
880 WARN_ON_ONCE(!irqs_disabled());
882 if (!tr->allocated_snapshot) {
883 /* Only the nop tracer should hit this when disabling */
884 WARN_ON_ONCE(tr->current_trace != &nop_trace);
888 arch_spin_lock(&ftrace_max_lock);
890 buf = tr->trace_buffer.buffer;
891 tr->trace_buffer.buffer = tr->max_buffer.buffer;
892 tr->max_buffer.buffer = buf;
894 __update_max_tr(tr, tsk, cpu);
895 arch_spin_unlock(&ftrace_max_lock);
899 * update_max_tr_single - only copy one trace over, and reset the rest
901 * @tsk - task with the latency
902 * @cpu - the cpu of the buffer to copy.
904 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
907 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
914 WARN_ON_ONCE(!irqs_disabled());
915 if (!tr->allocated_snapshot) {
916 /* Only the nop tracer should hit this when disabling */
917 WARN_ON_ONCE(tr->current_trace != &nop_trace);
921 arch_spin_lock(&ftrace_max_lock);
923 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
927 * We failed to swap the buffer due to a commit taking
928 * place on this CPU. We fail to record, but we reset
929 * the max trace buffer (no one writes directly to it)
930 * and flag that it failed.
932 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
933 "Failed to swap buffers due to commit in progress\n");
936 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
938 __update_max_tr(tr, tsk, cpu);
939 arch_spin_unlock(&ftrace_max_lock);
941 #endif /* CONFIG_TRACER_MAX_TRACE */
943 static void default_wait_pipe(struct trace_iterator *iter)
945 /* Iterators are static, they should be filled or empty */
946 if (trace_buffer_iter(iter, iter->cpu_file))
949 ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
952 #ifdef CONFIG_FTRACE_STARTUP_TEST
953 static int run_tracer_selftest(struct tracer *type)
955 struct trace_array *tr = &global_trace;
956 struct tracer *saved_tracer = tr->current_trace;
959 if (!type->selftest || tracing_selftest_disabled)
963 * Run a selftest on this tracer.
964 * Here we reset the trace buffer, and set the current
965 * tracer to be this tracer. The tracer can then run some
966 * internal tracing to verify that everything is in order.
967 * If we fail, we do not register this tracer.
969 tracing_reset_online_cpus(&tr->trace_buffer);
971 tr->current_trace = type;
973 #ifdef CONFIG_TRACER_MAX_TRACE
974 if (type->use_max_tr) {
975 /* If we expanded the buffers, make sure the max is expanded too */
976 if (ring_buffer_expanded)
977 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
978 RING_BUFFER_ALL_CPUS);
979 tr->allocated_snapshot = true;
983 /* the test is responsible for initializing and enabling */
984 pr_info("Testing tracer %s: ", type->name);
985 ret = type->selftest(type, tr);
986 /* the test is responsible for resetting too */
987 tr->current_trace = saved_tracer;
989 printk(KERN_CONT "FAILED!\n");
990 /* Add the warning after printing 'FAILED' */
994 /* Only reset on passing, to avoid touching corrupted buffers */
995 tracing_reset_online_cpus(&tr->trace_buffer);
997 #ifdef CONFIG_TRACER_MAX_TRACE
998 if (type->use_max_tr) {
999 tr->allocated_snapshot = false;
1001 /* Shrink the max buffer again */
1002 if (ring_buffer_expanded)
1003 ring_buffer_resize(tr->max_buffer.buffer, 1,
1004 RING_BUFFER_ALL_CPUS);
1008 printk(KERN_CONT "PASSED\n");
1012 static inline int run_tracer_selftest(struct tracer *type)
1016 #endif /* CONFIG_FTRACE_STARTUP_TEST */
1019 * register_tracer - register a tracer with the ftrace system.
1020 * @type - the plugin for the tracer
1022 * Register a new plugin tracer.
1024 int register_tracer(struct tracer *type)
1030 pr_info("Tracer must have a name\n");
1034 if (strlen(type->name) >= MAX_TRACER_SIZE) {
1035 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1039 mutex_lock(&trace_types_lock);
1041 tracing_selftest_running = true;
1043 for (t = trace_types; t; t = t->next) {
1044 if (strcmp(type->name, t->name) == 0) {
1046 pr_info("Tracer %s already registered\n",
1053 if (!type->set_flag)
1054 type->set_flag = &dummy_set_flag;
1056 type->flags = &dummy_tracer_flags;
1058 if (!type->flags->opts)
1059 type->flags->opts = dummy_tracer_opt;
1060 if (!type->wait_pipe)
1061 type->wait_pipe = default_wait_pipe;
1063 ret = run_tracer_selftest(type);
1067 type->next = trace_types;
1071 tracing_selftest_running = false;
1072 mutex_unlock(&trace_types_lock);
1074 if (ret || !default_bootup_tracer)
1077 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
1080 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1081 /* Do we want this tracer to start on bootup? */
1082 tracing_set_tracer(type->name);
1083 default_bootup_tracer = NULL;
1084 /* disable other selftests, since this will break it. */
1085 tracing_selftest_disabled = true;
1086 #ifdef CONFIG_FTRACE_STARTUP_TEST
1087 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1095 void tracing_reset(struct trace_buffer *buf, int cpu)
1097 struct ring_buffer *buffer = buf->buffer;
1102 ring_buffer_record_disable(buffer);
1104 /* Make sure all commits have finished */
1105 synchronize_sched();
1106 ring_buffer_reset_cpu(buffer, cpu);
1108 ring_buffer_record_enable(buffer);
1111 void tracing_reset_online_cpus(struct trace_buffer *buf)
1113 struct ring_buffer *buffer = buf->buffer;
1119 ring_buffer_record_disable(buffer);
1121 /* Make sure all commits have finished */
1122 synchronize_sched();
1124 buf->time_start = ftrace_now(buf->cpu);
1126 for_each_online_cpu(cpu)
1127 ring_buffer_reset_cpu(buffer, cpu);
1129 ring_buffer_record_enable(buffer);
1132 void tracing_reset_current(int cpu)
1134 tracing_reset(&global_trace.trace_buffer, cpu);
1137 void tracing_reset_all_online_cpus(void)
1139 struct trace_array *tr;
1141 mutex_lock(&trace_types_lock);
1142 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1143 tracing_reset_online_cpus(&tr->trace_buffer);
1144 #ifdef CONFIG_TRACER_MAX_TRACE
1145 tracing_reset_online_cpus(&tr->max_buffer);
1148 mutex_unlock(&trace_types_lock);
1151 #define SAVED_CMDLINES 128
1152 #define NO_CMDLINE_MAP UINT_MAX
1153 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1154 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1155 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1156 static int cmdline_idx;
1157 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1159 /* temporary disable recording */
1160 static atomic_t trace_record_cmdline_disabled __read_mostly;
1162 static void trace_init_cmdlines(void)
1164 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1165 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
1169 int is_tracing_stopped(void)
1171 return global_trace.stop_count;
1175 * ftrace_off_permanent - disable all ftrace code permanently
1177 * This should only be called when a serious anomally has
1178 * been detected. This will turn off the function tracing,
1179 * ring buffers, and other tracing utilites. It takes no
1180 * locks and can be called from any context.
1182 void ftrace_off_permanent(void)
1184 tracing_disabled = 1;
1186 tracing_off_permanent();
1190 * tracing_start - quick start of the tracer
1192 * If tracing is enabled but was stopped by tracing_stop,
1193 * this will start the tracer back up.
1195 void tracing_start(void)
1197 struct ring_buffer *buffer;
1198 unsigned long flags;
1200 if (tracing_disabled)
1203 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1204 if (--global_trace.stop_count) {
1205 if (global_trace.stop_count < 0) {
1206 /* Someone screwed up their debugging */
1208 global_trace.stop_count = 0;
1213 /* Prevent the buffers from switching */
1214 arch_spin_lock(&ftrace_max_lock);
1216 buffer = global_trace.trace_buffer.buffer;
1218 ring_buffer_record_enable(buffer);
1220 #ifdef CONFIG_TRACER_MAX_TRACE
1221 buffer = global_trace.max_buffer.buffer;
1223 ring_buffer_record_enable(buffer);
1226 arch_spin_unlock(&ftrace_max_lock);
1230 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1233 static void tracing_start_tr(struct trace_array *tr)
1235 struct ring_buffer *buffer;
1236 unsigned long flags;
1238 if (tracing_disabled)
1241 /* If global, we need to also start the max tracer */
1242 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1243 return tracing_start();
1245 raw_spin_lock_irqsave(&tr->start_lock, flags);
1247 if (--tr->stop_count) {
1248 if (tr->stop_count < 0) {
1249 /* Someone screwed up their debugging */
1256 buffer = tr->trace_buffer.buffer;
1258 ring_buffer_record_enable(buffer);
1261 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1265 * tracing_stop - quick stop of the tracer
1267 * Light weight way to stop tracing. Use in conjunction with
1270 void tracing_stop(void)
1272 struct ring_buffer *buffer;
1273 unsigned long flags;
1276 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1277 if (global_trace.stop_count++)
1280 /* Prevent the buffers from switching */
1281 arch_spin_lock(&ftrace_max_lock);
1283 buffer = global_trace.trace_buffer.buffer;
1285 ring_buffer_record_disable(buffer);
1287 #ifdef CONFIG_TRACER_MAX_TRACE
1288 buffer = global_trace.max_buffer.buffer;
1290 ring_buffer_record_disable(buffer);
1293 arch_spin_unlock(&ftrace_max_lock);
1296 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1299 static void tracing_stop_tr(struct trace_array *tr)
1301 struct ring_buffer *buffer;
1302 unsigned long flags;
1304 /* If global, we need to also stop the max tracer */
1305 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1306 return tracing_stop();
1308 raw_spin_lock_irqsave(&tr->start_lock, flags);
1309 if (tr->stop_count++)
1312 buffer = tr->trace_buffer.buffer;
1314 ring_buffer_record_disable(buffer);
1317 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1320 void trace_stop_cmdline_recording(void);
1322 static void trace_save_cmdline(struct task_struct *tsk)
1326 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1330 * It's not the end of the world if we don't get
1331 * the lock, but we also don't want to spin
1332 * nor do we want to disable interrupts,
1333 * so if we miss here, then better luck next time.
1335 if (!arch_spin_trylock(&trace_cmdline_lock))
1338 idx = map_pid_to_cmdline[tsk->pid];
1339 if (idx == NO_CMDLINE_MAP) {
1340 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1343 * Check whether the cmdline buffer at idx has a pid
1344 * mapped. We are going to overwrite that entry so we
1345 * need to clear the map_pid_to_cmdline. Otherwise we
1346 * would read the new comm for the old pid.
1348 pid = map_cmdline_to_pid[idx];
1349 if (pid != NO_CMDLINE_MAP)
1350 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1352 map_cmdline_to_pid[idx] = tsk->pid;
1353 map_pid_to_cmdline[tsk->pid] = idx;
1358 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1360 arch_spin_unlock(&trace_cmdline_lock);
1363 void trace_find_cmdline(int pid, char comm[])
1368 strcpy(comm, "<idle>");
1372 if (WARN_ON_ONCE(pid < 0)) {
1373 strcpy(comm, "<XXX>");
1377 if (pid > PID_MAX_DEFAULT) {
1378 strcpy(comm, "<...>");
1383 arch_spin_lock(&trace_cmdline_lock);
1384 map = map_pid_to_cmdline[pid];
1385 if (map != NO_CMDLINE_MAP)
1386 strcpy(comm, saved_cmdlines[map]);
1388 strcpy(comm, "<...>");
1390 arch_spin_unlock(&trace_cmdline_lock);
1394 void tracing_record_cmdline(struct task_struct *tsk)
1396 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1399 if (!__this_cpu_read(trace_cmdline_save))
1402 __this_cpu_write(trace_cmdline_save, false);
1404 trace_save_cmdline(tsk);
1408 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1411 struct task_struct *tsk = current;
1413 entry->preempt_count = pc & 0xff;
1414 entry->pid = (tsk) ? tsk->pid : 0;
1416 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1417 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1419 TRACE_FLAG_IRQS_NOSUPPORT |
1421 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1422 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1423 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1425 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1427 struct ring_buffer_event *
1428 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1431 unsigned long flags, int pc)
1433 struct ring_buffer_event *event;
1435 event = ring_buffer_lock_reserve(buffer, len);
1436 if (event != NULL) {
1437 struct trace_entry *ent = ring_buffer_event_data(event);
1439 tracing_generic_entry_update(ent, flags, pc);
1447 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1449 __this_cpu_write(trace_cmdline_save, true);
1450 ring_buffer_unlock_commit(buffer, event);
1454 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1455 struct ring_buffer_event *event,
1456 unsigned long flags, int pc)
1458 __buffer_unlock_commit(buffer, event);
1460 ftrace_trace_stack(buffer, flags, 6, pc);
1461 ftrace_trace_userstack(buffer, flags, pc);
1464 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1465 struct ring_buffer_event *event,
1466 unsigned long flags, int pc)
1468 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1470 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1472 struct ring_buffer_event *
1473 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1474 struct ftrace_event_file *ftrace_file,
1475 int type, unsigned long len,
1476 unsigned long flags, int pc)
1478 *current_rb = ftrace_file->tr->trace_buffer.buffer;
1479 return trace_buffer_lock_reserve(*current_rb,
1480 type, len, flags, pc);
1482 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1484 struct ring_buffer_event *
1485 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1486 int type, unsigned long len,
1487 unsigned long flags, int pc)
1489 *current_rb = global_trace.trace_buffer.buffer;
1490 return trace_buffer_lock_reserve(*current_rb,
1491 type, len, flags, pc);
1493 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1495 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1496 struct ring_buffer_event *event,
1497 unsigned long flags, int pc)
1499 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1501 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1503 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1504 struct ring_buffer_event *event,
1505 unsigned long flags, int pc,
1506 struct pt_regs *regs)
1508 __buffer_unlock_commit(buffer, event);
1510 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1511 ftrace_trace_userstack(buffer, flags, pc);
1513 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1515 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1516 struct ring_buffer_event *event)
1518 ring_buffer_discard_commit(buffer, event);
1520 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1523 trace_function(struct trace_array *tr,
1524 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1527 struct ftrace_event_call *call = &event_function;
1528 struct ring_buffer *buffer = tr->trace_buffer.buffer;
1529 struct ring_buffer_event *event;
1530 struct ftrace_entry *entry;
1532 /* If we are reading the ring buffer, don't trace */
1533 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1536 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1540 entry = ring_buffer_event_data(event);
1542 entry->parent_ip = parent_ip;
1544 if (!filter_check_discard(call, entry, buffer, event))
1545 __buffer_unlock_commit(buffer, event);
1549 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1550 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1553 if (likely(!atomic_read(&data->disabled)))
1554 trace_function(tr, ip, parent_ip, flags, pc);
1557 #ifdef CONFIG_STACKTRACE
1559 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1560 struct ftrace_stack {
1561 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1564 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1565 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1567 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1568 unsigned long flags,
1569 int skip, int pc, struct pt_regs *regs)
1571 struct ftrace_event_call *call = &event_kernel_stack;
1572 struct ring_buffer_event *event;
1573 struct stack_entry *entry;
1574 struct stack_trace trace;
1576 int size = FTRACE_STACK_ENTRIES;
1578 trace.nr_entries = 0;
1582 * Since events can happen in NMIs there's no safe way to
1583 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1584 * or NMI comes in, it will just have to use the default
1585 * FTRACE_STACK_SIZE.
1587 preempt_disable_notrace();
1589 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1591 * We don't need any atomic variables, just a barrier.
1592 * If an interrupt comes in, we don't care, because it would
1593 * have exited and put the counter back to what we want.
1594 * We just need a barrier to keep gcc from moving things
1598 if (use_stack == 1) {
1599 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1600 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1603 save_stack_trace_regs(regs, &trace);
1605 save_stack_trace(&trace);
1607 if (trace.nr_entries > size)
1608 size = trace.nr_entries;
1610 /* From now on, use_stack is a boolean */
1613 size *= sizeof(unsigned long);
1615 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1616 sizeof(*entry) + size, flags, pc);
1619 entry = ring_buffer_event_data(event);
1621 memset(&entry->caller, 0, size);
1624 memcpy(&entry->caller, trace.entries,
1625 trace.nr_entries * sizeof(unsigned long));
1627 trace.max_entries = FTRACE_STACK_ENTRIES;
1628 trace.entries = entry->caller;
1630 save_stack_trace_regs(regs, &trace);
1632 save_stack_trace(&trace);
1635 entry->size = trace.nr_entries;
1637 if (!filter_check_discard(call, entry, buffer, event))
1638 __buffer_unlock_commit(buffer, event);
1641 /* Again, don't let gcc optimize things here */
1643 __this_cpu_dec(ftrace_stack_reserve);
1644 preempt_enable_notrace();
1648 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1649 int skip, int pc, struct pt_regs *regs)
1651 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1654 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1657 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1660 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1663 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1666 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1669 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
1673 * trace_dump_stack - record a stack back trace in the trace buffer
1674 * @skip: Number of functions to skip (helper handlers)
1676 void trace_dump_stack(int skip)
1678 unsigned long flags;
1680 if (tracing_disabled || tracing_selftest_running)
1683 local_save_flags(flags);
1686 * Skip 3 more, seems to get us at the caller of
1690 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1691 flags, skip, preempt_count(), NULL);
1694 static DEFINE_PER_CPU(int, user_stack_count);
1697 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1699 struct ftrace_event_call *call = &event_user_stack;
1700 struct ring_buffer_event *event;
1701 struct userstack_entry *entry;
1702 struct stack_trace trace;
1704 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1708 * NMIs can not handle page faults, even with fix ups.
1709 * The save user stack can (and often does) fault.
1711 if (unlikely(in_nmi()))
1715 * prevent recursion, since the user stack tracing may
1716 * trigger other kernel events.
1719 if (__this_cpu_read(user_stack_count))
1722 __this_cpu_inc(user_stack_count);
1724 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1725 sizeof(*entry), flags, pc);
1727 goto out_drop_count;
1728 entry = ring_buffer_event_data(event);
1730 entry->tgid = current->tgid;
1731 memset(&entry->caller, 0, sizeof(entry->caller));
1733 trace.nr_entries = 0;
1734 trace.max_entries = FTRACE_STACK_ENTRIES;
1736 trace.entries = entry->caller;
1738 save_stack_trace_user(&trace);
1739 if (!filter_check_discard(call, entry, buffer, event))
1740 __buffer_unlock_commit(buffer, event);
1743 __this_cpu_dec(user_stack_count);
1749 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1751 ftrace_trace_userstack(tr, flags, preempt_count());
1755 #endif /* CONFIG_STACKTRACE */
1757 /* created for use with alloc_percpu */
1758 struct trace_buffer_struct {
1759 char buffer[TRACE_BUF_SIZE];
1762 static struct trace_buffer_struct *trace_percpu_buffer;
1763 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1764 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1765 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1768 * The buffer used is dependent on the context. There is a per cpu
1769 * buffer for normal context, softirq contex, hard irq context and
1770 * for NMI context. Thise allows for lockless recording.
1772 * Note, if the buffers failed to be allocated, then this returns NULL
1774 static char *get_trace_buf(void)
1776 struct trace_buffer_struct *percpu_buffer;
1779 * If we have allocated per cpu buffers, then we do not
1780 * need to do any locking.
1783 percpu_buffer = trace_percpu_nmi_buffer;
1785 percpu_buffer = trace_percpu_irq_buffer;
1786 else if (in_softirq())
1787 percpu_buffer = trace_percpu_sirq_buffer;
1789 percpu_buffer = trace_percpu_buffer;
1794 return this_cpu_ptr(&percpu_buffer->buffer[0]);
1797 static int alloc_percpu_trace_buffer(void)
1799 struct trace_buffer_struct *buffers;
1800 struct trace_buffer_struct *sirq_buffers;
1801 struct trace_buffer_struct *irq_buffers;
1802 struct trace_buffer_struct *nmi_buffers;
1804 buffers = alloc_percpu(struct trace_buffer_struct);
1808 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1812 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1816 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1820 trace_percpu_buffer = buffers;
1821 trace_percpu_sirq_buffer = sirq_buffers;
1822 trace_percpu_irq_buffer = irq_buffers;
1823 trace_percpu_nmi_buffer = nmi_buffers;
1828 free_percpu(irq_buffers);
1830 free_percpu(sirq_buffers);
1832 free_percpu(buffers);
1834 WARN(1, "Could not allocate percpu trace_printk buffer");
1838 static int buffers_allocated;
1840 void trace_printk_init_buffers(void)
1842 if (buffers_allocated)
1845 if (alloc_percpu_trace_buffer())
1848 pr_info("ftrace: Allocated trace_printk buffers\n");
1850 /* Expand the buffers to set size */
1851 tracing_update_buffers();
1853 buffers_allocated = 1;
1856 * trace_printk_init_buffers() can be called by modules.
1857 * If that happens, then we need to start cmdline recording
1858 * directly here. If the global_trace.buffer is already
1859 * allocated here, then this was called by module code.
1861 if (global_trace.trace_buffer.buffer)
1862 tracing_start_cmdline_record();
1865 void trace_printk_start_comm(void)
1867 /* Start tracing comms if trace printk is set */
1868 if (!buffers_allocated)
1870 tracing_start_cmdline_record();
1873 static void trace_printk_start_stop_comm(int enabled)
1875 if (!buffers_allocated)
1879 tracing_start_cmdline_record();
1881 tracing_stop_cmdline_record();
1885 * trace_vbprintk - write binary msg to tracing buffer
1888 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1890 struct ftrace_event_call *call = &event_bprint;
1891 struct ring_buffer_event *event;
1892 struct ring_buffer *buffer;
1893 struct trace_array *tr = &global_trace;
1894 struct bprint_entry *entry;
1895 unsigned long flags;
1897 int len = 0, size, pc;
1899 if (unlikely(tracing_selftest_running || tracing_disabled))
1902 /* Don't pollute graph traces with trace_vprintk internals */
1903 pause_graph_tracing();
1905 pc = preempt_count();
1906 preempt_disable_notrace();
1908 tbuffer = get_trace_buf();
1914 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1916 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1919 local_save_flags(flags);
1920 size = sizeof(*entry) + sizeof(u32) * len;
1921 buffer = tr->trace_buffer.buffer;
1922 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1926 entry = ring_buffer_event_data(event);
1930 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1931 if (!filter_check_discard(call, entry, buffer, event)) {
1932 __buffer_unlock_commit(buffer, event);
1933 ftrace_trace_stack(buffer, flags, 6, pc);
1937 preempt_enable_notrace();
1938 unpause_graph_tracing();
1942 EXPORT_SYMBOL_GPL(trace_vbprintk);
1945 __trace_array_vprintk(struct ring_buffer *buffer,
1946 unsigned long ip, const char *fmt, va_list args)
1948 struct ftrace_event_call *call = &event_print;
1949 struct ring_buffer_event *event;
1950 int len = 0, size, pc;
1951 struct print_entry *entry;
1952 unsigned long flags;
1955 if (tracing_disabled || tracing_selftest_running)
1958 /* Don't pollute graph traces with trace_vprintk internals */
1959 pause_graph_tracing();
1961 pc = preempt_count();
1962 preempt_disable_notrace();
1965 tbuffer = get_trace_buf();
1971 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1972 if (len > TRACE_BUF_SIZE)
1975 local_save_flags(flags);
1976 size = sizeof(*entry) + len + 1;
1977 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1981 entry = ring_buffer_event_data(event);
1984 memcpy(&entry->buf, tbuffer, len);
1985 entry->buf[len] = '\0';
1986 if (!filter_check_discard(call, entry, buffer, event)) {
1987 __buffer_unlock_commit(buffer, event);
1988 ftrace_trace_stack(buffer, flags, 6, pc);
1991 preempt_enable_notrace();
1992 unpause_graph_tracing();
1997 int trace_array_vprintk(struct trace_array *tr,
1998 unsigned long ip, const char *fmt, va_list args)
2000 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2003 int trace_array_printk(struct trace_array *tr,
2004 unsigned long ip, const char *fmt, ...)
2009 if (!(trace_flags & TRACE_ITER_PRINTK))
2013 ret = trace_array_vprintk(tr, ip, fmt, ap);
2018 int trace_array_printk_buf(struct ring_buffer *buffer,
2019 unsigned long ip, const char *fmt, ...)
2024 if (!(trace_flags & TRACE_ITER_PRINTK))
2028 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2033 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2035 return trace_array_vprintk(&global_trace, ip, fmt, args);
2037 EXPORT_SYMBOL_GPL(trace_vprintk);
2039 static void trace_iterator_increment(struct trace_iterator *iter)
2041 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2045 ring_buffer_read(buf_iter, NULL);
2048 static struct trace_entry *
2049 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2050 unsigned long *lost_events)
2052 struct ring_buffer_event *event;
2053 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
2056 event = ring_buffer_iter_peek(buf_iter, ts);
2058 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
2062 iter->ent_size = ring_buffer_event_length(event);
2063 return ring_buffer_event_data(event);
2069 static struct trace_entry *
2070 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2071 unsigned long *missing_events, u64 *ent_ts)
2073 struct ring_buffer *buffer = iter->trace_buffer->buffer;
2074 struct trace_entry *ent, *next = NULL;
2075 unsigned long lost_events = 0, next_lost = 0;
2076 int cpu_file = iter->cpu_file;
2077 u64 next_ts = 0, ts;
2083 * If we are in a per_cpu trace file, don't bother by iterating over
2084 * all cpu and peek directly.
2086 if (cpu_file > RING_BUFFER_ALL_CPUS) {
2087 if (ring_buffer_empty_cpu(buffer, cpu_file))
2089 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
2091 *ent_cpu = cpu_file;
2096 for_each_tracing_cpu(cpu) {
2098 if (ring_buffer_empty_cpu(buffer, cpu))
2101 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
2104 * Pick the entry with the smallest timestamp:
2106 if (ent && (!next || ts < next_ts)) {
2110 next_lost = lost_events;
2111 next_size = iter->ent_size;
2115 iter->ent_size = next_size;
2118 *ent_cpu = next_cpu;
2124 *missing_events = next_lost;
2129 /* Find the next real entry, without updating the iterator itself */
2130 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2131 int *ent_cpu, u64 *ent_ts)
2133 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
2136 /* Find the next real entry, and increment the iterator to the next entry */
2137 void *trace_find_next_entry_inc(struct trace_iterator *iter)
2139 iter->ent = __find_next_entry(iter, &iter->cpu,
2140 &iter->lost_events, &iter->ts);
2143 trace_iterator_increment(iter);
2145 return iter->ent ? iter : NULL;
2148 static void trace_consume(struct trace_iterator *iter)
2150 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
2151 &iter->lost_events);
2154 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
2156 struct trace_iterator *iter = m->private;
2160 WARN_ON_ONCE(iter->leftover);
2164 /* can't go backwards */
2169 ent = trace_find_next_entry_inc(iter);
2173 while (ent && iter->idx < i)
2174 ent = trace_find_next_entry_inc(iter);
2181 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2183 struct ring_buffer_event *event;
2184 struct ring_buffer_iter *buf_iter;
2185 unsigned long entries = 0;
2188 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2190 buf_iter = trace_buffer_iter(iter, cpu);
2194 ring_buffer_iter_reset(buf_iter);
2197 * We could have the case with the max latency tracers
2198 * that a reset never took place on a cpu. This is evident
2199 * by the timestamp being before the start of the buffer.
2201 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
2202 if (ts >= iter->trace_buffer->time_start)
2205 ring_buffer_read(buf_iter, NULL);
2208 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2212 * The current tracer is copied to avoid a global locking
2215 static void *s_start(struct seq_file *m, loff_t *pos)
2217 struct trace_iterator *iter = m->private;
2218 struct trace_array *tr = iter->tr;
2219 int cpu_file = iter->cpu_file;
2225 * copy the tracer to avoid using a global lock all around.
2226 * iter->trace is a copy of current_trace, the pointer to the
2227 * name may be used instead of a strcmp(), as iter->trace->name
2228 * will point to the same string as current_trace->name.
2230 mutex_lock(&trace_types_lock);
2231 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2232 *iter->trace = *tr->current_trace;
2233 mutex_unlock(&trace_types_lock);
2235 #ifdef CONFIG_TRACER_MAX_TRACE
2236 if (iter->snapshot && iter->trace->use_max_tr)
2237 return ERR_PTR(-EBUSY);
2240 if (!iter->snapshot)
2241 atomic_inc(&trace_record_cmdline_disabled);
2243 if (*pos != iter->pos) {
2248 if (cpu_file == RING_BUFFER_ALL_CPUS) {
2249 for_each_tracing_cpu(cpu)
2250 tracing_iter_reset(iter, cpu);
2252 tracing_iter_reset(iter, cpu_file);
2255 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2260 * If we overflowed the seq_file before, then we want
2261 * to just reuse the trace_seq buffer again.
2267 p = s_next(m, p, &l);
2271 trace_event_read_lock();
2272 trace_access_lock(cpu_file);
2276 static void s_stop(struct seq_file *m, void *p)
2278 struct trace_iterator *iter = m->private;
2280 #ifdef CONFIG_TRACER_MAX_TRACE
2281 if (iter->snapshot && iter->trace->use_max_tr)
2285 if (!iter->snapshot)
2286 atomic_dec(&trace_record_cmdline_disabled);
2288 trace_access_unlock(iter->cpu_file);
2289 trace_event_read_unlock();
2293 get_total_entries(struct trace_buffer *buf,
2294 unsigned long *total, unsigned long *entries)
2296 unsigned long count;
2302 for_each_tracing_cpu(cpu) {
2303 count = ring_buffer_entries_cpu(buf->buffer, cpu);
2305 * If this buffer has skipped entries, then we hold all
2306 * entries for the trace and we need to ignore the
2307 * ones before the time stamp.
2309 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2310 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
2311 /* total is the same as the entries */
2315 ring_buffer_overrun_cpu(buf->buffer, cpu);
2320 static void print_lat_help_header(struct seq_file *m)
2322 seq_puts(m, "# _------=> CPU# \n");
2323 seq_puts(m, "# / _-----=> irqs-off \n");
2324 seq_puts(m, "# | / _----=> need-resched \n");
2325 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2326 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2327 seq_puts(m, "# |||| / delay \n");
2328 seq_puts(m, "# cmd pid ||||| time | caller \n");
2329 seq_puts(m, "# \\ / ||||| \\ | / \n");
2332 static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
2334 unsigned long total;
2335 unsigned long entries;
2337 get_total_entries(buf, &total, &entries);
2338 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2339 entries, total, num_online_cpus());
2343 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
2345 print_event_info(buf, m);
2346 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2347 seq_puts(m, "# | | | | |\n");
2350 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
2352 print_event_info(buf, m);
2353 seq_puts(m, "# _-----=> irqs-off\n");
2354 seq_puts(m, "# / _----=> need-resched\n");
2355 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2356 seq_puts(m, "# || / _--=> preempt-depth\n");
2357 seq_puts(m, "# ||| / delay\n");
2358 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2359 seq_puts(m, "# | | | |||| | |\n");
2363 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2365 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2366 struct trace_buffer *buf = iter->trace_buffer;
2367 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2368 struct tracer *type = iter->trace;
2369 unsigned long entries;
2370 unsigned long total;
2371 const char *name = "preemption";
2375 get_total_entries(buf, &total, &entries);
2377 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2379 seq_puts(m, "# -----------------------------------"
2380 "---------------------------------\n");
2381 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2382 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2383 nsecs_to_usecs(data->saved_latency),
2387 #if defined(CONFIG_PREEMPT_NONE)
2389 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2391 #elif defined(CONFIG_PREEMPT)
2396 /* These are reserved for later use */
2399 seq_printf(m, " #P:%d)\n", num_online_cpus());
2403 seq_puts(m, "# -----------------\n");
2404 seq_printf(m, "# | task: %.16s-%d "
2405 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2406 data->comm, data->pid,
2407 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2408 data->policy, data->rt_priority);
2409 seq_puts(m, "# -----------------\n");
2411 if (data->critical_start) {
2412 seq_puts(m, "# => started at: ");
2413 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2414 trace_print_seq(m, &iter->seq);
2415 seq_puts(m, "\n# => ended at: ");
2416 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2417 trace_print_seq(m, &iter->seq);
2418 seq_puts(m, "\n#\n");
2424 static void test_cpu_buff_start(struct trace_iterator *iter)
2426 struct trace_seq *s = &iter->seq;
2428 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2431 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2434 if (cpumask_test_cpu(iter->cpu, iter->started))
2437 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2440 cpumask_set_cpu(iter->cpu, iter->started);
2442 /* Don't print started cpu buffer for the first entry of the trace */
2444 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2448 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2450 struct trace_seq *s = &iter->seq;
2451 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2452 struct trace_entry *entry;
2453 struct trace_event *event;
2457 test_cpu_buff_start(iter);
2459 event = ftrace_find_event(entry->type);
2461 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2462 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2463 if (!trace_print_lat_context(iter))
2466 if (!trace_print_context(iter))
2472 return event->funcs->trace(iter, sym_flags, event);
2474 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2477 return TRACE_TYPE_HANDLED;
2479 return TRACE_TYPE_PARTIAL_LINE;
2482 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2484 struct trace_seq *s = &iter->seq;
2485 struct trace_entry *entry;
2486 struct trace_event *event;
2490 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2491 if (!trace_seq_printf(s, "%d %d %llu ",
2492 entry->pid, iter->cpu, iter->ts))
2496 event = ftrace_find_event(entry->type);
2498 return event->funcs->raw(iter, 0, event);
2500 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2503 return TRACE_TYPE_HANDLED;
2505 return TRACE_TYPE_PARTIAL_LINE;
2508 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2510 struct trace_seq *s = &iter->seq;
2511 unsigned char newline = '\n';
2512 struct trace_entry *entry;
2513 struct trace_event *event;
2517 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2518 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2519 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2520 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2523 event = ftrace_find_event(entry->type);
2525 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2526 if (ret != TRACE_TYPE_HANDLED)
2530 SEQ_PUT_FIELD_RET(s, newline);
2532 return TRACE_TYPE_HANDLED;
2535 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2537 struct trace_seq *s = &iter->seq;
2538 struct trace_entry *entry;
2539 struct trace_event *event;
2543 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2544 SEQ_PUT_FIELD_RET(s, entry->pid);
2545 SEQ_PUT_FIELD_RET(s, iter->cpu);
2546 SEQ_PUT_FIELD_RET(s, iter->ts);
2549 event = ftrace_find_event(entry->type);
2550 return event ? event->funcs->binary(iter, 0, event) :
2554 int trace_empty(struct trace_iterator *iter)
2556 struct ring_buffer_iter *buf_iter;
2559 /* If we are looking at one CPU buffer, only check that one */
2560 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2561 cpu = iter->cpu_file;
2562 buf_iter = trace_buffer_iter(iter, cpu);
2564 if (!ring_buffer_iter_empty(buf_iter))
2567 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2573 for_each_tracing_cpu(cpu) {
2574 buf_iter = trace_buffer_iter(iter, cpu);
2576 if (!ring_buffer_iter_empty(buf_iter))
2579 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2587 /* Called with trace_event_read_lock() held. */
2588 enum print_line_t print_trace_line(struct trace_iterator *iter)
2590 enum print_line_t ret;
2592 if (iter->lost_events &&
2593 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2594 iter->cpu, iter->lost_events))
2595 return TRACE_TYPE_PARTIAL_LINE;
2597 if (iter->trace && iter->trace->print_line) {
2598 ret = iter->trace->print_line(iter);
2599 if (ret != TRACE_TYPE_UNHANDLED)
2603 if (iter->ent->type == TRACE_BPUTS &&
2604 trace_flags & TRACE_ITER_PRINTK &&
2605 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2606 return trace_print_bputs_msg_only(iter);
2608 if (iter->ent->type == TRACE_BPRINT &&
2609 trace_flags & TRACE_ITER_PRINTK &&
2610 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2611 return trace_print_bprintk_msg_only(iter);
2613 if (iter->ent->type == TRACE_PRINT &&
2614 trace_flags & TRACE_ITER_PRINTK &&
2615 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2616 return trace_print_printk_msg_only(iter);
2618 if (trace_flags & TRACE_ITER_BIN)
2619 return print_bin_fmt(iter);
2621 if (trace_flags & TRACE_ITER_HEX)
2622 return print_hex_fmt(iter);
2624 if (trace_flags & TRACE_ITER_RAW)
2625 return print_raw_fmt(iter);
2627 return print_trace_fmt(iter);
2630 void trace_latency_header(struct seq_file *m)
2632 struct trace_iterator *iter = m->private;
2634 /* print nothing if the buffers are empty */
2635 if (trace_empty(iter))
2638 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2639 print_trace_header(m, iter);
2641 if (!(trace_flags & TRACE_ITER_VERBOSE))
2642 print_lat_help_header(m);
2645 void trace_default_header(struct seq_file *m)
2647 struct trace_iterator *iter = m->private;
2649 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2652 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2653 /* print nothing if the buffers are empty */
2654 if (trace_empty(iter))
2656 print_trace_header(m, iter);
2657 if (!(trace_flags & TRACE_ITER_VERBOSE))
2658 print_lat_help_header(m);
2660 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2661 if (trace_flags & TRACE_ITER_IRQ_INFO)
2662 print_func_help_header_irq(iter->trace_buffer, m);
2664 print_func_help_header(iter->trace_buffer, m);
2669 static void test_ftrace_alive(struct seq_file *m)
2671 if (!ftrace_is_dead())
2673 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2674 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2677 #ifdef CONFIG_TRACER_MAX_TRACE
2678 static void show_snapshot_main_help(struct seq_file *m)
2680 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2681 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2682 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2683 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2684 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2685 seq_printf(m, "# is not a '0' or '1')\n");
2688 static void show_snapshot_percpu_help(struct seq_file *m)
2690 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2691 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2692 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2693 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2695 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2696 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2698 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2699 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2700 seq_printf(m, "# is not a '0' or '1')\n");
2703 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2705 if (iter->tr->allocated_snapshot)
2706 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2708 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2710 seq_printf(m, "# Snapshot commands:\n");
2711 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2712 show_snapshot_main_help(m);
2714 show_snapshot_percpu_help(m);
2717 /* Should never be called */
2718 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2721 static int s_show(struct seq_file *m, void *v)
2723 struct trace_iterator *iter = v;
2726 if (iter->ent == NULL) {
2728 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2730 test_ftrace_alive(m);
2732 if (iter->snapshot && trace_empty(iter))
2733 print_snapshot_help(m, iter);
2734 else if (iter->trace && iter->trace->print_header)
2735 iter->trace->print_header(m);
2737 trace_default_header(m);
2739 } else if (iter->leftover) {
2741 * If we filled the seq_file buffer earlier, we
2742 * want to just show it now.
2744 ret = trace_print_seq(m, &iter->seq);
2746 /* ret should this time be zero, but you never know */
2747 iter->leftover = ret;
2750 print_trace_line(iter);
2751 ret = trace_print_seq(m, &iter->seq);
2753 * If we overflow the seq_file buffer, then it will
2754 * ask us for this data again at start up.
2756 * ret is 0 if seq_file write succeeded.
2759 iter->leftover = ret;
2765 static const struct seq_operations tracer_seq_ops = {
2772 static struct trace_iterator *
2773 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2775 struct trace_cpu *tc = inode->i_private;
2776 struct trace_array *tr = tc->tr;
2777 struct trace_iterator *iter;
2780 if (tracing_disabled)
2781 return ERR_PTR(-ENODEV);
2783 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2785 return ERR_PTR(-ENOMEM);
2787 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2789 if (!iter->buffer_iter)
2793 * We make a copy of the current tracer to avoid concurrent
2794 * changes on it while we are reading.
2796 mutex_lock(&trace_types_lock);
2797 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2801 *iter->trace = *tr->current_trace;
2803 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2808 #ifdef CONFIG_TRACER_MAX_TRACE
2809 /* Currently only the top directory has a snapshot */
2810 if (tr->current_trace->print_max || snapshot)
2811 iter->trace_buffer = &tr->max_buffer;
2814 iter->trace_buffer = &tr->trace_buffer;
2815 iter->snapshot = snapshot;
2817 mutex_init(&iter->mutex);
2818 iter->cpu_file = tc->cpu;
2820 /* Notify the tracer early; before we stop tracing. */
2821 if (iter->trace && iter->trace->open)
2822 iter->trace->open(iter);
2824 /* Annotate start of buffers if we had overruns */
2825 if (ring_buffer_overruns(iter->trace_buffer->buffer))
2826 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2828 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2829 if (trace_clocks[trace_clock_id].in_ns)
2830 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2832 /* stop the trace while dumping if we are not opening "snapshot" */
2833 if (!iter->snapshot)
2834 tracing_stop_tr(tr);
2836 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
2837 for_each_tracing_cpu(cpu) {
2838 iter->buffer_iter[cpu] =
2839 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2841 ring_buffer_read_prepare_sync();
2842 for_each_tracing_cpu(cpu) {
2843 ring_buffer_read_start(iter->buffer_iter[cpu]);
2844 tracing_iter_reset(iter, cpu);
2847 cpu = iter->cpu_file;
2848 iter->buffer_iter[cpu] =
2849 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2850 ring_buffer_read_prepare_sync();
2851 ring_buffer_read_start(iter->buffer_iter[cpu]);
2852 tracing_iter_reset(iter, cpu);
2857 mutex_unlock(&trace_types_lock);
2862 mutex_unlock(&trace_types_lock);
2864 kfree(iter->buffer_iter);
2866 seq_release_private(inode, file);
2867 return ERR_PTR(-ENOMEM);
2870 int tracing_open_generic(struct inode *inode, struct file *filp)
2872 if (tracing_disabled)
2875 filp->private_data = inode->i_private;
2879 static int tracing_release(struct inode *inode, struct file *file)
2881 struct seq_file *m = file->private_data;
2882 struct trace_iterator *iter;
2883 struct trace_array *tr;
2886 if (!(file->f_mode & FMODE_READ))
2892 mutex_lock(&trace_types_lock);
2897 for_each_tracing_cpu(cpu) {
2898 if (iter->buffer_iter[cpu])
2899 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2902 if (iter->trace && iter->trace->close)
2903 iter->trace->close(iter);
2905 if (!iter->snapshot)
2906 /* reenable tracing if it was previously enabled */
2907 tracing_start_tr(tr);
2908 mutex_unlock(&trace_types_lock);
2910 mutex_destroy(&iter->mutex);
2911 free_cpumask_var(iter->started);
2913 kfree(iter->buffer_iter);
2914 seq_release_private(inode, file);
2918 static int tracing_open(struct inode *inode, struct file *file)
2920 struct trace_iterator *iter;
2923 /* If this file was open for write, then erase contents */
2924 if ((file->f_mode & FMODE_WRITE) &&
2925 (file->f_flags & O_TRUNC)) {
2926 struct trace_cpu *tc = inode->i_private;
2927 struct trace_array *tr = tc->tr;
2929 if (tc->cpu == RING_BUFFER_ALL_CPUS)
2930 tracing_reset_online_cpus(&tr->trace_buffer);
2932 tracing_reset(&tr->trace_buffer, tc->cpu);
2935 if (file->f_mode & FMODE_READ) {
2936 iter = __tracing_open(inode, file, false);
2938 ret = PTR_ERR(iter);
2939 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2940 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2946 t_next(struct seq_file *m, void *v, loff_t *pos)
2948 struct tracer *t = v;
2958 static void *t_start(struct seq_file *m, loff_t *pos)
2963 mutex_lock(&trace_types_lock);
2964 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2970 static void t_stop(struct seq_file *m, void *p)
2972 mutex_unlock(&trace_types_lock);
2975 static int t_show(struct seq_file *m, void *v)
2977 struct tracer *t = v;
2982 seq_printf(m, "%s", t->name);
2991 static const struct seq_operations show_traces_seq_ops = {
2998 static int show_traces_open(struct inode *inode, struct file *file)
3000 if (tracing_disabled)
3003 return seq_open(file, &show_traces_seq_ops);
3007 tracing_write_stub(struct file *filp, const char __user *ubuf,
3008 size_t count, loff_t *ppos)
3013 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
3015 if (file->f_mode & FMODE_READ)
3016 return seq_lseek(file, offset, origin);
3021 static const struct file_operations tracing_fops = {
3022 .open = tracing_open,
3024 .write = tracing_write_stub,
3025 .llseek = tracing_seek,
3026 .release = tracing_release,
3029 static const struct file_operations show_traces_fops = {
3030 .open = show_traces_open,
3032 .release = seq_release,
3033 .llseek = seq_lseek,
3037 * Only trace on a CPU if the bitmask is set:
3039 static cpumask_var_t tracing_cpumask;
3042 * The tracer itself will not take this lock, but still we want
3043 * to provide a consistent cpumask to user-space:
3045 static DEFINE_MUTEX(tracing_cpumask_update_lock);
3048 * Temporary storage for the character representation of the
3049 * CPU bitmask (and one more byte for the newline):
3051 static char mask_str[NR_CPUS + 1];
3054 tracing_cpumask_read(struct file *filp, char __user *ubuf,
3055 size_t count, loff_t *ppos)
3059 mutex_lock(&tracing_cpumask_update_lock);
3061 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
3062 if (count - len < 2) {
3066 len += sprintf(mask_str + len, "\n");
3067 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3070 mutex_unlock(&tracing_cpumask_update_lock);
3076 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3077 size_t count, loff_t *ppos)
3079 struct trace_array *tr = filp->private_data;
3080 cpumask_var_t tracing_cpumask_new;
3083 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3086 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
3090 mutex_lock(&tracing_cpumask_update_lock);
3092 local_irq_disable();
3093 arch_spin_lock(&ftrace_max_lock);
3094 for_each_tracing_cpu(cpu) {
3096 * Increase/decrease the disabled counter if we are
3097 * about to flip a bit in the cpumask:
3099 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
3100 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3101 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3102 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
3104 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
3105 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3106 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3107 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
3110 arch_spin_unlock(&ftrace_max_lock);
3113 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
3115 mutex_unlock(&tracing_cpumask_update_lock);
3116 free_cpumask_var(tracing_cpumask_new);
3121 free_cpumask_var(tracing_cpumask_new);
3126 static const struct file_operations tracing_cpumask_fops = {
3127 .open = tracing_open_generic,
3128 .read = tracing_cpumask_read,
3129 .write = tracing_cpumask_write,
3130 .llseek = generic_file_llseek,
3133 static int tracing_trace_options_show(struct seq_file *m, void *v)
3135 struct tracer_opt *trace_opts;
3136 struct trace_array *tr = m->private;
3140 mutex_lock(&trace_types_lock);
3141 tracer_flags = tr->current_trace->flags->val;
3142 trace_opts = tr->current_trace->flags->opts;
3144 for (i = 0; trace_options[i]; i++) {
3145 if (trace_flags & (1 << i))
3146 seq_printf(m, "%s\n", trace_options[i]);
3148 seq_printf(m, "no%s\n", trace_options[i]);
3151 for (i = 0; trace_opts[i].name; i++) {
3152 if (tracer_flags & trace_opts[i].bit)
3153 seq_printf(m, "%s\n", trace_opts[i].name);
3155 seq_printf(m, "no%s\n", trace_opts[i].name);
3157 mutex_unlock(&trace_types_lock);
3162 static int __set_tracer_option(struct tracer *trace,
3163 struct tracer_flags *tracer_flags,
3164 struct tracer_opt *opts, int neg)
3168 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
3173 tracer_flags->val &= ~opts->bit;
3175 tracer_flags->val |= opts->bit;
3179 /* Try to assign a tracer specific option */
3180 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3182 struct tracer_flags *tracer_flags = trace->flags;
3183 struct tracer_opt *opts = NULL;
3186 for (i = 0; tracer_flags->opts[i].name; i++) {
3187 opts = &tracer_flags->opts[i];
3189 if (strcmp(cmp, opts->name) == 0)
3190 return __set_tracer_option(trace, trace->flags,
3197 /* Some tracers require overwrite to stay enabled */
3198 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3200 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3206 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
3208 /* do nothing if flag is already set */
3209 if (!!(trace_flags & mask) == !!enabled)
3212 /* Give the tracer a chance to approve the change */
3213 if (tr->current_trace->flag_changed)
3214 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
3218 trace_flags |= mask;
3220 trace_flags &= ~mask;
3222 if (mask == TRACE_ITER_RECORD_CMD)
3223 trace_event_enable_cmd_record(enabled);
3225 if (mask == TRACE_ITER_OVERWRITE) {
3226 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
3227 #ifdef CONFIG_TRACER_MAX_TRACE
3228 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
3232 if (mask == TRACE_ITER_PRINTK)
3233 trace_printk_start_stop_comm(enabled);
3238 static int trace_set_options(struct trace_array *tr, char *option)
3245 cmp = strstrip(option);
3247 if (strncmp(cmp, "no", 2) == 0) {
3252 mutex_lock(&trace_types_lock);
3254 for (i = 0; trace_options[i]; i++) {
3255 if (strcmp(cmp, trace_options[i]) == 0) {
3256 ret = set_tracer_flag(tr, 1 << i, !neg);
3261 /* If no option could be set, test the specific tracer options */
3262 if (!trace_options[i])
3263 ret = set_tracer_option(tr->current_trace, cmp, neg);
3265 mutex_unlock(&trace_types_lock);
3271 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3272 size_t cnt, loff_t *ppos)
3274 struct seq_file *m = filp->private_data;
3275 struct trace_array *tr = m->private;
3279 if (cnt >= sizeof(buf))
3282 if (copy_from_user(&buf, ubuf, cnt))
3287 ret = trace_set_options(tr, buf);
3296 static int tracing_trace_options_open(struct inode *inode, struct file *file)
3298 if (tracing_disabled)
3301 return single_open(file, tracing_trace_options_show, inode->i_private);
3304 static const struct file_operations tracing_iter_fops = {
3305 .open = tracing_trace_options_open,
3307 .llseek = seq_lseek,
3308 .release = single_release,
3309 .write = tracing_trace_options_write,
3312 static const char readme_msg[] =
3313 "tracing mini-HOWTO:\n\n"
3314 "# echo 0 > tracing_on : quick way to disable tracing\n"
3315 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3316 " Important files:\n"
3317 " trace\t\t\t- The static contents of the buffer\n"
3318 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3319 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3320 " current_tracer\t- function and latency tracers\n"
3321 " available_tracers\t- list of configured tracers for current_tracer\n"
3322 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3323 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3324 " trace_clock\t\t-change the clock used to order events\n"
3325 " local: Per cpu clock but may not be synced across CPUs\n"
3326 " global: Synced across CPUs but slows tracing down.\n"
3327 " counter: Not a clock, but just an increment\n"
3328 " uptime: Jiffy counter from time of boot\n"
3329 " perf: Same clock that perf events use\n"
3330 #ifdef CONFIG_X86_64
3331 " x86-tsc: TSC cycle counter\n"
3333 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3334 " tracing_cpumask\t- Limit which CPUs to trace\n"
3335 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3336 "\t\t\t Remove sub-buffer with rmdir\n"
3337 " trace_options\t\t- Set format or modify how tracing happens\n"
3338 "\t\t\t Disable an option by adding a suffix 'no' to the option name\n"
3339 #ifdef CONFIG_DYNAMIC_FTRACE
3340 "\n available_filter_functions - list of functions that can be filtered on\n"
3341 " set_ftrace_filter\t- echo function name in here to only trace these functions\n"
3342 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3343 " modules: Can select a group via module\n"
3344 " Format: :mod:<module-name>\n"
3345 " example: echo :mod:ext3 > set_ftrace_filter\n"
3346 " triggers: a command to perform when function is hit\n"
3347 " Format: <function>:<trigger>[:count]\n"
3348 " trigger: traceon, traceoff\n"
3349 " enable_event:<system>:<event>\n"
3350 " disable_event:<system>:<event>\n"
3351 #ifdef CONFIG_STACKTRACE
3354 #ifdef CONFIG_TRACER_SNAPSHOT
3357 " example: echo do_fault:traceoff > set_ftrace_filter\n"
3358 " echo do_trap:traceoff:3 > set_ftrace_filter\n"
3359 " The first one will disable tracing every time do_fault is hit\n"
3360 " The second will disable tracing at most 3 times when do_trap is hit\n"
3361 " The first time do trap is hit and it disables tracing, the counter\n"
3362 " will decrement to 2. If tracing is already disabled, the counter\n"
3363 " will not decrement. It only decrements when the trigger did work\n"
3364 " To remove trigger without count:\n"
3365 " echo '!<function>:<trigger> > set_ftrace_filter\n"
3366 " To remove trigger with a count:\n"
3367 " echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
3368 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
3369 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3370 " modules: Can select a group via module command :mod:\n"
3371 " Does not accept triggers\n"
3372 #endif /* CONFIG_DYNAMIC_FTRACE */
3373 #ifdef CONFIG_FUNCTION_TRACER
3374 " set_ftrace_pid\t- Write pid(s) to only function trace those pids (function)\n"
3376 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3377 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3378 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3380 #ifdef CONFIG_TRACER_SNAPSHOT
3381 "\n snapshot\t\t- Like 'trace' but shows the content of the static snapshot buffer\n"
3382 "\t\t\t Read the contents for more information\n"
3384 #ifdef CONFIG_STACKTRACE
3385 " stack_trace\t\t- Shows the max stack trace when active\n"
3386 " stack_max_size\t- Shows current max stack size that was traced\n"
3387 "\t\t\t Write into this file to reset the max size (trigger a new trace)\n"
3388 #ifdef CONFIG_DYNAMIC_FTRACE
3389 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace traces\n"
3391 #endif /* CONFIG_STACKTRACE */
3395 tracing_readme_read(struct file *filp, char __user *ubuf,
3396 size_t cnt, loff_t *ppos)
3398 return simple_read_from_buffer(ubuf, cnt, ppos,
3399 readme_msg, strlen(readme_msg));
3402 static const struct file_operations tracing_readme_fops = {
3403 .open = tracing_open_generic,
3404 .read = tracing_readme_read,
3405 .llseek = generic_file_llseek,
3409 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3410 size_t cnt, loff_t *ppos)
3419 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3423 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3431 for (i = 0; i < SAVED_CMDLINES; i++) {
3434 pid = map_cmdline_to_pid[i];
3435 if (pid == -1 || pid == NO_CMDLINE_MAP)
3438 trace_find_cmdline(pid, buf_comm);
3439 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3444 len = simple_read_from_buffer(ubuf, cnt, ppos,
3453 static const struct file_operations tracing_saved_cmdlines_fops = {
3454 .open = tracing_open_generic,
3455 .read = tracing_saved_cmdlines_read,
3456 .llseek = generic_file_llseek,
3460 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3461 size_t cnt, loff_t *ppos)
3463 struct trace_array *tr = filp->private_data;
3464 char buf[MAX_TRACER_SIZE+2];
3467 mutex_lock(&trace_types_lock);
3468 r = sprintf(buf, "%s\n", tr->current_trace->name);
3469 mutex_unlock(&trace_types_lock);
3471 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3474 int tracer_init(struct tracer *t, struct trace_array *tr)
3476 tracing_reset_online_cpus(&tr->trace_buffer);
3480 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
3484 for_each_tracing_cpu(cpu)
3485 per_cpu_ptr(buf->data, cpu)->entries = val;
3488 #ifdef CONFIG_TRACER_MAX_TRACE
3489 /* resize @tr's buffer to the size of @size_tr's entries */
3490 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3491 struct trace_buffer *size_buf, int cpu_id)
3495 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3496 for_each_tracing_cpu(cpu) {
3497 ret = ring_buffer_resize(trace_buf->buffer,
3498 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
3501 per_cpu_ptr(trace_buf->data, cpu)->entries =
3502 per_cpu_ptr(size_buf->data, cpu)->entries;
3505 ret = ring_buffer_resize(trace_buf->buffer,
3506 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
3508 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3509 per_cpu_ptr(size_buf->data, cpu_id)->entries;
3514 #endif /* CONFIG_TRACER_MAX_TRACE */
3516 static int __tracing_resize_ring_buffer(struct trace_array *tr,
3517 unsigned long size, int cpu)
3522 * If kernel or user changes the size of the ring buffer
3523 * we use the size that was given, and we can forget about
3524 * expanding it later.
3526 ring_buffer_expanded = true;
3528 /* May be called before buffers are initialized */
3529 if (!tr->trace_buffer.buffer)
3532 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
3536 #ifdef CONFIG_TRACER_MAX_TRACE
3537 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3538 !tr->current_trace->use_max_tr)
3541 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
3543 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3544 &tr->trace_buffer, cpu);
3547 * AARGH! We are left with different
3548 * size max buffer!!!!
3549 * The max buffer is our "snapshot" buffer.
3550 * When a tracer needs a snapshot (one of the
3551 * latency tracers), it swaps the max buffer
3552 * with the saved snap shot. We succeeded to
3553 * update the size of the main buffer, but failed to
3554 * update the size of the max buffer. But when we tried
3555 * to reset the main buffer to the original size, we
3556 * failed there too. This is very unlikely to
3557 * happen, but if it does, warn and kill all
3561 tracing_disabled = 1;
3566 if (cpu == RING_BUFFER_ALL_CPUS)
3567 set_buffer_entries(&tr->max_buffer, size);
3569 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
3572 #endif /* CONFIG_TRACER_MAX_TRACE */
3574 if (cpu == RING_BUFFER_ALL_CPUS)
3575 set_buffer_entries(&tr->trace_buffer, size);
3577 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
3582 static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3583 unsigned long size, int cpu_id)
3587 mutex_lock(&trace_types_lock);
3589 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3590 /* make sure, this cpu is enabled in the mask */
3591 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3597 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
3602 mutex_unlock(&trace_types_lock);
3609 * tracing_update_buffers - used by tracing facility to expand ring buffers
3611 * To save on memory when the tracing is never used on a system with it
3612 * configured in. The ring buffers are set to a minimum size. But once
3613 * a user starts to use the tracing facility, then they need to grow
3614 * to their default size.
3616 * This function is to be called when a tracer is about to be used.
3618 int tracing_update_buffers(void)
3622 mutex_lock(&trace_types_lock);
3623 if (!ring_buffer_expanded)
3624 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
3625 RING_BUFFER_ALL_CPUS);
3626 mutex_unlock(&trace_types_lock);
3631 struct trace_option_dentry;
3633 static struct trace_option_dentry *
3634 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
3637 destroy_trace_option_files(struct trace_option_dentry *topts);
3639 static int tracing_set_tracer(const char *buf)
3641 static struct trace_option_dentry *topts;
3642 struct trace_array *tr = &global_trace;
3644 #ifdef CONFIG_TRACER_MAX_TRACE
3649 mutex_lock(&trace_types_lock);
3651 if (!ring_buffer_expanded) {
3652 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
3653 RING_BUFFER_ALL_CPUS);
3659 for (t = trace_types; t; t = t->next) {
3660 if (strcmp(t->name, buf) == 0)
3667 if (t == tr->current_trace)
3670 trace_branch_disable();
3672 tr->current_trace->enabled = false;
3674 if (tr->current_trace->reset)
3675 tr->current_trace->reset(tr);
3677 /* Current trace needs to be nop_trace before synchronize_sched */
3678 tr->current_trace = &nop_trace;
3680 #ifdef CONFIG_TRACER_MAX_TRACE
3681 had_max_tr = tr->allocated_snapshot;
3683 if (had_max_tr && !t->use_max_tr) {
3685 * We need to make sure that the update_max_tr sees that
3686 * current_trace changed to nop_trace to keep it from
3687 * swapping the buffers after we resize it.
3688 * The update_max_tr is called from interrupts disabled
3689 * so a synchronized_sched() is sufficient.
3691 synchronize_sched();
3695 destroy_trace_option_files(topts);
3697 topts = create_trace_option_files(tr, t);
3699 #ifdef CONFIG_TRACER_MAX_TRACE
3700 if (t->use_max_tr && !had_max_tr) {
3701 ret = alloc_snapshot(tr);
3708 ret = tracer_init(t, tr);
3713 tr->current_trace = t;
3714 tr->current_trace->enabled = true;
3715 trace_branch_enable(tr);
3717 mutex_unlock(&trace_types_lock);
3723 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3724 size_t cnt, loff_t *ppos)
3726 char buf[MAX_TRACER_SIZE+1];
3733 if (cnt > MAX_TRACER_SIZE)
3734 cnt = MAX_TRACER_SIZE;
3736 if (copy_from_user(&buf, ubuf, cnt))
3741 /* strip ending whitespace. */
3742 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3745 err = tracing_set_tracer(buf);
3755 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3756 size_t cnt, loff_t *ppos)
3758 unsigned long *ptr = filp->private_data;
3762 r = snprintf(buf, sizeof(buf), "%ld\n",
3763 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3764 if (r > sizeof(buf))
3766 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3770 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3771 size_t cnt, loff_t *ppos)
3773 unsigned long *ptr = filp->private_data;
3777 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3786 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3788 struct trace_cpu *tc = inode->i_private;
3789 struct trace_array *tr = tc->tr;
3790 struct trace_iterator *iter;
3793 if (tracing_disabled)
3796 mutex_lock(&trace_types_lock);
3798 /* create a buffer to store the information to pass to userspace */
3799 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3806 * We make a copy of the current tracer to avoid concurrent
3807 * changes on it while we are reading.
3809 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3814 *iter->trace = *tr->current_trace;
3816 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3821 /* trace pipe does not show start of buffer */
3822 cpumask_setall(iter->started);
3824 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3825 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3827 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3828 if (trace_clocks[trace_clock_id].in_ns)
3829 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3831 iter->cpu_file = tc->cpu;
3833 iter->trace_buffer = &tc->tr->trace_buffer;
3834 mutex_init(&iter->mutex);
3835 filp->private_data = iter;
3837 if (iter->trace->pipe_open)
3838 iter->trace->pipe_open(iter);
3840 nonseekable_open(inode, filp);
3842 mutex_unlock(&trace_types_lock);
3848 mutex_unlock(&trace_types_lock);
3852 static int tracing_release_pipe(struct inode *inode, struct file *file)
3854 struct trace_iterator *iter = file->private_data;
3856 mutex_lock(&trace_types_lock);
3858 if (iter->trace->pipe_close)
3859 iter->trace->pipe_close(iter);
3861 mutex_unlock(&trace_types_lock);
3863 free_cpumask_var(iter->started);
3864 mutex_destroy(&iter->mutex);
3872 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
3874 /* Iterators are static, they should be filled or empty */
3875 if (trace_buffer_iter(iter, iter->cpu_file))
3876 return POLLIN | POLLRDNORM;
3878 if (trace_flags & TRACE_ITER_BLOCK)
3880 * Always select as readable when in blocking mode
3882 return POLLIN | POLLRDNORM;
3884 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
3889 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3891 struct trace_iterator *iter = filp->private_data;
3893 return trace_poll(iter, filp, poll_table);
3897 * This is a make-shift waitqueue.
3898 * A tracer might use this callback on some rare cases:
3900 * 1) the current tracer might hold the runqueue lock when it wakes up
3901 * a reader, hence a deadlock (sched, function, and function graph tracers)
3902 * 2) the function tracers, trace all functions, we don't want
3903 * the overhead of calling wake_up and friends
3904 * (and tracing them too)
3906 * Anyway, this is really very primitive wakeup.
3908 void poll_wait_pipe(struct trace_iterator *iter)
3910 set_current_state(TASK_INTERRUPTIBLE);
3911 /* sleep for 100 msecs, and try again. */
3912 schedule_timeout(HZ / 10);
3915 /* Must be called with trace_types_lock mutex held. */
3916 static int tracing_wait_pipe(struct file *filp)
3918 struct trace_iterator *iter = filp->private_data;
3920 while (trace_empty(iter)) {
3922 if ((filp->f_flags & O_NONBLOCK)) {
3926 mutex_unlock(&iter->mutex);
3928 iter->trace->wait_pipe(iter);
3930 mutex_lock(&iter->mutex);
3932 if (signal_pending(current))
3936 * We block until we read something and tracing is disabled.
3937 * We still block if tracing is disabled, but we have never
3938 * read anything. This allows a user to cat this file, and
3939 * then enable tracing. But after we have read something,
3940 * we give an EOF when tracing is again disabled.
3942 * iter->pos will be 0 if we haven't read anything.
3944 if (!tracing_is_enabled() && iter->pos)
3955 tracing_read_pipe(struct file *filp, char __user *ubuf,
3956 size_t cnt, loff_t *ppos)
3958 struct trace_iterator *iter = filp->private_data;
3959 struct trace_array *tr = iter->tr;
3962 /* return any leftover data */
3963 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3967 trace_seq_init(&iter->seq);
3969 /* copy the tracer to avoid using a global lock all around */
3970 mutex_lock(&trace_types_lock);
3971 if (unlikely(iter->trace->name != tr->current_trace->name))
3972 *iter->trace = *tr->current_trace;
3973 mutex_unlock(&trace_types_lock);
3976 * Avoid more than one consumer on a single file descriptor
3977 * This is just a matter of traces coherency, the ring buffer itself
3980 mutex_lock(&iter->mutex);
3981 if (iter->trace->read) {
3982 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3988 sret = tracing_wait_pipe(filp);
3992 /* stop when tracing is finished */
3993 if (trace_empty(iter)) {
3998 if (cnt >= PAGE_SIZE)
3999 cnt = PAGE_SIZE - 1;
4001 /* reset all but tr, trace, and overruns */
4002 memset(&iter->seq, 0,
4003 sizeof(struct trace_iterator) -
4004 offsetof(struct trace_iterator, seq));
4007 trace_event_read_lock();
4008 trace_access_lock(iter->cpu_file);
4009 while (trace_find_next_entry_inc(iter) != NULL) {
4010 enum print_line_t ret;
4011 int len = iter->seq.len;
4013 ret = print_trace_line(iter);
4014 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4015 /* don't print partial lines */
4016 iter->seq.len = len;
4019 if (ret != TRACE_TYPE_NO_CONSUME)
4020 trace_consume(iter);
4022 if (iter->seq.len >= cnt)
4026 * Setting the full flag means we reached the trace_seq buffer
4027 * size and we should leave by partial output condition above.
4028 * One of the trace_seq_* functions is not used properly.
4030 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
4033 trace_access_unlock(iter->cpu_file);
4034 trace_event_read_unlock();
4036 /* Now copy what we have to the user */
4037 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4038 if (iter->seq.readpos >= iter->seq.len)
4039 trace_seq_init(&iter->seq);
4042 * If there was nothing to send to user, in spite of consuming trace
4043 * entries, go back to wait for more entries.
4049 mutex_unlock(&iter->mutex);
4054 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
4055 struct pipe_buffer *buf)
4057 __free_page(buf->page);
4060 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
4063 __free_page(spd->pages[idx]);
4066 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
4068 .map = generic_pipe_buf_map,
4069 .unmap = generic_pipe_buf_unmap,
4070 .confirm = generic_pipe_buf_confirm,
4071 .release = tracing_pipe_buf_release,
4072 .steal = generic_pipe_buf_steal,
4073 .get = generic_pipe_buf_get,
4077 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
4082 /* Seq buffer is page-sized, exactly what we need. */
4084 count = iter->seq.len;
4085 ret = print_trace_line(iter);
4086 count = iter->seq.len - count;
4089 iter->seq.len -= count;
4092 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4093 iter->seq.len -= count;
4097 if (ret != TRACE_TYPE_NO_CONSUME)
4098 trace_consume(iter);
4100 if (!trace_find_next_entry_inc(iter)) {
4110 static ssize_t tracing_splice_read_pipe(struct file *filp,
4112 struct pipe_inode_info *pipe,
4116 struct page *pages_def[PIPE_DEF_BUFFERS];
4117 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4118 struct trace_iterator *iter = filp->private_data;
4119 struct splice_pipe_desc spd = {
4121 .partial = partial_def,
4122 .nr_pages = 0, /* This gets updated below. */
4123 .nr_pages_max = PIPE_DEF_BUFFERS,
4125 .ops = &tracing_pipe_buf_ops,
4126 .spd_release = tracing_spd_release_pipe,
4128 struct trace_array *tr = iter->tr;
4133 if (splice_grow_spd(pipe, &spd))
4136 /* copy the tracer to avoid using a global lock all around */
4137 mutex_lock(&trace_types_lock);
4138 if (unlikely(iter->trace->name != tr->current_trace->name))
4139 *iter->trace = *tr->current_trace;
4140 mutex_unlock(&trace_types_lock);
4142 mutex_lock(&iter->mutex);
4144 if (iter->trace->splice_read) {
4145 ret = iter->trace->splice_read(iter, filp,
4146 ppos, pipe, len, flags);
4151 ret = tracing_wait_pipe(filp);
4155 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
4160 trace_event_read_lock();
4161 trace_access_lock(iter->cpu_file);
4163 /* Fill as many pages as possible. */
4164 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
4165 spd.pages[i] = alloc_page(GFP_KERNEL);
4169 rem = tracing_fill_pipe_page(rem, iter);
4171 /* Copy the data into the page, so we can start over. */
4172 ret = trace_seq_to_buffer(&iter->seq,
4173 page_address(spd.pages[i]),
4176 __free_page(spd.pages[i]);
4179 spd.partial[i].offset = 0;
4180 spd.partial[i].len = iter->seq.len;
4182 trace_seq_init(&iter->seq);
4185 trace_access_unlock(iter->cpu_file);
4186 trace_event_read_unlock();
4187 mutex_unlock(&iter->mutex);
4191 ret = splice_to_pipe(pipe, &spd);
4193 splice_shrink_spd(&spd);
4197 mutex_unlock(&iter->mutex);
4202 tracing_entries_read(struct file *filp, char __user *ubuf,
4203 size_t cnt, loff_t *ppos)
4205 struct trace_cpu *tc = filp->private_data;
4206 struct trace_array *tr = tc->tr;
4211 mutex_lock(&trace_types_lock);
4213 if (tc->cpu == RING_BUFFER_ALL_CPUS) {
4214 int cpu, buf_size_same;
4219 /* check if all cpu sizes are same */
4220 for_each_tracing_cpu(cpu) {
4221 /* fill in the size from first enabled cpu */
4223 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4224 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
4230 if (buf_size_same) {
4231 if (!ring_buffer_expanded)
4232 r = sprintf(buf, "%lu (expanded: %lu)\n",
4234 trace_buf_size >> 10);
4236 r = sprintf(buf, "%lu\n", size >> 10);
4238 r = sprintf(buf, "X\n");
4240 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, tc->cpu)->entries >> 10);
4242 mutex_unlock(&trace_types_lock);
4244 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4249 tracing_entries_write(struct file *filp, const char __user *ubuf,
4250 size_t cnt, loff_t *ppos)
4252 struct trace_cpu *tc = filp->private_data;
4256 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4260 /* must have at least 1 entry */
4264 /* value is in KB */
4267 ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
4277 tracing_total_entries_read(struct file *filp, char __user *ubuf,
4278 size_t cnt, loff_t *ppos)
4280 struct trace_array *tr = filp->private_data;
4283 unsigned long size = 0, expanded_size = 0;
4285 mutex_lock(&trace_types_lock);
4286 for_each_tracing_cpu(cpu) {
4287 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
4288 if (!ring_buffer_expanded)
4289 expanded_size += trace_buf_size >> 10;
4291 if (ring_buffer_expanded)
4292 r = sprintf(buf, "%lu\n", size);
4294 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4295 mutex_unlock(&trace_types_lock);
4297 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4301 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4302 size_t cnt, loff_t *ppos)
4305 * There is no need to read what the user has written, this function
4306 * is just to make sure that there is no error when "echo" is used
4315 tracing_free_buffer_release(struct inode *inode, struct file *filp)
4317 struct trace_array *tr = inode->i_private;
4319 /* disable tracing ? */
4320 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4322 /* resize the ring buffer to 0 */
4323 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4329 tracing_mark_write(struct file *filp, const char __user *ubuf,
4330 size_t cnt, loff_t *fpos)
4332 unsigned long addr = (unsigned long)ubuf;
4333 struct ring_buffer_event *event;
4334 struct ring_buffer *buffer;
4335 struct print_entry *entry;
4336 unsigned long irq_flags;
4337 struct page *pages[2];
4347 if (tracing_disabled)
4350 if (!(trace_flags & TRACE_ITER_MARKERS))
4353 if (cnt > TRACE_BUF_SIZE)
4354 cnt = TRACE_BUF_SIZE;
4357 * Userspace is injecting traces into the kernel trace buffer.
4358 * We want to be as non intrusive as possible.
4359 * To do so, we do not want to allocate any special buffers
4360 * or take any locks, but instead write the userspace data
4361 * straight into the ring buffer.
4363 * First we need to pin the userspace buffer into memory,
4364 * which, most likely it is, because it just referenced it.
4365 * But there's no guarantee that it is. By using get_user_pages_fast()
4366 * and kmap_atomic/kunmap_atomic() we can get access to the
4367 * pages directly. We then write the data directly into the
4370 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4372 /* check if we cross pages */
4373 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4376 offset = addr & (PAGE_SIZE - 1);
4379 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4380 if (ret < nr_pages) {
4382 put_page(pages[ret]);
4387 for (i = 0; i < nr_pages; i++)
4388 map_page[i] = kmap_atomic(pages[i]);
4390 local_save_flags(irq_flags);
4391 size = sizeof(*entry) + cnt + 2; /* possible \n added */
4392 buffer = global_trace.trace_buffer.buffer;
4393 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4394 irq_flags, preempt_count());
4396 /* Ring buffer disabled, return as if not open for write */
4401 entry = ring_buffer_event_data(event);
4402 entry->ip = _THIS_IP_;
4404 if (nr_pages == 2) {
4405 len = PAGE_SIZE - offset;
4406 memcpy(&entry->buf, map_page[0] + offset, len);
4407 memcpy(&entry->buf[len], map_page[1], cnt - len);
4409 memcpy(&entry->buf, map_page[0] + offset, cnt);
4411 if (entry->buf[cnt - 1] != '\n') {
4412 entry->buf[cnt] = '\n';
4413 entry->buf[cnt + 1] = '\0';
4415 entry->buf[cnt] = '\0';
4417 __buffer_unlock_commit(buffer, event);
4424 for (i = 0; i < nr_pages; i++){
4425 kunmap_atomic(map_page[i]);
4432 static int tracing_clock_show(struct seq_file *m, void *v)
4434 struct trace_array *tr = m->private;
4437 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4439 "%s%s%s%s", i ? " " : "",
4440 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4441 i == tr->clock_id ? "]" : "");
4447 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4448 size_t cnt, loff_t *fpos)
4450 struct seq_file *m = filp->private_data;
4451 struct trace_array *tr = m->private;
4453 const char *clockstr;
4456 if (cnt >= sizeof(buf))
4459 if (copy_from_user(&buf, ubuf, cnt))
4464 clockstr = strstrip(buf);
4466 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4467 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4470 if (i == ARRAY_SIZE(trace_clocks))
4473 mutex_lock(&trace_types_lock);
4477 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
4480 * New clock may not be consistent with the previous clock.
4481 * Reset the buffer so that it doesn't have incomparable timestamps.
4483 tracing_reset_online_cpus(&global_trace.trace_buffer);
4485 #ifdef CONFIG_TRACER_MAX_TRACE
4486 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4487 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4488 tracing_reset_online_cpus(&global_trace.max_buffer);
4491 mutex_unlock(&trace_types_lock);
4498 static int tracing_clock_open(struct inode *inode, struct file *file)
4500 if (tracing_disabled)
4503 return single_open(file, tracing_clock_show, inode->i_private);
4506 struct ftrace_buffer_info {
4507 struct trace_iterator iter;
4512 #ifdef CONFIG_TRACER_SNAPSHOT
4513 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4515 struct trace_cpu *tc = inode->i_private;
4516 struct trace_iterator *iter;
4520 if (file->f_mode & FMODE_READ) {
4521 iter = __tracing_open(inode, file, true);
4523 ret = PTR_ERR(iter);
4525 /* Writes still need the seq_file to hold the private data */
4526 m = kzalloc(sizeof(*m), GFP_KERNEL);
4529 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4535 iter->trace_buffer = &tc->tr->max_buffer;
4536 iter->cpu_file = tc->cpu;
4538 file->private_data = m;
4545 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4548 struct seq_file *m = filp->private_data;
4549 struct trace_iterator *iter = m->private;
4550 struct trace_array *tr = iter->tr;
4554 ret = tracing_update_buffers();
4558 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4562 mutex_lock(&trace_types_lock);
4564 if (tr->current_trace->use_max_tr) {
4571 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4575 if (tr->allocated_snapshot)
4579 /* Only allow per-cpu swap if the ring buffer supports it */
4580 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4581 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4586 if (!tr->allocated_snapshot) {
4587 ret = alloc_snapshot(tr);
4591 local_irq_disable();
4592 /* Now, we're going to swap */
4593 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4594 update_max_tr(tr, current, smp_processor_id());
4596 update_max_tr_single(tr, current, iter->cpu_file);
4600 if (tr->allocated_snapshot) {
4601 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4602 tracing_reset_online_cpus(&tr->max_buffer);
4604 tracing_reset(&tr->max_buffer, iter->cpu_file);
4614 mutex_unlock(&trace_types_lock);
4618 static int tracing_snapshot_release(struct inode *inode, struct file *file)
4620 struct seq_file *m = file->private_data;
4622 if (file->f_mode & FMODE_READ)
4623 return tracing_release(inode, file);
4625 /* If write only, the seq_file is just a stub */
4633 static int tracing_buffers_open(struct inode *inode, struct file *filp);
4634 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
4635 size_t count, loff_t *ppos);
4636 static int tracing_buffers_release(struct inode *inode, struct file *file);
4637 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4638 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
4640 static int snapshot_raw_open(struct inode *inode, struct file *filp)
4642 struct ftrace_buffer_info *info;
4645 ret = tracing_buffers_open(inode, filp);
4649 info = filp->private_data;
4651 if (info->iter.trace->use_max_tr) {
4652 tracing_buffers_release(inode, filp);
4656 info->iter.snapshot = true;
4657 info->iter.trace_buffer = &info->iter.tr->max_buffer;
4662 #endif /* CONFIG_TRACER_SNAPSHOT */
4665 static const struct file_operations tracing_max_lat_fops = {
4666 .open = tracing_open_generic,
4667 .read = tracing_max_lat_read,
4668 .write = tracing_max_lat_write,
4669 .llseek = generic_file_llseek,
4672 static const struct file_operations set_tracer_fops = {
4673 .open = tracing_open_generic,
4674 .read = tracing_set_trace_read,
4675 .write = tracing_set_trace_write,
4676 .llseek = generic_file_llseek,
4679 static const struct file_operations tracing_pipe_fops = {
4680 .open = tracing_open_pipe,
4681 .poll = tracing_poll_pipe,
4682 .read = tracing_read_pipe,
4683 .splice_read = tracing_splice_read_pipe,
4684 .release = tracing_release_pipe,
4685 .llseek = no_llseek,
4688 static const struct file_operations tracing_entries_fops = {
4689 .open = tracing_open_generic,
4690 .read = tracing_entries_read,
4691 .write = tracing_entries_write,
4692 .llseek = generic_file_llseek,
4695 static const struct file_operations tracing_total_entries_fops = {
4696 .open = tracing_open_generic,
4697 .read = tracing_total_entries_read,
4698 .llseek = generic_file_llseek,
4701 static const struct file_operations tracing_free_buffer_fops = {
4702 .write = tracing_free_buffer_write,
4703 .release = tracing_free_buffer_release,
4706 static const struct file_operations tracing_mark_fops = {
4707 .open = tracing_open_generic,
4708 .write = tracing_mark_write,
4709 .llseek = generic_file_llseek,
4712 static const struct file_operations trace_clock_fops = {
4713 .open = tracing_clock_open,
4715 .llseek = seq_lseek,
4716 .release = single_release,
4717 .write = tracing_clock_write,
4720 #ifdef CONFIG_TRACER_SNAPSHOT
4721 static const struct file_operations snapshot_fops = {
4722 .open = tracing_snapshot_open,
4724 .write = tracing_snapshot_write,
4725 .llseek = tracing_seek,
4726 .release = tracing_snapshot_release,
4729 static const struct file_operations snapshot_raw_fops = {
4730 .open = snapshot_raw_open,
4731 .read = tracing_buffers_read,
4732 .release = tracing_buffers_release,
4733 .splice_read = tracing_buffers_splice_read,
4734 .llseek = no_llseek,
4737 #endif /* CONFIG_TRACER_SNAPSHOT */
4739 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4741 struct trace_cpu *tc = inode->i_private;
4742 struct trace_array *tr = tc->tr;
4743 struct ftrace_buffer_info *info;
4745 if (tracing_disabled)
4748 info = kzalloc(sizeof(*info), GFP_KERNEL);
4752 mutex_lock(&trace_types_lock);
4757 info->iter.cpu_file = tc->cpu;
4758 info->iter.trace = tr->current_trace;
4759 info->iter.trace_buffer = &tr->trace_buffer;
4761 /* Force reading ring buffer for first read */
4762 info->read = (unsigned int)-1;
4764 filp->private_data = info;
4766 mutex_unlock(&trace_types_lock);
4768 return nonseekable_open(inode, filp);
4772 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
4774 struct ftrace_buffer_info *info = filp->private_data;
4775 struct trace_iterator *iter = &info->iter;
4777 return trace_poll(iter, filp, poll_table);
4781 tracing_buffers_read(struct file *filp, char __user *ubuf,
4782 size_t count, loff_t *ppos)
4784 struct ftrace_buffer_info *info = filp->private_data;
4785 struct trace_iterator *iter = &info->iter;
4792 mutex_lock(&trace_types_lock);
4794 #ifdef CONFIG_TRACER_MAX_TRACE
4795 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4802 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
4808 /* Do we have previous read data to read? */
4809 if (info->read < PAGE_SIZE)
4813 trace_access_lock(iter->cpu_file);
4814 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
4818 trace_access_unlock(iter->cpu_file);
4821 if (trace_empty(iter)) {
4822 if ((filp->f_flags & O_NONBLOCK)) {
4826 mutex_unlock(&trace_types_lock);
4827 iter->trace->wait_pipe(iter);
4828 mutex_lock(&trace_types_lock);
4829 if (signal_pending(current)) {
4841 size = PAGE_SIZE - info->read;
4845 ret = copy_to_user(ubuf, info->spare + info->read, size);
4856 mutex_unlock(&trace_types_lock);
4861 static int tracing_buffers_release(struct inode *inode, struct file *file)
4863 struct ftrace_buffer_info *info = file->private_data;
4864 struct trace_iterator *iter = &info->iter;
4866 mutex_lock(&trace_types_lock);
4868 WARN_ON(!iter->tr->ref);
4872 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
4875 mutex_unlock(&trace_types_lock);
4881 struct ring_buffer *buffer;
4886 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4887 struct pipe_buffer *buf)
4889 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4894 ring_buffer_free_read_page(ref->buffer, ref->page);
4899 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4900 struct pipe_buffer *buf)
4902 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4907 /* Pipe buffer operations for a buffer. */
4908 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4910 .map = generic_pipe_buf_map,
4911 .unmap = generic_pipe_buf_unmap,
4912 .confirm = generic_pipe_buf_confirm,
4913 .release = buffer_pipe_buf_release,
4914 .steal = generic_pipe_buf_steal,
4915 .get = buffer_pipe_buf_get,
4919 * Callback from splice_to_pipe(), if we need to release some pages
4920 * at the end of the spd in case we error'ed out in filling the pipe.
4922 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4924 struct buffer_ref *ref =
4925 (struct buffer_ref *)spd->partial[i].private;
4930 ring_buffer_free_read_page(ref->buffer, ref->page);
4932 spd->partial[i].private = 0;
4936 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4937 struct pipe_inode_info *pipe, size_t len,
4940 struct ftrace_buffer_info *info = file->private_data;
4941 struct trace_iterator *iter = &info->iter;
4942 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4943 struct page *pages_def[PIPE_DEF_BUFFERS];
4944 struct splice_pipe_desc spd = {
4946 .partial = partial_def,
4947 .nr_pages_max = PIPE_DEF_BUFFERS,
4949 .ops = &buffer_pipe_buf_ops,
4950 .spd_release = buffer_spd_release,
4952 struct buffer_ref *ref;
4953 int entries, size, i;
4956 mutex_lock(&trace_types_lock);
4958 #ifdef CONFIG_TRACER_MAX_TRACE
4959 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4965 if (splice_grow_spd(pipe, &spd)) {
4970 if (*ppos & (PAGE_SIZE - 1)) {
4975 if (len & (PAGE_SIZE - 1)) {
4976 if (len < PAGE_SIZE) {
4984 trace_access_lock(iter->cpu_file);
4985 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
4987 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4991 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4996 ref->buffer = iter->trace_buffer->buffer;
4997 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
5003 r = ring_buffer_read_page(ref->buffer, &ref->page,
5004 len, iter->cpu_file, 1);
5006 ring_buffer_free_read_page(ref->buffer, ref->page);
5012 * zero out any left over data, this is going to
5015 size = ring_buffer_page_len(ref->page);
5016 if (size < PAGE_SIZE)
5017 memset(ref->page + size, 0, PAGE_SIZE - size);
5019 page = virt_to_page(ref->page);
5021 spd.pages[i] = page;
5022 spd.partial[i].len = PAGE_SIZE;
5023 spd.partial[i].offset = 0;
5024 spd.partial[i].private = (unsigned long)ref;
5028 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5031 trace_access_unlock(iter->cpu_file);
5034 /* did we read anything? */
5035 if (!spd.nr_pages) {
5036 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
5040 mutex_unlock(&trace_types_lock);
5041 iter->trace->wait_pipe(iter);
5042 mutex_lock(&trace_types_lock);
5043 if (signal_pending(current)) {
5050 ret = splice_to_pipe(pipe, &spd);
5051 splice_shrink_spd(&spd);
5053 mutex_unlock(&trace_types_lock);
5058 static const struct file_operations tracing_buffers_fops = {
5059 .open = tracing_buffers_open,
5060 .read = tracing_buffers_read,
5061 .poll = tracing_buffers_poll,
5062 .release = tracing_buffers_release,
5063 .splice_read = tracing_buffers_splice_read,
5064 .llseek = no_llseek,
5068 tracing_stats_read(struct file *filp, char __user *ubuf,
5069 size_t count, loff_t *ppos)
5071 struct trace_cpu *tc = filp->private_data;
5072 struct trace_array *tr = tc->tr;
5073 struct trace_buffer *trace_buf = &tr->trace_buffer;
5074 struct trace_seq *s;
5076 unsigned long long t;
5077 unsigned long usec_rem;
5080 s = kmalloc(sizeof(*s), GFP_KERNEL);
5086 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
5087 trace_seq_printf(s, "entries: %ld\n", cnt);
5089 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
5090 trace_seq_printf(s, "overrun: %ld\n", cnt);
5092 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
5093 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5095 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
5096 trace_seq_printf(s, "bytes: %ld\n", cnt);
5098 if (trace_clocks[trace_clock_id].in_ns) {
5099 /* local or global for trace_clock */
5100 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5101 usec_rem = do_div(t, USEC_PER_SEC);
5102 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5105 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
5106 usec_rem = do_div(t, USEC_PER_SEC);
5107 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5109 /* counter or tsc mode for trace_clock */
5110 trace_seq_printf(s, "oldest event ts: %llu\n",
5111 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5113 trace_seq_printf(s, "now ts: %llu\n",
5114 ring_buffer_time_stamp(trace_buf->buffer, cpu));
5117 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
5118 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5120 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
5121 trace_seq_printf(s, "read events: %ld\n", cnt);
5123 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
5130 static const struct file_operations tracing_stats_fops = {
5131 .open = tracing_open_generic,
5132 .read = tracing_stats_read,
5133 .llseek = generic_file_llseek,
5136 #ifdef CONFIG_DYNAMIC_FTRACE
5138 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
5144 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
5145 size_t cnt, loff_t *ppos)
5147 static char ftrace_dyn_info_buffer[1024];
5148 static DEFINE_MUTEX(dyn_info_mutex);
5149 unsigned long *p = filp->private_data;
5150 char *buf = ftrace_dyn_info_buffer;
5151 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
5154 mutex_lock(&dyn_info_mutex);
5155 r = sprintf(buf, "%ld ", *p);
5157 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
5160 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5162 mutex_unlock(&dyn_info_mutex);
5167 static const struct file_operations tracing_dyn_info_fops = {
5168 .open = tracing_open_generic,
5169 .read = tracing_read_dyn_info,
5170 .llseek = generic_file_llseek,
5172 #endif /* CONFIG_DYNAMIC_FTRACE */
5174 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5176 ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5182 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5184 unsigned long *count = (long *)data;
5196 ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5197 struct ftrace_probe_ops *ops, void *data)
5199 long count = (long)data;
5201 seq_printf(m, "%ps:", (void *)ip);
5203 seq_printf(m, "snapshot");
5206 seq_printf(m, ":unlimited\n");
5208 seq_printf(m, ":count=%ld\n", count);
5213 static struct ftrace_probe_ops snapshot_probe_ops = {
5214 .func = ftrace_snapshot,
5215 .print = ftrace_snapshot_print,
5218 static struct ftrace_probe_ops snapshot_count_probe_ops = {
5219 .func = ftrace_count_snapshot,
5220 .print = ftrace_snapshot_print,
5224 ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5225 char *glob, char *cmd, char *param, int enable)
5227 struct ftrace_probe_ops *ops;
5228 void *count = (void *)-1;
5232 /* hash funcs only work with set_ftrace_filter */
5236 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
5238 if (glob[0] == '!') {
5239 unregister_ftrace_function_probe_func(glob+1, ops);
5246 number = strsep(¶m, ":");
5248 if (!strlen(number))
5252 * We use the callback data field (which is a pointer)
5255 ret = kstrtoul(number, 0, (unsigned long *)&count);
5260 ret = register_ftrace_function_probe(glob, ops, count);
5263 alloc_snapshot(&global_trace);
5265 return ret < 0 ? ret : 0;
5268 static struct ftrace_func_command ftrace_snapshot_cmd = {
5270 .func = ftrace_trace_snapshot_callback,
5273 static int register_snapshot_cmd(void)
5275 return register_ftrace_command(&ftrace_snapshot_cmd);
5278 static inline int register_snapshot_cmd(void) { return 0; }
5279 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
5281 struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
5286 if (!debugfs_initialized())
5289 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5290 tr->dir = debugfs_create_dir("tracing", NULL);
5293 pr_warn_once("Could not create debugfs directory 'tracing'\n");
5298 struct dentry *tracing_init_dentry(void)
5300 return tracing_init_dentry_tr(&global_trace);
5303 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
5305 struct dentry *d_tracer;
5308 return tr->percpu_dir;
5310 d_tracer = tracing_init_dentry_tr(tr);
5314 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
5316 WARN_ONCE(!tr->percpu_dir,
5317 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
5319 return tr->percpu_dir;
5323 tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
5325 struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
5326 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5327 struct dentry *d_cpu;
5328 char cpu_dir[30]; /* 30 characters should be more than enough */
5333 snprintf(cpu_dir, 30, "cpu%ld", cpu);
5334 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5336 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5340 /* per cpu trace_pipe */
5341 trace_create_file("trace_pipe", 0444, d_cpu,
5342 (void *)&data->trace_cpu, &tracing_pipe_fops);
5345 trace_create_file("trace", 0644, d_cpu,
5346 (void *)&data->trace_cpu, &tracing_fops);
5348 trace_create_file("trace_pipe_raw", 0444, d_cpu,
5349 (void *)&data->trace_cpu, &tracing_buffers_fops);
5351 trace_create_file("stats", 0444, d_cpu,
5352 (void *)&data->trace_cpu, &tracing_stats_fops);
5354 trace_create_file("buffer_size_kb", 0444, d_cpu,
5355 (void *)&data->trace_cpu, &tracing_entries_fops);
5357 #ifdef CONFIG_TRACER_SNAPSHOT
5358 trace_create_file("snapshot", 0644, d_cpu,
5359 (void *)&data->trace_cpu, &snapshot_fops);
5361 trace_create_file("snapshot_raw", 0444, d_cpu,
5362 (void *)&data->trace_cpu, &snapshot_raw_fops);
5366 #ifdef CONFIG_FTRACE_SELFTEST
5367 /* Let selftest have access to static functions in this file */
5368 #include "trace_selftest.c"
5371 struct trace_option_dentry {
5372 struct tracer_opt *opt;
5373 struct tracer_flags *flags;
5374 struct trace_array *tr;
5375 struct dentry *entry;
5379 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5382 struct trace_option_dentry *topt = filp->private_data;
5385 if (topt->flags->val & topt->opt->bit)
5390 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5394 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5397 struct trace_option_dentry *topt = filp->private_data;
5401 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5405 if (val != 0 && val != 1)
5408 if (!!(topt->flags->val & topt->opt->bit) != val) {
5409 mutex_lock(&trace_types_lock);
5410 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
5412 mutex_unlock(&trace_types_lock);
5423 static const struct file_operations trace_options_fops = {
5424 .open = tracing_open_generic,
5425 .read = trace_options_read,
5426 .write = trace_options_write,
5427 .llseek = generic_file_llseek,
5431 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5434 long index = (long)filp->private_data;
5437 if (trace_flags & (1 << index))
5442 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5446 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5449 struct trace_array *tr = &global_trace;
5450 long index = (long)filp->private_data;
5454 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5458 if (val != 0 && val != 1)
5461 mutex_lock(&trace_types_lock);
5462 ret = set_tracer_flag(tr, 1 << index, val);
5463 mutex_unlock(&trace_types_lock);
5473 static const struct file_operations trace_options_core_fops = {
5474 .open = tracing_open_generic,
5475 .read = trace_options_core_read,
5476 .write = trace_options_core_write,
5477 .llseek = generic_file_llseek,
5480 struct dentry *trace_create_file(const char *name,
5482 struct dentry *parent,
5484 const struct file_operations *fops)
5488 ret = debugfs_create_file(name, mode, parent, data, fops);
5490 pr_warning("Could not create debugfs '%s' entry\n", name);
5496 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
5498 struct dentry *d_tracer;
5503 d_tracer = tracing_init_dentry_tr(tr);
5507 tr->options = debugfs_create_dir("options", d_tracer);
5509 pr_warning("Could not create debugfs directory 'options'\n");
5517 create_trace_option_file(struct trace_array *tr,
5518 struct trace_option_dentry *topt,
5519 struct tracer_flags *flags,
5520 struct tracer_opt *opt)
5522 struct dentry *t_options;
5524 t_options = trace_options_init_dentry(tr);
5528 topt->flags = flags;
5532 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
5533 &trace_options_fops);
5537 static struct trace_option_dentry *
5538 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
5540 struct trace_option_dentry *topts;
5541 struct tracer_flags *flags;
5542 struct tracer_opt *opts;
5548 flags = tracer->flags;
5550 if (!flags || !flags->opts)
5555 for (cnt = 0; opts[cnt].name; cnt++)
5558 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
5562 for (cnt = 0; opts[cnt].name; cnt++)
5563 create_trace_option_file(tr, &topts[cnt], flags,
5570 destroy_trace_option_files(struct trace_option_dentry *topts)
5577 for (cnt = 0; topts[cnt].opt; cnt++) {
5578 if (topts[cnt].entry)
5579 debugfs_remove(topts[cnt].entry);
5585 static struct dentry *
5586 create_trace_option_core_file(struct trace_array *tr,
5587 const char *option, long index)
5589 struct dentry *t_options;
5591 t_options = trace_options_init_dentry(tr);
5595 return trace_create_file(option, 0644, t_options, (void *)index,
5596 &trace_options_core_fops);
5599 static __init void create_trace_options_dir(struct trace_array *tr)
5601 struct dentry *t_options;
5604 t_options = trace_options_init_dentry(tr);
5608 for (i = 0; trace_options[i]; i++)
5609 create_trace_option_core_file(tr, trace_options[i], i);
5613 rb_simple_read(struct file *filp, char __user *ubuf,
5614 size_t cnt, loff_t *ppos)
5616 struct trace_array *tr = filp->private_data;
5617 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5622 r = ring_buffer_record_is_on(buffer);
5626 r = sprintf(buf, "%d\n", r);
5628 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5632 rb_simple_write(struct file *filp, const char __user *ubuf,
5633 size_t cnt, loff_t *ppos)
5635 struct trace_array *tr = filp->private_data;
5636 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5640 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5645 mutex_lock(&trace_types_lock);
5647 ring_buffer_record_on(buffer);
5648 if (tr->current_trace->start)
5649 tr->current_trace->start(tr);
5651 ring_buffer_record_off(buffer);
5652 if (tr->current_trace->stop)
5653 tr->current_trace->stop(tr);
5655 mutex_unlock(&trace_types_lock);
5663 static const struct file_operations rb_simple_fops = {
5664 .open = tracing_open_generic,
5665 .read = rb_simple_read,
5666 .write = rb_simple_write,
5667 .llseek = default_llseek,
5670 struct dentry *trace_instance_dir;
5673 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5675 static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
5679 for_each_tracing_cpu(cpu) {
5680 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
5681 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
5682 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
5687 allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
5689 enum ring_buffer_flags rb_flags;
5691 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5693 buf->buffer = ring_buffer_alloc(size, rb_flags);
5697 buf->data = alloc_percpu(struct trace_array_cpu);
5699 ring_buffer_free(buf->buffer);
5703 init_trace_buffers(tr, buf);
5705 /* Allocate the first page for all buffers */
5706 set_buffer_entries(&tr->trace_buffer,
5707 ring_buffer_size(tr->trace_buffer.buffer, 0));
5712 static int allocate_trace_buffers(struct trace_array *tr, int size)
5716 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
5720 #ifdef CONFIG_TRACER_MAX_TRACE
5721 ret = allocate_trace_buffer(tr, &tr->max_buffer,
5722 allocate_snapshot ? size : 1);
5724 ring_buffer_free(tr->trace_buffer.buffer);
5725 free_percpu(tr->trace_buffer.data);
5728 tr->allocated_snapshot = allocate_snapshot;
5731 * Only the top level trace array gets its snapshot allocated
5732 * from the kernel command line.
5734 allocate_snapshot = false;
5739 static int new_instance_create(const char *name)
5741 struct trace_array *tr;
5744 mutex_lock(&trace_types_lock);
5747 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5748 if (tr->name && strcmp(tr->name, name) == 0)
5753 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
5757 tr->name = kstrdup(name, GFP_KERNEL);
5761 raw_spin_lock_init(&tr->start_lock);
5763 tr->current_trace = &nop_trace;
5765 INIT_LIST_HEAD(&tr->systems);
5766 INIT_LIST_HEAD(&tr->events);
5768 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
5771 /* Holder for file callbacks */
5772 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5773 tr->trace_cpu.tr = tr;
5775 tr->dir = debugfs_create_dir(name, trace_instance_dir);
5779 ret = event_trace_add_tracer(tr->dir, tr);
5783 init_tracer_debugfs(tr, tr->dir);
5785 list_add(&tr->list, &ftrace_trace_arrays);
5787 mutex_unlock(&trace_types_lock);
5792 if (tr->trace_buffer.buffer)
5793 ring_buffer_free(tr->trace_buffer.buffer);
5798 mutex_unlock(&trace_types_lock);
5804 static int instance_delete(const char *name)
5806 struct trace_array *tr;
5810 mutex_lock(&trace_types_lock);
5813 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5814 if (tr->name && strcmp(tr->name, name) == 0) {
5826 list_del(&tr->list);
5828 event_trace_del_tracer(tr);
5829 debugfs_remove_recursive(tr->dir);
5830 free_percpu(tr->trace_buffer.data);
5831 ring_buffer_free(tr->trace_buffer.buffer);
5839 mutex_unlock(&trace_types_lock);
5844 static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
5846 struct dentry *parent;
5849 /* Paranoid: Make sure the parent is the "instances" directory */
5850 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5851 if (WARN_ON_ONCE(parent != trace_instance_dir))
5855 * The inode mutex is locked, but debugfs_create_dir() will also
5856 * take the mutex. As the instances directory can not be destroyed
5857 * or changed in any other way, it is safe to unlock it, and
5858 * let the dentry try. If two users try to make the same dir at
5859 * the same time, then the new_instance_create() will determine the
5862 mutex_unlock(&inode->i_mutex);
5864 ret = new_instance_create(dentry->d_iname);
5866 mutex_lock(&inode->i_mutex);
5871 static int instance_rmdir(struct inode *inode, struct dentry *dentry)
5873 struct dentry *parent;
5876 /* Paranoid: Make sure the parent is the "instances" directory */
5877 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5878 if (WARN_ON_ONCE(parent != trace_instance_dir))
5881 /* The caller did a dget() on dentry */
5882 mutex_unlock(&dentry->d_inode->i_mutex);
5885 * The inode mutex is locked, but debugfs_create_dir() will also
5886 * take the mutex. As the instances directory can not be destroyed
5887 * or changed in any other way, it is safe to unlock it, and
5888 * let the dentry try. If two users try to make the same dir at
5889 * the same time, then the instance_delete() will determine the
5892 mutex_unlock(&inode->i_mutex);
5894 ret = instance_delete(dentry->d_iname);
5896 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
5897 mutex_lock(&dentry->d_inode->i_mutex);
5902 static const struct inode_operations instance_dir_inode_operations = {
5903 .lookup = simple_lookup,
5904 .mkdir = instance_mkdir,
5905 .rmdir = instance_rmdir,
5908 static __init void create_trace_instances(struct dentry *d_tracer)
5910 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
5911 if (WARN_ON(!trace_instance_dir))
5914 /* Hijack the dir inode operations, to allow mkdir */
5915 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
5919 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
5923 trace_create_file("trace_options", 0644, d_tracer,
5924 tr, &tracing_iter_fops);
5926 trace_create_file("trace", 0644, d_tracer,
5927 (void *)&tr->trace_cpu, &tracing_fops);
5929 trace_create_file("trace_pipe", 0444, d_tracer,
5930 (void *)&tr->trace_cpu, &tracing_pipe_fops);
5932 trace_create_file("buffer_size_kb", 0644, d_tracer,
5933 (void *)&tr->trace_cpu, &tracing_entries_fops);
5935 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
5936 tr, &tracing_total_entries_fops);
5938 trace_create_file("free_buffer", 0644, d_tracer,
5939 tr, &tracing_free_buffer_fops);
5941 trace_create_file("trace_marker", 0220, d_tracer,
5942 tr, &tracing_mark_fops);
5944 trace_create_file("trace_clock", 0644, d_tracer, tr,
5947 trace_create_file("tracing_on", 0644, d_tracer,
5948 tr, &rb_simple_fops);
5950 #ifdef CONFIG_TRACER_SNAPSHOT
5951 trace_create_file("snapshot", 0644, d_tracer,
5952 (void *)&tr->trace_cpu, &snapshot_fops);
5955 for_each_tracing_cpu(cpu)
5956 tracing_init_debugfs_percpu(tr, cpu);
5960 static __init int tracer_init_debugfs(void)
5962 struct dentry *d_tracer;
5964 trace_access_lock_init();
5966 d_tracer = tracing_init_dentry();
5970 init_tracer_debugfs(&global_trace, d_tracer);
5972 trace_create_file("tracing_cpumask", 0644, d_tracer,
5973 &global_trace, &tracing_cpumask_fops);
5975 trace_create_file("available_tracers", 0444, d_tracer,
5976 &global_trace, &show_traces_fops);
5978 trace_create_file("current_tracer", 0644, d_tracer,
5979 &global_trace, &set_tracer_fops);
5981 #ifdef CONFIG_TRACER_MAX_TRACE
5982 trace_create_file("tracing_max_latency", 0644, d_tracer,
5983 &tracing_max_latency, &tracing_max_lat_fops);
5986 trace_create_file("tracing_thresh", 0644, d_tracer,
5987 &tracing_thresh, &tracing_max_lat_fops);
5989 trace_create_file("README", 0444, d_tracer,
5990 NULL, &tracing_readme_fops);
5992 trace_create_file("saved_cmdlines", 0444, d_tracer,
5993 NULL, &tracing_saved_cmdlines_fops);
5995 #ifdef CONFIG_DYNAMIC_FTRACE
5996 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
5997 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
6000 create_trace_instances(d_tracer);
6002 create_trace_options_dir(&global_trace);
6007 static int trace_panic_handler(struct notifier_block *this,
6008 unsigned long event, void *unused)
6010 if (ftrace_dump_on_oops)
6011 ftrace_dump(ftrace_dump_on_oops);
6015 static struct notifier_block trace_panic_notifier = {
6016 .notifier_call = trace_panic_handler,
6018 .priority = 150 /* priority: INT_MAX >= x >= 0 */
6021 static int trace_die_handler(struct notifier_block *self,
6027 if (ftrace_dump_on_oops)
6028 ftrace_dump(ftrace_dump_on_oops);
6036 static struct notifier_block trace_die_notifier = {
6037 .notifier_call = trace_die_handler,
6042 * printk is set to max of 1024, we really don't need it that big.
6043 * Nothing should be printing 1000 characters anyway.
6045 #define TRACE_MAX_PRINT 1000
6048 * Define here KERN_TRACE so that we have one place to modify
6049 * it if we decide to change what log level the ftrace dump
6052 #define KERN_TRACE KERN_EMERG
6055 trace_printk_seq(struct trace_seq *s)
6057 /* Probably should print a warning here. */
6058 if (s->len >= TRACE_MAX_PRINT)
6059 s->len = TRACE_MAX_PRINT;
6061 /* should be zero ended, but we are paranoid. */
6062 s->buffer[s->len] = 0;
6064 printk(KERN_TRACE "%s", s->buffer);
6069 void trace_init_global_iter(struct trace_iterator *iter)
6071 iter->tr = &global_trace;
6072 iter->trace = iter->tr->current_trace;
6073 iter->cpu_file = RING_BUFFER_ALL_CPUS;
6074 iter->trace_buffer = &global_trace.trace_buffer;
6077 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
6079 /* use static because iter can be a bit big for the stack */
6080 static struct trace_iterator iter;
6081 static atomic_t dump_running;
6082 unsigned int old_userobj;
6083 unsigned long flags;
6086 /* Only allow one dump user at a time. */
6087 if (atomic_inc_return(&dump_running) != 1) {
6088 atomic_dec(&dump_running);
6093 * Always turn off tracing when we dump.
6094 * We don't need to show trace output of what happens
6095 * between multiple crashes.
6097 * If the user does a sysrq-z, then they can re-enable
6098 * tracing with echo 1 > tracing_on.
6102 local_irq_save(flags);
6104 /* Simulate the iterator */
6105 trace_init_global_iter(&iter);
6107 for_each_tracing_cpu(cpu) {
6108 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
6111 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
6113 /* don't look at user memory in panic mode */
6114 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
6116 switch (oops_dump_mode) {
6118 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6121 iter.cpu_file = raw_smp_processor_id();
6126 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
6127 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6130 printk(KERN_TRACE "Dumping ftrace buffer:\n");
6132 /* Did function tracer already get disabled? */
6133 if (ftrace_is_dead()) {
6134 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
6135 printk("# MAY BE MISSING FUNCTION EVENTS\n");
6139 * We need to stop all tracing on all CPUS to read the
6140 * the next buffer. This is a bit expensive, but is
6141 * not done often. We fill all what we can read,
6142 * and then release the locks again.
6145 while (!trace_empty(&iter)) {
6148 printk(KERN_TRACE "---------------------------------\n");
6152 /* reset all but tr, trace, and overruns */
6153 memset(&iter.seq, 0,
6154 sizeof(struct trace_iterator) -
6155 offsetof(struct trace_iterator, seq));
6156 iter.iter_flags |= TRACE_FILE_LAT_FMT;
6159 if (trace_find_next_entry_inc(&iter) != NULL) {
6162 ret = print_trace_line(&iter);
6163 if (ret != TRACE_TYPE_NO_CONSUME)
6164 trace_consume(&iter);
6166 touch_nmi_watchdog();
6168 trace_printk_seq(&iter.seq);
6172 printk(KERN_TRACE " (ftrace buffer empty)\n");
6174 printk(KERN_TRACE "---------------------------------\n");
6177 trace_flags |= old_userobj;
6179 for_each_tracing_cpu(cpu) {
6180 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
6182 atomic_dec(&dump_running);
6183 local_irq_restore(flags);
6185 EXPORT_SYMBOL_GPL(ftrace_dump);
6187 __init static int tracer_alloc_buffers(void)
6193 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
6196 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
6197 goto out_free_buffer_mask;
6199 /* Only allocate trace_printk buffers if a trace_printk exists */
6200 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
6201 /* Must be called before global_trace.buffer is allocated */
6202 trace_printk_init_buffers();
6204 /* To save memory, keep the ring buffer size to its minimum */
6205 if (ring_buffer_expanded)
6206 ring_buf_size = trace_buf_size;
6210 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
6211 cpumask_copy(tracing_cpumask, cpu_all_mask);
6213 raw_spin_lock_init(&global_trace.start_lock);
6215 /* TODO: make the number of buffers hot pluggable with CPUS */
6216 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
6217 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
6219 goto out_free_cpumask;
6222 if (global_trace.buffer_disabled)
6225 trace_init_cmdlines();
6228 * register_tracer() might reference current_trace, so it
6229 * needs to be set before we register anything. This is
6230 * just a bootstrap of current_trace anyway.
6232 global_trace.current_trace = &nop_trace;
6234 register_tracer(&nop_trace);
6236 /* All seems OK, enable tracing */
6237 tracing_disabled = 0;
6239 atomic_notifier_chain_register(&panic_notifier_list,
6240 &trace_panic_notifier);
6242 register_die_notifier(&trace_die_notifier);
6244 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
6246 /* Holder for file callbacks */
6247 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
6248 global_trace.trace_cpu.tr = &global_trace;
6250 INIT_LIST_HEAD(&global_trace.systems);
6251 INIT_LIST_HEAD(&global_trace.events);
6252 list_add(&global_trace.list, &ftrace_trace_arrays);
6254 while (trace_boot_options) {
6257 option = strsep(&trace_boot_options, ",");
6258 trace_set_options(&global_trace, option);
6261 register_snapshot_cmd();
6266 free_percpu(global_trace.trace_buffer.data);
6267 #ifdef CONFIG_TRACER_MAX_TRACE
6268 free_percpu(global_trace.max_buffer.data);
6270 free_cpumask_var(tracing_cpumask);
6271 out_free_buffer_mask:
6272 free_cpumask_var(tracing_buffer_mask);
6277 __init static int clear_boot_tracer(void)
6280 * The default tracer at boot buffer is an init section.
6281 * This function is called in lateinit. If we did not
6282 * find the boot tracer, then clear it out, to prevent
6283 * later registration from accessing the buffer that is
6284 * about to be freed.
6286 if (!default_bootup_tracer)
6289 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6290 default_bootup_tracer);
6291 default_bootup_tracer = NULL;
6296 early_initcall(tracer_alloc_buffers);
6297 fs_initcall(tracer_init_debugfs);
6298 late_initcall(clear_boot_tracer);