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)
273 if (trace_flags
& TRACE_ITER_BLOCK
)
276 * The runqueue_is_locked() can fail, but this is the best we
280 if (!runqueue_is_locked(cpu
))
281 wake_up(&trace_wait
);
285 static int __init
set_buf_size(char *str
)
287 unsigned long buf_size
;
291 buf_size
= memparse(str
, &str
);
292 /* nr_entries can not be zero */
295 trace_buf_size
= buf_size
;
298 __setup("trace_buf_size=", set_buf_size
);
300 unsigned long nsecs_to_usecs(unsigned long nsecs
)
305 /* These must match the bit postions in trace_iterator_flags */
306 static const char *trace_options
[] = {
335 { trace_clock_local
, "local" },
336 { trace_clock_global
, "global" },
342 * trace_parser_get_init - gets the buffer for trace parser
344 int trace_parser_get_init(struct trace_parser
*parser
, int size
)
346 memset(parser
, 0, sizeof(*parser
));
348 parser
->buffer
= kmalloc(size
, GFP_KERNEL
);
357 * trace_parser_put - frees the buffer for trace parser
359 void trace_parser_put(struct trace_parser
*parser
)
361 kfree(parser
->buffer
);
365 * trace_get_user - reads the user input string separated by space
366 * (matched by isspace(ch))
368 * For each string found the 'struct trace_parser' is updated,
369 * and the function returns.
371 * Returns number of bytes read.
373 * See kernel/trace/trace.h for 'struct trace_parser' details.
375 int trace_get_user(struct trace_parser
*parser
, const char __user
*ubuf
,
376 size_t cnt
, loff_t
*ppos
)
383 trace_parser_clear(parser
);
385 ret
= get_user(ch
, ubuf
++);
393 * The parser is not finished with the last write,
394 * continue reading the user input without skipping spaces.
397 /* skip white space */
398 while (cnt
&& isspace(ch
)) {
399 ret
= get_user(ch
, ubuf
++);
406 /* only spaces were written */
416 /* read the non-space input */
417 while (cnt
&& !isspace(ch
)) {
418 if (parser
->idx
< parser
->size
)
419 parser
->buffer
[parser
->idx
++] = ch
;
424 ret
= get_user(ch
, ubuf
++);
431 /* We either got finished input or we have to wait for another call. */
433 parser
->buffer
[parser
->idx
] = 0;
434 parser
->cont
= false;
437 parser
->buffer
[parser
->idx
++] = ch
;
447 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
455 if (s
->len
<= s
->readpos
)
458 len
= s
->len
- s
->readpos
;
461 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
471 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
476 if (s
->len
<= s
->readpos
)
479 len
= s
->len
- s
->readpos
;
482 ret
= memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
491 * ftrace_max_lock is used to protect the swapping of buffers
492 * when taking a max snapshot. The buffers themselves are
493 * protected by per_cpu spinlocks. But the action of the swap
494 * needs its own lock.
496 * This is defined as a raw_spinlock_t in order to help
497 * with performance when lockdep debugging is enabled.
499 * It is also used in other places outside the update_max_tr
500 * so it needs to be defined outside of the
501 * CONFIG_TRACER_MAX_TRACE.
503 static raw_spinlock_t ftrace_max_lock
=
504 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
506 #ifdef CONFIG_TRACER_MAX_TRACE
507 unsigned long __read_mostly tracing_max_latency
;
508 unsigned long __read_mostly tracing_thresh
;
511 * Copy the new maximum trace into the separate maximum-trace
512 * structure. (this way the maximum trace is permanently saved,
513 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
516 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
518 struct trace_array_cpu
*data
= tr
->data
[cpu
];
519 struct trace_array_cpu
*max_data
= tr
->data
[cpu
];
522 max_tr
.time_start
= data
->preempt_timestamp
;
524 max_data
= max_tr
.data
[cpu
];
525 max_data
->saved_latency
= tracing_max_latency
;
526 max_data
->critical_start
= data
->critical_start
;
527 max_data
->critical_end
= data
->critical_end
;
529 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
530 max_data
->pid
= tsk
->pid
;
531 max_data
->uid
= task_uid(tsk
);
532 max_data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
533 max_data
->policy
= tsk
->policy
;
534 max_data
->rt_priority
= tsk
->rt_priority
;
536 /* record this tasks comm */
537 tracing_record_cmdline(tsk
);
541 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
543 * @tsk: the task with the latency
544 * @cpu: The cpu that initiated the trace.
546 * Flip the buffers between the @tr and the max_tr and record information
547 * about which task was the cause of this latency.
550 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
552 struct ring_buffer
*buf
= tr
->buffer
;
554 if (trace_stop_count
)
557 WARN_ON_ONCE(!irqs_disabled());
558 __raw_spin_lock(&ftrace_max_lock
);
560 tr
->buffer
= max_tr
.buffer
;
563 __update_max_tr(tr
, tsk
, cpu
);
564 __raw_spin_unlock(&ftrace_max_lock
);
568 * update_max_tr_single - only copy one trace over, and reset the rest
570 * @tsk - task with the latency
571 * @cpu - the cpu of the buffer to copy.
573 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
576 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
580 if (trace_stop_count
)
583 WARN_ON_ONCE(!irqs_disabled());
584 __raw_spin_lock(&ftrace_max_lock
);
586 ftrace_disable_cpu();
588 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
592 * We failed to swap the buffer due to a commit taking
593 * place on this CPU. We fail to record, but we reset
594 * the max trace buffer (no one writes directly to it)
595 * and flag that it failed.
597 trace_array_printk(&max_tr
, _THIS_IP_
,
598 "Failed to swap buffers due to commit in progress\n");
603 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
&& ret
!= -EBUSY
);
605 __update_max_tr(tr
, tsk
, cpu
);
606 __raw_spin_unlock(&ftrace_max_lock
);
608 #endif /* CONFIG_TRACER_MAX_TRACE */
611 * register_tracer - register a tracer with the ftrace system.
612 * @type - the plugin for the tracer
614 * Register a new plugin tracer.
616 int register_tracer(struct tracer
*type
)
617 __releases(kernel_lock
)
618 __acquires(kernel_lock
)
624 pr_info("Tracer must have a name\n");
628 if (strlen(type
->name
) > MAX_TRACER_SIZE
) {
629 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE
);
634 * When this gets called we hold the BKL which means that
635 * preemption is disabled. Various trace selftests however
636 * need to disable and enable preemption for successful tests.
637 * So we drop the BKL here and grab it after the tests again.
640 mutex_lock(&trace_types_lock
);
642 tracing_selftest_running
= true;
644 for (t
= trace_types
; t
; t
= t
->next
) {
645 if (strcmp(type
->name
, t
->name
) == 0) {
647 pr_info("Tracer %s already registered\n",
655 type
->set_flag
= &dummy_set_flag
;
657 type
->flags
= &dummy_tracer_flags
;
659 if (!type
->flags
->opts
)
660 type
->flags
->opts
= dummy_tracer_opt
;
661 if (!type
->wait_pipe
)
662 type
->wait_pipe
= default_wait_pipe
;
665 #ifdef CONFIG_FTRACE_STARTUP_TEST
666 if (type
->selftest
&& !tracing_selftest_disabled
) {
667 struct tracer
*saved_tracer
= current_trace
;
668 struct trace_array
*tr
= &global_trace
;
671 * Run a selftest on this tracer.
672 * Here we reset the trace buffer, and set the current
673 * tracer to be this tracer. The tracer can then run some
674 * internal tracing to verify that everything is in order.
675 * If we fail, we do not register this tracer.
677 tracing_reset_online_cpus(tr
);
679 current_trace
= type
;
680 /* the test is responsible for initializing and enabling */
681 pr_info("Testing tracer %s: ", type
->name
);
682 ret
= type
->selftest(type
, tr
);
683 /* the test is responsible for resetting too */
684 current_trace
= saved_tracer
;
686 printk(KERN_CONT
"FAILED!\n");
689 /* Only reset on passing, to avoid touching corrupted buffers */
690 tracing_reset_online_cpus(tr
);
692 printk(KERN_CONT
"PASSED\n");
696 type
->next
= trace_types
;
700 tracing_selftest_running
= false;
701 mutex_unlock(&trace_types_lock
);
703 if (ret
|| !default_bootup_tracer
)
706 if (strncmp(default_bootup_tracer
, type
->name
, MAX_TRACER_SIZE
))
709 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
710 /* Do we want this tracer to start on bootup? */
711 tracing_set_tracer(type
->name
);
712 default_bootup_tracer
= NULL
;
713 /* disable other selftests, since this will break it. */
714 tracing_selftest_disabled
= 1;
715 #ifdef CONFIG_FTRACE_STARTUP_TEST
716 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
725 void unregister_tracer(struct tracer
*type
)
729 mutex_lock(&trace_types_lock
);
730 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
734 pr_info("Tracer %s not registered\n", type
->name
);
740 if (type
== current_trace
&& tracer_enabled
) {
743 if (current_trace
->stop
)
744 current_trace
->stop(&global_trace
);
745 current_trace
= &nop_trace
;
748 mutex_unlock(&trace_types_lock
);
751 static void __tracing_reset(struct trace_array
*tr
, int cpu
)
753 ftrace_disable_cpu();
754 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
758 void tracing_reset(struct trace_array
*tr
, int cpu
)
760 struct ring_buffer
*buffer
= tr
->buffer
;
762 ring_buffer_record_disable(buffer
);
764 /* Make sure all commits have finished */
766 __tracing_reset(tr
, cpu
);
768 ring_buffer_record_enable(buffer
);
771 void tracing_reset_online_cpus(struct trace_array
*tr
)
773 struct ring_buffer
*buffer
= tr
->buffer
;
776 ring_buffer_record_disable(buffer
);
778 /* Make sure all commits have finished */
781 tr
->time_start
= ftrace_now(tr
->cpu
);
783 for_each_online_cpu(cpu
)
784 __tracing_reset(tr
, cpu
);
786 ring_buffer_record_enable(buffer
);
789 void tracing_reset_current(int cpu
)
791 tracing_reset(&global_trace
, cpu
);
794 void tracing_reset_current_online_cpus(void)
796 tracing_reset_online_cpus(&global_trace
);
799 #define SAVED_CMDLINES 128
800 #define NO_CMDLINE_MAP UINT_MAX
801 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
802 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
803 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
804 static int cmdline_idx
;
805 static raw_spinlock_t trace_cmdline_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
807 /* temporary disable recording */
808 static atomic_t trace_record_cmdline_disabled __read_mostly
;
810 static void trace_init_cmdlines(void)
812 memset(&map_pid_to_cmdline
, NO_CMDLINE_MAP
, sizeof(map_pid_to_cmdline
));
813 memset(&map_cmdline_to_pid
, NO_CMDLINE_MAP
, sizeof(map_cmdline_to_pid
));
817 int is_tracing_stopped(void)
819 return trace_stop_count
;
823 * ftrace_off_permanent - disable all ftrace code permanently
825 * This should only be called when a serious anomally has
826 * been detected. This will turn off the function tracing,
827 * ring buffers, and other tracing utilites. It takes no
828 * locks and can be called from any context.
830 void ftrace_off_permanent(void)
832 tracing_disabled
= 1;
834 tracing_off_permanent();
838 * tracing_start - quick start of the tracer
840 * If tracing is enabled but was stopped by tracing_stop,
841 * this will start the tracer back up.
843 void tracing_start(void)
845 struct ring_buffer
*buffer
;
848 if (tracing_disabled
)
851 spin_lock_irqsave(&tracing_start_lock
, flags
);
852 if (--trace_stop_count
) {
853 if (trace_stop_count
< 0) {
854 /* Someone screwed up their debugging */
856 trace_stop_count
= 0;
862 buffer
= global_trace
.buffer
;
864 ring_buffer_record_enable(buffer
);
866 buffer
= max_tr
.buffer
;
868 ring_buffer_record_enable(buffer
);
872 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
876 * tracing_stop - quick stop of the tracer
878 * Light weight way to stop tracing. Use in conjunction with
881 void tracing_stop(void)
883 struct ring_buffer
*buffer
;
887 spin_lock_irqsave(&tracing_start_lock
, flags
);
888 if (trace_stop_count
++)
891 buffer
= global_trace
.buffer
;
893 ring_buffer_record_disable(buffer
);
895 buffer
= max_tr
.buffer
;
897 ring_buffer_record_disable(buffer
);
900 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
903 void trace_stop_cmdline_recording(void);
905 static void trace_save_cmdline(struct task_struct
*tsk
)
909 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
913 * It's not the end of the world if we don't get
914 * the lock, but we also don't want to spin
915 * nor do we want to disable interrupts,
916 * so if we miss here, then better luck next time.
918 if (!__raw_spin_trylock(&trace_cmdline_lock
))
921 idx
= map_pid_to_cmdline
[tsk
->pid
];
922 if (idx
== NO_CMDLINE_MAP
) {
923 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
926 * Check whether the cmdline buffer at idx has a pid
927 * mapped. We are going to overwrite that entry so we
928 * need to clear the map_pid_to_cmdline. Otherwise we
929 * would read the new comm for the old pid.
931 pid
= map_cmdline_to_pid
[idx
];
932 if (pid
!= NO_CMDLINE_MAP
)
933 map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
935 map_cmdline_to_pid
[idx
] = tsk
->pid
;
936 map_pid_to_cmdline
[tsk
->pid
] = idx
;
941 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
943 __raw_spin_unlock(&trace_cmdline_lock
);
946 void trace_find_cmdline(int pid
, char comm
[])
951 strcpy(comm
, "<idle>");
955 if (pid
> PID_MAX_DEFAULT
) {
956 strcpy(comm
, "<...>");
961 __raw_spin_lock(&trace_cmdline_lock
);
962 map
= map_pid_to_cmdline
[pid
];
963 if (map
!= NO_CMDLINE_MAP
)
964 strcpy(comm
, saved_cmdlines
[map
]);
966 strcpy(comm
, "<...>");
968 __raw_spin_unlock(&trace_cmdline_lock
);
972 void tracing_record_cmdline(struct task_struct
*tsk
)
974 if (atomic_read(&trace_record_cmdline_disabled
) || !tracer_enabled
||
978 trace_save_cmdline(tsk
);
982 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
985 struct task_struct
*tsk
= current
;
987 entry
->preempt_count
= pc
& 0xff;
988 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
989 entry
->lock_depth
= (tsk
) ? tsk
->lock_depth
: 0;
991 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
992 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
994 TRACE_FLAG_IRQS_NOSUPPORT
|
996 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
997 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
998 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
1000 EXPORT_SYMBOL_GPL(tracing_generic_entry_update
);
1002 struct ring_buffer_event
*
1003 trace_buffer_lock_reserve(struct ring_buffer
*buffer
,
1006 unsigned long flags
, int pc
)
1008 struct ring_buffer_event
*event
;
1010 event
= ring_buffer_lock_reserve(buffer
, len
);
1011 if (event
!= NULL
) {
1012 struct trace_entry
*ent
= ring_buffer_event_data(event
);
1014 tracing_generic_entry_update(ent
, flags
, pc
);
1022 __trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1023 struct ring_buffer_event
*event
,
1024 unsigned long flags
, int pc
,
1027 ring_buffer_unlock_commit(buffer
, event
);
1029 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1030 ftrace_trace_userstack(buffer
, flags
, pc
);
1036 void trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1037 struct ring_buffer_event
*event
,
1038 unsigned long flags
, int pc
)
1040 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
1043 struct ring_buffer_event
*
1044 trace_current_buffer_lock_reserve(struct ring_buffer
**current_rb
,
1045 int type
, unsigned long len
,
1046 unsigned long flags
, int pc
)
1048 *current_rb
= global_trace
.buffer
;
1049 return trace_buffer_lock_reserve(*current_rb
,
1050 type
, len
, flags
, pc
);
1052 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve
);
1054 void trace_current_buffer_unlock_commit(struct ring_buffer
*buffer
,
1055 struct ring_buffer_event
*event
,
1056 unsigned long flags
, int pc
)
1058 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
1060 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit
);
1062 void trace_nowake_buffer_unlock_commit(struct ring_buffer
*buffer
,
1063 struct ring_buffer_event
*event
,
1064 unsigned long flags
, int pc
)
1066 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 0);
1068 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit
);
1070 void trace_current_buffer_discard_commit(struct ring_buffer
*buffer
,
1071 struct ring_buffer_event
*event
)
1073 ring_buffer_discard_commit(buffer
, event
);
1075 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
1078 trace_function(struct trace_array
*tr
,
1079 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1082 struct ftrace_event_call
*call
= &event_function
;
1083 struct ring_buffer
*buffer
= tr
->buffer
;
1084 struct ring_buffer_event
*event
;
1085 struct ftrace_entry
*entry
;
1087 /* If we are reading the ring buffer, don't trace */
1088 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
1091 event
= trace_buffer_lock_reserve(buffer
, TRACE_FN
, sizeof(*entry
),
1095 entry
= ring_buffer_event_data(event
);
1097 entry
->parent_ip
= parent_ip
;
1099 if (!filter_check_discard(call
, entry
, buffer
, event
))
1100 ring_buffer_unlock_commit(buffer
, event
);
1104 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
1105 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1108 if (likely(!atomic_read(&data
->disabled
)))
1109 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
1112 #ifdef CONFIG_STACKTRACE
1113 static void __ftrace_trace_stack(struct ring_buffer
*buffer
,
1114 unsigned long flags
,
1117 struct ftrace_event_call
*call
= &event_kernel_stack
;
1118 struct ring_buffer_event
*event
;
1119 struct stack_entry
*entry
;
1120 struct stack_trace trace
;
1122 event
= trace_buffer_lock_reserve(buffer
, TRACE_STACK
,
1123 sizeof(*entry
), flags
, pc
);
1126 entry
= ring_buffer_event_data(event
);
1127 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1129 trace
.nr_entries
= 0;
1130 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1132 trace
.entries
= entry
->caller
;
1134 save_stack_trace(&trace
);
1135 if (!filter_check_discard(call
, entry
, buffer
, event
))
1136 ring_buffer_unlock_commit(buffer
, event
);
1139 void ftrace_trace_stack(struct ring_buffer
*buffer
, unsigned long flags
,
1142 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1145 __ftrace_trace_stack(buffer
, flags
, skip
, pc
);
1148 void __trace_stack(struct trace_array
*tr
, unsigned long flags
, int skip
,
1151 __ftrace_trace_stack(tr
->buffer
, flags
, skip
, pc
);
1155 ftrace_trace_userstack(struct ring_buffer
*buffer
, unsigned long flags
, int pc
)
1157 struct ftrace_event_call
*call
= &event_user_stack
;
1158 struct ring_buffer_event
*event
;
1159 struct userstack_entry
*entry
;
1160 struct stack_trace trace
;
1162 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1165 event
= trace_buffer_lock_reserve(buffer
, TRACE_USER_STACK
,
1166 sizeof(*entry
), flags
, pc
);
1169 entry
= ring_buffer_event_data(event
);
1171 entry
->tgid
= current
->tgid
;
1172 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1174 trace
.nr_entries
= 0;
1175 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1177 trace
.entries
= entry
->caller
;
1179 save_stack_trace_user(&trace
);
1180 if (!filter_check_discard(call
, entry
, buffer
, event
))
1181 ring_buffer_unlock_commit(buffer
, event
);
1185 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1187 ftrace_trace_userstack(tr
, flags
, preempt_count());
1191 #endif /* CONFIG_STACKTRACE */
1194 ftrace_trace_special(void *__tr
,
1195 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1198 struct ftrace_event_call
*call
= &event_special
;
1199 struct ring_buffer_event
*event
;
1200 struct trace_array
*tr
= __tr
;
1201 struct ring_buffer
*buffer
= tr
->buffer
;
1202 struct special_entry
*entry
;
1204 event
= trace_buffer_lock_reserve(buffer
, TRACE_SPECIAL
,
1205 sizeof(*entry
), 0, pc
);
1208 entry
= ring_buffer_event_data(event
);
1213 if (!filter_check_discard(call
, entry
, buffer
, event
))
1214 trace_buffer_unlock_commit(buffer
, event
, 0, pc
);
1218 __trace_special(void *__tr
, void *__data
,
1219 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1221 ftrace_trace_special(__tr
, arg1
, arg2
, arg3
, preempt_count());
1225 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1227 struct trace_array
*tr
= &global_trace
;
1228 struct trace_array_cpu
*data
;
1229 unsigned long flags
;
1233 if (tracing_disabled
)
1236 pc
= preempt_count();
1237 local_irq_save(flags
);
1238 cpu
= raw_smp_processor_id();
1239 data
= tr
->data
[cpu
];
1241 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1242 ftrace_trace_special(tr
, arg1
, arg2
, arg3
, pc
);
1244 atomic_dec(&data
->disabled
);
1245 local_irq_restore(flags
);
1249 * trace_vbprintk - write binary msg to tracing buffer
1252 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1254 static raw_spinlock_t trace_buf_lock
=
1255 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
1256 static u32 trace_buf
[TRACE_BUF_SIZE
];
1258 struct ftrace_event_call
*call
= &event_bprint
;
1259 struct ring_buffer_event
*event
;
1260 struct ring_buffer
*buffer
;
1261 struct trace_array
*tr
= &global_trace
;
1262 struct trace_array_cpu
*data
;
1263 struct bprint_entry
*entry
;
1264 unsigned long flags
;
1267 int cpu
, len
= 0, size
, pc
;
1269 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1272 /* Don't pollute graph traces with trace_vprintk internals */
1273 pause_graph_tracing();
1275 pc
= preempt_count();
1276 resched
= ftrace_preempt_disable();
1277 cpu
= raw_smp_processor_id();
1278 data
= tr
->data
[cpu
];
1280 disable
= atomic_inc_return(&data
->disabled
);
1281 if (unlikely(disable
!= 1))
1284 /* Lockdep uses trace_printk for lock tracing */
1285 local_irq_save(flags
);
1286 __raw_spin_lock(&trace_buf_lock
);
1287 len
= vbin_printf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1289 if (len
> TRACE_BUF_SIZE
|| len
< 0)
1292 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1293 buffer
= tr
->buffer
;
1294 event
= trace_buffer_lock_reserve(buffer
, TRACE_BPRINT
, size
,
1298 entry
= ring_buffer_event_data(event
);
1302 memcpy(entry
->buf
, trace_buf
, sizeof(u32
) * len
);
1303 if (!filter_check_discard(call
, entry
, buffer
, event
))
1304 ring_buffer_unlock_commit(buffer
, event
);
1307 __raw_spin_unlock(&trace_buf_lock
);
1308 local_irq_restore(flags
);
1311 atomic_dec_return(&data
->disabled
);
1312 ftrace_preempt_enable(resched
);
1313 unpause_graph_tracing();
1317 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1319 int trace_array_printk(struct trace_array
*tr
,
1320 unsigned long ip
, const char *fmt
, ...)
1325 if (!(trace_flags
& TRACE_ITER_PRINTK
))
1329 ret
= trace_array_vprintk(tr
, ip
, fmt
, ap
);
1334 int trace_array_vprintk(struct trace_array
*tr
,
1335 unsigned long ip
, const char *fmt
, va_list args
)
1337 static raw_spinlock_t trace_buf_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
1338 static char trace_buf
[TRACE_BUF_SIZE
];
1340 struct ftrace_event_call
*call
= &event_print
;
1341 struct ring_buffer_event
*event
;
1342 struct ring_buffer
*buffer
;
1343 struct trace_array_cpu
*data
;
1344 int cpu
, len
= 0, size
, pc
;
1345 struct print_entry
*entry
;
1346 unsigned long irq_flags
;
1349 if (tracing_disabled
|| tracing_selftest_running
)
1352 pc
= preempt_count();
1353 preempt_disable_notrace();
1354 cpu
= raw_smp_processor_id();
1355 data
= tr
->data
[cpu
];
1357 disable
= atomic_inc_return(&data
->disabled
);
1358 if (unlikely(disable
!= 1))
1361 pause_graph_tracing();
1362 raw_local_irq_save(irq_flags
);
1363 __raw_spin_lock(&trace_buf_lock
);
1364 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1366 len
= min(len
, TRACE_BUF_SIZE
-1);
1369 size
= sizeof(*entry
) + len
+ 1;
1370 buffer
= tr
->buffer
;
1371 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
1375 entry
= ring_buffer_event_data(event
);
1378 memcpy(&entry
->buf
, trace_buf
, len
);
1379 entry
->buf
[len
] = 0;
1380 if (!filter_check_discard(call
, entry
, buffer
, event
))
1381 ring_buffer_unlock_commit(buffer
, event
);
1384 __raw_spin_unlock(&trace_buf_lock
);
1385 raw_local_irq_restore(irq_flags
);
1386 unpause_graph_tracing();
1388 atomic_dec_return(&data
->disabled
);
1389 preempt_enable_notrace();
1394 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1396 return trace_array_printk(&global_trace
, ip
, fmt
, args
);
1398 EXPORT_SYMBOL_GPL(trace_vprintk
);
1400 enum trace_file_type
{
1401 TRACE_FILE_LAT_FMT
= 1,
1402 TRACE_FILE_ANNOTATE
= 2,
1405 static void trace_iterator_increment(struct trace_iterator
*iter
)
1407 /* Don't allow ftrace to trace into the ring buffers */
1408 ftrace_disable_cpu();
1411 if (iter
->buffer_iter
[iter
->cpu
])
1412 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1414 ftrace_enable_cpu();
1417 static struct trace_entry
*
1418 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1420 struct ring_buffer_event
*event
;
1421 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1423 /* Don't allow ftrace to trace into the ring buffers */
1424 ftrace_disable_cpu();
1427 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1429 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1431 ftrace_enable_cpu();
1433 return event
? ring_buffer_event_data(event
) : NULL
;
1436 static struct trace_entry
*
1437 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1439 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1440 struct trace_entry
*ent
, *next
= NULL
;
1441 int cpu_file
= iter
->cpu_file
;
1442 u64 next_ts
= 0, ts
;
1447 * If we are in a per_cpu trace file, don't bother by iterating over
1448 * all cpu and peek directly.
1450 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1451 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1453 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
);
1455 *ent_cpu
= cpu_file
;
1460 for_each_tracing_cpu(cpu
) {
1462 if (ring_buffer_empty_cpu(buffer
, cpu
))
1465 ent
= peek_next_entry(iter
, cpu
, &ts
);
1468 * Pick the entry with the smallest timestamp:
1470 if (ent
&& (!next
|| ts
< next_ts
)) {
1478 *ent_cpu
= next_cpu
;
1486 /* Find the next real entry, without updating the iterator itself */
1487 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1488 int *ent_cpu
, u64
*ent_ts
)
1490 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1493 /* Find the next real entry, and increment the iterator to the next entry */
1494 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1496 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1499 trace_iterator_increment(iter
);
1501 return iter
->ent
? iter
: NULL
;
1504 static void trace_consume(struct trace_iterator
*iter
)
1506 /* Don't allow ftrace to trace into the ring buffers */
1507 ftrace_disable_cpu();
1508 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1509 ftrace_enable_cpu();
1512 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1514 struct trace_iterator
*iter
= m
->private;
1520 /* can't go backwards */
1525 ent
= find_next_entry_inc(iter
);
1529 while (ent
&& iter
->idx
< i
)
1530 ent
= find_next_entry_inc(iter
);
1537 static void tracing_iter_reset(struct trace_iterator
*iter
, int cpu
)
1539 struct trace_array
*tr
= iter
->tr
;
1540 struct ring_buffer_event
*event
;
1541 struct ring_buffer_iter
*buf_iter
;
1542 unsigned long entries
= 0;
1545 tr
->data
[cpu
]->skipped_entries
= 0;
1547 if (!iter
->buffer_iter
[cpu
])
1550 buf_iter
= iter
->buffer_iter
[cpu
];
1551 ring_buffer_iter_reset(buf_iter
);
1554 * We could have the case with the max latency tracers
1555 * that a reset never took place on a cpu. This is evident
1556 * by the timestamp being before the start of the buffer.
1558 while ((event
= ring_buffer_iter_peek(buf_iter
, &ts
))) {
1559 if (ts
>= iter
->tr
->time_start
)
1562 ring_buffer_read(buf_iter
, NULL
);
1565 tr
->data
[cpu
]->skipped_entries
= entries
;
1569 * No necessary locking here. The worst thing which can
1570 * happen is loosing events consumed at the same time
1571 * by a trace_pipe reader.
1572 * Other than that, we don't risk to crash the ring buffer
1573 * because it serializes the readers.
1575 * The current tracer is copied to avoid a global locking
1578 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1580 struct trace_iterator
*iter
= m
->private;
1581 static struct tracer
*old_tracer
;
1582 int cpu_file
= iter
->cpu_file
;
1587 /* copy the tracer to avoid using a global lock all around */
1588 mutex_lock(&trace_types_lock
);
1589 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1590 old_tracer
= current_trace
;
1591 *iter
->trace
= *current_trace
;
1593 mutex_unlock(&trace_types_lock
);
1595 atomic_inc(&trace_record_cmdline_disabled
);
1597 if (*pos
!= iter
->pos
) {
1602 ftrace_disable_cpu();
1604 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1605 for_each_tracing_cpu(cpu
)
1606 tracing_iter_reset(iter
, cpu
);
1608 tracing_iter_reset(iter
, cpu_file
);
1610 ftrace_enable_cpu();
1612 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1617 p
= s_next(m
, p
, &l
);
1620 trace_event_read_lock();
1624 static void s_stop(struct seq_file
*m
, void *p
)
1626 atomic_dec(&trace_record_cmdline_disabled
);
1627 trace_event_read_unlock();
1630 static void print_lat_help_header(struct seq_file
*m
)
1632 seq_puts(m
, "# _------=> CPU# \n");
1633 seq_puts(m
, "# / _-----=> irqs-off \n");
1634 seq_puts(m
, "# | / _----=> need-resched \n");
1635 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1636 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1637 seq_puts(m
, "# |||| /_--=> lock-depth \n");
1638 seq_puts(m
, "# |||||/ delay \n");
1639 seq_puts(m
, "# cmd pid |||||| time | caller \n");
1640 seq_puts(m
, "# \\ / |||||| \\ | / \n");
1643 static void print_func_help_header(struct seq_file
*m
)
1645 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1646 seq_puts(m
, "# | | | | |\n");
1651 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1653 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1654 struct trace_array
*tr
= iter
->tr
;
1655 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1656 struct tracer
*type
= current_trace
;
1657 unsigned long entries
= 0;
1658 unsigned long total
= 0;
1659 unsigned long count
;
1660 const char *name
= "preemption";
1667 for_each_tracing_cpu(cpu
) {
1668 count
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
1670 * If this buffer has skipped entries, then we hold all
1671 * entries for the trace and we need to ignore the
1672 * ones before the time stamp.
1674 if (tr
->data
[cpu
]->skipped_entries
) {
1675 count
-= tr
->data
[cpu
]->skipped_entries
;
1676 /* total is the same as the entries */
1680 ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
1684 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
1686 seq_puts(m
, "# -----------------------------------"
1687 "---------------------------------\n");
1688 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1689 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1690 nsecs_to_usecs(data
->saved_latency
),
1694 #if defined(CONFIG_PREEMPT_NONE)
1696 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1698 #elif defined(CONFIG_PREEMPT)
1703 /* These are reserved for later use */
1706 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1710 seq_puts(m
, "# -----------------\n");
1711 seq_printf(m
, "# | task: %.16s-%d "
1712 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1713 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1714 data
->policy
, data
->rt_priority
);
1715 seq_puts(m
, "# -----------------\n");
1717 if (data
->critical_start
) {
1718 seq_puts(m
, "# => started at: ");
1719 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1720 trace_print_seq(m
, &iter
->seq
);
1721 seq_puts(m
, "\n# => ended at: ");
1722 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1723 trace_print_seq(m
, &iter
->seq
);
1724 seq_puts(m
, "\n#\n");
1730 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1732 struct trace_seq
*s
= &iter
->seq
;
1734 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1737 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1740 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1743 if (iter
->tr
->data
[iter
->cpu
]->skipped_entries
)
1746 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1748 /* Don't print started cpu buffer for the first entry of the trace */
1750 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
1754 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1756 struct trace_seq
*s
= &iter
->seq
;
1757 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1758 struct trace_entry
*entry
;
1759 struct trace_event
*event
;
1763 test_cpu_buff_start(iter
);
1765 event
= ftrace_find_event(entry
->type
);
1767 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1768 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1769 if (!trace_print_lat_context(iter
))
1772 if (!trace_print_context(iter
))
1778 return event
->trace(iter
, sym_flags
);
1780 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1783 return TRACE_TYPE_HANDLED
;
1785 return TRACE_TYPE_PARTIAL_LINE
;
1788 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1790 struct trace_seq
*s
= &iter
->seq
;
1791 struct trace_entry
*entry
;
1792 struct trace_event
*event
;
1796 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1797 if (!trace_seq_printf(s
, "%d %d %llu ",
1798 entry
->pid
, iter
->cpu
, iter
->ts
))
1802 event
= ftrace_find_event(entry
->type
);
1804 return event
->raw(iter
, 0);
1806 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1809 return TRACE_TYPE_HANDLED
;
1811 return TRACE_TYPE_PARTIAL_LINE
;
1814 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1816 struct trace_seq
*s
= &iter
->seq
;
1817 unsigned char newline
= '\n';
1818 struct trace_entry
*entry
;
1819 struct trace_event
*event
;
1823 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1824 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1825 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1826 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1829 event
= ftrace_find_event(entry
->type
);
1831 enum print_line_t ret
= event
->hex(iter
, 0);
1832 if (ret
!= TRACE_TYPE_HANDLED
)
1836 SEQ_PUT_FIELD_RET(s
, newline
);
1838 return TRACE_TYPE_HANDLED
;
1841 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1843 struct trace_seq
*s
= &iter
->seq
;
1844 struct trace_entry
*entry
;
1845 struct trace_event
*event
;
1849 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1850 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1851 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1852 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1855 event
= ftrace_find_event(entry
->type
);
1856 return event
? event
->binary(iter
, 0) : TRACE_TYPE_HANDLED
;
1859 static int trace_empty(struct trace_iterator
*iter
)
1863 /* If we are looking at one CPU buffer, only check that one */
1864 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
1865 cpu
= iter
->cpu_file
;
1866 if (iter
->buffer_iter
[cpu
]) {
1867 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1870 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1876 for_each_tracing_cpu(cpu
) {
1877 if (iter
->buffer_iter
[cpu
]) {
1878 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1881 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1889 /* Called with trace_event_read_lock() held. */
1890 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1892 enum print_line_t ret
;
1894 if (iter
->trace
&& iter
->trace
->print_line
) {
1895 ret
= iter
->trace
->print_line(iter
);
1896 if (ret
!= TRACE_TYPE_UNHANDLED
)
1900 if (iter
->ent
->type
== TRACE_BPRINT
&&
1901 trace_flags
& TRACE_ITER_PRINTK
&&
1902 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1903 return trace_print_bprintk_msg_only(iter
);
1905 if (iter
->ent
->type
== TRACE_PRINT
&&
1906 trace_flags
& TRACE_ITER_PRINTK
&&
1907 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1908 return trace_print_printk_msg_only(iter
);
1910 if (trace_flags
& TRACE_ITER_BIN
)
1911 return print_bin_fmt(iter
);
1913 if (trace_flags
& TRACE_ITER_HEX
)
1914 return print_hex_fmt(iter
);
1916 if (trace_flags
& TRACE_ITER_RAW
)
1917 return print_raw_fmt(iter
);
1919 return print_trace_fmt(iter
);
1922 static int s_show(struct seq_file
*m
, void *v
)
1924 struct trace_iterator
*iter
= v
;
1926 if (iter
->ent
== NULL
) {
1928 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1931 if (iter
->trace
&& iter
->trace
->print_header
)
1932 iter
->trace
->print_header(m
);
1933 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1934 /* print nothing if the buffers are empty */
1935 if (trace_empty(iter
))
1937 print_trace_header(m
, iter
);
1938 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1939 print_lat_help_header(m
);
1941 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1942 print_func_help_header(m
);
1945 print_trace_line(iter
);
1946 trace_print_seq(m
, &iter
->seq
);
1952 static const struct seq_operations tracer_seq_ops
= {
1959 static struct trace_iterator
*
1960 __tracing_open(struct inode
*inode
, struct file
*file
)
1962 long cpu_file
= (long) inode
->i_private
;
1963 void *fail_ret
= ERR_PTR(-ENOMEM
);
1964 struct trace_iterator
*iter
;
1968 if (tracing_disabled
)
1969 return ERR_PTR(-ENODEV
);
1971 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1973 return ERR_PTR(-ENOMEM
);
1976 * We make a copy of the current tracer to avoid concurrent
1977 * changes on it while we are reading.
1979 mutex_lock(&trace_types_lock
);
1980 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
1985 *iter
->trace
= *current_trace
;
1987 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
1990 cpumask_clear(iter
->started
);
1992 if (current_trace
&& current_trace
->print_max
)
1995 iter
->tr
= &global_trace
;
1997 mutex_init(&iter
->mutex
);
1998 iter
->cpu_file
= cpu_file
;
2000 /* Notify the tracer early; before we stop tracing. */
2001 if (iter
->trace
&& iter
->trace
->open
)
2002 iter
->trace
->open(iter
);
2004 /* Annotate start of buffers if we had overruns */
2005 if (ring_buffer_overruns(iter
->tr
->buffer
))
2006 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
2008 /* stop the trace while dumping */
2011 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
2012 for_each_tracing_cpu(cpu
) {
2014 iter
->buffer_iter
[cpu
] =
2015 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
2016 tracing_iter_reset(iter
, cpu
);
2019 cpu
= iter
->cpu_file
;
2020 iter
->buffer_iter
[cpu
] =
2021 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
2022 tracing_iter_reset(iter
, cpu
);
2025 ret
= seq_open(file
, &tracer_seq_ops
);
2027 fail_ret
= ERR_PTR(ret
);
2031 m
= file
->private_data
;
2034 mutex_unlock(&trace_types_lock
);
2039 for_each_tracing_cpu(cpu
) {
2040 if (iter
->buffer_iter
[cpu
])
2041 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2043 free_cpumask_var(iter
->started
);
2046 mutex_unlock(&trace_types_lock
);
2053 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
2055 if (tracing_disabled
)
2058 filp
->private_data
= inode
->i_private
;
2062 static int tracing_release(struct inode
*inode
, struct file
*file
)
2064 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2065 struct trace_iterator
*iter
;
2068 if (!(file
->f_mode
& FMODE_READ
))
2073 mutex_lock(&trace_types_lock
);
2074 for_each_tracing_cpu(cpu
) {
2075 if (iter
->buffer_iter
[cpu
])
2076 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2079 if (iter
->trace
&& iter
->trace
->close
)
2080 iter
->trace
->close(iter
);
2082 /* reenable tracing if it was previously enabled */
2084 mutex_unlock(&trace_types_lock
);
2086 seq_release(inode
, file
);
2087 mutex_destroy(&iter
->mutex
);
2088 free_cpumask_var(iter
->started
);
2094 static int tracing_open(struct inode
*inode
, struct file
*file
)
2096 struct trace_iterator
*iter
;
2099 /* If this file was open for write, then erase contents */
2100 if ((file
->f_mode
& FMODE_WRITE
) &&
2101 (file
->f_flags
& O_TRUNC
)) {
2102 long cpu
= (long) inode
->i_private
;
2104 if (cpu
== TRACE_PIPE_ALL_CPU
)
2105 tracing_reset_online_cpus(&global_trace
);
2107 tracing_reset(&global_trace
, cpu
);
2110 if (file
->f_mode
& FMODE_READ
) {
2111 iter
= __tracing_open(inode
, file
);
2113 ret
= PTR_ERR(iter
);
2114 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2115 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2121 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2123 struct tracer
*t
= v
;
2133 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2138 mutex_lock(&trace_types_lock
);
2139 for (t
= trace_types
; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2145 static void t_stop(struct seq_file
*m
, void *p
)
2147 mutex_unlock(&trace_types_lock
);
2150 static int t_show(struct seq_file
*m
, void *v
)
2152 struct tracer
*t
= v
;
2157 seq_printf(m
, "%s", t
->name
);
2166 static const struct seq_operations show_traces_seq_ops
= {
2173 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2175 if (tracing_disabled
)
2178 return seq_open(file
, &show_traces_seq_ops
);
2182 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
2183 size_t count
, loff_t
*ppos
)
2188 static const struct file_operations tracing_fops
= {
2189 .open
= tracing_open
,
2191 .write
= tracing_write_stub
,
2192 .llseek
= seq_lseek
,
2193 .release
= tracing_release
,
2196 static const struct file_operations show_traces_fops
= {
2197 .open
= show_traces_open
,
2199 .release
= seq_release
,
2203 * Only trace on a CPU if the bitmask is set:
2205 static cpumask_var_t tracing_cpumask
;
2208 * The tracer itself will not take this lock, but still we want
2209 * to provide a consistent cpumask to user-space:
2211 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2214 * Temporary storage for the character representation of the
2215 * CPU bitmask (and one more byte for the newline):
2217 static char mask_str
[NR_CPUS
+ 1];
2220 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2221 size_t count
, loff_t
*ppos
)
2225 mutex_lock(&tracing_cpumask_update_lock
);
2227 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2228 if (count
- len
< 2) {
2232 len
+= sprintf(mask_str
+ len
, "\n");
2233 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2236 mutex_unlock(&tracing_cpumask_update_lock
);
2242 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2243 size_t count
, loff_t
*ppos
)
2246 cpumask_var_t tracing_cpumask_new
;
2248 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2251 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2255 mutex_lock(&tracing_cpumask_update_lock
);
2257 local_irq_disable();
2258 __raw_spin_lock(&ftrace_max_lock
);
2259 for_each_tracing_cpu(cpu
) {
2261 * Increase/decrease the disabled counter if we are
2262 * about to flip a bit in the cpumask:
2264 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2265 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2266 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2268 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2269 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2270 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2273 __raw_spin_unlock(&ftrace_max_lock
);
2276 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2278 mutex_unlock(&tracing_cpumask_update_lock
);
2279 free_cpumask_var(tracing_cpumask_new
);
2284 free_cpumask_var(tracing_cpumask_new
);
2289 static const struct file_operations tracing_cpumask_fops
= {
2290 .open
= tracing_open_generic
,
2291 .read
= tracing_cpumask_read
,
2292 .write
= tracing_cpumask_write
,
2296 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2297 size_t cnt
, loff_t
*ppos
)
2299 struct tracer_opt
*trace_opts
;
2307 /* calculate max size */
2308 for (i
= 0; trace_options
[i
]; i
++) {
2309 len
+= strlen(trace_options
[i
]);
2310 len
+= 3; /* "no" and newline */
2313 mutex_lock(&trace_types_lock
);
2314 tracer_flags
= current_trace
->flags
->val
;
2315 trace_opts
= current_trace
->flags
->opts
;
2318 * Increase the size with names of options specific
2319 * of the current tracer.
2321 for (i
= 0; trace_opts
[i
].name
; i
++) {
2322 len
+= strlen(trace_opts
[i
].name
);
2323 len
+= 3; /* "no" and newline */
2327 buf
= kmalloc(len
+ 1, GFP_KERNEL
);
2329 mutex_unlock(&trace_types_lock
);
2333 for (i
= 0; trace_options
[i
]; i
++) {
2334 if (trace_flags
& (1 << i
))
2335 r
+= sprintf(buf
+ r
, "%s\n", trace_options
[i
]);
2337 r
+= sprintf(buf
+ r
, "no%s\n", trace_options
[i
]);
2340 for (i
= 0; trace_opts
[i
].name
; i
++) {
2341 if (tracer_flags
& trace_opts
[i
].bit
)
2342 r
+= sprintf(buf
+ r
, "%s\n",
2343 trace_opts
[i
].name
);
2345 r
+= sprintf(buf
+ r
, "no%s\n",
2346 trace_opts
[i
].name
);
2348 mutex_unlock(&trace_types_lock
);
2350 WARN_ON(r
>= len
+ 1);
2352 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2358 /* Try to assign a tracer specific option */
2359 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2361 struct tracer_flags
*tracer_flags
= trace
->flags
;
2362 struct tracer_opt
*opts
= NULL
;
2366 for (i
= 0; tracer_flags
->opts
[i
].name
; i
++) {
2367 opts
= &tracer_flags
->opts
[i
];
2368 len
= strlen(opts
->name
);
2370 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2371 ret
= trace
->set_flag(tracer_flags
->val
,
2377 if (!tracer_flags
->opts
[i
].name
)
2380 /* Refused to handle */
2385 tracer_flags
->val
&= ~opts
->bit
;
2387 tracer_flags
->val
|= opts
->bit
;
2392 static void set_tracer_flags(unsigned int mask
, int enabled
)
2394 /* do nothing if flag is already set */
2395 if (!!(trace_flags
& mask
) == !!enabled
)
2399 trace_flags
|= mask
;
2401 trace_flags
&= ~mask
;
2405 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2406 size_t cnt
, loff_t
*ppos
)
2414 if (cnt
>= sizeof(buf
))
2417 if (copy_from_user(&buf
, ubuf
, cnt
))
2422 if (strncmp(buf
, "no", 2) == 0) {
2427 for (i
= 0; trace_options
[i
]; i
++) {
2428 int len
= strlen(trace_options
[i
]);
2430 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2431 set_tracer_flags(1 << i
, !neg
);
2436 /* If no option could be set, test the specific tracer options */
2437 if (!trace_options
[i
]) {
2438 mutex_lock(&trace_types_lock
);
2439 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2440 mutex_unlock(&trace_types_lock
);
2450 static const struct file_operations tracing_iter_fops
= {
2451 .open
= tracing_open_generic
,
2452 .read
= tracing_trace_options_read
,
2453 .write
= tracing_trace_options_write
,
2456 static const char readme_msg
[] =
2457 "tracing mini-HOWTO:\n\n"
2458 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2459 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2460 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2461 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2463 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2464 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2466 "# cat /sys/kernel/debug/tracing/trace_options\n"
2467 "noprint-parent nosym-offset nosym-addr noverbose\n"
2468 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2469 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2470 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2471 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2475 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2476 size_t cnt
, loff_t
*ppos
)
2478 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2479 readme_msg
, strlen(readme_msg
));
2482 static const struct file_operations tracing_readme_fops
= {
2483 .open
= tracing_open_generic
,
2484 .read
= tracing_readme_read
,
2488 tracing_saved_cmdlines_read(struct file
*file
, char __user
*ubuf
,
2489 size_t cnt
, loff_t
*ppos
)
2498 file_buf
= kmalloc(SAVED_CMDLINES
*(16+TASK_COMM_LEN
), GFP_KERNEL
);
2502 buf_comm
= kmalloc(TASK_COMM_LEN
, GFP_KERNEL
);
2510 for (i
= 0; i
< SAVED_CMDLINES
; i
++) {
2513 pid
= map_cmdline_to_pid
[i
];
2514 if (pid
== -1 || pid
== NO_CMDLINE_MAP
)
2517 trace_find_cmdline(pid
, buf_comm
);
2518 r
= sprintf(buf
, "%d %s\n", pid
, buf_comm
);
2523 len
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
2532 static const struct file_operations tracing_saved_cmdlines_fops
= {
2533 .open
= tracing_open_generic
,
2534 .read
= tracing_saved_cmdlines_read
,
2538 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2539 size_t cnt
, loff_t
*ppos
)
2544 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2545 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2549 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2550 size_t cnt
, loff_t
*ppos
)
2552 struct trace_array
*tr
= filp
->private_data
;
2557 if (cnt
>= sizeof(buf
))
2560 if (copy_from_user(&buf
, ubuf
, cnt
))
2565 ret
= strict_strtoul(buf
, 10, &val
);
2571 mutex_lock(&trace_types_lock
);
2572 if (tracer_enabled
^ val
) {
2575 if (current_trace
->start
)
2576 current_trace
->start(tr
);
2581 if (current_trace
->stop
)
2582 current_trace
->stop(tr
);
2585 mutex_unlock(&trace_types_lock
);
2593 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2594 size_t cnt
, loff_t
*ppos
)
2596 char buf
[MAX_TRACER_SIZE
+2];
2599 mutex_lock(&trace_types_lock
);
2601 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2603 r
= sprintf(buf
, "\n");
2604 mutex_unlock(&trace_types_lock
);
2606 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2609 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2611 tracing_reset_online_cpus(tr
);
2615 static int tracing_resize_ring_buffer(unsigned long size
)
2620 * If kernel or user changes the size of the ring buffer
2621 * we use the size that was given, and we can forget about
2622 * expanding it later.
2624 ring_buffer_expanded
= 1;
2626 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2630 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2634 r
= ring_buffer_resize(global_trace
.buffer
,
2635 global_trace
.entries
);
2638 * AARGH! We are left with different
2639 * size max buffer!!!!
2640 * The max buffer is our "snapshot" buffer.
2641 * When a tracer needs a snapshot (one of the
2642 * latency tracers), it swaps the max buffer
2643 * with the saved snap shot. We succeeded to
2644 * update the size of the main buffer, but failed to
2645 * update the size of the max buffer. But when we tried
2646 * to reset the main buffer to the original size, we
2647 * failed there too. This is very unlikely to
2648 * happen, but if it does, warn and kill all
2652 tracing_disabled
= 1;
2657 global_trace
.entries
= size
;
2663 * tracing_update_buffers - used by tracing facility to expand ring buffers
2665 * To save on memory when the tracing is never used on a system with it
2666 * configured in. The ring buffers are set to a minimum size. But once
2667 * a user starts to use the tracing facility, then they need to grow
2668 * to their default size.
2670 * This function is to be called when a tracer is about to be used.
2672 int tracing_update_buffers(void)
2676 mutex_lock(&trace_types_lock
);
2677 if (!ring_buffer_expanded
)
2678 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2679 mutex_unlock(&trace_types_lock
);
2684 struct trace_option_dentry
;
2686 static struct trace_option_dentry
*
2687 create_trace_option_files(struct tracer
*tracer
);
2690 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2692 static int tracing_set_tracer(const char *buf
)
2694 static struct trace_option_dentry
*topts
;
2695 struct trace_array
*tr
= &global_trace
;
2699 mutex_lock(&trace_types_lock
);
2701 if (!ring_buffer_expanded
) {
2702 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2708 for (t
= trace_types
; t
; t
= t
->next
) {
2709 if (strcmp(t
->name
, buf
) == 0)
2716 if (t
== current_trace
)
2719 trace_branch_disable();
2720 if (current_trace
&& current_trace
->reset
)
2721 current_trace
->reset(tr
);
2723 destroy_trace_option_files(topts
);
2727 topts
= create_trace_option_files(current_trace
);
2730 ret
= tracer_init(t
, tr
);
2735 trace_branch_enable(tr
);
2737 mutex_unlock(&trace_types_lock
);
2743 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2744 size_t cnt
, loff_t
*ppos
)
2746 char buf
[MAX_TRACER_SIZE
+1];
2753 if (cnt
> MAX_TRACER_SIZE
)
2754 cnt
= MAX_TRACER_SIZE
;
2756 if (copy_from_user(&buf
, ubuf
, cnt
))
2761 /* strip ending whitespace. */
2762 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2765 err
= tracing_set_tracer(buf
);
2775 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2776 size_t cnt
, loff_t
*ppos
)
2778 unsigned long *ptr
= filp
->private_data
;
2782 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2783 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2784 if (r
> sizeof(buf
))
2786 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2790 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2791 size_t cnt
, loff_t
*ppos
)
2793 unsigned long *ptr
= filp
->private_data
;
2798 if (cnt
>= sizeof(buf
))
2801 if (copy_from_user(&buf
, ubuf
, cnt
))
2806 ret
= strict_strtoul(buf
, 10, &val
);
2815 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2817 long cpu_file
= (long) inode
->i_private
;
2818 struct trace_iterator
*iter
;
2821 if (tracing_disabled
)
2824 mutex_lock(&trace_types_lock
);
2826 /* We only allow one reader per cpu */
2827 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
2828 if (!cpumask_empty(tracing_reader_cpumask
)) {
2832 cpumask_setall(tracing_reader_cpumask
);
2834 if (!cpumask_test_cpu(cpu_file
, tracing_reader_cpumask
))
2835 cpumask_set_cpu(cpu_file
, tracing_reader_cpumask
);
2842 /* create a buffer to store the information to pass to userspace */
2843 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2850 * We make a copy of the current tracer to avoid concurrent
2851 * changes on it while we are reading.
2853 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2859 *iter
->trace
= *current_trace
;
2861 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2866 /* trace pipe does not show start of buffer */
2867 cpumask_setall(iter
->started
);
2869 if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2870 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2872 iter
->cpu_file
= cpu_file
;
2873 iter
->tr
= &global_trace
;
2874 mutex_init(&iter
->mutex
);
2875 filp
->private_data
= iter
;
2877 if (iter
->trace
->pipe_open
)
2878 iter
->trace
->pipe_open(iter
);
2881 mutex_unlock(&trace_types_lock
);
2887 mutex_unlock(&trace_types_lock
);
2891 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2893 struct trace_iterator
*iter
= file
->private_data
;
2895 mutex_lock(&trace_types_lock
);
2897 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
)
2898 cpumask_clear(tracing_reader_cpumask
);
2900 cpumask_clear_cpu(iter
->cpu_file
, tracing_reader_cpumask
);
2902 mutex_unlock(&trace_types_lock
);
2904 free_cpumask_var(iter
->started
);
2905 mutex_destroy(&iter
->mutex
);
2913 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2915 struct trace_iterator
*iter
= filp
->private_data
;
2917 if (trace_flags
& TRACE_ITER_BLOCK
) {
2919 * Always select as readable when in blocking mode
2921 return POLLIN
| POLLRDNORM
;
2923 if (!trace_empty(iter
))
2924 return POLLIN
| POLLRDNORM
;
2925 poll_wait(filp
, &trace_wait
, poll_table
);
2926 if (!trace_empty(iter
))
2927 return POLLIN
| POLLRDNORM
;
2934 void default_wait_pipe(struct trace_iterator
*iter
)
2938 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
2940 if (trace_empty(iter
))
2943 finish_wait(&trace_wait
, &wait
);
2947 * This is a make-shift waitqueue.
2948 * A tracer might use this callback on some rare cases:
2950 * 1) the current tracer might hold the runqueue lock when it wakes up
2951 * a reader, hence a deadlock (sched, function, and function graph tracers)
2952 * 2) the function tracers, trace all functions, we don't want
2953 * the overhead of calling wake_up and friends
2954 * (and tracing them too)
2956 * Anyway, this is really very primitive wakeup.
2958 void poll_wait_pipe(struct trace_iterator
*iter
)
2960 set_current_state(TASK_INTERRUPTIBLE
);
2961 /* sleep for 100 msecs, and try again. */
2962 schedule_timeout(HZ
/ 10);
2965 /* Must be called with trace_types_lock mutex held. */
2966 static int tracing_wait_pipe(struct file
*filp
)
2968 struct trace_iterator
*iter
= filp
->private_data
;
2970 while (trace_empty(iter
)) {
2972 if ((filp
->f_flags
& O_NONBLOCK
)) {
2976 mutex_unlock(&iter
->mutex
);
2978 iter
->trace
->wait_pipe(iter
);
2980 mutex_lock(&iter
->mutex
);
2982 if (signal_pending(current
))
2986 * We block until we read something and tracing is disabled.
2987 * We still block if tracing is disabled, but we have never
2988 * read anything. This allows a user to cat this file, and
2989 * then enable tracing. But after we have read something,
2990 * we give an EOF when tracing is again disabled.
2992 * iter->pos will be 0 if we haven't read anything.
2994 if (!tracer_enabled
&& iter
->pos
)
3005 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
3006 size_t cnt
, loff_t
*ppos
)
3008 struct trace_iterator
*iter
= filp
->private_data
;
3009 static struct tracer
*old_tracer
;
3012 /* return any leftover data */
3013 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3017 trace_seq_init(&iter
->seq
);
3019 /* copy the tracer to avoid using a global lock all around */
3020 mutex_lock(&trace_types_lock
);
3021 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3022 old_tracer
= current_trace
;
3023 *iter
->trace
= *current_trace
;
3025 mutex_unlock(&trace_types_lock
);
3028 * Avoid more than one consumer on a single file descriptor
3029 * This is just a matter of traces coherency, the ring buffer itself
3032 mutex_lock(&iter
->mutex
);
3033 if (iter
->trace
->read
) {
3034 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
3040 sret
= tracing_wait_pipe(filp
);
3044 /* stop when tracing is finished */
3045 if (trace_empty(iter
)) {
3050 if (cnt
>= PAGE_SIZE
)
3051 cnt
= PAGE_SIZE
- 1;
3053 /* reset all but tr, trace, and overruns */
3054 memset(&iter
->seq
, 0,
3055 sizeof(struct trace_iterator
) -
3056 offsetof(struct trace_iterator
, seq
));
3059 trace_event_read_lock();
3060 while (find_next_entry_inc(iter
) != NULL
) {
3061 enum print_line_t ret
;
3062 int len
= iter
->seq
.len
;
3064 ret
= print_trace_line(iter
);
3065 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3066 /* don't print partial lines */
3067 iter
->seq
.len
= len
;
3070 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3071 trace_consume(iter
);
3073 if (iter
->seq
.len
>= cnt
)
3076 trace_event_read_unlock();
3078 /* Now copy what we have to the user */
3079 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3080 if (iter
->seq
.readpos
>= iter
->seq
.len
)
3081 trace_seq_init(&iter
->seq
);
3084 * If there was nothing to send to user, inspite of consuming trace
3085 * entries, go back to wait for more entries.
3091 mutex_unlock(&iter
->mutex
);
3096 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
3097 struct pipe_buffer
*buf
)
3099 __free_page(buf
->page
);
3102 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
3105 __free_page(spd
->pages
[idx
]);
3108 static struct pipe_buf_operations tracing_pipe_buf_ops
= {
3110 .map
= generic_pipe_buf_map
,
3111 .unmap
= generic_pipe_buf_unmap
,
3112 .confirm
= generic_pipe_buf_confirm
,
3113 .release
= tracing_pipe_buf_release
,
3114 .steal
= generic_pipe_buf_steal
,
3115 .get
= generic_pipe_buf_get
,
3119 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
3124 /* Seq buffer is page-sized, exactly what we need. */
3126 count
= iter
->seq
.len
;
3127 ret
= print_trace_line(iter
);
3128 count
= iter
->seq
.len
- count
;
3131 iter
->seq
.len
-= count
;
3134 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3135 iter
->seq
.len
-= count
;
3139 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3140 trace_consume(iter
);
3142 if (!find_next_entry_inc(iter
)) {
3152 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3154 struct pipe_inode_info
*pipe
,
3158 struct page
*pages
[PIPE_BUFFERS
];
3159 struct partial_page partial
[PIPE_BUFFERS
];
3160 struct trace_iterator
*iter
= filp
->private_data
;
3161 struct splice_pipe_desc spd
= {
3164 .nr_pages
= 0, /* This gets updated below. */
3166 .ops
= &tracing_pipe_buf_ops
,
3167 .spd_release
= tracing_spd_release_pipe
,
3169 static struct tracer
*old_tracer
;
3174 /* copy the tracer to avoid using a global lock all around */
3175 mutex_lock(&trace_types_lock
);
3176 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3177 old_tracer
= current_trace
;
3178 *iter
->trace
= *current_trace
;
3180 mutex_unlock(&trace_types_lock
);
3182 mutex_lock(&iter
->mutex
);
3184 if (iter
->trace
->splice_read
) {
3185 ret
= iter
->trace
->splice_read(iter
, filp
,
3186 ppos
, pipe
, len
, flags
);
3191 ret
= tracing_wait_pipe(filp
);
3195 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
3200 trace_event_read_lock();
3202 /* Fill as many pages as possible. */
3203 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
3204 pages
[i
] = alloc_page(GFP_KERNEL
);
3208 rem
= tracing_fill_pipe_page(rem
, iter
);
3210 /* Copy the data into the page, so we can start over. */
3211 ret
= trace_seq_to_buffer(&iter
->seq
,
3212 page_address(pages
[i
]),
3215 __free_page(pages
[i
]);
3218 partial
[i
].offset
= 0;
3219 partial
[i
].len
= iter
->seq
.len
;
3221 trace_seq_init(&iter
->seq
);
3224 trace_event_read_unlock();
3225 mutex_unlock(&iter
->mutex
);
3229 return splice_to_pipe(pipe
, &spd
);
3232 mutex_unlock(&iter
->mutex
);
3238 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3239 size_t cnt
, loff_t
*ppos
)
3241 struct trace_array
*tr
= filp
->private_data
;
3245 mutex_lock(&trace_types_lock
);
3246 if (!ring_buffer_expanded
)
3247 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3249 trace_buf_size
>> 10);
3251 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3252 mutex_unlock(&trace_types_lock
);
3254 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3258 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3259 size_t cnt
, loff_t
*ppos
)
3265 if (cnt
>= sizeof(buf
))
3268 if (copy_from_user(&buf
, ubuf
, cnt
))
3273 ret
= strict_strtoul(buf
, 10, &val
);
3277 /* must have at least 1 entry */
3281 mutex_lock(&trace_types_lock
);
3285 /* disable all cpu buffers */
3286 for_each_tracing_cpu(cpu
) {
3287 if (global_trace
.data
[cpu
])
3288 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3289 if (max_tr
.data
[cpu
])
3290 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3293 /* value is in KB */
3296 if (val
!= global_trace
.entries
) {
3297 ret
= tracing_resize_ring_buffer(val
);
3306 /* If check pages failed, return ENOMEM */
3307 if (tracing_disabled
)
3310 for_each_tracing_cpu(cpu
) {
3311 if (global_trace
.data
[cpu
])
3312 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3313 if (max_tr
.data
[cpu
])
3314 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3318 max_tr
.entries
= global_trace
.entries
;
3319 mutex_unlock(&trace_types_lock
);
3324 static int mark_printk(const char *fmt
, ...)
3328 va_start(args
, fmt
);
3329 ret
= trace_vprintk(0, fmt
, args
);
3335 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3336 size_t cnt
, loff_t
*fpos
)
3341 if (tracing_disabled
)
3344 if (cnt
> TRACE_BUF_SIZE
)
3345 cnt
= TRACE_BUF_SIZE
;
3347 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3351 if (copy_from_user(buf
, ubuf
, cnt
)) {
3356 /* Cut from the first nil or newline. */
3358 end
= strchr(buf
, '\n');
3362 cnt
= mark_printk("%s\n", buf
);
3369 static ssize_t
tracing_clock_read(struct file
*filp
, char __user
*ubuf
,
3370 size_t cnt
, loff_t
*ppos
)
3376 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++)
3377 bufiter
+= snprintf(buf
+ bufiter
, sizeof(buf
) - bufiter
,
3378 "%s%s%s%s", i
? " " : "",
3379 i
== trace_clock_id
? "[" : "", trace_clocks
[i
].name
,
3380 i
== trace_clock_id
? "]" : "");
3381 bufiter
+= snprintf(buf
+ bufiter
, sizeof(buf
) - bufiter
, "\n");
3383 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, bufiter
);
3386 static ssize_t
tracing_clock_write(struct file
*filp
, const char __user
*ubuf
,
3387 size_t cnt
, loff_t
*fpos
)
3390 const char *clockstr
;
3393 if (cnt
>= sizeof(buf
))
3396 if (copy_from_user(&buf
, ubuf
, cnt
))
3401 clockstr
= strstrip(buf
);
3403 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++) {
3404 if (strcmp(trace_clocks
[i
].name
, clockstr
) == 0)
3407 if (i
== ARRAY_SIZE(trace_clocks
))
3412 mutex_lock(&trace_types_lock
);
3414 ring_buffer_set_clock(global_trace
.buffer
, trace_clocks
[i
].func
);
3416 ring_buffer_set_clock(max_tr
.buffer
, trace_clocks
[i
].func
);
3418 mutex_unlock(&trace_types_lock
);
3425 static const struct file_operations tracing_max_lat_fops
= {
3426 .open
= tracing_open_generic
,
3427 .read
= tracing_max_lat_read
,
3428 .write
= tracing_max_lat_write
,
3431 static const struct file_operations tracing_ctrl_fops
= {
3432 .open
= tracing_open_generic
,
3433 .read
= tracing_ctrl_read
,
3434 .write
= tracing_ctrl_write
,
3437 static const struct file_operations set_tracer_fops
= {
3438 .open
= tracing_open_generic
,
3439 .read
= tracing_set_trace_read
,
3440 .write
= tracing_set_trace_write
,
3443 static const struct file_operations tracing_pipe_fops
= {
3444 .open
= tracing_open_pipe
,
3445 .poll
= tracing_poll_pipe
,
3446 .read
= tracing_read_pipe
,
3447 .splice_read
= tracing_splice_read_pipe
,
3448 .release
= tracing_release_pipe
,
3451 static const struct file_operations tracing_entries_fops
= {
3452 .open
= tracing_open_generic
,
3453 .read
= tracing_entries_read
,
3454 .write
= tracing_entries_write
,
3457 static const struct file_operations tracing_mark_fops
= {
3458 .open
= tracing_open_generic
,
3459 .write
= tracing_mark_write
,
3462 static const struct file_operations trace_clock_fops
= {
3463 .open
= tracing_open_generic
,
3464 .read
= tracing_clock_read
,
3465 .write
= tracing_clock_write
,
3468 struct ftrace_buffer_info
{
3469 struct trace_array
*tr
;
3475 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3477 int cpu
= (int)(long)inode
->i_private
;
3478 struct ftrace_buffer_info
*info
;
3480 if (tracing_disabled
)
3483 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3487 info
->tr
= &global_trace
;
3490 /* Force reading ring buffer for first read */
3491 info
->read
= (unsigned int)-1;
3493 filp
->private_data
= info
;
3495 return nonseekable_open(inode
, filp
);
3499 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3500 size_t count
, loff_t
*ppos
)
3502 struct ftrace_buffer_info
*info
= filp
->private_data
;
3511 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3515 /* Do we have previous read data to read? */
3516 if (info
->read
< PAGE_SIZE
)
3521 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3528 pos
= ring_buffer_page_len(info
->spare
);
3530 if (pos
< PAGE_SIZE
)
3531 memset(info
->spare
+ pos
, 0, PAGE_SIZE
- pos
);
3534 size
= PAGE_SIZE
- info
->read
;
3538 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3549 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3551 struct ftrace_buffer_info
*info
= file
->private_data
;
3554 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3561 struct ring_buffer
*buffer
;
3566 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3567 struct pipe_buffer
*buf
)
3569 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3574 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3579 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3580 struct pipe_buffer
*buf
)
3585 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3586 struct pipe_buffer
*buf
)
3588 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3593 /* Pipe buffer operations for a buffer. */
3594 static struct pipe_buf_operations buffer_pipe_buf_ops
= {
3596 .map
= generic_pipe_buf_map
,
3597 .unmap
= generic_pipe_buf_unmap
,
3598 .confirm
= generic_pipe_buf_confirm
,
3599 .release
= buffer_pipe_buf_release
,
3600 .steal
= buffer_pipe_buf_steal
,
3601 .get
= buffer_pipe_buf_get
,
3605 * Callback from splice_to_pipe(), if we need to release some pages
3606 * at the end of the spd in case we error'ed out in filling the pipe.
3608 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3610 struct buffer_ref
*ref
=
3611 (struct buffer_ref
*)spd
->partial
[i
].private;
3616 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3618 spd
->partial
[i
].private = 0;
3622 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3623 struct pipe_inode_info
*pipe
, size_t len
,
3626 struct ftrace_buffer_info
*info
= file
->private_data
;
3627 struct partial_page partial
[PIPE_BUFFERS
];
3628 struct page
*pages
[PIPE_BUFFERS
];
3629 struct splice_pipe_desc spd
= {
3633 .ops
= &buffer_pipe_buf_ops
,
3634 .spd_release
= buffer_spd_release
,
3636 struct buffer_ref
*ref
;
3637 int entries
, size
, i
;
3640 if (*ppos
& (PAGE_SIZE
- 1)) {
3641 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3645 if (len
& (PAGE_SIZE
- 1)) {
3646 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3647 if (len
< PAGE_SIZE
)
3652 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3654 for (i
= 0; i
< PIPE_BUFFERS
&& len
&& entries
; i
++, len
-= PAGE_SIZE
) {
3658 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3663 ref
->buffer
= info
->tr
->buffer
;
3664 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3670 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3673 ring_buffer_free_read_page(ref
->buffer
,
3680 * zero out any left over data, this is going to
3683 size
= ring_buffer_page_len(ref
->page
);
3684 if (size
< PAGE_SIZE
)
3685 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3687 page
= virt_to_page(ref
->page
);
3689 spd
.pages
[i
] = page
;
3690 spd
.partial
[i
].len
= PAGE_SIZE
;
3691 spd
.partial
[i
].offset
= 0;
3692 spd
.partial
[i
].private = (unsigned long)ref
;
3696 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3701 /* did we read anything? */
3702 if (!spd
.nr_pages
) {
3703 if (flags
& SPLICE_F_NONBLOCK
)
3711 ret
= splice_to_pipe(pipe
, &spd
);
3716 static const struct file_operations tracing_buffers_fops
= {
3717 .open
= tracing_buffers_open
,
3718 .read
= tracing_buffers_read
,
3719 .release
= tracing_buffers_release
,
3720 .splice_read
= tracing_buffers_splice_read
,
3721 .llseek
= no_llseek
,
3725 tracing_stats_read(struct file
*filp
, char __user
*ubuf
,
3726 size_t count
, loff_t
*ppos
)
3728 unsigned long cpu
= (unsigned long)filp
->private_data
;
3729 struct trace_array
*tr
= &global_trace
;
3730 struct trace_seq
*s
;
3733 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
3739 cnt
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
3740 trace_seq_printf(s
, "entries: %ld\n", cnt
);
3742 cnt
= ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
3743 trace_seq_printf(s
, "overrun: %ld\n", cnt
);
3745 cnt
= ring_buffer_commit_overrun_cpu(tr
->buffer
, cpu
);
3746 trace_seq_printf(s
, "commit overrun: %ld\n", cnt
);
3748 count
= simple_read_from_buffer(ubuf
, count
, ppos
, s
->buffer
, s
->len
);
3755 static const struct file_operations tracing_stats_fops
= {
3756 .open
= tracing_open_generic
,
3757 .read
= tracing_stats_read
,
3760 #ifdef CONFIG_DYNAMIC_FTRACE
3762 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3768 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3769 size_t cnt
, loff_t
*ppos
)
3771 static char ftrace_dyn_info_buffer
[1024];
3772 static DEFINE_MUTEX(dyn_info_mutex
);
3773 unsigned long *p
= filp
->private_data
;
3774 char *buf
= ftrace_dyn_info_buffer
;
3775 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3778 mutex_lock(&dyn_info_mutex
);
3779 r
= sprintf(buf
, "%ld ", *p
);
3781 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3784 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3786 mutex_unlock(&dyn_info_mutex
);
3791 static const struct file_operations tracing_dyn_info_fops
= {
3792 .open
= tracing_open_generic
,
3793 .read
= tracing_read_dyn_info
,
3797 static struct dentry
*d_tracer
;
3799 struct dentry
*tracing_init_dentry(void)
3806 if (!debugfs_initialized())
3809 d_tracer
= debugfs_create_dir("tracing", NULL
);
3811 if (!d_tracer
&& !once
) {
3813 pr_warning("Could not create debugfs directory 'tracing'\n");
3820 static struct dentry
*d_percpu
;
3822 struct dentry
*tracing_dentry_percpu(void)
3825 struct dentry
*d_tracer
;
3830 d_tracer
= tracing_init_dentry();
3835 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
3837 if (!d_percpu
&& !once
) {
3839 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3846 static void tracing_init_debugfs_percpu(long cpu
)
3848 struct dentry
*d_percpu
= tracing_dentry_percpu();
3849 struct dentry
*d_cpu
;
3850 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3853 if (cpu
> 999 || cpu
< 0)
3856 sprintf(cpu_dir
, "cpu%ld", cpu
);
3857 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
3859 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
3863 /* per cpu trace_pipe */
3864 trace_create_file("trace_pipe", 0444, d_cpu
,
3865 (void *) cpu
, &tracing_pipe_fops
);
3868 trace_create_file("trace", 0644, d_cpu
,
3869 (void *) cpu
, &tracing_fops
);
3871 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
3872 (void *) cpu
, &tracing_buffers_fops
);
3874 trace_create_file("stats", 0444, d_cpu
,
3875 (void *) cpu
, &tracing_stats_fops
);
3878 #ifdef CONFIG_FTRACE_SELFTEST
3879 /* Let selftest have access to static functions in this file */
3880 #include "trace_selftest.c"
3883 struct trace_option_dentry
{
3884 struct tracer_opt
*opt
;
3885 struct tracer_flags
*flags
;
3886 struct dentry
*entry
;
3890 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3893 struct trace_option_dentry
*topt
= filp
->private_data
;
3896 if (topt
->flags
->val
& topt
->opt
->bit
)
3901 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3905 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3908 struct trace_option_dentry
*topt
= filp
->private_data
;
3913 if (cnt
>= sizeof(buf
))
3916 if (copy_from_user(&buf
, ubuf
, cnt
))
3921 ret
= strict_strtoul(buf
, 10, &val
);
3928 /* do nothing if already cleared */
3929 if (!(topt
->flags
->val
& topt
->opt
->bit
))
3932 mutex_lock(&trace_types_lock
);
3933 if (current_trace
->set_flag
)
3934 ret
= current_trace
->set_flag(topt
->flags
->val
,
3936 mutex_unlock(&trace_types_lock
);
3939 topt
->flags
->val
&= ~topt
->opt
->bit
;
3942 /* do nothing if already set */
3943 if (topt
->flags
->val
& topt
->opt
->bit
)
3946 mutex_lock(&trace_types_lock
);
3947 if (current_trace
->set_flag
)
3948 ret
= current_trace
->set_flag(topt
->flags
->val
,
3950 mutex_unlock(&trace_types_lock
);
3953 topt
->flags
->val
|= topt
->opt
->bit
;
3966 static const struct file_operations trace_options_fops
= {
3967 .open
= tracing_open_generic
,
3968 .read
= trace_options_read
,
3969 .write
= trace_options_write
,
3973 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3976 long index
= (long)filp
->private_data
;
3979 if (trace_flags
& (1 << index
))
3984 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3988 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3991 long index
= (long)filp
->private_data
;
3996 if (cnt
>= sizeof(buf
))
3999 if (copy_from_user(&buf
, ubuf
, cnt
))
4004 ret
= strict_strtoul(buf
, 10, &val
);
4008 if (val
!= 0 && val
!= 1)
4010 set_tracer_flags(1 << index
, val
);
4017 static const struct file_operations trace_options_core_fops
= {
4018 .open
= tracing_open_generic
,
4019 .read
= trace_options_core_read
,
4020 .write
= trace_options_core_write
,
4023 struct dentry
*trace_create_file(const char *name
,
4025 struct dentry
*parent
,
4027 const struct file_operations
*fops
)
4031 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
4033 pr_warning("Could not create debugfs '%s' entry\n", name
);
4039 static struct dentry
*trace_options_init_dentry(void)
4041 struct dentry
*d_tracer
;
4042 static struct dentry
*t_options
;
4047 d_tracer
= tracing_init_dentry();
4051 t_options
= debugfs_create_dir("options", d_tracer
);
4053 pr_warning("Could not create debugfs directory 'options'\n");
4061 create_trace_option_file(struct trace_option_dentry
*topt
,
4062 struct tracer_flags
*flags
,
4063 struct tracer_opt
*opt
)
4065 struct dentry
*t_options
;
4067 t_options
= trace_options_init_dentry();
4071 topt
->flags
= flags
;
4074 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
4075 &trace_options_fops
);
4079 static struct trace_option_dentry
*
4080 create_trace_option_files(struct tracer
*tracer
)
4082 struct trace_option_dentry
*topts
;
4083 struct tracer_flags
*flags
;
4084 struct tracer_opt
*opts
;
4090 flags
= tracer
->flags
;
4092 if (!flags
|| !flags
->opts
)
4097 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4100 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
4104 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4105 create_trace_option_file(&topts
[cnt
], flags
,
4112 destroy_trace_option_files(struct trace_option_dentry
*topts
)
4119 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
4120 if (topts
[cnt
].entry
)
4121 debugfs_remove(topts
[cnt
].entry
);
4127 static struct dentry
*
4128 create_trace_option_core_file(const char *option
, long index
)
4130 struct dentry
*t_options
;
4132 t_options
= trace_options_init_dentry();
4136 return trace_create_file(option
, 0644, t_options
, (void *)index
,
4137 &trace_options_core_fops
);
4140 static __init
void create_trace_options_dir(void)
4142 struct dentry
*t_options
;
4145 t_options
= trace_options_init_dentry();
4149 for (i
= 0; trace_options
[i
]; i
++)
4150 create_trace_option_core_file(trace_options
[i
], i
);
4153 static __init
int tracer_init_debugfs(void)
4155 struct dentry
*d_tracer
;
4158 d_tracer
= tracing_init_dentry();
4160 trace_create_file("tracing_enabled", 0644, d_tracer
,
4161 &global_trace
, &tracing_ctrl_fops
);
4163 trace_create_file("trace_options", 0644, d_tracer
,
4164 NULL
, &tracing_iter_fops
);
4166 trace_create_file("tracing_cpumask", 0644, d_tracer
,
4167 NULL
, &tracing_cpumask_fops
);
4169 trace_create_file("trace", 0644, d_tracer
,
4170 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
4172 trace_create_file("available_tracers", 0444, d_tracer
,
4173 &global_trace
, &show_traces_fops
);
4175 trace_create_file("current_tracer", 0644, d_tracer
,
4176 &global_trace
, &set_tracer_fops
);
4178 #ifdef CONFIG_TRACER_MAX_TRACE
4179 trace_create_file("tracing_max_latency", 0644, d_tracer
,
4180 &tracing_max_latency
, &tracing_max_lat_fops
);
4182 trace_create_file("tracing_thresh", 0644, d_tracer
,
4183 &tracing_thresh
, &tracing_max_lat_fops
);
4186 trace_create_file("README", 0444, d_tracer
,
4187 NULL
, &tracing_readme_fops
);
4189 trace_create_file("trace_pipe", 0444, d_tracer
,
4190 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
4192 trace_create_file("buffer_size_kb", 0644, d_tracer
,
4193 &global_trace
, &tracing_entries_fops
);
4195 trace_create_file("trace_marker", 0220, d_tracer
,
4196 NULL
, &tracing_mark_fops
);
4198 trace_create_file("saved_cmdlines", 0444, d_tracer
,
4199 NULL
, &tracing_saved_cmdlines_fops
);
4201 trace_create_file("trace_clock", 0644, d_tracer
, NULL
,
4204 #ifdef CONFIG_DYNAMIC_FTRACE
4205 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
4206 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
4208 #ifdef CONFIG_SYSPROF_TRACER
4209 init_tracer_sysprof_debugfs(d_tracer
);
4212 create_trace_options_dir();
4214 for_each_tracing_cpu(cpu
)
4215 tracing_init_debugfs_percpu(cpu
);
4220 static int trace_panic_handler(struct notifier_block
*this,
4221 unsigned long event
, void *unused
)
4223 if (ftrace_dump_on_oops
)
4228 static struct notifier_block trace_panic_notifier
= {
4229 .notifier_call
= trace_panic_handler
,
4231 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
4234 static int trace_die_handler(struct notifier_block
*self
,
4240 if (ftrace_dump_on_oops
)
4249 static struct notifier_block trace_die_notifier
= {
4250 .notifier_call
= trace_die_handler
,
4255 * printk is set to max of 1024, we really don't need it that big.
4256 * Nothing should be printing 1000 characters anyway.
4258 #define TRACE_MAX_PRINT 1000
4261 * Define here KERN_TRACE so that we have one place to modify
4262 * it if we decide to change what log level the ftrace dump
4265 #define KERN_TRACE KERN_EMERG
4268 trace_printk_seq(struct trace_seq
*s
)
4270 /* Probably should print a warning here. */
4274 /* should be zero ended, but we are paranoid. */
4275 s
->buffer
[s
->len
] = 0;
4277 printk(KERN_TRACE
"%s", s
->buffer
);
4282 static void __ftrace_dump(bool disable_tracing
)
4284 static raw_spinlock_t ftrace_dump_lock
=
4285 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
4286 /* use static because iter can be a bit big for the stack */
4287 static struct trace_iterator iter
;
4288 unsigned int old_userobj
;
4289 static int dump_ran
;
4290 unsigned long flags
;
4294 local_irq_save(flags
);
4295 __raw_spin_lock(&ftrace_dump_lock
);
4303 if (disable_tracing
)
4306 for_each_tracing_cpu(cpu
) {
4307 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
4310 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
4312 /* don't look at user memory in panic mode */
4313 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4315 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4317 /* Simulate the iterator */
4318 iter
.tr
= &global_trace
;
4319 iter
.trace
= current_trace
;
4320 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4323 * We need to stop all tracing on all CPUS to read the
4324 * the next buffer. This is a bit expensive, but is
4325 * not done often. We fill all what we can read,
4326 * and then release the locks again.
4329 while (!trace_empty(&iter
)) {
4332 printk(KERN_TRACE
"---------------------------------\n");
4336 /* reset all but tr, trace, and overruns */
4337 memset(&iter
.seq
, 0,
4338 sizeof(struct trace_iterator
) -
4339 offsetof(struct trace_iterator
, seq
));
4340 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4343 if (find_next_entry_inc(&iter
) != NULL
) {
4346 ret
= print_trace_line(&iter
);
4347 if (ret
!= TRACE_TYPE_NO_CONSUME
)
4348 trace_consume(&iter
);
4351 trace_printk_seq(&iter
.seq
);
4355 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4357 printk(KERN_TRACE
"---------------------------------\n");
4359 /* Re-enable tracing if requested */
4360 if (!disable_tracing
) {
4361 trace_flags
|= old_userobj
;
4363 for_each_tracing_cpu(cpu
) {
4364 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
4370 __raw_spin_unlock(&ftrace_dump_lock
);
4371 local_irq_restore(flags
);
4374 /* By default: disable tracing after the dump */
4375 void ftrace_dump(void)
4377 __ftrace_dump(true);
4380 __init
static int tracer_alloc_buffers(void)
4386 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4389 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4390 goto out_free_buffer_mask
;
4392 if (!alloc_cpumask_var(&tracing_reader_cpumask
, GFP_KERNEL
))
4393 goto out_free_tracing_cpumask
;
4395 /* To save memory, keep the ring buffer size to its minimum */
4396 if (ring_buffer_expanded
)
4397 ring_buf_size
= trace_buf_size
;
4401 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4402 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4403 cpumask_clear(tracing_reader_cpumask
);
4405 /* TODO: make the number of buffers hot pluggable with CPUS */
4406 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
,
4407 TRACE_BUFFER_FLAGS
);
4408 if (!global_trace
.buffer
) {
4409 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4411 goto out_free_cpumask
;
4413 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4416 #ifdef CONFIG_TRACER_MAX_TRACE
4417 max_tr
.buffer
= ring_buffer_alloc(ring_buf_size
,
4418 TRACE_BUFFER_FLAGS
);
4419 if (!max_tr
.buffer
) {
4420 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4422 ring_buffer_free(global_trace
.buffer
);
4423 goto out_free_cpumask
;
4425 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
4426 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
4429 /* Allocate the first page for all buffers */
4430 for_each_tracing_cpu(i
) {
4431 global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4432 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
4435 trace_init_cmdlines();
4437 register_tracer(&nop_trace
);
4438 current_trace
= &nop_trace
;
4439 #ifdef CONFIG_BOOT_TRACER
4440 register_tracer(&boot_tracer
);
4442 /* All seems OK, enable tracing */
4443 tracing_disabled
= 0;
4445 atomic_notifier_chain_register(&panic_notifier_list
,
4446 &trace_panic_notifier
);
4448 register_die_notifier(&trace_die_notifier
);
4453 free_cpumask_var(tracing_reader_cpumask
);
4454 out_free_tracing_cpumask
:
4455 free_cpumask_var(tracing_cpumask
);
4456 out_free_buffer_mask
:
4457 free_cpumask_var(tracing_buffer_mask
);
4462 __init
static int clear_boot_tracer(void)
4465 * The default tracer at boot buffer is an init section.
4466 * This function is called in lateinit. If we did not
4467 * find the boot tracer, then clear it out, to prevent
4468 * later registration from accessing the buffer that is
4469 * about to be freed.
4471 if (!default_bootup_tracer
)
4474 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4475 default_bootup_tracer
);
4476 default_bootup_tracer
= NULL
;
4481 early_initcall(tracer_alloc_buffers
);
4482 fs_initcall(tracer_init_debugfs
);
4483 late_initcall(clear_boot_tracer
);