2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/ring_buffer.h>
15 #include <linux/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/smp_lock.h>
21 #include <linux/notifier.h>
22 #include <linux/irqflags.h>
23 #include <linux/debugfs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/kprobes.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/ctype.h>
36 #include <linux/init.h>
37 #include <linux/poll.h>
38 #include <linux/gfp.h>
42 #include "trace_output.h"
44 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
47 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
50 int ring_buffer_expanded
;
53 * We need to change this state when a selftest is running.
54 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
56 * insertions into the ring-buffer such as trace_printk could occurred
57 * at the same time, giving false positive or negative results.
59 static bool __read_mostly tracing_selftest_running
;
62 * If a tracer is running, we do not want to run SELFTEST.
64 bool __read_mostly tracing_selftest_disabled
;
66 /* For tracers that don't implement custom flags */
67 static struct tracer_opt dummy_tracer_opt
[] = {
71 static struct tracer_flags dummy_tracer_flags
= {
73 .opts
= dummy_tracer_opt
76 static int dummy_set_flag(u32 old_flags
, u32 bit
, int set
)
82 * Kill all tracing for good (never come back).
83 * It is initialized to 1 but will turn to zero if the initialization
84 * of the tracer is successful. But that is the only place that sets
87 static int tracing_disabled
= 1;
89 DEFINE_PER_CPU(local_t
, ftrace_cpu_disabled
);
91 static inline void ftrace_disable_cpu(void)
94 local_inc(&__get_cpu_var(ftrace_cpu_disabled
));
97 static inline void ftrace_enable_cpu(void)
99 local_dec(&__get_cpu_var(ftrace_cpu_disabled
));
103 static cpumask_var_t __read_mostly tracing_buffer_mask
;
105 /* Define which cpu buffers are currently read in trace_pipe */
106 static cpumask_var_t tracing_reader_cpumask
;
108 #define for_each_tracing_cpu(cpu) \
109 for_each_cpu(cpu, tracing_buffer_mask)
112 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
114 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
115 * is set, then ftrace_dump is called. This will output the contents
116 * of the ftrace buffers to the console. This is very useful for
117 * capturing traces that lead to crashes and outputing it to a
120 * It is default off, but you can enable it with either specifying
121 * "ftrace_dump_on_oops" in the kernel command line, or setting
122 * /proc/sys/kernel/ftrace_dump_on_oops to true.
124 int ftrace_dump_on_oops
;
126 static int tracing_set_tracer(const char *buf
);
128 #define BOOTUP_TRACER_SIZE 100
129 static char bootup_tracer_buf
[BOOTUP_TRACER_SIZE
] __initdata
;
130 static char *default_bootup_tracer
;
132 static int __init
set_ftrace(char *str
)
134 strncpy(bootup_tracer_buf
, str
, BOOTUP_TRACER_SIZE
);
135 default_bootup_tracer
= bootup_tracer_buf
;
136 /* We are using ftrace early, expand it */
137 ring_buffer_expanded
= 1;
140 __setup("ftrace=", set_ftrace
);
142 static int __init
set_ftrace_dump_on_oops(char *str
)
144 ftrace_dump_on_oops
= 1;
147 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
149 unsigned long long ns2usecs(cycle_t nsec
)
157 * The global_trace is the descriptor that holds the tracing
158 * buffers for the live tracing. For each CPU, it contains
159 * a link list of pages that will store trace entries. The
160 * page descriptor of the pages in the memory is used to hold
161 * the link list by linking the lru item in the page descriptor
162 * to each of the pages in the buffer per CPU.
164 * For each active CPU there is a data field that holds the
165 * pages for the buffer for that CPU. Each CPU has the same number
166 * of pages allocated for its buffer.
168 static struct trace_array global_trace
;
170 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
172 int filter_current_check_discard(struct ring_buffer
*buffer
,
173 struct ftrace_event_call
*call
, void *rec
,
174 struct ring_buffer_event
*event
)
176 return filter_check_discard(call
, rec
, buffer
, event
);
178 EXPORT_SYMBOL_GPL(filter_current_check_discard
);
180 cycle_t
ftrace_now(int cpu
)
184 /* Early boot up does not have a buffer yet */
185 if (!global_trace
.buffer
)
186 return trace_clock_local();
188 ts
= ring_buffer_time_stamp(global_trace
.buffer
, cpu
);
189 ring_buffer_normalize_time_stamp(global_trace
.buffer
, cpu
, &ts
);
195 * The max_tr is used to snapshot the global_trace when a maximum
196 * latency is reached. Some tracers will use this to store a maximum
197 * trace while it continues examining live traces.
199 * The buffers for the max_tr are set up the same as the global_trace.
200 * When a snapshot is taken, the link list of the max_tr is swapped
201 * with the link list of the global_trace and the buffers are reset for
202 * the global_trace so the tracing can continue.
204 static struct trace_array max_tr
;
206 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
208 /* tracer_enabled is used to toggle activation of a tracer */
209 static int tracer_enabled
= 1;
212 * tracing_is_enabled - return tracer_enabled status
214 * This function is used by other tracers to know the status
215 * of the tracer_enabled flag. Tracers may use this function
216 * to know if it should enable their features when starting
217 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
219 int tracing_is_enabled(void)
221 return tracer_enabled
;
225 * trace_buf_size is the size in bytes that is allocated
226 * for a buffer. Note, the number of bytes is always rounded
229 * This number is purposely set to a low number of 16384.
230 * If the dump on oops happens, it will be much appreciated
231 * to not have to wait for all that output. Anyway this can be
232 * boot time and run time configurable.
234 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
236 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
238 /* trace_types holds a link list of available tracers. */
239 static struct tracer
*trace_types __read_mostly
;
241 /* current_trace points to the tracer that is currently active */
242 static struct tracer
*current_trace __read_mostly
;
245 * max_tracer_type_len is used to simplify the allocating of
246 * buffers to read userspace tracer names. We keep track of
247 * the longest tracer name registered.
249 static int max_tracer_type_len
;
252 * trace_types_lock is used to protect the trace_types list.
253 * This lock is also used to keep user access serialized.
254 * Accesses from userspace will grab this lock while userspace
255 * activities happen inside the kernel.
257 static DEFINE_MUTEX(trace_types_lock
);
259 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
260 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
262 /* trace_flags holds trace_options default values */
263 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
264 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
| TRACE_ITER_SLEEP_TIME
|
265 TRACE_ITER_GRAPH_TIME
;
267 static int trace_stop_count
;
268 static DEFINE_SPINLOCK(tracing_start_lock
);
271 * trace_wake_up - wake up tasks waiting for trace input
273 * Simply wakes up any task that is blocked on the trace_wait
274 * queue. These is used with trace_poll for tasks polling the trace.
276 void trace_wake_up(void)
279 * The runqueue_is_locked() can fail, but this is the best we
282 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
283 wake_up(&trace_wait
);
286 static int __init
set_buf_size(char *str
)
288 unsigned long buf_size
;
292 buf_size
= memparse(str
, &str
);
293 /* nr_entries can not be zero */
296 trace_buf_size
= buf_size
;
299 __setup("trace_buf_size=", set_buf_size
);
301 unsigned long nsecs_to_usecs(unsigned long nsecs
)
306 /* These must match the bit postions in trace_iterator_flags */
307 static const char *trace_options
[] = {
336 { trace_clock_local
, "local" },
337 { trace_clock_global
, "global" },
343 * trace_parser_get_init - gets the buffer for trace parser
345 int trace_parser_get_init(struct trace_parser
*parser
, int size
)
347 memset(parser
, 0, sizeof(*parser
));
349 parser
->buffer
= kmalloc(size
, GFP_KERNEL
);
358 * trace_parser_put - frees the buffer for trace parser
360 void trace_parser_put(struct trace_parser
*parser
)
362 kfree(parser
->buffer
);
366 * trace_get_user - reads the user input string separated by space
367 * (matched by isspace(ch))
369 * For each string found the 'struct trace_parser' is updated,
370 * and the function returns.
372 * Returns number of bytes read.
374 * See kernel/trace/trace.h for 'struct trace_parser' details.
376 int trace_get_user(struct trace_parser
*parser
, const char __user
*ubuf
,
377 size_t cnt
, loff_t
*ppos
)
384 trace_parser_clear(parser
);
386 ret
= get_user(ch
, ubuf
++);
394 * The parser is not finished with the last write,
395 * continue reading the user input without skipping spaces.
398 /* skip white space */
399 while (cnt
&& isspace(ch
)) {
400 ret
= get_user(ch
, ubuf
++);
407 /* only spaces were written */
417 /* read the non-space input */
418 while (cnt
&& !isspace(ch
)) {
419 if (parser
->idx
< parser
->size
)
420 parser
->buffer
[parser
->idx
++] = ch
;
425 ret
= get_user(ch
, ubuf
++);
432 /* We either got finished input or we have to wait for another call. */
434 parser
->buffer
[parser
->idx
] = 0;
435 parser
->cont
= false;
438 parser
->buffer
[parser
->idx
++] = ch
;
448 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
456 if (s
->len
<= s
->readpos
)
459 len
= s
->len
- s
->readpos
;
462 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
472 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
477 if (s
->len
<= s
->readpos
)
480 len
= s
->len
- s
->readpos
;
483 ret
= memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
492 * ftrace_max_lock is used to protect the swapping of buffers
493 * when taking a max snapshot. The buffers themselves are
494 * protected by per_cpu spinlocks. But the action of the swap
495 * needs its own lock.
497 * This is defined as a raw_spinlock_t in order to help
498 * with performance when lockdep debugging is enabled.
500 * It is also used in other places outside the update_max_tr
501 * so it needs to be defined outside of the
502 * CONFIG_TRACER_MAX_TRACE.
504 static raw_spinlock_t ftrace_max_lock
=
505 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
507 #ifdef CONFIG_TRACER_MAX_TRACE
508 unsigned long __read_mostly tracing_max_latency
;
509 unsigned long __read_mostly tracing_thresh
;
512 * Copy the new maximum trace into the separate maximum-trace
513 * structure. (this way the maximum trace is permanently saved,
514 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
517 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
519 struct trace_array_cpu
*data
= tr
->data
[cpu
];
520 struct trace_array_cpu
*max_data
= tr
->data
[cpu
];
523 max_tr
.time_start
= data
->preempt_timestamp
;
525 max_data
= max_tr
.data
[cpu
];
526 max_data
->saved_latency
= tracing_max_latency
;
527 max_data
->critical_start
= data
->critical_start
;
528 max_data
->critical_end
= data
->critical_end
;
530 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
531 max_data
->pid
= tsk
->pid
;
532 max_data
->uid
= task_uid(tsk
);
533 max_data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
534 max_data
->policy
= tsk
->policy
;
535 max_data
->rt_priority
= tsk
->rt_priority
;
537 /* record this tasks comm */
538 tracing_record_cmdline(tsk
);
542 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
544 * @tsk: the task with the latency
545 * @cpu: The cpu that initiated the trace.
547 * Flip the buffers between the @tr and the max_tr and record information
548 * about which task was the cause of this latency.
551 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
553 struct ring_buffer
*buf
= tr
->buffer
;
555 if (trace_stop_count
)
558 WARN_ON_ONCE(!irqs_disabled());
559 __raw_spin_lock(&ftrace_max_lock
);
561 tr
->buffer
= max_tr
.buffer
;
564 __update_max_tr(tr
, tsk
, cpu
);
565 __raw_spin_unlock(&ftrace_max_lock
);
569 * update_max_tr_single - only copy one trace over, and reset the rest
571 * @tsk - task with the latency
572 * @cpu - the cpu of the buffer to copy.
574 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
577 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
581 if (trace_stop_count
)
584 WARN_ON_ONCE(!irqs_disabled());
585 __raw_spin_lock(&ftrace_max_lock
);
587 ftrace_disable_cpu();
589 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
593 * We failed to swap the buffer due to a commit taking
594 * place on this CPU. We fail to record, but we reset
595 * the max trace buffer (no one writes directly to it)
596 * and flag that it failed.
598 trace_array_printk(&max_tr
, _THIS_IP_
,
599 "Failed to swap buffers due to commit in progress\n");
604 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
&& ret
!= -EBUSY
);
606 __update_max_tr(tr
, tsk
, cpu
);
607 __raw_spin_unlock(&ftrace_max_lock
);
609 #endif /* CONFIG_TRACER_MAX_TRACE */
612 * register_tracer - register a tracer with the ftrace system.
613 * @type - the plugin for the tracer
615 * Register a new plugin tracer.
617 int register_tracer(struct tracer
*type
)
618 __releases(kernel_lock
)
619 __acquires(kernel_lock
)
626 pr_info("Tracer must have a name\n");
631 * When this gets called we hold the BKL which means that
632 * preemption is disabled. Various trace selftests however
633 * need to disable and enable preemption for successful tests.
634 * So we drop the BKL here and grab it after the tests again.
637 mutex_lock(&trace_types_lock
);
639 tracing_selftest_running
= true;
641 for (t
= trace_types
; t
; t
= t
->next
) {
642 if (strcmp(type
->name
, t
->name
) == 0) {
644 pr_info("Trace %s already registered\n",
652 type
->set_flag
= &dummy_set_flag
;
654 type
->flags
= &dummy_tracer_flags
;
656 if (!type
->flags
->opts
)
657 type
->flags
->opts
= dummy_tracer_opt
;
658 if (!type
->wait_pipe
)
659 type
->wait_pipe
= default_wait_pipe
;
662 #ifdef CONFIG_FTRACE_STARTUP_TEST
663 if (type
->selftest
&& !tracing_selftest_disabled
) {
664 struct tracer
*saved_tracer
= current_trace
;
665 struct trace_array
*tr
= &global_trace
;
668 * Run a selftest on this tracer.
669 * Here we reset the trace buffer, and set the current
670 * tracer to be this tracer. The tracer can then run some
671 * internal tracing to verify that everything is in order.
672 * If we fail, we do not register this tracer.
674 tracing_reset_online_cpus(tr
);
676 current_trace
= type
;
677 /* the test is responsible for initializing and enabling */
678 pr_info("Testing tracer %s: ", type
->name
);
679 ret
= type
->selftest(type
, tr
);
680 /* the test is responsible for resetting too */
681 current_trace
= saved_tracer
;
683 printk(KERN_CONT
"FAILED!\n");
686 /* Only reset on passing, to avoid touching corrupted buffers */
687 tracing_reset_online_cpus(tr
);
689 printk(KERN_CONT
"PASSED\n");
693 type
->next
= trace_types
;
695 len
= strlen(type
->name
);
696 if (len
> max_tracer_type_len
)
697 max_tracer_type_len
= len
;
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
, BOOTUP_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
)
730 mutex_lock(&trace_types_lock
);
731 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
735 pr_info("Trace %s not registered\n", type
->name
);
741 if (type
== current_trace
&& tracer_enabled
) {
744 if (current_trace
->stop
)
745 current_trace
->stop(&global_trace
);
746 current_trace
= &nop_trace
;
749 if (strlen(type
->name
) != max_tracer_type_len
)
752 max_tracer_type_len
= 0;
753 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
754 len
= strlen((*t
)->name
);
755 if (len
> max_tracer_type_len
)
756 max_tracer_type_len
= len
;
759 mutex_unlock(&trace_types_lock
);
762 static void __tracing_reset(struct trace_array
*tr
, int cpu
)
764 ftrace_disable_cpu();
765 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
769 void tracing_reset(struct trace_array
*tr
, int cpu
)
771 struct ring_buffer
*buffer
= tr
->buffer
;
773 ring_buffer_record_disable(buffer
);
775 /* Make sure all commits have finished */
777 __tracing_reset(tr
, cpu
);
779 ring_buffer_record_enable(buffer
);
782 void tracing_reset_online_cpus(struct trace_array
*tr
)
784 struct ring_buffer
*buffer
= tr
->buffer
;
787 ring_buffer_record_disable(buffer
);
789 /* Make sure all commits have finished */
792 tr
->time_start
= ftrace_now(tr
->cpu
);
794 for_each_online_cpu(cpu
)
795 __tracing_reset(tr
, cpu
);
797 ring_buffer_record_enable(buffer
);
800 void tracing_reset_current(int cpu
)
802 tracing_reset(&global_trace
, cpu
);
805 void tracing_reset_current_online_cpus(void)
807 tracing_reset_online_cpus(&global_trace
);
810 #define SAVED_CMDLINES 128
811 #define NO_CMDLINE_MAP UINT_MAX
812 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
813 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
814 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
815 static int cmdline_idx
;
816 static raw_spinlock_t trace_cmdline_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
818 /* temporary disable recording */
819 static atomic_t trace_record_cmdline_disabled __read_mostly
;
821 static void trace_init_cmdlines(void)
823 memset(&map_pid_to_cmdline
, NO_CMDLINE_MAP
, sizeof(map_pid_to_cmdline
));
824 memset(&map_cmdline_to_pid
, NO_CMDLINE_MAP
, sizeof(map_cmdline_to_pid
));
828 int is_tracing_stopped(void)
830 return trace_stop_count
;
834 * ftrace_off_permanent - disable all ftrace code permanently
836 * This should only be called when a serious anomally has
837 * been detected. This will turn off the function tracing,
838 * ring buffers, and other tracing utilites. It takes no
839 * locks and can be called from any context.
841 void ftrace_off_permanent(void)
843 tracing_disabled
= 1;
845 tracing_off_permanent();
849 * tracing_start - quick start of the tracer
851 * If tracing is enabled but was stopped by tracing_stop,
852 * this will start the tracer back up.
854 void tracing_start(void)
856 struct ring_buffer
*buffer
;
859 if (tracing_disabled
)
862 spin_lock_irqsave(&tracing_start_lock
, flags
);
863 if (--trace_stop_count
) {
864 if (trace_stop_count
< 0) {
865 /* Someone screwed up their debugging */
867 trace_stop_count
= 0;
873 buffer
= global_trace
.buffer
;
875 ring_buffer_record_enable(buffer
);
877 buffer
= max_tr
.buffer
;
879 ring_buffer_record_enable(buffer
);
883 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
887 * tracing_stop - quick stop of the tracer
889 * Light weight way to stop tracing. Use in conjunction with
892 void tracing_stop(void)
894 struct ring_buffer
*buffer
;
898 spin_lock_irqsave(&tracing_start_lock
, flags
);
899 if (trace_stop_count
++)
902 buffer
= global_trace
.buffer
;
904 ring_buffer_record_disable(buffer
);
906 buffer
= max_tr
.buffer
;
908 ring_buffer_record_disable(buffer
);
911 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
914 void trace_stop_cmdline_recording(void);
916 static void trace_save_cmdline(struct task_struct
*tsk
)
920 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
924 * It's not the end of the world if we don't get
925 * the lock, but we also don't want to spin
926 * nor do we want to disable interrupts,
927 * so if we miss here, then better luck next time.
929 if (!__raw_spin_trylock(&trace_cmdline_lock
))
932 idx
= map_pid_to_cmdline
[tsk
->pid
];
933 if (idx
== NO_CMDLINE_MAP
) {
934 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
937 * Check whether the cmdline buffer at idx has a pid
938 * mapped. We are going to overwrite that entry so we
939 * need to clear the map_pid_to_cmdline. Otherwise we
940 * would read the new comm for the old pid.
942 pid
= map_cmdline_to_pid
[idx
];
943 if (pid
!= NO_CMDLINE_MAP
)
944 map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
946 map_cmdline_to_pid
[idx
] = tsk
->pid
;
947 map_pid_to_cmdline
[tsk
->pid
] = idx
;
952 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
954 __raw_spin_unlock(&trace_cmdline_lock
);
957 void trace_find_cmdline(int pid
, char comm
[])
962 strcpy(comm
, "<idle>");
966 if (pid
> PID_MAX_DEFAULT
) {
967 strcpy(comm
, "<...>");
972 __raw_spin_lock(&trace_cmdline_lock
);
973 map
= map_pid_to_cmdline
[pid
];
974 if (map
!= NO_CMDLINE_MAP
)
975 strcpy(comm
, saved_cmdlines
[map
]);
977 strcpy(comm
, "<...>");
979 __raw_spin_unlock(&trace_cmdline_lock
);
983 void tracing_record_cmdline(struct task_struct
*tsk
)
985 if (atomic_read(&trace_record_cmdline_disabled
) || !tracer_enabled
||
989 trace_save_cmdline(tsk
);
993 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
996 struct task_struct
*tsk
= current
;
998 entry
->preempt_count
= pc
& 0xff;
999 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
1000 entry
->lock_depth
= (tsk
) ? tsk
->lock_depth
: 0;
1002 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1003 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
1005 TRACE_FLAG_IRQS_NOSUPPORT
|
1007 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
1008 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
1009 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
1011 EXPORT_SYMBOL_GPL(tracing_generic_entry_update
);
1013 struct ring_buffer_event
*
1014 trace_buffer_lock_reserve(struct ring_buffer
*buffer
,
1017 unsigned long flags
, int pc
)
1019 struct ring_buffer_event
*event
;
1021 event
= ring_buffer_lock_reserve(buffer
, len
);
1022 if (event
!= NULL
) {
1023 struct trace_entry
*ent
= ring_buffer_event_data(event
);
1025 tracing_generic_entry_update(ent
, flags
, pc
);
1033 __trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1034 struct ring_buffer_event
*event
,
1035 unsigned long flags
, int pc
,
1038 ring_buffer_unlock_commit(buffer
, event
);
1040 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1041 ftrace_trace_userstack(buffer
, flags
, pc
);
1047 void trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1048 struct ring_buffer_event
*event
,
1049 unsigned long flags
, int pc
)
1051 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
1054 struct ring_buffer_event
*
1055 trace_current_buffer_lock_reserve(struct ring_buffer
**current_rb
,
1056 int type
, unsigned long len
,
1057 unsigned long flags
, int pc
)
1059 *current_rb
= global_trace
.buffer
;
1060 return trace_buffer_lock_reserve(*current_rb
,
1061 type
, len
, flags
, pc
);
1063 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve
);
1065 void trace_current_buffer_unlock_commit(struct ring_buffer
*buffer
,
1066 struct ring_buffer_event
*event
,
1067 unsigned long flags
, int pc
)
1069 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
1071 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit
);
1073 void trace_nowake_buffer_unlock_commit(struct ring_buffer
*buffer
,
1074 struct ring_buffer_event
*event
,
1075 unsigned long flags
, int pc
)
1077 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 0);
1079 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit
);
1081 void trace_current_buffer_discard_commit(struct ring_buffer
*buffer
,
1082 struct ring_buffer_event
*event
)
1084 ring_buffer_discard_commit(buffer
, event
);
1086 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
1089 trace_function(struct trace_array
*tr
,
1090 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1093 struct ftrace_event_call
*call
= &event_function
;
1094 struct ring_buffer
*buffer
= tr
->buffer
;
1095 struct ring_buffer_event
*event
;
1096 struct ftrace_entry
*entry
;
1098 /* If we are reading the ring buffer, don't trace */
1099 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
1102 event
= trace_buffer_lock_reserve(buffer
, TRACE_FN
, sizeof(*entry
),
1106 entry
= ring_buffer_event_data(event
);
1108 entry
->parent_ip
= parent_ip
;
1110 if (!filter_check_discard(call
, entry
, buffer
, event
))
1111 ring_buffer_unlock_commit(buffer
, event
);
1115 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
1116 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1119 if (likely(!atomic_read(&data
->disabled
)))
1120 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
1123 #ifdef CONFIG_STACKTRACE
1124 static void __ftrace_trace_stack(struct ring_buffer
*buffer
,
1125 unsigned long flags
,
1128 struct ftrace_event_call
*call
= &event_kernel_stack
;
1129 struct ring_buffer_event
*event
;
1130 struct stack_entry
*entry
;
1131 struct stack_trace trace
;
1133 event
= trace_buffer_lock_reserve(buffer
, TRACE_STACK
,
1134 sizeof(*entry
), flags
, pc
);
1137 entry
= ring_buffer_event_data(event
);
1138 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1140 trace
.nr_entries
= 0;
1141 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1143 trace
.entries
= entry
->caller
;
1145 save_stack_trace(&trace
);
1146 if (!filter_check_discard(call
, entry
, buffer
, event
))
1147 ring_buffer_unlock_commit(buffer
, event
);
1150 void ftrace_trace_stack(struct ring_buffer
*buffer
, unsigned long flags
,
1153 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1156 __ftrace_trace_stack(buffer
, flags
, skip
, pc
);
1159 void __trace_stack(struct trace_array
*tr
, unsigned long flags
, int skip
,
1162 __ftrace_trace_stack(tr
->buffer
, flags
, skip
, pc
);
1166 ftrace_trace_userstack(struct ring_buffer
*buffer
, unsigned long flags
, int pc
)
1168 struct ftrace_event_call
*call
= &event_user_stack
;
1169 struct ring_buffer_event
*event
;
1170 struct userstack_entry
*entry
;
1171 struct stack_trace trace
;
1173 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1176 event
= trace_buffer_lock_reserve(buffer
, TRACE_USER_STACK
,
1177 sizeof(*entry
), flags
, pc
);
1180 entry
= ring_buffer_event_data(event
);
1182 entry
->tgid
= current
->tgid
;
1183 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1185 trace
.nr_entries
= 0;
1186 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1188 trace
.entries
= entry
->caller
;
1190 save_stack_trace_user(&trace
);
1191 if (!filter_check_discard(call
, entry
, buffer
, event
))
1192 ring_buffer_unlock_commit(buffer
, event
);
1196 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1198 ftrace_trace_userstack(tr
, flags
, preempt_count());
1202 #endif /* CONFIG_STACKTRACE */
1205 ftrace_trace_special(void *__tr
,
1206 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1209 struct ftrace_event_call
*call
= &event_special
;
1210 struct ring_buffer_event
*event
;
1211 struct trace_array
*tr
= __tr
;
1212 struct ring_buffer
*buffer
= tr
->buffer
;
1213 struct special_entry
*entry
;
1215 event
= trace_buffer_lock_reserve(buffer
, TRACE_SPECIAL
,
1216 sizeof(*entry
), 0, pc
);
1219 entry
= ring_buffer_event_data(event
);
1224 if (!filter_check_discard(call
, entry
, buffer
, event
))
1225 trace_buffer_unlock_commit(buffer
, event
, 0, pc
);
1229 __trace_special(void *__tr
, void *__data
,
1230 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1232 ftrace_trace_special(__tr
, arg1
, arg2
, arg3
, preempt_count());
1236 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1238 struct trace_array
*tr
= &global_trace
;
1239 struct trace_array_cpu
*data
;
1240 unsigned long flags
;
1244 if (tracing_disabled
)
1247 pc
= preempt_count();
1248 local_irq_save(flags
);
1249 cpu
= raw_smp_processor_id();
1250 data
= tr
->data
[cpu
];
1252 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1253 ftrace_trace_special(tr
, arg1
, arg2
, arg3
, pc
);
1255 atomic_dec(&data
->disabled
);
1256 local_irq_restore(flags
);
1260 * trace_vbprintk - write binary msg to tracing buffer
1263 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1265 static raw_spinlock_t trace_buf_lock
=
1266 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
1267 static u32 trace_buf
[TRACE_BUF_SIZE
];
1269 struct ftrace_event_call
*call
= &event_bprint
;
1270 struct ring_buffer_event
*event
;
1271 struct ring_buffer
*buffer
;
1272 struct trace_array
*tr
= &global_trace
;
1273 struct trace_array_cpu
*data
;
1274 struct bprint_entry
*entry
;
1275 unsigned long flags
;
1278 int cpu
, len
= 0, size
, pc
;
1280 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1283 /* Don't pollute graph traces with trace_vprintk internals */
1284 pause_graph_tracing();
1286 pc
= preempt_count();
1287 resched
= ftrace_preempt_disable();
1288 cpu
= raw_smp_processor_id();
1289 data
= tr
->data
[cpu
];
1291 disable
= atomic_inc_return(&data
->disabled
);
1292 if (unlikely(disable
!= 1))
1295 /* Lockdep uses trace_printk for lock tracing */
1296 local_irq_save(flags
);
1297 __raw_spin_lock(&trace_buf_lock
);
1298 len
= vbin_printf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1300 if (len
> TRACE_BUF_SIZE
|| len
< 0)
1303 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1304 buffer
= tr
->buffer
;
1305 event
= trace_buffer_lock_reserve(buffer
, TRACE_BPRINT
, size
,
1309 entry
= ring_buffer_event_data(event
);
1313 memcpy(entry
->buf
, trace_buf
, sizeof(u32
) * len
);
1314 if (!filter_check_discard(call
, entry
, buffer
, event
))
1315 ring_buffer_unlock_commit(buffer
, event
);
1318 __raw_spin_unlock(&trace_buf_lock
);
1319 local_irq_restore(flags
);
1322 atomic_dec_return(&data
->disabled
);
1323 ftrace_preempt_enable(resched
);
1324 unpause_graph_tracing();
1328 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1330 int trace_array_printk(struct trace_array
*tr
,
1331 unsigned long ip
, const char *fmt
, ...)
1336 if (!(trace_flags
& TRACE_ITER_PRINTK
))
1340 ret
= trace_array_vprintk(tr
, ip
, fmt
, ap
);
1345 int trace_array_vprintk(struct trace_array
*tr
,
1346 unsigned long ip
, const char *fmt
, va_list args
)
1348 static raw_spinlock_t trace_buf_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
1349 static char trace_buf
[TRACE_BUF_SIZE
];
1351 struct ftrace_event_call
*call
= &event_print
;
1352 struct ring_buffer_event
*event
;
1353 struct ring_buffer
*buffer
;
1354 struct trace_array_cpu
*data
;
1355 int cpu
, len
= 0, size
, pc
;
1356 struct print_entry
*entry
;
1357 unsigned long irq_flags
;
1360 if (tracing_disabled
|| tracing_selftest_running
)
1363 pc
= preempt_count();
1364 preempt_disable_notrace();
1365 cpu
= raw_smp_processor_id();
1366 data
= tr
->data
[cpu
];
1368 disable
= atomic_inc_return(&data
->disabled
);
1369 if (unlikely(disable
!= 1))
1372 pause_graph_tracing();
1373 raw_local_irq_save(irq_flags
);
1374 __raw_spin_lock(&trace_buf_lock
);
1375 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1377 len
= min(len
, TRACE_BUF_SIZE
-1);
1380 size
= sizeof(*entry
) + len
+ 1;
1381 buffer
= tr
->buffer
;
1382 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
1386 entry
= ring_buffer_event_data(event
);
1389 memcpy(&entry
->buf
, trace_buf
, len
);
1390 entry
->buf
[len
] = 0;
1391 if (!filter_check_discard(call
, entry
, buffer
, event
))
1392 ring_buffer_unlock_commit(buffer
, event
);
1395 __raw_spin_unlock(&trace_buf_lock
);
1396 raw_local_irq_restore(irq_flags
);
1397 unpause_graph_tracing();
1399 atomic_dec_return(&data
->disabled
);
1400 preempt_enable_notrace();
1405 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1407 return trace_array_printk(&global_trace
, ip
, fmt
, args
);
1409 EXPORT_SYMBOL_GPL(trace_vprintk
);
1411 enum trace_file_type
{
1412 TRACE_FILE_LAT_FMT
= 1,
1413 TRACE_FILE_ANNOTATE
= 2,
1416 static void trace_iterator_increment(struct trace_iterator
*iter
)
1418 /* Don't allow ftrace to trace into the ring buffers */
1419 ftrace_disable_cpu();
1422 if (iter
->buffer_iter
[iter
->cpu
])
1423 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1425 ftrace_enable_cpu();
1428 static struct trace_entry
*
1429 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1431 struct ring_buffer_event
*event
;
1432 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1434 /* Don't allow ftrace to trace into the ring buffers */
1435 ftrace_disable_cpu();
1438 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1440 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1442 ftrace_enable_cpu();
1444 return event
? ring_buffer_event_data(event
) : NULL
;
1447 static struct trace_entry
*
1448 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1450 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1451 struct trace_entry
*ent
, *next
= NULL
;
1452 int cpu_file
= iter
->cpu_file
;
1453 u64 next_ts
= 0, ts
;
1458 * If we are in a per_cpu trace file, don't bother by iterating over
1459 * all cpu and peek directly.
1461 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1462 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1464 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
);
1466 *ent_cpu
= cpu_file
;
1471 for_each_tracing_cpu(cpu
) {
1473 if (ring_buffer_empty_cpu(buffer
, cpu
))
1476 ent
= peek_next_entry(iter
, cpu
, &ts
);
1479 * Pick the entry with the smallest timestamp:
1481 if (ent
&& (!next
|| ts
< next_ts
)) {
1489 *ent_cpu
= next_cpu
;
1497 /* Find the next real entry, without updating the iterator itself */
1498 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1499 int *ent_cpu
, u64
*ent_ts
)
1501 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1504 /* Find the next real entry, and increment the iterator to the next entry */
1505 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1507 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1510 trace_iterator_increment(iter
);
1512 return iter
->ent
? iter
: NULL
;
1515 static void trace_consume(struct trace_iterator
*iter
)
1517 /* Don't allow ftrace to trace into the ring buffers */
1518 ftrace_disable_cpu();
1519 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1520 ftrace_enable_cpu();
1523 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1525 struct trace_iterator
*iter
= m
->private;
1531 /* can't go backwards */
1536 ent
= find_next_entry_inc(iter
);
1540 while (ent
&& iter
->idx
< i
)
1541 ent
= find_next_entry_inc(iter
);
1548 static void tracing_iter_reset(struct trace_iterator
*iter
, int cpu
)
1550 struct trace_array
*tr
= iter
->tr
;
1551 struct ring_buffer_event
*event
;
1552 struct ring_buffer_iter
*buf_iter
;
1553 unsigned long entries
= 0;
1556 tr
->data
[cpu
]->skipped_entries
= 0;
1558 if (!iter
->buffer_iter
[cpu
])
1561 buf_iter
= iter
->buffer_iter
[cpu
];
1562 ring_buffer_iter_reset(buf_iter
);
1565 * We could have the case with the max latency tracers
1566 * that a reset never took place on a cpu. This is evident
1567 * by the timestamp being before the start of the buffer.
1569 while ((event
= ring_buffer_iter_peek(buf_iter
, &ts
))) {
1570 if (ts
>= iter
->tr
->time_start
)
1573 ring_buffer_read(buf_iter
, NULL
);
1576 tr
->data
[cpu
]->skipped_entries
= entries
;
1580 * No necessary locking here. The worst thing which can
1581 * happen is loosing events consumed at the same time
1582 * by a trace_pipe reader.
1583 * Other than that, we don't risk to crash the ring buffer
1584 * because it serializes the readers.
1586 * The current tracer is copied to avoid a global locking
1589 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1591 struct trace_iterator
*iter
= m
->private;
1592 static struct tracer
*old_tracer
;
1593 int cpu_file
= iter
->cpu_file
;
1598 /* copy the tracer to avoid using a global lock all around */
1599 mutex_lock(&trace_types_lock
);
1600 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1601 old_tracer
= current_trace
;
1602 *iter
->trace
= *current_trace
;
1604 mutex_unlock(&trace_types_lock
);
1606 atomic_inc(&trace_record_cmdline_disabled
);
1608 if (*pos
!= iter
->pos
) {
1613 ftrace_disable_cpu();
1615 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1616 for_each_tracing_cpu(cpu
)
1617 tracing_iter_reset(iter
, cpu
);
1619 tracing_iter_reset(iter
, cpu_file
);
1621 ftrace_enable_cpu();
1623 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1628 p
= s_next(m
, p
, &l
);
1631 trace_event_read_lock();
1635 static void s_stop(struct seq_file
*m
, void *p
)
1637 atomic_dec(&trace_record_cmdline_disabled
);
1638 trace_event_read_unlock();
1641 static void print_lat_help_header(struct seq_file
*m
)
1643 seq_puts(m
, "# _------=> CPU# \n");
1644 seq_puts(m
, "# / _-----=> irqs-off \n");
1645 seq_puts(m
, "# | / _----=> need-resched \n");
1646 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1647 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1648 seq_puts(m
, "# |||| /_--=> lock-depth \n");
1649 seq_puts(m
, "# |||||/ delay \n");
1650 seq_puts(m
, "# cmd pid |||||| time | caller \n");
1651 seq_puts(m
, "# \\ / |||||| \\ | / \n");
1654 static void print_func_help_header(struct seq_file
*m
)
1656 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1657 seq_puts(m
, "# | | | | |\n");
1662 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1664 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1665 struct trace_array
*tr
= iter
->tr
;
1666 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1667 struct tracer
*type
= current_trace
;
1668 unsigned long entries
= 0;
1669 unsigned long total
= 0;
1670 unsigned long count
;
1671 const char *name
= "preemption";
1678 for_each_tracing_cpu(cpu
) {
1679 count
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
1681 * If this buffer has skipped entries, then we hold all
1682 * entries for the trace and we need to ignore the
1683 * ones before the time stamp.
1685 if (tr
->data
[cpu
]->skipped_entries
) {
1686 count
-= tr
->data
[cpu
]->skipped_entries
;
1687 /* total is the same as the entries */
1691 ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
1695 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
1697 seq_puts(m
, "# -----------------------------------"
1698 "---------------------------------\n");
1699 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1700 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1701 nsecs_to_usecs(data
->saved_latency
),
1705 #if defined(CONFIG_PREEMPT_NONE)
1707 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1709 #elif defined(CONFIG_PREEMPT)
1714 /* These are reserved for later use */
1717 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1721 seq_puts(m
, "# -----------------\n");
1722 seq_printf(m
, "# | task: %.16s-%d "
1723 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1724 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1725 data
->policy
, data
->rt_priority
);
1726 seq_puts(m
, "# -----------------\n");
1728 if (data
->critical_start
) {
1729 seq_puts(m
, "# => started at: ");
1730 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1731 trace_print_seq(m
, &iter
->seq
);
1732 seq_puts(m
, "\n# => ended at: ");
1733 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1734 trace_print_seq(m
, &iter
->seq
);
1735 seq_puts(m
, "\n#\n");
1741 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1743 struct trace_seq
*s
= &iter
->seq
;
1745 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1748 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1751 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1754 if (iter
->tr
->data
[iter
->cpu
]->skipped_entries
)
1757 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1759 /* Don't print started cpu buffer for the first entry of the trace */
1761 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
1765 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1767 struct trace_seq
*s
= &iter
->seq
;
1768 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1769 struct trace_entry
*entry
;
1770 struct trace_event
*event
;
1774 test_cpu_buff_start(iter
);
1776 event
= ftrace_find_event(entry
->type
);
1778 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1779 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1780 if (!trace_print_lat_context(iter
))
1783 if (!trace_print_context(iter
))
1789 return event
->trace(iter
, sym_flags
);
1791 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1794 return TRACE_TYPE_HANDLED
;
1796 return TRACE_TYPE_PARTIAL_LINE
;
1799 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1801 struct trace_seq
*s
= &iter
->seq
;
1802 struct trace_entry
*entry
;
1803 struct trace_event
*event
;
1807 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1808 if (!trace_seq_printf(s
, "%d %d %llu ",
1809 entry
->pid
, iter
->cpu
, iter
->ts
))
1813 event
= ftrace_find_event(entry
->type
);
1815 return event
->raw(iter
, 0);
1817 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1820 return TRACE_TYPE_HANDLED
;
1822 return TRACE_TYPE_PARTIAL_LINE
;
1825 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1827 struct trace_seq
*s
= &iter
->seq
;
1828 unsigned char newline
= '\n';
1829 struct trace_entry
*entry
;
1830 struct trace_event
*event
;
1834 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1835 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1836 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1837 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1840 event
= ftrace_find_event(entry
->type
);
1842 enum print_line_t ret
= event
->hex(iter
, 0);
1843 if (ret
!= TRACE_TYPE_HANDLED
)
1847 SEQ_PUT_FIELD_RET(s
, newline
);
1849 return TRACE_TYPE_HANDLED
;
1852 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1854 struct trace_seq
*s
= &iter
->seq
;
1855 struct trace_entry
*entry
;
1856 struct trace_event
*event
;
1860 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1861 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1862 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1863 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1866 event
= ftrace_find_event(entry
->type
);
1867 return event
? event
->binary(iter
, 0) : TRACE_TYPE_HANDLED
;
1870 static int trace_empty(struct trace_iterator
*iter
)
1874 /* If we are looking at one CPU buffer, only check that one */
1875 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
1876 cpu
= iter
->cpu_file
;
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
))
1887 for_each_tracing_cpu(cpu
) {
1888 if (iter
->buffer_iter
[cpu
]) {
1889 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1892 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1900 /* Called with trace_event_read_lock() held. */
1901 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1903 enum print_line_t ret
;
1905 if (iter
->trace
&& iter
->trace
->print_line
) {
1906 ret
= iter
->trace
->print_line(iter
);
1907 if (ret
!= TRACE_TYPE_UNHANDLED
)
1911 if (iter
->ent
->type
== TRACE_BPRINT
&&
1912 trace_flags
& TRACE_ITER_PRINTK
&&
1913 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1914 return trace_print_bprintk_msg_only(iter
);
1916 if (iter
->ent
->type
== TRACE_PRINT
&&
1917 trace_flags
& TRACE_ITER_PRINTK
&&
1918 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1919 return trace_print_printk_msg_only(iter
);
1921 if (trace_flags
& TRACE_ITER_BIN
)
1922 return print_bin_fmt(iter
);
1924 if (trace_flags
& TRACE_ITER_HEX
)
1925 return print_hex_fmt(iter
);
1927 if (trace_flags
& TRACE_ITER_RAW
)
1928 return print_raw_fmt(iter
);
1930 return print_trace_fmt(iter
);
1933 static int s_show(struct seq_file
*m
, void *v
)
1935 struct trace_iterator
*iter
= v
;
1937 if (iter
->ent
== NULL
) {
1939 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1942 if (iter
->trace
&& iter
->trace
->print_header
)
1943 iter
->trace
->print_header(m
);
1944 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1945 /* print nothing if the buffers are empty */
1946 if (trace_empty(iter
))
1948 print_trace_header(m
, iter
);
1949 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1950 print_lat_help_header(m
);
1952 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1953 print_func_help_header(m
);
1956 print_trace_line(iter
);
1957 trace_print_seq(m
, &iter
->seq
);
1963 static struct seq_operations tracer_seq_ops
= {
1970 static struct trace_iterator
*
1971 __tracing_open(struct inode
*inode
, struct file
*file
)
1973 long cpu_file
= (long) inode
->i_private
;
1974 void *fail_ret
= ERR_PTR(-ENOMEM
);
1975 struct trace_iterator
*iter
;
1979 if (tracing_disabled
)
1980 return ERR_PTR(-ENODEV
);
1982 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1984 return ERR_PTR(-ENOMEM
);
1987 * We make a copy of the current tracer to avoid concurrent
1988 * changes on it while we are reading.
1990 mutex_lock(&trace_types_lock
);
1991 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
1996 *iter
->trace
= *current_trace
;
1998 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
2001 cpumask_clear(iter
->started
);
2003 if (current_trace
&& current_trace
->print_max
)
2006 iter
->tr
= &global_trace
;
2008 mutex_init(&iter
->mutex
);
2009 iter
->cpu_file
= cpu_file
;
2011 /* Notify the tracer early; before we stop tracing. */
2012 if (iter
->trace
&& iter
->trace
->open
)
2013 iter
->trace
->open(iter
);
2015 /* Annotate start of buffers if we had overruns */
2016 if (ring_buffer_overruns(iter
->tr
->buffer
))
2017 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
2019 /* stop the trace while dumping */
2022 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
2023 for_each_tracing_cpu(cpu
) {
2025 iter
->buffer_iter
[cpu
] =
2026 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
2027 tracing_iter_reset(iter
, cpu
);
2030 cpu
= iter
->cpu_file
;
2031 iter
->buffer_iter
[cpu
] =
2032 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
2033 tracing_iter_reset(iter
, cpu
);
2036 ret
= seq_open(file
, &tracer_seq_ops
);
2038 fail_ret
= ERR_PTR(ret
);
2042 m
= file
->private_data
;
2045 mutex_unlock(&trace_types_lock
);
2050 for_each_tracing_cpu(cpu
) {
2051 if (iter
->buffer_iter
[cpu
])
2052 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2054 free_cpumask_var(iter
->started
);
2057 mutex_unlock(&trace_types_lock
);
2064 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
2066 if (tracing_disabled
)
2069 filp
->private_data
= inode
->i_private
;
2073 static int tracing_release(struct inode
*inode
, struct file
*file
)
2075 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2076 struct trace_iterator
*iter
;
2079 if (!(file
->f_mode
& FMODE_READ
))
2084 mutex_lock(&trace_types_lock
);
2085 for_each_tracing_cpu(cpu
) {
2086 if (iter
->buffer_iter
[cpu
])
2087 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2090 if (iter
->trace
&& iter
->trace
->close
)
2091 iter
->trace
->close(iter
);
2093 /* reenable tracing if it was previously enabled */
2095 mutex_unlock(&trace_types_lock
);
2097 seq_release(inode
, file
);
2098 mutex_destroy(&iter
->mutex
);
2099 free_cpumask_var(iter
->started
);
2105 static int tracing_open(struct inode
*inode
, struct file
*file
)
2107 struct trace_iterator
*iter
;
2110 /* If this file was open for write, then erase contents */
2111 if ((file
->f_mode
& FMODE_WRITE
) &&
2112 (file
->f_flags
& O_TRUNC
)) {
2113 long cpu
= (long) inode
->i_private
;
2115 if (cpu
== TRACE_PIPE_ALL_CPU
)
2116 tracing_reset_online_cpus(&global_trace
);
2118 tracing_reset(&global_trace
, cpu
);
2121 if (file
->f_mode
& FMODE_READ
) {
2122 iter
= __tracing_open(inode
, file
);
2124 ret
= PTR_ERR(iter
);
2125 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2126 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2132 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2134 struct tracer
*t
= v
;
2144 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2149 mutex_lock(&trace_types_lock
);
2150 for (t
= trace_types
; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2156 static void t_stop(struct seq_file
*m
, void *p
)
2158 mutex_unlock(&trace_types_lock
);
2161 static int t_show(struct seq_file
*m
, void *v
)
2163 struct tracer
*t
= v
;
2168 seq_printf(m
, "%s", t
->name
);
2177 static struct seq_operations show_traces_seq_ops
= {
2184 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2186 if (tracing_disabled
)
2189 return seq_open(file
, &show_traces_seq_ops
);
2193 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
2194 size_t count
, loff_t
*ppos
)
2199 static const struct file_operations tracing_fops
= {
2200 .open
= tracing_open
,
2202 .write
= tracing_write_stub
,
2203 .llseek
= seq_lseek
,
2204 .release
= tracing_release
,
2207 static const struct file_operations show_traces_fops
= {
2208 .open
= show_traces_open
,
2210 .release
= seq_release
,
2214 * Only trace on a CPU if the bitmask is set:
2216 static cpumask_var_t tracing_cpumask
;
2219 * The tracer itself will not take this lock, but still we want
2220 * to provide a consistent cpumask to user-space:
2222 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2225 * Temporary storage for the character representation of the
2226 * CPU bitmask (and one more byte for the newline):
2228 static char mask_str
[NR_CPUS
+ 1];
2231 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2232 size_t count
, loff_t
*ppos
)
2236 mutex_lock(&tracing_cpumask_update_lock
);
2238 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2239 if (count
- len
< 2) {
2243 len
+= sprintf(mask_str
+ len
, "\n");
2244 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2247 mutex_unlock(&tracing_cpumask_update_lock
);
2253 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2254 size_t count
, loff_t
*ppos
)
2257 cpumask_var_t tracing_cpumask_new
;
2259 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2262 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2266 mutex_lock(&tracing_cpumask_update_lock
);
2268 local_irq_disable();
2269 __raw_spin_lock(&ftrace_max_lock
);
2270 for_each_tracing_cpu(cpu
) {
2272 * Increase/decrease the disabled counter if we are
2273 * about to flip a bit in the cpumask:
2275 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2276 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2277 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2279 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2280 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2281 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2284 __raw_spin_unlock(&ftrace_max_lock
);
2287 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2289 mutex_unlock(&tracing_cpumask_update_lock
);
2290 free_cpumask_var(tracing_cpumask_new
);
2295 free_cpumask_var(tracing_cpumask_new
);
2300 static const struct file_operations tracing_cpumask_fops
= {
2301 .open
= tracing_open_generic
,
2302 .read
= tracing_cpumask_read
,
2303 .write
= tracing_cpumask_write
,
2307 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2308 size_t cnt
, loff_t
*ppos
)
2310 struct tracer_opt
*trace_opts
;
2318 /* calculate max size */
2319 for (i
= 0; trace_options
[i
]; i
++) {
2320 len
+= strlen(trace_options
[i
]);
2321 len
+= 3; /* "no" and newline */
2324 mutex_lock(&trace_types_lock
);
2325 tracer_flags
= current_trace
->flags
->val
;
2326 trace_opts
= current_trace
->flags
->opts
;
2329 * Increase the size with names of options specific
2330 * of the current tracer.
2332 for (i
= 0; trace_opts
[i
].name
; i
++) {
2333 len
+= strlen(trace_opts
[i
].name
);
2334 len
+= 3; /* "no" and newline */
2338 buf
= kmalloc(len
+ 1, GFP_KERNEL
);
2340 mutex_unlock(&trace_types_lock
);
2344 for (i
= 0; trace_options
[i
]; i
++) {
2345 if (trace_flags
& (1 << i
))
2346 r
+= sprintf(buf
+ r
, "%s\n", trace_options
[i
]);
2348 r
+= sprintf(buf
+ r
, "no%s\n", trace_options
[i
]);
2351 for (i
= 0; trace_opts
[i
].name
; i
++) {
2352 if (tracer_flags
& trace_opts
[i
].bit
)
2353 r
+= sprintf(buf
+ r
, "%s\n",
2354 trace_opts
[i
].name
);
2356 r
+= sprintf(buf
+ r
, "no%s\n",
2357 trace_opts
[i
].name
);
2359 mutex_unlock(&trace_types_lock
);
2361 WARN_ON(r
>= len
+ 1);
2363 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2369 /* Try to assign a tracer specific option */
2370 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2372 struct tracer_flags
*tracer_flags
= trace
->flags
;
2373 struct tracer_opt
*opts
= NULL
;
2377 for (i
= 0; tracer_flags
->opts
[i
].name
; i
++) {
2378 opts
= &tracer_flags
->opts
[i
];
2379 len
= strlen(opts
->name
);
2381 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2382 ret
= trace
->set_flag(tracer_flags
->val
,
2388 if (!tracer_flags
->opts
[i
].name
)
2391 /* Refused to handle */
2396 tracer_flags
->val
&= ~opts
->bit
;
2398 tracer_flags
->val
|= opts
->bit
;
2403 static void set_tracer_flags(unsigned int mask
, int enabled
)
2405 /* do nothing if flag is already set */
2406 if (!!(trace_flags
& mask
) == !!enabled
)
2410 trace_flags
|= mask
;
2412 trace_flags
&= ~mask
;
2416 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2417 size_t cnt
, loff_t
*ppos
)
2425 if (cnt
>= sizeof(buf
))
2428 if (copy_from_user(&buf
, ubuf
, cnt
))
2433 if (strncmp(buf
, "no", 2) == 0) {
2438 for (i
= 0; trace_options
[i
]; i
++) {
2439 int len
= strlen(trace_options
[i
]);
2441 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2442 set_tracer_flags(1 << i
, !neg
);
2447 /* If no option could be set, test the specific tracer options */
2448 if (!trace_options
[i
]) {
2449 mutex_lock(&trace_types_lock
);
2450 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2451 mutex_unlock(&trace_types_lock
);
2461 static const struct file_operations tracing_iter_fops
= {
2462 .open
= tracing_open_generic
,
2463 .read
= tracing_trace_options_read
,
2464 .write
= tracing_trace_options_write
,
2467 static const char readme_msg
[] =
2468 "tracing mini-HOWTO:\n\n"
2469 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2470 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2471 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2472 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2474 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2475 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2477 "# cat /sys/kernel/debug/tracing/trace_options\n"
2478 "noprint-parent nosym-offset nosym-addr noverbose\n"
2479 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2480 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2481 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2482 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2486 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2487 size_t cnt
, loff_t
*ppos
)
2489 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2490 readme_msg
, strlen(readme_msg
));
2493 static const struct file_operations tracing_readme_fops
= {
2494 .open
= tracing_open_generic
,
2495 .read
= tracing_readme_read
,
2499 tracing_saved_cmdlines_read(struct file
*file
, char __user
*ubuf
,
2500 size_t cnt
, loff_t
*ppos
)
2509 file_buf
= kmalloc(SAVED_CMDLINES
*(16+TASK_COMM_LEN
), GFP_KERNEL
);
2513 buf_comm
= kmalloc(TASK_COMM_LEN
, GFP_KERNEL
);
2521 for (i
= 0; i
< SAVED_CMDLINES
; i
++) {
2524 pid
= map_cmdline_to_pid
[i
];
2525 if (pid
== -1 || pid
== NO_CMDLINE_MAP
)
2528 trace_find_cmdline(pid
, buf_comm
);
2529 r
= sprintf(buf
, "%d %s\n", pid
, buf_comm
);
2534 len
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
2543 static const struct file_operations tracing_saved_cmdlines_fops
= {
2544 .open
= tracing_open_generic
,
2545 .read
= tracing_saved_cmdlines_read
,
2549 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2550 size_t cnt
, loff_t
*ppos
)
2555 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2556 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2560 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2561 size_t cnt
, loff_t
*ppos
)
2563 struct trace_array
*tr
= filp
->private_data
;
2568 if (cnt
>= sizeof(buf
))
2571 if (copy_from_user(&buf
, ubuf
, cnt
))
2576 ret
= strict_strtoul(buf
, 10, &val
);
2582 mutex_lock(&trace_types_lock
);
2583 if (tracer_enabled
^ val
) {
2586 if (current_trace
->start
)
2587 current_trace
->start(tr
);
2592 if (current_trace
->stop
)
2593 current_trace
->stop(tr
);
2596 mutex_unlock(&trace_types_lock
);
2604 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2605 size_t cnt
, loff_t
*ppos
)
2607 char buf
[max_tracer_type_len
+2];
2610 mutex_lock(&trace_types_lock
);
2612 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2614 r
= sprintf(buf
, "\n");
2615 mutex_unlock(&trace_types_lock
);
2617 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2620 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2622 tracing_reset_online_cpus(tr
);
2626 static int tracing_resize_ring_buffer(unsigned long size
)
2631 * If kernel or user changes the size of the ring buffer
2632 * we use the size that was given, and we can forget about
2633 * expanding it later.
2635 ring_buffer_expanded
= 1;
2637 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2641 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2645 r
= ring_buffer_resize(global_trace
.buffer
,
2646 global_trace
.entries
);
2649 * AARGH! We are left with different
2650 * size max buffer!!!!
2651 * The max buffer is our "snapshot" buffer.
2652 * When a tracer needs a snapshot (one of the
2653 * latency tracers), it swaps the max buffer
2654 * with the saved snap shot. We succeeded to
2655 * update the size of the main buffer, but failed to
2656 * update the size of the max buffer. But when we tried
2657 * to reset the main buffer to the original size, we
2658 * failed there too. This is very unlikely to
2659 * happen, but if it does, warn and kill all
2663 tracing_disabled
= 1;
2668 global_trace
.entries
= size
;
2674 * tracing_update_buffers - used by tracing facility to expand ring buffers
2676 * To save on memory when the tracing is never used on a system with it
2677 * configured in. The ring buffers are set to a minimum size. But once
2678 * a user starts to use the tracing facility, then they need to grow
2679 * to their default size.
2681 * This function is to be called when a tracer is about to be used.
2683 int tracing_update_buffers(void)
2687 mutex_lock(&trace_types_lock
);
2688 if (!ring_buffer_expanded
)
2689 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2690 mutex_unlock(&trace_types_lock
);
2695 struct trace_option_dentry
;
2697 static struct trace_option_dentry
*
2698 create_trace_option_files(struct tracer
*tracer
);
2701 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2703 static int tracing_set_tracer(const char *buf
)
2705 static struct trace_option_dentry
*topts
;
2706 struct trace_array
*tr
= &global_trace
;
2710 mutex_lock(&trace_types_lock
);
2712 if (!ring_buffer_expanded
) {
2713 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2719 for (t
= trace_types
; t
; t
= t
->next
) {
2720 if (strcmp(t
->name
, buf
) == 0)
2727 if (t
== current_trace
)
2730 trace_branch_disable();
2731 if (current_trace
&& current_trace
->reset
)
2732 current_trace
->reset(tr
);
2734 destroy_trace_option_files(topts
);
2738 topts
= create_trace_option_files(current_trace
);
2741 ret
= tracer_init(t
, tr
);
2746 trace_branch_enable(tr
);
2748 mutex_unlock(&trace_types_lock
);
2754 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2755 size_t cnt
, loff_t
*ppos
)
2757 char buf
[max_tracer_type_len
+1];
2764 if (cnt
> max_tracer_type_len
)
2765 cnt
= max_tracer_type_len
;
2767 if (copy_from_user(&buf
, ubuf
, cnt
))
2772 /* strip ending whitespace. */
2773 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2776 err
= tracing_set_tracer(buf
);
2786 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2787 size_t cnt
, loff_t
*ppos
)
2789 unsigned long *ptr
= filp
->private_data
;
2793 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2794 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2795 if (r
> sizeof(buf
))
2797 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2801 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2802 size_t cnt
, loff_t
*ppos
)
2804 unsigned long *ptr
= filp
->private_data
;
2809 if (cnt
>= sizeof(buf
))
2812 if (copy_from_user(&buf
, ubuf
, cnt
))
2817 ret
= strict_strtoul(buf
, 10, &val
);
2826 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2828 long cpu_file
= (long) inode
->i_private
;
2829 struct trace_iterator
*iter
;
2832 if (tracing_disabled
)
2835 mutex_lock(&trace_types_lock
);
2837 /* We only allow one reader per cpu */
2838 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
2839 if (!cpumask_empty(tracing_reader_cpumask
)) {
2843 cpumask_setall(tracing_reader_cpumask
);
2845 if (!cpumask_test_cpu(cpu_file
, tracing_reader_cpumask
))
2846 cpumask_set_cpu(cpu_file
, tracing_reader_cpumask
);
2853 /* create a buffer to store the information to pass to userspace */
2854 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2861 * We make a copy of the current tracer to avoid concurrent
2862 * changes on it while we are reading.
2864 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2870 *iter
->trace
= *current_trace
;
2872 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2877 /* trace pipe does not show start of buffer */
2878 cpumask_setall(iter
->started
);
2880 if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2881 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2883 iter
->cpu_file
= cpu_file
;
2884 iter
->tr
= &global_trace
;
2885 mutex_init(&iter
->mutex
);
2886 filp
->private_data
= iter
;
2888 if (iter
->trace
->pipe_open
)
2889 iter
->trace
->pipe_open(iter
);
2892 mutex_unlock(&trace_types_lock
);
2898 mutex_unlock(&trace_types_lock
);
2902 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2904 struct trace_iterator
*iter
= file
->private_data
;
2906 mutex_lock(&trace_types_lock
);
2908 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
)
2909 cpumask_clear(tracing_reader_cpumask
);
2911 cpumask_clear_cpu(iter
->cpu_file
, tracing_reader_cpumask
);
2913 mutex_unlock(&trace_types_lock
);
2915 free_cpumask_var(iter
->started
);
2916 mutex_destroy(&iter
->mutex
);
2924 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2926 struct trace_iterator
*iter
= filp
->private_data
;
2928 if (trace_flags
& TRACE_ITER_BLOCK
) {
2930 * Always select as readable when in blocking mode
2932 return POLLIN
| POLLRDNORM
;
2934 if (!trace_empty(iter
))
2935 return POLLIN
| POLLRDNORM
;
2936 poll_wait(filp
, &trace_wait
, poll_table
);
2937 if (!trace_empty(iter
))
2938 return POLLIN
| POLLRDNORM
;
2945 void default_wait_pipe(struct trace_iterator
*iter
)
2949 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
2951 if (trace_empty(iter
))
2954 finish_wait(&trace_wait
, &wait
);
2958 * This is a make-shift waitqueue.
2959 * A tracer might use this callback on some rare cases:
2961 * 1) the current tracer might hold the runqueue lock when it wakes up
2962 * a reader, hence a deadlock (sched, function, and function graph tracers)
2963 * 2) the function tracers, trace all functions, we don't want
2964 * the overhead of calling wake_up and friends
2965 * (and tracing them too)
2967 * Anyway, this is really very primitive wakeup.
2969 void poll_wait_pipe(struct trace_iterator
*iter
)
2971 set_current_state(TASK_INTERRUPTIBLE
);
2972 /* sleep for 100 msecs, and try again. */
2973 schedule_timeout(HZ
/ 10);
2976 /* Must be called with trace_types_lock mutex held. */
2977 static int tracing_wait_pipe(struct file
*filp
)
2979 struct trace_iterator
*iter
= filp
->private_data
;
2981 while (trace_empty(iter
)) {
2983 if ((filp
->f_flags
& O_NONBLOCK
)) {
2987 mutex_unlock(&iter
->mutex
);
2989 iter
->trace
->wait_pipe(iter
);
2991 mutex_lock(&iter
->mutex
);
2993 if (signal_pending(current
))
2997 * We block until we read something and tracing is disabled.
2998 * We still block if tracing is disabled, but we have never
2999 * read anything. This allows a user to cat this file, and
3000 * then enable tracing. But after we have read something,
3001 * we give an EOF when tracing is again disabled.
3003 * iter->pos will be 0 if we haven't read anything.
3005 if (!tracer_enabled
&& iter
->pos
)
3016 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
3017 size_t cnt
, loff_t
*ppos
)
3019 struct trace_iterator
*iter
= filp
->private_data
;
3020 static struct tracer
*old_tracer
;
3023 /* return any leftover data */
3024 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3028 trace_seq_init(&iter
->seq
);
3030 /* copy the tracer to avoid using a global lock all around */
3031 mutex_lock(&trace_types_lock
);
3032 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3033 old_tracer
= current_trace
;
3034 *iter
->trace
= *current_trace
;
3036 mutex_unlock(&trace_types_lock
);
3039 * Avoid more than one consumer on a single file descriptor
3040 * This is just a matter of traces coherency, the ring buffer itself
3043 mutex_lock(&iter
->mutex
);
3044 if (iter
->trace
->read
) {
3045 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
3051 sret
= tracing_wait_pipe(filp
);
3055 /* stop when tracing is finished */
3056 if (trace_empty(iter
)) {
3061 if (cnt
>= PAGE_SIZE
)
3062 cnt
= PAGE_SIZE
- 1;
3064 /* reset all but tr, trace, and overruns */
3065 memset(&iter
->seq
, 0,
3066 sizeof(struct trace_iterator
) -
3067 offsetof(struct trace_iterator
, seq
));
3070 trace_event_read_lock();
3071 while (find_next_entry_inc(iter
) != NULL
) {
3072 enum print_line_t ret
;
3073 int len
= iter
->seq
.len
;
3075 ret
= print_trace_line(iter
);
3076 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3077 /* don't print partial lines */
3078 iter
->seq
.len
= len
;
3081 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3082 trace_consume(iter
);
3084 if (iter
->seq
.len
>= cnt
)
3087 trace_event_read_unlock();
3089 /* Now copy what we have to the user */
3090 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3091 if (iter
->seq
.readpos
>= iter
->seq
.len
)
3092 trace_seq_init(&iter
->seq
);
3095 * If there was nothing to send to user, inspite of consuming trace
3096 * entries, go back to wait for more entries.
3102 mutex_unlock(&iter
->mutex
);
3107 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
3108 struct pipe_buffer
*buf
)
3110 __free_page(buf
->page
);
3113 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
3116 __free_page(spd
->pages
[idx
]);
3119 static struct pipe_buf_operations tracing_pipe_buf_ops
= {
3121 .map
= generic_pipe_buf_map
,
3122 .unmap
= generic_pipe_buf_unmap
,
3123 .confirm
= generic_pipe_buf_confirm
,
3124 .release
= tracing_pipe_buf_release
,
3125 .steal
= generic_pipe_buf_steal
,
3126 .get
= generic_pipe_buf_get
,
3130 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
3135 /* Seq buffer is page-sized, exactly what we need. */
3137 count
= iter
->seq
.len
;
3138 ret
= print_trace_line(iter
);
3139 count
= iter
->seq
.len
- count
;
3142 iter
->seq
.len
-= count
;
3145 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3146 iter
->seq
.len
-= count
;
3150 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3151 trace_consume(iter
);
3153 if (!find_next_entry_inc(iter
)) {
3163 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3165 struct pipe_inode_info
*pipe
,
3169 struct page
*pages
[PIPE_BUFFERS
];
3170 struct partial_page partial
[PIPE_BUFFERS
];
3171 struct trace_iterator
*iter
= filp
->private_data
;
3172 struct splice_pipe_desc spd
= {
3175 .nr_pages
= 0, /* This gets updated below. */
3177 .ops
= &tracing_pipe_buf_ops
,
3178 .spd_release
= tracing_spd_release_pipe
,
3180 static struct tracer
*old_tracer
;
3185 /* copy the tracer to avoid using a global lock all around */
3186 mutex_lock(&trace_types_lock
);
3187 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3188 old_tracer
= current_trace
;
3189 *iter
->trace
= *current_trace
;
3191 mutex_unlock(&trace_types_lock
);
3193 mutex_lock(&iter
->mutex
);
3195 if (iter
->trace
->splice_read
) {
3196 ret
= iter
->trace
->splice_read(iter
, filp
,
3197 ppos
, pipe
, len
, flags
);
3202 ret
= tracing_wait_pipe(filp
);
3206 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
3211 trace_event_read_lock();
3213 /* Fill as many pages as possible. */
3214 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
3215 pages
[i
] = alloc_page(GFP_KERNEL
);
3219 rem
= tracing_fill_pipe_page(rem
, iter
);
3221 /* Copy the data into the page, so we can start over. */
3222 ret
= trace_seq_to_buffer(&iter
->seq
,
3223 page_address(pages
[i
]),
3226 __free_page(pages
[i
]);
3229 partial
[i
].offset
= 0;
3230 partial
[i
].len
= iter
->seq
.len
;
3232 trace_seq_init(&iter
->seq
);
3235 trace_event_read_unlock();
3236 mutex_unlock(&iter
->mutex
);
3240 return splice_to_pipe(pipe
, &spd
);
3243 mutex_unlock(&iter
->mutex
);
3249 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3250 size_t cnt
, loff_t
*ppos
)
3252 struct trace_array
*tr
= filp
->private_data
;
3256 mutex_lock(&trace_types_lock
);
3257 if (!ring_buffer_expanded
)
3258 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3260 trace_buf_size
>> 10);
3262 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3263 mutex_unlock(&trace_types_lock
);
3265 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3269 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3270 size_t cnt
, loff_t
*ppos
)
3276 if (cnt
>= sizeof(buf
))
3279 if (copy_from_user(&buf
, ubuf
, cnt
))
3284 ret
= strict_strtoul(buf
, 10, &val
);
3288 /* must have at least 1 entry */
3292 mutex_lock(&trace_types_lock
);
3296 /* disable all cpu buffers */
3297 for_each_tracing_cpu(cpu
) {
3298 if (global_trace
.data
[cpu
])
3299 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3300 if (max_tr
.data
[cpu
])
3301 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3304 /* value is in KB */
3307 if (val
!= global_trace
.entries
) {
3308 ret
= tracing_resize_ring_buffer(val
);
3317 /* If check pages failed, return ENOMEM */
3318 if (tracing_disabled
)
3321 for_each_tracing_cpu(cpu
) {
3322 if (global_trace
.data
[cpu
])
3323 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3324 if (max_tr
.data
[cpu
])
3325 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3329 max_tr
.entries
= global_trace
.entries
;
3330 mutex_unlock(&trace_types_lock
);
3335 static int mark_printk(const char *fmt
, ...)
3339 va_start(args
, fmt
);
3340 ret
= trace_vprintk(0, fmt
, args
);
3346 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3347 size_t cnt
, loff_t
*fpos
)
3352 if (tracing_disabled
)
3355 if (cnt
> TRACE_BUF_SIZE
)
3356 cnt
= TRACE_BUF_SIZE
;
3358 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3362 if (copy_from_user(buf
, ubuf
, cnt
)) {
3367 /* Cut from the first nil or newline. */
3369 end
= strchr(buf
, '\n');
3373 cnt
= mark_printk("%s\n", buf
);
3380 static ssize_t
tracing_clock_read(struct file
*filp
, char __user
*ubuf
,
3381 size_t cnt
, loff_t
*ppos
)
3387 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++)
3388 bufiter
+= snprintf(buf
+ bufiter
, sizeof(buf
) - bufiter
,
3389 "%s%s%s%s", i
? " " : "",
3390 i
== trace_clock_id
? "[" : "", trace_clocks
[i
].name
,
3391 i
== trace_clock_id
? "]" : "");
3392 bufiter
+= snprintf(buf
+ bufiter
, sizeof(buf
) - bufiter
, "\n");
3394 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, bufiter
);
3397 static ssize_t
tracing_clock_write(struct file
*filp
, const char __user
*ubuf
,
3398 size_t cnt
, loff_t
*fpos
)
3401 const char *clockstr
;
3404 if (cnt
>= sizeof(buf
))
3407 if (copy_from_user(&buf
, ubuf
, cnt
))
3412 clockstr
= strstrip(buf
);
3414 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++) {
3415 if (strcmp(trace_clocks
[i
].name
, clockstr
) == 0)
3418 if (i
== ARRAY_SIZE(trace_clocks
))
3423 mutex_lock(&trace_types_lock
);
3425 ring_buffer_set_clock(global_trace
.buffer
, trace_clocks
[i
].func
);
3427 ring_buffer_set_clock(max_tr
.buffer
, trace_clocks
[i
].func
);
3429 mutex_unlock(&trace_types_lock
);
3436 static const struct file_operations tracing_max_lat_fops
= {
3437 .open
= tracing_open_generic
,
3438 .read
= tracing_max_lat_read
,
3439 .write
= tracing_max_lat_write
,
3442 static const struct file_operations tracing_ctrl_fops
= {
3443 .open
= tracing_open_generic
,
3444 .read
= tracing_ctrl_read
,
3445 .write
= tracing_ctrl_write
,
3448 static const struct file_operations set_tracer_fops
= {
3449 .open
= tracing_open_generic
,
3450 .read
= tracing_set_trace_read
,
3451 .write
= tracing_set_trace_write
,
3454 static const struct file_operations tracing_pipe_fops
= {
3455 .open
= tracing_open_pipe
,
3456 .poll
= tracing_poll_pipe
,
3457 .read
= tracing_read_pipe
,
3458 .splice_read
= tracing_splice_read_pipe
,
3459 .release
= tracing_release_pipe
,
3462 static const struct file_operations tracing_entries_fops
= {
3463 .open
= tracing_open_generic
,
3464 .read
= tracing_entries_read
,
3465 .write
= tracing_entries_write
,
3468 static const struct file_operations tracing_mark_fops
= {
3469 .open
= tracing_open_generic
,
3470 .write
= tracing_mark_write
,
3473 static const struct file_operations trace_clock_fops
= {
3474 .open
= tracing_open_generic
,
3475 .read
= tracing_clock_read
,
3476 .write
= tracing_clock_write
,
3479 struct ftrace_buffer_info
{
3480 struct trace_array
*tr
;
3486 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3488 int cpu
= (int)(long)inode
->i_private
;
3489 struct ftrace_buffer_info
*info
;
3491 if (tracing_disabled
)
3494 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3498 info
->tr
= &global_trace
;
3501 /* Force reading ring buffer for first read */
3502 info
->read
= (unsigned int)-1;
3504 filp
->private_data
= info
;
3506 return nonseekable_open(inode
, filp
);
3510 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3511 size_t count
, loff_t
*ppos
)
3513 struct ftrace_buffer_info
*info
= filp
->private_data
;
3522 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3526 /* Do we have previous read data to read? */
3527 if (info
->read
< PAGE_SIZE
)
3532 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3539 pos
= ring_buffer_page_len(info
->spare
);
3541 if (pos
< PAGE_SIZE
)
3542 memset(info
->spare
+ pos
, 0, PAGE_SIZE
- pos
);
3545 size
= PAGE_SIZE
- info
->read
;
3549 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3560 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3562 struct ftrace_buffer_info
*info
= file
->private_data
;
3565 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3572 struct ring_buffer
*buffer
;
3577 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3578 struct pipe_buffer
*buf
)
3580 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3585 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3590 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3591 struct pipe_buffer
*buf
)
3596 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3597 struct pipe_buffer
*buf
)
3599 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3604 /* Pipe buffer operations for a buffer. */
3605 static struct pipe_buf_operations buffer_pipe_buf_ops
= {
3607 .map
= generic_pipe_buf_map
,
3608 .unmap
= generic_pipe_buf_unmap
,
3609 .confirm
= generic_pipe_buf_confirm
,
3610 .release
= buffer_pipe_buf_release
,
3611 .steal
= buffer_pipe_buf_steal
,
3612 .get
= buffer_pipe_buf_get
,
3616 * Callback from splice_to_pipe(), if we need to release some pages
3617 * at the end of the spd in case we error'ed out in filling the pipe.
3619 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3621 struct buffer_ref
*ref
=
3622 (struct buffer_ref
*)spd
->partial
[i
].private;
3627 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3629 spd
->partial
[i
].private = 0;
3633 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3634 struct pipe_inode_info
*pipe
, size_t len
,
3637 struct ftrace_buffer_info
*info
= file
->private_data
;
3638 struct partial_page partial
[PIPE_BUFFERS
];
3639 struct page
*pages
[PIPE_BUFFERS
];
3640 struct splice_pipe_desc spd
= {
3644 .ops
= &buffer_pipe_buf_ops
,
3645 .spd_release
= buffer_spd_release
,
3647 struct buffer_ref
*ref
;
3648 int entries
, size
, i
;
3651 if (*ppos
& (PAGE_SIZE
- 1)) {
3652 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3656 if (len
& (PAGE_SIZE
- 1)) {
3657 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3658 if (len
< PAGE_SIZE
)
3663 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3665 for (i
= 0; i
< PIPE_BUFFERS
&& len
&& entries
; i
++, len
-= PAGE_SIZE
) {
3669 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3674 ref
->buffer
= info
->tr
->buffer
;
3675 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3681 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3684 ring_buffer_free_read_page(ref
->buffer
,
3691 * zero out any left over data, this is going to
3694 size
= ring_buffer_page_len(ref
->page
);
3695 if (size
< PAGE_SIZE
)
3696 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3698 page
= virt_to_page(ref
->page
);
3700 spd
.pages
[i
] = page
;
3701 spd
.partial
[i
].len
= PAGE_SIZE
;
3702 spd
.partial
[i
].offset
= 0;
3703 spd
.partial
[i
].private = (unsigned long)ref
;
3707 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3712 /* did we read anything? */
3713 if (!spd
.nr_pages
) {
3714 if (flags
& SPLICE_F_NONBLOCK
)
3722 ret
= splice_to_pipe(pipe
, &spd
);
3727 static const struct file_operations tracing_buffers_fops
= {
3728 .open
= tracing_buffers_open
,
3729 .read
= tracing_buffers_read
,
3730 .release
= tracing_buffers_release
,
3731 .splice_read
= tracing_buffers_splice_read
,
3732 .llseek
= no_llseek
,
3736 tracing_stats_read(struct file
*filp
, char __user
*ubuf
,
3737 size_t count
, loff_t
*ppos
)
3739 unsigned long cpu
= (unsigned long)filp
->private_data
;
3740 struct trace_array
*tr
= &global_trace
;
3741 struct trace_seq
*s
;
3744 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
3750 cnt
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
3751 trace_seq_printf(s
, "entries: %ld\n", cnt
);
3753 cnt
= ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
3754 trace_seq_printf(s
, "overrun: %ld\n", cnt
);
3756 cnt
= ring_buffer_commit_overrun_cpu(tr
->buffer
, cpu
);
3757 trace_seq_printf(s
, "commit overrun: %ld\n", cnt
);
3759 count
= simple_read_from_buffer(ubuf
, count
, ppos
, s
->buffer
, s
->len
);
3766 static const struct file_operations tracing_stats_fops
= {
3767 .open
= tracing_open_generic
,
3768 .read
= tracing_stats_read
,
3771 #ifdef CONFIG_DYNAMIC_FTRACE
3773 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3779 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3780 size_t cnt
, loff_t
*ppos
)
3782 static char ftrace_dyn_info_buffer
[1024];
3783 static DEFINE_MUTEX(dyn_info_mutex
);
3784 unsigned long *p
= filp
->private_data
;
3785 char *buf
= ftrace_dyn_info_buffer
;
3786 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3789 mutex_lock(&dyn_info_mutex
);
3790 r
= sprintf(buf
, "%ld ", *p
);
3792 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3795 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3797 mutex_unlock(&dyn_info_mutex
);
3802 static const struct file_operations tracing_dyn_info_fops
= {
3803 .open
= tracing_open_generic
,
3804 .read
= tracing_read_dyn_info
,
3808 static struct dentry
*d_tracer
;
3810 struct dentry
*tracing_init_dentry(void)
3817 if (!debugfs_initialized())
3820 d_tracer
= debugfs_create_dir("tracing", NULL
);
3822 if (!d_tracer
&& !once
) {
3824 pr_warning("Could not create debugfs directory 'tracing'\n");
3831 static struct dentry
*d_percpu
;
3833 struct dentry
*tracing_dentry_percpu(void)
3836 struct dentry
*d_tracer
;
3841 d_tracer
= tracing_init_dentry();
3846 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
3848 if (!d_percpu
&& !once
) {
3850 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3857 static void tracing_init_debugfs_percpu(long cpu
)
3859 struct dentry
*d_percpu
= tracing_dentry_percpu();
3860 struct dentry
*d_cpu
;
3861 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3864 if (cpu
> 999 || cpu
< 0)
3867 sprintf(cpu_dir
, "cpu%ld", cpu
);
3868 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
3870 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
3874 /* per cpu trace_pipe */
3875 trace_create_file("trace_pipe", 0444, d_cpu
,
3876 (void *) cpu
, &tracing_pipe_fops
);
3879 trace_create_file("trace", 0644, d_cpu
,
3880 (void *) cpu
, &tracing_fops
);
3882 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
3883 (void *) cpu
, &tracing_buffers_fops
);
3885 trace_create_file("stats", 0444, d_cpu
,
3886 (void *) cpu
, &tracing_stats_fops
);
3889 #ifdef CONFIG_FTRACE_SELFTEST
3890 /* Let selftest have access to static functions in this file */
3891 #include "trace_selftest.c"
3894 struct trace_option_dentry
{
3895 struct tracer_opt
*opt
;
3896 struct tracer_flags
*flags
;
3897 struct dentry
*entry
;
3901 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3904 struct trace_option_dentry
*topt
= filp
->private_data
;
3907 if (topt
->flags
->val
& topt
->opt
->bit
)
3912 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3916 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3919 struct trace_option_dentry
*topt
= filp
->private_data
;
3924 if (cnt
>= sizeof(buf
))
3927 if (copy_from_user(&buf
, ubuf
, cnt
))
3932 ret
= strict_strtoul(buf
, 10, &val
);
3939 /* do nothing if already cleared */
3940 if (!(topt
->flags
->val
& topt
->opt
->bit
))
3943 mutex_lock(&trace_types_lock
);
3944 if (current_trace
->set_flag
)
3945 ret
= current_trace
->set_flag(topt
->flags
->val
,
3947 mutex_unlock(&trace_types_lock
);
3950 topt
->flags
->val
&= ~topt
->opt
->bit
;
3953 /* do nothing if already set */
3954 if (topt
->flags
->val
& topt
->opt
->bit
)
3957 mutex_lock(&trace_types_lock
);
3958 if (current_trace
->set_flag
)
3959 ret
= current_trace
->set_flag(topt
->flags
->val
,
3961 mutex_unlock(&trace_types_lock
);
3964 topt
->flags
->val
|= topt
->opt
->bit
;
3977 static const struct file_operations trace_options_fops
= {
3978 .open
= tracing_open_generic
,
3979 .read
= trace_options_read
,
3980 .write
= trace_options_write
,
3984 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3987 long index
= (long)filp
->private_data
;
3990 if (trace_flags
& (1 << index
))
3995 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3999 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
4002 long index
= (long)filp
->private_data
;
4007 if (cnt
>= sizeof(buf
))
4010 if (copy_from_user(&buf
, ubuf
, cnt
))
4015 ret
= strict_strtoul(buf
, 10, &val
);
4019 if (val
!= 0 && val
!= 1)
4021 set_tracer_flags(1 << index
, val
);
4028 static const struct file_operations trace_options_core_fops
= {
4029 .open
= tracing_open_generic
,
4030 .read
= trace_options_core_read
,
4031 .write
= trace_options_core_write
,
4034 struct dentry
*trace_create_file(const char *name
,
4036 struct dentry
*parent
,
4038 const struct file_operations
*fops
)
4042 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
4044 pr_warning("Could not create debugfs '%s' entry\n", name
);
4050 static struct dentry
*trace_options_init_dentry(void)
4052 struct dentry
*d_tracer
;
4053 static struct dentry
*t_options
;
4058 d_tracer
= tracing_init_dentry();
4062 t_options
= debugfs_create_dir("options", d_tracer
);
4064 pr_warning("Could not create debugfs directory 'options'\n");
4072 create_trace_option_file(struct trace_option_dentry
*topt
,
4073 struct tracer_flags
*flags
,
4074 struct tracer_opt
*opt
)
4076 struct dentry
*t_options
;
4078 t_options
= trace_options_init_dentry();
4082 topt
->flags
= flags
;
4085 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
4086 &trace_options_fops
);
4090 static struct trace_option_dentry
*
4091 create_trace_option_files(struct tracer
*tracer
)
4093 struct trace_option_dentry
*topts
;
4094 struct tracer_flags
*flags
;
4095 struct tracer_opt
*opts
;
4101 flags
= tracer
->flags
;
4103 if (!flags
|| !flags
->opts
)
4108 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4111 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
4115 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4116 create_trace_option_file(&topts
[cnt
], flags
,
4123 destroy_trace_option_files(struct trace_option_dentry
*topts
)
4130 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
4131 if (topts
[cnt
].entry
)
4132 debugfs_remove(topts
[cnt
].entry
);
4138 static struct dentry
*
4139 create_trace_option_core_file(const char *option
, long index
)
4141 struct dentry
*t_options
;
4143 t_options
= trace_options_init_dentry();
4147 return trace_create_file(option
, 0644, t_options
, (void *)index
,
4148 &trace_options_core_fops
);
4151 static __init
void create_trace_options_dir(void)
4153 struct dentry
*t_options
;
4156 t_options
= trace_options_init_dentry();
4160 for (i
= 0; trace_options
[i
]; i
++)
4161 create_trace_option_core_file(trace_options
[i
], i
);
4164 static __init
int tracer_init_debugfs(void)
4166 struct dentry
*d_tracer
;
4169 d_tracer
= tracing_init_dentry();
4171 trace_create_file("tracing_enabled", 0644, d_tracer
,
4172 &global_trace
, &tracing_ctrl_fops
);
4174 trace_create_file("trace_options", 0644, d_tracer
,
4175 NULL
, &tracing_iter_fops
);
4177 trace_create_file("tracing_cpumask", 0644, d_tracer
,
4178 NULL
, &tracing_cpumask_fops
);
4180 trace_create_file("trace", 0644, d_tracer
,
4181 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
4183 trace_create_file("available_tracers", 0444, d_tracer
,
4184 &global_trace
, &show_traces_fops
);
4186 trace_create_file("current_tracer", 0644, d_tracer
,
4187 &global_trace
, &set_tracer_fops
);
4189 #ifdef CONFIG_TRACER_MAX_TRACE
4190 trace_create_file("tracing_max_latency", 0644, d_tracer
,
4191 &tracing_max_latency
, &tracing_max_lat_fops
);
4193 trace_create_file("tracing_thresh", 0644, d_tracer
,
4194 &tracing_thresh
, &tracing_max_lat_fops
);
4197 trace_create_file("README", 0444, d_tracer
,
4198 NULL
, &tracing_readme_fops
);
4200 trace_create_file("trace_pipe", 0444, d_tracer
,
4201 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
4203 trace_create_file("buffer_size_kb", 0644, d_tracer
,
4204 &global_trace
, &tracing_entries_fops
);
4206 trace_create_file("trace_marker", 0220, d_tracer
,
4207 NULL
, &tracing_mark_fops
);
4209 trace_create_file("saved_cmdlines", 0444, d_tracer
,
4210 NULL
, &tracing_saved_cmdlines_fops
);
4212 trace_create_file("trace_clock", 0644, d_tracer
, NULL
,
4215 #ifdef CONFIG_DYNAMIC_FTRACE
4216 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
4217 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
4219 #ifdef CONFIG_SYSPROF_TRACER
4220 init_tracer_sysprof_debugfs(d_tracer
);
4223 create_trace_options_dir();
4225 for_each_tracing_cpu(cpu
)
4226 tracing_init_debugfs_percpu(cpu
);
4231 static int trace_panic_handler(struct notifier_block
*this,
4232 unsigned long event
, void *unused
)
4234 if (ftrace_dump_on_oops
)
4239 static struct notifier_block trace_panic_notifier
= {
4240 .notifier_call
= trace_panic_handler
,
4242 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
4245 static int trace_die_handler(struct notifier_block
*self
,
4251 if (ftrace_dump_on_oops
)
4260 static struct notifier_block trace_die_notifier
= {
4261 .notifier_call
= trace_die_handler
,
4266 * printk is set to max of 1024, we really don't need it that big.
4267 * Nothing should be printing 1000 characters anyway.
4269 #define TRACE_MAX_PRINT 1000
4272 * Define here KERN_TRACE so that we have one place to modify
4273 * it if we decide to change what log level the ftrace dump
4276 #define KERN_TRACE KERN_EMERG
4279 trace_printk_seq(struct trace_seq
*s
)
4281 /* Probably should print a warning here. */
4285 /* should be zero ended, but we are paranoid. */
4286 s
->buffer
[s
->len
] = 0;
4288 printk(KERN_TRACE
"%s", s
->buffer
);
4293 static void __ftrace_dump(bool disable_tracing
)
4295 static raw_spinlock_t ftrace_dump_lock
=
4296 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
4297 /* use static because iter can be a bit big for the stack */
4298 static struct trace_iterator iter
;
4299 unsigned int old_userobj
;
4300 static int dump_ran
;
4301 unsigned long flags
;
4305 local_irq_save(flags
);
4306 __raw_spin_lock(&ftrace_dump_lock
);
4314 if (disable_tracing
)
4317 for_each_tracing_cpu(cpu
) {
4318 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
4321 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
4323 /* don't look at user memory in panic mode */
4324 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4326 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4328 /* Simulate the iterator */
4329 iter
.tr
= &global_trace
;
4330 iter
.trace
= current_trace
;
4331 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4334 * We need to stop all tracing on all CPUS to read the
4335 * the next buffer. This is a bit expensive, but is
4336 * not done often. We fill all what we can read,
4337 * and then release the locks again.
4340 while (!trace_empty(&iter
)) {
4343 printk(KERN_TRACE
"---------------------------------\n");
4347 /* reset all but tr, trace, and overruns */
4348 memset(&iter
.seq
, 0,
4349 sizeof(struct trace_iterator
) -
4350 offsetof(struct trace_iterator
, seq
));
4351 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4354 if (find_next_entry_inc(&iter
) != NULL
) {
4357 ret
= print_trace_line(&iter
);
4358 if (ret
!= TRACE_TYPE_NO_CONSUME
)
4359 trace_consume(&iter
);
4362 trace_printk_seq(&iter
.seq
);
4366 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4368 printk(KERN_TRACE
"---------------------------------\n");
4370 /* Re-enable tracing if requested */
4371 if (!disable_tracing
) {
4372 trace_flags
|= old_userobj
;
4374 for_each_tracing_cpu(cpu
) {
4375 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
4381 __raw_spin_unlock(&ftrace_dump_lock
);
4382 local_irq_restore(flags
);
4385 /* By default: disable tracing after the dump */
4386 void ftrace_dump(void)
4388 __ftrace_dump(true);
4391 __init
static int tracer_alloc_buffers(void)
4397 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4400 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4401 goto out_free_buffer_mask
;
4403 if (!alloc_cpumask_var(&tracing_reader_cpumask
, GFP_KERNEL
))
4404 goto out_free_tracing_cpumask
;
4406 /* To save memory, keep the ring buffer size to its minimum */
4407 if (ring_buffer_expanded
)
4408 ring_buf_size
= trace_buf_size
;
4412 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4413 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4414 cpumask_clear(tracing_reader_cpumask
);
4416 /* TODO: make the number of buffers hot pluggable with CPUS */
4417 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
,
4418 TRACE_BUFFER_FLAGS
);
4419 if (!global_trace
.buffer
) {
4420 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4422 goto out_free_cpumask
;
4424 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4427 #ifdef CONFIG_TRACER_MAX_TRACE
4428 max_tr
.buffer
= ring_buffer_alloc(ring_buf_size
,
4429 TRACE_BUFFER_FLAGS
);
4430 if (!max_tr
.buffer
) {
4431 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4433 ring_buffer_free(global_trace
.buffer
);
4434 goto out_free_cpumask
;
4436 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
4437 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
4440 /* Allocate the first page for all buffers */
4441 for_each_tracing_cpu(i
) {
4442 global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4443 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
4446 trace_init_cmdlines();
4448 register_tracer(&nop_trace
);
4449 current_trace
= &nop_trace
;
4450 #ifdef CONFIG_BOOT_TRACER
4451 register_tracer(&boot_tracer
);
4453 /* All seems OK, enable tracing */
4454 tracing_disabled
= 0;
4456 atomic_notifier_chain_register(&panic_notifier_list
,
4457 &trace_panic_notifier
);
4459 register_die_notifier(&trace_die_notifier
);
4464 free_cpumask_var(tracing_reader_cpumask
);
4465 out_free_tracing_cpumask
:
4466 free_cpumask_var(tracing_cpumask
);
4467 out_free_buffer_mask
:
4468 free_cpumask_var(tracing_buffer_mask
);
4473 __init
static int clear_boot_tracer(void)
4476 * The default tracer at boot buffer is an init section.
4477 * This function is called in lateinit. If we did not
4478 * find the boot tracer, then clear it out, to prevent
4479 * later registration from accessing the buffer that is
4480 * about to be freed.
4482 if (!default_bootup_tracer
)
4485 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4486 default_bootup_tracer
);
4487 default_bootup_tracer
= NULL
;
4492 early_initcall(tracer_alloc_buffers
);
4493 fs_initcall(tracer_init_debugfs
);
4494 late_initcall(clear_boot_tracer
);