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 trace_array_get(struct trace_array *this_tr)
198 struct trace_array *tr;
201 mutex_lock(&trace_types_lock);
202 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
209 mutex_unlock(&trace_types_lock);
214 static void __trace_array_put(struct trace_array *this_tr)
216 WARN_ON(!this_tr->ref);
220 void trace_array_put(struct trace_array *this_tr)
222 mutex_lock(&trace_types_lock);
223 __trace_array_put(this_tr);
224 mutex_unlock(&trace_types_lock);
227 int filter_current_check_discard(struct ring_buffer *buffer,
228 struct ftrace_event_call *call, void *rec,
229 struct ring_buffer_event *event)
231 return filter_check_discard(call, rec, buffer, event);
233 EXPORT_SYMBOL_GPL(filter_current_check_discard);
235 cycle_t buffer_ftrace_now(struct trace_buffer *buf, int cpu)
239 /* Early boot up does not have a buffer yet */
241 return trace_clock_local();
243 ts = ring_buffer_time_stamp(buf->buffer, cpu);
244 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
249 cycle_t ftrace_now(int cpu)
251 return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
255 * tracing_is_enabled - Show if global_trace has been disabled
257 * Shows if the global trace has been enabled or not. It uses the
258 * mirror flag "buffer_disabled" to be used in fast paths such as for
259 * the irqsoff tracer. But it may be inaccurate due to races. If you
260 * need to know the accurate state, use tracing_is_on() which is a little
261 * slower, but accurate.
263 int tracing_is_enabled(void)
266 * For quick access (irqsoff uses this in fast path), just
267 * return the mirror variable of the state of the ring buffer.
268 * It's a little racy, but we don't really care.
271 return !global_trace.buffer_disabled;
275 * trace_buf_size is the size in bytes that is allocated
276 * for a buffer. Note, the number of bytes is always rounded
279 * This number is purposely set to a low number of 16384.
280 * If the dump on oops happens, it will be much appreciated
281 * to not have to wait for all that output. Anyway this can be
282 * boot time and run time configurable.
284 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
286 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
288 /* trace_types holds a link list of available tracers. */
289 static struct tracer *trace_types __read_mostly;
292 * trace_types_lock is used to protect the trace_types list.
294 DEFINE_MUTEX(trace_types_lock);
297 * serialize the access of the ring buffer
299 * ring buffer serializes readers, but it is low level protection.
300 * The validity of the events (which returns by ring_buffer_peek() ..etc)
301 * are not protected by ring buffer.
303 * The content of events may become garbage if we allow other process consumes
304 * these events concurrently:
305 * A) the page of the consumed events may become a normal page
306 * (not reader page) in ring buffer, and this page will be rewrited
307 * by events producer.
308 * B) The page of the consumed events may become a page for splice_read,
309 * and this page will be returned to system.
311 * These primitives allow multi process access to different cpu ring buffer
314 * These primitives don't distinguish read-only and read-consume access.
315 * Multi read-only access are also serialized.
319 static DECLARE_RWSEM(all_cpu_access_lock);
320 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
322 static inline void trace_access_lock(int cpu)
324 if (cpu == RING_BUFFER_ALL_CPUS) {
325 /* gain it for accessing the whole ring buffer. */
326 down_write(&all_cpu_access_lock);
328 /* gain it for accessing a cpu ring buffer. */
330 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
331 down_read(&all_cpu_access_lock);
333 /* Secondly block other access to this @cpu ring buffer. */
334 mutex_lock(&per_cpu(cpu_access_lock, cpu));
338 static inline void trace_access_unlock(int cpu)
340 if (cpu == RING_BUFFER_ALL_CPUS) {
341 up_write(&all_cpu_access_lock);
343 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
344 up_read(&all_cpu_access_lock);
348 static inline void trace_access_lock_init(void)
352 for_each_possible_cpu(cpu)
353 mutex_init(&per_cpu(cpu_access_lock, cpu));
358 static DEFINE_MUTEX(access_lock);
360 static inline void trace_access_lock(int cpu)
363 mutex_lock(&access_lock);
366 static inline void trace_access_unlock(int cpu)
369 mutex_unlock(&access_lock);
372 static inline void trace_access_lock_init(void)
378 /* trace_flags holds trace_options default values */
379 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
380 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
381 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
382 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
384 void tracer_tracing_on(struct trace_array *tr)
386 if (tr->trace_buffer.buffer)
387 ring_buffer_record_on(tr->trace_buffer.buffer);
389 * This flag is looked at when buffers haven't been allocated
390 * yet, or by some tracers (like irqsoff), that just want to
391 * know if the ring buffer has been disabled, but it can handle
392 * races of where it gets disabled but we still do a record.
393 * As the check is in the fast path of the tracers, it is more
394 * important to be fast than accurate.
396 tr->buffer_disabled = 0;
397 /* Make the flag seen by readers */
402 * tracing_on - enable tracing buffers
404 * This function enables tracing buffers that may have been
405 * disabled with tracing_off.
407 void tracing_on(void)
409 tracer_tracing_on(&global_trace);
411 EXPORT_SYMBOL_GPL(tracing_on);
414 * __trace_puts - write a constant string into the trace buffer.
415 * @ip: The address of the caller
416 * @str: The constant string to write
417 * @size: The size of the string.
419 int __trace_puts(unsigned long ip, const char *str, int size)
421 struct ring_buffer_event *event;
422 struct ring_buffer *buffer;
423 struct print_entry *entry;
424 unsigned long irq_flags;
427 alloc = sizeof(*entry) + size + 2; /* possible \n added */
429 local_save_flags(irq_flags);
430 buffer = global_trace.trace_buffer.buffer;
431 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
432 irq_flags, preempt_count());
436 entry = ring_buffer_event_data(event);
439 memcpy(&entry->buf, str, size);
441 /* Add a newline if necessary */
442 if (entry->buf[size - 1] != '\n') {
443 entry->buf[size] = '\n';
444 entry->buf[size + 1] = '\0';
446 entry->buf[size] = '\0';
448 __buffer_unlock_commit(buffer, event);
452 EXPORT_SYMBOL_GPL(__trace_puts);
455 * __trace_bputs - write the pointer to a constant string into trace buffer
456 * @ip: The address of the caller
457 * @str: The constant string to write to the buffer to
459 int __trace_bputs(unsigned long ip, const char *str)
461 struct ring_buffer_event *event;
462 struct ring_buffer *buffer;
463 struct bputs_entry *entry;
464 unsigned long irq_flags;
465 int size = sizeof(struct bputs_entry);
467 local_save_flags(irq_flags);
468 buffer = global_trace.trace_buffer.buffer;
469 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
470 irq_flags, preempt_count());
474 entry = ring_buffer_event_data(event);
478 __buffer_unlock_commit(buffer, event);
482 EXPORT_SYMBOL_GPL(__trace_bputs);
484 #ifdef CONFIG_TRACER_SNAPSHOT
486 * trace_snapshot - take a snapshot of the current buffer.
488 * This causes a swap between the snapshot buffer and the current live
489 * tracing buffer. You can use this to take snapshots of the live
490 * trace when some condition is triggered, but continue to trace.
492 * Note, make sure to allocate the snapshot with either
493 * a tracing_snapshot_alloc(), or by doing it manually
494 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
496 * If the snapshot buffer is not allocated, it will stop tracing.
497 * Basically making a permanent snapshot.
499 void tracing_snapshot(void)
501 struct trace_array *tr = &global_trace;
502 struct tracer *tracer = tr->current_trace;
506 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
507 internal_trace_puts("*** snapshot is being ignored ***\n");
511 if (!tr->allocated_snapshot) {
512 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
513 internal_trace_puts("*** stopping trace here! ***\n");
518 /* Note, snapshot can not be used when the tracer uses it */
519 if (tracer->use_max_tr) {
520 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
521 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
525 local_irq_save(flags);
526 update_max_tr(tr, current, smp_processor_id());
527 local_irq_restore(flags);
529 EXPORT_SYMBOL_GPL(tracing_snapshot);
531 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
532 struct trace_buffer *size_buf, int cpu_id);
533 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
535 static int alloc_snapshot(struct trace_array *tr)
539 if (!tr->allocated_snapshot) {
541 /* allocate spare buffer */
542 ret = resize_buffer_duplicate_size(&tr->max_buffer,
543 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
547 tr->allocated_snapshot = true;
553 void free_snapshot(struct trace_array *tr)
556 * We don't free the ring buffer. instead, resize it because
557 * The max_tr ring buffer has some state (e.g. ring->clock) and
558 * we want preserve it.
560 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
561 set_buffer_entries(&tr->max_buffer, 1);
562 tracing_reset_online_cpus(&tr->max_buffer);
563 tr->allocated_snapshot = false;
567 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
569 * This is similar to trace_snapshot(), but it will allocate the
570 * snapshot buffer if it isn't already allocated. Use this only
571 * where it is safe to sleep, as the allocation may sleep.
573 * This causes a swap between the snapshot buffer and the current live
574 * tracing buffer. You can use this to take snapshots of the live
575 * trace when some condition is triggered, but continue to trace.
577 void tracing_snapshot_alloc(void)
579 struct trace_array *tr = &global_trace;
582 ret = alloc_snapshot(tr);
583 if (WARN_ON(ret < 0))
588 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
590 void tracing_snapshot(void)
592 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
594 EXPORT_SYMBOL_GPL(tracing_snapshot);
595 void tracing_snapshot_alloc(void)
600 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
601 #endif /* CONFIG_TRACER_SNAPSHOT */
603 void tracer_tracing_off(struct trace_array *tr)
605 if (tr->trace_buffer.buffer)
606 ring_buffer_record_off(tr->trace_buffer.buffer);
608 * This flag is looked at when buffers haven't been allocated
609 * yet, or by some tracers (like irqsoff), that just want to
610 * know if the ring buffer has been disabled, but it can handle
611 * races of where it gets disabled but we still do a record.
612 * As the check is in the fast path of the tracers, it is more
613 * important to be fast than accurate.
615 tr->buffer_disabled = 1;
616 /* Make the flag seen by readers */
621 * tracing_off - turn off tracing buffers
623 * This function stops the tracing buffers from recording data.
624 * It does not disable any overhead the tracers themselves may
625 * be causing. This function simply causes all recording to
626 * the ring buffers to fail.
628 void tracing_off(void)
630 tracer_tracing_off(&global_trace);
632 EXPORT_SYMBOL_GPL(tracing_off);
635 * tracer_tracing_is_on - show real state of ring buffer enabled
636 * @tr : the trace array to know if ring buffer is enabled
638 * Shows real state of the ring buffer if it is enabled or not.
640 int tracer_tracing_is_on(struct trace_array *tr)
642 if (tr->trace_buffer.buffer)
643 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
644 return !tr->buffer_disabled;
648 * tracing_is_on - show state of ring buffers enabled
650 int tracing_is_on(void)
652 return tracer_tracing_is_on(&global_trace);
654 EXPORT_SYMBOL_GPL(tracing_is_on);
656 static int __init set_buf_size(char *str)
658 unsigned long buf_size;
662 buf_size = memparse(str, &str);
663 /* nr_entries can not be zero */
666 trace_buf_size = buf_size;
669 __setup("trace_buf_size=", set_buf_size);
671 static int __init set_tracing_thresh(char *str)
673 unsigned long threshold;
678 ret = kstrtoul(str, 0, &threshold);
681 tracing_thresh = threshold * 1000;
684 __setup("tracing_thresh=", set_tracing_thresh);
686 unsigned long nsecs_to_usecs(unsigned long nsecs)
691 /* These must match the bit postions in trace_iterator_flags */
692 static const char *trace_options[] = {
726 int in_ns; /* is this clock in nanoseconds? */
728 { trace_clock_local, "local", 1 },
729 { trace_clock_global, "global", 1 },
730 { trace_clock_counter, "counter", 0 },
731 { trace_clock_jiffies, "uptime", 1 },
732 { trace_clock, "perf", 1 },
737 * trace_parser_get_init - gets the buffer for trace parser
739 int trace_parser_get_init(struct trace_parser *parser, int size)
741 memset(parser, 0, sizeof(*parser));
743 parser->buffer = kmalloc(size, GFP_KERNEL);
752 * trace_parser_put - frees the buffer for trace parser
754 void trace_parser_put(struct trace_parser *parser)
756 kfree(parser->buffer);
760 * trace_get_user - reads the user input string separated by space
761 * (matched by isspace(ch))
763 * For each string found the 'struct trace_parser' is updated,
764 * and the function returns.
766 * Returns number of bytes read.
768 * See kernel/trace/trace.h for 'struct trace_parser' details.
770 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
771 size_t cnt, loff_t *ppos)
778 trace_parser_clear(parser);
780 ret = get_user(ch, ubuf++);
788 * The parser is not finished with the last write,
789 * continue reading the user input without skipping spaces.
792 /* skip white space */
793 while (cnt && isspace(ch)) {
794 ret = get_user(ch, ubuf++);
801 /* only spaces were written */
811 /* read the non-space input */
812 while (cnt && !isspace(ch)) {
813 if (parser->idx < parser->size - 1)
814 parser->buffer[parser->idx++] = ch;
819 ret = get_user(ch, ubuf++);
826 /* We either got finished input or we have to wait for another call. */
828 parser->buffer[parser->idx] = 0;
829 parser->cont = false;
830 } else if (parser->idx < parser->size - 1) {
832 parser->buffer[parser->idx++] = ch;
845 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
853 if (s->len <= s->readpos)
856 len = s->len - s->readpos;
859 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
869 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
873 if (s->len <= s->readpos)
876 len = s->len - s->readpos;
879 memcpy(buf, s->buffer + s->readpos, cnt);
886 * ftrace_max_lock is used to protect the swapping of buffers
887 * when taking a max snapshot. The buffers themselves are
888 * protected by per_cpu spinlocks. But the action of the swap
889 * needs its own lock.
891 * This is defined as a arch_spinlock_t in order to help
892 * with performance when lockdep debugging is enabled.
894 * It is also used in other places outside the update_max_tr
895 * so it needs to be defined outside of the
896 * CONFIG_TRACER_MAX_TRACE.
898 static arch_spinlock_t ftrace_max_lock =
899 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
901 unsigned long __read_mostly tracing_thresh;
903 #ifdef CONFIG_TRACER_MAX_TRACE
904 unsigned long __read_mostly tracing_max_latency;
907 * Copy the new maximum trace into the separate maximum-trace
908 * structure. (this way the maximum trace is permanently saved,
909 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
912 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
914 struct trace_buffer *trace_buf = &tr->trace_buffer;
915 struct trace_buffer *max_buf = &tr->max_buffer;
916 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
917 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
920 max_buf->time_start = data->preempt_timestamp;
922 max_data->saved_latency = tracing_max_latency;
923 max_data->critical_start = data->critical_start;
924 max_data->critical_end = data->critical_end;
926 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
927 max_data->pid = tsk->pid;
929 * If tsk == current, then use current_uid(), as that does not use
930 * RCU. The irq tracer can be called out of RCU scope.
933 max_data->uid = current_uid();
935 max_data->uid = task_uid(tsk);
937 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
938 max_data->policy = tsk->policy;
939 max_data->rt_priority = tsk->rt_priority;
941 /* record this tasks comm */
942 tracing_record_cmdline(tsk);
946 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
948 * @tsk: the task with the latency
949 * @cpu: The cpu that initiated the trace.
951 * Flip the buffers between the @tr and the max_tr and record information
952 * about which task was the cause of this latency.
955 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
957 struct ring_buffer *buf;
962 WARN_ON_ONCE(!irqs_disabled());
964 if (!tr->allocated_snapshot) {
965 /* Only the nop tracer should hit this when disabling */
966 WARN_ON_ONCE(tr->current_trace != &nop_trace);
970 arch_spin_lock(&ftrace_max_lock);
972 buf = tr->trace_buffer.buffer;
973 tr->trace_buffer.buffer = tr->max_buffer.buffer;
974 tr->max_buffer.buffer = buf;
976 __update_max_tr(tr, tsk, cpu);
977 arch_spin_unlock(&ftrace_max_lock);
981 * update_max_tr_single - only copy one trace over, and reset the rest
983 * @tsk - task with the latency
984 * @cpu - the cpu of the buffer to copy.
986 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
989 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
996 WARN_ON_ONCE(!irqs_disabled());
997 if (!tr->allocated_snapshot) {
998 /* Only the nop tracer should hit this when disabling */
999 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1003 arch_spin_lock(&ftrace_max_lock);
1005 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
1007 if (ret == -EBUSY) {
1009 * We failed to swap the buffer due to a commit taking
1010 * place on this CPU. We fail to record, but we reset
1011 * the max trace buffer (no one writes directly to it)
1012 * and flag that it failed.
1014 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
1015 "Failed to swap buffers due to commit in progress\n");
1018 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
1020 __update_max_tr(tr, tsk, cpu);
1021 arch_spin_unlock(&ftrace_max_lock);
1023 #endif /* CONFIG_TRACER_MAX_TRACE */
1025 static void default_wait_pipe(struct trace_iterator *iter)
1027 /* Iterators are static, they should be filled or empty */
1028 if (trace_buffer_iter(iter, iter->cpu_file))
1031 ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
1034 #ifdef CONFIG_FTRACE_STARTUP_TEST
1035 static int run_tracer_selftest(struct tracer *type)
1037 struct trace_array *tr = &global_trace;
1038 struct tracer *saved_tracer = tr->current_trace;
1041 if (!type->selftest || tracing_selftest_disabled)
1045 * Run a selftest on this tracer.
1046 * Here we reset the trace buffer, and set the current
1047 * tracer to be this tracer. The tracer can then run some
1048 * internal tracing to verify that everything is in order.
1049 * If we fail, we do not register this tracer.
1051 tracing_reset_online_cpus(&tr->trace_buffer);
1053 tr->current_trace = type;
1055 #ifdef CONFIG_TRACER_MAX_TRACE
1056 if (type->use_max_tr) {
1057 /* If we expanded the buffers, make sure the max is expanded too */
1058 if (ring_buffer_expanded)
1059 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1060 RING_BUFFER_ALL_CPUS);
1061 tr->allocated_snapshot = true;
1065 /* the test is responsible for initializing and enabling */
1066 pr_info("Testing tracer %s: ", type->name);
1067 ret = type->selftest(type, tr);
1068 /* the test is responsible for resetting too */
1069 tr->current_trace = saved_tracer;
1071 printk(KERN_CONT "FAILED!\n");
1072 /* Add the warning after printing 'FAILED' */
1076 /* Only reset on passing, to avoid touching corrupted buffers */
1077 tracing_reset_online_cpus(&tr->trace_buffer);
1079 #ifdef CONFIG_TRACER_MAX_TRACE
1080 if (type->use_max_tr) {
1081 tr->allocated_snapshot = false;
1083 /* Shrink the max buffer again */
1084 if (ring_buffer_expanded)
1085 ring_buffer_resize(tr->max_buffer.buffer, 1,
1086 RING_BUFFER_ALL_CPUS);
1090 printk(KERN_CONT "PASSED\n");
1094 static inline int run_tracer_selftest(struct tracer *type)
1098 #endif /* CONFIG_FTRACE_STARTUP_TEST */
1101 * register_tracer - register a tracer with the ftrace system.
1102 * @type - the plugin for the tracer
1104 * Register a new plugin tracer.
1106 int register_tracer(struct tracer *type)
1112 pr_info("Tracer must have a name\n");
1116 if (strlen(type->name) >= MAX_TRACER_SIZE) {
1117 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1121 mutex_lock(&trace_types_lock);
1123 tracing_selftest_running = true;
1125 for (t = trace_types; t; t = t->next) {
1126 if (strcmp(type->name, t->name) == 0) {
1128 pr_info("Tracer %s already registered\n",
1135 if (!type->set_flag)
1136 type->set_flag = &dummy_set_flag;
1138 type->flags = &dummy_tracer_flags;
1140 if (!type->flags->opts)
1141 type->flags->opts = dummy_tracer_opt;
1142 if (!type->wait_pipe)
1143 type->wait_pipe = default_wait_pipe;
1145 ret = run_tracer_selftest(type);
1149 type->next = trace_types;
1153 tracing_selftest_running = false;
1154 mutex_unlock(&trace_types_lock);
1156 if (ret || !default_bootup_tracer)
1159 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
1162 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1163 /* Do we want this tracer to start on bootup? */
1164 tracing_set_tracer(type->name);
1165 default_bootup_tracer = NULL;
1166 /* disable other selftests, since this will break it. */
1167 tracing_selftest_disabled = true;
1168 #ifdef CONFIG_FTRACE_STARTUP_TEST
1169 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1177 void tracing_reset(struct trace_buffer *buf, int cpu)
1179 struct ring_buffer *buffer = buf->buffer;
1184 ring_buffer_record_disable(buffer);
1186 /* Make sure all commits have finished */
1187 synchronize_sched();
1188 ring_buffer_reset_cpu(buffer, cpu);
1190 ring_buffer_record_enable(buffer);
1193 void tracing_reset_online_cpus(struct trace_buffer *buf)
1195 struct ring_buffer *buffer = buf->buffer;
1201 ring_buffer_record_disable(buffer);
1203 /* Make sure all commits have finished */
1204 synchronize_sched();
1206 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
1208 for_each_online_cpu(cpu)
1209 ring_buffer_reset_cpu(buffer, cpu);
1211 ring_buffer_record_enable(buffer);
1214 /* Must have trace_types_lock held */
1215 void tracing_reset_all_online_cpus(void)
1217 struct trace_array *tr;
1219 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1220 tracing_reset_online_cpus(&tr->trace_buffer);
1221 #ifdef CONFIG_TRACER_MAX_TRACE
1222 tracing_reset_online_cpus(&tr->max_buffer);
1227 #define SAVED_CMDLINES 128
1228 #define NO_CMDLINE_MAP UINT_MAX
1229 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1230 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1231 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1232 static unsigned saved_tgids[SAVED_CMDLINES];
1233 static int cmdline_idx;
1234 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1236 /* temporary disable recording */
1237 static atomic_t trace_record_cmdline_disabled __read_mostly;
1239 static void trace_init_cmdlines(void)
1241 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1242 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
1246 int is_tracing_stopped(void)
1248 return global_trace.stop_count;
1252 * ftrace_off_permanent - disable all ftrace code permanently
1254 * This should only be called when a serious anomally has
1255 * been detected. This will turn off the function tracing,
1256 * ring buffers, and other tracing utilites. It takes no
1257 * locks and can be called from any context.
1259 void ftrace_off_permanent(void)
1261 tracing_disabled = 1;
1263 tracing_off_permanent();
1267 * tracing_start - quick start of the tracer
1269 * If tracing is enabled but was stopped by tracing_stop,
1270 * this will start the tracer back up.
1272 void tracing_start(void)
1274 struct ring_buffer *buffer;
1275 unsigned long flags;
1277 if (tracing_disabled)
1280 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1281 if (--global_trace.stop_count) {
1282 if (global_trace.stop_count < 0) {
1283 /* Someone screwed up their debugging */
1285 global_trace.stop_count = 0;
1290 /* Prevent the buffers from switching */
1291 arch_spin_lock(&ftrace_max_lock);
1293 buffer = global_trace.trace_buffer.buffer;
1295 ring_buffer_record_enable(buffer);
1297 #ifdef CONFIG_TRACER_MAX_TRACE
1298 buffer = global_trace.max_buffer.buffer;
1300 ring_buffer_record_enable(buffer);
1303 arch_spin_unlock(&ftrace_max_lock);
1307 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1310 static void tracing_start_tr(struct trace_array *tr)
1312 struct ring_buffer *buffer;
1313 unsigned long flags;
1315 if (tracing_disabled)
1318 /* If global, we need to also start the max tracer */
1319 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1320 return tracing_start();
1322 raw_spin_lock_irqsave(&tr->start_lock, flags);
1324 if (--tr->stop_count) {
1325 if (tr->stop_count < 0) {
1326 /* Someone screwed up their debugging */
1333 buffer = tr->trace_buffer.buffer;
1335 ring_buffer_record_enable(buffer);
1338 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1342 * tracing_stop - quick stop of the tracer
1344 * Light weight way to stop tracing. Use in conjunction with
1347 void tracing_stop(void)
1349 struct ring_buffer *buffer;
1350 unsigned long flags;
1353 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1354 if (global_trace.stop_count++)
1357 /* Prevent the buffers from switching */
1358 arch_spin_lock(&ftrace_max_lock);
1360 buffer = global_trace.trace_buffer.buffer;
1362 ring_buffer_record_disable(buffer);
1364 #ifdef CONFIG_TRACER_MAX_TRACE
1365 buffer = global_trace.max_buffer.buffer;
1367 ring_buffer_record_disable(buffer);
1370 arch_spin_unlock(&ftrace_max_lock);
1373 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1376 static void tracing_stop_tr(struct trace_array *tr)
1378 struct ring_buffer *buffer;
1379 unsigned long flags;
1381 /* If global, we need to also stop the max tracer */
1382 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1383 return tracing_stop();
1385 raw_spin_lock_irqsave(&tr->start_lock, flags);
1386 if (tr->stop_count++)
1389 buffer = tr->trace_buffer.buffer;
1391 ring_buffer_record_disable(buffer);
1394 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1397 void trace_stop_cmdline_recording(void);
1399 static void trace_save_cmdline(struct task_struct *tsk)
1403 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1407 * It's not the end of the world if we don't get
1408 * the lock, but we also don't want to spin
1409 * nor do we want to disable interrupts,
1410 * so if we miss here, then better luck next time.
1412 if (!arch_spin_trylock(&trace_cmdline_lock))
1415 idx = map_pid_to_cmdline[tsk->pid];
1416 if (idx == NO_CMDLINE_MAP) {
1417 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1420 * Check whether the cmdline buffer at idx has a pid
1421 * mapped. We are going to overwrite that entry so we
1422 * need to clear the map_pid_to_cmdline. Otherwise we
1423 * would read the new comm for the old pid.
1425 pid = map_cmdline_to_pid[idx];
1426 if (pid != NO_CMDLINE_MAP)
1427 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1429 map_cmdline_to_pid[idx] = tsk->pid;
1430 map_pid_to_cmdline[tsk->pid] = idx;
1435 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1436 saved_tgids[idx] = tsk->tgid;
1438 arch_spin_unlock(&trace_cmdline_lock);
1441 void trace_find_cmdline(int pid, char comm[])
1446 strcpy(comm, "<idle>");
1450 if (WARN_ON_ONCE(pid < 0)) {
1451 strcpy(comm, "<XXX>");
1455 if (pid > PID_MAX_DEFAULT) {
1456 strcpy(comm, "<...>");
1461 arch_spin_lock(&trace_cmdline_lock);
1462 map = map_pid_to_cmdline[pid];
1463 if (map != NO_CMDLINE_MAP)
1464 strcpy(comm, saved_cmdlines[map]);
1466 strcpy(comm, "<...>");
1468 arch_spin_unlock(&trace_cmdline_lock);
1472 int trace_find_tgid(int pid)
1478 arch_spin_lock(&trace_cmdline_lock);
1479 map = map_pid_to_cmdline[pid];
1480 if (map != NO_CMDLINE_MAP)
1481 tgid = saved_tgids[map];
1485 arch_spin_unlock(&trace_cmdline_lock);
1491 void tracing_record_cmdline(struct task_struct *tsk)
1493 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1496 if (!__this_cpu_read(trace_cmdline_save))
1499 __this_cpu_write(trace_cmdline_save, false);
1501 trace_save_cmdline(tsk);
1505 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1508 struct task_struct *tsk = current;
1510 entry->preempt_count = pc & 0xff;
1511 entry->pid = (tsk) ? tsk->pid : 0;
1513 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1514 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1516 TRACE_FLAG_IRQS_NOSUPPORT |
1518 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1519 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1520 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1522 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1524 struct ring_buffer_event *
1525 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1528 unsigned long flags, int pc)
1530 struct ring_buffer_event *event;
1532 event = ring_buffer_lock_reserve(buffer, len);
1533 if (event != NULL) {
1534 struct trace_entry *ent = ring_buffer_event_data(event);
1536 tracing_generic_entry_update(ent, flags, pc);
1544 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1546 __this_cpu_write(trace_cmdline_save, true);
1547 ring_buffer_unlock_commit(buffer, event);
1551 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1552 struct ring_buffer_event *event,
1553 unsigned long flags, int pc)
1555 __buffer_unlock_commit(buffer, event);
1557 ftrace_trace_stack(buffer, flags, 6, pc);
1558 ftrace_trace_userstack(buffer, flags, pc);
1561 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1562 struct ring_buffer_event *event,
1563 unsigned long flags, int pc)
1565 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1567 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1569 struct ring_buffer_event *
1570 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1571 struct ftrace_event_file *ftrace_file,
1572 int type, unsigned long len,
1573 unsigned long flags, int pc)
1575 *current_rb = ftrace_file->tr->trace_buffer.buffer;
1576 return trace_buffer_lock_reserve(*current_rb,
1577 type, len, flags, pc);
1579 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1581 struct ring_buffer_event *
1582 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1583 int type, unsigned long len,
1584 unsigned long flags, int pc)
1586 *current_rb = global_trace.trace_buffer.buffer;
1587 return trace_buffer_lock_reserve(*current_rb,
1588 type, len, flags, pc);
1590 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1592 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1593 struct ring_buffer_event *event,
1594 unsigned long flags, int pc)
1596 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1598 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1600 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1601 struct ring_buffer_event *event,
1602 unsigned long flags, int pc,
1603 struct pt_regs *regs)
1605 __buffer_unlock_commit(buffer, event);
1607 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1608 ftrace_trace_userstack(buffer, flags, pc);
1610 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1612 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1613 struct ring_buffer_event *event)
1615 ring_buffer_discard_commit(buffer, event);
1617 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1620 trace_function(struct trace_array *tr,
1621 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1624 struct ftrace_event_call *call = &event_function;
1625 struct ring_buffer *buffer = tr->trace_buffer.buffer;
1626 struct ring_buffer_event *event;
1627 struct ftrace_entry *entry;
1629 /* If we are reading the ring buffer, don't trace */
1630 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1633 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1637 entry = ring_buffer_event_data(event);
1639 entry->parent_ip = parent_ip;
1641 if (!filter_check_discard(call, entry, buffer, event))
1642 __buffer_unlock_commit(buffer, event);
1646 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1647 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1650 if (likely(!atomic_read(&data->disabled)))
1651 trace_function(tr, ip, parent_ip, flags, pc);
1654 #ifdef CONFIG_STACKTRACE
1656 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1657 struct ftrace_stack {
1658 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1661 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1662 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1664 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1665 unsigned long flags,
1666 int skip, int pc, struct pt_regs *regs)
1668 struct ftrace_event_call *call = &event_kernel_stack;
1669 struct ring_buffer_event *event;
1670 struct stack_entry *entry;
1671 struct stack_trace trace;
1673 int size = FTRACE_STACK_ENTRIES;
1675 trace.nr_entries = 0;
1679 * Since events can happen in NMIs there's no safe way to
1680 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1681 * or NMI comes in, it will just have to use the default
1682 * FTRACE_STACK_SIZE.
1684 preempt_disable_notrace();
1686 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1688 * We don't need any atomic variables, just a barrier.
1689 * If an interrupt comes in, we don't care, because it would
1690 * have exited and put the counter back to what we want.
1691 * We just need a barrier to keep gcc from moving things
1695 if (use_stack == 1) {
1696 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1697 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1700 save_stack_trace_regs(regs, &trace);
1702 save_stack_trace(&trace);
1704 if (trace.nr_entries > size)
1705 size = trace.nr_entries;
1707 /* From now on, use_stack is a boolean */
1710 size *= sizeof(unsigned long);
1712 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1713 sizeof(*entry) + size, flags, pc);
1716 entry = ring_buffer_event_data(event);
1718 memset(&entry->caller, 0, size);
1721 memcpy(&entry->caller, trace.entries,
1722 trace.nr_entries * sizeof(unsigned long));
1724 trace.max_entries = FTRACE_STACK_ENTRIES;
1725 trace.entries = entry->caller;
1727 save_stack_trace_regs(regs, &trace);
1729 save_stack_trace(&trace);
1732 entry->size = trace.nr_entries;
1734 if (!filter_check_discard(call, entry, buffer, event))
1735 __buffer_unlock_commit(buffer, event);
1738 /* Again, don't let gcc optimize things here */
1740 __this_cpu_dec(ftrace_stack_reserve);
1741 preempt_enable_notrace();
1745 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1746 int skip, int pc, struct pt_regs *regs)
1748 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1751 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1754 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1757 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1760 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1763 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1766 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
1770 * trace_dump_stack - record a stack back trace in the trace buffer
1771 * @skip: Number of functions to skip (helper handlers)
1773 void trace_dump_stack(int skip)
1775 unsigned long flags;
1777 if (tracing_disabled || tracing_selftest_running)
1780 local_save_flags(flags);
1783 * Skip 3 more, seems to get us at the caller of
1787 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1788 flags, skip, preempt_count(), NULL);
1791 static DEFINE_PER_CPU(int, user_stack_count);
1794 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1796 struct ftrace_event_call *call = &event_user_stack;
1797 struct ring_buffer_event *event;
1798 struct userstack_entry *entry;
1799 struct stack_trace trace;
1801 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1805 * NMIs can not handle page faults, even with fix ups.
1806 * The save user stack can (and often does) fault.
1808 if (unlikely(in_nmi()))
1812 * prevent recursion, since the user stack tracing may
1813 * trigger other kernel events.
1816 if (__this_cpu_read(user_stack_count))
1819 __this_cpu_inc(user_stack_count);
1821 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1822 sizeof(*entry), flags, pc);
1824 goto out_drop_count;
1825 entry = ring_buffer_event_data(event);
1827 entry->tgid = current->tgid;
1828 memset(&entry->caller, 0, sizeof(entry->caller));
1830 trace.nr_entries = 0;
1831 trace.max_entries = FTRACE_STACK_ENTRIES;
1833 trace.entries = entry->caller;
1835 save_stack_trace_user(&trace);
1836 if (!filter_check_discard(call, entry, buffer, event))
1837 __buffer_unlock_commit(buffer, event);
1840 __this_cpu_dec(user_stack_count);
1846 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1848 ftrace_trace_userstack(tr, flags, preempt_count());
1852 #endif /* CONFIG_STACKTRACE */
1854 /* created for use with alloc_percpu */
1855 struct trace_buffer_struct {
1856 char buffer[TRACE_BUF_SIZE];
1859 static struct trace_buffer_struct *trace_percpu_buffer;
1860 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1861 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1862 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1865 * The buffer used is dependent on the context. There is a per cpu
1866 * buffer for normal context, softirq contex, hard irq context and
1867 * for NMI context. Thise allows for lockless recording.
1869 * Note, if the buffers failed to be allocated, then this returns NULL
1871 static char *get_trace_buf(void)
1873 struct trace_buffer_struct *percpu_buffer;
1876 * If we have allocated per cpu buffers, then we do not
1877 * need to do any locking.
1880 percpu_buffer = trace_percpu_nmi_buffer;
1882 percpu_buffer = trace_percpu_irq_buffer;
1883 else if (in_softirq())
1884 percpu_buffer = trace_percpu_sirq_buffer;
1886 percpu_buffer = trace_percpu_buffer;
1891 return this_cpu_ptr(&percpu_buffer->buffer[0]);
1894 static int alloc_percpu_trace_buffer(void)
1896 struct trace_buffer_struct *buffers;
1897 struct trace_buffer_struct *sirq_buffers;
1898 struct trace_buffer_struct *irq_buffers;
1899 struct trace_buffer_struct *nmi_buffers;
1901 buffers = alloc_percpu(struct trace_buffer_struct);
1905 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1909 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1913 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1917 trace_percpu_buffer = buffers;
1918 trace_percpu_sirq_buffer = sirq_buffers;
1919 trace_percpu_irq_buffer = irq_buffers;
1920 trace_percpu_nmi_buffer = nmi_buffers;
1925 free_percpu(irq_buffers);
1927 free_percpu(sirq_buffers);
1929 free_percpu(buffers);
1931 WARN(1, "Could not allocate percpu trace_printk buffer");
1935 static int buffers_allocated;
1937 void trace_printk_init_buffers(void)
1939 if (buffers_allocated)
1942 if (alloc_percpu_trace_buffer())
1945 pr_info("ftrace: Allocated trace_printk buffers\n");
1947 /* Expand the buffers to set size */
1948 tracing_update_buffers();
1950 buffers_allocated = 1;
1953 * trace_printk_init_buffers() can be called by modules.
1954 * If that happens, then we need to start cmdline recording
1955 * directly here. If the global_trace.buffer is already
1956 * allocated here, then this was called by module code.
1958 if (global_trace.trace_buffer.buffer)
1959 tracing_start_cmdline_record();
1962 void trace_printk_start_comm(void)
1964 /* Start tracing comms if trace printk is set */
1965 if (!buffers_allocated)
1967 tracing_start_cmdline_record();
1970 static void trace_printk_start_stop_comm(int enabled)
1972 if (!buffers_allocated)
1976 tracing_start_cmdline_record();
1978 tracing_stop_cmdline_record();
1982 * trace_vbprintk - write binary msg to tracing buffer
1985 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1987 struct ftrace_event_call *call = &event_bprint;
1988 struct ring_buffer_event *event;
1989 struct ring_buffer *buffer;
1990 struct trace_array *tr = &global_trace;
1991 struct bprint_entry *entry;
1992 unsigned long flags;
1994 int len = 0, size, pc;
1996 if (unlikely(tracing_selftest_running || tracing_disabled))
1999 /* Don't pollute graph traces with trace_vprintk internals */
2000 pause_graph_tracing();
2002 pc = preempt_count();
2003 preempt_disable_notrace();
2005 tbuffer = get_trace_buf();
2011 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
2013 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
2016 local_save_flags(flags);
2017 size = sizeof(*entry) + sizeof(u32) * len;
2018 buffer = tr->trace_buffer.buffer;
2019 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
2023 entry = ring_buffer_event_data(event);
2027 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
2028 if (!filter_check_discard(call, entry, buffer, event)) {
2029 __buffer_unlock_commit(buffer, event);
2030 ftrace_trace_stack(buffer, flags, 6, pc);
2034 preempt_enable_notrace();
2035 unpause_graph_tracing();
2039 EXPORT_SYMBOL_GPL(trace_vbprintk);
2042 __trace_array_vprintk(struct ring_buffer *buffer,
2043 unsigned long ip, const char *fmt, va_list args)
2045 struct ftrace_event_call *call = &event_print;
2046 struct ring_buffer_event *event;
2047 int len = 0, size, pc;
2048 struct print_entry *entry;
2049 unsigned long flags;
2052 if (tracing_disabled || tracing_selftest_running)
2055 /* Don't pollute graph traces with trace_vprintk internals */
2056 pause_graph_tracing();
2058 pc = preempt_count();
2059 preempt_disable_notrace();
2062 tbuffer = get_trace_buf();
2068 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
2069 if (len > TRACE_BUF_SIZE)
2072 local_save_flags(flags);
2073 size = sizeof(*entry) + len + 1;
2074 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
2078 entry = ring_buffer_event_data(event);
2081 memcpy(&entry->buf, tbuffer, len);
2082 entry->buf[len] = '\0';
2083 if (!filter_check_discard(call, entry, buffer, event)) {
2084 __buffer_unlock_commit(buffer, event);
2085 ftrace_trace_stack(buffer, flags, 6, pc);
2088 preempt_enable_notrace();
2089 unpause_graph_tracing();
2094 int trace_array_vprintk(struct trace_array *tr,
2095 unsigned long ip, const char *fmt, va_list args)
2097 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2100 int trace_array_printk(struct trace_array *tr,
2101 unsigned long ip, const char *fmt, ...)
2106 if (!(trace_flags & TRACE_ITER_PRINTK))
2110 ret = trace_array_vprintk(tr, ip, fmt, ap);
2115 int trace_array_printk_buf(struct ring_buffer *buffer,
2116 unsigned long ip, const char *fmt, ...)
2121 if (!(trace_flags & TRACE_ITER_PRINTK))
2125 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2130 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2132 return trace_array_vprintk(&global_trace, ip, fmt, args);
2134 EXPORT_SYMBOL_GPL(trace_vprintk);
2136 static void trace_iterator_increment(struct trace_iterator *iter)
2138 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2142 ring_buffer_read(buf_iter, NULL);
2145 static struct trace_entry *
2146 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2147 unsigned long *lost_events)
2149 struct ring_buffer_event *event;
2150 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
2153 event = ring_buffer_iter_peek(buf_iter, ts);
2155 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
2159 iter->ent_size = ring_buffer_event_length(event);
2160 return ring_buffer_event_data(event);
2166 static struct trace_entry *
2167 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2168 unsigned long *missing_events, u64 *ent_ts)
2170 struct ring_buffer *buffer = iter->trace_buffer->buffer;
2171 struct trace_entry *ent, *next = NULL;
2172 unsigned long lost_events = 0, next_lost = 0;
2173 int cpu_file = iter->cpu_file;
2174 u64 next_ts = 0, ts;
2180 * If we are in a per_cpu trace file, don't bother by iterating over
2181 * all cpu and peek directly.
2183 if (cpu_file > RING_BUFFER_ALL_CPUS) {
2184 if (ring_buffer_empty_cpu(buffer, cpu_file))
2186 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
2188 *ent_cpu = cpu_file;
2193 for_each_tracing_cpu(cpu) {
2195 if (ring_buffer_empty_cpu(buffer, cpu))
2198 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
2201 * Pick the entry with the smallest timestamp:
2203 if (ent && (!next || ts < next_ts)) {
2207 next_lost = lost_events;
2208 next_size = iter->ent_size;
2212 iter->ent_size = next_size;
2215 *ent_cpu = next_cpu;
2221 *missing_events = next_lost;
2226 /* Find the next real entry, without updating the iterator itself */
2227 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2228 int *ent_cpu, u64 *ent_ts)
2230 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
2233 /* Find the next real entry, and increment the iterator to the next entry */
2234 void *trace_find_next_entry_inc(struct trace_iterator *iter)
2236 iter->ent = __find_next_entry(iter, &iter->cpu,
2237 &iter->lost_events, &iter->ts);
2240 trace_iterator_increment(iter);
2242 return iter->ent ? iter : NULL;
2245 static void trace_consume(struct trace_iterator *iter)
2247 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
2248 &iter->lost_events);
2251 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
2253 struct trace_iterator *iter = m->private;
2257 WARN_ON_ONCE(iter->leftover);
2261 /* can't go backwards */
2266 ent = trace_find_next_entry_inc(iter);
2270 while (ent && iter->idx < i)
2271 ent = trace_find_next_entry_inc(iter);
2278 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2280 struct ring_buffer_event *event;
2281 struct ring_buffer_iter *buf_iter;
2282 unsigned long entries = 0;
2285 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2287 buf_iter = trace_buffer_iter(iter, cpu);
2291 ring_buffer_iter_reset(buf_iter);
2294 * We could have the case with the max latency tracers
2295 * that a reset never took place on a cpu. This is evident
2296 * by the timestamp being before the start of the buffer.
2298 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
2299 if (ts >= iter->trace_buffer->time_start)
2302 ring_buffer_read(buf_iter, NULL);
2305 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2309 * The current tracer is copied to avoid a global locking
2312 static void *s_start(struct seq_file *m, loff_t *pos)
2314 struct trace_iterator *iter = m->private;
2315 struct trace_array *tr = iter->tr;
2316 int cpu_file = iter->cpu_file;
2322 * copy the tracer to avoid using a global lock all around.
2323 * iter->trace is a copy of current_trace, the pointer to the
2324 * name may be used instead of a strcmp(), as iter->trace->name
2325 * will point to the same string as current_trace->name.
2327 mutex_lock(&trace_types_lock);
2328 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2329 *iter->trace = *tr->current_trace;
2330 mutex_unlock(&trace_types_lock);
2332 #ifdef CONFIG_TRACER_MAX_TRACE
2333 if (iter->snapshot && iter->trace->use_max_tr)
2334 return ERR_PTR(-EBUSY);
2337 if (!iter->snapshot)
2338 atomic_inc(&trace_record_cmdline_disabled);
2340 if (*pos != iter->pos) {
2345 if (cpu_file == RING_BUFFER_ALL_CPUS) {
2346 for_each_tracing_cpu(cpu)
2347 tracing_iter_reset(iter, cpu);
2349 tracing_iter_reset(iter, cpu_file);
2352 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2357 * If we overflowed the seq_file before, then we want
2358 * to just reuse the trace_seq buffer again.
2364 p = s_next(m, p, &l);
2368 trace_event_read_lock();
2369 trace_access_lock(cpu_file);
2373 static void s_stop(struct seq_file *m, void *p)
2375 struct trace_iterator *iter = m->private;
2377 #ifdef CONFIG_TRACER_MAX_TRACE
2378 if (iter->snapshot && iter->trace->use_max_tr)
2382 if (!iter->snapshot)
2383 atomic_dec(&trace_record_cmdline_disabled);
2385 trace_access_unlock(iter->cpu_file);
2386 trace_event_read_unlock();
2390 get_total_entries(struct trace_buffer *buf,
2391 unsigned long *total, unsigned long *entries)
2393 unsigned long count;
2399 for_each_tracing_cpu(cpu) {
2400 count = ring_buffer_entries_cpu(buf->buffer, cpu);
2402 * If this buffer has skipped entries, then we hold all
2403 * entries for the trace and we need to ignore the
2404 * ones before the time stamp.
2406 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2407 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
2408 /* total is the same as the entries */
2412 ring_buffer_overrun_cpu(buf->buffer, cpu);
2417 static void print_lat_help_header(struct seq_file *m)
2419 seq_puts(m, "# _------=> CPU# \n");
2420 seq_puts(m, "# / _-----=> irqs-off \n");
2421 seq_puts(m, "# | / _----=> need-resched \n");
2422 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2423 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2424 seq_puts(m, "# |||| / delay \n");
2425 seq_puts(m, "# cmd pid ||||| time | caller \n");
2426 seq_puts(m, "# \\ / ||||| \\ | / \n");
2429 static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
2431 unsigned long total;
2432 unsigned long entries;
2434 get_total_entries(buf, &total, &entries);
2435 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2436 entries, total, num_online_cpus());
2440 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
2442 print_event_info(buf, m);
2443 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2444 seq_puts(m, "# | | | | |\n");
2447 static void print_func_help_header_tgid(struct trace_buffer *buf, struct seq_file *m)
2449 print_event_info(buf, m);
2450 seq_puts(m, "# TASK-PID TGID CPU# TIMESTAMP FUNCTION\n");
2451 seq_puts(m, "# | | | | | |\n");
2454 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
2456 print_event_info(buf, m);
2457 seq_puts(m, "# _-----=> irqs-off\n");
2458 seq_puts(m, "# / _----=> need-resched\n");
2459 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2460 seq_puts(m, "# || / _--=> preempt-depth\n");
2461 seq_puts(m, "# ||| / delay\n");
2462 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2463 seq_puts(m, "# | | | |||| | |\n");
2466 static void print_func_help_header_irq_tgid(struct trace_buffer *buf, struct seq_file *m)
2468 print_event_info(buf, m);
2469 seq_puts(m, "# _-----=> irqs-off\n");
2470 seq_puts(m, "# / _----=> need-resched\n");
2471 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2472 seq_puts(m, "# || / _--=> preempt-depth\n");
2473 seq_puts(m, "# ||| / delay\n");
2474 seq_puts(m, "# TASK-PID TGID CPU# |||| TIMESTAMP FUNCTION\n");
2475 seq_puts(m, "# | | | | |||| | |\n");
2479 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2481 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2482 struct trace_buffer *buf = iter->trace_buffer;
2483 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2484 struct tracer *type = iter->trace;
2485 unsigned long entries;
2486 unsigned long total;
2487 const char *name = "preemption";
2491 get_total_entries(buf, &total, &entries);
2493 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2495 seq_puts(m, "# -----------------------------------"
2496 "---------------------------------\n");
2497 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2498 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2499 nsecs_to_usecs(data->saved_latency),
2503 #if defined(CONFIG_PREEMPT_NONE)
2505 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2507 #elif defined(CONFIG_PREEMPT)
2512 /* These are reserved for later use */
2515 seq_printf(m, " #P:%d)\n", num_online_cpus());
2519 seq_puts(m, "# -----------------\n");
2520 seq_printf(m, "# | task: %.16s-%d "
2521 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2522 data->comm, data->pid,
2523 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2524 data->policy, data->rt_priority);
2525 seq_puts(m, "# -----------------\n");
2527 if (data->critical_start) {
2528 seq_puts(m, "# => started at: ");
2529 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2530 trace_print_seq(m, &iter->seq);
2531 seq_puts(m, "\n# => ended at: ");
2532 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2533 trace_print_seq(m, &iter->seq);
2534 seq_puts(m, "\n#\n");
2540 static void test_cpu_buff_start(struct trace_iterator *iter)
2542 struct trace_seq *s = &iter->seq;
2544 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2547 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2550 if (cpumask_test_cpu(iter->cpu, iter->started))
2553 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2556 cpumask_set_cpu(iter->cpu, iter->started);
2558 /* Don't print started cpu buffer for the first entry of the trace */
2560 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2564 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2566 struct trace_seq *s = &iter->seq;
2567 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2568 struct trace_entry *entry;
2569 struct trace_event *event;
2573 test_cpu_buff_start(iter);
2575 event = ftrace_find_event(entry->type);
2577 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2578 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2579 if (!trace_print_lat_context(iter))
2582 if (!trace_print_context(iter))
2588 return event->funcs->trace(iter, sym_flags, event);
2590 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2593 return TRACE_TYPE_HANDLED;
2595 return TRACE_TYPE_PARTIAL_LINE;
2598 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2600 struct trace_seq *s = &iter->seq;
2601 struct trace_entry *entry;
2602 struct trace_event *event;
2606 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2607 if (!trace_seq_printf(s, "%d %d %llu ",
2608 entry->pid, iter->cpu, iter->ts))
2612 event = ftrace_find_event(entry->type);
2614 return event->funcs->raw(iter, 0, event);
2616 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2619 return TRACE_TYPE_HANDLED;
2621 return TRACE_TYPE_PARTIAL_LINE;
2624 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2626 struct trace_seq *s = &iter->seq;
2627 unsigned char newline = '\n';
2628 struct trace_entry *entry;
2629 struct trace_event *event;
2633 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2634 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2635 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2636 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2639 event = ftrace_find_event(entry->type);
2641 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2642 if (ret != TRACE_TYPE_HANDLED)
2646 SEQ_PUT_FIELD_RET(s, newline);
2648 return TRACE_TYPE_HANDLED;
2651 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2653 struct trace_seq *s = &iter->seq;
2654 struct trace_entry *entry;
2655 struct trace_event *event;
2659 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2660 SEQ_PUT_FIELD_RET(s, entry->pid);
2661 SEQ_PUT_FIELD_RET(s, iter->cpu);
2662 SEQ_PUT_FIELD_RET(s, iter->ts);
2665 event = ftrace_find_event(entry->type);
2666 return event ? event->funcs->binary(iter, 0, event) :
2670 int trace_empty(struct trace_iterator *iter)
2672 struct ring_buffer_iter *buf_iter;
2675 /* If we are looking at one CPU buffer, only check that one */
2676 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2677 cpu = iter->cpu_file;
2678 buf_iter = trace_buffer_iter(iter, cpu);
2680 if (!ring_buffer_iter_empty(buf_iter))
2683 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2689 for_each_tracing_cpu(cpu) {
2690 buf_iter = trace_buffer_iter(iter, cpu);
2692 if (!ring_buffer_iter_empty(buf_iter))
2695 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2703 /* Called with trace_event_read_lock() held. */
2704 enum print_line_t print_trace_line(struct trace_iterator *iter)
2706 enum print_line_t ret;
2708 if (iter->lost_events &&
2709 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2710 iter->cpu, iter->lost_events))
2711 return TRACE_TYPE_PARTIAL_LINE;
2713 if (iter->trace && iter->trace->print_line) {
2714 ret = iter->trace->print_line(iter);
2715 if (ret != TRACE_TYPE_UNHANDLED)
2719 if (iter->ent->type == TRACE_BPUTS &&
2720 trace_flags & TRACE_ITER_PRINTK &&
2721 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2722 return trace_print_bputs_msg_only(iter);
2724 if (iter->ent->type == TRACE_BPRINT &&
2725 trace_flags & TRACE_ITER_PRINTK &&
2726 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2727 return trace_print_bprintk_msg_only(iter);
2729 if (iter->ent->type == TRACE_PRINT &&
2730 trace_flags & TRACE_ITER_PRINTK &&
2731 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2732 return trace_print_printk_msg_only(iter);
2734 if (trace_flags & TRACE_ITER_BIN)
2735 return print_bin_fmt(iter);
2737 if (trace_flags & TRACE_ITER_HEX)
2738 return print_hex_fmt(iter);
2740 if (trace_flags & TRACE_ITER_RAW)
2741 return print_raw_fmt(iter);
2743 return print_trace_fmt(iter);
2746 void trace_latency_header(struct seq_file *m)
2748 struct trace_iterator *iter = m->private;
2750 /* print nothing if the buffers are empty */
2751 if (trace_empty(iter))
2754 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2755 print_trace_header(m, iter);
2757 if (!(trace_flags & TRACE_ITER_VERBOSE))
2758 print_lat_help_header(m);
2761 void trace_default_header(struct seq_file *m)
2763 struct trace_iterator *iter = m->private;
2765 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2768 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2769 /* print nothing if the buffers are empty */
2770 if (trace_empty(iter))
2772 print_trace_header(m, iter);
2773 if (!(trace_flags & TRACE_ITER_VERBOSE))
2774 print_lat_help_header(m);
2776 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2777 if (trace_flags & TRACE_ITER_IRQ_INFO)
2778 if (trace_flags & TRACE_ITER_TGID)
2779 print_func_help_header_irq_tgid(iter->trace_buffer, m);
2781 print_func_help_header_irq(iter->trace_buffer, m);
2783 if (trace_flags & TRACE_ITER_TGID)
2784 print_func_help_header_tgid(iter->trace_buffer, m);
2786 print_func_help_header(iter->trace_buffer, m);
2791 static void test_ftrace_alive(struct seq_file *m)
2793 if (!ftrace_is_dead())
2795 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2796 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2799 #ifdef CONFIG_TRACER_MAX_TRACE
2800 static void show_snapshot_main_help(struct seq_file *m)
2802 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2803 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2804 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2805 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2806 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2807 seq_printf(m, "# is not a '0' or '1')\n");
2810 static void show_snapshot_percpu_help(struct seq_file *m)
2812 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2813 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2814 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2815 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2817 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2818 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2820 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2821 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2822 seq_printf(m, "# is not a '0' or '1')\n");
2825 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2827 if (iter->tr->allocated_snapshot)
2828 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2830 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2832 seq_printf(m, "# Snapshot commands:\n");
2833 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2834 show_snapshot_main_help(m);
2836 show_snapshot_percpu_help(m);
2839 /* Should never be called */
2840 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2843 static int s_show(struct seq_file *m, void *v)
2845 struct trace_iterator *iter = v;
2848 if (iter->ent == NULL) {
2850 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2852 test_ftrace_alive(m);
2854 if (iter->snapshot && trace_empty(iter))
2855 print_snapshot_help(m, iter);
2856 else if (iter->trace && iter->trace->print_header)
2857 iter->trace->print_header(m);
2859 trace_default_header(m);
2861 } else if (iter->leftover) {
2863 * If we filled the seq_file buffer earlier, we
2864 * want to just show it now.
2866 ret = trace_print_seq(m, &iter->seq);
2868 /* ret should this time be zero, but you never know */
2869 iter->leftover = ret;
2872 print_trace_line(iter);
2873 ret = trace_print_seq(m, &iter->seq);
2875 * If we overflow the seq_file buffer, then it will
2876 * ask us for this data again at start up.
2878 * ret is 0 if seq_file write succeeded.
2881 iter->leftover = ret;
2888 * Should be used after trace_array_get(), trace_types_lock
2889 * ensures that i_cdev was already initialized.
2891 static inline int tracing_get_cpu(struct inode *inode)
2893 if (inode->i_cdev) /* See trace_create_cpu_file() */
2894 return (long)inode->i_cdev - 1;
2895 return RING_BUFFER_ALL_CPUS;
2898 static const struct seq_operations tracer_seq_ops = {
2905 static struct trace_iterator *
2906 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2908 struct trace_array *tr = inode->i_private;
2909 struct trace_iterator *iter;
2912 if (tracing_disabled)
2913 return ERR_PTR(-ENODEV);
2915 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2917 return ERR_PTR(-ENOMEM);
2919 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2921 if (!iter->buffer_iter)
2925 * We make a copy of the current tracer to avoid concurrent
2926 * changes on it while we are reading.
2928 mutex_lock(&trace_types_lock);
2929 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2933 *iter->trace = *tr->current_trace;
2935 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2940 #ifdef CONFIG_TRACER_MAX_TRACE
2941 /* Currently only the top directory has a snapshot */
2942 if (tr->current_trace->print_max || snapshot)
2943 iter->trace_buffer = &tr->max_buffer;
2946 iter->trace_buffer = &tr->trace_buffer;
2947 iter->snapshot = snapshot;
2949 iter->cpu_file = tracing_get_cpu(inode);
2950 mutex_init(&iter->mutex);
2952 /* Notify the tracer early; before we stop tracing. */
2953 if (iter->trace && iter->trace->open)
2954 iter->trace->open(iter);
2956 /* Annotate start of buffers if we had overruns */
2957 if (ring_buffer_overruns(iter->trace_buffer->buffer))
2958 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2960 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2961 if (trace_clocks[tr->clock_id].in_ns)
2962 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2964 /* stop the trace while dumping if we are not opening "snapshot" */
2965 if (!iter->snapshot)
2966 tracing_stop_tr(tr);
2968 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
2969 for_each_tracing_cpu(cpu) {
2970 iter->buffer_iter[cpu] =
2971 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2973 ring_buffer_read_prepare_sync();
2974 for_each_tracing_cpu(cpu) {
2975 ring_buffer_read_start(iter->buffer_iter[cpu]);
2976 tracing_iter_reset(iter, cpu);
2979 cpu = iter->cpu_file;
2980 iter->buffer_iter[cpu] =
2981 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2982 ring_buffer_read_prepare_sync();
2983 ring_buffer_read_start(iter->buffer_iter[cpu]);
2984 tracing_iter_reset(iter, cpu);
2987 mutex_unlock(&trace_types_lock);
2992 mutex_unlock(&trace_types_lock);
2994 kfree(iter->buffer_iter);
2996 seq_release_private(inode, file);
2997 return ERR_PTR(-ENOMEM);
3000 int tracing_open_generic(struct inode *inode, struct file *filp)
3002 if (tracing_disabled)
3005 filp->private_data = inode->i_private;
3010 * Open and update trace_array ref count.
3011 * Must have the current trace_array passed to it.
3013 int tracing_open_generic_tr(struct inode *inode, struct file *filp)
3015 struct trace_array *tr = inode->i_private;
3017 if (tracing_disabled)
3020 if (trace_array_get(tr) < 0)
3023 filp->private_data = inode->i_private;
3028 static int tracing_release(struct inode *inode, struct file *file)
3030 struct trace_array *tr = inode->i_private;
3031 struct seq_file *m = file->private_data;
3032 struct trace_iterator *iter;
3035 if (!(file->f_mode & FMODE_READ)) {
3036 trace_array_put(tr);
3040 /* Writes do not use seq_file */
3042 mutex_lock(&trace_types_lock);
3044 for_each_tracing_cpu(cpu) {
3045 if (iter->buffer_iter[cpu])
3046 ring_buffer_read_finish(iter->buffer_iter[cpu]);
3049 if (iter->trace && iter->trace->close)
3050 iter->trace->close(iter);
3052 if (!iter->snapshot)
3053 /* reenable tracing if it was previously enabled */
3054 tracing_start_tr(tr);
3056 __trace_array_put(tr);
3058 mutex_unlock(&trace_types_lock);
3060 mutex_destroy(&iter->mutex);
3061 free_cpumask_var(iter->started);
3063 kfree(iter->buffer_iter);
3064 seq_release_private(inode, file);
3069 static int tracing_release_generic_tr(struct inode *inode, struct file *file)
3071 struct trace_array *tr = inode->i_private;
3073 trace_array_put(tr);
3077 static int tracing_single_release_tr(struct inode *inode, struct file *file)
3079 struct trace_array *tr = inode->i_private;
3081 trace_array_put(tr);
3083 return single_release(inode, file);
3086 static int tracing_open(struct inode *inode, struct file *file)
3088 struct trace_array *tr = inode->i_private;
3089 struct trace_iterator *iter;
3092 if (trace_array_get(tr) < 0)
3095 /* If this file was open for write, then erase contents */
3096 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
3097 int cpu = tracing_get_cpu(inode);
3099 if (cpu == RING_BUFFER_ALL_CPUS)
3100 tracing_reset_online_cpus(&tr->trace_buffer);
3102 tracing_reset(&tr->trace_buffer, cpu);
3105 if (file->f_mode & FMODE_READ) {
3106 iter = __tracing_open(inode, file, false);
3108 ret = PTR_ERR(iter);
3109 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
3110 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3114 trace_array_put(tr);
3120 t_next(struct seq_file *m, void *v, loff_t *pos)
3122 struct tracer *t = v;
3132 static void *t_start(struct seq_file *m, loff_t *pos)
3137 mutex_lock(&trace_types_lock);
3138 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
3144 static void t_stop(struct seq_file *m, void *p)
3146 mutex_unlock(&trace_types_lock);
3149 static int t_show(struct seq_file *m, void *v)
3151 struct tracer *t = v;
3156 seq_printf(m, "%s", t->name);
3165 static const struct seq_operations show_traces_seq_ops = {
3172 static int show_traces_open(struct inode *inode, struct file *file)
3174 if (tracing_disabled)
3177 return seq_open(file, &show_traces_seq_ops);
3181 tracing_write_stub(struct file *filp, const char __user *ubuf,
3182 size_t count, loff_t *ppos)
3187 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
3189 if (file->f_mode & FMODE_READ)
3190 return seq_lseek(file, offset, origin);
3195 static const struct file_operations tracing_fops = {
3196 .open = tracing_open,
3198 .write = tracing_write_stub,
3199 .llseek = tracing_seek,
3200 .release = tracing_release,
3203 static const struct file_operations show_traces_fops = {
3204 .open = show_traces_open,
3206 .release = seq_release,
3207 .llseek = seq_lseek,
3211 * Only trace on a CPU if the bitmask is set:
3213 static cpumask_var_t tracing_cpumask;
3216 * The tracer itself will not take this lock, but still we want
3217 * to provide a consistent cpumask to user-space:
3219 static DEFINE_MUTEX(tracing_cpumask_update_lock);
3222 * Temporary storage for the character representation of the
3223 * CPU bitmask (and one more byte for the newline):
3225 static char mask_str[NR_CPUS + 1];
3228 tracing_cpumask_read(struct file *filp, char __user *ubuf,
3229 size_t count, loff_t *ppos)
3233 mutex_lock(&tracing_cpumask_update_lock);
3235 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
3236 if (count - len < 2) {
3240 len += sprintf(mask_str + len, "\n");
3241 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3244 mutex_unlock(&tracing_cpumask_update_lock);
3250 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3251 size_t count, loff_t *ppos)
3253 struct trace_array *tr = filp->private_data;
3254 cpumask_var_t tracing_cpumask_new;
3257 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3260 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
3264 mutex_lock(&tracing_cpumask_update_lock);
3266 local_irq_disable();
3267 arch_spin_lock(&ftrace_max_lock);
3268 for_each_tracing_cpu(cpu) {
3270 * Increase/decrease the disabled counter if we are
3271 * about to flip a bit in the cpumask:
3273 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
3274 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3275 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3276 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
3278 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
3279 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3280 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3281 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
3284 arch_spin_unlock(&ftrace_max_lock);
3287 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
3289 mutex_unlock(&tracing_cpumask_update_lock);
3290 free_cpumask_var(tracing_cpumask_new);
3295 free_cpumask_var(tracing_cpumask_new);
3300 static const struct file_operations tracing_cpumask_fops = {
3301 .open = tracing_open_generic,
3302 .read = tracing_cpumask_read,
3303 .write = tracing_cpumask_write,
3304 .llseek = generic_file_llseek,
3307 static int tracing_trace_options_show(struct seq_file *m, void *v)
3309 struct tracer_opt *trace_opts;
3310 struct trace_array *tr = m->private;
3314 mutex_lock(&trace_types_lock);
3315 tracer_flags = tr->current_trace->flags->val;
3316 trace_opts = tr->current_trace->flags->opts;
3318 for (i = 0; trace_options[i]; i++) {
3319 if (trace_flags & (1 << i))
3320 seq_printf(m, "%s\n", trace_options[i]);
3322 seq_printf(m, "no%s\n", trace_options[i]);
3325 for (i = 0; trace_opts[i].name; i++) {
3326 if (tracer_flags & trace_opts[i].bit)
3327 seq_printf(m, "%s\n", trace_opts[i].name);
3329 seq_printf(m, "no%s\n", trace_opts[i].name);
3331 mutex_unlock(&trace_types_lock);
3336 static int __set_tracer_option(struct tracer *trace,
3337 struct tracer_flags *tracer_flags,
3338 struct tracer_opt *opts, int neg)
3342 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
3347 tracer_flags->val &= ~opts->bit;
3349 tracer_flags->val |= opts->bit;
3353 /* Try to assign a tracer specific option */
3354 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3356 struct tracer_flags *tracer_flags = trace->flags;
3357 struct tracer_opt *opts = NULL;
3360 for (i = 0; tracer_flags->opts[i].name; i++) {
3361 opts = &tracer_flags->opts[i];
3363 if (strcmp(cmp, opts->name) == 0)
3364 return __set_tracer_option(trace, trace->flags,
3371 /* Some tracers require overwrite to stay enabled */
3372 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3374 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3380 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
3382 /* do nothing if flag is already set */
3383 if (!!(trace_flags & mask) == !!enabled)
3386 /* Give the tracer a chance to approve the change */
3387 if (tr->current_trace->flag_changed)
3388 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
3392 trace_flags |= mask;
3394 trace_flags &= ~mask;
3396 if (mask == TRACE_ITER_RECORD_CMD)
3397 trace_event_enable_cmd_record(enabled);
3399 if (mask == TRACE_ITER_OVERWRITE) {
3400 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
3401 #ifdef CONFIG_TRACER_MAX_TRACE
3402 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
3406 if (mask == TRACE_ITER_PRINTK)
3407 trace_printk_start_stop_comm(enabled);
3412 static int trace_set_options(struct trace_array *tr, char *option)
3419 cmp = strstrip(option);
3421 if (strncmp(cmp, "no", 2) == 0) {
3426 mutex_lock(&trace_types_lock);
3428 for (i = 0; trace_options[i]; i++) {
3429 if (strcmp(cmp, trace_options[i]) == 0) {
3430 ret = set_tracer_flag(tr, 1 << i, !neg);
3435 /* If no option could be set, test the specific tracer options */
3436 if (!trace_options[i])
3437 ret = set_tracer_option(tr->current_trace, cmp, neg);
3439 mutex_unlock(&trace_types_lock);
3445 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3446 size_t cnt, loff_t *ppos)
3448 struct seq_file *m = filp->private_data;
3449 struct trace_array *tr = m->private;
3453 if (cnt >= sizeof(buf))
3456 if (copy_from_user(&buf, ubuf, cnt))
3461 ret = trace_set_options(tr, buf);
3470 static int tracing_trace_options_open(struct inode *inode, struct file *file)
3472 struct trace_array *tr = inode->i_private;
3475 if (tracing_disabled)
3478 if (trace_array_get(tr) < 0)
3481 ret = single_open(file, tracing_trace_options_show, inode->i_private);
3483 trace_array_put(tr);
3488 static const struct file_operations tracing_iter_fops = {
3489 .open = tracing_trace_options_open,
3491 .llseek = seq_lseek,
3492 .release = tracing_single_release_tr,
3493 .write = tracing_trace_options_write,
3496 static const char readme_msg[] =
3497 "tracing mini-HOWTO:\n\n"
3498 "# echo 0 > tracing_on : quick way to disable tracing\n"
3499 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3500 " Important files:\n"
3501 " trace\t\t\t- The static contents of the buffer\n"
3502 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3503 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3504 " current_tracer\t- function and latency tracers\n"
3505 " available_tracers\t- list of configured tracers for current_tracer\n"
3506 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3507 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3508 " trace_clock\t\t-change the clock used to order events\n"
3509 " local: Per cpu clock but may not be synced across CPUs\n"
3510 " global: Synced across CPUs but slows tracing down.\n"
3511 " counter: Not a clock, but just an increment\n"
3512 " uptime: Jiffy counter from time of boot\n"
3513 " perf: Same clock that perf events use\n"
3514 #ifdef CONFIG_X86_64
3515 " x86-tsc: TSC cycle counter\n"
3517 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3518 " tracing_cpumask\t- Limit which CPUs to trace\n"
3519 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3520 "\t\t\t Remove sub-buffer with rmdir\n"
3521 " trace_options\t\t- Set format or modify how tracing happens\n"
3522 "\t\t\t Disable an option by adding a suffix 'no' to the option name\n"
3523 #ifdef CONFIG_DYNAMIC_FTRACE
3524 "\n available_filter_functions - list of functions that can be filtered on\n"
3525 " set_ftrace_filter\t- echo function name in here to only trace these functions\n"
3526 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3527 " modules: Can select a group via module\n"
3528 " Format: :mod:<module-name>\n"
3529 " example: echo :mod:ext3 > set_ftrace_filter\n"
3530 " triggers: a command to perform when function is hit\n"
3531 " Format: <function>:<trigger>[:count]\n"
3532 " trigger: traceon, traceoff\n"
3533 " enable_event:<system>:<event>\n"
3534 " disable_event:<system>:<event>\n"
3535 #ifdef CONFIG_STACKTRACE
3538 #ifdef CONFIG_TRACER_SNAPSHOT
3541 " example: echo do_fault:traceoff > set_ftrace_filter\n"
3542 " echo do_trap:traceoff:3 > set_ftrace_filter\n"
3543 " The first one will disable tracing every time do_fault is hit\n"
3544 " The second will disable tracing at most 3 times when do_trap is hit\n"
3545 " The first time do trap is hit and it disables tracing, the counter\n"
3546 " will decrement to 2. If tracing is already disabled, the counter\n"
3547 " will not decrement. It only decrements when the trigger did work\n"
3548 " To remove trigger without count:\n"
3549 " echo '!<function>:<trigger> > set_ftrace_filter\n"
3550 " To remove trigger with a count:\n"
3551 " echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
3552 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
3553 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3554 " modules: Can select a group via module command :mod:\n"
3555 " Does not accept triggers\n"
3556 #endif /* CONFIG_DYNAMIC_FTRACE */
3557 #ifdef CONFIG_FUNCTION_TRACER
3558 " set_ftrace_pid\t- Write pid(s) to only function trace those pids (function)\n"
3560 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3561 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3562 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3564 #ifdef CONFIG_TRACER_SNAPSHOT
3565 "\n snapshot\t\t- Like 'trace' but shows the content of the static snapshot buffer\n"
3566 "\t\t\t Read the contents for more information\n"
3568 #ifdef CONFIG_STACKTRACE
3569 " stack_trace\t\t- Shows the max stack trace when active\n"
3570 " stack_max_size\t- Shows current max stack size that was traced\n"
3571 "\t\t\t Write into this file to reset the max size (trigger a new trace)\n"
3572 #ifdef CONFIG_DYNAMIC_FTRACE
3573 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace traces\n"
3575 #endif /* CONFIG_STACKTRACE */
3579 tracing_readme_read(struct file *filp, char __user *ubuf,
3580 size_t cnt, loff_t *ppos)
3582 return simple_read_from_buffer(ubuf, cnt, ppos,
3583 readme_msg, strlen(readme_msg));
3586 static const struct file_operations tracing_readme_fops = {
3587 .open = tracing_open_generic,
3588 .read = tracing_readme_read,
3589 .llseek = generic_file_llseek,
3593 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3594 size_t cnt, loff_t *ppos)
3603 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3607 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3615 for (i = 0; i < SAVED_CMDLINES; i++) {
3618 pid = map_cmdline_to_pid[i];
3619 if (pid == -1 || pid == NO_CMDLINE_MAP)
3622 trace_find_cmdline(pid, buf_comm);
3623 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3628 len = simple_read_from_buffer(ubuf, cnt, ppos,
3637 static const struct file_operations tracing_saved_cmdlines_fops = {
3638 .open = tracing_open_generic,
3639 .read = tracing_saved_cmdlines_read,
3640 .llseek = generic_file_llseek,
3644 tracing_saved_tgids_read(struct file *file, char __user *ubuf,
3645 size_t cnt, loff_t *ppos)
3653 file_buf = kmalloc(SAVED_CMDLINES*(16+1+16), GFP_KERNEL);
3659 for (i = 0; i < SAVED_CMDLINES; i++) {
3663 pid = map_cmdline_to_pid[i];
3664 if (pid == -1 || pid == NO_CMDLINE_MAP)
3667 tgid = trace_find_tgid(pid);
3668 r = sprintf(buf, "%d %d\n", pid, tgid);
3673 len = simple_read_from_buffer(ubuf, cnt, ppos,
3681 static const struct file_operations tracing_saved_tgids_fops = {
3682 .open = tracing_open_generic,
3683 .read = tracing_saved_tgids_read,
3684 .llseek = generic_file_llseek,
3688 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3689 size_t cnt, loff_t *ppos)
3691 struct trace_array *tr = filp->private_data;
3692 char buf[MAX_TRACER_SIZE+2];
3695 mutex_lock(&trace_types_lock);
3696 r = sprintf(buf, "%s\n", tr->current_trace->name);
3697 mutex_unlock(&trace_types_lock);
3699 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3702 int tracer_init(struct tracer *t, struct trace_array *tr)
3704 tracing_reset_online_cpus(&tr->trace_buffer);
3708 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
3712 for_each_tracing_cpu(cpu)
3713 per_cpu_ptr(buf->data, cpu)->entries = val;
3716 #ifdef CONFIG_TRACER_MAX_TRACE
3717 /* resize @tr's buffer to the size of @size_tr's entries */
3718 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3719 struct trace_buffer *size_buf, int cpu_id)
3723 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3724 for_each_tracing_cpu(cpu) {
3725 ret = ring_buffer_resize(trace_buf->buffer,
3726 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
3729 per_cpu_ptr(trace_buf->data, cpu)->entries =
3730 per_cpu_ptr(size_buf->data, cpu)->entries;
3733 ret = ring_buffer_resize(trace_buf->buffer,
3734 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
3736 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3737 per_cpu_ptr(size_buf->data, cpu_id)->entries;
3742 #endif /* CONFIG_TRACER_MAX_TRACE */
3744 static int __tracing_resize_ring_buffer(struct trace_array *tr,
3745 unsigned long size, int cpu)
3750 * If kernel or user changes the size of the ring buffer
3751 * we use the size that was given, and we can forget about
3752 * expanding it later.
3754 ring_buffer_expanded = true;
3756 /* May be called before buffers are initialized */
3757 if (!tr->trace_buffer.buffer)
3760 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
3764 #ifdef CONFIG_TRACER_MAX_TRACE
3765 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3766 !tr->current_trace->use_max_tr)
3769 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
3771 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3772 &tr->trace_buffer, cpu);
3775 * AARGH! We are left with different
3776 * size max buffer!!!!
3777 * The max buffer is our "snapshot" buffer.
3778 * When a tracer needs a snapshot (one of the
3779 * latency tracers), it swaps the max buffer
3780 * with the saved snap shot. We succeeded to
3781 * update the size of the main buffer, but failed to
3782 * update the size of the max buffer. But when we tried
3783 * to reset the main buffer to the original size, we
3784 * failed there too. This is very unlikely to
3785 * happen, but if it does, warn and kill all
3789 tracing_disabled = 1;
3794 if (cpu == RING_BUFFER_ALL_CPUS)
3795 set_buffer_entries(&tr->max_buffer, size);
3797 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
3800 #endif /* CONFIG_TRACER_MAX_TRACE */
3802 if (cpu == RING_BUFFER_ALL_CPUS)
3803 set_buffer_entries(&tr->trace_buffer, size);
3805 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
3810 static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3811 unsigned long size, int cpu_id)
3815 mutex_lock(&trace_types_lock);
3817 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3818 /* make sure, this cpu is enabled in the mask */
3819 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3825 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
3830 mutex_unlock(&trace_types_lock);
3837 * tracing_update_buffers - used by tracing facility to expand ring buffers
3839 * To save on memory when the tracing is never used on a system with it
3840 * configured in. The ring buffers are set to a minimum size. But once
3841 * a user starts to use the tracing facility, then they need to grow
3842 * to their default size.
3844 * This function is to be called when a tracer is about to be used.
3846 int tracing_update_buffers(void)
3850 mutex_lock(&trace_types_lock);
3851 if (!ring_buffer_expanded)
3852 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
3853 RING_BUFFER_ALL_CPUS);
3854 mutex_unlock(&trace_types_lock);
3859 struct trace_option_dentry;
3861 static struct trace_option_dentry *
3862 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
3865 destroy_trace_option_files(struct trace_option_dentry *topts);
3867 static int tracing_set_tracer(const char *buf)
3869 static struct trace_option_dentry *topts;
3870 struct trace_array *tr = &global_trace;
3872 #ifdef CONFIG_TRACER_MAX_TRACE
3877 mutex_lock(&trace_types_lock);
3879 if (!ring_buffer_expanded) {
3880 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
3881 RING_BUFFER_ALL_CPUS);
3887 for (t = trace_types; t; t = t->next) {
3888 if (strcmp(t->name, buf) == 0)
3895 if (t == tr->current_trace)
3898 trace_branch_disable();
3900 tr->current_trace->enabled = false;
3902 if (tr->current_trace->reset)
3903 tr->current_trace->reset(tr);
3905 /* Current trace needs to be nop_trace before synchronize_sched */
3906 tr->current_trace = &nop_trace;
3908 #ifdef CONFIG_TRACER_MAX_TRACE
3909 had_max_tr = tr->allocated_snapshot;
3911 if (had_max_tr && !t->use_max_tr) {
3913 * We need to make sure that the update_max_tr sees that
3914 * current_trace changed to nop_trace to keep it from
3915 * swapping the buffers after we resize it.
3916 * The update_max_tr is called from interrupts disabled
3917 * so a synchronized_sched() is sufficient.
3919 synchronize_sched();
3923 destroy_trace_option_files(topts);
3925 topts = create_trace_option_files(tr, t);
3927 #ifdef CONFIG_TRACER_MAX_TRACE
3928 if (t->use_max_tr && !had_max_tr) {
3929 ret = alloc_snapshot(tr);
3936 ret = tracer_init(t, tr);
3941 tr->current_trace = t;
3942 tr->current_trace->enabled = true;
3943 trace_branch_enable(tr);
3945 mutex_unlock(&trace_types_lock);
3951 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3952 size_t cnt, loff_t *ppos)
3954 char buf[MAX_TRACER_SIZE+1];
3961 if (cnt > MAX_TRACER_SIZE)
3962 cnt = MAX_TRACER_SIZE;
3964 if (copy_from_user(&buf, ubuf, cnt))
3969 /* strip ending whitespace. */
3970 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3973 err = tracing_set_tracer(buf);
3983 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3984 size_t cnt, loff_t *ppos)
3986 unsigned long *ptr = filp->private_data;
3990 r = snprintf(buf, sizeof(buf), "%ld\n",
3991 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3992 if (r > sizeof(buf))
3994 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3998 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3999 size_t cnt, loff_t *ppos)
4001 unsigned long *ptr = filp->private_data;
4005 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4014 static int tracing_open_pipe(struct inode *inode, struct file *filp)
4016 struct trace_array *tr = inode->i_private;
4017 struct trace_iterator *iter;
4020 if (tracing_disabled)
4023 if (trace_array_get(tr) < 0)
4026 mutex_lock(&trace_types_lock);
4028 /* create a buffer to store the information to pass to userspace */
4029 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4032 __trace_array_put(tr);
4037 * We make a copy of the current tracer to avoid concurrent
4038 * changes on it while we are reading.
4040 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
4045 *iter->trace = *tr->current_trace;
4047 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
4052 /* trace pipe does not show start of buffer */
4053 cpumask_setall(iter->started);
4055 if (trace_flags & TRACE_ITER_LATENCY_FMT)
4056 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4058 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
4059 if (trace_clocks[tr->clock_id].in_ns)
4060 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4063 iter->trace_buffer = &tr->trace_buffer;
4064 iter->cpu_file = tracing_get_cpu(inode);
4065 mutex_init(&iter->mutex);
4066 filp->private_data = iter;
4068 if (iter->trace->pipe_open)
4069 iter->trace->pipe_open(iter);
4071 nonseekable_open(inode, filp);
4073 mutex_unlock(&trace_types_lock);
4079 __trace_array_put(tr);
4080 mutex_unlock(&trace_types_lock);
4084 static int tracing_release_pipe(struct inode *inode, struct file *file)
4086 struct trace_iterator *iter = file->private_data;
4087 struct trace_array *tr = inode->i_private;
4089 mutex_lock(&trace_types_lock);
4091 if (iter->trace->pipe_close)
4092 iter->trace->pipe_close(iter);
4094 mutex_unlock(&trace_types_lock);
4096 free_cpumask_var(iter->started);
4097 mutex_destroy(&iter->mutex);
4101 trace_array_put(tr);
4107 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
4109 /* Iterators are static, they should be filled or empty */
4110 if (trace_buffer_iter(iter, iter->cpu_file))
4111 return POLLIN | POLLRDNORM;
4113 if (trace_flags & TRACE_ITER_BLOCK)
4115 * Always select as readable when in blocking mode
4117 return POLLIN | POLLRDNORM;
4119 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
4124 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
4126 struct trace_iterator *iter = filp->private_data;
4128 return trace_poll(iter, filp, poll_table);
4132 * This is a make-shift waitqueue.
4133 * A tracer might use this callback on some rare cases:
4135 * 1) the current tracer might hold the runqueue lock when it wakes up
4136 * a reader, hence a deadlock (sched, function, and function graph tracers)
4137 * 2) the function tracers, trace all functions, we don't want
4138 * the overhead of calling wake_up and friends
4139 * (and tracing them too)
4141 * Anyway, this is really very primitive wakeup.
4143 void poll_wait_pipe(struct trace_iterator *iter)
4145 set_current_state(TASK_INTERRUPTIBLE);
4146 /* sleep for 100 msecs, and try again. */
4147 schedule_timeout(HZ / 10);
4150 /* Must be called with trace_types_lock mutex held. */
4151 static int tracing_wait_pipe(struct file *filp)
4153 struct trace_iterator *iter = filp->private_data;
4155 while (trace_empty(iter)) {
4157 if ((filp->f_flags & O_NONBLOCK)) {
4161 mutex_unlock(&iter->mutex);
4163 iter->trace->wait_pipe(iter);
4165 mutex_lock(&iter->mutex);
4167 if (signal_pending(current))
4171 * We block until we read something and tracing is disabled.
4172 * We still block if tracing is disabled, but we have never
4173 * read anything. This allows a user to cat this file, and
4174 * then enable tracing. But after we have read something,
4175 * we give an EOF when tracing is again disabled.
4177 * iter->pos will be 0 if we haven't read anything.
4179 if (!tracing_is_on() && iter->pos)
4190 tracing_read_pipe(struct file *filp, char __user *ubuf,
4191 size_t cnt, loff_t *ppos)
4193 struct trace_iterator *iter = filp->private_data;
4194 struct trace_array *tr = iter->tr;
4197 /* return any leftover data */
4198 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4202 trace_seq_init(&iter->seq);
4204 /* copy the tracer to avoid using a global lock all around */
4205 mutex_lock(&trace_types_lock);
4206 if (unlikely(iter->trace->name != tr->current_trace->name))
4207 *iter->trace = *tr->current_trace;
4208 mutex_unlock(&trace_types_lock);
4211 * Avoid more than one consumer on a single file descriptor
4212 * This is just a matter of traces coherency, the ring buffer itself
4215 mutex_lock(&iter->mutex);
4216 if (iter->trace->read) {
4217 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
4223 sret = tracing_wait_pipe(filp);
4227 /* stop when tracing is finished */
4228 if (trace_empty(iter)) {
4233 if (cnt >= PAGE_SIZE)
4234 cnt = PAGE_SIZE - 1;
4236 /* reset all but tr, trace, and overruns */
4237 memset(&iter->seq, 0,
4238 sizeof(struct trace_iterator) -
4239 offsetof(struct trace_iterator, seq));
4240 cpumask_clear(iter->started);
4243 trace_event_read_lock();
4244 trace_access_lock(iter->cpu_file);
4245 while (trace_find_next_entry_inc(iter) != NULL) {
4246 enum print_line_t ret;
4247 int len = iter->seq.len;
4249 ret = print_trace_line(iter);
4250 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4251 /* don't print partial lines */
4252 iter->seq.len = len;
4255 if (ret != TRACE_TYPE_NO_CONSUME)
4256 trace_consume(iter);
4258 if (iter->seq.len >= cnt)
4262 * Setting the full flag means we reached the trace_seq buffer
4263 * size and we should leave by partial output condition above.
4264 * One of the trace_seq_* functions is not used properly.
4266 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
4269 trace_access_unlock(iter->cpu_file);
4270 trace_event_read_unlock();
4272 /* Now copy what we have to the user */
4273 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4274 if (iter->seq.readpos >= iter->seq.len)
4275 trace_seq_init(&iter->seq);
4278 * If there was nothing to send to user, in spite of consuming trace
4279 * entries, go back to wait for more entries.
4285 mutex_unlock(&iter->mutex);
4290 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
4291 struct pipe_buffer *buf)
4293 __free_page(buf->page);
4296 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
4299 __free_page(spd->pages[idx]);
4302 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
4304 .map = generic_pipe_buf_map,
4305 .unmap = generic_pipe_buf_unmap,
4306 .confirm = generic_pipe_buf_confirm,
4307 .release = tracing_pipe_buf_release,
4308 .steal = generic_pipe_buf_steal,
4309 .get = generic_pipe_buf_get,
4313 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
4318 /* Seq buffer is page-sized, exactly what we need. */
4320 count = iter->seq.len;
4321 ret = print_trace_line(iter);
4322 count = iter->seq.len - count;
4325 iter->seq.len -= count;
4328 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4329 iter->seq.len -= count;
4333 if (ret != TRACE_TYPE_NO_CONSUME)
4334 trace_consume(iter);
4336 if (!trace_find_next_entry_inc(iter)) {
4346 static ssize_t tracing_splice_read_pipe(struct file *filp,
4348 struct pipe_inode_info *pipe,
4352 struct page *pages_def[PIPE_DEF_BUFFERS];
4353 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4354 struct trace_iterator *iter = filp->private_data;
4355 struct splice_pipe_desc spd = {
4357 .partial = partial_def,
4358 .nr_pages = 0, /* This gets updated below. */
4359 .nr_pages_max = PIPE_DEF_BUFFERS,
4361 .ops = &tracing_pipe_buf_ops,
4362 .spd_release = tracing_spd_release_pipe,
4364 struct trace_array *tr = iter->tr;
4369 if (splice_grow_spd(pipe, &spd))
4372 /* copy the tracer to avoid using a global lock all around */
4373 mutex_lock(&trace_types_lock);
4374 if (unlikely(iter->trace->name != tr->current_trace->name))
4375 *iter->trace = *tr->current_trace;
4376 mutex_unlock(&trace_types_lock);
4378 mutex_lock(&iter->mutex);
4380 if (iter->trace->splice_read) {
4381 ret = iter->trace->splice_read(iter, filp,
4382 ppos, pipe, len, flags);
4387 ret = tracing_wait_pipe(filp);
4391 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
4396 trace_event_read_lock();
4397 trace_access_lock(iter->cpu_file);
4399 /* Fill as many pages as possible. */
4400 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
4401 spd.pages[i] = alloc_page(GFP_KERNEL);
4405 rem = tracing_fill_pipe_page(rem, iter);
4407 /* Copy the data into the page, so we can start over. */
4408 ret = trace_seq_to_buffer(&iter->seq,
4409 page_address(spd.pages[i]),
4412 __free_page(spd.pages[i]);
4415 spd.partial[i].offset = 0;
4416 spd.partial[i].len = iter->seq.len;
4418 trace_seq_init(&iter->seq);
4421 trace_access_unlock(iter->cpu_file);
4422 trace_event_read_unlock();
4423 mutex_unlock(&iter->mutex);
4427 ret = splice_to_pipe(pipe, &spd);
4429 splice_shrink_spd(&spd);
4433 mutex_unlock(&iter->mutex);
4438 tracing_entries_read(struct file *filp, char __user *ubuf,
4439 size_t cnt, loff_t *ppos)
4441 struct inode *inode = file_inode(filp);
4442 struct trace_array *tr = inode->i_private;
4443 int cpu = tracing_get_cpu(inode);
4448 mutex_lock(&trace_types_lock);
4450 if (cpu == RING_BUFFER_ALL_CPUS) {
4451 int cpu, buf_size_same;
4456 /* check if all cpu sizes are same */
4457 for_each_tracing_cpu(cpu) {
4458 /* fill in the size from first enabled cpu */
4460 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4461 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
4467 if (buf_size_same) {
4468 if (!ring_buffer_expanded)
4469 r = sprintf(buf, "%lu (expanded: %lu)\n",
4471 trace_buf_size >> 10);
4473 r = sprintf(buf, "%lu\n", size >> 10);
4475 r = sprintf(buf, "X\n");
4477 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
4479 mutex_unlock(&trace_types_lock);
4481 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4486 tracing_entries_write(struct file *filp, const char __user *ubuf,
4487 size_t cnt, loff_t *ppos)
4489 struct inode *inode = file_inode(filp);
4490 struct trace_array *tr = inode->i_private;
4494 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4498 /* must have at least 1 entry */
4502 /* value is in KB */
4504 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
4514 tracing_total_entries_read(struct file *filp, char __user *ubuf,
4515 size_t cnt, loff_t *ppos)
4517 struct trace_array *tr = filp->private_data;
4520 unsigned long size = 0, expanded_size = 0;
4522 mutex_lock(&trace_types_lock);
4523 for_each_tracing_cpu(cpu) {
4524 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
4525 if (!ring_buffer_expanded)
4526 expanded_size += trace_buf_size >> 10;
4528 if (ring_buffer_expanded)
4529 r = sprintf(buf, "%lu\n", size);
4531 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4532 mutex_unlock(&trace_types_lock);
4534 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4538 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4539 size_t cnt, loff_t *ppos)
4542 * There is no need to read what the user has written, this function
4543 * is just to make sure that there is no error when "echo" is used
4552 tracing_free_buffer_release(struct inode *inode, struct file *filp)
4554 struct trace_array *tr = inode->i_private;
4556 /* disable tracing ? */
4557 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4558 tracer_tracing_off(tr);
4559 /* resize the ring buffer to 0 */
4560 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4562 trace_array_put(tr);
4568 tracing_mark_write(struct file *filp, const char __user *ubuf,
4569 size_t cnt, loff_t *fpos)
4571 unsigned long addr = (unsigned long)ubuf;
4572 struct trace_array *tr = filp->private_data;
4573 struct ring_buffer_event *event;
4574 struct ring_buffer *buffer;
4575 struct print_entry *entry;
4576 unsigned long irq_flags;
4577 struct page *pages[2];
4587 if (tracing_disabled)
4590 if (!(trace_flags & TRACE_ITER_MARKERS))
4593 if (cnt > TRACE_BUF_SIZE)
4594 cnt = TRACE_BUF_SIZE;
4597 * Userspace is injecting traces into the kernel trace buffer.
4598 * We want to be as non intrusive as possible.
4599 * To do so, we do not want to allocate any special buffers
4600 * or take any locks, but instead write the userspace data
4601 * straight into the ring buffer.
4603 * First we need to pin the userspace buffer into memory,
4604 * which, most likely it is, because it just referenced it.
4605 * But there's no guarantee that it is. By using get_user_pages_fast()
4606 * and kmap_atomic/kunmap_atomic() we can get access to the
4607 * pages directly. We then write the data directly into the
4610 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4612 /* check if we cross pages */
4613 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4616 offset = addr & (PAGE_SIZE - 1);
4619 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4620 if (ret < nr_pages) {
4622 put_page(pages[ret]);
4627 for (i = 0; i < nr_pages; i++)
4628 map_page[i] = kmap_atomic(pages[i]);
4630 local_save_flags(irq_flags);
4631 size = sizeof(*entry) + cnt + 2; /* possible \n added */
4632 buffer = tr->trace_buffer.buffer;
4633 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4634 irq_flags, preempt_count());
4636 /* Ring buffer disabled, return as if not open for write */
4641 entry = ring_buffer_event_data(event);
4642 entry->ip = _THIS_IP_;
4644 if (nr_pages == 2) {
4645 len = PAGE_SIZE - offset;
4646 memcpy(&entry->buf, map_page[0] + offset, len);
4647 memcpy(&entry->buf[len], map_page[1], cnt - len);
4649 memcpy(&entry->buf, map_page[0] + offset, cnt);
4651 if (entry->buf[cnt - 1] != '\n') {
4652 entry->buf[cnt] = '\n';
4653 entry->buf[cnt + 1] = '\0';
4655 entry->buf[cnt] = '\0';
4657 __buffer_unlock_commit(buffer, event);
4664 for (i = 0; i < nr_pages; i++){
4665 kunmap_atomic(map_page[i]);
4672 static int tracing_clock_show(struct seq_file *m, void *v)
4674 struct trace_array *tr = m->private;
4677 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4679 "%s%s%s%s", i ? " " : "",
4680 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4681 i == tr->clock_id ? "]" : "");
4687 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4688 size_t cnt, loff_t *fpos)
4690 struct seq_file *m = filp->private_data;
4691 struct trace_array *tr = m->private;
4693 const char *clockstr;
4696 if (cnt >= sizeof(buf))
4699 if (copy_from_user(&buf, ubuf, cnt))
4704 clockstr = strstrip(buf);
4706 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4707 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4710 if (i == ARRAY_SIZE(trace_clocks))
4713 mutex_lock(&trace_types_lock);
4717 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
4720 * New clock may not be consistent with the previous clock.
4721 * Reset the buffer so that it doesn't have incomparable timestamps.
4723 tracing_reset_online_cpus(&tr->trace_buffer);
4725 #ifdef CONFIG_TRACER_MAX_TRACE
4726 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4727 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4728 tracing_reset_online_cpus(&tr->max_buffer);
4731 mutex_unlock(&trace_types_lock);
4738 static int tracing_clock_open(struct inode *inode, struct file *file)
4740 struct trace_array *tr = inode->i_private;
4743 if (tracing_disabled)
4746 if (trace_array_get(tr))
4749 ret = single_open(file, tracing_clock_show, inode->i_private);
4751 trace_array_put(tr);
4756 struct ftrace_buffer_info {
4757 struct trace_iterator iter;
4762 #ifdef CONFIG_TRACER_SNAPSHOT
4763 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4765 struct trace_array *tr = inode->i_private;
4766 struct trace_iterator *iter;
4770 if (trace_array_get(tr) < 0)
4773 if (file->f_mode & FMODE_READ) {
4774 iter = __tracing_open(inode, file, true);
4776 ret = PTR_ERR(iter);
4778 /* Writes still need the seq_file to hold the private data */
4780 m = kzalloc(sizeof(*m), GFP_KERNEL);
4783 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4791 iter->trace_buffer = &tr->max_buffer;
4792 iter->cpu_file = tracing_get_cpu(inode);
4794 file->private_data = m;
4798 trace_array_put(tr);
4804 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4807 struct seq_file *m = filp->private_data;
4808 struct trace_iterator *iter = m->private;
4809 struct trace_array *tr = iter->tr;
4813 ret = tracing_update_buffers();
4817 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4821 mutex_lock(&trace_types_lock);
4823 if (tr->current_trace->use_max_tr) {
4830 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4834 if (tr->allocated_snapshot)
4838 /* Only allow per-cpu swap if the ring buffer supports it */
4839 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4840 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4845 if (!tr->allocated_snapshot) {
4846 ret = alloc_snapshot(tr);
4850 local_irq_disable();
4851 /* Now, we're going to swap */
4852 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4853 update_max_tr(tr, current, smp_processor_id());
4855 update_max_tr_single(tr, current, iter->cpu_file);
4859 if (tr->allocated_snapshot) {
4860 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4861 tracing_reset_online_cpus(&tr->max_buffer);
4863 tracing_reset(&tr->max_buffer, iter->cpu_file);
4873 mutex_unlock(&trace_types_lock);
4877 static int tracing_snapshot_release(struct inode *inode, struct file *file)
4879 struct seq_file *m = file->private_data;
4882 ret = tracing_release(inode, file);
4884 if (file->f_mode & FMODE_READ)
4887 /* If write only, the seq_file is just a stub */
4895 static int tracing_buffers_open(struct inode *inode, struct file *filp);
4896 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
4897 size_t count, loff_t *ppos);
4898 static int tracing_buffers_release(struct inode *inode, struct file *file);
4899 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4900 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
4902 static int snapshot_raw_open(struct inode *inode, struct file *filp)
4904 struct ftrace_buffer_info *info;
4907 ret = tracing_buffers_open(inode, filp);
4911 info = filp->private_data;
4913 if (info->iter.trace->use_max_tr) {
4914 tracing_buffers_release(inode, filp);
4918 info->iter.snapshot = true;
4919 info->iter.trace_buffer = &info->iter.tr->max_buffer;
4924 #endif /* CONFIG_TRACER_SNAPSHOT */
4927 static const struct file_operations tracing_max_lat_fops = {
4928 .open = tracing_open_generic,
4929 .read = tracing_max_lat_read,
4930 .write = tracing_max_lat_write,
4931 .llseek = generic_file_llseek,
4934 static const struct file_operations set_tracer_fops = {
4935 .open = tracing_open_generic,
4936 .read = tracing_set_trace_read,
4937 .write = tracing_set_trace_write,
4938 .llseek = generic_file_llseek,
4941 static const struct file_operations tracing_pipe_fops = {
4942 .open = tracing_open_pipe,
4943 .poll = tracing_poll_pipe,
4944 .read = tracing_read_pipe,
4945 .splice_read = tracing_splice_read_pipe,
4946 .release = tracing_release_pipe,
4947 .llseek = no_llseek,
4950 static const struct file_operations tracing_entries_fops = {
4951 .open = tracing_open_generic_tr,
4952 .read = tracing_entries_read,
4953 .write = tracing_entries_write,
4954 .llseek = generic_file_llseek,
4955 .release = tracing_release_generic_tr,
4958 static const struct file_operations tracing_total_entries_fops = {
4959 .open = tracing_open_generic_tr,
4960 .read = tracing_total_entries_read,
4961 .llseek = generic_file_llseek,
4962 .release = tracing_release_generic_tr,
4965 static const struct file_operations tracing_free_buffer_fops = {
4966 .open = tracing_open_generic_tr,
4967 .write = tracing_free_buffer_write,
4968 .release = tracing_free_buffer_release,
4971 static const struct file_operations tracing_mark_fops = {
4972 .open = tracing_open_generic_tr,
4973 .write = tracing_mark_write,
4974 .llseek = generic_file_llseek,
4975 .release = tracing_release_generic_tr,
4978 static const struct file_operations trace_clock_fops = {
4979 .open = tracing_clock_open,
4981 .llseek = seq_lseek,
4982 .release = tracing_single_release_tr,
4983 .write = tracing_clock_write,
4986 #ifdef CONFIG_TRACER_SNAPSHOT
4987 static const struct file_operations snapshot_fops = {
4988 .open = tracing_snapshot_open,
4990 .write = tracing_snapshot_write,
4991 .llseek = tracing_seek,
4992 .release = tracing_snapshot_release,
4995 static const struct file_operations snapshot_raw_fops = {
4996 .open = snapshot_raw_open,
4997 .read = tracing_buffers_read,
4998 .release = tracing_buffers_release,
4999 .splice_read = tracing_buffers_splice_read,
5000 .llseek = no_llseek,
5003 #endif /* CONFIG_TRACER_SNAPSHOT */
5005 static int tracing_buffers_open(struct inode *inode, struct file *filp)
5007 struct trace_array *tr = inode->i_private;
5008 struct ftrace_buffer_info *info;
5011 if (tracing_disabled)
5014 if (trace_array_get(tr) < 0)
5017 info = kzalloc(sizeof(*info), GFP_KERNEL);
5019 trace_array_put(tr);
5023 mutex_lock(&trace_types_lock);
5026 info->iter.cpu_file = tracing_get_cpu(inode);
5027 info->iter.trace = tr->current_trace;
5028 info->iter.trace_buffer = &tr->trace_buffer;
5030 /* Force reading ring buffer for first read */
5031 info->read = (unsigned int)-1;
5033 filp->private_data = info;
5035 mutex_unlock(&trace_types_lock);
5037 ret = nonseekable_open(inode, filp);
5039 trace_array_put(tr);
5045 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
5047 struct ftrace_buffer_info *info = filp->private_data;
5048 struct trace_iterator *iter = &info->iter;
5050 return trace_poll(iter, filp, poll_table);
5054 tracing_buffers_read(struct file *filp, char __user *ubuf,
5055 size_t count, loff_t *ppos)
5057 struct ftrace_buffer_info *info = filp->private_data;
5058 struct trace_iterator *iter = &info->iter;
5065 mutex_lock(&trace_types_lock);
5067 #ifdef CONFIG_TRACER_MAX_TRACE
5068 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5075 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
5081 /* Do we have previous read data to read? */
5082 if (info->read < PAGE_SIZE)
5086 trace_access_lock(iter->cpu_file);
5087 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
5091 trace_access_unlock(iter->cpu_file);
5094 if (trace_empty(iter)) {
5095 if ((filp->f_flags & O_NONBLOCK)) {
5099 mutex_unlock(&trace_types_lock);
5100 iter->trace->wait_pipe(iter);
5101 mutex_lock(&trace_types_lock);
5102 if (signal_pending(current)) {
5114 size = PAGE_SIZE - info->read;
5118 ret = copy_to_user(ubuf, info->spare + info->read, size);
5129 mutex_unlock(&trace_types_lock);
5134 static int tracing_buffers_release(struct inode *inode, struct file *file)
5136 struct ftrace_buffer_info *info = file->private_data;
5137 struct trace_iterator *iter = &info->iter;
5139 mutex_lock(&trace_types_lock);
5141 __trace_array_put(iter->tr);
5144 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
5147 mutex_unlock(&trace_types_lock);
5153 struct ring_buffer *buffer;
5158 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
5159 struct pipe_buffer *buf)
5161 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5166 ring_buffer_free_read_page(ref->buffer, ref->page);
5171 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
5172 struct pipe_buffer *buf)
5174 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5179 /* Pipe buffer operations for a buffer. */
5180 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
5182 .map = generic_pipe_buf_map,
5183 .unmap = generic_pipe_buf_unmap,
5184 .confirm = generic_pipe_buf_confirm,
5185 .release = buffer_pipe_buf_release,
5186 .steal = generic_pipe_buf_steal,
5187 .get = buffer_pipe_buf_get,
5191 * Callback from splice_to_pipe(), if we need to release some pages
5192 * at the end of the spd in case we error'ed out in filling the pipe.
5194 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
5196 struct buffer_ref *ref =
5197 (struct buffer_ref *)spd->partial[i].private;
5202 ring_buffer_free_read_page(ref->buffer, ref->page);
5204 spd->partial[i].private = 0;
5208 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5209 struct pipe_inode_info *pipe, size_t len,
5212 struct ftrace_buffer_info *info = file->private_data;
5213 struct trace_iterator *iter = &info->iter;
5214 struct partial_page partial_def[PIPE_DEF_BUFFERS];
5215 struct page *pages_def[PIPE_DEF_BUFFERS];
5216 struct splice_pipe_desc spd = {
5218 .partial = partial_def,
5219 .nr_pages_max = PIPE_DEF_BUFFERS,
5221 .ops = &buffer_pipe_buf_ops,
5222 .spd_release = buffer_spd_release,
5224 struct buffer_ref *ref;
5225 int entries, size, i;
5228 mutex_lock(&trace_types_lock);
5230 #ifdef CONFIG_TRACER_MAX_TRACE
5231 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5237 if (splice_grow_spd(pipe, &spd)) {
5242 if (*ppos & (PAGE_SIZE - 1)) {
5247 if (len & (PAGE_SIZE - 1)) {
5248 if (len < PAGE_SIZE) {
5256 trace_access_lock(iter->cpu_file);
5257 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5259 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
5263 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
5268 ref->buffer = iter->trace_buffer->buffer;
5269 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
5275 r = ring_buffer_read_page(ref->buffer, &ref->page,
5276 len, iter->cpu_file, 1);
5278 ring_buffer_free_read_page(ref->buffer, ref->page);
5284 * zero out any left over data, this is going to
5287 size = ring_buffer_page_len(ref->page);
5288 if (size < PAGE_SIZE)
5289 memset(ref->page + size, 0, PAGE_SIZE - size);
5291 page = virt_to_page(ref->page);
5293 spd.pages[i] = page;
5294 spd.partial[i].len = PAGE_SIZE;
5295 spd.partial[i].offset = 0;
5296 spd.partial[i].private = (unsigned long)ref;
5300 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5303 trace_access_unlock(iter->cpu_file);
5306 /* did we read anything? */
5307 if (!spd.nr_pages) {
5308 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
5312 mutex_unlock(&trace_types_lock);
5313 iter->trace->wait_pipe(iter);
5314 mutex_lock(&trace_types_lock);
5315 if (signal_pending(current)) {
5322 ret = splice_to_pipe(pipe, &spd);
5323 splice_shrink_spd(&spd);
5325 mutex_unlock(&trace_types_lock);
5330 static const struct file_operations tracing_buffers_fops = {
5331 .open = tracing_buffers_open,
5332 .read = tracing_buffers_read,
5333 .poll = tracing_buffers_poll,
5334 .release = tracing_buffers_release,
5335 .splice_read = tracing_buffers_splice_read,
5336 .llseek = no_llseek,
5340 tracing_stats_read(struct file *filp, char __user *ubuf,
5341 size_t count, loff_t *ppos)
5343 struct inode *inode = file_inode(filp);
5344 struct trace_array *tr = inode->i_private;
5345 struct trace_buffer *trace_buf = &tr->trace_buffer;
5346 int cpu = tracing_get_cpu(inode);
5347 struct trace_seq *s;
5349 unsigned long long t;
5350 unsigned long usec_rem;
5352 s = kmalloc(sizeof(*s), GFP_KERNEL);
5358 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
5359 trace_seq_printf(s, "entries: %ld\n", cnt);
5361 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
5362 trace_seq_printf(s, "overrun: %ld\n", cnt);
5364 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
5365 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5367 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
5368 trace_seq_printf(s, "bytes: %ld\n", cnt);
5370 if (trace_clocks[tr->clock_id].in_ns) {
5371 /* local or global for trace_clock */
5372 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5373 usec_rem = do_div(t, USEC_PER_SEC);
5374 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5377 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
5378 usec_rem = do_div(t, USEC_PER_SEC);
5379 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5381 /* counter or tsc mode for trace_clock */
5382 trace_seq_printf(s, "oldest event ts: %llu\n",
5383 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5385 trace_seq_printf(s, "now ts: %llu\n",
5386 ring_buffer_time_stamp(trace_buf->buffer, cpu));
5389 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
5390 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5392 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
5393 trace_seq_printf(s, "read events: %ld\n", cnt);
5395 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
5402 static const struct file_operations tracing_stats_fops = {
5403 .open = tracing_open_generic_tr,
5404 .read = tracing_stats_read,
5405 .llseek = generic_file_llseek,
5406 .release = tracing_release_generic_tr,
5409 #ifdef CONFIG_DYNAMIC_FTRACE
5411 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
5417 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
5418 size_t cnt, loff_t *ppos)
5420 static char ftrace_dyn_info_buffer[1024];
5421 static DEFINE_MUTEX(dyn_info_mutex);
5422 unsigned long *p = filp->private_data;
5423 char *buf = ftrace_dyn_info_buffer;
5424 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
5427 mutex_lock(&dyn_info_mutex);
5428 r = sprintf(buf, "%ld ", *p);
5430 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
5433 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5435 mutex_unlock(&dyn_info_mutex);
5440 static const struct file_operations tracing_dyn_info_fops = {
5441 .open = tracing_open_generic,
5442 .read = tracing_read_dyn_info,
5443 .llseek = generic_file_llseek,
5445 #endif /* CONFIG_DYNAMIC_FTRACE */
5447 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5449 ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5455 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5457 unsigned long *count = (long *)data;
5469 ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5470 struct ftrace_probe_ops *ops, void *data)
5472 long count = (long)data;
5474 seq_printf(m, "%ps:", (void *)ip);
5476 seq_printf(m, "snapshot");
5479 seq_printf(m, ":unlimited\n");
5481 seq_printf(m, ":count=%ld\n", count);
5486 static struct ftrace_probe_ops snapshot_probe_ops = {
5487 .func = ftrace_snapshot,
5488 .print = ftrace_snapshot_print,
5491 static struct ftrace_probe_ops snapshot_count_probe_ops = {
5492 .func = ftrace_count_snapshot,
5493 .print = ftrace_snapshot_print,
5497 ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5498 char *glob, char *cmd, char *param, int enable)
5500 struct ftrace_probe_ops *ops;
5501 void *count = (void *)-1;
5505 /* hash funcs only work with set_ftrace_filter */
5509 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
5511 if (glob[0] == '!') {
5512 unregister_ftrace_function_probe_func(glob+1, ops);
5519 number = strsep(¶m, ":");
5521 if (!strlen(number))
5525 * We use the callback data field (which is a pointer)
5528 ret = kstrtoul(number, 0, (unsigned long *)&count);
5533 ret = register_ftrace_function_probe(glob, ops, count);
5536 alloc_snapshot(&global_trace);
5538 return ret < 0 ? ret : 0;
5541 static struct ftrace_func_command ftrace_snapshot_cmd = {
5543 .func = ftrace_trace_snapshot_callback,
5546 static int register_snapshot_cmd(void)
5548 return register_ftrace_command(&ftrace_snapshot_cmd);
5551 static inline int register_snapshot_cmd(void) { return 0; }
5552 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
5554 struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
5559 if (!debugfs_initialized())
5562 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5563 tr->dir = debugfs_create_dir("tracing", NULL);
5566 pr_warn_once("Could not create debugfs directory 'tracing'\n");
5571 struct dentry *tracing_init_dentry(void)
5573 return tracing_init_dentry_tr(&global_trace);
5576 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
5578 struct dentry *d_tracer;
5581 return tr->percpu_dir;
5583 d_tracer = tracing_init_dentry_tr(tr);
5587 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
5589 WARN_ONCE(!tr->percpu_dir,
5590 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
5592 return tr->percpu_dir;
5595 static struct dentry *
5596 trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
5597 void *data, long cpu, const struct file_operations *fops)
5599 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
5601 if (ret) /* See tracing_get_cpu() */
5602 ret->d_inode->i_cdev = (void *)(cpu + 1);
5607 tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
5609 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5610 struct dentry *d_cpu;
5611 char cpu_dir[30]; /* 30 characters should be more than enough */
5616 snprintf(cpu_dir, 30, "cpu%ld", cpu);
5617 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5619 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5623 /* per cpu trace_pipe */
5624 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
5625 tr, cpu, &tracing_pipe_fops);
5628 trace_create_cpu_file("trace", 0644, d_cpu,
5629 tr, cpu, &tracing_fops);
5631 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
5632 tr, cpu, &tracing_buffers_fops);
5634 trace_create_cpu_file("stats", 0444, d_cpu,
5635 tr, cpu, &tracing_stats_fops);
5637 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
5638 tr, cpu, &tracing_entries_fops);
5640 #ifdef CONFIG_TRACER_SNAPSHOT
5641 trace_create_cpu_file("snapshot", 0644, d_cpu,
5642 tr, cpu, &snapshot_fops);
5644 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
5645 tr, cpu, &snapshot_raw_fops);
5649 #ifdef CONFIG_FTRACE_SELFTEST
5650 /* Let selftest have access to static functions in this file */
5651 #include "trace_selftest.c"
5654 struct trace_option_dentry {
5655 struct tracer_opt *opt;
5656 struct tracer_flags *flags;
5657 struct trace_array *tr;
5658 struct dentry *entry;
5662 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5665 struct trace_option_dentry *topt = filp->private_data;
5668 if (topt->flags->val & topt->opt->bit)
5673 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5677 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5680 struct trace_option_dentry *topt = filp->private_data;
5684 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5688 if (val != 0 && val != 1)
5691 if (!!(topt->flags->val & topt->opt->bit) != val) {
5692 mutex_lock(&trace_types_lock);
5693 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
5695 mutex_unlock(&trace_types_lock);
5706 static const struct file_operations trace_options_fops = {
5707 .open = tracing_open_generic,
5708 .read = trace_options_read,
5709 .write = trace_options_write,
5710 .llseek = generic_file_llseek,
5714 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5717 long index = (long)filp->private_data;
5720 if (trace_flags & (1 << index))
5725 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5729 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5732 struct trace_array *tr = &global_trace;
5733 long index = (long)filp->private_data;
5737 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5741 if (val != 0 && val != 1)
5744 mutex_lock(&trace_types_lock);
5745 ret = set_tracer_flag(tr, 1 << index, val);
5746 mutex_unlock(&trace_types_lock);
5756 static const struct file_operations trace_options_core_fops = {
5757 .open = tracing_open_generic,
5758 .read = trace_options_core_read,
5759 .write = trace_options_core_write,
5760 .llseek = generic_file_llseek,
5763 struct dentry *trace_create_file(const char *name,
5765 struct dentry *parent,
5767 const struct file_operations *fops)
5771 ret = debugfs_create_file(name, mode, parent, data, fops);
5773 pr_warning("Could not create debugfs '%s' entry\n", name);
5779 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
5781 struct dentry *d_tracer;
5786 d_tracer = tracing_init_dentry_tr(tr);
5790 tr->options = debugfs_create_dir("options", d_tracer);
5792 pr_warning("Could not create debugfs directory 'options'\n");
5800 create_trace_option_file(struct trace_array *tr,
5801 struct trace_option_dentry *topt,
5802 struct tracer_flags *flags,
5803 struct tracer_opt *opt)
5805 struct dentry *t_options;
5807 t_options = trace_options_init_dentry(tr);
5811 topt->flags = flags;
5815 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
5816 &trace_options_fops);
5820 static struct trace_option_dentry *
5821 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
5823 struct trace_option_dentry *topts;
5824 struct tracer_flags *flags;
5825 struct tracer_opt *opts;
5831 flags = tracer->flags;
5833 if (!flags || !flags->opts)
5838 for (cnt = 0; opts[cnt].name; cnt++)
5841 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
5845 for (cnt = 0; opts[cnt].name; cnt++)
5846 create_trace_option_file(tr, &topts[cnt], flags,
5853 destroy_trace_option_files(struct trace_option_dentry *topts)
5860 for (cnt = 0; topts[cnt].opt; cnt++) {
5861 if (topts[cnt].entry)
5862 debugfs_remove(topts[cnt].entry);
5868 static struct dentry *
5869 create_trace_option_core_file(struct trace_array *tr,
5870 const char *option, long index)
5872 struct dentry *t_options;
5874 t_options = trace_options_init_dentry(tr);
5878 return trace_create_file(option, 0644, t_options, (void *)index,
5879 &trace_options_core_fops);
5882 static __init void create_trace_options_dir(struct trace_array *tr)
5884 struct dentry *t_options;
5887 t_options = trace_options_init_dentry(tr);
5891 for (i = 0; trace_options[i]; i++)
5892 create_trace_option_core_file(tr, trace_options[i], i);
5896 rb_simple_read(struct file *filp, char __user *ubuf,
5897 size_t cnt, loff_t *ppos)
5899 struct trace_array *tr = filp->private_data;
5903 r = tracer_tracing_is_on(tr);
5904 r = sprintf(buf, "%d\n", r);
5906 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5910 rb_simple_write(struct file *filp, const char __user *ubuf,
5911 size_t cnt, loff_t *ppos)
5913 struct trace_array *tr = filp->private_data;
5914 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5918 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5923 mutex_lock(&trace_types_lock);
5925 tracer_tracing_on(tr);
5926 if (tr->current_trace->start)
5927 tr->current_trace->start(tr);
5929 tracer_tracing_off(tr);
5930 if (tr->current_trace->stop)
5931 tr->current_trace->stop(tr);
5933 mutex_unlock(&trace_types_lock);
5941 static const struct file_operations rb_simple_fops = {
5942 .open = tracing_open_generic_tr,
5943 .read = rb_simple_read,
5944 .write = rb_simple_write,
5945 .release = tracing_release_generic_tr,
5946 .llseek = default_llseek,
5949 struct dentry *trace_instance_dir;
5952 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5954 static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
5958 for_each_tracing_cpu(cpu) {
5959 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
5960 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
5961 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
5966 allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
5968 enum ring_buffer_flags rb_flags;
5970 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5972 buf->buffer = ring_buffer_alloc(size, rb_flags);
5976 buf->data = alloc_percpu(struct trace_array_cpu);
5978 ring_buffer_free(buf->buffer);
5982 init_trace_buffers(tr, buf);
5984 /* Allocate the first page for all buffers */
5985 set_buffer_entries(&tr->trace_buffer,
5986 ring_buffer_size(tr->trace_buffer.buffer, 0));
5991 static int allocate_trace_buffers(struct trace_array *tr, int size)
5995 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
5999 #ifdef CONFIG_TRACER_MAX_TRACE
6000 ret = allocate_trace_buffer(tr, &tr->max_buffer,
6001 allocate_snapshot ? size : 1);
6003 ring_buffer_free(tr->trace_buffer.buffer);
6004 free_percpu(tr->trace_buffer.data);
6007 tr->allocated_snapshot = allocate_snapshot;
6010 * Only the top level trace array gets its snapshot allocated
6011 * from the kernel command line.
6013 allocate_snapshot = false;
6018 static int new_instance_create(const char *name)
6020 struct trace_array *tr;
6023 mutex_lock(&trace_types_lock);
6026 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6027 if (tr->name && strcmp(tr->name, name) == 0)
6032 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
6036 tr->name = kstrdup(name, GFP_KERNEL);
6040 raw_spin_lock_init(&tr->start_lock);
6042 tr->current_trace = &nop_trace;
6044 INIT_LIST_HEAD(&tr->systems);
6045 INIT_LIST_HEAD(&tr->events);
6047 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
6050 /* Holder for file callbacks */
6051 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
6052 tr->trace_cpu.tr = tr;
6054 tr->dir = debugfs_create_dir(name, trace_instance_dir);
6058 ret = event_trace_add_tracer(tr->dir, tr);
6060 debugfs_remove_recursive(tr->dir);
6064 init_tracer_debugfs(tr, tr->dir);
6066 list_add(&tr->list, &ftrace_trace_arrays);
6068 mutex_unlock(&trace_types_lock);
6073 if (tr->trace_buffer.buffer)
6074 ring_buffer_free(tr->trace_buffer.buffer);
6079 mutex_unlock(&trace_types_lock);
6085 static int instance_delete(const char *name)
6087 struct trace_array *tr;
6091 mutex_lock(&trace_types_lock);
6094 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6095 if (tr->name && strcmp(tr->name, name) == 0) {
6107 list_del(&tr->list);
6109 event_trace_del_tracer(tr);
6110 debugfs_remove_recursive(tr->dir);
6111 free_percpu(tr->trace_buffer.data);
6112 ring_buffer_free(tr->trace_buffer.buffer);
6120 mutex_unlock(&trace_types_lock);
6125 static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
6127 struct dentry *parent;
6130 /* Paranoid: Make sure the parent is the "instances" directory */
6131 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
6132 if (WARN_ON_ONCE(parent != trace_instance_dir))
6136 * The inode mutex is locked, but debugfs_create_dir() will also
6137 * take the mutex. As the instances directory can not be destroyed
6138 * or changed in any other way, it is safe to unlock it, and
6139 * let the dentry try. If two users try to make the same dir at
6140 * the same time, then the new_instance_create() will determine the
6143 mutex_unlock(&inode->i_mutex);
6145 ret = new_instance_create(dentry->d_iname);
6147 mutex_lock(&inode->i_mutex);
6152 static int instance_rmdir(struct inode *inode, struct dentry *dentry)
6154 struct dentry *parent;
6157 /* Paranoid: Make sure the parent is the "instances" directory */
6158 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
6159 if (WARN_ON_ONCE(parent != trace_instance_dir))
6162 /* The caller did a dget() on dentry */
6163 mutex_unlock(&dentry->d_inode->i_mutex);
6166 * The inode mutex is locked, but debugfs_create_dir() will also
6167 * take the mutex. As the instances directory can not be destroyed
6168 * or changed in any other way, it is safe to unlock it, and
6169 * let the dentry try. If two users try to make the same dir at
6170 * the same time, then the instance_delete() will determine the
6173 mutex_unlock(&inode->i_mutex);
6175 ret = instance_delete(dentry->d_iname);
6177 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
6178 mutex_lock(&dentry->d_inode->i_mutex);
6183 static const struct inode_operations instance_dir_inode_operations = {
6184 .lookup = simple_lookup,
6185 .mkdir = instance_mkdir,
6186 .rmdir = instance_rmdir,
6189 static __init void create_trace_instances(struct dentry *d_tracer)
6191 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
6192 if (WARN_ON(!trace_instance_dir))
6195 /* Hijack the dir inode operations, to allow mkdir */
6196 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
6200 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
6204 trace_create_file("trace_options", 0644, d_tracer,
6205 tr, &tracing_iter_fops);
6207 trace_create_file("trace", 0644, d_tracer,
6210 trace_create_file("trace_pipe", 0444, d_tracer,
6211 tr, &tracing_pipe_fops);
6213 trace_create_file("buffer_size_kb", 0644, d_tracer,
6214 tr, &tracing_entries_fops);
6216 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
6217 tr, &tracing_total_entries_fops);
6219 trace_create_file("free_buffer", 0644, d_tracer,
6220 tr, &tracing_free_buffer_fops);
6222 trace_create_file("trace_marker", 0220, d_tracer,
6223 tr, &tracing_mark_fops);
6225 trace_create_file("saved_tgids", 0444, d_tracer,
6226 tr, &tracing_saved_tgids_fops);
6228 trace_create_file("trace_clock", 0644, d_tracer, tr,
6231 trace_create_file("tracing_on", 0644, d_tracer,
6232 tr, &rb_simple_fops);
6234 #ifdef CONFIG_TRACER_SNAPSHOT
6235 trace_create_file("snapshot", 0644, d_tracer,
6236 tr, &snapshot_fops);
6239 for_each_tracing_cpu(cpu)
6240 tracing_init_debugfs_percpu(tr, cpu);
6244 static __init int tracer_init_debugfs(void)
6246 struct dentry *d_tracer;
6248 trace_access_lock_init();
6250 d_tracer = tracing_init_dentry();
6254 init_tracer_debugfs(&global_trace, d_tracer);
6256 trace_create_file("tracing_cpumask", 0644, d_tracer,
6257 &global_trace, &tracing_cpumask_fops);
6259 trace_create_file("available_tracers", 0444, d_tracer,
6260 &global_trace, &show_traces_fops);
6262 trace_create_file("current_tracer", 0644, d_tracer,
6263 &global_trace, &set_tracer_fops);
6265 #ifdef CONFIG_TRACER_MAX_TRACE
6266 trace_create_file("tracing_max_latency", 0644, d_tracer,
6267 &tracing_max_latency, &tracing_max_lat_fops);
6270 trace_create_file("tracing_thresh", 0644, d_tracer,
6271 &tracing_thresh, &tracing_max_lat_fops);
6273 trace_create_file("README", 0444, d_tracer,
6274 NULL, &tracing_readme_fops);
6276 trace_create_file("saved_cmdlines", 0444, d_tracer,
6277 NULL, &tracing_saved_cmdlines_fops);
6279 #ifdef CONFIG_DYNAMIC_FTRACE
6280 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
6281 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
6284 create_trace_instances(d_tracer);
6286 create_trace_options_dir(&global_trace);
6291 static int trace_panic_handler(struct notifier_block *this,
6292 unsigned long event, void *unused)
6294 if (ftrace_dump_on_oops)
6295 ftrace_dump(ftrace_dump_on_oops);
6299 static struct notifier_block trace_panic_notifier = {
6300 .notifier_call = trace_panic_handler,
6302 .priority = 150 /* priority: INT_MAX >= x >= 0 */
6305 static int trace_die_handler(struct notifier_block *self,
6311 if (ftrace_dump_on_oops)
6312 ftrace_dump(ftrace_dump_on_oops);
6320 static struct notifier_block trace_die_notifier = {
6321 .notifier_call = trace_die_handler,
6326 * printk is set to max of 1024, we really don't need it that big.
6327 * Nothing should be printing 1000 characters anyway.
6329 #define TRACE_MAX_PRINT 1000
6332 * Define here KERN_TRACE so that we have one place to modify
6333 * it if we decide to change what log level the ftrace dump
6336 #define KERN_TRACE KERN_EMERG
6339 trace_printk_seq(struct trace_seq *s)
6341 /* Probably should print a warning here. */
6342 if (s->len >= TRACE_MAX_PRINT)
6343 s->len = TRACE_MAX_PRINT;
6345 /* should be zero ended, but we are paranoid. */
6346 s->buffer[s->len] = 0;
6348 printk(KERN_TRACE "%s", s->buffer);
6353 void trace_init_global_iter(struct trace_iterator *iter)
6355 iter->tr = &global_trace;
6356 iter->trace = iter->tr->current_trace;
6357 iter->cpu_file = RING_BUFFER_ALL_CPUS;
6358 iter->trace_buffer = &global_trace.trace_buffer;
6361 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
6363 /* use static because iter can be a bit big for the stack */
6364 static struct trace_iterator iter;
6365 static atomic_t dump_running;
6366 unsigned int old_userobj;
6367 unsigned long flags;
6370 /* Only allow one dump user at a time. */
6371 if (atomic_inc_return(&dump_running) != 1) {
6372 atomic_dec(&dump_running);
6377 * Always turn off tracing when we dump.
6378 * We don't need to show trace output of what happens
6379 * between multiple crashes.
6381 * If the user does a sysrq-z, then they can re-enable
6382 * tracing with echo 1 > tracing_on.
6386 local_irq_save(flags);
6388 /* Simulate the iterator */
6389 trace_init_global_iter(&iter);
6391 for_each_tracing_cpu(cpu) {
6392 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
6395 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
6397 /* don't look at user memory in panic mode */
6398 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
6400 switch (oops_dump_mode) {
6402 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6405 iter.cpu_file = raw_smp_processor_id();
6410 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
6411 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6414 printk(KERN_TRACE "Dumping ftrace buffer:\n");
6416 /* Did function tracer already get disabled? */
6417 if (ftrace_is_dead()) {
6418 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
6419 printk("# MAY BE MISSING FUNCTION EVENTS\n");
6423 * We need to stop all tracing on all CPUS to read the
6424 * the next buffer. This is a bit expensive, but is
6425 * not done often. We fill all what we can read,
6426 * and then release the locks again.
6429 while (!trace_empty(&iter)) {
6432 printk(KERN_TRACE "---------------------------------\n");
6436 /* reset all but tr, trace, and overruns */
6437 memset(&iter.seq, 0,
6438 sizeof(struct trace_iterator) -
6439 offsetof(struct trace_iterator, seq));
6440 iter.iter_flags |= TRACE_FILE_LAT_FMT;
6443 if (trace_find_next_entry_inc(&iter) != NULL) {
6446 ret = print_trace_line(&iter);
6447 if (ret != TRACE_TYPE_NO_CONSUME)
6448 trace_consume(&iter);
6450 touch_nmi_watchdog();
6452 trace_printk_seq(&iter.seq);
6456 printk(KERN_TRACE " (ftrace buffer empty)\n");
6458 printk(KERN_TRACE "---------------------------------\n");
6461 trace_flags |= old_userobj;
6463 for_each_tracing_cpu(cpu) {
6464 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
6466 atomic_dec(&dump_running);
6467 local_irq_restore(flags);
6469 EXPORT_SYMBOL_GPL(ftrace_dump);
6471 __init static int tracer_alloc_buffers(void)
6477 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
6480 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
6481 goto out_free_buffer_mask;
6483 /* Only allocate trace_printk buffers if a trace_printk exists */
6484 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
6485 /* Must be called before global_trace.buffer is allocated */
6486 trace_printk_init_buffers();
6488 /* To save memory, keep the ring buffer size to its minimum */
6489 if (ring_buffer_expanded)
6490 ring_buf_size = trace_buf_size;
6494 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
6495 cpumask_copy(tracing_cpumask, cpu_all_mask);
6497 raw_spin_lock_init(&global_trace.start_lock);
6499 /* TODO: make the number of buffers hot pluggable with CPUS */
6500 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
6501 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
6503 goto out_free_cpumask;
6506 if (global_trace.buffer_disabled)
6509 trace_init_cmdlines();
6512 * register_tracer() might reference current_trace, so it
6513 * needs to be set before we register anything. This is
6514 * just a bootstrap of current_trace anyway.
6516 global_trace.current_trace = &nop_trace;
6518 register_tracer(&nop_trace);
6520 /* All seems OK, enable tracing */
6521 tracing_disabled = 0;
6523 atomic_notifier_chain_register(&panic_notifier_list,
6524 &trace_panic_notifier);
6526 register_die_notifier(&trace_die_notifier);
6528 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
6530 /* Holder for file callbacks */
6531 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
6532 global_trace.trace_cpu.tr = &global_trace;
6534 INIT_LIST_HEAD(&global_trace.systems);
6535 INIT_LIST_HEAD(&global_trace.events);
6536 list_add(&global_trace.list, &ftrace_trace_arrays);
6538 while (trace_boot_options) {
6541 option = strsep(&trace_boot_options, ",");
6542 trace_set_options(&global_trace, option);
6545 register_snapshot_cmd();
6550 free_percpu(global_trace.trace_buffer.data);
6551 #ifdef CONFIG_TRACER_MAX_TRACE
6552 free_percpu(global_trace.max_buffer.data);
6554 free_cpumask_var(tracing_cpumask);
6555 out_free_buffer_mask:
6556 free_cpumask_var(tracing_buffer_mask);
6561 __init static int clear_boot_tracer(void)
6564 * The default tracer at boot buffer is an init section.
6565 * This function is called in lateinit. If we did not
6566 * find the boot tracer, then clear it out, to prevent
6567 * later registration from accessing the buffer that is
6568 * about to be freed.
6570 if (!default_bootup_tracer)
6573 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6574 default_bootup_tracer);
6575 default_bootup_tracer = NULL;
6580 early_initcall(tracer_alloc_buffers);
6581 fs_initcall(tracer_init_debugfs);
6582 late_initcall(clear_boot_tracer);