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 Nadia Yvette Chambers
14 #include <linux/ring_buffer.h>
15 #include <generated/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/irq_work.h>
23 #include <linux/debugfs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/kprobes.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/rwsem.h>
36 #include <linux/slab.h>
37 #include <linux/ctype.h>
38 #include <linux/init.h>
39 #include <linux/poll.h>
40 #include <linux/nmi.h>
42 #include <linux/sched/rt.h>
45 #include "trace_output.h"
48 * On boot up, the ring buffer is set to the minimum size, so that
49 * we do not waste memory on systems that are not using tracing.
51 int ring_buffer_expanded
;
54 * We need to change this state when a selftest is running.
55 * A selftest will lurk into the ring-buffer to count the
56 * entries inserted during the selftest although some concurrent
57 * insertions into the ring-buffer such as trace_printk could occurred
58 * at the same time, giving false positive or negative results.
60 static bool __read_mostly tracing_selftest_running
;
63 * If a tracer is running, we do not want to run SELFTEST.
65 bool __read_mostly tracing_selftest_disabled
;
67 /* For tracers that don't implement custom flags */
68 static struct tracer_opt dummy_tracer_opt
[] = {
72 static struct tracer_flags dummy_tracer_flags
= {
74 .opts
= dummy_tracer_opt
77 static int dummy_set_flag(u32 old_flags
, u32 bit
, int set
)
83 * To prevent the comm cache from being overwritten when no
84 * tracing is active, only save the comm when a trace event
87 static DEFINE_PER_CPU(bool, trace_cmdline_save
);
90 * When a reader is waiting for data, then this variable is
93 static bool trace_wakeup_needed
;
95 static struct irq_work trace_work_wakeup
;
98 * Kill all tracing for good (never come back).
99 * It is initialized to 1 but will turn to zero if the initialization
100 * of the tracer is successful. But that is the only place that sets
103 static int tracing_disabled
= 1;
105 DEFINE_PER_CPU(int, ftrace_cpu_disabled
);
107 cpumask_var_t __read_mostly tracing_buffer_mask
;
110 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
112 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
113 * is set, then ftrace_dump is called. This will output the contents
114 * of the ftrace buffers to the console. This is very useful for
115 * capturing traces that lead to crashes and outputing it to a
118 * It is default off, but you can enable it with either specifying
119 * "ftrace_dump_on_oops" in the kernel command line, or setting
120 * /proc/sys/kernel/ftrace_dump_on_oops
121 * Set 1 if you want to dump buffers of all CPUs
122 * Set 2 if you want to dump the buffer of the CPU that triggered oops
125 enum ftrace_dump_mode ftrace_dump_on_oops
;
127 static int tracing_set_tracer(const char *buf
);
129 #define MAX_TRACER_SIZE 100
130 static char bootup_tracer_buf
[MAX_TRACER_SIZE
] __initdata
;
131 static char *default_bootup_tracer
;
133 static int __init
set_cmdline_ftrace(char *str
)
135 strncpy(bootup_tracer_buf
, str
, MAX_TRACER_SIZE
);
136 default_bootup_tracer
= bootup_tracer_buf
;
137 /* We are using ftrace early, expand it */
138 ring_buffer_expanded
= 1;
141 __setup("ftrace=", set_cmdline_ftrace
);
143 static int __init
set_ftrace_dump_on_oops(char *str
)
145 if (*str
++ != '=' || !*str
) {
146 ftrace_dump_on_oops
= DUMP_ALL
;
150 if (!strcmp("orig_cpu", str
)) {
151 ftrace_dump_on_oops
= DUMP_ORIG
;
157 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
160 static char trace_boot_options_buf
[MAX_TRACER_SIZE
] __initdata
;
161 static char *trace_boot_options __initdata
;
163 static int __init
set_trace_boot_options(char *str
)
165 strncpy(trace_boot_options_buf
, str
, MAX_TRACER_SIZE
);
166 trace_boot_options
= trace_boot_options_buf
;
169 __setup("trace_options=", set_trace_boot_options
);
171 unsigned long long ns2usecs(cycle_t nsec
)
179 * The global_trace is the descriptor that holds the tracing
180 * buffers for the live tracing. For each CPU, it contains
181 * a link list of pages that will store trace entries. The
182 * page descriptor of the pages in the memory is used to hold
183 * the link list by linking the lru item in the page descriptor
184 * to each of the pages in the buffer per CPU.
186 * For each active CPU there is a data field that holds the
187 * pages for the buffer for that CPU. Each CPU has the same number
188 * of pages allocated for its buffer.
190 static struct trace_array global_trace
;
192 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
194 int filter_current_check_discard(struct ring_buffer
*buffer
,
195 struct ftrace_event_call
*call
, void *rec
,
196 struct ring_buffer_event
*event
)
198 return filter_check_discard(call
, rec
, buffer
, event
);
200 EXPORT_SYMBOL_GPL(filter_current_check_discard
);
202 cycle_t
ftrace_now(int cpu
)
206 /* Early boot up does not have a buffer yet */
207 if (!global_trace
.buffer
)
208 return trace_clock_local();
210 ts
= ring_buffer_time_stamp(global_trace
.buffer
, cpu
);
211 ring_buffer_normalize_time_stamp(global_trace
.buffer
, cpu
, &ts
);
217 * The max_tr is used to snapshot the global_trace when a maximum
218 * latency is reached. Some tracers will use this to store a maximum
219 * trace while it continues examining live traces.
221 * The buffers for the max_tr are set up the same as the global_trace.
222 * When a snapshot is taken, the link list of the max_tr is swapped
223 * with the link list of the global_trace and the buffers are reset for
224 * the global_trace so the tracing can continue.
226 static struct trace_array max_tr
;
228 static DEFINE_PER_CPU(struct trace_array_cpu
, max_tr_data
);
230 int tracing_is_enabled(void)
232 return tracing_is_on();
236 * trace_buf_size is the size in bytes that is allocated
237 * for a buffer. Note, the number of bytes is always rounded
240 * This number is purposely set to a low number of 16384.
241 * If the dump on oops happens, it will be much appreciated
242 * to not have to wait for all that output. Anyway this can be
243 * boot time and run time configurable.
245 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
247 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
249 /* trace_types holds a link list of available tracers. */
250 static struct tracer
*trace_types __read_mostly
;
252 /* current_trace points to the tracer that is currently active */
253 static struct tracer
*current_trace __read_mostly
= &nop_trace
;
256 * trace_types_lock is used to protect the trace_types list.
258 static DEFINE_MUTEX(trace_types_lock
);
261 * serialize the access of the ring buffer
263 * ring buffer serializes readers, but it is low level protection.
264 * The validity of the events (which returns by ring_buffer_peek() ..etc)
265 * are not protected by ring buffer.
267 * The content of events may become garbage if we allow other process consumes
268 * these events concurrently:
269 * A) the page of the consumed events may become a normal page
270 * (not reader page) in ring buffer, and this page will be rewrited
271 * by events producer.
272 * B) The page of the consumed events may become a page for splice_read,
273 * and this page will be returned to system.
275 * These primitives allow multi process access to different cpu ring buffer
278 * These primitives don't distinguish read-only and read-consume access.
279 * Multi read-only access are also serialized.
283 static DECLARE_RWSEM(all_cpu_access_lock
);
284 static DEFINE_PER_CPU(struct mutex
, cpu_access_lock
);
286 static inline void trace_access_lock(int cpu
)
288 if (cpu
== TRACE_PIPE_ALL_CPU
) {
289 /* gain it for accessing the whole ring buffer. */
290 down_write(&all_cpu_access_lock
);
292 /* gain it for accessing a cpu ring buffer. */
294 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
295 down_read(&all_cpu_access_lock
);
297 /* Secondly block other access to this @cpu ring buffer. */
298 mutex_lock(&per_cpu(cpu_access_lock
, cpu
));
302 static inline void trace_access_unlock(int cpu
)
304 if (cpu
== TRACE_PIPE_ALL_CPU
) {
305 up_write(&all_cpu_access_lock
);
307 mutex_unlock(&per_cpu(cpu_access_lock
, cpu
));
308 up_read(&all_cpu_access_lock
);
312 static inline void trace_access_lock_init(void)
316 for_each_possible_cpu(cpu
)
317 mutex_init(&per_cpu(cpu_access_lock
, cpu
));
322 static DEFINE_MUTEX(access_lock
);
324 static inline void trace_access_lock(int cpu
)
327 mutex_lock(&access_lock
);
330 static inline void trace_access_unlock(int cpu
)
333 mutex_unlock(&access_lock
);
336 static inline void trace_access_lock_init(void)
342 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
343 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
345 /* trace_flags holds trace_options default values */
346 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
347 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
| TRACE_ITER_SLEEP_TIME
|
348 TRACE_ITER_GRAPH_TIME
| TRACE_ITER_RECORD_CMD
| TRACE_ITER_OVERWRITE
|
349 TRACE_ITER_IRQ_INFO
| TRACE_ITER_MARKERS
;
351 static int trace_stop_count
;
352 static DEFINE_RAW_SPINLOCK(tracing_start_lock
);
355 * trace_wake_up - wake up tasks waiting for trace input
357 * Schedules a delayed work to wake up any task that is blocked on the
358 * trace_wait queue. These is used with trace_poll for tasks polling the
361 static void trace_wake_up(struct irq_work
*work
)
363 wake_up_all(&trace_wait
);
368 * tracing_on - enable tracing buffers
370 * This function enables tracing buffers that may have been
371 * disabled with tracing_off.
373 void tracing_on(void)
375 if (global_trace
.buffer
)
376 ring_buffer_record_on(global_trace
.buffer
);
378 * This flag is only looked at when buffers haven't been
379 * allocated yet. We don't really care about the race
380 * between setting this flag and actually turning
383 global_trace
.buffer_disabled
= 0;
385 EXPORT_SYMBOL_GPL(tracing_on
);
388 * tracing_off - turn off tracing buffers
390 * This function stops the tracing buffers from recording data.
391 * It does not disable any overhead the tracers themselves may
392 * be causing. This function simply causes all recording to
393 * the ring buffers to fail.
395 void tracing_off(void)
397 if (global_trace
.buffer
)
398 ring_buffer_record_off(global_trace
.buffer
);
400 * This flag is only looked at when buffers haven't been
401 * allocated yet. We don't really care about the race
402 * between setting this flag and actually turning
405 global_trace
.buffer_disabled
= 1;
407 EXPORT_SYMBOL_GPL(tracing_off
);
410 * tracing_is_on - show state of ring buffers enabled
412 int tracing_is_on(void)
414 if (global_trace
.buffer
)
415 return ring_buffer_record_is_on(global_trace
.buffer
);
416 return !global_trace
.buffer_disabled
;
418 EXPORT_SYMBOL_GPL(tracing_is_on
);
420 static int __init
set_buf_size(char *str
)
422 unsigned long buf_size
;
426 buf_size
= memparse(str
, &str
);
427 /* nr_entries can not be zero */
430 trace_buf_size
= buf_size
;
433 __setup("trace_buf_size=", set_buf_size
);
435 static int __init
set_tracing_thresh(char *str
)
437 unsigned long threshold
;
442 ret
= kstrtoul(str
, 0, &threshold
);
445 tracing_thresh
= threshold
* 1000;
448 __setup("tracing_thresh=", set_tracing_thresh
);
450 unsigned long nsecs_to_usecs(unsigned long nsecs
)
455 /* These must match the bit postions in trace_iterator_flags */
456 static const char *trace_options
[] = {
488 int in_ns
; /* is this clock in nanoseconds? */
490 { trace_clock_local
, "local", 1 },
491 { trace_clock_global
, "global", 1 },
492 { trace_clock_counter
, "counter", 0 },
499 * trace_parser_get_init - gets the buffer for trace parser
501 int trace_parser_get_init(struct trace_parser
*parser
, int size
)
503 memset(parser
, 0, sizeof(*parser
));
505 parser
->buffer
= kmalloc(size
, GFP_KERNEL
);
514 * trace_parser_put - frees the buffer for trace parser
516 void trace_parser_put(struct trace_parser
*parser
)
518 kfree(parser
->buffer
);
522 * trace_get_user - reads the user input string separated by space
523 * (matched by isspace(ch))
525 * For each string found the 'struct trace_parser' is updated,
526 * and the function returns.
528 * Returns number of bytes read.
530 * See kernel/trace/trace.h for 'struct trace_parser' details.
532 int trace_get_user(struct trace_parser
*parser
, const char __user
*ubuf
,
533 size_t cnt
, loff_t
*ppos
)
540 trace_parser_clear(parser
);
542 ret
= get_user(ch
, ubuf
++);
550 * The parser is not finished with the last write,
551 * continue reading the user input without skipping spaces.
554 /* skip white space */
555 while (cnt
&& isspace(ch
)) {
556 ret
= get_user(ch
, ubuf
++);
563 /* only spaces were written */
573 /* read the non-space input */
574 while (cnt
&& !isspace(ch
)) {
575 if (parser
->idx
< parser
->size
- 1)
576 parser
->buffer
[parser
->idx
++] = ch
;
581 ret
= get_user(ch
, ubuf
++);
588 /* We either got finished input or we have to wait for another call. */
590 parser
->buffer
[parser
->idx
] = 0;
591 parser
->cont
= false;
594 parser
->buffer
[parser
->idx
++] = ch
;
604 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
612 if (s
->len
<= s
->readpos
)
615 len
= s
->len
- s
->readpos
;
618 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
628 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
632 if (s
->len
<= s
->readpos
)
635 len
= s
->len
- s
->readpos
;
638 memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
645 * ftrace_max_lock is used to protect the swapping of buffers
646 * when taking a max snapshot. The buffers themselves are
647 * protected by per_cpu spinlocks. But the action of the swap
648 * needs its own lock.
650 * This is defined as a arch_spinlock_t in order to help
651 * with performance when lockdep debugging is enabled.
653 * It is also used in other places outside the update_max_tr
654 * so it needs to be defined outside of the
655 * CONFIG_TRACER_MAX_TRACE.
657 static arch_spinlock_t ftrace_max_lock
=
658 (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
660 unsigned long __read_mostly tracing_thresh
;
662 #ifdef CONFIG_TRACER_MAX_TRACE
663 unsigned long __read_mostly tracing_max_latency
;
666 * Copy the new maximum trace into the separate maximum-trace
667 * structure. (this way the maximum trace is permanently saved,
668 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
671 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
673 struct trace_array_cpu
*data
= tr
->data
[cpu
];
674 struct trace_array_cpu
*max_data
;
677 max_tr
.time_start
= data
->preempt_timestamp
;
679 max_data
= max_tr
.data
[cpu
];
680 max_data
->saved_latency
= tracing_max_latency
;
681 max_data
->critical_start
= data
->critical_start
;
682 max_data
->critical_end
= data
->critical_end
;
684 memcpy(max_data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
685 max_data
->pid
= tsk
->pid
;
686 max_data
->uid
= task_uid(tsk
);
687 max_data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
688 max_data
->policy
= tsk
->policy
;
689 max_data
->rt_priority
= tsk
->rt_priority
;
691 /* record this tasks comm */
692 tracing_record_cmdline(tsk
);
696 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
698 * @tsk: the task with the latency
699 * @cpu: The cpu that initiated the trace.
701 * Flip the buffers between the @tr and the max_tr and record information
702 * about which task was the cause of this latency.
705 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
707 struct ring_buffer
*buf
= tr
->buffer
;
709 if (trace_stop_count
)
712 WARN_ON_ONCE(!irqs_disabled());
714 if (!current_trace
->allocated_snapshot
) {
715 /* Only the nop tracer should hit this when disabling */
716 WARN_ON_ONCE(current_trace
!= &nop_trace
);
720 arch_spin_lock(&ftrace_max_lock
);
722 tr
->buffer
= max_tr
.buffer
;
725 __update_max_tr(tr
, tsk
, cpu
);
726 arch_spin_unlock(&ftrace_max_lock
);
730 * update_max_tr_single - only copy one trace over, and reset the rest
732 * @tsk - task with the latency
733 * @cpu - the cpu of the buffer to copy.
735 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
738 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
742 if (trace_stop_count
)
745 WARN_ON_ONCE(!irqs_disabled());
746 if (WARN_ON_ONCE(!current_trace
->allocated_snapshot
))
749 arch_spin_lock(&ftrace_max_lock
);
751 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
755 * We failed to swap the buffer due to a commit taking
756 * place on this CPU. We fail to record, but we reset
757 * the max trace buffer (no one writes directly to it)
758 * and flag that it failed.
760 trace_array_printk(&max_tr
, _THIS_IP_
,
761 "Failed to swap buffers due to commit in progress\n");
764 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
&& ret
!= -EBUSY
);
766 __update_max_tr(tr
, tsk
, cpu
);
767 arch_spin_unlock(&ftrace_max_lock
);
769 #endif /* CONFIG_TRACER_MAX_TRACE */
771 static void default_wait_pipe(struct trace_iterator
*iter
)
775 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
778 * The events can happen in critical sections where
779 * checking a work queue can cause deadlocks.
780 * After adding a task to the queue, this flag is set
781 * only to notify events to try to wake up the queue
784 * We don't clear it even if the buffer is no longer
785 * empty. The flag only causes the next event to run
786 * irq_work to do the work queue wake up. The worse
787 * that can happen if we race with !trace_empty() is that
788 * an event will cause an irq_work to try to wake up
791 * There's no reason to protect this flag either, as
792 * the work queue and irq_work logic will do the necessary
793 * synchronization for the wake ups. The only thing
794 * that is necessary is that the wake up happens after
795 * a task has been queued. It's OK for spurious wake ups.
797 trace_wakeup_needed
= true;
799 if (trace_empty(iter
))
802 finish_wait(&trace_wait
, &wait
);
806 * register_tracer - register a tracer with the ftrace system.
807 * @type - the plugin for the tracer
809 * Register a new plugin tracer.
811 int register_tracer(struct tracer
*type
)
817 pr_info("Tracer must have a name\n");
821 if (strlen(type
->name
) >= MAX_TRACER_SIZE
) {
822 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE
);
826 mutex_lock(&trace_types_lock
);
828 tracing_selftest_running
= true;
830 for (t
= trace_types
; t
; t
= t
->next
) {
831 if (strcmp(type
->name
, t
->name
) == 0) {
833 pr_info("Tracer %s already registered\n",
841 type
->set_flag
= &dummy_set_flag
;
843 type
->flags
= &dummy_tracer_flags
;
845 if (!type
->flags
->opts
)
846 type
->flags
->opts
= dummy_tracer_opt
;
847 if (!type
->wait_pipe
)
848 type
->wait_pipe
= default_wait_pipe
;
851 #ifdef CONFIG_FTRACE_STARTUP_TEST
852 if (type
->selftest
&& !tracing_selftest_disabled
) {
853 struct tracer
*saved_tracer
= current_trace
;
854 struct trace_array
*tr
= &global_trace
;
857 * Run a selftest on this tracer.
858 * Here we reset the trace buffer, and set the current
859 * tracer to be this tracer. The tracer can then run some
860 * internal tracing to verify that everything is in order.
861 * If we fail, we do not register this tracer.
863 tracing_reset_online_cpus(tr
);
865 current_trace
= type
;
867 if (type
->use_max_tr
) {
868 /* If we expanded the buffers, make sure the max is expanded too */
869 if (ring_buffer_expanded
)
870 ring_buffer_resize(max_tr
.buffer
, trace_buf_size
,
871 RING_BUFFER_ALL_CPUS
);
872 type
->allocated_snapshot
= true;
875 /* the test is responsible for initializing and enabling */
876 pr_info("Testing tracer %s: ", type
->name
);
877 ret
= type
->selftest(type
, tr
);
878 /* the test is responsible for resetting too */
879 current_trace
= saved_tracer
;
881 printk(KERN_CONT
"FAILED!\n");
882 /* Add the warning after printing 'FAILED' */
886 /* Only reset on passing, to avoid touching corrupted buffers */
887 tracing_reset_online_cpus(tr
);
889 if (type
->use_max_tr
) {
890 type
->allocated_snapshot
= false;
892 /* Shrink the max buffer again */
893 if (ring_buffer_expanded
)
894 ring_buffer_resize(max_tr
.buffer
, 1,
895 RING_BUFFER_ALL_CPUS
);
898 printk(KERN_CONT
"PASSED\n");
902 type
->next
= trace_types
;
906 tracing_selftest_running
= false;
907 mutex_unlock(&trace_types_lock
);
909 if (ret
|| !default_bootup_tracer
)
912 if (strncmp(default_bootup_tracer
, type
->name
, MAX_TRACER_SIZE
))
915 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
916 /* Do we want this tracer to start on bootup? */
917 tracing_set_tracer(type
->name
);
918 default_bootup_tracer
= NULL
;
919 /* disable other selftests, since this will break it. */
920 tracing_selftest_disabled
= 1;
921 #ifdef CONFIG_FTRACE_STARTUP_TEST
922 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
930 void tracing_reset(struct trace_array
*tr
, int cpu
)
932 struct ring_buffer
*buffer
= tr
->buffer
;
937 ring_buffer_record_disable(buffer
);
939 /* Make sure all commits have finished */
941 ring_buffer_reset_cpu(buffer
, cpu
);
943 ring_buffer_record_enable(buffer
);
946 void tracing_reset_online_cpus(struct trace_array
*tr
)
948 struct ring_buffer
*buffer
= tr
->buffer
;
954 ring_buffer_record_disable(buffer
);
956 /* Make sure all commits have finished */
959 tr
->time_start
= ftrace_now(tr
->cpu
);
961 for_each_online_cpu(cpu
)
962 ring_buffer_reset_cpu(buffer
, cpu
);
964 ring_buffer_record_enable(buffer
);
967 void tracing_reset_current(int cpu
)
969 tracing_reset(&global_trace
, cpu
);
972 void tracing_reset_current_online_cpus(void)
974 tracing_reset_online_cpus(&global_trace
);
977 #define SAVED_CMDLINES 128
978 #define NO_CMDLINE_MAP UINT_MAX
979 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
980 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
981 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
982 static int cmdline_idx
;
983 static arch_spinlock_t trace_cmdline_lock
= __ARCH_SPIN_LOCK_UNLOCKED
;
985 /* temporary disable recording */
986 static atomic_t trace_record_cmdline_disabled __read_mostly
;
988 static void trace_init_cmdlines(void)
990 memset(&map_pid_to_cmdline
, NO_CMDLINE_MAP
, sizeof(map_pid_to_cmdline
));
991 memset(&map_cmdline_to_pid
, NO_CMDLINE_MAP
, sizeof(map_cmdline_to_pid
));
995 int is_tracing_stopped(void)
997 return trace_stop_count
;
1001 * ftrace_off_permanent - disable all ftrace code permanently
1003 * This should only be called when a serious anomally has
1004 * been detected. This will turn off the function tracing,
1005 * ring buffers, and other tracing utilites. It takes no
1006 * locks and can be called from any context.
1008 void ftrace_off_permanent(void)
1010 tracing_disabled
= 1;
1012 tracing_off_permanent();
1016 * tracing_start - quick start of the tracer
1018 * If tracing is enabled but was stopped by tracing_stop,
1019 * this will start the tracer back up.
1021 void tracing_start(void)
1023 struct ring_buffer
*buffer
;
1024 unsigned long flags
;
1026 if (tracing_disabled
)
1029 raw_spin_lock_irqsave(&tracing_start_lock
, flags
);
1030 if (--trace_stop_count
) {
1031 if (trace_stop_count
< 0) {
1032 /* Someone screwed up their debugging */
1034 trace_stop_count
= 0;
1039 /* Prevent the buffers from switching */
1040 arch_spin_lock(&ftrace_max_lock
);
1042 buffer
= global_trace
.buffer
;
1044 ring_buffer_record_enable(buffer
);
1046 buffer
= max_tr
.buffer
;
1048 ring_buffer_record_enable(buffer
);
1050 arch_spin_unlock(&ftrace_max_lock
);
1054 raw_spin_unlock_irqrestore(&tracing_start_lock
, flags
);
1058 * tracing_stop - quick stop of the tracer
1060 * Light weight way to stop tracing. Use in conjunction with
1063 void tracing_stop(void)
1065 struct ring_buffer
*buffer
;
1066 unsigned long flags
;
1069 raw_spin_lock_irqsave(&tracing_start_lock
, flags
);
1070 if (trace_stop_count
++)
1073 /* Prevent the buffers from switching */
1074 arch_spin_lock(&ftrace_max_lock
);
1076 buffer
= global_trace
.buffer
;
1078 ring_buffer_record_disable(buffer
);
1080 buffer
= max_tr
.buffer
;
1082 ring_buffer_record_disable(buffer
);
1084 arch_spin_unlock(&ftrace_max_lock
);
1087 raw_spin_unlock_irqrestore(&tracing_start_lock
, flags
);
1090 void trace_stop_cmdline_recording(void);
1092 static void trace_save_cmdline(struct task_struct
*tsk
)
1096 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
1100 * It's not the end of the world if we don't get
1101 * the lock, but we also don't want to spin
1102 * nor do we want to disable interrupts,
1103 * so if we miss here, then better luck next time.
1105 if (!arch_spin_trylock(&trace_cmdline_lock
))
1108 idx
= map_pid_to_cmdline
[tsk
->pid
];
1109 if (idx
== NO_CMDLINE_MAP
) {
1110 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
1113 * Check whether the cmdline buffer at idx has a pid
1114 * mapped. We are going to overwrite that entry so we
1115 * need to clear the map_pid_to_cmdline. Otherwise we
1116 * would read the new comm for the old pid.
1118 pid
= map_cmdline_to_pid
[idx
];
1119 if (pid
!= NO_CMDLINE_MAP
)
1120 map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
1122 map_cmdline_to_pid
[idx
] = tsk
->pid
;
1123 map_pid_to_cmdline
[tsk
->pid
] = idx
;
1128 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
1130 arch_spin_unlock(&trace_cmdline_lock
);
1133 void trace_find_cmdline(int pid
, char comm
[])
1138 strcpy(comm
, "<idle>");
1142 if (WARN_ON_ONCE(pid
< 0)) {
1143 strcpy(comm
, "<XXX>");
1147 if (pid
> PID_MAX_DEFAULT
) {
1148 strcpy(comm
, "<...>");
1153 arch_spin_lock(&trace_cmdline_lock
);
1154 map
= map_pid_to_cmdline
[pid
];
1155 if (map
!= NO_CMDLINE_MAP
)
1156 strcpy(comm
, saved_cmdlines
[map
]);
1158 strcpy(comm
, "<...>");
1160 arch_spin_unlock(&trace_cmdline_lock
);
1164 void tracing_record_cmdline(struct task_struct
*tsk
)
1166 if (atomic_read(&trace_record_cmdline_disabled
) || !tracing_is_on())
1169 if (!__this_cpu_read(trace_cmdline_save
))
1172 __this_cpu_write(trace_cmdline_save
, false);
1174 trace_save_cmdline(tsk
);
1178 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
1181 struct task_struct
*tsk
= current
;
1183 entry
->preempt_count
= pc
& 0xff;
1184 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
1186 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1187 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
1189 TRACE_FLAG_IRQS_NOSUPPORT
|
1191 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
1192 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
1193 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
1195 EXPORT_SYMBOL_GPL(tracing_generic_entry_update
);
1197 struct ring_buffer_event
*
1198 trace_buffer_lock_reserve(struct ring_buffer
*buffer
,
1201 unsigned long flags
, int pc
)
1203 struct ring_buffer_event
*event
;
1205 event
= ring_buffer_lock_reserve(buffer
, len
);
1206 if (event
!= NULL
) {
1207 struct trace_entry
*ent
= ring_buffer_event_data(event
);
1209 tracing_generic_entry_update(ent
, flags
, pc
);
1217 __buffer_unlock_commit(struct ring_buffer
*buffer
, struct ring_buffer_event
*event
)
1219 __this_cpu_write(trace_cmdline_save
, true);
1220 if (trace_wakeup_needed
) {
1221 trace_wakeup_needed
= false;
1222 /* irq_work_queue() supplies it's own memory barriers */
1223 irq_work_queue(&trace_work_wakeup
);
1225 ring_buffer_unlock_commit(buffer
, event
);
1229 __trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1230 struct ring_buffer_event
*event
,
1231 unsigned long flags
, int pc
)
1233 __buffer_unlock_commit(buffer
, event
);
1235 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1236 ftrace_trace_userstack(buffer
, flags
, pc
);
1239 void trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1240 struct ring_buffer_event
*event
,
1241 unsigned long flags
, int pc
)
1243 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
1245 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit
);
1247 struct ring_buffer_event
*
1248 trace_current_buffer_lock_reserve(struct ring_buffer
**current_rb
,
1249 int type
, unsigned long len
,
1250 unsigned long flags
, int pc
)
1252 *current_rb
= global_trace
.buffer
;
1253 return trace_buffer_lock_reserve(*current_rb
,
1254 type
, len
, flags
, pc
);
1256 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve
);
1258 void trace_current_buffer_unlock_commit(struct ring_buffer
*buffer
,
1259 struct ring_buffer_event
*event
,
1260 unsigned long flags
, int pc
)
1262 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
1264 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit
);
1266 void trace_buffer_unlock_commit_regs(struct ring_buffer
*buffer
,
1267 struct ring_buffer_event
*event
,
1268 unsigned long flags
, int pc
,
1269 struct pt_regs
*regs
)
1271 __buffer_unlock_commit(buffer
, event
);
1273 ftrace_trace_stack_regs(buffer
, flags
, 0, pc
, regs
);
1274 ftrace_trace_userstack(buffer
, flags
, pc
);
1276 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs
);
1278 void trace_current_buffer_discard_commit(struct ring_buffer
*buffer
,
1279 struct ring_buffer_event
*event
)
1281 ring_buffer_discard_commit(buffer
, event
);
1283 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
1286 trace_function(struct trace_array
*tr
,
1287 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1290 struct ftrace_event_call
*call
= &event_function
;
1291 struct ring_buffer
*buffer
= tr
->buffer
;
1292 struct ring_buffer_event
*event
;
1293 struct ftrace_entry
*entry
;
1295 /* If we are reading the ring buffer, don't trace */
1296 if (unlikely(__this_cpu_read(ftrace_cpu_disabled
)))
1299 event
= trace_buffer_lock_reserve(buffer
, TRACE_FN
, sizeof(*entry
),
1303 entry
= ring_buffer_event_data(event
);
1305 entry
->parent_ip
= parent_ip
;
1307 if (!filter_check_discard(call
, entry
, buffer
, event
))
1308 __buffer_unlock_commit(buffer
, event
);
1312 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
1313 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1316 if (likely(!atomic_read(&data
->disabled
)))
1317 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
1320 #ifdef CONFIG_STACKTRACE
1322 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1323 struct ftrace_stack
{
1324 unsigned long calls
[FTRACE_STACK_MAX_ENTRIES
];
1327 static DEFINE_PER_CPU(struct ftrace_stack
, ftrace_stack
);
1328 static DEFINE_PER_CPU(int, ftrace_stack_reserve
);
1330 static void __ftrace_trace_stack(struct ring_buffer
*buffer
,
1331 unsigned long flags
,
1332 int skip
, int pc
, struct pt_regs
*regs
)
1334 struct ftrace_event_call
*call
= &event_kernel_stack
;
1335 struct ring_buffer_event
*event
;
1336 struct stack_entry
*entry
;
1337 struct stack_trace trace
;
1339 int size
= FTRACE_STACK_ENTRIES
;
1341 trace
.nr_entries
= 0;
1345 * Since events can happen in NMIs there's no safe way to
1346 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1347 * or NMI comes in, it will just have to use the default
1348 * FTRACE_STACK_SIZE.
1350 preempt_disable_notrace();
1352 use_stack
= __this_cpu_inc_return(ftrace_stack_reserve
);
1354 * We don't need any atomic variables, just a barrier.
1355 * If an interrupt comes in, we don't care, because it would
1356 * have exited and put the counter back to what we want.
1357 * We just need a barrier to keep gcc from moving things
1361 if (use_stack
== 1) {
1362 trace
.entries
= &__get_cpu_var(ftrace_stack
).calls
[0];
1363 trace
.max_entries
= FTRACE_STACK_MAX_ENTRIES
;
1366 save_stack_trace_regs(regs
, &trace
);
1368 save_stack_trace(&trace
);
1370 if (trace
.nr_entries
> size
)
1371 size
= trace
.nr_entries
;
1373 /* From now on, use_stack is a boolean */
1376 size
*= sizeof(unsigned long);
1378 event
= trace_buffer_lock_reserve(buffer
, TRACE_STACK
,
1379 sizeof(*entry
) + size
, flags
, pc
);
1382 entry
= ring_buffer_event_data(event
);
1384 memset(&entry
->caller
, 0, size
);
1387 memcpy(&entry
->caller
, trace
.entries
,
1388 trace
.nr_entries
* sizeof(unsigned long));
1390 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1391 trace
.entries
= entry
->caller
;
1393 save_stack_trace_regs(regs
, &trace
);
1395 save_stack_trace(&trace
);
1398 entry
->size
= trace
.nr_entries
;
1400 if (!filter_check_discard(call
, entry
, buffer
, event
))
1401 __buffer_unlock_commit(buffer
, event
);
1404 /* Again, don't let gcc optimize things here */
1406 __this_cpu_dec(ftrace_stack_reserve
);
1407 preempt_enable_notrace();
1411 void ftrace_trace_stack_regs(struct ring_buffer
*buffer
, unsigned long flags
,
1412 int skip
, int pc
, struct pt_regs
*regs
)
1414 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1417 __ftrace_trace_stack(buffer
, flags
, skip
, pc
, regs
);
1420 void ftrace_trace_stack(struct ring_buffer
*buffer
, unsigned long flags
,
1423 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1426 __ftrace_trace_stack(buffer
, flags
, skip
, pc
, NULL
);
1429 void __trace_stack(struct trace_array
*tr
, unsigned long flags
, int skip
,
1432 __ftrace_trace_stack(tr
->buffer
, flags
, skip
, pc
, NULL
);
1436 * trace_dump_stack - record a stack back trace in the trace buffer
1438 void trace_dump_stack(void)
1440 unsigned long flags
;
1442 if (tracing_disabled
|| tracing_selftest_running
)
1445 local_save_flags(flags
);
1447 /* skipping 3 traces, seems to get us at the caller of this function */
1448 __ftrace_trace_stack(global_trace
.buffer
, flags
, 3, preempt_count(), NULL
);
1451 static DEFINE_PER_CPU(int, user_stack_count
);
1454 ftrace_trace_userstack(struct ring_buffer
*buffer
, unsigned long flags
, int pc
)
1456 struct ftrace_event_call
*call
= &event_user_stack
;
1457 struct ring_buffer_event
*event
;
1458 struct userstack_entry
*entry
;
1459 struct stack_trace trace
;
1461 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1465 * NMIs can not handle page faults, even with fix ups.
1466 * The save user stack can (and often does) fault.
1468 if (unlikely(in_nmi()))
1472 * prevent recursion, since the user stack tracing may
1473 * trigger other kernel events.
1476 if (__this_cpu_read(user_stack_count
))
1479 __this_cpu_inc(user_stack_count
);
1481 event
= trace_buffer_lock_reserve(buffer
, TRACE_USER_STACK
,
1482 sizeof(*entry
), flags
, pc
);
1484 goto out_drop_count
;
1485 entry
= ring_buffer_event_data(event
);
1487 entry
->tgid
= current
->tgid
;
1488 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1490 trace
.nr_entries
= 0;
1491 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1493 trace
.entries
= entry
->caller
;
1495 save_stack_trace_user(&trace
);
1496 if (!filter_check_discard(call
, entry
, buffer
, event
))
1497 __buffer_unlock_commit(buffer
, event
);
1500 __this_cpu_dec(user_stack_count
);
1506 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1508 ftrace_trace_userstack(tr
, flags
, preempt_count());
1512 #endif /* CONFIG_STACKTRACE */
1514 /* created for use with alloc_percpu */
1515 struct trace_buffer_struct
{
1516 char buffer
[TRACE_BUF_SIZE
];
1519 static struct trace_buffer_struct
*trace_percpu_buffer
;
1520 static struct trace_buffer_struct
*trace_percpu_sirq_buffer
;
1521 static struct trace_buffer_struct
*trace_percpu_irq_buffer
;
1522 static struct trace_buffer_struct
*trace_percpu_nmi_buffer
;
1525 * The buffer used is dependent on the context. There is a per cpu
1526 * buffer for normal context, softirq contex, hard irq context and
1527 * for NMI context. Thise allows for lockless recording.
1529 * Note, if the buffers failed to be allocated, then this returns NULL
1531 static char *get_trace_buf(void)
1533 struct trace_buffer_struct
*percpu_buffer
;
1536 * If we have allocated per cpu buffers, then we do not
1537 * need to do any locking.
1540 percpu_buffer
= trace_percpu_nmi_buffer
;
1542 percpu_buffer
= trace_percpu_irq_buffer
;
1543 else if (in_softirq())
1544 percpu_buffer
= trace_percpu_sirq_buffer
;
1546 percpu_buffer
= trace_percpu_buffer
;
1551 return this_cpu_ptr(&percpu_buffer
->buffer
[0]);
1554 static int alloc_percpu_trace_buffer(void)
1556 struct trace_buffer_struct
*buffers
;
1557 struct trace_buffer_struct
*sirq_buffers
;
1558 struct trace_buffer_struct
*irq_buffers
;
1559 struct trace_buffer_struct
*nmi_buffers
;
1561 buffers
= alloc_percpu(struct trace_buffer_struct
);
1565 sirq_buffers
= alloc_percpu(struct trace_buffer_struct
);
1569 irq_buffers
= alloc_percpu(struct trace_buffer_struct
);
1573 nmi_buffers
= alloc_percpu(struct trace_buffer_struct
);
1577 trace_percpu_buffer
= buffers
;
1578 trace_percpu_sirq_buffer
= sirq_buffers
;
1579 trace_percpu_irq_buffer
= irq_buffers
;
1580 trace_percpu_nmi_buffer
= nmi_buffers
;
1585 free_percpu(irq_buffers
);
1587 free_percpu(sirq_buffers
);
1589 free_percpu(buffers
);
1591 WARN(1, "Could not allocate percpu trace_printk buffer");
1595 static int buffers_allocated
;
1597 void trace_printk_init_buffers(void)
1599 if (buffers_allocated
)
1602 if (alloc_percpu_trace_buffer())
1605 pr_info("ftrace: Allocated trace_printk buffers\n");
1607 /* Expand the buffers to set size */
1608 tracing_update_buffers();
1610 buffers_allocated
= 1;
1613 * trace_printk_init_buffers() can be called by modules.
1614 * If that happens, then we need to start cmdline recording
1615 * directly here. If the global_trace.buffer is already
1616 * allocated here, then this was called by module code.
1618 if (global_trace
.buffer
)
1619 tracing_start_cmdline_record();
1622 void trace_printk_start_comm(void)
1624 /* Start tracing comms if trace printk is set */
1625 if (!buffers_allocated
)
1627 tracing_start_cmdline_record();
1630 static void trace_printk_start_stop_comm(int enabled
)
1632 if (!buffers_allocated
)
1636 tracing_start_cmdline_record();
1638 tracing_stop_cmdline_record();
1642 * trace_vbprintk - write binary msg to tracing buffer
1645 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1647 struct ftrace_event_call
*call
= &event_bprint
;
1648 struct ring_buffer_event
*event
;
1649 struct ring_buffer
*buffer
;
1650 struct trace_array
*tr
= &global_trace
;
1651 struct bprint_entry
*entry
;
1652 unsigned long flags
;
1654 int len
= 0, size
, pc
;
1656 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1659 /* Don't pollute graph traces with trace_vprintk internals */
1660 pause_graph_tracing();
1662 pc
= preempt_count();
1663 preempt_disable_notrace();
1665 tbuffer
= get_trace_buf();
1671 len
= vbin_printf((u32
*)tbuffer
, TRACE_BUF_SIZE
/sizeof(int), fmt
, args
);
1673 if (len
> TRACE_BUF_SIZE
/sizeof(int) || len
< 0)
1676 local_save_flags(flags
);
1677 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1678 buffer
= tr
->buffer
;
1679 event
= trace_buffer_lock_reserve(buffer
, TRACE_BPRINT
, size
,
1683 entry
= ring_buffer_event_data(event
);
1687 memcpy(entry
->buf
, tbuffer
, sizeof(u32
) * len
);
1688 if (!filter_check_discard(call
, entry
, buffer
, event
)) {
1689 __buffer_unlock_commit(buffer
, event
);
1690 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1694 preempt_enable_notrace();
1695 unpause_graph_tracing();
1699 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1701 int trace_array_printk(struct trace_array
*tr
,
1702 unsigned long ip
, const char *fmt
, ...)
1707 if (!(trace_flags
& TRACE_ITER_PRINTK
))
1711 ret
= trace_array_vprintk(tr
, ip
, fmt
, ap
);
1716 int trace_array_vprintk(struct trace_array
*tr
,
1717 unsigned long ip
, const char *fmt
, va_list args
)
1719 struct ftrace_event_call
*call
= &event_print
;
1720 struct ring_buffer_event
*event
;
1721 struct ring_buffer
*buffer
;
1722 int len
= 0, size
, pc
;
1723 struct print_entry
*entry
;
1724 unsigned long flags
;
1727 if (tracing_disabled
|| tracing_selftest_running
)
1730 /* Don't pollute graph traces with trace_vprintk internals */
1731 pause_graph_tracing();
1733 pc
= preempt_count();
1734 preempt_disable_notrace();
1737 tbuffer
= get_trace_buf();
1743 len
= vsnprintf(tbuffer
, TRACE_BUF_SIZE
, fmt
, args
);
1744 if (len
> TRACE_BUF_SIZE
)
1747 local_save_flags(flags
);
1748 size
= sizeof(*entry
) + len
+ 1;
1749 buffer
= tr
->buffer
;
1750 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
1754 entry
= ring_buffer_event_data(event
);
1757 memcpy(&entry
->buf
, tbuffer
, len
);
1758 entry
->buf
[len
] = '\0';
1759 if (!filter_check_discard(call
, entry
, buffer
, event
)) {
1760 __buffer_unlock_commit(buffer
, event
);
1761 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1764 preempt_enable_notrace();
1765 unpause_graph_tracing();
1770 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1772 return trace_array_vprintk(&global_trace
, ip
, fmt
, args
);
1774 EXPORT_SYMBOL_GPL(trace_vprintk
);
1776 static void trace_iterator_increment(struct trace_iterator
*iter
)
1778 struct ring_buffer_iter
*buf_iter
= trace_buffer_iter(iter
, iter
->cpu
);
1782 ring_buffer_read(buf_iter
, NULL
);
1785 static struct trace_entry
*
1786 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
,
1787 unsigned long *lost_events
)
1789 struct ring_buffer_event
*event
;
1790 struct ring_buffer_iter
*buf_iter
= trace_buffer_iter(iter
, cpu
);
1793 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1795 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
,
1799 iter
->ent_size
= ring_buffer_event_length(event
);
1800 return ring_buffer_event_data(event
);
1806 static struct trace_entry
*
1807 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
,
1808 unsigned long *missing_events
, u64
*ent_ts
)
1810 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1811 struct trace_entry
*ent
, *next
= NULL
;
1812 unsigned long lost_events
= 0, next_lost
= 0;
1813 int cpu_file
= iter
->cpu_file
;
1814 u64 next_ts
= 0, ts
;
1820 * If we are in a per_cpu trace file, don't bother by iterating over
1821 * all cpu and peek directly.
1823 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1824 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1826 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
, missing_events
);
1828 *ent_cpu
= cpu_file
;
1833 for_each_tracing_cpu(cpu
) {
1835 if (ring_buffer_empty_cpu(buffer
, cpu
))
1838 ent
= peek_next_entry(iter
, cpu
, &ts
, &lost_events
);
1841 * Pick the entry with the smallest timestamp:
1843 if (ent
&& (!next
|| ts
< next_ts
)) {
1847 next_lost
= lost_events
;
1848 next_size
= iter
->ent_size
;
1852 iter
->ent_size
= next_size
;
1855 *ent_cpu
= next_cpu
;
1861 *missing_events
= next_lost
;
1866 /* Find the next real entry, without updating the iterator itself */
1867 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1868 int *ent_cpu
, u64
*ent_ts
)
1870 return __find_next_entry(iter
, ent_cpu
, NULL
, ent_ts
);
1873 /* Find the next real entry, and increment the iterator to the next entry */
1874 void *trace_find_next_entry_inc(struct trace_iterator
*iter
)
1876 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
,
1877 &iter
->lost_events
, &iter
->ts
);
1880 trace_iterator_increment(iter
);
1882 return iter
->ent
? iter
: NULL
;
1885 static void trace_consume(struct trace_iterator
*iter
)
1887 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
,
1888 &iter
->lost_events
);
1891 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1893 struct trace_iterator
*iter
= m
->private;
1897 WARN_ON_ONCE(iter
->leftover
);
1901 /* can't go backwards */
1906 ent
= trace_find_next_entry_inc(iter
);
1910 while (ent
&& iter
->idx
< i
)
1911 ent
= trace_find_next_entry_inc(iter
);
1918 void tracing_iter_reset(struct trace_iterator
*iter
, int cpu
)
1920 struct trace_array
*tr
= iter
->tr
;
1921 struct ring_buffer_event
*event
;
1922 struct ring_buffer_iter
*buf_iter
;
1923 unsigned long entries
= 0;
1926 tr
->data
[cpu
]->skipped_entries
= 0;
1928 buf_iter
= trace_buffer_iter(iter
, cpu
);
1932 ring_buffer_iter_reset(buf_iter
);
1935 * We could have the case with the max latency tracers
1936 * that a reset never took place on a cpu. This is evident
1937 * by the timestamp being before the start of the buffer.
1939 while ((event
= ring_buffer_iter_peek(buf_iter
, &ts
))) {
1940 if (ts
>= iter
->tr
->time_start
)
1943 ring_buffer_read(buf_iter
, NULL
);
1946 tr
->data
[cpu
]->skipped_entries
= entries
;
1950 * The current tracer is copied to avoid a global locking
1953 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1955 struct trace_iterator
*iter
= m
->private;
1956 int cpu_file
= iter
->cpu_file
;
1962 * copy the tracer to avoid using a global lock all around.
1963 * iter->trace is a copy of current_trace, the pointer to the
1964 * name may be used instead of a strcmp(), as iter->trace->name
1965 * will point to the same string as current_trace->name.
1967 mutex_lock(&trace_types_lock
);
1968 if (unlikely(current_trace
&& iter
->trace
->name
!= current_trace
->name
))
1969 *iter
->trace
= *current_trace
;
1970 mutex_unlock(&trace_types_lock
);
1972 if (iter
->snapshot
&& iter
->trace
->use_max_tr
)
1973 return ERR_PTR(-EBUSY
);
1975 if (!iter
->snapshot
)
1976 atomic_inc(&trace_record_cmdline_disabled
);
1978 if (*pos
!= iter
->pos
) {
1983 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1984 for_each_tracing_cpu(cpu
)
1985 tracing_iter_reset(iter
, cpu
);
1987 tracing_iter_reset(iter
, cpu_file
);
1990 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1995 * If we overflowed the seq_file before, then we want
1996 * to just reuse the trace_seq buffer again.
2002 p
= s_next(m
, p
, &l
);
2006 trace_event_read_lock();
2007 trace_access_lock(cpu_file
);
2011 static void s_stop(struct seq_file
*m
, void *p
)
2013 struct trace_iterator
*iter
= m
->private;
2015 if (iter
->snapshot
&& iter
->trace
->use_max_tr
)
2018 if (!iter
->snapshot
)
2019 atomic_dec(&trace_record_cmdline_disabled
);
2020 trace_access_unlock(iter
->cpu_file
);
2021 trace_event_read_unlock();
2025 get_total_entries(struct trace_array
*tr
, unsigned long *total
, unsigned long *entries
)
2027 unsigned long count
;
2033 for_each_tracing_cpu(cpu
) {
2034 count
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
2036 * If this buffer has skipped entries, then we hold all
2037 * entries for the trace and we need to ignore the
2038 * ones before the time stamp.
2040 if (tr
->data
[cpu
]->skipped_entries
) {
2041 count
-= tr
->data
[cpu
]->skipped_entries
;
2042 /* total is the same as the entries */
2046 ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
2051 static void print_lat_help_header(struct seq_file
*m
)
2053 seq_puts(m
, "# _------=> CPU# \n");
2054 seq_puts(m
, "# / _-----=> irqs-off \n");
2055 seq_puts(m
, "# | / _----=> need-resched \n");
2056 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
2057 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
2058 seq_puts(m
, "# |||| / delay \n");
2059 seq_puts(m
, "# cmd pid ||||| time | caller \n");
2060 seq_puts(m
, "# \\ / ||||| \\ | / \n");
2063 static void print_event_info(struct trace_array
*tr
, struct seq_file
*m
)
2065 unsigned long total
;
2066 unsigned long entries
;
2068 get_total_entries(tr
, &total
, &entries
);
2069 seq_printf(m
, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2070 entries
, total
, num_online_cpus());
2074 static void print_func_help_header(struct trace_array
*tr
, struct seq_file
*m
)
2076 print_event_info(tr
, m
);
2077 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2078 seq_puts(m
, "# | | | | |\n");
2081 static void print_func_help_header_irq(struct trace_array
*tr
, struct seq_file
*m
)
2083 print_event_info(tr
, m
);
2084 seq_puts(m
, "# _-----=> irqs-off\n");
2085 seq_puts(m
, "# / _----=> need-resched\n");
2086 seq_puts(m
, "# | / _---=> hardirq/softirq\n");
2087 seq_puts(m
, "# || / _--=> preempt-depth\n");
2088 seq_puts(m
, "# ||| / delay\n");
2089 seq_puts(m
, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2090 seq_puts(m
, "# | | | |||| | |\n");
2094 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
2096 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
2097 struct trace_array
*tr
= iter
->tr
;
2098 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
2099 struct tracer
*type
= current_trace
;
2100 unsigned long entries
;
2101 unsigned long total
;
2102 const char *name
= "preemption";
2106 get_total_entries(tr
, &total
, &entries
);
2108 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
2110 seq_puts(m
, "# -----------------------------------"
2111 "---------------------------------\n");
2112 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2113 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2114 nsecs_to_usecs(data
->saved_latency
),
2118 #if defined(CONFIG_PREEMPT_NONE)
2120 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2122 #elif defined(CONFIG_PREEMPT)
2127 /* These are reserved for later use */
2130 seq_printf(m
, " #P:%d)\n", num_online_cpus());
2134 seq_puts(m
, "# -----------------\n");
2135 seq_printf(m
, "# | task: %.16s-%d "
2136 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2137 data
->comm
, data
->pid
,
2138 from_kuid_munged(seq_user_ns(m
), data
->uid
), data
->nice
,
2139 data
->policy
, data
->rt_priority
);
2140 seq_puts(m
, "# -----------------\n");
2142 if (data
->critical_start
) {
2143 seq_puts(m
, "# => started at: ");
2144 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
2145 trace_print_seq(m
, &iter
->seq
);
2146 seq_puts(m
, "\n# => ended at: ");
2147 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
2148 trace_print_seq(m
, &iter
->seq
);
2149 seq_puts(m
, "\n#\n");
2155 static void test_cpu_buff_start(struct trace_iterator
*iter
)
2157 struct trace_seq
*s
= &iter
->seq
;
2159 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
2162 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
2165 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
2168 if (iter
->tr
->data
[iter
->cpu
]->skipped_entries
)
2171 cpumask_set_cpu(iter
->cpu
, iter
->started
);
2173 /* Don't print started cpu buffer for the first entry of the trace */
2175 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
2179 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
2181 struct trace_seq
*s
= &iter
->seq
;
2182 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
2183 struct trace_entry
*entry
;
2184 struct trace_event
*event
;
2188 test_cpu_buff_start(iter
);
2190 event
= ftrace_find_event(entry
->type
);
2192 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2193 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
2194 if (!trace_print_lat_context(iter
))
2197 if (!trace_print_context(iter
))
2203 return event
->funcs
->trace(iter
, sym_flags
, event
);
2205 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
2208 return TRACE_TYPE_HANDLED
;
2210 return TRACE_TYPE_PARTIAL_LINE
;
2213 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
2215 struct trace_seq
*s
= &iter
->seq
;
2216 struct trace_entry
*entry
;
2217 struct trace_event
*event
;
2221 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2222 if (!trace_seq_printf(s
, "%d %d %llu ",
2223 entry
->pid
, iter
->cpu
, iter
->ts
))
2227 event
= ftrace_find_event(entry
->type
);
2229 return event
->funcs
->raw(iter
, 0, event
);
2231 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
2234 return TRACE_TYPE_HANDLED
;
2236 return TRACE_TYPE_PARTIAL_LINE
;
2239 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
2241 struct trace_seq
*s
= &iter
->seq
;
2242 unsigned char newline
= '\n';
2243 struct trace_entry
*entry
;
2244 struct trace_event
*event
;
2248 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2249 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
2250 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
2251 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
2254 event
= ftrace_find_event(entry
->type
);
2256 enum print_line_t ret
= event
->funcs
->hex(iter
, 0, event
);
2257 if (ret
!= TRACE_TYPE_HANDLED
)
2261 SEQ_PUT_FIELD_RET(s
, newline
);
2263 return TRACE_TYPE_HANDLED
;
2266 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
2268 struct trace_seq
*s
= &iter
->seq
;
2269 struct trace_entry
*entry
;
2270 struct trace_event
*event
;
2274 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2275 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
2276 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
2277 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
2280 event
= ftrace_find_event(entry
->type
);
2281 return event
? event
->funcs
->binary(iter
, 0, event
) :
2285 int trace_empty(struct trace_iterator
*iter
)
2287 struct ring_buffer_iter
*buf_iter
;
2290 /* If we are looking at one CPU buffer, only check that one */
2291 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
2292 cpu
= iter
->cpu_file
;
2293 buf_iter
= trace_buffer_iter(iter
, cpu
);
2295 if (!ring_buffer_iter_empty(buf_iter
))
2298 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
2304 for_each_tracing_cpu(cpu
) {
2305 buf_iter
= trace_buffer_iter(iter
, cpu
);
2307 if (!ring_buffer_iter_empty(buf_iter
))
2310 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
2318 /* Called with trace_event_read_lock() held. */
2319 enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
2321 enum print_line_t ret
;
2323 if (iter
->lost_events
&&
2324 !trace_seq_printf(&iter
->seq
, "CPU:%d [LOST %lu EVENTS]\n",
2325 iter
->cpu
, iter
->lost_events
))
2326 return TRACE_TYPE_PARTIAL_LINE
;
2328 if (iter
->trace
&& iter
->trace
->print_line
) {
2329 ret
= iter
->trace
->print_line(iter
);
2330 if (ret
!= TRACE_TYPE_UNHANDLED
)
2334 if (iter
->ent
->type
== TRACE_BPRINT
&&
2335 trace_flags
& TRACE_ITER_PRINTK
&&
2336 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2337 return trace_print_bprintk_msg_only(iter
);
2339 if (iter
->ent
->type
== TRACE_PRINT
&&
2340 trace_flags
& TRACE_ITER_PRINTK
&&
2341 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2342 return trace_print_printk_msg_only(iter
);
2344 if (trace_flags
& TRACE_ITER_BIN
)
2345 return print_bin_fmt(iter
);
2347 if (trace_flags
& TRACE_ITER_HEX
)
2348 return print_hex_fmt(iter
);
2350 if (trace_flags
& TRACE_ITER_RAW
)
2351 return print_raw_fmt(iter
);
2353 return print_trace_fmt(iter
);
2356 void trace_latency_header(struct seq_file
*m
)
2358 struct trace_iterator
*iter
= m
->private;
2360 /* print nothing if the buffers are empty */
2361 if (trace_empty(iter
))
2364 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
2365 print_trace_header(m
, iter
);
2367 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2368 print_lat_help_header(m
);
2371 void trace_default_header(struct seq_file
*m
)
2373 struct trace_iterator
*iter
= m
->private;
2375 if (!(trace_flags
& TRACE_ITER_CONTEXT_INFO
))
2378 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
2379 /* print nothing if the buffers are empty */
2380 if (trace_empty(iter
))
2382 print_trace_header(m
, iter
);
2383 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2384 print_lat_help_header(m
);
2386 if (!(trace_flags
& TRACE_ITER_VERBOSE
)) {
2387 if (trace_flags
& TRACE_ITER_IRQ_INFO
)
2388 print_func_help_header_irq(iter
->tr
, m
);
2390 print_func_help_header(iter
->tr
, m
);
2395 static void test_ftrace_alive(struct seq_file
*m
)
2397 if (!ftrace_is_dead())
2399 seq_printf(m
, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2400 seq_printf(m
, "# MAY BE MISSING FUNCTION EVENTS\n");
2403 static int s_show(struct seq_file
*m
, void *v
)
2405 struct trace_iterator
*iter
= v
;
2408 if (iter
->ent
== NULL
) {
2410 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
2412 test_ftrace_alive(m
);
2414 if (iter
->trace
&& iter
->trace
->print_header
)
2415 iter
->trace
->print_header(m
);
2417 trace_default_header(m
);
2419 } else if (iter
->leftover
) {
2421 * If we filled the seq_file buffer earlier, we
2422 * want to just show it now.
2424 ret
= trace_print_seq(m
, &iter
->seq
);
2426 /* ret should this time be zero, but you never know */
2427 iter
->leftover
= ret
;
2430 print_trace_line(iter
);
2431 ret
= trace_print_seq(m
, &iter
->seq
);
2433 * If we overflow the seq_file buffer, then it will
2434 * ask us for this data again at start up.
2436 * ret is 0 if seq_file write succeeded.
2439 iter
->leftover
= ret
;
2445 static const struct seq_operations tracer_seq_ops
= {
2452 static struct trace_iterator
*
2453 __tracing_open(struct inode
*inode
, struct file
*file
, bool snapshot
)
2455 long cpu_file
= (long) inode
->i_private
;
2456 struct trace_iterator
*iter
;
2459 if (tracing_disabled
)
2460 return ERR_PTR(-ENODEV
);
2462 iter
= __seq_open_private(file
, &tracer_seq_ops
, sizeof(*iter
));
2464 return ERR_PTR(-ENOMEM
);
2466 iter
->buffer_iter
= kzalloc(sizeof(*iter
->buffer_iter
) * num_possible_cpus(),
2468 if (!iter
->buffer_iter
)
2472 * We make a copy of the current tracer to avoid concurrent
2473 * changes on it while we are reading.
2475 mutex_lock(&trace_types_lock
);
2476 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2480 *iter
->trace
= *current_trace
;
2482 if (!zalloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
2485 if (current_trace
->print_max
|| snapshot
)
2488 iter
->tr
= &global_trace
;
2489 iter
->snapshot
= snapshot
;
2491 mutex_init(&iter
->mutex
);
2492 iter
->cpu_file
= cpu_file
;
2494 /* Notify the tracer early; before we stop tracing. */
2495 if (iter
->trace
&& iter
->trace
->open
)
2496 iter
->trace
->open(iter
);
2498 /* Annotate start of buffers if we had overruns */
2499 if (ring_buffer_overruns(iter
->tr
->buffer
))
2500 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
2502 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2503 if (trace_clocks
[trace_clock_id
].in_ns
)
2504 iter
->iter_flags
|= TRACE_FILE_TIME_IN_NS
;
2506 /* stop the trace while dumping if we are not opening "snapshot" */
2507 if (!iter
->snapshot
)
2510 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
2511 for_each_tracing_cpu(cpu
) {
2512 iter
->buffer_iter
[cpu
] =
2513 ring_buffer_read_prepare(iter
->tr
->buffer
, cpu
);
2515 ring_buffer_read_prepare_sync();
2516 for_each_tracing_cpu(cpu
) {
2517 ring_buffer_read_start(iter
->buffer_iter
[cpu
]);
2518 tracing_iter_reset(iter
, cpu
);
2521 cpu
= iter
->cpu_file
;
2522 iter
->buffer_iter
[cpu
] =
2523 ring_buffer_read_prepare(iter
->tr
->buffer
, cpu
);
2524 ring_buffer_read_prepare_sync();
2525 ring_buffer_read_start(iter
->buffer_iter
[cpu
]);
2526 tracing_iter_reset(iter
, cpu
);
2529 mutex_unlock(&trace_types_lock
);
2534 mutex_unlock(&trace_types_lock
);
2536 kfree(iter
->buffer_iter
);
2538 seq_release_private(inode
, file
);
2539 return ERR_PTR(-ENOMEM
);
2542 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
2544 if (tracing_disabled
)
2547 filp
->private_data
= inode
->i_private
;
2551 static int tracing_release(struct inode
*inode
, struct file
*file
)
2553 struct seq_file
*m
= file
->private_data
;
2554 struct trace_iterator
*iter
;
2557 if (!(file
->f_mode
& FMODE_READ
))
2562 mutex_lock(&trace_types_lock
);
2563 for_each_tracing_cpu(cpu
) {
2564 if (iter
->buffer_iter
[cpu
])
2565 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2568 if (iter
->trace
&& iter
->trace
->close
)
2569 iter
->trace
->close(iter
);
2571 if (!iter
->snapshot
)
2572 /* reenable tracing if it was previously enabled */
2574 mutex_unlock(&trace_types_lock
);
2576 mutex_destroy(&iter
->mutex
);
2577 free_cpumask_var(iter
->started
);
2579 kfree(iter
->buffer_iter
);
2580 seq_release_private(inode
, file
);
2584 static int tracing_open(struct inode
*inode
, struct file
*file
)
2586 struct trace_iterator
*iter
;
2589 /* If this file was open for write, then erase contents */
2590 if ((file
->f_mode
& FMODE_WRITE
) &&
2591 (file
->f_flags
& O_TRUNC
)) {
2592 long cpu
= (long) inode
->i_private
;
2594 if (cpu
== TRACE_PIPE_ALL_CPU
)
2595 tracing_reset_online_cpus(&global_trace
);
2597 tracing_reset(&global_trace
, cpu
);
2600 if (file
->f_mode
& FMODE_READ
) {
2601 iter
= __tracing_open(inode
, file
, false);
2603 ret
= PTR_ERR(iter
);
2604 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2605 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2611 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2613 struct tracer
*t
= v
;
2623 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2628 mutex_lock(&trace_types_lock
);
2629 for (t
= trace_types
; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2635 static void t_stop(struct seq_file
*m
, void *p
)
2637 mutex_unlock(&trace_types_lock
);
2640 static int t_show(struct seq_file
*m
, void *v
)
2642 struct tracer
*t
= v
;
2647 seq_printf(m
, "%s", t
->name
);
2656 static const struct seq_operations show_traces_seq_ops
= {
2663 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2665 if (tracing_disabled
)
2668 return seq_open(file
, &show_traces_seq_ops
);
2672 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
2673 size_t count
, loff_t
*ppos
)
2678 static loff_t
tracing_seek(struct file
*file
, loff_t offset
, int origin
)
2680 if (file
->f_mode
& FMODE_READ
)
2681 return seq_lseek(file
, offset
, origin
);
2686 static const struct file_operations tracing_fops
= {
2687 .open
= tracing_open
,
2689 .write
= tracing_write_stub
,
2690 .llseek
= tracing_seek
,
2691 .release
= tracing_release
,
2694 static const struct file_operations show_traces_fops
= {
2695 .open
= show_traces_open
,
2697 .release
= seq_release
,
2698 .llseek
= seq_lseek
,
2702 * Only trace on a CPU if the bitmask is set:
2704 static cpumask_var_t tracing_cpumask
;
2707 * The tracer itself will not take this lock, but still we want
2708 * to provide a consistent cpumask to user-space:
2710 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2713 * Temporary storage for the character representation of the
2714 * CPU bitmask (and one more byte for the newline):
2716 static char mask_str
[NR_CPUS
+ 1];
2719 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2720 size_t count
, loff_t
*ppos
)
2724 mutex_lock(&tracing_cpumask_update_lock
);
2726 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2727 if (count
- len
< 2) {
2731 len
+= sprintf(mask_str
+ len
, "\n");
2732 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2735 mutex_unlock(&tracing_cpumask_update_lock
);
2741 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2742 size_t count
, loff_t
*ppos
)
2745 cpumask_var_t tracing_cpumask_new
;
2747 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2750 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2754 mutex_lock(&tracing_cpumask_update_lock
);
2756 local_irq_disable();
2757 arch_spin_lock(&ftrace_max_lock
);
2758 for_each_tracing_cpu(cpu
) {
2760 * Increase/decrease the disabled counter if we are
2761 * about to flip a bit in the cpumask:
2763 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2764 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2765 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2766 ring_buffer_record_disable_cpu(global_trace
.buffer
, cpu
);
2768 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2769 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2770 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2771 ring_buffer_record_enable_cpu(global_trace
.buffer
, cpu
);
2774 arch_spin_unlock(&ftrace_max_lock
);
2777 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2779 mutex_unlock(&tracing_cpumask_update_lock
);
2780 free_cpumask_var(tracing_cpumask_new
);
2785 free_cpumask_var(tracing_cpumask_new
);
2790 static const struct file_operations tracing_cpumask_fops
= {
2791 .open
= tracing_open_generic
,
2792 .read
= tracing_cpumask_read
,
2793 .write
= tracing_cpumask_write
,
2794 .llseek
= generic_file_llseek
,
2797 static int tracing_trace_options_show(struct seq_file
*m
, void *v
)
2799 struct tracer_opt
*trace_opts
;
2803 mutex_lock(&trace_types_lock
);
2804 tracer_flags
= current_trace
->flags
->val
;
2805 trace_opts
= current_trace
->flags
->opts
;
2807 for (i
= 0; trace_options
[i
]; i
++) {
2808 if (trace_flags
& (1 << i
))
2809 seq_printf(m
, "%s\n", trace_options
[i
]);
2811 seq_printf(m
, "no%s\n", trace_options
[i
]);
2814 for (i
= 0; trace_opts
[i
].name
; i
++) {
2815 if (tracer_flags
& trace_opts
[i
].bit
)
2816 seq_printf(m
, "%s\n", trace_opts
[i
].name
);
2818 seq_printf(m
, "no%s\n", trace_opts
[i
].name
);
2820 mutex_unlock(&trace_types_lock
);
2825 static int __set_tracer_option(struct tracer
*trace
,
2826 struct tracer_flags
*tracer_flags
,
2827 struct tracer_opt
*opts
, int neg
)
2831 ret
= trace
->set_flag(tracer_flags
->val
, opts
->bit
, !neg
);
2836 tracer_flags
->val
&= ~opts
->bit
;
2838 tracer_flags
->val
|= opts
->bit
;
2842 /* Try to assign a tracer specific option */
2843 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2845 struct tracer_flags
*tracer_flags
= trace
->flags
;
2846 struct tracer_opt
*opts
= NULL
;
2849 for (i
= 0; tracer_flags
->opts
[i
].name
; i
++) {
2850 opts
= &tracer_flags
->opts
[i
];
2852 if (strcmp(cmp
, opts
->name
) == 0)
2853 return __set_tracer_option(trace
, trace
->flags
,
2860 static void set_tracer_flags(unsigned int mask
, int enabled
)
2862 /* do nothing if flag is already set */
2863 if (!!(trace_flags
& mask
) == !!enabled
)
2867 trace_flags
|= mask
;
2869 trace_flags
&= ~mask
;
2871 if (mask
== TRACE_ITER_RECORD_CMD
)
2872 trace_event_enable_cmd_record(enabled
);
2874 if (mask
== TRACE_ITER_OVERWRITE
)
2875 ring_buffer_change_overwrite(global_trace
.buffer
, enabled
);
2877 if (mask
== TRACE_ITER_PRINTK
)
2878 trace_printk_start_stop_comm(enabled
);
2881 static int trace_set_options(char *option
)
2888 cmp
= strstrip(option
);
2890 if (strncmp(cmp
, "no", 2) == 0) {
2895 for (i
= 0; trace_options
[i
]; i
++) {
2896 if (strcmp(cmp
, trace_options
[i
]) == 0) {
2897 set_tracer_flags(1 << i
, !neg
);
2902 /* If no option could be set, test the specific tracer options */
2903 if (!trace_options
[i
]) {
2904 mutex_lock(&trace_types_lock
);
2905 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2906 mutex_unlock(&trace_types_lock
);
2913 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2914 size_t cnt
, loff_t
*ppos
)
2918 if (cnt
>= sizeof(buf
))
2921 if (copy_from_user(&buf
, ubuf
, cnt
))
2926 trace_set_options(buf
);
2933 static int tracing_trace_options_open(struct inode
*inode
, struct file
*file
)
2935 if (tracing_disabled
)
2937 return single_open(file
, tracing_trace_options_show
, NULL
);
2940 static const struct file_operations tracing_iter_fops
= {
2941 .open
= tracing_trace_options_open
,
2943 .llseek
= seq_lseek
,
2944 .release
= single_release
,
2945 .write
= tracing_trace_options_write
,
2948 static const char readme_msg
[] =
2949 "tracing mini-HOWTO:\n\n"
2950 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2951 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2952 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
2953 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2955 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
2956 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2958 "# cat /sys/kernel/debug/tracing/trace_options\n"
2959 "noprint-parent nosym-offset nosym-addr noverbose\n"
2960 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2961 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
2962 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2963 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
2967 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2968 size_t cnt
, loff_t
*ppos
)
2970 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2971 readme_msg
, strlen(readme_msg
));
2974 static const struct file_operations tracing_readme_fops
= {
2975 .open
= tracing_open_generic
,
2976 .read
= tracing_readme_read
,
2977 .llseek
= generic_file_llseek
,
2981 tracing_saved_cmdlines_read(struct file
*file
, char __user
*ubuf
,
2982 size_t cnt
, loff_t
*ppos
)
2991 file_buf
= kmalloc(SAVED_CMDLINES
*(16+TASK_COMM_LEN
), GFP_KERNEL
);
2995 buf_comm
= kmalloc(TASK_COMM_LEN
, GFP_KERNEL
);
3003 for (i
= 0; i
< SAVED_CMDLINES
; i
++) {
3006 pid
= map_cmdline_to_pid
[i
];
3007 if (pid
== -1 || pid
== NO_CMDLINE_MAP
)
3010 trace_find_cmdline(pid
, buf_comm
);
3011 r
= sprintf(buf
, "%d %s\n", pid
, buf_comm
);
3016 len
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
3025 static const struct file_operations tracing_saved_cmdlines_fops
= {
3026 .open
= tracing_open_generic
,
3027 .read
= tracing_saved_cmdlines_read
,
3028 .llseek
= generic_file_llseek
,
3032 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
3033 size_t cnt
, loff_t
*ppos
)
3035 char buf
[MAX_TRACER_SIZE
+2];
3038 mutex_lock(&trace_types_lock
);
3039 r
= sprintf(buf
, "%s\n", current_trace
->name
);
3040 mutex_unlock(&trace_types_lock
);
3042 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3045 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
3047 tracing_reset_online_cpus(tr
);
3051 static void set_buffer_entries(struct trace_array
*tr
, unsigned long val
)
3054 for_each_tracing_cpu(cpu
)
3055 tr
->data
[cpu
]->entries
= val
;
3058 /* resize @tr's buffer to the size of @size_tr's entries */
3059 static int resize_buffer_duplicate_size(struct trace_array
*tr
,
3060 struct trace_array
*size_tr
, int cpu_id
)
3064 if (cpu_id
== RING_BUFFER_ALL_CPUS
) {
3065 for_each_tracing_cpu(cpu
) {
3066 ret
= ring_buffer_resize(tr
->buffer
,
3067 size_tr
->data
[cpu
]->entries
, cpu
);
3070 tr
->data
[cpu
]->entries
= size_tr
->data
[cpu
]->entries
;
3073 ret
= ring_buffer_resize(tr
->buffer
,
3074 size_tr
->data
[cpu_id
]->entries
, cpu_id
);
3076 tr
->data
[cpu_id
]->entries
=
3077 size_tr
->data
[cpu_id
]->entries
;
3083 static int __tracing_resize_ring_buffer(unsigned long size
, int cpu
)
3088 * If kernel or user changes the size of the ring buffer
3089 * we use the size that was given, and we can forget about
3090 * expanding it later.
3092 ring_buffer_expanded
= 1;
3094 /* May be called before buffers are initialized */
3095 if (!global_trace
.buffer
)
3098 ret
= ring_buffer_resize(global_trace
.buffer
, size
, cpu
);
3102 if (!current_trace
->use_max_tr
)
3105 ret
= ring_buffer_resize(max_tr
.buffer
, size
, cpu
);
3107 int r
= resize_buffer_duplicate_size(&global_trace
,
3108 &global_trace
, cpu
);
3111 * AARGH! We are left with different
3112 * size max buffer!!!!
3113 * The max buffer is our "snapshot" buffer.
3114 * When a tracer needs a snapshot (one of the
3115 * latency tracers), it swaps the max buffer
3116 * with the saved snap shot. We succeeded to
3117 * update the size of the main buffer, but failed to
3118 * update the size of the max buffer. But when we tried
3119 * to reset the main buffer to the original size, we
3120 * failed there too. This is very unlikely to
3121 * happen, but if it does, warn and kill all
3125 tracing_disabled
= 1;
3130 if (cpu
== RING_BUFFER_ALL_CPUS
)
3131 set_buffer_entries(&max_tr
, size
);
3133 max_tr
.data
[cpu
]->entries
= size
;
3136 if (cpu
== RING_BUFFER_ALL_CPUS
)
3137 set_buffer_entries(&global_trace
, size
);
3139 global_trace
.data
[cpu
]->entries
= size
;
3144 static ssize_t
tracing_resize_ring_buffer(unsigned long size
, int cpu_id
)
3148 mutex_lock(&trace_types_lock
);
3150 if (cpu_id
!= RING_BUFFER_ALL_CPUS
) {
3151 /* make sure, this cpu is enabled in the mask */
3152 if (!cpumask_test_cpu(cpu_id
, tracing_buffer_mask
)) {
3158 ret
= __tracing_resize_ring_buffer(size
, cpu_id
);
3163 mutex_unlock(&trace_types_lock
);
3170 * tracing_update_buffers - used by tracing facility to expand ring buffers
3172 * To save on memory when the tracing is never used on a system with it
3173 * configured in. The ring buffers are set to a minimum size. But once
3174 * a user starts to use the tracing facility, then they need to grow
3175 * to their default size.
3177 * This function is to be called when a tracer is about to be used.
3179 int tracing_update_buffers(void)
3183 mutex_lock(&trace_types_lock
);
3184 if (!ring_buffer_expanded
)
3185 ret
= __tracing_resize_ring_buffer(trace_buf_size
,
3186 RING_BUFFER_ALL_CPUS
);
3187 mutex_unlock(&trace_types_lock
);
3192 struct trace_option_dentry
;
3194 static struct trace_option_dentry
*
3195 create_trace_option_files(struct tracer
*tracer
);
3198 destroy_trace_option_files(struct trace_option_dentry
*topts
);
3200 static int tracing_set_tracer(const char *buf
)
3202 static struct trace_option_dentry
*topts
;
3203 struct trace_array
*tr
= &global_trace
;
3208 mutex_lock(&trace_types_lock
);
3210 if (!ring_buffer_expanded
) {
3211 ret
= __tracing_resize_ring_buffer(trace_buf_size
,
3212 RING_BUFFER_ALL_CPUS
);
3218 for (t
= trace_types
; t
; t
= t
->next
) {
3219 if (strcmp(t
->name
, buf
) == 0)
3226 if (t
== current_trace
)
3229 trace_branch_disable();
3230 if (current_trace
->reset
)
3231 current_trace
->reset(tr
);
3233 had_max_tr
= current_trace
->allocated_snapshot
;
3234 current_trace
= &nop_trace
;
3236 if (had_max_tr
&& !t
->use_max_tr
) {
3238 * We need to make sure that the update_max_tr sees that
3239 * current_trace changed to nop_trace to keep it from
3240 * swapping the buffers after we resize it.
3241 * The update_max_tr is called from interrupts disabled
3242 * so a synchronized_sched() is sufficient.
3244 synchronize_sched();
3246 * We don't free the ring buffer. instead, resize it because
3247 * The max_tr ring buffer has some state (e.g. ring->clock) and
3248 * we want preserve it.
3250 ring_buffer_resize(max_tr
.buffer
, 1, RING_BUFFER_ALL_CPUS
);
3251 set_buffer_entries(&max_tr
, 1);
3252 tracing_reset_online_cpus(&max_tr
);
3253 current_trace
->allocated_snapshot
= false;
3255 destroy_trace_option_files(topts
);
3257 topts
= create_trace_option_files(t
);
3258 if (t
->use_max_tr
&& !had_max_tr
) {
3259 /* we need to make per cpu buffer sizes equivalent */
3260 ret
= resize_buffer_duplicate_size(&max_tr
, &global_trace
,
3261 RING_BUFFER_ALL_CPUS
);
3264 t
->allocated_snapshot
= true;
3268 ret
= tracer_init(t
, tr
);
3274 trace_branch_enable(tr
);
3276 mutex_unlock(&trace_types_lock
);
3282 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
3283 size_t cnt
, loff_t
*ppos
)
3285 char buf
[MAX_TRACER_SIZE
+1];
3292 if (cnt
> MAX_TRACER_SIZE
)
3293 cnt
= MAX_TRACER_SIZE
;
3295 if (copy_from_user(&buf
, ubuf
, cnt
))
3300 /* strip ending whitespace. */
3301 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
3304 err
= tracing_set_tracer(buf
);
3314 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
3315 size_t cnt
, loff_t
*ppos
)
3317 unsigned long *ptr
= filp
->private_data
;
3321 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
3322 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
3323 if (r
> sizeof(buf
))
3325 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3329 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
3330 size_t cnt
, loff_t
*ppos
)
3332 unsigned long *ptr
= filp
->private_data
;
3336 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
3345 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
3347 long cpu_file
= (long) inode
->i_private
;
3348 struct trace_iterator
*iter
;
3351 if (tracing_disabled
)
3354 mutex_lock(&trace_types_lock
);
3356 /* create a buffer to store the information to pass to userspace */
3357 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
3364 * We make a copy of the current tracer to avoid concurrent
3365 * changes on it while we are reading.
3367 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
3372 *iter
->trace
= *current_trace
;
3374 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
3379 /* trace pipe does not show start of buffer */
3380 cpumask_setall(iter
->started
);
3382 if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
3383 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
3385 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3386 if (trace_clocks
[trace_clock_id
].in_ns
)
3387 iter
->iter_flags
|= TRACE_FILE_TIME_IN_NS
;
3389 iter
->cpu_file
= cpu_file
;
3390 iter
->tr
= &global_trace
;
3391 mutex_init(&iter
->mutex
);
3392 filp
->private_data
= iter
;
3394 if (iter
->trace
->pipe_open
)
3395 iter
->trace
->pipe_open(iter
);
3397 nonseekable_open(inode
, filp
);
3399 mutex_unlock(&trace_types_lock
);
3405 mutex_unlock(&trace_types_lock
);
3409 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
3411 struct trace_iterator
*iter
= file
->private_data
;
3413 mutex_lock(&trace_types_lock
);
3415 if (iter
->trace
->pipe_close
)
3416 iter
->trace
->pipe_close(iter
);
3418 mutex_unlock(&trace_types_lock
);
3420 free_cpumask_var(iter
->started
);
3421 mutex_destroy(&iter
->mutex
);
3429 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
3431 struct trace_iterator
*iter
= filp
->private_data
;
3433 if (trace_flags
& TRACE_ITER_BLOCK
) {
3435 * Always select as readable when in blocking mode
3437 return POLLIN
| POLLRDNORM
;
3439 if (!trace_empty(iter
))
3440 return POLLIN
| POLLRDNORM
;
3441 poll_wait(filp
, &trace_wait
, poll_table
);
3442 if (!trace_empty(iter
))
3443 return POLLIN
| POLLRDNORM
;
3450 * This is a make-shift waitqueue.
3451 * A tracer might use this callback on some rare cases:
3453 * 1) the current tracer might hold the runqueue lock when it wakes up
3454 * a reader, hence a deadlock (sched, function, and function graph tracers)
3455 * 2) the function tracers, trace all functions, we don't want
3456 * the overhead of calling wake_up and friends
3457 * (and tracing them too)
3459 * Anyway, this is really very primitive wakeup.
3461 void poll_wait_pipe(struct trace_iterator
*iter
)
3463 set_current_state(TASK_INTERRUPTIBLE
);
3464 /* sleep for 100 msecs, and try again. */
3465 schedule_timeout(HZ
/ 10);
3468 /* Must be called with trace_types_lock mutex held. */
3469 static int tracing_wait_pipe(struct file
*filp
)
3471 struct trace_iterator
*iter
= filp
->private_data
;
3473 while (trace_empty(iter
)) {
3475 if ((filp
->f_flags
& O_NONBLOCK
)) {
3479 mutex_unlock(&iter
->mutex
);
3481 iter
->trace
->wait_pipe(iter
);
3483 mutex_lock(&iter
->mutex
);
3485 if (signal_pending(current
))
3489 * We block until we read something and tracing is disabled.
3490 * We still block if tracing is disabled, but we have never
3491 * read anything. This allows a user to cat this file, and
3492 * then enable tracing. But after we have read something,
3493 * we give an EOF when tracing is again disabled.
3495 * iter->pos will be 0 if we haven't read anything.
3497 if (!tracing_is_enabled() && iter
->pos
)
3508 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
3509 size_t cnt
, loff_t
*ppos
)
3511 struct trace_iterator
*iter
= filp
->private_data
;
3514 /* return any leftover data */
3515 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3519 trace_seq_init(&iter
->seq
);
3521 /* copy the tracer to avoid using a global lock all around */
3522 mutex_lock(&trace_types_lock
);
3523 if (unlikely(iter
->trace
->name
!= current_trace
->name
))
3524 *iter
->trace
= *current_trace
;
3525 mutex_unlock(&trace_types_lock
);
3528 * Avoid more than one consumer on a single file descriptor
3529 * This is just a matter of traces coherency, the ring buffer itself
3532 mutex_lock(&iter
->mutex
);
3533 if (iter
->trace
->read
) {
3534 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
3540 sret
= tracing_wait_pipe(filp
);
3544 /* stop when tracing is finished */
3545 if (trace_empty(iter
)) {
3550 if (cnt
>= PAGE_SIZE
)
3551 cnt
= PAGE_SIZE
- 1;
3553 /* reset all but tr, trace, and overruns */
3554 memset(&iter
->seq
, 0,
3555 sizeof(struct trace_iterator
) -
3556 offsetof(struct trace_iterator
, seq
));
3559 trace_event_read_lock();
3560 trace_access_lock(iter
->cpu_file
);
3561 while (trace_find_next_entry_inc(iter
) != NULL
) {
3562 enum print_line_t ret
;
3563 int len
= iter
->seq
.len
;
3565 ret
= print_trace_line(iter
);
3566 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3567 /* don't print partial lines */
3568 iter
->seq
.len
= len
;
3571 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3572 trace_consume(iter
);
3574 if (iter
->seq
.len
>= cnt
)
3578 * Setting the full flag means we reached the trace_seq buffer
3579 * size and we should leave by partial output condition above.
3580 * One of the trace_seq_* functions is not used properly.
3582 WARN_ONCE(iter
->seq
.full
, "full flag set for trace type %d",
3585 trace_access_unlock(iter
->cpu_file
);
3586 trace_event_read_unlock();
3588 /* Now copy what we have to the user */
3589 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3590 if (iter
->seq
.readpos
>= iter
->seq
.len
)
3591 trace_seq_init(&iter
->seq
);
3594 * If there was nothing to send to user, in spite of consuming trace
3595 * entries, go back to wait for more entries.
3601 mutex_unlock(&iter
->mutex
);
3606 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
3607 struct pipe_buffer
*buf
)
3609 __free_page(buf
->page
);
3612 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
3615 __free_page(spd
->pages
[idx
]);
3618 static const struct pipe_buf_operations tracing_pipe_buf_ops
= {
3620 .map
= generic_pipe_buf_map
,
3621 .unmap
= generic_pipe_buf_unmap
,
3622 .confirm
= generic_pipe_buf_confirm
,
3623 .release
= tracing_pipe_buf_release
,
3624 .steal
= generic_pipe_buf_steal
,
3625 .get
= generic_pipe_buf_get
,
3629 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
3634 /* Seq buffer is page-sized, exactly what we need. */
3636 count
= iter
->seq
.len
;
3637 ret
= print_trace_line(iter
);
3638 count
= iter
->seq
.len
- count
;
3641 iter
->seq
.len
-= count
;
3644 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3645 iter
->seq
.len
-= count
;
3649 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3650 trace_consume(iter
);
3652 if (!trace_find_next_entry_inc(iter
)) {
3662 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3664 struct pipe_inode_info
*pipe
,
3668 struct page
*pages_def
[PIPE_DEF_BUFFERS
];
3669 struct partial_page partial_def
[PIPE_DEF_BUFFERS
];
3670 struct trace_iterator
*iter
= filp
->private_data
;
3671 struct splice_pipe_desc spd
= {
3673 .partial
= partial_def
,
3674 .nr_pages
= 0, /* This gets updated below. */
3675 .nr_pages_max
= PIPE_DEF_BUFFERS
,
3677 .ops
= &tracing_pipe_buf_ops
,
3678 .spd_release
= tracing_spd_release_pipe
,
3684 if (splice_grow_spd(pipe
, &spd
))
3687 /* copy the tracer to avoid using a global lock all around */
3688 mutex_lock(&trace_types_lock
);
3689 if (unlikely(iter
->trace
->name
!= current_trace
->name
))
3690 *iter
->trace
= *current_trace
;
3691 mutex_unlock(&trace_types_lock
);
3693 mutex_lock(&iter
->mutex
);
3695 if (iter
->trace
->splice_read
) {
3696 ret
= iter
->trace
->splice_read(iter
, filp
,
3697 ppos
, pipe
, len
, flags
);
3702 ret
= tracing_wait_pipe(filp
);
3706 if (!iter
->ent
&& !trace_find_next_entry_inc(iter
)) {
3711 trace_event_read_lock();
3712 trace_access_lock(iter
->cpu_file
);
3714 /* Fill as many pages as possible. */
3715 for (i
= 0, rem
= len
; i
< pipe
->buffers
&& rem
; i
++) {
3716 spd
.pages
[i
] = alloc_page(GFP_KERNEL
);
3720 rem
= tracing_fill_pipe_page(rem
, iter
);
3722 /* Copy the data into the page, so we can start over. */
3723 ret
= trace_seq_to_buffer(&iter
->seq
,
3724 page_address(spd
.pages
[i
]),
3727 __free_page(spd
.pages
[i
]);
3730 spd
.partial
[i
].offset
= 0;
3731 spd
.partial
[i
].len
= iter
->seq
.len
;
3733 trace_seq_init(&iter
->seq
);
3736 trace_access_unlock(iter
->cpu_file
);
3737 trace_event_read_unlock();
3738 mutex_unlock(&iter
->mutex
);
3742 ret
= splice_to_pipe(pipe
, &spd
);
3744 splice_shrink_spd(&spd
);
3748 mutex_unlock(&iter
->mutex
);
3752 struct ftrace_entries_info
{
3753 struct trace_array
*tr
;
3757 static int tracing_entries_open(struct inode
*inode
, struct file
*filp
)
3759 struct ftrace_entries_info
*info
;
3761 if (tracing_disabled
)
3764 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3768 info
->tr
= &global_trace
;
3769 info
->cpu
= (unsigned long)inode
->i_private
;
3771 filp
->private_data
= info
;
3777 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3778 size_t cnt
, loff_t
*ppos
)
3780 struct ftrace_entries_info
*info
= filp
->private_data
;
3781 struct trace_array
*tr
= info
->tr
;
3786 mutex_lock(&trace_types_lock
);
3788 if (info
->cpu
== RING_BUFFER_ALL_CPUS
) {
3789 int cpu
, buf_size_same
;
3794 /* check if all cpu sizes are same */
3795 for_each_tracing_cpu(cpu
) {
3796 /* fill in the size from first enabled cpu */
3798 size
= tr
->data
[cpu
]->entries
;
3799 if (size
!= tr
->data
[cpu
]->entries
) {
3805 if (buf_size_same
) {
3806 if (!ring_buffer_expanded
)
3807 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3809 trace_buf_size
>> 10);
3811 r
= sprintf(buf
, "%lu\n", size
>> 10);
3813 r
= sprintf(buf
, "X\n");
3815 r
= sprintf(buf
, "%lu\n", tr
->data
[info
->cpu
]->entries
>> 10);
3817 mutex_unlock(&trace_types_lock
);
3819 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3824 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3825 size_t cnt
, loff_t
*ppos
)
3827 struct ftrace_entries_info
*info
= filp
->private_data
;
3831 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
3835 /* must have at least 1 entry */
3839 /* value is in KB */
3842 ret
= tracing_resize_ring_buffer(val
, info
->cpu
);
3852 tracing_entries_release(struct inode
*inode
, struct file
*filp
)
3854 struct ftrace_entries_info
*info
= filp
->private_data
;
3862 tracing_total_entries_read(struct file
*filp
, char __user
*ubuf
,
3863 size_t cnt
, loff_t
*ppos
)
3865 struct trace_array
*tr
= filp
->private_data
;
3868 unsigned long size
= 0, expanded_size
= 0;
3870 mutex_lock(&trace_types_lock
);
3871 for_each_tracing_cpu(cpu
) {
3872 size
+= tr
->data
[cpu
]->entries
>> 10;
3873 if (!ring_buffer_expanded
)
3874 expanded_size
+= trace_buf_size
>> 10;
3876 if (ring_buffer_expanded
)
3877 r
= sprintf(buf
, "%lu\n", size
);
3879 r
= sprintf(buf
, "%lu (expanded: %lu)\n", size
, expanded_size
);
3880 mutex_unlock(&trace_types_lock
);
3882 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3886 tracing_free_buffer_write(struct file
*filp
, const char __user
*ubuf
,
3887 size_t cnt
, loff_t
*ppos
)
3890 * There is no need to read what the user has written, this function
3891 * is just to make sure that there is no error when "echo" is used
3900 tracing_free_buffer_release(struct inode
*inode
, struct file
*filp
)
3902 /* disable tracing ? */
3903 if (trace_flags
& TRACE_ITER_STOP_ON_FREE
)
3905 /* resize the ring buffer to 0 */
3906 tracing_resize_ring_buffer(0, RING_BUFFER_ALL_CPUS
);
3912 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3913 size_t cnt
, loff_t
*fpos
)
3915 unsigned long addr
= (unsigned long)ubuf
;
3916 struct ring_buffer_event
*event
;
3917 struct ring_buffer
*buffer
;
3918 struct print_entry
*entry
;
3919 unsigned long irq_flags
;
3920 struct page
*pages
[2];
3930 if (tracing_disabled
)
3933 if (!(trace_flags
& TRACE_ITER_MARKERS
))
3936 if (cnt
> TRACE_BUF_SIZE
)
3937 cnt
= TRACE_BUF_SIZE
;
3940 * Userspace is injecting traces into the kernel trace buffer.
3941 * We want to be as non intrusive as possible.
3942 * To do so, we do not want to allocate any special buffers
3943 * or take any locks, but instead write the userspace data
3944 * straight into the ring buffer.
3946 * First we need to pin the userspace buffer into memory,
3947 * which, most likely it is, because it just referenced it.
3948 * But there's no guarantee that it is. By using get_user_pages_fast()
3949 * and kmap_atomic/kunmap_atomic() we can get access to the
3950 * pages directly. We then write the data directly into the
3953 BUILD_BUG_ON(TRACE_BUF_SIZE
>= PAGE_SIZE
);
3955 /* check if we cross pages */
3956 if ((addr
& PAGE_MASK
) != ((addr
+ cnt
) & PAGE_MASK
))
3959 offset
= addr
& (PAGE_SIZE
- 1);
3962 ret
= get_user_pages_fast(addr
, nr_pages
, 0, pages
);
3963 if (ret
< nr_pages
) {
3965 put_page(pages
[ret
]);
3970 for (i
= 0; i
< nr_pages
; i
++)
3971 map_page
[i
] = kmap_atomic(pages
[i
]);
3973 local_save_flags(irq_flags
);
3974 size
= sizeof(*entry
) + cnt
+ 2; /* possible \n added */
3975 buffer
= global_trace
.buffer
;
3976 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
3977 irq_flags
, preempt_count());
3979 /* Ring buffer disabled, return as if not open for write */
3984 entry
= ring_buffer_event_data(event
);
3985 entry
->ip
= _THIS_IP_
;
3987 if (nr_pages
== 2) {
3988 len
= PAGE_SIZE
- offset
;
3989 memcpy(&entry
->buf
, map_page
[0] + offset
, len
);
3990 memcpy(&entry
->buf
[len
], map_page
[1], cnt
- len
);
3992 memcpy(&entry
->buf
, map_page
[0] + offset
, cnt
);
3994 if (entry
->buf
[cnt
- 1] != '\n') {
3995 entry
->buf
[cnt
] = '\n';
3996 entry
->buf
[cnt
+ 1] = '\0';
3998 entry
->buf
[cnt
] = '\0';
4000 __buffer_unlock_commit(buffer
, event
);
4007 for (i
= 0; i
< nr_pages
; i
++){
4008 kunmap_atomic(map_page
[i
]);
4015 static int tracing_clock_show(struct seq_file
*m
, void *v
)
4019 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++)
4021 "%s%s%s%s", i
? " " : "",
4022 i
== trace_clock_id
? "[" : "", trace_clocks
[i
].name
,
4023 i
== trace_clock_id
? "]" : "");
4029 static ssize_t
tracing_clock_write(struct file
*filp
, const char __user
*ubuf
,
4030 size_t cnt
, loff_t
*fpos
)
4033 const char *clockstr
;
4036 if (cnt
>= sizeof(buf
))
4039 if (copy_from_user(&buf
, ubuf
, cnt
))
4044 clockstr
= strstrip(buf
);
4046 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++) {
4047 if (strcmp(trace_clocks
[i
].name
, clockstr
) == 0)
4050 if (i
== ARRAY_SIZE(trace_clocks
))
4055 mutex_lock(&trace_types_lock
);
4057 ring_buffer_set_clock(global_trace
.buffer
, trace_clocks
[i
].func
);
4059 ring_buffer_set_clock(max_tr
.buffer
, trace_clocks
[i
].func
);
4062 * New clock may not be consistent with the previous clock.
4063 * Reset the buffer so that it doesn't have incomparable timestamps.
4065 tracing_reset_online_cpus(&global_trace
);
4066 tracing_reset_online_cpus(&max_tr
);
4068 mutex_unlock(&trace_types_lock
);
4075 static int tracing_clock_open(struct inode
*inode
, struct file
*file
)
4077 if (tracing_disabled
)
4079 return single_open(file
, tracing_clock_show
, NULL
);
4082 #ifdef CONFIG_TRACER_SNAPSHOT
4083 static int tracing_snapshot_open(struct inode
*inode
, struct file
*file
)
4085 struct trace_iterator
*iter
;
4088 if (file
->f_mode
& FMODE_READ
) {
4089 iter
= __tracing_open(inode
, file
, true);
4091 ret
= PTR_ERR(iter
);
4097 tracing_snapshot_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
4103 ret
= tracing_update_buffers();
4107 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
4111 mutex_lock(&trace_types_lock
);
4113 if (current_trace
->use_max_tr
) {
4120 if (current_trace
->allocated_snapshot
) {
4121 /* free spare buffer */
4122 ring_buffer_resize(max_tr
.buffer
, 1,
4123 RING_BUFFER_ALL_CPUS
);
4124 set_buffer_entries(&max_tr
, 1);
4125 tracing_reset_online_cpus(&max_tr
);
4126 current_trace
->allocated_snapshot
= false;
4130 if (!current_trace
->allocated_snapshot
) {
4131 /* allocate spare buffer */
4132 ret
= resize_buffer_duplicate_size(&max_tr
,
4133 &global_trace
, RING_BUFFER_ALL_CPUS
);
4136 current_trace
->allocated_snapshot
= true;
4139 local_irq_disable();
4140 /* Now, we're going to swap */
4141 update_max_tr(&global_trace
, current
, smp_processor_id());
4145 if (current_trace
->allocated_snapshot
)
4146 tracing_reset_online_cpus(&max_tr
);
4157 mutex_unlock(&trace_types_lock
);
4160 #endif /* CONFIG_TRACER_SNAPSHOT */
4163 static const struct file_operations tracing_max_lat_fops
= {
4164 .open
= tracing_open_generic
,
4165 .read
= tracing_max_lat_read
,
4166 .write
= tracing_max_lat_write
,
4167 .llseek
= generic_file_llseek
,
4170 static const struct file_operations set_tracer_fops
= {
4171 .open
= tracing_open_generic
,
4172 .read
= tracing_set_trace_read
,
4173 .write
= tracing_set_trace_write
,
4174 .llseek
= generic_file_llseek
,
4177 static const struct file_operations tracing_pipe_fops
= {
4178 .open
= tracing_open_pipe
,
4179 .poll
= tracing_poll_pipe
,
4180 .read
= tracing_read_pipe
,
4181 .splice_read
= tracing_splice_read_pipe
,
4182 .release
= tracing_release_pipe
,
4183 .llseek
= no_llseek
,
4186 static const struct file_operations tracing_entries_fops
= {
4187 .open
= tracing_entries_open
,
4188 .read
= tracing_entries_read
,
4189 .write
= tracing_entries_write
,
4190 .release
= tracing_entries_release
,
4191 .llseek
= generic_file_llseek
,
4194 static const struct file_operations tracing_total_entries_fops
= {
4195 .open
= tracing_open_generic
,
4196 .read
= tracing_total_entries_read
,
4197 .llseek
= generic_file_llseek
,
4200 static const struct file_operations tracing_free_buffer_fops
= {
4201 .write
= tracing_free_buffer_write
,
4202 .release
= tracing_free_buffer_release
,
4205 static const struct file_operations tracing_mark_fops
= {
4206 .open
= tracing_open_generic
,
4207 .write
= tracing_mark_write
,
4208 .llseek
= generic_file_llseek
,
4211 static const struct file_operations trace_clock_fops
= {
4212 .open
= tracing_clock_open
,
4214 .llseek
= seq_lseek
,
4215 .release
= single_release
,
4216 .write
= tracing_clock_write
,
4219 #ifdef CONFIG_TRACER_SNAPSHOT
4220 static const struct file_operations snapshot_fops
= {
4221 .open
= tracing_snapshot_open
,
4223 .write
= tracing_snapshot_write
,
4224 .llseek
= tracing_seek
,
4225 .release
= tracing_release
,
4227 #endif /* CONFIG_TRACER_SNAPSHOT */
4229 struct ftrace_buffer_info
{
4230 struct trace_array
*tr
;
4236 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
4238 int cpu
= (int)(long)inode
->i_private
;
4239 struct ftrace_buffer_info
*info
;
4241 if (tracing_disabled
)
4244 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
4248 info
->tr
= &global_trace
;
4251 /* Force reading ring buffer for first read */
4252 info
->read
= (unsigned int)-1;
4254 filp
->private_data
= info
;
4256 return nonseekable_open(inode
, filp
);
4260 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
4261 size_t count
, loff_t
*ppos
)
4263 struct ftrace_buffer_info
*info
= filp
->private_data
;
4271 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
, info
->cpu
);
4275 /* Do we have previous read data to read? */
4276 if (info
->read
< PAGE_SIZE
)
4279 trace_access_lock(info
->cpu
);
4280 ret
= ring_buffer_read_page(info
->tr
->buffer
,
4284 trace_access_unlock(info
->cpu
);
4291 size
= PAGE_SIZE
- info
->read
;
4295 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
4306 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
4308 struct ftrace_buffer_info
*info
= file
->private_data
;
4311 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
4318 struct ring_buffer
*buffer
;
4323 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
4324 struct pipe_buffer
*buf
)
4326 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
4331 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
4336 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
4337 struct pipe_buffer
*buf
)
4339 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
4344 /* Pipe buffer operations for a buffer. */
4345 static const struct pipe_buf_operations buffer_pipe_buf_ops
= {
4347 .map
= generic_pipe_buf_map
,
4348 .unmap
= generic_pipe_buf_unmap
,
4349 .confirm
= generic_pipe_buf_confirm
,
4350 .release
= buffer_pipe_buf_release
,
4351 .steal
= generic_pipe_buf_steal
,
4352 .get
= buffer_pipe_buf_get
,
4356 * Callback from splice_to_pipe(), if we need to release some pages
4357 * at the end of the spd in case we error'ed out in filling the pipe.
4359 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
4361 struct buffer_ref
*ref
=
4362 (struct buffer_ref
*)spd
->partial
[i
].private;
4367 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
4369 spd
->partial
[i
].private = 0;
4373 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
4374 struct pipe_inode_info
*pipe
, size_t len
,
4377 struct ftrace_buffer_info
*info
= file
->private_data
;
4378 struct partial_page partial_def
[PIPE_DEF_BUFFERS
];
4379 struct page
*pages_def
[PIPE_DEF_BUFFERS
];
4380 struct splice_pipe_desc spd
= {
4382 .partial
= partial_def
,
4383 .nr_pages_max
= PIPE_DEF_BUFFERS
,
4385 .ops
= &buffer_pipe_buf_ops
,
4386 .spd_release
= buffer_spd_release
,
4388 struct buffer_ref
*ref
;
4389 int entries
, size
, i
;
4392 if (splice_grow_spd(pipe
, &spd
))
4395 if (*ppos
& (PAGE_SIZE
- 1)) {
4400 if (len
& (PAGE_SIZE
- 1)) {
4401 if (len
< PAGE_SIZE
) {
4408 trace_access_lock(info
->cpu
);
4409 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
4411 for (i
= 0; i
< pipe
->buffers
&& len
&& entries
; i
++, len
-= PAGE_SIZE
) {
4415 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
4420 ref
->buffer
= info
->tr
->buffer
;
4421 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
, info
->cpu
);
4427 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
4430 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
4436 * zero out any left over data, this is going to
4439 size
= ring_buffer_page_len(ref
->page
);
4440 if (size
< PAGE_SIZE
)
4441 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
4443 page
= virt_to_page(ref
->page
);
4445 spd
.pages
[i
] = page
;
4446 spd
.partial
[i
].len
= PAGE_SIZE
;
4447 spd
.partial
[i
].offset
= 0;
4448 spd
.partial
[i
].private = (unsigned long)ref
;
4452 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
4455 trace_access_unlock(info
->cpu
);
4458 /* did we read anything? */
4459 if (!spd
.nr_pages
) {
4460 if (flags
& SPLICE_F_NONBLOCK
)
4468 ret
= splice_to_pipe(pipe
, &spd
);
4469 splice_shrink_spd(&spd
);
4474 static const struct file_operations tracing_buffers_fops
= {
4475 .open
= tracing_buffers_open
,
4476 .read
= tracing_buffers_read
,
4477 .release
= tracing_buffers_release
,
4478 .splice_read
= tracing_buffers_splice_read
,
4479 .llseek
= no_llseek
,
4483 tracing_stats_read(struct file
*filp
, char __user
*ubuf
,
4484 size_t count
, loff_t
*ppos
)
4486 unsigned long cpu
= (unsigned long)filp
->private_data
;
4487 struct trace_array
*tr
= &global_trace
;
4488 struct trace_seq
*s
;
4490 unsigned long long t
;
4491 unsigned long usec_rem
;
4493 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
4499 cnt
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
4500 trace_seq_printf(s
, "entries: %ld\n", cnt
);
4502 cnt
= ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
4503 trace_seq_printf(s
, "overrun: %ld\n", cnt
);
4505 cnt
= ring_buffer_commit_overrun_cpu(tr
->buffer
, cpu
);
4506 trace_seq_printf(s
, "commit overrun: %ld\n", cnt
);
4508 cnt
= ring_buffer_bytes_cpu(tr
->buffer
, cpu
);
4509 trace_seq_printf(s
, "bytes: %ld\n", cnt
);
4511 if (trace_clocks
[trace_clock_id
].in_ns
) {
4512 /* local or global for trace_clock */
4513 t
= ns2usecs(ring_buffer_oldest_event_ts(tr
->buffer
, cpu
));
4514 usec_rem
= do_div(t
, USEC_PER_SEC
);
4515 trace_seq_printf(s
, "oldest event ts: %5llu.%06lu\n",
4518 t
= ns2usecs(ring_buffer_time_stamp(tr
->buffer
, cpu
));
4519 usec_rem
= do_div(t
, USEC_PER_SEC
);
4520 trace_seq_printf(s
, "now ts: %5llu.%06lu\n", t
, usec_rem
);
4522 /* counter or tsc mode for trace_clock */
4523 trace_seq_printf(s
, "oldest event ts: %llu\n",
4524 ring_buffer_oldest_event_ts(tr
->buffer
, cpu
));
4526 trace_seq_printf(s
, "now ts: %llu\n",
4527 ring_buffer_time_stamp(tr
->buffer
, cpu
));
4530 cnt
= ring_buffer_dropped_events_cpu(tr
->buffer
, cpu
);
4531 trace_seq_printf(s
, "dropped events: %ld\n", cnt
);
4533 cnt
= ring_buffer_read_events_cpu(tr
->buffer
, cpu
);
4534 trace_seq_printf(s
, "read events: %ld\n", cnt
);
4536 count
= simple_read_from_buffer(ubuf
, count
, ppos
, s
->buffer
, s
->len
);
4543 static const struct file_operations tracing_stats_fops
= {
4544 .open
= tracing_open_generic
,
4545 .read
= tracing_stats_read
,
4546 .llseek
= generic_file_llseek
,
4549 #ifdef CONFIG_DYNAMIC_FTRACE
4551 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
4557 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
4558 size_t cnt
, loff_t
*ppos
)
4560 static char ftrace_dyn_info_buffer
[1024];
4561 static DEFINE_MUTEX(dyn_info_mutex
);
4562 unsigned long *p
= filp
->private_data
;
4563 char *buf
= ftrace_dyn_info_buffer
;
4564 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
4567 mutex_lock(&dyn_info_mutex
);
4568 r
= sprintf(buf
, "%ld ", *p
);
4570 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
4573 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
4575 mutex_unlock(&dyn_info_mutex
);
4580 static const struct file_operations tracing_dyn_info_fops
= {
4581 .open
= tracing_open_generic
,
4582 .read
= tracing_read_dyn_info
,
4583 .llseek
= generic_file_llseek
,
4587 static struct dentry
*d_tracer
;
4589 struct dentry
*tracing_init_dentry(void)
4596 if (!debugfs_initialized())
4599 d_tracer
= debugfs_create_dir("tracing", NULL
);
4601 if (!d_tracer
&& !once
) {
4603 pr_warning("Could not create debugfs directory 'tracing'\n");
4610 static struct dentry
*d_percpu
;
4612 static struct dentry
*tracing_dentry_percpu(void)
4615 struct dentry
*d_tracer
;
4620 d_tracer
= tracing_init_dentry();
4625 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
4627 if (!d_percpu
&& !once
) {
4629 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4636 static void tracing_init_debugfs_percpu(long cpu
)
4638 struct dentry
*d_percpu
= tracing_dentry_percpu();
4639 struct dentry
*d_cpu
;
4640 char cpu_dir
[30]; /* 30 characters should be more than enough */
4645 snprintf(cpu_dir
, 30, "cpu%ld", cpu
);
4646 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
4648 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
4652 /* per cpu trace_pipe */
4653 trace_create_file("trace_pipe", 0444, d_cpu
,
4654 (void *) cpu
, &tracing_pipe_fops
);
4657 trace_create_file("trace", 0644, d_cpu
,
4658 (void *) cpu
, &tracing_fops
);
4660 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
4661 (void *) cpu
, &tracing_buffers_fops
);
4663 trace_create_file("stats", 0444, d_cpu
,
4664 (void *) cpu
, &tracing_stats_fops
);
4666 trace_create_file("buffer_size_kb", 0444, d_cpu
,
4667 (void *) cpu
, &tracing_entries_fops
);
4670 #ifdef CONFIG_FTRACE_SELFTEST
4671 /* Let selftest have access to static functions in this file */
4672 #include "trace_selftest.c"
4675 struct trace_option_dentry
{
4676 struct tracer_opt
*opt
;
4677 struct tracer_flags
*flags
;
4678 struct dentry
*entry
;
4682 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
4685 struct trace_option_dentry
*topt
= filp
->private_data
;
4688 if (topt
->flags
->val
& topt
->opt
->bit
)
4693 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
4697 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
4700 struct trace_option_dentry
*topt
= filp
->private_data
;
4704 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
4708 if (val
!= 0 && val
!= 1)
4711 if (!!(topt
->flags
->val
& topt
->opt
->bit
) != val
) {
4712 mutex_lock(&trace_types_lock
);
4713 ret
= __set_tracer_option(current_trace
, topt
->flags
,
4715 mutex_unlock(&trace_types_lock
);
4726 static const struct file_operations trace_options_fops
= {
4727 .open
= tracing_open_generic
,
4728 .read
= trace_options_read
,
4729 .write
= trace_options_write
,
4730 .llseek
= generic_file_llseek
,
4734 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
4737 long index
= (long)filp
->private_data
;
4740 if (trace_flags
& (1 << index
))
4745 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
4749 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
4752 long index
= (long)filp
->private_data
;
4756 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
4760 if (val
!= 0 && val
!= 1)
4762 set_tracer_flags(1 << index
, val
);
4769 static const struct file_operations trace_options_core_fops
= {
4770 .open
= tracing_open_generic
,
4771 .read
= trace_options_core_read
,
4772 .write
= trace_options_core_write
,
4773 .llseek
= generic_file_llseek
,
4776 struct dentry
*trace_create_file(const char *name
,
4778 struct dentry
*parent
,
4780 const struct file_operations
*fops
)
4784 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
4786 pr_warning("Could not create debugfs '%s' entry\n", name
);
4792 static struct dentry
*trace_options_init_dentry(void)
4794 struct dentry
*d_tracer
;
4795 static struct dentry
*t_options
;
4800 d_tracer
= tracing_init_dentry();
4804 t_options
= debugfs_create_dir("options", d_tracer
);
4806 pr_warning("Could not create debugfs directory 'options'\n");
4814 create_trace_option_file(struct trace_option_dentry
*topt
,
4815 struct tracer_flags
*flags
,
4816 struct tracer_opt
*opt
)
4818 struct dentry
*t_options
;
4820 t_options
= trace_options_init_dentry();
4824 topt
->flags
= flags
;
4827 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
4828 &trace_options_fops
);
4832 static struct trace_option_dentry
*
4833 create_trace_option_files(struct tracer
*tracer
)
4835 struct trace_option_dentry
*topts
;
4836 struct tracer_flags
*flags
;
4837 struct tracer_opt
*opts
;
4843 flags
= tracer
->flags
;
4845 if (!flags
|| !flags
->opts
)
4850 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4853 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
4857 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4858 create_trace_option_file(&topts
[cnt
], flags
,
4865 destroy_trace_option_files(struct trace_option_dentry
*topts
)
4872 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
4873 if (topts
[cnt
].entry
)
4874 debugfs_remove(topts
[cnt
].entry
);
4880 static struct dentry
*
4881 create_trace_option_core_file(const char *option
, long index
)
4883 struct dentry
*t_options
;
4885 t_options
= trace_options_init_dentry();
4889 return trace_create_file(option
, 0644, t_options
, (void *)index
,
4890 &trace_options_core_fops
);
4893 static __init
void create_trace_options_dir(void)
4895 struct dentry
*t_options
;
4898 t_options
= trace_options_init_dentry();
4902 for (i
= 0; trace_options
[i
]; i
++)
4903 create_trace_option_core_file(trace_options
[i
], i
);
4907 rb_simple_read(struct file
*filp
, char __user
*ubuf
,
4908 size_t cnt
, loff_t
*ppos
)
4910 struct trace_array
*tr
= filp
->private_data
;
4911 struct ring_buffer
*buffer
= tr
->buffer
;
4916 r
= ring_buffer_record_is_on(buffer
);
4920 r
= sprintf(buf
, "%d\n", r
);
4922 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
4926 rb_simple_write(struct file
*filp
, const char __user
*ubuf
,
4927 size_t cnt
, loff_t
*ppos
)
4929 struct trace_array
*tr
= filp
->private_data
;
4930 struct ring_buffer
*buffer
= tr
->buffer
;
4934 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
4939 mutex_lock(&trace_types_lock
);
4941 ring_buffer_record_on(buffer
);
4942 if (current_trace
->start
)
4943 current_trace
->start(tr
);
4945 ring_buffer_record_off(buffer
);
4946 if (current_trace
->stop
)
4947 current_trace
->stop(tr
);
4949 mutex_unlock(&trace_types_lock
);
4957 static const struct file_operations rb_simple_fops
= {
4958 .open
= tracing_open_generic
,
4959 .read
= rb_simple_read
,
4960 .write
= rb_simple_write
,
4961 .llseek
= default_llseek
,
4964 static __init
int tracer_init_debugfs(void)
4966 struct dentry
*d_tracer
;
4969 trace_access_lock_init();
4971 d_tracer
= tracing_init_dentry();
4973 trace_create_file("trace_options", 0644, d_tracer
,
4974 NULL
, &tracing_iter_fops
);
4976 trace_create_file("tracing_cpumask", 0644, d_tracer
,
4977 NULL
, &tracing_cpumask_fops
);
4979 trace_create_file("trace", 0644, d_tracer
,
4980 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
4982 trace_create_file("available_tracers", 0444, d_tracer
,
4983 &global_trace
, &show_traces_fops
);
4985 trace_create_file("current_tracer", 0644, d_tracer
,
4986 &global_trace
, &set_tracer_fops
);
4988 #ifdef CONFIG_TRACER_MAX_TRACE
4989 trace_create_file("tracing_max_latency", 0644, d_tracer
,
4990 &tracing_max_latency
, &tracing_max_lat_fops
);
4993 trace_create_file("tracing_thresh", 0644, d_tracer
,
4994 &tracing_thresh
, &tracing_max_lat_fops
);
4996 trace_create_file("README", 0444, d_tracer
,
4997 NULL
, &tracing_readme_fops
);
4999 trace_create_file("trace_pipe", 0444, d_tracer
,
5000 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
5002 trace_create_file("buffer_size_kb", 0644, d_tracer
,
5003 (void *) RING_BUFFER_ALL_CPUS
, &tracing_entries_fops
);
5005 trace_create_file("buffer_total_size_kb", 0444, d_tracer
,
5006 &global_trace
, &tracing_total_entries_fops
);
5008 trace_create_file("free_buffer", 0644, d_tracer
,
5009 &global_trace
, &tracing_free_buffer_fops
);
5011 trace_create_file("trace_marker", 0220, d_tracer
,
5012 NULL
, &tracing_mark_fops
);
5014 trace_create_file("saved_cmdlines", 0444, d_tracer
,
5015 NULL
, &tracing_saved_cmdlines_fops
);
5017 trace_create_file("trace_clock", 0644, d_tracer
, NULL
,
5020 trace_create_file("tracing_on", 0644, d_tracer
,
5021 &global_trace
, &rb_simple_fops
);
5023 #ifdef CONFIG_DYNAMIC_FTRACE
5024 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
5025 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
5028 #ifdef CONFIG_TRACER_SNAPSHOT
5029 trace_create_file("snapshot", 0644, d_tracer
,
5030 (void *) TRACE_PIPE_ALL_CPU
, &snapshot_fops
);
5033 create_trace_options_dir();
5035 for_each_tracing_cpu(cpu
)
5036 tracing_init_debugfs_percpu(cpu
);
5041 static int trace_panic_handler(struct notifier_block
*this,
5042 unsigned long event
, void *unused
)
5044 if (ftrace_dump_on_oops
)
5045 ftrace_dump(ftrace_dump_on_oops
);
5049 static struct notifier_block trace_panic_notifier
= {
5050 .notifier_call
= trace_panic_handler
,
5052 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
5055 static int trace_die_handler(struct notifier_block
*self
,
5061 if (ftrace_dump_on_oops
)
5062 ftrace_dump(ftrace_dump_on_oops
);
5070 static struct notifier_block trace_die_notifier
= {
5071 .notifier_call
= trace_die_handler
,
5076 * printk is set to max of 1024, we really don't need it that big.
5077 * Nothing should be printing 1000 characters anyway.
5079 #define TRACE_MAX_PRINT 1000
5082 * Define here KERN_TRACE so that we have one place to modify
5083 * it if we decide to change what log level the ftrace dump
5086 #define KERN_TRACE KERN_EMERG
5089 trace_printk_seq(struct trace_seq
*s
)
5091 /* Probably should print a warning here. */
5095 /* should be zero ended, but we are paranoid. */
5096 s
->buffer
[s
->len
] = 0;
5098 printk(KERN_TRACE
"%s", s
->buffer
);
5103 void trace_init_global_iter(struct trace_iterator
*iter
)
5105 iter
->tr
= &global_trace
;
5106 iter
->trace
= current_trace
;
5107 iter
->cpu_file
= TRACE_PIPE_ALL_CPU
;
5111 __ftrace_dump(bool disable_tracing
, enum ftrace_dump_mode oops_dump_mode
)
5113 static arch_spinlock_t ftrace_dump_lock
=
5114 (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
5115 /* use static because iter can be a bit big for the stack */
5116 static struct trace_iterator iter
;
5117 unsigned int old_userobj
;
5118 static int dump_ran
;
5119 unsigned long flags
;
5123 local_irq_save(flags
);
5124 arch_spin_lock(&ftrace_dump_lock
);
5132 /* Did function tracer already get disabled? */
5133 if (ftrace_is_dead()) {
5134 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
5135 printk("# MAY BE MISSING FUNCTION EVENTS\n");
5138 if (disable_tracing
)
5141 /* Simulate the iterator */
5142 trace_init_global_iter(&iter
);
5144 for_each_tracing_cpu(cpu
) {
5145 atomic_inc(&iter
.tr
->data
[cpu
]->disabled
);
5148 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
5150 /* don't look at user memory in panic mode */
5151 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
5153 switch (oops_dump_mode
) {
5155 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
5158 iter
.cpu_file
= raw_smp_processor_id();
5163 printk(KERN_TRACE
"Bad dumping mode, switching to all CPUs dump\n");
5164 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
5167 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
5170 * We need to stop all tracing on all CPUS to read the
5171 * the next buffer. This is a bit expensive, but is
5172 * not done often. We fill all what we can read,
5173 * and then release the locks again.
5176 while (!trace_empty(&iter
)) {
5179 printk(KERN_TRACE
"---------------------------------\n");
5183 /* reset all but tr, trace, and overruns */
5184 memset(&iter
.seq
, 0,
5185 sizeof(struct trace_iterator
) -
5186 offsetof(struct trace_iterator
, seq
));
5187 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
5190 if (trace_find_next_entry_inc(&iter
) != NULL
) {
5193 ret
= print_trace_line(&iter
);
5194 if (ret
!= TRACE_TYPE_NO_CONSUME
)
5195 trace_consume(&iter
);
5197 touch_nmi_watchdog();
5199 trace_printk_seq(&iter
.seq
);
5203 printk(KERN_TRACE
" (ftrace buffer empty)\n");
5205 printk(KERN_TRACE
"---------------------------------\n");
5208 /* Re-enable tracing if requested */
5209 if (!disable_tracing
) {
5210 trace_flags
|= old_userobj
;
5212 for_each_tracing_cpu(cpu
) {
5213 atomic_dec(&iter
.tr
->data
[cpu
]->disabled
);
5219 arch_spin_unlock(&ftrace_dump_lock
);
5220 local_irq_restore(flags
);
5223 /* By default: disable tracing after the dump */
5224 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode
)
5226 __ftrace_dump(true, oops_dump_mode
);
5228 EXPORT_SYMBOL_GPL(ftrace_dump
);
5230 __init
static int tracer_alloc_buffers(void)
5233 enum ring_buffer_flags rb_flags
;
5238 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
5241 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
5242 goto out_free_buffer_mask
;
5244 /* Only allocate trace_printk buffers if a trace_printk exists */
5245 if (__stop___trace_bprintk_fmt
!= __start___trace_bprintk_fmt
)
5246 /* Must be called before global_trace.buffer is allocated */
5247 trace_printk_init_buffers();
5249 /* To save memory, keep the ring buffer size to its minimum */
5250 if (ring_buffer_expanded
)
5251 ring_buf_size
= trace_buf_size
;
5255 rb_flags
= trace_flags
& TRACE_ITER_OVERWRITE
? RB_FL_OVERWRITE
: 0;
5257 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
5258 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
5260 /* TODO: make the number of buffers hot pluggable with CPUS */
5261 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
, rb_flags
);
5262 if (!global_trace
.buffer
) {
5263 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
5265 goto out_free_cpumask
;
5267 if (global_trace
.buffer_disabled
)
5271 #ifdef CONFIG_TRACER_MAX_TRACE
5272 max_tr
.buffer
= ring_buffer_alloc(1, rb_flags
);
5273 if (!max_tr
.buffer
) {
5274 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
5276 ring_buffer_free(global_trace
.buffer
);
5277 goto out_free_cpumask
;
5281 /* Allocate the first page for all buffers */
5282 for_each_tracing_cpu(i
) {
5283 global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
5284 max_tr
.data
[i
] = &per_cpu(max_tr_data
, i
);
5287 set_buffer_entries(&global_trace
,
5288 ring_buffer_size(global_trace
.buffer
, 0));
5289 #ifdef CONFIG_TRACER_MAX_TRACE
5290 set_buffer_entries(&max_tr
, 1);
5293 trace_init_cmdlines();
5294 init_irq_work(&trace_work_wakeup
, trace_wake_up
);
5296 register_tracer(&nop_trace
);
5298 /* All seems OK, enable tracing */
5299 tracing_disabled
= 0;
5301 atomic_notifier_chain_register(&panic_notifier_list
,
5302 &trace_panic_notifier
);
5304 register_die_notifier(&trace_die_notifier
);
5306 while (trace_boot_options
) {
5309 option
= strsep(&trace_boot_options
, ",");
5310 trace_set_options(option
);
5316 free_cpumask_var(tracing_cpumask
);
5317 out_free_buffer_mask
:
5318 free_cpumask_var(tracing_buffer_mask
);
5323 __init
static int clear_boot_tracer(void)
5326 * The default tracer at boot buffer is an init section.
5327 * This function is called in lateinit. If we did not
5328 * find the boot tracer, then clear it out, to prevent
5329 * later registration from accessing the buffer that is
5330 * about to be freed.
5332 if (!default_bootup_tracer
)
5335 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
5336 default_bootup_tracer
);
5337 default_bootup_tracer
= NULL
;
5342 early_initcall(tracer_alloc_buffers
);
5343 fs_initcall(tracer_init_debugfs
);
5344 late_initcall(clear_boot_tracer
);