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 ftrace_event_call
*call
, void *rec
,
173 struct ring_buffer_event
*event
)
175 return filter_check_discard(call
, rec
, global_trace
.buffer
, event
);
177 EXPORT_SYMBOL_GPL(filter_current_check_discard
);
179 cycle_t
ftrace_now(int cpu
)
183 /* Early boot up does not have a buffer yet */
184 if (!global_trace
.buffer
)
185 return trace_clock_local();
187 ts
= ring_buffer_time_stamp(global_trace
.buffer
, cpu
);
188 ring_buffer_normalize_time_stamp(global_trace
.buffer
, cpu
, &ts
);
194 * The max_tr is used to snapshot the global_trace when a maximum
195 * latency is reached. Some tracers will use this to store a maximum
196 * trace while it continues examining live traces.
198 * The buffers for the max_tr are set up the same as the global_trace.
199 * When a snapshot is taken, the link list of the max_tr is swapped
200 * with the link list of the global_trace and the buffers are reset for
201 * the global_trace so the tracing can continue.
203 static struct trace_array max_tr
;
205 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
207 /* tracer_enabled is used to toggle activation of a tracer */
208 static int tracer_enabled
= 1;
211 * tracing_is_enabled - return tracer_enabled status
213 * This function is used by other tracers to know the status
214 * of the tracer_enabled flag. Tracers may use this function
215 * to know if it should enable their features when starting
216 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
218 int tracing_is_enabled(void)
220 return tracer_enabled
;
224 * trace_buf_size is the size in bytes that is allocated
225 * for a buffer. Note, the number of bytes is always rounded
228 * This number is purposely set to a low number of 16384.
229 * If the dump on oops happens, it will be much appreciated
230 * to not have to wait for all that output. Anyway this can be
231 * boot time and run time configurable.
233 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
235 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
237 /* trace_types holds a link list of available tracers. */
238 static struct tracer
*trace_types __read_mostly
;
240 /* current_trace points to the tracer that is currently active */
241 static struct tracer
*current_trace __read_mostly
;
244 * max_tracer_type_len is used to simplify the allocating of
245 * buffers to read userspace tracer names. We keep track of
246 * the longest tracer name registered.
248 static int max_tracer_type_len
;
251 * trace_types_lock is used to protect the trace_types list.
252 * This lock is also used to keep user access serialized.
253 * Accesses from userspace will grab this lock while userspace
254 * activities happen inside the kernel.
256 static DEFINE_MUTEX(trace_types_lock
);
258 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
259 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
261 /* trace_flags holds trace_options default values */
262 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
263 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
| TRACE_ITER_SLEEP_TIME
|
264 TRACE_ITER_GRAPH_TIME
;
267 * trace_wake_up - wake up tasks waiting for trace input
269 * Simply wakes up any task that is blocked on the trace_wait
270 * queue. These is used with trace_poll for tasks polling the trace.
272 void trace_wake_up(void)
275 * The runqueue_is_locked() can fail, but this is the best we
278 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
279 wake_up(&trace_wait
);
282 static int __init
set_buf_size(char *str
)
284 unsigned long buf_size
;
288 buf_size
= memparse(str
, &str
);
289 /* nr_entries can not be zero */
292 trace_buf_size
= buf_size
;
295 __setup("trace_buf_size=", set_buf_size
);
297 unsigned long nsecs_to_usecs(unsigned long nsecs
)
302 /* These must match the bit postions in trace_iterator_flags */
303 static const char *trace_options
[] = {
332 { trace_clock_local
, "local" },
333 { trace_clock_global
, "global" },
338 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
346 if (s
->len
<= s
->readpos
)
349 len
= s
->len
- s
->readpos
;
352 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
362 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
367 if (s
->len
<= s
->readpos
)
370 len
= s
->len
- s
->readpos
;
373 ret
= memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
382 * ftrace_max_lock is used to protect the swapping of buffers
383 * when taking a max snapshot. The buffers themselves are
384 * protected by per_cpu spinlocks. But the action of the swap
385 * needs its own lock.
387 * This is defined as a raw_spinlock_t in order to help
388 * with performance when lockdep debugging is enabled.
390 * It is also used in other places outside the update_max_tr
391 * so it needs to be defined outside of the
392 * CONFIG_TRACER_MAX_TRACE.
394 static raw_spinlock_t ftrace_max_lock
=
395 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
397 #ifdef CONFIG_TRACER_MAX_TRACE
398 unsigned long __read_mostly tracing_max_latency
;
399 unsigned long __read_mostly tracing_thresh
;
402 * Copy the new maximum trace into the separate maximum-trace
403 * structure. (this way the maximum trace is permanently saved,
404 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
407 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
409 struct trace_array_cpu
*data
= tr
->data
[cpu
];
412 max_tr
.time_start
= data
->preempt_timestamp
;
414 data
= max_tr
.data
[cpu
];
415 data
->saved_latency
= tracing_max_latency
;
417 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
418 data
->pid
= tsk
->pid
;
419 data
->uid
= task_uid(tsk
);
420 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
421 data
->policy
= tsk
->policy
;
422 data
->rt_priority
= tsk
->rt_priority
;
424 /* record this tasks comm */
425 tracing_record_cmdline(tsk
);
429 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
431 * @tsk: the task with the latency
432 * @cpu: The cpu that initiated the trace.
434 * Flip the buffers between the @tr and the max_tr and record information
435 * about which task was the cause of this latency.
438 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
440 struct ring_buffer
*buf
= tr
->buffer
;
442 WARN_ON_ONCE(!irqs_disabled());
443 __raw_spin_lock(&ftrace_max_lock
);
445 tr
->buffer
= max_tr
.buffer
;
448 ftrace_disable_cpu();
449 ring_buffer_reset(tr
->buffer
);
452 __update_max_tr(tr
, tsk
, cpu
);
453 __raw_spin_unlock(&ftrace_max_lock
);
457 * update_max_tr_single - only copy one trace over, and reset the rest
459 * @tsk - task with the latency
460 * @cpu - the cpu of the buffer to copy.
462 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
465 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
469 WARN_ON_ONCE(!irqs_disabled());
470 __raw_spin_lock(&ftrace_max_lock
);
472 ftrace_disable_cpu();
474 ring_buffer_reset(max_tr
.buffer
);
475 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
479 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
);
481 __update_max_tr(tr
, tsk
, cpu
);
482 __raw_spin_unlock(&ftrace_max_lock
);
484 #endif /* CONFIG_TRACER_MAX_TRACE */
487 * register_tracer - register a tracer with the ftrace system.
488 * @type - the plugin for the tracer
490 * Register a new plugin tracer.
492 int register_tracer(struct tracer
*type
)
493 __releases(kernel_lock
)
494 __acquires(kernel_lock
)
501 pr_info("Tracer must have a name\n");
506 * When this gets called we hold the BKL which means that
507 * preemption is disabled. Various trace selftests however
508 * need to disable and enable preemption for successful tests.
509 * So we drop the BKL here and grab it after the tests again.
512 mutex_lock(&trace_types_lock
);
514 tracing_selftest_running
= true;
516 for (t
= trace_types
; t
; t
= t
->next
) {
517 if (strcmp(type
->name
, t
->name
) == 0) {
519 pr_info("Trace %s already registered\n",
527 type
->set_flag
= &dummy_set_flag
;
529 type
->flags
= &dummy_tracer_flags
;
531 if (!type
->flags
->opts
)
532 type
->flags
->opts
= dummy_tracer_opt
;
533 if (!type
->wait_pipe
)
534 type
->wait_pipe
= default_wait_pipe
;
537 #ifdef CONFIG_FTRACE_STARTUP_TEST
538 if (type
->selftest
&& !tracing_selftest_disabled
) {
539 struct tracer
*saved_tracer
= current_trace
;
540 struct trace_array
*tr
= &global_trace
;
544 * Run a selftest on this tracer.
545 * Here we reset the trace buffer, and set the current
546 * tracer to be this tracer. The tracer can then run some
547 * internal tracing to verify that everything is in order.
548 * If we fail, we do not register this tracer.
550 for_each_tracing_cpu(i
)
551 tracing_reset(tr
, i
);
553 current_trace
= type
;
554 /* the test is responsible for initializing and enabling */
555 pr_info("Testing tracer %s: ", type
->name
);
556 ret
= type
->selftest(type
, tr
);
557 /* the test is responsible for resetting too */
558 current_trace
= saved_tracer
;
560 printk(KERN_CONT
"FAILED!\n");
563 /* Only reset on passing, to avoid touching corrupted buffers */
564 for_each_tracing_cpu(i
)
565 tracing_reset(tr
, i
);
567 printk(KERN_CONT
"PASSED\n");
571 type
->next
= trace_types
;
573 len
= strlen(type
->name
);
574 if (len
> max_tracer_type_len
)
575 max_tracer_type_len
= len
;
578 tracing_selftest_running
= false;
579 mutex_unlock(&trace_types_lock
);
581 if (ret
|| !default_bootup_tracer
)
584 if (strncmp(default_bootup_tracer
, type
->name
, BOOTUP_TRACER_SIZE
))
587 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
588 /* Do we want this tracer to start on bootup? */
589 tracing_set_tracer(type
->name
);
590 default_bootup_tracer
= NULL
;
591 /* disable other selftests, since this will break it. */
592 tracing_selftest_disabled
= 1;
593 #ifdef CONFIG_FTRACE_STARTUP_TEST
594 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
603 void unregister_tracer(struct tracer
*type
)
608 mutex_lock(&trace_types_lock
);
609 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
613 pr_info("Trace %s not registered\n", type
->name
);
619 if (type
== current_trace
&& tracer_enabled
) {
622 if (current_trace
->stop
)
623 current_trace
->stop(&global_trace
);
624 current_trace
= &nop_trace
;
627 if (strlen(type
->name
) != max_tracer_type_len
)
630 max_tracer_type_len
= 0;
631 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
632 len
= strlen((*t
)->name
);
633 if (len
> max_tracer_type_len
)
634 max_tracer_type_len
= len
;
637 mutex_unlock(&trace_types_lock
);
640 void tracing_reset(struct trace_array
*tr
, int cpu
)
642 ftrace_disable_cpu();
643 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
647 void tracing_reset_online_cpus(struct trace_array
*tr
)
651 tr
->time_start
= ftrace_now(tr
->cpu
);
653 for_each_online_cpu(cpu
)
654 tracing_reset(tr
, cpu
);
657 void tracing_reset_current(int cpu
)
659 tracing_reset(&global_trace
, cpu
);
662 void tracing_reset_current_online_cpus(void)
664 tracing_reset_online_cpus(&global_trace
);
667 #define SAVED_CMDLINES 128
668 #define NO_CMDLINE_MAP UINT_MAX
669 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
670 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
671 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
672 static int cmdline_idx
;
673 static raw_spinlock_t trace_cmdline_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
675 /* temporary disable recording */
676 static atomic_t trace_record_cmdline_disabled __read_mostly
;
678 static void trace_init_cmdlines(void)
680 memset(&map_pid_to_cmdline
, NO_CMDLINE_MAP
, sizeof(map_pid_to_cmdline
));
681 memset(&map_cmdline_to_pid
, NO_CMDLINE_MAP
, sizeof(map_cmdline_to_pid
));
685 static int trace_stop_count
;
686 static DEFINE_SPINLOCK(tracing_start_lock
);
689 * ftrace_off_permanent - disable all ftrace code permanently
691 * This should only be called when a serious anomally has
692 * been detected. This will turn off the function tracing,
693 * ring buffers, and other tracing utilites. It takes no
694 * locks and can be called from any context.
696 void ftrace_off_permanent(void)
698 tracing_disabled
= 1;
700 tracing_off_permanent();
704 * tracing_start - quick start of the tracer
706 * If tracing is enabled but was stopped by tracing_stop,
707 * this will start the tracer back up.
709 void tracing_start(void)
711 struct ring_buffer
*buffer
;
714 if (tracing_disabled
)
717 spin_lock_irqsave(&tracing_start_lock
, flags
);
718 if (--trace_stop_count
) {
719 if (trace_stop_count
< 0) {
720 /* Someone screwed up their debugging */
722 trace_stop_count
= 0;
728 buffer
= global_trace
.buffer
;
730 ring_buffer_record_enable(buffer
);
732 buffer
= max_tr
.buffer
;
734 ring_buffer_record_enable(buffer
);
738 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
742 * tracing_stop - quick stop of the tracer
744 * Light weight way to stop tracing. Use in conjunction with
747 void tracing_stop(void)
749 struct ring_buffer
*buffer
;
753 spin_lock_irqsave(&tracing_start_lock
, flags
);
754 if (trace_stop_count
++)
757 buffer
= global_trace
.buffer
;
759 ring_buffer_record_disable(buffer
);
761 buffer
= max_tr
.buffer
;
763 ring_buffer_record_disable(buffer
);
766 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
769 void trace_stop_cmdline_recording(void);
771 static void trace_save_cmdline(struct task_struct
*tsk
)
775 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
779 * It's not the end of the world if we don't get
780 * the lock, but we also don't want to spin
781 * nor do we want to disable interrupts,
782 * so if we miss here, then better luck next time.
784 if (!__raw_spin_trylock(&trace_cmdline_lock
))
787 idx
= map_pid_to_cmdline
[tsk
->pid
];
788 if (idx
== NO_CMDLINE_MAP
) {
789 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
792 * Check whether the cmdline buffer at idx has a pid
793 * mapped. We are going to overwrite that entry so we
794 * need to clear the map_pid_to_cmdline. Otherwise we
795 * would read the new comm for the old pid.
797 pid
= map_cmdline_to_pid
[idx
];
798 if (pid
!= NO_CMDLINE_MAP
)
799 map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
801 map_cmdline_to_pid
[idx
] = tsk
->pid
;
802 map_pid_to_cmdline
[tsk
->pid
] = idx
;
807 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
809 __raw_spin_unlock(&trace_cmdline_lock
);
812 void trace_find_cmdline(int pid
, char comm
[])
817 strcpy(comm
, "<idle>");
821 if (pid
> PID_MAX_DEFAULT
) {
822 strcpy(comm
, "<...>");
827 __raw_spin_lock(&trace_cmdline_lock
);
828 map
= map_pid_to_cmdline
[pid
];
829 if (map
!= NO_CMDLINE_MAP
)
830 strcpy(comm
, saved_cmdlines
[map
]);
832 strcpy(comm
, "<...>");
834 __raw_spin_unlock(&trace_cmdline_lock
);
838 void tracing_record_cmdline(struct task_struct
*tsk
)
840 if (atomic_read(&trace_record_cmdline_disabled
) || !tracer_enabled
||
844 trace_save_cmdline(tsk
);
848 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
851 struct task_struct
*tsk
= current
;
853 entry
->preempt_count
= pc
& 0xff;
854 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
855 entry
->tgid
= (tsk
) ? tsk
->tgid
: 0;
857 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
858 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
860 TRACE_FLAG_IRQS_NOSUPPORT
|
862 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
863 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
864 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
866 EXPORT_SYMBOL_GPL(tracing_generic_entry_update
);
868 struct ring_buffer_event
*trace_buffer_lock_reserve(struct trace_array
*tr
,
871 unsigned long flags
, int pc
)
873 struct ring_buffer_event
*event
;
875 event
= ring_buffer_lock_reserve(tr
->buffer
, len
);
877 struct trace_entry
*ent
= ring_buffer_event_data(event
);
879 tracing_generic_entry_update(ent
, flags
, pc
);
886 static inline void __trace_buffer_unlock_commit(struct trace_array
*tr
,
887 struct ring_buffer_event
*event
,
888 unsigned long flags
, int pc
,
891 ring_buffer_unlock_commit(tr
->buffer
, event
);
893 ftrace_trace_stack(tr
, flags
, 6, pc
);
894 ftrace_trace_userstack(tr
, flags
, pc
);
900 void trace_buffer_unlock_commit(struct trace_array
*tr
,
901 struct ring_buffer_event
*event
,
902 unsigned long flags
, int pc
)
904 __trace_buffer_unlock_commit(tr
, event
, flags
, pc
, 1);
907 struct ring_buffer_event
*
908 trace_current_buffer_lock_reserve(int type
, unsigned long len
,
909 unsigned long flags
, int pc
)
911 return trace_buffer_lock_reserve(&global_trace
,
912 type
, len
, flags
, pc
);
914 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve
);
916 void trace_current_buffer_unlock_commit(struct ring_buffer_event
*event
,
917 unsigned long flags
, int pc
)
919 __trace_buffer_unlock_commit(&global_trace
, event
, flags
, pc
, 1);
921 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit
);
923 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event
*event
,
924 unsigned long flags
, int pc
)
926 __trace_buffer_unlock_commit(&global_trace
, event
, flags
, pc
, 0);
928 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit
);
930 void trace_current_buffer_discard_commit(struct ring_buffer_event
*event
)
932 ring_buffer_discard_commit(global_trace
.buffer
, event
);
934 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
937 trace_function(struct trace_array
*tr
,
938 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
941 struct ftrace_event_call
*call
= &event_function
;
942 struct ring_buffer_event
*event
;
943 struct ftrace_entry
*entry
;
945 /* If we are reading the ring buffer, don't trace */
946 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
949 event
= trace_buffer_lock_reserve(tr
, TRACE_FN
, sizeof(*entry
),
953 entry
= ring_buffer_event_data(event
);
955 entry
->parent_ip
= parent_ip
;
957 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
958 ring_buffer_unlock_commit(tr
->buffer
, event
);
962 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
963 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
966 if (likely(!atomic_read(&data
->disabled
)))
967 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
970 #ifdef CONFIG_STACKTRACE
971 static void __ftrace_trace_stack(struct trace_array
*tr
,
975 struct ftrace_event_call
*call
= &event_kernel_stack
;
976 struct ring_buffer_event
*event
;
977 struct stack_entry
*entry
;
978 struct stack_trace trace
;
980 event
= trace_buffer_lock_reserve(tr
, TRACE_STACK
,
981 sizeof(*entry
), flags
, pc
);
984 entry
= ring_buffer_event_data(event
);
985 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
987 trace
.nr_entries
= 0;
988 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
990 trace
.entries
= entry
->caller
;
992 save_stack_trace(&trace
);
993 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
994 ring_buffer_unlock_commit(tr
->buffer
, event
);
997 void ftrace_trace_stack(struct trace_array
*tr
, unsigned long flags
, int skip
,
1000 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1003 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
1006 void __trace_stack(struct trace_array
*tr
, unsigned long flags
, int skip
,
1009 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
1012 void ftrace_trace_userstack(struct trace_array
*tr
, unsigned long flags
, int pc
)
1014 struct ftrace_event_call
*call
= &event_user_stack
;
1015 struct ring_buffer_event
*event
;
1016 struct userstack_entry
*entry
;
1017 struct stack_trace trace
;
1019 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1022 event
= trace_buffer_lock_reserve(tr
, TRACE_USER_STACK
,
1023 sizeof(*entry
), flags
, pc
);
1026 entry
= ring_buffer_event_data(event
);
1028 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1030 trace
.nr_entries
= 0;
1031 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1033 trace
.entries
= entry
->caller
;
1035 save_stack_trace_user(&trace
);
1036 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1037 ring_buffer_unlock_commit(tr
->buffer
, event
);
1041 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1043 ftrace_trace_userstack(tr
, flags
, preempt_count());
1047 #endif /* CONFIG_STACKTRACE */
1050 ftrace_trace_special(void *__tr
,
1051 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1054 struct ring_buffer_event
*event
;
1055 struct trace_array
*tr
= __tr
;
1056 struct special_entry
*entry
;
1058 event
= trace_buffer_lock_reserve(tr
, TRACE_SPECIAL
,
1059 sizeof(*entry
), 0, pc
);
1062 entry
= ring_buffer_event_data(event
);
1066 trace_buffer_unlock_commit(tr
, event
, 0, pc
);
1070 __trace_special(void *__tr
, void *__data
,
1071 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1073 ftrace_trace_special(__tr
, arg1
, arg2
, arg3
, preempt_count());
1077 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1079 struct trace_array
*tr
= &global_trace
;
1080 struct trace_array_cpu
*data
;
1081 unsigned long flags
;
1085 if (tracing_disabled
)
1088 pc
= preempt_count();
1089 local_irq_save(flags
);
1090 cpu
= raw_smp_processor_id();
1091 data
= tr
->data
[cpu
];
1093 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1094 ftrace_trace_special(tr
, arg1
, arg2
, arg3
, pc
);
1096 atomic_dec(&data
->disabled
);
1097 local_irq_restore(flags
);
1101 * trace_vbprintk - write binary msg to tracing buffer
1104 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
1106 static raw_spinlock_t trace_buf_lock
=
1107 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
1108 static u32 trace_buf
[TRACE_BUF_SIZE
];
1110 struct ftrace_event_call
*call
= &event_bprint
;
1111 struct ring_buffer_event
*event
;
1112 struct trace_array
*tr
= &global_trace
;
1113 struct trace_array_cpu
*data
;
1114 struct bprint_entry
*entry
;
1115 unsigned long flags
;
1118 int cpu
, len
= 0, size
, pc
;
1120 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
1123 /* Don't pollute graph traces with trace_vprintk internals */
1124 pause_graph_tracing();
1126 pc
= preempt_count();
1127 resched
= ftrace_preempt_disable();
1128 cpu
= raw_smp_processor_id();
1129 data
= tr
->data
[cpu
];
1131 disable
= atomic_inc_return(&data
->disabled
);
1132 if (unlikely(disable
!= 1))
1135 /* Lockdep uses trace_printk for lock tracing */
1136 local_irq_save(flags
);
1137 __raw_spin_lock(&trace_buf_lock
);
1138 len
= vbin_printf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1140 if (len
> TRACE_BUF_SIZE
|| len
< 0)
1143 size
= sizeof(*entry
) + sizeof(u32
) * len
;
1144 event
= trace_buffer_lock_reserve(tr
, TRACE_BPRINT
, size
, flags
, pc
);
1147 entry
= ring_buffer_event_data(event
);
1151 memcpy(entry
->buf
, trace_buf
, sizeof(u32
) * len
);
1152 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1153 ring_buffer_unlock_commit(tr
->buffer
, event
);
1156 __raw_spin_unlock(&trace_buf_lock
);
1157 local_irq_restore(flags
);
1160 atomic_dec_return(&data
->disabled
);
1161 ftrace_preempt_enable(resched
);
1162 unpause_graph_tracing();
1166 EXPORT_SYMBOL_GPL(trace_vbprintk
);
1168 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
1170 static raw_spinlock_t trace_buf_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
1171 static char trace_buf
[TRACE_BUF_SIZE
];
1173 struct ftrace_event_call
*call
= &event_print
;
1174 struct ring_buffer_event
*event
;
1175 struct trace_array
*tr
= &global_trace
;
1176 struct trace_array_cpu
*data
;
1177 int cpu
, len
= 0, size
, pc
;
1178 struct print_entry
*entry
;
1179 unsigned long irq_flags
;
1182 if (tracing_disabled
|| tracing_selftest_running
)
1185 pc
= preempt_count();
1186 preempt_disable_notrace();
1187 cpu
= raw_smp_processor_id();
1188 data
= tr
->data
[cpu
];
1190 disable
= atomic_inc_return(&data
->disabled
);
1191 if (unlikely(disable
!= 1))
1194 pause_graph_tracing();
1195 raw_local_irq_save(irq_flags
);
1196 __raw_spin_lock(&trace_buf_lock
);
1197 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
1199 len
= min(len
, TRACE_BUF_SIZE
-1);
1202 size
= sizeof(*entry
) + len
+ 1;
1203 event
= trace_buffer_lock_reserve(tr
, TRACE_PRINT
, size
, irq_flags
, pc
);
1206 entry
= ring_buffer_event_data(event
);
1209 memcpy(&entry
->buf
, trace_buf
, len
);
1210 entry
->buf
[len
] = 0;
1211 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
1212 ring_buffer_unlock_commit(tr
->buffer
, event
);
1215 __raw_spin_unlock(&trace_buf_lock
);
1216 raw_local_irq_restore(irq_flags
);
1217 unpause_graph_tracing();
1219 atomic_dec_return(&data
->disabled
);
1220 preempt_enable_notrace();
1224 EXPORT_SYMBOL_GPL(trace_vprintk
);
1226 enum trace_file_type
{
1227 TRACE_FILE_LAT_FMT
= 1,
1228 TRACE_FILE_ANNOTATE
= 2,
1231 static void trace_iterator_increment(struct trace_iterator
*iter
)
1233 /* Don't allow ftrace to trace into the ring buffers */
1234 ftrace_disable_cpu();
1237 if (iter
->buffer_iter
[iter
->cpu
])
1238 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1240 ftrace_enable_cpu();
1243 static struct trace_entry
*
1244 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1246 struct ring_buffer_event
*event
;
1247 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1249 /* Don't allow ftrace to trace into the ring buffers */
1250 ftrace_disable_cpu();
1253 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1255 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1257 ftrace_enable_cpu();
1259 return event
? ring_buffer_event_data(event
) : NULL
;
1262 static struct trace_entry
*
1263 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1265 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1266 struct trace_entry
*ent
, *next
= NULL
;
1267 int cpu_file
= iter
->cpu_file
;
1268 u64 next_ts
= 0, ts
;
1273 * If we are in a per_cpu trace file, don't bother by iterating over
1274 * all cpu and peek directly.
1276 if (cpu_file
> TRACE_PIPE_ALL_CPU
) {
1277 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
1279 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
);
1281 *ent_cpu
= cpu_file
;
1286 for_each_tracing_cpu(cpu
) {
1288 if (ring_buffer_empty_cpu(buffer
, cpu
))
1291 ent
= peek_next_entry(iter
, cpu
, &ts
);
1294 * Pick the entry with the smallest timestamp:
1296 if (ent
&& (!next
|| ts
< next_ts
)) {
1304 *ent_cpu
= next_cpu
;
1312 /* Find the next real entry, without updating the iterator itself */
1313 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1314 int *ent_cpu
, u64
*ent_ts
)
1316 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1319 /* Find the next real entry, and increment the iterator to the next entry */
1320 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1322 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1325 trace_iterator_increment(iter
);
1327 return iter
->ent
? iter
: NULL
;
1330 static void trace_consume(struct trace_iterator
*iter
)
1332 /* Don't allow ftrace to trace into the ring buffers */
1333 ftrace_disable_cpu();
1334 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1335 ftrace_enable_cpu();
1338 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1340 struct trace_iterator
*iter
= m
->private;
1346 /* can't go backwards */
1351 ent
= find_next_entry_inc(iter
);
1355 while (ent
&& iter
->idx
< i
)
1356 ent
= find_next_entry_inc(iter
);
1364 * No necessary locking here. The worst thing which can
1365 * happen is loosing events consumed at the same time
1366 * by a trace_pipe reader.
1367 * Other than that, we don't risk to crash the ring buffer
1368 * because it serializes the readers.
1370 * The current tracer is copied to avoid a global locking
1373 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1375 struct trace_iterator
*iter
= m
->private;
1376 static struct tracer
*old_tracer
;
1377 int cpu_file
= iter
->cpu_file
;
1382 /* copy the tracer to avoid using a global lock all around */
1383 mutex_lock(&trace_types_lock
);
1384 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
1385 old_tracer
= current_trace
;
1386 *iter
->trace
= *current_trace
;
1388 mutex_unlock(&trace_types_lock
);
1390 atomic_inc(&trace_record_cmdline_disabled
);
1392 if (*pos
!= iter
->pos
) {
1397 ftrace_disable_cpu();
1399 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
1400 for_each_tracing_cpu(cpu
)
1401 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1403 ring_buffer_iter_reset(iter
->buffer_iter
[cpu_file
]);
1406 ftrace_enable_cpu();
1408 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1413 p
= s_next(m
, p
, &l
);
1416 trace_event_read_lock();
1420 static void s_stop(struct seq_file
*m
, void *p
)
1422 atomic_dec(&trace_record_cmdline_disabled
);
1423 trace_event_read_unlock();
1426 static void print_lat_help_header(struct seq_file
*m
)
1428 seq_puts(m
, "# _------=> CPU# \n");
1429 seq_puts(m
, "# / _-----=> irqs-off \n");
1430 seq_puts(m
, "# | / _----=> need-resched \n");
1431 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1432 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1433 seq_puts(m
, "# |||| / \n");
1434 seq_puts(m
, "# ||||| delay \n");
1435 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1436 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1439 static void print_func_help_header(struct seq_file
*m
)
1441 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1442 seq_puts(m
, "# | | | | |\n");
1447 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1449 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1450 struct trace_array
*tr
= iter
->tr
;
1451 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1452 struct tracer
*type
= current_trace
;
1453 unsigned long total
;
1454 unsigned long entries
;
1455 const char *name
= "preemption";
1460 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1462 ring_buffer_overruns(iter
->tr
->buffer
);
1464 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
1466 seq_puts(m
, "# -----------------------------------"
1467 "---------------------------------\n");
1468 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1469 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1470 nsecs_to_usecs(data
->saved_latency
),
1474 #if defined(CONFIG_PREEMPT_NONE)
1476 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1478 #elif defined(CONFIG_PREEMPT)
1483 /* These are reserved for later use */
1486 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1490 seq_puts(m
, "# -----------------\n");
1491 seq_printf(m
, "# | task: %.16s-%d "
1492 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1493 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1494 data
->policy
, data
->rt_priority
);
1495 seq_puts(m
, "# -----------------\n");
1497 if (data
->critical_start
) {
1498 seq_puts(m
, "# => started at: ");
1499 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1500 trace_print_seq(m
, &iter
->seq
);
1501 seq_puts(m
, "\n# => ended at: ");
1502 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1503 trace_print_seq(m
, &iter
->seq
);
1510 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1512 struct trace_seq
*s
= &iter
->seq
;
1514 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1517 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1520 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1523 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1525 /* Don't print started cpu buffer for the first entry of the trace */
1527 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
1531 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1533 struct trace_seq
*s
= &iter
->seq
;
1534 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1535 struct trace_entry
*entry
;
1536 struct trace_event
*event
;
1540 test_cpu_buff_start(iter
);
1542 event
= ftrace_find_event(entry
->type
);
1544 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1545 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1546 if (!trace_print_lat_context(iter
))
1549 if (!trace_print_context(iter
))
1555 return event
->trace(iter
, sym_flags
);
1557 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1560 return TRACE_TYPE_HANDLED
;
1562 return TRACE_TYPE_PARTIAL_LINE
;
1565 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1567 struct trace_seq
*s
= &iter
->seq
;
1568 struct trace_entry
*entry
;
1569 struct trace_event
*event
;
1573 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1574 if (!trace_seq_printf(s
, "%d %d %llu ",
1575 entry
->pid
, iter
->cpu
, iter
->ts
))
1579 event
= ftrace_find_event(entry
->type
);
1581 return event
->raw(iter
, 0);
1583 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1586 return TRACE_TYPE_HANDLED
;
1588 return TRACE_TYPE_PARTIAL_LINE
;
1591 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1593 struct trace_seq
*s
= &iter
->seq
;
1594 unsigned char newline
= '\n';
1595 struct trace_entry
*entry
;
1596 struct trace_event
*event
;
1600 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1601 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1602 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1603 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1606 event
= ftrace_find_event(entry
->type
);
1608 enum print_line_t ret
= event
->hex(iter
, 0);
1609 if (ret
!= TRACE_TYPE_HANDLED
)
1613 SEQ_PUT_FIELD_RET(s
, newline
);
1615 return TRACE_TYPE_HANDLED
;
1618 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1620 struct trace_seq
*s
= &iter
->seq
;
1621 struct trace_entry
*entry
;
1622 struct trace_event
*event
;
1626 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1627 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1628 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1629 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1632 event
= ftrace_find_event(entry
->type
);
1633 return event
? event
->binary(iter
, 0) : TRACE_TYPE_HANDLED
;
1636 static int trace_empty(struct trace_iterator
*iter
)
1640 /* If we are looking at one CPU buffer, only check that one */
1641 if (iter
->cpu_file
!= TRACE_PIPE_ALL_CPU
) {
1642 cpu
= iter
->cpu_file
;
1643 if (iter
->buffer_iter
[cpu
]) {
1644 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1647 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1653 for_each_tracing_cpu(cpu
) {
1654 if (iter
->buffer_iter
[cpu
]) {
1655 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1658 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1666 /* Called with trace_event_read_lock() held. */
1667 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1669 enum print_line_t ret
;
1671 if (iter
->trace
&& iter
->trace
->print_line
) {
1672 ret
= iter
->trace
->print_line(iter
);
1673 if (ret
!= TRACE_TYPE_UNHANDLED
)
1677 if (iter
->ent
->type
== TRACE_BPRINT
&&
1678 trace_flags
& TRACE_ITER_PRINTK
&&
1679 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1680 return trace_print_bprintk_msg_only(iter
);
1682 if (iter
->ent
->type
== TRACE_PRINT
&&
1683 trace_flags
& TRACE_ITER_PRINTK
&&
1684 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1685 return trace_print_printk_msg_only(iter
);
1687 if (trace_flags
& TRACE_ITER_BIN
)
1688 return print_bin_fmt(iter
);
1690 if (trace_flags
& TRACE_ITER_HEX
)
1691 return print_hex_fmt(iter
);
1693 if (trace_flags
& TRACE_ITER_RAW
)
1694 return print_raw_fmt(iter
);
1696 return print_trace_fmt(iter
);
1699 static int s_show(struct seq_file
*m
, void *v
)
1701 struct trace_iterator
*iter
= v
;
1703 if (iter
->ent
== NULL
) {
1705 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1708 if (iter
->trace
&& iter
->trace
->print_header
)
1709 iter
->trace
->print_header(m
);
1710 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1711 /* print nothing if the buffers are empty */
1712 if (trace_empty(iter
))
1714 print_trace_header(m
, iter
);
1715 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1716 print_lat_help_header(m
);
1718 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1719 print_func_help_header(m
);
1722 print_trace_line(iter
);
1723 trace_print_seq(m
, &iter
->seq
);
1729 static struct seq_operations tracer_seq_ops
= {
1736 static struct trace_iterator
*
1737 __tracing_open(struct inode
*inode
, struct file
*file
)
1739 long cpu_file
= (long) inode
->i_private
;
1740 void *fail_ret
= ERR_PTR(-ENOMEM
);
1741 struct trace_iterator
*iter
;
1745 if (tracing_disabled
)
1746 return ERR_PTR(-ENODEV
);
1748 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1750 return ERR_PTR(-ENOMEM
);
1753 * We make a copy of the current tracer to avoid concurrent
1754 * changes on it while we are reading.
1756 mutex_lock(&trace_types_lock
);
1757 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
1762 *iter
->trace
= *current_trace
;
1764 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
1767 cpumask_clear(iter
->started
);
1769 if (current_trace
&& current_trace
->print_max
)
1772 iter
->tr
= &global_trace
;
1774 mutex_init(&iter
->mutex
);
1775 iter
->cpu_file
= cpu_file
;
1777 /* Notify the tracer early; before we stop tracing. */
1778 if (iter
->trace
&& iter
->trace
->open
)
1779 iter
->trace
->open(iter
);
1781 /* Annotate start of buffers if we had overruns */
1782 if (ring_buffer_overruns(iter
->tr
->buffer
))
1783 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
1785 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
) {
1786 for_each_tracing_cpu(cpu
) {
1788 iter
->buffer_iter
[cpu
] =
1789 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1792 cpu
= iter
->cpu_file
;
1793 iter
->buffer_iter
[cpu
] =
1794 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1797 /* TODO stop tracer */
1798 ret
= seq_open(file
, &tracer_seq_ops
);
1800 fail_ret
= ERR_PTR(ret
);
1804 m
= file
->private_data
;
1807 /* stop the trace while dumping */
1810 mutex_unlock(&trace_types_lock
);
1815 for_each_tracing_cpu(cpu
) {
1816 if (iter
->buffer_iter
[cpu
])
1817 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1819 free_cpumask_var(iter
->started
);
1821 mutex_unlock(&trace_types_lock
);
1828 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1830 if (tracing_disabled
)
1833 filp
->private_data
= inode
->i_private
;
1837 static int tracing_release(struct inode
*inode
, struct file
*file
)
1839 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1840 struct trace_iterator
*iter
;
1843 if (!(file
->f_mode
& FMODE_READ
))
1848 mutex_lock(&trace_types_lock
);
1849 for_each_tracing_cpu(cpu
) {
1850 if (iter
->buffer_iter
[cpu
])
1851 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1854 if (iter
->trace
&& iter
->trace
->close
)
1855 iter
->trace
->close(iter
);
1857 /* reenable tracing if it was previously enabled */
1859 mutex_unlock(&trace_types_lock
);
1861 seq_release(inode
, file
);
1862 mutex_destroy(&iter
->mutex
);
1863 free_cpumask_var(iter
->started
);
1869 static int tracing_open(struct inode
*inode
, struct file
*file
)
1871 struct trace_iterator
*iter
;
1874 /* If this file was open for write, then erase contents */
1875 if ((file
->f_mode
& FMODE_WRITE
) &&
1876 (file
->f_flags
& O_TRUNC
)) {
1877 long cpu
= (long) inode
->i_private
;
1879 if (cpu
== TRACE_PIPE_ALL_CPU
)
1880 tracing_reset_online_cpus(&global_trace
);
1882 tracing_reset(&global_trace
, cpu
);
1885 if (file
->f_mode
& FMODE_READ
) {
1886 iter
= __tracing_open(inode
, file
);
1888 ret
= PTR_ERR(iter
);
1889 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
1890 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
1896 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1898 struct tracer
*t
= v
;
1908 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1913 mutex_lock(&trace_types_lock
);
1914 for (t
= trace_types
; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
1920 static void t_stop(struct seq_file
*m
, void *p
)
1922 mutex_unlock(&trace_types_lock
);
1925 static int t_show(struct seq_file
*m
, void *v
)
1927 struct tracer
*t
= v
;
1932 seq_printf(m
, "%s", t
->name
);
1941 static struct seq_operations show_traces_seq_ops
= {
1948 static int show_traces_open(struct inode
*inode
, struct file
*file
)
1950 if (tracing_disabled
)
1953 return seq_open(file
, &show_traces_seq_ops
);
1957 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
1958 size_t count
, loff_t
*ppos
)
1963 static const struct file_operations tracing_fops
= {
1964 .open
= tracing_open
,
1966 .write
= tracing_write_stub
,
1967 .llseek
= seq_lseek
,
1968 .release
= tracing_release
,
1971 static const struct file_operations show_traces_fops
= {
1972 .open
= show_traces_open
,
1974 .release
= seq_release
,
1978 * Only trace on a CPU if the bitmask is set:
1980 static cpumask_var_t tracing_cpumask
;
1983 * The tracer itself will not take this lock, but still we want
1984 * to provide a consistent cpumask to user-space:
1986 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
1989 * Temporary storage for the character representation of the
1990 * CPU bitmask (and one more byte for the newline):
1992 static char mask_str
[NR_CPUS
+ 1];
1995 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
1996 size_t count
, loff_t
*ppos
)
2000 mutex_lock(&tracing_cpumask_update_lock
);
2002 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2003 if (count
- len
< 2) {
2007 len
+= sprintf(mask_str
+ len
, "\n");
2008 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2011 mutex_unlock(&tracing_cpumask_update_lock
);
2017 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2018 size_t count
, loff_t
*ppos
)
2021 cpumask_var_t tracing_cpumask_new
;
2023 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2026 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2030 mutex_lock(&tracing_cpumask_update_lock
);
2032 local_irq_disable();
2033 __raw_spin_lock(&ftrace_max_lock
);
2034 for_each_tracing_cpu(cpu
) {
2036 * Increase/decrease the disabled counter if we are
2037 * about to flip a bit in the cpumask:
2039 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2040 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2041 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2043 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2044 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2045 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2048 __raw_spin_unlock(&ftrace_max_lock
);
2051 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2053 mutex_unlock(&tracing_cpumask_update_lock
);
2054 free_cpumask_var(tracing_cpumask_new
);
2059 free_cpumask_var(tracing_cpumask_new
);
2064 static const struct file_operations tracing_cpumask_fops
= {
2065 .open
= tracing_open_generic
,
2066 .read
= tracing_cpumask_read
,
2067 .write
= tracing_cpumask_write
,
2071 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2072 size_t cnt
, loff_t
*ppos
)
2074 struct tracer_opt
*trace_opts
;
2082 /* calculate max size */
2083 for (i
= 0; trace_options
[i
]; i
++) {
2084 len
+= strlen(trace_options
[i
]);
2085 len
+= 3; /* "no" and newline */
2088 mutex_lock(&trace_types_lock
);
2089 tracer_flags
= current_trace
->flags
->val
;
2090 trace_opts
= current_trace
->flags
->opts
;
2093 * Increase the size with names of options specific
2094 * of the current tracer.
2096 for (i
= 0; trace_opts
[i
].name
; i
++) {
2097 len
+= strlen(trace_opts
[i
].name
);
2098 len
+= 3; /* "no" and newline */
2102 buf
= kmalloc(len
+ 1, GFP_KERNEL
);
2104 mutex_unlock(&trace_types_lock
);
2108 for (i
= 0; trace_options
[i
]; i
++) {
2109 if (trace_flags
& (1 << i
))
2110 r
+= sprintf(buf
+ r
, "%s\n", trace_options
[i
]);
2112 r
+= sprintf(buf
+ r
, "no%s\n", trace_options
[i
]);
2115 for (i
= 0; trace_opts
[i
].name
; i
++) {
2116 if (tracer_flags
& trace_opts
[i
].bit
)
2117 r
+= sprintf(buf
+ r
, "%s\n",
2118 trace_opts
[i
].name
);
2120 r
+= sprintf(buf
+ r
, "no%s\n",
2121 trace_opts
[i
].name
);
2123 mutex_unlock(&trace_types_lock
);
2125 WARN_ON(r
>= len
+ 1);
2127 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2133 /* Try to assign a tracer specific option */
2134 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2136 struct tracer_flags
*tracer_flags
= trace
->flags
;
2137 struct tracer_opt
*opts
= NULL
;
2141 for (i
= 0; tracer_flags
->opts
[i
].name
; i
++) {
2142 opts
= &tracer_flags
->opts
[i
];
2143 len
= strlen(opts
->name
);
2145 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2146 ret
= trace
->set_flag(tracer_flags
->val
,
2152 if (!tracer_flags
->opts
[i
].name
)
2155 /* Refused to handle */
2160 tracer_flags
->val
&= ~opts
->bit
;
2162 tracer_flags
->val
|= opts
->bit
;
2167 static void set_tracer_flags(unsigned int mask
, int enabled
)
2169 /* do nothing if flag is already set */
2170 if (!!(trace_flags
& mask
) == !!enabled
)
2174 trace_flags
|= mask
;
2176 trace_flags
&= ~mask
;
2180 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2181 size_t cnt
, loff_t
*ppos
)
2189 if (cnt
>= sizeof(buf
))
2192 if (copy_from_user(&buf
, ubuf
, cnt
))
2197 if (strncmp(buf
, "no", 2) == 0) {
2202 for (i
= 0; trace_options
[i
]; i
++) {
2203 int len
= strlen(trace_options
[i
]);
2205 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2206 set_tracer_flags(1 << i
, !neg
);
2211 /* If no option could be set, test the specific tracer options */
2212 if (!trace_options
[i
]) {
2213 mutex_lock(&trace_types_lock
);
2214 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2215 mutex_unlock(&trace_types_lock
);
2225 static const struct file_operations tracing_iter_fops
= {
2226 .open
= tracing_open_generic
,
2227 .read
= tracing_trace_options_read
,
2228 .write
= tracing_trace_options_write
,
2231 static const char readme_msg
[] =
2232 "tracing mini-HOWTO:\n\n"
2233 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2234 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2235 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2236 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2238 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2239 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2241 "# cat /sys/kernel/debug/tracing/trace_options\n"
2242 "noprint-parent nosym-offset nosym-addr noverbose\n"
2243 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2244 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2245 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2246 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2250 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2251 size_t cnt
, loff_t
*ppos
)
2253 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2254 readme_msg
, strlen(readme_msg
));
2257 static const struct file_operations tracing_readme_fops
= {
2258 .open
= tracing_open_generic
,
2259 .read
= tracing_readme_read
,
2263 tracing_saved_cmdlines_read(struct file
*file
, char __user
*ubuf
,
2264 size_t cnt
, loff_t
*ppos
)
2273 file_buf
= kmalloc(SAVED_CMDLINES
*(16+TASK_COMM_LEN
), GFP_KERNEL
);
2277 buf_comm
= kmalloc(TASK_COMM_LEN
, GFP_KERNEL
);
2285 for (i
= 0; i
< SAVED_CMDLINES
; i
++) {
2288 pid
= map_cmdline_to_pid
[i
];
2289 if (pid
== -1 || pid
== NO_CMDLINE_MAP
)
2292 trace_find_cmdline(pid
, buf_comm
);
2293 r
= sprintf(buf
, "%d %s\n", pid
, buf_comm
);
2298 len
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
2307 static const struct file_operations tracing_saved_cmdlines_fops
= {
2308 .open
= tracing_open_generic
,
2309 .read
= tracing_saved_cmdlines_read
,
2313 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2314 size_t cnt
, loff_t
*ppos
)
2319 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2320 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2324 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2325 size_t cnt
, loff_t
*ppos
)
2327 struct trace_array
*tr
= filp
->private_data
;
2332 if (cnt
>= sizeof(buf
))
2335 if (copy_from_user(&buf
, ubuf
, cnt
))
2340 ret
= strict_strtoul(buf
, 10, &val
);
2346 mutex_lock(&trace_types_lock
);
2347 if (tracer_enabled
^ val
) {
2350 if (current_trace
->start
)
2351 current_trace
->start(tr
);
2356 if (current_trace
->stop
)
2357 current_trace
->stop(tr
);
2360 mutex_unlock(&trace_types_lock
);
2368 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2369 size_t cnt
, loff_t
*ppos
)
2371 char buf
[max_tracer_type_len
+2];
2374 mutex_lock(&trace_types_lock
);
2376 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2378 r
= sprintf(buf
, "\n");
2379 mutex_unlock(&trace_types_lock
);
2381 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2384 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2386 tracing_reset_online_cpus(tr
);
2390 static int tracing_resize_ring_buffer(unsigned long size
)
2395 * If kernel or user changes the size of the ring buffer
2396 * we use the size that was given, and we can forget about
2397 * expanding it later.
2399 ring_buffer_expanded
= 1;
2401 ret
= ring_buffer_resize(global_trace
.buffer
, size
);
2405 ret
= ring_buffer_resize(max_tr
.buffer
, size
);
2409 r
= ring_buffer_resize(global_trace
.buffer
,
2410 global_trace
.entries
);
2413 * AARGH! We are left with different
2414 * size max buffer!!!!
2415 * The max buffer is our "snapshot" buffer.
2416 * When a tracer needs a snapshot (one of the
2417 * latency tracers), it swaps the max buffer
2418 * with the saved snap shot. We succeeded to
2419 * update the size of the main buffer, but failed to
2420 * update the size of the max buffer. But when we tried
2421 * to reset the main buffer to the original size, we
2422 * failed there too. This is very unlikely to
2423 * happen, but if it does, warn and kill all
2427 tracing_disabled
= 1;
2432 global_trace
.entries
= size
;
2438 * tracing_update_buffers - used by tracing facility to expand ring buffers
2440 * To save on memory when the tracing is never used on a system with it
2441 * configured in. The ring buffers are set to a minimum size. But once
2442 * a user starts to use the tracing facility, then they need to grow
2443 * to their default size.
2445 * This function is to be called when a tracer is about to be used.
2447 int tracing_update_buffers(void)
2451 mutex_lock(&trace_types_lock
);
2452 if (!ring_buffer_expanded
)
2453 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2454 mutex_unlock(&trace_types_lock
);
2459 struct trace_option_dentry
;
2461 static struct trace_option_dentry
*
2462 create_trace_option_files(struct tracer
*tracer
);
2465 destroy_trace_option_files(struct trace_option_dentry
*topts
);
2467 static int tracing_set_tracer(const char *buf
)
2469 static struct trace_option_dentry
*topts
;
2470 struct trace_array
*tr
= &global_trace
;
2474 mutex_lock(&trace_types_lock
);
2476 if (!ring_buffer_expanded
) {
2477 ret
= tracing_resize_ring_buffer(trace_buf_size
);
2483 for (t
= trace_types
; t
; t
= t
->next
) {
2484 if (strcmp(t
->name
, buf
) == 0)
2491 if (t
== current_trace
)
2494 trace_branch_disable();
2495 if (current_trace
&& current_trace
->reset
)
2496 current_trace
->reset(tr
);
2498 destroy_trace_option_files(topts
);
2502 topts
= create_trace_option_files(current_trace
);
2505 ret
= tracer_init(t
, tr
);
2510 trace_branch_enable(tr
);
2512 mutex_unlock(&trace_types_lock
);
2518 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2519 size_t cnt
, loff_t
*ppos
)
2521 char buf
[max_tracer_type_len
+1];
2528 if (cnt
> max_tracer_type_len
)
2529 cnt
= max_tracer_type_len
;
2531 if (copy_from_user(&buf
, ubuf
, cnt
))
2536 /* strip ending whitespace. */
2537 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2540 err
= tracing_set_tracer(buf
);
2550 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2551 size_t cnt
, loff_t
*ppos
)
2553 unsigned long *ptr
= filp
->private_data
;
2557 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2558 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2559 if (r
> sizeof(buf
))
2561 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2565 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2566 size_t cnt
, loff_t
*ppos
)
2568 unsigned long *ptr
= filp
->private_data
;
2573 if (cnt
>= sizeof(buf
))
2576 if (copy_from_user(&buf
, ubuf
, cnt
))
2581 ret
= strict_strtoul(buf
, 10, &val
);
2590 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2592 long cpu_file
= (long) inode
->i_private
;
2593 struct trace_iterator
*iter
;
2596 if (tracing_disabled
)
2599 mutex_lock(&trace_types_lock
);
2601 /* We only allow one reader per cpu */
2602 if (cpu_file
== TRACE_PIPE_ALL_CPU
) {
2603 if (!cpumask_empty(tracing_reader_cpumask
)) {
2607 cpumask_setall(tracing_reader_cpumask
);
2609 if (!cpumask_test_cpu(cpu_file
, tracing_reader_cpumask
))
2610 cpumask_set_cpu(cpu_file
, tracing_reader_cpumask
);
2617 /* create a buffer to store the information to pass to userspace */
2618 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2625 * We make a copy of the current tracer to avoid concurrent
2626 * changes on it while we are reading.
2628 iter
->trace
= kmalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
2634 *iter
->trace
= *current_trace
;
2636 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2641 /* trace pipe does not show start of buffer */
2642 cpumask_setall(iter
->started
);
2644 if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
2645 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2647 iter
->cpu_file
= cpu_file
;
2648 iter
->tr
= &global_trace
;
2649 mutex_init(&iter
->mutex
);
2650 filp
->private_data
= iter
;
2652 if (iter
->trace
->pipe_open
)
2653 iter
->trace
->pipe_open(iter
);
2656 mutex_unlock(&trace_types_lock
);
2662 mutex_unlock(&trace_types_lock
);
2666 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2668 struct trace_iterator
*iter
= file
->private_data
;
2670 mutex_lock(&trace_types_lock
);
2672 if (iter
->cpu_file
== TRACE_PIPE_ALL_CPU
)
2673 cpumask_clear(tracing_reader_cpumask
);
2675 cpumask_clear_cpu(iter
->cpu_file
, tracing_reader_cpumask
);
2677 mutex_unlock(&trace_types_lock
);
2679 free_cpumask_var(iter
->started
);
2680 mutex_destroy(&iter
->mutex
);
2688 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2690 struct trace_iterator
*iter
= filp
->private_data
;
2692 if (trace_flags
& TRACE_ITER_BLOCK
) {
2694 * Always select as readable when in blocking mode
2696 return POLLIN
| POLLRDNORM
;
2698 if (!trace_empty(iter
))
2699 return POLLIN
| POLLRDNORM
;
2700 poll_wait(filp
, &trace_wait
, poll_table
);
2701 if (!trace_empty(iter
))
2702 return POLLIN
| POLLRDNORM
;
2709 void default_wait_pipe(struct trace_iterator
*iter
)
2713 prepare_to_wait(&trace_wait
, &wait
, TASK_INTERRUPTIBLE
);
2715 if (trace_empty(iter
))
2718 finish_wait(&trace_wait
, &wait
);
2722 * This is a make-shift waitqueue.
2723 * A tracer might use this callback on some rare cases:
2725 * 1) the current tracer might hold the runqueue lock when it wakes up
2726 * a reader, hence a deadlock (sched, function, and function graph tracers)
2727 * 2) the function tracers, trace all functions, we don't want
2728 * the overhead of calling wake_up and friends
2729 * (and tracing them too)
2731 * Anyway, this is really very primitive wakeup.
2733 void poll_wait_pipe(struct trace_iterator
*iter
)
2735 set_current_state(TASK_INTERRUPTIBLE
);
2736 /* sleep for 100 msecs, and try again. */
2737 schedule_timeout(HZ
/ 10);
2740 /* Must be called with trace_types_lock mutex held. */
2741 static int tracing_wait_pipe(struct file
*filp
)
2743 struct trace_iterator
*iter
= filp
->private_data
;
2745 while (trace_empty(iter
)) {
2747 if ((filp
->f_flags
& O_NONBLOCK
)) {
2751 mutex_unlock(&iter
->mutex
);
2753 iter
->trace
->wait_pipe(iter
);
2755 mutex_lock(&iter
->mutex
);
2757 if (signal_pending(current
))
2761 * We block until we read something and tracing is disabled.
2762 * We still block if tracing is disabled, but we have never
2763 * read anything. This allows a user to cat this file, and
2764 * then enable tracing. But after we have read something,
2765 * we give an EOF when tracing is again disabled.
2767 * iter->pos will be 0 if we haven't read anything.
2769 if (!tracer_enabled
&& iter
->pos
)
2780 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2781 size_t cnt
, loff_t
*ppos
)
2783 struct trace_iterator
*iter
= filp
->private_data
;
2784 static struct tracer
*old_tracer
;
2787 /* return any leftover data */
2788 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2792 trace_seq_init(&iter
->seq
);
2794 /* copy the tracer to avoid using a global lock all around */
2795 mutex_lock(&trace_types_lock
);
2796 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
2797 old_tracer
= current_trace
;
2798 *iter
->trace
= *current_trace
;
2800 mutex_unlock(&trace_types_lock
);
2803 * Avoid more than one consumer on a single file descriptor
2804 * This is just a matter of traces coherency, the ring buffer itself
2807 mutex_lock(&iter
->mutex
);
2808 if (iter
->trace
->read
) {
2809 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2815 sret
= tracing_wait_pipe(filp
);
2819 /* stop when tracing is finished */
2820 if (trace_empty(iter
)) {
2825 if (cnt
>= PAGE_SIZE
)
2826 cnt
= PAGE_SIZE
- 1;
2828 /* reset all but tr, trace, and overruns */
2829 memset(&iter
->seq
, 0,
2830 sizeof(struct trace_iterator
) -
2831 offsetof(struct trace_iterator
, seq
));
2834 trace_event_read_lock();
2835 while (find_next_entry_inc(iter
) != NULL
) {
2836 enum print_line_t ret
;
2837 int len
= iter
->seq
.len
;
2839 ret
= print_trace_line(iter
);
2840 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2841 /* don't print partial lines */
2842 iter
->seq
.len
= len
;
2845 if (ret
!= TRACE_TYPE_NO_CONSUME
)
2846 trace_consume(iter
);
2848 if (iter
->seq
.len
>= cnt
)
2851 trace_event_read_unlock();
2853 /* Now copy what we have to the user */
2854 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2855 if (iter
->seq
.readpos
>= iter
->seq
.len
)
2856 trace_seq_init(&iter
->seq
);
2859 * If there was nothing to send to user, inspite of consuming trace
2860 * entries, go back to wait for more entries.
2866 mutex_unlock(&iter
->mutex
);
2871 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
2872 struct pipe_buffer
*buf
)
2874 __free_page(buf
->page
);
2877 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
2880 __free_page(spd
->pages
[idx
]);
2883 static struct pipe_buf_operations tracing_pipe_buf_ops
= {
2885 .map
= generic_pipe_buf_map
,
2886 .unmap
= generic_pipe_buf_unmap
,
2887 .confirm
= generic_pipe_buf_confirm
,
2888 .release
= tracing_pipe_buf_release
,
2889 .steal
= generic_pipe_buf_steal
,
2890 .get
= generic_pipe_buf_get
,
2894 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
2899 /* Seq buffer is page-sized, exactly what we need. */
2901 count
= iter
->seq
.len
;
2902 ret
= print_trace_line(iter
);
2903 count
= iter
->seq
.len
- count
;
2906 iter
->seq
.len
-= count
;
2909 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2910 iter
->seq
.len
-= count
;
2914 if (ret
!= TRACE_TYPE_NO_CONSUME
)
2915 trace_consume(iter
);
2917 if (!find_next_entry_inc(iter
)) {
2927 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
2929 struct pipe_inode_info
*pipe
,
2933 struct page
*pages
[PIPE_BUFFERS
];
2934 struct partial_page partial
[PIPE_BUFFERS
];
2935 struct trace_iterator
*iter
= filp
->private_data
;
2936 struct splice_pipe_desc spd
= {
2939 .nr_pages
= 0, /* This gets updated below. */
2941 .ops
= &tracing_pipe_buf_ops
,
2942 .spd_release
= tracing_spd_release_pipe
,
2944 static struct tracer
*old_tracer
;
2949 /* copy the tracer to avoid using a global lock all around */
2950 mutex_lock(&trace_types_lock
);
2951 if (unlikely(old_tracer
!= current_trace
&& current_trace
)) {
2952 old_tracer
= current_trace
;
2953 *iter
->trace
= *current_trace
;
2955 mutex_unlock(&trace_types_lock
);
2957 mutex_lock(&iter
->mutex
);
2959 if (iter
->trace
->splice_read
) {
2960 ret
= iter
->trace
->splice_read(iter
, filp
,
2961 ppos
, pipe
, len
, flags
);
2966 ret
= tracing_wait_pipe(filp
);
2970 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
2975 trace_event_read_lock();
2977 /* Fill as many pages as possible. */
2978 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
2979 pages
[i
] = alloc_page(GFP_KERNEL
);
2983 rem
= tracing_fill_pipe_page(rem
, iter
);
2985 /* Copy the data into the page, so we can start over. */
2986 ret
= trace_seq_to_buffer(&iter
->seq
,
2987 page_address(pages
[i
]),
2990 __free_page(pages
[i
]);
2993 partial
[i
].offset
= 0;
2994 partial
[i
].len
= iter
->seq
.len
;
2996 trace_seq_init(&iter
->seq
);
2999 trace_event_read_unlock();
3000 mutex_unlock(&iter
->mutex
);
3004 return splice_to_pipe(pipe
, &spd
);
3007 mutex_unlock(&iter
->mutex
);
3013 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3014 size_t cnt
, loff_t
*ppos
)
3016 struct trace_array
*tr
= filp
->private_data
;
3020 mutex_lock(&trace_types_lock
);
3021 if (!ring_buffer_expanded
)
3022 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
3024 trace_buf_size
>> 10);
3026 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3027 mutex_unlock(&trace_types_lock
);
3029 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3033 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3034 size_t cnt
, loff_t
*ppos
)
3040 if (cnt
>= sizeof(buf
))
3043 if (copy_from_user(&buf
, ubuf
, cnt
))
3048 ret
= strict_strtoul(buf
, 10, &val
);
3052 /* must have at least 1 entry */
3056 mutex_lock(&trace_types_lock
);
3060 /* disable all cpu buffers */
3061 for_each_tracing_cpu(cpu
) {
3062 if (global_trace
.data
[cpu
])
3063 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3064 if (max_tr
.data
[cpu
])
3065 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3068 /* value is in KB */
3071 if (val
!= global_trace
.entries
) {
3072 ret
= tracing_resize_ring_buffer(val
);
3081 /* If check pages failed, return ENOMEM */
3082 if (tracing_disabled
)
3085 for_each_tracing_cpu(cpu
) {
3086 if (global_trace
.data
[cpu
])
3087 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3088 if (max_tr
.data
[cpu
])
3089 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3093 max_tr
.entries
= global_trace
.entries
;
3094 mutex_unlock(&trace_types_lock
);
3099 static int mark_printk(const char *fmt
, ...)
3103 va_start(args
, fmt
);
3104 ret
= trace_vprintk(0, fmt
, args
);
3110 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3111 size_t cnt
, loff_t
*fpos
)
3116 if (tracing_disabled
)
3119 if (cnt
> TRACE_BUF_SIZE
)
3120 cnt
= TRACE_BUF_SIZE
;
3122 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3126 if (copy_from_user(buf
, ubuf
, cnt
)) {
3131 /* Cut from the first nil or newline. */
3133 end
= strchr(buf
, '\n');
3137 cnt
= mark_printk("%s\n", buf
);
3144 static ssize_t
tracing_clock_read(struct file
*filp
, char __user
*ubuf
,
3145 size_t cnt
, loff_t
*ppos
)
3151 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++)
3152 bufiter
+= snprintf(buf
+ bufiter
, sizeof(buf
) - bufiter
,
3153 "%s%s%s%s", i
? " " : "",
3154 i
== trace_clock_id
? "[" : "", trace_clocks
[i
].name
,
3155 i
== trace_clock_id
? "]" : "");
3156 bufiter
+= snprintf(buf
+ bufiter
, sizeof(buf
) - bufiter
, "\n");
3158 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, bufiter
);
3161 static ssize_t
tracing_clock_write(struct file
*filp
, const char __user
*ubuf
,
3162 size_t cnt
, loff_t
*fpos
)
3165 const char *clockstr
;
3168 if (cnt
>= sizeof(buf
))
3171 if (copy_from_user(&buf
, ubuf
, cnt
))
3176 clockstr
= strstrip(buf
);
3178 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++) {
3179 if (strcmp(trace_clocks
[i
].name
, clockstr
) == 0)
3182 if (i
== ARRAY_SIZE(trace_clocks
))
3187 mutex_lock(&trace_types_lock
);
3189 ring_buffer_set_clock(global_trace
.buffer
, trace_clocks
[i
].func
);
3191 ring_buffer_set_clock(max_tr
.buffer
, trace_clocks
[i
].func
);
3193 mutex_unlock(&trace_types_lock
);
3200 static const struct file_operations tracing_max_lat_fops
= {
3201 .open
= tracing_open_generic
,
3202 .read
= tracing_max_lat_read
,
3203 .write
= tracing_max_lat_write
,
3206 static const struct file_operations tracing_ctrl_fops
= {
3207 .open
= tracing_open_generic
,
3208 .read
= tracing_ctrl_read
,
3209 .write
= tracing_ctrl_write
,
3212 static const struct file_operations set_tracer_fops
= {
3213 .open
= tracing_open_generic
,
3214 .read
= tracing_set_trace_read
,
3215 .write
= tracing_set_trace_write
,
3218 static const struct file_operations tracing_pipe_fops
= {
3219 .open
= tracing_open_pipe
,
3220 .poll
= tracing_poll_pipe
,
3221 .read
= tracing_read_pipe
,
3222 .splice_read
= tracing_splice_read_pipe
,
3223 .release
= tracing_release_pipe
,
3226 static const struct file_operations tracing_entries_fops
= {
3227 .open
= tracing_open_generic
,
3228 .read
= tracing_entries_read
,
3229 .write
= tracing_entries_write
,
3232 static const struct file_operations tracing_mark_fops
= {
3233 .open
= tracing_open_generic
,
3234 .write
= tracing_mark_write
,
3237 static const struct file_operations trace_clock_fops
= {
3238 .open
= tracing_open_generic
,
3239 .read
= tracing_clock_read
,
3240 .write
= tracing_clock_write
,
3243 struct ftrace_buffer_info
{
3244 struct trace_array
*tr
;
3250 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
3252 int cpu
= (int)(long)inode
->i_private
;
3253 struct ftrace_buffer_info
*info
;
3255 if (tracing_disabled
)
3258 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
3262 info
->tr
= &global_trace
;
3265 /* Force reading ring buffer for first read */
3266 info
->read
= (unsigned int)-1;
3268 filp
->private_data
= info
;
3270 return nonseekable_open(inode
, filp
);
3274 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
3275 size_t count
, loff_t
*ppos
)
3277 struct ftrace_buffer_info
*info
= filp
->private_data
;
3286 info
->spare
= ring_buffer_alloc_read_page(info
->tr
->buffer
);
3290 /* Do we have previous read data to read? */
3291 if (info
->read
< PAGE_SIZE
)
3296 ret
= ring_buffer_read_page(info
->tr
->buffer
,
3303 pos
= ring_buffer_page_len(info
->spare
);
3305 if (pos
< PAGE_SIZE
)
3306 memset(info
->spare
+ pos
, 0, PAGE_SIZE
- pos
);
3309 size
= PAGE_SIZE
- info
->read
;
3313 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
3324 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
3326 struct ftrace_buffer_info
*info
= file
->private_data
;
3329 ring_buffer_free_read_page(info
->tr
->buffer
, info
->spare
);
3336 struct ring_buffer
*buffer
;
3341 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
3342 struct pipe_buffer
*buf
)
3344 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3349 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3354 static int buffer_pipe_buf_steal(struct pipe_inode_info
*pipe
,
3355 struct pipe_buffer
*buf
)
3360 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
3361 struct pipe_buffer
*buf
)
3363 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
3368 /* Pipe buffer operations for a buffer. */
3369 static struct pipe_buf_operations buffer_pipe_buf_ops
= {
3371 .map
= generic_pipe_buf_map
,
3372 .unmap
= generic_pipe_buf_unmap
,
3373 .confirm
= generic_pipe_buf_confirm
,
3374 .release
= buffer_pipe_buf_release
,
3375 .steal
= buffer_pipe_buf_steal
,
3376 .get
= buffer_pipe_buf_get
,
3380 * Callback from splice_to_pipe(), if we need to release some pages
3381 * at the end of the spd in case we error'ed out in filling the pipe.
3383 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
3385 struct buffer_ref
*ref
=
3386 (struct buffer_ref
*)spd
->partial
[i
].private;
3391 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
3393 spd
->partial
[i
].private = 0;
3397 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
3398 struct pipe_inode_info
*pipe
, size_t len
,
3401 struct ftrace_buffer_info
*info
= file
->private_data
;
3402 struct partial_page partial
[PIPE_BUFFERS
];
3403 struct page
*pages
[PIPE_BUFFERS
];
3404 struct splice_pipe_desc spd
= {
3408 .ops
= &buffer_pipe_buf_ops
,
3409 .spd_release
= buffer_spd_release
,
3411 struct buffer_ref
*ref
;
3412 int entries
, size
, i
;
3415 if (*ppos
& (PAGE_SIZE
- 1)) {
3416 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3420 if (len
& (PAGE_SIZE
- 1)) {
3421 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3422 if (len
< PAGE_SIZE
)
3427 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3429 for (i
= 0; i
< PIPE_BUFFERS
&& len
&& entries
; i
++, len
-= PAGE_SIZE
) {
3433 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
3438 ref
->buffer
= info
->tr
->buffer
;
3439 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
);
3445 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
3448 ring_buffer_free_read_page(ref
->buffer
,
3455 * zero out any left over data, this is going to
3458 size
= ring_buffer_page_len(ref
->page
);
3459 if (size
< PAGE_SIZE
)
3460 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
3462 page
= virt_to_page(ref
->page
);
3464 spd
.pages
[i
] = page
;
3465 spd
.partial
[i
].len
= PAGE_SIZE
;
3466 spd
.partial
[i
].offset
= 0;
3467 spd
.partial
[i
].private = (unsigned long)ref
;
3471 entries
= ring_buffer_entries_cpu(info
->tr
->buffer
, info
->cpu
);
3476 /* did we read anything? */
3477 if (!spd
.nr_pages
) {
3478 if (flags
& SPLICE_F_NONBLOCK
)
3486 ret
= splice_to_pipe(pipe
, &spd
);
3491 static const struct file_operations tracing_buffers_fops
= {
3492 .open
= tracing_buffers_open
,
3493 .read
= tracing_buffers_read
,
3494 .release
= tracing_buffers_release
,
3495 .splice_read
= tracing_buffers_splice_read
,
3496 .llseek
= no_llseek
,
3500 tracing_stats_read(struct file
*filp
, char __user
*ubuf
,
3501 size_t count
, loff_t
*ppos
)
3503 unsigned long cpu
= (unsigned long)filp
->private_data
;
3504 struct trace_array
*tr
= &global_trace
;
3505 struct trace_seq
*s
;
3508 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
3514 cnt
= ring_buffer_entries_cpu(tr
->buffer
, cpu
);
3515 trace_seq_printf(s
, "entries: %ld\n", cnt
);
3517 cnt
= ring_buffer_overrun_cpu(tr
->buffer
, cpu
);
3518 trace_seq_printf(s
, "overrun: %ld\n", cnt
);
3520 cnt
= ring_buffer_commit_overrun_cpu(tr
->buffer
, cpu
);
3521 trace_seq_printf(s
, "commit overrun: %ld\n", cnt
);
3523 count
= simple_read_from_buffer(ubuf
, count
, ppos
, s
->buffer
, s
->len
);
3530 static const struct file_operations tracing_stats_fops
= {
3531 .open
= tracing_open_generic
,
3532 .read
= tracing_stats_read
,
3535 #ifdef CONFIG_DYNAMIC_FTRACE
3537 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3543 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3544 size_t cnt
, loff_t
*ppos
)
3546 static char ftrace_dyn_info_buffer
[1024];
3547 static DEFINE_MUTEX(dyn_info_mutex
);
3548 unsigned long *p
= filp
->private_data
;
3549 char *buf
= ftrace_dyn_info_buffer
;
3550 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3553 mutex_lock(&dyn_info_mutex
);
3554 r
= sprintf(buf
, "%ld ", *p
);
3556 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3559 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3561 mutex_unlock(&dyn_info_mutex
);
3566 static const struct file_operations tracing_dyn_info_fops
= {
3567 .open
= tracing_open_generic
,
3568 .read
= tracing_read_dyn_info
,
3572 static struct dentry
*d_tracer
;
3574 struct dentry
*tracing_init_dentry(void)
3581 if (!debugfs_initialized())
3584 d_tracer
= debugfs_create_dir("tracing", NULL
);
3586 if (!d_tracer
&& !once
) {
3588 pr_warning("Could not create debugfs directory 'tracing'\n");
3595 static struct dentry
*d_percpu
;
3597 struct dentry
*tracing_dentry_percpu(void)
3600 struct dentry
*d_tracer
;
3605 d_tracer
= tracing_init_dentry();
3610 d_percpu
= debugfs_create_dir("per_cpu", d_tracer
);
3612 if (!d_percpu
&& !once
) {
3614 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3621 static void tracing_init_debugfs_percpu(long cpu
)
3623 struct dentry
*d_percpu
= tracing_dentry_percpu();
3624 struct dentry
*d_cpu
;
3625 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3628 if (cpu
> 999 || cpu
< 0)
3631 sprintf(cpu_dir
, "cpu%ld", cpu
);
3632 d_cpu
= debugfs_create_dir(cpu_dir
, d_percpu
);
3634 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir
);
3638 /* per cpu trace_pipe */
3639 trace_create_file("trace_pipe", 0444, d_cpu
,
3640 (void *) cpu
, &tracing_pipe_fops
);
3643 trace_create_file("trace", 0644, d_cpu
,
3644 (void *) cpu
, &tracing_fops
);
3646 trace_create_file("trace_pipe_raw", 0444, d_cpu
,
3647 (void *) cpu
, &tracing_buffers_fops
);
3649 trace_create_file("stats", 0444, d_cpu
,
3650 (void *) cpu
, &tracing_stats_fops
);
3653 #ifdef CONFIG_FTRACE_SELFTEST
3654 /* Let selftest have access to static functions in this file */
3655 #include "trace_selftest.c"
3658 struct trace_option_dentry
{
3659 struct tracer_opt
*opt
;
3660 struct tracer_flags
*flags
;
3661 struct dentry
*entry
;
3665 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3668 struct trace_option_dentry
*topt
= filp
->private_data
;
3671 if (topt
->flags
->val
& topt
->opt
->bit
)
3676 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3680 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3683 struct trace_option_dentry
*topt
= filp
->private_data
;
3688 if (cnt
>= sizeof(buf
))
3691 if (copy_from_user(&buf
, ubuf
, cnt
))
3696 ret
= strict_strtoul(buf
, 10, &val
);
3703 /* do nothing if already cleared */
3704 if (!(topt
->flags
->val
& topt
->opt
->bit
))
3707 mutex_lock(&trace_types_lock
);
3708 if (current_trace
->set_flag
)
3709 ret
= current_trace
->set_flag(topt
->flags
->val
,
3711 mutex_unlock(&trace_types_lock
);
3714 topt
->flags
->val
&= ~topt
->opt
->bit
;
3717 /* do nothing if already set */
3718 if (topt
->flags
->val
& topt
->opt
->bit
)
3721 mutex_lock(&trace_types_lock
);
3722 if (current_trace
->set_flag
)
3723 ret
= current_trace
->set_flag(topt
->flags
->val
,
3725 mutex_unlock(&trace_types_lock
);
3728 topt
->flags
->val
|= topt
->opt
->bit
;
3741 static const struct file_operations trace_options_fops
= {
3742 .open
= tracing_open_generic
,
3743 .read
= trace_options_read
,
3744 .write
= trace_options_write
,
3748 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
3751 long index
= (long)filp
->private_data
;
3754 if (trace_flags
& (1 << index
))
3759 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
3763 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
3766 long index
= (long)filp
->private_data
;
3771 if (cnt
>= sizeof(buf
))
3774 if (copy_from_user(&buf
, ubuf
, cnt
))
3779 ret
= strict_strtoul(buf
, 10, &val
);
3785 trace_flags
&= ~(1 << index
);
3788 trace_flags
|= 1 << index
;
3800 static const struct file_operations trace_options_core_fops
= {
3801 .open
= tracing_open_generic
,
3802 .read
= trace_options_core_read
,
3803 .write
= trace_options_core_write
,
3806 struct dentry
*trace_create_file(const char *name
,
3808 struct dentry
*parent
,
3810 const struct file_operations
*fops
)
3814 ret
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
3816 pr_warning("Could not create debugfs '%s' entry\n", name
);
3822 static struct dentry
*trace_options_init_dentry(void)
3824 struct dentry
*d_tracer
;
3825 static struct dentry
*t_options
;
3830 d_tracer
= tracing_init_dentry();
3834 t_options
= debugfs_create_dir("options", d_tracer
);
3836 pr_warning("Could not create debugfs directory 'options'\n");
3844 create_trace_option_file(struct trace_option_dentry
*topt
,
3845 struct tracer_flags
*flags
,
3846 struct tracer_opt
*opt
)
3848 struct dentry
*t_options
;
3850 t_options
= trace_options_init_dentry();
3854 topt
->flags
= flags
;
3857 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
3858 &trace_options_fops
);
3862 static struct trace_option_dentry
*
3863 create_trace_option_files(struct tracer
*tracer
)
3865 struct trace_option_dentry
*topts
;
3866 struct tracer_flags
*flags
;
3867 struct tracer_opt
*opts
;
3873 flags
= tracer
->flags
;
3875 if (!flags
|| !flags
->opts
)
3880 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3883 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
3887 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
3888 create_trace_option_file(&topts
[cnt
], flags
,
3895 destroy_trace_option_files(struct trace_option_dentry
*topts
)
3902 for (cnt
= 0; topts
[cnt
].opt
; cnt
++) {
3903 if (topts
[cnt
].entry
)
3904 debugfs_remove(topts
[cnt
].entry
);
3910 static struct dentry
*
3911 create_trace_option_core_file(const char *option
, long index
)
3913 struct dentry
*t_options
;
3915 t_options
= trace_options_init_dentry();
3919 return trace_create_file(option
, 0644, t_options
, (void *)index
,
3920 &trace_options_core_fops
);
3923 static __init
void create_trace_options_dir(void)
3925 struct dentry
*t_options
;
3928 t_options
= trace_options_init_dentry();
3932 for (i
= 0; trace_options
[i
]; i
++)
3933 create_trace_option_core_file(trace_options
[i
], i
);
3936 static __init
int tracer_init_debugfs(void)
3938 struct dentry
*d_tracer
;
3941 d_tracer
= tracing_init_dentry();
3943 trace_create_file("tracing_enabled", 0644, d_tracer
,
3944 &global_trace
, &tracing_ctrl_fops
);
3946 trace_create_file("trace_options", 0644, d_tracer
,
3947 NULL
, &tracing_iter_fops
);
3949 trace_create_file("tracing_cpumask", 0644, d_tracer
,
3950 NULL
, &tracing_cpumask_fops
);
3952 trace_create_file("trace", 0644, d_tracer
,
3953 (void *) TRACE_PIPE_ALL_CPU
, &tracing_fops
);
3955 trace_create_file("available_tracers", 0444, d_tracer
,
3956 &global_trace
, &show_traces_fops
);
3958 trace_create_file("current_tracer", 0644, d_tracer
,
3959 &global_trace
, &set_tracer_fops
);
3961 #ifdef CONFIG_TRACER_MAX_TRACE
3962 trace_create_file("tracing_max_latency", 0644, d_tracer
,
3963 &tracing_max_latency
, &tracing_max_lat_fops
);
3965 trace_create_file("tracing_thresh", 0644, d_tracer
,
3966 &tracing_thresh
, &tracing_max_lat_fops
);
3969 trace_create_file("README", 0444, d_tracer
,
3970 NULL
, &tracing_readme_fops
);
3972 trace_create_file("trace_pipe", 0444, d_tracer
,
3973 (void *) TRACE_PIPE_ALL_CPU
, &tracing_pipe_fops
);
3975 trace_create_file("buffer_size_kb", 0644, d_tracer
,
3976 &global_trace
, &tracing_entries_fops
);
3978 trace_create_file("trace_marker", 0220, d_tracer
,
3979 NULL
, &tracing_mark_fops
);
3981 trace_create_file("saved_cmdlines", 0444, d_tracer
,
3982 NULL
, &tracing_saved_cmdlines_fops
);
3984 trace_create_file("trace_clock", 0644, d_tracer
, NULL
,
3987 #ifdef CONFIG_DYNAMIC_FTRACE
3988 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
3989 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
3991 #ifdef CONFIG_SYSPROF_TRACER
3992 init_tracer_sysprof_debugfs(d_tracer
);
3995 create_trace_options_dir();
3997 for_each_tracing_cpu(cpu
)
3998 tracing_init_debugfs_percpu(cpu
);
4003 static int trace_panic_handler(struct notifier_block
*this,
4004 unsigned long event
, void *unused
)
4006 if (ftrace_dump_on_oops
)
4011 static struct notifier_block trace_panic_notifier
= {
4012 .notifier_call
= trace_panic_handler
,
4014 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
4017 static int trace_die_handler(struct notifier_block
*self
,
4023 if (ftrace_dump_on_oops
)
4032 static struct notifier_block trace_die_notifier
= {
4033 .notifier_call
= trace_die_handler
,
4038 * printk is set to max of 1024, we really don't need it that big.
4039 * Nothing should be printing 1000 characters anyway.
4041 #define TRACE_MAX_PRINT 1000
4044 * Define here KERN_TRACE so that we have one place to modify
4045 * it if we decide to change what log level the ftrace dump
4048 #define KERN_TRACE KERN_EMERG
4051 trace_printk_seq(struct trace_seq
*s
)
4053 /* Probably should print a warning here. */
4057 /* should be zero ended, but we are paranoid. */
4058 s
->buffer
[s
->len
] = 0;
4060 printk(KERN_TRACE
"%s", s
->buffer
);
4065 static void __ftrace_dump(bool disable_tracing
)
4067 static raw_spinlock_t ftrace_dump_lock
=
4068 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
4069 /* use static because iter can be a bit big for the stack */
4070 static struct trace_iterator iter
;
4071 unsigned int old_userobj
;
4072 static int dump_ran
;
4073 unsigned long flags
;
4077 local_irq_save(flags
);
4078 __raw_spin_lock(&ftrace_dump_lock
);
4086 if (disable_tracing
)
4089 for_each_tracing_cpu(cpu
) {
4090 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
4093 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
4095 /* don't look at user memory in panic mode */
4096 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
4098 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
4100 /* Simulate the iterator */
4101 iter
.tr
= &global_trace
;
4102 iter
.trace
= current_trace
;
4103 iter
.cpu_file
= TRACE_PIPE_ALL_CPU
;
4106 * We need to stop all tracing on all CPUS to read the
4107 * the next buffer. This is a bit expensive, but is
4108 * not done often. We fill all what we can read,
4109 * and then release the locks again.
4112 while (!trace_empty(&iter
)) {
4115 printk(KERN_TRACE
"---------------------------------\n");
4119 /* reset all but tr, trace, and overruns */
4120 memset(&iter
.seq
, 0,
4121 sizeof(struct trace_iterator
) -
4122 offsetof(struct trace_iterator
, seq
));
4123 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
4126 if (find_next_entry_inc(&iter
) != NULL
) {
4129 ret
= print_trace_line(&iter
);
4130 if (ret
!= TRACE_TYPE_NO_CONSUME
)
4131 trace_consume(&iter
);
4134 trace_printk_seq(&iter
.seq
);
4138 printk(KERN_TRACE
" (ftrace buffer empty)\n");
4140 printk(KERN_TRACE
"---------------------------------\n");
4142 /* Re-enable tracing if requested */
4143 if (!disable_tracing
) {
4144 trace_flags
|= old_userobj
;
4146 for_each_tracing_cpu(cpu
) {
4147 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
4153 __raw_spin_unlock(&ftrace_dump_lock
);
4154 local_irq_restore(flags
);
4157 /* By default: disable tracing after the dump */
4158 void ftrace_dump(void)
4160 __ftrace_dump(true);
4163 __init
static int tracer_alloc_buffers(void)
4169 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
4172 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
4173 goto out_free_buffer_mask
;
4175 if (!alloc_cpumask_var(&tracing_reader_cpumask
, GFP_KERNEL
))
4176 goto out_free_tracing_cpumask
;
4178 /* To save memory, keep the ring buffer size to its minimum */
4179 if (ring_buffer_expanded
)
4180 ring_buf_size
= trace_buf_size
;
4184 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
4185 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
4186 cpumask_clear(tracing_reader_cpumask
);
4188 /* TODO: make the number of buffers hot pluggable with CPUS */
4189 global_trace
.buffer
= ring_buffer_alloc(ring_buf_size
,
4190 TRACE_BUFFER_FLAGS
);
4191 if (!global_trace
.buffer
) {
4192 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
4194 goto out_free_cpumask
;
4196 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
4199 #ifdef CONFIG_TRACER_MAX_TRACE
4200 max_tr
.buffer
= ring_buffer_alloc(ring_buf_size
,
4201 TRACE_BUFFER_FLAGS
);
4202 if (!max_tr
.buffer
) {
4203 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
4205 ring_buffer_free(global_trace
.buffer
);
4206 goto out_free_cpumask
;
4208 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
4209 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
4212 /* Allocate the first page for all buffers */
4213 for_each_tracing_cpu(i
) {
4214 global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
4215 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
4218 trace_init_cmdlines();
4220 register_tracer(&nop_trace
);
4221 current_trace
= &nop_trace
;
4222 #ifdef CONFIG_BOOT_TRACER
4223 register_tracer(&boot_tracer
);
4225 /* All seems OK, enable tracing */
4226 tracing_disabled
= 0;
4228 atomic_notifier_chain_register(&panic_notifier_list
,
4229 &trace_panic_notifier
);
4231 register_die_notifier(&trace_die_notifier
);
4236 free_cpumask_var(tracing_reader_cpumask
);
4237 out_free_tracing_cpumask
:
4238 free_cpumask_var(tracing_cpumask
);
4239 out_free_buffer_mask
:
4240 free_cpumask_var(tracing_buffer_mask
);
4245 __init
static int clear_boot_tracer(void)
4248 * The default tracer at boot buffer is an init section.
4249 * This function is called in lateinit. If we did not
4250 * find the boot tracer, then clear it out, to prevent
4251 * later registration from accessing the buffer that is
4252 * about to be freed.
4254 if (!default_bootup_tracer
)
4257 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
4258 default_bootup_tracer
);
4259 default_bootup_tracer
= NULL
;
4264 early_initcall(tracer_alloc_buffers
);
4265 fs_initcall(tracer_init_debugfs
);
4266 late_initcall(clear_boot_tracer
);