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/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/rwsem.h>
35 #include <linux/slab.h>
36 #include <linux/ctype.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
39 #include <linux/nmi.h>
41 #include <linux/sched/rt.h>
44 #include "trace_output.h"
47 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
50 bool ring_buffer_expanded;
53 * We need to change this state when a selftest is running.
54 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
56 * insertions into the ring-buffer such as trace_printk could occurred
57 * at the same time, giving false positive or negative results.
59 static bool __read_mostly tracing_selftest_running;
62 * If a tracer is running, we do not want to run SELFTEST.
64 bool __read_mostly tracing_selftest_disabled;
66 /* For tracers that don't implement custom flags */
67 static struct tracer_opt dummy_tracer_opt[] = {
71 static struct tracer_flags dummy_tracer_flags = {
73 .opts = dummy_tracer_opt
76 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
82 * To prevent the comm cache from being overwritten when no
83 * tracing is active, only save the comm when a trace event
86 static DEFINE_PER_CPU(bool, trace_cmdline_save);
89 * Kill all tracing for good (never come back).
90 * It is initialized to 1 but will turn to zero if the initialization
91 * of the tracer is successful. But that is the only place that sets
94 static int tracing_disabled = 1;
96 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
98 cpumask_var_t __read_mostly tracing_buffer_mask;
101 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
103 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
104 * is set, then ftrace_dump is called. This will output the contents
105 * of the ftrace buffers to the console. This is very useful for
106 * capturing traces that lead to crashes and outputing it to a
109 * It is default off, but you can enable it with either specifying
110 * "ftrace_dump_on_oops" in the kernel command line, or setting
111 * /proc/sys/kernel/ftrace_dump_on_oops
112 * Set 1 if you want to dump buffers of all CPUs
113 * Set 2 if you want to dump the buffer of the CPU that triggered oops
116 enum ftrace_dump_mode ftrace_dump_on_oops;
118 static int tracing_set_tracer(const char *buf);
120 #define MAX_TRACER_SIZE 100
121 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
122 static char *default_bootup_tracer;
124 static bool allocate_snapshot;
126 static int __init set_cmdline_ftrace(char *str)
128 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
129 default_bootup_tracer = bootup_tracer_buf;
130 /* We are using ftrace early, expand it */
131 ring_buffer_expanded = true;
134 __setup("ftrace=", set_cmdline_ftrace);
136 static int __init set_ftrace_dump_on_oops(char *str)
138 if (*str++ != '=' || !*str) {
139 ftrace_dump_on_oops = DUMP_ALL;
143 if (!strcmp("orig_cpu", str)) {
144 ftrace_dump_on_oops = DUMP_ORIG;
150 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
152 static int __init boot_alloc_snapshot(char *str)
154 allocate_snapshot = true;
155 /* We also need the main ring buffer expanded */
156 ring_buffer_expanded = true;
159 __setup("alloc_snapshot", boot_alloc_snapshot);
162 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
163 static char *trace_boot_options __initdata;
165 static int __init set_trace_boot_options(char *str)
167 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
168 trace_boot_options = trace_boot_options_buf;
171 __setup("trace_options=", set_trace_boot_options);
173 unsigned long long ns2usecs(cycle_t nsec)
181 * The global_trace is the descriptor that holds the tracing
182 * buffers for the live tracing. For each CPU, it contains
183 * a link list of pages that will store trace entries. The
184 * page descriptor of the pages in the memory is used to hold
185 * the link list by linking the lru item in the page descriptor
186 * to each of the pages in the buffer per CPU.
188 * For each active CPU there is a data field that holds the
189 * pages for the buffer for that CPU. Each CPU has the same number
190 * of pages allocated for its buffer.
192 static struct trace_array global_trace;
194 LIST_HEAD(ftrace_trace_arrays);
196 int trace_array_get(struct trace_array *this_tr)
198 struct trace_array *tr;
201 mutex_lock(&trace_types_lock);
202 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
209 mutex_unlock(&trace_types_lock);
214 static void __trace_array_put(struct trace_array *this_tr)
216 WARN_ON(!this_tr->ref);
220 void trace_array_put(struct trace_array *this_tr)
222 mutex_lock(&trace_types_lock);
223 __trace_array_put(this_tr);
224 mutex_unlock(&trace_types_lock);
227 int filter_current_check_discard(struct ring_buffer *buffer,
228 struct ftrace_event_call *call, void *rec,
229 struct ring_buffer_event *event)
231 return filter_check_discard(call, rec, buffer, event);
233 EXPORT_SYMBOL_GPL(filter_current_check_discard);
235 cycle_t buffer_ftrace_now(struct trace_buffer *buf, int cpu)
239 /* Early boot up does not have a buffer yet */
241 return trace_clock_local();
243 ts = ring_buffer_time_stamp(buf->buffer, cpu);
244 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
249 cycle_t ftrace_now(int cpu)
251 return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
255 * tracing_is_enabled - Show if global_trace has been disabled
257 * Shows if the global trace has been enabled or not. It uses the
258 * mirror flag "buffer_disabled" to be used in fast paths such as for
259 * the irqsoff tracer. But it may be inaccurate due to races. If you
260 * need to know the accurate state, use tracing_is_on() which is a little
261 * slower, but accurate.
263 int tracing_is_enabled(void)
266 * For quick access (irqsoff uses this in fast path), just
267 * return the mirror variable of the state of the ring buffer.
268 * It's a little racy, but we don't really care.
271 return !global_trace.buffer_disabled;
275 * trace_buf_size is the size in bytes that is allocated
276 * for a buffer. Note, the number of bytes is always rounded
279 * This number is purposely set to a low number of 16384.
280 * If the dump on oops happens, it will be much appreciated
281 * to not have to wait for all that output. Anyway this can be
282 * boot time and run time configurable.
284 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
286 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
288 /* trace_types holds a link list of available tracers. */
289 static struct tracer *trace_types __read_mostly;
292 * trace_types_lock is used to protect the trace_types list.
294 DEFINE_MUTEX(trace_types_lock);
297 * serialize the access of the ring buffer
299 * ring buffer serializes readers, but it is low level protection.
300 * The validity of the events (which returns by ring_buffer_peek() ..etc)
301 * are not protected by ring buffer.
303 * The content of events may become garbage if we allow other process consumes
304 * these events concurrently:
305 * A) the page of the consumed events may become a normal page
306 * (not reader page) in ring buffer, and this page will be rewrited
307 * by events producer.
308 * B) The page of the consumed events may become a page for splice_read,
309 * and this page will be returned to system.
311 * These primitives allow multi process access to different cpu ring buffer
314 * These primitives don't distinguish read-only and read-consume access.
315 * Multi read-only access are also serialized.
319 static DECLARE_RWSEM(all_cpu_access_lock);
320 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
322 static inline void trace_access_lock(int cpu)
324 if (cpu == RING_BUFFER_ALL_CPUS) {
325 /* gain it for accessing the whole ring buffer. */
326 down_write(&all_cpu_access_lock);
328 /* gain it for accessing a cpu ring buffer. */
330 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
331 down_read(&all_cpu_access_lock);
333 /* Secondly block other access to this @cpu ring buffer. */
334 mutex_lock(&per_cpu(cpu_access_lock, cpu));
338 static inline void trace_access_unlock(int cpu)
340 if (cpu == RING_BUFFER_ALL_CPUS) {
341 up_write(&all_cpu_access_lock);
343 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
344 up_read(&all_cpu_access_lock);
348 static inline void trace_access_lock_init(void)
352 for_each_possible_cpu(cpu)
353 mutex_init(&per_cpu(cpu_access_lock, cpu));
358 static DEFINE_MUTEX(access_lock);
360 static inline void trace_access_lock(int cpu)
363 mutex_lock(&access_lock);
366 static inline void trace_access_unlock(int cpu)
369 mutex_unlock(&access_lock);
372 static inline void trace_access_lock_init(void)
378 /* trace_flags holds trace_options default values */
379 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
380 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
381 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
382 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
384 void tracer_tracing_on(struct trace_array *tr)
386 if (tr->trace_buffer.buffer)
387 ring_buffer_record_on(tr->trace_buffer.buffer);
389 * This flag is looked at when buffers haven't been allocated
390 * yet, or by some tracers (like irqsoff), that just want to
391 * know if the ring buffer has been disabled, but it can handle
392 * races of where it gets disabled but we still do a record.
393 * As the check is in the fast path of the tracers, it is more
394 * important to be fast than accurate.
396 tr->buffer_disabled = 0;
397 /* Make the flag seen by readers */
402 * tracing_on - enable tracing buffers
404 * This function enables tracing buffers that may have been
405 * disabled with tracing_off.
407 void tracing_on(void)
409 tracer_tracing_on(&global_trace);
411 EXPORT_SYMBOL_GPL(tracing_on);
414 * __trace_puts - write a constant string into the trace buffer.
415 * @ip: The address of the caller
416 * @str: The constant string to write
417 * @size: The size of the string.
419 int __trace_puts(unsigned long ip, const char *str, int size)
421 struct ring_buffer_event *event;
422 struct ring_buffer *buffer;
423 struct print_entry *entry;
424 unsigned long irq_flags;
427 alloc = sizeof(*entry) + size + 2; /* possible \n added */
429 local_save_flags(irq_flags);
430 buffer = global_trace.trace_buffer.buffer;
431 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
432 irq_flags, preempt_count());
436 entry = ring_buffer_event_data(event);
439 memcpy(&entry->buf, str, size);
441 /* Add a newline if necessary */
442 if (entry->buf[size - 1] != '\n') {
443 entry->buf[size] = '\n';
444 entry->buf[size + 1] = '\0';
446 entry->buf[size] = '\0';
448 __buffer_unlock_commit(buffer, event);
452 EXPORT_SYMBOL_GPL(__trace_puts);
455 * __trace_bputs - write the pointer to a constant string into trace buffer
456 * @ip: The address of the caller
457 * @str: The constant string to write to the buffer to
459 int __trace_bputs(unsigned long ip, const char *str)
461 struct ring_buffer_event *event;
462 struct ring_buffer *buffer;
463 struct bputs_entry *entry;
464 unsigned long irq_flags;
465 int size = sizeof(struct bputs_entry);
467 local_save_flags(irq_flags);
468 buffer = global_trace.trace_buffer.buffer;
469 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
470 irq_flags, preempt_count());
474 entry = ring_buffer_event_data(event);
478 __buffer_unlock_commit(buffer, event);
482 EXPORT_SYMBOL_GPL(__trace_bputs);
484 #ifdef CONFIG_TRACER_SNAPSHOT
486 * trace_snapshot - take a snapshot of the current buffer.
488 * This causes a swap between the snapshot buffer and the current live
489 * tracing buffer. You can use this to take snapshots of the live
490 * trace when some condition is triggered, but continue to trace.
492 * Note, make sure to allocate the snapshot with either
493 * a tracing_snapshot_alloc(), or by doing it manually
494 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
496 * If the snapshot buffer is not allocated, it will stop tracing.
497 * Basically making a permanent snapshot.
499 void tracing_snapshot(void)
501 struct trace_array *tr = &global_trace;
502 struct tracer *tracer = tr->current_trace;
506 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
507 internal_trace_puts("*** snapshot is being ignored ***\n");
511 if (!tr->allocated_snapshot) {
512 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
513 internal_trace_puts("*** stopping trace here! ***\n");
518 /* Note, snapshot can not be used when the tracer uses it */
519 if (tracer->use_max_tr) {
520 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
521 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
525 local_irq_save(flags);
526 update_max_tr(tr, current, smp_processor_id());
527 local_irq_restore(flags);
529 EXPORT_SYMBOL_GPL(tracing_snapshot);
531 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
532 struct trace_buffer *size_buf, int cpu_id);
533 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
535 static int alloc_snapshot(struct trace_array *tr)
539 if (!tr->allocated_snapshot) {
541 /* allocate spare buffer */
542 ret = resize_buffer_duplicate_size(&tr->max_buffer,
543 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
547 tr->allocated_snapshot = true;
553 void free_snapshot(struct trace_array *tr)
556 * We don't free the ring buffer. instead, resize it because
557 * The max_tr ring buffer has some state (e.g. ring->clock) and
558 * we want preserve it.
560 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
561 set_buffer_entries(&tr->max_buffer, 1);
562 tracing_reset_online_cpus(&tr->max_buffer);
563 tr->allocated_snapshot = false;
567 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
569 * This is similar to trace_snapshot(), but it will allocate the
570 * snapshot buffer if it isn't already allocated. Use this only
571 * where it is safe to sleep, as the allocation may sleep.
573 * This causes a swap between the snapshot buffer and the current live
574 * tracing buffer. You can use this to take snapshots of the live
575 * trace when some condition is triggered, but continue to trace.
577 void tracing_snapshot_alloc(void)
579 struct trace_array *tr = &global_trace;
582 ret = alloc_snapshot(tr);
583 if (WARN_ON(ret < 0))
588 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
590 void tracing_snapshot(void)
592 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
594 EXPORT_SYMBOL_GPL(tracing_snapshot);
595 void tracing_snapshot_alloc(void)
600 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
601 #endif /* CONFIG_TRACER_SNAPSHOT */
603 void tracer_tracing_off(struct trace_array *tr)
605 if (tr->trace_buffer.buffer)
606 ring_buffer_record_off(tr->trace_buffer.buffer);
608 * This flag is looked at when buffers haven't been allocated
609 * yet, or by some tracers (like irqsoff), that just want to
610 * know if the ring buffer has been disabled, but it can handle
611 * races of where it gets disabled but we still do a record.
612 * As the check is in the fast path of the tracers, it is more
613 * important to be fast than accurate.
615 tr->buffer_disabled = 1;
616 /* Make the flag seen by readers */
621 * tracing_off - turn off tracing buffers
623 * This function stops the tracing buffers from recording data.
624 * It does not disable any overhead the tracers themselves may
625 * be causing. This function simply causes all recording to
626 * the ring buffers to fail.
628 void tracing_off(void)
630 tracer_tracing_off(&global_trace);
632 EXPORT_SYMBOL_GPL(tracing_off);
635 * tracer_tracing_is_on - show real state of ring buffer enabled
636 * @tr : the trace array to know if ring buffer is enabled
638 * Shows real state of the ring buffer if it is enabled or not.
640 int tracer_tracing_is_on(struct trace_array *tr)
642 if (tr->trace_buffer.buffer)
643 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
644 return !tr->buffer_disabled;
648 * tracing_is_on - show state of ring buffers enabled
650 int tracing_is_on(void)
652 return tracer_tracing_is_on(&global_trace);
654 EXPORT_SYMBOL_GPL(tracing_is_on);
656 static int __init set_buf_size(char *str)
658 unsigned long buf_size;
662 buf_size = memparse(str, &str);
663 /* nr_entries can not be zero */
666 trace_buf_size = buf_size;
669 __setup("trace_buf_size=", set_buf_size);
671 static int __init set_tracing_thresh(char *str)
673 unsigned long threshold;
678 ret = kstrtoul(str, 0, &threshold);
681 tracing_thresh = threshold * 1000;
684 __setup("tracing_thresh=", set_tracing_thresh);
686 unsigned long nsecs_to_usecs(unsigned long nsecs)
691 /* These must match the bit postions in trace_iterator_flags */
692 static const char *trace_options[] = {
725 int in_ns; /* is this clock in nanoseconds? */
727 { trace_clock_local, "local", 1 },
728 { trace_clock_global, "global", 1 },
729 { trace_clock_counter, "counter", 0 },
730 { trace_clock_jiffies, "uptime", 1 },
731 { trace_clock, "perf", 1 },
736 * trace_parser_get_init - gets the buffer for trace parser
738 int trace_parser_get_init(struct trace_parser *parser, int size)
740 memset(parser, 0, sizeof(*parser));
742 parser->buffer = kmalloc(size, GFP_KERNEL);
751 * trace_parser_put - frees the buffer for trace parser
753 void trace_parser_put(struct trace_parser *parser)
755 kfree(parser->buffer);
759 * trace_get_user - reads the user input string separated by space
760 * (matched by isspace(ch))
762 * For each string found the 'struct trace_parser' is updated,
763 * and the function returns.
765 * Returns number of bytes read.
767 * See kernel/trace/trace.h for 'struct trace_parser' details.
769 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
770 size_t cnt, loff_t *ppos)
777 trace_parser_clear(parser);
779 ret = get_user(ch, ubuf++);
787 * The parser is not finished with the last write,
788 * continue reading the user input without skipping spaces.
791 /* skip white space */
792 while (cnt && isspace(ch)) {
793 ret = get_user(ch, ubuf++);
800 /* only spaces were written */
810 /* read the non-space input */
811 while (cnt && !isspace(ch)) {
812 if (parser->idx < parser->size - 1)
813 parser->buffer[parser->idx++] = ch;
818 ret = get_user(ch, ubuf++);
825 /* We either got finished input or we have to wait for another call. */
827 parser->buffer[parser->idx] = 0;
828 parser->cont = false;
829 } else if (parser->idx < parser->size - 1) {
831 parser->buffer[parser->idx++] = ch;
844 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
852 if (s->len <= s->readpos)
855 len = s->len - s->readpos;
858 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
868 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
872 if (s->len <= s->readpos)
875 len = s->len - s->readpos;
878 memcpy(buf, s->buffer + s->readpos, cnt);
885 * ftrace_max_lock is used to protect the swapping of buffers
886 * when taking a max snapshot. The buffers themselves are
887 * protected by per_cpu spinlocks. But the action of the swap
888 * needs its own lock.
890 * This is defined as a arch_spinlock_t in order to help
891 * with performance when lockdep debugging is enabled.
893 * It is also used in other places outside the update_max_tr
894 * so it needs to be defined outside of the
895 * CONFIG_TRACER_MAX_TRACE.
897 static arch_spinlock_t ftrace_max_lock =
898 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
900 unsigned long __read_mostly tracing_thresh;
902 #ifdef CONFIG_TRACER_MAX_TRACE
903 unsigned long __read_mostly tracing_max_latency;
906 * Copy the new maximum trace into the separate maximum-trace
907 * structure. (this way the maximum trace is permanently saved,
908 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
911 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
913 struct trace_buffer *trace_buf = &tr->trace_buffer;
914 struct trace_buffer *max_buf = &tr->max_buffer;
915 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
916 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
919 max_buf->time_start = data->preempt_timestamp;
921 max_data->saved_latency = tracing_max_latency;
922 max_data->critical_start = data->critical_start;
923 max_data->critical_end = data->critical_end;
925 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
926 max_data->pid = tsk->pid;
928 * If tsk == current, then use current_uid(), as that does not use
929 * RCU. The irq tracer can be called out of RCU scope.
932 max_data->uid = current_uid();
934 max_data->uid = task_uid(tsk);
936 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
937 max_data->policy = tsk->policy;
938 max_data->rt_priority = tsk->rt_priority;
940 /* record this tasks comm */
941 tracing_record_cmdline(tsk);
945 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
947 * @tsk: the task with the latency
948 * @cpu: The cpu that initiated the trace.
950 * Flip the buffers between the @tr and the max_tr and record information
951 * about which task was the cause of this latency.
954 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
956 struct ring_buffer *buf;
961 WARN_ON_ONCE(!irqs_disabled());
963 if (!tr->allocated_snapshot) {
964 /* Only the nop tracer should hit this when disabling */
965 WARN_ON_ONCE(tr->current_trace != &nop_trace);
969 arch_spin_lock(&ftrace_max_lock);
971 buf = tr->trace_buffer.buffer;
972 tr->trace_buffer.buffer = tr->max_buffer.buffer;
973 tr->max_buffer.buffer = buf;
975 __update_max_tr(tr, tsk, cpu);
976 arch_spin_unlock(&ftrace_max_lock);
980 * update_max_tr_single - only copy one trace over, and reset the rest
982 * @tsk - task with the latency
983 * @cpu - the cpu of the buffer to copy.
985 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
988 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
995 WARN_ON_ONCE(!irqs_disabled());
996 if (!tr->allocated_snapshot) {
997 /* Only the nop tracer should hit this when disabling */
998 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1002 arch_spin_lock(&ftrace_max_lock);
1004 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
1006 if (ret == -EBUSY) {
1008 * We failed to swap the buffer due to a commit taking
1009 * place on this CPU. We fail to record, but we reset
1010 * the max trace buffer (no one writes directly to it)
1011 * and flag that it failed.
1013 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
1014 "Failed to swap buffers due to commit in progress\n");
1017 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
1019 __update_max_tr(tr, tsk, cpu);
1020 arch_spin_unlock(&ftrace_max_lock);
1022 #endif /* CONFIG_TRACER_MAX_TRACE */
1024 static void default_wait_pipe(struct trace_iterator *iter)
1026 /* Iterators are static, they should be filled or empty */
1027 if (trace_buffer_iter(iter, iter->cpu_file))
1030 ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
1033 #ifdef CONFIG_FTRACE_STARTUP_TEST
1034 static int run_tracer_selftest(struct tracer *type)
1036 struct trace_array *tr = &global_trace;
1037 struct tracer *saved_tracer = tr->current_trace;
1040 if (!type->selftest || tracing_selftest_disabled)
1044 * Run a selftest on this tracer.
1045 * Here we reset the trace buffer, and set the current
1046 * tracer to be this tracer. The tracer can then run some
1047 * internal tracing to verify that everything is in order.
1048 * If we fail, we do not register this tracer.
1050 tracing_reset_online_cpus(&tr->trace_buffer);
1052 tr->current_trace = type;
1054 #ifdef CONFIG_TRACER_MAX_TRACE
1055 if (type->use_max_tr) {
1056 /* If we expanded the buffers, make sure the max is expanded too */
1057 if (ring_buffer_expanded)
1058 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1059 RING_BUFFER_ALL_CPUS);
1060 tr->allocated_snapshot = true;
1064 /* the test is responsible for initializing and enabling */
1065 pr_info("Testing tracer %s: ", type->name);
1066 ret = type->selftest(type, tr);
1067 /* the test is responsible for resetting too */
1068 tr->current_trace = saved_tracer;
1070 printk(KERN_CONT "FAILED!\n");
1071 /* Add the warning after printing 'FAILED' */
1075 /* Only reset on passing, to avoid touching corrupted buffers */
1076 tracing_reset_online_cpus(&tr->trace_buffer);
1078 #ifdef CONFIG_TRACER_MAX_TRACE
1079 if (type->use_max_tr) {
1080 tr->allocated_snapshot = false;
1082 /* Shrink the max buffer again */
1083 if (ring_buffer_expanded)
1084 ring_buffer_resize(tr->max_buffer.buffer, 1,
1085 RING_BUFFER_ALL_CPUS);
1089 printk(KERN_CONT "PASSED\n");
1093 static inline int run_tracer_selftest(struct tracer *type)
1097 #endif /* CONFIG_FTRACE_STARTUP_TEST */
1100 * register_tracer - register a tracer with the ftrace system.
1101 * @type - the plugin for the tracer
1103 * Register a new plugin tracer.
1105 int register_tracer(struct tracer *type)
1111 pr_info("Tracer must have a name\n");
1115 if (strlen(type->name) >= MAX_TRACER_SIZE) {
1116 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1120 mutex_lock(&trace_types_lock);
1122 tracing_selftest_running = true;
1124 for (t = trace_types; t; t = t->next) {
1125 if (strcmp(type->name, t->name) == 0) {
1127 pr_info("Tracer %s already registered\n",
1134 if (!type->set_flag)
1135 type->set_flag = &dummy_set_flag;
1137 type->flags = &dummy_tracer_flags;
1139 if (!type->flags->opts)
1140 type->flags->opts = dummy_tracer_opt;
1141 if (!type->wait_pipe)
1142 type->wait_pipe = default_wait_pipe;
1144 ret = run_tracer_selftest(type);
1148 type->next = trace_types;
1152 tracing_selftest_running = false;
1153 mutex_unlock(&trace_types_lock);
1155 if (ret || !default_bootup_tracer)
1158 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
1161 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1162 /* Do we want this tracer to start on bootup? */
1163 tracing_set_tracer(type->name);
1164 default_bootup_tracer = NULL;
1165 /* disable other selftests, since this will break it. */
1166 tracing_selftest_disabled = true;
1167 #ifdef CONFIG_FTRACE_STARTUP_TEST
1168 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1176 void tracing_reset(struct trace_buffer *buf, int cpu)
1178 struct ring_buffer *buffer = buf->buffer;
1183 ring_buffer_record_disable(buffer);
1185 /* Make sure all commits have finished */
1186 synchronize_sched();
1187 ring_buffer_reset_cpu(buffer, cpu);
1189 ring_buffer_record_enable(buffer);
1192 void tracing_reset_online_cpus(struct trace_buffer *buf)
1194 struct ring_buffer *buffer = buf->buffer;
1200 ring_buffer_record_disable(buffer);
1202 /* Make sure all commits have finished */
1203 synchronize_sched();
1205 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
1207 for_each_online_cpu(cpu)
1208 ring_buffer_reset_cpu(buffer, cpu);
1210 ring_buffer_record_enable(buffer);
1213 /* Must have trace_types_lock held */
1214 void tracing_reset_all_online_cpus(void)
1216 struct trace_array *tr;
1218 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1219 tracing_reset_online_cpus(&tr->trace_buffer);
1220 #ifdef CONFIG_TRACER_MAX_TRACE
1221 tracing_reset_online_cpus(&tr->max_buffer);
1226 #define SAVED_CMDLINES 128
1227 #define NO_CMDLINE_MAP UINT_MAX
1228 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1229 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1230 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1231 static int cmdline_idx;
1232 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1234 /* temporary disable recording */
1235 static atomic_t trace_record_cmdline_disabled __read_mostly;
1237 static void trace_init_cmdlines(void)
1239 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1240 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
1244 int is_tracing_stopped(void)
1246 return global_trace.stop_count;
1250 * ftrace_off_permanent - disable all ftrace code permanently
1252 * This should only be called when a serious anomally has
1253 * been detected. This will turn off the function tracing,
1254 * ring buffers, and other tracing utilites. It takes no
1255 * locks and can be called from any context.
1257 void ftrace_off_permanent(void)
1259 tracing_disabled = 1;
1261 tracing_off_permanent();
1265 * tracing_start - quick start of the tracer
1267 * If tracing is enabled but was stopped by tracing_stop,
1268 * this will start the tracer back up.
1270 void tracing_start(void)
1272 struct ring_buffer *buffer;
1273 unsigned long flags;
1275 if (tracing_disabled)
1278 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1279 if (--global_trace.stop_count) {
1280 if (global_trace.stop_count < 0) {
1281 /* Someone screwed up their debugging */
1283 global_trace.stop_count = 0;
1288 /* Prevent the buffers from switching */
1289 arch_spin_lock(&ftrace_max_lock);
1291 buffer = global_trace.trace_buffer.buffer;
1293 ring_buffer_record_enable(buffer);
1295 #ifdef CONFIG_TRACER_MAX_TRACE
1296 buffer = global_trace.max_buffer.buffer;
1298 ring_buffer_record_enable(buffer);
1301 arch_spin_unlock(&ftrace_max_lock);
1305 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1308 static void tracing_start_tr(struct trace_array *tr)
1310 struct ring_buffer *buffer;
1311 unsigned long flags;
1313 if (tracing_disabled)
1316 /* If global, we need to also start the max tracer */
1317 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1318 return tracing_start();
1320 raw_spin_lock_irqsave(&tr->start_lock, flags);
1322 if (--tr->stop_count) {
1323 if (tr->stop_count < 0) {
1324 /* Someone screwed up their debugging */
1331 buffer = tr->trace_buffer.buffer;
1333 ring_buffer_record_enable(buffer);
1336 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1340 * tracing_stop - quick stop of the tracer
1342 * Light weight way to stop tracing. Use in conjunction with
1345 void tracing_stop(void)
1347 struct ring_buffer *buffer;
1348 unsigned long flags;
1351 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1352 if (global_trace.stop_count++)
1355 /* Prevent the buffers from switching */
1356 arch_spin_lock(&ftrace_max_lock);
1358 buffer = global_trace.trace_buffer.buffer;
1360 ring_buffer_record_disable(buffer);
1362 #ifdef CONFIG_TRACER_MAX_TRACE
1363 buffer = global_trace.max_buffer.buffer;
1365 ring_buffer_record_disable(buffer);
1368 arch_spin_unlock(&ftrace_max_lock);
1371 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1374 static void tracing_stop_tr(struct trace_array *tr)
1376 struct ring_buffer *buffer;
1377 unsigned long flags;
1379 /* If global, we need to also stop the max tracer */
1380 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1381 return tracing_stop();
1383 raw_spin_lock_irqsave(&tr->start_lock, flags);
1384 if (tr->stop_count++)
1387 buffer = tr->trace_buffer.buffer;
1389 ring_buffer_record_disable(buffer);
1392 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1395 void trace_stop_cmdline_recording(void);
1397 static void trace_save_cmdline(struct task_struct *tsk)
1401 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1405 * It's not the end of the world if we don't get
1406 * the lock, but we also don't want to spin
1407 * nor do we want to disable interrupts,
1408 * so if we miss here, then better luck next time.
1410 if (!arch_spin_trylock(&trace_cmdline_lock))
1413 idx = map_pid_to_cmdline[tsk->pid];
1414 if (idx == NO_CMDLINE_MAP) {
1415 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1418 * Check whether the cmdline buffer at idx has a pid
1419 * mapped. We are going to overwrite that entry so we
1420 * need to clear the map_pid_to_cmdline. Otherwise we
1421 * would read the new comm for the old pid.
1423 pid = map_cmdline_to_pid[idx];
1424 if (pid != NO_CMDLINE_MAP)
1425 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1427 map_cmdline_to_pid[idx] = tsk->pid;
1428 map_pid_to_cmdline[tsk->pid] = idx;
1433 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1435 arch_spin_unlock(&trace_cmdline_lock);
1438 void trace_find_cmdline(int pid, char comm[])
1443 strcpy(comm, "<idle>");
1447 if (WARN_ON_ONCE(pid < 0)) {
1448 strcpy(comm, "<XXX>");
1452 if (pid > PID_MAX_DEFAULT) {
1453 strcpy(comm, "<...>");
1458 arch_spin_lock(&trace_cmdline_lock);
1459 map = map_pid_to_cmdline[pid];
1460 if (map != NO_CMDLINE_MAP)
1461 strcpy(comm, saved_cmdlines[map]);
1463 strcpy(comm, "<...>");
1465 arch_spin_unlock(&trace_cmdline_lock);
1469 void tracing_record_cmdline(struct task_struct *tsk)
1471 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1474 if (!__this_cpu_read(trace_cmdline_save))
1477 __this_cpu_write(trace_cmdline_save, false);
1479 trace_save_cmdline(tsk);
1483 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1486 struct task_struct *tsk = current;
1488 entry->preempt_count = pc & 0xff;
1489 entry->pid = (tsk) ? tsk->pid : 0;
1491 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1492 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1494 TRACE_FLAG_IRQS_NOSUPPORT |
1496 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1497 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1498 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1500 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1502 struct ring_buffer_event *
1503 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1506 unsigned long flags, int pc)
1508 struct ring_buffer_event *event;
1510 event = ring_buffer_lock_reserve(buffer, len);
1511 if (event != NULL) {
1512 struct trace_entry *ent = ring_buffer_event_data(event);
1514 tracing_generic_entry_update(ent, flags, pc);
1522 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1524 __this_cpu_write(trace_cmdline_save, true);
1525 ring_buffer_unlock_commit(buffer, event);
1529 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1530 struct ring_buffer_event *event,
1531 unsigned long flags, int pc)
1533 __buffer_unlock_commit(buffer, event);
1535 ftrace_trace_stack(buffer, flags, 6, pc);
1536 ftrace_trace_userstack(buffer, flags, pc);
1539 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1540 struct ring_buffer_event *event,
1541 unsigned long flags, int pc)
1543 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1545 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1547 struct ring_buffer_event *
1548 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1549 struct ftrace_event_file *ftrace_file,
1550 int type, unsigned long len,
1551 unsigned long flags, int pc)
1553 *current_rb = ftrace_file->tr->trace_buffer.buffer;
1554 return trace_buffer_lock_reserve(*current_rb,
1555 type, len, flags, pc);
1557 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1559 struct ring_buffer_event *
1560 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1561 int type, unsigned long len,
1562 unsigned long flags, int pc)
1564 *current_rb = global_trace.trace_buffer.buffer;
1565 return trace_buffer_lock_reserve(*current_rb,
1566 type, len, flags, pc);
1568 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1570 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1571 struct ring_buffer_event *event,
1572 unsigned long flags, int pc)
1574 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1576 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1578 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1579 struct ring_buffer_event *event,
1580 unsigned long flags, int pc,
1581 struct pt_regs *regs)
1583 __buffer_unlock_commit(buffer, event);
1585 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1586 ftrace_trace_userstack(buffer, flags, pc);
1588 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1590 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1591 struct ring_buffer_event *event)
1593 ring_buffer_discard_commit(buffer, event);
1595 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1598 trace_function(struct trace_array *tr,
1599 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1602 struct ftrace_event_call *call = &event_function;
1603 struct ring_buffer *buffer = tr->trace_buffer.buffer;
1604 struct ring_buffer_event *event;
1605 struct ftrace_entry *entry;
1607 /* If we are reading the ring buffer, don't trace */
1608 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1611 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1615 entry = ring_buffer_event_data(event);
1617 entry->parent_ip = parent_ip;
1619 if (!filter_check_discard(call, entry, buffer, event))
1620 __buffer_unlock_commit(buffer, event);
1624 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1625 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1628 if (likely(!atomic_read(&data->disabled)))
1629 trace_function(tr, ip, parent_ip, flags, pc);
1632 #ifdef CONFIG_STACKTRACE
1634 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1635 struct ftrace_stack {
1636 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1639 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1640 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1642 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1643 unsigned long flags,
1644 int skip, int pc, struct pt_regs *regs)
1646 struct ftrace_event_call *call = &event_kernel_stack;
1647 struct ring_buffer_event *event;
1648 struct stack_entry *entry;
1649 struct stack_trace trace;
1651 int size = FTRACE_STACK_ENTRIES;
1653 trace.nr_entries = 0;
1657 * Since events can happen in NMIs there's no safe way to
1658 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1659 * or NMI comes in, it will just have to use the default
1660 * FTRACE_STACK_SIZE.
1662 preempt_disable_notrace();
1664 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1666 * We don't need any atomic variables, just a barrier.
1667 * If an interrupt comes in, we don't care, because it would
1668 * have exited and put the counter back to what we want.
1669 * We just need a barrier to keep gcc from moving things
1673 if (use_stack == 1) {
1674 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1675 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1678 save_stack_trace_regs(regs, &trace);
1680 save_stack_trace(&trace);
1682 if (trace.nr_entries > size)
1683 size = trace.nr_entries;
1685 /* From now on, use_stack is a boolean */
1688 size *= sizeof(unsigned long);
1690 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1691 sizeof(*entry) + size, flags, pc);
1694 entry = ring_buffer_event_data(event);
1696 memset(&entry->caller, 0, size);
1699 memcpy(&entry->caller, trace.entries,
1700 trace.nr_entries * sizeof(unsigned long));
1702 trace.max_entries = FTRACE_STACK_ENTRIES;
1703 trace.entries = entry->caller;
1705 save_stack_trace_regs(regs, &trace);
1707 save_stack_trace(&trace);
1710 entry->size = trace.nr_entries;
1712 if (!filter_check_discard(call, entry, buffer, event))
1713 __buffer_unlock_commit(buffer, event);
1716 /* Again, don't let gcc optimize things here */
1718 __this_cpu_dec(ftrace_stack_reserve);
1719 preempt_enable_notrace();
1723 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1724 int skip, int pc, struct pt_regs *regs)
1726 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1729 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1732 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1735 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1738 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1741 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1744 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
1748 * trace_dump_stack - record a stack back trace in the trace buffer
1749 * @skip: Number of functions to skip (helper handlers)
1751 void trace_dump_stack(int skip)
1753 unsigned long flags;
1755 if (tracing_disabled || tracing_selftest_running)
1758 local_save_flags(flags);
1761 * Skip 3 more, seems to get us at the caller of
1765 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1766 flags, skip, preempt_count(), NULL);
1769 static DEFINE_PER_CPU(int, user_stack_count);
1772 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1774 struct ftrace_event_call *call = &event_user_stack;
1775 struct ring_buffer_event *event;
1776 struct userstack_entry *entry;
1777 struct stack_trace trace;
1779 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1783 * NMIs can not handle page faults, even with fix ups.
1784 * The save user stack can (and often does) fault.
1786 if (unlikely(in_nmi()))
1790 * prevent recursion, since the user stack tracing may
1791 * trigger other kernel events.
1794 if (__this_cpu_read(user_stack_count))
1797 __this_cpu_inc(user_stack_count);
1799 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1800 sizeof(*entry), flags, pc);
1802 goto out_drop_count;
1803 entry = ring_buffer_event_data(event);
1805 entry->tgid = current->tgid;
1806 memset(&entry->caller, 0, sizeof(entry->caller));
1808 trace.nr_entries = 0;
1809 trace.max_entries = FTRACE_STACK_ENTRIES;
1811 trace.entries = entry->caller;
1813 save_stack_trace_user(&trace);
1814 if (!filter_check_discard(call, entry, buffer, event))
1815 __buffer_unlock_commit(buffer, event);
1818 __this_cpu_dec(user_stack_count);
1824 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1826 ftrace_trace_userstack(tr, flags, preempt_count());
1830 #endif /* CONFIG_STACKTRACE */
1832 /* created for use with alloc_percpu */
1833 struct trace_buffer_struct {
1834 char buffer[TRACE_BUF_SIZE];
1837 static struct trace_buffer_struct *trace_percpu_buffer;
1838 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1839 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1840 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1843 * The buffer used is dependent on the context. There is a per cpu
1844 * buffer for normal context, softirq contex, hard irq context and
1845 * for NMI context. Thise allows for lockless recording.
1847 * Note, if the buffers failed to be allocated, then this returns NULL
1849 static char *get_trace_buf(void)
1851 struct trace_buffer_struct *percpu_buffer;
1854 * If we have allocated per cpu buffers, then we do not
1855 * need to do any locking.
1858 percpu_buffer = trace_percpu_nmi_buffer;
1860 percpu_buffer = trace_percpu_irq_buffer;
1861 else if (in_softirq())
1862 percpu_buffer = trace_percpu_sirq_buffer;
1864 percpu_buffer = trace_percpu_buffer;
1869 return this_cpu_ptr(&percpu_buffer->buffer[0]);
1872 static int alloc_percpu_trace_buffer(void)
1874 struct trace_buffer_struct *buffers;
1875 struct trace_buffer_struct *sirq_buffers;
1876 struct trace_buffer_struct *irq_buffers;
1877 struct trace_buffer_struct *nmi_buffers;
1879 buffers = alloc_percpu(struct trace_buffer_struct);
1883 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1887 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1891 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1895 trace_percpu_buffer = buffers;
1896 trace_percpu_sirq_buffer = sirq_buffers;
1897 trace_percpu_irq_buffer = irq_buffers;
1898 trace_percpu_nmi_buffer = nmi_buffers;
1903 free_percpu(irq_buffers);
1905 free_percpu(sirq_buffers);
1907 free_percpu(buffers);
1909 WARN(1, "Could not allocate percpu trace_printk buffer");
1913 static int buffers_allocated;
1915 void trace_printk_init_buffers(void)
1917 if (buffers_allocated)
1920 if (alloc_percpu_trace_buffer())
1923 pr_info("ftrace: Allocated trace_printk buffers\n");
1925 /* Expand the buffers to set size */
1926 tracing_update_buffers();
1928 buffers_allocated = 1;
1931 * trace_printk_init_buffers() can be called by modules.
1932 * If that happens, then we need to start cmdline recording
1933 * directly here. If the global_trace.buffer is already
1934 * allocated here, then this was called by module code.
1936 if (global_trace.trace_buffer.buffer)
1937 tracing_start_cmdline_record();
1940 void trace_printk_start_comm(void)
1942 /* Start tracing comms if trace printk is set */
1943 if (!buffers_allocated)
1945 tracing_start_cmdline_record();
1948 static void trace_printk_start_stop_comm(int enabled)
1950 if (!buffers_allocated)
1954 tracing_start_cmdline_record();
1956 tracing_stop_cmdline_record();
1960 * trace_vbprintk - write binary msg to tracing buffer
1963 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1965 struct ftrace_event_call *call = &event_bprint;
1966 struct ring_buffer_event *event;
1967 struct ring_buffer *buffer;
1968 struct trace_array *tr = &global_trace;
1969 struct bprint_entry *entry;
1970 unsigned long flags;
1972 int len = 0, size, pc;
1974 if (unlikely(tracing_selftest_running || tracing_disabled))
1977 /* Don't pollute graph traces with trace_vprintk internals */
1978 pause_graph_tracing();
1980 pc = preempt_count();
1981 preempt_disable_notrace();
1983 tbuffer = get_trace_buf();
1989 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1991 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1994 local_save_flags(flags);
1995 size = sizeof(*entry) + sizeof(u32) * len;
1996 buffer = tr->trace_buffer.buffer;
1997 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
2001 entry = ring_buffer_event_data(event);
2005 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
2006 if (!filter_check_discard(call, entry, buffer, event)) {
2007 __buffer_unlock_commit(buffer, event);
2008 ftrace_trace_stack(buffer, flags, 6, pc);
2012 preempt_enable_notrace();
2013 unpause_graph_tracing();
2017 EXPORT_SYMBOL_GPL(trace_vbprintk);
2020 __trace_array_vprintk(struct ring_buffer *buffer,
2021 unsigned long ip, const char *fmt, va_list args)
2023 struct ftrace_event_call *call = &event_print;
2024 struct ring_buffer_event *event;
2025 int len = 0, size, pc;
2026 struct print_entry *entry;
2027 unsigned long flags;
2030 if (tracing_disabled || tracing_selftest_running)
2033 /* Don't pollute graph traces with trace_vprintk internals */
2034 pause_graph_tracing();
2036 pc = preempt_count();
2037 preempt_disable_notrace();
2040 tbuffer = get_trace_buf();
2046 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
2047 if (len > TRACE_BUF_SIZE)
2050 local_save_flags(flags);
2051 size = sizeof(*entry) + len + 1;
2052 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
2056 entry = ring_buffer_event_data(event);
2059 memcpy(&entry->buf, tbuffer, len);
2060 entry->buf[len] = '\0';
2061 if (!filter_check_discard(call, entry, buffer, event)) {
2062 __buffer_unlock_commit(buffer, event);
2063 ftrace_trace_stack(buffer, flags, 6, pc);
2066 preempt_enable_notrace();
2067 unpause_graph_tracing();
2072 int trace_array_vprintk(struct trace_array *tr,
2073 unsigned long ip, const char *fmt, va_list args)
2075 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2078 int trace_array_printk(struct trace_array *tr,
2079 unsigned long ip, const char *fmt, ...)
2084 if (!(trace_flags & TRACE_ITER_PRINTK))
2088 ret = trace_array_vprintk(tr, ip, fmt, ap);
2093 int trace_array_printk_buf(struct ring_buffer *buffer,
2094 unsigned long ip, const char *fmt, ...)
2099 if (!(trace_flags & TRACE_ITER_PRINTK))
2103 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2108 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2110 return trace_array_vprintk(&global_trace, ip, fmt, args);
2112 EXPORT_SYMBOL_GPL(trace_vprintk);
2114 static void trace_iterator_increment(struct trace_iterator *iter)
2116 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2120 ring_buffer_read(buf_iter, NULL);
2123 static struct trace_entry *
2124 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2125 unsigned long *lost_events)
2127 struct ring_buffer_event *event;
2128 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
2131 event = ring_buffer_iter_peek(buf_iter, ts);
2133 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
2137 iter->ent_size = ring_buffer_event_length(event);
2138 return ring_buffer_event_data(event);
2144 static struct trace_entry *
2145 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2146 unsigned long *missing_events, u64 *ent_ts)
2148 struct ring_buffer *buffer = iter->trace_buffer->buffer;
2149 struct trace_entry *ent, *next = NULL;
2150 unsigned long lost_events = 0, next_lost = 0;
2151 int cpu_file = iter->cpu_file;
2152 u64 next_ts = 0, ts;
2158 * If we are in a per_cpu trace file, don't bother by iterating over
2159 * all cpu and peek directly.
2161 if (cpu_file > RING_BUFFER_ALL_CPUS) {
2162 if (ring_buffer_empty_cpu(buffer, cpu_file))
2164 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
2166 *ent_cpu = cpu_file;
2171 for_each_tracing_cpu(cpu) {
2173 if (ring_buffer_empty_cpu(buffer, cpu))
2176 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
2179 * Pick the entry with the smallest timestamp:
2181 if (ent && (!next || ts < next_ts)) {
2185 next_lost = lost_events;
2186 next_size = iter->ent_size;
2190 iter->ent_size = next_size;
2193 *ent_cpu = next_cpu;
2199 *missing_events = next_lost;
2204 /* Find the next real entry, without updating the iterator itself */
2205 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2206 int *ent_cpu, u64 *ent_ts)
2208 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
2211 /* Find the next real entry, and increment the iterator to the next entry */
2212 void *trace_find_next_entry_inc(struct trace_iterator *iter)
2214 iter->ent = __find_next_entry(iter, &iter->cpu,
2215 &iter->lost_events, &iter->ts);
2218 trace_iterator_increment(iter);
2220 return iter->ent ? iter : NULL;
2223 static void trace_consume(struct trace_iterator *iter)
2225 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
2226 &iter->lost_events);
2229 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
2231 struct trace_iterator *iter = m->private;
2235 WARN_ON_ONCE(iter->leftover);
2239 /* can't go backwards */
2244 ent = trace_find_next_entry_inc(iter);
2248 while (ent && iter->idx < i)
2249 ent = trace_find_next_entry_inc(iter);
2256 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2258 struct ring_buffer_event *event;
2259 struct ring_buffer_iter *buf_iter;
2260 unsigned long entries = 0;
2263 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2265 buf_iter = trace_buffer_iter(iter, cpu);
2269 ring_buffer_iter_reset(buf_iter);
2272 * We could have the case with the max latency tracers
2273 * that a reset never took place on a cpu. This is evident
2274 * by the timestamp being before the start of the buffer.
2276 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
2277 if (ts >= iter->trace_buffer->time_start)
2280 ring_buffer_read(buf_iter, NULL);
2283 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2287 * The current tracer is copied to avoid a global locking
2290 static void *s_start(struct seq_file *m, loff_t *pos)
2292 struct trace_iterator *iter = m->private;
2293 struct trace_array *tr = iter->tr;
2294 int cpu_file = iter->cpu_file;
2300 * copy the tracer to avoid using a global lock all around.
2301 * iter->trace is a copy of current_trace, the pointer to the
2302 * name may be used instead of a strcmp(), as iter->trace->name
2303 * will point to the same string as current_trace->name.
2305 mutex_lock(&trace_types_lock);
2306 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2307 *iter->trace = *tr->current_trace;
2308 mutex_unlock(&trace_types_lock);
2310 #ifdef CONFIG_TRACER_MAX_TRACE
2311 if (iter->snapshot && iter->trace->use_max_tr)
2312 return ERR_PTR(-EBUSY);
2315 if (!iter->snapshot)
2316 atomic_inc(&trace_record_cmdline_disabled);
2318 if (*pos != iter->pos) {
2323 if (cpu_file == RING_BUFFER_ALL_CPUS) {
2324 for_each_tracing_cpu(cpu)
2325 tracing_iter_reset(iter, cpu);
2327 tracing_iter_reset(iter, cpu_file);
2330 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2335 * If we overflowed the seq_file before, then we want
2336 * to just reuse the trace_seq buffer again.
2342 p = s_next(m, p, &l);
2346 trace_event_read_lock();
2347 trace_access_lock(cpu_file);
2351 static void s_stop(struct seq_file *m, void *p)
2353 struct trace_iterator *iter = m->private;
2355 #ifdef CONFIG_TRACER_MAX_TRACE
2356 if (iter->snapshot && iter->trace->use_max_tr)
2360 if (!iter->snapshot)
2361 atomic_dec(&trace_record_cmdline_disabled);
2363 trace_access_unlock(iter->cpu_file);
2364 trace_event_read_unlock();
2368 get_total_entries(struct trace_buffer *buf,
2369 unsigned long *total, unsigned long *entries)
2371 unsigned long count;
2377 for_each_tracing_cpu(cpu) {
2378 count = ring_buffer_entries_cpu(buf->buffer, cpu);
2380 * If this buffer has skipped entries, then we hold all
2381 * entries for the trace and we need to ignore the
2382 * ones before the time stamp.
2384 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2385 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
2386 /* total is the same as the entries */
2390 ring_buffer_overrun_cpu(buf->buffer, cpu);
2395 static void print_lat_help_header(struct seq_file *m)
2397 seq_puts(m, "# _------=> CPU# \n");
2398 seq_puts(m, "# / _-----=> irqs-off \n");
2399 seq_puts(m, "# | / _----=> need-resched \n");
2400 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2401 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2402 seq_puts(m, "# |||| / delay \n");
2403 seq_puts(m, "# cmd pid ||||| time | caller \n");
2404 seq_puts(m, "# \\ / ||||| \\ | / \n");
2407 static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
2409 unsigned long total;
2410 unsigned long entries;
2412 get_total_entries(buf, &total, &entries);
2413 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2414 entries, total, num_online_cpus());
2418 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
2420 print_event_info(buf, m);
2421 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2422 seq_puts(m, "# | | | | |\n");
2425 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
2427 print_event_info(buf, m);
2428 seq_puts(m, "# _-----=> irqs-off\n");
2429 seq_puts(m, "# / _----=> need-resched\n");
2430 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2431 seq_puts(m, "# || / _--=> preempt-depth\n");
2432 seq_puts(m, "# ||| / delay\n");
2433 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2434 seq_puts(m, "# | | | |||| | |\n");
2438 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2440 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2441 struct trace_buffer *buf = iter->trace_buffer;
2442 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2443 struct tracer *type = iter->trace;
2444 unsigned long entries;
2445 unsigned long total;
2446 const char *name = "preemption";
2450 get_total_entries(buf, &total, &entries);
2452 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2454 seq_puts(m, "# -----------------------------------"
2455 "---------------------------------\n");
2456 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2457 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2458 nsecs_to_usecs(data->saved_latency),
2462 #if defined(CONFIG_PREEMPT_NONE)
2464 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2466 #elif defined(CONFIG_PREEMPT)
2471 /* These are reserved for later use */
2474 seq_printf(m, " #P:%d)\n", num_online_cpus());
2478 seq_puts(m, "# -----------------\n");
2479 seq_printf(m, "# | task: %.16s-%d "
2480 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2481 data->comm, data->pid,
2482 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2483 data->policy, data->rt_priority);
2484 seq_puts(m, "# -----------------\n");
2486 if (data->critical_start) {
2487 seq_puts(m, "# => started at: ");
2488 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2489 trace_print_seq(m, &iter->seq);
2490 seq_puts(m, "\n# => ended at: ");
2491 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2492 trace_print_seq(m, &iter->seq);
2493 seq_puts(m, "\n#\n");
2499 static void test_cpu_buff_start(struct trace_iterator *iter)
2501 struct trace_seq *s = &iter->seq;
2503 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2506 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2509 if (cpumask_test_cpu(iter->cpu, iter->started))
2512 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2515 cpumask_set_cpu(iter->cpu, iter->started);
2517 /* Don't print started cpu buffer for the first entry of the trace */
2519 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2523 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2525 struct trace_seq *s = &iter->seq;
2526 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2527 struct trace_entry *entry;
2528 struct trace_event *event;
2532 test_cpu_buff_start(iter);
2534 event = ftrace_find_event(entry->type);
2536 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2537 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2538 if (!trace_print_lat_context(iter))
2541 if (!trace_print_context(iter))
2547 return event->funcs->trace(iter, sym_flags, event);
2549 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2552 return TRACE_TYPE_HANDLED;
2554 return TRACE_TYPE_PARTIAL_LINE;
2557 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2559 struct trace_seq *s = &iter->seq;
2560 struct trace_entry *entry;
2561 struct trace_event *event;
2565 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2566 if (!trace_seq_printf(s, "%d %d %llu ",
2567 entry->pid, iter->cpu, iter->ts))
2571 event = ftrace_find_event(entry->type);
2573 return event->funcs->raw(iter, 0, event);
2575 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2578 return TRACE_TYPE_HANDLED;
2580 return TRACE_TYPE_PARTIAL_LINE;
2583 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2585 struct trace_seq *s = &iter->seq;
2586 unsigned char newline = '\n';
2587 struct trace_entry *entry;
2588 struct trace_event *event;
2592 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2593 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2594 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2595 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2598 event = ftrace_find_event(entry->type);
2600 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2601 if (ret != TRACE_TYPE_HANDLED)
2605 SEQ_PUT_FIELD_RET(s, newline);
2607 return TRACE_TYPE_HANDLED;
2610 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2612 struct trace_seq *s = &iter->seq;
2613 struct trace_entry *entry;
2614 struct trace_event *event;
2618 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2619 SEQ_PUT_FIELD_RET(s, entry->pid);
2620 SEQ_PUT_FIELD_RET(s, iter->cpu);
2621 SEQ_PUT_FIELD_RET(s, iter->ts);
2624 event = ftrace_find_event(entry->type);
2625 return event ? event->funcs->binary(iter, 0, event) :
2629 int trace_empty(struct trace_iterator *iter)
2631 struct ring_buffer_iter *buf_iter;
2634 /* If we are looking at one CPU buffer, only check that one */
2635 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2636 cpu = iter->cpu_file;
2637 buf_iter = trace_buffer_iter(iter, cpu);
2639 if (!ring_buffer_iter_empty(buf_iter))
2642 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2648 for_each_tracing_cpu(cpu) {
2649 buf_iter = trace_buffer_iter(iter, cpu);
2651 if (!ring_buffer_iter_empty(buf_iter))
2654 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2662 /* Called with trace_event_read_lock() held. */
2663 enum print_line_t print_trace_line(struct trace_iterator *iter)
2665 enum print_line_t ret;
2667 if (iter->lost_events &&
2668 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2669 iter->cpu, iter->lost_events))
2670 return TRACE_TYPE_PARTIAL_LINE;
2672 if (iter->trace && iter->trace->print_line) {
2673 ret = iter->trace->print_line(iter);
2674 if (ret != TRACE_TYPE_UNHANDLED)
2678 if (iter->ent->type == TRACE_BPUTS &&
2679 trace_flags & TRACE_ITER_PRINTK &&
2680 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2681 return trace_print_bputs_msg_only(iter);
2683 if (iter->ent->type == TRACE_BPRINT &&
2684 trace_flags & TRACE_ITER_PRINTK &&
2685 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2686 return trace_print_bprintk_msg_only(iter);
2688 if (iter->ent->type == TRACE_PRINT &&
2689 trace_flags & TRACE_ITER_PRINTK &&
2690 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2691 return trace_print_printk_msg_only(iter);
2693 if (trace_flags & TRACE_ITER_BIN)
2694 return print_bin_fmt(iter);
2696 if (trace_flags & TRACE_ITER_HEX)
2697 return print_hex_fmt(iter);
2699 if (trace_flags & TRACE_ITER_RAW)
2700 return print_raw_fmt(iter);
2702 return print_trace_fmt(iter);
2705 void trace_latency_header(struct seq_file *m)
2707 struct trace_iterator *iter = m->private;
2709 /* print nothing if the buffers are empty */
2710 if (trace_empty(iter))
2713 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2714 print_trace_header(m, iter);
2716 if (!(trace_flags & TRACE_ITER_VERBOSE))
2717 print_lat_help_header(m);
2720 void trace_default_header(struct seq_file *m)
2722 struct trace_iterator *iter = m->private;
2724 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2727 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2728 /* print nothing if the buffers are empty */
2729 if (trace_empty(iter))
2731 print_trace_header(m, iter);
2732 if (!(trace_flags & TRACE_ITER_VERBOSE))
2733 print_lat_help_header(m);
2735 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2736 if (trace_flags & TRACE_ITER_IRQ_INFO)
2737 print_func_help_header_irq(iter->trace_buffer, m);
2739 print_func_help_header(iter->trace_buffer, m);
2744 static void test_ftrace_alive(struct seq_file *m)
2746 if (!ftrace_is_dead())
2748 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2749 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2752 #ifdef CONFIG_TRACER_MAX_TRACE
2753 static void show_snapshot_main_help(struct seq_file *m)
2755 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2756 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2757 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2758 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2759 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2760 seq_printf(m, "# is not a '0' or '1')\n");
2763 static void show_snapshot_percpu_help(struct seq_file *m)
2765 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2766 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2767 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2768 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2770 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2771 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2773 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2774 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2775 seq_printf(m, "# is not a '0' or '1')\n");
2778 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2780 if (iter->tr->allocated_snapshot)
2781 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2783 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2785 seq_printf(m, "# Snapshot commands:\n");
2786 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2787 show_snapshot_main_help(m);
2789 show_snapshot_percpu_help(m);
2792 /* Should never be called */
2793 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2796 static int s_show(struct seq_file *m, void *v)
2798 struct trace_iterator *iter = v;
2801 if (iter->ent == NULL) {
2803 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2805 test_ftrace_alive(m);
2807 if (iter->snapshot && trace_empty(iter))
2808 print_snapshot_help(m, iter);
2809 else if (iter->trace && iter->trace->print_header)
2810 iter->trace->print_header(m);
2812 trace_default_header(m);
2814 } else if (iter->leftover) {
2816 * If we filled the seq_file buffer earlier, we
2817 * want to just show it now.
2819 ret = trace_print_seq(m, &iter->seq);
2821 /* ret should this time be zero, but you never know */
2822 iter->leftover = ret;
2825 print_trace_line(iter);
2826 ret = trace_print_seq(m, &iter->seq);
2828 * If we overflow the seq_file buffer, then it will
2829 * ask us for this data again at start up.
2831 * ret is 0 if seq_file write succeeded.
2834 iter->leftover = ret;
2841 * Should be used after trace_array_get(), trace_types_lock
2842 * ensures that i_cdev was already initialized.
2844 static inline int tracing_get_cpu(struct inode *inode)
2846 if (inode->i_cdev) /* See trace_create_cpu_file() */
2847 return (long)inode->i_cdev - 1;
2848 return RING_BUFFER_ALL_CPUS;
2851 static const struct seq_operations tracer_seq_ops = {
2858 static struct trace_iterator *
2859 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2861 struct trace_array *tr = inode->i_private;
2862 struct trace_iterator *iter;
2865 if (tracing_disabled)
2866 return ERR_PTR(-ENODEV);
2868 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2870 return ERR_PTR(-ENOMEM);
2872 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2874 if (!iter->buffer_iter)
2878 * We make a copy of the current tracer to avoid concurrent
2879 * changes on it while we are reading.
2881 mutex_lock(&trace_types_lock);
2882 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2886 *iter->trace = *tr->current_trace;
2888 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2893 #ifdef CONFIG_TRACER_MAX_TRACE
2894 /* Currently only the top directory has a snapshot */
2895 if (tr->current_trace->print_max || snapshot)
2896 iter->trace_buffer = &tr->max_buffer;
2899 iter->trace_buffer = &tr->trace_buffer;
2900 iter->snapshot = snapshot;
2902 iter->cpu_file = tracing_get_cpu(inode);
2903 mutex_init(&iter->mutex);
2905 /* Notify the tracer early; before we stop tracing. */
2906 if (iter->trace && iter->trace->open)
2907 iter->trace->open(iter);
2909 /* Annotate start of buffers if we had overruns */
2910 if (ring_buffer_overruns(iter->trace_buffer->buffer))
2911 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2913 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2914 if (trace_clocks[tr->clock_id].in_ns)
2915 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2917 /* stop the trace while dumping if we are not opening "snapshot" */
2918 if (!iter->snapshot)
2919 tracing_stop_tr(tr);
2921 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
2922 for_each_tracing_cpu(cpu) {
2923 iter->buffer_iter[cpu] =
2924 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2926 ring_buffer_read_prepare_sync();
2927 for_each_tracing_cpu(cpu) {
2928 ring_buffer_read_start(iter->buffer_iter[cpu]);
2929 tracing_iter_reset(iter, cpu);
2932 cpu = iter->cpu_file;
2933 iter->buffer_iter[cpu] =
2934 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2935 ring_buffer_read_prepare_sync();
2936 ring_buffer_read_start(iter->buffer_iter[cpu]);
2937 tracing_iter_reset(iter, cpu);
2940 mutex_unlock(&trace_types_lock);
2945 mutex_unlock(&trace_types_lock);
2947 kfree(iter->buffer_iter);
2949 seq_release_private(inode, file);
2950 return ERR_PTR(-ENOMEM);
2953 int tracing_open_generic(struct inode *inode, struct file *filp)
2955 if (tracing_disabled)
2958 filp->private_data = inode->i_private;
2963 * Open and update trace_array ref count.
2964 * Must have the current trace_array passed to it.
2966 int tracing_open_generic_tr(struct inode *inode, struct file *filp)
2968 struct trace_array *tr = inode->i_private;
2970 if (tracing_disabled)
2973 if (trace_array_get(tr) < 0)
2976 filp->private_data = inode->i_private;
2981 static int tracing_release(struct inode *inode, struct file *file)
2983 struct trace_array *tr = inode->i_private;
2984 struct seq_file *m = file->private_data;
2985 struct trace_iterator *iter;
2988 if (!(file->f_mode & FMODE_READ)) {
2989 trace_array_put(tr);
2993 /* Writes do not use seq_file */
2995 mutex_lock(&trace_types_lock);
2997 for_each_tracing_cpu(cpu) {
2998 if (iter->buffer_iter[cpu])
2999 ring_buffer_read_finish(iter->buffer_iter[cpu]);
3002 if (iter->trace && iter->trace->close)
3003 iter->trace->close(iter);
3005 if (!iter->snapshot)
3006 /* reenable tracing if it was previously enabled */
3007 tracing_start_tr(tr);
3009 __trace_array_put(tr);
3011 mutex_unlock(&trace_types_lock);
3013 mutex_destroy(&iter->mutex);
3014 free_cpumask_var(iter->started);
3016 kfree(iter->buffer_iter);
3017 seq_release_private(inode, file);
3022 static int tracing_release_generic_tr(struct inode *inode, struct file *file)
3024 struct trace_array *tr = inode->i_private;
3026 trace_array_put(tr);
3030 static int tracing_single_release_tr(struct inode *inode, struct file *file)
3032 struct trace_array *tr = inode->i_private;
3034 trace_array_put(tr);
3036 return single_release(inode, file);
3039 static int tracing_open(struct inode *inode, struct file *file)
3041 struct trace_array *tr = inode->i_private;
3042 struct trace_iterator *iter;
3045 if (trace_array_get(tr) < 0)
3048 /* If this file was open for write, then erase contents */
3049 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
3050 int cpu = tracing_get_cpu(inode);
3052 if (cpu == RING_BUFFER_ALL_CPUS)
3053 tracing_reset_online_cpus(&tr->trace_buffer);
3055 tracing_reset(&tr->trace_buffer, cpu);
3058 if (file->f_mode & FMODE_READ) {
3059 iter = __tracing_open(inode, file, false);
3061 ret = PTR_ERR(iter);
3062 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
3063 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3067 trace_array_put(tr);
3073 t_next(struct seq_file *m, void *v, loff_t *pos)
3075 struct tracer *t = v;
3085 static void *t_start(struct seq_file *m, loff_t *pos)
3090 mutex_lock(&trace_types_lock);
3091 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
3097 static void t_stop(struct seq_file *m, void *p)
3099 mutex_unlock(&trace_types_lock);
3102 static int t_show(struct seq_file *m, void *v)
3104 struct tracer *t = v;
3109 seq_printf(m, "%s", t->name);
3118 static const struct seq_operations show_traces_seq_ops = {
3125 static int show_traces_open(struct inode *inode, struct file *file)
3127 if (tracing_disabled)
3130 return seq_open(file, &show_traces_seq_ops);
3134 tracing_write_stub(struct file *filp, const char __user *ubuf,
3135 size_t count, loff_t *ppos)
3140 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
3142 if (file->f_mode & FMODE_READ)
3143 return seq_lseek(file, offset, origin);
3148 static const struct file_operations tracing_fops = {
3149 .open = tracing_open,
3151 .write = tracing_write_stub,
3152 .llseek = tracing_seek,
3153 .release = tracing_release,
3156 static const struct file_operations show_traces_fops = {
3157 .open = show_traces_open,
3159 .release = seq_release,
3160 .llseek = seq_lseek,
3164 * Only trace on a CPU if the bitmask is set:
3166 static cpumask_var_t tracing_cpumask;
3169 * The tracer itself will not take this lock, but still we want
3170 * to provide a consistent cpumask to user-space:
3172 static DEFINE_MUTEX(tracing_cpumask_update_lock);
3175 * Temporary storage for the character representation of the
3176 * CPU bitmask (and one more byte for the newline):
3178 static char mask_str[NR_CPUS + 1];
3181 tracing_cpumask_read(struct file *filp, char __user *ubuf,
3182 size_t count, loff_t *ppos)
3186 mutex_lock(&tracing_cpumask_update_lock);
3188 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
3189 if (count - len < 2) {
3193 len += sprintf(mask_str + len, "\n");
3194 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3197 mutex_unlock(&tracing_cpumask_update_lock);
3203 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3204 size_t count, loff_t *ppos)
3206 struct trace_array *tr = filp->private_data;
3207 cpumask_var_t tracing_cpumask_new;
3210 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3213 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
3217 mutex_lock(&tracing_cpumask_update_lock);
3219 local_irq_disable();
3220 arch_spin_lock(&ftrace_max_lock);
3221 for_each_tracing_cpu(cpu) {
3223 * Increase/decrease the disabled counter if we are
3224 * about to flip a bit in the cpumask:
3226 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
3227 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3228 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3229 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
3231 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
3232 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3233 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3234 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
3237 arch_spin_unlock(&ftrace_max_lock);
3240 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
3242 mutex_unlock(&tracing_cpumask_update_lock);
3243 free_cpumask_var(tracing_cpumask_new);
3248 free_cpumask_var(tracing_cpumask_new);
3253 static const struct file_operations tracing_cpumask_fops = {
3254 .open = tracing_open_generic,
3255 .read = tracing_cpumask_read,
3256 .write = tracing_cpumask_write,
3257 .llseek = generic_file_llseek,
3260 static int tracing_trace_options_show(struct seq_file *m, void *v)
3262 struct tracer_opt *trace_opts;
3263 struct trace_array *tr = m->private;
3267 mutex_lock(&trace_types_lock);
3268 tracer_flags = tr->current_trace->flags->val;
3269 trace_opts = tr->current_trace->flags->opts;
3271 for (i = 0; trace_options[i]; i++) {
3272 if (trace_flags & (1 << i))
3273 seq_printf(m, "%s\n", trace_options[i]);
3275 seq_printf(m, "no%s\n", trace_options[i]);
3278 for (i = 0; trace_opts[i].name; i++) {
3279 if (tracer_flags & trace_opts[i].bit)
3280 seq_printf(m, "%s\n", trace_opts[i].name);
3282 seq_printf(m, "no%s\n", trace_opts[i].name);
3284 mutex_unlock(&trace_types_lock);
3289 static int __set_tracer_option(struct tracer *trace,
3290 struct tracer_flags *tracer_flags,
3291 struct tracer_opt *opts, int neg)
3295 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
3300 tracer_flags->val &= ~opts->bit;
3302 tracer_flags->val |= opts->bit;
3306 /* Try to assign a tracer specific option */
3307 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3309 struct tracer_flags *tracer_flags = trace->flags;
3310 struct tracer_opt *opts = NULL;
3313 for (i = 0; tracer_flags->opts[i].name; i++) {
3314 opts = &tracer_flags->opts[i];
3316 if (strcmp(cmp, opts->name) == 0)
3317 return __set_tracer_option(trace, trace->flags,
3324 /* Some tracers require overwrite to stay enabled */
3325 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3327 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3333 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
3335 /* do nothing if flag is already set */
3336 if (!!(trace_flags & mask) == !!enabled)
3339 /* Give the tracer a chance to approve the change */
3340 if (tr->current_trace->flag_changed)
3341 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
3345 trace_flags |= mask;
3347 trace_flags &= ~mask;
3349 if (mask == TRACE_ITER_RECORD_CMD)
3350 trace_event_enable_cmd_record(enabled);
3352 if (mask == TRACE_ITER_OVERWRITE) {
3353 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
3354 #ifdef CONFIG_TRACER_MAX_TRACE
3355 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
3359 if (mask == TRACE_ITER_PRINTK)
3360 trace_printk_start_stop_comm(enabled);
3365 static int trace_set_options(struct trace_array *tr, char *option)
3372 cmp = strstrip(option);
3374 if (strncmp(cmp, "no", 2) == 0) {
3379 mutex_lock(&trace_types_lock);
3381 for (i = 0; trace_options[i]; i++) {
3382 if (strcmp(cmp, trace_options[i]) == 0) {
3383 ret = set_tracer_flag(tr, 1 << i, !neg);
3388 /* If no option could be set, test the specific tracer options */
3389 if (!trace_options[i])
3390 ret = set_tracer_option(tr->current_trace, cmp, neg);
3392 mutex_unlock(&trace_types_lock);
3398 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3399 size_t cnt, loff_t *ppos)
3401 struct seq_file *m = filp->private_data;
3402 struct trace_array *tr = m->private;
3406 if (cnt >= sizeof(buf))
3409 if (copy_from_user(&buf, ubuf, cnt))
3414 ret = trace_set_options(tr, buf);
3423 static int tracing_trace_options_open(struct inode *inode, struct file *file)
3425 struct trace_array *tr = inode->i_private;
3428 if (tracing_disabled)
3431 if (trace_array_get(tr) < 0)
3434 ret = single_open(file, tracing_trace_options_show, inode->i_private);
3436 trace_array_put(tr);
3441 static const struct file_operations tracing_iter_fops = {
3442 .open = tracing_trace_options_open,
3444 .llseek = seq_lseek,
3445 .release = tracing_single_release_tr,
3446 .write = tracing_trace_options_write,
3449 static const char readme_msg[] =
3450 "tracing mini-HOWTO:\n\n"
3451 "# echo 0 > tracing_on : quick way to disable tracing\n"
3452 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3453 " Important files:\n"
3454 " trace\t\t\t- The static contents of the buffer\n"
3455 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3456 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3457 " current_tracer\t- function and latency tracers\n"
3458 " available_tracers\t- list of configured tracers for current_tracer\n"
3459 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3460 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3461 " trace_clock\t\t-change the clock used to order events\n"
3462 " local: Per cpu clock but may not be synced across CPUs\n"
3463 " global: Synced across CPUs but slows tracing down.\n"
3464 " counter: Not a clock, but just an increment\n"
3465 " uptime: Jiffy counter from time of boot\n"
3466 " perf: Same clock that perf events use\n"
3467 #ifdef CONFIG_X86_64
3468 " x86-tsc: TSC cycle counter\n"
3470 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3471 " tracing_cpumask\t- Limit which CPUs to trace\n"
3472 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3473 "\t\t\t Remove sub-buffer with rmdir\n"
3474 " trace_options\t\t- Set format or modify how tracing happens\n"
3475 "\t\t\t Disable an option by adding a suffix 'no' to the option name\n"
3476 #ifdef CONFIG_DYNAMIC_FTRACE
3477 "\n available_filter_functions - list of functions that can be filtered on\n"
3478 " set_ftrace_filter\t- echo function name in here to only trace these functions\n"
3479 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3480 " modules: Can select a group via module\n"
3481 " Format: :mod:<module-name>\n"
3482 " example: echo :mod:ext3 > set_ftrace_filter\n"
3483 " triggers: a command to perform when function is hit\n"
3484 " Format: <function>:<trigger>[:count]\n"
3485 " trigger: traceon, traceoff\n"
3486 " enable_event:<system>:<event>\n"
3487 " disable_event:<system>:<event>\n"
3488 #ifdef CONFIG_STACKTRACE
3491 #ifdef CONFIG_TRACER_SNAPSHOT
3494 " example: echo do_fault:traceoff > set_ftrace_filter\n"
3495 " echo do_trap:traceoff:3 > set_ftrace_filter\n"
3496 " The first one will disable tracing every time do_fault is hit\n"
3497 " The second will disable tracing at most 3 times when do_trap is hit\n"
3498 " The first time do trap is hit and it disables tracing, the counter\n"
3499 " will decrement to 2. If tracing is already disabled, the counter\n"
3500 " will not decrement. It only decrements when the trigger did work\n"
3501 " To remove trigger without count:\n"
3502 " echo '!<function>:<trigger> > set_ftrace_filter\n"
3503 " To remove trigger with a count:\n"
3504 " echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
3505 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
3506 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3507 " modules: Can select a group via module command :mod:\n"
3508 " Does not accept triggers\n"
3509 #endif /* CONFIG_DYNAMIC_FTRACE */
3510 #ifdef CONFIG_FUNCTION_TRACER
3511 " set_ftrace_pid\t- Write pid(s) to only function trace those pids (function)\n"
3513 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3514 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3515 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3517 #ifdef CONFIG_TRACER_SNAPSHOT
3518 "\n snapshot\t\t- Like 'trace' but shows the content of the static snapshot buffer\n"
3519 "\t\t\t Read the contents for more information\n"
3521 #ifdef CONFIG_STACKTRACE
3522 " stack_trace\t\t- Shows the max stack trace when active\n"
3523 " stack_max_size\t- Shows current max stack size that was traced\n"
3524 "\t\t\t Write into this file to reset the max size (trigger a new trace)\n"
3525 #ifdef CONFIG_DYNAMIC_FTRACE
3526 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace traces\n"
3528 #endif /* CONFIG_STACKTRACE */
3532 tracing_readme_read(struct file *filp, char __user *ubuf,
3533 size_t cnt, loff_t *ppos)
3535 return simple_read_from_buffer(ubuf, cnt, ppos,
3536 readme_msg, strlen(readme_msg));
3539 static const struct file_operations tracing_readme_fops = {
3540 .open = tracing_open_generic,
3541 .read = tracing_readme_read,
3542 .llseek = generic_file_llseek,
3546 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3547 size_t cnt, loff_t *ppos)
3556 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3560 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3568 for (i = 0; i < SAVED_CMDLINES; i++) {
3571 pid = map_cmdline_to_pid[i];
3572 if (pid == -1 || pid == NO_CMDLINE_MAP)
3575 trace_find_cmdline(pid, buf_comm);
3576 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3581 len = simple_read_from_buffer(ubuf, cnt, ppos,
3590 static const struct file_operations tracing_saved_cmdlines_fops = {
3591 .open = tracing_open_generic,
3592 .read = tracing_saved_cmdlines_read,
3593 .llseek = generic_file_llseek,
3597 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3598 size_t cnt, loff_t *ppos)
3600 struct trace_array *tr = filp->private_data;
3601 char buf[MAX_TRACER_SIZE+2];
3604 mutex_lock(&trace_types_lock);
3605 r = sprintf(buf, "%s\n", tr->current_trace->name);
3606 mutex_unlock(&trace_types_lock);
3608 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3611 int tracer_init(struct tracer *t, struct trace_array *tr)
3613 tracing_reset_online_cpus(&tr->trace_buffer);
3617 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
3621 for_each_tracing_cpu(cpu)
3622 per_cpu_ptr(buf->data, cpu)->entries = val;
3625 #ifdef CONFIG_TRACER_MAX_TRACE
3626 /* resize @tr's buffer to the size of @size_tr's entries */
3627 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3628 struct trace_buffer *size_buf, int cpu_id)
3632 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3633 for_each_tracing_cpu(cpu) {
3634 ret = ring_buffer_resize(trace_buf->buffer,
3635 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
3638 per_cpu_ptr(trace_buf->data, cpu)->entries =
3639 per_cpu_ptr(size_buf->data, cpu)->entries;
3642 ret = ring_buffer_resize(trace_buf->buffer,
3643 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
3645 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3646 per_cpu_ptr(size_buf->data, cpu_id)->entries;
3651 #endif /* CONFIG_TRACER_MAX_TRACE */
3653 static int __tracing_resize_ring_buffer(struct trace_array *tr,
3654 unsigned long size, int cpu)
3659 * If kernel or user changes the size of the ring buffer
3660 * we use the size that was given, and we can forget about
3661 * expanding it later.
3663 ring_buffer_expanded = true;
3665 /* May be called before buffers are initialized */
3666 if (!tr->trace_buffer.buffer)
3669 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
3673 #ifdef CONFIG_TRACER_MAX_TRACE
3674 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3675 !tr->current_trace->use_max_tr)
3678 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
3680 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3681 &tr->trace_buffer, cpu);
3684 * AARGH! We are left with different
3685 * size max buffer!!!!
3686 * The max buffer is our "snapshot" buffer.
3687 * When a tracer needs a snapshot (one of the
3688 * latency tracers), it swaps the max buffer
3689 * with the saved snap shot. We succeeded to
3690 * update the size of the main buffer, but failed to
3691 * update the size of the max buffer. But when we tried
3692 * to reset the main buffer to the original size, we
3693 * failed there too. This is very unlikely to
3694 * happen, but if it does, warn and kill all
3698 tracing_disabled = 1;
3703 if (cpu == RING_BUFFER_ALL_CPUS)
3704 set_buffer_entries(&tr->max_buffer, size);
3706 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
3709 #endif /* CONFIG_TRACER_MAX_TRACE */
3711 if (cpu == RING_BUFFER_ALL_CPUS)
3712 set_buffer_entries(&tr->trace_buffer, size);
3714 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
3719 static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3720 unsigned long size, int cpu_id)
3724 mutex_lock(&trace_types_lock);
3726 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3727 /* make sure, this cpu is enabled in the mask */
3728 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3734 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
3739 mutex_unlock(&trace_types_lock);
3746 * tracing_update_buffers - used by tracing facility to expand ring buffers
3748 * To save on memory when the tracing is never used on a system with it
3749 * configured in. The ring buffers are set to a minimum size. But once
3750 * a user starts to use the tracing facility, then they need to grow
3751 * to their default size.
3753 * This function is to be called when a tracer is about to be used.
3755 int tracing_update_buffers(void)
3759 mutex_lock(&trace_types_lock);
3760 if (!ring_buffer_expanded)
3761 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
3762 RING_BUFFER_ALL_CPUS);
3763 mutex_unlock(&trace_types_lock);
3768 struct trace_option_dentry;
3770 static struct trace_option_dentry *
3771 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
3774 destroy_trace_option_files(struct trace_option_dentry *topts);
3776 static int tracing_set_tracer(const char *buf)
3778 static struct trace_option_dentry *topts;
3779 struct trace_array *tr = &global_trace;
3781 #ifdef CONFIG_TRACER_MAX_TRACE
3786 mutex_lock(&trace_types_lock);
3788 if (!ring_buffer_expanded) {
3789 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
3790 RING_BUFFER_ALL_CPUS);
3796 for (t = trace_types; t; t = t->next) {
3797 if (strcmp(t->name, buf) == 0)
3804 if (t == tr->current_trace)
3807 trace_branch_disable();
3809 tr->current_trace->enabled = false;
3811 if (tr->current_trace->reset)
3812 tr->current_trace->reset(tr);
3814 /* Current trace needs to be nop_trace before synchronize_sched */
3815 tr->current_trace = &nop_trace;
3817 #ifdef CONFIG_TRACER_MAX_TRACE
3818 had_max_tr = tr->allocated_snapshot;
3820 if (had_max_tr && !t->use_max_tr) {
3822 * We need to make sure that the update_max_tr sees that
3823 * current_trace changed to nop_trace to keep it from
3824 * swapping the buffers after we resize it.
3825 * The update_max_tr is called from interrupts disabled
3826 * so a synchronized_sched() is sufficient.
3828 synchronize_sched();
3832 destroy_trace_option_files(topts);
3834 topts = create_trace_option_files(tr, t);
3836 #ifdef CONFIG_TRACER_MAX_TRACE
3837 if (t->use_max_tr && !had_max_tr) {
3838 ret = alloc_snapshot(tr);
3845 ret = tracer_init(t, tr);
3850 tr->current_trace = t;
3851 tr->current_trace->enabled = true;
3852 trace_branch_enable(tr);
3854 mutex_unlock(&trace_types_lock);
3860 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3861 size_t cnt, loff_t *ppos)
3863 char buf[MAX_TRACER_SIZE+1];
3870 if (cnt > MAX_TRACER_SIZE)
3871 cnt = MAX_TRACER_SIZE;
3873 if (copy_from_user(&buf, ubuf, cnt))
3878 /* strip ending whitespace. */
3879 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3882 err = tracing_set_tracer(buf);
3892 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3893 size_t cnt, loff_t *ppos)
3895 unsigned long *ptr = filp->private_data;
3899 r = snprintf(buf, sizeof(buf), "%ld\n",
3900 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3901 if (r > sizeof(buf))
3903 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3907 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3908 size_t cnt, loff_t *ppos)
3910 unsigned long *ptr = filp->private_data;
3914 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3923 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3925 struct trace_array *tr = inode->i_private;
3926 struct trace_iterator *iter;
3929 if (tracing_disabled)
3932 if (trace_array_get(tr) < 0)
3935 mutex_lock(&trace_types_lock);
3937 /* create a buffer to store the information to pass to userspace */
3938 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3941 __trace_array_put(tr);
3946 * We make a copy of the current tracer to avoid concurrent
3947 * changes on it while we are reading.
3949 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3954 *iter->trace = *tr->current_trace;
3956 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3961 /* trace pipe does not show start of buffer */
3962 cpumask_setall(iter->started);
3964 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3965 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3967 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3968 if (trace_clocks[tr->clock_id].in_ns)
3969 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3972 iter->trace_buffer = &tr->trace_buffer;
3973 iter->cpu_file = tracing_get_cpu(inode);
3974 mutex_init(&iter->mutex);
3975 filp->private_data = iter;
3977 if (iter->trace->pipe_open)
3978 iter->trace->pipe_open(iter);
3980 nonseekable_open(inode, filp);
3982 mutex_unlock(&trace_types_lock);
3988 __trace_array_put(tr);
3989 mutex_unlock(&trace_types_lock);
3993 static int tracing_release_pipe(struct inode *inode, struct file *file)
3995 struct trace_iterator *iter = file->private_data;
3996 struct trace_array *tr = inode->i_private;
3998 mutex_lock(&trace_types_lock);
4000 if (iter->trace->pipe_close)
4001 iter->trace->pipe_close(iter);
4003 mutex_unlock(&trace_types_lock);
4005 free_cpumask_var(iter->started);
4006 mutex_destroy(&iter->mutex);
4010 trace_array_put(tr);
4016 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
4018 /* Iterators are static, they should be filled or empty */
4019 if (trace_buffer_iter(iter, iter->cpu_file))
4020 return POLLIN | POLLRDNORM;
4022 if (trace_flags & TRACE_ITER_BLOCK)
4024 * Always select as readable when in blocking mode
4026 return POLLIN | POLLRDNORM;
4028 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
4033 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
4035 struct trace_iterator *iter = filp->private_data;
4037 return trace_poll(iter, filp, poll_table);
4041 * This is a make-shift waitqueue.
4042 * A tracer might use this callback on some rare cases:
4044 * 1) the current tracer might hold the runqueue lock when it wakes up
4045 * a reader, hence a deadlock (sched, function, and function graph tracers)
4046 * 2) the function tracers, trace all functions, we don't want
4047 * the overhead of calling wake_up and friends
4048 * (and tracing them too)
4050 * Anyway, this is really very primitive wakeup.
4052 void poll_wait_pipe(struct trace_iterator *iter)
4054 set_current_state(TASK_INTERRUPTIBLE);
4055 /* sleep for 100 msecs, and try again. */
4056 schedule_timeout(HZ / 10);
4059 /* Must be called with trace_types_lock mutex held. */
4060 static int tracing_wait_pipe(struct file *filp)
4062 struct trace_iterator *iter = filp->private_data;
4064 while (trace_empty(iter)) {
4066 if ((filp->f_flags & O_NONBLOCK)) {
4070 mutex_unlock(&iter->mutex);
4072 iter->trace->wait_pipe(iter);
4074 mutex_lock(&iter->mutex);
4076 if (signal_pending(current))
4080 * We block until we read something and tracing is disabled.
4081 * We still block if tracing is disabled, but we have never
4082 * read anything. This allows a user to cat this file, and
4083 * then enable tracing. But after we have read something,
4084 * we give an EOF when tracing is again disabled.
4086 * iter->pos will be 0 if we haven't read anything.
4088 if (!tracing_is_on() && iter->pos)
4099 tracing_read_pipe(struct file *filp, char __user *ubuf,
4100 size_t cnt, loff_t *ppos)
4102 struct trace_iterator *iter = filp->private_data;
4103 struct trace_array *tr = iter->tr;
4106 /* return any leftover data */
4107 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4111 trace_seq_init(&iter->seq);
4113 /* copy the tracer to avoid using a global lock all around */
4114 mutex_lock(&trace_types_lock);
4115 if (unlikely(iter->trace->name != tr->current_trace->name))
4116 *iter->trace = *tr->current_trace;
4117 mutex_unlock(&trace_types_lock);
4120 * Avoid more than one consumer on a single file descriptor
4121 * This is just a matter of traces coherency, the ring buffer itself
4124 mutex_lock(&iter->mutex);
4125 if (iter->trace->read) {
4126 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
4132 sret = tracing_wait_pipe(filp);
4136 /* stop when tracing is finished */
4137 if (trace_empty(iter)) {
4142 if (cnt >= PAGE_SIZE)
4143 cnt = PAGE_SIZE - 1;
4145 /* reset all but tr, trace, and overruns */
4146 memset(&iter->seq, 0,
4147 sizeof(struct trace_iterator) -
4148 offsetof(struct trace_iterator, seq));
4149 cpumask_clear(iter->started);
4152 trace_event_read_lock();
4153 trace_access_lock(iter->cpu_file);
4154 while (trace_find_next_entry_inc(iter) != NULL) {
4155 enum print_line_t ret;
4156 int len = iter->seq.len;
4158 ret = print_trace_line(iter);
4159 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4160 /* don't print partial lines */
4161 iter->seq.len = len;
4164 if (ret != TRACE_TYPE_NO_CONSUME)
4165 trace_consume(iter);
4167 if (iter->seq.len >= cnt)
4171 * Setting the full flag means we reached the trace_seq buffer
4172 * size and we should leave by partial output condition above.
4173 * One of the trace_seq_* functions is not used properly.
4175 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
4178 trace_access_unlock(iter->cpu_file);
4179 trace_event_read_unlock();
4181 /* Now copy what we have to the user */
4182 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4183 if (iter->seq.readpos >= iter->seq.len)
4184 trace_seq_init(&iter->seq);
4187 * If there was nothing to send to user, in spite of consuming trace
4188 * entries, go back to wait for more entries.
4194 mutex_unlock(&iter->mutex);
4199 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
4200 struct pipe_buffer *buf)
4202 __free_page(buf->page);
4205 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
4208 __free_page(spd->pages[idx]);
4211 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
4213 .map = generic_pipe_buf_map,
4214 .unmap = generic_pipe_buf_unmap,
4215 .confirm = generic_pipe_buf_confirm,
4216 .release = tracing_pipe_buf_release,
4217 .steal = generic_pipe_buf_steal,
4218 .get = generic_pipe_buf_get,
4222 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
4227 /* Seq buffer is page-sized, exactly what we need. */
4229 count = iter->seq.len;
4230 ret = print_trace_line(iter);
4231 count = iter->seq.len - count;
4234 iter->seq.len -= count;
4237 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4238 iter->seq.len -= count;
4242 if (ret != TRACE_TYPE_NO_CONSUME)
4243 trace_consume(iter);
4245 if (!trace_find_next_entry_inc(iter)) {
4255 static ssize_t tracing_splice_read_pipe(struct file *filp,
4257 struct pipe_inode_info *pipe,
4261 struct page *pages_def[PIPE_DEF_BUFFERS];
4262 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4263 struct trace_iterator *iter = filp->private_data;
4264 struct splice_pipe_desc spd = {
4266 .partial = partial_def,
4267 .nr_pages = 0, /* This gets updated below. */
4268 .nr_pages_max = PIPE_DEF_BUFFERS,
4270 .ops = &tracing_pipe_buf_ops,
4271 .spd_release = tracing_spd_release_pipe,
4273 struct trace_array *tr = iter->tr;
4278 if (splice_grow_spd(pipe, &spd))
4281 /* copy the tracer to avoid using a global lock all around */
4282 mutex_lock(&trace_types_lock);
4283 if (unlikely(iter->trace->name != tr->current_trace->name))
4284 *iter->trace = *tr->current_trace;
4285 mutex_unlock(&trace_types_lock);
4287 mutex_lock(&iter->mutex);
4289 if (iter->trace->splice_read) {
4290 ret = iter->trace->splice_read(iter, filp,
4291 ppos, pipe, len, flags);
4296 ret = tracing_wait_pipe(filp);
4300 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
4305 trace_event_read_lock();
4306 trace_access_lock(iter->cpu_file);
4308 /* Fill as many pages as possible. */
4309 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
4310 spd.pages[i] = alloc_page(GFP_KERNEL);
4314 rem = tracing_fill_pipe_page(rem, iter);
4316 /* Copy the data into the page, so we can start over. */
4317 ret = trace_seq_to_buffer(&iter->seq,
4318 page_address(spd.pages[i]),
4321 __free_page(spd.pages[i]);
4324 spd.partial[i].offset = 0;
4325 spd.partial[i].len = iter->seq.len;
4327 trace_seq_init(&iter->seq);
4330 trace_access_unlock(iter->cpu_file);
4331 trace_event_read_unlock();
4332 mutex_unlock(&iter->mutex);
4336 ret = splice_to_pipe(pipe, &spd);
4338 splice_shrink_spd(&spd);
4342 mutex_unlock(&iter->mutex);
4347 tracing_entries_read(struct file *filp, char __user *ubuf,
4348 size_t cnt, loff_t *ppos)
4350 struct inode *inode = file_inode(filp);
4351 struct trace_array *tr = inode->i_private;
4352 int cpu = tracing_get_cpu(inode);
4357 mutex_lock(&trace_types_lock);
4359 if (cpu == RING_BUFFER_ALL_CPUS) {
4360 int cpu, buf_size_same;
4365 /* check if all cpu sizes are same */
4366 for_each_tracing_cpu(cpu) {
4367 /* fill in the size from first enabled cpu */
4369 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4370 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
4376 if (buf_size_same) {
4377 if (!ring_buffer_expanded)
4378 r = sprintf(buf, "%lu (expanded: %lu)\n",
4380 trace_buf_size >> 10);
4382 r = sprintf(buf, "%lu\n", size >> 10);
4384 r = sprintf(buf, "X\n");
4386 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
4388 mutex_unlock(&trace_types_lock);
4390 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4395 tracing_entries_write(struct file *filp, const char __user *ubuf,
4396 size_t cnt, loff_t *ppos)
4398 struct inode *inode = file_inode(filp);
4399 struct trace_array *tr = inode->i_private;
4403 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4407 /* must have at least 1 entry */
4411 /* value is in KB */
4413 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
4423 tracing_total_entries_read(struct file *filp, char __user *ubuf,
4424 size_t cnt, loff_t *ppos)
4426 struct trace_array *tr = filp->private_data;
4429 unsigned long size = 0, expanded_size = 0;
4431 mutex_lock(&trace_types_lock);
4432 for_each_tracing_cpu(cpu) {
4433 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
4434 if (!ring_buffer_expanded)
4435 expanded_size += trace_buf_size >> 10;
4437 if (ring_buffer_expanded)
4438 r = sprintf(buf, "%lu\n", size);
4440 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4441 mutex_unlock(&trace_types_lock);
4443 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4447 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4448 size_t cnt, loff_t *ppos)
4451 * There is no need to read what the user has written, this function
4452 * is just to make sure that there is no error when "echo" is used
4461 tracing_free_buffer_release(struct inode *inode, struct file *filp)
4463 struct trace_array *tr = inode->i_private;
4465 /* disable tracing ? */
4466 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4467 tracer_tracing_off(tr);
4468 /* resize the ring buffer to 0 */
4469 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4471 trace_array_put(tr);
4477 tracing_mark_write(struct file *filp, const char __user *ubuf,
4478 size_t cnt, loff_t *fpos)
4480 unsigned long addr = (unsigned long)ubuf;
4481 struct trace_array *tr = filp->private_data;
4482 struct ring_buffer_event *event;
4483 struct ring_buffer *buffer;
4484 struct print_entry *entry;
4485 unsigned long irq_flags;
4486 struct page *pages[2];
4496 if (tracing_disabled)
4499 if (!(trace_flags & TRACE_ITER_MARKERS))
4502 if (cnt > TRACE_BUF_SIZE)
4503 cnt = TRACE_BUF_SIZE;
4506 * Userspace is injecting traces into the kernel trace buffer.
4507 * We want to be as non intrusive as possible.
4508 * To do so, we do not want to allocate any special buffers
4509 * or take any locks, but instead write the userspace data
4510 * straight into the ring buffer.
4512 * First we need to pin the userspace buffer into memory,
4513 * which, most likely it is, because it just referenced it.
4514 * But there's no guarantee that it is. By using get_user_pages_fast()
4515 * and kmap_atomic/kunmap_atomic() we can get access to the
4516 * pages directly. We then write the data directly into the
4519 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4521 /* check if we cross pages */
4522 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4525 offset = addr & (PAGE_SIZE - 1);
4528 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4529 if (ret < nr_pages) {
4531 put_page(pages[ret]);
4536 for (i = 0; i < nr_pages; i++)
4537 map_page[i] = kmap_atomic(pages[i]);
4539 local_save_flags(irq_flags);
4540 size = sizeof(*entry) + cnt + 2; /* possible \n added */
4541 buffer = tr->trace_buffer.buffer;
4542 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4543 irq_flags, preempt_count());
4545 /* Ring buffer disabled, return as if not open for write */
4550 entry = ring_buffer_event_data(event);
4551 entry->ip = _THIS_IP_;
4553 if (nr_pages == 2) {
4554 len = PAGE_SIZE - offset;
4555 memcpy(&entry->buf, map_page[0] + offset, len);
4556 memcpy(&entry->buf[len], map_page[1], cnt - len);
4558 memcpy(&entry->buf, map_page[0] + offset, cnt);
4560 if (entry->buf[cnt - 1] != '\n') {
4561 entry->buf[cnt] = '\n';
4562 entry->buf[cnt + 1] = '\0';
4564 entry->buf[cnt] = '\0';
4566 __buffer_unlock_commit(buffer, event);
4573 for (i = 0; i < nr_pages; i++){
4574 kunmap_atomic(map_page[i]);
4581 static int tracing_clock_show(struct seq_file *m, void *v)
4583 struct trace_array *tr = m->private;
4586 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4588 "%s%s%s%s", i ? " " : "",
4589 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4590 i == tr->clock_id ? "]" : "");
4596 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4597 size_t cnt, loff_t *fpos)
4599 struct seq_file *m = filp->private_data;
4600 struct trace_array *tr = m->private;
4602 const char *clockstr;
4605 if (cnt >= sizeof(buf))
4608 if (copy_from_user(&buf, ubuf, cnt))
4613 clockstr = strstrip(buf);
4615 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4616 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4619 if (i == ARRAY_SIZE(trace_clocks))
4622 mutex_lock(&trace_types_lock);
4626 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
4629 * New clock may not be consistent with the previous clock.
4630 * Reset the buffer so that it doesn't have incomparable timestamps.
4632 tracing_reset_online_cpus(&tr->trace_buffer);
4634 #ifdef CONFIG_TRACER_MAX_TRACE
4635 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4636 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4637 tracing_reset_online_cpus(&tr->max_buffer);
4640 mutex_unlock(&trace_types_lock);
4647 static int tracing_clock_open(struct inode *inode, struct file *file)
4649 struct trace_array *tr = inode->i_private;
4652 if (tracing_disabled)
4655 if (trace_array_get(tr))
4658 ret = single_open(file, tracing_clock_show, inode->i_private);
4660 trace_array_put(tr);
4665 struct ftrace_buffer_info {
4666 struct trace_iterator iter;
4671 #ifdef CONFIG_TRACER_SNAPSHOT
4672 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4674 struct trace_array *tr = inode->i_private;
4675 struct trace_iterator *iter;
4679 if (trace_array_get(tr) < 0)
4682 if (file->f_mode & FMODE_READ) {
4683 iter = __tracing_open(inode, file, true);
4685 ret = PTR_ERR(iter);
4687 /* Writes still need the seq_file to hold the private data */
4689 m = kzalloc(sizeof(*m), GFP_KERNEL);
4692 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4700 iter->trace_buffer = &tr->max_buffer;
4701 iter->cpu_file = tracing_get_cpu(inode);
4703 file->private_data = m;
4707 trace_array_put(tr);
4713 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4716 struct seq_file *m = filp->private_data;
4717 struct trace_iterator *iter = m->private;
4718 struct trace_array *tr = iter->tr;
4722 ret = tracing_update_buffers();
4726 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4730 mutex_lock(&trace_types_lock);
4732 if (tr->current_trace->use_max_tr) {
4739 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4743 if (tr->allocated_snapshot)
4747 /* Only allow per-cpu swap if the ring buffer supports it */
4748 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4749 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4754 if (!tr->allocated_snapshot) {
4755 ret = alloc_snapshot(tr);
4759 local_irq_disable();
4760 /* Now, we're going to swap */
4761 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4762 update_max_tr(tr, current, smp_processor_id());
4764 update_max_tr_single(tr, current, iter->cpu_file);
4768 if (tr->allocated_snapshot) {
4769 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4770 tracing_reset_online_cpus(&tr->max_buffer);
4772 tracing_reset(&tr->max_buffer, iter->cpu_file);
4782 mutex_unlock(&trace_types_lock);
4786 static int tracing_snapshot_release(struct inode *inode, struct file *file)
4788 struct seq_file *m = file->private_data;
4791 ret = tracing_release(inode, file);
4793 if (file->f_mode & FMODE_READ)
4796 /* If write only, the seq_file is just a stub */
4804 static int tracing_buffers_open(struct inode *inode, struct file *filp);
4805 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
4806 size_t count, loff_t *ppos);
4807 static int tracing_buffers_release(struct inode *inode, struct file *file);
4808 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4809 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
4811 static int snapshot_raw_open(struct inode *inode, struct file *filp)
4813 struct ftrace_buffer_info *info;
4816 ret = tracing_buffers_open(inode, filp);
4820 info = filp->private_data;
4822 if (info->iter.trace->use_max_tr) {
4823 tracing_buffers_release(inode, filp);
4827 info->iter.snapshot = true;
4828 info->iter.trace_buffer = &info->iter.tr->max_buffer;
4833 #endif /* CONFIG_TRACER_SNAPSHOT */
4836 static const struct file_operations tracing_max_lat_fops = {
4837 .open = tracing_open_generic,
4838 .read = tracing_max_lat_read,
4839 .write = tracing_max_lat_write,
4840 .llseek = generic_file_llseek,
4843 static const struct file_operations set_tracer_fops = {
4844 .open = tracing_open_generic,
4845 .read = tracing_set_trace_read,
4846 .write = tracing_set_trace_write,
4847 .llseek = generic_file_llseek,
4850 static const struct file_operations tracing_pipe_fops = {
4851 .open = tracing_open_pipe,
4852 .poll = tracing_poll_pipe,
4853 .read = tracing_read_pipe,
4854 .splice_read = tracing_splice_read_pipe,
4855 .release = tracing_release_pipe,
4856 .llseek = no_llseek,
4859 static const struct file_operations tracing_entries_fops = {
4860 .open = tracing_open_generic_tr,
4861 .read = tracing_entries_read,
4862 .write = tracing_entries_write,
4863 .llseek = generic_file_llseek,
4864 .release = tracing_release_generic_tr,
4867 static const struct file_operations tracing_total_entries_fops = {
4868 .open = tracing_open_generic_tr,
4869 .read = tracing_total_entries_read,
4870 .llseek = generic_file_llseek,
4871 .release = tracing_release_generic_tr,
4874 static const struct file_operations tracing_free_buffer_fops = {
4875 .open = tracing_open_generic_tr,
4876 .write = tracing_free_buffer_write,
4877 .release = tracing_free_buffer_release,
4880 static const struct file_operations tracing_mark_fops = {
4881 .open = tracing_open_generic_tr,
4882 .write = tracing_mark_write,
4883 .llseek = generic_file_llseek,
4884 .release = tracing_release_generic_tr,
4887 static const struct file_operations trace_clock_fops = {
4888 .open = tracing_clock_open,
4890 .llseek = seq_lseek,
4891 .release = tracing_single_release_tr,
4892 .write = tracing_clock_write,
4895 #ifdef CONFIG_TRACER_SNAPSHOT
4896 static const struct file_operations snapshot_fops = {
4897 .open = tracing_snapshot_open,
4899 .write = tracing_snapshot_write,
4900 .llseek = tracing_seek,
4901 .release = tracing_snapshot_release,
4904 static const struct file_operations snapshot_raw_fops = {
4905 .open = snapshot_raw_open,
4906 .read = tracing_buffers_read,
4907 .release = tracing_buffers_release,
4908 .splice_read = tracing_buffers_splice_read,
4909 .llseek = no_llseek,
4912 #endif /* CONFIG_TRACER_SNAPSHOT */
4914 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4916 struct trace_array *tr = inode->i_private;
4917 struct ftrace_buffer_info *info;
4920 if (tracing_disabled)
4923 if (trace_array_get(tr) < 0)
4926 info = kzalloc(sizeof(*info), GFP_KERNEL);
4928 trace_array_put(tr);
4932 mutex_lock(&trace_types_lock);
4935 info->iter.cpu_file = tracing_get_cpu(inode);
4936 info->iter.trace = tr->current_trace;
4937 info->iter.trace_buffer = &tr->trace_buffer;
4939 /* Force reading ring buffer for first read */
4940 info->read = (unsigned int)-1;
4942 filp->private_data = info;
4944 mutex_unlock(&trace_types_lock);
4946 ret = nonseekable_open(inode, filp);
4948 trace_array_put(tr);
4954 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
4956 struct ftrace_buffer_info *info = filp->private_data;
4957 struct trace_iterator *iter = &info->iter;
4959 return trace_poll(iter, filp, poll_table);
4963 tracing_buffers_read(struct file *filp, char __user *ubuf,
4964 size_t count, loff_t *ppos)
4966 struct ftrace_buffer_info *info = filp->private_data;
4967 struct trace_iterator *iter = &info->iter;
4974 mutex_lock(&trace_types_lock);
4976 #ifdef CONFIG_TRACER_MAX_TRACE
4977 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4984 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
4990 /* Do we have previous read data to read? */
4991 if (info->read < PAGE_SIZE)
4995 trace_access_lock(iter->cpu_file);
4996 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
5000 trace_access_unlock(iter->cpu_file);
5003 if (trace_empty(iter)) {
5004 if ((filp->f_flags & O_NONBLOCK)) {
5008 mutex_unlock(&trace_types_lock);
5009 iter->trace->wait_pipe(iter);
5010 mutex_lock(&trace_types_lock);
5011 if (signal_pending(current)) {
5023 size = PAGE_SIZE - info->read;
5027 ret = copy_to_user(ubuf, info->spare + info->read, size);
5038 mutex_unlock(&trace_types_lock);
5043 static int tracing_buffers_release(struct inode *inode, struct file *file)
5045 struct ftrace_buffer_info *info = file->private_data;
5046 struct trace_iterator *iter = &info->iter;
5048 mutex_lock(&trace_types_lock);
5050 __trace_array_put(iter->tr);
5053 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
5056 mutex_unlock(&trace_types_lock);
5062 struct ring_buffer *buffer;
5067 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
5068 struct pipe_buffer *buf)
5070 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5075 ring_buffer_free_read_page(ref->buffer, ref->page);
5080 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
5081 struct pipe_buffer *buf)
5083 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5088 /* Pipe buffer operations for a buffer. */
5089 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
5091 .map = generic_pipe_buf_map,
5092 .unmap = generic_pipe_buf_unmap,
5093 .confirm = generic_pipe_buf_confirm,
5094 .release = buffer_pipe_buf_release,
5095 .steal = generic_pipe_buf_steal,
5096 .get = buffer_pipe_buf_get,
5100 * Callback from splice_to_pipe(), if we need to release some pages
5101 * at the end of the spd in case we error'ed out in filling the pipe.
5103 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
5105 struct buffer_ref *ref =
5106 (struct buffer_ref *)spd->partial[i].private;
5111 ring_buffer_free_read_page(ref->buffer, ref->page);
5113 spd->partial[i].private = 0;
5117 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5118 struct pipe_inode_info *pipe, size_t len,
5121 struct ftrace_buffer_info *info = file->private_data;
5122 struct trace_iterator *iter = &info->iter;
5123 struct partial_page partial_def[PIPE_DEF_BUFFERS];
5124 struct page *pages_def[PIPE_DEF_BUFFERS];
5125 struct splice_pipe_desc spd = {
5127 .partial = partial_def,
5128 .nr_pages_max = PIPE_DEF_BUFFERS,
5130 .ops = &buffer_pipe_buf_ops,
5131 .spd_release = buffer_spd_release,
5133 struct buffer_ref *ref;
5134 int entries, size, i;
5137 mutex_lock(&trace_types_lock);
5139 #ifdef CONFIG_TRACER_MAX_TRACE
5140 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5146 if (splice_grow_spd(pipe, &spd)) {
5151 if (*ppos & (PAGE_SIZE - 1)) {
5156 if (len & (PAGE_SIZE - 1)) {
5157 if (len < PAGE_SIZE) {
5165 trace_access_lock(iter->cpu_file);
5166 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5168 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
5172 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
5177 ref->buffer = iter->trace_buffer->buffer;
5178 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
5184 r = ring_buffer_read_page(ref->buffer, &ref->page,
5185 len, iter->cpu_file, 1);
5187 ring_buffer_free_read_page(ref->buffer, ref->page);
5193 * zero out any left over data, this is going to
5196 size = ring_buffer_page_len(ref->page);
5197 if (size < PAGE_SIZE)
5198 memset(ref->page + size, 0, PAGE_SIZE - size);
5200 page = virt_to_page(ref->page);
5202 spd.pages[i] = page;
5203 spd.partial[i].len = PAGE_SIZE;
5204 spd.partial[i].offset = 0;
5205 spd.partial[i].private = (unsigned long)ref;
5209 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5212 trace_access_unlock(iter->cpu_file);
5215 /* did we read anything? */
5216 if (!spd.nr_pages) {
5217 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
5221 mutex_unlock(&trace_types_lock);
5222 iter->trace->wait_pipe(iter);
5223 mutex_lock(&trace_types_lock);
5224 if (signal_pending(current)) {
5231 ret = splice_to_pipe(pipe, &spd);
5232 splice_shrink_spd(&spd);
5234 mutex_unlock(&trace_types_lock);
5239 static const struct file_operations tracing_buffers_fops = {
5240 .open = tracing_buffers_open,
5241 .read = tracing_buffers_read,
5242 .poll = tracing_buffers_poll,
5243 .release = tracing_buffers_release,
5244 .splice_read = tracing_buffers_splice_read,
5245 .llseek = no_llseek,
5249 tracing_stats_read(struct file *filp, char __user *ubuf,
5250 size_t count, loff_t *ppos)
5252 struct inode *inode = file_inode(filp);
5253 struct trace_array *tr = inode->i_private;
5254 struct trace_buffer *trace_buf = &tr->trace_buffer;
5255 int cpu = tracing_get_cpu(inode);
5256 struct trace_seq *s;
5258 unsigned long long t;
5259 unsigned long usec_rem;
5261 s = kmalloc(sizeof(*s), GFP_KERNEL);
5267 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
5268 trace_seq_printf(s, "entries: %ld\n", cnt);
5270 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
5271 trace_seq_printf(s, "overrun: %ld\n", cnt);
5273 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
5274 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5276 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
5277 trace_seq_printf(s, "bytes: %ld\n", cnt);
5279 if (trace_clocks[tr->clock_id].in_ns) {
5280 /* local or global for trace_clock */
5281 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5282 usec_rem = do_div(t, USEC_PER_SEC);
5283 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5286 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
5287 usec_rem = do_div(t, USEC_PER_SEC);
5288 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5290 /* counter or tsc mode for trace_clock */
5291 trace_seq_printf(s, "oldest event ts: %llu\n",
5292 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5294 trace_seq_printf(s, "now ts: %llu\n",
5295 ring_buffer_time_stamp(trace_buf->buffer, cpu));
5298 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
5299 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5301 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
5302 trace_seq_printf(s, "read events: %ld\n", cnt);
5304 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
5311 static const struct file_operations tracing_stats_fops = {
5312 .open = tracing_open_generic_tr,
5313 .read = tracing_stats_read,
5314 .llseek = generic_file_llseek,
5315 .release = tracing_release_generic_tr,
5318 #ifdef CONFIG_DYNAMIC_FTRACE
5320 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
5326 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
5327 size_t cnt, loff_t *ppos)
5329 static char ftrace_dyn_info_buffer[1024];
5330 static DEFINE_MUTEX(dyn_info_mutex);
5331 unsigned long *p = filp->private_data;
5332 char *buf = ftrace_dyn_info_buffer;
5333 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
5336 mutex_lock(&dyn_info_mutex);
5337 r = sprintf(buf, "%ld ", *p);
5339 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
5342 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5344 mutex_unlock(&dyn_info_mutex);
5349 static const struct file_operations tracing_dyn_info_fops = {
5350 .open = tracing_open_generic,
5351 .read = tracing_read_dyn_info,
5352 .llseek = generic_file_llseek,
5354 #endif /* CONFIG_DYNAMIC_FTRACE */
5356 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5358 ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5364 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5366 unsigned long *count = (long *)data;
5378 ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5379 struct ftrace_probe_ops *ops, void *data)
5381 long count = (long)data;
5383 seq_printf(m, "%ps:", (void *)ip);
5385 seq_printf(m, "snapshot");
5388 seq_printf(m, ":unlimited\n");
5390 seq_printf(m, ":count=%ld\n", count);
5395 static struct ftrace_probe_ops snapshot_probe_ops = {
5396 .func = ftrace_snapshot,
5397 .print = ftrace_snapshot_print,
5400 static struct ftrace_probe_ops snapshot_count_probe_ops = {
5401 .func = ftrace_count_snapshot,
5402 .print = ftrace_snapshot_print,
5406 ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5407 char *glob, char *cmd, char *param, int enable)
5409 struct ftrace_probe_ops *ops;
5410 void *count = (void *)-1;
5414 /* hash funcs only work with set_ftrace_filter */
5418 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
5420 if (glob[0] == '!') {
5421 unregister_ftrace_function_probe_func(glob+1, ops);
5428 number = strsep(¶m, ":");
5430 if (!strlen(number))
5434 * We use the callback data field (which is a pointer)
5437 ret = kstrtoul(number, 0, (unsigned long *)&count);
5442 ret = register_ftrace_function_probe(glob, ops, count);
5445 alloc_snapshot(&global_trace);
5447 return ret < 0 ? ret : 0;
5450 static struct ftrace_func_command ftrace_snapshot_cmd = {
5452 .func = ftrace_trace_snapshot_callback,
5455 static int register_snapshot_cmd(void)
5457 return register_ftrace_command(&ftrace_snapshot_cmd);
5460 static inline int register_snapshot_cmd(void) { return 0; }
5461 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
5463 struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
5468 if (!debugfs_initialized())
5471 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5472 tr->dir = debugfs_create_dir("tracing", NULL);
5475 pr_warn_once("Could not create debugfs directory 'tracing'\n");
5480 struct dentry *tracing_init_dentry(void)
5482 return tracing_init_dentry_tr(&global_trace);
5485 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
5487 struct dentry *d_tracer;
5490 return tr->percpu_dir;
5492 d_tracer = tracing_init_dentry_tr(tr);
5496 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
5498 WARN_ONCE(!tr->percpu_dir,
5499 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
5501 return tr->percpu_dir;
5504 static struct dentry *
5505 trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
5506 void *data, long cpu, const struct file_operations *fops)
5508 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
5510 if (ret) /* See tracing_get_cpu() */
5511 ret->d_inode->i_cdev = (void *)(cpu + 1);
5516 tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
5518 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5519 struct dentry *d_cpu;
5520 char cpu_dir[30]; /* 30 characters should be more than enough */
5525 snprintf(cpu_dir, 30, "cpu%ld", cpu);
5526 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5528 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5532 /* per cpu trace_pipe */
5533 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
5534 tr, cpu, &tracing_pipe_fops);
5537 trace_create_cpu_file("trace", 0644, d_cpu,
5538 tr, cpu, &tracing_fops);
5540 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
5541 tr, cpu, &tracing_buffers_fops);
5543 trace_create_cpu_file("stats", 0444, d_cpu,
5544 tr, cpu, &tracing_stats_fops);
5546 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
5547 tr, cpu, &tracing_entries_fops);
5549 #ifdef CONFIG_TRACER_SNAPSHOT
5550 trace_create_cpu_file("snapshot", 0644, d_cpu,
5551 tr, cpu, &snapshot_fops);
5553 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
5554 tr, cpu, &snapshot_raw_fops);
5558 #ifdef CONFIG_FTRACE_SELFTEST
5559 /* Let selftest have access to static functions in this file */
5560 #include "trace_selftest.c"
5563 struct trace_option_dentry {
5564 struct tracer_opt *opt;
5565 struct tracer_flags *flags;
5566 struct trace_array *tr;
5567 struct dentry *entry;
5571 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5574 struct trace_option_dentry *topt = filp->private_data;
5577 if (topt->flags->val & topt->opt->bit)
5582 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5586 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5589 struct trace_option_dentry *topt = filp->private_data;
5593 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5597 if (val != 0 && val != 1)
5600 if (!!(topt->flags->val & topt->opt->bit) != val) {
5601 mutex_lock(&trace_types_lock);
5602 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
5604 mutex_unlock(&trace_types_lock);
5615 static const struct file_operations trace_options_fops = {
5616 .open = tracing_open_generic,
5617 .read = trace_options_read,
5618 .write = trace_options_write,
5619 .llseek = generic_file_llseek,
5623 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5626 long index = (long)filp->private_data;
5629 if (trace_flags & (1 << index))
5634 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5638 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5641 struct trace_array *tr = &global_trace;
5642 long index = (long)filp->private_data;
5646 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5650 if (val != 0 && val != 1)
5653 mutex_lock(&trace_types_lock);
5654 ret = set_tracer_flag(tr, 1 << index, val);
5655 mutex_unlock(&trace_types_lock);
5665 static const struct file_operations trace_options_core_fops = {
5666 .open = tracing_open_generic,
5667 .read = trace_options_core_read,
5668 .write = trace_options_core_write,
5669 .llseek = generic_file_llseek,
5672 struct dentry *trace_create_file(const char *name,
5674 struct dentry *parent,
5676 const struct file_operations *fops)
5680 ret = debugfs_create_file(name, mode, parent, data, fops);
5682 pr_warning("Could not create debugfs '%s' entry\n", name);
5688 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
5690 struct dentry *d_tracer;
5695 d_tracer = tracing_init_dentry_tr(tr);
5699 tr->options = debugfs_create_dir("options", d_tracer);
5701 pr_warning("Could not create debugfs directory 'options'\n");
5709 create_trace_option_file(struct trace_array *tr,
5710 struct trace_option_dentry *topt,
5711 struct tracer_flags *flags,
5712 struct tracer_opt *opt)
5714 struct dentry *t_options;
5716 t_options = trace_options_init_dentry(tr);
5720 topt->flags = flags;
5724 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
5725 &trace_options_fops);
5729 static struct trace_option_dentry *
5730 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
5732 struct trace_option_dentry *topts;
5733 struct tracer_flags *flags;
5734 struct tracer_opt *opts;
5740 flags = tracer->flags;
5742 if (!flags || !flags->opts)
5747 for (cnt = 0; opts[cnt].name; cnt++)
5750 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
5754 for (cnt = 0; opts[cnt].name; cnt++)
5755 create_trace_option_file(tr, &topts[cnt], flags,
5762 destroy_trace_option_files(struct trace_option_dentry *topts)
5769 for (cnt = 0; topts[cnt].opt; cnt++) {
5770 if (topts[cnt].entry)
5771 debugfs_remove(topts[cnt].entry);
5777 static struct dentry *
5778 create_trace_option_core_file(struct trace_array *tr,
5779 const char *option, long index)
5781 struct dentry *t_options;
5783 t_options = trace_options_init_dentry(tr);
5787 return trace_create_file(option, 0644, t_options, (void *)index,
5788 &trace_options_core_fops);
5791 static __init void create_trace_options_dir(struct trace_array *tr)
5793 struct dentry *t_options;
5796 t_options = trace_options_init_dentry(tr);
5800 for (i = 0; trace_options[i]; i++)
5801 create_trace_option_core_file(tr, trace_options[i], i);
5805 rb_simple_read(struct file *filp, char __user *ubuf,
5806 size_t cnt, loff_t *ppos)
5808 struct trace_array *tr = filp->private_data;
5812 r = tracer_tracing_is_on(tr);
5813 r = sprintf(buf, "%d\n", r);
5815 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5819 rb_simple_write(struct file *filp, const char __user *ubuf,
5820 size_t cnt, loff_t *ppos)
5822 struct trace_array *tr = filp->private_data;
5823 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5827 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5832 mutex_lock(&trace_types_lock);
5834 tracer_tracing_on(tr);
5835 if (tr->current_trace->start)
5836 tr->current_trace->start(tr);
5838 tracer_tracing_off(tr);
5839 if (tr->current_trace->stop)
5840 tr->current_trace->stop(tr);
5842 mutex_unlock(&trace_types_lock);
5850 static const struct file_operations rb_simple_fops = {
5851 .open = tracing_open_generic_tr,
5852 .read = rb_simple_read,
5853 .write = rb_simple_write,
5854 .release = tracing_release_generic_tr,
5855 .llseek = default_llseek,
5858 struct dentry *trace_instance_dir;
5861 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5863 static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
5867 for_each_tracing_cpu(cpu) {
5868 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
5869 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
5870 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
5875 allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
5877 enum ring_buffer_flags rb_flags;
5879 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5881 buf->buffer = ring_buffer_alloc(size, rb_flags);
5885 buf->data = alloc_percpu(struct trace_array_cpu);
5887 ring_buffer_free(buf->buffer);
5891 init_trace_buffers(tr, buf);
5893 /* Allocate the first page for all buffers */
5894 set_buffer_entries(&tr->trace_buffer,
5895 ring_buffer_size(tr->trace_buffer.buffer, 0));
5900 static int allocate_trace_buffers(struct trace_array *tr, int size)
5904 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
5908 #ifdef CONFIG_TRACER_MAX_TRACE
5909 ret = allocate_trace_buffer(tr, &tr->max_buffer,
5910 allocate_snapshot ? size : 1);
5912 ring_buffer_free(tr->trace_buffer.buffer);
5913 free_percpu(tr->trace_buffer.data);
5916 tr->allocated_snapshot = allocate_snapshot;
5919 * Only the top level trace array gets its snapshot allocated
5920 * from the kernel command line.
5922 allocate_snapshot = false;
5927 static int new_instance_create(const char *name)
5929 struct trace_array *tr;
5932 mutex_lock(&trace_types_lock);
5935 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5936 if (tr->name && strcmp(tr->name, name) == 0)
5941 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
5945 tr->name = kstrdup(name, GFP_KERNEL);
5949 raw_spin_lock_init(&tr->start_lock);
5951 tr->current_trace = &nop_trace;
5953 INIT_LIST_HEAD(&tr->systems);
5954 INIT_LIST_HEAD(&tr->events);
5956 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
5959 /* Holder for file callbacks */
5960 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5961 tr->trace_cpu.tr = tr;
5963 tr->dir = debugfs_create_dir(name, trace_instance_dir);
5967 ret = event_trace_add_tracer(tr->dir, tr);
5969 debugfs_remove_recursive(tr->dir);
5973 init_tracer_debugfs(tr, tr->dir);
5975 list_add(&tr->list, &ftrace_trace_arrays);
5977 mutex_unlock(&trace_types_lock);
5982 if (tr->trace_buffer.buffer)
5983 ring_buffer_free(tr->trace_buffer.buffer);
5988 mutex_unlock(&trace_types_lock);
5994 static int instance_delete(const char *name)
5996 struct trace_array *tr;
6000 mutex_lock(&trace_types_lock);
6003 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6004 if (tr->name && strcmp(tr->name, name) == 0) {
6016 list_del(&tr->list);
6018 event_trace_del_tracer(tr);
6019 debugfs_remove_recursive(tr->dir);
6020 free_percpu(tr->trace_buffer.data);
6021 ring_buffer_free(tr->trace_buffer.buffer);
6029 mutex_unlock(&trace_types_lock);
6034 static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
6036 struct dentry *parent;
6039 /* Paranoid: Make sure the parent is the "instances" directory */
6040 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
6041 if (WARN_ON_ONCE(parent != trace_instance_dir))
6045 * The inode mutex is locked, but debugfs_create_dir() will also
6046 * take the mutex. As the instances directory can not be destroyed
6047 * or changed in any other way, it is safe to unlock it, and
6048 * let the dentry try. If two users try to make the same dir at
6049 * the same time, then the new_instance_create() will determine the
6052 mutex_unlock(&inode->i_mutex);
6054 ret = new_instance_create(dentry->d_iname);
6056 mutex_lock(&inode->i_mutex);
6061 static int instance_rmdir(struct inode *inode, struct dentry *dentry)
6063 struct dentry *parent;
6066 /* Paranoid: Make sure the parent is the "instances" directory */
6067 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
6068 if (WARN_ON_ONCE(parent != trace_instance_dir))
6071 /* The caller did a dget() on dentry */
6072 mutex_unlock(&dentry->d_inode->i_mutex);
6075 * The inode mutex is locked, but debugfs_create_dir() will also
6076 * take the mutex. As the instances directory can not be destroyed
6077 * or changed in any other way, it is safe to unlock it, and
6078 * let the dentry try. If two users try to make the same dir at
6079 * the same time, then the instance_delete() will determine the
6082 mutex_unlock(&inode->i_mutex);
6084 ret = instance_delete(dentry->d_iname);
6086 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
6087 mutex_lock(&dentry->d_inode->i_mutex);
6092 static const struct inode_operations instance_dir_inode_operations = {
6093 .lookup = simple_lookup,
6094 .mkdir = instance_mkdir,
6095 .rmdir = instance_rmdir,
6098 static __init void create_trace_instances(struct dentry *d_tracer)
6100 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
6101 if (WARN_ON(!trace_instance_dir))
6104 /* Hijack the dir inode operations, to allow mkdir */
6105 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
6109 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
6113 trace_create_file("trace_options", 0644, d_tracer,
6114 tr, &tracing_iter_fops);
6116 trace_create_file("trace", 0644, d_tracer,
6119 trace_create_file("trace_pipe", 0444, d_tracer,
6120 tr, &tracing_pipe_fops);
6122 trace_create_file("buffer_size_kb", 0644, d_tracer,
6123 tr, &tracing_entries_fops);
6125 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
6126 tr, &tracing_total_entries_fops);
6128 trace_create_file("free_buffer", 0644, d_tracer,
6129 tr, &tracing_free_buffer_fops);
6131 trace_create_file("trace_marker", 0220, d_tracer,
6132 tr, &tracing_mark_fops);
6134 trace_create_file("trace_clock", 0644, d_tracer, tr,
6137 trace_create_file("tracing_on", 0644, d_tracer,
6138 tr, &rb_simple_fops);
6140 #ifdef CONFIG_TRACER_SNAPSHOT
6141 trace_create_file("snapshot", 0644, d_tracer,
6142 tr, &snapshot_fops);
6145 for_each_tracing_cpu(cpu)
6146 tracing_init_debugfs_percpu(tr, cpu);
6150 static __init int tracer_init_debugfs(void)
6152 struct dentry *d_tracer;
6154 trace_access_lock_init();
6156 d_tracer = tracing_init_dentry();
6160 init_tracer_debugfs(&global_trace, d_tracer);
6162 trace_create_file("tracing_cpumask", 0644, d_tracer,
6163 &global_trace, &tracing_cpumask_fops);
6165 trace_create_file("available_tracers", 0444, d_tracer,
6166 &global_trace, &show_traces_fops);
6168 trace_create_file("current_tracer", 0644, d_tracer,
6169 &global_trace, &set_tracer_fops);
6171 #ifdef CONFIG_TRACER_MAX_TRACE
6172 trace_create_file("tracing_max_latency", 0644, d_tracer,
6173 &tracing_max_latency, &tracing_max_lat_fops);
6176 trace_create_file("tracing_thresh", 0644, d_tracer,
6177 &tracing_thresh, &tracing_max_lat_fops);
6179 trace_create_file("README", 0444, d_tracer,
6180 NULL, &tracing_readme_fops);
6182 trace_create_file("saved_cmdlines", 0444, d_tracer,
6183 NULL, &tracing_saved_cmdlines_fops);
6185 #ifdef CONFIG_DYNAMIC_FTRACE
6186 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
6187 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
6190 create_trace_instances(d_tracer);
6192 create_trace_options_dir(&global_trace);
6197 static int trace_panic_handler(struct notifier_block *this,
6198 unsigned long event, void *unused)
6200 if (ftrace_dump_on_oops)
6201 ftrace_dump(ftrace_dump_on_oops);
6205 static struct notifier_block trace_panic_notifier = {
6206 .notifier_call = trace_panic_handler,
6208 .priority = 150 /* priority: INT_MAX >= x >= 0 */
6211 static int trace_die_handler(struct notifier_block *self,
6217 if (ftrace_dump_on_oops)
6218 ftrace_dump(ftrace_dump_on_oops);
6226 static struct notifier_block trace_die_notifier = {
6227 .notifier_call = trace_die_handler,
6232 * printk is set to max of 1024, we really don't need it that big.
6233 * Nothing should be printing 1000 characters anyway.
6235 #define TRACE_MAX_PRINT 1000
6238 * Define here KERN_TRACE so that we have one place to modify
6239 * it if we decide to change what log level the ftrace dump
6242 #define KERN_TRACE KERN_EMERG
6245 trace_printk_seq(struct trace_seq *s)
6247 /* Probably should print a warning here. */
6248 if (s->len >= TRACE_MAX_PRINT)
6249 s->len = TRACE_MAX_PRINT;
6251 /* should be zero ended, but we are paranoid. */
6252 s->buffer[s->len] = 0;
6254 printk(KERN_TRACE "%s", s->buffer);
6259 void trace_init_global_iter(struct trace_iterator *iter)
6261 iter->tr = &global_trace;
6262 iter->trace = iter->tr->current_trace;
6263 iter->cpu_file = RING_BUFFER_ALL_CPUS;
6264 iter->trace_buffer = &global_trace.trace_buffer;
6267 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
6269 /* use static because iter can be a bit big for the stack */
6270 static struct trace_iterator iter;
6271 static atomic_t dump_running;
6272 unsigned int old_userobj;
6273 unsigned long flags;
6276 /* Only allow one dump user at a time. */
6277 if (atomic_inc_return(&dump_running) != 1) {
6278 atomic_dec(&dump_running);
6283 * Always turn off tracing when we dump.
6284 * We don't need to show trace output of what happens
6285 * between multiple crashes.
6287 * If the user does a sysrq-z, then they can re-enable
6288 * tracing with echo 1 > tracing_on.
6292 local_irq_save(flags);
6294 /* Simulate the iterator */
6295 trace_init_global_iter(&iter);
6297 for_each_tracing_cpu(cpu) {
6298 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
6301 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
6303 /* don't look at user memory in panic mode */
6304 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
6306 switch (oops_dump_mode) {
6308 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6311 iter.cpu_file = raw_smp_processor_id();
6316 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
6317 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6320 printk(KERN_TRACE "Dumping ftrace buffer:\n");
6322 /* Did function tracer already get disabled? */
6323 if (ftrace_is_dead()) {
6324 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
6325 printk("# MAY BE MISSING FUNCTION EVENTS\n");
6329 * We need to stop all tracing on all CPUS to read the
6330 * the next buffer. This is a bit expensive, but is
6331 * not done often. We fill all what we can read,
6332 * and then release the locks again.
6335 while (!trace_empty(&iter)) {
6338 printk(KERN_TRACE "---------------------------------\n");
6342 /* reset all but tr, trace, and overruns */
6343 memset(&iter.seq, 0,
6344 sizeof(struct trace_iterator) -
6345 offsetof(struct trace_iterator, seq));
6346 iter.iter_flags |= TRACE_FILE_LAT_FMT;
6349 if (trace_find_next_entry_inc(&iter) != NULL) {
6352 ret = print_trace_line(&iter);
6353 if (ret != TRACE_TYPE_NO_CONSUME)
6354 trace_consume(&iter);
6356 touch_nmi_watchdog();
6358 trace_printk_seq(&iter.seq);
6362 printk(KERN_TRACE " (ftrace buffer empty)\n");
6364 printk(KERN_TRACE "---------------------------------\n");
6367 trace_flags |= old_userobj;
6369 for_each_tracing_cpu(cpu) {
6370 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
6372 atomic_dec(&dump_running);
6373 local_irq_restore(flags);
6375 EXPORT_SYMBOL_GPL(ftrace_dump);
6377 __init static int tracer_alloc_buffers(void)
6383 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
6386 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
6387 goto out_free_buffer_mask;
6389 /* Only allocate trace_printk buffers if a trace_printk exists */
6390 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
6391 /* Must be called before global_trace.buffer is allocated */
6392 trace_printk_init_buffers();
6394 /* To save memory, keep the ring buffer size to its minimum */
6395 if (ring_buffer_expanded)
6396 ring_buf_size = trace_buf_size;
6400 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
6401 cpumask_copy(tracing_cpumask, cpu_all_mask);
6403 raw_spin_lock_init(&global_trace.start_lock);
6405 /* TODO: make the number of buffers hot pluggable with CPUS */
6406 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
6407 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
6409 goto out_free_cpumask;
6412 if (global_trace.buffer_disabled)
6415 trace_init_cmdlines();
6418 * register_tracer() might reference current_trace, so it
6419 * needs to be set before we register anything. This is
6420 * just a bootstrap of current_trace anyway.
6422 global_trace.current_trace = &nop_trace;
6424 register_tracer(&nop_trace);
6426 /* All seems OK, enable tracing */
6427 tracing_disabled = 0;
6429 atomic_notifier_chain_register(&panic_notifier_list,
6430 &trace_panic_notifier);
6432 register_die_notifier(&trace_die_notifier);
6434 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
6436 /* Holder for file callbacks */
6437 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
6438 global_trace.trace_cpu.tr = &global_trace;
6440 INIT_LIST_HEAD(&global_trace.systems);
6441 INIT_LIST_HEAD(&global_trace.events);
6442 list_add(&global_trace.list, &ftrace_trace_arrays);
6444 while (trace_boot_options) {
6447 option = strsep(&trace_boot_options, ",");
6448 trace_set_options(&global_trace, option);
6451 register_snapshot_cmd();
6456 free_percpu(global_trace.trace_buffer.data);
6457 #ifdef CONFIG_TRACER_MAX_TRACE
6458 free_percpu(global_trace.max_buffer.data);
6460 free_cpumask_var(tracing_cpumask);
6461 out_free_buffer_mask:
6462 free_cpumask_var(tracing_buffer_mask);
6467 __init static int clear_boot_tracer(void)
6470 * The default tracer at boot buffer is an init section.
6471 * This function is called in lateinit. If we did not
6472 * find the boot tracer, then clear it out, to prevent
6473 * later registration from accessing the buffer that is
6474 * about to be freed.
6476 if (!default_bootup_tracer)
6479 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6480 default_bootup_tracer);
6481 default_bootup_tracer = NULL;
6486 early_initcall(tracer_alloc_buffers);
6487 fs_initcall(tracer_init_debugfs);
6488 late_initcall(clear_boot_tracer);