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/ctype.h>
34 #include <linux/init.h>
35 #include <linux/poll.h>
36 #include <linux/gfp.h>
40 #include "trace_output.h"
42 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
44 unsigned long __read_mostly tracing_max_latency
;
45 unsigned long __read_mostly tracing_thresh
;
48 * On boot up, the ring buffer is set to the minimum size, so that
49 * we do not waste memory on systems that are not using tracing.
51 static int ring_buffer_expanded
;
54 * We need to change this state when a selftest is running.
55 * A selftest will lurk into the ring-buffer to count the
56 * entries inserted during the selftest although some concurrent
57 * insertions into the ring-buffer such as trace_printk could occurred
58 * at the same time, giving false positive or negative results.
60 static bool __read_mostly tracing_selftest_running
;
63 * If a tracer is running, we do not want to run SELFTEST.
65 static bool __read_mostly tracing_selftest_disabled
;
67 /* For tracers that don't implement custom flags */
68 static struct tracer_opt dummy_tracer_opt
[] = {
72 static struct tracer_flags dummy_tracer_flags
= {
74 .opts
= dummy_tracer_opt
77 static int dummy_set_flag(u32 old_flags
, u32 bit
, int set
)
83 * Kill all tracing for good (never come back).
84 * It is initialized to 1 but will turn to zero if the initialization
85 * of the tracer is successful. But that is the only place that sets
88 static int tracing_disabled
= 1;
90 static DEFINE_PER_CPU(local_t
, ftrace_cpu_disabled
);
92 static inline void ftrace_disable_cpu(void)
95 local_inc(&__get_cpu_var(ftrace_cpu_disabled
));
98 static inline void ftrace_enable_cpu(void)
100 local_dec(&__get_cpu_var(ftrace_cpu_disabled
));
104 static cpumask_var_t __read_mostly tracing_buffer_mask
;
106 /* Define which cpu buffers are currently read in trace_pipe */
107 static cpumask_var_t tracing_reader_cpumask
;
109 #define for_each_tracing_cpu(cpu) \
110 for_each_cpu(cpu, tracing_buffer_mask)
113 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
115 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
116 * is set, then ftrace_dump is called. This will output the contents
117 * of the ftrace buffers to the console. This is very useful for
118 * capturing traces that lead to crashes and outputing it to a
121 * It is default off, but you can enable it with either specifying
122 * "ftrace_dump_on_oops" in the kernel command line, or setting
123 * /proc/sys/kernel/ftrace_dump_on_oops to true.
125 int ftrace_dump_on_oops
;
127 static int tracing_set_tracer(const char *buf
);
129 #define BOOTUP_TRACER_SIZE 100
130 static char bootup_tracer_buf
[BOOTUP_TRACER_SIZE
] __initdata
;
131 static char *default_bootup_tracer
;
133 static int __init
set_ftrace(char *str
)
135 strncpy(bootup_tracer_buf
, str
, BOOTUP_TRACER_SIZE
);
136 default_bootup_tracer
= bootup_tracer_buf
;
137 /* We are using ftrace early, expand it */
138 ring_buffer_expanded
= 1;
141 __setup("ftrace=", set_ftrace
);
143 static int __init
set_ftrace_dump_on_oops(char *str
)
145 ftrace_dump_on_oops
= 1;
148 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
151 ns2usecs(cycle_t nsec
)
158 cycle_t
ftrace_now(int cpu
)
160 u64 ts
= ring_buffer_time_stamp(cpu
);
161 ring_buffer_normalize_time_stamp(cpu
, &ts
);
166 * The global_trace is the descriptor that holds the tracing
167 * buffers for the live tracing. For each CPU, it contains
168 * a link list of pages that will store trace entries. The
169 * page descriptor of the pages in the memory is used to hold
170 * the link list by linking the lru item in the page descriptor
171 * to each of the pages in the buffer per CPU.
173 * For each active CPU there is a data field that holds the
174 * pages for the buffer for that CPU. Each CPU has the same number
175 * of pages allocated for its buffer.
177 static struct trace_array global_trace
;
179 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
182 * The max_tr is used to snapshot the global_trace when a maximum
183 * latency is reached. Some tracers will use this to store a maximum
184 * trace while it continues examining live traces.
186 * The buffers for the max_tr are set up the same as the global_trace.
187 * When a snapshot is taken, the link list of the max_tr is swapped
188 * with the link list of the global_trace and the buffers are reset for
189 * the global_trace so the tracing can continue.
191 static struct trace_array max_tr
;
193 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
195 /* tracer_enabled is used to toggle activation of a tracer */
196 static int tracer_enabled
= 1;
199 * tracing_is_enabled - return tracer_enabled status
201 * This function is used by other tracers to know the status
202 * of the tracer_enabled flag. Tracers may use this function
203 * to know if it should enable their features when starting
204 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
206 int tracing_is_enabled(void)
208 return tracer_enabled
;
212 * trace_buf_size is the size in bytes that is allocated
213 * for a buffer. Note, the number of bytes is always rounded
216 * This number is purposely set to a low number of 16384.
217 * If the dump on oops happens, it will be much appreciated
218 * to not have to wait for all that output. Anyway this can be
219 * boot time and run time configurable.
221 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
223 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
225 /* trace_types holds a link list of available tracers. */
226 static struct tracer
*trace_types __read_mostly
;
228 /* current_trace points to the tracer that is currently active */
229 static struct tracer
*current_trace __read_mostly
;
232 * max_tracer_type_len is used to simplify the allocating of
233 * buffers to read userspace tracer names. We keep track of
234 * the longest tracer name registered.
236 static int max_tracer_type_len
;
239 * trace_types_lock is used to protect the trace_types list.
240 * This lock is also used to keep user access serialized.
241 * Accesses from userspace will grab this lock while userspace
242 * activities happen inside the kernel.
244 static DEFINE_MUTEX(trace_types_lock
);
246 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
247 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
249 /* trace_flags holds trace_options default values */
250 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
251 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
;
254 * trace_wake_up - wake up tasks waiting for trace input
256 * Simply wakes up any task that is blocked on the trace_wait
257 * queue. These is used with trace_poll for tasks polling the trace.
259 void trace_wake_up(void)
262 * The runqueue_is_locked() can fail, but this is the best we
265 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
266 wake_up(&trace_wait
);
269 static int __init
set_buf_size(char *str
)
271 unsigned long buf_size
;
276 ret
= strict_strtoul(str
, 0, &buf_size
);
277 /* nr_entries can not be zero */
278 if (ret
< 0 || buf_size
== 0)
280 trace_buf_size
= buf_size
;
283 __setup("trace_buf_size=", set_buf_size
);
285 unsigned long nsecs_to_usecs(unsigned long nsecs
)
290 /* These must match the bit postions in trace_iterator_flags */
291 static const char *trace_options
[] = {
315 * ftrace_max_lock is used to protect the swapping of buffers
316 * when taking a max snapshot. The buffers themselves are
317 * protected by per_cpu spinlocks. But the action of the swap
318 * needs its own lock.
320 * This is defined as a raw_spinlock_t in order to help
321 * with performance when lockdep debugging is enabled.
323 static raw_spinlock_t ftrace_max_lock
=
324 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
327 * Copy the new maximum trace into the separate maximum-trace
328 * structure. (this way the maximum trace is permanently saved,
329 * for later retrieval via /debugfs/tracing/latency_trace)
332 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
334 struct trace_array_cpu
*data
= tr
->data
[cpu
];
337 max_tr
.time_start
= data
->preempt_timestamp
;
339 data
= max_tr
.data
[cpu
];
340 data
->saved_latency
= tracing_max_latency
;
342 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
343 data
->pid
= tsk
->pid
;
344 data
->uid
= task_uid(tsk
);
345 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
346 data
->policy
= tsk
->policy
;
347 data
->rt_priority
= tsk
->rt_priority
;
349 /* record this tasks comm */
350 tracing_record_cmdline(tsk
);
353 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
361 if (s
->len
<= s
->readpos
)
364 len
= s
->len
- s
->readpos
;
367 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
377 ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
382 if (s
->len
<= s
->readpos
)
385 len
= s
->len
- s
->readpos
;
388 ret
= memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
397 trace_print_seq(struct seq_file
*m
, struct trace_seq
*s
)
399 int len
= s
->len
>= PAGE_SIZE
? PAGE_SIZE
- 1 : s
->len
;
402 seq_puts(m
, s
->buffer
);
408 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
410 * @tsk: the task with the latency
411 * @cpu: The cpu that initiated the trace.
413 * Flip the buffers between the @tr and the max_tr and record information
414 * about which task was the cause of this latency.
417 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
419 struct ring_buffer
*buf
= tr
->buffer
;
421 WARN_ON_ONCE(!irqs_disabled());
422 __raw_spin_lock(&ftrace_max_lock
);
424 tr
->buffer
= max_tr
.buffer
;
427 ftrace_disable_cpu();
428 ring_buffer_reset(tr
->buffer
);
431 __update_max_tr(tr
, tsk
, cpu
);
432 __raw_spin_unlock(&ftrace_max_lock
);
436 * update_max_tr_single - only copy one trace over, and reset the rest
438 * @tsk - task with the latency
439 * @cpu - the cpu of the buffer to copy.
441 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
444 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
448 WARN_ON_ONCE(!irqs_disabled());
449 __raw_spin_lock(&ftrace_max_lock
);
451 ftrace_disable_cpu();
453 ring_buffer_reset(max_tr
.buffer
);
454 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
458 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
);
460 __update_max_tr(tr
, tsk
, cpu
);
461 __raw_spin_unlock(&ftrace_max_lock
);
465 * register_tracer - register a tracer with the ftrace system.
466 * @type - the plugin for the tracer
468 * Register a new plugin tracer.
470 int register_tracer(struct tracer
*type
)
471 __releases(kernel_lock
)
472 __acquires(kernel_lock
)
479 pr_info("Tracer must have a name\n");
484 * When this gets called we hold the BKL which means that
485 * preemption is disabled. Various trace selftests however
486 * need to disable and enable preemption for successful tests.
487 * So we drop the BKL here and grab it after the tests again.
490 mutex_lock(&trace_types_lock
);
492 tracing_selftest_running
= true;
494 for (t
= trace_types
; t
; t
= t
->next
) {
495 if (strcmp(type
->name
, t
->name
) == 0) {
497 pr_info("Trace %s already registered\n",
505 type
->set_flag
= &dummy_set_flag
;
507 type
->flags
= &dummy_tracer_flags
;
509 if (!type
->flags
->opts
)
510 type
->flags
->opts
= dummy_tracer_opt
;
511 if (!type
->wait_pipe
)
512 type
->wait_pipe
= default_wait_pipe
;
515 #ifdef CONFIG_FTRACE_STARTUP_TEST
516 if (type
->selftest
&& !tracing_selftest_disabled
) {
517 struct tracer
*saved_tracer
= current_trace
;
518 struct trace_array
*tr
= &global_trace
;
522 * Run a selftest on this tracer.
523 * Here we reset the trace buffer, and set the current
524 * tracer to be this tracer. The tracer can then run some
525 * internal tracing to verify that everything is in order.
526 * If we fail, we do not register this tracer.
528 for_each_tracing_cpu(i
)
529 tracing_reset(tr
, i
);
531 current_trace
= type
;
532 /* the test is responsible for initializing and enabling */
533 pr_info("Testing tracer %s: ", type
->name
);
534 ret
= type
->selftest(type
, tr
);
535 /* the test is responsible for resetting too */
536 current_trace
= saved_tracer
;
538 printk(KERN_CONT
"FAILED!\n");
541 /* Only reset on passing, to avoid touching corrupted buffers */
542 for_each_tracing_cpu(i
)
543 tracing_reset(tr
, i
);
545 printk(KERN_CONT
"PASSED\n");
549 type
->next
= trace_types
;
551 len
= strlen(type
->name
);
552 if (len
> max_tracer_type_len
)
553 max_tracer_type_len
= len
;
556 tracing_selftest_running
= false;
557 mutex_unlock(&trace_types_lock
);
559 if (ret
|| !default_bootup_tracer
)
562 if (strncmp(default_bootup_tracer
, type
->name
, BOOTUP_TRACER_SIZE
))
565 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
566 /* Do we want this tracer to start on bootup? */
567 tracing_set_tracer(type
->name
);
568 default_bootup_tracer
= NULL
;
569 /* disable other selftests, since this will break it. */
570 tracing_selftest_disabled
= 1;
571 #ifdef CONFIG_FTRACE_STARTUP_TEST
572 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
581 void unregister_tracer(struct tracer
*type
)
586 mutex_lock(&trace_types_lock
);
587 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
591 pr_info("Trace %s not registered\n", type
->name
);
597 if (type
== current_trace
&& tracer_enabled
) {
600 if (current_trace
->stop
)
601 current_trace
->stop(&global_trace
);
602 current_trace
= &nop_trace
;
605 if (strlen(type
->name
) != max_tracer_type_len
)
608 max_tracer_type_len
= 0;
609 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
610 len
= strlen((*t
)->name
);
611 if (len
> max_tracer_type_len
)
612 max_tracer_type_len
= len
;
615 mutex_unlock(&trace_types_lock
);
618 void tracing_reset(struct trace_array
*tr
, int cpu
)
620 ftrace_disable_cpu();
621 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
625 void tracing_reset_online_cpus(struct trace_array
*tr
)
629 tr
->time_start
= ftrace_now(tr
->cpu
);
631 for_each_online_cpu(cpu
)
632 tracing_reset(tr
, cpu
);
635 #define SAVED_CMDLINES 128
636 #define NO_CMDLINE_MAP UINT_MAX
637 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
638 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
639 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
640 static int cmdline_idx
;
641 static raw_spinlock_t trace_cmdline_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
643 /* temporary disable recording */
644 static atomic_t trace_record_cmdline_disabled __read_mostly
;
646 static void trace_init_cmdlines(void)
648 memset(&map_pid_to_cmdline
, NO_CMDLINE_MAP
, sizeof(map_pid_to_cmdline
));
649 memset(&map_cmdline_to_pid
, NO_CMDLINE_MAP
, sizeof(map_cmdline_to_pid
));
653 static int trace_stop_count
;
654 static DEFINE_SPINLOCK(tracing_start_lock
);
657 * ftrace_off_permanent - disable all ftrace code permanently
659 * This should only be called when a serious anomally has
660 * been detected. This will turn off the function tracing,
661 * ring buffers, and other tracing utilites. It takes no
662 * locks and can be called from any context.
664 void ftrace_off_permanent(void)
666 tracing_disabled
= 1;
668 tracing_off_permanent();
672 * tracing_start - quick start of the tracer
674 * If tracing is enabled but was stopped by tracing_stop,
675 * this will start the tracer back up.
677 void tracing_start(void)
679 struct ring_buffer
*buffer
;
682 if (tracing_disabled
)
685 spin_lock_irqsave(&tracing_start_lock
, flags
);
686 if (--trace_stop_count
) {
687 if (trace_stop_count
< 0) {
688 /* Someone screwed up their debugging */
690 trace_stop_count
= 0;
696 buffer
= global_trace
.buffer
;
698 ring_buffer_record_enable(buffer
);
700 buffer
= max_tr
.buffer
;
702 ring_buffer_record_enable(buffer
);
706 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
710 * tracing_stop - quick stop of the tracer
712 * Light weight way to stop tracing. Use in conjunction with
715 void tracing_stop(void)
717 struct ring_buffer
*buffer
;
721 spin_lock_irqsave(&tracing_start_lock
, flags
);
722 if (trace_stop_count
++)
725 buffer
= global_trace
.buffer
;
727 ring_buffer_record_disable(buffer
);
729 buffer
= max_tr
.buffer
;
731 ring_buffer_record_disable(buffer
);
734 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
737 void trace_stop_cmdline_recording(void);
739 static void trace_save_cmdline(struct task_struct
*tsk
)
743 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
747 * It's not the end of the world if we don't get
748 * the lock, but we also don't want to spin
749 * nor do we want to disable interrupts,
750 * so if we miss here, then better luck next time.
752 if (!__raw_spin_trylock(&trace_cmdline_lock
))
755 idx
= map_pid_to_cmdline
[tsk
->pid
];
756 if (idx
== NO_CMDLINE_MAP
) {
757 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
760 * Check whether the cmdline buffer at idx has a pid
761 * mapped. We are going to overwrite that entry so we
762 * need to clear the map_pid_to_cmdline. Otherwise we
763 * would read the new comm for the old pid.
765 pid
= map_cmdline_to_pid
[idx
];
766 if (pid
!= NO_CMDLINE_MAP
)
767 map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
769 map_cmdline_to_pid
[idx
] = tsk
->pid
;
770 map_pid_to_cmdline
[tsk
->pid
] = idx
;
775 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
777 __raw_spin_unlock(&trace_cmdline_lock
);
780 void trace_find_cmdline(int pid
, char comm
[])
785 strcpy(comm
, "<idle>");
789 if (pid
> PID_MAX_DEFAULT
) {
790 strcpy(comm
, "<...>");
794 __raw_spin_lock(&trace_cmdline_lock
);
795 map
= map_pid_to_cmdline
[pid
];
796 if (map
!= NO_CMDLINE_MAP
)
797 strcpy(comm
, saved_cmdlines
[map
]);
799 strcpy(comm
, "<...>");
801 __raw_spin_unlock(&trace_cmdline_lock
);
804 void tracing_record_cmdline(struct task_struct
*tsk
)
806 if (atomic_read(&trace_record_cmdline_disabled
) || !tracer_enabled
||
810 trace_save_cmdline(tsk
);
814 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
817 struct task_struct
*tsk
= current
;
819 entry
->preempt_count
= pc
& 0xff;
820 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
821 entry
->tgid
= (tsk
) ? tsk
->tgid
: 0;
823 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
824 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
826 TRACE_FLAG_IRQS_NOSUPPORT
|
828 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
829 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
830 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
833 struct ring_buffer_event
*trace_buffer_lock_reserve(struct trace_array
*tr
,
836 unsigned long flags
, int pc
)
838 struct ring_buffer_event
*event
;
840 event
= ring_buffer_lock_reserve(tr
->buffer
, len
);
842 struct trace_entry
*ent
= ring_buffer_event_data(event
);
844 tracing_generic_entry_update(ent
, flags
, pc
);
850 static void ftrace_trace_stack(struct trace_array
*tr
,
851 unsigned long flags
, int skip
, int pc
);
852 static void ftrace_trace_userstack(struct trace_array
*tr
,
853 unsigned long flags
, int pc
);
855 void trace_buffer_unlock_commit(struct trace_array
*tr
,
856 struct ring_buffer_event
*event
,
857 unsigned long flags
, int pc
)
859 ring_buffer_unlock_commit(tr
->buffer
, event
);
861 ftrace_trace_stack(tr
, flags
, 6, pc
);
862 ftrace_trace_userstack(tr
, flags
, pc
);
866 struct ring_buffer_event
*
867 trace_current_buffer_lock_reserve(unsigned char type
, unsigned long len
,
868 unsigned long flags
, int pc
)
870 return trace_buffer_lock_reserve(&global_trace
,
871 type
, len
, flags
, pc
);
874 void trace_current_buffer_unlock_commit(struct ring_buffer_event
*event
,
875 unsigned long flags
, int pc
)
877 return trace_buffer_unlock_commit(&global_trace
, event
, flags
, pc
);
881 trace_function(struct trace_array
*tr
,
882 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
885 struct ring_buffer_event
*event
;
886 struct ftrace_entry
*entry
;
888 /* If we are reading the ring buffer, don't trace */
889 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
892 event
= trace_buffer_lock_reserve(tr
, TRACE_FN
, sizeof(*entry
),
896 entry
= ring_buffer_event_data(event
);
898 entry
->parent_ip
= parent_ip
;
899 ring_buffer_unlock_commit(tr
->buffer
, event
);
902 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
903 static void __trace_graph_entry(struct trace_array
*tr
,
904 struct ftrace_graph_ent
*trace
,
908 struct ring_buffer_event
*event
;
909 struct ftrace_graph_ent_entry
*entry
;
911 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
914 event
= trace_buffer_lock_reserve(&global_trace
, TRACE_GRAPH_ENT
,
915 sizeof(*entry
), flags
, pc
);
918 entry
= ring_buffer_event_data(event
);
919 entry
->graph_ent
= *trace
;
920 ring_buffer_unlock_commit(global_trace
.buffer
, event
);
923 static void __trace_graph_return(struct trace_array
*tr
,
924 struct ftrace_graph_ret
*trace
,
928 struct ring_buffer_event
*event
;
929 struct ftrace_graph_ret_entry
*entry
;
931 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
934 event
= trace_buffer_lock_reserve(&global_trace
, TRACE_GRAPH_RET
,
935 sizeof(*entry
), flags
, pc
);
938 entry
= ring_buffer_event_data(event
);
940 ring_buffer_unlock_commit(global_trace
.buffer
, event
);
945 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
946 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
949 if (likely(!atomic_read(&data
->disabled
)))
950 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
953 static void __ftrace_trace_stack(struct trace_array
*tr
,
957 #ifdef CONFIG_STACKTRACE
958 struct ring_buffer_event
*event
;
959 struct stack_entry
*entry
;
960 struct stack_trace trace
;
962 event
= trace_buffer_lock_reserve(tr
, TRACE_STACK
,
963 sizeof(*entry
), flags
, pc
);
966 entry
= ring_buffer_event_data(event
);
967 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
969 trace
.nr_entries
= 0;
970 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
972 trace
.entries
= entry
->caller
;
974 save_stack_trace(&trace
);
975 ring_buffer_unlock_commit(tr
->buffer
, event
);
979 static void ftrace_trace_stack(struct trace_array
*tr
,
983 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
986 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
989 void __trace_stack(struct trace_array
*tr
,
993 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
996 static void ftrace_trace_userstack(struct trace_array
*tr
,
997 unsigned long flags
, int pc
)
999 #ifdef CONFIG_STACKTRACE
1000 struct ring_buffer_event
*event
;
1001 struct userstack_entry
*entry
;
1002 struct stack_trace trace
;
1004 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1007 event
= trace_buffer_lock_reserve(tr
, TRACE_USER_STACK
,
1008 sizeof(*entry
), flags
, pc
);
1011 entry
= ring_buffer_event_data(event
);
1013 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1015 trace
.nr_entries
= 0;
1016 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1018 trace
.entries
= entry
->caller
;
1020 save_stack_trace_user(&trace
);
1021 ring_buffer_unlock_commit(tr
->buffer
, event
);
1026 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1028 ftrace_trace_userstack(tr
, flags
, preempt_count());
1033 ftrace_trace_special(void *__tr
,
1034 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1037 struct ring_buffer_event
*event
;
1038 struct trace_array
*tr
= __tr
;
1039 struct special_entry
*entry
;
1041 event
= trace_buffer_lock_reserve(tr
, TRACE_SPECIAL
,
1042 sizeof(*entry
), 0, pc
);
1045 entry
= ring_buffer_event_data(event
);
1049 trace_buffer_unlock_commit(tr
, event
, 0, pc
);
1053 __trace_special(void *__tr
, void *__data
,
1054 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1056 ftrace_trace_special(__tr
, arg1
, arg2
, arg3
, preempt_count());
1060 tracing_sched_switch_trace(struct trace_array
*tr
,
1061 struct task_struct
*prev
,
1062 struct task_struct
*next
,
1063 unsigned long flags
, int pc
)
1065 struct ring_buffer_event
*event
;
1066 struct ctx_switch_entry
*entry
;
1068 event
= trace_buffer_lock_reserve(tr
, TRACE_CTX
,
1069 sizeof(*entry
), flags
, pc
);
1072 entry
= ring_buffer_event_data(event
);
1073 entry
->prev_pid
= prev
->pid
;
1074 entry
->prev_prio
= prev
->prio
;
1075 entry
->prev_state
= prev
->state
;
1076 entry
->next_pid
= next
->pid
;
1077 entry
->next_prio
= next
->prio
;
1078 entry
->next_state
= next
->state
;
1079 entry
->next_cpu
= task_cpu(next
);
1080 trace_buffer_unlock_commit(tr
, event
, flags
, pc
);
1084 tracing_sched_wakeup_trace(struct trace_array
*tr
,
1085 struct task_struct
*wakee
,
1086 struct task_struct
*curr
,
1087 unsigned long flags
, int pc
)
1089 struct ring_buffer_event
*event
;
1090 struct ctx_switch_entry
*entry
;
1092 event
= trace_buffer_lock_reserve(tr
, TRACE_WAKE
,
1093 sizeof(*entry
), flags
, pc
);
1096 entry
= ring_buffer_event_data(event
);
1097 entry
->prev_pid
= curr
->pid
;
1098 entry
->prev_prio
= curr
->prio
;
1099 entry
->prev_state
= curr
->state
;
1100 entry
->next_pid
= wakee
->pid
;
1101 entry
->next_prio
= wakee
->prio
;
1102 entry
->next_state
= wakee
->state
;
1103 entry
->next_cpu
= task_cpu(wakee
);
1105 ring_buffer_unlock_commit(tr
->buffer
, event
);
1106 ftrace_trace_stack(tr
, flags
, 6, pc
);
1107 ftrace_trace_userstack(tr
, flags
, pc
);
1111 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1113 struct trace_array
*tr
= &global_trace
;
1114 struct trace_array_cpu
*data
;
1115 unsigned long flags
;
1119 if (tracing_disabled
)
1122 pc
= preempt_count();
1123 local_irq_save(flags
);
1124 cpu
= raw_smp_processor_id();
1125 data
= tr
->data
[cpu
];
1127 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1128 ftrace_trace_special(tr
, arg1
, arg2
, arg3
, pc
);
1130 atomic_dec(&data
->disabled
);
1131 local_irq_restore(flags
);
1134 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1135 int trace_graph_entry(struct ftrace_graph_ent
*trace
)
1137 struct trace_array
*tr
= &global_trace
;
1138 struct trace_array_cpu
*data
;
1139 unsigned long flags
;
1144 if (!ftrace_trace_task(current
))
1147 if (!ftrace_graph_addr(trace
->func
))
1150 local_irq_save(flags
);
1151 cpu
= raw_smp_processor_id();
1152 data
= tr
->data
[cpu
];
1153 disabled
= atomic_inc_return(&data
->disabled
);
1154 if (likely(disabled
== 1)) {
1155 pc
= preempt_count();
1156 __trace_graph_entry(tr
, trace
, flags
, pc
);
1158 /* Only do the atomic if it is not already set */
1159 if (!test_tsk_trace_graph(current
))
1160 set_tsk_trace_graph(current
);
1161 atomic_dec(&data
->disabled
);
1162 local_irq_restore(flags
);
1167 void trace_graph_return(struct ftrace_graph_ret
*trace
)
1169 struct trace_array
*tr
= &global_trace
;
1170 struct trace_array_cpu
*data
;
1171 unsigned long flags
;
1176 local_irq_save(flags
);
1177 cpu
= raw_smp_processor_id();
1178 data
= tr
->data
[cpu
];
1179 disabled
= atomic_inc_return(&data
->disabled
);
1180 if (likely(disabled
== 1)) {
1181 pc
= preempt_count();
1182 __trace_graph_return(tr
, trace
, flags
, pc
);
1185 clear_tsk_trace_graph(current
);
1186 atomic_dec(&data
->disabled
);
1187 local_irq_restore(flags
);
1189 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1193 * trace_vbprintk - write binary msg to tracing buffer
1196 int trace_vbprintk(unsigned long ip
, int depth
, const char *fmt
, va_list args
)
1198 static raw_spinlock_t trace_buf_lock
=
1199 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
1200 static u32 trace_buf
[TRACE_BUF_SIZE
];
1202 struct ring_buffer_event
*event
;
1203 struct trace_array
*tr
= &global_trace
;
1204 struct trace_array_cpu
*data
;
1205 struct bprint_entry
*entry
;
1206 unsigned long flags
;
1208 int cpu
, len
= 0, size
, pc
;
1210 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1213 /* Don't pollute graph traces with trace_vprintk internals */
1214 pause_graph_tracing();
1216 pc
= preempt_count();
1217 resched
= ftrace_preempt_disable();
1218 cpu
= raw_smp_processor_id();
1219 data
= tr
->data
[cpu
];
1221 if (unlikely(atomic_read(&data
->disabled
)))
1224 /* Lockdep uses trace_printk for lock tracing */
1225 local_irq_save(flags
);
1226 __raw_spin_lock(&trace_buf_lock
);
1227 len
= vbin_printf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1229 if (len
> TRACE_BUF_SIZE
|| len
< 0)
1232 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1233 event
= trace_buffer_lock_reserve(tr
, TRACE_BPRINT
, size
, flags
, pc
);
1236 entry
= ring_buffer_event_data(event
);
1238 entry
->depth
= depth
;
1241 memcpy(entry
->buf
, trace_buf
, sizeof(u32
) * len
);
1242 ring_buffer_unlock_commit(tr
->buffer
, event
);
1245 __raw_spin_unlock(&trace_buf_lock
);
1246 local_irq_restore(flags
);
1249 ftrace_preempt_enable(resched
);
1250 unpause_graph_tracing();
1254 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1256 int trace_vprintk(unsigned long ip
, int depth
, const char *fmt
, va_list args
)
1258 static raw_spinlock_t trace_buf_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
1259 static char trace_buf
[TRACE_BUF_SIZE
];
1261 struct ring_buffer_event
*event
;
1262 struct trace_array
*tr
= &global_trace
;
1263 struct trace_array_cpu
*data
;
1264 int cpu
, len
= 0, size
, pc
;
1265 struct print_entry
*entry
;
1266 unsigned long irq_flags
;
1268 if (tracing_disabled
|| tracing_selftest_running
)
1271 pc
= preempt_count();
1272 preempt_disable_notrace();
1273 cpu
= raw_smp_processor_id();
1274 data
= tr
->data
[cpu
];
1276 if (unlikely(atomic_read(&data
->disabled
)))
1279 pause_graph_tracing();
1280 raw_local_irq_save(irq_flags
);
1281 __raw_spin_lock(&trace_buf_lock
);
1282 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1284 len
= min(len
, TRACE_BUF_SIZE
-1);
1287 size
= sizeof(*entry
) + len
+ 1;
1288 event
= trace_buffer_lock_reserve(tr
, TRACE_PRINT
, size
, irq_flags
, pc
);
1291 entry
= ring_buffer_event_data(event
);
1293 entry
->depth
= depth
;
1295 memcpy(&entry
->buf
, trace_buf
, len
);
1296 entry
->buf
[len
] = 0;
1297 ring_buffer_unlock_commit(tr
->buffer
, event
);
1300 __raw_spin_unlock(&trace_buf_lock
);
1301 raw_local_irq_restore(irq_flags
);
1302 unpause_graph_tracing();
1304 preempt_enable_notrace();
1308 EXPORT_SYMBOL_GPL(trace_vprintk
);
1310 enum trace_file_type
{
1311 TRACE_FILE_LAT_FMT
= 1,
1312 TRACE_FILE_ANNOTATE
= 2,
1315 static void trace_iterator_increment(struct trace_iterator
*iter
)
1317 /* Don't allow ftrace to trace into the ring buffers */
1318 ftrace_disable_cpu();
1321 if (iter
->buffer_iter
[iter
->cpu
])
1322 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1324 ftrace_enable_cpu();
1327 static struct trace_entry
*
1328 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1330 struct ring_buffer_event
*event
;
1331 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1333 /* Don't allow ftrace to trace into the ring buffers */
1334 ftrace_disable_cpu();
1337 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1339 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1341 ftrace_enable_cpu();
1343 return event
? ring_buffer_event_data(event
) : NULL
;
1346 static struct trace_entry
*
1347 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1349 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1350 struct trace_entry
*ent
, *next
= NULL
;
1351 int cpu_file
= iter
->cpu_file
;
1352 u64 next_ts
= 0, ts
;
1357 * If we are in a per_cpu trace file, don't bother by iterating over
1358 * all cpu and peek directly.
1360 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1361 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1363 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
);
1365 *ent_cpu
= cpu_file
;
1370 for_each_tracing_cpu(cpu
) {
1372 if (ring_buffer_empty_cpu(buffer
, cpu
))
1375 ent
= peek_next_entry(iter
, cpu
, &ts
);
1378 * Pick the entry with the smallest timestamp:
1380 if (ent
&& (!next
|| ts
< next_ts
)) {
1388 *ent_cpu
= next_cpu
;
1396 /* Find the next real entry, without updating the iterator itself */
1397 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1398 int *ent_cpu
, u64
*ent_ts
)
1400 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1403 /* Find the next real entry, and increment the iterator to the next entry */
1404 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1406 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1409 trace_iterator_increment(iter
);
1411 return iter
->ent
? iter
: NULL
;
1414 static void trace_consume(struct trace_iterator
*iter
)
1416 /* Don't allow ftrace to trace into the ring buffers */
1417 ftrace_disable_cpu();
1418 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1419 ftrace_enable_cpu();
1422 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1424 struct trace_iterator
*iter
= m
->private;
1430 /* can't go backwards */
1435 ent
= find_next_entry_inc(iter
);
1439 while (ent
&& iter
->idx
< i
)
1440 ent
= find_next_entry_inc(iter
);
1448 * No necessary locking here. The worst thing which can
1449 * happen is loosing events consumed at the same time
1450 * by a trace_pipe reader.
1451 * Other than that, we don't risk to crash the ring buffer
1452 * because it serializes the readers.
1454 * The current tracer is copied to avoid a global locking
1457 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1459 struct trace_iterator
*iter
= m
->private;
1460 static struct tracer
*old_tracer
;
1461 int cpu_file
= iter
->cpu_file
;
1466 /* copy the tracer to avoid using a global lock all around */
1467 mutex_lock(&trace_types_lock
);
1468 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1469 old_tracer
= current_trace
;
1470 *iter
->trace
= *current_trace
;
1472 mutex_unlock(&trace_types_lock
);
1474 atomic_inc(&trace_record_cmdline_disabled
);
1476 if (*pos
!= iter
->pos
) {
1481 ftrace_disable_cpu();
1483 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1484 for_each_tracing_cpu(cpu
)
1485 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1487 ring_buffer_iter_reset(iter
->buffer_iter
[cpu_file
]);
1490 ftrace_enable_cpu();
1492 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1497 p
= s_next(m
, p
, &l
);
1503 static void s_stop(struct seq_file
*m
, void *p
)
1505 atomic_dec(&trace_record_cmdline_disabled
);
1508 static void print_lat_help_header(struct seq_file
*m
)
1510 seq_puts(m
, "# _------=> CPU# \n");
1511 seq_puts(m
, "# / _-----=> irqs-off \n");
1512 seq_puts(m
, "# | / _----=> need-resched \n");
1513 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1514 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1515 seq_puts(m
, "# |||| / \n");
1516 seq_puts(m
, "# ||||| delay \n");
1517 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1518 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1521 static void print_func_help_header(struct seq_file
*m
)
1523 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1524 seq_puts(m
, "# | | | | |\n");
1529 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1531 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1532 struct trace_array
*tr
= iter
->tr
;
1533 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1534 struct tracer
*type
= current_trace
;
1535 unsigned long total
;
1536 unsigned long entries
;
1537 const char *name
= "preemption";
1542 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1544 ring_buffer_overruns(iter
->tr
->buffer
);
1546 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
1548 seq_puts(m
, "# -----------------------------------"
1549 "---------------------------------\n");
1550 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1551 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1552 nsecs_to_usecs(data
->saved_latency
),
1556 #if defined(CONFIG_PREEMPT_NONE)
1558 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1560 #elif defined(CONFIG_PREEMPT)
1565 /* These are reserved for later use */
1568 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1572 seq_puts(m
, "# -----------------\n");
1573 seq_printf(m
, "# | task: %.16s-%d "
1574 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1575 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1576 data
->policy
, data
->rt_priority
);
1577 seq_puts(m
, "# -----------------\n");
1579 if (data
->critical_start
) {
1580 seq_puts(m
, "# => started at: ");
1581 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1582 trace_print_seq(m
, &iter
->seq
);
1583 seq_puts(m
, "\n# => ended at: ");
1584 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1585 trace_print_seq(m
, &iter
->seq
);
1592 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1594 struct trace_seq
*s
= &iter
->seq
;
1596 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1599 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1602 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1605 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1606 trace_seq_printf(s
, "##### CPU %u buffer started ####\n", iter
->cpu
);
1609 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1611 struct trace_seq
*s
= &iter
->seq
;
1612 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1613 struct trace_entry
*entry
;
1614 struct trace_event
*event
;
1618 test_cpu_buff_start(iter
);
1620 event
= ftrace_find_event(entry
->type
);
1622 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1623 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1624 if (!trace_print_lat_context(iter
))
1627 if (!trace_print_context(iter
))
1633 return event
->trace(iter
, sym_flags
);
1635 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1638 return TRACE_TYPE_HANDLED
;
1640 return TRACE_TYPE_PARTIAL_LINE
;
1643 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1645 struct trace_seq
*s
= &iter
->seq
;
1646 struct trace_entry
*entry
;
1647 struct trace_event
*event
;
1651 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1652 if (!trace_seq_printf(s
, "%d %d %llu ",
1653 entry
->pid
, iter
->cpu
, iter
->ts
))
1657 event
= ftrace_find_event(entry
->type
);
1659 return event
->raw(iter
, 0);
1661 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1664 return TRACE_TYPE_HANDLED
;
1666 return TRACE_TYPE_PARTIAL_LINE
;
1669 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1671 struct trace_seq
*s
= &iter
->seq
;
1672 unsigned char newline
= '\n';
1673 struct trace_entry
*entry
;
1674 struct trace_event
*event
;
1678 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1679 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1680 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1681 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1684 event
= ftrace_find_event(entry
->type
);
1686 enum print_line_t ret
= event
->hex(iter
, 0);
1687 if (ret
!= TRACE_TYPE_HANDLED
)
1691 SEQ_PUT_FIELD_RET(s
, newline
);
1693 return TRACE_TYPE_HANDLED
;
1696 static enum print_line_t
print_bprintk_msg_only(struct trace_iterator
*iter
)
1698 struct trace_seq
*s
= &iter
->seq
;
1699 struct trace_entry
*entry
= iter
->ent
;
1700 struct bprint_entry
*field
;
1703 trace_assign_type(field
, entry
);
1705 ret
= trace_seq_bprintf(s
, field
->fmt
, field
->buf
);
1707 return TRACE_TYPE_PARTIAL_LINE
;
1709 return TRACE_TYPE_HANDLED
;
1712 static enum print_line_t
print_printk_msg_only(struct trace_iterator
*iter
)
1714 struct trace_seq
*s
= &iter
->seq
;
1715 struct trace_entry
*entry
= iter
->ent
;
1716 struct print_entry
*field
;
1719 trace_assign_type(field
, entry
);
1721 ret
= trace_seq_printf(s
, "%s", field
->buf
);
1723 return TRACE_TYPE_PARTIAL_LINE
;
1725 return TRACE_TYPE_HANDLED
;
1728 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1730 struct trace_seq
*s
= &iter
->seq
;
1731 struct trace_entry
*entry
;
1732 struct trace_event
*event
;
1736 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1737 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1738 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1739 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1742 event
= ftrace_find_event(entry
->type
);
1743 return event
? event
->binary(iter
, 0) : TRACE_TYPE_HANDLED
;
1746 static int trace_empty(struct trace_iterator
*iter
)
1750 /* If we are looking at one CPU buffer, only check that one */
1751 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
1752 cpu
= iter
->cpu_file
;
1753 if (iter
->buffer_iter
[cpu
]) {
1754 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1757 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1763 for_each_tracing_cpu(cpu
) {
1764 if (iter
->buffer_iter
[cpu
]) {
1765 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1768 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1776 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1778 enum print_line_t ret
;
1780 if (iter
->trace
&& iter
->trace
->print_line
) {
1781 ret
= iter
->trace
->print_line(iter
);
1782 if (ret
!= TRACE_TYPE_UNHANDLED
)
1786 if (iter
->ent
->type
== TRACE_BPRINT
&&
1787 trace_flags
& TRACE_ITER_PRINTK
&&
1788 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1789 return print_bprintk_msg_only(iter
);
1791 if (iter
->ent
->type
== TRACE_PRINT
&&
1792 trace_flags
& TRACE_ITER_PRINTK
&&
1793 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1794 return print_printk_msg_only(iter
);
1796 if (trace_flags
& TRACE_ITER_BIN
)
1797 return print_bin_fmt(iter
);
1799 if (trace_flags
& TRACE_ITER_HEX
)
1800 return print_hex_fmt(iter
);
1802 if (trace_flags
& TRACE_ITER_RAW
)
1803 return print_raw_fmt(iter
);
1805 return print_trace_fmt(iter
);
1808 static int s_show(struct seq_file
*m
, void *v
)
1810 struct trace_iterator
*iter
= v
;
1812 if (iter
->ent
== NULL
) {
1814 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1817 if (iter
->trace
&& iter
->trace
->print_header
)
1818 iter
->trace
->print_header(m
);
1819 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1820 /* print nothing if the buffers are empty */
1821 if (trace_empty(iter
))
1823 print_trace_header(m
, iter
);
1824 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1825 print_lat_help_header(m
);
1827 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1828 print_func_help_header(m
);
1831 print_trace_line(iter
);
1832 trace_print_seq(m
, &iter
->seq
);
1838 static struct seq_operations tracer_seq_ops
= {
1845 static struct trace_iterator
*
1846 __tracing_open(struct inode
*inode
, struct file
*file
)
1848 long cpu_file
= (long) inode
->i_private
;
1849 void *fail_ret
= ERR_PTR(-ENOMEM
);
1850 struct trace_iterator
*iter
;
1854 if (tracing_disabled
)
1855 return ERR_PTR(-ENODEV
);
1857 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1859 return ERR_PTR(-ENOMEM
);
1862 * We make a copy of the current tracer to avoid concurrent
1863 * changes on it while we are reading.
1865 mutex_lock(&trace_types_lock
);
1866 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
1871 *iter
->trace
= *current_trace
;
1873 if (current_trace
&& current_trace
->print_max
)
1876 iter
->tr
= &global_trace
;
1878 mutex_init(&iter
->mutex
);
1879 iter
->cpu_file
= cpu_file
;
1881 /* Notify the tracer early; before we stop tracing. */
1882 if (iter
->trace
&& iter
->trace
->open
)
1883 iter
->trace
->open(iter
);
1885 /* Annotate start of buffers if we had overruns */
1886 if (ring_buffer_overruns(iter
->tr
->buffer
))
1887 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
1889 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
1890 for_each_tracing_cpu(cpu
) {
1892 iter
->buffer_iter
[cpu
] =
1893 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1896 cpu
= iter
->cpu_file
;
1897 iter
->buffer_iter
[cpu
] =
1898 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1901 /* TODO stop tracer */
1902 ret
= seq_open(file
, &tracer_seq_ops
);
1904 fail_ret
= ERR_PTR(ret
);
1908 m
= file
->private_data
;
1911 /* stop the trace while dumping */
1914 mutex_unlock(&trace_types_lock
);
1919 for_each_tracing_cpu(cpu
) {
1920 if (iter
->buffer_iter
[cpu
])
1921 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1924 mutex_unlock(&trace_types_lock
);
1931 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1933 if (tracing_disabled
)
1936 filp
->private_data
= inode
->i_private
;
1940 static int tracing_release(struct inode
*inode
, struct file
*file
)
1942 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1943 struct trace_iterator
*iter
= m
->private;
1946 mutex_lock(&trace_types_lock
);
1947 for_each_tracing_cpu(cpu
) {
1948 if (iter
->buffer_iter
[cpu
])
1949 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1952 if (iter
->trace
&& iter
->trace
->close
)
1953 iter
->trace
->close(iter
);
1955 /* reenable tracing if it was previously enabled */
1957 mutex_unlock(&trace_types_lock
);
1959 seq_release(inode
, file
);
1960 mutex_destroy(&iter
->mutex
);
1966 static int tracing_open(struct inode
*inode
, struct file
*file
)
1968 struct trace_iterator
*iter
;
1971 iter
= __tracing_open(inode
, file
);
1973 ret
= PTR_ERR(iter
);
1974 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
1975 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
1981 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1983 struct tracer
*t
= m
->private;
1995 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1997 struct tracer
*t
= m
->private;
2000 mutex_lock(&trace_types_lock
);
2001 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2007 static void t_stop(struct seq_file
*m
, void *p
)
2009 mutex_unlock(&trace_types_lock
);
2012 static int t_show(struct seq_file
*m
, void *v
)
2014 struct tracer
*t
= v
;
2019 seq_printf(m
, "%s", t
->name
);
2028 static struct seq_operations show_traces_seq_ops
= {
2035 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2039 if (tracing_disabled
)
2042 ret
= seq_open(file
, &show_traces_seq_ops
);
2044 struct seq_file
*m
= file
->private_data
;
2045 m
->private = trace_types
;
2051 static const struct file_operations tracing_fops
= {
2052 .open
= tracing_open
,
2054 .llseek
= seq_lseek
,
2055 .release
= tracing_release
,
2058 static const struct file_operations show_traces_fops
= {
2059 .open
= show_traces_open
,
2061 .release
= seq_release
,
2065 * Only trace on a CPU if the bitmask is set:
2067 static cpumask_var_t tracing_cpumask
;
2070 * The tracer itself will not take this lock, but still we want
2071 * to provide a consistent cpumask to user-space:
2073 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2076 * Temporary storage for the character representation of the
2077 * CPU bitmask (and one more byte for the newline):
2079 static char mask_str
[NR_CPUS
+ 1];
2082 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2083 size_t count
, loff_t
*ppos
)
2087 mutex_lock(&tracing_cpumask_update_lock
);
2089 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2090 if (count
- len
< 2) {
2094 len
+= sprintf(mask_str
+ len
, "\n");
2095 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2098 mutex_unlock(&tracing_cpumask_update_lock
);
2104 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2105 size_t count
, loff_t
*ppos
)
2108 cpumask_var_t tracing_cpumask_new
;
2110 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2113 mutex_lock(&tracing_cpumask_update_lock
);
2114 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2118 local_irq_disable();
2119 __raw_spin_lock(&ftrace_max_lock
);
2120 for_each_tracing_cpu(cpu
) {
2122 * Increase/decrease the disabled counter if we are
2123 * about to flip a bit in the cpumask:
2125 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2126 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2127 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2129 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2130 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2131 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2134 __raw_spin_unlock(&ftrace_max_lock
);
2137 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2139 mutex_unlock(&tracing_cpumask_update_lock
);
2140 free_cpumask_var(tracing_cpumask_new
);
2145 mutex_unlock(&tracing_cpumask_update_lock
);
2146 free_cpumask_var(tracing_cpumask
);
2151 static const struct file_operations tracing_cpumask_fops
= {
2152 .open
= tracing_open_generic
,
2153 .read
= tracing_cpumask_read
,
2154 .write
= tracing_cpumask_write
,
2158 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2159 size_t cnt
, loff_t
*ppos
)
2161 struct tracer_opt
*trace_opts
;
2169 /* calculate max size */
2170 for (i
= 0; trace_options
[i
]; i
++) {
2171 len
+= strlen(trace_options
[i
]);
2172 len
+= 3; /* "no" and newline */
2175 mutex_lock(&trace_types_lock
);
2176 tracer_flags
= current_trace
->flags
->val
;
2177 trace_opts
= current_trace
->flags
->opts
;
2180 * Increase the size with names of options specific
2181 * of the current tracer.
2183 for (i
= 0; trace_opts
[i
].name
; i
++) {
2184 len
+= strlen(trace_opts
[i
].name
);
2185 len
+= 3; /* "no" and newline */
2188 /* +2 for \n and \0 */
2189 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2191 mutex_unlock(&trace_types_lock
);
2195 for (i
= 0; trace_options
[i
]; i
++) {
2196 if (trace_flags
& (1 << i
))
2197 r
+= sprintf(buf
+ r
, "%s\n", trace_options
[i
]);
2199 r
+= sprintf(buf
+ r
, "no%s\n", trace_options
[i
]);
2202 for (i
= 0; trace_opts
[i
].name
; i
++) {
2203 if (tracer_flags
& trace_opts
[i
].bit
)
2204 r
+= sprintf(buf
+ r
, "%s\n",
2205 trace_opts
[i
].name
);
2207 r
+= sprintf(buf
+ r
, "no%s\n",
2208 trace_opts
[i
].name
);
2210 mutex_unlock(&trace_types_lock
);
2212 WARN_ON(r
>= len
+ 2);
2214 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2220 /* Try to assign a tracer specific option */
2221 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2223 struct tracer_flags
*trace_flags
= trace
->flags
;
2224 struct tracer_opt
*opts
= NULL
;
2228 for (i
= 0; trace_flags
->opts
[i
].name
; i
++) {
2229 opts
= &trace_flags
->opts
[i
];
2230 len
= strlen(opts
->name
);
2232 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2233 ret
= trace
->set_flag(trace_flags
->val
,
2239 if (!trace_flags
->opts
[i
].name
)
2242 /* Refused to handle */
2247 trace_flags
->val
&= ~opts
->bit
;
2249 trace_flags
->val
|= opts
->bit
;
2255 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2256 size_t cnt
, loff_t
*ppos
)
2264 if (cnt
>= sizeof(buf
))
2267 if (copy_from_user(&buf
, ubuf
, cnt
))
2272 if (strncmp(buf
, "no", 2) == 0) {
2277 for (i
= 0; trace_options
[i
]; i
++) {
2278 int len
= strlen(trace_options
[i
]);
2280 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2282 trace_flags
&= ~(1 << i
);
2284 trace_flags
|= (1 << i
);
2289 /* If no option could be set, test the specific tracer options */
2290 if (!trace_options
[i
]) {
2291 mutex_lock(&trace_types_lock
);
2292 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2293 mutex_unlock(&trace_types_lock
);
2303 static const struct file_operations tracing_iter_fops
= {
2304 .open
= tracing_open_generic
,
2305 .read
= tracing_trace_options_read
,
2306 .write
= tracing_trace_options_write
,
2309 static const char readme_msg
[] =
2310 "tracing mini-HOWTO:\n\n"
2312 "# mount -t debugfs nodev /debug\n\n"
2313 "# cat /debug/tracing/available_tracers\n"
2314 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2315 "# cat /debug/tracing/current_tracer\n"
2317 "# echo sched_switch > /debug/tracing/current_tracer\n"
2318 "# cat /debug/tracing/current_tracer\n"
2320 "# cat /debug/tracing/trace_options\n"
2321 "noprint-parent nosym-offset nosym-addr noverbose\n"
2322 "# echo print-parent > /debug/tracing/trace_options\n"
2323 "# echo 1 > /debug/tracing/tracing_enabled\n"
2324 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2325 "echo 0 > /debug/tracing/tracing_enabled\n"
2329 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2330 size_t cnt
, loff_t
*ppos
)
2332 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2333 readme_msg
, strlen(readme_msg
));
2336 static const struct file_operations tracing_readme_fops
= {
2337 .open
= tracing_open_generic
,
2338 .read
= tracing_readme_read
,
2342 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2343 size_t cnt
, loff_t
*ppos
)
2348 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2349 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2353 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2354 size_t cnt
, loff_t
*ppos
)
2356 struct trace_array
*tr
= filp
->private_data
;
2361 if (cnt
>= sizeof(buf
))
2364 if (copy_from_user(&buf
, ubuf
, cnt
))
2369 ret
= strict_strtoul(buf
, 10, &val
);
2375 mutex_lock(&trace_types_lock
);
2376 if (tracer_enabled
^ val
) {
2379 if (current_trace
->start
)
2380 current_trace
->start(tr
);
2385 if (current_trace
->stop
)
2386 current_trace
->stop(tr
);
2389 mutex_unlock(&trace_types_lock
);
2397 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2398 size_t cnt
, loff_t
*ppos
)
2400 char buf
[max_tracer_type_len
+2];
2403 mutex_lock(&trace_types_lock
);
2405 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2407 r
= sprintf(buf
, "\n");
2408 mutex_unlock(&trace_types_lock
);
2410 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2413 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2415 tracing_reset_online_cpus(tr
);
2419 static int tracing_resize_ring_buffer(unsigned long size
)
2424 * If kernel or user changes the size of the ring buffer
2425 * we use the size that was given, and we can forget about
2426 * expanding it later.
2428 ring_buffer_expanded
= 1;
2430 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2434 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2438 r
= ring_buffer_resize(global_trace
.buffer
,
2439 global_trace
.entries
);
2442 * AARGH! We are left with different
2443 * size max buffer!!!!
2444 * The max buffer is our "snapshot" buffer.
2445 * When a tracer needs a snapshot (one of the
2446 * latency tracers), it swaps the max buffer
2447 * with the saved snap shot. We succeeded to
2448 * update the size of the main buffer, but failed to
2449 * update the size of the max buffer. But when we tried
2450 * to reset the main buffer to the original size, we
2451 * failed there too. This is very unlikely to
2452 * happen, but if it does, warn and kill all
2456 tracing_disabled
= 1;
2461 global_trace
.entries
= size
;
2467 * tracing_update_buffers - used by tracing facility to expand ring buffers
2469 * To save on memory when the tracing is never used on a system with it
2470 * configured in. The ring buffers are set to a minimum size. But once
2471 * a user starts to use the tracing facility, then they need to grow
2472 * to their default size.
2474 * This function is to be called when a tracer is about to be used.
2476 int tracing_update_buffers(void)
2480 mutex_lock(&trace_types_lock
);
2481 if (!ring_buffer_expanded
)
2482 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2483 mutex_unlock(&trace_types_lock
);
2488 struct trace_option_dentry
;
2490 static struct trace_option_dentry
*
2491 create_trace_option_files(struct tracer
*tracer
);
2494 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2496 static int tracing_set_tracer(const char *buf
)
2498 static struct trace_option_dentry
*topts
;
2499 struct trace_array
*tr
= &global_trace
;
2503 mutex_lock(&trace_types_lock
);
2505 if (!ring_buffer_expanded
) {
2506 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2512 for (t
= trace_types
; t
; t
= t
->next
) {
2513 if (strcmp(t
->name
, buf
) == 0)
2520 if (t
== current_trace
)
2523 trace_branch_disable();
2524 if (current_trace
&& current_trace
->reset
)
2525 current_trace
->reset(tr
);
2527 destroy_trace_option_files(topts
);
2531 topts
= create_trace_option_files(current_trace
);
2534 ret
= tracer_init(t
, tr
);
2539 trace_branch_enable(tr
);
2541 mutex_unlock(&trace_types_lock
);
2547 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2548 size_t cnt
, loff_t
*ppos
)
2550 char buf
[max_tracer_type_len
+1];
2557 if (cnt
> max_tracer_type_len
)
2558 cnt
= max_tracer_type_len
;
2560 if (copy_from_user(&buf
, ubuf
, cnt
))
2565 /* strip ending whitespace. */
2566 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2569 err
= tracing_set_tracer(buf
);
2579 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2580 size_t cnt
, loff_t
*ppos
)
2582 unsigned long *ptr
= filp
->private_data
;
2586 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2587 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2588 if (r
> sizeof(buf
))
2590 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2594 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2595 size_t cnt
, loff_t
*ppos
)
2597 unsigned long *ptr
= filp
->private_data
;
2602 if (cnt
>= sizeof(buf
))
2605 if (copy_from_user(&buf
, ubuf
, cnt
))
2610 ret
= strict_strtoul(buf
, 10, &val
);
2619 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2621 long cpu_file
= (long) inode
->i_private
;
2622 struct trace_iterator
*iter
;
2625 if (tracing_disabled
)
2628 mutex_lock(&trace_types_lock
);
2630 /* We only allow one reader per cpu */
2631 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
2632 if (!cpumask_empty(tracing_reader_cpumask
)) {
2636 cpumask_setall(tracing_reader_cpumask
);
2638 if (!cpumask_test_cpu(cpu_file
, tracing_reader_cpumask
))
2639 cpumask_set_cpu(cpu_file
, tracing_reader_cpumask
);
2646 /* create a buffer to store the information to pass to userspace */
2647 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2654 * We make a copy of the current tracer to avoid concurrent
2655 * changes on it while we are reading.
2657 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2663 *iter
->trace
= *current_trace
;
2665 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2670 /* trace pipe does not show start of buffer */
2671 cpumask_setall(iter
->started
);
2673 iter
->cpu_file
= cpu_file
;
2674 iter
->tr
= &global_trace
;
2675 mutex_init(&iter
->mutex
);
2676 filp
->private_data
= iter
;
2678 if (iter
->trace
->pipe_open
)
2679 iter
->trace
->pipe_open(iter
);
2682 mutex_unlock(&trace_types_lock
);
2688 mutex_unlock(&trace_types_lock
);
2692 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2694 struct trace_iterator
*iter
= file
->private_data
;
2696 mutex_lock(&trace_types_lock
);
2698 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
)
2699 cpumask_clear(tracing_reader_cpumask
);
2701 cpumask_clear_cpu(iter
->cpu_file
, tracing_reader_cpumask
);
2703 mutex_unlock(&trace_types_lock
);
2705 free_cpumask_var(iter
->started
);
2706 mutex_destroy(&iter
->mutex
);
2714 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2716 struct trace_iterator
*iter
= filp
->private_data
;
2718 if (trace_flags
& TRACE_ITER_BLOCK
) {
2720 * Always select as readable when in blocking mode
2722 return POLLIN
| POLLRDNORM
;
2724 if (!trace_empty(iter
))
2725 return POLLIN
| POLLRDNORM
;
2726 poll_wait(filp
, &trace_wait
, poll_table
);
2727 if (!trace_empty(iter
))
2728 return POLLIN
| POLLRDNORM
;
2735 void default_wait_pipe(struct trace_iterator
*iter
)
2739 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
2741 if (trace_empty(iter
))
2744 finish_wait(&trace_wait
, &wait
);
2748 * This is a make-shift waitqueue.
2749 * A tracer might use this callback on some rare cases:
2751 * 1) the current tracer might hold the runqueue lock when it wakes up
2752 * a reader, hence a deadlock (sched, function, and function graph tracers)
2753 * 2) the function tracers, trace all functions, we don't want
2754 * the overhead of calling wake_up and friends
2755 * (and tracing them too)
2757 * Anyway, this is really very primitive wakeup.
2759 void poll_wait_pipe(struct trace_iterator
*iter
)
2761 set_current_state(TASK_INTERRUPTIBLE
);
2762 /* sleep for 100 msecs, and try again. */
2763 schedule_timeout(HZ
/ 10);
2766 /* Must be called with trace_types_lock mutex held. */
2767 static int tracing_wait_pipe(struct file
*filp
)
2769 struct trace_iterator
*iter
= filp
->private_data
;
2771 while (trace_empty(iter
)) {
2773 if ((filp
->f_flags
& O_NONBLOCK
)) {
2777 mutex_unlock(&iter
->mutex
);
2779 iter
->trace
->wait_pipe(iter
);
2781 mutex_lock(&iter
->mutex
);
2783 if (signal_pending(current
))
2787 * We block until we read something and tracing is disabled.
2788 * We still block if tracing is disabled, but we have never
2789 * read anything. This allows a user to cat this file, and
2790 * then enable tracing. But after we have read something,
2791 * we give an EOF when tracing is again disabled.
2793 * iter->pos will be 0 if we haven't read anything.
2795 if (!tracer_enabled
&& iter
->pos
)
2806 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2807 size_t cnt
, loff_t
*ppos
)
2809 struct trace_iterator
*iter
= filp
->private_data
;
2810 static struct tracer
*old_tracer
;
2813 /* return any leftover data */
2814 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2818 trace_seq_init(&iter
->seq
);
2820 /* copy the tracer to avoid using a global lock all around */
2821 mutex_lock(&trace_types_lock
);
2822 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
2823 old_tracer
= current_trace
;
2824 *iter
->trace
= *current_trace
;
2826 mutex_unlock(&trace_types_lock
);
2829 * Avoid more than one consumer on a single file descriptor
2830 * This is just a matter of traces coherency, the ring buffer itself
2833 mutex_lock(&iter
->mutex
);
2834 if (iter
->trace
->read
) {
2835 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2841 sret
= tracing_wait_pipe(filp
);
2845 /* stop when tracing is finished */
2846 if (trace_empty(iter
)) {
2851 if (cnt
>= PAGE_SIZE
)
2852 cnt
= PAGE_SIZE
- 1;
2854 /* reset all but tr, trace, and overruns */
2855 memset(&iter
->seq
, 0,
2856 sizeof(struct trace_iterator
) -
2857 offsetof(struct trace_iterator
, seq
));
2860 while (find_next_entry_inc(iter
) != NULL
) {
2861 enum print_line_t ret
;
2862 int len
= iter
->seq
.len
;
2864 ret
= print_trace_line(iter
);
2865 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2866 /* don't print partial lines */
2867 iter
->seq
.len
= len
;
2870 if (ret
!= TRACE_TYPE_NO_CONSUME
)
2871 trace_consume(iter
);
2873 if (iter
->seq
.len
>= cnt
)
2877 /* Now copy what we have to the user */
2878 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2879 if (iter
->seq
.readpos
>= iter
->seq
.len
)
2880 trace_seq_init(&iter
->seq
);
2883 * If there was nothing to send to user, inspite of consuming trace
2884 * entries, go back to wait for more entries.
2890 mutex_unlock(&iter
->mutex
);
2895 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
2896 struct pipe_buffer
*buf
)
2898 __free_page(buf
->page
);
2901 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
2904 __free_page(spd
->pages
[idx
]);
2907 static struct pipe_buf_operations tracing_pipe_buf_ops
= {
2909 .map
= generic_pipe_buf_map
,
2910 .unmap
= generic_pipe_buf_unmap
,
2911 .confirm
= generic_pipe_buf_confirm
,
2912 .release
= tracing_pipe_buf_release
,
2913 .steal
= generic_pipe_buf_steal
,
2914 .get
= generic_pipe_buf_get
,
2918 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
2923 /* Seq buffer is page-sized, exactly what we need. */
2925 count
= iter
->seq
.len
;
2926 ret
= print_trace_line(iter
);
2927 count
= iter
->seq
.len
- count
;
2930 iter
->seq
.len
-= count
;
2933 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2934 iter
->seq
.len
-= count
;
2938 trace_consume(iter
);
2940 if (!find_next_entry_inc(iter
)) {
2950 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
2952 struct pipe_inode_info
*pipe
,
2956 struct page
*pages
[PIPE_BUFFERS
];
2957 struct partial_page partial
[PIPE_BUFFERS
];
2958 struct trace_iterator
*iter
= filp
->private_data
;
2959 struct splice_pipe_desc spd
= {
2962 .nr_pages
= 0, /* This gets updated below. */
2964 .ops
= &tracing_pipe_buf_ops
,
2965 .spd_release
= tracing_spd_release_pipe
,
2967 static struct tracer
*old_tracer
;
2972 /* copy the tracer to avoid using a global lock all around */
2973 mutex_lock(&trace_types_lock
);
2974 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
2975 old_tracer
= current_trace
;
2976 *iter
->trace
= *current_trace
;
2978 mutex_unlock(&trace_types_lock
);
2980 mutex_lock(&iter
->mutex
);
2982 if (iter
->trace
->splice_read
) {
2983 ret
= iter
->trace
->splice_read(iter
, filp
,
2984 ppos
, pipe
, len
, flags
);
2989 ret
= tracing_wait_pipe(filp
);
2993 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
2998 /* Fill as many pages as possible. */
2999 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
3000 pages
[i
] = alloc_page(GFP_KERNEL
);
3004 rem
= tracing_fill_pipe_page(rem
, iter
);
3006 /* Copy the data into the page, so we can start over. */
3007 ret
= trace_seq_to_buffer(&iter
->seq
,
3008 page_address(pages
[i
]),
3011 __free_page(pages
[i
]);
3014 partial
[i
].offset
= 0;
3015 partial
[i
].len
= iter
->seq
.len
;
3017 trace_seq_init(&iter
->seq
);
3020 mutex_unlock(&iter
->mutex
);
3024 return splice_to_pipe(pipe
, &spd
);
3027 mutex_unlock(&iter
->mutex
);
3033 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3034 size_t cnt
, loff_t
*ppos
)
3036 struct trace_array
*tr
= filp
->private_data
;
3040 mutex_lock(&trace_types_lock
);
3041 if (!ring_buffer_expanded
)
3042 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3044 trace_buf_size
>> 10);
3046 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3047 mutex_unlock(&trace_types_lock
);
3049 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3053 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3054 size_t cnt
, loff_t
*ppos
)
3060 if (cnt
>= sizeof(buf
))
3063 if (copy_from_user(&buf
, ubuf
, cnt
))
3068 ret
= strict_strtoul(buf
, 10, &val
);
3072 /* must have at least 1 entry */
3076 mutex_lock(&trace_types_lock
);
3080 /* disable all cpu buffers */
3081 for_each_tracing_cpu(cpu
) {
3082 if (global_trace
.data
[cpu
])
3083 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3084 if (max_tr
.data
[cpu
])
3085 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3088 /* value is in KB */
3091 if (val
!= global_trace
.entries
) {
3092 ret
= tracing_resize_ring_buffer(val
);
3101 /* If check pages failed, return ENOMEM */
3102 if (tracing_disabled
)
3105 for_each_tracing_cpu(cpu
) {
3106 if (global_trace
.data
[cpu
])
3107 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3108 if (max_tr
.data
[cpu
])
3109 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3113 max_tr
.entries
= global_trace
.entries
;
3114 mutex_unlock(&trace_types_lock
);
3119 static int mark_printk(const char *fmt
, ...)
3123 va_start(args
, fmt
);
3124 ret
= trace_vprintk(0, -1, fmt
, args
);
3130 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3131 size_t cnt
, loff_t
*fpos
)
3136 if (tracing_disabled
)
3139 if (cnt
> TRACE_BUF_SIZE
)
3140 cnt
= TRACE_BUF_SIZE
;
3142 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3146 if (copy_from_user(buf
, ubuf
, cnt
)) {
3151 /* Cut from the first nil or newline. */
3153 end
= strchr(buf
, '\n');
3157 cnt
= mark_printk("%s\n", buf
);
3164 static const struct file_operations tracing_max_lat_fops
= {
3165 .open
= tracing_open_generic
,
3166 .read
= tracing_max_lat_read
,
3167 .write
= tracing_max_lat_write
,
3170 static const struct file_operations tracing_ctrl_fops
= {
3171 .open
= tracing_open_generic
,
3172 .read
= tracing_ctrl_read
,
3173 .write
= tracing_ctrl_write
,
3176 static const struct file_operations set_tracer_fops
= {
3177 .open
= tracing_open_generic
,
3178 .read
= tracing_set_trace_read
,
3179 .write
= tracing_set_trace_write
,
3182 static const struct file_operations tracing_pipe_fops
= {
3183 .open
= tracing_open_pipe
,
3184 .poll
= tracing_poll_pipe
,
3185 .read
= tracing_read_pipe
,
3186 .splice_read
= tracing_splice_read_pipe
,
3187 .release
= tracing_release_pipe
,
3190 static const struct file_operations tracing_entries_fops
= {
3191 .open
= tracing_open_generic
,
3192 .read
= tracing_entries_read
,
3193 .write
= tracing_entries_write
,
3196 static const struct file_operations tracing_mark_fops
= {
3197 .open
= tracing_open_generic
,
3198 .write
= tracing_mark_write
,
3201 struct ftrace_buffer_info
{
3202 struct trace_array
*tr
;
3208 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3210 int cpu
= (int)(long)inode
->i_private
;
3211 struct ftrace_buffer_info
*info
;
3213 if (tracing_disabled
)
3216 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3220 info
->tr
= &global_trace
;
3222 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3223 /* Force reading ring buffer for first read */
3224 info
->read
= (unsigned int)-1;
3228 filp
->private_data
= info
;
3238 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3239 size_t count
, loff_t
*ppos
)
3241 struct ftrace_buffer_info
*info
= filp
->private_data
;
3249 /* Do we have previous read data to read? */
3250 if (info
->read
< PAGE_SIZE
)
3255 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3262 pos
= ring_buffer_page_len(info
->spare
);
3264 if (pos
< PAGE_SIZE
)
3265 memset(info
->spare
+ pos
, 0, PAGE_SIZE
- pos
);
3268 size
= PAGE_SIZE
- info
->read
;
3272 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3283 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3285 struct ftrace_buffer_info
*info
= file
->private_data
;
3287 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3294 struct ring_buffer
*buffer
;
3299 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3300 struct pipe_buffer
*buf
)
3302 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3307 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3312 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3313 struct pipe_buffer
*buf
)
3318 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3319 struct pipe_buffer
*buf
)
3321 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3326 /* Pipe buffer operations for a buffer. */
3327 static struct pipe_buf_operations buffer_pipe_buf_ops
= {
3329 .map
= generic_pipe_buf_map
,
3330 .unmap
= generic_pipe_buf_unmap
,
3331 .confirm
= generic_pipe_buf_confirm
,
3332 .release
= buffer_pipe_buf_release
,
3333 .steal
= buffer_pipe_buf_steal
,
3334 .get
= buffer_pipe_buf_get
,
3338 * Callback from splice_to_pipe(), if we need to release some pages
3339 * at the end of the spd in case we error'ed out in filling the pipe.
3341 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3343 struct buffer_ref
*ref
=
3344 (struct buffer_ref
*)spd
->partial
[i
].private;
3349 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3351 spd
->partial
[i
].private = 0;
3355 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3356 struct pipe_inode_info
*pipe
, size_t len
,
3359 struct ftrace_buffer_info
*info
= file
->private_data
;
3360 struct partial_page partial
[PIPE_BUFFERS
];
3361 struct page
*pages
[PIPE_BUFFERS
];
3362 struct splice_pipe_desc spd
= {
3366 .ops
= &buffer_pipe_buf_ops
,
3367 .spd_release
= buffer_spd_release
,
3369 struct buffer_ref
*ref
;
3374 * We can't seek on a buffer input
3376 if (unlikely(*ppos
))
3380 for (i
= 0; i
< PIPE_BUFFERS
&& len
; i
++, len
-= size
) {
3384 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3388 ref
->buffer
= info
->tr
->buffer
;
3389 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3395 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3398 ring_buffer_free_read_page(ref
->buffer
,
3405 * zero out any left over data, this is going to
3408 size
= ring_buffer_page_len(ref
->page
);
3409 if (size
< PAGE_SIZE
)
3410 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3412 page
= virt_to_page(ref
->page
);
3414 spd
.pages
[i
] = page
;
3415 spd
.partial
[i
].len
= PAGE_SIZE
;
3416 spd
.partial
[i
].offset
= 0;
3417 spd
.partial
[i
].private = (unsigned long)ref
;
3423 /* did we read anything? */
3424 if (!spd
.nr_pages
) {
3425 if (flags
& SPLICE_F_NONBLOCK
)
3433 ret
= splice_to_pipe(pipe
, &spd
);
3438 static const struct file_operations tracing_buffers_fops
= {
3439 .open
= tracing_buffers_open
,
3440 .read
= tracing_buffers_read
,
3441 .release
= tracing_buffers_release
,
3442 .splice_read
= tracing_buffers_splice_read
,
3443 .llseek
= no_llseek
,
3446 #ifdef CONFIG_DYNAMIC_FTRACE
3448 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3454 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3455 size_t cnt
, loff_t
*ppos
)
3457 static char ftrace_dyn_info_buffer
[1024];
3458 static DEFINE_MUTEX(dyn_info_mutex
);
3459 unsigned long *p
= filp
->private_data
;
3460 char *buf
= ftrace_dyn_info_buffer
;
3461 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3464 mutex_lock(&dyn_info_mutex
);
3465 r
= sprintf(buf
, "%ld ", *p
);
3467 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3470 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3472 mutex_unlock(&dyn_info_mutex
);
3477 static const struct file_operations tracing_dyn_info_fops
= {
3478 .open
= tracing_open_generic
,
3479 .read
= tracing_read_dyn_info
,
3483 static struct dentry
*d_tracer
;
3485 struct dentry
*tracing_init_dentry(void)
3492 d_tracer
= debugfs_create_dir("tracing", NULL
);
3494 if (!d_tracer
&& !once
) {
3496 pr_warning("Could not create debugfs directory 'tracing'\n");
3503 static struct dentry
*d_percpu
;
3505 struct dentry
*tracing_dentry_percpu(void)
3508 struct dentry
*d_tracer
;
3513 d_tracer
= tracing_init_dentry();
3518 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
3520 if (!d_percpu
&& !once
) {
3522 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3529 static void tracing_init_debugfs_percpu(long cpu
)
3531 struct dentry
*d_percpu
= tracing_dentry_percpu();
3532 struct dentry
*entry
, *d_cpu
;
3533 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3536 if (cpu
> 999 || cpu
< 0)
3539 sprintf(cpu_dir
, "cpu%ld", cpu
);
3540 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
3542 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
3546 /* per cpu trace_pipe */
3547 entry
= debugfs_create_file("trace_pipe", 0444, d_cpu
,
3548 (void *) cpu
, &tracing_pipe_fops
);
3550 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3553 entry
= debugfs_create_file("trace", 0444, d_cpu
,
3554 (void *) cpu
, &tracing_fops
);
3556 pr_warning("Could not create debugfs 'trace' entry\n");
3558 entry
= debugfs_create_file("trace_pipe_raw", 0444, d_cpu
,
3559 (void *) cpu
, &tracing_buffers_fops
);
3561 pr_warning("Could not create debugfs 'trace_pipe_raw' entry\n");
3564 #ifdef CONFIG_FTRACE_SELFTEST
3565 /* Let selftest have access to static functions in this file */
3566 #include "trace_selftest.c"
3569 struct trace_option_dentry
{
3570 struct tracer_opt
*opt
;
3571 struct tracer_flags
*flags
;
3572 struct dentry
*entry
;
3576 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3579 struct trace_option_dentry
*topt
= filp
->private_data
;
3582 if (topt
->flags
->val
& topt
->opt
->bit
)
3587 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3591 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3594 struct trace_option_dentry
*topt
= filp
->private_data
;
3599 if (cnt
>= sizeof(buf
))
3602 if (copy_from_user(&buf
, ubuf
, cnt
))
3607 ret
= strict_strtoul(buf
, 10, &val
);
3614 /* do nothing if already cleared */
3615 if (!(topt
->flags
->val
& topt
->opt
->bit
))
3618 mutex_lock(&trace_types_lock
);
3619 if (current_trace
->set_flag
)
3620 ret
= current_trace
->set_flag(topt
->flags
->val
,
3622 mutex_unlock(&trace_types_lock
);
3625 topt
->flags
->val
&= ~topt
->opt
->bit
;
3628 /* do nothing if already set */
3629 if (topt
->flags
->val
& topt
->opt
->bit
)
3632 mutex_lock(&trace_types_lock
);
3633 if (current_trace
->set_flag
)
3634 ret
= current_trace
->set_flag(topt
->flags
->val
,
3636 mutex_unlock(&trace_types_lock
);
3639 topt
->flags
->val
|= topt
->opt
->bit
;
3652 static const struct file_operations trace_options_fops
= {
3653 .open
= tracing_open_generic
,
3654 .read
= trace_options_read
,
3655 .write
= trace_options_write
,
3659 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3662 long index
= (long)filp
->private_data
;
3665 if (trace_flags
& (1 << index
))
3670 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3674 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3677 long index
= (long)filp
->private_data
;
3682 if (cnt
>= sizeof(buf
))
3685 if (copy_from_user(&buf
, ubuf
, cnt
))
3690 ret
= strict_strtoul(buf
, 10, &val
);
3696 trace_flags
&= ~(1 << index
);
3699 trace_flags
|= 1 << index
;
3711 static const struct file_operations trace_options_core_fops
= {
3712 .open
= tracing_open_generic
,
3713 .read
= trace_options_core_read
,
3714 .write
= trace_options_core_write
,
3717 static struct dentry
*trace_options_init_dentry(void)
3719 struct dentry
*d_tracer
;
3720 static struct dentry
*t_options
;
3725 d_tracer
= tracing_init_dentry();
3729 t_options
= debugfs_create_dir("options", d_tracer
);
3731 pr_warning("Could not create debugfs directory 'options'\n");
3739 create_trace_option_file(struct trace_option_dentry
*topt
,
3740 struct tracer_flags
*flags
,
3741 struct tracer_opt
*opt
)
3743 struct dentry
*t_options
;
3744 struct dentry
*entry
;
3746 t_options
= trace_options_init_dentry();
3750 topt
->flags
= flags
;
3753 entry
= debugfs_create_file(opt
->name
, 0644, t_options
, topt
,
3754 &trace_options_fops
);
3756 topt
->entry
= entry
;
3760 static struct trace_option_dentry
*
3761 create_trace_option_files(struct tracer
*tracer
)
3763 struct trace_option_dentry
*topts
;
3764 struct tracer_flags
*flags
;
3765 struct tracer_opt
*opts
;
3771 flags
= tracer
->flags
;
3773 if (!flags
|| !flags
->opts
)
3778 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3781 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
3785 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3786 create_trace_option_file(&topts
[cnt
], flags
,
3793 destroy_trace_option_files(struct trace_option_dentry
*topts
)
3800 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
3801 if (topts
[cnt
].entry
)
3802 debugfs_remove(topts
[cnt
].entry
);
3808 static struct dentry
*
3809 create_trace_option_core_file(const char *option
, long index
)
3811 struct dentry
*t_options
;
3812 struct dentry
*entry
;
3814 t_options
= trace_options_init_dentry();
3818 entry
= debugfs_create_file(option
, 0644, t_options
, (void *)index
,
3819 &trace_options_core_fops
);
3824 static __init
void create_trace_options_dir(void)
3826 struct dentry
*t_options
;
3827 struct dentry
*entry
;
3830 t_options
= trace_options_init_dentry();
3834 for (i
= 0; trace_options
[i
]; i
++) {
3835 entry
= create_trace_option_core_file(trace_options
[i
], i
);
3837 pr_warning("Could not create debugfs %s entry\n",
3842 static __init
int tracer_init_debugfs(void)
3844 struct dentry
*d_tracer
;
3845 struct dentry
*entry
;
3848 d_tracer
= tracing_init_dentry();
3850 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
3851 &global_trace
, &tracing_ctrl_fops
);
3853 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3855 entry
= debugfs_create_file("trace_options", 0644, d_tracer
,
3856 NULL
, &tracing_iter_fops
);
3858 pr_warning("Could not create debugfs 'trace_options' entry\n");
3860 create_trace_options_dir();
3862 entry
= debugfs_create_file("tracing_cpumask", 0644, d_tracer
,
3863 NULL
, &tracing_cpumask_fops
);
3865 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3867 entry
= debugfs_create_file("trace", 0444, d_tracer
,
3868 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
3870 pr_warning("Could not create debugfs 'trace' entry\n");
3872 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
3873 &global_trace
, &show_traces_fops
);
3875 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3877 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
3878 &global_trace
, &set_tracer_fops
);
3880 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3882 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
3883 &tracing_max_latency
,
3884 &tracing_max_lat_fops
);
3886 pr_warning("Could not create debugfs "
3887 "'tracing_max_latency' entry\n");
3889 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
3890 &tracing_thresh
, &tracing_max_lat_fops
);
3892 pr_warning("Could not create debugfs "
3893 "'tracing_thresh' entry\n");
3894 entry
= debugfs_create_file("README", 0644, d_tracer
,
3895 NULL
, &tracing_readme_fops
);
3897 pr_warning("Could not create debugfs 'README' entry\n");
3899 entry
= debugfs_create_file("trace_pipe", 0444, d_tracer
,
3900 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
3902 pr_warning("Could not create debugfs "
3903 "'trace_pipe' entry\n");
3905 entry
= debugfs_create_file("buffer_size_kb", 0644, d_tracer
,
3906 &global_trace
, &tracing_entries_fops
);
3908 pr_warning("Could not create debugfs "
3909 "'buffer_size_kb' entry\n");
3911 entry
= debugfs_create_file("trace_marker", 0220, d_tracer
,
3912 NULL
, &tracing_mark_fops
);
3914 pr_warning("Could not create debugfs "
3915 "'trace_marker' entry\n");
3917 #ifdef CONFIG_DYNAMIC_FTRACE
3918 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
3919 &ftrace_update_tot_cnt
,
3920 &tracing_dyn_info_fops
);
3922 pr_warning("Could not create debugfs "
3923 "'dyn_ftrace_total_info' entry\n");
3925 #ifdef CONFIG_SYSPROF_TRACER
3926 init_tracer_sysprof_debugfs(d_tracer
);
3929 for_each_tracing_cpu(cpu
)
3930 tracing_init_debugfs_percpu(cpu
);
3935 static int trace_panic_handler(struct notifier_block
*this,
3936 unsigned long event
, void *unused
)
3938 if (ftrace_dump_on_oops
)
3943 static struct notifier_block trace_panic_notifier
= {
3944 .notifier_call
= trace_panic_handler
,
3946 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
3949 static int trace_die_handler(struct notifier_block
*self
,
3955 if (ftrace_dump_on_oops
)
3964 static struct notifier_block trace_die_notifier
= {
3965 .notifier_call
= trace_die_handler
,
3970 * printk is set to max of 1024, we really don't need it that big.
3971 * Nothing should be printing 1000 characters anyway.
3973 #define TRACE_MAX_PRINT 1000
3976 * Define here KERN_TRACE so that we have one place to modify
3977 * it if we decide to change what log level the ftrace dump
3980 #define KERN_TRACE KERN_EMERG
3983 trace_printk_seq(struct trace_seq
*s
)
3985 /* Probably should print a warning here. */
3989 /* should be zero ended, but we are paranoid. */
3990 s
->buffer
[s
->len
] = 0;
3992 printk(KERN_TRACE
"%s", s
->buffer
);
3997 void ftrace_dump(void)
3999 static DEFINE_SPINLOCK(ftrace_dump_lock
);
4000 /* use static because iter can be a bit big for the stack */
4001 static struct trace_iterator iter
;
4002 static int dump_ran
;
4003 unsigned long flags
;
4007 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
4013 /* No turning back! */
4017 for_each_tracing_cpu(cpu
) {
4018 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
4021 /* don't look at user memory in panic mode */
4022 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4024 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4026 /* Simulate the iterator */
4027 iter
.tr
= &global_trace
;
4028 iter
.trace
= current_trace
;
4029 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4032 * We need to stop all tracing on all CPUS to read the
4033 * the next buffer. This is a bit expensive, but is
4034 * not done often. We fill all what we can read,
4035 * and then release the locks again.
4038 while (!trace_empty(&iter
)) {
4041 printk(KERN_TRACE
"---------------------------------\n");
4045 /* reset all but tr, trace, and overruns */
4046 memset(&iter
.seq
, 0,
4047 sizeof(struct trace_iterator
) -
4048 offsetof(struct trace_iterator
, seq
));
4049 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4052 if (find_next_entry_inc(&iter
) != NULL
) {
4053 print_trace_line(&iter
);
4054 trace_consume(&iter
);
4057 trace_printk_seq(&iter
.seq
);
4061 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4063 printk(KERN_TRACE
"---------------------------------\n");
4066 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
4069 __init
static int tracer_alloc_buffers(void)
4071 struct trace_array_cpu
*data
;
4076 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4079 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4080 goto out_free_buffer_mask
;
4082 if (!alloc_cpumask_var(&tracing_reader_cpumask
, GFP_KERNEL
))
4083 goto out_free_tracing_cpumask
;
4085 /* To save memory, keep the ring buffer size to its minimum */
4086 if (ring_buffer_expanded
)
4087 ring_buf_size
= trace_buf_size
;
4091 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4092 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4093 cpumask_clear(tracing_reader_cpumask
);
4095 /* TODO: make the number of buffers hot pluggable with CPUS */
4096 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
,
4097 TRACE_BUFFER_FLAGS
);
4098 if (!global_trace
.buffer
) {
4099 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4101 goto out_free_cpumask
;
4103 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4106 #ifdef CONFIG_TRACER_MAX_TRACE
4107 max_tr
.buffer
= ring_buffer_alloc(ring_buf_size
,
4108 TRACE_BUFFER_FLAGS
);
4109 if (!max_tr
.buffer
) {
4110 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4112 ring_buffer_free(global_trace
.buffer
);
4113 goto out_free_cpumask
;
4115 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
4116 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
4119 /* Allocate the first page for all buffers */
4120 for_each_tracing_cpu(i
) {
4121 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4122 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
4125 trace_init_cmdlines();
4127 register_tracer(&nop_trace
);
4128 current_trace
= &nop_trace
;
4129 #ifdef CONFIG_BOOT_TRACER
4130 register_tracer(&boot_tracer
);
4132 /* All seems OK, enable tracing */
4133 tracing_disabled
= 0;
4135 atomic_notifier_chain_register(&panic_notifier_list
,
4136 &trace_panic_notifier
);
4138 register_die_notifier(&trace_die_notifier
);
4143 free_cpumask_var(tracing_reader_cpumask
);
4144 out_free_tracing_cpumask
:
4145 free_cpumask_var(tracing_cpumask
);
4146 out_free_buffer_mask
:
4147 free_cpumask_var(tracing_buffer_mask
);
4152 __init
static int clear_boot_tracer(void)
4155 * The default tracer at boot buffer is an init section.
4156 * This function is called in lateinit. If we did not
4157 * find the boot tracer, then clear it out, to prevent
4158 * later registration from accessing the buffer that is
4159 * about to be freed.
4161 if (!default_bootup_tracer
)
4164 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4165 default_bootup_tracer
);
4166 default_bootup_tracer
= NULL
;
4171 early_initcall(tracer_alloc_buffers
);
4172 fs_initcall(tracer_init_debugfs
);
4173 late_initcall(clear_boot_tracer
);