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 MAX_TRACER_SIZE 100
129 static char bootup_tracer_buf
[MAX_TRACER_SIZE
] __initdata
;
130 static char *default_bootup_tracer
;
132 static int __init
set_ftrace(char *str
)
134 strncpy(bootup_tracer_buf
, str
, MAX_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 * trace_types_lock is used to protect the trace_types list.
246 * This lock is also used to keep user access serialized.
247 * Accesses from userspace will grab this lock while userspace
248 * activities happen inside the kernel.
250 static DEFINE_MUTEX(trace_types_lock
);
252 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
253 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
255 /* trace_flags holds trace_options default values */
256 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
257 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
| TRACE_ITER_SLEEP_TIME
|
258 TRACE_ITER_GRAPH_TIME
;
260 static int trace_stop_count
;
261 static DEFINE_SPINLOCK(tracing_start_lock
);
264 * trace_wake_up - wake up tasks waiting for trace input
266 * Simply wakes up any task that is blocked on the trace_wait
267 * queue. These is used with trace_poll for tasks polling the trace.
269 void trace_wake_up(void)
272 * The runqueue_is_locked() can fail, but this is the best we
275 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
276 wake_up(&trace_wait
);
279 static int __init
set_buf_size(char *str
)
281 unsigned long buf_size
;
285 buf_size
= memparse(str
, &str
);
286 /* nr_entries can not be zero */
289 trace_buf_size
= buf_size
;
292 __setup("trace_buf_size=", set_buf_size
);
294 unsigned long nsecs_to_usecs(unsigned long nsecs
)
299 /* These must match the bit postions in trace_iterator_flags */
300 static const char *trace_options
[] = {
329 { trace_clock_local
, "local" },
330 { trace_clock_global
, "global" },
336 * trace_parser_get_init - gets the buffer for trace parser
338 int trace_parser_get_init(struct trace_parser
*parser
, int size
)
340 memset(parser
, 0, sizeof(*parser
));
342 parser
->buffer
= kmalloc(size
, GFP_KERNEL
);
351 * trace_parser_put - frees the buffer for trace parser
353 void trace_parser_put(struct trace_parser
*parser
)
355 kfree(parser
->buffer
);
359 * trace_get_user - reads the user input string separated by space
360 * (matched by isspace(ch))
362 * For each string found the 'struct trace_parser' is updated,
363 * and the function returns.
365 * Returns number of bytes read.
367 * See kernel/trace/trace.h for 'struct trace_parser' details.
369 int trace_get_user(struct trace_parser
*parser
, const char __user
*ubuf
,
370 size_t cnt
, loff_t
*ppos
)
377 trace_parser_clear(parser
);
379 ret
= get_user(ch
, ubuf
++);
387 * The parser is not finished with the last write,
388 * continue reading the user input without skipping spaces.
391 /* skip white space */
392 while (cnt
&& isspace(ch
)) {
393 ret
= get_user(ch
, ubuf
++);
400 /* only spaces were written */
410 /* read the non-space input */
411 while (cnt
&& !isspace(ch
)) {
412 if (parser
->idx
< parser
->size
)
413 parser
->buffer
[parser
->idx
++] = ch
;
418 ret
= get_user(ch
, ubuf
++);
425 /* We either got finished input or we have to wait for another call. */
427 parser
->buffer
[parser
->idx
] = 0;
428 parser
->cont
= false;
431 parser
->buffer
[parser
->idx
++] = ch
;
441 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
449 if (s
->len
<= s
->readpos
)
452 len
= s
->len
- s
->readpos
;
455 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
465 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
470 if (s
->len
<= s
->readpos
)
473 len
= s
->len
- s
->readpos
;
476 ret
= memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
485 * ftrace_max_lock is used to protect the swapping of buffers
486 * when taking a max snapshot. The buffers themselves are
487 * protected by per_cpu spinlocks. But the action of the swap
488 * needs its own lock.
490 * This is defined as a raw_spinlock_t in order to help
491 * with performance when lockdep debugging is enabled.
493 * It is also used in other places outside the update_max_tr
494 * so it needs to be defined outside of the
495 * CONFIG_TRACER_MAX_TRACE.
497 static raw_spinlock_t ftrace_max_lock
=
498 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
500 #ifdef CONFIG_TRACER_MAX_TRACE
501 unsigned long __read_mostly tracing_max_latency
;
502 unsigned long __read_mostly tracing_thresh
;
505 * Copy the new maximum trace into the separate maximum-trace
506 * structure. (this way the maximum trace is permanently saved,
507 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
510 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
512 struct trace_array_cpu
*data
= tr
->data
[cpu
];
513 struct trace_array_cpu
*max_data
= tr
->data
[cpu
];
516 max_tr
.time_start
= data
->preempt_timestamp
;
518 max_data
= max_tr
.data
[cpu
];
519 max_data
->saved_latency
= tracing_max_latency
;
520 max_data
->critical_start
= data
->critical_start
;
521 max_data
->critical_end
= data
->critical_end
;
523 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
524 max_data
->pid
= tsk
->pid
;
525 max_data
->uid
= task_uid(tsk
);
526 max_data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
527 max_data
->policy
= tsk
->policy
;
528 max_data
->rt_priority
= tsk
->rt_priority
;
530 /* record this tasks comm */
531 tracing_record_cmdline(tsk
);
535 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
537 * @tsk: the task with the latency
538 * @cpu: The cpu that initiated the trace.
540 * Flip the buffers between the @tr and the max_tr and record information
541 * about which task was the cause of this latency.
544 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
546 struct ring_buffer
*buf
= tr
->buffer
;
548 if (trace_stop_count
)
551 WARN_ON_ONCE(!irqs_disabled());
552 __raw_spin_lock(&ftrace_max_lock
);
554 tr
->buffer
= max_tr
.buffer
;
557 __update_max_tr(tr
, tsk
, cpu
);
558 __raw_spin_unlock(&ftrace_max_lock
);
562 * update_max_tr_single - only copy one trace over, and reset the rest
564 * @tsk - task with the latency
565 * @cpu - the cpu of the buffer to copy.
567 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
570 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
574 if (trace_stop_count
)
577 WARN_ON_ONCE(!irqs_disabled());
578 __raw_spin_lock(&ftrace_max_lock
);
580 ftrace_disable_cpu();
582 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
586 * We failed to swap the buffer due to a commit taking
587 * place on this CPU. We fail to record, but we reset
588 * the max trace buffer (no one writes directly to it)
589 * and flag that it failed.
591 trace_array_printk(&max_tr
, _THIS_IP_
,
592 "Failed to swap buffers due to commit in progress\n");
597 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
&& ret
!= -EBUSY
);
599 __update_max_tr(tr
, tsk
, cpu
);
600 __raw_spin_unlock(&ftrace_max_lock
);
602 #endif /* CONFIG_TRACER_MAX_TRACE */
605 * register_tracer - register a tracer with the ftrace system.
606 * @type - the plugin for the tracer
608 * Register a new plugin tracer.
610 int register_tracer(struct tracer
*type
)
611 __releases(kernel_lock
)
612 __acquires(kernel_lock
)
618 pr_info("Tracer must have a name\n");
622 if (strlen(type
->name
) > MAX_TRACER_SIZE
) {
623 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE
);
628 * When this gets called we hold the BKL which means that
629 * preemption is disabled. Various trace selftests however
630 * need to disable and enable preemption for successful tests.
631 * So we drop the BKL here and grab it after the tests again.
634 mutex_lock(&trace_types_lock
);
636 tracing_selftest_running
= true;
638 for (t
= trace_types
; t
; t
= t
->next
) {
639 if (strcmp(type
->name
, t
->name
) == 0) {
641 pr_info("Tracer %s already registered\n",
649 type
->set_flag
= &dummy_set_flag
;
651 type
->flags
= &dummy_tracer_flags
;
653 if (!type
->flags
->opts
)
654 type
->flags
->opts
= dummy_tracer_opt
;
655 if (!type
->wait_pipe
)
656 type
->wait_pipe
= default_wait_pipe
;
659 #ifdef CONFIG_FTRACE_STARTUP_TEST
660 if (type
->selftest
&& !tracing_selftest_disabled
) {
661 struct tracer
*saved_tracer
= current_trace
;
662 struct trace_array
*tr
= &global_trace
;
665 * Run a selftest on this tracer.
666 * Here we reset the trace buffer, and set the current
667 * tracer to be this tracer. The tracer can then run some
668 * internal tracing to verify that everything is in order.
669 * If we fail, we do not register this tracer.
671 tracing_reset_online_cpus(tr
);
673 current_trace
= type
;
674 /* the test is responsible for initializing and enabling */
675 pr_info("Testing tracer %s: ", type
->name
);
676 ret
= type
->selftest(type
, tr
);
677 /* the test is responsible for resetting too */
678 current_trace
= saved_tracer
;
680 printk(KERN_CONT
"FAILED!\n");
683 /* Only reset on passing, to avoid touching corrupted buffers */
684 tracing_reset_online_cpus(tr
);
686 printk(KERN_CONT
"PASSED\n");
690 type
->next
= trace_types
;
694 tracing_selftest_running
= false;
695 mutex_unlock(&trace_types_lock
);
697 if (ret
|| !default_bootup_tracer
)
700 if (strncmp(default_bootup_tracer
, type
->name
, MAX_TRACER_SIZE
))
703 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
704 /* Do we want this tracer to start on bootup? */
705 tracing_set_tracer(type
->name
);
706 default_bootup_tracer
= NULL
;
707 /* disable other selftests, since this will break it. */
708 tracing_selftest_disabled
= 1;
709 #ifdef CONFIG_FTRACE_STARTUP_TEST
710 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
719 void unregister_tracer(struct tracer
*type
)
723 mutex_lock(&trace_types_lock
);
724 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
728 pr_info("Tracer %s not registered\n", type
->name
);
734 if (type
== current_trace
&& tracer_enabled
) {
737 if (current_trace
->stop
)
738 current_trace
->stop(&global_trace
);
739 current_trace
= &nop_trace
;
742 mutex_unlock(&trace_types_lock
);
745 static void __tracing_reset(struct trace_array
*tr
, int cpu
)
747 ftrace_disable_cpu();
748 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
752 void tracing_reset(struct trace_array
*tr
, int cpu
)
754 struct ring_buffer
*buffer
= tr
->buffer
;
756 ring_buffer_record_disable(buffer
);
758 /* Make sure all commits have finished */
760 __tracing_reset(tr
, cpu
);
762 ring_buffer_record_enable(buffer
);
765 void tracing_reset_online_cpus(struct trace_array
*tr
)
767 struct ring_buffer
*buffer
= tr
->buffer
;
770 ring_buffer_record_disable(buffer
);
772 /* Make sure all commits have finished */
775 tr
->time_start
= ftrace_now(tr
->cpu
);
777 for_each_online_cpu(cpu
)
778 __tracing_reset(tr
, cpu
);
780 ring_buffer_record_enable(buffer
);
783 void tracing_reset_current(int cpu
)
785 tracing_reset(&global_trace
, cpu
);
788 void tracing_reset_current_online_cpus(void)
790 tracing_reset_online_cpus(&global_trace
);
793 #define SAVED_CMDLINES 128
794 #define NO_CMDLINE_MAP UINT_MAX
795 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
796 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
797 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
798 static int cmdline_idx
;
799 static raw_spinlock_t trace_cmdline_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
801 /* temporary disable recording */
802 static atomic_t trace_record_cmdline_disabled __read_mostly
;
804 static void trace_init_cmdlines(void)
806 memset(&map_pid_to_cmdline
, NO_CMDLINE_MAP
, sizeof(map_pid_to_cmdline
));
807 memset(&map_cmdline_to_pid
, NO_CMDLINE_MAP
, sizeof(map_cmdline_to_pid
));
811 int is_tracing_stopped(void)
813 return trace_stop_count
;
817 * ftrace_off_permanent - disable all ftrace code permanently
819 * This should only be called when a serious anomally has
820 * been detected. This will turn off the function tracing,
821 * ring buffers, and other tracing utilites. It takes no
822 * locks and can be called from any context.
824 void ftrace_off_permanent(void)
826 tracing_disabled
= 1;
828 tracing_off_permanent();
832 * tracing_start - quick start of the tracer
834 * If tracing is enabled but was stopped by tracing_stop,
835 * this will start the tracer back up.
837 void tracing_start(void)
839 struct ring_buffer
*buffer
;
842 if (tracing_disabled
)
845 spin_lock_irqsave(&tracing_start_lock
, flags
);
846 if (--trace_stop_count
) {
847 if (trace_stop_count
< 0) {
848 /* Someone screwed up their debugging */
850 trace_stop_count
= 0;
856 buffer
= global_trace
.buffer
;
858 ring_buffer_record_enable(buffer
);
860 buffer
= max_tr
.buffer
;
862 ring_buffer_record_enable(buffer
);
866 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
870 * tracing_stop - quick stop of the tracer
872 * Light weight way to stop tracing. Use in conjunction with
875 void tracing_stop(void)
877 struct ring_buffer
*buffer
;
881 spin_lock_irqsave(&tracing_start_lock
, flags
);
882 if (trace_stop_count
++)
885 buffer
= global_trace
.buffer
;
887 ring_buffer_record_disable(buffer
);
889 buffer
= max_tr
.buffer
;
891 ring_buffer_record_disable(buffer
);
894 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
897 void trace_stop_cmdline_recording(void);
899 static void trace_save_cmdline(struct task_struct
*tsk
)
903 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
907 * It's not the end of the world if we don't get
908 * the lock, but we also don't want to spin
909 * nor do we want to disable interrupts,
910 * so if we miss here, then better luck next time.
912 if (!__raw_spin_trylock(&trace_cmdline_lock
))
915 idx
= map_pid_to_cmdline
[tsk
->pid
];
916 if (idx
== NO_CMDLINE_MAP
) {
917 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
920 * Check whether the cmdline buffer at idx has a pid
921 * mapped. We are going to overwrite that entry so we
922 * need to clear the map_pid_to_cmdline. Otherwise we
923 * would read the new comm for the old pid.
925 pid
= map_cmdline_to_pid
[idx
];
926 if (pid
!= NO_CMDLINE_MAP
)
927 map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
929 map_cmdline_to_pid
[idx
] = tsk
->pid
;
930 map_pid_to_cmdline
[tsk
->pid
] = idx
;
935 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
937 __raw_spin_unlock(&trace_cmdline_lock
);
940 void trace_find_cmdline(int pid
, char comm
[])
945 strcpy(comm
, "<idle>");
949 if (pid
> PID_MAX_DEFAULT
) {
950 strcpy(comm
, "<...>");
955 __raw_spin_lock(&trace_cmdline_lock
);
956 map
= map_pid_to_cmdline
[pid
];
957 if (map
!= NO_CMDLINE_MAP
)
958 strcpy(comm
, saved_cmdlines
[map
]);
960 strcpy(comm
, "<...>");
962 __raw_spin_unlock(&trace_cmdline_lock
);
966 void tracing_record_cmdline(struct task_struct
*tsk
)
968 if (atomic_read(&trace_record_cmdline_disabled
) || !tracer_enabled
||
972 trace_save_cmdline(tsk
);
976 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
979 struct task_struct
*tsk
= current
;
981 entry
->preempt_count
= pc
& 0xff;
982 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
983 entry
->lock_depth
= (tsk
) ? tsk
->lock_depth
: 0;
985 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
986 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
988 TRACE_FLAG_IRQS_NOSUPPORT
|
990 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
991 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
992 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
994 EXPORT_SYMBOL_GPL(tracing_generic_entry_update
);
996 struct ring_buffer_event
*
997 trace_buffer_lock_reserve(struct ring_buffer
*buffer
,
1000 unsigned long flags
, int pc
)
1002 struct ring_buffer_event
*event
;
1004 event
= ring_buffer_lock_reserve(buffer
, len
);
1005 if (event
!= NULL
) {
1006 struct trace_entry
*ent
= ring_buffer_event_data(event
);
1008 tracing_generic_entry_update(ent
, flags
, pc
);
1016 __trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1017 struct ring_buffer_event
*event
,
1018 unsigned long flags
, int pc
,
1021 ring_buffer_unlock_commit(buffer
, event
);
1023 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1024 ftrace_trace_userstack(buffer
, flags
, pc
);
1030 void trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1031 struct ring_buffer_event
*event
,
1032 unsigned long flags
, int pc
)
1034 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
1037 struct ring_buffer_event
*
1038 trace_current_buffer_lock_reserve(struct ring_buffer
**current_rb
,
1039 int type
, unsigned long len
,
1040 unsigned long flags
, int pc
)
1042 *current_rb
= global_trace
.buffer
;
1043 return trace_buffer_lock_reserve(*current_rb
,
1044 type
, len
, flags
, pc
);
1046 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve
);
1048 void trace_current_buffer_unlock_commit(struct ring_buffer
*buffer
,
1049 struct ring_buffer_event
*event
,
1050 unsigned long flags
, int pc
)
1052 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
1054 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit
);
1056 void trace_nowake_buffer_unlock_commit(struct ring_buffer
*buffer
,
1057 struct ring_buffer_event
*event
,
1058 unsigned long flags
, int pc
)
1060 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 0);
1062 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit
);
1064 void trace_current_buffer_discard_commit(struct ring_buffer
*buffer
,
1065 struct ring_buffer_event
*event
)
1067 ring_buffer_discard_commit(buffer
, event
);
1069 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
1072 trace_function(struct trace_array
*tr
,
1073 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1076 struct ftrace_event_call
*call
= &event_function
;
1077 struct ring_buffer
*buffer
= tr
->buffer
;
1078 struct ring_buffer_event
*event
;
1079 struct ftrace_entry
*entry
;
1081 /* If we are reading the ring buffer, don't trace */
1082 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
1085 event
= trace_buffer_lock_reserve(buffer
, TRACE_FN
, sizeof(*entry
),
1089 entry
= ring_buffer_event_data(event
);
1091 entry
->parent_ip
= parent_ip
;
1093 if (!filter_check_discard(call
, entry
, buffer
, event
))
1094 ring_buffer_unlock_commit(buffer
, event
);
1098 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
1099 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1102 if (likely(!atomic_read(&data
->disabled
)))
1103 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
1106 #ifdef CONFIG_STACKTRACE
1107 static void __ftrace_trace_stack(struct ring_buffer
*buffer
,
1108 unsigned long flags
,
1111 struct ftrace_event_call
*call
= &event_kernel_stack
;
1112 struct ring_buffer_event
*event
;
1113 struct stack_entry
*entry
;
1114 struct stack_trace trace
;
1116 event
= trace_buffer_lock_reserve(buffer
, TRACE_STACK
,
1117 sizeof(*entry
), flags
, pc
);
1120 entry
= ring_buffer_event_data(event
);
1121 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1123 trace
.nr_entries
= 0;
1124 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1126 trace
.entries
= entry
->caller
;
1128 save_stack_trace(&trace
);
1129 if (!filter_check_discard(call
, entry
, buffer
, event
))
1130 ring_buffer_unlock_commit(buffer
, event
);
1133 void ftrace_trace_stack(struct ring_buffer
*buffer
, unsigned long flags
,
1136 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1139 __ftrace_trace_stack(buffer
, flags
, skip
, pc
);
1142 void __trace_stack(struct trace_array
*tr
, unsigned long flags
, int skip
,
1145 __ftrace_trace_stack(tr
->buffer
, flags
, skip
, pc
);
1149 ftrace_trace_userstack(struct ring_buffer
*buffer
, unsigned long flags
, int pc
)
1151 struct ftrace_event_call
*call
= &event_user_stack
;
1152 struct ring_buffer_event
*event
;
1153 struct userstack_entry
*entry
;
1154 struct stack_trace trace
;
1156 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1159 event
= trace_buffer_lock_reserve(buffer
, TRACE_USER_STACK
,
1160 sizeof(*entry
), flags
, pc
);
1163 entry
= ring_buffer_event_data(event
);
1165 entry
->tgid
= current
->tgid
;
1166 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1168 trace
.nr_entries
= 0;
1169 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1171 trace
.entries
= entry
->caller
;
1173 save_stack_trace_user(&trace
);
1174 if (!filter_check_discard(call
, entry
, buffer
, event
))
1175 ring_buffer_unlock_commit(buffer
, event
);
1179 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1181 ftrace_trace_userstack(tr
, flags
, preempt_count());
1185 #endif /* CONFIG_STACKTRACE */
1188 ftrace_trace_special(void *__tr
,
1189 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1192 struct ftrace_event_call
*call
= &event_special
;
1193 struct ring_buffer_event
*event
;
1194 struct trace_array
*tr
= __tr
;
1195 struct ring_buffer
*buffer
= tr
->buffer
;
1196 struct special_entry
*entry
;
1198 event
= trace_buffer_lock_reserve(buffer
, TRACE_SPECIAL
,
1199 sizeof(*entry
), 0, pc
);
1202 entry
= ring_buffer_event_data(event
);
1207 if (!filter_check_discard(call
, entry
, buffer
, event
))
1208 trace_buffer_unlock_commit(buffer
, event
, 0, pc
);
1212 __trace_special(void *__tr
, void *__data
,
1213 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1215 ftrace_trace_special(__tr
, arg1
, arg2
, arg3
, preempt_count());
1219 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1221 struct trace_array
*tr
= &global_trace
;
1222 struct trace_array_cpu
*data
;
1223 unsigned long flags
;
1227 if (tracing_disabled
)
1230 pc
= preempt_count();
1231 local_irq_save(flags
);
1232 cpu
= raw_smp_processor_id();
1233 data
= tr
->data
[cpu
];
1235 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1236 ftrace_trace_special(tr
, arg1
, arg2
, arg3
, pc
);
1238 atomic_dec(&data
->disabled
);
1239 local_irq_restore(flags
);
1243 * trace_vbprintk - write binary msg to tracing buffer
1246 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1248 static raw_spinlock_t trace_buf_lock
=
1249 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
1250 static u32 trace_buf
[TRACE_BUF_SIZE
];
1252 struct ftrace_event_call
*call
= &event_bprint
;
1253 struct ring_buffer_event
*event
;
1254 struct ring_buffer
*buffer
;
1255 struct trace_array
*tr
= &global_trace
;
1256 struct trace_array_cpu
*data
;
1257 struct bprint_entry
*entry
;
1258 unsigned long flags
;
1261 int cpu
, len
= 0, size
, pc
;
1263 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1266 /* Don't pollute graph traces with trace_vprintk internals */
1267 pause_graph_tracing();
1269 pc
= preempt_count();
1270 resched
= ftrace_preempt_disable();
1271 cpu
= raw_smp_processor_id();
1272 data
= tr
->data
[cpu
];
1274 disable
= atomic_inc_return(&data
->disabled
);
1275 if (unlikely(disable
!= 1))
1278 /* Lockdep uses trace_printk for lock tracing */
1279 local_irq_save(flags
);
1280 __raw_spin_lock(&trace_buf_lock
);
1281 len
= vbin_printf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1283 if (len
> TRACE_BUF_SIZE
|| len
< 0)
1286 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1287 buffer
= tr
->buffer
;
1288 event
= trace_buffer_lock_reserve(buffer
, TRACE_BPRINT
, size
,
1292 entry
= ring_buffer_event_data(event
);
1296 memcpy(entry
->buf
, trace_buf
, sizeof(u32
) * len
);
1297 if (!filter_check_discard(call
, entry
, buffer
, event
))
1298 ring_buffer_unlock_commit(buffer
, event
);
1301 __raw_spin_unlock(&trace_buf_lock
);
1302 local_irq_restore(flags
);
1305 atomic_dec_return(&data
->disabled
);
1306 ftrace_preempt_enable(resched
);
1307 unpause_graph_tracing();
1311 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1313 int trace_array_printk(struct trace_array
*tr
,
1314 unsigned long ip
, const char *fmt
, ...)
1319 if (!(trace_flags
& TRACE_ITER_PRINTK
))
1323 ret
= trace_array_vprintk(tr
, ip
, fmt
, ap
);
1328 int trace_array_vprintk(struct trace_array
*tr
,
1329 unsigned long ip
, const char *fmt
, va_list args
)
1331 static raw_spinlock_t trace_buf_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
1332 static char trace_buf
[TRACE_BUF_SIZE
];
1334 struct ftrace_event_call
*call
= &event_print
;
1335 struct ring_buffer_event
*event
;
1336 struct ring_buffer
*buffer
;
1337 struct trace_array_cpu
*data
;
1338 int cpu
, len
= 0, size
, pc
;
1339 struct print_entry
*entry
;
1340 unsigned long irq_flags
;
1343 if (tracing_disabled
|| tracing_selftest_running
)
1346 pc
= preempt_count();
1347 preempt_disable_notrace();
1348 cpu
= raw_smp_processor_id();
1349 data
= tr
->data
[cpu
];
1351 disable
= atomic_inc_return(&data
->disabled
);
1352 if (unlikely(disable
!= 1))
1355 pause_graph_tracing();
1356 raw_local_irq_save(irq_flags
);
1357 __raw_spin_lock(&trace_buf_lock
);
1358 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1360 len
= min(len
, TRACE_BUF_SIZE
-1);
1363 size
= sizeof(*entry
) + len
+ 1;
1364 buffer
= tr
->buffer
;
1365 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
1369 entry
= ring_buffer_event_data(event
);
1372 memcpy(&entry
->buf
, trace_buf
, len
);
1373 entry
->buf
[len
] = 0;
1374 if (!filter_check_discard(call
, entry
, buffer
, event
))
1375 ring_buffer_unlock_commit(buffer
, event
);
1378 __raw_spin_unlock(&trace_buf_lock
);
1379 raw_local_irq_restore(irq_flags
);
1380 unpause_graph_tracing();
1382 atomic_dec_return(&data
->disabled
);
1383 preempt_enable_notrace();
1388 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1390 return trace_array_printk(&global_trace
, ip
, fmt
, args
);
1392 EXPORT_SYMBOL_GPL(trace_vprintk
);
1394 enum trace_file_type
{
1395 TRACE_FILE_LAT_FMT
= 1,
1396 TRACE_FILE_ANNOTATE
= 2,
1399 static void trace_iterator_increment(struct trace_iterator
*iter
)
1401 /* Don't allow ftrace to trace into the ring buffers */
1402 ftrace_disable_cpu();
1405 if (iter
->buffer_iter
[iter
->cpu
])
1406 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1408 ftrace_enable_cpu();
1411 static struct trace_entry
*
1412 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1414 struct ring_buffer_event
*event
;
1415 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1417 /* Don't allow ftrace to trace into the ring buffers */
1418 ftrace_disable_cpu();
1421 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1423 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1425 ftrace_enable_cpu();
1427 return event
? ring_buffer_event_data(event
) : NULL
;
1430 static struct trace_entry
*
1431 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1433 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1434 struct trace_entry
*ent
, *next
= NULL
;
1435 int cpu_file
= iter
->cpu_file
;
1436 u64 next_ts
= 0, ts
;
1441 * If we are in a per_cpu trace file, don't bother by iterating over
1442 * all cpu and peek directly.
1444 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1445 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1447 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
);
1449 *ent_cpu
= cpu_file
;
1454 for_each_tracing_cpu(cpu
) {
1456 if (ring_buffer_empty_cpu(buffer
, cpu
))
1459 ent
= peek_next_entry(iter
, cpu
, &ts
);
1462 * Pick the entry with the smallest timestamp:
1464 if (ent
&& (!next
|| ts
< next_ts
)) {
1472 *ent_cpu
= next_cpu
;
1480 /* Find the next real entry, without updating the iterator itself */
1481 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1482 int *ent_cpu
, u64
*ent_ts
)
1484 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1487 /* Find the next real entry, and increment the iterator to the next entry */
1488 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1490 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1493 trace_iterator_increment(iter
);
1495 return iter
->ent
? iter
: NULL
;
1498 static void trace_consume(struct trace_iterator
*iter
)
1500 /* Don't allow ftrace to trace into the ring buffers */
1501 ftrace_disable_cpu();
1502 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1503 ftrace_enable_cpu();
1506 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1508 struct trace_iterator
*iter
= m
->private;
1514 /* can't go backwards */
1519 ent
= find_next_entry_inc(iter
);
1523 while (ent
&& iter
->idx
< i
)
1524 ent
= find_next_entry_inc(iter
);
1531 static void tracing_iter_reset(struct trace_iterator
*iter
, int cpu
)
1533 struct trace_array
*tr
= iter
->tr
;
1534 struct ring_buffer_event
*event
;
1535 struct ring_buffer_iter
*buf_iter
;
1536 unsigned long entries
= 0;
1539 tr
->data
[cpu
]->skipped_entries
= 0;
1541 if (!iter
->buffer_iter
[cpu
])
1544 buf_iter
= iter
->buffer_iter
[cpu
];
1545 ring_buffer_iter_reset(buf_iter
);
1548 * We could have the case with the max latency tracers
1549 * that a reset never took place on a cpu. This is evident
1550 * by the timestamp being before the start of the buffer.
1552 while ((event
= ring_buffer_iter_peek(buf_iter
, &ts
))) {
1553 if (ts
>= iter
->tr
->time_start
)
1556 ring_buffer_read(buf_iter
, NULL
);
1559 tr
->data
[cpu
]->skipped_entries
= entries
;
1563 * No necessary locking here. The worst thing which can
1564 * happen is loosing events consumed at the same time
1565 * by a trace_pipe reader.
1566 * Other than that, we don't risk to crash the ring buffer
1567 * because it serializes the readers.
1569 * The current tracer is copied to avoid a global locking
1572 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1574 struct trace_iterator
*iter
= m
->private;
1575 static struct tracer
*old_tracer
;
1576 int cpu_file
= iter
->cpu_file
;
1581 /* copy the tracer to avoid using a global lock all around */
1582 mutex_lock(&trace_types_lock
);
1583 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1584 old_tracer
= current_trace
;
1585 *iter
->trace
= *current_trace
;
1587 mutex_unlock(&trace_types_lock
);
1589 atomic_inc(&trace_record_cmdline_disabled
);
1591 if (*pos
!= iter
->pos
) {
1596 ftrace_disable_cpu();
1598 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1599 for_each_tracing_cpu(cpu
)
1600 tracing_iter_reset(iter
, cpu
);
1602 tracing_iter_reset(iter
, cpu_file
);
1604 ftrace_enable_cpu();
1606 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1611 p
= s_next(m
, p
, &l
);
1614 trace_event_read_lock();
1618 static void s_stop(struct seq_file
*m
, void *p
)
1620 atomic_dec(&trace_record_cmdline_disabled
);
1621 trace_event_read_unlock();
1624 static void print_lat_help_header(struct seq_file
*m
)
1626 seq_puts(m
, "# _------=> CPU# \n");
1627 seq_puts(m
, "# / _-----=> irqs-off \n");
1628 seq_puts(m
, "# | / _----=> need-resched \n");
1629 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1630 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1631 seq_puts(m
, "# |||| /_--=> lock-depth \n");
1632 seq_puts(m
, "# |||||/ delay \n");
1633 seq_puts(m
, "# cmd pid |||||| time | caller \n");
1634 seq_puts(m
, "# \\ / |||||| \\ | / \n");
1637 static void print_func_help_header(struct seq_file
*m
)
1639 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1640 seq_puts(m
, "# | | | | |\n");
1645 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1647 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1648 struct trace_array
*tr
= iter
->tr
;
1649 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1650 struct tracer
*type
= current_trace
;
1651 unsigned long entries
= 0;
1652 unsigned long total
= 0;
1653 unsigned long count
;
1654 const char *name
= "preemption";
1661 for_each_tracing_cpu(cpu
) {
1662 count
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
1664 * If this buffer has skipped entries, then we hold all
1665 * entries for the trace and we need to ignore the
1666 * ones before the time stamp.
1668 if (tr
->data
[cpu
]->skipped_entries
) {
1669 count
-= tr
->data
[cpu
]->skipped_entries
;
1670 /* total is the same as the entries */
1674 ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
1678 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
1680 seq_puts(m
, "# -----------------------------------"
1681 "---------------------------------\n");
1682 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1683 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1684 nsecs_to_usecs(data
->saved_latency
),
1688 #if defined(CONFIG_PREEMPT_NONE)
1690 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1692 #elif defined(CONFIG_PREEMPT)
1697 /* These are reserved for later use */
1700 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1704 seq_puts(m
, "# -----------------\n");
1705 seq_printf(m
, "# | task: %.16s-%d "
1706 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1707 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1708 data
->policy
, data
->rt_priority
);
1709 seq_puts(m
, "# -----------------\n");
1711 if (data
->critical_start
) {
1712 seq_puts(m
, "# => started at: ");
1713 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1714 trace_print_seq(m
, &iter
->seq
);
1715 seq_puts(m
, "\n# => ended at: ");
1716 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1717 trace_print_seq(m
, &iter
->seq
);
1718 seq_puts(m
, "\n#\n");
1724 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1726 struct trace_seq
*s
= &iter
->seq
;
1728 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1731 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1734 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1737 if (iter
->tr
->data
[iter
->cpu
]->skipped_entries
)
1740 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1742 /* Don't print started cpu buffer for the first entry of the trace */
1744 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
1748 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1750 struct trace_seq
*s
= &iter
->seq
;
1751 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1752 struct trace_entry
*entry
;
1753 struct trace_event
*event
;
1757 test_cpu_buff_start(iter
);
1759 event
= ftrace_find_event(entry
->type
);
1761 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1762 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1763 if (!trace_print_lat_context(iter
))
1766 if (!trace_print_context(iter
))
1772 return event
->trace(iter
, sym_flags
);
1774 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1777 return TRACE_TYPE_HANDLED
;
1779 return TRACE_TYPE_PARTIAL_LINE
;
1782 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1784 struct trace_seq
*s
= &iter
->seq
;
1785 struct trace_entry
*entry
;
1786 struct trace_event
*event
;
1790 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1791 if (!trace_seq_printf(s
, "%d %d %llu ",
1792 entry
->pid
, iter
->cpu
, iter
->ts
))
1796 event
= ftrace_find_event(entry
->type
);
1798 return event
->raw(iter
, 0);
1800 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1803 return TRACE_TYPE_HANDLED
;
1805 return TRACE_TYPE_PARTIAL_LINE
;
1808 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1810 struct trace_seq
*s
= &iter
->seq
;
1811 unsigned char newline
= '\n';
1812 struct trace_entry
*entry
;
1813 struct trace_event
*event
;
1817 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1818 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1819 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1820 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1823 event
= ftrace_find_event(entry
->type
);
1825 enum print_line_t ret
= event
->hex(iter
, 0);
1826 if (ret
!= TRACE_TYPE_HANDLED
)
1830 SEQ_PUT_FIELD_RET(s
, newline
);
1832 return TRACE_TYPE_HANDLED
;
1835 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1837 struct trace_seq
*s
= &iter
->seq
;
1838 struct trace_entry
*entry
;
1839 struct trace_event
*event
;
1843 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1844 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1845 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1846 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1849 event
= ftrace_find_event(entry
->type
);
1850 return event
? event
->binary(iter
, 0) : TRACE_TYPE_HANDLED
;
1853 static int trace_empty(struct trace_iterator
*iter
)
1857 /* If we are looking at one CPU buffer, only check that one */
1858 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
1859 cpu
= iter
->cpu_file
;
1860 if (iter
->buffer_iter
[cpu
]) {
1861 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1864 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1870 for_each_tracing_cpu(cpu
) {
1871 if (iter
->buffer_iter
[cpu
]) {
1872 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1875 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1883 /* Called with trace_event_read_lock() held. */
1884 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1886 enum print_line_t ret
;
1888 if (iter
->trace
&& iter
->trace
->print_line
) {
1889 ret
= iter
->trace
->print_line(iter
);
1890 if (ret
!= TRACE_TYPE_UNHANDLED
)
1894 if (iter
->ent
->type
== TRACE_BPRINT
&&
1895 trace_flags
& TRACE_ITER_PRINTK
&&
1896 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1897 return trace_print_bprintk_msg_only(iter
);
1899 if (iter
->ent
->type
== TRACE_PRINT
&&
1900 trace_flags
& TRACE_ITER_PRINTK
&&
1901 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1902 return trace_print_printk_msg_only(iter
);
1904 if (trace_flags
& TRACE_ITER_BIN
)
1905 return print_bin_fmt(iter
);
1907 if (trace_flags
& TRACE_ITER_HEX
)
1908 return print_hex_fmt(iter
);
1910 if (trace_flags
& TRACE_ITER_RAW
)
1911 return print_raw_fmt(iter
);
1913 return print_trace_fmt(iter
);
1916 static int s_show(struct seq_file
*m
, void *v
)
1918 struct trace_iterator
*iter
= v
;
1920 if (iter
->ent
== NULL
) {
1922 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1925 if (iter
->trace
&& iter
->trace
->print_header
)
1926 iter
->trace
->print_header(m
);
1927 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1928 /* print nothing if the buffers are empty */
1929 if (trace_empty(iter
))
1931 print_trace_header(m
, iter
);
1932 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1933 print_lat_help_header(m
);
1935 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1936 print_func_help_header(m
);
1939 print_trace_line(iter
);
1940 trace_print_seq(m
, &iter
->seq
);
1946 static struct seq_operations tracer_seq_ops
= {
1953 static struct trace_iterator
*
1954 __tracing_open(struct inode
*inode
, struct file
*file
)
1956 long cpu_file
= (long) inode
->i_private
;
1957 void *fail_ret
= ERR_PTR(-ENOMEM
);
1958 struct trace_iterator
*iter
;
1962 if (tracing_disabled
)
1963 return ERR_PTR(-ENODEV
);
1965 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1967 return ERR_PTR(-ENOMEM
);
1970 * We make a copy of the current tracer to avoid concurrent
1971 * changes on it while we are reading.
1973 mutex_lock(&trace_types_lock
);
1974 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
1979 *iter
->trace
= *current_trace
;
1981 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
1984 cpumask_clear(iter
->started
);
1986 if (current_trace
&& current_trace
->print_max
)
1989 iter
->tr
= &global_trace
;
1991 mutex_init(&iter
->mutex
);
1992 iter
->cpu_file
= cpu_file
;
1994 /* Notify the tracer early; before we stop tracing. */
1995 if (iter
->trace
&& iter
->trace
->open
)
1996 iter
->trace
->open(iter
);
1998 /* Annotate start of buffers if we had overruns */
1999 if (ring_buffer_overruns(iter
->tr
->buffer
))
2000 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
2002 /* stop the trace while dumping */
2005 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
2006 for_each_tracing_cpu(cpu
) {
2008 iter
->buffer_iter
[cpu
] =
2009 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
2010 tracing_iter_reset(iter
, cpu
);
2013 cpu
= iter
->cpu_file
;
2014 iter
->buffer_iter
[cpu
] =
2015 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
2016 tracing_iter_reset(iter
, cpu
);
2019 ret
= seq_open(file
, &tracer_seq_ops
);
2021 fail_ret
= ERR_PTR(ret
);
2025 m
= file
->private_data
;
2028 mutex_unlock(&trace_types_lock
);
2033 for_each_tracing_cpu(cpu
) {
2034 if (iter
->buffer_iter
[cpu
])
2035 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2037 free_cpumask_var(iter
->started
);
2040 mutex_unlock(&trace_types_lock
);
2047 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
2049 if (tracing_disabled
)
2052 filp
->private_data
= inode
->i_private
;
2056 static int tracing_release(struct inode
*inode
, struct file
*file
)
2058 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2059 struct trace_iterator
*iter
;
2062 if (!(file
->f_mode
& FMODE_READ
))
2067 mutex_lock(&trace_types_lock
);
2068 for_each_tracing_cpu(cpu
) {
2069 if (iter
->buffer_iter
[cpu
])
2070 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2073 if (iter
->trace
&& iter
->trace
->close
)
2074 iter
->trace
->close(iter
);
2076 /* reenable tracing if it was previously enabled */
2078 mutex_unlock(&trace_types_lock
);
2080 seq_release(inode
, file
);
2081 mutex_destroy(&iter
->mutex
);
2082 free_cpumask_var(iter
->started
);
2088 static int tracing_open(struct inode
*inode
, struct file
*file
)
2090 struct trace_iterator
*iter
;
2093 /* If this file was open for write, then erase contents */
2094 if ((file
->f_mode
& FMODE_WRITE
) &&
2095 (file
->f_flags
& O_TRUNC
)) {
2096 long cpu
= (long) inode
->i_private
;
2098 if (cpu
== TRACE_PIPE_ALL_CPU
)
2099 tracing_reset_online_cpus(&global_trace
);
2101 tracing_reset(&global_trace
, cpu
);
2104 if (file
->f_mode
& FMODE_READ
) {
2105 iter
= __tracing_open(inode
, file
);
2107 ret
= PTR_ERR(iter
);
2108 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2109 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2115 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2117 struct tracer
*t
= v
;
2127 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2132 mutex_lock(&trace_types_lock
);
2133 for (t
= trace_types
; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2139 static void t_stop(struct seq_file
*m
, void *p
)
2141 mutex_unlock(&trace_types_lock
);
2144 static int t_show(struct seq_file
*m
, void *v
)
2146 struct tracer
*t
= v
;
2151 seq_printf(m
, "%s", t
->name
);
2160 static struct seq_operations show_traces_seq_ops
= {
2167 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2169 if (tracing_disabled
)
2172 return seq_open(file
, &show_traces_seq_ops
);
2176 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
2177 size_t count
, loff_t
*ppos
)
2182 static const struct file_operations tracing_fops
= {
2183 .open
= tracing_open
,
2185 .write
= tracing_write_stub
,
2186 .llseek
= seq_lseek
,
2187 .release
= tracing_release
,
2190 static const struct file_operations show_traces_fops
= {
2191 .open
= show_traces_open
,
2193 .release
= seq_release
,
2197 * Only trace on a CPU if the bitmask is set:
2199 static cpumask_var_t tracing_cpumask
;
2202 * The tracer itself will not take this lock, but still we want
2203 * to provide a consistent cpumask to user-space:
2205 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2208 * Temporary storage for the character representation of the
2209 * CPU bitmask (and one more byte for the newline):
2211 static char mask_str
[NR_CPUS
+ 1];
2214 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2215 size_t count
, loff_t
*ppos
)
2219 mutex_lock(&tracing_cpumask_update_lock
);
2221 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2222 if (count
- len
< 2) {
2226 len
+= sprintf(mask_str
+ len
, "\n");
2227 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2230 mutex_unlock(&tracing_cpumask_update_lock
);
2236 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2237 size_t count
, loff_t
*ppos
)
2240 cpumask_var_t tracing_cpumask_new
;
2242 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2245 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2249 mutex_lock(&tracing_cpumask_update_lock
);
2251 local_irq_disable();
2252 __raw_spin_lock(&ftrace_max_lock
);
2253 for_each_tracing_cpu(cpu
) {
2255 * Increase/decrease the disabled counter if we are
2256 * about to flip a bit in the cpumask:
2258 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2259 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2260 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2262 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2263 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2264 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2267 __raw_spin_unlock(&ftrace_max_lock
);
2270 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2272 mutex_unlock(&tracing_cpumask_update_lock
);
2273 free_cpumask_var(tracing_cpumask_new
);
2278 free_cpumask_var(tracing_cpumask_new
);
2283 static const struct file_operations tracing_cpumask_fops
= {
2284 .open
= tracing_open_generic
,
2285 .read
= tracing_cpumask_read
,
2286 .write
= tracing_cpumask_write
,
2290 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2291 size_t cnt
, loff_t
*ppos
)
2293 struct tracer_opt
*trace_opts
;
2301 /* calculate max size */
2302 for (i
= 0; trace_options
[i
]; i
++) {
2303 len
+= strlen(trace_options
[i
]);
2304 len
+= 3; /* "no" and newline */
2307 mutex_lock(&trace_types_lock
);
2308 tracer_flags
= current_trace
->flags
->val
;
2309 trace_opts
= current_trace
->flags
->opts
;
2312 * Increase the size with names of options specific
2313 * of the current tracer.
2315 for (i
= 0; trace_opts
[i
].name
; i
++) {
2316 len
+= strlen(trace_opts
[i
].name
);
2317 len
+= 3; /* "no" and newline */
2321 buf
= kmalloc(len
+ 1, GFP_KERNEL
);
2323 mutex_unlock(&trace_types_lock
);
2327 for (i
= 0; trace_options
[i
]; i
++) {
2328 if (trace_flags
& (1 << i
))
2329 r
+= sprintf(buf
+ r
, "%s\n", trace_options
[i
]);
2331 r
+= sprintf(buf
+ r
, "no%s\n", trace_options
[i
]);
2334 for (i
= 0; trace_opts
[i
].name
; i
++) {
2335 if (tracer_flags
& trace_opts
[i
].bit
)
2336 r
+= sprintf(buf
+ r
, "%s\n",
2337 trace_opts
[i
].name
);
2339 r
+= sprintf(buf
+ r
, "no%s\n",
2340 trace_opts
[i
].name
);
2342 mutex_unlock(&trace_types_lock
);
2344 WARN_ON(r
>= len
+ 1);
2346 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2352 /* Try to assign a tracer specific option */
2353 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2355 struct tracer_flags
*tracer_flags
= trace
->flags
;
2356 struct tracer_opt
*opts
= NULL
;
2360 for (i
= 0; tracer_flags
->opts
[i
].name
; i
++) {
2361 opts
= &tracer_flags
->opts
[i
];
2362 len
= strlen(opts
->name
);
2364 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2365 ret
= trace
->set_flag(tracer_flags
->val
,
2371 if (!tracer_flags
->opts
[i
].name
)
2374 /* Refused to handle */
2379 tracer_flags
->val
&= ~opts
->bit
;
2381 tracer_flags
->val
|= opts
->bit
;
2386 static void set_tracer_flags(unsigned int mask
, int enabled
)
2388 /* do nothing if flag is already set */
2389 if (!!(trace_flags
& mask
) == !!enabled
)
2393 trace_flags
|= mask
;
2395 trace_flags
&= ~mask
;
2399 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2400 size_t cnt
, loff_t
*ppos
)
2408 if (cnt
>= sizeof(buf
))
2411 if (copy_from_user(&buf
, ubuf
, cnt
))
2416 if (strncmp(buf
, "no", 2) == 0) {
2421 for (i
= 0; trace_options
[i
]; i
++) {
2422 int len
= strlen(trace_options
[i
]);
2424 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2425 set_tracer_flags(1 << i
, !neg
);
2430 /* If no option could be set, test the specific tracer options */
2431 if (!trace_options
[i
]) {
2432 mutex_lock(&trace_types_lock
);
2433 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2434 mutex_unlock(&trace_types_lock
);
2444 static const struct file_operations tracing_iter_fops
= {
2445 .open
= tracing_open_generic
,
2446 .read
= tracing_trace_options_read
,
2447 .write
= tracing_trace_options_write
,
2450 static const char readme_msg
[] =
2451 "tracing mini-HOWTO:\n\n"
2452 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2453 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2454 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2455 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2457 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2458 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2460 "# cat /sys/kernel/debug/tracing/trace_options\n"
2461 "noprint-parent nosym-offset nosym-addr noverbose\n"
2462 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2463 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2464 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2465 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2469 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2470 size_t cnt
, loff_t
*ppos
)
2472 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2473 readme_msg
, strlen(readme_msg
));
2476 static const struct file_operations tracing_readme_fops
= {
2477 .open
= tracing_open_generic
,
2478 .read
= tracing_readme_read
,
2482 tracing_saved_cmdlines_read(struct file
*file
, char __user
*ubuf
,
2483 size_t cnt
, loff_t
*ppos
)
2492 file_buf
= kmalloc(SAVED_CMDLINES
*(16+TASK_COMM_LEN
), GFP_KERNEL
);
2496 buf_comm
= kmalloc(TASK_COMM_LEN
, GFP_KERNEL
);
2504 for (i
= 0; i
< SAVED_CMDLINES
; i
++) {
2507 pid
= map_cmdline_to_pid
[i
];
2508 if (pid
== -1 || pid
== NO_CMDLINE_MAP
)
2511 trace_find_cmdline(pid
, buf_comm
);
2512 r
= sprintf(buf
, "%d %s\n", pid
, buf_comm
);
2517 len
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
2526 static const struct file_operations tracing_saved_cmdlines_fops
= {
2527 .open
= tracing_open_generic
,
2528 .read
= tracing_saved_cmdlines_read
,
2532 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2533 size_t cnt
, loff_t
*ppos
)
2538 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2539 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2543 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2544 size_t cnt
, loff_t
*ppos
)
2546 struct trace_array
*tr
= filp
->private_data
;
2551 if (cnt
>= sizeof(buf
))
2554 if (copy_from_user(&buf
, ubuf
, cnt
))
2559 ret
= strict_strtoul(buf
, 10, &val
);
2565 mutex_lock(&trace_types_lock
);
2566 if (tracer_enabled
^ val
) {
2569 if (current_trace
->start
)
2570 current_trace
->start(tr
);
2575 if (current_trace
->stop
)
2576 current_trace
->stop(tr
);
2579 mutex_unlock(&trace_types_lock
);
2587 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2588 size_t cnt
, loff_t
*ppos
)
2590 char buf
[MAX_TRACER_SIZE
+2];
2593 mutex_lock(&trace_types_lock
);
2595 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2597 r
= sprintf(buf
, "\n");
2598 mutex_unlock(&trace_types_lock
);
2600 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2603 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2605 tracing_reset_online_cpus(tr
);
2609 static int tracing_resize_ring_buffer(unsigned long size
)
2614 * If kernel or user changes the size of the ring buffer
2615 * we use the size that was given, and we can forget about
2616 * expanding it later.
2618 ring_buffer_expanded
= 1;
2620 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2624 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2628 r
= ring_buffer_resize(global_trace
.buffer
,
2629 global_trace
.entries
);
2632 * AARGH! We are left with different
2633 * size max buffer!!!!
2634 * The max buffer is our "snapshot" buffer.
2635 * When a tracer needs a snapshot (one of the
2636 * latency tracers), it swaps the max buffer
2637 * with the saved snap shot. We succeeded to
2638 * update the size of the main buffer, but failed to
2639 * update the size of the max buffer. But when we tried
2640 * to reset the main buffer to the original size, we
2641 * failed there too. This is very unlikely to
2642 * happen, but if it does, warn and kill all
2646 tracing_disabled
= 1;
2651 global_trace
.entries
= size
;
2657 * tracing_update_buffers - used by tracing facility to expand ring buffers
2659 * To save on memory when the tracing is never used on a system with it
2660 * configured in. The ring buffers are set to a minimum size. But once
2661 * a user starts to use the tracing facility, then they need to grow
2662 * to their default size.
2664 * This function is to be called when a tracer is about to be used.
2666 int tracing_update_buffers(void)
2670 mutex_lock(&trace_types_lock
);
2671 if (!ring_buffer_expanded
)
2672 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2673 mutex_unlock(&trace_types_lock
);
2678 struct trace_option_dentry
;
2680 static struct trace_option_dentry
*
2681 create_trace_option_files(struct tracer
*tracer
);
2684 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2686 static int tracing_set_tracer(const char *buf
)
2688 static struct trace_option_dentry
*topts
;
2689 struct trace_array
*tr
= &global_trace
;
2693 mutex_lock(&trace_types_lock
);
2695 if (!ring_buffer_expanded
) {
2696 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2702 for (t
= trace_types
; t
; t
= t
->next
) {
2703 if (strcmp(t
->name
, buf
) == 0)
2710 if (t
== current_trace
)
2713 trace_branch_disable();
2714 if (current_trace
&& current_trace
->reset
)
2715 current_trace
->reset(tr
);
2717 destroy_trace_option_files(topts
);
2721 topts
= create_trace_option_files(current_trace
);
2724 ret
= tracer_init(t
, tr
);
2729 trace_branch_enable(tr
);
2731 mutex_unlock(&trace_types_lock
);
2737 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2738 size_t cnt
, loff_t
*ppos
)
2740 char buf
[MAX_TRACER_SIZE
+1];
2747 if (cnt
> MAX_TRACER_SIZE
)
2748 cnt
= MAX_TRACER_SIZE
;
2750 if (copy_from_user(&buf
, ubuf
, cnt
))
2755 /* strip ending whitespace. */
2756 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2759 err
= tracing_set_tracer(buf
);
2769 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2770 size_t cnt
, loff_t
*ppos
)
2772 unsigned long *ptr
= filp
->private_data
;
2776 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2777 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2778 if (r
> sizeof(buf
))
2780 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2784 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2785 size_t cnt
, loff_t
*ppos
)
2787 unsigned long *ptr
= filp
->private_data
;
2792 if (cnt
>= sizeof(buf
))
2795 if (copy_from_user(&buf
, ubuf
, cnt
))
2800 ret
= strict_strtoul(buf
, 10, &val
);
2809 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2811 long cpu_file
= (long) inode
->i_private
;
2812 struct trace_iterator
*iter
;
2815 if (tracing_disabled
)
2818 mutex_lock(&trace_types_lock
);
2820 /* We only allow one reader per cpu */
2821 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
2822 if (!cpumask_empty(tracing_reader_cpumask
)) {
2826 cpumask_setall(tracing_reader_cpumask
);
2828 if (!cpumask_test_cpu(cpu_file
, tracing_reader_cpumask
))
2829 cpumask_set_cpu(cpu_file
, tracing_reader_cpumask
);
2836 /* create a buffer to store the information to pass to userspace */
2837 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2844 * We make a copy of the current tracer to avoid concurrent
2845 * changes on it while we are reading.
2847 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2853 *iter
->trace
= *current_trace
;
2855 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2860 /* trace pipe does not show start of buffer */
2861 cpumask_setall(iter
->started
);
2863 if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2864 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2866 iter
->cpu_file
= cpu_file
;
2867 iter
->tr
= &global_trace
;
2868 mutex_init(&iter
->mutex
);
2869 filp
->private_data
= iter
;
2871 if (iter
->trace
->pipe_open
)
2872 iter
->trace
->pipe_open(iter
);
2875 mutex_unlock(&trace_types_lock
);
2881 mutex_unlock(&trace_types_lock
);
2885 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2887 struct trace_iterator
*iter
= file
->private_data
;
2889 mutex_lock(&trace_types_lock
);
2891 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
)
2892 cpumask_clear(tracing_reader_cpumask
);
2894 cpumask_clear_cpu(iter
->cpu_file
, tracing_reader_cpumask
);
2896 mutex_unlock(&trace_types_lock
);
2898 free_cpumask_var(iter
->started
);
2899 mutex_destroy(&iter
->mutex
);
2907 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2909 struct trace_iterator
*iter
= filp
->private_data
;
2911 if (trace_flags
& TRACE_ITER_BLOCK
) {
2913 * Always select as readable when in blocking mode
2915 return POLLIN
| POLLRDNORM
;
2917 if (!trace_empty(iter
))
2918 return POLLIN
| POLLRDNORM
;
2919 poll_wait(filp
, &trace_wait
, poll_table
);
2920 if (!trace_empty(iter
))
2921 return POLLIN
| POLLRDNORM
;
2928 void default_wait_pipe(struct trace_iterator
*iter
)
2932 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
2934 if (trace_empty(iter
))
2937 finish_wait(&trace_wait
, &wait
);
2941 * This is a make-shift waitqueue.
2942 * A tracer might use this callback on some rare cases:
2944 * 1) the current tracer might hold the runqueue lock when it wakes up
2945 * a reader, hence a deadlock (sched, function, and function graph tracers)
2946 * 2) the function tracers, trace all functions, we don't want
2947 * the overhead of calling wake_up and friends
2948 * (and tracing them too)
2950 * Anyway, this is really very primitive wakeup.
2952 void poll_wait_pipe(struct trace_iterator
*iter
)
2954 set_current_state(TASK_INTERRUPTIBLE
);
2955 /* sleep for 100 msecs, and try again. */
2956 schedule_timeout(HZ
/ 10);
2959 /* Must be called with trace_types_lock mutex held. */
2960 static int tracing_wait_pipe(struct file
*filp
)
2962 struct trace_iterator
*iter
= filp
->private_data
;
2964 while (trace_empty(iter
)) {
2966 if ((filp
->f_flags
& O_NONBLOCK
)) {
2970 mutex_unlock(&iter
->mutex
);
2972 iter
->trace
->wait_pipe(iter
);
2974 mutex_lock(&iter
->mutex
);
2976 if (signal_pending(current
))
2980 * We block until we read something and tracing is disabled.
2981 * We still block if tracing is disabled, but we have never
2982 * read anything. This allows a user to cat this file, and
2983 * then enable tracing. But after we have read something,
2984 * we give an EOF when tracing is again disabled.
2986 * iter->pos will be 0 if we haven't read anything.
2988 if (!tracer_enabled
&& iter
->pos
)
2999 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
3000 size_t cnt
, loff_t
*ppos
)
3002 struct trace_iterator
*iter
= filp
->private_data
;
3003 static struct tracer
*old_tracer
;
3006 /* return any leftover data */
3007 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3011 trace_seq_init(&iter
->seq
);
3013 /* copy the tracer to avoid using a global lock all around */
3014 mutex_lock(&trace_types_lock
);
3015 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3016 old_tracer
= current_trace
;
3017 *iter
->trace
= *current_trace
;
3019 mutex_unlock(&trace_types_lock
);
3022 * Avoid more than one consumer on a single file descriptor
3023 * This is just a matter of traces coherency, the ring buffer itself
3026 mutex_lock(&iter
->mutex
);
3027 if (iter
->trace
->read
) {
3028 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
3034 sret
= tracing_wait_pipe(filp
);
3038 /* stop when tracing is finished */
3039 if (trace_empty(iter
)) {
3044 if (cnt
>= PAGE_SIZE
)
3045 cnt
= PAGE_SIZE
- 1;
3047 /* reset all but tr, trace, and overruns */
3048 memset(&iter
->seq
, 0,
3049 sizeof(struct trace_iterator
) -
3050 offsetof(struct trace_iterator
, seq
));
3053 trace_event_read_lock();
3054 while (find_next_entry_inc(iter
) != NULL
) {
3055 enum print_line_t ret
;
3056 int len
= iter
->seq
.len
;
3058 ret
= print_trace_line(iter
);
3059 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3060 /* don't print partial lines */
3061 iter
->seq
.len
= len
;
3064 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3065 trace_consume(iter
);
3067 if (iter
->seq
.len
>= cnt
)
3070 trace_event_read_unlock();
3072 /* Now copy what we have to the user */
3073 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3074 if (iter
->seq
.readpos
>= iter
->seq
.len
)
3075 trace_seq_init(&iter
->seq
);
3078 * If there was nothing to send to user, inspite of consuming trace
3079 * entries, go back to wait for more entries.
3085 mutex_unlock(&iter
->mutex
);
3090 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
3091 struct pipe_buffer
*buf
)
3093 __free_page(buf
->page
);
3096 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
3099 __free_page(spd
->pages
[idx
]);
3102 static struct pipe_buf_operations tracing_pipe_buf_ops
= {
3104 .map
= generic_pipe_buf_map
,
3105 .unmap
= generic_pipe_buf_unmap
,
3106 .confirm
= generic_pipe_buf_confirm
,
3107 .release
= tracing_pipe_buf_release
,
3108 .steal
= generic_pipe_buf_steal
,
3109 .get
= generic_pipe_buf_get
,
3113 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
3118 /* Seq buffer is page-sized, exactly what we need. */
3120 count
= iter
->seq
.len
;
3121 ret
= print_trace_line(iter
);
3122 count
= iter
->seq
.len
- count
;
3125 iter
->seq
.len
-= count
;
3128 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3129 iter
->seq
.len
-= count
;
3133 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3134 trace_consume(iter
);
3136 if (!find_next_entry_inc(iter
)) {
3146 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3148 struct pipe_inode_info
*pipe
,
3152 struct page
*pages
[PIPE_BUFFERS
];
3153 struct partial_page partial
[PIPE_BUFFERS
];
3154 struct trace_iterator
*iter
= filp
->private_data
;
3155 struct splice_pipe_desc spd
= {
3158 .nr_pages
= 0, /* This gets updated below. */
3160 .ops
= &tracing_pipe_buf_ops
,
3161 .spd_release
= tracing_spd_release_pipe
,
3163 static struct tracer
*old_tracer
;
3168 /* copy the tracer to avoid using a global lock all around */
3169 mutex_lock(&trace_types_lock
);
3170 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3171 old_tracer
= current_trace
;
3172 *iter
->trace
= *current_trace
;
3174 mutex_unlock(&trace_types_lock
);
3176 mutex_lock(&iter
->mutex
);
3178 if (iter
->trace
->splice_read
) {
3179 ret
= iter
->trace
->splice_read(iter
, filp
,
3180 ppos
, pipe
, len
, flags
);
3185 ret
= tracing_wait_pipe(filp
);
3189 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
3194 trace_event_read_lock();
3196 /* Fill as many pages as possible. */
3197 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
3198 pages
[i
] = alloc_page(GFP_KERNEL
);
3202 rem
= tracing_fill_pipe_page(rem
, iter
);
3204 /* Copy the data into the page, so we can start over. */
3205 ret
= trace_seq_to_buffer(&iter
->seq
,
3206 page_address(pages
[i
]),
3209 __free_page(pages
[i
]);
3212 partial
[i
].offset
= 0;
3213 partial
[i
].len
= iter
->seq
.len
;
3215 trace_seq_init(&iter
->seq
);
3218 trace_event_read_unlock();
3219 mutex_unlock(&iter
->mutex
);
3223 return splice_to_pipe(pipe
, &spd
);
3226 mutex_unlock(&iter
->mutex
);
3232 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3233 size_t cnt
, loff_t
*ppos
)
3235 struct trace_array
*tr
= filp
->private_data
;
3239 mutex_lock(&trace_types_lock
);
3240 if (!ring_buffer_expanded
)
3241 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3243 trace_buf_size
>> 10);
3245 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3246 mutex_unlock(&trace_types_lock
);
3248 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3252 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3253 size_t cnt
, loff_t
*ppos
)
3259 if (cnt
>= sizeof(buf
))
3262 if (copy_from_user(&buf
, ubuf
, cnt
))
3267 ret
= strict_strtoul(buf
, 10, &val
);
3271 /* must have at least 1 entry */
3275 mutex_lock(&trace_types_lock
);
3279 /* disable all cpu buffers */
3280 for_each_tracing_cpu(cpu
) {
3281 if (global_trace
.data
[cpu
])
3282 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3283 if (max_tr
.data
[cpu
])
3284 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3287 /* value is in KB */
3290 if (val
!= global_trace
.entries
) {
3291 ret
= tracing_resize_ring_buffer(val
);
3300 /* If check pages failed, return ENOMEM */
3301 if (tracing_disabled
)
3304 for_each_tracing_cpu(cpu
) {
3305 if (global_trace
.data
[cpu
])
3306 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3307 if (max_tr
.data
[cpu
])
3308 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3312 max_tr
.entries
= global_trace
.entries
;
3313 mutex_unlock(&trace_types_lock
);
3318 static int mark_printk(const char *fmt
, ...)
3322 va_start(args
, fmt
);
3323 ret
= trace_vprintk(0, fmt
, args
);
3329 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3330 size_t cnt
, loff_t
*fpos
)
3335 if (tracing_disabled
)
3338 if (cnt
> TRACE_BUF_SIZE
)
3339 cnt
= TRACE_BUF_SIZE
;
3341 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3345 if (copy_from_user(buf
, ubuf
, cnt
)) {
3350 /* Cut from the first nil or newline. */
3352 end
= strchr(buf
, '\n');
3356 cnt
= mark_printk("%s\n", buf
);
3363 static ssize_t
tracing_clock_read(struct file
*filp
, char __user
*ubuf
,
3364 size_t cnt
, loff_t
*ppos
)
3370 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++)
3371 bufiter
+= snprintf(buf
+ bufiter
, sizeof(buf
) - bufiter
,
3372 "%s%s%s%s", i
? " " : "",
3373 i
== trace_clock_id
? "[" : "", trace_clocks
[i
].name
,
3374 i
== trace_clock_id
? "]" : "");
3375 bufiter
+= snprintf(buf
+ bufiter
, sizeof(buf
) - bufiter
, "\n");
3377 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, bufiter
);
3380 static ssize_t
tracing_clock_write(struct file
*filp
, const char __user
*ubuf
,
3381 size_t cnt
, loff_t
*fpos
)
3384 const char *clockstr
;
3387 if (cnt
>= sizeof(buf
))
3390 if (copy_from_user(&buf
, ubuf
, cnt
))
3395 clockstr
= strstrip(buf
);
3397 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++) {
3398 if (strcmp(trace_clocks
[i
].name
, clockstr
) == 0)
3401 if (i
== ARRAY_SIZE(trace_clocks
))
3406 mutex_lock(&trace_types_lock
);
3408 ring_buffer_set_clock(global_trace
.buffer
, trace_clocks
[i
].func
);
3410 ring_buffer_set_clock(max_tr
.buffer
, trace_clocks
[i
].func
);
3412 mutex_unlock(&trace_types_lock
);
3419 static const struct file_operations tracing_max_lat_fops
= {
3420 .open
= tracing_open_generic
,
3421 .read
= tracing_max_lat_read
,
3422 .write
= tracing_max_lat_write
,
3425 static const struct file_operations tracing_ctrl_fops
= {
3426 .open
= tracing_open_generic
,
3427 .read
= tracing_ctrl_read
,
3428 .write
= tracing_ctrl_write
,
3431 static const struct file_operations set_tracer_fops
= {
3432 .open
= tracing_open_generic
,
3433 .read
= tracing_set_trace_read
,
3434 .write
= tracing_set_trace_write
,
3437 static const struct file_operations tracing_pipe_fops
= {
3438 .open
= tracing_open_pipe
,
3439 .poll
= tracing_poll_pipe
,
3440 .read
= tracing_read_pipe
,
3441 .splice_read
= tracing_splice_read_pipe
,
3442 .release
= tracing_release_pipe
,
3445 static const struct file_operations tracing_entries_fops
= {
3446 .open
= tracing_open_generic
,
3447 .read
= tracing_entries_read
,
3448 .write
= tracing_entries_write
,
3451 static const struct file_operations tracing_mark_fops
= {
3452 .open
= tracing_open_generic
,
3453 .write
= tracing_mark_write
,
3456 static const struct file_operations trace_clock_fops
= {
3457 .open
= tracing_open_generic
,
3458 .read
= tracing_clock_read
,
3459 .write
= tracing_clock_write
,
3462 struct ftrace_buffer_info
{
3463 struct trace_array
*tr
;
3469 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3471 int cpu
= (int)(long)inode
->i_private
;
3472 struct ftrace_buffer_info
*info
;
3474 if (tracing_disabled
)
3477 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3481 info
->tr
= &global_trace
;
3484 /* Force reading ring buffer for first read */
3485 info
->read
= (unsigned int)-1;
3487 filp
->private_data
= info
;
3489 return nonseekable_open(inode
, filp
);
3493 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3494 size_t count
, loff_t
*ppos
)
3496 struct ftrace_buffer_info
*info
= filp
->private_data
;
3505 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3509 /* Do we have previous read data to read? */
3510 if (info
->read
< PAGE_SIZE
)
3515 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3522 pos
= ring_buffer_page_len(info
->spare
);
3524 if (pos
< PAGE_SIZE
)
3525 memset(info
->spare
+ pos
, 0, PAGE_SIZE
- pos
);
3528 size
= PAGE_SIZE
- info
->read
;
3532 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3543 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3545 struct ftrace_buffer_info
*info
= file
->private_data
;
3548 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3555 struct ring_buffer
*buffer
;
3560 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3561 struct pipe_buffer
*buf
)
3563 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3568 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3573 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3574 struct pipe_buffer
*buf
)
3579 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3580 struct pipe_buffer
*buf
)
3582 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3587 /* Pipe buffer operations for a buffer. */
3588 static struct pipe_buf_operations buffer_pipe_buf_ops
= {
3590 .map
= generic_pipe_buf_map
,
3591 .unmap
= generic_pipe_buf_unmap
,
3592 .confirm
= generic_pipe_buf_confirm
,
3593 .release
= buffer_pipe_buf_release
,
3594 .steal
= buffer_pipe_buf_steal
,
3595 .get
= buffer_pipe_buf_get
,
3599 * Callback from splice_to_pipe(), if we need to release some pages
3600 * at the end of the spd in case we error'ed out in filling the pipe.
3602 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3604 struct buffer_ref
*ref
=
3605 (struct buffer_ref
*)spd
->partial
[i
].private;
3610 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3612 spd
->partial
[i
].private = 0;
3616 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3617 struct pipe_inode_info
*pipe
, size_t len
,
3620 struct ftrace_buffer_info
*info
= file
->private_data
;
3621 struct partial_page partial
[PIPE_BUFFERS
];
3622 struct page
*pages
[PIPE_BUFFERS
];
3623 struct splice_pipe_desc spd
= {
3627 .ops
= &buffer_pipe_buf_ops
,
3628 .spd_release
= buffer_spd_release
,
3630 struct buffer_ref
*ref
;
3631 int entries
, size
, i
;
3634 if (*ppos
& (PAGE_SIZE
- 1)) {
3635 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3639 if (len
& (PAGE_SIZE
- 1)) {
3640 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3641 if (len
< PAGE_SIZE
)
3646 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3648 for (i
= 0; i
< PIPE_BUFFERS
&& len
&& entries
; i
++, len
-= PAGE_SIZE
) {
3652 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3657 ref
->buffer
= info
->tr
->buffer
;
3658 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3664 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3667 ring_buffer_free_read_page(ref
->buffer
,
3674 * zero out any left over data, this is going to
3677 size
= ring_buffer_page_len(ref
->page
);
3678 if (size
< PAGE_SIZE
)
3679 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3681 page
= virt_to_page(ref
->page
);
3683 spd
.pages
[i
] = page
;
3684 spd
.partial
[i
].len
= PAGE_SIZE
;
3685 spd
.partial
[i
].offset
= 0;
3686 spd
.partial
[i
].private = (unsigned long)ref
;
3690 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3695 /* did we read anything? */
3696 if (!spd
.nr_pages
) {
3697 if (flags
& SPLICE_F_NONBLOCK
)
3705 ret
= splice_to_pipe(pipe
, &spd
);
3710 static const struct file_operations tracing_buffers_fops
= {
3711 .open
= tracing_buffers_open
,
3712 .read
= tracing_buffers_read
,
3713 .release
= tracing_buffers_release
,
3714 .splice_read
= tracing_buffers_splice_read
,
3715 .llseek
= no_llseek
,
3719 tracing_stats_read(struct file
*filp
, char __user
*ubuf
,
3720 size_t count
, loff_t
*ppos
)
3722 unsigned long cpu
= (unsigned long)filp
->private_data
;
3723 struct trace_array
*tr
= &global_trace
;
3724 struct trace_seq
*s
;
3727 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
3733 cnt
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
3734 trace_seq_printf(s
, "entries: %ld\n", cnt
);
3736 cnt
= ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
3737 trace_seq_printf(s
, "overrun: %ld\n", cnt
);
3739 cnt
= ring_buffer_commit_overrun_cpu(tr
->buffer
, cpu
);
3740 trace_seq_printf(s
, "commit overrun: %ld\n", cnt
);
3742 count
= simple_read_from_buffer(ubuf
, count
, ppos
, s
->buffer
, s
->len
);
3749 static const struct file_operations tracing_stats_fops
= {
3750 .open
= tracing_open_generic
,
3751 .read
= tracing_stats_read
,
3754 #ifdef CONFIG_DYNAMIC_FTRACE
3756 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3762 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3763 size_t cnt
, loff_t
*ppos
)
3765 static char ftrace_dyn_info_buffer
[1024];
3766 static DEFINE_MUTEX(dyn_info_mutex
);
3767 unsigned long *p
= filp
->private_data
;
3768 char *buf
= ftrace_dyn_info_buffer
;
3769 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3772 mutex_lock(&dyn_info_mutex
);
3773 r
= sprintf(buf
, "%ld ", *p
);
3775 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3778 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3780 mutex_unlock(&dyn_info_mutex
);
3785 static const struct file_operations tracing_dyn_info_fops
= {
3786 .open
= tracing_open_generic
,
3787 .read
= tracing_read_dyn_info
,
3791 static struct dentry
*d_tracer
;
3793 struct dentry
*tracing_init_dentry(void)
3800 if (!debugfs_initialized())
3803 d_tracer
= debugfs_create_dir("tracing", NULL
);
3805 if (!d_tracer
&& !once
) {
3807 pr_warning("Could not create debugfs directory 'tracing'\n");
3814 static struct dentry
*d_percpu
;
3816 struct dentry
*tracing_dentry_percpu(void)
3819 struct dentry
*d_tracer
;
3824 d_tracer
= tracing_init_dentry();
3829 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
3831 if (!d_percpu
&& !once
) {
3833 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3840 static void tracing_init_debugfs_percpu(long cpu
)
3842 struct dentry
*d_percpu
= tracing_dentry_percpu();
3843 struct dentry
*d_cpu
;
3844 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3847 if (cpu
> 999 || cpu
< 0)
3850 sprintf(cpu_dir
, "cpu%ld", cpu
);
3851 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
3853 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
3857 /* per cpu trace_pipe */
3858 trace_create_file("trace_pipe", 0444, d_cpu
,
3859 (void *) cpu
, &tracing_pipe_fops
);
3862 trace_create_file("trace", 0644, d_cpu
,
3863 (void *) cpu
, &tracing_fops
);
3865 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
3866 (void *) cpu
, &tracing_buffers_fops
);
3868 trace_create_file("stats", 0444, d_cpu
,
3869 (void *) cpu
, &tracing_stats_fops
);
3872 #ifdef CONFIG_FTRACE_SELFTEST
3873 /* Let selftest have access to static functions in this file */
3874 #include "trace_selftest.c"
3877 struct trace_option_dentry
{
3878 struct tracer_opt
*opt
;
3879 struct tracer_flags
*flags
;
3880 struct dentry
*entry
;
3884 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3887 struct trace_option_dentry
*topt
= filp
->private_data
;
3890 if (topt
->flags
->val
& topt
->opt
->bit
)
3895 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3899 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3902 struct trace_option_dentry
*topt
= filp
->private_data
;
3907 if (cnt
>= sizeof(buf
))
3910 if (copy_from_user(&buf
, ubuf
, cnt
))
3915 ret
= strict_strtoul(buf
, 10, &val
);
3922 /* do nothing if already cleared */
3923 if (!(topt
->flags
->val
& topt
->opt
->bit
))
3926 mutex_lock(&trace_types_lock
);
3927 if (current_trace
->set_flag
)
3928 ret
= current_trace
->set_flag(topt
->flags
->val
,
3930 mutex_unlock(&trace_types_lock
);
3933 topt
->flags
->val
&= ~topt
->opt
->bit
;
3936 /* do nothing if already set */
3937 if (topt
->flags
->val
& topt
->opt
->bit
)
3940 mutex_lock(&trace_types_lock
);
3941 if (current_trace
->set_flag
)
3942 ret
= current_trace
->set_flag(topt
->flags
->val
,
3944 mutex_unlock(&trace_types_lock
);
3947 topt
->flags
->val
|= topt
->opt
->bit
;
3960 static const struct file_operations trace_options_fops
= {
3961 .open
= tracing_open_generic
,
3962 .read
= trace_options_read
,
3963 .write
= trace_options_write
,
3967 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3970 long index
= (long)filp
->private_data
;
3973 if (trace_flags
& (1 << index
))
3978 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3982 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3985 long index
= (long)filp
->private_data
;
3990 if (cnt
>= sizeof(buf
))
3993 if (copy_from_user(&buf
, ubuf
, cnt
))
3998 ret
= strict_strtoul(buf
, 10, &val
);
4002 if (val
!= 0 && val
!= 1)
4004 set_tracer_flags(1 << index
, val
);
4011 static const struct file_operations trace_options_core_fops
= {
4012 .open
= tracing_open_generic
,
4013 .read
= trace_options_core_read
,
4014 .write
= trace_options_core_write
,
4017 struct dentry
*trace_create_file(const char *name
,
4019 struct dentry
*parent
,
4021 const struct file_operations
*fops
)
4025 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
4027 pr_warning("Could not create debugfs '%s' entry\n", name
);
4033 static struct dentry
*trace_options_init_dentry(void)
4035 struct dentry
*d_tracer
;
4036 static struct dentry
*t_options
;
4041 d_tracer
= tracing_init_dentry();
4045 t_options
= debugfs_create_dir("options", d_tracer
);
4047 pr_warning("Could not create debugfs directory 'options'\n");
4055 create_trace_option_file(struct trace_option_dentry
*topt
,
4056 struct tracer_flags
*flags
,
4057 struct tracer_opt
*opt
)
4059 struct dentry
*t_options
;
4061 t_options
= trace_options_init_dentry();
4065 topt
->flags
= flags
;
4068 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
4069 &trace_options_fops
);
4073 static struct trace_option_dentry
*
4074 create_trace_option_files(struct tracer
*tracer
)
4076 struct trace_option_dentry
*topts
;
4077 struct tracer_flags
*flags
;
4078 struct tracer_opt
*opts
;
4084 flags
= tracer
->flags
;
4086 if (!flags
|| !flags
->opts
)
4091 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4094 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
4098 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4099 create_trace_option_file(&topts
[cnt
], flags
,
4106 destroy_trace_option_files(struct trace_option_dentry
*topts
)
4113 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
4114 if (topts
[cnt
].entry
)
4115 debugfs_remove(topts
[cnt
].entry
);
4121 static struct dentry
*
4122 create_trace_option_core_file(const char *option
, long index
)
4124 struct dentry
*t_options
;
4126 t_options
= trace_options_init_dentry();
4130 return trace_create_file(option
, 0644, t_options
, (void *)index
,
4131 &trace_options_core_fops
);
4134 static __init
void create_trace_options_dir(void)
4136 struct dentry
*t_options
;
4139 t_options
= trace_options_init_dentry();
4143 for (i
= 0; trace_options
[i
]; i
++)
4144 create_trace_option_core_file(trace_options
[i
], i
);
4147 static __init
int tracer_init_debugfs(void)
4149 struct dentry
*d_tracer
;
4152 d_tracer
= tracing_init_dentry();
4154 trace_create_file("tracing_enabled", 0644, d_tracer
,
4155 &global_trace
, &tracing_ctrl_fops
);
4157 trace_create_file("trace_options", 0644, d_tracer
,
4158 NULL
, &tracing_iter_fops
);
4160 trace_create_file("tracing_cpumask", 0644, d_tracer
,
4161 NULL
, &tracing_cpumask_fops
);
4163 trace_create_file("trace", 0644, d_tracer
,
4164 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
4166 trace_create_file("available_tracers", 0444, d_tracer
,
4167 &global_trace
, &show_traces_fops
);
4169 trace_create_file("current_tracer", 0644, d_tracer
,
4170 &global_trace
, &set_tracer_fops
);
4172 #ifdef CONFIG_TRACER_MAX_TRACE
4173 trace_create_file("tracing_max_latency", 0644, d_tracer
,
4174 &tracing_max_latency
, &tracing_max_lat_fops
);
4176 trace_create_file("tracing_thresh", 0644, d_tracer
,
4177 &tracing_thresh
, &tracing_max_lat_fops
);
4180 trace_create_file("README", 0444, d_tracer
,
4181 NULL
, &tracing_readme_fops
);
4183 trace_create_file("trace_pipe", 0444, d_tracer
,
4184 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
4186 trace_create_file("buffer_size_kb", 0644, d_tracer
,
4187 &global_trace
, &tracing_entries_fops
);
4189 trace_create_file("trace_marker", 0220, d_tracer
,
4190 NULL
, &tracing_mark_fops
);
4192 trace_create_file("saved_cmdlines", 0444, d_tracer
,
4193 NULL
, &tracing_saved_cmdlines_fops
);
4195 trace_create_file("trace_clock", 0644, d_tracer
, NULL
,
4198 #ifdef CONFIG_DYNAMIC_FTRACE
4199 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
4200 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
4202 #ifdef CONFIG_SYSPROF_TRACER
4203 init_tracer_sysprof_debugfs(d_tracer
);
4206 create_trace_options_dir();
4208 for_each_tracing_cpu(cpu
)
4209 tracing_init_debugfs_percpu(cpu
);
4214 static int trace_panic_handler(struct notifier_block
*this,
4215 unsigned long event
, void *unused
)
4217 if (ftrace_dump_on_oops
)
4222 static struct notifier_block trace_panic_notifier
= {
4223 .notifier_call
= trace_panic_handler
,
4225 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
4228 static int trace_die_handler(struct notifier_block
*self
,
4234 if (ftrace_dump_on_oops
)
4243 static struct notifier_block trace_die_notifier
= {
4244 .notifier_call
= trace_die_handler
,
4249 * printk is set to max of 1024, we really don't need it that big.
4250 * Nothing should be printing 1000 characters anyway.
4252 #define TRACE_MAX_PRINT 1000
4255 * Define here KERN_TRACE so that we have one place to modify
4256 * it if we decide to change what log level the ftrace dump
4259 #define KERN_TRACE KERN_EMERG
4262 trace_printk_seq(struct trace_seq
*s
)
4264 /* Probably should print a warning here. */
4268 /* should be zero ended, but we are paranoid. */
4269 s
->buffer
[s
->len
] = 0;
4271 printk(KERN_TRACE
"%s", s
->buffer
);
4276 static void __ftrace_dump(bool disable_tracing
)
4278 static raw_spinlock_t ftrace_dump_lock
=
4279 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
4280 /* use static because iter can be a bit big for the stack */
4281 static struct trace_iterator iter
;
4282 unsigned int old_userobj
;
4283 static int dump_ran
;
4284 unsigned long flags
;
4288 local_irq_save(flags
);
4289 __raw_spin_lock(&ftrace_dump_lock
);
4297 if (disable_tracing
)
4300 for_each_tracing_cpu(cpu
) {
4301 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
4304 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
4306 /* don't look at user memory in panic mode */
4307 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4309 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4311 /* Simulate the iterator */
4312 iter
.tr
= &global_trace
;
4313 iter
.trace
= current_trace
;
4314 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4317 * We need to stop all tracing on all CPUS to read the
4318 * the next buffer. This is a bit expensive, but is
4319 * not done often. We fill all what we can read,
4320 * and then release the locks again.
4323 while (!trace_empty(&iter
)) {
4326 printk(KERN_TRACE
"---------------------------------\n");
4330 /* reset all but tr, trace, and overruns */
4331 memset(&iter
.seq
, 0,
4332 sizeof(struct trace_iterator
) -
4333 offsetof(struct trace_iterator
, seq
));
4334 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4337 if (find_next_entry_inc(&iter
) != NULL
) {
4340 ret
= print_trace_line(&iter
);
4341 if (ret
!= TRACE_TYPE_NO_CONSUME
)
4342 trace_consume(&iter
);
4345 trace_printk_seq(&iter
.seq
);
4349 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4351 printk(KERN_TRACE
"---------------------------------\n");
4353 /* Re-enable tracing if requested */
4354 if (!disable_tracing
) {
4355 trace_flags
|= old_userobj
;
4357 for_each_tracing_cpu(cpu
) {
4358 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
4364 __raw_spin_unlock(&ftrace_dump_lock
);
4365 local_irq_restore(flags
);
4368 /* By default: disable tracing after the dump */
4369 void ftrace_dump(void)
4371 __ftrace_dump(true);
4374 __init
static int tracer_alloc_buffers(void)
4380 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4383 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4384 goto out_free_buffer_mask
;
4386 if (!alloc_cpumask_var(&tracing_reader_cpumask
, GFP_KERNEL
))
4387 goto out_free_tracing_cpumask
;
4389 /* To save memory, keep the ring buffer size to its minimum */
4390 if (ring_buffer_expanded
)
4391 ring_buf_size
= trace_buf_size
;
4395 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4396 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4397 cpumask_clear(tracing_reader_cpumask
);
4399 /* TODO: make the number of buffers hot pluggable with CPUS */
4400 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
,
4401 TRACE_BUFFER_FLAGS
);
4402 if (!global_trace
.buffer
) {
4403 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4405 goto out_free_cpumask
;
4407 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4410 #ifdef CONFIG_TRACER_MAX_TRACE
4411 max_tr
.buffer
= ring_buffer_alloc(ring_buf_size
,
4412 TRACE_BUFFER_FLAGS
);
4413 if (!max_tr
.buffer
) {
4414 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4416 ring_buffer_free(global_trace
.buffer
);
4417 goto out_free_cpumask
;
4419 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
4420 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
4423 /* Allocate the first page for all buffers */
4424 for_each_tracing_cpu(i
) {
4425 global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4426 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
4429 trace_init_cmdlines();
4431 register_tracer(&nop_trace
);
4432 current_trace
= &nop_trace
;
4433 #ifdef CONFIG_BOOT_TRACER
4434 register_tracer(&boot_tracer
);
4436 /* All seems OK, enable tracing */
4437 tracing_disabled
= 0;
4439 atomic_notifier_chain_register(&panic_notifier_list
,
4440 &trace_panic_notifier
);
4442 register_die_notifier(&trace_die_notifier
);
4447 free_cpumask_var(tracing_reader_cpumask
);
4448 out_free_tracing_cpumask
:
4449 free_cpumask_var(tracing_cpumask
);
4450 out_free_buffer_mask
:
4451 free_cpumask_var(tracing_buffer_mask
);
4456 __init
static int clear_boot_tracer(void)
4459 * The default tracer at boot buffer is an init section.
4460 * This function is called in lateinit. If we did not
4461 * find the boot tracer, then clear it out, to prevent
4462 * later registration from accessing the buffer that is
4463 * about to be freed.
4465 if (!default_bootup_tracer
)
4468 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4469 default_bootup_tracer
);
4470 default_bootup_tracer
= NULL
;
4475 early_initcall(tracer_alloc_buffers
);
4476 fs_initcall(tracer_init_debugfs
);
4477 late_initcall(clear_boot_tracer
);