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/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/ctype.h>
35 #include <linux/init.h>
36 #include <linux/poll.h>
37 #include <linux/gfp.h>
41 #include "trace_output.h"
43 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
45 unsigned long __read_mostly tracing_max_latency
;
46 unsigned long __read_mostly tracing_thresh
;
49 * On boot up, the ring buffer is set to the minimum size, so that
50 * we do not waste memory on systems that are not using tracing.
52 static int ring_buffer_expanded
;
55 * We need to change this state when a selftest is running.
56 * A selftest will lurk into the ring-buffer to count the
57 * entries inserted during the selftest although some concurrent
58 * insertions into the ring-buffer such as trace_printk could occurred
59 * at the same time, giving false positive or negative results.
61 static bool __read_mostly tracing_selftest_running
;
64 * If a tracer is running, we do not want to run SELFTEST.
66 static bool __read_mostly tracing_selftest_disabled
;
68 /* For tracers that don't implement custom flags */
69 static struct tracer_opt dummy_tracer_opt
[] = {
73 static struct tracer_flags dummy_tracer_flags
= {
75 .opts
= dummy_tracer_opt
78 static int dummy_set_flag(u32 old_flags
, u32 bit
, int set
)
84 * Kill all tracing for good (never come back).
85 * It is initialized to 1 but will turn to zero if the initialization
86 * of the tracer is successful. But that is the only place that sets
89 static int tracing_disabled
= 1;
91 static DEFINE_PER_CPU(local_t
, ftrace_cpu_disabled
);
93 static inline void ftrace_disable_cpu(void)
96 local_inc(&__get_cpu_var(ftrace_cpu_disabled
));
99 static inline void ftrace_enable_cpu(void)
101 local_dec(&__get_cpu_var(ftrace_cpu_disabled
));
105 static cpumask_var_t __read_mostly tracing_buffer_mask
;
107 /* Define which cpu buffers are currently read in trace_pipe */
108 static cpumask_var_t tracing_reader_cpumask
;
110 #define for_each_tracing_cpu(cpu) \
111 for_each_cpu(cpu, tracing_buffer_mask)
114 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
116 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
117 * is set, then ftrace_dump is called. This will output the contents
118 * of the ftrace buffers to the console. This is very useful for
119 * capturing traces that lead to crashes and outputing it to a
122 * It is default off, but you can enable it with either specifying
123 * "ftrace_dump_on_oops" in the kernel command line, or setting
124 * /proc/sys/kernel/ftrace_dump_on_oops to true.
126 int ftrace_dump_on_oops
;
128 static int tracing_set_tracer(const char *buf
);
130 #define BOOTUP_TRACER_SIZE 100
131 static char bootup_tracer_buf
[BOOTUP_TRACER_SIZE
] __initdata
;
132 static char *default_bootup_tracer
;
134 static int __init
set_ftrace(char *str
)
136 strncpy(bootup_tracer_buf
, str
, BOOTUP_TRACER_SIZE
);
137 default_bootup_tracer
= bootup_tracer_buf
;
138 /* We are using ftrace early, expand it */
139 ring_buffer_expanded
= 1;
142 __setup("ftrace=", set_ftrace
);
144 static int __init
set_ftrace_dump_on_oops(char *str
)
146 ftrace_dump_on_oops
= 1;
149 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
151 unsigned long long ns2usecs(cycle_t nsec
)
159 * The global_trace is the descriptor that holds the tracing
160 * buffers for the live tracing. For each CPU, it contains
161 * a link list of pages that will store trace entries. The
162 * page descriptor of the pages in the memory is used to hold
163 * the link list by linking the lru item in the page descriptor
164 * to each of the pages in the buffer per CPU.
166 * For each active CPU there is a data field that holds the
167 * pages for the buffer for that CPU. Each CPU has the same number
168 * of pages allocated for its buffer.
170 static struct trace_array global_trace
;
172 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
174 int filter_current_check_discard(struct ftrace_event_call
*call
, void *rec
,
175 struct ring_buffer_event
*event
)
177 return filter_check_discard(call
, rec
, global_trace
.buffer
, event
);
179 EXPORT_SYMBOL_GPL(filter_current_check_discard
);
181 cycle_t
ftrace_now(int cpu
)
185 /* Early boot up does not have a buffer yet */
186 if (!global_trace
.buffer
)
187 return trace_clock_local();
189 ts
= ring_buffer_time_stamp(global_trace
.buffer
, cpu
);
190 ring_buffer_normalize_time_stamp(global_trace
.buffer
, cpu
, &ts
);
196 * The max_tr is used to snapshot the global_trace when a maximum
197 * latency is reached. Some tracers will use this to store a maximum
198 * trace while it continues examining live traces.
200 * The buffers for the max_tr are set up the same as the global_trace.
201 * When a snapshot is taken, the link list of the max_tr is swapped
202 * with the link list of the global_trace and the buffers are reset for
203 * the global_trace so the tracing can continue.
205 static struct trace_array max_tr
;
207 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
209 /* tracer_enabled is used to toggle activation of a tracer */
210 static int tracer_enabled
= 1;
213 * tracing_is_enabled - return tracer_enabled status
215 * This function is used by other tracers to know the status
216 * of the tracer_enabled flag. Tracers may use this function
217 * to know if it should enable their features when starting
218 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
220 int tracing_is_enabled(void)
222 return tracer_enabled
;
226 * trace_buf_size is the size in bytes that is allocated
227 * for a buffer. Note, the number of bytes is always rounded
230 * This number is purposely set to a low number of 16384.
231 * If the dump on oops happens, it will be much appreciated
232 * to not have to wait for all that output. Anyway this can be
233 * boot time and run time configurable.
235 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
237 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
239 /* trace_types holds a link list of available tracers. */
240 static struct tracer
*trace_types __read_mostly
;
242 /* current_trace points to the tracer that is currently active */
243 static struct tracer
*current_trace __read_mostly
;
246 * max_tracer_type_len is used to simplify the allocating of
247 * buffers to read userspace tracer names. We keep track of
248 * the longest tracer name registered.
250 static int max_tracer_type_len
;
253 * trace_types_lock is used to protect the trace_types list.
254 * This lock is also used to keep user access serialized.
255 * Accesses from userspace will grab this lock while userspace
256 * activities happen inside the kernel.
258 static DEFINE_MUTEX(trace_types_lock
);
260 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
261 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
263 /* trace_flags holds trace_options default values */
264 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
265 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
| TRACE_ITER_SLEEP_TIME
|
266 TRACE_ITER_GRAPH_TIME
;
269 * trace_wake_up - wake up tasks waiting for trace input
271 * Simply wakes up any task that is blocked on the trace_wait
272 * queue. These is used with trace_poll for tasks polling the trace.
274 void trace_wake_up(void)
277 * The runqueue_is_locked() can fail, but this is the best we
280 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
281 wake_up(&trace_wait
);
284 static int __init
set_buf_size(char *str
)
286 unsigned long buf_size
;
291 ret
= strict_strtoul(str
, 0, &buf_size
);
292 /* nr_entries can not be zero */
293 if (ret
< 0 || buf_size
== 0)
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 /debugfs/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 #define SAVED_CMDLINES 128
643 #define NO_CMDLINE_MAP UINT_MAX
644 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
645 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
646 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
647 static int cmdline_idx
;
648 static raw_spinlock_t trace_cmdline_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
650 /* temporary disable recording */
651 static atomic_t trace_record_cmdline_disabled __read_mostly
;
653 static void trace_init_cmdlines(void)
655 memset(&map_pid_to_cmdline
, NO_CMDLINE_MAP
, sizeof(map_pid_to_cmdline
));
656 memset(&map_cmdline_to_pid
, NO_CMDLINE_MAP
, sizeof(map_cmdline_to_pid
));
660 static int trace_stop_count
;
661 static DEFINE_SPINLOCK(tracing_start_lock
);
664 * ftrace_off_permanent - disable all ftrace code permanently
666 * This should only be called when a serious anomally has
667 * been detected. This will turn off the function tracing,
668 * ring buffers, and other tracing utilites. It takes no
669 * locks and can be called from any context.
671 void ftrace_off_permanent(void)
673 tracing_disabled
= 1;
675 tracing_off_permanent();
679 * tracing_start - quick start of the tracer
681 * If tracing is enabled but was stopped by tracing_stop,
682 * this will start the tracer back up.
684 void tracing_start(void)
686 struct ring_buffer
*buffer
;
689 if (tracing_disabled
)
692 spin_lock_irqsave(&tracing_start_lock
, flags
);
693 if (--trace_stop_count
) {
694 if (trace_stop_count
< 0) {
695 /* Someone screwed up their debugging */
697 trace_stop_count
= 0;
703 buffer
= global_trace
.buffer
;
705 ring_buffer_record_enable(buffer
);
707 buffer
= max_tr
.buffer
;
709 ring_buffer_record_enable(buffer
);
713 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
717 * tracing_stop - quick stop of the tracer
719 * Light weight way to stop tracing. Use in conjunction with
722 void tracing_stop(void)
724 struct ring_buffer
*buffer
;
728 spin_lock_irqsave(&tracing_start_lock
, flags
);
729 if (trace_stop_count
++)
732 buffer
= global_trace
.buffer
;
734 ring_buffer_record_disable(buffer
);
736 buffer
= max_tr
.buffer
;
738 ring_buffer_record_disable(buffer
);
741 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
744 void trace_stop_cmdline_recording(void);
746 static void trace_save_cmdline(struct task_struct
*tsk
)
750 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
754 * It's not the end of the world if we don't get
755 * the lock, but we also don't want to spin
756 * nor do we want to disable interrupts,
757 * so if we miss here, then better luck next time.
759 if (!__raw_spin_trylock(&trace_cmdline_lock
))
762 idx
= map_pid_to_cmdline
[tsk
->pid
];
763 if (idx
== NO_CMDLINE_MAP
) {
764 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
767 * Check whether the cmdline buffer at idx has a pid
768 * mapped. We are going to overwrite that entry so we
769 * need to clear the map_pid_to_cmdline. Otherwise we
770 * would read the new comm for the old pid.
772 pid
= map_cmdline_to_pid
[idx
];
773 if (pid
!= NO_CMDLINE_MAP
)
774 map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
776 map_cmdline_to_pid
[idx
] = tsk
->pid
;
777 map_pid_to_cmdline
[tsk
->pid
] = idx
;
782 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
784 __raw_spin_unlock(&trace_cmdline_lock
);
787 void trace_find_cmdline(int pid
, char comm
[])
792 strcpy(comm
, "<idle>");
796 if (pid
> PID_MAX_DEFAULT
) {
797 strcpy(comm
, "<...>");
801 __raw_spin_lock(&trace_cmdline_lock
);
802 map
= map_pid_to_cmdline
[pid
];
803 if (map
!= NO_CMDLINE_MAP
)
804 strcpy(comm
, saved_cmdlines
[map
]);
806 strcpy(comm
, "<...>");
808 __raw_spin_unlock(&trace_cmdline_lock
);
811 void tracing_record_cmdline(struct task_struct
*tsk
)
813 if (atomic_read(&trace_record_cmdline_disabled
) || !tracer_enabled
||
817 trace_save_cmdline(tsk
);
821 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
824 struct task_struct
*tsk
= current
;
826 entry
->preempt_count
= pc
& 0xff;
827 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
828 entry
->tgid
= (tsk
) ? tsk
->tgid
: 0;
830 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
831 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
833 TRACE_FLAG_IRQS_NOSUPPORT
|
835 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
836 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
837 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
840 struct ring_buffer_event
*trace_buffer_lock_reserve(struct trace_array
*tr
,
843 unsigned long flags
, int pc
)
845 struct ring_buffer_event
*event
;
847 event
= ring_buffer_lock_reserve(tr
->buffer
, len
);
849 struct trace_entry
*ent
= ring_buffer_event_data(event
);
851 tracing_generic_entry_update(ent
, flags
, pc
);
857 static void ftrace_trace_stack(struct trace_array
*tr
,
858 unsigned long flags
, int skip
, int pc
);
859 static void ftrace_trace_userstack(struct trace_array
*tr
,
860 unsigned long flags
, int pc
);
862 static inline void __trace_buffer_unlock_commit(struct trace_array
*tr
,
863 struct ring_buffer_event
*event
,
864 unsigned long flags
, int pc
,
867 ring_buffer_unlock_commit(tr
->buffer
, event
);
869 ftrace_trace_stack(tr
, flags
, 6, pc
);
870 ftrace_trace_userstack(tr
, flags
, pc
);
876 void trace_buffer_unlock_commit(struct trace_array
*tr
,
877 struct ring_buffer_event
*event
,
878 unsigned long flags
, int pc
)
880 __trace_buffer_unlock_commit(tr
, event
, flags
, pc
, 1);
883 struct ring_buffer_event
*
884 trace_current_buffer_lock_reserve(unsigned char type
, unsigned long len
,
885 unsigned long flags
, int pc
)
887 return trace_buffer_lock_reserve(&global_trace
,
888 type
, len
, flags
, pc
);
890 EXPORT_SYMBOL(trace_current_buffer_lock_reserve
);
892 void trace_current_buffer_unlock_commit(struct ring_buffer_event
*event
,
893 unsigned long flags
, int pc
)
895 __trace_buffer_unlock_commit(&global_trace
, event
, flags
, pc
, 1);
897 EXPORT_SYMBOL(trace_current_buffer_unlock_commit
);
899 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event
*event
,
900 unsigned long flags
, int pc
)
902 __trace_buffer_unlock_commit(&global_trace
, event
, flags
, pc
, 0);
904 EXPORT_SYMBOL(trace_nowake_buffer_unlock_commit
);
906 void trace_current_buffer_discard_commit(struct ring_buffer_event
*event
)
908 ring_buffer_discard_commit(global_trace
.buffer
, event
);
910 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
913 trace_function(struct trace_array
*tr
,
914 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
917 struct ftrace_event_call
*call
= &event_function
;
918 struct ring_buffer_event
*event
;
919 struct ftrace_entry
*entry
;
921 /* If we are reading the ring buffer, don't trace */
922 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
925 event
= trace_buffer_lock_reserve(tr
, TRACE_FN
, sizeof(*entry
),
929 entry
= ring_buffer_event_data(event
);
931 entry
->parent_ip
= parent_ip
;
933 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
934 ring_buffer_unlock_commit(tr
->buffer
, event
);
937 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
938 static int __trace_graph_entry(struct trace_array
*tr
,
939 struct ftrace_graph_ent
*trace
,
943 struct ftrace_event_call
*call
= &event_funcgraph_entry
;
944 struct ring_buffer_event
*event
;
945 struct ftrace_graph_ent_entry
*entry
;
947 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
950 event
= trace_buffer_lock_reserve(&global_trace
, TRACE_GRAPH_ENT
,
951 sizeof(*entry
), flags
, pc
);
954 entry
= ring_buffer_event_data(event
);
955 entry
->graph_ent
= *trace
;
956 if (!filter_current_check_discard(call
, entry
, event
))
957 ring_buffer_unlock_commit(global_trace
.buffer
, event
);
962 static void __trace_graph_return(struct trace_array
*tr
,
963 struct ftrace_graph_ret
*trace
,
967 struct ftrace_event_call
*call
= &event_funcgraph_exit
;
968 struct ring_buffer_event
*event
;
969 struct ftrace_graph_ret_entry
*entry
;
971 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
974 event
= trace_buffer_lock_reserve(&global_trace
, TRACE_GRAPH_RET
,
975 sizeof(*entry
), flags
, pc
);
978 entry
= ring_buffer_event_data(event
);
980 if (!filter_current_check_discard(call
, entry
, event
))
981 ring_buffer_unlock_commit(global_trace
.buffer
, event
);
986 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
987 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
990 if (likely(!atomic_read(&data
->disabled
)))
991 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
994 static void __ftrace_trace_stack(struct trace_array
*tr
,
998 #ifdef CONFIG_STACKTRACE
999 struct ftrace_event_call
*call
= &event_kernel_stack
;
1000 struct ring_buffer_event
*event
;
1001 struct stack_entry
*entry
;
1002 struct stack_trace trace
;
1004 event
= trace_buffer_lock_reserve(tr
, TRACE_STACK
,
1005 sizeof(*entry
), flags
, pc
);
1008 entry
= ring_buffer_event_data(event
);
1009 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1011 trace
.nr_entries
= 0;
1012 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1014 trace
.entries
= entry
->caller
;
1016 save_stack_trace(&trace
);
1017 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1018 ring_buffer_unlock_commit(tr
->buffer
, event
);
1022 static void ftrace_trace_stack(struct trace_array
*tr
,
1023 unsigned long flags
,
1026 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1029 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
1032 void __trace_stack(struct trace_array
*tr
,
1033 unsigned long flags
,
1036 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
1039 static void ftrace_trace_userstack(struct trace_array
*tr
,
1040 unsigned long flags
, int pc
)
1042 #ifdef CONFIG_STACKTRACE
1043 struct ftrace_event_call
*call
= &event_user_stack
;
1044 struct ring_buffer_event
*event
;
1045 struct userstack_entry
*entry
;
1046 struct stack_trace trace
;
1048 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1051 event
= trace_buffer_lock_reserve(tr
, TRACE_USER_STACK
,
1052 sizeof(*entry
), flags
, pc
);
1055 entry
= ring_buffer_event_data(event
);
1057 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1059 trace
.nr_entries
= 0;
1060 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1062 trace
.entries
= entry
->caller
;
1064 save_stack_trace_user(&trace
);
1065 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1066 ring_buffer_unlock_commit(tr
->buffer
, event
);
1071 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1073 ftrace_trace_userstack(tr
, flags
, preempt_count());
1078 ftrace_trace_special(void *__tr
,
1079 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1082 struct ring_buffer_event
*event
;
1083 struct trace_array
*tr
= __tr
;
1084 struct special_entry
*entry
;
1086 event
= trace_buffer_lock_reserve(tr
, TRACE_SPECIAL
,
1087 sizeof(*entry
), 0, pc
);
1090 entry
= ring_buffer_event_data(event
);
1094 trace_buffer_unlock_commit(tr
, event
, 0, pc
);
1098 __trace_special(void *__tr
, void *__data
,
1099 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1101 ftrace_trace_special(__tr
, arg1
, arg2
, arg3
, preempt_count());
1105 tracing_sched_switch_trace(struct trace_array
*tr
,
1106 struct task_struct
*prev
,
1107 struct task_struct
*next
,
1108 unsigned long flags
, int pc
)
1110 struct ftrace_event_call
*call
= &event_context_switch
;
1111 struct ring_buffer_event
*event
;
1112 struct ctx_switch_entry
*entry
;
1114 event
= trace_buffer_lock_reserve(tr
, TRACE_CTX
,
1115 sizeof(*entry
), flags
, pc
);
1118 entry
= ring_buffer_event_data(event
);
1119 entry
->prev_pid
= prev
->pid
;
1120 entry
->prev_prio
= prev
->prio
;
1121 entry
->prev_state
= prev
->state
;
1122 entry
->next_pid
= next
->pid
;
1123 entry
->next_prio
= next
->prio
;
1124 entry
->next_state
= next
->state
;
1125 entry
->next_cpu
= task_cpu(next
);
1127 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1128 trace_buffer_unlock_commit(tr
, event
, flags
, pc
);
1132 tracing_sched_wakeup_trace(struct trace_array
*tr
,
1133 struct task_struct
*wakee
,
1134 struct task_struct
*curr
,
1135 unsigned long flags
, int pc
)
1137 struct ftrace_event_call
*call
= &event_wakeup
;
1138 struct ring_buffer_event
*event
;
1139 struct ctx_switch_entry
*entry
;
1141 event
= trace_buffer_lock_reserve(tr
, TRACE_WAKE
,
1142 sizeof(*entry
), flags
, pc
);
1145 entry
= ring_buffer_event_data(event
);
1146 entry
->prev_pid
= curr
->pid
;
1147 entry
->prev_prio
= curr
->prio
;
1148 entry
->prev_state
= curr
->state
;
1149 entry
->next_pid
= wakee
->pid
;
1150 entry
->next_prio
= wakee
->prio
;
1151 entry
->next_state
= wakee
->state
;
1152 entry
->next_cpu
= task_cpu(wakee
);
1154 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1155 ring_buffer_unlock_commit(tr
->buffer
, event
);
1156 ftrace_trace_stack(tr
, flags
, 6, pc
);
1157 ftrace_trace_userstack(tr
, flags
, pc
);
1161 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1163 struct trace_array
*tr
= &global_trace
;
1164 struct trace_array_cpu
*data
;
1165 unsigned long flags
;
1169 if (tracing_disabled
)
1172 pc
= preempt_count();
1173 local_irq_save(flags
);
1174 cpu
= raw_smp_processor_id();
1175 data
= tr
->data
[cpu
];
1177 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1178 ftrace_trace_special(tr
, arg1
, arg2
, arg3
, pc
);
1180 atomic_dec(&data
->disabled
);
1181 local_irq_restore(flags
);
1184 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1185 int trace_graph_entry(struct ftrace_graph_ent
*trace
)
1187 struct trace_array
*tr
= &global_trace
;
1188 struct trace_array_cpu
*data
;
1189 unsigned long flags
;
1195 if (!ftrace_trace_task(current
))
1198 if (!ftrace_graph_addr(trace
->func
))
1201 local_irq_save(flags
);
1202 cpu
= raw_smp_processor_id();
1203 data
= tr
->data
[cpu
];
1204 disabled
= atomic_inc_return(&data
->disabled
);
1205 if (likely(disabled
== 1)) {
1206 pc
= preempt_count();
1207 ret
= __trace_graph_entry(tr
, trace
, flags
, pc
);
1211 /* Only do the atomic if it is not already set */
1212 if (!test_tsk_trace_graph(current
))
1213 set_tsk_trace_graph(current
);
1215 atomic_dec(&data
->disabled
);
1216 local_irq_restore(flags
);
1221 void trace_graph_return(struct ftrace_graph_ret
*trace
)
1223 struct trace_array
*tr
= &global_trace
;
1224 struct trace_array_cpu
*data
;
1225 unsigned long flags
;
1230 local_irq_save(flags
);
1231 cpu
= raw_smp_processor_id();
1232 data
= tr
->data
[cpu
];
1233 disabled
= atomic_inc_return(&data
->disabled
);
1234 if (likely(disabled
== 1)) {
1235 pc
= preempt_count();
1236 __trace_graph_return(tr
, trace
, flags
, pc
);
1239 clear_tsk_trace_graph(current
);
1240 atomic_dec(&data
->disabled
);
1241 local_irq_restore(flags
);
1243 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1247 * trace_vbprintk - write binary msg to tracing buffer
1250 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1252 static raw_spinlock_t trace_buf_lock
=
1253 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
1254 static u32 trace_buf
[TRACE_BUF_SIZE
];
1256 struct ftrace_event_call
*call
= &event_bprint
;
1257 struct ring_buffer_event
*event
;
1258 struct trace_array
*tr
= &global_trace
;
1259 struct trace_array_cpu
*data
;
1260 struct bprint_entry
*entry
;
1261 unsigned long flags
;
1264 int cpu
, len
= 0, size
, pc
;
1266 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1269 /* Don't pollute graph traces with trace_vprintk internals */
1270 pause_graph_tracing();
1272 pc
= preempt_count();
1273 resched
= ftrace_preempt_disable();
1274 cpu
= raw_smp_processor_id();
1275 data
= tr
->data
[cpu
];
1277 disable
= atomic_inc_return(&data
->disabled
);
1278 if (unlikely(disable
!= 1))
1281 /* Lockdep uses trace_printk for lock tracing */
1282 local_irq_save(flags
);
1283 __raw_spin_lock(&trace_buf_lock
);
1284 len
= vbin_printf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1286 if (len
> TRACE_BUF_SIZE
|| len
< 0)
1289 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1290 event
= trace_buffer_lock_reserve(tr
, TRACE_BPRINT
, size
, flags
, pc
);
1293 entry
= ring_buffer_event_data(event
);
1297 memcpy(entry
->buf
, trace_buf
, sizeof(u32
) * len
);
1298 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1299 ring_buffer_unlock_commit(tr
->buffer
, event
);
1302 __raw_spin_unlock(&trace_buf_lock
);
1303 local_irq_restore(flags
);
1306 atomic_dec_return(&data
->disabled
);
1307 ftrace_preempt_enable(resched
);
1308 unpause_graph_tracing();
1312 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1314 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1316 static raw_spinlock_t trace_buf_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
1317 static char trace_buf
[TRACE_BUF_SIZE
];
1319 struct ftrace_event_call
*call
= &event_print
;
1320 struct ring_buffer_event
*event
;
1321 struct trace_array
*tr
= &global_trace
;
1322 struct trace_array_cpu
*data
;
1323 int cpu
, len
= 0, size
, pc
;
1324 struct print_entry
*entry
;
1325 unsigned long irq_flags
;
1328 if (tracing_disabled
|| tracing_selftest_running
)
1331 pc
= preempt_count();
1332 preempt_disable_notrace();
1333 cpu
= raw_smp_processor_id();
1334 data
= tr
->data
[cpu
];
1336 disable
= atomic_inc_return(&data
->disabled
);
1337 if (unlikely(disable
!= 1))
1340 pause_graph_tracing();
1341 raw_local_irq_save(irq_flags
);
1342 __raw_spin_lock(&trace_buf_lock
);
1343 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1345 len
= min(len
, TRACE_BUF_SIZE
-1);
1348 size
= sizeof(*entry
) + len
+ 1;
1349 event
= trace_buffer_lock_reserve(tr
, TRACE_PRINT
, size
, irq_flags
, pc
);
1352 entry
= ring_buffer_event_data(event
);
1355 memcpy(&entry
->buf
, trace_buf
, len
);
1356 entry
->buf
[len
] = 0;
1357 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1358 ring_buffer_unlock_commit(tr
->buffer
, event
);
1361 __raw_spin_unlock(&trace_buf_lock
);
1362 raw_local_irq_restore(irq_flags
);
1363 unpause_graph_tracing();
1365 atomic_dec_return(&data
->disabled
);
1366 preempt_enable_notrace();
1370 EXPORT_SYMBOL_GPL(trace_vprintk
);
1372 enum trace_file_type
{
1373 TRACE_FILE_LAT_FMT
= 1,
1374 TRACE_FILE_ANNOTATE
= 2,
1377 static void trace_iterator_increment(struct trace_iterator
*iter
)
1379 /* Don't allow ftrace to trace into the ring buffers */
1380 ftrace_disable_cpu();
1383 if (iter
->buffer_iter
[iter
->cpu
])
1384 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1386 ftrace_enable_cpu();
1389 static struct trace_entry
*
1390 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1392 struct ring_buffer_event
*event
;
1393 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1395 /* Don't allow ftrace to trace into the ring buffers */
1396 ftrace_disable_cpu();
1399 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1401 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1403 ftrace_enable_cpu();
1405 return event
? ring_buffer_event_data(event
) : NULL
;
1408 static struct trace_entry
*
1409 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1411 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1412 struct trace_entry
*ent
, *next
= NULL
;
1413 int cpu_file
= iter
->cpu_file
;
1414 u64 next_ts
= 0, ts
;
1419 * If we are in a per_cpu trace file, don't bother by iterating over
1420 * all cpu and peek directly.
1422 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1423 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1425 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
);
1427 *ent_cpu
= cpu_file
;
1432 for_each_tracing_cpu(cpu
) {
1434 if (ring_buffer_empty_cpu(buffer
, cpu
))
1437 ent
= peek_next_entry(iter
, cpu
, &ts
);
1440 * Pick the entry with the smallest timestamp:
1442 if (ent
&& (!next
|| ts
< next_ts
)) {
1450 *ent_cpu
= next_cpu
;
1458 /* Find the next real entry, without updating the iterator itself */
1459 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1460 int *ent_cpu
, u64
*ent_ts
)
1462 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1465 /* Find the next real entry, and increment the iterator to the next entry */
1466 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1468 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1471 trace_iterator_increment(iter
);
1473 return iter
->ent
? iter
: NULL
;
1476 static void trace_consume(struct trace_iterator
*iter
)
1478 /* Don't allow ftrace to trace into the ring buffers */
1479 ftrace_disable_cpu();
1480 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1481 ftrace_enable_cpu();
1484 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1486 struct trace_iterator
*iter
= m
->private;
1492 /* can't go backwards */
1497 ent
= find_next_entry_inc(iter
);
1501 while (ent
&& iter
->idx
< i
)
1502 ent
= find_next_entry_inc(iter
);
1510 * No necessary locking here. The worst thing which can
1511 * happen is loosing events consumed at the same time
1512 * by a trace_pipe reader.
1513 * Other than that, we don't risk to crash the ring buffer
1514 * because it serializes the readers.
1516 * The current tracer is copied to avoid a global locking
1519 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1521 struct trace_iterator
*iter
= m
->private;
1522 static struct tracer
*old_tracer
;
1523 int cpu_file
= iter
->cpu_file
;
1528 /* copy the tracer to avoid using a global lock all around */
1529 mutex_lock(&trace_types_lock
);
1530 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1531 old_tracer
= current_trace
;
1532 *iter
->trace
= *current_trace
;
1534 mutex_unlock(&trace_types_lock
);
1536 atomic_inc(&trace_record_cmdline_disabled
);
1538 if (*pos
!= iter
->pos
) {
1543 ftrace_disable_cpu();
1545 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1546 for_each_tracing_cpu(cpu
)
1547 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1549 ring_buffer_iter_reset(iter
->buffer_iter
[cpu_file
]);
1552 ftrace_enable_cpu();
1554 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1559 p
= s_next(m
, p
, &l
);
1565 static void s_stop(struct seq_file
*m
, void *p
)
1567 atomic_dec(&trace_record_cmdline_disabled
);
1570 static void print_lat_help_header(struct seq_file
*m
)
1572 seq_puts(m
, "# _------=> CPU# \n");
1573 seq_puts(m
, "# / _-----=> irqs-off \n");
1574 seq_puts(m
, "# | / _----=> need-resched \n");
1575 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1576 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1577 seq_puts(m
, "# |||| / \n");
1578 seq_puts(m
, "# ||||| delay \n");
1579 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1580 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1583 static void print_func_help_header(struct seq_file
*m
)
1585 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1586 seq_puts(m
, "# | | | | |\n");
1591 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1593 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1594 struct trace_array
*tr
= iter
->tr
;
1595 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1596 struct tracer
*type
= current_trace
;
1597 unsigned long total
;
1598 unsigned long entries
;
1599 const char *name
= "preemption";
1604 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1606 ring_buffer_overruns(iter
->tr
->buffer
);
1608 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
1610 seq_puts(m
, "# -----------------------------------"
1611 "---------------------------------\n");
1612 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1613 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1614 nsecs_to_usecs(data
->saved_latency
),
1618 #if defined(CONFIG_PREEMPT_NONE)
1620 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1622 #elif defined(CONFIG_PREEMPT)
1627 /* These are reserved for later use */
1630 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1634 seq_puts(m
, "# -----------------\n");
1635 seq_printf(m
, "# | task: %.16s-%d "
1636 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1637 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1638 data
->policy
, data
->rt_priority
);
1639 seq_puts(m
, "# -----------------\n");
1641 if (data
->critical_start
) {
1642 seq_puts(m
, "# => started at: ");
1643 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1644 trace_print_seq(m
, &iter
->seq
);
1645 seq_puts(m
, "\n# => ended at: ");
1646 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1647 trace_print_seq(m
, &iter
->seq
);
1654 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1656 struct trace_seq
*s
= &iter
->seq
;
1658 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1661 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1664 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1667 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1669 /* Don't print started cpu buffer for the first entry of the trace */
1671 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
1675 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1677 struct trace_seq
*s
= &iter
->seq
;
1678 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1679 struct trace_entry
*entry
;
1680 struct trace_event
*event
;
1684 test_cpu_buff_start(iter
);
1686 event
= ftrace_find_event(entry
->type
);
1688 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1689 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1690 if (!trace_print_lat_context(iter
))
1693 if (!trace_print_context(iter
))
1699 return event
->trace(iter
, sym_flags
);
1701 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1704 return TRACE_TYPE_HANDLED
;
1706 return TRACE_TYPE_PARTIAL_LINE
;
1709 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1711 struct trace_seq
*s
= &iter
->seq
;
1712 struct trace_entry
*entry
;
1713 struct trace_event
*event
;
1717 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1718 if (!trace_seq_printf(s
, "%d %d %llu ",
1719 entry
->pid
, iter
->cpu
, iter
->ts
))
1723 event
= ftrace_find_event(entry
->type
);
1725 return event
->raw(iter
, 0);
1727 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1730 return TRACE_TYPE_HANDLED
;
1732 return TRACE_TYPE_PARTIAL_LINE
;
1735 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1737 struct trace_seq
*s
= &iter
->seq
;
1738 unsigned char newline
= '\n';
1739 struct trace_entry
*entry
;
1740 struct trace_event
*event
;
1744 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1745 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1746 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1747 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1750 event
= ftrace_find_event(entry
->type
);
1752 enum print_line_t ret
= event
->hex(iter
, 0);
1753 if (ret
!= TRACE_TYPE_HANDLED
)
1757 SEQ_PUT_FIELD_RET(s
, newline
);
1759 return TRACE_TYPE_HANDLED
;
1762 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1764 struct trace_seq
*s
= &iter
->seq
;
1765 struct trace_entry
*entry
;
1766 struct trace_event
*event
;
1770 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1771 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1772 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1773 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1776 event
= ftrace_find_event(entry
->type
);
1777 return event
? event
->binary(iter
, 0) : TRACE_TYPE_HANDLED
;
1780 static int trace_empty(struct trace_iterator
*iter
)
1784 /* If we are looking at one CPU buffer, only check that one */
1785 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
1786 cpu
= iter
->cpu_file
;
1787 if (iter
->buffer_iter
[cpu
]) {
1788 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1791 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1797 for_each_tracing_cpu(cpu
) {
1798 if (iter
->buffer_iter
[cpu
]) {
1799 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1802 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1810 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1812 enum print_line_t ret
;
1814 if (iter
->trace
&& iter
->trace
->print_line
) {
1815 ret
= iter
->trace
->print_line(iter
);
1816 if (ret
!= TRACE_TYPE_UNHANDLED
)
1820 if (iter
->ent
->type
== TRACE_BPRINT
&&
1821 trace_flags
& TRACE_ITER_PRINTK
&&
1822 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1823 return trace_print_bprintk_msg_only(iter
);
1825 if (iter
->ent
->type
== TRACE_PRINT
&&
1826 trace_flags
& TRACE_ITER_PRINTK
&&
1827 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1828 return trace_print_printk_msg_only(iter
);
1830 if (trace_flags
& TRACE_ITER_BIN
)
1831 return print_bin_fmt(iter
);
1833 if (trace_flags
& TRACE_ITER_HEX
)
1834 return print_hex_fmt(iter
);
1836 if (trace_flags
& TRACE_ITER_RAW
)
1837 return print_raw_fmt(iter
);
1839 return print_trace_fmt(iter
);
1842 static int s_show(struct seq_file
*m
, void *v
)
1844 struct trace_iterator
*iter
= v
;
1846 if (iter
->ent
== NULL
) {
1848 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1851 if (iter
->trace
&& iter
->trace
->print_header
)
1852 iter
->trace
->print_header(m
);
1853 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1854 /* print nothing if the buffers are empty */
1855 if (trace_empty(iter
))
1857 print_trace_header(m
, iter
);
1858 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1859 print_lat_help_header(m
);
1861 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1862 print_func_help_header(m
);
1865 print_trace_line(iter
);
1866 trace_print_seq(m
, &iter
->seq
);
1872 static struct seq_operations tracer_seq_ops
= {
1879 static struct trace_iterator
*
1880 __tracing_open(struct inode
*inode
, struct file
*file
)
1882 long cpu_file
= (long) inode
->i_private
;
1883 void *fail_ret
= ERR_PTR(-ENOMEM
);
1884 struct trace_iterator
*iter
;
1888 if (tracing_disabled
)
1889 return ERR_PTR(-ENODEV
);
1891 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1893 return ERR_PTR(-ENOMEM
);
1896 * We make a copy of the current tracer to avoid concurrent
1897 * changes on it while we are reading.
1899 mutex_lock(&trace_types_lock
);
1900 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
1905 *iter
->trace
= *current_trace
;
1907 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
1910 cpumask_clear(iter
->started
);
1912 if (current_trace
&& current_trace
->print_max
)
1915 iter
->tr
= &global_trace
;
1917 mutex_init(&iter
->mutex
);
1918 iter
->cpu_file
= cpu_file
;
1920 /* Notify the tracer early; before we stop tracing. */
1921 if (iter
->trace
&& iter
->trace
->open
)
1922 iter
->trace
->open(iter
);
1924 /* Annotate start of buffers if we had overruns */
1925 if (ring_buffer_overruns(iter
->tr
->buffer
))
1926 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
1928 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
1929 for_each_tracing_cpu(cpu
) {
1931 iter
->buffer_iter
[cpu
] =
1932 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1935 cpu
= iter
->cpu_file
;
1936 iter
->buffer_iter
[cpu
] =
1937 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1940 /* TODO stop tracer */
1941 ret
= seq_open(file
, &tracer_seq_ops
);
1943 fail_ret
= ERR_PTR(ret
);
1947 m
= file
->private_data
;
1950 /* stop the trace while dumping */
1953 mutex_unlock(&trace_types_lock
);
1958 for_each_tracing_cpu(cpu
) {
1959 if (iter
->buffer_iter
[cpu
])
1960 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1962 free_cpumask_var(iter
->started
);
1964 mutex_unlock(&trace_types_lock
);
1971 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1973 if (tracing_disabled
)
1976 filp
->private_data
= inode
->i_private
;
1980 static int tracing_release(struct inode
*inode
, struct file
*file
)
1982 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1983 struct trace_iterator
*iter
;
1986 if (!(file
->f_mode
& FMODE_READ
))
1991 mutex_lock(&trace_types_lock
);
1992 for_each_tracing_cpu(cpu
) {
1993 if (iter
->buffer_iter
[cpu
])
1994 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1997 if (iter
->trace
&& iter
->trace
->close
)
1998 iter
->trace
->close(iter
);
2000 /* reenable tracing if it was previously enabled */
2002 mutex_unlock(&trace_types_lock
);
2004 seq_release(inode
, file
);
2005 mutex_destroy(&iter
->mutex
);
2006 free_cpumask_var(iter
->started
);
2012 static int tracing_open(struct inode
*inode
, struct file
*file
)
2014 struct trace_iterator
*iter
;
2017 /* If this file was open for write, then erase contents */
2018 if ((file
->f_mode
& FMODE_WRITE
) &&
2019 !(file
->f_flags
& O_APPEND
)) {
2020 long cpu
= (long) inode
->i_private
;
2022 if (cpu
== TRACE_PIPE_ALL_CPU
)
2023 tracing_reset_online_cpus(&global_trace
);
2025 tracing_reset(&global_trace
, cpu
);
2028 if (file
->f_mode
& FMODE_READ
) {
2029 iter
= __tracing_open(inode
, file
);
2031 ret
= PTR_ERR(iter
);
2032 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2033 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2039 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2041 struct tracer
*t
= m
->private;
2053 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2055 struct tracer
*t
= m
->private;
2058 mutex_lock(&trace_types_lock
);
2059 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2065 static void t_stop(struct seq_file
*m
, void *p
)
2067 mutex_unlock(&trace_types_lock
);
2070 static int t_show(struct seq_file
*m
, void *v
)
2072 struct tracer
*t
= v
;
2077 seq_printf(m
, "%s", t
->name
);
2086 static struct seq_operations show_traces_seq_ops
= {
2093 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2097 if (tracing_disabled
)
2100 ret
= seq_open(file
, &show_traces_seq_ops
);
2102 struct seq_file
*m
= file
->private_data
;
2103 m
->private = trace_types
;
2110 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
2111 size_t count
, loff_t
*ppos
)
2116 static const struct file_operations tracing_fops
= {
2117 .open
= tracing_open
,
2119 .write
= tracing_write_stub
,
2120 .llseek
= seq_lseek
,
2121 .release
= tracing_release
,
2124 static const struct file_operations show_traces_fops
= {
2125 .open
= show_traces_open
,
2127 .release
= seq_release
,
2131 * Only trace on a CPU if the bitmask is set:
2133 static cpumask_var_t tracing_cpumask
;
2136 * The tracer itself will not take this lock, but still we want
2137 * to provide a consistent cpumask to user-space:
2139 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2142 * Temporary storage for the character representation of the
2143 * CPU bitmask (and one more byte for the newline):
2145 static char mask_str
[NR_CPUS
+ 1];
2148 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2149 size_t count
, loff_t
*ppos
)
2153 mutex_lock(&tracing_cpumask_update_lock
);
2155 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2156 if (count
- len
< 2) {
2160 len
+= sprintf(mask_str
+ len
, "\n");
2161 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2164 mutex_unlock(&tracing_cpumask_update_lock
);
2170 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2171 size_t count
, loff_t
*ppos
)
2174 cpumask_var_t tracing_cpumask_new
;
2176 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2179 mutex_lock(&tracing_cpumask_update_lock
);
2180 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2184 local_irq_disable();
2185 __raw_spin_lock(&ftrace_max_lock
);
2186 for_each_tracing_cpu(cpu
) {
2188 * Increase/decrease the disabled counter if we are
2189 * about to flip a bit in the cpumask:
2191 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2192 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2193 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2195 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2196 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2197 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2200 __raw_spin_unlock(&ftrace_max_lock
);
2203 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2205 mutex_unlock(&tracing_cpumask_update_lock
);
2206 free_cpumask_var(tracing_cpumask_new
);
2211 mutex_unlock(&tracing_cpumask_update_lock
);
2212 free_cpumask_var(tracing_cpumask
);
2217 static const struct file_operations tracing_cpumask_fops
= {
2218 .open
= tracing_open_generic
,
2219 .read
= tracing_cpumask_read
,
2220 .write
= tracing_cpumask_write
,
2224 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2225 size_t cnt
, loff_t
*ppos
)
2227 struct tracer_opt
*trace_opts
;
2235 /* calculate max size */
2236 for (i
= 0; trace_options
[i
]; i
++) {
2237 len
+= strlen(trace_options
[i
]);
2238 len
+= 3; /* "no" and newline */
2241 mutex_lock(&trace_types_lock
);
2242 tracer_flags
= current_trace
->flags
->val
;
2243 trace_opts
= current_trace
->flags
->opts
;
2246 * Increase the size with names of options specific
2247 * of the current tracer.
2249 for (i
= 0; trace_opts
[i
].name
; i
++) {
2250 len
+= strlen(trace_opts
[i
].name
);
2251 len
+= 3; /* "no" and newline */
2254 /* +2 for \n and \0 */
2255 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2257 mutex_unlock(&trace_types_lock
);
2261 for (i
= 0; trace_options
[i
]; i
++) {
2262 if (trace_flags
& (1 << i
))
2263 r
+= sprintf(buf
+ r
, "%s\n", trace_options
[i
]);
2265 r
+= sprintf(buf
+ r
, "no%s\n", trace_options
[i
]);
2268 for (i
= 0; trace_opts
[i
].name
; i
++) {
2269 if (tracer_flags
& trace_opts
[i
].bit
)
2270 r
+= sprintf(buf
+ r
, "%s\n",
2271 trace_opts
[i
].name
);
2273 r
+= sprintf(buf
+ r
, "no%s\n",
2274 trace_opts
[i
].name
);
2276 mutex_unlock(&trace_types_lock
);
2278 WARN_ON(r
>= len
+ 2);
2280 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2286 /* Try to assign a tracer specific option */
2287 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2289 struct tracer_flags
*trace_flags
= trace
->flags
;
2290 struct tracer_opt
*opts
= NULL
;
2294 for (i
= 0; trace_flags
->opts
[i
].name
; i
++) {
2295 opts
= &trace_flags
->opts
[i
];
2296 len
= strlen(opts
->name
);
2298 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2299 ret
= trace
->set_flag(trace_flags
->val
,
2305 if (!trace_flags
->opts
[i
].name
)
2308 /* Refused to handle */
2313 trace_flags
->val
&= ~opts
->bit
;
2315 trace_flags
->val
|= opts
->bit
;
2320 static void set_tracer_flags(unsigned int mask
, int enabled
)
2322 /* do nothing if flag is already set */
2323 if (!!(trace_flags
& mask
) == !!enabled
)
2327 trace_flags
|= mask
;
2329 trace_flags
&= ~mask
;
2331 if (mask
== TRACE_ITER_GLOBAL_CLK
) {
2335 func
= trace_clock_global
;
2337 func
= trace_clock_local
;
2339 mutex_lock(&trace_types_lock
);
2340 ring_buffer_set_clock(global_trace
.buffer
, func
);
2343 ring_buffer_set_clock(max_tr
.buffer
, func
);
2344 mutex_unlock(&trace_types_lock
);
2349 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2350 size_t cnt
, loff_t
*ppos
)
2358 if (cnt
>= sizeof(buf
))
2361 if (copy_from_user(&buf
, ubuf
, cnt
))
2366 if (strncmp(buf
, "no", 2) == 0) {
2371 for (i
= 0; trace_options
[i
]; i
++) {
2372 int len
= strlen(trace_options
[i
]);
2374 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2375 set_tracer_flags(1 << i
, !neg
);
2380 /* If no option could be set, test the specific tracer options */
2381 if (!trace_options
[i
]) {
2382 mutex_lock(&trace_types_lock
);
2383 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2384 mutex_unlock(&trace_types_lock
);
2394 static const struct file_operations tracing_iter_fops
= {
2395 .open
= tracing_open_generic
,
2396 .read
= tracing_trace_options_read
,
2397 .write
= tracing_trace_options_write
,
2400 static const char readme_msg
[] =
2401 "tracing mini-HOWTO:\n\n"
2403 "# mount -t debugfs nodev /debug\n\n"
2404 "# cat /debug/tracing/available_tracers\n"
2405 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2406 "# cat /debug/tracing/current_tracer\n"
2408 "# echo sched_switch > /debug/tracing/current_tracer\n"
2409 "# cat /debug/tracing/current_tracer\n"
2411 "# cat /debug/tracing/trace_options\n"
2412 "noprint-parent nosym-offset nosym-addr noverbose\n"
2413 "# echo print-parent > /debug/tracing/trace_options\n"
2414 "# echo 1 > /debug/tracing/tracing_enabled\n"
2415 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2416 "echo 0 > /debug/tracing/tracing_enabled\n"
2420 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2421 size_t cnt
, loff_t
*ppos
)
2423 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2424 readme_msg
, strlen(readme_msg
));
2427 static const struct file_operations tracing_readme_fops
= {
2428 .open
= tracing_open_generic
,
2429 .read
= tracing_readme_read
,
2433 tracing_saved_cmdlines_read(struct file
*file
, char __user
*ubuf
,
2434 size_t cnt
, loff_t
*ppos
)
2443 file_buf
= kmalloc(SAVED_CMDLINES
*(16+TASK_COMM_LEN
), GFP_KERNEL
);
2447 buf_comm
= kmalloc(TASK_COMM_LEN
, GFP_KERNEL
);
2455 for (i
= 0; i
< SAVED_CMDLINES
; i
++) {
2458 pid
= map_cmdline_to_pid
[i
];
2459 if (pid
== -1 || pid
== NO_CMDLINE_MAP
)
2462 trace_find_cmdline(pid
, buf_comm
);
2463 r
= sprintf(buf
, "%d %s\n", pid
, buf_comm
);
2468 len
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
2477 static const struct file_operations tracing_saved_cmdlines_fops
= {
2478 .open
= tracing_open_generic
,
2479 .read
= tracing_saved_cmdlines_read
,
2483 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2484 size_t cnt
, loff_t
*ppos
)
2489 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2490 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2494 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2495 size_t cnt
, loff_t
*ppos
)
2497 struct trace_array
*tr
= filp
->private_data
;
2502 if (cnt
>= sizeof(buf
))
2505 if (copy_from_user(&buf
, ubuf
, cnt
))
2510 ret
= strict_strtoul(buf
, 10, &val
);
2516 mutex_lock(&trace_types_lock
);
2517 if (tracer_enabled
^ val
) {
2520 if (current_trace
->start
)
2521 current_trace
->start(tr
);
2526 if (current_trace
->stop
)
2527 current_trace
->stop(tr
);
2530 mutex_unlock(&trace_types_lock
);
2538 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2539 size_t cnt
, loff_t
*ppos
)
2541 char buf
[max_tracer_type_len
+2];
2544 mutex_lock(&trace_types_lock
);
2546 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2548 r
= sprintf(buf
, "\n");
2549 mutex_unlock(&trace_types_lock
);
2551 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2554 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2556 tracing_reset_online_cpus(tr
);
2560 static int tracing_resize_ring_buffer(unsigned long size
)
2565 * If kernel or user changes the size of the ring buffer
2566 * we use the size that was given, and we can forget about
2567 * expanding it later.
2569 ring_buffer_expanded
= 1;
2571 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2575 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2579 r
= ring_buffer_resize(global_trace
.buffer
,
2580 global_trace
.entries
);
2583 * AARGH! We are left with different
2584 * size max buffer!!!!
2585 * The max buffer is our "snapshot" buffer.
2586 * When a tracer needs a snapshot (one of the
2587 * latency tracers), it swaps the max buffer
2588 * with the saved snap shot. We succeeded to
2589 * update the size of the main buffer, but failed to
2590 * update the size of the max buffer. But when we tried
2591 * to reset the main buffer to the original size, we
2592 * failed there too. This is very unlikely to
2593 * happen, but if it does, warn and kill all
2597 tracing_disabled
= 1;
2602 global_trace
.entries
= size
;
2608 * tracing_update_buffers - used by tracing facility to expand ring buffers
2610 * To save on memory when the tracing is never used on a system with it
2611 * configured in. The ring buffers are set to a minimum size. But once
2612 * a user starts to use the tracing facility, then they need to grow
2613 * to their default size.
2615 * This function is to be called when a tracer is about to be used.
2617 int tracing_update_buffers(void)
2621 mutex_lock(&trace_types_lock
);
2622 if (!ring_buffer_expanded
)
2623 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2624 mutex_unlock(&trace_types_lock
);
2629 struct trace_option_dentry
;
2631 static struct trace_option_dentry
*
2632 create_trace_option_files(struct tracer
*tracer
);
2635 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2637 static int tracing_set_tracer(const char *buf
)
2639 static struct trace_option_dentry
*topts
;
2640 struct trace_array
*tr
= &global_trace
;
2644 mutex_lock(&trace_types_lock
);
2646 if (!ring_buffer_expanded
) {
2647 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2653 for (t
= trace_types
; t
; t
= t
->next
) {
2654 if (strcmp(t
->name
, buf
) == 0)
2661 if (t
== current_trace
)
2664 trace_branch_disable();
2665 if (current_trace
&& current_trace
->reset
)
2666 current_trace
->reset(tr
);
2668 destroy_trace_option_files(topts
);
2672 topts
= create_trace_option_files(current_trace
);
2675 ret
= tracer_init(t
, tr
);
2680 trace_branch_enable(tr
);
2682 mutex_unlock(&trace_types_lock
);
2688 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2689 size_t cnt
, loff_t
*ppos
)
2691 char buf
[max_tracer_type_len
+1];
2698 if (cnt
> max_tracer_type_len
)
2699 cnt
= max_tracer_type_len
;
2701 if (copy_from_user(&buf
, ubuf
, cnt
))
2706 /* strip ending whitespace. */
2707 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2710 err
= tracing_set_tracer(buf
);
2720 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2721 size_t cnt
, loff_t
*ppos
)
2723 unsigned long *ptr
= filp
->private_data
;
2727 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2728 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2729 if (r
> sizeof(buf
))
2731 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2735 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2736 size_t cnt
, loff_t
*ppos
)
2738 unsigned long *ptr
= filp
->private_data
;
2743 if (cnt
>= sizeof(buf
))
2746 if (copy_from_user(&buf
, ubuf
, cnt
))
2751 ret
= strict_strtoul(buf
, 10, &val
);
2760 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2762 long cpu_file
= (long) inode
->i_private
;
2763 struct trace_iterator
*iter
;
2766 if (tracing_disabled
)
2769 mutex_lock(&trace_types_lock
);
2771 /* We only allow one reader per cpu */
2772 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
2773 if (!cpumask_empty(tracing_reader_cpumask
)) {
2777 cpumask_setall(tracing_reader_cpumask
);
2779 if (!cpumask_test_cpu(cpu_file
, tracing_reader_cpumask
))
2780 cpumask_set_cpu(cpu_file
, tracing_reader_cpumask
);
2787 /* create a buffer to store the information to pass to userspace */
2788 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2795 * We make a copy of the current tracer to avoid concurrent
2796 * changes on it while we are reading.
2798 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2804 *iter
->trace
= *current_trace
;
2806 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2811 /* trace pipe does not show start of buffer */
2812 cpumask_setall(iter
->started
);
2814 iter
->cpu_file
= cpu_file
;
2815 iter
->tr
= &global_trace
;
2816 mutex_init(&iter
->mutex
);
2817 filp
->private_data
= iter
;
2819 if (iter
->trace
->pipe_open
)
2820 iter
->trace
->pipe_open(iter
);
2823 mutex_unlock(&trace_types_lock
);
2829 mutex_unlock(&trace_types_lock
);
2833 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2835 struct trace_iterator
*iter
= file
->private_data
;
2837 mutex_lock(&trace_types_lock
);
2839 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
)
2840 cpumask_clear(tracing_reader_cpumask
);
2842 cpumask_clear_cpu(iter
->cpu_file
, tracing_reader_cpumask
);
2844 mutex_unlock(&trace_types_lock
);
2846 free_cpumask_var(iter
->started
);
2847 mutex_destroy(&iter
->mutex
);
2855 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2857 struct trace_iterator
*iter
= filp
->private_data
;
2859 if (trace_flags
& TRACE_ITER_BLOCK
) {
2861 * Always select as readable when in blocking mode
2863 return POLLIN
| POLLRDNORM
;
2865 if (!trace_empty(iter
))
2866 return POLLIN
| POLLRDNORM
;
2867 poll_wait(filp
, &trace_wait
, poll_table
);
2868 if (!trace_empty(iter
))
2869 return POLLIN
| POLLRDNORM
;
2876 void default_wait_pipe(struct trace_iterator
*iter
)
2880 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
2882 if (trace_empty(iter
))
2885 finish_wait(&trace_wait
, &wait
);
2889 * This is a make-shift waitqueue.
2890 * A tracer might use this callback on some rare cases:
2892 * 1) the current tracer might hold the runqueue lock when it wakes up
2893 * a reader, hence a deadlock (sched, function, and function graph tracers)
2894 * 2) the function tracers, trace all functions, we don't want
2895 * the overhead of calling wake_up and friends
2896 * (and tracing them too)
2898 * Anyway, this is really very primitive wakeup.
2900 void poll_wait_pipe(struct trace_iterator
*iter
)
2902 set_current_state(TASK_INTERRUPTIBLE
);
2903 /* sleep for 100 msecs, and try again. */
2904 schedule_timeout(HZ
/ 10);
2907 /* Must be called with trace_types_lock mutex held. */
2908 static int tracing_wait_pipe(struct file
*filp
)
2910 struct trace_iterator
*iter
= filp
->private_data
;
2912 while (trace_empty(iter
)) {
2914 if ((filp
->f_flags
& O_NONBLOCK
)) {
2918 mutex_unlock(&iter
->mutex
);
2920 iter
->trace
->wait_pipe(iter
);
2922 mutex_lock(&iter
->mutex
);
2924 if (signal_pending(current
))
2928 * We block until we read something and tracing is disabled.
2929 * We still block if tracing is disabled, but we have never
2930 * read anything. This allows a user to cat this file, and
2931 * then enable tracing. But after we have read something,
2932 * we give an EOF when tracing is again disabled.
2934 * iter->pos will be 0 if we haven't read anything.
2936 if (!tracer_enabled
&& iter
->pos
)
2947 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2948 size_t cnt
, loff_t
*ppos
)
2950 struct trace_iterator
*iter
= filp
->private_data
;
2951 static struct tracer
*old_tracer
;
2954 /* return any leftover data */
2955 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2959 trace_seq_init(&iter
->seq
);
2961 /* copy the tracer to avoid using a global lock all around */
2962 mutex_lock(&trace_types_lock
);
2963 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
2964 old_tracer
= current_trace
;
2965 *iter
->trace
= *current_trace
;
2967 mutex_unlock(&trace_types_lock
);
2970 * Avoid more than one consumer on a single file descriptor
2971 * This is just a matter of traces coherency, the ring buffer itself
2974 mutex_lock(&iter
->mutex
);
2975 if (iter
->trace
->read
) {
2976 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2982 sret
= tracing_wait_pipe(filp
);
2986 /* stop when tracing is finished */
2987 if (trace_empty(iter
)) {
2992 if (cnt
>= PAGE_SIZE
)
2993 cnt
= PAGE_SIZE
- 1;
2995 /* reset all but tr, trace, and overruns */
2996 memset(&iter
->seq
, 0,
2997 sizeof(struct trace_iterator
) -
2998 offsetof(struct trace_iterator
, seq
));
3001 while (find_next_entry_inc(iter
) != NULL
) {
3002 enum print_line_t ret
;
3003 int len
= iter
->seq
.len
;
3005 ret
= print_trace_line(iter
);
3006 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3007 /* don't print partial lines */
3008 iter
->seq
.len
= len
;
3011 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3012 trace_consume(iter
);
3014 if (iter
->seq
.len
>= cnt
)
3018 /* Now copy what we have to the user */
3019 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3020 if (iter
->seq
.readpos
>= iter
->seq
.len
)
3021 trace_seq_init(&iter
->seq
);
3024 * If there was nothing to send to user, inspite of consuming trace
3025 * entries, go back to wait for more entries.
3031 mutex_unlock(&iter
->mutex
);
3036 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
3037 struct pipe_buffer
*buf
)
3039 __free_page(buf
->page
);
3042 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
3045 __free_page(spd
->pages
[idx
]);
3048 static struct pipe_buf_operations tracing_pipe_buf_ops
= {
3050 .map
= generic_pipe_buf_map
,
3051 .unmap
= generic_pipe_buf_unmap
,
3052 .confirm
= generic_pipe_buf_confirm
,
3053 .release
= tracing_pipe_buf_release
,
3054 .steal
= generic_pipe_buf_steal
,
3055 .get
= generic_pipe_buf_get
,
3059 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
3064 /* Seq buffer is page-sized, exactly what we need. */
3066 count
= iter
->seq
.len
;
3067 ret
= print_trace_line(iter
);
3068 count
= iter
->seq
.len
- count
;
3071 iter
->seq
.len
-= count
;
3074 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3075 iter
->seq
.len
-= count
;
3079 trace_consume(iter
);
3081 if (!find_next_entry_inc(iter
)) {
3091 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3093 struct pipe_inode_info
*pipe
,
3097 struct page
*pages
[PIPE_BUFFERS
];
3098 struct partial_page partial
[PIPE_BUFFERS
];
3099 struct trace_iterator
*iter
= filp
->private_data
;
3100 struct splice_pipe_desc spd
= {
3103 .nr_pages
= 0, /* This gets updated below. */
3105 .ops
= &tracing_pipe_buf_ops
,
3106 .spd_release
= tracing_spd_release_pipe
,
3108 static struct tracer
*old_tracer
;
3113 /* copy the tracer to avoid using a global lock all around */
3114 mutex_lock(&trace_types_lock
);
3115 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3116 old_tracer
= current_trace
;
3117 *iter
->trace
= *current_trace
;
3119 mutex_unlock(&trace_types_lock
);
3121 mutex_lock(&iter
->mutex
);
3123 if (iter
->trace
->splice_read
) {
3124 ret
= iter
->trace
->splice_read(iter
, filp
,
3125 ppos
, pipe
, len
, flags
);
3130 ret
= tracing_wait_pipe(filp
);
3134 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
3139 /* Fill as many pages as possible. */
3140 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
3141 pages
[i
] = alloc_page(GFP_KERNEL
);
3145 rem
= tracing_fill_pipe_page(rem
, iter
);
3147 /* Copy the data into the page, so we can start over. */
3148 ret
= trace_seq_to_buffer(&iter
->seq
,
3149 page_address(pages
[i
]),
3152 __free_page(pages
[i
]);
3155 partial
[i
].offset
= 0;
3156 partial
[i
].len
= iter
->seq
.len
;
3158 trace_seq_init(&iter
->seq
);
3161 mutex_unlock(&iter
->mutex
);
3165 return splice_to_pipe(pipe
, &spd
);
3168 mutex_unlock(&iter
->mutex
);
3174 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3175 size_t cnt
, loff_t
*ppos
)
3177 struct trace_array
*tr
= filp
->private_data
;
3181 mutex_lock(&trace_types_lock
);
3182 if (!ring_buffer_expanded
)
3183 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3185 trace_buf_size
>> 10);
3187 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3188 mutex_unlock(&trace_types_lock
);
3190 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3194 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3195 size_t cnt
, loff_t
*ppos
)
3201 if (cnt
>= sizeof(buf
))
3204 if (copy_from_user(&buf
, ubuf
, cnt
))
3209 ret
= strict_strtoul(buf
, 10, &val
);
3213 /* must have at least 1 entry */
3217 mutex_lock(&trace_types_lock
);
3221 /* disable all cpu buffers */
3222 for_each_tracing_cpu(cpu
) {
3223 if (global_trace
.data
[cpu
])
3224 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3225 if (max_tr
.data
[cpu
])
3226 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3229 /* value is in KB */
3232 if (val
!= global_trace
.entries
) {
3233 ret
= tracing_resize_ring_buffer(val
);
3242 /* If check pages failed, return ENOMEM */
3243 if (tracing_disabled
)
3246 for_each_tracing_cpu(cpu
) {
3247 if (global_trace
.data
[cpu
])
3248 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3249 if (max_tr
.data
[cpu
])
3250 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3254 max_tr
.entries
= global_trace
.entries
;
3255 mutex_unlock(&trace_types_lock
);
3260 static int mark_printk(const char *fmt
, ...)
3264 va_start(args
, fmt
);
3265 ret
= trace_vprintk(0, fmt
, args
);
3271 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3272 size_t cnt
, loff_t
*fpos
)
3277 if (tracing_disabled
)
3280 if (cnt
> TRACE_BUF_SIZE
)
3281 cnt
= TRACE_BUF_SIZE
;
3283 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3287 if (copy_from_user(buf
, ubuf
, cnt
)) {
3292 /* Cut from the first nil or newline. */
3294 end
= strchr(buf
, '\n');
3298 cnt
= mark_printk("%s\n", buf
);
3305 static const struct file_operations tracing_max_lat_fops
= {
3306 .open
= tracing_open_generic
,
3307 .read
= tracing_max_lat_read
,
3308 .write
= tracing_max_lat_write
,
3311 static const struct file_operations tracing_ctrl_fops
= {
3312 .open
= tracing_open_generic
,
3313 .read
= tracing_ctrl_read
,
3314 .write
= tracing_ctrl_write
,
3317 static const struct file_operations set_tracer_fops
= {
3318 .open
= tracing_open_generic
,
3319 .read
= tracing_set_trace_read
,
3320 .write
= tracing_set_trace_write
,
3323 static const struct file_operations tracing_pipe_fops
= {
3324 .open
= tracing_open_pipe
,
3325 .poll
= tracing_poll_pipe
,
3326 .read
= tracing_read_pipe
,
3327 .splice_read
= tracing_splice_read_pipe
,
3328 .release
= tracing_release_pipe
,
3331 static const struct file_operations tracing_entries_fops
= {
3332 .open
= tracing_open_generic
,
3333 .read
= tracing_entries_read
,
3334 .write
= tracing_entries_write
,
3337 static const struct file_operations tracing_mark_fops
= {
3338 .open
= tracing_open_generic
,
3339 .write
= tracing_mark_write
,
3342 struct ftrace_buffer_info
{
3343 struct trace_array
*tr
;
3349 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3351 int cpu
= (int)(long)inode
->i_private
;
3352 struct ftrace_buffer_info
*info
;
3354 if (tracing_disabled
)
3357 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3361 info
->tr
= &global_trace
;
3364 /* Force reading ring buffer for first read */
3365 info
->read
= (unsigned int)-1;
3367 filp
->private_data
= info
;
3369 return nonseekable_open(inode
, filp
);
3373 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3374 size_t count
, loff_t
*ppos
)
3376 struct ftrace_buffer_info
*info
= filp
->private_data
;
3385 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3389 /* Do we have previous read data to read? */
3390 if (info
->read
< PAGE_SIZE
)
3395 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3402 pos
= ring_buffer_page_len(info
->spare
);
3404 if (pos
< PAGE_SIZE
)
3405 memset(info
->spare
+ pos
, 0, PAGE_SIZE
- pos
);
3408 size
= PAGE_SIZE
- info
->read
;
3412 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3423 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3425 struct ftrace_buffer_info
*info
= file
->private_data
;
3428 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3435 struct ring_buffer
*buffer
;
3440 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3441 struct pipe_buffer
*buf
)
3443 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3448 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3453 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3454 struct pipe_buffer
*buf
)
3459 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3460 struct pipe_buffer
*buf
)
3462 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3467 /* Pipe buffer operations for a buffer. */
3468 static struct pipe_buf_operations buffer_pipe_buf_ops
= {
3470 .map
= generic_pipe_buf_map
,
3471 .unmap
= generic_pipe_buf_unmap
,
3472 .confirm
= generic_pipe_buf_confirm
,
3473 .release
= buffer_pipe_buf_release
,
3474 .steal
= buffer_pipe_buf_steal
,
3475 .get
= buffer_pipe_buf_get
,
3479 * Callback from splice_to_pipe(), if we need to release some pages
3480 * at the end of the spd in case we error'ed out in filling the pipe.
3482 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3484 struct buffer_ref
*ref
=
3485 (struct buffer_ref
*)spd
->partial
[i
].private;
3490 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3492 spd
->partial
[i
].private = 0;
3496 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3497 struct pipe_inode_info
*pipe
, size_t len
,
3500 struct ftrace_buffer_info
*info
= file
->private_data
;
3501 struct partial_page partial
[PIPE_BUFFERS
];
3502 struct page
*pages
[PIPE_BUFFERS
];
3503 struct splice_pipe_desc spd
= {
3507 .ops
= &buffer_pipe_buf_ops
,
3508 .spd_release
= buffer_spd_release
,
3510 struct buffer_ref
*ref
;
3514 if (*ppos
& (PAGE_SIZE
- 1)) {
3515 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3519 if (len
& (PAGE_SIZE
- 1)) {
3520 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3521 if (len
< PAGE_SIZE
)
3526 for (i
= 0; i
< PIPE_BUFFERS
&& len
; i
++, len
-= PAGE_SIZE
) {
3530 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3534 ref
->buffer
= info
->tr
->buffer
;
3535 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3541 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3544 ring_buffer_free_read_page(ref
->buffer
,
3551 * zero out any left over data, this is going to
3554 size
= ring_buffer_page_len(ref
->page
);
3555 if (size
< PAGE_SIZE
)
3556 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3558 page
= virt_to_page(ref
->page
);
3560 spd
.pages
[i
] = page
;
3561 spd
.partial
[i
].len
= PAGE_SIZE
;
3562 spd
.partial
[i
].offset
= 0;
3563 spd
.partial
[i
].private = (unsigned long)ref
;
3570 /* did we read anything? */
3571 if (!spd
.nr_pages
) {
3572 if (flags
& SPLICE_F_NONBLOCK
)
3580 ret
= splice_to_pipe(pipe
, &spd
);
3585 static const struct file_operations tracing_buffers_fops
= {
3586 .open
= tracing_buffers_open
,
3587 .read
= tracing_buffers_read
,
3588 .release
= tracing_buffers_release
,
3589 .splice_read
= tracing_buffers_splice_read
,
3590 .llseek
= no_llseek
,
3593 #ifdef CONFIG_DYNAMIC_FTRACE
3595 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3601 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3602 size_t cnt
, loff_t
*ppos
)
3604 static char ftrace_dyn_info_buffer
[1024];
3605 static DEFINE_MUTEX(dyn_info_mutex
);
3606 unsigned long *p
= filp
->private_data
;
3607 char *buf
= ftrace_dyn_info_buffer
;
3608 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3611 mutex_lock(&dyn_info_mutex
);
3612 r
= sprintf(buf
, "%ld ", *p
);
3614 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3617 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3619 mutex_unlock(&dyn_info_mutex
);
3624 static const struct file_operations tracing_dyn_info_fops
= {
3625 .open
= tracing_open_generic
,
3626 .read
= tracing_read_dyn_info
,
3630 static struct dentry
*d_tracer
;
3632 struct dentry
*tracing_init_dentry(void)
3639 if (!debugfs_initialized())
3642 d_tracer
= debugfs_create_dir("tracing", NULL
);
3644 if (!d_tracer
&& !once
) {
3646 pr_warning("Could not create debugfs directory 'tracing'\n");
3653 static struct dentry
*d_percpu
;
3655 struct dentry
*tracing_dentry_percpu(void)
3658 struct dentry
*d_tracer
;
3663 d_tracer
= tracing_init_dentry();
3668 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
3670 if (!d_percpu
&& !once
) {
3672 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3679 static void tracing_init_debugfs_percpu(long cpu
)
3681 struct dentry
*d_percpu
= tracing_dentry_percpu();
3682 struct dentry
*d_cpu
;
3683 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3686 if (cpu
> 999 || cpu
< 0)
3689 sprintf(cpu_dir
, "cpu%ld", cpu
);
3690 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
3692 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
3696 /* per cpu trace_pipe */
3697 trace_create_file("trace_pipe", 0444, d_cpu
,
3698 (void *) cpu
, &tracing_pipe_fops
);
3701 trace_create_file("trace", 0644, d_cpu
,
3702 (void *) cpu
, &tracing_fops
);
3704 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
3705 (void *) cpu
, &tracing_buffers_fops
);
3708 #ifdef CONFIG_FTRACE_SELFTEST
3709 /* Let selftest have access to static functions in this file */
3710 #include "trace_selftest.c"
3713 struct trace_option_dentry
{
3714 struct tracer_opt
*opt
;
3715 struct tracer_flags
*flags
;
3716 struct dentry
*entry
;
3720 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3723 struct trace_option_dentry
*topt
= filp
->private_data
;
3726 if (topt
->flags
->val
& topt
->opt
->bit
)
3731 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3735 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3738 struct trace_option_dentry
*topt
= filp
->private_data
;
3743 if (cnt
>= sizeof(buf
))
3746 if (copy_from_user(&buf
, ubuf
, cnt
))
3751 ret
= strict_strtoul(buf
, 10, &val
);
3758 /* do nothing if already cleared */
3759 if (!(topt
->flags
->val
& topt
->opt
->bit
))
3762 mutex_lock(&trace_types_lock
);
3763 if (current_trace
->set_flag
)
3764 ret
= current_trace
->set_flag(topt
->flags
->val
,
3766 mutex_unlock(&trace_types_lock
);
3769 topt
->flags
->val
&= ~topt
->opt
->bit
;
3772 /* do nothing if already set */
3773 if (topt
->flags
->val
& topt
->opt
->bit
)
3776 mutex_lock(&trace_types_lock
);
3777 if (current_trace
->set_flag
)
3778 ret
= current_trace
->set_flag(topt
->flags
->val
,
3780 mutex_unlock(&trace_types_lock
);
3783 topt
->flags
->val
|= topt
->opt
->bit
;
3796 static const struct file_operations trace_options_fops
= {
3797 .open
= tracing_open_generic
,
3798 .read
= trace_options_read
,
3799 .write
= trace_options_write
,
3803 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3806 long index
= (long)filp
->private_data
;
3809 if (trace_flags
& (1 << index
))
3814 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3818 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3821 long index
= (long)filp
->private_data
;
3826 if (cnt
>= sizeof(buf
))
3829 if (copy_from_user(&buf
, ubuf
, cnt
))
3834 ret
= strict_strtoul(buf
, 10, &val
);
3840 trace_flags
&= ~(1 << index
);
3843 trace_flags
|= 1 << index
;
3855 static const struct file_operations trace_options_core_fops
= {
3856 .open
= tracing_open_generic
,
3857 .read
= trace_options_core_read
,
3858 .write
= trace_options_core_write
,
3861 struct dentry
*trace_create_file(const char *name
,
3863 struct dentry
*parent
,
3865 const struct file_operations
*fops
)
3869 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
3871 pr_warning("Could not create debugfs '%s' entry\n", name
);
3877 static struct dentry
*trace_options_init_dentry(void)
3879 struct dentry
*d_tracer
;
3880 static struct dentry
*t_options
;
3885 d_tracer
= tracing_init_dentry();
3889 t_options
= debugfs_create_dir("options", d_tracer
);
3891 pr_warning("Could not create debugfs directory 'options'\n");
3899 create_trace_option_file(struct trace_option_dentry
*topt
,
3900 struct tracer_flags
*flags
,
3901 struct tracer_opt
*opt
)
3903 struct dentry
*t_options
;
3905 t_options
= trace_options_init_dentry();
3909 topt
->flags
= flags
;
3912 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
3913 &trace_options_fops
);
3917 static struct trace_option_dentry
*
3918 create_trace_option_files(struct tracer
*tracer
)
3920 struct trace_option_dentry
*topts
;
3921 struct tracer_flags
*flags
;
3922 struct tracer_opt
*opts
;
3928 flags
= tracer
->flags
;
3930 if (!flags
|| !flags
->opts
)
3935 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3938 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
3942 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3943 create_trace_option_file(&topts
[cnt
], flags
,
3950 destroy_trace_option_files(struct trace_option_dentry
*topts
)
3957 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
3958 if (topts
[cnt
].entry
)
3959 debugfs_remove(topts
[cnt
].entry
);
3965 static struct dentry
*
3966 create_trace_option_core_file(const char *option
, long index
)
3968 struct dentry
*t_options
;
3970 t_options
= trace_options_init_dentry();
3974 return trace_create_file(option
, 0644, t_options
, (void *)index
,
3975 &trace_options_core_fops
);
3978 static __init
void create_trace_options_dir(void)
3980 struct dentry
*t_options
;
3983 t_options
= trace_options_init_dentry();
3987 for (i
= 0; trace_options
[i
]; i
++)
3988 create_trace_option_core_file(trace_options
[i
], i
);
3991 static __init
int tracer_init_debugfs(void)
3993 struct dentry
*d_tracer
;
3996 d_tracer
= tracing_init_dentry();
3998 trace_create_file("tracing_enabled", 0644, d_tracer
,
3999 &global_trace
, &tracing_ctrl_fops
);
4001 trace_create_file("trace_options", 0644, d_tracer
,
4002 NULL
, &tracing_iter_fops
);
4004 trace_create_file("tracing_cpumask", 0644, d_tracer
,
4005 NULL
, &tracing_cpumask_fops
);
4007 trace_create_file("trace", 0644, d_tracer
,
4008 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
4010 trace_create_file("available_tracers", 0444, d_tracer
,
4011 &global_trace
, &show_traces_fops
);
4013 trace_create_file("current_tracer", 0644, d_tracer
,
4014 &global_trace
, &set_tracer_fops
);
4016 trace_create_file("tracing_max_latency", 0644, d_tracer
,
4017 &tracing_max_latency
, &tracing_max_lat_fops
);
4019 trace_create_file("tracing_thresh", 0644, d_tracer
,
4020 &tracing_thresh
, &tracing_max_lat_fops
);
4022 trace_create_file("README", 0444, d_tracer
,
4023 NULL
, &tracing_readme_fops
);
4025 trace_create_file("trace_pipe", 0444, d_tracer
,
4026 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
4028 trace_create_file("buffer_size_kb", 0644, d_tracer
,
4029 &global_trace
, &tracing_entries_fops
);
4031 trace_create_file("trace_marker", 0220, d_tracer
,
4032 NULL
, &tracing_mark_fops
);
4034 trace_create_file("saved_cmdlines", 0444, d_tracer
,
4035 NULL
, &tracing_saved_cmdlines_fops
);
4037 #ifdef CONFIG_DYNAMIC_FTRACE
4038 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
4039 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
4041 #ifdef CONFIG_SYSPROF_TRACER
4042 init_tracer_sysprof_debugfs(d_tracer
);
4045 create_trace_options_dir();
4047 for_each_tracing_cpu(cpu
)
4048 tracing_init_debugfs_percpu(cpu
);
4053 static int trace_panic_handler(struct notifier_block
*this,
4054 unsigned long event
, void *unused
)
4056 if (ftrace_dump_on_oops
)
4061 static struct notifier_block trace_panic_notifier
= {
4062 .notifier_call
= trace_panic_handler
,
4064 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
4067 static int trace_die_handler(struct notifier_block
*self
,
4073 if (ftrace_dump_on_oops
)
4082 static struct notifier_block trace_die_notifier
= {
4083 .notifier_call
= trace_die_handler
,
4088 * printk is set to max of 1024, we really don't need it that big.
4089 * Nothing should be printing 1000 characters anyway.
4091 #define TRACE_MAX_PRINT 1000
4094 * Define here KERN_TRACE so that we have one place to modify
4095 * it if we decide to change what log level the ftrace dump
4098 #define KERN_TRACE KERN_EMERG
4101 trace_printk_seq(struct trace_seq
*s
)
4103 /* Probably should print a warning here. */
4107 /* should be zero ended, but we are paranoid. */
4108 s
->buffer
[s
->len
] = 0;
4110 printk(KERN_TRACE
"%s", s
->buffer
);
4115 static void __ftrace_dump(bool disable_tracing
)
4117 static DEFINE_SPINLOCK(ftrace_dump_lock
);
4118 /* use static because iter can be a bit big for the stack */
4119 static struct trace_iterator iter
;
4120 unsigned int old_userobj
;
4121 static int dump_ran
;
4122 unsigned long flags
;
4126 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
4134 if (disable_tracing
)
4137 for_each_tracing_cpu(cpu
) {
4138 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
4141 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
4143 /* don't look at user memory in panic mode */
4144 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4146 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4148 /* Simulate the iterator */
4149 iter
.tr
= &global_trace
;
4150 iter
.trace
= current_trace
;
4151 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4154 * We need to stop all tracing on all CPUS to read the
4155 * the next buffer. This is a bit expensive, but is
4156 * not done often. We fill all what we can read,
4157 * and then release the locks again.
4160 while (!trace_empty(&iter
)) {
4163 printk(KERN_TRACE
"---------------------------------\n");
4167 /* reset all but tr, trace, and overruns */
4168 memset(&iter
.seq
, 0,
4169 sizeof(struct trace_iterator
) -
4170 offsetof(struct trace_iterator
, seq
));
4171 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4174 if (find_next_entry_inc(&iter
) != NULL
) {
4175 print_trace_line(&iter
);
4176 trace_consume(&iter
);
4179 trace_printk_seq(&iter
.seq
);
4183 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4185 printk(KERN_TRACE
"---------------------------------\n");
4187 /* Re-enable tracing if requested */
4188 if (!disable_tracing
) {
4189 trace_flags
|= old_userobj
;
4191 for_each_tracing_cpu(cpu
) {
4192 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
4198 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
4201 /* By default: disable tracing after the dump */
4202 void ftrace_dump(void)
4204 __ftrace_dump(true);
4207 __init
static int tracer_alloc_buffers(void)
4209 struct trace_array_cpu
*data
;
4214 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4217 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4218 goto out_free_buffer_mask
;
4220 if (!alloc_cpumask_var(&tracing_reader_cpumask
, GFP_KERNEL
))
4221 goto out_free_tracing_cpumask
;
4223 /* To save memory, keep the ring buffer size to its minimum */
4224 if (ring_buffer_expanded
)
4225 ring_buf_size
= trace_buf_size
;
4229 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4230 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4231 cpumask_clear(tracing_reader_cpumask
);
4233 /* TODO: make the number of buffers hot pluggable with CPUS */
4234 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
,
4235 TRACE_BUFFER_FLAGS
);
4236 if (!global_trace
.buffer
) {
4237 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4239 goto out_free_cpumask
;
4241 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4244 #ifdef CONFIG_TRACER_MAX_TRACE
4245 max_tr
.buffer
= ring_buffer_alloc(ring_buf_size
,
4246 TRACE_BUFFER_FLAGS
);
4247 if (!max_tr
.buffer
) {
4248 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4250 ring_buffer_free(global_trace
.buffer
);
4251 goto out_free_cpumask
;
4253 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
4254 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
4257 /* Allocate the first page for all buffers */
4258 for_each_tracing_cpu(i
) {
4259 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4260 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
4263 trace_init_cmdlines();
4265 register_tracer(&nop_trace
);
4266 current_trace
= &nop_trace
;
4267 #ifdef CONFIG_BOOT_TRACER
4268 register_tracer(&boot_tracer
);
4270 /* All seems OK, enable tracing */
4271 tracing_disabled
= 0;
4273 atomic_notifier_chain_register(&panic_notifier_list
,
4274 &trace_panic_notifier
);
4276 register_die_notifier(&trace_die_notifier
);
4281 free_cpumask_var(tracing_reader_cpumask
);
4282 out_free_tracing_cpumask
:
4283 free_cpumask_var(tracing_cpumask
);
4284 out_free_buffer_mask
:
4285 free_cpumask_var(tracing_buffer_mask
);
4290 __init
static int clear_boot_tracer(void)
4293 * The default tracer at boot buffer is an init section.
4294 * This function is called in lateinit. If we did not
4295 * find the boot tracer, then clear it out, to prevent
4296 * later registration from accessing the buffer that is
4297 * about to be freed.
4299 if (!default_bootup_tracer
)
4302 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4303 default_bootup_tracer
);
4304 default_bootup_tracer
= NULL
;
4309 early_initcall(tracer_alloc_buffers
);
4310 fs_initcall(tracer_init_debugfs
);
4311 late_initcall(clear_boot_tracer
);