2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/ring_buffer.h>
15 #include <linux/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/ctype.h>
35 #include <linux/init.h>
36 #include <linux/poll.h>
37 #include <linux/gfp.h>
41 #include "trace_output.h"
43 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
45 unsigned long __read_mostly tracing_max_latency
;
46 unsigned long __read_mostly tracing_thresh
;
49 * On boot up, the ring buffer is set to the minimum size, so that
50 * we do not waste memory on systems that are not using tracing.
52 static int ring_buffer_expanded
;
55 * We need to change this state when a selftest is running.
56 * A selftest will lurk into the ring-buffer to count the
57 * entries inserted during the selftest although some concurrent
58 * insertions into the ring-buffer such as trace_printk could occurred
59 * at the same time, giving false positive or negative results.
61 static bool __read_mostly tracing_selftest_running
;
64 * If a tracer is running, we do not want to run SELFTEST.
66 static bool __read_mostly tracing_selftest_disabled
;
68 /* For tracers that don't implement custom flags */
69 static struct tracer_opt dummy_tracer_opt
[] = {
73 static struct tracer_flags dummy_tracer_flags
= {
75 .opts
= dummy_tracer_opt
78 static int dummy_set_flag(u32 old_flags
, u32 bit
, int set
)
84 * Kill all tracing for good (never come back).
85 * It is initialized to 1 but will turn to zero if the initialization
86 * of the tracer is successful. But that is the only place that sets
89 static int tracing_disabled
= 1;
91 static DEFINE_PER_CPU(local_t
, ftrace_cpu_disabled
);
93 static inline void ftrace_disable_cpu(void)
96 local_inc(&__get_cpu_var(ftrace_cpu_disabled
));
99 static inline void ftrace_enable_cpu(void)
101 local_dec(&__get_cpu_var(ftrace_cpu_disabled
));
105 static cpumask_var_t __read_mostly tracing_buffer_mask
;
107 /* Define which cpu buffers are currently read in trace_pipe */
108 static cpumask_var_t tracing_reader_cpumask
;
110 #define for_each_tracing_cpu(cpu) \
111 for_each_cpu(cpu, tracing_buffer_mask)
114 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
116 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
117 * is set, then ftrace_dump is called. This will output the contents
118 * of the ftrace buffers to the console. This is very useful for
119 * capturing traces that lead to crashes and outputing it to a
122 * It is default off, but you can enable it with either specifying
123 * "ftrace_dump_on_oops" in the kernel command line, or setting
124 * /proc/sys/kernel/ftrace_dump_on_oops to true.
126 int ftrace_dump_on_oops
;
128 static int tracing_set_tracer(const char *buf
);
130 #define BOOTUP_TRACER_SIZE 100
131 static char bootup_tracer_buf
[BOOTUP_TRACER_SIZE
] __initdata
;
132 static char *default_bootup_tracer
;
134 static int __init
set_ftrace(char *str
)
136 strncpy(bootup_tracer_buf
, str
, BOOTUP_TRACER_SIZE
);
137 default_bootup_tracer
= bootup_tracer_buf
;
138 /* We are using ftrace early, expand it */
139 ring_buffer_expanded
= 1;
142 __setup("ftrace=", set_ftrace
);
144 static int __init
set_ftrace_dump_on_oops(char *str
)
146 ftrace_dump_on_oops
= 1;
149 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
151 unsigned long long ns2usecs(cycle_t nsec
)
159 * The global_trace is the descriptor that holds the tracing
160 * buffers for the live tracing. For each CPU, it contains
161 * a link list of pages that will store trace entries. The
162 * page descriptor of the pages in the memory is used to hold
163 * the link list by linking the lru item in the page descriptor
164 * to each of the pages in the buffer per CPU.
166 * For each active CPU there is a data field that holds the
167 * pages for the buffer for that CPU. Each CPU has the same number
168 * of pages allocated for its buffer.
170 static struct trace_array global_trace
;
172 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
174 cycle_t
ftrace_now(int cpu
)
178 /* Early boot up does not have a buffer yet */
179 if (!global_trace
.buffer
)
180 return trace_clock_local();
182 ts
= ring_buffer_time_stamp(global_trace
.buffer
, cpu
);
183 ring_buffer_normalize_time_stamp(global_trace
.buffer
, cpu
, &ts
);
189 * The max_tr is used to snapshot the global_trace when a maximum
190 * latency is reached. Some tracers will use this to store a maximum
191 * trace while it continues examining live traces.
193 * The buffers for the max_tr are set up the same as the global_trace.
194 * When a snapshot is taken, the link list of the max_tr is swapped
195 * with the link list of the global_trace and the buffers are reset for
196 * the global_trace so the tracing can continue.
198 static struct trace_array max_tr
;
200 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
202 /* tracer_enabled is used to toggle activation of a tracer */
203 static int tracer_enabled
= 1;
206 * tracing_is_enabled - return tracer_enabled status
208 * This function is used by other tracers to know the status
209 * of the tracer_enabled flag. Tracers may use this function
210 * to know if it should enable their features when starting
211 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
213 int tracing_is_enabled(void)
215 return tracer_enabled
;
219 * trace_buf_size is the size in bytes that is allocated
220 * for a buffer. Note, the number of bytes is always rounded
223 * This number is purposely set to a low number of 16384.
224 * If the dump on oops happens, it will be much appreciated
225 * to not have to wait for all that output. Anyway this can be
226 * boot time and run time configurable.
228 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
230 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
232 /* trace_types holds a link list of available tracers. */
233 static struct tracer
*trace_types __read_mostly
;
235 /* current_trace points to the tracer that is currently active */
236 static struct tracer
*current_trace __read_mostly
;
239 * max_tracer_type_len is used to simplify the allocating of
240 * buffers to read userspace tracer names. We keep track of
241 * the longest tracer name registered.
243 static int max_tracer_type_len
;
246 * trace_types_lock is used to protect the trace_types list.
247 * This lock is also used to keep user access serialized.
248 * Accesses from userspace will grab this lock while userspace
249 * activities happen inside the kernel.
251 static DEFINE_MUTEX(trace_types_lock
);
253 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
254 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
256 /* trace_flags holds trace_options default values */
257 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
258 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
| TRACE_ITER_SLEEP_TIME
|
259 TRACE_ITER_GRAPH_TIME
;
262 * trace_wake_up - wake up tasks waiting for trace input
264 * Simply wakes up any task that is blocked on the trace_wait
265 * queue. These is used with trace_poll for tasks polling the trace.
267 void trace_wake_up(void)
270 * The runqueue_is_locked() can fail, but this is the best we
273 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
274 wake_up(&trace_wait
);
277 static int __init
set_buf_size(char *str
)
279 unsigned long buf_size
;
284 ret
= strict_strtoul(str
, 0, &buf_size
);
285 /* nr_entries can not be zero */
286 if (ret
< 0 || buf_size
== 0)
288 trace_buf_size
= buf_size
;
291 __setup("trace_buf_size=", set_buf_size
);
293 unsigned long nsecs_to_usecs(unsigned long nsecs
)
298 /* These must match the bit postions in trace_iterator_flags */
299 static const char *trace_options
[] = {
326 * ftrace_max_lock is used to protect the swapping of buffers
327 * when taking a max snapshot. The buffers themselves are
328 * protected by per_cpu spinlocks. But the action of the swap
329 * needs its own lock.
331 * This is defined as a raw_spinlock_t in order to help
332 * with performance when lockdep debugging is enabled.
334 static raw_spinlock_t ftrace_max_lock
=
335 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
338 * Copy the new maximum trace into the separate maximum-trace
339 * structure. (this way the maximum trace is permanently saved,
340 * for later retrieval via /debugfs/tracing/latency_trace)
343 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
345 struct trace_array_cpu
*data
= tr
->data
[cpu
];
348 max_tr
.time_start
= data
->preempt_timestamp
;
350 data
= max_tr
.data
[cpu
];
351 data
->saved_latency
= tracing_max_latency
;
353 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
354 data
->pid
= tsk
->pid
;
355 data
->uid
= task_uid(tsk
);
356 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
357 data
->policy
= tsk
->policy
;
358 data
->rt_priority
= tsk
->rt_priority
;
360 /* record this tasks comm */
361 tracing_record_cmdline(tsk
);
364 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
372 if (s
->len
<= s
->readpos
)
375 len
= s
->len
- s
->readpos
;
378 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
388 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
393 if (s
->len
<= s
->readpos
)
396 len
= s
->len
- s
->readpos
;
399 ret
= memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
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 static inline void __trace_buffer_unlock_commit(struct trace_array
*tr
,
856 struct ring_buffer_event
*event
,
857 unsigned long flags
, int pc
,
860 ring_buffer_unlock_commit(tr
->buffer
, event
);
862 ftrace_trace_stack(tr
, flags
, 6, pc
);
863 ftrace_trace_userstack(tr
, flags
, pc
);
869 void trace_buffer_unlock_commit(struct trace_array
*tr
,
870 struct ring_buffer_event
*event
,
871 unsigned long flags
, int pc
)
873 __trace_buffer_unlock_commit(tr
, event
, flags
, pc
, 1);
876 struct ring_buffer_event
*
877 trace_current_buffer_lock_reserve(unsigned char type
, unsigned long len
,
878 unsigned long flags
, int pc
)
880 return trace_buffer_lock_reserve(&global_trace
,
881 type
, len
, flags
, pc
);
884 void trace_current_buffer_unlock_commit(struct ring_buffer_event
*event
,
885 unsigned long flags
, int pc
)
887 __trace_buffer_unlock_commit(&global_trace
, event
, flags
, pc
, 1);
890 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event
*event
,
891 unsigned long flags
, int pc
)
893 __trace_buffer_unlock_commit(&global_trace
, event
, flags
, pc
, 0);
896 void trace_current_buffer_discard_commit(struct ring_buffer_event
*event
)
898 ring_buffer_discard_commit(global_trace
.buffer
, event
);
902 trace_function(struct trace_array
*tr
,
903 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
906 struct ftrace_event_call
*call
= &event_function
;
907 struct ring_buffer_event
*event
;
908 struct ftrace_entry
*entry
;
910 /* If we are reading the ring buffer, don't trace */
911 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
914 event
= trace_buffer_lock_reserve(tr
, TRACE_FN
, sizeof(*entry
),
918 entry
= ring_buffer_event_data(event
);
920 entry
->parent_ip
= parent_ip
;
922 filter_check_discard(call
, entry
, event
);
924 ring_buffer_unlock_commit(tr
->buffer
, event
);
927 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
928 static int __trace_graph_entry(struct trace_array
*tr
,
929 struct ftrace_graph_ent
*trace
,
933 struct ftrace_event_call
*call
= &event_funcgraph_entry
;
934 struct ring_buffer_event
*event
;
935 struct ftrace_graph_ent_entry
*entry
;
937 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
940 event
= trace_buffer_lock_reserve(&global_trace
, TRACE_GRAPH_ENT
,
941 sizeof(*entry
), flags
, pc
);
944 entry
= ring_buffer_event_data(event
);
945 entry
->graph_ent
= *trace
;
946 filter_check_discard(call
, entry
, event
);
947 ring_buffer_unlock_commit(global_trace
.buffer
, event
);
952 static void __trace_graph_return(struct trace_array
*tr
,
953 struct ftrace_graph_ret
*trace
,
957 struct ftrace_event_call
*call
= &event_funcgraph_exit
;
958 struct ring_buffer_event
*event
;
959 struct ftrace_graph_ret_entry
*entry
;
961 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
964 event
= trace_buffer_lock_reserve(&global_trace
, TRACE_GRAPH_RET
,
965 sizeof(*entry
), flags
, pc
);
968 entry
= ring_buffer_event_data(event
);
970 filter_check_discard(call
, entry
, event
);
971 ring_buffer_unlock_commit(global_trace
.buffer
, event
);
976 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
977 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
980 if (likely(!atomic_read(&data
->disabled
)))
981 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
984 static void __ftrace_trace_stack(struct trace_array
*tr
,
988 #ifdef CONFIG_STACKTRACE
989 struct ftrace_event_call
*call
= &event_kernel_stack
;
990 struct ring_buffer_event
*event
;
991 struct stack_entry
*entry
;
992 struct stack_trace trace
;
994 event
= trace_buffer_lock_reserve(tr
, TRACE_STACK
,
995 sizeof(*entry
), flags
, pc
);
998 entry
= ring_buffer_event_data(event
);
999 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1001 trace
.nr_entries
= 0;
1002 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1004 trace
.entries
= entry
->caller
;
1006 save_stack_trace(&trace
);
1007 filter_check_discard(call
, entry
, event
);
1008 ring_buffer_unlock_commit(tr
->buffer
, event
);
1012 static void ftrace_trace_stack(struct trace_array
*tr
,
1013 unsigned long flags
,
1016 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1019 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
1022 void __trace_stack(struct trace_array
*tr
,
1023 unsigned long flags
,
1026 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
1029 static void ftrace_trace_userstack(struct trace_array
*tr
,
1030 unsigned long flags
, int pc
)
1032 #ifdef CONFIG_STACKTRACE
1033 struct ftrace_event_call
*call
= &event_user_stack
;
1034 struct ring_buffer_event
*event
;
1035 struct userstack_entry
*entry
;
1036 struct stack_trace trace
;
1038 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1041 event
= trace_buffer_lock_reserve(tr
, TRACE_USER_STACK
,
1042 sizeof(*entry
), flags
, pc
);
1045 entry
= ring_buffer_event_data(event
);
1047 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1049 trace
.nr_entries
= 0;
1050 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1052 trace
.entries
= entry
->caller
;
1054 save_stack_trace_user(&trace
);
1055 filter_check_discard(call
, entry
, event
);
1056 ring_buffer_unlock_commit(tr
->buffer
, event
);
1061 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1063 ftrace_trace_userstack(tr
, flags
, preempt_count());
1068 ftrace_trace_special(void *__tr
,
1069 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1072 struct ring_buffer_event
*event
;
1073 struct trace_array
*tr
= __tr
;
1074 struct special_entry
*entry
;
1076 event
= trace_buffer_lock_reserve(tr
, TRACE_SPECIAL
,
1077 sizeof(*entry
), 0, pc
);
1080 entry
= ring_buffer_event_data(event
);
1084 trace_buffer_unlock_commit(tr
, event
, 0, pc
);
1088 __trace_special(void *__tr
, void *__data
,
1089 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1091 ftrace_trace_special(__tr
, arg1
, arg2
, arg3
, preempt_count());
1095 tracing_sched_switch_trace(struct trace_array
*tr
,
1096 struct task_struct
*prev
,
1097 struct task_struct
*next
,
1098 unsigned long flags
, int pc
)
1100 struct ftrace_event_call
*call
= &event_context_switch
;
1101 struct ring_buffer_event
*event
;
1102 struct ctx_switch_entry
*entry
;
1104 event
= trace_buffer_lock_reserve(tr
, TRACE_CTX
,
1105 sizeof(*entry
), flags
, pc
);
1108 entry
= ring_buffer_event_data(event
);
1109 entry
->prev_pid
= prev
->pid
;
1110 entry
->prev_prio
= prev
->prio
;
1111 entry
->prev_state
= prev
->state
;
1112 entry
->next_pid
= next
->pid
;
1113 entry
->next_prio
= next
->prio
;
1114 entry
->next_state
= next
->state
;
1115 entry
->next_cpu
= task_cpu(next
);
1117 filter_check_discard(call
, entry
, event
);
1119 trace_buffer_unlock_commit(tr
, event
, flags
, pc
);
1123 tracing_sched_wakeup_trace(struct trace_array
*tr
,
1124 struct task_struct
*wakee
,
1125 struct task_struct
*curr
,
1126 unsigned long flags
, int pc
)
1128 struct ftrace_event_call
*call
= &event_wakeup
;
1129 struct ring_buffer_event
*event
;
1130 struct ctx_switch_entry
*entry
;
1132 event
= trace_buffer_lock_reserve(tr
, TRACE_WAKE
,
1133 sizeof(*entry
), flags
, pc
);
1136 entry
= ring_buffer_event_data(event
);
1137 entry
->prev_pid
= curr
->pid
;
1138 entry
->prev_prio
= curr
->prio
;
1139 entry
->prev_state
= curr
->state
;
1140 entry
->next_pid
= wakee
->pid
;
1141 entry
->next_prio
= wakee
->prio
;
1142 entry
->next_state
= wakee
->state
;
1143 entry
->next_cpu
= task_cpu(wakee
);
1145 filter_check_discard(call
, entry
, event
);
1147 ring_buffer_unlock_commit(tr
->buffer
, event
);
1148 ftrace_trace_stack(tr
, flags
, 6, pc
);
1149 ftrace_trace_userstack(tr
, flags
, pc
);
1153 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1155 struct trace_array
*tr
= &global_trace
;
1156 struct trace_array_cpu
*data
;
1157 unsigned long flags
;
1161 if (tracing_disabled
)
1164 pc
= preempt_count();
1165 local_irq_save(flags
);
1166 cpu
= raw_smp_processor_id();
1167 data
= tr
->data
[cpu
];
1169 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1170 ftrace_trace_special(tr
, arg1
, arg2
, arg3
, pc
);
1172 atomic_dec(&data
->disabled
);
1173 local_irq_restore(flags
);
1176 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1177 int trace_graph_entry(struct ftrace_graph_ent
*trace
)
1179 struct trace_array
*tr
= &global_trace
;
1180 struct trace_array_cpu
*data
;
1181 unsigned long flags
;
1187 if (!ftrace_trace_task(current
))
1190 if (!ftrace_graph_addr(trace
->func
))
1193 local_irq_save(flags
);
1194 cpu
= raw_smp_processor_id();
1195 data
= tr
->data
[cpu
];
1196 disabled
= atomic_inc_return(&data
->disabled
);
1197 if (likely(disabled
== 1)) {
1198 pc
= preempt_count();
1199 ret
= __trace_graph_entry(tr
, trace
, flags
, pc
);
1203 /* Only do the atomic if it is not already set */
1204 if (!test_tsk_trace_graph(current
))
1205 set_tsk_trace_graph(current
);
1207 atomic_dec(&data
->disabled
);
1208 local_irq_restore(flags
);
1213 void trace_graph_return(struct ftrace_graph_ret
*trace
)
1215 struct trace_array
*tr
= &global_trace
;
1216 struct trace_array_cpu
*data
;
1217 unsigned long flags
;
1222 local_irq_save(flags
);
1223 cpu
= raw_smp_processor_id();
1224 data
= tr
->data
[cpu
];
1225 disabled
= atomic_inc_return(&data
->disabled
);
1226 if (likely(disabled
== 1)) {
1227 pc
= preempt_count();
1228 __trace_graph_return(tr
, trace
, flags
, pc
);
1231 clear_tsk_trace_graph(current
);
1232 atomic_dec(&data
->disabled
);
1233 local_irq_restore(flags
);
1235 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1239 * trace_vbprintk - write binary msg to tracing buffer
1242 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1244 static raw_spinlock_t trace_buf_lock
=
1245 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
1246 static u32 trace_buf
[TRACE_BUF_SIZE
];
1248 struct ftrace_event_call
*call
= &event_bprint
;
1249 struct ring_buffer_event
*event
;
1250 struct trace_array
*tr
= &global_trace
;
1251 struct trace_array_cpu
*data
;
1252 struct bprint_entry
*entry
;
1253 unsigned long flags
;
1255 int cpu
, len
= 0, size
, pc
;
1257 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1260 /* Don't pollute graph traces with trace_vprintk internals */
1261 pause_graph_tracing();
1263 pc
= preempt_count();
1264 resched
= ftrace_preempt_disable();
1265 cpu
= raw_smp_processor_id();
1266 data
= tr
->data
[cpu
];
1268 if (unlikely(atomic_read(&data
->disabled
)))
1271 /* Lockdep uses trace_printk for lock tracing */
1272 local_irq_save(flags
);
1273 __raw_spin_lock(&trace_buf_lock
);
1274 len
= vbin_printf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1276 if (len
> TRACE_BUF_SIZE
|| len
< 0)
1279 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1280 event
= trace_buffer_lock_reserve(tr
, TRACE_BPRINT
, size
, flags
, pc
);
1283 entry
= ring_buffer_event_data(event
);
1287 memcpy(entry
->buf
, trace_buf
, sizeof(u32
) * len
);
1288 filter_check_discard(call
, entry
, event
);
1289 ring_buffer_unlock_commit(tr
->buffer
, event
);
1292 __raw_spin_unlock(&trace_buf_lock
);
1293 local_irq_restore(flags
);
1296 ftrace_preempt_enable(resched
);
1297 unpause_graph_tracing();
1301 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1303 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1305 static raw_spinlock_t trace_buf_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
1306 static char trace_buf
[TRACE_BUF_SIZE
];
1308 struct ftrace_event_call
*call
= &event_print
;
1309 struct ring_buffer_event
*event
;
1310 struct trace_array
*tr
= &global_trace
;
1311 struct trace_array_cpu
*data
;
1312 int cpu
, len
= 0, size
, pc
;
1313 struct print_entry
*entry
;
1314 unsigned long irq_flags
;
1316 if (tracing_disabled
|| tracing_selftest_running
)
1319 pc
= preempt_count();
1320 preempt_disable_notrace();
1321 cpu
= raw_smp_processor_id();
1322 data
= tr
->data
[cpu
];
1324 if (unlikely(atomic_read(&data
->disabled
)))
1327 pause_graph_tracing();
1328 raw_local_irq_save(irq_flags
);
1329 __raw_spin_lock(&trace_buf_lock
);
1330 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1332 len
= min(len
, TRACE_BUF_SIZE
-1);
1335 size
= sizeof(*entry
) + len
+ 1;
1336 event
= trace_buffer_lock_reserve(tr
, TRACE_PRINT
, size
, irq_flags
, pc
);
1339 entry
= ring_buffer_event_data(event
);
1342 memcpy(&entry
->buf
, trace_buf
, len
);
1343 entry
->buf
[len
] = 0;
1344 filter_check_discard(call
, entry
, event
);
1345 ring_buffer_unlock_commit(tr
->buffer
, event
);
1348 __raw_spin_unlock(&trace_buf_lock
);
1349 raw_local_irq_restore(irq_flags
);
1350 unpause_graph_tracing();
1352 preempt_enable_notrace();
1356 EXPORT_SYMBOL_GPL(trace_vprintk
);
1358 enum trace_file_type
{
1359 TRACE_FILE_LAT_FMT
= 1,
1360 TRACE_FILE_ANNOTATE
= 2,
1363 static void trace_iterator_increment(struct trace_iterator
*iter
)
1365 /* Don't allow ftrace to trace into the ring buffers */
1366 ftrace_disable_cpu();
1369 if (iter
->buffer_iter
[iter
->cpu
])
1370 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1372 ftrace_enable_cpu();
1375 static struct trace_entry
*
1376 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1378 struct ring_buffer_event
*event
;
1379 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1381 /* Don't allow ftrace to trace into the ring buffers */
1382 ftrace_disable_cpu();
1385 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1387 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1389 ftrace_enable_cpu();
1391 return event
? ring_buffer_event_data(event
) : NULL
;
1394 static struct trace_entry
*
1395 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1397 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1398 struct trace_entry
*ent
, *next
= NULL
;
1399 int cpu_file
= iter
->cpu_file
;
1400 u64 next_ts
= 0, ts
;
1405 * If we are in a per_cpu trace file, don't bother by iterating over
1406 * all cpu and peek directly.
1408 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1409 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1411 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
);
1413 *ent_cpu
= cpu_file
;
1418 for_each_tracing_cpu(cpu
) {
1420 if (ring_buffer_empty_cpu(buffer
, cpu
))
1423 ent
= peek_next_entry(iter
, cpu
, &ts
);
1426 * Pick the entry with the smallest timestamp:
1428 if (ent
&& (!next
|| ts
< next_ts
)) {
1436 *ent_cpu
= next_cpu
;
1444 /* Find the next real entry, without updating the iterator itself */
1445 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1446 int *ent_cpu
, u64
*ent_ts
)
1448 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1451 /* Find the next real entry, and increment the iterator to the next entry */
1452 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1454 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1457 trace_iterator_increment(iter
);
1459 return iter
->ent
? iter
: NULL
;
1462 static void trace_consume(struct trace_iterator
*iter
)
1464 /* Don't allow ftrace to trace into the ring buffers */
1465 ftrace_disable_cpu();
1466 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1467 ftrace_enable_cpu();
1470 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1472 struct trace_iterator
*iter
= m
->private;
1478 /* can't go backwards */
1483 ent
= find_next_entry_inc(iter
);
1487 while (ent
&& iter
->idx
< i
)
1488 ent
= find_next_entry_inc(iter
);
1496 * No necessary locking here. The worst thing which can
1497 * happen is loosing events consumed at the same time
1498 * by a trace_pipe reader.
1499 * Other than that, we don't risk to crash the ring buffer
1500 * because it serializes the readers.
1502 * The current tracer is copied to avoid a global locking
1505 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1507 struct trace_iterator
*iter
= m
->private;
1508 static struct tracer
*old_tracer
;
1509 int cpu_file
= iter
->cpu_file
;
1514 /* copy the tracer to avoid using a global lock all around */
1515 mutex_lock(&trace_types_lock
);
1516 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1517 old_tracer
= current_trace
;
1518 *iter
->trace
= *current_trace
;
1520 mutex_unlock(&trace_types_lock
);
1522 atomic_inc(&trace_record_cmdline_disabled
);
1524 if (*pos
!= iter
->pos
) {
1529 ftrace_disable_cpu();
1531 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1532 for_each_tracing_cpu(cpu
)
1533 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1535 ring_buffer_iter_reset(iter
->buffer_iter
[cpu_file
]);
1538 ftrace_enable_cpu();
1540 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1545 p
= s_next(m
, p
, &l
);
1551 static void s_stop(struct seq_file
*m
, void *p
)
1553 atomic_dec(&trace_record_cmdline_disabled
);
1556 static void print_lat_help_header(struct seq_file
*m
)
1558 seq_puts(m
, "# _------=> CPU# \n");
1559 seq_puts(m
, "# / _-----=> irqs-off \n");
1560 seq_puts(m
, "# | / _----=> need-resched \n");
1561 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1562 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1563 seq_puts(m
, "# |||| / \n");
1564 seq_puts(m
, "# ||||| delay \n");
1565 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1566 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1569 static void print_func_help_header(struct seq_file
*m
)
1571 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1572 seq_puts(m
, "# | | | | |\n");
1577 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1579 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1580 struct trace_array
*tr
= iter
->tr
;
1581 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1582 struct tracer
*type
= current_trace
;
1583 unsigned long total
;
1584 unsigned long entries
;
1585 const char *name
= "preemption";
1590 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1592 ring_buffer_overruns(iter
->tr
->buffer
);
1594 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
1596 seq_puts(m
, "# -----------------------------------"
1597 "---------------------------------\n");
1598 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1599 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1600 nsecs_to_usecs(data
->saved_latency
),
1604 #if defined(CONFIG_PREEMPT_NONE)
1606 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1608 #elif defined(CONFIG_PREEMPT)
1613 /* These are reserved for later use */
1616 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1620 seq_puts(m
, "# -----------------\n");
1621 seq_printf(m
, "# | task: %.16s-%d "
1622 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1623 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1624 data
->policy
, data
->rt_priority
);
1625 seq_puts(m
, "# -----------------\n");
1627 if (data
->critical_start
) {
1628 seq_puts(m
, "# => started at: ");
1629 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1630 trace_print_seq(m
, &iter
->seq
);
1631 seq_puts(m
, "\n# => ended at: ");
1632 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1633 trace_print_seq(m
, &iter
->seq
);
1640 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1642 struct trace_seq
*s
= &iter
->seq
;
1644 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1647 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1650 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1653 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1655 /* Don't print started cpu buffer for the first entry of the trace */
1657 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
1661 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1663 struct trace_seq
*s
= &iter
->seq
;
1664 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1665 struct trace_entry
*entry
;
1666 struct trace_event
*event
;
1670 test_cpu_buff_start(iter
);
1672 event
= ftrace_find_event(entry
->type
);
1674 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1675 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1676 if (!trace_print_lat_context(iter
))
1679 if (!trace_print_context(iter
))
1685 return event
->trace(iter
, sym_flags
);
1687 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1690 return TRACE_TYPE_HANDLED
;
1692 return TRACE_TYPE_PARTIAL_LINE
;
1695 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1697 struct trace_seq
*s
= &iter
->seq
;
1698 struct trace_entry
*entry
;
1699 struct trace_event
*event
;
1703 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1704 if (!trace_seq_printf(s
, "%d %d %llu ",
1705 entry
->pid
, iter
->cpu
, iter
->ts
))
1709 event
= ftrace_find_event(entry
->type
);
1711 return event
->raw(iter
, 0);
1713 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1716 return TRACE_TYPE_HANDLED
;
1718 return TRACE_TYPE_PARTIAL_LINE
;
1721 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1723 struct trace_seq
*s
= &iter
->seq
;
1724 unsigned char newline
= '\n';
1725 struct trace_entry
*entry
;
1726 struct trace_event
*event
;
1730 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1731 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1732 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1733 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1736 event
= ftrace_find_event(entry
->type
);
1738 enum print_line_t ret
= event
->hex(iter
, 0);
1739 if (ret
!= TRACE_TYPE_HANDLED
)
1743 SEQ_PUT_FIELD_RET(s
, newline
);
1745 return TRACE_TYPE_HANDLED
;
1748 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1750 struct trace_seq
*s
= &iter
->seq
;
1751 struct trace_entry
*entry
;
1752 struct trace_event
*event
;
1756 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1757 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1758 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1759 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1762 event
= ftrace_find_event(entry
->type
);
1763 return event
? event
->binary(iter
, 0) : TRACE_TYPE_HANDLED
;
1766 static int trace_empty(struct trace_iterator
*iter
)
1770 /* If we are looking at one CPU buffer, only check that one */
1771 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
1772 cpu
= iter
->cpu_file
;
1773 if (iter
->buffer_iter
[cpu
]) {
1774 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1777 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1783 for_each_tracing_cpu(cpu
) {
1784 if (iter
->buffer_iter
[cpu
]) {
1785 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1788 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1796 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1798 enum print_line_t ret
;
1800 if (iter
->trace
&& iter
->trace
->print_line
) {
1801 ret
= iter
->trace
->print_line(iter
);
1802 if (ret
!= TRACE_TYPE_UNHANDLED
)
1806 if (iter
->ent
->type
== TRACE_BPRINT
&&
1807 trace_flags
& TRACE_ITER_PRINTK
&&
1808 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1809 return trace_print_bprintk_msg_only(iter
);
1811 if (iter
->ent
->type
== TRACE_PRINT
&&
1812 trace_flags
& TRACE_ITER_PRINTK
&&
1813 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1814 return trace_print_printk_msg_only(iter
);
1816 if (trace_flags
& TRACE_ITER_BIN
)
1817 return print_bin_fmt(iter
);
1819 if (trace_flags
& TRACE_ITER_HEX
)
1820 return print_hex_fmt(iter
);
1822 if (trace_flags
& TRACE_ITER_RAW
)
1823 return print_raw_fmt(iter
);
1825 return print_trace_fmt(iter
);
1828 static int s_show(struct seq_file
*m
, void *v
)
1830 struct trace_iterator
*iter
= v
;
1832 if (iter
->ent
== NULL
) {
1834 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1837 if (iter
->trace
&& iter
->trace
->print_header
)
1838 iter
->trace
->print_header(m
);
1839 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1840 /* print nothing if the buffers are empty */
1841 if (trace_empty(iter
))
1843 print_trace_header(m
, iter
);
1844 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1845 print_lat_help_header(m
);
1847 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1848 print_func_help_header(m
);
1851 print_trace_line(iter
);
1852 trace_print_seq(m
, &iter
->seq
);
1858 static struct seq_operations tracer_seq_ops
= {
1865 static struct trace_iterator
*
1866 __tracing_open(struct inode
*inode
, struct file
*file
)
1868 long cpu_file
= (long) inode
->i_private
;
1869 void *fail_ret
= ERR_PTR(-ENOMEM
);
1870 struct trace_iterator
*iter
;
1874 if (tracing_disabled
)
1875 return ERR_PTR(-ENODEV
);
1877 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1879 return ERR_PTR(-ENOMEM
);
1882 * We make a copy of the current tracer to avoid concurrent
1883 * changes on it while we are reading.
1885 mutex_lock(&trace_types_lock
);
1886 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
1891 *iter
->trace
= *current_trace
;
1893 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
1896 cpumask_clear(iter
->started
);
1898 if (current_trace
&& current_trace
->print_max
)
1901 iter
->tr
= &global_trace
;
1903 mutex_init(&iter
->mutex
);
1904 iter
->cpu_file
= cpu_file
;
1906 /* Notify the tracer early; before we stop tracing. */
1907 if (iter
->trace
&& iter
->trace
->open
)
1908 iter
->trace
->open(iter
);
1910 /* Annotate start of buffers if we had overruns */
1911 if (ring_buffer_overruns(iter
->tr
->buffer
))
1912 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
1914 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
1915 for_each_tracing_cpu(cpu
) {
1917 iter
->buffer_iter
[cpu
] =
1918 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1921 cpu
= iter
->cpu_file
;
1922 iter
->buffer_iter
[cpu
] =
1923 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1926 /* TODO stop tracer */
1927 ret
= seq_open(file
, &tracer_seq_ops
);
1929 fail_ret
= ERR_PTR(ret
);
1933 m
= file
->private_data
;
1936 /* stop the trace while dumping */
1939 mutex_unlock(&trace_types_lock
);
1944 for_each_tracing_cpu(cpu
) {
1945 if (iter
->buffer_iter
[cpu
])
1946 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1948 free_cpumask_var(iter
->started
);
1950 mutex_unlock(&trace_types_lock
);
1957 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1959 if (tracing_disabled
)
1962 filp
->private_data
= inode
->i_private
;
1966 static int tracing_release(struct inode
*inode
, struct file
*file
)
1968 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1969 struct trace_iterator
*iter
;
1972 if (!(file
->f_mode
& FMODE_READ
))
1977 mutex_lock(&trace_types_lock
);
1978 for_each_tracing_cpu(cpu
) {
1979 if (iter
->buffer_iter
[cpu
])
1980 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1983 if (iter
->trace
&& iter
->trace
->close
)
1984 iter
->trace
->close(iter
);
1986 /* reenable tracing if it was previously enabled */
1988 mutex_unlock(&trace_types_lock
);
1990 seq_release(inode
, file
);
1991 mutex_destroy(&iter
->mutex
);
1992 free_cpumask_var(iter
->started
);
1998 static int tracing_open(struct inode
*inode
, struct file
*file
)
2000 struct trace_iterator
*iter
;
2003 /* If this file was open for write, then erase contents */
2004 if ((file
->f_mode
& FMODE_WRITE
) &&
2005 !(file
->f_flags
& O_APPEND
)) {
2006 long cpu
= (long) inode
->i_private
;
2008 if (cpu
== TRACE_PIPE_ALL_CPU
)
2009 tracing_reset_online_cpus(&global_trace
);
2011 tracing_reset(&global_trace
, cpu
);
2014 if (file
->f_mode
& FMODE_READ
) {
2015 iter
= __tracing_open(inode
, file
);
2017 ret
= PTR_ERR(iter
);
2018 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2019 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2025 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2027 struct tracer
*t
= m
->private;
2039 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2041 struct tracer
*t
= m
->private;
2044 mutex_lock(&trace_types_lock
);
2045 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2051 static void t_stop(struct seq_file
*m
, void *p
)
2053 mutex_unlock(&trace_types_lock
);
2056 static int t_show(struct seq_file
*m
, void *v
)
2058 struct tracer
*t
= v
;
2063 seq_printf(m
, "%s", t
->name
);
2072 static struct seq_operations show_traces_seq_ops
= {
2079 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2083 if (tracing_disabled
)
2086 ret
= seq_open(file
, &show_traces_seq_ops
);
2088 struct seq_file
*m
= file
->private_data
;
2089 m
->private = trace_types
;
2096 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
2097 size_t count
, loff_t
*ppos
)
2102 static const struct file_operations tracing_fops
= {
2103 .open
= tracing_open
,
2105 .write
= tracing_write_stub
,
2106 .llseek
= seq_lseek
,
2107 .release
= tracing_release
,
2110 static const struct file_operations show_traces_fops
= {
2111 .open
= show_traces_open
,
2113 .release
= seq_release
,
2117 * Only trace on a CPU if the bitmask is set:
2119 static cpumask_var_t tracing_cpumask
;
2122 * The tracer itself will not take this lock, but still we want
2123 * to provide a consistent cpumask to user-space:
2125 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2128 * Temporary storage for the character representation of the
2129 * CPU bitmask (and one more byte for the newline):
2131 static char mask_str
[NR_CPUS
+ 1];
2134 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2135 size_t count
, loff_t
*ppos
)
2139 mutex_lock(&tracing_cpumask_update_lock
);
2141 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2142 if (count
- len
< 2) {
2146 len
+= sprintf(mask_str
+ len
, "\n");
2147 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2150 mutex_unlock(&tracing_cpumask_update_lock
);
2156 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2157 size_t count
, loff_t
*ppos
)
2160 cpumask_var_t tracing_cpumask_new
;
2162 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2165 mutex_lock(&tracing_cpumask_update_lock
);
2166 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2170 local_irq_disable();
2171 __raw_spin_lock(&ftrace_max_lock
);
2172 for_each_tracing_cpu(cpu
) {
2174 * Increase/decrease the disabled counter if we are
2175 * about to flip a bit in the cpumask:
2177 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2178 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2179 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2181 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2182 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2183 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2186 __raw_spin_unlock(&ftrace_max_lock
);
2189 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2191 mutex_unlock(&tracing_cpumask_update_lock
);
2192 free_cpumask_var(tracing_cpumask_new
);
2197 mutex_unlock(&tracing_cpumask_update_lock
);
2198 free_cpumask_var(tracing_cpumask
);
2203 static const struct file_operations tracing_cpumask_fops
= {
2204 .open
= tracing_open_generic
,
2205 .read
= tracing_cpumask_read
,
2206 .write
= tracing_cpumask_write
,
2210 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2211 size_t cnt
, loff_t
*ppos
)
2213 struct tracer_opt
*trace_opts
;
2221 /* calculate max size */
2222 for (i
= 0; trace_options
[i
]; i
++) {
2223 len
+= strlen(trace_options
[i
]);
2224 len
+= 3; /* "no" and newline */
2227 mutex_lock(&trace_types_lock
);
2228 tracer_flags
= current_trace
->flags
->val
;
2229 trace_opts
= current_trace
->flags
->opts
;
2232 * Increase the size with names of options specific
2233 * of the current tracer.
2235 for (i
= 0; trace_opts
[i
].name
; i
++) {
2236 len
+= strlen(trace_opts
[i
].name
);
2237 len
+= 3; /* "no" and newline */
2240 /* +2 for \n and \0 */
2241 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2243 mutex_unlock(&trace_types_lock
);
2247 for (i
= 0; trace_options
[i
]; i
++) {
2248 if (trace_flags
& (1 << i
))
2249 r
+= sprintf(buf
+ r
, "%s\n", trace_options
[i
]);
2251 r
+= sprintf(buf
+ r
, "no%s\n", trace_options
[i
]);
2254 for (i
= 0; trace_opts
[i
].name
; i
++) {
2255 if (tracer_flags
& trace_opts
[i
].bit
)
2256 r
+= sprintf(buf
+ r
, "%s\n",
2257 trace_opts
[i
].name
);
2259 r
+= sprintf(buf
+ r
, "no%s\n",
2260 trace_opts
[i
].name
);
2262 mutex_unlock(&trace_types_lock
);
2264 WARN_ON(r
>= len
+ 2);
2266 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2272 /* Try to assign a tracer specific option */
2273 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2275 struct tracer_flags
*trace_flags
= trace
->flags
;
2276 struct tracer_opt
*opts
= NULL
;
2280 for (i
= 0; trace_flags
->opts
[i
].name
; i
++) {
2281 opts
= &trace_flags
->opts
[i
];
2282 len
= strlen(opts
->name
);
2284 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2285 ret
= trace
->set_flag(trace_flags
->val
,
2291 if (!trace_flags
->opts
[i
].name
)
2294 /* Refused to handle */
2299 trace_flags
->val
&= ~opts
->bit
;
2301 trace_flags
->val
|= opts
->bit
;
2306 static void set_tracer_flags(unsigned int mask
, int enabled
)
2308 /* do nothing if flag is already set */
2309 if (!!(trace_flags
& mask
) == !!enabled
)
2313 trace_flags
|= mask
;
2315 trace_flags
&= ~mask
;
2317 if (mask
== TRACE_ITER_GLOBAL_CLK
) {
2321 func
= trace_clock_global
;
2323 func
= trace_clock_local
;
2325 mutex_lock(&trace_types_lock
);
2326 ring_buffer_set_clock(global_trace
.buffer
, func
);
2329 ring_buffer_set_clock(max_tr
.buffer
, func
);
2330 mutex_unlock(&trace_types_lock
);
2335 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2336 size_t cnt
, loff_t
*ppos
)
2344 if (cnt
>= sizeof(buf
))
2347 if (copy_from_user(&buf
, ubuf
, cnt
))
2352 if (strncmp(buf
, "no", 2) == 0) {
2357 for (i
= 0; trace_options
[i
]; i
++) {
2358 int len
= strlen(trace_options
[i
]);
2360 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2361 set_tracer_flags(1 << i
, !neg
);
2366 /* If no option could be set, test the specific tracer options */
2367 if (!trace_options
[i
]) {
2368 mutex_lock(&trace_types_lock
);
2369 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2370 mutex_unlock(&trace_types_lock
);
2380 static const struct file_operations tracing_iter_fops
= {
2381 .open
= tracing_open_generic
,
2382 .read
= tracing_trace_options_read
,
2383 .write
= tracing_trace_options_write
,
2386 static const char readme_msg
[] =
2387 "tracing mini-HOWTO:\n\n"
2389 "# mount -t debugfs nodev /debug\n\n"
2390 "# cat /debug/tracing/available_tracers\n"
2391 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2392 "# cat /debug/tracing/current_tracer\n"
2394 "# echo sched_switch > /debug/tracing/current_tracer\n"
2395 "# cat /debug/tracing/current_tracer\n"
2397 "# cat /debug/tracing/trace_options\n"
2398 "noprint-parent nosym-offset nosym-addr noverbose\n"
2399 "# echo print-parent > /debug/tracing/trace_options\n"
2400 "# echo 1 > /debug/tracing/tracing_enabled\n"
2401 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2402 "echo 0 > /debug/tracing/tracing_enabled\n"
2406 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2407 size_t cnt
, loff_t
*ppos
)
2409 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2410 readme_msg
, strlen(readme_msg
));
2413 static const struct file_operations tracing_readme_fops
= {
2414 .open
= tracing_open_generic
,
2415 .read
= tracing_readme_read
,
2419 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2420 size_t cnt
, loff_t
*ppos
)
2425 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2426 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2430 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2431 size_t cnt
, loff_t
*ppos
)
2433 struct trace_array
*tr
= filp
->private_data
;
2438 if (cnt
>= sizeof(buf
))
2441 if (copy_from_user(&buf
, ubuf
, cnt
))
2446 ret
= strict_strtoul(buf
, 10, &val
);
2452 mutex_lock(&trace_types_lock
);
2453 if (tracer_enabled
^ val
) {
2456 if (current_trace
->start
)
2457 current_trace
->start(tr
);
2462 if (current_trace
->stop
)
2463 current_trace
->stop(tr
);
2466 mutex_unlock(&trace_types_lock
);
2474 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2475 size_t cnt
, loff_t
*ppos
)
2477 char buf
[max_tracer_type_len
+2];
2480 mutex_lock(&trace_types_lock
);
2482 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2484 r
= sprintf(buf
, "\n");
2485 mutex_unlock(&trace_types_lock
);
2487 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2490 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2492 tracing_reset_online_cpus(tr
);
2496 static int tracing_resize_ring_buffer(unsigned long size
)
2501 * If kernel or user changes the size of the ring buffer
2502 * we use the size that was given, and we can forget about
2503 * expanding it later.
2505 ring_buffer_expanded
= 1;
2507 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2511 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2515 r
= ring_buffer_resize(global_trace
.buffer
,
2516 global_trace
.entries
);
2519 * AARGH! We are left with different
2520 * size max buffer!!!!
2521 * The max buffer is our "snapshot" buffer.
2522 * When a tracer needs a snapshot (one of the
2523 * latency tracers), it swaps the max buffer
2524 * with the saved snap shot. We succeeded to
2525 * update the size of the main buffer, but failed to
2526 * update the size of the max buffer. But when we tried
2527 * to reset the main buffer to the original size, we
2528 * failed there too. This is very unlikely to
2529 * happen, but if it does, warn and kill all
2533 tracing_disabled
= 1;
2538 global_trace
.entries
= size
;
2544 * tracing_update_buffers - used by tracing facility to expand ring buffers
2546 * To save on memory when the tracing is never used on a system with it
2547 * configured in. The ring buffers are set to a minimum size. But once
2548 * a user starts to use the tracing facility, then they need to grow
2549 * to their default size.
2551 * This function is to be called when a tracer is about to be used.
2553 int tracing_update_buffers(void)
2557 mutex_lock(&trace_types_lock
);
2558 if (!ring_buffer_expanded
)
2559 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2560 mutex_unlock(&trace_types_lock
);
2565 struct trace_option_dentry
;
2567 static struct trace_option_dentry
*
2568 create_trace_option_files(struct tracer
*tracer
);
2571 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2573 static int tracing_set_tracer(const char *buf
)
2575 static struct trace_option_dentry
*topts
;
2576 struct trace_array
*tr
= &global_trace
;
2580 mutex_lock(&trace_types_lock
);
2582 if (!ring_buffer_expanded
) {
2583 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2589 for (t
= trace_types
; t
; t
= t
->next
) {
2590 if (strcmp(t
->name
, buf
) == 0)
2597 if (t
== current_trace
)
2600 trace_branch_disable();
2601 if (current_trace
&& current_trace
->reset
)
2602 current_trace
->reset(tr
);
2604 destroy_trace_option_files(topts
);
2608 topts
= create_trace_option_files(current_trace
);
2611 ret
= tracer_init(t
, tr
);
2616 trace_branch_enable(tr
);
2618 mutex_unlock(&trace_types_lock
);
2624 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2625 size_t cnt
, loff_t
*ppos
)
2627 char buf
[max_tracer_type_len
+1];
2634 if (cnt
> max_tracer_type_len
)
2635 cnt
= max_tracer_type_len
;
2637 if (copy_from_user(&buf
, ubuf
, cnt
))
2642 /* strip ending whitespace. */
2643 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2646 err
= tracing_set_tracer(buf
);
2656 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2657 size_t cnt
, loff_t
*ppos
)
2659 unsigned long *ptr
= filp
->private_data
;
2663 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2664 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2665 if (r
> sizeof(buf
))
2667 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2671 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2672 size_t cnt
, loff_t
*ppos
)
2674 unsigned long *ptr
= filp
->private_data
;
2679 if (cnt
>= sizeof(buf
))
2682 if (copy_from_user(&buf
, ubuf
, cnt
))
2687 ret
= strict_strtoul(buf
, 10, &val
);
2696 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2698 long cpu_file
= (long) inode
->i_private
;
2699 struct trace_iterator
*iter
;
2702 if (tracing_disabled
)
2705 mutex_lock(&trace_types_lock
);
2707 /* We only allow one reader per cpu */
2708 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
2709 if (!cpumask_empty(tracing_reader_cpumask
)) {
2713 cpumask_setall(tracing_reader_cpumask
);
2715 if (!cpumask_test_cpu(cpu_file
, tracing_reader_cpumask
))
2716 cpumask_set_cpu(cpu_file
, tracing_reader_cpumask
);
2723 /* create a buffer to store the information to pass to userspace */
2724 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2731 * We make a copy of the current tracer to avoid concurrent
2732 * changes on it while we are reading.
2734 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2740 *iter
->trace
= *current_trace
;
2742 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2747 /* trace pipe does not show start of buffer */
2748 cpumask_setall(iter
->started
);
2750 iter
->cpu_file
= cpu_file
;
2751 iter
->tr
= &global_trace
;
2752 mutex_init(&iter
->mutex
);
2753 filp
->private_data
= iter
;
2755 if (iter
->trace
->pipe_open
)
2756 iter
->trace
->pipe_open(iter
);
2759 mutex_unlock(&trace_types_lock
);
2765 mutex_unlock(&trace_types_lock
);
2769 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2771 struct trace_iterator
*iter
= file
->private_data
;
2773 mutex_lock(&trace_types_lock
);
2775 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
)
2776 cpumask_clear(tracing_reader_cpumask
);
2778 cpumask_clear_cpu(iter
->cpu_file
, tracing_reader_cpumask
);
2780 mutex_unlock(&trace_types_lock
);
2782 free_cpumask_var(iter
->started
);
2783 mutex_destroy(&iter
->mutex
);
2791 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2793 struct trace_iterator
*iter
= filp
->private_data
;
2795 if (trace_flags
& TRACE_ITER_BLOCK
) {
2797 * Always select as readable when in blocking mode
2799 return POLLIN
| POLLRDNORM
;
2801 if (!trace_empty(iter
))
2802 return POLLIN
| POLLRDNORM
;
2803 poll_wait(filp
, &trace_wait
, poll_table
);
2804 if (!trace_empty(iter
))
2805 return POLLIN
| POLLRDNORM
;
2812 void default_wait_pipe(struct trace_iterator
*iter
)
2816 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
2818 if (trace_empty(iter
))
2821 finish_wait(&trace_wait
, &wait
);
2825 * This is a make-shift waitqueue.
2826 * A tracer might use this callback on some rare cases:
2828 * 1) the current tracer might hold the runqueue lock when it wakes up
2829 * a reader, hence a deadlock (sched, function, and function graph tracers)
2830 * 2) the function tracers, trace all functions, we don't want
2831 * the overhead of calling wake_up and friends
2832 * (and tracing them too)
2834 * Anyway, this is really very primitive wakeup.
2836 void poll_wait_pipe(struct trace_iterator
*iter
)
2838 set_current_state(TASK_INTERRUPTIBLE
);
2839 /* sleep for 100 msecs, and try again. */
2840 schedule_timeout(HZ
/ 10);
2843 /* Must be called with trace_types_lock mutex held. */
2844 static int tracing_wait_pipe(struct file
*filp
)
2846 struct trace_iterator
*iter
= filp
->private_data
;
2848 while (trace_empty(iter
)) {
2850 if ((filp
->f_flags
& O_NONBLOCK
)) {
2854 mutex_unlock(&iter
->mutex
);
2856 iter
->trace
->wait_pipe(iter
);
2858 mutex_lock(&iter
->mutex
);
2860 if (signal_pending(current
))
2864 * We block until we read something and tracing is disabled.
2865 * We still block if tracing is disabled, but we have never
2866 * read anything. This allows a user to cat this file, and
2867 * then enable tracing. But after we have read something,
2868 * we give an EOF when tracing is again disabled.
2870 * iter->pos will be 0 if we haven't read anything.
2872 if (!tracer_enabled
&& iter
->pos
)
2883 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2884 size_t cnt
, loff_t
*ppos
)
2886 struct trace_iterator
*iter
= filp
->private_data
;
2887 static struct tracer
*old_tracer
;
2890 /* return any leftover data */
2891 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2895 trace_seq_init(&iter
->seq
);
2897 /* copy the tracer to avoid using a global lock all around */
2898 mutex_lock(&trace_types_lock
);
2899 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
2900 old_tracer
= current_trace
;
2901 *iter
->trace
= *current_trace
;
2903 mutex_unlock(&trace_types_lock
);
2906 * Avoid more than one consumer on a single file descriptor
2907 * This is just a matter of traces coherency, the ring buffer itself
2910 mutex_lock(&iter
->mutex
);
2911 if (iter
->trace
->read
) {
2912 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2918 sret
= tracing_wait_pipe(filp
);
2922 /* stop when tracing is finished */
2923 if (trace_empty(iter
)) {
2928 if (cnt
>= PAGE_SIZE
)
2929 cnt
= PAGE_SIZE
- 1;
2931 /* reset all but tr, trace, and overruns */
2932 memset(&iter
->seq
, 0,
2933 sizeof(struct trace_iterator
) -
2934 offsetof(struct trace_iterator
, seq
));
2937 while (find_next_entry_inc(iter
) != NULL
) {
2938 enum print_line_t ret
;
2939 int len
= iter
->seq
.len
;
2941 ret
= print_trace_line(iter
);
2942 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2943 /* don't print partial lines */
2944 iter
->seq
.len
= len
;
2947 if (ret
!= TRACE_TYPE_NO_CONSUME
)
2948 trace_consume(iter
);
2950 if (iter
->seq
.len
>= cnt
)
2954 /* Now copy what we have to the user */
2955 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2956 if (iter
->seq
.readpos
>= iter
->seq
.len
)
2957 trace_seq_init(&iter
->seq
);
2960 * If there was nothing to send to user, inspite of consuming trace
2961 * entries, go back to wait for more entries.
2967 mutex_unlock(&iter
->mutex
);
2972 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
2973 struct pipe_buffer
*buf
)
2975 __free_page(buf
->page
);
2978 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
2981 __free_page(spd
->pages
[idx
]);
2984 static struct pipe_buf_operations tracing_pipe_buf_ops
= {
2986 .map
= generic_pipe_buf_map
,
2987 .unmap
= generic_pipe_buf_unmap
,
2988 .confirm
= generic_pipe_buf_confirm
,
2989 .release
= tracing_pipe_buf_release
,
2990 .steal
= generic_pipe_buf_steal
,
2991 .get
= generic_pipe_buf_get
,
2995 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
3000 /* Seq buffer is page-sized, exactly what we need. */
3002 count
= iter
->seq
.len
;
3003 ret
= print_trace_line(iter
);
3004 count
= iter
->seq
.len
- count
;
3007 iter
->seq
.len
-= count
;
3010 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3011 iter
->seq
.len
-= count
;
3015 trace_consume(iter
);
3017 if (!find_next_entry_inc(iter
)) {
3027 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3029 struct pipe_inode_info
*pipe
,
3033 struct page
*pages
[PIPE_BUFFERS
];
3034 struct partial_page partial
[PIPE_BUFFERS
];
3035 struct trace_iterator
*iter
= filp
->private_data
;
3036 struct splice_pipe_desc spd
= {
3039 .nr_pages
= 0, /* This gets updated below. */
3041 .ops
= &tracing_pipe_buf_ops
,
3042 .spd_release
= tracing_spd_release_pipe
,
3044 static struct tracer
*old_tracer
;
3049 /* copy the tracer to avoid using a global lock all around */
3050 mutex_lock(&trace_types_lock
);
3051 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3052 old_tracer
= current_trace
;
3053 *iter
->trace
= *current_trace
;
3055 mutex_unlock(&trace_types_lock
);
3057 mutex_lock(&iter
->mutex
);
3059 if (iter
->trace
->splice_read
) {
3060 ret
= iter
->trace
->splice_read(iter
, filp
,
3061 ppos
, pipe
, len
, flags
);
3066 ret
= tracing_wait_pipe(filp
);
3070 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
3075 /* Fill as many pages as possible. */
3076 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
3077 pages
[i
] = alloc_page(GFP_KERNEL
);
3081 rem
= tracing_fill_pipe_page(rem
, iter
);
3083 /* Copy the data into the page, so we can start over. */
3084 ret
= trace_seq_to_buffer(&iter
->seq
,
3085 page_address(pages
[i
]),
3088 __free_page(pages
[i
]);
3091 partial
[i
].offset
= 0;
3092 partial
[i
].len
= iter
->seq
.len
;
3094 trace_seq_init(&iter
->seq
);
3097 mutex_unlock(&iter
->mutex
);
3101 return splice_to_pipe(pipe
, &spd
);
3104 mutex_unlock(&iter
->mutex
);
3110 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3111 size_t cnt
, loff_t
*ppos
)
3113 struct trace_array
*tr
= filp
->private_data
;
3117 mutex_lock(&trace_types_lock
);
3118 if (!ring_buffer_expanded
)
3119 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3121 trace_buf_size
>> 10);
3123 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3124 mutex_unlock(&trace_types_lock
);
3126 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3130 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3131 size_t cnt
, loff_t
*ppos
)
3137 if (cnt
>= sizeof(buf
))
3140 if (copy_from_user(&buf
, ubuf
, cnt
))
3145 ret
= strict_strtoul(buf
, 10, &val
);
3149 /* must have at least 1 entry */
3153 mutex_lock(&trace_types_lock
);
3157 /* disable all cpu buffers */
3158 for_each_tracing_cpu(cpu
) {
3159 if (global_trace
.data
[cpu
])
3160 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3161 if (max_tr
.data
[cpu
])
3162 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3165 /* value is in KB */
3168 if (val
!= global_trace
.entries
) {
3169 ret
= tracing_resize_ring_buffer(val
);
3178 /* If check pages failed, return ENOMEM */
3179 if (tracing_disabled
)
3182 for_each_tracing_cpu(cpu
) {
3183 if (global_trace
.data
[cpu
])
3184 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3185 if (max_tr
.data
[cpu
])
3186 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3190 max_tr
.entries
= global_trace
.entries
;
3191 mutex_unlock(&trace_types_lock
);
3196 static int mark_printk(const char *fmt
, ...)
3200 va_start(args
, fmt
);
3201 ret
= trace_vprintk(0, fmt
, args
);
3207 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3208 size_t cnt
, loff_t
*fpos
)
3213 if (tracing_disabled
)
3216 if (cnt
> TRACE_BUF_SIZE
)
3217 cnt
= TRACE_BUF_SIZE
;
3219 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3223 if (copy_from_user(buf
, ubuf
, cnt
)) {
3228 /* Cut from the first nil or newline. */
3230 end
= strchr(buf
, '\n');
3234 cnt
= mark_printk("%s\n", buf
);
3241 static const struct file_operations tracing_max_lat_fops
= {
3242 .open
= tracing_open_generic
,
3243 .read
= tracing_max_lat_read
,
3244 .write
= tracing_max_lat_write
,
3247 static const struct file_operations tracing_ctrl_fops
= {
3248 .open
= tracing_open_generic
,
3249 .read
= tracing_ctrl_read
,
3250 .write
= tracing_ctrl_write
,
3253 static const struct file_operations set_tracer_fops
= {
3254 .open
= tracing_open_generic
,
3255 .read
= tracing_set_trace_read
,
3256 .write
= tracing_set_trace_write
,
3259 static const struct file_operations tracing_pipe_fops
= {
3260 .open
= tracing_open_pipe
,
3261 .poll
= tracing_poll_pipe
,
3262 .read
= tracing_read_pipe
,
3263 .splice_read
= tracing_splice_read_pipe
,
3264 .release
= tracing_release_pipe
,
3267 static const struct file_operations tracing_entries_fops
= {
3268 .open
= tracing_open_generic
,
3269 .read
= tracing_entries_read
,
3270 .write
= tracing_entries_write
,
3273 static const struct file_operations tracing_mark_fops
= {
3274 .open
= tracing_open_generic
,
3275 .write
= tracing_mark_write
,
3278 struct ftrace_buffer_info
{
3279 struct trace_array
*tr
;
3285 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3287 int cpu
= (int)(long)inode
->i_private
;
3288 struct ftrace_buffer_info
*info
;
3290 if (tracing_disabled
)
3293 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3297 info
->tr
= &global_trace
;
3300 /* Force reading ring buffer for first read */
3301 info
->read
= (unsigned int)-1;
3303 filp
->private_data
= info
;
3305 return nonseekable_open(inode
, filp
);
3309 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3310 size_t count
, loff_t
*ppos
)
3312 struct ftrace_buffer_info
*info
= filp
->private_data
;
3321 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3325 /* Do we have previous read data to read? */
3326 if (info
->read
< PAGE_SIZE
)
3331 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3338 pos
= ring_buffer_page_len(info
->spare
);
3340 if (pos
< PAGE_SIZE
)
3341 memset(info
->spare
+ pos
, 0, PAGE_SIZE
- pos
);
3344 size
= PAGE_SIZE
- info
->read
;
3348 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3359 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3361 struct ftrace_buffer_info
*info
= file
->private_data
;
3364 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3371 struct ring_buffer
*buffer
;
3376 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3377 struct pipe_buffer
*buf
)
3379 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3384 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3389 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3390 struct pipe_buffer
*buf
)
3395 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3396 struct pipe_buffer
*buf
)
3398 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3403 /* Pipe buffer operations for a buffer. */
3404 static struct pipe_buf_operations buffer_pipe_buf_ops
= {
3406 .map
= generic_pipe_buf_map
,
3407 .unmap
= generic_pipe_buf_unmap
,
3408 .confirm
= generic_pipe_buf_confirm
,
3409 .release
= buffer_pipe_buf_release
,
3410 .steal
= buffer_pipe_buf_steal
,
3411 .get
= buffer_pipe_buf_get
,
3415 * Callback from splice_to_pipe(), if we need to release some pages
3416 * at the end of the spd in case we error'ed out in filling the pipe.
3418 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3420 struct buffer_ref
*ref
=
3421 (struct buffer_ref
*)spd
->partial
[i
].private;
3426 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3428 spd
->partial
[i
].private = 0;
3432 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3433 struct pipe_inode_info
*pipe
, size_t len
,
3436 struct ftrace_buffer_info
*info
= file
->private_data
;
3437 struct partial_page partial
[PIPE_BUFFERS
];
3438 struct page
*pages
[PIPE_BUFFERS
];
3439 struct splice_pipe_desc spd
= {
3443 .ops
= &buffer_pipe_buf_ops
,
3444 .spd_release
= buffer_spd_release
,
3446 struct buffer_ref
*ref
;
3450 if (*ppos
& (PAGE_SIZE
- 1)) {
3451 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3455 if (len
& (PAGE_SIZE
- 1)) {
3456 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3457 if (len
< PAGE_SIZE
)
3462 for (i
= 0; i
< PIPE_BUFFERS
&& len
; i
++, len
-= PAGE_SIZE
) {
3466 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3470 ref
->buffer
= info
->tr
->buffer
;
3471 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3477 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3480 ring_buffer_free_read_page(ref
->buffer
,
3487 * zero out any left over data, this is going to
3490 size
= ring_buffer_page_len(ref
->page
);
3491 if (size
< PAGE_SIZE
)
3492 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3494 page
= virt_to_page(ref
->page
);
3496 spd
.pages
[i
] = page
;
3497 spd
.partial
[i
].len
= PAGE_SIZE
;
3498 spd
.partial
[i
].offset
= 0;
3499 spd
.partial
[i
].private = (unsigned long)ref
;
3506 /* did we read anything? */
3507 if (!spd
.nr_pages
) {
3508 if (flags
& SPLICE_F_NONBLOCK
)
3516 ret
= splice_to_pipe(pipe
, &spd
);
3521 static const struct file_operations tracing_buffers_fops
= {
3522 .open
= tracing_buffers_open
,
3523 .read
= tracing_buffers_read
,
3524 .release
= tracing_buffers_release
,
3525 .splice_read
= tracing_buffers_splice_read
,
3526 .llseek
= no_llseek
,
3529 #ifdef CONFIG_DYNAMIC_FTRACE
3531 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3537 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3538 size_t cnt
, loff_t
*ppos
)
3540 static char ftrace_dyn_info_buffer
[1024];
3541 static DEFINE_MUTEX(dyn_info_mutex
);
3542 unsigned long *p
= filp
->private_data
;
3543 char *buf
= ftrace_dyn_info_buffer
;
3544 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3547 mutex_lock(&dyn_info_mutex
);
3548 r
= sprintf(buf
, "%ld ", *p
);
3550 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3553 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3555 mutex_unlock(&dyn_info_mutex
);
3560 static const struct file_operations tracing_dyn_info_fops
= {
3561 .open
= tracing_open_generic
,
3562 .read
= tracing_read_dyn_info
,
3566 static struct dentry
*d_tracer
;
3568 struct dentry
*tracing_init_dentry(void)
3575 if (!debugfs_initialized())
3578 d_tracer
= debugfs_create_dir("tracing", NULL
);
3580 if (!d_tracer
&& !once
) {
3582 pr_warning("Could not create debugfs directory 'tracing'\n");
3589 static struct dentry
*d_percpu
;
3591 struct dentry
*tracing_dentry_percpu(void)
3594 struct dentry
*d_tracer
;
3599 d_tracer
= tracing_init_dentry();
3604 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
3606 if (!d_percpu
&& !once
) {
3608 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3615 static void tracing_init_debugfs_percpu(long cpu
)
3617 struct dentry
*d_percpu
= tracing_dentry_percpu();
3618 struct dentry
*d_cpu
;
3619 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3622 if (cpu
> 999 || cpu
< 0)
3625 sprintf(cpu_dir
, "cpu%ld", cpu
);
3626 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
3628 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
3632 /* per cpu trace_pipe */
3633 trace_create_file("trace_pipe", 0444, d_cpu
,
3634 (void *) cpu
, &tracing_pipe_fops
);
3637 trace_create_file("trace", 0644, d_cpu
,
3638 (void *) cpu
, &tracing_fops
);
3640 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
3641 (void *) cpu
, &tracing_buffers_fops
);
3644 #ifdef CONFIG_FTRACE_SELFTEST
3645 /* Let selftest have access to static functions in this file */
3646 #include "trace_selftest.c"
3649 struct trace_option_dentry
{
3650 struct tracer_opt
*opt
;
3651 struct tracer_flags
*flags
;
3652 struct dentry
*entry
;
3656 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3659 struct trace_option_dentry
*topt
= filp
->private_data
;
3662 if (topt
->flags
->val
& topt
->opt
->bit
)
3667 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3671 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3674 struct trace_option_dentry
*topt
= filp
->private_data
;
3679 if (cnt
>= sizeof(buf
))
3682 if (copy_from_user(&buf
, ubuf
, cnt
))
3687 ret
= strict_strtoul(buf
, 10, &val
);
3694 /* do nothing if already cleared */
3695 if (!(topt
->flags
->val
& topt
->opt
->bit
))
3698 mutex_lock(&trace_types_lock
);
3699 if (current_trace
->set_flag
)
3700 ret
= current_trace
->set_flag(topt
->flags
->val
,
3702 mutex_unlock(&trace_types_lock
);
3705 topt
->flags
->val
&= ~topt
->opt
->bit
;
3708 /* do nothing if already set */
3709 if (topt
->flags
->val
& topt
->opt
->bit
)
3712 mutex_lock(&trace_types_lock
);
3713 if (current_trace
->set_flag
)
3714 ret
= current_trace
->set_flag(topt
->flags
->val
,
3716 mutex_unlock(&trace_types_lock
);
3719 topt
->flags
->val
|= topt
->opt
->bit
;
3732 static const struct file_operations trace_options_fops
= {
3733 .open
= tracing_open_generic
,
3734 .read
= trace_options_read
,
3735 .write
= trace_options_write
,
3739 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3742 long index
= (long)filp
->private_data
;
3745 if (trace_flags
& (1 << index
))
3750 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3754 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3757 long index
= (long)filp
->private_data
;
3762 if (cnt
>= sizeof(buf
))
3765 if (copy_from_user(&buf
, ubuf
, cnt
))
3770 ret
= strict_strtoul(buf
, 10, &val
);
3776 trace_flags
&= ~(1 << index
);
3779 trace_flags
|= 1 << index
;
3791 static const struct file_operations trace_options_core_fops
= {
3792 .open
= tracing_open_generic
,
3793 .read
= trace_options_core_read
,
3794 .write
= trace_options_core_write
,
3797 struct dentry
*trace_create_file(const char *name
,
3799 struct dentry
*parent
,
3801 const struct file_operations
*fops
)
3805 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
3807 pr_warning("Could not create debugfs '%s' entry\n", name
);
3813 static struct dentry
*trace_options_init_dentry(void)
3815 struct dentry
*d_tracer
;
3816 static struct dentry
*t_options
;
3821 d_tracer
= tracing_init_dentry();
3825 t_options
= debugfs_create_dir("options", d_tracer
);
3827 pr_warning("Could not create debugfs directory 'options'\n");
3835 create_trace_option_file(struct trace_option_dentry
*topt
,
3836 struct tracer_flags
*flags
,
3837 struct tracer_opt
*opt
)
3839 struct dentry
*t_options
;
3841 t_options
= trace_options_init_dentry();
3845 topt
->flags
= flags
;
3848 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
3849 &trace_options_fops
);
3853 static struct trace_option_dentry
*
3854 create_trace_option_files(struct tracer
*tracer
)
3856 struct trace_option_dentry
*topts
;
3857 struct tracer_flags
*flags
;
3858 struct tracer_opt
*opts
;
3864 flags
= tracer
->flags
;
3866 if (!flags
|| !flags
->opts
)
3871 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3874 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
3878 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3879 create_trace_option_file(&topts
[cnt
], flags
,
3886 destroy_trace_option_files(struct trace_option_dentry
*topts
)
3893 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
3894 if (topts
[cnt
].entry
)
3895 debugfs_remove(topts
[cnt
].entry
);
3901 static struct dentry
*
3902 create_trace_option_core_file(const char *option
, long index
)
3904 struct dentry
*t_options
;
3906 t_options
= trace_options_init_dentry();
3910 return trace_create_file(option
, 0644, t_options
, (void *)index
,
3911 &trace_options_core_fops
);
3914 static __init
void create_trace_options_dir(void)
3916 struct dentry
*t_options
;
3919 t_options
= trace_options_init_dentry();
3923 for (i
= 0; trace_options
[i
]; i
++)
3924 create_trace_option_core_file(trace_options
[i
], i
);
3927 static __init
int tracer_init_debugfs(void)
3929 struct dentry
*d_tracer
;
3932 d_tracer
= tracing_init_dentry();
3934 trace_create_file("tracing_enabled", 0644, d_tracer
,
3935 &global_trace
, &tracing_ctrl_fops
);
3937 trace_create_file("trace_options", 0644, d_tracer
,
3938 NULL
, &tracing_iter_fops
);
3940 trace_create_file("tracing_cpumask", 0644, d_tracer
,
3941 NULL
, &tracing_cpumask_fops
);
3943 trace_create_file("trace", 0644, d_tracer
,
3944 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
3946 trace_create_file("available_tracers", 0444, d_tracer
,
3947 &global_trace
, &show_traces_fops
);
3949 trace_create_file("current_tracer", 0444, d_tracer
,
3950 &global_trace
, &set_tracer_fops
);
3952 trace_create_file("tracing_max_latency", 0644, d_tracer
,
3953 &tracing_max_latency
, &tracing_max_lat_fops
);
3955 trace_create_file("tracing_thresh", 0644, d_tracer
,
3956 &tracing_thresh
, &tracing_max_lat_fops
);
3958 trace_create_file("README", 0644, d_tracer
,
3959 NULL
, &tracing_readme_fops
);
3961 trace_create_file("trace_pipe", 0444, d_tracer
,
3962 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
3964 trace_create_file("buffer_size_kb", 0644, d_tracer
,
3965 &global_trace
, &tracing_entries_fops
);
3967 trace_create_file("trace_marker", 0220, d_tracer
,
3968 NULL
, &tracing_mark_fops
);
3970 #ifdef CONFIG_DYNAMIC_FTRACE
3971 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
3972 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
3974 #ifdef CONFIG_SYSPROF_TRACER
3975 init_tracer_sysprof_debugfs(d_tracer
);
3978 create_trace_options_dir();
3980 for_each_tracing_cpu(cpu
)
3981 tracing_init_debugfs_percpu(cpu
);
3986 static int trace_panic_handler(struct notifier_block
*this,
3987 unsigned long event
, void *unused
)
3989 if (ftrace_dump_on_oops
)
3994 static struct notifier_block trace_panic_notifier
= {
3995 .notifier_call
= trace_panic_handler
,
3997 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
4000 static int trace_die_handler(struct notifier_block
*self
,
4006 if (ftrace_dump_on_oops
)
4015 static struct notifier_block trace_die_notifier
= {
4016 .notifier_call
= trace_die_handler
,
4021 * printk is set to max of 1024, we really don't need it that big.
4022 * Nothing should be printing 1000 characters anyway.
4024 #define TRACE_MAX_PRINT 1000
4027 * Define here KERN_TRACE so that we have one place to modify
4028 * it if we decide to change what log level the ftrace dump
4031 #define KERN_TRACE KERN_EMERG
4034 trace_printk_seq(struct trace_seq
*s
)
4036 /* Probably should print a warning here. */
4040 /* should be zero ended, but we are paranoid. */
4041 s
->buffer
[s
->len
] = 0;
4043 printk(KERN_TRACE
"%s", s
->buffer
);
4048 static void __ftrace_dump(bool disable_tracing
)
4050 static DEFINE_SPINLOCK(ftrace_dump_lock
);
4051 /* use static because iter can be a bit big for the stack */
4052 static struct trace_iterator iter
;
4053 unsigned int old_userobj
;
4054 static int dump_ran
;
4055 unsigned long flags
;
4059 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
4067 if (disable_tracing
)
4070 for_each_tracing_cpu(cpu
) {
4071 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
4074 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
4076 /* don't look at user memory in panic mode */
4077 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4079 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4081 /* Simulate the iterator */
4082 iter
.tr
= &global_trace
;
4083 iter
.trace
= current_trace
;
4084 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4087 * We need to stop all tracing on all CPUS to read the
4088 * the next buffer. This is a bit expensive, but is
4089 * not done often. We fill all what we can read,
4090 * and then release the locks again.
4093 while (!trace_empty(&iter
)) {
4096 printk(KERN_TRACE
"---------------------------------\n");
4100 /* reset all but tr, trace, and overruns */
4101 memset(&iter
.seq
, 0,
4102 sizeof(struct trace_iterator
) -
4103 offsetof(struct trace_iterator
, seq
));
4104 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4107 if (find_next_entry_inc(&iter
) != NULL
) {
4108 print_trace_line(&iter
);
4109 trace_consume(&iter
);
4112 trace_printk_seq(&iter
.seq
);
4116 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4118 printk(KERN_TRACE
"---------------------------------\n");
4120 /* Re-enable tracing if requested */
4121 if (!disable_tracing
) {
4122 trace_flags
|= old_userobj
;
4124 for_each_tracing_cpu(cpu
) {
4125 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
4131 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
4134 /* By default: disable tracing after the dump */
4135 void ftrace_dump(void)
4137 __ftrace_dump(true);
4140 __init
static int tracer_alloc_buffers(void)
4142 struct trace_array_cpu
*data
;
4147 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4150 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4151 goto out_free_buffer_mask
;
4153 if (!alloc_cpumask_var(&tracing_reader_cpumask
, GFP_KERNEL
))
4154 goto out_free_tracing_cpumask
;
4156 /* To save memory, keep the ring buffer size to its minimum */
4157 if (ring_buffer_expanded
)
4158 ring_buf_size
= trace_buf_size
;
4162 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4163 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4164 cpumask_clear(tracing_reader_cpumask
);
4166 /* TODO: make the number of buffers hot pluggable with CPUS */
4167 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
,
4168 TRACE_BUFFER_FLAGS
);
4169 if (!global_trace
.buffer
) {
4170 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4172 goto out_free_cpumask
;
4174 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4177 #ifdef CONFIG_TRACER_MAX_TRACE
4178 max_tr
.buffer
= ring_buffer_alloc(ring_buf_size
,
4179 TRACE_BUFFER_FLAGS
);
4180 if (!max_tr
.buffer
) {
4181 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4183 ring_buffer_free(global_trace
.buffer
);
4184 goto out_free_cpumask
;
4186 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
4187 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
4190 /* Allocate the first page for all buffers */
4191 for_each_tracing_cpu(i
) {
4192 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4193 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
4196 trace_init_cmdlines();
4198 register_tracer(&nop_trace
);
4199 current_trace
= &nop_trace
;
4200 #ifdef CONFIG_BOOT_TRACER
4201 register_tracer(&boot_tracer
);
4203 /* All seems OK, enable tracing */
4204 tracing_disabled
= 0;
4206 atomic_notifier_chain_register(&panic_notifier_list
,
4207 &trace_panic_notifier
);
4209 register_die_notifier(&trace_die_notifier
);
4214 free_cpumask_var(tracing_reader_cpumask
);
4215 out_free_tracing_cpumask
:
4216 free_cpumask_var(tracing_cpumask
);
4217 out_free_buffer_mask
:
4218 free_cpumask_var(tracing_buffer_mask
);
4223 __init
static int clear_boot_tracer(void)
4226 * The default tracer at boot buffer is an init section.
4227 * This function is called in lateinit. If we did not
4228 * find the boot tracer, then clear it out, to prevent
4229 * later registration from accessing the buffer that is
4230 * about to be freed.
4232 if (!default_bootup_tracer
)
4235 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4236 default_bootup_tracer
);
4237 default_bootup_tracer
= NULL
;
4242 early_initcall(tracer_alloc_buffers
);
4243 fs_initcall(tracer_init_debugfs
);
4244 late_initcall(clear_boot_tracer
);