2 * ring buffer based function tracer
4 * Copyright (C) 2007-2012 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 Nadia Yvette Chambers
14 #include <linux/ring_buffer.h>
15 #include <generated/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/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/tracefs.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/mount.h>
36 #include <linux/rwsem.h>
37 #include <linux/slab.h>
38 #include <linux/ctype.h>
39 #include <linux/init.h>
40 #include <linux/poll.h>
41 #include <linux/nmi.h>
43 #include <linux/sched/rt.h>
46 #include "trace_output.h"
49 * On boot up, the ring buffer is set to the minimum size, so that
50 * we do not waste memory on systems that are not using tracing.
52 bool ring_buffer_expanded
;
55 * We need to change this state when a selftest is running.
56 * A selftest will lurk into the ring-buffer to count the
57 * entries inserted during the selftest although some concurrent
58 * insertions into the ring-buffer such as trace_printk could occurred
59 * at the same time, giving false positive or negative results.
61 static bool __read_mostly tracing_selftest_running
;
64 * If a tracer is running, we do not want to run SELFTEST.
66 bool __read_mostly tracing_selftest_disabled
;
68 /* Pipe tracepoints to printk */
69 struct trace_iterator
*tracepoint_print_iter
;
70 int tracepoint_printk
;
72 /* For tracers that don't implement custom flags */
73 static struct tracer_opt dummy_tracer_opt
[] = {
77 static struct tracer_flags dummy_tracer_flags
= {
79 .opts
= dummy_tracer_opt
83 dummy_set_flag(struct trace_array
*tr
, u32 old_flags
, u32 bit
, int set
)
89 * To prevent the comm cache from being overwritten when no
90 * tracing is active, only save the comm when a trace event
93 static DEFINE_PER_CPU(bool, trace_cmdline_save
);
96 * Kill all tracing for good (never come back).
97 * It is initialized to 1 but will turn to zero if the initialization
98 * of the tracer is successful. But that is the only place that sets
101 static int tracing_disabled
= 1;
103 DEFINE_PER_CPU(int, ftrace_cpu_disabled
);
105 cpumask_var_t __read_mostly tracing_buffer_mask
;
108 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
110 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
111 * is set, then ftrace_dump is called. This will output the contents
112 * of the ftrace buffers to the console. This is very useful for
113 * capturing traces that lead to crashes and outputing it to a
116 * It is default off, but you can enable it with either specifying
117 * "ftrace_dump_on_oops" in the kernel command line, or setting
118 * /proc/sys/kernel/ftrace_dump_on_oops
119 * Set 1 if you want to dump buffers of all CPUs
120 * Set 2 if you want to dump the buffer of the CPU that triggered oops
123 enum ftrace_dump_mode ftrace_dump_on_oops
;
125 /* When set, tracing will stop when a WARN*() is hit */
126 int __disable_trace_on_warning
;
128 #ifdef CONFIG_TRACE_ENUM_MAP_FILE
129 /* Map of enums to their values, for "enum_map" file */
130 struct trace_enum_map_head
{
132 unsigned long length
;
135 union trace_enum_map_item
;
137 struct trace_enum_map_tail
{
139 * "end" is first and points to NULL as it must be different
140 * than "mod" or "enum_string"
142 union trace_enum_map_item
*next
;
143 const char *end
; /* points to NULL */
146 static DEFINE_MUTEX(trace_enum_mutex
);
149 * The trace_enum_maps are saved in an array with two extra elements,
150 * one at the beginning, and one at the end. The beginning item contains
151 * the count of the saved maps (head.length), and the module they
152 * belong to if not built in (head.mod). The ending item contains a
153 * pointer to the next array of saved enum_map items.
155 union trace_enum_map_item
{
156 struct trace_enum_map map
;
157 struct trace_enum_map_head head
;
158 struct trace_enum_map_tail tail
;
161 static union trace_enum_map_item
*trace_enum_maps
;
162 #endif /* CONFIG_TRACE_ENUM_MAP_FILE */
164 static int tracing_set_tracer(struct trace_array
*tr
, const char *buf
);
166 #define MAX_TRACER_SIZE 100
167 static char bootup_tracer_buf
[MAX_TRACER_SIZE
] __initdata
;
168 static char *default_bootup_tracer
;
170 static bool allocate_snapshot
;
172 static int __init
set_cmdline_ftrace(char *str
)
174 strlcpy(bootup_tracer_buf
, str
, MAX_TRACER_SIZE
);
175 default_bootup_tracer
= bootup_tracer_buf
;
176 /* We are using ftrace early, expand it */
177 ring_buffer_expanded
= true;
180 __setup("ftrace=", set_cmdline_ftrace
);
182 static int __init
set_ftrace_dump_on_oops(char *str
)
184 if (*str
++ != '=' || !*str
) {
185 ftrace_dump_on_oops
= DUMP_ALL
;
189 if (!strcmp("orig_cpu", str
)) {
190 ftrace_dump_on_oops
= DUMP_ORIG
;
196 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
198 static int __init
stop_trace_on_warning(char *str
)
200 if ((strcmp(str
, "=0") != 0 && strcmp(str
, "=off") != 0))
201 __disable_trace_on_warning
= 1;
204 __setup("traceoff_on_warning", stop_trace_on_warning
);
206 static int __init
boot_alloc_snapshot(char *str
)
208 allocate_snapshot
= true;
209 /* We also need the main ring buffer expanded */
210 ring_buffer_expanded
= true;
213 __setup("alloc_snapshot", boot_alloc_snapshot
);
216 static char trace_boot_options_buf
[MAX_TRACER_SIZE
] __initdata
;
217 static char *trace_boot_options __initdata
;
219 static int __init
set_trace_boot_options(char *str
)
221 strlcpy(trace_boot_options_buf
, str
, MAX_TRACER_SIZE
);
222 trace_boot_options
= trace_boot_options_buf
;
225 __setup("trace_options=", set_trace_boot_options
);
227 static char trace_boot_clock_buf
[MAX_TRACER_SIZE
] __initdata
;
228 static char *trace_boot_clock __initdata
;
230 static int __init
set_trace_boot_clock(char *str
)
232 strlcpy(trace_boot_clock_buf
, str
, MAX_TRACER_SIZE
);
233 trace_boot_clock
= trace_boot_clock_buf
;
236 __setup("trace_clock=", set_trace_boot_clock
);
238 static int __init
set_tracepoint_printk(char *str
)
240 if ((strcmp(str
, "=0") != 0 && strcmp(str
, "=off") != 0))
241 tracepoint_printk
= 1;
244 __setup("tp_printk", set_tracepoint_printk
);
246 unsigned long long ns2usecs(cycle_t nsec
)
254 * The global_trace is the descriptor that holds the tracing
255 * buffers for the live tracing. For each CPU, it contains
256 * a link list of pages that will store trace entries. The
257 * page descriptor of the pages in the memory is used to hold
258 * the link list by linking the lru item in the page descriptor
259 * to each of the pages in the buffer per CPU.
261 * For each active CPU there is a data field that holds the
262 * pages for the buffer for that CPU. Each CPU has the same number
263 * of pages allocated for its buffer.
265 static struct trace_array global_trace
;
267 LIST_HEAD(ftrace_trace_arrays
);
269 int trace_array_get(struct trace_array
*this_tr
)
271 struct trace_array
*tr
;
274 mutex_lock(&trace_types_lock
);
275 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
282 mutex_unlock(&trace_types_lock
);
287 static void __trace_array_put(struct trace_array
*this_tr
)
289 WARN_ON(!this_tr
->ref
);
293 void trace_array_put(struct trace_array
*this_tr
)
295 mutex_lock(&trace_types_lock
);
296 __trace_array_put(this_tr
);
297 mutex_unlock(&trace_types_lock
);
300 int filter_check_discard(struct ftrace_event_file
*file
, void *rec
,
301 struct ring_buffer
*buffer
,
302 struct ring_buffer_event
*event
)
304 if (unlikely(file
->flags
& FTRACE_EVENT_FL_FILTERED
) &&
305 !filter_match_preds(file
->filter
, rec
)) {
306 ring_buffer_discard_commit(buffer
, event
);
312 EXPORT_SYMBOL_GPL(filter_check_discard
);
314 int call_filter_check_discard(struct ftrace_event_call
*call
, void *rec
,
315 struct ring_buffer
*buffer
,
316 struct ring_buffer_event
*event
)
318 if (unlikely(call
->flags
& TRACE_EVENT_FL_FILTERED
) &&
319 !filter_match_preds(call
->filter
, rec
)) {
320 ring_buffer_discard_commit(buffer
, event
);
326 EXPORT_SYMBOL_GPL(call_filter_check_discard
);
328 static cycle_t
buffer_ftrace_now(struct trace_buffer
*buf
, int cpu
)
332 /* Early boot up does not have a buffer yet */
334 return trace_clock_local();
336 ts
= ring_buffer_time_stamp(buf
->buffer
, cpu
);
337 ring_buffer_normalize_time_stamp(buf
->buffer
, cpu
, &ts
);
342 cycle_t
ftrace_now(int cpu
)
344 return buffer_ftrace_now(&global_trace
.trace_buffer
, cpu
);
348 * tracing_is_enabled - Show if global_trace has been disabled
350 * Shows if the global trace has been enabled or not. It uses the
351 * mirror flag "buffer_disabled" to be used in fast paths such as for
352 * the irqsoff tracer. But it may be inaccurate due to races. If you
353 * need to know the accurate state, use tracing_is_on() which is a little
354 * slower, but accurate.
356 int tracing_is_enabled(void)
359 * For quick access (irqsoff uses this in fast path), just
360 * return the mirror variable of the state of the ring buffer.
361 * It's a little racy, but we don't really care.
364 return !global_trace
.buffer_disabled
;
368 * trace_buf_size is the size in bytes that is allocated
369 * for a buffer. Note, the number of bytes is always rounded
372 * This number is purposely set to a low number of 16384.
373 * If the dump on oops happens, it will be much appreciated
374 * to not have to wait for all that output. Anyway this can be
375 * boot time and run time configurable.
377 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
379 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
381 /* trace_types holds a link list of available tracers. */
382 static struct tracer
*trace_types __read_mostly
;
385 * trace_types_lock is used to protect the trace_types list.
387 DEFINE_MUTEX(trace_types_lock
);
390 * serialize the access of the ring buffer
392 * ring buffer serializes readers, but it is low level protection.
393 * The validity of the events (which returns by ring_buffer_peek() ..etc)
394 * are not protected by ring buffer.
396 * The content of events may become garbage if we allow other process consumes
397 * these events concurrently:
398 * A) the page of the consumed events may become a normal page
399 * (not reader page) in ring buffer, and this page will be rewrited
400 * by events producer.
401 * B) The page of the consumed events may become a page for splice_read,
402 * and this page will be returned to system.
404 * These primitives allow multi process access to different cpu ring buffer
407 * These primitives don't distinguish read-only and read-consume access.
408 * Multi read-only access are also serialized.
412 static DECLARE_RWSEM(all_cpu_access_lock
);
413 static DEFINE_PER_CPU(struct mutex
, cpu_access_lock
);
415 static inline void trace_access_lock(int cpu
)
417 if (cpu
== RING_BUFFER_ALL_CPUS
) {
418 /* gain it for accessing the whole ring buffer. */
419 down_write(&all_cpu_access_lock
);
421 /* gain it for accessing a cpu ring buffer. */
423 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
424 down_read(&all_cpu_access_lock
);
426 /* Secondly block other access to this @cpu ring buffer. */
427 mutex_lock(&per_cpu(cpu_access_lock
, cpu
));
431 static inline void trace_access_unlock(int cpu
)
433 if (cpu
== RING_BUFFER_ALL_CPUS
) {
434 up_write(&all_cpu_access_lock
);
436 mutex_unlock(&per_cpu(cpu_access_lock
, cpu
));
437 up_read(&all_cpu_access_lock
);
441 static inline void trace_access_lock_init(void)
445 for_each_possible_cpu(cpu
)
446 mutex_init(&per_cpu(cpu_access_lock
, cpu
));
451 static DEFINE_MUTEX(access_lock
);
453 static inline void trace_access_lock(int cpu
)
456 mutex_lock(&access_lock
);
459 static inline void trace_access_unlock(int cpu
)
462 mutex_unlock(&access_lock
);
465 static inline void trace_access_lock_init(void)
471 /* trace_flags holds trace_options default values */
472 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
473 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
| TRACE_ITER_SLEEP_TIME
|
474 TRACE_ITER_GRAPH_TIME
| TRACE_ITER_RECORD_CMD
| TRACE_ITER_OVERWRITE
|
475 TRACE_ITER_IRQ_INFO
| TRACE_ITER_MARKERS
| TRACE_ITER_FUNCTION
;
477 static void tracer_tracing_on(struct trace_array
*tr
)
479 if (tr
->trace_buffer
.buffer
)
480 ring_buffer_record_on(tr
->trace_buffer
.buffer
);
482 * This flag is looked at when buffers haven't been allocated
483 * yet, or by some tracers (like irqsoff), that just want to
484 * know if the ring buffer has been disabled, but it can handle
485 * races of where it gets disabled but we still do a record.
486 * As the check is in the fast path of the tracers, it is more
487 * important to be fast than accurate.
489 tr
->buffer_disabled
= 0;
490 /* Make the flag seen by readers */
495 * tracing_on - enable tracing buffers
497 * This function enables tracing buffers that may have been
498 * disabled with tracing_off.
500 void tracing_on(void)
502 tracer_tracing_on(&global_trace
);
504 EXPORT_SYMBOL_GPL(tracing_on
);
507 * __trace_puts - write a constant string into the trace buffer.
508 * @ip: The address of the caller
509 * @str: The constant string to write
510 * @size: The size of the string.
512 int __trace_puts(unsigned long ip
, const char *str
, int size
)
514 struct ring_buffer_event
*event
;
515 struct ring_buffer
*buffer
;
516 struct print_entry
*entry
;
517 unsigned long irq_flags
;
521 if (!(trace_flags
& TRACE_ITER_PRINTK
))
524 pc
= preempt_count();
526 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
529 alloc
= sizeof(*entry
) + size
+ 2; /* possible \n added */
531 local_save_flags(irq_flags
);
532 buffer
= global_trace
.trace_buffer
.buffer
;
533 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, alloc
,
538 entry
= ring_buffer_event_data(event
);
541 memcpy(&entry
->buf
, str
, size
);
543 /* Add a newline if necessary */
544 if (entry
->buf
[size
- 1] != '\n') {
545 entry
->buf
[size
] = '\n';
546 entry
->buf
[size
+ 1] = '\0';
548 entry
->buf
[size
] = '\0';
550 __buffer_unlock_commit(buffer
, event
);
551 ftrace_trace_stack(buffer
, irq_flags
, 4, pc
);
555 EXPORT_SYMBOL_GPL(__trace_puts
);
558 * __trace_bputs - write the pointer to a constant string into trace buffer
559 * @ip: The address of the caller
560 * @str: The constant string to write to the buffer to
562 int __trace_bputs(unsigned long ip
, const char *str
)
564 struct ring_buffer_event
*event
;
565 struct ring_buffer
*buffer
;
566 struct bputs_entry
*entry
;
567 unsigned long irq_flags
;
568 int size
= sizeof(struct bputs_entry
);
571 if (!(trace_flags
& TRACE_ITER_PRINTK
))
574 pc
= preempt_count();
576 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
579 local_save_flags(irq_flags
);
580 buffer
= global_trace
.trace_buffer
.buffer
;
581 event
= trace_buffer_lock_reserve(buffer
, TRACE_BPUTS
, size
,
586 entry
= ring_buffer_event_data(event
);
590 __buffer_unlock_commit(buffer
, event
);
591 ftrace_trace_stack(buffer
, irq_flags
, 4, pc
);
595 EXPORT_SYMBOL_GPL(__trace_bputs
);
597 #ifdef CONFIG_TRACER_SNAPSHOT
599 * trace_snapshot - take a snapshot of the current buffer.
601 * This causes a swap between the snapshot buffer and the current live
602 * tracing buffer. You can use this to take snapshots of the live
603 * trace when some condition is triggered, but continue to trace.
605 * Note, make sure to allocate the snapshot with either
606 * a tracing_snapshot_alloc(), or by doing it manually
607 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
609 * If the snapshot buffer is not allocated, it will stop tracing.
610 * Basically making a permanent snapshot.
612 void tracing_snapshot(void)
614 struct trace_array
*tr
= &global_trace
;
615 struct tracer
*tracer
= tr
->current_trace
;
619 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
620 internal_trace_puts("*** snapshot is being ignored ***\n");
624 if (!tr
->allocated_snapshot
) {
625 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
626 internal_trace_puts("*** stopping trace here! ***\n");
631 /* Note, snapshot can not be used when the tracer uses it */
632 if (tracer
->use_max_tr
) {
633 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
634 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
638 local_irq_save(flags
);
639 update_max_tr(tr
, current
, smp_processor_id());
640 local_irq_restore(flags
);
642 EXPORT_SYMBOL_GPL(tracing_snapshot
);
644 static int resize_buffer_duplicate_size(struct trace_buffer
*trace_buf
,
645 struct trace_buffer
*size_buf
, int cpu_id
);
646 static void set_buffer_entries(struct trace_buffer
*buf
, unsigned long val
);
648 static int alloc_snapshot(struct trace_array
*tr
)
652 if (!tr
->allocated_snapshot
) {
654 /* allocate spare buffer */
655 ret
= resize_buffer_duplicate_size(&tr
->max_buffer
,
656 &tr
->trace_buffer
, RING_BUFFER_ALL_CPUS
);
660 tr
->allocated_snapshot
= true;
666 static void free_snapshot(struct trace_array
*tr
)
669 * We don't free the ring buffer. instead, resize it because
670 * The max_tr ring buffer has some state (e.g. ring->clock) and
671 * we want preserve it.
673 ring_buffer_resize(tr
->max_buffer
.buffer
, 1, RING_BUFFER_ALL_CPUS
);
674 set_buffer_entries(&tr
->max_buffer
, 1);
675 tracing_reset_online_cpus(&tr
->max_buffer
);
676 tr
->allocated_snapshot
= false;
680 * tracing_alloc_snapshot - allocate snapshot buffer.
682 * This only allocates the snapshot buffer if it isn't already
683 * allocated - it doesn't also take a snapshot.
685 * This is meant to be used in cases where the snapshot buffer needs
686 * to be set up for events that can't sleep but need to be able to
687 * trigger a snapshot.
689 int tracing_alloc_snapshot(void)
691 struct trace_array
*tr
= &global_trace
;
694 ret
= alloc_snapshot(tr
);
699 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot
);
702 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
704 * This is similar to trace_snapshot(), but it will allocate the
705 * snapshot buffer if it isn't already allocated. Use this only
706 * where it is safe to sleep, as the allocation may sleep.
708 * This causes a swap between the snapshot buffer and the current live
709 * tracing buffer. You can use this to take snapshots of the live
710 * trace when some condition is triggered, but continue to trace.
712 void tracing_snapshot_alloc(void)
716 ret
= tracing_alloc_snapshot();
722 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc
);
724 void tracing_snapshot(void)
726 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
728 EXPORT_SYMBOL_GPL(tracing_snapshot
);
729 int tracing_alloc_snapshot(void)
731 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
734 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot
);
735 void tracing_snapshot_alloc(void)
740 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc
);
741 #endif /* CONFIG_TRACER_SNAPSHOT */
743 static void tracer_tracing_off(struct trace_array
*tr
)
745 if (tr
->trace_buffer
.buffer
)
746 ring_buffer_record_off(tr
->trace_buffer
.buffer
);
748 * This flag is looked at when buffers haven't been allocated
749 * yet, or by some tracers (like irqsoff), that just want to
750 * know if the ring buffer has been disabled, but it can handle
751 * races of where it gets disabled but we still do a record.
752 * As the check is in the fast path of the tracers, it is more
753 * important to be fast than accurate.
755 tr
->buffer_disabled
= 1;
756 /* Make the flag seen by readers */
761 * tracing_off - turn off tracing buffers
763 * This function stops the tracing buffers from recording data.
764 * It does not disable any overhead the tracers themselves may
765 * be causing. This function simply causes all recording to
766 * the ring buffers to fail.
768 void tracing_off(void)
770 tracer_tracing_off(&global_trace
);
772 EXPORT_SYMBOL_GPL(tracing_off
);
774 void disable_trace_on_warning(void)
776 if (__disable_trace_on_warning
)
781 * tracer_tracing_is_on - show real state of ring buffer enabled
782 * @tr : the trace array to know if ring buffer is enabled
784 * Shows real state of the ring buffer if it is enabled or not.
786 static int tracer_tracing_is_on(struct trace_array
*tr
)
788 if (tr
->trace_buffer
.buffer
)
789 return ring_buffer_record_is_on(tr
->trace_buffer
.buffer
);
790 return !tr
->buffer_disabled
;
794 * tracing_is_on - show state of ring buffers enabled
796 int tracing_is_on(void)
798 return tracer_tracing_is_on(&global_trace
);
800 EXPORT_SYMBOL_GPL(tracing_is_on
);
802 static int __init
set_buf_size(char *str
)
804 unsigned long buf_size
;
808 buf_size
= memparse(str
, &str
);
809 /* nr_entries can not be zero */
812 trace_buf_size
= buf_size
;
815 __setup("trace_buf_size=", set_buf_size
);
817 static int __init
set_tracing_thresh(char *str
)
819 unsigned long threshold
;
824 ret
= kstrtoul(str
, 0, &threshold
);
827 tracing_thresh
= threshold
* 1000;
830 __setup("tracing_thresh=", set_tracing_thresh
);
832 unsigned long nsecs_to_usecs(unsigned long nsecs
)
837 /* These must match the bit postions in trace_iterator_flags */
838 static const char *trace_options
[] = {
871 int in_ns
; /* is this clock in nanoseconds? */
873 { trace_clock_local
, "local", 1 },
874 { trace_clock_global
, "global", 1 },
875 { trace_clock_counter
, "counter", 0 },
876 { trace_clock_jiffies
, "uptime", 0 },
877 { trace_clock
, "perf", 1 },
878 { ktime_get_mono_fast_ns
, "mono", 1 },
883 * trace_parser_get_init - gets the buffer for trace parser
885 int trace_parser_get_init(struct trace_parser
*parser
, int size
)
887 memset(parser
, 0, sizeof(*parser
));
889 parser
->buffer
= kmalloc(size
, GFP_KERNEL
);
898 * trace_parser_put - frees the buffer for trace parser
900 void trace_parser_put(struct trace_parser
*parser
)
902 kfree(parser
->buffer
);
906 * trace_get_user - reads the user input string separated by space
907 * (matched by isspace(ch))
909 * For each string found the 'struct trace_parser' is updated,
910 * and the function returns.
912 * Returns number of bytes read.
914 * See kernel/trace/trace.h for 'struct trace_parser' details.
916 int trace_get_user(struct trace_parser
*parser
, const char __user
*ubuf
,
917 size_t cnt
, loff_t
*ppos
)
924 trace_parser_clear(parser
);
926 ret
= get_user(ch
, ubuf
++);
934 * The parser is not finished with the last write,
935 * continue reading the user input without skipping spaces.
938 /* skip white space */
939 while (cnt
&& isspace(ch
)) {
940 ret
= get_user(ch
, ubuf
++);
947 /* only spaces were written */
957 /* read the non-space input */
958 while (cnt
&& !isspace(ch
)) {
959 if (parser
->idx
< parser
->size
- 1)
960 parser
->buffer
[parser
->idx
++] = ch
;
965 ret
= get_user(ch
, ubuf
++);
972 /* We either got finished input or we have to wait for another call. */
974 parser
->buffer
[parser
->idx
] = 0;
975 parser
->cont
= false;
976 } else if (parser
->idx
< parser
->size
- 1) {
978 parser
->buffer
[parser
->idx
++] = ch
;
991 /* TODO add a seq_buf_to_buffer() */
992 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
996 if (trace_seq_used(s
) <= s
->seq
.readpos
)
999 len
= trace_seq_used(s
) - s
->seq
.readpos
;
1002 memcpy(buf
, s
->buffer
+ s
->seq
.readpos
, cnt
);
1004 s
->seq
.readpos
+= cnt
;
1008 unsigned long __read_mostly tracing_thresh
;
1010 #ifdef CONFIG_TRACER_MAX_TRACE
1012 * Copy the new maximum trace into the separate maximum-trace
1013 * structure. (this way the maximum trace is permanently saved,
1014 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
1017 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
1019 struct trace_buffer
*trace_buf
= &tr
->trace_buffer
;
1020 struct trace_buffer
*max_buf
= &tr
->max_buffer
;
1021 struct trace_array_cpu
*data
= per_cpu_ptr(trace_buf
->data
, cpu
);
1022 struct trace_array_cpu
*max_data
= per_cpu_ptr(max_buf
->data
, cpu
);
1025 max_buf
->time_start
= data
->preempt_timestamp
;
1027 max_data
->saved_latency
= tr
->max_latency
;
1028 max_data
->critical_start
= data
->critical_start
;
1029 max_data
->critical_end
= data
->critical_end
;
1031 memcpy(max_data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
1032 max_data
->pid
= tsk
->pid
;
1034 * If tsk == current, then use current_uid(), as that does not use
1035 * RCU. The irq tracer can be called out of RCU scope.
1038 max_data
->uid
= current_uid();
1040 max_data
->uid
= task_uid(tsk
);
1042 max_data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
1043 max_data
->policy
= tsk
->policy
;
1044 max_data
->rt_priority
= tsk
->rt_priority
;
1046 /* record this tasks comm */
1047 tracing_record_cmdline(tsk
);
1051 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1053 * @tsk: the task with the latency
1054 * @cpu: The cpu that initiated the trace.
1056 * Flip the buffers between the @tr and the max_tr and record information
1057 * about which task was the cause of this latency.
1060 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
1062 struct ring_buffer
*buf
;
1067 WARN_ON_ONCE(!irqs_disabled());
1069 if (!tr
->allocated_snapshot
) {
1070 /* Only the nop tracer should hit this when disabling */
1071 WARN_ON_ONCE(tr
->current_trace
!= &nop_trace
);
1075 arch_spin_lock(&tr
->max_lock
);
1077 buf
= tr
->trace_buffer
.buffer
;
1078 tr
->trace_buffer
.buffer
= tr
->max_buffer
.buffer
;
1079 tr
->max_buffer
.buffer
= buf
;
1081 __update_max_tr(tr
, tsk
, cpu
);
1082 arch_spin_unlock(&tr
->max_lock
);
1086 * update_max_tr_single - only copy one trace over, and reset the rest
1088 * @tsk - task with the latency
1089 * @cpu - the cpu of the buffer to copy.
1091 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
1094 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
1101 WARN_ON_ONCE(!irqs_disabled());
1102 if (!tr
->allocated_snapshot
) {
1103 /* Only the nop tracer should hit this when disabling */
1104 WARN_ON_ONCE(tr
->current_trace
!= &nop_trace
);
1108 arch_spin_lock(&tr
->max_lock
);
1110 ret
= ring_buffer_swap_cpu(tr
->max_buffer
.buffer
, tr
->trace_buffer
.buffer
, cpu
);
1112 if (ret
== -EBUSY
) {
1114 * We failed to swap the buffer due to a commit taking
1115 * place on this CPU. We fail to record, but we reset
1116 * the max trace buffer (no one writes directly to it)
1117 * and flag that it failed.
1119 trace_array_printk_buf(tr
->max_buffer
.buffer
, _THIS_IP_
,
1120 "Failed to swap buffers due to commit in progress\n");
1123 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
&& ret
!= -EBUSY
);
1125 __update_max_tr(tr
, tsk
, cpu
);
1126 arch_spin_unlock(&tr
->max_lock
);
1128 #endif /* CONFIG_TRACER_MAX_TRACE */
1130 static int wait_on_pipe(struct trace_iterator
*iter
, bool full
)
1132 /* Iterators are static, they should be filled or empty */
1133 if (trace_buffer_iter(iter
, iter
->cpu_file
))
1136 return ring_buffer_wait(iter
->trace_buffer
->buffer
, iter
->cpu_file
,
1140 #ifdef CONFIG_FTRACE_STARTUP_TEST
1141 static int run_tracer_selftest(struct tracer
*type
)
1143 struct trace_array
*tr
= &global_trace
;
1144 struct tracer
*saved_tracer
= tr
->current_trace
;
1147 if (!type
->selftest
|| tracing_selftest_disabled
)
1151 * Run a selftest on this tracer.
1152 * Here we reset the trace buffer, and set the current
1153 * tracer to be this tracer. The tracer can then run some
1154 * internal tracing to verify that everything is in order.
1155 * If we fail, we do not register this tracer.
1157 tracing_reset_online_cpus(&tr
->trace_buffer
);
1159 tr
->current_trace
= type
;
1161 #ifdef CONFIG_TRACER_MAX_TRACE
1162 if (type
->use_max_tr
) {
1163 /* If we expanded the buffers, make sure the max is expanded too */
1164 if (ring_buffer_expanded
)
1165 ring_buffer_resize(tr
->max_buffer
.buffer
, trace_buf_size
,
1166 RING_BUFFER_ALL_CPUS
);
1167 tr
->allocated_snapshot
= true;
1171 /* the test is responsible for initializing and enabling */
1172 pr_info("Testing tracer %s: ", type
->name
);
1173 ret
= type
->selftest(type
, tr
);
1174 /* the test is responsible for resetting too */
1175 tr
->current_trace
= saved_tracer
;
1177 printk(KERN_CONT
"FAILED!\n");
1178 /* Add the warning after printing 'FAILED' */
1182 /* Only reset on passing, to avoid touching corrupted buffers */
1183 tracing_reset_online_cpus(&tr
->trace_buffer
);
1185 #ifdef CONFIG_TRACER_MAX_TRACE
1186 if (type
->use_max_tr
) {
1187 tr
->allocated_snapshot
= false;
1189 /* Shrink the max buffer again */
1190 if (ring_buffer_expanded
)
1191 ring_buffer_resize(tr
->max_buffer
.buffer
, 1,
1192 RING_BUFFER_ALL_CPUS
);
1196 printk(KERN_CONT
"PASSED\n");
1200 static inline int run_tracer_selftest(struct tracer
*type
)
1204 #endif /* CONFIG_FTRACE_STARTUP_TEST */
1207 * register_tracer - register a tracer with the ftrace system.
1208 * @type - the plugin for the tracer
1210 * Register a new plugin tracer.
1212 int register_tracer(struct tracer
*type
)
1218 pr_info("Tracer must have a name\n");
1222 if (strlen(type
->name
) >= MAX_TRACER_SIZE
) {
1223 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE
);
1227 mutex_lock(&trace_types_lock
);
1229 tracing_selftest_running
= true;
1231 for (t
= trace_types
; t
; t
= t
->next
) {
1232 if (strcmp(type
->name
, t
->name
) == 0) {
1234 pr_info("Tracer %s already registered\n",
1241 if (!type
->set_flag
)
1242 type
->set_flag
= &dummy_set_flag
;
1244 type
->flags
= &dummy_tracer_flags
;
1246 if (!type
->flags
->opts
)
1247 type
->flags
->opts
= dummy_tracer_opt
;
1249 ret
= run_tracer_selftest(type
);
1253 type
->next
= trace_types
;
1257 tracing_selftest_running
= false;
1258 mutex_unlock(&trace_types_lock
);
1260 if (ret
|| !default_bootup_tracer
)
1263 if (strncmp(default_bootup_tracer
, type
->name
, MAX_TRACER_SIZE
))
1266 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
1267 /* Do we want this tracer to start on bootup? */
1268 tracing_set_tracer(&global_trace
, type
->name
);
1269 default_bootup_tracer
= NULL
;
1270 /* disable other selftests, since this will break it. */
1271 tracing_selftest_disabled
= true;
1272 #ifdef CONFIG_FTRACE_STARTUP_TEST
1273 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
1281 void tracing_reset(struct trace_buffer
*buf
, int cpu
)
1283 struct ring_buffer
*buffer
= buf
->buffer
;
1288 ring_buffer_record_disable(buffer
);
1290 /* Make sure all commits have finished */
1291 synchronize_sched();
1292 ring_buffer_reset_cpu(buffer
, cpu
);
1294 ring_buffer_record_enable(buffer
);
1297 void tracing_reset_online_cpus(struct trace_buffer
*buf
)
1299 struct ring_buffer
*buffer
= buf
->buffer
;
1305 ring_buffer_record_disable(buffer
);
1307 /* Make sure all commits have finished */
1308 synchronize_sched();
1310 buf
->time_start
= buffer_ftrace_now(buf
, buf
->cpu
);
1312 for_each_online_cpu(cpu
)
1313 ring_buffer_reset_cpu(buffer
, cpu
);
1315 ring_buffer_record_enable(buffer
);
1318 /* Must have trace_types_lock held */
1319 void tracing_reset_all_online_cpus(void)
1321 struct trace_array
*tr
;
1323 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
1324 tracing_reset_online_cpus(&tr
->trace_buffer
);
1325 #ifdef CONFIG_TRACER_MAX_TRACE
1326 tracing_reset_online_cpus(&tr
->max_buffer
);
1331 #define SAVED_CMDLINES_DEFAULT 128
1332 #define NO_CMDLINE_MAP UINT_MAX
1333 static arch_spinlock_t trace_cmdline_lock
= __ARCH_SPIN_LOCK_UNLOCKED
;
1334 struct saved_cmdlines_buffer
{
1335 unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
1336 unsigned *map_cmdline_to_pid
;
1337 unsigned cmdline_num
;
1339 char *saved_cmdlines
;
1341 static struct saved_cmdlines_buffer
*savedcmd
;
1343 /* temporary disable recording */
1344 static atomic_t trace_record_cmdline_disabled __read_mostly
;
1346 static inline char *get_saved_cmdlines(int idx
)
1348 return &savedcmd
->saved_cmdlines
[idx
* TASK_COMM_LEN
];
1351 static inline void set_cmdline(int idx
, const char *cmdline
)
1353 memcpy(get_saved_cmdlines(idx
), cmdline
, TASK_COMM_LEN
);
1356 static int allocate_cmdlines_buffer(unsigned int val
,
1357 struct saved_cmdlines_buffer
*s
)
1359 s
->map_cmdline_to_pid
= kmalloc(val
* sizeof(*s
->map_cmdline_to_pid
),
1361 if (!s
->map_cmdline_to_pid
)
1364 s
->saved_cmdlines
= kmalloc(val
* TASK_COMM_LEN
, GFP_KERNEL
);
1365 if (!s
->saved_cmdlines
) {
1366 kfree(s
->map_cmdline_to_pid
);
1371 s
->cmdline_num
= val
;
1372 memset(&s
->map_pid_to_cmdline
, NO_CMDLINE_MAP
,
1373 sizeof(s
->map_pid_to_cmdline
));
1374 memset(s
->map_cmdline_to_pid
, NO_CMDLINE_MAP
,
1375 val
* sizeof(*s
->map_cmdline_to_pid
));
1380 static int trace_create_savedcmd(void)
1384 savedcmd
= kmalloc(sizeof(*savedcmd
), GFP_KERNEL
);
1388 ret
= allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT
, savedcmd
);
1398 int is_tracing_stopped(void)
1400 return global_trace
.stop_count
;
1404 * tracing_start - quick start of the tracer
1406 * If tracing is enabled but was stopped by tracing_stop,
1407 * this will start the tracer back up.
1409 void tracing_start(void)
1411 struct ring_buffer
*buffer
;
1412 unsigned long flags
;
1414 if (tracing_disabled
)
1417 raw_spin_lock_irqsave(&global_trace
.start_lock
, flags
);
1418 if (--global_trace
.stop_count
) {
1419 if (global_trace
.stop_count
< 0) {
1420 /* Someone screwed up their debugging */
1422 global_trace
.stop_count
= 0;
1427 /* Prevent the buffers from switching */
1428 arch_spin_lock(&global_trace
.max_lock
);
1430 buffer
= global_trace
.trace_buffer
.buffer
;
1432 ring_buffer_record_enable(buffer
);
1434 #ifdef CONFIG_TRACER_MAX_TRACE
1435 buffer
= global_trace
.max_buffer
.buffer
;
1437 ring_buffer_record_enable(buffer
);
1440 arch_spin_unlock(&global_trace
.max_lock
);
1443 raw_spin_unlock_irqrestore(&global_trace
.start_lock
, flags
);
1446 static void tracing_start_tr(struct trace_array
*tr
)
1448 struct ring_buffer
*buffer
;
1449 unsigned long flags
;
1451 if (tracing_disabled
)
1454 /* If global, we need to also start the max tracer */
1455 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
)
1456 return tracing_start();
1458 raw_spin_lock_irqsave(&tr
->start_lock
, flags
);
1460 if (--tr
->stop_count
) {
1461 if (tr
->stop_count
< 0) {
1462 /* Someone screwed up their debugging */
1469 buffer
= tr
->trace_buffer
.buffer
;
1471 ring_buffer_record_enable(buffer
);
1474 raw_spin_unlock_irqrestore(&tr
->start_lock
, flags
);
1478 * tracing_stop - quick stop of the tracer
1480 * Light weight way to stop tracing. Use in conjunction with
1483 void tracing_stop(void)
1485 struct ring_buffer
*buffer
;
1486 unsigned long flags
;
1488 raw_spin_lock_irqsave(&global_trace
.start_lock
, flags
);
1489 if (global_trace
.stop_count
++)
1492 /* Prevent the buffers from switching */
1493 arch_spin_lock(&global_trace
.max_lock
);
1495 buffer
= global_trace
.trace_buffer
.buffer
;
1497 ring_buffer_record_disable(buffer
);
1499 #ifdef CONFIG_TRACER_MAX_TRACE
1500 buffer
= global_trace
.max_buffer
.buffer
;
1502 ring_buffer_record_disable(buffer
);
1505 arch_spin_unlock(&global_trace
.max_lock
);
1508 raw_spin_unlock_irqrestore(&global_trace
.start_lock
, flags
);
1511 static void tracing_stop_tr(struct trace_array
*tr
)
1513 struct ring_buffer
*buffer
;
1514 unsigned long flags
;
1516 /* If global, we need to also stop the max tracer */
1517 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
)
1518 return tracing_stop();
1520 raw_spin_lock_irqsave(&tr
->start_lock
, flags
);
1521 if (tr
->stop_count
++)
1524 buffer
= tr
->trace_buffer
.buffer
;
1526 ring_buffer_record_disable(buffer
);
1529 raw_spin_unlock_irqrestore(&tr
->start_lock
, flags
);
1532 void trace_stop_cmdline_recording(void);
1534 static int trace_save_cmdline(struct task_struct
*tsk
)
1538 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
1542 * It's not the end of the world if we don't get
1543 * the lock, but we also don't want to spin
1544 * nor do we want to disable interrupts,
1545 * so if we miss here, then better luck next time.
1547 if (!arch_spin_trylock(&trace_cmdline_lock
))
1550 idx
= savedcmd
->map_pid_to_cmdline
[tsk
->pid
];
1551 if (idx
== NO_CMDLINE_MAP
) {
1552 idx
= (savedcmd
->cmdline_idx
+ 1) % savedcmd
->cmdline_num
;
1555 * Check whether the cmdline buffer at idx has a pid
1556 * mapped. We are going to overwrite that entry so we
1557 * need to clear the map_pid_to_cmdline. Otherwise we
1558 * would read the new comm for the old pid.
1560 pid
= savedcmd
->map_cmdline_to_pid
[idx
];
1561 if (pid
!= NO_CMDLINE_MAP
)
1562 savedcmd
->map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
1564 savedcmd
->map_cmdline_to_pid
[idx
] = tsk
->pid
;
1565 savedcmd
->map_pid_to_cmdline
[tsk
->pid
] = idx
;
1567 savedcmd
->cmdline_idx
= idx
;
1570 set_cmdline(idx
, tsk
->comm
);
1572 arch_spin_unlock(&trace_cmdline_lock
);
1577 static void __trace_find_cmdline(int pid
, char comm
[])
1582 strcpy(comm
, "<idle>");
1586 if (WARN_ON_ONCE(pid
< 0)) {
1587 strcpy(comm
, "<XXX>");
1591 if (pid
> PID_MAX_DEFAULT
) {
1592 strcpy(comm
, "<...>");
1596 map
= savedcmd
->map_pid_to_cmdline
[pid
];
1597 if (map
!= NO_CMDLINE_MAP
)
1598 strcpy(comm
, get_saved_cmdlines(map
));
1600 strcpy(comm
, "<...>");
1603 void trace_find_cmdline(int pid
, char comm
[])
1606 arch_spin_lock(&trace_cmdline_lock
);
1608 __trace_find_cmdline(pid
, comm
);
1610 arch_spin_unlock(&trace_cmdline_lock
);
1614 void tracing_record_cmdline(struct task_struct
*tsk
)
1616 if (atomic_read(&trace_record_cmdline_disabled
) || !tracing_is_on())
1619 if (!__this_cpu_read(trace_cmdline_save
))
1622 if (trace_save_cmdline(tsk
))
1623 __this_cpu_write(trace_cmdline_save
, false);
1627 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
1630 struct task_struct
*tsk
= current
;
1632 entry
->preempt_count
= pc
& 0xff;
1633 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
1635 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1636 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
1638 TRACE_FLAG_IRQS_NOSUPPORT
|
1640 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
1641 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
1642 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0) |
1643 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED
: 0);
1645 EXPORT_SYMBOL_GPL(tracing_generic_entry_update
);
1647 struct ring_buffer_event
*
1648 trace_buffer_lock_reserve(struct ring_buffer
*buffer
,
1651 unsigned long flags
, int pc
)
1653 struct ring_buffer_event
*event
;
1655 event
= ring_buffer_lock_reserve(buffer
, len
);
1656 if (event
!= NULL
) {
1657 struct trace_entry
*ent
= ring_buffer_event_data(event
);
1659 tracing_generic_entry_update(ent
, flags
, pc
);
1667 __buffer_unlock_commit(struct ring_buffer
*buffer
, struct ring_buffer_event
*event
)
1669 __this_cpu_write(trace_cmdline_save
, true);
1670 ring_buffer_unlock_commit(buffer
, event
);
1674 __trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1675 struct ring_buffer_event
*event
,
1676 unsigned long flags
, int pc
)
1678 __buffer_unlock_commit(buffer
, event
);
1680 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1681 ftrace_trace_userstack(buffer
, flags
, pc
);
1684 void trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1685 struct ring_buffer_event
*event
,
1686 unsigned long flags
, int pc
)
1688 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
1690 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit
);
1692 static struct ring_buffer
*temp_buffer
;
1694 struct ring_buffer_event
*
1695 trace_event_buffer_lock_reserve(struct ring_buffer
**current_rb
,
1696 struct ftrace_event_file
*ftrace_file
,
1697 int type
, unsigned long len
,
1698 unsigned long flags
, int pc
)
1700 struct ring_buffer_event
*entry
;
1702 *current_rb
= ftrace_file
->tr
->trace_buffer
.buffer
;
1703 entry
= trace_buffer_lock_reserve(*current_rb
,
1704 type
, len
, flags
, pc
);
1706 * If tracing is off, but we have triggers enabled
1707 * we still need to look at the event data. Use the temp_buffer
1708 * to store the trace event for the tigger to use. It's recusive
1709 * safe and will not be recorded anywhere.
1711 if (!entry
&& ftrace_file
->flags
& FTRACE_EVENT_FL_TRIGGER_COND
) {
1712 *current_rb
= temp_buffer
;
1713 entry
= trace_buffer_lock_reserve(*current_rb
,
1714 type
, len
, flags
, pc
);
1718 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve
);
1720 struct ring_buffer_event
*
1721 trace_current_buffer_lock_reserve(struct ring_buffer
**current_rb
,
1722 int type
, unsigned long len
,
1723 unsigned long flags
, int pc
)
1725 *current_rb
= global_trace
.trace_buffer
.buffer
;
1726 return trace_buffer_lock_reserve(*current_rb
,
1727 type
, len
, flags
, pc
);
1729 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve
);
1731 void trace_current_buffer_unlock_commit(struct ring_buffer
*buffer
,
1732 struct ring_buffer_event
*event
,
1733 unsigned long flags
, int pc
)
1735 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
1737 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit
);
1739 void trace_buffer_unlock_commit_regs(struct ring_buffer
*buffer
,
1740 struct ring_buffer_event
*event
,
1741 unsigned long flags
, int pc
,
1742 struct pt_regs
*regs
)
1744 __buffer_unlock_commit(buffer
, event
);
1746 ftrace_trace_stack_regs(buffer
, flags
, 0, pc
, regs
);
1747 ftrace_trace_userstack(buffer
, flags
, pc
);
1749 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs
);
1751 void trace_current_buffer_discard_commit(struct ring_buffer
*buffer
,
1752 struct ring_buffer_event
*event
)
1754 ring_buffer_discard_commit(buffer
, event
);
1756 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
1759 trace_function(struct trace_array
*tr
,
1760 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1763 struct ftrace_event_call
*call
= &event_function
;
1764 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
1765 struct ring_buffer_event
*event
;
1766 struct ftrace_entry
*entry
;
1768 /* If we are reading the ring buffer, don't trace */
1769 if (unlikely(__this_cpu_read(ftrace_cpu_disabled
)))
1772 event
= trace_buffer_lock_reserve(buffer
, TRACE_FN
, sizeof(*entry
),
1776 entry
= ring_buffer_event_data(event
);
1778 entry
->parent_ip
= parent_ip
;
1780 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
1781 __buffer_unlock_commit(buffer
, event
);
1784 #ifdef CONFIG_STACKTRACE
1786 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1787 struct ftrace_stack
{
1788 unsigned long calls
[FTRACE_STACK_MAX_ENTRIES
];
1791 static DEFINE_PER_CPU(struct ftrace_stack
, ftrace_stack
);
1792 static DEFINE_PER_CPU(int, ftrace_stack_reserve
);
1794 static void __ftrace_trace_stack(struct ring_buffer
*buffer
,
1795 unsigned long flags
,
1796 int skip
, int pc
, struct pt_regs
*regs
)
1798 struct ftrace_event_call
*call
= &event_kernel_stack
;
1799 struct ring_buffer_event
*event
;
1800 struct stack_entry
*entry
;
1801 struct stack_trace trace
;
1803 int size
= FTRACE_STACK_ENTRIES
;
1805 trace
.nr_entries
= 0;
1809 * Since events can happen in NMIs there's no safe way to
1810 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1811 * or NMI comes in, it will just have to use the default
1812 * FTRACE_STACK_SIZE.
1814 preempt_disable_notrace();
1816 use_stack
= __this_cpu_inc_return(ftrace_stack_reserve
);
1818 * We don't need any atomic variables, just a barrier.
1819 * If an interrupt comes in, we don't care, because it would
1820 * have exited and put the counter back to what we want.
1821 * We just need a barrier to keep gcc from moving things
1825 if (use_stack
== 1) {
1826 trace
.entries
= this_cpu_ptr(ftrace_stack
.calls
);
1827 trace
.max_entries
= FTRACE_STACK_MAX_ENTRIES
;
1830 save_stack_trace_regs(regs
, &trace
);
1832 save_stack_trace(&trace
);
1834 if (trace
.nr_entries
> size
)
1835 size
= trace
.nr_entries
;
1837 /* From now on, use_stack is a boolean */
1840 size
*= sizeof(unsigned long);
1842 event
= trace_buffer_lock_reserve(buffer
, TRACE_STACK
,
1843 sizeof(*entry
) + size
, flags
, pc
);
1846 entry
= ring_buffer_event_data(event
);
1848 memset(&entry
->caller
, 0, size
);
1851 memcpy(&entry
->caller
, trace
.entries
,
1852 trace
.nr_entries
* sizeof(unsigned long));
1854 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1855 trace
.entries
= entry
->caller
;
1857 save_stack_trace_regs(regs
, &trace
);
1859 save_stack_trace(&trace
);
1862 entry
->size
= trace
.nr_entries
;
1864 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
1865 __buffer_unlock_commit(buffer
, event
);
1868 /* Again, don't let gcc optimize things here */
1870 __this_cpu_dec(ftrace_stack_reserve
);
1871 preempt_enable_notrace();
1875 void ftrace_trace_stack_regs(struct ring_buffer
*buffer
, unsigned long flags
,
1876 int skip
, int pc
, struct pt_regs
*regs
)
1878 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1881 __ftrace_trace_stack(buffer
, flags
, skip
, pc
, regs
);
1884 void ftrace_trace_stack(struct ring_buffer
*buffer
, unsigned long flags
,
1887 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1890 __ftrace_trace_stack(buffer
, flags
, skip
, pc
, NULL
);
1893 void __trace_stack(struct trace_array
*tr
, unsigned long flags
, int skip
,
1896 __ftrace_trace_stack(tr
->trace_buffer
.buffer
, flags
, skip
, pc
, NULL
);
1900 * trace_dump_stack - record a stack back trace in the trace buffer
1901 * @skip: Number of functions to skip (helper handlers)
1903 void trace_dump_stack(int skip
)
1905 unsigned long flags
;
1907 if (tracing_disabled
|| tracing_selftest_running
)
1910 local_save_flags(flags
);
1913 * Skip 3 more, seems to get us at the caller of
1917 __ftrace_trace_stack(global_trace
.trace_buffer
.buffer
,
1918 flags
, skip
, preempt_count(), NULL
);
1921 static DEFINE_PER_CPU(int, user_stack_count
);
1924 ftrace_trace_userstack(struct ring_buffer
*buffer
, unsigned long flags
, int pc
)
1926 struct ftrace_event_call
*call
= &event_user_stack
;
1927 struct ring_buffer_event
*event
;
1928 struct userstack_entry
*entry
;
1929 struct stack_trace trace
;
1931 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1935 * NMIs can not handle page faults, even with fix ups.
1936 * The save user stack can (and often does) fault.
1938 if (unlikely(in_nmi()))
1942 * prevent recursion, since the user stack tracing may
1943 * trigger other kernel events.
1946 if (__this_cpu_read(user_stack_count
))
1949 __this_cpu_inc(user_stack_count
);
1951 event
= trace_buffer_lock_reserve(buffer
, TRACE_USER_STACK
,
1952 sizeof(*entry
), flags
, pc
);
1954 goto out_drop_count
;
1955 entry
= ring_buffer_event_data(event
);
1957 entry
->tgid
= current
->tgid
;
1958 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1960 trace
.nr_entries
= 0;
1961 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1963 trace
.entries
= entry
->caller
;
1965 save_stack_trace_user(&trace
);
1966 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
1967 __buffer_unlock_commit(buffer
, event
);
1970 __this_cpu_dec(user_stack_count
);
1976 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1978 ftrace_trace_userstack(tr
, flags
, preempt_count());
1982 #endif /* CONFIG_STACKTRACE */
1984 /* created for use with alloc_percpu */
1985 struct trace_buffer_struct
{
1986 char buffer
[TRACE_BUF_SIZE
];
1989 static struct trace_buffer_struct
*trace_percpu_buffer
;
1990 static struct trace_buffer_struct
*trace_percpu_sirq_buffer
;
1991 static struct trace_buffer_struct
*trace_percpu_irq_buffer
;
1992 static struct trace_buffer_struct
*trace_percpu_nmi_buffer
;
1995 * The buffer used is dependent on the context. There is a per cpu
1996 * buffer for normal context, softirq contex, hard irq context and
1997 * for NMI context. Thise allows for lockless recording.
1999 * Note, if the buffers failed to be allocated, then this returns NULL
2001 static char *get_trace_buf(void)
2003 struct trace_buffer_struct
*percpu_buffer
;
2006 * If we have allocated per cpu buffers, then we do not
2007 * need to do any locking.
2010 percpu_buffer
= trace_percpu_nmi_buffer
;
2012 percpu_buffer
= trace_percpu_irq_buffer
;
2013 else if (in_softirq())
2014 percpu_buffer
= trace_percpu_sirq_buffer
;
2016 percpu_buffer
= trace_percpu_buffer
;
2021 return this_cpu_ptr(&percpu_buffer
->buffer
[0]);
2024 static int alloc_percpu_trace_buffer(void)
2026 struct trace_buffer_struct
*buffers
;
2027 struct trace_buffer_struct
*sirq_buffers
;
2028 struct trace_buffer_struct
*irq_buffers
;
2029 struct trace_buffer_struct
*nmi_buffers
;
2031 buffers
= alloc_percpu(struct trace_buffer_struct
);
2035 sirq_buffers
= alloc_percpu(struct trace_buffer_struct
);
2039 irq_buffers
= alloc_percpu(struct trace_buffer_struct
);
2043 nmi_buffers
= alloc_percpu(struct trace_buffer_struct
);
2047 trace_percpu_buffer
= buffers
;
2048 trace_percpu_sirq_buffer
= sirq_buffers
;
2049 trace_percpu_irq_buffer
= irq_buffers
;
2050 trace_percpu_nmi_buffer
= nmi_buffers
;
2055 free_percpu(irq_buffers
);
2057 free_percpu(sirq_buffers
);
2059 free_percpu(buffers
);
2061 WARN(1, "Could not allocate percpu trace_printk buffer");
2065 static int buffers_allocated
;
2067 void trace_printk_init_buffers(void)
2069 if (buffers_allocated
)
2072 if (alloc_percpu_trace_buffer())
2075 /* trace_printk() is for debug use only. Don't use it in production. */
2078 pr_warning("**********************************************************\n");
2079 pr_warning("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2080 pr_warning("** **\n");
2081 pr_warning("** trace_printk() being used. Allocating extra memory. **\n");
2082 pr_warning("** **\n");
2083 pr_warning("** This means that this is a DEBUG kernel and it is **\n");
2084 pr_warning("** unsafe for production use. **\n");
2085 pr_warning("** **\n");
2086 pr_warning("** If you see this message and you are not debugging **\n");
2087 pr_warning("** the kernel, report this immediately to your vendor! **\n");
2088 pr_warning("** **\n");
2089 pr_warning("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2090 pr_warning("**********************************************************\n");
2092 /* Expand the buffers to set size */
2093 tracing_update_buffers();
2095 buffers_allocated
= 1;
2098 * trace_printk_init_buffers() can be called by modules.
2099 * If that happens, then we need to start cmdline recording
2100 * directly here. If the global_trace.buffer is already
2101 * allocated here, then this was called by module code.
2103 if (global_trace
.trace_buffer
.buffer
)
2104 tracing_start_cmdline_record();
2107 void trace_printk_start_comm(void)
2109 /* Start tracing comms if trace printk is set */
2110 if (!buffers_allocated
)
2112 tracing_start_cmdline_record();
2115 static void trace_printk_start_stop_comm(int enabled
)
2117 if (!buffers_allocated
)
2121 tracing_start_cmdline_record();
2123 tracing_stop_cmdline_record();
2127 * trace_vbprintk - write binary msg to tracing buffer
2130 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
2132 struct ftrace_event_call
*call
= &event_bprint
;
2133 struct ring_buffer_event
*event
;
2134 struct ring_buffer
*buffer
;
2135 struct trace_array
*tr
= &global_trace
;
2136 struct bprint_entry
*entry
;
2137 unsigned long flags
;
2139 int len
= 0, size
, pc
;
2141 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
2144 /* Don't pollute graph traces with trace_vprintk internals */
2145 pause_graph_tracing();
2147 pc
= preempt_count();
2148 preempt_disable_notrace();
2150 tbuffer
= get_trace_buf();
2156 len
= vbin_printf((u32
*)tbuffer
, TRACE_BUF_SIZE
/sizeof(int), fmt
, args
);
2158 if (len
> TRACE_BUF_SIZE
/sizeof(int) || len
< 0)
2161 local_save_flags(flags
);
2162 size
= sizeof(*entry
) + sizeof(u32
) * len
;
2163 buffer
= tr
->trace_buffer
.buffer
;
2164 event
= trace_buffer_lock_reserve(buffer
, TRACE_BPRINT
, size
,
2168 entry
= ring_buffer_event_data(event
);
2172 memcpy(entry
->buf
, tbuffer
, sizeof(u32
) * len
);
2173 if (!call_filter_check_discard(call
, entry
, buffer
, event
)) {
2174 __buffer_unlock_commit(buffer
, event
);
2175 ftrace_trace_stack(buffer
, flags
, 6, pc
);
2179 preempt_enable_notrace();
2180 unpause_graph_tracing();
2184 EXPORT_SYMBOL_GPL(trace_vbprintk
);
2187 __trace_array_vprintk(struct ring_buffer
*buffer
,
2188 unsigned long ip
, const char *fmt
, va_list args
)
2190 struct ftrace_event_call
*call
= &event_print
;
2191 struct ring_buffer_event
*event
;
2192 int len
= 0, size
, pc
;
2193 struct print_entry
*entry
;
2194 unsigned long flags
;
2197 if (tracing_disabled
|| tracing_selftest_running
)
2200 /* Don't pollute graph traces with trace_vprintk internals */
2201 pause_graph_tracing();
2203 pc
= preempt_count();
2204 preempt_disable_notrace();
2207 tbuffer
= get_trace_buf();
2213 len
= vscnprintf(tbuffer
, TRACE_BUF_SIZE
, fmt
, args
);
2215 local_save_flags(flags
);
2216 size
= sizeof(*entry
) + len
+ 1;
2217 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
2221 entry
= ring_buffer_event_data(event
);
2224 memcpy(&entry
->buf
, tbuffer
, len
+ 1);
2225 if (!call_filter_check_discard(call
, entry
, buffer
, event
)) {
2226 __buffer_unlock_commit(buffer
, event
);
2227 ftrace_trace_stack(buffer
, flags
, 6, pc
);
2230 preempt_enable_notrace();
2231 unpause_graph_tracing();
2236 int trace_array_vprintk(struct trace_array
*tr
,
2237 unsigned long ip
, const char *fmt
, va_list args
)
2239 return __trace_array_vprintk(tr
->trace_buffer
.buffer
, ip
, fmt
, args
);
2242 int trace_array_printk(struct trace_array
*tr
,
2243 unsigned long ip
, const char *fmt
, ...)
2248 if (!(trace_flags
& TRACE_ITER_PRINTK
))
2252 ret
= trace_array_vprintk(tr
, ip
, fmt
, ap
);
2257 int trace_array_printk_buf(struct ring_buffer
*buffer
,
2258 unsigned long ip
, const char *fmt
, ...)
2263 if (!(trace_flags
& TRACE_ITER_PRINTK
))
2267 ret
= __trace_array_vprintk(buffer
, ip
, fmt
, ap
);
2272 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
2274 return trace_array_vprintk(&global_trace
, ip
, fmt
, args
);
2276 EXPORT_SYMBOL_GPL(trace_vprintk
);
2278 static void trace_iterator_increment(struct trace_iterator
*iter
)
2280 struct ring_buffer_iter
*buf_iter
= trace_buffer_iter(iter
, iter
->cpu
);
2284 ring_buffer_read(buf_iter
, NULL
);
2287 static struct trace_entry
*
2288 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
,
2289 unsigned long *lost_events
)
2291 struct ring_buffer_event
*event
;
2292 struct ring_buffer_iter
*buf_iter
= trace_buffer_iter(iter
, cpu
);
2295 event
= ring_buffer_iter_peek(buf_iter
, ts
);
2297 event
= ring_buffer_peek(iter
->trace_buffer
->buffer
, cpu
, ts
,
2301 iter
->ent_size
= ring_buffer_event_length(event
);
2302 return ring_buffer_event_data(event
);
2308 static struct trace_entry
*
2309 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
,
2310 unsigned long *missing_events
, u64
*ent_ts
)
2312 struct ring_buffer
*buffer
= iter
->trace_buffer
->buffer
;
2313 struct trace_entry
*ent
, *next
= NULL
;
2314 unsigned long lost_events
= 0, next_lost
= 0;
2315 int cpu_file
= iter
->cpu_file
;
2316 u64 next_ts
= 0, ts
;
2322 * If we are in a per_cpu trace file, don't bother by iterating over
2323 * all cpu and peek directly.
2325 if (cpu_file
> RING_BUFFER_ALL_CPUS
) {
2326 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
2328 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
, missing_events
);
2330 *ent_cpu
= cpu_file
;
2335 for_each_tracing_cpu(cpu
) {
2337 if (ring_buffer_empty_cpu(buffer
, cpu
))
2340 ent
= peek_next_entry(iter
, cpu
, &ts
, &lost_events
);
2343 * Pick the entry with the smallest timestamp:
2345 if (ent
&& (!next
|| ts
< next_ts
)) {
2349 next_lost
= lost_events
;
2350 next_size
= iter
->ent_size
;
2354 iter
->ent_size
= next_size
;
2357 *ent_cpu
= next_cpu
;
2363 *missing_events
= next_lost
;
2368 /* Find the next real entry, without updating the iterator itself */
2369 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
2370 int *ent_cpu
, u64
*ent_ts
)
2372 return __find_next_entry(iter
, ent_cpu
, NULL
, ent_ts
);
2375 /* Find the next real entry, and increment the iterator to the next entry */
2376 void *trace_find_next_entry_inc(struct trace_iterator
*iter
)
2378 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
,
2379 &iter
->lost_events
, &iter
->ts
);
2382 trace_iterator_increment(iter
);
2384 return iter
->ent
? iter
: NULL
;
2387 static void trace_consume(struct trace_iterator
*iter
)
2389 ring_buffer_consume(iter
->trace_buffer
->buffer
, iter
->cpu
, &iter
->ts
,
2390 &iter
->lost_events
);
2393 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2395 struct trace_iterator
*iter
= m
->private;
2399 WARN_ON_ONCE(iter
->leftover
);
2403 /* can't go backwards */
2408 ent
= trace_find_next_entry_inc(iter
);
2412 while (ent
&& iter
->idx
< i
)
2413 ent
= trace_find_next_entry_inc(iter
);
2420 void tracing_iter_reset(struct trace_iterator
*iter
, int cpu
)
2422 struct ring_buffer_event
*event
;
2423 struct ring_buffer_iter
*buf_iter
;
2424 unsigned long entries
= 0;
2427 per_cpu_ptr(iter
->trace_buffer
->data
, cpu
)->skipped_entries
= 0;
2429 buf_iter
= trace_buffer_iter(iter
, cpu
);
2433 ring_buffer_iter_reset(buf_iter
);
2436 * We could have the case with the max latency tracers
2437 * that a reset never took place on a cpu. This is evident
2438 * by the timestamp being before the start of the buffer.
2440 while ((event
= ring_buffer_iter_peek(buf_iter
, &ts
))) {
2441 if (ts
>= iter
->trace_buffer
->time_start
)
2444 ring_buffer_read(buf_iter
, NULL
);
2447 per_cpu_ptr(iter
->trace_buffer
->data
, cpu
)->skipped_entries
= entries
;
2451 * The current tracer is copied to avoid a global locking
2454 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
2456 struct trace_iterator
*iter
= m
->private;
2457 struct trace_array
*tr
= iter
->tr
;
2458 int cpu_file
= iter
->cpu_file
;
2464 * copy the tracer to avoid using a global lock all around.
2465 * iter->trace is a copy of current_trace, the pointer to the
2466 * name may be used instead of a strcmp(), as iter->trace->name
2467 * will point to the same string as current_trace->name.
2469 mutex_lock(&trace_types_lock
);
2470 if (unlikely(tr
->current_trace
&& iter
->trace
->name
!= tr
->current_trace
->name
))
2471 *iter
->trace
= *tr
->current_trace
;
2472 mutex_unlock(&trace_types_lock
);
2474 #ifdef CONFIG_TRACER_MAX_TRACE
2475 if (iter
->snapshot
&& iter
->trace
->use_max_tr
)
2476 return ERR_PTR(-EBUSY
);
2479 if (!iter
->snapshot
)
2480 atomic_inc(&trace_record_cmdline_disabled
);
2482 if (*pos
!= iter
->pos
) {
2487 if (cpu_file
== RING_BUFFER_ALL_CPUS
) {
2488 for_each_tracing_cpu(cpu
)
2489 tracing_iter_reset(iter
, cpu
);
2491 tracing_iter_reset(iter
, cpu_file
);
2494 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
2499 * If we overflowed the seq_file before, then we want
2500 * to just reuse the trace_seq buffer again.
2506 p
= s_next(m
, p
, &l
);
2510 trace_event_read_lock();
2511 trace_access_lock(cpu_file
);
2515 static void s_stop(struct seq_file
*m
, void *p
)
2517 struct trace_iterator
*iter
= m
->private;
2519 #ifdef CONFIG_TRACER_MAX_TRACE
2520 if (iter
->snapshot
&& iter
->trace
->use_max_tr
)
2524 if (!iter
->snapshot
)
2525 atomic_dec(&trace_record_cmdline_disabled
);
2527 trace_access_unlock(iter
->cpu_file
);
2528 trace_event_read_unlock();
2532 get_total_entries(struct trace_buffer
*buf
,
2533 unsigned long *total
, unsigned long *entries
)
2535 unsigned long count
;
2541 for_each_tracing_cpu(cpu
) {
2542 count
= ring_buffer_entries_cpu(buf
->buffer
, cpu
);
2544 * If this buffer has skipped entries, then we hold all
2545 * entries for the trace and we need to ignore the
2546 * ones before the time stamp.
2548 if (per_cpu_ptr(buf
->data
, cpu
)->skipped_entries
) {
2549 count
-= per_cpu_ptr(buf
->data
, cpu
)->skipped_entries
;
2550 /* total is the same as the entries */
2554 ring_buffer_overrun_cpu(buf
->buffer
, cpu
);
2559 static void print_lat_help_header(struct seq_file
*m
)
2561 seq_puts(m
, "# _------=> CPU# \n"
2562 "# / _-----=> irqs-off \n"
2563 "# | / _----=> need-resched \n"
2564 "# || / _---=> hardirq/softirq \n"
2565 "# ||| / _--=> preempt-depth \n"
2567 "# cmd pid ||||| time | caller \n"
2568 "# \\ / ||||| \\ | / \n");
2571 static void print_event_info(struct trace_buffer
*buf
, struct seq_file
*m
)
2573 unsigned long total
;
2574 unsigned long entries
;
2576 get_total_entries(buf
, &total
, &entries
);
2577 seq_printf(m
, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2578 entries
, total
, num_online_cpus());
2582 static void print_func_help_header(struct trace_buffer
*buf
, struct seq_file
*m
)
2584 print_event_info(buf
, m
);
2585 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n"
2589 static void print_func_help_header_irq(struct trace_buffer
*buf
, struct seq_file
*m
)
2591 print_event_info(buf
, m
);
2592 seq_puts(m
, "# _-----=> irqs-off\n"
2593 "# / _----=> need-resched\n"
2594 "# | / _---=> hardirq/softirq\n"
2595 "# || / _--=> preempt-depth\n"
2597 "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
2598 "# | | | |||| | |\n");
2602 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
2604 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
2605 struct trace_buffer
*buf
= iter
->trace_buffer
;
2606 struct trace_array_cpu
*data
= per_cpu_ptr(buf
->data
, buf
->cpu
);
2607 struct tracer
*type
= iter
->trace
;
2608 unsigned long entries
;
2609 unsigned long total
;
2610 const char *name
= "preemption";
2614 get_total_entries(buf
, &total
, &entries
);
2616 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
2618 seq_puts(m
, "# -----------------------------------"
2619 "---------------------------------\n");
2620 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2621 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2622 nsecs_to_usecs(data
->saved_latency
),
2626 #if defined(CONFIG_PREEMPT_NONE)
2628 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2630 #elif defined(CONFIG_PREEMPT)
2635 /* These are reserved for later use */
2638 seq_printf(m
, " #P:%d)\n", num_online_cpus());
2642 seq_puts(m
, "# -----------------\n");
2643 seq_printf(m
, "# | task: %.16s-%d "
2644 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2645 data
->comm
, data
->pid
,
2646 from_kuid_munged(seq_user_ns(m
), data
->uid
), data
->nice
,
2647 data
->policy
, data
->rt_priority
);
2648 seq_puts(m
, "# -----------------\n");
2650 if (data
->critical_start
) {
2651 seq_puts(m
, "# => started at: ");
2652 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
2653 trace_print_seq(m
, &iter
->seq
);
2654 seq_puts(m
, "\n# => ended at: ");
2655 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
2656 trace_print_seq(m
, &iter
->seq
);
2657 seq_puts(m
, "\n#\n");
2663 static void test_cpu_buff_start(struct trace_iterator
*iter
)
2665 struct trace_seq
*s
= &iter
->seq
;
2667 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
2670 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
2673 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
2676 if (per_cpu_ptr(iter
->trace_buffer
->data
, iter
->cpu
)->skipped_entries
)
2679 cpumask_set_cpu(iter
->cpu
, iter
->started
);
2681 /* Don't print started cpu buffer for the first entry of the trace */
2683 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
2687 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
2689 struct trace_seq
*s
= &iter
->seq
;
2690 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
2691 struct trace_entry
*entry
;
2692 struct trace_event
*event
;
2696 test_cpu_buff_start(iter
);
2698 event
= ftrace_find_event(entry
->type
);
2700 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2701 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
2702 trace_print_lat_context(iter
);
2704 trace_print_context(iter
);
2707 if (trace_seq_has_overflowed(s
))
2708 return TRACE_TYPE_PARTIAL_LINE
;
2711 return event
->funcs
->trace(iter
, sym_flags
, event
);
2713 trace_seq_printf(s
, "Unknown type %d\n", entry
->type
);
2715 return trace_handle_return(s
);
2718 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
2720 struct trace_seq
*s
= &iter
->seq
;
2721 struct trace_entry
*entry
;
2722 struct trace_event
*event
;
2726 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
)
2727 trace_seq_printf(s
, "%d %d %llu ",
2728 entry
->pid
, iter
->cpu
, iter
->ts
);
2730 if (trace_seq_has_overflowed(s
))
2731 return TRACE_TYPE_PARTIAL_LINE
;
2733 event
= ftrace_find_event(entry
->type
);
2735 return event
->funcs
->raw(iter
, 0, event
);
2737 trace_seq_printf(s
, "%d ?\n", entry
->type
);
2739 return trace_handle_return(s
);
2742 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
2744 struct trace_seq
*s
= &iter
->seq
;
2745 unsigned char newline
= '\n';
2746 struct trace_entry
*entry
;
2747 struct trace_event
*event
;
2751 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2752 SEQ_PUT_HEX_FIELD(s
, entry
->pid
);
2753 SEQ_PUT_HEX_FIELD(s
, iter
->cpu
);
2754 SEQ_PUT_HEX_FIELD(s
, iter
->ts
);
2755 if (trace_seq_has_overflowed(s
))
2756 return TRACE_TYPE_PARTIAL_LINE
;
2759 event
= ftrace_find_event(entry
->type
);
2761 enum print_line_t ret
= event
->funcs
->hex(iter
, 0, event
);
2762 if (ret
!= TRACE_TYPE_HANDLED
)
2766 SEQ_PUT_FIELD(s
, newline
);
2768 return trace_handle_return(s
);
2771 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
2773 struct trace_seq
*s
= &iter
->seq
;
2774 struct trace_entry
*entry
;
2775 struct trace_event
*event
;
2779 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2780 SEQ_PUT_FIELD(s
, entry
->pid
);
2781 SEQ_PUT_FIELD(s
, iter
->cpu
);
2782 SEQ_PUT_FIELD(s
, iter
->ts
);
2783 if (trace_seq_has_overflowed(s
))
2784 return TRACE_TYPE_PARTIAL_LINE
;
2787 event
= ftrace_find_event(entry
->type
);
2788 return event
? event
->funcs
->binary(iter
, 0, event
) :
2792 int trace_empty(struct trace_iterator
*iter
)
2794 struct ring_buffer_iter
*buf_iter
;
2797 /* If we are looking at one CPU buffer, only check that one */
2798 if (iter
->cpu_file
!= RING_BUFFER_ALL_CPUS
) {
2799 cpu
= iter
->cpu_file
;
2800 buf_iter
= trace_buffer_iter(iter
, cpu
);
2802 if (!ring_buffer_iter_empty(buf_iter
))
2805 if (!ring_buffer_empty_cpu(iter
->trace_buffer
->buffer
, cpu
))
2811 for_each_tracing_cpu(cpu
) {
2812 buf_iter
= trace_buffer_iter(iter
, cpu
);
2814 if (!ring_buffer_iter_empty(buf_iter
))
2817 if (!ring_buffer_empty_cpu(iter
->trace_buffer
->buffer
, cpu
))
2825 /* Called with trace_event_read_lock() held. */
2826 enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
2828 enum print_line_t ret
;
2830 if (iter
->lost_events
) {
2831 trace_seq_printf(&iter
->seq
, "CPU:%d [LOST %lu EVENTS]\n",
2832 iter
->cpu
, iter
->lost_events
);
2833 if (trace_seq_has_overflowed(&iter
->seq
))
2834 return TRACE_TYPE_PARTIAL_LINE
;
2837 if (iter
->trace
&& iter
->trace
->print_line
) {
2838 ret
= iter
->trace
->print_line(iter
);
2839 if (ret
!= TRACE_TYPE_UNHANDLED
)
2843 if (iter
->ent
->type
== TRACE_BPUTS
&&
2844 trace_flags
& TRACE_ITER_PRINTK
&&
2845 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2846 return trace_print_bputs_msg_only(iter
);
2848 if (iter
->ent
->type
== TRACE_BPRINT
&&
2849 trace_flags
& TRACE_ITER_PRINTK
&&
2850 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2851 return trace_print_bprintk_msg_only(iter
);
2853 if (iter
->ent
->type
== TRACE_PRINT
&&
2854 trace_flags
& TRACE_ITER_PRINTK
&&
2855 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2856 return trace_print_printk_msg_only(iter
);
2858 if (trace_flags
& TRACE_ITER_BIN
)
2859 return print_bin_fmt(iter
);
2861 if (trace_flags
& TRACE_ITER_HEX
)
2862 return print_hex_fmt(iter
);
2864 if (trace_flags
& TRACE_ITER_RAW
)
2865 return print_raw_fmt(iter
);
2867 return print_trace_fmt(iter
);
2870 void trace_latency_header(struct seq_file
*m
)
2872 struct trace_iterator
*iter
= m
->private;
2874 /* print nothing if the buffers are empty */
2875 if (trace_empty(iter
))
2878 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
2879 print_trace_header(m
, iter
);
2881 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2882 print_lat_help_header(m
);
2885 void trace_default_header(struct seq_file
*m
)
2887 struct trace_iterator
*iter
= m
->private;
2889 if (!(trace_flags
& TRACE_ITER_CONTEXT_INFO
))
2892 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
2893 /* print nothing if the buffers are empty */
2894 if (trace_empty(iter
))
2896 print_trace_header(m
, iter
);
2897 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2898 print_lat_help_header(m
);
2900 if (!(trace_flags
& TRACE_ITER_VERBOSE
)) {
2901 if (trace_flags
& TRACE_ITER_IRQ_INFO
)
2902 print_func_help_header_irq(iter
->trace_buffer
, m
);
2904 print_func_help_header(iter
->trace_buffer
, m
);
2909 static void test_ftrace_alive(struct seq_file
*m
)
2911 if (!ftrace_is_dead())
2913 seq_puts(m
, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
2914 "# MAY BE MISSING FUNCTION EVENTS\n");
2917 #ifdef CONFIG_TRACER_MAX_TRACE
2918 static void show_snapshot_main_help(struct seq_file
*m
)
2920 seq_puts(m
, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
2921 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
2922 "# Takes a snapshot of the main buffer.\n"
2923 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
2924 "# (Doesn't have to be '2' works with any number that\n"
2925 "# is not a '0' or '1')\n");
2928 static void show_snapshot_percpu_help(struct seq_file
*m
)
2930 seq_puts(m
, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2931 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2932 seq_puts(m
, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
2933 "# Takes a snapshot of the main buffer for this cpu.\n");
2935 seq_puts(m
, "# echo 1 > snapshot : Not supported with this kernel.\n"
2936 "# Must use main snapshot file to allocate.\n");
2938 seq_puts(m
, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
2939 "# (Doesn't have to be '2' works with any number that\n"
2940 "# is not a '0' or '1')\n");
2943 static void print_snapshot_help(struct seq_file
*m
, struct trace_iterator
*iter
)
2945 if (iter
->tr
->allocated_snapshot
)
2946 seq_puts(m
, "#\n# * Snapshot is allocated *\n#\n");
2948 seq_puts(m
, "#\n# * Snapshot is freed *\n#\n");
2950 seq_puts(m
, "# Snapshot commands:\n");
2951 if (iter
->cpu_file
== RING_BUFFER_ALL_CPUS
)
2952 show_snapshot_main_help(m
);
2954 show_snapshot_percpu_help(m
);
2957 /* Should never be called */
2958 static inline void print_snapshot_help(struct seq_file
*m
, struct trace_iterator
*iter
) { }
2961 static int s_show(struct seq_file
*m
, void *v
)
2963 struct trace_iterator
*iter
= v
;
2966 if (iter
->ent
== NULL
) {
2968 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
2970 test_ftrace_alive(m
);
2972 if (iter
->snapshot
&& trace_empty(iter
))
2973 print_snapshot_help(m
, iter
);
2974 else if (iter
->trace
&& iter
->trace
->print_header
)
2975 iter
->trace
->print_header(m
);
2977 trace_default_header(m
);
2979 } else if (iter
->leftover
) {
2981 * If we filled the seq_file buffer earlier, we
2982 * want to just show it now.
2984 ret
= trace_print_seq(m
, &iter
->seq
);
2986 /* ret should this time be zero, but you never know */
2987 iter
->leftover
= ret
;
2990 print_trace_line(iter
);
2991 ret
= trace_print_seq(m
, &iter
->seq
);
2993 * If we overflow the seq_file buffer, then it will
2994 * ask us for this data again at start up.
2996 * ret is 0 if seq_file write succeeded.
2999 iter
->leftover
= ret
;
3006 * Should be used after trace_array_get(), trace_types_lock
3007 * ensures that i_cdev was already initialized.
3009 static inline int tracing_get_cpu(struct inode
*inode
)
3011 if (inode
->i_cdev
) /* See trace_create_cpu_file() */
3012 return (long)inode
->i_cdev
- 1;
3013 return RING_BUFFER_ALL_CPUS
;
3016 static const struct seq_operations tracer_seq_ops
= {
3023 static struct trace_iterator
*
3024 __tracing_open(struct inode
*inode
, struct file
*file
, bool snapshot
)
3026 struct trace_array
*tr
= inode
->i_private
;
3027 struct trace_iterator
*iter
;
3030 if (tracing_disabled
)
3031 return ERR_PTR(-ENODEV
);
3033 iter
= __seq_open_private(file
, &tracer_seq_ops
, sizeof(*iter
));
3035 return ERR_PTR(-ENOMEM
);
3037 iter
->buffer_iter
= kzalloc(sizeof(*iter
->buffer_iter
) * num_possible_cpus(),
3039 if (!iter
->buffer_iter
)
3043 * We make a copy of the current tracer to avoid concurrent
3044 * changes on it while we are reading.
3046 mutex_lock(&trace_types_lock
);
3047 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
3051 *iter
->trace
= *tr
->current_trace
;
3053 if (!zalloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
3058 #ifdef CONFIG_TRACER_MAX_TRACE
3059 /* Currently only the top directory has a snapshot */
3060 if (tr
->current_trace
->print_max
|| snapshot
)
3061 iter
->trace_buffer
= &tr
->max_buffer
;
3064 iter
->trace_buffer
= &tr
->trace_buffer
;
3065 iter
->snapshot
= snapshot
;
3067 iter
->cpu_file
= tracing_get_cpu(inode
);
3068 mutex_init(&iter
->mutex
);
3070 /* Notify the tracer early; before we stop tracing. */
3071 if (iter
->trace
&& iter
->trace
->open
)
3072 iter
->trace
->open(iter
);
3074 /* Annotate start of buffers if we had overruns */
3075 if (ring_buffer_overruns(iter
->trace_buffer
->buffer
))
3076 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
3078 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3079 if (trace_clocks
[tr
->clock_id
].in_ns
)
3080 iter
->iter_flags
|= TRACE_FILE_TIME_IN_NS
;
3082 /* stop the trace while dumping if we are not opening "snapshot" */
3083 if (!iter
->snapshot
)
3084 tracing_stop_tr(tr
);
3086 if (iter
->cpu_file
== RING_BUFFER_ALL_CPUS
) {
3087 for_each_tracing_cpu(cpu
) {
3088 iter
->buffer_iter
[cpu
] =
3089 ring_buffer_read_prepare(iter
->trace_buffer
->buffer
, cpu
);
3091 ring_buffer_read_prepare_sync();
3092 for_each_tracing_cpu(cpu
) {
3093 ring_buffer_read_start(iter
->buffer_iter
[cpu
]);
3094 tracing_iter_reset(iter
, cpu
);
3097 cpu
= iter
->cpu_file
;
3098 iter
->buffer_iter
[cpu
] =
3099 ring_buffer_read_prepare(iter
->trace_buffer
->buffer
, cpu
);
3100 ring_buffer_read_prepare_sync();
3101 ring_buffer_read_start(iter
->buffer_iter
[cpu
]);
3102 tracing_iter_reset(iter
, cpu
);
3105 mutex_unlock(&trace_types_lock
);
3110 mutex_unlock(&trace_types_lock
);
3112 kfree(iter
->buffer_iter
);
3114 seq_release_private(inode
, file
);
3115 return ERR_PTR(-ENOMEM
);
3118 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
3120 if (tracing_disabled
)
3123 filp
->private_data
= inode
->i_private
;
3127 bool tracing_is_disabled(void)
3129 return (tracing_disabled
) ? true: false;
3133 * Open and update trace_array ref count.
3134 * Must have the current trace_array passed to it.
3136 static int tracing_open_generic_tr(struct inode
*inode
, struct file
*filp
)
3138 struct trace_array
*tr
= inode
->i_private
;
3140 if (tracing_disabled
)
3143 if (trace_array_get(tr
) < 0)
3146 filp
->private_data
= inode
->i_private
;
3151 static int tracing_release(struct inode
*inode
, struct file
*file
)
3153 struct trace_array
*tr
= inode
->i_private
;
3154 struct seq_file
*m
= file
->private_data
;
3155 struct trace_iterator
*iter
;
3158 if (!(file
->f_mode
& FMODE_READ
)) {
3159 trace_array_put(tr
);
3163 /* Writes do not use seq_file */
3165 mutex_lock(&trace_types_lock
);
3167 for_each_tracing_cpu(cpu
) {
3168 if (iter
->buffer_iter
[cpu
])
3169 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
3172 if (iter
->trace
&& iter
->trace
->close
)
3173 iter
->trace
->close(iter
);
3175 if (!iter
->snapshot
)
3176 /* reenable tracing if it was previously enabled */
3177 tracing_start_tr(tr
);
3179 __trace_array_put(tr
);
3181 mutex_unlock(&trace_types_lock
);
3183 mutex_destroy(&iter
->mutex
);
3184 free_cpumask_var(iter
->started
);
3186 kfree(iter
->buffer_iter
);
3187 seq_release_private(inode
, file
);
3192 static int tracing_release_generic_tr(struct inode
*inode
, struct file
*file
)
3194 struct trace_array
*tr
= inode
->i_private
;
3196 trace_array_put(tr
);
3200 static int tracing_single_release_tr(struct inode
*inode
, struct file
*file
)
3202 struct trace_array
*tr
= inode
->i_private
;
3204 trace_array_put(tr
);
3206 return single_release(inode
, file
);
3209 static int tracing_open(struct inode
*inode
, struct file
*file
)
3211 struct trace_array
*tr
= inode
->i_private
;
3212 struct trace_iterator
*iter
;
3215 if (trace_array_get(tr
) < 0)
3218 /* If this file was open for write, then erase contents */
3219 if ((file
->f_mode
& FMODE_WRITE
) && (file
->f_flags
& O_TRUNC
)) {
3220 int cpu
= tracing_get_cpu(inode
);
3222 if (cpu
== RING_BUFFER_ALL_CPUS
)
3223 tracing_reset_online_cpus(&tr
->trace_buffer
);
3225 tracing_reset(&tr
->trace_buffer
, cpu
);
3228 if (file
->f_mode
& FMODE_READ
) {
3229 iter
= __tracing_open(inode
, file
, false);
3231 ret
= PTR_ERR(iter
);
3232 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
3233 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
3237 trace_array_put(tr
);
3243 * Some tracers are not suitable for instance buffers.
3244 * A tracer is always available for the global array (toplevel)
3245 * or if it explicitly states that it is.
3248 trace_ok_for_array(struct tracer
*t
, struct trace_array
*tr
)
3250 return (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
) || t
->allow_instances
;
3253 /* Find the next tracer that this trace array may use */
3254 static struct tracer
*
3255 get_tracer_for_array(struct trace_array
*tr
, struct tracer
*t
)
3257 while (t
&& !trace_ok_for_array(t
, tr
))
3264 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3266 struct trace_array
*tr
= m
->private;
3267 struct tracer
*t
= v
;
3272 t
= get_tracer_for_array(tr
, t
->next
);
3277 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
3279 struct trace_array
*tr
= m
->private;
3283 mutex_lock(&trace_types_lock
);
3285 t
= get_tracer_for_array(tr
, trace_types
);
3286 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
3292 static void t_stop(struct seq_file
*m
, void *p
)
3294 mutex_unlock(&trace_types_lock
);
3297 static int t_show(struct seq_file
*m
, void *v
)
3299 struct tracer
*t
= v
;
3304 seq_puts(m
, t
->name
);
3313 static const struct seq_operations show_traces_seq_ops
= {
3320 static int show_traces_open(struct inode
*inode
, struct file
*file
)
3322 struct trace_array
*tr
= inode
->i_private
;
3326 if (tracing_disabled
)
3329 ret
= seq_open(file
, &show_traces_seq_ops
);
3333 m
= file
->private_data
;
3340 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
3341 size_t count
, loff_t
*ppos
)
3346 loff_t
tracing_lseek(struct file
*file
, loff_t offset
, int whence
)
3350 if (file
->f_mode
& FMODE_READ
)
3351 ret
= seq_lseek(file
, offset
, whence
);
3353 file
->f_pos
= ret
= 0;
3358 static const struct file_operations tracing_fops
= {
3359 .open
= tracing_open
,
3361 .write
= tracing_write_stub
,
3362 .llseek
= tracing_lseek
,
3363 .release
= tracing_release
,
3366 static const struct file_operations show_traces_fops
= {
3367 .open
= show_traces_open
,
3369 .release
= seq_release
,
3370 .llseek
= seq_lseek
,
3374 * The tracer itself will not take this lock, but still we want
3375 * to provide a consistent cpumask to user-space:
3377 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
3380 * Temporary storage for the character representation of the
3381 * CPU bitmask (and one more byte for the newline):
3383 static char mask_str
[NR_CPUS
+ 1];
3386 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
3387 size_t count
, loff_t
*ppos
)
3389 struct trace_array
*tr
= file_inode(filp
)->i_private
;
3392 mutex_lock(&tracing_cpumask_update_lock
);
3394 len
= snprintf(mask_str
, count
, "%*pb\n",
3395 cpumask_pr_args(tr
->tracing_cpumask
));
3400 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
3403 mutex_unlock(&tracing_cpumask_update_lock
);
3409 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
3410 size_t count
, loff_t
*ppos
)
3412 struct trace_array
*tr
= file_inode(filp
)->i_private
;
3413 cpumask_var_t tracing_cpumask_new
;
3416 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
3419 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
3423 mutex_lock(&tracing_cpumask_update_lock
);
3425 local_irq_disable();
3426 arch_spin_lock(&tr
->max_lock
);
3427 for_each_tracing_cpu(cpu
) {
3429 * Increase/decrease the disabled counter if we are
3430 * about to flip a bit in the cpumask:
3432 if (cpumask_test_cpu(cpu
, tr
->tracing_cpumask
) &&
3433 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
3434 atomic_inc(&per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->disabled
);
3435 ring_buffer_record_disable_cpu(tr
->trace_buffer
.buffer
, cpu
);
3437 if (!cpumask_test_cpu(cpu
, tr
->tracing_cpumask
) &&
3438 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
3439 atomic_dec(&per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->disabled
);
3440 ring_buffer_record_enable_cpu(tr
->trace_buffer
.buffer
, cpu
);
3443 arch_spin_unlock(&tr
->max_lock
);
3446 cpumask_copy(tr
->tracing_cpumask
, tracing_cpumask_new
);
3448 mutex_unlock(&tracing_cpumask_update_lock
);
3449 free_cpumask_var(tracing_cpumask_new
);
3454 free_cpumask_var(tracing_cpumask_new
);
3459 static const struct file_operations tracing_cpumask_fops
= {
3460 .open
= tracing_open_generic_tr
,
3461 .read
= tracing_cpumask_read
,
3462 .write
= tracing_cpumask_write
,
3463 .release
= tracing_release_generic_tr
,
3464 .llseek
= generic_file_llseek
,
3467 static int tracing_trace_options_show(struct seq_file
*m
, void *v
)
3469 struct tracer_opt
*trace_opts
;
3470 struct trace_array
*tr
= m
->private;
3474 mutex_lock(&trace_types_lock
);
3475 tracer_flags
= tr
->current_trace
->flags
->val
;
3476 trace_opts
= tr
->current_trace
->flags
->opts
;
3478 for (i
= 0; trace_options
[i
]; i
++) {
3479 if (trace_flags
& (1 << i
))
3480 seq_printf(m
, "%s\n", trace_options
[i
]);
3482 seq_printf(m
, "no%s\n", trace_options
[i
]);
3485 for (i
= 0; trace_opts
[i
].name
; i
++) {
3486 if (tracer_flags
& trace_opts
[i
].bit
)
3487 seq_printf(m
, "%s\n", trace_opts
[i
].name
);
3489 seq_printf(m
, "no%s\n", trace_opts
[i
].name
);
3491 mutex_unlock(&trace_types_lock
);
3496 static int __set_tracer_option(struct trace_array
*tr
,
3497 struct tracer_flags
*tracer_flags
,
3498 struct tracer_opt
*opts
, int neg
)
3500 struct tracer
*trace
= tr
->current_trace
;
3503 ret
= trace
->set_flag(tr
, tracer_flags
->val
, opts
->bit
, !neg
);
3508 tracer_flags
->val
&= ~opts
->bit
;
3510 tracer_flags
->val
|= opts
->bit
;
3514 /* Try to assign a tracer specific option */
3515 static int set_tracer_option(struct trace_array
*tr
, char *cmp
, int neg
)
3517 struct tracer
*trace
= tr
->current_trace
;
3518 struct tracer_flags
*tracer_flags
= trace
->flags
;
3519 struct tracer_opt
*opts
= NULL
;
3522 for (i
= 0; tracer_flags
->opts
[i
].name
; i
++) {
3523 opts
= &tracer_flags
->opts
[i
];
3525 if (strcmp(cmp
, opts
->name
) == 0)
3526 return __set_tracer_option(tr
, trace
->flags
, opts
, neg
);
3532 /* Some tracers require overwrite to stay enabled */
3533 int trace_keep_overwrite(struct tracer
*tracer
, u32 mask
, int set
)
3535 if (tracer
->enabled
&& (mask
& TRACE_ITER_OVERWRITE
) && !set
)
3541 int set_tracer_flag(struct trace_array
*tr
, unsigned int mask
, int enabled
)
3543 /* do nothing if flag is already set */
3544 if (!!(trace_flags
& mask
) == !!enabled
)
3547 /* Give the tracer a chance to approve the change */
3548 if (tr
->current_trace
->flag_changed
)
3549 if (tr
->current_trace
->flag_changed(tr
, mask
, !!enabled
))
3553 trace_flags
|= mask
;
3555 trace_flags
&= ~mask
;
3557 if (mask
== TRACE_ITER_RECORD_CMD
)
3558 trace_event_enable_cmd_record(enabled
);
3560 if (mask
== TRACE_ITER_OVERWRITE
) {
3561 ring_buffer_change_overwrite(tr
->trace_buffer
.buffer
, enabled
);
3562 #ifdef CONFIG_TRACER_MAX_TRACE
3563 ring_buffer_change_overwrite(tr
->max_buffer
.buffer
, enabled
);
3567 if (mask
== TRACE_ITER_PRINTK
)
3568 trace_printk_start_stop_comm(enabled
);
3573 static int trace_set_options(struct trace_array
*tr
, char *option
)
3580 cmp
= strstrip(option
);
3582 if (strncmp(cmp
, "no", 2) == 0) {
3587 mutex_lock(&trace_types_lock
);
3589 for (i
= 0; trace_options
[i
]; i
++) {
3590 if (strcmp(cmp
, trace_options
[i
]) == 0) {
3591 ret
= set_tracer_flag(tr
, 1 << i
, !neg
);
3596 /* If no option could be set, test the specific tracer options */
3597 if (!trace_options
[i
])
3598 ret
= set_tracer_option(tr
, cmp
, neg
);
3600 mutex_unlock(&trace_types_lock
);
3606 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
3607 size_t cnt
, loff_t
*ppos
)
3609 struct seq_file
*m
= filp
->private_data
;
3610 struct trace_array
*tr
= m
->private;
3614 if (cnt
>= sizeof(buf
))
3617 if (copy_from_user(&buf
, ubuf
, cnt
))
3622 ret
= trace_set_options(tr
, buf
);
3631 static int tracing_trace_options_open(struct inode
*inode
, struct file
*file
)
3633 struct trace_array
*tr
= inode
->i_private
;
3636 if (tracing_disabled
)
3639 if (trace_array_get(tr
) < 0)
3642 ret
= single_open(file
, tracing_trace_options_show
, inode
->i_private
);
3644 trace_array_put(tr
);
3649 static const struct file_operations tracing_iter_fops
= {
3650 .open
= tracing_trace_options_open
,
3652 .llseek
= seq_lseek
,
3653 .release
= tracing_single_release_tr
,
3654 .write
= tracing_trace_options_write
,
3657 static const char readme_msg
[] =
3658 "tracing mini-HOWTO:\n\n"
3659 "# echo 0 > tracing_on : quick way to disable tracing\n"
3660 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3661 " Important files:\n"
3662 " trace\t\t\t- The static contents of the buffer\n"
3663 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3664 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3665 " current_tracer\t- function and latency tracers\n"
3666 " available_tracers\t- list of configured tracers for current_tracer\n"
3667 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3668 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3669 " trace_clock\t\t-change the clock used to order events\n"
3670 " local: Per cpu clock but may not be synced across CPUs\n"
3671 " global: Synced across CPUs but slows tracing down.\n"
3672 " counter: Not a clock, but just an increment\n"
3673 " uptime: Jiffy counter from time of boot\n"
3674 " perf: Same clock that perf events use\n"
3675 #ifdef CONFIG_X86_64
3676 " x86-tsc: TSC cycle counter\n"
3678 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3679 " tracing_cpumask\t- Limit which CPUs to trace\n"
3680 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3681 "\t\t\t Remove sub-buffer with rmdir\n"
3682 " trace_options\t\t- Set format or modify how tracing happens\n"
3683 "\t\t\t Disable an option by adding a suffix 'no' to the\n"
3684 "\t\t\t option name\n"
3685 " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
3686 #ifdef CONFIG_DYNAMIC_FTRACE
3687 "\n available_filter_functions - list of functions that can be filtered on\n"
3688 " set_ftrace_filter\t- echo function name in here to only trace these\n"
3689 "\t\t\t functions\n"
3690 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3691 "\t modules: Can select a group via module\n"
3692 "\t Format: :mod:<module-name>\n"
3693 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
3694 "\t triggers: a command to perform when function is hit\n"
3695 "\t Format: <function>:<trigger>[:count]\n"
3696 "\t trigger: traceon, traceoff\n"
3697 "\t\t enable_event:<system>:<event>\n"
3698 "\t\t disable_event:<system>:<event>\n"
3699 #ifdef CONFIG_STACKTRACE
3702 #ifdef CONFIG_TRACER_SNAPSHOT
3707 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
3708 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
3709 "\t The first one will disable tracing every time do_fault is hit\n"
3710 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
3711 "\t The first time do trap is hit and it disables tracing, the\n"
3712 "\t counter will decrement to 2. If tracing is already disabled,\n"
3713 "\t the counter will not decrement. It only decrements when the\n"
3714 "\t trigger did work\n"
3715 "\t To remove trigger without count:\n"
3716 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
3717 "\t To remove trigger with a count:\n"
3718 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
3719 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
3720 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3721 "\t modules: Can select a group via module command :mod:\n"
3722 "\t Does not accept triggers\n"
3723 #endif /* CONFIG_DYNAMIC_FTRACE */
3724 #ifdef CONFIG_FUNCTION_TRACER
3725 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
3728 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3729 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3730 " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n"
3731 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3733 #ifdef CONFIG_TRACER_SNAPSHOT
3734 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
3735 "\t\t\t snapshot buffer. Read the contents for more\n"
3736 "\t\t\t information\n"
3738 #ifdef CONFIG_STACK_TRACER
3739 " stack_trace\t\t- Shows the max stack trace when active\n"
3740 " stack_max_size\t- Shows current max stack size that was traced\n"
3741 "\t\t\t Write into this file to reset the max size (trigger a\n"
3742 "\t\t\t new trace)\n"
3743 #ifdef CONFIG_DYNAMIC_FTRACE
3744 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
3747 #endif /* CONFIG_STACK_TRACER */
3748 " events/\t\t- Directory containing all trace event subsystems:\n"
3749 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
3750 " events/<system>/\t- Directory containing all trace events for <system>:\n"
3751 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
3753 " filter\t\t- If set, only events passing filter are traced\n"
3754 " events/<system>/<event>/\t- Directory containing control files for\n"
3756 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
3757 " filter\t\t- If set, only events passing filter are traced\n"
3758 " trigger\t\t- If set, a command to perform when event is hit\n"
3759 "\t Format: <trigger>[:count][if <filter>]\n"
3760 "\t trigger: traceon, traceoff\n"
3761 "\t enable_event:<system>:<event>\n"
3762 "\t disable_event:<system>:<event>\n"
3763 #ifdef CONFIG_STACKTRACE
3766 #ifdef CONFIG_TRACER_SNAPSHOT
3769 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
3770 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
3771 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
3772 "\t events/block/block_unplug/trigger\n"
3773 "\t The first disables tracing every time block_unplug is hit.\n"
3774 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
3775 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
3776 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
3777 "\t Like function triggers, the counter is only decremented if it\n"
3778 "\t enabled or disabled tracing.\n"
3779 "\t To remove a trigger without a count:\n"
3780 "\t echo '!<trigger> > <system>/<event>/trigger\n"
3781 "\t To remove a trigger with a count:\n"
3782 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
3783 "\t Filters can be ignored when removing a trigger.\n"
3787 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
3788 size_t cnt
, loff_t
*ppos
)
3790 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
3791 readme_msg
, strlen(readme_msg
));
3794 static const struct file_operations tracing_readme_fops
= {
3795 .open
= tracing_open_generic
,
3796 .read
= tracing_readme_read
,
3797 .llseek
= generic_file_llseek
,
3800 static void *saved_cmdlines_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3802 unsigned int *ptr
= v
;
3804 if (*pos
|| m
->count
)
3809 for (; ptr
< &savedcmd
->map_cmdline_to_pid
[savedcmd
->cmdline_num
];
3811 if (*ptr
== -1 || *ptr
== NO_CMDLINE_MAP
)
3820 static void *saved_cmdlines_start(struct seq_file
*m
, loff_t
*pos
)
3826 arch_spin_lock(&trace_cmdline_lock
);
3828 v
= &savedcmd
->map_cmdline_to_pid
[0];
3830 v
= saved_cmdlines_next(m
, v
, &l
);
3838 static void saved_cmdlines_stop(struct seq_file
*m
, void *v
)
3840 arch_spin_unlock(&trace_cmdline_lock
);
3844 static int saved_cmdlines_show(struct seq_file
*m
, void *v
)
3846 char buf
[TASK_COMM_LEN
];
3847 unsigned int *pid
= v
;
3849 __trace_find_cmdline(*pid
, buf
);
3850 seq_printf(m
, "%d %s\n", *pid
, buf
);
3854 static const struct seq_operations tracing_saved_cmdlines_seq_ops
= {
3855 .start
= saved_cmdlines_start
,
3856 .next
= saved_cmdlines_next
,
3857 .stop
= saved_cmdlines_stop
,
3858 .show
= saved_cmdlines_show
,
3861 static int tracing_saved_cmdlines_open(struct inode
*inode
, struct file
*filp
)
3863 if (tracing_disabled
)
3866 return seq_open(filp
, &tracing_saved_cmdlines_seq_ops
);
3869 static const struct file_operations tracing_saved_cmdlines_fops
= {
3870 .open
= tracing_saved_cmdlines_open
,
3872 .llseek
= seq_lseek
,
3873 .release
= seq_release
,
3877 tracing_saved_cmdlines_size_read(struct file
*filp
, char __user
*ubuf
,
3878 size_t cnt
, loff_t
*ppos
)
3883 arch_spin_lock(&trace_cmdline_lock
);
3884 r
= scnprintf(buf
, sizeof(buf
), "%u\n", savedcmd
->cmdline_num
);
3885 arch_spin_unlock(&trace_cmdline_lock
);
3887 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3890 static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer
*s
)
3892 kfree(s
->saved_cmdlines
);
3893 kfree(s
->map_cmdline_to_pid
);
3897 static int tracing_resize_saved_cmdlines(unsigned int val
)
3899 struct saved_cmdlines_buffer
*s
, *savedcmd_temp
;
3901 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
3905 if (allocate_cmdlines_buffer(val
, s
) < 0) {
3910 arch_spin_lock(&trace_cmdline_lock
);
3911 savedcmd_temp
= savedcmd
;
3913 arch_spin_unlock(&trace_cmdline_lock
);
3914 free_saved_cmdlines_buffer(savedcmd_temp
);
3920 tracing_saved_cmdlines_size_write(struct file
*filp
, const char __user
*ubuf
,
3921 size_t cnt
, loff_t
*ppos
)
3926 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
3930 /* must have at least 1 entry or less than PID_MAX_DEFAULT */
3931 if (!val
|| val
> PID_MAX_DEFAULT
)
3934 ret
= tracing_resize_saved_cmdlines((unsigned int)val
);
3943 static const struct file_operations tracing_saved_cmdlines_size_fops
= {
3944 .open
= tracing_open_generic
,
3945 .read
= tracing_saved_cmdlines_size_read
,
3946 .write
= tracing_saved_cmdlines_size_write
,
3949 #ifdef CONFIG_TRACE_ENUM_MAP_FILE
3950 static union trace_enum_map_item
*
3951 update_enum_map(union trace_enum_map_item
*ptr
)
3953 if (!ptr
->map
.enum_string
) {
3954 if (ptr
->tail
.next
) {
3955 ptr
= ptr
->tail
.next
;
3956 /* Set ptr to the next real item (skip head) */
3964 static void *enum_map_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3966 union trace_enum_map_item
*ptr
= v
;
3969 * Paranoid! If ptr points to end, we don't want to increment past it.
3970 * This really should never happen.
3972 ptr
= update_enum_map(ptr
);
3973 if (WARN_ON_ONCE(!ptr
))
3980 ptr
= update_enum_map(ptr
);
3985 static void *enum_map_start(struct seq_file
*m
, loff_t
*pos
)
3987 union trace_enum_map_item
*v
;
3990 mutex_lock(&trace_enum_mutex
);
3992 v
= trace_enum_maps
;
3996 while (v
&& l
< *pos
) {
3997 v
= enum_map_next(m
, v
, &l
);
4003 static void enum_map_stop(struct seq_file
*m
, void *v
)
4005 mutex_unlock(&trace_enum_mutex
);
4008 static int enum_map_show(struct seq_file
*m
, void *v
)
4010 union trace_enum_map_item
*ptr
= v
;
4012 seq_printf(m
, "%s %ld (%s)\n",
4013 ptr
->map
.enum_string
, ptr
->map
.enum_value
,
4019 static const struct seq_operations tracing_enum_map_seq_ops
= {
4020 .start
= enum_map_start
,
4021 .next
= enum_map_next
,
4022 .stop
= enum_map_stop
,
4023 .show
= enum_map_show
,
4026 static int tracing_enum_map_open(struct inode
*inode
, struct file
*filp
)
4028 if (tracing_disabled
)
4031 return seq_open(filp
, &tracing_enum_map_seq_ops
);
4034 static const struct file_operations tracing_enum_map_fops
= {
4035 .open
= tracing_enum_map_open
,
4037 .llseek
= seq_lseek
,
4038 .release
= seq_release
,
4041 static inline union trace_enum_map_item
*
4042 trace_enum_jmp_to_tail(union trace_enum_map_item
*ptr
)
4044 /* Return tail of array given the head */
4045 return ptr
+ ptr
->head
.length
+ 1;
4049 trace_insert_enum_map_file(struct module
*mod
, struct trace_enum_map
**start
,
4052 struct trace_enum_map
**stop
;
4053 struct trace_enum_map
**map
;
4054 union trace_enum_map_item
*map_array
;
4055 union trace_enum_map_item
*ptr
;
4060 * The trace_enum_maps contains the map plus a head and tail item,
4061 * where the head holds the module and length of array, and the
4062 * tail holds a pointer to the next list.
4064 map_array
= kmalloc(sizeof(*map_array
) * (len
+ 2), GFP_KERNEL
);
4066 pr_warning("Unable to allocate trace enum mapping\n");
4070 mutex_lock(&trace_enum_mutex
);
4072 if (!trace_enum_maps
)
4073 trace_enum_maps
= map_array
;
4075 ptr
= trace_enum_maps
;
4077 ptr
= trace_enum_jmp_to_tail(ptr
);
4078 if (!ptr
->tail
.next
)
4080 ptr
= ptr
->tail
.next
;
4083 ptr
->tail
.next
= map_array
;
4085 map_array
->head
.mod
= mod
;
4086 map_array
->head
.length
= len
;
4089 for (map
= start
; (unsigned long)map
< (unsigned long)stop
; map
++) {
4090 map_array
->map
= **map
;
4093 memset(map_array
, 0, sizeof(*map_array
));
4095 mutex_unlock(&trace_enum_mutex
);
4098 static void trace_create_enum_file(struct dentry
*d_tracer
)
4100 trace_create_file("enum_map", 0444, d_tracer
,
4101 NULL
, &tracing_enum_map_fops
);
4104 #else /* CONFIG_TRACE_ENUM_MAP_FILE */
4105 static inline void trace_create_enum_file(struct dentry
*d_tracer
) { }
4106 static inline void trace_insert_enum_map_file(struct module
*mod
,
4107 struct trace_enum_map
**start
, int len
) { }
4108 #endif /* !CONFIG_TRACE_ENUM_MAP_FILE */
4110 static void trace_insert_enum_map(struct module
*mod
,
4111 struct trace_enum_map
**start
, int len
)
4113 struct trace_enum_map
**map
;
4120 trace_event_enum_update(map
, len
);
4122 trace_insert_enum_map_file(mod
, start
, len
);
4126 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
4127 size_t cnt
, loff_t
*ppos
)
4129 struct trace_array
*tr
= filp
->private_data
;
4130 char buf
[MAX_TRACER_SIZE
+2];
4133 mutex_lock(&trace_types_lock
);
4134 r
= sprintf(buf
, "%s\n", tr
->current_trace
->name
);
4135 mutex_unlock(&trace_types_lock
);
4137 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
4140 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
4142 tracing_reset_online_cpus(&tr
->trace_buffer
);
4146 static void set_buffer_entries(struct trace_buffer
*buf
, unsigned long val
)
4150 for_each_tracing_cpu(cpu
)
4151 per_cpu_ptr(buf
->data
, cpu
)->entries
= val
;
4154 #ifdef CONFIG_TRACER_MAX_TRACE
4155 /* resize @tr's buffer to the size of @size_tr's entries */
4156 static int resize_buffer_duplicate_size(struct trace_buffer
*trace_buf
,
4157 struct trace_buffer
*size_buf
, int cpu_id
)
4161 if (cpu_id
== RING_BUFFER_ALL_CPUS
) {
4162 for_each_tracing_cpu(cpu
) {
4163 ret
= ring_buffer_resize(trace_buf
->buffer
,
4164 per_cpu_ptr(size_buf
->data
, cpu
)->entries
, cpu
);
4167 per_cpu_ptr(trace_buf
->data
, cpu
)->entries
=
4168 per_cpu_ptr(size_buf
->data
, cpu
)->entries
;
4171 ret
= ring_buffer_resize(trace_buf
->buffer
,
4172 per_cpu_ptr(size_buf
->data
, cpu_id
)->entries
, cpu_id
);
4174 per_cpu_ptr(trace_buf
->data
, cpu_id
)->entries
=
4175 per_cpu_ptr(size_buf
->data
, cpu_id
)->entries
;
4180 #endif /* CONFIG_TRACER_MAX_TRACE */
4182 static int __tracing_resize_ring_buffer(struct trace_array
*tr
,
4183 unsigned long size
, int cpu
)
4188 * If kernel or user changes the size of the ring buffer
4189 * we use the size that was given, and we can forget about
4190 * expanding it later.
4192 ring_buffer_expanded
= true;
4194 /* May be called before buffers are initialized */
4195 if (!tr
->trace_buffer
.buffer
)
4198 ret
= ring_buffer_resize(tr
->trace_buffer
.buffer
, size
, cpu
);
4202 #ifdef CONFIG_TRACER_MAX_TRACE
4203 if (!(tr
->flags
& TRACE_ARRAY_FL_GLOBAL
) ||
4204 !tr
->current_trace
->use_max_tr
)
4207 ret
= ring_buffer_resize(tr
->max_buffer
.buffer
, size
, cpu
);
4209 int r
= resize_buffer_duplicate_size(&tr
->trace_buffer
,
4210 &tr
->trace_buffer
, cpu
);
4213 * AARGH! We are left with different
4214 * size max buffer!!!!
4215 * The max buffer is our "snapshot" buffer.
4216 * When a tracer needs a snapshot (one of the
4217 * latency tracers), it swaps the max buffer
4218 * with the saved snap shot. We succeeded to
4219 * update the size of the main buffer, but failed to
4220 * update the size of the max buffer. But when we tried
4221 * to reset the main buffer to the original size, we
4222 * failed there too. This is very unlikely to
4223 * happen, but if it does, warn and kill all
4227 tracing_disabled
= 1;
4232 if (cpu
== RING_BUFFER_ALL_CPUS
)
4233 set_buffer_entries(&tr
->max_buffer
, size
);
4235 per_cpu_ptr(tr
->max_buffer
.data
, cpu
)->entries
= size
;
4238 #endif /* CONFIG_TRACER_MAX_TRACE */
4240 if (cpu
== RING_BUFFER_ALL_CPUS
)
4241 set_buffer_entries(&tr
->trace_buffer
, size
);
4243 per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->entries
= size
;
4248 static ssize_t
tracing_resize_ring_buffer(struct trace_array
*tr
,
4249 unsigned long size
, int cpu_id
)
4253 mutex_lock(&trace_types_lock
);
4255 if (cpu_id
!= RING_BUFFER_ALL_CPUS
) {
4256 /* make sure, this cpu is enabled in the mask */
4257 if (!cpumask_test_cpu(cpu_id
, tracing_buffer_mask
)) {
4263 ret
= __tracing_resize_ring_buffer(tr
, size
, cpu_id
);
4268 mutex_unlock(&trace_types_lock
);
4275 * tracing_update_buffers - used by tracing facility to expand ring buffers
4277 * To save on memory when the tracing is never used on a system with it
4278 * configured in. The ring buffers are set to a minimum size. But once
4279 * a user starts to use the tracing facility, then they need to grow
4280 * to their default size.
4282 * This function is to be called when a tracer is about to be used.
4284 int tracing_update_buffers(void)
4288 mutex_lock(&trace_types_lock
);
4289 if (!ring_buffer_expanded
)
4290 ret
= __tracing_resize_ring_buffer(&global_trace
, trace_buf_size
,
4291 RING_BUFFER_ALL_CPUS
);
4292 mutex_unlock(&trace_types_lock
);
4297 struct trace_option_dentry
;
4299 static struct trace_option_dentry
*
4300 create_trace_option_files(struct trace_array
*tr
, struct tracer
*tracer
);
4303 destroy_trace_option_files(struct trace_option_dentry
*topts
);
4306 * Used to clear out the tracer before deletion of an instance.
4307 * Must have trace_types_lock held.
4309 static void tracing_set_nop(struct trace_array
*tr
)
4311 if (tr
->current_trace
== &nop_trace
)
4314 tr
->current_trace
->enabled
--;
4316 if (tr
->current_trace
->reset
)
4317 tr
->current_trace
->reset(tr
);
4319 tr
->current_trace
= &nop_trace
;
4322 static void update_tracer_options(struct trace_array
*tr
, struct tracer
*t
)
4324 static struct trace_option_dentry
*topts
;
4326 /* Only enable if the directory has been created already. */
4330 /* Currently, only the top instance has options */
4331 if (!(tr
->flags
& TRACE_ARRAY_FL_GLOBAL
))
4334 destroy_trace_option_files(topts
);
4335 topts
= create_trace_option_files(tr
, t
);
4338 static int tracing_set_tracer(struct trace_array
*tr
, const char *buf
)
4341 #ifdef CONFIG_TRACER_MAX_TRACE
4346 mutex_lock(&trace_types_lock
);
4348 if (!ring_buffer_expanded
) {
4349 ret
= __tracing_resize_ring_buffer(tr
, trace_buf_size
,
4350 RING_BUFFER_ALL_CPUS
);
4356 for (t
= trace_types
; t
; t
= t
->next
) {
4357 if (strcmp(t
->name
, buf
) == 0)
4364 if (t
== tr
->current_trace
)
4367 /* Some tracers are only allowed for the top level buffer */
4368 if (!trace_ok_for_array(t
, tr
)) {
4373 /* If trace pipe files are being read, we can't change the tracer */
4374 if (tr
->current_trace
->ref
) {
4379 trace_branch_disable();
4381 tr
->current_trace
->enabled
--;
4383 if (tr
->current_trace
->reset
)
4384 tr
->current_trace
->reset(tr
);
4386 /* Current trace needs to be nop_trace before synchronize_sched */
4387 tr
->current_trace
= &nop_trace
;
4389 #ifdef CONFIG_TRACER_MAX_TRACE
4390 had_max_tr
= tr
->allocated_snapshot
;
4392 if (had_max_tr
&& !t
->use_max_tr
) {
4394 * We need to make sure that the update_max_tr sees that
4395 * current_trace changed to nop_trace to keep it from
4396 * swapping the buffers after we resize it.
4397 * The update_max_tr is called from interrupts disabled
4398 * so a synchronized_sched() is sufficient.
4400 synchronize_sched();
4404 update_tracer_options(tr
, t
);
4406 #ifdef CONFIG_TRACER_MAX_TRACE
4407 if (t
->use_max_tr
&& !had_max_tr
) {
4408 ret
= alloc_snapshot(tr
);
4415 ret
= tracer_init(t
, tr
);
4420 tr
->current_trace
= t
;
4421 tr
->current_trace
->enabled
++;
4422 trace_branch_enable(tr
);
4424 mutex_unlock(&trace_types_lock
);
4430 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
4431 size_t cnt
, loff_t
*ppos
)
4433 struct trace_array
*tr
= filp
->private_data
;
4434 char buf
[MAX_TRACER_SIZE
+1];
4441 if (cnt
> MAX_TRACER_SIZE
)
4442 cnt
= MAX_TRACER_SIZE
;
4444 if (copy_from_user(&buf
, ubuf
, cnt
))
4449 /* strip ending whitespace. */
4450 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
4453 err
= tracing_set_tracer(tr
, buf
);
4463 tracing_nsecs_read(unsigned long *ptr
, char __user
*ubuf
,
4464 size_t cnt
, loff_t
*ppos
)
4469 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
4470 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
4471 if (r
> sizeof(buf
))
4473 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
4477 tracing_nsecs_write(unsigned long *ptr
, const char __user
*ubuf
,
4478 size_t cnt
, loff_t
*ppos
)
4483 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
4493 tracing_thresh_read(struct file
*filp
, char __user
*ubuf
,
4494 size_t cnt
, loff_t
*ppos
)
4496 return tracing_nsecs_read(&tracing_thresh
, ubuf
, cnt
, ppos
);
4500 tracing_thresh_write(struct file
*filp
, const char __user
*ubuf
,
4501 size_t cnt
, loff_t
*ppos
)
4503 struct trace_array
*tr
= filp
->private_data
;
4506 mutex_lock(&trace_types_lock
);
4507 ret
= tracing_nsecs_write(&tracing_thresh
, ubuf
, cnt
, ppos
);
4511 if (tr
->current_trace
->update_thresh
) {
4512 ret
= tr
->current_trace
->update_thresh(tr
);
4519 mutex_unlock(&trace_types_lock
);
4525 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
4526 size_t cnt
, loff_t
*ppos
)
4528 return tracing_nsecs_read(filp
->private_data
, ubuf
, cnt
, ppos
);
4532 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
4533 size_t cnt
, loff_t
*ppos
)
4535 return tracing_nsecs_write(filp
->private_data
, ubuf
, cnt
, ppos
);
4538 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
4540 struct trace_array
*tr
= inode
->i_private
;
4541 struct trace_iterator
*iter
;
4544 if (tracing_disabled
)
4547 if (trace_array_get(tr
) < 0)
4550 mutex_lock(&trace_types_lock
);
4552 /* create a buffer to store the information to pass to userspace */
4553 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
4556 __trace_array_put(tr
);
4560 trace_seq_init(&iter
->seq
);
4561 iter
->trace
= tr
->current_trace
;
4563 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
4568 /* trace pipe does not show start of buffer */
4569 cpumask_setall(iter
->started
);
4571 if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
4572 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
4574 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
4575 if (trace_clocks
[tr
->clock_id
].in_ns
)
4576 iter
->iter_flags
|= TRACE_FILE_TIME_IN_NS
;
4579 iter
->trace_buffer
= &tr
->trace_buffer
;
4580 iter
->cpu_file
= tracing_get_cpu(inode
);
4581 mutex_init(&iter
->mutex
);
4582 filp
->private_data
= iter
;
4584 if (iter
->trace
->pipe_open
)
4585 iter
->trace
->pipe_open(iter
);
4587 nonseekable_open(inode
, filp
);
4589 tr
->current_trace
->ref
++;
4591 mutex_unlock(&trace_types_lock
);
4597 __trace_array_put(tr
);
4598 mutex_unlock(&trace_types_lock
);
4602 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
4604 struct trace_iterator
*iter
= file
->private_data
;
4605 struct trace_array
*tr
= inode
->i_private
;
4607 mutex_lock(&trace_types_lock
);
4609 tr
->current_trace
->ref
--;
4611 if (iter
->trace
->pipe_close
)
4612 iter
->trace
->pipe_close(iter
);
4614 mutex_unlock(&trace_types_lock
);
4616 free_cpumask_var(iter
->started
);
4617 mutex_destroy(&iter
->mutex
);
4620 trace_array_put(tr
);
4626 trace_poll(struct trace_iterator
*iter
, struct file
*filp
, poll_table
*poll_table
)
4628 /* Iterators are static, they should be filled or empty */
4629 if (trace_buffer_iter(iter
, iter
->cpu_file
))
4630 return POLLIN
| POLLRDNORM
;
4632 if (trace_flags
& TRACE_ITER_BLOCK
)
4634 * Always select as readable when in blocking mode
4636 return POLLIN
| POLLRDNORM
;
4638 return ring_buffer_poll_wait(iter
->trace_buffer
->buffer
, iter
->cpu_file
,
4643 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
4645 struct trace_iterator
*iter
= filp
->private_data
;
4647 return trace_poll(iter
, filp
, poll_table
);
4650 /* Must be called with iter->mutex held. */
4651 static int tracing_wait_pipe(struct file
*filp
)
4653 struct trace_iterator
*iter
= filp
->private_data
;
4656 while (trace_empty(iter
)) {
4658 if ((filp
->f_flags
& O_NONBLOCK
)) {
4663 * We block until we read something and tracing is disabled.
4664 * We still block if tracing is disabled, but we have never
4665 * read anything. This allows a user to cat this file, and
4666 * then enable tracing. But after we have read something,
4667 * we give an EOF when tracing is again disabled.
4669 * iter->pos will be 0 if we haven't read anything.
4671 if (!tracing_is_on() && iter
->pos
)
4674 mutex_unlock(&iter
->mutex
);
4676 ret
= wait_on_pipe(iter
, false);
4678 mutex_lock(&iter
->mutex
);
4691 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
4692 size_t cnt
, loff_t
*ppos
)
4694 struct trace_iterator
*iter
= filp
->private_data
;
4697 /* return any leftover data */
4698 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
4702 trace_seq_init(&iter
->seq
);
4705 * Avoid more than one consumer on a single file descriptor
4706 * This is just a matter of traces coherency, the ring buffer itself
4709 mutex_lock(&iter
->mutex
);
4710 if (iter
->trace
->read
) {
4711 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
4717 sret
= tracing_wait_pipe(filp
);
4721 /* stop when tracing is finished */
4722 if (trace_empty(iter
)) {
4727 if (cnt
>= PAGE_SIZE
)
4728 cnt
= PAGE_SIZE
- 1;
4730 /* reset all but tr, trace, and overruns */
4731 memset(&iter
->seq
, 0,
4732 sizeof(struct trace_iterator
) -
4733 offsetof(struct trace_iterator
, seq
));
4734 cpumask_clear(iter
->started
);
4737 trace_event_read_lock();
4738 trace_access_lock(iter
->cpu_file
);
4739 while (trace_find_next_entry_inc(iter
) != NULL
) {
4740 enum print_line_t ret
;
4741 int save_len
= iter
->seq
.seq
.len
;
4743 ret
= print_trace_line(iter
);
4744 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
4745 /* don't print partial lines */
4746 iter
->seq
.seq
.len
= save_len
;
4749 if (ret
!= TRACE_TYPE_NO_CONSUME
)
4750 trace_consume(iter
);
4752 if (trace_seq_used(&iter
->seq
) >= cnt
)
4756 * Setting the full flag means we reached the trace_seq buffer
4757 * size and we should leave by partial output condition above.
4758 * One of the trace_seq_* functions is not used properly.
4760 WARN_ONCE(iter
->seq
.full
, "full flag set for trace type %d",
4763 trace_access_unlock(iter
->cpu_file
);
4764 trace_event_read_unlock();
4766 /* Now copy what we have to the user */
4767 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
4768 if (iter
->seq
.seq
.readpos
>= trace_seq_used(&iter
->seq
))
4769 trace_seq_init(&iter
->seq
);
4772 * If there was nothing to send to user, in spite of consuming trace
4773 * entries, go back to wait for more entries.
4779 mutex_unlock(&iter
->mutex
);
4784 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
4787 __free_page(spd
->pages
[idx
]);
4790 static const struct pipe_buf_operations tracing_pipe_buf_ops
= {
4792 .confirm
= generic_pipe_buf_confirm
,
4793 .release
= generic_pipe_buf_release
,
4794 .steal
= generic_pipe_buf_steal
,
4795 .get
= generic_pipe_buf_get
,
4799 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
4805 /* Seq buffer is page-sized, exactly what we need. */
4807 save_len
= iter
->seq
.seq
.len
;
4808 ret
= print_trace_line(iter
);
4810 if (trace_seq_has_overflowed(&iter
->seq
)) {
4811 iter
->seq
.seq
.len
= save_len
;
4816 * This should not be hit, because it should only
4817 * be set if the iter->seq overflowed. But check it
4818 * anyway to be safe.
4820 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
4821 iter
->seq
.seq
.len
= save_len
;
4825 count
= trace_seq_used(&iter
->seq
) - save_len
;
4828 iter
->seq
.seq
.len
= save_len
;
4832 if (ret
!= TRACE_TYPE_NO_CONSUME
)
4833 trace_consume(iter
);
4835 if (!trace_find_next_entry_inc(iter
)) {
4845 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
4847 struct pipe_inode_info
*pipe
,
4851 struct page
*pages_def
[PIPE_DEF_BUFFERS
];
4852 struct partial_page partial_def
[PIPE_DEF_BUFFERS
];
4853 struct trace_iterator
*iter
= filp
->private_data
;
4854 struct splice_pipe_desc spd
= {
4856 .partial
= partial_def
,
4857 .nr_pages
= 0, /* This gets updated below. */
4858 .nr_pages_max
= PIPE_DEF_BUFFERS
,
4860 .ops
= &tracing_pipe_buf_ops
,
4861 .spd_release
= tracing_spd_release_pipe
,
4867 if (splice_grow_spd(pipe
, &spd
))
4870 mutex_lock(&iter
->mutex
);
4872 if (iter
->trace
->splice_read
) {
4873 ret
= iter
->trace
->splice_read(iter
, filp
,
4874 ppos
, pipe
, len
, flags
);
4879 ret
= tracing_wait_pipe(filp
);
4883 if (!iter
->ent
&& !trace_find_next_entry_inc(iter
)) {
4888 trace_event_read_lock();
4889 trace_access_lock(iter
->cpu_file
);
4891 /* Fill as many pages as possible. */
4892 for (i
= 0, rem
= len
; i
< spd
.nr_pages_max
&& rem
; i
++) {
4893 spd
.pages
[i
] = alloc_page(GFP_KERNEL
);
4897 rem
= tracing_fill_pipe_page(rem
, iter
);
4899 /* Copy the data into the page, so we can start over. */
4900 ret
= trace_seq_to_buffer(&iter
->seq
,
4901 page_address(spd
.pages
[i
]),
4902 trace_seq_used(&iter
->seq
));
4904 __free_page(spd
.pages
[i
]);
4907 spd
.partial
[i
].offset
= 0;
4908 spd
.partial
[i
].len
= trace_seq_used(&iter
->seq
);
4910 trace_seq_init(&iter
->seq
);
4913 trace_access_unlock(iter
->cpu_file
);
4914 trace_event_read_unlock();
4915 mutex_unlock(&iter
->mutex
);
4919 ret
= splice_to_pipe(pipe
, &spd
);
4921 splice_shrink_spd(&spd
);
4925 mutex_unlock(&iter
->mutex
);
4930 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
4931 size_t cnt
, loff_t
*ppos
)
4933 struct inode
*inode
= file_inode(filp
);
4934 struct trace_array
*tr
= inode
->i_private
;
4935 int cpu
= tracing_get_cpu(inode
);
4940 mutex_lock(&trace_types_lock
);
4942 if (cpu
== RING_BUFFER_ALL_CPUS
) {
4943 int cpu
, buf_size_same
;
4948 /* check if all cpu sizes are same */
4949 for_each_tracing_cpu(cpu
) {
4950 /* fill in the size from first enabled cpu */
4952 size
= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->entries
;
4953 if (size
!= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->entries
) {
4959 if (buf_size_same
) {
4960 if (!ring_buffer_expanded
)
4961 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
4963 trace_buf_size
>> 10);
4965 r
= sprintf(buf
, "%lu\n", size
>> 10);
4967 r
= sprintf(buf
, "X\n");
4969 r
= sprintf(buf
, "%lu\n", per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->entries
>> 10);
4971 mutex_unlock(&trace_types_lock
);
4973 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
4978 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
4979 size_t cnt
, loff_t
*ppos
)
4981 struct inode
*inode
= file_inode(filp
);
4982 struct trace_array
*tr
= inode
->i_private
;
4986 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
4990 /* must have at least 1 entry */
4994 /* value is in KB */
4996 ret
= tracing_resize_ring_buffer(tr
, val
, tracing_get_cpu(inode
));
5006 tracing_total_entries_read(struct file
*filp
, char __user
*ubuf
,
5007 size_t cnt
, loff_t
*ppos
)
5009 struct trace_array
*tr
= filp
->private_data
;
5012 unsigned long size
= 0, expanded_size
= 0;
5014 mutex_lock(&trace_types_lock
);
5015 for_each_tracing_cpu(cpu
) {
5016 size
+= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->entries
>> 10;
5017 if (!ring_buffer_expanded
)
5018 expanded_size
+= trace_buf_size
>> 10;
5020 if (ring_buffer_expanded
)
5021 r
= sprintf(buf
, "%lu\n", size
);
5023 r
= sprintf(buf
, "%lu (expanded: %lu)\n", size
, expanded_size
);
5024 mutex_unlock(&trace_types_lock
);
5026 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
5030 tracing_free_buffer_write(struct file
*filp
, const char __user
*ubuf
,
5031 size_t cnt
, loff_t
*ppos
)
5034 * There is no need to read what the user has written, this function
5035 * is just to make sure that there is no error when "echo" is used
5044 tracing_free_buffer_release(struct inode
*inode
, struct file
*filp
)
5046 struct trace_array
*tr
= inode
->i_private
;
5048 /* disable tracing ? */
5049 if (trace_flags
& TRACE_ITER_STOP_ON_FREE
)
5050 tracer_tracing_off(tr
);
5051 /* resize the ring buffer to 0 */
5052 tracing_resize_ring_buffer(tr
, 0, RING_BUFFER_ALL_CPUS
);
5054 trace_array_put(tr
);
5060 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
5061 size_t cnt
, loff_t
*fpos
)
5063 unsigned long addr
= (unsigned long)ubuf
;
5064 struct trace_array
*tr
= filp
->private_data
;
5065 struct ring_buffer_event
*event
;
5066 struct ring_buffer
*buffer
;
5067 struct print_entry
*entry
;
5068 unsigned long irq_flags
;
5069 struct page
*pages
[2];
5079 if (tracing_disabled
)
5082 if (!(trace_flags
& TRACE_ITER_MARKERS
))
5085 if (cnt
> TRACE_BUF_SIZE
)
5086 cnt
= TRACE_BUF_SIZE
;
5089 * Userspace is injecting traces into the kernel trace buffer.
5090 * We want to be as non intrusive as possible.
5091 * To do so, we do not want to allocate any special buffers
5092 * or take any locks, but instead write the userspace data
5093 * straight into the ring buffer.
5095 * First we need to pin the userspace buffer into memory,
5096 * which, most likely it is, because it just referenced it.
5097 * But there's no guarantee that it is. By using get_user_pages_fast()
5098 * and kmap_atomic/kunmap_atomic() we can get access to the
5099 * pages directly. We then write the data directly into the
5102 BUILD_BUG_ON(TRACE_BUF_SIZE
>= PAGE_SIZE
);
5104 /* check if we cross pages */
5105 if ((addr
& PAGE_MASK
) != ((addr
+ cnt
) & PAGE_MASK
))
5108 offset
= addr
& (PAGE_SIZE
- 1);
5111 ret
= get_user_pages_fast(addr
, nr_pages
, 0, pages
);
5112 if (ret
< nr_pages
) {
5114 put_page(pages
[ret
]);
5119 for (i
= 0; i
< nr_pages
; i
++)
5120 map_page
[i
] = kmap_atomic(pages
[i
]);
5122 local_save_flags(irq_flags
);
5123 size
= sizeof(*entry
) + cnt
+ 2; /* possible \n added */
5124 buffer
= tr
->trace_buffer
.buffer
;
5125 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
5126 irq_flags
, preempt_count());
5128 /* Ring buffer disabled, return as if not open for write */
5133 entry
= ring_buffer_event_data(event
);
5134 entry
->ip
= _THIS_IP_
;
5136 if (nr_pages
== 2) {
5137 len
= PAGE_SIZE
- offset
;
5138 memcpy(&entry
->buf
, map_page
[0] + offset
, len
);
5139 memcpy(&entry
->buf
[len
], map_page
[1], cnt
- len
);
5141 memcpy(&entry
->buf
, map_page
[0] + offset
, cnt
);
5143 if (entry
->buf
[cnt
- 1] != '\n') {
5144 entry
->buf
[cnt
] = '\n';
5145 entry
->buf
[cnt
+ 1] = '\0';
5147 entry
->buf
[cnt
] = '\0';
5149 __buffer_unlock_commit(buffer
, event
);
5156 for (i
= nr_pages
- 1; i
>= 0; i
--) {
5157 kunmap_atomic(map_page
[i
]);
5164 static int tracing_clock_show(struct seq_file
*m
, void *v
)
5166 struct trace_array
*tr
= m
->private;
5169 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++)
5171 "%s%s%s%s", i
? " " : "",
5172 i
== tr
->clock_id
? "[" : "", trace_clocks
[i
].name
,
5173 i
== tr
->clock_id
? "]" : "");
5179 static int tracing_set_clock(struct trace_array
*tr
, const char *clockstr
)
5183 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++) {
5184 if (strcmp(trace_clocks
[i
].name
, clockstr
) == 0)
5187 if (i
== ARRAY_SIZE(trace_clocks
))
5190 mutex_lock(&trace_types_lock
);
5194 ring_buffer_set_clock(tr
->trace_buffer
.buffer
, trace_clocks
[i
].func
);
5197 * New clock may not be consistent with the previous clock.
5198 * Reset the buffer so that it doesn't have incomparable timestamps.
5200 tracing_reset_online_cpus(&tr
->trace_buffer
);
5202 #ifdef CONFIG_TRACER_MAX_TRACE
5203 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
&& tr
->max_buffer
.buffer
)
5204 ring_buffer_set_clock(tr
->max_buffer
.buffer
, trace_clocks
[i
].func
);
5205 tracing_reset_online_cpus(&tr
->max_buffer
);
5208 mutex_unlock(&trace_types_lock
);
5213 static ssize_t
tracing_clock_write(struct file
*filp
, const char __user
*ubuf
,
5214 size_t cnt
, loff_t
*fpos
)
5216 struct seq_file
*m
= filp
->private_data
;
5217 struct trace_array
*tr
= m
->private;
5219 const char *clockstr
;
5222 if (cnt
>= sizeof(buf
))
5225 if (copy_from_user(&buf
, ubuf
, cnt
))
5230 clockstr
= strstrip(buf
);
5232 ret
= tracing_set_clock(tr
, clockstr
);
5241 static int tracing_clock_open(struct inode
*inode
, struct file
*file
)
5243 struct trace_array
*tr
= inode
->i_private
;
5246 if (tracing_disabled
)
5249 if (trace_array_get(tr
))
5252 ret
= single_open(file
, tracing_clock_show
, inode
->i_private
);
5254 trace_array_put(tr
);
5259 struct ftrace_buffer_info
{
5260 struct trace_iterator iter
;
5265 #ifdef CONFIG_TRACER_SNAPSHOT
5266 static int tracing_snapshot_open(struct inode
*inode
, struct file
*file
)
5268 struct trace_array
*tr
= inode
->i_private
;
5269 struct trace_iterator
*iter
;
5273 if (trace_array_get(tr
) < 0)
5276 if (file
->f_mode
& FMODE_READ
) {
5277 iter
= __tracing_open(inode
, file
, true);
5279 ret
= PTR_ERR(iter
);
5281 /* Writes still need the seq_file to hold the private data */
5283 m
= kzalloc(sizeof(*m
), GFP_KERNEL
);
5286 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
5294 iter
->trace_buffer
= &tr
->max_buffer
;
5295 iter
->cpu_file
= tracing_get_cpu(inode
);
5297 file
->private_data
= m
;
5301 trace_array_put(tr
);
5307 tracing_snapshot_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
5310 struct seq_file
*m
= filp
->private_data
;
5311 struct trace_iterator
*iter
= m
->private;
5312 struct trace_array
*tr
= iter
->tr
;
5316 ret
= tracing_update_buffers();
5320 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
5324 mutex_lock(&trace_types_lock
);
5326 if (tr
->current_trace
->use_max_tr
) {
5333 if (iter
->cpu_file
!= RING_BUFFER_ALL_CPUS
) {
5337 if (tr
->allocated_snapshot
)
5341 /* Only allow per-cpu swap if the ring buffer supports it */
5342 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
5343 if (iter
->cpu_file
!= RING_BUFFER_ALL_CPUS
) {
5348 if (!tr
->allocated_snapshot
) {
5349 ret
= alloc_snapshot(tr
);
5353 local_irq_disable();
5354 /* Now, we're going to swap */
5355 if (iter
->cpu_file
== RING_BUFFER_ALL_CPUS
)
5356 update_max_tr(tr
, current
, smp_processor_id());
5358 update_max_tr_single(tr
, current
, iter
->cpu_file
);
5362 if (tr
->allocated_snapshot
) {
5363 if (iter
->cpu_file
== RING_BUFFER_ALL_CPUS
)
5364 tracing_reset_online_cpus(&tr
->max_buffer
);
5366 tracing_reset(&tr
->max_buffer
, iter
->cpu_file
);
5376 mutex_unlock(&trace_types_lock
);
5380 static int tracing_snapshot_release(struct inode
*inode
, struct file
*file
)
5382 struct seq_file
*m
= file
->private_data
;
5385 ret
= tracing_release(inode
, file
);
5387 if (file
->f_mode
& FMODE_READ
)
5390 /* If write only, the seq_file is just a stub */
5398 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
);
5399 static ssize_t
tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
5400 size_t count
, loff_t
*ppos
);
5401 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
);
5402 static ssize_t
tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
5403 struct pipe_inode_info
*pipe
, size_t len
, unsigned int flags
);
5405 static int snapshot_raw_open(struct inode
*inode
, struct file
*filp
)
5407 struct ftrace_buffer_info
*info
;
5410 ret
= tracing_buffers_open(inode
, filp
);
5414 info
= filp
->private_data
;
5416 if (info
->iter
.trace
->use_max_tr
) {
5417 tracing_buffers_release(inode
, filp
);
5421 info
->iter
.snapshot
= true;
5422 info
->iter
.trace_buffer
= &info
->iter
.tr
->max_buffer
;
5427 #endif /* CONFIG_TRACER_SNAPSHOT */
5430 static const struct file_operations tracing_thresh_fops
= {
5431 .open
= tracing_open_generic
,
5432 .read
= tracing_thresh_read
,
5433 .write
= tracing_thresh_write
,
5434 .llseek
= generic_file_llseek
,
5437 static const struct file_operations tracing_max_lat_fops
= {
5438 .open
= tracing_open_generic
,
5439 .read
= tracing_max_lat_read
,
5440 .write
= tracing_max_lat_write
,
5441 .llseek
= generic_file_llseek
,
5444 static const struct file_operations set_tracer_fops
= {
5445 .open
= tracing_open_generic
,
5446 .read
= tracing_set_trace_read
,
5447 .write
= tracing_set_trace_write
,
5448 .llseek
= generic_file_llseek
,
5451 static const struct file_operations tracing_pipe_fops
= {
5452 .open
= tracing_open_pipe
,
5453 .poll
= tracing_poll_pipe
,
5454 .read
= tracing_read_pipe
,
5455 .splice_read
= tracing_splice_read_pipe
,
5456 .release
= tracing_release_pipe
,
5457 .llseek
= no_llseek
,
5460 static const struct file_operations tracing_entries_fops
= {
5461 .open
= tracing_open_generic_tr
,
5462 .read
= tracing_entries_read
,
5463 .write
= tracing_entries_write
,
5464 .llseek
= generic_file_llseek
,
5465 .release
= tracing_release_generic_tr
,
5468 static const struct file_operations tracing_total_entries_fops
= {
5469 .open
= tracing_open_generic_tr
,
5470 .read
= tracing_total_entries_read
,
5471 .llseek
= generic_file_llseek
,
5472 .release
= tracing_release_generic_tr
,
5475 static const struct file_operations tracing_free_buffer_fops
= {
5476 .open
= tracing_open_generic_tr
,
5477 .write
= tracing_free_buffer_write
,
5478 .release
= tracing_free_buffer_release
,
5481 static const struct file_operations tracing_mark_fops
= {
5482 .open
= tracing_open_generic_tr
,
5483 .write
= tracing_mark_write
,
5484 .llseek
= generic_file_llseek
,
5485 .release
= tracing_release_generic_tr
,
5488 static const struct file_operations trace_clock_fops
= {
5489 .open
= tracing_clock_open
,
5491 .llseek
= seq_lseek
,
5492 .release
= tracing_single_release_tr
,
5493 .write
= tracing_clock_write
,
5496 #ifdef CONFIG_TRACER_SNAPSHOT
5497 static const struct file_operations snapshot_fops
= {
5498 .open
= tracing_snapshot_open
,
5500 .write
= tracing_snapshot_write
,
5501 .llseek
= tracing_lseek
,
5502 .release
= tracing_snapshot_release
,
5505 static const struct file_operations snapshot_raw_fops
= {
5506 .open
= snapshot_raw_open
,
5507 .read
= tracing_buffers_read
,
5508 .release
= tracing_buffers_release
,
5509 .splice_read
= tracing_buffers_splice_read
,
5510 .llseek
= no_llseek
,
5513 #endif /* CONFIG_TRACER_SNAPSHOT */
5515 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
5517 struct trace_array
*tr
= inode
->i_private
;
5518 struct ftrace_buffer_info
*info
;
5521 if (tracing_disabled
)
5524 if (trace_array_get(tr
) < 0)
5527 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
5529 trace_array_put(tr
);
5533 mutex_lock(&trace_types_lock
);
5536 info
->iter
.cpu_file
= tracing_get_cpu(inode
);
5537 info
->iter
.trace
= tr
->current_trace
;
5538 info
->iter
.trace_buffer
= &tr
->trace_buffer
;
5540 /* Force reading ring buffer for first read */
5541 info
->read
= (unsigned int)-1;
5543 filp
->private_data
= info
;
5545 tr
->current_trace
->ref
++;
5547 mutex_unlock(&trace_types_lock
);
5549 ret
= nonseekable_open(inode
, filp
);
5551 trace_array_put(tr
);
5557 tracing_buffers_poll(struct file
*filp
, poll_table
*poll_table
)
5559 struct ftrace_buffer_info
*info
= filp
->private_data
;
5560 struct trace_iterator
*iter
= &info
->iter
;
5562 return trace_poll(iter
, filp
, poll_table
);
5566 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
5567 size_t count
, loff_t
*ppos
)
5569 struct ftrace_buffer_info
*info
= filp
->private_data
;
5570 struct trace_iterator
*iter
= &info
->iter
;
5577 #ifdef CONFIG_TRACER_MAX_TRACE
5578 if (iter
->snapshot
&& iter
->tr
->current_trace
->use_max_tr
)
5583 info
->spare
= ring_buffer_alloc_read_page(iter
->trace_buffer
->buffer
,
5588 /* Do we have previous read data to read? */
5589 if (info
->read
< PAGE_SIZE
)
5593 trace_access_lock(iter
->cpu_file
);
5594 ret
= ring_buffer_read_page(iter
->trace_buffer
->buffer
,
5598 trace_access_unlock(iter
->cpu_file
);
5601 if (trace_empty(iter
)) {
5602 if ((filp
->f_flags
& O_NONBLOCK
))
5605 ret
= wait_on_pipe(iter
, false);
5616 size
= PAGE_SIZE
- info
->read
;
5620 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
5632 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
5634 struct ftrace_buffer_info
*info
= file
->private_data
;
5635 struct trace_iterator
*iter
= &info
->iter
;
5637 mutex_lock(&trace_types_lock
);
5639 iter
->tr
->current_trace
->ref
--;
5641 __trace_array_put(iter
->tr
);
5644 ring_buffer_free_read_page(iter
->trace_buffer
->buffer
, info
->spare
);
5647 mutex_unlock(&trace_types_lock
);
5653 struct ring_buffer
*buffer
;
5658 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
5659 struct pipe_buffer
*buf
)
5661 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
5666 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
5671 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
5672 struct pipe_buffer
*buf
)
5674 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
5679 /* Pipe buffer operations for a buffer. */
5680 static const struct pipe_buf_operations buffer_pipe_buf_ops
= {
5682 .confirm
= generic_pipe_buf_confirm
,
5683 .release
= buffer_pipe_buf_release
,
5684 .steal
= generic_pipe_buf_steal
,
5685 .get
= buffer_pipe_buf_get
,
5689 * Callback from splice_to_pipe(), if we need to release some pages
5690 * at the end of the spd in case we error'ed out in filling the pipe.
5692 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
5694 struct buffer_ref
*ref
=
5695 (struct buffer_ref
*)spd
->partial
[i
].private;
5700 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
5702 spd
->partial
[i
].private = 0;
5706 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
5707 struct pipe_inode_info
*pipe
, size_t len
,
5710 struct ftrace_buffer_info
*info
= file
->private_data
;
5711 struct trace_iterator
*iter
= &info
->iter
;
5712 struct partial_page partial_def
[PIPE_DEF_BUFFERS
];
5713 struct page
*pages_def
[PIPE_DEF_BUFFERS
];
5714 struct splice_pipe_desc spd
= {
5716 .partial
= partial_def
,
5717 .nr_pages_max
= PIPE_DEF_BUFFERS
,
5719 .ops
= &buffer_pipe_buf_ops
,
5720 .spd_release
= buffer_spd_release
,
5722 struct buffer_ref
*ref
;
5723 int entries
, size
, i
;
5726 #ifdef CONFIG_TRACER_MAX_TRACE
5727 if (iter
->snapshot
&& iter
->tr
->current_trace
->use_max_tr
)
5731 if (splice_grow_spd(pipe
, &spd
))
5734 if (*ppos
& (PAGE_SIZE
- 1))
5737 if (len
& (PAGE_SIZE
- 1)) {
5738 if (len
< PAGE_SIZE
)
5744 trace_access_lock(iter
->cpu_file
);
5745 entries
= ring_buffer_entries_cpu(iter
->trace_buffer
->buffer
, iter
->cpu_file
);
5747 for (i
= 0; i
< spd
.nr_pages_max
&& len
&& entries
; i
++, len
-= PAGE_SIZE
) {
5751 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
5758 ref
->buffer
= iter
->trace_buffer
->buffer
;
5759 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
, iter
->cpu_file
);
5766 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
5767 len
, iter
->cpu_file
, 1);
5769 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
5775 * zero out any left over data, this is going to
5778 size
= ring_buffer_page_len(ref
->page
);
5779 if (size
< PAGE_SIZE
)
5780 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
5782 page
= virt_to_page(ref
->page
);
5784 spd
.pages
[i
] = page
;
5785 spd
.partial
[i
].len
= PAGE_SIZE
;
5786 spd
.partial
[i
].offset
= 0;
5787 spd
.partial
[i
].private = (unsigned long)ref
;
5791 entries
= ring_buffer_entries_cpu(iter
->trace_buffer
->buffer
, iter
->cpu_file
);
5794 trace_access_unlock(iter
->cpu_file
);
5797 /* did we read anything? */
5798 if (!spd
.nr_pages
) {
5802 if ((file
->f_flags
& O_NONBLOCK
) || (flags
& SPLICE_F_NONBLOCK
))
5805 ret
= wait_on_pipe(iter
, true);
5812 ret
= splice_to_pipe(pipe
, &spd
);
5813 splice_shrink_spd(&spd
);
5818 static const struct file_operations tracing_buffers_fops
= {
5819 .open
= tracing_buffers_open
,
5820 .read
= tracing_buffers_read
,
5821 .poll
= tracing_buffers_poll
,
5822 .release
= tracing_buffers_release
,
5823 .splice_read
= tracing_buffers_splice_read
,
5824 .llseek
= no_llseek
,
5828 tracing_stats_read(struct file
*filp
, char __user
*ubuf
,
5829 size_t count
, loff_t
*ppos
)
5831 struct inode
*inode
= file_inode(filp
);
5832 struct trace_array
*tr
= inode
->i_private
;
5833 struct trace_buffer
*trace_buf
= &tr
->trace_buffer
;
5834 int cpu
= tracing_get_cpu(inode
);
5835 struct trace_seq
*s
;
5837 unsigned long long t
;
5838 unsigned long usec_rem
;
5840 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
5846 cnt
= ring_buffer_entries_cpu(trace_buf
->buffer
, cpu
);
5847 trace_seq_printf(s
, "entries: %ld\n", cnt
);
5849 cnt
= ring_buffer_overrun_cpu(trace_buf
->buffer
, cpu
);
5850 trace_seq_printf(s
, "overrun: %ld\n", cnt
);
5852 cnt
= ring_buffer_commit_overrun_cpu(trace_buf
->buffer
, cpu
);
5853 trace_seq_printf(s
, "commit overrun: %ld\n", cnt
);
5855 cnt
= ring_buffer_bytes_cpu(trace_buf
->buffer
, cpu
);
5856 trace_seq_printf(s
, "bytes: %ld\n", cnt
);
5858 if (trace_clocks
[tr
->clock_id
].in_ns
) {
5859 /* local or global for trace_clock */
5860 t
= ns2usecs(ring_buffer_oldest_event_ts(trace_buf
->buffer
, cpu
));
5861 usec_rem
= do_div(t
, USEC_PER_SEC
);
5862 trace_seq_printf(s
, "oldest event ts: %5llu.%06lu\n",
5865 t
= ns2usecs(ring_buffer_time_stamp(trace_buf
->buffer
, cpu
));
5866 usec_rem
= do_div(t
, USEC_PER_SEC
);
5867 trace_seq_printf(s
, "now ts: %5llu.%06lu\n", t
, usec_rem
);
5869 /* counter or tsc mode for trace_clock */
5870 trace_seq_printf(s
, "oldest event ts: %llu\n",
5871 ring_buffer_oldest_event_ts(trace_buf
->buffer
, cpu
));
5873 trace_seq_printf(s
, "now ts: %llu\n",
5874 ring_buffer_time_stamp(trace_buf
->buffer
, cpu
));
5877 cnt
= ring_buffer_dropped_events_cpu(trace_buf
->buffer
, cpu
);
5878 trace_seq_printf(s
, "dropped events: %ld\n", cnt
);
5880 cnt
= ring_buffer_read_events_cpu(trace_buf
->buffer
, cpu
);
5881 trace_seq_printf(s
, "read events: %ld\n", cnt
);
5883 count
= simple_read_from_buffer(ubuf
, count
, ppos
,
5884 s
->buffer
, trace_seq_used(s
));
5891 static const struct file_operations tracing_stats_fops
= {
5892 .open
= tracing_open_generic_tr
,
5893 .read
= tracing_stats_read
,
5894 .llseek
= generic_file_llseek
,
5895 .release
= tracing_release_generic_tr
,
5898 #ifdef CONFIG_DYNAMIC_FTRACE
5900 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
5906 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
5907 size_t cnt
, loff_t
*ppos
)
5909 static char ftrace_dyn_info_buffer
[1024];
5910 static DEFINE_MUTEX(dyn_info_mutex
);
5911 unsigned long *p
= filp
->private_data
;
5912 char *buf
= ftrace_dyn_info_buffer
;
5913 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
5916 mutex_lock(&dyn_info_mutex
);
5917 r
= sprintf(buf
, "%ld ", *p
);
5919 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
5922 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
5924 mutex_unlock(&dyn_info_mutex
);
5929 static const struct file_operations tracing_dyn_info_fops
= {
5930 .open
= tracing_open_generic
,
5931 .read
= tracing_read_dyn_info
,
5932 .llseek
= generic_file_llseek
,
5934 #endif /* CONFIG_DYNAMIC_FTRACE */
5936 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5938 ftrace_snapshot(unsigned long ip
, unsigned long parent_ip
, void **data
)
5944 ftrace_count_snapshot(unsigned long ip
, unsigned long parent_ip
, void **data
)
5946 unsigned long *count
= (long *)data
;
5958 ftrace_snapshot_print(struct seq_file
*m
, unsigned long ip
,
5959 struct ftrace_probe_ops
*ops
, void *data
)
5961 long count
= (long)data
;
5963 seq_printf(m
, "%ps:", (void *)ip
);
5965 seq_puts(m
, "snapshot");
5968 seq_puts(m
, ":unlimited\n");
5970 seq_printf(m
, ":count=%ld\n", count
);
5975 static struct ftrace_probe_ops snapshot_probe_ops
= {
5976 .func
= ftrace_snapshot
,
5977 .print
= ftrace_snapshot_print
,
5980 static struct ftrace_probe_ops snapshot_count_probe_ops
= {
5981 .func
= ftrace_count_snapshot
,
5982 .print
= ftrace_snapshot_print
,
5986 ftrace_trace_snapshot_callback(struct ftrace_hash
*hash
,
5987 char *glob
, char *cmd
, char *param
, int enable
)
5989 struct ftrace_probe_ops
*ops
;
5990 void *count
= (void *)-1;
5994 /* hash funcs only work with set_ftrace_filter */
5998 ops
= param
? &snapshot_count_probe_ops
: &snapshot_probe_ops
;
6000 if (glob
[0] == '!') {
6001 unregister_ftrace_function_probe_func(glob
+1, ops
);
6008 number
= strsep(¶m
, ":");
6010 if (!strlen(number
))
6014 * We use the callback data field (which is a pointer)
6017 ret
= kstrtoul(number
, 0, (unsigned long *)&count
);
6022 ret
= register_ftrace_function_probe(glob
, ops
, count
);
6025 alloc_snapshot(&global_trace
);
6027 return ret
< 0 ? ret
: 0;
6030 static struct ftrace_func_command ftrace_snapshot_cmd
= {
6032 .func
= ftrace_trace_snapshot_callback
,
6035 static __init
int register_snapshot_cmd(void)
6037 return register_ftrace_command(&ftrace_snapshot_cmd
);
6040 static inline __init
int register_snapshot_cmd(void) { return 0; }
6041 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
6043 static struct dentry
*tracing_get_dentry(struct trace_array
*tr
)
6045 if (WARN_ON(!tr
->dir
))
6046 return ERR_PTR(-ENODEV
);
6048 /* Top directory uses NULL as the parent */
6049 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
)
6052 /* All sub buffers have a descriptor */
6056 static struct dentry
*tracing_dentry_percpu(struct trace_array
*tr
, int cpu
)
6058 struct dentry
*d_tracer
;
6061 return tr
->percpu_dir
;
6063 d_tracer
= tracing_get_dentry(tr
);
6064 if (IS_ERR(d_tracer
))
6067 tr
->percpu_dir
= tracefs_create_dir("per_cpu", d_tracer
);
6069 WARN_ONCE(!tr
->percpu_dir
,
6070 "Could not create tracefs directory 'per_cpu/%d'\n", cpu
);
6072 return tr
->percpu_dir
;
6075 static struct dentry
*
6076 trace_create_cpu_file(const char *name
, umode_t mode
, struct dentry
*parent
,
6077 void *data
, long cpu
, const struct file_operations
*fops
)
6079 struct dentry
*ret
= trace_create_file(name
, mode
, parent
, data
, fops
);
6081 if (ret
) /* See tracing_get_cpu() */
6082 d_inode(ret
)->i_cdev
= (void *)(cpu
+ 1);
6087 tracing_init_tracefs_percpu(struct trace_array
*tr
, long cpu
)
6089 struct dentry
*d_percpu
= tracing_dentry_percpu(tr
, cpu
);
6090 struct dentry
*d_cpu
;
6091 char cpu_dir
[30]; /* 30 characters should be more than enough */
6096 snprintf(cpu_dir
, 30, "cpu%ld", cpu
);
6097 d_cpu
= tracefs_create_dir(cpu_dir
, d_percpu
);
6099 pr_warning("Could not create tracefs '%s' entry\n", cpu_dir
);
6103 /* per cpu trace_pipe */
6104 trace_create_cpu_file("trace_pipe", 0444, d_cpu
,
6105 tr
, cpu
, &tracing_pipe_fops
);
6108 trace_create_cpu_file("trace", 0644, d_cpu
,
6109 tr
, cpu
, &tracing_fops
);
6111 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu
,
6112 tr
, cpu
, &tracing_buffers_fops
);
6114 trace_create_cpu_file("stats", 0444, d_cpu
,
6115 tr
, cpu
, &tracing_stats_fops
);
6117 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu
,
6118 tr
, cpu
, &tracing_entries_fops
);
6120 #ifdef CONFIG_TRACER_SNAPSHOT
6121 trace_create_cpu_file("snapshot", 0644, d_cpu
,
6122 tr
, cpu
, &snapshot_fops
);
6124 trace_create_cpu_file("snapshot_raw", 0444, d_cpu
,
6125 tr
, cpu
, &snapshot_raw_fops
);
6129 #ifdef CONFIG_FTRACE_SELFTEST
6130 /* Let selftest have access to static functions in this file */
6131 #include "trace_selftest.c"
6134 struct trace_option_dentry
{
6135 struct tracer_opt
*opt
;
6136 struct tracer_flags
*flags
;
6137 struct trace_array
*tr
;
6138 struct dentry
*entry
;
6142 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
6145 struct trace_option_dentry
*topt
= filp
->private_data
;
6148 if (topt
->flags
->val
& topt
->opt
->bit
)
6153 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
6157 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
6160 struct trace_option_dentry
*topt
= filp
->private_data
;
6164 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
6168 if (val
!= 0 && val
!= 1)
6171 if (!!(topt
->flags
->val
& topt
->opt
->bit
) != val
) {
6172 mutex_lock(&trace_types_lock
);
6173 ret
= __set_tracer_option(topt
->tr
, topt
->flags
,
6175 mutex_unlock(&trace_types_lock
);
6186 static const struct file_operations trace_options_fops
= {
6187 .open
= tracing_open_generic
,
6188 .read
= trace_options_read
,
6189 .write
= trace_options_write
,
6190 .llseek
= generic_file_llseek
,
6194 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
6197 long index
= (long)filp
->private_data
;
6200 if (trace_flags
& (1 << index
))
6205 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
6209 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
6212 struct trace_array
*tr
= &global_trace
;
6213 long index
= (long)filp
->private_data
;
6217 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
6221 if (val
!= 0 && val
!= 1)
6224 mutex_lock(&trace_types_lock
);
6225 ret
= set_tracer_flag(tr
, 1 << index
, val
);
6226 mutex_unlock(&trace_types_lock
);
6236 static const struct file_operations trace_options_core_fops
= {
6237 .open
= tracing_open_generic
,
6238 .read
= trace_options_core_read
,
6239 .write
= trace_options_core_write
,
6240 .llseek
= generic_file_llseek
,
6243 struct dentry
*trace_create_file(const char *name
,
6245 struct dentry
*parent
,
6247 const struct file_operations
*fops
)
6251 ret
= tracefs_create_file(name
, mode
, parent
, data
, fops
);
6253 pr_warning("Could not create tracefs '%s' entry\n", name
);
6259 static struct dentry
*trace_options_init_dentry(struct trace_array
*tr
)
6261 struct dentry
*d_tracer
;
6266 d_tracer
= tracing_get_dentry(tr
);
6267 if (IS_ERR(d_tracer
))
6270 tr
->options
= tracefs_create_dir("options", d_tracer
);
6272 pr_warning("Could not create tracefs directory 'options'\n");
6280 create_trace_option_file(struct trace_array
*tr
,
6281 struct trace_option_dentry
*topt
,
6282 struct tracer_flags
*flags
,
6283 struct tracer_opt
*opt
)
6285 struct dentry
*t_options
;
6287 t_options
= trace_options_init_dentry(tr
);
6291 topt
->flags
= flags
;
6295 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
6296 &trace_options_fops
);
6300 static struct trace_option_dentry
*
6301 create_trace_option_files(struct trace_array
*tr
, struct tracer
*tracer
)
6303 struct trace_option_dentry
*topts
;
6304 struct tracer_flags
*flags
;
6305 struct tracer_opt
*opts
;
6311 flags
= tracer
->flags
;
6313 if (!flags
|| !flags
->opts
)
6318 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
6321 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
6325 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
6326 create_trace_option_file(tr
, &topts
[cnt
], flags
,
6333 destroy_trace_option_files(struct trace_option_dentry
*topts
)
6340 for (cnt
= 0; topts
[cnt
].opt
; cnt
++)
6341 tracefs_remove(topts
[cnt
].entry
);
6346 static struct dentry
*
6347 create_trace_option_core_file(struct trace_array
*tr
,
6348 const char *option
, long index
)
6350 struct dentry
*t_options
;
6352 t_options
= trace_options_init_dentry(tr
);
6356 return trace_create_file(option
, 0644, t_options
, (void *)index
,
6357 &trace_options_core_fops
);
6360 static __init
void create_trace_options_dir(struct trace_array
*tr
)
6362 struct dentry
*t_options
;
6365 t_options
= trace_options_init_dentry(tr
);
6369 for (i
= 0; trace_options
[i
]; i
++)
6370 create_trace_option_core_file(tr
, trace_options
[i
], i
);
6374 rb_simple_read(struct file
*filp
, char __user
*ubuf
,
6375 size_t cnt
, loff_t
*ppos
)
6377 struct trace_array
*tr
= filp
->private_data
;
6381 r
= tracer_tracing_is_on(tr
);
6382 r
= sprintf(buf
, "%d\n", r
);
6384 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
6388 rb_simple_write(struct file
*filp
, const char __user
*ubuf
,
6389 size_t cnt
, loff_t
*ppos
)
6391 struct trace_array
*tr
= filp
->private_data
;
6392 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
6396 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
6401 mutex_lock(&trace_types_lock
);
6403 tracer_tracing_on(tr
);
6404 if (tr
->current_trace
->start
)
6405 tr
->current_trace
->start(tr
);
6407 tracer_tracing_off(tr
);
6408 if (tr
->current_trace
->stop
)
6409 tr
->current_trace
->stop(tr
);
6411 mutex_unlock(&trace_types_lock
);
6419 static const struct file_operations rb_simple_fops
= {
6420 .open
= tracing_open_generic_tr
,
6421 .read
= rb_simple_read
,
6422 .write
= rb_simple_write
,
6423 .release
= tracing_release_generic_tr
,
6424 .llseek
= default_llseek
,
6427 struct dentry
*trace_instance_dir
;
6430 init_tracer_tracefs(struct trace_array
*tr
, struct dentry
*d_tracer
);
6433 allocate_trace_buffer(struct trace_array
*tr
, struct trace_buffer
*buf
, int size
)
6435 enum ring_buffer_flags rb_flags
;
6437 rb_flags
= trace_flags
& TRACE_ITER_OVERWRITE
? RB_FL_OVERWRITE
: 0;
6441 buf
->buffer
= ring_buffer_alloc(size
, rb_flags
);
6445 buf
->data
= alloc_percpu(struct trace_array_cpu
);
6447 ring_buffer_free(buf
->buffer
);
6451 /* Allocate the first page for all buffers */
6452 set_buffer_entries(&tr
->trace_buffer
,
6453 ring_buffer_size(tr
->trace_buffer
.buffer
, 0));
6458 static int allocate_trace_buffers(struct trace_array
*tr
, int size
)
6462 ret
= allocate_trace_buffer(tr
, &tr
->trace_buffer
, size
);
6466 #ifdef CONFIG_TRACER_MAX_TRACE
6467 ret
= allocate_trace_buffer(tr
, &tr
->max_buffer
,
6468 allocate_snapshot
? size
: 1);
6470 ring_buffer_free(tr
->trace_buffer
.buffer
);
6471 free_percpu(tr
->trace_buffer
.data
);
6474 tr
->allocated_snapshot
= allocate_snapshot
;
6477 * Only the top level trace array gets its snapshot allocated
6478 * from the kernel command line.
6480 allocate_snapshot
= false;
6485 static void free_trace_buffer(struct trace_buffer
*buf
)
6488 ring_buffer_free(buf
->buffer
);
6490 free_percpu(buf
->data
);
6495 static void free_trace_buffers(struct trace_array
*tr
)
6500 free_trace_buffer(&tr
->trace_buffer
);
6502 #ifdef CONFIG_TRACER_MAX_TRACE
6503 free_trace_buffer(&tr
->max_buffer
);
6507 static int instance_mkdir(const char *name
)
6509 struct trace_array
*tr
;
6512 mutex_lock(&trace_types_lock
);
6515 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
6516 if (tr
->name
&& strcmp(tr
->name
, name
) == 0)
6521 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
6525 tr
->name
= kstrdup(name
, GFP_KERNEL
);
6529 if (!alloc_cpumask_var(&tr
->tracing_cpumask
, GFP_KERNEL
))
6532 cpumask_copy(tr
->tracing_cpumask
, cpu_all_mask
);
6534 raw_spin_lock_init(&tr
->start_lock
);
6536 tr
->max_lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
6538 tr
->current_trace
= &nop_trace
;
6540 INIT_LIST_HEAD(&tr
->systems
);
6541 INIT_LIST_HEAD(&tr
->events
);
6543 if (allocate_trace_buffers(tr
, trace_buf_size
) < 0)
6546 tr
->dir
= tracefs_create_dir(name
, trace_instance_dir
);
6550 ret
= event_trace_add_tracer(tr
->dir
, tr
);
6552 tracefs_remove_recursive(tr
->dir
);
6556 init_tracer_tracefs(tr
, tr
->dir
);
6558 list_add(&tr
->list
, &ftrace_trace_arrays
);
6560 mutex_unlock(&trace_types_lock
);
6565 free_trace_buffers(tr
);
6566 free_cpumask_var(tr
->tracing_cpumask
);
6571 mutex_unlock(&trace_types_lock
);
6577 static int instance_rmdir(const char *name
)
6579 struct trace_array
*tr
;
6583 mutex_lock(&trace_types_lock
);
6586 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
6587 if (tr
->name
&& strcmp(tr
->name
, name
) == 0) {
6596 if (tr
->ref
|| (tr
->current_trace
&& tr
->current_trace
->ref
))
6599 list_del(&tr
->list
);
6601 tracing_set_nop(tr
);
6602 event_trace_del_tracer(tr
);
6603 ftrace_destroy_function_files(tr
);
6604 debugfs_remove_recursive(tr
->dir
);
6605 free_trace_buffers(tr
);
6613 mutex_unlock(&trace_types_lock
);
6618 static __init
void create_trace_instances(struct dentry
*d_tracer
)
6620 trace_instance_dir
= tracefs_create_instance_dir("instances", d_tracer
,
6623 if (WARN_ON(!trace_instance_dir
))
6628 init_tracer_tracefs(struct trace_array
*tr
, struct dentry
*d_tracer
)
6632 trace_create_file("available_tracers", 0444, d_tracer
,
6633 tr
, &show_traces_fops
);
6635 trace_create_file("current_tracer", 0644, d_tracer
,
6636 tr
, &set_tracer_fops
);
6638 trace_create_file("tracing_cpumask", 0644, d_tracer
,
6639 tr
, &tracing_cpumask_fops
);
6641 trace_create_file("trace_options", 0644, d_tracer
,
6642 tr
, &tracing_iter_fops
);
6644 trace_create_file("trace", 0644, d_tracer
,
6647 trace_create_file("trace_pipe", 0444, d_tracer
,
6648 tr
, &tracing_pipe_fops
);
6650 trace_create_file("buffer_size_kb", 0644, d_tracer
,
6651 tr
, &tracing_entries_fops
);
6653 trace_create_file("buffer_total_size_kb", 0444, d_tracer
,
6654 tr
, &tracing_total_entries_fops
);
6656 trace_create_file("free_buffer", 0200, d_tracer
,
6657 tr
, &tracing_free_buffer_fops
);
6659 trace_create_file("trace_marker", 0220, d_tracer
,
6660 tr
, &tracing_mark_fops
);
6662 trace_create_file("trace_clock", 0644, d_tracer
, tr
,
6665 trace_create_file("tracing_on", 0644, d_tracer
,
6666 tr
, &rb_simple_fops
);
6668 #ifdef CONFIG_TRACER_MAX_TRACE
6669 trace_create_file("tracing_max_latency", 0644, d_tracer
,
6670 &tr
->max_latency
, &tracing_max_lat_fops
);
6673 if (ftrace_create_function_files(tr
, d_tracer
))
6674 WARN(1, "Could not allocate function filter files");
6676 #ifdef CONFIG_TRACER_SNAPSHOT
6677 trace_create_file("snapshot", 0644, d_tracer
,
6678 tr
, &snapshot_fops
);
6681 for_each_tracing_cpu(cpu
)
6682 tracing_init_tracefs_percpu(tr
, cpu
);
6686 static struct vfsmount
*trace_automount(void *ingore
)
6688 struct vfsmount
*mnt
;
6689 struct file_system_type
*type
;
6692 * To maintain backward compatibility for tools that mount
6693 * debugfs to get to the tracing facility, tracefs is automatically
6694 * mounted to the debugfs/tracing directory.
6696 type
= get_fs_type("tracefs");
6699 mnt
= vfs_kern_mount(type
, 0, "tracefs", NULL
);
6700 put_filesystem(type
);
6709 * tracing_init_dentry - initialize top level trace array
6711 * This is called when creating files or directories in the tracing
6712 * directory. It is called via fs_initcall() by any of the boot up code
6713 * and expects to return the dentry of the top level tracing directory.
6715 struct dentry
*tracing_init_dentry(void)
6717 struct trace_array
*tr
= &global_trace
;
6719 /* The top level trace array uses NULL as parent */
6723 if (WARN_ON(!debugfs_initialized()))
6724 return ERR_PTR(-ENODEV
);
6727 * As there may still be users that expect the tracing
6728 * files to exist in debugfs/tracing, we must automount
6729 * the tracefs file system there, so older tools still
6730 * work with the newer kerenl.
6732 tr
->dir
= debugfs_create_automount("tracing", NULL
,
6733 trace_automount
, NULL
);
6735 pr_warn_once("Could not create debugfs directory 'tracing'\n");
6736 return ERR_PTR(-ENOMEM
);
6742 extern struct trace_enum_map
*__start_ftrace_enum_maps
[];
6743 extern struct trace_enum_map
*__stop_ftrace_enum_maps
[];
6745 static void __init
trace_enum_init(void)
6749 len
= __stop_ftrace_enum_maps
- __start_ftrace_enum_maps
;
6750 trace_insert_enum_map(NULL
, __start_ftrace_enum_maps
, len
);
6753 #ifdef CONFIG_MODULES
6754 static void trace_module_add_enums(struct module
*mod
)
6756 if (!mod
->num_trace_enums
)
6760 * Modules with bad taint do not have events created, do
6761 * not bother with enums either.
6763 if (trace_module_has_bad_taint(mod
))
6766 trace_insert_enum_map(mod
, mod
->trace_enums
, mod
->num_trace_enums
);
6769 #ifdef CONFIG_TRACE_ENUM_MAP_FILE
6770 static void trace_module_remove_enums(struct module
*mod
)
6772 union trace_enum_map_item
*map
;
6773 union trace_enum_map_item
**last
= &trace_enum_maps
;
6775 if (!mod
->num_trace_enums
)
6778 mutex_lock(&trace_enum_mutex
);
6780 map
= trace_enum_maps
;
6783 if (map
->head
.mod
== mod
)
6785 map
= trace_enum_jmp_to_tail(map
);
6786 last
= &map
->tail
.next
;
6787 map
= map
->tail
.next
;
6792 *last
= trace_enum_jmp_to_tail(map
)->tail
.next
;
6795 mutex_unlock(&trace_enum_mutex
);
6798 static inline void trace_module_remove_enums(struct module
*mod
) { }
6799 #endif /* CONFIG_TRACE_ENUM_MAP_FILE */
6801 static int trace_module_notify(struct notifier_block
*self
,
6802 unsigned long val
, void *data
)
6804 struct module
*mod
= data
;
6807 case MODULE_STATE_COMING
:
6808 trace_module_add_enums(mod
);
6810 case MODULE_STATE_GOING
:
6811 trace_module_remove_enums(mod
);
6818 static struct notifier_block trace_module_nb
= {
6819 .notifier_call
= trace_module_notify
,
6822 #endif /* CONFIG_MODULES */
6824 static __init
int tracer_init_tracefs(void)
6826 struct dentry
*d_tracer
;
6828 trace_access_lock_init();
6830 d_tracer
= tracing_init_dentry();
6831 if (IS_ERR(d_tracer
))
6834 init_tracer_tracefs(&global_trace
, d_tracer
);
6836 trace_create_file("tracing_thresh", 0644, d_tracer
,
6837 &global_trace
, &tracing_thresh_fops
);
6839 trace_create_file("README", 0444, d_tracer
,
6840 NULL
, &tracing_readme_fops
);
6842 trace_create_file("saved_cmdlines", 0444, d_tracer
,
6843 NULL
, &tracing_saved_cmdlines_fops
);
6845 trace_create_file("saved_cmdlines_size", 0644, d_tracer
,
6846 NULL
, &tracing_saved_cmdlines_size_fops
);
6850 trace_create_enum_file(d_tracer
);
6852 #ifdef CONFIG_MODULES
6853 register_module_notifier(&trace_module_nb
);
6856 #ifdef CONFIG_DYNAMIC_FTRACE
6857 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
6858 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
6861 create_trace_instances(d_tracer
);
6863 create_trace_options_dir(&global_trace
);
6865 /* If the tracer was started via cmdline, create options for it here */
6866 if (global_trace
.current_trace
!= &nop_trace
)
6867 update_tracer_options(&global_trace
, global_trace
.current_trace
);
6872 static int trace_panic_handler(struct notifier_block
*this,
6873 unsigned long event
, void *unused
)
6875 if (ftrace_dump_on_oops
)
6876 ftrace_dump(ftrace_dump_on_oops
);
6880 static struct notifier_block trace_panic_notifier
= {
6881 .notifier_call
= trace_panic_handler
,
6883 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
6886 static int trace_die_handler(struct notifier_block
*self
,
6892 if (ftrace_dump_on_oops
)
6893 ftrace_dump(ftrace_dump_on_oops
);
6901 static struct notifier_block trace_die_notifier
= {
6902 .notifier_call
= trace_die_handler
,
6907 * printk is set to max of 1024, we really don't need it that big.
6908 * Nothing should be printing 1000 characters anyway.
6910 #define TRACE_MAX_PRINT 1000
6913 * Define here KERN_TRACE so that we have one place to modify
6914 * it if we decide to change what log level the ftrace dump
6917 #define KERN_TRACE KERN_EMERG
6920 trace_printk_seq(struct trace_seq
*s
)
6922 /* Probably should print a warning here. */
6923 if (s
->seq
.len
>= TRACE_MAX_PRINT
)
6924 s
->seq
.len
= TRACE_MAX_PRINT
;
6927 * More paranoid code. Although the buffer size is set to
6928 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
6929 * an extra layer of protection.
6931 if (WARN_ON_ONCE(s
->seq
.len
>= s
->seq
.size
))
6932 s
->seq
.len
= s
->seq
.size
- 1;
6934 /* should be zero ended, but we are paranoid. */
6935 s
->buffer
[s
->seq
.len
] = 0;
6937 printk(KERN_TRACE
"%s", s
->buffer
);
6942 void trace_init_global_iter(struct trace_iterator
*iter
)
6944 iter
->tr
= &global_trace
;
6945 iter
->trace
= iter
->tr
->current_trace
;
6946 iter
->cpu_file
= RING_BUFFER_ALL_CPUS
;
6947 iter
->trace_buffer
= &global_trace
.trace_buffer
;
6949 if (iter
->trace
&& iter
->trace
->open
)
6950 iter
->trace
->open(iter
);
6952 /* Annotate start of buffers if we had overruns */
6953 if (ring_buffer_overruns(iter
->trace_buffer
->buffer
))
6954 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
6956 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
6957 if (trace_clocks
[iter
->tr
->clock_id
].in_ns
)
6958 iter
->iter_flags
|= TRACE_FILE_TIME_IN_NS
;
6961 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode
)
6963 /* use static because iter can be a bit big for the stack */
6964 static struct trace_iterator iter
;
6965 static atomic_t dump_running
;
6966 unsigned int old_userobj
;
6967 unsigned long flags
;
6970 /* Only allow one dump user at a time. */
6971 if (atomic_inc_return(&dump_running
) != 1) {
6972 atomic_dec(&dump_running
);
6977 * Always turn off tracing when we dump.
6978 * We don't need to show trace output of what happens
6979 * between multiple crashes.
6981 * If the user does a sysrq-z, then they can re-enable
6982 * tracing with echo 1 > tracing_on.
6986 local_irq_save(flags
);
6988 /* Simulate the iterator */
6989 trace_init_global_iter(&iter
);
6991 for_each_tracing_cpu(cpu
) {
6992 atomic_inc(&per_cpu_ptr(iter
.tr
->trace_buffer
.data
, cpu
)->disabled
);
6995 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
6997 /* don't look at user memory in panic mode */
6998 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
7000 switch (oops_dump_mode
) {
7002 iter
.cpu_file
= RING_BUFFER_ALL_CPUS
;
7005 iter
.cpu_file
= raw_smp_processor_id();
7010 printk(KERN_TRACE
"Bad dumping mode, switching to all CPUs dump\n");
7011 iter
.cpu_file
= RING_BUFFER_ALL_CPUS
;
7014 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
7016 /* Did function tracer already get disabled? */
7017 if (ftrace_is_dead()) {
7018 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
7019 printk("# MAY BE MISSING FUNCTION EVENTS\n");
7023 * We need to stop all tracing on all CPUS to read the
7024 * the next buffer. This is a bit expensive, but is
7025 * not done often. We fill all what we can read,
7026 * and then release the locks again.
7029 while (!trace_empty(&iter
)) {
7032 printk(KERN_TRACE
"---------------------------------\n");
7036 /* reset all but tr, trace, and overruns */
7037 memset(&iter
.seq
, 0,
7038 sizeof(struct trace_iterator
) -
7039 offsetof(struct trace_iterator
, seq
));
7040 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
7043 if (trace_find_next_entry_inc(&iter
) != NULL
) {
7046 ret
= print_trace_line(&iter
);
7047 if (ret
!= TRACE_TYPE_NO_CONSUME
)
7048 trace_consume(&iter
);
7050 touch_nmi_watchdog();
7052 trace_printk_seq(&iter
.seq
);
7056 printk(KERN_TRACE
" (ftrace buffer empty)\n");
7058 printk(KERN_TRACE
"---------------------------------\n");
7061 trace_flags
|= old_userobj
;
7063 for_each_tracing_cpu(cpu
) {
7064 atomic_dec(&per_cpu_ptr(iter
.trace_buffer
->data
, cpu
)->disabled
);
7066 atomic_dec(&dump_running
);
7067 local_irq_restore(flags
);
7069 EXPORT_SYMBOL_GPL(ftrace_dump
);
7071 __init
static int tracer_alloc_buffers(void)
7076 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
7079 if (!alloc_cpumask_var(&global_trace
.tracing_cpumask
, GFP_KERNEL
))
7080 goto out_free_buffer_mask
;
7082 /* Only allocate trace_printk buffers if a trace_printk exists */
7083 if (__stop___trace_bprintk_fmt
!= __start___trace_bprintk_fmt
)
7084 /* Must be called before global_trace.buffer is allocated */
7085 trace_printk_init_buffers();
7087 /* To save memory, keep the ring buffer size to its minimum */
7088 if (ring_buffer_expanded
)
7089 ring_buf_size
= trace_buf_size
;
7093 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
7094 cpumask_copy(global_trace
.tracing_cpumask
, cpu_all_mask
);
7096 raw_spin_lock_init(&global_trace
.start_lock
);
7098 /* Used for event triggers */
7099 temp_buffer
= ring_buffer_alloc(PAGE_SIZE
, RB_FL_OVERWRITE
);
7101 goto out_free_cpumask
;
7103 if (trace_create_savedcmd() < 0)
7104 goto out_free_temp_buffer
;
7106 /* TODO: make the number of buffers hot pluggable with CPUS */
7107 if (allocate_trace_buffers(&global_trace
, ring_buf_size
) < 0) {
7108 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
7110 goto out_free_savedcmd
;
7113 if (global_trace
.buffer_disabled
)
7116 if (trace_boot_clock
) {
7117 ret
= tracing_set_clock(&global_trace
, trace_boot_clock
);
7119 pr_warning("Trace clock %s not defined, going back to default\n",
7124 * register_tracer() might reference current_trace, so it
7125 * needs to be set before we register anything. This is
7126 * just a bootstrap of current_trace anyway.
7128 global_trace
.current_trace
= &nop_trace
;
7130 global_trace
.max_lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
7132 ftrace_init_global_array_ops(&global_trace
);
7134 register_tracer(&nop_trace
);
7136 /* All seems OK, enable tracing */
7137 tracing_disabled
= 0;
7139 atomic_notifier_chain_register(&panic_notifier_list
,
7140 &trace_panic_notifier
);
7142 register_die_notifier(&trace_die_notifier
);
7144 global_trace
.flags
= TRACE_ARRAY_FL_GLOBAL
;
7146 INIT_LIST_HEAD(&global_trace
.systems
);
7147 INIT_LIST_HEAD(&global_trace
.events
);
7148 list_add(&global_trace
.list
, &ftrace_trace_arrays
);
7150 while (trace_boot_options
) {
7153 option
= strsep(&trace_boot_options
, ",");
7154 trace_set_options(&global_trace
, option
);
7157 register_snapshot_cmd();
7162 free_saved_cmdlines_buffer(savedcmd
);
7163 out_free_temp_buffer
:
7164 ring_buffer_free(temp_buffer
);
7166 free_cpumask_var(global_trace
.tracing_cpumask
);
7167 out_free_buffer_mask
:
7168 free_cpumask_var(tracing_buffer_mask
);
7173 void __init
trace_init(void)
7175 if (tracepoint_printk
) {
7176 tracepoint_print_iter
=
7177 kmalloc(sizeof(*tracepoint_print_iter
), GFP_KERNEL
);
7178 if (WARN_ON(!tracepoint_print_iter
))
7179 tracepoint_printk
= 0;
7181 tracer_alloc_buffers();
7185 __init
static int clear_boot_tracer(void)
7188 * The default tracer at boot buffer is an init section.
7189 * This function is called in lateinit. If we did not
7190 * find the boot tracer, then clear it out, to prevent
7191 * later registration from accessing the buffer that is
7192 * about to be freed.
7194 if (!default_bootup_tracer
)
7197 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
7198 default_bootup_tracer
);
7199 default_bootup_tracer
= NULL
;
7204 fs_initcall(tracer_init_tracefs
);
7205 late_initcall(clear_boot_tracer
);