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
)
744 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
748 * It's not the end of the world if we don't get
749 * the lock, but we also don't want to spin
750 * nor do we want to disable interrupts,
751 * so if we miss here, then better luck next time.
753 if (!__raw_spin_trylock(&trace_cmdline_lock
))
756 idx
= map_pid_to_cmdline
[tsk
->pid
];
757 if (idx
== NO_CMDLINE_MAP
) {
758 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
760 map
= map_cmdline_to_pid
[idx
];
761 if (map
!= NO_CMDLINE_MAP
)
762 map_pid_to_cmdline
[map
] = NO_CMDLINE_MAP
;
764 map_pid_to_cmdline
[tsk
->pid
] = idx
;
769 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
771 __raw_spin_unlock(&trace_cmdline_lock
);
774 void trace_find_cmdline(int pid
, char comm
[])
779 strcpy(comm
, "<idle>");
783 if (pid
> PID_MAX_DEFAULT
) {
784 strcpy(comm
, "<...>");
788 __raw_spin_lock(&trace_cmdline_lock
);
789 map
= map_pid_to_cmdline
[pid
];
790 if (map
== NO_CMDLINE_MAP
)
793 strcpy(comm
, saved_cmdlines
[map
]);
796 __raw_spin_unlock(&trace_cmdline_lock
);
799 void tracing_record_cmdline(struct task_struct
*tsk
)
801 if (atomic_read(&trace_record_cmdline_disabled
) || !tracer_enabled
||
805 trace_save_cmdline(tsk
);
809 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
812 struct task_struct
*tsk
= current
;
814 entry
->preempt_count
= pc
& 0xff;
815 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
816 entry
->tgid
= (tsk
) ? tsk
->tgid
: 0;
818 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
819 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
821 TRACE_FLAG_IRQS_NOSUPPORT
|
823 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
824 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
825 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
828 struct ring_buffer_event
*trace_buffer_lock_reserve(struct trace_array
*tr
,
831 unsigned long flags
, int pc
)
833 struct ring_buffer_event
*event
;
835 event
= ring_buffer_lock_reserve(tr
->buffer
, len
);
837 struct trace_entry
*ent
= ring_buffer_event_data(event
);
839 tracing_generic_entry_update(ent
, flags
, pc
);
845 static void ftrace_trace_stack(struct trace_array
*tr
,
846 unsigned long flags
, int skip
, int pc
);
847 static void ftrace_trace_userstack(struct trace_array
*tr
,
848 unsigned long flags
, int pc
);
850 void trace_buffer_unlock_commit(struct trace_array
*tr
,
851 struct ring_buffer_event
*event
,
852 unsigned long flags
, int pc
)
854 ring_buffer_unlock_commit(tr
->buffer
, event
);
856 ftrace_trace_stack(tr
, flags
, 6, pc
);
857 ftrace_trace_userstack(tr
, flags
, pc
);
861 struct ring_buffer_event
*
862 trace_current_buffer_lock_reserve(unsigned char type
, unsigned long len
,
863 unsigned long flags
, int pc
)
865 return trace_buffer_lock_reserve(&global_trace
,
866 type
, len
, flags
, pc
);
869 void trace_current_buffer_unlock_commit(struct ring_buffer_event
*event
,
870 unsigned long flags
, int pc
)
872 return trace_buffer_unlock_commit(&global_trace
, event
, flags
, pc
);
876 trace_function(struct trace_array
*tr
,
877 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
880 struct ring_buffer_event
*event
;
881 struct ftrace_entry
*entry
;
883 /* If we are reading the ring buffer, don't trace */
884 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
887 event
= trace_buffer_lock_reserve(tr
, TRACE_FN
, sizeof(*entry
),
891 entry
= ring_buffer_event_data(event
);
893 entry
->parent_ip
= parent_ip
;
894 ring_buffer_unlock_commit(tr
->buffer
, event
);
897 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
898 static void __trace_graph_entry(struct trace_array
*tr
,
899 struct ftrace_graph_ent
*trace
,
903 struct ring_buffer_event
*event
;
904 struct ftrace_graph_ent_entry
*entry
;
906 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
909 event
= trace_buffer_lock_reserve(&global_trace
, TRACE_GRAPH_ENT
,
910 sizeof(*entry
), flags
, pc
);
913 entry
= ring_buffer_event_data(event
);
914 entry
->graph_ent
= *trace
;
915 ring_buffer_unlock_commit(global_trace
.buffer
, event
);
918 static void __trace_graph_return(struct trace_array
*tr
,
919 struct ftrace_graph_ret
*trace
,
923 struct ring_buffer_event
*event
;
924 struct ftrace_graph_ret_entry
*entry
;
926 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
929 event
= trace_buffer_lock_reserve(&global_trace
, TRACE_GRAPH_RET
,
930 sizeof(*entry
), flags
, pc
);
933 entry
= ring_buffer_event_data(event
);
935 ring_buffer_unlock_commit(global_trace
.buffer
, event
);
940 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
941 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
944 if (likely(!atomic_read(&data
->disabled
)))
945 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
948 static void __ftrace_trace_stack(struct trace_array
*tr
,
952 #ifdef CONFIG_STACKTRACE
953 struct ring_buffer_event
*event
;
954 struct stack_entry
*entry
;
955 struct stack_trace trace
;
957 event
= trace_buffer_lock_reserve(tr
, TRACE_STACK
,
958 sizeof(*entry
), flags
, pc
);
961 entry
= ring_buffer_event_data(event
);
962 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
964 trace
.nr_entries
= 0;
965 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
967 trace
.entries
= entry
->caller
;
969 save_stack_trace(&trace
);
970 ring_buffer_unlock_commit(tr
->buffer
, event
);
974 static void ftrace_trace_stack(struct trace_array
*tr
,
978 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
981 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
984 void __trace_stack(struct trace_array
*tr
,
988 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
991 static void ftrace_trace_userstack(struct trace_array
*tr
,
992 unsigned long flags
, int pc
)
994 #ifdef CONFIG_STACKTRACE
995 struct ring_buffer_event
*event
;
996 struct userstack_entry
*entry
;
997 struct stack_trace trace
;
999 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1002 event
= trace_buffer_lock_reserve(tr
, TRACE_USER_STACK
,
1003 sizeof(*entry
), flags
, pc
);
1006 entry
= ring_buffer_event_data(event
);
1008 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1010 trace
.nr_entries
= 0;
1011 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1013 trace
.entries
= entry
->caller
;
1015 save_stack_trace_user(&trace
);
1016 ring_buffer_unlock_commit(tr
->buffer
, event
);
1021 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1023 ftrace_trace_userstack(tr
, flags
, preempt_count());
1028 ftrace_trace_special(void *__tr
,
1029 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1032 struct ring_buffer_event
*event
;
1033 struct trace_array
*tr
= __tr
;
1034 struct special_entry
*entry
;
1036 event
= trace_buffer_lock_reserve(tr
, TRACE_SPECIAL
,
1037 sizeof(*entry
), 0, pc
);
1040 entry
= ring_buffer_event_data(event
);
1044 trace_buffer_unlock_commit(tr
, event
, 0, pc
);
1048 __trace_special(void *__tr
, void *__data
,
1049 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1051 ftrace_trace_special(__tr
, arg1
, arg2
, arg3
, preempt_count());
1055 tracing_sched_switch_trace(struct trace_array
*tr
,
1056 struct task_struct
*prev
,
1057 struct task_struct
*next
,
1058 unsigned long flags
, int pc
)
1060 struct ring_buffer_event
*event
;
1061 struct ctx_switch_entry
*entry
;
1063 event
= trace_buffer_lock_reserve(tr
, TRACE_CTX
,
1064 sizeof(*entry
), flags
, pc
);
1067 entry
= ring_buffer_event_data(event
);
1068 entry
->prev_pid
= prev
->pid
;
1069 entry
->prev_prio
= prev
->prio
;
1070 entry
->prev_state
= prev
->state
;
1071 entry
->next_pid
= next
->pid
;
1072 entry
->next_prio
= next
->prio
;
1073 entry
->next_state
= next
->state
;
1074 entry
->next_cpu
= task_cpu(next
);
1075 trace_buffer_unlock_commit(tr
, event
, flags
, pc
);
1079 tracing_sched_wakeup_trace(struct trace_array
*tr
,
1080 struct task_struct
*wakee
,
1081 struct task_struct
*curr
,
1082 unsigned long flags
, int pc
)
1084 struct ring_buffer_event
*event
;
1085 struct ctx_switch_entry
*entry
;
1087 event
= trace_buffer_lock_reserve(tr
, TRACE_WAKE
,
1088 sizeof(*entry
), flags
, pc
);
1091 entry
= ring_buffer_event_data(event
);
1092 entry
->prev_pid
= curr
->pid
;
1093 entry
->prev_prio
= curr
->prio
;
1094 entry
->prev_state
= curr
->state
;
1095 entry
->next_pid
= wakee
->pid
;
1096 entry
->next_prio
= wakee
->prio
;
1097 entry
->next_state
= wakee
->state
;
1098 entry
->next_cpu
= task_cpu(wakee
);
1100 ring_buffer_unlock_commit(tr
->buffer
, event
);
1101 ftrace_trace_stack(tr
, flags
, 6, pc
);
1102 ftrace_trace_userstack(tr
, flags
, pc
);
1106 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1108 struct trace_array
*tr
= &global_trace
;
1109 struct trace_array_cpu
*data
;
1110 unsigned long flags
;
1114 if (tracing_disabled
)
1117 pc
= preempt_count();
1118 local_irq_save(flags
);
1119 cpu
= raw_smp_processor_id();
1120 data
= tr
->data
[cpu
];
1122 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1123 ftrace_trace_special(tr
, arg1
, arg2
, arg3
, pc
);
1125 atomic_dec(&data
->disabled
);
1126 local_irq_restore(flags
);
1129 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1130 int trace_graph_entry(struct ftrace_graph_ent
*trace
)
1132 struct trace_array
*tr
= &global_trace
;
1133 struct trace_array_cpu
*data
;
1134 unsigned long flags
;
1139 if (!ftrace_trace_task(current
))
1142 if (!ftrace_graph_addr(trace
->func
))
1145 local_irq_save(flags
);
1146 cpu
= raw_smp_processor_id();
1147 data
= tr
->data
[cpu
];
1148 disabled
= atomic_inc_return(&data
->disabled
);
1149 if (likely(disabled
== 1)) {
1150 pc
= preempt_count();
1151 __trace_graph_entry(tr
, trace
, flags
, pc
);
1153 /* Only do the atomic if it is not already set */
1154 if (!test_tsk_trace_graph(current
))
1155 set_tsk_trace_graph(current
);
1156 atomic_dec(&data
->disabled
);
1157 local_irq_restore(flags
);
1162 void trace_graph_return(struct ftrace_graph_ret
*trace
)
1164 struct trace_array
*tr
= &global_trace
;
1165 struct trace_array_cpu
*data
;
1166 unsigned long flags
;
1171 local_irq_save(flags
);
1172 cpu
= raw_smp_processor_id();
1173 data
= tr
->data
[cpu
];
1174 disabled
= atomic_inc_return(&data
->disabled
);
1175 if (likely(disabled
== 1)) {
1176 pc
= preempt_count();
1177 __trace_graph_return(tr
, trace
, flags
, pc
);
1180 clear_tsk_trace_graph(current
);
1181 atomic_dec(&data
->disabled
);
1182 local_irq_restore(flags
);
1184 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1188 * trace_vbprintk - write binary msg to tracing buffer
1191 int trace_vbprintk(unsigned long ip
, int depth
, const char *fmt
, va_list args
)
1193 static raw_spinlock_t trace_buf_lock
=
1194 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
1195 static u32 trace_buf
[TRACE_BUF_SIZE
];
1197 struct ring_buffer_event
*event
;
1198 struct trace_array
*tr
= &global_trace
;
1199 struct trace_array_cpu
*data
;
1200 struct bprint_entry
*entry
;
1201 unsigned long flags
;
1203 int cpu
, len
= 0, size
, pc
;
1205 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1208 /* Don't pollute graph traces with trace_vprintk internals */
1209 pause_graph_tracing();
1211 pc
= preempt_count();
1212 resched
= ftrace_preempt_disable();
1213 cpu
= raw_smp_processor_id();
1214 data
= tr
->data
[cpu
];
1216 if (unlikely(atomic_read(&data
->disabled
)))
1219 /* Lockdep uses trace_printk for lock tracing */
1220 local_irq_save(flags
);
1221 __raw_spin_lock(&trace_buf_lock
);
1222 len
= vbin_printf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1224 if (len
> TRACE_BUF_SIZE
|| len
< 0)
1227 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1228 event
= trace_buffer_lock_reserve(tr
, TRACE_BPRINT
, size
, flags
, pc
);
1231 entry
= ring_buffer_event_data(event
);
1233 entry
->depth
= depth
;
1236 memcpy(entry
->buf
, trace_buf
, sizeof(u32
) * len
);
1237 ring_buffer_unlock_commit(tr
->buffer
, event
);
1240 __raw_spin_unlock(&trace_buf_lock
);
1241 local_irq_restore(flags
);
1244 ftrace_preempt_enable(resched
);
1245 unpause_graph_tracing();
1249 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1251 int trace_vprintk(unsigned long ip
, int depth
, const char *fmt
, va_list args
)
1253 static raw_spinlock_t trace_buf_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
1254 static char trace_buf
[TRACE_BUF_SIZE
];
1256 struct ring_buffer_event
*event
;
1257 struct trace_array
*tr
= &global_trace
;
1258 struct trace_array_cpu
*data
;
1259 int cpu
, len
= 0, size
, pc
;
1260 struct print_entry
*entry
;
1261 unsigned long irq_flags
;
1263 if (tracing_disabled
|| tracing_selftest_running
)
1266 pc
= preempt_count();
1267 preempt_disable_notrace();
1268 cpu
= raw_smp_processor_id();
1269 data
= tr
->data
[cpu
];
1271 if (unlikely(atomic_read(&data
->disabled
)))
1274 pause_graph_tracing();
1275 raw_local_irq_save(irq_flags
);
1276 __raw_spin_lock(&trace_buf_lock
);
1277 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1279 len
= min(len
, TRACE_BUF_SIZE
-1);
1282 size
= sizeof(*entry
) + len
+ 1;
1283 event
= trace_buffer_lock_reserve(tr
, TRACE_PRINT
, size
, irq_flags
, pc
);
1286 entry
= ring_buffer_event_data(event
);
1288 entry
->depth
= depth
;
1290 memcpy(&entry
->buf
, trace_buf
, len
);
1291 entry
->buf
[len
] = 0;
1292 ring_buffer_unlock_commit(tr
->buffer
, event
);
1295 __raw_spin_unlock(&trace_buf_lock
);
1296 raw_local_irq_restore(irq_flags
);
1297 unpause_graph_tracing();
1299 preempt_enable_notrace();
1303 EXPORT_SYMBOL_GPL(trace_vprintk
);
1305 enum trace_file_type
{
1306 TRACE_FILE_LAT_FMT
= 1,
1307 TRACE_FILE_ANNOTATE
= 2,
1310 static void trace_iterator_increment(struct trace_iterator
*iter
)
1312 /* Don't allow ftrace to trace into the ring buffers */
1313 ftrace_disable_cpu();
1316 if (iter
->buffer_iter
[iter
->cpu
])
1317 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1319 ftrace_enable_cpu();
1322 static struct trace_entry
*
1323 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1325 struct ring_buffer_event
*event
;
1326 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1328 /* Don't allow ftrace to trace into the ring buffers */
1329 ftrace_disable_cpu();
1332 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1334 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1336 ftrace_enable_cpu();
1338 return event
? ring_buffer_event_data(event
) : NULL
;
1341 static struct trace_entry
*
1342 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1344 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1345 struct trace_entry
*ent
, *next
= NULL
;
1346 int cpu_file
= iter
->cpu_file
;
1347 u64 next_ts
= 0, ts
;
1352 * If we are in a per_cpu trace file, don't bother by iterating over
1353 * all cpu and peek directly.
1355 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1356 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1358 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
);
1360 *ent_cpu
= cpu_file
;
1365 for_each_tracing_cpu(cpu
) {
1367 if (ring_buffer_empty_cpu(buffer
, cpu
))
1370 ent
= peek_next_entry(iter
, cpu
, &ts
);
1373 * Pick the entry with the smallest timestamp:
1375 if (ent
&& (!next
|| ts
< next_ts
)) {
1383 *ent_cpu
= next_cpu
;
1391 /* Find the next real entry, without updating the iterator itself */
1392 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1393 int *ent_cpu
, u64
*ent_ts
)
1395 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1398 /* Find the next real entry, and increment the iterator to the next entry */
1399 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1401 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1404 trace_iterator_increment(iter
);
1406 return iter
->ent
? iter
: NULL
;
1409 static void trace_consume(struct trace_iterator
*iter
)
1411 /* Don't allow ftrace to trace into the ring buffers */
1412 ftrace_disable_cpu();
1413 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1414 ftrace_enable_cpu();
1417 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1419 struct trace_iterator
*iter
= m
->private;
1425 /* can't go backwards */
1430 ent
= find_next_entry_inc(iter
);
1434 while (ent
&& iter
->idx
< i
)
1435 ent
= find_next_entry_inc(iter
);
1443 * No necessary locking here. The worst thing which can
1444 * happen is loosing events consumed at the same time
1445 * by a trace_pipe reader.
1446 * Other than that, we don't risk to crash the ring buffer
1447 * because it serializes the readers.
1449 * The current tracer is copied to avoid a global locking
1452 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1454 struct trace_iterator
*iter
= m
->private;
1455 static struct tracer
*old_tracer
;
1456 int cpu_file
= iter
->cpu_file
;
1461 /* copy the tracer to avoid using a global lock all around */
1462 mutex_lock(&trace_types_lock
);
1463 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1464 old_tracer
= current_trace
;
1465 *iter
->trace
= *current_trace
;
1467 mutex_unlock(&trace_types_lock
);
1469 atomic_inc(&trace_record_cmdline_disabled
);
1471 if (*pos
!= iter
->pos
) {
1476 ftrace_disable_cpu();
1478 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1479 for_each_tracing_cpu(cpu
)
1480 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1482 ring_buffer_iter_reset(iter
->buffer_iter
[cpu_file
]);
1485 ftrace_enable_cpu();
1487 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1492 p
= s_next(m
, p
, &l
);
1498 static void s_stop(struct seq_file
*m
, void *p
)
1500 atomic_dec(&trace_record_cmdline_disabled
);
1503 static void print_lat_help_header(struct seq_file
*m
)
1505 seq_puts(m
, "# _------=> CPU# \n");
1506 seq_puts(m
, "# / _-----=> irqs-off \n");
1507 seq_puts(m
, "# | / _----=> need-resched \n");
1508 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1509 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1510 seq_puts(m
, "# |||| / \n");
1511 seq_puts(m
, "# ||||| delay \n");
1512 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1513 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1516 static void print_func_help_header(struct seq_file
*m
)
1518 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1519 seq_puts(m
, "# | | | | |\n");
1524 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1526 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1527 struct trace_array
*tr
= iter
->tr
;
1528 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1529 struct tracer
*type
= current_trace
;
1530 unsigned long total
;
1531 unsigned long entries
;
1532 const char *name
= "preemption";
1537 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1539 ring_buffer_overruns(iter
->tr
->buffer
);
1541 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
1543 seq_puts(m
, "# -----------------------------------"
1544 "---------------------------------\n");
1545 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1546 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1547 nsecs_to_usecs(data
->saved_latency
),
1551 #if defined(CONFIG_PREEMPT_NONE)
1553 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1555 #elif defined(CONFIG_PREEMPT)
1560 /* These are reserved for later use */
1563 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1567 seq_puts(m
, "# -----------------\n");
1568 seq_printf(m
, "# | task: %.16s-%d "
1569 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1570 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1571 data
->policy
, data
->rt_priority
);
1572 seq_puts(m
, "# -----------------\n");
1574 if (data
->critical_start
) {
1575 seq_puts(m
, "# => started at: ");
1576 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1577 trace_print_seq(m
, &iter
->seq
);
1578 seq_puts(m
, "\n# => ended at: ");
1579 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1580 trace_print_seq(m
, &iter
->seq
);
1587 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1589 struct trace_seq
*s
= &iter
->seq
;
1591 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1594 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1597 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1600 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1601 trace_seq_printf(s
, "##### CPU %u buffer started ####\n", iter
->cpu
);
1604 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1606 struct trace_seq
*s
= &iter
->seq
;
1607 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1608 struct trace_entry
*entry
;
1609 struct trace_event
*event
;
1613 test_cpu_buff_start(iter
);
1615 event
= ftrace_find_event(entry
->type
);
1617 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1618 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1619 if (!trace_print_lat_context(iter
))
1622 if (!trace_print_context(iter
))
1628 return event
->trace(iter
, sym_flags
);
1630 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1633 return TRACE_TYPE_HANDLED
;
1635 return TRACE_TYPE_PARTIAL_LINE
;
1638 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1640 struct trace_seq
*s
= &iter
->seq
;
1641 struct trace_entry
*entry
;
1642 struct trace_event
*event
;
1646 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1647 if (!trace_seq_printf(s
, "%d %d %llu ",
1648 entry
->pid
, iter
->cpu
, iter
->ts
))
1652 event
= ftrace_find_event(entry
->type
);
1654 return event
->raw(iter
, 0);
1656 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1659 return TRACE_TYPE_HANDLED
;
1661 return TRACE_TYPE_PARTIAL_LINE
;
1664 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1666 struct trace_seq
*s
= &iter
->seq
;
1667 unsigned char newline
= '\n';
1668 struct trace_entry
*entry
;
1669 struct trace_event
*event
;
1673 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1674 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1675 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1676 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1679 event
= ftrace_find_event(entry
->type
);
1681 enum print_line_t ret
= event
->hex(iter
, 0);
1682 if (ret
!= TRACE_TYPE_HANDLED
)
1686 SEQ_PUT_FIELD_RET(s
, newline
);
1688 return TRACE_TYPE_HANDLED
;
1691 static enum print_line_t
print_bprintk_msg_only(struct trace_iterator
*iter
)
1693 struct trace_seq
*s
= &iter
->seq
;
1694 struct trace_entry
*entry
= iter
->ent
;
1695 struct bprint_entry
*field
;
1698 trace_assign_type(field
, entry
);
1700 ret
= trace_seq_bprintf(s
, field
->fmt
, field
->buf
);
1702 return TRACE_TYPE_PARTIAL_LINE
;
1704 return TRACE_TYPE_HANDLED
;
1707 static enum print_line_t
print_printk_msg_only(struct trace_iterator
*iter
)
1709 struct trace_seq
*s
= &iter
->seq
;
1710 struct trace_entry
*entry
= iter
->ent
;
1711 struct print_entry
*field
;
1714 trace_assign_type(field
, entry
);
1716 ret
= trace_seq_printf(s
, "%s", field
->buf
);
1718 return TRACE_TYPE_PARTIAL_LINE
;
1720 return TRACE_TYPE_HANDLED
;
1723 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1725 struct trace_seq
*s
= &iter
->seq
;
1726 struct trace_entry
*entry
;
1727 struct trace_event
*event
;
1731 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1732 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1733 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1734 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1737 event
= ftrace_find_event(entry
->type
);
1738 return event
? event
->binary(iter
, 0) : TRACE_TYPE_HANDLED
;
1741 static int trace_empty(struct trace_iterator
*iter
)
1745 /* If we are looking at one CPU buffer, only check that one */
1746 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
1747 cpu
= iter
->cpu_file
;
1748 if (iter
->buffer_iter
[cpu
]) {
1749 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1752 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1758 for_each_tracing_cpu(cpu
) {
1759 if (iter
->buffer_iter
[cpu
]) {
1760 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1763 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1771 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1773 enum print_line_t ret
;
1775 if (iter
->trace
&& iter
->trace
->print_line
) {
1776 ret
= iter
->trace
->print_line(iter
);
1777 if (ret
!= TRACE_TYPE_UNHANDLED
)
1781 if (iter
->ent
->type
== TRACE_BPRINT
&&
1782 trace_flags
& TRACE_ITER_PRINTK
&&
1783 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1784 return print_bprintk_msg_only(iter
);
1786 if (iter
->ent
->type
== TRACE_PRINT
&&
1787 trace_flags
& TRACE_ITER_PRINTK
&&
1788 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1789 return print_printk_msg_only(iter
);
1791 if (trace_flags
& TRACE_ITER_BIN
)
1792 return print_bin_fmt(iter
);
1794 if (trace_flags
& TRACE_ITER_HEX
)
1795 return print_hex_fmt(iter
);
1797 if (trace_flags
& TRACE_ITER_RAW
)
1798 return print_raw_fmt(iter
);
1800 return print_trace_fmt(iter
);
1803 static int s_show(struct seq_file
*m
, void *v
)
1805 struct trace_iterator
*iter
= v
;
1807 if (iter
->ent
== NULL
) {
1809 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1812 if (iter
->trace
&& iter
->trace
->print_header
)
1813 iter
->trace
->print_header(m
);
1814 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1815 /* print nothing if the buffers are empty */
1816 if (trace_empty(iter
))
1818 print_trace_header(m
, iter
);
1819 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1820 print_lat_help_header(m
);
1822 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1823 print_func_help_header(m
);
1826 print_trace_line(iter
);
1827 trace_print_seq(m
, &iter
->seq
);
1833 static struct seq_operations tracer_seq_ops
= {
1840 static struct trace_iterator
*
1841 __tracing_open(struct inode
*inode
, struct file
*file
)
1843 long cpu_file
= (long) inode
->i_private
;
1844 void *fail_ret
= ERR_PTR(-ENOMEM
);
1845 struct trace_iterator
*iter
;
1849 if (tracing_disabled
)
1850 return ERR_PTR(-ENODEV
);
1852 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1854 return ERR_PTR(-ENOMEM
);
1857 * We make a copy of the current tracer to avoid concurrent
1858 * changes on it while we are reading.
1860 mutex_lock(&trace_types_lock
);
1861 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
1866 *iter
->trace
= *current_trace
;
1868 if (current_trace
&& current_trace
->print_max
)
1871 iter
->tr
= &global_trace
;
1873 mutex_init(&iter
->mutex
);
1874 iter
->cpu_file
= cpu_file
;
1876 /* Notify the tracer early; before we stop tracing. */
1877 if (iter
->trace
&& iter
->trace
->open
)
1878 iter
->trace
->open(iter
);
1880 /* Annotate start of buffers if we had overruns */
1881 if (ring_buffer_overruns(iter
->tr
->buffer
))
1882 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
1884 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
1885 for_each_tracing_cpu(cpu
) {
1887 iter
->buffer_iter
[cpu
] =
1888 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1891 cpu
= iter
->cpu_file
;
1892 iter
->buffer_iter
[cpu
] =
1893 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1896 /* TODO stop tracer */
1897 ret
= seq_open(file
, &tracer_seq_ops
);
1899 fail_ret
= ERR_PTR(ret
);
1903 m
= file
->private_data
;
1906 /* stop the trace while dumping */
1909 mutex_unlock(&trace_types_lock
);
1914 for_each_tracing_cpu(cpu
) {
1915 if (iter
->buffer_iter
[cpu
])
1916 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1919 mutex_unlock(&trace_types_lock
);
1926 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1928 if (tracing_disabled
)
1931 filp
->private_data
= inode
->i_private
;
1935 static int tracing_release(struct inode
*inode
, struct file
*file
)
1937 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1938 struct trace_iterator
*iter
= m
->private;
1941 mutex_lock(&trace_types_lock
);
1942 for_each_tracing_cpu(cpu
) {
1943 if (iter
->buffer_iter
[cpu
])
1944 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1947 if (iter
->trace
&& iter
->trace
->close
)
1948 iter
->trace
->close(iter
);
1950 /* reenable tracing if it was previously enabled */
1952 mutex_unlock(&trace_types_lock
);
1954 seq_release(inode
, file
);
1955 mutex_destroy(&iter
->mutex
);
1961 static int tracing_open(struct inode
*inode
, struct file
*file
)
1963 struct trace_iterator
*iter
;
1966 iter
= __tracing_open(inode
, file
);
1968 ret
= PTR_ERR(iter
);
1969 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
1970 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
1976 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1978 struct tracer
*t
= m
->private;
1990 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1992 struct tracer
*t
= m
->private;
1995 mutex_lock(&trace_types_lock
);
1996 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2002 static void t_stop(struct seq_file
*m
, void *p
)
2004 mutex_unlock(&trace_types_lock
);
2007 static int t_show(struct seq_file
*m
, void *v
)
2009 struct tracer
*t
= v
;
2014 seq_printf(m
, "%s", t
->name
);
2023 static struct seq_operations show_traces_seq_ops
= {
2030 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2034 if (tracing_disabled
)
2037 ret
= seq_open(file
, &show_traces_seq_ops
);
2039 struct seq_file
*m
= file
->private_data
;
2040 m
->private = trace_types
;
2046 static const struct file_operations tracing_fops
= {
2047 .open
= tracing_open
,
2049 .llseek
= seq_lseek
,
2050 .release
= tracing_release
,
2053 static const struct file_operations show_traces_fops
= {
2054 .open
= show_traces_open
,
2056 .release
= seq_release
,
2060 * Only trace on a CPU if the bitmask is set:
2062 static cpumask_var_t tracing_cpumask
;
2065 * The tracer itself will not take this lock, but still we want
2066 * to provide a consistent cpumask to user-space:
2068 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2071 * Temporary storage for the character representation of the
2072 * CPU bitmask (and one more byte for the newline):
2074 static char mask_str
[NR_CPUS
+ 1];
2077 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2078 size_t count
, loff_t
*ppos
)
2082 mutex_lock(&tracing_cpumask_update_lock
);
2084 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2085 if (count
- len
< 2) {
2089 len
+= sprintf(mask_str
+ len
, "\n");
2090 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2093 mutex_unlock(&tracing_cpumask_update_lock
);
2099 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2100 size_t count
, loff_t
*ppos
)
2103 cpumask_var_t tracing_cpumask_new
;
2105 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2108 mutex_lock(&tracing_cpumask_update_lock
);
2109 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2113 local_irq_disable();
2114 __raw_spin_lock(&ftrace_max_lock
);
2115 for_each_tracing_cpu(cpu
) {
2117 * Increase/decrease the disabled counter if we are
2118 * about to flip a bit in the cpumask:
2120 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2121 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2122 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2124 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2125 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2126 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2129 __raw_spin_unlock(&ftrace_max_lock
);
2132 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2134 mutex_unlock(&tracing_cpumask_update_lock
);
2135 free_cpumask_var(tracing_cpumask_new
);
2140 mutex_unlock(&tracing_cpumask_update_lock
);
2141 free_cpumask_var(tracing_cpumask
);
2146 static const struct file_operations tracing_cpumask_fops
= {
2147 .open
= tracing_open_generic
,
2148 .read
= tracing_cpumask_read
,
2149 .write
= tracing_cpumask_write
,
2153 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2154 size_t cnt
, loff_t
*ppos
)
2156 struct tracer_opt
*trace_opts
;
2164 /* calculate max size */
2165 for (i
= 0; trace_options
[i
]; i
++) {
2166 len
+= strlen(trace_options
[i
]);
2167 len
+= 3; /* "no" and newline */
2170 mutex_lock(&trace_types_lock
);
2171 tracer_flags
= current_trace
->flags
->val
;
2172 trace_opts
= current_trace
->flags
->opts
;
2175 * Increase the size with names of options specific
2176 * of the current tracer.
2178 for (i
= 0; trace_opts
[i
].name
; i
++) {
2179 len
+= strlen(trace_opts
[i
].name
);
2180 len
+= 3; /* "no" and newline */
2183 /* +2 for \n and \0 */
2184 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2186 mutex_unlock(&trace_types_lock
);
2190 for (i
= 0; trace_options
[i
]; i
++) {
2191 if (trace_flags
& (1 << i
))
2192 r
+= sprintf(buf
+ r
, "%s\n", trace_options
[i
]);
2194 r
+= sprintf(buf
+ r
, "no%s\n", trace_options
[i
]);
2197 for (i
= 0; trace_opts
[i
].name
; i
++) {
2198 if (tracer_flags
& trace_opts
[i
].bit
)
2199 r
+= sprintf(buf
+ r
, "%s\n",
2200 trace_opts
[i
].name
);
2202 r
+= sprintf(buf
+ r
, "no%s\n",
2203 trace_opts
[i
].name
);
2205 mutex_unlock(&trace_types_lock
);
2207 WARN_ON(r
>= len
+ 2);
2209 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2215 /* Try to assign a tracer specific option */
2216 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2218 struct tracer_flags
*trace_flags
= trace
->flags
;
2219 struct tracer_opt
*opts
= NULL
;
2223 for (i
= 0; trace_flags
->opts
[i
].name
; i
++) {
2224 opts
= &trace_flags
->opts
[i
];
2225 len
= strlen(opts
->name
);
2227 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2228 ret
= trace
->set_flag(trace_flags
->val
,
2234 if (!trace_flags
->opts
[i
].name
)
2237 /* Refused to handle */
2242 trace_flags
->val
&= ~opts
->bit
;
2244 trace_flags
->val
|= opts
->bit
;
2250 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2251 size_t cnt
, loff_t
*ppos
)
2259 if (cnt
>= sizeof(buf
))
2262 if (copy_from_user(&buf
, ubuf
, cnt
))
2267 if (strncmp(buf
, "no", 2) == 0) {
2272 for (i
= 0; trace_options
[i
]; i
++) {
2273 int len
= strlen(trace_options
[i
]);
2275 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2277 trace_flags
&= ~(1 << i
);
2279 trace_flags
|= (1 << i
);
2284 /* If no option could be set, test the specific tracer options */
2285 if (!trace_options
[i
]) {
2286 mutex_lock(&trace_types_lock
);
2287 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2288 mutex_unlock(&trace_types_lock
);
2298 static const struct file_operations tracing_iter_fops
= {
2299 .open
= tracing_open_generic
,
2300 .read
= tracing_trace_options_read
,
2301 .write
= tracing_trace_options_write
,
2304 static const char readme_msg
[] =
2305 "tracing mini-HOWTO:\n\n"
2307 "# mount -t debugfs nodev /debug\n\n"
2308 "# cat /debug/tracing/available_tracers\n"
2309 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2310 "# cat /debug/tracing/current_tracer\n"
2312 "# echo sched_switch > /debug/tracing/current_tracer\n"
2313 "# cat /debug/tracing/current_tracer\n"
2315 "# cat /debug/tracing/trace_options\n"
2316 "noprint-parent nosym-offset nosym-addr noverbose\n"
2317 "# echo print-parent > /debug/tracing/trace_options\n"
2318 "# echo 1 > /debug/tracing/tracing_enabled\n"
2319 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2320 "echo 0 > /debug/tracing/tracing_enabled\n"
2324 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2325 size_t cnt
, loff_t
*ppos
)
2327 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2328 readme_msg
, strlen(readme_msg
));
2331 static const struct file_operations tracing_readme_fops
= {
2332 .open
= tracing_open_generic
,
2333 .read
= tracing_readme_read
,
2337 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2338 size_t cnt
, loff_t
*ppos
)
2343 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2344 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2348 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2349 size_t cnt
, loff_t
*ppos
)
2351 struct trace_array
*tr
= filp
->private_data
;
2356 if (cnt
>= sizeof(buf
))
2359 if (copy_from_user(&buf
, ubuf
, cnt
))
2364 ret
= strict_strtoul(buf
, 10, &val
);
2370 mutex_lock(&trace_types_lock
);
2371 if (tracer_enabled
^ val
) {
2374 if (current_trace
->start
)
2375 current_trace
->start(tr
);
2380 if (current_trace
->stop
)
2381 current_trace
->stop(tr
);
2384 mutex_unlock(&trace_types_lock
);
2392 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2393 size_t cnt
, loff_t
*ppos
)
2395 char buf
[max_tracer_type_len
+2];
2398 mutex_lock(&trace_types_lock
);
2400 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2402 r
= sprintf(buf
, "\n");
2403 mutex_unlock(&trace_types_lock
);
2405 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2408 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2410 tracing_reset_online_cpus(tr
);
2414 static int tracing_resize_ring_buffer(unsigned long size
)
2419 * If kernel or user changes the size of the ring buffer
2420 * we use the size that was given, and we can forget about
2421 * expanding it later.
2423 ring_buffer_expanded
= 1;
2425 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2429 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2433 r
= ring_buffer_resize(global_trace
.buffer
,
2434 global_trace
.entries
);
2437 * AARGH! We are left with different
2438 * size max buffer!!!!
2439 * The max buffer is our "snapshot" buffer.
2440 * When a tracer needs a snapshot (one of the
2441 * latency tracers), it swaps the max buffer
2442 * with the saved snap shot. We succeeded to
2443 * update the size of the main buffer, but failed to
2444 * update the size of the max buffer. But when we tried
2445 * to reset the main buffer to the original size, we
2446 * failed there too. This is very unlikely to
2447 * happen, but if it does, warn and kill all
2451 tracing_disabled
= 1;
2456 global_trace
.entries
= size
;
2462 * tracing_update_buffers - used by tracing facility to expand ring buffers
2464 * To save on memory when the tracing is never used on a system with it
2465 * configured in. The ring buffers are set to a minimum size. But once
2466 * a user starts to use the tracing facility, then they need to grow
2467 * to their default size.
2469 * This function is to be called when a tracer is about to be used.
2471 int tracing_update_buffers(void)
2475 mutex_lock(&trace_types_lock
);
2476 if (!ring_buffer_expanded
)
2477 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2478 mutex_unlock(&trace_types_lock
);
2483 struct trace_option_dentry
;
2485 static struct trace_option_dentry
*
2486 create_trace_option_files(struct tracer
*tracer
);
2489 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2491 static int tracing_set_tracer(const char *buf
)
2493 static struct trace_option_dentry
*topts
;
2494 struct trace_array
*tr
= &global_trace
;
2498 mutex_lock(&trace_types_lock
);
2500 if (!ring_buffer_expanded
) {
2501 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2507 for (t
= trace_types
; t
; t
= t
->next
) {
2508 if (strcmp(t
->name
, buf
) == 0)
2515 if (t
== current_trace
)
2518 trace_branch_disable();
2519 if (current_trace
&& current_trace
->reset
)
2520 current_trace
->reset(tr
);
2522 destroy_trace_option_files(topts
);
2526 topts
= create_trace_option_files(current_trace
);
2529 ret
= tracer_init(t
, tr
);
2534 trace_branch_enable(tr
);
2536 mutex_unlock(&trace_types_lock
);
2542 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2543 size_t cnt
, loff_t
*ppos
)
2545 char buf
[max_tracer_type_len
+1];
2552 if (cnt
> max_tracer_type_len
)
2553 cnt
= max_tracer_type_len
;
2555 if (copy_from_user(&buf
, ubuf
, cnt
))
2560 /* strip ending whitespace. */
2561 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2564 err
= tracing_set_tracer(buf
);
2574 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2575 size_t cnt
, loff_t
*ppos
)
2577 unsigned long *ptr
= filp
->private_data
;
2581 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2582 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2583 if (r
> sizeof(buf
))
2585 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2589 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2590 size_t cnt
, loff_t
*ppos
)
2592 unsigned long *ptr
= filp
->private_data
;
2597 if (cnt
>= sizeof(buf
))
2600 if (copy_from_user(&buf
, ubuf
, cnt
))
2605 ret
= strict_strtoul(buf
, 10, &val
);
2614 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2616 long cpu_file
= (long) inode
->i_private
;
2617 struct trace_iterator
*iter
;
2620 if (tracing_disabled
)
2623 mutex_lock(&trace_types_lock
);
2625 /* We only allow one reader per cpu */
2626 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
2627 if (!cpumask_empty(tracing_reader_cpumask
)) {
2631 cpumask_setall(tracing_reader_cpumask
);
2633 if (!cpumask_test_cpu(cpu_file
, tracing_reader_cpumask
))
2634 cpumask_set_cpu(cpu_file
, tracing_reader_cpumask
);
2641 /* create a buffer to store the information to pass to userspace */
2642 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2649 * We make a copy of the current tracer to avoid concurrent
2650 * changes on it while we are reading.
2652 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2658 *iter
->trace
= *current_trace
;
2660 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2665 /* trace pipe does not show start of buffer */
2666 cpumask_setall(iter
->started
);
2668 iter
->cpu_file
= cpu_file
;
2669 iter
->tr
= &global_trace
;
2670 mutex_init(&iter
->mutex
);
2671 filp
->private_data
= iter
;
2673 if (iter
->trace
->pipe_open
)
2674 iter
->trace
->pipe_open(iter
);
2677 mutex_unlock(&trace_types_lock
);
2683 mutex_unlock(&trace_types_lock
);
2687 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2689 struct trace_iterator
*iter
= file
->private_data
;
2691 mutex_lock(&trace_types_lock
);
2693 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
)
2694 cpumask_clear(tracing_reader_cpumask
);
2696 cpumask_clear_cpu(iter
->cpu_file
, tracing_reader_cpumask
);
2698 mutex_unlock(&trace_types_lock
);
2700 free_cpumask_var(iter
->started
);
2701 mutex_destroy(&iter
->mutex
);
2709 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2711 struct trace_iterator
*iter
= filp
->private_data
;
2713 if (trace_flags
& TRACE_ITER_BLOCK
) {
2715 * Always select as readable when in blocking mode
2717 return POLLIN
| POLLRDNORM
;
2719 if (!trace_empty(iter
))
2720 return POLLIN
| POLLRDNORM
;
2721 poll_wait(filp
, &trace_wait
, poll_table
);
2722 if (!trace_empty(iter
))
2723 return POLLIN
| POLLRDNORM
;
2730 void default_wait_pipe(struct trace_iterator
*iter
)
2734 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
2736 if (trace_empty(iter
))
2739 finish_wait(&trace_wait
, &wait
);
2743 * This is a make-shift waitqueue.
2744 * A tracer might use this callback on some rare cases:
2746 * 1) the current tracer might hold the runqueue lock when it wakes up
2747 * a reader, hence a deadlock (sched, function, and function graph tracers)
2748 * 2) the function tracers, trace all functions, we don't want
2749 * the overhead of calling wake_up and friends
2750 * (and tracing them too)
2752 * Anyway, this is really very primitive wakeup.
2754 void poll_wait_pipe(struct trace_iterator
*iter
)
2756 set_current_state(TASK_INTERRUPTIBLE
);
2757 /* sleep for 100 msecs, and try again. */
2758 schedule_timeout(HZ
/ 10);
2761 /* Must be called with trace_types_lock mutex held. */
2762 static int tracing_wait_pipe(struct file
*filp
)
2764 struct trace_iterator
*iter
= filp
->private_data
;
2766 while (trace_empty(iter
)) {
2768 if ((filp
->f_flags
& O_NONBLOCK
)) {
2772 mutex_unlock(&iter
->mutex
);
2774 iter
->trace
->wait_pipe(iter
);
2776 mutex_lock(&iter
->mutex
);
2778 if (signal_pending(current
))
2782 * We block until we read something and tracing is disabled.
2783 * We still block if tracing is disabled, but we have never
2784 * read anything. This allows a user to cat this file, and
2785 * then enable tracing. But after we have read something,
2786 * we give an EOF when tracing is again disabled.
2788 * iter->pos will be 0 if we haven't read anything.
2790 if (!tracer_enabled
&& iter
->pos
)
2801 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2802 size_t cnt
, loff_t
*ppos
)
2804 struct trace_iterator
*iter
= filp
->private_data
;
2805 static struct tracer
*old_tracer
;
2808 /* return any leftover data */
2809 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2813 trace_seq_init(&iter
->seq
);
2815 /* copy the tracer to avoid using a global lock all around */
2816 mutex_lock(&trace_types_lock
);
2817 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
2818 old_tracer
= current_trace
;
2819 *iter
->trace
= *current_trace
;
2821 mutex_unlock(&trace_types_lock
);
2824 * Avoid more than one consumer on a single file descriptor
2825 * This is just a matter of traces coherency, the ring buffer itself
2828 mutex_lock(&iter
->mutex
);
2829 if (iter
->trace
->read
) {
2830 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2836 sret
= tracing_wait_pipe(filp
);
2840 /* stop when tracing is finished */
2841 if (trace_empty(iter
)) {
2846 if (cnt
>= PAGE_SIZE
)
2847 cnt
= PAGE_SIZE
- 1;
2849 /* reset all but tr, trace, and overruns */
2850 memset(&iter
->seq
, 0,
2851 sizeof(struct trace_iterator
) -
2852 offsetof(struct trace_iterator
, seq
));
2855 while (find_next_entry_inc(iter
) != NULL
) {
2856 enum print_line_t ret
;
2857 int len
= iter
->seq
.len
;
2859 ret
= print_trace_line(iter
);
2860 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2861 /* don't print partial lines */
2862 iter
->seq
.len
= len
;
2865 if (ret
!= TRACE_TYPE_NO_CONSUME
)
2866 trace_consume(iter
);
2868 if (iter
->seq
.len
>= cnt
)
2872 /* Now copy what we have to the user */
2873 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2874 if (iter
->seq
.readpos
>= iter
->seq
.len
)
2875 trace_seq_init(&iter
->seq
);
2878 * If there was nothing to send to user, inspite of consuming trace
2879 * entries, go back to wait for more entries.
2885 mutex_unlock(&iter
->mutex
);
2890 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
2891 struct pipe_buffer
*buf
)
2893 __free_page(buf
->page
);
2896 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
2899 __free_page(spd
->pages
[idx
]);
2902 static struct pipe_buf_operations tracing_pipe_buf_ops
= {
2904 .map
= generic_pipe_buf_map
,
2905 .unmap
= generic_pipe_buf_unmap
,
2906 .confirm
= generic_pipe_buf_confirm
,
2907 .release
= tracing_pipe_buf_release
,
2908 .steal
= generic_pipe_buf_steal
,
2909 .get
= generic_pipe_buf_get
,
2913 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
2918 /* Seq buffer is page-sized, exactly what we need. */
2920 count
= iter
->seq
.len
;
2921 ret
= print_trace_line(iter
);
2922 count
= iter
->seq
.len
- count
;
2925 iter
->seq
.len
-= count
;
2928 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2929 iter
->seq
.len
-= count
;
2933 trace_consume(iter
);
2935 if (!find_next_entry_inc(iter
)) {
2945 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
2947 struct pipe_inode_info
*pipe
,
2951 struct page
*pages
[PIPE_BUFFERS
];
2952 struct partial_page partial
[PIPE_BUFFERS
];
2953 struct trace_iterator
*iter
= filp
->private_data
;
2954 struct splice_pipe_desc spd
= {
2957 .nr_pages
= 0, /* This gets updated below. */
2959 .ops
= &tracing_pipe_buf_ops
,
2960 .spd_release
= tracing_spd_release_pipe
,
2962 static struct tracer
*old_tracer
;
2967 /* copy the tracer to avoid using a global lock all around */
2968 mutex_lock(&trace_types_lock
);
2969 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
2970 old_tracer
= current_trace
;
2971 *iter
->trace
= *current_trace
;
2973 mutex_unlock(&trace_types_lock
);
2975 mutex_lock(&iter
->mutex
);
2977 if (iter
->trace
->splice_read
) {
2978 ret
= iter
->trace
->splice_read(iter
, filp
,
2979 ppos
, pipe
, len
, flags
);
2984 ret
= tracing_wait_pipe(filp
);
2988 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
2993 /* Fill as many pages as possible. */
2994 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
2995 pages
[i
] = alloc_page(GFP_KERNEL
);
2999 rem
= tracing_fill_pipe_page(rem
, iter
);
3001 /* Copy the data into the page, so we can start over. */
3002 ret
= trace_seq_to_buffer(&iter
->seq
,
3003 page_address(pages
[i
]),
3006 __free_page(pages
[i
]);
3009 partial
[i
].offset
= 0;
3010 partial
[i
].len
= iter
->seq
.len
;
3012 trace_seq_init(&iter
->seq
);
3015 mutex_unlock(&iter
->mutex
);
3019 return splice_to_pipe(pipe
, &spd
);
3022 mutex_unlock(&iter
->mutex
);
3028 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3029 size_t cnt
, loff_t
*ppos
)
3031 struct trace_array
*tr
= filp
->private_data
;
3035 mutex_lock(&trace_types_lock
);
3036 if (!ring_buffer_expanded
)
3037 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3039 trace_buf_size
>> 10);
3041 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3042 mutex_unlock(&trace_types_lock
);
3044 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3048 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3049 size_t cnt
, loff_t
*ppos
)
3055 if (cnt
>= sizeof(buf
))
3058 if (copy_from_user(&buf
, ubuf
, cnt
))
3063 ret
= strict_strtoul(buf
, 10, &val
);
3067 /* must have at least 1 entry */
3071 mutex_lock(&trace_types_lock
);
3075 /* disable all cpu buffers */
3076 for_each_tracing_cpu(cpu
) {
3077 if (global_trace
.data
[cpu
])
3078 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3079 if (max_tr
.data
[cpu
])
3080 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3083 /* value is in KB */
3086 if (val
!= global_trace
.entries
) {
3087 ret
= tracing_resize_ring_buffer(val
);
3096 /* If check pages failed, return ENOMEM */
3097 if (tracing_disabled
)
3100 for_each_tracing_cpu(cpu
) {
3101 if (global_trace
.data
[cpu
])
3102 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3103 if (max_tr
.data
[cpu
])
3104 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3108 max_tr
.entries
= global_trace
.entries
;
3109 mutex_unlock(&trace_types_lock
);
3114 static int mark_printk(const char *fmt
, ...)
3118 va_start(args
, fmt
);
3119 ret
= trace_vprintk(0, -1, fmt
, args
);
3125 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3126 size_t cnt
, loff_t
*fpos
)
3131 if (tracing_disabled
)
3134 if (cnt
> TRACE_BUF_SIZE
)
3135 cnt
= TRACE_BUF_SIZE
;
3137 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3141 if (copy_from_user(buf
, ubuf
, cnt
)) {
3146 /* Cut from the first nil or newline. */
3148 end
= strchr(buf
, '\n');
3152 cnt
= mark_printk("%s\n", buf
);
3159 static const struct file_operations tracing_max_lat_fops
= {
3160 .open
= tracing_open_generic
,
3161 .read
= tracing_max_lat_read
,
3162 .write
= tracing_max_lat_write
,
3165 static const struct file_operations tracing_ctrl_fops
= {
3166 .open
= tracing_open_generic
,
3167 .read
= tracing_ctrl_read
,
3168 .write
= tracing_ctrl_write
,
3171 static const struct file_operations set_tracer_fops
= {
3172 .open
= tracing_open_generic
,
3173 .read
= tracing_set_trace_read
,
3174 .write
= tracing_set_trace_write
,
3177 static const struct file_operations tracing_pipe_fops
= {
3178 .open
= tracing_open_pipe
,
3179 .poll
= tracing_poll_pipe
,
3180 .read
= tracing_read_pipe
,
3181 .splice_read
= tracing_splice_read_pipe
,
3182 .release
= tracing_release_pipe
,
3185 static const struct file_operations tracing_entries_fops
= {
3186 .open
= tracing_open_generic
,
3187 .read
= tracing_entries_read
,
3188 .write
= tracing_entries_write
,
3191 static const struct file_operations tracing_mark_fops
= {
3192 .open
= tracing_open_generic
,
3193 .write
= tracing_mark_write
,
3196 struct ftrace_buffer_info
{
3197 struct trace_array
*tr
;
3203 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3205 int cpu
= (int)(long)inode
->i_private
;
3206 struct ftrace_buffer_info
*info
;
3208 if (tracing_disabled
)
3211 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3215 info
->tr
= &global_trace
;
3217 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3218 /* Force reading ring buffer for first read */
3219 info
->read
= (unsigned int)-1;
3223 filp
->private_data
= info
;
3233 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3234 size_t count
, loff_t
*ppos
)
3236 struct ftrace_buffer_info
*info
= filp
->private_data
;
3244 /* Do we have previous read data to read? */
3245 if (info
->read
< PAGE_SIZE
)
3250 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3257 pos
= ring_buffer_page_len(info
->spare
);
3259 if (pos
< PAGE_SIZE
)
3260 memset(info
->spare
+ pos
, 0, PAGE_SIZE
- pos
);
3263 size
= PAGE_SIZE
- info
->read
;
3267 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3278 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3280 struct ftrace_buffer_info
*info
= file
->private_data
;
3282 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3289 struct ring_buffer
*buffer
;
3294 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3295 struct pipe_buffer
*buf
)
3297 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3302 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3307 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3308 struct pipe_buffer
*buf
)
3313 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3314 struct pipe_buffer
*buf
)
3316 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3321 /* Pipe buffer operations for a buffer. */
3322 static struct pipe_buf_operations buffer_pipe_buf_ops
= {
3324 .map
= generic_pipe_buf_map
,
3325 .unmap
= generic_pipe_buf_unmap
,
3326 .confirm
= generic_pipe_buf_confirm
,
3327 .release
= buffer_pipe_buf_release
,
3328 .steal
= buffer_pipe_buf_steal
,
3329 .get
= buffer_pipe_buf_get
,
3333 * Callback from splice_to_pipe(), if we need to release some pages
3334 * at the end of the spd in case we error'ed out in filling the pipe.
3336 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3338 struct buffer_ref
*ref
=
3339 (struct buffer_ref
*)spd
->partial
[i
].private;
3344 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3346 spd
->partial
[i
].private = 0;
3350 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3351 struct pipe_inode_info
*pipe
, size_t len
,
3354 struct ftrace_buffer_info
*info
= file
->private_data
;
3355 struct partial_page partial
[PIPE_BUFFERS
];
3356 struct page
*pages
[PIPE_BUFFERS
];
3357 struct splice_pipe_desc spd
= {
3361 .ops
= &buffer_pipe_buf_ops
,
3362 .spd_release
= buffer_spd_release
,
3364 struct buffer_ref
*ref
;
3369 * We can't seek on a buffer input
3371 if (unlikely(*ppos
))
3375 for (i
= 0; i
< PIPE_BUFFERS
&& len
; i
++, len
-= size
) {
3379 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3383 ref
->buffer
= info
->tr
->buffer
;
3384 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3390 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3393 ring_buffer_free_read_page(ref
->buffer
,
3400 * zero out any left over data, this is going to
3403 size
= ring_buffer_page_len(ref
->page
);
3404 if (size
< PAGE_SIZE
)
3405 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3407 page
= virt_to_page(ref
->page
);
3409 spd
.pages
[i
] = page
;
3410 spd
.partial
[i
].len
= PAGE_SIZE
;
3411 spd
.partial
[i
].offset
= 0;
3412 spd
.partial
[i
].private = (unsigned long)ref
;
3418 /* did we read anything? */
3419 if (!spd
.nr_pages
) {
3420 if (flags
& SPLICE_F_NONBLOCK
)
3428 ret
= splice_to_pipe(pipe
, &spd
);
3433 static const struct file_operations tracing_buffers_fops
= {
3434 .open
= tracing_buffers_open
,
3435 .read
= tracing_buffers_read
,
3436 .release
= tracing_buffers_release
,
3437 .splice_read
= tracing_buffers_splice_read
,
3438 .llseek
= no_llseek
,
3441 #ifdef CONFIG_DYNAMIC_FTRACE
3443 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3449 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3450 size_t cnt
, loff_t
*ppos
)
3452 static char ftrace_dyn_info_buffer
[1024];
3453 static DEFINE_MUTEX(dyn_info_mutex
);
3454 unsigned long *p
= filp
->private_data
;
3455 char *buf
= ftrace_dyn_info_buffer
;
3456 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3459 mutex_lock(&dyn_info_mutex
);
3460 r
= sprintf(buf
, "%ld ", *p
);
3462 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3465 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3467 mutex_unlock(&dyn_info_mutex
);
3472 static const struct file_operations tracing_dyn_info_fops
= {
3473 .open
= tracing_open_generic
,
3474 .read
= tracing_read_dyn_info
,
3478 static struct dentry
*d_tracer
;
3480 struct dentry
*tracing_init_dentry(void)
3487 d_tracer
= debugfs_create_dir("tracing", NULL
);
3489 if (!d_tracer
&& !once
) {
3491 pr_warning("Could not create debugfs directory 'tracing'\n");
3498 static struct dentry
*d_percpu
;
3500 struct dentry
*tracing_dentry_percpu(void)
3503 struct dentry
*d_tracer
;
3508 d_tracer
= tracing_init_dentry();
3513 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
3515 if (!d_percpu
&& !once
) {
3517 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3524 static void tracing_init_debugfs_percpu(long cpu
)
3526 struct dentry
*d_percpu
= tracing_dentry_percpu();
3527 struct dentry
*entry
, *d_cpu
;
3528 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3531 if (cpu
> 999 || cpu
< 0)
3534 sprintf(cpu_dir
, "cpu%ld", cpu
);
3535 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
3537 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
3541 /* per cpu trace_pipe */
3542 entry
= debugfs_create_file("trace_pipe", 0444, d_cpu
,
3543 (void *) cpu
, &tracing_pipe_fops
);
3545 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3548 entry
= debugfs_create_file("trace", 0444, d_cpu
,
3549 (void *) cpu
, &tracing_fops
);
3551 pr_warning("Could not create debugfs 'trace' entry\n");
3553 entry
= debugfs_create_file("trace_pipe_raw", 0444, d_cpu
,
3554 (void *) cpu
, &tracing_buffers_fops
);
3556 pr_warning("Could not create debugfs 'trace_pipe_raw' entry\n");
3559 #ifdef CONFIG_FTRACE_SELFTEST
3560 /* Let selftest have access to static functions in this file */
3561 #include "trace_selftest.c"
3564 struct trace_option_dentry
{
3565 struct tracer_opt
*opt
;
3566 struct tracer_flags
*flags
;
3567 struct dentry
*entry
;
3571 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3574 struct trace_option_dentry
*topt
= filp
->private_data
;
3577 if (topt
->flags
->val
& topt
->opt
->bit
)
3582 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3586 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3589 struct trace_option_dentry
*topt
= filp
->private_data
;
3594 if (cnt
>= sizeof(buf
))
3597 if (copy_from_user(&buf
, ubuf
, cnt
))
3602 ret
= strict_strtoul(buf
, 10, &val
);
3609 /* do nothing if already cleared */
3610 if (!(topt
->flags
->val
& topt
->opt
->bit
))
3613 mutex_lock(&trace_types_lock
);
3614 if (current_trace
->set_flag
)
3615 ret
= current_trace
->set_flag(topt
->flags
->val
,
3617 mutex_unlock(&trace_types_lock
);
3620 topt
->flags
->val
&= ~topt
->opt
->bit
;
3623 /* do nothing if already set */
3624 if (topt
->flags
->val
& topt
->opt
->bit
)
3627 mutex_lock(&trace_types_lock
);
3628 if (current_trace
->set_flag
)
3629 ret
= current_trace
->set_flag(topt
->flags
->val
,
3631 mutex_unlock(&trace_types_lock
);
3634 topt
->flags
->val
|= topt
->opt
->bit
;
3647 static const struct file_operations trace_options_fops
= {
3648 .open
= tracing_open_generic
,
3649 .read
= trace_options_read
,
3650 .write
= trace_options_write
,
3654 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3657 long index
= (long)filp
->private_data
;
3660 if (trace_flags
& (1 << index
))
3665 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3669 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3672 long index
= (long)filp
->private_data
;
3677 if (cnt
>= sizeof(buf
))
3680 if (copy_from_user(&buf
, ubuf
, cnt
))
3685 ret
= strict_strtoul(buf
, 10, &val
);
3691 trace_flags
&= ~(1 << index
);
3694 trace_flags
|= 1 << index
;
3706 static const struct file_operations trace_options_core_fops
= {
3707 .open
= tracing_open_generic
,
3708 .read
= trace_options_core_read
,
3709 .write
= trace_options_core_write
,
3712 static struct dentry
*trace_options_init_dentry(void)
3714 struct dentry
*d_tracer
;
3715 static struct dentry
*t_options
;
3720 d_tracer
= tracing_init_dentry();
3724 t_options
= debugfs_create_dir("options", d_tracer
);
3726 pr_warning("Could not create debugfs directory 'options'\n");
3734 create_trace_option_file(struct trace_option_dentry
*topt
,
3735 struct tracer_flags
*flags
,
3736 struct tracer_opt
*opt
)
3738 struct dentry
*t_options
;
3739 struct dentry
*entry
;
3741 t_options
= trace_options_init_dentry();
3745 topt
->flags
= flags
;
3748 entry
= debugfs_create_file(opt
->name
, 0644, t_options
, topt
,
3749 &trace_options_fops
);
3751 topt
->entry
= entry
;
3755 static struct trace_option_dentry
*
3756 create_trace_option_files(struct tracer
*tracer
)
3758 struct trace_option_dentry
*topts
;
3759 struct tracer_flags
*flags
;
3760 struct tracer_opt
*opts
;
3766 flags
= tracer
->flags
;
3768 if (!flags
|| !flags
->opts
)
3773 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3776 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
3780 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3781 create_trace_option_file(&topts
[cnt
], flags
,
3788 destroy_trace_option_files(struct trace_option_dentry
*topts
)
3795 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
3796 if (topts
[cnt
].entry
)
3797 debugfs_remove(topts
[cnt
].entry
);
3803 static struct dentry
*
3804 create_trace_option_core_file(const char *option
, long index
)
3806 struct dentry
*t_options
;
3807 struct dentry
*entry
;
3809 t_options
= trace_options_init_dentry();
3813 entry
= debugfs_create_file(option
, 0644, t_options
, (void *)index
,
3814 &trace_options_core_fops
);
3819 static __init
void create_trace_options_dir(void)
3821 struct dentry
*t_options
;
3822 struct dentry
*entry
;
3825 t_options
= trace_options_init_dentry();
3829 for (i
= 0; trace_options
[i
]; i
++) {
3830 entry
= create_trace_option_core_file(trace_options
[i
], i
);
3832 pr_warning("Could not create debugfs %s entry\n",
3837 static __init
int tracer_init_debugfs(void)
3839 struct dentry
*d_tracer
;
3840 struct dentry
*entry
;
3843 d_tracer
= tracing_init_dentry();
3845 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
3846 &global_trace
, &tracing_ctrl_fops
);
3848 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3850 entry
= debugfs_create_file("trace_options", 0644, d_tracer
,
3851 NULL
, &tracing_iter_fops
);
3853 pr_warning("Could not create debugfs 'trace_options' entry\n");
3855 create_trace_options_dir();
3857 entry
= debugfs_create_file("tracing_cpumask", 0644, d_tracer
,
3858 NULL
, &tracing_cpumask_fops
);
3860 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3862 entry
= debugfs_create_file("trace", 0444, d_tracer
,
3863 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
3865 pr_warning("Could not create debugfs 'trace' entry\n");
3867 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
3868 &global_trace
, &show_traces_fops
);
3870 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3872 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
3873 &global_trace
, &set_tracer_fops
);
3875 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3877 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
3878 &tracing_max_latency
,
3879 &tracing_max_lat_fops
);
3881 pr_warning("Could not create debugfs "
3882 "'tracing_max_latency' entry\n");
3884 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
3885 &tracing_thresh
, &tracing_max_lat_fops
);
3887 pr_warning("Could not create debugfs "
3888 "'tracing_thresh' entry\n");
3889 entry
= debugfs_create_file("README", 0644, d_tracer
,
3890 NULL
, &tracing_readme_fops
);
3892 pr_warning("Could not create debugfs 'README' entry\n");
3894 entry
= debugfs_create_file("trace_pipe", 0444, d_tracer
,
3895 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
3897 pr_warning("Could not create debugfs "
3898 "'trace_pipe' entry\n");
3900 entry
= debugfs_create_file("buffer_size_kb", 0644, d_tracer
,
3901 &global_trace
, &tracing_entries_fops
);
3903 pr_warning("Could not create debugfs "
3904 "'buffer_size_kb' entry\n");
3906 entry
= debugfs_create_file("trace_marker", 0220, d_tracer
,
3907 NULL
, &tracing_mark_fops
);
3909 pr_warning("Could not create debugfs "
3910 "'trace_marker' entry\n");
3912 #ifdef CONFIG_DYNAMIC_FTRACE
3913 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
3914 &ftrace_update_tot_cnt
,
3915 &tracing_dyn_info_fops
);
3917 pr_warning("Could not create debugfs "
3918 "'dyn_ftrace_total_info' entry\n");
3920 #ifdef CONFIG_SYSPROF_TRACER
3921 init_tracer_sysprof_debugfs(d_tracer
);
3924 for_each_tracing_cpu(cpu
)
3925 tracing_init_debugfs_percpu(cpu
);
3930 static int trace_panic_handler(struct notifier_block
*this,
3931 unsigned long event
, void *unused
)
3933 if (ftrace_dump_on_oops
)
3938 static struct notifier_block trace_panic_notifier
= {
3939 .notifier_call
= trace_panic_handler
,
3941 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
3944 static int trace_die_handler(struct notifier_block
*self
,
3950 if (ftrace_dump_on_oops
)
3959 static struct notifier_block trace_die_notifier
= {
3960 .notifier_call
= trace_die_handler
,
3965 * printk is set to max of 1024, we really don't need it that big.
3966 * Nothing should be printing 1000 characters anyway.
3968 #define TRACE_MAX_PRINT 1000
3971 * Define here KERN_TRACE so that we have one place to modify
3972 * it if we decide to change what log level the ftrace dump
3975 #define KERN_TRACE KERN_EMERG
3978 trace_printk_seq(struct trace_seq
*s
)
3980 /* Probably should print a warning here. */
3984 /* should be zero ended, but we are paranoid. */
3985 s
->buffer
[s
->len
] = 0;
3987 printk(KERN_TRACE
"%s", s
->buffer
);
3992 void ftrace_dump(void)
3994 static DEFINE_SPINLOCK(ftrace_dump_lock
);
3995 /* use static because iter can be a bit big for the stack */
3996 static struct trace_iterator iter
;
3997 static int dump_ran
;
3998 unsigned long flags
;
4002 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
4008 /* No turning back! */
4012 for_each_tracing_cpu(cpu
) {
4013 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
4016 /* don't look at user memory in panic mode */
4017 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4019 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4021 /* Simulate the iterator */
4022 iter
.tr
= &global_trace
;
4023 iter
.trace
= current_trace
;
4024 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4027 * We need to stop all tracing on all CPUS to read the
4028 * the next buffer. This is a bit expensive, but is
4029 * not done often. We fill all what we can read,
4030 * and then release the locks again.
4033 while (!trace_empty(&iter
)) {
4036 printk(KERN_TRACE
"---------------------------------\n");
4040 /* reset all but tr, trace, and overruns */
4041 memset(&iter
.seq
, 0,
4042 sizeof(struct trace_iterator
) -
4043 offsetof(struct trace_iterator
, seq
));
4044 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4047 if (find_next_entry_inc(&iter
) != NULL
) {
4048 print_trace_line(&iter
);
4049 trace_consume(&iter
);
4052 trace_printk_seq(&iter
.seq
);
4056 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4058 printk(KERN_TRACE
"---------------------------------\n");
4061 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
4064 __init
static int tracer_alloc_buffers(void)
4066 struct trace_array_cpu
*data
;
4071 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4074 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4075 goto out_free_buffer_mask
;
4077 if (!alloc_cpumask_var(&tracing_reader_cpumask
, GFP_KERNEL
))
4078 goto out_free_tracing_cpumask
;
4080 /* To save memory, keep the ring buffer size to its minimum */
4081 if (ring_buffer_expanded
)
4082 ring_buf_size
= trace_buf_size
;
4086 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4087 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4088 cpumask_clear(tracing_reader_cpumask
);
4090 /* TODO: make the number of buffers hot pluggable with CPUS */
4091 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
,
4092 TRACE_BUFFER_FLAGS
);
4093 if (!global_trace
.buffer
) {
4094 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4096 goto out_free_cpumask
;
4098 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4101 #ifdef CONFIG_TRACER_MAX_TRACE
4102 max_tr
.buffer
= ring_buffer_alloc(ring_buf_size
,
4103 TRACE_BUFFER_FLAGS
);
4104 if (!max_tr
.buffer
) {
4105 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4107 ring_buffer_free(global_trace
.buffer
);
4108 goto out_free_cpumask
;
4110 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
4111 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
4114 /* Allocate the first page for all buffers */
4115 for_each_tracing_cpu(i
) {
4116 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4117 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
4120 trace_init_cmdlines();
4122 register_tracer(&nop_trace
);
4123 current_trace
= &nop_trace
;
4124 #ifdef CONFIG_BOOT_TRACER
4125 register_tracer(&boot_tracer
);
4127 /* All seems OK, enable tracing */
4128 tracing_disabled
= 0;
4130 atomic_notifier_chain_register(&panic_notifier_list
,
4131 &trace_panic_notifier
);
4133 register_die_notifier(&trace_die_notifier
);
4138 free_cpumask_var(tracing_reader_cpumask
);
4139 out_free_tracing_cpumask
:
4140 free_cpumask_var(tracing_cpumask
);
4141 out_free_buffer_mask
:
4142 free_cpumask_var(tracing_buffer_mask
);
4147 __init
static int clear_boot_tracer(void)
4150 * The default tracer at boot buffer is an init section.
4151 * This function is called in lateinit. If we did not
4152 * find the boot tracer, then clear it out, to prevent
4153 * later registration from accessing the buffer that is
4154 * about to be freed.
4156 if (!default_bootup_tracer
)
4159 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4160 default_bootup_tracer
);
4161 default_bootup_tracer
= NULL
;
4166 early_initcall(tracer_alloc_buffers
);
4167 fs_initcall(tracer_init_debugfs
);
4168 late_initcall(clear_boot_tracer
);