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/smp_lock.h>
21 #include <linux/notifier.h>
22 #include <linux/irqflags.h>
23 #include <linux/debugfs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/kprobes.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/ctype.h>
36 #include <linux/init.h>
37 #include <linux/poll.h>
38 #include <linux/gfp.h>
42 #include "trace_output.h"
44 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
47 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
50 int ring_buffer_expanded
;
53 * We need to change this state when a selftest is running.
54 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
56 * insertions into the ring-buffer such as trace_printk could occurred
57 * at the same time, giving false positive or negative results.
59 static bool __read_mostly tracing_selftest_running
;
62 * If a tracer is running, we do not want to run SELFTEST.
64 bool __read_mostly tracing_selftest_disabled
;
66 /* For tracers that don't implement custom flags */
67 static struct tracer_opt dummy_tracer_opt
[] = {
71 static struct tracer_flags dummy_tracer_flags
= {
73 .opts
= dummy_tracer_opt
76 static int dummy_set_flag(u32 old_flags
, u32 bit
, int set
)
82 * Kill all tracing for good (never come back).
83 * It is initialized to 1 but will turn to zero if the initialization
84 * of the tracer is successful. But that is the only place that sets
87 static int tracing_disabled
= 1;
89 DEFINE_PER_CPU(local_t
, ftrace_cpu_disabled
);
91 static inline void ftrace_disable_cpu(void)
94 local_inc(&__get_cpu_var(ftrace_cpu_disabled
));
97 static inline void ftrace_enable_cpu(void)
99 local_dec(&__get_cpu_var(ftrace_cpu_disabled
));
103 static cpumask_var_t __read_mostly tracing_buffer_mask
;
105 /* Define which cpu buffers are currently read in trace_pipe */
106 static cpumask_var_t tracing_reader_cpumask
;
108 #define for_each_tracing_cpu(cpu) \
109 for_each_cpu(cpu, tracing_buffer_mask)
112 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
114 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
115 * is set, then ftrace_dump is called. This will output the contents
116 * of the ftrace buffers to the console. This is very useful for
117 * capturing traces that lead to crashes and outputing it to a
120 * It is default off, but you can enable it with either specifying
121 * "ftrace_dump_on_oops" in the kernel command line, or setting
122 * /proc/sys/kernel/ftrace_dump_on_oops to true.
124 int ftrace_dump_on_oops
;
126 static int tracing_set_tracer(const char *buf
);
128 #define BOOTUP_TRACER_SIZE 100
129 static char bootup_tracer_buf
[BOOTUP_TRACER_SIZE
] __initdata
;
130 static char *default_bootup_tracer
;
132 static int __init
set_ftrace(char *str
)
134 strncpy(bootup_tracer_buf
, str
, BOOTUP_TRACER_SIZE
);
135 default_bootup_tracer
= bootup_tracer_buf
;
136 /* We are using ftrace early, expand it */
137 ring_buffer_expanded
= 1;
140 __setup("ftrace=", set_ftrace
);
142 static int __init
set_ftrace_dump_on_oops(char *str
)
144 ftrace_dump_on_oops
= 1;
147 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
149 unsigned long long ns2usecs(cycle_t nsec
)
157 * The global_trace is the descriptor that holds the tracing
158 * buffers for the live tracing. For each CPU, it contains
159 * a link list of pages that will store trace entries. The
160 * page descriptor of the pages in the memory is used to hold
161 * the link list by linking the lru item in the page descriptor
162 * to each of the pages in the buffer per CPU.
164 * For each active CPU there is a data field that holds the
165 * pages for the buffer for that CPU. Each CPU has the same number
166 * of pages allocated for its buffer.
168 static struct trace_array global_trace
;
170 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
172 int filter_current_check_discard(struct ring_buffer
*buffer
,
173 struct ftrace_event_call
*call
, void *rec
,
174 struct ring_buffer_event
*event
)
176 return filter_check_discard(call
, rec
, buffer
, event
);
178 EXPORT_SYMBOL_GPL(filter_current_check_discard
);
180 cycle_t
ftrace_now(int cpu
)
184 /* Early boot up does not have a buffer yet */
185 if (!global_trace
.buffer
)
186 return trace_clock_local();
188 ts
= ring_buffer_time_stamp(global_trace
.buffer
, cpu
);
189 ring_buffer_normalize_time_stamp(global_trace
.buffer
, cpu
, &ts
);
195 * The max_tr is used to snapshot the global_trace when a maximum
196 * latency is reached. Some tracers will use this to store a maximum
197 * trace while it continues examining live traces.
199 * The buffers for the max_tr are set up the same as the global_trace.
200 * When a snapshot is taken, the link list of the max_tr is swapped
201 * with the link list of the global_trace and the buffers are reset for
202 * the global_trace so the tracing can continue.
204 static struct trace_array max_tr
;
206 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
208 /* tracer_enabled is used to toggle activation of a tracer */
209 static int tracer_enabled
= 1;
212 * tracing_is_enabled - return tracer_enabled status
214 * This function is used by other tracers to know the status
215 * of the tracer_enabled flag. Tracers may use this function
216 * to know if it should enable their features when starting
217 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
219 int tracing_is_enabled(void)
221 return tracer_enabled
;
225 * trace_buf_size is the size in bytes that is allocated
226 * for a buffer. Note, the number of bytes is always rounded
229 * This number is purposely set to a low number of 16384.
230 * If the dump on oops happens, it will be much appreciated
231 * to not have to wait for all that output. Anyway this can be
232 * boot time and run time configurable.
234 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
236 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
238 /* trace_types holds a link list of available tracers. */
239 static struct tracer
*trace_types __read_mostly
;
241 /* current_trace points to the tracer that is currently active */
242 static struct tracer
*current_trace __read_mostly
;
245 * max_tracer_type_len is used to simplify the allocating of
246 * buffers to read userspace tracer names. We keep track of
247 * the longest tracer name registered.
249 static int max_tracer_type_len
;
252 * trace_types_lock is used to protect the trace_types list.
253 * This lock is also used to keep user access serialized.
254 * Accesses from userspace will grab this lock while userspace
255 * activities happen inside the kernel.
257 static DEFINE_MUTEX(trace_types_lock
);
259 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
260 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
262 /* trace_flags holds trace_options default values */
263 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
264 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
| TRACE_ITER_SLEEP_TIME
|
265 TRACE_ITER_GRAPH_TIME
;
267 static int trace_stop_count
;
268 static DEFINE_SPINLOCK(tracing_start_lock
);
271 * trace_wake_up - wake up tasks waiting for trace input
273 * Simply wakes up any task that is blocked on the trace_wait
274 * queue. These is used with trace_poll for tasks polling the trace.
276 void trace_wake_up(void)
279 * The runqueue_is_locked() can fail, but this is the best we
282 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
283 wake_up(&trace_wait
);
286 static int __init
set_buf_size(char *str
)
288 unsigned long buf_size
;
292 buf_size
= memparse(str
, &str
);
293 /* nr_entries can not be zero */
296 trace_buf_size
= buf_size
;
299 __setup("trace_buf_size=", set_buf_size
);
301 unsigned long nsecs_to_usecs(unsigned long nsecs
)
306 /* These must match the bit postions in trace_iterator_flags */
307 static const char *trace_options
[] = {
336 { trace_clock_local
, "local" },
337 { trace_clock_global
, "global" },
342 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
350 if (s
->len
<= s
->readpos
)
353 len
= s
->len
- s
->readpos
;
356 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
366 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
371 if (s
->len
<= s
->readpos
)
374 len
= s
->len
- s
->readpos
;
377 ret
= memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
386 * ftrace_max_lock is used to protect the swapping of buffers
387 * when taking a max snapshot. The buffers themselves are
388 * protected by per_cpu spinlocks. But the action of the swap
389 * needs its own lock.
391 * This is defined as a raw_spinlock_t in order to help
392 * with performance when lockdep debugging is enabled.
394 * It is also used in other places outside the update_max_tr
395 * so it needs to be defined outside of the
396 * CONFIG_TRACER_MAX_TRACE.
398 static raw_spinlock_t ftrace_max_lock
=
399 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
401 #ifdef CONFIG_TRACER_MAX_TRACE
402 unsigned long __read_mostly tracing_max_latency
;
403 unsigned long __read_mostly tracing_thresh
;
406 * Copy the new maximum trace into the separate maximum-trace
407 * structure. (this way the maximum trace is permanently saved,
408 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
411 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
413 struct trace_array_cpu
*data
= tr
->data
[cpu
];
414 struct trace_array_cpu
*max_data
= tr
->data
[cpu
];
417 max_tr
.time_start
= data
->preempt_timestamp
;
419 max_data
= max_tr
.data
[cpu
];
420 max_data
->saved_latency
= tracing_max_latency
;
421 max_data
->critical_start
= data
->critical_start
;
422 max_data
->critical_end
= data
->critical_end
;
424 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
425 max_data
->pid
= tsk
->pid
;
426 max_data
->uid
= task_uid(tsk
);
427 max_data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
428 max_data
->policy
= tsk
->policy
;
429 max_data
->rt_priority
= tsk
->rt_priority
;
431 /* record this tasks comm */
432 tracing_record_cmdline(tsk
);
436 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
438 * @tsk: the task with the latency
439 * @cpu: The cpu that initiated the trace.
441 * Flip the buffers between the @tr and the max_tr and record information
442 * about which task was the cause of this latency.
445 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
447 struct ring_buffer
*buf
= tr
->buffer
;
449 if (trace_stop_count
)
452 WARN_ON_ONCE(!irqs_disabled());
453 __raw_spin_lock(&ftrace_max_lock
);
455 tr
->buffer
= max_tr
.buffer
;
458 __update_max_tr(tr
, tsk
, cpu
);
459 __raw_spin_unlock(&ftrace_max_lock
);
463 * update_max_tr_single - only copy one trace over, and reset the rest
465 * @tsk - task with the latency
466 * @cpu - the cpu of the buffer to copy.
468 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
471 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
475 if (trace_stop_count
)
478 WARN_ON_ONCE(!irqs_disabled());
479 __raw_spin_lock(&ftrace_max_lock
);
481 ftrace_disable_cpu();
483 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
487 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
);
489 __update_max_tr(tr
, tsk
, cpu
);
490 __raw_spin_unlock(&ftrace_max_lock
);
492 #endif /* CONFIG_TRACER_MAX_TRACE */
495 * register_tracer - register a tracer with the ftrace system.
496 * @type - the plugin for the tracer
498 * Register a new plugin tracer.
500 int register_tracer(struct tracer
*type
)
501 __releases(kernel_lock
)
502 __acquires(kernel_lock
)
509 pr_info("Tracer must have a name\n");
514 * When this gets called we hold the BKL which means that
515 * preemption is disabled. Various trace selftests however
516 * need to disable and enable preemption for successful tests.
517 * So we drop the BKL here and grab it after the tests again.
520 mutex_lock(&trace_types_lock
);
522 tracing_selftest_running
= true;
524 for (t
= trace_types
; t
; t
= t
->next
) {
525 if (strcmp(type
->name
, t
->name
) == 0) {
527 pr_info("Trace %s already registered\n",
535 type
->set_flag
= &dummy_set_flag
;
537 type
->flags
= &dummy_tracer_flags
;
539 if (!type
->flags
->opts
)
540 type
->flags
->opts
= dummy_tracer_opt
;
541 if (!type
->wait_pipe
)
542 type
->wait_pipe
= default_wait_pipe
;
545 #ifdef CONFIG_FTRACE_STARTUP_TEST
546 if (type
->selftest
&& !tracing_selftest_disabled
) {
547 struct tracer
*saved_tracer
= current_trace
;
548 struct trace_array
*tr
= &global_trace
;
551 * Run a selftest on this tracer.
552 * Here we reset the trace buffer, and set the current
553 * tracer to be this tracer. The tracer can then run some
554 * internal tracing to verify that everything is in order.
555 * If we fail, we do not register this tracer.
557 tracing_reset_online_cpus(tr
);
559 current_trace
= type
;
560 /* the test is responsible for initializing and enabling */
561 pr_info("Testing tracer %s: ", type
->name
);
562 ret
= type
->selftest(type
, tr
);
563 /* the test is responsible for resetting too */
564 current_trace
= saved_tracer
;
566 printk(KERN_CONT
"FAILED!\n");
569 /* Only reset on passing, to avoid touching corrupted buffers */
570 tracing_reset_online_cpus(tr
);
572 printk(KERN_CONT
"PASSED\n");
576 type
->next
= trace_types
;
578 len
= strlen(type
->name
);
579 if (len
> max_tracer_type_len
)
580 max_tracer_type_len
= len
;
583 tracing_selftest_running
= false;
584 mutex_unlock(&trace_types_lock
);
586 if (ret
|| !default_bootup_tracer
)
589 if (strncmp(default_bootup_tracer
, type
->name
, BOOTUP_TRACER_SIZE
))
592 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
593 /* Do we want this tracer to start on bootup? */
594 tracing_set_tracer(type
->name
);
595 default_bootup_tracer
= NULL
;
596 /* disable other selftests, since this will break it. */
597 tracing_selftest_disabled
= 1;
598 #ifdef CONFIG_FTRACE_STARTUP_TEST
599 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
608 void unregister_tracer(struct tracer
*type
)
613 mutex_lock(&trace_types_lock
);
614 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
618 pr_info("Trace %s not registered\n", type
->name
);
624 if (type
== current_trace
&& tracer_enabled
) {
627 if (current_trace
->stop
)
628 current_trace
->stop(&global_trace
);
629 current_trace
= &nop_trace
;
632 if (strlen(type
->name
) != max_tracer_type_len
)
635 max_tracer_type_len
= 0;
636 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
637 len
= strlen((*t
)->name
);
638 if (len
> max_tracer_type_len
)
639 max_tracer_type_len
= len
;
642 mutex_unlock(&trace_types_lock
);
645 static void __tracing_reset(struct trace_array
*tr
, int cpu
)
647 ftrace_disable_cpu();
648 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
652 void tracing_reset(struct trace_array
*tr
, int cpu
)
654 struct ring_buffer
*buffer
= tr
->buffer
;
656 ring_buffer_record_disable(buffer
);
658 /* Make sure all commits have finished */
660 __tracing_reset(tr
, cpu
);
662 ring_buffer_record_enable(buffer
);
665 void tracing_reset_online_cpus(struct trace_array
*tr
)
667 struct ring_buffer
*buffer
= tr
->buffer
;
670 ring_buffer_record_disable(buffer
);
672 /* Make sure all commits have finished */
675 tr
->time_start
= ftrace_now(tr
->cpu
);
677 for_each_online_cpu(cpu
)
678 __tracing_reset(tr
, cpu
);
680 ring_buffer_record_enable(buffer
);
683 void tracing_reset_current(int cpu
)
685 tracing_reset(&global_trace
, cpu
);
688 void tracing_reset_current_online_cpus(void)
690 tracing_reset_online_cpus(&global_trace
);
693 #define SAVED_CMDLINES 128
694 #define NO_CMDLINE_MAP UINT_MAX
695 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
696 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
697 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
698 static int cmdline_idx
;
699 static raw_spinlock_t trace_cmdline_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
701 /* temporary disable recording */
702 static atomic_t trace_record_cmdline_disabled __read_mostly
;
704 static void trace_init_cmdlines(void)
706 memset(&map_pid_to_cmdline
, NO_CMDLINE_MAP
, sizeof(map_pid_to_cmdline
));
707 memset(&map_cmdline_to_pid
, NO_CMDLINE_MAP
, sizeof(map_cmdline_to_pid
));
712 * ftrace_off_permanent - disable all ftrace code permanently
714 * This should only be called when a serious anomally has
715 * been detected. This will turn off the function tracing,
716 * ring buffers, and other tracing utilites. It takes no
717 * locks and can be called from any context.
719 void ftrace_off_permanent(void)
721 tracing_disabled
= 1;
723 tracing_off_permanent();
727 * tracing_start - quick start of the tracer
729 * If tracing is enabled but was stopped by tracing_stop,
730 * this will start the tracer back up.
732 void tracing_start(void)
734 struct ring_buffer
*buffer
;
737 if (tracing_disabled
)
740 spin_lock_irqsave(&tracing_start_lock
, flags
);
741 if (--trace_stop_count
) {
742 if (trace_stop_count
< 0) {
743 /* Someone screwed up their debugging */
745 trace_stop_count
= 0;
751 buffer
= global_trace
.buffer
;
753 ring_buffer_record_enable(buffer
);
755 buffer
= max_tr
.buffer
;
757 ring_buffer_record_enable(buffer
);
761 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
765 * tracing_stop - quick stop of the tracer
767 * Light weight way to stop tracing. Use in conjunction with
770 void tracing_stop(void)
772 struct ring_buffer
*buffer
;
776 spin_lock_irqsave(&tracing_start_lock
, flags
);
777 if (trace_stop_count
++)
780 buffer
= global_trace
.buffer
;
782 ring_buffer_record_disable(buffer
);
784 buffer
= max_tr
.buffer
;
786 ring_buffer_record_disable(buffer
);
789 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
792 void trace_stop_cmdline_recording(void);
794 static void trace_save_cmdline(struct task_struct
*tsk
)
798 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
802 * It's not the end of the world if we don't get
803 * the lock, but we also don't want to spin
804 * nor do we want to disable interrupts,
805 * so if we miss here, then better luck next time.
807 if (!__raw_spin_trylock(&trace_cmdline_lock
))
810 idx
= map_pid_to_cmdline
[tsk
->pid
];
811 if (idx
== NO_CMDLINE_MAP
) {
812 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
815 * Check whether the cmdline buffer at idx has a pid
816 * mapped. We are going to overwrite that entry so we
817 * need to clear the map_pid_to_cmdline. Otherwise we
818 * would read the new comm for the old pid.
820 pid
= map_cmdline_to_pid
[idx
];
821 if (pid
!= NO_CMDLINE_MAP
)
822 map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
824 map_cmdline_to_pid
[idx
] = tsk
->pid
;
825 map_pid_to_cmdline
[tsk
->pid
] = idx
;
830 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
832 __raw_spin_unlock(&trace_cmdline_lock
);
835 void trace_find_cmdline(int pid
, char comm
[])
840 strcpy(comm
, "<idle>");
844 if (pid
> PID_MAX_DEFAULT
) {
845 strcpy(comm
, "<...>");
850 __raw_spin_lock(&trace_cmdline_lock
);
851 map
= map_pid_to_cmdline
[pid
];
852 if (map
!= NO_CMDLINE_MAP
)
853 strcpy(comm
, saved_cmdlines
[map
]);
855 strcpy(comm
, "<...>");
857 __raw_spin_unlock(&trace_cmdline_lock
);
861 void tracing_record_cmdline(struct task_struct
*tsk
)
863 if (atomic_read(&trace_record_cmdline_disabled
) || !tracer_enabled
||
867 trace_save_cmdline(tsk
);
871 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
874 struct task_struct
*tsk
= current
;
876 entry
->preempt_count
= pc
& 0xff;
877 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
878 entry
->tgid
= (tsk
) ? tsk
->tgid
: 0;
880 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
881 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
883 TRACE_FLAG_IRQS_NOSUPPORT
|
885 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
886 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
887 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
889 EXPORT_SYMBOL_GPL(tracing_generic_entry_update
);
891 struct ring_buffer_event
*
892 trace_buffer_lock_reserve(struct ring_buffer
*buffer
,
895 unsigned long flags
, int pc
)
897 struct ring_buffer_event
*event
;
899 event
= ring_buffer_lock_reserve(buffer
, len
);
901 struct trace_entry
*ent
= ring_buffer_event_data(event
);
903 tracing_generic_entry_update(ent
, flags
, pc
);
911 __trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
912 struct ring_buffer_event
*event
,
913 unsigned long flags
, int pc
,
916 ring_buffer_unlock_commit(buffer
, event
);
918 ftrace_trace_stack(buffer
, flags
, 6, pc
);
919 ftrace_trace_userstack(buffer
, flags
, pc
);
925 void trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
926 struct ring_buffer_event
*event
,
927 unsigned long flags
, int pc
)
929 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
932 struct ring_buffer_event
*
933 trace_current_buffer_lock_reserve(struct ring_buffer
**current_rb
,
934 int type
, unsigned long len
,
935 unsigned long flags
, int pc
)
937 *current_rb
= global_trace
.buffer
;
938 return trace_buffer_lock_reserve(*current_rb
,
939 type
, len
, flags
, pc
);
941 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve
);
943 void trace_current_buffer_unlock_commit(struct ring_buffer
*buffer
,
944 struct ring_buffer_event
*event
,
945 unsigned long flags
, int pc
)
947 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
949 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit
);
951 void trace_nowake_buffer_unlock_commit(struct ring_buffer
*buffer
,
952 struct ring_buffer_event
*event
,
953 unsigned long flags
, int pc
)
955 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 0);
957 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit
);
959 void trace_current_buffer_discard_commit(struct ring_buffer
*buffer
,
960 struct ring_buffer_event
*event
)
962 ring_buffer_discard_commit(buffer
, event
);
964 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
967 trace_function(struct trace_array
*tr
,
968 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
971 struct ftrace_event_call
*call
= &event_function
;
972 struct ring_buffer
*buffer
= tr
->buffer
;
973 struct ring_buffer_event
*event
;
974 struct ftrace_entry
*entry
;
976 /* If we are reading the ring buffer, don't trace */
977 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
980 event
= trace_buffer_lock_reserve(buffer
, TRACE_FN
, sizeof(*entry
),
984 entry
= ring_buffer_event_data(event
);
986 entry
->parent_ip
= parent_ip
;
988 if (!filter_check_discard(call
, entry
, buffer
, event
))
989 ring_buffer_unlock_commit(buffer
, event
);
993 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
994 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
997 if (likely(!atomic_read(&data
->disabled
)))
998 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
1001 #ifdef CONFIG_STACKTRACE
1002 static void __ftrace_trace_stack(struct ring_buffer
*buffer
,
1003 unsigned long flags
,
1006 struct ftrace_event_call
*call
= &event_kernel_stack
;
1007 struct ring_buffer_event
*event
;
1008 struct stack_entry
*entry
;
1009 struct stack_trace trace
;
1011 event
= trace_buffer_lock_reserve(buffer
, TRACE_STACK
,
1012 sizeof(*entry
), flags
, pc
);
1015 entry
= ring_buffer_event_data(event
);
1016 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1018 trace
.nr_entries
= 0;
1019 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1021 trace
.entries
= entry
->caller
;
1023 save_stack_trace(&trace
);
1024 if (!filter_check_discard(call
, entry
, buffer
, event
))
1025 ring_buffer_unlock_commit(buffer
, event
);
1028 void ftrace_trace_stack(struct ring_buffer
*buffer
, unsigned long flags
,
1031 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1034 __ftrace_trace_stack(buffer
, flags
, skip
, pc
);
1037 void __trace_stack(struct trace_array
*tr
, unsigned long flags
, int skip
,
1040 __ftrace_trace_stack(tr
->buffer
, flags
, skip
, pc
);
1044 ftrace_trace_userstack(struct ring_buffer
*buffer
, unsigned long flags
, int pc
)
1046 struct ftrace_event_call
*call
= &event_user_stack
;
1047 struct ring_buffer_event
*event
;
1048 struct userstack_entry
*entry
;
1049 struct stack_trace trace
;
1051 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1054 event
= trace_buffer_lock_reserve(buffer
, TRACE_USER_STACK
,
1055 sizeof(*entry
), flags
, pc
);
1058 entry
= ring_buffer_event_data(event
);
1060 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1062 trace
.nr_entries
= 0;
1063 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1065 trace
.entries
= entry
->caller
;
1067 save_stack_trace_user(&trace
);
1068 if (!filter_check_discard(call
, entry
, buffer
, event
))
1069 ring_buffer_unlock_commit(buffer
, event
);
1073 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1075 ftrace_trace_userstack(tr
, flags
, preempt_count());
1079 #endif /* CONFIG_STACKTRACE */
1082 ftrace_trace_special(void *__tr
,
1083 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1086 struct ring_buffer_event
*event
;
1087 struct trace_array
*tr
= __tr
;
1088 struct ring_buffer
*buffer
= tr
->buffer
;
1089 struct special_entry
*entry
;
1091 event
= trace_buffer_lock_reserve(buffer
, TRACE_SPECIAL
,
1092 sizeof(*entry
), 0, pc
);
1095 entry
= ring_buffer_event_data(event
);
1099 trace_buffer_unlock_commit(buffer
, event
, 0, pc
);
1103 __trace_special(void *__tr
, void *__data
,
1104 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1106 ftrace_trace_special(__tr
, arg1
, arg2
, arg3
, preempt_count());
1110 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1112 struct trace_array
*tr
= &global_trace
;
1113 struct trace_array_cpu
*data
;
1114 unsigned long flags
;
1118 if (tracing_disabled
)
1121 pc
= preempt_count();
1122 local_irq_save(flags
);
1123 cpu
= raw_smp_processor_id();
1124 data
= tr
->data
[cpu
];
1126 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1127 ftrace_trace_special(tr
, arg1
, arg2
, arg3
, pc
);
1129 atomic_dec(&data
->disabled
);
1130 local_irq_restore(flags
);
1134 * trace_vbprintk - write binary msg to tracing buffer
1137 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1139 static raw_spinlock_t trace_buf_lock
=
1140 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
1141 static u32 trace_buf
[TRACE_BUF_SIZE
];
1143 struct ftrace_event_call
*call
= &event_bprint
;
1144 struct ring_buffer_event
*event
;
1145 struct ring_buffer
*buffer
;
1146 struct trace_array
*tr
= &global_trace
;
1147 struct trace_array_cpu
*data
;
1148 struct bprint_entry
*entry
;
1149 unsigned long flags
;
1152 int cpu
, len
= 0, size
, pc
;
1154 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1157 /* Don't pollute graph traces with trace_vprintk internals */
1158 pause_graph_tracing();
1160 pc
= preempt_count();
1161 resched
= ftrace_preempt_disable();
1162 cpu
= raw_smp_processor_id();
1163 data
= tr
->data
[cpu
];
1165 disable
= atomic_inc_return(&data
->disabled
);
1166 if (unlikely(disable
!= 1))
1169 /* Lockdep uses trace_printk for lock tracing */
1170 local_irq_save(flags
);
1171 __raw_spin_lock(&trace_buf_lock
);
1172 len
= vbin_printf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1174 if (len
> TRACE_BUF_SIZE
|| len
< 0)
1177 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1178 buffer
= tr
->buffer
;
1179 event
= trace_buffer_lock_reserve(buffer
, TRACE_BPRINT
, size
,
1183 entry
= ring_buffer_event_data(event
);
1187 memcpy(entry
->buf
, trace_buf
, sizeof(u32
) * len
);
1188 if (!filter_check_discard(call
, entry
, buffer
, event
))
1189 ring_buffer_unlock_commit(buffer
, event
);
1192 __raw_spin_unlock(&trace_buf_lock
);
1193 local_irq_restore(flags
);
1196 atomic_dec_return(&data
->disabled
);
1197 ftrace_preempt_enable(resched
);
1198 unpause_graph_tracing();
1202 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1204 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1206 static raw_spinlock_t trace_buf_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
1207 static char trace_buf
[TRACE_BUF_SIZE
];
1209 struct ftrace_event_call
*call
= &event_print
;
1210 struct ring_buffer_event
*event
;
1211 struct ring_buffer
*buffer
;
1212 struct trace_array
*tr
= &global_trace
;
1213 struct trace_array_cpu
*data
;
1214 int cpu
, len
= 0, size
, pc
;
1215 struct print_entry
*entry
;
1216 unsigned long irq_flags
;
1219 if (tracing_disabled
|| tracing_selftest_running
)
1222 pc
= preempt_count();
1223 preempt_disable_notrace();
1224 cpu
= raw_smp_processor_id();
1225 data
= tr
->data
[cpu
];
1227 disable
= atomic_inc_return(&data
->disabled
);
1228 if (unlikely(disable
!= 1))
1231 pause_graph_tracing();
1232 raw_local_irq_save(irq_flags
);
1233 __raw_spin_lock(&trace_buf_lock
);
1234 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1236 len
= min(len
, TRACE_BUF_SIZE
-1);
1239 size
= sizeof(*entry
) + len
+ 1;
1240 buffer
= tr
->buffer
;
1241 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
1245 entry
= ring_buffer_event_data(event
);
1248 memcpy(&entry
->buf
, trace_buf
, len
);
1249 entry
->buf
[len
] = 0;
1250 if (!filter_check_discard(call
, entry
, buffer
, event
))
1251 ring_buffer_unlock_commit(buffer
, event
);
1254 __raw_spin_unlock(&trace_buf_lock
);
1255 raw_local_irq_restore(irq_flags
);
1256 unpause_graph_tracing();
1258 atomic_dec_return(&data
->disabled
);
1259 preempt_enable_notrace();
1263 EXPORT_SYMBOL_GPL(trace_vprintk
);
1265 enum trace_file_type
{
1266 TRACE_FILE_LAT_FMT
= 1,
1267 TRACE_FILE_ANNOTATE
= 2,
1270 static void trace_iterator_increment(struct trace_iterator
*iter
)
1272 /* Don't allow ftrace to trace into the ring buffers */
1273 ftrace_disable_cpu();
1276 if (iter
->buffer_iter
[iter
->cpu
])
1277 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1279 ftrace_enable_cpu();
1282 static struct trace_entry
*
1283 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1285 struct ring_buffer_event
*event
;
1286 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1288 /* Don't allow ftrace to trace into the ring buffers */
1289 ftrace_disable_cpu();
1292 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1294 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1296 ftrace_enable_cpu();
1298 return event
? ring_buffer_event_data(event
) : NULL
;
1301 static struct trace_entry
*
1302 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1304 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1305 struct trace_entry
*ent
, *next
= NULL
;
1306 int cpu_file
= iter
->cpu_file
;
1307 u64 next_ts
= 0, ts
;
1312 * If we are in a per_cpu trace file, don't bother by iterating over
1313 * all cpu and peek directly.
1315 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1316 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1318 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
);
1320 *ent_cpu
= cpu_file
;
1325 for_each_tracing_cpu(cpu
) {
1327 if (ring_buffer_empty_cpu(buffer
, cpu
))
1330 ent
= peek_next_entry(iter
, cpu
, &ts
);
1333 * Pick the entry with the smallest timestamp:
1335 if (ent
&& (!next
|| ts
< next_ts
)) {
1343 *ent_cpu
= next_cpu
;
1351 /* Find the next real entry, without updating the iterator itself */
1352 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1353 int *ent_cpu
, u64
*ent_ts
)
1355 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1358 /* Find the next real entry, and increment the iterator to the next entry */
1359 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1361 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1364 trace_iterator_increment(iter
);
1366 return iter
->ent
? iter
: NULL
;
1369 static void trace_consume(struct trace_iterator
*iter
)
1371 /* Don't allow ftrace to trace into the ring buffers */
1372 ftrace_disable_cpu();
1373 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1374 ftrace_enable_cpu();
1377 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1379 struct trace_iterator
*iter
= m
->private;
1385 /* can't go backwards */
1390 ent
= find_next_entry_inc(iter
);
1394 while (ent
&& iter
->idx
< i
)
1395 ent
= find_next_entry_inc(iter
);
1402 static void tracing_iter_reset(struct trace_iterator
*iter
, int cpu
)
1404 struct trace_array
*tr
= iter
->tr
;
1405 struct ring_buffer_event
*event
;
1406 struct ring_buffer_iter
*buf_iter
;
1407 unsigned long entries
= 0;
1410 tr
->data
[cpu
]->skipped_entries
= 0;
1412 if (!iter
->buffer_iter
[cpu
])
1415 buf_iter
= iter
->buffer_iter
[cpu
];
1416 ring_buffer_iter_reset(buf_iter
);
1419 * We could have the case with the max latency tracers
1420 * that a reset never took place on a cpu. This is evident
1421 * by the timestamp being before the start of the buffer.
1423 while ((event
= ring_buffer_iter_peek(buf_iter
, &ts
))) {
1424 if (ts
>= iter
->tr
->time_start
)
1427 ring_buffer_read(buf_iter
, NULL
);
1430 tr
->data
[cpu
]->skipped_entries
= entries
;
1434 * No necessary locking here. The worst thing which can
1435 * happen is loosing events consumed at the same time
1436 * by a trace_pipe reader.
1437 * Other than that, we don't risk to crash the ring buffer
1438 * because it serializes the readers.
1440 * The current tracer is copied to avoid a global locking
1443 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1445 struct trace_iterator
*iter
= m
->private;
1446 static struct tracer
*old_tracer
;
1447 int cpu_file
= iter
->cpu_file
;
1452 /* copy the tracer to avoid using a global lock all around */
1453 mutex_lock(&trace_types_lock
);
1454 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1455 old_tracer
= current_trace
;
1456 *iter
->trace
= *current_trace
;
1458 mutex_unlock(&trace_types_lock
);
1460 atomic_inc(&trace_record_cmdline_disabled
);
1462 if (*pos
!= iter
->pos
) {
1467 ftrace_disable_cpu();
1469 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1470 for_each_tracing_cpu(cpu
)
1471 tracing_iter_reset(iter
, cpu
);
1473 tracing_iter_reset(iter
, cpu_file
);
1475 ftrace_enable_cpu();
1477 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1482 p
= s_next(m
, p
, &l
);
1485 trace_event_read_lock();
1489 static void s_stop(struct seq_file
*m
, void *p
)
1491 atomic_dec(&trace_record_cmdline_disabled
);
1492 trace_event_read_unlock();
1495 static void print_lat_help_header(struct seq_file
*m
)
1497 seq_puts(m
, "# _------=> CPU# \n");
1498 seq_puts(m
, "# / _-----=> irqs-off \n");
1499 seq_puts(m
, "# | / _----=> need-resched \n");
1500 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1501 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1502 seq_puts(m
, "# |||| / \n");
1503 seq_puts(m
, "# ||||| delay \n");
1504 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1505 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1508 static void print_func_help_header(struct seq_file
*m
)
1510 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1511 seq_puts(m
, "# | | | | |\n");
1516 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1518 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1519 struct trace_array
*tr
= iter
->tr
;
1520 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1521 struct tracer
*type
= current_trace
;
1522 unsigned long entries
= 0;
1523 unsigned long total
= 0;
1524 unsigned long count
;
1525 const char *name
= "preemption";
1532 for_each_tracing_cpu(cpu
) {
1533 count
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
1535 * If this buffer has skipped entries, then we hold all
1536 * entries for the trace and we need to ignore the
1537 * ones before the time stamp.
1539 if (tr
->data
[cpu
]->skipped_entries
) {
1540 count
-= tr
->data
[cpu
]->skipped_entries
;
1541 /* total is the same as the entries */
1545 ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
1549 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
1551 seq_puts(m
, "# -----------------------------------"
1552 "---------------------------------\n");
1553 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1554 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1555 nsecs_to_usecs(data
->saved_latency
),
1559 #if defined(CONFIG_PREEMPT_NONE)
1561 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1563 #elif defined(CONFIG_PREEMPT)
1568 /* These are reserved for later use */
1571 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1575 seq_puts(m
, "# -----------------\n");
1576 seq_printf(m
, "# | task: %.16s-%d "
1577 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1578 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1579 data
->policy
, data
->rt_priority
);
1580 seq_puts(m
, "# -----------------\n");
1582 if (data
->critical_start
) {
1583 seq_puts(m
, "# => started at: ");
1584 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1585 trace_print_seq(m
, &iter
->seq
);
1586 seq_puts(m
, "\n# => ended at: ");
1587 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1588 trace_print_seq(m
, &iter
->seq
);
1589 seq_puts(m
, "\n#\n");
1595 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1597 struct trace_seq
*s
= &iter
->seq
;
1599 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1602 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1605 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1608 if (iter
->tr
->data
[iter
->cpu
]->skipped_entries
)
1611 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1613 /* Don't print started cpu buffer for the first entry of the trace */
1615 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
1619 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1621 struct trace_seq
*s
= &iter
->seq
;
1622 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1623 struct trace_entry
*entry
;
1624 struct trace_event
*event
;
1628 test_cpu_buff_start(iter
);
1630 event
= ftrace_find_event(entry
->type
);
1632 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1633 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1634 if (!trace_print_lat_context(iter
))
1637 if (!trace_print_context(iter
))
1643 return event
->trace(iter
, sym_flags
);
1645 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1648 return TRACE_TYPE_HANDLED
;
1650 return TRACE_TYPE_PARTIAL_LINE
;
1653 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1655 struct trace_seq
*s
= &iter
->seq
;
1656 struct trace_entry
*entry
;
1657 struct trace_event
*event
;
1661 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1662 if (!trace_seq_printf(s
, "%d %d %llu ",
1663 entry
->pid
, iter
->cpu
, iter
->ts
))
1667 event
= ftrace_find_event(entry
->type
);
1669 return event
->raw(iter
, 0);
1671 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1674 return TRACE_TYPE_HANDLED
;
1676 return TRACE_TYPE_PARTIAL_LINE
;
1679 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1681 struct trace_seq
*s
= &iter
->seq
;
1682 unsigned char newline
= '\n';
1683 struct trace_entry
*entry
;
1684 struct trace_event
*event
;
1688 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1689 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1690 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1691 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1694 event
= ftrace_find_event(entry
->type
);
1696 enum print_line_t ret
= event
->hex(iter
, 0);
1697 if (ret
!= TRACE_TYPE_HANDLED
)
1701 SEQ_PUT_FIELD_RET(s
, newline
);
1703 return TRACE_TYPE_HANDLED
;
1706 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1708 struct trace_seq
*s
= &iter
->seq
;
1709 struct trace_entry
*entry
;
1710 struct trace_event
*event
;
1714 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1715 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1716 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1717 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1720 event
= ftrace_find_event(entry
->type
);
1721 return event
? event
->binary(iter
, 0) : TRACE_TYPE_HANDLED
;
1724 static int trace_empty(struct trace_iterator
*iter
)
1728 /* If we are looking at one CPU buffer, only check that one */
1729 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
1730 cpu
= iter
->cpu_file
;
1731 if (iter
->buffer_iter
[cpu
]) {
1732 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1735 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1741 for_each_tracing_cpu(cpu
) {
1742 if (iter
->buffer_iter
[cpu
]) {
1743 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1746 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1754 /* Called with trace_event_read_lock() held. */
1755 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1757 enum print_line_t ret
;
1759 if (iter
->trace
&& iter
->trace
->print_line
) {
1760 ret
= iter
->trace
->print_line(iter
);
1761 if (ret
!= TRACE_TYPE_UNHANDLED
)
1765 if (iter
->ent
->type
== TRACE_BPRINT
&&
1766 trace_flags
& TRACE_ITER_PRINTK
&&
1767 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1768 return trace_print_bprintk_msg_only(iter
);
1770 if (iter
->ent
->type
== TRACE_PRINT
&&
1771 trace_flags
& TRACE_ITER_PRINTK
&&
1772 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1773 return trace_print_printk_msg_only(iter
);
1775 if (trace_flags
& TRACE_ITER_BIN
)
1776 return print_bin_fmt(iter
);
1778 if (trace_flags
& TRACE_ITER_HEX
)
1779 return print_hex_fmt(iter
);
1781 if (trace_flags
& TRACE_ITER_RAW
)
1782 return print_raw_fmt(iter
);
1784 return print_trace_fmt(iter
);
1787 static int s_show(struct seq_file
*m
, void *v
)
1789 struct trace_iterator
*iter
= v
;
1791 if (iter
->ent
== NULL
) {
1793 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1796 if (iter
->trace
&& iter
->trace
->print_header
)
1797 iter
->trace
->print_header(m
);
1798 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1799 /* print nothing if the buffers are empty */
1800 if (trace_empty(iter
))
1802 print_trace_header(m
, iter
);
1803 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1804 print_lat_help_header(m
);
1806 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1807 print_func_help_header(m
);
1810 print_trace_line(iter
);
1811 trace_print_seq(m
, &iter
->seq
);
1817 static struct seq_operations tracer_seq_ops
= {
1824 static struct trace_iterator
*
1825 __tracing_open(struct inode
*inode
, struct file
*file
)
1827 long cpu_file
= (long) inode
->i_private
;
1828 void *fail_ret
= ERR_PTR(-ENOMEM
);
1829 struct trace_iterator
*iter
;
1833 if (tracing_disabled
)
1834 return ERR_PTR(-ENODEV
);
1836 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1838 return ERR_PTR(-ENOMEM
);
1841 * We make a copy of the current tracer to avoid concurrent
1842 * changes on it while we are reading.
1844 mutex_lock(&trace_types_lock
);
1845 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
1850 *iter
->trace
= *current_trace
;
1852 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
1855 cpumask_clear(iter
->started
);
1857 if (current_trace
&& current_trace
->print_max
)
1860 iter
->tr
= &global_trace
;
1862 mutex_init(&iter
->mutex
);
1863 iter
->cpu_file
= cpu_file
;
1865 /* Notify the tracer early; before we stop tracing. */
1866 if (iter
->trace
&& iter
->trace
->open
)
1867 iter
->trace
->open(iter
);
1869 /* Annotate start of buffers if we had overruns */
1870 if (ring_buffer_overruns(iter
->tr
->buffer
))
1871 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
1873 /* stop the trace while dumping */
1876 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
1877 for_each_tracing_cpu(cpu
) {
1879 iter
->buffer_iter
[cpu
] =
1880 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1881 tracing_iter_reset(iter
, cpu
);
1884 cpu
= iter
->cpu_file
;
1885 iter
->buffer_iter
[cpu
] =
1886 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1887 tracing_iter_reset(iter
, cpu
);
1890 ret
= seq_open(file
, &tracer_seq_ops
);
1892 fail_ret
= ERR_PTR(ret
);
1896 m
= file
->private_data
;
1899 mutex_unlock(&trace_types_lock
);
1904 for_each_tracing_cpu(cpu
) {
1905 if (iter
->buffer_iter
[cpu
])
1906 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1908 free_cpumask_var(iter
->started
);
1911 mutex_unlock(&trace_types_lock
);
1918 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1920 if (tracing_disabled
)
1923 filp
->private_data
= inode
->i_private
;
1927 static int tracing_release(struct inode
*inode
, struct file
*file
)
1929 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1930 struct trace_iterator
*iter
;
1933 if (!(file
->f_mode
& FMODE_READ
))
1938 mutex_lock(&trace_types_lock
);
1939 for_each_tracing_cpu(cpu
) {
1940 if (iter
->buffer_iter
[cpu
])
1941 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1944 if (iter
->trace
&& iter
->trace
->close
)
1945 iter
->trace
->close(iter
);
1947 /* reenable tracing if it was previously enabled */
1949 mutex_unlock(&trace_types_lock
);
1951 seq_release(inode
, file
);
1952 mutex_destroy(&iter
->mutex
);
1953 free_cpumask_var(iter
->started
);
1959 static int tracing_open(struct inode
*inode
, struct file
*file
)
1961 struct trace_iterator
*iter
;
1964 /* If this file was open for write, then erase contents */
1965 if ((file
->f_mode
& FMODE_WRITE
) &&
1966 (file
->f_flags
& O_TRUNC
)) {
1967 long cpu
= (long) inode
->i_private
;
1969 if (cpu
== TRACE_PIPE_ALL_CPU
)
1970 tracing_reset_online_cpus(&global_trace
);
1972 tracing_reset(&global_trace
, cpu
);
1975 if (file
->f_mode
& FMODE_READ
) {
1976 iter
= __tracing_open(inode
, file
);
1978 ret
= PTR_ERR(iter
);
1979 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
1980 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
1986 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1988 struct tracer
*t
= v
;
1998 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2003 mutex_lock(&trace_types_lock
);
2004 for (t
= trace_types
; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2010 static void t_stop(struct seq_file
*m
, void *p
)
2012 mutex_unlock(&trace_types_lock
);
2015 static int t_show(struct seq_file
*m
, void *v
)
2017 struct tracer
*t
= v
;
2022 seq_printf(m
, "%s", t
->name
);
2031 static struct seq_operations show_traces_seq_ops
= {
2038 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2040 if (tracing_disabled
)
2043 return seq_open(file
, &show_traces_seq_ops
);
2047 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
2048 size_t count
, loff_t
*ppos
)
2053 static const struct file_operations tracing_fops
= {
2054 .open
= tracing_open
,
2056 .write
= tracing_write_stub
,
2057 .llseek
= seq_lseek
,
2058 .release
= tracing_release
,
2061 static const struct file_operations show_traces_fops
= {
2062 .open
= show_traces_open
,
2064 .release
= seq_release
,
2068 * Only trace on a CPU if the bitmask is set:
2070 static cpumask_var_t tracing_cpumask
;
2073 * The tracer itself will not take this lock, but still we want
2074 * to provide a consistent cpumask to user-space:
2076 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2079 * Temporary storage for the character representation of the
2080 * CPU bitmask (and one more byte for the newline):
2082 static char mask_str
[NR_CPUS
+ 1];
2085 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2086 size_t count
, loff_t
*ppos
)
2090 mutex_lock(&tracing_cpumask_update_lock
);
2092 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2093 if (count
- len
< 2) {
2097 len
+= sprintf(mask_str
+ len
, "\n");
2098 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2101 mutex_unlock(&tracing_cpumask_update_lock
);
2107 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2108 size_t count
, loff_t
*ppos
)
2111 cpumask_var_t tracing_cpumask_new
;
2113 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2116 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2120 mutex_lock(&tracing_cpumask_update_lock
);
2122 local_irq_disable();
2123 __raw_spin_lock(&ftrace_max_lock
);
2124 for_each_tracing_cpu(cpu
) {
2126 * Increase/decrease the disabled counter if we are
2127 * about to flip a bit in the cpumask:
2129 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2130 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2131 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2133 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2134 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2135 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2138 __raw_spin_unlock(&ftrace_max_lock
);
2141 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2143 mutex_unlock(&tracing_cpumask_update_lock
);
2144 free_cpumask_var(tracing_cpumask_new
);
2149 free_cpumask_var(tracing_cpumask_new
);
2154 static const struct file_operations tracing_cpumask_fops
= {
2155 .open
= tracing_open_generic
,
2156 .read
= tracing_cpumask_read
,
2157 .write
= tracing_cpumask_write
,
2161 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2162 size_t cnt
, loff_t
*ppos
)
2164 struct tracer_opt
*trace_opts
;
2172 /* calculate max size */
2173 for (i
= 0; trace_options
[i
]; i
++) {
2174 len
+= strlen(trace_options
[i
]);
2175 len
+= 3; /* "no" and newline */
2178 mutex_lock(&trace_types_lock
);
2179 tracer_flags
= current_trace
->flags
->val
;
2180 trace_opts
= current_trace
->flags
->opts
;
2183 * Increase the size with names of options specific
2184 * of the current tracer.
2186 for (i
= 0; trace_opts
[i
].name
; i
++) {
2187 len
+= strlen(trace_opts
[i
].name
);
2188 len
+= 3; /* "no" and newline */
2192 buf
= kmalloc(len
+ 1, GFP_KERNEL
);
2194 mutex_unlock(&trace_types_lock
);
2198 for (i
= 0; trace_options
[i
]; i
++) {
2199 if (trace_flags
& (1 << i
))
2200 r
+= sprintf(buf
+ r
, "%s\n", trace_options
[i
]);
2202 r
+= sprintf(buf
+ r
, "no%s\n", trace_options
[i
]);
2205 for (i
= 0; trace_opts
[i
].name
; i
++) {
2206 if (tracer_flags
& trace_opts
[i
].bit
)
2207 r
+= sprintf(buf
+ r
, "%s\n",
2208 trace_opts
[i
].name
);
2210 r
+= sprintf(buf
+ r
, "no%s\n",
2211 trace_opts
[i
].name
);
2213 mutex_unlock(&trace_types_lock
);
2215 WARN_ON(r
>= len
+ 1);
2217 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2223 /* Try to assign a tracer specific option */
2224 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2226 struct tracer_flags
*tracer_flags
= trace
->flags
;
2227 struct tracer_opt
*opts
= NULL
;
2231 for (i
= 0; tracer_flags
->opts
[i
].name
; i
++) {
2232 opts
= &tracer_flags
->opts
[i
];
2233 len
= strlen(opts
->name
);
2235 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2236 ret
= trace
->set_flag(tracer_flags
->val
,
2242 if (!tracer_flags
->opts
[i
].name
)
2245 /* Refused to handle */
2250 tracer_flags
->val
&= ~opts
->bit
;
2252 tracer_flags
->val
|= opts
->bit
;
2257 static void set_tracer_flags(unsigned int mask
, int enabled
)
2259 /* do nothing if flag is already set */
2260 if (!!(trace_flags
& mask
) == !!enabled
)
2264 trace_flags
|= mask
;
2266 trace_flags
&= ~mask
;
2270 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2271 size_t cnt
, loff_t
*ppos
)
2279 if (cnt
>= sizeof(buf
))
2282 if (copy_from_user(&buf
, ubuf
, cnt
))
2287 if (strncmp(buf
, "no", 2) == 0) {
2292 for (i
= 0; trace_options
[i
]; i
++) {
2293 int len
= strlen(trace_options
[i
]);
2295 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2296 set_tracer_flags(1 << i
, !neg
);
2301 /* If no option could be set, test the specific tracer options */
2302 if (!trace_options
[i
]) {
2303 mutex_lock(&trace_types_lock
);
2304 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2305 mutex_unlock(&trace_types_lock
);
2315 static const struct file_operations tracing_iter_fops
= {
2316 .open
= tracing_open_generic
,
2317 .read
= tracing_trace_options_read
,
2318 .write
= tracing_trace_options_write
,
2321 static const char readme_msg
[] =
2322 "tracing mini-HOWTO:\n\n"
2323 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2324 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2325 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2326 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2328 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2329 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2331 "# cat /sys/kernel/debug/tracing/trace_options\n"
2332 "noprint-parent nosym-offset nosym-addr noverbose\n"
2333 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2334 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2335 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2336 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2340 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2341 size_t cnt
, loff_t
*ppos
)
2343 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2344 readme_msg
, strlen(readme_msg
));
2347 static const struct file_operations tracing_readme_fops
= {
2348 .open
= tracing_open_generic
,
2349 .read
= tracing_readme_read
,
2353 tracing_saved_cmdlines_read(struct file
*file
, char __user
*ubuf
,
2354 size_t cnt
, loff_t
*ppos
)
2363 file_buf
= kmalloc(SAVED_CMDLINES
*(16+TASK_COMM_LEN
), GFP_KERNEL
);
2367 buf_comm
= kmalloc(TASK_COMM_LEN
, GFP_KERNEL
);
2375 for (i
= 0; i
< SAVED_CMDLINES
; i
++) {
2378 pid
= map_cmdline_to_pid
[i
];
2379 if (pid
== -1 || pid
== NO_CMDLINE_MAP
)
2382 trace_find_cmdline(pid
, buf_comm
);
2383 r
= sprintf(buf
, "%d %s\n", pid
, buf_comm
);
2388 len
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
2397 static const struct file_operations tracing_saved_cmdlines_fops
= {
2398 .open
= tracing_open_generic
,
2399 .read
= tracing_saved_cmdlines_read
,
2403 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2404 size_t cnt
, loff_t
*ppos
)
2409 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2410 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2414 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2415 size_t cnt
, loff_t
*ppos
)
2417 struct trace_array
*tr
= filp
->private_data
;
2422 if (cnt
>= sizeof(buf
))
2425 if (copy_from_user(&buf
, ubuf
, cnt
))
2430 ret
= strict_strtoul(buf
, 10, &val
);
2436 mutex_lock(&trace_types_lock
);
2437 if (tracer_enabled
^ val
) {
2440 if (current_trace
->start
)
2441 current_trace
->start(tr
);
2446 if (current_trace
->stop
)
2447 current_trace
->stop(tr
);
2450 mutex_unlock(&trace_types_lock
);
2458 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2459 size_t cnt
, loff_t
*ppos
)
2461 char buf
[max_tracer_type_len
+2];
2464 mutex_lock(&trace_types_lock
);
2466 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2468 r
= sprintf(buf
, "\n");
2469 mutex_unlock(&trace_types_lock
);
2471 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2474 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2476 tracing_reset_online_cpus(tr
);
2480 static int tracing_resize_ring_buffer(unsigned long size
)
2485 * If kernel or user changes the size of the ring buffer
2486 * we use the size that was given, and we can forget about
2487 * expanding it later.
2489 ring_buffer_expanded
= 1;
2491 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2495 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2499 r
= ring_buffer_resize(global_trace
.buffer
,
2500 global_trace
.entries
);
2503 * AARGH! We are left with different
2504 * size max buffer!!!!
2505 * The max buffer is our "snapshot" buffer.
2506 * When a tracer needs a snapshot (one of the
2507 * latency tracers), it swaps the max buffer
2508 * with the saved snap shot. We succeeded to
2509 * update the size of the main buffer, but failed to
2510 * update the size of the max buffer. But when we tried
2511 * to reset the main buffer to the original size, we
2512 * failed there too. This is very unlikely to
2513 * happen, but if it does, warn and kill all
2517 tracing_disabled
= 1;
2522 global_trace
.entries
= size
;
2528 * tracing_update_buffers - used by tracing facility to expand ring buffers
2530 * To save on memory when the tracing is never used on a system with it
2531 * configured in. The ring buffers are set to a minimum size. But once
2532 * a user starts to use the tracing facility, then they need to grow
2533 * to their default size.
2535 * This function is to be called when a tracer is about to be used.
2537 int tracing_update_buffers(void)
2541 mutex_lock(&trace_types_lock
);
2542 if (!ring_buffer_expanded
)
2543 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2544 mutex_unlock(&trace_types_lock
);
2549 struct trace_option_dentry
;
2551 static struct trace_option_dentry
*
2552 create_trace_option_files(struct tracer
*tracer
);
2555 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2557 static int tracing_set_tracer(const char *buf
)
2559 static struct trace_option_dentry
*topts
;
2560 struct trace_array
*tr
= &global_trace
;
2564 mutex_lock(&trace_types_lock
);
2566 if (!ring_buffer_expanded
) {
2567 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2573 for (t
= trace_types
; t
; t
= t
->next
) {
2574 if (strcmp(t
->name
, buf
) == 0)
2581 if (t
== current_trace
)
2584 trace_branch_disable();
2585 if (current_trace
&& current_trace
->reset
)
2586 current_trace
->reset(tr
);
2588 destroy_trace_option_files(topts
);
2592 topts
= create_trace_option_files(current_trace
);
2595 ret
= tracer_init(t
, tr
);
2600 trace_branch_enable(tr
);
2602 mutex_unlock(&trace_types_lock
);
2608 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2609 size_t cnt
, loff_t
*ppos
)
2611 char buf
[max_tracer_type_len
+1];
2618 if (cnt
> max_tracer_type_len
)
2619 cnt
= max_tracer_type_len
;
2621 if (copy_from_user(&buf
, ubuf
, cnt
))
2626 /* strip ending whitespace. */
2627 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2630 err
= tracing_set_tracer(buf
);
2640 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2641 size_t cnt
, loff_t
*ppos
)
2643 unsigned long *ptr
= filp
->private_data
;
2647 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2648 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2649 if (r
> sizeof(buf
))
2651 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2655 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2656 size_t cnt
, loff_t
*ppos
)
2658 unsigned long *ptr
= filp
->private_data
;
2663 if (cnt
>= sizeof(buf
))
2666 if (copy_from_user(&buf
, ubuf
, cnt
))
2671 ret
= strict_strtoul(buf
, 10, &val
);
2680 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2682 long cpu_file
= (long) inode
->i_private
;
2683 struct trace_iterator
*iter
;
2686 if (tracing_disabled
)
2689 mutex_lock(&trace_types_lock
);
2691 /* We only allow one reader per cpu */
2692 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
2693 if (!cpumask_empty(tracing_reader_cpumask
)) {
2697 cpumask_setall(tracing_reader_cpumask
);
2699 if (!cpumask_test_cpu(cpu_file
, tracing_reader_cpumask
))
2700 cpumask_set_cpu(cpu_file
, tracing_reader_cpumask
);
2707 /* create a buffer to store the information to pass to userspace */
2708 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2715 * We make a copy of the current tracer to avoid concurrent
2716 * changes on it while we are reading.
2718 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2724 *iter
->trace
= *current_trace
;
2726 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2731 /* trace pipe does not show start of buffer */
2732 cpumask_setall(iter
->started
);
2734 if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2735 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2737 iter
->cpu_file
= cpu_file
;
2738 iter
->tr
= &global_trace
;
2739 mutex_init(&iter
->mutex
);
2740 filp
->private_data
= iter
;
2742 if (iter
->trace
->pipe_open
)
2743 iter
->trace
->pipe_open(iter
);
2746 mutex_unlock(&trace_types_lock
);
2752 mutex_unlock(&trace_types_lock
);
2756 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2758 struct trace_iterator
*iter
= file
->private_data
;
2760 mutex_lock(&trace_types_lock
);
2762 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
)
2763 cpumask_clear(tracing_reader_cpumask
);
2765 cpumask_clear_cpu(iter
->cpu_file
, tracing_reader_cpumask
);
2767 mutex_unlock(&trace_types_lock
);
2769 free_cpumask_var(iter
->started
);
2770 mutex_destroy(&iter
->mutex
);
2778 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2780 struct trace_iterator
*iter
= filp
->private_data
;
2782 if (trace_flags
& TRACE_ITER_BLOCK
) {
2784 * Always select as readable when in blocking mode
2786 return POLLIN
| POLLRDNORM
;
2788 if (!trace_empty(iter
))
2789 return POLLIN
| POLLRDNORM
;
2790 poll_wait(filp
, &trace_wait
, poll_table
);
2791 if (!trace_empty(iter
))
2792 return POLLIN
| POLLRDNORM
;
2799 void default_wait_pipe(struct trace_iterator
*iter
)
2803 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
2805 if (trace_empty(iter
))
2808 finish_wait(&trace_wait
, &wait
);
2812 * This is a make-shift waitqueue.
2813 * A tracer might use this callback on some rare cases:
2815 * 1) the current tracer might hold the runqueue lock when it wakes up
2816 * a reader, hence a deadlock (sched, function, and function graph tracers)
2817 * 2) the function tracers, trace all functions, we don't want
2818 * the overhead of calling wake_up and friends
2819 * (and tracing them too)
2821 * Anyway, this is really very primitive wakeup.
2823 void poll_wait_pipe(struct trace_iterator
*iter
)
2825 set_current_state(TASK_INTERRUPTIBLE
);
2826 /* sleep for 100 msecs, and try again. */
2827 schedule_timeout(HZ
/ 10);
2830 /* Must be called with trace_types_lock mutex held. */
2831 static int tracing_wait_pipe(struct file
*filp
)
2833 struct trace_iterator
*iter
= filp
->private_data
;
2835 while (trace_empty(iter
)) {
2837 if ((filp
->f_flags
& O_NONBLOCK
)) {
2841 mutex_unlock(&iter
->mutex
);
2843 iter
->trace
->wait_pipe(iter
);
2845 mutex_lock(&iter
->mutex
);
2847 if (signal_pending(current
))
2851 * We block until we read something and tracing is disabled.
2852 * We still block if tracing is disabled, but we have never
2853 * read anything. This allows a user to cat this file, and
2854 * then enable tracing. But after we have read something,
2855 * we give an EOF when tracing is again disabled.
2857 * iter->pos will be 0 if we haven't read anything.
2859 if (!tracer_enabled
&& iter
->pos
)
2870 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2871 size_t cnt
, loff_t
*ppos
)
2873 struct trace_iterator
*iter
= filp
->private_data
;
2874 static struct tracer
*old_tracer
;
2877 /* return any leftover data */
2878 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2882 trace_seq_init(&iter
->seq
);
2884 /* copy the tracer to avoid using a global lock all around */
2885 mutex_lock(&trace_types_lock
);
2886 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
2887 old_tracer
= current_trace
;
2888 *iter
->trace
= *current_trace
;
2890 mutex_unlock(&trace_types_lock
);
2893 * Avoid more than one consumer on a single file descriptor
2894 * This is just a matter of traces coherency, the ring buffer itself
2897 mutex_lock(&iter
->mutex
);
2898 if (iter
->trace
->read
) {
2899 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2905 sret
= tracing_wait_pipe(filp
);
2909 /* stop when tracing is finished */
2910 if (trace_empty(iter
)) {
2915 if (cnt
>= PAGE_SIZE
)
2916 cnt
= PAGE_SIZE
- 1;
2918 /* reset all but tr, trace, and overruns */
2919 memset(&iter
->seq
, 0,
2920 sizeof(struct trace_iterator
) -
2921 offsetof(struct trace_iterator
, seq
));
2924 trace_event_read_lock();
2925 while (find_next_entry_inc(iter
) != NULL
) {
2926 enum print_line_t ret
;
2927 int len
= iter
->seq
.len
;
2929 ret
= print_trace_line(iter
);
2930 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2931 /* don't print partial lines */
2932 iter
->seq
.len
= len
;
2935 if (ret
!= TRACE_TYPE_NO_CONSUME
)
2936 trace_consume(iter
);
2938 if (iter
->seq
.len
>= cnt
)
2941 trace_event_read_unlock();
2943 /* Now copy what we have to the user */
2944 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2945 if (iter
->seq
.readpos
>= iter
->seq
.len
)
2946 trace_seq_init(&iter
->seq
);
2949 * If there was nothing to send to user, inspite of consuming trace
2950 * entries, go back to wait for more entries.
2956 mutex_unlock(&iter
->mutex
);
2961 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
2962 struct pipe_buffer
*buf
)
2964 __free_page(buf
->page
);
2967 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
2970 __free_page(spd
->pages
[idx
]);
2973 static struct pipe_buf_operations tracing_pipe_buf_ops
= {
2975 .map
= generic_pipe_buf_map
,
2976 .unmap
= generic_pipe_buf_unmap
,
2977 .confirm
= generic_pipe_buf_confirm
,
2978 .release
= tracing_pipe_buf_release
,
2979 .steal
= generic_pipe_buf_steal
,
2980 .get
= generic_pipe_buf_get
,
2984 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
2989 /* Seq buffer is page-sized, exactly what we need. */
2991 count
= iter
->seq
.len
;
2992 ret
= print_trace_line(iter
);
2993 count
= iter
->seq
.len
- count
;
2996 iter
->seq
.len
-= count
;
2999 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3000 iter
->seq
.len
-= count
;
3004 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3005 trace_consume(iter
);
3007 if (!find_next_entry_inc(iter
)) {
3017 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3019 struct pipe_inode_info
*pipe
,
3023 struct page
*pages
[PIPE_BUFFERS
];
3024 struct partial_page partial
[PIPE_BUFFERS
];
3025 struct trace_iterator
*iter
= filp
->private_data
;
3026 struct splice_pipe_desc spd
= {
3029 .nr_pages
= 0, /* This gets updated below. */
3031 .ops
= &tracing_pipe_buf_ops
,
3032 .spd_release
= tracing_spd_release_pipe
,
3034 static struct tracer
*old_tracer
;
3039 /* copy the tracer to avoid using a global lock all around */
3040 mutex_lock(&trace_types_lock
);
3041 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3042 old_tracer
= current_trace
;
3043 *iter
->trace
= *current_trace
;
3045 mutex_unlock(&trace_types_lock
);
3047 mutex_lock(&iter
->mutex
);
3049 if (iter
->trace
->splice_read
) {
3050 ret
= iter
->trace
->splice_read(iter
, filp
,
3051 ppos
, pipe
, len
, flags
);
3056 ret
= tracing_wait_pipe(filp
);
3060 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
3065 trace_event_read_lock();
3067 /* Fill as many pages as possible. */
3068 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
3069 pages
[i
] = alloc_page(GFP_KERNEL
);
3073 rem
= tracing_fill_pipe_page(rem
, iter
);
3075 /* Copy the data into the page, so we can start over. */
3076 ret
= trace_seq_to_buffer(&iter
->seq
,
3077 page_address(pages
[i
]),
3080 __free_page(pages
[i
]);
3083 partial
[i
].offset
= 0;
3084 partial
[i
].len
= iter
->seq
.len
;
3086 trace_seq_init(&iter
->seq
);
3089 trace_event_read_unlock();
3090 mutex_unlock(&iter
->mutex
);
3094 return splice_to_pipe(pipe
, &spd
);
3097 mutex_unlock(&iter
->mutex
);
3103 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3104 size_t cnt
, loff_t
*ppos
)
3106 struct trace_array
*tr
= filp
->private_data
;
3110 mutex_lock(&trace_types_lock
);
3111 if (!ring_buffer_expanded
)
3112 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3114 trace_buf_size
>> 10);
3116 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3117 mutex_unlock(&trace_types_lock
);
3119 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3123 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3124 size_t cnt
, loff_t
*ppos
)
3130 if (cnt
>= sizeof(buf
))
3133 if (copy_from_user(&buf
, ubuf
, cnt
))
3138 ret
= strict_strtoul(buf
, 10, &val
);
3142 /* must have at least 1 entry */
3146 mutex_lock(&trace_types_lock
);
3150 /* disable all cpu buffers */
3151 for_each_tracing_cpu(cpu
) {
3152 if (global_trace
.data
[cpu
])
3153 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3154 if (max_tr
.data
[cpu
])
3155 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3158 /* value is in KB */
3161 if (val
!= global_trace
.entries
) {
3162 ret
= tracing_resize_ring_buffer(val
);
3171 /* If check pages failed, return ENOMEM */
3172 if (tracing_disabled
)
3175 for_each_tracing_cpu(cpu
) {
3176 if (global_trace
.data
[cpu
])
3177 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3178 if (max_tr
.data
[cpu
])
3179 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3183 max_tr
.entries
= global_trace
.entries
;
3184 mutex_unlock(&trace_types_lock
);
3189 static int mark_printk(const char *fmt
, ...)
3193 va_start(args
, fmt
);
3194 ret
= trace_vprintk(0, fmt
, args
);
3200 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3201 size_t cnt
, loff_t
*fpos
)
3206 if (tracing_disabled
)
3209 if (cnt
> TRACE_BUF_SIZE
)
3210 cnt
= TRACE_BUF_SIZE
;
3212 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3216 if (copy_from_user(buf
, ubuf
, cnt
)) {
3221 /* Cut from the first nil or newline. */
3223 end
= strchr(buf
, '\n');
3227 cnt
= mark_printk("%s\n", buf
);
3234 static ssize_t
tracing_clock_read(struct file
*filp
, char __user
*ubuf
,
3235 size_t cnt
, loff_t
*ppos
)
3241 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++)
3242 bufiter
+= snprintf(buf
+ bufiter
, sizeof(buf
) - bufiter
,
3243 "%s%s%s%s", i
? " " : "",
3244 i
== trace_clock_id
? "[" : "", trace_clocks
[i
].name
,
3245 i
== trace_clock_id
? "]" : "");
3246 bufiter
+= snprintf(buf
+ bufiter
, sizeof(buf
) - bufiter
, "\n");
3248 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, bufiter
);
3251 static ssize_t
tracing_clock_write(struct file
*filp
, const char __user
*ubuf
,
3252 size_t cnt
, loff_t
*fpos
)
3255 const char *clockstr
;
3258 if (cnt
>= sizeof(buf
))
3261 if (copy_from_user(&buf
, ubuf
, cnt
))
3266 clockstr
= strstrip(buf
);
3268 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++) {
3269 if (strcmp(trace_clocks
[i
].name
, clockstr
) == 0)
3272 if (i
== ARRAY_SIZE(trace_clocks
))
3277 mutex_lock(&trace_types_lock
);
3279 ring_buffer_set_clock(global_trace
.buffer
, trace_clocks
[i
].func
);
3281 ring_buffer_set_clock(max_tr
.buffer
, trace_clocks
[i
].func
);
3283 mutex_unlock(&trace_types_lock
);
3290 static const struct file_operations tracing_max_lat_fops
= {
3291 .open
= tracing_open_generic
,
3292 .read
= tracing_max_lat_read
,
3293 .write
= tracing_max_lat_write
,
3296 static const struct file_operations tracing_ctrl_fops
= {
3297 .open
= tracing_open_generic
,
3298 .read
= tracing_ctrl_read
,
3299 .write
= tracing_ctrl_write
,
3302 static const struct file_operations set_tracer_fops
= {
3303 .open
= tracing_open_generic
,
3304 .read
= tracing_set_trace_read
,
3305 .write
= tracing_set_trace_write
,
3308 static const struct file_operations tracing_pipe_fops
= {
3309 .open
= tracing_open_pipe
,
3310 .poll
= tracing_poll_pipe
,
3311 .read
= tracing_read_pipe
,
3312 .splice_read
= tracing_splice_read_pipe
,
3313 .release
= tracing_release_pipe
,
3316 static const struct file_operations tracing_entries_fops
= {
3317 .open
= tracing_open_generic
,
3318 .read
= tracing_entries_read
,
3319 .write
= tracing_entries_write
,
3322 static const struct file_operations tracing_mark_fops
= {
3323 .open
= tracing_open_generic
,
3324 .write
= tracing_mark_write
,
3327 static const struct file_operations trace_clock_fops
= {
3328 .open
= tracing_open_generic
,
3329 .read
= tracing_clock_read
,
3330 .write
= tracing_clock_write
,
3333 struct ftrace_buffer_info
{
3334 struct trace_array
*tr
;
3340 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3342 int cpu
= (int)(long)inode
->i_private
;
3343 struct ftrace_buffer_info
*info
;
3345 if (tracing_disabled
)
3348 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3352 info
->tr
= &global_trace
;
3355 /* Force reading ring buffer for first read */
3356 info
->read
= (unsigned int)-1;
3358 filp
->private_data
= info
;
3360 return nonseekable_open(inode
, filp
);
3364 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3365 size_t count
, loff_t
*ppos
)
3367 struct ftrace_buffer_info
*info
= filp
->private_data
;
3376 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3380 /* Do we have previous read data to read? */
3381 if (info
->read
< PAGE_SIZE
)
3386 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3393 pos
= ring_buffer_page_len(info
->spare
);
3395 if (pos
< PAGE_SIZE
)
3396 memset(info
->spare
+ pos
, 0, PAGE_SIZE
- pos
);
3399 size
= PAGE_SIZE
- info
->read
;
3403 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3414 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3416 struct ftrace_buffer_info
*info
= file
->private_data
;
3419 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3426 struct ring_buffer
*buffer
;
3431 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3432 struct pipe_buffer
*buf
)
3434 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3439 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3444 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3445 struct pipe_buffer
*buf
)
3450 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3451 struct pipe_buffer
*buf
)
3453 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3458 /* Pipe buffer operations for a buffer. */
3459 static struct pipe_buf_operations buffer_pipe_buf_ops
= {
3461 .map
= generic_pipe_buf_map
,
3462 .unmap
= generic_pipe_buf_unmap
,
3463 .confirm
= generic_pipe_buf_confirm
,
3464 .release
= buffer_pipe_buf_release
,
3465 .steal
= buffer_pipe_buf_steal
,
3466 .get
= buffer_pipe_buf_get
,
3470 * Callback from splice_to_pipe(), if we need to release some pages
3471 * at the end of the spd in case we error'ed out in filling the pipe.
3473 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3475 struct buffer_ref
*ref
=
3476 (struct buffer_ref
*)spd
->partial
[i
].private;
3481 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3483 spd
->partial
[i
].private = 0;
3487 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3488 struct pipe_inode_info
*pipe
, size_t len
,
3491 struct ftrace_buffer_info
*info
= file
->private_data
;
3492 struct partial_page partial
[PIPE_BUFFERS
];
3493 struct page
*pages
[PIPE_BUFFERS
];
3494 struct splice_pipe_desc spd
= {
3498 .ops
= &buffer_pipe_buf_ops
,
3499 .spd_release
= buffer_spd_release
,
3501 struct buffer_ref
*ref
;
3502 int entries
, size
, i
;
3505 if (*ppos
& (PAGE_SIZE
- 1)) {
3506 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3510 if (len
& (PAGE_SIZE
- 1)) {
3511 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3512 if (len
< PAGE_SIZE
)
3517 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3519 for (i
= 0; i
< PIPE_BUFFERS
&& len
&& entries
; i
++, len
-= PAGE_SIZE
) {
3523 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3528 ref
->buffer
= info
->tr
->buffer
;
3529 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3535 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3538 ring_buffer_free_read_page(ref
->buffer
,
3545 * zero out any left over data, this is going to
3548 size
= ring_buffer_page_len(ref
->page
);
3549 if (size
< PAGE_SIZE
)
3550 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3552 page
= virt_to_page(ref
->page
);
3554 spd
.pages
[i
] = page
;
3555 spd
.partial
[i
].len
= PAGE_SIZE
;
3556 spd
.partial
[i
].offset
= 0;
3557 spd
.partial
[i
].private = (unsigned long)ref
;
3561 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3566 /* did we read anything? */
3567 if (!spd
.nr_pages
) {
3568 if (flags
& SPLICE_F_NONBLOCK
)
3576 ret
= splice_to_pipe(pipe
, &spd
);
3581 static const struct file_operations tracing_buffers_fops
= {
3582 .open
= tracing_buffers_open
,
3583 .read
= tracing_buffers_read
,
3584 .release
= tracing_buffers_release
,
3585 .splice_read
= tracing_buffers_splice_read
,
3586 .llseek
= no_llseek
,
3590 tracing_stats_read(struct file
*filp
, char __user
*ubuf
,
3591 size_t count
, loff_t
*ppos
)
3593 unsigned long cpu
= (unsigned long)filp
->private_data
;
3594 struct trace_array
*tr
= &global_trace
;
3595 struct trace_seq
*s
;
3598 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
3604 cnt
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
3605 trace_seq_printf(s
, "entries: %ld\n", cnt
);
3607 cnt
= ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
3608 trace_seq_printf(s
, "overrun: %ld\n", cnt
);
3610 cnt
= ring_buffer_commit_overrun_cpu(tr
->buffer
, cpu
);
3611 trace_seq_printf(s
, "commit overrun: %ld\n", cnt
);
3613 count
= simple_read_from_buffer(ubuf
, count
, ppos
, s
->buffer
, s
->len
);
3620 static const struct file_operations tracing_stats_fops
= {
3621 .open
= tracing_open_generic
,
3622 .read
= tracing_stats_read
,
3625 #ifdef CONFIG_DYNAMIC_FTRACE
3627 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3633 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3634 size_t cnt
, loff_t
*ppos
)
3636 static char ftrace_dyn_info_buffer
[1024];
3637 static DEFINE_MUTEX(dyn_info_mutex
);
3638 unsigned long *p
= filp
->private_data
;
3639 char *buf
= ftrace_dyn_info_buffer
;
3640 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3643 mutex_lock(&dyn_info_mutex
);
3644 r
= sprintf(buf
, "%ld ", *p
);
3646 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3649 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3651 mutex_unlock(&dyn_info_mutex
);
3656 static const struct file_operations tracing_dyn_info_fops
= {
3657 .open
= tracing_open_generic
,
3658 .read
= tracing_read_dyn_info
,
3662 static struct dentry
*d_tracer
;
3664 struct dentry
*tracing_init_dentry(void)
3671 if (!debugfs_initialized())
3674 d_tracer
= debugfs_create_dir("tracing", NULL
);
3676 if (!d_tracer
&& !once
) {
3678 pr_warning("Could not create debugfs directory 'tracing'\n");
3685 static struct dentry
*d_percpu
;
3687 struct dentry
*tracing_dentry_percpu(void)
3690 struct dentry
*d_tracer
;
3695 d_tracer
= tracing_init_dentry();
3700 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
3702 if (!d_percpu
&& !once
) {
3704 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3711 static void tracing_init_debugfs_percpu(long cpu
)
3713 struct dentry
*d_percpu
= tracing_dentry_percpu();
3714 struct dentry
*d_cpu
;
3715 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3718 if (cpu
> 999 || cpu
< 0)
3721 sprintf(cpu_dir
, "cpu%ld", cpu
);
3722 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
3724 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
3728 /* per cpu trace_pipe */
3729 trace_create_file("trace_pipe", 0444, d_cpu
,
3730 (void *) cpu
, &tracing_pipe_fops
);
3733 trace_create_file("trace", 0644, d_cpu
,
3734 (void *) cpu
, &tracing_fops
);
3736 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
3737 (void *) cpu
, &tracing_buffers_fops
);
3739 trace_create_file("stats", 0444, d_cpu
,
3740 (void *) cpu
, &tracing_stats_fops
);
3743 #ifdef CONFIG_FTRACE_SELFTEST
3744 /* Let selftest have access to static functions in this file */
3745 #include "trace_selftest.c"
3748 struct trace_option_dentry
{
3749 struct tracer_opt
*opt
;
3750 struct tracer_flags
*flags
;
3751 struct dentry
*entry
;
3755 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3758 struct trace_option_dentry
*topt
= filp
->private_data
;
3761 if (topt
->flags
->val
& topt
->opt
->bit
)
3766 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3770 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3773 struct trace_option_dentry
*topt
= filp
->private_data
;
3778 if (cnt
>= sizeof(buf
))
3781 if (copy_from_user(&buf
, ubuf
, cnt
))
3786 ret
= strict_strtoul(buf
, 10, &val
);
3793 /* do nothing if already cleared */
3794 if (!(topt
->flags
->val
& topt
->opt
->bit
))
3797 mutex_lock(&trace_types_lock
);
3798 if (current_trace
->set_flag
)
3799 ret
= current_trace
->set_flag(topt
->flags
->val
,
3801 mutex_unlock(&trace_types_lock
);
3804 topt
->flags
->val
&= ~topt
->opt
->bit
;
3807 /* do nothing if already set */
3808 if (topt
->flags
->val
& topt
->opt
->bit
)
3811 mutex_lock(&trace_types_lock
);
3812 if (current_trace
->set_flag
)
3813 ret
= current_trace
->set_flag(topt
->flags
->val
,
3815 mutex_unlock(&trace_types_lock
);
3818 topt
->flags
->val
|= topt
->opt
->bit
;
3831 static const struct file_operations trace_options_fops
= {
3832 .open
= tracing_open_generic
,
3833 .read
= trace_options_read
,
3834 .write
= trace_options_write
,
3838 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3841 long index
= (long)filp
->private_data
;
3844 if (trace_flags
& (1 << index
))
3849 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3853 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3856 long index
= (long)filp
->private_data
;
3861 if (cnt
>= sizeof(buf
))
3864 if (copy_from_user(&buf
, ubuf
, cnt
))
3869 ret
= strict_strtoul(buf
, 10, &val
);
3875 trace_flags
&= ~(1 << index
);
3878 trace_flags
|= 1 << index
;
3890 static const struct file_operations trace_options_core_fops
= {
3891 .open
= tracing_open_generic
,
3892 .read
= trace_options_core_read
,
3893 .write
= trace_options_core_write
,
3896 struct dentry
*trace_create_file(const char *name
,
3898 struct dentry
*parent
,
3900 const struct file_operations
*fops
)
3904 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
3906 pr_warning("Could not create debugfs '%s' entry\n", name
);
3912 static struct dentry
*trace_options_init_dentry(void)
3914 struct dentry
*d_tracer
;
3915 static struct dentry
*t_options
;
3920 d_tracer
= tracing_init_dentry();
3924 t_options
= debugfs_create_dir("options", d_tracer
);
3926 pr_warning("Could not create debugfs directory 'options'\n");
3934 create_trace_option_file(struct trace_option_dentry
*topt
,
3935 struct tracer_flags
*flags
,
3936 struct tracer_opt
*opt
)
3938 struct dentry
*t_options
;
3940 t_options
= trace_options_init_dentry();
3944 topt
->flags
= flags
;
3947 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
3948 &trace_options_fops
);
3952 static struct trace_option_dentry
*
3953 create_trace_option_files(struct tracer
*tracer
)
3955 struct trace_option_dentry
*topts
;
3956 struct tracer_flags
*flags
;
3957 struct tracer_opt
*opts
;
3963 flags
= tracer
->flags
;
3965 if (!flags
|| !flags
->opts
)
3970 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3973 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
3977 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3978 create_trace_option_file(&topts
[cnt
], flags
,
3985 destroy_trace_option_files(struct trace_option_dentry
*topts
)
3992 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
3993 if (topts
[cnt
].entry
)
3994 debugfs_remove(topts
[cnt
].entry
);
4000 static struct dentry
*
4001 create_trace_option_core_file(const char *option
, long index
)
4003 struct dentry
*t_options
;
4005 t_options
= trace_options_init_dentry();
4009 return trace_create_file(option
, 0644, t_options
, (void *)index
,
4010 &trace_options_core_fops
);
4013 static __init
void create_trace_options_dir(void)
4015 struct dentry
*t_options
;
4018 t_options
= trace_options_init_dentry();
4022 for (i
= 0; trace_options
[i
]; i
++)
4023 create_trace_option_core_file(trace_options
[i
], i
);
4026 static __init
int tracer_init_debugfs(void)
4028 struct dentry
*d_tracer
;
4031 d_tracer
= tracing_init_dentry();
4033 trace_create_file("tracing_enabled", 0644, d_tracer
,
4034 &global_trace
, &tracing_ctrl_fops
);
4036 trace_create_file("trace_options", 0644, d_tracer
,
4037 NULL
, &tracing_iter_fops
);
4039 trace_create_file("tracing_cpumask", 0644, d_tracer
,
4040 NULL
, &tracing_cpumask_fops
);
4042 trace_create_file("trace", 0644, d_tracer
,
4043 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
4045 trace_create_file("available_tracers", 0444, d_tracer
,
4046 &global_trace
, &show_traces_fops
);
4048 trace_create_file("current_tracer", 0644, d_tracer
,
4049 &global_trace
, &set_tracer_fops
);
4051 #ifdef CONFIG_TRACER_MAX_TRACE
4052 trace_create_file("tracing_max_latency", 0644, d_tracer
,
4053 &tracing_max_latency
, &tracing_max_lat_fops
);
4055 trace_create_file("tracing_thresh", 0644, d_tracer
,
4056 &tracing_thresh
, &tracing_max_lat_fops
);
4059 trace_create_file("README", 0444, d_tracer
,
4060 NULL
, &tracing_readme_fops
);
4062 trace_create_file("trace_pipe", 0444, d_tracer
,
4063 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
4065 trace_create_file("buffer_size_kb", 0644, d_tracer
,
4066 &global_trace
, &tracing_entries_fops
);
4068 trace_create_file("trace_marker", 0220, d_tracer
,
4069 NULL
, &tracing_mark_fops
);
4071 trace_create_file("saved_cmdlines", 0444, d_tracer
,
4072 NULL
, &tracing_saved_cmdlines_fops
);
4074 trace_create_file("trace_clock", 0644, d_tracer
, NULL
,
4077 #ifdef CONFIG_DYNAMIC_FTRACE
4078 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
4079 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
4081 #ifdef CONFIG_SYSPROF_TRACER
4082 init_tracer_sysprof_debugfs(d_tracer
);
4085 create_trace_options_dir();
4087 for_each_tracing_cpu(cpu
)
4088 tracing_init_debugfs_percpu(cpu
);
4093 static int trace_panic_handler(struct notifier_block
*this,
4094 unsigned long event
, void *unused
)
4096 if (ftrace_dump_on_oops
)
4101 static struct notifier_block trace_panic_notifier
= {
4102 .notifier_call
= trace_panic_handler
,
4104 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
4107 static int trace_die_handler(struct notifier_block
*self
,
4113 if (ftrace_dump_on_oops
)
4122 static struct notifier_block trace_die_notifier
= {
4123 .notifier_call
= trace_die_handler
,
4128 * printk is set to max of 1024, we really don't need it that big.
4129 * Nothing should be printing 1000 characters anyway.
4131 #define TRACE_MAX_PRINT 1000
4134 * Define here KERN_TRACE so that we have one place to modify
4135 * it if we decide to change what log level the ftrace dump
4138 #define KERN_TRACE KERN_EMERG
4141 trace_printk_seq(struct trace_seq
*s
)
4143 /* Probably should print a warning here. */
4147 /* should be zero ended, but we are paranoid. */
4148 s
->buffer
[s
->len
] = 0;
4150 printk(KERN_TRACE
"%s", s
->buffer
);
4155 static void __ftrace_dump(bool disable_tracing
)
4157 static raw_spinlock_t ftrace_dump_lock
=
4158 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
4159 /* use static because iter can be a bit big for the stack */
4160 static struct trace_iterator iter
;
4161 unsigned int old_userobj
;
4162 static int dump_ran
;
4163 unsigned long flags
;
4167 local_irq_save(flags
);
4168 __raw_spin_lock(&ftrace_dump_lock
);
4176 if (disable_tracing
)
4179 for_each_tracing_cpu(cpu
) {
4180 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
4183 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
4185 /* don't look at user memory in panic mode */
4186 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4188 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4190 /* Simulate the iterator */
4191 iter
.tr
= &global_trace
;
4192 iter
.trace
= current_trace
;
4193 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4196 * We need to stop all tracing on all CPUS to read the
4197 * the next buffer. This is a bit expensive, but is
4198 * not done often. We fill all what we can read,
4199 * and then release the locks again.
4202 while (!trace_empty(&iter
)) {
4205 printk(KERN_TRACE
"---------------------------------\n");
4209 /* reset all but tr, trace, and overruns */
4210 memset(&iter
.seq
, 0,
4211 sizeof(struct trace_iterator
) -
4212 offsetof(struct trace_iterator
, seq
));
4213 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4216 if (find_next_entry_inc(&iter
) != NULL
) {
4219 ret
= print_trace_line(&iter
);
4220 if (ret
!= TRACE_TYPE_NO_CONSUME
)
4221 trace_consume(&iter
);
4224 trace_printk_seq(&iter
.seq
);
4228 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4230 printk(KERN_TRACE
"---------------------------------\n");
4232 /* Re-enable tracing if requested */
4233 if (!disable_tracing
) {
4234 trace_flags
|= old_userobj
;
4236 for_each_tracing_cpu(cpu
) {
4237 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
4243 __raw_spin_unlock(&ftrace_dump_lock
);
4244 local_irq_restore(flags
);
4247 /* By default: disable tracing after the dump */
4248 void ftrace_dump(void)
4250 __ftrace_dump(true);
4253 __init
static int tracer_alloc_buffers(void)
4259 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4262 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4263 goto out_free_buffer_mask
;
4265 if (!alloc_cpumask_var(&tracing_reader_cpumask
, GFP_KERNEL
))
4266 goto out_free_tracing_cpumask
;
4268 /* To save memory, keep the ring buffer size to its minimum */
4269 if (ring_buffer_expanded
)
4270 ring_buf_size
= trace_buf_size
;
4274 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4275 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4276 cpumask_clear(tracing_reader_cpumask
);
4278 /* TODO: make the number of buffers hot pluggable with CPUS */
4279 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
,
4280 TRACE_BUFFER_FLAGS
);
4281 if (!global_trace
.buffer
) {
4282 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4284 goto out_free_cpumask
;
4286 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4289 #ifdef CONFIG_TRACER_MAX_TRACE
4290 max_tr
.buffer
= ring_buffer_alloc(ring_buf_size
,
4291 TRACE_BUFFER_FLAGS
);
4292 if (!max_tr
.buffer
) {
4293 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4295 ring_buffer_free(global_trace
.buffer
);
4296 goto out_free_cpumask
;
4298 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
4299 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
4302 /* Allocate the first page for all buffers */
4303 for_each_tracing_cpu(i
) {
4304 global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4305 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
4308 trace_init_cmdlines();
4310 register_tracer(&nop_trace
);
4311 current_trace
= &nop_trace
;
4312 #ifdef CONFIG_BOOT_TRACER
4313 register_tracer(&boot_tracer
);
4315 /* All seems OK, enable tracing */
4316 tracing_disabled
= 0;
4318 atomic_notifier_chain_register(&panic_notifier_list
,
4319 &trace_panic_notifier
);
4321 register_die_notifier(&trace_die_notifier
);
4326 free_cpumask_var(tracing_reader_cpumask
);
4327 out_free_tracing_cpumask
:
4328 free_cpumask_var(tracing_cpumask
);
4329 out_free_buffer_mask
:
4330 free_cpumask_var(tracing_buffer_mask
);
4335 __init
static int clear_boot_tracer(void)
4338 * The default tracer at boot buffer is an init section.
4339 * This function is called in lateinit. If we did not
4340 * find the boot tracer, then clear it out, to prevent
4341 * later registration from accessing the buffer that is
4342 * about to be freed.
4344 if (!default_bootup_tracer
)
4347 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4348 default_bootup_tracer
);
4349 default_bootup_tracer
= NULL
;
4354 early_initcall(tracer_alloc_buffers
);
4355 fs_initcall(tracer_init_debugfs
);
4356 late_initcall(clear_boot_tracer
);