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 <linux/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/ctype.h>
36 #include <linux/init.h>
37 #include <linux/poll.h>
38 #include <linux/gfp.h>
42 #include "trace_output.h"
44 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
46 unsigned long __read_mostly tracing_max_latency
;
47 unsigned long __read_mostly tracing_thresh
;
50 * On boot up, the ring buffer is set to the minimum size, so that
51 * we do not waste memory on systems that are not using tracing.
53 static int ring_buffer_expanded
;
56 * We need to change this state when a selftest is running.
57 * A selftest will lurk into the ring-buffer to count the
58 * entries inserted during the selftest although some concurrent
59 * insertions into the ring-buffer such as trace_printk could occurred
60 * at the same time, giving false positive or negative results.
62 static bool __read_mostly tracing_selftest_running
;
65 * If a tracer is running, we do not want to run SELFTEST.
67 static bool __read_mostly tracing_selftest_disabled
;
69 /* For tracers that don't implement custom flags */
70 static struct tracer_opt dummy_tracer_opt
[] = {
74 static struct tracer_flags dummy_tracer_flags
= {
76 .opts
= dummy_tracer_opt
79 static int dummy_set_flag(u32 old_flags
, u32 bit
, int set
)
85 * Kill all tracing for good (never come back).
86 * It is initialized to 1 but will turn to zero if the initialization
87 * of the tracer is successful. But that is the only place that sets
90 static int tracing_disabled
= 1;
92 static DEFINE_PER_CPU(local_t
, ftrace_cpu_disabled
);
94 static inline void ftrace_disable_cpu(void)
97 local_inc(&__get_cpu_var(ftrace_cpu_disabled
));
100 static inline void ftrace_enable_cpu(void)
102 local_dec(&__get_cpu_var(ftrace_cpu_disabled
));
106 static cpumask_var_t __read_mostly tracing_buffer_mask
;
108 /* Define which cpu buffers are currently read in trace_pipe */
109 static cpumask_var_t tracing_reader_cpumask
;
111 #define for_each_tracing_cpu(cpu) \
112 for_each_cpu(cpu, tracing_buffer_mask)
115 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
117 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
118 * is set, then ftrace_dump is called. This will output the contents
119 * of the ftrace buffers to the console. This is very useful for
120 * capturing traces that lead to crashes and outputing it to a
123 * It is default off, but you can enable it with either specifying
124 * "ftrace_dump_on_oops" in the kernel command line, or setting
125 * /proc/sys/kernel/ftrace_dump_on_oops to true.
127 int ftrace_dump_on_oops
;
129 static int tracing_set_tracer(const char *buf
);
131 #define BOOTUP_TRACER_SIZE 100
132 static char bootup_tracer_buf
[BOOTUP_TRACER_SIZE
] __initdata
;
133 static char *default_bootup_tracer
;
135 static int __init
set_ftrace(char *str
)
137 strncpy(bootup_tracer_buf
, str
, BOOTUP_TRACER_SIZE
);
138 default_bootup_tracer
= bootup_tracer_buf
;
139 /* We are using ftrace early, expand it */
140 ring_buffer_expanded
= 1;
143 __setup("ftrace=", set_ftrace
);
145 static int __init
set_ftrace_dump_on_oops(char *str
)
147 ftrace_dump_on_oops
= 1;
150 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
152 unsigned long long ns2usecs(cycle_t nsec
)
160 * The global_trace is the descriptor that holds the tracing
161 * buffers for the live tracing. For each CPU, it contains
162 * a link list of pages that will store trace entries. The
163 * page descriptor of the pages in the memory is used to hold
164 * the link list by linking the lru item in the page descriptor
165 * to each of the pages in the buffer per CPU.
167 * For each active CPU there is a data field that holds the
168 * pages for the buffer for that CPU. Each CPU has the same number
169 * of pages allocated for its buffer.
171 static struct trace_array global_trace
;
173 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
175 int filter_current_check_discard(struct ftrace_event_call
*call
, void *rec
,
176 struct ring_buffer_event
*event
)
178 return filter_check_discard(call
, rec
, global_trace
.buffer
, event
);
180 EXPORT_SYMBOL_GPL(filter_current_check_discard
);
182 cycle_t
ftrace_now(int cpu
)
186 /* Early boot up does not have a buffer yet */
187 if (!global_trace
.buffer
)
188 return trace_clock_local();
190 ts
= ring_buffer_time_stamp(global_trace
.buffer
, cpu
);
191 ring_buffer_normalize_time_stamp(global_trace
.buffer
, cpu
, &ts
);
197 * The max_tr is used to snapshot the global_trace when a maximum
198 * latency is reached. Some tracers will use this to store a maximum
199 * trace while it continues examining live traces.
201 * The buffers for the max_tr are set up the same as the global_trace.
202 * When a snapshot is taken, the link list of the max_tr is swapped
203 * with the link list of the global_trace and the buffers are reset for
204 * the global_trace so the tracing can continue.
206 static struct trace_array max_tr
;
208 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
210 /* tracer_enabled is used to toggle activation of a tracer */
211 static int tracer_enabled
= 1;
214 * tracing_is_enabled - return tracer_enabled status
216 * This function is used by other tracers to know the status
217 * of the tracer_enabled flag. Tracers may use this function
218 * to know if it should enable their features when starting
219 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
221 int tracing_is_enabled(void)
223 return tracer_enabled
;
227 * trace_buf_size is the size in bytes that is allocated
228 * for a buffer. Note, the number of bytes is always rounded
231 * This number is purposely set to a low number of 16384.
232 * If the dump on oops happens, it will be much appreciated
233 * to not have to wait for all that output. Anyway this can be
234 * boot time and run time configurable.
236 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
238 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
240 /* trace_types holds a link list of available tracers. */
241 static struct tracer
*trace_types __read_mostly
;
243 /* current_trace points to the tracer that is currently active */
244 static struct tracer
*current_trace __read_mostly
;
247 * max_tracer_type_len is used to simplify the allocating of
248 * buffers to read userspace tracer names. We keep track of
249 * the longest tracer name registered.
251 static int max_tracer_type_len
;
254 * trace_types_lock is used to protect the trace_types list.
255 * This lock is also used to keep user access serialized.
256 * Accesses from userspace will grab this lock while userspace
257 * activities happen inside the kernel.
259 static DEFINE_MUTEX(trace_types_lock
);
261 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
262 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
264 /* trace_flags holds trace_options default values */
265 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
266 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
| TRACE_ITER_SLEEP_TIME
|
267 TRACE_ITER_GRAPH_TIME
;
270 * trace_wake_up - wake up tasks waiting for trace input
272 * Simply wakes up any task that is blocked on the trace_wait
273 * queue. These is used with trace_poll for tasks polling the trace.
275 void trace_wake_up(void)
278 * The runqueue_is_locked() can fail, but this is the best we
281 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
282 wake_up(&trace_wait
);
285 static int __init
set_buf_size(char *str
)
287 unsigned long buf_size
;
291 buf_size
= memparse(str
, &str
);
292 /* nr_entries can not be zero */
295 trace_buf_size
= buf_size
;
298 __setup("trace_buf_size=", set_buf_size
);
300 unsigned long nsecs_to_usecs(unsigned long nsecs
)
305 /* These must match the bit postions in trace_iterator_flags */
306 static const char *trace_options
[] = {
333 * ftrace_max_lock is used to protect the swapping of buffers
334 * when taking a max snapshot. The buffers themselves are
335 * protected by per_cpu spinlocks. But the action of the swap
336 * needs its own lock.
338 * This is defined as a raw_spinlock_t in order to help
339 * with performance when lockdep debugging is enabled.
341 static raw_spinlock_t ftrace_max_lock
=
342 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
345 * Copy the new maximum trace into the separate maximum-trace
346 * structure. (this way the maximum trace is permanently saved,
347 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
350 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
352 struct trace_array_cpu
*data
= tr
->data
[cpu
];
355 max_tr
.time_start
= data
->preempt_timestamp
;
357 data
= max_tr
.data
[cpu
];
358 data
->saved_latency
= tracing_max_latency
;
360 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
361 data
->pid
= tsk
->pid
;
362 data
->uid
= task_uid(tsk
);
363 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
364 data
->policy
= tsk
->policy
;
365 data
->rt_priority
= tsk
->rt_priority
;
367 /* record this tasks comm */
368 tracing_record_cmdline(tsk
);
371 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
379 if (s
->len
<= s
->readpos
)
382 len
= s
->len
- s
->readpos
;
385 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
395 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
400 if (s
->len
<= s
->readpos
)
403 len
= s
->len
- s
->readpos
;
406 ret
= memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
415 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
417 * @tsk: the task with the latency
418 * @cpu: The cpu that initiated the trace.
420 * Flip the buffers between the @tr and the max_tr and record information
421 * about which task was the cause of this latency.
424 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
426 struct ring_buffer
*buf
= tr
->buffer
;
428 WARN_ON_ONCE(!irqs_disabled());
429 __raw_spin_lock(&ftrace_max_lock
);
431 tr
->buffer
= max_tr
.buffer
;
434 ftrace_disable_cpu();
435 ring_buffer_reset(tr
->buffer
);
438 __update_max_tr(tr
, tsk
, cpu
);
439 __raw_spin_unlock(&ftrace_max_lock
);
443 * update_max_tr_single - only copy one trace over, and reset the rest
445 * @tsk - task with the latency
446 * @cpu - the cpu of the buffer to copy.
448 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
451 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
455 WARN_ON_ONCE(!irqs_disabled());
456 __raw_spin_lock(&ftrace_max_lock
);
458 ftrace_disable_cpu();
460 ring_buffer_reset(max_tr
.buffer
);
461 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
465 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
);
467 __update_max_tr(tr
, tsk
, cpu
);
468 __raw_spin_unlock(&ftrace_max_lock
);
472 * register_tracer - register a tracer with the ftrace system.
473 * @type - the plugin for the tracer
475 * Register a new plugin tracer.
477 int register_tracer(struct tracer
*type
)
478 __releases(kernel_lock
)
479 __acquires(kernel_lock
)
486 pr_info("Tracer must have a name\n");
491 * When this gets called we hold the BKL which means that
492 * preemption is disabled. Various trace selftests however
493 * need to disable and enable preemption for successful tests.
494 * So we drop the BKL here and grab it after the tests again.
497 mutex_lock(&trace_types_lock
);
499 tracing_selftest_running
= true;
501 for (t
= trace_types
; t
; t
= t
->next
) {
502 if (strcmp(type
->name
, t
->name
) == 0) {
504 pr_info("Trace %s already registered\n",
512 type
->set_flag
= &dummy_set_flag
;
514 type
->flags
= &dummy_tracer_flags
;
516 if (!type
->flags
->opts
)
517 type
->flags
->opts
= dummy_tracer_opt
;
518 if (!type
->wait_pipe
)
519 type
->wait_pipe
= default_wait_pipe
;
522 #ifdef CONFIG_FTRACE_STARTUP_TEST
523 if (type
->selftest
&& !tracing_selftest_disabled
) {
524 struct tracer
*saved_tracer
= current_trace
;
525 struct trace_array
*tr
= &global_trace
;
529 * Run a selftest on this tracer.
530 * Here we reset the trace buffer, and set the current
531 * tracer to be this tracer. The tracer can then run some
532 * internal tracing to verify that everything is in order.
533 * If we fail, we do not register this tracer.
535 for_each_tracing_cpu(i
)
536 tracing_reset(tr
, i
);
538 current_trace
= type
;
539 /* the test is responsible for initializing and enabling */
540 pr_info("Testing tracer %s: ", type
->name
);
541 ret
= type
->selftest(type
, tr
);
542 /* the test is responsible for resetting too */
543 current_trace
= saved_tracer
;
545 printk(KERN_CONT
"FAILED!\n");
548 /* Only reset on passing, to avoid touching corrupted buffers */
549 for_each_tracing_cpu(i
)
550 tracing_reset(tr
, i
);
552 printk(KERN_CONT
"PASSED\n");
556 type
->next
= trace_types
;
558 len
= strlen(type
->name
);
559 if (len
> max_tracer_type_len
)
560 max_tracer_type_len
= len
;
563 tracing_selftest_running
= false;
564 mutex_unlock(&trace_types_lock
);
566 if (ret
|| !default_bootup_tracer
)
569 if (strncmp(default_bootup_tracer
, type
->name
, BOOTUP_TRACER_SIZE
))
572 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
573 /* Do we want this tracer to start on bootup? */
574 tracing_set_tracer(type
->name
);
575 default_bootup_tracer
= NULL
;
576 /* disable other selftests, since this will break it. */
577 tracing_selftest_disabled
= 1;
578 #ifdef CONFIG_FTRACE_STARTUP_TEST
579 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
588 void unregister_tracer(struct tracer
*type
)
593 mutex_lock(&trace_types_lock
);
594 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
598 pr_info("Trace %s not registered\n", type
->name
);
604 if (type
== current_trace
&& tracer_enabled
) {
607 if (current_trace
->stop
)
608 current_trace
->stop(&global_trace
);
609 current_trace
= &nop_trace
;
612 if (strlen(type
->name
) != max_tracer_type_len
)
615 max_tracer_type_len
= 0;
616 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
617 len
= strlen((*t
)->name
);
618 if (len
> max_tracer_type_len
)
619 max_tracer_type_len
= len
;
622 mutex_unlock(&trace_types_lock
);
625 void tracing_reset(struct trace_array
*tr
, int cpu
)
627 ftrace_disable_cpu();
628 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
632 void tracing_reset_online_cpus(struct trace_array
*tr
)
636 tr
->time_start
= ftrace_now(tr
->cpu
);
638 for_each_online_cpu(cpu
)
639 tracing_reset(tr
, cpu
);
642 void tracing_reset_current(int cpu
)
644 tracing_reset(&global_trace
, cpu
);
647 void tracing_reset_current_online_cpus(void)
649 tracing_reset_online_cpus(&global_trace
);
652 #define SAVED_CMDLINES 128
653 #define NO_CMDLINE_MAP UINT_MAX
654 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
655 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
656 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
657 static int cmdline_idx
;
658 static raw_spinlock_t trace_cmdline_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
660 /* temporary disable recording */
661 static atomic_t trace_record_cmdline_disabled __read_mostly
;
663 static void trace_init_cmdlines(void)
665 memset(&map_pid_to_cmdline
, NO_CMDLINE_MAP
, sizeof(map_pid_to_cmdline
));
666 memset(&map_cmdline_to_pid
, NO_CMDLINE_MAP
, sizeof(map_cmdline_to_pid
));
670 static int trace_stop_count
;
671 static DEFINE_SPINLOCK(tracing_start_lock
);
674 * ftrace_off_permanent - disable all ftrace code permanently
676 * This should only be called when a serious anomally has
677 * been detected. This will turn off the function tracing,
678 * ring buffers, and other tracing utilites. It takes no
679 * locks and can be called from any context.
681 void ftrace_off_permanent(void)
683 tracing_disabled
= 1;
685 tracing_off_permanent();
689 * tracing_start - quick start of the tracer
691 * If tracing is enabled but was stopped by tracing_stop,
692 * this will start the tracer back up.
694 void tracing_start(void)
696 struct ring_buffer
*buffer
;
699 if (tracing_disabled
)
702 spin_lock_irqsave(&tracing_start_lock
, flags
);
703 if (--trace_stop_count
) {
704 if (trace_stop_count
< 0) {
705 /* Someone screwed up their debugging */
707 trace_stop_count
= 0;
713 buffer
= global_trace
.buffer
;
715 ring_buffer_record_enable(buffer
);
717 buffer
= max_tr
.buffer
;
719 ring_buffer_record_enable(buffer
);
723 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
727 * tracing_stop - quick stop of the tracer
729 * Light weight way to stop tracing. Use in conjunction with
732 void tracing_stop(void)
734 struct ring_buffer
*buffer
;
738 spin_lock_irqsave(&tracing_start_lock
, flags
);
739 if (trace_stop_count
++)
742 buffer
= global_trace
.buffer
;
744 ring_buffer_record_disable(buffer
);
746 buffer
= max_tr
.buffer
;
748 ring_buffer_record_disable(buffer
);
751 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
754 void trace_stop_cmdline_recording(void);
756 static void trace_save_cmdline(struct task_struct
*tsk
)
760 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
764 * It's not the end of the world if we don't get
765 * the lock, but we also don't want to spin
766 * nor do we want to disable interrupts,
767 * so if we miss here, then better luck next time.
769 if (!__raw_spin_trylock(&trace_cmdline_lock
))
772 idx
= map_pid_to_cmdline
[tsk
->pid
];
773 if (idx
== NO_CMDLINE_MAP
) {
774 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
777 * Check whether the cmdline buffer at idx has a pid
778 * mapped. We are going to overwrite that entry so we
779 * need to clear the map_pid_to_cmdline. Otherwise we
780 * would read the new comm for the old pid.
782 pid
= map_cmdline_to_pid
[idx
];
783 if (pid
!= NO_CMDLINE_MAP
)
784 map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
786 map_cmdline_to_pid
[idx
] = tsk
->pid
;
787 map_pid_to_cmdline
[tsk
->pid
] = idx
;
792 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
794 __raw_spin_unlock(&trace_cmdline_lock
);
797 void trace_find_cmdline(int pid
, char comm
[])
802 strcpy(comm
, "<idle>");
806 if (pid
> PID_MAX_DEFAULT
) {
807 strcpy(comm
, "<...>");
812 __raw_spin_lock(&trace_cmdline_lock
);
813 map
= map_pid_to_cmdline
[pid
];
814 if (map
!= NO_CMDLINE_MAP
)
815 strcpy(comm
, saved_cmdlines
[map
]);
817 strcpy(comm
, "<...>");
819 __raw_spin_unlock(&trace_cmdline_lock
);
823 void tracing_record_cmdline(struct task_struct
*tsk
)
825 if (atomic_read(&trace_record_cmdline_disabled
) || !tracer_enabled
||
829 trace_save_cmdline(tsk
);
833 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
836 struct task_struct
*tsk
= current
;
838 entry
->preempt_count
= pc
& 0xff;
839 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
840 entry
->tgid
= (tsk
) ? tsk
->tgid
: 0;
842 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
843 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
845 TRACE_FLAG_IRQS_NOSUPPORT
|
847 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
848 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
849 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
851 EXPORT_SYMBOL_GPL(tracing_generic_entry_update
);
853 struct ring_buffer_event
*trace_buffer_lock_reserve(struct trace_array
*tr
,
856 unsigned long flags
, int pc
)
858 struct ring_buffer_event
*event
;
860 event
= ring_buffer_lock_reserve(tr
->buffer
, len
);
862 struct trace_entry
*ent
= ring_buffer_event_data(event
);
864 tracing_generic_entry_update(ent
, flags
, pc
);
870 static void ftrace_trace_stack(struct trace_array
*tr
,
871 unsigned long flags
, int skip
, int pc
);
872 static void ftrace_trace_userstack(struct trace_array
*tr
,
873 unsigned long flags
, int pc
);
875 static inline void __trace_buffer_unlock_commit(struct trace_array
*tr
,
876 struct ring_buffer_event
*event
,
877 unsigned long flags
, int pc
,
880 ring_buffer_unlock_commit(tr
->buffer
, event
);
882 ftrace_trace_stack(tr
, flags
, 6, pc
);
883 ftrace_trace_userstack(tr
, flags
, pc
);
889 void trace_buffer_unlock_commit(struct trace_array
*tr
,
890 struct ring_buffer_event
*event
,
891 unsigned long flags
, int pc
)
893 __trace_buffer_unlock_commit(tr
, event
, flags
, pc
, 1);
896 struct ring_buffer_event
*
897 trace_current_buffer_lock_reserve(int type
, unsigned long len
,
898 unsigned long flags
, int pc
)
900 return trace_buffer_lock_reserve(&global_trace
,
901 type
, len
, flags
, pc
);
903 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve
);
905 void trace_current_buffer_unlock_commit(struct ring_buffer_event
*event
,
906 unsigned long flags
, int pc
)
908 __trace_buffer_unlock_commit(&global_trace
, event
, flags
, pc
, 1);
910 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit
);
912 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event
*event
,
913 unsigned long flags
, int pc
)
915 __trace_buffer_unlock_commit(&global_trace
, event
, flags
, pc
, 0);
917 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit
);
919 void trace_current_buffer_discard_commit(struct ring_buffer_event
*event
)
921 ring_buffer_discard_commit(global_trace
.buffer
, event
);
923 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
926 trace_function(struct trace_array
*tr
,
927 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
930 struct ftrace_event_call
*call
= &event_function
;
931 struct ring_buffer_event
*event
;
932 struct ftrace_entry
*entry
;
934 /* If we are reading the ring buffer, don't trace */
935 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
938 event
= trace_buffer_lock_reserve(tr
, TRACE_FN
, sizeof(*entry
),
942 entry
= ring_buffer_event_data(event
);
944 entry
->parent_ip
= parent_ip
;
946 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
947 ring_buffer_unlock_commit(tr
->buffer
, event
);
950 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
951 static int __trace_graph_entry(struct trace_array
*tr
,
952 struct ftrace_graph_ent
*trace
,
956 struct ftrace_event_call
*call
= &event_funcgraph_entry
;
957 struct ring_buffer_event
*event
;
958 struct ftrace_graph_ent_entry
*entry
;
960 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
963 event
= trace_buffer_lock_reserve(&global_trace
, TRACE_GRAPH_ENT
,
964 sizeof(*entry
), flags
, pc
);
967 entry
= ring_buffer_event_data(event
);
968 entry
->graph_ent
= *trace
;
969 if (!filter_current_check_discard(call
, entry
, event
))
970 ring_buffer_unlock_commit(global_trace
.buffer
, event
);
975 static void __trace_graph_return(struct trace_array
*tr
,
976 struct ftrace_graph_ret
*trace
,
980 struct ftrace_event_call
*call
= &event_funcgraph_exit
;
981 struct ring_buffer_event
*event
;
982 struct ftrace_graph_ret_entry
*entry
;
984 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
987 event
= trace_buffer_lock_reserve(&global_trace
, TRACE_GRAPH_RET
,
988 sizeof(*entry
), flags
, pc
);
991 entry
= ring_buffer_event_data(event
);
993 if (!filter_current_check_discard(call
, entry
, event
))
994 ring_buffer_unlock_commit(global_trace
.buffer
, event
);
999 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
1000 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1003 if (likely(!atomic_read(&data
->disabled
)))
1004 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
1007 static void __ftrace_trace_stack(struct trace_array
*tr
,
1008 unsigned long flags
,
1011 #ifdef CONFIG_STACKTRACE
1012 struct ftrace_event_call
*call
= &event_kernel_stack
;
1013 struct ring_buffer_event
*event
;
1014 struct stack_entry
*entry
;
1015 struct stack_trace trace
;
1017 event
= trace_buffer_lock_reserve(tr
, TRACE_STACK
,
1018 sizeof(*entry
), flags
, pc
);
1021 entry
= ring_buffer_event_data(event
);
1022 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1024 trace
.nr_entries
= 0;
1025 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1027 trace
.entries
= entry
->caller
;
1029 save_stack_trace(&trace
);
1030 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1031 ring_buffer_unlock_commit(tr
->buffer
, event
);
1035 static void ftrace_trace_stack(struct trace_array
*tr
,
1036 unsigned long flags
,
1039 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1042 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
1045 void __trace_stack(struct trace_array
*tr
,
1046 unsigned long flags
,
1049 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
1052 static void ftrace_trace_userstack(struct trace_array
*tr
,
1053 unsigned long flags
, int pc
)
1055 #ifdef CONFIG_STACKTRACE
1056 struct ftrace_event_call
*call
= &event_user_stack
;
1057 struct ring_buffer_event
*event
;
1058 struct userstack_entry
*entry
;
1059 struct stack_trace trace
;
1061 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1064 event
= trace_buffer_lock_reserve(tr
, TRACE_USER_STACK
,
1065 sizeof(*entry
), flags
, pc
);
1068 entry
= ring_buffer_event_data(event
);
1070 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1072 trace
.nr_entries
= 0;
1073 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1075 trace
.entries
= entry
->caller
;
1077 save_stack_trace_user(&trace
);
1078 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1079 ring_buffer_unlock_commit(tr
->buffer
, event
);
1084 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1086 ftrace_trace_userstack(tr
, flags
, preempt_count());
1091 ftrace_trace_special(void *__tr
,
1092 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1095 struct ring_buffer_event
*event
;
1096 struct trace_array
*tr
= __tr
;
1097 struct special_entry
*entry
;
1099 event
= trace_buffer_lock_reserve(tr
, TRACE_SPECIAL
,
1100 sizeof(*entry
), 0, pc
);
1103 entry
= ring_buffer_event_data(event
);
1107 trace_buffer_unlock_commit(tr
, event
, 0, pc
);
1111 __trace_special(void *__tr
, void *__data
,
1112 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1114 ftrace_trace_special(__tr
, arg1
, arg2
, arg3
, preempt_count());
1118 tracing_sched_switch_trace(struct trace_array
*tr
,
1119 struct task_struct
*prev
,
1120 struct task_struct
*next
,
1121 unsigned long flags
, int pc
)
1123 struct ftrace_event_call
*call
= &event_context_switch
;
1124 struct ring_buffer_event
*event
;
1125 struct ctx_switch_entry
*entry
;
1127 event
= trace_buffer_lock_reserve(tr
, TRACE_CTX
,
1128 sizeof(*entry
), flags
, pc
);
1131 entry
= ring_buffer_event_data(event
);
1132 entry
->prev_pid
= prev
->pid
;
1133 entry
->prev_prio
= prev
->prio
;
1134 entry
->prev_state
= prev
->state
;
1135 entry
->next_pid
= next
->pid
;
1136 entry
->next_prio
= next
->prio
;
1137 entry
->next_state
= next
->state
;
1138 entry
->next_cpu
= task_cpu(next
);
1140 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1141 trace_buffer_unlock_commit(tr
, event
, flags
, pc
);
1145 tracing_sched_wakeup_trace(struct trace_array
*tr
,
1146 struct task_struct
*wakee
,
1147 struct task_struct
*curr
,
1148 unsigned long flags
, int pc
)
1150 struct ftrace_event_call
*call
= &event_wakeup
;
1151 struct ring_buffer_event
*event
;
1152 struct ctx_switch_entry
*entry
;
1154 event
= trace_buffer_lock_reserve(tr
, TRACE_WAKE
,
1155 sizeof(*entry
), flags
, pc
);
1158 entry
= ring_buffer_event_data(event
);
1159 entry
->prev_pid
= curr
->pid
;
1160 entry
->prev_prio
= curr
->prio
;
1161 entry
->prev_state
= curr
->state
;
1162 entry
->next_pid
= wakee
->pid
;
1163 entry
->next_prio
= wakee
->prio
;
1164 entry
->next_state
= wakee
->state
;
1165 entry
->next_cpu
= task_cpu(wakee
);
1167 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1168 ring_buffer_unlock_commit(tr
->buffer
, event
);
1169 ftrace_trace_stack(tr
, flags
, 6, pc
);
1170 ftrace_trace_userstack(tr
, flags
, pc
);
1174 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1176 struct trace_array
*tr
= &global_trace
;
1177 struct trace_array_cpu
*data
;
1178 unsigned long flags
;
1182 if (tracing_disabled
)
1185 pc
= preempt_count();
1186 local_irq_save(flags
);
1187 cpu
= raw_smp_processor_id();
1188 data
= tr
->data
[cpu
];
1190 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1191 ftrace_trace_special(tr
, arg1
, arg2
, arg3
, pc
);
1193 atomic_dec(&data
->disabled
);
1194 local_irq_restore(flags
);
1197 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1198 int trace_graph_entry(struct ftrace_graph_ent
*trace
)
1200 struct trace_array
*tr
= &global_trace
;
1201 struct trace_array_cpu
*data
;
1202 unsigned long flags
;
1208 if (!ftrace_trace_task(current
))
1211 if (!ftrace_graph_addr(trace
->func
))
1214 local_irq_save(flags
);
1215 cpu
= raw_smp_processor_id();
1216 data
= tr
->data
[cpu
];
1217 disabled
= atomic_inc_return(&data
->disabled
);
1218 if (likely(disabled
== 1)) {
1219 pc
= preempt_count();
1220 ret
= __trace_graph_entry(tr
, trace
, flags
, pc
);
1224 /* Only do the atomic if it is not already set */
1225 if (!test_tsk_trace_graph(current
))
1226 set_tsk_trace_graph(current
);
1228 atomic_dec(&data
->disabled
);
1229 local_irq_restore(flags
);
1234 void trace_graph_return(struct ftrace_graph_ret
*trace
)
1236 struct trace_array
*tr
= &global_trace
;
1237 struct trace_array_cpu
*data
;
1238 unsigned long flags
;
1243 local_irq_save(flags
);
1244 cpu
= raw_smp_processor_id();
1245 data
= tr
->data
[cpu
];
1246 disabled
= atomic_inc_return(&data
->disabled
);
1247 if (likely(disabled
== 1)) {
1248 pc
= preempt_count();
1249 __trace_graph_return(tr
, trace
, flags
, pc
);
1252 clear_tsk_trace_graph(current
);
1253 atomic_dec(&data
->disabled
);
1254 local_irq_restore(flags
);
1256 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1260 * trace_vbprintk - write binary msg to tracing buffer
1263 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1265 static raw_spinlock_t trace_buf_lock
=
1266 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
1267 static u32 trace_buf
[TRACE_BUF_SIZE
];
1269 struct ftrace_event_call
*call
= &event_bprint
;
1270 struct ring_buffer_event
*event
;
1271 struct trace_array
*tr
= &global_trace
;
1272 struct trace_array_cpu
*data
;
1273 struct bprint_entry
*entry
;
1274 unsigned long flags
;
1277 int cpu
, len
= 0, size
, pc
;
1279 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1282 /* Don't pollute graph traces with trace_vprintk internals */
1283 pause_graph_tracing();
1285 pc
= preempt_count();
1286 resched
= ftrace_preempt_disable();
1287 cpu
= raw_smp_processor_id();
1288 data
= tr
->data
[cpu
];
1290 disable
= atomic_inc_return(&data
->disabled
);
1291 if (unlikely(disable
!= 1))
1294 /* Lockdep uses trace_printk for lock tracing */
1295 local_irq_save(flags
);
1296 __raw_spin_lock(&trace_buf_lock
);
1297 len
= vbin_printf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1299 if (len
> TRACE_BUF_SIZE
|| len
< 0)
1302 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1303 event
= trace_buffer_lock_reserve(tr
, TRACE_BPRINT
, size
, flags
, pc
);
1306 entry
= ring_buffer_event_data(event
);
1310 memcpy(entry
->buf
, trace_buf
, sizeof(u32
) * len
);
1311 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1312 ring_buffer_unlock_commit(tr
->buffer
, event
);
1315 __raw_spin_unlock(&trace_buf_lock
);
1316 local_irq_restore(flags
);
1319 atomic_dec_return(&data
->disabled
);
1320 ftrace_preempt_enable(resched
);
1321 unpause_graph_tracing();
1325 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1327 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1329 static raw_spinlock_t trace_buf_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
1330 static char trace_buf
[TRACE_BUF_SIZE
];
1332 struct ftrace_event_call
*call
= &event_print
;
1333 struct ring_buffer_event
*event
;
1334 struct trace_array
*tr
= &global_trace
;
1335 struct trace_array_cpu
*data
;
1336 int cpu
, len
= 0, size
, pc
;
1337 struct print_entry
*entry
;
1338 unsigned long irq_flags
;
1341 if (tracing_disabled
|| tracing_selftest_running
)
1344 pc
= preempt_count();
1345 preempt_disable_notrace();
1346 cpu
= raw_smp_processor_id();
1347 data
= tr
->data
[cpu
];
1349 disable
= atomic_inc_return(&data
->disabled
);
1350 if (unlikely(disable
!= 1))
1353 pause_graph_tracing();
1354 raw_local_irq_save(irq_flags
);
1355 __raw_spin_lock(&trace_buf_lock
);
1356 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1358 len
= min(len
, TRACE_BUF_SIZE
-1);
1361 size
= sizeof(*entry
) + len
+ 1;
1362 event
= trace_buffer_lock_reserve(tr
, TRACE_PRINT
, size
, irq_flags
, pc
);
1365 entry
= ring_buffer_event_data(event
);
1368 memcpy(&entry
->buf
, trace_buf
, len
);
1369 entry
->buf
[len
] = 0;
1370 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1371 ring_buffer_unlock_commit(tr
->buffer
, event
);
1374 __raw_spin_unlock(&trace_buf_lock
);
1375 raw_local_irq_restore(irq_flags
);
1376 unpause_graph_tracing();
1378 atomic_dec_return(&data
->disabled
);
1379 preempt_enable_notrace();
1383 EXPORT_SYMBOL_GPL(trace_vprintk
);
1385 enum trace_file_type
{
1386 TRACE_FILE_LAT_FMT
= 1,
1387 TRACE_FILE_ANNOTATE
= 2,
1390 static void trace_iterator_increment(struct trace_iterator
*iter
)
1392 /* Don't allow ftrace to trace into the ring buffers */
1393 ftrace_disable_cpu();
1396 if (iter
->buffer_iter
[iter
->cpu
])
1397 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1399 ftrace_enable_cpu();
1402 static struct trace_entry
*
1403 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1405 struct ring_buffer_event
*event
;
1406 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1408 /* Don't allow ftrace to trace into the ring buffers */
1409 ftrace_disable_cpu();
1412 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1414 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1416 ftrace_enable_cpu();
1418 return event
? ring_buffer_event_data(event
) : NULL
;
1421 static struct trace_entry
*
1422 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1424 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1425 struct trace_entry
*ent
, *next
= NULL
;
1426 int cpu_file
= iter
->cpu_file
;
1427 u64 next_ts
= 0, ts
;
1432 * If we are in a per_cpu trace file, don't bother by iterating over
1433 * all cpu and peek directly.
1435 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1436 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1438 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
);
1440 *ent_cpu
= cpu_file
;
1445 for_each_tracing_cpu(cpu
) {
1447 if (ring_buffer_empty_cpu(buffer
, cpu
))
1450 ent
= peek_next_entry(iter
, cpu
, &ts
);
1453 * Pick the entry with the smallest timestamp:
1455 if (ent
&& (!next
|| ts
< next_ts
)) {
1463 *ent_cpu
= next_cpu
;
1471 /* Find the next real entry, without updating the iterator itself */
1472 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1473 int *ent_cpu
, u64
*ent_ts
)
1475 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1478 /* Find the next real entry, and increment the iterator to the next entry */
1479 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1481 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1484 trace_iterator_increment(iter
);
1486 return iter
->ent
? iter
: NULL
;
1489 static void trace_consume(struct trace_iterator
*iter
)
1491 /* Don't allow ftrace to trace into the ring buffers */
1492 ftrace_disable_cpu();
1493 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1494 ftrace_enable_cpu();
1497 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1499 struct trace_iterator
*iter
= m
->private;
1505 /* can't go backwards */
1510 ent
= find_next_entry_inc(iter
);
1514 while (ent
&& iter
->idx
< i
)
1515 ent
= find_next_entry_inc(iter
);
1523 * No necessary locking here. The worst thing which can
1524 * happen is loosing events consumed at the same time
1525 * by a trace_pipe reader.
1526 * Other than that, we don't risk to crash the ring buffer
1527 * because it serializes the readers.
1529 * The current tracer is copied to avoid a global locking
1532 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1534 struct trace_iterator
*iter
= m
->private;
1535 static struct tracer
*old_tracer
;
1536 int cpu_file
= iter
->cpu_file
;
1541 /* copy the tracer to avoid using a global lock all around */
1542 mutex_lock(&trace_types_lock
);
1543 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1544 old_tracer
= current_trace
;
1545 *iter
->trace
= *current_trace
;
1547 mutex_unlock(&trace_types_lock
);
1549 atomic_inc(&trace_record_cmdline_disabled
);
1551 if (*pos
!= iter
->pos
) {
1556 ftrace_disable_cpu();
1558 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1559 for_each_tracing_cpu(cpu
)
1560 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1562 ring_buffer_iter_reset(iter
->buffer_iter
[cpu_file
]);
1565 ftrace_enable_cpu();
1567 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1572 p
= s_next(m
, p
, &l
);
1575 trace_event_read_lock();
1579 static void s_stop(struct seq_file
*m
, void *p
)
1581 atomic_dec(&trace_record_cmdline_disabled
);
1582 trace_event_read_unlock();
1585 static void print_lat_help_header(struct seq_file
*m
)
1587 seq_puts(m
, "# _------=> CPU# \n");
1588 seq_puts(m
, "# / _-----=> irqs-off \n");
1589 seq_puts(m
, "# | / _----=> need-resched \n");
1590 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1591 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1592 seq_puts(m
, "# |||| / \n");
1593 seq_puts(m
, "# ||||| delay \n");
1594 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1595 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1598 static void print_func_help_header(struct seq_file
*m
)
1600 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1601 seq_puts(m
, "# | | | | |\n");
1606 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1608 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1609 struct trace_array
*tr
= iter
->tr
;
1610 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1611 struct tracer
*type
= current_trace
;
1612 unsigned long total
;
1613 unsigned long entries
;
1614 const char *name
= "preemption";
1619 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1621 ring_buffer_overruns(iter
->tr
->buffer
);
1623 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
1625 seq_puts(m
, "# -----------------------------------"
1626 "---------------------------------\n");
1627 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1628 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1629 nsecs_to_usecs(data
->saved_latency
),
1633 #if defined(CONFIG_PREEMPT_NONE)
1635 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1637 #elif defined(CONFIG_PREEMPT)
1642 /* These are reserved for later use */
1645 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1649 seq_puts(m
, "# -----------------\n");
1650 seq_printf(m
, "# | task: %.16s-%d "
1651 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1652 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1653 data
->policy
, data
->rt_priority
);
1654 seq_puts(m
, "# -----------------\n");
1656 if (data
->critical_start
) {
1657 seq_puts(m
, "# => started at: ");
1658 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1659 trace_print_seq(m
, &iter
->seq
);
1660 seq_puts(m
, "\n# => ended at: ");
1661 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1662 trace_print_seq(m
, &iter
->seq
);
1669 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1671 struct trace_seq
*s
= &iter
->seq
;
1673 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1676 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1679 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1682 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1684 /* Don't print started cpu buffer for the first entry of the trace */
1686 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
1690 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1692 struct trace_seq
*s
= &iter
->seq
;
1693 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1694 struct trace_entry
*entry
;
1695 struct trace_event
*event
;
1699 test_cpu_buff_start(iter
);
1701 event
= ftrace_find_event(entry
->type
);
1703 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1704 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1705 if (!trace_print_lat_context(iter
))
1708 if (!trace_print_context(iter
))
1714 return event
->trace(iter
, sym_flags
);
1716 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1719 return TRACE_TYPE_HANDLED
;
1721 return TRACE_TYPE_PARTIAL_LINE
;
1724 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1726 struct trace_seq
*s
= &iter
->seq
;
1727 struct trace_entry
*entry
;
1728 struct trace_event
*event
;
1732 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1733 if (!trace_seq_printf(s
, "%d %d %llu ",
1734 entry
->pid
, iter
->cpu
, iter
->ts
))
1738 event
= ftrace_find_event(entry
->type
);
1740 return event
->raw(iter
, 0);
1742 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1745 return TRACE_TYPE_HANDLED
;
1747 return TRACE_TYPE_PARTIAL_LINE
;
1750 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1752 struct trace_seq
*s
= &iter
->seq
;
1753 unsigned char newline
= '\n';
1754 struct trace_entry
*entry
;
1755 struct trace_event
*event
;
1759 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1760 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1761 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1762 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1765 event
= ftrace_find_event(entry
->type
);
1767 enum print_line_t ret
= event
->hex(iter
, 0);
1768 if (ret
!= TRACE_TYPE_HANDLED
)
1772 SEQ_PUT_FIELD_RET(s
, newline
);
1774 return TRACE_TYPE_HANDLED
;
1777 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1779 struct trace_seq
*s
= &iter
->seq
;
1780 struct trace_entry
*entry
;
1781 struct trace_event
*event
;
1785 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1786 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1787 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1788 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1791 event
= ftrace_find_event(entry
->type
);
1792 return event
? event
->binary(iter
, 0) : TRACE_TYPE_HANDLED
;
1795 static int trace_empty(struct trace_iterator
*iter
)
1799 /* If we are looking at one CPU buffer, only check that one */
1800 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
1801 cpu
= iter
->cpu_file
;
1802 if (iter
->buffer_iter
[cpu
]) {
1803 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1806 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1812 for_each_tracing_cpu(cpu
) {
1813 if (iter
->buffer_iter
[cpu
]) {
1814 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1817 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1825 /* Called with trace_event_read_lock() held. */
1826 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1828 enum print_line_t ret
;
1830 if (iter
->trace
&& iter
->trace
->print_line
) {
1831 ret
= iter
->trace
->print_line(iter
);
1832 if (ret
!= TRACE_TYPE_UNHANDLED
)
1836 if (iter
->ent
->type
== TRACE_BPRINT
&&
1837 trace_flags
& TRACE_ITER_PRINTK
&&
1838 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1839 return trace_print_bprintk_msg_only(iter
);
1841 if (iter
->ent
->type
== TRACE_PRINT
&&
1842 trace_flags
& TRACE_ITER_PRINTK
&&
1843 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1844 return trace_print_printk_msg_only(iter
);
1846 if (trace_flags
& TRACE_ITER_BIN
)
1847 return print_bin_fmt(iter
);
1849 if (trace_flags
& TRACE_ITER_HEX
)
1850 return print_hex_fmt(iter
);
1852 if (trace_flags
& TRACE_ITER_RAW
)
1853 return print_raw_fmt(iter
);
1855 return print_trace_fmt(iter
);
1858 static int s_show(struct seq_file
*m
, void *v
)
1860 struct trace_iterator
*iter
= v
;
1862 if (iter
->ent
== NULL
) {
1864 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1867 if (iter
->trace
&& iter
->trace
->print_header
)
1868 iter
->trace
->print_header(m
);
1869 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1870 /* print nothing if the buffers are empty */
1871 if (trace_empty(iter
))
1873 print_trace_header(m
, iter
);
1874 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1875 print_lat_help_header(m
);
1877 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1878 print_func_help_header(m
);
1881 print_trace_line(iter
);
1882 trace_print_seq(m
, &iter
->seq
);
1888 static struct seq_operations tracer_seq_ops
= {
1895 static struct trace_iterator
*
1896 __tracing_open(struct inode
*inode
, struct file
*file
)
1898 long cpu_file
= (long) inode
->i_private
;
1899 void *fail_ret
= ERR_PTR(-ENOMEM
);
1900 struct trace_iterator
*iter
;
1904 if (tracing_disabled
)
1905 return ERR_PTR(-ENODEV
);
1907 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1909 return ERR_PTR(-ENOMEM
);
1912 * We make a copy of the current tracer to avoid concurrent
1913 * changes on it while we are reading.
1915 mutex_lock(&trace_types_lock
);
1916 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
1921 *iter
->trace
= *current_trace
;
1923 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
1926 cpumask_clear(iter
->started
);
1928 if (current_trace
&& current_trace
->print_max
)
1931 iter
->tr
= &global_trace
;
1933 mutex_init(&iter
->mutex
);
1934 iter
->cpu_file
= cpu_file
;
1936 /* Notify the tracer early; before we stop tracing. */
1937 if (iter
->trace
&& iter
->trace
->open
)
1938 iter
->trace
->open(iter
);
1940 /* Annotate start of buffers if we had overruns */
1941 if (ring_buffer_overruns(iter
->tr
->buffer
))
1942 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
1944 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
1945 for_each_tracing_cpu(cpu
) {
1947 iter
->buffer_iter
[cpu
] =
1948 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1951 cpu
= iter
->cpu_file
;
1952 iter
->buffer_iter
[cpu
] =
1953 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1956 /* TODO stop tracer */
1957 ret
= seq_open(file
, &tracer_seq_ops
);
1959 fail_ret
= ERR_PTR(ret
);
1963 m
= file
->private_data
;
1966 /* stop the trace while dumping */
1969 mutex_unlock(&trace_types_lock
);
1974 for_each_tracing_cpu(cpu
) {
1975 if (iter
->buffer_iter
[cpu
])
1976 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1978 free_cpumask_var(iter
->started
);
1980 mutex_unlock(&trace_types_lock
);
1987 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1989 if (tracing_disabled
)
1992 filp
->private_data
= inode
->i_private
;
1996 static int tracing_release(struct inode
*inode
, struct file
*file
)
1998 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1999 struct trace_iterator
*iter
;
2002 if (!(file
->f_mode
& FMODE_READ
))
2007 mutex_lock(&trace_types_lock
);
2008 for_each_tracing_cpu(cpu
) {
2009 if (iter
->buffer_iter
[cpu
])
2010 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2013 if (iter
->trace
&& iter
->trace
->close
)
2014 iter
->trace
->close(iter
);
2016 /* reenable tracing if it was previously enabled */
2018 mutex_unlock(&trace_types_lock
);
2020 seq_release(inode
, file
);
2021 mutex_destroy(&iter
->mutex
);
2022 free_cpumask_var(iter
->started
);
2028 static int tracing_open(struct inode
*inode
, struct file
*file
)
2030 struct trace_iterator
*iter
;
2033 /* If this file was open for write, then erase contents */
2034 if ((file
->f_mode
& FMODE_WRITE
) &&
2035 (file
->f_flags
& O_TRUNC
)) {
2036 long cpu
= (long) inode
->i_private
;
2038 if (cpu
== TRACE_PIPE_ALL_CPU
)
2039 tracing_reset_online_cpus(&global_trace
);
2041 tracing_reset(&global_trace
, cpu
);
2044 if (file
->f_mode
& FMODE_READ
) {
2045 iter
= __tracing_open(inode
, file
);
2047 ret
= PTR_ERR(iter
);
2048 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2049 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2055 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2057 struct tracer
*t
= v
;
2067 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2072 mutex_lock(&trace_types_lock
);
2073 for (t
= trace_types
; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2079 static void t_stop(struct seq_file
*m
, void *p
)
2081 mutex_unlock(&trace_types_lock
);
2084 static int t_show(struct seq_file
*m
, void *v
)
2086 struct tracer
*t
= v
;
2091 seq_printf(m
, "%s", t
->name
);
2100 static struct seq_operations show_traces_seq_ops
= {
2107 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2109 if (tracing_disabled
)
2112 return seq_open(file
, &show_traces_seq_ops
);
2116 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
2117 size_t count
, loff_t
*ppos
)
2122 static const struct file_operations tracing_fops
= {
2123 .open
= tracing_open
,
2125 .write
= tracing_write_stub
,
2126 .llseek
= seq_lseek
,
2127 .release
= tracing_release
,
2130 static const struct file_operations show_traces_fops
= {
2131 .open
= show_traces_open
,
2133 .release
= seq_release
,
2137 * Only trace on a CPU if the bitmask is set:
2139 static cpumask_var_t tracing_cpumask
;
2142 * The tracer itself will not take this lock, but still we want
2143 * to provide a consistent cpumask to user-space:
2145 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2148 * Temporary storage for the character representation of the
2149 * CPU bitmask (and one more byte for the newline):
2151 static char mask_str
[NR_CPUS
+ 1];
2154 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2155 size_t count
, loff_t
*ppos
)
2159 mutex_lock(&tracing_cpumask_update_lock
);
2161 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2162 if (count
- len
< 2) {
2166 len
+= sprintf(mask_str
+ len
, "\n");
2167 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2170 mutex_unlock(&tracing_cpumask_update_lock
);
2176 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2177 size_t count
, loff_t
*ppos
)
2180 cpumask_var_t tracing_cpumask_new
;
2182 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2185 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2189 mutex_lock(&tracing_cpumask_update_lock
);
2191 local_irq_disable();
2192 __raw_spin_lock(&ftrace_max_lock
);
2193 for_each_tracing_cpu(cpu
) {
2195 * Increase/decrease the disabled counter if we are
2196 * about to flip a bit in the cpumask:
2198 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2199 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2200 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2202 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2203 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2204 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2207 __raw_spin_unlock(&ftrace_max_lock
);
2210 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2212 mutex_unlock(&tracing_cpumask_update_lock
);
2213 free_cpumask_var(tracing_cpumask_new
);
2218 free_cpumask_var(tracing_cpumask_new
);
2223 static const struct file_operations tracing_cpumask_fops
= {
2224 .open
= tracing_open_generic
,
2225 .read
= tracing_cpumask_read
,
2226 .write
= tracing_cpumask_write
,
2230 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2231 size_t cnt
, loff_t
*ppos
)
2233 struct tracer_opt
*trace_opts
;
2241 /* calculate max size */
2242 for (i
= 0; trace_options
[i
]; i
++) {
2243 len
+= strlen(trace_options
[i
]);
2244 len
+= 3; /* "no" and newline */
2247 mutex_lock(&trace_types_lock
);
2248 tracer_flags
= current_trace
->flags
->val
;
2249 trace_opts
= current_trace
->flags
->opts
;
2252 * Increase the size with names of options specific
2253 * of the current tracer.
2255 for (i
= 0; trace_opts
[i
].name
; i
++) {
2256 len
+= strlen(trace_opts
[i
].name
);
2257 len
+= 3; /* "no" and newline */
2260 /* +2 for \n and \0 */
2261 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2263 mutex_unlock(&trace_types_lock
);
2267 for (i
= 0; trace_options
[i
]; i
++) {
2268 if (trace_flags
& (1 << i
))
2269 r
+= sprintf(buf
+ r
, "%s\n", trace_options
[i
]);
2271 r
+= sprintf(buf
+ r
, "no%s\n", trace_options
[i
]);
2274 for (i
= 0; trace_opts
[i
].name
; i
++) {
2275 if (tracer_flags
& trace_opts
[i
].bit
)
2276 r
+= sprintf(buf
+ r
, "%s\n",
2277 trace_opts
[i
].name
);
2279 r
+= sprintf(buf
+ r
, "no%s\n",
2280 trace_opts
[i
].name
);
2282 mutex_unlock(&trace_types_lock
);
2284 WARN_ON(r
>= len
+ 2);
2286 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2292 /* Try to assign a tracer specific option */
2293 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2295 struct tracer_flags
*trace_flags
= trace
->flags
;
2296 struct tracer_opt
*opts
= NULL
;
2300 for (i
= 0; trace_flags
->opts
[i
].name
; i
++) {
2301 opts
= &trace_flags
->opts
[i
];
2302 len
= strlen(opts
->name
);
2304 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2305 ret
= trace
->set_flag(trace_flags
->val
,
2311 if (!trace_flags
->opts
[i
].name
)
2314 /* Refused to handle */
2319 trace_flags
->val
&= ~opts
->bit
;
2321 trace_flags
->val
|= opts
->bit
;
2326 static void set_tracer_flags(unsigned int mask
, int enabled
)
2328 /* do nothing if flag is already set */
2329 if (!!(trace_flags
& mask
) == !!enabled
)
2333 trace_flags
|= mask
;
2335 trace_flags
&= ~mask
;
2337 if (mask
== TRACE_ITER_GLOBAL_CLK
) {
2341 func
= trace_clock_global
;
2343 func
= trace_clock_local
;
2345 mutex_lock(&trace_types_lock
);
2346 ring_buffer_set_clock(global_trace
.buffer
, func
);
2349 ring_buffer_set_clock(max_tr
.buffer
, func
);
2350 mutex_unlock(&trace_types_lock
);
2355 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2356 size_t cnt
, loff_t
*ppos
)
2364 if (cnt
>= sizeof(buf
))
2367 if (copy_from_user(&buf
, ubuf
, cnt
))
2372 if (strncmp(buf
, "no", 2) == 0) {
2377 for (i
= 0; trace_options
[i
]; i
++) {
2378 int len
= strlen(trace_options
[i
]);
2380 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2381 set_tracer_flags(1 << i
, !neg
);
2386 /* If no option could be set, test the specific tracer options */
2387 if (!trace_options
[i
]) {
2388 mutex_lock(&trace_types_lock
);
2389 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2390 mutex_unlock(&trace_types_lock
);
2400 static const struct file_operations tracing_iter_fops
= {
2401 .open
= tracing_open_generic
,
2402 .read
= tracing_trace_options_read
,
2403 .write
= tracing_trace_options_write
,
2406 static const char readme_msg
[] =
2407 "tracing mini-HOWTO:\n\n"
2408 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2409 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2410 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2411 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2413 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2414 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2416 "# cat /sys/kernel/debug/tracing/trace_options\n"
2417 "noprint-parent nosym-offset nosym-addr noverbose\n"
2418 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2419 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2420 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2421 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2425 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2426 size_t cnt
, loff_t
*ppos
)
2428 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2429 readme_msg
, strlen(readme_msg
));
2432 static const struct file_operations tracing_readme_fops
= {
2433 .open
= tracing_open_generic
,
2434 .read
= tracing_readme_read
,
2438 tracing_saved_cmdlines_read(struct file
*file
, char __user
*ubuf
,
2439 size_t cnt
, loff_t
*ppos
)
2448 file_buf
= kmalloc(SAVED_CMDLINES
*(16+TASK_COMM_LEN
), GFP_KERNEL
);
2452 buf_comm
= kmalloc(TASK_COMM_LEN
, GFP_KERNEL
);
2460 for (i
= 0; i
< SAVED_CMDLINES
; i
++) {
2463 pid
= map_cmdline_to_pid
[i
];
2464 if (pid
== -1 || pid
== NO_CMDLINE_MAP
)
2467 trace_find_cmdline(pid
, buf_comm
);
2468 r
= sprintf(buf
, "%d %s\n", pid
, buf_comm
);
2473 len
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
2482 static const struct file_operations tracing_saved_cmdlines_fops
= {
2483 .open
= tracing_open_generic
,
2484 .read
= tracing_saved_cmdlines_read
,
2488 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2489 size_t cnt
, loff_t
*ppos
)
2494 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2495 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2499 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2500 size_t cnt
, loff_t
*ppos
)
2502 struct trace_array
*tr
= filp
->private_data
;
2507 if (cnt
>= sizeof(buf
))
2510 if (copy_from_user(&buf
, ubuf
, cnt
))
2515 ret
= strict_strtoul(buf
, 10, &val
);
2521 mutex_lock(&trace_types_lock
);
2522 if (tracer_enabled
^ val
) {
2525 if (current_trace
->start
)
2526 current_trace
->start(tr
);
2531 if (current_trace
->stop
)
2532 current_trace
->stop(tr
);
2535 mutex_unlock(&trace_types_lock
);
2543 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2544 size_t cnt
, loff_t
*ppos
)
2546 char buf
[max_tracer_type_len
+2];
2549 mutex_lock(&trace_types_lock
);
2551 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2553 r
= sprintf(buf
, "\n");
2554 mutex_unlock(&trace_types_lock
);
2556 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2559 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2561 tracing_reset_online_cpus(tr
);
2565 static int tracing_resize_ring_buffer(unsigned long size
)
2570 * If kernel or user changes the size of the ring buffer
2571 * we use the size that was given, and we can forget about
2572 * expanding it later.
2574 ring_buffer_expanded
= 1;
2576 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2580 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2584 r
= ring_buffer_resize(global_trace
.buffer
,
2585 global_trace
.entries
);
2588 * AARGH! We are left with different
2589 * size max buffer!!!!
2590 * The max buffer is our "snapshot" buffer.
2591 * When a tracer needs a snapshot (one of the
2592 * latency tracers), it swaps the max buffer
2593 * with the saved snap shot. We succeeded to
2594 * update the size of the main buffer, but failed to
2595 * update the size of the max buffer. But when we tried
2596 * to reset the main buffer to the original size, we
2597 * failed there too. This is very unlikely to
2598 * happen, but if it does, warn and kill all
2602 tracing_disabled
= 1;
2607 global_trace
.entries
= size
;
2613 * tracing_update_buffers - used by tracing facility to expand ring buffers
2615 * To save on memory when the tracing is never used on a system with it
2616 * configured in. The ring buffers are set to a minimum size. But once
2617 * a user starts to use the tracing facility, then they need to grow
2618 * to their default size.
2620 * This function is to be called when a tracer is about to be used.
2622 int tracing_update_buffers(void)
2626 mutex_lock(&trace_types_lock
);
2627 if (!ring_buffer_expanded
)
2628 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2629 mutex_unlock(&trace_types_lock
);
2634 struct trace_option_dentry
;
2636 static struct trace_option_dentry
*
2637 create_trace_option_files(struct tracer
*tracer
);
2640 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2642 static int tracing_set_tracer(const char *buf
)
2644 static struct trace_option_dentry
*topts
;
2645 struct trace_array
*tr
= &global_trace
;
2649 mutex_lock(&trace_types_lock
);
2651 if (!ring_buffer_expanded
) {
2652 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2658 for (t
= trace_types
; t
; t
= t
->next
) {
2659 if (strcmp(t
->name
, buf
) == 0)
2666 if (t
== current_trace
)
2669 trace_branch_disable();
2670 if (current_trace
&& current_trace
->reset
)
2671 current_trace
->reset(tr
);
2673 destroy_trace_option_files(topts
);
2677 topts
= create_trace_option_files(current_trace
);
2680 ret
= tracer_init(t
, tr
);
2685 trace_branch_enable(tr
);
2687 mutex_unlock(&trace_types_lock
);
2693 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2694 size_t cnt
, loff_t
*ppos
)
2696 char buf
[max_tracer_type_len
+1];
2703 if (cnt
> max_tracer_type_len
)
2704 cnt
= max_tracer_type_len
;
2706 if (copy_from_user(&buf
, ubuf
, cnt
))
2711 /* strip ending whitespace. */
2712 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2715 err
= tracing_set_tracer(buf
);
2725 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2726 size_t cnt
, loff_t
*ppos
)
2728 unsigned long *ptr
= filp
->private_data
;
2732 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2733 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2734 if (r
> sizeof(buf
))
2736 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2740 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2741 size_t cnt
, loff_t
*ppos
)
2743 unsigned long *ptr
= filp
->private_data
;
2748 if (cnt
>= sizeof(buf
))
2751 if (copy_from_user(&buf
, ubuf
, cnt
))
2756 ret
= strict_strtoul(buf
, 10, &val
);
2765 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2767 long cpu_file
= (long) inode
->i_private
;
2768 struct trace_iterator
*iter
;
2771 if (tracing_disabled
)
2774 mutex_lock(&trace_types_lock
);
2776 /* We only allow one reader per cpu */
2777 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
2778 if (!cpumask_empty(tracing_reader_cpumask
)) {
2782 cpumask_setall(tracing_reader_cpumask
);
2784 if (!cpumask_test_cpu(cpu_file
, tracing_reader_cpumask
))
2785 cpumask_set_cpu(cpu_file
, tracing_reader_cpumask
);
2792 /* create a buffer to store the information to pass to userspace */
2793 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2800 * We make a copy of the current tracer to avoid concurrent
2801 * changes on it while we are reading.
2803 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2809 *iter
->trace
= *current_trace
;
2811 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2816 /* trace pipe does not show start of buffer */
2817 cpumask_setall(iter
->started
);
2819 if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2820 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2822 iter
->cpu_file
= cpu_file
;
2823 iter
->tr
= &global_trace
;
2824 mutex_init(&iter
->mutex
);
2825 filp
->private_data
= iter
;
2827 if (iter
->trace
->pipe_open
)
2828 iter
->trace
->pipe_open(iter
);
2831 mutex_unlock(&trace_types_lock
);
2837 mutex_unlock(&trace_types_lock
);
2841 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2843 struct trace_iterator
*iter
= file
->private_data
;
2845 mutex_lock(&trace_types_lock
);
2847 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
)
2848 cpumask_clear(tracing_reader_cpumask
);
2850 cpumask_clear_cpu(iter
->cpu_file
, tracing_reader_cpumask
);
2852 mutex_unlock(&trace_types_lock
);
2854 free_cpumask_var(iter
->started
);
2855 mutex_destroy(&iter
->mutex
);
2863 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2865 struct trace_iterator
*iter
= filp
->private_data
;
2867 if (trace_flags
& TRACE_ITER_BLOCK
) {
2869 * Always select as readable when in blocking mode
2871 return POLLIN
| POLLRDNORM
;
2873 if (!trace_empty(iter
))
2874 return POLLIN
| POLLRDNORM
;
2875 poll_wait(filp
, &trace_wait
, poll_table
);
2876 if (!trace_empty(iter
))
2877 return POLLIN
| POLLRDNORM
;
2884 void default_wait_pipe(struct trace_iterator
*iter
)
2888 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
2890 if (trace_empty(iter
))
2893 finish_wait(&trace_wait
, &wait
);
2897 * This is a make-shift waitqueue.
2898 * A tracer might use this callback on some rare cases:
2900 * 1) the current tracer might hold the runqueue lock when it wakes up
2901 * a reader, hence a deadlock (sched, function, and function graph tracers)
2902 * 2) the function tracers, trace all functions, we don't want
2903 * the overhead of calling wake_up and friends
2904 * (and tracing them too)
2906 * Anyway, this is really very primitive wakeup.
2908 void poll_wait_pipe(struct trace_iterator
*iter
)
2910 set_current_state(TASK_INTERRUPTIBLE
);
2911 /* sleep for 100 msecs, and try again. */
2912 schedule_timeout(HZ
/ 10);
2915 /* Must be called with trace_types_lock mutex held. */
2916 static int tracing_wait_pipe(struct file
*filp
)
2918 struct trace_iterator
*iter
= filp
->private_data
;
2920 while (trace_empty(iter
)) {
2922 if ((filp
->f_flags
& O_NONBLOCK
)) {
2926 mutex_unlock(&iter
->mutex
);
2928 iter
->trace
->wait_pipe(iter
);
2930 mutex_lock(&iter
->mutex
);
2932 if (signal_pending(current
))
2936 * We block until we read something and tracing is disabled.
2937 * We still block if tracing is disabled, but we have never
2938 * read anything. This allows a user to cat this file, and
2939 * then enable tracing. But after we have read something,
2940 * we give an EOF when tracing is again disabled.
2942 * iter->pos will be 0 if we haven't read anything.
2944 if (!tracer_enabled
&& iter
->pos
)
2955 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2956 size_t cnt
, loff_t
*ppos
)
2958 struct trace_iterator
*iter
= filp
->private_data
;
2959 static struct tracer
*old_tracer
;
2962 /* return any leftover data */
2963 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2967 trace_seq_init(&iter
->seq
);
2969 /* copy the tracer to avoid using a global lock all around */
2970 mutex_lock(&trace_types_lock
);
2971 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
2972 old_tracer
= current_trace
;
2973 *iter
->trace
= *current_trace
;
2975 mutex_unlock(&trace_types_lock
);
2978 * Avoid more than one consumer on a single file descriptor
2979 * This is just a matter of traces coherency, the ring buffer itself
2982 mutex_lock(&iter
->mutex
);
2983 if (iter
->trace
->read
) {
2984 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2990 sret
= tracing_wait_pipe(filp
);
2994 /* stop when tracing is finished */
2995 if (trace_empty(iter
)) {
3000 if (cnt
>= PAGE_SIZE
)
3001 cnt
= PAGE_SIZE
- 1;
3003 /* reset all but tr, trace, and overruns */
3004 memset(&iter
->seq
, 0,
3005 sizeof(struct trace_iterator
) -
3006 offsetof(struct trace_iterator
, seq
));
3009 trace_event_read_lock();
3010 while (find_next_entry_inc(iter
) != NULL
) {
3011 enum print_line_t ret
;
3012 int len
= iter
->seq
.len
;
3014 ret
= print_trace_line(iter
);
3015 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3016 /* don't print partial lines */
3017 iter
->seq
.len
= len
;
3020 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3021 trace_consume(iter
);
3023 if (iter
->seq
.len
>= cnt
)
3026 trace_event_read_unlock();
3028 /* Now copy what we have to the user */
3029 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3030 if (iter
->seq
.readpos
>= iter
->seq
.len
)
3031 trace_seq_init(&iter
->seq
);
3034 * If there was nothing to send to user, inspite of consuming trace
3035 * entries, go back to wait for more entries.
3041 mutex_unlock(&iter
->mutex
);
3046 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
3047 struct pipe_buffer
*buf
)
3049 __free_page(buf
->page
);
3052 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
3055 __free_page(spd
->pages
[idx
]);
3058 static struct pipe_buf_operations tracing_pipe_buf_ops
= {
3060 .map
= generic_pipe_buf_map
,
3061 .unmap
= generic_pipe_buf_unmap
,
3062 .confirm
= generic_pipe_buf_confirm
,
3063 .release
= tracing_pipe_buf_release
,
3064 .steal
= generic_pipe_buf_steal
,
3065 .get
= generic_pipe_buf_get
,
3069 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
3074 /* Seq buffer is page-sized, exactly what we need. */
3076 count
= iter
->seq
.len
;
3077 ret
= print_trace_line(iter
);
3078 count
= iter
->seq
.len
- count
;
3081 iter
->seq
.len
-= count
;
3084 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3085 iter
->seq
.len
-= count
;
3089 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3090 trace_consume(iter
);
3092 if (!find_next_entry_inc(iter
)) {
3102 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3104 struct pipe_inode_info
*pipe
,
3108 struct page
*pages
[PIPE_BUFFERS
];
3109 struct partial_page partial
[PIPE_BUFFERS
];
3110 struct trace_iterator
*iter
= filp
->private_data
;
3111 struct splice_pipe_desc spd
= {
3114 .nr_pages
= 0, /* This gets updated below. */
3116 .ops
= &tracing_pipe_buf_ops
,
3117 .spd_release
= tracing_spd_release_pipe
,
3119 static struct tracer
*old_tracer
;
3124 /* copy the tracer to avoid using a global lock all around */
3125 mutex_lock(&trace_types_lock
);
3126 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3127 old_tracer
= current_trace
;
3128 *iter
->trace
= *current_trace
;
3130 mutex_unlock(&trace_types_lock
);
3132 mutex_lock(&iter
->mutex
);
3134 if (iter
->trace
->splice_read
) {
3135 ret
= iter
->trace
->splice_read(iter
, filp
,
3136 ppos
, pipe
, len
, flags
);
3141 ret
= tracing_wait_pipe(filp
);
3145 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
3150 trace_event_read_lock();
3152 /* Fill as many pages as possible. */
3153 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
3154 pages
[i
] = alloc_page(GFP_KERNEL
);
3158 rem
= tracing_fill_pipe_page(rem
, iter
);
3160 /* Copy the data into the page, so we can start over. */
3161 ret
= trace_seq_to_buffer(&iter
->seq
,
3162 page_address(pages
[i
]),
3165 __free_page(pages
[i
]);
3168 partial
[i
].offset
= 0;
3169 partial
[i
].len
= iter
->seq
.len
;
3171 trace_seq_init(&iter
->seq
);
3174 trace_event_read_unlock();
3175 mutex_unlock(&iter
->mutex
);
3179 return splice_to_pipe(pipe
, &spd
);
3182 mutex_unlock(&iter
->mutex
);
3188 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3189 size_t cnt
, loff_t
*ppos
)
3191 struct trace_array
*tr
= filp
->private_data
;
3195 mutex_lock(&trace_types_lock
);
3196 if (!ring_buffer_expanded
)
3197 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3199 trace_buf_size
>> 10);
3201 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3202 mutex_unlock(&trace_types_lock
);
3204 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3208 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3209 size_t cnt
, loff_t
*ppos
)
3215 if (cnt
>= sizeof(buf
))
3218 if (copy_from_user(&buf
, ubuf
, cnt
))
3223 ret
= strict_strtoul(buf
, 10, &val
);
3227 /* must have at least 1 entry */
3231 mutex_lock(&trace_types_lock
);
3235 /* disable all cpu buffers */
3236 for_each_tracing_cpu(cpu
) {
3237 if (global_trace
.data
[cpu
])
3238 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3239 if (max_tr
.data
[cpu
])
3240 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3243 /* value is in KB */
3246 if (val
!= global_trace
.entries
) {
3247 ret
= tracing_resize_ring_buffer(val
);
3256 /* If check pages failed, return ENOMEM */
3257 if (tracing_disabled
)
3260 for_each_tracing_cpu(cpu
) {
3261 if (global_trace
.data
[cpu
])
3262 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3263 if (max_tr
.data
[cpu
])
3264 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3268 max_tr
.entries
= global_trace
.entries
;
3269 mutex_unlock(&trace_types_lock
);
3274 static int mark_printk(const char *fmt
, ...)
3278 va_start(args
, fmt
);
3279 ret
= trace_vprintk(0, fmt
, args
);
3285 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3286 size_t cnt
, loff_t
*fpos
)
3291 if (tracing_disabled
)
3294 if (cnt
> TRACE_BUF_SIZE
)
3295 cnt
= TRACE_BUF_SIZE
;
3297 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3301 if (copy_from_user(buf
, ubuf
, cnt
)) {
3306 /* Cut from the first nil or newline. */
3308 end
= strchr(buf
, '\n');
3312 cnt
= mark_printk("%s\n", buf
);
3319 static const struct file_operations tracing_max_lat_fops
= {
3320 .open
= tracing_open_generic
,
3321 .read
= tracing_max_lat_read
,
3322 .write
= tracing_max_lat_write
,
3325 static const struct file_operations tracing_ctrl_fops
= {
3326 .open
= tracing_open_generic
,
3327 .read
= tracing_ctrl_read
,
3328 .write
= tracing_ctrl_write
,
3331 static const struct file_operations set_tracer_fops
= {
3332 .open
= tracing_open_generic
,
3333 .read
= tracing_set_trace_read
,
3334 .write
= tracing_set_trace_write
,
3337 static const struct file_operations tracing_pipe_fops
= {
3338 .open
= tracing_open_pipe
,
3339 .poll
= tracing_poll_pipe
,
3340 .read
= tracing_read_pipe
,
3341 .splice_read
= tracing_splice_read_pipe
,
3342 .release
= tracing_release_pipe
,
3345 static const struct file_operations tracing_entries_fops
= {
3346 .open
= tracing_open_generic
,
3347 .read
= tracing_entries_read
,
3348 .write
= tracing_entries_write
,
3351 static const struct file_operations tracing_mark_fops
= {
3352 .open
= tracing_open_generic
,
3353 .write
= tracing_mark_write
,
3356 struct ftrace_buffer_info
{
3357 struct trace_array
*tr
;
3363 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3365 int cpu
= (int)(long)inode
->i_private
;
3366 struct ftrace_buffer_info
*info
;
3368 if (tracing_disabled
)
3371 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3375 info
->tr
= &global_trace
;
3378 /* Force reading ring buffer for first read */
3379 info
->read
= (unsigned int)-1;
3381 filp
->private_data
= info
;
3383 return nonseekable_open(inode
, filp
);
3387 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3388 size_t count
, loff_t
*ppos
)
3390 struct ftrace_buffer_info
*info
= filp
->private_data
;
3399 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3403 /* Do we have previous read data to read? */
3404 if (info
->read
< PAGE_SIZE
)
3409 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3416 pos
= ring_buffer_page_len(info
->spare
);
3418 if (pos
< PAGE_SIZE
)
3419 memset(info
->spare
+ pos
, 0, PAGE_SIZE
- pos
);
3422 size
= PAGE_SIZE
- info
->read
;
3426 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3437 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3439 struct ftrace_buffer_info
*info
= file
->private_data
;
3442 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3449 struct ring_buffer
*buffer
;
3454 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3455 struct pipe_buffer
*buf
)
3457 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3462 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3467 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3468 struct pipe_buffer
*buf
)
3473 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3474 struct pipe_buffer
*buf
)
3476 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3481 /* Pipe buffer operations for a buffer. */
3482 static struct pipe_buf_operations buffer_pipe_buf_ops
= {
3484 .map
= generic_pipe_buf_map
,
3485 .unmap
= generic_pipe_buf_unmap
,
3486 .confirm
= generic_pipe_buf_confirm
,
3487 .release
= buffer_pipe_buf_release
,
3488 .steal
= buffer_pipe_buf_steal
,
3489 .get
= buffer_pipe_buf_get
,
3493 * Callback from splice_to_pipe(), if we need to release some pages
3494 * at the end of the spd in case we error'ed out in filling the pipe.
3496 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3498 struct buffer_ref
*ref
=
3499 (struct buffer_ref
*)spd
->partial
[i
].private;
3504 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3506 spd
->partial
[i
].private = 0;
3510 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3511 struct pipe_inode_info
*pipe
, size_t len
,
3514 struct ftrace_buffer_info
*info
= file
->private_data
;
3515 struct partial_page partial
[PIPE_BUFFERS
];
3516 struct page
*pages
[PIPE_BUFFERS
];
3517 struct splice_pipe_desc spd
= {
3521 .ops
= &buffer_pipe_buf_ops
,
3522 .spd_release
= buffer_spd_release
,
3524 struct buffer_ref
*ref
;
3525 int entries
, size
, i
;
3528 if (*ppos
& (PAGE_SIZE
- 1)) {
3529 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3533 if (len
& (PAGE_SIZE
- 1)) {
3534 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3535 if (len
< PAGE_SIZE
)
3540 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3542 for (i
= 0; i
< PIPE_BUFFERS
&& len
&& entries
; i
++, len
-= PAGE_SIZE
) {
3546 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3551 ref
->buffer
= info
->tr
->buffer
;
3552 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3558 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3561 ring_buffer_free_read_page(ref
->buffer
,
3568 * zero out any left over data, this is going to
3571 size
= ring_buffer_page_len(ref
->page
);
3572 if (size
< PAGE_SIZE
)
3573 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3575 page
= virt_to_page(ref
->page
);
3577 spd
.pages
[i
] = page
;
3578 spd
.partial
[i
].len
= PAGE_SIZE
;
3579 spd
.partial
[i
].offset
= 0;
3580 spd
.partial
[i
].private = (unsigned long)ref
;
3584 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3589 /* did we read anything? */
3590 if (!spd
.nr_pages
) {
3591 if (flags
& SPLICE_F_NONBLOCK
)
3599 ret
= splice_to_pipe(pipe
, &spd
);
3604 static const struct file_operations tracing_buffers_fops
= {
3605 .open
= tracing_buffers_open
,
3606 .read
= tracing_buffers_read
,
3607 .release
= tracing_buffers_release
,
3608 .splice_read
= tracing_buffers_splice_read
,
3609 .llseek
= no_llseek
,
3613 tracing_stats_read(struct file
*filp
, char __user
*ubuf
,
3614 size_t count
, loff_t
*ppos
)
3616 unsigned long cpu
= (unsigned long)filp
->private_data
;
3617 struct trace_array
*tr
= &global_trace
;
3618 struct trace_seq
*s
;
3621 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
3627 cnt
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
3628 trace_seq_printf(s
, "entries: %ld\n", cnt
);
3630 cnt
= ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
3631 trace_seq_printf(s
, "overrun: %ld\n", cnt
);
3633 cnt
= ring_buffer_commit_overrun_cpu(tr
->buffer
, cpu
);
3634 trace_seq_printf(s
, "commit overrun: %ld\n", cnt
);
3636 cnt
= ring_buffer_nmi_dropped_cpu(tr
->buffer
, cpu
);
3637 trace_seq_printf(s
, "nmi dropped: %ld\n", cnt
);
3639 count
= simple_read_from_buffer(ubuf
, count
, ppos
, s
->buffer
, s
->len
);
3646 static const struct file_operations tracing_stats_fops
= {
3647 .open
= tracing_open_generic
,
3648 .read
= tracing_stats_read
,
3651 #ifdef CONFIG_DYNAMIC_FTRACE
3653 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3659 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3660 size_t cnt
, loff_t
*ppos
)
3662 static char ftrace_dyn_info_buffer
[1024];
3663 static DEFINE_MUTEX(dyn_info_mutex
);
3664 unsigned long *p
= filp
->private_data
;
3665 char *buf
= ftrace_dyn_info_buffer
;
3666 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3669 mutex_lock(&dyn_info_mutex
);
3670 r
= sprintf(buf
, "%ld ", *p
);
3672 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3675 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3677 mutex_unlock(&dyn_info_mutex
);
3682 static const struct file_operations tracing_dyn_info_fops
= {
3683 .open
= tracing_open_generic
,
3684 .read
= tracing_read_dyn_info
,
3688 static struct dentry
*d_tracer
;
3690 struct dentry
*tracing_init_dentry(void)
3697 if (!debugfs_initialized())
3700 d_tracer
= debugfs_create_dir("tracing", NULL
);
3702 if (!d_tracer
&& !once
) {
3704 pr_warning("Could not create debugfs directory 'tracing'\n");
3711 static struct dentry
*d_percpu
;
3713 struct dentry
*tracing_dentry_percpu(void)
3716 struct dentry
*d_tracer
;
3721 d_tracer
= tracing_init_dentry();
3726 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
3728 if (!d_percpu
&& !once
) {
3730 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3737 static void tracing_init_debugfs_percpu(long cpu
)
3739 struct dentry
*d_percpu
= tracing_dentry_percpu();
3740 struct dentry
*d_cpu
;
3741 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3744 if (cpu
> 999 || cpu
< 0)
3747 sprintf(cpu_dir
, "cpu%ld", cpu
);
3748 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
3750 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
3754 /* per cpu trace_pipe */
3755 trace_create_file("trace_pipe", 0444, d_cpu
,
3756 (void *) cpu
, &tracing_pipe_fops
);
3759 trace_create_file("trace", 0644, d_cpu
,
3760 (void *) cpu
, &tracing_fops
);
3762 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
3763 (void *) cpu
, &tracing_buffers_fops
);
3765 trace_create_file("stats", 0444, d_cpu
,
3766 (void *) cpu
, &tracing_stats_fops
);
3769 #ifdef CONFIG_FTRACE_SELFTEST
3770 /* Let selftest have access to static functions in this file */
3771 #include "trace_selftest.c"
3774 struct trace_option_dentry
{
3775 struct tracer_opt
*opt
;
3776 struct tracer_flags
*flags
;
3777 struct dentry
*entry
;
3781 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3784 struct trace_option_dentry
*topt
= filp
->private_data
;
3787 if (topt
->flags
->val
& topt
->opt
->bit
)
3792 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3796 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3799 struct trace_option_dentry
*topt
= filp
->private_data
;
3804 if (cnt
>= sizeof(buf
))
3807 if (copy_from_user(&buf
, ubuf
, cnt
))
3812 ret
= strict_strtoul(buf
, 10, &val
);
3819 /* do nothing if already cleared */
3820 if (!(topt
->flags
->val
& topt
->opt
->bit
))
3823 mutex_lock(&trace_types_lock
);
3824 if (current_trace
->set_flag
)
3825 ret
= current_trace
->set_flag(topt
->flags
->val
,
3827 mutex_unlock(&trace_types_lock
);
3830 topt
->flags
->val
&= ~topt
->opt
->bit
;
3833 /* do nothing if already set */
3834 if (topt
->flags
->val
& topt
->opt
->bit
)
3837 mutex_lock(&trace_types_lock
);
3838 if (current_trace
->set_flag
)
3839 ret
= current_trace
->set_flag(topt
->flags
->val
,
3841 mutex_unlock(&trace_types_lock
);
3844 topt
->flags
->val
|= topt
->opt
->bit
;
3857 static const struct file_operations trace_options_fops
= {
3858 .open
= tracing_open_generic
,
3859 .read
= trace_options_read
,
3860 .write
= trace_options_write
,
3864 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3867 long index
= (long)filp
->private_data
;
3870 if (trace_flags
& (1 << index
))
3875 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3879 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3882 long index
= (long)filp
->private_data
;
3887 if (cnt
>= sizeof(buf
))
3890 if (copy_from_user(&buf
, ubuf
, cnt
))
3895 ret
= strict_strtoul(buf
, 10, &val
);
3901 trace_flags
&= ~(1 << index
);
3904 trace_flags
|= 1 << index
;
3916 static const struct file_operations trace_options_core_fops
= {
3917 .open
= tracing_open_generic
,
3918 .read
= trace_options_core_read
,
3919 .write
= trace_options_core_write
,
3922 struct dentry
*trace_create_file(const char *name
,
3924 struct dentry
*parent
,
3926 const struct file_operations
*fops
)
3930 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
3932 pr_warning("Could not create debugfs '%s' entry\n", name
);
3938 static struct dentry
*trace_options_init_dentry(void)
3940 struct dentry
*d_tracer
;
3941 static struct dentry
*t_options
;
3946 d_tracer
= tracing_init_dentry();
3950 t_options
= debugfs_create_dir("options", d_tracer
);
3952 pr_warning("Could not create debugfs directory 'options'\n");
3960 create_trace_option_file(struct trace_option_dentry
*topt
,
3961 struct tracer_flags
*flags
,
3962 struct tracer_opt
*opt
)
3964 struct dentry
*t_options
;
3966 t_options
= trace_options_init_dentry();
3970 topt
->flags
= flags
;
3973 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
3974 &trace_options_fops
);
3978 static struct trace_option_dentry
*
3979 create_trace_option_files(struct tracer
*tracer
)
3981 struct trace_option_dentry
*topts
;
3982 struct tracer_flags
*flags
;
3983 struct tracer_opt
*opts
;
3989 flags
= tracer
->flags
;
3991 if (!flags
|| !flags
->opts
)
3996 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3999 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
4003 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4004 create_trace_option_file(&topts
[cnt
], flags
,
4011 destroy_trace_option_files(struct trace_option_dentry
*topts
)
4018 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
4019 if (topts
[cnt
].entry
)
4020 debugfs_remove(topts
[cnt
].entry
);
4026 static struct dentry
*
4027 create_trace_option_core_file(const char *option
, long index
)
4029 struct dentry
*t_options
;
4031 t_options
= trace_options_init_dentry();
4035 return trace_create_file(option
, 0644, t_options
, (void *)index
,
4036 &trace_options_core_fops
);
4039 static __init
void create_trace_options_dir(void)
4041 struct dentry
*t_options
;
4044 t_options
= trace_options_init_dentry();
4048 for (i
= 0; trace_options
[i
]; i
++)
4049 create_trace_option_core_file(trace_options
[i
], i
);
4052 static __init
int tracer_init_debugfs(void)
4054 struct dentry
*d_tracer
;
4057 d_tracer
= tracing_init_dentry();
4059 trace_create_file("tracing_enabled", 0644, d_tracer
,
4060 &global_trace
, &tracing_ctrl_fops
);
4062 trace_create_file("trace_options", 0644, d_tracer
,
4063 NULL
, &tracing_iter_fops
);
4065 trace_create_file("tracing_cpumask", 0644, d_tracer
,
4066 NULL
, &tracing_cpumask_fops
);
4068 trace_create_file("trace", 0644, d_tracer
,
4069 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
4071 trace_create_file("available_tracers", 0444, d_tracer
,
4072 &global_trace
, &show_traces_fops
);
4074 trace_create_file("current_tracer", 0644, d_tracer
,
4075 &global_trace
, &set_tracer_fops
);
4077 trace_create_file("tracing_max_latency", 0644, d_tracer
,
4078 &tracing_max_latency
, &tracing_max_lat_fops
);
4080 trace_create_file("tracing_thresh", 0644, d_tracer
,
4081 &tracing_thresh
, &tracing_max_lat_fops
);
4083 trace_create_file("README", 0444, d_tracer
,
4084 NULL
, &tracing_readme_fops
);
4086 trace_create_file("trace_pipe", 0444, d_tracer
,
4087 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
4089 trace_create_file("buffer_size_kb", 0644, d_tracer
,
4090 &global_trace
, &tracing_entries_fops
);
4092 trace_create_file("trace_marker", 0220, d_tracer
,
4093 NULL
, &tracing_mark_fops
);
4095 trace_create_file("saved_cmdlines", 0444, d_tracer
,
4096 NULL
, &tracing_saved_cmdlines_fops
);
4098 #ifdef CONFIG_DYNAMIC_FTRACE
4099 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
4100 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
4102 #ifdef CONFIG_SYSPROF_TRACER
4103 init_tracer_sysprof_debugfs(d_tracer
);
4106 create_trace_options_dir();
4108 for_each_tracing_cpu(cpu
)
4109 tracing_init_debugfs_percpu(cpu
);
4114 static int trace_panic_handler(struct notifier_block
*this,
4115 unsigned long event
, void *unused
)
4117 if (ftrace_dump_on_oops
)
4122 static struct notifier_block trace_panic_notifier
= {
4123 .notifier_call
= trace_panic_handler
,
4125 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
4128 static int trace_die_handler(struct notifier_block
*self
,
4134 if (ftrace_dump_on_oops
)
4143 static struct notifier_block trace_die_notifier
= {
4144 .notifier_call
= trace_die_handler
,
4149 * printk is set to max of 1024, we really don't need it that big.
4150 * Nothing should be printing 1000 characters anyway.
4152 #define TRACE_MAX_PRINT 1000
4155 * Define here KERN_TRACE so that we have one place to modify
4156 * it if we decide to change what log level the ftrace dump
4159 #define KERN_TRACE KERN_EMERG
4162 trace_printk_seq(struct trace_seq
*s
)
4164 /* Probably should print a warning here. */
4168 /* should be zero ended, but we are paranoid. */
4169 s
->buffer
[s
->len
] = 0;
4171 printk(KERN_TRACE
"%s", s
->buffer
);
4176 static void __ftrace_dump(bool disable_tracing
)
4178 static raw_spinlock_t ftrace_dump_lock
=
4179 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
4180 /* use static because iter can be a bit big for the stack */
4181 static struct trace_iterator iter
;
4182 unsigned int old_userobj
;
4183 static int dump_ran
;
4184 unsigned long flags
;
4188 local_irq_save(flags
);
4189 __raw_spin_lock(&ftrace_dump_lock
);
4197 if (disable_tracing
)
4200 for_each_tracing_cpu(cpu
) {
4201 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
4204 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
4206 /* don't look at user memory in panic mode */
4207 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4209 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4211 /* Simulate the iterator */
4212 iter
.tr
= &global_trace
;
4213 iter
.trace
= current_trace
;
4214 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4217 * We need to stop all tracing on all CPUS to read the
4218 * the next buffer. This is a bit expensive, but is
4219 * not done often. We fill all what we can read,
4220 * and then release the locks again.
4223 while (!trace_empty(&iter
)) {
4226 printk(KERN_TRACE
"---------------------------------\n");
4230 /* reset all but tr, trace, and overruns */
4231 memset(&iter
.seq
, 0,
4232 sizeof(struct trace_iterator
) -
4233 offsetof(struct trace_iterator
, seq
));
4234 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4237 if (find_next_entry_inc(&iter
) != NULL
) {
4240 ret
= print_trace_line(&iter
);
4241 if (ret
!= TRACE_TYPE_NO_CONSUME
)
4242 trace_consume(&iter
);
4245 trace_printk_seq(&iter
.seq
);
4249 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4251 printk(KERN_TRACE
"---------------------------------\n");
4253 /* Re-enable tracing if requested */
4254 if (!disable_tracing
) {
4255 trace_flags
|= old_userobj
;
4257 for_each_tracing_cpu(cpu
) {
4258 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
4264 __raw_spin_unlock(&ftrace_dump_lock
);
4265 local_irq_restore(flags
);
4268 /* By default: disable tracing after the dump */
4269 void ftrace_dump(void)
4271 __ftrace_dump(true);
4274 __init
static int tracer_alloc_buffers(void)
4276 struct trace_array_cpu
*data
;
4281 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4284 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4285 goto out_free_buffer_mask
;
4287 if (!alloc_cpumask_var(&tracing_reader_cpumask
, GFP_KERNEL
))
4288 goto out_free_tracing_cpumask
;
4290 /* To save memory, keep the ring buffer size to its minimum */
4291 if (ring_buffer_expanded
)
4292 ring_buf_size
= trace_buf_size
;
4296 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4297 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4298 cpumask_clear(tracing_reader_cpumask
);
4300 /* TODO: make the number of buffers hot pluggable with CPUS */
4301 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
,
4302 TRACE_BUFFER_FLAGS
);
4303 if (!global_trace
.buffer
) {
4304 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4306 goto out_free_cpumask
;
4308 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4311 #ifdef CONFIG_TRACER_MAX_TRACE
4312 max_tr
.buffer
= ring_buffer_alloc(ring_buf_size
,
4313 TRACE_BUFFER_FLAGS
);
4314 if (!max_tr
.buffer
) {
4315 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4317 ring_buffer_free(global_trace
.buffer
);
4318 goto out_free_cpumask
;
4320 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
4321 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
4324 /* Allocate the first page for all buffers */
4325 for_each_tracing_cpu(i
) {
4326 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4327 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
4330 trace_init_cmdlines();
4332 register_tracer(&nop_trace
);
4333 current_trace
= &nop_trace
;
4334 #ifdef CONFIG_BOOT_TRACER
4335 register_tracer(&boot_tracer
);
4337 /* All seems OK, enable tracing */
4338 tracing_disabled
= 0;
4340 atomic_notifier_chain_register(&panic_notifier_list
,
4341 &trace_panic_notifier
);
4343 register_die_notifier(&trace_die_notifier
);
4348 free_cpumask_var(tracing_reader_cpumask
);
4349 out_free_tracing_cpumask
:
4350 free_cpumask_var(tracing_cpumask
);
4351 out_free_buffer_mask
:
4352 free_cpumask_var(tracing_buffer_mask
);
4357 __init
static int clear_boot_tracer(void)
4360 * The default tracer at boot buffer is an init section.
4361 * This function is called in lateinit. If we did not
4362 * find the boot tracer, then clear it out, to prevent
4363 * later registration from accessing the buffer that is
4364 * about to be freed.
4366 if (!default_bootup_tracer
)
4369 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4370 default_bootup_tracer
);
4371 default_bootup_tracer
= NULL
;
4376 early_initcall(tracer_alloc_buffers
);
4377 fs_initcall(tracer_init_debugfs
);
4378 late_initcall(clear_boot_tracer
);