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 strncpy(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 strncpy(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 filter_current_check_discard(struct ring_buffer *buffer,
197 struct ftrace_event_call *call, void *rec,
198 struct ring_buffer_event *event)
200 return filter_check_discard(call, rec, buffer, event);
202 EXPORT_SYMBOL_GPL(filter_current_check_discard);
204 cycle_t ftrace_now(int cpu)
208 /* Early boot up does not have a buffer yet */
209 if (!global_trace.trace_buffer.buffer)
210 return trace_clock_local();
212 ts = ring_buffer_time_stamp(global_trace.trace_buffer.buffer, cpu);
213 ring_buffer_normalize_time_stamp(global_trace.trace_buffer.buffer, cpu, &ts);
218 int tracing_is_enabled(void)
220 return tracing_is_on();
224 * trace_buf_size is the size in bytes that is allocated
225 * for a buffer. Note, the number of bytes is always rounded
228 * This number is purposely set to a low number of 16384.
229 * If the dump on oops happens, it will be much appreciated
230 * to not have to wait for all that output. Anyway this can be
231 * boot time and run time configurable.
233 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
235 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
237 /* trace_types holds a link list of available tracers. */
238 static struct tracer *trace_types __read_mostly;
241 * trace_types_lock is used to protect the trace_types list.
243 static DEFINE_MUTEX(trace_types_lock);
246 * serialize the access of the ring buffer
248 * ring buffer serializes readers, but it is low level protection.
249 * The validity of the events (which returns by ring_buffer_peek() ..etc)
250 * are not protected by ring buffer.
252 * The content of events may become garbage if we allow other process consumes
253 * these events concurrently:
254 * A) the page of the consumed events may become a normal page
255 * (not reader page) in ring buffer, and this page will be rewrited
256 * by events producer.
257 * B) The page of the consumed events may become a page for splice_read,
258 * and this page will be returned to system.
260 * These primitives allow multi process access to different cpu ring buffer
263 * These primitives don't distinguish read-only and read-consume access.
264 * Multi read-only access are also serialized.
268 static DECLARE_RWSEM(all_cpu_access_lock);
269 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
271 static inline void trace_access_lock(int cpu)
273 if (cpu == RING_BUFFER_ALL_CPUS) {
274 /* gain it for accessing the whole ring buffer. */
275 down_write(&all_cpu_access_lock);
277 /* gain it for accessing a cpu ring buffer. */
279 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
280 down_read(&all_cpu_access_lock);
282 /* Secondly block other access to this @cpu ring buffer. */
283 mutex_lock(&per_cpu(cpu_access_lock, cpu));
287 static inline void trace_access_unlock(int cpu)
289 if (cpu == RING_BUFFER_ALL_CPUS) {
290 up_write(&all_cpu_access_lock);
292 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
293 up_read(&all_cpu_access_lock);
297 static inline void trace_access_lock_init(void)
301 for_each_possible_cpu(cpu)
302 mutex_init(&per_cpu(cpu_access_lock, cpu));
307 static DEFINE_MUTEX(access_lock);
309 static inline void trace_access_lock(int cpu)
312 mutex_lock(&access_lock);
315 static inline void trace_access_unlock(int cpu)
318 mutex_unlock(&access_lock);
321 static inline void trace_access_lock_init(void)
327 /* trace_flags holds trace_options default values */
328 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
329 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
330 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
331 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
334 * tracing_on - enable tracing buffers
336 * This function enables tracing buffers that may have been
337 * disabled with tracing_off.
339 void tracing_on(void)
341 if (global_trace.trace_buffer.buffer)
342 ring_buffer_record_on(global_trace.trace_buffer.buffer);
344 * This flag is only looked at when buffers haven't been
345 * allocated yet. We don't really care about the race
346 * between setting this flag and actually turning
349 global_trace.buffer_disabled = 0;
351 EXPORT_SYMBOL_GPL(tracing_on);
354 * __trace_puts - write a constant string into the trace buffer.
355 * @ip: The address of the caller
356 * @str: The constant string to write
357 * @size: The size of the string.
359 int __trace_puts(unsigned long ip, const char *str, int size)
361 struct ring_buffer_event *event;
362 struct ring_buffer *buffer;
363 struct print_entry *entry;
364 unsigned long irq_flags;
367 alloc = sizeof(*entry) + size + 2; /* possible \n added */
369 local_save_flags(irq_flags);
370 buffer = global_trace.trace_buffer.buffer;
371 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
372 irq_flags, preempt_count());
376 entry = ring_buffer_event_data(event);
379 memcpy(&entry->buf, str, size);
381 /* Add a newline if necessary */
382 if (entry->buf[size - 1] != '\n') {
383 entry->buf[size] = '\n';
384 entry->buf[size + 1] = '\0';
386 entry->buf[size] = '\0';
388 __buffer_unlock_commit(buffer, event);
392 EXPORT_SYMBOL_GPL(__trace_puts);
395 * __trace_bputs - write the pointer to a constant string into trace buffer
396 * @ip: The address of the caller
397 * @str: The constant string to write to the buffer to
399 int __trace_bputs(unsigned long ip, const char *str)
401 struct ring_buffer_event *event;
402 struct ring_buffer *buffer;
403 struct bputs_entry *entry;
404 unsigned long irq_flags;
405 int size = sizeof(struct bputs_entry);
407 local_save_flags(irq_flags);
408 buffer = global_trace.trace_buffer.buffer;
409 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
410 irq_flags, preempt_count());
414 entry = ring_buffer_event_data(event);
418 __buffer_unlock_commit(buffer, event);
422 EXPORT_SYMBOL_GPL(__trace_bputs);
424 #ifdef CONFIG_TRACER_SNAPSHOT
426 * trace_snapshot - take a snapshot of the current buffer.
428 * This causes a swap between the snapshot buffer and the current live
429 * tracing buffer. You can use this to take snapshots of the live
430 * trace when some condition is triggered, but continue to trace.
432 * Note, make sure to allocate the snapshot with either
433 * a tracing_snapshot_alloc(), or by doing it manually
434 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
436 * If the snapshot buffer is not allocated, it will stop tracing.
437 * Basically making a permanent snapshot.
439 void tracing_snapshot(void)
441 struct trace_array *tr = &global_trace;
442 struct tracer *tracer = tr->current_trace;
446 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
447 internal_trace_puts("*** snapshot is being ignored ***\n");
451 if (!tr->allocated_snapshot) {
452 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
453 internal_trace_puts("*** stopping trace here! ***\n");
458 /* Note, snapshot can not be used when the tracer uses it */
459 if (tracer->use_max_tr) {
460 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
461 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
465 local_irq_save(flags);
466 update_max_tr(tr, current, smp_processor_id());
467 local_irq_restore(flags);
469 EXPORT_SYMBOL_GPL(tracing_snapshot);
471 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
472 struct trace_buffer *size_buf, int cpu_id);
473 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
475 static int alloc_snapshot(struct trace_array *tr)
479 if (!tr->allocated_snapshot) {
481 /* allocate spare buffer */
482 ret = resize_buffer_duplicate_size(&tr->max_buffer,
483 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
487 tr->allocated_snapshot = true;
493 void free_snapshot(struct trace_array *tr)
496 * We don't free the ring buffer. instead, resize it because
497 * The max_tr ring buffer has some state (e.g. ring->clock) and
498 * we want preserve it.
500 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
501 set_buffer_entries(&tr->max_buffer, 1);
502 tracing_reset_online_cpus(&tr->max_buffer);
503 tr->allocated_snapshot = false;
507 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
509 * This is similar to trace_snapshot(), but it will allocate the
510 * snapshot buffer if it isn't already allocated. Use this only
511 * where it is safe to sleep, as the allocation may sleep.
513 * This causes a swap between the snapshot buffer and the current live
514 * tracing buffer. You can use this to take snapshots of the live
515 * trace when some condition is triggered, but continue to trace.
517 void tracing_snapshot_alloc(void)
519 struct trace_array *tr = &global_trace;
522 ret = alloc_snapshot(tr);
523 if (WARN_ON(ret < 0))
528 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
530 void tracing_snapshot(void)
532 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
534 EXPORT_SYMBOL_GPL(tracing_snapshot);
535 void tracing_snapshot_alloc(void)
540 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
541 #endif /* CONFIG_TRACER_SNAPSHOT */
544 * tracing_off - turn off tracing buffers
546 * This function stops the tracing buffers from recording data.
547 * It does not disable any overhead the tracers themselves may
548 * be causing. This function simply causes all recording to
549 * the ring buffers to fail.
551 void tracing_off(void)
553 if (global_trace.trace_buffer.buffer)
554 ring_buffer_record_off(global_trace.trace_buffer.buffer);
556 * This flag is only looked at when buffers haven't been
557 * allocated yet. We don't really care about the race
558 * between setting this flag and actually turning
561 global_trace.buffer_disabled = 1;
563 EXPORT_SYMBOL_GPL(tracing_off);
566 * tracing_is_on - show state of ring buffers enabled
568 int tracing_is_on(void)
570 if (global_trace.trace_buffer.buffer)
571 return ring_buffer_record_is_on(global_trace.trace_buffer.buffer);
572 return !global_trace.buffer_disabled;
574 EXPORT_SYMBOL_GPL(tracing_is_on);
576 static int __init set_buf_size(char *str)
578 unsigned long buf_size;
582 buf_size = memparse(str, &str);
583 /* nr_entries can not be zero */
586 trace_buf_size = buf_size;
589 __setup("trace_buf_size=", set_buf_size);
591 static int __init set_tracing_thresh(char *str)
593 unsigned long threshold;
598 ret = kstrtoul(str, 0, &threshold);
601 tracing_thresh = threshold * 1000;
604 __setup("tracing_thresh=", set_tracing_thresh);
606 unsigned long nsecs_to_usecs(unsigned long nsecs)
611 /* These must match the bit postions in trace_iterator_flags */
612 static const char *trace_options[] = {
645 int in_ns; /* is this clock in nanoseconds? */
647 { trace_clock_local, "local", 1 },
648 { trace_clock_global, "global", 1 },
649 { trace_clock_counter, "counter", 0 },
650 { trace_clock_jiffies, "uptime", 1 },
651 { trace_clock, "perf", 1 },
658 * trace_parser_get_init - gets the buffer for trace parser
660 int trace_parser_get_init(struct trace_parser *parser, int size)
662 memset(parser, 0, sizeof(*parser));
664 parser->buffer = kmalloc(size, GFP_KERNEL);
673 * trace_parser_put - frees the buffer for trace parser
675 void trace_parser_put(struct trace_parser *parser)
677 kfree(parser->buffer);
681 * trace_get_user - reads the user input string separated by space
682 * (matched by isspace(ch))
684 * For each string found the 'struct trace_parser' is updated,
685 * and the function returns.
687 * Returns number of bytes read.
689 * See kernel/trace/trace.h for 'struct trace_parser' details.
691 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
692 size_t cnt, loff_t *ppos)
699 trace_parser_clear(parser);
701 ret = get_user(ch, ubuf++);
709 * The parser is not finished with the last write,
710 * continue reading the user input without skipping spaces.
713 /* skip white space */
714 while (cnt && isspace(ch)) {
715 ret = get_user(ch, ubuf++);
722 /* only spaces were written */
732 /* read the non-space input */
733 while (cnt && !isspace(ch)) {
734 if (parser->idx < parser->size - 1)
735 parser->buffer[parser->idx++] = ch;
740 ret = get_user(ch, ubuf++);
747 /* We either got finished input or we have to wait for another call. */
749 parser->buffer[parser->idx] = 0;
750 parser->cont = false;
753 parser->buffer[parser->idx++] = ch;
763 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
771 if (s->len <= s->readpos)
774 len = s->len - s->readpos;
777 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
787 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
791 if (s->len <= s->readpos)
794 len = s->len - s->readpos;
797 memcpy(buf, s->buffer + s->readpos, cnt);
804 * ftrace_max_lock is used to protect the swapping of buffers
805 * when taking a max snapshot. The buffers themselves are
806 * protected by per_cpu spinlocks. But the action of the swap
807 * needs its own lock.
809 * This is defined as a arch_spinlock_t in order to help
810 * with performance when lockdep debugging is enabled.
812 * It is also used in other places outside the update_max_tr
813 * so it needs to be defined outside of the
814 * CONFIG_TRACER_MAX_TRACE.
816 static arch_spinlock_t ftrace_max_lock =
817 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
819 unsigned long __read_mostly tracing_thresh;
821 #ifdef CONFIG_TRACER_MAX_TRACE
822 unsigned long __read_mostly tracing_max_latency;
825 * Copy the new maximum trace into the separate maximum-trace
826 * structure. (this way the maximum trace is permanently saved,
827 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
830 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
832 struct trace_buffer *trace_buf = &tr->trace_buffer;
833 struct trace_buffer *max_buf = &tr->max_buffer;
834 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
835 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
838 max_buf->time_start = data->preempt_timestamp;
840 max_data->saved_latency = tracing_max_latency;
841 max_data->critical_start = data->critical_start;
842 max_data->critical_end = data->critical_end;
844 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
845 max_data->pid = tsk->pid;
846 max_data->uid = task_uid(tsk);
847 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
848 max_data->policy = tsk->policy;
849 max_data->rt_priority = tsk->rt_priority;
851 /* record this tasks comm */
852 tracing_record_cmdline(tsk);
856 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
858 * @tsk: the task with the latency
859 * @cpu: The cpu that initiated the trace.
861 * Flip the buffers between the @tr and the max_tr and record information
862 * about which task was the cause of this latency.
865 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
867 struct ring_buffer *buf;
872 WARN_ON_ONCE(!irqs_disabled());
874 if (!tr->allocated_snapshot) {
875 /* Only the nop tracer should hit this when disabling */
876 WARN_ON_ONCE(tr->current_trace != &nop_trace);
880 arch_spin_lock(&ftrace_max_lock);
882 buf = tr->trace_buffer.buffer;
883 tr->trace_buffer.buffer = tr->max_buffer.buffer;
884 tr->max_buffer.buffer = buf;
886 __update_max_tr(tr, tsk, cpu);
887 arch_spin_unlock(&ftrace_max_lock);
891 * update_max_tr_single - only copy one trace over, and reset the rest
893 * @tsk - task with the latency
894 * @cpu - the cpu of the buffer to copy.
896 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
899 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
906 WARN_ON_ONCE(!irqs_disabled());
907 if (WARN_ON_ONCE(!tr->allocated_snapshot))
910 arch_spin_lock(&ftrace_max_lock);
912 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
916 * We failed to swap the buffer due to a commit taking
917 * place on this CPU. We fail to record, but we reset
918 * the max trace buffer (no one writes directly to it)
919 * and flag that it failed.
921 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
922 "Failed to swap buffers due to commit in progress\n");
925 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
927 __update_max_tr(tr, tsk, cpu);
928 arch_spin_unlock(&ftrace_max_lock);
930 #endif /* CONFIG_TRACER_MAX_TRACE */
932 static void default_wait_pipe(struct trace_iterator *iter)
934 /* Iterators are static, they should be filled or empty */
935 if (trace_buffer_iter(iter, iter->cpu_file))
938 ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
941 #ifdef CONFIG_FTRACE_STARTUP_TEST
942 static int run_tracer_selftest(struct tracer *type)
944 struct trace_array *tr = &global_trace;
945 struct tracer *saved_tracer = tr->current_trace;
948 if (!type->selftest || tracing_selftest_disabled)
952 * Run a selftest on this tracer.
953 * Here we reset the trace buffer, and set the current
954 * tracer to be this tracer. The tracer can then run some
955 * internal tracing to verify that everything is in order.
956 * If we fail, we do not register this tracer.
958 tracing_reset_online_cpus(&tr->trace_buffer);
960 tr->current_trace = type;
962 #ifdef CONFIG_TRACER_MAX_TRACE
963 if (type->use_max_tr) {
964 /* If we expanded the buffers, make sure the max is expanded too */
965 if (ring_buffer_expanded)
966 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
967 RING_BUFFER_ALL_CPUS);
968 tr->allocated_snapshot = true;
972 /* the test is responsible for initializing and enabling */
973 pr_info("Testing tracer %s: ", type->name);
974 ret = type->selftest(type, tr);
975 /* the test is responsible for resetting too */
976 tr->current_trace = saved_tracer;
978 printk(KERN_CONT "FAILED!\n");
979 /* Add the warning after printing 'FAILED' */
983 /* Only reset on passing, to avoid touching corrupted buffers */
984 tracing_reset_online_cpus(&tr->trace_buffer);
986 #ifdef CONFIG_TRACER_MAX_TRACE
987 if (type->use_max_tr) {
988 tr->allocated_snapshot = false;
990 /* Shrink the max buffer again */
991 if (ring_buffer_expanded)
992 ring_buffer_resize(tr->max_buffer.buffer, 1,
993 RING_BUFFER_ALL_CPUS);
997 printk(KERN_CONT "PASSED\n");
1001 static inline int run_tracer_selftest(struct tracer *type)
1005 #endif /* CONFIG_FTRACE_STARTUP_TEST */
1008 * register_tracer - register a tracer with the ftrace system.
1009 * @type - the plugin for the tracer
1011 * Register a new plugin tracer.
1013 int register_tracer(struct tracer *type)
1019 pr_info("Tracer must have a name\n");
1023 if (strlen(type->name) >= MAX_TRACER_SIZE) {
1024 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1028 mutex_lock(&trace_types_lock);
1030 tracing_selftest_running = true;
1032 for (t = trace_types; t; t = t->next) {
1033 if (strcmp(type->name, t->name) == 0) {
1035 pr_info("Tracer %s already registered\n",
1042 if (!type->set_flag)
1043 type->set_flag = &dummy_set_flag;
1045 type->flags = &dummy_tracer_flags;
1047 if (!type->flags->opts)
1048 type->flags->opts = dummy_tracer_opt;
1049 if (!type->wait_pipe)
1050 type->wait_pipe = default_wait_pipe;
1052 ret = run_tracer_selftest(type);
1056 type->next = trace_types;
1060 tracing_selftest_running = false;
1061 mutex_unlock(&trace_types_lock);
1063 if (ret || !default_bootup_tracer)
1066 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
1069 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1070 /* Do we want this tracer to start on bootup? */
1071 tracing_set_tracer(type->name);
1072 default_bootup_tracer = NULL;
1073 /* disable other selftests, since this will break it. */
1074 tracing_selftest_disabled = true;
1075 #ifdef CONFIG_FTRACE_STARTUP_TEST
1076 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1084 void tracing_reset(struct trace_buffer *buf, int cpu)
1086 struct ring_buffer *buffer = buf->buffer;
1091 ring_buffer_record_disable(buffer);
1093 /* Make sure all commits have finished */
1094 synchronize_sched();
1095 ring_buffer_reset_cpu(buffer, cpu);
1097 ring_buffer_record_enable(buffer);
1100 void tracing_reset_online_cpus(struct trace_buffer *buf)
1102 struct ring_buffer *buffer = buf->buffer;
1108 ring_buffer_record_disable(buffer);
1110 /* Make sure all commits have finished */
1111 synchronize_sched();
1113 buf->time_start = ftrace_now(buf->cpu);
1115 for_each_online_cpu(cpu)
1116 ring_buffer_reset_cpu(buffer, cpu);
1118 ring_buffer_record_enable(buffer);
1121 void tracing_reset_current(int cpu)
1123 tracing_reset(&global_trace.trace_buffer, cpu);
1126 void tracing_reset_all_online_cpus(void)
1128 struct trace_array *tr;
1130 mutex_lock(&trace_types_lock);
1131 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1132 tracing_reset_online_cpus(&tr->trace_buffer);
1133 #ifdef CONFIG_TRACER_MAX_TRACE
1134 tracing_reset_online_cpus(&tr->max_buffer);
1137 mutex_unlock(&trace_types_lock);
1140 #define SAVED_CMDLINES 128
1141 #define NO_CMDLINE_MAP UINT_MAX
1142 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1143 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1144 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1145 static int cmdline_idx;
1146 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1148 /* temporary disable recording */
1149 static atomic_t trace_record_cmdline_disabled __read_mostly;
1151 static void trace_init_cmdlines(void)
1153 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1154 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
1158 int is_tracing_stopped(void)
1160 return global_trace.stop_count;
1164 * ftrace_off_permanent - disable all ftrace code permanently
1166 * This should only be called when a serious anomally has
1167 * been detected. This will turn off the function tracing,
1168 * ring buffers, and other tracing utilites. It takes no
1169 * locks and can be called from any context.
1171 void ftrace_off_permanent(void)
1173 tracing_disabled = 1;
1175 tracing_off_permanent();
1179 * tracing_start - quick start of the tracer
1181 * If tracing is enabled but was stopped by tracing_stop,
1182 * this will start the tracer back up.
1184 void tracing_start(void)
1186 struct ring_buffer *buffer;
1187 unsigned long flags;
1189 if (tracing_disabled)
1192 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1193 if (--global_trace.stop_count) {
1194 if (global_trace.stop_count < 0) {
1195 /* Someone screwed up their debugging */
1197 global_trace.stop_count = 0;
1202 /* Prevent the buffers from switching */
1203 arch_spin_lock(&ftrace_max_lock);
1205 buffer = global_trace.trace_buffer.buffer;
1207 ring_buffer_record_enable(buffer);
1209 #ifdef CONFIG_TRACER_MAX_TRACE
1210 buffer = global_trace.max_buffer.buffer;
1212 ring_buffer_record_enable(buffer);
1215 arch_spin_unlock(&ftrace_max_lock);
1219 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1222 static void tracing_start_tr(struct trace_array *tr)
1224 struct ring_buffer *buffer;
1225 unsigned long flags;
1227 if (tracing_disabled)
1230 /* If global, we need to also start the max tracer */
1231 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1232 return tracing_start();
1234 raw_spin_lock_irqsave(&tr->start_lock, flags);
1236 if (--tr->stop_count) {
1237 if (tr->stop_count < 0) {
1238 /* Someone screwed up their debugging */
1245 buffer = tr->trace_buffer.buffer;
1247 ring_buffer_record_enable(buffer);
1250 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1254 * tracing_stop - quick stop of the tracer
1256 * Light weight way to stop tracing. Use in conjunction with
1259 void tracing_stop(void)
1261 struct ring_buffer *buffer;
1262 unsigned long flags;
1265 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1266 if (global_trace.stop_count++)
1269 /* Prevent the buffers from switching */
1270 arch_spin_lock(&ftrace_max_lock);
1272 buffer = global_trace.trace_buffer.buffer;
1274 ring_buffer_record_disable(buffer);
1276 #ifdef CONFIG_TRACER_MAX_TRACE
1277 buffer = global_trace.max_buffer.buffer;
1279 ring_buffer_record_disable(buffer);
1282 arch_spin_unlock(&ftrace_max_lock);
1285 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1288 static void tracing_stop_tr(struct trace_array *tr)
1290 struct ring_buffer *buffer;
1291 unsigned long flags;
1293 /* If global, we need to also stop the max tracer */
1294 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1295 return tracing_stop();
1297 raw_spin_lock_irqsave(&tr->start_lock, flags);
1298 if (tr->stop_count++)
1301 buffer = tr->trace_buffer.buffer;
1303 ring_buffer_record_disable(buffer);
1306 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1309 void trace_stop_cmdline_recording(void);
1311 static void trace_save_cmdline(struct task_struct *tsk)
1315 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1319 * It's not the end of the world if we don't get
1320 * the lock, but we also don't want to spin
1321 * nor do we want to disable interrupts,
1322 * so if we miss here, then better luck next time.
1324 if (!arch_spin_trylock(&trace_cmdline_lock))
1327 idx = map_pid_to_cmdline[tsk->pid];
1328 if (idx == NO_CMDLINE_MAP) {
1329 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1332 * Check whether the cmdline buffer at idx has a pid
1333 * mapped. We are going to overwrite that entry so we
1334 * need to clear the map_pid_to_cmdline. Otherwise we
1335 * would read the new comm for the old pid.
1337 pid = map_cmdline_to_pid[idx];
1338 if (pid != NO_CMDLINE_MAP)
1339 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1341 map_cmdline_to_pid[idx] = tsk->pid;
1342 map_pid_to_cmdline[tsk->pid] = idx;
1347 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1349 arch_spin_unlock(&trace_cmdline_lock);
1352 void trace_find_cmdline(int pid, char comm[])
1357 strcpy(comm, "<idle>");
1361 if (WARN_ON_ONCE(pid < 0)) {
1362 strcpy(comm, "<XXX>");
1366 if (pid > PID_MAX_DEFAULT) {
1367 strcpy(comm, "<...>");
1372 arch_spin_lock(&trace_cmdline_lock);
1373 map = map_pid_to_cmdline[pid];
1374 if (map != NO_CMDLINE_MAP)
1375 strcpy(comm, saved_cmdlines[map]);
1377 strcpy(comm, "<...>");
1379 arch_spin_unlock(&trace_cmdline_lock);
1383 void tracing_record_cmdline(struct task_struct *tsk)
1385 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1388 if (!__this_cpu_read(trace_cmdline_save))
1391 __this_cpu_write(trace_cmdline_save, false);
1393 trace_save_cmdline(tsk);
1397 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1400 struct task_struct *tsk = current;
1402 entry->preempt_count = pc & 0xff;
1403 entry->pid = (tsk) ? tsk->pid : 0;
1405 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1406 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1408 TRACE_FLAG_IRQS_NOSUPPORT |
1410 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1411 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1412 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1414 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1416 struct ring_buffer_event *
1417 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1420 unsigned long flags, int pc)
1422 struct ring_buffer_event *event;
1424 event = ring_buffer_lock_reserve(buffer, len);
1425 if (event != NULL) {
1426 struct trace_entry *ent = ring_buffer_event_data(event);
1428 tracing_generic_entry_update(ent, flags, pc);
1436 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1438 __this_cpu_write(trace_cmdline_save, true);
1439 ring_buffer_unlock_commit(buffer, event);
1443 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1444 struct ring_buffer_event *event,
1445 unsigned long flags, int pc)
1447 __buffer_unlock_commit(buffer, event);
1449 ftrace_trace_stack(buffer, flags, 6, pc);
1450 ftrace_trace_userstack(buffer, flags, pc);
1453 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1454 struct ring_buffer_event *event,
1455 unsigned long flags, int pc)
1457 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1459 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1461 struct ring_buffer_event *
1462 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1463 struct ftrace_event_file *ftrace_file,
1464 int type, unsigned long len,
1465 unsigned long flags, int pc)
1467 *current_rb = ftrace_file->tr->trace_buffer.buffer;
1468 return trace_buffer_lock_reserve(*current_rb,
1469 type, len, flags, pc);
1471 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1473 struct ring_buffer_event *
1474 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1475 int type, unsigned long len,
1476 unsigned long flags, int pc)
1478 *current_rb = global_trace.trace_buffer.buffer;
1479 return trace_buffer_lock_reserve(*current_rb,
1480 type, len, flags, pc);
1482 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1484 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1485 struct ring_buffer_event *event,
1486 unsigned long flags, int pc)
1488 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1490 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1492 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1493 struct ring_buffer_event *event,
1494 unsigned long flags, int pc,
1495 struct pt_regs *regs)
1497 __buffer_unlock_commit(buffer, event);
1499 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1500 ftrace_trace_userstack(buffer, flags, pc);
1502 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1504 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1505 struct ring_buffer_event *event)
1507 ring_buffer_discard_commit(buffer, event);
1509 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1512 trace_function(struct trace_array *tr,
1513 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1516 struct ftrace_event_call *call = &event_function;
1517 struct ring_buffer *buffer = tr->trace_buffer.buffer;
1518 struct ring_buffer_event *event;
1519 struct ftrace_entry *entry;
1521 /* If we are reading the ring buffer, don't trace */
1522 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1525 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1529 entry = ring_buffer_event_data(event);
1531 entry->parent_ip = parent_ip;
1533 if (!filter_check_discard(call, entry, buffer, event))
1534 __buffer_unlock_commit(buffer, event);
1538 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1539 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1542 if (likely(!atomic_read(&data->disabled)))
1543 trace_function(tr, ip, parent_ip, flags, pc);
1546 #ifdef CONFIG_STACKTRACE
1548 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1549 struct ftrace_stack {
1550 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1553 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1554 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1556 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1557 unsigned long flags,
1558 int skip, int pc, struct pt_regs *regs)
1560 struct ftrace_event_call *call = &event_kernel_stack;
1561 struct ring_buffer_event *event;
1562 struct stack_entry *entry;
1563 struct stack_trace trace;
1565 int size = FTRACE_STACK_ENTRIES;
1567 trace.nr_entries = 0;
1571 * Since events can happen in NMIs there's no safe way to
1572 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1573 * or NMI comes in, it will just have to use the default
1574 * FTRACE_STACK_SIZE.
1576 preempt_disable_notrace();
1578 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1580 * We don't need any atomic variables, just a barrier.
1581 * If an interrupt comes in, we don't care, because it would
1582 * have exited and put the counter back to what we want.
1583 * We just need a barrier to keep gcc from moving things
1587 if (use_stack == 1) {
1588 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1589 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1592 save_stack_trace_regs(regs, &trace);
1594 save_stack_trace(&trace);
1596 if (trace.nr_entries > size)
1597 size = trace.nr_entries;
1599 /* From now on, use_stack is a boolean */
1602 size *= sizeof(unsigned long);
1604 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1605 sizeof(*entry) + size, flags, pc);
1608 entry = ring_buffer_event_data(event);
1610 memset(&entry->caller, 0, size);
1613 memcpy(&entry->caller, trace.entries,
1614 trace.nr_entries * sizeof(unsigned long));
1616 trace.max_entries = FTRACE_STACK_ENTRIES;
1617 trace.entries = entry->caller;
1619 save_stack_trace_regs(regs, &trace);
1621 save_stack_trace(&trace);
1624 entry->size = trace.nr_entries;
1626 if (!filter_check_discard(call, entry, buffer, event))
1627 __buffer_unlock_commit(buffer, event);
1630 /* Again, don't let gcc optimize things here */
1632 __this_cpu_dec(ftrace_stack_reserve);
1633 preempt_enable_notrace();
1637 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1638 int skip, int pc, struct pt_regs *regs)
1640 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1643 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1646 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1649 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1652 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1655 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1658 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
1662 * trace_dump_stack - record a stack back trace in the trace buffer
1663 * @skip: Number of functions to skip (helper handlers)
1665 void trace_dump_stack(int skip)
1667 unsigned long flags;
1669 if (tracing_disabled || tracing_selftest_running)
1672 local_save_flags(flags);
1675 * Skip 3 more, seems to get us at the caller of
1679 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1680 flags, skip, preempt_count(), NULL);
1683 static DEFINE_PER_CPU(int, user_stack_count);
1686 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1688 struct ftrace_event_call *call = &event_user_stack;
1689 struct ring_buffer_event *event;
1690 struct userstack_entry *entry;
1691 struct stack_trace trace;
1693 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1697 * NMIs can not handle page faults, even with fix ups.
1698 * The save user stack can (and often does) fault.
1700 if (unlikely(in_nmi()))
1704 * prevent recursion, since the user stack tracing may
1705 * trigger other kernel events.
1708 if (__this_cpu_read(user_stack_count))
1711 __this_cpu_inc(user_stack_count);
1713 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1714 sizeof(*entry), flags, pc);
1716 goto out_drop_count;
1717 entry = ring_buffer_event_data(event);
1719 entry->tgid = current->tgid;
1720 memset(&entry->caller, 0, sizeof(entry->caller));
1722 trace.nr_entries = 0;
1723 trace.max_entries = FTRACE_STACK_ENTRIES;
1725 trace.entries = entry->caller;
1727 save_stack_trace_user(&trace);
1728 if (!filter_check_discard(call, entry, buffer, event))
1729 __buffer_unlock_commit(buffer, event);
1732 __this_cpu_dec(user_stack_count);
1738 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1740 ftrace_trace_userstack(tr, flags, preempt_count());
1744 #endif /* CONFIG_STACKTRACE */
1746 /* created for use with alloc_percpu */
1747 struct trace_buffer_struct {
1748 char buffer[TRACE_BUF_SIZE];
1751 static struct trace_buffer_struct *trace_percpu_buffer;
1752 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1753 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1754 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1757 * The buffer used is dependent on the context. There is a per cpu
1758 * buffer for normal context, softirq contex, hard irq context and
1759 * for NMI context. Thise allows for lockless recording.
1761 * Note, if the buffers failed to be allocated, then this returns NULL
1763 static char *get_trace_buf(void)
1765 struct trace_buffer_struct *percpu_buffer;
1768 * If we have allocated per cpu buffers, then we do not
1769 * need to do any locking.
1772 percpu_buffer = trace_percpu_nmi_buffer;
1774 percpu_buffer = trace_percpu_irq_buffer;
1775 else if (in_softirq())
1776 percpu_buffer = trace_percpu_sirq_buffer;
1778 percpu_buffer = trace_percpu_buffer;
1783 return this_cpu_ptr(&percpu_buffer->buffer[0]);
1786 static int alloc_percpu_trace_buffer(void)
1788 struct trace_buffer_struct *buffers;
1789 struct trace_buffer_struct *sirq_buffers;
1790 struct trace_buffer_struct *irq_buffers;
1791 struct trace_buffer_struct *nmi_buffers;
1793 buffers = alloc_percpu(struct trace_buffer_struct);
1797 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1801 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1805 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1809 trace_percpu_buffer = buffers;
1810 trace_percpu_sirq_buffer = sirq_buffers;
1811 trace_percpu_irq_buffer = irq_buffers;
1812 trace_percpu_nmi_buffer = nmi_buffers;
1817 free_percpu(irq_buffers);
1819 free_percpu(sirq_buffers);
1821 free_percpu(buffers);
1823 WARN(1, "Could not allocate percpu trace_printk buffer");
1827 static int buffers_allocated;
1829 void trace_printk_init_buffers(void)
1831 if (buffers_allocated)
1834 if (alloc_percpu_trace_buffer())
1837 pr_info("ftrace: Allocated trace_printk buffers\n");
1839 /* Expand the buffers to set size */
1840 tracing_update_buffers();
1842 buffers_allocated = 1;
1845 * trace_printk_init_buffers() can be called by modules.
1846 * If that happens, then we need to start cmdline recording
1847 * directly here. If the global_trace.buffer is already
1848 * allocated here, then this was called by module code.
1850 if (global_trace.trace_buffer.buffer)
1851 tracing_start_cmdline_record();
1854 void trace_printk_start_comm(void)
1856 /* Start tracing comms if trace printk is set */
1857 if (!buffers_allocated)
1859 tracing_start_cmdline_record();
1862 static void trace_printk_start_stop_comm(int enabled)
1864 if (!buffers_allocated)
1868 tracing_start_cmdline_record();
1870 tracing_stop_cmdline_record();
1874 * trace_vbprintk - write binary msg to tracing buffer
1877 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1879 struct ftrace_event_call *call = &event_bprint;
1880 struct ring_buffer_event *event;
1881 struct ring_buffer *buffer;
1882 struct trace_array *tr = &global_trace;
1883 struct bprint_entry *entry;
1884 unsigned long flags;
1886 int len = 0, size, pc;
1888 if (unlikely(tracing_selftest_running || tracing_disabled))
1891 /* Don't pollute graph traces with trace_vprintk internals */
1892 pause_graph_tracing();
1894 pc = preempt_count();
1895 preempt_disable_notrace();
1897 tbuffer = get_trace_buf();
1903 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1905 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1908 local_save_flags(flags);
1909 size = sizeof(*entry) + sizeof(u32) * len;
1910 buffer = tr->trace_buffer.buffer;
1911 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1915 entry = ring_buffer_event_data(event);
1919 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1920 if (!filter_check_discard(call, entry, buffer, event)) {
1921 __buffer_unlock_commit(buffer, event);
1922 ftrace_trace_stack(buffer, flags, 6, pc);
1926 preempt_enable_notrace();
1927 unpause_graph_tracing();
1931 EXPORT_SYMBOL_GPL(trace_vbprintk);
1934 __trace_array_vprintk(struct ring_buffer *buffer,
1935 unsigned long ip, const char *fmt, va_list args)
1937 struct ftrace_event_call *call = &event_print;
1938 struct ring_buffer_event *event;
1939 int len = 0, size, pc;
1940 struct print_entry *entry;
1941 unsigned long flags;
1944 if (tracing_disabled || tracing_selftest_running)
1947 /* Don't pollute graph traces with trace_vprintk internals */
1948 pause_graph_tracing();
1950 pc = preempt_count();
1951 preempt_disable_notrace();
1954 tbuffer = get_trace_buf();
1960 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1961 if (len > TRACE_BUF_SIZE)
1964 local_save_flags(flags);
1965 size = sizeof(*entry) + len + 1;
1966 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1970 entry = ring_buffer_event_data(event);
1973 memcpy(&entry->buf, tbuffer, len);
1974 entry->buf[len] = '\0';
1975 if (!filter_check_discard(call, entry, buffer, event)) {
1976 __buffer_unlock_commit(buffer, event);
1977 ftrace_trace_stack(buffer, flags, 6, pc);
1980 preempt_enable_notrace();
1981 unpause_graph_tracing();
1986 int trace_array_vprintk(struct trace_array *tr,
1987 unsigned long ip, const char *fmt, va_list args)
1989 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
1992 int trace_array_printk(struct trace_array *tr,
1993 unsigned long ip, const char *fmt, ...)
1998 if (!(trace_flags & TRACE_ITER_PRINTK))
2002 ret = trace_array_vprintk(tr, ip, fmt, ap);
2007 int trace_array_printk_buf(struct ring_buffer *buffer,
2008 unsigned long ip, const char *fmt, ...)
2013 if (!(trace_flags & TRACE_ITER_PRINTK))
2017 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2022 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2024 return trace_array_vprintk(&global_trace, ip, fmt, args);
2026 EXPORT_SYMBOL_GPL(trace_vprintk);
2028 static void trace_iterator_increment(struct trace_iterator *iter)
2030 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2034 ring_buffer_read(buf_iter, NULL);
2037 static struct trace_entry *
2038 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2039 unsigned long *lost_events)
2041 struct ring_buffer_event *event;
2042 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
2045 event = ring_buffer_iter_peek(buf_iter, ts);
2047 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
2051 iter->ent_size = ring_buffer_event_length(event);
2052 return ring_buffer_event_data(event);
2058 static struct trace_entry *
2059 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2060 unsigned long *missing_events, u64 *ent_ts)
2062 struct ring_buffer *buffer = iter->trace_buffer->buffer;
2063 struct trace_entry *ent, *next = NULL;
2064 unsigned long lost_events = 0, next_lost = 0;
2065 int cpu_file = iter->cpu_file;
2066 u64 next_ts = 0, ts;
2072 * If we are in a per_cpu trace file, don't bother by iterating over
2073 * all cpu and peek directly.
2075 if (cpu_file > RING_BUFFER_ALL_CPUS) {
2076 if (ring_buffer_empty_cpu(buffer, cpu_file))
2078 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
2080 *ent_cpu = cpu_file;
2085 for_each_tracing_cpu(cpu) {
2087 if (ring_buffer_empty_cpu(buffer, cpu))
2090 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
2093 * Pick the entry with the smallest timestamp:
2095 if (ent && (!next || ts < next_ts)) {
2099 next_lost = lost_events;
2100 next_size = iter->ent_size;
2104 iter->ent_size = next_size;
2107 *ent_cpu = next_cpu;
2113 *missing_events = next_lost;
2118 /* Find the next real entry, without updating the iterator itself */
2119 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2120 int *ent_cpu, u64 *ent_ts)
2122 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
2125 /* Find the next real entry, and increment the iterator to the next entry */
2126 void *trace_find_next_entry_inc(struct trace_iterator *iter)
2128 iter->ent = __find_next_entry(iter, &iter->cpu,
2129 &iter->lost_events, &iter->ts);
2132 trace_iterator_increment(iter);
2134 return iter->ent ? iter : NULL;
2137 static void trace_consume(struct trace_iterator *iter)
2139 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
2140 &iter->lost_events);
2143 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
2145 struct trace_iterator *iter = m->private;
2149 WARN_ON_ONCE(iter->leftover);
2153 /* can't go backwards */
2158 ent = trace_find_next_entry_inc(iter);
2162 while (ent && iter->idx < i)
2163 ent = trace_find_next_entry_inc(iter);
2170 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2172 struct ring_buffer_event *event;
2173 struct ring_buffer_iter *buf_iter;
2174 unsigned long entries = 0;
2177 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2179 buf_iter = trace_buffer_iter(iter, cpu);
2183 ring_buffer_iter_reset(buf_iter);
2186 * We could have the case with the max latency tracers
2187 * that a reset never took place on a cpu. This is evident
2188 * by the timestamp being before the start of the buffer.
2190 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
2191 if (ts >= iter->trace_buffer->time_start)
2194 ring_buffer_read(buf_iter, NULL);
2197 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2201 * The current tracer is copied to avoid a global locking
2204 static void *s_start(struct seq_file *m, loff_t *pos)
2206 struct trace_iterator *iter = m->private;
2207 struct trace_array *tr = iter->tr;
2208 int cpu_file = iter->cpu_file;
2214 * copy the tracer to avoid using a global lock all around.
2215 * iter->trace is a copy of current_trace, the pointer to the
2216 * name may be used instead of a strcmp(), as iter->trace->name
2217 * will point to the same string as current_trace->name.
2219 mutex_lock(&trace_types_lock);
2220 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2221 *iter->trace = *tr->current_trace;
2222 mutex_unlock(&trace_types_lock);
2224 #ifdef CONFIG_TRACER_MAX_TRACE
2225 if (iter->snapshot && iter->trace->use_max_tr)
2226 return ERR_PTR(-EBUSY);
2229 if (!iter->snapshot)
2230 atomic_inc(&trace_record_cmdline_disabled);
2232 if (*pos != iter->pos) {
2237 if (cpu_file == RING_BUFFER_ALL_CPUS) {
2238 for_each_tracing_cpu(cpu)
2239 tracing_iter_reset(iter, cpu);
2241 tracing_iter_reset(iter, cpu_file);
2244 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2249 * If we overflowed the seq_file before, then we want
2250 * to just reuse the trace_seq buffer again.
2256 p = s_next(m, p, &l);
2260 trace_event_read_lock();
2261 trace_access_lock(cpu_file);
2265 static void s_stop(struct seq_file *m, void *p)
2267 struct trace_iterator *iter = m->private;
2269 #ifdef CONFIG_TRACER_MAX_TRACE
2270 if (iter->snapshot && iter->trace->use_max_tr)
2274 if (!iter->snapshot)
2275 atomic_dec(&trace_record_cmdline_disabled);
2277 trace_access_unlock(iter->cpu_file);
2278 trace_event_read_unlock();
2282 get_total_entries(struct trace_buffer *buf,
2283 unsigned long *total, unsigned long *entries)
2285 unsigned long count;
2291 for_each_tracing_cpu(cpu) {
2292 count = ring_buffer_entries_cpu(buf->buffer, cpu);
2294 * If this buffer has skipped entries, then we hold all
2295 * entries for the trace and we need to ignore the
2296 * ones before the time stamp.
2298 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2299 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
2300 /* total is the same as the entries */
2304 ring_buffer_overrun_cpu(buf->buffer, cpu);
2309 static void print_lat_help_header(struct seq_file *m)
2311 seq_puts(m, "# _------=> CPU# \n");
2312 seq_puts(m, "# / _-----=> irqs-off \n");
2313 seq_puts(m, "# | / _----=> need-resched \n");
2314 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2315 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2316 seq_puts(m, "# |||| / delay \n");
2317 seq_puts(m, "# cmd pid ||||| time | caller \n");
2318 seq_puts(m, "# \\ / ||||| \\ | / \n");
2321 static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
2323 unsigned long total;
2324 unsigned long entries;
2326 get_total_entries(buf, &total, &entries);
2327 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2328 entries, total, num_online_cpus());
2332 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
2334 print_event_info(buf, m);
2335 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2336 seq_puts(m, "# | | | | |\n");
2339 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
2341 print_event_info(buf, m);
2342 seq_puts(m, "# _-----=> irqs-off\n");
2343 seq_puts(m, "# / _----=> need-resched\n");
2344 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2345 seq_puts(m, "# || / _--=> preempt-depth\n");
2346 seq_puts(m, "# ||| / delay\n");
2347 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2348 seq_puts(m, "# | | | |||| | |\n");
2352 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2354 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2355 struct trace_buffer *buf = iter->trace_buffer;
2356 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2357 struct tracer *type = iter->trace;
2358 unsigned long entries;
2359 unsigned long total;
2360 const char *name = "preemption";
2364 get_total_entries(buf, &total, &entries);
2366 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2368 seq_puts(m, "# -----------------------------------"
2369 "---------------------------------\n");
2370 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2371 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2372 nsecs_to_usecs(data->saved_latency),
2376 #if defined(CONFIG_PREEMPT_NONE)
2378 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2380 #elif defined(CONFIG_PREEMPT)
2385 /* These are reserved for later use */
2388 seq_printf(m, " #P:%d)\n", num_online_cpus());
2392 seq_puts(m, "# -----------------\n");
2393 seq_printf(m, "# | task: %.16s-%d "
2394 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2395 data->comm, data->pid,
2396 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2397 data->policy, data->rt_priority);
2398 seq_puts(m, "# -----------------\n");
2400 if (data->critical_start) {
2401 seq_puts(m, "# => started at: ");
2402 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2403 trace_print_seq(m, &iter->seq);
2404 seq_puts(m, "\n# => ended at: ");
2405 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2406 trace_print_seq(m, &iter->seq);
2407 seq_puts(m, "\n#\n");
2413 static void test_cpu_buff_start(struct trace_iterator *iter)
2415 struct trace_seq *s = &iter->seq;
2417 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2420 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2423 if (cpumask_test_cpu(iter->cpu, iter->started))
2426 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2429 cpumask_set_cpu(iter->cpu, iter->started);
2431 /* Don't print started cpu buffer for the first entry of the trace */
2433 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2437 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2439 struct trace_seq *s = &iter->seq;
2440 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2441 struct trace_entry *entry;
2442 struct trace_event *event;
2446 test_cpu_buff_start(iter);
2448 event = ftrace_find_event(entry->type);
2450 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2451 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2452 if (!trace_print_lat_context(iter))
2455 if (!trace_print_context(iter))
2461 return event->funcs->trace(iter, sym_flags, event);
2463 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2466 return TRACE_TYPE_HANDLED;
2468 return TRACE_TYPE_PARTIAL_LINE;
2471 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2473 struct trace_seq *s = &iter->seq;
2474 struct trace_entry *entry;
2475 struct trace_event *event;
2479 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2480 if (!trace_seq_printf(s, "%d %d %llu ",
2481 entry->pid, iter->cpu, iter->ts))
2485 event = ftrace_find_event(entry->type);
2487 return event->funcs->raw(iter, 0, event);
2489 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2492 return TRACE_TYPE_HANDLED;
2494 return TRACE_TYPE_PARTIAL_LINE;
2497 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2499 struct trace_seq *s = &iter->seq;
2500 unsigned char newline = '\n';
2501 struct trace_entry *entry;
2502 struct trace_event *event;
2506 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2507 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2508 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2509 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2512 event = ftrace_find_event(entry->type);
2514 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2515 if (ret != TRACE_TYPE_HANDLED)
2519 SEQ_PUT_FIELD_RET(s, newline);
2521 return TRACE_TYPE_HANDLED;
2524 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2526 struct trace_seq *s = &iter->seq;
2527 struct trace_entry *entry;
2528 struct trace_event *event;
2532 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2533 SEQ_PUT_FIELD_RET(s, entry->pid);
2534 SEQ_PUT_FIELD_RET(s, iter->cpu);
2535 SEQ_PUT_FIELD_RET(s, iter->ts);
2538 event = ftrace_find_event(entry->type);
2539 return event ? event->funcs->binary(iter, 0, event) :
2543 int trace_empty(struct trace_iterator *iter)
2545 struct ring_buffer_iter *buf_iter;
2548 /* If we are looking at one CPU buffer, only check that one */
2549 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2550 cpu = iter->cpu_file;
2551 buf_iter = trace_buffer_iter(iter, cpu);
2553 if (!ring_buffer_iter_empty(buf_iter))
2556 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2562 for_each_tracing_cpu(cpu) {
2563 buf_iter = trace_buffer_iter(iter, cpu);
2565 if (!ring_buffer_iter_empty(buf_iter))
2568 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2576 /* Called with trace_event_read_lock() held. */
2577 enum print_line_t print_trace_line(struct trace_iterator *iter)
2579 enum print_line_t ret;
2581 if (iter->lost_events &&
2582 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2583 iter->cpu, iter->lost_events))
2584 return TRACE_TYPE_PARTIAL_LINE;
2586 if (iter->trace && iter->trace->print_line) {
2587 ret = iter->trace->print_line(iter);
2588 if (ret != TRACE_TYPE_UNHANDLED)
2592 if (iter->ent->type == TRACE_BPUTS &&
2593 trace_flags & TRACE_ITER_PRINTK &&
2594 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2595 return trace_print_bputs_msg_only(iter);
2597 if (iter->ent->type == TRACE_BPRINT &&
2598 trace_flags & TRACE_ITER_PRINTK &&
2599 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2600 return trace_print_bprintk_msg_only(iter);
2602 if (iter->ent->type == TRACE_PRINT &&
2603 trace_flags & TRACE_ITER_PRINTK &&
2604 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2605 return trace_print_printk_msg_only(iter);
2607 if (trace_flags & TRACE_ITER_BIN)
2608 return print_bin_fmt(iter);
2610 if (trace_flags & TRACE_ITER_HEX)
2611 return print_hex_fmt(iter);
2613 if (trace_flags & TRACE_ITER_RAW)
2614 return print_raw_fmt(iter);
2616 return print_trace_fmt(iter);
2619 void trace_latency_header(struct seq_file *m)
2621 struct trace_iterator *iter = m->private;
2623 /* print nothing if the buffers are empty */
2624 if (trace_empty(iter))
2627 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2628 print_trace_header(m, iter);
2630 if (!(trace_flags & TRACE_ITER_VERBOSE))
2631 print_lat_help_header(m);
2634 void trace_default_header(struct seq_file *m)
2636 struct trace_iterator *iter = m->private;
2638 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2641 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2642 /* print nothing if the buffers are empty */
2643 if (trace_empty(iter))
2645 print_trace_header(m, iter);
2646 if (!(trace_flags & TRACE_ITER_VERBOSE))
2647 print_lat_help_header(m);
2649 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2650 if (trace_flags & TRACE_ITER_IRQ_INFO)
2651 print_func_help_header_irq(iter->trace_buffer, m);
2653 print_func_help_header(iter->trace_buffer, m);
2658 static void test_ftrace_alive(struct seq_file *m)
2660 if (!ftrace_is_dead())
2662 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2663 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2666 #ifdef CONFIG_TRACER_MAX_TRACE
2667 static void show_snapshot_main_help(struct seq_file *m)
2669 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2670 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2671 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2672 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2673 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2674 seq_printf(m, "# is not a '0' or '1')\n");
2677 static void show_snapshot_percpu_help(struct seq_file *m)
2679 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2680 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2681 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2682 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2684 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2685 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2687 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2688 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2689 seq_printf(m, "# is not a '0' or '1')\n");
2692 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2694 if (iter->tr->allocated_snapshot)
2695 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2697 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2699 seq_printf(m, "# Snapshot commands:\n");
2700 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2701 show_snapshot_main_help(m);
2703 show_snapshot_percpu_help(m);
2706 /* Should never be called */
2707 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2710 static int s_show(struct seq_file *m, void *v)
2712 struct trace_iterator *iter = v;
2715 if (iter->ent == NULL) {
2717 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2719 test_ftrace_alive(m);
2721 if (iter->snapshot && trace_empty(iter))
2722 print_snapshot_help(m, iter);
2723 else if (iter->trace && iter->trace->print_header)
2724 iter->trace->print_header(m);
2726 trace_default_header(m);
2728 } else if (iter->leftover) {
2730 * If we filled the seq_file buffer earlier, we
2731 * want to just show it now.
2733 ret = trace_print_seq(m, &iter->seq);
2735 /* ret should this time be zero, but you never know */
2736 iter->leftover = ret;
2739 print_trace_line(iter);
2740 ret = trace_print_seq(m, &iter->seq);
2742 * If we overflow the seq_file buffer, then it will
2743 * ask us for this data again at start up.
2745 * ret is 0 if seq_file write succeeded.
2748 iter->leftover = ret;
2754 static const struct seq_operations tracer_seq_ops = {
2761 static struct trace_iterator *
2762 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2764 struct trace_cpu *tc = inode->i_private;
2765 struct trace_array *tr = tc->tr;
2766 struct trace_iterator *iter;
2769 if (tracing_disabled)
2770 return ERR_PTR(-ENODEV);
2772 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2774 return ERR_PTR(-ENOMEM);
2776 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2778 if (!iter->buffer_iter)
2782 * We make a copy of the current tracer to avoid concurrent
2783 * changes on it while we are reading.
2785 mutex_lock(&trace_types_lock);
2786 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2790 *iter->trace = *tr->current_trace;
2792 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2797 #ifdef CONFIG_TRACER_MAX_TRACE
2798 /* Currently only the top directory has a snapshot */
2799 if (tr->current_trace->print_max || snapshot)
2800 iter->trace_buffer = &tr->max_buffer;
2803 iter->trace_buffer = &tr->trace_buffer;
2804 iter->snapshot = snapshot;
2806 mutex_init(&iter->mutex);
2807 iter->cpu_file = tc->cpu;
2809 /* Notify the tracer early; before we stop tracing. */
2810 if (iter->trace && iter->trace->open)
2811 iter->trace->open(iter);
2813 /* Annotate start of buffers if we had overruns */
2814 if (ring_buffer_overruns(iter->trace_buffer->buffer))
2815 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2817 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2818 if (trace_clocks[trace_clock_id].in_ns)
2819 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2821 /* stop the trace while dumping if we are not opening "snapshot" */
2822 if (!iter->snapshot)
2823 tracing_stop_tr(tr);
2825 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
2826 for_each_tracing_cpu(cpu) {
2827 iter->buffer_iter[cpu] =
2828 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2830 ring_buffer_read_prepare_sync();
2831 for_each_tracing_cpu(cpu) {
2832 ring_buffer_read_start(iter->buffer_iter[cpu]);
2833 tracing_iter_reset(iter, cpu);
2836 cpu = iter->cpu_file;
2837 iter->buffer_iter[cpu] =
2838 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2839 ring_buffer_read_prepare_sync();
2840 ring_buffer_read_start(iter->buffer_iter[cpu]);
2841 tracing_iter_reset(iter, cpu);
2846 mutex_unlock(&trace_types_lock);
2851 mutex_unlock(&trace_types_lock);
2853 kfree(iter->buffer_iter);
2855 seq_release_private(inode, file);
2856 return ERR_PTR(-ENOMEM);
2859 int tracing_open_generic(struct inode *inode, struct file *filp)
2861 if (tracing_disabled)
2864 filp->private_data = inode->i_private;
2868 static int tracing_release(struct inode *inode, struct file *file)
2870 struct seq_file *m = file->private_data;
2871 struct trace_iterator *iter;
2872 struct trace_array *tr;
2875 if (!(file->f_mode & FMODE_READ))
2881 mutex_lock(&trace_types_lock);
2886 for_each_tracing_cpu(cpu) {
2887 if (iter->buffer_iter[cpu])
2888 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2891 if (iter->trace && iter->trace->close)
2892 iter->trace->close(iter);
2894 if (!iter->snapshot)
2895 /* reenable tracing if it was previously enabled */
2896 tracing_start_tr(tr);
2897 mutex_unlock(&trace_types_lock);
2899 mutex_destroy(&iter->mutex);
2900 free_cpumask_var(iter->started);
2902 kfree(iter->buffer_iter);
2903 seq_release_private(inode, file);
2907 static int tracing_open(struct inode *inode, struct file *file)
2909 struct trace_iterator *iter;
2912 /* If this file was open for write, then erase contents */
2913 if ((file->f_mode & FMODE_WRITE) &&
2914 (file->f_flags & O_TRUNC)) {
2915 struct trace_cpu *tc = inode->i_private;
2916 struct trace_array *tr = tc->tr;
2918 if (tc->cpu == RING_BUFFER_ALL_CPUS)
2919 tracing_reset_online_cpus(&tr->trace_buffer);
2921 tracing_reset(&tr->trace_buffer, tc->cpu);
2924 if (file->f_mode & FMODE_READ) {
2925 iter = __tracing_open(inode, file, false);
2927 ret = PTR_ERR(iter);
2928 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2929 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2935 t_next(struct seq_file *m, void *v, loff_t *pos)
2937 struct tracer *t = v;
2947 static void *t_start(struct seq_file *m, loff_t *pos)
2952 mutex_lock(&trace_types_lock);
2953 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2959 static void t_stop(struct seq_file *m, void *p)
2961 mutex_unlock(&trace_types_lock);
2964 static int t_show(struct seq_file *m, void *v)
2966 struct tracer *t = v;
2971 seq_printf(m, "%s", t->name);
2980 static const struct seq_operations show_traces_seq_ops = {
2987 static int show_traces_open(struct inode *inode, struct file *file)
2989 if (tracing_disabled)
2992 return seq_open(file, &show_traces_seq_ops);
2996 tracing_write_stub(struct file *filp, const char __user *ubuf,
2997 size_t count, loff_t *ppos)
3002 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
3004 if (file->f_mode & FMODE_READ)
3005 return seq_lseek(file, offset, origin);
3010 static const struct file_operations tracing_fops = {
3011 .open = tracing_open,
3013 .write = tracing_write_stub,
3014 .llseek = tracing_seek,
3015 .release = tracing_release,
3018 static const struct file_operations show_traces_fops = {
3019 .open = show_traces_open,
3021 .release = seq_release,
3022 .llseek = seq_lseek,
3026 * Only trace on a CPU if the bitmask is set:
3028 static cpumask_var_t tracing_cpumask;
3031 * The tracer itself will not take this lock, but still we want
3032 * to provide a consistent cpumask to user-space:
3034 static DEFINE_MUTEX(tracing_cpumask_update_lock);
3037 * Temporary storage for the character representation of the
3038 * CPU bitmask (and one more byte for the newline):
3040 static char mask_str[NR_CPUS + 1];
3043 tracing_cpumask_read(struct file *filp, char __user *ubuf,
3044 size_t count, loff_t *ppos)
3048 mutex_lock(&tracing_cpumask_update_lock);
3050 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
3051 if (count - len < 2) {
3055 len += sprintf(mask_str + len, "\n");
3056 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3059 mutex_unlock(&tracing_cpumask_update_lock);
3065 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3066 size_t count, loff_t *ppos)
3068 struct trace_array *tr = filp->private_data;
3069 cpumask_var_t tracing_cpumask_new;
3072 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3075 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
3079 mutex_lock(&tracing_cpumask_update_lock);
3081 local_irq_disable();
3082 arch_spin_lock(&ftrace_max_lock);
3083 for_each_tracing_cpu(cpu) {
3085 * Increase/decrease the disabled counter if we are
3086 * about to flip a bit in the cpumask:
3088 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
3089 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3090 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3091 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
3093 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
3094 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3095 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3096 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
3099 arch_spin_unlock(&ftrace_max_lock);
3102 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
3104 mutex_unlock(&tracing_cpumask_update_lock);
3105 free_cpumask_var(tracing_cpumask_new);
3110 free_cpumask_var(tracing_cpumask_new);
3115 static const struct file_operations tracing_cpumask_fops = {
3116 .open = tracing_open_generic,
3117 .read = tracing_cpumask_read,
3118 .write = tracing_cpumask_write,
3119 .llseek = generic_file_llseek,
3122 static int tracing_trace_options_show(struct seq_file *m, void *v)
3124 struct tracer_opt *trace_opts;
3125 struct trace_array *tr = m->private;
3129 mutex_lock(&trace_types_lock);
3130 tracer_flags = tr->current_trace->flags->val;
3131 trace_opts = tr->current_trace->flags->opts;
3133 for (i = 0; trace_options[i]; i++) {
3134 if (trace_flags & (1 << i))
3135 seq_printf(m, "%s\n", trace_options[i]);
3137 seq_printf(m, "no%s\n", trace_options[i]);
3140 for (i = 0; trace_opts[i].name; i++) {
3141 if (tracer_flags & trace_opts[i].bit)
3142 seq_printf(m, "%s\n", trace_opts[i].name);
3144 seq_printf(m, "no%s\n", trace_opts[i].name);
3146 mutex_unlock(&trace_types_lock);
3151 static int __set_tracer_option(struct tracer *trace,
3152 struct tracer_flags *tracer_flags,
3153 struct tracer_opt *opts, int neg)
3157 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
3162 tracer_flags->val &= ~opts->bit;
3164 tracer_flags->val |= opts->bit;
3168 /* Try to assign a tracer specific option */
3169 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3171 struct tracer_flags *tracer_flags = trace->flags;
3172 struct tracer_opt *opts = NULL;
3175 for (i = 0; tracer_flags->opts[i].name; i++) {
3176 opts = &tracer_flags->opts[i];
3178 if (strcmp(cmp, opts->name) == 0)
3179 return __set_tracer_option(trace, trace->flags,
3186 /* Some tracers require overwrite to stay enabled */
3187 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3189 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3195 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
3197 /* do nothing if flag is already set */
3198 if (!!(trace_flags & mask) == !!enabled)
3201 /* Give the tracer a chance to approve the change */
3202 if (tr->current_trace->flag_changed)
3203 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
3207 trace_flags |= mask;
3209 trace_flags &= ~mask;
3211 if (mask == TRACE_ITER_RECORD_CMD)
3212 trace_event_enable_cmd_record(enabled);
3214 if (mask == TRACE_ITER_OVERWRITE) {
3215 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
3216 #ifdef CONFIG_TRACER_MAX_TRACE
3217 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
3221 if (mask == TRACE_ITER_PRINTK)
3222 trace_printk_start_stop_comm(enabled);
3227 static int trace_set_options(struct trace_array *tr, char *option)
3234 cmp = strstrip(option);
3236 if (strncmp(cmp, "no", 2) == 0) {
3241 mutex_lock(&trace_types_lock);
3243 for (i = 0; trace_options[i]; i++) {
3244 if (strcmp(cmp, trace_options[i]) == 0) {
3245 ret = set_tracer_flag(tr, 1 << i, !neg);
3250 /* If no option could be set, test the specific tracer options */
3251 if (!trace_options[i])
3252 ret = set_tracer_option(tr->current_trace, cmp, neg);
3254 mutex_unlock(&trace_types_lock);
3260 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3261 size_t cnt, loff_t *ppos)
3263 struct seq_file *m = filp->private_data;
3264 struct trace_array *tr = m->private;
3268 if (cnt >= sizeof(buf))
3271 if (copy_from_user(&buf, ubuf, cnt))
3276 ret = trace_set_options(tr, buf);
3285 static int tracing_trace_options_open(struct inode *inode, struct file *file)
3287 if (tracing_disabled)
3290 return single_open(file, tracing_trace_options_show, inode->i_private);
3293 static const struct file_operations tracing_iter_fops = {
3294 .open = tracing_trace_options_open,
3296 .llseek = seq_lseek,
3297 .release = single_release,
3298 .write = tracing_trace_options_write,
3301 static const char readme_msg[] =
3302 "tracing mini-HOWTO:\n\n"
3303 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
3304 "# cat /sys/kernel/debug/tracing/available_tracers\n"
3305 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
3306 "# cat /sys/kernel/debug/tracing/current_tracer\n"
3308 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
3309 "# cat /sys/kernel/debug/tracing/current_tracer\n"
3311 "# cat /sys/kernel/debug/tracing/trace_options\n"
3312 "noprint-parent nosym-offset nosym-addr noverbose\n"
3313 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
3314 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
3315 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
3316 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
3320 tracing_readme_read(struct file *filp, char __user *ubuf,
3321 size_t cnt, loff_t *ppos)
3323 return simple_read_from_buffer(ubuf, cnt, ppos,
3324 readme_msg, strlen(readme_msg));
3327 static const struct file_operations tracing_readme_fops = {
3328 .open = tracing_open_generic,
3329 .read = tracing_readme_read,
3330 .llseek = generic_file_llseek,
3334 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3335 size_t cnt, loff_t *ppos)
3344 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3348 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3356 for (i = 0; i < SAVED_CMDLINES; i++) {
3359 pid = map_cmdline_to_pid[i];
3360 if (pid == -1 || pid == NO_CMDLINE_MAP)
3363 trace_find_cmdline(pid, buf_comm);
3364 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3369 len = simple_read_from_buffer(ubuf, cnt, ppos,
3378 static const struct file_operations tracing_saved_cmdlines_fops = {
3379 .open = tracing_open_generic,
3380 .read = tracing_saved_cmdlines_read,
3381 .llseek = generic_file_llseek,
3385 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3386 size_t cnt, loff_t *ppos)
3388 struct trace_array *tr = filp->private_data;
3389 char buf[MAX_TRACER_SIZE+2];
3392 mutex_lock(&trace_types_lock);
3393 r = sprintf(buf, "%s\n", tr->current_trace->name);
3394 mutex_unlock(&trace_types_lock);
3396 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3399 int tracer_init(struct tracer *t, struct trace_array *tr)
3401 tracing_reset_online_cpus(&tr->trace_buffer);
3405 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
3409 for_each_tracing_cpu(cpu)
3410 per_cpu_ptr(buf->data, cpu)->entries = val;
3413 #ifdef CONFIG_TRACER_MAX_TRACE
3414 /* resize @tr's buffer to the size of @size_tr's entries */
3415 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3416 struct trace_buffer *size_buf, int cpu_id)
3420 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3421 for_each_tracing_cpu(cpu) {
3422 ret = ring_buffer_resize(trace_buf->buffer,
3423 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
3426 per_cpu_ptr(trace_buf->data, cpu)->entries =
3427 per_cpu_ptr(size_buf->data, cpu)->entries;
3430 ret = ring_buffer_resize(trace_buf->buffer,
3431 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
3433 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3434 per_cpu_ptr(size_buf->data, cpu_id)->entries;
3439 #endif /* CONFIG_TRACER_MAX_TRACE */
3441 static int __tracing_resize_ring_buffer(struct trace_array *tr,
3442 unsigned long size, int cpu)
3447 * If kernel or user changes the size of the ring buffer
3448 * we use the size that was given, and we can forget about
3449 * expanding it later.
3451 ring_buffer_expanded = true;
3453 /* May be called before buffers are initialized */
3454 if (!tr->trace_buffer.buffer)
3457 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
3461 #ifdef CONFIG_TRACER_MAX_TRACE
3462 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3463 !tr->current_trace->use_max_tr)
3466 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
3468 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3469 &tr->trace_buffer, cpu);
3472 * AARGH! We are left with different
3473 * size max buffer!!!!
3474 * The max buffer is our "snapshot" buffer.
3475 * When a tracer needs a snapshot (one of the
3476 * latency tracers), it swaps the max buffer
3477 * with the saved snap shot. We succeeded to
3478 * update the size of the main buffer, but failed to
3479 * update the size of the max buffer. But when we tried
3480 * to reset the main buffer to the original size, we
3481 * failed there too. This is very unlikely to
3482 * happen, but if it does, warn and kill all
3486 tracing_disabled = 1;
3491 if (cpu == RING_BUFFER_ALL_CPUS)
3492 set_buffer_entries(&tr->max_buffer, size);
3494 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
3497 #endif /* CONFIG_TRACER_MAX_TRACE */
3499 if (cpu == RING_BUFFER_ALL_CPUS)
3500 set_buffer_entries(&tr->trace_buffer, size);
3502 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
3507 static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3508 unsigned long size, int cpu_id)
3512 mutex_lock(&trace_types_lock);
3514 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3515 /* make sure, this cpu is enabled in the mask */
3516 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3522 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
3527 mutex_unlock(&trace_types_lock);
3534 * tracing_update_buffers - used by tracing facility to expand ring buffers
3536 * To save on memory when the tracing is never used on a system with it
3537 * configured in. The ring buffers are set to a minimum size. But once
3538 * a user starts to use the tracing facility, then they need to grow
3539 * to their default size.
3541 * This function is to be called when a tracer is about to be used.
3543 int tracing_update_buffers(void)
3547 mutex_lock(&trace_types_lock);
3548 if (!ring_buffer_expanded)
3549 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
3550 RING_BUFFER_ALL_CPUS);
3551 mutex_unlock(&trace_types_lock);
3556 struct trace_option_dentry;
3558 static struct trace_option_dentry *
3559 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
3562 destroy_trace_option_files(struct trace_option_dentry *topts);
3564 static int tracing_set_tracer(const char *buf)
3566 static struct trace_option_dentry *topts;
3567 struct trace_array *tr = &global_trace;
3569 #ifdef CONFIG_TRACER_MAX_TRACE
3574 mutex_lock(&trace_types_lock);
3576 if (!ring_buffer_expanded) {
3577 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
3578 RING_BUFFER_ALL_CPUS);
3584 for (t = trace_types; t; t = t->next) {
3585 if (strcmp(t->name, buf) == 0)
3592 if (t == tr->current_trace)
3595 trace_branch_disable();
3597 tr->current_trace->enabled = false;
3599 if (tr->current_trace->reset)
3600 tr->current_trace->reset(tr);
3602 /* Current trace needs to be nop_trace before synchronize_sched */
3603 tr->current_trace = &nop_trace;
3605 #ifdef CONFIG_TRACER_MAX_TRACE
3606 had_max_tr = tr->allocated_snapshot;
3608 if (had_max_tr && !t->use_max_tr) {
3610 * We need to make sure that the update_max_tr sees that
3611 * current_trace changed to nop_trace to keep it from
3612 * swapping the buffers after we resize it.
3613 * The update_max_tr is called from interrupts disabled
3614 * so a synchronized_sched() is sufficient.
3616 synchronize_sched();
3620 destroy_trace_option_files(topts);
3622 topts = create_trace_option_files(tr, t);
3624 #ifdef CONFIG_TRACER_MAX_TRACE
3625 if (t->use_max_tr && !had_max_tr) {
3626 ret = alloc_snapshot(tr);
3633 ret = tracer_init(t, tr);
3638 tr->current_trace = t;
3639 tr->current_trace->enabled = true;
3640 trace_branch_enable(tr);
3642 mutex_unlock(&trace_types_lock);
3648 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3649 size_t cnt, loff_t *ppos)
3651 char buf[MAX_TRACER_SIZE+1];
3658 if (cnt > MAX_TRACER_SIZE)
3659 cnt = MAX_TRACER_SIZE;
3661 if (copy_from_user(&buf, ubuf, cnt))
3666 /* strip ending whitespace. */
3667 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3670 err = tracing_set_tracer(buf);
3680 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3681 size_t cnt, loff_t *ppos)
3683 unsigned long *ptr = filp->private_data;
3687 r = snprintf(buf, sizeof(buf), "%ld\n",
3688 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3689 if (r > sizeof(buf))
3691 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3695 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3696 size_t cnt, loff_t *ppos)
3698 unsigned long *ptr = filp->private_data;
3702 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3711 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3713 struct trace_cpu *tc = inode->i_private;
3714 struct trace_array *tr = tc->tr;
3715 struct trace_iterator *iter;
3718 if (tracing_disabled)
3721 mutex_lock(&trace_types_lock);
3723 /* create a buffer to store the information to pass to userspace */
3724 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3731 * We make a copy of the current tracer to avoid concurrent
3732 * changes on it while we are reading.
3734 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3739 *iter->trace = *tr->current_trace;
3741 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3746 /* trace pipe does not show start of buffer */
3747 cpumask_setall(iter->started);
3749 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3750 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3752 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3753 if (trace_clocks[trace_clock_id].in_ns)
3754 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3756 iter->cpu_file = tc->cpu;
3758 iter->trace_buffer = &tc->tr->trace_buffer;
3759 mutex_init(&iter->mutex);
3760 filp->private_data = iter;
3762 if (iter->trace->pipe_open)
3763 iter->trace->pipe_open(iter);
3765 nonseekable_open(inode, filp);
3767 mutex_unlock(&trace_types_lock);
3773 mutex_unlock(&trace_types_lock);
3777 static int tracing_release_pipe(struct inode *inode, struct file *file)
3779 struct trace_iterator *iter = file->private_data;
3781 mutex_lock(&trace_types_lock);
3783 if (iter->trace->pipe_close)
3784 iter->trace->pipe_close(iter);
3786 mutex_unlock(&trace_types_lock);
3788 free_cpumask_var(iter->started);
3789 mutex_destroy(&iter->mutex);
3797 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
3799 /* Iterators are static, they should be filled or empty */
3800 if (trace_buffer_iter(iter, iter->cpu_file))
3801 return POLLIN | POLLRDNORM;
3803 if (trace_flags & TRACE_ITER_BLOCK)
3805 * Always select as readable when in blocking mode
3807 return POLLIN | POLLRDNORM;
3809 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
3814 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3816 struct trace_iterator *iter = filp->private_data;
3818 return trace_poll(iter, filp, poll_table);
3822 * This is a make-shift waitqueue.
3823 * A tracer might use this callback on some rare cases:
3825 * 1) the current tracer might hold the runqueue lock when it wakes up
3826 * a reader, hence a deadlock (sched, function, and function graph tracers)
3827 * 2) the function tracers, trace all functions, we don't want
3828 * the overhead of calling wake_up and friends
3829 * (and tracing them too)
3831 * Anyway, this is really very primitive wakeup.
3833 void poll_wait_pipe(struct trace_iterator *iter)
3835 set_current_state(TASK_INTERRUPTIBLE);
3836 /* sleep for 100 msecs, and try again. */
3837 schedule_timeout(HZ / 10);
3840 /* Must be called with trace_types_lock mutex held. */
3841 static int tracing_wait_pipe(struct file *filp)
3843 struct trace_iterator *iter = filp->private_data;
3845 while (trace_empty(iter)) {
3847 if ((filp->f_flags & O_NONBLOCK)) {
3851 mutex_unlock(&iter->mutex);
3853 iter->trace->wait_pipe(iter);
3855 mutex_lock(&iter->mutex);
3857 if (signal_pending(current))
3861 * We block until we read something and tracing is disabled.
3862 * We still block if tracing is disabled, but we have never
3863 * read anything. This allows a user to cat this file, and
3864 * then enable tracing. But after we have read something,
3865 * we give an EOF when tracing is again disabled.
3867 * iter->pos will be 0 if we haven't read anything.
3869 if (!tracing_is_enabled() && iter->pos)
3880 tracing_read_pipe(struct file *filp, char __user *ubuf,
3881 size_t cnt, loff_t *ppos)
3883 struct trace_iterator *iter = filp->private_data;
3884 struct trace_array *tr = iter->tr;
3887 /* return any leftover data */
3888 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3892 trace_seq_init(&iter->seq);
3894 /* copy the tracer to avoid using a global lock all around */
3895 mutex_lock(&trace_types_lock);
3896 if (unlikely(iter->trace->name != tr->current_trace->name))
3897 *iter->trace = *tr->current_trace;
3898 mutex_unlock(&trace_types_lock);
3901 * Avoid more than one consumer on a single file descriptor
3902 * This is just a matter of traces coherency, the ring buffer itself
3905 mutex_lock(&iter->mutex);
3906 if (iter->trace->read) {
3907 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3913 sret = tracing_wait_pipe(filp);
3917 /* stop when tracing is finished */
3918 if (trace_empty(iter)) {
3923 if (cnt >= PAGE_SIZE)
3924 cnt = PAGE_SIZE - 1;
3926 /* reset all but tr, trace, and overruns */
3927 memset(&iter->seq, 0,
3928 sizeof(struct trace_iterator) -
3929 offsetof(struct trace_iterator, seq));
3932 trace_event_read_lock();
3933 trace_access_lock(iter->cpu_file);
3934 while (trace_find_next_entry_inc(iter) != NULL) {
3935 enum print_line_t ret;
3936 int len = iter->seq.len;
3938 ret = print_trace_line(iter);
3939 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3940 /* don't print partial lines */
3941 iter->seq.len = len;
3944 if (ret != TRACE_TYPE_NO_CONSUME)
3945 trace_consume(iter);
3947 if (iter->seq.len >= cnt)
3951 * Setting the full flag means we reached the trace_seq buffer
3952 * size and we should leave by partial output condition above.
3953 * One of the trace_seq_* functions is not used properly.
3955 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3958 trace_access_unlock(iter->cpu_file);
3959 trace_event_read_unlock();
3961 /* Now copy what we have to the user */
3962 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3963 if (iter->seq.readpos >= iter->seq.len)
3964 trace_seq_init(&iter->seq);
3967 * If there was nothing to send to user, in spite of consuming trace
3968 * entries, go back to wait for more entries.
3974 mutex_unlock(&iter->mutex);
3979 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3980 struct pipe_buffer *buf)
3982 __free_page(buf->page);
3985 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3988 __free_page(spd->pages[idx]);
3991 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3993 .map = generic_pipe_buf_map,
3994 .unmap = generic_pipe_buf_unmap,
3995 .confirm = generic_pipe_buf_confirm,
3996 .release = tracing_pipe_buf_release,
3997 .steal = generic_pipe_buf_steal,
3998 .get = generic_pipe_buf_get,
4002 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
4007 /* Seq buffer is page-sized, exactly what we need. */
4009 count = iter->seq.len;
4010 ret = print_trace_line(iter);
4011 count = iter->seq.len - count;
4014 iter->seq.len -= count;
4017 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4018 iter->seq.len -= count;
4022 if (ret != TRACE_TYPE_NO_CONSUME)
4023 trace_consume(iter);
4025 if (!trace_find_next_entry_inc(iter)) {
4035 static ssize_t tracing_splice_read_pipe(struct file *filp,
4037 struct pipe_inode_info *pipe,
4041 struct page *pages_def[PIPE_DEF_BUFFERS];
4042 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4043 struct trace_iterator *iter = filp->private_data;
4044 struct splice_pipe_desc spd = {
4046 .partial = partial_def,
4047 .nr_pages = 0, /* This gets updated below. */
4048 .nr_pages_max = PIPE_DEF_BUFFERS,
4050 .ops = &tracing_pipe_buf_ops,
4051 .spd_release = tracing_spd_release_pipe,
4053 struct trace_array *tr = iter->tr;
4058 if (splice_grow_spd(pipe, &spd))
4061 /* copy the tracer to avoid using a global lock all around */
4062 mutex_lock(&trace_types_lock);
4063 if (unlikely(iter->trace->name != tr->current_trace->name))
4064 *iter->trace = *tr->current_trace;
4065 mutex_unlock(&trace_types_lock);
4067 mutex_lock(&iter->mutex);
4069 if (iter->trace->splice_read) {
4070 ret = iter->trace->splice_read(iter, filp,
4071 ppos, pipe, len, flags);
4076 ret = tracing_wait_pipe(filp);
4080 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
4085 trace_event_read_lock();
4086 trace_access_lock(iter->cpu_file);
4088 /* Fill as many pages as possible. */
4089 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
4090 spd.pages[i] = alloc_page(GFP_KERNEL);
4094 rem = tracing_fill_pipe_page(rem, iter);
4096 /* Copy the data into the page, so we can start over. */
4097 ret = trace_seq_to_buffer(&iter->seq,
4098 page_address(spd.pages[i]),
4101 __free_page(spd.pages[i]);
4104 spd.partial[i].offset = 0;
4105 spd.partial[i].len = iter->seq.len;
4107 trace_seq_init(&iter->seq);
4110 trace_access_unlock(iter->cpu_file);
4111 trace_event_read_unlock();
4112 mutex_unlock(&iter->mutex);
4116 ret = splice_to_pipe(pipe, &spd);
4118 splice_shrink_spd(&spd);
4122 mutex_unlock(&iter->mutex);
4127 tracing_entries_read(struct file *filp, char __user *ubuf,
4128 size_t cnt, loff_t *ppos)
4130 struct trace_cpu *tc = filp->private_data;
4131 struct trace_array *tr = tc->tr;
4136 mutex_lock(&trace_types_lock);
4138 if (tc->cpu == RING_BUFFER_ALL_CPUS) {
4139 int cpu, buf_size_same;
4144 /* check if all cpu sizes are same */
4145 for_each_tracing_cpu(cpu) {
4146 /* fill in the size from first enabled cpu */
4148 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4149 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
4155 if (buf_size_same) {
4156 if (!ring_buffer_expanded)
4157 r = sprintf(buf, "%lu (expanded: %lu)\n",
4159 trace_buf_size >> 10);
4161 r = sprintf(buf, "%lu\n", size >> 10);
4163 r = sprintf(buf, "X\n");
4165 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, tc->cpu)->entries >> 10);
4167 mutex_unlock(&trace_types_lock);
4169 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4174 tracing_entries_write(struct file *filp, const char __user *ubuf,
4175 size_t cnt, loff_t *ppos)
4177 struct trace_cpu *tc = filp->private_data;
4181 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4185 /* must have at least 1 entry */
4189 /* value is in KB */
4192 ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
4202 tracing_total_entries_read(struct file *filp, char __user *ubuf,
4203 size_t cnt, loff_t *ppos)
4205 struct trace_array *tr = filp->private_data;
4208 unsigned long size = 0, expanded_size = 0;
4210 mutex_lock(&trace_types_lock);
4211 for_each_tracing_cpu(cpu) {
4212 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
4213 if (!ring_buffer_expanded)
4214 expanded_size += trace_buf_size >> 10;
4216 if (ring_buffer_expanded)
4217 r = sprintf(buf, "%lu\n", size);
4219 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4220 mutex_unlock(&trace_types_lock);
4222 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4226 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4227 size_t cnt, loff_t *ppos)
4230 * There is no need to read what the user has written, this function
4231 * is just to make sure that there is no error when "echo" is used
4240 tracing_free_buffer_release(struct inode *inode, struct file *filp)
4242 struct trace_array *tr = inode->i_private;
4244 /* disable tracing ? */
4245 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4247 /* resize the ring buffer to 0 */
4248 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4254 tracing_mark_write(struct file *filp, const char __user *ubuf,
4255 size_t cnt, loff_t *fpos)
4257 unsigned long addr = (unsigned long)ubuf;
4258 struct ring_buffer_event *event;
4259 struct ring_buffer *buffer;
4260 struct print_entry *entry;
4261 unsigned long irq_flags;
4262 struct page *pages[2];
4272 if (tracing_disabled)
4275 if (!(trace_flags & TRACE_ITER_MARKERS))
4278 if (cnt > TRACE_BUF_SIZE)
4279 cnt = TRACE_BUF_SIZE;
4282 * Userspace is injecting traces into the kernel trace buffer.
4283 * We want to be as non intrusive as possible.
4284 * To do so, we do not want to allocate any special buffers
4285 * or take any locks, but instead write the userspace data
4286 * straight into the ring buffer.
4288 * First we need to pin the userspace buffer into memory,
4289 * which, most likely it is, because it just referenced it.
4290 * But there's no guarantee that it is. By using get_user_pages_fast()
4291 * and kmap_atomic/kunmap_atomic() we can get access to the
4292 * pages directly. We then write the data directly into the
4295 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4297 /* check if we cross pages */
4298 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4301 offset = addr & (PAGE_SIZE - 1);
4304 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4305 if (ret < nr_pages) {
4307 put_page(pages[ret]);
4312 for (i = 0; i < nr_pages; i++)
4313 map_page[i] = kmap_atomic(pages[i]);
4315 local_save_flags(irq_flags);
4316 size = sizeof(*entry) + cnt + 2; /* possible \n added */
4317 buffer = global_trace.trace_buffer.buffer;
4318 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4319 irq_flags, preempt_count());
4321 /* Ring buffer disabled, return as if not open for write */
4326 entry = ring_buffer_event_data(event);
4327 entry->ip = _THIS_IP_;
4329 if (nr_pages == 2) {
4330 len = PAGE_SIZE - offset;
4331 memcpy(&entry->buf, map_page[0] + offset, len);
4332 memcpy(&entry->buf[len], map_page[1], cnt - len);
4334 memcpy(&entry->buf, map_page[0] + offset, cnt);
4336 if (entry->buf[cnt - 1] != '\n') {
4337 entry->buf[cnt] = '\n';
4338 entry->buf[cnt + 1] = '\0';
4340 entry->buf[cnt] = '\0';
4342 __buffer_unlock_commit(buffer, event);
4349 for (i = 0; i < nr_pages; i++){
4350 kunmap_atomic(map_page[i]);
4357 static int tracing_clock_show(struct seq_file *m, void *v)
4359 struct trace_array *tr = m->private;
4362 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4364 "%s%s%s%s", i ? " " : "",
4365 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4366 i == tr->clock_id ? "]" : "");
4372 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4373 size_t cnt, loff_t *fpos)
4375 struct seq_file *m = filp->private_data;
4376 struct trace_array *tr = m->private;
4378 const char *clockstr;
4381 if (cnt >= sizeof(buf))
4384 if (copy_from_user(&buf, ubuf, cnt))
4389 clockstr = strstrip(buf);
4391 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4392 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4395 if (i == ARRAY_SIZE(trace_clocks))
4398 mutex_lock(&trace_types_lock);
4402 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
4405 * New clock may not be consistent with the previous clock.
4406 * Reset the buffer so that it doesn't have incomparable timestamps.
4408 tracing_reset_online_cpus(&global_trace.trace_buffer);
4410 #ifdef CONFIG_TRACER_MAX_TRACE
4411 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4412 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4413 tracing_reset_online_cpus(&global_trace.max_buffer);
4416 mutex_unlock(&trace_types_lock);
4423 static int tracing_clock_open(struct inode *inode, struct file *file)
4425 if (tracing_disabled)
4428 return single_open(file, tracing_clock_show, inode->i_private);
4431 struct ftrace_buffer_info {
4432 struct trace_iterator iter;
4437 #ifdef CONFIG_TRACER_SNAPSHOT
4438 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4440 struct trace_cpu *tc = inode->i_private;
4441 struct trace_iterator *iter;
4445 if (file->f_mode & FMODE_READ) {
4446 iter = __tracing_open(inode, file, true);
4448 ret = PTR_ERR(iter);
4450 /* Writes still need the seq_file to hold the private data */
4451 m = kzalloc(sizeof(*m), GFP_KERNEL);
4454 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4460 iter->trace_buffer = &tc->tr->max_buffer;
4461 iter->cpu_file = tc->cpu;
4463 file->private_data = m;
4470 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4473 struct seq_file *m = filp->private_data;
4474 struct trace_iterator *iter = m->private;
4475 struct trace_array *tr = iter->tr;
4479 ret = tracing_update_buffers();
4483 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4487 mutex_lock(&trace_types_lock);
4489 if (tr->current_trace->use_max_tr) {
4496 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4500 if (tr->allocated_snapshot)
4504 /* Only allow per-cpu swap if the ring buffer supports it */
4505 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4506 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4511 if (!tr->allocated_snapshot) {
4512 ret = alloc_snapshot(tr);
4516 local_irq_disable();
4517 /* Now, we're going to swap */
4518 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4519 update_max_tr(tr, current, smp_processor_id());
4521 update_max_tr_single(tr, current, iter->cpu_file);
4525 if (tr->allocated_snapshot) {
4526 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4527 tracing_reset_online_cpus(&tr->max_buffer);
4529 tracing_reset(&tr->max_buffer, iter->cpu_file);
4539 mutex_unlock(&trace_types_lock);
4543 static int tracing_snapshot_release(struct inode *inode, struct file *file)
4545 struct seq_file *m = file->private_data;
4547 if (file->f_mode & FMODE_READ)
4548 return tracing_release(inode, file);
4550 /* If write only, the seq_file is just a stub */
4558 static int tracing_buffers_open(struct inode *inode, struct file *filp);
4559 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
4560 size_t count, loff_t *ppos);
4561 static int tracing_buffers_release(struct inode *inode, struct file *file);
4562 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4563 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
4565 static int snapshot_raw_open(struct inode *inode, struct file *filp)
4567 struct ftrace_buffer_info *info;
4570 ret = tracing_buffers_open(inode, filp);
4574 info = filp->private_data;
4576 if (info->iter.trace->use_max_tr) {
4577 tracing_buffers_release(inode, filp);
4581 info->iter.snapshot = true;
4582 info->iter.trace_buffer = &info->iter.tr->max_buffer;
4587 #endif /* CONFIG_TRACER_SNAPSHOT */
4590 static const struct file_operations tracing_max_lat_fops = {
4591 .open = tracing_open_generic,
4592 .read = tracing_max_lat_read,
4593 .write = tracing_max_lat_write,
4594 .llseek = generic_file_llseek,
4597 static const struct file_operations set_tracer_fops = {
4598 .open = tracing_open_generic,
4599 .read = tracing_set_trace_read,
4600 .write = tracing_set_trace_write,
4601 .llseek = generic_file_llseek,
4604 static const struct file_operations tracing_pipe_fops = {
4605 .open = tracing_open_pipe,
4606 .poll = tracing_poll_pipe,
4607 .read = tracing_read_pipe,
4608 .splice_read = tracing_splice_read_pipe,
4609 .release = tracing_release_pipe,
4610 .llseek = no_llseek,
4613 static const struct file_operations tracing_entries_fops = {
4614 .open = tracing_open_generic,
4615 .read = tracing_entries_read,
4616 .write = tracing_entries_write,
4617 .llseek = generic_file_llseek,
4620 static const struct file_operations tracing_total_entries_fops = {
4621 .open = tracing_open_generic,
4622 .read = tracing_total_entries_read,
4623 .llseek = generic_file_llseek,
4626 static const struct file_operations tracing_free_buffer_fops = {
4627 .write = tracing_free_buffer_write,
4628 .release = tracing_free_buffer_release,
4631 static const struct file_operations tracing_mark_fops = {
4632 .open = tracing_open_generic,
4633 .write = tracing_mark_write,
4634 .llseek = generic_file_llseek,
4637 static const struct file_operations trace_clock_fops = {
4638 .open = tracing_clock_open,
4640 .llseek = seq_lseek,
4641 .release = single_release,
4642 .write = tracing_clock_write,
4645 #ifdef CONFIG_TRACER_SNAPSHOT
4646 static const struct file_operations snapshot_fops = {
4647 .open = tracing_snapshot_open,
4649 .write = tracing_snapshot_write,
4650 .llseek = tracing_seek,
4651 .release = tracing_snapshot_release,
4654 static const struct file_operations snapshot_raw_fops = {
4655 .open = snapshot_raw_open,
4656 .read = tracing_buffers_read,
4657 .release = tracing_buffers_release,
4658 .splice_read = tracing_buffers_splice_read,
4659 .llseek = no_llseek,
4662 #endif /* CONFIG_TRACER_SNAPSHOT */
4664 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4666 struct trace_cpu *tc = inode->i_private;
4667 struct trace_array *tr = tc->tr;
4668 struct ftrace_buffer_info *info;
4670 if (tracing_disabled)
4673 info = kzalloc(sizeof(*info), GFP_KERNEL);
4677 mutex_lock(&trace_types_lock);
4682 info->iter.cpu_file = tc->cpu;
4683 info->iter.trace = tr->current_trace;
4684 info->iter.trace_buffer = &tr->trace_buffer;
4686 /* Force reading ring buffer for first read */
4687 info->read = (unsigned int)-1;
4689 filp->private_data = info;
4691 mutex_unlock(&trace_types_lock);
4693 return nonseekable_open(inode, filp);
4697 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
4699 struct ftrace_buffer_info *info = filp->private_data;
4700 struct trace_iterator *iter = &info->iter;
4702 return trace_poll(iter, filp, poll_table);
4706 tracing_buffers_read(struct file *filp, char __user *ubuf,
4707 size_t count, loff_t *ppos)
4709 struct ftrace_buffer_info *info = filp->private_data;
4710 struct trace_iterator *iter = &info->iter;
4717 mutex_lock(&trace_types_lock);
4719 #ifdef CONFIG_TRACER_MAX_TRACE
4720 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4727 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
4733 /* Do we have previous read data to read? */
4734 if (info->read < PAGE_SIZE)
4738 trace_access_lock(iter->cpu_file);
4739 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
4743 trace_access_unlock(iter->cpu_file);
4746 if (trace_empty(iter)) {
4747 if ((filp->f_flags & O_NONBLOCK)) {
4751 mutex_unlock(&trace_types_lock);
4752 iter->trace->wait_pipe(iter);
4753 mutex_lock(&trace_types_lock);
4754 if (signal_pending(current)) {
4766 size = PAGE_SIZE - info->read;
4770 ret = copy_to_user(ubuf, info->spare + info->read, size);
4781 mutex_unlock(&trace_types_lock);
4786 static int tracing_buffers_release(struct inode *inode, struct file *file)
4788 struct ftrace_buffer_info *info = file->private_data;
4789 struct trace_iterator *iter = &info->iter;
4791 mutex_lock(&trace_types_lock);
4793 WARN_ON(!iter->tr->ref);
4797 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
4800 mutex_unlock(&trace_types_lock);
4806 struct ring_buffer *buffer;
4811 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4812 struct pipe_buffer *buf)
4814 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4819 ring_buffer_free_read_page(ref->buffer, ref->page);
4824 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4825 struct pipe_buffer *buf)
4827 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4832 /* Pipe buffer operations for a buffer. */
4833 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4835 .map = generic_pipe_buf_map,
4836 .unmap = generic_pipe_buf_unmap,
4837 .confirm = generic_pipe_buf_confirm,
4838 .release = buffer_pipe_buf_release,
4839 .steal = generic_pipe_buf_steal,
4840 .get = buffer_pipe_buf_get,
4844 * Callback from splice_to_pipe(), if we need to release some pages
4845 * at the end of the spd in case we error'ed out in filling the pipe.
4847 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4849 struct buffer_ref *ref =
4850 (struct buffer_ref *)spd->partial[i].private;
4855 ring_buffer_free_read_page(ref->buffer, ref->page);
4857 spd->partial[i].private = 0;
4861 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4862 struct pipe_inode_info *pipe, size_t len,
4865 struct ftrace_buffer_info *info = file->private_data;
4866 struct trace_iterator *iter = &info->iter;
4867 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4868 struct page *pages_def[PIPE_DEF_BUFFERS];
4869 struct splice_pipe_desc spd = {
4871 .partial = partial_def,
4872 .nr_pages_max = PIPE_DEF_BUFFERS,
4874 .ops = &buffer_pipe_buf_ops,
4875 .spd_release = buffer_spd_release,
4877 struct buffer_ref *ref;
4878 int entries, size, i;
4881 mutex_lock(&trace_types_lock);
4883 #ifdef CONFIG_TRACER_MAX_TRACE
4884 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4890 if (splice_grow_spd(pipe, &spd)) {
4895 if (*ppos & (PAGE_SIZE - 1)) {
4900 if (len & (PAGE_SIZE - 1)) {
4901 if (len < PAGE_SIZE) {
4909 trace_access_lock(iter->cpu_file);
4910 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
4912 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4916 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4921 ref->buffer = iter->trace_buffer->buffer;
4922 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
4928 r = ring_buffer_read_page(ref->buffer, &ref->page,
4929 len, iter->cpu_file, 1);
4931 ring_buffer_free_read_page(ref->buffer, ref->page);
4937 * zero out any left over data, this is going to
4940 size = ring_buffer_page_len(ref->page);
4941 if (size < PAGE_SIZE)
4942 memset(ref->page + size, 0, PAGE_SIZE - size);
4944 page = virt_to_page(ref->page);
4946 spd.pages[i] = page;
4947 spd.partial[i].len = PAGE_SIZE;
4948 spd.partial[i].offset = 0;
4949 spd.partial[i].private = (unsigned long)ref;
4953 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
4956 trace_access_unlock(iter->cpu_file);
4959 /* did we read anything? */
4960 if (!spd.nr_pages) {
4961 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
4965 mutex_unlock(&trace_types_lock);
4966 iter->trace->wait_pipe(iter);
4967 mutex_lock(&trace_types_lock);
4968 if (signal_pending(current)) {
4975 ret = splice_to_pipe(pipe, &spd);
4976 splice_shrink_spd(&spd);
4978 mutex_unlock(&trace_types_lock);
4983 static const struct file_operations tracing_buffers_fops = {
4984 .open = tracing_buffers_open,
4985 .read = tracing_buffers_read,
4986 .poll = tracing_buffers_poll,
4987 .release = tracing_buffers_release,
4988 .splice_read = tracing_buffers_splice_read,
4989 .llseek = no_llseek,
4993 tracing_stats_read(struct file *filp, char __user *ubuf,
4994 size_t count, loff_t *ppos)
4996 struct trace_cpu *tc = filp->private_data;
4997 struct trace_array *tr = tc->tr;
4998 struct trace_buffer *trace_buf = &tr->trace_buffer;
4999 struct trace_seq *s;
5001 unsigned long long t;
5002 unsigned long usec_rem;
5005 s = kmalloc(sizeof(*s), GFP_KERNEL);
5011 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
5012 trace_seq_printf(s, "entries: %ld\n", cnt);
5014 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
5015 trace_seq_printf(s, "overrun: %ld\n", cnt);
5017 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
5018 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5020 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
5021 trace_seq_printf(s, "bytes: %ld\n", cnt);
5023 if (trace_clocks[trace_clock_id].in_ns) {
5024 /* local or global for trace_clock */
5025 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5026 usec_rem = do_div(t, USEC_PER_SEC);
5027 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5030 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
5031 usec_rem = do_div(t, USEC_PER_SEC);
5032 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5034 /* counter or tsc mode for trace_clock */
5035 trace_seq_printf(s, "oldest event ts: %llu\n",
5036 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5038 trace_seq_printf(s, "now ts: %llu\n",
5039 ring_buffer_time_stamp(trace_buf->buffer, cpu));
5042 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
5043 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5045 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
5046 trace_seq_printf(s, "read events: %ld\n", cnt);
5048 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
5055 static const struct file_operations tracing_stats_fops = {
5056 .open = tracing_open_generic,
5057 .read = tracing_stats_read,
5058 .llseek = generic_file_llseek,
5061 #ifdef CONFIG_DYNAMIC_FTRACE
5063 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
5069 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
5070 size_t cnt, loff_t *ppos)
5072 static char ftrace_dyn_info_buffer[1024];
5073 static DEFINE_MUTEX(dyn_info_mutex);
5074 unsigned long *p = filp->private_data;
5075 char *buf = ftrace_dyn_info_buffer;
5076 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
5079 mutex_lock(&dyn_info_mutex);
5080 r = sprintf(buf, "%ld ", *p);
5082 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
5085 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5087 mutex_unlock(&dyn_info_mutex);
5092 static const struct file_operations tracing_dyn_info_fops = {
5093 .open = tracing_open_generic,
5094 .read = tracing_read_dyn_info,
5095 .llseek = generic_file_llseek,
5097 #endif /* CONFIG_DYNAMIC_FTRACE */
5099 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5101 ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5107 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5109 unsigned long *count = (long *)data;
5121 ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5122 struct ftrace_probe_ops *ops, void *data)
5124 long count = (long)data;
5126 seq_printf(m, "%ps:", (void *)ip);
5128 seq_printf(m, "snapshot");
5131 seq_printf(m, ":unlimited\n");
5133 seq_printf(m, ":count=%ld\n", count);
5138 static struct ftrace_probe_ops snapshot_probe_ops = {
5139 .func = ftrace_snapshot,
5140 .print = ftrace_snapshot_print,
5143 static struct ftrace_probe_ops snapshot_count_probe_ops = {
5144 .func = ftrace_count_snapshot,
5145 .print = ftrace_snapshot_print,
5149 ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5150 char *glob, char *cmd, char *param, int enable)
5152 struct ftrace_probe_ops *ops;
5153 void *count = (void *)-1;
5157 /* hash funcs only work with set_ftrace_filter */
5161 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
5163 if (glob[0] == '!') {
5164 unregister_ftrace_function_probe_func(glob+1, ops);
5171 number = strsep(¶m, ":");
5173 if (!strlen(number))
5177 * We use the callback data field (which is a pointer)
5180 ret = kstrtoul(number, 0, (unsigned long *)&count);
5185 ret = register_ftrace_function_probe(glob, ops, count);
5188 alloc_snapshot(&global_trace);
5190 return ret < 0 ? ret : 0;
5193 static struct ftrace_func_command ftrace_snapshot_cmd = {
5195 .func = ftrace_trace_snapshot_callback,
5198 static int register_snapshot_cmd(void)
5200 return register_ftrace_command(&ftrace_snapshot_cmd);
5203 static inline int register_snapshot_cmd(void) { return 0; }
5204 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
5206 struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
5211 if (!debugfs_initialized())
5214 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5215 tr->dir = debugfs_create_dir("tracing", NULL);
5218 pr_warn_once("Could not create debugfs directory 'tracing'\n");
5223 struct dentry *tracing_init_dentry(void)
5225 return tracing_init_dentry_tr(&global_trace);
5228 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
5230 struct dentry *d_tracer;
5233 return tr->percpu_dir;
5235 d_tracer = tracing_init_dentry_tr(tr);
5239 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
5241 WARN_ONCE(!tr->percpu_dir,
5242 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
5244 return tr->percpu_dir;
5248 tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
5250 struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
5251 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5252 struct dentry *d_cpu;
5253 char cpu_dir[30]; /* 30 characters should be more than enough */
5258 snprintf(cpu_dir, 30, "cpu%ld", cpu);
5259 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5261 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5265 /* per cpu trace_pipe */
5266 trace_create_file("trace_pipe", 0444, d_cpu,
5267 (void *)&data->trace_cpu, &tracing_pipe_fops);
5270 trace_create_file("trace", 0644, d_cpu,
5271 (void *)&data->trace_cpu, &tracing_fops);
5273 trace_create_file("trace_pipe_raw", 0444, d_cpu,
5274 (void *)&data->trace_cpu, &tracing_buffers_fops);
5276 trace_create_file("stats", 0444, d_cpu,
5277 (void *)&data->trace_cpu, &tracing_stats_fops);
5279 trace_create_file("buffer_size_kb", 0444, d_cpu,
5280 (void *)&data->trace_cpu, &tracing_entries_fops);
5282 #ifdef CONFIG_TRACER_SNAPSHOT
5283 trace_create_file("snapshot", 0644, d_cpu,
5284 (void *)&data->trace_cpu, &snapshot_fops);
5286 trace_create_file("snapshot_raw", 0444, d_cpu,
5287 (void *)&data->trace_cpu, &snapshot_raw_fops);
5291 #ifdef CONFIG_FTRACE_SELFTEST
5292 /* Let selftest have access to static functions in this file */
5293 #include "trace_selftest.c"
5296 struct trace_option_dentry {
5297 struct tracer_opt *opt;
5298 struct tracer_flags *flags;
5299 struct trace_array *tr;
5300 struct dentry *entry;
5304 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5307 struct trace_option_dentry *topt = filp->private_data;
5310 if (topt->flags->val & topt->opt->bit)
5315 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5319 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5322 struct trace_option_dentry *topt = filp->private_data;
5326 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5330 if (val != 0 && val != 1)
5333 if (!!(topt->flags->val & topt->opt->bit) != val) {
5334 mutex_lock(&trace_types_lock);
5335 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
5337 mutex_unlock(&trace_types_lock);
5348 static const struct file_operations trace_options_fops = {
5349 .open = tracing_open_generic,
5350 .read = trace_options_read,
5351 .write = trace_options_write,
5352 .llseek = generic_file_llseek,
5356 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5359 long index = (long)filp->private_data;
5362 if (trace_flags & (1 << index))
5367 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5371 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5374 struct trace_array *tr = &global_trace;
5375 long index = (long)filp->private_data;
5379 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5383 if (val != 0 && val != 1)
5386 mutex_lock(&trace_types_lock);
5387 ret = set_tracer_flag(tr, 1 << index, val);
5388 mutex_unlock(&trace_types_lock);
5398 static const struct file_operations trace_options_core_fops = {
5399 .open = tracing_open_generic,
5400 .read = trace_options_core_read,
5401 .write = trace_options_core_write,
5402 .llseek = generic_file_llseek,
5405 struct dentry *trace_create_file(const char *name,
5407 struct dentry *parent,
5409 const struct file_operations *fops)
5413 ret = debugfs_create_file(name, mode, parent, data, fops);
5415 pr_warning("Could not create debugfs '%s' entry\n", name);
5421 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
5423 struct dentry *d_tracer;
5428 d_tracer = tracing_init_dentry_tr(tr);
5432 tr->options = debugfs_create_dir("options", d_tracer);
5434 pr_warning("Could not create debugfs directory 'options'\n");
5442 create_trace_option_file(struct trace_array *tr,
5443 struct trace_option_dentry *topt,
5444 struct tracer_flags *flags,
5445 struct tracer_opt *opt)
5447 struct dentry *t_options;
5449 t_options = trace_options_init_dentry(tr);
5453 topt->flags = flags;
5457 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
5458 &trace_options_fops);
5462 static struct trace_option_dentry *
5463 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
5465 struct trace_option_dentry *topts;
5466 struct tracer_flags *flags;
5467 struct tracer_opt *opts;
5473 flags = tracer->flags;
5475 if (!flags || !flags->opts)
5480 for (cnt = 0; opts[cnt].name; cnt++)
5483 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
5487 for (cnt = 0; opts[cnt].name; cnt++)
5488 create_trace_option_file(tr, &topts[cnt], flags,
5495 destroy_trace_option_files(struct trace_option_dentry *topts)
5502 for (cnt = 0; topts[cnt].opt; cnt++) {
5503 if (topts[cnt].entry)
5504 debugfs_remove(topts[cnt].entry);
5510 static struct dentry *
5511 create_trace_option_core_file(struct trace_array *tr,
5512 const char *option, long index)
5514 struct dentry *t_options;
5516 t_options = trace_options_init_dentry(tr);
5520 return trace_create_file(option, 0644, t_options, (void *)index,
5521 &trace_options_core_fops);
5524 static __init void create_trace_options_dir(struct trace_array *tr)
5526 struct dentry *t_options;
5529 t_options = trace_options_init_dentry(tr);
5533 for (i = 0; trace_options[i]; i++)
5534 create_trace_option_core_file(tr, trace_options[i], i);
5538 rb_simple_read(struct file *filp, char __user *ubuf,
5539 size_t cnt, loff_t *ppos)
5541 struct trace_array *tr = filp->private_data;
5542 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5547 r = ring_buffer_record_is_on(buffer);
5551 r = sprintf(buf, "%d\n", r);
5553 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5557 rb_simple_write(struct file *filp, const char __user *ubuf,
5558 size_t cnt, loff_t *ppos)
5560 struct trace_array *tr = filp->private_data;
5561 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5565 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5570 mutex_lock(&trace_types_lock);
5572 ring_buffer_record_on(buffer);
5573 if (tr->current_trace->start)
5574 tr->current_trace->start(tr);
5576 ring_buffer_record_off(buffer);
5577 if (tr->current_trace->stop)
5578 tr->current_trace->stop(tr);
5580 mutex_unlock(&trace_types_lock);
5588 static const struct file_operations rb_simple_fops = {
5589 .open = tracing_open_generic,
5590 .read = rb_simple_read,
5591 .write = rb_simple_write,
5592 .llseek = default_llseek,
5595 struct dentry *trace_instance_dir;
5598 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5600 static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
5604 for_each_tracing_cpu(cpu) {
5605 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
5606 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
5607 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
5612 allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
5614 enum ring_buffer_flags rb_flags;
5616 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5618 buf->buffer = ring_buffer_alloc(size, rb_flags);
5622 buf->data = alloc_percpu(struct trace_array_cpu);
5624 ring_buffer_free(buf->buffer);
5628 init_trace_buffers(tr, buf);
5630 /* Allocate the first page for all buffers */
5631 set_buffer_entries(&tr->trace_buffer,
5632 ring_buffer_size(tr->trace_buffer.buffer, 0));
5637 static int allocate_trace_buffers(struct trace_array *tr, int size)
5641 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
5645 #ifdef CONFIG_TRACER_MAX_TRACE
5646 ret = allocate_trace_buffer(tr, &tr->max_buffer,
5647 allocate_snapshot ? size : 1);
5649 ring_buffer_free(tr->trace_buffer.buffer);
5650 free_percpu(tr->trace_buffer.data);
5653 tr->allocated_snapshot = allocate_snapshot;
5656 * Only the top level trace array gets its snapshot allocated
5657 * from the kernel command line.
5659 allocate_snapshot = false;
5664 static int new_instance_create(const char *name)
5666 struct trace_array *tr;
5669 mutex_lock(&trace_types_lock);
5672 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5673 if (tr->name && strcmp(tr->name, name) == 0)
5678 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
5682 tr->name = kstrdup(name, GFP_KERNEL);
5686 raw_spin_lock_init(&tr->start_lock);
5688 tr->current_trace = &nop_trace;
5690 INIT_LIST_HEAD(&tr->systems);
5691 INIT_LIST_HEAD(&tr->events);
5693 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
5696 /* Holder for file callbacks */
5697 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5698 tr->trace_cpu.tr = tr;
5700 tr->dir = debugfs_create_dir(name, trace_instance_dir);
5704 ret = event_trace_add_tracer(tr->dir, tr);
5708 init_tracer_debugfs(tr, tr->dir);
5710 list_add(&tr->list, &ftrace_trace_arrays);
5712 mutex_unlock(&trace_types_lock);
5717 if (tr->trace_buffer.buffer)
5718 ring_buffer_free(tr->trace_buffer.buffer);
5723 mutex_unlock(&trace_types_lock);
5729 static int instance_delete(const char *name)
5731 struct trace_array *tr;
5735 mutex_lock(&trace_types_lock);
5738 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5739 if (tr->name && strcmp(tr->name, name) == 0) {
5751 list_del(&tr->list);
5753 event_trace_del_tracer(tr);
5754 debugfs_remove_recursive(tr->dir);
5755 free_percpu(tr->trace_buffer.data);
5756 ring_buffer_free(tr->trace_buffer.buffer);
5764 mutex_unlock(&trace_types_lock);
5769 static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
5771 struct dentry *parent;
5774 /* Paranoid: Make sure the parent is the "instances" directory */
5775 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5776 if (WARN_ON_ONCE(parent != trace_instance_dir))
5780 * The inode mutex is locked, but debugfs_create_dir() will also
5781 * take the mutex. As the instances directory can not be destroyed
5782 * or changed in any other way, it is safe to unlock it, and
5783 * let the dentry try. If two users try to make the same dir at
5784 * the same time, then the new_instance_create() will determine the
5787 mutex_unlock(&inode->i_mutex);
5789 ret = new_instance_create(dentry->d_iname);
5791 mutex_lock(&inode->i_mutex);
5796 static int instance_rmdir(struct inode *inode, struct dentry *dentry)
5798 struct dentry *parent;
5801 /* Paranoid: Make sure the parent is the "instances" directory */
5802 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5803 if (WARN_ON_ONCE(parent != trace_instance_dir))
5806 /* The caller did a dget() on dentry */
5807 mutex_unlock(&dentry->d_inode->i_mutex);
5810 * The inode mutex is locked, but debugfs_create_dir() will also
5811 * take the mutex. As the instances directory can not be destroyed
5812 * or changed in any other way, it is safe to unlock it, and
5813 * let the dentry try. If two users try to make the same dir at
5814 * the same time, then the instance_delete() will determine the
5817 mutex_unlock(&inode->i_mutex);
5819 ret = instance_delete(dentry->d_iname);
5821 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
5822 mutex_lock(&dentry->d_inode->i_mutex);
5827 static const struct inode_operations instance_dir_inode_operations = {
5828 .lookup = simple_lookup,
5829 .mkdir = instance_mkdir,
5830 .rmdir = instance_rmdir,
5833 static __init void create_trace_instances(struct dentry *d_tracer)
5835 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
5836 if (WARN_ON(!trace_instance_dir))
5839 /* Hijack the dir inode operations, to allow mkdir */
5840 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
5844 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
5848 trace_create_file("trace_options", 0644, d_tracer,
5849 tr, &tracing_iter_fops);
5851 trace_create_file("trace", 0644, d_tracer,
5852 (void *)&tr->trace_cpu, &tracing_fops);
5854 trace_create_file("trace_pipe", 0444, d_tracer,
5855 (void *)&tr->trace_cpu, &tracing_pipe_fops);
5857 trace_create_file("buffer_size_kb", 0644, d_tracer,
5858 (void *)&tr->trace_cpu, &tracing_entries_fops);
5860 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
5861 tr, &tracing_total_entries_fops);
5863 trace_create_file("free_buffer", 0644, d_tracer,
5864 tr, &tracing_free_buffer_fops);
5866 trace_create_file("trace_marker", 0220, d_tracer,
5867 tr, &tracing_mark_fops);
5869 trace_create_file("trace_clock", 0644, d_tracer, tr,
5872 trace_create_file("tracing_on", 0644, d_tracer,
5873 tr, &rb_simple_fops);
5875 #ifdef CONFIG_TRACER_SNAPSHOT
5876 trace_create_file("snapshot", 0644, d_tracer,
5877 (void *)&tr->trace_cpu, &snapshot_fops);
5880 for_each_tracing_cpu(cpu)
5881 tracing_init_debugfs_percpu(tr, cpu);
5885 static __init int tracer_init_debugfs(void)
5887 struct dentry *d_tracer;
5889 trace_access_lock_init();
5891 d_tracer = tracing_init_dentry();
5893 init_tracer_debugfs(&global_trace, d_tracer);
5895 trace_create_file("tracing_cpumask", 0644, d_tracer,
5896 &global_trace, &tracing_cpumask_fops);
5898 trace_create_file("available_tracers", 0444, d_tracer,
5899 &global_trace, &show_traces_fops);
5901 trace_create_file("current_tracer", 0644, d_tracer,
5902 &global_trace, &set_tracer_fops);
5904 #ifdef CONFIG_TRACER_MAX_TRACE
5905 trace_create_file("tracing_max_latency", 0644, d_tracer,
5906 &tracing_max_latency, &tracing_max_lat_fops);
5909 trace_create_file("tracing_thresh", 0644, d_tracer,
5910 &tracing_thresh, &tracing_max_lat_fops);
5912 trace_create_file("README", 0444, d_tracer,
5913 NULL, &tracing_readme_fops);
5915 trace_create_file("saved_cmdlines", 0444, d_tracer,
5916 NULL, &tracing_saved_cmdlines_fops);
5918 #ifdef CONFIG_DYNAMIC_FTRACE
5919 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
5920 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
5923 create_trace_instances(d_tracer);
5925 create_trace_options_dir(&global_trace);
5930 static int trace_panic_handler(struct notifier_block *this,
5931 unsigned long event, void *unused)
5933 if (ftrace_dump_on_oops)
5934 ftrace_dump(ftrace_dump_on_oops);
5938 static struct notifier_block trace_panic_notifier = {
5939 .notifier_call = trace_panic_handler,
5941 .priority = 150 /* priority: INT_MAX >= x >= 0 */
5944 static int trace_die_handler(struct notifier_block *self,
5950 if (ftrace_dump_on_oops)
5951 ftrace_dump(ftrace_dump_on_oops);
5959 static struct notifier_block trace_die_notifier = {
5960 .notifier_call = trace_die_handler,
5965 * printk is set to max of 1024, we really don't need it that big.
5966 * Nothing should be printing 1000 characters anyway.
5968 #define TRACE_MAX_PRINT 1000
5971 * Define here KERN_TRACE so that we have one place to modify
5972 * it if we decide to change what log level the ftrace dump
5975 #define KERN_TRACE KERN_EMERG
5978 trace_printk_seq(struct trace_seq *s)
5980 /* Probably should print a warning here. */
5981 if (s->len >= TRACE_MAX_PRINT)
5982 s->len = TRACE_MAX_PRINT;
5984 /* should be zero ended, but we are paranoid. */
5985 s->buffer[s->len] = 0;
5987 printk(KERN_TRACE "%s", s->buffer);
5992 void trace_init_global_iter(struct trace_iterator *iter)
5994 iter->tr = &global_trace;
5995 iter->trace = iter->tr->current_trace;
5996 iter->cpu_file = RING_BUFFER_ALL_CPUS;
5997 iter->trace_buffer = &global_trace.trace_buffer;
6001 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
6003 static arch_spinlock_t ftrace_dump_lock =
6004 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
6005 /* use static because iter can be a bit big for the stack */
6006 static struct trace_iterator iter;
6007 unsigned int old_userobj;
6008 static int dump_ran;
6009 unsigned long flags;
6013 local_irq_save(flags);
6014 arch_spin_lock(&ftrace_dump_lock);
6022 /* Did function tracer already get disabled? */
6023 if (ftrace_is_dead()) {
6024 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
6025 printk("# MAY BE MISSING FUNCTION EVENTS\n");
6028 if (disable_tracing)
6031 /* Simulate the iterator */
6032 trace_init_global_iter(&iter);
6034 for_each_tracing_cpu(cpu) {
6035 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
6038 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
6040 /* don't look at user memory in panic mode */
6041 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
6043 switch (oops_dump_mode) {
6045 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6048 iter.cpu_file = raw_smp_processor_id();
6053 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
6054 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6057 printk(KERN_TRACE "Dumping ftrace buffer:\n");
6060 * We need to stop all tracing on all CPUS to read the
6061 * the next buffer. This is a bit expensive, but is
6062 * not done often. We fill all what we can read,
6063 * and then release the locks again.
6066 while (!trace_empty(&iter)) {
6069 printk(KERN_TRACE "---------------------------------\n");
6073 /* reset all but tr, trace, and overruns */
6074 memset(&iter.seq, 0,
6075 sizeof(struct trace_iterator) -
6076 offsetof(struct trace_iterator, seq));
6077 iter.iter_flags |= TRACE_FILE_LAT_FMT;
6080 if (trace_find_next_entry_inc(&iter) != NULL) {
6083 ret = print_trace_line(&iter);
6084 if (ret != TRACE_TYPE_NO_CONSUME)
6085 trace_consume(&iter);
6087 touch_nmi_watchdog();
6089 trace_printk_seq(&iter.seq);
6093 printk(KERN_TRACE " (ftrace buffer empty)\n");
6095 printk(KERN_TRACE "---------------------------------\n");
6098 /* Re-enable tracing if requested */
6099 if (!disable_tracing) {
6100 trace_flags |= old_userobj;
6102 for_each_tracing_cpu(cpu) {
6103 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
6109 arch_spin_unlock(&ftrace_dump_lock);
6110 local_irq_restore(flags);
6113 /* By default: disable tracing after the dump */
6114 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
6116 __ftrace_dump(true, oops_dump_mode);
6118 EXPORT_SYMBOL_GPL(ftrace_dump);
6120 __init static int tracer_alloc_buffers(void)
6126 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
6129 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
6130 goto out_free_buffer_mask;
6132 /* Only allocate trace_printk buffers if a trace_printk exists */
6133 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
6134 /* Must be called before global_trace.buffer is allocated */
6135 trace_printk_init_buffers();
6137 /* To save memory, keep the ring buffer size to its minimum */
6138 if (ring_buffer_expanded)
6139 ring_buf_size = trace_buf_size;
6143 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
6144 cpumask_copy(tracing_cpumask, cpu_all_mask);
6146 raw_spin_lock_init(&global_trace.start_lock);
6148 /* TODO: make the number of buffers hot pluggable with CPUS */
6149 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
6150 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
6152 goto out_free_cpumask;
6155 if (global_trace.buffer_disabled)
6158 trace_init_cmdlines();
6160 register_tracer(&nop_trace);
6162 global_trace.current_trace = &nop_trace;
6164 /* All seems OK, enable tracing */
6165 tracing_disabled = 0;
6167 atomic_notifier_chain_register(&panic_notifier_list,
6168 &trace_panic_notifier);
6170 register_die_notifier(&trace_die_notifier);
6172 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
6174 /* Holder for file callbacks */
6175 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
6176 global_trace.trace_cpu.tr = &global_trace;
6178 INIT_LIST_HEAD(&global_trace.systems);
6179 INIT_LIST_HEAD(&global_trace.events);
6180 list_add(&global_trace.list, &ftrace_trace_arrays);
6182 while (trace_boot_options) {
6185 option = strsep(&trace_boot_options, ",");
6186 trace_set_options(&global_trace, option);
6189 register_snapshot_cmd();
6194 free_percpu(global_trace.trace_buffer.data);
6195 #ifdef CONFIG_TRACER_MAX_TRACE
6196 free_percpu(global_trace.max_buffer.data);
6198 free_cpumask_var(tracing_cpumask);
6199 out_free_buffer_mask:
6200 free_cpumask_var(tracing_buffer_mask);
6205 __init static int clear_boot_tracer(void)
6208 * The default tracer at boot buffer is an init section.
6209 * This function is called in lateinit. If we did not
6210 * find the boot tracer, then clear it out, to prevent
6211 * later registration from accessing the buffer that is
6212 * about to be freed.
6214 if (!default_bootup_tracer)
6217 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6218 default_bootup_tracer);
6219 default_bootup_tracer = NULL;
6224 early_initcall(tracer_alloc_buffers);
6225 fs_initcall(tracer_init_debugfs);
6226 late_initcall(clear_boot_tracer);