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);
898 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event
*event
,
899 unsigned long flags
, int pc
)
901 __trace_buffer_unlock_commit(&global_trace
, event
, flags
, pc
, 0);
904 void trace_current_buffer_discard_commit(struct ring_buffer_event
*event
)
906 ring_buffer_discard_commit(global_trace
.buffer
, event
);
908 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit
);
911 trace_function(struct trace_array
*tr
,
912 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
915 struct ftrace_event_call
*call
= &event_function
;
916 struct ring_buffer_event
*event
;
917 struct ftrace_entry
*entry
;
919 /* If we are reading the ring buffer, don't trace */
920 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
923 event
= trace_buffer_lock_reserve(tr
, TRACE_FN
, sizeof(*entry
),
927 entry
= ring_buffer_event_data(event
);
929 entry
->parent_ip
= parent_ip
;
931 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
932 ring_buffer_unlock_commit(tr
->buffer
, event
);
935 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
936 static int __trace_graph_entry(struct trace_array
*tr
,
937 struct ftrace_graph_ent
*trace
,
941 struct ftrace_event_call
*call
= &event_funcgraph_entry
;
942 struct ring_buffer_event
*event
;
943 struct ftrace_graph_ent_entry
*entry
;
945 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
948 event
= trace_buffer_lock_reserve(&global_trace
, TRACE_GRAPH_ENT
,
949 sizeof(*entry
), flags
, pc
);
952 entry
= ring_buffer_event_data(event
);
953 entry
->graph_ent
= *trace
;
954 if (!filter_current_check_discard(call
, entry
, event
))
955 ring_buffer_unlock_commit(global_trace
.buffer
, event
);
960 static void __trace_graph_return(struct trace_array
*tr
,
961 struct ftrace_graph_ret
*trace
,
965 struct ftrace_event_call
*call
= &event_funcgraph_exit
;
966 struct ring_buffer_event
*event
;
967 struct ftrace_graph_ret_entry
*entry
;
969 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
972 event
= trace_buffer_lock_reserve(&global_trace
, TRACE_GRAPH_RET
,
973 sizeof(*entry
), flags
, pc
);
976 entry
= ring_buffer_event_data(event
);
978 if (!filter_current_check_discard(call
, entry
, event
))
979 ring_buffer_unlock_commit(global_trace
.buffer
, event
);
984 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
985 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
988 if (likely(!atomic_read(&data
->disabled
)))
989 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
992 static void __ftrace_trace_stack(struct trace_array
*tr
,
996 #ifdef CONFIG_STACKTRACE
997 struct ftrace_event_call
*call
= &event_kernel_stack
;
998 struct ring_buffer_event
*event
;
999 struct stack_entry
*entry
;
1000 struct stack_trace trace
;
1002 event
= trace_buffer_lock_reserve(tr
, TRACE_STACK
,
1003 sizeof(*entry
), flags
, pc
);
1006 entry
= ring_buffer_event_data(event
);
1007 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1009 trace
.nr_entries
= 0;
1010 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1012 trace
.entries
= entry
->caller
;
1014 save_stack_trace(&trace
);
1015 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1016 ring_buffer_unlock_commit(tr
->buffer
, event
);
1020 static void ftrace_trace_stack(struct trace_array
*tr
,
1021 unsigned long flags
,
1024 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1027 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
1030 void __trace_stack(struct trace_array
*tr
,
1031 unsigned long flags
,
1034 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
1037 static void ftrace_trace_userstack(struct trace_array
*tr
,
1038 unsigned long flags
, int pc
)
1040 #ifdef CONFIG_STACKTRACE
1041 struct ftrace_event_call
*call
= &event_user_stack
;
1042 struct ring_buffer_event
*event
;
1043 struct userstack_entry
*entry
;
1044 struct stack_trace trace
;
1046 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1049 event
= trace_buffer_lock_reserve(tr
, TRACE_USER_STACK
,
1050 sizeof(*entry
), flags
, pc
);
1053 entry
= ring_buffer_event_data(event
);
1055 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1057 trace
.nr_entries
= 0;
1058 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1060 trace
.entries
= entry
->caller
;
1062 save_stack_trace_user(&trace
);
1063 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1064 ring_buffer_unlock_commit(tr
->buffer
, event
);
1069 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1071 ftrace_trace_userstack(tr
, flags
, preempt_count());
1076 ftrace_trace_special(void *__tr
,
1077 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1080 struct ring_buffer_event
*event
;
1081 struct trace_array
*tr
= __tr
;
1082 struct special_entry
*entry
;
1084 event
= trace_buffer_lock_reserve(tr
, TRACE_SPECIAL
,
1085 sizeof(*entry
), 0, pc
);
1088 entry
= ring_buffer_event_data(event
);
1092 trace_buffer_unlock_commit(tr
, event
, 0, pc
);
1096 __trace_special(void *__tr
, void *__data
,
1097 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1099 ftrace_trace_special(__tr
, arg1
, arg2
, arg3
, preempt_count());
1103 tracing_sched_switch_trace(struct trace_array
*tr
,
1104 struct task_struct
*prev
,
1105 struct task_struct
*next
,
1106 unsigned long flags
, int pc
)
1108 struct ftrace_event_call
*call
= &event_context_switch
;
1109 struct ring_buffer_event
*event
;
1110 struct ctx_switch_entry
*entry
;
1112 event
= trace_buffer_lock_reserve(tr
, TRACE_CTX
,
1113 sizeof(*entry
), flags
, pc
);
1116 entry
= ring_buffer_event_data(event
);
1117 entry
->prev_pid
= prev
->pid
;
1118 entry
->prev_prio
= prev
->prio
;
1119 entry
->prev_state
= prev
->state
;
1120 entry
->next_pid
= next
->pid
;
1121 entry
->next_prio
= next
->prio
;
1122 entry
->next_state
= next
->state
;
1123 entry
->next_cpu
= task_cpu(next
);
1125 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1126 trace_buffer_unlock_commit(tr
, event
, flags
, pc
);
1130 tracing_sched_wakeup_trace(struct trace_array
*tr
,
1131 struct task_struct
*wakee
,
1132 struct task_struct
*curr
,
1133 unsigned long flags
, int pc
)
1135 struct ftrace_event_call
*call
= &event_wakeup
;
1136 struct ring_buffer_event
*event
;
1137 struct ctx_switch_entry
*entry
;
1139 event
= trace_buffer_lock_reserve(tr
, TRACE_WAKE
,
1140 sizeof(*entry
), flags
, pc
);
1143 entry
= ring_buffer_event_data(event
);
1144 entry
->prev_pid
= curr
->pid
;
1145 entry
->prev_prio
= curr
->prio
;
1146 entry
->prev_state
= curr
->state
;
1147 entry
->next_pid
= wakee
->pid
;
1148 entry
->next_prio
= wakee
->prio
;
1149 entry
->next_state
= wakee
->state
;
1150 entry
->next_cpu
= task_cpu(wakee
);
1152 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1153 ring_buffer_unlock_commit(tr
->buffer
, event
);
1154 ftrace_trace_stack(tr
, flags
, 6, pc
);
1155 ftrace_trace_userstack(tr
, flags
, pc
);
1159 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1161 struct trace_array
*tr
= &global_trace
;
1162 struct trace_array_cpu
*data
;
1163 unsigned long flags
;
1167 if (tracing_disabled
)
1170 pc
= preempt_count();
1171 local_irq_save(flags
);
1172 cpu
= raw_smp_processor_id();
1173 data
= tr
->data
[cpu
];
1175 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1176 ftrace_trace_special(tr
, arg1
, arg2
, arg3
, pc
);
1178 atomic_dec(&data
->disabled
);
1179 local_irq_restore(flags
);
1182 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1183 int trace_graph_entry(struct ftrace_graph_ent
*trace
)
1185 struct trace_array
*tr
= &global_trace
;
1186 struct trace_array_cpu
*data
;
1187 unsigned long flags
;
1193 if (!ftrace_trace_task(current
))
1196 if (!ftrace_graph_addr(trace
->func
))
1199 local_irq_save(flags
);
1200 cpu
= raw_smp_processor_id();
1201 data
= tr
->data
[cpu
];
1202 disabled
= atomic_inc_return(&data
->disabled
);
1203 if (likely(disabled
== 1)) {
1204 pc
= preempt_count();
1205 ret
= __trace_graph_entry(tr
, trace
, flags
, pc
);
1209 /* Only do the atomic if it is not already set */
1210 if (!test_tsk_trace_graph(current
))
1211 set_tsk_trace_graph(current
);
1213 atomic_dec(&data
->disabled
);
1214 local_irq_restore(flags
);
1219 void trace_graph_return(struct ftrace_graph_ret
*trace
)
1221 struct trace_array
*tr
= &global_trace
;
1222 struct trace_array_cpu
*data
;
1223 unsigned long flags
;
1228 local_irq_save(flags
);
1229 cpu
= raw_smp_processor_id();
1230 data
= tr
->data
[cpu
];
1231 disabled
= atomic_inc_return(&data
->disabled
);
1232 if (likely(disabled
== 1)) {
1233 pc
= preempt_count();
1234 __trace_graph_return(tr
, trace
, flags
, pc
);
1237 clear_tsk_trace_graph(current
);
1238 atomic_dec(&data
->disabled
);
1239 local_irq_restore(flags
);
1241 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1245 * trace_vbprintk - write binary msg to tracing buffer
1248 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1250 static raw_spinlock_t trace_buf_lock
=
1251 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
1252 static u32 trace_buf
[TRACE_BUF_SIZE
];
1254 struct ftrace_event_call
*call
= &event_bprint
;
1255 struct ring_buffer_event
*event
;
1256 struct trace_array
*tr
= &global_trace
;
1257 struct trace_array_cpu
*data
;
1258 struct bprint_entry
*entry
;
1259 unsigned long flags
;
1261 int cpu
, len
= 0, size
, pc
;
1263 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1266 /* Don't pollute graph traces with trace_vprintk internals */
1267 pause_graph_tracing();
1269 pc
= preempt_count();
1270 resched
= ftrace_preempt_disable();
1271 cpu
= raw_smp_processor_id();
1272 data
= tr
->data
[cpu
];
1274 if (unlikely(atomic_read(&data
->disabled
)))
1277 /* Lockdep uses trace_printk for lock tracing */
1278 local_irq_save(flags
);
1279 __raw_spin_lock(&trace_buf_lock
);
1280 len
= vbin_printf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1282 if (len
> TRACE_BUF_SIZE
|| len
< 0)
1285 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1286 event
= trace_buffer_lock_reserve(tr
, TRACE_BPRINT
, size
, flags
, pc
);
1289 entry
= ring_buffer_event_data(event
);
1293 memcpy(entry
->buf
, trace_buf
, sizeof(u32
) * len
);
1294 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1295 ring_buffer_unlock_commit(tr
->buffer
, event
);
1298 __raw_spin_unlock(&trace_buf_lock
);
1299 local_irq_restore(flags
);
1302 ftrace_preempt_enable(resched
);
1303 unpause_graph_tracing();
1307 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1309 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1311 static raw_spinlock_t trace_buf_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
1312 static char trace_buf
[TRACE_BUF_SIZE
];
1314 struct ftrace_event_call
*call
= &event_print
;
1315 struct ring_buffer_event
*event
;
1316 struct trace_array
*tr
= &global_trace
;
1317 struct trace_array_cpu
*data
;
1318 int cpu
, len
= 0, size
, pc
;
1319 struct print_entry
*entry
;
1320 unsigned long irq_flags
;
1322 if (tracing_disabled
|| tracing_selftest_running
)
1325 pc
= preempt_count();
1326 preempt_disable_notrace();
1327 cpu
= raw_smp_processor_id();
1328 data
= tr
->data
[cpu
];
1330 if (unlikely(atomic_read(&data
->disabled
)))
1333 pause_graph_tracing();
1334 raw_local_irq_save(irq_flags
);
1335 __raw_spin_lock(&trace_buf_lock
);
1336 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1338 len
= min(len
, TRACE_BUF_SIZE
-1);
1341 size
= sizeof(*entry
) + len
+ 1;
1342 event
= trace_buffer_lock_reserve(tr
, TRACE_PRINT
, size
, irq_flags
, pc
);
1345 entry
= ring_buffer_event_data(event
);
1348 memcpy(&entry
->buf
, trace_buf
, len
);
1349 entry
->buf
[len
] = 0;
1350 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1351 ring_buffer_unlock_commit(tr
->buffer
, event
);
1354 __raw_spin_unlock(&trace_buf_lock
);
1355 raw_local_irq_restore(irq_flags
);
1356 unpause_graph_tracing();
1358 preempt_enable_notrace();
1362 EXPORT_SYMBOL_GPL(trace_vprintk
);
1364 enum trace_file_type
{
1365 TRACE_FILE_LAT_FMT
= 1,
1366 TRACE_FILE_ANNOTATE
= 2,
1369 static void trace_iterator_increment(struct trace_iterator
*iter
)
1371 /* Don't allow ftrace to trace into the ring buffers */
1372 ftrace_disable_cpu();
1375 if (iter
->buffer_iter
[iter
->cpu
])
1376 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1378 ftrace_enable_cpu();
1381 static struct trace_entry
*
1382 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1384 struct ring_buffer_event
*event
;
1385 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1387 /* Don't allow ftrace to trace into the ring buffers */
1388 ftrace_disable_cpu();
1391 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1393 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1395 ftrace_enable_cpu();
1397 return event
? ring_buffer_event_data(event
) : NULL
;
1400 static struct trace_entry
*
1401 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1403 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1404 struct trace_entry
*ent
, *next
= NULL
;
1405 int cpu_file
= iter
->cpu_file
;
1406 u64 next_ts
= 0, ts
;
1411 * If we are in a per_cpu trace file, don't bother by iterating over
1412 * all cpu and peek directly.
1414 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1415 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1417 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
);
1419 *ent_cpu
= cpu_file
;
1424 for_each_tracing_cpu(cpu
) {
1426 if (ring_buffer_empty_cpu(buffer
, cpu
))
1429 ent
= peek_next_entry(iter
, cpu
, &ts
);
1432 * Pick the entry with the smallest timestamp:
1434 if (ent
&& (!next
|| ts
< next_ts
)) {
1442 *ent_cpu
= next_cpu
;
1450 /* Find the next real entry, without updating the iterator itself */
1451 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1452 int *ent_cpu
, u64
*ent_ts
)
1454 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1457 /* Find the next real entry, and increment the iterator to the next entry */
1458 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1460 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1463 trace_iterator_increment(iter
);
1465 return iter
->ent
? iter
: NULL
;
1468 static void trace_consume(struct trace_iterator
*iter
)
1470 /* Don't allow ftrace to trace into the ring buffers */
1471 ftrace_disable_cpu();
1472 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1473 ftrace_enable_cpu();
1476 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1478 struct trace_iterator
*iter
= m
->private;
1484 /* can't go backwards */
1489 ent
= find_next_entry_inc(iter
);
1493 while (ent
&& iter
->idx
< i
)
1494 ent
= find_next_entry_inc(iter
);
1502 * No necessary locking here. The worst thing which can
1503 * happen is loosing events consumed at the same time
1504 * by a trace_pipe reader.
1505 * Other than that, we don't risk to crash the ring buffer
1506 * because it serializes the readers.
1508 * The current tracer is copied to avoid a global locking
1511 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1513 struct trace_iterator
*iter
= m
->private;
1514 static struct tracer
*old_tracer
;
1515 int cpu_file
= iter
->cpu_file
;
1520 /* copy the tracer to avoid using a global lock all around */
1521 mutex_lock(&trace_types_lock
);
1522 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1523 old_tracer
= current_trace
;
1524 *iter
->trace
= *current_trace
;
1526 mutex_unlock(&trace_types_lock
);
1528 atomic_inc(&trace_record_cmdline_disabled
);
1530 if (*pos
!= iter
->pos
) {
1535 ftrace_disable_cpu();
1537 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1538 for_each_tracing_cpu(cpu
)
1539 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1541 ring_buffer_iter_reset(iter
->buffer_iter
[cpu_file
]);
1544 ftrace_enable_cpu();
1546 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1551 p
= s_next(m
, p
, &l
);
1557 static void s_stop(struct seq_file
*m
, void *p
)
1559 atomic_dec(&trace_record_cmdline_disabled
);
1562 static void print_lat_help_header(struct seq_file
*m
)
1564 seq_puts(m
, "# _------=> CPU# \n");
1565 seq_puts(m
, "# / _-----=> irqs-off \n");
1566 seq_puts(m
, "# | / _----=> need-resched \n");
1567 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1568 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1569 seq_puts(m
, "# |||| / \n");
1570 seq_puts(m
, "# ||||| delay \n");
1571 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1572 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1575 static void print_func_help_header(struct seq_file
*m
)
1577 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1578 seq_puts(m
, "# | | | | |\n");
1583 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1585 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1586 struct trace_array
*tr
= iter
->tr
;
1587 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1588 struct tracer
*type
= current_trace
;
1589 unsigned long total
;
1590 unsigned long entries
;
1591 const char *name
= "preemption";
1596 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1598 ring_buffer_overruns(iter
->tr
->buffer
);
1600 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
1602 seq_puts(m
, "# -----------------------------------"
1603 "---------------------------------\n");
1604 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1605 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1606 nsecs_to_usecs(data
->saved_latency
),
1610 #if defined(CONFIG_PREEMPT_NONE)
1612 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1614 #elif defined(CONFIG_PREEMPT)
1619 /* These are reserved for later use */
1622 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1626 seq_puts(m
, "# -----------------\n");
1627 seq_printf(m
, "# | task: %.16s-%d "
1628 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1629 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1630 data
->policy
, data
->rt_priority
);
1631 seq_puts(m
, "# -----------------\n");
1633 if (data
->critical_start
) {
1634 seq_puts(m
, "# => started at: ");
1635 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1636 trace_print_seq(m
, &iter
->seq
);
1637 seq_puts(m
, "\n# => ended at: ");
1638 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1639 trace_print_seq(m
, &iter
->seq
);
1646 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1648 struct trace_seq
*s
= &iter
->seq
;
1650 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1653 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1656 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1659 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1661 /* Don't print started cpu buffer for the first entry of the trace */
1663 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
1667 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1669 struct trace_seq
*s
= &iter
->seq
;
1670 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1671 struct trace_entry
*entry
;
1672 struct trace_event
*event
;
1676 test_cpu_buff_start(iter
);
1678 event
= ftrace_find_event(entry
->type
);
1680 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1681 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1682 if (!trace_print_lat_context(iter
))
1685 if (!trace_print_context(iter
))
1691 return event
->trace(iter
, sym_flags
);
1693 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1696 return TRACE_TYPE_HANDLED
;
1698 return TRACE_TYPE_PARTIAL_LINE
;
1701 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1703 struct trace_seq
*s
= &iter
->seq
;
1704 struct trace_entry
*entry
;
1705 struct trace_event
*event
;
1709 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1710 if (!trace_seq_printf(s
, "%d %d %llu ",
1711 entry
->pid
, iter
->cpu
, iter
->ts
))
1715 event
= ftrace_find_event(entry
->type
);
1717 return event
->raw(iter
, 0);
1719 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1722 return TRACE_TYPE_HANDLED
;
1724 return TRACE_TYPE_PARTIAL_LINE
;
1727 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1729 struct trace_seq
*s
= &iter
->seq
;
1730 unsigned char newline
= '\n';
1731 struct trace_entry
*entry
;
1732 struct trace_event
*event
;
1736 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1737 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1738 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1739 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1742 event
= ftrace_find_event(entry
->type
);
1744 enum print_line_t ret
= event
->hex(iter
, 0);
1745 if (ret
!= TRACE_TYPE_HANDLED
)
1749 SEQ_PUT_FIELD_RET(s
, newline
);
1751 return TRACE_TYPE_HANDLED
;
1754 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1756 struct trace_seq
*s
= &iter
->seq
;
1757 struct trace_entry
*entry
;
1758 struct trace_event
*event
;
1762 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1763 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1764 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1765 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1768 event
= ftrace_find_event(entry
->type
);
1769 return event
? event
->binary(iter
, 0) : TRACE_TYPE_HANDLED
;
1772 static int trace_empty(struct trace_iterator
*iter
)
1776 /* If we are looking at one CPU buffer, only check that one */
1777 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
1778 cpu
= iter
->cpu_file
;
1779 if (iter
->buffer_iter
[cpu
]) {
1780 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1783 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1789 for_each_tracing_cpu(cpu
) {
1790 if (iter
->buffer_iter
[cpu
]) {
1791 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1794 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1802 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1804 enum print_line_t ret
;
1806 if (iter
->trace
&& iter
->trace
->print_line
) {
1807 ret
= iter
->trace
->print_line(iter
);
1808 if (ret
!= TRACE_TYPE_UNHANDLED
)
1812 if (iter
->ent
->type
== TRACE_BPRINT
&&
1813 trace_flags
& TRACE_ITER_PRINTK
&&
1814 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1815 return trace_print_bprintk_msg_only(iter
);
1817 if (iter
->ent
->type
== TRACE_PRINT
&&
1818 trace_flags
& TRACE_ITER_PRINTK
&&
1819 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1820 return trace_print_printk_msg_only(iter
);
1822 if (trace_flags
& TRACE_ITER_BIN
)
1823 return print_bin_fmt(iter
);
1825 if (trace_flags
& TRACE_ITER_HEX
)
1826 return print_hex_fmt(iter
);
1828 if (trace_flags
& TRACE_ITER_RAW
)
1829 return print_raw_fmt(iter
);
1831 return print_trace_fmt(iter
);
1834 static int s_show(struct seq_file
*m
, void *v
)
1836 struct trace_iterator
*iter
= v
;
1838 if (iter
->ent
== NULL
) {
1840 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1843 if (iter
->trace
&& iter
->trace
->print_header
)
1844 iter
->trace
->print_header(m
);
1845 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1846 /* print nothing if the buffers are empty */
1847 if (trace_empty(iter
))
1849 print_trace_header(m
, iter
);
1850 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1851 print_lat_help_header(m
);
1853 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1854 print_func_help_header(m
);
1857 print_trace_line(iter
);
1858 trace_print_seq(m
, &iter
->seq
);
1864 static struct seq_operations tracer_seq_ops
= {
1871 static struct trace_iterator
*
1872 __tracing_open(struct inode
*inode
, struct file
*file
)
1874 long cpu_file
= (long) inode
->i_private
;
1875 void *fail_ret
= ERR_PTR(-ENOMEM
);
1876 struct trace_iterator
*iter
;
1880 if (tracing_disabled
)
1881 return ERR_PTR(-ENODEV
);
1883 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1885 return ERR_PTR(-ENOMEM
);
1888 * We make a copy of the current tracer to avoid concurrent
1889 * changes on it while we are reading.
1891 mutex_lock(&trace_types_lock
);
1892 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
1897 *iter
->trace
= *current_trace
;
1899 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
1902 cpumask_clear(iter
->started
);
1904 if (current_trace
&& current_trace
->print_max
)
1907 iter
->tr
= &global_trace
;
1909 mutex_init(&iter
->mutex
);
1910 iter
->cpu_file
= cpu_file
;
1912 /* Notify the tracer early; before we stop tracing. */
1913 if (iter
->trace
&& iter
->trace
->open
)
1914 iter
->trace
->open(iter
);
1916 /* Annotate start of buffers if we had overruns */
1917 if (ring_buffer_overruns(iter
->tr
->buffer
))
1918 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
1920 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
1921 for_each_tracing_cpu(cpu
) {
1923 iter
->buffer_iter
[cpu
] =
1924 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1927 cpu
= iter
->cpu_file
;
1928 iter
->buffer_iter
[cpu
] =
1929 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1932 /* TODO stop tracer */
1933 ret
= seq_open(file
, &tracer_seq_ops
);
1935 fail_ret
= ERR_PTR(ret
);
1939 m
= file
->private_data
;
1942 /* stop the trace while dumping */
1945 mutex_unlock(&trace_types_lock
);
1950 for_each_tracing_cpu(cpu
) {
1951 if (iter
->buffer_iter
[cpu
])
1952 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1954 free_cpumask_var(iter
->started
);
1956 mutex_unlock(&trace_types_lock
);
1963 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1965 if (tracing_disabled
)
1968 filp
->private_data
= inode
->i_private
;
1972 static int tracing_release(struct inode
*inode
, struct file
*file
)
1974 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1975 struct trace_iterator
*iter
;
1978 if (!(file
->f_mode
& FMODE_READ
))
1983 mutex_lock(&trace_types_lock
);
1984 for_each_tracing_cpu(cpu
) {
1985 if (iter
->buffer_iter
[cpu
])
1986 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1989 if (iter
->trace
&& iter
->trace
->close
)
1990 iter
->trace
->close(iter
);
1992 /* reenable tracing if it was previously enabled */
1994 mutex_unlock(&trace_types_lock
);
1996 seq_release(inode
, file
);
1997 mutex_destroy(&iter
->mutex
);
1998 free_cpumask_var(iter
->started
);
2004 static int tracing_open(struct inode
*inode
, struct file
*file
)
2006 struct trace_iterator
*iter
;
2009 /* If this file was open for write, then erase contents */
2010 if ((file
->f_mode
& FMODE_WRITE
) &&
2011 !(file
->f_flags
& O_APPEND
)) {
2012 long cpu
= (long) inode
->i_private
;
2014 if (cpu
== TRACE_PIPE_ALL_CPU
)
2015 tracing_reset_online_cpus(&global_trace
);
2017 tracing_reset(&global_trace
, cpu
);
2020 if (file
->f_mode
& FMODE_READ
) {
2021 iter
= __tracing_open(inode
, file
);
2023 ret
= PTR_ERR(iter
);
2024 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2025 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2031 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2033 struct tracer
*t
= m
->private;
2045 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2047 struct tracer
*t
= m
->private;
2050 mutex_lock(&trace_types_lock
);
2051 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2057 static void t_stop(struct seq_file
*m
, void *p
)
2059 mutex_unlock(&trace_types_lock
);
2062 static int t_show(struct seq_file
*m
, void *v
)
2064 struct tracer
*t
= v
;
2069 seq_printf(m
, "%s", t
->name
);
2078 static struct seq_operations show_traces_seq_ops
= {
2085 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2089 if (tracing_disabled
)
2092 ret
= seq_open(file
, &show_traces_seq_ops
);
2094 struct seq_file
*m
= file
->private_data
;
2095 m
->private = trace_types
;
2102 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
2103 size_t count
, loff_t
*ppos
)
2108 static const struct file_operations tracing_fops
= {
2109 .open
= tracing_open
,
2111 .write
= tracing_write_stub
,
2112 .llseek
= seq_lseek
,
2113 .release
= tracing_release
,
2116 static const struct file_operations show_traces_fops
= {
2117 .open
= show_traces_open
,
2119 .release
= seq_release
,
2123 * Only trace on a CPU if the bitmask is set:
2125 static cpumask_var_t tracing_cpumask
;
2128 * The tracer itself will not take this lock, but still we want
2129 * to provide a consistent cpumask to user-space:
2131 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2134 * Temporary storage for the character representation of the
2135 * CPU bitmask (and one more byte for the newline):
2137 static char mask_str
[NR_CPUS
+ 1];
2140 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2141 size_t count
, loff_t
*ppos
)
2145 mutex_lock(&tracing_cpumask_update_lock
);
2147 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2148 if (count
- len
< 2) {
2152 len
+= sprintf(mask_str
+ len
, "\n");
2153 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2156 mutex_unlock(&tracing_cpumask_update_lock
);
2162 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2163 size_t count
, loff_t
*ppos
)
2166 cpumask_var_t tracing_cpumask_new
;
2168 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2171 mutex_lock(&tracing_cpumask_update_lock
);
2172 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2176 local_irq_disable();
2177 __raw_spin_lock(&ftrace_max_lock
);
2178 for_each_tracing_cpu(cpu
) {
2180 * Increase/decrease the disabled counter if we are
2181 * about to flip a bit in the cpumask:
2183 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2184 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2185 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2187 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2188 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2189 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2192 __raw_spin_unlock(&ftrace_max_lock
);
2195 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2197 mutex_unlock(&tracing_cpumask_update_lock
);
2198 free_cpumask_var(tracing_cpumask_new
);
2203 mutex_unlock(&tracing_cpumask_update_lock
);
2204 free_cpumask_var(tracing_cpumask
);
2209 static const struct file_operations tracing_cpumask_fops
= {
2210 .open
= tracing_open_generic
,
2211 .read
= tracing_cpumask_read
,
2212 .write
= tracing_cpumask_write
,
2216 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2217 size_t cnt
, loff_t
*ppos
)
2219 struct tracer_opt
*trace_opts
;
2227 /* calculate max size */
2228 for (i
= 0; trace_options
[i
]; i
++) {
2229 len
+= strlen(trace_options
[i
]);
2230 len
+= 3; /* "no" and newline */
2233 mutex_lock(&trace_types_lock
);
2234 tracer_flags
= current_trace
->flags
->val
;
2235 trace_opts
= current_trace
->flags
->opts
;
2238 * Increase the size with names of options specific
2239 * of the current tracer.
2241 for (i
= 0; trace_opts
[i
].name
; i
++) {
2242 len
+= strlen(trace_opts
[i
].name
);
2243 len
+= 3; /* "no" and newline */
2246 /* +2 for \n and \0 */
2247 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2249 mutex_unlock(&trace_types_lock
);
2253 for (i
= 0; trace_options
[i
]; i
++) {
2254 if (trace_flags
& (1 << i
))
2255 r
+= sprintf(buf
+ r
, "%s\n", trace_options
[i
]);
2257 r
+= sprintf(buf
+ r
, "no%s\n", trace_options
[i
]);
2260 for (i
= 0; trace_opts
[i
].name
; i
++) {
2261 if (tracer_flags
& trace_opts
[i
].bit
)
2262 r
+= sprintf(buf
+ r
, "%s\n",
2263 trace_opts
[i
].name
);
2265 r
+= sprintf(buf
+ r
, "no%s\n",
2266 trace_opts
[i
].name
);
2268 mutex_unlock(&trace_types_lock
);
2270 WARN_ON(r
>= len
+ 2);
2272 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2278 /* Try to assign a tracer specific option */
2279 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2281 struct tracer_flags
*trace_flags
= trace
->flags
;
2282 struct tracer_opt
*opts
= NULL
;
2286 for (i
= 0; trace_flags
->opts
[i
].name
; i
++) {
2287 opts
= &trace_flags
->opts
[i
];
2288 len
= strlen(opts
->name
);
2290 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2291 ret
= trace
->set_flag(trace_flags
->val
,
2297 if (!trace_flags
->opts
[i
].name
)
2300 /* Refused to handle */
2305 trace_flags
->val
&= ~opts
->bit
;
2307 trace_flags
->val
|= opts
->bit
;
2312 static void set_tracer_flags(unsigned int mask
, int enabled
)
2314 /* do nothing if flag is already set */
2315 if (!!(trace_flags
& mask
) == !!enabled
)
2319 trace_flags
|= mask
;
2321 trace_flags
&= ~mask
;
2323 if (mask
== TRACE_ITER_GLOBAL_CLK
) {
2327 func
= trace_clock_global
;
2329 func
= trace_clock_local
;
2331 mutex_lock(&trace_types_lock
);
2332 ring_buffer_set_clock(global_trace
.buffer
, func
);
2335 ring_buffer_set_clock(max_tr
.buffer
, func
);
2336 mutex_unlock(&trace_types_lock
);
2341 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2342 size_t cnt
, loff_t
*ppos
)
2350 if (cnt
>= sizeof(buf
))
2353 if (copy_from_user(&buf
, ubuf
, cnt
))
2358 if (strncmp(buf
, "no", 2) == 0) {
2363 for (i
= 0; trace_options
[i
]; i
++) {
2364 int len
= strlen(trace_options
[i
]);
2366 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2367 set_tracer_flags(1 << i
, !neg
);
2372 /* If no option could be set, test the specific tracer options */
2373 if (!trace_options
[i
]) {
2374 mutex_lock(&trace_types_lock
);
2375 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2376 mutex_unlock(&trace_types_lock
);
2386 static const struct file_operations tracing_iter_fops
= {
2387 .open
= tracing_open_generic
,
2388 .read
= tracing_trace_options_read
,
2389 .write
= tracing_trace_options_write
,
2392 static const char readme_msg
[] =
2393 "tracing mini-HOWTO:\n\n"
2395 "# mount -t debugfs nodev /debug\n\n"
2396 "# cat /debug/tracing/available_tracers\n"
2397 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2398 "# cat /debug/tracing/current_tracer\n"
2400 "# echo sched_switch > /debug/tracing/current_tracer\n"
2401 "# cat /debug/tracing/current_tracer\n"
2403 "# cat /debug/tracing/trace_options\n"
2404 "noprint-parent nosym-offset nosym-addr noverbose\n"
2405 "# echo print-parent > /debug/tracing/trace_options\n"
2406 "# echo 1 > /debug/tracing/tracing_enabled\n"
2407 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2408 "echo 0 > /debug/tracing/tracing_enabled\n"
2412 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2413 size_t cnt
, loff_t
*ppos
)
2415 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2416 readme_msg
, strlen(readme_msg
));
2419 static const struct file_operations tracing_readme_fops
= {
2420 .open
= tracing_open_generic
,
2421 .read
= tracing_readme_read
,
2425 tracing_saved_cmdlines_read(struct file
*file
, char __user
*ubuf
,
2426 size_t cnt
, loff_t
*ppos
)
2435 file_buf
= kmalloc(SAVED_CMDLINES
*(16+TASK_COMM_LEN
), GFP_KERNEL
);
2439 buf_comm
= kmalloc(TASK_COMM_LEN
, GFP_KERNEL
);
2447 for (i
= 0; i
< SAVED_CMDLINES
; i
++) {
2450 pid
= map_cmdline_to_pid
[i
];
2451 if (pid
== -1 || pid
== NO_CMDLINE_MAP
)
2454 trace_find_cmdline(pid
, buf_comm
);
2455 r
= sprintf(buf
, "%d %s\n", pid
, buf_comm
);
2460 len
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
2469 static const struct file_operations tracing_saved_cmdlines_fops
= {
2470 .open
= tracing_open_generic
,
2471 .read
= tracing_saved_cmdlines_read
,
2475 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2476 size_t cnt
, loff_t
*ppos
)
2481 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2482 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2486 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2487 size_t cnt
, loff_t
*ppos
)
2489 struct trace_array
*tr
= filp
->private_data
;
2494 if (cnt
>= sizeof(buf
))
2497 if (copy_from_user(&buf
, ubuf
, cnt
))
2502 ret
= strict_strtoul(buf
, 10, &val
);
2508 mutex_lock(&trace_types_lock
);
2509 if (tracer_enabled
^ val
) {
2512 if (current_trace
->start
)
2513 current_trace
->start(tr
);
2518 if (current_trace
->stop
)
2519 current_trace
->stop(tr
);
2522 mutex_unlock(&trace_types_lock
);
2530 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2531 size_t cnt
, loff_t
*ppos
)
2533 char buf
[max_tracer_type_len
+2];
2536 mutex_lock(&trace_types_lock
);
2538 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2540 r
= sprintf(buf
, "\n");
2541 mutex_unlock(&trace_types_lock
);
2543 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2546 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2548 tracing_reset_online_cpus(tr
);
2552 static int tracing_resize_ring_buffer(unsigned long size
)
2557 * If kernel or user changes the size of the ring buffer
2558 * we use the size that was given, and we can forget about
2559 * expanding it later.
2561 ring_buffer_expanded
= 1;
2563 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2567 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2571 r
= ring_buffer_resize(global_trace
.buffer
,
2572 global_trace
.entries
);
2575 * AARGH! We are left with different
2576 * size max buffer!!!!
2577 * The max buffer is our "snapshot" buffer.
2578 * When a tracer needs a snapshot (one of the
2579 * latency tracers), it swaps the max buffer
2580 * with the saved snap shot. We succeeded to
2581 * update the size of the main buffer, but failed to
2582 * update the size of the max buffer. But when we tried
2583 * to reset the main buffer to the original size, we
2584 * failed there too. This is very unlikely to
2585 * happen, but if it does, warn and kill all
2589 tracing_disabled
= 1;
2594 global_trace
.entries
= size
;
2600 * tracing_update_buffers - used by tracing facility to expand ring buffers
2602 * To save on memory when the tracing is never used on a system with it
2603 * configured in. The ring buffers are set to a minimum size. But once
2604 * a user starts to use the tracing facility, then they need to grow
2605 * to their default size.
2607 * This function is to be called when a tracer is about to be used.
2609 int tracing_update_buffers(void)
2613 mutex_lock(&trace_types_lock
);
2614 if (!ring_buffer_expanded
)
2615 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2616 mutex_unlock(&trace_types_lock
);
2621 struct trace_option_dentry
;
2623 static struct trace_option_dentry
*
2624 create_trace_option_files(struct tracer
*tracer
);
2627 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2629 static int tracing_set_tracer(const char *buf
)
2631 static struct trace_option_dentry
*topts
;
2632 struct trace_array
*tr
= &global_trace
;
2636 mutex_lock(&trace_types_lock
);
2638 if (!ring_buffer_expanded
) {
2639 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2645 for (t
= trace_types
; t
; t
= t
->next
) {
2646 if (strcmp(t
->name
, buf
) == 0)
2653 if (t
== current_trace
)
2656 trace_branch_disable();
2657 if (current_trace
&& current_trace
->reset
)
2658 current_trace
->reset(tr
);
2660 destroy_trace_option_files(topts
);
2664 topts
= create_trace_option_files(current_trace
);
2667 ret
= tracer_init(t
, tr
);
2672 trace_branch_enable(tr
);
2674 mutex_unlock(&trace_types_lock
);
2680 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2681 size_t cnt
, loff_t
*ppos
)
2683 char buf
[max_tracer_type_len
+1];
2690 if (cnt
> max_tracer_type_len
)
2691 cnt
= max_tracer_type_len
;
2693 if (copy_from_user(&buf
, ubuf
, cnt
))
2698 /* strip ending whitespace. */
2699 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2702 err
= tracing_set_tracer(buf
);
2712 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2713 size_t cnt
, loff_t
*ppos
)
2715 unsigned long *ptr
= filp
->private_data
;
2719 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2720 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2721 if (r
> sizeof(buf
))
2723 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2727 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2728 size_t cnt
, loff_t
*ppos
)
2730 unsigned long *ptr
= filp
->private_data
;
2735 if (cnt
>= sizeof(buf
))
2738 if (copy_from_user(&buf
, ubuf
, cnt
))
2743 ret
= strict_strtoul(buf
, 10, &val
);
2752 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2754 long cpu_file
= (long) inode
->i_private
;
2755 struct trace_iterator
*iter
;
2758 if (tracing_disabled
)
2761 mutex_lock(&trace_types_lock
);
2763 /* We only allow one reader per cpu */
2764 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
2765 if (!cpumask_empty(tracing_reader_cpumask
)) {
2769 cpumask_setall(tracing_reader_cpumask
);
2771 if (!cpumask_test_cpu(cpu_file
, tracing_reader_cpumask
))
2772 cpumask_set_cpu(cpu_file
, tracing_reader_cpumask
);
2779 /* create a buffer to store the information to pass to userspace */
2780 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2787 * We make a copy of the current tracer to avoid concurrent
2788 * changes on it while we are reading.
2790 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2796 *iter
->trace
= *current_trace
;
2798 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2803 /* trace pipe does not show start of buffer */
2804 cpumask_setall(iter
->started
);
2806 iter
->cpu_file
= cpu_file
;
2807 iter
->tr
= &global_trace
;
2808 mutex_init(&iter
->mutex
);
2809 filp
->private_data
= iter
;
2811 if (iter
->trace
->pipe_open
)
2812 iter
->trace
->pipe_open(iter
);
2815 mutex_unlock(&trace_types_lock
);
2821 mutex_unlock(&trace_types_lock
);
2825 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2827 struct trace_iterator
*iter
= file
->private_data
;
2829 mutex_lock(&trace_types_lock
);
2831 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
)
2832 cpumask_clear(tracing_reader_cpumask
);
2834 cpumask_clear_cpu(iter
->cpu_file
, tracing_reader_cpumask
);
2836 mutex_unlock(&trace_types_lock
);
2838 free_cpumask_var(iter
->started
);
2839 mutex_destroy(&iter
->mutex
);
2847 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2849 struct trace_iterator
*iter
= filp
->private_data
;
2851 if (trace_flags
& TRACE_ITER_BLOCK
) {
2853 * Always select as readable when in blocking mode
2855 return POLLIN
| POLLRDNORM
;
2857 if (!trace_empty(iter
))
2858 return POLLIN
| POLLRDNORM
;
2859 poll_wait(filp
, &trace_wait
, poll_table
);
2860 if (!trace_empty(iter
))
2861 return POLLIN
| POLLRDNORM
;
2868 void default_wait_pipe(struct trace_iterator
*iter
)
2872 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
2874 if (trace_empty(iter
))
2877 finish_wait(&trace_wait
, &wait
);
2881 * This is a make-shift waitqueue.
2882 * A tracer might use this callback on some rare cases:
2884 * 1) the current tracer might hold the runqueue lock when it wakes up
2885 * a reader, hence a deadlock (sched, function, and function graph tracers)
2886 * 2) the function tracers, trace all functions, we don't want
2887 * the overhead of calling wake_up and friends
2888 * (and tracing them too)
2890 * Anyway, this is really very primitive wakeup.
2892 void poll_wait_pipe(struct trace_iterator
*iter
)
2894 set_current_state(TASK_INTERRUPTIBLE
);
2895 /* sleep for 100 msecs, and try again. */
2896 schedule_timeout(HZ
/ 10);
2899 /* Must be called with trace_types_lock mutex held. */
2900 static int tracing_wait_pipe(struct file
*filp
)
2902 struct trace_iterator
*iter
= filp
->private_data
;
2904 while (trace_empty(iter
)) {
2906 if ((filp
->f_flags
& O_NONBLOCK
)) {
2910 mutex_unlock(&iter
->mutex
);
2912 iter
->trace
->wait_pipe(iter
);
2914 mutex_lock(&iter
->mutex
);
2916 if (signal_pending(current
))
2920 * We block until we read something and tracing is disabled.
2921 * We still block if tracing is disabled, but we have never
2922 * read anything. This allows a user to cat this file, and
2923 * then enable tracing. But after we have read something,
2924 * we give an EOF when tracing is again disabled.
2926 * iter->pos will be 0 if we haven't read anything.
2928 if (!tracer_enabled
&& iter
->pos
)
2939 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2940 size_t cnt
, loff_t
*ppos
)
2942 struct trace_iterator
*iter
= filp
->private_data
;
2943 static struct tracer
*old_tracer
;
2946 /* return any leftover data */
2947 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2951 trace_seq_init(&iter
->seq
);
2953 /* copy the tracer to avoid using a global lock all around */
2954 mutex_lock(&trace_types_lock
);
2955 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
2956 old_tracer
= current_trace
;
2957 *iter
->trace
= *current_trace
;
2959 mutex_unlock(&trace_types_lock
);
2962 * Avoid more than one consumer on a single file descriptor
2963 * This is just a matter of traces coherency, the ring buffer itself
2966 mutex_lock(&iter
->mutex
);
2967 if (iter
->trace
->read
) {
2968 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2974 sret
= tracing_wait_pipe(filp
);
2978 /* stop when tracing is finished */
2979 if (trace_empty(iter
)) {
2984 if (cnt
>= PAGE_SIZE
)
2985 cnt
= PAGE_SIZE
- 1;
2987 /* reset all but tr, trace, and overruns */
2988 memset(&iter
->seq
, 0,
2989 sizeof(struct trace_iterator
) -
2990 offsetof(struct trace_iterator
, seq
));
2993 while (find_next_entry_inc(iter
) != NULL
) {
2994 enum print_line_t ret
;
2995 int len
= iter
->seq
.len
;
2997 ret
= print_trace_line(iter
);
2998 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2999 /* don't print partial lines */
3000 iter
->seq
.len
= len
;
3003 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3004 trace_consume(iter
);
3006 if (iter
->seq
.len
>= cnt
)
3010 /* Now copy what we have to the user */
3011 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3012 if (iter
->seq
.readpos
>= iter
->seq
.len
)
3013 trace_seq_init(&iter
->seq
);
3016 * If there was nothing to send to user, inspite of consuming trace
3017 * entries, go back to wait for more entries.
3023 mutex_unlock(&iter
->mutex
);
3028 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
3029 struct pipe_buffer
*buf
)
3031 __free_page(buf
->page
);
3034 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
3037 __free_page(spd
->pages
[idx
]);
3040 static struct pipe_buf_operations tracing_pipe_buf_ops
= {
3042 .map
= generic_pipe_buf_map
,
3043 .unmap
= generic_pipe_buf_unmap
,
3044 .confirm
= generic_pipe_buf_confirm
,
3045 .release
= tracing_pipe_buf_release
,
3046 .steal
= generic_pipe_buf_steal
,
3047 .get
= generic_pipe_buf_get
,
3051 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
3056 /* Seq buffer is page-sized, exactly what we need. */
3058 count
= iter
->seq
.len
;
3059 ret
= print_trace_line(iter
);
3060 count
= iter
->seq
.len
- count
;
3063 iter
->seq
.len
-= count
;
3066 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3067 iter
->seq
.len
-= count
;
3071 trace_consume(iter
);
3073 if (!find_next_entry_inc(iter
)) {
3083 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3085 struct pipe_inode_info
*pipe
,
3089 struct page
*pages
[PIPE_BUFFERS
];
3090 struct partial_page partial
[PIPE_BUFFERS
];
3091 struct trace_iterator
*iter
= filp
->private_data
;
3092 struct splice_pipe_desc spd
= {
3095 .nr_pages
= 0, /* This gets updated below. */
3097 .ops
= &tracing_pipe_buf_ops
,
3098 .spd_release
= tracing_spd_release_pipe
,
3100 static struct tracer
*old_tracer
;
3105 /* copy the tracer to avoid using a global lock all around */
3106 mutex_lock(&trace_types_lock
);
3107 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3108 old_tracer
= current_trace
;
3109 *iter
->trace
= *current_trace
;
3111 mutex_unlock(&trace_types_lock
);
3113 mutex_lock(&iter
->mutex
);
3115 if (iter
->trace
->splice_read
) {
3116 ret
= iter
->trace
->splice_read(iter
, filp
,
3117 ppos
, pipe
, len
, flags
);
3122 ret
= tracing_wait_pipe(filp
);
3126 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
3131 /* Fill as many pages as possible. */
3132 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
3133 pages
[i
] = alloc_page(GFP_KERNEL
);
3137 rem
= tracing_fill_pipe_page(rem
, iter
);
3139 /* Copy the data into the page, so we can start over. */
3140 ret
= trace_seq_to_buffer(&iter
->seq
,
3141 page_address(pages
[i
]),
3144 __free_page(pages
[i
]);
3147 partial
[i
].offset
= 0;
3148 partial
[i
].len
= iter
->seq
.len
;
3150 trace_seq_init(&iter
->seq
);
3153 mutex_unlock(&iter
->mutex
);
3157 return splice_to_pipe(pipe
, &spd
);
3160 mutex_unlock(&iter
->mutex
);
3166 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3167 size_t cnt
, loff_t
*ppos
)
3169 struct trace_array
*tr
= filp
->private_data
;
3173 mutex_lock(&trace_types_lock
);
3174 if (!ring_buffer_expanded
)
3175 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3177 trace_buf_size
>> 10);
3179 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3180 mutex_unlock(&trace_types_lock
);
3182 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3186 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3187 size_t cnt
, loff_t
*ppos
)
3193 if (cnt
>= sizeof(buf
))
3196 if (copy_from_user(&buf
, ubuf
, cnt
))
3201 ret
= strict_strtoul(buf
, 10, &val
);
3205 /* must have at least 1 entry */
3209 mutex_lock(&trace_types_lock
);
3213 /* disable all cpu buffers */
3214 for_each_tracing_cpu(cpu
) {
3215 if (global_trace
.data
[cpu
])
3216 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3217 if (max_tr
.data
[cpu
])
3218 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3221 /* value is in KB */
3224 if (val
!= global_trace
.entries
) {
3225 ret
= tracing_resize_ring_buffer(val
);
3234 /* If check pages failed, return ENOMEM */
3235 if (tracing_disabled
)
3238 for_each_tracing_cpu(cpu
) {
3239 if (global_trace
.data
[cpu
])
3240 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3241 if (max_tr
.data
[cpu
])
3242 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3246 max_tr
.entries
= global_trace
.entries
;
3247 mutex_unlock(&trace_types_lock
);
3252 static int mark_printk(const char *fmt
, ...)
3256 va_start(args
, fmt
);
3257 ret
= trace_vprintk(0, fmt
, args
);
3263 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3264 size_t cnt
, loff_t
*fpos
)
3269 if (tracing_disabled
)
3272 if (cnt
> TRACE_BUF_SIZE
)
3273 cnt
= TRACE_BUF_SIZE
;
3275 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3279 if (copy_from_user(buf
, ubuf
, cnt
)) {
3284 /* Cut from the first nil or newline. */
3286 end
= strchr(buf
, '\n');
3290 cnt
= mark_printk("%s\n", buf
);
3297 static const struct file_operations tracing_max_lat_fops
= {
3298 .open
= tracing_open_generic
,
3299 .read
= tracing_max_lat_read
,
3300 .write
= tracing_max_lat_write
,
3303 static const struct file_operations tracing_ctrl_fops
= {
3304 .open
= tracing_open_generic
,
3305 .read
= tracing_ctrl_read
,
3306 .write
= tracing_ctrl_write
,
3309 static const struct file_operations set_tracer_fops
= {
3310 .open
= tracing_open_generic
,
3311 .read
= tracing_set_trace_read
,
3312 .write
= tracing_set_trace_write
,
3315 static const struct file_operations tracing_pipe_fops
= {
3316 .open
= tracing_open_pipe
,
3317 .poll
= tracing_poll_pipe
,
3318 .read
= tracing_read_pipe
,
3319 .splice_read
= tracing_splice_read_pipe
,
3320 .release
= tracing_release_pipe
,
3323 static const struct file_operations tracing_entries_fops
= {
3324 .open
= tracing_open_generic
,
3325 .read
= tracing_entries_read
,
3326 .write
= tracing_entries_write
,
3329 static const struct file_operations tracing_mark_fops
= {
3330 .open
= tracing_open_generic
,
3331 .write
= tracing_mark_write
,
3334 struct ftrace_buffer_info
{
3335 struct trace_array
*tr
;
3341 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3343 int cpu
= (int)(long)inode
->i_private
;
3344 struct ftrace_buffer_info
*info
;
3346 if (tracing_disabled
)
3349 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3353 info
->tr
= &global_trace
;
3356 /* Force reading ring buffer for first read */
3357 info
->read
= (unsigned int)-1;
3359 filp
->private_data
= info
;
3361 return nonseekable_open(inode
, filp
);
3365 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3366 size_t count
, loff_t
*ppos
)
3368 struct ftrace_buffer_info
*info
= filp
->private_data
;
3377 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3381 /* Do we have previous read data to read? */
3382 if (info
->read
< PAGE_SIZE
)
3387 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3394 pos
= ring_buffer_page_len(info
->spare
);
3396 if (pos
< PAGE_SIZE
)
3397 memset(info
->spare
+ pos
, 0, PAGE_SIZE
- pos
);
3400 size
= PAGE_SIZE
- info
->read
;
3404 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3415 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3417 struct ftrace_buffer_info
*info
= file
->private_data
;
3420 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3427 struct ring_buffer
*buffer
;
3432 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3433 struct pipe_buffer
*buf
)
3435 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3440 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3445 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3446 struct pipe_buffer
*buf
)
3451 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3452 struct pipe_buffer
*buf
)
3454 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3459 /* Pipe buffer operations for a buffer. */
3460 static struct pipe_buf_operations buffer_pipe_buf_ops
= {
3462 .map
= generic_pipe_buf_map
,
3463 .unmap
= generic_pipe_buf_unmap
,
3464 .confirm
= generic_pipe_buf_confirm
,
3465 .release
= buffer_pipe_buf_release
,
3466 .steal
= buffer_pipe_buf_steal
,
3467 .get
= buffer_pipe_buf_get
,
3471 * Callback from splice_to_pipe(), if we need to release some pages
3472 * at the end of the spd in case we error'ed out in filling the pipe.
3474 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3476 struct buffer_ref
*ref
=
3477 (struct buffer_ref
*)spd
->partial
[i
].private;
3482 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3484 spd
->partial
[i
].private = 0;
3488 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3489 struct pipe_inode_info
*pipe
, size_t len
,
3492 struct ftrace_buffer_info
*info
= file
->private_data
;
3493 struct partial_page partial
[PIPE_BUFFERS
];
3494 struct page
*pages
[PIPE_BUFFERS
];
3495 struct splice_pipe_desc spd
= {
3499 .ops
= &buffer_pipe_buf_ops
,
3500 .spd_release
= buffer_spd_release
,
3502 struct buffer_ref
*ref
;
3506 if (*ppos
& (PAGE_SIZE
- 1)) {
3507 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3511 if (len
& (PAGE_SIZE
- 1)) {
3512 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3513 if (len
< PAGE_SIZE
)
3518 for (i
= 0; i
< PIPE_BUFFERS
&& len
; i
++, len
-= PAGE_SIZE
) {
3522 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3526 ref
->buffer
= info
->tr
->buffer
;
3527 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3533 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3536 ring_buffer_free_read_page(ref
->buffer
,
3543 * zero out any left over data, this is going to
3546 size
= ring_buffer_page_len(ref
->page
);
3547 if (size
< PAGE_SIZE
)
3548 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3550 page
= virt_to_page(ref
->page
);
3552 spd
.pages
[i
] = page
;
3553 spd
.partial
[i
].len
= PAGE_SIZE
;
3554 spd
.partial
[i
].offset
= 0;
3555 spd
.partial
[i
].private = (unsigned long)ref
;
3562 /* did we read anything? */
3563 if (!spd
.nr_pages
) {
3564 if (flags
& SPLICE_F_NONBLOCK
)
3572 ret
= splice_to_pipe(pipe
, &spd
);
3577 static const struct file_operations tracing_buffers_fops
= {
3578 .open
= tracing_buffers_open
,
3579 .read
= tracing_buffers_read
,
3580 .release
= tracing_buffers_release
,
3581 .splice_read
= tracing_buffers_splice_read
,
3582 .llseek
= no_llseek
,
3585 #ifdef CONFIG_DYNAMIC_FTRACE
3587 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3593 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3594 size_t cnt
, loff_t
*ppos
)
3596 static char ftrace_dyn_info_buffer
[1024];
3597 static DEFINE_MUTEX(dyn_info_mutex
);
3598 unsigned long *p
= filp
->private_data
;
3599 char *buf
= ftrace_dyn_info_buffer
;
3600 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3603 mutex_lock(&dyn_info_mutex
);
3604 r
= sprintf(buf
, "%ld ", *p
);
3606 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3609 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3611 mutex_unlock(&dyn_info_mutex
);
3616 static const struct file_operations tracing_dyn_info_fops
= {
3617 .open
= tracing_open_generic
,
3618 .read
= tracing_read_dyn_info
,
3622 static struct dentry
*d_tracer
;
3624 struct dentry
*tracing_init_dentry(void)
3631 if (!debugfs_initialized())
3634 d_tracer
= debugfs_create_dir("tracing", NULL
);
3636 if (!d_tracer
&& !once
) {
3638 pr_warning("Could not create debugfs directory 'tracing'\n");
3645 static struct dentry
*d_percpu
;
3647 struct dentry
*tracing_dentry_percpu(void)
3650 struct dentry
*d_tracer
;
3655 d_tracer
= tracing_init_dentry();
3660 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
3662 if (!d_percpu
&& !once
) {
3664 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3671 static void tracing_init_debugfs_percpu(long cpu
)
3673 struct dentry
*d_percpu
= tracing_dentry_percpu();
3674 struct dentry
*d_cpu
;
3675 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3678 if (cpu
> 999 || cpu
< 0)
3681 sprintf(cpu_dir
, "cpu%ld", cpu
);
3682 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
3684 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
3688 /* per cpu trace_pipe */
3689 trace_create_file("trace_pipe", 0444, d_cpu
,
3690 (void *) cpu
, &tracing_pipe_fops
);
3693 trace_create_file("trace", 0644, d_cpu
,
3694 (void *) cpu
, &tracing_fops
);
3696 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
3697 (void *) cpu
, &tracing_buffers_fops
);
3700 #ifdef CONFIG_FTRACE_SELFTEST
3701 /* Let selftest have access to static functions in this file */
3702 #include "trace_selftest.c"
3705 struct trace_option_dentry
{
3706 struct tracer_opt
*opt
;
3707 struct tracer_flags
*flags
;
3708 struct dentry
*entry
;
3712 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3715 struct trace_option_dentry
*topt
= filp
->private_data
;
3718 if (topt
->flags
->val
& topt
->opt
->bit
)
3723 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3727 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3730 struct trace_option_dentry
*topt
= filp
->private_data
;
3735 if (cnt
>= sizeof(buf
))
3738 if (copy_from_user(&buf
, ubuf
, cnt
))
3743 ret
= strict_strtoul(buf
, 10, &val
);
3750 /* do nothing if already cleared */
3751 if (!(topt
->flags
->val
& topt
->opt
->bit
))
3754 mutex_lock(&trace_types_lock
);
3755 if (current_trace
->set_flag
)
3756 ret
= current_trace
->set_flag(topt
->flags
->val
,
3758 mutex_unlock(&trace_types_lock
);
3761 topt
->flags
->val
&= ~topt
->opt
->bit
;
3764 /* do nothing if already set */
3765 if (topt
->flags
->val
& topt
->opt
->bit
)
3768 mutex_lock(&trace_types_lock
);
3769 if (current_trace
->set_flag
)
3770 ret
= current_trace
->set_flag(topt
->flags
->val
,
3772 mutex_unlock(&trace_types_lock
);
3775 topt
->flags
->val
|= topt
->opt
->bit
;
3788 static const struct file_operations trace_options_fops
= {
3789 .open
= tracing_open_generic
,
3790 .read
= trace_options_read
,
3791 .write
= trace_options_write
,
3795 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3798 long index
= (long)filp
->private_data
;
3801 if (trace_flags
& (1 << index
))
3806 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3810 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3813 long index
= (long)filp
->private_data
;
3818 if (cnt
>= sizeof(buf
))
3821 if (copy_from_user(&buf
, ubuf
, cnt
))
3826 ret
= strict_strtoul(buf
, 10, &val
);
3832 trace_flags
&= ~(1 << index
);
3835 trace_flags
|= 1 << index
;
3847 static const struct file_operations trace_options_core_fops
= {
3848 .open
= tracing_open_generic
,
3849 .read
= trace_options_core_read
,
3850 .write
= trace_options_core_write
,
3853 struct dentry
*trace_create_file(const char *name
,
3855 struct dentry
*parent
,
3857 const struct file_operations
*fops
)
3861 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
3863 pr_warning("Could not create debugfs '%s' entry\n", name
);
3869 static struct dentry
*trace_options_init_dentry(void)
3871 struct dentry
*d_tracer
;
3872 static struct dentry
*t_options
;
3877 d_tracer
= tracing_init_dentry();
3881 t_options
= debugfs_create_dir("options", d_tracer
);
3883 pr_warning("Could not create debugfs directory 'options'\n");
3891 create_trace_option_file(struct trace_option_dentry
*topt
,
3892 struct tracer_flags
*flags
,
3893 struct tracer_opt
*opt
)
3895 struct dentry
*t_options
;
3897 t_options
= trace_options_init_dentry();
3901 topt
->flags
= flags
;
3904 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
3905 &trace_options_fops
);
3909 static struct trace_option_dentry
*
3910 create_trace_option_files(struct tracer
*tracer
)
3912 struct trace_option_dentry
*topts
;
3913 struct tracer_flags
*flags
;
3914 struct tracer_opt
*opts
;
3920 flags
= tracer
->flags
;
3922 if (!flags
|| !flags
->opts
)
3927 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3930 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
3934 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3935 create_trace_option_file(&topts
[cnt
], flags
,
3942 destroy_trace_option_files(struct trace_option_dentry
*topts
)
3949 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
3950 if (topts
[cnt
].entry
)
3951 debugfs_remove(topts
[cnt
].entry
);
3957 static struct dentry
*
3958 create_trace_option_core_file(const char *option
, long index
)
3960 struct dentry
*t_options
;
3962 t_options
= trace_options_init_dentry();
3966 return trace_create_file(option
, 0644, t_options
, (void *)index
,
3967 &trace_options_core_fops
);
3970 static __init
void create_trace_options_dir(void)
3972 struct dentry
*t_options
;
3975 t_options
= trace_options_init_dentry();
3979 for (i
= 0; trace_options
[i
]; i
++)
3980 create_trace_option_core_file(trace_options
[i
], i
);
3983 static __init
int tracer_init_debugfs(void)
3985 struct dentry
*d_tracer
;
3988 d_tracer
= tracing_init_dentry();
3990 trace_create_file("tracing_enabled", 0644, d_tracer
,
3991 &global_trace
, &tracing_ctrl_fops
);
3993 trace_create_file("trace_options", 0644, d_tracer
,
3994 NULL
, &tracing_iter_fops
);
3996 trace_create_file("tracing_cpumask", 0644, d_tracer
,
3997 NULL
, &tracing_cpumask_fops
);
3999 trace_create_file("trace", 0644, d_tracer
,
4000 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
4002 trace_create_file("available_tracers", 0444, d_tracer
,
4003 &global_trace
, &show_traces_fops
);
4005 trace_create_file("current_tracer", 0444, d_tracer
,
4006 &global_trace
, &set_tracer_fops
);
4008 trace_create_file("tracing_max_latency", 0644, d_tracer
,
4009 &tracing_max_latency
, &tracing_max_lat_fops
);
4011 trace_create_file("tracing_thresh", 0644, d_tracer
,
4012 &tracing_thresh
, &tracing_max_lat_fops
);
4014 trace_create_file("README", 0644, d_tracer
,
4015 NULL
, &tracing_readme_fops
);
4017 trace_create_file("trace_pipe", 0444, d_tracer
,
4018 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
4020 trace_create_file("buffer_size_kb", 0644, d_tracer
,
4021 &global_trace
, &tracing_entries_fops
);
4023 trace_create_file("trace_marker", 0220, d_tracer
,
4024 NULL
, &tracing_mark_fops
);
4026 trace_create_file("saved_cmdlines", 0444, d_tracer
,
4027 NULL
, &tracing_saved_cmdlines_fops
);
4029 #ifdef CONFIG_DYNAMIC_FTRACE
4030 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
4031 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
4033 #ifdef CONFIG_SYSPROF_TRACER
4034 init_tracer_sysprof_debugfs(d_tracer
);
4037 create_trace_options_dir();
4039 for_each_tracing_cpu(cpu
)
4040 tracing_init_debugfs_percpu(cpu
);
4045 static int trace_panic_handler(struct notifier_block
*this,
4046 unsigned long event
, void *unused
)
4048 if (ftrace_dump_on_oops
)
4053 static struct notifier_block trace_panic_notifier
= {
4054 .notifier_call
= trace_panic_handler
,
4056 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
4059 static int trace_die_handler(struct notifier_block
*self
,
4065 if (ftrace_dump_on_oops
)
4074 static struct notifier_block trace_die_notifier
= {
4075 .notifier_call
= trace_die_handler
,
4080 * printk is set to max of 1024, we really don't need it that big.
4081 * Nothing should be printing 1000 characters anyway.
4083 #define TRACE_MAX_PRINT 1000
4086 * Define here KERN_TRACE so that we have one place to modify
4087 * it if we decide to change what log level the ftrace dump
4090 #define KERN_TRACE KERN_EMERG
4093 trace_printk_seq(struct trace_seq
*s
)
4095 /* Probably should print a warning here. */
4099 /* should be zero ended, but we are paranoid. */
4100 s
->buffer
[s
->len
] = 0;
4102 printk(KERN_TRACE
"%s", s
->buffer
);
4107 static void __ftrace_dump(bool disable_tracing
)
4109 static DEFINE_SPINLOCK(ftrace_dump_lock
);
4110 /* use static because iter can be a bit big for the stack */
4111 static struct trace_iterator iter
;
4112 unsigned int old_userobj
;
4113 static int dump_ran
;
4114 unsigned long flags
;
4118 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
4126 if (disable_tracing
)
4129 for_each_tracing_cpu(cpu
) {
4130 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
4133 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
4135 /* don't look at user memory in panic mode */
4136 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4138 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4140 /* Simulate the iterator */
4141 iter
.tr
= &global_trace
;
4142 iter
.trace
= current_trace
;
4143 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4146 * We need to stop all tracing on all CPUS to read the
4147 * the next buffer. This is a bit expensive, but is
4148 * not done often. We fill all what we can read,
4149 * and then release the locks again.
4152 while (!trace_empty(&iter
)) {
4155 printk(KERN_TRACE
"---------------------------------\n");
4159 /* reset all but tr, trace, and overruns */
4160 memset(&iter
.seq
, 0,
4161 sizeof(struct trace_iterator
) -
4162 offsetof(struct trace_iterator
, seq
));
4163 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4166 if (find_next_entry_inc(&iter
) != NULL
) {
4167 print_trace_line(&iter
);
4168 trace_consume(&iter
);
4171 trace_printk_seq(&iter
.seq
);
4175 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4177 printk(KERN_TRACE
"---------------------------------\n");
4179 /* Re-enable tracing if requested */
4180 if (!disable_tracing
) {
4181 trace_flags
|= old_userobj
;
4183 for_each_tracing_cpu(cpu
) {
4184 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
4190 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
4193 /* By default: disable tracing after the dump */
4194 void ftrace_dump(void)
4196 __ftrace_dump(true);
4199 __init
static int tracer_alloc_buffers(void)
4201 struct trace_array_cpu
*data
;
4206 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4209 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4210 goto out_free_buffer_mask
;
4212 if (!alloc_cpumask_var(&tracing_reader_cpumask
, GFP_KERNEL
))
4213 goto out_free_tracing_cpumask
;
4215 /* To save memory, keep the ring buffer size to its minimum */
4216 if (ring_buffer_expanded
)
4217 ring_buf_size
= trace_buf_size
;
4221 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4222 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4223 cpumask_clear(tracing_reader_cpumask
);
4225 /* TODO: make the number of buffers hot pluggable with CPUS */
4226 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
,
4227 TRACE_BUFFER_FLAGS
);
4228 if (!global_trace
.buffer
) {
4229 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4231 goto out_free_cpumask
;
4233 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4236 #ifdef CONFIG_TRACER_MAX_TRACE
4237 max_tr
.buffer
= ring_buffer_alloc(ring_buf_size
,
4238 TRACE_BUFFER_FLAGS
);
4239 if (!max_tr
.buffer
) {
4240 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4242 ring_buffer_free(global_trace
.buffer
);
4243 goto out_free_cpumask
;
4245 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
4246 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
4249 /* Allocate the first page for all buffers */
4250 for_each_tracing_cpu(i
) {
4251 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4252 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
4255 trace_init_cmdlines();
4257 register_tracer(&nop_trace
);
4258 current_trace
= &nop_trace
;
4259 #ifdef CONFIG_BOOT_TRACER
4260 register_tracer(&boot_tracer
);
4262 /* All seems OK, enable tracing */
4263 tracing_disabled
= 0;
4265 atomic_notifier_chain_register(&panic_notifier_list
,
4266 &trace_panic_notifier
);
4268 register_die_notifier(&trace_die_notifier
);
4273 free_cpumask_var(tracing_reader_cpumask
);
4274 out_free_tracing_cpumask
:
4275 free_cpumask_var(tracing_cpumask
);
4276 out_free_buffer_mask
:
4277 free_cpumask_var(tracing_buffer_mask
);
4282 __init
static int clear_boot_tracer(void)
4285 * The default tracer at boot buffer is an init section.
4286 * This function is called in lateinit. If we did not
4287 * find the boot tracer, then clear it out, to prevent
4288 * later registration from accessing the buffer that is
4289 * about to be freed.
4291 if (!default_bootup_tracer
)
4294 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4295 default_bootup_tracer
);
4296 default_bootup_tracer
= NULL
;
4301 early_initcall(tracer_alloc_buffers
);
4302 fs_initcall(tracer_init_debugfs
);
4303 late_initcall(clear_boot_tracer
);