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>
39 #include <linux/nmi.h>
43 #include "trace_output.h"
46 * On boot up, the ring buffer is set to the minimum size, so that
47 * we do not waste memory on systems that are not using tracing.
49 int ring_buffer_expanded
;
52 * We need to change this state when a selftest is running.
53 * A selftest will lurk into the ring-buffer to count the
54 * entries inserted during the selftest although some concurrent
55 * insertions into the ring-buffer such as trace_printk could occurred
56 * at the same time, giving false positive or negative results.
58 static bool __read_mostly tracing_selftest_running
;
61 * If a tracer is running, we do not want to run SELFTEST.
63 bool __read_mostly tracing_selftest_disabled
;
65 /* For tracers that don't implement custom flags */
66 static struct tracer_opt dummy_tracer_opt
[] = {
70 static struct tracer_flags dummy_tracer_flags
= {
72 .opts
= dummy_tracer_opt
75 static int dummy_set_flag(u32 old_flags
, u32 bit
, int set
)
81 * Kill all tracing for good (never come back).
82 * It is initialized to 1 but will turn to zero if the initialization
83 * of the tracer is successful. But that is the only place that sets
86 static int tracing_disabled
= 1;
88 DEFINE_PER_CPU(int, ftrace_cpu_disabled
);
90 static inline void ftrace_disable_cpu(void)
93 __this_cpu_inc(ftrace_cpu_disabled
);
96 static inline void ftrace_enable_cpu(void)
98 __this_cpu_dec(ftrace_cpu_disabled
);
102 cpumask_var_t __read_mostly tracing_buffer_mask
;
105 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
107 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
108 * is set, then ftrace_dump is called. This will output the contents
109 * of the ftrace buffers to the console. This is very useful for
110 * capturing traces that lead to crashes and outputing it to a
113 * It is default off, but you can enable it with either specifying
114 * "ftrace_dump_on_oops" in the kernel command line, or setting
115 * /proc/sys/kernel/ftrace_dump_on_oops
116 * Set 1 if you want to dump buffers of all CPUs
117 * Set 2 if you want to dump the buffer of the CPU that triggered oops
120 enum ftrace_dump_mode ftrace_dump_on_oops
;
122 static int tracing_set_tracer(const char *buf
);
124 #define MAX_TRACER_SIZE 100
125 static char bootup_tracer_buf
[MAX_TRACER_SIZE
] __initdata
;
126 static char *default_bootup_tracer
;
128 static int __init
set_cmdline_ftrace(char *str
)
130 strncpy(bootup_tracer_buf
, str
, MAX_TRACER_SIZE
);
131 default_bootup_tracer
= bootup_tracer_buf
;
132 /* We are using ftrace early, expand it */
133 ring_buffer_expanded
= 1;
136 __setup("ftrace=", set_cmdline_ftrace
);
138 static int __init
set_ftrace_dump_on_oops(char *str
)
140 if (*str
++ != '=' || !*str
) {
141 ftrace_dump_on_oops
= DUMP_ALL
;
145 if (!strcmp("orig_cpu", str
)) {
146 ftrace_dump_on_oops
= DUMP_ORIG
;
152 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
154 unsigned long long ns2usecs(cycle_t nsec
)
162 * The global_trace is the descriptor that holds the tracing
163 * buffers for the live tracing. For each CPU, it contains
164 * a link list of pages that will store trace entries. The
165 * page descriptor of the pages in the memory is used to hold
166 * the link list by linking the lru item in the page descriptor
167 * to each of the pages in the buffer per CPU.
169 * For each active CPU there is a data field that holds the
170 * pages for the buffer for that CPU. Each CPU has the same number
171 * of pages allocated for its buffer.
173 static struct trace_array global_trace
;
175 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
177 int filter_current_check_discard(struct ring_buffer
*buffer
,
178 struct ftrace_event_call
*call
, void *rec
,
179 struct ring_buffer_event
*event
)
181 return filter_check_discard(call
, rec
, buffer
, event
);
183 EXPORT_SYMBOL_GPL(filter_current_check_discard
);
185 cycle_t
ftrace_now(int cpu
)
189 /* Early boot up does not have a buffer yet */
190 if (!global_trace
.buffer
)
191 return trace_clock_local();
193 ts
= ring_buffer_time_stamp(global_trace
.buffer
, cpu
);
194 ring_buffer_normalize_time_stamp(global_trace
.buffer
, cpu
, &ts
);
200 * The max_tr is used to snapshot the global_trace when a maximum
201 * latency is reached. Some tracers will use this to store a maximum
202 * trace while it continues examining live traces.
204 * The buffers for the max_tr are set up the same as the global_trace.
205 * When a snapshot is taken, the link list of the max_tr is swapped
206 * with the link list of the global_trace and the buffers are reset for
207 * the global_trace so the tracing can continue.
209 static struct trace_array max_tr
;
211 static DEFINE_PER_CPU(struct trace_array_cpu
, max_tr_data
);
213 /* tracer_enabled is used to toggle activation of a tracer */
214 static int tracer_enabled
= 1;
217 * tracing_is_enabled - return tracer_enabled status
219 * This function is used by other tracers to know the status
220 * of the tracer_enabled flag. Tracers may use this function
221 * to know if it should enable their features when starting
222 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
224 int tracing_is_enabled(void)
226 return tracer_enabled
;
230 * trace_buf_size is the size in bytes that is allocated
231 * for a buffer. Note, the number of bytes is always rounded
234 * This number is purposely set to a low number of 16384.
235 * If the dump on oops happens, it will be much appreciated
236 * to not have to wait for all that output. Anyway this can be
237 * boot time and run time configurable.
239 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
241 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
243 /* trace_types holds a link list of available tracers. */
244 static struct tracer
*trace_types __read_mostly
;
246 /* current_trace points to the tracer that is currently active */
247 static struct tracer
*current_trace __read_mostly
;
250 * trace_types_lock is used to protect the trace_types list.
252 static DEFINE_MUTEX(trace_types_lock
);
255 * serialize the access of the ring buffer
257 * ring buffer serializes readers, but it is low level protection.
258 * The validity of the events (which returns by ring_buffer_peek() ..etc)
259 * are not protected by ring buffer.
261 * The content of events may become garbage if we allow other process consumes
262 * these events concurrently:
263 * A) the page of the consumed events may become a normal page
264 * (not reader page) in ring buffer, and this page will be rewrited
265 * by events producer.
266 * B) The page of the consumed events may become a page for splice_read,
267 * and this page will be returned to system.
269 * These primitives allow multi process access to different cpu ring buffer
272 * These primitives don't distinguish read-only and read-consume access.
273 * Multi read-only access are also serialized.
277 static DECLARE_RWSEM(all_cpu_access_lock
);
278 static DEFINE_PER_CPU(struct mutex
, cpu_access_lock
);
280 static inline void trace_access_lock(int cpu
)
282 if (cpu
== TRACE_PIPE_ALL_CPU
) {
283 /* gain it for accessing the whole ring buffer. */
284 down_write(&all_cpu_access_lock
);
286 /* gain it for accessing a cpu ring buffer. */
288 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
289 down_read(&all_cpu_access_lock
);
291 /* Secondly block other access to this @cpu ring buffer. */
292 mutex_lock(&per_cpu(cpu_access_lock
, cpu
));
296 static inline void trace_access_unlock(int cpu
)
298 if (cpu
== TRACE_PIPE_ALL_CPU
) {
299 up_write(&all_cpu_access_lock
);
301 mutex_unlock(&per_cpu(cpu_access_lock
, cpu
));
302 up_read(&all_cpu_access_lock
);
306 static inline void trace_access_lock_init(void)
310 for_each_possible_cpu(cpu
)
311 mutex_init(&per_cpu(cpu_access_lock
, cpu
));
316 static DEFINE_MUTEX(access_lock
);
318 static inline void trace_access_lock(int cpu
)
321 mutex_lock(&access_lock
);
324 static inline void trace_access_unlock(int cpu
)
327 mutex_unlock(&access_lock
);
330 static inline void trace_access_lock_init(void)
336 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
337 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
339 /* trace_flags holds trace_options default values */
340 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
341 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
| TRACE_ITER_SLEEP_TIME
|
342 TRACE_ITER_GRAPH_TIME
| TRACE_ITER_RECORD_CMD
| TRACE_ITER_OVERWRITE
|
345 static int trace_stop_count
;
346 static DEFINE_RAW_SPINLOCK(tracing_start_lock
);
348 static void wakeup_work_handler(struct work_struct
*work
)
350 wake_up(&trace_wait
);
353 static DECLARE_DELAYED_WORK(wakeup_work
, wakeup_work_handler
);
356 * tracing_on - enable tracing buffers
358 * This function enables tracing buffers that may have been
359 * disabled with tracing_off.
361 void tracing_on(void)
363 if (global_trace
.buffer
)
364 ring_buffer_record_on(global_trace
.buffer
);
366 * This flag is only looked at when buffers haven't been
367 * allocated yet. We don't really care about the race
368 * between setting this flag and actually turning
371 global_trace
.buffer_disabled
= 0;
373 EXPORT_SYMBOL_GPL(tracing_on
);
376 * tracing_off - turn off tracing buffers
378 * This function stops the tracing buffers from recording data.
379 * It does not disable any overhead the tracers themselves may
380 * be causing. This function simply causes all recording to
381 * the ring buffers to fail.
383 void tracing_off(void)
385 if (global_trace
.buffer
)
386 ring_buffer_record_on(global_trace
.buffer
);
388 * This flag is only looked at when buffers haven't been
389 * allocated yet. We don't really care about the race
390 * between setting this flag and actually turning
393 global_trace
.buffer_disabled
= 1;
395 EXPORT_SYMBOL_GPL(tracing_off
);
398 * tracing_is_on - show state of ring buffers enabled
400 int tracing_is_on(void)
402 if (global_trace
.buffer
)
403 return ring_buffer_record_is_on(global_trace
.buffer
);
404 return !global_trace
.buffer_disabled
;
406 EXPORT_SYMBOL_GPL(tracing_is_on
);
409 * trace_wake_up - wake up tasks waiting for trace input
411 * Schedules a delayed work to wake up any task that is blocked on the
412 * trace_wait queue. These is used with trace_poll for tasks polling the
415 void trace_wake_up(void)
417 const unsigned long delay
= msecs_to_jiffies(2);
419 if (trace_flags
& TRACE_ITER_BLOCK
)
421 schedule_delayed_work(&wakeup_work
, delay
);
424 static int __init
set_buf_size(char *str
)
426 unsigned long buf_size
;
430 buf_size
= memparse(str
, &str
);
431 /* nr_entries can not be zero */
434 trace_buf_size
= buf_size
;
437 __setup("trace_buf_size=", set_buf_size
);
439 static int __init
set_tracing_thresh(char *str
)
441 unsigned long threshhold
;
446 ret
= strict_strtoul(str
, 0, &threshhold
);
449 tracing_thresh
= threshhold
* 1000;
452 __setup("tracing_thresh=", set_tracing_thresh
);
454 unsigned long nsecs_to_usecs(unsigned long nsecs
)
459 /* These must match the bit postions in trace_iterator_flags */
460 static const char *trace_options
[] = {
492 { trace_clock_local
, "local" },
493 { trace_clock_global
, "global" },
494 { trace_clock_counter
, "counter" },
500 * trace_parser_get_init - gets the buffer for trace parser
502 int trace_parser_get_init(struct trace_parser
*parser
, int size
)
504 memset(parser
, 0, sizeof(*parser
));
506 parser
->buffer
= kmalloc(size
, GFP_KERNEL
);
515 * trace_parser_put - frees the buffer for trace parser
517 void trace_parser_put(struct trace_parser
*parser
)
519 kfree(parser
->buffer
);
523 * trace_get_user - reads the user input string separated by space
524 * (matched by isspace(ch))
526 * For each string found the 'struct trace_parser' is updated,
527 * and the function returns.
529 * Returns number of bytes read.
531 * See kernel/trace/trace.h for 'struct trace_parser' details.
533 int trace_get_user(struct trace_parser
*parser
, const char __user
*ubuf
,
534 size_t cnt
, loff_t
*ppos
)
541 trace_parser_clear(parser
);
543 ret
= get_user(ch
, ubuf
++);
551 * The parser is not finished with the last write,
552 * continue reading the user input without skipping spaces.
555 /* skip white space */
556 while (cnt
&& isspace(ch
)) {
557 ret
= get_user(ch
, ubuf
++);
564 /* only spaces were written */
574 /* read the non-space input */
575 while (cnt
&& !isspace(ch
)) {
576 if (parser
->idx
< parser
->size
- 1)
577 parser
->buffer
[parser
->idx
++] = ch
;
582 ret
= get_user(ch
, ubuf
++);
589 /* We either got finished input or we have to wait for another call. */
591 parser
->buffer
[parser
->idx
] = 0;
592 parser
->cont
= false;
595 parser
->buffer
[parser
->idx
++] = ch
;
605 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
613 if (s
->len
<= s
->readpos
)
616 len
= s
->len
- s
->readpos
;
619 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
629 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
634 if (s
->len
<= s
->readpos
)
637 len
= s
->len
- s
->readpos
;
640 ret
= memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
649 * ftrace_max_lock is used to protect the swapping of buffers
650 * when taking a max snapshot. The buffers themselves are
651 * protected by per_cpu spinlocks. But the action of the swap
652 * needs its own lock.
654 * This is defined as a arch_spinlock_t in order to help
655 * with performance when lockdep debugging is enabled.
657 * It is also used in other places outside the update_max_tr
658 * so it needs to be defined outside of the
659 * CONFIG_TRACER_MAX_TRACE.
661 static arch_spinlock_t ftrace_max_lock
=
662 (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
664 unsigned long __read_mostly tracing_thresh
;
666 #ifdef CONFIG_TRACER_MAX_TRACE
667 unsigned long __read_mostly tracing_max_latency
;
670 * Copy the new maximum trace into the separate maximum-trace
671 * structure. (this way the maximum trace is permanently saved,
672 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
675 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
677 struct trace_array_cpu
*data
= tr
->data
[cpu
];
678 struct trace_array_cpu
*max_data
;
681 max_tr
.time_start
= data
->preempt_timestamp
;
683 max_data
= max_tr
.data
[cpu
];
684 max_data
->saved_latency
= tracing_max_latency
;
685 max_data
->critical_start
= data
->critical_start
;
686 max_data
->critical_end
= data
->critical_end
;
688 memcpy(max_data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
689 max_data
->pid
= tsk
->pid
;
690 max_data
->uid
= task_uid(tsk
);
691 max_data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
692 max_data
->policy
= tsk
->policy
;
693 max_data
->rt_priority
= tsk
->rt_priority
;
695 /* record this tasks comm */
696 tracing_record_cmdline(tsk
);
700 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
702 * @tsk: the task with the latency
703 * @cpu: The cpu that initiated the trace.
705 * Flip the buffers between the @tr and the max_tr and record information
706 * about which task was the cause of this latency.
709 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
711 struct ring_buffer
*buf
= tr
->buffer
;
713 if (trace_stop_count
)
716 WARN_ON_ONCE(!irqs_disabled());
717 if (!current_trace
->use_max_tr
) {
721 arch_spin_lock(&ftrace_max_lock
);
723 tr
->buffer
= max_tr
.buffer
;
726 __update_max_tr(tr
, tsk
, cpu
);
727 arch_spin_unlock(&ftrace_max_lock
);
731 * update_max_tr_single - only copy one trace over, and reset the rest
733 * @tsk - task with the latency
734 * @cpu - the cpu of the buffer to copy.
736 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
739 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
743 if (trace_stop_count
)
746 WARN_ON_ONCE(!irqs_disabled());
747 if (!current_trace
->use_max_tr
) {
752 arch_spin_lock(&ftrace_max_lock
);
754 ftrace_disable_cpu();
756 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
760 * We failed to swap the buffer due to a commit taking
761 * place on this CPU. We fail to record, but we reset
762 * the max trace buffer (no one writes directly to it)
763 * and flag that it failed.
765 trace_array_printk(&max_tr
, _THIS_IP_
,
766 "Failed to swap buffers due to commit in progress\n");
771 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
&& ret
!= -EBUSY
);
773 __update_max_tr(tr
, tsk
, cpu
);
774 arch_spin_unlock(&ftrace_max_lock
);
776 #endif /* CONFIG_TRACER_MAX_TRACE */
779 * register_tracer - register a tracer with the ftrace system.
780 * @type - the plugin for the tracer
782 * Register a new plugin tracer.
784 int register_tracer(struct tracer
*type
)
785 __releases(kernel_lock
)
786 __acquires(kernel_lock
)
792 pr_info("Tracer must have a name\n");
796 if (strlen(type
->name
) >= MAX_TRACER_SIZE
) {
797 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE
);
801 mutex_lock(&trace_types_lock
);
803 tracing_selftest_running
= true;
805 for (t
= trace_types
; t
; t
= t
->next
) {
806 if (strcmp(type
->name
, t
->name
) == 0) {
808 pr_info("Tracer %s already registered\n",
816 type
->set_flag
= &dummy_set_flag
;
818 type
->flags
= &dummy_tracer_flags
;
820 if (!type
->flags
->opts
)
821 type
->flags
->opts
= dummy_tracer_opt
;
822 if (!type
->wait_pipe
)
823 type
->wait_pipe
= default_wait_pipe
;
826 #ifdef CONFIG_FTRACE_STARTUP_TEST
827 if (type
->selftest
&& !tracing_selftest_disabled
) {
828 struct tracer
*saved_tracer
= current_trace
;
829 struct trace_array
*tr
= &global_trace
;
832 * Run a selftest on this tracer.
833 * Here we reset the trace buffer, and set the current
834 * tracer to be this tracer. The tracer can then run some
835 * internal tracing to verify that everything is in order.
836 * If we fail, we do not register this tracer.
838 tracing_reset_online_cpus(tr
);
840 current_trace
= type
;
842 /* If we expanded the buffers, make sure the max is expanded too */
843 if (ring_buffer_expanded
&& type
->use_max_tr
)
844 ring_buffer_resize(max_tr
.buffer
, trace_buf_size
);
846 /* the test is responsible for initializing and enabling */
847 pr_info("Testing tracer %s: ", type
->name
);
848 ret
= type
->selftest(type
, tr
);
849 /* the test is responsible for resetting too */
850 current_trace
= saved_tracer
;
852 printk(KERN_CONT
"FAILED!\n");
855 /* Only reset on passing, to avoid touching corrupted buffers */
856 tracing_reset_online_cpus(tr
);
858 /* Shrink the max buffer again */
859 if (ring_buffer_expanded
&& type
->use_max_tr
)
860 ring_buffer_resize(max_tr
.buffer
, 1);
862 printk(KERN_CONT
"PASSED\n");
866 type
->next
= trace_types
;
870 tracing_selftest_running
= false;
871 mutex_unlock(&trace_types_lock
);
873 if (ret
|| !default_bootup_tracer
)
876 if (strncmp(default_bootup_tracer
, type
->name
, MAX_TRACER_SIZE
))
879 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
880 /* Do we want this tracer to start on bootup? */
881 tracing_set_tracer(type
->name
);
882 default_bootup_tracer
= NULL
;
883 /* disable other selftests, since this will break it. */
884 tracing_selftest_disabled
= 1;
885 #ifdef CONFIG_FTRACE_STARTUP_TEST
886 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
894 void unregister_tracer(struct tracer
*type
)
898 mutex_lock(&trace_types_lock
);
899 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
903 pr_info("Tracer %s not registered\n", type
->name
);
909 if (type
== current_trace
&& tracer_enabled
) {
912 if (current_trace
->stop
)
913 current_trace
->stop(&global_trace
);
914 current_trace
= &nop_trace
;
917 mutex_unlock(&trace_types_lock
);
920 static void __tracing_reset(struct ring_buffer
*buffer
, int cpu
)
922 ftrace_disable_cpu();
923 ring_buffer_reset_cpu(buffer
, cpu
);
927 void tracing_reset(struct trace_array
*tr
, int cpu
)
929 struct ring_buffer
*buffer
= tr
->buffer
;
931 ring_buffer_record_disable(buffer
);
933 /* Make sure all commits have finished */
935 __tracing_reset(buffer
, cpu
);
937 ring_buffer_record_enable(buffer
);
940 void tracing_reset_online_cpus(struct trace_array
*tr
)
942 struct ring_buffer
*buffer
= tr
->buffer
;
945 ring_buffer_record_disable(buffer
);
947 /* Make sure all commits have finished */
950 tr
->time_start
= ftrace_now(tr
->cpu
);
952 for_each_online_cpu(cpu
)
953 __tracing_reset(buffer
, cpu
);
955 ring_buffer_record_enable(buffer
);
958 void tracing_reset_current(int cpu
)
960 tracing_reset(&global_trace
, cpu
);
963 void tracing_reset_current_online_cpus(void)
965 tracing_reset_online_cpus(&global_trace
);
968 #define SAVED_CMDLINES 128
969 #define NO_CMDLINE_MAP UINT_MAX
970 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
971 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
972 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
973 static int cmdline_idx
;
974 static arch_spinlock_t trace_cmdline_lock
= __ARCH_SPIN_LOCK_UNLOCKED
;
976 /* temporary disable recording */
977 static atomic_t trace_record_cmdline_disabled __read_mostly
;
979 static void trace_init_cmdlines(void)
981 memset(&map_pid_to_cmdline
, NO_CMDLINE_MAP
, sizeof(map_pid_to_cmdline
));
982 memset(&map_cmdline_to_pid
, NO_CMDLINE_MAP
, sizeof(map_cmdline_to_pid
));
986 int is_tracing_stopped(void)
988 return trace_stop_count
;
992 * ftrace_off_permanent - disable all ftrace code permanently
994 * This should only be called when a serious anomally has
995 * been detected. This will turn off the function tracing,
996 * ring buffers, and other tracing utilites. It takes no
997 * locks and can be called from any context.
999 void ftrace_off_permanent(void)
1001 tracing_disabled
= 1;
1003 tracing_off_permanent();
1007 * tracing_start - quick start of the tracer
1009 * If tracing is enabled but was stopped by tracing_stop,
1010 * this will start the tracer back up.
1012 void tracing_start(void)
1014 struct ring_buffer
*buffer
;
1015 unsigned long flags
;
1017 if (tracing_disabled
)
1020 raw_spin_lock_irqsave(&tracing_start_lock
, flags
);
1021 if (--trace_stop_count
) {
1022 if (trace_stop_count
< 0) {
1023 /* Someone screwed up their debugging */
1025 trace_stop_count
= 0;
1030 /* Prevent the buffers from switching */
1031 arch_spin_lock(&ftrace_max_lock
);
1033 buffer
= global_trace
.buffer
;
1035 ring_buffer_record_enable(buffer
);
1037 buffer
= max_tr
.buffer
;
1039 ring_buffer_record_enable(buffer
);
1041 arch_spin_unlock(&ftrace_max_lock
);
1045 raw_spin_unlock_irqrestore(&tracing_start_lock
, flags
);
1049 * tracing_stop - quick stop of the tracer
1051 * Light weight way to stop tracing. Use in conjunction with
1054 void tracing_stop(void)
1056 struct ring_buffer
*buffer
;
1057 unsigned long flags
;
1060 raw_spin_lock_irqsave(&tracing_start_lock
, flags
);
1061 if (trace_stop_count
++)
1064 /* Prevent the buffers from switching */
1065 arch_spin_lock(&ftrace_max_lock
);
1067 buffer
= global_trace
.buffer
;
1069 ring_buffer_record_disable(buffer
);
1071 buffer
= max_tr
.buffer
;
1073 ring_buffer_record_disable(buffer
);
1075 arch_spin_unlock(&ftrace_max_lock
);
1078 raw_spin_unlock_irqrestore(&tracing_start_lock
, flags
);
1081 void trace_stop_cmdline_recording(void);
1083 static void trace_save_cmdline(struct task_struct
*tsk
)
1087 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
1091 * It's not the end of the world if we don't get
1092 * the lock, but we also don't want to spin
1093 * nor do we want to disable interrupts,
1094 * so if we miss here, then better luck next time.
1096 if (!arch_spin_trylock(&trace_cmdline_lock
))
1099 idx
= map_pid_to_cmdline
[tsk
->pid
];
1100 if (idx
== NO_CMDLINE_MAP
) {
1101 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
1104 * Check whether the cmdline buffer at idx has a pid
1105 * mapped. We are going to overwrite that entry so we
1106 * need to clear the map_pid_to_cmdline. Otherwise we
1107 * would read the new comm for the old pid.
1109 pid
= map_cmdline_to_pid
[idx
];
1110 if (pid
!= NO_CMDLINE_MAP
)
1111 map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
1113 map_cmdline_to_pid
[idx
] = tsk
->pid
;
1114 map_pid_to_cmdline
[tsk
->pid
] = idx
;
1119 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
1121 arch_spin_unlock(&trace_cmdline_lock
);
1124 void trace_find_cmdline(int pid
, char comm
[])
1129 strcpy(comm
, "<idle>");
1133 if (WARN_ON_ONCE(pid
< 0)) {
1134 strcpy(comm
, "<XXX>");
1138 if (pid
> PID_MAX_DEFAULT
) {
1139 strcpy(comm
, "<...>");
1144 arch_spin_lock(&trace_cmdline_lock
);
1145 map
= map_pid_to_cmdline
[pid
];
1146 if (map
!= NO_CMDLINE_MAP
)
1147 strcpy(comm
, saved_cmdlines
[map
]);
1149 strcpy(comm
, "<...>");
1151 arch_spin_unlock(&trace_cmdline_lock
);
1155 void tracing_record_cmdline(struct task_struct
*tsk
)
1157 if (atomic_read(&trace_record_cmdline_disabled
) || !tracer_enabled
||
1161 trace_save_cmdline(tsk
);
1165 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
1168 struct task_struct
*tsk
= current
;
1170 entry
->preempt_count
= pc
& 0xff;
1171 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
1174 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1175 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
1177 TRACE_FLAG_IRQS_NOSUPPORT
|
1179 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
1180 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
1181 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
1183 EXPORT_SYMBOL_GPL(tracing_generic_entry_update
);
1185 struct ring_buffer_event
*
1186 trace_buffer_lock_reserve(struct ring_buffer
*buffer
,
1189 unsigned long flags
, int pc
)
1191 struct ring_buffer_event
*event
;
1193 event
= ring_buffer_lock_reserve(buffer
, len
);
1194 if (event
!= NULL
) {
1195 struct trace_entry
*ent
= ring_buffer_event_data(event
);
1197 tracing_generic_entry_update(ent
, flags
, pc
);
1205 __trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1206 struct ring_buffer_event
*event
,
1207 unsigned long flags
, int pc
,
1210 ring_buffer_unlock_commit(buffer
, event
);
1212 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1213 ftrace_trace_userstack(buffer
, flags
, pc
);
1219 void trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1220 struct ring_buffer_event
*event
,
1221 unsigned long flags
, int pc
)
1223 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
1226 struct ring_buffer_event
*
1227 trace_current_buffer_lock_reserve(struct ring_buffer
**current_rb
,
1228 int type
, unsigned long len
,
1229 unsigned long flags
, int pc
)
1231 *current_rb
= global_trace
.buffer
;
1232 return trace_buffer_lock_reserve(*current_rb
,
1233 type
, len
, flags
, pc
);
1235 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve
);
1237 void trace_current_buffer_unlock_commit(struct ring_buffer
*buffer
,
1238 struct ring_buffer_event
*event
,
1239 unsigned long flags
, int pc
)
1241 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
1243 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit
);
1245 void trace_nowake_buffer_unlock_commit(struct ring_buffer
*buffer
,
1246 struct ring_buffer_event
*event
,
1247 unsigned long flags
, int pc
)
1249 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 0);
1251 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit
);
1253 void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer
*buffer
,
1254 struct ring_buffer_event
*event
,
1255 unsigned long flags
, int pc
,
1256 struct pt_regs
*regs
)
1258 ring_buffer_unlock_commit(buffer
, event
);
1260 ftrace_trace_stack_regs(buffer
, flags
, 0, pc
, regs
);
1261 ftrace_trace_userstack(buffer
, flags
, pc
);
1263 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs
);
1265 void trace_current_buffer_discard_commit(struct ring_buffer
*buffer
,
1266 struct ring_buffer_event
*event
)
1268 ring_buffer_discard_commit(buffer
, event
);
1270 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
1273 trace_function(struct trace_array
*tr
,
1274 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1277 struct ftrace_event_call
*call
= &event_function
;
1278 struct ring_buffer
*buffer
= tr
->buffer
;
1279 struct ring_buffer_event
*event
;
1280 struct ftrace_entry
*entry
;
1282 /* If we are reading the ring buffer, don't trace */
1283 if (unlikely(__this_cpu_read(ftrace_cpu_disabled
)))
1286 event
= trace_buffer_lock_reserve(buffer
, TRACE_FN
, sizeof(*entry
),
1290 entry
= ring_buffer_event_data(event
);
1292 entry
->parent_ip
= parent_ip
;
1294 if (!filter_check_discard(call
, entry
, buffer
, event
))
1295 ring_buffer_unlock_commit(buffer
, event
);
1299 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
1300 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1303 if (likely(!atomic_read(&data
->disabled
)))
1304 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
1307 #ifdef CONFIG_STACKTRACE
1309 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1310 struct ftrace_stack
{
1311 unsigned long calls
[FTRACE_STACK_MAX_ENTRIES
];
1314 static DEFINE_PER_CPU(struct ftrace_stack
, ftrace_stack
);
1315 static DEFINE_PER_CPU(int, ftrace_stack_reserve
);
1317 static void __ftrace_trace_stack(struct ring_buffer
*buffer
,
1318 unsigned long flags
,
1319 int skip
, int pc
, struct pt_regs
*regs
)
1321 struct ftrace_event_call
*call
= &event_kernel_stack
;
1322 struct ring_buffer_event
*event
;
1323 struct stack_entry
*entry
;
1324 struct stack_trace trace
;
1326 int size
= FTRACE_STACK_ENTRIES
;
1328 trace
.nr_entries
= 0;
1332 * Since events can happen in NMIs there's no safe way to
1333 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1334 * or NMI comes in, it will just have to use the default
1335 * FTRACE_STACK_SIZE.
1337 preempt_disable_notrace();
1339 use_stack
= ++__get_cpu_var(ftrace_stack_reserve
);
1341 * We don't need any atomic variables, just a barrier.
1342 * If an interrupt comes in, we don't care, because it would
1343 * have exited and put the counter back to what we want.
1344 * We just need a barrier to keep gcc from moving things
1348 if (use_stack
== 1) {
1349 trace
.entries
= &__get_cpu_var(ftrace_stack
).calls
[0];
1350 trace
.max_entries
= FTRACE_STACK_MAX_ENTRIES
;
1353 save_stack_trace_regs(regs
, &trace
);
1355 save_stack_trace(&trace
);
1357 if (trace
.nr_entries
> size
)
1358 size
= trace
.nr_entries
;
1360 /* From now on, use_stack is a boolean */
1363 size
*= sizeof(unsigned long);
1365 event
= trace_buffer_lock_reserve(buffer
, TRACE_STACK
,
1366 sizeof(*entry
) + size
, flags
, pc
);
1369 entry
= ring_buffer_event_data(event
);
1371 memset(&entry
->caller
, 0, size
);
1374 memcpy(&entry
->caller
, trace
.entries
,
1375 trace
.nr_entries
* sizeof(unsigned long));
1377 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1378 trace
.entries
= entry
->caller
;
1380 save_stack_trace_regs(regs
, &trace
);
1382 save_stack_trace(&trace
);
1385 entry
->size
= trace
.nr_entries
;
1387 if (!filter_check_discard(call
, entry
, buffer
, event
))
1388 ring_buffer_unlock_commit(buffer
, event
);
1391 /* Again, don't let gcc optimize things here */
1393 __get_cpu_var(ftrace_stack_reserve
)--;
1394 preempt_enable_notrace();
1398 void ftrace_trace_stack_regs(struct ring_buffer
*buffer
, unsigned long flags
,
1399 int skip
, int pc
, struct pt_regs
*regs
)
1401 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1404 __ftrace_trace_stack(buffer
, flags
, skip
, pc
, regs
);
1407 void ftrace_trace_stack(struct ring_buffer
*buffer
, unsigned long flags
,
1410 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1413 __ftrace_trace_stack(buffer
, flags
, skip
, pc
, NULL
);
1416 void __trace_stack(struct trace_array
*tr
, unsigned long flags
, int skip
,
1419 __ftrace_trace_stack(tr
->buffer
, flags
, skip
, pc
, NULL
);
1423 * trace_dump_stack - record a stack back trace in the trace buffer
1425 void trace_dump_stack(void)
1427 unsigned long flags
;
1429 if (tracing_disabled
|| tracing_selftest_running
)
1432 local_save_flags(flags
);
1434 /* skipping 3 traces, seems to get us at the caller of this function */
1435 __ftrace_trace_stack(global_trace
.buffer
, flags
, 3, preempt_count(), NULL
);
1438 static DEFINE_PER_CPU(int, user_stack_count
);
1441 ftrace_trace_userstack(struct ring_buffer
*buffer
, unsigned long flags
, int pc
)
1443 struct ftrace_event_call
*call
= &event_user_stack
;
1444 struct ring_buffer_event
*event
;
1445 struct userstack_entry
*entry
;
1446 struct stack_trace trace
;
1448 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1452 * NMIs can not handle page faults, even with fix ups.
1453 * The save user stack can (and often does) fault.
1455 if (unlikely(in_nmi()))
1459 * prevent recursion, since the user stack tracing may
1460 * trigger other kernel events.
1463 if (__this_cpu_read(user_stack_count
))
1466 __this_cpu_inc(user_stack_count
);
1468 event
= trace_buffer_lock_reserve(buffer
, TRACE_USER_STACK
,
1469 sizeof(*entry
), flags
, pc
);
1471 goto out_drop_count
;
1472 entry
= ring_buffer_event_data(event
);
1474 entry
->tgid
= current
->tgid
;
1475 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1477 trace
.nr_entries
= 0;
1478 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1480 trace
.entries
= entry
->caller
;
1482 save_stack_trace_user(&trace
);
1483 if (!filter_check_discard(call
, entry
, buffer
, event
))
1484 ring_buffer_unlock_commit(buffer
, event
);
1487 __this_cpu_dec(user_stack_count
);
1493 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1495 ftrace_trace_userstack(tr
, flags
, preempt_count());
1499 #endif /* CONFIG_STACKTRACE */
1501 /* created for use with alloc_percpu */
1502 struct trace_buffer_struct
{
1503 char buffer
[TRACE_BUF_SIZE
];
1506 static struct trace_buffer_struct
*trace_percpu_buffer
;
1507 static struct trace_buffer_struct
*trace_percpu_sirq_buffer
;
1508 static struct trace_buffer_struct
*trace_percpu_irq_buffer
;
1509 static struct trace_buffer_struct
*trace_percpu_nmi_buffer
;
1512 * The buffer used is dependent on the context. There is a per cpu
1513 * buffer for normal context, softirq contex, hard irq context and
1514 * for NMI context. Thise allows for lockless recording.
1516 * Note, if the buffers failed to be allocated, then this returns NULL
1518 static char *get_trace_buf(void)
1520 struct trace_buffer_struct
*percpu_buffer
;
1521 struct trace_buffer_struct
*buffer
;
1524 * If we have allocated per cpu buffers, then we do not
1525 * need to do any locking.
1528 percpu_buffer
= trace_percpu_nmi_buffer
;
1530 percpu_buffer
= trace_percpu_irq_buffer
;
1531 else if (in_softirq())
1532 percpu_buffer
= trace_percpu_sirq_buffer
;
1534 percpu_buffer
= trace_percpu_buffer
;
1539 buffer
= per_cpu_ptr(percpu_buffer
, smp_processor_id());
1541 return buffer
->buffer
;
1544 static int alloc_percpu_trace_buffer(void)
1546 struct trace_buffer_struct
*buffers
;
1547 struct trace_buffer_struct
*sirq_buffers
;
1548 struct trace_buffer_struct
*irq_buffers
;
1549 struct trace_buffer_struct
*nmi_buffers
;
1551 buffers
= alloc_percpu(struct trace_buffer_struct
);
1555 sirq_buffers
= alloc_percpu(struct trace_buffer_struct
);
1559 irq_buffers
= alloc_percpu(struct trace_buffer_struct
);
1563 nmi_buffers
= alloc_percpu(struct trace_buffer_struct
);
1567 trace_percpu_buffer
= buffers
;
1568 trace_percpu_sirq_buffer
= sirq_buffers
;
1569 trace_percpu_irq_buffer
= irq_buffers
;
1570 trace_percpu_nmi_buffer
= nmi_buffers
;
1575 free_percpu(irq_buffers
);
1577 free_percpu(sirq_buffers
);
1579 free_percpu(buffers
);
1581 WARN(1, "Could not allocate percpu trace_printk buffer");
1585 void trace_printk_init_buffers(void)
1587 static int buffers_allocated
;
1589 if (buffers_allocated
)
1592 if (alloc_percpu_trace_buffer())
1595 pr_info("ftrace: Allocated trace_printk buffers\n");
1597 buffers_allocated
= 1;
1601 * trace_vbprintk - write binary msg to tracing buffer
1604 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1606 struct ftrace_event_call
*call
= &event_bprint
;
1607 struct ring_buffer_event
*event
;
1608 struct ring_buffer
*buffer
;
1609 struct trace_array
*tr
= &global_trace
;
1610 struct bprint_entry
*entry
;
1611 unsigned long flags
;
1613 int len
= 0, size
, pc
;
1615 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1618 /* Don't pollute graph traces with trace_vprintk internals */
1619 pause_graph_tracing();
1621 pc
= preempt_count();
1622 preempt_disable_notrace();
1624 tbuffer
= get_trace_buf();
1630 len
= vbin_printf((u32
*)tbuffer
, TRACE_BUF_SIZE
/sizeof(int), fmt
, args
);
1632 if (len
> TRACE_BUF_SIZE
/sizeof(int) || len
< 0)
1635 local_save_flags(flags
);
1636 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1637 buffer
= tr
->buffer
;
1638 event
= trace_buffer_lock_reserve(buffer
, TRACE_BPRINT
, size
,
1642 entry
= ring_buffer_event_data(event
);
1646 memcpy(entry
->buf
, tbuffer
, sizeof(u32
) * len
);
1647 if (!filter_check_discard(call
, entry
, buffer
, event
)) {
1648 ring_buffer_unlock_commit(buffer
, event
);
1649 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1653 preempt_enable_notrace();
1654 unpause_graph_tracing();
1658 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1660 int trace_array_printk(struct trace_array
*tr
,
1661 unsigned long ip
, const char *fmt
, ...)
1666 if (!(trace_flags
& TRACE_ITER_PRINTK
))
1670 ret
= trace_array_vprintk(tr
, ip
, fmt
, ap
);
1675 int trace_array_vprintk(struct trace_array
*tr
,
1676 unsigned long ip
, const char *fmt
, va_list args
)
1678 struct ftrace_event_call
*call
= &event_print
;
1679 struct ring_buffer_event
*event
;
1680 struct ring_buffer
*buffer
;
1681 int len
= 0, size
, pc
;
1682 struct print_entry
*entry
;
1683 unsigned long flags
;
1686 if (tracing_disabled
|| tracing_selftest_running
)
1689 /* Don't pollute graph traces with trace_vprintk internals */
1690 pause_graph_tracing();
1692 pc
= preempt_count();
1693 preempt_disable_notrace();
1696 tbuffer
= get_trace_buf();
1702 len
= vsnprintf(tbuffer
, TRACE_BUF_SIZE
, fmt
, args
);
1703 if (len
> TRACE_BUF_SIZE
)
1706 local_save_flags(flags
);
1707 size
= sizeof(*entry
) + len
+ 1;
1708 buffer
= tr
->buffer
;
1709 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
1713 entry
= ring_buffer_event_data(event
);
1716 memcpy(&entry
->buf
, tbuffer
, len
);
1717 entry
->buf
[len
] = '\0';
1718 if (!filter_check_discard(call
, entry
, buffer
, event
)) {
1719 ring_buffer_unlock_commit(buffer
, event
);
1720 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1723 preempt_enable_notrace();
1724 unpause_graph_tracing();
1729 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1731 return trace_array_vprintk(&global_trace
, ip
, fmt
, args
);
1733 EXPORT_SYMBOL_GPL(trace_vprintk
);
1735 static void trace_iterator_increment(struct trace_iterator
*iter
)
1737 /* Don't allow ftrace to trace into the ring buffers */
1738 ftrace_disable_cpu();
1741 if (iter
->buffer_iter
[iter
->cpu
])
1742 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1744 ftrace_enable_cpu();
1747 static struct trace_entry
*
1748 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
,
1749 unsigned long *lost_events
)
1751 struct ring_buffer_event
*event
;
1752 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1754 /* Don't allow ftrace to trace into the ring buffers */
1755 ftrace_disable_cpu();
1758 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1760 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
,
1763 ftrace_enable_cpu();
1766 iter
->ent_size
= ring_buffer_event_length(event
);
1767 return ring_buffer_event_data(event
);
1773 static struct trace_entry
*
1774 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
,
1775 unsigned long *missing_events
, u64
*ent_ts
)
1777 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1778 struct trace_entry
*ent
, *next
= NULL
;
1779 unsigned long lost_events
= 0, next_lost
= 0;
1780 int cpu_file
= iter
->cpu_file
;
1781 u64 next_ts
= 0, ts
;
1787 * If we are in a per_cpu trace file, don't bother by iterating over
1788 * all cpu and peek directly.
1790 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1791 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1793 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
, missing_events
);
1795 *ent_cpu
= cpu_file
;
1800 for_each_tracing_cpu(cpu
) {
1802 if (ring_buffer_empty_cpu(buffer
, cpu
))
1805 ent
= peek_next_entry(iter
, cpu
, &ts
, &lost_events
);
1808 * Pick the entry with the smallest timestamp:
1810 if (ent
&& (!next
|| ts
< next_ts
)) {
1814 next_lost
= lost_events
;
1815 next_size
= iter
->ent_size
;
1819 iter
->ent_size
= next_size
;
1822 *ent_cpu
= next_cpu
;
1828 *missing_events
= next_lost
;
1833 /* Find the next real entry, without updating the iterator itself */
1834 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1835 int *ent_cpu
, u64
*ent_ts
)
1837 return __find_next_entry(iter
, ent_cpu
, NULL
, ent_ts
);
1840 /* Find the next real entry, and increment the iterator to the next entry */
1841 void *trace_find_next_entry_inc(struct trace_iterator
*iter
)
1843 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
,
1844 &iter
->lost_events
, &iter
->ts
);
1847 trace_iterator_increment(iter
);
1849 return iter
->ent
? iter
: NULL
;
1852 static void trace_consume(struct trace_iterator
*iter
)
1854 /* Don't allow ftrace to trace into the ring buffers */
1855 ftrace_disable_cpu();
1856 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
,
1857 &iter
->lost_events
);
1858 ftrace_enable_cpu();
1861 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1863 struct trace_iterator
*iter
= m
->private;
1867 WARN_ON_ONCE(iter
->leftover
);
1871 /* can't go backwards */
1876 ent
= trace_find_next_entry_inc(iter
);
1880 while (ent
&& iter
->idx
< i
)
1881 ent
= trace_find_next_entry_inc(iter
);
1888 void tracing_iter_reset(struct trace_iterator
*iter
, int cpu
)
1890 struct trace_array
*tr
= iter
->tr
;
1891 struct ring_buffer_event
*event
;
1892 struct ring_buffer_iter
*buf_iter
;
1893 unsigned long entries
= 0;
1896 tr
->data
[cpu
]->skipped_entries
= 0;
1898 if (!iter
->buffer_iter
[cpu
])
1901 buf_iter
= iter
->buffer_iter
[cpu
];
1902 ring_buffer_iter_reset(buf_iter
);
1905 * We could have the case with the max latency tracers
1906 * that a reset never took place on a cpu. This is evident
1907 * by the timestamp being before the start of the buffer.
1909 while ((event
= ring_buffer_iter_peek(buf_iter
, &ts
))) {
1910 if (ts
>= iter
->tr
->time_start
)
1913 ring_buffer_read(buf_iter
, NULL
);
1916 tr
->data
[cpu
]->skipped_entries
= entries
;
1920 * The current tracer is copied to avoid a global locking
1923 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1925 struct trace_iterator
*iter
= m
->private;
1926 static struct tracer
*old_tracer
;
1927 int cpu_file
= iter
->cpu_file
;
1932 /* copy the tracer to avoid using a global lock all around */
1933 mutex_lock(&trace_types_lock
);
1934 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1935 old_tracer
= current_trace
;
1936 *iter
->trace
= *current_trace
;
1938 mutex_unlock(&trace_types_lock
);
1940 atomic_inc(&trace_record_cmdline_disabled
);
1942 if (*pos
!= iter
->pos
) {
1947 ftrace_disable_cpu();
1949 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1950 for_each_tracing_cpu(cpu
)
1951 tracing_iter_reset(iter
, cpu
);
1953 tracing_iter_reset(iter
, cpu_file
);
1955 ftrace_enable_cpu();
1958 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1963 * If we overflowed the seq_file before, then we want
1964 * to just reuse the trace_seq buffer again.
1970 p
= s_next(m
, p
, &l
);
1974 trace_event_read_lock();
1975 trace_access_lock(cpu_file
);
1979 static void s_stop(struct seq_file
*m
, void *p
)
1981 struct trace_iterator
*iter
= m
->private;
1983 atomic_dec(&trace_record_cmdline_disabled
);
1984 trace_access_unlock(iter
->cpu_file
);
1985 trace_event_read_unlock();
1989 get_total_entries(struct trace_array
*tr
, unsigned long *total
, unsigned long *entries
)
1991 unsigned long count
;
1997 for_each_tracing_cpu(cpu
) {
1998 count
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
2000 * If this buffer has skipped entries, then we hold all
2001 * entries for the trace and we need to ignore the
2002 * ones before the time stamp.
2004 if (tr
->data
[cpu
]->skipped_entries
) {
2005 count
-= tr
->data
[cpu
]->skipped_entries
;
2006 /* total is the same as the entries */
2010 ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
2015 static void print_lat_help_header(struct seq_file
*m
)
2017 seq_puts(m
, "# _------=> CPU# \n");
2018 seq_puts(m
, "# / _-----=> irqs-off \n");
2019 seq_puts(m
, "# | / _----=> need-resched \n");
2020 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
2021 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
2022 seq_puts(m
, "# |||| / delay \n");
2023 seq_puts(m
, "# cmd pid ||||| time | caller \n");
2024 seq_puts(m
, "# \\ / ||||| \\ | / \n");
2027 static void print_event_info(struct trace_array
*tr
, struct seq_file
*m
)
2029 unsigned long total
;
2030 unsigned long entries
;
2032 get_total_entries(tr
, &total
, &entries
);
2033 seq_printf(m
, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2034 entries
, total
, num_online_cpus());
2038 static void print_func_help_header(struct trace_array
*tr
, struct seq_file
*m
)
2040 print_event_info(tr
, m
);
2041 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2042 seq_puts(m
, "# | | | | |\n");
2045 static void print_func_help_header_irq(struct trace_array
*tr
, struct seq_file
*m
)
2047 print_event_info(tr
, m
);
2048 seq_puts(m
, "# _-----=> irqs-off\n");
2049 seq_puts(m
, "# / _----=> need-resched\n");
2050 seq_puts(m
, "# | / _---=> hardirq/softirq\n");
2051 seq_puts(m
, "# || / _--=> preempt-depth\n");
2052 seq_puts(m
, "# ||| / delay\n");
2053 seq_puts(m
, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2054 seq_puts(m
, "# | | | |||| | |\n");
2058 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
2060 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
2061 struct trace_array
*tr
= iter
->tr
;
2062 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
2063 struct tracer
*type
= current_trace
;
2064 unsigned long entries
;
2065 unsigned long total
;
2066 const char *name
= "preemption";
2071 get_total_entries(tr
, &total
, &entries
);
2073 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
2075 seq_puts(m
, "# -----------------------------------"
2076 "---------------------------------\n");
2077 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2078 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2079 nsecs_to_usecs(data
->saved_latency
),
2083 #if defined(CONFIG_PREEMPT_NONE)
2085 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2087 #elif defined(CONFIG_PREEMPT)
2092 /* These are reserved for later use */
2095 seq_printf(m
, " #P:%d)\n", num_online_cpus());
2099 seq_puts(m
, "# -----------------\n");
2100 seq_printf(m
, "# | task: %.16s-%d "
2101 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2102 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
2103 data
->policy
, data
->rt_priority
);
2104 seq_puts(m
, "# -----------------\n");
2106 if (data
->critical_start
) {
2107 seq_puts(m
, "# => started at: ");
2108 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
2109 trace_print_seq(m
, &iter
->seq
);
2110 seq_puts(m
, "\n# => ended at: ");
2111 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
2112 trace_print_seq(m
, &iter
->seq
);
2113 seq_puts(m
, "\n#\n");
2119 static void test_cpu_buff_start(struct trace_iterator
*iter
)
2121 struct trace_seq
*s
= &iter
->seq
;
2123 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
2126 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
2129 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
2132 if (iter
->tr
->data
[iter
->cpu
]->skipped_entries
)
2135 cpumask_set_cpu(iter
->cpu
, iter
->started
);
2137 /* Don't print started cpu buffer for the first entry of the trace */
2139 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
2143 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
2145 struct trace_seq
*s
= &iter
->seq
;
2146 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
2147 struct trace_entry
*entry
;
2148 struct trace_event
*event
;
2152 test_cpu_buff_start(iter
);
2154 event
= ftrace_find_event(entry
->type
);
2156 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2157 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
2158 if (!trace_print_lat_context(iter
))
2161 if (!trace_print_context(iter
))
2167 return event
->funcs
->trace(iter
, sym_flags
, event
);
2169 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
2172 return TRACE_TYPE_HANDLED
;
2174 return TRACE_TYPE_PARTIAL_LINE
;
2177 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
2179 struct trace_seq
*s
= &iter
->seq
;
2180 struct trace_entry
*entry
;
2181 struct trace_event
*event
;
2185 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2186 if (!trace_seq_printf(s
, "%d %d %llu ",
2187 entry
->pid
, iter
->cpu
, iter
->ts
))
2191 event
= ftrace_find_event(entry
->type
);
2193 return event
->funcs
->raw(iter
, 0, event
);
2195 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
2198 return TRACE_TYPE_HANDLED
;
2200 return TRACE_TYPE_PARTIAL_LINE
;
2203 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
2205 struct trace_seq
*s
= &iter
->seq
;
2206 unsigned char newline
= '\n';
2207 struct trace_entry
*entry
;
2208 struct trace_event
*event
;
2212 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2213 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
2214 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
2215 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
2218 event
= ftrace_find_event(entry
->type
);
2220 enum print_line_t ret
= event
->funcs
->hex(iter
, 0, event
);
2221 if (ret
!= TRACE_TYPE_HANDLED
)
2225 SEQ_PUT_FIELD_RET(s
, newline
);
2227 return TRACE_TYPE_HANDLED
;
2230 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
2232 struct trace_seq
*s
= &iter
->seq
;
2233 struct trace_entry
*entry
;
2234 struct trace_event
*event
;
2238 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2239 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
2240 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
2241 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
2244 event
= ftrace_find_event(entry
->type
);
2245 return event
? event
->funcs
->binary(iter
, 0, event
) :
2249 int trace_empty(struct trace_iterator
*iter
)
2253 /* If we are looking at one CPU buffer, only check that one */
2254 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
2255 cpu
= iter
->cpu_file
;
2256 if (iter
->buffer_iter
[cpu
]) {
2257 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
2260 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
2266 for_each_tracing_cpu(cpu
) {
2267 if (iter
->buffer_iter
[cpu
]) {
2268 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
2271 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
2279 /* Called with trace_event_read_lock() held. */
2280 enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
2282 enum print_line_t ret
;
2284 if (iter
->lost_events
&&
2285 !trace_seq_printf(&iter
->seq
, "CPU:%d [LOST %lu EVENTS]\n",
2286 iter
->cpu
, iter
->lost_events
))
2287 return TRACE_TYPE_PARTIAL_LINE
;
2289 if (iter
->trace
&& iter
->trace
->print_line
) {
2290 ret
= iter
->trace
->print_line(iter
);
2291 if (ret
!= TRACE_TYPE_UNHANDLED
)
2295 if (iter
->ent
->type
== TRACE_BPRINT
&&
2296 trace_flags
& TRACE_ITER_PRINTK
&&
2297 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2298 return trace_print_bprintk_msg_only(iter
);
2300 if (iter
->ent
->type
== TRACE_PRINT
&&
2301 trace_flags
& TRACE_ITER_PRINTK
&&
2302 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2303 return trace_print_printk_msg_only(iter
);
2305 if (trace_flags
& TRACE_ITER_BIN
)
2306 return print_bin_fmt(iter
);
2308 if (trace_flags
& TRACE_ITER_HEX
)
2309 return print_hex_fmt(iter
);
2311 if (trace_flags
& TRACE_ITER_RAW
)
2312 return print_raw_fmt(iter
);
2314 return print_trace_fmt(iter
);
2317 void trace_latency_header(struct seq_file
*m
)
2319 struct trace_iterator
*iter
= m
->private;
2321 /* print nothing if the buffers are empty */
2322 if (trace_empty(iter
))
2325 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
2326 print_trace_header(m
, iter
);
2328 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2329 print_lat_help_header(m
);
2332 void trace_default_header(struct seq_file
*m
)
2334 struct trace_iterator
*iter
= m
->private;
2336 if (!(trace_flags
& TRACE_ITER_CONTEXT_INFO
))
2339 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
2340 /* print nothing if the buffers are empty */
2341 if (trace_empty(iter
))
2343 print_trace_header(m
, iter
);
2344 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2345 print_lat_help_header(m
);
2347 if (!(trace_flags
& TRACE_ITER_VERBOSE
)) {
2348 if (trace_flags
& TRACE_ITER_IRQ_INFO
)
2349 print_func_help_header_irq(iter
->tr
, m
);
2351 print_func_help_header(iter
->tr
, m
);
2356 static void test_ftrace_alive(struct seq_file
*m
)
2358 if (!ftrace_is_dead())
2360 seq_printf(m
, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2361 seq_printf(m
, "# MAY BE MISSING FUNCTION EVENTS\n");
2364 static int s_show(struct seq_file
*m
, void *v
)
2366 struct trace_iterator
*iter
= v
;
2369 if (iter
->ent
== NULL
) {
2371 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
2373 test_ftrace_alive(m
);
2375 if (iter
->trace
&& iter
->trace
->print_header
)
2376 iter
->trace
->print_header(m
);
2378 trace_default_header(m
);
2380 } else if (iter
->leftover
) {
2382 * If we filled the seq_file buffer earlier, we
2383 * want to just show it now.
2385 ret
= trace_print_seq(m
, &iter
->seq
);
2387 /* ret should this time be zero, but you never know */
2388 iter
->leftover
= ret
;
2391 print_trace_line(iter
);
2392 ret
= trace_print_seq(m
, &iter
->seq
);
2394 * If we overflow the seq_file buffer, then it will
2395 * ask us for this data again at start up.
2397 * ret is 0 if seq_file write succeeded.
2400 iter
->leftover
= ret
;
2406 static const struct seq_operations tracer_seq_ops
= {
2413 static struct trace_iterator
*
2414 __tracing_open(struct inode
*inode
, struct file
*file
)
2416 long cpu_file
= (long) inode
->i_private
;
2417 void *fail_ret
= ERR_PTR(-ENOMEM
);
2418 struct trace_iterator
*iter
;
2422 if (tracing_disabled
)
2423 return ERR_PTR(-ENODEV
);
2425 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2427 return ERR_PTR(-ENOMEM
);
2430 * We make a copy of the current tracer to avoid concurrent
2431 * changes on it while we are reading.
2433 mutex_lock(&trace_types_lock
);
2434 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2439 *iter
->trace
= *current_trace
;
2441 if (!zalloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
2444 if (current_trace
&& current_trace
->print_max
)
2447 iter
->tr
= &global_trace
;
2449 mutex_init(&iter
->mutex
);
2450 iter
->cpu_file
= cpu_file
;
2452 /* Notify the tracer early; before we stop tracing. */
2453 if (iter
->trace
&& iter
->trace
->open
)
2454 iter
->trace
->open(iter
);
2456 /* Annotate start of buffers if we had overruns */
2457 if (ring_buffer_overruns(iter
->tr
->buffer
))
2458 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
2460 /* stop the trace while dumping */
2463 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
2464 for_each_tracing_cpu(cpu
) {
2465 iter
->buffer_iter
[cpu
] =
2466 ring_buffer_read_prepare(iter
->tr
->buffer
, cpu
);
2468 ring_buffer_read_prepare_sync();
2469 for_each_tracing_cpu(cpu
) {
2470 ring_buffer_read_start(iter
->buffer_iter
[cpu
]);
2471 tracing_iter_reset(iter
, cpu
);
2474 cpu
= iter
->cpu_file
;
2475 iter
->buffer_iter
[cpu
] =
2476 ring_buffer_read_prepare(iter
->tr
->buffer
, cpu
);
2477 ring_buffer_read_prepare_sync();
2478 ring_buffer_read_start(iter
->buffer_iter
[cpu
]);
2479 tracing_iter_reset(iter
, cpu
);
2482 ret
= seq_open(file
, &tracer_seq_ops
);
2484 fail_ret
= ERR_PTR(ret
);
2488 m
= file
->private_data
;
2491 mutex_unlock(&trace_types_lock
);
2496 for_each_tracing_cpu(cpu
) {
2497 if (iter
->buffer_iter
[cpu
])
2498 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2500 free_cpumask_var(iter
->started
);
2503 mutex_unlock(&trace_types_lock
);
2510 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
2512 if (tracing_disabled
)
2515 filp
->private_data
= inode
->i_private
;
2519 static int tracing_release(struct inode
*inode
, struct file
*file
)
2521 struct seq_file
*m
= file
->private_data
;
2522 struct trace_iterator
*iter
;
2525 if (!(file
->f_mode
& FMODE_READ
))
2530 mutex_lock(&trace_types_lock
);
2531 for_each_tracing_cpu(cpu
) {
2532 if (iter
->buffer_iter
[cpu
])
2533 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2536 if (iter
->trace
&& iter
->trace
->close
)
2537 iter
->trace
->close(iter
);
2539 /* reenable tracing if it was previously enabled */
2541 mutex_unlock(&trace_types_lock
);
2543 seq_release(inode
, file
);
2544 mutex_destroy(&iter
->mutex
);
2545 free_cpumask_var(iter
->started
);
2551 static int tracing_open(struct inode
*inode
, struct file
*file
)
2553 struct trace_iterator
*iter
;
2556 /* If this file was open for write, then erase contents */
2557 if ((file
->f_mode
& FMODE_WRITE
) &&
2558 (file
->f_flags
& O_TRUNC
)) {
2559 long cpu
= (long) inode
->i_private
;
2561 if (cpu
== TRACE_PIPE_ALL_CPU
)
2562 tracing_reset_online_cpus(&global_trace
);
2564 tracing_reset(&global_trace
, cpu
);
2567 if (file
->f_mode
& FMODE_READ
) {
2568 iter
= __tracing_open(inode
, file
);
2570 ret
= PTR_ERR(iter
);
2571 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2572 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2578 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2580 struct tracer
*t
= v
;
2590 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2595 mutex_lock(&trace_types_lock
);
2596 for (t
= trace_types
; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2602 static void t_stop(struct seq_file
*m
, void *p
)
2604 mutex_unlock(&trace_types_lock
);
2607 static int t_show(struct seq_file
*m
, void *v
)
2609 struct tracer
*t
= v
;
2614 seq_printf(m
, "%s", t
->name
);
2623 static const struct seq_operations show_traces_seq_ops
= {
2630 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2632 if (tracing_disabled
)
2635 return seq_open(file
, &show_traces_seq_ops
);
2639 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
2640 size_t count
, loff_t
*ppos
)
2645 static loff_t
tracing_seek(struct file
*file
, loff_t offset
, int origin
)
2647 if (file
->f_mode
& FMODE_READ
)
2648 return seq_lseek(file
, offset
, origin
);
2653 static const struct file_operations tracing_fops
= {
2654 .open
= tracing_open
,
2656 .write
= tracing_write_stub
,
2657 .llseek
= tracing_seek
,
2658 .release
= tracing_release
,
2661 static const struct file_operations show_traces_fops
= {
2662 .open
= show_traces_open
,
2664 .release
= seq_release
,
2665 .llseek
= seq_lseek
,
2669 * Only trace on a CPU if the bitmask is set:
2671 static cpumask_var_t tracing_cpumask
;
2674 * The tracer itself will not take this lock, but still we want
2675 * to provide a consistent cpumask to user-space:
2677 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2680 * Temporary storage for the character representation of the
2681 * CPU bitmask (and one more byte for the newline):
2683 static char mask_str
[NR_CPUS
+ 1];
2686 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2687 size_t count
, loff_t
*ppos
)
2691 mutex_lock(&tracing_cpumask_update_lock
);
2693 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2694 if (count
- len
< 2) {
2698 len
+= sprintf(mask_str
+ len
, "\n");
2699 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2702 mutex_unlock(&tracing_cpumask_update_lock
);
2708 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2709 size_t count
, loff_t
*ppos
)
2712 cpumask_var_t tracing_cpumask_new
;
2714 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2717 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2721 mutex_lock(&tracing_cpumask_update_lock
);
2723 local_irq_disable();
2724 arch_spin_lock(&ftrace_max_lock
);
2725 for_each_tracing_cpu(cpu
) {
2727 * Increase/decrease the disabled counter if we are
2728 * about to flip a bit in the cpumask:
2730 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2731 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2732 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2734 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2735 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2736 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2739 arch_spin_unlock(&ftrace_max_lock
);
2742 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2744 mutex_unlock(&tracing_cpumask_update_lock
);
2745 free_cpumask_var(tracing_cpumask_new
);
2750 free_cpumask_var(tracing_cpumask_new
);
2755 static const struct file_operations tracing_cpumask_fops
= {
2756 .open
= tracing_open_generic
,
2757 .read
= tracing_cpumask_read
,
2758 .write
= tracing_cpumask_write
,
2759 .llseek
= generic_file_llseek
,
2762 static int tracing_trace_options_show(struct seq_file
*m
, void *v
)
2764 struct tracer_opt
*trace_opts
;
2768 mutex_lock(&trace_types_lock
);
2769 tracer_flags
= current_trace
->flags
->val
;
2770 trace_opts
= current_trace
->flags
->opts
;
2772 for (i
= 0; trace_options
[i
]; i
++) {
2773 if (trace_flags
& (1 << i
))
2774 seq_printf(m
, "%s\n", trace_options
[i
]);
2776 seq_printf(m
, "no%s\n", trace_options
[i
]);
2779 for (i
= 0; trace_opts
[i
].name
; i
++) {
2780 if (tracer_flags
& trace_opts
[i
].bit
)
2781 seq_printf(m
, "%s\n", trace_opts
[i
].name
);
2783 seq_printf(m
, "no%s\n", trace_opts
[i
].name
);
2785 mutex_unlock(&trace_types_lock
);
2790 static int __set_tracer_option(struct tracer
*trace
,
2791 struct tracer_flags
*tracer_flags
,
2792 struct tracer_opt
*opts
, int neg
)
2796 ret
= trace
->set_flag(tracer_flags
->val
, opts
->bit
, !neg
);
2801 tracer_flags
->val
&= ~opts
->bit
;
2803 tracer_flags
->val
|= opts
->bit
;
2807 /* Try to assign a tracer specific option */
2808 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2810 struct tracer_flags
*tracer_flags
= trace
->flags
;
2811 struct tracer_opt
*opts
= NULL
;
2814 for (i
= 0; tracer_flags
->opts
[i
].name
; i
++) {
2815 opts
= &tracer_flags
->opts
[i
];
2817 if (strcmp(cmp
, opts
->name
) == 0)
2818 return __set_tracer_option(trace
, trace
->flags
,
2825 static void set_tracer_flags(unsigned int mask
, int enabled
)
2827 /* do nothing if flag is already set */
2828 if (!!(trace_flags
& mask
) == !!enabled
)
2832 trace_flags
|= mask
;
2834 trace_flags
&= ~mask
;
2836 if (mask
== TRACE_ITER_RECORD_CMD
)
2837 trace_event_enable_cmd_record(enabled
);
2839 if (mask
== TRACE_ITER_OVERWRITE
)
2840 ring_buffer_change_overwrite(global_trace
.buffer
, enabled
);
2844 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2845 size_t cnt
, loff_t
*ppos
)
2853 if (cnt
>= sizeof(buf
))
2856 if (copy_from_user(&buf
, ubuf
, cnt
))
2860 cmp
= strstrip(buf
);
2862 if (strncmp(cmp
, "no", 2) == 0) {
2867 for (i
= 0; trace_options
[i
]; i
++) {
2868 if (strcmp(cmp
, trace_options
[i
]) == 0) {
2869 set_tracer_flags(1 << i
, !neg
);
2874 /* If no option could be set, test the specific tracer options */
2875 if (!trace_options
[i
]) {
2876 mutex_lock(&trace_types_lock
);
2877 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2878 mutex_unlock(&trace_types_lock
);
2888 static int tracing_trace_options_open(struct inode
*inode
, struct file
*file
)
2890 if (tracing_disabled
)
2892 return single_open(file
, tracing_trace_options_show
, NULL
);
2895 static const struct file_operations tracing_iter_fops
= {
2896 .open
= tracing_trace_options_open
,
2898 .llseek
= seq_lseek
,
2899 .release
= single_release
,
2900 .write
= tracing_trace_options_write
,
2903 static const char readme_msg
[] =
2904 "tracing mini-HOWTO:\n\n"
2905 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2906 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2907 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
2908 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2910 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
2911 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2913 "# cat /sys/kernel/debug/tracing/trace_options\n"
2914 "noprint-parent nosym-offset nosym-addr noverbose\n"
2915 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2916 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
2917 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2918 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
2922 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2923 size_t cnt
, loff_t
*ppos
)
2925 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2926 readme_msg
, strlen(readme_msg
));
2929 static const struct file_operations tracing_readme_fops
= {
2930 .open
= tracing_open_generic
,
2931 .read
= tracing_readme_read
,
2932 .llseek
= generic_file_llseek
,
2936 tracing_saved_cmdlines_read(struct file
*file
, char __user
*ubuf
,
2937 size_t cnt
, loff_t
*ppos
)
2946 file_buf
= kmalloc(SAVED_CMDLINES
*(16+TASK_COMM_LEN
), GFP_KERNEL
);
2950 buf_comm
= kmalloc(TASK_COMM_LEN
, GFP_KERNEL
);
2958 for (i
= 0; i
< SAVED_CMDLINES
; i
++) {
2961 pid
= map_cmdline_to_pid
[i
];
2962 if (pid
== -1 || pid
== NO_CMDLINE_MAP
)
2965 trace_find_cmdline(pid
, buf_comm
);
2966 r
= sprintf(buf
, "%d %s\n", pid
, buf_comm
);
2971 len
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
2980 static const struct file_operations tracing_saved_cmdlines_fops
= {
2981 .open
= tracing_open_generic
,
2982 .read
= tracing_saved_cmdlines_read
,
2983 .llseek
= generic_file_llseek
,
2987 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2988 size_t cnt
, loff_t
*ppos
)
2993 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2994 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2998 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2999 size_t cnt
, loff_t
*ppos
)
3001 struct trace_array
*tr
= filp
->private_data
;
3005 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
3011 mutex_lock(&trace_types_lock
);
3012 if (tracer_enabled
^ val
) {
3014 /* Only need to warn if this is used to change the state */
3015 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
3019 if (current_trace
->start
)
3020 current_trace
->start(tr
);
3025 if (current_trace
->stop
)
3026 current_trace
->stop(tr
);
3029 mutex_unlock(&trace_types_lock
);
3037 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
3038 size_t cnt
, loff_t
*ppos
)
3040 char buf
[MAX_TRACER_SIZE
+2];
3043 mutex_lock(&trace_types_lock
);
3045 r
= sprintf(buf
, "%s\n", current_trace
->name
);
3047 r
= sprintf(buf
, "\n");
3048 mutex_unlock(&trace_types_lock
);
3050 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3053 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
3055 tracing_reset_online_cpus(tr
);
3059 static int __tracing_resize_ring_buffer(unsigned long size
)
3064 * If kernel or user changes the size of the ring buffer
3065 * we use the size that was given, and we can forget about
3066 * expanding it later.
3068 ring_buffer_expanded
= 1;
3070 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
3074 if (!current_trace
->use_max_tr
)
3077 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
3081 r
= ring_buffer_resize(global_trace
.buffer
,
3082 global_trace
.entries
);
3085 * AARGH! We are left with different
3086 * size max buffer!!!!
3087 * The max buffer is our "snapshot" buffer.
3088 * When a tracer needs a snapshot (one of the
3089 * latency tracers), it swaps the max buffer
3090 * with the saved snap shot. We succeeded to
3091 * update the size of the main buffer, but failed to
3092 * update the size of the max buffer. But when we tried
3093 * to reset the main buffer to the original size, we
3094 * failed there too. This is very unlikely to
3095 * happen, but if it does, warn and kill all
3099 tracing_disabled
= 1;
3104 max_tr
.entries
= size
;
3106 global_trace
.entries
= size
;
3111 static ssize_t
tracing_resize_ring_buffer(unsigned long size
)
3113 int cpu
, ret
= size
;
3115 mutex_lock(&trace_types_lock
);
3119 /* disable all cpu buffers */
3120 for_each_tracing_cpu(cpu
) {
3121 if (global_trace
.data
[cpu
])
3122 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3123 if (max_tr
.data
[cpu
])
3124 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3127 if (size
!= global_trace
.entries
)
3128 ret
= __tracing_resize_ring_buffer(size
);
3133 for_each_tracing_cpu(cpu
) {
3134 if (global_trace
.data
[cpu
])
3135 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3136 if (max_tr
.data
[cpu
])
3137 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3141 mutex_unlock(&trace_types_lock
);
3148 * tracing_update_buffers - used by tracing facility to expand ring buffers
3150 * To save on memory when the tracing is never used on a system with it
3151 * configured in. The ring buffers are set to a minimum size. But once
3152 * a user starts to use the tracing facility, then they need to grow
3153 * to their default size.
3155 * This function is to be called when a tracer is about to be used.
3157 int tracing_update_buffers(void)
3161 mutex_lock(&trace_types_lock
);
3162 if (!ring_buffer_expanded
)
3163 ret
= __tracing_resize_ring_buffer(trace_buf_size
);
3164 mutex_unlock(&trace_types_lock
);
3169 struct trace_option_dentry
;
3171 static struct trace_option_dentry
*
3172 create_trace_option_files(struct tracer
*tracer
);
3175 destroy_trace_option_files(struct trace_option_dentry
*topts
);
3177 static int tracing_set_tracer(const char *buf
)
3179 static struct trace_option_dentry
*topts
;
3180 struct trace_array
*tr
= &global_trace
;
3184 mutex_lock(&trace_types_lock
);
3186 if (!ring_buffer_expanded
) {
3187 ret
= __tracing_resize_ring_buffer(trace_buf_size
);
3193 for (t
= trace_types
; t
; t
= t
->next
) {
3194 if (strcmp(t
->name
, buf
) == 0)
3201 if (t
== current_trace
)
3204 trace_branch_disable();
3205 if (current_trace
&& current_trace
->reset
)
3206 current_trace
->reset(tr
);
3207 if (current_trace
&& current_trace
->use_max_tr
) {
3209 * We don't free the ring buffer. instead, resize it because
3210 * The max_tr ring buffer has some state (e.g. ring->clock) and
3211 * we want preserve it.
3213 ring_buffer_resize(max_tr
.buffer
, 1);
3216 destroy_trace_option_files(topts
);
3220 topts
= create_trace_option_files(current_trace
);
3221 if (current_trace
->use_max_tr
) {
3222 ret
= ring_buffer_resize(max_tr
.buffer
, global_trace
.entries
);
3225 max_tr
.entries
= global_trace
.entries
;
3229 ret
= tracer_init(t
, tr
);
3234 trace_branch_enable(tr
);
3236 mutex_unlock(&trace_types_lock
);
3242 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
3243 size_t cnt
, loff_t
*ppos
)
3245 char buf
[MAX_TRACER_SIZE
+1];
3252 if (cnt
> MAX_TRACER_SIZE
)
3253 cnt
= MAX_TRACER_SIZE
;
3255 if (copy_from_user(&buf
, ubuf
, cnt
))
3260 /* strip ending whitespace. */
3261 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
3264 err
= tracing_set_tracer(buf
);
3274 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
3275 size_t cnt
, loff_t
*ppos
)
3277 unsigned long *ptr
= filp
->private_data
;
3281 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
3282 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
3283 if (r
> sizeof(buf
))
3285 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3289 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
3290 size_t cnt
, loff_t
*ppos
)
3292 unsigned long *ptr
= filp
->private_data
;
3296 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
3305 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
3307 long cpu_file
= (long) inode
->i_private
;
3308 struct trace_iterator
*iter
;
3311 if (tracing_disabled
)
3314 mutex_lock(&trace_types_lock
);
3316 /* create a buffer to store the information to pass to userspace */
3317 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
3324 * We make a copy of the current tracer to avoid concurrent
3325 * changes on it while we are reading.
3327 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
3333 *iter
->trace
= *current_trace
;
3335 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
3340 /* trace pipe does not show start of buffer */
3341 cpumask_setall(iter
->started
);
3343 if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
3344 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
3346 iter
->cpu_file
= cpu_file
;
3347 iter
->tr
= &global_trace
;
3348 mutex_init(&iter
->mutex
);
3349 filp
->private_data
= iter
;
3351 if (iter
->trace
->pipe_open
)
3352 iter
->trace
->pipe_open(iter
);
3354 nonseekable_open(inode
, filp
);
3356 mutex_unlock(&trace_types_lock
);
3362 mutex_unlock(&trace_types_lock
);
3366 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
3368 struct trace_iterator
*iter
= file
->private_data
;
3370 mutex_lock(&trace_types_lock
);
3372 if (iter
->trace
->pipe_close
)
3373 iter
->trace
->pipe_close(iter
);
3375 mutex_unlock(&trace_types_lock
);
3377 free_cpumask_var(iter
->started
);
3378 mutex_destroy(&iter
->mutex
);
3386 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
3388 struct trace_iterator
*iter
= filp
->private_data
;
3390 if (trace_flags
& TRACE_ITER_BLOCK
) {
3392 * Always select as readable when in blocking mode
3394 return POLLIN
| POLLRDNORM
;
3396 if (!trace_empty(iter
))
3397 return POLLIN
| POLLRDNORM
;
3398 poll_wait(filp
, &trace_wait
, poll_table
);
3399 if (!trace_empty(iter
))
3400 return POLLIN
| POLLRDNORM
;
3407 void default_wait_pipe(struct trace_iterator
*iter
)
3411 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
3413 if (trace_empty(iter
))
3416 finish_wait(&trace_wait
, &wait
);
3420 * This is a make-shift waitqueue.
3421 * A tracer might use this callback on some rare cases:
3423 * 1) the current tracer might hold the runqueue lock when it wakes up
3424 * a reader, hence a deadlock (sched, function, and function graph tracers)
3425 * 2) the function tracers, trace all functions, we don't want
3426 * the overhead of calling wake_up and friends
3427 * (and tracing them too)
3429 * Anyway, this is really very primitive wakeup.
3431 void poll_wait_pipe(struct trace_iterator
*iter
)
3433 set_current_state(TASK_INTERRUPTIBLE
);
3434 /* sleep for 100 msecs, and try again. */
3435 schedule_timeout(HZ
/ 10);
3438 /* Must be called with trace_types_lock mutex held. */
3439 static int tracing_wait_pipe(struct file
*filp
)
3441 struct trace_iterator
*iter
= filp
->private_data
;
3443 while (trace_empty(iter
)) {
3445 if ((filp
->f_flags
& O_NONBLOCK
)) {
3449 mutex_unlock(&iter
->mutex
);
3451 iter
->trace
->wait_pipe(iter
);
3453 mutex_lock(&iter
->mutex
);
3455 if (signal_pending(current
))
3459 * We block until we read something and tracing is disabled.
3460 * We still block if tracing is disabled, but we have never
3461 * read anything. This allows a user to cat this file, and
3462 * then enable tracing. But after we have read something,
3463 * we give an EOF when tracing is again disabled.
3465 * iter->pos will be 0 if we haven't read anything.
3467 if (!tracer_enabled
&& iter
->pos
)
3478 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
3479 size_t cnt
, loff_t
*ppos
)
3481 struct trace_iterator
*iter
= filp
->private_data
;
3482 static struct tracer
*old_tracer
;
3485 /* return any leftover data */
3486 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3490 trace_seq_init(&iter
->seq
);
3492 /* copy the tracer to avoid using a global lock all around */
3493 mutex_lock(&trace_types_lock
);
3494 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3495 old_tracer
= current_trace
;
3496 *iter
->trace
= *current_trace
;
3498 mutex_unlock(&trace_types_lock
);
3501 * Avoid more than one consumer on a single file descriptor
3502 * This is just a matter of traces coherency, the ring buffer itself
3505 mutex_lock(&iter
->mutex
);
3506 if (iter
->trace
->read
) {
3507 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
3513 sret
= tracing_wait_pipe(filp
);
3517 /* stop when tracing is finished */
3518 if (trace_empty(iter
)) {
3523 if (cnt
>= PAGE_SIZE
)
3524 cnt
= PAGE_SIZE
- 1;
3526 /* reset all but tr, trace, and overruns */
3527 memset(&iter
->seq
, 0,
3528 sizeof(struct trace_iterator
) -
3529 offsetof(struct trace_iterator
, seq
));
3532 trace_event_read_lock();
3533 trace_access_lock(iter
->cpu_file
);
3534 while (trace_find_next_entry_inc(iter
) != NULL
) {
3535 enum print_line_t ret
;
3536 int len
= iter
->seq
.len
;
3538 ret
= print_trace_line(iter
);
3539 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3540 /* don't print partial lines */
3541 iter
->seq
.len
= len
;
3544 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3545 trace_consume(iter
);
3547 if (iter
->seq
.len
>= cnt
)
3551 * Setting the full flag means we reached the trace_seq buffer
3552 * size and we should leave by partial output condition above.
3553 * One of the trace_seq_* functions is not used properly.
3555 WARN_ONCE(iter
->seq
.full
, "full flag set for trace type %d",
3558 trace_access_unlock(iter
->cpu_file
);
3559 trace_event_read_unlock();
3561 /* Now copy what we have to the user */
3562 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3563 if (iter
->seq
.readpos
>= iter
->seq
.len
)
3564 trace_seq_init(&iter
->seq
);
3567 * If there was nothing to send to user, in spite of consuming trace
3568 * entries, go back to wait for more entries.
3574 mutex_unlock(&iter
->mutex
);
3579 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
3580 struct pipe_buffer
*buf
)
3582 __free_page(buf
->page
);
3585 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
3588 __free_page(spd
->pages
[idx
]);
3591 static const struct pipe_buf_operations tracing_pipe_buf_ops
= {
3593 .map
= generic_pipe_buf_map
,
3594 .unmap
= generic_pipe_buf_unmap
,
3595 .confirm
= generic_pipe_buf_confirm
,
3596 .release
= tracing_pipe_buf_release
,
3597 .steal
= generic_pipe_buf_steal
,
3598 .get
= generic_pipe_buf_get
,
3602 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
3607 /* Seq buffer is page-sized, exactly what we need. */
3609 count
= iter
->seq
.len
;
3610 ret
= print_trace_line(iter
);
3611 count
= iter
->seq
.len
- count
;
3614 iter
->seq
.len
-= count
;
3617 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3618 iter
->seq
.len
-= count
;
3622 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3623 trace_consume(iter
);
3625 if (!trace_find_next_entry_inc(iter
)) {
3635 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3637 struct pipe_inode_info
*pipe
,
3641 struct page
*pages_def
[PIPE_DEF_BUFFERS
];
3642 struct partial_page partial_def
[PIPE_DEF_BUFFERS
];
3643 struct trace_iterator
*iter
= filp
->private_data
;
3644 struct splice_pipe_desc spd
= {
3646 .partial
= partial_def
,
3647 .nr_pages
= 0, /* This gets updated below. */
3649 .ops
= &tracing_pipe_buf_ops
,
3650 .spd_release
= tracing_spd_release_pipe
,
3652 static struct tracer
*old_tracer
;
3657 if (splice_grow_spd(pipe
, &spd
))
3660 /* copy the tracer to avoid using a global lock all around */
3661 mutex_lock(&trace_types_lock
);
3662 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3663 old_tracer
= current_trace
;
3664 *iter
->trace
= *current_trace
;
3666 mutex_unlock(&trace_types_lock
);
3668 mutex_lock(&iter
->mutex
);
3670 if (iter
->trace
->splice_read
) {
3671 ret
= iter
->trace
->splice_read(iter
, filp
,
3672 ppos
, pipe
, len
, flags
);
3677 ret
= tracing_wait_pipe(filp
);
3681 if (!iter
->ent
&& !trace_find_next_entry_inc(iter
)) {
3686 trace_event_read_lock();
3687 trace_access_lock(iter
->cpu_file
);
3689 /* Fill as many pages as possible. */
3690 for (i
= 0, rem
= len
; i
< pipe
->buffers
&& rem
; i
++) {
3691 spd
.pages
[i
] = alloc_page(GFP_KERNEL
);
3695 rem
= tracing_fill_pipe_page(rem
, iter
);
3697 /* Copy the data into the page, so we can start over. */
3698 ret
= trace_seq_to_buffer(&iter
->seq
,
3699 page_address(spd
.pages
[i
]),
3702 __free_page(spd
.pages
[i
]);
3705 spd
.partial
[i
].offset
= 0;
3706 spd
.partial
[i
].len
= iter
->seq
.len
;
3708 trace_seq_init(&iter
->seq
);
3711 trace_access_unlock(iter
->cpu_file
);
3712 trace_event_read_unlock();
3713 mutex_unlock(&iter
->mutex
);
3717 ret
= splice_to_pipe(pipe
, &spd
);
3719 splice_shrink_spd(pipe
, &spd
);
3723 mutex_unlock(&iter
->mutex
);
3728 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3729 size_t cnt
, loff_t
*ppos
)
3731 struct trace_array
*tr
= filp
->private_data
;
3735 mutex_lock(&trace_types_lock
);
3736 if (!ring_buffer_expanded
)
3737 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3739 trace_buf_size
>> 10);
3741 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3742 mutex_unlock(&trace_types_lock
);
3744 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3748 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3749 size_t cnt
, loff_t
*ppos
)
3754 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
3758 /* must have at least 1 entry */
3762 /* value is in KB */
3765 ret
= tracing_resize_ring_buffer(val
);
3775 tracing_total_entries_read(struct file
*filp
, char __user
*ubuf
,
3776 size_t cnt
, loff_t
*ppos
)
3778 struct trace_array
*tr
= filp
->private_data
;
3781 unsigned long size
= 0, expanded_size
= 0;
3783 mutex_lock(&trace_types_lock
);
3784 for_each_tracing_cpu(cpu
) {
3785 size
+= tr
->entries
>> 10;
3786 if (!ring_buffer_expanded
)
3787 expanded_size
+= trace_buf_size
>> 10;
3789 if (ring_buffer_expanded
)
3790 r
= sprintf(buf
, "%lu\n", size
);
3792 r
= sprintf(buf
, "%lu (expanded: %lu)\n", size
, expanded_size
);
3793 mutex_unlock(&trace_types_lock
);
3795 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3799 tracing_free_buffer_write(struct file
*filp
, const char __user
*ubuf
,
3800 size_t cnt
, loff_t
*ppos
)
3803 * There is no need to read what the user has written, this function
3804 * is just to make sure that there is no error when "echo" is used
3813 tracing_free_buffer_release(struct inode
*inode
, struct file
*filp
)
3815 /* disable tracing ? */
3816 if (trace_flags
& TRACE_ITER_STOP_ON_FREE
)
3818 /* resize the ring buffer to 0 */
3819 tracing_resize_ring_buffer(0);
3825 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3826 size_t cnt
, loff_t
*fpos
)
3828 unsigned long addr
= (unsigned long)ubuf
;
3829 struct ring_buffer_event
*event
;
3830 struct ring_buffer
*buffer
;
3831 struct print_entry
*entry
;
3832 unsigned long irq_flags
;
3833 struct page
*pages
[2];
3843 if (tracing_disabled
)
3846 if (cnt
> TRACE_BUF_SIZE
)
3847 cnt
= TRACE_BUF_SIZE
;
3850 * Userspace is injecting traces into the kernel trace buffer.
3851 * We want to be as non intrusive as possible.
3852 * To do so, we do not want to allocate any special buffers
3853 * or take any locks, but instead write the userspace data
3854 * straight into the ring buffer.
3856 * First we need to pin the userspace buffer into memory,
3857 * which, most likely it is, because it just referenced it.
3858 * But there's no guarantee that it is. By using get_user_pages_fast()
3859 * and kmap_atomic/kunmap_atomic() we can get access to the
3860 * pages directly. We then write the data directly into the
3863 BUILD_BUG_ON(TRACE_BUF_SIZE
>= PAGE_SIZE
);
3865 /* check if we cross pages */
3866 if ((addr
& PAGE_MASK
) != ((addr
+ cnt
) & PAGE_MASK
))
3869 offset
= addr
& (PAGE_SIZE
- 1);
3872 ret
= get_user_pages_fast(addr
, nr_pages
, 0, pages
);
3873 if (ret
< nr_pages
) {
3875 put_page(pages
[ret
]);
3880 page1
= kmap_atomic(pages
[0]);
3882 page2
= kmap_atomic(pages
[1]);
3884 local_save_flags(irq_flags
);
3885 size
= sizeof(*entry
) + cnt
+ 2; /* possible \n added */
3886 buffer
= global_trace
.buffer
;
3887 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
3888 irq_flags
, preempt_count());
3890 /* Ring buffer disabled, return as if not open for write */
3895 entry
= ring_buffer_event_data(event
);
3896 entry
->ip
= _THIS_IP_
;
3898 if (nr_pages
== 2) {
3899 len
= PAGE_SIZE
- offset
;
3900 memcpy(&entry
->buf
, page1
+ offset
, len
);
3901 memcpy(&entry
->buf
[len
], page2
, cnt
- len
);
3903 memcpy(&entry
->buf
, page1
+ offset
, cnt
);
3905 if (entry
->buf
[cnt
- 1] != '\n') {
3906 entry
->buf
[cnt
] = '\n';
3907 entry
->buf
[cnt
+ 1] = '\0';
3909 entry
->buf
[cnt
] = '\0';
3911 ring_buffer_unlock_commit(buffer
, event
);
3919 kunmap_atomic(page2
);
3920 kunmap_atomic(page1
);
3921 while (nr_pages
> 0)
3922 put_page(pages
[--nr_pages
]);
3927 static int tracing_clock_show(struct seq_file
*m
, void *v
)
3931 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++)
3933 "%s%s%s%s", i
? " " : "",
3934 i
== trace_clock_id
? "[" : "", trace_clocks
[i
].name
,
3935 i
== trace_clock_id
? "]" : "");
3941 static ssize_t
tracing_clock_write(struct file
*filp
, const char __user
*ubuf
,
3942 size_t cnt
, loff_t
*fpos
)
3945 const char *clockstr
;
3948 if (cnt
>= sizeof(buf
))
3951 if (copy_from_user(&buf
, ubuf
, cnt
))
3956 clockstr
= strstrip(buf
);
3958 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++) {
3959 if (strcmp(trace_clocks
[i
].name
, clockstr
) == 0)
3962 if (i
== ARRAY_SIZE(trace_clocks
))
3967 mutex_lock(&trace_types_lock
);
3969 ring_buffer_set_clock(global_trace
.buffer
, trace_clocks
[i
].func
);
3971 ring_buffer_set_clock(max_tr
.buffer
, trace_clocks
[i
].func
);
3973 mutex_unlock(&trace_types_lock
);
3980 static int tracing_clock_open(struct inode
*inode
, struct file
*file
)
3982 if (tracing_disabled
)
3984 return single_open(file
, tracing_clock_show
, NULL
);
3987 static const struct file_operations tracing_max_lat_fops
= {
3988 .open
= tracing_open_generic
,
3989 .read
= tracing_max_lat_read
,
3990 .write
= tracing_max_lat_write
,
3991 .llseek
= generic_file_llseek
,
3994 static const struct file_operations tracing_ctrl_fops
= {
3995 .open
= tracing_open_generic
,
3996 .read
= tracing_ctrl_read
,
3997 .write
= tracing_ctrl_write
,
3998 .llseek
= generic_file_llseek
,
4001 static const struct file_operations set_tracer_fops
= {
4002 .open
= tracing_open_generic
,
4003 .read
= tracing_set_trace_read
,
4004 .write
= tracing_set_trace_write
,
4005 .llseek
= generic_file_llseek
,
4008 static const struct file_operations tracing_pipe_fops
= {
4009 .open
= tracing_open_pipe
,
4010 .poll
= tracing_poll_pipe
,
4011 .read
= tracing_read_pipe
,
4012 .splice_read
= tracing_splice_read_pipe
,
4013 .release
= tracing_release_pipe
,
4014 .llseek
= no_llseek
,
4017 static const struct file_operations tracing_entries_fops
= {
4018 .open
= tracing_open_generic
,
4019 .read
= tracing_entries_read
,
4020 .write
= tracing_entries_write
,
4021 .llseek
= generic_file_llseek
,
4024 static const struct file_operations tracing_total_entries_fops
= {
4025 .open
= tracing_open_generic
,
4026 .read
= tracing_total_entries_read
,
4027 .llseek
= generic_file_llseek
,
4030 static const struct file_operations tracing_free_buffer_fops
= {
4031 .write
= tracing_free_buffer_write
,
4032 .release
= tracing_free_buffer_release
,
4035 static const struct file_operations tracing_mark_fops
= {
4036 .open
= tracing_open_generic
,
4037 .write
= tracing_mark_write
,
4038 .llseek
= generic_file_llseek
,
4041 static const struct file_operations trace_clock_fops
= {
4042 .open
= tracing_clock_open
,
4044 .llseek
= seq_lseek
,
4045 .release
= single_release
,
4046 .write
= tracing_clock_write
,
4049 struct ftrace_buffer_info
{
4050 struct trace_array
*tr
;
4056 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
4058 int cpu
= (int)(long)inode
->i_private
;
4059 struct ftrace_buffer_info
*info
;
4061 if (tracing_disabled
)
4064 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
4068 info
->tr
= &global_trace
;
4071 /* Force reading ring buffer for first read */
4072 info
->read
= (unsigned int)-1;
4074 filp
->private_data
= info
;
4076 return nonseekable_open(inode
, filp
);
4080 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
4081 size_t count
, loff_t
*ppos
)
4083 struct ftrace_buffer_info
*info
= filp
->private_data
;
4091 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
, info
->cpu
);
4095 /* Do we have previous read data to read? */
4096 if (info
->read
< PAGE_SIZE
)
4099 trace_access_lock(info
->cpu
);
4100 ret
= ring_buffer_read_page(info
->tr
->buffer
,
4104 trace_access_unlock(info
->cpu
);
4111 size
= PAGE_SIZE
- info
->read
;
4115 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
4126 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
4128 struct ftrace_buffer_info
*info
= file
->private_data
;
4131 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
4138 struct ring_buffer
*buffer
;
4143 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
4144 struct pipe_buffer
*buf
)
4146 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
4151 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
4156 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
4157 struct pipe_buffer
*buf
)
4162 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
4163 struct pipe_buffer
*buf
)
4165 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
4170 /* Pipe buffer operations for a buffer. */
4171 static const struct pipe_buf_operations buffer_pipe_buf_ops
= {
4173 .map
= generic_pipe_buf_map
,
4174 .unmap
= generic_pipe_buf_unmap
,
4175 .confirm
= generic_pipe_buf_confirm
,
4176 .release
= buffer_pipe_buf_release
,
4177 .steal
= buffer_pipe_buf_steal
,
4178 .get
= buffer_pipe_buf_get
,
4182 * Callback from splice_to_pipe(), if we need to release some pages
4183 * at the end of the spd in case we error'ed out in filling the pipe.
4185 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
4187 struct buffer_ref
*ref
=
4188 (struct buffer_ref
*)spd
->partial
[i
].private;
4193 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
4195 spd
->partial
[i
].private = 0;
4199 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
4200 struct pipe_inode_info
*pipe
, size_t len
,
4203 struct ftrace_buffer_info
*info
= file
->private_data
;
4204 struct partial_page partial_def
[PIPE_DEF_BUFFERS
];
4205 struct page
*pages_def
[PIPE_DEF_BUFFERS
];
4206 struct splice_pipe_desc spd
= {
4208 .partial
= partial_def
,
4210 .ops
= &buffer_pipe_buf_ops
,
4211 .spd_release
= buffer_spd_release
,
4213 struct buffer_ref
*ref
;
4214 int entries
, size
, i
;
4217 if (splice_grow_spd(pipe
, &spd
))
4220 if (*ppos
& (PAGE_SIZE
- 1)) {
4221 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
4226 if (len
& (PAGE_SIZE
- 1)) {
4227 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
4228 if (len
< PAGE_SIZE
) {
4235 trace_access_lock(info
->cpu
);
4236 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
4238 for (i
= 0; i
< pipe
->buffers
&& len
&& entries
; i
++, len
-= PAGE_SIZE
) {
4242 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
4247 ref
->buffer
= info
->tr
->buffer
;
4248 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
, info
->cpu
);
4254 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
4257 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
4263 * zero out any left over data, this is going to
4266 size
= ring_buffer_page_len(ref
->page
);
4267 if (size
< PAGE_SIZE
)
4268 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
4270 page
= virt_to_page(ref
->page
);
4272 spd
.pages
[i
] = page
;
4273 spd
.partial
[i
].len
= PAGE_SIZE
;
4274 spd
.partial
[i
].offset
= 0;
4275 spd
.partial
[i
].private = (unsigned long)ref
;
4279 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
4282 trace_access_unlock(info
->cpu
);
4285 /* did we read anything? */
4286 if (!spd
.nr_pages
) {
4287 if (flags
& SPLICE_F_NONBLOCK
)
4295 ret
= splice_to_pipe(pipe
, &spd
);
4296 splice_shrink_spd(pipe
, &spd
);
4301 static const struct file_operations tracing_buffers_fops
= {
4302 .open
= tracing_buffers_open
,
4303 .read
= tracing_buffers_read
,
4304 .release
= tracing_buffers_release
,
4305 .splice_read
= tracing_buffers_splice_read
,
4306 .llseek
= no_llseek
,
4310 tracing_stats_read(struct file
*filp
, char __user
*ubuf
,
4311 size_t count
, loff_t
*ppos
)
4313 unsigned long cpu
= (unsigned long)filp
->private_data
;
4314 struct trace_array
*tr
= &global_trace
;
4315 struct trace_seq
*s
;
4317 unsigned long long t
;
4318 unsigned long usec_rem
;
4320 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
4326 cnt
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
4327 trace_seq_printf(s
, "entries: %ld\n", cnt
);
4329 cnt
= ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
4330 trace_seq_printf(s
, "overrun: %ld\n", cnt
);
4332 cnt
= ring_buffer_commit_overrun_cpu(tr
->buffer
, cpu
);
4333 trace_seq_printf(s
, "commit overrun: %ld\n", cnt
);
4335 cnt
= ring_buffer_bytes_cpu(tr
->buffer
, cpu
);
4336 trace_seq_printf(s
, "bytes: %ld\n", cnt
);
4338 t
= ns2usecs(ring_buffer_oldest_event_ts(tr
->buffer
, cpu
));
4339 usec_rem
= do_div(t
, USEC_PER_SEC
);
4340 trace_seq_printf(s
, "oldest event ts: %5llu.%06lu\n", t
, usec_rem
);
4342 t
= ns2usecs(ring_buffer_time_stamp(tr
->buffer
, cpu
));
4343 usec_rem
= do_div(t
, USEC_PER_SEC
);
4344 trace_seq_printf(s
, "now ts: %5llu.%06lu\n", t
, usec_rem
);
4346 count
= simple_read_from_buffer(ubuf
, count
, ppos
, s
->buffer
, s
->len
);
4353 static const struct file_operations tracing_stats_fops
= {
4354 .open
= tracing_open_generic
,
4355 .read
= tracing_stats_read
,
4356 .llseek
= generic_file_llseek
,
4359 #ifdef CONFIG_DYNAMIC_FTRACE
4361 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
4367 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
4368 size_t cnt
, loff_t
*ppos
)
4370 static char ftrace_dyn_info_buffer
[1024];
4371 static DEFINE_MUTEX(dyn_info_mutex
);
4372 unsigned long *p
= filp
->private_data
;
4373 char *buf
= ftrace_dyn_info_buffer
;
4374 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
4377 mutex_lock(&dyn_info_mutex
);
4378 r
= sprintf(buf
, "%ld ", *p
);
4380 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
4383 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
4385 mutex_unlock(&dyn_info_mutex
);
4390 static const struct file_operations tracing_dyn_info_fops
= {
4391 .open
= tracing_open_generic
,
4392 .read
= tracing_read_dyn_info
,
4393 .llseek
= generic_file_llseek
,
4397 static struct dentry
*d_tracer
;
4399 struct dentry
*tracing_init_dentry(void)
4406 if (!debugfs_initialized())
4409 d_tracer
= debugfs_create_dir("tracing", NULL
);
4411 if (!d_tracer
&& !once
) {
4413 pr_warning("Could not create debugfs directory 'tracing'\n");
4420 static struct dentry
*d_percpu
;
4422 struct dentry
*tracing_dentry_percpu(void)
4425 struct dentry
*d_tracer
;
4430 d_tracer
= tracing_init_dentry();
4435 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
4437 if (!d_percpu
&& !once
) {
4439 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4446 static void tracing_init_debugfs_percpu(long cpu
)
4448 struct dentry
*d_percpu
= tracing_dentry_percpu();
4449 struct dentry
*d_cpu
;
4450 char cpu_dir
[30]; /* 30 characters should be more than enough */
4452 snprintf(cpu_dir
, 30, "cpu%ld", cpu
);
4453 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
4455 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
4459 /* per cpu trace_pipe */
4460 trace_create_file("trace_pipe", 0444, d_cpu
,
4461 (void *) cpu
, &tracing_pipe_fops
);
4464 trace_create_file("trace", 0644, d_cpu
,
4465 (void *) cpu
, &tracing_fops
);
4467 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
4468 (void *) cpu
, &tracing_buffers_fops
);
4470 trace_create_file("stats", 0444, d_cpu
,
4471 (void *) cpu
, &tracing_stats_fops
);
4474 #ifdef CONFIG_FTRACE_SELFTEST
4475 /* Let selftest have access to static functions in this file */
4476 #include "trace_selftest.c"
4479 struct trace_option_dentry
{
4480 struct tracer_opt
*opt
;
4481 struct tracer_flags
*flags
;
4482 struct dentry
*entry
;
4486 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
4489 struct trace_option_dentry
*topt
= filp
->private_data
;
4492 if (topt
->flags
->val
& topt
->opt
->bit
)
4497 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
4501 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
4504 struct trace_option_dentry
*topt
= filp
->private_data
;
4508 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
4512 if (val
!= 0 && val
!= 1)
4515 if (!!(topt
->flags
->val
& topt
->opt
->bit
) != val
) {
4516 mutex_lock(&trace_types_lock
);
4517 ret
= __set_tracer_option(current_trace
, topt
->flags
,
4519 mutex_unlock(&trace_types_lock
);
4530 static const struct file_operations trace_options_fops
= {
4531 .open
= tracing_open_generic
,
4532 .read
= trace_options_read
,
4533 .write
= trace_options_write
,
4534 .llseek
= generic_file_llseek
,
4538 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
4541 long index
= (long)filp
->private_data
;
4544 if (trace_flags
& (1 << index
))
4549 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
4553 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
4556 long index
= (long)filp
->private_data
;
4560 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
4564 if (val
!= 0 && val
!= 1)
4566 set_tracer_flags(1 << index
, val
);
4573 static const struct file_operations trace_options_core_fops
= {
4574 .open
= tracing_open_generic
,
4575 .read
= trace_options_core_read
,
4576 .write
= trace_options_core_write
,
4577 .llseek
= generic_file_llseek
,
4580 struct dentry
*trace_create_file(const char *name
,
4582 struct dentry
*parent
,
4584 const struct file_operations
*fops
)
4588 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
4590 pr_warning("Could not create debugfs '%s' entry\n", name
);
4596 static struct dentry
*trace_options_init_dentry(void)
4598 struct dentry
*d_tracer
;
4599 static struct dentry
*t_options
;
4604 d_tracer
= tracing_init_dentry();
4608 t_options
= debugfs_create_dir("options", d_tracer
);
4610 pr_warning("Could not create debugfs directory 'options'\n");
4618 create_trace_option_file(struct trace_option_dentry
*topt
,
4619 struct tracer_flags
*flags
,
4620 struct tracer_opt
*opt
)
4622 struct dentry
*t_options
;
4624 t_options
= trace_options_init_dentry();
4628 topt
->flags
= flags
;
4631 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
4632 &trace_options_fops
);
4636 static struct trace_option_dentry
*
4637 create_trace_option_files(struct tracer
*tracer
)
4639 struct trace_option_dentry
*topts
;
4640 struct tracer_flags
*flags
;
4641 struct tracer_opt
*opts
;
4647 flags
= tracer
->flags
;
4649 if (!flags
|| !flags
->opts
)
4654 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4657 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
4661 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4662 create_trace_option_file(&topts
[cnt
], flags
,
4669 destroy_trace_option_files(struct trace_option_dentry
*topts
)
4676 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
4677 if (topts
[cnt
].entry
)
4678 debugfs_remove(topts
[cnt
].entry
);
4684 static struct dentry
*
4685 create_trace_option_core_file(const char *option
, long index
)
4687 struct dentry
*t_options
;
4689 t_options
= trace_options_init_dentry();
4693 return trace_create_file(option
, 0644, t_options
, (void *)index
,
4694 &trace_options_core_fops
);
4697 static __init
void create_trace_options_dir(void)
4699 struct dentry
*t_options
;
4702 t_options
= trace_options_init_dentry();
4706 for (i
= 0; trace_options
[i
]; i
++)
4707 create_trace_option_core_file(trace_options
[i
], i
);
4711 rb_simple_read(struct file
*filp
, char __user
*ubuf
,
4712 size_t cnt
, loff_t
*ppos
)
4714 struct ring_buffer
*buffer
= filp
->private_data
;
4719 r
= ring_buffer_record_is_on(buffer
);
4723 r
= sprintf(buf
, "%d\n", r
);
4725 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
4729 rb_simple_write(struct file
*filp
, const char __user
*ubuf
,
4730 size_t cnt
, loff_t
*ppos
)
4732 struct ring_buffer
*buffer
= filp
->private_data
;
4736 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
4742 ring_buffer_record_on(buffer
);
4744 ring_buffer_record_off(buffer
);
4752 static const struct file_operations rb_simple_fops
= {
4753 .open
= tracing_open_generic
,
4754 .read
= rb_simple_read
,
4755 .write
= rb_simple_write
,
4756 .llseek
= default_llseek
,
4759 static __init
int tracer_init_debugfs(void)
4761 struct dentry
*d_tracer
;
4764 trace_access_lock_init();
4766 d_tracer
= tracing_init_dentry();
4768 trace_create_file("tracing_enabled", 0644, d_tracer
,
4769 &global_trace
, &tracing_ctrl_fops
);
4771 trace_create_file("trace_options", 0644, d_tracer
,
4772 NULL
, &tracing_iter_fops
);
4774 trace_create_file("tracing_cpumask", 0644, d_tracer
,
4775 NULL
, &tracing_cpumask_fops
);
4777 trace_create_file("trace", 0644, d_tracer
,
4778 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
4780 trace_create_file("available_tracers", 0444, d_tracer
,
4781 &global_trace
, &show_traces_fops
);
4783 trace_create_file("current_tracer", 0644, d_tracer
,
4784 &global_trace
, &set_tracer_fops
);
4786 #ifdef CONFIG_TRACER_MAX_TRACE
4787 trace_create_file("tracing_max_latency", 0644, d_tracer
,
4788 &tracing_max_latency
, &tracing_max_lat_fops
);
4791 trace_create_file("tracing_thresh", 0644, d_tracer
,
4792 &tracing_thresh
, &tracing_max_lat_fops
);
4794 trace_create_file("README", 0444, d_tracer
,
4795 NULL
, &tracing_readme_fops
);
4797 trace_create_file("trace_pipe", 0444, d_tracer
,
4798 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
4800 trace_create_file("buffer_size_kb", 0644, d_tracer
,
4801 &global_trace
, &tracing_entries_fops
);
4803 trace_create_file("buffer_total_size_kb", 0444, d_tracer
,
4804 &global_trace
, &tracing_total_entries_fops
);
4806 trace_create_file("free_buffer", 0644, d_tracer
,
4807 &global_trace
, &tracing_free_buffer_fops
);
4809 trace_create_file("trace_marker", 0220, d_tracer
,
4810 NULL
, &tracing_mark_fops
);
4812 trace_create_file("saved_cmdlines", 0444, d_tracer
,
4813 NULL
, &tracing_saved_cmdlines_fops
);
4815 trace_create_file("trace_clock", 0644, d_tracer
, NULL
,
4818 trace_create_file("tracing_on", 0644, d_tracer
,
4819 global_trace
.buffer
, &rb_simple_fops
);
4821 #ifdef CONFIG_DYNAMIC_FTRACE
4822 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
4823 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
4826 create_trace_options_dir();
4828 for_each_tracing_cpu(cpu
)
4829 tracing_init_debugfs_percpu(cpu
);
4834 static int trace_panic_handler(struct notifier_block
*this,
4835 unsigned long event
, void *unused
)
4837 if (ftrace_dump_on_oops
)
4838 ftrace_dump(ftrace_dump_on_oops
);
4842 static struct notifier_block trace_panic_notifier
= {
4843 .notifier_call
= trace_panic_handler
,
4845 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
4848 static int trace_die_handler(struct notifier_block
*self
,
4854 if (ftrace_dump_on_oops
)
4855 ftrace_dump(ftrace_dump_on_oops
);
4863 static struct notifier_block trace_die_notifier
= {
4864 .notifier_call
= trace_die_handler
,
4869 * printk is set to max of 1024, we really don't need it that big.
4870 * Nothing should be printing 1000 characters anyway.
4872 #define TRACE_MAX_PRINT 1000
4875 * Define here KERN_TRACE so that we have one place to modify
4876 * it if we decide to change what log level the ftrace dump
4879 #define KERN_TRACE KERN_EMERG
4882 trace_printk_seq(struct trace_seq
*s
)
4884 /* Probably should print a warning here. */
4888 /* should be zero ended, but we are paranoid. */
4889 s
->buffer
[s
->len
] = 0;
4891 printk(KERN_TRACE
"%s", s
->buffer
);
4896 void trace_init_global_iter(struct trace_iterator
*iter
)
4898 iter
->tr
= &global_trace
;
4899 iter
->trace
= current_trace
;
4900 iter
->cpu_file
= TRACE_PIPE_ALL_CPU
;
4904 __ftrace_dump(bool disable_tracing
, enum ftrace_dump_mode oops_dump_mode
)
4906 static arch_spinlock_t ftrace_dump_lock
=
4907 (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
4908 /* use static because iter can be a bit big for the stack */
4909 static struct trace_iterator iter
;
4910 unsigned int old_userobj
;
4911 static int dump_ran
;
4912 unsigned long flags
;
4916 local_irq_save(flags
);
4917 arch_spin_lock(&ftrace_dump_lock
);
4925 /* Did function tracer already get disabled? */
4926 if (ftrace_is_dead()) {
4927 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
4928 printk("# MAY BE MISSING FUNCTION EVENTS\n");
4931 if (disable_tracing
)
4934 trace_init_global_iter(&iter
);
4936 for_each_tracing_cpu(cpu
) {
4937 atomic_inc(&iter
.tr
->data
[cpu
]->disabled
);
4940 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
4942 /* don't look at user memory in panic mode */
4943 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4945 /* Simulate the iterator */
4946 iter
.tr
= &global_trace
;
4947 iter
.trace
= current_trace
;
4949 switch (oops_dump_mode
) {
4951 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4954 iter
.cpu_file
= raw_smp_processor_id();
4959 printk(KERN_TRACE
"Bad dumping mode, switching to all CPUs dump\n");
4960 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4963 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4966 * We need to stop all tracing on all CPUS to read the
4967 * the next buffer. This is a bit expensive, but is
4968 * not done often. We fill all what we can read,
4969 * and then release the locks again.
4972 while (!trace_empty(&iter
)) {
4975 printk(KERN_TRACE
"---------------------------------\n");
4979 /* reset all but tr, trace, and overruns */
4980 memset(&iter
.seq
, 0,
4981 sizeof(struct trace_iterator
) -
4982 offsetof(struct trace_iterator
, seq
));
4983 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4986 if (trace_find_next_entry_inc(&iter
) != NULL
) {
4989 ret
= print_trace_line(&iter
);
4990 if (ret
!= TRACE_TYPE_NO_CONSUME
)
4991 trace_consume(&iter
);
4993 touch_nmi_watchdog();
4995 trace_printk_seq(&iter
.seq
);
4999 printk(KERN_TRACE
" (ftrace buffer empty)\n");
5001 printk(KERN_TRACE
"---------------------------------\n");
5004 /* Re-enable tracing if requested */
5005 if (!disable_tracing
) {
5006 trace_flags
|= old_userobj
;
5008 for_each_tracing_cpu(cpu
) {
5009 atomic_dec(&iter
.tr
->data
[cpu
]->disabled
);
5015 arch_spin_unlock(&ftrace_dump_lock
);
5016 local_irq_restore(flags
);
5019 /* By default: disable tracing after the dump */
5020 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode
)
5022 __ftrace_dump(true, oops_dump_mode
);
5024 EXPORT_SYMBOL_GPL(ftrace_dump
);
5026 __init
static int tracer_alloc_buffers(void)
5029 enum ring_buffer_flags rb_flags
;
5034 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
5037 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
5038 goto out_free_buffer_mask
;
5040 /* Only allocate trace_printk buffers if a trace_printk exists */
5041 if (__stop___trace_bprintk_fmt
!= __start___trace_bprintk_fmt
)
5042 trace_printk_init_buffers();
5044 /* To save memory, keep the ring buffer size to its minimum */
5045 if (ring_buffer_expanded
)
5046 ring_buf_size
= trace_buf_size
;
5050 rb_flags
= trace_flags
& TRACE_ITER_OVERWRITE
? RB_FL_OVERWRITE
: 0;
5052 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
5053 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
5055 /* TODO: make the number of buffers hot pluggable with CPUS */
5056 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
, rb_flags
);
5057 if (!global_trace
.buffer
) {
5058 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
5060 goto out_free_cpumask
;
5062 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
5063 if (global_trace
.buffer_disabled
)
5067 #ifdef CONFIG_TRACER_MAX_TRACE
5068 max_tr
.buffer
= ring_buffer_alloc(1, rb_flags
);
5069 if (!max_tr
.buffer
) {
5070 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
5072 ring_buffer_free(global_trace
.buffer
);
5073 goto out_free_cpumask
;
5078 /* Allocate the first page for all buffers */
5079 for_each_tracing_cpu(i
) {
5080 global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
5081 max_tr
.data
[i
] = &per_cpu(max_tr_data
, i
);
5084 trace_init_cmdlines();
5086 register_tracer(&nop_trace
);
5087 current_trace
= &nop_trace
;
5088 /* All seems OK, enable tracing */
5089 tracing_disabled
= 0;
5091 atomic_notifier_chain_register(&panic_notifier_list
,
5092 &trace_panic_notifier
);
5094 register_die_notifier(&trace_die_notifier
);
5099 free_cpumask_var(tracing_cpumask
);
5100 out_free_buffer_mask
:
5101 free_cpumask_var(tracing_buffer_mask
);
5106 __init
static int clear_boot_tracer(void)
5109 * The default tracer at boot buffer is an init section.
5110 * This function is called in lateinit. If we did not
5111 * find the boot tracer, then clear it out, to prevent
5112 * later registration from accessing the buffer that is
5113 * about to be freed.
5115 if (!default_bootup_tracer
)
5118 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
5119 default_bootup_tracer
);
5120 default_bootup_tracer
= NULL
;
5125 early_initcall(tracer_alloc_buffers
);
5126 fs_initcall(tracer_init_debugfs
);
5127 late_initcall(clear_boot_tracer
);