2 * ring buffer based function tracer
4 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 Nadia Yvette Chambers
14 #include <linux/ring_buffer.h>
15 #include <generated/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/rwsem.h>
35 #include <linux/slab.h>
36 #include <linux/ctype.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
39 #include <linux/nmi.h>
41 #include <linux/sched/rt.h>
44 #include "trace_output.h"
47 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
50 int ring_buffer_expanded
;
53 * We need to change this state when a selftest is running.
54 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
56 * insertions into the ring-buffer such as trace_printk could occurred
57 * at the same time, giving false positive or negative results.
59 static bool __read_mostly tracing_selftest_running
;
62 * If a tracer is running, we do not want to run SELFTEST.
64 bool __read_mostly tracing_selftest_disabled
;
66 /* For tracers that don't implement custom flags */
67 static struct tracer_opt dummy_tracer_opt
[] = {
71 static struct tracer_flags dummy_tracer_flags
= {
73 .opts
= dummy_tracer_opt
76 static int dummy_set_flag(u32 old_flags
, u32 bit
, int set
)
82 * To prevent the comm cache from being overwritten when no
83 * tracing is active, only save the comm when a trace event
86 static DEFINE_PER_CPU(bool, trace_cmdline_save
);
89 * Kill all tracing for good (never come back).
90 * It is initialized to 1 but will turn to zero if the initialization
91 * of the tracer is successful. But that is the only place that sets
94 static int tracing_disabled
= 1;
96 DEFINE_PER_CPU(int, ftrace_cpu_disabled
);
98 cpumask_var_t __read_mostly tracing_buffer_mask
;
101 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
103 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
104 * is set, then ftrace_dump is called. This will output the contents
105 * of the ftrace buffers to the console. This is very useful for
106 * capturing traces that lead to crashes and outputing it to a
109 * It is default off, but you can enable it with either specifying
110 * "ftrace_dump_on_oops" in the kernel command line, or setting
111 * /proc/sys/kernel/ftrace_dump_on_oops
112 * Set 1 if you want to dump buffers of all CPUs
113 * Set 2 if you want to dump the buffer of the CPU that triggered oops
116 enum ftrace_dump_mode ftrace_dump_on_oops
;
118 static int tracing_set_tracer(const char *buf
);
120 #define MAX_TRACER_SIZE 100
121 static char bootup_tracer_buf
[MAX_TRACER_SIZE
] __initdata
;
122 static char *default_bootup_tracer
;
124 static int __init
set_cmdline_ftrace(char *str
)
126 strncpy(bootup_tracer_buf
, str
, MAX_TRACER_SIZE
);
127 default_bootup_tracer
= bootup_tracer_buf
;
128 /* We are using ftrace early, expand it */
129 ring_buffer_expanded
= 1;
132 __setup("ftrace=", set_cmdline_ftrace
);
134 static int __init
set_ftrace_dump_on_oops(char *str
)
136 if (*str
++ != '=' || !*str
) {
137 ftrace_dump_on_oops
= DUMP_ALL
;
141 if (!strcmp("orig_cpu", str
)) {
142 ftrace_dump_on_oops
= DUMP_ORIG
;
148 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
151 static char trace_boot_options_buf
[MAX_TRACER_SIZE
] __initdata
;
152 static char *trace_boot_options __initdata
;
154 static int __init
set_trace_boot_options(char *str
)
156 strncpy(trace_boot_options_buf
, str
, MAX_TRACER_SIZE
);
157 trace_boot_options
= trace_boot_options_buf
;
160 __setup("trace_options=", set_trace_boot_options
);
162 unsigned long long ns2usecs(cycle_t nsec
)
170 * The global_trace is the descriptor that holds the tracing
171 * buffers for the live tracing. For each CPU, it contains
172 * a link list of pages that will store trace entries. The
173 * page descriptor of the pages in the memory is used to hold
174 * the link list by linking the lru item in the page descriptor
175 * to each of the pages in the buffer per CPU.
177 * For each active CPU there is a data field that holds the
178 * pages for the buffer for that CPU. Each CPU has the same number
179 * of pages allocated for its buffer.
181 static struct trace_array global_trace
;
183 LIST_HEAD(ftrace_trace_arrays
);
185 int filter_current_check_discard(struct ring_buffer
*buffer
,
186 struct ftrace_event_call
*call
, void *rec
,
187 struct ring_buffer_event
*event
)
189 return filter_check_discard(call
, rec
, buffer
, event
);
191 EXPORT_SYMBOL_GPL(filter_current_check_discard
);
193 cycle_t
ftrace_now(int cpu
)
197 /* Early boot up does not have a buffer yet */
198 if (!global_trace
.trace_buffer
.buffer
)
199 return trace_clock_local();
201 ts
= ring_buffer_time_stamp(global_trace
.trace_buffer
.buffer
, cpu
);
202 ring_buffer_normalize_time_stamp(global_trace
.trace_buffer
.buffer
, cpu
, &ts
);
207 int tracing_is_enabled(void)
209 return tracing_is_on();
213 * trace_buf_size is the size in bytes that is allocated
214 * for a buffer. Note, the number of bytes is always rounded
217 * This number is purposely set to a low number of 16384.
218 * If the dump on oops happens, it will be much appreciated
219 * to not have to wait for all that output. Anyway this can be
220 * boot time and run time configurable.
222 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
224 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
226 /* trace_types holds a link list of available tracers. */
227 static struct tracer
*trace_types __read_mostly
;
230 * trace_types_lock is used to protect the trace_types list.
232 static DEFINE_MUTEX(trace_types_lock
);
235 * serialize the access of the ring buffer
237 * ring buffer serializes readers, but it is low level protection.
238 * The validity of the events (which returns by ring_buffer_peek() ..etc)
239 * are not protected by ring buffer.
241 * The content of events may become garbage if we allow other process consumes
242 * these events concurrently:
243 * A) the page of the consumed events may become a normal page
244 * (not reader page) in ring buffer, and this page will be rewrited
245 * by events producer.
246 * B) The page of the consumed events may become a page for splice_read,
247 * and this page will be returned to system.
249 * These primitives allow multi process access to different cpu ring buffer
252 * These primitives don't distinguish read-only and read-consume access.
253 * Multi read-only access are also serialized.
257 static DECLARE_RWSEM(all_cpu_access_lock
);
258 static DEFINE_PER_CPU(struct mutex
, cpu_access_lock
);
260 static inline void trace_access_lock(int cpu
)
262 if (cpu
== RING_BUFFER_ALL_CPUS
) {
263 /* gain it for accessing the whole ring buffer. */
264 down_write(&all_cpu_access_lock
);
266 /* gain it for accessing a cpu ring buffer. */
268 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
269 down_read(&all_cpu_access_lock
);
271 /* Secondly block other access to this @cpu ring buffer. */
272 mutex_lock(&per_cpu(cpu_access_lock
, cpu
));
276 static inline void trace_access_unlock(int cpu
)
278 if (cpu
== RING_BUFFER_ALL_CPUS
) {
279 up_write(&all_cpu_access_lock
);
281 mutex_unlock(&per_cpu(cpu_access_lock
, cpu
));
282 up_read(&all_cpu_access_lock
);
286 static inline void trace_access_lock_init(void)
290 for_each_possible_cpu(cpu
)
291 mutex_init(&per_cpu(cpu_access_lock
, cpu
));
296 static DEFINE_MUTEX(access_lock
);
298 static inline void trace_access_lock(int cpu
)
301 mutex_lock(&access_lock
);
304 static inline void trace_access_unlock(int cpu
)
307 mutex_unlock(&access_lock
);
310 static inline void trace_access_lock_init(void)
316 /* trace_flags holds trace_options default values */
317 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
318 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
| TRACE_ITER_SLEEP_TIME
|
319 TRACE_ITER_GRAPH_TIME
| TRACE_ITER_RECORD_CMD
| TRACE_ITER_OVERWRITE
|
320 TRACE_ITER_IRQ_INFO
| TRACE_ITER_MARKERS
;
323 * tracing_on - enable tracing buffers
325 * This function enables tracing buffers that may have been
326 * disabled with tracing_off.
328 void tracing_on(void)
330 if (global_trace
.trace_buffer
.buffer
)
331 ring_buffer_record_on(global_trace
.trace_buffer
.buffer
);
333 * This flag is only looked at when buffers haven't been
334 * allocated yet. We don't really care about the race
335 * between setting this flag and actually turning
338 global_trace
.buffer_disabled
= 0;
340 EXPORT_SYMBOL_GPL(tracing_on
);
343 * tracing_off - turn off tracing buffers
345 * This function stops the tracing buffers from recording data.
346 * It does not disable any overhead the tracers themselves may
347 * be causing. This function simply causes all recording to
348 * the ring buffers to fail.
350 void tracing_off(void)
352 if (global_trace
.trace_buffer
.buffer
)
353 ring_buffer_record_off(global_trace
.trace_buffer
.buffer
);
355 * This flag is only looked at when buffers haven't been
356 * allocated yet. We don't really care about the race
357 * between setting this flag and actually turning
360 global_trace
.buffer_disabled
= 1;
362 EXPORT_SYMBOL_GPL(tracing_off
);
365 * tracing_is_on - show state of ring buffers enabled
367 int tracing_is_on(void)
369 if (global_trace
.trace_buffer
.buffer
)
370 return ring_buffer_record_is_on(global_trace
.trace_buffer
.buffer
);
371 return !global_trace
.buffer_disabled
;
373 EXPORT_SYMBOL_GPL(tracing_is_on
);
375 static int __init
set_buf_size(char *str
)
377 unsigned long buf_size
;
381 buf_size
= memparse(str
, &str
);
382 /* nr_entries can not be zero */
385 trace_buf_size
= buf_size
;
388 __setup("trace_buf_size=", set_buf_size
);
390 static int __init
set_tracing_thresh(char *str
)
392 unsigned long threshold
;
397 ret
= kstrtoul(str
, 0, &threshold
);
400 tracing_thresh
= threshold
* 1000;
403 __setup("tracing_thresh=", set_tracing_thresh
);
405 unsigned long nsecs_to_usecs(unsigned long nsecs
)
410 /* These must match the bit postions in trace_iterator_flags */
411 static const char *trace_options
[] = {
443 int in_ns
; /* is this clock in nanoseconds? */
445 { trace_clock_local
, "local", 1 },
446 { trace_clock_global
, "global", 1 },
447 { trace_clock_counter
, "counter", 0 },
454 * trace_parser_get_init - gets the buffer for trace parser
456 int trace_parser_get_init(struct trace_parser
*parser
, int size
)
458 memset(parser
, 0, sizeof(*parser
));
460 parser
->buffer
= kmalloc(size
, GFP_KERNEL
);
469 * trace_parser_put - frees the buffer for trace parser
471 void trace_parser_put(struct trace_parser
*parser
)
473 kfree(parser
->buffer
);
477 * trace_get_user - reads the user input string separated by space
478 * (matched by isspace(ch))
480 * For each string found the 'struct trace_parser' is updated,
481 * and the function returns.
483 * Returns number of bytes read.
485 * See kernel/trace/trace.h for 'struct trace_parser' details.
487 int trace_get_user(struct trace_parser
*parser
, const char __user
*ubuf
,
488 size_t cnt
, loff_t
*ppos
)
495 trace_parser_clear(parser
);
497 ret
= get_user(ch
, ubuf
++);
505 * The parser is not finished with the last write,
506 * continue reading the user input without skipping spaces.
509 /* skip white space */
510 while (cnt
&& isspace(ch
)) {
511 ret
= get_user(ch
, ubuf
++);
518 /* only spaces were written */
528 /* read the non-space input */
529 while (cnt
&& !isspace(ch
)) {
530 if (parser
->idx
< parser
->size
- 1)
531 parser
->buffer
[parser
->idx
++] = ch
;
536 ret
= get_user(ch
, ubuf
++);
543 /* We either got finished input or we have to wait for another call. */
545 parser
->buffer
[parser
->idx
] = 0;
546 parser
->cont
= false;
549 parser
->buffer
[parser
->idx
++] = ch
;
559 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
567 if (s
->len
<= s
->readpos
)
570 len
= s
->len
- s
->readpos
;
573 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
583 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
587 if (s
->len
<= s
->readpos
)
590 len
= s
->len
- s
->readpos
;
593 memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
600 * ftrace_max_lock is used to protect the swapping of buffers
601 * when taking a max snapshot. The buffers themselves are
602 * protected by per_cpu spinlocks. But the action of the swap
603 * needs its own lock.
605 * This is defined as a arch_spinlock_t in order to help
606 * with performance when lockdep debugging is enabled.
608 * It is also used in other places outside the update_max_tr
609 * so it needs to be defined outside of the
610 * CONFIG_TRACER_MAX_TRACE.
612 static arch_spinlock_t ftrace_max_lock
=
613 (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
615 unsigned long __read_mostly tracing_thresh
;
617 #ifdef CONFIG_TRACER_MAX_TRACE
618 unsigned long __read_mostly tracing_max_latency
;
621 * Copy the new maximum trace into the separate maximum-trace
622 * structure. (this way the maximum trace is permanently saved,
623 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
626 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
628 struct trace_buffer
*trace_buf
= &tr
->trace_buffer
;
629 struct trace_buffer
*max_buf
= &tr
->max_buffer
;
630 struct trace_array_cpu
*data
= per_cpu_ptr(trace_buf
->data
, cpu
);
631 struct trace_array_cpu
*max_data
= per_cpu_ptr(max_buf
->data
, cpu
);
634 max_buf
->time_start
= data
->preempt_timestamp
;
636 max_data
->saved_latency
= tracing_max_latency
;
637 max_data
->critical_start
= data
->critical_start
;
638 max_data
->critical_end
= data
->critical_end
;
640 memcpy(max_data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
641 max_data
->pid
= tsk
->pid
;
642 max_data
->uid
= task_uid(tsk
);
643 max_data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
644 max_data
->policy
= tsk
->policy
;
645 max_data
->rt_priority
= tsk
->rt_priority
;
647 /* record this tasks comm */
648 tracing_record_cmdline(tsk
);
652 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
654 * @tsk: the task with the latency
655 * @cpu: The cpu that initiated the trace.
657 * Flip the buffers between the @tr and the max_tr and record information
658 * about which task was the cause of this latency.
661 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
663 struct ring_buffer
*buf
;
668 WARN_ON_ONCE(!irqs_disabled());
670 if (!tr
->current_trace
->allocated_snapshot
) {
671 /* Only the nop tracer should hit this when disabling */
672 WARN_ON_ONCE(tr
->current_trace
!= &nop_trace
);
676 arch_spin_lock(&ftrace_max_lock
);
678 buf
= tr
->trace_buffer
.buffer
;
679 tr
->trace_buffer
.buffer
= tr
->max_buffer
.buffer
;
680 tr
->max_buffer
.buffer
= buf
;
682 __update_max_tr(tr
, tsk
, cpu
);
683 arch_spin_unlock(&ftrace_max_lock
);
687 * update_max_tr_single - only copy one trace over, and reset the rest
689 * @tsk - task with the latency
690 * @cpu - the cpu of the buffer to copy.
692 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
695 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
702 WARN_ON_ONCE(!irqs_disabled());
703 if (WARN_ON_ONCE(!tr
->current_trace
->allocated_snapshot
))
706 arch_spin_lock(&ftrace_max_lock
);
708 ret
= ring_buffer_swap_cpu(tr
->max_buffer
.buffer
, tr
->trace_buffer
.buffer
, cpu
);
712 * We failed to swap the buffer due to a commit taking
713 * place on this CPU. We fail to record, but we reset
714 * the max trace buffer (no one writes directly to it)
715 * and flag that it failed.
717 trace_array_printk_buf(tr
->max_buffer
.buffer
, _THIS_IP_
,
718 "Failed to swap buffers due to commit in progress\n");
721 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
&& ret
!= -EBUSY
);
723 __update_max_tr(tr
, tsk
, cpu
);
724 arch_spin_unlock(&ftrace_max_lock
);
726 #endif /* CONFIG_TRACER_MAX_TRACE */
728 static void default_wait_pipe(struct trace_iterator
*iter
)
730 /* Iterators are static, they should be filled or empty */
731 if (trace_buffer_iter(iter
, iter
->cpu_file
))
734 ring_buffer_wait(iter
->trace_buffer
->buffer
, iter
->cpu_file
);
738 * register_tracer - register a tracer with the ftrace system.
739 * @type - the plugin for the tracer
741 * Register a new plugin tracer.
743 int register_tracer(struct tracer
*type
)
749 pr_info("Tracer must have a name\n");
753 if (strlen(type
->name
) >= MAX_TRACER_SIZE
) {
754 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE
);
758 mutex_lock(&trace_types_lock
);
760 tracing_selftest_running
= true;
762 for (t
= trace_types
; t
; t
= t
->next
) {
763 if (strcmp(type
->name
, t
->name
) == 0) {
765 pr_info("Tracer %s already registered\n",
773 type
->set_flag
= &dummy_set_flag
;
775 type
->flags
= &dummy_tracer_flags
;
777 if (!type
->flags
->opts
)
778 type
->flags
->opts
= dummy_tracer_opt
;
779 if (!type
->wait_pipe
)
780 type
->wait_pipe
= default_wait_pipe
;
783 #ifdef CONFIG_FTRACE_STARTUP_TEST
784 if (type
->selftest
&& !tracing_selftest_disabled
) {
785 struct trace_array
*tr
= &global_trace
;
786 struct tracer
*saved_tracer
= tr
->current_trace
;
789 * Run a selftest on this tracer.
790 * Here we reset the trace buffer, and set the current
791 * tracer to be this tracer. The tracer can then run some
792 * internal tracing to verify that everything is in order.
793 * If we fail, we do not register this tracer.
795 tracing_reset_online_cpus(&tr
->trace_buffer
);
797 tr
->current_trace
= type
;
799 #ifdef CONFIG_TRACER_MAX_TRACE
800 if (type
->use_max_tr
) {
801 /* If we expanded the buffers, make sure the max is expanded too */
802 if (ring_buffer_expanded
)
803 ring_buffer_resize(tr
->max_buffer
.buffer
, trace_buf_size
,
804 RING_BUFFER_ALL_CPUS
);
805 type
->allocated_snapshot
= true;
809 /* the test is responsible for initializing and enabling */
810 pr_info("Testing tracer %s: ", type
->name
);
811 ret
= type
->selftest(type
, tr
);
812 /* the test is responsible for resetting too */
813 tr
->current_trace
= saved_tracer
;
815 printk(KERN_CONT
"FAILED!\n");
816 /* Add the warning after printing 'FAILED' */
820 /* Only reset on passing, to avoid touching corrupted buffers */
821 tracing_reset_online_cpus(&tr
->trace_buffer
);
823 #ifdef CONFIG_TRACER_MAX_TRACE
824 if (type
->use_max_tr
) {
825 type
->allocated_snapshot
= false;
827 /* Shrink the max buffer again */
828 if (ring_buffer_expanded
)
829 ring_buffer_resize(tr
->max_buffer
.buffer
, 1,
830 RING_BUFFER_ALL_CPUS
);
834 printk(KERN_CONT
"PASSED\n");
838 type
->next
= trace_types
;
842 tracing_selftest_running
= false;
843 mutex_unlock(&trace_types_lock
);
845 if (ret
|| !default_bootup_tracer
)
848 if (strncmp(default_bootup_tracer
, type
->name
, MAX_TRACER_SIZE
))
851 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
852 /* Do we want this tracer to start on bootup? */
853 tracing_set_tracer(type
->name
);
854 default_bootup_tracer
= NULL
;
855 /* disable other selftests, since this will break it. */
856 tracing_selftest_disabled
= 1;
857 #ifdef CONFIG_FTRACE_STARTUP_TEST
858 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
866 void tracing_reset(struct trace_buffer
*buf
, int cpu
)
868 struct ring_buffer
*buffer
= buf
->buffer
;
873 ring_buffer_record_disable(buffer
);
875 /* Make sure all commits have finished */
877 ring_buffer_reset_cpu(buffer
, cpu
);
879 ring_buffer_record_enable(buffer
);
882 void tracing_reset_online_cpus(struct trace_buffer
*buf
)
884 struct ring_buffer
*buffer
= buf
->buffer
;
890 ring_buffer_record_disable(buffer
);
892 /* Make sure all commits have finished */
895 buf
->time_start
= ftrace_now(buf
->cpu
);
897 for_each_online_cpu(cpu
)
898 ring_buffer_reset_cpu(buffer
, cpu
);
900 ring_buffer_record_enable(buffer
);
903 void tracing_reset_current(int cpu
)
905 tracing_reset(&global_trace
.trace_buffer
, cpu
);
908 void tracing_reset_all_online_cpus(void)
910 struct trace_array
*tr
;
912 mutex_lock(&trace_types_lock
);
913 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
914 tracing_reset_online_cpus(&tr
->trace_buffer
);
915 #ifdef CONFIG_TRACER_MAX_TRACE
916 tracing_reset_online_cpus(&tr
->max_buffer
);
919 mutex_unlock(&trace_types_lock
);
922 #define SAVED_CMDLINES 128
923 #define NO_CMDLINE_MAP UINT_MAX
924 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
925 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
926 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
927 static int cmdline_idx
;
928 static arch_spinlock_t trace_cmdline_lock
= __ARCH_SPIN_LOCK_UNLOCKED
;
930 /* temporary disable recording */
931 static atomic_t trace_record_cmdline_disabled __read_mostly
;
933 static void trace_init_cmdlines(void)
935 memset(&map_pid_to_cmdline
, NO_CMDLINE_MAP
, sizeof(map_pid_to_cmdline
));
936 memset(&map_cmdline_to_pid
, NO_CMDLINE_MAP
, sizeof(map_cmdline_to_pid
));
940 int is_tracing_stopped(void)
942 return global_trace
.stop_count
;
946 * ftrace_off_permanent - disable all ftrace code permanently
948 * This should only be called when a serious anomally has
949 * been detected. This will turn off the function tracing,
950 * ring buffers, and other tracing utilites. It takes no
951 * locks and can be called from any context.
953 void ftrace_off_permanent(void)
955 tracing_disabled
= 1;
957 tracing_off_permanent();
961 * tracing_start - quick start of the tracer
963 * If tracing is enabled but was stopped by tracing_stop,
964 * this will start the tracer back up.
966 void tracing_start(void)
968 struct ring_buffer
*buffer
;
971 if (tracing_disabled
)
974 raw_spin_lock_irqsave(&global_trace
.start_lock
, flags
);
975 if (--global_trace
.stop_count
) {
976 if (global_trace
.stop_count
< 0) {
977 /* Someone screwed up their debugging */
979 global_trace
.stop_count
= 0;
984 /* Prevent the buffers from switching */
985 arch_spin_lock(&ftrace_max_lock
);
987 buffer
= global_trace
.trace_buffer
.buffer
;
989 ring_buffer_record_enable(buffer
);
991 #ifdef CONFIG_TRACER_MAX_TRACE
992 buffer
= global_trace
.max_buffer
.buffer
;
994 ring_buffer_record_enable(buffer
);
997 arch_spin_unlock(&ftrace_max_lock
);
1001 raw_spin_unlock_irqrestore(&global_trace
.start_lock
, flags
);
1004 static void tracing_start_tr(struct trace_array
*tr
)
1006 struct ring_buffer
*buffer
;
1007 unsigned long flags
;
1009 if (tracing_disabled
)
1012 /* If global, we need to also start the max tracer */
1013 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
)
1014 return tracing_start();
1016 raw_spin_lock_irqsave(&tr
->start_lock
, flags
);
1018 if (--tr
->stop_count
) {
1019 if (tr
->stop_count
< 0) {
1020 /* Someone screwed up their debugging */
1027 buffer
= tr
->trace_buffer
.buffer
;
1029 ring_buffer_record_enable(buffer
);
1032 raw_spin_unlock_irqrestore(&tr
->start_lock
, flags
);
1036 * tracing_stop - quick stop of the tracer
1038 * Light weight way to stop tracing. Use in conjunction with
1041 void tracing_stop(void)
1043 struct ring_buffer
*buffer
;
1044 unsigned long flags
;
1047 raw_spin_lock_irqsave(&global_trace
.start_lock
, flags
);
1048 if (global_trace
.stop_count
++)
1051 /* Prevent the buffers from switching */
1052 arch_spin_lock(&ftrace_max_lock
);
1054 buffer
= global_trace
.trace_buffer
.buffer
;
1056 ring_buffer_record_disable(buffer
);
1058 #ifdef CONFIG_TRACER_MAX_TRACE
1059 buffer
= global_trace
.max_buffer
.buffer
;
1061 ring_buffer_record_disable(buffer
);
1064 arch_spin_unlock(&ftrace_max_lock
);
1067 raw_spin_unlock_irqrestore(&global_trace
.start_lock
, flags
);
1070 static void tracing_stop_tr(struct trace_array
*tr
)
1072 struct ring_buffer
*buffer
;
1073 unsigned long flags
;
1075 /* If global, we need to also stop the max tracer */
1076 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
)
1077 return tracing_stop();
1079 raw_spin_lock_irqsave(&tr
->start_lock
, flags
);
1080 if (tr
->stop_count
++)
1083 buffer
= tr
->trace_buffer
.buffer
;
1085 ring_buffer_record_disable(buffer
);
1088 raw_spin_unlock_irqrestore(&tr
->start_lock
, flags
);
1091 void trace_stop_cmdline_recording(void);
1093 static void trace_save_cmdline(struct task_struct
*tsk
)
1097 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
1101 * It's not the end of the world if we don't get
1102 * the lock, but we also don't want to spin
1103 * nor do we want to disable interrupts,
1104 * so if we miss here, then better luck next time.
1106 if (!arch_spin_trylock(&trace_cmdline_lock
))
1109 idx
= map_pid_to_cmdline
[tsk
->pid
];
1110 if (idx
== NO_CMDLINE_MAP
) {
1111 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
1114 * Check whether the cmdline buffer at idx has a pid
1115 * mapped. We are going to overwrite that entry so we
1116 * need to clear the map_pid_to_cmdline. Otherwise we
1117 * would read the new comm for the old pid.
1119 pid
= map_cmdline_to_pid
[idx
];
1120 if (pid
!= NO_CMDLINE_MAP
)
1121 map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
1123 map_cmdline_to_pid
[idx
] = tsk
->pid
;
1124 map_pid_to_cmdline
[tsk
->pid
] = idx
;
1129 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
1131 arch_spin_unlock(&trace_cmdline_lock
);
1134 void trace_find_cmdline(int pid
, char comm
[])
1139 strcpy(comm
, "<idle>");
1143 if (WARN_ON_ONCE(pid
< 0)) {
1144 strcpy(comm
, "<XXX>");
1148 if (pid
> PID_MAX_DEFAULT
) {
1149 strcpy(comm
, "<...>");
1154 arch_spin_lock(&trace_cmdline_lock
);
1155 map
= map_pid_to_cmdline
[pid
];
1156 if (map
!= NO_CMDLINE_MAP
)
1157 strcpy(comm
, saved_cmdlines
[map
]);
1159 strcpy(comm
, "<...>");
1161 arch_spin_unlock(&trace_cmdline_lock
);
1165 void tracing_record_cmdline(struct task_struct
*tsk
)
1167 if (atomic_read(&trace_record_cmdline_disabled
) || !tracing_is_on())
1170 if (!__this_cpu_read(trace_cmdline_save
))
1173 __this_cpu_write(trace_cmdline_save
, false);
1175 trace_save_cmdline(tsk
);
1179 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
1182 struct task_struct
*tsk
= current
;
1184 entry
->preempt_count
= pc
& 0xff;
1185 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
1187 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1188 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
1190 TRACE_FLAG_IRQS_NOSUPPORT
|
1192 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
1193 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
1194 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
1196 EXPORT_SYMBOL_GPL(tracing_generic_entry_update
);
1198 struct ring_buffer_event
*
1199 trace_buffer_lock_reserve(struct ring_buffer
*buffer
,
1202 unsigned long flags
, int pc
)
1204 struct ring_buffer_event
*event
;
1206 event
= ring_buffer_lock_reserve(buffer
, len
);
1207 if (event
!= NULL
) {
1208 struct trace_entry
*ent
= ring_buffer_event_data(event
);
1210 tracing_generic_entry_update(ent
, flags
, pc
);
1218 __buffer_unlock_commit(struct ring_buffer
*buffer
, struct ring_buffer_event
*event
)
1220 __this_cpu_write(trace_cmdline_save
, true);
1221 ring_buffer_unlock_commit(buffer
, event
);
1225 __trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1226 struct ring_buffer_event
*event
,
1227 unsigned long flags
, int pc
)
1229 __buffer_unlock_commit(buffer
, event
);
1231 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1232 ftrace_trace_userstack(buffer
, flags
, pc
);
1235 void trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1236 struct ring_buffer_event
*event
,
1237 unsigned long flags
, int pc
)
1239 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
1241 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit
);
1243 struct ring_buffer_event
*
1244 trace_event_buffer_lock_reserve(struct ring_buffer
**current_rb
,
1245 struct ftrace_event_file
*ftrace_file
,
1246 int type
, unsigned long len
,
1247 unsigned long flags
, int pc
)
1249 *current_rb
= ftrace_file
->tr
->trace_buffer
.buffer
;
1250 return trace_buffer_lock_reserve(*current_rb
,
1251 type
, len
, flags
, pc
);
1253 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve
);
1255 struct ring_buffer_event
*
1256 trace_current_buffer_lock_reserve(struct ring_buffer
**current_rb
,
1257 int type
, unsigned long len
,
1258 unsigned long flags
, int pc
)
1260 *current_rb
= global_trace
.trace_buffer
.buffer
;
1261 return trace_buffer_lock_reserve(*current_rb
,
1262 type
, len
, flags
, pc
);
1264 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve
);
1266 void trace_current_buffer_unlock_commit(struct ring_buffer
*buffer
,
1267 struct ring_buffer_event
*event
,
1268 unsigned long flags
, int pc
)
1270 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
1272 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit
);
1274 void trace_buffer_unlock_commit_regs(struct ring_buffer
*buffer
,
1275 struct ring_buffer_event
*event
,
1276 unsigned long flags
, int pc
,
1277 struct pt_regs
*regs
)
1279 __buffer_unlock_commit(buffer
, event
);
1281 ftrace_trace_stack_regs(buffer
, flags
, 0, pc
, regs
);
1282 ftrace_trace_userstack(buffer
, flags
, pc
);
1284 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs
);
1286 void trace_current_buffer_discard_commit(struct ring_buffer
*buffer
,
1287 struct ring_buffer_event
*event
)
1289 ring_buffer_discard_commit(buffer
, event
);
1291 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
1294 trace_function(struct trace_array
*tr
,
1295 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1298 struct ftrace_event_call
*call
= &event_function
;
1299 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
1300 struct ring_buffer_event
*event
;
1301 struct ftrace_entry
*entry
;
1303 /* If we are reading the ring buffer, don't trace */
1304 if (unlikely(__this_cpu_read(ftrace_cpu_disabled
)))
1307 event
= trace_buffer_lock_reserve(buffer
, TRACE_FN
, sizeof(*entry
),
1311 entry
= ring_buffer_event_data(event
);
1313 entry
->parent_ip
= parent_ip
;
1315 if (!filter_check_discard(call
, entry
, buffer
, event
))
1316 __buffer_unlock_commit(buffer
, event
);
1320 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
1321 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1324 if (likely(!atomic_read(&data
->disabled
)))
1325 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
1328 #ifdef CONFIG_STACKTRACE
1330 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1331 struct ftrace_stack
{
1332 unsigned long calls
[FTRACE_STACK_MAX_ENTRIES
];
1335 static DEFINE_PER_CPU(struct ftrace_stack
, ftrace_stack
);
1336 static DEFINE_PER_CPU(int, ftrace_stack_reserve
);
1338 static void __ftrace_trace_stack(struct ring_buffer
*buffer
,
1339 unsigned long flags
,
1340 int skip
, int pc
, struct pt_regs
*regs
)
1342 struct ftrace_event_call
*call
= &event_kernel_stack
;
1343 struct ring_buffer_event
*event
;
1344 struct stack_entry
*entry
;
1345 struct stack_trace trace
;
1347 int size
= FTRACE_STACK_ENTRIES
;
1349 trace
.nr_entries
= 0;
1353 * Since events can happen in NMIs there's no safe way to
1354 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1355 * or NMI comes in, it will just have to use the default
1356 * FTRACE_STACK_SIZE.
1358 preempt_disable_notrace();
1360 use_stack
= __this_cpu_inc_return(ftrace_stack_reserve
);
1362 * We don't need any atomic variables, just a barrier.
1363 * If an interrupt comes in, we don't care, because it would
1364 * have exited and put the counter back to what we want.
1365 * We just need a barrier to keep gcc from moving things
1369 if (use_stack
== 1) {
1370 trace
.entries
= &__get_cpu_var(ftrace_stack
).calls
[0];
1371 trace
.max_entries
= FTRACE_STACK_MAX_ENTRIES
;
1374 save_stack_trace_regs(regs
, &trace
);
1376 save_stack_trace(&trace
);
1378 if (trace
.nr_entries
> size
)
1379 size
= trace
.nr_entries
;
1381 /* From now on, use_stack is a boolean */
1384 size
*= sizeof(unsigned long);
1386 event
= trace_buffer_lock_reserve(buffer
, TRACE_STACK
,
1387 sizeof(*entry
) + size
, flags
, pc
);
1390 entry
= ring_buffer_event_data(event
);
1392 memset(&entry
->caller
, 0, size
);
1395 memcpy(&entry
->caller
, trace
.entries
,
1396 trace
.nr_entries
* sizeof(unsigned long));
1398 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1399 trace
.entries
= entry
->caller
;
1401 save_stack_trace_regs(regs
, &trace
);
1403 save_stack_trace(&trace
);
1406 entry
->size
= trace
.nr_entries
;
1408 if (!filter_check_discard(call
, entry
, buffer
, event
))
1409 __buffer_unlock_commit(buffer
, event
);
1412 /* Again, don't let gcc optimize things here */
1414 __this_cpu_dec(ftrace_stack_reserve
);
1415 preempt_enable_notrace();
1419 void ftrace_trace_stack_regs(struct ring_buffer
*buffer
, unsigned long flags
,
1420 int skip
, int pc
, struct pt_regs
*regs
)
1422 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1425 __ftrace_trace_stack(buffer
, flags
, skip
, pc
, regs
);
1428 void ftrace_trace_stack(struct ring_buffer
*buffer
, unsigned long flags
,
1431 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1434 __ftrace_trace_stack(buffer
, flags
, skip
, pc
, NULL
);
1437 void __trace_stack(struct trace_array
*tr
, unsigned long flags
, int skip
,
1440 __ftrace_trace_stack(tr
->trace_buffer
.buffer
, flags
, skip
, pc
, NULL
);
1444 * trace_dump_stack - record a stack back trace in the trace buffer
1446 void trace_dump_stack(void)
1448 unsigned long flags
;
1450 if (tracing_disabled
|| tracing_selftest_running
)
1453 local_save_flags(flags
);
1455 /* skipping 3 traces, seems to get us at the caller of this function */
1456 __ftrace_trace_stack(global_trace
.trace_buffer
.buffer
, flags
, 3,
1457 preempt_count(), NULL
);
1460 static DEFINE_PER_CPU(int, user_stack_count
);
1463 ftrace_trace_userstack(struct ring_buffer
*buffer
, unsigned long flags
, int pc
)
1465 struct ftrace_event_call
*call
= &event_user_stack
;
1466 struct ring_buffer_event
*event
;
1467 struct userstack_entry
*entry
;
1468 struct stack_trace trace
;
1470 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1474 * NMIs can not handle page faults, even with fix ups.
1475 * The save user stack can (and often does) fault.
1477 if (unlikely(in_nmi()))
1481 * prevent recursion, since the user stack tracing may
1482 * trigger other kernel events.
1485 if (__this_cpu_read(user_stack_count
))
1488 __this_cpu_inc(user_stack_count
);
1490 event
= trace_buffer_lock_reserve(buffer
, TRACE_USER_STACK
,
1491 sizeof(*entry
), flags
, pc
);
1493 goto out_drop_count
;
1494 entry
= ring_buffer_event_data(event
);
1496 entry
->tgid
= current
->tgid
;
1497 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1499 trace
.nr_entries
= 0;
1500 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1502 trace
.entries
= entry
->caller
;
1504 save_stack_trace_user(&trace
);
1505 if (!filter_check_discard(call
, entry
, buffer
, event
))
1506 __buffer_unlock_commit(buffer
, event
);
1509 __this_cpu_dec(user_stack_count
);
1515 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1517 ftrace_trace_userstack(tr
, flags
, preempt_count());
1521 #endif /* CONFIG_STACKTRACE */
1523 /* created for use with alloc_percpu */
1524 struct trace_buffer_struct
{
1525 char buffer
[TRACE_BUF_SIZE
];
1528 static struct trace_buffer_struct
*trace_percpu_buffer
;
1529 static struct trace_buffer_struct
*trace_percpu_sirq_buffer
;
1530 static struct trace_buffer_struct
*trace_percpu_irq_buffer
;
1531 static struct trace_buffer_struct
*trace_percpu_nmi_buffer
;
1534 * The buffer used is dependent on the context. There is a per cpu
1535 * buffer for normal context, softirq contex, hard irq context and
1536 * for NMI context. Thise allows for lockless recording.
1538 * Note, if the buffers failed to be allocated, then this returns NULL
1540 static char *get_trace_buf(void)
1542 struct trace_buffer_struct
*percpu_buffer
;
1545 * If we have allocated per cpu buffers, then we do not
1546 * need to do any locking.
1549 percpu_buffer
= trace_percpu_nmi_buffer
;
1551 percpu_buffer
= trace_percpu_irq_buffer
;
1552 else if (in_softirq())
1553 percpu_buffer
= trace_percpu_sirq_buffer
;
1555 percpu_buffer
= trace_percpu_buffer
;
1560 return this_cpu_ptr(&percpu_buffer
->buffer
[0]);
1563 static int alloc_percpu_trace_buffer(void)
1565 struct trace_buffer_struct
*buffers
;
1566 struct trace_buffer_struct
*sirq_buffers
;
1567 struct trace_buffer_struct
*irq_buffers
;
1568 struct trace_buffer_struct
*nmi_buffers
;
1570 buffers
= alloc_percpu(struct trace_buffer_struct
);
1574 sirq_buffers
= alloc_percpu(struct trace_buffer_struct
);
1578 irq_buffers
= alloc_percpu(struct trace_buffer_struct
);
1582 nmi_buffers
= alloc_percpu(struct trace_buffer_struct
);
1586 trace_percpu_buffer
= buffers
;
1587 trace_percpu_sirq_buffer
= sirq_buffers
;
1588 trace_percpu_irq_buffer
= irq_buffers
;
1589 trace_percpu_nmi_buffer
= nmi_buffers
;
1594 free_percpu(irq_buffers
);
1596 free_percpu(sirq_buffers
);
1598 free_percpu(buffers
);
1600 WARN(1, "Could not allocate percpu trace_printk buffer");
1604 static int buffers_allocated
;
1606 void trace_printk_init_buffers(void)
1608 if (buffers_allocated
)
1611 if (alloc_percpu_trace_buffer())
1614 pr_info("ftrace: Allocated trace_printk buffers\n");
1616 /* Expand the buffers to set size */
1617 tracing_update_buffers();
1619 buffers_allocated
= 1;
1622 * trace_printk_init_buffers() can be called by modules.
1623 * If that happens, then we need to start cmdline recording
1624 * directly here. If the global_trace.buffer is already
1625 * allocated here, then this was called by module code.
1627 if (global_trace
.trace_buffer
.buffer
)
1628 tracing_start_cmdline_record();
1631 void trace_printk_start_comm(void)
1633 /* Start tracing comms if trace printk is set */
1634 if (!buffers_allocated
)
1636 tracing_start_cmdline_record();
1639 static void trace_printk_start_stop_comm(int enabled
)
1641 if (!buffers_allocated
)
1645 tracing_start_cmdline_record();
1647 tracing_stop_cmdline_record();
1651 * trace_vbprintk - write binary msg to tracing buffer
1654 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1656 struct ftrace_event_call
*call
= &event_bprint
;
1657 struct ring_buffer_event
*event
;
1658 struct ring_buffer
*buffer
;
1659 struct trace_array
*tr
= &global_trace
;
1660 struct bprint_entry
*entry
;
1661 unsigned long flags
;
1663 int len
= 0, size
, pc
;
1665 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1668 /* Don't pollute graph traces with trace_vprintk internals */
1669 pause_graph_tracing();
1671 pc
= preempt_count();
1672 preempt_disable_notrace();
1674 tbuffer
= get_trace_buf();
1680 len
= vbin_printf((u32
*)tbuffer
, TRACE_BUF_SIZE
/sizeof(int), fmt
, args
);
1682 if (len
> TRACE_BUF_SIZE
/sizeof(int) || len
< 0)
1685 local_save_flags(flags
);
1686 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1687 buffer
= tr
->trace_buffer
.buffer
;
1688 event
= trace_buffer_lock_reserve(buffer
, TRACE_BPRINT
, size
,
1692 entry
= ring_buffer_event_data(event
);
1696 memcpy(entry
->buf
, tbuffer
, sizeof(u32
) * len
);
1697 if (!filter_check_discard(call
, entry
, buffer
, event
)) {
1698 __buffer_unlock_commit(buffer
, event
);
1699 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1703 preempt_enable_notrace();
1704 unpause_graph_tracing();
1708 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1711 __trace_array_vprintk(struct ring_buffer
*buffer
,
1712 unsigned long ip
, const char *fmt
, va_list args
)
1714 struct ftrace_event_call
*call
= &event_print
;
1715 struct ring_buffer_event
*event
;
1716 int len
= 0, size
, pc
;
1717 struct print_entry
*entry
;
1718 unsigned long flags
;
1721 if (tracing_disabled
|| tracing_selftest_running
)
1724 /* Don't pollute graph traces with trace_vprintk internals */
1725 pause_graph_tracing();
1727 pc
= preempt_count();
1728 preempt_disable_notrace();
1731 tbuffer
= get_trace_buf();
1737 len
= vsnprintf(tbuffer
, TRACE_BUF_SIZE
, fmt
, args
);
1738 if (len
> TRACE_BUF_SIZE
)
1741 local_save_flags(flags
);
1742 size
= sizeof(*entry
) + len
+ 1;
1743 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
1747 entry
= ring_buffer_event_data(event
);
1750 memcpy(&entry
->buf
, tbuffer
, len
);
1751 entry
->buf
[len
] = '\0';
1752 if (!filter_check_discard(call
, entry
, buffer
, event
)) {
1753 __buffer_unlock_commit(buffer
, event
);
1754 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1757 preempt_enable_notrace();
1758 unpause_graph_tracing();
1763 int trace_array_vprintk(struct trace_array
*tr
,
1764 unsigned long ip
, const char *fmt
, va_list args
)
1766 return __trace_array_vprintk(tr
->trace_buffer
.buffer
, ip
, fmt
, args
);
1769 int trace_array_printk(struct trace_array
*tr
,
1770 unsigned long ip
, const char *fmt
, ...)
1775 if (!(trace_flags
& TRACE_ITER_PRINTK
))
1779 ret
= trace_array_vprintk(tr
, ip
, fmt
, ap
);
1784 int trace_array_printk_buf(struct ring_buffer
*buffer
,
1785 unsigned long ip
, const char *fmt
, ...)
1790 if (!(trace_flags
& TRACE_ITER_PRINTK
))
1794 ret
= __trace_array_vprintk(buffer
, ip
, fmt
, ap
);
1799 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1801 return trace_array_vprintk(&global_trace
, ip
, fmt
, args
);
1803 EXPORT_SYMBOL_GPL(trace_vprintk
);
1805 static void trace_iterator_increment(struct trace_iterator
*iter
)
1807 struct ring_buffer_iter
*buf_iter
= trace_buffer_iter(iter
, iter
->cpu
);
1811 ring_buffer_read(buf_iter
, NULL
);
1814 static struct trace_entry
*
1815 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
,
1816 unsigned long *lost_events
)
1818 struct ring_buffer_event
*event
;
1819 struct ring_buffer_iter
*buf_iter
= trace_buffer_iter(iter
, cpu
);
1822 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1824 event
= ring_buffer_peek(iter
->trace_buffer
->buffer
, cpu
, ts
,
1828 iter
->ent_size
= ring_buffer_event_length(event
);
1829 return ring_buffer_event_data(event
);
1835 static struct trace_entry
*
1836 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
,
1837 unsigned long *missing_events
, u64
*ent_ts
)
1839 struct ring_buffer
*buffer
= iter
->trace_buffer
->buffer
;
1840 struct trace_entry
*ent
, *next
= NULL
;
1841 unsigned long lost_events
= 0, next_lost
= 0;
1842 int cpu_file
= iter
->cpu_file
;
1843 u64 next_ts
= 0, ts
;
1849 * If we are in a per_cpu trace file, don't bother by iterating over
1850 * all cpu and peek directly.
1852 if (cpu_file
> RING_BUFFER_ALL_CPUS
) {
1853 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1855 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
, missing_events
);
1857 *ent_cpu
= cpu_file
;
1862 for_each_tracing_cpu(cpu
) {
1864 if (ring_buffer_empty_cpu(buffer
, cpu
))
1867 ent
= peek_next_entry(iter
, cpu
, &ts
, &lost_events
);
1870 * Pick the entry with the smallest timestamp:
1872 if (ent
&& (!next
|| ts
< next_ts
)) {
1876 next_lost
= lost_events
;
1877 next_size
= iter
->ent_size
;
1881 iter
->ent_size
= next_size
;
1884 *ent_cpu
= next_cpu
;
1890 *missing_events
= next_lost
;
1895 /* Find the next real entry, without updating the iterator itself */
1896 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1897 int *ent_cpu
, u64
*ent_ts
)
1899 return __find_next_entry(iter
, ent_cpu
, NULL
, ent_ts
);
1902 /* Find the next real entry, and increment the iterator to the next entry */
1903 void *trace_find_next_entry_inc(struct trace_iterator
*iter
)
1905 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
,
1906 &iter
->lost_events
, &iter
->ts
);
1909 trace_iterator_increment(iter
);
1911 return iter
->ent
? iter
: NULL
;
1914 static void trace_consume(struct trace_iterator
*iter
)
1916 ring_buffer_consume(iter
->trace_buffer
->buffer
, iter
->cpu
, &iter
->ts
,
1917 &iter
->lost_events
);
1920 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1922 struct trace_iterator
*iter
= m
->private;
1926 WARN_ON_ONCE(iter
->leftover
);
1930 /* can't go backwards */
1935 ent
= trace_find_next_entry_inc(iter
);
1939 while (ent
&& iter
->idx
< i
)
1940 ent
= trace_find_next_entry_inc(iter
);
1947 void tracing_iter_reset(struct trace_iterator
*iter
, int cpu
)
1949 struct ring_buffer_event
*event
;
1950 struct ring_buffer_iter
*buf_iter
;
1951 unsigned long entries
= 0;
1954 per_cpu_ptr(iter
->trace_buffer
->data
, cpu
)->skipped_entries
= 0;
1956 buf_iter
= trace_buffer_iter(iter
, cpu
);
1960 ring_buffer_iter_reset(buf_iter
);
1963 * We could have the case with the max latency tracers
1964 * that a reset never took place on a cpu. This is evident
1965 * by the timestamp being before the start of the buffer.
1967 while ((event
= ring_buffer_iter_peek(buf_iter
, &ts
))) {
1968 if (ts
>= iter
->trace_buffer
->time_start
)
1971 ring_buffer_read(buf_iter
, NULL
);
1974 per_cpu_ptr(iter
->trace_buffer
->data
, cpu
)->skipped_entries
= entries
;
1978 * The current tracer is copied to avoid a global locking
1981 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1983 struct trace_iterator
*iter
= m
->private;
1984 struct trace_array
*tr
= iter
->tr
;
1985 int cpu_file
= iter
->cpu_file
;
1991 * copy the tracer to avoid using a global lock all around.
1992 * iter->trace is a copy of current_trace, the pointer to the
1993 * name may be used instead of a strcmp(), as iter->trace->name
1994 * will point to the same string as current_trace->name.
1996 mutex_lock(&trace_types_lock
);
1997 if (unlikely(tr
->current_trace
&& iter
->trace
->name
!= tr
->current_trace
->name
))
1998 *iter
->trace
= *tr
->current_trace
;
1999 mutex_unlock(&trace_types_lock
);
2001 #ifdef CONFIG_TRACER_MAX_TRACE
2002 if (iter
->snapshot
&& iter
->trace
->use_max_tr
)
2003 return ERR_PTR(-EBUSY
);
2006 if (!iter
->snapshot
)
2007 atomic_inc(&trace_record_cmdline_disabled
);
2009 if (*pos
!= iter
->pos
) {
2014 if (cpu_file
== RING_BUFFER_ALL_CPUS
) {
2015 for_each_tracing_cpu(cpu
)
2016 tracing_iter_reset(iter
, cpu
);
2018 tracing_iter_reset(iter
, cpu_file
);
2021 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
2026 * If we overflowed the seq_file before, then we want
2027 * to just reuse the trace_seq buffer again.
2033 p
= s_next(m
, p
, &l
);
2037 trace_event_read_lock();
2038 trace_access_lock(cpu_file
);
2042 static void s_stop(struct seq_file
*m
, void *p
)
2044 struct trace_iterator
*iter
= m
->private;
2046 #ifdef CONFIG_TRACER_MAX_TRACE
2047 if (iter
->snapshot
&& iter
->trace
->use_max_tr
)
2051 if (!iter
->snapshot
)
2052 atomic_dec(&trace_record_cmdline_disabled
);
2054 trace_access_unlock(iter
->cpu_file
);
2055 trace_event_read_unlock();
2059 get_total_entries(struct trace_buffer
*buf
,
2060 unsigned long *total
, unsigned long *entries
)
2062 unsigned long count
;
2068 for_each_tracing_cpu(cpu
) {
2069 count
= ring_buffer_entries_cpu(buf
->buffer
, cpu
);
2071 * If this buffer has skipped entries, then we hold all
2072 * entries for the trace and we need to ignore the
2073 * ones before the time stamp.
2075 if (per_cpu_ptr(buf
->data
, cpu
)->skipped_entries
) {
2076 count
-= per_cpu_ptr(buf
->data
, cpu
)->skipped_entries
;
2077 /* total is the same as the entries */
2081 ring_buffer_overrun_cpu(buf
->buffer
, cpu
);
2086 static void print_lat_help_header(struct seq_file
*m
)
2088 seq_puts(m
, "# _------=> CPU# \n");
2089 seq_puts(m
, "# / _-----=> irqs-off \n");
2090 seq_puts(m
, "# | / _----=> need-resched \n");
2091 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
2092 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
2093 seq_puts(m
, "# |||| / delay \n");
2094 seq_puts(m
, "# cmd pid ||||| time | caller \n");
2095 seq_puts(m
, "# \\ / ||||| \\ | / \n");
2098 static void print_event_info(struct trace_buffer
*buf
, struct seq_file
*m
)
2100 unsigned long total
;
2101 unsigned long entries
;
2103 get_total_entries(buf
, &total
, &entries
);
2104 seq_printf(m
, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2105 entries
, total
, num_online_cpus());
2109 static void print_func_help_header(struct trace_buffer
*buf
, struct seq_file
*m
)
2111 print_event_info(buf
, m
);
2112 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2113 seq_puts(m
, "# | | | | |\n");
2116 static void print_func_help_header_irq(struct trace_buffer
*buf
, struct seq_file
*m
)
2118 print_event_info(buf
, m
);
2119 seq_puts(m
, "# _-----=> irqs-off\n");
2120 seq_puts(m
, "# / _----=> need-resched\n");
2121 seq_puts(m
, "# | / _---=> hardirq/softirq\n");
2122 seq_puts(m
, "# || / _--=> preempt-depth\n");
2123 seq_puts(m
, "# ||| / delay\n");
2124 seq_puts(m
, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2125 seq_puts(m
, "# | | | |||| | |\n");
2129 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
2131 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
2132 struct trace_buffer
*buf
= iter
->trace_buffer
;
2133 struct trace_array_cpu
*data
= per_cpu_ptr(buf
->data
, buf
->cpu
);
2134 struct tracer
*type
= iter
->trace
;
2135 unsigned long entries
;
2136 unsigned long total
;
2137 const char *name
= "preemption";
2141 get_total_entries(buf
, &total
, &entries
);
2143 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
2145 seq_puts(m
, "# -----------------------------------"
2146 "---------------------------------\n");
2147 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2148 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2149 nsecs_to_usecs(data
->saved_latency
),
2153 #if defined(CONFIG_PREEMPT_NONE)
2155 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2157 #elif defined(CONFIG_PREEMPT)
2162 /* These are reserved for later use */
2165 seq_printf(m
, " #P:%d)\n", num_online_cpus());
2169 seq_puts(m
, "# -----------------\n");
2170 seq_printf(m
, "# | task: %.16s-%d "
2171 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2172 data
->comm
, data
->pid
,
2173 from_kuid_munged(seq_user_ns(m
), data
->uid
), data
->nice
,
2174 data
->policy
, data
->rt_priority
);
2175 seq_puts(m
, "# -----------------\n");
2177 if (data
->critical_start
) {
2178 seq_puts(m
, "# => started at: ");
2179 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
2180 trace_print_seq(m
, &iter
->seq
);
2181 seq_puts(m
, "\n# => ended at: ");
2182 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
2183 trace_print_seq(m
, &iter
->seq
);
2184 seq_puts(m
, "\n#\n");
2190 static void test_cpu_buff_start(struct trace_iterator
*iter
)
2192 struct trace_seq
*s
= &iter
->seq
;
2194 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
2197 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
2200 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
2203 if (per_cpu_ptr(iter
->trace_buffer
->data
, iter
->cpu
)->skipped_entries
)
2206 cpumask_set_cpu(iter
->cpu
, iter
->started
);
2208 /* Don't print started cpu buffer for the first entry of the trace */
2210 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
2214 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
2216 struct trace_seq
*s
= &iter
->seq
;
2217 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
2218 struct trace_entry
*entry
;
2219 struct trace_event
*event
;
2223 test_cpu_buff_start(iter
);
2225 event
= ftrace_find_event(entry
->type
);
2227 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2228 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
2229 if (!trace_print_lat_context(iter
))
2232 if (!trace_print_context(iter
))
2238 return event
->funcs
->trace(iter
, sym_flags
, event
);
2240 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
2243 return TRACE_TYPE_HANDLED
;
2245 return TRACE_TYPE_PARTIAL_LINE
;
2248 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
2250 struct trace_seq
*s
= &iter
->seq
;
2251 struct trace_entry
*entry
;
2252 struct trace_event
*event
;
2256 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2257 if (!trace_seq_printf(s
, "%d %d %llu ",
2258 entry
->pid
, iter
->cpu
, iter
->ts
))
2262 event
= ftrace_find_event(entry
->type
);
2264 return event
->funcs
->raw(iter
, 0, event
);
2266 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
2269 return TRACE_TYPE_HANDLED
;
2271 return TRACE_TYPE_PARTIAL_LINE
;
2274 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
2276 struct trace_seq
*s
= &iter
->seq
;
2277 unsigned char newline
= '\n';
2278 struct trace_entry
*entry
;
2279 struct trace_event
*event
;
2283 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2284 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
2285 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
2286 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
2289 event
= ftrace_find_event(entry
->type
);
2291 enum print_line_t ret
= event
->funcs
->hex(iter
, 0, event
);
2292 if (ret
!= TRACE_TYPE_HANDLED
)
2296 SEQ_PUT_FIELD_RET(s
, newline
);
2298 return TRACE_TYPE_HANDLED
;
2301 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
2303 struct trace_seq
*s
= &iter
->seq
;
2304 struct trace_entry
*entry
;
2305 struct trace_event
*event
;
2309 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2310 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
2311 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
2312 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
2315 event
= ftrace_find_event(entry
->type
);
2316 return event
? event
->funcs
->binary(iter
, 0, event
) :
2320 int trace_empty(struct trace_iterator
*iter
)
2322 struct ring_buffer_iter
*buf_iter
;
2325 /* If we are looking at one CPU buffer, only check that one */
2326 if (iter
->cpu_file
!= RING_BUFFER_ALL_CPUS
) {
2327 cpu
= iter
->cpu_file
;
2328 buf_iter
= trace_buffer_iter(iter
, cpu
);
2330 if (!ring_buffer_iter_empty(buf_iter
))
2333 if (!ring_buffer_empty_cpu(iter
->trace_buffer
->buffer
, cpu
))
2339 for_each_tracing_cpu(cpu
) {
2340 buf_iter
= trace_buffer_iter(iter
, cpu
);
2342 if (!ring_buffer_iter_empty(buf_iter
))
2345 if (!ring_buffer_empty_cpu(iter
->trace_buffer
->buffer
, cpu
))
2353 /* Called with trace_event_read_lock() held. */
2354 enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
2356 enum print_line_t ret
;
2358 if (iter
->lost_events
&&
2359 !trace_seq_printf(&iter
->seq
, "CPU:%d [LOST %lu EVENTS]\n",
2360 iter
->cpu
, iter
->lost_events
))
2361 return TRACE_TYPE_PARTIAL_LINE
;
2363 if (iter
->trace
&& iter
->trace
->print_line
) {
2364 ret
= iter
->trace
->print_line(iter
);
2365 if (ret
!= TRACE_TYPE_UNHANDLED
)
2369 if (iter
->ent
->type
== TRACE_BPRINT
&&
2370 trace_flags
& TRACE_ITER_PRINTK
&&
2371 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2372 return trace_print_bprintk_msg_only(iter
);
2374 if (iter
->ent
->type
== TRACE_PRINT
&&
2375 trace_flags
& TRACE_ITER_PRINTK
&&
2376 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2377 return trace_print_printk_msg_only(iter
);
2379 if (trace_flags
& TRACE_ITER_BIN
)
2380 return print_bin_fmt(iter
);
2382 if (trace_flags
& TRACE_ITER_HEX
)
2383 return print_hex_fmt(iter
);
2385 if (trace_flags
& TRACE_ITER_RAW
)
2386 return print_raw_fmt(iter
);
2388 return print_trace_fmt(iter
);
2391 void trace_latency_header(struct seq_file
*m
)
2393 struct trace_iterator
*iter
= m
->private;
2395 /* print nothing if the buffers are empty */
2396 if (trace_empty(iter
))
2399 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
2400 print_trace_header(m
, iter
);
2402 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2403 print_lat_help_header(m
);
2406 void trace_default_header(struct seq_file
*m
)
2408 struct trace_iterator
*iter
= m
->private;
2410 if (!(trace_flags
& TRACE_ITER_CONTEXT_INFO
))
2413 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
2414 /* print nothing if the buffers are empty */
2415 if (trace_empty(iter
))
2417 print_trace_header(m
, iter
);
2418 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2419 print_lat_help_header(m
);
2421 if (!(trace_flags
& TRACE_ITER_VERBOSE
)) {
2422 if (trace_flags
& TRACE_ITER_IRQ_INFO
)
2423 print_func_help_header_irq(iter
->trace_buffer
, m
);
2425 print_func_help_header(iter
->trace_buffer
, m
);
2430 static void test_ftrace_alive(struct seq_file
*m
)
2432 if (!ftrace_is_dead())
2434 seq_printf(m
, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2435 seq_printf(m
, "# MAY BE MISSING FUNCTION EVENTS\n");
2438 #ifdef CONFIG_TRACER_MAX_TRACE
2439 static void show_snapshot_main_help(struct seq_file
*m
)
2441 seq_printf(m
, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2442 seq_printf(m
, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2443 seq_printf(m
, "# Takes a snapshot of the main buffer.\n");
2444 seq_printf(m
, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2445 seq_printf(m
, "# (Doesn't have to be '2' works with any number that\n");
2446 seq_printf(m
, "# is not a '0' or '1')\n");
2449 static void show_snapshot_percpu_help(struct seq_file
*m
)
2451 seq_printf(m
, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2452 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2453 seq_printf(m
, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2454 seq_printf(m
, "# Takes a snapshot of the main buffer for this cpu.\n");
2456 seq_printf(m
, "# echo 1 > snapshot : Not supported with this kernel.\n");
2457 seq_printf(m
, "# Must use main snapshot file to allocate.\n");
2459 seq_printf(m
, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2460 seq_printf(m
, "# (Doesn't have to be '2' works with any number that\n");
2461 seq_printf(m
, "# is not a '0' or '1')\n");
2464 static void print_snapshot_help(struct seq_file
*m
, struct trace_iterator
*iter
)
2466 if (iter
->trace
->allocated_snapshot
)
2467 seq_printf(m
, "#\n# * Snapshot is allocated *\n#\n");
2469 seq_printf(m
, "#\n# * Snapshot is freed *\n#\n");
2471 seq_printf(m
, "# Snapshot commands:\n");
2472 if (iter
->cpu_file
== RING_BUFFER_ALL_CPUS
)
2473 show_snapshot_main_help(m
);
2475 show_snapshot_percpu_help(m
);
2478 /* Should never be called */
2479 static inline void print_snapshot_help(struct seq_file
*m
, struct trace_iterator
*iter
) { }
2482 static int s_show(struct seq_file
*m
, void *v
)
2484 struct trace_iterator
*iter
= v
;
2487 if (iter
->ent
== NULL
) {
2489 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
2491 test_ftrace_alive(m
);
2493 if (iter
->snapshot
&& trace_empty(iter
))
2494 print_snapshot_help(m
, iter
);
2495 else if (iter
->trace
&& iter
->trace
->print_header
)
2496 iter
->trace
->print_header(m
);
2498 trace_default_header(m
);
2500 } else if (iter
->leftover
) {
2502 * If we filled the seq_file buffer earlier, we
2503 * want to just show it now.
2505 ret
= trace_print_seq(m
, &iter
->seq
);
2507 /* ret should this time be zero, but you never know */
2508 iter
->leftover
= ret
;
2511 print_trace_line(iter
);
2512 ret
= trace_print_seq(m
, &iter
->seq
);
2514 * If we overflow the seq_file buffer, then it will
2515 * ask us for this data again at start up.
2517 * ret is 0 if seq_file write succeeded.
2520 iter
->leftover
= ret
;
2526 static const struct seq_operations tracer_seq_ops
= {
2533 static struct trace_iterator
*
2534 __tracing_open(struct inode
*inode
, struct file
*file
, bool snapshot
)
2536 struct trace_cpu
*tc
= inode
->i_private
;
2537 struct trace_array
*tr
= tc
->tr
;
2538 struct trace_iterator
*iter
;
2541 if (tracing_disabled
)
2542 return ERR_PTR(-ENODEV
);
2544 iter
= __seq_open_private(file
, &tracer_seq_ops
, sizeof(*iter
));
2546 return ERR_PTR(-ENOMEM
);
2548 iter
->buffer_iter
= kzalloc(sizeof(*iter
->buffer_iter
) * num_possible_cpus(),
2550 if (!iter
->buffer_iter
)
2554 * We make a copy of the current tracer to avoid concurrent
2555 * changes on it while we are reading.
2557 mutex_lock(&trace_types_lock
);
2558 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2562 *iter
->trace
= *tr
->current_trace
;
2564 if (!zalloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
2569 #ifdef CONFIG_TRACER_MAX_TRACE
2570 /* Currently only the top directory has a snapshot */
2571 if (tr
->current_trace
->print_max
|| snapshot
)
2572 iter
->trace_buffer
= &tr
->max_buffer
;
2575 iter
->trace_buffer
= &tr
->trace_buffer
;
2576 iter
->snapshot
= snapshot
;
2578 mutex_init(&iter
->mutex
);
2579 iter
->cpu_file
= tc
->cpu
;
2581 /* Notify the tracer early; before we stop tracing. */
2582 if (iter
->trace
&& iter
->trace
->open
)
2583 iter
->trace
->open(iter
);
2585 /* Annotate start of buffers if we had overruns */
2586 if (ring_buffer_overruns(iter
->trace_buffer
->buffer
))
2587 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
2589 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2590 if (trace_clocks
[trace_clock_id
].in_ns
)
2591 iter
->iter_flags
|= TRACE_FILE_TIME_IN_NS
;
2593 /* stop the trace while dumping if we are not opening "snapshot" */
2594 if (!iter
->snapshot
)
2595 tracing_stop_tr(tr
);
2597 if (iter
->cpu_file
== RING_BUFFER_ALL_CPUS
) {
2598 for_each_tracing_cpu(cpu
) {
2599 iter
->buffer_iter
[cpu
] =
2600 ring_buffer_read_prepare(iter
->trace_buffer
->buffer
, cpu
);
2602 ring_buffer_read_prepare_sync();
2603 for_each_tracing_cpu(cpu
) {
2604 ring_buffer_read_start(iter
->buffer_iter
[cpu
]);
2605 tracing_iter_reset(iter
, cpu
);
2608 cpu
= iter
->cpu_file
;
2609 iter
->buffer_iter
[cpu
] =
2610 ring_buffer_read_prepare(iter
->trace_buffer
->buffer
, cpu
);
2611 ring_buffer_read_prepare_sync();
2612 ring_buffer_read_start(iter
->buffer_iter
[cpu
]);
2613 tracing_iter_reset(iter
, cpu
);
2616 mutex_unlock(&trace_types_lock
);
2621 mutex_unlock(&trace_types_lock
);
2623 kfree(iter
->buffer_iter
);
2625 seq_release_private(inode
, file
);
2626 return ERR_PTR(-ENOMEM
);
2629 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
2631 if (tracing_disabled
)
2634 filp
->private_data
= inode
->i_private
;
2638 static int tracing_release(struct inode
*inode
, struct file
*file
)
2640 struct seq_file
*m
= file
->private_data
;
2641 struct trace_iterator
*iter
;
2642 struct trace_array
*tr
;
2645 if (!(file
->f_mode
& FMODE_READ
))
2651 mutex_lock(&trace_types_lock
);
2652 for_each_tracing_cpu(cpu
) {
2653 if (iter
->buffer_iter
[cpu
])
2654 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2657 if (iter
->trace
&& iter
->trace
->close
)
2658 iter
->trace
->close(iter
);
2660 if (!iter
->snapshot
)
2661 /* reenable tracing if it was previously enabled */
2662 tracing_start_tr(tr
);
2663 mutex_unlock(&trace_types_lock
);
2665 mutex_destroy(&iter
->mutex
);
2666 free_cpumask_var(iter
->started
);
2668 kfree(iter
->buffer_iter
);
2669 seq_release_private(inode
, file
);
2673 static int tracing_open(struct inode
*inode
, struct file
*file
)
2675 struct trace_iterator
*iter
;
2678 /* If this file was open for write, then erase contents */
2679 if ((file
->f_mode
& FMODE_WRITE
) &&
2680 (file
->f_flags
& O_TRUNC
)) {
2681 struct trace_cpu
*tc
= inode
->i_private
;
2682 struct trace_array
*tr
= tc
->tr
;
2684 if (tc
->cpu
== RING_BUFFER_ALL_CPUS
)
2685 tracing_reset_online_cpus(&tr
->trace_buffer
);
2687 tracing_reset(&tr
->trace_buffer
, tc
->cpu
);
2690 if (file
->f_mode
& FMODE_READ
) {
2691 iter
= __tracing_open(inode
, file
, false);
2693 ret
= PTR_ERR(iter
);
2694 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2695 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2701 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2703 struct tracer
*t
= v
;
2713 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2718 mutex_lock(&trace_types_lock
);
2719 for (t
= trace_types
; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2725 static void t_stop(struct seq_file
*m
, void *p
)
2727 mutex_unlock(&trace_types_lock
);
2730 static int t_show(struct seq_file
*m
, void *v
)
2732 struct tracer
*t
= v
;
2737 seq_printf(m
, "%s", t
->name
);
2746 static const struct seq_operations show_traces_seq_ops
= {
2753 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2755 if (tracing_disabled
)
2758 return seq_open(file
, &show_traces_seq_ops
);
2762 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
2763 size_t count
, loff_t
*ppos
)
2768 static loff_t
tracing_seek(struct file
*file
, loff_t offset
, int origin
)
2770 if (file
->f_mode
& FMODE_READ
)
2771 return seq_lseek(file
, offset
, origin
);
2776 static const struct file_operations tracing_fops
= {
2777 .open
= tracing_open
,
2779 .write
= tracing_write_stub
,
2780 .llseek
= tracing_seek
,
2781 .release
= tracing_release
,
2784 static const struct file_operations show_traces_fops
= {
2785 .open
= show_traces_open
,
2787 .release
= seq_release
,
2788 .llseek
= seq_lseek
,
2792 * Only trace on a CPU if the bitmask is set:
2794 static cpumask_var_t tracing_cpumask
;
2797 * The tracer itself will not take this lock, but still we want
2798 * to provide a consistent cpumask to user-space:
2800 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2803 * Temporary storage for the character representation of the
2804 * CPU bitmask (and one more byte for the newline):
2806 static char mask_str
[NR_CPUS
+ 1];
2809 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2810 size_t count
, loff_t
*ppos
)
2814 mutex_lock(&tracing_cpumask_update_lock
);
2816 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2817 if (count
- len
< 2) {
2821 len
+= sprintf(mask_str
+ len
, "\n");
2822 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2825 mutex_unlock(&tracing_cpumask_update_lock
);
2831 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2832 size_t count
, loff_t
*ppos
)
2834 struct trace_array
*tr
= filp
->private_data
;
2835 cpumask_var_t tracing_cpumask_new
;
2838 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2841 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2845 mutex_lock(&tracing_cpumask_update_lock
);
2847 local_irq_disable();
2848 arch_spin_lock(&ftrace_max_lock
);
2849 for_each_tracing_cpu(cpu
) {
2851 * Increase/decrease the disabled counter if we are
2852 * about to flip a bit in the cpumask:
2854 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2855 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2856 atomic_inc(&per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->disabled
);
2857 ring_buffer_record_disable_cpu(tr
->trace_buffer
.buffer
, cpu
);
2859 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2860 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2861 atomic_dec(&per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->disabled
);
2862 ring_buffer_record_enable_cpu(tr
->trace_buffer
.buffer
, cpu
);
2865 arch_spin_unlock(&ftrace_max_lock
);
2868 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2870 mutex_unlock(&tracing_cpumask_update_lock
);
2871 free_cpumask_var(tracing_cpumask_new
);
2876 free_cpumask_var(tracing_cpumask_new
);
2881 static const struct file_operations tracing_cpumask_fops
= {
2882 .open
= tracing_open_generic
,
2883 .read
= tracing_cpumask_read
,
2884 .write
= tracing_cpumask_write
,
2885 .llseek
= generic_file_llseek
,
2888 static int tracing_trace_options_show(struct seq_file
*m
, void *v
)
2890 struct tracer_opt
*trace_opts
;
2891 struct trace_array
*tr
= m
->private;
2895 mutex_lock(&trace_types_lock
);
2896 tracer_flags
= tr
->current_trace
->flags
->val
;
2897 trace_opts
= tr
->current_trace
->flags
->opts
;
2899 for (i
= 0; trace_options
[i
]; i
++) {
2900 if (trace_flags
& (1 << i
))
2901 seq_printf(m
, "%s\n", trace_options
[i
]);
2903 seq_printf(m
, "no%s\n", trace_options
[i
]);
2906 for (i
= 0; trace_opts
[i
].name
; i
++) {
2907 if (tracer_flags
& trace_opts
[i
].bit
)
2908 seq_printf(m
, "%s\n", trace_opts
[i
].name
);
2910 seq_printf(m
, "no%s\n", trace_opts
[i
].name
);
2912 mutex_unlock(&trace_types_lock
);
2917 static int __set_tracer_option(struct tracer
*trace
,
2918 struct tracer_flags
*tracer_flags
,
2919 struct tracer_opt
*opts
, int neg
)
2923 ret
= trace
->set_flag(tracer_flags
->val
, opts
->bit
, !neg
);
2928 tracer_flags
->val
&= ~opts
->bit
;
2930 tracer_flags
->val
|= opts
->bit
;
2934 /* Try to assign a tracer specific option */
2935 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2937 struct tracer_flags
*tracer_flags
= trace
->flags
;
2938 struct tracer_opt
*opts
= NULL
;
2941 for (i
= 0; tracer_flags
->opts
[i
].name
; i
++) {
2942 opts
= &tracer_flags
->opts
[i
];
2944 if (strcmp(cmp
, opts
->name
) == 0)
2945 return __set_tracer_option(trace
, trace
->flags
,
2952 /* Some tracers require overwrite to stay enabled */
2953 int trace_keep_overwrite(struct tracer
*tracer
, u32 mask
, int set
)
2955 if (tracer
->enabled
&& (mask
& TRACE_ITER_OVERWRITE
) && !set
)
2961 int set_tracer_flag(struct trace_array
*tr
, unsigned int mask
, int enabled
)
2963 /* do nothing if flag is already set */
2964 if (!!(trace_flags
& mask
) == !!enabled
)
2967 /* Give the tracer a chance to approve the change */
2968 if (tr
->current_trace
->flag_changed
)
2969 if (tr
->current_trace
->flag_changed(tr
->current_trace
, mask
, !!enabled
))
2973 trace_flags
|= mask
;
2975 trace_flags
&= ~mask
;
2977 if (mask
== TRACE_ITER_RECORD_CMD
)
2978 trace_event_enable_cmd_record(enabled
);
2980 if (mask
== TRACE_ITER_OVERWRITE
) {
2981 ring_buffer_change_overwrite(tr
->trace_buffer
.buffer
, enabled
);
2982 #ifdef CONFIG_TRACER_MAX_TRACE
2983 ring_buffer_change_overwrite(tr
->max_buffer
.buffer
, enabled
);
2987 if (mask
== TRACE_ITER_PRINTK
)
2988 trace_printk_start_stop_comm(enabled
);
2993 static int trace_set_options(struct trace_array
*tr
, char *option
)
3000 cmp
= strstrip(option
);
3002 if (strncmp(cmp
, "no", 2) == 0) {
3007 mutex_lock(&trace_types_lock
);
3009 for (i
= 0; trace_options
[i
]; i
++) {
3010 if (strcmp(cmp
, trace_options
[i
]) == 0) {
3011 ret
= set_tracer_flag(tr
, 1 << i
, !neg
);
3016 /* If no option could be set, test the specific tracer options */
3017 if (!trace_options
[i
])
3018 ret
= set_tracer_option(tr
->current_trace
, cmp
, neg
);
3020 mutex_unlock(&trace_types_lock
);
3026 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
3027 size_t cnt
, loff_t
*ppos
)
3029 struct seq_file
*m
= filp
->private_data
;
3030 struct trace_array
*tr
= m
->private;
3034 if (cnt
>= sizeof(buf
))
3037 if (copy_from_user(&buf
, ubuf
, cnt
))
3042 ret
= trace_set_options(tr
, buf
);
3051 static int tracing_trace_options_open(struct inode
*inode
, struct file
*file
)
3053 if (tracing_disabled
)
3056 return single_open(file
, tracing_trace_options_show
, inode
->i_private
);
3059 static const struct file_operations tracing_iter_fops
= {
3060 .open
= tracing_trace_options_open
,
3062 .llseek
= seq_lseek
,
3063 .release
= single_release
,
3064 .write
= tracing_trace_options_write
,
3067 static const char readme_msg
[] =
3068 "tracing mini-HOWTO:\n\n"
3069 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
3070 "# cat /sys/kernel/debug/tracing/available_tracers\n"
3071 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
3072 "# cat /sys/kernel/debug/tracing/current_tracer\n"
3074 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
3075 "# cat /sys/kernel/debug/tracing/current_tracer\n"
3077 "# cat /sys/kernel/debug/tracing/trace_options\n"
3078 "noprint-parent nosym-offset nosym-addr noverbose\n"
3079 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
3080 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
3081 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
3082 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
3086 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
3087 size_t cnt
, loff_t
*ppos
)
3089 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
3090 readme_msg
, strlen(readme_msg
));
3093 static const struct file_operations tracing_readme_fops
= {
3094 .open
= tracing_open_generic
,
3095 .read
= tracing_readme_read
,
3096 .llseek
= generic_file_llseek
,
3100 tracing_saved_cmdlines_read(struct file
*file
, char __user
*ubuf
,
3101 size_t cnt
, loff_t
*ppos
)
3110 file_buf
= kmalloc(SAVED_CMDLINES
*(16+TASK_COMM_LEN
), GFP_KERNEL
);
3114 buf_comm
= kmalloc(TASK_COMM_LEN
, GFP_KERNEL
);
3122 for (i
= 0; i
< SAVED_CMDLINES
; i
++) {
3125 pid
= map_cmdline_to_pid
[i
];
3126 if (pid
== -1 || pid
== NO_CMDLINE_MAP
)
3129 trace_find_cmdline(pid
, buf_comm
);
3130 r
= sprintf(buf
, "%d %s\n", pid
, buf_comm
);
3135 len
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
3144 static const struct file_operations tracing_saved_cmdlines_fops
= {
3145 .open
= tracing_open_generic
,
3146 .read
= tracing_saved_cmdlines_read
,
3147 .llseek
= generic_file_llseek
,
3151 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
3152 size_t cnt
, loff_t
*ppos
)
3154 struct trace_array
*tr
= filp
->private_data
;
3155 char buf
[MAX_TRACER_SIZE
+2];
3158 mutex_lock(&trace_types_lock
);
3159 r
= sprintf(buf
, "%s\n", tr
->current_trace
->name
);
3160 mutex_unlock(&trace_types_lock
);
3162 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3165 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
3167 tracing_reset_online_cpus(&tr
->trace_buffer
);
3171 static void set_buffer_entries(struct trace_buffer
*buf
, unsigned long val
)
3174 for_each_tracing_cpu(cpu
)
3175 per_cpu_ptr(buf
->data
, cpu
)->entries
= val
;
3178 #ifdef CONFIG_TRACER_MAX_TRACE
3179 /* resize @tr's buffer to the size of @size_tr's entries */
3180 static int resize_buffer_duplicate_size(struct trace_buffer
*trace_buf
,
3181 struct trace_buffer
*size_buf
, int cpu_id
)
3185 if (cpu_id
== RING_BUFFER_ALL_CPUS
) {
3186 for_each_tracing_cpu(cpu
) {
3187 ret
= ring_buffer_resize(trace_buf
->buffer
,
3188 per_cpu_ptr(size_buf
->data
, cpu
)->entries
, cpu
);
3191 per_cpu_ptr(trace_buf
->data
, cpu
)->entries
=
3192 per_cpu_ptr(size_buf
->data
, cpu
)->entries
;
3195 ret
= ring_buffer_resize(trace_buf
->buffer
,
3196 per_cpu_ptr(size_buf
->data
, cpu_id
)->entries
, cpu_id
);
3198 per_cpu_ptr(trace_buf
->data
, cpu_id
)->entries
=
3199 per_cpu_ptr(size_buf
->data
, cpu_id
)->entries
;
3204 #endif /* CONFIG_TRACER_MAX_TRACE */
3206 static int __tracing_resize_ring_buffer(struct trace_array
*tr
,
3207 unsigned long size
, int cpu
)
3212 * If kernel or user changes the size of the ring buffer
3213 * we use the size that was given, and we can forget about
3214 * expanding it later.
3216 ring_buffer_expanded
= 1;
3218 /* May be called before buffers are initialized */
3219 if (!tr
->trace_buffer
.buffer
)
3222 ret
= ring_buffer_resize(tr
->trace_buffer
.buffer
, size
, cpu
);
3226 #ifdef CONFIG_TRACER_MAX_TRACE
3227 if (!(tr
->flags
& TRACE_ARRAY_FL_GLOBAL
) ||
3228 !tr
->current_trace
->use_max_tr
)
3231 ret
= ring_buffer_resize(tr
->max_buffer
.buffer
, size
, cpu
);
3233 int r
= resize_buffer_duplicate_size(&tr
->trace_buffer
,
3234 &tr
->trace_buffer
, cpu
);
3237 * AARGH! We are left with different
3238 * size max buffer!!!!
3239 * The max buffer is our "snapshot" buffer.
3240 * When a tracer needs a snapshot (one of the
3241 * latency tracers), it swaps the max buffer
3242 * with the saved snap shot. We succeeded to
3243 * update the size of the main buffer, but failed to
3244 * update the size of the max buffer. But when we tried
3245 * to reset the main buffer to the original size, we
3246 * failed there too. This is very unlikely to
3247 * happen, but if it does, warn and kill all
3251 tracing_disabled
= 1;
3256 if (cpu
== RING_BUFFER_ALL_CPUS
)
3257 set_buffer_entries(&tr
->max_buffer
, size
);
3259 per_cpu_ptr(tr
->max_buffer
.data
, cpu
)->entries
= size
;
3262 #endif /* CONFIG_TRACER_MAX_TRACE */
3264 if (cpu
== RING_BUFFER_ALL_CPUS
)
3265 set_buffer_entries(&tr
->trace_buffer
, size
);
3267 per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->entries
= size
;
3272 static ssize_t
tracing_resize_ring_buffer(struct trace_array
*tr
,
3273 unsigned long size
, int cpu_id
)
3277 mutex_lock(&trace_types_lock
);
3279 if (cpu_id
!= RING_BUFFER_ALL_CPUS
) {
3280 /* make sure, this cpu is enabled in the mask */
3281 if (!cpumask_test_cpu(cpu_id
, tracing_buffer_mask
)) {
3287 ret
= __tracing_resize_ring_buffer(tr
, size
, cpu_id
);
3292 mutex_unlock(&trace_types_lock
);
3299 * tracing_update_buffers - used by tracing facility to expand ring buffers
3301 * To save on memory when the tracing is never used on a system with it
3302 * configured in. The ring buffers are set to a minimum size. But once
3303 * a user starts to use the tracing facility, then they need to grow
3304 * to their default size.
3306 * This function is to be called when a tracer is about to be used.
3308 int tracing_update_buffers(void)
3312 mutex_lock(&trace_types_lock
);
3313 if (!ring_buffer_expanded
)
3314 ret
= __tracing_resize_ring_buffer(&global_trace
, trace_buf_size
,
3315 RING_BUFFER_ALL_CPUS
);
3316 mutex_unlock(&trace_types_lock
);
3321 struct trace_option_dentry
;
3323 static struct trace_option_dentry
*
3324 create_trace_option_files(struct trace_array
*tr
, struct tracer
*tracer
);
3327 destroy_trace_option_files(struct trace_option_dentry
*topts
);
3329 static int tracing_set_tracer(const char *buf
)
3331 static struct trace_option_dentry
*topts
;
3332 struct trace_array
*tr
= &global_trace
;
3334 #ifdef CONFIG_TRACER_MAX_TRACE
3339 mutex_lock(&trace_types_lock
);
3341 if (!ring_buffer_expanded
) {
3342 ret
= __tracing_resize_ring_buffer(tr
, trace_buf_size
,
3343 RING_BUFFER_ALL_CPUS
);
3349 for (t
= trace_types
; t
; t
= t
->next
) {
3350 if (strcmp(t
->name
, buf
) == 0)
3357 if (t
== tr
->current_trace
)
3360 trace_branch_disable();
3362 tr
->current_trace
->enabled
= false;
3364 if (tr
->current_trace
->reset
)
3365 tr
->current_trace
->reset(tr
);
3367 #ifdef CONFIG_TRACER_MAX_TRACE
3368 had_max_tr
= tr
->current_trace
->allocated_snapshot
;
3370 /* Current trace needs to be nop_trace before synchronize_sched */
3371 tr
->current_trace
= &nop_trace
;
3373 if (had_max_tr
&& !t
->use_max_tr
) {
3375 * We need to make sure that the update_max_tr sees that
3376 * current_trace changed to nop_trace to keep it from
3377 * swapping the buffers after we resize it.
3378 * The update_max_tr is called from interrupts disabled
3379 * so a synchronized_sched() is sufficient.
3381 synchronize_sched();
3383 * We don't free the ring buffer. instead, resize it because
3384 * The max_tr ring buffer has some state (e.g. ring->clock) and
3385 * we want preserve it.
3387 ring_buffer_resize(tr
->max_buffer
.buffer
, 1, RING_BUFFER_ALL_CPUS
);
3388 set_buffer_entries(&tr
->max_buffer
, 1);
3389 tracing_reset_online_cpus(&tr
->max_buffer
);
3390 tr
->current_trace
->allocated_snapshot
= false;
3393 tr
->current_trace
= &nop_trace
;
3395 destroy_trace_option_files(topts
);
3397 topts
= create_trace_option_files(tr
, t
);
3399 #ifdef CONFIG_TRACER_MAX_TRACE
3400 if (t
->use_max_tr
&& !had_max_tr
) {
3401 /* we need to make per cpu buffer sizes equivalent */
3402 ret
= resize_buffer_duplicate_size(&tr
->max_buffer
, &tr
->trace_buffer
,
3403 RING_BUFFER_ALL_CPUS
);
3406 t
->allocated_snapshot
= true;
3411 ret
= tracer_init(t
, tr
);
3416 tr
->current_trace
= t
;
3417 tr
->current_trace
->enabled
= true;
3418 trace_branch_enable(tr
);
3420 mutex_unlock(&trace_types_lock
);
3426 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
3427 size_t cnt
, loff_t
*ppos
)
3429 char buf
[MAX_TRACER_SIZE
+1];
3436 if (cnt
> MAX_TRACER_SIZE
)
3437 cnt
= MAX_TRACER_SIZE
;
3439 if (copy_from_user(&buf
, ubuf
, cnt
))
3444 /* strip ending whitespace. */
3445 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
3448 err
= tracing_set_tracer(buf
);
3458 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
3459 size_t cnt
, loff_t
*ppos
)
3461 unsigned long *ptr
= filp
->private_data
;
3465 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
3466 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
3467 if (r
> sizeof(buf
))
3469 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3473 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
3474 size_t cnt
, loff_t
*ppos
)
3476 unsigned long *ptr
= filp
->private_data
;
3480 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
3489 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
3491 struct trace_cpu
*tc
= inode
->i_private
;
3492 struct trace_array
*tr
= tc
->tr
;
3493 struct trace_iterator
*iter
;
3496 if (tracing_disabled
)
3499 mutex_lock(&trace_types_lock
);
3501 /* create a buffer to store the information to pass to userspace */
3502 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
3509 * We make a copy of the current tracer to avoid concurrent
3510 * changes on it while we are reading.
3512 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
3517 *iter
->trace
= *tr
->current_trace
;
3519 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
3524 /* trace pipe does not show start of buffer */
3525 cpumask_setall(iter
->started
);
3527 if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
3528 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
3530 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3531 if (trace_clocks
[trace_clock_id
].in_ns
)
3532 iter
->iter_flags
|= TRACE_FILE_TIME_IN_NS
;
3534 iter
->cpu_file
= tc
->cpu
;
3536 iter
->trace_buffer
= &tc
->tr
->trace_buffer
;
3537 mutex_init(&iter
->mutex
);
3538 filp
->private_data
= iter
;
3540 if (iter
->trace
->pipe_open
)
3541 iter
->trace
->pipe_open(iter
);
3543 nonseekable_open(inode
, filp
);
3545 mutex_unlock(&trace_types_lock
);
3551 mutex_unlock(&trace_types_lock
);
3555 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
3557 struct trace_iterator
*iter
= file
->private_data
;
3559 mutex_lock(&trace_types_lock
);
3561 if (iter
->trace
->pipe_close
)
3562 iter
->trace
->pipe_close(iter
);
3564 mutex_unlock(&trace_types_lock
);
3566 free_cpumask_var(iter
->started
);
3567 mutex_destroy(&iter
->mutex
);
3575 trace_poll(struct trace_iterator
*iter
, struct file
*filp
, poll_table
*poll_table
)
3577 /* Iterators are static, they should be filled or empty */
3578 if (trace_buffer_iter(iter
, iter
->cpu_file
))
3579 return POLLIN
| POLLRDNORM
;
3581 if (trace_flags
& TRACE_ITER_BLOCK
)
3583 * Always select as readable when in blocking mode
3585 return POLLIN
| POLLRDNORM
;
3587 return ring_buffer_poll_wait(iter
->trace_buffer
->buffer
, iter
->cpu_file
,
3592 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
3594 struct trace_iterator
*iter
= filp
->private_data
;
3596 return trace_poll(iter
, filp
, poll_table
);
3600 * This is a make-shift waitqueue.
3601 * A tracer might use this callback on some rare cases:
3603 * 1) the current tracer might hold the runqueue lock when it wakes up
3604 * a reader, hence a deadlock (sched, function, and function graph tracers)
3605 * 2) the function tracers, trace all functions, we don't want
3606 * the overhead of calling wake_up and friends
3607 * (and tracing them too)
3609 * Anyway, this is really very primitive wakeup.
3611 void poll_wait_pipe(struct trace_iterator
*iter
)
3613 set_current_state(TASK_INTERRUPTIBLE
);
3614 /* sleep for 100 msecs, and try again. */
3615 schedule_timeout(HZ
/ 10);
3618 /* Must be called with trace_types_lock mutex held. */
3619 static int tracing_wait_pipe(struct file
*filp
)
3621 struct trace_iterator
*iter
= filp
->private_data
;
3623 while (trace_empty(iter
)) {
3625 if ((filp
->f_flags
& O_NONBLOCK
)) {
3629 mutex_unlock(&iter
->mutex
);
3631 iter
->trace
->wait_pipe(iter
);
3633 mutex_lock(&iter
->mutex
);
3635 if (signal_pending(current
))
3639 * We block until we read something and tracing is disabled.
3640 * We still block if tracing is disabled, but we have never
3641 * read anything. This allows a user to cat this file, and
3642 * then enable tracing. But after we have read something,
3643 * we give an EOF when tracing is again disabled.
3645 * iter->pos will be 0 if we haven't read anything.
3647 if (!tracing_is_enabled() && iter
->pos
)
3658 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
3659 size_t cnt
, loff_t
*ppos
)
3661 struct trace_iterator
*iter
= filp
->private_data
;
3662 struct trace_array
*tr
= iter
->tr
;
3665 /* return any leftover data */
3666 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3670 trace_seq_init(&iter
->seq
);
3672 /* copy the tracer to avoid using a global lock all around */
3673 mutex_lock(&trace_types_lock
);
3674 if (unlikely(iter
->trace
->name
!= tr
->current_trace
->name
))
3675 *iter
->trace
= *tr
->current_trace
;
3676 mutex_unlock(&trace_types_lock
);
3679 * Avoid more than one consumer on a single file descriptor
3680 * This is just a matter of traces coherency, the ring buffer itself
3683 mutex_lock(&iter
->mutex
);
3684 if (iter
->trace
->read
) {
3685 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
3691 sret
= tracing_wait_pipe(filp
);
3695 /* stop when tracing is finished */
3696 if (trace_empty(iter
)) {
3701 if (cnt
>= PAGE_SIZE
)
3702 cnt
= PAGE_SIZE
- 1;
3704 /* reset all but tr, trace, and overruns */
3705 memset(&iter
->seq
, 0,
3706 sizeof(struct trace_iterator
) -
3707 offsetof(struct trace_iterator
, seq
));
3710 trace_event_read_lock();
3711 trace_access_lock(iter
->cpu_file
);
3712 while (trace_find_next_entry_inc(iter
) != NULL
) {
3713 enum print_line_t ret
;
3714 int len
= iter
->seq
.len
;
3716 ret
= print_trace_line(iter
);
3717 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3718 /* don't print partial lines */
3719 iter
->seq
.len
= len
;
3722 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3723 trace_consume(iter
);
3725 if (iter
->seq
.len
>= cnt
)
3729 * Setting the full flag means we reached the trace_seq buffer
3730 * size and we should leave by partial output condition above.
3731 * One of the trace_seq_* functions is not used properly.
3733 WARN_ONCE(iter
->seq
.full
, "full flag set for trace type %d",
3736 trace_access_unlock(iter
->cpu_file
);
3737 trace_event_read_unlock();
3739 /* Now copy what we have to the user */
3740 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3741 if (iter
->seq
.readpos
>= iter
->seq
.len
)
3742 trace_seq_init(&iter
->seq
);
3745 * If there was nothing to send to user, in spite of consuming trace
3746 * entries, go back to wait for more entries.
3752 mutex_unlock(&iter
->mutex
);
3757 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
3758 struct pipe_buffer
*buf
)
3760 __free_page(buf
->page
);
3763 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
3766 __free_page(spd
->pages
[idx
]);
3769 static const struct pipe_buf_operations tracing_pipe_buf_ops
= {
3771 .map
= generic_pipe_buf_map
,
3772 .unmap
= generic_pipe_buf_unmap
,
3773 .confirm
= generic_pipe_buf_confirm
,
3774 .release
= tracing_pipe_buf_release
,
3775 .steal
= generic_pipe_buf_steal
,
3776 .get
= generic_pipe_buf_get
,
3780 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
3785 /* Seq buffer is page-sized, exactly what we need. */
3787 count
= iter
->seq
.len
;
3788 ret
= print_trace_line(iter
);
3789 count
= iter
->seq
.len
- count
;
3792 iter
->seq
.len
-= count
;
3795 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3796 iter
->seq
.len
-= count
;
3800 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3801 trace_consume(iter
);
3803 if (!trace_find_next_entry_inc(iter
)) {
3813 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3815 struct pipe_inode_info
*pipe
,
3819 struct page
*pages_def
[PIPE_DEF_BUFFERS
];
3820 struct partial_page partial_def
[PIPE_DEF_BUFFERS
];
3821 struct trace_iterator
*iter
= filp
->private_data
;
3822 struct splice_pipe_desc spd
= {
3824 .partial
= partial_def
,
3825 .nr_pages
= 0, /* This gets updated below. */
3826 .nr_pages_max
= PIPE_DEF_BUFFERS
,
3828 .ops
= &tracing_pipe_buf_ops
,
3829 .spd_release
= tracing_spd_release_pipe
,
3831 struct trace_array
*tr
= iter
->tr
;
3836 if (splice_grow_spd(pipe
, &spd
))
3839 /* copy the tracer to avoid using a global lock all around */
3840 mutex_lock(&trace_types_lock
);
3841 if (unlikely(iter
->trace
->name
!= tr
->current_trace
->name
))
3842 *iter
->trace
= *tr
->current_trace
;
3843 mutex_unlock(&trace_types_lock
);
3845 mutex_lock(&iter
->mutex
);
3847 if (iter
->trace
->splice_read
) {
3848 ret
= iter
->trace
->splice_read(iter
, filp
,
3849 ppos
, pipe
, len
, flags
);
3854 ret
= tracing_wait_pipe(filp
);
3858 if (!iter
->ent
&& !trace_find_next_entry_inc(iter
)) {
3863 trace_event_read_lock();
3864 trace_access_lock(iter
->cpu_file
);
3866 /* Fill as many pages as possible. */
3867 for (i
= 0, rem
= len
; i
< pipe
->buffers
&& rem
; i
++) {
3868 spd
.pages
[i
] = alloc_page(GFP_KERNEL
);
3872 rem
= tracing_fill_pipe_page(rem
, iter
);
3874 /* Copy the data into the page, so we can start over. */
3875 ret
= trace_seq_to_buffer(&iter
->seq
,
3876 page_address(spd
.pages
[i
]),
3879 __free_page(spd
.pages
[i
]);
3882 spd
.partial
[i
].offset
= 0;
3883 spd
.partial
[i
].len
= iter
->seq
.len
;
3885 trace_seq_init(&iter
->seq
);
3888 trace_access_unlock(iter
->cpu_file
);
3889 trace_event_read_unlock();
3890 mutex_unlock(&iter
->mutex
);
3894 ret
= splice_to_pipe(pipe
, &spd
);
3896 splice_shrink_spd(&spd
);
3900 mutex_unlock(&iter
->mutex
);
3905 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3906 size_t cnt
, loff_t
*ppos
)
3908 struct trace_cpu
*tc
= filp
->private_data
;
3909 struct trace_array
*tr
= tc
->tr
;
3914 mutex_lock(&trace_types_lock
);
3916 if (tc
->cpu
== RING_BUFFER_ALL_CPUS
) {
3917 int cpu
, buf_size_same
;
3922 /* check if all cpu sizes are same */
3923 for_each_tracing_cpu(cpu
) {
3924 /* fill in the size from first enabled cpu */
3926 size
= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->entries
;
3927 if (size
!= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->entries
) {
3933 if (buf_size_same
) {
3934 if (!ring_buffer_expanded
)
3935 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3937 trace_buf_size
>> 10);
3939 r
= sprintf(buf
, "%lu\n", size
>> 10);
3941 r
= sprintf(buf
, "X\n");
3943 r
= sprintf(buf
, "%lu\n", per_cpu_ptr(tr
->trace_buffer
.data
, tc
->cpu
)->entries
>> 10);
3945 mutex_unlock(&trace_types_lock
);
3947 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3952 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3953 size_t cnt
, loff_t
*ppos
)
3955 struct trace_cpu
*tc
= filp
->private_data
;
3959 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
3963 /* must have at least 1 entry */
3967 /* value is in KB */
3970 ret
= tracing_resize_ring_buffer(tc
->tr
, val
, tc
->cpu
);
3980 tracing_total_entries_read(struct file
*filp
, char __user
*ubuf
,
3981 size_t cnt
, loff_t
*ppos
)
3983 struct trace_array
*tr
= filp
->private_data
;
3986 unsigned long size
= 0, expanded_size
= 0;
3988 mutex_lock(&trace_types_lock
);
3989 for_each_tracing_cpu(cpu
) {
3990 size
+= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->entries
>> 10;
3991 if (!ring_buffer_expanded
)
3992 expanded_size
+= trace_buf_size
>> 10;
3994 if (ring_buffer_expanded
)
3995 r
= sprintf(buf
, "%lu\n", size
);
3997 r
= sprintf(buf
, "%lu (expanded: %lu)\n", size
, expanded_size
);
3998 mutex_unlock(&trace_types_lock
);
4000 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
4004 tracing_free_buffer_write(struct file
*filp
, const char __user
*ubuf
,
4005 size_t cnt
, loff_t
*ppos
)
4008 * There is no need to read what the user has written, this function
4009 * is just to make sure that there is no error when "echo" is used
4018 tracing_free_buffer_release(struct inode
*inode
, struct file
*filp
)
4020 struct trace_array
*tr
= inode
->i_private
;
4022 /* disable tracing ? */
4023 if (trace_flags
& TRACE_ITER_STOP_ON_FREE
)
4025 /* resize the ring buffer to 0 */
4026 tracing_resize_ring_buffer(tr
, 0, RING_BUFFER_ALL_CPUS
);
4032 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
4033 size_t cnt
, loff_t
*fpos
)
4035 unsigned long addr
= (unsigned long)ubuf
;
4036 struct ring_buffer_event
*event
;
4037 struct ring_buffer
*buffer
;
4038 struct print_entry
*entry
;
4039 unsigned long irq_flags
;
4040 struct page
*pages
[2];
4050 if (tracing_disabled
)
4053 if (!(trace_flags
& TRACE_ITER_MARKERS
))
4056 if (cnt
> TRACE_BUF_SIZE
)
4057 cnt
= TRACE_BUF_SIZE
;
4060 * Userspace is injecting traces into the kernel trace buffer.
4061 * We want to be as non intrusive as possible.
4062 * To do so, we do not want to allocate any special buffers
4063 * or take any locks, but instead write the userspace data
4064 * straight into the ring buffer.
4066 * First we need to pin the userspace buffer into memory,
4067 * which, most likely it is, because it just referenced it.
4068 * But there's no guarantee that it is. By using get_user_pages_fast()
4069 * and kmap_atomic/kunmap_atomic() we can get access to the
4070 * pages directly. We then write the data directly into the
4073 BUILD_BUG_ON(TRACE_BUF_SIZE
>= PAGE_SIZE
);
4075 /* check if we cross pages */
4076 if ((addr
& PAGE_MASK
) != ((addr
+ cnt
) & PAGE_MASK
))
4079 offset
= addr
& (PAGE_SIZE
- 1);
4082 ret
= get_user_pages_fast(addr
, nr_pages
, 0, pages
);
4083 if (ret
< nr_pages
) {
4085 put_page(pages
[ret
]);
4090 for (i
= 0; i
< nr_pages
; i
++)
4091 map_page
[i
] = kmap_atomic(pages
[i
]);
4093 local_save_flags(irq_flags
);
4094 size
= sizeof(*entry
) + cnt
+ 2; /* possible \n added */
4095 buffer
= global_trace
.trace_buffer
.buffer
;
4096 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
4097 irq_flags
, preempt_count());
4099 /* Ring buffer disabled, return as if not open for write */
4104 entry
= ring_buffer_event_data(event
);
4105 entry
->ip
= _THIS_IP_
;
4107 if (nr_pages
== 2) {
4108 len
= PAGE_SIZE
- offset
;
4109 memcpy(&entry
->buf
, map_page
[0] + offset
, len
);
4110 memcpy(&entry
->buf
[len
], map_page
[1], cnt
- len
);
4112 memcpy(&entry
->buf
, map_page
[0] + offset
, cnt
);
4114 if (entry
->buf
[cnt
- 1] != '\n') {
4115 entry
->buf
[cnt
] = '\n';
4116 entry
->buf
[cnt
+ 1] = '\0';
4118 entry
->buf
[cnt
] = '\0';
4120 __buffer_unlock_commit(buffer
, event
);
4127 for (i
= 0; i
< nr_pages
; i
++){
4128 kunmap_atomic(map_page
[i
]);
4135 static int tracing_clock_show(struct seq_file
*m
, void *v
)
4137 struct trace_array
*tr
= m
->private;
4140 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++)
4142 "%s%s%s%s", i
? " " : "",
4143 i
== tr
->clock_id
? "[" : "", trace_clocks
[i
].name
,
4144 i
== tr
->clock_id
? "]" : "");
4150 static ssize_t
tracing_clock_write(struct file
*filp
, const char __user
*ubuf
,
4151 size_t cnt
, loff_t
*fpos
)
4153 struct seq_file
*m
= filp
->private_data
;
4154 struct trace_array
*tr
= m
->private;
4156 const char *clockstr
;
4159 if (cnt
>= sizeof(buf
))
4162 if (copy_from_user(&buf
, ubuf
, cnt
))
4167 clockstr
= strstrip(buf
);
4169 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++) {
4170 if (strcmp(trace_clocks
[i
].name
, clockstr
) == 0)
4173 if (i
== ARRAY_SIZE(trace_clocks
))
4176 mutex_lock(&trace_types_lock
);
4180 ring_buffer_set_clock(tr
->trace_buffer
.buffer
, trace_clocks
[i
].func
);
4183 * New clock may not be consistent with the previous clock.
4184 * Reset the buffer so that it doesn't have incomparable timestamps.
4186 tracing_reset_online_cpus(&global_trace
.trace_buffer
);
4188 #ifdef CONFIG_TRACER_MAX_TRACE
4189 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
&& tr
->max_buffer
.buffer
)
4190 ring_buffer_set_clock(tr
->max_buffer
.buffer
, trace_clocks
[i
].func
);
4191 tracing_reset_online_cpus(&global_trace
.max_buffer
);
4194 mutex_unlock(&trace_types_lock
);
4201 static int tracing_clock_open(struct inode
*inode
, struct file
*file
)
4203 if (tracing_disabled
)
4206 return single_open(file
, tracing_clock_show
, inode
->i_private
);
4209 #ifdef CONFIG_TRACER_SNAPSHOT
4210 static int tracing_snapshot_open(struct inode
*inode
, struct file
*file
)
4212 struct trace_cpu
*tc
= inode
->i_private
;
4213 struct trace_iterator
*iter
;
4217 if (file
->f_mode
& FMODE_READ
) {
4218 iter
= __tracing_open(inode
, file
, true);
4220 ret
= PTR_ERR(iter
);
4222 /* Writes still need the seq_file to hold the private data */
4223 m
= kzalloc(sizeof(*m
), GFP_KERNEL
);
4226 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
4232 iter
->trace_buffer
= &tc
->tr
->max_buffer
;
4233 iter
->cpu_file
= tc
->cpu
;
4235 file
->private_data
= m
;
4242 tracing_snapshot_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
4245 struct seq_file
*m
= filp
->private_data
;
4246 struct trace_iterator
*iter
= m
->private;
4247 struct trace_array
*tr
= iter
->tr
;
4251 ret
= tracing_update_buffers();
4255 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
4259 mutex_lock(&trace_types_lock
);
4261 if (tr
->current_trace
->use_max_tr
) {
4268 if (iter
->cpu_file
!= RING_BUFFER_ALL_CPUS
) {
4272 if (tr
->current_trace
->allocated_snapshot
) {
4273 /* free spare buffer */
4274 ring_buffer_resize(tr
->max_buffer
.buffer
, 1,
4275 RING_BUFFER_ALL_CPUS
);
4276 set_buffer_entries(&tr
->max_buffer
, 1);
4277 tracing_reset_online_cpus(&tr
->max_buffer
);
4278 tr
->current_trace
->allocated_snapshot
= false;
4282 /* Only allow per-cpu swap if the ring buffer supports it */
4283 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4284 if (iter
->cpu_file
!= RING_BUFFER_ALL_CPUS
) {
4289 if (!tr
->current_trace
->allocated_snapshot
) {
4290 /* allocate spare buffer */
4291 ret
= resize_buffer_duplicate_size(&tr
->max_buffer
,
4292 &tr
->trace_buffer
, RING_BUFFER_ALL_CPUS
);
4295 tr
->current_trace
->allocated_snapshot
= true;
4297 local_irq_disable();
4298 /* Now, we're going to swap */
4299 if (iter
->cpu_file
== RING_BUFFER_ALL_CPUS
)
4300 update_max_tr(&global_trace
, current
, smp_processor_id());
4302 update_max_tr_single(&global_trace
, current
, iter
->cpu_file
);
4306 if (tr
->current_trace
->allocated_snapshot
) {
4307 if (iter
->cpu_file
== RING_BUFFER_ALL_CPUS
)
4308 tracing_reset_online_cpus(&tr
->max_buffer
);
4310 tracing_reset(&tr
->max_buffer
, iter
->cpu_file
);
4320 mutex_unlock(&trace_types_lock
);
4324 static int tracing_snapshot_release(struct inode
*inode
, struct file
*file
)
4326 struct seq_file
*m
= file
->private_data
;
4328 if (file
->f_mode
& FMODE_READ
)
4329 return tracing_release(inode
, file
);
4331 /* If write only, the seq_file is just a stub */
4339 #endif /* CONFIG_TRACER_SNAPSHOT */
4342 static const struct file_operations tracing_max_lat_fops
= {
4343 .open
= tracing_open_generic
,
4344 .read
= tracing_max_lat_read
,
4345 .write
= tracing_max_lat_write
,
4346 .llseek
= generic_file_llseek
,
4349 static const struct file_operations set_tracer_fops
= {
4350 .open
= tracing_open_generic
,
4351 .read
= tracing_set_trace_read
,
4352 .write
= tracing_set_trace_write
,
4353 .llseek
= generic_file_llseek
,
4356 static const struct file_operations tracing_pipe_fops
= {
4357 .open
= tracing_open_pipe
,
4358 .poll
= tracing_poll_pipe
,
4359 .read
= tracing_read_pipe
,
4360 .splice_read
= tracing_splice_read_pipe
,
4361 .release
= tracing_release_pipe
,
4362 .llseek
= no_llseek
,
4365 static const struct file_operations tracing_entries_fops
= {
4366 .open
= tracing_open_generic
,
4367 .read
= tracing_entries_read
,
4368 .write
= tracing_entries_write
,
4369 .llseek
= generic_file_llseek
,
4372 static const struct file_operations tracing_total_entries_fops
= {
4373 .open
= tracing_open_generic
,
4374 .read
= tracing_total_entries_read
,
4375 .llseek
= generic_file_llseek
,
4378 static const struct file_operations tracing_free_buffer_fops
= {
4379 .write
= tracing_free_buffer_write
,
4380 .release
= tracing_free_buffer_release
,
4383 static const struct file_operations tracing_mark_fops
= {
4384 .open
= tracing_open_generic
,
4385 .write
= tracing_mark_write
,
4386 .llseek
= generic_file_llseek
,
4389 static const struct file_operations trace_clock_fops
= {
4390 .open
= tracing_clock_open
,
4392 .llseek
= seq_lseek
,
4393 .release
= single_release
,
4394 .write
= tracing_clock_write
,
4397 #ifdef CONFIG_TRACER_SNAPSHOT
4398 static const struct file_operations snapshot_fops
= {
4399 .open
= tracing_snapshot_open
,
4401 .write
= tracing_snapshot_write
,
4402 .llseek
= tracing_seek
,
4403 .release
= tracing_snapshot_release
,
4405 #endif /* CONFIG_TRACER_SNAPSHOT */
4407 struct ftrace_buffer_info
{
4408 struct trace_iterator iter
;
4413 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
4415 struct trace_cpu
*tc
= inode
->i_private
;
4416 struct trace_array
*tr
= tc
->tr
;
4417 struct ftrace_buffer_info
*info
;
4419 if (tracing_disabled
)
4422 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
4427 info
->iter
.cpu_file
= tc
->cpu
;
4428 info
->iter
.trace
= tr
->current_trace
;
4429 info
->iter
.trace_buffer
= &tr
->trace_buffer
;
4431 /* Force reading ring buffer for first read */
4432 info
->read
= (unsigned int)-1;
4434 filp
->private_data
= info
;
4436 return nonseekable_open(inode
, filp
);
4440 tracing_buffers_poll(struct file
*filp
, poll_table
*poll_table
)
4442 struct ftrace_buffer_info
*info
= filp
->private_data
;
4443 struct trace_iterator
*iter
= &info
->iter
;
4445 return trace_poll(iter
, filp
, poll_table
);
4449 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
4450 size_t count
, loff_t
*ppos
)
4452 struct ftrace_buffer_info
*info
= filp
->private_data
;
4453 struct trace_iterator
*iter
= &info
->iter
;
4461 info
->spare
= ring_buffer_alloc_read_page(iter
->trace_buffer
->buffer
,
4466 /* Do we have previous read data to read? */
4467 if (info
->read
< PAGE_SIZE
)
4471 trace_access_lock(iter
->cpu_file
);
4472 ret
= ring_buffer_read_page(iter
->trace_buffer
->buffer
,
4476 trace_access_unlock(iter
->cpu_file
);
4479 if (trace_empty(iter
)) {
4480 if ((filp
->f_flags
& O_NONBLOCK
))
4482 iter
->trace
->wait_pipe(iter
);
4483 if (signal_pending(current
))
4493 size
= PAGE_SIZE
- info
->read
;
4497 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
4508 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
4510 struct ftrace_buffer_info
*info
= file
->private_data
;
4511 struct trace_iterator
*iter
= &info
->iter
;
4514 ring_buffer_free_read_page(iter
->trace_buffer
->buffer
, info
->spare
);
4521 struct ring_buffer
*buffer
;
4526 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
4527 struct pipe_buffer
*buf
)
4529 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
4534 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
4539 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
4540 struct pipe_buffer
*buf
)
4542 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
4547 /* Pipe buffer operations for a buffer. */
4548 static const struct pipe_buf_operations buffer_pipe_buf_ops
= {
4550 .map
= generic_pipe_buf_map
,
4551 .unmap
= generic_pipe_buf_unmap
,
4552 .confirm
= generic_pipe_buf_confirm
,
4553 .release
= buffer_pipe_buf_release
,
4554 .steal
= generic_pipe_buf_steal
,
4555 .get
= buffer_pipe_buf_get
,
4559 * Callback from splice_to_pipe(), if we need to release some pages
4560 * at the end of the spd in case we error'ed out in filling the pipe.
4562 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
4564 struct buffer_ref
*ref
=
4565 (struct buffer_ref
*)spd
->partial
[i
].private;
4570 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
4572 spd
->partial
[i
].private = 0;
4576 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
4577 struct pipe_inode_info
*pipe
, size_t len
,
4580 struct ftrace_buffer_info
*info
= file
->private_data
;
4581 struct trace_iterator
*iter
= &info
->iter
;
4582 struct partial_page partial_def
[PIPE_DEF_BUFFERS
];
4583 struct page
*pages_def
[PIPE_DEF_BUFFERS
];
4584 struct splice_pipe_desc spd
= {
4586 .partial
= partial_def
,
4587 .nr_pages_max
= PIPE_DEF_BUFFERS
,
4589 .ops
= &buffer_pipe_buf_ops
,
4590 .spd_release
= buffer_spd_release
,
4592 struct buffer_ref
*ref
;
4593 int entries
, size
, i
;
4596 if (splice_grow_spd(pipe
, &spd
))
4599 if (*ppos
& (PAGE_SIZE
- 1)) {
4604 if (len
& (PAGE_SIZE
- 1)) {
4605 if (len
< PAGE_SIZE
) {
4613 trace_access_lock(iter
->cpu_file
);
4614 entries
= ring_buffer_entries_cpu(iter
->trace_buffer
->buffer
, iter
->cpu_file
);
4616 for (i
= 0; i
< pipe
->buffers
&& len
&& entries
; i
++, len
-= PAGE_SIZE
) {
4620 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
4625 ref
->buffer
= iter
->trace_buffer
->buffer
;
4626 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
, iter
->cpu_file
);
4632 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
4633 len
, iter
->cpu_file
, 1);
4635 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
4641 * zero out any left over data, this is going to
4644 size
= ring_buffer_page_len(ref
->page
);
4645 if (size
< PAGE_SIZE
)
4646 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
4648 page
= virt_to_page(ref
->page
);
4650 spd
.pages
[i
] = page
;
4651 spd
.partial
[i
].len
= PAGE_SIZE
;
4652 spd
.partial
[i
].offset
= 0;
4653 spd
.partial
[i
].private = (unsigned long)ref
;
4657 entries
= ring_buffer_entries_cpu(iter
->trace_buffer
->buffer
, iter
->cpu_file
);
4660 trace_access_unlock(iter
->cpu_file
);
4663 /* did we read anything? */
4664 if (!spd
.nr_pages
) {
4665 if ((file
->f_flags
& O_NONBLOCK
) || (flags
& SPLICE_F_NONBLOCK
)) {
4669 iter
->trace
->wait_pipe(iter
);
4670 if (signal_pending(current
)) {
4677 ret
= splice_to_pipe(pipe
, &spd
);
4678 splice_shrink_spd(&spd
);
4683 static const struct file_operations tracing_buffers_fops
= {
4684 .open
= tracing_buffers_open
,
4685 .read
= tracing_buffers_read
,
4686 .poll
= tracing_buffers_poll
,
4687 .release
= tracing_buffers_release
,
4688 .splice_read
= tracing_buffers_splice_read
,
4689 .llseek
= no_llseek
,
4693 tracing_stats_read(struct file
*filp
, char __user
*ubuf
,
4694 size_t count
, loff_t
*ppos
)
4696 struct trace_cpu
*tc
= filp
->private_data
;
4697 struct trace_array
*tr
= tc
->tr
;
4698 struct trace_buffer
*trace_buf
= &tr
->trace_buffer
;
4699 struct trace_seq
*s
;
4701 unsigned long long t
;
4702 unsigned long usec_rem
;
4705 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
4711 cnt
= ring_buffer_entries_cpu(trace_buf
->buffer
, cpu
);
4712 trace_seq_printf(s
, "entries: %ld\n", cnt
);
4714 cnt
= ring_buffer_overrun_cpu(trace_buf
->buffer
, cpu
);
4715 trace_seq_printf(s
, "overrun: %ld\n", cnt
);
4717 cnt
= ring_buffer_commit_overrun_cpu(trace_buf
->buffer
, cpu
);
4718 trace_seq_printf(s
, "commit overrun: %ld\n", cnt
);
4720 cnt
= ring_buffer_bytes_cpu(trace_buf
->buffer
, cpu
);
4721 trace_seq_printf(s
, "bytes: %ld\n", cnt
);
4723 if (trace_clocks
[trace_clock_id
].in_ns
) {
4724 /* local or global for trace_clock */
4725 t
= ns2usecs(ring_buffer_oldest_event_ts(trace_buf
->buffer
, cpu
));
4726 usec_rem
= do_div(t
, USEC_PER_SEC
);
4727 trace_seq_printf(s
, "oldest event ts: %5llu.%06lu\n",
4730 t
= ns2usecs(ring_buffer_time_stamp(trace_buf
->buffer
, cpu
));
4731 usec_rem
= do_div(t
, USEC_PER_SEC
);
4732 trace_seq_printf(s
, "now ts: %5llu.%06lu\n", t
, usec_rem
);
4734 /* counter or tsc mode for trace_clock */
4735 trace_seq_printf(s
, "oldest event ts: %llu\n",
4736 ring_buffer_oldest_event_ts(trace_buf
->buffer
, cpu
));
4738 trace_seq_printf(s
, "now ts: %llu\n",
4739 ring_buffer_time_stamp(trace_buf
->buffer
, cpu
));
4742 cnt
= ring_buffer_dropped_events_cpu(trace_buf
->buffer
, cpu
);
4743 trace_seq_printf(s
, "dropped events: %ld\n", cnt
);
4745 cnt
= ring_buffer_read_events_cpu(trace_buf
->buffer
, cpu
);
4746 trace_seq_printf(s
, "read events: %ld\n", cnt
);
4748 count
= simple_read_from_buffer(ubuf
, count
, ppos
, s
->buffer
, s
->len
);
4755 static const struct file_operations tracing_stats_fops
= {
4756 .open
= tracing_open_generic
,
4757 .read
= tracing_stats_read
,
4758 .llseek
= generic_file_llseek
,
4761 #ifdef CONFIG_DYNAMIC_FTRACE
4763 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
4769 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
4770 size_t cnt
, loff_t
*ppos
)
4772 static char ftrace_dyn_info_buffer
[1024];
4773 static DEFINE_MUTEX(dyn_info_mutex
);
4774 unsigned long *p
= filp
->private_data
;
4775 char *buf
= ftrace_dyn_info_buffer
;
4776 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
4779 mutex_lock(&dyn_info_mutex
);
4780 r
= sprintf(buf
, "%ld ", *p
);
4782 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
4785 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
4787 mutex_unlock(&dyn_info_mutex
);
4792 static const struct file_operations tracing_dyn_info_fops
= {
4793 .open
= tracing_open_generic
,
4794 .read
= tracing_read_dyn_info
,
4795 .llseek
= generic_file_llseek
,
4799 struct dentry
*tracing_init_dentry_tr(struct trace_array
*tr
)
4806 if (!debugfs_initialized())
4809 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
)
4810 tr
->dir
= debugfs_create_dir("tracing", NULL
);
4812 if (!tr
->dir
&& !once
) {
4814 pr_warning("Could not create debugfs directory 'tracing'\n");
4821 struct dentry
*tracing_init_dentry(void)
4823 return tracing_init_dentry_tr(&global_trace
);
4826 static struct dentry
*tracing_dentry_percpu(struct trace_array
*tr
, int cpu
)
4828 struct dentry
*d_tracer
;
4831 return tr
->percpu_dir
;
4833 d_tracer
= tracing_init_dentry_tr(tr
);
4837 tr
->percpu_dir
= debugfs_create_dir("per_cpu", d_tracer
);
4839 WARN_ONCE(!tr
->percpu_dir
,
4840 "Could not create debugfs directory 'per_cpu/%d'\n", cpu
);
4842 return tr
->percpu_dir
;
4846 tracing_init_debugfs_percpu(struct trace_array
*tr
, long cpu
)
4848 struct trace_array_cpu
*data
= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
);
4849 struct dentry
*d_percpu
= tracing_dentry_percpu(tr
, cpu
);
4850 struct dentry
*d_cpu
;
4851 char cpu_dir
[30]; /* 30 characters should be more than enough */
4856 snprintf(cpu_dir
, 30, "cpu%ld", cpu
);
4857 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
4859 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
4863 /* per cpu trace_pipe */
4864 trace_create_file("trace_pipe", 0444, d_cpu
,
4865 (void *)&data
->trace_cpu
, &tracing_pipe_fops
);
4868 trace_create_file("trace", 0644, d_cpu
,
4869 (void *)&data
->trace_cpu
, &tracing_fops
);
4871 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
4872 (void *)&data
->trace_cpu
, &tracing_buffers_fops
);
4874 trace_create_file("stats", 0444, d_cpu
,
4875 (void *)&data
->trace_cpu
, &tracing_stats_fops
);
4877 trace_create_file("buffer_size_kb", 0444, d_cpu
,
4878 (void *)&data
->trace_cpu
, &tracing_entries_fops
);
4880 #ifdef CONFIG_TRACER_SNAPSHOT
4881 trace_create_file("snapshot", 0644, d_cpu
,
4882 (void *)&data
->trace_cpu
, &snapshot_fops
);
4886 #ifdef CONFIG_FTRACE_SELFTEST
4887 /* Let selftest have access to static functions in this file */
4888 #include "trace_selftest.c"
4891 struct trace_option_dentry
{
4892 struct tracer_opt
*opt
;
4893 struct tracer_flags
*flags
;
4894 struct trace_array
*tr
;
4895 struct dentry
*entry
;
4899 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
4902 struct trace_option_dentry
*topt
= filp
->private_data
;
4905 if (topt
->flags
->val
& topt
->opt
->bit
)
4910 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
4914 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
4917 struct trace_option_dentry
*topt
= filp
->private_data
;
4921 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
4925 if (val
!= 0 && val
!= 1)
4928 if (!!(topt
->flags
->val
& topt
->opt
->bit
) != val
) {
4929 mutex_lock(&trace_types_lock
);
4930 ret
= __set_tracer_option(topt
->tr
->current_trace
, topt
->flags
,
4932 mutex_unlock(&trace_types_lock
);
4943 static const struct file_operations trace_options_fops
= {
4944 .open
= tracing_open_generic
,
4945 .read
= trace_options_read
,
4946 .write
= trace_options_write
,
4947 .llseek
= generic_file_llseek
,
4951 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
4954 long index
= (long)filp
->private_data
;
4957 if (trace_flags
& (1 << index
))
4962 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
4966 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
4969 struct trace_array
*tr
= &global_trace
;
4970 long index
= (long)filp
->private_data
;
4974 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
4978 if (val
!= 0 && val
!= 1)
4981 mutex_lock(&trace_types_lock
);
4982 ret
= set_tracer_flag(tr
, 1 << index
, val
);
4983 mutex_unlock(&trace_types_lock
);
4993 static const struct file_operations trace_options_core_fops
= {
4994 .open
= tracing_open_generic
,
4995 .read
= trace_options_core_read
,
4996 .write
= trace_options_core_write
,
4997 .llseek
= generic_file_llseek
,
5000 struct dentry
*trace_create_file(const char *name
,
5002 struct dentry
*parent
,
5004 const struct file_operations
*fops
)
5008 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
5010 pr_warning("Could not create debugfs '%s' entry\n", name
);
5016 static struct dentry
*trace_options_init_dentry(struct trace_array
*tr
)
5018 struct dentry
*d_tracer
;
5023 d_tracer
= tracing_init_dentry_tr(tr
);
5027 tr
->options
= debugfs_create_dir("options", d_tracer
);
5029 pr_warning("Could not create debugfs directory 'options'\n");
5037 create_trace_option_file(struct trace_array
*tr
,
5038 struct trace_option_dentry
*topt
,
5039 struct tracer_flags
*flags
,
5040 struct tracer_opt
*opt
)
5042 struct dentry
*t_options
;
5044 t_options
= trace_options_init_dentry(tr
);
5048 topt
->flags
= flags
;
5052 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
5053 &trace_options_fops
);
5057 static struct trace_option_dentry
*
5058 create_trace_option_files(struct trace_array
*tr
, struct tracer
*tracer
)
5060 struct trace_option_dentry
*topts
;
5061 struct tracer_flags
*flags
;
5062 struct tracer_opt
*opts
;
5068 flags
= tracer
->flags
;
5070 if (!flags
|| !flags
->opts
)
5075 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
5078 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
5082 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
5083 create_trace_option_file(tr
, &topts
[cnt
], flags
,
5090 destroy_trace_option_files(struct trace_option_dentry
*topts
)
5097 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
5098 if (topts
[cnt
].entry
)
5099 debugfs_remove(topts
[cnt
].entry
);
5105 static struct dentry
*
5106 create_trace_option_core_file(struct trace_array
*tr
,
5107 const char *option
, long index
)
5109 struct dentry
*t_options
;
5111 t_options
= trace_options_init_dentry(tr
);
5115 return trace_create_file(option
, 0644, t_options
, (void *)index
,
5116 &trace_options_core_fops
);
5119 static __init
void create_trace_options_dir(struct trace_array
*tr
)
5121 struct dentry
*t_options
;
5124 t_options
= trace_options_init_dentry(tr
);
5128 for (i
= 0; trace_options
[i
]; i
++)
5129 create_trace_option_core_file(tr
, trace_options
[i
], i
);
5133 rb_simple_read(struct file
*filp
, char __user
*ubuf
,
5134 size_t cnt
, loff_t
*ppos
)
5136 struct trace_array
*tr
= filp
->private_data
;
5137 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
5142 r
= ring_buffer_record_is_on(buffer
);
5146 r
= sprintf(buf
, "%d\n", r
);
5148 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
5152 rb_simple_write(struct file
*filp
, const char __user
*ubuf
,
5153 size_t cnt
, loff_t
*ppos
)
5155 struct trace_array
*tr
= filp
->private_data
;
5156 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
5160 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
5165 mutex_lock(&trace_types_lock
);
5167 ring_buffer_record_on(buffer
);
5168 if (tr
->current_trace
->start
)
5169 tr
->current_trace
->start(tr
);
5171 ring_buffer_record_off(buffer
);
5172 if (tr
->current_trace
->stop
)
5173 tr
->current_trace
->stop(tr
);
5175 mutex_unlock(&trace_types_lock
);
5183 static const struct file_operations rb_simple_fops
= {
5184 .open
= tracing_open_generic
,
5185 .read
= rb_simple_read
,
5186 .write
= rb_simple_write
,
5187 .llseek
= default_llseek
,
5190 struct dentry
*trace_instance_dir
;
5193 init_tracer_debugfs(struct trace_array
*tr
, struct dentry
*d_tracer
);
5195 static int new_instance_create(const char *name
)
5197 enum ring_buffer_flags rb_flags
;
5198 struct trace_array
*tr
;
5202 mutex_lock(&trace_types_lock
);
5205 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
5206 if (tr
->name
&& strcmp(tr
->name
, name
) == 0)
5211 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
5215 tr
->name
= kstrdup(name
, GFP_KERNEL
);
5219 raw_spin_lock_init(&tr
->start_lock
);
5221 tr
->current_trace
= &nop_trace
;
5223 INIT_LIST_HEAD(&tr
->systems
);
5224 INIT_LIST_HEAD(&tr
->events
);
5226 rb_flags
= trace_flags
& TRACE_ITER_OVERWRITE
? RB_FL_OVERWRITE
: 0;
5228 tr
->trace_buffer
.buffer
= ring_buffer_alloc(trace_buf_size
, rb_flags
);
5229 if (!tr
->trace_buffer
.buffer
)
5232 tr
->trace_buffer
.data
= alloc_percpu(struct trace_array_cpu
);
5233 if (!tr
->trace_buffer
.data
)
5236 for_each_tracing_cpu(i
) {
5237 memset(per_cpu_ptr(tr
->trace_buffer
.data
, i
), 0, sizeof(struct trace_array_cpu
));
5238 per_cpu_ptr(tr
->trace_buffer
.data
, i
)->trace_cpu
.cpu
= i
;
5239 per_cpu_ptr(tr
->trace_buffer
.data
, i
)->trace_cpu
.tr
= tr
;
5242 /* Holder for file callbacks */
5243 tr
->trace_cpu
.cpu
= RING_BUFFER_ALL_CPUS
;
5244 tr
->trace_cpu
.tr
= tr
;
5246 tr
->dir
= debugfs_create_dir(name
, trace_instance_dir
);
5250 ret
= event_trace_add_tracer(tr
->dir
, tr
);
5254 init_tracer_debugfs(tr
, tr
->dir
);
5256 list_add(&tr
->list
, &ftrace_trace_arrays
);
5258 mutex_unlock(&trace_types_lock
);
5263 if (tr
->trace_buffer
.buffer
)
5264 ring_buffer_free(tr
->trace_buffer
.buffer
);
5269 mutex_unlock(&trace_types_lock
);
5275 static int instance_delete(const char *name
)
5277 struct trace_array
*tr
;
5281 mutex_lock(&trace_types_lock
);
5284 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
5285 if (tr
->name
&& strcmp(tr
->name
, name
) == 0) {
5293 list_del(&tr
->list
);
5295 event_trace_del_tracer(tr
);
5296 debugfs_remove_recursive(tr
->dir
);
5297 free_percpu(tr
->trace_buffer
.data
);
5298 ring_buffer_free(tr
->trace_buffer
.buffer
);
5306 mutex_unlock(&trace_types_lock
);
5311 static int instance_mkdir (struct inode
*inode
, struct dentry
*dentry
, umode_t mode
)
5313 struct dentry
*parent
;
5316 /* Paranoid: Make sure the parent is the "instances" directory */
5317 parent
= hlist_entry(inode
->i_dentry
.first
, struct dentry
, d_alias
);
5318 if (WARN_ON_ONCE(parent
!= trace_instance_dir
))
5322 * The inode mutex is locked, but debugfs_create_dir() will also
5323 * take the mutex. As the instances directory can not be destroyed
5324 * or changed in any other way, it is safe to unlock it, and
5325 * let the dentry try. If two users try to make the same dir at
5326 * the same time, then the new_instance_create() will determine the
5329 mutex_unlock(&inode
->i_mutex
);
5331 ret
= new_instance_create(dentry
->d_iname
);
5333 mutex_lock(&inode
->i_mutex
);
5338 static int instance_rmdir(struct inode
*inode
, struct dentry
*dentry
)
5340 struct dentry
*parent
;
5343 /* Paranoid: Make sure the parent is the "instances" directory */
5344 parent
= hlist_entry(inode
->i_dentry
.first
, struct dentry
, d_alias
);
5345 if (WARN_ON_ONCE(parent
!= trace_instance_dir
))
5348 /* The caller did a dget() on dentry */
5349 mutex_unlock(&dentry
->d_inode
->i_mutex
);
5352 * The inode mutex is locked, but debugfs_create_dir() will also
5353 * take the mutex. As the instances directory can not be destroyed
5354 * or changed in any other way, it is safe to unlock it, and
5355 * let the dentry try. If two users try to make the same dir at
5356 * the same time, then the instance_delete() will determine the
5359 mutex_unlock(&inode
->i_mutex
);
5361 ret
= instance_delete(dentry
->d_iname
);
5363 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
5364 mutex_lock(&dentry
->d_inode
->i_mutex
);
5369 static const struct inode_operations instance_dir_inode_operations
= {
5370 .lookup
= simple_lookup
,
5371 .mkdir
= instance_mkdir
,
5372 .rmdir
= instance_rmdir
,
5375 static __init
void create_trace_instances(struct dentry
*d_tracer
)
5377 trace_instance_dir
= debugfs_create_dir("instances", d_tracer
);
5378 if (WARN_ON(!trace_instance_dir
))
5381 /* Hijack the dir inode operations, to allow mkdir */
5382 trace_instance_dir
->d_inode
->i_op
= &instance_dir_inode_operations
;
5386 init_tracer_debugfs(struct trace_array
*tr
, struct dentry
*d_tracer
)
5389 trace_create_file("trace_options", 0644, d_tracer
,
5390 tr
, &tracing_iter_fops
);
5392 trace_create_file("trace", 0644, d_tracer
,
5393 (void *)&tr
->trace_cpu
, &tracing_fops
);
5395 trace_create_file("trace_pipe", 0444, d_tracer
,
5396 (void *)&tr
->trace_cpu
, &tracing_pipe_fops
);
5398 trace_create_file("buffer_size_kb", 0644, d_tracer
,
5399 (void *)&tr
->trace_cpu
, &tracing_entries_fops
);
5401 trace_create_file("buffer_total_size_kb", 0444, d_tracer
,
5402 tr
, &tracing_total_entries_fops
);
5404 trace_create_file("free_buffer", 0644, d_tracer
,
5405 tr
, &tracing_free_buffer_fops
);
5407 trace_create_file("trace_marker", 0220, d_tracer
,
5408 tr
, &tracing_mark_fops
);
5410 trace_create_file("trace_clock", 0644, d_tracer
, tr
,
5413 trace_create_file("tracing_on", 0644, d_tracer
,
5414 tr
, &rb_simple_fops
);
5417 static __init
int tracer_init_debugfs(void)
5419 struct dentry
*d_tracer
;
5422 trace_access_lock_init();
5424 d_tracer
= tracing_init_dentry();
5426 init_tracer_debugfs(&global_trace
, d_tracer
);
5428 trace_create_file("tracing_cpumask", 0644, d_tracer
,
5429 &global_trace
, &tracing_cpumask_fops
);
5431 trace_create_file("available_tracers", 0444, d_tracer
,
5432 &global_trace
, &show_traces_fops
);
5434 trace_create_file("current_tracer", 0644, d_tracer
,
5435 &global_trace
, &set_tracer_fops
);
5437 #ifdef CONFIG_TRACER_MAX_TRACE
5438 trace_create_file("tracing_max_latency", 0644, d_tracer
,
5439 &tracing_max_latency
, &tracing_max_lat_fops
);
5442 trace_create_file("tracing_thresh", 0644, d_tracer
,
5443 &tracing_thresh
, &tracing_max_lat_fops
);
5445 trace_create_file("README", 0444, d_tracer
,
5446 NULL
, &tracing_readme_fops
);
5448 trace_create_file("saved_cmdlines", 0444, d_tracer
,
5449 NULL
, &tracing_saved_cmdlines_fops
);
5451 #ifdef CONFIG_DYNAMIC_FTRACE
5452 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
5453 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
5456 #ifdef CONFIG_TRACER_SNAPSHOT
5457 trace_create_file("snapshot", 0644, d_tracer
,
5458 (void *)&global_trace
.trace_cpu
, &snapshot_fops
);
5461 create_trace_instances(d_tracer
);
5463 create_trace_options_dir(&global_trace
);
5465 for_each_tracing_cpu(cpu
)
5466 tracing_init_debugfs_percpu(&global_trace
, cpu
);
5471 static int trace_panic_handler(struct notifier_block
*this,
5472 unsigned long event
, void *unused
)
5474 if (ftrace_dump_on_oops
)
5475 ftrace_dump(ftrace_dump_on_oops
);
5479 static struct notifier_block trace_panic_notifier
= {
5480 .notifier_call
= trace_panic_handler
,
5482 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
5485 static int trace_die_handler(struct notifier_block
*self
,
5491 if (ftrace_dump_on_oops
)
5492 ftrace_dump(ftrace_dump_on_oops
);
5500 static struct notifier_block trace_die_notifier
= {
5501 .notifier_call
= trace_die_handler
,
5506 * printk is set to max of 1024, we really don't need it that big.
5507 * Nothing should be printing 1000 characters anyway.
5509 #define TRACE_MAX_PRINT 1000
5512 * Define here KERN_TRACE so that we have one place to modify
5513 * it if we decide to change what log level the ftrace dump
5516 #define KERN_TRACE KERN_EMERG
5519 trace_printk_seq(struct trace_seq
*s
)
5521 /* Probably should print a warning here. */
5525 /* should be zero ended, but we are paranoid. */
5526 s
->buffer
[s
->len
] = 0;
5528 printk(KERN_TRACE
"%s", s
->buffer
);
5533 void trace_init_global_iter(struct trace_iterator
*iter
)
5535 iter
->tr
= &global_trace
;
5536 iter
->trace
= iter
->tr
->current_trace
;
5537 iter
->cpu_file
= RING_BUFFER_ALL_CPUS
;
5538 iter
->trace_buffer
= &global_trace
.trace_buffer
;
5542 __ftrace_dump(bool disable_tracing
, enum ftrace_dump_mode oops_dump_mode
)
5544 static arch_spinlock_t ftrace_dump_lock
=
5545 (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
5546 /* use static because iter can be a bit big for the stack */
5547 static struct trace_iterator iter
;
5548 unsigned int old_userobj
;
5549 static int dump_ran
;
5550 unsigned long flags
;
5554 local_irq_save(flags
);
5555 arch_spin_lock(&ftrace_dump_lock
);
5563 /* Did function tracer already get disabled? */
5564 if (ftrace_is_dead()) {
5565 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
5566 printk("# MAY BE MISSING FUNCTION EVENTS\n");
5569 if (disable_tracing
)
5572 /* Simulate the iterator */
5573 trace_init_global_iter(&iter
);
5575 for_each_tracing_cpu(cpu
) {
5576 atomic_inc(&per_cpu_ptr(iter
.tr
->trace_buffer
.data
, cpu
)->disabled
);
5579 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
5581 /* don't look at user memory in panic mode */
5582 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
5584 switch (oops_dump_mode
) {
5586 iter
.cpu_file
= RING_BUFFER_ALL_CPUS
;
5589 iter
.cpu_file
= raw_smp_processor_id();
5594 printk(KERN_TRACE
"Bad dumping mode, switching to all CPUs dump\n");
5595 iter
.cpu_file
= RING_BUFFER_ALL_CPUS
;
5598 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
5601 * We need to stop all tracing on all CPUS to read the
5602 * the next buffer. This is a bit expensive, but is
5603 * not done often. We fill all what we can read,
5604 * and then release the locks again.
5607 while (!trace_empty(&iter
)) {
5610 printk(KERN_TRACE
"---------------------------------\n");
5614 /* reset all but tr, trace, and overruns */
5615 memset(&iter
.seq
, 0,
5616 sizeof(struct trace_iterator
) -
5617 offsetof(struct trace_iterator
, seq
));
5618 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
5621 if (trace_find_next_entry_inc(&iter
) != NULL
) {
5624 ret
= print_trace_line(&iter
);
5625 if (ret
!= TRACE_TYPE_NO_CONSUME
)
5626 trace_consume(&iter
);
5628 touch_nmi_watchdog();
5630 trace_printk_seq(&iter
.seq
);
5634 printk(KERN_TRACE
" (ftrace buffer empty)\n");
5636 printk(KERN_TRACE
"---------------------------------\n");
5639 /* Re-enable tracing if requested */
5640 if (!disable_tracing
) {
5641 trace_flags
|= old_userobj
;
5643 for_each_tracing_cpu(cpu
) {
5644 atomic_dec(&per_cpu_ptr(iter
.trace_buffer
->data
, cpu
)->disabled
);
5650 arch_spin_unlock(&ftrace_dump_lock
);
5651 local_irq_restore(flags
);
5654 /* By default: disable tracing after the dump */
5655 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode
)
5657 __ftrace_dump(true, oops_dump_mode
);
5659 EXPORT_SYMBOL_GPL(ftrace_dump
);
5661 __init
static int tracer_alloc_buffers(void)
5664 enum ring_buffer_flags rb_flags
;
5669 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
5672 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
5673 goto out_free_buffer_mask
;
5675 /* Only allocate trace_printk buffers if a trace_printk exists */
5676 if (__stop___trace_bprintk_fmt
!= __start___trace_bprintk_fmt
)
5677 /* Must be called before global_trace.buffer is allocated */
5678 trace_printk_init_buffers();
5680 /* To save memory, keep the ring buffer size to its minimum */
5681 if (ring_buffer_expanded
)
5682 ring_buf_size
= trace_buf_size
;
5686 rb_flags
= trace_flags
& TRACE_ITER_OVERWRITE
? RB_FL_OVERWRITE
: 0;
5688 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
5689 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
5691 raw_spin_lock_init(&global_trace
.start_lock
);
5693 /* TODO: make the number of buffers hot pluggable with CPUS */
5694 global_trace
.trace_buffer
.buffer
= ring_buffer_alloc(ring_buf_size
, rb_flags
);
5695 if (!global_trace
.trace_buffer
.buffer
) {
5696 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
5698 goto out_free_cpumask
;
5701 global_trace
.trace_buffer
.data
= alloc_percpu(struct trace_array_cpu
);
5703 if (!global_trace
.trace_buffer
.data
) {
5704 printk(KERN_ERR
"tracer: failed to allocate percpu memory!\n");
5706 goto out_free_cpumask
;
5709 for_each_tracing_cpu(i
) {
5710 memset(per_cpu_ptr(global_trace
.trace_buffer
.data
, i
), 0,
5711 sizeof(struct trace_array_cpu
));
5712 per_cpu_ptr(global_trace
.trace_buffer
.data
, i
)->trace_cpu
.cpu
= i
;
5713 per_cpu_ptr(global_trace
.trace_buffer
.data
, i
)->trace_cpu
.tr
= &global_trace
;
5716 if (global_trace
.buffer_disabled
)
5719 #ifdef CONFIG_TRACER_MAX_TRACE
5720 global_trace
.max_buffer
.data
= alloc_percpu(struct trace_array_cpu
);
5721 if (!global_trace
.max_buffer
.data
) {
5722 printk(KERN_ERR
"tracer: failed to allocate percpu memory!\n");
5724 goto out_free_cpumask
;
5726 global_trace
.max_buffer
.buffer
= ring_buffer_alloc(1, rb_flags
);
5727 if (!global_trace
.max_buffer
.buffer
) {
5728 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
5730 ring_buffer_free(global_trace
.trace_buffer
.buffer
);
5731 goto out_free_cpumask
;
5734 for_each_tracing_cpu(i
) {
5735 memset(per_cpu_ptr(global_trace
.max_buffer
.data
, i
), 0,
5736 sizeof(struct trace_array_cpu
));
5737 per_cpu_ptr(global_trace
.max_buffer
.data
, i
)->trace_cpu
.cpu
= i
;
5738 per_cpu_ptr(global_trace
.max_buffer
.data
, i
)->trace_cpu
.tr
= &global_trace
;
5742 /* Allocate the first page for all buffers */
5743 set_buffer_entries(&global_trace
.trace_buffer
,
5744 ring_buffer_size(global_trace
.trace_buffer
.buffer
, 0));
5745 #ifdef CONFIG_TRACER_MAX_TRACE
5746 set_buffer_entries(&global_trace
.max_buffer
, 1);
5749 trace_init_cmdlines();
5751 register_tracer(&nop_trace
);
5753 global_trace
.current_trace
= &nop_trace
;
5755 /* All seems OK, enable tracing */
5756 tracing_disabled
= 0;
5758 atomic_notifier_chain_register(&panic_notifier_list
,
5759 &trace_panic_notifier
);
5761 register_die_notifier(&trace_die_notifier
);
5763 global_trace
.flags
= TRACE_ARRAY_FL_GLOBAL
;
5765 /* Holder for file callbacks */
5766 global_trace
.trace_cpu
.cpu
= RING_BUFFER_ALL_CPUS
;
5767 global_trace
.trace_cpu
.tr
= &global_trace
;
5769 INIT_LIST_HEAD(&global_trace
.systems
);
5770 INIT_LIST_HEAD(&global_trace
.events
);
5771 list_add(&global_trace
.list
, &ftrace_trace_arrays
);
5773 while (trace_boot_options
) {
5776 option
= strsep(&trace_boot_options
, ",");
5777 trace_set_options(&global_trace
, option
);
5783 free_percpu(global_trace
.trace_buffer
.data
);
5784 #ifdef CONFIG_TRACER_MAX_TRACE
5785 free_percpu(global_trace
.max_buffer
.data
);
5787 free_cpumask_var(tracing_cpumask
);
5788 out_free_buffer_mask
:
5789 free_cpumask_var(tracing_buffer_mask
);
5794 __init
static int clear_boot_tracer(void)
5797 * The default tracer at boot buffer is an init section.
5798 * This function is called in lateinit. If we did not
5799 * find the boot tracer, then clear it out, to prevent
5800 * later registration from accessing the buffer that is
5801 * about to be freed.
5803 if (!default_bootup_tracer
)
5806 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
5807 default_bootup_tracer
);
5808 default_bootup_tracer
= NULL
;
5813 early_initcall(tracer_alloc_buffers
);
5814 fs_initcall(tracer_init_debugfs
);
5815 late_initcall(clear_boot_tracer
);