2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 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 William Lee Irwin III
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>
42 #include "trace_output.h"
45 * On boot up, the ring buffer is set to the minimum size, so that
46 * we do not waste memory on systems that are not using tracing.
48 int ring_buffer_expanded
;
51 * We need to change this state when a selftest is running.
52 * A selftest will lurk into the ring-buffer to count the
53 * entries inserted during the selftest although some concurrent
54 * insertions into the ring-buffer such as trace_printk could occurred
55 * at the same time, giving false positive or negative results.
57 static bool __read_mostly tracing_selftest_running
;
60 * If a tracer is running, we do not want to run SELFTEST.
62 bool __read_mostly tracing_selftest_disabled
;
64 /* For tracers that don't implement custom flags */
65 static struct tracer_opt dummy_tracer_opt
[] = {
69 static struct tracer_flags dummy_tracer_flags
= {
71 .opts
= dummy_tracer_opt
74 static int dummy_set_flag(u32 old_flags
, u32 bit
, int set
)
80 * Kill all tracing for good (never come back).
81 * It is initialized to 1 but will turn to zero if the initialization
82 * of the tracer is successful. But that is the only place that sets
85 static int tracing_disabled
= 1;
87 DEFINE_PER_CPU(int, ftrace_cpu_disabled
);
89 static inline void ftrace_disable_cpu(void)
92 __this_cpu_inc(ftrace_cpu_disabled
);
95 static inline void ftrace_enable_cpu(void)
97 __this_cpu_dec(ftrace_cpu_disabled
);
101 cpumask_var_t __read_mostly tracing_buffer_mask
;
104 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
106 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
107 * is set, then ftrace_dump is called. This will output the contents
108 * of the ftrace buffers to the console. This is very useful for
109 * capturing traces that lead to crashes and outputing it to a
112 * It is default off, but you can enable it with either specifying
113 * "ftrace_dump_on_oops" in the kernel command line, or setting
114 * /proc/sys/kernel/ftrace_dump_on_oops
115 * Set 1 if you want to dump buffers of all CPUs
116 * Set 2 if you want to dump the buffer of the CPU that triggered oops
119 enum ftrace_dump_mode ftrace_dump_on_oops
;
121 static int tracing_set_tracer(const char *buf
);
123 #define MAX_TRACER_SIZE 100
124 static char bootup_tracer_buf
[MAX_TRACER_SIZE
] __initdata
;
125 static char *default_bootup_tracer
;
127 static int __init
set_cmdline_ftrace(char *str
)
129 strncpy(bootup_tracer_buf
, str
, MAX_TRACER_SIZE
);
130 default_bootup_tracer
= bootup_tracer_buf
;
131 /* We are using ftrace early, expand it */
132 ring_buffer_expanded
= 1;
135 __setup("ftrace=", set_cmdline_ftrace
);
137 static int __init
set_ftrace_dump_on_oops(char *str
)
139 if (*str
++ != '=' || !*str
) {
140 ftrace_dump_on_oops
= DUMP_ALL
;
144 if (!strcmp("orig_cpu", str
)) {
145 ftrace_dump_on_oops
= DUMP_ORIG
;
151 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
153 unsigned long long ns2usecs(cycle_t nsec
)
161 * The global_trace is the descriptor that holds the tracing
162 * buffers for the live tracing. For each CPU, it contains
163 * a link list of pages that will store trace entries. The
164 * page descriptor of the pages in the memory is used to hold
165 * the link list by linking the lru item in the page descriptor
166 * to each of the pages in the buffer per CPU.
168 * For each active CPU there is a data field that holds the
169 * pages for the buffer for that CPU. Each CPU has the same number
170 * of pages allocated for its buffer.
172 static struct trace_array global_trace
;
174 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
176 int filter_current_check_discard(struct ring_buffer
*buffer
,
177 struct ftrace_event_call
*call
, void *rec
,
178 struct ring_buffer_event
*event
)
180 return filter_check_discard(call
, rec
, buffer
, event
);
182 EXPORT_SYMBOL_GPL(filter_current_check_discard
);
184 cycle_t
ftrace_now(int cpu
)
188 /* Early boot up does not have a buffer yet */
189 if (!global_trace
.buffer
)
190 return trace_clock_local();
192 ts
= ring_buffer_time_stamp(global_trace
.buffer
, cpu
);
193 ring_buffer_normalize_time_stamp(global_trace
.buffer
, cpu
, &ts
);
199 * The max_tr is used to snapshot the global_trace when a maximum
200 * latency is reached. Some tracers will use this to store a maximum
201 * trace while it continues examining live traces.
203 * The buffers for the max_tr are set up the same as the global_trace.
204 * When a snapshot is taken, the link list of the max_tr is swapped
205 * with the link list of the global_trace and the buffers are reset for
206 * the global_trace so the tracing can continue.
208 static struct trace_array max_tr
;
210 static DEFINE_PER_CPU(struct trace_array_cpu
, max_tr_data
);
212 /* tracer_enabled is used to toggle activation of a tracer */
213 static int tracer_enabled
= 1;
216 * tracing_is_enabled - return tracer_enabled status
218 * This function is used by other tracers to know the status
219 * of the tracer_enabled flag. Tracers may use this function
220 * to know if it should enable their features when starting
221 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
223 int tracing_is_enabled(void)
225 return tracer_enabled
;
229 * trace_buf_size is the size in bytes that is allocated
230 * for a buffer. Note, the number of bytes is always rounded
233 * This number is purposely set to a low number of 16384.
234 * If the dump on oops happens, it will be much appreciated
235 * to not have to wait for all that output. Anyway this can be
236 * boot time and run time configurable.
238 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
240 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
242 /* trace_types holds a link list of available tracers. */
243 static struct tracer
*trace_types __read_mostly
;
245 /* current_trace points to the tracer that is currently active */
246 static struct tracer
*current_trace __read_mostly
;
249 * trace_types_lock is used to protect the trace_types list.
251 static DEFINE_MUTEX(trace_types_lock
);
254 * serialize the access of the ring buffer
256 * ring buffer serializes readers, but it is low level protection.
257 * The validity of the events (which returns by ring_buffer_peek() ..etc)
258 * are not protected by ring buffer.
260 * The content of events may become garbage if we allow other process consumes
261 * these events concurrently:
262 * A) the page of the consumed events may become a normal page
263 * (not reader page) in ring buffer, and this page will be rewrited
264 * by events producer.
265 * B) The page of the consumed events may become a page for splice_read,
266 * and this page will be returned to system.
268 * These primitives allow multi process access to different cpu ring buffer
271 * These primitives don't distinguish read-only and read-consume access.
272 * Multi read-only access are also serialized.
276 static DECLARE_RWSEM(all_cpu_access_lock
);
277 static DEFINE_PER_CPU(struct mutex
, cpu_access_lock
);
279 static inline void trace_access_lock(int cpu
)
281 if (cpu
== TRACE_PIPE_ALL_CPU
) {
282 /* gain it for accessing the whole ring buffer. */
283 down_write(&all_cpu_access_lock
);
285 /* gain it for accessing a cpu ring buffer. */
287 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
288 down_read(&all_cpu_access_lock
);
290 /* Secondly block other access to this @cpu ring buffer. */
291 mutex_lock(&per_cpu(cpu_access_lock
, cpu
));
295 static inline void trace_access_unlock(int cpu
)
297 if (cpu
== TRACE_PIPE_ALL_CPU
) {
298 up_write(&all_cpu_access_lock
);
300 mutex_unlock(&per_cpu(cpu_access_lock
, cpu
));
301 up_read(&all_cpu_access_lock
);
305 static inline void trace_access_lock_init(void)
309 for_each_possible_cpu(cpu
)
310 mutex_init(&per_cpu(cpu_access_lock
, cpu
));
315 static DEFINE_MUTEX(access_lock
);
317 static inline void trace_access_lock(int cpu
)
320 mutex_lock(&access_lock
);
323 static inline void trace_access_unlock(int cpu
)
326 mutex_unlock(&access_lock
);
329 static inline void trace_access_lock_init(void)
335 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
336 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
338 /* trace_flags holds trace_options default values */
339 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
340 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
| TRACE_ITER_SLEEP_TIME
|
341 TRACE_ITER_GRAPH_TIME
| TRACE_ITER_RECORD_CMD
| TRACE_ITER_OVERWRITE
;
343 static int trace_stop_count
;
344 static DEFINE_SPINLOCK(tracing_start_lock
);
347 * trace_wake_up - wake up tasks waiting for trace input
349 * Simply wakes up any task that is blocked on the trace_wait
350 * queue. These is used with trace_poll for tasks polling the trace.
352 void trace_wake_up(void)
356 if (trace_flags
& TRACE_ITER_BLOCK
)
359 * The runqueue_is_locked() can fail, but this is the best we
363 if (!runqueue_is_locked(cpu
))
364 wake_up(&trace_wait
);
368 static int __init
set_buf_size(char *str
)
370 unsigned long buf_size
;
374 buf_size
= memparse(str
, &str
);
375 /* nr_entries can not be zero */
378 trace_buf_size
= buf_size
;
381 __setup("trace_buf_size=", set_buf_size
);
383 static int __init
set_tracing_thresh(char *str
)
385 unsigned long threshhold
;
390 ret
= strict_strtoul(str
, 0, &threshhold
);
393 tracing_thresh
= threshhold
* 1000;
396 __setup("tracing_thresh=", set_tracing_thresh
);
398 unsigned long nsecs_to_usecs(unsigned long nsecs
)
403 /* These must match the bit postions in trace_iterator_flags */
404 static const char *trace_options
[] = {
434 { trace_clock_local
, "local" },
435 { trace_clock_global
, "global" },
441 * trace_parser_get_init - gets the buffer for trace parser
443 int trace_parser_get_init(struct trace_parser
*parser
, int size
)
445 memset(parser
, 0, sizeof(*parser
));
447 parser
->buffer
= kmalloc(size
, GFP_KERNEL
);
456 * trace_parser_put - frees the buffer for trace parser
458 void trace_parser_put(struct trace_parser
*parser
)
460 kfree(parser
->buffer
);
464 * trace_get_user - reads the user input string separated by space
465 * (matched by isspace(ch))
467 * For each string found the 'struct trace_parser' is updated,
468 * and the function returns.
470 * Returns number of bytes read.
472 * See kernel/trace/trace.h for 'struct trace_parser' details.
474 int trace_get_user(struct trace_parser
*parser
, const char __user
*ubuf
,
475 size_t cnt
, loff_t
*ppos
)
482 trace_parser_clear(parser
);
484 ret
= get_user(ch
, ubuf
++);
492 * The parser is not finished with the last write,
493 * continue reading the user input without skipping spaces.
496 /* skip white space */
497 while (cnt
&& isspace(ch
)) {
498 ret
= get_user(ch
, ubuf
++);
505 /* only spaces were written */
515 /* read the non-space input */
516 while (cnt
&& !isspace(ch
)) {
517 if (parser
->idx
< parser
->size
- 1)
518 parser
->buffer
[parser
->idx
++] = ch
;
523 ret
= get_user(ch
, ubuf
++);
530 /* We either got finished input or we have to wait for another call. */
532 parser
->buffer
[parser
->idx
] = 0;
533 parser
->cont
= false;
536 parser
->buffer
[parser
->idx
++] = ch
;
546 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
554 if (s
->len
<= s
->readpos
)
557 len
= s
->len
- s
->readpos
;
560 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
570 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
575 if (s
->len
<= s
->readpos
)
578 len
= s
->len
- s
->readpos
;
581 ret
= memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
590 * ftrace_max_lock is used to protect the swapping of buffers
591 * when taking a max snapshot. The buffers themselves are
592 * protected by per_cpu spinlocks. But the action of the swap
593 * needs its own lock.
595 * This is defined as a arch_spinlock_t in order to help
596 * with performance when lockdep debugging is enabled.
598 * It is also used in other places outside the update_max_tr
599 * so it needs to be defined outside of the
600 * CONFIG_TRACER_MAX_TRACE.
602 static arch_spinlock_t ftrace_max_lock
=
603 (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
605 unsigned long __read_mostly tracing_thresh
;
607 #ifdef CONFIG_TRACER_MAX_TRACE
608 unsigned long __read_mostly tracing_max_latency
;
611 * Copy the new maximum trace into the separate maximum-trace
612 * structure. (this way the maximum trace is permanently saved,
613 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
616 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
618 struct trace_array_cpu
*data
= tr
->data
[cpu
];
619 struct trace_array_cpu
*max_data
;
622 max_tr
.time_start
= data
->preempt_timestamp
;
624 max_data
= max_tr
.data
[cpu
];
625 max_data
->saved_latency
= tracing_max_latency
;
626 max_data
->critical_start
= data
->critical_start
;
627 max_data
->critical_end
= data
->critical_end
;
629 memcpy(max_data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
630 max_data
->pid
= tsk
->pid
;
631 max_data
->uid
= task_uid(tsk
);
632 max_data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
633 max_data
->policy
= tsk
->policy
;
634 max_data
->rt_priority
= tsk
->rt_priority
;
636 /* record this tasks comm */
637 tracing_record_cmdline(tsk
);
641 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
643 * @tsk: the task with the latency
644 * @cpu: The cpu that initiated the trace.
646 * Flip the buffers between the @tr and the max_tr and record information
647 * about which task was the cause of this latency.
650 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
652 struct ring_buffer
*buf
= tr
->buffer
;
654 if (trace_stop_count
)
657 WARN_ON_ONCE(!irqs_disabled());
658 if (!current_trace
->use_max_tr
) {
662 arch_spin_lock(&ftrace_max_lock
);
664 tr
->buffer
= max_tr
.buffer
;
667 __update_max_tr(tr
, tsk
, cpu
);
668 arch_spin_unlock(&ftrace_max_lock
);
672 * update_max_tr_single - only copy one trace over, and reset the rest
674 * @tsk - task with the latency
675 * @cpu - the cpu of the buffer to copy.
677 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
680 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
684 if (trace_stop_count
)
687 WARN_ON_ONCE(!irqs_disabled());
688 if (!current_trace
->use_max_tr
) {
693 arch_spin_lock(&ftrace_max_lock
);
695 ftrace_disable_cpu();
697 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
701 * We failed to swap the buffer due to a commit taking
702 * place on this CPU. We fail to record, but we reset
703 * the max trace buffer (no one writes directly to it)
704 * and flag that it failed.
706 trace_array_printk(&max_tr
, _THIS_IP_
,
707 "Failed to swap buffers due to commit in progress\n");
712 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
&& ret
!= -EBUSY
);
714 __update_max_tr(tr
, tsk
, cpu
);
715 arch_spin_unlock(&ftrace_max_lock
);
717 #endif /* CONFIG_TRACER_MAX_TRACE */
720 * register_tracer - register a tracer with the ftrace system.
721 * @type - the plugin for the tracer
723 * Register a new plugin tracer.
725 int register_tracer(struct tracer
*type
)
726 __releases(kernel_lock
)
727 __acquires(kernel_lock
)
733 pr_info("Tracer must have a name\n");
737 if (strlen(type
->name
) >= MAX_TRACER_SIZE
) {
738 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE
);
742 mutex_lock(&trace_types_lock
);
744 tracing_selftest_running
= true;
746 for (t
= trace_types
; t
; t
= t
->next
) {
747 if (strcmp(type
->name
, t
->name
) == 0) {
749 pr_info("Tracer %s already registered\n",
757 type
->set_flag
= &dummy_set_flag
;
759 type
->flags
= &dummy_tracer_flags
;
761 if (!type
->flags
->opts
)
762 type
->flags
->opts
= dummy_tracer_opt
;
763 if (!type
->wait_pipe
)
764 type
->wait_pipe
= default_wait_pipe
;
767 #ifdef CONFIG_FTRACE_STARTUP_TEST
768 if (type
->selftest
&& !tracing_selftest_disabled
) {
769 struct tracer
*saved_tracer
= current_trace
;
770 struct trace_array
*tr
= &global_trace
;
773 * Run a selftest on this tracer.
774 * Here we reset the trace buffer, and set the current
775 * tracer to be this tracer. The tracer can then run some
776 * internal tracing to verify that everything is in order.
777 * If we fail, we do not register this tracer.
779 tracing_reset_online_cpus(tr
);
781 current_trace
= type
;
782 /* the test is responsible for initializing and enabling */
783 pr_info("Testing tracer %s: ", type
->name
);
784 ret
= type
->selftest(type
, tr
);
785 /* the test is responsible for resetting too */
786 current_trace
= saved_tracer
;
788 printk(KERN_CONT
"FAILED!\n");
791 /* Only reset on passing, to avoid touching corrupted buffers */
792 tracing_reset_online_cpus(tr
);
794 printk(KERN_CONT
"PASSED\n");
798 type
->next
= trace_types
;
802 tracing_selftest_running
= false;
803 mutex_unlock(&trace_types_lock
);
805 if (ret
|| !default_bootup_tracer
)
808 if (strncmp(default_bootup_tracer
, type
->name
, MAX_TRACER_SIZE
))
811 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
812 /* Do we want this tracer to start on bootup? */
813 tracing_set_tracer(type
->name
);
814 default_bootup_tracer
= NULL
;
815 /* disable other selftests, since this will break it. */
816 tracing_selftest_disabled
= 1;
817 #ifdef CONFIG_FTRACE_STARTUP_TEST
818 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
826 void unregister_tracer(struct tracer
*type
)
830 mutex_lock(&trace_types_lock
);
831 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
835 pr_info("Tracer %s not registered\n", type
->name
);
841 if (type
== current_trace
&& tracer_enabled
) {
844 if (current_trace
->stop
)
845 current_trace
->stop(&global_trace
);
846 current_trace
= &nop_trace
;
849 mutex_unlock(&trace_types_lock
);
852 static void __tracing_reset(struct ring_buffer
*buffer
, int cpu
)
854 ftrace_disable_cpu();
855 ring_buffer_reset_cpu(buffer
, cpu
);
859 void tracing_reset(struct trace_array
*tr
, int cpu
)
861 struct ring_buffer
*buffer
= tr
->buffer
;
863 ring_buffer_record_disable(buffer
);
865 /* Make sure all commits have finished */
867 __tracing_reset(buffer
, cpu
);
869 ring_buffer_record_enable(buffer
);
872 void tracing_reset_online_cpus(struct trace_array
*tr
)
874 struct ring_buffer
*buffer
= tr
->buffer
;
877 ring_buffer_record_disable(buffer
);
879 /* Make sure all commits have finished */
882 tr
->time_start
= ftrace_now(tr
->cpu
);
884 for_each_online_cpu(cpu
)
885 __tracing_reset(buffer
, cpu
);
887 ring_buffer_record_enable(buffer
);
890 void tracing_reset_current(int cpu
)
892 tracing_reset(&global_trace
, cpu
);
895 void tracing_reset_current_online_cpus(void)
897 tracing_reset_online_cpus(&global_trace
);
900 #define SAVED_CMDLINES 128
901 #define NO_CMDLINE_MAP UINT_MAX
902 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
903 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
904 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
905 static int cmdline_idx
;
906 static arch_spinlock_t trace_cmdline_lock
= __ARCH_SPIN_LOCK_UNLOCKED
;
908 /* temporary disable recording */
909 static atomic_t trace_record_cmdline_disabled __read_mostly
;
911 static void trace_init_cmdlines(void)
913 memset(&map_pid_to_cmdline
, NO_CMDLINE_MAP
, sizeof(map_pid_to_cmdline
));
914 memset(&map_cmdline_to_pid
, NO_CMDLINE_MAP
, sizeof(map_cmdline_to_pid
));
918 int is_tracing_stopped(void)
920 return trace_stop_count
;
924 * ftrace_off_permanent - disable all ftrace code permanently
926 * This should only be called when a serious anomally has
927 * been detected. This will turn off the function tracing,
928 * ring buffers, and other tracing utilites. It takes no
929 * locks and can be called from any context.
931 void ftrace_off_permanent(void)
933 tracing_disabled
= 1;
935 tracing_off_permanent();
939 * tracing_start - quick start of the tracer
941 * If tracing is enabled but was stopped by tracing_stop,
942 * this will start the tracer back up.
944 void tracing_start(void)
946 struct ring_buffer
*buffer
;
949 if (tracing_disabled
)
952 spin_lock_irqsave(&tracing_start_lock
, flags
);
953 if (--trace_stop_count
) {
954 if (trace_stop_count
< 0) {
955 /* Someone screwed up their debugging */
957 trace_stop_count
= 0;
962 /* Prevent the buffers from switching */
963 arch_spin_lock(&ftrace_max_lock
);
965 buffer
= global_trace
.buffer
;
967 ring_buffer_record_enable(buffer
);
969 buffer
= max_tr
.buffer
;
971 ring_buffer_record_enable(buffer
);
973 arch_spin_unlock(&ftrace_max_lock
);
977 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
981 * tracing_stop - quick stop of the tracer
983 * Light weight way to stop tracing. Use in conjunction with
986 void tracing_stop(void)
988 struct ring_buffer
*buffer
;
992 spin_lock_irqsave(&tracing_start_lock
, flags
);
993 if (trace_stop_count
++)
996 /* Prevent the buffers from switching */
997 arch_spin_lock(&ftrace_max_lock
);
999 buffer
= global_trace
.buffer
;
1001 ring_buffer_record_disable(buffer
);
1003 buffer
= max_tr
.buffer
;
1005 ring_buffer_record_disable(buffer
);
1007 arch_spin_unlock(&ftrace_max_lock
);
1010 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
1013 void trace_stop_cmdline_recording(void);
1015 static void trace_save_cmdline(struct task_struct
*tsk
)
1019 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
1023 * It's not the end of the world if we don't get
1024 * the lock, but we also don't want to spin
1025 * nor do we want to disable interrupts,
1026 * so if we miss here, then better luck next time.
1028 if (!arch_spin_trylock(&trace_cmdline_lock
))
1031 idx
= map_pid_to_cmdline
[tsk
->pid
];
1032 if (idx
== NO_CMDLINE_MAP
) {
1033 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
1036 * Check whether the cmdline buffer at idx has a pid
1037 * mapped. We are going to overwrite that entry so we
1038 * need to clear the map_pid_to_cmdline. Otherwise we
1039 * would read the new comm for the old pid.
1041 pid
= map_cmdline_to_pid
[idx
];
1042 if (pid
!= NO_CMDLINE_MAP
)
1043 map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
1045 map_cmdline_to_pid
[idx
] = tsk
->pid
;
1046 map_pid_to_cmdline
[tsk
->pid
] = idx
;
1051 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
1053 arch_spin_unlock(&trace_cmdline_lock
);
1056 void trace_find_cmdline(int pid
, char comm
[])
1061 strcpy(comm
, "<idle>");
1065 if (WARN_ON_ONCE(pid
< 0)) {
1066 strcpy(comm
, "<XXX>");
1070 if (pid
> PID_MAX_DEFAULT
) {
1071 strcpy(comm
, "<...>");
1076 arch_spin_lock(&trace_cmdline_lock
);
1077 map
= map_pid_to_cmdline
[pid
];
1078 if (map
!= NO_CMDLINE_MAP
)
1079 strcpy(comm
, saved_cmdlines
[map
]);
1081 strcpy(comm
, "<...>");
1083 arch_spin_unlock(&trace_cmdline_lock
);
1087 void tracing_record_cmdline(struct task_struct
*tsk
)
1089 if (atomic_read(&trace_record_cmdline_disabled
) || !tracer_enabled
||
1093 trace_save_cmdline(tsk
);
1097 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
1100 struct task_struct
*tsk
= current
;
1102 entry
->preempt_count
= pc
& 0xff;
1103 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
1104 entry
->lock_depth
= (tsk
) ? tsk
->lock_depth
: 0;
1106 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1107 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
1109 TRACE_FLAG_IRQS_NOSUPPORT
|
1111 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
1112 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
1113 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
1115 EXPORT_SYMBOL_GPL(tracing_generic_entry_update
);
1117 struct ring_buffer_event
*
1118 trace_buffer_lock_reserve(struct ring_buffer
*buffer
,
1121 unsigned long flags
, int pc
)
1123 struct ring_buffer_event
*event
;
1125 event
= ring_buffer_lock_reserve(buffer
, len
);
1126 if (event
!= NULL
) {
1127 struct trace_entry
*ent
= ring_buffer_event_data(event
);
1129 tracing_generic_entry_update(ent
, flags
, pc
);
1137 __trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1138 struct ring_buffer_event
*event
,
1139 unsigned long flags
, int pc
,
1142 ring_buffer_unlock_commit(buffer
, event
);
1144 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1145 ftrace_trace_userstack(buffer
, flags
, pc
);
1151 void trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1152 struct ring_buffer_event
*event
,
1153 unsigned long flags
, int pc
)
1155 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
1158 struct ring_buffer_event
*
1159 trace_current_buffer_lock_reserve(struct ring_buffer
**current_rb
,
1160 int type
, unsigned long len
,
1161 unsigned long flags
, int pc
)
1163 *current_rb
= global_trace
.buffer
;
1164 return trace_buffer_lock_reserve(*current_rb
,
1165 type
, len
, flags
, pc
);
1167 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve
);
1169 void trace_current_buffer_unlock_commit(struct ring_buffer
*buffer
,
1170 struct ring_buffer_event
*event
,
1171 unsigned long flags
, int pc
)
1173 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
1175 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit
);
1177 void trace_nowake_buffer_unlock_commit(struct ring_buffer
*buffer
,
1178 struct ring_buffer_event
*event
,
1179 unsigned long flags
, int pc
)
1181 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 0);
1183 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit
);
1185 void trace_current_buffer_discard_commit(struct ring_buffer
*buffer
,
1186 struct ring_buffer_event
*event
)
1188 ring_buffer_discard_commit(buffer
, event
);
1190 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
1193 trace_function(struct trace_array
*tr
,
1194 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1197 struct ftrace_event_call
*call
= &event_function
;
1198 struct ring_buffer
*buffer
= tr
->buffer
;
1199 struct ring_buffer_event
*event
;
1200 struct ftrace_entry
*entry
;
1202 /* If we are reading the ring buffer, don't trace */
1203 if (unlikely(__this_cpu_read(ftrace_cpu_disabled
)))
1206 event
= trace_buffer_lock_reserve(buffer
, TRACE_FN
, sizeof(*entry
),
1210 entry
= ring_buffer_event_data(event
);
1212 entry
->parent_ip
= parent_ip
;
1214 if (!filter_check_discard(call
, entry
, buffer
, event
))
1215 ring_buffer_unlock_commit(buffer
, event
);
1219 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
1220 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1223 if (likely(!atomic_read(&data
->disabled
)))
1224 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
1227 #ifdef CONFIG_STACKTRACE
1228 static void __ftrace_trace_stack(struct ring_buffer
*buffer
,
1229 unsigned long flags
,
1232 struct ftrace_event_call
*call
= &event_kernel_stack
;
1233 struct ring_buffer_event
*event
;
1234 struct stack_entry
*entry
;
1235 struct stack_trace trace
;
1237 event
= trace_buffer_lock_reserve(buffer
, TRACE_STACK
,
1238 sizeof(*entry
), flags
, pc
);
1241 entry
= ring_buffer_event_data(event
);
1242 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1244 trace
.nr_entries
= 0;
1245 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1247 trace
.entries
= entry
->caller
;
1249 save_stack_trace(&trace
);
1250 if (!filter_check_discard(call
, entry
, buffer
, event
))
1251 ring_buffer_unlock_commit(buffer
, event
);
1254 void ftrace_trace_stack(struct ring_buffer
*buffer
, unsigned long flags
,
1257 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1260 __ftrace_trace_stack(buffer
, flags
, skip
, pc
);
1263 void __trace_stack(struct trace_array
*tr
, unsigned long flags
, int skip
,
1266 __ftrace_trace_stack(tr
->buffer
, flags
, skip
, pc
);
1270 * trace_dump_stack - record a stack back trace in the trace buffer
1272 void trace_dump_stack(void)
1274 unsigned long flags
;
1276 if (tracing_disabled
|| tracing_selftest_running
)
1279 local_save_flags(flags
);
1281 /* skipping 3 traces, seems to get us at the caller of this function */
1282 __ftrace_trace_stack(global_trace
.buffer
, flags
, 3, preempt_count());
1285 static DEFINE_PER_CPU(int, user_stack_count
);
1288 ftrace_trace_userstack(struct ring_buffer
*buffer
, unsigned long flags
, int pc
)
1290 struct ftrace_event_call
*call
= &event_user_stack
;
1291 struct ring_buffer_event
*event
;
1292 struct userstack_entry
*entry
;
1293 struct stack_trace trace
;
1295 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1299 * NMIs can not handle page faults, even with fix ups.
1300 * The save user stack can (and often does) fault.
1302 if (unlikely(in_nmi()))
1306 * prevent recursion, since the user stack tracing may
1307 * trigger other kernel events.
1310 if (__this_cpu_read(user_stack_count
))
1313 __this_cpu_inc(user_stack_count
);
1315 event
= trace_buffer_lock_reserve(buffer
, TRACE_USER_STACK
,
1316 sizeof(*entry
), flags
, pc
);
1318 goto out_drop_count
;
1319 entry
= ring_buffer_event_data(event
);
1321 entry
->tgid
= current
->tgid
;
1322 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1324 trace
.nr_entries
= 0;
1325 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1327 trace
.entries
= entry
->caller
;
1329 save_stack_trace_user(&trace
);
1330 if (!filter_check_discard(call
, entry
, buffer
, event
))
1331 ring_buffer_unlock_commit(buffer
, event
);
1334 __this_cpu_dec(user_stack_count
);
1340 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1342 ftrace_trace_userstack(tr
, flags
, preempt_count());
1346 #endif /* CONFIG_STACKTRACE */
1349 * trace_vbprintk - write binary msg to tracing buffer
1352 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1354 static arch_spinlock_t trace_buf_lock
=
1355 (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
1356 static u32 trace_buf
[TRACE_BUF_SIZE
];
1358 struct ftrace_event_call
*call
= &event_bprint
;
1359 struct ring_buffer_event
*event
;
1360 struct ring_buffer
*buffer
;
1361 struct trace_array
*tr
= &global_trace
;
1362 struct trace_array_cpu
*data
;
1363 struct bprint_entry
*entry
;
1364 unsigned long flags
;
1366 int cpu
, len
= 0, size
, pc
;
1368 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1371 /* Don't pollute graph traces with trace_vprintk internals */
1372 pause_graph_tracing();
1374 pc
= preempt_count();
1375 preempt_disable_notrace();
1376 cpu
= raw_smp_processor_id();
1377 data
= tr
->data
[cpu
];
1379 disable
= atomic_inc_return(&data
->disabled
);
1380 if (unlikely(disable
!= 1))
1383 /* Lockdep uses trace_printk for lock tracing */
1384 local_irq_save(flags
);
1385 arch_spin_lock(&trace_buf_lock
);
1386 len
= vbin_printf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1388 if (len
> TRACE_BUF_SIZE
|| len
< 0)
1391 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1392 buffer
= tr
->buffer
;
1393 event
= trace_buffer_lock_reserve(buffer
, TRACE_BPRINT
, size
,
1397 entry
= ring_buffer_event_data(event
);
1401 memcpy(entry
->buf
, trace_buf
, sizeof(u32
) * len
);
1402 if (!filter_check_discard(call
, entry
, buffer
, event
)) {
1403 ring_buffer_unlock_commit(buffer
, event
);
1404 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1408 arch_spin_unlock(&trace_buf_lock
);
1409 local_irq_restore(flags
);
1412 atomic_dec_return(&data
->disabled
);
1413 preempt_enable_notrace();
1414 unpause_graph_tracing();
1418 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1420 int trace_array_printk(struct trace_array
*tr
,
1421 unsigned long ip
, const char *fmt
, ...)
1426 if (!(trace_flags
& TRACE_ITER_PRINTK
))
1430 ret
= trace_array_vprintk(tr
, ip
, fmt
, ap
);
1435 int trace_array_vprintk(struct trace_array
*tr
,
1436 unsigned long ip
, const char *fmt
, va_list args
)
1438 static arch_spinlock_t trace_buf_lock
= __ARCH_SPIN_LOCK_UNLOCKED
;
1439 static char trace_buf
[TRACE_BUF_SIZE
];
1441 struct ftrace_event_call
*call
= &event_print
;
1442 struct ring_buffer_event
*event
;
1443 struct ring_buffer
*buffer
;
1444 struct trace_array_cpu
*data
;
1445 int cpu
, len
= 0, size
, pc
;
1446 struct print_entry
*entry
;
1447 unsigned long irq_flags
;
1450 if (tracing_disabled
|| tracing_selftest_running
)
1453 pc
= preempt_count();
1454 preempt_disable_notrace();
1455 cpu
= raw_smp_processor_id();
1456 data
= tr
->data
[cpu
];
1458 disable
= atomic_inc_return(&data
->disabled
);
1459 if (unlikely(disable
!= 1))
1462 pause_graph_tracing();
1463 raw_local_irq_save(irq_flags
);
1464 arch_spin_lock(&trace_buf_lock
);
1465 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1467 size
= sizeof(*entry
) + len
+ 1;
1468 buffer
= tr
->buffer
;
1469 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
1473 entry
= ring_buffer_event_data(event
);
1476 memcpy(&entry
->buf
, trace_buf
, len
);
1477 entry
->buf
[len
] = '\0';
1478 if (!filter_check_discard(call
, entry
, buffer
, event
)) {
1479 ring_buffer_unlock_commit(buffer
, event
);
1480 ftrace_trace_stack(buffer
, irq_flags
, 6, pc
);
1484 arch_spin_unlock(&trace_buf_lock
);
1485 raw_local_irq_restore(irq_flags
);
1486 unpause_graph_tracing();
1488 atomic_dec_return(&data
->disabled
);
1489 preempt_enable_notrace();
1494 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1496 return trace_array_vprintk(&global_trace
, ip
, fmt
, args
);
1498 EXPORT_SYMBOL_GPL(trace_vprintk
);
1500 static void trace_iterator_increment(struct trace_iterator
*iter
)
1502 /* Don't allow ftrace to trace into the ring buffers */
1503 ftrace_disable_cpu();
1506 if (iter
->buffer_iter
[iter
->cpu
])
1507 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1509 ftrace_enable_cpu();
1512 static struct trace_entry
*
1513 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
,
1514 unsigned long *lost_events
)
1516 struct ring_buffer_event
*event
;
1517 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1519 /* Don't allow ftrace to trace into the ring buffers */
1520 ftrace_disable_cpu();
1523 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1525 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
,
1528 ftrace_enable_cpu();
1530 return event
? ring_buffer_event_data(event
) : NULL
;
1533 static struct trace_entry
*
1534 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
,
1535 unsigned long *missing_events
, u64
*ent_ts
)
1537 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1538 struct trace_entry
*ent
, *next
= NULL
;
1539 unsigned long lost_events
= 0, next_lost
= 0;
1540 int cpu_file
= iter
->cpu_file
;
1541 u64 next_ts
= 0, ts
;
1546 * If we are in a per_cpu trace file, don't bother by iterating over
1547 * all cpu and peek directly.
1549 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1550 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1552 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
, missing_events
);
1554 *ent_cpu
= cpu_file
;
1559 for_each_tracing_cpu(cpu
) {
1561 if (ring_buffer_empty_cpu(buffer
, cpu
))
1564 ent
= peek_next_entry(iter
, cpu
, &ts
, &lost_events
);
1567 * Pick the entry with the smallest timestamp:
1569 if (ent
&& (!next
|| ts
< next_ts
)) {
1573 next_lost
= lost_events
;
1578 *ent_cpu
= next_cpu
;
1584 *missing_events
= next_lost
;
1589 /* Find the next real entry, without updating the iterator itself */
1590 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1591 int *ent_cpu
, u64
*ent_ts
)
1593 return __find_next_entry(iter
, ent_cpu
, NULL
, ent_ts
);
1596 /* Find the next real entry, and increment the iterator to the next entry */
1597 void *trace_find_next_entry_inc(struct trace_iterator
*iter
)
1599 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
,
1600 &iter
->lost_events
, &iter
->ts
);
1603 trace_iterator_increment(iter
);
1605 return iter
->ent
? iter
: NULL
;
1608 static void trace_consume(struct trace_iterator
*iter
)
1610 /* Don't allow ftrace to trace into the ring buffers */
1611 ftrace_disable_cpu();
1612 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
,
1613 &iter
->lost_events
);
1614 ftrace_enable_cpu();
1617 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1619 struct trace_iterator
*iter
= m
->private;
1623 WARN_ON_ONCE(iter
->leftover
);
1627 /* can't go backwards */
1632 ent
= trace_find_next_entry_inc(iter
);
1636 while (ent
&& iter
->idx
< i
)
1637 ent
= trace_find_next_entry_inc(iter
);
1644 void tracing_iter_reset(struct trace_iterator
*iter
, int cpu
)
1646 struct trace_array
*tr
= iter
->tr
;
1647 struct ring_buffer_event
*event
;
1648 struct ring_buffer_iter
*buf_iter
;
1649 unsigned long entries
= 0;
1652 tr
->data
[cpu
]->skipped_entries
= 0;
1654 if (!iter
->buffer_iter
[cpu
])
1657 buf_iter
= iter
->buffer_iter
[cpu
];
1658 ring_buffer_iter_reset(buf_iter
);
1661 * We could have the case with the max latency tracers
1662 * that a reset never took place on a cpu. This is evident
1663 * by the timestamp being before the start of the buffer.
1665 while ((event
= ring_buffer_iter_peek(buf_iter
, &ts
))) {
1666 if (ts
>= iter
->tr
->time_start
)
1669 ring_buffer_read(buf_iter
, NULL
);
1672 tr
->data
[cpu
]->skipped_entries
= entries
;
1676 * The current tracer is copied to avoid a global locking
1679 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1681 struct trace_iterator
*iter
= m
->private;
1682 static struct tracer
*old_tracer
;
1683 int cpu_file
= iter
->cpu_file
;
1688 /* copy the tracer to avoid using a global lock all around */
1689 mutex_lock(&trace_types_lock
);
1690 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1691 old_tracer
= current_trace
;
1692 *iter
->trace
= *current_trace
;
1694 mutex_unlock(&trace_types_lock
);
1696 atomic_inc(&trace_record_cmdline_disabled
);
1698 if (*pos
!= iter
->pos
) {
1703 ftrace_disable_cpu();
1705 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1706 for_each_tracing_cpu(cpu
)
1707 tracing_iter_reset(iter
, cpu
);
1709 tracing_iter_reset(iter
, cpu_file
);
1711 ftrace_enable_cpu();
1714 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1719 * If we overflowed the seq_file before, then we want
1720 * to just reuse the trace_seq buffer again.
1726 p
= s_next(m
, p
, &l
);
1730 trace_event_read_lock();
1731 trace_access_lock(cpu_file
);
1735 static void s_stop(struct seq_file
*m
, void *p
)
1737 struct trace_iterator
*iter
= m
->private;
1739 atomic_dec(&trace_record_cmdline_disabled
);
1740 trace_access_unlock(iter
->cpu_file
);
1741 trace_event_read_unlock();
1744 static void print_lat_help_header(struct seq_file
*m
)
1746 seq_puts(m
, "# _------=> CPU# \n");
1747 seq_puts(m
, "# / _-----=> irqs-off \n");
1748 seq_puts(m
, "# | / _----=> need-resched \n");
1749 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1750 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1751 seq_puts(m
, "# |||| /_--=> lock-depth \n");
1752 seq_puts(m
, "# |||||/ delay \n");
1753 seq_puts(m
, "# cmd pid |||||| time | caller \n");
1754 seq_puts(m
, "# \\ / |||||| \\ | / \n");
1757 static void print_func_help_header(struct seq_file
*m
)
1759 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1760 seq_puts(m
, "# | | | | |\n");
1765 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1767 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1768 struct trace_array
*tr
= iter
->tr
;
1769 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1770 struct tracer
*type
= current_trace
;
1771 unsigned long entries
= 0;
1772 unsigned long total
= 0;
1773 unsigned long count
;
1774 const char *name
= "preemption";
1781 for_each_tracing_cpu(cpu
) {
1782 count
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
1784 * If this buffer has skipped entries, then we hold all
1785 * entries for the trace and we need to ignore the
1786 * ones before the time stamp.
1788 if (tr
->data
[cpu
]->skipped_entries
) {
1789 count
-= tr
->data
[cpu
]->skipped_entries
;
1790 /* total is the same as the entries */
1794 ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
1798 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
1800 seq_puts(m
, "# -----------------------------------"
1801 "---------------------------------\n");
1802 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1803 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1804 nsecs_to_usecs(data
->saved_latency
),
1808 #if defined(CONFIG_PREEMPT_NONE)
1810 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1812 #elif defined(CONFIG_PREEMPT)
1817 /* These are reserved for later use */
1820 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1824 seq_puts(m
, "# -----------------\n");
1825 seq_printf(m
, "# | task: %.16s-%d "
1826 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1827 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1828 data
->policy
, data
->rt_priority
);
1829 seq_puts(m
, "# -----------------\n");
1831 if (data
->critical_start
) {
1832 seq_puts(m
, "# => started at: ");
1833 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1834 trace_print_seq(m
, &iter
->seq
);
1835 seq_puts(m
, "\n# => ended at: ");
1836 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1837 trace_print_seq(m
, &iter
->seq
);
1838 seq_puts(m
, "\n#\n");
1844 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1846 struct trace_seq
*s
= &iter
->seq
;
1848 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1851 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1854 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1857 if (iter
->tr
->data
[iter
->cpu
]->skipped_entries
)
1860 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1862 /* Don't print started cpu buffer for the first entry of the trace */
1864 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
1868 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1870 struct trace_seq
*s
= &iter
->seq
;
1871 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1872 struct trace_entry
*entry
;
1873 struct trace_event
*event
;
1877 test_cpu_buff_start(iter
);
1879 event
= ftrace_find_event(entry
->type
);
1881 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1882 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1883 if (!trace_print_lat_context(iter
))
1886 if (!trace_print_context(iter
))
1892 return event
->funcs
->trace(iter
, sym_flags
, event
);
1894 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1897 return TRACE_TYPE_HANDLED
;
1899 return TRACE_TYPE_PARTIAL_LINE
;
1902 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1904 struct trace_seq
*s
= &iter
->seq
;
1905 struct trace_entry
*entry
;
1906 struct trace_event
*event
;
1910 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1911 if (!trace_seq_printf(s
, "%d %d %llu ",
1912 entry
->pid
, iter
->cpu
, iter
->ts
))
1916 event
= ftrace_find_event(entry
->type
);
1918 return event
->funcs
->raw(iter
, 0, event
);
1920 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1923 return TRACE_TYPE_HANDLED
;
1925 return TRACE_TYPE_PARTIAL_LINE
;
1928 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1930 struct trace_seq
*s
= &iter
->seq
;
1931 unsigned char newline
= '\n';
1932 struct trace_entry
*entry
;
1933 struct trace_event
*event
;
1937 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1938 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1939 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1940 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1943 event
= ftrace_find_event(entry
->type
);
1945 enum print_line_t ret
= event
->funcs
->hex(iter
, 0, event
);
1946 if (ret
!= TRACE_TYPE_HANDLED
)
1950 SEQ_PUT_FIELD_RET(s
, newline
);
1952 return TRACE_TYPE_HANDLED
;
1955 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1957 struct trace_seq
*s
= &iter
->seq
;
1958 struct trace_entry
*entry
;
1959 struct trace_event
*event
;
1963 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1964 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1965 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1966 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1969 event
= ftrace_find_event(entry
->type
);
1970 return event
? event
->funcs
->binary(iter
, 0, event
) :
1974 int trace_empty(struct trace_iterator
*iter
)
1978 /* If we are looking at one CPU buffer, only check that one */
1979 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
1980 cpu
= iter
->cpu_file
;
1981 if (iter
->buffer_iter
[cpu
]) {
1982 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1985 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1991 for_each_tracing_cpu(cpu
) {
1992 if (iter
->buffer_iter
[cpu
]) {
1993 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1996 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
2004 /* Called with trace_event_read_lock() held. */
2005 enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
2007 enum print_line_t ret
;
2009 if (iter
->lost_events
)
2010 trace_seq_printf(&iter
->seq
, "CPU:%d [LOST %lu EVENTS]\n",
2011 iter
->cpu
, iter
->lost_events
);
2013 if (iter
->trace
&& iter
->trace
->print_line
) {
2014 ret
= iter
->trace
->print_line(iter
);
2015 if (ret
!= TRACE_TYPE_UNHANDLED
)
2019 if (iter
->ent
->type
== TRACE_BPRINT
&&
2020 trace_flags
& TRACE_ITER_PRINTK
&&
2021 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2022 return trace_print_bprintk_msg_only(iter
);
2024 if (iter
->ent
->type
== TRACE_PRINT
&&
2025 trace_flags
& TRACE_ITER_PRINTK
&&
2026 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2027 return trace_print_printk_msg_only(iter
);
2029 if (trace_flags
& TRACE_ITER_BIN
)
2030 return print_bin_fmt(iter
);
2032 if (trace_flags
& TRACE_ITER_HEX
)
2033 return print_hex_fmt(iter
);
2035 if (trace_flags
& TRACE_ITER_RAW
)
2036 return print_raw_fmt(iter
);
2038 return print_trace_fmt(iter
);
2041 void trace_default_header(struct seq_file
*m
)
2043 struct trace_iterator
*iter
= m
->private;
2045 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
2046 /* print nothing if the buffers are empty */
2047 if (trace_empty(iter
))
2049 print_trace_header(m
, iter
);
2050 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2051 print_lat_help_header(m
);
2053 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2054 print_func_help_header(m
);
2058 static int s_show(struct seq_file
*m
, void *v
)
2060 struct trace_iterator
*iter
= v
;
2063 if (iter
->ent
== NULL
) {
2065 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
2068 if (iter
->trace
&& iter
->trace
->print_header
)
2069 iter
->trace
->print_header(m
);
2071 trace_default_header(m
);
2073 } else if (iter
->leftover
) {
2075 * If we filled the seq_file buffer earlier, we
2076 * want to just show it now.
2078 ret
= trace_print_seq(m
, &iter
->seq
);
2080 /* ret should this time be zero, but you never know */
2081 iter
->leftover
= ret
;
2084 print_trace_line(iter
);
2085 ret
= trace_print_seq(m
, &iter
->seq
);
2087 * If we overflow the seq_file buffer, then it will
2088 * ask us for this data again at start up.
2090 * ret is 0 if seq_file write succeeded.
2093 iter
->leftover
= ret
;
2099 static const struct seq_operations tracer_seq_ops
= {
2106 static struct trace_iterator
*
2107 __tracing_open(struct inode
*inode
, struct file
*file
)
2109 long cpu_file
= (long) inode
->i_private
;
2110 void *fail_ret
= ERR_PTR(-ENOMEM
);
2111 struct trace_iterator
*iter
;
2115 if (tracing_disabled
)
2116 return ERR_PTR(-ENODEV
);
2118 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2120 return ERR_PTR(-ENOMEM
);
2123 * We make a copy of the current tracer to avoid concurrent
2124 * changes on it while we are reading.
2126 mutex_lock(&trace_types_lock
);
2127 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2132 *iter
->trace
= *current_trace
;
2134 if (!zalloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
2137 if (current_trace
&& current_trace
->print_max
)
2140 iter
->tr
= &global_trace
;
2142 mutex_init(&iter
->mutex
);
2143 iter
->cpu_file
= cpu_file
;
2145 /* Notify the tracer early; before we stop tracing. */
2146 if (iter
->trace
&& iter
->trace
->open
)
2147 iter
->trace
->open(iter
);
2149 /* Annotate start of buffers if we had overruns */
2150 if (ring_buffer_overruns(iter
->tr
->buffer
))
2151 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
2153 /* stop the trace while dumping */
2156 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
2157 for_each_tracing_cpu(cpu
) {
2158 iter
->buffer_iter
[cpu
] =
2159 ring_buffer_read_prepare(iter
->tr
->buffer
, cpu
);
2161 ring_buffer_read_prepare_sync();
2162 for_each_tracing_cpu(cpu
) {
2163 ring_buffer_read_start(iter
->buffer_iter
[cpu
]);
2164 tracing_iter_reset(iter
, cpu
);
2167 cpu
= iter
->cpu_file
;
2168 iter
->buffer_iter
[cpu
] =
2169 ring_buffer_read_prepare(iter
->tr
->buffer
, cpu
);
2170 ring_buffer_read_prepare_sync();
2171 ring_buffer_read_start(iter
->buffer_iter
[cpu
]);
2172 tracing_iter_reset(iter
, cpu
);
2175 ret
= seq_open(file
, &tracer_seq_ops
);
2177 fail_ret
= ERR_PTR(ret
);
2181 m
= file
->private_data
;
2184 mutex_unlock(&trace_types_lock
);
2189 for_each_tracing_cpu(cpu
) {
2190 if (iter
->buffer_iter
[cpu
])
2191 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2193 free_cpumask_var(iter
->started
);
2196 mutex_unlock(&trace_types_lock
);
2203 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
2205 if (tracing_disabled
)
2208 filp
->private_data
= inode
->i_private
;
2212 static int tracing_release(struct inode
*inode
, struct file
*file
)
2214 struct seq_file
*m
= file
->private_data
;
2215 struct trace_iterator
*iter
;
2218 if (!(file
->f_mode
& FMODE_READ
))
2223 mutex_lock(&trace_types_lock
);
2224 for_each_tracing_cpu(cpu
) {
2225 if (iter
->buffer_iter
[cpu
])
2226 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2229 if (iter
->trace
&& iter
->trace
->close
)
2230 iter
->trace
->close(iter
);
2232 /* reenable tracing if it was previously enabled */
2234 mutex_unlock(&trace_types_lock
);
2236 seq_release(inode
, file
);
2237 mutex_destroy(&iter
->mutex
);
2238 free_cpumask_var(iter
->started
);
2244 static int tracing_open(struct inode
*inode
, struct file
*file
)
2246 struct trace_iterator
*iter
;
2249 /* If this file was open for write, then erase contents */
2250 if ((file
->f_mode
& FMODE_WRITE
) &&
2251 (file
->f_flags
& O_TRUNC
)) {
2252 long cpu
= (long) inode
->i_private
;
2254 if (cpu
== TRACE_PIPE_ALL_CPU
)
2255 tracing_reset_online_cpus(&global_trace
);
2257 tracing_reset(&global_trace
, cpu
);
2260 if (file
->f_mode
& FMODE_READ
) {
2261 iter
= __tracing_open(inode
, file
);
2263 ret
= PTR_ERR(iter
);
2264 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2265 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2271 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2273 struct tracer
*t
= v
;
2283 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2288 mutex_lock(&trace_types_lock
);
2289 for (t
= trace_types
; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2295 static void t_stop(struct seq_file
*m
, void *p
)
2297 mutex_unlock(&trace_types_lock
);
2300 static int t_show(struct seq_file
*m
, void *v
)
2302 struct tracer
*t
= v
;
2307 seq_printf(m
, "%s", t
->name
);
2316 static const struct seq_operations show_traces_seq_ops
= {
2323 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2325 if (tracing_disabled
)
2328 return seq_open(file
, &show_traces_seq_ops
);
2332 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
2333 size_t count
, loff_t
*ppos
)
2338 static loff_t
tracing_seek(struct file
*file
, loff_t offset
, int origin
)
2340 if (file
->f_mode
& FMODE_READ
)
2341 return seq_lseek(file
, offset
, origin
);
2346 static const struct file_operations tracing_fops
= {
2347 .open
= tracing_open
,
2349 .write
= tracing_write_stub
,
2350 .llseek
= tracing_seek
,
2351 .release
= tracing_release
,
2354 static const struct file_operations show_traces_fops
= {
2355 .open
= show_traces_open
,
2357 .release
= seq_release
,
2358 .llseek
= seq_lseek
,
2362 * Only trace on a CPU if the bitmask is set:
2364 static cpumask_var_t tracing_cpumask
;
2367 * The tracer itself will not take this lock, but still we want
2368 * to provide a consistent cpumask to user-space:
2370 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2373 * Temporary storage for the character representation of the
2374 * CPU bitmask (and one more byte for the newline):
2376 static char mask_str
[NR_CPUS
+ 1];
2379 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2380 size_t count
, loff_t
*ppos
)
2384 mutex_lock(&tracing_cpumask_update_lock
);
2386 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2387 if (count
- len
< 2) {
2391 len
+= sprintf(mask_str
+ len
, "\n");
2392 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2395 mutex_unlock(&tracing_cpumask_update_lock
);
2401 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2402 size_t count
, loff_t
*ppos
)
2405 cpumask_var_t tracing_cpumask_new
;
2407 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2410 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2414 mutex_lock(&tracing_cpumask_update_lock
);
2416 local_irq_disable();
2417 arch_spin_lock(&ftrace_max_lock
);
2418 for_each_tracing_cpu(cpu
) {
2420 * Increase/decrease the disabled counter if we are
2421 * about to flip a bit in the cpumask:
2423 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2424 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2425 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2427 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2428 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2429 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2432 arch_spin_unlock(&ftrace_max_lock
);
2435 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2437 mutex_unlock(&tracing_cpumask_update_lock
);
2438 free_cpumask_var(tracing_cpumask_new
);
2443 free_cpumask_var(tracing_cpumask_new
);
2448 static const struct file_operations tracing_cpumask_fops
= {
2449 .open
= tracing_open_generic
,
2450 .read
= tracing_cpumask_read
,
2451 .write
= tracing_cpumask_write
,
2452 .llseek
= generic_file_llseek
,
2455 static int tracing_trace_options_show(struct seq_file
*m
, void *v
)
2457 struct tracer_opt
*trace_opts
;
2461 mutex_lock(&trace_types_lock
);
2462 tracer_flags
= current_trace
->flags
->val
;
2463 trace_opts
= current_trace
->flags
->opts
;
2465 for (i
= 0; trace_options
[i
]; i
++) {
2466 if (trace_flags
& (1 << i
))
2467 seq_printf(m
, "%s\n", trace_options
[i
]);
2469 seq_printf(m
, "no%s\n", trace_options
[i
]);
2472 for (i
= 0; trace_opts
[i
].name
; i
++) {
2473 if (tracer_flags
& trace_opts
[i
].bit
)
2474 seq_printf(m
, "%s\n", trace_opts
[i
].name
);
2476 seq_printf(m
, "no%s\n", trace_opts
[i
].name
);
2478 mutex_unlock(&trace_types_lock
);
2483 static int __set_tracer_option(struct tracer
*trace
,
2484 struct tracer_flags
*tracer_flags
,
2485 struct tracer_opt
*opts
, int neg
)
2489 ret
= trace
->set_flag(tracer_flags
->val
, opts
->bit
, !neg
);
2494 tracer_flags
->val
&= ~opts
->bit
;
2496 tracer_flags
->val
|= opts
->bit
;
2500 /* Try to assign a tracer specific option */
2501 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2503 struct tracer_flags
*tracer_flags
= trace
->flags
;
2504 struct tracer_opt
*opts
= NULL
;
2507 for (i
= 0; tracer_flags
->opts
[i
].name
; i
++) {
2508 opts
= &tracer_flags
->opts
[i
];
2510 if (strcmp(cmp
, opts
->name
) == 0)
2511 return __set_tracer_option(trace
, trace
->flags
,
2518 static void set_tracer_flags(unsigned int mask
, int enabled
)
2520 /* do nothing if flag is already set */
2521 if (!!(trace_flags
& mask
) == !!enabled
)
2525 trace_flags
|= mask
;
2527 trace_flags
&= ~mask
;
2529 if (mask
== TRACE_ITER_RECORD_CMD
)
2530 trace_event_enable_cmd_record(enabled
);
2532 if (mask
== TRACE_ITER_OVERWRITE
)
2533 ring_buffer_change_overwrite(global_trace
.buffer
, enabled
);
2537 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2538 size_t cnt
, loff_t
*ppos
)
2546 if (cnt
>= sizeof(buf
))
2549 if (copy_from_user(&buf
, ubuf
, cnt
))
2553 cmp
= strstrip(buf
);
2555 if (strncmp(cmp
, "no", 2) == 0) {
2560 for (i
= 0; trace_options
[i
]; i
++) {
2561 if (strcmp(cmp
, trace_options
[i
]) == 0) {
2562 set_tracer_flags(1 << i
, !neg
);
2567 /* If no option could be set, test the specific tracer options */
2568 if (!trace_options
[i
]) {
2569 mutex_lock(&trace_types_lock
);
2570 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2571 mutex_unlock(&trace_types_lock
);
2581 static int tracing_trace_options_open(struct inode
*inode
, struct file
*file
)
2583 if (tracing_disabled
)
2585 return single_open(file
, tracing_trace_options_show
, NULL
);
2588 static const struct file_operations tracing_iter_fops
= {
2589 .open
= tracing_trace_options_open
,
2591 .llseek
= seq_lseek
,
2592 .release
= single_release
,
2593 .write
= tracing_trace_options_write
,
2596 static const char readme_msg
[] =
2597 "tracing mini-HOWTO:\n\n"
2598 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2599 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2600 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2601 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2603 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2604 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2606 "# cat /sys/kernel/debug/tracing/trace_options\n"
2607 "noprint-parent nosym-offset nosym-addr noverbose\n"
2608 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2609 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2610 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2611 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2615 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2616 size_t cnt
, loff_t
*ppos
)
2618 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2619 readme_msg
, strlen(readme_msg
));
2622 static const struct file_operations tracing_readme_fops
= {
2623 .open
= tracing_open_generic
,
2624 .read
= tracing_readme_read
,
2625 .llseek
= generic_file_llseek
,
2629 tracing_saved_cmdlines_read(struct file
*file
, char __user
*ubuf
,
2630 size_t cnt
, loff_t
*ppos
)
2639 file_buf
= kmalloc(SAVED_CMDLINES
*(16+TASK_COMM_LEN
), GFP_KERNEL
);
2643 buf_comm
= kmalloc(TASK_COMM_LEN
, GFP_KERNEL
);
2651 for (i
= 0; i
< SAVED_CMDLINES
; i
++) {
2654 pid
= map_cmdline_to_pid
[i
];
2655 if (pid
== -1 || pid
== NO_CMDLINE_MAP
)
2658 trace_find_cmdline(pid
, buf_comm
);
2659 r
= sprintf(buf
, "%d %s\n", pid
, buf_comm
);
2664 len
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
2673 static const struct file_operations tracing_saved_cmdlines_fops
= {
2674 .open
= tracing_open_generic
,
2675 .read
= tracing_saved_cmdlines_read
,
2676 .llseek
= generic_file_llseek
,
2680 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2681 size_t cnt
, loff_t
*ppos
)
2686 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2687 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2691 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2692 size_t cnt
, loff_t
*ppos
)
2694 struct trace_array
*tr
= filp
->private_data
;
2699 if (cnt
>= sizeof(buf
))
2702 if (copy_from_user(&buf
, ubuf
, cnt
))
2707 ret
= strict_strtoul(buf
, 10, &val
);
2713 mutex_lock(&trace_types_lock
);
2714 if (tracer_enabled
^ val
) {
2716 /* Only need to warn if this is used to change the state */
2717 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
2721 if (current_trace
->start
)
2722 current_trace
->start(tr
);
2727 if (current_trace
->stop
)
2728 current_trace
->stop(tr
);
2731 mutex_unlock(&trace_types_lock
);
2739 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2740 size_t cnt
, loff_t
*ppos
)
2742 char buf
[MAX_TRACER_SIZE
+2];
2745 mutex_lock(&trace_types_lock
);
2747 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2749 r
= sprintf(buf
, "\n");
2750 mutex_unlock(&trace_types_lock
);
2752 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2755 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2757 tracing_reset_online_cpus(tr
);
2761 static int tracing_resize_ring_buffer(unsigned long size
)
2766 * If kernel or user changes the size of the ring buffer
2767 * we use the size that was given, and we can forget about
2768 * expanding it later.
2770 ring_buffer_expanded
= 1;
2772 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2776 if (!current_trace
->use_max_tr
)
2779 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2783 r
= ring_buffer_resize(global_trace
.buffer
,
2784 global_trace
.entries
);
2787 * AARGH! We are left with different
2788 * size max buffer!!!!
2789 * The max buffer is our "snapshot" buffer.
2790 * When a tracer needs a snapshot (one of the
2791 * latency tracers), it swaps the max buffer
2792 * with the saved snap shot. We succeeded to
2793 * update the size of the main buffer, but failed to
2794 * update the size of the max buffer. But when we tried
2795 * to reset the main buffer to the original size, we
2796 * failed there too. This is very unlikely to
2797 * happen, but if it does, warn and kill all
2801 tracing_disabled
= 1;
2806 max_tr
.entries
= size
;
2808 global_trace
.entries
= size
;
2815 * tracing_update_buffers - used by tracing facility to expand ring buffers
2817 * To save on memory when the tracing is never used on a system with it
2818 * configured in. The ring buffers are set to a minimum size. But once
2819 * a user starts to use the tracing facility, then they need to grow
2820 * to their default size.
2822 * This function is to be called when a tracer is about to be used.
2824 int tracing_update_buffers(void)
2828 mutex_lock(&trace_types_lock
);
2829 if (!ring_buffer_expanded
)
2830 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2831 mutex_unlock(&trace_types_lock
);
2836 struct trace_option_dentry
;
2838 static struct trace_option_dentry
*
2839 create_trace_option_files(struct tracer
*tracer
);
2842 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2844 static int tracing_set_tracer(const char *buf
)
2846 static struct trace_option_dentry
*topts
;
2847 struct trace_array
*tr
= &global_trace
;
2851 mutex_lock(&trace_types_lock
);
2853 if (!ring_buffer_expanded
) {
2854 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2860 for (t
= trace_types
; t
; t
= t
->next
) {
2861 if (strcmp(t
->name
, buf
) == 0)
2868 if (t
== current_trace
)
2871 trace_branch_disable();
2872 if (current_trace
&& current_trace
->reset
)
2873 current_trace
->reset(tr
);
2874 if (current_trace
&& current_trace
->use_max_tr
) {
2876 * We don't free the ring buffer. instead, resize it because
2877 * The max_tr ring buffer has some state (e.g. ring->clock) and
2878 * we want preserve it.
2880 ring_buffer_resize(max_tr
.buffer
, 1);
2883 destroy_trace_option_files(topts
);
2887 topts
= create_trace_option_files(current_trace
);
2888 if (current_trace
->use_max_tr
) {
2889 ret
= ring_buffer_resize(max_tr
.buffer
, global_trace
.entries
);
2892 max_tr
.entries
= global_trace
.entries
;
2896 ret
= tracer_init(t
, tr
);
2901 trace_branch_enable(tr
);
2903 mutex_unlock(&trace_types_lock
);
2909 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2910 size_t cnt
, loff_t
*ppos
)
2912 char buf
[MAX_TRACER_SIZE
+1];
2919 if (cnt
> MAX_TRACER_SIZE
)
2920 cnt
= MAX_TRACER_SIZE
;
2922 if (copy_from_user(&buf
, ubuf
, cnt
))
2927 /* strip ending whitespace. */
2928 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2931 err
= tracing_set_tracer(buf
);
2941 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2942 size_t cnt
, loff_t
*ppos
)
2944 unsigned long *ptr
= filp
->private_data
;
2948 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2949 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2950 if (r
> sizeof(buf
))
2952 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2956 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2957 size_t cnt
, loff_t
*ppos
)
2959 unsigned long *ptr
= filp
->private_data
;
2964 if (cnt
>= sizeof(buf
))
2967 if (copy_from_user(&buf
, ubuf
, cnt
))
2972 ret
= strict_strtoul(buf
, 10, &val
);
2981 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2983 long cpu_file
= (long) inode
->i_private
;
2984 struct trace_iterator
*iter
;
2987 if (tracing_disabled
)
2990 mutex_lock(&trace_types_lock
);
2992 /* create a buffer to store the information to pass to userspace */
2993 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
3000 * We make a copy of the current tracer to avoid concurrent
3001 * changes on it while we are reading.
3003 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
3009 *iter
->trace
= *current_trace
;
3011 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
3016 /* trace pipe does not show start of buffer */
3017 cpumask_setall(iter
->started
);
3019 if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
3020 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
3022 iter
->cpu_file
= cpu_file
;
3023 iter
->tr
= &global_trace
;
3024 mutex_init(&iter
->mutex
);
3025 filp
->private_data
= iter
;
3027 if (iter
->trace
->pipe_open
)
3028 iter
->trace
->pipe_open(iter
);
3030 nonseekable_open(inode
, filp
);
3032 mutex_unlock(&trace_types_lock
);
3038 mutex_unlock(&trace_types_lock
);
3042 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
3044 struct trace_iterator
*iter
= file
->private_data
;
3046 mutex_lock(&trace_types_lock
);
3048 if (iter
->trace
->pipe_close
)
3049 iter
->trace
->pipe_close(iter
);
3051 mutex_unlock(&trace_types_lock
);
3053 free_cpumask_var(iter
->started
);
3054 mutex_destroy(&iter
->mutex
);
3062 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
3064 struct trace_iterator
*iter
= filp
->private_data
;
3066 if (trace_flags
& TRACE_ITER_BLOCK
) {
3068 * Always select as readable when in blocking mode
3070 return POLLIN
| POLLRDNORM
;
3072 if (!trace_empty(iter
))
3073 return POLLIN
| POLLRDNORM
;
3074 poll_wait(filp
, &trace_wait
, poll_table
);
3075 if (!trace_empty(iter
))
3076 return POLLIN
| POLLRDNORM
;
3083 void default_wait_pipe(struct trace_iterator
*iter
)
3087 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
3089 if (trace_empty(iter
))
3092 finish_wait(&trace_wait
, &wait
);
3096 * This is a make-shift waitqueue.
3097 * A tracer might use this callback on some rare cases:
3099 * 1) the current tracer might hold the runqueue lock when it wakes up
3100 * a reader, hence a deadlock (sched, function, and function graph tracers)
3101 * 2) the function tracers, trace all functions, we don't want
3102 * the overhead of calling wake_up and friends
3103 * (and tracing them too)
3105 * Anyway, this is really very primitive wakeup.
3107 void poll_wait_pipe(struct trace_iterator
*iter
)
3109 set_current_state(TASK_INTERRUPTIBLE
);
3110 /* sleep for 100 msecs, and try again. */
3111 schedule_timeout(HZ
/ 10);
3114 /* Must be called with trace_types_lock mutex held. */
3115 static int tracing_wait_pipe(struct file
*filp
)
3117 struct trace_iterator
*iter
= filp
->private_data
;
3119 while (trace_empty(iter
)) {
3121 if ((filp
->f_flags
& O_NONBLOCK
)) {
3125 mutex_unlock(&iter
->mutex
);
3127 iter
->trace
->wait_pipe(iter
);
3129 mutex_lock(&iter
->mutex
);
3131 if (signal_pending(current
))
3135 * We block until we read something and tracing is disabled.
3136 * We still block if tracing is disabled, but we have never
3137 * read anything. This allows a user to cat this file, and
3138 * then enable tracing. But after we have read something,
3139 * we give an EOF when tracing is again disabled.
3141 * iter->pos will be 0 if we haven't read anything.
3143 if (!tracer_enabled
&& iter
->pos
)
3154 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
3155 size_t cnt
, loff_t
*ppos
)
3157 struct trace_iterator
*iter
= filp
->private_data
;
3158 static struct tracer
*old_tracer
;
3161 /* return any leftover data */
3162 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3166 trace_seq_init(&iter
->seq
);
3168 /* copy the tracer to avoid using a global lock all around */
3169 mutex_lock(&trace_types_lock
);
3170 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3171 old_tracer
= current_trace
;
3172 *iter
->trace
= *current_trace
;
3174 mutex_unlock(&trace_types_lock
);
3177 * Avoid more than one consumer on a single file descriptor
3178 * This is just a matter of traces coherency, the ring buffer itself
3181 mutex_lock(&iter
->mutex
);
3182 if (iter
->trace
->read
) {
3183 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
3189 sret
= tracing_wait_pipe(filp
);
3193 /* stop when tracing is finished */
3194 if (trace_empty(iter
)) {
3199 if (cnt
>= PAGE_SIZE
)
3200 cnt
= PAGE_SIZE
- 1;
3202 /* reset all but tr, trace, and overruns */
3203 memset(&iter
->seq
, 0,
3204 sizeof(struct trace_iterator
) -
3205 offsetof(struct trace_iterator
, seq
));
3208 trace_event_read_lock();
3209 trace_access_lock(iter
->cpu_file
);
3210 while (trace_find_next_entry_inc(iter
) != NULL
) {
3211 enum print_line_t ret
;
3212 int len
= iter
->seq
.len
;
3214 ret
= print_trace_line(iter
);
3215 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3216 /* don't print partial lines */
3217 iter
->seq
.len
= len
;
3220 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3221 trace_consume(iter
);
3223 if (iter
->seq
.len
>= cnt
)
3226 trace_access_unlock(iter
->cpu_file
);
3227 trace_event_read_unlock();
3229 /* Now copy what we have to the user */
3230 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3231 if (iter
->seq
.readpos
>= iter
->seq
.len
)
3232 trace_seq_init(&iter
->seq
);
3235 * If there was nothing to send to user, inspite of consuming trace
3236 * entries, go back to wait for more entries.
3242 mutex_unlock(&iter
->mutex
);
3247 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
3248 struct pipe_buffer
*buf
)
3250 __free_page(buf
->page
);
3253 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
3256 __free_page(spd
->pages
[idx
]);
3259 static const struct pipe_buf_operations tracing_pipe_buf_ops
= {
3261 .map
= generic_pipe_buf_map
,
3262 .unmap
= generic_pipe_buf_unmap
,
3263 .confirm
= generic_pipe_buf_confirm
,
3264 .release
= tracing_pipe_buf_release
,
3265 .steal
= generic_pipe_buf_steal
,
3266 .get
= generic_pipe_buf_get
,
3270 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
3275 /* Seq buffer is page-sized, exactly what we need. */
3277 count
= iter
->seq
.len
;
3278 ret
= print_trace_line(iter
);
3279 count
= iter
->seq
.len
- count
;
3282 iter
->seq
.len
-= count
;
3285 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3286 iter
->seq
.len
-= count
;
3290 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3291 trace_consume(iter
);
3293 if (!trace_find_next_entry_inc(iter
)) {
3303 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3305 struct pipe_inode_info
*pipe
,
3309 struct page
*pages_def
[PIPE_DEF_BUFFERS
];
3310 struct partial_page partial_def
[PIPE_DEF_BUFFERS
];
3311 struct trace_iterator
*iter
= filp
->private_data
;
3312 struct splice_pipe_desc spd
= {
3314 .partial
= partial_def
,
3315 .nr_pages
= 0, /* This gets updated below. */
3317 .ops
= &tracing_pipe_buf_ops
,
3318 .spd_release
= tracing_spd_release_pipe
,
3320 static struct tracer
*old_tracer
;
3325 if (splice_grow_spd(pipe
, &spd
))
3328 /* copy the tracer to avoid using a global lock all around */
3329 mutex_lock(&trace_types_lock
);
3330 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3331 old_tracer
= current_trace
;
3332 *iter
->trace
= *current_trace
;
3334 mutex_unlock(&trace_types_lock
);
3336 mutex_lock(&iter
->mutex
);
3338 if (iter
->trace
->splice_read
) {
3339 ret
= iter
->trace
->splice_read(iter
, filp
,
3340 ppos
, pipe
, len
, flags
);
3345 ret
= tracing_wait_pipe(filp
);
3349 if (!iter
->ent
&& !trace_find_next_entry_inc(iter
)) {
3354 trace_event_read_lock();
3355 trace_access_lock(iter
->cpu_file
);
3357 /* Fill as many pages as possible. */
3358 for (i
= 0, rem
= len
; i
< pipe
->buffers
&& rem
; i
++) {
3359 spd
.pages
[i
] = alloc_page(GFP_KERNEL
);
3363 rem
= tracing_fill_pipe_page(rem
, iter
);
3365 /* Copy the data into the page, so we can start over. */
3366 ret
= trace_seq_to_buffer(&iter
->seq
,
3367 page_address(spd
.pages
[i
]),
3370 __free_page(spd
.pages
[i
]);
3373 spd
.partial
[i
].offset
= 0;
3374 spd
.partial
[i
].len
= iter
->seq
.len
;
3376 trace_seq_init(&iter
->seq
);
3379 trace_access_unlock(iter
->cpu_file
);
3380 trace_event_read_unlock();
3381 mutex_unlock(&iter
->mutex
);
3385 ret
= splice_to_pipe(pipe
, &spd
);
3387 splice_shrink_spd(pipe
, &spd
);
3391 mutex_unlock(&iter
->mutex
);
3396 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3397 size_t cnt
, loff_t
*ppos
)
3399 struct trace_array
*tr
= filp
->private_data
;
3403 mutex_lock(&trace_types_lock
);
3404 if (!ring_buffer_expanded
)
3405 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3407 trace_buf_size
>> 10);
3409 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3410 mutex_unlock(&trace_types_lock
);
3412 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3416 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3417 size_t cnt
, loff_t
*ppos
)
3423 if (cnt
>= sizeof(buf
))
3426 if (copy_from_user(&buf
, ubuf
, cnt
))
3431 ret
= strict_strtoul(buf
, 10, &val
);
3435 /* must have at least 1 entry */
3439 mutex_lock(&trace_types_lock
);
3443 /* disable all cpu buffers */
3444 for_each_tracing_cpu(cpu
) {
3445 if (global_trace
.data
[cpu
])
3446 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3447 if (max_tr
.data
[cpu
])
3448 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3451 /* value is in KB */
3454 if (val
!= global_trace
.entries
) {
3455 ret
= tracing_resize_ring_buffer(val
);
3464 /* If check pages failed, return ENOMEM */
3465 if (tracing_disabled
)
3468 for_each_tracing_cpu(cpu
) {
3469 if (global_trace
.data
[cpu
])
3470 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3471 if (max_tr
.data
[cpu
])
3472 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3476 mutex_unlock(&trace_types_lock
);
3481 static int mark_printk(const char *fmt
, ...)
3485 va_start(args
, fmt
);
3486 ret
= trace_vprintk(0, fmt
, args
);
3492 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3493 size_t cnt
, loff_t
*fpos
)
3498 if (tracing_disabled
)
3501 if (cnt
> TRACE_BUF_SIZE
)
3502 cnt
= TRACE_BUF_SIZE
;
3504 buf
= kmalloc(cnt
+ 2, GFP_KERNEL
);
3508 if (copy_from_user(buf
, ubuf
, cnt
)) {
3512 if (buf
[cnt
-1] != '\n') {
3518 written
= mark_printk("%s", buf
);
3522 /* don't tell userspace we wrote more - it might confuse them */
3529 static int tracing_clock_show(struct seq_file
*m
, void *v
)
3533 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++)
3535 "%s%s%s%s", i
? " " : "",
3536 i
== trace_clock_id
? "[" : "", trace_clocks
[i
].name
,
3537 i
== trace_clock_id
? "]" : "");
3543 static ssize_t
tracing_clock_write(struct file
*filp
, const char __user
*ubuf
,
3544 size_t cnt
, loff_t
*fpos
)
3547 const char *clockstr
;
3550 if (cnt
>= sizeof(buf
))
3553 if (copy_from_user(&buf
, ubuf
, cnt
))
3558 clockstr
= strstrip(buf
);
3560 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++) {
3561 if (strcmp(trace_clocks
[i
].name
, clockstr
) == 0)
3564 if (i
== ARRAY_SIZE(trace_clocks
))
3569 mutex_lock(&trace_types_lock
);
3571 ring_buffer_set_clock(global_trace
.buffer
, trace_clocks
[i
].func
);
3573 ring_buffer_set_clock(max_tr
.buffer
, trace_clocks
[i
].func
);
3575 mutex_unlock(&trace_types_lock
);
3582 static int tracing_clock_open(struct inode
*inode
, struct file
*file
)
3584 if (tracing_disabled
)
3586 return single_open(file
, tracing_clock_show
, NULL
);
3589 static const struct file_operations tracing_max_lat_fops
= {
3590 .open
= tracing_open_generic
,
3591 .read
= tracing_max_lat_read
,
3592 .write
= tracing_max_lat_write
,
3593 .llseek
= generic_file_llseek
,
3596 static const struct file_operations tracing_ctrl_fops
= {
3597 .open
= tracing_open_generic
,
3598 .read
= tracing_ctrl_read
,
3599 .write
= tracing_ctrl_write
,
3600 .llseek
= generic_file_llseek
,
3603 static const struct file_operations set_tracer_fops
= {
3604 .open
= tracing_open_generic
,
3605 .read
= tracing_set_trace_read
,
3606 .write
= tracing_set_trace_write
,
3607 .llseek
= generic_file_llseek
,
3610 static const struct file_operations tracing_pipe_fops
= {
3611 .open
= tracing_open_pipe
,
3612 .poll
= tracing_poll_pipe
,
3613 .read
= tracing_read_pipe
,
3614 .splice_read
= tracing_splice_read_pipe
,
3615 .release
= tracing_release_pipe
,
3616 .llseek
= no_llseek
,
3619 static const struct file_operations tracing_entries_fops
= {
3620 .open
= tracing_open_generic
,
3621 .read
= tracing_entries_read
,
3622 .write
= tracing_entries_write
,
3623 .llseek
= generic_file_llseek
,
3626 static const struct file_operations tracing_mark_fops
= {
3627 .open
= tracing_open_generic
,
3628 .write
= tracing_mark_write
,
3629 .llseek
= generic_file_llseek
,
3632 static const struct file_operations trace_clock_fops
= {
3633 .open
= tracing_clock_open
,
3635 .llseek
= seq_lseek
,
3636 .release
= single_release
,
3637 .write
= tracing_clock_write
,
3640 struct ftrace_buffer_info
{
3641 struct trace_array
*tr
;
3647 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3649 int cpu
= (int)(long)inode
->i_private
;
3650 struct ftrace_buffer_info
*info
;
3652 if (tracing_disabled
)
3655 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3659 info
->tr
= &global_trace
;
3662 /* Force reading ring buffer for first read */
3663 info
->read
= (unsigned int)-1;
3665 filp
->private_data
= info
;
3667 return nonseekable_open(inode
, filp
);
3671 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3672 size_t count
, loff_t
*ppos
)
3674 struct ftrace_buffer_info
*info
= filp
->private_data
;
3682 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3686 /* Do we have previous read data to read? */
3687 if (info
->read
< PAGE_SIZE
)
3692 trace_access_lock(info
->cpu
);
3693 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3697 trace_access_unlock(info
->cpu
);
3702 size
= PAGE_SIZE
- info
->read
;
3706 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3717 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3719 struct ftrace_buffer_info
*info
= file
->private_data
;
3722 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3729 struct ring_buffer
*buffer
;
3734 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3735 struct pipe_buffer
*buf
)
3737 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3742 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3747 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3748 struct pipe_buffer
*buf
)
3753 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3754 struct pipe_buffer
*buf
)
3756 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3761 /* Pipe buffer operations for a buffer. */
3762 static const struct pipe_buf_operations buffer_pipe_buf_ops
= {
3764 .map
= generic_pipe_buf_map
,
3765 .unmap
= generic_pipe_buf_unmap
,
3766 .confirm
= generic_pipe_buf_confirm
,
3767 .release
= buffer_pipe_buf_release
,
3768 .steal
= buffer_pipe_buf_steal
,
3769 .get
= buffer_pipe_buf_get
,
3773 * Callback from splice_to_pipe(), if we need to release some pages
3774 * at the end of the spd in case we error'ed out in filling the pipe.
3776 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3778 struct buffer_ref
*ref
=
3779 (struct buffer_ref
*)spd
->partial
[i
].private;
3784 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3786 spd
->partial
[i
].private = 0;
3790 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3791 struct pipe_inode_info
*pipe
, size_t len
,
3794 struct ftrace_buffer_info
*info
= file
->private_data
;
3795 struct partial_page partial_def
[PIPE_DEF_BUFFERS
];
3796 struct page
*pages_def
[PIPE_DEF_BUFFERS
];
3797 struct splice_pipe_desc spd
= {
3799 .partial
= partial_def
,
3801 .ops
= &buffer_pipe_buf_ops
,
3802 .spd_release
= buffer_spd_release
,
3804 struct buffer_ref
*ref
;
3805 int entries
, size
, i
;
3808 if (splice_grow_spd(pipe
, &spd
))
3811 if (*ppos
& (PAGE_SIZE
- 1)) {
3812 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3817 if (len
& (PAGE_SIZE
- 1)) {
3818 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3819 if (len
< PAGE_SIZE
) {
3826 trace_access_lock(info
->cpu
);
3827 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3829 for (i
= 0; i
< pipe
->buffers
&& len
&& entries
; i
++, len
-= PAGE_SIZE
) {
3833 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3838 ref
->buffer
= info
->tr
->buffer
;
3839 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3845 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3848 ring_buffer_free_read_page(ref
->buffer
,
3855 * zero out any left over data, this is going to
3858 size
= ring_buffer_page_len(ref
->page
);
3859 if (size
< PAGE_SIZE
)
3860 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3862 page
= virt_to_page(ref
->page
);
3864 spd
.pages
[i
] = page
;
3865 spd
.partial
[i
].len
= PAGE_SIZE
;
3866 spd
.partial
[i
].offset
= 0;
3867 spd
.partial
[i
].private = (unsigned long)ref
;
3871 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3874 trace_access_unlock(info
->cpu
);
3877 /* did we read anything? */
3878 if (!spd
.nr_pages
) {
3879 if (flags
& SPLICE_F_NONBLOCK
)
3887 ret
= splice_to_pipe(pipe
, &spd
);
3888 splice_shrink_spd(pipe
, &spd
);
3893 static const struct file_operations tracing_buffers_fops
= {
3894 .open
= tracing_buffers_open
,
3895 .read
= tracing_buffers_read
,
3896 .release
= tracing_buffers_release
,
3897 .splice_read
= tracing_buffers_splice_read
,
3898 .llseek
= no_llseek
,
3902 tracing_stats_read(struct file
*filp
, char __user
*ubuf
,
3903 size_t count
, loff_t
*ppos
)
3905 unsigned long cpu
= (unsigned long)filp
->private_data
;
3906 struct trace_array
*tr
= &global_trace
;
3907 struct trace_seq
*s
;
3910 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
3916 cnt
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
3917 trace_seq_printf(s
, "entries: %ld\n", cnt
);
3919 cnt
= ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
3920 trace_seq_printf(s
, "overrun: %ld\n", cnt
);
3922 cnt
= ring_buffer_commit_overrun_cpu(tr
->buffer
, cpu
);
3923 trace_seq_printf(s
, "commit overrun: %ld\n", cnt
);
3925 count
= simple_read_from_buffer(ubuf
, count
, ppos
, s
->buffer
, s
->len
);
3932 static const struct file_operations tracing_stats_fops
= {
3933 .open
= tracing_open_generic
,
3934 .read
= tracing_stats_read
,
3935 .llseek
= generic_file_llseek
,
3938 #ifdef CONFIG_DYNAMIC_FTRACE
3940 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3946 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3947 size_t cnt
, loff_t
*ppos
)
3949 static char ftrace_dyn_info_buffer
[1024];
3950 static DEFINE_MUTEX(dyn_info_mutex
);
3951 unsigned long *p
= filp
->private_data
;
3952 char *buf
= ftrace_dyn_info_buffer
;
3953 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3956 mutex_lock(&dyn_info_mutex
);
3957 r
= sprintf(buf
, "%ld ", *p
);
3959 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3962 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3964 mutex_unlock(&dyn_info_mutex
);
3969 static const struct file_operations tracing_dyn_info_fops
= {
3970 .open
= tracing_open_generic
,
3971 .read
= tracing_read_dyn_info
,
3972 .llseek
= generic_file_llseek
,
3976 static struct dentry
*d_tracer
;
3978 struct dentry
*tracing_init_dentry(void)
3985 if (!debugfs_initialized())
3988 d_tracer
= debugfs_create_dir("tracing", NULL
);
3990 if (!d_tracer
&& !once
) {
3992 pr_warning("Could not create debugfs directory 'tracing'\n");
3999 static struct dentry
*d_percpu
;
4001 struct dentry
*tracing_dentry_percpu(void)
4004 struct dentry
*d_tracer
;
4009 d_tracer
= tracing_init_dentry();
4014 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
4016 if (!d_percpu
&& !once
) {
4018 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4025 static void tracing_init_debugfs_percpu(long cpu
)
4027 struct dentry
*d_percpu
= tracing_dentry_percpu();
4028 struct dentry
*d_cpu
;
4029 char cpu_dir
[30]; /* 30 characters should be more than enough */
4031 snprintf(cpu_dir
, 30, "cpu%ld", cpu
);
4032 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
4034 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
4038 /* per cpu trace_pipe */
4039 trace_create_file("trace_pipe", 0444, d_cpu
,
4040 (void *) cpu
, &tracing_pipe_fops
);
4043 trace_create_file("trace", 0644, d_cpu
,
4044 (void *) cpu
, &tracing_fops
);
4046 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
4047 (void *) cpu
, &tracing_buffers_fops
);
4049 trace_create_file("stats", 0444, d_cpu
,
4050 (void *) cpu
, &tracing_stats_fops
);
4053 #ifdef CONFIG_FTRACE_SELFTEST
4054 /* Let selftest have access to static functions in this file */
4055 #include "trace_selftest.c"
4058 struct trace_option_dentry
{
4059 struct tracer_opt
*opt
;
4060 struct tracer_flags
*flags
;
4061 struct dentry
*entry
;
4065 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
4068 struct trace_option_dentry
*topt
= filp
->private_data
;
4071 if (topt
->flags
->val
& topt
->opt
->bit
)
4076 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
4080 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
4083 struct trace_option_dentry
*topt
= filp
->private_data
;
4088 if (cnt
>= sizeof(buf
))
4091 if (copy_from_user(&buf
, ubuf
, cnt
))
4096 ret
= strict_strtoul(buf
, 10, &val
);
4100 if (val
!= 0 && val
!= 1)
4103 if (!!(topt
->flags
->val
& topt
->opt
->bit
) != val
) {
4104 mutex_lock(&trace_types_lock
);
4105 ret
= __set_tracer_option(current_trace
, topt
->flags
,
4107 mutex_unlock(&trace_types_lock
);
4118 static const struct file_operations trace_options_fops
= {
4119 .open
= tracing_open_generic
,
4120 .read
= trace_options_read
,
4121 .write
= trace_options_write
,
4122 .llseek
= generic_file_llseek
,
4126 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
4129 long index
= (long)filp
->private_data
;
4132 if (trace_flags
& (1 << index
))
4137 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
4141 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
4144 long index
= (long)filp
->private_data
;
4149 if (cnt
>= sizeof(buf
))
4152 if (copy_from_user(&buf
, ubuf
, cnt
))
4157 ret
= strict_strtoul(buf
, 10, &val
);
4161 if (val
!= 0 && val
!= 1)
4163 set_tracer_flags(1 << index
, val
);
4170 static const struct file_operations trace_options_core_fops
= {
4171 .open
= tracing_open_generic
,
4172 .read
= trace_options_core_read
,
4173 .write
= trace_options_core_write
,
4174 .llseek
= generic_file_llseek
,
4177 struct dentry
*trace_create_file(const char *name
,
4179 struct dentry
*parent
,
4181 const struct file_operations
*fops
)
4185 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
4187 pr_warning("Could not create debugfs '%s' entry\n", name
);
4193 static struct dentry
*trace_options_init_dentry(void)
4195 struct dentry
*d_tracer
;
4196 static struct dentry
*t_options
;
4201 d_tracer
= tracing_init_dentry();
4205 t_options
= debugfs_create_dir("options", d_tracer
);
4207 pr_warning("Could not create debugfs directory 'options'\n");
4215 create_trace_option_file(struct trace_option_dentry
*topt
,
4216 struct tracer_flags
*flags
,
4217 struct tracer_opt
*opt
)
4219 struct dentry
*t_options
;
4221 t_options
= trace_options_init_dentry();
4225 topt
->flags
= flags
;
4228 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
4229 &trace_options_fops
);
4233 static struct trace_option_dentry
*
4234 create_trace_option_files(struct tracer
*tracer
)
4236 struct trace_option_dentry
*topts
;
4237 struct tracer_flags
*flags
;
4238 struct tracer_opt
*opts
;
4244 flags
= tracer
->flags
;
4246 if (!flags
|| !flags
->opts
)
4251 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4254 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
4258 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4259 create_trace_option_file(&topts
[cnt
], flags
,
4266 destroy_trace_option_files(struct trace_option_dentry
*topts
)
4273 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
4274 if (topts
[cnt
].entry
)
4275 debugfs_remove(topts
[cnt
].entry
);
4281 static struct dentry
*
4282 create_trace_option_core_file(const char *option
, long index
)
4284 struct dentry
*t_options
;
4286 t_options
= trace_options_init_dentry();
4290 return trace_create_file(option
, 0644, t_options
, (void *)index
,
4291 &trace_options_core_fops
);
4294 static __init
void create_trace_options_dir(void)
4296 struct dentry
*t_options
;
4299 t_options
= trace_options_init_dentry();
4303 for (i
= 0; trace_options
[i
]; i
++)
4304 create_trace_option_core_file(trace_options
[i
], i
);
4307 static __init
int tracer_init_debugfs(void)
4309 struct dentry
*d_tracer
;
4312 trace_access_lock_init();
4314 d_tracer
= tracing_init_dentry();
4316 trace_create_file("tracing_enabled", 0644, d_tracer
,
4317 &global_trace
, &tracing_ctrl_fops
);
4319 trace_create_file("trace_options", 0644, d_tracer
,
4320 NULL
, &tracing_iter_fops
);
4322 trace_create_file("tracing_cpumask", 0644, d_tracer
,
4323 NULL
, &tracing_cpumask_fops
);
4325 trace_create_file("trace", 0644, d_tracer
,
4326 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
4328 trace_create_file("available_tracers", 0444, d_tracer
,
4329 &global_trace
, &show_traces_fops
);
4331 trace_create_file("current_tracer", 0644, d_tracer
,
4332 &global_trace
, &set_tracer_fops
);
4334 #ifdef CONFIG_TRACER_MAX_TRACE
4335 trace_create_file("tracing_max_latency", 0644, d_tracer
,
4336 &tracing_max_latency
, &tracing_max_lat_fops
);
4339 trace_create_file("tracing_thresh", 0644, d_tracer
,
4340 &tracing_thresh
, &tracing_max_lat_fops
);
4342 trace_create_file("README", 0444, d_tracer
,
4343 NULL
, &tracing_readme_fops
);
4345 trace_create_file("trace_pipe", 0444, d_tracer
,
4346 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
4348 trace_create_file("buffer_size_kb", 0644, d_tracer
,
4349 &global_trace
, &tracing_entries_fops
);
4351 trace_create_file("trace_marker", 0220, d_tracer
,
4352 NULL
, &tracing_mark_fops
);
4354 trace_create_file("saved_cmdlines", 0444, d_tracer
,
4355 NULL
, &tracing_saved_cmdlines_fops
);
4357 trace_create_file("trace_clock", 0644, d_tracer
, NULL
,
4360 #ifdef CONFIG_DYNAMIC_FTRACE
4361 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
4362 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
4365 create_trace_options_dir();
4367 for_each_tracing_cpu(cpu
)
4368 tracing_init_debugfs_percpu(cpu
);
4373 static int trace_panic_handler(struct notifier_block
*this,
4374 unsigned long event
, void *unused
)
4376 if (ftrace_dump_on_oops
)
4377 ftrace_dump(ftrace_dump_on_oops
);
4381 static struct notifier_block trace_panic_notifier
= {
4382 .notifier_call
= trace_panic_handler
,
4384 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
4387 static int trace_die_handler(struct notifier_block
*self
,
4393 if (ftrace_dump_on_oops
)
4394 ftrace_dump(ftrace_dump_on_oops
);
4402 static struct notifier_block trace_die_notifier
= {
4403 .notifier_call
= trace_die_handler
,
4408 * printk is set to max of 1024, we really don't need it that big.
4409 * Nothing should be printing 1000 characters anyway.
4411 #define TRACE_MAX_PRINT 1000
4414 * Define here KERN_TRACE so that we have one place to modify
4415 * it if we decide to change what log level the ftrace dump
4418 #define KERN_TRACE KERN_EMERG
4421 trace_printk_seq(struct trace_seq
*s
)
4423 /* Probably should print a warning here. */
4427 /* should be zero ended, but we are paranoid. */
4428 s
->buffer
[s
->len
] = 0;
4430 printk(KERN_TRACE
"%s", s
->buffer
);
4435 void trace_init_global_iter(struct trace_iterator
*iter
)
4437 iter
->tr
= &global_trace
;
4438 iter
->trace
= current_trace
;
4439 iter
->cpu_file
= TRACE_PIPE_ALL_CPU
;
4443 __ftrace_dump(bool disable_tracing
, enum ftrace_dump_mode oops_dump_mode
)
4445 static arch_spinlock_t ftrace_dump_lock
=
4446 (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
4447 /* use static because iter can be a bit big for the stack */
4448 static struct trace_iterator iter
;
4449 unsigned int old_userobj
;
4450 static int dump_ran
;
4451 unsigned long flags
;
4455 local_irq_save(flags
);
4456 arch_spin_lock(&ftrace_dump_lock
);
4464 if (disable_tracing
)
4467 trace_init_global_iter(&iter
);
4469 for_each_tracing_cpu(cpu
) {
4470 atomic_inc(&iter
.tr
->data
[cpu
]->disabled
);
4473 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
4475 /* don't look at user memory in panic mode */
4476 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4478 /* Simulate the iterator */
4479 iter
.tr
= &global_trace
;
4480 iter
.trace
= current_trace
;
4482 switch (oops_dump_mode
) {
4484 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4487 iter
.cpu_file
= raw_smp_processor_id();
4492 printk(KERN_TRACE
"Bad dumping mode, switching to all CPUs dump\n");
4493 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4496 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4499 * We need to stop all tracing on all CPUS to read the
4500 * the next buffer. This is a bit expensive, but is
4501 * not done often. We fill all what we can read,
4502 * and then release the locks again.
4505 while (!trace_empty(&iter
)) {
4508 printk(KERN_TRACE
"---------------------------------\n");
4512 /* reset all but tr, trace, and overruns */
4513 memset(&iter
.seq
, 0,
4514 sizeof(struct trace_iterator
) -
4515 offsetof(struct trace_iterator
, seq
));
4516 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4519 if (trace_find_next_entry_inc(&iter
) != NULL
) {
4522 ret
= print_trace_line(&iter
);
4523 if (ret
!= TRACE_TYPE_NO_CONSUME
)
4524 trace_consume(&iter
);
4527 trace_printk_seq(&iter
.seq
);
4531 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4533 printk(KERN_TRACE
"---------------------------------\n");
4536 /* Re-enable tracing if requested */
4537 if (!disable_tracing
) {
4538 trace_flags
|= old_userobj
;
4540 for_each_tracing_cpu(cpu
) {
4541 atomic_dec(&iter
.tr
->data
[cpu
]->disabled
);
4547 arch_spin_unlock(&ftrace_dump_lock
);
4548 local_irq_restore(flags
);
4551 /* By default: disable tracing after the dump */
4552 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode
)
4554 __ftrace_dump(true, oops_dump_mode
);
4557 __init
static int tracer_alloc_buffers(void)
4560 enum ring_buffer_flags rb_flags
;
4565 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4568 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4569 goto out_free_buffer_mask
;
4571 /* To save memory, keep the ring buffer size to its minimum */
4572 if (ring_buffer_expanded
)
4573 ring_buf_size
= trace_buf_size
;
4577 rb_flags
= trace_flags
& TRACE_ITER_OVERWRITE
? RB_FL_OVERWRITE
: 0;
4579 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4580 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4582 /* TODO: make the number of buffers hot pluggable with CPUS */
4583 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
, rb_flags
);
4584 if (!global_trace
.buffer
) {
4585 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4587 goto out_free_cpumask
;
4589 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4592 #ifdef CONFIG_TRACER_MAX_TRACE
4593 max_tr
.buffer
= ring_buffer_alloc(1, rb_flags
);
4594 if (!max_tr
.buffer
) {
4595 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4597 ring_buffer_free(global_trace
.buffer
);
4598 goto out_free_cpumask
;
4603 /* Allocate the first page for all buffers */
4604 for_each_tracing_cpu(i
) {
4605 global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4606 max_tr
.data
[i
] = &per_cpu(max_tr_data
, i
);
4609 trace_init_cmdlines();
4611 register_tracer(&nop_trace
);
4612 current_trace
= &nop_trace
;
4613 /* All seems OK, enable tracing */
4614 tracing_disabled
= 0;
4616 atomic_notifier_chain_register(&panic_notifier_list
,
4617 &trace_panic_notifier
);
4619 register_die_notifier(&trace_die_notifier
);
4624 free_cpumask_var(tracing_cpumask
);
4625 out_free_buffer_mask
:
4626 free_cpumask_var(tracing_buffer_mask
);
4631 __init
static int clear_boot_tracer(void)
4634 * The default tracer at boot buffer is an init section.
4635 * This function is called in lateinit. If we did not
4636 * find the boot tracer, then clear it out, to prevent
4637 * later registration from accessing the buffer that is
4638 * about to be freed.
4640 if (!default_bootup_tracer
)
4643 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4644 default_bootup_tracer
);
4645 default_bootup_tracer
= NULL
;
4650 early_initcall(tracer_alloc_buffers
);
4651 fs_initcall(tracer_init_debugfs
);
4652 late_initcall(clear_boot_tracer
);