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 trace_event_file
*file
, void *rec
,
301 struct ring_buffer
*buffer
,
302 struct ring_buffer_event
*event
)
304 if (unlikely(file
->flags
& EVENT_FILE_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 trace_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 },
879 { ktime_get_raw_fast_ns
, "mono_raw", 1 },
884 * trace_parser_get_init - gets the buffer for trace parser
886 int trace_parser_get_init(struct trace_parser
*parser
, int size
)
888 memset(parser
, 0, sizeof(*parser
));
890 parser
->buffer
= kmalloc(size
, GFP_KERNEL
);
899 * trace_parser_put - frees the buffer for trace parser
901 void trace_parser_put(struct trace_parser
*parser
)
903 kfree(parser
->buffer
);
907 * trace_get_user - reads the user input string separated by space
908 * (matched by isspace(ch))
910 * For each string found the 'struct trace_parser' is updated,
911 * and the function returns.
913 * Returns number of bytes read.
915 * See kernel/trace/trace.h for 'struct trace_parser' details.
917 int trace_get_user(struct trace_parser
*parser
, const char __user
*ubuf
,
918 size_t cnt
, loff_t
*ppos
)
925 trace_parser_clear(parser
);
927 ret
= get_user(ch
, ubuf
++);
935 * The parser is not finished with the last write,
936 * continue reading the user input without skipping spaces.
939 /* skip white space */
940 while (cnt
&& isspace(ch
)) {
941 ret
= get_user(ch
, ubuf
++);
948 /* only spaces were written */
958 /* read the non-space input */
959 while (cnt
&& !isspace(ch
)) {
960 if (parser
->idx
< parser
->size
- 1)
961 parser
->buffer
[parser
->idx
++] = ch
;
966 ret
= get_user(ch
, ubuf
++);
973 /* We either got finished input or we have to wait for another call. */
975 parser
->buffer
[parser
->idx
] = 0;
976 parser
->cont
= false;
977 } else if (parser
->idx
< parser
->size
- 1) {
979 parser
->buffer
[parser
->idx
++] = ch
;
992 /* TODO add a seq_buf_to_buffer() */
993 static ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
997 if (trace_seq_used(s
) <= s
->seq
.readpos
)
1000 len
= trace_seq_used(s
) - s
->seq
.readpos
;
1003 memcpy(buf
, s
->buffer
+ s
->seq
.readpos
, cnt
);
1005 s
->seq
.readpos
+= cnt
;
1009 unsigned long __read_mostly tracing_thresh
;
1011 #ifdef CONFIG_TRACER_MAX_TRACE
1013 * Copy the new maximum trace into the separate maximum-trace
1014 * structure. (this way the maximum trace is permanently saved,
1015 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
1018 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
1020 struct trace_buffer
*trace_buf
= &tr
->trace_buffer
;
1021 struct trace_buffer
*max_buf
= &tr
->max_buffer
;
1022 struct trace_array_cpu
*data
= per_cpu_ptr(trace_buf
->data
, cpu
);
1023 struct trace_array_cpu
*max_data
= per_cpu_ptr(max_buf
->data
, cpu
);
1026 max_buf
->time_start
= data
->preempt_timestamp
;
1028 max_data
->saved_latency
= tr
->max_latency
;
1029 max_data
->critical_start
= data
->critical_start
;
1030 max_data
->critical_end
= data
->critical_end
;
1032 memcpy(max_data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
1033 max_data
->pid
= tsk
->pid
;
1035 * If tsk == current, then use current_uid(), as that does not use
1036 * RCU. The irq tracer can be called out of RCU scope.
1039 max_data
->uid
= current_uid();
1041 max_data
->uid
= task_uid(tsk
);
1043 max_data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
1044 max_data
->policy
= tsk
->policy
;
1045 max_data
->rt_priority
= tsk
->rt_priority
;
1047 /* record this tasks comm */
1048 tracing_record_cmdline(tsk
);
1052 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1054 * @tsk: the task with the latency
1055 * @cpu: The cpu that initiated the trace.
1057 * Flip the buffers between the @tr and the max_tr and record information
1058 * about which task was the cause of this latency.
1061 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
1063 struct ring_buffer
*buf
;
1068 WARN_ON_ONCE(!irqs_disabled());
1070 if (!tr
->allocated_snapshot
) {
1071 /* Only the nop tracer should hit this when disabling */
1072 WARN_ON_ONCE(tr
->current_trace
!= &nop_trace
);
1076 arch_spin_lock(&tr
->max_lock
);
1078 buf
= tr
->trace_buffer
.buffer
;
1079 tr
->trace_buffer
.buffer
= tr
->max_buffer
.buffer
;
1080 tr
->max_buffer
.buffer
= buf
;
1082 __update_max_tr(tr
, tsk
, cpu
);
1083 arch_spin_unlock(&tr
->max_lock
);
1087 * update_max_tr_single - only copy one trace over, and reset the rest
1089 * @tsk - task with the latency
1090 * @cpu - the cpu of the buffer to copy.
1092 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
1095 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
1102 WARN_ON_ONCE(!irqs_disabled());
1103 if (!tr
->allocated_snapshot
) {
1104 /* Only the nop tracer should hit this when disabling */
1105 WARN_ON_ONCE(tr
->current_trace
!= &nop_trace
);
1109 arch_spin_lock(&tr
->max_lock
);
1111 ret
= ring_buffer_swap_cpu(tr
->max_buffer
.buffer
, tr
->trace_buffer
.buffer
, cpu
);
1113 if (ret
== -EBUSY
) {
1115 * We failed to swap the buffer due to a commit taking
1116 * place on this CPU. We fail to record, but we reset
1117 * the max trace buffer (no one writes directly to it)
1118 * and flag that it failed.
1120 trace_array_printk_buf(tr
->max_buffer
.buffer
, _THIS_IP_
,
1121 "Failed to swap buffers due to commit in progress\n");
1124 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
&& ret
!= -EBUSY
);
1126 __update_max_tr(tr
, tsk
, cpu
);
1127 arch_spin_unlock(&tr
->max_lock
);
1129 #endif /* CONFIG_TRACER_MAX_TRACE */
1131 static int wait_on_pipe(struct trace_iterator
*iter
, bool full
)
1133 /* Iterators are static, they should be filled or empty */
1134 if (trace_buffer_iter(iter
, iter
->cpu_file
))
1137 return ring_buffer_wait(iter
->trace_buffer
->buffer
, iter
->cpu_file
,
1141 #ifdef CONFIG_FTRACE_STARTUP_TEST
1142 static int run_tracer_selftest(struct tracer
*type
)
1144 struct trace_array
*tr
= &global_trace
;
1145 struct tracer
*saved_tracer
= tr
->current_trace
;
1148 if (!type
->selftest
|| tracing_selftest_disabled
)
1152 * Run a selftest on this tracer.
1153 * Here we reset the trace buffer, and set the current
1154 * tracer to be this tracer. The tracer can then run some
1155 * internal tracing to verify that everything is in order.
1156 * If we fail, we do not register this tracer.
1158 tracing_reset_online_cpus(&tr
->trace_buffer
);
1160 tr
->current_trace
= type
;
1162 #ifdef CONFIG_TRACER_MAX_TRACE
1163 if (type
->use_max_tr
) {
1164 /* If we expanded the buffers, make sure the max is expanded too */
1165 if (ring_buffer_expanded
)
1166 ring_buffer_resize(tr
->max_buffer
.buffer
, trace_buf_size
,
1167 RING_BUFFER_ALL_CPUS
);
1168 tr
->allocated_snapshot
= true;
1172 /* the test is responsible for initializing and enabling */
1173 pr_info("Testing tracer %s: ", type
->name
);
1174 ret
= type
->selftest(type
, tr
);
1175 /* the test is responsible for resetting too */
1176 tr
->current_trace
= saved_tracer
;
1178 printk(KERN_CONT
"FAILED!\n");
1179 /* Add the warning after printing 'FAILED' */
1183 /* Only reset on passing, to avoid touching corrupted buffers */
1184 tracing_reset_online_cpus(&tr
->trace_buffer
);
1186 #ifdef CONFIG_TRACER_MAX_TRACE
1187 if (type
->use_max_tr
) {
1188 tr
->allocated_snapshot
= false;
1190 /* Shrink the max buffer again */
1191 if (ring_buffer_expanded
)
1192 ring_buffer_resize(tr
->max_buffer
.buffer
, 1,
1193 RING_BUFFER_ALL_CPUS
);
1197 printk(KERN_CONT
"PASSED\n");
1201 static inline int run_tracer_selftest(struct tracer
*type
)
1205 #endif /* CONFIG_FTRACE_STARTUP_TEST */
1208 * register_tracer - register a tracer with the ftrace system.
1209 * @type - the plugin for the tracer
1211 * Register a new plugin tracer.
1213 int register_tracer(struct tracer
*type
)
1219 pr_info("Tracer must have a name\n");
1223 if (strlen(type
->name
) >= MAX_TRACER_SIZE
) {
1224 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE
);
1228 mutex_lock(&trace_types_lock
);
1230 tracing_selftest_running
= true;
1232 for (t
= trace_types
; t
; t
= t
->next
) {
1233 if (strcmp(type
->name
, t
->name
) == 0) {
1235 pr_info("Tracer %s already registered\n",
1242 if (!type
->set_flag
)
1243 type
->set_flag
= &dummy_set_flag
;
1245 type
->flags
= &dummy_tracer_flags
;
1247 if (!type
->flags
->opts
)
1248 type
->flags
->opts
= dummy_tracer_opt
;
1250 ret
= run_tracer_selftest(type
);
1254 type
->next
= trace_types
;
1258 tracing_selftest_running
= false;
1259 mutex_unlock(&trace_types_lock
);
1261 if (ret
|| !default_bootup_tracer
)
1264 if (strncmp(default_bootup_tracer
, type
->name
, MAX_TRACER_SIZE
))
1267 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
1268 /* Do we want this tracer to start on bootup? */
1269 tracing_set_tracer(&global_trace
, type
->name
);
1270 default_bootup_tracer
= NULL
;
1271 /* disable other selftests, since this will break it. */
1272 tracing_selftest_disabled
= true;
1273 #ifdef CONFIG_FTRACE_STARTUP_TEST
1274 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
1282 void tracing_reset(struct trace_buffer
*buf
, int cpu
)
1284 struct ring_buffer
*buffer
= buf
->buffer
;
1289 ring_buffer_record_disable(buffer
);
1291 /* Make sure all commits have finished */
1292 synchronize_sched();
1293 ring_buffer_reset_cpu(buffer
, cpu
);
1295 ring_buffer_record_enable(buffer
);
1298 void tracing_reset_online_cpus(struct trace_buffer
*buf
)
1300 struct ring_buffer
*buffer
= buf
->buffer
;
1306 ring_buffer_record_disable(buffer
);
1308 /* Make sure all commits have finished */
1309 synchronize_sched();
1311 buf
->time_start
= buffer_ftrace_now(buf
, buf
->cpu
);
1313 for_each_online_cpu(cpu
)
1314 ring_buffer_reset_cpu(buffer
, cpu
);
1316 ring_buffer_record_enable(buffer
);
1319 /* Must have trace_types_lock held */
1320 void tracing_reset_all_online_cpus(void)
1322 struct trace_array
*tr
;
1324 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
1325 tracing_reset_online_cpus(&tr
->trace_buffer
);
1326 #ifdef CONFIG_TRACER_MAX_TRACE
1327 tracing_reset_online_cpus(&tr
->max_buffer
);
1332 #define SAVED_CMDLINES_DEFAULT 128
1333 #define NO_CMDLINE_MAP UINT_MAX
1334 static arch_spinlock_t trace_cmdline_lock
= __ARCH_SPIN_LOCK_UNLOCKED
;
1335 struct saved_cmdlines_buffer
{
1336 unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
1337 unsigned *map_cmdline_to_pid
;
1338 unsigned cmdline_num
;
1340 char *saved_cmdlines
;
1342 static struct saved_cmdlines_buffer
*savedcmd
;
1344 /* temporary disable recording */
1345 static atomic_t trace_record_cmdline_disabled __read_mostly
;
1347 static inline char *get_saved_cmdlines(int idx
)
1349 return &savedcmd
->saved_cmdlines
[idx
* TASK_COMM_LEN
];
1352 static inline void set_cmdline(int idx
, const char *cmdline
)
1354 memcpy(get_saved_cmdlines(idx
), cmdline
, TASK_COMM_LEN
);
1357 static int allocate_cmdlines_buffer(unsigned int val
,
1358 struct saved_cmdlines_buffer
*s
)
1360 s
->map_cmdline_to_pid
= kmalloc(val
* sizeof(*s
->map_cmdline_to_pid
),
1362 if (!s
->map_cmdline_to_pid
)
1365 s
->saved_cmdlines
= kmalloc(val
* TASK_COMM_LEN
, GFP_KERNEL
);
1366 if (!s
->saved_cmdlines
) {
1367 kfree(s
->map_cmdline_to_pid
);
1372 s
->cmdline_num
= val
;
1373 memset(&s
->map_pid_to_cmdline
, NO_CMDLINE_MAP
,
1374 sizeof(s
->map_pid_to_cmdline
));
1375 memset(s
->map_cmdline_to_pid
, NO_CMDLINE_MAP
,
1376 val
* sizeof(*s
->map_cmdline_to_pid
));
1381 static int trace_create_savedcmd(void)
1385 savedcmd
= kmalloc(sizeof(*savedcmd
), GFP_KERNEL
);
1389 ret
= allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT
, savedcmd
);
1399 int is_tracing_stopped(void)
1401 return global_trace
.stop_count
;
1405 * tracing_start - quick start of the tracer
1407 * If tracing is enabled but was stopped by tracing_stop,
1408 * this will start the tracer back up.
1410 void tracing_start(void)
1412 struct ring_buffer
*buffer
;
1413 unsigned long flags
;
1415 if (tracing_disabled
)
1418 raw_spin_lock_irqsave(&global_trace
.start_lock
, flags
);
1419 if (--global_trace
.stop_count
) {
1420 if (global_trace
.stop_count
< 0) {
1421 /* Someone screwed up their debugging */
1423 global_trace
.stop_count
= 0;
1428 /* Prevent the buffers from switching */
1429 arch_spin_lock(&global_trace
.max_lock
);
1431 buffer
= global_trace
.trace_buffer
.buffer
;
1433 ring_buffer_record_enable(buffer
);
1435 #ifdef CONFIG_TRACER_MAX_TRACE
1436 buffer
= global_trace
.max_buffer
.buffer
;
1438 ring_buffer_record_enable(buffer
);
1441 arch_spin_unlock(&global_trace
.max_lock
);
1444 raw_spin_unlock_irqrestore(&global_trace
.start_lock
, flags
);
1447 static void tracing_start_tr(struct trace_array
*tr
)
1449 struct ring_buffer
*buffer
;
1450 unsigned long flags
;
1452 if (tracing_disabled
)
1455 /* If global, we need to also start the max tracer */
1456 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
)
1457 return tracing_start();
1459 raw_spin_lock_irqsave(&tr
->start_lock
, flags
);
1461 if (--tr
->stop_count
) {
1462 if (tr
->stop_count
< 0) {
1463 /* Someone screwed up their debugging */
1470 buffer
= tr
->trace_buffer
.buffer
;
1472 ring_buffer_record_enable(buffer
);
1475 raw_spin_unlock_irqrestore(&tr
->start_lock
, flags
);
1479 * tracing_stop - quick stop of the tracer
1481 * Light weight way to stop tracing. Use in conjunction with
1484 void tracing_stop(void)
1486 struct ring_buffer
*buffer
;
1487 unsigned long flags
;
1489 raw_spin_lock_irqsave(&global_trace
.start_lock
, flags
);
1490 if (global_trace
.stop_count
++)
1493 /* Prevent the buffers from switching */
1494 arch_spin_lock(&global_trace
.max_lock
);
1496 buffer
= global_trace
.trace_buffer
.buffer
;
1498 ring_buffer_record_disable(buffer
);
1500 #ifdef CONFIG_TRACER_MAX_TRACE
1501 buffer
= global_trace
.max_buffer
.buffer
;
1503 ring_buffer_record_disable(buffer
);
1506 arch_spin_unlock(&global_trace
.max_lock
);
1509 raw_spin_unlock_irqrestore(&global_trace
.start_lock
, flags
);
1512 static void tracing_stop_tr(struct trace_array
*tr
)
1514 struct ring_buffer
*buffer
;
1515 unsigned long flags
;
1517 /* If global, we need to also stop the max tracer */
1518 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
)
1519 return tracing_stop();
1521 raw_spin_lock_irqsave(&tr
->start_lock
, flags
);
1522 if (tr
->stop_count
++)
1525 buffer
= tr
->trace_buffer
.buffer
;
1527 ring_buffer_record_disable(buffer
);
1530 raw_spin_unlock_irqrestore(&tr
->start_lock
, flags
);
1533 void trace_stop_cmdline_recording(void);
1535 static int trace_save_cmdline(struct task_struct
*tsk
)
1539 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
1543 * It's not the end of the world if we don't get
1544 * the lock, but we also don't want to spin
1545 * nor do we want to disable interrupts,
1546 * so if we miss here, then better luck next time.
1548 if (!arch_spin_trylock(&trace_cmdline_lock
))
1551 idx
= savedcmd
->map_pid_to_cmdline
[tsk
->pid
];
1552 if (idx
== NO_CMDLINE_MAP
) {
1553 idx
= (savedcmd
->cmdline_idx
+ 1) % savedcmd
->cmdline_num
;
1556 * Check whether the cmdline buffer at idx has a pid
1557 * mapped. We are going to overwrite that entry so we
1558 * need to clear the map_pid_to_cmdline. Otherwise we
1559 * would read the new comm for the old pid.
1561 pid
= savedcmd
->map_cmdline_to_pid
[idx
];
1562 if (pid
!= NO_CMDLINE_MAP
)
1563 savedcmd
->map_pid_to_cmdline
[pid
] = NO_CMDLINE_MAP
;
1565 savedcmd
->map_cmdline_to_pid
[idx
] = tsk
->pid
;
1566 savedcmd
->map_pid_to_cmdline
[tsk
->pid
] = idx
;
1568 savedcmd
->cmdline_idx
= idx
;
1571 set_cmdline(idx
, tsk
->comm
);
1573 arch_spin_unlock(&trace_cmdline_lock
);
1578 static void __trace_find_cmdline(int pid
, char comm
[])
1583 strcpy(comm
, "<idle>");
1587 if (WARN_ON_ONCE(pid
< 0)) {
1588 strcpy(comm
, "<XXX>");
1592 if (pid
> PID_MAX_DEFAULT
) {
1593 strcpy(comm
, "<...>");
1597 map
= savedcmd
->map_pid_to_cmdline
[pid
];
1598 if (map
!= NO_CMDLINE_MAP
)
1599 strcpy(comm
, get_saved_cmdlines(map
));
1601 strcpy(comm
, "<...>");
1604 void trace_find_cmdline(int pid
, char comm
[])
1607 arch_spin_lock(&trace_cmdline_lock
);
1609 __trace_find_cmdline(pid
, comm
);
1611 arch_spin_unlock(&trace_cmdline_lock
);
1615 void tracing_record_cmdline(struct task_struct
*tsk
)
1617 if (atomic_read(&trace_record_cmdline_disabled
) || !tracing_is_on())
1620 if (!__this_cpu_read(trace_cmdline_save
))
1623 if (trace_save_cmdline(tsk
))
1624 __this_cpu_write(trace_cmdline_save
, false);
1628 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
1631 struct task_struct
*tsk
= current
;
1633 entry
->preempt_count
= pc
& 0xff;
1634 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
1636 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1637 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
1639 TRACE_FLAG_IRQS_NOSUPPORT
|
1641 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
1642 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
1643 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0) |
1644 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED
: 0);
1646 EXPORT_SYMBOL_GPL(tracing_generic_entry_update
);
1648 struct ring_buffer_event
*
1649 trace_buffer_lock_reserve(struct ring_buffer
*buffer
,
1652 unsigned long flags
, int pc
)
1654 struct ring_buffer_event
*event
;
1656 event
= ring_buffer_lock_reserve(buffer
, len
);
1657 if (event
!= NULL
) {
1658 struct trace_entry
*ent
= ring_buffer_event_data(event
);
1660 tracing_generic_entry_update(ent
, flags
, pc
);
1668 __buffer_unlock_commit(struct ring_buffer
*buffer
, struct ring_buffer_event
*event
)
1670 __this_cpu_write(trace_cmdline_save
, true);
1671 ring_buffer_unlock_commit(buffer
, event
);
1675 __trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1676 struct ring_buffer_event
*event
,
1677 unsigned long flags
, int pc
)
1679 __buffer_unlock_commit(buffer
, event
);
1681 ftrace_trace_stack(buffer
, flags
, 6, pc
);
1682 ftrace_trace_userstack(buffer
, flags
, pc
);
1685 void trace_buffer_unlock_commit(struct ring_buffer
*buffer
,
1686 struct ring_buffer_event
*event
,
1687 unsigned long flags
, int pc
)
1689 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
1691 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit
);
1693 static struct ring_buffer
*temp_buffer
;
1695 struct ring_buffer_event
*
1696 trace_event_buffer_lock_reserve(struct ring_buffer
**current_rb
,
1697 struct trace_event_file
*trace_file
,
1698 int type
, unsigned long len
,
1699 unsigned long flags
, int pc
)
1701 struct ring_buffer_event
*entry
;
1703 *current_rb
= trace_file
->tr
->trace_buffer
.buffer
;
1704 entry
= trace_buffer_lock_reserve(*current_rb
,
1705 type
, len
, flags
, pc
);
1707 * If tracing is off, but we have triggers enabled
1708 * we still need to look at the event data. Use the temp_buffer
1709 * to store the trace event for the tigger to use. It's recusive
1710 * safe and will not be recorded anywhere.
1712 if (!entry
&& trace_file
->flags
& EVENT_FILE_FL_TRIGGER_COND
) {
1713 *current_rb
= temp_buffer
;
1714 entry
= trace_buffer_lock_reserve(*current_rb
,
1715 type
, len
, flags
, pc
);
1719 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve
);
1721 struct ring_buffer_event
*
1722 trace_current_buffer_lock_reserve(struct ring_buffer
**current_rb
,
1723 int type
, unsigned long len
,
1724 unsigned long flags
, int pc
)
1726 *current_rb
= global_trace
.trace_buffer
.buffer
;
1727 return trace_buffer_lock_reserve(*current_rb
,
1728 type
, len
, flags
, pc
);
1730 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve
);
1732 void trace_current_buffer_unlock_commit(struct ring_buffer
*buffer
,
1733 struct ring_buffer_event
*event
,
1734 unsigned long flags
, int pc
)
1736 __trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
1738 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit
);
1740 void trace_buffer_unlock_commit_regs(struct ring_buffer
*buffer
,
1741 struct ring_buffer_event
*event
,
1742 unsigned long flags
, int pc
,
1743 struct pt_regs
*regs
)
1745 __buffer_unlock_commit(buffer
, event
);
1747 ftrace_trace_stack_regs(buffer
, flags
, 0, pc
, regs
);
1748 ftrace_trace_userstack(buffer
, flags
, pc
);
1750 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs
);
1752 void trace_current_buffer_discard_commit(struct ring_buffer
*buffer
,
1753 struct ring_buffer_event
*event
)
1755 ring_buffer_discard_commit(buffer
, event
);
1757 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit
);
1760 trace_function(struct trace_array
*tr
,
1761 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
1764 struct trace_event_call
*call
= &event_function
;
1765 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
1766 struct ring_buffer_event
*event
;
1767 struct ftrace_entry
*entry
;
1769 /* If we are reading the ring buffer, don't trace */
1770 if (unlikely(__this_cpu_read(ftrace_cpu_disabled
)))
1773 event
= trace_buffer_lock_reserve(buffer
, TRACE_FN
, sizeof(*entry
),
1777 entry
= ring_buffer_event_data(event
);
1779 entry
->parent_ip
= parent_ip
;
1781 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
1782 __buffer_unlock_commit(buffer
, event
);
1785 #ifdef CONFIG_STACKTRACE
1787 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1788 struct ftrace_stack
{
1789 unsigned long calls
[FTRACE_STACK_MAX_ENTRIES
];
1792 static DEFINE_PER_CPU(struct ftrace_stack
, ftrace_stack
);
1793 static DEFINE_PER_CPU(int, ftrace_stack_reserve
);
1795 static void __ftrace_trace_stack(struct ring_buffer
*buffer
,
1796 unsigned long flags
,
1797 int skip
, int pc
, struct pt_regs
*regs
)
1799 struct trace_event_call
*call
= &event_kernel_stack
;
1800 struct ring_buffer_event
*event
;
1801 struct stack_entry
*entry
;
1802 struct stack_trace trace
;
1804 int size
= FTRACE_STACK_ENTRIES
;
1806 trace
.nr_entries
= 0;
1810 * Since events can happen in NMIs there's no safe way to
1811 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1812 * or NMI comes in, it will just have to use the default
1813 * FTRACE_STACK_SIZE.
1815 preempt_disable_notrace();
1817 use_stack
= __this_cpu_inc_return(ftrace_stack_reserve
);
1819 * We don't need any atomic variables, just a barrier.
1820 * If an interrupt comes in, we don't care, because it would
1821 * have exited and put the counter back to what we want.
1822 * We just need a barrier to keep gcc from moving things
1826 if (use_stack
== 1) {
1827 trace
.entries
= this_cpu_ptr(ftrace_stack
.calls
);
1828 trace
.max_entries
= FTRACE_STACK_MAX_ENTRIES
;
1831 save_stack_trace_regs(regs
, &trace
);
1833 save_stack_trace(&trace
);
1835 if (trace
.nr_entries
> size
)
1836 size
= trace
.nr_entries
;
1838 /* From now on, use_stack is a boolean */
1841 size
*= sizeof(unsigned long);
1843 event
= trace_buffer_lock_reserve(buffer
, TRACE_STACK
,
1844 sizeof(*entry
) + size
, flags
, pc
);
1847 entry
= ring_buffer_event_data(event
);
1849 memset(&entry
->caller
, 0, size
);
1852 memcpy(&entry
->caller
, trace
.entries
,
1853 trace
.nr_entries
* sizeof(unsigned long));
1855 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1856 trace
.entries
= entry
->caller
;
1858 save_stack_trace_regs(regs
, &trace
);
1860 save_stack_trace(&trace
);
1863 entry
->size
= trace
.nr_entries
;
1865 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
1866 __buffer_unlock_commit(buffer
, event
);
1869 /* Again, don't let gcc optimize things here */
1871 __this_cpu_dec(ftrace_stack_reserve
);
1872 preempt_enable_notrace();
1876 void ftrace_trace_stack_regs(struct ring_buffer
*buffer
, unsigned long flags
,
1877 int skip
, int pc
, struct pt_regs
*regs
)
1879 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1882 __ftrace_trace_stack(buffer
, flags
, skip
, pc
, regs
);
1885 void ftrace_trace_stack(struct ring_buffer
*buffer
, unsigned long flags
,
1888 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
1891 __ftrace_trace_stack(buffer
, flags
, skip
, pc
, NULL
);
1894 void __trace_stack(struct trace_array
*tr
, unsigned long flags
, int skip
,
1897 __ftrace_trace_stack(tr
->trace_buffer
.buffer
, flags
, skip
, pc
, NULL
);
1901 * trace_dump_stack - record a stack back trace in the trace buffer
1902 * @skip: Number of functions to skip (helper handlers)
1904 void trace_dump_stack(int skip
)
1906 unsigned long flags
;
1908 if (tracing_disabled
|| tracing_selftest_running
)
1911 local_save_flags(flags
);
1914 * Skip 3 more, seems to get us at the caller of
1918 __ftrace_trace_stack(global_trace
.trace_buffer
.buffer
,
1919 flags
, skip
, preempt_count(), NULL
);
1922 static DEFINE_PER_CPU(int, user_stack_count
);
1925 ftrace_trace_userstack(struct ring_buffer
*buffer
, unsigned long flags
, int pc
)
1927 struct trace_event_call
*call
= &event_user_stack
;
1928 struct ring_buffer_event
*event
;
1929 struct userstack_entry
*entry
;
1930 struct stack_trace trace
;
1932 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1936 * NMIs can not handle page faults, even with fix ups.
1937 * The save user stack can (and often does) fault.
1939 if (unlikely(in_nmi()))
1943 * prevent recursion, since the user stack tracing may
1944 * trigger other kernel events.
1947 if (__this_cpu_read(user_stack_count
))
1950 __this_cpu_inc(user_stack_count
);
1952 event
= trace_buffer_lock_reserve(buffer
, TRACE_USER_STACK
,
1953 sizeof(*entry
), flags
, pc
);
1955 goto out_drop_count
;
1956 entry
= ring_buffer_event_data(event
);
1958 entry
->tgid
= current
->tgid
;
1959 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1961 trace
.nr_entries
= 0;
1962 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1964 trace
.entries
= entry
->caller
;
1966 save_stack_trace_user(&trace
);
1967 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
1968 __buffer_unlock_commit(buffer
, event
);
1971 __this_cpu_dec(user_stack_count
);
1977 static void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
1979 ftrace_trace_userstack(tr
, flags
, preempt_count());
1983 #endif /* CONFIG_STACKTRACE */
1985 /* created for use with alloc_percpu */
1986 struct trace_buffer_struct
{
1987 char buffer
[TRACE_BUF_SIZE
];
1990 static struct trace_buffer_struct
*trace_percpu_buffer
;
1991 static struct trace_buffer_struct
*trace_percpu_sirq_buffer
;
1992 static struct trace_buffer_struct
*trace_percpu_irq_buffer
;
1993 static struct trace_buffer_struct
*trace_percpu_nmi_buffer
;
1996 * The buffer used is dependent on the context. There is a per cpu
1997 * buffer for normal context, softirq contex, hard irq context and
1998 * for NMI context. Thise allows for lockless recording.
2000 * Note, if the buffers failed to be allocated, then this returns NULL
2002 static char *get_trace_buf(void)
2004 struct trace_buffer_struct
*percpu_buffer
;
2007 * If we have allocated per cpu buffers, then we do not
2008 * need to do any locking.
2011 percpu_buffer
= trace_percpu_nmi_buffer
;
2013 percpu_buffer
= trace_percpu_irq_buffer
;
2014 else if (in_softirq())
2015 percpu_buffer
= trace_percpu_sirq_buffer
;
2017 percpu_buffer
= trace_percpu_buffer
;
2022 return this_cpu_ptr(&percpu_buffer
->buffer
[0]);
2025 static int alloc_percpu_trace_buffer(void)
2027 struct trace_buffer_struct
*buffers
;
2028 struct trace_buffer_struct
*sirq_buffers
;
2029 struct trace_buffer_struct
*irq_buffers
;
2030 struct trace_buffer_struct
*nmi_buffers
;
2032 buffers
= alloc_percpu(struct trace_buffer_struct
);
2036 sirq_buffers
= alloc_percpu(struct trace_buffer_struct
);
2040 irq_buffers
= alloc_percpu(struct trace_buffer_struct
);
2044 nmi_buffers
= alloc_percpu(struct trace_buffer_struct
);
2048 trace_percpu_buffer
= buffers
;
2049 trace_percpu_sirq_buffer
= sirq_buffers
;
2050 trace_percpu_irq_buffer
= irq_buffers
;
2051 trace_percpu_nmi_buffer
= nmi_buffers
;
2056 free_percpu(irq_buffers
);
2058 free_percpu(sirq_buffers
);
2060 free_percpu(buffers
);
2062 WARN(1, "Could not allocate percpu trace_printk buffer");
2066 static int buffers_allocated
;
2068 void trace_printk_init_buffers(void)
2070 if (buffers_allocated
)
2073 if (alloc_percpu_trace_buffer())
2076 /* trace_printk() is for debug use only. Don't use it in production. */
2079 pr_warning("**********************************************************\n");
2080 pr_warning("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2081 pr_warning("** **\n");
2082 pr_warning("** trace_printk() being used. Allocating extra memory. **\n");
2083 pr_warning("** **\n");
2084 pr_warning("** This means that this is a DEBUG kernel and it is **\n");
2085 pr_warning("** unsafe for production use. **\n");
2086 pr_warning("** **\n");
2087 pr_warning("** If you see this message and you are not debugging **\n");
2088 pr_warning("** the kernel, report this immediately to your vendor! **\n");
2089 pr_warning("** **\n");
2090 pr_warning("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2091 pr_warning("**********************************************************\n");
2093 /* Expand the buffers to set size */
2094 tracing_update_buffers();
2096 buffers_allocated
= 1;
2099 * trace_printk_init_buffers() can be called by modules.
2100 * If that happens, then we need to start cmdline recording
2101 * directly here. If the global_trace.buffer is already
2102 * allocated here, then this was called by module code.
2104 if (global_trace
.trace_buffer
.buffer
)
2105 tracing_start_cmdline_record();
2108 void trace_printk_start_comm(void)
2110 /* Start tracing comms if trace printk is set */
2111 if (!buffers_allocated
)
2113 tracing_start_cmdline_record();
2116 static void trace_printk_start_stop_comm(int enabled
)
2118 if (!buffers_allocated
)
2122 tracing_start_cmdline_record();
2124 tracing_stop_cmdline_record();
2128 * trace_vbprintk - write binary msg to tracing buffer
2131 int trace_vbprintk(unsigned long ip
, const char *fmt
, va_list args
)
2133 struct trace_event_call
*call
= &event_bprint
;
2134 struct ring_buffer_event
*event
;
2135 struct ring_buffer
*buffer
;
2136 struct trace_array
*tr
= &global_trace
;
2137 struct bprint_entry
*entry
;
2138 unsigned long flags
;
2140 int len
= 0, size
, pc
;
2142 if (unlikely(tracing_selftest_running
|| tracing_disabled
))
2145 /* Don't pollute graph traces with trace_vprintk internals */
2146 pause_graph_tracing();
2148 pc
= preempt_count();
2149 preempt_disable_notrace();
2151 tbuffer
= get_trace_buf();
2157 len
= vbin_printf((u32
*)tbuffer
, TRACE_BUF_SIZE
/sizeof(int), fmt
, args
);
2159 if (len
> TRACE_BUF_SIZE
/sizeof(int) || len
< 0)
2162 local_save_flags(flags
);
2163 size
= sizeof(*entry
) + sizeof(u32
) * len
;
2164 buffer
= tr
->trace_buffer
.buffer
;
2165 event
= trace_buffer_lock_reserve(buffer
, TRACE_BPRINT
, size
,
2169 entry
= ring_buffer_event_data(event
);
2173 memcpy(entry
->buf
, tbuffer
, sizeof(u32
) * len
);
2174 if (!call_filter_check_discard(call
, entry
, buffer
, event
)) {
2175 __buffer_unlock_commit(buffer
, event
);
2176 ftrace_trace_stack(buffer
, flags
, 6, pc
);
2180 preempt_enable_notrace();
2181 unpause_graph_tracing();
2185 EXPORT_SYMBOL_GPL(trace_vbprintk
);
2188 __trace_array_vprintk(struct ring_buffer
*buffer
,
2189 unsigned long ip
, const char *fmt
, va_list args
)
2191 struct trace_event_call
*call
= &event_print
;
2192 struct ring_buffer_event
*event
;
2193 int len
= 0, size
, pc
;
2194 struct print_entry
*entry
;
2195 unsigned long flags
;
2198 if (tracing_disabled
|| tracing_selftest_running
)
2201 /* Don't pollute graph traces with trace_vprintk internals */
2202 pause_graph_tracing();
2204 pc
= preempt_count();
2205 preempt_disable_notrace();
2208 tbuffer
= get_trace_buf();
2214 len
= vscnprintf(tbuffer
, TRACE_BUF_SIZE
, fmt
, args
);
2216 local_save_flags(flags
);
2217 size
= sizeof(*entry
) + len
+ 1;
2218 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
2222 entry
= ring_buffer_event_data(event
);
2225 memcpy(&entry
->buf
, tbuffer
, len
+ 1);
2226 if (!call_filter_check_discard(call
, entry
, buffer
, event
)) {
2227 __buffer_unlock_commit(buffer
, event
);
2228 ftrace_trace_stack(buffer
, flags
, 6, pc
);
2231 preempt_enable_notrace();
2232 unpause_graph_tracing();
2237 int trace_array_vprintk(struct trace_array
*tr
,
2238 unsigned long ip
, const char *fmt
, va_list args
)
2240 return __trace_array_vprintk(tr
->trace_buffer
.buffer
, ip
, fmt
, args
);
2243 int trace_array_printk(struct trace_array
*tr
,
2244 unsigned long ip
, const char *fmt
, ...)
2249 if (!(trace_flags
& TRACE_ITER_PRINTK
))
2253 ret
= trace_array_vprintk(tr
, ip
, fmt
, ap
);
2258 int trace_array_printk_buf(struct ring_buffer
*buffer
,
2259 unsigned long ip
, const char *fmt
, ...)
2264 if (!(trace_flags
& TRACE_ITER_PRINTK
))
2268 ret
= __trace_array_vprintk(buffer
, ip
, fmt
, ap
);
2273 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
2275 return trace_array_vprintk(&global_trace
, ip
, fmt
, args
);
2277 EXPORT_SYMBOL_GPL(trace_vprintk
);
2279 static void trace_iterator_increment(struct trace_iterator
*iter
)
2281 struct ring_buffer_iter
*buf_iter
= trace_buffer_iter(iter
, iter
->cpu
);
2285 ring_buffer_read(buf_iter
, NULL
);
2288 static struct trace_entry
*
2289 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
,
2290 unsigned long *lost_events
)
2292 struct ring_buffer_event
*event
;
2293 struct ring_buffer_iter
*buf_iter
= trace_buffer_iter(iter
, cpu
);
2296 event
= ring_buffer_iter_peek(buf_iter
, ts
);
2298 event
= ring_buffer_peek(iter
->trace_buffer
->buffer
, cpu
, ts
,
2302 iter
->ent_size
= ring_buffer_event_length(event
);
2303 return ring_buffer_event_data(event
);
2309 static struct trace_entry
*
2310 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
,
2311 unsigned long *missing_events
, u64
*ent_ts
)
2313 struct ring_buffer
*buffer
= iter
->trace_buffer
->buffer
;
2314 struct trace_entry
*ent
, *next
= NULL
;
2315 unsigned long lost_events
= 0, next_lost
= 0;
2316 int cpu_file
= iter
->cpu_file
;
2317 u64 next_ts
= 0, ts
;
2323 * If we are in a per_cpu trace file, don't bother by iterating over
2324 * all cpu and peek directly.
2326 if (cpu_file
> RING_BUFFER_ALL_CPUS
) {
2327 if (ring_buffer_empty_cpu(buffer
, cpu_file
))
2329 ent
= peek_next_entry(iter
, cpu_file
, ent_ts
, missing_events
);
2331 *ent_cpu
= cpu_file
;
2336 for_each_tracing_cpu(cpu
) {
2338 if (ring_buffer_empty_cpu(buffer
, cpu
))
2341 ent
= peek_next_entry(iter
, cpu
, &ts
, &lost_events
);
2344 * Pick the entry with the smallest timestamp:
2346 if (ent
&& (!next
|| ts
< next_ts
)) {
2350 next_lost
= lost_events
;
2351 next_size
= iter
->ent_size
;
2355 iter
->ent_size
= next_size
;
2358 *ent_cpu
= next_cpu
;
2364 *missing_events
= next_lost
;
2369 /* Find the next real entry, without updating the iterator itself */
2370 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
2371 int *ent_cpu
, u64
*ent_ts
)
2373 return __find_next_entry(iter
, ent_cpu
, NULL
, ent_ts
);
2376 /* Find the next real entry, and increment the iterator to the next entry */
2377 void *trace_find_next_entry_inc(struct trace_iterator
*iter
)
2379 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
,
2380 &iter
->lost_events
, &iter
->ts
);
2383 trace_iterator_increment(iter
);
2385 return iter
->ent
? iter
: NULL
;
2388 static void trace_consume(struct trace_iterator
*iter
)
2390 ring_buffer_consume(iter
->trace_buffer
->buffer
, iter
->cpu
, &iter
->ts
,
2391 &iter
->lost_events
);
2394 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2396 struct trace_iterator
*iter
= m
->private;
2400 WARN_ON_ONCE(iter
->leftover
);
2404 /* can't go backwards */
2409 ent
= trace_find_next_entry_inc(iter
);
2413 while (ent
&& iter
->idx
< i
)
2414 ent
= trace_find_next_entry_inc(iter
);
2421 void tracing_iter_reset(struct trace_iterator
*iter
, int cpu
)
2423 struct ring_buffer_event
*event
;
2424 struct ring_buffer_iter
*buf_iter
;
2425 unsigned long entries
= 0;
2428 per_cpu_ptr(iter
->trace_buffer
->data
, cpu
)->skipped_entries
= 0;
2430 buf_iter
= trace_buffer_iter(iter
, cpu
);
2434 ring_buffer_iter_reset(buf_iter
);
2437 * We could have the case with the max latency tracers
2438 * that a reset never took place on a cpu. This is evident
2439 * by the timestamp being before the start of the buffer.
2441 while ((event
= ring_buffer_iter_peek(buf_iter
, &ts
))) {
2442 if (ts
>= iter
->trace_buffer
->time_start
)
2445 ring_buffer_read(buf_iter
, NULL
);
2448 per_cpu_ptr(iter
->trace_buffer
->data
, cpu
)->skipped_entries
= entries
;
2452 * The current tracer is copied to avoid a global locking
2455 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
2457 struct trace_iterator
*iter
= m
->private;
2458 struct trace_array
*tr
= iter
->tr
;
2459 int cpu_file
= iter
->cpu_file
;
2465 * copy the tracer to avoid using a global lock all around.
2466 * iter->trace is a copy of current_trace, the pointer to the
2467 * name may be used instead of a strcmp(), as iter->trace->name
2468 * will point to the same string as current_trace->name.
2470 mutex_lock(&trace_types_lock
);
2471 if (unlikely(tr
->current_trace
&& iter
->trace
->name
!= tr
->current_trace
->name
))
2472 *iter
->trace
= *tr
->current_trace
;
2473 mutex_unlock(&trace_types_lock
);
2475 #ifdef CONFIG_TRACER_MAX_TRACE
2476 if (iter
->snapshot
&& iter
->trace
->use_max_tr
)
2477 return ERR_PTR(-EBUSY
);
2480 if (!iter
->snapshot
)
2481 atomic_inc(&trace_record_cmdline_disabled
);
2483 if (*pos
!= iter
->pos
) {
2488 if (cpu_file
== RING_BUFFER_ALL_CPUS
) {
2489 for_each_tracing_cpu(cpu
)
2490 tracing_iter_reset(iter
, cpu
);
2492 tracing_iter_reset(iter
, cpu_file
);
2495 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
2500 * If we overflowed the seq_file before, then we want
2501 * to just reuse the trace_seq buffer again.
2507 p
= s_next(m
, p
, &l
);
2511 trace_event_read_lock();
2512 trace_access_lock(cpu_file
);
2516 static void s_stop(struct seq_file
*m
, void *p
)
2518 struct trace_iterator
*iter
= m
->private;
2520 #ifdef CONFIG_TRACER_MAX_TRACE
2521 if (iter
->snapshot
&& iter
->trace
->use_max_tr
)
2525 if (!iter
->snapshot
)
2526 atomic_dec(&trace_record_cmdline_disabled
);
2528 trace_access_unlock(iter
->cpu_file
);
2529 trace_event_read_unlock();
2533 get_total_entries(struct trace_buffer
*buf
,
2534 unsigned long *total
, unsigned long *entries
)
2536 unsigned long count
;
2542 for_each_tracing_cpu(cpu
) {
2543 count
= ring_buffer_entries_cpu(buf
->buffer
, cpu
);
2545 * If this buffer has skipped entries, then we hold all
2546 * entries for the trace and we need to ignore the
2547 * ones before the time stamp.
2549 if (per_cpu_ptr(buf
->data
, cpu
)->skipped_entries
) {
2550 count
-= per_cpu_ptr(buf
->data
, cpu
)->skipped_entries
;
2551 /* total is the same as the entries */
2555 ring_buffer_overrun_cpu(buf
->buffer
, cpu
);
2560 static void print_lat_help_header(struct seq_file
*m
)
2562 seq_puts(m
, "# _------=> CPU# \n"
2563 "# / _-----=> irqs-off \n"
2564 "# | / _----=> need-resched \n"
2565 "# || / _---=> hardirq/softirq \n"
2566 "# ||| / _--=> preempt-depth \n"
2568 "# cmd pid ||||| time | caller \n"
2569 "# \\ / ||||| \\ | / \n");
2572 static void print_event_info(struct trace_buffer
*buf
, struct seq_file
*m
)
2574 unsigned long total
;
2575 unsigned long entries
;
2577 get_total_entries(buf
, &total
, &entries
);
2578 seq_printf(m
, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2579 entries
, total
, num_online_cpus());
2583 static void print_func_help_header(struct trace_buffer
*buf
, struct seq_file
*m
)
2585 print_event_info(buf
, m
);
2586 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n"
2590 static void print_func_help_header_irq(struct trace_buffer
*buf
, struct seq_file
*m
)
2592 print_event_info(buf
, m
);
2593 seq_puts(m
, "# _-----=> irqs-off\n"
2594 "# / _----=> need-resched\n"
2595 "# | / _---=> hardirq/softirq\n"
2596 "# || / _--=> preempt-depth\n"
2598 "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
2599 "# | | | |||| | |\n");
2603 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
2605 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
2606 struct trace_buffer
*buf
= iter
->trace_buffer
;
2607 struct trace_array_cpu
*data
= per_cpu_ptr(buf
->data
, buf
->cpu
);
2608 struct tracer
*type
= iter
->trace
;
2609 unsigned long entries
;
2610 unsigned long total
;
2611 const char *name
= "preemption";
2615 get_total_entries(buf
, &total
, &entries
);
2617 seq_printf(m
, "# %s latency trace v1.1.5 on %s\n",
2619 seq_puts(m
, "# -----------------------------------"
2620 "---------------------------------\n");
2621 seq_printf(m
, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2622 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2623 nsecs_to_usecs(data
->saved_latency
),
2627 #if defined(CONFIG_PREEMPT_NONE)
2629 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2631 #elif defined(CONFIG_PREEMPT)
2636 /* These are reserved for later use */
2639 seq_printf(m
, " #P:%d)\n", num_online_cpus());
2643 seq_puts(m
, "# -----------------\n");
2644 seq_printf(m
, "# | task: %.16s-%d "
2645 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2646 data
->comm
, data
->pid
,
2647 from_kuid_munged(seq_user_ns(m
), data
->uid
), data
->nice
,
2648 data
->policy
, data
->rt_priority
);
2649 seq_puts(m
, "# -----------------\n");
2651 if (data
->critical_start
) {
2652 seq_puts(m
, "# => started at: ");
2653 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
2654 trace_print_seq(m
, &iter
->seq
);
2655 seq_puts(m
, "\n# => ended at: ");
2656 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
2657 trace_print_seq(m
, &iter
->seq
);
2658 seq_puts(m
, "\n#\n");
2664 static void test_cpu_buff_start(struct trace_iterator
*iter
)
2666 struct trace_seq
*s
= &iter
->seq
;
2668 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
2671 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
2674 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
2677 if (per_cpu_ptr(iter
->trace_buffer
->data
, iter
->cpu
)->skipped_entries
)
2680 cpumask_set_cpu(iter
->cpu
, iter
->started
);
2682 /* Don't print started cpu buffer for the first entry of the trace */
2684 trace_seq_printf(s
, "##### CPU %u buffer started ####\n",
2688 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
2690 struct trace_seq
*s
= &iter
->seq
;
2691 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
2692 struct trace_entry
*entry
;
2693 struct trace_event
*event
;
2697 test_cpu_buff_start(iter
);
2699 event
= ftrace_find_event(entry
->type
);
2701 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2702 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
2703 trace_print_lat_context(iter
);
2705 trace_print_context(iter
);
2708 if (trace_seq_has_overflowed(s
))
2709 return TRACE_TYPE_PARTIAL_LINE
;
2712 return event
->funcs
->trace(iter
, sym_flags
, event
);
2714 trace_seq_printf(s
, "Unknown type %d\n", entry
->type
);
2716 return trace_handle_return(s
);
2719 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
2721 struct trace_seq
*s
= &iter
->seq
;
2722 struct trace_entry
*entry
;
2723 struct trace_event
*event
;
2727 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
)
2728 trace_seq_printf(s
, "%d %d %llu ",
2729 entry
->pid
, iter
->cpu
, iter
->ts
);
2731 if (trace_seq_has_overflowed(s
))
2732 return TRACE_TYPE_PARTIAL_LINE
;
2734 event
= ftrace_find_event(entry
->type
);
2736 return event
->funcs
->raw(iter
, 0, event
);
2738 trace_seq_printf(s
, "%d ?\n", entry
->type
);
2740 return trace_handle_return(s
);
2743 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
2745 struct trace_seq
*s
= &iter
->seq
;
2746 unsigned char newline
= '\n';
2747 struct trace_entry
*entry
;
2748 struct trace_event
*event
;
2752 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2753 SEQ_PUT_HEX_FIELD(s
, entry
->pid
);
2754 SEQ_PUT_HEX_FIELD(s
, iter
->cpu
);
2755 SEQ_PUT_HEX_FIELD(s
, iter
->ts
);
2756 if (trace_seq_has_overflowed(s
))
2757 return TRACE_TYPE_PARTIAL_LINE
;
2760 event
= ftrace_find_event(entry
->type
);
2762 enum print_line_t ret
= event
->funcs
->hex(iter
, 0, event
);
2763 if (ret
!= TRACE_TYPE_HANDLED
)
2767 SEQ_PUT_FIELD(s
, newline
);
2769 return trace_handle_return(s
);
2772 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
2774 struct trace_seq
*s
= &iter
->seq
;
2775 struct trace_entry
*entry
;
2776 struct trace_event
*event
;
2780 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
2781 SEQ_PUT_FIELD(s
, entry
->pid
);
2782 SEQ_PUT_FIELD(s
, iter
->cpu
);
2783 SEQ_PUT_FIELD(s
, iter
->ts
);
2784 if (trace_seq_has_overflowed(s
))
2785 return TRACE_TYPE_PARTIAL_LINE
;
2788 event
= ftrace_find_event(entry
->type
);
2789 return event
? event
->funcs
->binary(iter
, 0, event
) :
2793 int trace_empty(struct trace_iterator
*iter
)
2795 struct ring_buffer_iter
*buf_iter
;
2798 /* If we are looking at one CPU buffer, only check that one */
2799 if (iter
->cpu_file
!= RING_BUFFER_ALL_CPUS
) {
2800 cpu
= iter
->cpu_file
;
2801 buf_iter
= trace_buffer_iter(iter
, cpu
);
2803 if (!ring_buffer_iter_empty(buf_iter
))
2806 if (!ring_buffer_empty_cpu(iter
->trace_buffer
->buffer
, cpu
))
2812 for_each_tracing_cpu(cpu
) {
2813 buf_iter
= trace_buffer_iter(iter
, cpu
);
2815 if (!ring_buffer_iter_empty(buf_iter
))
2818 if (!ring_buffer_empty_cpu(iter
->trace_buffer
->buffer
, cpu
))
2826 /* Called with trace_event_read_lock() held. */
2827 enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
2829 enum print_line_t ret
;
2831 if (iter
->lost_events
) {
2832 trace_seq_printf(&iter
->seq
, "CPU:%d [LOST %lu EVENTS]\n",
2833 iter
->cpu
, iter
->lost_events
);
2834 if (trace_seq_has_overflowed(&iter
->seq
))
2835 return TRACE_TYPE_PARTIAL_LINE
;
2838 if (iter
->trace
&& iter
->trace
->print_line
) {
2839 ret
= iter
->trace
->print_line(iter
);
2840 if (ret
!= TRACE_TYPE_UNHANDLED
)
2844 if (iter
->ent
->type
== TRACE_BPUTS
&&
2845 trace_flags
& TRACE_ITER_PRINTK
&&
2846 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2847 return trace_print_bputs_msg_only(iter
);
2849 if (iter
->ent
->type
== TRACE_BPRINT
&&
2850 trace_flags
& TRACE_ITER_PRINTK
&&
2851 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2852 return trace_print_bprintk_msg_only(iter
);
2854 if (iter
->ent
->type
== TRACE_PRINT
&&
2855 trace_flags
& TRACE_ITER_PRINTK
&&
2856 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2857 return trace_print_printk_msg_only(iter
);
2859 if (trace_flags
& TRACE_ITER_BIN
)
2860 return print_bin_fmt(iter
);
2862 if (trace_flags
& TRACE_ITER_HEX
)
2863 return print_hex_fmt(iter
);
2865 if (trace_flags
& TRACE_ITER_RAW
)
2866 return print_raw_fmt(iter
);
2868 return print_trace_fmt(iter
);
2871 void trace_latency_header(struct seq_file
*m
)
2873 struct trace_iterator
*iter
= m
->private;
2875 /* print nothing if the buffers are empty */
2876 if (trace_empty(iter
))
2879 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
2880 print_trace_header(m
, iter
);
2882 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2883 print_lat_help_header(m
);
2886 void trace_default_header(struct seq_file
*m
)
2888 struct trace_iterator
*iter
= m
->private;
2890 if (!(trace_flags
& TRACE_ITER_CONTEXT_INFO
))
2893 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
2894 /* print nothing if the buffers are empty */
2895 if (trace_empty(iter
))
2897 print_trace_header(m
, iter
);
2898 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2899 print_lat_help_header(m
);
2901 if (!(trace_flags
& TRACE_ITER_VERBOSE
)) {
2902 if (trace_flags
& TRACE_ITER_IRQ_INFO
)
2903 print_func_help_header_irq(iter
->trace_buffer
, m
);
2905 print_func_help_header(iter
->trace_buffer
, m
);
2910 static void test_ftrace_alive(struct seq_file
*m
)
2912 if (!ftrace_is_dead())
2914 seq_puts(m
, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
2915 "# MAY BE MISSING FUNCTION EVENTS\n");
2918 #ifdef CONFIG_TRACER_MAX_TRACE
2919 static void show_snapshot_main_help(struct seq_file
*m
)
2921 seq_puts(m
, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
2922 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
2923 "# Takes a snapshot of the main buffer.\n"
2924 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
2925 "# (Doesn't have to be '2' works with any number that\n"
2926 "# is not a '0' or '1')\n");
2929 static void show_snapshot_percpu_help(struct seq_file
*m
)
2931 seq_puts(m
, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2932 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2933 seq_puts(m
, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
2934 "# Takes a snapshot of the main buffer for this cpu.\n");
2936 seq_puts(m
, "# echo 1 > snapshot : Not supported with this kernel.\n"
2937 "# Must use main snapshot file to allocate.\n");
2939 seq_puts(m
, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
2940 "# (Doesn't have to be '2' works with any number that\n"
2941 "# is not a '0' or '1')\n");
2944 static void print_snapshot_help(struct seq_file
*m
, struct trace_iterator
*iter
)
2946 if (iter
->tr
->allocated_snapshot
)
2947 seq_puts(m
, "#\n# * Snapshot is allocated *\n#\n");
2949 seq_puts(m
, "#\n# * Snapshot is freed *\n#\n");
2951 seq_puts(m
, "# Snapshot commands:\n");
2952 if (iter
->cpu_file
== RING_BUFFER_ALL_CPUS
)
2953 show_snapshot_main_help(m
);
2955 show_snapshot_percpu_help(m
);
2958 /* Should never be called */
2959 static inline void print_snapshot_help(struct seq_file
*m
, struct trace_iterator
*iter
) { }
2962 static int s_show(struct seq_file
*m
, void *v
)
2964 struct trace_iterator
*iter
= v
;
2967 if (iter
->ent
== NULL
) {
2969 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
2971 test_ftrace_alive(m
);
2973 if (iter
->snapshot
&& trace_empty(iter
))
2974 print_snapshot_help(m
, iter
);
2975 else if (iter
->trace
&& iter
->trace
->print_header
)
2976 iter
->trace
->print_header(m
);
2978 trace_default_header(m
);
2980 } else if (iter
->leftover
) {
2982 * If we filled the seq_file buffer earlier, we
2983 * want to just show it now.
2985 ret
= trace_print_seq(m
, &iter
->seq
);
2987 /* ret should this time be zero, but you never know */
2988 iter
->leftover
= ret
;
2991 print_trace_line(iter
);
2992 ret
= trace_print_seq(m
, &iter
->seq
);
2994 * If we overflow the seq_file buffer, then it will
2995 * ask us for this data again at start up.
2997 * ret is 0 if seq_file write succeeded.
3000 iter
->leftover
= ret
;
3007 * Should be used after trace_array_get(), trace_types_lock
3008 * ensures that i_cdev was already initialized.
3010 static inline int tracing_get_cpu(struct inode
*inode
)
3012 if (inode
->i_cdev
) /* See trace_create_cpu_file() */
3013 return (long)inode
->i_cdev
- 1;
3014 return RING_BUFFER_ALL_CPUS
;
3017 static const struct seq_operations tracer_seq_ops
= {
3024 static struct trace_iterator
*
3025 __tracing_open(struct inode
*inode
, struct file
*file
, bool snapshot
)
3027 struct trace_array
*tr
= inode
->i_private
;
3028 struct trace_iterator
*iter
;
3031 if (tracing_disabled
)
3032 return ERR_PTR(-ENODEV
);
3034 iter
= __seq_open_private(file
, &tracer_seq_ops
, sizeof(*iter
));
3036 return ERR_PTR(-ENOMEM
);
3038 iter
->buffer_iter
= kzalloc(sizeof(*iter
->buffer_iter
) * num_possible_cpus(),
3040 if (!iter
->buffer_iter
)
3044 * We make a copy of the current tracer to avoid concurrent
3045 * changes on it while we are reading.
3047 mutex_lock(&trace_types_lock
);
3048 iter
->trace
= kzalloc(sizeof(*iter
->trace
), GFP_KERNEL
);
3052 *iter
->trace
= *tr
->current_trace
;
3054 if (!zalloc_cpumask_var(&iter
->started
, GFP_KERNEL
))
3059 #ifdef CONFIG_TRACER_MAX_TRACE
3060 /* Currently only the top directory has a snapshot */
3061 if (tr
->current_trace
->print_max
|| snapshot
)
3062 iter
->trace_buffer
= &tr
->max_buffer
;
3065 iter
->trace_buffer
= &tr
->trace_buffer
;
3066 iter
->snapshot
= snapshot
;
3068 iter
->cpu_file
= tracing_get_cpu(inode
);
3069 mutex_init(&iter
->mutex
);
3071 /* Notify the tracer early; before we stop tracing. */
3072 if (iter
->trace
&& iter
->trace
->open
)
3073 iter
->trace
->open(iter
);
3075 /* Annotate start of buffers if we had overruns */
3076 if (ring_buffer_overruns(iter
->trace_buffer
->buffer
))
3077 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
3079 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3080 if (trace_clocks
[tr
->clock_id
].in_ns
)
3081 iter
->iter_flags
|= TRACE_FILE_TIME_IN_NS
;
3083 /* stop the trace while dumping if we are not opening "snapshot" */
3084 if (!iter
->snapshot
)
3085 tracing_stop_tr(tr
);
3087 if (iter
->cpu_file
== RING_BUFFER_ALL_CPUS
) {
3088 for_each_tracing_cpu(cpu
) {
3089 iter
->buffer_iter
[cpu
] =
3090 ring_buffer_read_prepare(iter
->trace_buffer
->buffer
, cpu
);
3092 ring_buffer_read_prepare_sync();
3093 for_each_tracing_cpu(cpu
) {
3094 ring_buffer_read_start(iter
->buffer_iter
[cpu
]);
3095 tracing_iter_reset(iter
, cpu
);
3098 cpu
= iter
->cpu_file
;
3099 iter
->buffer_iter
[cpu
] =
3100 ring_buffer_read_prepare(iter
->trace_buffer
->buffer
, cpu
);
3101 ring_buffer_read_prepare_sync();
3102 ring_buffer_read_start(iter
->buffer_iter
[cpu
]);
3103 tracing_iter_reset(iter
, cpu
);
3106 mutex_unlock(&trace_types_lock
);
3111 mutex_unlock(&trace_types_lock
);
3113 kfree(iter
->buffer_iter
);
3115 seq_release_private(inode
, file
);
3116 return ERR_PTR(-ENOMEM
);
3119 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
3121 if (tracing_disabled
)
3124 filp
->private_data
= inode
->i_private
;
3128 bool tracing_is_disabled(void)
3130 return (tracing_disabled
) ? true: false;
3134 * Open and update trace_array ref count.
3135 * Must have the current trace_array passed to it.
3137 static int tracing_open_generic_tr(struct inode
*inode
, struct file
*filp
)
3139 struct trace_array
*tr
= inode
->i_private
;
3141 if (tracing_disabled
)
3144 if (trace_array_get(tr
) < 0)
3147 filp
->private_data
= inode
->i_private
;
3152 static int tracing_release(struct inode
*inode
, struct file
*file
)
3154 struct trace_array
*tr
= inode
->i_private
;
3155 struct seq_file
*m
= file
->private_data
;
3156 struct trace_iterator
*iter
;
3159 if (!(file
->f_mode
& FMODE_READ
)) {
3160 trace_array_put(tr
);
3164 /* Writes do not use seq_file */
3166 mutex_lock(&trace_types_lock
);
3168 for_each_tracing_cpu(cpu
) {
3169 if (iter
->buffer_iter
[cpu
])
3170 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
3173 if (iter
->trace
&& iter
->trace
->close
)
3174 iter
->trace
->close(iter
);
3176 if (!iter
->snapshot
)
3177 /* reenable tracing if it was previously enabled */
3178 tracing_start_tr(tr
);
3180 __trace_array_put(tr
);
3182 mutex_unlock(&trace_types_lock
);
3184 mutex_destroy(&iter
->mutex
);
3185 free_cpumask_var(iter
->started
);
3187 kfree(iter
->buffer_iter
);
3188 seq_release_private(inode
, file
);
3193 static int tracing_release_generic_tr(struct inode
*inode
, struct file
*file
)
3195 struct trace_array
*tr
= inode
->i_private
;
3197 trace_array_put(tr
);
3201 static int tracing_single_release_tr(struct inode
*inode
, struct file
*file
)
3203 struct trace_array
*tr
= inode
->i_private
;
3205 trace_array_put(tr
);
3207 return single_release(inode
, file
);
3210 static int tracing_open(struct inode
*inode
, struct file
*file
)
3212 struct trace_array
*tr
= inode
->i_private
;
3213 struct trace_iterator
*iter
;
3216 if (trace_array_get(tr
) < 0)
3219 /* If this file was open for write, then erase contents */
3220 if ((file
->f_mode
& FMODE_WRITE
) && (file
->f_flags
& O_TRUNC
)) {
3221 int cpu
= tracing_get_cpu(inode
);
3223 if (cpu
== RING_BUFFER_ALL_CPUS
)
3224 tracing_reset_online_cpus(&tr
->trace_buffer
);
3226 tracing_reset(&tr
->trace_buffer
, cpu
);
3229 if (file
->f_mode
& FMODE_READ
) {
3230 iter
= __tracing_open(inode
, file
, false);
3232 ret
= PTR_ERR(iter
);
3233 else if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
3234 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
3238 trace_array_put(tr
);
3244 * Some tracers are not suitable for instance buffers.
3245 * A tracer is always available for the global array (toplevel)
3246 * or if it explicitly states that it is.
3249 trace_ok_for_array(struct tracer
*t
, struct trace_array
*tr
)
3251 return (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
) || t
->allow_instances
;
3254 /* Find the next tracer that this trace array may use */
3255 static struct tracer
*
3256 get_tracer_for_array(struct trace_array
*tr
, struct tracer
*t
)
3258 while (t
&& !trace_ok_for_array(t
, tr
))
3265 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3267 struct trace_array
*tr
= m
->private;
3268 struct tracer
*t
= v
;
3273 t
= get_tracer_for_array(tr
, t
->next
);
3278 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
3280 struct trace_array
*tr
= m
->private;
3284 mutex_lock(&trace_types_lock
);
3286 t
= get_tracer_for_array(tr
, trace_types
);
3287 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
3293 static void t_stop(struct seq_file
*m
, void *p
)
3295 mutex_unlock(&trace_types_lock
);
3298 static int t_show(struct seq_file
*m
, void *v
)
3300 struct tracer
*t
= v
;
3305 seq_puts(m
, t
->name
);
3314 static const struct seq_operations show_traces_seq_ops
= {
3321 static int show_traces_open(struct inode
*inode
, struct file
*file
)
3323 struct trace_array
*tr
= inode
->i_private
;
3327 if (tracing_disabled
)
3330 ret
= seq_open(file
, &show_traces_seq_ops
);
3334 m
= file
->private_data
;
3341 tracing_write_stub(struct file
*filp
, const char __user
*ubuf
,
3342 size_t count
, loff_t
*ppos
)
3347 loff_t
tracing_lseek(struct file
*file
, loff_t offset
, int whence
)
3351 if (file
->f_mode
& FMODE_READ
)
3352 ret
= seq_lseek(file
, offset
, whence
);
3354 file
->f_pos
= ret
= 0;
3359 static const struct file_operations tracing_fops
= {
3360 .open
= tracing_open
,
3362 .write
= tracing_write_stub
,
3363 .llseek
= tracing_lseek
,
3364 .release
= tracing_release
,
3367 static const struct file_operations show_traces_fops
= {
3368 .open
= show_traces_open
,
3370 .release
= seq_release
,
3371 .llseek
= seq_lseek
,
3375 * The tracer itself will not take this lock, but still we want
3376 * to provide a consistent cpumask to user-space:
3378 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
3381 * Temporary storage for the character representation of the
3382 * CPU bitmask (and one more byte for the newline):
3384 static char mask_str
[NR_CPUS
+ 1];
3387 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
3388 size_t count
, loff_t
*ppos
)
3390 struct trace_array
*tr
= file_inode(filp
)->i_private
;
3393 mutex_lock(&tracing_cpumask_update_lock
);
3395 len
= snprintf(mask_str
, count
, "%*pb\n",
3396 cpumask_pr_args(tr
->tracing_cpumask
));
3401 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
3404 mutex_unlock(&tracing_cpumask_update_lock
);
3410 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
3411 size_t count
, loff_t
*ppos
)
3413 struct trace_array
*tr
= file_inode(filp
)->i_private
;
3414 cpumask_var_t tracing_cpumask_new
;
3417 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
3420 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
3424 mutex_lock(&tracing_cpumask_update_lock
);
3426 local_irq_disable();
3427 arch_spin_lock(&tr
->max_lock
);
3428 for_each_tracing_cpu(cpu
) {
3430 * Increase/decrease the disabled counter if we are
3431 * about to flip a bit in the cpumask:
3433 if (cpumask_test_cpu(cpu
, tr
->tracing_cpumask
) &&
3434 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
3435 atomic_inc(&per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->disabled
);
3436 ring_buffer_record_disable_cpu(tr
->trace_buffer
.buffer
, cpu
);
3438 if (!cpumask_test_cpu(cpu
, tr
->tracing_cpumask
) &&
3439 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
3440 atomic_dec(&per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->disabled
);
3441 ring_buffer_record_enable_cpu(tr
->trace_buffer
.buffer
, cpu
);
3444 arch_spin_unlock(&tr
->max_lock
);
3447 cpumask_copy(tr
->tracing_cpumask
, tracing_cpumask_new
);
3449 mutex_unlock(&tracing_cpumask_update_lock
);
3450 free_cpumask_var(tracing_cpumask_new
);
3455 free_cpumask_var(tracing_cpumask_new
);
3460 static const struct file_operations tracing_cpumask_fops
= {
3461 .open
= tracing_open_generic_tr
,
3462 .read
= tracing_cpumask_read
,
3463 .write
= tracing_cpumask_write
,
3464 .release
= tracing_release_generic_tr
,
3465 .llseek
= generic_file_llseek
,
3468 static int tracing_trace_options_show(struct seq_file
*m
, void *v
)
3470 struct tracer_opt
*trace_opts
;
3471 struct trace_array
*tr
= m
->private;
3475 mutex_lock(&trace_types_lock
);
3476 tracer_flags
= tr
->current_trace
->flags
->val
;
3477 trace_opts
= tr
->current_trace
->flags
->opts
;
3479 for (i
= 0; trace_options
[i
]; i
++) {
3480 if (trace_flags
& (1 << i
))
3481 seq_printf(m
, "%s\n", trace_options
[i
]);
3483 seq_printf(m
, "no%s\n", trace_options
[i
]);
3486 for (i
= 0; trace_opts
[i
].name
; i
++) {
3487 if (tracer_flags
& trace_opts
[i
].bit
)
3488 seq_printf(m
, "%s\n", trace_opts
[i
].name
);
3490 seq_printf(m
, "no%s\n", trace_opts
[i
].name
);
3492 mutex_unlock(&trace_types_lock
);
3497 static int __set_tracer_option(struct trace_array
*tr
,
3498 struct tracer_flags
*tracer_flags
,
3499 struct tracer_opt
*opts
, int neg
)
3501 struct tracer
*trace
= tr
->current_trace
;
3504 ret
= trace
->set_flag(tr
, tracer_flags
->val
, opts
->bit
, !neg
);
3509 tracer_flags
->val
&= ~opts
->bit
;
3511 tracer_flags
->val
|= opts
->bit
;
3515 /* Try to assign a tracer specific option */
3516 static int set_tracer_option(struct trace_array
*tr
, char *cmp
, int neg
)
3518 struct tracer
*trace
= tr
->current_trace
;
3519 struct tracer_flags
*tracer_flags
= trace
->flags
;
3520 struct tracer_opt
*opts
= NULL
;
3523 for (i
= 0; tracer_flags
->opts
[i
].name
; i
++) {
3524 opts
= &tracer_flags
->opts
[i
];
3526 if (strcmp(cmp
, opts
->name
) == 0)
3527 return __set_tracer_option(tr
, trace
->flags
, opts
, neg
);
3533 /* Some tracers require overwrite to stay enabled */
3534 int trace_keep_overwrite(struct tracer
*tracer
, u32 mask
, int set
)
3536 if (tracer
->enabled
&& (mask
& TRACE_ITER_OVERWRITE
) && !set
)
3542 int set_tracer_flag(struct trace_array
*tr
, unsigned int mask
, int enabled
)
3544 /* do nothing if flag is already set */
3545 if (!!(trace_flags
& mask
) == !!enabled
)
3548 /* Give the tracer a chance to approve the change */
3549 if (tr
->current_trace
->flag_changed
)
3550 if (tr
->current_trace
->flag_changed(tr
, mask
, !!enabled
))
3554 trace_flags
|= mask
;
3556 trace_flags
&= ~mask
;
3558 if (mask
== TRACE_ITER_RECORD_CMD
)
3559 trace_event_enable_cmd_record(enabled
);
3561 if (mask
== TRACE_ITER_OVERWRITE
) {
3562 ring_buffer_change_overwrite(tr
->trace_buffer
.buffer
, enabled
);
3563 #ifdef CONFIG_TRACER_MAX_TRACE
3564 ring_buffer_change_overwrite(tr
->max_buffer
.buffer
, enabled
);
3568 if (mask
== TRACE_ITER_PRINTK
)
3569 trace_printk_start_stop_comm(enabled
);
3574 static int trace_set_options(struct trace_array
*tr
, char *option
)
3581 cmp
= strstrip(option
);
3583 if (strncmp(cmp
, "no", 2) == 0) {
3588 mutex_lock(&trace_types_lock
);
3590 for (i
= 0; trace_options
[i
]; i
++) {
3591 if (strcmp(cmp
, trace_options
[i
]) == 0) {
3592 ret
= set_tracer_flag(tr
, 1 << i
, !neg
);
3597 /* If no option could be set, test the specific tracer options */
3598 if (!trace_options
[i
])
3599 ret
= set_tracer_option(tr
, cmp
, neg
);
3601 mutex_unlock(&trace_types_lock
);
3607 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
3608 size_t cnt
, loff_t
*ppos
)
3610 struct seq_file
*m
= filp
->private_data
;
3611 struct trace_array
*tr
= m
->private;
3615 if (cnt
>= sizeof(buf
))
3618 if (copy_from_user(&buf
, ubuf
, cnt
))
3623 ret
= trace_set_options(tr
, buf
);
3632 static int tracing_trace_options_open(struct inode
*inode
, struct file
*file
)
3634 struct trace_array
*tr
= inode
->i_private
;
3637 if (tracing_disabled
)
3640 if (trace_array_get(tr
) < 0)
3643 ret
= single_open(file
, tracing_trace_options_show
, inode
->i_private
);
3645 trace_array_put(tr
);
3650 static const struct file_operations tracing_iter_fops
= {
3651 .open
= tracing_trace_options_open
,
3653 .llseek
= seq_lseek
,
3654 .release
= tracing_single_release_tr
,
3655 .write
= tracing_trace_options_write
,
3658 static const char readme_msg
[] =
3659 "tracing mini-HOWTO:\n\n"
3660 "# echo 0 > tracing_on : quick way to disable tracing\n"
3661 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3662 " Important files:\n"
3663 " trace\t\t\t- The static contents of the buffer\n"
3664 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3665 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3666 " current_tracer\t- function and latency tracers\n"
3667 " available_tracers\t- list of configured tracers for current_tracer\n"
3668 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3669 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3670 " trace_clock\t\t-change the clock used to order events\n"
3671 " local: Per cpu clock but may not be synced across CPUs\n"
3672 " global: Synced across CPUs but slows tracing down.\n"
3673 " counter: Not a clock, but just an increment\n"
3674 " uptime: Jiffy counter from time of boot\n"
3675 " perf: Same clock that perf events use\n"
3676 #ifdef CONFIG_X86_64
3677 " x86-tsc: TSC cycle counter\n"
3679 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3680 " tracing_cpumask\t- Limit which CPUs to trace\n"
3681 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3682 "\t\t\t Remove sub-buffer with rmdir\n"
3683 " trace_options\t\t- Set format or modify how tracing happens\n"
3684 "\t\t\t Disable an option by adding a suffix 'no' to the\n"
3685 "\t\t\t option name\n"
3686 " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
3687 #ifdef CONFIG_DYNAMIC_FTRACE
3688 "\n available_filter_functions - list of functions that can be filtered on\n"
3689 " set_ftrace_filter\t- echo function name in here to only trace these\n"
3690 "\t\t\t functions\n"
3691 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3692 "\t modules: Can select a group via module\n"
3693 "\t Format: :mod:<module-name>\n"
3694 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
3695 "\t triggers: a command to perform when function is hit\n"
3696 "\t Format: <function>:<trigger>[:count]\n"
3697 "\t trigger: traceon, traceoff\n"
3698 "\t\t enable_event:<system>:<event>\n"
3699 "\t\t disable_event:<system>:<event>\n"
3700 #ifdef CONFIG_STACKTRACE
3703 #ifdef CONFIG_TRACER_SNAPSHOT
3708 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
3709 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
3710 "\t The first one will disable tracing every time do_fault is hit\n"
3711 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
3712 "\t The first time do trap is hit and it disables tracing, the\n"
3713 "\t counter will decrement to 2. If tracing is already disabled,\n"
3714 "\t the counter will not decrement. It only decrements when the\n"
3715 "\t trigger did work\n"
3716 "\t To remove trigger without count:\n"
3717 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
3718 "\t To remove trigger with a count:\n"
3719 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
3720 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
3721 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3722 "\t modules: Can select a group via module command :mod:\n"
3723 "\t Does not accept triggers\n"
3724 #endif /* CONFIG_DYNAMIC_FTRACE */
3725 #ifdef CONFIG_FUNCTION_TRACER
3726 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
3729 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3730 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3731 " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n"
3732 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3734 #ifdef CONFIG_TRACER_SNAPSHOT
3735 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
3736 "\t\t\t snapshot buffer. Read the contents for more\n"
3737 "\t\t\t information\n"
3739 #ifdef CONFIG_STACK_TRACER
3740 " stack_trace\t\t- Shows the max stack trace when active\n"
3741 " stack_max_size\t- Shows current max stack size that was traced\n"
3742 "\t\t\t Write into this file to reset the max size (trigger a\n"
3743 "\t\t\t new trace)\n"
3744 #ifdef CONFIG_DYNAMIC_FTRACE
3745 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
3748 #endif /* CONFIG_STACK_TRACER */
3749 " events/\t\t- Directory containing all trace event subsystems:\n"
3750 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
3751 " events/<system>/\t- Directory containing all trace events for <system>:\n"
3752 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
3754 " filter\t\t- If set, only events passing filter are traced\n"
3755 " events/<system>/<event>/\t- Directory containing control files for\n"
3757 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
3758 " filter\t\t- If set, only events passing filter are traced\n"
3759 " trigger\t\t- If set, a command to perform when event is hit\n"
3760 "\t Format: <trigger>[:count][if <filter>]\n"
3761 "\t trigger: traceon, traceoff\n"
3762 "\t enable_event:<system>:<event>\n"
3763 "\t disable_event:<system>:<event>\n"
3764 #ifdef CONFIG_STACKTRACE
3767 #ifdef CONFIG_TRACER_SNAPSHOT
3770 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
3771 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
3772 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
3773 "\t events/block/block_unplug/trigger\n"
3774 "\t The first disables tracing every time block_unplug is hit.\n"
3775 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
3776 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
3777 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
3778 "\t Like function triggers, the counter is only decremented if it\n"
3779 "\t enabled or disabled tracing.\n"
3780 "\t To remove a trigger without a count:\n"
3781 "\t echo '!<trigger> > <system>/<event>/trigger\n"
3782 "\t To remove a trigger with a count:\n"
3783 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
3784 "\t Filters can be ignored when removing a trigger.\n"
3788 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
3789 size_t cnt
, loff_t
*ppos
)
3791 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
3792 readme_msg
, strlen(readme_msg
));
3795 static const struct file_operations tracing_readme_fops
= {
3796 .open
= tracing_open_generic
,
3797 .read
= tracing_readme_read
,
3798 .llseek
= generic_file_llseek
,
3801 static void *saved_cmdlines_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3803 unsigned int *ptr
= v
;
3805 if (*pos
|| m
->count
)
3810 for (; ptr
< &savedcmd
->map_cmdline_to_pid
[savedcmd
->cmdline_num
];
3812 if (*ptr
== -1 || *ptr
== NO_CMDLINE_MAP
)
3821 static void *saved_cmdlines_start(struct seq_file
*m
, loff_t
*pos
)
3827 arch_spin_lock(&trace_cmdline_lock
);
3829 v
= &savedcmd
->map_cmdline_to_pid
[0];
3831 v
= saved_cmdlines_next(m
, v
, &l
);
3839 static void saved_cmdlines_stop(struct seq_file
*m
, void *v
)
3841 arch_spin_unlock(&trace_cmdline_lock
);
3845 static int saved_cmdlines_show(struct seq_file
*m
, void *v
)
3847 char buf
[TASK_COMM_LEN
];
3848 unsigned int *pid
= v
;
3850 __trace_find_cmdline(*pid
, buf
);
3851 seq_printf(m
, "%d %s\n", *pid
, buf
);
3855 static const struct seq_operations tracing_saved_cmdlines_seq_ops
= {
3856 .start
= saved_cmdlines_start
,
3857 .next
= saved_cmdlines_next
,
3858 .stop
= saved_cmdlines_stop
,
3859 .show
= saved_cmdlines_show
,
3862 static int tracing_saved_cmdlines_open(struct inode
*inode
, struct file
*filp
)
3864 if (tracing_disabled
)
3867 return seq_open(filp
, &tracing_saved_cmdlines_seq_ops
);
3870 static const struct file_operations tracing_saved_cmdlines_fops
= {
3871 .open
= tracing_saved_cmdlines_open
,
3873 .llseek
= seq_lseek
,
3874 .release
= seq_release
,
3878 tracing_saved_cmdlines_size_read(struct file
*filp
, char __user
*ubuf
,
3879 size_t cnt
, loff_t
*ppos
)
3884 arch_spin_lock(&trace_cmdline_lock
);
3885 r
= scnprintf(buf
, sizeof(buf
), "%u\n", savedcmd
->cmdline_num
);
3886 arch_spin_unlock(&trace_cmdline_lock
);
3888 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3891 static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer
*s
)
3893 kfree(s
->saved_cmdlines
);
3894 kfree(s
->map_cmdline_to_pid
);
3898 static int tracing_resize_saved_cmdlines(unsigned int val
)
3900 struct saved_cmdlines_buffer
*s
, *savedcmd_temp
;
3902 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
3906 if (allocate_cmdlines_buffer(val
, s
) < 0) {
3911 arch_spin_lock(&trace_cmdline_lock
);
3912 savedcmd_temp
= savedcmd
;
3914 arch_spin_unlock(&trace_cmdline_lock
);
3915 free_saved_cmdlines_buffer(savedcmd_temp
);
3921 tracing_saved_cmdlines_size_write(struct file
*filp
, const char __user
*ubuf
,
3922 size_t cnt
, loff_t
*ppos
)
3927 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
3931 /* must have at least 1 entry or less than PID_MAX_DEFAULT */
3932 if (!val
|| val
> PID_MAX_DEFAULT
)
3935 ret
= tracing_resize_saved_cmdlines((unsigned int)val
);
3944 static const struct file_operations tracing_saved_cmdlines_size_fops
= {
3945 .open
= tracing_open_generic
,
3946 .read
= tracing_saved_cmdlines_size_read
,
3947 .write
= tracing_saved_cmdlines_size_write
,
3950 #ifdef CONFIG_TRACE_ENUM_MAP_FILE
3951 static union trace_enum_map_item
*
3952 update_enum_map(union trace_enum_map_item
*ptr
)
3954 if (!ptr
->map
.enum_string
) {
3955 if (ptr
->tail
.next
) {
3956 ptr
= ptr
->tail
.next
;
3957 /* Set ptr to the next real item (skip head) */
3965 static void *enum_map_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3967 union trace_enum_map_item
*ptr
= v
;
3970 * Paranoid! If ptr points to end, we don't want to increment past it.
3971 * This really should never happen.
3973 ptr
= update_enum_map(ptr
);
3974 if (WARN_ON_ONCE(!ptr
))
3981 ptr
= update_enum_map(ptr
);
3986 static void *enum_map_start(struct seq_file
*m
, loff_t
*pos
)
3988 union trace_enum_map_item
*v
;
3991 mutex_lock(&trace_enum_mutex
);
3993 v
= trace_enum_maps
;
3997 while (v
&& l
< *pos
) {
3998 v
= enum_map_next(m
, v
, &l
);
4004 static void enum_map_stop(struct seq_file
*m
, void *v
)
4006 mutex_unlock(&trace_enum_mutex
);
4009 static int enum_map_show(struct seq_file
*m
, void *v
)
4011 union trace_enum_map_item
*ptr
= v
;
4013 seq_printf(m
, "%s %ld (%s)\n",
4014 ptr
->map
.enum_string
, ptr
->map
.enum_value
,
4020 static const struct seq_operations tracing_enum_map_seq_ops
= {
4021 .start
= enum_map_start
,
4022 .next
= enum_map_next
,
4023 .stop
= enum_map_stop
,
4024 .show
= enum_map_show
,
4027 static int tracing_enum_map_open(struct inode
*inode
, struct file
*filp
)
4029 if (tracing_disabled
)
4032 return seq_open(filp
, &tracing_enum_map_seq_ops
);
4035 static const struct file_operations tracing_enum_map_fops
= {
4036 .open
= tracing_enum_map_open
,
4038 .llseek
= seq_lseek
,
4039 .release
= seq_release
,
4042 static inline union trace_enum_map_item
*
4043 trace_enum_jmp_to_tail(union trace_enum_map_item
*ptr
)
4045 /* Return tail of array given the head */
4046 return ptr
+ ptr
->head
.length
+ 1;
4050 trace_insert_enum_map_file(struct module
*mod
, struct trace_enum_map
**start
,
4053 struct trace_enum_map
**stop
;
4054 struct trace_enum_map
**map
;
4055 union trace_enum_map_item
*map_array
;
4056 union trace_enum_map_item
*ptr
;
4061 * The trace_enum_maps contains the map plus a head and tail item,
4062 * where the head holds the module and length of array, and the
4063 * tail holds a pointer to the next list.
4065 map_array
= kmalloc(sizeof(*map_array
) * (len
+ 2), GFP_KERNEL
);
4067 pr_warning("Unable to allocate trace enum mapping\n");
4071 mutex_lock(&trace_enum_mutex
);
4073 if (!trace_enum_maps
)
4074 trace_enum_maps
= map_array
;
4076 ptr
= trace_enum_maps
;
4078 ptr
= trace_enum_jmp_to_tail(ptr
);
4079 if (!ptr
->tail
.next
)
4081 ptr
= ptr
->tail
.next
;
4084 ptr
->tail
.next
= map_array
;
4086 map_array
->head
.mod
= mod
;
4087 map_array
->head
.length
= len
;
4090 for (map
= start
; (unsigned long)map
< (unsigned long)stop
; map
++) {
4091 map_array
->map
= **map
;
4094 memset(map_array
, 0, sizeof(*map_array
));
4096 mutex_unlock(&trace_enum_mutex
);
4099 static void trace_create_enum_file(struct dentry
*d_tracer
)
4101 trace_create_file("enum_map", 0444, d_tracer
,
4102 NULL
, &tracing_enum_map_fops
);
4105 #else /* CONFIG_TRACE_ENUM_MAP_FILE */
4106 static inline void trace_create_enum_file(struct dentry
*d_tracer
) { }
4107 static inline void trace_insert_enum_map_file(struct module
*mod
,
4108 struct trace_enum_map
**start
, int len
) { }
4109 #endif /* !CONFIG_TRACE_ENUM_MAP_FILE */
4111 static void trace_insert_enum_map(struct module
*mod
,
4112 struct trace_enum_map
**start
, int len
)
4114 struct trace_enum_map
**map
;
4121 trace_event_enum_update(map
, len
);
4123 trace_insert_enum_map_file(mod
, start
, len
);
4127 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
4128 size_t cnt
, loff_t
*ppos
)
4130 struct trace_array
*tr
= filp
->private_data
;
4131 char buf
[MAX_TRACER_SIZE
+2];
4134 mutex_lock(&trace_types_lock
);
4135 r
= sprintf(buf
, "%s\n", tr
->current_trace
->name
);
4136 mutex_unlock(&trace_types_lock
);
4138 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
4141 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
4143 tracing_reset_online_cpus(&tr
->trace_buffer
);
4147 static void set_buffer_entries(struct trace_buffer
*buf
, unsigned long val
)
4151 for_each_tracing_cpu(cpu
)
4152 per_cpu_ptr(buf
->data
, cpu
)->entries
= val
;
4155 #ifdef CONFIG_TRACER_MAX_TRACE
4156 /* resize @tr's buffer to the size of @size_tr's entries */
4157 static int resize_buffer_duplicate_size(struct trace_buffer
*trace_buf
,
4158 struct trace_buffer
*size_buf
, int cpu_id
)
4162 if (cpu_id
== RING_BUFFER_ALL_CPUS
) {
4163 for_each_tracing_cpu(cpu
) {
4164 ret
= ring_buffer_resize(trace_buf
->buffer
,
4165 per_cpu_ptr(size_buf
->data
, cpu
)->entries
, cpu
);
4168 per_cpu_ptr(trace_buf
->data
, cpu
)->entries
=
4169 per_cpu_ptr(size_buf
->data
, cpu
)->entries
;
4172 ret
= ring_buffer_resize(trace_buf
->buffer
,
4173 per_cpu_ptr(size_buf
->data
, cpu_id
)->entries
, cpu_id
);
4175 per_cpu_ptr(trace_buf
->data
, cpu_id
)->entries
=
4176 per_cpu_ptr(size_buf
->data
, cpu_id
)->entries
;
4181 #endif /* CONFIG_TRACER_MAX_TRACE */
4183 static int __tracing_resize_ring_buffer(struct trace_array
*tr
,
4184 unsigned long size
, int cpu
)
4189 * If kernel or user changes the size of the ring buffer
4190 * we use the size that was given, and we can forget about
4191 * expanding it later.
4193 ring_buffer_expanded
= true;
4195 /* May be called before buffers are initialized */
4196 if (!tr
->trace_buffer
.buffer
)
4199 ret
= ring_buffer_resize(tr
->trace_buffer
.buffer
, size
, cpu
);
4203 #ifdef CONFIG_TRACER_MAX_TRACE
4204 if (!(tr
->flags
& TRACE_ARRAY_FL_GLOBAL
) ||
4205 !tr
->current_trace
->use_max_tr
)
4208 ret
= ring_buffer_resize(tr
->max_buffer
.buffer
, size
, cpu
);
4210 int r
= resize_buffer_duplicate_size(&tr
->trace_buffer
,
4211 &tr
->trace_buffer
, cpu
);
4214 * AARGH! We are left with different
4215 * size max buffer!!!!
4216 * The max buffer is our "snapshot" buffer.
4217 * When a tracer needs a snapshot (one of the
4218 * latency tracers), it swaps the max buffer
4219 * with the saved snap shot. We succeeded to
4220 * update the size of the main buffer, but failed to
4221 * update the size of the max buffer. But when we tried
4222 * to reset the main buffer to the original size, we
4223 * failed there too. This is very unlikely to
4224 * happen, but if it does, warn and kill all
4228 tracing_disabled
= 1;
4233 if (cpu
== RING_BUFFER_ALL_CPUS
)
4234 set_buffer_entries(&tr
->max_buffer
, size
);
4236 per_cpu_ptr(tr
->max_buffer
.data
, cpu
)->entries
= size
;
4239 #endif /* CONFIG_TRACER_MAX_TRACE */
4241 if (cpu
== RING_BUFFER_ALL_CPUS
)
4242 set_buffer_entries(&tr
->trace_buffer
, size
);
4244 per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->entries
= size
;
4249 static ssize_t
tracing_resize_ring_buffer(struct trace_array
*tr
,
4250 unsigned long size
, int cpu_id
)
4254 mutex_lock(&trace_types_lock
);
4256 if (cpu_id
!= RING_BUFFER_ALL_CPUS
) {
4257 /* make sure, this cpu is enabled in the mask */
4258 if (!cpumask_test_cpu(cpu_id
, tracing_buffer_mask
)) {
4264 ret
= __tracing_resize_ring_buffer(tr
, size
, cpu_id
);
4269 mutex_unlock(&trace_types_lock
);
4276 * tracing_update_buffers - used by tracing facility to expand ring buffers
4278 * To save on memory when the tracing is never used on a system with it
4279 * configured in. The ring buffers are set to a minimum size. But once
4280 * a user starts to use the tracing facility, then they need to grow
4281 * to their default size.
4283 * This function is to be called when a tracer is about to be used.
4285 int tracing_update_buffers(void)
4289 mutex_lock(&trace_types_lock
);
4290 if (!ring_buffer_expanded
)
4291 ret
= __tracing_resize_ring_buffer(&global_trace
, trace_buf_size
,
4292 RING_BUFFER_ALL_CPUS
);
4293 mutex_unlock(&trace_types_lock
);
4298 struct trace_option_dentry
;
4300 static struct trace_option_dentry
*
4301 create_trace_option_files(struct trace_array
*tr
, struct tracer
*tracer
);
4304 destroy_trace_option_files(struct trace_option_dentry
*topts
);
4307 * Used to clear out the tracer before deletion of an instance.
4308 * Must have trace_types_lock held.
4310 static void tracing_set_nop(struct trace_array
*tr
)
4312 if (tr
->current_trace
== &nop_trace
)
4315 tr
->current_trace
->enabled
--;
4317 if (tr
->current_trace
->reset
)
4318 tr
->current_trace
->reset(tr
);
4320 tr
->current_trace
= &nop_trace
;
4323 static void update_tracer_options(struct trace_array
*tr
, struct tracer
*t
)
4325 static struct trace_option_dentry
*topts
;
4327 /* Only enable if the directory has been created already. */
4331 /* Currently, only the top instance has options */
4332 if (!(tr
->flags
& TRACE_ARRAY_FL_GLOBAL
))
4335 destroy_trace_option_files(topts
);
4336 topts
= create_trace_option_files(tr
, t
);
4339 static int tracing_set_tracer(struct trace_array
*tr
, const char *buf
)
4342 #ifdef CONFIG_TRACER_MAX_TRACE
4347 mutex_lock(&trace_types_lock
);
4349 if (!ring_buffer_expanded
) {
4350 ret
= __tracing_resize_ring_buffer(tr
, trace_buf_size
,
4351 RING_BUFFER_ALL_CPUS
);
4357 for (t
= trace_types
; t
; t
= t
->next
) {
4358 if (strcmp(t
->name
, buf
) == 0)
4365 if (t
== tr
->current_trace
)
4368 /* Some tracers are only allowed for the top level buffer */
4369 if (!trace_ok_for_array(t
, tr
)) {
4374 /* If trace pipe files are being read, we can't change the tracer */
4375 if (tr
->current_trace
->ref
) {
4380 trace_branch_disable();
4382 tr
->current_trace
->enabled
--;
4384 if (tr
->current_trace
->reset
)
4385 tr
->current_trace
->reset(tr
);
4387 /* Current trace needs to be nop_trace before synchronize_sched */
4388 tr
->current_trace
= &nop_trace
;
4390 #ifdef CONFIG_TRACER_MAX_TRACE
4391 had_max_tr
= tr
->allocated_snapshot
;
4393 if (had_max_tr
&& !t
->use_max_tr
) {
4395 * We need to make sure that the update_max_tr sees that
4396 * current_trace changed to nop_trace to keep it from
4397 * swapping the buffers after we resize it.
4398 * The update_max_tr is called from interrupts disabled
4399 * so a synchronized_sched() is sufficient.
4401 synchronize_sched();
4405 update_tracer_options(tr
, t
);
4407 #ifdef CONFIG_TRACER_MAX_TRACE
4408 if (t
->use_max_tr
&& !had_max_tr
) {
4409 ret
= alloc_snapshot(tr
);
4416 ret
= tracer_init(t
, tr
);
4421 tr
->current_trace
= t
;
4422 tr
->current_trace
->enabled
++;
4423 trace_branch_enable(tr
);
4425 mutex_unlock(&trace_types_lock
);
4431 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
4432 size_t cnt
, loff_t
*ppos
)
4434 struct trace_array
*tr
= filp
->private_data
;
4435 char buf
[MAX_TRACER_SIZE
+1];
4442 if (cnt
> MAX_TRACER_SIZE
)
4443 cnt
= MAX_TRACER_SIZE
;
4445 if (copy_from_user(&buf
, ubuf
, cnt
))
4450 /* strip ending whitespace. */
4451 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
4454 err
= tracing_set_tracer(tr
, buf
);
4464 tracing_nsecs_read(unsigned long *ptr
, char __user
*ubuf
,
4465 size_t cnt
, loff_t
*ppos
)
4470 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
4471 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
4472 if (r
> sizeof(buf
))
4474 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
4478 tracing_nsecs_write(unsigned long *ptr
, const char __user
*ubuf
,
4479 size_t cnt
, loff_t
*ppos
)
4484 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
4494 tracing_thresh_read(struct file
*filp
, char __user
*ubuf
,
4495 size_t cnt
, loff_t
*ppos
)
4497 return tracing_nsecs_read(&tracing_thresh
, ubuf
, cnt
, ppos
);
4501 tracing_thresh_write(struct file
*filp
, const char __user
*ubuf
,
4502 size_t cnt
, loff_t
*ppos
)
4504 struct trace_array
*tr
= filp
->private_data
;
4507 mutex_lock(&trace_types_lock
);
4508 ret
= tracing_nsecs_write(&tracing_thresh
, ubuf
, cnt
, ppos
);
4512 if (tr
->current_trace
->update_thresh
) {
4513 ret
= tr
->current_trace
->update_thresh(tr
);
4520 mutex_unlock(&trace_types_lock
);
4526 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
4527 size_t cnt
, loff_t
*ppos
)
4529 return tracing_nsecs_read(filp
->private_data
, ubuf
, cnt
, ppos
);
4533 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
4534 size_t cnt
, loff_t
*ppos
)
4536 return tracing_nsecs_write(filp
->private_data
, ubuf
, cnt
, ppos
);
4539 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
4541 struct trace_array
*tr
= inode
->i_private
;
4542 struct trace_iterator
*iter
;
4545 if (tracing_disabled
)
4548 if (trace_array_get(tr
) < 0)
4551 mutex_lock(&trace_types_lock
);
4553 /* create a buffer to store the information to pass to userspace */
4554 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
4557 __trace_array_put(tr
);
4561 trace_seq_init(&iter
->seq
);
4562 iter
->trace
= tr
->current_trace
;
4564 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
4569 /* trace pipe does not show start of buffer */
4570 cpumask_setall(iter
->started
);
4572 if (trace_flags
& TRACE_ITER_LATENCY_FMT
)
4573 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
4575 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
4576 if (trace_clocks
[tr
->clock_id
].in_ns
)
4577 iter
->iter_flags
|= TRACE_FILE_TIME_IN_NS
;
4580 iter
->trace_buffer
= &tr
->trace_buffer
;
4581 iter
->cpu_file
= tracing_get_cpu(inode
);
4582 mutex_init(&iter
->mutex
);
4583 filp
->private_data
= iter
;
4585 if (iter
->trace
->pipe_open
)
4586 iter
->trace
->pipe_open(iter
);
4588 nonseekable_open(inode
, filp
);
4590 tr
->current_trace
->ref
++;
4592 mutex_unlock(&trace_types_lock
);
4598 __trace_array_put(tr
);
4599 mutex_unlock(&trace_types_lock
);
4603 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
4605 struct trace_iterator
*iter
= file
->private_data
;
4606 struct trace_array
*tr
= inode
->i_private
;
4608 mutex_lock(&trace_types_lock
);
4610 tr
->current_trace
->ref
--;
4612 if (iter
->trace
->pipe_close
)
4613 iter
->trace
->pipe_close(iter
);
4615 mutex_unlock(&trace_types_lock
);
4617 free_cpumask_var(iter
->started
);
4618 mutex_destroy(&iter
->mutex
);
4621 trace_array_put(tr
);
4627 trace_poll(struct trace_iterator
*iter
, struct file
*filp
, poll_table
*poll_table
)
4629 /* Iterators are static, they should be filled or empty */
4630 if (trace_buffer_iter(iter
, iter
->cpu_file
))
4631 return POLLIN
| POLLRDNORM
;
4633 if (trace_flags
& TRACE_ITER_BLOCK
)
4635 * Always select as readable when in blocking mode
4637 return POLLIN
| POLLRDNORM
;
4639 return ring_buffer_poll_wait(iter
->trace_buffer
->buffer
, iter
->cpu_file
,
4644 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
4646 struct trace_iterator
*iter
= filp
->private_data
;
4648 return trace_poll(iter
, filp
, poll_table
);
4651 /* Must be called with iter->mutex held. */
4652 static int tracing_wait_pipe(struct file
*filp
)
4654 struct trace_iterator
*iter
= filp
->private_data
;
4657 while (trace_empty(iter
)) {
4659 if ((filp
->f_flags
& O_NONBLOCK
)) {
4664 * We block until we read something and tracing is disabled.
4665 * We still block if tracing is disabled, but we have never
4666 * read anything. This allows a user to cat this file, and
4667 * then enable tracing. But after we have read something,
4668 * we give an EOF when tracing is again disabled.
4670 * iter->pos will be 0 if we haven't read anything.
4672 if (!tracing_is_on() && iter
->pos
)
4675 mutex_unlock(&iter
->mutex
);
4677 ret
= wait_on_pipe(iter
, false);
4679 mutex_lock(&iter
->mutex
);
4692 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
4693 size_t cnt
, loff_t
*ppos
)
4695 struct trace_iterator
*iter
= filp
->private_data
;
4698 /* return any leftover data */
4699 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
4703 trace_seq_init(&iter
->seq
);
4706 * Avoid more than one consumer on a single file descriptor
4707 * This is just a matter of traces coherency, the ring buffer itself
4710 mutex_lock(&iter
->mutex
);
4711 if (iter
->trace
->read
) {
4712 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
4718 sret
= tracing_wait_pipe(filp
);
4722 /* stop when tracing is finished */
4723 if (trace_empty(iter
)) {
4728 if (cnt
>= PAGE_SIZE
)
4729 cnt
= PAGE_SIZE
- 1;
4731 /* reset all but tr, trace, and overruns */
4732 memset(&iter
->seq
, 0,
4733 sizeof(struct trace_iterator
) -
4734 offsetof(struct trace_iterator
, seq
));
4735 cpumask_clear(iter
->started
);
4738 trace_event_read_lock();
4739 trace_access_lock(iter
->cpu_file
);
4740 while (trace_find_next_entry_inc(iter
) != NULL
) {
4741 enum print_line_t ret
;
4742 int save_len
= iter
->seq
.seq
.len
;
4744 ret
= print_trace_line(iter
);
4745 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
4746 /* don't print partial lines */
4747 iter
->seq
.seq
.len
= save_len
;
4750 if (ret
!= TRACE_TYPE_NO_CONSUME
)
4751 trace_consume(iter
);
4753 if (trace_seq_used(&iter
->seq
) >= cnt
)
4757 * Setting the full flag means we reached the trace_seq buffer
4758 * size and we should leave by partial output condition above.
4759 * One of the trace_seq_* functions is not used properly.
4761 WARN_ONCE(iter
->seq
.full
, "full flag set for trace type %d",
4764 trace_access_unlock(iter
->cpu_file
);
4765 trace_event_read_unlock();
4767 /* Now copy what we have to the user */
4768 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
4769 if (iter
->seq
.seq
.readpos
>= trace_seq_used(&iter
->seq
))
4770 trace_seq_init(&iter
->seq
);
4773 * If there was nothing to send to user, in spite of consuming trace
4774 * entries, go back to wait for more entries.
4780 mutex_unlock(&iter
->mutex
);
4785 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
4788 __free_page(spd
->pages
[idx
]);
4791 static const struct pipe_buf_operations tracing_pipe_buf_ops
= {
4793 .confirm
= generic_pipe_buf_confirm
,
4794 .release
= generic_pipe_buf_release
,
4795 .steal
= generic_pipe_buf_steal
,
4796 .get
= generic_pipe_buf_get
,
4800 tracing_fill_pipe_page(size_t rem
, struct trace_iterator
*iter
)
4806 /* Seq buffer is page-sized, exactly what we need. */
4808 save_len
= iter
->seq
.seq
.len
;
4809 ret
= print_trace_line(iter
);
4811 if (trace_seq_has_overflowed(&iter
->seq
)) {
4812 iter
->seq
.seq
.len
= save_len
;
4817 * This should not be hit, because it should only
4818 * be set if the iter->seq overflowed. But check it
4819 * anyway to be safe.
4821 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
4822 iter
->seq
.seq
.len
= save_len
;
4826 count
= trace_seq_used(&iter
->seq
) - save_len
;
4829 iter
->seq
.seq
.len
= save_len
;
4833 if (ret
!= TRACE_TYPE_NO_CONSUME
)
4834 trace_consume(iter
);
4836 if (!trace_find_next_entry_inc(iter
)) {
4846 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
4848 struct pipe_inode_info
*pipe
,
4852 struct page
*pages_def
[PIPE_DEF_BUFFERS
];
4853 struct partial_page partial_def
[PIPE_DEF_BUFFERS
];
4854 struct trace_iterator
*iter
= filp
->private_data
;
4855 struct splice_pipe_desc spd
= {
4857 .partial
= partial_def
,
4858 .nr_pages
= 0, /* This gets updated below. */
4859 .nr_pages_max
= PIPE_DEF_BUFFERS
,
4861 .ops
= &tracing_pipe_buf_ops
,
4862 .spd_release
= tracing_spd_release_pipe
,
4868 if (splice_grow_spd(pipe
, &spd
))
4871 mutex_lock(&iter
->mutex
);
4873 if (iter
->trace
->splice_read
) {
4874 ret
= iter
->trace
->splice_read(iter
, filp
,
4875 ppos
, pipe
, len
, flags
);
4880 ret
= tracing_wait_pipe(filp
);
4884 if (!iter
->ent
&& !trace_find_next_entry_inc(iter
)) {
4889 trace_event_read_lock();
4890 trace_access_lock(iter
->cpu_file
);
4892 /* Fill as many pages as possible. */
4893 for (i
= 0, rem
= len
; i
< spd
.nr_pages_max
&& rem
; i
++) {
4894 spd
.pages
[i
] = alloc_page(GFP_KERNEL
);
4898 rem
= tracing_fill_pipe_page(rem
, iter
);
4900 /* Copy the data into the page, so we can start over. */
4901 ret
= trace_seq_to_buffer(&iter
->seq
,
4902 page_address(spd
.pages
[i
]),
4903 trace_seq_used(&iter
->seq
));
4905 __free_page(spd
.pages
[i
]);
4908 spd
.partial
[i
].offset
= 0;
4909 spd
.partial
[i
].len
= trace_seq_used(&iter
->seq
);
4911 trace_seq_init(&iter
->seq
);
4914 trace_access_unlock(iter
->cpu_file
);
4915 trace_event_read_unlock();
4916 mutex_unlock(&iter
->mutex
);
4920 ret
= splice_to_pipe(pipe
, &spd
);
4922 splice_shrink_spd(&spd
);
4926 mutex_unlock(&iter
->mutex
);
4931 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
4932 size_t cnt
, loff_t
*ppos
)
4934 struct inode
*inode
= file_inode(filp
);
4935 struct trace_array
*tr
= inode
->i_private
;
4936 int cpu
= tracing_get_cpu(inode
);
4941 mutex_lock(&trace_types_lock
);
4943 if (cpu
== RING_BUFFER_ALL_CPUS
) {
4944 int cpu
, buf_size_same
;
4949 /* check if all cpu sizes are same */
4950 for_each_tracing_cpu(cpu
) {
4951 /* fill in the size from first enabled cpu */
4953 size
= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->entries
;
4954 if (size
!= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->entries
) {
4960 if (buf_size_same
) {
4961 if (!ring_buffer_expanded
)
4962 r
= sprintf(buf
, "%lu (expanded: %lu)\n",
4964 trace_buf_size
>> 10);
4966 r
= sprintf(buf
, "%lu\n", size
>> 10);
4968 r
= sprintf(buf
, "X\n");
4970 r
= sprintf(buf
, "%lu\n", per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->entries
>> 10);
4972 mutex_unlock(&trace_types_lock
);
4974 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
4979 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
4980 size_t cnt
, loff_t
*ppos
)
4982 struct inode
*inode
= file_inode(filp
);
4983 struct trace_array
*tr
= inode
->i_private
;
4987 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
4991 /* must have at least 1 entry */
4995 /* value is in KB */
4997 ret
= tracing_resize_ring_buffer(tr
, val
, tracing_get_cpu(inode
));
5007 tracing_total_entries_read(struct file
*filp
, char __user
*ubuf
,
5008 size_t cnt
, loff_t
*ppos
)
5010 struct trace_array
*tr
= filp
->private_data
;
5013 unsigned long size
= 0, expanded_size
= 0;
5015 mutex_lock(&trace_types_lock
);
5016 for_each_tracing_cpu(cpu
) {
5017 size
+= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->entries
>> 10;
5018 if (!ring_buffer_expanded
)
5019 expanded_size
+= trace_buf_size
>> 10;
5021 if (ring_buffer_expanded
)
5022 r
= sprintf(buf
, "%lu\n", size
);
5024 r
= sprintf(buf
, "%lu (expanded: %lu)\n", size
, expanded_size
);
5025 mutex_unlock(&trace_types_lock
);
5027 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
5031 tracing_free_buffer_write(struct file
*filp
, const char __user
*ubuf
,
5032 size_t cnt
, loff_t
*ppos
)
5035 * There is no need to read what the user has written, this function
5036 * is just to make sure that there is no error when "echo" is used
5045 tracing_free_buffer_release(struct inode
*inode
, struct file
*filp
)
5047 struct trace_array
*tr
= inode
->i_private
;
5049 /* disable tracing ? */
5050 if (trace_flags
& TRACE_ITER_STOP_ON_FREE
)
5051 tracer_tracing_off(tr
);
5052 /* resize the ring buffer to 0 */
5053 tracing_resize_ring_buffer(tr
, 0, RING_BUFFER_ALL_CPUS
);
5055 trace_array_put(tr
);
5061 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
5062 size_t cnt
, loff_t
*fpos
)
5064 unsigned long addr
= (unsigned long)ubuf
;
5065 struct trace_array
*tr
= filp
->private_data
;
5066 struct ring_buffer_event
*event
;
5067 struct ring_buffer
*buffer
;
5068 struct print_entry
*entry
;
5069 unsigned long irq_flags
;
5070 struct page
*pages
[2];
5080 if (tracing_disabled
)
5083 if (!(trace_flags
& TRACE_ITER_MARKERS
))
5086 if (cnt
> TRACE_BUF_SIZE
)
5087 cnt
= TRACE_BUF_SIZE
;
5090 * Userspace is injecting traces into the kernel trace buffer.
5091 * We want to be as non intrusive as possible.
5092 * To do so, we do not want to allocate any special buffers
5093 * or take any locks, but instead write the userspace data
5094 * straight into the ring buffer.
5096 * First we need to pin the userspace buffer into memory,
5097 * which, most likely it is, because it just referenced it.
5098 * But there's no guarantee that it is. By using get_user_pages_fast()
5099 * and kmap_atomic/kunmap_atomic() we can get access to the
5100 * pages directly. We then write the data directly into the
5103 BUILD_BUG_ON(TRACE_BUF_SIZE
>= PAGE_SIZE
);
5105 /* check if we cross pages */
5106 if ((addr
& PAGE_MASK
) != ((addr
+ cnt
) & PAGE_MASK
))
5109 offset
= addr
& (PAGE_SIZE
- 1);
5112 ret
= get_user_pages_fast(addr
, nr_pages
, 0, pages
);
5113 if (ret
< nr_pages
) {
5115 put_page(pages
[ret
]);
5120 for (i
= 0; i
< nr_pages
; i
++)
5121 map_page
[i
] = kmap_atomic(pages
[i
]);
5123 local_save_flags(irq_flags
);
5124 size
= sizeof(*entry
) + cnt
+ 2; /* possible \n added */
5125 buffer
= tr
->trace_buffer
.buffer
;
5126 event
= trace_buffer_lock_reserve(buffer
, TRACE_PRINT
, size
,
5127 irq_flags
, preempt_count());
5129 /* Ring buffer disabled, return as if not open for write */
5134 entry
= ring_buffer_event_data(event
);
5135 entry
->ip
= _THIS_IP_
;
5137 if (nr_pages
== 2) {
5138 len
= PAGE_SIZE
- offset
;
5139 memcpy(&entry
->buf
, map_page
[0] + offset
, len
);
5140 memcpy(&entry
->buf
[len
], map_page
[1], cnt
- len
);
5142 memcpy(&entry
->buf
, map_page
[0] + offset
, cnt
);
5144 if (entry
->buf
[cnt
- 1] != '\n') {
5145 entry
->buf
[cnt
] = '\n';
5146 entry
->buf
[cnt
+ 1] = '\0';
5148 entry
->buf
[cnt
] = '\0';
5150 __buffer_unlock_commit(buffer
, event
);
5157 for (i
= nr_pages
- 1; i
>= 0; i
--) {
5158 kunmap_atomic(map_page
[i
]);
5165 static int tracing_clock_show(struct seq_file
*m
, void *v
)
5167 struct trace_array
*tr
= m
->private;
5170 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++)
5172 "%s%s%s%s", i
? " " : "",
5173 i
== tr
->clock_id
? "[" : "", trace_clocks
[i
].name
,
5174 i
== tr
->clock_id
? "]" : "");
5180 static int tracing_set_clock(struct trace_array
*tr
, const char *clockstr
)
5184 for (i
= 0; i
< ARRAY_SIZE(trace_clocks
); i
++) {
5185 if (strcmp(trace_clocks
[i
].name
, clockstr
) == 0)
5188 if (i
== ARRAY_SIZE(trace_clocks
))
5191 mutex_lock(&trace_types_lock
);
5195 ring_buffer_set_clock(tr
->trace_buffer
.buffer
, trace_clocks
[i
].func
);
5198 * New clock may not be consistent with the previous clock.
5199 * Reset the buffer so that it doesn't have incomparable timestamps.
5201 tracing_reset_online_cpus(&tr
->trace_buffer
);
5203 #ifdef CONFIG_TRACER_MAX_TRACE
5204 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
&& tr
->max_buffer
.buffer
)
5205 ring_buffer_set_clock(tr
->max_buffer
.buffer
, trace_clocks
[i
].func
);
5206 tracing_reset_online_cpus(&tr
->max_buffer
);
5209 mutex_unlock(&trace_types_lock
);
5214 static ssize_t
tracing_clock_write(struct file
*filp
, const char __user
*ubuf
,
5215 size_t cnt
, loff_t
*fpos
)
5217 struct seq_file
*m
= filp
->private_data
;
5218 struct trace_array
*tr
= m
->private;
5220 const char *clockstr
;
5223 if (cnt
>= sizeof(buf
))
5226 if (copy_from_user(&buf
, ubuf
, cnt
))
5231 clockstr
= strstrip(buf
);
5233 ret
= tracing_set_clock(tr
, clockstr
);
5242 static int tracing_clock_open(struct inode
*inode
, struct file
*file
)
5244 struct trace_array
*tr
= inode
->i_private
;
5247 if (tracing_disabled
)
5250 if (trace_array_get(tr
))
5253 ret
= single_open(file
, tracing_clock_show
, inode
->i_private
);
5255 trace_array_put(tr
);
5260 struct ftrace_buffer_info
{
5261 struct trace_iterator iter
;
5266 #ifdef CONFIG_TRACER_SNAPSHOT
5267 static int tracing_snapshot_open(struct inode
*inode
, struct file
*file
)
5269 struct trace_array
*tr
= inode
->i_private
;
5270 struct trace_iterator
*iter
;
5274 if (trace_array_get(tr
) < 0)
5277 if (file
->f_mode
& FMODE_READ
) {
5278 iter
= __tracing_open(inode
, file
, true);
5280 ret
= PTR_ERR(iter
);
5282 /* Writes still need the seq_file to hold the private data */
5284 m
= kzalloc(sizeof(*m
), GFP_KERNEL
);
5287 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
5295 iter
->trace_buffer
= &tr
->max_buffer
;
5296 iter
->cpu_file
= tracing_get_cpu(inode
);
5298 file
->private_data
= m
;
5302 trace_array_put(tr
);
5308 tracing_snapshot_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
5311 struct seq_file
*m
= filp
->private_data
;
5312 struct trace_iterator
*iter
= m
->private;
5313 struct trace_array
*tr
= iter
->tr
;
5317 ret
= tracing_update_buffers();
5321 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
5325 mutex_lock(&trace_types_lock
);
5327 if (tr
->current_trace
->use_max_tr
) {
5334 if (iter
->cpu_file
!= RING_BUFFER_ALL_CPUS
) {
5338 if (tr
->allocated_snapshot
)
5342 /* Only allow per-cpu swap if the ring buffer supports it */
5343 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
5344 if (iter
->cpu_file
!= RING_BUFFER_ALL_CPUS
) {
5349 if (!tr
->allocated_snapshot
) {
5350 ret
= alloc_snapshot(tr
);
5354 local_irq_disable();
5355 /* Now, we're going to swap */
5356 if (iter
->cpu_file
== RING_BUFFER_ALL_CPUS
)
5357 update_max_tr(tr
, current
, smp_processor_id());
5359 update_max_tr_single(tr
, current
, iter
->cpu_file
);
5363 if (tr
->allocated_snapshot
) {
5364 if (iter
->cpu_file
== RING_BUFFER_ALL_CPUS
)
5365 tracing_reset_online_cpus(&tr
->max_buffer
);
5367 tracing_reset(&tr
->max_buffer
, iter
->cpu_file
);
5377 mutex_unlock(&trace_types_lock
);
5381 static int tracing_snapshot_release(struct inode
*inode
, struct file
*file
)
5383 struct seq_file
*m
= file
->private_data
;
5386 ret
= tracing_release(inode
, file
);
5388 if (file
->f_mode
& FMODE_READ
)
5391 /* If write only, the seq_file is just a stub */
5399 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
);
5400 static ssize_t
tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
5401 size_t count
, loff_t
*ppos
);
5402 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
);
5403 static ssize_t
tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
5404 struct pipe_inode_info
*pipe
, size_t len
, unsigned int flags
);
5406 static int snapshot_raw_open(struct inode
*inode
, struct file
*filp
)
5408 struct ftrace_buffer_info
*info
;
5411 ret
= tracing_buffers_open(inode
, filp
);
5415 info
= filp
->private_data
;
5417 if (info
->iter
.trace
->use_max_tr
) {
5418 tracing_buffers_release(inode
, filp
);
5422 info
->iter
.snapshot
= true;
5423 info
->iter
.trace_buffer
= &info
->iter
.tr
->max_buffer
;
5428 #endif /* CONFIG_TRACER_SNAPSHOT */
5431 static const struct file_operations tracing_thresh_fops
= {
5432 .open
= tracing_open_generic
,
5433 .read
= tracing_thresh_read
,
5434 .write
= tracing_thresh_write
,
5435 .llseek
= generic_file_llseek
,
5438 static const struct file_operations tracing_max_lat_fops
= {
5439 .open
= tracing_open_generic
,
5440 .read
= tracing_max_lat_read
,
5441 .write
= tracing_max_lat_write
,
5442 .llseek
= generic_file_llseek
,
5445 static const struct file_operations set_tracer_fops
= {
5446 .open
= tracing_open_generic
,
5447 .read
= tracing_set_trace_read
,
5448 .write
= tracing_set_trace_write
,
5449 .llseek
= generic_file_llseek
,
5452 static const struct file_operations tracing_pipe_fops
= {
5453 .open
= tracing_open_pipe
,
5454 .poll
= tracing_poll_pipe
,
5455 .read
= tracing_read_pipe
,
5456 .splice_read
= tracing_splice_read_pipe
,
5457 .release
= tracing_release_pipe
,
5458 .llseek
= no_llseek
,
5461 static const struct file_operations tracing_entries_fops
= {
5462 .open
= tracing_open_generic_tr
,
5463 .read
= tracing_entries_read
,
5464 .write
= tracing_entries_write
,
5465 .llseek
= generic_file_llseek
,
5466 .release
= tracing_release_generic_tr
,
5469 static const struct file_operations tracing_total_entries_fops
= {
5470 .open
= tracing_open_generic_tr
,
5471 .read
= tracing_total_entries_read
,
5472 .llseek
= generic_file_llseek
,
5473 .release
= tracing_release_generic_tr
,
5476 static const struct file_operations tracing_free_buffer_fops
= {
5477 .open
= tracing_open_generic_tr
,
5478 .write
= tracing_free_buffer_write
,
5479 .release
= tracing_free_buffer_release
,
5482 static const struct file_operations tracing_mark_fops
= {
5483 .open
= tracing_open_generic_tr
,
5484 .write
= tracing_mark_write
,
5485 .llseek
= generic_file_llseek
,
5486 .release
= tracing_release_generic_tr
,
5489 static const struct file_operations trace_clock_fops
= {
5490 .open
= tracing_clock_open
,
5492 .llseek
= seq_lseek
,
5493 .release
= tracing_single_release_tr
,
5494 .write
= tracing_clock_write
,
5497 #ifdef CONFIG_TRACER_SNAPSHOT
5498 static const struct file_operations snapshot_fops
= {
5499 .open
= tracing_snapshot_open
,
5501 .write
= tracing_snapshot_write
,
5502 .llseek
= tracing_lseek
,
5503 .release
= tracing_snapshot_release
,
5506 static const struct file_operations snapshot_raw_fops
= {
5507 .open
= snapshot_raw_open
,
5508 .read
= tracing_buffers_read
,
5509 .release
= tracing_buffers_release
,
5510 .splice_read
= tracing_buffers_splice_read
,
5511 .llseek
= no_llseek
,
5514 #endif /* CONFIG_TRACER_SNAPSHOT */
5516 static int tracing_buffers_open(struct inode
*inode
, struct file
*filp
)
5518 struct trace_array
*tr
= inode
->i_private
;
5519 struct ftrace_buffer_info
*info
;
5522 if (tracing_disabled
)
5525 if (trace_array_get(tr
) < 0)
5528 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
5530 trace_array_put(tr
);
5534 mutex_lock(&trace_types_lock
);
5537 info
->iter
.cpu_file
= tracing_get_cpu(inode
);
5538 info
->iter
.trace
= tr
->current_trace
;
5539 info
->iter
.trace_buffer
= &tr
->trace_buffer
;
5541 /* Force reading ring buffer for first read */
5542 info
->read
= (unsigned int)-1;
5544 filp
->private_data
= info
;
5546 tr
->current_trace
->ref
++;
5548 mutex_unlock(&trace_types_lock
);
5550 ret
= nonseekable_open(inode
, filp
);
5552 trace_array_put(tr
);
5558 tracing_buffers_poll(struct file
*filp
, poll_table
*poll_table
)
5560 struct ftrace_buffer_info
*info
= filp
->private_data
;
5561 struct trace_iterator
*iter
= &info
->iter
;
5563 return trace_poll(iter
, filp
, poll_table
);
5567 tracing_buffers_read(struct file
*filp
, char __user
*ubuf
,
5568 size_t count
, loff_t
*ppos
)
5570 struct ftrace_buffer_info
*info
= filp
->private_data
;
5571 struct trace_iterator
*iter
= &info
->iter
;
5578 #ifdef CONFIG_TRACER_MAX_TRACE
5579 if (iter
->snapshot
&& iter
->tr
->current_trace
->use_max_tr
)
5584 info
->spare
= ring_buffer_alloc_read_page(iter
->trace_buffer
->buffer
,
5589 /* Do we have previous read data to read? */
5590 if (info
->read
< PAGE_SIZE
)
5594 trace_access_lock(iter
->cpu_file
);
5595 ret
= ring_buffer_read_page(iter
->trace_buffer
->buffer
,
5599 trace_access_unlock(iter
->cpu_file
);
5602 if (trace_empty(iter
)) {
5603 if ((filp
->f_flags
& O_NONBLOCK
))
5606 ret
= wait_on_pipe(iter
, false);
5617 size
= PAGE_SIZE
- info
->read
;
5621 ret
= copy_to_user(ubuf
, info
->spare
+ info
->read
, size
);
5633 static int tracing_buffers_release(struct inode
*inode
, struct file
*file
)
5635 struct ftrace_buffer_info
*info
= file
->private_data
;
5636 struct trace_iterator
*iter
= &info
->iter
;
5638 mutex_lock(&trace_types_lock
);
5640 iter
->tr
->current_trace
->ref
--;
5642 __trace_array_put(iter
->tr
);
5645 ring_buffer_free_read_page(iter
->trace_buffer
->buffer
, info
->spare
);
5648 mutex_unlock(&trace_types_lock
);
5654 struct ring_buffer
*buffer
;
5659 static void buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
5660 struct pipe_buffer
*buf
)
5662 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
5667 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
5672 static void buffer_pipe_buf_get(struct pipe_inode_info
*pipe
,
5673 struct pipe_buffer
*buf
)
5675 struct buffer_ref
*ref
= (struct buffer_ref
*)buf
->private;
5680 /* Pipe buffer operations for a buffer. */
5681 static const struct pipe_buf_operations buffer_pipe_buf_ops
= {
5683 .confirm
= generic_pipe_buf_confirm
,
5684 .release
= buffer_pipe_buf_release
,
5685 .steal
= generic_pipe_buf_steal
,
5686 .get
= buffer_pipe_buf_get
,
5690 * Callback from splice_to_pipe(), if we need to release some pages
5691 * at the end of the spd in case we error'ed out in filling the pipe.
5693 static void buffer_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
5695 struct buffer_ref
*ref
=
5696 (struct buffer_ref
*)spd
->partial
[i
].private;
5701 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
5703 spd
->partial
[i
].private = 0;
5707 tracing_buffers_splice_read(struct file
*file
, loff_t
*ppos
,
5708 struct pipe_inode_info
*pipe
, size_t len
,
5711 struct ftrace_buffer_info
*info
= file
->private_data
;
5712 struct trace_iterator
*iter
= &info
->iter
;
5713 struct partial_page partial_def
[PIPE_DEF_BUFFERS
];
5714 struct page
*pages_def
[PIPE_DEF_BUFFERS
];
5715 struct splice_pipe_desc spd
= {
5717 .partial
= partial_def
,
5718 .nr_pages_max
= PIPE_DEF_BUFFERS
,
5720 .ops
= &buffer_pipe_buf_ops
,
5721 .spd_release
= buffer_spd_release
,
5723 struct buffer_ref
*ref
;
5724 int entries
, size
, i
;
5727 #ifdef CONFIG_TRACER_MAX_TRACE
5728 if (iter
->snapshot
&& iter
->tr
->current_trace
->use_max_tr
)
5732 if (splice_grow_spd(pipe
, &spd
))
5735 if (*ppos
& (PAGE_SIZE
- 1))
5738 if (len
& (PAGE_SIZE
- 1)) {
5739 if (len
< PAGE_SIZE
)
5745 trace_access_lock(iter
->cpu_file
);
5746 entries
= ring_buffer_entries_cpu(iter
->trace_buffer
->buffer
, iter
->cpu_file
);
5748 for (i
= 0; i
< spd
.nr_pages_max
&& len
&& entries
; i
++, len
-= PAGE_SIZE
) {
5752 ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
5759 ref
->buffer
= iter
->trace_buffer
->buffer
;
5760 ref
->page
= ring_buffer_alloc_read_page(ref
->buffer
, iter
->cpu_file
);
5767 r
= ring_buffer_read_page(ref
->buffer
, &ref
->page
,
5768 len
, iter
->cpu_file
, 1);
5770 ring_buffer_free_read_page(ref
->buffer
, ref
->page
);
5776 * zero out any left over data, this is going to
5779 size
= ring_buffer_page_len(ref
->page
);
5780 if (size
< PAGE_SIZE
)
5781 memset(ref
->page
+ size
, 0, PAGE_SIZE
- size
);
5783 page
= virt_to_page(ref
->page
);
5785 spd
.pages
[i
] = page
;
5786 spd
.partial
[i
].len
= PAGE_SIZE
;
5787 spd
.partial
[i
].offset
= 0;
5788 spd
.partial
[i
].private = (unsigned long)ref
;
5792 entries
= ring_buffer_entries_cpu(iter
->trace_buffer
->buffer
, iter
->cpu_file
);
5795 trace_access_unlock(iter
->cpu_file
);
5798 /* did we read anything? */
5799 if (!spd
.nr_pages
) {
5803 if ((file
->f_flags
& O_NONBLOCK
) || (flags
& SPLICE_F_NONBLOCK
))
5806 ret
= wait_on_pipe(iter
, true);
5813 ret
= splice_to_pipe(pipe
, &spd
);
5814 splice_shrink_spd(&spd
);
5819 static const struct file_operations tracing_buffers_fops
= {
5820 .open
= tracing_buffers_open
,
5821 .read
= tracing_buffers_read
,
5822 .poll
= tracing_buffers_poll
,
5823 .release
= tracing_buffers_release
,
5824 .splice_read
= tracing_buffers_splice_read
,
5825 .llseek
= no_llseek
,
5829 tracing_stats_read(struct file
*filp
, char __user
*ubuf
,
5830 size_t count
, loff_t
*ppos
)
5832 struct inode
*inode
= file_inode(filp
);
5833 struct trace_array
*tr
= inode
->i_private
;
5834 struct trace_buffer
*trace_buf
= &tr
->trace_buffer
;
5835 int cpu
= tracing_get_cpu(inode
);
5836 struct trace_seq
*s
;
5838 unsigned long long t
;
5839 unsigned long usec_rem
;
5841 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
5847 cnt
= ring_buffer_entries_cpu(trace_buf
->buffer
, cpu
);
5848 trace_seq_printf(s
, "entries: %ld\n", cnt
);
5850 cnt
= ring_buffer_overrun_cpu(trace_buf
->buffer
, cpu
);
5851 trace_seq_printf(s
, "overrun: %ld\n", cnt
);
5853 cnt
= ring_buffer_commit_overrun_cpu(trace_buf
->buffer
, cpu
);
5854 trace_seq_printf(s
, "commit overrun: %ld\n", cnt
);
5856 cnt
= ring_buffer_bytes_cpu(trace_buf
->buffer
, cpu
);
5857 trace_seq_printf(s
, "bytes: %ld\n", cnt
);
5859 if (trace_clocks
[tr
->clock_id
].in_ns
) {
5860 /* local or global for trace_clock */
5861 t
= ns2usecs(ring_buffer_oldest_event_ts(trace_buf
->buffer
, cpu
));
5862 usec_rem
= do_div(t
, USEC_PER_SEC
);
5863 trace_seq_printf(s
, "oldest event ts: %5llu.%06lu\n",
5866 t
= ns2usecs(ring_buffer_time_stamp(trace_buf
->buffer
, cpu
));
5867 usec_rem
= do_div(t
, USEC_PER_SEC
);
5868 trace_seq_printf(s
, "now ts: %5llu.%06lu\n", t
, usec_rem
);
5870 /* counter or tsc mode for trace_clock */
5871 trace_seq_printf(s
, "oldest event ts: %llu\n",
5872 ring_buffer_oldest_event_ts(trace_buf
->buffer
, cpu
));
5874 trace_seq_printf(s
, "now ts: %llu\n",
5875 ring_buffer_time_stamp(trace_buf
->buffer
, cpu
));
5878 cnt
= ring_buffer_dropped_events_cpu(trace_buf
->buffer
, cpu
);
5879 trace_seq_printf(s
, "dropped events: %ld\n", cnt
);
5881 cnt
= ring_buffer_read_events_cpu(trace_buf
->buffer
, cpu
);
5882 trace_seq_printf(s
, "read events: %ld\n", cnt
);
5884 count
= simple_read_from_buffer(ubuf
, count
, ppos
,
5885 s
->buffer
, trace_seq_used(s
));
5892 static const struct file_operations tracing_stats_fops
= {
5893 .open
= tracing_open_generic_tr
,
5894 .read
= tracing_stats_read
,
5895 .llseek
= generic_file_llseek
,
5896 .release
= tracing_release_generic_tr
,
5899 #ifdef CONFIG_DYNAMIC_FTRACE
5901 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
5907 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
5908 size_t cnt
, loff_t
*ppos
)
5910 static char ftrace_dyn_info_buffer
[1024];
5911 static DEFINE_MUTEX(dyn_info_mutex
);
5912 unsigned long *p
= filp
->private_data
;
5913 char *buf
= ftrace_dyn_info_buffer
;
5914 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
5917 mutex_lock(&dyn_info_mutex
);
5918 r
= sprintf(buf
, "%ld ", *p
);
5920 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
5923 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
5925 mutex_unlock(&dyn_info_mutex
);
5930 static const struct file_operations tracing_dyn_info_fops
= {
5931 .open
= tracing_open_generic
,
5932 .read
= tracing_read_dyn_info
,
5933 .llseek
= generic_file_llseek
,
5935 #endif /* CONFIG_DYNAMIC_FTRACE */
5937 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5939 ftrace_snapshot(unsigned long ip
, unsigned long parent_ip
, void **data
)
5945 ftrace_count_snapshot(unsigned long ip
, unsigned long parent_ip
, void **data
)
5947 unsigned long *count
= (long *)data
;
5959 ftrace_snapshot_print(struct seq_file
*m
, unsigned long ip
,
5960 struct ftrace_probe_ops
*ops
, void *data
)
5962 long count
= (long)data
;
5964 seq_printf(m
, "%ps:", (void *)ip
);
5966 seq_puts(m
, "snapshot");
5969 seq_puts(m
, ":unlimited\n");
5971 seq_printf(m
, ":count=%ld\n", count
);
5976 static struct ftrace_probe_ops snapshot_probe_ops
= {
5977 .func
= ftrace_snapshot
,
5978 .print
= ftrace_snapshot_print
,
5981 static struct ftrace_probe_ops snapshot_count_probe_ops
= {
5982 .func
= ftrace_count_snapshot
,
5983 .print
= ftrace_snapshot_print
,
5987 ftrace_trace_snapshot_callback(struct ftrace_hash
*hash
,
5988 char *glob
, char *cmd
, char *param
, int enable
)
5990 struct ftrace_probe_ops
*ops
;
5991 void *count
= (void *)-1;
5995 /* hash funcs only work with set_ftrace_filter */
5999 ops
= param
? &snapshot_count_probe_ops
: &snapshot_probe_ops
;
6001 if (glob
[0] == '!') {
6002 unregister_ftrace_function_probe_func(glob
+1, ops
);
6009 number
= strsep(¶m
, ":");
6011 if (!strlen(number
))
6015 * We use the callback data field (which is a pointer)
6018 ret
= kstrtoul(number
, 0, (unsigned long *)&count
);
6023 ret
= register_ftrace_function_probe(glob
, ops
, count
);
6026 alloc_snapshot(&global_trace
);
6028 return ret
< 0 ? ret
: 0;
6031 static struct ftrace_func_command ftrace_snapshot_cmd
= {
6033 .func
= ftrace_trace_snapshot_callback
,
6036 static __init
int register_snapshot_cmd(void)
6038 return register_ftrace_command(&ftrace_snapshot_cmd
);
6041 static inline __init
int register_snapshot_cmd(void) { return 0; }
6042 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
6044 static struct dentry
*tracing_get_dentry(struct trace_array
*tr
)
6046 if (WARN_ON(!tr
->dir
))
6047 return ERR_PTR(-ENODEV
);
6049 /* Top directory uses NULL as the parent */
6050 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
)
6053 /* All sub buffers have a descriptor */
6057 static struct dentry
*tracing_dentry_percpu(struct trace_array
*tr
, int cpu
)
6059 struct dentry
*d_tracer
;
6062 return tr
->percpu_dir
;
6064 d_tracer
= tracing_get_dentry(tr
);
6065 if (IS_ERR(d_tracer
))
6068 tr
->percpu_dir
= tracefs_create_dir("per_cpu", d_tracer
);
6070 WARN_ONCE(!tr
->percpu_dir
,
6071 "Could not create tracefs directory 'per_cpu/%d'\n", cpu
);
6073 return tr
->percpu_dir
;
6076 static struct dentry
*
6077 trace_create_cpu_file(const char *name
, umode_t mode
, struct dentry
*parent
,
6078 void *data
, long cpu
, const struct file_operations
*fops
)
6080 struct dentry
*ret
= trace_create_file(name
, mode
, parent
, data
, fops
);
6082 if (ret
) /* See tracing_get_cpu() */
6083 d_inode(ret
)->i_cdev
= (void *)(cpu
+ 1);
6088 tracing_init_tracefs_percpu(struct trace_array
*tr
, long cpu
)
6090 struct dentry
*d_percpu
= tracing_dentry_percpu(tr
, cpu
);
6091 struct dentry
*d_cpu
;
6092 char cpu_dir
[30]; /* 30 characters should be more than enough */
6097 snprintf(cpu_dir
, 30, "cpu%ld", cpu
);
6098 d_cpu
= tracefs_create_dir(cpu_dir
, d_percpu
);
6100 pr_warning("Could not create tracefs '%s' entry\n", cpu_dir
);
6104 /* per cpu trace_pipe */
6105 trace_create_cpu_file("trace_pipe", 0444, d_cpu
,
6106 tr
, cpu
, &tracing_pipe_fops
);
6109 trace_create_cpu_file("trace", 0644, d_cpu
,
6110 tr
, cpu
, &tracing_fops
);
6112 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu
,
6113 tr
, cpu
, &tracing_buffers_fops
);
6115 trace_create_cpu_file("stats", 0444, d_cpu
,
6116 tr
, cpu
, &tracing_stats_fops
);
6118 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu
,
6119 tr
, cpu
, &tracing_entries_fops
);
6121 #ifdef CONFIG_TRACER_SNAPSHOT
6122 trace_create_cpu_file("snapshot", 0644, d_cpu
,
6123 tr
, cpu
, &snapshot_fops
);
6125 trace_create_cpu_file("snapshot_raw", 0444, d_cpu
,
6126 tr
, cpu
, &snapshot_raw_fops
);
6130 #ifdef CONFIG_FTRACE_SELFTEST
6131 /* Let selftest have access to static functions in this file */
6132 #include "trace_selftest.c"
6135 struct trace_option_dentry
{
6136 struct tracer_opt
*opt
;
6137 struct tracer_flags
*flags
;
6138 struct trace_array
*tr
;
6139 struct dentry
*entry
;
6143 trace_options_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
6146 struct trace_option_dentry
*topt
= filp
->private_data
;
6149 if (topt
->flags
->val
& topt
->opt
->bit
)
6154 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
6158 trace_options_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
6161 struct trace_option_dentry
*topt
= filp
->private_data
;
6165 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
6169 if (val
!= 0 && val
!= 1)
6172 if (!!(topt
->flags
->val
& topt
->opt
->bit
) != val
) {
6173 mutex_lock(&trace_types_lock
);
6174 ret
= __set_tracer_option(topt
->tr
, topt
->flags
,
6176 mutex_unlock(&trace_types_lock
);
6187 static const struct file_operations trace_options_fops
= {
6188 .open
= tracing_open_generic
,
6189 .read
= trace_options_read
,
6190 .write
= trace_options_write
,
6191 .llseek
= generic_file_llseek
,
6195 trace_options_core_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
6198 long index
= (long)filp
->private_data
;
6201 if (trace_flags
& (1 << index
))
6206 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
6210 trace_options_core_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
6213 struct trace_array
*tr
= &global_trace
;
6214 long index
= (long)filp
->private_data
;
6218 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
6222 if (val
!= 0 && val
!= 1)
6225 mutex_lock(&trace_types_lock
);
6226 ret
= set_tracer_flag(tr
, 1 << index
, val
);
6227 mutex_unlock(&trace_types_lock
);
6237 static const struct file_operations trace_options_core_fops
= {
6238 .open
= tracing_open_generic
,
6239 .read
= trace_options_core_read
,
6240 .write
= trace_options_core_write
,
6241 .llseek
= generic_file_llseek
,
6244 struct dentry
*trace_create_file(const char *name
,
6246 struct dentry
*parent
,
6248 const struct file_operations
*fops
)
6252 ret
= tracefs_create_file(name
, mode
, parent
, data
, fops
);
6254 pr_warning("Could not create tracefs '%s' entry\n", name
);
6260 static struct dentry
*trace_options_init_dentry(struct trace_array
*tr
)
6262 struct dentry
*d_tracer
;
6267 d_tracer
= tracing_get_dentry(tr
);
6268 if (IS_ERR(d_tracer
))
6271 tr
->options
= tracefs_create_dir("options", d_tracer
);
6273 pr_warning("Could not create tracefs directory 'options'\n");
6281 create_trace_option_file(struct trace_array
*tr
,
6282 struct trace_option_dentry
*topt
,
6283 struct tracer_flags
*flags
,
6284 struct tracer_opt
*opt
)
6286 struct dentry
*t_options
;
6288 t_options
= trace_options_init_dentry(tr
);
6292 topt
->flags
= flags
;
6296 topt
->entry
= trace_create_file(opt
->name
, 0644, t_options
, topt
,
6297 &trace_options_fops
);
6301 static struct trace_option_dentry
*
6302 create_trace_option_files(struct trace_array
*tr
, struct tracer
*tracer
)
6304 struct trace_option_dentry
*topts
;
6305 struct tracer_flags
*flags
;
6306 struct tracer_opt
*opts
;
6312 flags
= tracer
->flags
;
6314 if (!flags
|| !flags
->opts
)
6319 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
6322 topts
= kcalloc(cnt
+ 1, sizeof(*topts
), GFP_KERNEL
);
6326 for (cnt
= 0; opts
[cnt
].name
; cnt
++)
6327 create_trace_option_file(tr
, &topts
[cnt
], flags
,
6334 destroy_trace_option_files(struct trace_option_dentry
*topts
)
6341 for (cnt
= 0; topts
[cnt
].opt
; cnt
++)
6342 tracefs_remove(topts
[cnt
].entry
);
6347 static struct dentry
*
6348 create_trace_option_core_file(struct trace_array
*tr
,
6349 const char *option
, long index
)
6351 struct dentry
*t_options
;
6353 t_options
= trace_options_init_dentry(tr
);
6357 return trace_create_file(option
, 0644, t_options
, (void *)index
,
6358 &trace_options_core_fops
);
6361 static __init
void create_trace_options_dir(struct trace_array
*tr
)
6363 struct dentry
*t_options
;
6366 t_options
= trace_options_init_dentry(tr
);
6370 for (i
= 0; trace_options
[i
]; i
++)
6371 create_trace_option_core_file(tr
, trace_options
[i
], i
);
6375 rb_simple_read(struct file
*filp
, char __user
*ubuf
,
6376 size_t cnt
, loff_t
*ppos
)
6378 struct trace_array
*tr
= filp
->private_data
;
6382 r
= tracer_tracing_is_on(tr
);
6383 r
= sprintf(buf
, "%d\n", r
);
6385 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
6389 rb_simple_write(struct file
*filp
, const char __user
*ubuf
,
6390 size_t cnt
, loff_t
*ppos
)
6392 struct trace_array
*tr
= filp
->private_data
;
6393 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
6397 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
6402 mutex_lock(&trace_types_lock
);
6404 tracer_tracing_on(tr
);
6405 if (tr
->current_trace
->start
)
6406 tr
->current_trace
->start(tr
);
6408 tracer_tracing_off(tr
);
6409 if (tr
->current_trace
->stop
)
6410 tr
->current_trace
->stop(tr
);
6412 mutex_unlock(&trace_types_lock
);
6420 static const struct file_operations rb_simple_fops
= {
6421 .open
= tracing_open_generic_tr
,
6422 .read
= rb_simple_read
,
6423 .write
= rb_simple_write
,
6424 .release
= tracing_release_generic_tr
,
6425 .llseek
= default_llseek
,
6428 struct dentry
*trace_instance_dir
;
6431 init_tracer_tracefs(struct trace_array
*tr
, struct dentry
*d_tracer
);
6434 allocate_trace_buffer(struct trace_array
*tr
, struct trace_buffer
*buf
, int size
)
6436 enum ring_buffer_flags rb_flags
;
6438 rb_flags
= trace_flags
& TRACE_ITER_OVERWRITE
? RB_FL_OVERWRITE
: 0;
6442 buf
->buffer
= ring_buffer_alloc(size
, rb_flags
);
6446 buf
->data
= alloc_percpu(struct trace_array_cpu
);
6448 ring_buffer_free(buf
->buffer
);
6452 /* Allocate the first page for all buffers */
6453 set_buffer_entries(&tr
->trace_buffer
,
6454 ring_buffer_size(tr
->trace_buffer
.buffer
, 0));
6459 static int allocate_trace_buffers(struct trace_array
*tr
, int size
)
6463 ret
= allocate_trace_buffer(tr
, &tr
->trace_buffer
, size
);
6467 #ifdef CONFIG_TRACER_MAX_TRACE
6468 ret
= allocate_trace_buffer(tr
, &tr
->max_buffer
,
6469 allocate_snapshot
? size
: 1);
6471 ring_buffer_free(tr
->trace_buffer
.buffer
);
6472 free_percpu(tr
->trace_buffer
.data
);
6475 tr
->allocated_snapshot
= allocate_snapshot
;
6478 * Only the top level trace array gets its snapshot allocated
6479 * from the kernel command line.
6481 allocate_snapshot
= false;
6486 static void free_trace_buffer(struct trace_buffer
*buf
)
6489 ring_buffer_free(buf
->buffer
);
6491 free_percpu(buf
->data
);
6496 static void free_trace_buffers(struct trace_array
*tr
)
6501 free_trace_buffer(&tr
->trace_buffer
);
6503 #ifdef CONFIG_TRACER_MAX_TRACE
6504 free_trace_buffer(&tr
->max_buffer
);
6508 static int instance_mkdir(const char *name
)
6510 struct trace_array
*tr
;
6513 mutex_lock(&trace_types_lock
);
6516 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
6517 if (tr
->name
&& strcmp(tr
->name
, name
) == 0)
6522 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
6526 tr
->name
= kstrdup(name
, GFP_KERNEL
);
6530 if (!alloc_cpumask_var(&tr
->tracing_cpumask
, GFP_KERNEL
))
6533 cpumask_copy(tr
->tracing_cpumask
, cpu_all_mask
);
6535 raw_spin_lock_init(&tr
->start_lock
);
6537 tr
->max_lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
6539 tr
->current_trace
= &nop_trace
;
6541 INIT_LIST_HEAD(&tr
->systems
);
6542 INIT_LIST_HEAD(&tr
->events
);
6544 if (allocate_trace_buffers(tr
, trace_buf_size
) < 0)
6547 tr
->dir
= tracefs_create_dir(name
, trace_instance_dir
);
6551 ret
= event_trace_add_tracer(tr
->dir
, tr
);
6553 tracefs_remove_recursive(tr
->dir
);
6557 init_tracer_tracefs(tr
, tr
->dir
);
6559 list_add(&tr
->list
, &ftrace_trace_arrays
);
6561 mutex_unlock(&trace_types_lock
);
6566 free_trace_buffers(tr
);
6567 free_cpumask_var(tr
->tracing_cpumask
);
6572 mutex_unlock(&trace_types_lock
);
6578 static int instance_rmdir(const char *name
)
6580 struct trace_array
*tr
;
6584 mutex_lock(&trace_types_lock
);
6587 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
6588 if (tr
->name
&& strcmp(tr
->name
, name
) == 0) {
6597 if (tr
->ref
|| (tr
->current_trace
&& tr
->current_trace
->ref
))
6600 list_del(&tr
->list
);
6602 tracing_set_nop(tr
);
6603 event_trace_del_tracer(tr
);
6604 ftrace_destroy_function_files(tr
);
6605 debugfs_remove_recursive(tr
->dir
);
6606 free_trace_buffers(tr
);
6614 mutex_unlock(&trace_types_lock
);
6619 static __init
void create_trace_instances(struct dentry
*d_tracer
)
6621 trace_instance_dir
= tracefs_create_instance_dir("instances", d_tracer
,
6624 if (WARN_ON(!trace_instance_dir
))
6629 init_tracer_tracefs(struct trace_array
*tr
, struct dentry
*d_tracer
)
6633 trace_create_file("available_tracers", 0444, d_tracer
,
6634 tr
, &show_traces_fops
);
6636 trace_create_file("current_tracer", 0644, d_tracer
,
6637 tr
, &set_tracer_fops
);
6639 trace_create_file("tracing_cpumask", 0644, d_tracer
,
6640 tr
, &tracing_cpumask_fops
);
6642 trace_create_file("trace_options", 0644, d_tracer
,
6643 tr
, &tracing_iter_fops
);
6645 trace_create_file("trace", 0644, d_tracer
,
6648 trace_create_file("trace_pipe", 0444, d_tracer
,
6649 tr
, &tracing_pipe_fops
);
6651 trace_create_file("buffer_size_kb", 0644, d_tracer
,
6652 tr
, &tracing_entries_fops
);
6654 trace_create_file("buffer_total_size_kb", 0444, d_tracer
,
6655 tr
, &tracing_total_entries_fops
);
6657 trace_create_file("free_buffer", 0200, d_tracer
,
6658 tr
, &tracing_free_buffer_fops
);
6660 trace_create_file("trace_marker", 0220, d_tracer
,
6661 tr
, &tracing_mark_fops
);
6663 trace_create_file("trace_clock", 0644, d_tracer
, tr
,
6666 trace_create_file("tracing_on", 0644, d_tracer
,
6667 tr
, &rb_simple_fops
);
6669 #ifdef CONFIG_TRACER_MAX_TRACE
6670 trace_create_file("tracing_max_latency", 0644, d_tracer
,
6671 &tr
->max_latency
, &tracing_max_lat_fops
);
6674 if (ftrace_create_function_files(tr
, d_tracer
))
6675 WARN(1, "Could not allocate function filter files");
6677 #ifdef CONFIG_TRACER_SNAPSHOT
6678 trace_create_file("snapshot", 0644, d_tracer
,
6679 tr
, &snapshot_fops
);
6682 for_each_tracing_cpu(cpu
)
6683 tracing_init_tracefs_percpu(tr
, cpu
);
6687 static struct vfsmount
*trace_automount(void *ingore
)
6689 struct vfsmount
*mnt
;
6690 struct file_system_type
*type
;
6693 * To maintain backward compatibility for tools that mount
6694 * debugfs to get to the tracing facility, tracefs is automatically
6695 * mounted to the debugfs/tracing directory.
6697 type
= get_fs_type("tracefs");
6700 mnt
= vfs_kern_mount(type
, 0, "tracefs", NULL
);
6701 put_filesystem(type
);
6710 * tracing_init_dentry - initialize top level trace array
6712 * This is called when creating files or directories in the tracing
6713 * directory. It is called via fs_initcall() by any of the boot up code
6714 * and expects to return the dentry of the top level tracing directory.
6716 struct dentry
*tracing_init_dentry(void)
6718 struct trace_array
*tr
= &global_trace
;
6720 /* The top level trace array uses NULL as parent */
6724 if (WARN_ON(!debugfs_initialized()))
6725 return ERR_PTR(-ENODEV
);
6728 * As there may still be users that expect the tracing
6729 * files to exist in debugfs/tracing, we must automount
6730 * the tracefs file system there, so older tools still
6731 * work with the newer kerenl.
6733 tr
->dir
= debugfs_create_automount("tracing", NULL
,
6734 trace_automount
, NULL
);
6736 pr_warn_once("Could not create debugfs directory 'tracing'\n");
6737 return ERR_PTR(-ENOMEM
);
6743 extern struct trace_enum_map
*__start_ftrace_enum_maps
[];
6744 extern struct trace_enum_map
*__stop_ftrace_enum_maps
[];
6746 static void __init
trace_enum_init(void)
6750 len
= __stop_ftrace_enum_maps
- __start_ftrace_enum_maps
;
6751 trace_insert_enum_map(NULL
, __start_ftrace_enum_maps
, len
);
6754 #ifdef CONFIG_MODULES
6755 static void trace_module_add_enums(struct module
*mod
)
6757 if (!mod
->num_trace_enums
)
6761 * Modules with bad taint do not have events created, do
6762 * not bother with enums either.
6764 if (trace_module_has_bad_taint(mod
))
6767 trace_insert_enum_map(mod
, mod
->trace_enums
, mod
->num_trace_enums
);
6770 #ifdef CONFIG_TRACE_ENUM_MAP_FILE
6771 static void trace_module_remove_enums(struct module
*mod
)
6773 union trace_enum_map_item
*map
;
6774 union trace_enum_map_item
**last
= &trace_enum_maps
;
6776 if (!mod
->num_trace_enums
)
6779 mutex_lock(&trace_enum_mutex
);
6781 map
= trace_enum_maps
;
6784 if (map
->head
.mod
== mod
)
6786 map
= trace_enum_jmp_to_tail(map
);
6787 last
= &map
->tail
.next
;
6788 map
= map
->tail
.next
;
6793 *last
= trace_enum_jmp_to_tail(map
)->tail
.next
;
6796 mutex_unlock(&trace_enum_mutex
);
6799 static inline void trace_module_remove_enums(struct module
*mod
) { }
6800 #endif /* CONFIG_TRACE_ENUM_MAP_FILE */
6802 static int trace_module_notify(struct notifier_block
*self
,
6803 unsigned long val
, void *data
)
6805 struct module
*mod
= data
;
6808 case MODULE_STATE_COMING
:
6809 trace_module_add_enums(mod
);
6811 case MODULE_STATE_GOING
:
6812 trace_module_remove_enums(mod
);
6819 static struct notifier_block trace_module_nb
= {
6820 .notifier_call
= trace_module_notify
,
6823 #endif /* CONFIG_MODULES */
6825 static __init
int tracer_init_tracefs(void)
6827 struct dentry
*d_tracer
;
6829 trace_access_lock_init();
6831 d_tracer
= tracing_init_dentry();
6832 if (IS_ERR(d_tracer
))
6835 init_tracer_tracefs(&global_trace
, d_tracer
);
6837 trace_create_file("tracing_thresh", 0644, d_tracer
,
6838 &global_trace
, &tracing_thresh_fops
);
6840 trace_create_file("README", 0444, d_tracer
,
6841 NULL
, &tracing_readme_fops
);
6843 trace_create_file("saved_cmdlines", 0444, d_tracer
,
6844 NULL
, &tracing_saved_cmdlines_fops
);
6846 trace_create_file("saved_cmdlines_size", 0644, d_tracer
,
6847 NULL
, &tracing_saved_cmdlines_size_fops
);
6851 trace_create_enum_file(d_tracer
);
6853 #ifdef CONFIG_MODULES
6854 register_module_notifier(&trace_module_nb
);
6857 #ifdef CONFIG_DYNAMIC_FTRACE
6858 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
6859 &ftrace_update_tot_cnt
, &tracing_dyn_info_fops
);
6862 create_trace_instances(d_tracer
);
6864 create_trace_options_dir(&global_trace
);
6866 /* If the tracer was started via cmdline, create options for it here */
6867 if (global_trace
.current_trace
!= &nop_trace
)
6868 update_tracer_options(&global_trace
, global_trace
.current_trace
);
6873 static int trace_panic_handler(struct notifier_block
*this,
6874 unsigned long event
, void *unused
)
6876 if (ftrace_dump_on_oops
)
6877 ftrace_dump(ftrace_dump_on_oops
);
6881 static struct notifier_block trace_panic_notifier
= {
6882 .notifier_call
= trace_panic_handler
,
6884 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
6887 static int trace_die_handler(struct notifier_block
*self
,
6893 if (ftrace_dump_on_oops
)
6894 ftrace_dump(ftrace_dump_on_oops
);
6902 static struct notifier_block trace_die_notifier
= {
6903 .notifier_call
= trace_die_handler
,
6908 * printk is set to max of 1024, we really don't need it that big.
6909 * Nothing should be printing 1000 characters anyway.
6911 #define TRACE_MAX_PRINT 1000
6914 * Define here KERN_TRACE so that we have one place to modify
6915 * it if we decide to change what log level the ftrace dump
6918 #define KERN_TRACE KERN_EMERG
6921 trace_printk_seq(struct trace_seq
*s
)
6923 /* Probably should print a warning here. */
6924 if (s
->seq
.len
>= TRACE_MAX_PRINT
)
6925 s
->seq
.len
= TRACE_MAX_PRINT
;
6928 * More paranoid code. Although the buffer size is set to
6929 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
6930 * an extra layer of protection.
6932 if (WARN_ON_ONCE(s
->seq
.len
>= s
->seq
.size
))
6933 s
->seq
.len
= s
->seq
.size
- 1;
6935 /* should be zero ended, but we are paranoid. */
6936 s
->buffer
[s
->seq
.len
] = 0;
6938 printk(KERN_TRACE
"%s", s
->buffer
);
6943 void trace_init_global_iter(struct trace_iterator
*iter
)
6945 iter
->tr
= &global_trace
;
6946 iter
->trace
= iter
->tr
->current_trace
;
6947 iter
->cpu_file
= RING_BUFFER_ALL_CPUS
;
6948 iter
->trace_buffer
= &global_trace
.trace_buffer
;
6950 if (iter
->trace
&& iter
->trace
->open
)
6951 iter
->trace
->open(iter
);
6953 /* Annotate start of buffers if we had overruns */
6954 if (ring_buffer_overruns(iter
->trace_buffer
->buffer
))
6955 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
6957 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
6958 if (trace_clocks
[iter
->tr
->clock_id
].in_ns
)
6959 iter
->iter_flags
|= TRACE_FILE_TIME_IN_NS
;
6962 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode
)
6964 /* use static because iter can be a bit big for the stack */
6965 static struct trace_iterator iter
;
6966 static atomic_t dump_running
;
6967 unsigned int old_userobj
;
6968 unsigned long flags
;
6971 /* Only allow one dump user at a time. */
6972 if (atomic_inc_return(&dump_running
) != 1) {
6973 atomic_dec(&dump_running
);
6978 * Always turn off tracing when we dump.
6979 * We don't need to show trace output of what happens
6980 * between multiple crashes.
6982 * If the user does a sysrq-z, then they can re-enable
6983 * tracing with echo 1 > tracing_on.
6987 local_irq_save(flags
);
6989 /* Simulate the iterator */
6990 trace_init_global_iter(&iter
);
6992 for_each_tracing_cpu(cpu
) {
6993 atomic_inc(&per_cpu_ptr(iter
.tr
->trace_buffer
.data
, cpu
)->disabled
);
6996 old_userobj
= trace_flags
& TRACE_ITER_SYM_USEROBJ
;
6998 /* don't look at user memory in panic mode */
6999 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
7001 switch (oops_dump_mode
) {
7003 iter
.cpu_file
= RING_BUFFER_ALL_CPUS
;
7006 iter
.cpu_file
= raw_smp_processor_id();
7011 printk(KERN_TRACE
"Bad dumping mode, switching to all CPUs dump\n");
7012 iter
.cpu_file
= RING_BUFFER_ALL_CPUS
;
7015 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
7017 /* Did function tracer already get disabled? */
7018 if (ftrace_is_dead()) {
7019 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
7020 printk("# MAY BE MISSING FUNCTION EVENTS\n");
7024 * We need to stop all tracing on all CPUS to read the
7025 * the next buffer. This is a bit expensive, but is
7026 * not done often. We fill all what we can read,
7027 * and then release the locks again.
7030 while (!trace_empty(&iter
)) {
7033 printk(KERN_TRACE
"---------------------------------\n");
7037 /* reset all but tr, trace, and overruns */
7038 memset(&iter
.seq
, 0,
7039 sizeof(struct trace_iterator
) -
7040 offsetof(struct trace_iterator
, seq
));
7041 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
7044 if (trace_find_next_entry_inc(&iter
) != NULL
) {
7047 ret
= print_trace_line(&iter
);
7048 if (ret
!= TRACE_TYPE_NO_CONSUME
)
7049 trace_consume(&iter
);
7051 touch_nmi_watchdog();
7053 trace_printk_seq(&iter
.seq
);
7057 printk(KERN_TRACE
" (ftrace buffer empty)\n");
7059 printk(KERN_TRACE
"---------------------------------\n");
7062 trace_flags
|= old_userobj
;
7064 for_each_tracing_cpu(cpu
) {
7065 atomic_dec(&per_cpu_ptr(iter
.trace_buffer
->data
, cpu
)->disabled
);
7067 atomic_dec(&dump_running
);
7068 local_irq_restore(flags
);
7070 EXPORT_SYMBOL_GPL(ftrace_dump
);
7072 __init
static int tracer_alloc_buffers(void)
7077 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
7080 if (!alloc_cpumask_var(&global_trace
.tracing_cpumask
, GFP_KERNEL
))
7081 goto out_free_buffer_mask
;
7083 /* Only allocate trace_printk buffers if a trace_printk exists */
7084 if (__stop___trace_bprintk_fmt
!= __start___trace_bprintk_fmt
)
7085 /* Must be called before global_trace.buffer is allocated */
7086 trace_printk_init_buffers();
7088 /* To save memory, keep the ring buffer size to its minimum */
7089 if (ring_buffer_expanded
)
7090 ring_buf_size
= trace_buf_size
;
7094 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
7095 cpumask_copy(global_trace
.tracing_cpumask
, cpu_all_mask
);
7097 raw_spin_lock_init(&global_trace
.start_lock
);
7099 /* Used for event triggers */
7100 temp_buffer
= ring_buffer_alloc(PAGE_SIZE
, RB_FL_OVERWRITE
);
7102 goto out_free_cpumask
;
7104 if (trace_create_savedcmd() < 0)
7105 goto out_free_temp_buffer
;
7107 /* TODO: make the number of buffers hot pluggable with CPUS */
7108 if (allocate_trace_buffers(&global_trace
, ring_buf_size
) < 0) {
7109 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
7111 goto out_free_savedcmd
;
7114 if (global_trace
.buffer_disabled
)
7117 if (trace_boot_clock
) {
7118 ret
= tracing_set_clock(&global_trace
, trace_boot_clock
);
7120 pr_warning("Trace clock %s not defined, going back to default\n",
7125 * register_tracer() might reference current_trace, so it
7126 * needs to be set before we register anything. This is
7127 * just a bootstrap of current_trace anyway.
7129 global_trace
.current_trace
= &nop_trace
;
7131 global_trace
.max_lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
7133 ftrace_init_global_array_ops(&global_trace
);
7135 register_tracer(&nop_trace
);
7137 /* All seems OK, enable tracing */
7138 tracing_disabled
= 0;
7140 atomic_notifier_chain_register(&panic_notifier_list
,
7141 &trace_panic_notifier
);
7143 register_die_notifier(&trace_die_notifier
);
7145 global_trace
.flags
= TRACE_ARRAY_FL_GLOBAL
;
7147 INIT_LIST_HEAD(&global_trace
.systems
);
7148 INIT_LIST_HEAD(&global_trace
.events
);
7149 list_add(&global_trace
.list
, &ftrace_trace_arrays
);
7151 while (trace_boot_options
) {
7154 option
= strsep(&trace_boot_options
, ",");
7155 trace_set_options(&global_trace
, option
);
7158 register_snapshot_cmd();
7163 free_saved_cmdlines_buffer(savedcmd
);
7164 out_free_temp_buffer
:
7165 ring_buffer_free(temp_buffer
);
7167 free_cpumask_var(global_trace
.tracing_cpumask
);
7168 out_free_buffer_mask
:
7169 free_cpumask_var(tracing_buffer_mask
);
7174 void __init
trace_init(void)
7176 if (tracepoint_printk
) {
7177 tracepoint_print_iter
=
7178 kmalloc(sizeof(*tracepoint_print_iter
), GFP_KERNEL
);
7179 if (WARN_ON(!tracepoint_print_iter
))
7180 tracepoint_printk
= 0;
7182 tracer_alloc_buffers();
7186 __init
static int clear_boot_tracer(void)
7189 * The default tracer at boot buffer is an init section.
7190 * This function is called in lateinit. If we did not
7191 * find the boot tracer, then clear it out, to prevent
7192 * later registration from accessing the buffer that is
7193 * about to be freed.
7195 if (!default_bootup_tracer
)
7198 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
7199 default_bootup_tracer
);
7200 default_bootup_tracer
= NULL
;
7205 fs_initcall(tracer_init_tracefs
);
7206 late_initcall(clear_boot_tracer
);