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[] = {
646 int in_ns; /* is this clock in nanoseconds? */
648 { trace_clock_local, "local", 1 },
649 { trace_clock_global, "global", 1 },
650 { trace_clock_counter, "counter", 0 },
651 { trace_clock_jiffies, "uptime", 1 },
652 { trace_clock, "perf", 1 },
657 * trace_parser_get_init - gets the buffer for trace parser
659 int trace_parser_get_init(struct trace_parser *parser, int size)
661 memset(parser, 0, sizeof(*parser));
663 parser->buffer = kmalloc(size, GFP_KERNEL);
672 * trace_parser_put - frees the buffer for trace parser
674 void trace_parser_put(struct trace_parser *parser)
676 kfree(parser->buffer);
680 * trace_get_user - reads the user input string separated by space
681 * (matched by isspace(ch))
683 * For each string found the 'struct trace_parser' is updated,
684 * and the function returns.
686 * Returns number of bytes read.
688 * See kernel/trace/trace.h for 'struct trace_parser' details.
690 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
691 size_t cnt, loff_t *ppos)
698 trace_parser_clear(parser);
700 ret = get_user(ch, ubuf++);
708 * The parser is not finished with the last write,
709 * continue reading the user input without skipping spaces.
712 /* skip white space */
713 while (cnt && isspace(ch)) {
714 ret = get_user(ch, ubuf++);
721 /* only spaces were written */
731 /* read the non-space input */
732 while (cnt && !isspace(ch)) {
733 if (parser->idx < parser->size - 1)
734 parser->buffer[parser->idx++] = ch;
739 ret = get_user(ch, ubuf++);
746 /* We either got finished input or we have to wait for another call. */
748 parser->buffer[parser->idx] = 0;
749 parser->cont = false;
752 parser->buffer[parser->idx++] = ch;
762 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
770 if (s->len <= s->readpos)
773 len = s->len - s->readpos;
776 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
786 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
790 if (s->len <= s->readpos)
793 len = s->len - s->readpos;
796 memcpy(buf, s->buffer + s->readpos, cnt);
803 * ftrace_max_lock is used to protect the swapping of buffers
804 * when taking a max snapshot. The buffers themselves are
805 * protected by per_cpu spinlocks. But the action of the swap
806 * needs its own lock.
808 * This is defined as a arch_spinlock_t in order to help
809 * with performance when lockdep debugging is enabled.
811 * It is also used in other places outside the update_max_tr
812 * so it needs to be defined outside of the
813 * CONFIG_TRACER_MAX_TRACE.
815 static arch_spinlock_t ftrace_max_lock =
816 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
818 unsigned long __read_mostly tracing_thresh;
820 #ifdef CONFIG_TRACER_MAX_TRACE
821 unsigned long __read_mostly tracing_max_latency;
824 * Copy the new maximum trace into the separate maximum-trace
825 * structure. (this way the maximum trace is permanently saved,
826 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
829 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
831 struct trace_buffer *trace_buf = &tr->trace_buffer;
832 struct trace_buffer *max_buf = &tr->max_buffer;
833 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
834 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
837 max_buf->time_start = data->preempt_timestamp;
839 max_data->saved_latency = tracing_max_latency;
840 max_data->critical_start = data->critical_start;
841 max_data->critical_end = data->critical_end;
843 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
844 max_data->pid = tsk->pid;
846 * If tsk == current, then use current_uid(), as that does not use
847 * RCU. The irq tracer can be called out of RCU scope.
850 max_data->uid = current_uid();
852 max_data->uid = task_uid(tsk);
854 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
855 max_data->policy = tsk->policy;
856 max_data->rt_priority = tsk->rt_priority;
858 /* record this tasks comm */
859 tracing_record_cmdline(tsk);
863 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
865 * @tsk: the task with the latency
866 * @cpu: The cpu that initiated the trace.
868 * Flip the buffers between the @tr and the max_tr and record information
869 * about which task was the cause of this latency.
872 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
874 struct ring_buffer *buf;
879 WARN_ON_ONCE(!irqs_disabled());
881 if (!tr->allocated_snapshot) {
882 /* Only the nop tracer should hit this when disabling */
883 WARN_ON_ONCE(tr->current_trace != &nop_trace);
887 arch_spin_lock(&ftrace_max_lock);
889 buf = tr->trace_buffer.buffer;
890 tr->trace_buffer.buffer = tr->max_buffer.buffer;
891 tr->max_buffer.buffer = buf;
893 __update_max_tr(tr, tsk, cpu);
894 arch_spin_unlock(&ftrace_max_lock);
898 * update_max_tr_single - only copy one trace over, and reset the rest
900 * @tsk - task with the latency
901 * @cpu - the cpu of the buffer to copy.
903 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
906 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
913 WARN_ON_ONCE(!irqs_disabled());
914 if (!tr->allocated_snapshot) {
915 /* Only the nop tracer should hit this when disabling */
916 WARN_ON_ONCE(tr->current_trace != &nop_trace);
920 arch_spin_lock(&ftrace_max_lock);
922 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
926 * We failed to swap the buffer due to a commit taking
927 * place on this CPU. We fail to record, but we reset
928 * the max trace buffer (no one writes directly to it)
929 * and flag that it failed.
931 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
932 "Failed to swap buffers due to commit in progress\n");
935 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
937 __update_max_tr(tr, tsk, cpu);
938 arch_spin_unlock(&ftrace_max_lock);
940 #endif /* CONFIG_TRACER_MAX_TRACE */
942 static void default_wait_pipe(struct trace_iterator *iter)
944 /* Iterators are static, they should be filled or empty */
945 if (trace_buffer_iter(iter, iter->cpu_file))
948 ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
951 #ifdef CONFIG_FTRACE_STARTUP_TEST
952 static int run_tracer_selftest(struct tracer *type)
954 struct trace_array *tr = &global_trace;
955 struct tracer *saved_tracer = tr->current_trace;
958 if (!type->selftest || tracing_selftest_disabled)
962 * Run a selftest on this tracer.
963 * Here we reset the trace buffer, and set the current
964 * tracer to be this tracer. The tracer can then run some
965 * internal tracing to verify that everything is in order.
966 * If we fail, we do not register this tracer.
968 tracing_reset_online_cpus(&tr->trace_buffer);
970 tr->current_trace = type;
972 #ifdef CONFIG_TRACER_MAX_TRACE
973 if (type->use_max_tr) {
974 /* If we expanded the buffers, make sure the max is expanded too */
975 if (ring_buffer_expanded)
976 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
977 RING_BUFFER_ALL_CPUS);
978 tr->allocated_snapshot = true;
982 /* the test is responsible for initializing and enabling */
983 pr_info("Testing tracer %s: ", type->name);
984 ret = type->selftest(type, tr);
985 /* the test is responsible for resetting too */
986 tr->current_trace = saved_tracer;
988 printk(KERN_CONT "FAILED!\n");
989 /* Add the warning after printing 'FAILED' */
993 /* Only reset on passing, to avoid touching corrupted buffers */
994 tracing_reset_online_cpus(&tr->trace_buffer);
996 #ifdef CONFIG_TRACER_MAX_TRACE
997 if (type->use_max_tr) {
998 tr->allocated_snapshot = false;
1000 /* Shrink the max buffer again */
1001 if (ring_buffer_expanded)
1002 ring_buffer_resize(tr->max_buffer.buffer, 1,
1003 RING_BUFFER_ALL_CPUS);
1007 printk(KERN_CONT "PASSED\n");
1011 static inline int run_tracer_selftest(struct tracer *type)
1015 #endif /* CONFIG_FTRACE_STARTUP_TEST */
1018 * register_tracer - register a tracer with the ftrace system.
1019 * @type - the plugin for the tracer
1021 * Register a new plugin tracer.
1023 int register_tracer(struct tracer *type)
1029 pr_info("Tracer must have a name\n");
1033 if (strlen(type->name) >= MAX_TRACER_SIZE) {
1034 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1038 mutex_lock(&trace_types_lock);
1040 tracing_selftest_running = true;
1042 for (t = trace_types; t; t = t->next) {
1043 if (strcmp(type->name, t->name) == 0) {
1045 pr_info("Tracer %s already registered\n",
1052 if (!type->set_flag)
1053 type->set_flag = &dummy_set_flag;
1055 type->flags = &dummy_tracer_flags;
1057 if (!type->flags->opts)
1058 type->flags->opts = dummy_tracer_opt;
1059 if (!type->wait_pipe)
1060 type->wait_pipe = default_wait_pipe;
1062 ret = run_tracer_selftest(type);
1066 type->next = trace_types;
1070 tracing_selftest_running = false;
1071 mutex_unlock(&trace_types_lock);
1073 if (ret || !default_bootup_tracer)
1076 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
1079 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1080 /* Do we want this tracer to start on bootup? */
1081 tracing_set_tracer(type->name);
1082 default_bootup_tracer = NULL;
1083 /* disable other selftests, since this will break it. */
1084 tracing_selftest_disabled = true;
1085 #ifdef CONFIG_FTRACE_STARTUP_TEST
1086 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1094 void tracing_reset(struct trace_buffer *buf, int cpu)
1096 struct ring_buffer *buffer = buf->buffer;
1101 ring_buffer_record_disable(buffer);
1103 /* Make sure all commits have finished */
1104 synchronize_sched();
1105 ring_buffer_reset_cpu(buffer, cpu);
1107 ring_buffer_record_enable(buffer);
1110 void tracing_reset_online_cpus(struct trace_buffer *buf)
1112 struct ring_buffer *buffer = buf->buffer;
1118 ring_buffer_record_disable(buffer);
1120 /* Make sure all commits have finished */
1121 synchronize_sched();
1123 buf->time_start = ftrace_now(buf->cpu);
1125 for_each_online_cpu(cpu)
1126 ring_buffer_reset_cpu(buffer, cpu);
1128 ring_buffer_record_enable(buffer);
1131 void tracing_reset_current(int cpu)
1133 tracing_reset(&global_trace.trace_buffer, cpu);
1136 void tracing_reset_all_online_cpus(void)
1138 struct trace_array *tr;
1140 mutex_lock(&trace_types_lock);
1141 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1142 tracing_reset_online_cpus(&tr->trace_buffer);
1143 #ifdef CONFIG_TRACER_MAX_TRACE
1144 tracing_reset_online_cpus(&tr->max_buffer);
1147 mutex_unlock(&trace_types_lock);
1150 #define SAVED_CMDLINES 128
1151 #define NO_CMDLINE_MAP UINT_MAX
1152 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1153 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1154 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1155 static unsigned saved_tgids[SAVED_CMDLINES];
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);
1359 saved_tgids[idx] = tsk->tgid;
1361 arch_spin_unlock(&trace_cmdline_lock);
1364 void trace_find_cmdline(int pid, char comm[])
1369 strcpy(comm, "<idle>");
1373 if (WARN_ON_ONCE(pid < 0)) {
1374 strcpy(comm, "<XXX>");
1378 if (pid > PID_MAX_DEFAULT) {
1379 strcpy(comm, "<...>");
1384 arch_spin_lock(&trace_cmdline_lock);
1385 map = map_pid_to_cmdline[pid];
1386 if (map != NO_CMDLINE_MAP)
1387 strcpy(comm, saved_cmdlines[map]);
1389 strcpy(comm, "<...>");
1391 arch_spin_unlock(&trace_cmdline_lock);
1395 int trace_find_tgid(int pid)
1401 arch_spin_lock(&trace_cmdline_lock);
1402 map = map_pid_to_cmdline[pid];
1403 if (map != NO_CMDLINE_MAP)
1404 tgid = saved_tgids[map];
1408 arch_spin_unlock(&trace_cmdline_lock);
1414 void tracing_record_cmdline(struct task_struct *tsk)
1416 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1419 if (!__this_cpu_read(trace_cmdline_save))
1422 __this_cpu_write(trace_cmdline_save, false);
1424 trace_save_cmdline(tsk);
1428 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1431 struct task_struct *tsk = current;
1433 entry->preempt_count = pc & 0xff;
1434 entry->pid = (tsk) ? tsk->pid : 0;
1436 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1437 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1439 TRACE_FLAG_IRQS_NOSUPPORT |
1441 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1442 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1443 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1445 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1447 struct ring_buffer_event *
1448 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1451 unsigned long flags, int pc)
1453 struct ring_buffer_event *event;
1455 event = ring_buffer_lock_reserve(buffer, len);
1456 if (event != NULL) {
1457 struct trace_entry *ent = ring_buffer_event_data(event);
1459 tracing_generic_entry_update(ent, flags, pc);
1467 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1469 __this_cpu_write(trace_cmdline_save, true);
1470 ring_buffer_unlock_commit(buffer, event);
1474 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1475 struct ring_buffer_event *event,
1476 unsigned long flags, int pc)
1478 __buffer_unlock_commit(buffer, event);
1480 ftrace_trace_stack(buffer, flags, 6, pc);
1481 ftrace_trace_userstack(buffer, flags, pc);
1484 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1485 struct ring_buffer_event *event,
1486 unsigned long flags, int pc)
1488 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1490 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1492 struct ring_buffer_event *
1493 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1494 struct ftrace_event_file *ftrace_file,
1495 int type, unsigned long len,
1496 unsigned long flags, int pc)
1498 *current_rb = ftrace_file->tr->trace_buffer.buffer;
1499 return trace_buffer_lock_reserve(*current_rb,
1500 type, len, flags, pc);
1502 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1504 struct ring_buffer_event *
1505 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1506 int type, unsigned long len,
1507 unsigned long flags, int pc)
1509 *current_rb = global_trace.trace_buffer.buffer;
1510 return trace_buffer_lock_reserve(*current_rb,
1511 type, len, flags, pc);
1513 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1515 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1516 struct ring_buffer_event *event,
1517 unsigned long flags, int pc)
1519 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1521 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1523 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1524 struct ring_buffer_event *event,
1525 unsigned long flags, int pc,
1526 struct pt_regs *regs)
1528 __buffer_unlock_commit(buffer, event);
1530 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1531 ftrace_trace_userstack(buffer, flags, pc);
1533 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1535 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1536 struct ring_buffer_event *event)
1538 ring_buffer_discard_commit(buffer, event);
1540 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1543 trace_function(struct trace_array *tr,
1544 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1547 struct ftrace_event_call *call = &event_function;
1548 struct ring_buffer *buffer = tr->trace_buffer.buffer;
1549 struct ring_buffer_event *event;
1550 struct ftrace_entry *entry;
1552 /* If we are reading the ring buffer, don't trace */
1553 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1556 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1560 entry = ring_buffer_event_data(event);
1562 entry->parent_ip = parent_ip;
1564 if (!filter_check_discard(call, entry, buffer, event))
1565 __buffer_unlock_commit(buffer, event);
1569 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1570 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1573 if (likely(!atomic_read(&data->disabled)))
1574 trace_function(tr, ip, parent_ip, flags, pc);
1577 #ifdef CONFIG_STACKTRACE
1579 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1580 struct ftrace_stack {
1581 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1584 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1585 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1587 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1588 unsigned long flags,
1589 int skip, int pc, struct pt_regs *regs)
1591 struct ftrace_event_call *call = &event_kernel_stack;
1592 struct ring_buffer_event *event;
1593 struct stack_entry *entry;
1594 struct stack_trace trace;
1596 int size = FTRACE_STACK_ENTRIES;
1598 trace.nr_entries = 0;
1602 * Since events can happen in NMIs there's no safe way to
1603 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1604 * or NMI comes in, it will just have to use the default
1605 * FTRACE_STACK_SIZE.
1607 preempt_disable_notrace();
1609 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1611 * We don't need any atomic variables, just a barrier.
1612 * If an interrupt comes in, we don't care, because it would
1613 * have exited and put the counter back to what we want.
1614 * We just need a barrier to keep gcc from moving things
1618 if (use_stack == 1) {
1619 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1620 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1623 save_stack_trace_regs(regs, &trace);
1625 save_stack_trace(&trace);
1627 if (trace.nr_entries > size)
1628 size = trace.nr_entries;
1630 /* From now on, use_stack is a boolean */
1633 size *= sizeof(unsigned long);
1635 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1636 sizeof(*entry) + size, flags, pc);
1639 entry = ring_buffer_event_data(event);
1641 memset(&entry->caller, 0, size);
1644 memcpy(&entry->caller, trace.entries,
1645 trace.nr_entries * sizeof(unsigned long));
1647 trace.max_entries = FTRACE_STACK_ENTRIES;
1648 trace.entries = entry->caller;
1650 save_stack_trace_regs(regs, &trace);
1652 save_stack_trace(&trace);
1655 entry->size = trace.nr_entries;
1657 if (!filter_check_discard(call, entry, buffer, event))
1658 __buffer_unlock_commit(buffer, event);
1661 /* Again, don't let gcc optimize things here */
1663 __this_cpu_dec(ftrace_stack_reserve);
1664 preempt_enable_notrace();
1668 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1669 int skip, int pc, struct pt_regs *regs)
1671 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1674 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1677 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1680 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1683 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1686 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1689 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
1693 * trace_dump_stack - record a stack back trace in the trace buffer
1694 * @skip: Number of functions to skip (helper handlers)
1696 void trace_dump_stack(int skip)
1698 unsigned long flags;
1700 if (tracing_disabled || tracing_selftest_running)
1703 local_save_flags(flags);
1706 * Skip 3 more, seems to get us at the caller of
1710 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1711 flags, skip, preempt_count(), NULL);
1714 static DEFINE_PER_CPU(int, user_stack_count);
1717 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1719 struct ftrace_event_call *call = &event_user_stack;
1720 struct ring_buffer_event *event;
1721 struct userstack_entry *entry;
1722 struct stack_trace trace;
1724 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1728 * NMIs can not handle page faults, even with fix ups.
1729 * The save user stack can (and often does) fault.
1731 if (unlikely(in_nmi()))
1735 * prevent recursion, since the user stack tracing may
1736 * trigger other kernel events.
1739 if (__this_cpu_read(user_stack_count))
1742 __this_cpu_inc(user_stack_count);
1744 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1745 sizeof(*entry), flags, pc);
1747 goto out_drop_count;
1748 entry = ring_buffer_event_data(event);
1750 entry->tgid = current->tgid;
1751 memset(&entry->caller, 0, sizeof(entry->caller));
1753 trace.nr_entries = 0;
1754 trace.max_entries = FTRACE_STACK_ENTRIES;
1756 trace.entries = entry->caller;
1758 save_stack_trace_user(&trace);
1759 if (!filter_check_discard(call, entry, buffer, event))
1760 __buffer_unlock_commit(buffer, event);
1763 __this_cpu_dec(user_stack_count);
1769 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1771 ftrace_trace_userstack(tr, flags, preempt_count());
1775 #endif /* CONFIG_STACKTRACE */
1777 /* created for use with alloc_percpu */
1778 struct trace_buffer_struct {
1779 char buffer[TRACE_BUF_SIZE];
1782 static struct trace_buffer_struct *trace_percpu_buffer;
1783 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1784 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1785 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1788 * The buffer used is dependent on the context. There is a per cpu
1789 * buffer for normal context, softirq contex, hard irq context and
1790 * for NMI context. Thise allows for lockless recording.
1792 * Note, if the buffers failed to be allocated, then this returns NULL
1794 static char *get_trace_buf(void)
1796 struct trace_buffer_struct *percpu_buffer;
1799 * If we have allocated per cpu buffers, then we do not
1800 * need to do any locking.
1803 percpu_buffer = trace_percpu_nmi_buffer;
1805 percpu_buffer = trace_percpu_irq_buffer;
1806 else if (in_softirq())
1807 percpu_buffer = trace_percpu_sirq_buffer;
1809 percpu_buffer = trace_percpu_buffer;
1814 return this_cpu_ptr(&percpu_buffer->buffer[0]);
1817 static int alloc_percpu_trace_buffer(void)
1819 struct trace_buffer_struct *buffers;
1820 struct trace_buffer_struct *sirq_buffers;
1821 struct trace_buffer_struct *irq_buffers;
1822 struct trace_buffer_struct *nmi_buffers;
1824 buffers = alloc_percpu(struct trace_buffer_struct);
1828 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1832 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1836 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1840 trace_percpu_buffer = buffers;
1841 trace_percpu_sirq_buffer = sirq_buffers;
1842 trace_percpu_irq_buffer = irq_buffers;
1843 trace_percpu_nmi_buffer = nmi_buffers;
1848 free_percpu(irq_buffers);
1850 free_percpu(sirq_buffers);
1852 free_percpu(buffers);
1854 WARN(1, "Could not allocate percpu trace_printk buffer");
1858 static int buffers_allocated;
1860 void trace_printk_init_buffers(void)
1862 if (buffers_allocated)
1865 if (alloc_percpu_trace_buffer())
1868 pr_info("ftrace: Allocated trace_printk buffers\n");
1870 /* Expand the buffers to set size */
1871 tracing_update_buffers();
1873 buffers_allocated = 1;
1876 * trace_printk_init_buffers() can be called by modules.
1877 * If that happens, then we need to start cmdline recording
1878 * directly here. If the global_trace.buffer is already
1879 * allocated here, then this was called by module code.
1881 if (global_trace.trace_buffer.buffer)
1882 tracing_start_cmdline_record();
1885 void trace_printk_start_comm(void)
1887 /* Start tracing comms if trace printk is set */
1888 if (!buffers_allocated)
1890 tracing_start_cmdline_record();
1893 static void trace_printk_start_stop_comm(int enabled)
1895 if (!buffers_allocated)
1899 tracing_start_cmdline_record();
1901 tracing_stop_cmdline_record();
1905 * trace_vbprintk - write binary msg to tracing buffer
1908 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1910 struct ftrace_event_call *call = &event_bprint;
1911 struct ring_buffer_event *event;
1912 struct ring_buffer *buffer;
1913 struct trace_array *tr = &global_trace;
1914 struct bprint_entry *entry;
1915 unsigned long flags;
1917 int len = 0, size, pc;
1919 if (unlikely(tracing_selftest_running || tracing_disabled))
1922 /* Don't pollute graph traces with trace_vprintk internals */
1923 pause_graph_tracing();
1925 pc = preempt_count();
1926 preempt_disable_notrace();
1928 tbuffer = get_trace_buf();
1934 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1936 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1939 local_save_flags(flags);
1940 size = sizeof(*entry) + sizeof(u32) * len;
1941 buffer = tr->trace_buffer.buffer;
1942 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1946 entry = ring_buffer_event_data(event);
1950 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1951 if (!filter_check_discard(call, entry, buffer, event)) {
1952 __buffer_unlock_commit(buffer, event);
1953 ftrace_trace_stack(buffer, flags, 6, pc);
1957 preempt_enable_notrace();
1958 unpause_graph_tracing();
1962 EXPORT_SYMBOL_GPL(trace_vbprintk);
1965 __trace_array_vprintk(struct ring_buffer *buffer,
1966 unsigned long ip, const char *fmt, va_list args)
1968 struct ftrace_event_call *call = &event_print;
1969 struct ring_buffer_event *event;
1970 int len = 0, size, pc;
1971 struct print_entry *entry;
1972 unsigned long flags;
1975 if (tracing_disabled || tracing_selftest_running)
1978 /* Don't pollute graph traces with trace_vprintk internals */
1979 pause_graph_tracing();
1981 pc = preempt_count();
1982 preempt_disable_notrace();
1985 tbuffer = get_trace_buf();
1991 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1992 if (len > TRACE_BUF_SIZE)
1995 local_save_flags(flags);
1996 size = sizeof(*entry) + len + 1;
1997 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
2001 entry = ring_buffer_event_data(event);
2004 memcpy(&entry->buf, tbuffer, len);
2005 entry->buf[len] = '\0';
2006 if (!filter_check_discard(call, entry, buffer, event)) {
2007 __buffer_unlock_commit(buffer, event);
2008 ftrace_trace_stack(buffer, flags, 6, pc);
2011 preempt_enable_notrace();
2012 unpause_graph_tracing();
2017 int trace_array_vprintk(struct trace_array *tr,
2018 unsigned long ip, const char *fmt, va_list args)
2020 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2023 int trace_array_printk(struct trace_array *tr,
2024 unsigned long ip, const char *fmt, ...)
2029 if (!(trace_flags & TRACE_ITER_PRINTK))
2033 ret = trace_array_vprintk(tr, ip, fmt, ap);
2038 int trace_array_printk_buf(struct ring_buffer *buffer,
2039 unsigned long ip, const char *fmt, ...)
2044 if (!(trace_flags & TRACE_ITER_PRINTK))
2048 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2053 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2055 return trace_array_vprintk(&global_trace, ip, fmt, args);
2057 EXPORT_SYMBOL_GPL(trace_vprintk);
2059 static void trace_iterator_increment(struct trace_iterator *iter)
2061 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2065 ring_buffer_read(buf_iter, NULL);
2068 static struct trace_entry *
2069 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2070 unsigned long *lost_events)
2072 struct ring_buffer_event *event;
2073 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
2076 event = ring_buffer_iter_peek(buf_iter, ts);
2078 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
2082 iter->ent_size = ring_buffer_event_length(event);
2083 return ring_buffer_event_data(event);
2089 static struct trace_entry *
2090 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2091 unsigned long *missing_events, u64 *ent_ts)
2093 struct ring_buffer *buffer = iter->trace_buffer->buffer;
2094 struct trace_entry *ent, *next = NULL;
2095 unsigned long lost_events = 0, next_lost = 0;
2096 int cpu_file = iter->cpu_file;
2097 u64 next_ts = 0, ts;
2103 * If we are in a per_cpu trace file, don't bother by iterating over
2104 * all cpu and peek directly.
2106 if (cpu_file > RING_BUFFER_ALL_CPUS) {
2107 if (ring_buffer_empty_cpu(buffer, cpu_file))
2109 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
2111 *ent_cpu = cpu_file;
2116 for_each_tracing_cpu(cpu) {
2118 if (ring_buffer_empty_cpu(buffer, cpu))
2121 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
2124 * Pick the entry with the smallest timestamp:
2126 if (ent && (!next || ts < next_ts)) {
2130 next_lost = lost_events;
2131 next_size = iter->ent_size;
2135 iter->ent_size = next_size;
2138 *ent_cpu = next_cpu;
2144 *missing_events = next_lost;
2149 /* Find the next real entry, without updating the iterator itself */
2150 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2151 int *ent_cpu, u64 *ent_ts)
2153 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
2156 /* Find the next real entry, and increment the iterator to the next entry */
2157 void *trace_find_next_entry_inc(struct trace_iterator *iter)
2159 iter->ent = __find_next_entry(iter, &iter->cpu,
2160 &iter->lost_events, &iter->ts);
2163 trace_iterator_increment(iter);
2165 return iter->ent ? iter : NULL;
2168 static void trace_consume(struct trace_iterator *iter)
2170 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
2171 &iter->lost_events);
2174 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
2176 struct trace_iterator *iter = m->private;
2180 WARN_ON_ONCE(iter->leftover);
2184 /* can't go backwards */
2189 ent = trace_find_next_entry_inc(iter);
2193 while (ent && iter->idx < i)
2194 ent = trace_find_next_entry_inc(iter);
2201 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2203 struct ring_buffer_event *event;
2204 struct ring_buffer_iter *buf_iter;
2205 unsigned long entries = 0;
2208 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2210 buf_iter = trace_buffer_iter(iter, cpu);
2214 ring_buffer_iter_reset(buf_iter);
2217 * We could have the case with the max latency tracers
2218 * that a reset never took place on a cpu. This is evident
2219 * by the timestamp being before the start of the buffer.
2221 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
2222 if (ts >= iter->trace_buffer->time_start)
2225 ring_buffer_read(buf_iter, NULL);
2228 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2232 * The current tracer is copied to avoid a global locking
2235 static void *s_start(struct seq_file *m, loff_t *pos)
2237 struct trace_iterator *iter = m->private;
2238 struct trace_array *tr = iter->tr;
2239 int cpu_file = iter->cpu_file;
2245 * copy the tracer to avoid using a global lock all around.
2246 * iter->trace is a copy of current_trace, the pointer to the
2247 * name may be used instead of a strcmp(), as iter->trace->name
2248 * will point to the same string as current_trace->name.
2250 mutex_lock(&trace_types_lock);
2251 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2252 *iter->trace = *tr->current_trace;
2253 mutex_unlock(&trace_types_lock);
2255 #ifdef CONFIG_TRACER_MAX_TRACE
2256 if (iter->snapshot && iter->trace->use_max_tr)
2257 return ERR_PTR(-EBUSY);
2260 if (!iter->snapshot)
2261 atomic_inc(&trace_record_cmdline_disabled);
2263 if (*pos != iter->pos) {
2268 if (cpu_file == RING_BUFFER_ALL_CPUS) {
2269 for_each_tracing_cpu(cpu)
2270 tracing_iter_reset(iter, cpu);
2272 tracing_iter_reset(iter, cpu_file);
2275 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2280 * If we overflowed the seq_file before, then we want
2281 * to just reuse the trace_seq buffer again.
2287 p = s_next(m, p, &l);
2291 trace_event_read_lock();
2292 trace_access_lock(cpu_file);
2296 static void s_stop(struct seq_file *m, void *p)
2298 struct trace_iterator *iter = m->private;
2300 #ifdef CONFIG_TRACER_MAX_TRACE
2301 if (iter->snapshot && iter->trace->use_max_tr)
2305 if (!iter->snapshot)
2306 atomic_dec(&trace_record_cmdline_disabled);
2308 trace_access_unlock(iter->cpu_file);
2309 trace_event_read_unlock();
2313 get_total_entries(struct trace_buffer *buf,
2314 unsigned long *total, unsigned long *entries)
2316 unsigned long count;
2322 for_each_tracing_cpu(cpu) {
2323 count = ring_buffer_entries_cpu(buf->buffer, cpu);
2325 * If this buffer has skipped entries, then we hold all
2326 * entries for the trace and we need to ignore the
2327 * ones before the time stamp.
2329 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2330 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
2331 /* total is the same as the entries */
2335 ring_buffer_overrun_cpu(buf->buffer, cpu);
2340 static void print_lat_help_header(struct seq_file *m)
2342 seq_puts(m, "# _------=> CPU# \n");
2343 seq_puts(m, "# / _-----=> irqs-off \n");
2344 seq_puts(m, "# | / _----=> need-resched \n");
2345 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2346 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2347 seq_puts(m, "# |||| / delay \n");
2348 seq_puts(m, "# cmd pid ||||| time | caller \n");
2349 seq_puts(m, "# \\ / ||||| \\ | / \n");
2352 static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
2354 unsigned long total;
2355 unsigned long entries;
2357 get_total_entries(buf, &total, &entries);
2358 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2359 entries, total, num_online_cpus());
2363 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
2365 print_event_info(buf, m);
2366 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2367 seq_puts(m, "# | | | | |\n");
2370 static void print_func_help_header_tgid(struct trace_buffer *buf, struct seq_file *m)
2372 print_event_info(buf, m);
2373 seq_puts(m, "# TASK-PID TGID CPU# TIMESTAMP FUNCTION\n");
2374 seq_puts(m, "# | | | | | |\n");
2377 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
2379 print_event_info(buf, m);
2380 seq_puts(m, "# _-----=> irqs-off\n");
2381 seq_puts(m, "# / _----=> need-resched\n");
2382 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2383 seq_puts(m, "# || / _--=> preempt-depth\n");
2384 seq_puts(m, "# ||| / delay\n");
2385 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2386 seq_puts(m, "# | | | |||| | |\n");
2389 static void print_func_help_header_irq_tgid(struct trace_buffer *buf, struct seq_file *m)
2391 print_event_info(buf, m);
2392 seq_puts(m, "# _-----=> irqs-off\n");
2393 seq_puts(m, "# / _----=> need-resched\n");
2394 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2395 seq_puts(m, "# || / _--=> preempt-depth\n");
2396 seq_puts(m, "# ||| / delay\n");
2397 seq_puts(m, "# TASK-PID TGID CPU# |||| TIMESTAMP FUNCTION\n");
2398 seq_puts(m, "# | | | | |||| | |\n");
2402 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2404 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2405 struct trace_buffer *buf = iter->trace_buffer;
2406 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2407 struct tracer *type = iter->trace;
2408 unsigned long entries;
2409 unsigned long total;
2410 const char *name = "preemption";
2414 get_total_entries(buf, &total, &entries);
2416 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2418 seq_puts(m, "# -----------------------------------"
2419 "---------------------------------\n");
2420 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2421 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2422 nsecs_to_usecs(data->saved_latency),
2426 #if defined(CONFIG_PREEMPT_NONE)
2428 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2430 #elif defined(CONFIG_PREEMPT)
2435 /* These are reserved for later use */
2438 seq_printf(m, " #P:%d)\n", num_online_cpus());
2442 seq_puts(m, "# -----------------\n");
2443 seq_printf(m, "# | task: %.16s-%d "
2444 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2445 data->comm, data->pid,
2446 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2447 data->policy, data->rt_priority);
2448 seq_puts(m, "# -----------------\n");
2450 if (data->critical_start) {
2451 seq_puts(m, "# => started at: ");
2452 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2453 trace_print_seq(m, &iter->seq);
2454 seq_puts(m, "\n# => ended at: ");
2455 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2456 trace_print_seq(m, &iter->seq);
2457 seq_puts(m, "\n#\n");
2463 static void test_cpu_buff_start(struct trace_iterator *iter)
2465 struct trace_seq *s = &iter->seq;
2467 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2470 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2473 if (cpumask_test_cpu(iter->cpu, iter->started))
2476 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2479 cpumask_set_cpu(iter->cpu, iter->started);
2481 /* Don't print started cpu buffer for the first entry of the trace */
2483 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2487 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2489 struct trace_seq *s = &iter->seq;
2490 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2491 struct trace_entry *entry;
2492 struct trace_event *event;
2496 test_cpu_buff_start(iter);
2498 event = ftrace_find_event(entry->type);
2500 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2501 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2502 if (!trace_print_lat_context(iter))
2505 if (!trace_print_context(iter))
2511 return event->funcs->trace(iter, sym_flags, event);
2513 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2516 return TRACE_TYPE_HANDLED;
2518 return TRACE_TYPE_PARTIAL_LINE;
2521 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2523 struct trace_seq *s = &iter->seq;
2524 struct trace_entry *entry;
2525 struct trace_event *event;
2529 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2530 if (!trace_seq_printf(s, "%d %d %llu ",
2531 entry->pid, iter->cpu, iter->ts))
2535 event = ftrace_find_event(entry->type);
2537 return event->funcs->raw(iter, 0, event);
2539 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2542 return TRACE_TYPE_HANDLED;
2544 return TRACE_TYPE_PARTIAL_LINE;
2547 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2549 struct trace_seq *s = &iter->seq;
2550 unsigned char newline = '\n';
2551 struct trace_entry *entry;
2552 struct trace_event *event;
2556 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2557 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2558 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2559 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2562 event = ftrace_find_event(entry->type);
2564 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2565 if (ret != TRACE_TYPE_HANDLED)
2569 SEQ_PUT_FIELD_RET(s, newline);
2571 return TRACE_TYPE_HANDLED;
2574 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2576 struct trace_seq *s = &iter->seq;
2577 struct trace_entry *entry;
2578 struct trace_event *event;
2582 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2583 SEQ_PUT_FIELD_RET(s, entry->pid);
2584 SEQ_PUT_FIELD_RET(s, iter->cpu);
2585 SEQ_PUT_FIELD_RET(s, iter->ts);
2588 event = ftrace_find_event(entry->type);
2589 return event ? event->funcs->binary(iter, 0, event) :
2593 int trace_empty(struct trace_iterator *iter)
2595 struct ring_buffer_iter *buf_iter;
2598 /* If we are looking at one CPU buffer, only check that one */
2599 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2600 cpu = iter->cpu_file;
2601 buf_iter = trace_buffer_iter(iter, cpu);
2603 if (!ring_buffer_iter_empty(buf_iter))
2606 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2612 for_each_tracing_cpu(cpu) {
2613 buf_iter = trace_buffer_iter(iter, cpu);
2615 if (!ring_buffer_iter_empty(buf_iter))
2618 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2626 /* Called with trace_event_read_lock() held. */
2627 enum print_line_t print_trace_line(struct trace_iterator *iter)
2629 enum print_line_t ret;
2631 if (iter->lost_events &&
2632 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2633 iter->cpu, iter->lost_events))
2634 return TRACE_TYPE_PARTIAL_LINE;
2636 if (iter->trace && iter->trace->print_line) {
2637 ret = iter->trace->print_line(iter);
2638 if (ret != TRACE_TYPE_UNHANDLED)
2642 if (iter->ent->type == TRACE_BPUTS &&
2643 trace_flags & TRACE_ITER_PRINTK &&
2644 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2645 return trace_print_bputs_msg_only(iter);
2647 if (iter->ent->type == TRACE_BPRINT &&
2648 trace_flags & TRACE_ITER_PRINTK &&
2649 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2650 return trace_print_bprintk_msg_only(iter);
2652 if (iter->ent->type == TRACE_PRINT &&
2653 trace_flags & TRACE_ITER_PRINTK &&
2654 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2655 return trace_print_printk_msg_only(iter);
2657 if (trace_flags & TRACE_ITER_BIN)
2658 return print_bin_fmt(iter);
2660 if (trace_flags & TRACE_ITER_HEX)
2661 return print_hex_fmt(iter);
2663 if (trace_flags & TRACE_ITER_RAW)
2664 return print_raw_fmt(iter);
2666 return print_trace_fmt(iter);
2669 void trace_latency_header(struct seq_file *m)
2671 struct trace_iterator *iter = m->private;
2673 /* print nothing if the buffers are empty */
2674 if (trace_empty(iter))
2677 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2678 print_trace_header(m, iter);
2680 if (!(trace_flags & TRACE_ITER_VERBOSE))
2681 print_lat_help_header(m);
2684 void trace_default_header(struct seq_file *m)
2686 struct trace_iterator *iter = m->private;
2688 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2691 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2692 /* print nothing if the buffers are empty */
2693 if (trace_empty(iter))
2695 print_trace_header(m, iter);
2696 if (!(trace_flags & TRACE_ITER_VERBOSE))
2697 print_lat_help_header(m);
2699 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2700 if (trace_flags & TRACE_ITER_IRQ_INFO)
2701 if (trace_flags & TRACE_ITER_TGID)
2702 print_func_help_header_irq_tgid(iter->trace_buffer, m);
2704 print_func_help_header_irq(iter->trace_buffer, m);
2706 if (trace_flags & TRACE_ITER_TGID)
2707 print_func_help_header_tgid(iter->trace_buffer, m);
2709 print_func_help_header(iter->trace_buffer, m);
2714 static void test_ftrace_alive(struct seq_file *m)
2716 if (!ftrace_is_dead())
2718 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2719 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2722 #ifdef CONFIG_TRACER_MAX_TRACE
2723 static void show_snapshot_main_help(struct seq_file *m)
2725 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2726 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2727 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2728 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2729 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2730 seq_printf(m, "# is not a '0' or '1')\n");
2733 static void show_snapshot_percpu_help(struct seq_file *m)
2735 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2736 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2737 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2738 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2740 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2741 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2743 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2744 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2745 seq_printf(m, "# is not a '0' or '1')\n");
2748 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2750 if (iter->tr->allocated_snapshot)
2751 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2753 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2755 seq_printf(m, "# Snapshot commands:\n");
2756 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2757 show_snapshot_main_help(m);
2759 show_snapshot_percpu_help(m);
2762 /* Should never be called */
2763 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2766 static int s_show(struct seq_file *m, void *v)
2768 struct trace_iterator *iter = v;
2771 if (iter->ent == NULL) {
2773 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2775 test_ftrace_alive(m);
2777 if (iter->snapshot && trace_empty(iter))
2778 print_snapshot_help(m, iter);
2779 else if (iter->trace && iter->trace->print_header)
2780 iter->trace->print_header(m);
2782 trace_default_header(m);
2784 } else if (iter->leftover) {
2786 * If we filled the seq_file buffer earlier, we
2787 * want to just show it now.
2789 ret = trace_print_seq(m, &iter->seq);
2791 /* ret should this time be zero, but you never know */
2792 iter->leftover = ret;
2795 print_trace_line(iter);
2796 ret = trace_print_seq(m, &iter->seq);
2798 * If we overflow the seq_file buffer, then it will
2799 * ask us for this data again at start up.
2801 * ret is 0 if seq_file write succeeded.
2804 iter->leftover = ret;
2810 static const struct seq_operations tracer_seq_ops = {
2817 static struct trace_iterator *
2818 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2820 struct trace_cpu *tc = inode->i_private;
2821 struct trace_array *tr = tc->tr;
2822 struct trace_iterator *iter;
2825 if (tracing_disabled)
2826 return ERR_PTR(-ENODEV);
2828 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2830 return ERR_PTR(-ENOMEM);
2832 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2834 if (!iter->buffer_iter)
2838 * We make a copy of the current tracer to avoid concurrent
2839 * changes on it while we are reading.
2841 mutex_lock(&trace_types_lock);
2842 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2846 *iter->trace = *tr->current_trace;
2848 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2853 #ifdef CONFIG_TRACER_MAX_TRACE
2854 /* Currently only the top directory has a snapshot */
2855 if (tr->current_trace->print_max || snapshot)
2856 iter->trace_buffer = &tr->max_buffer;
2859 iter->trace_buffer = &tr->trace_buffer;
2860 iter->snapshot = snapshot;
2862 mutex_init(&iter->mutex);
2863 iter->cpu_file = tc->cpu;
2865 /* Notify the tracer early; before we stop tracing. */
2866 if (iter->trace && iter->trace->open)
2867 iter->trace->open(iter);
2869 /* Annotate start of buffers if we had overruns */
2870 if (ring_buffer_overruns(iter->trace_buffer->buffer))
2871 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2873 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2874 if (trace_clocks[tr->clock_id].in_ns)
2875 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2877 /* stop the trace while dumping if we are not opening "snapshot" */
2878 if (!iter->snapshot)
2879 tracing_stop_tr(tr);
2881 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
2882 for_each_tracing_cpu(cpu) {
2883 iter->buffer_iter[cpu] =
2884 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2886 ring_buffer_read_prepare_sync();
2887 for_each_tracing_cpu(cpu) {
2888 ring_buffer_read_start(iter->buffer_iter[cpu]);
2889 tracing_iter_reset(iter, cpu);
2892 cpu = iter->cpu_file;
2893 iter->buffer_iter[cpu] =
2894 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2895 ring_buffer_read_prepare_sync();
2896 ring_buffer_read_start(iter->buffer_iter[cpu]);
2897 tracing_iter_reset(iter, cpu);
2902 mutex_unlock(&trace_types_lock);
2907 mutex_unlock(&trace_types_lock);
2909 kfree(iter->buffer_iter);
2911 seq_release_private(inode, file);
2912 return ERR_PTR(-ENOMEM);
2915 int tracing_open_generic(struct inode *inode, struct file *filp)
2917 if (tracing_disabled)
2920 filp->private_data = inode->i_private;
2924 static int tracing_release(struct inode *inode, struct file *file)
2926 struct seq_file *m = file->private_data;
2927 struct trace_iterator *iter;
2928 struct trace_array *tr;
2931 if (!(file->f_mode & FMODE_READ))
2937 mutex_lock(&trace_types_lock);
2942 for_each_tracing_cpu(cpu) {
2943 if (iter->buffer_iter[cpu])
2944 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2947 if (iter->trace && iter->trace->close)
2948 iter->trace->close(iter);
2950 if (!iter->snapshot)
2951 /* reenable tracing if it was previously enabled */
2952 tracing_start_tr(tr);
2953 mutex_unlock(&trace_types_lock);
2955 mutex_destroy(&iter->mutex);
2956 free_cpumask_var(iter->started);
2958 kfree(iter->buffer_iter);
2959 seq_release_private(inode, file);
2963 static int tracing_open(struct inode *inode, struct file *file)
2965 struct trace_iterator *iter;
2968 /* If this file was open for write, then erase contents */
2969 if ((file->f_mode & FMODE_WRITE) &&
2970 (file->f_flags & O_TRUNC)) {
2971 struct trace_cpu *tc = inode->i_private;
2972 struct trace_array *tr = tc->tr;
2974 if (tc->cpu == RING_BUFFER_ALL_CPUS)
2975 tracing_reset_online_cpus(&tr->trace_buffer);
2977 tracing_reset(&tr->trace_buffer, tc->cpu);
2980 if (file->f_mode & FMODE_READ) {
2981 iter = __tracing_open(inode, file, false);
2983 ret = PTR_ERR(iter);
2984 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2985 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2991 t_next(struct seq_file *m, void *v, loff_t *pos)
2993 struct tracer *t = v;
3003 static void *t_start(struct seq_file *m, loff_t *pos)
3008 mutex_lock(&trace_types_lock);
3009 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
3015 static void t_stop(struct seq_file *m, void *p)
3017 mutex_unlock(&trace_types_lock);
3020 static int t_show(struct seq_file *m, void *v)
3022 struct tracer *t = v;
3027 seq_printf(m, "%s", t->name);
3036 static const struct seq_operations show_traces_seq_ops = {
3043 static int show_traces_open(struct inode *inode, struct file *file)
3045 if (tracing_disabled)
3048 return seq_open(file, &show_traces_seq_ops);
3052 tracing_write_stub(struct file *filp, const char __user *ubuf,
3053 size_t count, loff_t *ppos)
3058 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
3060 if (file->f_mode & FMODE_READ)
3061 return seq_lseek(file, offset, origin);
3066 static const struct file_operations tracing_fops = {
3067 .open = tracing_open,
3069 .write = tracing_write_stub,
3070 .llseek = tracing_seek,
3071 .release = tracing_release,
3074 static const struct file_operations show_traces_fops = {
3075 .open = show_traces_open,
3077 .release = seq_release,
3078 .llseek = seq_lseek,
3082 * Only trace on a CPU if the bitmask is set:
3084 static cpumask_var_t tracing_cpumask;
3087 * The tracer itself will not take this lock, but still we want
3088 * to provide a consistent cpumask to user-space:
3090 static DEFINE_MUTEX(tracing_cpumask_update_lock);
3093 * Temporary storage for the character representation of the
3094 * CPU bitmask (and one more byte for the newline):
3096 static char mask_str[NR_CPUS + 1];
3099 tracing_cpumask_read(struct file *filp, char __user *ubuf,
3100 size_t count, loff_t *ppos)
3104 mutex_lock(&tracing_cpumask_update_lock);
3106 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
3107 if (count - len < 2) {
3111 len += sprintf(mask_str + len, "\n");
3112 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3115 mutex_unlock(&tracing_cpumask_update_lock);
3121 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3122 size_t count, loff_t *ppos)
3124 struct trace_array *tr = filp->private_data;
3125 cpumask_var_t tracing_cpumask_new;
3128 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3131 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
3135 mutex_lock(&tracing_cpumask_update_lock);
3137 local_irq_disable();
3138 arch_spin_lock(&ftrace_max_lock);
3139 for_each_tracing_cpu(cpu) {
3141 * Increase/decrease the disabled counter if we are
3142 * about to flip a bit in the cpumask:
3144 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
3145 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3146 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3147 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
3149 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
3150 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3151 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3152 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
3155 arch_spin_unlock(&ftrace_max_lock);
3158 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
3160 mutex_unlock(&tracing_cpumask_update_lock);
3161 free_cpumask_var(tracing_cpumask_new);
3166 free_cpumask_var(tracing_cpumask_new);
3171 static const struct file_operations tracing_cpumask_fops = {
3172 .open = tracing_open_generic,
3173 .read = tracing_cpumask_read,
3174 .write = tracing_cpumask_write,
3175 .llseek = generic_file_llseek,
3178 static int tracing_trace_options_show(struct seq_file *m, void *v)
3180 struct tracer_opt *trace_opts;
3181 struct trace_array *tr = m->private;
3185 mutex_lock(&trace_types_lock);
3186 tracer_flags = tr->current_trace->flags->val;
3187 trace_opts = tr->current_trace->flags->opts;
3189 for (i = 0; trace_options[i]; i++) {
3190 if (trace_flags & (1 << i))
3191 seq_printf(m, "%s\n", trace_options[i]);
3193 seq_printf(m, "no%s\n", trace_options[i]);
3196 for (i = 0; trace_opts[i].name; i++) {
3197 if (tracer_flags & trace_opts[i].bit)
3198 seq_printf(m, "%s\n", trace_opts[i].name);
3200 seq_printf(m, "no%s\n", trace_opts[i].name);
3202 mutex_unlock(&trace_types_lock);
3207 static int __set_tracer_option(struct tracer *trace,
3208 struct tracer_flags *tracer_flags,
3209 struct tracer_opt *opts, int neg)
3213 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
3218 tracer_flags->val &= ~opts->bit;
3220 tracer_flags->val |= opts->bit;
3224 /* Try to assign a tracer specific option */
3225 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3227 struct tracer_flags *tracer_flags = trace->flags;
3228 struct tracer_opt *opts = NULL;
3231 for (i = 0; tracer_flags->opts[i].name; i++) {
3232 opts = &tracer_flags->opts[i];
3234 if (strcmp(cmp, opts->name) == 0)
3235 return __set_tracer_option(trace, trace->flags,
3242 /* Some tracers require overwrite to stay enabled */
3243 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3245 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3251 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
3253 /* do nothing if flag is already set */
3254 if (!!(trace_flags & mask) == !!enabled)
3257 /* Give the tracer a chance to approve the change */
3258 if (tr->current_trace->flag_changed)
3259 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
3263 trace_flags |= mask;
3265 trace_flags &= ~mask;
3267 if (mask == TRACE_ITER_RECORD_CMD)
3268 trace_event_enable_cmd_record(enabled);
3270 if (mask == TRACE_ITER_OVERWRITE) {
3271 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
3272 #ifdef CONFIG_TRACER_MAX_TRACE
3273 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
3277 if (mask == TRACE_ITER_PRINTK)
3278 trace_printk_start_stop_comm(enabled);
3283 static int trace_set_options(struct trace_array *tr, char *option)
3290 cmp = strstrip(option);
3292 if (strncmp(cmp, "no", 2) == 0) {
3297 mutex_lock(&trace_types_lock);
3299 for (i = 0; trace_options[i]; i++) {
3300 if (strcmp(cmp, trace_options[i]) == 0) {
3301 ret = set_tracer_flag(tr, 1 << i, !neg);
3306 /* If no option could be set, test the specific tracer options */
3307 if (!trace_options[i])
3308 ret = set_tracer_option(tr->current_trace, cmp, neg);
3310 mutex_unlock(&trace_types_lock);
3316 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3317 size_t cnt, loff_t *ppos)
3319 struct seq_file *m = filp->private_data;
3320 struct trace_array *tr = m->private;
3324 if (cnt >= sizeof(buf))
3327 if (copy_from_user(&buf, ubuf, cnt))
3332 ret = trace_set_options(tr, buf);
3341 static int tracing_trace_options_open(struct inode *inode, struct file *file)
3343 if (tracing_disabled)
3346 return single_open(file, tracing_trace_options_show, inode->i_private);
3349 static const struct file_operations tracing_iter_fops = {
3350 .open = tracing_trace_options_open,
3352 .llseek = seq_lseek,
3353 .release = single_release,
3354 .write = tracing_trace_options_write,
3357 static const char readme_msg[] =
3358 "tracing mini-HOWTO:\n\n"
3359 "# echo 0 > tracing_on : quick way to disable tracing\n"
3360 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3361 " Important files:\n"
3362 " trace\t\t\t- The static contents of the buffer\n"
3363 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3364 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3365 " current_tracer\t- function and latency tracers\n"
3366 " available_tracers\t- list of configured tracers for current_tracer\n"
3367 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3368 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3369 " trace_clock\t\t-change the clock used to order events\n"
3370 " local: Per cpu clock but may not be synced across CPUs\n"
3371 " global: Synced across CPUs but slows tracing down.\n"
3372 " counter: Not a clock, but just an increment\n"
3373 " uptime: Jiffy counter from time of boot\n"
3374 " perf: Same clock that perf events use\n"
3375 #ifdef CONFIG_X86_64
3376 " x86-tsc: TSC cycle counter\n"
3378 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3379 " tracing_cpumask\t- Limit which CPUs to trace\n"
3380 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3381 "\t\t\t Remove sub-buffer with rmdir\n"
3382 " trace_options\t\t- Set format or modify how tracing happens\n"
3383 "\t\t\t Disable an option by adding a suffix 'no' to the option name\n"
3384 #ifdef CONFIG_DYNAMIC_FTRACE
3385 "\n available_filter_functions - list of functions that can be filtered on\n"
3386 " set_ftrace_filter\t- echo function name in here to only trace these functions\n"
3387 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3388 " modules: Can select a group via module\n"
3389 " Format: :mod:<module-name>\n"
3390 " example: echo :mod:ext3 > set_ftrace_filter\n"
3391 " triggers: a command to perform when function is hit\n"
3392 " Format: <function>:<trigger>[:count]\n"
3393 " trigger: traceon, traceoff\n"
3394 " enable_event:<system>:<event>\n"
3395 " disable_event:<system>:<event>\n"
3396 #ifdef CONFIG_STACKTRACE
3399 #ifdef CONFIG_TRACER_SNAPSHOT
3402 " example: echo do_fault:traceoff > set_ftrace_filter\n"
3403 " echo do_trap:traceoff:3 > set_ftrace_filter\n"
3404 " The first one will disable tracing every time do_fault is hit\n"
3405 " The second will disable tracing at most 3 times when do_trap is hit\n"
3406 " The first time do trap is hit and it disables tracing, the counter\n"
3407 " will decrement to 2. If tracing is already disabled, the counter\n"
3408 " will not decrement. It only decrements when the trigger did work\n"
3409 " To remove trigger without count:\n"
3410 " echo '!<function>:<trigger> > set_ftrace_filter\n"
3411 " To remove trigger with a count:\n"
3412 " echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
3413 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
3414 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3415 " modules: Can select a group via module command :mod:\n"
3416 " Does not accept triggers\n"
3417 #endif /* CONFIG_DYNAMIC_FTRACE */
3418 #ifdef CONFIG_FUNCTION_TRACER
3419 " set_ftrace_pid\t- Write pid(s) to only function trace those pids (function)\n"
3421 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3422 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3423 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3425 #ifdef CONFIG_TRACER_SNAPSHOT
3426 "\n snapshot\t\t- Like 'trace' but shows the content of the static snapshot buffer\n"
3427 "\t\t\t Read the contents for more information\n"
3429 #ifdef CONFIG_STACKTRACE
3430 " stack_trace\t\t- Shows the max stack trace when active\n"
3431 " stack_max_size\t- Shows current max stack size that was traced\n"
3432 "\t\t\t Write into this file to reset the max size (trigger a new trace)\n"
3433 #ifdef CONFIG_DYNAMIC_FTRACE
3434 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace traces\n"
3436 #endif /* CONFIG_STACKTRACE */
3440 tracing_readme_read(struct file *filp, char __user *ubuf,
3441 size_t cnt, loff_t *ppos)
3443 return simple_read_from_buffer(ubuf, cnt, ppos,
3444 readme_msg, strlen(readme_msg));
3447 static const struct file_operations tracing_readme_fops = {
3448 .open = tracing_open_generic,
3449 .read = tracing_readme_read,
3450 .llseek = generic_file_llseek,
3454 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3455 size_t cnt, loff_t *ppos)
3464 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3468 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3476 for (i = 0; i < SAVED_CMDLINES; i++) {
3479 pid = map_cmdline_to_pid[i];
3480 if (pid == -1 || pid == NO_CMDLINE_MAP)
3483 trace_find_cmdline(pid, buf_comm);
3484 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3489 len = simple_read_from_buffer(ubuf, cnt, ppos,
3498 static const struct file_operations tracing_saved_cmdlines_fops = {
3499 .open = tracing_open_generic,
3500 .read = tracing_saved_cmdlines_read,
3501 .llseek = generic_file_llseek,
3505 tracing_saved_tgids_read(struct file *file, char __user *ubuf,
3506 size_t cnt, loff_t *ppos)
3514 file_buf = kmalloc(SAVED_CMDLINES*(16+1+16), GFP_KERNEL);
3520 for (i = 0; i < SAVED_CMDLINES; i++) {
3524 pid = map_cmdline_to_pid[i];
3525 if (pid == -1 || pid == NO_CMDLINE_MAP)
3528 tgid = trace_find_tgid(pid);
3529 r = sprintf(buf, "%d %d\n", pid, tgid);
3534 len = simple_read_from_buffer(ubuf, cnt, ppos,
3542 static const struct file_operations tracing_saved_tgids_fops = {
3543 .open = tracing_open_generic,
3544 .read = tracing_saved_tgids_read,
3545 .llseek = generic_file_llseek,
3549 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3550 size_t cnt, loff_t *ppos)
3552 struct trace_array *tr = filp->private_data;
3553 char buf[MAX_TRACER_SIZE+2];
3556 mutex_lock(&trace_types_lock);
3557 r = sprintf(buf, "%s\n", tr->current_trace->name);
3558 mutex_unlock(&trace_types_lock);
3560 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3563 int tracer_init(struct tracer *t, struct trace_array *tr)
3565 tracing_reset_online_cpus(&tr->trace_buffer);
3569 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
3573 for_each_tracing_cpu(cpu)
3574 per_cpu_ptr(buf->data, cpu)->entries = val;
3577 #ifdef CONFIG_TRACER_MAX_TRACE
3578 /* resize @tr's buffer to the size of @size_tr's entries */
3579 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3580 struct trace_buffer *size_buf, int cpu_id)
3584 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3585 for_each_tracing_cpu(cpu) {
3586 ret = ring_buffer_resize(trace_buf->buffer,
3587 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
3590 per_cpu_ptr(trace_buf->data, cpu)->entries =
3591 per_cpu_ptr(size_buf->data, cpu)->entries;
3594 ret = ring_buffer_resize(trace_buf->buffer,
3595 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
3597 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3598 per_cpu_ptr(size_buf->data, cpu_id)->entries;
3603 #endif /* CONFIG_TRACER_MAX_TRACE */
3605 static int __tracing_resize_ring_buffer(struct trace_array *tr,
3606 unsigned long size, int cpu)
3611 * If kernel or user changes the size of the ring buffer
3612 * we use the size that was given, and we can forget about
3613 * expanding it later.
3615 ring_buffer_expanded = true;
3617 /* May be called before buffers are initialized */
3618 if (!tr->trace_buffer.buffer)
3621 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
3625 #ifdef CONFIG_TRACER_MAX_TRACE
3626 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3627 !tr->current_trace->use_max_tr)
3630 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
3632 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3633 &tr->trace_buffer, cpu);
3636 * AARGH! We are left with different
3637 * size max buffer!!!!
3638 * The max buffer is our "snapshot" buffer.
3639 * When a tracer needs a snapshot (one of the
3640 * latency tracers), it swaps the max buffer
3641 * with the saved snap shot. We succeeded to
3642 * update the size of the main buffer, but failed to
3643 * update the size of the max buffer. But when we tried
3644 * to reset the main buffer to the original size, we
3645 * failed there too. This is very unlikely to
3646 * happen, but if it does, warn and kill all
3650 tracing_disabled = 1;
3655 if (cpu == RING_BUFFER_ALL_CPUS)
3656 set_buffer_entries(&tr->max_buffer, size);
3658 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
3661 #endif /* CONFIG_TRACER_MAX_TRACE */
3663 if (cpu == RING_BUFFER_ALL_CPUS)
3664 set_buffer_entries(&tr->trace_buffer, size);
3666 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
3671 static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3672 unsigned long size, int cpu_id)
3676 mutex_lock(&trace_types_lock);
3678 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3679 /* make sure, this cpu is enabled in the mask */
3680 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3686 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
3691 mutex_unlock(&trace_types_lock);
3698 * tracing_update_buffers - used by tracing facility to expand ring buffers
3700 * To save on memory when the tracing is never used on a system with it
3701 * configured in. The ring buffers are set to a minimum size. But once
3702 * a user starts to use the tracing facility, then they need to grow
3703 * to their default size.
3705 * This function is to be called when a tracer is about to be used.
3707 int tracing_update_buffers(void)
3711 mutex_lock(&trace_types_lock);
3712 if (!ring_buffer_expanded)
3713 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
3714 RING_BUFFER_ALL_CPUS);
3715 mutex_unlock(&trace_types_lock);
3720 struct trace_option_dentry;
3722 static struct trace_option_dentry *
3723 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
3726 destroy_trace_option_files(struct trace_option_dentry *topts);
3728 static int tracing_set_tracer(const char *buf)
3730 static struct trace_option_dentry *topts;
3731 struct trace_array *tr = &global_trace;
3733 #ifdef CONFIG_TRACER_MAX_TRACE
3738 mutex_lock(&trace_types_lock);
3740 if (!ring_buffer_expanded) {
3741 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
3742 RING_BUFFER_ALL_CPUS);
3748 for (t = trace_types; t; t = t->next) {
3749 if (strcmp(t->name, buf) == 0)
3756 if (t == tr->current_trace)
3759 trace_branch_disable();
3761 tr->current_trace->enabled = false;
3763 if (tr->current_trace->reset)
3764 tr->current_trace->reset(tr);
3766 /* Current trace needs to be nop_trace before synchronize_sched */
3767 tr->current_trace = &nop_trace;
3769 #ifdef CONFIG_TRACER_MAX_TRACE
3770 had_max_tr = tr->allocated_snapshot;
3772 if (had_max_tr && !t->use_max_tr) {
3774 * We need to make sure that the update_max_tr sees that
3775 * current_trace changed to nop_trace to keep it from
3776 * swapping the buffers after we resize it.
3777 * The update_max_tr is called from interrupts disabled
3778 * so a synchronized_sched() is sufficient.
3780 synchronize_sched();
3784 destroy_trace_option_files(topts);
3786 topts = create_trace_option_files(tr, t);
3788 #ifdef CONFIG_TRACER_MAX_TRACE
3789 if (t->use_max_tr && !had_max_tr) {
3790 ret = alloc_snapshot(tr);
3797 ret = tracer_init(t, tr);
3802 tr->current_trace = t;
3803 tr->current_trace->enabled = true;
3804 trace_branch_enable(tr);
3806 mutex_unlock(&trace_types_lock);
3812 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3813 size_t cnt, loff_t *ppos)
3815 char buf[MAX_TRACER_SIZE+1];
3822 if (cnt > MAX_TRACER_SIZE)
3823 cnt = MAX_TRACER_SIZE;
3825 if (copy_from_user(&buf, ubuf, cnt))
3830 /* strip ending whitespace. */
3831 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3834 err = tracing_set_tracer(buf);
3844 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3845 size_t cnt, loff_t *ppos)
3847 unsigned long *ptr = filp->private_data;
3851 r = snprintf(buf, sizeof(buf), "%ld\n",
3852 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3853 if (r > sizeof(buf))
3855 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3859 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3860 size_t cnt, loff_t *ppos)
3862 unsigned long *ptr = filp->private_data;
3866 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3875 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3877 struct trace_cpu *tc = inode->i_private;
3878 struct trace_array *tr = tc->tr;
3879 struct trace_iterator *iter;
3882 if (tracing_disabled)
3885 mutex_lock(&trace_types_lock);
3887 /* create a buffer to store the information to pass to userspace */
3888 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3895 * We make a copy of the current tracer to avoid concurrent
3896 * changes on it while we are reading.
3898 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3903 *iter->trace = *tr->current_trace;
3905 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3910 /* trace pipe does not show start of buffer */
3911 cpumask_setall(iter->started);
3913 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3914 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3916 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3917 if (trace_clocks[tr->clock_id].in_ns)
3918 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3920 iter->cpu_file = tc->cpu;
3922 iter->trace_buffer = &tc->tr->trace_buffer;
3923 mutex_init(&iter->mutex);
3924 filp->private_data = iter;
3926 if (iter->trace->pipe_open)
3927 iter->trace->pipe_open(iter);
3929 nonseekable_open(inode, filp);
3931 mutex_unlock(&trace_types_lock);
3937 mutex_unlock(&trace_types_lock);
3941 static int tracing_release_pipe(struct inode *inode, struct file *file)
3943 struct trace_iterator *iter = file->private_data;
3945 mutex_lock(&trace_types_lock);
3947 if (iter->trace->pipe_close)
3948 iter->trace->pipe_close(iter);
3950 mutex_unlock(&trace_types_lock);
3952 free_cpumask_var(iter->started);
3953 mutex_destroy(&iter->mutex);
3961 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
3963 /* Iterators are static, they should be filled or empty */
3964 if (trace_buffer_iter(iter, iter->cpu_file))
3965 return POLLIN | POLLRDNORM;
3967 if (trace_flags & TRACE_ITER_BLOCK)
3969 * Always select as readable when in blocking mode
3971 return POLLIN | POLLRDNORM;
3973 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
3978 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3980 struct trace_iterator *iter = filp->private_data;
3982 return trace_poll(iter, filp, poll_table);
3986 * This is a make-shift waitqueue.
3987 * A tracer might use this callback on some rare cases:
3989 * 1) the current tracer might hold the runqueue lock when it wakes up
3990 * a reader, hence a deadlock (sched, function, and function graph tracers)
3991 * 2) the function tracers, trace all functions, we don't want
3992 * the overhead of calling wake_up and friends
3993 * (and tracing them too)
3995 * Anyway, this is really very primitive wakeup.
3997 void poll_wait_pipe(struct trace_iterator *iter)
3999 set_current_state(TASK_INTERRUPTIBLE);
4000 /* sleep for 100 msecs, and try again. */
4001 schedule_timeout(HZ / 10);
4004 /* Must be called with trace_types_lock mutex held. */
4005 static int tracing_wait_pipe(struct file *filp)
4007 struct trace_iterator *iter = filp->private_data;
4009 while (trace_empty(iter)) {
4011 if ((filp->f_flags & O_NONBLOCK)) {
4015 mutex_unlock(&iter->mutex);
4017 iter->trace->wait_pipe(iter);
4019 mutex_lock(&iter->mutex);
4021 if (signal_pending(current))
4025 * We block until we read something and tracing is disabled.
4026 * We still block if tracing is disabled, but we have never
4027 * read anything. This allows a user to cat this file, and
4028 * then enable tracing. But after we have read something,
4029 * we give an EOF when tracing is again disabled.
4031 * iter->pos will be 0 if we haven't read anything.
4033 if (!tracing_is_enabled() && iter->pos)
4044 tracing_read_pipe(struct file *filp, char __user *ubuf,
4045 size_t cnt, loff_t *ppos)
4047 struct trace_iterator *iter = filp->private_data;
4048 struct trace_array *tr = iter->tr;
4051 /* return any leftover data */
4052 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4056 trace_seq_init(&iter->seq);
4058 /* copy the tracer to avoid using a global lock all around */
4059 mutex_lock(&trace_types_lock);
4060 if (unlikely(iter->trace->name != tr->current_trace->name))
4061 *iter->trace = *tr->current_trace;
4062 mutex_unlock(&trace_types_lock);
4065 * Avoid more than one consumer on a single file descriptor
4066 * This is just a matter of traces coherency, the ring buffer itself
4069 mutex_lock(&iter->mutex);
4070 if (iter->trace->read) {
4071 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
4077 sret = tracing_wait_pipe(filp);
4081 /* stop when tracing is finished */
4082 if (trace_empty(iter)) {
4087 if (cnt >= PAGE_SIZE)
4088 cnt = PAGE_SIZE - 1;
4090 /* reset all but tr, trace, and overruns */
4091 memset(&iter->seq, 0,
4092 sizeof(struct trace_iterator) -
4093 offsetof(struct trace_iterator, seq));
4096 trace_event_read_lock();
4097 trace_access_lock(iter->cpu_file);
4098 while (trace_find_next_entry_inc(iter) != NULL) {
4099 enum print_line_t ret;
4100 int len = iter->seq.len;
4102 ret = print_trace_line(iter);
4103 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4104 /* don't print partial lines */
4105 iter->seq.len = len;
4108 if (ret != TRACE_TYPE_NO_CONSUME)
4109 trace_consume(iter);
4111 if (iter->seq.len >= cnt)
4115 * Setting the full flag means we reached the trace_seq buffer
4116 * size and we should leave by partial output condition above.
4117 * One of the trace_seq_* functions is not used properly.
4119 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
4122 trace_access_unlock(iter->cpu_file);
4123 trace_event_read_unlock();
4125 /* Now copy what we have to the user */
4126 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4127 if (iter->seq.readpos >= iter->seq.len)
4128 trace_seq_init(&iter->seq);
4131 * If there was nothing to send to user, in spite of consuming trace
4132 * entries, go back to wait for more entries.
4138 mutex_unlock(&iter->mutex);
4143 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
4144 struct pipe_buffer *buf)
4146 __free_page(buf->page);
4149 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
4152 __free_page(spd->pages[idx]);
4155 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
4157 .map = generic_pipe_buf_map,
4158 .unmap = generic_pipe_buf_unmap,
4159 .confirm = generic_pipe_buf_confirm,
4160 .release = tracing_pipe_buf_release,
4161 .steal = generic_pipe_buf_steal,
4162 .get = generic_pipe_buf_get,
4166 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
4171 /* Seq buffer is page-sized, exactly what we need. */
4173 count = iter->seq.len;
4174 ret = print_trace_line(iter);
4175 count = iter->seq.len - count;
4178 iter->seq.len -= count;
4181 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4182 iter->seq.len -= count;
4186 if (ret != TRACE_TYPE_NO_CONSUME)
4187 trace_consume(iter);
4189 if (!trace_find_next_entry_inc(iter)) {
4199 static ssize_t tracing_splice_read_pipe(struct file *filp,
4201 struct pipe_inode_info *pipe,
4205 struct page *pages_def[PIPE_DEF_BUFFERS];
4206 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4207 struct trace_iterator *iter = filp->private_data;
4208 struct splice_pipe_desc spd = {
4210 .partial = partial_def,
4211 .nr_pages = 0, /* This gets updated below. */
4212 .nr_pages_max = PIPE_DEF_BUFFERS,
4214 .ops = &tracing_pipe_buf_ops,
4215 .spd_release = tracing_spd_release_pipe,
4217 struct trace_array *tr = iter->tr;
4222 if (splice_grow_spd(pipe, &spd))
4225 /* copy the tracer to avoid using a global lock all around */
4226 mutex_lock(&trace_types_lock);
4227 if (unlikely(iter->trace->name != tr->current_trace->name))
4228 *iter->trace = *tr->current_trace;
4229 mutex_unlock(&trace_types_lock);
4231 mutex_lock(&iter->mutex);
4233 if (iter->trace->splice_read) {
4234 ret = iter->trace->splice_read(iter, filp,
4235 ppos, pipe, len, flags);
4240 ret = tracing_wait_pipe(filp);
4244 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
4249 trace_event_read_lock();
4250 trace_access_lock(iter->cpu_file);
4252 /* Fill as many pages as possible. */
4253 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
4254 spd.pages[i] = alloc_page(GFP_KERNEL);
4258 rem = tracing_fill_pipe_page(rem, iter);
4260 /* Copy the data into the page, so we can start over. */
4261 ret = trace_seq_to_buffer(&iter->seq,
4262 page_address(spd.pages[i]),
4265 __free_page(spd.pages[i]);
4268 spd.partial[i].offset = 0;
4269 spd.partial[i].len = iter->seq.len;
4271 trace_seq_init(&iter->seq);
4274 trace_access_unlock(iter->cpu_file);
4275 trace_event_read_unlock();
4276 mutex_unlock(&iter->mutex);
4280 ret = splice_to_pipe(pipe, &spd);
4282 splice_shrink_spd(&spd);
4286 mutex_unlock(&iter->mutex);
4291 tracing_entries_read(struct file *filp, char __user *ubuf,
4292 size_t cnt, loff_t *ppos)
4294 struct trace_cpu *tc = filp->private_data;
4295 struct trace_array *tr = tc->tr;
4300 mutex_lock(&trace_types_lock);
4302 if (tc->cpu == RING_BUFFER_ALL_CPUS) {
4303 int cpu, buf_size_same;
4308 /* check if all cpu sizes are same */
4309 for_each_tracing_cpu(cpu) {
4310 /* fill in the size from first enabled cpu */
4312 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4313 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
4319 if (buf_size_same) {
4320 if (!ring_buffer_expanded)
4321 r = sprintf(buf, "%lu (expanded: %lu)\n",
4323 trace_buf_size >> 10);
4325 r = sprintf(buf, "%lu\n", size >> 10);
4327 r = sprintf(buf, "X\n");
4329 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, tc->cpu)->entries >> 10);
4331 mutex_unlock(&trace_types_lock);
4333 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4338 tracing_entries_write(struct file *filp, const char __user *ubuf,
4339 size_t cnt, loff_t *ppos)
4341 struct trace_cpu *tc = filp->private_data;
4345 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4349 /* must have at least 1 entry */
4353 /* value is in KB */
4356 ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
4366 tracing_total_entries_read(struct file *filp, char __user *ubuf,
4367 size_t cnt, loff_t *ppos)
4369 struct trace_array *tr = filp->private_data;
4372 unsigned long size = 0, expanded_size = 0;
4374 mutex_lock(&trace_types_lock);
4375 for_each_tracing_cpu(cpu) {
4376 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
4377 if (!ring_buffer_expanded)
4378 expanded_size += trace_buf_size >> 10;
4380 if (ring_buffer_expanded)
4381 r = sprintf(buf, "%lu\n", size);
4383 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4384 mutex_unlock(&trace_types_lock);
4386 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4390 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4391 size_t cnt, loff_t *ppos)
4394 * There is no need to read what the user has written, this function
4395 * is just to make sure that there is no error when "echo" is used
4404 tracing_free_buffer_release(struct inode *inode, struct file *filp)
4406 struct trace_array *tr = inode->i_private;
4408 /* disable tracing ? */
4409 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4411 /* resize the ring buffer to 0 */
4412 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4418 tracing_mark_write(struct file *filp, const char __user *ubuf,
4419 size_t cnt, loff_t *fpos)
4421 unsigned long addr = (unsigned long)ubuf;
4422 struct ring_buffer_event *event;
4423 struct ring_buffer *buffer;
4424 struct print_entry *entry;
4425 unsigned long irq_flags;
4426 struct page *pages[2];
4436 if (tracing_disabled)
4439 if (!(trace_flags & TRACE_ITER_MARKERS))
4442 if (cnt > TRACE_BUF_SIZE)
4443 cnt = TRACE_BUF_SIZE;
4446 * Userspace is injecting traces into the kernel trace buffer.
4447 * We want to be as non intrusive as possible.
4448 * To do so, we do not want to allocate any special buffers
4449 * or take any locks, but instead write the userspace data
4450 * straight into the ring buffer.
4452 * First we need to pin the userspace buffer into memory,
4453 * which, most likely it is, because it just referenced it.
4454 * But there's no guarantee that it is. By using get_user_pages_fast()
4455 * and kmap_atomic/kunmap_atomic() we can get access to the
4456 * pages directly. We then write the data directly into the
4459 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4461 /* check if we cross pages */
4462 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4465 offset = addr & (PAGE_SIZE - 1);
4468 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4469 if (ret < nr_pages) {
4471 put_page(pages[ret]);
4476 for (i = 0; i < nr_pages; i++)
4477 map_page[i] = kmap_atomic(pages[i]);
4479 local_save_flags(irq_flags);
4480 size = sizeof(*entry) + cnt + 2; /* possible \n added */
4481 buffer = global_trace.trace_buffer.buffer;
4482 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4483 irq_flags, preempt_count());
4485 /* Ring buffer disabled, return as if not open for write */
4490 entry = ring_buffer_event_data(event);
4491 entry->ip = _THIS_IP_;
4493 if (nr_pages == 2) {
4494 len = PAGE_SIZE - offset;
4495 memcpy(&entry->buf, map_page[0] + offset, len);
4496 memcpy(&entry->buf[len], map_page[1], cnt - len);
4498 memcpy(&entry->buf, map_page[0] + offset, cnt);
4500 if (entry->buf[cnt - 1] != '\n') {
4501 entry->buf[cnt] = '\n';
4502 entry->buf[cnt + 1] = '\0';
4504 entry->buf[cnt] = '\0';
4506 __buffer_unlock_commit(buffer, event);
4513 for (i = 0; i < nr_pages; i++){
4514 kunmap_atomic(map_page[i]);
4521 static int tracing_clock_show(struct seq_file *m, void *v)
4523 struct trace_array *tr = m->private;
4526 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4528 "%s%s%s%s", i ? " " : "",
4529 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4530 i == tr->clock_id ? "]" : "");
4536 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4537 size_t cnt, loff_t *fpos)
4539 struct seq_file *m = filp->private_data;
4540 struct trace_array *tr = m->private;
4542 const char *clockstr;
4545 if (cnt >= sizeof(buf))
4548 if (copy_from_user(&buf, ubuf, cnt))
4553 clockstr = strstrip(buf);
4555 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4556 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4559 if (i == ARRAY_SIZE(trace_clocks))
4562 mutex_lock(&trace_types_lock);
4566 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
4569 * New clock may not be consistent with the previous clock.
4570 * Reset the buffer so that it doesn't have incomparable timestamps.
4572 tracing_reset_online_cpus(&global_trace.trace_buffer);
4574 #ifdef CONFIG_TRACER_MAX_TRACE
4575 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4576 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4577 tracing_reset_online_cpus(&global_trace.max_buffer);
4580 mutex_unlock(&trace_types_lock);
4587 static int tracing_clock_open(struct inode *inode, struct file *file)
4589 if (tracing_disabled)
4592 return single_open(file, tracing_clock_show, inode->i_private);
4595 struct ftrace_buffer_info {
4596 struct trace_iterator iter;
4601 #ifdef CONFIG_TRACER_SNAPSHOT
4602 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4604 struct trace_cpu *tc = inode->i_private;
4605 struct trace_iterator *iter;
4609 if (file->f_mode & FMODE_READ) {
4610 iter = __tracing_open(inode, file, true);
4612 ret = PTR_ERR(iter);
4614 /* Writes still need the seq_file to hold the private data */
4615 m = kzalloc(sizeof(*m), GFP_KERNEL);
4618 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4624 iter->trace_buffer = &tc->tr->max_buffer;
4625 iter->cpu_file = tc->cpu;
4627 file->private_data = m;
4634 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4637 struct seq_file *m = filp->private_data;
4638 struct trace_iterator *iter = m->private;
4639 struct trace_array *tr = iter->tr;
4643 ret = tracing_update_buffers();
4647 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4651 mutex_lock(&trace_types_lock);
4653 if (tr->current_trace->use_max_tr) {
4660 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4664 if (tr->allocated_snapshot)
4668 /* Only allow per-cpu swap if the ring buffer supports it */
4669 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4670 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4675 if (!tr->allocated_snapshot) {
4676 ret = alloc_snapshot(tr);
4680 local_irq_disable();
4681 /* Now, we're going to swap */
4682 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4683 update_max_tr(tr, current, smp_processor_id());
4685 update_max_tr_single(tr, current, iter->cpu_file);
4689 if (tr->allocated_snapshot) {
4690 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4691 tracing_reset_online_cpus(&tr->max_buffer);
4693 tracing_reset(&tr->max_buffer, iter->cpu_file);
4703 mutex_unlock(&trace_types_lock);
4707 static int tracing_snapshot_release(struct inode *inode, struct file *file)
4709 struct seq_file *m = file->private_data;
4711 if (file->f_mode & FMODE_READ)
4712 return tracing_release(inode, file);
4714 /* If write only, the seq_file is just a stub */
4722 static int tracing_buffers_open(struct inode *inode, struct file *filp);
4723 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
4724 size_t count, loff_t *ppos);
4725 static int tracing_buffers_release(struct inode *inode, struct file *file);
4726 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4727 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
4729 static int snapshot_raw_open(struct inode *inode, struct file *filp)
4731 struct ftrace_buffer_info *info;
4734 ret = tracing_buffers_open(inode, filp);
4738 info = filp->private_data;
4740 if (info->iter.trace->use_max_tr) {
4741 tracing_buffers_release(inode, filp);
4745 info->iter.snapshot = true;
4746 info->iter.trace_buffer = &info->iter.tr->max_buffer;
4751 #endif /* CONFIG_TRACER_SNAPSHOT */
4754 static const struct file_operations tracing_max_lat_fops = {
4755 .open = tracing_open_generic,
4756 .read = tracing_max_lat_read,
4757 .write = tracing_max_lat_write,
4758 .llseek = generic_file_llseek,
4761 static const struct file_operations set_tracer_fops = {
4762 .open = tracing_open_generic,
4763 .read = tracing_set_trace_read,
4764 .write = tracing_set_trace_write,
4765 .llseek = generic_file_llseek,
4768 static const struct file_operations tracing_pipe_fops = {
4769 .open = tracing_open_pipe,
4770 .poll = tracing_poll_pipe,
4771 .read = tracing_read_pipe,
4772 .splice_read = tracing_splice_read_pipe,
4773 .release = tracing_release_pipe,
4774 .llseek = no_llseek,
4777 static const struct file_operations tracing_entries_fops = {
4778 .open = tracing_open_generic,
4779 .read = tracing_entries_read,
4780 .write = tracing_entries_write,
4781 .llseek = generic_file_llseek,
4784 static const struct file_operations tracing_total_entries_fops = {
4785 .open = tracing_open_generic,
4786 .read = tracing_total_entries_read,
4787 .llseek = generic_file_llseek,
4790 static const struct file_operations tracing_free_buffer_fops = {
4791 .write = tracing_free_buffer_write,
4792 .release = tracing_free_buffer_release,
4795 static const struct file_operations tracing_mark_fops = {
4796 .open = tracing_open_generic,
4797 .write = tracing_mark_write,
4798 .llseek = generic_file_llseek,
4801 static const struct file_operations trace_clock_fops = {
4802 .open = tracing_clock_open,
4804 .llseek = seq_lseek,
4805 .release = single_release,
4806 .write = tracing_clock_write,
4809 #ifdef CONFIG_TRACER_SNAPSHOT
4810 static const struct file_operations snapshot_fops = {
4811 .open = tracing_snapshot_open,
4813 .write = tracing_snapshot_write,
4814 .llseek = tracing_seek,
4815 .release = tracing_snapshot_release,
4818 static const struct file_operations snapshot_raw_fops = {
4819 .open = snapshot_raw_open,
4820 .read = tracing_buffers_read,
4821 .release = tracing_buffers_release,
4822 .splice_read = tracing_buffers_splice_read,
4823 .llseek = no_llseek,
4826 #endif /* CONFIG_TRACER_SNAPSHOT */
4828 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4830 struct trace_cpu *tc = inode->i_private;
4831 struct trace_array *tr = tc->tr;
4832 struct ftrace_buffer_info *info;
4834 if (tracing_disabled)
4837 info = kzalloc(sizeof(*info), GFP_KERNEL);
4841 mutex_lock(&trace_types_lock);
4846 info->iter.cpu_file = tc->cpu;
4847 info->iter.trace = tr->current_trace;
4848 info->iter.trace_buffer = &tr->trace_buffer;
4850 /* Force reading ring buffer for first read */
4851 info->read = (unsigned int)-1;
4853 filp->private_data = info;
4855 mutex_unlock(&trace_types_lock);
4857 return nonseekable_open(inode, filp);
4861 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
4863 struct ftrace_buffer_info *info = filp->private_data;
4864 struct trace_iterator *iter = &info->iter;
4866 return trace_poll(iter, filp, poll_table);
4870 tracing_buffers_read(struct file *filp, char __user *ubuf,
4871 size_t count, loff_t *ppos)
4873 struct ftrace_buffer_info *info = filp->private_data;
4874 struct trace_iterator *iter = &info->iter;
4881 mutex_lock(&trace_types_lock);
4883 #ifdef CONFIG_TRACER_MAX_TRACE
4884 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4891 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
4897 /* Do we have previous read data to read? */
4898 if (info->read < PAGE_SIZE)
4902 trace_access_lock(iter->cpu_file);
4903 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
4907 trace_access_unlock(iter->cpu_file);
4910 if (trace_empty(iter)) {
4911 if ((filp->f_flags & O_NONBLOCK)) {
4915 mutex_unlock(&trace_types_lock);
4916 iter->trace->wait_pipe(iter);
4917 mutex_lock(&trace_types_lock);
4918 if (signal_pending(current)) {
4930 size = PAGE_SIZE - info->read;
4934 ret = copy_to_user(ubuf, info->spare + info->read, size);
4945 mutex_unlock(&trace_types_lock);
4950 static int tracing_buffers_release(struct inode *inode, struct file *file)
4952 struct ftrace_buffer_info *info = file->private_data;
4953 struct trace_iterator *iter = &info->iter;
4955 mutex_lock(&trace_types_lock);
4957 WARN_ON(!iter->tr->ref);
4961 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
4964 mutex_unlock(&trace_types_lock);
4970 struct ring_buffer *buffer;
4975 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4976 struct pipe_buffer *buf)
4978 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4983 ring_buffer_free_read_page(ref->buffer, ref->page);
4988 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4989 struct pipe_buffer *buf)
4991 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4996 /* Pipe buffer operations for a buffer. */
4997 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4999 .map = generic_pipe_buf_map,
5000 .unmap = generic_pipe_buf_unmap,
5001 .confirm = generic_pipe_buf_confirm,
5002 .release = buffer_pipe_buf_release,
5003 .steal = generic_pipe_buf_steal,
5004 .get = buffer_pipe_buf_get,
5008 * Callback from splice_to_pipe(), if we need to release some pages
5009 * at the end of the spd in case we error'ed out in filling the pipe.
5011 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
5013 struct buffer_ref *ref =
5014 (struct buffer_ref *)spd->partial[i].private;
5019 ring_buffer_free_read_page(ref->buffer, ref->page);
5021 spd->partial[i].private = 0;
5025 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5026 struct pipe_inode_info *pipe, size_t len,
5029 struct ftrace_buffer_info *info = file->private_data;
5030 struct trace_iterator *iter = &info->iter;
5031 struct partial_page partial_def[PIPE_DEF_BUFFERS];
5032 struct page *pages_def[PIPE_DEF_BUFFERS];
5033 struct splice_pipe_desc spd = {
5035 .partial = partial_def,
5036 .nr_pages_max = PIPE_DEF_BUFFERS,
5038 .ops = &buffer_pipe_buf_ops,
5039 .spd_release = buffer_spd_release,
5041 struct buffer_ref *ref;
5042 int entries, size, i;
5045 mutex_lock(&trace_types_lock);
5047 #ifdef CONFIG_TRACER_MAX_TRACE
5048 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5054 if (splice_grow_spd(pipe, &spd)) {
5059 if (*ppos & (PAGE_SIZE - 1)) {
5064 if (len & (PAGE_SIZE - 1)) {
5065 if (len < PAGE_SIZE) {
5073 trace_access_lock(iter->cpu_file);
5074 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5076 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
5080 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
5085 ref->buffer = iter->trace_buffer->buffer;
5086 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
5092 r = ring_buffer_read_page(ref->buffer, &ref->page,
5093 len, iter->cpu_file, 1);
5095 ring_buffer_free_read_page(ref->buffer, ref->page);
5101 * zero out any left over data, this is going to
5104 size = ring_buffer_page_len(ref->page);
5105 if (size < PAGE_SIZE)
5106 memset(ref->page + size, 0, PAGE_SIZE - size);
5108 page = virt_to_page(ref->page);
5110 spd.pages[i] = page;
5111 spd.partial[i].len = PAGE_SIZE;
5112 spd.partial[i].offset = 0;
5113 spd.partial[i].private = (unsigned long)ref;
5117 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5120 trace_access_unlock(iter->cpu_file);
5123 /* did we read anything? */
5124 if (!spd.nr_pages) {
5125 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
5129 mutex_unlock(&trace_types_lock);
5130 iter->trace->wait_pipe(iter);
5131 mutex_lock(&trace_types_lock);
5132 if (signal_pending(current)) {
5139 ret = splice_to_pipe(pipe, &spd);
5140 splice_shrink_spd(&spd);
5142 mutex_unlock(&trace_types_lock);
5147 static const struct file_operations tracing_buffers_fops = {
5148 .open = tracing_buffers_open,
5149 .read = tracing_buffers_read,
5150 .poll = tracing_buffers_poll,
5151 .release = tracing_buffers_release,
5152 .splice_read = tracing_buffers_splice_read,
5153 .llseek = no_llseek,
5157 tracing_stats_read(struct file *filp, char __user *ubuf,
5158 size_t count, loff_t *ppos)
5160 struct trace_cpu *tc = filp->private_data;
5161 struct trace_array *tr = tc->tr;
5162 struct trace_buffer *trace_buf = &tr->trace_buffer;
5163 struct trace_seq *s;
5165 unsigned long long t;
5166 unsigned long usec_rem;
5169 s = kmalloc(sizeof(*s), GFP_KERNEL);
5175 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
5176 trace_seq_printf(s, "entries: %ld\n", cnt);
5178 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
5179 trace_seq_printf(s, "overrun: %ld\n", cnt);
5181 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
5182 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5184 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
5185 trace_seq_printf(s, "bytes: %ld\n", cnt);
5187 if (trace_clocks[tr->clock_id].in_ns) {
5188 /* local or global for trace_clock */
5189 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5190 usec_rem = do_div(t, USEC_PER_SEC);
5191 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5194 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
5195 usec_rem = do_div(t, USEC_PER_SEC);
5196 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5198 /* counter or tsc mode for trace_clock */
5199 trace_seq_printf(s, "oldest event ts: %llu\n",
5200 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5202 trace_seq_printf(s, "now ts: %llu\n",
5203 ring_buffer_time_stamp(trace_buf->buffer, cpu));
5206 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
5207 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5209 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
5210 trace_seq_printf(s, "read events: %ld\n", cnt);
5212 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
5219 static const struct file_operations tracing_stats_fops = {
5220 .open = tracing_open_generic,
5221 .read = tracing_stats_read,
5222 .llseek = generic_file_llseek,
5225 #ifdef CONFIG_DYNAMIC_FTRACE
5227 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
5233 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
5234 size_t cnt, loff_t *ppos)
5236 static char ftrace_dyn_info_buffer[1024];
5237 static DEFINE_MUTEX(dyn_info_mutex);
5238 unsigned long *p = filp->private_data;
5239 char *buf = ftrace_dyn_info_buffer;
5240 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
5243 mutex_lock(&dyn_info_mutex);
5244 r = sprintf(buf, "%ld ", *p);
5246 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
5249 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5251 mutex_unlock(&dyn_info_mutex);
5256 static const struct file_operations tracing_dyn_info_fops = {
5257 .open = tracing_open_generic,
5258 .read = tracing_read_dyn_info,
5259 .llseek = generic_file_llseek,
5261 #endif /* CONFIG_DYNAMIC_FTRACE */
5263 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5265 ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5271 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5273 unsigned long *count = (long *)data;
5285 ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5286 struct ftrace_probe_ops *ops, void *data)
5288 long count = (long)data;
5290 seq_printf(m, "%ps:", (void *)ip);
5292 seq_printf(m, "snapshot");
5295 seq_printf(m, ":unlimited\n");
5297 seq_printf(m, ":count=%ld\n", count);
5302 static struct ftrace_probe_ops snapshot_probe_ops = {
5303 .func = ftrace_snapshot,
5304 .print = ftrace_snapshot_print,
5307 static struct ftrace_probe_ops snapshot_count_probe_ops = {
5308 .func = ftrace_count_snapshot,
5309 .print = ftrace_snapshot_print,
5313 ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5314 char *glob, char *cmd, char *param, int enable)
5316 struct ftrace_probe_ops *ops;
5317 void *count = (void *)-1;
5321 /* hash funcs only work with set_ftrace_filter */
5325 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
5327 if (glob[0] == '!') {
5328 unregister_ftrace_function_probe_func(glob+1, ops);
5335 number = strsep(¶m, ":");
5337 if (!strlen(number))
5341 * We use the callback data field (which is a pointer)
5344 ret = kstrtoul(number, 0, (unsigned long *)&count);
5349 ret = register_ftrace_function_probe(glob, ops, count);
5352 alloc_snapshot(&global_trace);
5354 return ret < 0 ? ret : 0;
5357 static struct ftrace_func_command ftrace_snapshot_cmd = {
5359 .func = ftrace_trace_snapshot_callback,
5362 static int register_snapshot_cmd(void)
5364 return register_ftrace_command(&ftrace_snapshot_cmd);
5367 static inline int register_snapshot_cmd(void) { return 0; }
5368 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
5370 struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
5375 if (!debugfs_initialized())
5378 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5379 tr->dir = debugfs_create_dir("tracing", NULL);
5382 pr_warn_once("Could not create debugfs directory 'tracing'\n");
5387 struct dentry *tracing_init_dentry(void)
5389 return tracing_init_dentry_tr(&global_trace);
5392 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
5394 struct dentry *d_tracer;
5397 return tr->percpu_dir;
5399 d_tracer = tracing_init_dentry_tr(tr);
5403 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
5405 WARN_ONCE(!tr->percpu_dir,
5406 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
5408 return tr->percpu_dir;
5412 tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
5414 struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
5415 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5416 struct dentry *d_cpu;
5417 char cpu_dir[30]; /* 30 characters should be more than enough */
5422 snprintf(cpu_dir, 30, "cpu%ld", cpu);
5423 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5425 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5429 /* per cpu trace_pipe */
5430 trace_create_file("trace_pipe", 0444, d_cpu,
5431 (void *)&data->trace_cpu, &tracing_pipe_fops);
5434 trace_create_file("trace", 0644, d_cpu,
5435 (void *)&data->trace_cpu, &tracing_fops);
5437 trace_create_file("trace_pipe_raw", 0444, d_cpu,
5438 (void *)&data->trace_cpu, &tracing_buffers_fops);
5440 trace_create_file("stats", 0444, d_cpu,
5441 (void *)&data->trace_cpu, &tracing_stats_fops);
5443 trace_create_file("buffer_size_kb", 0444, d_cpu,
5444 (void *)&data->trace_cpu, &tracing_entries_fops);
5446 #ifdef CONFIG_TRACER_SNAPSHOT
5447 trace_create_file("snapshot", 0644, d_cpu,
5448 (void *)&data->trace_cpu, &snapshot_fops);
5450 trace_create_file("snapshot_raw", 0444, d_cpu,
5451 (void *)&data->trace_cpu, &snapshot_raw_fops);
5455 #ifdef CONFIG_FTRACE_SELFTEST
5456 /* Let selftest have access to static functions in this file */
5457 #include "trace_selftest.c"
5460 struct trace_option_dentry {
5461 struct tracer_opt *opt;
5462 struct tracer_flags *flags;
5463 struct trace_array *tr;
5464 struct dentry *entry;
5468 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5471 struct trace_option_dentry *topt = filp->private_data;
5474 if (topt->flags->val & topt->opt->bit)
5479 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5483 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5486 struct trace_option_dentry *topt = filp->private_data;
5490 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5494 if (val != 0 && val != 1)
5497 if (!!(topt->flags->val & topt->opt->bit) != val) {
5498 mutex_lock(&trace_types_lock);
5499 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
5501 mutex_unlock(&trace_types_lock);
5512 static const struct file_operations trace_options_fops = {
5513 .open = tracing_open_generic,
5514 .read = trace_options_read,
5515 .write = trace_options_write,
5516 .llseek = generic_file_llseek,
5520 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5523 long index = (long)filp->private_data;
5526 if (trace_flags & (1 << index))
5531 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5535 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5538 struct trace_array *tr = &global_trace;
5539 long index = (long)filp->private_data;
5543 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5547 if (val != 0 && val != 1)
5550 mutex_lock(&trace_types_lock);
5551 ret = set_tracer_flag(tr, 1 << index, val);
5552 mutex_unlock(&trace_types_lock);
5562 static const struct file_operations trace_options_core_fops = {
5563 .open = tracing_open_generic,
5564 .read = trace_options_core_read,
5565 .write = trace_options_core_write,
5566 .llseek = generic_file_llseek,
5569 struct dentry *trace_create_file(const char *name,
5571 struct dentry *parent,
5573 const struct file_operations *fops)
5577 ret = debugfs_create_file(name, mode, parent, data, fops);
5579 pr_warning("Could not create debugfs '%s' entry\n", name);
5585 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
5587 struct dentry *d_tracer;
5592 d_tracer = tracing_init_dentry_tr(tr);
5596 tr->options = debugfs_create_dir("options", d_tracer);
5598 pr_warning("Could not create debugfs directory 'options'\n");
5606 create_trace_option_file(struct trace_array *tr,
5607 struct trace_option_dentry *topt,
5608 struct tracer_flags *flags,
5609 struct tracer_opt *opt)
5611 struct dentry *t_options;
5613 t_options = trace_options_init_dentry(tr);
5617 topt->flags = flags;
5621 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
5622 &trace_options_fops);
5626 static struct trace_option_dentry *
5627 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
5629 struct trace_option_dentry *topts;
5630 struct tracer_flags *flags;
5631 struct tracer_opt *opts;
5637 flags = tracer->flags;
5639 if (!flags || !flags->opts)
5644 for (cnt = 0; opts[cnt].name; cnt++)
5647 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
5651 for (cnt = 0; opts[cnt].name; cnt++)
5652 create_trace_option_file(tr, &topts[cnt], flags,
5659 destroy_trace_option_files(struct trace_option_dentry *topts)
5666 for (cnt = 0; topts[cnt].opt; cnt++) {
5667 if (topts[cnt].entry)
5668 debugfs_remove(topts[cnt].entry);
5674 static struct dentry *
5675 create_trace_option_core_file(struct trace_array *tr,
5676 const char *option, long index)
5678 struct dentry *t_options;
5680 t_options = trace_options_init_dentry(tr);
5684 return trace_create_file(option, 0644, t_options, (void *)index,
5685 &trace_options_core_fops);
5688 static __init void create_trace_options_dir(struct trace_array *tr)
5690 struct dentry *t_options;
5693 t_options = trace_options_init_dentry(tr);
5697 for (i = 0; trace_options[i]; i++)
5698 create_trace_option_core_file(tr, trace_options[i], i);
5702 rb_simple_read(struct file *filp, char __user *ubuf,
5703 size_t cnt, loff_t *ppos)
5705 struct trace_array *tr = filp->private_data;
5706 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5711 r = ring_buffer_record_is_on(buffer);
5715 r = sprintf(buf, "%d\n", r);
5717 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5721 rb_simple_write(struct file *filp, const char __user *ubuf,
5722 size_t cnt, loff_t *ppos)
5724 struct trace_array *tr = filp->private_data;
5725 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5729 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5734 mutex_lock(&trace_types_lock);
5736 ring_buffer_record_on(buffer);
5737 if (tr->current_trace->start)
5738 tr->current_trace->start(tr);
5740 ring_buffer_record_off(buffer);
5741 if (tr->current_trace->stop)
5742 tr->current_trace->stop(tr);
5744 mutex_unlock(&trace_types_lock);
5752 static const struct file_operations rb_simple_fops = {
5753 .open = tracing_open_generic,
5754 .read = rb_simple_read,
5755 .write = rb_simple_write,
5756 .llseek = default_llseek,
5759 struct dentry *trace_instance_dir;
5762 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5764 static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
5768 for_each_tracing_cpu(cpu) {
5769 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
5770 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
5771 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
5776 allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
5778 enum ring_buffer_flags rb_flags;
5780 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5782 buf->buffer = ring_buffer_alloc(size, rb_flags);
5786 buf->data = alloc_percpu(struct trace_array_cpu);
5788 ring_buffer_free(buf->buffer);
5792 init_trace_buffers(tr, buf);
5794 /* Allocate the first page for all buffers */
5795 set_buffer_entries(&tr->trace_buffer,
5796 ring_buffer_size(tr->trace_buffer.buffer, 0));
5801 static int allocate_trace_buffers(struct trace_array *tr, int size)
5805 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
5809 #ifdef CONFIG_TRACER_MAX_TRACE
5810 ret = allocate_trace_buffer(tr, &tr->max_buffer,
5811 allocate_snapshot ? size : 1);
5813 ring_buffer_free(tr->trace_buffer.buffer);
5814 free_percpu(tr->trace_buffer.data);
5817 tr->allocated_snapshot = allocate_snapshot;
5820 * Only the top level trace array gets its snapshot allocated
5821 * from the kernel command line.
5823 allocate_snapshot = false;
5828 static int new_instance_create(const char *name)
5830 struct trace_array *tr;
5833 mutex_lock(&trace_types_lock);
5836 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5837 if (tr->name && strcmp(tr->name, name) == 0)
5842 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
5846 tr->name = kstrdup(name, GFP_KERNEL);
5850 raw_spin_lock_init(&tr->start_lock);
5852 tr->current_trace = &nop_trace;
5854 INIT_LIST_HEAD(&tr->systems);
5855 INIT_LIST_HEAD(&tr->events);
5857 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
5860 /* Holder for file callbacks */
5861 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5862 tr->trace_cpu.tr = tr;
5864 tr->dir = debugfs_create_dir(name, trace_instance_dir);
5868 ret = event_trace_add_tracer(tr->dir, tr);
5872 init_tracer_debugfs(tr, tr->dir);
5874 list_add(&tr->list, &ftrace_trace_arrays);
5876 mutex_unlock(&trace_types_lock);
5881 if (tr->trace_buffer.buffer)
5882 ring_buffer_free(tr->trace_buffer.buffer);
5887 mutex_unlock(&trace_types_lock);
5893 static int instance_delete(const char *name)
5895 struct trace_array *tr;
5899 mutex_lock(&trace_types_lock);
5902 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5903 if (tr->name && strcmp(tr->name, name) == 0) {
5915 list_del(&tr->list);
5917 event_trace_del_tracer(tr);
5918 debugfs_remove_recursive(tr->dir);
5919 free_percpu(tr->trace_buffer.data);
5920 ring_buffer_free(tr->trace_buffer.buffer);
5928 mutex_unlock(&trace_types_lock);
5933 static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
5935 struct dentry *parent;
5938 /* Paranoid: Make sure the parent is the "instances" directory */
5939 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5940 if (WARN_ON_ONCE(parent != trace_instance_dir))
5944 * The inode mutex is locked, but debugfs_create_dir() will also
5945 * take the mutex. As the instances directory can not be destroyed
5946 * or changed in any other way, it is safe to unlock it, and
5947 * let the dentry try. If two users try to make the same dir at
5948 * the same time, then the new_instance_create() will determine the
5951 mutex_unlock(&inode->i_mutex);
5953 ret = new_instance_create(dentry->d_iname);
5955 mutex_lock(&inode->i_mutex);
5960 static int instance_rmdir(struct inode *inode, struct dentry *dentry)
5962 struct dentry *parent;
5965 /* Paranoid: Make sure the parent is the "instances" directory */
5966 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5967 if (WARN_ON_ONCE(parent != trace_instance_dir))
5970 /* The caller did a dget() on dentry */
5971 mutex_unlock(&dentry->d_inode->i_mutex);
5974 * The inode mutex is locked, but debugfs_create_dir() will also
5975 * take the mutex. As the instances directory can not be destroyed
5976 * or changed in any other way, it is safe to unlock it, and
5977 * let the dentry try. If two users try to make the same dir at
5978 * the same time, then the instance_delete() will determine the
5981 mutex_unlock(&inode->i_mutex);
5983 ret = instance_delete(dentry->d_iname);
5985 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
5986 mutex_lock(&dentry->d_inode->i_mutex);
5991 static const struct inode_operations instance_dir_inode_operations = {
5992 .lookup = simple_lookup,
5993 .mkdir = instance_mkdir,
5994 .rmdir = instance_rmdir,
5997 static __init void create_trace_instances(struct dentry *d_tracer)
5999 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
6000 if (WARN_ON(!trace_instance_dir))
6003 /* Hijack the dir inode operations, to allow mkdir */
6004 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
6008 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
6012 trace_create_file("trace_options", 0644, d_tracer,
6013 tr, &tracing_iter_fops);
6015 trace_create_file("trace", 0644, d_tracer,
6016 (void *)&tr->trace_cpu, &tracing_fops);
6018 trace_create_file("trace_pipe", 0444, d_tracer,
6019 (void *)&tr->trace_cpu, &tracing_pipe_fops);
6021 trace_create_file("buffer_size_kb", 0644, d_tracer,
6022 (void *)&tr->trace_cpu, &tracing_entries_fops);
6024 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
6025 tr, &tracing_total_entries_fops);
6027 trace_create_file("free_buffer", 0644, d_tracer,
6028 tr, &tracing_free_buffer_fops);
6030 trace_create_file("trace_marker", 0220, d_tracer,
6031 tr, &tracing_mark_fops);
6033 trace_create_file("saved_tgids", 0444, d_tracer,
6034 tr, &tracing_saved_tgids_fops);
6036 trace_create_file("trace_clock", 0644, d_tracer, tr,
6039 trace_create_file("tracing_on", 0644, d_tracer,
6040 tr, &rb_simple_fops);
6042 #ifdef CONFIG_TRACER_SNAPSHOT
6043 trace_create_file("snapshot", 0644, d_tracer,
6044 (void *)&tr->trace_cpu, &snapshot_fops);
6047 for_each_tracing_cpu(cpu)
6048 tracing_init_debugfs_percpu(tr, cpu);
6052 static __init int tracer_init_debugfs(void)
6054 struct dentry *d_tracer;
6056 trace_access_lock_init();
6058 d_tracer = tracing_init_dentry();
6062 init_tracer_debugfs(&global_trace, d_tracer);
6064 trace_create_file("tracing_cpumask", 0644, d_tracer,
6065 &global_trace, &tracing_cpumask_fops);
6067 trace_create_file("available_tracers", 0444, d_tracer,
6068 &global_trace, &show_traces_fops);
6070 trace_create_file("current_tracer", 0644, d_tracer,
6071 &global_trace, &set_tracer_fops);
6073 #ifdef CONFIG_TRACER_MAX_TRACE
6074 trace_create_file("tracing_max_latency", 0644, d_tracer,
6075 &tracing_max_latency, &tracing_max_lat_fops);
6078 trace_create_file("tracing_thresh", 0644, d_tracer,
6079 &tracing_thresh, &tracing_max_lat_fops);
6081 trace_create_file("README", 0444, d_tracer,
6082 NULL, &tracing_readme_fops);
6084 trace_create_file("saved_cmdlines", 0444, d_tracer,
6085 NULL, &tracing_saved_cmdlines_fops);
6087 #ifdef CONFIG_DYNAMIC_FTRACE
6088 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
6089 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
6092 create_trace_instances(d_tracer);
6094 create_trace_options_dir(&global_trace);
6099 static int trace_panic_handler(struct notifier_block *this,
6100 unsigned long event, void *unused)
6102 if (ftrace_dump_on_oops)
6103 ftrace_dump(ftrace_dump_on_oops);
6107 static struct notifier_block trace_panic_notifier = {
6108 .notifier_call = trace_panic_handler,
6110 .priority = 150 /* priority: INT_MAX >= x >= 0 */
6113 static int trace_die_handler(struct notifier_block *self,
6119 if (ftrace_dump_on_oops)
6120 ftrace_dump(ftrace_dump_on_oops);
6128 static struct notifier_block trace_die_notifier = {
6129 .notifier_call = trace_die_handler,
6134 * printk is set to max of 1024, we really don't need it that big.
6135 * Nothing should be printing 1000 characters anyway.
6137 #define TRACE_MAX_PRINT 1000
6140 * Define here KERN_TRACE so that we have one place to modify
6141 * it if we decide to change what log level the ftrace dump
6144 #define KERN_TRACE KERN_EMERG
6147 trace_printk_seq(struct trace_seq *s)
6149 /* Probably should print a warning here. */
6150 if (s->len >= TRACE_MAX_PRINT)
6151 s->len = TRACE_MAX_PRINT;
6153 /* should be zero ended, but we are paranoid. */
6154 s->buffer[s->len] = 0;
6156 printk(KERN_TRACE "%s", s->buffer);
6161 void trace_init_global_iter(struct trace_iterator *iter)
6163 iter->tr = &global_trace;
6164 iter->trace = iter->tr->current_trace;
6165 iter->cpu_file = RING_BUFFER_ALL_CPUS;
6166 iter->trace_buffer = &global_trace.trace_buffer;
6169 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
6171 /* use static because iter can be a bit big for the stack */
6172 static struct trace_iterator iter;
6173 static atomic_t dump_running;
6174 unsigned int old_userobj;
6175 unsigned long flags;
6178 /* Only allow one dump user at a time. */
6179 if (atomic_inc_return(&dump_running) != 1) {
6180 atomic_dec(&dump_running);
6185 * Always turn off tracing when we dump.
6186 * We don't need to show trace output of what happens
6187 * between multiple crashes.
6189 * If the user does a sysrq-z, then they can re-enable
6190 * tracing with echo 1 > tracing_on.
6194 local_irq_save(flags);
6196 /* Simulate the iterator */
6197 trace_init_global_iter(&iter);
6199 for_each_tracing_cpu(cpu) {
6200 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
6203 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
6205 /* don't look at user memory in panic mode */
6206 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
6208 switch (oops_dump_mode) {
6210 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6213 iter.cpu_file = raw_smp_processor_id();
6218 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
6219 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6222 printk(KERN_TRACE "Dumping ftrace buffer:\n");
6224 /* Did function tracer already get disabled? */
6225 if (ftrace_is_dead()) {
6226 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
6227 printk("# MAY BE MISSING FUNCTION EVENTS\n");
6231 * We need to stop all tracing on all CPUS to read the
6232 * the next buffer. This is a bit expensive, but is
6233 * not done often. We fill all what we can read,
6234 * and then release the locks again.
6237 while (!trace_empty(&iter)) {
6240 printk(KERN_TRACE "---------------------------------\n");
6244 /* reset all but tr, trace, and overruns */
6245 memset(&iter.seq, 0,
6246 sizeof(struct trace_iterator) -
6247 offsetof(struct trace_iterator, seq));
6248 iter.iter_flags |= TRACE_FILE_LAT_FMT;
6251 if (trace_find_next_entry_inc(&iter) != NULL) {
6254 ret = print_trace_line(&iter);
6255 if (ret != TRACE_TYPE_NO_CONSUME)
6256 trace_consume(&iter);
6258 touch_nmi_watchdog();
6260 trace_printk_seq(&iter.seq);
6264 printk(KERN_TRACE " (ftrace buffer empty)\n");
6266 printk(KERN_TRACE "---------------------------------\n");
6269 trace_flags |= old_userobj;
6271 for_each_tracing_cpu(cpu) {
6272 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
6274 atomic_dec(&dump_running);
6275 local_irq_restore(flags);
6277 EXPORT_SYMBOL_GPL(ftrace_dump);
6279 __init static int tracer_alloc_buffers(void)
6285 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
6288 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
6289 goto out_free_buffer_mask;
6291 /* Only allocate trace_printk buffers if a trace_printk exists */
6292 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
6293 /* Must be called before global_trace.buffer is allocated */
6294 trace_printk_init_buffers();
6296 /* To save memory, keep the ring buffer size to its minimum */
6297 if (ring_buffer_expanded)
6298 ring_buf_size = trace_buf_size;
6302 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
6303 cpumask_copy(tracing_cpumask, cpu_all_mask);
6305 raw_spin_lock_init(&global_trace.start_lock);
6307 /* TODO: make the number of buffers hot pluggable with CPUS */
6308 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
6309 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
6311 goto out_free_cpumask;
6314 if (global_trace.buffer_disabled)
6317 trace_init_cmdlines();
6320 * register_tracer() might reference current_trace, so it
6321 * needs to be set before we register anything. This is
6322 * just a bootstrap of current_trace anyway.
6324 global_trace.current_trace = &nop_trace;
6326 register_tracer(&nop_trace);
6328 /* All seems OK, enable tracing */
6329 tracing_disabled = 0;
6331 atomic_notifier_chain_register(&panic_notifier_list,
6332 &trace_panic_notifier);
6334 register_die_notifier(&trace_die_notifier);
6336 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
6338 /* Holder for file callbacks */
6339 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
6340 global_trace.trace_cpu.tr = &global_trace;
6342 INIT_LIST_HEAD(&global_trace.systems);
6343 INIT_LIST_HEAD(&global_trace.events);
6344 list_add(&global_trace.list, &ftrace_trace_arrays);
6346 while (trace_boot_options) {
6349 option = strsep(&trace_boot_options, ",");
6350 trace_set_options(&global_trace, option);
6353 register_snapshot_cmd();
6358 free_percpu(global_trace.trace_buffer.data);
6359 #ifdef CONFIG_TRACER_MAX_TRACE
6360 free_percpu(global_trace.max_buffer.data);
6362 free_cpumask_var(tracing_cpumask);
6363 out_free_buffer_mask:
6364 free_cpumask_var(tracing_buffer_mask);
6369 __init static int clear_boot_tracer(void)
6372 * The default tracer at boot buffer is an init section.
6373 * This function is called in lateinit. If we did not
6374 * find the boot tracer, then clear it out, to prevent
6375 * later registration from accessing the buffer that is
6376 * about to be freed.
6378 if (!default_bootup_tracer)
6381 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6382 default_bootup_tracer);
6383 default_bootup_tracer = NULL;
6388 early_initcall(tracer_alloc_buffers);
6389 fs_initcall(tracer_init_debugfs);
6390 late_initcall(clear_boot_tracer);