2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/ring_buffer.h>
15 #include <generated/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/smp_lock.h>
21 #include <linux/notifier.h>
22 #include <linux/irqflags.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/ctype.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
39 #include <linux/gfp.h>
43 #include "trace_output.h"
45 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
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 * Kill all tracing for good (never come back).
84 * It is initialized to 1 but will turn to zero if the initialization
85 * of the tracer is successful. But that is the only place that sets
88 static int tracing_disabled
= 1;
90 DEFINE_PER_CPU(int, ftrace_cpu_disabled
);
92 static inline void ftrace_disable_cpu(void)
95 __this_cpu_inc(ftrace_cpu_disabled
);
98 static inline void ftrace_enable_cpu(void)
100 __this_cpu_dec(ftrace_cpu_disabled
);
104 static cpumask_var_t __read_mostly tracing_buffer_mask
;
106 #define for_each_tracing_cpu(cpu) \
107 for_each_cpu(cpu, 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 to true.
122 int ftrace_dump_on_oops
;
124 static int tracing_set_tracer(const char *buf
);
126 #define MAX_TRACER_SIZE 100
127 static char bootup_tracer_buf
[MAX_TRACER_SIZE
] __initdata
;
128 static char *default_bootup_tracer
;
130 static int __init
set_cmdline_ftrace(char *str
)
132 strncpy(bootup_tracer_buf
, str
, MAX_TRACER_SIZE
);
133 default_bootup_tracer
= bootup_tracer_buf
;
134 /* We are using ftrace early, expand it */
135 ring_buffer_expanded
= 1;
138 __setup("ftrace=", set_cmdline_ftrace
);
140 static int __init
set_ftrace_dump_on_oops(char *str
)
142 ftrace_dump_on_oops
= 1;
145 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
147 unsigned long long ns2usecs(cycle_t nsec
)
155 * The global_trace is the descriptor that holds the tracing
156 * buffers for the live tracing. For each CPU, it contains
157 * a link list of pages that will store trace entries. The
158 * page descriptor of the pages in the memory is used to hold
159 * the link list by linking the lru item in the page descriptor
160 * to each of the pages in the buffer per CPU.
162 * For each active CPU there is a data field that holds the
163 * pages for the buffer for that CPU. Each CPU has the same number
164 * of pages allocated for its buffer.
166 static struct trace_array global_trace
;
168 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
170 int filter_current_check_discard(struct ring_buffer
*buffer
,
171 struct ftrace_event_call
*call
, void *rec
,
172 struct ring_buffer_event
*event
)
174 return filter_check_discard(call
, rec
, buffer
, event
);
176 EXPORT_SYMBOL_GPL(filter_current_check_discard
);
178 cycle_t
ftrace_now(int cpu
)
182 /* Early boot up does not have a buffer yet */
183 if (!global_trace
.buffer
)
184 return trace_clock_local();
186 ts
= ring_buffer_time_stamp(global_trace
.buffer
, cpu
);
187 ring_buffer_normalize_time_stamp(global_trace
.buffer
, cpu
, &ts
);
193 * The max_tr is used to snapshot the global_trace when a maximum
194 * latency is reached. Some tracers will use this to store a maximum
195 * trace while it continues examining live traces.
197 * The buffers for the max_tr are set up the same as the global_trace.
198 * When a snapshot is taken, the link list of the max_tr is swapped
199 * with the link list of the global_trace and the buffers are reset for
200 * the global_trace so the tracing can continue.
202 static struct trace_array max_tr
;
204 static DEFINE_PER_CPU(struct trace_array_cpu
, max_tr_data
);
206 /* tracer_enabled is used to toggle activation of a tracer */
207 static int tracer_enabled
= 1;
210 * tracing_is_enabled - return tracer_enabled status
212 * This function is used by other tracers to know the status
213 * of the tracer_enabled flag. Tracers may use this function
214 * to know if it should enable their features when starting
215 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
217 int tracing_is_enabled(void)
219 return tracer_enabled
;
223 * trace_buf_size is the size in bytes that is allocated
224 * for a buffer. Note, the number of bytes is always rounded
227 * This number is purposely set to a low number of 16384.
228 * If the dump on oops happens, it will be much appreciated
229 * to not have to wait for all that output. Anyway this can be
230 * boot time and run time configurable.
232 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
234 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
236 /* trace_types holds a link list of available tracers. */
237 static struct tracer
*trace_types __read_mostly
;
239 /* current_trace points to the tracer that is currently active */
240 static struct tracer
*current_trace __read_mostly
;
243 * trace_types_lock is used to protect the trace_types list.
245 static DEFINE_MUTEX(trace_types_lock
);
248 * serialize the access of the ring buffer
250 * ring buffer serializes readers, but it is low level protection.
251 * The validity of the events (which returns by ring_buffer_peek() ..etc)
252 * are not protected by ring buffer.
254 * The content of events may become garbage if we allow other process consumes
255 * these events concurrently:
256 * A) the page of the consumed events may become a normal page
257 * (not reader page) in ring buffer, and this page will be rewrited
258 * by events producer.
259 * B) The page of the consumed events may become a page for splice_read,
260 * and this page will be returned to system.
262 * These primitives allow multi process access to different cpu ring buffer
265 * These primitives don't distinguish read-only and read-consume access.
266 * Multi read-only access are also serialized.
270 static DECLARE_RWSEM(all_cpu_access_lock
);
271 static DEFINE_PER_CPU(struct mutex
, cpu_access_lock
);
273 static inline void trace_access_lock(int cpu
)
275 if (cpu
== TRACE_PIPE_ALL_CPU
) {
276 /* gain it for accessing the whole ring buffer. */
277 down_write(&all_cpu_access_lock
);
279 /* gain it for accessing a cpu ring buffer. */
281 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
282 down_read(&all_cpu_access_lock
);
284 /* Secondly block other access to this @cpu ring buffer. */
285 mutex_lock(&per_cpu(cpu_access_lock
, cpu
));
289 static inline void trace_access_unlock(int cpu
)
291 if (cpu
== TRACE_PIPE_ALL_CPU
) {
292 up_write(&all_cpu_access_lock
);
294 mutex_unlock(&per_cpu(cpu_access_lock
, cpu
));
295 up_read(&all_cpu_access_lock
);
299 static inline void trace_access_lock_init(void)
303 for_each_possible_cpu(cpu
)
304 mutex_init(&per_cpu(cpu_access_lock
, cpu
));
309 static DEFINE_MUTEX(access_lock
);
311 static inline void trace_access_lock(int cpu
)
314 mutex_lock(&access_lock
);
317 static inline void trace_access_unlock(int cpu
)
320 mutex_unlock(&access_lock
);
323 static inline void trace_access_lock_init(void)
329 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
330 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
332 /* trace_flags holds trace_options default values */
333 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
334 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
| TRACE_ITER_SLEEP_TIME
|
335 TRACE_ITER_GRAPH_TIME
;
337 static int trace_stop_count
;
338 static DEFINE_SPINLOCK(tracing_start_lock
);
341 * trace_wake_up - wake up tasks waiting for trace input
343 * Simply wakes up any task that is blocked on the trace_wait
344 * queue. These is used with trace_poll for tasks polling the trace.
346 void trace_wake_up(void)
350 if (trace_flags
& TRACE_ITER_BLOCK
)
353 * The runqueue_is_locked() can fail, but this is the best we
357 if (!runqueue_is_locked(cpu
))
358 wake_up(&trace_wait
);
362 static int __init
set_buf_size(char *str
)
364 unsigned long buf_size
;
368 buf_size
= memparse(str
, &str
);
369 /* nr_entries can not be zero */
372 trace_buf_size
= buf_size
;
375 __setup("trace_buf_size=", set_buf_size
);
377 unsigned long nsecs_to_usecs(unsigned long nsecs
)
382 /* These must match the bit postions in trace_iterator_flags */
383 static const char *trace_options
[] = {
411 { trace_clock_local
, "local" },
412 { trace_clock_global
, "global" },
418 * trace_parser_get_init - gets the buffer for trace parser
420 int trace_parser_get_init(struct trace_parser
*parser
, int size
)
422 memset(parser
, 0, sizeof(*parser
));
424 parser
->buffer
= kmalloc(size
, GFP_KERNEL
);
433 * trace_parser_put - frees the buffer for trace parser
435 void trace_parser_put(struct trace_parser
*parser
)
437 kfree(parser
->buffer
);
441 * trace_get_user - reads the user input string separated by space
442 * (matched by isspace(ch))
444 * For each string found the 'struct trace_parser' is updated,
445 * and the function returns.
447 * Returns number of bytes read.
449 * See kernel/trace/trace.h for 'struct trace_parser' details.
451 int trace_get_user(struct trace_parser
*parser
, const char __user
*ubuf
,
452 size_t cnt
, loff_t
*ppos
)
459 trace_parser_clear(parser
);
461 ret
= get_user(ch
, ubuf
++);
469 * The parser is not finished with the last write,
470 * continue reading the user input without skipping spaces.
473 /* skip white space */
474 while (cnt
&& isspace(ch
)) {
475 ret
= get_user(ch
, ubuf
++);
482 /* only spaces were written */
492 /* read the non-space input */
493 while (cnt
&& !isspace(ch
)) {
494 if (parser
->idx
< parser
->size
- 1)
495 parser
->buffer
[parser
->idx
++] = ch
;
500 ret
= get_user(ch
, ubuf
++);
507 /* We either got finished input or we have to wait for another call. */
509 parser
->buffer
[parser
->idx
] = 0;
510 parser
->cont
= false;
513 parser
->buffer
[parser
->idx
++] = ch
;
523 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
531 if (s
->len
<= s
->readpos
)
534 len
= s
->len
- s
->readpos
;
537 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
547 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
552 if (s
->len
<= s
->readpos
)
555 len
= s
->len
- s
->readpos
;
558 ret
= memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
567 * ftrace_max_lock is used to protect the swapping of buffers
568 * when taking a max snapshot. The buffers themselves are
569 * protected by per_cpu spinlocks. But the action of the swap
570 * needs its own lock.
572 * This is defined as a arch_spinlock_t in order to help
573 * with performance when lockdep debugging is enabled.
575 * It is also used in other places outside the update_max_tr
576 * so it needs to be defined outside of the
577 * CONFIG_TRACER_MAX_TRACE.
579 static arch_spinlock_t ftrace_max_lock
=
580 (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
582 #ifdef CONFIG_TRACER_MAX_TRACE
583 unsigned long __read_mostly tracing_max_latency
;
584 unsigned long __read_mostly tracing_thresh
;
587 * Copy the new maximum trace into the separate maximum-trace
588 * structure. (this way the maximum trace is permanently saved,
589 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
592 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
594 struct trace_array_cpu
*data
= tr
->data
[cpu
];
595 struct trace_array_cpu
*max_data
= tr
->data
[cpu
];
598 max_tr
.time_start
= data
->preempt_timestamp
;
600 max_data
= max_tr
.data
[cpu
];
601 max_data
->saved_latency
= tracing_max_latency
;
602 max_data
->critical_start
= data
->critical_start
;
603 max_data
->critical_end
= data
->critical_end
;
605 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
606 max_data
->pid
= tsk
->pid
;
607 max_data
->uid
= task_uid(tsk
);
608 max_data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
609 max_data
->policy
= tsk
->policy
;
610 max_data
->rt_priority
= tsk
->rt_priority
;
612 /* record this tasks comm */
613 tracing_record_cmdline(tsk
);
617 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
619 * @tsk: the task with the latency
620 * @cpu: The cpu that initiated the trace.
622 * Flip the buffers between the @tr and the max_tr and record information
623 * about which task was the cause of this latency.
626 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
628 struct ring_buffer
*buf
= tr
->buffer
;
630 if (trace_stop_count
)
633 WARN_ON_ONCE(!irqs_disabled());
634 arch_spin_lock(&ftrace_max_lock
);
636 tr
->buffer
= max_tr
.buffer
;
639 __update_max_tr(tr
, tsk
, cpu
);
640 arch_spin_unlock(&ftrace_max_lock
);
644 * update_max_tr_single - only copy one trace over, and reset the rest
646 * @tsk - task with the latency
647 * @cpu - the cpu of the buffer to copy.
649 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
652 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
656 if (trace_stop_count
)
659 WARN_ON_ONCE(!irqs_disabled());
660 arch_spin_lock(&ftrace_max_lock
);
662 ftrace_disable_cpu();
664 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
668 * We failed to swap the buffer due to a commit taking
669 * place on this CPU. We fail to record, but we reset
670 * the max trace buffer (no one writes directly to it)
671 * and flag that it failed.
673 trace_array_printk(&max_tr
, _THIS_IP_
,
674 "Failed to swap buffers due to commit in progress\n");
679 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
&& ret
!= -EBUSY
);
681 __update_max_tr(tr
, tsk
, cpu
);
682 arch_spin_unlock(&ftrace_max_lock
);
684 #endif /* CONFIG_TRACER_MAX_TRACE */
687 * register_tracer - register a tracer with the ftrace system.
688 * @type - the plugin for the tracer
690 * Register a new plugin tracer.
692 int register_tracer(struct tracer
*type
)
693 __releases(kernel_lock
)
694 __acquires(kernel_lock
)
700 pr_info("Tracer must have a name\n");
704 if (strlen(type
->name
) > MAX_TRACER_SIZE
) {
705 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE
);
710 * When this gets called we hold the BKL which means that
711 * preemption is disabled. Various trace selftests however
712 * need to disable and enable preemption for successful tests.
713 * So we drop the BKL here and grab it after the tests again.
716 mutex_lock(&trace_types_lock
);
718 tracing_selftest_running
= true;
720 for (t
= trace_types
; t
; t
= t
->next
) {
721 if (strcmp(type
->name
, t
->name
) == 0) {
723 pr_info("Tracer %s already registered\n",
731 type
->set_flag
= &dummy_set_flag
;
733 type
->flags
= &dummy_tracer_flags
;
735 if (!type
->flags
->opts
)
736 type
->flags
->opts
= dummy_tracer_opt
;
737 if (!type
->wait_pipe
)
738 type
->wait_pipe
= default_wait_pipe
;
741 #ifdef CONFIG_FTRACE_STARTUP_TEST
742 if (type
->selftest
&& !tracing_selftest_disabled
) {
743 struct tracer
*saved_tracer
= current_trace
;
744 struct trace_array
*tr
= &global_trace
;
747 * Run a selftest on this tracer.
748 * Here we reset the trace buffer, and set the current
749 * tracer to be this tracer. The tracer can then run some
750 * internal tracing to verify that everything is in order.
751 * If we fail, we do not register this tracer.
753 tracing_reset_online_cpus(tr
);
755 current_trace
= type
;
756 /* the test is responsible for initializing and enabling */
757 pr_info("Testing tracer %s: ", type
->name
);
758 ret
= type
->selftest(type
, tr
);
759 /* the test is responsible for resetting too */
760 current_trace
= saved_tracer
;
762 printk(KERN_CONT
"FAILED!\n");
765 /* Only reset on passing, to avoid touching corrupted buffers */
766 tracing_reset_online_cpus(tr
);
768 printk(KERN_CONT
"PASSED\n");
772 type
->next
= trace_types
;
776 tracing_selftest_running
= false;
777 mutex_unlock(&trace_types_lock
);
779 if (ret
|| !default_bootup_tracer
)
782 if (strncmp(default_bootup_tracer
, type
->name
, MAX_TRACER_SIZE
))
785 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
786 /* Do we want this tracer to start on bootup? */
787 tracing_set_tracer(type
->name
);
788 default_bootup_tracer
= NULL
;
789 /* disable other selftests, since this will break it. */
790 tracing_selftest_disabled
= 1;
791 #ifdef CONFIG_FTRACE_STARTUP_TEST
792 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
801 void unregister_tracer(struct tracer
*type
)
805 mutex_lock(&trace_types_lock
);
806 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
810 pr_info("Tracer %s not registered\n", type
->name
);
816 if (type
== current_trace
&& tracer_enabled
) {
819 if (current_trace
->stop
)
820 current_trace
->stop(&global_trace
);
821 current_trace
= &nop_trace
;
824 mutex_unlock(&trace_types_lock
);
827 static void __tracing_reset(struct trace_array
*tr
, int cpu
)
829 ftrace_disable_cpu();
830 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
834 void tracing_reset(struct trace_array
*tr
, int cpu
)
836 struct ring_buffer
*buffer
= tr
->buffer
;
838 ring_buffer_record_disable(buffer
);
840 /* Make sure all commits have finished */
842 __tracing_reset(tr
, cpu
);
844 ring_buffer_record_enable(buffer
);
847 void tracing_reset_online_cpus(struct trace_array
*tr
)
849 struct ring_buffer
*buffer
= tr
->buffer
;
852 ring_buffer_record_disable(buffer
);
854 /* Make sure all commits have finished */
857 tr
->time_start
= ftrace_now(tr
->cpu
);
859 for_each_online_cpu(cpu
)
860 __tracing_reset(tr
, cpu
);
862 ring_buffer_record_enable(buffer
);
865 void tracing_reset_current(int cpu
)
867 tracing_reset(&global_trace
, cpu
);
870 void tracing_reset_current_online_cpus(void)
872 tracing_reset_online_cpus(&global_trace
);
875 #define SAVED_CMDLINES 128
876 #define NO_CMDLINE_MAP UINT_MAX
877 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
878 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
879 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
880 static int cmdline_idx
;
881 static arch_spinlock_t trace_cmdline_lock
= __ARCH_SPIN_LOCK_UNLOCKED
;
883 /* temporary disable recording */
884 static atomic_t trace_record_cmdline_disabled __read_mostly
;
886 static void trace_init_cmdlines(void)
888 memset(&map_pid_to_cmdline
, NO_CMDLINE_MAP
, sizeof(map_pid_to_cmdline
));
889 memset(&map_cmdline_to_pid
, NO_CMDLINE_MAP
, sizeof(map_cmdline_to_pid
));
893 int is_tracing_stopped(void)
895 return trace_stop_count
;
899 * ftrace_off_permanent - disable all ftrace code permanently
901 * This should only be called when a serious anomally has
902 * been detected. This will turn off the function tracing,
903 * ring buffers, and other tracing utilites. It takes no
904 * locks and can be called from any context.
906 void ftrace_off_permanent(void)
908 tracing_disabled
= 1;
910 tracing_off_permanent();
914 * tracing_start - quick start of the tracer
916 * If tracing is enabled but was stopped by tracing_stop,
917 * this will start the tracer back up.
919 void tracing_start(void)
921 struct ring_buffer
*buffer
;
924 if (tracing_disabled
)
927 spin_lock_irqsave(&tracing_start_lock
, flags
);
928 if (--trace_stop_count
) {
929 if (trace_stop_count
< 0) {
930 /* Someone screwed up their debugging */
932 trace_stop_count
= 0;
938 buffer
= global_trace
.buffer
;
940 ring_buffer_record_enable(buffer
);
942 buffer
= max_tr
.buffer
;
944 ring_buffer_record_enable(buffer
);
948 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
952 * tracing_stop - quick stop of the tracer
954 * Light weight way to stop tracing. Use in conjunction with
957 void tracing_stop(void)
959 struct ring_buffer
*buffer
;
963 spin_lock_irqsave(&tracing_start_lock
, flags
);
964 if (trace_stop_count
++)
967 buffer
= global_trace
.buffer
;
969 ring_buffer_record_disable(buffer
);
971 buffer
= max_tr
.buffer
;
973 ring_buffer_record_disable(buffer
);
976 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
979 void trace_stop_cmdline_recording(void);
981 static void trace_save_cmdline(struct task_struct
*tsk
)
985 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
989 * It's not the end of the world if we don't get
990 * the lock, but we also don't want to spin
991 * nor do we want to disable interrupts,
992 * so if we miss here, then better luck next time.
994 if (!arch_spin_trylock(&trace_cmdline_lock
))
997 idx
= map_pid_to_cmdline
[tsk
->pid
];
998 if (idx
== NO_CMDLINE_MAP
) {
999 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
1002 * Check whether the cmdline buffer at idx has a pid
1003 * mapped. We are going to overwrite that entry so we
1004 * need to clear the map_pid_to_cmdline. Otherwise we
1005 * would read the new comm for the old pid.
1007 pid
= map_cmdline_to_pid
[idx
];
1008 if (pid
!= NO_CMDLINE_MAP
)
1009 map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
1011 map_cmdline_to_pid
[idx
] = tsk
->pid
;
1012 map_pid_to_cmdline
[tsk
->pid
] = idx
;
1017 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
1019 arch_spin_unlock(&trace_cmdline_lock
);
1022 void trace_find_cmdline(int pid
, char comm
[])
1027 strcpy(comm
, "<idle>");
1031 if (WARN_ON_ONCE(pid
< 0)) {
1032 strcpy(comm
, "<XXX>");
1036 if (pid
> PID_MAX_DEFAULT
) {
1037 strcpy(comm
, "<...>");
1042 arch_spin_lock(&trace_cmdline_lock
);
1043 map
= map_pid_to_cmdline
[pid
];
1044 if (map
!= NO_CMDLINE_MAP
)
1045 strcpy(comm
, saved_cmdlines
[map
]);
1047 strcpy(comm
, "<...>");
1049 arch_spin_unlock(&trace_cmdline_lock
);
1053 void tracing_record_cmdline(struct task_struct
*tsk
)
1055 if (atomic_read(&trace_record_cmdline_disabled
) || !tracer_enabled
||
1059 trace_save_cmdline(tsk
);
1063 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
1066 struct task_struct
*tsk
= current
;
1068 entry
->preempt_count
= pc
& 0xff;
1069 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
1070 entry
->lock_depth
= (tsk
) ? tsk
->lock_depth
: 0;
1072 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1073 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
1075 TRACE_FLAG_IRQS_NOSUPPORT
|
1077 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
1078 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
1079 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
1081 EXPORT_SYMBOL_GPL(tracing_generic_entry_update
);
1083 struct ring_buffer_event
*
1084 trace_buffer_lock_reserve(struct ring_buffer
*buffer
,
1087 unsigned long flags
, int pc
)
1089 struct ring_buffer_event
*event
;
1091 event
= ring_buffer_lock_reserve(buffer
, len
);
1092 if (event
!= NULL
) {
1093 struct trace_entry
*ent
= ring_buffer_event_data(event
);
1095 tracing_generic_entry_update(ent
, flags
, pc
);
1103 __trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1104 struct ring_buffer_event
*event
,
1105 unsigned long flags
, int pc
,
1108 ring_buffer_unlock_commit(buffer
, event
);
1110 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1111 ftrace_trace_userstack(buffer
, flags
, pc
);
1117 void trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1118 struct ring_buffer_event
*event
,
1119 unsigned long flags
, int pc
)
1121 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
1124 struct ring_buffer_event
*
1125 trace_current_buffer_lock_reserve(struct ring_buffer
**current_rb
,
1126 int type
, unsigned long len
,
1127 unsigned long flags
, int pc
)
1129 *current_rb
= global_trace
.buffer
;
1130 return trace_buffer_lock_reserve(*current_rb
,
1131 type
, len
, flags
, pc
);
1133 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve
);
1135 void trace_current_buffer_unlock_commit(struct ring_buffer
*buffer
,
1136 struct ring_buffer_event
*event
,
1137 unsigned long flags
, int pc
)
1139 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
1141 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit
);
1143 void trace_nowake_buffer_unlock_commit(struct ring_buffer
*buffer
,
1144 struct ring_buffer_event
*event
,
1145 unsigned long flags
, int pc
)
1147 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 0);
1149 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit
);
1151 void trace_current_buffer_discard_commit(struct ring_buffer
*buffer
,
1152 struct ring_buffer_event
*event
)
1154 ring_buffer_discard_commit(buffer
, event
);
1156 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
1159 trace_function(struct trace_array
*tr
,
1160 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1163 struct ftrace_event_call
*call
= &event_function
;
1164 struct ring_buffer
*buffer
= tr
->buffer
;
1165 struct ring_buffer_event
*event
;
1166 struct ftrace_entry
*entry
;
1168 /* If we are reading the ring buffer, don't trace */
1169 if (unlikely(__this_cpu_read(ftrace_cpu_disabled
)))
1172 event
= trace_buffer_lock_reserve(buffer
, TRACE_FN
, sizeof(*entry
),
1176 entry
= ring_buffer_event_data(event
);
1178 entry
->parent_ip
= parent_ip
;
1180 if (!filter_check_discard(call
, entry
, buffer
, event
))
1181 ring_buffer_unlock_commit(buffer
, event
);
1185 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
1186 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1189 if (likely(!atomic_read(&data
->disabled
)))
1190 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
1193 #ifdef CONFIG_STACKTRACE
1194 static void __ftrace_trace_stack(struct ring_buffer
*buffer
,
1195 unsigned long flags
,
1198 struct ftrace_event_call
*call
= &event_kernel_stack
;
1199 struct ring_buffer_event
*event
;
1200 struct stack_entry
*entry
;
1201 struct stack_trace trace
;
1203 event
= trace_buffer_lock_reserve(buffer
, TRACE_STACK
,
1204 sizeof(*entry
), flags
, pc
);
1207 entry
= ring_buffer_event_data(event
);
1208 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1210 trace
.nr_entries
= 0;
1211 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1213 trace
.entries
= entry
->caller
;
1215 save_stack_trace(&trace
);
1216 if (!filter_check_discard(call
, entry
, buffer
, event
))
1217 ring_buffer_unlock_commit(buffer
, event
);
1220 void ftrace_trace_stack(struct ring_buffer
*buffer
, unsigned long flags
,
1223 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1226 __ftrace_trace_stack(buffer
, flags
, skip
, pc
);
1229 void __trace_stack(struct trace_array
*tr
, unsigned long flags
, int skip
,
1232 __ftrace_trace_stack(tr
->buffer
, flags
, skip
, pc
);
1236 * trace_dump_stack - record a stack back trace in the trace buffer
1238 void trace_dump_stack(void)
1240 unsigned long flags
;
1242 if (tracing_disabled
|| tracing_selftest_running
)
1245 local_save_flags(flags
);
1247 /* skipping 3 traces, seems to get us at the caller of this function */
1248 __ftrace_trace_stack(global_trace
.buffer
, flags
, 3, preempt_count());
1252 ftrace_trace_userstack(struct ring_buffer
*buffer
, unsigned long flags
, int pc
)
1254 struct ftrace_event_call
*call
= &event_user_stack
;
1255 struct ring_buffer_event
*event
;
1256 struct userstack_entry
*entry
;
1257 struct stack_trace trace
;
1259 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1262 event
= trace_buffer_lock_reserve(buffer
, TRACE_USER_STACK
,
1263 sizeof(*entry
), flags
, pc
);
1266 entry
= ring_buffer_event_data(event
);
1268 entry
->tgid
= current
->tgid
;
1269 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1271 trace
.nr_entries
= 0;
1272 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1274 trace
.entries
= entry
->caller
;
1276 save_stack_trace_user(&trace
);
1277 if (!filter_check_discard(call
, entry
, buffer
, event
))
1278 ring_buffer_unlock_commit(buffer
, event
);
1282 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1284 ftrace_trace_userstack(tr
, flags
, preempt_count());
1288 #endif /* CONFIG_STACKTRACE */
1291 ftrace_trace_special(void *__tr
,
1292 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1295 struct ftrace_event_call
*call
= &event_special
;
1296 struct ring_buffer_event
*event
;
1297 struct trace_array
*tr
= __tr
;
1298 struct ring_buffer
*buffer
= tr
->buffer
;
1299 struct special_entry
*entry
;
1301 event
= trace_buffer_lock_reserve(buffer
, TRACE_SPECIAL
,
1302 sizeof(*entry
), 0, pc
);
1305 entry
= ring_buffer_event_data(event
);
1310 if (!filter_check_discard(call
, entry
, buffer
, event
))
1311 trace_buffer_unlock_commit(buffer
, event
, 0, pc
);
1315 __trace_special(void *__tr
, void *__data
,
1316 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1318 ftrace_trace_special(__tr
, arg1
, arg2
, arg3
, preempt_count());
1322 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1324 struct trace_array
*tr
= &global_trace
;
1325 struct trace_array_cpu
*data
;
1326 unsigned long flags
;
1330 if (tracing_disabled
)
1333 pc
= preempt_count();
1334 local_irq_save(flags
);
1335 cpu
= raw_smp_processor_id();
1336 data
= tr
->data
[cpu
];
1338 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1339 ftrace_trace_special(tr
, arg1
, arg2
, arg3
, pc
);
1341 atomic_dec(&data
->disabled
);
1342 local_irq_restore(flags
);
1346 * trace_vbprintk - write binary msg to tracing buffer
1349 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1351 static arch_spinlock_t trace_buf_lock
=
1352 (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
1353 static u32 trace_buf
[TRACE_BUF_SIZE
];
1355 struct ftrace_event_call
*call
= &event_bprint
;
1356 struct ring_buffer_event
*event
;
1357 struct ring_buffer
*buffer
;
1358 struct trace_array
*tr
= &global_trace
;
1359 struct trace_array_cpu
*data
;
1360 struct bprint_entry
*entry
;
1361 unsigned long flags
;
1364 int cpu
, len
= 0, size
, pc
;
1366 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1369 /* Don't pollute graph traces with trace_vprintk internals */
1370 pause_graph_tracing();
1372 pc
= preempt_count();
1373 resched
= ftrace_preempt_disable();
1374 cpu
= raw_smp_processor_id();
1375 data
= tr
->data
[cpu
];
1377 disable
= atomic_inc_return(&data
->disabled
);
1378 if (unlikely(disable
!= 1))
1381 /* Lockdep uses trace_printk for lock tracing */
1382 local_irq_save(flags
);
1383 arch_spin_lock(&trace_buf_lock
);
1384 len
= vbin_printf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1386 if (len
> TRACE_BUF_SIZE
|| len
< 0)
1389 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1390 buffer
= tr
->buffer
;
1391 event
= trace_buffer_lock_reserve(buffer
, TRACE_BPRINT
, size
,
1395 entry
= ring_buffer_event_data(event
);
1399 memcpy(entry
->buf
, trace_buf
, sizeof(u32
) * len
);
1400 if (!filter_check_discard(call
, entry
, buffer
, event
)) {
1401 ring_buffer_unlock_commit(buffer
, event
);
1402 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1406 arch_spin_unlock(&trace_buf_lock
);
1407 local_irq_restore(flags
);
1410 atomic_dec_return(&data
->disabled
);
1411 ftrace_preempt_enable(resched
);
1412 unpause_graph_tracing();
1416 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1418 int trace_array_printk(struct trace_array
*tr
,
1419 unsigned long ip
, const char *fmt
, ...)
1424 if (!(trace_flags
& TRACE_ITER_PRINTK
))
1428 ret
= trace_array_vprintk(tr
, ip
, fmt
, ap
);
1433 int trace_array_vprintk(struct trace_array
*tr
,
1434 unsigned long ip
, const char *fmt
, va_list args
)
1436 static arch_spinlock_t trace_buf_lock
= __ARCH_SPIN_LOCK_UNLOCKED
;
1437 static char trace_buf
[TRACE_BUF_SIZE
];
1439 struct ftrace_event_call
*call
= &event_print
;
1440 struct ring_buffer_event
*event
;
1441 struct ring_buffer
*buffer
;
1442 struct trace_array_cpu
*data
;
1443 int cpu
, len
= 0, size
, pc
;
1444 struct print_entry
*entry
;
1445 unsigned long irq_flags
;
1448 if (tracing_disabled
|| tracing_selftest_running
)
1451 pc
= preempt_count();
1452 preempt_disable_notrace();
1453 cpu
= raw_smp_processor_id();
1454 data
= tr
->data
[cpu
];
1456 disable
= atomic_inc_return(&data
->disabled
);
1457 if (unlikely(disable
!= 1))
1460 pause_graph_tracing();
1461 raw_local_irq_save(irq_flags
);
1462 arch_spin_lock(&trace_buf_lock
);
1463 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1465 size
= sizeof(*entry
) + len
+ 1;
1466 buffer
= tr
->buffer
;
1467 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
1471 entry
= ring_buffer_event_data(event
);
1474 memcpy(&entry
->buf
, trace_buf
, len
);
1475 entry
->buf
[len
] = '\0';
1476 if (!filter_check_discard(call
, entry
, buffer
, event
)) {
1477 ring_buffer_unlock_commit(buffer
, event
);
1478 ftrace_trace_stack(buffer
, irq_flags
, 6, pc
);
1482 arch_spin_unlock(&trace_buf_lock
);
1483 raw_local_irq_restore(irq_flags
);
1484 unpause_graph_tracing();
1486 atomic_dec_return(&data
->disabled
);
1487 preempt_enable_notrace();
1492 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1494 return trace_array_vprintk(&global_trace
, ip
, fmt
, args
);
1496 EXPORT_SYMBOL_GPL(trace_vprintk
);
1498 enum trace_file_type
{
1499 TRACE_FILE_LAT_FMT
= 1,
1500 TRACE_FILE_ANNOTATE
= 2,
1503 static void trace_iterator_increment(struct trace_iterator
*iter
)
1505 /* Don't allow ftrace to trace into the ring buffers */
1506 ftrace_disable_cpu();
1509 if (iter
->buffer_iter
[iter
->cpu
])
1510 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1512 ftrace_enable_cpu();
1515 static struct trace_entry
*
1516 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1518 struct ring_buffer_event
*event
;
1519 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1521 /* Don't allow ftrace to trace into the ring buffers */
1522 ftrace_disable_cpu();
1525 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1527 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1529 ftrace_enable_cpu();
1531 return event
? ring_buffer_event_data(event
) : NULL
;
1534 static struct trace_entry
*
1535 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1537 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1538 struct trace_entry
*ent
, *next
= NULL
;
1539 int cpu_file
= iter
->cpu_file
;
1540 u64 next_ts
= 0, ts
;
1545 * If we are in a per_cpu trace file, don't bother by iterating over
1546 * all cpu and peek directly.
1548 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1549 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1551 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
);
1553 *ent_cpu
= cpu_file
;
1558 for_each_tracing_cpu(cpu
) {
1560 if (ring_buffer_empty_cpu(buffer
, cpu
))
1563 ent
= peek_next_entry(iter
, cpu
, &ts
);
1566 * Pick the entry with the smallest timestamp:
1568 if (ent
&& (!next
|| ts
< next_ts
)) {
1576 *ent_cpu
= next_cpu
;
1584 /* Find the next real entry, without updating the iterator itself */
1585 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1586 int *ent_cpu
, u64
*ent_ts
)
1588 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1591 /* Find the next real entry, and increment the iterator to the next entry */
1592 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1594 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1597 trace_iterator_increment(iter
);
1599 return iter
->ent
? iter
: NULL
;
1602 static void trace_consume(struct trace_iterator
*iter
)
1604 /* Don't allow ftrace to trace into the ring buffers */
1605 ftrace_disable_cpu();
1606 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1607 ftrace_enable_cpu();
1610 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1612 struct trace_iterator
*iter
= m
->private;
1616 WARN_ON_ONCE(iter
->leftover
);
1620 /* can't go backwards */
1625 ent
= find_next_entry_inc(iter
);
1629 while (ent
&& iter
->idx
< i
)
1630 ent
= find_next_entry_inc(iter
);
1637 static void tracing_iter_reset(struct trace_iterator
*iter
, int cpu
)
1639 struct trace_array
*tr
= iter
->tr
;
1640 struct ring_buffer_event
*event
;
1641 struct ring_buffer_iter
*buf_iter
;
1642 unsigned long entries
= 0;
1645 tr
->data
[cpu
]->skipped_entries
= 0;
1647 if (!iter
->buffer_iter
[cpu
])
1650 buf_iter
= iter
->buffer_iter
[cpu
];
1651 ring_buffer_iter_reset(buf_iter
);
1654 * We could have the case with the max latency tracers
1655 * that a reset never took place on a cpu. This is evident
1656 * by the timestamp being before the start of the buffer.
1658 while ((event
= ring_buffer_iter_peek(buf_iter
, &ts
))) {
1659 if (ts
>= iter
->tr
->time_start
)
1662 ring_buffer_read(buf_iter
, NULL
);
1665 tr
->data
[cpu
]->skipped_entries
= entries
;
1669 * The current tracer is copied to avoid a global locking
1672 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1674 struct trace_iterator
*iter
= m
->private;
1675 static struct tracer
*old_tracer
;
1676 int cpu_file
= iter
->cpu_file
;
1681 /* copy the tracer to avoid using a global lock all around */
1682 mutex_lock(&trace_types_lock
);
1683 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1684 old_tracer
= current_trace
;
1685 *iter
->trace
= *current_trace
;
1687 mutex_unlock(&trace_types_lock
);
1689 atomic_inc(&trace_record_cmdline_disabled
);
1691 if (*pos
!= iter
->pos
) {
1696 ftrace_disable_cpu();
1698 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1699 for_each_tracing_cpu(cpu
)
1700 tracing_iter_reset(iter
, cpu
);
1702 tracing_iter_reset(iter
, cpu_file
);
1704 ftrace_enable_cpu();
1706 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1711 * If we overflowed the seq_file before, then we want
1712 * to just reuse the trace_seq buffer again.
1718 p
= s_next(m
, p
, &l
);
1722 trace_event_read_lock();
1723 trace_access_lock(cpu_file
);
1727 static void s_stop(struct seq_file
*m
, void *p
)
1729 struct trace_iterator
*iter
= m
->private;
1731 atomic_dec(&trace_record_cmdline_disabled
);
1732 trace_access_unlock(iter
->cpu_file
);
1733 trace_event_read_unlock();
1736 static void print_lat_help_header(struct seq_file
*m
)
1738 seq_puts(m
, "# _------=> CPU# \n");
1739 seq_puts(m
, "# / _-----=> irqs-off \n");
1740 seq_puts(m
, "# | / _----=> need-resched \n");
1741 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1742 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1743 seq_puts(m
, "# |||| /_--=> lock-depth \n");
1744 seq_puts(m
, "# |||||/ delay \n");
1745 seq_puts(m
, "# cmd pid |||||| time | caller \n");
1746 seq_puts(m
, "# \\ / |||||| \\ | / \n");
1749 static void print_func_help_header(struct seq_file
*m
)
1751 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1752 seq_puts(m
, "# | | | | |\n");
1757 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1759 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1760 struct trace_array
*tr
= iter
->tr
;
1761 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1762 struct tracer
*type
= current_trace
;
1763 unsigned long entries
= 0;
1764 unsigned long total
= 0;
1765 unsigned long count
;
1766 const char *name
= "preemption";
1773 for_each_tracing_cpu(cpu
) {
1774 count
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
1776 * If this buffer has skipped entries, then we hold all
1777 * entries for the trace and we need to ignore the
1778 * ones before the time stamp.
1780 if (tr
->data
[cpu
]->skipped_entries
) {
1781 count
-= tr
->data
[cpu
]->skipped_entries
;
1782 /* total is the same as the entries */
1786 ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
1790 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
1792 seq_puts(m
, "# -----------------------------------"
1793 "---------------------------------\n");
1794 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1795 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1796 nsecs_to_usecs(data
->saved_latency
),
1800 #if defined(CONFIG_PREEMPT_NONE)
1802 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1804 #elif defined(CONFIG_PREEMPT)
1809 /* These are reserved for later use */
1812 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1816 seq_puts(m
, "# -----------------\n");
1817 seq_printf(m
, "# | task: %.16s-%d "
1818 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1819 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1820 data
->policy
, data
->rt_priority
);
1821 seq_puts(m
, "# -----------------\n");
1823 if (data
->critical_start
) {
1824 seq_puts(m
, "# => started at: ");
1825 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1826 trace_print_seq(m
, &iter
->seq
);
1827 seq_puts(m
, "\n# => ended at: ");
1828 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1829 trace_print_seq(m
, &iter
->seq
);
1830 seq_puts(m
, "\n#\n");
1836 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1838 struct trace_seq
*s
= &iter
->seq
;
1840 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1843 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1846 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1849 if (iter
->tr
->data
[iter
->cpu
]->skipped_entries
)
1852 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1854 /* Don't print started cpu buffer for the first entry of the trace */
1856 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
1860 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1862 struct trace_seq
*s
= &iter
->seq
;
1863 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1864 struct trace_entry
*entry
;
1865 struct trace_event
*event
;
1869 test_cpu_buff_start(iter
);
1871 event
= ftrace_find_event(entry
->type
);
1873 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1874 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1875 if (!trace_print_lat_context(iter
))
1878 if (!trace_print_context(iter
))
1884 return event
->trace(iter
, sym_flags
);
1886 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1889 return TRACE_TYPE_HANDLED
;
1891 return TRACE_TYPE_PARTIAL_LINE
;
1894 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1896 struct trace_seq
*s
= &iter
->seq
;
1897 struct trace_entry
*entry
;
1898 struct trace_event
*event
;
1902 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1903 if (!trace_seq_printf(s
, "%d %d %llu ",
1904 entry
->pid
, iter
->cpu
, iter
->ts
))
1908 event
= ftrace_find_event(entry
->type
);
1910 return event
->raw(iter
, 0);
1912 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1915 return TRACE_TYPE_HANDLED
;
1917 return TRACE_TYPE_PARTIAL_LINE
;
1920 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1922 struct trace_seq
*s
= &iter
->seq
;
1923 unsigned char newline
= '\n';
1924 struct trace_entry
*entry
;
1925 struct trace_event
*event
;
1929 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1930 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1931 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1932 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1935 event
= ftrace_find_event(entry
->type
);
1937 enum print_line_t ret
= event
->hex(iter
, 0);
1938 if (ret
!= TRACE_TYPE_HANDLED
)
1942 SEQ_PUT_FIELD_RET(s
, newline
);
1944 return TRACE_TYPE_HANDLED
;
1947 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1949 struct trace_seq
*s
= &iter
->seq
;
1950 struct trace_entry
*entry
;
1951 struct trace_event
*event
;
1955 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1956 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1957 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1958 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1961 event
= ftrace_find_event(entry
->type
);
1962 return event
? event
->binary(iter
, 0) : TRACE_TYPE_HANDLED
;
1965 static int trace_empty(struct trace_iterator
*iter
)
1969 /* If we are looking at one CPU buffer, only check that one */
1970 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
1971 cpu
= iter
->cpu_file
;
1972 if (iter
->buffer_iter
[cpu
]) {
1973 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1976 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1982 for_each_tracing_cpu(cpu
) {
1983 if (iter
->buffer_iter
[cpu
]) {
1984 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1987 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1995 /* Called with trace_event_read_lock() held. */
1996 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1998 enum print_line_t ret
;
2000 if (iter
->trace
&& iter
->trace
->print_line
) {
2001 ret
= iter
->trace
->print_line(iter
);
2002 if (ret
!= TRACE_TYPE_UNHANDLED
)
2006 if (iter
->ent
->type
== TRACE_BPRINT
&&
2007 trace_flags
& TRACE_ITER_PRINTK
&&
2008 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2009 return trace_print_bprintk_msg_only(iter
);
2011 if (iter
->ent
->type
== TRACE_PRINT
&&
2012 trace_flags
& TRACE_ITER_PRINTK
&&
2013 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2014 return trace_print_printk_msg_only(iter
);
2016 if (trace_flags
& TRACE_ITER_BIN
)
2017 return print_bin_fmt(iter
);
2019 if (trace_flags
& TRACE_ITER_HEX
)
2020 return print_hex_fmt(iter
);
2022 if (trace_flags
& TRACE_ITER_RAW
)
2023 return print_raw_fmt(iter
);
2025 return print_trace_fmt(iter
);
2028 static int s_show(struct seq_file
*m
, void *v
)
2030 struct trace_iterator
*iter
= v
;
2033 if (iter
->ent
== NULL
) {
2035 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
2038 if (iter
->trace
&& iter
->trace
->print_header
)
2039 iter
->trace
->print_header(m
);
2040 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
2041 /* print nothing if the buffers are empty */
2042 if (trace_empty(iter
))
2044 print_trace_header(m
, iter
);
2045 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2046 print_lat_help_header(m
);
2048 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2049 print_func_help_header(m
);
2051 } else if (iter
->leftover
) {
2053 * If we filled the seq_file buffer earlier, we
2054 * want to just show it now.
2056 ret
= trace_print_seq(m
, &iter
->seq
);
2058 /* ret should this time be zero, but you never know */
2059 iter
->leftover
= ret
;
2062 print_trace_line(iter
);
2063 ret
= trace_print_seq(m
, &iter
->seq
);
2065 * If we overflow the seq_file buffer, then it will
2066 * ask us for this data again at start up.
2068 * ret is 0 if seq_file write succeeded.
2071 iter
->leftover
= ret
;
2077 static const struct seq_operations tracer_seq_ops
= {
2084 static struct trace_iterator
*
2085 __tracing_open(struct inode
*inode
, struct file
*file
)
2087 long cpu_file
= (long) inode
->i_private
;
2088 void *fail_ret
= ERR_PTR(-ENOMEM
);
2089 struct trace_iterator
*iter
;
2093 if (tracing_disabled
)
2094 return ERR_PTR(-ENODEV
);
2096 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2098 return ERR_PTR(-ENOMEM
);
2101 * We make a copy of the current tracer to avoid concurrent
2102 * changes on it while we are reading.
2104 mutex_lock(&trace_types_lock
);
2105 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2110 *iter
->trace
= *current_trace
;
2112 if (!zalloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
2115 if (current_trace
&& current_trace
->print_max
)
2118 iter
->tr
= &global_trace
;
2120 mutex_init(&iter
->mutex
);
2121 iter
->cpu_file
= cpu_file
;
2123 /* Notify the tracer early; before we stop tracing. */
2124 if (iter
->trace
&& iter
->trace
->open
)
2125 iter
->trace
->open(iter
);
2127 /* Annotate start of buffers if we had overruns */
2128 if (ring_buffer_overruns(iter
->tr
->buffer
))
2129 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
2131 /* stop the trace while dumping */
2134 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
2135 for_each_tracing_cpu(cpu
) {
2137 iter
->buffer_iter
[cpu
] =
2138 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
2139 tracing_iter_reset(iter
, cpu
);
2142 cpu
= iter
->cpu_file
;
2143 iter
->buffer_iter
[cpu
] =
2144 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
2145 tracing_iter_reset(iter
, cpu
);
2148 ret
= seq_open(file
, &tracer_seq_ops
);
2150 fail_ret
= ERR_PTR(ret
);
2154 m
= file
->private_data
;
2157 mutex_unlock(&trace_types_lock
);
2162 for_each_tracing_cpu(cpu
) {
2163 if (iter
->buffer_iter
[cpu
])
2164 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2166 free_cpumask_var(iter
->started
);
2169 mutex_unlock(&trace_types_lock
);
2176 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
2178 if (tracing_disabled
)
2181 filp
->private_data
= inode
->i_private
;
2185 static int tracing_release(struct inode
*inode
, struct file
*file
)
2187 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2188 struct trace_iterator
*iter
;
2191 if (!(file
->f_mode
& FMODE_READ
))
2196 mutex_lock(&trace_types_lock
);
2197 for_each_tracing_cpu(cpu
) {
2198 if (iter
->buffer_iter
[cpu
])
2199 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2202 if (iter
->trace
&& iter
->trace
->close
)
2203 iter
->trace
->close(iter
);
2205 /* reenable tracing if it was previously enabled */
2207 mutex_unlock(&trace_types_lock
);
2209 seq_release(inode
, file
);
2210 mutex_destroy(&iter
->mutex
);
2211 free_cpumask_var(iter
->started
);
2217 static int tracing_open(struct inode
*inode
, struct file
*file
)
2219 struct trace_iterator
*iter
;
2222 /* If this file was open for write, then erase contents */
2223 if ((file
->f_mode
& FMODE_WRITE
) &&
2224 (file
->f_flags
& O_TRUNC
)) {
2225 long cpu
= (long) inode
->i_private
;
2227 if (cpu
== TRACE_PIPE_ALL_CPU
)
2228 tracing_reset_online_cpus(&global_trace
);
2230 tracing_reset(&global_trace
, cpu
);
2233 if (file
->f_mode
& FMODE_READ
) {
2234 iter
= __tracing_open(inode
, file
);
2236 ret
= PTR_ERR(iter
);
2237 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2238 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2244 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2246 struct tracer
*t
= v
;
2256 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2261 mutex_lock(&trace_types_lock
);
2262 for (t
= trace_types
; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2268 static void t_stop(struct seq_file
*m
, void *p
)
2270 mutex_unlock(&trace_types_lock
);
2273 static int t_show(struct seq_file
*m
, void *v
)
2275 struct tracer
*t
= v
;
2280 seq_printf(m
, "%s", t
->name
);
2289 static const struct seq_operations show_traces_seq_ops
= {
2296 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2298 if (tracing_disabled
)
2301 return seq_open(file
, &show_traces_seq_ops
);
2305 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
2306 size_t count
, loff_t
*ppos
)
2311 static const struct file_operations tracing_fops
= {
2312 .open
= tracing_open
,
2314 .write
= tracing_write_stub
,
2315 .llseek
= seq_lseek
,
2316 .release
= tracing_release
,
2319 static const struct file_operations show_traces_fops
= {
2320 .open
= show_traces_open
,
2322 .release
= seq_release
,
2326 * Only trace on a CPU if the bitmask is set:
2328 static cpumask_var_t tracing_cpumask
;
2331 * The tracer itself will not take this lock, but still we want
2332 * to provide a consistent cpumask to user-space:
2334 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2337 * Temporary storage for the character representation of the
2338 * CPU bitmask (and one more byte for the newline):
2340 static char mask_str
[NR_CPUS
+ 1];
2343 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2344 size_t count
, loff_t
*ppos
)
2348 mutex_lock(&tracing_cpumask_update_lock
);
2350 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2351 if (count
- len
< 2) {
2355 len
+= sprintf(mask_str
+ len
, "\n");
2356 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2359 mutex_unlock(&tracing_cpumask_update_lock
);
2365 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2366 size_t count
, loff_t
*ppos
)
2369 cpumask_var_t tracing_cpumask_new
;
2371 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2374 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2378 mutex_lock(&tracing_cpumask_update_lock
);
2380 local_irq_disable();
2381 arch_spin_lock(&ftrace_max_lock
);
2382 for_each_tracing_cpu(cpu
) {
2384 * Increase/decrease the disabled counter if we are
2385 * about to flip a bit in the cpumask:
2387 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2388 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2389 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2391 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2392 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2393 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2396 arch_spin_unlock(&ftrace_max_lock
);
2399 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2401 mutex_unlock(&tracing_cpumask_update_lock
);
2402 free_cpumask_var(tracing_cpumask_new
);
2407 free_cpumask_var(tracing_cpumask_new
);
2412 static const struct file_operations tracing_cpumask_fops
= {
2413 .open
= tracing_open_generic
,
2414 .read
= tracing_cpumask_read
,
2415 .write
= tracing_cpumask_write
,
2418 static int tracing_trace_options_show(struct seq_file
*m
, void *v
)
2420 struct tracer_opt
*trace_opts
;
2424 mutex_lock(&trace_types_lock
);
2425 tracer_flags
= current_trace
->flags
->val
;
2426 trace_opts
= current_trace
->flags
->opts
;
2428 for (i
= 0; trace_options
[i
]; i
++) {
2429 if (trace_flags
& (1 << i
))
2430 seq_printf(m
, "%s\n", trace_options
[i
]);
2432 seq_printf(m
, "no%s\n", trace_options
[i
]);
2435 for (i
= 0; trace_opts
[i
].name
; i
++) {
2436 if (tracer_flags
& trace_opts
[i
].bit
)
2437 seq_printf(m
, "%s\n", trace_opts
[i
].name
);
2439 seq_printf(m
, "no%s\n", trace_opts
[i
].name
);
2441 mutex_unlock(&trace_types_lock
);
2446 static int __set_tracer_option(struct tracer
*trace
,
2447 struct tracer_flags
*tracer_flags
,
2448 struct tracer_opt
*opts
, int neg
)
2452 ret
= trace
->set_flag(tracer_flags
->val
, opts
->bit
, !neg
);
2457 tracer_flags
->val
&= ~opts
->bit
;
2459 tracer_flags
->val
|= opts
->bit
;
2463 /* Try to assign a tracer specific option */
2464 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2466 struct tracer_flags
*tracer_flags
= trace
->flags
;
2467 struct tracer_opt
*opts
= NULL
;
2470 for (i
= 0; tracer_flags
->opts
[i
].name
; i
++) {
2471 opts
= &tracer_flags
->opts
[i
];
2473 if (strcmp(cmp
, opts
->name
) == 0)
2474 return __set_tracer_option(trace
, trace
->flags
,
2481 static void set_tracer_flags(unsigned int mask
, int enabled
)
2483 /* do nothing if flag is already set */
2484 if (!!(trace_flags
& mask
) == !!enabled
)
2488 trace_flags
|= mask
;
2490 trace_flags
&= ~mask
;
2494 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2495 size_t cnt
, loff_t
*ppos
)
2503 if (cnt
>= sizeof(buf
))
2506 if (copy_from_user(&buf
, ubuf
, cnt
))
2510 cmp
= strstrip(buf
);
2512 if (strncmp(cmp
, "no", 2) == 0) {
2517 for (i
= 0; trace_options
[i
]; i
++) {
2518 if (strcmp(cmp
, trace_options
[i
]) == 0) {
2519 set_tracer_flags(1 << i
, !neg
);
2524 /* If no option could be set, test the specific tracer options */
2525 if (!trace_options
[i
]) {
2526 mutex_lock(&trace_types_lock
);
2527 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2528 mutex_unlock(&trace_types_lock
);
2538 static int tracing_trace_options_open(struct inode
*inode
, struct file
*file
)
2540 if (tracing_disabled
)
2542 return single_open(file
, tracing_trace_options_show
, NULL
);
2545 static const struct file_operations tracing_iter_fops
= {
2546 .open
= tracing_trace_options_open
,
2548 .llseek
= seq_lseek
,
2549 .release
= single_release
,
2550 .write
= tracing_trace_options_write
,
2553 static const char readme_msg
[] =
2554 "tracing mini-HOWTO:\n\n"
2555 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2556 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2557 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2558 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2560 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2561 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2563 "# cat /sys/kernel/debug/tracing/trace_options\n"
2564 "noprint-parent nosym-offset nosym-addr noverbose\n"
2565 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2566 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2567 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2568 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2572 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2573 size_t cnt
, loff_t
*ppos
)
2575 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2576 readme_msg
, strlen(readme_msg
));
2579 static const struct file_operations tracing_readme_fops
= {
2580 .open
= tracing_open_generic
,
2581 .read
= tracing_readme_read
,
2585 tracing_saved_cmdlines_read(struct file
*file
, char __user
*ubuf
,
2586 size_t cnt
, loff_t
*ppos
)
2595 file_buf
= kmalloc(SAVED_CMDLINES
*(16+TASK_COMM_LEN
), GFP_KERNEL
);
2599 buf_comm
= kmalloc(TASK_COMM_LEN
, GFP_KERNEL
);
2607 for (i
= 0; i
< SAVED_CMDLINES
; i
++) {
2610 pid
= map_cmdline_to_pid
[i
];
2611 if (pid
== -1 || pid
== NO_CMDLINE_MAP
)
2614 trace_find_cmdline(pid
, buf_comm
);
2615 r
= sprintf(buf
, "%d %s\n", pid
, buf_comm
);
2620 len
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
2629 static const struct file_operations tracing_saved_cmdlines_fops
= {
2630 .open
= tracing_open_generic
,
2631 .read
= tracing_saved_cmdlines_read
,
2635 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2636 size_t cnt
, loff_t
*ppos
)
2641 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2642 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2646 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2647 size_t cnt
, loff_t
*ppos
)
2649 struct trace_array
*tr
= filp
->private_data
;
2654 if (cnt
>= sizeof(buf
))
2657 if (copy_from_user(&buf
, ubuf
, cnt
))
2662 ret
= strict_strtoul(buf
, 10, &val
);
2668 mutex_lock(&trace_types_lock
);
2669 if (tracer_enabled
^ val
) {
2672 if (current_trace
->start
)
2673 current_trace
->start(tr
);
2678 if (current_trace
->stop
)
2679 current_trace
->stop(tr
);
2682 mutex_unlock(&trace_types_lock
);
2690 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2691 size_t cnt
, loff_t
*ppos
)
2693 char buf
[MAX_TRACER_SIZE
+2];
2696 mutex_lock(&trace_types_lock
);
2698 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2700 r
= sprintf(buf
, "\n");
2701 mutex_unlock(&trace_types_lock
);
2703 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2706 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2708 tracing_reset_online_cpus(tr
);
2712 static int tracing_resize_ring_buffer(unsigned long size
)
2717 * If kernel or user changes the size of the ring buffer
2718 * we use the size that was given, and we can forget about
2719 * expanding it later.
2721 ring_buffer_expanded
= 1;
2723 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2727 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2731 r
= ring_buffer_resize(global_trace
.buffer
,
2732 global_trace
.entries
);
2735 * AARGH! We are left with different
2736 * size max buffer!!!!
2737 * The max buffer is our "snapshot" buffer.
2738 * When a tracer needs a snapshot (one of the
2739 * latency tracers), it swaps the max buffer
2740 * with the saved snap shot. We succeeded to
2741 * update the size of the main buffer, but failed to
2742 * update the size of the max buffer. But when we tried
2743 * to reset the main buffer to the original size, we
2744 * failed there too. This is very unlikely to
2745 * happen, but if it does, warn and kill all
2749 tracing_disabled
= 1;
2754 global_trace
.entries
= size
;
2760 * tracing_update_buffers - used by tracing facility to expand ring buffers
2762 * To save on memory when the tracing is never used on a system with it
2763 * configured in. The ring buffers are set to a minimum size. But once
2764 * a user starts to use the tracing facility, then they need to grow
2765 * to their default size.
2767 * This function is to be called when a tracer is about to be used.
2769 int tracing_update_buffers(void)
2773 mutex_lock(&trace_types_lock
);
2774 if (!ring_buffer_expanded
)
2775 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2776 mutex_unlock(&trace_types_lock
);
2781 struct trace_option_dentry
;
2783 static struct trace_option_dentry
*
2784 create_trace_option_files(struct tracer
*tracer
);
2787 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2789 static int tracing_set_tracer(const char *buf
)
2791 static struct trace_option_dentry
*topts
;
2792 struct trace_array
*tr
= &global_trace
;
2796 mutex_lock(&trace_types_lock
);
2798 if (!ring_buffer_expanded
) {
2799 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2805 for (t
= trace_types
; t
; t
= t
->next
) {
2806 if (strcmp(t
->name
, buf
) == 0)
2813 if (t
== current_trace
)
2816 trace_branch_disable();
2817 if (current_trace
&& current_trace
->reset
)
2818 current_trace
->reset(tr
);
2820 destroy_trace_option_files(topts
);
2824 topts
= create_trace_option_files(current_trace
);
2827 ret
= tracer_init(t
, tr
);
2832 trace_branch_enable(tr
);
2834 mutex_unlock(&trace_types_lock
);
2840 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2841 size_t cnt
, loff_t
*ppos
)
2843 char buf
[MAX_TRACER_SIZE
+1];
2850 if (cnt
> MAX_TRACER_SIZE
)
2851 cnt
= MAX_TRACER_SIZE
;
2853 if (copy_from_user(&buf
, ubuf
, cnt
))
2858 /* strip ending whitespace. */
2859 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2862 err
= tracing_set_tracer(buf
);
2872 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2873 size_t cnt
, loff_t
*ppos
)
2875 unsigned long *ptr
= filp
->private_data
;
2879 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2880 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2881 if (r
> sizeof(buf
))
2883 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2887 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2888 size_t cnt
, loff_t
*ppos
)
2890 unsigned long *ptr
= filp
->private_data
;
2895 if (cnt
>= sizeof(buf
))
2898 if (copy_from_user(&buf
, ubuf
, cnt
))
2903 ret
= strict_strtoul(buf
, 10, &val
);
2912 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2914 long cpu_file
= (long) inode
->i_private
;
2915 struct trace_iterator
*iter
;
2918 if (tracing_disabled
)
2921 mutex_lock(&trace_types_lock
);
2923 /* create a buffer to store the information to pass to userspace */
2924 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2931 * We make a copy of the current tracer to avoid concurrent
2932 * changes on it while we are reading.
2934 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2940 *iter
->trace
= *current_trace
;
2942 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2947 /* trace pipe does not show start of buffer */
2948 cpumask_setall(iter
->started
);
2950 if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2951 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2953 iter
->cpu_file
= cpu_file
;
2954 iter
->tr
= &global_trace
;
2955 mutex_init(&iter
->mutex
);
2956 filp
->private_data
= iter
;
2958 if (iter
->trace
->pipe_open
)
2959 iter
->trace
->pipe_open(iter
);
2962 mutex_unlock(&trace_types_lock
);
2968 mutex_unlock(&trace_types_lock
);
2972 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2974 struct trace_iterator
*iter
= file
->private_data
;
2976 mutex_lock(&trace_types_lock
);
2978 if (iter
->trace
->pipe_close
)
2979 iter
->trace
->pipe_close(iter
);
2981 mutex_unlock(&trace_types_lock
);
2983 free_cpumask_var(iter
->started
);
2984 mutex_destroy(&iter
->mutex
);
2992 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2994 struct trace_iterator
*iter
= filp
->private_data
;
2996 if (trace_flags
& TRACE_ITER_BLOCK
) {
2998 * Always select as readable when in blocking mode
3000 return POLLIN
| POLLRDNORM
;
3002 if (!trace_empty(iter
))
3003 return POLLIN
| POLLRDNORM
;
3004 poll_wait(filp
, &trace_wait
, poll_table
);
3005 if (!trace_empty(iter
))
3006 return POLLIN
| POLLRDNORM
;
3013 void default_wait_pipe(struct trace_iterator
*iter
)
3017 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
3019 if (trace_empty(iter
))
3022 finish_wait(&trace_wait
, &wait
);
3026 * This is a make-shift waitqueue.
3027 * A tracer might use this callback on some rare cases:
3029 * 1) the current tracer might hold the runqueue lock when it wakes up
3030 * a reader, hence a deadlock (sched, function, and function graph tracers)
3031 * 2) the function tracers, trace all functions, we don't want
3032 * the overhead of calling wake_up and friends
3033 * (and tracing them too)
3035 * Anyway, this is really very primitive wakeup.
3037 void poll_wait_pipe(struct trace_iterator
*iter
)
3039 set_current_state(TASK_INTERRUPTIBLE
);
3040 /* sleep for 100 msecs, and try again. */
3041 schedule_timeout(HZ
/ 10);
3044 /* Must be called with trace_types_lock mutex held. */
3045 static int tracing_wait_pipe(struct file
*filp
)
3047 struct trace_iterator
*iter
= filp
->private_data
;
3049 while (trace_empty(iter
)) {
3051 if ((filp
->f_flags
& O_NONBLOCK
)) {
3055 mutex_unlock(&iter
->mutex
);
3057 iter
->trace
->wait_pipe(iter
);
3059 mutex_lock(&iter
->mutex
);
3061 if (signal_pending(current
))
3065 * We block until we read something and tracing is disabled.
3066 * We still block if tracing is disabled, but we have never
3067 * read anything. This allows a user to cat this file, and
3068 * then enable tracing. But after we have read something,
3069 * we give an EOF when tracing is again disabled.
3071 * iter->pos will be 0 if we haven't read anything.
3073 if (!tracer_enabled
&& iter
->pos
)
3084 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
3085 size_t cnt
, loff_t
*ppos
)
3087 struct trace_iterator
*iter
= filp
->private_data
;
3088 static struct tracer
*old_tracer
;
3091 /* return any leftover data */
3092 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3096 trace_seq_init(&iter
->seq
);
3098 /* copy the tracer to avoid using a global lock all around */
3099 mutex_lock(&trace_types_lock
);
3100 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3101 old_tracer
= current_trace
;
3102 *iter
->trace
= *current_trace
;
3104 mutex_unlock(&trace_types_lock
);
3107 * Avoid more than one consumer on a single file descriptor
3108 * This is just a matter of traces coherency, the ring buffer itself
3111 mutex_lock(&iter
->mutex
);
3112 if (iter
->trace
->read
) {
3113 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
3119 sret
= tracing_wait_pipe(filp
);
3123 /* stop when tracing is finished */
3124 if (trace_empty(iter
)) {
3129 if (cnt
>= PAGE_SIZE
)
3130 cnt
= PAGE_SIZE
- 1;
3132 /* reset all but tr, trace, and overruns */
3133 memset(&iter
->seq
, 0,
3134 sizeof(struct trace_iterator
) -
3135 offsetof(struct trace_iterator
, seq
));
3138 trace_event_read_lock();
3139 trace_access_lock(iter
->cpu_file
);
3140 while (find_next_entry_inc(iter
) != NULL
) {
3141 enum print_line_t ret
;
3142 int len
= iter
->seq
.len
;
3144 ret
= print_trace_line(iter
);
3145 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3146 /* don't print partial lines */
3147 iter
->seq
.len
= len
;
3150 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3151 trace_consume(iter
);
3153 if (iter
->seq
.len
>= cnt
)
3156 trace_access_unlock(iter
->cpu_file
);
3157 trace_event_read_unlock();
3159 /* Now copy what we have to the user */
3160 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3161 if (iter
->seq
.readpos
>= iter
->seq
.len
)
3162 trace_seq_init(&iter
->seq
);
3165 * If there was nothing to send to user, inspite of consuming trace
3166 * entries, go back to wait for more entries.
3172 mutex_unlock(&iter
->mutex
);
3177 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
3178 struct pipe_buffer
*buf
)
3180 __free_page(buf
->page
);
3183 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
3186 __free_page(spd
->pages
[idx
]);
3189 static const struct pipe_buf_operations tracing_pipe_buf_ops
= {
3191 .map
= generic_pipe_buf_map
,
3192 .unmap
= generic_pipe_buf_unmap
,
3193 .confirm
= generic_pipe_buf_confirm
,
3194 .release
= tracing_pipe_buf_release
,
3195 .steal
= generic_pipe_buf_steal
,
3196 .get
= generic_pipe_buf_get
,
3200 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
3205 /* Seq buffer is page-sized, exactly what we need. */
3207 count
= iter
->seq
.len
;
3208 ret
= print_trace_line(iter
);
3209 count
= iter
->seq
.len
- count
;
3212 iter
->seq
.len
-= count
;
3215 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3216 iter
->seq
.len
-= count
;
3220 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3221 trace_consume(iter
);
3223 if (!find_next_entry_inc(iter
)) {
3233 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3235 struct pipe_inode_info
*pipe
,
3239 struct page
*pages
[PIPE_BUFFERS
];
3240 struct partial_page partial
[PIPE_BUFFERS
];
3241 struct trace_iterator
*iter
= filp
->private_data
;
3242 struct splice_pipe_desc spd
= {
3245 .nr_pages
= 0, /* This gets updated below. */
3247 .ops
= &tracing_pipe_buf_ops
,
3248 .spd_release
= tracing_spd_release_pipe
,
3250 static struct tracer
*old_tracer
;
3255 /* copy the tracer to avoid using a global lock all around */
3256 mutex_lock(&trace_types_lock
);
3257 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3258 old_tracer
= current_trace
;
3259 *iter
->trace
= *current_trace
;
3261 mutex_unlock(&trace_types_lock
);
3263 mutex_lock(&iter
->mutex
);
3265 if (iter
->trace
->splice_read
) {
3266 ret
= iter
->trace
->splice_read(iter
, filp
,
3267 ppos
, pipe
, len
, flags
);
3272 ret
= tracing_wait_pipe(filp
);
3276 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
3281 trace_event_read_lock();
3282 trace_access_lock(iter
->cpu_file
);
3284 /* Fill as many pages as possible. */
3285 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
3286 pages
[i
] = alloc_page(GFP_KERNEL
);
3290 rem
= tracing_fill_pipe_page(rem
, iter
);
3292 /* Copy the data into the page, so we can start over. */
3293 ret
= trace_seq_to_buffer(&iter
->seq
,
3294 page_address(pages
[i
]),
3297 __free_page(pages
[i
]);
3300 partial
[i
].offset
= 0;
3301 partial
[i
].len
= iter
->seq
.len
;
3303 trace_seq_init(&iter
->seq
);
3306 trace_access_unlock(iter
->cpu_file
);
3307 trace_event_read_unlock();
3308 mutex_unlock(&iter
->mutex
);
3312 return splice_to_pipe(pipe
, &spd
);
3315 mutex_unlock(&iter
->mutex
);
3321 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3322 size_t cnt
, loff_t
*ppos
)
3324 struct trace_array
*tr
= filp
->private_data
;
3328 mutex_lock(&trace_types_lock
);
3329 if (!ring_buffer_expanded
)
3330 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3332 trace_buf_size
>> 10);
3334 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3335 mutex_unlock(&trace_types_lock
);
3337 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3341 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3342 size_t cnt
, loff_t
*ppos
)
3348 if (cnt
>= sizeof(buf
))
3351 if (copy_from_user(&buf
, ubuf
, cnt
))
3356 ret
= strict_strtoul(buf
, 10, &val
);
3360 /* must have at least 1 entry */
3364 mutex_lock(&trace_types_lock
);
3368 /* disable all cpu buffers */
3369 for_each_tracing_cpu(cpu
) {
3370 if (global_trace
.data
[cpu
])
3371 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3372 if (max_tr
.data
[cpu
])
3373 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3376 /* value is in KB */
3379 if (val
!= global_trace
.entries
) {
3380 ret
= tracing_resize_ring_buffer(val
);
3389 /* If check pages failed, return ENOMEM */
3390 if (tracing_disabled
)
3393 for_each_tracing_cpu(cpu
) {
3394 if (global_trace
.data
[cpu
])
3395 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3396 if (max_tr
.data
[cpu
])
3397 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3401 max_tr
.entries
= global_trace
.entries
;
3402 mutex_unlock(&trace_types_lock
);
3407 static int mark_printk(const char *fmt
, ...)
3411 va_start(args
, fmt
);
3412 ret
= trace_vprintk(0, fmt
, args
);
3418 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3419 size_t cnt
, loff_t
*fpos
)
3423 if (tracing_disabled
)
3426 if (cnt
> TRACE_BUF_SIZE
)
3427 cnt
= TRACE_BUF_SIZE
;
3429 buf
= kmalloc(cnt
+ 2, GFP_KERNEL
);
3433 if (copy_from_user(buf
, ubuf
, cnt
)) {
3437 if (buf
[cnt
-1] != '\n') {
3443 cnt
= mark_printk("%s", buf
);
3450 static int tracing_clock_show(struct seq_file
*m
, void *v
)
3454 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++)
3456 "%s%s%s%s", i
? " " : "",
3457 i
== trace_clock_id
? "[" : "", trace_clocks
[i
].name
,
3458 i
== trace_clock_id
? "]" : "");
3464 static ssize_t
tracing_clock_write(struct file
*filp
, const char __user
*ubuf
,
3465 size_t cnt
, loff_t
*fpos
)
3468 const char *clockstr
;
3471 if (cnt
>= sizeof(buf
))
3474 if (copy_from_user(&buf
, ubuf
, cnt
))
3479 clockstr
= strstrip(buf
);
3481 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++) {
3482 if (strcmp(trace_clocks
[i
].name
, clockstr
) == 0)
3485 if (i
== ARRAY_SIZE(trace_clocks
))
3490 mutex_lock(&trace_types_lock
);
3492 ring_buffer_set_clock(global_trace
.buffer
, trace_clocks
[i
].func
);
3494 ring_buffer_set_clock(max_tr
.buffer
, trace_clocks
[i
].func
);
3496 mutex_unlock(&trace_types_lock
);
3503 static int tracing_clock_open(struct inode
*inode
, struct file
*file
)
3505 if (tracing_disabled
)
3507 return single_open(file
, tracing_clock_show
, NULL
);
3510 static const struct file_operations tracing_max_lat_fops
= {
3511 .open
= tracing_open_generic
,
3512 .read
= tracing_max_lat_read
,
3513 .write
= tracing_max_lat_write
,
3516 static const struct file_operations tracing_ctrl_fops
= {
3517 .open
= tracing_open_generic
,
3518 .read
= tracing_ctrl_read
,
3519 .write
= tracing_ctrl_write
,
3522 static const struct file_operations set_tracer_fops
= {
3523 .open
= tracing_open_generic
,
3524 .read
= tracing_set_trace_read
,
3525 .write
= tracing_set_trace_write
,
3528 static const struct file_operations tracing_pipe_fops
= {
3529 .open
= tracing_open_pipe
,
3530 .poll
= tracing_poll_pipe
,
3531 .read
= tracing_read_pipe
,
3532 .splice_read
= tracing_splice_read_pipe
,
3533 .release
= tracing_release_pipe
,
3536 static const struct file_operations tracing_entries_fops
= {
3537 .open
= tracing_open_generic
,
3538 .read
= tracing_entries_read
,
3539 .write
= tracing_entries_write
,
3542 static const struct file_operations tracing_mark_fops
= {
3543 .open
= tracing_open_generic
,
3544 .write
= tracing_mark_write
,
3547 static const struct file_operations trace_clock_fops
= {
3548 .open
= tracing_clock_open
,
3550 .llseek
= seq_lseek
,
3551 .release
= single_release
,
3552 .write
= tracing_clock_write
,
3555 struct ftrace_buffer_info
{
3556 struct trace_array
*tr
;
3562 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3564 int cpu
= (int)(long)inode
->i_private
;
3565 struct ftrace_buffer_info
*info
;
3567 if (tracing_disabled
)
3570 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3574 info
->tr
= &global_trace
;
3577 /* Force reading ring buffer for first read */
3578 info
->read
= (unsigned int)-1;
3580 filp
->private_data
= info
;
3582 return nonseekable_open(inode
, filp
);
3586 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3587 size_t count
, loff_t
*ppos
)
3589 struct ftrace_buffer_info
*info
= filp
->private_data
;
3598 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3602 /* Do we have previous read data to read? */
3603 if (info
->read
< PAGE_SIZE
)
3608 trace_access_lock(info
->cpu
);
3609 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3613 trace_access_unlock(info
->cpu
);
3617 pos
= ring_buffer_page_len(info
->spare
);
3619 if (pos
< PAGE_SIZE
)
3620 memset(info
->spare
+ pos
, 0, PAGE_SIZE
- pos
);
3623 size
= PAGE_SIZE
- info
->read
;
3627 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3638 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3640 struct ftrace_buffer_info
*info
= file
->private_data
;
3643 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3650 struct ring_buffer
*buffer
;
3655 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3656 struct pipe_buffer
*buf
)
3658 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3663 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3668 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3669 struct pipe_buffer
*buf
)
3674 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3675 struct pipe_buffer
*buf
)
3677 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3682 /* Pipe buffer operations for a buffer. */
3683 static const struct pipe_buf_operations buffer_pipe_buf_ops
= {
3685 .map
= generic_pipe_buf_map
,
3686 .unmap
= generic_pipe_buf_unmap
,
3687 .confirm
= generic_pipe_buf_confirm
,
3688 .release
= buffer_pipe_buf_release
,
3689 .steal
= buffer_pipe_buf_steal
,
3690 .get
= buffer_pipe_buf_get
,
3694 * Callback from splice_to_pipe(), if we need to release some pages
3695 * at the end of the spd in case we error'ed out in filling the pipe.
3697 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3699 struct buffer_ref
*ref
=
3700 (struct buffer_ref
*)spd
->partial
[i
].private;
3705 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3707 spd
->partial
[i
].private = 0;
3711 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3712 struct pipe_inode_info
*pipe
, size_t len
,
3715 struct ftrace_buffer_info
*info
= file
->private_data
;
3716 struct partial_page partial
[PIPE_BUFFERS
];
3717 struct page
*pages
[PIPE_BUFFERS
];
3718 struct splice_pipe_desc spd
= {
3722 .ops
= &buffer_pipe_buf_ops
,
3723 .spd_release
= buffer_spd_release
,
3725 struct buffer_ref
*ref
;
3726 int entries
, size
, i
;
3729 if (*ppos
& (PAGE_SIZE
- 1)) {
3730 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3734 if (len
& (PAGE_SIZE
- 1)) {
3735 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3736 if (len
< PAGE_SIZE
)
3741 trace_access_lock(info
->cpu
);
3742 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3744 for (i
= 0; i
< PIPE_BUFFERS
&& len
&& entries
; i
++, len
-= PAGE_SIZE
) {
3748 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3753 ref
->buffer
= info
->tr
->buffer
;
3754 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3760 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3763 ring_buffer_free_read_page(ref
->buffer
,
3770 * zero out any left over data, this is going to
3773 size
= ring_buffer_page_len(ref
->page
);
3774 if (size
< PAGE_SIZE
)
3775 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3777 page
= virt_to_page(ref
->page
);
3779 spd
.pages
[i
] = page
;
3780 spd
.partial
[i
].len
= PAGE_SIZE
;
3781 spd
.partial
[i
].offset
= 0;
3782 spd
.partial
[i
].private = (unsigned long)ref
;
3786 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3789 trace_access_unlock(info
->cpu
);
3792 /* did we read anything? */
3793 if (!spd
.nr_pages
) {
3794 if (flags
& SPLICE_F_NONBLOCK
)
3802 ret
= splice_to_pipe(pipe
, &spd
);
3807 static const struct file_operations tracing_buffers_fops
= {
3808 .open
= tracing_buffers_open
,
3809 .read
= tracing_buffers_read
,
3810 .release
= tracing_buffers_release
,
3811 .splice_read
= tracing_buffers_splice_read
,
3812 .llseek
= no_llseek
,
3816 tracing_stats_read(struct file
*filp
, char __user
*ubuf
,
3817 size_t count
, loff_t
*ppos
)
3819 unsigned long cpu
= (unsigned long)filp
->private_data
;
3820 struct trace_array
*tr
= &global_trace
;
3821 struct trace_seq
*s
;
3824 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
3830 cnt
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
3831 trace_seq_printf(s
, "entries: %ld\n", cnt
);
3833 cnt
= ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
3834 trace_seq_printf(s
, "overrun: %ld\n", cnt
);
3836 cnt
= ring_buffer_commit_overrun_cpu(tr
->buffer
, cpu
);
3837 trace_seq_printf(s
, "commit overrun: %ld\n", cnt
);
3839 count
= simple_read_from_buffer(ubuf
, count
, ppos
, s
->buffer
, s
->len
);
3846 static const struct file_operations tracing_stats_fops
= {
3847 .open
= tracing_open_generic
,
3848 .read
= tracing_stats_read
,
3851 #ifdef CONFIG_DYNAMIC_FTRACE
3853 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3859 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3860 size_t cnt
, loff_t
*ppos
)
3862 static char ftrace_dyn_info_buffer
[1024];
3863 static DEFINE_MUTEX(dyn_info_mutex
);
3864 unsigned long *p
= filp
->private_data
;
3865 char *buf
= ftrace_dyn_info_buffer
;
3866 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3869 mutex_lock(&dyn_info_mutex
);
3870 r
= sprintf(buf
, "%ld ", *p
);
3872 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3875 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3877 mutex_unlock(&dyn_info_mutex
);
3882 static const struct file_operations tracing_dyn_info_fops
= {
3883 .open
= tracing_open_generic
,
3884 .read
= tracing_read_dyn_info
,
3888 static struct dentry
*d_tracer
;
3890 struct dentry
*tracing_init_dentry(void)
3897 if (!debugfs_initialized())
3900 d_tracer
= debugfs_create_dir("tracing", NULL
);
3902 if (!d_tracer
&& !once
) {
3904 pr_warning("Could not create debugfs directory 'tracing'\n");
3911 static struct dentry
*d_percpu
;
3913 struct dentry
*tracing_dentry_percpu(void)
3916 struct dentry
*d_tracer
;
3921 d_tracer
= tracing_init_dentry();
3926 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
3928 if (!d_percpu
&& !once
) {
3930 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3937 static void tracing_init_debugfs_percpu(long cpu
)
3939 struct dentry
*d_percpu
= tracing_dentry_percpu();
3940 struct dentry
*d_cpu
;
3941 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3944 if (cpu
> 999 || cpu
< 0)
3947 sprintf(cpu_dir
, "cpu%ld", cpu
);
3948 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
3950 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
3954 /* per cpu trace_pipe */
3955 trace_create_file("trace_pipe", 0444, d_cpu
,
3956 (void *) cpu
, &tracing_pipe_fops
);
3959 trace_create_file("trace", 0644, d_cpu
,
3960 (void *) cpu
, &tracing_fops
);
3962 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
3963 (void *) cpu
, &tracing_buffers_fops
);
3965 trace_create_file("stats", 0444, d_cpu
,
3966 (void *) cpu
, &tracing_stats_fops
);
3969 #ifdef CONFIG_FTRACE_SELFTEST
3970 /* Let selftest have access to static functions in this file */
3971 #include "trace_selftest.c"
3974 struct trace_option_dentry
{
3975 struct tracer_opt
*opt
;
3976 struct tracer_flags
*flags
;
3977 struct dentry
*entry
;
3981 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3984 struct trace_option_dentry
*topt
= filp
->private_data
;
3987 if (topt
->flags
->val
& topt
->opt
->bit
)
3992 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3996 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3999 struct trace_option_dentry
*topt
= filp
->private_data
;
4004 if (cnt
>= sizeof(buf
))
4007 if (copy_from_user(&buf
, ubuf
, cnt
))
4012 ret
= strict_strtoul(buf
, 10, &val
);
4016 if (val
!= 0 && val
!= 1)
4019 if (!!(topt
->flags
->val
& topt
->opt
->bit
) != val
) {
4020 mutex_lock(&trace_types_lock
);
4021 ret
= __set_tracer_option(current_trace
, topt
->flags
,
4023 mutex_unlock(&trace_types_lock
);
4034 static const struct file_operations trace_options_fops
= {
4035 .open
= tracing_open_generic
,
4036 .read
= trace_options_read
,
4037 .write
= trace_options_write
,
4041 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
4044 long index
= (long)filp
->private_data
;
4047 if (trace_flags
& (1 << index
))
4052 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
4056 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
4059 long index
= (long)filp
->private_data
;
4064 if (cnt
>= sizeof(buf
))
4067 if (copy_from_user(&buf
, ubuf
, cnt
))
4072 ret
= strict_strtoul(buf
, 10, &val
);
4076 if (val
!= 0 && val
!= 1)
4078 set_tracer_flags(1 << index
, val
);
4085 static const struct file_operations trace_options_core_fops
= {
4086 .open
= tracing_open_generic
,
4087 .read
= trace_options_core_read
,
4088 .write
= trace_options_core_write
,
4091 struct dentry
*trace_create_file(const char *name
,
4093 struct dentry
*parent
,
4095 const struct file_operations
*fops
)
4099 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
4101 pr_warning("Could not create debugfs '%s' entry\n", name
);
4107 static struct dentry
*trace_options_init_dentry(void)
4109 struct dentry
*d_tracer
;
4110 static struct dentry
*t_options
;
4115 d_tracer
= tracing_init_dentry();
4119 t_options
= debugfs_create_dir("options", d_tracer
);
4121 pr_warning("Could not create debugfs directory 'options'\n");
4129 create_trace_option_file(struct trace_option_dentry
*topt
,
4130 struct tracer_flags
*flags
,
4131 struct tracer_opt
*opt
)
4133 struct dentry
*t_options
;
4135 t_options
= trace_options_init_dentry();
4139 topt
->flags
= flags
;
4142 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
4143 &trace_options_fops
);
4147 static struct trace_option_dentry
*
4148 create_trace_option_files(struct tracer
*tracer
)
4150 struct trace_option_dentry
*topts
;
4151 struct tracer_flags
*flags
;
4152 struct tracer_opt
*opts
;
4158 flags
= tracer
->flags
;
4160 if (!flags
|| !flags
->opts
)
4165 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4168 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
4172 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4173 create_trace_option_file(&topts
[cnt
], flags
,
4180 destroy_trace_option_files(struct trace_option_dentry
*topts
)
4187 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
4188 if (topts
[cnt
].entry
)
4189 debugfs_remove(topts
[cnt
].entry
);
4195 static struct dentry
*
4196 create_trace_option_core_file(const char *option
, long index
)
4198 struct dentry
*t_options
;
4200 t_options
= trace_options_init_dentry();
4204 return trace_create_file(option
, 0644, t_options
, (void *)index
,
4205 &trace_options_core_fops
);
4208 static __init
void create_trace_options_dir(void)
4210 struct dentry
*t_options
;
4213 t_options
= trace_options_init_dentry();
4217 for (i
= 0; trace_options
[i
]; i
++)
4218 create_trace_option_core_file(trace_options
[i
], i
);
4221 static __init
int tracer_init_debugfs(void)
4223 struct dentry
*d_tracer
;
4226 trace_access_lock_init();
4228 d_tracer
= tracing_init_dentry();
4230 trace_create_file("tracing_enabled", 0644, d_tracer
,
4231 &global_trace
, &tracing_ctrl_fops
);
4233 trace_create_file("trace_options", 0644, d_tracer
,
4234 NULL
, &tracing_iter_fops
);
4236 trace_create_file("tracing_cpumask", 0644, d_tracer
,
4237 NULL
, &tracing_cpumask_fops
);
4239 trace_create_file("trace", 0644, d_tracer
,
4240 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
4242 trace_create_file("available_tracers", 0444, d_tracer
,
4243 &global_trace
, &show_traces_fops
);
4245 trace_create_file("current_tracer", 0644, d_tracer
,
4246 &global_trace
, &set_tracer_fops
);
4248 #ifdef CONFIG_TRACER_MAX_TRACE
4249 trace_create_file("tracing_max_latency", 0644, d_tracer
,
4250 &tracing_max_latency
, &tracing_max_lat_fops
);
4252 trace_create_file("tracing_thresh", 0644, d_tracer
,
4253 &tracing_thresh
, &tracing_max_lat_fops
);
4256 trace_create_file("README", 0444, d_tracer
,
4257 NULL
, &tracing_readme_fops
);
4259 trace_create_file("trace_pipe", 0444, d_tracer
,
4260 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
4262 trace_create_file("buffer_size_kb", 0644, d_tracer
,
4263 &global_trace
, &tracing_entries_fops
);
4265 trace_create_file("trace_marker", 0220, d_tracer
,
4266 NULL
, &tracing_mark_fops
);
4268 trace_create_file("saved_cmdlines", 0444, d_tracer
,
4269 NULL
, &tracing_saved_cmdlines_fops
);
4271 trace_create_file("trace_clock", 0644, d_tracer
, NULL
,
4274 #ifdef CONFIG_DYNAMIC_FTRACE
4275 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
4276 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
4278 #ifdef CONFIG_SYSPROF_TRACER
4279 init_tracer_sysprof_debugfs(d_tracer
);
4282 create_trace_options_dir();
4284 for_each_tracing_cpu(cpu
)
4285 tracing_init_debugfs_percpu(cpu
);
4290 static int trace_panic_handler(struct notifier_block
*this,
4291 unsigned long event
, void *unused
)
4293 if (ftrace_dump_on_oops
)
4298 static struct notifier_block trace_panic_notifier
= {
4299 .notifier_call
= trace_panic_handler
,
4301 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
4304 static int trace_die_handler(struct notifier_block
*self
,
4310 if (ftrace_dump_on_oops
)
4319 static struct notifier_block trace_die_notifier
= {
4320 .notifier_call
= trace_die_handler
,
4325 * printk is set to max of 1024, we really don't need it that big.
4326 * Nothing should be printing 1000 characters anyway.
4328 #define TRACE_MAX_PRINT 1000
4331 * Define here KERN_TRACE so that we have one place to modify
4332 * it if we decide to change what log level the ftrace dump
4335 #define KERN_TRACE KERN_EMERG
4338 trace_printk_seq(struct trace_seq
*s
)
4340 /* Probably should print a warning here. */
4344 /* should be zero ended, but we are paranoid. */
4345 s
->buffer
[s
->len
] = 0;
4347 printk(KERN_TRACE
"%s", s
->buffer
);
4352 static void __ftrace_dump(bool disable_tracing
)
4354 static arch_spinlock_t ftrace_dump_lock
=
4355 (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
4356 /* use static because iter can be a bit big for the stack */
4357 static struct trace_iterator iter
;
4358 unsigned int old_userobj
;
4359 static int dump_ran
;
4360 unsigned long flags
;
4364 local_irq_save(flags
);
4365 arch_spin_lock(&ftrace_dump_lock
);
4373 if (disable_tracing
)
4376 for_each_tracing_cpu(cpu
) {
4377 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
4380 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
4382 /* don't look at user memory in panic mode */
4383 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4385 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4387 /* Simulate the iterator */
4388 iter
.tr
= &global_trace
;
4389 iter
.trace
= current_trace
;
4390 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4393 * We need to stop all tracing on all CPUS to read the
4394 * the next buffer. This is a bit expensive, but is
4395 * not done often. We fill all what we can read,
4396 * and then release the locks again.
4399 while (!trace_empty(&iter
)) {
4402 printk(KERN_TRACE
"---------------------------------\n");
4406 /* reset all but tr, trace, and overruns */
4407 memset(&iter
.seq
, 0,
4408 sizeof(struct trace_iterator
) -
4409 offsetof(struct trace_iterator
, seq
));
4410 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4413 if (find_next_entry_inc(&iter
) != NULL
) {
4416 ret
= print_trace_line(&iter
);
4417 if (ret
!= TRACE_TYPE_NO_CONSUME
)
4418 trace_consume(&iter
);
4421 trace_printk_seq(&iter
.seq
);
4425 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4427 printk(KERN_TRACE
"---------------------------------\n");
4429 /* Re-enable tracing if requested */
4430 if (!disable_tracing
) {
4431 trace_flags
|= old_userobj
;
4433 for_each_tracing_cpu(cpu
) {
4434 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
4440 arch_spin_unlock(&ftrace_dump_lock
);
4441 local_irq_restore(flags
);
4444 /* By default: disable tracing after the dump */
4445 void ftrace_dump(void)
4447 __ftrace_dump(true);
4450 __init
static int tracer_alloc_buffers(void)
4456 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4459 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4460 goto out_free_buffer_mask
;
4462 /* To save memory, keep the ring buffer size to its minimum */
4463 if (ring_buffer_expanded
)
4464 ring_buf_size
= trace_buf_size
;
4468 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4469 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4471 /* TODO: make the number of buffers hot pluggable with CPUS */
4472 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
,
4473 TRACE_BUFFER_FLAGS
);
4474 if (!global_trace
.buffer
) {
4475 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4477 goto out_free_cpumask
;
4479 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4482 #ifdef CONFIG_TRACER_MAX_TRACE
4483 max_tr
.buffer
= ring_buffer_alloc(ring_buf_size
,
4484 TRACE_BUFFER_FLAGS
);
4485 if (!max_tr
.buffer
) {
4486 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4488 ring_buffer_free(global_trace
.buffer
);
4489 goto out_free_cpumask
;
4491 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
4492 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
4495 /* Allocate the first page for all buffers */
4496 for_each_tracing_cpu(i
) {
4497 global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4498 max_tr
.data
[i
] = &per_cpu(max_tr_data
, i
);
4501 trace_init_cmdlines();
4503 register_tracer(&nop_trace
);
4504 current_trace
= &nop_trace
;
4505 #ifdef CONFIG_BOOT_TRACER
4506 register_tracer(&boot_tracer
);
4508 /* All seems OK, enable tracing */
4509 tracing_disabled
= 0;
4511 atomic_notifier_chain_register(&panic_notifier_list
,
4512 &trace_panic_notifier
);
4514 register_die_notifier(&trace_die_notifier
);
4519 free_cpumask_var(tracing_cpumask
);
4520 out_free_buffer_mask
:
4521 free_cpumask_var(tracing_buffer_mask
);
4526 __init
static int clear_boot_tracer(void)
4529 * The default tracer at boot buffer is an init section.
4530 * This function is called in lateinit. If we did not
4531 * find the boot tracer, then clear it out, to prevent
4532 * later registration from accessing the buffer that is
4533 * about to be freed.
4535 if (!default_bootup_tracer
)
4538 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4539 default_bootup_tracer
);
4540 default_bootup_tracer
= NULL
;
4545 early_initcall(tracer_alloc_buffers
);
4546 fs_initcall(tracer_init_debugfs
);
4547 late_initcall(clear_boot_tracer
);