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 if (unlikely(tracing_selftest_running || tracing_disabled))
430 alloc = sizeof(*entry) + size + 2; /* possible \n added */
432 local_save_flags(irq_flags);
433 buffer = global_trace.trace_buffer.buffer;
434 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
435 irq_flags, preempt_count());
439 entry = ring_buffer_event_data(event);
442 memcpy(&entry->buf, str, size);
444 /* Add a newline if necessary */
445 if (entry->buf[size - 1] != '\n') {
446 entry->buf[size] = '\n';
447 entry->buf[size + 1] = '\0';
449 entry->buf[size] = '\0';
451 __buffer_unlock_commit(buffer, event);
455 EXPORT_SYMBOL_GPL(__trace_puts);
458 * __trace_bputs - write the pointer to a constant string into trace buffer
459 * @ip: The address of the caller
460 * @str: The constant string to write to the buffer to
462 int __trace_bputs(unsigned long ip, const char *str)
464 struct ring_buffer_event *event;
465 struct ring_buffer *buffer;
466 struct bputs_entry *entry;
467 unsigned long irq_flags;
468 int size = sizeof(struct bputs_entry);
470 if (unlikely(tracing_selftest_running || tracing_disabled))
473 local_save_flags(irq_flags);
474 buffer = global_trace.trace_buffer.buffer;
475 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
476 irq_flags, preempt_count());
480 entry = ring_buffer_event_data(event);
484 __buffer_unlock_commit(buffer, event);
488 EXPORT_SYMBOL_GPL(__trace_bputs);
490 #ifdef CONFIG_TRACER_SNAPSHOT
492 * trace_snapshot - take a snapshot of the current buffer.
494 * This causes a swap between the snapshot buffer and the current live
495 * tracing buffer. You can use this to take snapshots of the live
496 * trace when some condition is triggered, but continue to trace.
498 * Note, make sure to allocate the snapshot with either
499 * a tracing_snapshot_alloc(), or by doing it manually
500 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
502 * If the snapshot buffer is not allocated, it will stop tracing.
503 * Basically making a permanent snapshot.
505 void tracing_snapshot(void)
507 struct trace_array *tr = &global_trace;
508 struct tracer *tracer = tr->current_trace;
512 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
513 internal_trace_puts("*** snapshot is being ignored ***\n");
517 if (!tr->allocated_snapshot) {
518 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
519 internal_trace_puts("*** stopping trace here! ***\n");
524 /* Note, snapshot can not be used when the tracer uses it */
525 if (tracer->use_max_tr) {
526 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
527 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
531 local_irq_save(flags);
532 update_max_tr(tr, current, smp_processor_id());
533 local_irq_restore(flags);
535 EXPORT_SYMBOL_GPL(tracing_snapshot);
537 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
538 struct trace_buffer *size_buf, int cpu_id);
539 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
541 static int alloc_snapshot(struct trace_array *tr)
545 if (!tr->allocated_snapshot) {
547 /* allocate spare buffer */
548 ret = resize_buffer_duplicate_size(&tr->max_buffer,
549 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
553 tr->allocated_snapshot = true;
559 void free_snapshot(struct trace_array *tr)
562 * We don't free the ring buffer. instead, resize it because
563 * The max_tr ring buffer has some state (e.g. ring->clock) and
564 * we want preserve it.
566 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
567 set_buffer_entries(&tr->max_buffer, 1);
568 tracing_reset_online_cpus(&tr->max_buffer);
569 tr->allocated_snapshot = false;
573 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
575 * This is similar to trace_snapshot(), but it will allocate the
576 * snapshot buffer if it isn't already allocated. Use this only
577 * where it is safe to sleep, as the allocation may sleep.
579 * This causes a swap between the snapshot buffer and the current live
580 * tracing buffer. You can use this to take snapshots of the live
581 * trace when some condition is triggered, but continue to trace.
583 void tracing_snapshot_alloc(void)
585 struct trace_array *tr = &global_trace;
588 ret = alloc_snapshot(tr);
589 if (WARN_ON(ret < 0))
594 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
596 void tracing_snapshot(void)
598 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
600 EXPORT_SYMBOL_GPL(tracing_snapshot);
601 void tracing_snapshot_alloc(void)
606 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
607 #endif /* CONFIG_TRACER_SNAPSHOT */
609 void tracer_tracing_off(struct trace_array *tr)
611 if (tr->trace_buffer.buffer)
612 ring_buffer_record_off(tr->trace_buffer.buffer);
614 * This flag is looked at when buffers haven't been allocated
615 * yet, or by some tracers (like irqsoff), that just want to
616 * know if the ring buffer has been disabled, but it can handle
617 * races of where it gets disabled but we still do a record.
618 * As the check is in the fast path of the tracers, it is more
619 * important to be fast than accurate.
621 tr->buffer_disabled = 1;
622 /* Make the flag seen by readers */
627 * tracing_off - turn off tracing buffers
629 * This function stops the tracing buffers from recording data.
630 * It does not disable any overhead the tracers themselves may
631 * be causing. This function simply causes all recording to
632 * the ring buffers to fail.
634 void tracing_off(void)
636 tracer_tracing_off(&global_trace);
638 EXPORT_SYMBOL_GPL(tracing_off);
641 * tracer_tracing_is_on - show real state of ring buffer enabled
642 * @tr : the trace array to know if ring buffer is enabled
644 * Shows real state of the ring buffer if it is enabled or not.
646 int tracer_tracing_is_on(struct trace_array *tr)
648 if (tr->trace_buffer.buffer)
649 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
650 return !tr->buffer_disabled;
654 * tracing_is_on - show state of ring buffers enabled
656 int tracing_is_on(void)
658 return tracer_tracing_is_on(&global_trace);
660 EXPORT_SYMBOL_GPL(tracing_is_on);
662 static int __init set_buf_size(char *str)
664 unsigned long buf_size;
668 buf_size = memparse(str, &str);
669 /* nr_entries can not be zero */
672 trace_buf_size = buf_size;
675 __setup("trace_buf_size=", set_buf_size);
677 static int __init set_tracing_thresh(char *str)
679 unsigned long threshold;
684 ret = kstrtoul(str, 0, &threshold);
687 tracing_thresh = threshold * 1000;
690 __setup("tracing_thresh=", set_tracing_thresh);
692 unsigned long nsecs_to_usecs(unsigned long nsecs)
697 /* These must match the bit postions in trace_iterator_flags */
698 static const char *trace_options[] = {
732 int in_ns; /* is this clock in nanoseconds? */
734 { trace_clock_local, "local", 1 },
735 { trace_clock_global, "global", 1 },
736 { trace_clock_counter, "counter", 0 },
737 { trace_clock_jiffies, "uptime", 1 },
738 { trace_clock, "perf", 1 },
743 * trace_parser_get_init - gets the buffer for trace parser
745 int trace_parser_get_init(struct trace_parser *parser, int size)
747 memset(parser, 0, sizeof(*parser));
749 parser->buffer = kmalloc(size, GFP_KERNEL);
758 * trace_parser_put - frees the buffer for trace parser
760 void trace_parser_put(struct trace_parser *parser)
762 kfree(parser->buffer);
766 * trace_get_user - reads the user input string separated by space
767 * (matched by isspace(ch))
769 * For each string found the 'struct trace_parser' is updated,
770 * and the function returns.
772 * Returns number of bytes read.
774 * See kernel/trace/trace.h for 'struct trace_parser' details.
776 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
777 size_t cnt, loff_t *ppos)
784 trace_parser_clear(parser);
786 ret = get_user(ch, ubuf++);
794 * The parser is not finished with the last write,
795 * continue reading the user input without skipping spaces.
798 /* skip white space */
799 while (cnt && isspace(ch)) {
800 ret = get_user(ch, ubuf++);
807 /* only spaces were written */
817 /* read the non-space input */
818 while (cnt && !isspace(ch)) {
819 if (parser->idx < parser->size - 1)
820 parser->buffer[parser->idx++] = ch;
825 ret = get_user(ch, ubuf++);
832 /* We either got finished input or we have to wait for another call. */
834 parser->buffer[parser->idx] = 0;
835 parser->cont = false;
836 } else if (parser->idx < parser->size - 1) {
838 parser->buffer[parser->idx++] = ch;
851 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
859 if (s->len <= s->readpos)
862 len = s->len - s->readpos;
865 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
875 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
879 if (s->len <= s->readpos)
882 len = s->len - s->readpos;
885 memcpy(buf, s->buffer + s->readpos, cnt);
892 * ftrace_max_lock is used to protect the swapping of buffers
893 * when taking a max snapshot. The buffers themselves are
894 * protected by per_cpu spinlocks. But the action of the swap
895 * needs its own lock.
897 * This is defined as a arch_spinlock_t in order to help
898 * with performance when lockdep debugging is enabled.
900 * It is also used in other places outside the update_max_tr
901 * so it needs to be defined outside of the
902 * CONFIG_TRACER_MAX_TRACE.
904 static arch_spinlock_t ftrace_max_lock =
905 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
907 unsigned long __read_mostly tracing_thresh;
909 #ifdef CONFIG_TRACER_MAX_TRACE
910 unsigned long __read_mostly tracing_max_latency;
913 * Copy the new maximum trace into the separate maximum-trace
914 * structure. (this way the maximum trace is permanently saved,
915 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
918 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
920 struct trace_buffer *trace_buf = &tr->trace_buffer;
921 struct trace_buffer *max_buf = &tr->max_buffer;
922 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
923 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
926 max_buf->time_start = data->preempt_timestamp;
928 max_data->saved_latency = tracing_max_latency;
929 max_data->critical_start = data->critical_start;
930 max_data->critical_end = data->critical_end;
932 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
933 max_data->pid = tsk->pid;
935 * If tsk == current, then use current_uid(), as that does not use
936 * RCU. The irq tracer can be called out of RCU scope.
939 max_data->uid = current_uid();
941 max_data->uid = task_uid(tsk);
943 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
944 max_data->policy = tsk->policy;
945 max_data->rt_priority = tsk->rt_priority;
947 /* record this tasks comm */
948 tracing_record_cmdline(tsk);
952 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
954 * @tsk: the task with the latency
955 * @cpu: The cpu that initiated the trace.
957 * Flip the buffers between the @tr and the max_tr and record information
958 * about which task was the cause of this latency.
961 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
963 struct ring_buffer *buf;
968 WARN_ON_ONCE(!irqs_disabled());
970 if (!tr->allocated_snapshot) {
971 /* Only the nop tracer should hit this when disabling */
972 WARN_ON_ONCE(tr->current_trace != &nop_trace);
976 arch_spin_lock(&ftrace_max_lock);
978 buf = tr->trace_buffer.buffer;
979 tr->trace_buffer.buffer = tr->max_buffer.buffer;
980 tr->max_buffer.buffer = buf;
982 __update_max_tr(tr, tsk, cpu);
983 arch_spin_unlock(&ftrace_max_lock);
987 * update_max_tr_single - only copy one trace over, and reset the rest
989 * @tsk - task with the latency
990 * @cpu - the cpu of the buffer to copy.
992 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
995 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1002 WARN_ON_ONCE(!irqs_disabled());
1003 if (!tr->allocated_snapshot) {
1004 /* Only the nop tracer should hit this when disabling */
1005 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1009 arch_spin_lock(&ftrace_max_lock);
1011 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
1013 if (ret == -EBUSY) {
1015 * We failed to swap the buffer due to a commit taking
1016 * place on this CPU. We fail to record, but we reset
1017 * the max trace buffer (no one writes directly to it)
1018 * and flag that it failed.
1020 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
1021 "Failed to swap buffers due to commit in progress\n");
1024 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
1026 __update_max_tr(tr, tsk, cpu);
1027 arch_spin_unlock(&ftrace_max_lock);
1029 #endif /* CONFIG_TRACER_MAX_TRACE */
1031 static void default_wait_pipe(struct trace_iterator *iter)
1033 /* Iterators are static, they should be filled or empty */
1034 if (trace_buffer_iter(iter, iter->cpu_file))
1037 ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
1040 #ifdef CONFIG_FTRACE_STARTUP_TEST
1041 static int run_tracer_selftest(struct tracer *type)
1043 struct trace_array *tr = &global_trace;
1044 struct tracer *saved_tracer = tr->current_trace;
1047 if (!type->selftest || tracing_selftest_disabled)
1051 * Run a selftest on this tracer.
1052 * Here we reset the trace buffer, and set the current
1053 * tracer to be this tracer. The tracer can then run some
1054 * internal tracing to verify that everything is in order.
1055 * If we fail, we do not register this tracer.
1057 tracing_reset_online_cpus(&tr->trace_buffer);
1059 tr->current_trace = type;
1061 #ifdef CONFIG_TRACER_MAX_TRACE
1062 if (type->use_max_tr) {
1063 /* If we expanded the buffers, make sure the max is expanded too */
1064 if (ring_buffer_expanded)
1065 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1066 RING_BUFFER_ALL_CPUS);
1067 tr->allocated_snapshot = true;
1071 /* the test is responsible for initializing and enabling */
1072 pr_info("Testing tracer %s: ", type->name);
1073 ret = type->selftest(type, tr);
1074 /* the test is responsible for resetting too */
1075 tr->current_trace = saved_tracer;
1077 printk(KERN_CONT "FAILED!\n");
1078 /* Add the warning after printing 'FAILED' */
1082 /* Only reset on passing, to avoid touching corrupted buffers */
1083 tracing_reset_online_cpus(&tr->trace_buffer);
1085 #ifdef CONFIG_TRACER_MAX_TRACE
1086 if (type->use_max_tr) {
1087 tr->allocated_snapshot = false;
1089 /* Shrink the max buffer again */
1090 if (ring_buffer_expanded)
1091 ring_buffer_resize(tr->max_buffer.buffer, 1,
1092 RING_BUFFER_ALL_CPUS);
1096 printk(KERN_CONT "PASSED\n");
1100 static inline int run_tracer_selftest(struct tracer *type)
1104 #endif /* CONFIG_FTRACE_STARTUP_TEST */
1107 * register_tracer - register a tracer with the ftrace system.
1108 * @type - the plugin for the tracer
1110 * Register a new plugin tracer.
1112 int register_tracer(struct tracer *type)
1118 pr_info("Tracer must have a name\n");
1122 if (strlen(type->name) >= MAX_TRACER_SIZE) {
1123 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1127 mutex_lock(&trace_types_lock);
1129 tracing_selftest_running = true;
1131 for (t = trace_types; t; t = t->next) {
1132 if (strcmp(type->name, t->name) == 0) {
1134 pr_info("Tracer %s already registered\n",
1141 if (!type->set_flag)
1142 type->set_flag = &dummy_set_flag;
1144 type->flags = &dummy_tracer_flags;
1146 if (!type->flags->opts)
1147 type->flags->opts = dummy_tracer_opt;
1148 if (!type->wait_pipe)
1149 type->wait_pipe = default_wait_pipe;
1151 ret = run_tracer_selftest(type);
1155 type->next = trace_types;
1159 tracing_selftest_running = false;
1160 mutex_unlock(&trace_types_lock);
1162 if (ret || !default_bootup_tracer)
1165 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
1168 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1169 /* Do we want this tracer to start on bootup? */
1170 tracing_set_tracer(type->name);
1171 default_bootup_tracer = NULL;
1172 /* disable other selftests, since this will break it. */
1173 tracing_selftest_disabled = true;
1174 #ifdef CONFIG_FTRACE_STARTUP_TEST
1175 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1183 void tracing_reset(struct trace_buffer *buf, int cpu)
1185 struct ring_buffer *buffer = buf->buffer;
1190 ring_buffer_record_disable(buffer);
1192 /* Make sure all commits have finished */
1193 synchronize_sched();
1194 ring_buffer_reset_cpu(buffer, cpu);
1196 ring_buffer_record_enable(buffer);
1199 void tracing_reset_online_cpus(struct trace_buffer *buf)
1201 struct ring_buffer *buffer = buf->buffer;
1207 ring_buffer_record_disable(buffer);
1209 /* Make sure all commits have finished */
1210 synchronize_sched();
1212 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
1214 for_each_online_cpu(cpu)
1215 ring_buffer_reset_cpu(buffer, cpu);
1217 ring_buffer_record_enable(buffer);
1220 /* Must have trace_types_lock held */
1221 void tracing_reset_all_online_cpus(void)
1223 struct trace_array *tr;
1225 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1226 tracing_reset_online_cpus(&tr->trace_buffer);
1227 #ifdef CONFIG_TRACER_MAX_TRACE
1228 tracing_reset_online_cpus(&tr->max_buffer);
1233 #define SAVED_CMDLINES 128
1234 #define NO_CMDLINE_MAP UINT_MAX
1235 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1236 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1237 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1238 static unsigned saved_tgids[SAVED_CMDLINES];
1239 static int cmdline_idx;
1240 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1242 /* temporary disable recording */
1243 static atomic_t trace_record_cmdline_disabled __read_mostly;
1245 static void trace_init_cmdlines(void)
1247 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1248 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
1252 int is_tracing_stopped(void)
1254 return global_trace.stop_count;
1258 * ftrace_off_permanent - disable all ftrace code permanently
1260 * This should only be called when a serious anomally has
1261 * been detected. This will turn off the function tracing,
1262 * ring buffers, and other tracing utilites. It takes no
1263 * locks and can be called from any context.
1265 void ftrace_off_permanent(void)
1267 tracing_disabled = 1;
1269 tracing_off_permanent();
1273 * tracing_start - quick start of the tracer
1275 * If tracing is enabled but was stopped by tracing_stop,
1276 * this will start the tracer back up.
1278 void tracing_start(void)
1280 struct ring_buffer *buffer;
1281 unsigned long flags;
1283 if (tracing_disabled)
1286 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1287 if (--global_trace.stop_count) {
1288 if (global_trace.stop_count < 0) {
1289 /* Someone screwed up their debugging */
1291 global_trace.stop_count = 0;
1296 /* Prevent the buffers from switching */
1297 arch_spin_lock(&ftrace_max_lock);
1299 buffer = global_trace.trace_buffer.buffer;
1301 ring_buffer_record_enable(buffer);
1303 #ifdef CONFIG_TRACER_MAX_TRACE
1304 buffer = global_trace.max_buffer.buffer;
1306 ring_buffer_record_enable(buffer);
1309 arch_spin_unlock(&ftrace_max_lock);
1313 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1316 static void tracing_start_tr(struct trace_array *tr)
1318 struct ring_buffer *buffer;
1319 unsigned long flags;
1321 if (tracing_disabled)
1324 /* If global, we need to also start the max tracer */
1325 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1326 return tracing_start();
1328 raw_spin_lock_irqsave(&tr->start_lock, flags);
1330 if (--tr->stop_count) {
1331 if (tr->stop_count < 0) {
1332 /* Someone screwed up their debugging */
1339 buffer = tr->trace_buffer.buffer;
1341 ring_buffer_record_enable(buffer);
1344 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1348 * tracing_stop - quick stop of the tracer
1350 * Light weight way to stop tracing. Use in conjunction with
1353 void tracing_stop(void)
1355 struct ring_buffer *buffer;
1356 unsigned long flags;
1359 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1360 if (global_trace.stop_count++)
1363 /* Prevent the buffers from switching */
1364 arch_spin_lock(&ftrace_max_lock);
1366 buffer = global_trace.trace_buffer.buffer;
1368 ring_buffer_record_disable(buffer);
1370 #ifdef CONFIG_TRACER_MAX_TRACE
1371 buffer = global_trace.max_buffer.buffer;
1373 ring_buffer_record_disable(buffer);
1376 arch_spin_unlock(&ftrace_max_lock);
1379 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1382 static void tracing_stop_tr(struct trace_array *tr)
1384 struct ring_buffer *buffer;
1385 unsigned long flags;
1387 /* If global, we need to also stop the max tracer */
1388 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1389 return tracing_stop();
1391 raw_spin_lock_irqsave(&tr->start_lock, flags);
1392 if (tr->stop_count++)
1395 buffer = tr->trace_buffer.buffer;
1397 ring_buffer_record_disable(buffer);
1400 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1403 void trace_stop_cmdline_recording(void);
1405 static void trace_save_cmdline(struct task_struct *tsk)
1409 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1413 * It's not the end of the world if we don't get
1414 * the lock, but we also don't want to spin
1415 * nor do we want to disable interrupts,
1416 * so if we miss here, then better luck next time.
1418 if (!arch_spin_trylock(&trace_cmdline_lock))
1421 idx = map_pid_to_cmdline[tsk->pid];
1422 if (idx == NO_CMDLINE_MAP) {
1423 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1426 * Check whether the cmdline buffer at idx has a pid
1427 * mapped. We are going to overwrite that entry so we
1428 * need to clear the map_pid_to_cmdline. Otherwise we
1429 * would read the new comm for the old pid.
1431 pid = map_cmdline_to_pid[idx];
1432 if (pid != NO_CMDLINE_MAP)
1433 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1435 map_cmdline_to_pid[idx] = tsk->pid;
1436 map_pid_to_cmdline[tsk->pid] = idx;
1441 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1442 saved_tgids[idx] = tsk->tgid;
1444 arch_spin_unlock(&trace_cmdline_lock);
1447 void trace_find_cmdline(int pid, char comm[])
1452 strcpy(comm, "<idle>");
1456 if (WARN_ON_ONCE(pid < 0)) {
1457 strcpy(comm, "<XXX>");
1461 if (pid > PID_MAX_DEFAULT) {
1462 strcpy(comm, "<...>");
1467 arch_spin_lock(&trace_cmdline_lock);
1468 map = map_pid_to_cmdline[pid];
1469 if (map != NO_CMDLINE_MAP)
1470 strcpy(comm, saved_cmdlines[map]);
1472 strcpy(comm, "<...>");
1474 arch_spin_unlock(&trace_cmdline_lock);
1478 int trace_find_tgid(int pid)
1484 arch_spin_lock(&trace_cmdline_lock);
1485 map = map_pid_to_cmdline[pid];
1486 if (map != NO_CMDLINE_MAP)
1487 tgid = saved_tgids[map];
1491 arch_spin_unlock(&trace_cmdline_lock);
1497 void tracing_record_cmdline(struct task_struct *tsk)
1499 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1502 if (!__this_cpu_read(trace_cmdline_save))
1505 __this_cpu_write(trace_cmdline_save, false);
1507 trace_save_cmdline(tsk);
1511 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1514 struct task_struct *tsk = current;
1516 entry->preempt_count = pc & 0xff;
1517 entry->pid = (tsk) ? tsk->pid : 0;
1519 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1520 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1522 TRACE_FLAG_IRQS_NOSUPPORT |
1524 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1525 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1526 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1528 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1530 struct ring_buffer_event *
1531 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1534 unsigned long flags, int pc)
1536 struct ring_buffer_event *event;
1538 event = ring_buffer_lock_reserve(buffer, len);
1539 if (event != NULL) {
1540 struct trace_entry *ent = ring_buffer_event_data(event);
1542 tracing_generic_entry_update(ent, flags, pc);
1550 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1552 __this_cpu_write(trace_cmdline_save, true);
1553 ring_buffer_unlock_commit(buffer, event);
1557 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1558 struct ring_buffer_event *event,
1559 unsigned long flags, int pc)
1561 __buffer_unlock_commit(buffer, event);
1563 ftrace_trace_stack(buffer, flags, 6, pc);
1564 ftrace_trace_userstack(buffer, flags, pc);
1567 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1568 struct ring_buffer_event *event,
1569 unsigned long flags, int pc)
1571 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1573 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1575 struct ring_buffer_event *
1576 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1577 struct ftrace_event_file *ftrace_file,
1578 int type, unsigned long len,
1579 unsigned long flags, int pc)
1581 *current_rb = ftrace_file->tr->trace_buffer.buffer;
1582 return trace_buffer_lock_reserve(*current_rb,
1583 type, len, flags, pc);
1585 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1587 struct ring_buffer_event *
1588 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1589 int type, unsigned long len,
1590 unsigned long flags, int pc)
1592 *current_rb = global_trace.trace_buffer.buffer;
1593 return trace_buffer_lock_reserve(*current_rb,
1594 type, len, flags, pc);
1596 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1598 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1599 struct ring_buffer_event *event,
1600 unsigned long flags, int pc)
1602 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1604 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1606 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1607 struct ring_buffer_event *event,
1608 unsigned long flags, int pc,
1609 struct pt_regs *regs)
1611 __buffer_unlock_commit(buffer, event);
1613 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1614 ftrace_trace_userstack(buffer, flags, pc);
1616 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1618 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1619 struct ring_buffer_event *event)
1621 ring_buffer_discard_commit(buffer, event);
1623 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1626 trace_function(struct trace_array *tr,
1627 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1630 struct ftrace_event_call *call = &event_function;
1631 struct ring_buffer *buffer = tr->trace_buffer.buffer;
1632 struct ring_buffer_event *event;
1633 struct ftrace_entry *entry;
1635 /* If we are reading the ring buffer, don't trace */
1636 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1639 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1643 entry = ring_buffer_event_data(event);
1645 entry->parent_ip = parent_ip;
1647 if (!filter_check_discard(call, entry, buffer, event))
1648 __buffer_unlock_commit(buffer, event);
1652 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1653 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1656 if (likely(!atomic_read(&data->disabled)))
1657 trace_function(tr, ip, parent_ip, flags, pc);
1660 #ifdef CONFIG_STACKTRACE
1662 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1663 struct ftrace_stack {
1664 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1667 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1668 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1670 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1671 unsigned long flags,
1672 int skip, int pc, struct pt_regs *regs)
1674 struct ftrace_event_call *call = &event_kernel_stack;
1675 struct ring_buffer_event *event;
1676 struct stack_entry *entry;
1677 struct stack_trace trace;
1679 int size = FTRACE_STACK_ENTRIES;
1681 trace.nr_entries = 0;
1685 * Since events can happen in NMIs there's no safe way to
1686 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1687 * or NMI comes in, it will just have to use the default
1688 * FTRACE_STACK_SIZE.
1690 preempt_disable_notrace();
1692 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1694 * We don't need any atomic variables, just a barrier.
1695 * If an interrupt comes in, we don't care, because it would
1696 * have exited and put the counter back to what we want.
1697 * We just need a barrier to keep gcc from moving things
1701 if (use_stack == 1) {
1702 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1703 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1706 save_stack_trace_regs(regs, &trace);
1708 save_stack_trace(&trace);
1710 if (trace.nr_entries > size)
1711 size = trace.nr_entries;
1713 /* From now on, use_stack is a boolean */
1716 size *= sizeof(unsigned long);
1718 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1719 sizeof(*entry) + size, flags, pc);
1722 entry = ring_buffer_event_data(event);
1724 memset(&entry->caller, 0, size);
1727 memcpy(&entry->caller, trace.entries,
1728 trace.nr_entries * sizeof(unsigned long));
1730 trace.max_entries = FTRACE_STACK_ENTRIES;
1731 trace.entries = entry->caller;
1733 save_stack_trace_regs(regs, &trace);
1735 save_stack_trace(&trace);
1738 entry->size = trace.nr_entries;
1740 if (!filter_check_discard(call, entry, buffer, event))
1741 __buffer_unlock_commit(buffer, event);
1744 /* Again, don't let gcc optimize things here */
1746 __this_cpu_dec(ftrace_stack_reserve);
1747 preempt_enable_notrace();
1751 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1752 int skip, int pc, struct pt_regs *regs)
1754 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1757 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1760 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1763 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1766 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1769 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1772 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
1776 * trace_dump_stack - record a stack back trace in the trace buffer
1777 * @skip: Number of functions to skip (helper handlers)
1779 void trace_dump_stack(int skip)
1781 unsigned long flags;
1783 if (tracing_disabled || tracing_selftest_running)
1786 local_save_flags(flags);
1789 * Skip 3 more, seems to get us at the caller of
1793 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1794 flags, skip, preempt_count(), NULL);
1797 static DEFINE_PER_CPU(int, user_stack_count);
1800 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1802 struct ftrace_event_call *call = &event_user_stack;
1803 struct ring_buffer_event *event;
1804 struct userstack_entry *entry;
1805 struct stack_trace trace;
1807 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1811 * NMIs can not handle page faults, even with fix ups.
1812 * The save user stack can (and often does) fault.
1814 if (unlikely(in_nmi()))
1818 * prevent recursion, since the user stack tracing may
1819 * trigger other kernel events.
1822 if (__this_cpu_read(user_stack_count))
1825 __this_cpu_inc(user_stack_count);
1827 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1828 sizeof(*entry), flags, pc);
1830 goto out_drop_count;
1831 entry = ring_buffer_event_data(event);
1833 entry->tgid = current->tgid;
1834 memset(&entry->caller, 0, sizeof(entry->caller));
1836 trace.nr_entries = 0;
1837 trace.max_entries = FTRACE_STACK_ENTRIES;
1839 trace.entries = entry->caller;
1841 save_stack_trace_user(&trace);
1842 if (!filter_check_discard(call, entry, buffer, event))
1843 __buffer_unlock_commit(buffer, event);
1846 __this_cpu_dec(user_stack_count);
1852 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1854 ftrace_trace_userstack(tr, flags, preempt_count());
1858 #endif /* CONFIG_STACKTRACE */
1860 /* created for use with alloc_percpu */
1861 struct trace_buffer_struct {
1862 char buffer[TRACE_BUF_SIZE];
1865 static struct trace_buffer_struct *trace_percpu_buffer;
1866 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1867 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1868 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1871 * The buffer used is dependent on the context. There is a per cpu
1872 * buffer for normal context, softirq contex, hard irq context and
1873 * for NMI context. Thise allows for lockless recording.
1875 * Note, if the buffers failed to be allocated, then this returns NULL
1877 static char *get_trace_buf(void)
1879 struct trace_buffer_struct *percpu_buffer;
1882 * If we have allocated per cpu buffers, then we do not
1883 * need to do any locking.
1886 percpu_buffer = trace_percpu_nmi_buffer;
1888 percpu_buffer = trace_percpu_irq_buffer;
1889 else if (in_softirq())
1890 percpu_buffer = trace_percpu_sirq_buffer;
1892 percpu_buffer = trace_percpu_buffer;
1897 return this_cpu_ptr(&percpu_buffer->buffer[0]);
1900 static int alloc_percpu_trace_buffer(void)
1902 struct trace_buffer_struct *buffers;
1903 struct trace_buffer_struct *sirq_buffers;
1904 struct trace_buffer_struct *irq_buffers;
1905 struct trace_buffer_struct *nmi_buffers;
1907 buffers = alloc_percpu(struct trace_buffer_struct);
1911 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1915 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1919 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1923 trace_percpu_buffer = buffers;
1924 trace_percpu_sirq_buffer = sirq_buffers;
1925 trace_percpu_irq_buffer = irq_buffers;
1926 trace_percpu_nmi_buffer = nmi_buffers;
1931 free_percpu(irq_buffers);
1933 free_percpu(sirq_buffers);
1935 free_percpu(buffers);
1937 WARN(1, "Could not allocate percpu trace_printk buffer");
1941 static int buffers_allocated;
1943 void trace_printk_init_buffers(void)
1945 if (buffers_allocated)
1948 if (alloc_percpu_trace_buffer())
1951 pr_info("ftrace: Allocated trace_printk buffers\n");
1953 /* Expand the buffers to set size */
1954 tracing_update_buffers();
1956 buffers_allocated = 1;
1959 * trace_printk_init_buffers() can be called by modules.
1960 * If that happens, then we need to start cmdline recording
1961 * directly here. If the global_trace.buffer is already
1962 * allocated here, then this was called by module code.
1964 if (global_trace.trace_buffer.buffer)
1965 tracing_start_cmdline_record();
1968 void trace_printk_start_comm(void)
1970 /* Start tracing comms if trace printk is set */
1971 if (!buffers_allocated)
1973 tracing_start_cmdline_record();
1976 static void trace_printk_start_stop_comm(int enabled)
1978 if (!buffers_allocated)
1982 tracing_start_cmdline_record();
1984 tracing_stop_cmdline_record();
1988 * trace_vbprintk - write binary msg to tracing buffer
1991 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1993 struct ftrace_event_call *call = &event_bprint;
1994 struct ring_buffer_event *event;
1995 struct ring_buffer *buffer;
1996 struct trace_array *tr = &global_trace;
1997 struct bprint_entry *entry;
1998 unsigned long flags;
2000 int len = 0, size, pc;
2002 if (unlikely(tracing_selftest_running || tracing_disabled))
2005 /* Don't pollute graph traces with trace_vprintk internals */
2006 pause_graph_tracing();
2008 pc = preempt_count();
2009 preempt_disable_notrace();
2011 tbuffer = get_trace_buf();
2017 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
2019 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
2022 local_save_flags(flags);
2023 size = sizeof(*entry) + sizeof(u32) * len;
2024 buffer = tr->trace_buffer.buffer;
2025 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
2029 entry = ring_buffer_event_data(event);
2033 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
2034 if (!filter_check_discard(call, entry, buffer, event)) {
2035 __buffer_unlock_commit(buffer, event);
2036 ftrace_trace_stack(buffer, flags, 6, pc);
2040 preempt_enable_notrace();
2041 unpause_graph_tracing();
2045 EXPORT_SYMBOL_GPL(trace_vbprintk);
2048 __trace_array_vprintk(struct ring_buffer *buffer,
2049 unsigned long ip, const char *fmt, va_list args)
2051 struct ftrace_event_call *call = &event_print;
2052 struct ring_buffer_event *event;
2053 int len = 0, size, pc;
2054 struct print_entry *entry;
2055 unsigned long flags;
2058 if (tracing_disabled || tracing_selftest_running)
2061 /* Don't pollute graph traces with trace_vprintk internals */
2062 pause_graph_tracing();
2064 pc = preempt_count();
2065 preempt_disable_notrace();
2068 tbuffer = get_trace_buf();
2074 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
2075 if (len > TRACE_BUF_SIZE)
2078 local_save_flags(flags);
2079 size = sizeof(*entry) + len + 1;
2080 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
2084 entry = ring_buffer_event_data(event);
2087 memcpy(&entry->buf, tbuffer, len);
2088 entry->buf[len] = '\0';
2089 if (!filter_check_discard(call, entry, buffer, event)) {
2090 __buffer_unlock_commit(buffer, event);
2091 ftrace_trace_stack(buffer, flags, 6, pc);
2094 preempt_enable_notrace();
2095 unpause_graph_tracing();
2100 int trace_array_vprintk(struct trace_array *tr,
2101 unsigned long ip, const char *fmt, va_list args)
2103 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2106 int trace_array_printk(struct trace_array *tr,
2107 unsigned long ip, const char *fmt, ...)
2112 if (!(trace_flags & TRACE_ITER_PRINTK))
2116 ret = trace_array_vprintk(tr, ip, fmt, ap);
2121 int trace_array_printk_buf(struct ring_buffer *buffer,
2122 unsigned long ip, const char *fmt, ...)
2127 if (!(trace_flags & TRACE_ITER_PRINTK))
2131 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2136 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2138 return trace_array_vprintk(&global_trace, ip, fmt, args);
2140 EXPORT_SYMBOL_GPL(trace_vprintk);
2142 static void trace_iterator_increment(struct trace_iterator *iter)
2144 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2148 ring_buffer_read(buf_iter, NULL);
2151 static struct trace_entry *
2152 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2153 unsigned long *lost_events)
2155 struct ring_buffer_event *event;
2156 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
2159 event = ring_buffer_iter_peek(buf_iter, ts);
2161 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
2165 iter->ent_size = ring_buffer_event_length(event);
2166 return ring_buffer_event_data(event);
2172 static struct trace_entry *
2173 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2174 unsigned long *missing_events, u64 *ent_ts)
2176 struct ring_buffer *buffer = iter->trace_buffer->buffer;
2177 struct trace_entry *ent, *next = NULL;
2178 unsigned long lost_events = 0, next_lost = 0;
2179 int cpu_file = iter->cpu_file;
2180 u64 next_ts = 0, ts;
2186 * If we are in a per_cpu trace file, don't bother by iterating over
2187 * all cpu and peek directly.
2189 if (cpu_file > RING_BUFFER_ALL_CPUS) {
2190 if (ring_buffer_empty_cpu(buffer, cpu_file))
2192 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
2194 *ent_cpu = cpu_file;
2199 for_each_tracing_cpu(cpu) {
2201 if (ring_buffer_empty_cpu(buffer, cpu))
2204 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
2207 * Pick the entry with the smallest timestamp:
2209 if (ent && (!next || ts < next_ts)) {
2213 next_lost = lost_events;
2214 next_size = iter->ent_size;
2218 iter->ent_size = next_size;
2221 *ent_cpu = next_cpu;
2227 *missing_events = next_lost;
2232 /* Find the next real entry, without updating the iterator itself */
2233 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2234 int *ent_cpu, u64 *ent_ts)
2236 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
2239 /* Find the next real entry, and increment the iterator to the next entry */
2240 void *trace_find_next_entry_inc(struct trace_iterator *iter)
2242 iter->ent = __find_next_entry(iter, &iter->cpu,
2243 &iter->lost_events, &iter->ts);
2246 trace_iterator_increment(iter);
2248 return iter->ent ? iter : NULL;
2251 static void trace_consume(struct trace_iterator *iter)
2253 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
2254 &iter->lost_events);
2257 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
2259 struct trace_iterator *iter = m->private;
2263 WARN_ON_ONCE(iter->leftover);
2267 /* can't go backwards */
2272 ent = trace_find_next_entry_inc(iter);
2276 while (ent && iter->idx < i)
2277 ent = trace_find_next_entry_inc(iter);
2284 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2286 struct ring_buffer_event *event;
2287 struct ring_buffer_iter *buf_iter;
2288 unsigned long entries = 0;
2291 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2293 buf_iter = trace_buffer_iter(iter, cpu);
2297 ring_buffer_iter_reset(buf_iter);
2300 * We could have the case with the max latency tracers
2301 * that a reset never took place on a cpu. This is evident
2302 * by the timestamp being before the start of the buffer.
2304 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
2305 if (ts >= iter->trace_buffer->time_start)
2308 ring_buffer_read(buf_iter, NULL);
2311 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2315 * The current tracer is copied to avoid a global locking
2318 static void *s_start(struct seq_file *m, loff_t *pos)
2320 struct trace_iterator *iter = m->private;
2321 struct trace_array *tr = iter->tr;
2322 int cpu_file = iter->cpu_file;
2328 * copy the tracer to avoid using a global lock all around.
2329 * iter->trace is a copy of current_trace, the pointer to the
2330 * name may be used instead of a strcmp(), as iter->trace->name
2331 * will point to the same string as current_trace->name.
2333 mutex_lock(&trace_types_lock);
2334 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2335 *iter->trace = *tr->current_trace;
2336 mutex_unlock(&trace_types_lock);
2338 #ifdef CONFIG_TRACER_MAX_TRACE
2339 if (iter->snapshot && iter->trace->use_max_tr)
2340 return ERR_PTR(-EBUSY);
2343 if (!iter->snapshot)
2344 atomic_inc(&trace_record_cmdline_disabled);
2346 if (*pos != iter->pos) {
2351 if (cpu_file == RING_BUFFER_ALL_CPUS) {
2352 for_each_tracing_cpu(cpu)
2353 tracing_iter_reset(iter, cpu);
2355 tracing_iter_reset(iter, cpu_file);
2358 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2363 * If we overflowed the seq_file before, then we want
2364 * to just reuse the trace_seq buffer again.
2370 p = s_next(m, p, &l);
2374 trace_event_read_lock();
2375 trace_access_lock(cpu_file);
2379 static void s_stop(struct seq_file *m, void *p)
2381 struct trace_iterator *iter = m->private;
2383 #ifdef CONFIG_TRACER_MAX_TRACE
2384 if (iter->snapshot && iter->trace->use_max_tr)
2388 if (!iter->snapshot)
2389 atomic_dec(&trace_record_cmdline_disabled);
2391 trace_access_unlock(iter->cpu_file);
2392 trace_event_read_unlock();
2396 get_total_entries(struct trace_buffer *buf,
2397 unsigned long *total, unsigned long *entries)
2399 unsigned long count;
2405 for_each_tracing_cpu(cpu) {
2406 count = ring_buffer_entries_cpu(buf->buffer, cpu);
2408 * If this buffer has skipped entries, then we hold all
2409 * entries for the trace and we need to ignore the
2410 * ones before the time stamp.
2412 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2413 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
2414 /* total is the same as the entries */
2418 ring_buffer_overrun_cpu(buf->buffer, cpu);
2423 static void print_lat_help_header(struct seq_file *m)
2425 seq_puts(m, "# _------=> CPU# \n");
2426 seq_puts(m, "# / _-----=> irqs-off \n");
2427 seq_puts(m, "# | / _----=> need-resched \n");
2428 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2429 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2430 seq_puts(m, "# |||| / delay \n");
2431 seq_puts(m, "# cmd pid ||||| time | caller \n");
2432 seq_puts(m, "# \\ / ||||| \\ | / \n");
2435 static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
2437 unsigned long total;
2438 unsigned long entries;
2440 get_total_entries(buf, &total, &entries);
2441 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2442 entries, total, num_online_cpus());
2446 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
2448 print_event_info(buf, m);
2449 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2450 seq_puts(m, "# | | | | |\n");
2453 static void print_func_help_header_tgid(struct trace_buffer *buf, struct seq_file *m)
2455 print_event_info(buf, m);
2456 seq_puts(m, "# TASK-PID TGID CPU# TIMESTAMP FUNCTION\n");
2457 seq_puts(m, "# | | | | | |\n");
2460 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
2462 print_event_info(buf, m);
2463 seq_puts(m, "# _-----=> irqs-off\n");
2464 seq_puts(m, "# / _----=> need-resched\n");
2465 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2466 seq_puts(m, "# || / _--=> preempt-depth\n");
2467 seq_puts(m, "# ||| / delay\n");
2468 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2469 seq_puts(m, "# | | | |||| | |\n");
2472 static void print_func_help_header_irq_tgid(struct trace_buffer *buf, struct seq_file *m)
2474 print_event_info(buf, m);
2475 seq_puts(m, "# _-----=> irqs-off\n");
2476 seq_puts(m, "# / _----=> need-resched\n");
2477 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2478 seq_puts(m, "# || / _--=> preempt-depth\n");
2479 seq_puts(m, "# ||| / delay\n");
2480 seq_puts(m, "# TASK-PID TGID CPU# |||| TIMESTAMP FUNCTION\n");
2481 seq_puts(m, "# | | | | |||| | |\n");
2485 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2487 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2488 struct trace_buffer *buf = iter->trace_buffer;
2489 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2490 struct tracer *type = iter->trace;
2491 unsigned long entries;
2492 unsigned long total;
2493 const char *name = "preemption";
2497 get_total_entries(buf, &total, &entries);
2499 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2501 seq_puts(m, "# -----------------------------------"
2502 "---------------------------------\n");
2503 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2504 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2505 nsecs_to_usecs(data->saved_latency),
2509 #if defined(CONFIG_PREEMPT_NONE)
2511 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2513 #elif defined(CONFIG_PREEMPT)
2518 /* These are reserved for later use */
2521 seq_printf(m, " #P:%d)\n", num_online_cpus());
2525 seq_puts(m, "# -----------------\n");
2526 seq_printf(m, "# | task: %.16s-%d "
2527 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2528 data->comm, data->pid,
2529 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2530 data->policy, data->rt_priority);
2531 seq_puts(m, "# -----------------\n");
2533 if (data->critical_start) {
2534 seq_puts(m, "# => started at: ");
2535 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2536 trace_print_seq(m, &iter->seq);
2537 seq_puts(m, "\n# => ended at: ");
2538 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2539 trace_print_seq(m, &iter->seq);
2540 seq_puts(m, "\n#\n");
2546 static void test_cpu_buff_start(struct trace_iterator *iter)
2548 struct trace_seq *s = &iter->seq;
2550 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2553 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2556 if (cpumask_test_cpu(iter->cpu, iter->started))
2559 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2562 cpumask_set_cpu(iter->cpu, iter->started);
2564 /* Don't print started cpu buffer for the first entry of the trace */
2566 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2570 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2572 struct trace_seq *s = &iter->seq;
2573 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2574 struct trace_entry *entry;
2575 struct trace_event *event;
2579 test_cpu_buff_start(iter);
2581 event = ftrace_find_event(entry->type);
2583 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2584 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2585 if (!trace_print_lat_context(iter))
2588 if (!trace_print_context(iter))
2594 return event->funcs->trace(iter, sym_flags, event);
2596 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2599 return TRACE_TYPE_HANDLED;
2601 return TRACE_TYPE_PARTIAL_LINE;
2604 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2606 struct trace_seq *s = &iter->seq;
2607 struct trace_entry *entry;
2608 struct trace_event *event;
2612 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2613 if (!trace_seq_printf(s, "%d %d %llu ",
2614 entry->pid, iter->cpu, iter->ts))
2618 event = ftrace_find_event(entry->type);
2620 return event->funcs->raw(iter, 0, event);
2622 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2625 return TRACE_TYPE_HANDLED;
2627 return TRACE_TYPE_PARTIAL_LINE;
2630 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2632 struct trace_seq *s = &iter->seq;
2633 unsigned char newline = '\n';
2634 struct trace_entry *entry;
2635 struct trace_event *event;
2639 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2640 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2641 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2642 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2645 event = ftrace_find_event(entry->type);
2647 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2648 if (ret != TRACE_TYPE_HANDLED)
2652 SEQ_PUT_FIELD_RET(s, newline);
2654 return TRACE_TYPE_HANDLED;
2657 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2659 struct trace_seq *s = &iter->seq;
2660 struct trace_entry *entry;
2661 struct trace_event *event;
2665 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2666 SEQ_PUT_FIELD_RET(s, entry->pid);
2667 SEQ_PUT_FIELD_RET(s, iter->cpu);
2668 SEQ_PUT_FIELD_RET(s, iter->ts);
2671 event = ftrace_find_event(entry->type);
2672 return event ? event->funcs->binary(iter, 0, event) :
2676 int trace_empty(struct trace_iterator *iter)
2678 struct ring_buffer_iter *buf_iter;
2681 /* If we are looking at one CPU buffer, only check that one */
2682 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2683 cpu = iter->cpu_file;
2684 buf_iter = trace_buffer_iter(iter, cpu);
2686 if (!ring_buffer_iter_empty(buf_iter))
2689 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2695 for_each_tracing_cpu(cpu) {
2696 buf_iter = trace_buffer_iter(iter, cpu);
2698 if (!ring_buffer_iter_empty(buf_iter))
2701 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2709 /* Called with trace_event_read_lock() held. */
2710 enum print_line_t print_trace_line(struct trace_iterator *iter)
2712 enum print_line_t ret;
2714 if (iter->lost_events &&
2715 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2716 iter->cpu, iter->lost_events))
2717 return TRACE_TYPE_PARTIAL_LINE;
2719 if (iter->trace && iter->trace->print_line) {
2720 ret = iter->trace->print_line(iter);
2721 if (ret != TRACE_TYPE_UNHANDLED)
2725 if (iter->ent->type == TRACE_BPUTS &&
2726 trace_flags & TRACE_ITER_PRINTK &&
2727 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2728 return trace_print_bputs_msg_only(iter);
2730 if (iter->ent->type == TRACE_BPRINT &&
2731 trace_flags & TRACE_ITER_PRINTK &&
2732 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2733 return trace_print_bprintk_msg_only(iter);
2735 if (iter->ent->type == TRACE_PRINT &&
2736 trace_flags & TRACE_ITER_PRINTK &&
2737 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2738 return trace_print_printk_msg_only(iter);
2740 if (trace_flags & TRACE_ITER_BIN)
2741 return print_bin_fmt(iter);
2743 if (trace_flags & TRACE_ITER_HEX)
2744 return print_hex_fmt(iter);
2746 if (trace_flags & TRACE_ITER_RAW)
2747 return print_raw_fmt(iter);
2749 return print_trace_fmt(iter);
2752 void trace_latency_header(struct seq_file *m)
2754 struct trace_iterator *iter = m->private;
2756 /* print nothing if the buffers are empty */
2757 if (trace_empty(iter))
2760 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2761 print_trace_header(m, iter);
2763 if (!(trace_flags & TRACE_ITER_VERBOSE))
2764 print_lat_help_header(m);
2767 void trace_default_header(struct seq_file *m)
2769 struct trace_iterator *iter = m->private;
2771 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2774 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2775 /* print nothing if the buffers are empty */
2776 if (trace_empty(iter))
2778 print_trace_header(m, iter);
2779 if (!(trace_flags & TRACE_ITER_VERBOSE))
2780 print_lat_help_header(m);
2782 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2783 if (trace_flags & TRACE_ITER_IRQ_INFO)
2784 if (trace_flags & TRACE_ITER_TGID)
2785 print_func_help_header_irq_tgid(iter->trace_buffer, m);
2787 print_func_help_header_irq(iter->trace_buffer, m);
2789 if (trace_flags & TRACE_ITER_TGID)
2790 print_func_help_header_tgid(iter->trace_buffer, m);
2792 print_func_help_header(iter->trace_buffer, m);
2797 static void test_ftrace_alive(struct seq_file *m)
2799 if (!ftrace_is_dead())
2801 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2802 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2805 #ifdef CONFIG_TRACER_MAX_TRACE
2806 static void show_snapshot_main_help(struct seq_file *m)
2808 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2809 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2810 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2811 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2812 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2813 seq_printf(m, "# is not a '0' or '1')\n");
2816 static void show_snapshot_percpu_help(struct seq_file *m)
2818 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2819 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2820 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2821 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2823 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2824 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2826 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2827 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2828 seq_printf(m, "# is not a '0' or '1')\n");
2831 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2833 if (iter->tr->allocated_snapshot)
2834 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2836 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2838 seq_printf(m, "# Snapshot commands:\n");
2839 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2840 show_snapshot_main_help(m);
2842 show_snapshot_percpu_help(m);
2845 /* Should never be called */
2846 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2849 static int s_show(struct seq_file *m, void *v)
2851 struct trace_iterator *iter = v;
2854 if (iter->ent == NULL) {
2856 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2858 test_ftrace_alive(m);
2860 if (iter->snapshot && trace_empty(iter))
2861 print_snapshot_help(m, iter);
2862 else if (iter->trace && iter->trace->print_header)
2863 iter->trace->print_header(m);
2865 trace_default_header(m);
2867 } else if (iter->leftover) {
2869 * If we filled the seq_file buffer earlier, we
2870 * want to just show it now.
2872 ret = trace_print_seq(m, &iter->seq);
2874 /* ret should this time be zero, but you never know */
2875 iter->leftover = ret;
2878 print_trace_line(iter);
2879 ret = trace_print_seq(m, &iter->seq);
2881 * If we overflow the seq_file buffer, then it will
2882 * ask us for this data again at start up.
2884 * ret is 0 if seq_file write succeeded.
2887 iter->leftover = ret;
2894 * Should be used after trace_array_get(), trace_types_lock
2895 * ensures that i_cdev was already initialized.
2897 static inline int tracing_get_cpu(struct inode *inode)
2899 if (inode->i_cdev) /* See trace_create_cpu_file() */
2900 return (long)inode->i_cdev - 1;
2901 return RING_BUFFER_ALL_CPUS;
2904 static const struct seq_operations tracer_seq_ops = {
2911 static struct trace_iterator *
2912 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2914 struct trace_array *tr = inode->i_private;
2915 struct trace_iterator *iter;
2918 if (tracing_disabled)
2919 return ERR_PTR(-ENODEV);
2921 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2923 return ERR_PTR(-ENOMEM);
2925 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2927 if (!iter->buffer_iter)
2931 * We make a copy of the current tracer to avoid concurrent
2932 * changes on it while we are reading.
2934 mutex_lock(&trace_types_lock);
2935 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2939 *iter->trace = *tr->current_trace;
2941 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2946 #ifdef CONFIG_TRACER_MAX_TRACE
2947 /* Currently only the top directory has a snapshot */
2948 if (tr->current_trace->print_max || snapshot)
2949 iter->trace_buffer = &tr->max_buffer;
2952 iter->trace_buffer = &tr->trace_buffer;
2953 iter->snapshot = snapshot;
2955 iter->cpu_file = tracing_get_cpu(inode);
2956 mutex_init(&iter->mutex);
2958 /* Notify the tracer early; before we stop tracing. */
2959 if (iter->trace && iter->trace->open)
2960 iter->trace->open(iter);
2962 /* Annotate start of buffers if we had overruns */
2963 if (ring_buffer_overruns(iter->trace_buffer->buffer))
2964 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2966 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2967 if (trace_clocks[tr->clock_id].in_ns)
2968 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2970 /* stop the trace while dumping if we are not opening "snapshot" */
2971 if (!iter->snapshot)
2972 tracing_stop_tr(tr);
2974 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
2975 for_each_tracing_cpu(cpu) {
2976 iter->buffer_iter[cpu] =
2977 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2979 ring_buffer_read_prepare_sync();
2980 for_each_tracing_cpu(cpu) {
2981 ring_buffer_read_start(iter->buffer_iter[cpu]);
2982 tracing_iter_reset(iter, cpu);
2985 cpu = iter->cpu_file;
2986 iter->buffer_iter[cpu] =
2987 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2988 ring_buffer_read_prepare_sync();
2989 ring_buffer_read_start(iter->buffer_iter[cpu]);
2990 tracing_iter_reset(iter, cpu);
2993 mutex_unlock(&trace_types_lock);
2998 mutex_unlock(&trace_types_lock);
3000 kfree(iter->buffer_iter);
3002 seq_release_private(inode, file);
3003 return ERR_PTR(-ENOMEM);
3006 int tracing_open_generic(struct inode *inode, struct file *filp)
3008 if (tracing_disabled)
3011 filp->private_data = inode->i_private;
3016 * Open and update trace_array ref count.
3017 * Must have the current trace_array passed to it.
3019 int tracing_open_generic_tr(struct inode *inode, struct file *filp)
3021 struct trace_array *tr = inode->i_private;
3023 if (tracing_disabled)
3026 if (trace_array_get(tr) < 0)
3029 filp->private_data = inode->i_private;
3034 static int tracing_release(struct inode *inode, struct file *file)
3036 struct trace_array *tr = inode->i_private;
3037 struct seq_file *m = file->private_data;
3038 struct trace_iterator *iter;
3041 if (!(file->f_mode & FMODE_READ)) {
3042 trace_array_put(tr);
3046 /* Writes do not use seq_file */
3048 mutex_lock(&trace_types_lock);
3050 for_each_tracing_cpu(cpu) {
3051 if (iter->buffer_iter[cpu])
3052 ring_buffer_read_finish(iter->buffer_iter[cpu]);
3055 if (iter->trace && iter->trace->close)
3056 iter->trace->close(iter);
3058 if (!iter->snapshot)
3059 /* reenable tracing if it was previously enabled */
3060 tracing_start_tr(tr);
3062 __trace_array_put(tr);
3064 mutex_unlock(&trace_types_lock);
3066 mutex_destroy(&iter->mutex);
3067 free_cpumask_var(iter->started);
3069 kfree(iter->buffer_iter);
3070 seq_release_private(inode, file);
3075 static int tracing_release_generic_tr(struct inode *inode, struct file *file)
3077 struct trace_array *tr = inode->i_private;
3079 trace_array_put(tr);
3083 static int tracing_single_release_tr(struct inode *inode, struct file *file)
3085 struct trace_array *tr = inode->i_private;
3087 trace_array_put(tr);
3089 return single_release(inode, file);
3092 static int tracing_open(struct inode *inode, struct file *file)
3094 struct trace_array *tr = inode->i_private;
3095 struct trace_iterator *iter;
3098 if (trace_array_get(tr) < 0)
3101 /* If this file was open for write, then erase contents */
3102 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
3103 int cpu = tracing_get_cpu(inode);
3105 if (cpu == RING_BUFFER_ALL_CPUS)
3106 tracing_reset_online_cpus(&tr->trace_buffer);
3108 tracing_reset(&tr->trace_buffer, cpu);
3111 if (file->f_mode & FMODE_READ) {
3112 iter = __tracing_open(inode, file, false);
3114 ret = PTR_ERR(iter);
3115 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
3116 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3120 trace_array_put(tr);
3126 t_next(struct seq_file *m, void *v, loff_t *pos)
3128 struct tracer *t = v;
3138 static void *t_start(struct seq_file *m, loff_t *pos)
3143 mutex_lock(&trace_types_lock);
3144 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
3150 static void t_stop(struct seq_file *m, void *p)
3152 mutex_unlock(&trace_types_lock);
3155 static int t_show(struct seq_file *m, void *v)
3157 struct tracer *t = v;
3162 seq_printf(m, "%s", t->name);
3171 static const struct seq_operations show_traces_seq_ops = {
3178 static int show_traces_open(struct inode *inode, struct file *file)
3180 if (tracing_disabled)
3183 return seq_open(file, &show_traces_seq_ops);
3187 tracing_write_stub(struct file *filp, const char __user *ubuf,
3188 size_t count, loff_t *ppos)
3193 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
3195 if (file->f_mode & FMODE_READ)
3196 return seq_lseek(file, offset, origin);
3201 static const struct file_operations tracing_fops = {
3202 .open = tracing_open,
3204 .write = tracing_write_stub,
3205 .llseek = tracing_seek,
3206 .release = tracing_release,
3209 static const struct file_operations show_traces_fops = {
3210 .open = show_traces_open,
3212 .release = seq_release,
3213 .llseek = seq_lseek,
3217 * Only trace on a CPU if the bitmask is set:
3219 static cpumask_var_t tracing_cpumask;
3222 * The tracer itself will not take this lock, but still we want
3223 * to provide a consistent cpumask to user-space:
3225 static DEFINE_MUTEX(tracing_cpumask_update_lock);
3228 * Temporary storage for the character representation of the
3229 * CPU bitmask (and one more byte for the newline):
3231 static char mask_str[NR_CPUS + 1];
3234 tracing_cpumask_read(struct file *filp, char __user *ubuf,
3235 size_t count, loff_t *ppos)
3239 mutex_lock(&tracing_cpumask_update_lock);
3241 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
3242 if (count - len < 2) {
3246 len += sprintf(mask_str + len, "\n");
3247 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3250 mutex_unlock(&tracing_cpumask_update_lock);
3256 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3257 size_t count, loff_t *ppos)
3259 struct trace_array *tr = filp->private_data;
3260 cpumask_var_t tracing_cpumask_new;
3263 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3266 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
3270 mutex_lock(&tracing_cpumask_update_lock);
3272 local_irq_disable();
3273 arch_spin_lock(&ftrace_max_lock);
3274 for_each_tracing_cpu(cpu) {
3276 * Increase/decrease the disabled counter if we are
3277 * about to flip a bit in the cpumask:
3279 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
3280 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3281 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3282 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
3284 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
3285 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3286 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3287 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
3290 arch_spin_unlock(&ftrace_max_lock);
3293 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
3295 mutex_unlock(&tracing_cpumask_update_lock);
3296 free_cpumask_var(tracing_cpumask_new);
3301 free_cpumask_var(tracing_cpumask_new);
3306 static const struct file_operations tracing_cpumask_fops = {
3307 .open = tracing_open_generic,
3308 .read = tracing_cpumask_read,
3309 .write = tracing_cpumask_write,
3310 .llseek = generic_file_llseek,
3313 static int tracing_trace_options_show(struct seq_file *m, void *v)
3315 struct tracer_opt *trace_opts;
3316 struct trace_array *tr = m->private;
3320 mutex_lock(&trace_types_lock);
3321 tracer_flags = tr->current_trace->flags->val;
3322 trace_opts = tr->current_trace->flags->opts;
3324 for (i = 0; trace_options[i]; i++) {
3325 if (trace_flags & (1 << i))
3326 seq_printf(m, "%s\n", trace_options[i]);
3328 seq_printf(m, "no%s\n", trace_options[i]);
3331 for (i = 0; trace_opts[i].name; i++) {
3332 if (tracer_flags & trace_opts[i].bit)
3333 seq_printf(m, "%s\n", trace_opts[i].name);
3335 seq_printf(m, "no%s\n", trace_opts[i].name);
3337 mutex_unlock(&trace_types_lock);
3342 static int __set_tracer_option(struct tracer *trace,
3343 struct tracer_flags *tracer_flags,
3344 struct tracer_opt *opts, int neg)
3348 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
3353 tracer_flags->val &= ~opts->bit;
3355 tracer_flags->val |= opts->bit;
3359 /* Try to assign a tracer specific option */
3360 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3362 struct tracer_flags *tracer_flags = trace->flags;
3363 struct tracer_opt *opts = NULL;
3366 for (i = 0; tracer_flags->opts[i].name; i++) {
3367 opts = &tracer_flags->opts[i];
3369 if (strcmp(cmp, opts->name) == 0)
3370 return __set_tracer_option(trace, trace->flags,
3377 /* Some tracers require overwrite to stay enabled */
3378 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3380 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3386 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
3388 /* do nothing if flag is already set */
3389 if (!!(trace_flags & mask) == !!enabled)
3392 /* Give the tracer a chance to approve the change */
3393 if (tr->current_trace->flag_changed)
3394 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
3398 trace_flags |= mask;
3400 trace_flags &= ~mask;
3402 if (mask == TRACE_ITER_RECORD_CMD)
3403 trace_event_enable_cmd_record(enabled);
3405 if (mask == TRACE_ITER_OVERWRITE) {
3406 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
3407 #ifdef CONFIG_TRACER_MAX_TRACE
3408 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
3412 if (mask == TRACE_ITER_PRINTK)
3413 trace_printk_start_stop_comm(enabled);
3418 static int trace_set_options(struct trace_array *tr, char *option)
3425 cmp = strstrip(option);
3427 if (strncmp(cmp, "no", 2) == 0) {
3432 mutex_lock(&trace_types_lock);
3434 for (i = 0; trace_options[i]; i++) {
3435 if (strcmp(cmp, trace_options[i]) == 0) {
3436 ret = set_tracer_flag(tr, 1 << i, !neg);
3441 /* If no option could be set, test the specific tracer options */
3442 if (!trace_options[i])
3443 ret = set_tracer_option(tr->current_trace, cmp, neg);
3445 mutex_unlock(&trace_types_lock);
3451 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3452 size_t cnt, loff_t *ppos)
3454 struct seq_file *m = filp->private_data;
3455 struct trace_array *tr = m->private;
3459 if (cnt >= sizeof(buf))
3462 if (copy_from_user(&buf, ubuf, cnt))
3467 ret = trace_set_options(tr, buf);
3476 static int tracing_trace_options_open(struct inode *inode, struct file *file)
3478 struct trace_array *tr = inode->i_private;
3481 if (tracing_disabled)
3484 if (trace_array_get(tr) < 0)
3487 ret = single_open(file, tracing_trace_options_show, inode->i_private);
3489 trace_array_put(tr);
3494 static const struct file_operations tracing_iter_fops = {
3495 .open = tracing_trace_options_open,
3497 .llseek = seq_lseek,
3498 .release = tracing_single_release_tr,
3499 .write = tracing_trace_options_write,
3502 static const char readme_msg[] =
3503 "tracing mini-HOWTO:\n\n"
3504 "# echo 0 > tracing_on : quick way to disable tracing\n"
3505 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3506 " Important files:\n"
3507 " trace\t\t\t- The static contents of the buffer\n"
3508 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3509 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3510 " current_tracer\t- function and latency tracers\n"
3511 " available_tracers\t- list of configured tracers for current_tracer\n"
3512 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3513 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3514 " trace_clock\t\t-change the clock used to order events\n"
3515 " local: Per cpu clock but may not be synced across CPUs\n"
3516 " global: Synced across CPUs but slows tracing down.\n"
3517 " counter: Not a clock, but just an increment\n"
3518 " uptime: Jiffy counter from time of boot\n"
3519 " perf: Same clock that perf events use\n"
3520 #ifdef CONFIG_X86_64
3521 " x86-tsc: TSC cycle counter\n"
3523 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3524 " tracing_cpumask\t- Limit which CPUs to trace\n"
3525 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3526 "\t\t\t Remove sub-buffer with rmdir\n"
3527 " trace_options\t\t- Set format or modify how tracing happens\n"
3528 "\t\t\t Disable an option by adding a suffix 'no' to the option name\n"
3529 #ifdef CONFIG_DYNAMIC_FTRACE
3530 "\n available_filter_functions - list of functions that can be filtered on\n"
3531 " set_ftrace_filter\t- echo function name in here to only trace these functions\n"
3532 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3533 " modules: Can select a group via module\n"
3534 " Format: :mod:<module-name>\n"
3535 " example: echo :mod:ext3 > set_ftrace_filter\n"
3536 " triggers: a command to perform when function is hit\n"
3537 " Format: <function>:<trigger>[:count]\n"
3538 " trigger: traceon, traceoff\n"
3539 " enable_event:<system>:<event>\n"
3540 " disable_event:<system>:<event>\n"
3541 #ifdef CONFIG_STACKTRACE
3544 #ifdef CONFIG_TRACER_SNAPSHOT
3547 " example: echo do_fault:traceoff > set_ftrace_filter\n"
3548 " echo do_trap:traceoff:3 > set_ftrace_filter\n"
3549 " The first one will disable tracing every time do_fault is hit\n"
3550 " The second will disable tracing at most 3 times when do_trap is hit\n"
3551 " The first time do trap is hit and it disables tracing, the counter\n"
3552 " will decrement to 2. If tracing is already disabled, the counter\n"
3553 " will not decrement. It only decrements when the trigger did work\n"
3554 " To remove trigger without count:\n"
3555 " echo '!<function>:<trigger> > set_ftrace_filter\n"
3556 " To remove trigger with a count:\n"
3557 " echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
3558 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
3559 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3560 " modules: Can select a group via module command :mod:\n"
3561 " Does not accept triggers\n"
3562 #endif /* CONFIG_DYNAMIC_FTRACE */
3563 #ifdef CONFIG_FUNCTION_TRACER
3564 " set_ftrace_pid\t- Write pid(s) to only function trace those pids (function)\n"
3566 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3567 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3568 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3570 #ifdef CONFIG_TRACER_SNAPSHOT
3571 "\n snapshot\t\t- Like 'trace' but shows the content of the static snapshot buffer\n"
3572 "\t\t\t Read the contents for more information\n"
3574 #ifdef CONFIG_STACKTRACE
3575 " stack_trace\t\t- Shows the max stack trace when active\n"
3576 " stack_max_size\t- Shows current max stack size that was traced\n"
3577 "\t\t\t Write into this file to reset the max size (trigger a new trace)\n"
3578 #ifdef CONFIG_DYNAMIC_FTRACE
3579 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace traces\n"
3581 #endif /* CONFIG_STACKTRACE */
3585 tracing_readme_read(struct file *filp, char __user *ubuf,
3586 size_t cnt, loff_t *ppos)
3588 return simple_read_from_buffer(ubuf, cnt, ppos,
3589 readme_msg, strlen(readme_msg));
3592 static const struct file_operations tracing_readme_fops = {
3593 .open = tracing_open_generic,
3594 .read = tracing_readme_read,
3595 .llseek = generic_file_llseek,
3599 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3600 size_t cnt, loff_t *ppos)
3609 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3613 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3621 for (i = 0; i < SAVED_CMDLINES; i++) {
3624 pid = map_cmdline_to_pid[i];
3625 if (pid == -1 || pid == NO_CMDLINE_MAP)
3628 trace_find_cmdline(pid, buf_comm);
3629 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3634 len = simple_read_from_buffer(ubuf, cnt, ppos,
3643 static const struct file_operations tracing_saved_cmdlines_fops = {
3644 .open = tracing_open_generic,
3645 .read = tracing_saved_cmdlines_read,
3646 .llseek = generic_file_llseek,
3650 tracing_saved_tgids_read(struct file *file, char __user *ubuf,
3651 size_t cnt, loff_t *ppos)
3659 file_buf = kmalloc(SAVED_CMDLINES*(16+1+16), GFP_KERNEL);
3665 for (i = 0; i < SAVED_CMDLINES; i++) {
3669 pid = map_cmdline_to_pid[i];
3670 if (pid == -1 || pid == NO_CMDLINE_MAP)
3673 tgid = trace_find_tgid(pid);
3674 r = sprintf(buf, "%d %d\n", pid, tgid);
3679 len = simple_read_from_buffer(ubuf, cnt, ppos,
3687 static const struct file_operations tracing_saved_tgids_fops = {
3688 .open = tracing_open_generic,
3689 .read = tracing_saved_tgids_read,
3690 .llseek = generic_file_llseek,
3694 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3695 size_t cnt, loff_t *ppos)
3697 struct trace_array *tr = filp->private_data;
3698 char buf[MAX_TRACER_SIZE+2];
3701 mutex_lock(&trace_types_lock);
3702 r = sprintf(buf, "%s\n", tr->current_trace->name);
3703 mutex_unlock(&trace_types_lock);
3705 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3708 int tracer_init(struct tracer *t, struct trace_array *tr)
3710 tracing_reset_online_cpus(&tr->trace_buffer);
3714 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
3718 for_each_tracing_cpu(cpu)
3719 per_cpu_ptr(buf->data, cpu)->entries = val;
3722 #ifdef CONFIG_TRACER_MAX_TRACE
3723 /* resize @tr's buffer to the size of @size_tr's entries */
3724 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3725 struct trace_buffer *size_buf, int cpu_id)
3729 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3730 for_each_tracing_cpu(cpu) {
3731 ret = ring_buffer_resize(trace_buf->buffer,
3732 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
3735 per_cpu_ptr(trace_buf->data, cpu)->entries =
3736 per_cpu_ptr(size_buf->data, cpu)->entries;
3739 ret = ring_buffer_resize(trace_buf->buffer,
3740 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
3742 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3743 per_cpu_ptr(size_buf->data, cpu_id)->entries;
3748 #endif /* CONFIG_TRACER_MAX_TRACE */
3750 static int __tracing_resize_ring_buffer(struct trace_array *tr,
3751 unsigned long size, int cpu)
3756 * If kernel or user changes the size of the ring buffer
3757 * we use the size that was given, and we can forget about
3758 * expanding it later.
3760 ring_buffer_expanded = true;
3762 /* May be called before buffers are initialized */
3763 if (!tr->trace_buffer.buffer)
3766 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
3770 #ifdef CONFIG_TRACER_MAX_TRACE
3771 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3772 !tr->current_trace->use_max_tr)
3775 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
3777 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3778 &tr->trace_buffer, cpu);
3781 * AARGH! We are left with different
3782 * size max buffer!!!!
3783 * The max buffer is our "snapshot" buffer.
3784 * When a tracer needs a snapshot (one of the
3785 * latency tracers), it swaps the max buffer
3786 * with the saved snap shot. We succeeded to
3787 * update the size of the main buffer, but failed to
3788 * update the size of the max buffer. But when we tried
3789 * to reset the main buffer to the original size, we
3790 * failed there too. This is very unlikely to
3791 * happen, but if it does, warn and kill all
3795 tracing_disabled = 1;
3800 if (cpu == RING_BUFFER_ALL_CPUS)
3801 set_buffer_entries(&tr->max_buffer, size);
3803 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
3806 #endif /* CONFIG_TRACER_MAX_TRACE */
3808 if (cpu == RING_BUFFER_ALL_CPUS)
3809 set_buffer_entries(&tr->trace_buffer, size);
3811 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
3816 static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3817 unsigned long size, int cpu_id)
3821 mutex_lock(&trace_types_lock);
3823 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3824 /* make sure, this cpu is enabled in the mask */
3825 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3831 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
3836 mutex_unlock(&trace_types_lock);
3843 * tracing_update_buffers - used by tracing facility to expand ring buffers
3845 * To save on memory when the tracing is never used on a system with it
3846 * configured in. The ring buffers are set to a minimum size. But once
3847 * a user starts to use the tracing facility, then they need to grow
3848 * to their default size.
3850 * This function is to be called when a tracer is about to be used.
3852 int tracing_update_buffers(void)
3856 mutex_lock(&trace_types_lock);
3857 if (!ring_buffer_expanded)
3858 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
3859 RING_BUFFER_ALL_CPUS);
3860 mutex_unlock(&trace_types_lock);
3865 struct trace_option_dentry;
3867 static struct trace_option_dentry *
3868 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
3871 destroy_trace_option_files(struct trace_option_dentry *topts);
3873 static int tracing_set_tracer(const char *buf)
3875 static struct trace_option_dentry *topts;
3876 struct trace_array *tr = &global_trace;
3878 #ifdef CONFIG_TRACER_MAX_TRACE
3883 mutex_lock(&trace_types_lock);
3885 if (!ring_buffer_expanded) {
3886 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
3887 RING_BUFFER_ALL_CPUS);
3893 for (t = trace_types; t; t = t->next) {
3894 if (strcmp(t->name, buf) == 0)
3901 if (t == tr->current_trace)
3904 trace_branch_disable();
3906 tr->current_trace->enabled = false;
3908 if (tr->current_trace->reset)
3909 tr->current_trace->reset(tr);
3911 /* Current trace needs to be nop_trace before synchronize_sched */
3912 tr->current_trace = &nop_trace;
3914 #ifdef CONFIG_TRACER_MAX_TRACE
3915 had_max_tr = tr->allocated_snapshot;
3917 if (had_max_tr && !t->use_max_tr) {
3919 * We need to make sure that the update_max_tr sees that
3920 * current_trace changed to nop_trace to keep it from
3921 * swapping the buffers after we resize it.
3922 * The update_max_tr is called from interrupts disabled
3923 * so a synchronized_sched() is sufficient.
3925 synchronize_sched();
3929 destroy_trace_option_files(topts);
3931 topts = create_trace_option_files(tr, t);
3933 #ifdef CONFIG_TRACER_MAX_TRACE
3934 if (t->use_max_tr && !had_max_tr) {
3935 ret = alloc_snapshot(tr);
3942 ret = tracer_init(t, tr);
3947 tr->current_trace = t;
3948 tr->current_trace->enabled = true;
3949 trace_branch_enable(tr);
3951 mutex_unlock(&trace_types_lock);
3957 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3958 size_t cnt, loff_t *ppos)
3960 char buf[MAX_TRACER_SIZE+1];
3967 if (cnt > MAX_TRACER_SIZE)
3968 cnt = MAX_TRACER_SIZE;
3970 if (copy_from_user(&buf, ubuf, cnt))
3975 /* strip ending whitespace. */
3976 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3979 err = tracing_set_tracer(buf);
3989 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3990 size_t cnt, loff_t *ppos)
3992 unsigned long *ptr = filp->private_data;
3996 r = snprintf(buf, sizeof(buf), "%ld\n",
3997 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3998 if (r > sizeof(buf))
4000 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4004 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
4005 size_t cnt, loff_t *ppos)
4007 unsigned long *ptr = filp->private_data;
4011 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4020 static int tracing_open_pipe(struct inode *inode, struct file *filp)
4022 struct trace_array *tr = inode->i_private;
4023 struct trace_iterator *iter;
4026 if (tracing_disabled)
4029 if (trace_array_get(tr) < 0)
4032 mutex_lock(&trace_types_lock);
4034 /* create a buffer to store the information to pass to userspace */
4035 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4038 __trace_array_put(tr);
4043 * We make a copy of the current tracer to avoid concurrent
4044 * changes on it while we are reading.
4046 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
4051 *iter->trace = *tr->current_trace;
4053 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
4058 /* trace pipe does not show start of buffer */
4059 cpumask_setall(iter->started);
4061 if (trace_flags & TRACE_ITER_LATENCY_FMT)
4062 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4064 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
4065 if (trace_clocks[tr->clock_id].in_ns)
4066 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4069 iter->trace_buffer = &tr->trace_buffer;
4070 iter->cpu_file = tracing_get_cpu(inode);
4071 mutex_init(&iter->mutex);
4072 filp->private_data = iter;
4074 if (iter->trace->pipe_open)
4075 iter->trace->pipe_open(iter);
4077 nonseekable_open(inode, filp);
4079 mutex_unlock(&trace_types_lock);
4085 __trace_array_put(tr);
4086 mutex_unlock(&trace_types_lock);
4090 static int tracing_release_pipe(struct inode *inode, struct file *file)
4092 struct trace_iterator *iter = file->private_data;
4093 struct trace_array *tr = inode->i_private;
4095 mutex_lock(&trace_types_lock);
4097 if (iter->trace->pipe_close)
4098 iter->trace->pipe_close(iter);
4100 mutex_unlock(&trace_types_lock);
4102 free_cpumask_var(iter->started);
4103 mutex_destroy(&iter->mutex);
4107 trace_array_put(tr);
4113 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
4115 /* Iterators are static, they should be filled or empty */
4116 if (trace_buffer_iter(iter, iter->cpu_file))
4117 return POLLIN | POLLRDNORM;
4119 if (trace_flags & TRACE_ITER_BLOCK)
4121 * Always select as readable when in blocking mode
4123 return POLLIN | POLLRDNORM;
4125 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
4130 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
4132 struct trace_iterator *iter = filp->private_data;
4134 return trace_poll(iter, filp, poll_table);
4138 * This is a make-shift waitqueue.
4139 * A tracer might use this callback on some rare cases:
4141 * 1) the current tracer might hold the runqueue lock when it wakes up
4142 * a reader, hence a deadlock (sched, function, and function graph tracers)
4143 * 2) the function tracers, trace all functions, we don't want
4144 * the overhead of calling wake_up and friends
4145 * (and tracing them too)
4147 * Anyway, this is really very primitive wakeup.
4149 void poll_wait_pipe(struct trace_iterator *iter)
4151 set_current_state(TASK_INTERRUPTIBLE);
4152 /* sleep for 100 msecs, and try again. */
4153 schedule_timeout(HZ / 10);
4156 /* Must be called with trace_types_lock mutex held. */
4157 static int tracing_wait_pipe(struct file *filp)
4159 struct trace_iterator *iter = filp->private_data;
4161 while (trace_empty(iter)) {
4163 if ((filp->f_flags & O_NONBLOCK)) {
4167 mutex_unlock(&iter->mutex);
4169 iter->trace->wait_pipe(iter);
4171 mutex_lock(&iter->mutex);
4173 if (signal_pending(current))
4177 * We block until we read something and tracing is disabled.
4178 * We still block if tracing is disabled, but we have never
4179 * read anything. This allows a user to cat this file, and
4180 * then enable tracing. But after we have read something,
4181 * we give an EOF when tracing is again disabled.
4183 * iter->pos will be 0 if we haven't read anything.
4185 if (!tracing_is_on() && iter->pos)
4196 tracing_read_pipe(struct file *filp, char __user *ubuf,
4197 size_t cnt, loff_t *ppos)
4199 struct trace_iterator *iter = filp->private_data;
4200 struct trace_array *tr = iter->tr;
4203 /* return any leftover data */
4204 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4208 trace_seq_init(&iter->seq);
4210 /* copy the tracer to avoid using a global lock all around */
4211 mutex_lock(&trace_types_lock);
4212 if (unlikely(iter->trace->name != tr->current_trace->name))
4213 *iter->trace = *tr->current_trace;
4214 mutex_unlock(&trace_types_lock);
4217 * Avoid more than one consumer on a single file descriptor
4218 * This is just a matter of traces coherency, the ring buffer itself
4221 mutex_lock(&iter->mutex);
4222 if (iter->trace->read) {
4223 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
4229 sret = tracing_wait_pipe(filp);
4233 /* stop when tracing is finished */
4234 if (trace_empty(iter)) {
4239 if (cnt >= PAGE_SIZE)
4240 cnt = PAGE_SIZE - 1;
4242 /* reset all but tr, trace, and overruns */
4243 memset(&iter->seq, 0,
4244 sizeof(struct trace_iterator) -
4245 offsetof(struct trace_iterator, seq));
4246 cpumask_clear(iter->started);
4249 trace_event_read_lock();
4250 trace_access_lock(iter->cpu_file);
4251 while (trace_find_next_entry_inc(iter) != NULL) {
4252 enum print_line_t ret;
4253 int len = iter->seq.len;
4255 ret = print_trace_line(iter);
4256 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4257 /* don't print partial lines */
4258 iter->seq.len = len;
4261 if (ret != TRACE_TYPE_NO_CONSUME)
4262 trace_consume(iter);
4264 if (iter->seq.len >= cnt)
4268 * Setting the full flag means we reached the trace_seq buffer
4269 * size and we should leave by partial output condition above.
4270 * One of the trace_seq_* functions is not used properly.
4272 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
4275 trace_access_unlock(iter->cpu_file);
4276 trace_event_read_unlock();
4278 /* Now copy what we have to the user */
4279 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4280 if (iter->seq.readpos >= iter->seq.len)
4281 trace_seq_init(&iter->seq);
4284 * If there was nothing to send to user, in spite of consuming trace
4285 * entries, go back to wait for more entries.
4291 mutex_unlock(&iter->mutex);
4296 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
4297 struct pipe_buffer *buf)
4299 __free_page(buf->page);
4302 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
4305 __free_page(spd->pages[idx]);
4308 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
4310 .map = generic_pipe_buf_map,
4311 .unmap = generic_pipe_buf_unmap,
4312 .confirm = generic_pipe_buf_confirm,
4313 .release = tracing_pipe_buf_release,
4314 .steal = generic_pipe_buf_steal,
4315 .get = generic_pipe_buf_get,
4319 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
4324 /* Seq buffer is page-sized, exactly what we need. */
4326 count = iter->seq.len;
4327 ret = print_trace_line(iter);
4328 count = iter->seq.len - count;
4331 iter->seq.len -= count;
4334 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4335 iter->seq.len -= count;
4339 if (ret != TRACE_TYPE_NO_CONSUME)
4340 trace_consume(iter);
4342 if (!trace_find_next_entry_inc(iter)) {
4352 static ssize_t tracing_splice_read_pipe(struct file *filp,
4354 struct pipe_inode_info *pipe,
4358 struct page *pages_def[PIPE_DEF_BUFFERS];
4359 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4360 struct trace_iterator *iter = filp->private_data;
4361 struct splice_pipe_desc spd = {
4363 .partial = partial_def,
4364 .nr_pages = 0, /* This gets updated below. */
4365 .nr_pages_max = PIPE_DEF_BUFFERS,
4367 .ops = &tracing_pipe_buf_ops,
4368 .spd_release = tracing_spd_release_pipe,
4370 struct trace_array *tr = iter->tr;
4375 if (splice_grow_spd(pipe, &spd))
4378 /* copy the tracer to avoid using a global lock all around */
4379 mutex_lock(&trace_types_lock);
4380 if (unlikely(iter->trace->name != tr->current_trace->name))
4381 *iter->trace = *tr->current_trace;
4382 mutex_unlock(&trace_types_lock);
4384 mutex_lock(&iter->mutex);
4386 if (iter->trace->splice_read) {
4387 ret = iter->trace->splice_read(iter, filp,
4388 ppos, pipe, len, flags);
4393 ret = tracing_wait_pipe(filp);
4397 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
4402 trace_event_read_lock();
4403 trace_access_lock(iter->cpu_file);
4405 /* Fill as many pages as possible. */
4406 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
4407 spd.pages[i] = alloc_page(GFP_KERNEL);
4411 rem = tracing_fill_pipe_page(rem, iter);
4413 /* Copy the data into the page, so we can start over. */
4414 ret = trace_seq_to_buffer(&iter->seq,
4415 page_address(spd.pages[i]),
4418 __free_page(spd.pages[i]);
4421 spd.partial[i].offset = 0;
4422 spd.partial[i].len = iter->seq.len;
4424 trace_seq_init(&iter->seq);
4427 trace_access_unlock(iter->cpu_file);
4428 trace_event_read_unlock();
4429 mutex_unlock(&iter->mutex);
4433 ret = splice_to_pipe(pipe, &spd);
4435 splice_shrink_spd(&spd);
4439 mutex_unlock(&iter->mutex);
4444 tracing_entries_read(struct file *filp, char __user *ubuf,
4445 size_t cnt, loff_t *ppos)
4447 struct inode *inode = file_inode(filp);
4448 struct trace_array *tr = inode->i_private;
4449 int cpu = tracing_get_cpu(inode);
4454 mutex_lock(&trace_types_lock);
4456 if (cpu == RING_BUFFER_ALL_CPUS) {
4457 int cpu, buf_size_same;
4462 /* check if all cpu sizes are same */
4463 for_each_tracing_cpu(cpu) {
4464 /* fill in the size from first enabled cpu */
4466 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4467 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
4473 if (buf_size_same) {
4474 if (!ring_buffer_expanded)
4475 r = sprintf(buf, "%lu (expanded: %lu)\n",
4477 trace_buf_size >> 10);
4479 r = sprintf(buf, "%lu\n", size >> 10);
4481 r = sprintf(buf, "X\n");
4483 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
4485 mutex_unlock(&trace_types_lock);
4487 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4492 tracing_entries_write(struct file *filp, const char __user *ubuf,
4493 size_t cnt, loff_t *ppos)
4495 struct inode *inode = file_inode(filp);
4496 struct trace_array *tr = inode->i_private;
4500 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4504 /* must have at least 1 entry */
4508 /* value is in KB */
4510 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
4520 tracing_total_entries_read(struct file *filp, char __user *ubuf,
4521 size_t cnt, loff_t *ppos)
4523 struct trace_array *tr = filp->private_data;
4526 unsigned long size = 0, expanded_size = 0;
4528 mutex_lock(&trace_types_lock);
4529 for_each_tracing_cpu(cpu) {
4530 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
4531 if (!ring_buffer_expanded)
4532 expanded_size += trace_buf_size >> 10;
4534 if (ring_buffer_expanded)
4535 r = sprintf(buf, "%lu\n", size);
4537 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4538 mutex_unlock(&trace_types_lock);
4540 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4544 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4545 size_t cnt, loff_t *ppos)
4548 * There is no need to read what the user has written, this function
4549 * is just to make sure that there is no error when "echo" is used
4558 tracing_free_buffer_release(struct inode *inode, struct file *filp)
4560 struct trace_array *tr = inode->i_private;
4562 /* disable tracing ? */
4563 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4564 tracer_tracing_off(tr);
4565 /* resize the ring buffer to 0 */
4566 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4568 trace_array_put(tr);
4574 tracing_mark_write(struct file *filp, const char __user *ubuf,
4575 size_t cnt, loff_t *fpos)
4577 unsigned long addr = (unsigned long)ubuf;
4578 struct trace_array *tr = filp->private_data;
4579 struct ring_buffer_event *event;
4580 struct ring_buffer *buffer;
4581 struct print_entry *entry;
4582 unsigned long irq_flags;
4583 struct page *pages[2];
4593 if (tracing_disabled)
4596 if (!(trace_flags & TRACE_ITER_MARKERS))
4599 if (cnt > TRACE_BUF_SIZE)
4600 cnt = TRACE_BUF_SIZE;
4603 * Userspace is injecting traces into the kernel trace buffer.
4604 * We want to be as non intrusive as possible.
4605 * To do so, we do not want to allocate any special buffers
4606 * or take any locks, but instead write the userspace data
4607 * straight into the ring buffer.
4609 * First we need to pin the userspace buffer into memory,
4610 * which, most likely it is, because it just referenced it.
4611 * But there's no guarantee that it is. By using get_user_pages_fast()
4612 * and kmap_atomic/kunmap_atomic() we can get access to the
4613 * pages directly. We then write the data directly into the
4616 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4618 /* check if we cross pages */
4619 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4622 offset = addr & (PAGE_SIZE - 1);
4625 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4626 if (ret < nr_pages) {
4628 put_page(pages[ret]);
4633 for (i = 0; i < nr_pages; i++)
4634 map_page[i] = kmap_atomic(pages[i]);
4636 local_save_flags(irq_flags);
4637 size = sizeof(*entry) + cnt + 2; /* possible \n added */
4638 buffer = tr->trace_buffer.buffer;
4639 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4640 irq_flags, preempt_count());
4642 /* Ring buffer disabled, return as if not open for write */
4647 entry = ring_buffer_event_data(event);
4648 entry->ip = _THIS_IP_;
4650 if (nr_pages == 2) {
4651 len = PAGE_SIZE - offset;
4652 memcpy(&entry->buf, map_page[0] + offset, len);
4653 memcpy(&entry->buf[len], map_page[1], cnt - len);
4655 memcpy(&entry->buf, map_page[0] + offset, cnt);
4657 if (entry->buf[cnt - 1] != '\n') {
4658 entry->buf[cnt] = '\n';
4659 entry->buf[cnt + 1] = '\0';
4661 entry->buf[cnt] = '\0';
4663 __buffer_unlock_commit(buffer, event);
4670 for (i = 0; i < nr_pages; i++){
4671 kunmap_atomic(map_page[i]);
4678 static int tracing_clock_show(struct seq_file *m, void *v)
4680 struct trace_array *tr = m->private;
4683 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4685 "%s%s%s%s", i ? " " : "",
4686 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4687 i == tr->clock_id ? "]" : "");
4693 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4694 size_t cnt, loff_t *fpos)
4696 struct seq_file *m = filp->private_data;
4697 struct trace_array *tr = m->private;
4699 const char *clockstr;
4702 if (cnt >= sizeof(buf))
4705 if (copy_from_user(&buf, ubuf, cnt))
4710 clockstr = strstrip(buf);
4712 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4713 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4716 if (i == ARRAY_SIZE(trace_clocks))
4719 mutex_lock(&trace_types_lock);
4723 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
4726 * New clock may not be consistent with the previous clock.
4727 * Reset the buffer so that it doesn't have incomparable timestamps.
4729 tracing_reset_online_cpus(&tr->trace_buffer);
4731 #ifdef CONFIG_TRACER_MAX_TRACE
4732 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4733 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4734 tracing_reset_online_cpus(&tr->max_buffer);
4737 mutex_unlock(&trace_types_lock);
4744 static int tracing_clock_open(struct inode *inode, struct file *file)
4746 struct trace_array *tr = inode->i_private;
4749 if (tracing_disabled)
4752 if (trace_array_get(tr))
4755 ret = single_open(file, tracing_clock_show, inode->i_private);
4757 trace_array_put(tr);
4762 struct ftrace_buffer_info {
4763 struct trace_iterator iter;
4768 #ifdef CONFIG_TRACER_SNAPSHOT
4769 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4771 struct trace_array *tr = inode->i_private;
4772 struct trace_iterator *iter;
4776 if (trace_array_get(tr) < 0)
4779 if (file->f_mode & FMODE_READ) {
4780 iter = __tracing_open(inode, file, true);
4782 ret = PTR_ERR(iter);
4784 /* Writes still need the seq_file to hold the private data */
4786 m = kzalloc(sizeof(*m), GFP_KERNEL);
4789 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4797 iter->trace_buffer = &tr->max_buffer;
4798 iter->cpu_file = tracing_get_cpu(inode);
4800 file->private_data = m;
4804 trace_array_put(tr);
4810 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4813 struct seq_file *m = filp->private_data;
4814 struct trace_iterator *iter = m->private;
4815 struct trace_array *tr = iter->tr;
4819 ret = tracing_update_buffers();
4823 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4827 mutex_lock(&trace_types_lock);
4829 if (tr->current_trace->use_max_tr) {
4836 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4840 if (tr->allocated_snapshot)
4844 /* Only allow per-cpu swap if the ring buffer supports it */
4845 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4846 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4851 if (!tr->allocated_snapshot) {
4852 ret = alloc_snapshot(tr);
4856 local_irq_disable();
4857 /* Now, we're going to swap */
4858 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4859 update_max_tr(tr, current, smp_processor_id());
4861 update_max_tr_single(tr, current, iter->cpu_file);
4865 if (tr->allocated_snapshot) {
4866 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4867 tracing_reset_online_cpus(&tr->max_buffer);
4869 tracing_reset(&tr->max_buffer, iter->cpu_file);
4879 mutex_unlock(&trace_types_lock);
4883 static int tracing_snapshot_release(struct inode *inode, struct file *file)
4885 struct seq_file *m = file->private_data;
4888 ret = tracing_release(inode, file);
4890 if (file->f_mode & FMODE_READ)
4893 /* If write only, the seq_file is just a stub */
4901 static int tracing_buffers_open(struct inode *inode, struct file *filp);
4902 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
4903 size_t count, loff_t *ppos);
4904 static int tracing_buffers_release(struct inode *inode, struct file *file);
4905 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4906 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
4908 static int snapshot_raw_open(struct inode *inode, struct file *filp)
4910 struct ftrace_buffer_info *info;
4913 ret = tracing_buffers_open(inode, filp);
4917 info = filp->private_data;
4919 if (info->iter.trace->use_max_tr) {
4920 tracing_buffers_release(inode, filp);
4924 info->iter.snapshot = true;
4925 info->iter.trace_buffer = &info->iter.tr->max_buffer;
4930 #endif /* CONFIG_TRACER_SNAPSHOT */
4933 static const struct file_operations tracing_max_lat_fops = {
4934 .open = tracing_open_generic,
4935 .read = tracing_max_lat_read,
4936 .write = tracing_max_lat_write,
4937 .llseek = generic_file_llseek,
4940 static const struct file_operations set_tracer_fops = {
4941 .open = tracing_open_generic,
4942 .read = tracing_set_trace_read,
4943 .write = tracing_set_trace_write,
4944 .llseek = generic_file_llseek,
4947 static const struct file_operations tracing_pipe_fops = {
4948 .open = tracing_open_pipe,
4949 .poll = tracing_poll_pipe,
4950 .read = tracing_read_pipe,
4951 .splice_read = tracing_splice_read_pipe,
4952 .release = tracing_release_pipe,
4953 .llseek = no_llseek,
4956 static const struct file_operations tracing_entries_fops = {
4957 .open = tracing_open_generic_tr,
4958 .read = tracing_entries_read,
4959 .write = tracing_entries_write,
4960 .llseek = generic_file_llseek,
4961 .release = tracing_release_generic_tr,
4964 static const struct file_operations tracing_total_entries_fops = {
4965 .open = tracing_open_generic_tr,
4966 .read = tracing_total_entries_read,
4967 .llseek = generic_file_llseek,
4968 .release = tracing_release_generic_tr,
4971 static const struct file_operations tracing_free_buffer_fops = {
4972 .open = tracing_open_generic_tr,
4973 .write = tracing_free_buffer_write,
4974 .release = tracing_free_buffer_release,
4977 static const struct file_operations tracing_mark_fops = {
4978 .open = tracing_open_generic_tr,
4979 .write = tracing_mark_write,
4980 .llseek = generic_file_llseek,
4981 .release = tracing_release_generic_tr,
4984 static const struct file_operations trace_clock_fops = {
4985 .open = tracing_clock_open,
4987 .llseek = seq_lseek,
4988 .release = tracing_single_release_tr,
4989 .write = tracing_clock_write,
4992 #ifdef CONFIG_TRACER_SNAPSHOT
4993 static const struct file_operations snapshot_fops = {
4994 .open = tracing_snapshot_open,
4996 .write = tracing_snapshot_write,
4997 .llseek = tracing_seek,
4998 .release = tracing_snapshot_release,
5001 static const struct file_operations snapshot_raw_fops = {
5002 .open = snapshot_raw_open,
5003 .read = tracing_buffers_read,
5004 .release = tracing_buffers_release,
5005 .splice_read = tracing_buffers_splice_read,
5006 .llseek = no_llseek,
5009 #endif /* CONFIG_TRACER_SNAPSHOT */
5011 static int tracing_buffers_open(struct inode *inode, struct file *filp)
5013 struct trace_array *tr = inode->i_private;
5014 struct ftrace_buffer_info *info;
5017 if (tracing_disabled)
5020 if (trace_array_get(tr) < 0)
5023 info = kzalloc(sizeof(*info), GFP_KERNEL);
5025 trace_array_put(tr);
5029 mutex_lock(&trace_types_lock);
5032 info->iter.cpu_file = tracing_get_cpu(inode);
5033 info->iter.trace = tr->current_trace;
5034 info->iter.trace_buffer = &tr->trace_buffer;
5036 /* Force reading ring buffer for first read */
5037 info->read = (unsigned int)-1;
5039 filp->private_data = info;
5041 mutex_unlock(&trace_types_lock);
5043 ret = nonseekable_open(inode, filp);
5045 trace_array_put(tr);
5051 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
5053 struct ftrace_buffer_info *info = filp->private_data;
5054 struct trace_iterator *iter = &info->iter;
5056 return trace_poll(iter, filp, poll_table);
5060 tracing_buffers_read(struct file *filp, char __user *ubuf,
5061 size_t count, loff_t *ppos)
5063 struct ftrace_buffer_info *info = filp->private_data;
5064 struct trace_iterator *iter = &info->iter;
5071 mutex_lock(&trace_types_lock);
5073 #ifdef CONFIG_TRACER_MAX_TRACE
5074 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5081 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
5087 /* Do we have previous read data to read? */
5088 if (info->read < PAGE_SIZE)
5092 trace_access_lock(iter->cpu_file);
5093 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
5097 trace_access_unlock(iter->cpu_file);
5100 if (trace_empty(iter)) {
5101 if ((filp->f_flags & O_NONBLOCK)) {
5105 mutex_unlock(&trace_types_lock);
5106 iter->trace->wait_pipe(iter);
5107 mutex_lock(&trace_types_lock);
5108 if (signal_pending(current)) {
5120 size = PAGE_SIZE - info->read;
5124 ret = copy_to_user(ubuf, info->spare + info->read, size);
5135 mutex_unlock(&trace_types_lock);
5140 static int tracing_buffers_release(struct inode *inode, struct file *file)
5142 struct ftrace_buffer_info *info = file->private_data;
5143 struct trace_iterator *iter = &info->iter;
5145 mutex_lock(&trace_types_lock);
5147 __trace_array_put(iter->tr);
5150 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
5153 mutex_unlock(&trace_types_lock);
5159 struct ring_buffer *buffer;
5164 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
5165 struct pipe_buffer *buf)
5167 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5172 ring_buffer_free_read_page(ref->buffer, ref->page);
5177 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
5178 struct pipe_buffer *buf)
5180 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5185 /* Pipe buffer operations for a buffer. */
5186 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
5188 .map = generic_pipe_buf_map,
5189 .unmap = generic_pipe_buf_unmap,
5190 .confirm = generic_pipe_buf_confirm,
5191 .release = buffer_pipe_buf_release,
5192 .steal = generic_pipe_buf_steal,
5193 .get = buffer_pipe_buf_get,
5197 * Callback from splice_to_pipe(), if we need to release some pages
5198 * at the end of the spd in case we error'ed out in filling the pipe.
5200 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
5202 struct buffer_ref *ref =
5203 (struct buffer_ref *)spd->partial[i].private;
5208 ring_buffer_free_read_page(ref->buffer, ref->page);
5210 spd->partial[i].private = 0;
5214 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5215 struct pipe_inode_info *pipe, size_t len,
5218 struct ftrace_buffer_info *info = file->private_data;
5219 struct trace_iterator *iter = &info->iter;
5220 struct partial_page partial_def[PIPE_DEF_BUFFERS];
5221 struct page *pages_def[PIPE_DEF_BUFFERS];
5222 struct splice_pipe_desc spd = {
5224 .partial = partial_def,
5225 .nr_pages_max = PIPE_DEF_BUFFERS,
5227 .ops = &buffer_pipe_buf_ops,
5228 .spd_release = buffer_spd_release,
5230 struct buffer_ref *ref;
5231 int entries, size, i;
5234 mutex_lock(&trace_types_lock);
5236 #ifdef CONFIG_TRACER_MAX_TRACE
5237 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5243 if (splice_grow_spd(pipe, &spd)) {
5248 if (*ppos & (PAGE_SIZE - 1)) {
5253 if (len & (PAGE_SIZE - 1)) {
5254 if (len < PAGE_SIZE) {
5262 trace_access_lock(iter->cpu_file);
5263 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5265 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
5269 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
5274 ref->buffer = iter->trace_buffer->buffer;
5275 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
5281 r = ring_buffer_read_page(ref->buffer, &ref->page,
5282 len, iter->cpu_file, 1);
5284 ring_buffer_free_read_page(ref->buffer, ref->page);
5290 * zero out any left over data, this is going to
5293 size = ring_buffer_page_len(ref->page);
5294 if (size < PAGE_SIZE)
5295 memset(ref->page + size, 0, PAGE_SIZE - size);
5297 page = virt_to_page(ref->page);
5299 spd.pages[i] = page;
5300 spd.partial[i].len = PAGE_SIZE;
5301 spd.partial[i].offset = 0;
5302 spd.partial[i].private = (unsigned long)ref;
5306 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5309 trace_access_unlock(iter->cpu_file);
5312 /* did we read anything? */
5313 if (!spd.nr_pages) {
5314 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
5318 mutex_unlock(&trace_types_lock);
5319 iter->trace->wait_pipe(iter);
5320 mutex_lock(&trace_types_lock);
5321 if (signal_pending(current)) {
5328 ret = splice_to_pipe(pipe, &spd);
5329 splice_shrink_spd(&spd);
5331 mutex_unlock(&trace_types_lock);
5336 static const struct file_operations tracing_buffers_fops = {
5337 .open = tracing_buffers_open,
5338 .read = tracing_buffers_read,
5339 .poll = tracing_buffers_poll,
5340 .release = tracing_buffers_release,
5341 .splice_read = tracing_buffers_splice_read,
5342 .llseek = no_llseek,
5346 tracing_stats_read(struct file *filp, char __user *ubuf,
5347 size_t count, loff_t *ppos)
5349 struct inode *inode = file_inode(filp);
5350 struct trace_array *tr = inode->i_private;
5351 struct trace_buffer *trace_buf = &tr->trace_buffer;
5352 int cpu = tracing_get_cpu(inode);
5353 struct trace_seq *s;
5355 unsigned long long t;
5356 unsigned long usec_rem;
5358 s = kmalloc(sizeof(*s), GFP_KERNEL);
5364 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
5365 trace_seq_printf(s, "entries: %ld\n", cnt);
5367 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
5368 trace_seq_printf(s, "overrun: %ld\n", cnt);
5370 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
5371 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5373 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
5374 trace_seq_printf(s, "bytes: %ld\n", cnt);
5376 if (trace_clocks[tr->clock_id].in_ns) {
5377 /* local or global for trace_clock */
5378 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5379 usec_rem = do_div(t, USEC_PER_SEC);
5380 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5383 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
5384 usec_rem = do_div(t, USEC_PER_SEC);
5385 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5387 /* counter or tsc mode for trace_clock */
5388 trace_seq_printf(s, "oldest event ts: %llu\n",
5389 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5391 trace_seq_printf(s, "now ts: %llu\n",
5392 ring_buffer_time_stamp(trace_buf->buffer, cpu));
5395 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
5396 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5398 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
5399 trace_seq_printf(s, "read events: %ld\n", cnt);
5401 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
5408 static const struct file_operations tracing_stats_fops = {
5409 .open = tracing_open_generic_tr,
5410 .read = tracing_stats_read,
5411 .llseek = generic_file_llseek,
5412 .release = tracing_release_generic_tr,
5415 #ifdef CONFIG_DYNAMIC_FTRACE
5417 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
5423 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
5424 size_t cnt, loff_t *ppos)
5426 static char ftrace_dyn_info_buffer[1024];
5427 static DEFINE_MUTEX(dyn_info_mutex);
5428 unsigned long *p = filp->private_data;
5429 char *buf = ftrace_dyn_info_buffer;
5430 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
5433 mutex_lock(&dyn_info_mutex);
5434 r = sprintf(buf, "%ld ", *p);
5436 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
5439 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5441 mutex_unlock(&dyn_info_mutex);
5446 static const struct file_operations tracing_dyn_info_fops = {
5447 .open = tracing_open_generic,
5448 .read = tracing_read_dyn_info,
5449 .llseek = generic_file_llseek,
5451 #endif /* CONFIG_DYNAMIC_FTRACE */
5453 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5455 ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5461 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5463 unsigned long *count = (long *)data;
5475 ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5476 struct ftrace_probe_ops *ops, void *data)
5478 long count = (long)data;
5480 seq_printf(m, "%ps:", (void *)ip);
5482 seq_printf(m, "snapshot");
5485 seq_printf(m, ":unlimited\n");
5487 seq_printf(m, ":count=%ld\n", count);
5492 static struct ftrace_probe_ops snapshot_probe_ops = {
5493 .func = ftrace_snapshot,
5494 .print = ftrace_snapshot_print,
5497 static struct ftrace_probe_ops snapshot_count_probe_ops = {
5498 .func = ftrace_count_snapshot,
5499 .print = ftrace_snapshot_print,
5503 ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5504 char *glob, char *cmd, char *param, int enable)
5506 struct ftrace_probe_ops *ops;
5507 void *count = (void *)-1;
5511 /* hash funcs only work with set_ftrace_filter */
5515 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
5517 if (glob[0] == '!') {
5518 unregister_ftrace_function_probe_func(glob+1, ops);
5525 number = strsep(¶m, ":");
5527 if (!strlen(number))
5531 * We use the callback data field (which is a pointer)
5534 ret = kstrtoul(number, 0, (unsigned long *)&count);
5539 ret = register_ftrace_function_probe(glob, ops, count);
5542 alloc_snapshot(&global_trace);
5544 return ret < 0 ? ret : 0;
5547 static struct ftrace_func_command ftrace_snapshot_cmd = {
5549 .func = ftrace_trace_snapshot_callback,
5552 static int register_snapshot_cmd(void)
5554 return register_ftrace_command(&ftrace_snapshot_cmd);
5557 static inline int register_snapshot_cmd(void) { return 0; }
5558 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
5560 struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
5565 if (!debugfs_initialized())
5568 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5569 tr->dir = debugfs_create_dir("tracing", NULL);
5572 pr_warn_once("Could not create debugfs directory 'tracing'\n");
5577 struct dentry *tracing_init_dentry(void)
5579 return tracing_init_dentry_tr(&global_trace);
5582 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
5584 struct dentry *d_tracer;
5587 return tr->percpu_dir;
5589 d_tracer = tracing_init_dentry_tr(tr);
5593 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
5595 WARN_ONCE(!tr->percpu_dir,
5596 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
5598 return tr->percpu_dir;
5601 static struct dentry *
5602 trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
5603 void *data, long cpu, const struct file_operations *fops)
5605 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
5607 if (ret) /* See tracing_get_cpu() */
5608 ret->d_inode->i_cdev = (void *)(cpu + 1);
5613 tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
5615 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5616 struct dentry *d_cpu;
5617 char cpu_dir[30]; /* 30 characters should be more than enough */
5622 snprintf(cpu_dir, 30, "cpu%ld", cpu);
5623 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5625 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5629 /* per cpu trace_pipe */
5630 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
5631 tr, cpu, &tracing_pipe_fops);
5634 trace_create_cpu_file("trace", 0644, d_cpu,
5635 tr, cpu, &tracing_fops);
5637 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
5638 tr, cpu, &tracing_buffers_fops);
5640 trace_create_cpu_file("stats", 0444, d_cpu,
5641 tr, cpu, &tracing_stats_fops);
5643 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
5644 tr, cpu, &tracing_entries_fops);
5646 #ifdef CONFIG_TRACER_SNAPSHOT
5647 trace_create_cpu_file("snapshot", 0644, d_cpu,
5648 tr, cpu, &snapshot_fops);
5650 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
5651 tr, cpu, &snapshot_raw_fops);
5655 #ifdef CONFIG_FTRACE_SELFTEST
5656 /* Let selftest have access to static functions in this file */
5657 #include "trace_selftest.c"
5660 struct trace_option_dentry {
5661 struct tracer_opt *opt;
5662 struct tracer_flags *flags;
5663 struct trace_array *tr;
5664 struct dentry *entry;
5668 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5671 struct trace_option_dentry *topt = filp->private_data;
5674 if (topt->flags->val & topt->opt->bit)
5679 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5683 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5686 struct trace_option_dentry *topt = filp->private_data;
5690 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5694 if (val != 0 && val != 1)
5697 if (!!(topt->flags->val & topt->opt->bit) != val) {
5698 mutex_lock(&trace_types_lock);
5699 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
5701 mutex_unlock(&trace_types_lock);
5712 static const struct file_operations trace_options_fops = {
5713 .open = tracing_open_generic,
5714 .read = trace_options_read,
5715 .write = trace_options_write,
5716 .llseek = generic_file_llseek,
5720 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5723 long index = (long)filp->private_data;
5726 if (trace_flags & (1 << index))
5731 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5735 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5738 struct trace_array *tr = &global_trace;
5739 long index = (long)filp->private_data;
5743 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5747 if (val != 0 && val != 1)
5750 mutex_lock(&trace_types_lock);
5751 ret = set_tracer_flag(tr, 1 << index, val);
5752 mutex_unlock(&trace_types_lock);
5762 static const struct file_operations trace_options_core_fops = {
5763 .open = tracing_open_generic,
5764 .read = trace_options_core_read,
5765 .write = trace_options_core_write,
5766 .llseek = generic_file_llseek,
5769 struct dentry *trace_create_file(const char *name,
5771 struct dentry *parent,
5773 const struct file_operations *fops)
5777 ret = debugfs_create_file(name, mode, parent, data, fops);
5779 pr_warning("Could not create debugfs '%s' entry\n", name);
5785 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
5787 struct dentry *d_tracer;
5792 d_tracer = tracing_init_dentry_tr(tr);
5796 tr->options = debugfs_create_dir("options", d_tracer);
5798 pr_warning("Could not create debugfs directory 'options'\n");
5806 create_trace_option_file(struct trace_array *tr,
5807 struct trace_option_dentry *topt,
5808 struct tracer_flags *flags,
5809 struct tracer_opt *opt)
5811 struct dentry *t_options;
5813 t_options = trace_options_init_dentry(tr);
5817 topt->flags = flags;
5821 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
5822 &trace_options_fops);
5826 static struct trace_option_dentry *
5827 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
5829 struct trace_option_dentry *topts;
5830 struct tracer_flags *flags;
5831 struct tracer_opt *opts;
5837 flags = tracer->flags;
5839 if (!flags || !flags->opts)
5844 for (cnt = 0; opts[cnt].name; cnt++)
5847 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
5851 for (cnt = 0; opts[cnt].name; cnt++)
5852 create_trace_option_file(tr, &topts[cnt], flags,
5859 destroy_trace_option_files(struct trace_option_dentry *topts)
5866 for (cnt = 0; topts[cnt].opt; cnt++) {
5867 if (topts[cnt].entry)
5868 debugfs_remove(topts[cnt].entry);
5874 static struct dentry *
5875 create_trace_option_core_file(struct trace_array *tr,
5876 const char *option, long index)
5878 struct dentry *t_options;
5880 t_options = trace_options_init_dentry(tr);
5884 return trace_create_file(option, 0644, t_options, (void *)index,
5885 &trace_options_core_fops);
5888 static __init void create_trace_options_dir(struct trace_array *tr)
5890 struct dentry *t_options;
5893 t_options = trace_options_init_dentry(tr);
5897 for (i = 0; trace_options[i]; i++)
5898 create_trace_option_core_file(tr, trace_options[i], i);
5902 rb_simple_read(struct file *filp, char __user *ubuf,
5903 size_t cnt, loff_t *ppos)
5905 struct trace_array *tr = filp->private_data;
5909 r = tracer_tracing_is_on(tr);
5910 r = sprintf(buf, "%d\n", r);
5912 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5916 rb_simple_write(struct file *filp, const char __user *ubuf,
5917 size_t cnt, loff_t *ppos)
5919 struct trace_array *tr = filp->private_data;
5920 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5924 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5929 mutex_lock(&trace_types_lock);
5931 tracer_tracing_on(tr);
5932 if (tr->current_trace->start)
5933 tr->current_trace->start(tr);
5935 tracer_tracing_off(tr);
5936 if (tr->current_trace->stop)
5937 tr->current_trace->stop(tr);
5939 mutex_unlock(&trace_types_lock);
5947 static const struct file_operations rb_simple_fops = {
5948 .open = tracing_open_generic_tr,
5949 .read = rb_simple_read,
5950 .write = rb_simple_write,
5951 .release = tracing_release_generic_tr,
5952 .llseek = default_llseek,
5955 struct dentry *trace_instance_dir;
5958 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5960 static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
5964 for_each_tracing_cpu(cpu) {
5965 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
5966 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
5967 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
5972 allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
5974 enum ring_buffer_flags rb_flags;
5976 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5980 buf->buffer = ring_buffer_alloc(size, rb_flags);
5984 buf->data = alloc_percpu(struct trace_array_cpu);
5986 ring_buffer_free(buf->buffer);
5990 init_trace_buffers(tr, buf);
5992 /* Allocate the first page for all buffers */
5993 set_buffer_entries(&tr->trace_buffer,
5994 ring_buffer_size(tr->trace_buffer.buffer, 0));
5999 static int allocate_trace_buffers(struct trace_array *tr, int size)
6003 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
6007 #ifdef CONFIG_TRACER_MAX_TRACE
6008 ret = allocate_trace_buffer(tr, &tr->max_buffer,
6009 allocate_snapshot ? size : 1);
6011 ring_buffer_free(tr->trace_buffer.buffer);
6012 free_percpu(tr->trace_buffer.data);
6015 tr->allocated_snapshot = allocate_snapshot;
6018 * Only the top level trace array gets its snapshot allocated
6019 * from the kernel command line.
6021 allocate_snapshot = false;
6026 static int new_instance_create(const char *name)
6028 struct trace_array *tr;
6031 mutex_lock(&trace_types_lock);
6034 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6035 if (tr->name && strcmp(tr->name, name) == 0)
6040 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
6044 tr->name = kstrdup(name, GFP_KERNEL);
6048 raw_spin_lock_init(&tr->start_lock);
6050 tr->current_trace = &nop_trace;
6052 INIT_LIST_HEAD(&tr->systems);
6053 INIT_LIST_HEAD(&tr->events);
6055 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
6058 /* Holder for file callbacks */
6059 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
6060 tr->trace_cpu.tr = tr;
6062 tr->dir = debugfs_create_dir(name, trace_instance_dir);
6066 ret = event_trace_add_tracer(tr->dir, tr);
6068 debugfs_remove_recursive(tr->dir);
6072 init_tracer_debugfs(tr, tr->dir);
6074 list_add(&tr->list, &ftrace_trace_arrays);
6076 mutex_unlock(&trace_types_lock);
6081 if (tr->trace_buffer.buffer)
6082 ring_buffer_free(tr->trace_buffer.buffer);
6087 mutex_unlock(&trace_types_lock);
6093 static int instance_delete(const char *name)
6095 struct trace_array *tr;
6099 mutex_lock(&trace_types_lock);
6102 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6103 if (tr->name && strcmp(tr->name, name) == 0) {
6115 list_del(&tr->list);
6117 event_trace_del_tracer(tr);
6118 debugfs_remove_recursive(tr->dir);
6119 free_percpu(tr->trace_buffer.data);
6120 ring_buffer_free(tr->trace_buffer.buffer);
6128 mutex_unlock(&trace_types_lock);
6133 static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
6135 struct dentry *parent;
6138 /* Paranoid: Make sure the parent is the "instances" directory */
6139 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
6140 if (WARN_ON_ONCE(parent != trace_instance_dir))
6144 * The inode mutex is locked, but debugfs_create_dir() will also
6145 * take the mutex. As the instances directory can not be destroyed
6146 * or changed in any other way, it is safe to unlock it, and
6147 * let the dentry try. If two users try to make the same dir at
6148 * the same time, then the new_instance_create() will determine the
6151 mutex_unlock(&inode->i_mutex);
6153 ret = new_instance_create(dentry->d_iname);
6155 mutex_lock(&inode->i_mutex);
6160 static int instance_rmdir(struct inode *inode, struct dentry *dentry)
6162 struct dentry *parent;
6165 /* Paranoid: Make sure the parent is the "instances" directory */
6166 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
6167 if (WARN_ON_ONCE(parent != trace_instance_dir))
6170 /* The caller did a dget() on dentry */
6171 mutex_unlock(&dentry->d_inode->i_mutex);
6174 * The inode mutex is locked, but debugfs_create_dir() will also
6175 * take the mutex. As the instances directory can not be destroyed
6176 * or changed in any other way, it is safe to unlock it, and
6177 * let the dentry try. If two users try to make the same dir at
6178 * the same time, then the instance_delete() will determine the
6181 mutex_unlock(&inode->i_mutex);
6183 ret = instance_delete(dentry->d_iname);
6185 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
6186 mutex_lock(&dentry->d_inode->i_mutex);
6191 static const struct inode_operations instance_dir_inode_operations = {
6192 .lookup = simple_lookup,
6193 .mkdir = instance_mkdir,
6194 .rmdir = instance_rmdir,
6197 static __init void create_trace_instances(struct dentry *d_tracer)
6199 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
6200 if (WARN_ON(!trace_instance_dir))
6203 /* Hijack the dir inode operations, to allow mkdir */
6204 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
6208 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
6212 trace_create_file("trace_options", 0644, d_tracer,
6213 tr, &tracing_iter_fops);
6215 trace_create_file("trace", 0644, d_tracer,
6218 trace_create_file("trace_pipe", 0444, d_tracer,
6219 tr, &tracing_pipe_fops);
6221 trace_create_file("buffer_size_kb", 0644, d_tracer,
6222 tr, &tracing_entries_fops);
6224 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
6225 tr, &tracing_total_entries_fops);
6227 trace_create_file("free_buffer", 0644, d_tracer,
6228 tr, &tracing_free_buffer_fops);
6230 trace_create_file("trace_marker", 0220, d_tracer,
6231 tr, &tracing_mark_fops);
6233 trace_create_file("saved_tgids", 0444, d_tracer,
6234 tr, &tracing_saved_tgids_fops);
6236 trace_create_file("trace_clock", 0644, d_tracer, tr,
6239 trace_create_file("tracing_on", 0644, d_tracer,
6240 tr, &rb_simple_fops);
6242 #ifdef CONFIG_TRACER_SNAPSHOT
6243 trace_create_file("snapshot", 0644, d_tracer,
6244 tr, &snapshot_fops);
6247 for_each_tracing_cpu(cpu)
6248 tracing_init_debugfs_percpu(tr, cpu);
6252 static __init int tracer_init_debugfs(void)
6254 struct dentry *d_tracer;
6256 trace_access_lock_init();
6258 d_tracer = tracing_init_dentry();
6262 init_tracer_debugfs(&global_trace, d_tracer);
6264 trace_create_file("tracing_cpumask", 0644, d_tracer,
6265 &global_trace, &tracing_cpumask_fops);
6267 trace_create_file("available_tracers", 0444, d_tracer,
6268 &global_trace, &show_traces_fops);
6270 trace_create_file("current_tracer", 0644, d_tracer,
6271 &global_trace, &set_tracer_fops);
6273 #ifdef CONFIG_TRACER_MAX_TRACE
6274 trace_create_file("tracing_max_latency", 0644, d_tracer,
6275 &tracing_max_latency, &tracing_max_lat_fops);
6278 trace_create_file("tracing_thresh", 0644, d_tracer,
6279 &tracing_thresh, &tracing_max_lat_fops);
6281 trace_create_file("README", 0444, d_tracer,
6282 NULL, &tracing_readme_fops);
6284 trace_create_file("saved_cmdlines", 0444, d_tracer,
6285 NULL, &tracing_saved_cmdlines_fops);
6287 #ifdef CONFIG_DYNAMIC_FTRACE
6288 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
6289 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
6292 create_trace_instances(d_tracer);
6294 create_trace_options_dir(&global_trace);
6299 static int trace_panic_handler(struct notifier_block *this,
6300 unsigned long event, void *unused)
6302 if (ftrace_dump_on_oops)
6303 ftrace_dump(ftrace_dump_on_oops);
6307 static struct notifier_block trace_panic_notifier = {
6308 .notifier_call = trace_panic_handler,
6310 .priority = 150 /* priority: INT_MAX >= x >= 0 */
6313 static int trace_die_handler(struct notifier_block *self,
6319 if (ftrace_dump_on_oops)
6320 ftrace_dump(ftrace_dump_on_oops);
6328 static struct notifier_block trace_die_notifier = {
6329 .notifier_call = trace_die_handler,
6334 * printk is set to max of 1024, we really don't need it that big.
6335 * Nothing should be printing 1000 characters anyway.
6337 #define TRACE_MAX_PRINT 1000
6340 * Define here KERN_TRACE so that we have one place to modify
6341 * it if we decide to change what log level the ftrace dump
6344 #define KERN_TRACE KERN_EMERG
6347 trace_printk_seq(struct trace_seq *s)
6349 /* Probably should print a warning here. */
6350 if (s->len >= TRACE_MAX_PRINT)
6351 s->len = TRACE_MAX_PRINT;
6353 /* should be zero ended, but we are paranoid. */
6354 s->buffer[s->len] = 0;
6356 printk(KERN_TRACE "%s", s->buffer);
6361 void trace_init_global_iter(struct trace_iterator *iter)
6363 iter->tr = &global_trace;
6364 iter->trace = iter->tr->current_trace;
6365 iter->cpu_file = RING_BUFFER_ALL_CPUS;
6366 iter->trace_buffer = &global_trace.trace_buffer;
6369 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
6371 /* use static because iter can be a bit big for the stack */
6372 static struct trace_iterator iter;
6373 static atomic_t dump_running;
6374 unsigned int old_userobj;
6375 unsigned long flags;
6378 /* Only allow one dump user at a time. */
6379 if (atomic_inc_return(&dump_running) != 1) {
6380 atomic_dec(&dump_running);
6385 * Always turn off tracing when we dump.
6386 * We don't need to show trace output of what happens
6387 * between multiple crashes.
6389 * If the user does a sysrq-z, then they can re-enable
6390 * tracing with echo 1 > tracing_on.
6394 local_irq_save(flags);
6396 /* Simulate the iterator */
6397 trace_init_global_iter(&iter);
6399 for_each_tracing_cpu(cpu) {
6400 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
6403 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
6405 /* don't look at user memory in panic mode */
6406 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
6408 switch (oops_dump_mode) {
6410 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6413 iter.cpu_file = raw_smp_processor_id();
6418 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
6419 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6422 printk(KERN_TRACE "Dumping ftrace buffer:\n");
6424 /* Did function tracer already get disabled? */
6425 if (ftrace_is_dead()) {
6426 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
6427 printk("# MAY BE MISSING FUNCTION EVENTS\n");
6431 * We need to stop all tracing on all CPUS to read the
6432 * the next buffer. This is a bit expensive, but is
6433 * not done often. We fill all what we can read,
6434 * and then release the locks again.
6437 while (!trace_empty(&iter)) {
6440 printk(KERN_TRACE "---------------------------------\n");
6444 /* reset all but tr, trace, and overruns */
6445 memset(&iter.seq, 0,
6446 sizeof(struct trace_iterator) -
6447 offsetof(struct trace_iterator, seq));
6448 iter.iter_flags |= TRACE_FILE_LAT_FMT;
6451 if (trace_find_next_entry_inc(&iter) != NULL) {
6454 ret = print_trace_line(&iter);
6455 if (ret != TRACE_TYPE_NO_CONSUME)
6456 trace_consume(&iter);
6458 touch_nmi_watchdog();
6460 trace_printk_seq(&iter.seq);
6464 printk(KERN_TRACE " (ftrace buffer empty)\n");
6466 printk(KERN_TRACE "---------------------------------\n");
6469 trace_flags |= old_userobj;
6471 for_each_tracing_cpu(cpu) {
6472 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
6474 atomic_dec(&dump_running);
6475 local_irq_restore(flags);
6477 EXPORT_SYMBOL_GPL(ftrace_dump);
6479 __init static int tracer_alloc_buffers(void)
6485 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
6488 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
6489 goto out_free_buffer_mask;
6491 /* Only allocate trace_printk buffers if a trace_printk exists */
6492 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
6493 /* Must be called before global_trace.buffer is allocated */
6494 trace_printk_init_buffers();
6496 /* To save memory, keep the ring buffer size to its minimum */
6497 if (ring_buffer_expanded)
6498 ring_buf_size = trace_buf_size;
6502 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
6503 cpumask_copy(tracing_cpumask, cpu_all_mask);
6505 raw_spin_lock_init(&global_trace.start_lock);
6507 /* TODO: make the number of buffers hot pluggable with CPUS */
6508 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
6509 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
6511 goto out_free_cpumask;
6514 if (global_trace.buffer_disabled)
6517 trace_init_cmdlines();
6520 * register_tracer() might reference current_trace, so it
6521 * needs to be set before we register anything. This is
6522 * just a bootstrap of current_trace anyway.
6524 global_trace.current_trace = &nop_trace;
6526 register_tracer(&nop_trace);
6528 /* All seems OK, enable tracing */
6529 tracing_disabled = 0;
6531 atomic_notifier_chain_register(&panic_notifier_list,
6532 &trace_panic_notifier);
6534 register_die_notifier(&trace_die_notifier);
6536 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
6538 /* Holder for file callbacks */
6539 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
6540 global_trace.trace_cpu.tr = &global_trace;
6542 INIT_LIST_HEAD(&global_trace.systems);
6543 INIT_LIST_HEAD(&global_trace.events);
6544 list_add(&global_trace.list, &ftrace_trace_arrays);
6546 while (trace_boot_options) {
6549 option = strsep(&trace_boot_options, ",");
6550 trace_set_options(&global_trace, option);
6553 register_snapshot_cmd();
6558 free_percpu(global_trace.trace_buffer.data);
6559 #ifdef CONFIG_TRACER_MAX_TRACE
6560 free_percpu(global_trace.max_buffer.data);
6562 free_cpumask_var(tracing_cpumask);
6563 out_free_buffer_mask:
6564 free_cpumask_var(tracing_buffer_mask);
6569 __init static int clear_boot_tracer(void)
6572 * The default tracer at boot buffer is an init section.
6573 * This function is called in lateinit. If we did not
6574 * find the boot tracer, then clear it out, to prevent
6575 * later registration from accessing the buffer that is
6576 * about to be freed.
6578 if (!default_bootup_tracer)
6581 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6582 default_bootup_tracer);
6583 default_bootup_tracer = NULL;
6588 early_initcall(tracer_alloc_buffers);
6589 fs_initcall(tracer_init_debugfs);
6590 late_initcall(clear_boot_tracer);