2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/ring_buffer.h>
15 #include <linux/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/smp_lock.h>
21 #include <linux/notifier.h>
22 #include <linux/irqflags.h>
23 #include <linux/debugfs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/kprobes.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/ctype.h>
36 #include <linux/init.h>
37 #include <linux/poll.h>
38 #include <linux/gfp.h>
42 #include "trace_output.h"
44 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
47 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
50 int ring_buffer_expanded
;
53 * We need to change this state when a selftest is running.
54 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
56 * insertions into the ring-buffer such as trace_printk could occurred
57 * at the same time, giving false positive or negative results.
59 static bool __read_mostly tracing_selftest_running
;
62 * If a tracer is running, we do not want to run SELFTEST.
64 bool __read_mostly tracing_selftest_disabled
;
66 /* For tracers that don't implement custom flags */
67 static struct tracer_opt dummy_tracer_opt
[] = {
71 static struct tracer_flags dummy_tracer_flags
= {
73 .opts
= dummy_tracer_opt
76 static int dummy_set_flag(u32 old_flags
, u32 bit
, int set
)
82 * Kill all tracing for good (never come back).
83 * It is initialized to 1 but will turn to zero if the initialization
84 * of the tracer is successful. But that is the only place that sets
87 static int tracing_disabled
= 1;
89 DEFINE_PER_CPU(local_t
, ftrace_cpu_disabled
);
91 static inline void ftrace_disable_cpu(void)
94 local_inc(&__get_cpu_var(ftrace_cpu_disabled
));
97 static inline void ftrace_enable_cpu(void)
99 local_dec(&__get_cpu_var(ftrace_cpu_disabled
));
103 static cpumask_var_t __read_mostly tracing_buffer_mask
;
105 /* Define which cpu buffers are currently read in trace_pipe */
106 static cpumask_var_t tracing_reader_cpumask
;
108 #define for_each_tracing_cpu(cpu) \
109 for_each_cpu(cpu, tracing_buffer_mask)
112 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
114 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
115 * is set, then ftrace_dump is called. This will output the contents
116 * of the ftrace buffers to the console. This is very useful for
117 * capturing traces that lead to crashes and outputing it to a
120 * It is default off, but you can enable it with either specifying
121 * "ftrace_dump_on_oops" in the kernel command line, or setting
122 * /proc/sys/kernel/ftrace_dump_on_oops to true.
124 int ftrace_dump_on_oops
;
126 static int tracing_set_tracer(const char *buf
);
128 #define BOOTUP_TRACER_SIZE 100
129 static char bootup_tracer_buf
[BOOTUP_TRACER_SIZE
] __initdata
;
130 static char *default_bootup_tracer
;
132 static int __init
set_ftrace(char *str
)
134 strncpy(bootup_tracer_buf
, str
, BOOTUP_TRACER_SIZE
);
135 default_bootup_tracer
= bootup_tracer_buf
;
136 /* We are using ftrace early, expand it */
137 ring_buffer_expanded
= 1;
140 __setup("ftrace=", set_ftrace
);
142 static int __init
set_ftrace_dump_on_oops(char *str
)
144 ftrace_dump_on_oops
= 1;
147 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
149 unsigned long long ns2usecs(cycle_t nsec
)
157 * The global_trace is the descriptor that holds the tracing
158 * buffers for the live tracing. For each CPU, it contains
159 * a link list of pages that will store trace entries. The
160 * page descriptor of the pages in the memory is used to hold
161 * the link list by linking the lru item in the page descriptor
162 * to each of the pages in the buffer per CPU.
164 * For each active CPU there is a data field that holds the
165 * pages for the buffer for that CPU. Each CPU has the same number
166 * of pages allocated for its buffer.
168 static struct trace_array global_trace
;
170 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
172 int filter_current_check_discard(struct ring_buffer
*buffer
,
173 struct ftrace_event_call
*call
, void *rec
,
174 struct ring_buffer_event
*event
)
176 return filter_check_discard(call
, rec
, buffer
, event
);
178 EXPORT_SYMBOL_GPL(filter_current_check_discard
);
180 cycle_t
ftrace_now(int cpu
)
184 /* Early boot up does not have a buffer yet */
185 if (!global_trace
.buffer
)
186 return trace_clock_local();
188 ts
= ring_buffer_time_stamp(global_trace
.buffer
, cpu
);
189 ring_buffer_normalize_time_stamp(global_trace
.buffer
, cpu
, &ts
);
195 * The max_tr is used to snapshot the global_trace when a maximum
196 * latency is reached. Some tracers will use this to store a maximum
197 * trace while it continues examining live traces.
199 * The buffers for the max_tr are set up the same as the global_trace.
200 * When a snapshot is taken, the link list of the max_tr is swapped
201 * with the link list of the global_trace and the buffers are reset for
202 * the global_trace so the tracing can continue.
204 static struct trace_array max_tr
;
206 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
208 /* tracer_enabled is used to toggle activation of a tracer */
209 static int tracer_enabled
= 1;
212 * tracing_is_enabled - return tracer_enabled status
214 * This function is used by other tracers to know the status
215 * of the tracer_enabled flag. Tracers may use this function
216 * to know if it should enable their features when starting
217 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
219 int tracing_is_enabled(void)
221 return tracer_enabled
;
225 * trace_buf_size is the size in bytes that is allocated
226 * for a buffer. Note, the number of bytes is always rounded
229 * This number is purposely set to a low number of 16384.
230 * If the dump on oops happens, it will be much appreciated
231 * to not have to wait for all that output. Anyway this can be
232 * boot time and run time configurable.
234 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
236 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
238 /* trace_types holds a link list of available tracers. */
239 static struct tracer
*trace_types __read_mostly
;
241 /* current_trace points to the tracer that is currently active */
242 static struct tracer
*current_trace __read_mostly
;
245 * max_tracer_type_len is used to simplify the allocating of
246 * buffers to read userspace tracer names. We keep track of
247 * the longest tracer name registered.
249 static int max_tracer_type_len
;
252 * trace_types_lock is used to protect the trace_types list.
253 * This lock is also used to keep user access serialized.
254 * Accesses from userspace will grab this lock while userspace
255 * activities happen inside the kernel.
257 static DEFINE_MUTEX(trace_types_lock
);
259 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
260 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
262 /* trace_flags holds trace_options default values */
263 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
264 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
| TRACE_ITER_SLEEP_TIME
|
265 TRACE_ITER_GRAPH_TIME
;
267 static int trace_stop_count
;
268 static DEFINE_SPINLOCK(tracing_start_lock
);
271 * trace_wake_up - wake up tasks waiting for trace input
273 * Simply wakes up any task that is blocked on the trace_wait
274 * queue. These is used with trace_poll for tasks polling the trace.
276 void trace_wake_up(void)
279 * The runqueue_is_locked() can fail, but this is the best we
282 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
283 wake_up(&trace_wait
);
286 static int __init
set_buf_size(char *str
)
288 unsigned long buf_size
;
292 buf_size
= memparse(str
, &str
);
293 /* nr_entries can not be zero */
296 trace_buf_size
= buf_size
;
299 __setup("trace_buf_size=", set_buf_size
);
301 unsigned long nsecs_to_usecs(unsigned long nsecs
)
306 /* These must match the bit postions in trace_iterator_flags */
307 static const char *trace_options
[] = {
336 { trace_clock_local
, "local" },
337 { trace_clock_global
, "global" },
342 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
350 if (s
->len
<= s
->readpos
)
353 len
= s
->len
- s
->readpos
;
356 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
366 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
371 if (s
->len
<= s
->readpos
)
374 len
= s
->len
- s
->readpos
;
377 ret
= memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
386 * ftrace_max_lock is used to protect the swapping of buffers
387 * when taking a max snapshot. The buffers themselves are
388 * protected by per_cpu spinlocks. But the action of the swap
389 * needs its own lock.
391 * This is defined as a raw_spinlock_t in order to help
392 * with performance when lockdep debugging is enabled.
394 * It is also used in other places outside the update_max_tr
395 * so it needs to be defined outside of the
396 * CONFIG_TRACER_MAX_TRACE.
398 static raw_spinlock_t ftrace_max_lock
=
399 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
401 #ifdef CONFIG_TRACER_MAX_TRACE
402 unsigned long __read_mostly tracing_max_latency
;
403 unsigned long __read_mostly tracing_thresh
;
406 * Copy the new maximum trace into the separate maximum-trace
407 * structure. (this way the maximum trace is permanently saved,
408 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
411 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
413 struct trace_array_cpu
*data
= tr
->data
[cpu
];
414 struct trace_array_cpu
*max_data
= tr
->data
[cpu
];
417 max_tr
.time_start
= data
->preempt_timestamp
;
419 max_data
= max_tr
.data
[cpu
];
420 max_data
->saved_latency
= tracing_max_latency
;
421 max_data
->critical_start
= data
->critical_start
;
422 max_data
->critical_end
= data
->critical_end
;
424 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
425 max_data
->pid
= tsk
->pid
;
426 max_data
->uid
= task_uid(tsk
);
427 max_data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
428 max_data
->policy
= tsk
->policy
;
429 max_data
->rt_priority
= tsk
->rt_priority
;
431 /* record this tasks comm */
432 tracing_record_cmdline(tsk
);
436 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
438 * @tsk: the task with the latency
439 * @cpu: The cpu that initiated the trace.
441 * Flip the buffers between the @tr and the max_tr and record information
442 * about which task was the cause of this latency.
445 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
447 struct ring_buffer
*buf
= tr
->buffer
;
449 if (trace_stop_count
)
452 WARN_ON_ONCE(!irqs_disabled());
453 __raw_spin_lock(&ftrace_max_lock
);
455 tr
->buffer
= max_tr
.buffer
;
458 __update_max_tr(tr
, tsk
, cpu
);
459 __raw_spin_unlock(&ftrace_max_lock
);
463 * update_max_tr_single - only copy one trace over, and reset the rest
465 * @tsk - task with the latency
466 * @cpu - the cpu of the buffer to copy.
468 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
471 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
475 if (trace_stop_count
)
478 WARN_ON_ONCE(!irqs_disabled());
479 __raw_spin_lock(&ftrace_max_lock
);
481 ftrace_disable_cpu();
483 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
487 * We failed to swap the buffer due to a commit taking
488 * place on this CPU. We fail to record, but we reset
489 * the max trace buffer (no one writes directly to it)
490 * and flag that it failed.
492 trace_array_printk(&max_tr
, _THIS_IP_
,
493 "Failed to swap buffers due to commit in progress\n");
498 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
&& ret
!= -EBUSY
);
500 __update_max_tr(tr
, tsk
, cpu
);
501 __raw_spin_unlock(&ftrace_max_lock
);
503 #endif /* CONFIG_TRACER_MAX_TRACE */
506 * register_tracer - register a tracer with the ftrace system.
507 * @type - the plugin for the tracer
509 * Register a new plugin tracer.
511 int register_tracer(struct tracer
*type
)
512 __releases(kernel_lock
)
513 __acquires(kernel_lock
)
520 pr_info("Tracer must have a name\n");
525 * When this gets called we hold the BKL which means that
526 * preemption is disabled. Various trace selftests however
527 * need to disable and enable preemption for successful tests.
528 * So we drop the BKL here and grab it after the tests again.
531 mutex_lock(&trace_types_lock
);
533 tracing_selftest_running
= true;
535 for (t
= trace_types
; t
; t
= t
->next
) {
536 if (strcmp(type
->name
, t
->name
) == 0) {
538 pr_info("Trace %s already registered\n",
546 type
->set_flag
= &dummy_set_flag
;
548 type
->flags
= &dummy_tracer_flags
;
550 if (!type
->flags
->opts
)
551 type
->flags
->opts
= dummy_tracer_opt
;
552 if (!type
->wait_pipe
)
553 type
->wait_pipe
= default_wait_pipe
;
556 #ifdef CONFIG_FTRACE_STARTUP_TEST
557 if (type
->selftest
&& !tracing_selftest_disabled
) {
558 struct tracer
*saved_tracer
= current_trace
;
559 struct trace_array
*tr
= &global_trace
;
562 * Run a selftest on this tracer.
563 * Here we reset the trace buffer, and set the current
564 * tracer to be this tracer. The tracer can then run some
565 * internal tracing to verify that everything is in order.
566 * If we fail, we do not register this tracer.
568 tracing_reset_online_cpus(tr
);
570 current_trace
= type
;
571 /* the test is responsible for initializing and enabling */
572 pr_info("Testing tracer %s: ", type
->name
);
573 ret
= type
->selftest(type
, tr
);
574 /* the test is responsible for resetting too */
575 current_trace
= saved_tracer
;
577 printk(KERN_CONT
"FAILED!\n");
580 /* Only reset on passing, to avoid touching corrupted buffers */
581 tracing_reset_online_cpus(tr
);
583 printk(KERN_CONT
"PASSED\n");
587 type
->next
= trace_types
;
589 len
= strlen(type
->name
);
590 if (len
> max_tracer_type_len
)
591 max_tracer_type_len
= len
;
594 tracing_selftest_running
= false;
595 mutex_unlock(&trace_types_lock
);
597 if (ret
|| !default_bootup_tracer
)
600 if (strncmp(default_bootup_tracer
, type
->name
, BOOTUP_TRACER_SIZE
))
603 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
604 /* Do we want this tracer to start on bootup? */
605 tracing_set_tracer(type
->name
);
606 default_bootup_tracer
= NULL
;
607 /* disable other selftests, since this will break it. */
608 tracing_selftest_disabled
= 1;
609 #ifdef CONFIG_FTRACE_STARTUP_TEST
610 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
619 void unregister_tracer(struct tracer
*type
)
624 mutex_lock(&trace_types_lock
);
625 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
629 pr_info("Trace %s not registered\n", type
->name
);
635 if (type
== current_trace
&& tracer_enabled
) {
638 if (current_trace
->stop
)
639 current_trace
->stop(&global_trace
);
640 current_trace
= &nop_trace
;
643 if (strlen(type
->name
) != max_tracer_type_len
)
646 max_tracer_type_len
= 0;
647 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
648 len
= strlen((*t
)->name
);
649 if (len
> max_tracer_type_len
)
650 max_tracer_type_len
= len
;
653 mutex_unlock(&trace_types_lock
);
656 static void __tracing_reset(struct trace_array
*tr
, int cpu
)
658 ftrace_disable_cpu();
659 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
663 void tracing_reset(struct trace_array
*tr
, int cpu
)
665 struct ring_buffer
*buffer
= tr
->buffer
;
667 ring_buffer_record_disable(buffer
);
669 /* Make sure all commits have finished */
671 __tracing_reset(tr
, cpu
);
673 ring_buffer_record_enable(buffer
);
676 void tracing_reset_online_cpus(struct trace_array
*tr
)
678 struct ring_buffer
*buffer
= tr
->buffer
;
681 ring_buffer_record_disable(buffer
);
683 /* Make sure all commits have finished */
686 tr
->time_start
= ftrace_now(tr
->cpu
);
688 for_each_online_cpu(cpu
)
689 __tracing_reset(tr
, cpu
);
691 ring_buffer_record_enable(buffer
);
694 void tracing_reset_current(int cpu
)
696 tracing_reset(&global_trace
, cpu
);
699 void tracing_reset_current_online_cpus(void)
701 tracing_reset_online_cpus(&global_trace
);
704 #define SAVED_CMDLINES 128
705 #define NO_CMDLINE_MAP UINT_MAX
706 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
707 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
708 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
709 static int cmdline_idx
;
710 static raw_spinlock_t trace_cmdline_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
712 /* temporary disable recording */
713 static atomic_t trace_record_cmdline_disabled __read_mostly
;
715 static void trace_init_cmdlines(void)
717 memset(&map_pid_to_cmdline
, NO_CMDLINE_MAP
, sizeof(map_pid_to_cmdline
));
718 memset(&map_cmdline_to_pid
, NO_CMDLINE_MAP
, sizeof(map_cmdline_to_pid
));
723 * ftrace_off_permanent - disable all ftrace code permanently
725 * This should only be called when a serious anomally has
726 * been detected. This will turn off the function tracing,
727 * ring buffers, and other tracing utilites. It takes no
728 * locks and can be called from any context.
730 void ftrace_off_permanent(void)
732 tracing_disabled
= 1;
734 tracing_off_permanent();
738 * tracing_start - quick start of the tracer
740 * If tracing is enabled but was stopped by tracing_stop,
741 * this will start the tracer back up.
743 void tracing_start(void)
745 struct ring_buffer
*buffer
;
748 if (tracing_disabled
)
751 spin_lock_irqsave(&tracing_start_lock
, flags
);
752 if (--trace_stop_count
) {
753 if (trace_stop_count
< 0) {
754 /* Someone screwed up their debugging */
756 trace_stop_count
= 0;
762 buffer
= global_trace
.buffer
;
764 ring_buffer_record_enable(buffer
);
766 buffer
= max_tr
.buffer
;
768 ring_buffer_record_enable(buffer
);
772 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
776 * tracing_stop - quick stop of the tracer
778 * Light weight way to stop tracing. Use in conjunction with
781 void tracing_stop(void)
783 struct ring_buffer
*buffer
;
787 spin_lock_irqsave(&tracing_start_lock
, flags
);
788 if (trace_stop_count
++)
791 buffer
= global_trace
.buffer
;
793 ring_buffer_record_disable(buffer
);
795 buffer
= max_tr
.buffer
;
797 ring_buffer_record_disable(buffer
);
800 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
803 void trace_stop_cmdline_recording(void);
805 static void trace_save_cmdline(struct task_struct
*tsk
)
809 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
813 * It's not the end of the world if we don't get
814 * the lock, but we also don't want to spin
815 * nor do we want to disable interrupts,
816 * so if we miss here, then better luck next time.
818 if (!__raw_spin_trylock(&trace_cmdline_lock
))
821 idx
= map_pid_to_cmdline
[tsk
->pid
];
822 if (idx
== NO_CMDLINE_MAP
) {
823 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
826 * Check whether the cmdline buffer at idx has a pid
827 * mapped. We are going to overwrite that entry so we
828 * need to clear the map_pid_to_cmdline. Otherwise we
829 * would read the new comm for the old pid.
831 pid
= map_cmdline_to_pid
[idx
];
832 if (pid
!= NO_CMDLINE_MAP
)
833 map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
835 map_cmdline_to_pid
[idx
] = tsk
->pid
;
836 map_pid_to_cmdline
[tsk
->pid
] = idx
;
841 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
843 __raw_spin_unlock(&trace_cmdline_lock
);
846 void trace_find_cmdline(int pid
, char comm
[])
851 strcpy(comm
, "<idle>");
855 if (pid
> PID_MAX_DEFAULT
) {
856 strcpy(comm
, "<...>");
861 __raw_spin_lock(&trace_cmdline_lock
);
862 map
= map_pid_to_cmdline
[pid
];
863 if (map
!= NO_CMDLINE_MAP
)
864 strcpy(comm
, saved_cmdlines
[map
]);
866 strcpy(comm
, "<...>");
868 __raw_spin_unlock(&trace_cmdline_lock
);
872 void tracing_record_cmdline(struct task_struct
*tsk
)
874 if (atomic_read(&trace_record_cmdline_disabled
) || !tracer_enabled
||
878 trace_save_cmdline(tsk
);
882 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
885 struct task_struct
*tsk
= current
;
887 entry
->preempt_count
= pc
& 0xff;
888 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
889 entry
->tgid
= (tsk
) ? tsk
->tgid
: 0;
891 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
892 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
894 TRACE_FLAG_IRQS_NOSUPPORT
|
896 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
897 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
898 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
900 EXPORT_SYMBOL_GPL(tracing_generic_entry_update
);
902 struct ring_buffer_event
*
903 trace_buffer_lock_reserve(struct ring_buffer
*buffer
,
906 unsigned long flags
, int pc
)
908 struct ring_buffer_event
*event
;
910 event
= ring_buffer_lock_reserve(buffer
, len
);
912 struct trace_entry
*ent
= ring_buffer_event_data(event
);
914 tracing_generic_entry_update(ent
, flags
, pc
);
922 __trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
923 struct ring_buffer_event
*event
,
924 unsigned long flags
, int pc
,
927 ring_buffer_unlock_commit(buffer
, event
);
929 ftrace_trace_stack(buffer
, flags
, 6, pc
);
930 ftrace_trace_userstack(buffer
, flags
, pc
);
936 void trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
937 struct ring_buffer_event
*event
,
938 unsigned long flags
, int pc
)
940 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
943 struct ring_buffer_event
*
944 trace_current_buffer_lock_reserve(struct ring_buffer
**current_rb
,
945 int type
, unsigned long len
,
946 unsigned long flags
, int pc
)
948 *current_rb
= global_trace
.buffer
;
949 return trace_buffer_lock_reserve(*current_rb
,
950 type
, len
, flags
, pc
);
952 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve
);
954 void trace_current_buffer_unlock_commit(struct ring_buffer
*buffer
,
955 struct ring_buffer_event
*event
,
956 unsigned long flags
, int pc
)
958 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 1);
960 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit
);
962 void trace_nowake_buffer_unlock_commit(struct ring_buffer
*buffer
,
963 struct ring_buffer_event
*event
,
964 unsigned long flags
, int pc
)
966 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
, 0);
968 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit
);
970 void trace_current_buffer_discard_commit(struct ring_buffer
*buffer
,
971 struct ring_buffer_event
*event
)
973 ring_buffer_discard_commit(buffer
, event
);
975 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
978 trace_function(struct trace_array
*tr
,
979 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
982 struct ftrace_event_call
*call
= &event_function
;
983 struct ring_buffer
*buffer
= tr
->buffer
;
984 struct ring_buffer_event
*event
;
985 struct ftrace_entry
*entry
;
987 /* If we are reading the ring buffer, don't trace */
988 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
991 event
= trace_buffer_lock_reserve(buffer
, TRACE_FN
, sizeof(*entry
),
995 entry
= ring_buffer_event_data(event
);
997 entry
->parent_ip
= parent_ip
;
999 if (!filter_check_discard(call
, entry
, buffer
, event
))
1000 ring_buffer_unlock_commit(buffer
, event
);
1004 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
1005 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1008 if (likely(!atomic_read(&data
->disabled
)))
1009 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
1012 #ifdef CONFIG_STACKTRACE
1013 static void __ftrace_trace_stack(struct ring_buffer
*buffer
,
1014 unsigned long flags
,
1017 struct ftrace_event_call
*call
= &event_kernel_stack
;
1018 struct ring_buffer_event
*event
;
1019 struct stack_entry
*entry
;
1020 struct stack_trace trace
;
1022 event
= trace_buffer_lock_reserve(buffer
, TRACE_STACK
,
1023 sizeof(*entry
), flags
, pc
);
1026 entry
= ring_buffer_event_data(event
);
1027 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1029 trace
.nr_entries
= 0;
1030 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1032 trace
.entries
= entry
->caller
;
1034 save_stack_trace(&trace
);
1035 if (!filter_check_discard(call
, entry
, buffer
, event
))
1036 ring_buffer_unlock_commit(buffer
, event
);
1039 void ftrace_trace_stack(struct ring_buffer
*buffer
, unsigned long flags
,
1042 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1045 __ftrace_trace_stack(buffer
, flags
, skip
, pc
);
1048 void __trace_stack(struct trace_array
*tr
, unsigned long flags
, int skip
,
1051 __ftrace_trace_stack(tr
->buffer
, flags
, skip
, pc
);
1055 ftrace_trace_userstack(struct ring_buffer
*buffer
, unsigned long flags
, int pc
)
1057 struct ftrace_event_call
*call
= &event_user_stack
;
1058 struct ring_buffer_event
*event
;
1059 struct userstack_entry
*entry
;
1060 struct stack_trace trace
;
1062 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1065 event
= trace_buffer_lock_reserve(buffer
, TRACE_USER_STACK
,
1066 sizeof(*entry
), flags
, pc
);
1069 entry
= ring_buffer_event_data(event
);
1071 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1073 trace
.nr_entries
= 0;
1074 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1076 trace
.entries
= entry
->caller
;
1078 save_stack_trace_user(&trace
);
1079 if (!filter_check_discard(call
, entry
, buffer
, event
))
1080 ring_buffer_unlock_commit(buffer
, event
);
1084 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1086 ftrace_trace_userstack(tr
, flags
, preempt_count());
1090 #endif /* CONFIG_STACKTRACE */
1093 ftrace_trace_special(void *__tr
,
1094 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1097 struct ring_buffer_event
*event
;
1098 struct trace_array
*tr
= __tr
;
1099 struct ring_buffer
*buffer
= tr
->buffer
;
1100 struct special_entry
*entry
;
1102 event
= trace_buffer_lock_reserve(buffer
, TRACE_SPECIAL
,
1103 sizeof(*entry
), 0, pc
);
1106 entry
= ring_buffer_event_data(event
);
1110 trace_buffer_unlock_commit(buffer
, event
, 0, pc
);
1114 __trace_special(void *__tr
, void *__data
,
1115 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1117 ftrace_trace_special(__tr
, arg1
, arg2
, arg3
, preempt_count());
1121 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1123 struct trace_array
*tr
= &global_trace
;
1124 struct trace_array_cpu
*data
;
1125 unsigned long flags
;
1129 if (tracing_disabled
)
1132 pc
= preempt_count();
1133 local_irq_save(flags
);
1134 cpu
= raw_smp_processor_id();
1135 data
= tr
->data
[cpu
];
1137 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1138 ftrace_trace_special(tr
, arg1
, arg2
, arg3
, pc
);
1140 atomic_dec(&data
->disabled
);
1141 local_irq_restore(flags
);
1145 * trace_vbprintk - write binary msg to tracing buffer
1148 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1150 static raw_spinlock_t trace_buf_lock
=
1151 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
1152 static u32 trace_buf
[TRACE_BUF_SIZE
];
1154 struct ftrace_event_call
*call
= &event_bprint
;
1155 struct ring_buffer_event
*event
;
1156 struct ring_buffer
*buffer
;
1157 struct trace_array
*tr
= &global_trace
;
1158 struct trace_array_cpu
*data
;
1159 struct bprint_entry
*entry
;
1160 unsigned long flags
;
1163 int cpu
, len
= 0, size
, pc
;
1165 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1168 /* Don't pollute graph traces with trace_vprintk internals */
1169 pause_graph_tracing();
1171 pc
= preempt_count();
1172 resched
= ftrace_preempt_disable();
1173 cpu
= raw_smp_processor_id();
1174 data
= tr
->data
[cpu
];
1176 disable
= atomic_inc_return(&data
->disabled
);
1177 if (unlikely(disable
!= 1))
1180 /* Lockdep uses trace_printk for lock tracing */
1181 local_irq_save(flags
);
1182 __raw_spin_lock(&trace_buf_lock
);
1183 len
= vbin_printf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1185 if (len
> TRACE_BUF_SIZE
|| len
< 0)
1188 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1189 buffer
= tr
->buffer
;
1190 event
= trace_buffer_lock_reserve(buffer
, TRACE_BPRINT
, size
,
1194 entry
= ring_buffer_event_data(event
);
1198 memcpy(entry
->buf
, trace_buf
, sizeof(u32
) * len
);
1199 if (!filter_check_discard(call
, entry
, buffer
, event
))
1200 ring_buffer_unlock_commit(buffer
, event
);
1203 __raw_spin_unlock(&trace_buf_lock
);
1204 local_irq_restore(flags
);
1207 atomic_dec_return(&data
->disabled
);
1208 ftrace_preempt_enable(resched
);
1209 unpause_graph_tracing();
1213 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1215 int trace_array_printk(struct trace_array
*tr
,
1216 unsigned long ip
, const char *fmt
, ...)
1221 if (!(trace_flags
& TRACE_ITER_PRINTK
))
1225 ret
= trace_array_vprintk(tr
, ip
, fmt
, ap
);
1230 int trace_array_vprintk(struct trace_array
*tr
,
1231 unsigned long ip
, const char *fmt
, va_list args
)
1233 static raw_spinlock_t trace_buf_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
1234 static char trace_buf
[TRACE_BUF_SIZE
];
1236 struct ftrace_event_call
*call
= &event_print
;
1237 struct ring_buffer_event
*event
;
1238 struct ring_buffer
*buffer
;
1239 struct trace_array_cpu
*data
;
1240 int cpu
, len
= 0, size
, pc
;
1241 struct print_entry
*entry
;
1242 unsigned long irq_flags
;
1245 if (tracing_disabled
|| tracing_selftest_running
)
1248 pc
= preempt_count();
1249 preempt_disable_notrace();
1250 cpu
= raw_smp_processor_id();
1251 data
= tr
->data
[cpu
];
1253 disable
= atomic_inc_return(&data
->disabled
);
1254 if (unlikely(disable
!= 1))
1257 pause_graph_tracing();
1258 raw_local_irq_save(irq_flags
);
1259 __raw_spin_lock(&trace_buf_lock
);
1260 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1262 len
= min(len
, TRACE_BUF_SIZE
-1);
1265 size
= sizeof(*entry
) + len
+ 1;
1266 buffer
= tr
->buffer
;
1267 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
1271 entry
= ring_buffer_event_data(event
);
1274 memcpy(&entry
->buf
, trace_buf
, len
);
1275 entry
->buf
[len
] = 0;
1276 if (!filter_check_discard(call
, entry
, buffer
, event
))
1277 ring_buffer_unlock_commit(buffer
, event
);
1280 __raw_spin_unlock(&trace_buf_lock
);
1281 raw_local_irq_restore(irq_flags
);
1282 unpause_graph_tracing();
1284 atomic_dec_return(&data
->disabled
);
1285 preempt_enable_notrace();
1290 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1292 return trace_array_printk(&global_trace
, ip
, fmt
, args
);
1294 EXPORT_SYMBOL_GPL(trace_vprintk
);
1296 enum trace_file_type
{
1297 TRACE_FILE_LAT_FMT
= 1,
1298 TRACE_FILE_ANNOTATE
= 2,
1301 static void trace_iterator_increment(struct trace_iterator
*iter
)
1303 /* Don't allow ftrace to trace into the ring buffers */
1304 ftrace_disable_cpu();
1307 if (iter
->buffer_iter
[iter
->cpu
])
1308 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1310 ftrace_enable_cpu();
1313 static struct trace_entry
*
1314 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1316 struct ring_buffer_event
*event
;
1317 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1319 /* Don't allow ftrace to trace into the ring buffers */
1320 ftrace_disable_cpu();
1323 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1325 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1327 ftrace_enable_cpu();
1329 return event
? ring_buffer_event_data(event
) : NULL
;
1332 static struct trace_entry
*
1333 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1335 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1336 struct trace_entry
*ent
, *next
= NULL
;
1337 int cpu_file
= iter
->cpu_file
;
1338 u64 next_ts
= 0, ts
;
1343 * If we are in a per_cpu trace file, don't bother by iterating over
1344 * all cpu and peek directly.
1346 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1347 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1349 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
);
1351 *ent_cpu
= cpu_file
;
1356 for_each_tracing_cpu(cpu
) {
1358 if (ring_buffer_empty_cpu(buffer
, cpu
))
1361 ent
= peek_next_entry(iter
, cpu
, &ts
);
1364 * Pick the entry with the smallest timestamp:
1366 if (ent
&& (!next
|| ts
< next_ts
)) {
1374 *ent_cpu
= next_cpu
;
1382 /* Find the next real entry, without updating the iterator itself */
1383 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1384 int *ent_cpu
, u64
*ent_ts
)
1386 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1389 /* Find the next real entry, and increment the iterator to the next entry */
1390 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1392 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1395 trace_iterator_increment(iter
);
1397 return iter
->ent
? iter
: NULL
;
1400 static void trace_consume(struct trace_iterator
*iter
)
1402 /* Don't allow ftrace to trace into the ring buffers */
1403 ftrace_disable_cpu();
1404 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1405 ftrace_enable_cpu();
1408 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1410 struct trace_iterator
*iter
= m
->private;
1416 /* can't go backwards */
1421 ent
= find_next_entry_inc(iter
);
1425 while (ent
&& iter
->idx
< i
)
1426 ent
= find_next_entry_inc(iter
);
1433 static void tracing_iter_reset(struct trace_iterator
*iter
, int cpu
)
1435 struct trace_array
*tr
= iter
->tr
;
1436 struct ring_buffer_event
*event
;
1437 struct ring_buffer_iter
*buf_iter
;
1438 unsigned long entries
= 0;
1441 tr
->data
[cpu
]->skipped_entries
= 0;
1443 if (!iter
->buffer_iter
[cpu
])
1446 buf_iter
= iter
->buffer_iter
[cpu
];
1447 ring_buffer_iter_reset(buf_iter
);
1450 * We could have the case with the max latency tracers
1451 * that a reset never took place on a cpu. This is evident
1452 * by the timestamp being before the start of the buffer.
1454 while ((event
= ring_buffer_iter_peek(buf_iter
, &ts
))) {
1455 if (ts
>= iter
->tr
->time_start
)
1458 ring_buffer_read(buf_iter
, NULL
);
1461 tr
->data
[cpu
]->skipped_entries
= entries
;
1465 * No necessary locking here. The worst thing which can
1466 * happen is loosing events consumed at the same time
1467 * by a trace_pipe reader.
1468 * Other than that, we don't risk to crash the ring buffer
1469 * because it serializes the readers.
1471 * The current tracer is copied to avoid a global locking
1474 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1476 struct trace_iterator
*iter
= m
->private;
1477 static struct tracer
*old_tracer
;
1478 int cpu_file
= iter
->cpu_file
;
1483 /* copy the tracer to avoid using a global lock all around */
1484 mutex_lock(&trace_types_lock
);
1485 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1486 old_tracer
= current_trace
;
1487 *iter
->trace
= *current_trace
;
1489 mutex_unlock(&trace_types_lock
);
1491 atomic_inc(&trace_record_cmdline_disabled
);
1493 if (*pos
!= iter
->pos
) {
1498 ftrace_disable_cpu();
1500 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1501 for_each_tracing_cpu(cpu
)
1502 tracing_iter_reset(iter
, cpu
);
1504 tracing_iter_reset(iter
, cpu_file
);
1506 ftrace_enable_cpu();
1508 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1513 p
= s_next(m
, p
, &l
);
1516 trace_event_read_lock();
1520 static void s_stop(struct seq_file
*m
, void *p
)
1522 atomic_dec(&trace_record_cmdline_disabled
);
1523 trace_event_read_unlock();
1526 static void print_lat_help_header(struct seq_file
*m
)
1528 seq_puts(m
, "# _------=> CPU# \n");
1529 seq_puts(m
, "# / _-----=> irqs-off \n");
1530 seq_puts(m
, "# | / _----=> need-resched \n");
1531 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1532 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1533 seq_puts(m
, "# |||| / \n");
1534 seq_puts(m
, "# ||||| delay \n");
1535 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1536 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1539 static void print_func_help_header(struct seq_file
*m
)
1541 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1542 seq_puts(m
, "# | | | | |\n");
1547 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1549 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1550 struct trace_array
*tr
= iter
->tr
;
1551 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1552 struct tracer
*type
= current_trace
;
1553 unsigned long entries
= 0;
1554 unsigned long total
= 0;
1555 unsigned long count
;
1556 const char *name
= "preemption";
1563 for_each_tracing_cpu(cpu
) {
1564 count
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
1566 * If this buffer has skipped entries, then we hold all
1567 * entries for the trace and we need to ignore the
1568 * ones before the time stamp.
1570 if (tr
->data
[cpu
]->skipped_entries
) {
1571 count
-= tr
->data
[cpu
]->skipped_entries
;
1572 /* total is the same as the entries */
1576 ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
1580 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
1582 seq_puts(m
, "# -----------------------------------"
1583 "---------------------------------\n");
1584 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1585 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1586 nsecs_to_usecs(data
->saved_latency
),
1590 #if defined(CONFIG_PREEMPT_NONE)
1592 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1594 #elif defined(CONFIG_PREEMPT)
1599 /* These are reserved for later use */
1602 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1606 seq_puts(m
, "# -----------------\n");
1607 seq_printf(m
, "# | task: %.16s-%d "
1608 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1609 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1610 data
->policy
, data
->rt_priority
);
1611 seq_puts(m
, "# -----------------\n");
1613 if (data
->critical_start
) {
1614 seq_puts(m
, "# => started at: ");
1615 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1616 trace_print_seq(m
, &iter
->seq
);
1617 seq_puts(m
, "\n# => ended at: ");
1618 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1619 trace_print_seq(m
, &iter
->seq
);
1620 seq_puts(m
, "\n#\n");
1626 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1628 struct trace_seq
*s
= &iter
->seq
;
1630 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1633 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1636 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1639 if (iter
->tr
->data
[iter
->cpu
]->skipped_entries
)
1642 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1644 /* Don't print started cpu buffer for the first entry of the trace */
1646 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
1650 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1652 struct trace_seq
*s
= &iter
->seq
;
1653 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1654 struct trace_entry
*entry
;
1655 struct trace_event
*event
;
1659 test_cpu_buff_start(iter
);
1661 event
= ftrace_find_event(entry
->type
);
1663 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1664 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1665 if (!trace_print_lat_context(iter
))
1668 if (!trace_print_context(iter
))
1674 return event
->trace(iter
, sym_flags
);
1676 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1679 return TRACE_TYPE_HANDLED
;
1681 return TRACE_TYPE_PARTIAL_LINE
;
1684 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1686 struct trace_seq
*s
= &iter
->seq
;
1687 struct trace_entry
*entry
;
1688 struct trace_event
*event
;
1692 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1693 if (!trace_seq_printf(s
, "%d %d %llu ",
1694 entry
->pid
, iter
->cpu
, iter
->ts
))
1698 event
= ftrace_find_event(entry
->type
);
1700 return event
->raw(iter
, 0);
1702 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1705 return TRACE_TYPE_HANDLED
;
1707 return TRACE_TYPE_PARTIAL_LINE
;
1710 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1712 struct trace_seq
*s
= &iter
->seq
;
1713 unsigned char newline
= '\n';
1714 struct trace_entry
*entry
;
1715 struct trace_event
*event
;
1719 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1720 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1721 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1722 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1725 event
= ftrace_find_event(entry
->type
);
1727 enum print_line_t ret
= event
->hex(iter
, 0);
1728 if (ret
!= TRACE_TYPE_HANDLED
)
1732 SEQ_PUT_FIELD_RET(s
, newline
);
1734 return TRACE_TYPE_HANDLED
;
1737 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1739 struct trace_seq
*s
= &iter
->seq
;
1740 struct trace_entry
*entry
;
1741 struct trace_event
*event
;
1745 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1746 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1747 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1748 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1751 event
= ftrace_find_event(entry
->type
);
1752 return event
? event
->binary(iter
, 0) : TRACE_TYPE_HANDLED
;
1755 static int trace_empty(struct trace_iterator
*iter
)
1759 /* If we are looking at one CPU buffer, only check that one */
1760 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
1761 cpu
= iter
->cpu_file
;
1762 if (iter
->buffer_iter
[cpu
]) {
1763 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1766 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1772 for_each_tracing_cpu(cpu
) {
1773 if (iter
->buffer_iter
[cpu
]) {
1774 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1777 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1785 /* Called with trace_event_read_lock() held. */
1786 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1788 enum print_line_t ret
;
1790 if (iter
->trace
&& iter
->trace
->print_line
) {
1791 ret
= iter
->trace
->print_line(iter
);
1792 if (ret
!= TRACE_TYPE_UNHANDLED
)
1796 if (iter
->ent
->type
== TRACE_BPRINT
&&
1797 trace_flags
& TRACE_ITER_PRINTK
&&
1798 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1799 return trace_print_bprintk_msg_only(iter
);
1801 if (iter
->ent
->type
== TRACE_PRINT
&&
1802 trace_flags
& TRACE_ITER_PRINTK
&&
1803 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1804 return trace_print_printk_msg_only(iter
);
1806 if (trace_flags
& TRACE_ITER_BIN
)
1807 return print_bin_fmt(iter
);
1809 if (trace_flags
& TRACE_ITER_HEX
)
1810 return print_hex_fmt(iter
);
1812 if (trace_flags
& TRACE_ITER_RAW
)
1813 return print_raw_fmt(iter
);
1815 return print_trace_fmt(iter
);
1818 static int s_show(struct seq_file
*m
, void *v
)
1820 struct trace_iterator
*iter
= v
;
1822 if (iter
->ent
== NULL
) {
1824 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1827 if (iter
->trace
&& iter
->trace
->print_header
)
1828 iter
->trace
->print_header(m
);
1829 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1830 /* print nothing if the buffers are empty */
1831 if (trace_empty(iter
))
1833 print_trace_header(m
, iter
);
1834 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1835 print_lat_help_header(m
);
1837 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1838 print_func_help_header(m
);
1841 print_trace_line(iter
);
1842 trace_print_seq(m
, &iter
->seq
);
1848 static struct seq_operations tracer_seq_ops
= {
1855 static struct trace_iterator
*
1856 __tracing_open(struct inode
*inode
, struct file
*file
)
1858 long cpu_file
= (long) inode
->i_private
;
1859 void *fail_ret
= ERR_PTR(-ENOMEM
);
1860 struct trace_iterator
*iter
;
1864 if (tracing_disabled
)
1865 return ERR_PTR(-ENODEV
);
1867 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1869 return ERR_PTR(-ENOMEM
);
1872 * We make a copy of the current tracer to avoid concurrent
1873 * changes on it while we are reading.
1875 mutex_lock(&trace_types_lock
);
1876 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
1881 *iter
->trace
= *current_trace
;
1883 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
1886 cpumask_clear(iter
->started
);
1888 if (current_trace
&& current_trace
->print_max
)
1891 iter
->tr
= &global_trace
;
1893 mutex_init(&iter
->mutex
);
1894 iter
->cpu_file
= cpu_file
;
1896 /* Notify the tracer early; before we stop tracing. */
1897 if (iter
->trace
&& iter
->trace
->open
)
1898 iter
->trace
->open(iter
);
1900 /* Annotate start of buffers if we had overruns */
1901 if (ring_buffer_overruns(iter
->tr
->buffer
))
1902 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
1904 /* stop the trace while dumping */
1907 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
1908 for_each_tracing_cpu(cpu
) {
1910 iter
->buffer_iter
[cpu
] =
1911 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1912 tracing_iter_reset(iter
, cpu
);
1915 cpu
= iter
->cpu_file
;
1916 iter
->buffer_iter
[cpu
] =
1917 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1918 tracing_iter_reset(iter
, cpu
);
1921 ret
= seq_open(file
, &tracer_seq_ops
);
1923 fail_ret
= ERR_PTR(ret
);
1927 m
= file
->private_data
;
1930 mutex_unlock(&trace_types_lock
);
1935 for_each_tracing_cpu(cpu
) {
1936 if (iter
->buffer_iter
[cpu
])
1937 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1939 free_cpumask_var(iter
->started
);
1942 mutex_unlock(&trace_types_lock
);
1949 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1951 if (tracing_disabled
)
1954 filp
->private_data
= inode
->i_private
;
1958 static int tracing_release(struct inode
*inode
, struct file
*file
)
1960 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1961 struct trace_iterator
*iter
;
1964 if (!(file
->f_mode
& FMODE_READ
))
1969 mutex_lock(&trace_types_lock
);
1970 for_each_tracing_cpu(cpu
) {
1971 if (iter
->buffer_iter
[cpu
])
1972 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1975 if (iter
->trace
&& iter
->trace
->close
)
1976 iter
->trace
->close(iter
);
1978 /* reenable tracing if it was previously enabled */
1980 mutex_unlock(&trace_types_lock
);
1982 seq_release(inode
, file
);
1983 mutex_destroy(&iter
->mutex
);
1984 free_cpumask_var(iter
->started
);
1990 static int tracing_open(struct inode
*inode
, struct file
*file
)
1992 struct trace_iterator
*iter
;
1995 /* If this file was open for write, then erase contents */
1996 if ((file
->f_mode
& FMODE_WRITE
) &&
1997 (file
->f_flags
& O_TRUNC
)) {
1998 long cpu
= (long) inode
->i_private
;
2000 if (cpu
== TRACE_PIPE_ALL_CPU
)
2001 tracing_reset_online_cpus(&global_trace
);
2003 tracing_reset(&global_trace
, cpu
);
2006 if (file
->f_mode
& FMODE_READ
) {
2007 iter
= __tracing_open(inode
, file
);
2009 ret
= PTR_ERR(iter
);
2010 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2011 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2017 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2019 struct tracer
*t
= v
;
2029 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2034 mutex_lock(&trace_types_lock
);
2035 for (t
= trace_types
; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2041 static void t_stop(struct seq_file
*m
, void *p
)
2043 mutex_unlock(&trace_types_lock
);
2046 static int t_show(struct seq_file
*m
, void *v
)
2048 struct tracer
*t
= v
;
2053 seq_printf(m
, "%s", t
->name
);
2062 static struct seq_operations show_traces_seq_ops
= {
2069 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2071 if (tracing_disabled
)
2074 return seq_open(file
, &show_traces_seq_ops
);
2078 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
2079 size_t count
, loff_t
*ppos
)
2084 static const struct file_operations tracing_fops
= {
2085 .open
= tracing_open
,
2087 .write
= tracing_write_stub
,
2088 .llseek
= seq_lseek
,
2089 .release
= tracing_release
,
2092 static const struct file_operations show_traces_fops
= {
2093 .open
= show_traces_open
,
2095 .release
= seq_release
,
2099 * Only trace on a CPU if the bitmask is set:
2101 static cpumask_var_t tracing_cpumask
;
2104 * The tracer itself will not take this lock, but still we want
2105 * to provide a consistent cpumask to user-space:
2107 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2110 * Temporary storage for the character representation of the
2111 * CPU bitmask (and one more byte for the newline):
2113 static char mask_str
[NR_CPUS
+ 1];
2116 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2117 size_t count
, loff_t
*ppos
)
2121 mutex_lock(&tracing_cpumask_update_lock
);
2123 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2124 if (count
- len
< 2) {
2128 len
+= sprintf(mask_str
+ len
, "\n");
2129 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2132 mutex_unlock(&tracing_cpumask_update_lock
);
2138 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2139 size_t count
, loff_t
*ppos
)
2142 cpumask_var_t tracing_cpumask_new
;
2144 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2147 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2151 mutex_lock(&tracing_cpumask_update_lock
);
2153 local_irq_disable();
2154 __raw_spin_lock(&ftrace_max_lock
);
2155 for_each_tracing_cpu(cpu
) {
2157 * Increase/decrease the disabled counter if we are
2158 * about to flip a bit in the cpumask:
2160 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2161 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2162 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2164 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2165 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2166 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2169 __raw_spin_unlock(&ftrace_max_lock
);
2172 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2174 mutex_unlock(&tracing_cpumask_update_lock
);
2175 free_cpumask_var(tracing_cpumask_new
);
2180 free_cpumask_var(tracing_cpumask_new
);
2185 static const struct file_operations tracing_cpumask_fops
= {
2186 .open
= tracing_open_generic
,
2187 .read
= tracing_cpumask_read
,
2188 .write
= tracing_cpumask_write
,
2192 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2193 size_t cnt
, loff_t
*ppos
)
2195 struct tracer_opt
*trace_opts
;
2203 /* calculate max size */
2204 for (i
= 0; trace_options
[i
]; i
++) {
2205 len
+= strlen(trace_options
[i
]);
2206 len
+= 3; /* "no" and newline */
2209 mutex_lock(&trace_types_lock
);
2210 tracer_flags
= current_trace
->flags
->val
;
2211 trace_opts
= current_trace
->flags
->opts
;
2214 * Increase the size with names of options specific
2215 * of the current tracer.
2217 for (i
= 0; trace_opts
[i
].name
; i
++) {
2218 len
+= strlen(trace_opts
[i
].name
);
2219 len
+= 3; /* "no" and newline */
2223 buf
= kmalloc(len
+ 1, GFP_KERNEL
);
2225 mutex_unlock(&trace_types_lock
);
2229 for (i
= 0; trace_options
[i
]; i
++) {
2230 if (trace_flags
& (1 << i
))
2231 r
+= sprintf(buf
+ r
, "%s\n", trace_options
[i
]);
2233 r
+= sprintf(buf
+ r
, "no%s\n", trace_options
[i
]);
2236 for (i
= 0; trace_opts
[i
].name
; i
++) {
2237 if (tracer_flags
& trace_opts
[i
].bit
)
2238 r
+= sprintf(buf
+ r
, "%s\n",
2239 trace_opts
[i
].name
);
2241 r
+= sprintf(buf
+ r
, "no%s\n",
2242 trace_opts
[i
].name
);
2244 mutex_unlock(&trace_types_lock
);
2246 WARN_ON(r
>= len
+ 1);
2248 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2254 /* Try to assign a tracer specific option */
2255 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2257 struct tracer_flags
*tracer_flags
= trace
->flags
;
2258 struct tracer_opt
*opts
= NULL
;
2262 for (i
= 0; tracer_flags
->opts
[i
].name
; i
++) {
2263 opts
= &tracer_flags
->opts
[i
];
2264 len
= strlen(opts
->name
);
2266 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2267 ret
= trace
->set_flag(tracer_flags
->val
,
2273 if (!tracer_flags
->opts
[i
].name
)
2276 /* Refused to handle */
2281 tracer_flags
->val
&= ~opts
->bit
;
2283 tracer_flags
->val
|= opts
->bit
;
2288 static void set_tracer_flags(unsigned int mask
, int enabled
)
2290 /* do nothing if flag is already set */
2291 if (!!(trace_flags
& mask
) == !!enabled
)
2295 trace_flags
|= mask
;
2297 trace_flags
&= ~mask
;
2301 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2302 size_t cnt
, loff_t
*ppos
)
2310 if (cnt
>= sizeof(buf
))
2313 if (copy_from_user(&buf
, ubuf
, cnt
))
2318 if (strncmp(buf
, "no", 2) == 0) {
2323 for (i
= 0; trace_options
[i
]; i
++) {
2324 int len
= strlen(trace_options
[i
]);
2326 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2327 set_tracer_flags(1 << i
, !neg
);
2332 /* If no option could be set, test the specific tracer options */
2333 if (!trace_options
[i
]) {
2334 mutex_lock(&trace_types_lock
);
2335 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2336 mutex_unlock(&trace_types_lock
);
2346 static const struct file_operations tracing_iter_fops
= {
2347 .open
= tracing_open_generic
,
2348 .read
= tracing_trace_options_read
,
2349 .write
= tracing_trace_options_write
,
2352 static const char readme_msg
[] =
2353 "tracing mini-HOWTO:\n\n"
2354 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2355 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2356 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2357 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2359 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2360 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2362 "# cat /sys/kernel/debug/tracing/trace_options\n"
2363 "noprint-parent nosym-offset nosym-addr noverbose\n"
2364 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2365 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2366 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2367 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2371 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2372 size_t cnt
, loff_t
*ppos
)
2374 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2375 readme_msg
, strlen(readme_msg
));
2378 static const struct file_operations tracing_readme_fops
= {
2379 .open
= tracing_open_generic
,
2380 .read
= tracing_readme_read
,
2384 tracing_saved_cmdlines_read(struct file
*file
, char __user
*ubuf
,
2385 size_t cnt
, loff_t
*ppos
)
2394 file_buf
= kmalloc(SAVED_CMDLINES
*(16+TASK_COMM_LEN
), GFP_KERNEL
);
2398 buf_comm
= kmalloc(TASK_COMM_LEN
, GFP_KERNEL
);
2406 for (i
= 0; i
< SAVED_CMDLINES
; i
++) {
2409 pid
= map_cmdline_to_pid
[i
];
2410 if (pid
== -1 || pid
== NO_CMDLINE_MAP
)
2413 trace_find_cmdline(pid
, buf_comm
);
2414 r
= sprintf(buf
, "%d %s\n", pid
, buf_comm
);
2419 len
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
2428 static const struct file_operations tracing_saved_cmdlines_fops
= {
2429 .open
= tracing_open_generic
,
2430 .read
= tracing_saved_cmdlines_read
,
2434 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2435 size_t cnt
, loff_t
*ppos
)
2440 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2441 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2445 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2446 size_t cnt
, loff_t
*ppos
)
2448 struct trace_array
*tr
= filp
->private_data
;
2453 if (cnt
>= sizeof(buf
))
2456 if (copy_from_user(&buf
, ubuf
, cnt
))
2461 ret
= strict_strtoul(buf
, 10, &val
);
2467 mutex_lock(&trace_types_lock
);
2468 if (tracer_enabled
^ val
) {
2471 if (current_trace
->start
)
2472 current_trace
->start(tr
);
2477 if (current_trace
->stop
)
2478 current_trace
->stop(tr
);
2481 mutex_unlock(&trace_types_lock
);
2489 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2490 size_t cnt
, loff_t
*ppos
)
2492 char buf
[max_tracer_type_len
+2];
2495 mutex_lock(&trace_types_lock
);
2497 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2499 r
= sprintf(buf
, "\n");
2500 mutex_unlock(&trace_types_lock
);
2502 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2505 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2507 tracing_reset_online_cpus(tr
);
2511 static int tracing_resize_ring_buffer(unsigned long size
)
2516 * If kernel or user changes the size of the ring buffer
2517 * we use the size that was given, and we can forget about
2518 * expanding it later.
2520 ring_buffer_expanded
= 1;
2522 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2526 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2530 r
= ring_buffer_resize(global_trace
.buffer
,
2531 global_trace
.entries
);
2534 * AARGH! We are left with different
2535 * size max buffer!!!!
2536 * The max buffer is our "snapshot" buffer.
2537 * When a tracer needs a snapshot (one of the
2538 * latency tracers), it swaps the max buffer
2539 * with the saved snap shot. We succeeded to
2540 * update the size of the main buffer, but failed to
2541 * update the size of the max buffer. But when we tried
2542 * to reset the main buffer to the original size, we
2543 * failed there too. This is very unlikely to
2544 * happen, but if it does, warn and kill all
2548 tracing_disabled
= 1;
2553 global_trace
.entries
= size
;
2559 * tracing_update_buffers - used by tracing facility to expand ring buffers
2561 * To save on memory when the tracing is never used on a system with it
2562 * configured in. The ring buffers are set to a minimum size. But once
2563 * a user starts to use the tracing facility, then they need to grow
2564 * to their default size.
2566 * This function is to be called when a tracer is about to be used.
2568 int tracing_update_buffers(void)
2572 mutex_lock(&trace_types_lock
);
2573 if (!ring_buffer_expanded
)
2574 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2575 mutex_unlock(&trace_types_lock
);
2580 struct trace_option_dentry
;
2582 static struct trace_option_dentry
*
2583 create_trace_option_files(struct tracer
*tracer
);
2586 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2588 static int tracing_set_tracer(const char *buf
)
2590 static struct trace_option_dentry
*topts
;
2591 struct trace_array
*tr
= &global_trace
;
2595 mutex_lock(&trace_types_lock
);
2597 if (!ring_buffer_expanded
) {
2598 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2604 for (t
= trace_types
; t
; t
= t
->next
) {
2605 if (strcmp(t
->name
, buf
) == 0)
2612 if (t
== current_trace
)
2615 trace_branch_disable();
2616 if (current_trace
&& current_trace
->reset
)
2617 current_trace
->reset(tr
);
2619 destroy_trace_option_files(topts
);
2623 topts
= create_trace_option_files(current_trace
);
2626 ret
= tracer_init(t
, tr
);
2631 trace_branch_enable(tr
);
2633 mutex_unlock(&trace_types_lock
);
2639 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2640 size_t cnt
, loff_t
*ppos
)
2642 char buf
[max_tracer_type_len
+1];
2649 if (cnt
> max_tracer_type_len
)
2650 cnt
= max_tracer_type_len
;
2652 if (copy_from_user(&buf
, ubuf
, cnt
))
2657 /* strip ending whitespace. */
2658 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2661 err
= tracing_set_tracer(buf
);
2671 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2672 size_t cnt
, loff_t
*ppos
)
2674 unsigned long *ptr
= filp
->private_data
;
2678 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2679 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2680 if (r
> sizeof(buf
))
2682 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2686 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2687 size_t cnt
, loff_t
*ppos
)
2689 unsigned long *ptr
= filp
->private_data
;
2694 if (cnt
>= sizeof(buf
))
2697 if (copy_from_user(&buf
, ubuf
, cnt
))
2702 ret
= strict_strtoul(buf
, 10, &val
);
2711 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2713 long cpu_file
= (long) inode
->i_private
;
2714 struct trace_iterator
*iter
;
2717 if (tracing_disabled
)
2720 mutex_lock(&trace_types_lock
);
2722 /* We only allow one reader per cpu */
2723 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
2724 if (!cpumask_empty(tracing_reader_cpumask
)) {
2728 cpumask_setall(tracing_reader_cpumask
);
2730 if (!cpumask_test_cpu(cpu_file
, tracing_reader_cpumask
))
2731 cpumask_set_cpu(cpu_file
, tracing_reader_cpumask
);
2738 /* create a buffer to store the information to pass to userspace */
2739 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2746 * We make a copy of the current tracer to avoid concurrent
2747 * changes on it while we are reading.
2749 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2755 *iter
->trace
= *current_trace
;
2757 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2762 /* trace pipe does not show start of buffer */
2763 cpumask_setall(iter
->started
);
2765 if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2766 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2768 iter
->cpu_file
= cpu_file
;
2769 iter
->tr
= &global_trace
;
2770 mutex_init(&iter
->mutex
);
2771 filp
->private_data
= iter
;
2773 if (iter
->trace
->pipe_open
)
2774 iter
->trace
->pipe_open(iter
);
2777 mutex_unlock(&trace_types_lock
);
2783 mutex_unlock(&trace_types_lock
);
2787 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2789 struct trace_iterator
*iter
= file
->private_data
;
2791 mutex_lock(&trace_types_lock
);
2793 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
)
2794 cpumask_clear(tracing_reader_cpumask
);
2796 cpumask_clear_cpu(iter
->cpu_file
, tracing_reader_cpumask
);
2798 mutex_unlock(&trace_types_lock
);
2800 free_cpumask_var(iter
->started
);
2801 mutex_destroy(&iter
->mutex
);
2809 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2811 struct trace_iterator
*iter
= filp
->private_data
;
2813 if (trace_flags
& TRACE_ITER_BLOCK
) {
2815 * Always select as readable when in blocking mode
2817 return POLLIN
| POLLRDNORM
;
2819 if (!trace_empty(iter
))
2820 return POLLIN
| POLLRDNORM
;
2821 poll_wait(filp
, &trace_wait
, poll_table
);
2822 if (!trace_empty(iter
))
2823 return POLLIN
| POLLRDNORM
;
2830 void default_wait_pipe(struct trace_iterator
*iter
)
2834 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
2836 if (trace_empty(iter
))
2839 finish_wait(&trace_wait
, &wait
);
2843 * This is a make-shift waitqueue.
2844 * A tracer might use this callback on some rare cases:
2846 * 1) the current tracer might hold the runqueue lock when it wakes up
2847 * a reader, hence a deadlock (sched, function, and function graph tracers)
2848 * 2) the function tracers, trace all functions, we don't want
2849 * the overhead of calling wake_up and friends
2850 * (and tracing them too)
2852 * Anyway, this is really very primitive wakeup.
2854 void poll_wait_pipe(struct trace_iterator
*iter
)
2856 set_current_state(TASK_INTERRUPTIBLE
);
2857 /* sleep for 100 msecs, and try again. */
2858 schedule_timeout(HZ
/ 10);
2861 /* Must be called with trace_types_lock mutex held. */
2862 static int tracing_wait_pipe(struct file
*filp
)
2864 struct trace_iterator
*iter
= filp
->private_data
;
2866 while (trace_empty(iter
)) {
2868 if ((filp
->f_flags
& O_NONBLOCK
)) {
2872 mutex_unlock(&iter
->mutex
);
2874 iter
->trace
->wait_pipe(iter
);
2876 mutex_lock(&iter
->mutex
);
2878 if (signal_pending(current
))
2882 * We block until we read something and tracing is disabled.
2883 * We still block if tracing is disabled, but we have never
2884 * read anything. This allows a user to cat this file, and
2885 * then enable tracing. But after we have read something,
2886 * we give an EOF when tracing is again disabled.
2888 * iter->pos will be 0 if we haven't read anything.
2890 if (!tracer_enabled
&& iter
->pos
)
2901 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2902 size_t cnt
, loff_t
*ppos
)
2904 struct trace_iterator
*iter
= filp
->private_data
;
2905 static struct tracer
*old_tracer
;
2908 /* return any leftover data */
2909 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2913 trace_seq_init(&iter
->seq
);
2915 /* copy the tracer to avoid using a global lock all around */
2916 mutex_lock(&trace_types_lock
);
2917 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
2918 old_tracer
= current_trace
;
2919 *iter
->trace
= *current_trace
;
2921 mutex_unlock(&trace_types_lock
);
2924 * Avoid more than one consumer on a single file descriptor
2925 * This is just a matter of traces coherency, the ring buffer itself
2928 mutex_lock(&iter
->mutex
);
2929 if (iter
->trace
->read
) {
2930 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2936 sret
= tracing_wait_pipe(filp
);
2940 /* stop when tracing is finished */
2941 if (trace_empty(iter
)) {
2946 if (cnt
>= PAGE_SIZE
)
2947 cnt
= PAGE_SIZE
- 1;
2949 /* reset all but tr, trace, and overruns */
2950 memset(&iter
->seq
, 0,
2951 sizeof(struct trace_iterator
) -
2952 offsetof(struct trace_iterator
, seq
));
2955 trace_event_read_lock();
2956 while (find_next_entry_inc(iter
) != NULL
) {
2957 enum print_line_t ret
;
2958 int len
= iter
->seq
.len
;
2960 ret
= print_trace_line(iter
);
2961 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2962 /* don't print partial lines */
2963 iter
->seq
.len
= len
;
2966 if (ret
!= TRACE_TYPE_NO_CONSUME
)
2967 trace_consume(iter
);
2969 if (iter
->seq
.len
>= cnt
)
2972 trace_event_read_unlock();
2974 /* Now copy what we have to the user */
2975 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2976 if (iter
->seq
.readpos
>= iter
->seq
.len
)
2977 trace_seq_init(&iter
->seq
);
2980 * If there was nothing to send to user, inspite of consuming trace
2981 * entries, go back to wait for more entries.
2987 mutex_unlock(&iter
->mutex
);
2992 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
2993 struct pipe_buffer
*buf
)
2995 __free_page(buf
->page
);
2998 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
3001 __free_page(spd
->pages
[idx
]);
3004 static struct pipe_buf_operations tracing_pipe_buf_ops
= {
3006 .map
= generic_pipe_buf_map
,
3007 .unmap
= generic_pipe_buf_unmap
,
3008 .confirm
= generic_pipe_buf_confirm
,
3009 .release
= tracing_pipe_buf_release
,
3010 .steal
= generic_pipe_buf_steal
,
3011 .get
= generic_pipe_buf_get
,
3015 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
3020 /* Seq buffer is page-sized, exactly what we need. */
3022 count
= iter
->seq
.len
;
3023 ret
= print_trace_line(iter
);
3024 count
= iter
->seq
.len
- count
;
3027 iter
->seq
.len
-= count
;
3030 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3031 iter
->seq
.len
-= count
;
3035 if (ret
!= TRACE_TYPE_NO_CONSUME
)
3036 trace_consume(iter
);
3038 if (!find_next_entry_inc(iter
)) {
3048 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
3050 struct pipe_inode_info
*pipe
,
3054 struct page
*pages
[PIPE_BUFFERS
];
3055 struct partial_page partial
[PIPE_BUFFERS
];
3056 struct trace_iterator
*iter
= filp
->private_data
;
3057 struct splice_pipe_desc spd
= {
3060 .nr_pages
= 0, /* This gets updated below. */
3062 .ops
= &tracing_pipe_buf_ops
,
3063 .spd_release
= tracing_spd_release_pipe
,
3065 static struct tracer
*old_tracer
;
3070 /* copy the tracer to avoid using a global lock all around */
3071 mutex_lock(&trace_types_lock
);
3072 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
3073 old_tracer
= current_trace
;
3074 *iter
->trace
= *current_trace
;
3076 mutex_unlock(&trace_types_lock
);
3078 mutex_lock(&iter
->mutex
);
3080 if (iter
->trace
->splice_read
) {
3081 ret
= iter
->trace
->splice_read(iter
, filp
,
3082 ppos
, pipe
, len
, flags
);
3087 ret
= tracing_wait_pipe(filp
);
3091 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
3096 trace_event_read_lock();
3098 /* Fill as many pages as possible. */
3099 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
3100 pages
[i
] = alloc_page(GFP_KERNEL
);
3104 rem
= tracing_fill_pipe_page(rem
, iter
);
3106 /* Copy the data into the page, so we can start over. */
3107 ret
= trace_seq_to_buffer(&iter
->seq
,
3108 page_address(pages
[i
]),
3111 __free_page(pages
[i
]);
3114 partial
[i
].offset
= 0;
3115 partial
[i
].len
= iter
->seq
.len
;
3117 trace_seq_init(&iter
->seq
);
3120 trace_event_read_unlock();
3121 mutex_unlock(&iter
->mutex
);
3125 return splice_to_pipe(pipe
, &spd
);
3128 mutex_unlock(&iter
->mutex
);
3134 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3135 size_t cnt
, loff_t
*ppos
)
3137 struct trace_array
*tr
= filp
->private_data
;
3141 mutex_lock(&trace_types_lock
);
3142 if (!ring_buffer_expanded
)
3143 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3145 trace_buf_size
>> 10);
3147 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3148 mutex_unlock(&trace_types_lock
);
3150 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3154 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3155 size_t cnt
, loff_t
*ppos
)
3161 if (cnt
>= sizeof(buf
))
3164 if (copy_from_user(&buf
, ubuf
, cnt
))
3169 ret
= strict_strtoul(buf
, 10, &val
);
3173 /* must have at least 1 entry */
3177 mutex_lock(&trace_types_lock
);
3181 /* disable all cpu buffers */
3182 for_each_tracing_cpu(cpu
) {
3183 if (global_trace
.data
[cpu
])
3184 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3185 if (max_tr
.data
[cpu
])
3186 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3189 /* value is in KB */
3192 if (val
!= global_trace
.entries
) {
3193 ret
= tracing_resize_ring_buffer(val
);
3202 /* If check pages failed, return ENOMEM */
3203 if (tracing_disabled
)
3206 for_each_tracing_cpu(cpu
) {
3207 if (global_trace
.data
[cpu
])
3208 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3209 if (max_tr
.data
[cpu
])
3210 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3214 max_tr
.entries
= global_trace
.entries
;
3215 mutex_unlock(&trace_types_lock
);
3220 static int mark_printk(const char *fmt
, ...)
3224 va_start(args
, fmt
);
3225 ret
= trace_vprintk(0, fmt
, args
);
3231 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3232 size_t cnt
, loff_t
*fpos
)
3237 if (tracing_disabled
)
3240 if (cnt
> TRACE_BUF_SIZE
)
3241 cnt
= TRACE_BUF_SIZE
;
3243 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3247 if (copy_from_user(buf
, ubuf
, cnt
)) {
3252 /* Cut from the first nil or newline. */
3254 end
= strchr(buf
, '\n');
3258 cnt
= mark_printk("%s\n", buf
);
3265 static ssize_t
tracing_clock_read(struct file
*filp
, char __user
*ubuf
,
3266 size_t cnt
, loff_t
*ppos
)
3272 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++)
3273 bufiter
+= snprintf(buf
+ bufiter
, sizeof(buf
) - bufiter
,
3274 "%s%s%s%s", i
? " " : "",
3275 i
== trace_clock_id
? "[" : "", trace_clocks
[i
].name
,
3276 i
== trace_clock_id
? "]" : "");
3277 bufiter
+= snprintf(buf
+ bufiter
, sizeof(buf
) - bufiter
, "\n");
3279 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, bufiter
);
3282 static ssize_t
tracing_clock_write(struct file
*filp
, const char __user
*ubuf
,
3283 size_t cnt
, loff_t
*fpos
)
3286 const char *clockstr
;
3289 if (cnt
>= sizeof(buf
))
3292 if (copy_from_user(&buf
, ubuf
, cnt
))
3297 clockstr
= strstrip(buf
);
3299 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++) {
3300 if (strcmp(trace_clocks
[i
].name
, clockstr
) == 0)
3303 if (i
== ARRAY_SIZE(trace_clocks
))
3308 mutex_lock(&trace_types_lock
);
3310 ring_buffer_set_clock(global_trace
.buffer
, trace_clocks
[i
].func
);
3312 ring_buffer_set_clock(max_tr
.buffer
, trace_clocks
[i
].func
);
3314 mutex_unlock(&trace_types_lock
);
3321 static const struct file_operations tracing_max_lat_fops
= {
3322 .open
= tracing_open_generic
,
3323 .read
= tracing_max_lat_read
,
3324 .write
= tracing_max_lat_write
,
3327 static const struct file_operations tracing_ctrl_fops
= {
3328 .open
= tracing_open_generic
,
3329 .read
= tracing_ctrl_read
,
3330 .write
= tracing_ctrl_write
,
3333 static const struct file_operations set_tracer_fops
= {
3334 .open
= tracing_open_generic
,
3335 .read
= tracing_set_trace_read
,
3336 .write
= tracing_set_trace_write
,
3339 static const struct file_operations tracing_pipe_fops
= {
3340 .open
= tracing_open_pipe
,
3341 .poll
= tracing_poll_pipe
,
3342 .read
= tracing_read_pipe
,
3343 .splice_read
= tracing_splice_read_pipe
,
3344 .release
= tracing_release_pipe
,
3347 static const struct file_operations tracing_entries_fops
= {
3348 .open
= tracing_open_generic
,
3349 .read
= tracing_entries_read
,
3350 .write
= tracing_entries_write
,
3353 static const struct file_operations tracing_mark_fops
= {
3354 .open
= tracing_open_generic
,
3355 .write
= tracing_mark_write
,
3358 static const struct file_operations trace_clock_fops
= {
3359 .open
= tracing_open_generic
,
3360 .read
= tracing_clock_read
,
3361 .write
= tracing_clock_write
,
3364 struct ftrace_buffer_info
{
3365 struct trace_array
*tr
;
3371 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3373 int cpu
= (int)(long)inode
->i_private
;
3374 struct ftrace_buffer_info
*info
;
3376 if (tracing_disabled
)
3379 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3383 info
->tr
= &global_trace
;
3386 /* Force reading ring buffer for first read */
3387 info
->read
= (unsigned int)-1;
3389 filp
->private_data
= info
;
3391 return nonseekable_open(inode
, filp
);
3395 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3396 size_t count
, loff_t
*ppos
)
3398 struct ftrace_buffer_info
*info
= filp
->private_data
;
3407 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3411 /* Do we have previous read data to read? */
3412 if (info
->read
< PAGE_SIZE
)
3417 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3424 pos
= ring_buffer_page_len(info
->spare
);
3426 if (pos
< PAGE_SIZE
)
3427 memset(info
->spare
+ pos
, 0, PAGE_SIZE
- pos
);
3430 size
= PAGE_SIZE
- info
->read
;
3434 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3445 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3447 struct ftrace_buffer_info
*info
= file
->private_data
;
3450 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3457 struct ring_buffer
*buffer
;
3462 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3463 struct pipe_buffer
*buf
)
3465 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3470 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3475 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3476 struct pipe_buffer
*buf
)
3481 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3482 struct pipe_buffer
*buf
)
3484 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3489 /* Pipe buffer operations for a buffer. */
3490 static struct pipe_buf_operations buffer_pipe_buf_ops
= {
3492 .map
= generic_pipe_buf_map
,
3493 .unmap
= generic_pipe_buf_unmap
,
3494 .confirm
= generic_pipe_buf_confirm
,
3495 .release
= buffer_pipe_buf_release
,
3496 .steal
= buffer_pipe_buf_steal
,
3497 .get
= buffer_pipe_buf_get
,
3501 * Callback from splice_to_pipe(), if we need to release some pages
3502 * at the end of the spd in case we error'ed out in filling the pipe.
3504 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3506 struct buffer_ref
*ref
=
3507 (struct buffer_ref
*)spd
->partial
[i
].private;
3512 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3514 spd
->partial
[i
].private = 0;
3518 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3519 struct pipe_inode_info
*pipe
, size_t len
,
3522 struct ftrace_buffer_info
*info
= file
->private_data
;
3523 struct partial_page partial
[PIPE_BUFFERS
];
3524 struct page
*pages
[PIPE_BUFFERS
];
3525 struct splice_pipe_desc spd
= {
3529 .ops
= &buffer_pipe_buf_ops
,
3530 .spd_release
= buffer_spd_release
,
3532 struct buffer_ref
*ref
;
3533 int entries
, size
, i
;
3536 if (*ppos
& (PAGE_SIZE
- 1)) {
3537 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3541 if (len
& (PAGE_SIZE
- 1)) {
3542 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3543 if (len
< PAGE_SIZE
)
3548 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3550 for (i
= 0; i
< PIPE_BUFFERS
&& len
&& entries
; i
++, len
-= PAGE_SIZE
) {
3554 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3559 ref
->buffer
= info
->tr
->buffer
;
3560 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3566 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3569 ring_buffer_free_read_page(ref
->buffer
,
3576 * zero out any left over data, this is going to
3579 size
= ring_buffer_page_len(ref
->page
);
3580 if (size
< PAGE_SIZE
)
3581 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3583 page
= virt_to_page(ref
->page
);
3585 spd
.pages
[i
] = page
;
3586 spd
.partial
[i
].len
= PAGE_SIZE
;
3587 spd
.partial
[i
].offset
= 0;
3588 spd
.partial
[i
].private = (unsigned long)ref
;
3592 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3597 /* did we read anything? */
3598 if (!spd
.nr_pages
) {
3599 if (flags
& SPLICE_F_NONBLOCK
)
3607 ret
= splice_to_pipe(pipe
, &spd
);
3612 static const struct file_operations tracing_buffers_fops
= {
3613 .open
= tracing_buffers_open
,
3614 .read
= tracing_buffers_read
,
3615 .release
= tracing_buffers_release
,
3616 .splice_read
= tracing_buffers_splice_read
,
3617 .llseek
= no_llseek
,
3621 tracing_stats_read(struct file
*filp
, char __user
*ubuf
,
3622 size_t count
, loff_t
*ppos
)
3624 unsigned long cpu
= (unsigned long)filp
->private_data
;
3625 struct trace_array
*tr
= &global_trace
;
3626 struct trace_seq
*s
;
3629 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
3635 cnt
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
3636 trace_seq_printf(s
, "entries: %ld\n", cnt
);
3638 cnt
= ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
3639 trace_seq_printf(s
, "overrun: %ld\n", cnt
);
3641 cnt
= ring_buffer_commit_overrun_cpu(tr
->buffer
, cpu
);
3642 trace_seq_printf(s
, "commit overrun: %ld\n", cnt
);
3644 count
= simple_read_from_buffer(ubuf
, count
, ppos
, s
->buffer
, s
->len
);
3651 static const struct file_operations tracing_stats_fops
= {
3652 .open
= tracing_open_generic
,
3653 .read
= tracing_stats_read
,
3656 #ifdef CONFIG_DYNAMIC_FTRACE
3658 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3664 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3665 size_t cnt
, loff_t
*ppos
)
3667 static char ftrace_dyn_info_buffer
[1024];
3668 static DEFINE_MUTEX(dyn_info_mutex
);
3669 unsigned long *p
= filp
->private_data
;
3670 char *buf
= ftrace_dyn_info_buffer
;
3671 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3674 mutex_lock(&dyn_info_mutex
);
3675 r
= sprintf(buf
, "%ld ", *p
);
3677 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3680 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3682 mutex_unlock(&dyn_info_mutex
);
3687 static const struct file_operations tracing_dyn_info_fops
= {
3688 .open
= tracing_open_generic
,
3689 .read
= tracing_read_dyn_info
,
3693 static struct dentry
*d_tracer
;
3695 struct dentry
*tracing_init_dentry(void)
3702 if (!debugfs_initialized())
3705 d_tracer
= debugfs_create_dir("tracing", NULL
);
3707 if (!d_tracer
&& !once
) {
3709 pr_warning("Could not create debugfs directory 'tracing'\n");
3716 static struct dentry
*d_percpu
;
3718 struct dentry
*tracing_dentry_percpu(void)
3721 struct dentry
*d_tracer
;
3726 d_tracer
= tracing_init_dentry();
3731 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
3733 if (!d_percpu
&& !once
) {
3735 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3742 static void tracing_init_debugfs_percpu(long cpu
)
3744 struct dentry
*d_percpu
= tracing_dentry_percpu();
3745 struct dentry
*d_cpu
;
3746 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3749 if (cpu
> 999 || cpu
< 0)
3752 sprintf(cpu_dir
, "cpu%ld", cpu
);
3753 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
3755 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
3759 /* per cpu trace_pipe */
3760 trace_create_file("trace_pipe", 0444, d_cpu
,
3761 (void *) cpu
, &tracing_pipe_fops
);
3764 trace_create_file("trace", 0644, d_cpu
,
3765 (void *) cpu
, &tracing_fops
);
3767 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
3768 (void *) cpu
, &tracing_buffers_fops
);
3770 trace_create_file("stats", 0444, d_cpu
,
3771 (void *) cpu
, &tracing_stats_fops
);
3774 #ifdef CONFIG_FTRACE_SELFTEST
3775 /* Let selftest have access to static functions in this file */
3776 #include "trace_selftest.c"
3779 struct trace_option_dentry
{
3780 struct tracer_opt
*opt
;
3781 struct tracer_flags
*flags
;
3782 struct dentry
*entry
;
3786 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3789 struct trace_option_dentry
*topt
= filp
->private_data
;
3792 if (topt
->flags
->val
& topt
->opt
->bit
)
3797 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3801 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3804 struct trace_option_dentry
*topt
= filp
->private_data
;
3809 if (cnt
>= sizeof(buf
))
3812 if (copy_from_user(&buf
, ubuf
, cnt
))
3817 ret
= strict_strtoul(buf
, 10, &val
);
3824 /* do nothing if already cleared */
3825 if (!(topt
->flags
->val
& topt
->opt
->bit
))
3828 mutex_lock(&trace_types_lock
);
3829 if (current_trace
->set_flag
)
3830 ret
= current_trace
->set_flag(topt
->flags
->val
,
3832 mutex_unlock(&trace_types_lock
);
3835 topt
->flags
->val
&= ~topt
->opt
->bit
;
3838 /* do nothing if already set */
3839 if (topt
->flags
->val
& topt
->opt
->bit
)
3842 mutex_lock(&trace_types_lock
);
3843 if (current_trace
->set_flag
)
3844 ret
= current_trace
->set_flag(topt
->flags
->val
,
3846 mutex_unlock(&trace_types_lock
);
3849 topt
->flags
->val
|= topt
->opt
->bit
;
3862 static const struct file_operations trace_options_fops
= {
3863 .open
= tracing_open_generic
,
3864 .read
= trace_options_read
,
3865 .write
= trace_options_write
,
3869 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3872 long index
= (long)filp
->private_data
;
3875 if (trace_flags
& (1 << index
))
3880 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3884 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3887 long index
= (long)filp
->private_data
;
3892 if (cnt
>= sizeof(buf
))
3895 if (copy_from_user(&buf
, ubuf
, cnt
))
3900 ret
= strict_strtoul(buf
, 10, &val
);
3904 if (val
!= 0 && val
!= 1)
3906 set_tracer_flags(1 << index
, val
);
3913 static const struct file_operations trace_options_core_fops
= {
3914 .open
= tracing_open_generic
,
3915 .read
= trace_options_core_read
,
3916 .write
= trace_options_core_write
,
3919 struct dentry
*trace_create_file(const char *name
,
3921 struct dentry
*parent
,
3923 const struct file_operations
*fops
)
3927 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
3929 pr_warning("Could not create debugfs '%s' entry\n", name
);
3935 static struct dentry
*trace_options_init_dentry(void)
3937 struct dentry
*d_tracer
;
3938 static struct dentry
*t_options
;
3943 d_tracer
= tracing_init_dentry();
3947 t_options
= debugfs_create_dir("options", d_tracer
);
3949 pr_warning("Could not create debugfs directory 'options'\n");
3957 create_trace_option_file(struct trace_option_dentry
*topt
,
3958 struct tracer_flags
*flags
,
3959 struct tracer_opt
*opt
)
3961 struct dentry
*t_options
;
3963 t_options
= trace_options_init_dentry();
3967 topt
->flags
= flags
;
3970 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
3971 &trace_options_fops
);
3975 static struct trace_option_dentry
*
3976 create_trace_option_files(struct tracer
*tracer
)
3978 struct trace_option_dentry
*topts
;
3979 struct tracer_flags
*flags
;
3980 struct tracer_opt
*opts
;
3986 flags
= tracer
->flags
;
3988 if (!flags
|| !flags
->opts
)
3993 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3996 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
4000 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
4001 create_trace_option_file(&topts
[cnt
], flags
,
4008 destroy_trace_option_files(struct trace_option_dentry
*topts
)
4015 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
4016 if (topts
[cnt
].entry
)
4017 debugfs_remove(topts
[cnt
].entry
);
4023 static struct dentry
*
4024 create_trace_option_core_file(const char *option
, long index
)
4026 struct dentry
*t_options
;
4028 t_options
= trace_options_init_dentry();
4032 return trace_create_file(option
, 0644, t_options
, (void *)index
,
4033 &trace_options_core_fops
);
4036 static __init
void create_trace_options_dir(void)
4038 struct dentry
*t_options
;
4041 t_options
= trace_options_init_dentry();
4045 for (i
= 0; trace_options
[i
]; i
++)
4046 create_trace_option_core_file(trace_options
[i
], i
);
4049 static __init
int tracer_init_debugfs(void)
4051 struct dentry
*d_tracer
;
4054 d_tracer
= tracing_init_dentry();
4056 trace_create_file("tracing_enabled", 0644, d_tracer
,
4057 &global_trace
, &tracing_ctrl_fops
);
4059 trace_create_file("trace_options", 0644, d_tracer
,
4060 NULL
, &tracing_iter_fops
);
4062 trace_create_file("tracing_cpumask", 0644, d_tracer
,
4063 NULL
, &tracing_cpumask_fops
);
4065 trace_create_file("trace", 0644, d_tracer
,
4066 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
4068 trace_create_file("available_tracers", 0444, d_tracer
,
4069 &global_trace
, &show_traces_fops
);
4071 trace_create_file("current_tracer", 0644, d_tracer
,
4072 &global_trace
, &set_tracer_fops
);
4074 #ifdef CONFIG_TRACER_MAX_TRACE
4075 trace_create_file("tracing_max_latency", 0644, d_tracer
,
4076 &tracing_max_latency
, &tracing_max_lat_fops
);
4078 trace_create_file("tracing_thresh", 0644, d_tracer
,
4079 &tracing_thresh
, &tracing_max_lat_fops
);
4082 trace_create_file("README", 0444, d_tracer
,
4083 NULL
, &tracing_readme_fops
);
4085 trace_create_file("trace_pipe", 0444, d_tracer
,
4086 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
4088 trace_create_file("buffer_size_kb", 0644, d_tracer
,
4089 &global_trace
, &tracing_entries_fops
);
4091 trace_create_file("trace_marker", 0220, d_tracer
,
4092 NULL
, &tracing_mark_fops
);
4094 trace_create_file("saved_cmdlines", 0444, d_tracer
,
4095 NULL
, &tracing_saved_cmdlines_fops
);
4097 trace_create_file("trace_clock", 0644, d_tracer
, NULL
,
4100 #ifdef CONFIG_DYNAMIC_FTRACE
4101 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
4102 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
4104 #ifdef CONFIG_SYSPROF_TRACER
4105 init_tracer_sysprof_debugfs(d_tracer
);
4108 create_trace_options_dir();
4110 for_each_tracing_cpu(cpu
)
4111 tracing_init_debugfs_percpu(cpu
);
4116 static int trace_panic_handler(struct notifier_block
*this,
4117 unsigned long event
, void *unused
)
4119 if (ftrace_dump_on_oops
)
4124 static struct notifier_block trace_panic_notifier
= {
4125 .notifier_call
= trace_panic_handler
,
4127 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
4130 static int trace_die_handler(struct notifier_block
*self
,
4136 if (ftrace_dump_on_oops
)
4145 static struct notifier_block trace_die_notifier
= {
4146 .notifier_call
= trace_die_handler
,
4151 * printk is set to max of 1024, we really don't need it that big.
4152 * Nothing should be printing 1000 characters anyway.
4154 #define TRACE_MAX_PRINT 1000
4157 * Define here KERN_TRACE so that we have one place to modify
4158 * it if we decide to change what log level the ftrace dump
4161 #define KERN_TRACE KERN_EMERG
4164 trace_printk_seq(struct trace_seq
*s
)
4166 /* Probably should print a warning here. */
4170 /* should be zero ended, but we are paranoid. */
4171 s
->buffer
[s
->len
] = 0;
4173 printk(KERN_TRACE
"%s", s
->buffer
);
4178 static void __ftrace_dump(bool disable_tracing
)
4180 static raw_spinlock_t ftrace_dump_lock
=
4181 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
4182 /* use static because iter can be a bit big for the stack */
4183 static struct trace_iterator iter
;
4184 unsigned int old_userobj
;
4185 static int dump_ran
;
4186 unsigned long flags
;
4190 local_irq_save(flags
);
4191 __raw_spin_lock(&ftrace_dump_lock
);
4199 if (disable_tracing
)
4202 for_each_tracing_cpu(cpu
) {
4203 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
4206 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
4208 /* don't look at user memory in panic mode */
4209 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4211 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4213 /* Simulate the iterator */
4214 iter
.tr
= &global_trace
;
4215 iter
.trace
= current_trace
;
4216 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4219 * We need to stop all tracing on all CPUS to read the
4220 * the next buffer. This is a bit expensive, but is
4221 * not done often. We fill all what we can read,
4222 * and then release the locks again.
4225 while (!trace_empty(&iter
)) {
4228 printk(KERN_TRACE
"---------------------------------\n");
4232 /* reset all but tr, trace, and overruns */
4233 memset(&iter
.seq
, 0,
4234 sizeof(struct trace_iterator
) -
4235 offsetof(struct trace_iterator
, seq
));
4236 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4239 if (find_next_entry_inc(&iter
) != NULL
) {
4242 ret
= print_trace_line(&iter
);
4243 if (ret
!= TRACE_TYPE_NO_CONSUME
)
4244 trace_consume(&iter
);
4247 trace_printk_seq(&iter
.seq
);
4251 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4253 printk(KERN_TRACE
"---------------------------------\n");
4255 /* Re-enable tracing if requested */
4256 if (!disable_tracing
) {
4257 trace_flags
|= old_userobj
;
4259 for_each_tracing_cpu(cpu
) {
4260 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
4266 __raw_spin_unlock(&ftrace_dump_lock
);
4267 local_irq_restore(flags
);
4270 /* By default: disable tracing after the dump */
4271 void ftrace_dump(void)
4273 __ftrace_dump(true);
4276 __init
static int tracer_alloc_buffers(void)
4282 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4285 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4286 goto out_free_buffer_mask
;
4288 if (!alloc_cpumask_var(&tracing_reader_cpumask
, GFP_KERNEL
))
4289 goto out_free_tracing_cpumask
;
4291 /* To save memory, keep the ring buffer size to its minimum */
4292 if (ring_buffer_expanded
)
4293 ring_buf_size
= trace_buf_size
;
4297 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4298 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4299 cpumask_clear(tracing_reader_cpumask
);
4301 /* TODO: make the number of buffers hot pluggable with CPUS */
4302 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
,
4303 TRACE_BUFFER_FLAGS
);
4304 if (!global_trace
.buffer
) {
4305 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4307 goto out_free_cpumask
;
4309 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4312 #ifdef CONFIG_TRACER_MAX_TRACE
4313 max_tr
.buffer
= ring_buffer_alloc(ring_buf_size
,
4314 TRACE_BUFFER_FLAGS
);
4315 if (!max_tr
.buffer
) {
4316 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4318 ring_buffer_free(global_trace
.buffer
);
4319 goto out_free_cpumask
;
4321 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
4322 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
4325 /* Allocate the first page for all buffers */
4326 for_each_tracing_cpu(i
) {
4327 global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4328 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
4331 trace_init_cmdlines();
4333 register_tracer(&nop_trace
);
4334 current_trace
= &nop_trace
;
4335 #ifdef CONFIG_BOOT_TRACER
4336 register_tracer(&boot_tracer
);
4338 /* All seems OK, enable tracing */
4339 tracing_disabled
= 0;
4341 atomic_notifier_chain_register(&panic_notifier_list
,
4342 &trace_panic_notifier
);
4344 register_die_notifier(&trace_die_notifier
);
4349 free_cpumask_var(tracing_reader_cpumask
);
4350 out_free_tracing_cpumask
:
4351 free_cpumask_var(tracing_cpumask
);
4352 out_free_buffer_mask
:
4353 free_cpumask_var(tracing_buffer_mask
);
4358 __init
static int clear_boot_tracer(void)
4361 * The default tracer at boot buffer is an init section.
4362 * This function is called in lateinit. If we did not
4363 * find the boot tracer, then clear it out, to prevent
4364 * later registration from accessing the buffer that is
4365 * about to be freed.
4367 if (!default_bootup_tracer
)
4370 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4371 default_bootup_tracer
);
4372 default_bootup_tracer
= NULL
;
4377 early_initcall(tracer_alloc_buffers
);
4378 fs_initcall(tracer_init_debugfs
);
4379 late_initcall(clear_boot_tracer
);