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_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2426 size_t cnt
, loff_t
*ppos
)
2431 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2432 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2436 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2437 size_t cnt
, loff_t
*ppos
)
2439 struct trace_array
*tr
= filp
->private_data
;
2444 if (cnt
>= sizeof(buf
))
2447 if (copy_from_user(&buf
, ubuf
, cnt
))
2452 ret
= strict_strtoul(buf
, 10, &val
);
2458 mutex_lock(&trace_types_lock
);
2459 if (tracer_enabled
^ val
) {
2462 if (current_trace
->start
)
2463 current_trace
->start(tr
);
2468 if (current_trace
->stop
)
2469 current_trace
->stop(tr
);
2472 mutex_unlock(&trace_types_lock
);
2480 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2481 size_t cnt
, loff_t
*ppos
)
2483 char buf
[max_tracer_type_len
+2];
2486 mutex_lock(&trace_types_lock
);
2488 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2490 r
= sprintf(buf
, "\n");
2491 mutex_unlock(&trace_types_lock
);
2493 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2496 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2498 tracing_reset_online_cpus(tr
);
2502 static int tracing_resize_ring_buffer(unsigned long size
)
2507 * If kernel or user changes the size of the ring buffer
2508 * we use the size that was given, and we can forget about
2509 * expanding it later.
2511 ring_buffer_expanded
= 1;
2513 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2517 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2521 r
= ring_buffer_resize(global_trace
.buffer
,
2522 global_trace
.entries
);
2525 * AARGH! We are left with different
2526 * size max buffer!!!!
2527 * The max buffer is our "snapshot" buffer.
2528 * When a tracer needs a snapshot (one of the
2529 * latency tracers), it swaps the max buffer
2530 * with the saved snap shot. We succeeded to
2531 * update the size of the main buffer, but failed to
2532 * update the size of the max buffer. But when we tried
2533 * to reset the main buffer to the original size, we
2534 * failed there too. This is very unlikely to
2535 * happen, but if it does, warn and kill all
2539 tracing_disabled
= 1;
2544 global_trace
.entries
= size
;
2550 * tracing_update_buffers - used by tracing facility to expand ring buffers
2552 * To save on memory when the tracing is never used on a system with it
2553 * configured in. The ring buffers are set to a minimum size. But once
2554 * a user starts to use the tracing facility, then they need to grow
2555 * to their default size.
2557 * This function is to be called when a tracer is about to be used.
2559 int tracing_update_buffers(void)
2563 mutex_lock(&trace_types_lock
);
2564 if (!ring_buffer_expanded
)
2565 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2566 mutex_unlock(&trace_types_lock
);
2571 struct trace_option_dentry
;
2573 static struct trace_option_dentry
*
2574 create_trace_option_files(struct tracer
*tracer
);
2577 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2579 static int tracing_set_tracer(const char *buf
)
2581 static struct trace_option_dentry
*topts
;
2582 struct trace_array
*tr
= &global_trace
;
2586 mutex_lock(&trace_types_lock
);
2588 if (!ring_buffer_expanded
) {
2589 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2595 for (t
= trace_types
; t
; t
= t
->next
) {
2596 if (strcmp(t
->name
, buf
) == 0)
2603 if (t
== current_trace
)
2606 trace_branch_disable();
2607 if (current_trace
&& current_trace
->reset
)
2608 current_trace
->reset(tr
);
2610 destroy_trace_option_files(topts
);
2614 topts
= create_trace_option_files(current_trace
);
2617 ret
= tracer_init(t
, tr
);
2622 trace_branch_enable(tr
);
2624 mutex_unlock(&trace_types_lock
);
2630 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2631 size_t cnt
, loff_t
*ppos
)
2633 char buf
[max_tracer_type_len
+1];
2640 if (cnt
> max_tracer_type_len
)
2641 cnt
= max_tracer_type_len
;
2643 if (copy_from_user(&buf
, ubuf
, cnt
))
2648 /* strip ending whitespace. */
2649 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2652 err
= tracing_set_tracer(buf
);
2662 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2663 size_t cnt
, loff_t
*ppos
)
2665 unsigned long *ptr
= filp
->private_data
;
2669 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2670 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2671 if (r
> sizeof(buf
))
2673 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2677 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2678 size_t cnt
, loff_t
*ppos
)
2680 unsigned long *ptr
= filp
->private_data
;
2685 if (cnt
>= sizeof(buf
))
2688 if (copy_from_user(&buf
, ubuf
, cnt
))
2693 ret
= strict_strtoul(buf
, 10, &val
);
2702 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2704 long cpu_file
= (long) inode
->i_private
;
2705 struct trace_iterator
*iter
;
2708 if (tracing_disabled
)
2711 mutex_lock(&trace_types_lock
);
2713 /* We only allow one reader per cpu */
2714 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
2715 if (!cpumask_empty(tracing_reader_cpumask
)) {
2719 cpumask_setall(tracing_reader_cpumask
);
2721 if (!cpumask_test_cpu(cpu_file
, tracing_reader_cpumask
))
2722 cpumask_set_cpu(cpu_file
, tracing_reader_cpumask
);
2729 /* create a buffer to store the information to pass to userspace */
2730 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2737 * We make a copy of the current tracer to avoid concurrent
2738 * changes on it while we are reading.
2740 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2746 *iter
->trace
= *current_trace
;
2748 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2753 /* trace pipe does not show start of buffer */
2754 cpumask_setall(iter
->started
);
2756 iter
->cpu_file
= cpu_file
;
2757 iter
->tr
= &global_trace
;
2758 mutex_init(&iter
->mutex
);
2759 filp
->private_data
= iter
;
2761 if (iter
->trace
->pipe_open
)
2762 iter
->trace
->pipe_open(iter
);
2765 mutex_unlock(&trace_types_lock
);
2771 mutex_unlock(&trace_types_lock
);
2775 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2777 struct trace_iterator
*iter
= file
->private_data
;
2779 mutex_lock(&trace_types_lock
);
2781 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
)
2782 cpumask_clear(tracing_reader_cpumask
);
2784 cpumask_clear_cpu(iter
->cpu_file
, tracing_reader_cpumask
);
2786 mutex_unlock(&trace_types_lock
);
2788 free_cpumask_var(iter
->started
);
2789 mutex_destroy(&iter
->mutex
);
2797 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2799 struct trace_iterator
*iter
= filp
->private_data
;
2801 if (trace_flags
& TRACE_ITER_BLOCK
) {
2803 * Always select as readable when in blocking mode
2805 return POLLIN
| POLLRDNORM
;
2807 if (!trace_empty(iter
))
2808 return POLLIN
| POLLRDNORM
;
2809 poll_wait(filp
, &trace_wait
, poll_table
);
2810 if (!trace_empty(iter
))
2811 return POLLIN
| POLLRDNORM
;
2818 void default_wait_pipe(struct trace_iterator
*iter
)
2822 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
2824 if (trace_empty(iter
))
2827 finish_wait(&trace_wait
, &wait
);
2831 * This is a make-shift waitqueue.
2832 * A tracer might use this callback on some rare cases:
2834 * 1) the current tracer might hold the runqueue lock when it wakes up
2835 * a reader, hence a deadlock (sched, function, and function graph tracers)
2836 * 2) the function tracers, trace all functions, we don't want
2837 * the overhead of calling wake_up and friends
2838 * (and tracing them too)
2840 * Anyway, this is really very primitive wakeup.
2842 void poll_wait_pipe(struct trace_iterator
*iter
)
2844 set_current_state(TASK_INTERRUPTIBLE
);
2845 /* sleep for 100 msecs, and try again. */
2846 schedule_timeout(HZ
/ 10);
2849 /* Must be called with trace_types_lock mutex held. */
2850 static int tracing_wait_pipe(struct file
*filp
)
2852 struct trace_iterator
*iter
= filp
->private_data
;
2854 while (trace_empty(iter
)) {
2856 if ((filp
->f_flags
& O_NONBLOCK
)) {
2860 mutex_unlock(&iter
->mutex
);
2862 iter
->trace
->wait_pipe(iter
);
2864 mutex_lock(&iter
->mutex
);
2866 if (signal_pending(current
))
2870 * We block until we read something and tracing is disabled.
2871 * We still block if tracing is disabled, but we have never
2872 * read anything. This allows a user to cat this file, and
2873 * then enable tracing. But after we have read something,
2874 * we give an EOF when tracing is again disabled.
2876 * iter->pos will be 0 if we haven't read anything.
2878 if (!tracer_enabled
&& iter
->pos
)
2889 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2890 size_t cnt
, loff_t
*ppos
)
2892 struct trace_iterator
*iter
= filp
->private_data
;
2893 static struct tracer
*old_tracer
;
2896 /* return any leftover data */
2897 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2901 trace_seq_init(&iter
->seq
);
2903 /* copy the tracer to avoid using a global lock all around */
2904 mutex_lock(&trace_types_lock
);
2905 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
2906 old_tracer
= current_trace
;
2907 *iter
->trace
= *current_trace
;
2909 mutex_unlock(&trace_types_lock
);
2912 * Avoid more than one consumer on a single file descriptor
2913 * This is just a matter of traces coherency, the ring buffer itself
2916 mutex_lock(&iter
->mutex
);
2917 if (iter
->trace
->read
) {
2918 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2924 sret
= tracing_wait_pipe(filp
);
2928 /* stop when tracing is finished */
2929 if (trace_empty(iter
)) {
2934 if (cnt
>= PAGE_SIZE
)
2935 cnt
= PAGE_SIZE
- 1;
2937 /* reset all but tr, trace, and overruns */
2938 memset(&iter
->seq
, 0,
2939 sizeof(struct trace_iterator
) -
2940 offsetof(struct trace_iterator
, seq
));
2943 while (find_next_entry_inc(iter
) != NULL
) {
2944 enum print_line_t ret
;
2945 int len
= iter
->seq
.len
;
2947 ret
= print_trace_line(iter
);
2948 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2949 /* don't print partial lines */
2950 iter
->seq
.len
= len
;
2953 if (ret
!= TRACE_TYPE_NO_CONSUME
)
2954 trace_consume(iter
);
2956 if (iter
->seq
.len
>= cnt
)
2960 /* Now copy what we have to the user */
2961 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2962 if (iter
->seq
.readpos
>= iter
->seq
.len
)
2963 trace_seq_init(&iter
->seq
);
2966 * If there was nothing to send to user, inspite of consuming trace
2967 * entries, go back to wait for more entries.
2973 mutex_unlock(&iter
->mutex
);
2978 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
2979 struct pipe_buffer
*buf
)
2981 __free_page(buf
->page
);
2984 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
2987 __free_page(spd
->pages
[idx
]);
2990 static struct pipe_buf_operations tracing_pipe_buf_ops
= {
2992 .map
= generic_pipe_buf_map
,
2993 .unmap
= generic_pipe_buf_unmap
,
2994 .confirm
= generic_pipe_buf_confirm
,
2995 .release
= tracing_pipe_buf_release
,
2996 .steal
= generic_pipe_buf_steal
,
2997 .get
= generic_pipe_buf_get
,
3001 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
3006 /* Seq buffer is page-sized, exactly what we need. */
3008 count
= iter
->seq
.len
;
3009 ret
= print_trace_line(iter
);
3010 count
= iter
->seq
.len
- count
;
3013 iter
->seq
.len
-= count
;
3016 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3017 iter
->seq
.len
-= count
;
3021 trace_consume(iter
);
3023 if (!find_next_entry_inc(iter
)) {
3033 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3035 struct pipe_inode_info
*pipe
,
3039 struct page
*pages
[PIPE_BUFFERS
];
3040 struct partial_page partial
[PIPE_BUFFERS
];
3041 struct trace_iterator
*iter
= filp
->private_data
;
3042 struct splice_pipe_desc spd
= {
3045 .nr_pages
= 0, /* This gets updated below. */
3047 .ops
= &tracing_pipe_buf_ops
,
3048 .spd_release
= tracing_spd_release_pipe
,
3050 static struct tracer
*old_tracer
;
3055 /* copy the tracer to avoid using a global lock all around */
3056 mutex_lock(&trace_types_lock
);
3057 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3058 old_tracer
= current_trace
;
3059 *iter
->trace
= *current_trace
;
3061 mutex_unlock(&trace_types_lock
);
3063 mutex_lock(&iter
->mutex
);
3065 if (iter
->trace
->splice_read
) {
3066 ret
= iter
->trace
->splice_read(iter
, filp
,
3067 ppos
, pipe
, len
, flags
);
3072 ret
= tracing_wait_pipe(filp
);
3076 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
3081 /* Fill as many pages as possible. */
3082 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
3083 pages
[i
] = alloc_page(GFP_KERNEL
);
3087 rem
= tracing_fill_pipe_page(rem
, iter
);
3089 /* Copy the data into the page, so we can start over. */
3090 ret
= trace_seq_to_buffer(&iter
->seq
,
3091 page_address(pages
[i
]),
3094 __free_page(pages
[i
]);
3097 partial
[i
].offset
= 0;
3098 partial
[i
].len
= iter
->seq
.len
;
3100 trace_seq_init(&iter
->seq
);
3103 mutex_unlock(&iter
->mutex
);
3107 return splice_to_pipe(pipe
, &spd
);
3110 mutex_unlock(&iter
->mutex
);
3116 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3117 size_t cnt
, loff_t
*ppos
)
3119 struct trace_array
*tr
= filp
->private_data
;
3123 mutex_lock(&trace_types_lock
);
3124 if (!ring_buffer_expanded
)
3125 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3127 trace_buf_size
>> 10);
3129 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3130 mutex_unlock(&trace_types_lock
);
3132 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3136 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3137 size_t cnt
, loff_t
*ppos
)
3143 if (cnt
>= sizeof(buf
))
3146 if (copy_from_user(&buf
, ubuf
, cnt
))
3151 ret
= strict_strtoul(buf
, 10, &val
);
3155 /* must have at least 1 entry */
3159 mutex_lock(&trace_types_lock
);
3163 /* disable all cpu buffers */
3164 for_each_tracing_cpu(cpu
) {
3165 if (global_trace
.data
[cpu
])
3166 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3167 if (max_tr
.data
[cpu
])
3168 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3171 /* value is in KB */
3174 if (val
!= global_trace
.entries
) {
3175 ret
= tracing_resize_ring_buffer(val
);
3184 /* If check pages failed, return ENOMEM */
3185 if (tracing_disabled
)
3188 for_each_tracing_cpu(cpu
) {
3189 if (global_trace
.data
[cpu
])
3190 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3191 if (max_tr
.data
[cpu
])
3192 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3196 max_tr
.entries
= global_trace
.entries
;
3197 mutex_unlock(&trace_types_lock
);
3202 static int mark_printk(const char *fmt
, ...)
3206 va_start(args
, fmt
);
3207 ret
= trace_vprintk(0, fmt
, args
);
3213 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3214 size_t cnt
, loff_t
*fpos
)
3219 if (tracing_disabled
)
3222 if (cnt
> TRACE_BUF_SIZE
)
3223 cnt
= TRACE_BUF_SIZE
;
3225 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3229 if (copy_from_user(buf
, ubuf
, cnt
)) {
3234 /* Cut from the first nil or newline. */
3236 end
= strchr(buf
, '\n');
3240 cnt
= mark_printk("%s\n", buf
);
3247 static const struct file_operations tracing_max_lat_fops
= {
3248 .open
= tracing_open_generic
,
3249 .read
= tracing_max_lat_read
,
3250 .write
= tracing_max_lat_write
,
3253 static const struct file_operations tracing_ctrl_fops
= {
3254 .open
= tracing_open_generic
,
3255 .read
= tracing_ctrl_read
,
3256 .write
= tracing_ctrl_write
,
3259 static const struct file_operations set_tracer_fops
= {
3260 .open
= tracing_open_generic
,
3261 .read
= tracing_set_trace_read
,
3262 .write
= tracing_set_trace_write
,
3265 static const struct file_operations tracing_pipe_fops
= {
3266 .open
= tracing_open_pipe
,
3267 .poll
= tracing_poll_pipe
,
3268 .read
= tracing_read_pipe
,
3269 .splice_read
= tracing_splice_read_pipe
,
3270 .release
= tracing_release_pipe
,
3273 static const struct file_operations tracing_entries_fops
= {
3274 .open
= tracing_open_generic
,
3275 .read
= tracing_entries_read
,
3276 .write
= tracing_entries_write
,
3279 static const struct file_operations tracing_mark_fops
= {
3280 .open
= tracing_open_generic
,
3281 .write
= tracing_mark_write
,
3284 struct ftrace_buffer_info
{
3285 struct trace_array
*tr
;
3291 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3293 int cpu
= (int)(long)inode
->i_private
;
3294 struct ftrace_buffer_info
*info
;
3296 if (tracing_disabled
)
3299 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3303 info
->tr
= &global_trace
;
3306 /* Force reading ring buffer for first read */
3307 info
->read
= (unsigned int)-1;
3309 filp
->private_data
= info
;
3311 return nonseekable_open(inode
, filp
);
3315 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3316 size_t count
, loff_t
*ppos
)
3318 struct ftrace_buffer_info
*info
= filp
->private_data
;
3327 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3331 /* Do we have previous read data to read? */
3332 if (info
->read
< PAGE_SIZE
)
3337 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3344 pos
= ring_buffer_page_len(info
->spare
);
3346 if (pos
< PAGE_SIZE
)
3347 memset(info
->spare
+ pos
, 0, PAGE_SIZE
- pos
);
3350 size
= PAGE_SIZE
- info
->read
;
3354 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3365 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3367 struct ftrace_buffer_info
*info
= file
->private_data
;
3370 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3377 struct ring_buffer
*buffer
;
3382 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3383 struct pipe_buffer
*buf
)
3385 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3390 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3395 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3396 struct pipe_buffer
*buf
)
3401 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3402 struct pipe_buffer
*buf
)
3404 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3409 /* Pipe buffer operations for a buffer. */
3410 static struct pipe_buf_operations buffer_pipe_buf_ops
= {
3412 .map
= generic_pipe_buf_map
,
3413 .unmap
= generic_pipe_buf_unmap
,
3414 .confirm
= generic_pipe_buf_confirm
,
3415 .release
= buffer_pipe_buf_release
,
3416 .steal
= buffer_pipe_buf_steal
,
3417 .get
= buffer_pipe_buf_get
,
3421 * Callback from splice_to_pipe(), if we need to release some pages
3422 * at the end of the spd in case we error'ed out in filling the pipe.
3424 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3426 struct buffer_ref
*ref
=
3427 (struct buffer_ref
*)spd
->partial
[i
].private;
3432 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3434 spd
->partial
[i
].private = 0;
3438 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3439 struct pipe_inode_info
*pipe
, size_t len
,
3442 struct ftrace_buffer_info
*info
= file
->private_data
;
3443 struct partial_page partial
[PIPE_BUFFERS
];
3444 struct page
*pages
[PIPE_BUFFERS
];
3445 struct splice_pipe_desc spd
= {
3449 .ops
= &buffer_pipe_buf_ops
,
3450 .spd_release
= buffer_spd_release
,
3452 struct buffer_ref
*ref
;
3456 if (*ppos
& (PAGE_SIZE
- 1)) {
3457 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3461 if (len
& (PAGE_SIZE
- 1)) {
3462 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3463 if (len
< PAGE_SIZE
)
3468 for (i
= 0; i
< PIPE_BUFFERS
&& len
; i
++, len
-= PAGE_SIZE
) {
3472 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3476 ref
->buffer
= info
->tr
->buffer
;
3477 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3483 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3486 ring_buffer_free_read_page(ref
->buffer
,
3493 * zero out any left over data, this is going to
3496 size
= ring_buffer_page_len(ref
->page
);
3497 if (size
< PAGE_SIZE
)
3498 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3500 page
= virt_to_page(ref
->page
);
3502 spd
.pages
[i
] = page
;
3503 spd
.partial
[i
].len
= PAGE_SIZE
;
3504 spd
.partial
[i
].offset
= 0;
3505 spd
.partial
[i
].private = (unsigned long)ref
;
3512 /* did we read anything? */
3513 if (!spd
.nr_pages
) {
3514 if (flags
& SPLICE_F_NONBLOCK
)
3522 ret
= splice_to_pipe(pipe
, &spd
);
3527 static const struct file_operations tracing_buffers_fops
= {
3528 .open
= tracing_buffers_open
,
3529 .read
= tracing_buffers_read
,
3530 .release
= tracing_buffers_release
,
3531 .splice_read
= tracing_buffers_splice_read
,
3532 .llseek
= no_llseek
,
3535 #ifdef CONFIG_DYNAMIC_FTRACE
3537 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3543 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3544 size_t cnt
, loff_t
*ppos
)
3546 static char ftrace_dyn_info_buffer
[1024];
3547 static DEFINE_MUTEX(dyn_info_mutex
);
3548 unsigned long *p
= filp
->private_data
;
3549 char *buf
= ftrace_dyn_info_buffer
;
3550 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3553 mutex_lock(&dyn_info_mutex
);
3554 r
= sprintf(buf
, "%ld ", *p
);
3556 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3559 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3561 mutex_unlock(&dyn_info_mutex
);
3566 static const struct file_operations tracing_dyn_info_fops
= {
3567 .open
= tracing_open_generic
,
3568 .read
= tracing_read_dyn_info
,
3572 static struct dentry
*d_tracer
;
3574 struct dentry
*tracing_init_dentry(void)
3581 if (!debugfs_initialized())
3584 d_tracer
= debugfs_create_dir("tracing", NULL
);
3586 if (!d_tracer
&& !once
) {
3588 pr_warning("Could not create debugfs directory 'tracing'\n");
3595 static struct dentry
*d_percpu
;
3597 struct dentry
*tracing_dentry_percpu(void)
3600 struct dentry
*d_tracer
;
3605 d_tracer
= tracing_init_dentry();
3610 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
3612 if (!d_percpu
&& !once
) {
3614 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3621 static void tracing_init_debugfs_percpu(long cpu
)
3623 struct dentry
*d_percpu
= tracing_dentry_percpu();
3624 struct dentry
*d_cpu
;
3625 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3628 if (cpu
> 999 || cpu
< 0)
3631 sprintf(cpu_dir
, "cpu%ld", cpu
);
3632 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
3634 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
3638 /* per cpu trace_pipe */
3639 trace_create_file("trace_pipe", 0444, d_cpu
,
3640 (void *) cpu
, &tracing_pipe_fops
);
3643 trace_create_file("trace", 0644, d_cpu
,
3644 (void *) cpu
, &tracing_fops
);
3646 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
3647 (void *) cpu
, &tracing_buffers_fops
);
3650 #ifdef CONFIG_FTRACE_SELFTEST
3651 /* Let selftest have access to static functions in this file */
3652 #include "trace_selftest.c"
3655 struct trace_option_dentry
{
3656 struct tracer_opt
*opt
;
3657 struct tracer_flags
*flags
;
3658 struct dentry
*entry
;
3662 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3665 struct trace_option_dentry
*topt
= filp
->private_data
;
3668 if (topt
->flags
->val
& topt
->opt
->bit
)
3673 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3677 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3680 struct trace_option_dentry
*topt
= filp
->private_data
;
3685 if (cnt
>= sizeof(buf
))
3688 if (copy_from_user(&buf
, ubuf
, cnt
))
3693 ret
= strict_strtoul(buf
, 10, &val
);
3700 /* do nothing if already cleared */
3701 if (!(topt
->flags
->val
& topt
->opt
->bit
))
3704 mutex_lock(&trace_types_lock
);
3705 if (current_trace
->set_flag
)
3706 ret
= current_trace
->set_flag(topt
->flags
->val
,
3708 mutex_unlock(&trace_types_lock
);
3711 topt
->flags
->val
&= ~topt
->opt
->bit
;
3714 /* do nothing if already set */
3715 if (topt
->flags
->val
& topt
->opt
->bit
)
3718 mutex_lock(&trace_types_lock
);
3719 if (current_trace
->set_flag
)
3720 ret
= current_trace
->set_flag(topt
->flags
->val
,
3722 mutex_unlock(&trace_types_lock
);
3725 topt
->flags
->val
|= topt
->opt
->bit
;
3738 static const struct file_operations trace_options_fops
= {
3739 .open
= tracing_open_generic
,
3740 .read
= trace_options_read
,
3741 .write
= trace_options_write
,
3745 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3748 long index
= (long)filp
->private_data
;
3751 if (trace_flags
& (1 << index
))
3756 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3760 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3763 long index
= (long)filp
->private_data
;
3768 if (cnt
>= sizeof(buf
))
3771 if (copy_from_user(&buf
, ubuf
, cnt
))
3776 ret
= strict_strtoul(buf
, 10, &val
);
3782 trace_flags
&= ~(1 << index
);
3785 trace_flags
|= 1 << index
;
3797 static const struct file_operations trace_options_core_fops
= {
3798 .open
= tracing_open_generic
,
3799 .read
= trace_options_core_read
,
3800 .write
= trace_options_core_write
,
3803 struct dentry
*trace_create_file(const char *name
,
3805 struct dentry
*parent
,
3807 const struct file_operations
*fops
)
3811 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
3813 pr_warning("Could not create debugfs '%s' entry\n", name
);
3819 static struct dentry
*trace_options_init_dentry(void)
3821 struct dentry
*d_tracer
;
3822 static struct dentry
*t_options
;
3827 d_tracer
= tracing_init_dentry();
3831 t_options
= debugfs_create_dir("options", d_tracer
);
3833 pr_warning("Could not create debugfs directory 'options'\n");
3841 create_trace_option_file(struct trace_option_dentry
*topt
,
3842 struct tracer_flags
*flags
,
3843 struct tracer_opt
*opt
)
3845 struct dentry
*t_options
;
3847 t_options
= trace_options_init_dentry();
3851 topt
->flags
= flags
;
3854 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
3855 &trace_options_fops
);
3859 static struct trace_option_dentry
*
3860 create_trace_option_files(struct tracer
*tracer
)
3862 struct trace_option_dentry
*topts
;
3863 struct tracer_flags
*flags
;
3864 struct tracer_opt
*opts
;
3870 flags
= tracer
->flags
;
3872 if (!flags
|| !flags
->opts
)
3877 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3880 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
3884 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3885 create_trace_option_file(&topts
[cnt
], flags
,
3892 destroy_trace_option_files(struct trace_option_dentry
*topts
)
3899 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
3900 if (topts
[cnt
].entry
)
3901 debugfs_remove(topts
[cnt
].entry
);
3907 static struct dentry
*
3908 create_trace_option_core_file(const char *option
, long index
)
3910 struct dentry
*t_options
;
3912 t_options
= trace_options_init_dentry();
3916 return trace_create_file(option
, 0644, t_options
, (void *)index
,
3917 &trace_options_core_fops
);
3920 static __init
void create_trace_options_dir(void)
3922 struct dentry
*t_options
;
3925 t_options
= trace_options_init_dentry();
3929 for (i
= 0; trace_options
[i
]; i
++)
3930 create_trace_option_core_file(trace_options
[i
], i
);
3933 static __init
int tracer_init_debugfs(void)
3935 struct dentry
*d_tracer
;
3938 d_tracer
= tracing_init_dentry();
3940 trace_create_file("tracing_enabled", 0644, d_tracer
,
3941 &global_trace
, &tracing_ctrl_fops
);
3943 trace_create_file("trace_options", 0644, d_tracer
,
3944 NULL
, &tracing_iter_fops
);
3946 trace_create_file("tracing_cpumask", 0644, d_tracer
,
3947 NULL
, &tracing_cpumask_fops
);
3949 trace_create_file("trace", 0644, d_tracer
,
3950 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
3952 trace_create_file("available_tracers", 0444, d_tracer
,
3953 &global_trace
, &show_traces_fops
);
3955 trace_create_file("current_tracer", 0444, d_tracer
,
3956 &global_trace
, &set_tracer_fops
);
3958 trace_create_file("tracing_max_latency", 0644, d_tracer
,
3959 &tracing_max_latency
, &tracing_max_lat_fops
);
3961 trace_create_file("tracing_thresh", 0644, d_tracer
,
3962 &tracing_thresh
, &tracing_max_lat_fops
);
3964 trace_create_file("README", 0644, d_tracer
,
3965 NULL
, &tracing_readme_fops
);
3967 trace_create_file("trace_pipe", 0444, d_tracer
,
3968 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
3970 trace_create_file("buffer_size_kb", 0644, d_tracer
,
3971 &global_trace
, &tracing_entries_fops
);
3973 trace_create_file("trace_marker", 0220, d_tracer
,
3974 NULL
, &tracing_mark_fops
);
3976 #ifdef CONFIG_DYNAMIC_FTRACE
3977 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
3978 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
3980 #ifdef CONFIG_SYSPROF_TRACER
3981 init_tracer_sysprof_debugfs(d_tracer
);
3984 create_trace_options_dir();
3986 for_each_tracing_cpu(cpu
)
3987 tracing_init_debugfs_percpu(cpu
);
3992 static int trace_panic_handler(struct notifier_block
*this,
3993 unsigned long event
, void *unused
)
3995 if (ftrace_dump_on_oops
)
4000 static struct notifier_block trace_panic_notifier
= {
4001 .notifier_call
= trace_panic_handler
,
4003 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
4006 static int trace_die_handler(struct notifier_block
*self
,
4012 if (ftrace_dump_on_oops
)
4021 static struct notifier_block trace_die_notifier
= {
4022 .notifier_call
= trace_die_handler
,
4027 * printk is set to max of 1024, we really don't need it that big.
4028 * Nothing should be printing 1000 characters anyway.
4030 #define TRACE_MAX_PRINT 1000
4033 * Define here KERN_TRACE so that we have one place to modify
4034 * it if we decide to change what log level the ftrace dump
4037 #define KERN_TRACE KERN_EMERG
4040 trace_printk_seq(struct trace_seq
*s
)
4042 /* Probably should print a warning here. */
4046 /* should be zero ended, but we are paranoid. */
4047 s
->buffer
[s
->len
] = 0;
4049 printk(KERN_TRACE
"%s", s
->buffer
);
4054 static void __ftrace_dump(bool disable_tracing
)
4056 static DEFINE_SPINLOCK(ftrace_dump_lock
);
4057 /* use static because iter can be a bit big for the stack */
4058 static struct trace_iterator iter
;
4059 unsigned int old_userobj
;
4060 static int dump_ran
;
4061 unsigned long flags
;
4065 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
4073 if (disable_tracing
)
4076 for_each_tracing_cpu(cpu
) {
4077 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
4080 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
4082 /* don't look at user memory in panic mode */
4083 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4085 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4087 /* Simulate the iterator */
4088 iter
.tr
= &global_trace
;
4089 iter
.trace
= current_trace
;
4090 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4093 * We need to stop all tracing on all CPUS to read the
4094 * the next buffer. This is a bit expensive, but is
4095 * not done often. We fill all what we can read,
4096 * and then release the locks again.
4099 while (!trace_empty(&iter
)) {
4102 printk(KERN_TRACE
"---------------------------------\n");
4106 /* reset all but tr, trace, and overruns */
4107 memset(&iter
.seq
, 0,
4108 sizeof(struct trace_iterator
) -
4109 offsetof(struct trace_iterator
, seq
));
4110 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4113 if (find_next_entry_inc(&iter
) != NULL
) {
4114 print_trace_line(&iter
);
4115 trace_consume(&iter
);
4118 trace_printk_seq(&iter
.seq
);
4122 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4124 printk(KERN_TRACE
"---------------------------------\n");
4126 /* Re-enable tracing if requested */
4127 if (!disable_tracing
) {
4128 trace_flags
|= old_userobj
;
4130 for_each_tracing_cpu(cpu
) {
4131 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
4137 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
4140 /* By default: disable tracing after the dump */
4141 void ftrace_dump(void)
4143 __ftrace_dump(true);
4146 __init
static int tracer_alloc_buffers(void)
4148 struct trace_array_cpu
*data
;
4153 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4156 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4157 goto out_free_buffer_mask
;
4159 if (!alloc_cpumask_var(&tracing_reader_cpumask
, GFP_KERNEL
))
4160 goto out_free_tracing_cpumask
;
4162 /* To save memory, keep the ring buffer size to its minimum */
4163 if (ring_buffer_expanded
)
4164 ring_buf_size
= trace_buf_size
;
4168 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4169 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4170 cpumask_clear(tracing_reader_cpumask
);
4172 /* TODO: make the number of buffers hot pluggable with CPUS */
4173 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
,
4174 TRACE_BUFFER_FLAGS
);
4175 if (!global_trace
.buffer
) {
4176 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4178 goto out_free_cpumask
;
4180 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4183 #ifdef CONFIG_TRACER_MAX_TRACE
4184 max_tr
.buffer
= ring_buffer_alloc(ring_buf_size
,
4185 TRACE_BUFFER_FLAGS
);
4186 if (!max_tr
.buffer
) {
4187 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4189 ring_buffer_free(global_trace
.buffer
);
4190 goto out_free_cpumask
;
4192 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
4193 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
4196 /* Allocate the first page for all buffers */
4197 for_each_tracing_cpu(i
) {
4198 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4199 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
4202 trace_init_cmdlines();
4204 register_tracer(&nop_trace
);
4205 current_trace
= &nop_trace
;
4206 #ifdef CONFIG_BOOT_TRACER
4207 register_tracer(&boot_tracer
);
4209 /* All seems OK, enable tracing */
4210 tracing_disabled
= 0;
4212 atomic_notifier_chain_register(&panic_notifier_list
,
4213 &trace_panic_notifier
);
4215 register_die_notifier(&trace_die_notifier
);
4220 free_cpumask_var(tracing_reader_cpumask
);
4221 out_free_tracing_cpumask
:
4222 free_cpumask_var(tracing_cpumask
);
4223 out_free_buffer_mask
:
4224 free_cpumask_var(tracing_buffer_mask
);
4229 __init
static int clear_boot_tracer(void)
4232 * The default tracer at boot buffer is an init section.
4233 * This function is called in lateinit. If we did not
4234 * find the boot tracer, then clear it out, to prevent
4235 * later registration from accessing the buffer that is
4236 * about to be freed.
4238 if (!default_bootup_tracer
)
4241 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4242 default_bootup_tracer
);
4243 default_bootup_tracer
= NULL
;
4248 early_initcall(tracer_alloc_buffers
);
4249 fs_initcall(tracer_init_debugfs
);
4250 late_initcall(clear_boot_tracer
);