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 /* When set, tracing will stop when a WARN*() is hit */
119 int __disable_trace_on_warning;
121 static int tracing_set_tracer(const char *buf);
123 #define MAX_TRACER_SIZE 100
124 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
125 static char *default_bootup_tracer;
127 static bool allocate_snapshot;
129 static int __init set_cmdline_ftrace(char *str)
131 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
132 default_bootup_tracer = bootup_tracer_buf;
133 /* We are using ftrace early, expand it */
134 ring_buffer_expanded = true;
137 __setup("ftrace=", set_cmdline_ftrace);
139 static int __init set_ftrace_dump_on_oops(char *str)
141 if (*str++ != '=' || !*str) {
142 ftrace_dump_on_oops = DUMP_ALL;
146 if (!strcmp("orig_cpu", str)) {
147 ftrace_dump_on_oops = DUMP_ORIG;
153 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
155 static int __init stop_trace_on_warning(char *str)
157 __disable_trace_on_warning = 1;
160 __setup("traceoff_on_warning=", stop_trace_on_warning);
162 static int __init boot_alloc_snapshot(char *str)
164 allocate_snapshot = true;
165 /* We also need the main ring buffer expanded */
166 ring_buffer_expanded = true;
169 __setup("alloc_snapshot", boot_alloc_snapshot);
172 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
173 static char *trace_boot_options __initdata;
175 static int __init set_trace_boot_options(char *str)
177 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
178 trace_boot_options = trace_boot_options_buf;
181 __setup("trace_options=", set_trace_boot_options);
184 unsigned long long ns2usecs(cycle_t nsec)
192 * The global_trace is the descriptor that holds the tracing
193 * buffers for the live tracing. For each CPU, it contains
194 * a link list of pages that will store trace entries. The
195 * page descriptor of the pages in the memory is used to hold
196 * the link list by linking the lru item in the page descriptor
197 * to each of the pages in the buffer per CPU.
199 * For each active CPU there is a data field that holds the
200 * pages for the buffer for that CPU. Each CPU has the same number
201 * of pages allocated for its buffer.
203 static struct trace_array global_trace;
205 LIST_HEAD(ftrace_trace_arrays);
207 int filter_current_check_discard(struct ring_buffer *buffer,
208 struct ftrace_event_call *call, void *rec,
209 struct ring_buffer_event *event)
211 return filter_check_discard(call, rec, buffer, event);
213 EXPORT_SYMBOL_GPL(filter_current_check_discard);
215 cycle_t ftrace_now(int cpu)
219 /* Early boot up does not have a buffer yet */
220 if (!global_trace.trace_buffer.buffer)
221 return trace_clock_local();
223 ts = ring_buffer_time_stamp(global_trace.trace_buffer.buffer, cpu);
224 ring_buffer_normalize_time_stamp(global_trace.trace_buffer.buffer, cpu, &ts);
230 * tracing_is_enabled - Show if global_trace has been disabled
232 * Shows if the global trace has been enabled or not. It uses the
233 * mirror flag "buffer_disabled" to be used in fast paths such as for
234 * the irqsoff tracer. But it may be inaccurate due to races. If you
235 * need to know the accurate state, use tracing_is_on() which is a little
236 * slower, but accurate.
238 int tracing_is_enabled(void)
241 * For quick access (irqsoff uses this in fast path), just
242 * return the mirror variable of the state of the ring buffer.
243 * It's a little racy, but we don't really care.
246 return !global_trace.buffer_disabled;
250 * trace_buf_size is the size in bytes that is allocated
251 * for a buffer. Note, the number of bytes is always rounded
254 * This number is purposely set to a low number of 16384.
255 * If the dump on oops happens, it will be much appreciated
256 * to not have to wait for all that output. Anyway this can be
257 * boot time and run time configurable.
259 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
261 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
263 /* trace_types holds a link list of available tracers. */
264 static struct tracer *trace_types __read_mostly;
267 * trace_types_lock is used to protect the trace_types list.
269 DEFINE_MUTEX(trace_types_lock);
272 * serialize the access of the ring buffer
274 * ring buffer serializes readers, but it is low level protection.
275 * The validity of the events (which returns by ring_buffer_peek() ..etc)
276 * are not protected by ring buffer.
278 * The content of events may become garbage if we allow other process consumes
279 * these events concurrently:
280 * A) the page of the consumed events may become a normal page
281 * (not reader page) in ring buffer, and this page will be rewrited
282 * by events producer.
283 * B) The page of the consumed events may become a page for splice_read,
284 * and this page will be returned to system.
286 * These primitives allow multi process access to different cpu ring buffer
289 * These primitives don't distinguish read-only and read-consume access.
290 * Multi read-only access are also serialized.
294 static DECLARE_RWSEM(all_cpu_access_lock);
295 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
297 static inline void trace_access_lock(int cpu)
299 if (cpu == RING_BUFFER_ALL_CPUS) {
300 /* gain it for accessing the whole ring buffer. */
301 down_write(&all_cpu_access_lock);
303 /* gain it for accessing a cpu ring buffer. */
305 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
306 down_read(&all_cpu_access_lock);
308 /* Secondly block other access to this @cpu ring buffer. */
309 mutex_lock(&per_cpu(cpu_access_lock, cpu));
313 static inline void trace_access_unlock(int cpu)
315 if (cpu == RING_BUFFER_ALL_CPUS) {
316 up_write(&all_cpu_access_lock);
318 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
319 up_read(&all_cpu_access_lock);
323 static inline void trace_access_lock_init(void)
327 for_each_possible_cpu(cpu)
328 mutex_init(&per_cpu(cpu_access_lock, cpu));
333 static DEFINE_MUTEX(access_lock);
335 static inline void trace_access_lock(int cpu)
338 mutex_lock(&access_lock);
341 static inline void trace_access_unlock(int cpu)
344 mutex_unlock(&access_lock);
347 static inline void trace_access_lock_init(void)
353 /* trace_flags holds trace_options default values */
354 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
355 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
356 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
357 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
359 void tracer_tracing_on(struct trace_array *tr)
361 if (tr->trace_buffer.buffer)
362 ring_buffer_record_on(tr->trace_buffer.buffer);
364 * This flag is looked at when buffers haven't been allocated
365 * yet, or by some tracers (like irqsoff), that just want to
366 * know if the ring buffer has been disabled, but it can handle
367 * races of where it gets disabled but we still do a record.
368 * As the check is in the fast path of the tracers, it is more
369 * important to be fast than accurate.
371 tr->buffer_disabled = 0;
372 /* Make the flag seen by readers */
377 * tracing_on - enable tracing buffers
379 * This function enables tracing buffers that may have been
380 * disabled with tracing_off.
382 void tracing_on(void)
384 tracer_tracing_on(&global_trace);
386 EXPORT_SYMBOL_GPL(tracing_on);
389 * __trace_puts - write a constant string into the trace buffer.
390 * @ip: The address of the caller
391 * @str: The constant string to write
392 * @size: The size of the string.
394 int __trace_puts(unsigned long ip, const char *str, int size)
396 struct ring_buffer_event *event;
397 struct ring_buffer *buffer;
398 struct print_entry *entry;
399 unsigned long irq_flags;
402 alloc = sizeof(*entry) + size + 2; /* possible \n added */
404 local_save_flags(irq_flags);
405 buffer = global_trace.trace_buffer.buffer;
406 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
407 irq_flags, preempt_count());
411 entry = ring_buffer_event_data(event);
414 memcpy(&entry->buf, str, size);
416 /* Add a newline if necessary */
417 if (entry->buf[size - 1] != '\n') {
418 entry->buf[size] = '\n';
419 entry->buf[size + 1] = '\0';
421 entry->buf[size] = '\0';
423 __buffer_unlock_commit(buffer, event);
427 EXPORT_SYMBOL_GPL(__trace_puts);
430 * __trace_bputs - write the pointer to a constant string into trace buffer
431 * @ip: The address of the caller
432 * @str: The constant string to write to the buffer to
434 int __trace_bputs(unsigned long ip, const char *str)
436 struct ring_buffer_event *event;
437 struct ring_buffer *buffer;
438 struct bputs_entry *entry;
439 unsigned long irq_flags;
440 int size = sizeof(struct bputs_entry);
442 local_save_flags(irq_flags);
443 buffer = global_trace.trace_buffer.buffer;
444 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
445 irq_flags, preempt_count());
449 entry = ring_buffer_event_data(event);
453 __buffer_unlock_commit(buffer, event);
457 EXPORT_SYMBOL_GPL(__trace_bputs);
459 #ifdef CONFIG_TRACER_SNAPSHOT
461 * trace_snapshot - take a snapshot of the current buffer.
463 * This causes a swap between the snapshot buffer and the current live
464 * tracing buffer. You can use this to take snapshots of the live
465 * trace when some condition is triggered, but continue to trace.
467 * Note, make sure to allocate the snapshot with either
468 * a tracing_snapshot_alloc(), or by doing it manually
469 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
471 * If the snapshot buffer is not allocated, it will stop tracing.
472 * Basically making a permanent snapshot.
474 void tracing_snapshot(void)
476 struct trace_array *tr = &global_trace;
477 struct tracer *tracer = tr->current_trace;
481 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
482 internal_trace_puts("*** snapshot is being ignored ***\n");
486 if (!tr->allocated_snapshot) {
487 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
488 internal_trace_puts("*** stopping trace here! ***\n");
493 /* Note, snapshot can not be used when the tracer uses it */
494 if (tracer->use_max_tr) {
495 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
496 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
500 local_irq_save(flags);
501 update_max_tr(tr, current, smp_processor_id());
502 local_irq_restore(flags);
504 EXPORT_SYMBOL_GPL(tracing_snapshot);
506 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
507 struct trace_buffer *size_buf, int cpu_id);
508 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
510 static int alloc_snapshot(struct trace_array *tr)
514 if (!tr->allocated_snapshot) {
516 /* allocate spare buffer */
517 ret = resize_buffer_duplicate_size(&tr->max_buffer,
518 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
522 tr->allocated_snapshot = true;
528 void free_snapshot(struct trace_array *tr)
531 * We don't free the ring buffer. instead, resize it because
532 * The max_tr ring buffer has some state (e.g. ring->clock) and
533 * we want preserve it.
535 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
536 set_buffer_entries(&tr->max_buffer, 1);
537 tracing_reset_online_cpus(&tr->max_buffer);
538 tr->allocated_snapshot = false;
542 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
544 * This is similar to trace_snapshot(), but it will allocate the
545 * snapshot buffer if it isn't already allocated. Use this only
546 * where it is safe to sleep, as the allocation may sleep.
548 * This causes a swap between the snapshot buffer and the current live
549 * tracing buffer. You can use this to take snapshots of the live
550 * trace when some condition is triggered, but continue to trace.
552 void tracing_snapshot_alloc(void)
554 struct trace_array *tr = &global_trace;
557 ret = alloc_snapshot(tr);
558 if (WARN_ON(ret < 0))
563 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
565 void tracing_snapshot(void)
567 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
569 EXPORT_SYMBOL_GPL(tracing_snapshot);
570 void tracing_snapshot_alloc(void)
575 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
576 #endif /* CONFIG_TRACER_SNAPSHOT */
578 void tracer_tracing_off(struct trace_array *tr)
580 if (tr->trace_buffer.buffer)
581 ring_buffer_record_off(tr->trace_buffer.buffer);
583 * This flag is looked at when buffers haven't been allocated
584 * yet, or by some tracers (like irqsoff), that just want to
585 * know if the ring buffer has been disabled, but it can handle
586 * races of where it gets disabled but we still do a record.
587 * As the check is in the fast path of the tracers, it is more
588 * important to be fast than accurate.
590 tr->buffer_disabled = 1;
591 /* Make the flag seen by readers */
596 * tracing_off - turn off tracing buffers
598 * This function stops the tracing buffers from recording data.
599 * It does not disable any overhead the tracers themselves may
600 * be causing. This function simply causes all recording to
601 * the ring buffers to fail.
603 void tracing_off(void)
605 tracer_tracing_off(&global_trace);
607 EXPORT_SYMBOL_GPL(tracing_off);
609 void disable_trace_on_warning(void)
611 if (__disable_trace_on_warning)
616 * tracer_tracing_is_on - show real state of ring buffer enabled
617 * @tr : the trace array to know if ring buffer is enabled
619 * Shows real state of the ring buffer if it is enabled or not.
621 int tracer_tracing_is_on(struct trace_array *tr)
623 if (tr->trace_buffer.buffer)
624 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
625 return !tr->buffer_disabled;
629 * tracing_is_on - show state of ring buffers enabled
631 int tracing_is_on(void)
633 return tracer_tracing_is_on(&global_trace);
635 EXPORT_SYMBOL_GPL(tracing_is_on);
637 static int __init set_buf_size(char *str)
639 unsigned long buf_size;
643 buf_size = memparse(str, &str);
644 /* nr_entries can not be zero */
647 trace_buf_size = buf_size;
650 __setup("trace_buf_size=", set_buf_size);
652 static int __init set_tracing_thresh(char *str)
654 unsigned long threshold;
659 ret = kstrtoul(str, 0, &threshold);
662 tracing_thresh = threshold * 1000;
665 __setup("tracing_thresh=", set_tracing_thresh);
667 unsigned long nsecs_to_usecs(unsigned long nsecs)
672 /* These must match the bit postions in trace_iterator_flags */
673 static const char *trace_options[] = {
706 int in_ns; /* is this clock in nanoseconds? */
708 { trace_clock_local, "local", 1 },
709 { trace_clock_global, "global", 1 },
710 { trace_clock_counter, "counter", 0 },
711 { trace_clock_jiffies, "uptime", 1 },
712 { trace_clock, "perf", 1 },
719 * trace_parser_get_init - gets the buffer for trace parser
721 int trace_parser_get_init(struct trace_parser *parser, int size)
723 memset(parser, 0, sizeof(*parser));
725 parser->buffer = kmalloc(size, GFP_KERNEL);
734 * trace_parser_put - frees the buffer for trace parser
736 void trace_parser_put(struct trace_parser *parser)
738 kfree(parser->buffer);
742 * trace_get_user - reads the user input string separated by space
743 * (matched by isspace(ch))
745 * For each string found the 'struct trace_parser' is updated,
746 * and the function returns.
748 * Returns number of bytes read.
750 * See kernel/trace/trace.h for 'struct trace_parser' details.
752 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
753 size_t cnt, loff_t *ppos)
760 trace_parser_clear(parser);
762 ret = get_user(ch, ubuf++);
770 * The parser is not finished with the last write,
771 * continue reading the user input without skipping spaces.
774 /* skip white space */
775 while (cnt && isspace(ch)) {
776 ret = get_user(ch, ubuf++);
783 /* only spaces were written */
793 /* read the non-space input */
794 while (cnt && !isspace(ch)) {
795 if (parser->idx < parser->size - 1)
796 parser->buffer[parser->idx++] = ch;
801 ret = get_user(ch, ubuf++);
808 /* We either got finished input or we have to wait for another call. */
810 parser->buffer[parser->idx] = 0;
811 parser->cont = false;
814 parser->buffer[parser->idx++] = ch;
824 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
832 if (s->len <= s->readpos)
835 len = s->len - s->readpos;
838 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
848 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
852 if (s->len <= s->readpos)
855 len = s->len - s->readpos;
858 memcpy(buf, s->buffer + s->readpos, cnt);
865 * ftrace_max_lock is used to protect the swapping of buffers
866 * when taking a max snapshot. The buffers themselves are
867 * protected by per_cpu spinlocks. But the action of the swap
868 * needs its own lock.
870 * This is defined as a arch_spinlock_t in order to help
871 * with performance when lockdep debugging is enabled.
873 * It is also used in other places outside the update_max_tr
874 * so it needs to be defined outside of the
875 * CONFIG_TRACER_MAX_TRACE.
877 static arch_spinlock_t ftrace_max_lock =
878 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
880 unsigned long __read_mostly tracing_thresh;
882 #ifdef CONFIG_TRACER_MAX_TRACE
883 unsigned long __read_mostly tracing_max_latency;
886 * Copy the new maximum trace into the separate maximum-trace
887 * structure. (this way the maximum trace is permanently saved,
888 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
891 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
893 struct trace_buffer *trace_buf = &tr->trace_buffer;
894 struct trace_buffer *max_buf = &tr->max_buffer;
895 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
896 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
899 max_buf->time_start = data->preempt_timestamp;
901 max_data->saved_latency = tracing_max_latency;
902 max_data->critical_start = data->critical_start;
903 max_data->critical_end = data->critical_end;
905 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
906 max_data->pid = tsk->pid;
908 * If tsk == current, then use current_uid(), as that does not use
909 * RCU. The irq tracer can be called out of RCU scope.
912 max_data->uid = current_uid();
914 max_data->uid = task_uid(tsk);
916 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
917 max_data->policy = tsk->policy;
918 max_data->rt_priority = tsk->rt_priority;
920 /* record this tasks comm */
921 tracing_record_cmdline(tsk);
925 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
927 * @tsk: the task with the latency
928 * @cpu: The cpu that initiated the trace.
930 * Flip the buffers between the @tr and the max_tr and record information
931 * about which task was the cause of this latency.
934 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
936 struct ring_buffer *buf;
941 WARN_ON_ONCE(!irqs_disabled());
943 if (!tr->allocated_snapshot) {
944 /* Only the nop tracer should hit this when disabling */
945 WARN_ON_ONCE(tr->current_trace != &nop_trace);
949 arch_spin_lock(&ftrace_max_lock);
951 buf = tr->trace_buffer.buffer;
952 tr->trace_buffer.buffer = tr->max_buffer.buffer;
953 tr->max_buffer.buffer = buf;
955 __update_max_tr(tr, tsk, cpu);
956 arch_spin_unlock(&ftrace_max_lock);
960 * update_max_tr_single - only copy one trace over, and reset the rest
962 * @tsk - task with the latency
963 * @cpu - the cpu of the buffer to copy.
965 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
968 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
975 WARN_ON_ONCE(!irqs_disabled());
976 if (!tr->allocated_snapshot) {
977 /* Only the nop tracer should hit this when disabling */
978 WARN_ON_ONCE(tr->current_trace != &nop_trace);
982 arch_spin_lock(&ftrace_max_lock);
984 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
988 * We failed to swap the buffer due to a commit taking
989 * place on this CPU. We fail to record, but we reset
990 * the max trace buffer (no one writes directly to it)
991 * and flag that it failed.
993 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
994 "Failed to swap buffers due to commit in progress\n");
997 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
999 __update_max_tr(tr, tsk, cpu);
1000 arch_spin_unlock(&ftrace_max_lock);
1002 #endif /* CONFIG_TRACER_MAX_TRACE */
1004 static void default_wait_pipe(struct trace_iterator *iter)
1006 /* Iterators are static, they should be filled or empty */
1007 if (trace_buffer_iter(iter, iter->cpu_file))
1010 ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
1013 #ifdef CONFIG_FTRACE_STARTUP_TEST
1014 static int run_tracer_selftest(struct tracer *type)
1016 struct trace_array *tr = &global_trace;
1017 struct tracer *saved_tracer = tr->current_trace;
1020 if (!type->selftest || tracing_selftest_disabled)
1024 * Run a selftest on this tracer.
1025 * Here we reset the trace buffer, and set the current
1026 * tracer to be this tracer. The tracer can then run some
1027 * internal tracing to verify that everything is in order.
1028 * If we fail, we do not register this tracer.
1030 tracing_reset_online_cpus(&tr->trace_buffer);
1032 tr->current_trace = type;
1034 #ifdef CONFIG_TRACER_MAX_TRACE
1035 if (type->use_max_tr) {
1036 /* If we expanded the buffers, make sure the max is expanded too */
1037 if (ring_buffer_expanded)
1038 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1039 RING_BUFFER_ALL_CPUS);
1040 tr->allocated_snapshot = true;
1044 /* the test is responsible for initializing and enabling */
1045 pr_info("Testing tracer %s: ", type->name);
1046 ret = type->selftest(type, tr);
1047 /* the test is responsible for resetting too */
1048 tr->current_trace = saved_tracer;
1050 printk(KERN_CONT "FAILED!\n");
1051 /* Add the warning after printing 'FAILED' */
1055 /* Only reset on passing, to avoid touching corrupted buffers */
1056 tracing_reset_online_cpus(&tr->trace_buffer);
1058 #ifdef CONFIG_TRACER_MAX_TRACE
1059 if (type->use_max_tr) {
1060 tr->allocated_snapshot = false;
1062 /* Shrink the max buffer again */
1063 if (ring_buffer_expanded)
1064 ring_buffer_resize(tr->max_buffer.buffer, 1,
1065 RING_BUFFER_ALL_CPUS);
1069 printk(KERN_CONT "PASSED\n");
1073 static inline int run_tracer_selftest(struct tracer *type)
1077 #endif /* CONFIG_FTRACE_STARTUP_TEST */
1080 * register_tracer - register a tracer with the ftrace system.
1081 * @type - the plugin for the tracer
1083 * Register a new plugin tracer.
1085 int register_tracer(struct tracer *type)
1091 pr_info("Tracer must have a name\n");
1095 if (strlen(type->name) >= MAX_TRACER_SIZE) {
1096 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1100 mutex_lock(&trace_types_lock);
1102 tracing_selftest_running = true;
1104 for (t = trace_types; t; t = t->next) {
1105 if (strcmp(type->name, t->name) == 0) {
1107 pr_info("Tracer %s already registered\n",
1114 if (!type->set_flag)
1115 type->set_flag = &dummy_set_flag;
1117 type->flags = &dummy_tracer_flags;
1119 if (!type->flags->opts)
1120 type->flags->opts = dummy_tracer_opt;
1121 if (!type->wait_pipe)
1122 type->wait_pipe = default_wait_pipe;
1124 ret = run_tracer_selftest(type);
1128 type->next = trace_types;
1132 tracing_selftest_running = false;
1133 mutex_unlock(&trace_types_lock);
1135 if (ret || !default_bootup_tracer)
1138 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
1141 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1142 /* Do we want this tracer to start on bootup? */
1143 tracing_set_tracer(type->name);
1144 default_bootup_tracer = NULL;
1145 /* disable other selftests, since this will break it. */
1146 tracing_selftest_disabled = true;
1147 #ifdef CONFIG_FTRACE_STARTUP_TEST
1148 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1156 void tracing_reset(struct trace_buffer *buf, int cpu)
1158 struct ring_buffer *buffer = buf->buffer;
1163 ring_buffer_record_disable(buffer);
1165 /* Make sure all commits have finished */
1166 synchronize_sched();
1167 ring_buffer_reset_cpu(buffer, cpu);
1169 ring_buffer_record_enable(buffer);
1172 void tracing_reset_online_cpus(struct trace_buffer *buf)
1174 struct ring_buffer *buffer = buf->buffer;
1180 ring_buffer_record_disable(buffer);
1182 /* Make sure all commits have finished */
1183 synchronize_sched();
1185 buf->time_start = ftrace_now(buf->cpu);
1187 for_each_online_cpu(cpu)
1188 ring_buffer_reset_cpu(buffer, cpu);
1190 ring_buffer_record_enable(buffer);
1193 void tracing_reset_current(int cpu)
1195 tracing_reset(&global_trace.trace_buffer, cpu);
1198 void tracing_reset_all_online_cpus(void)
1200 struct trace_array *tr;
1202 mutex_lock(&trace_types_lock);
1203 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1204 tracing_reset_online_cpus(&tr->trace_buffer);
1205 #ifdef CONFIG_TRACER_MAX_TRACE
1206 tracing_reset_online_cpus(&tr->max_buffer);
1209 mutex_unlock(&trace_types_lock);
1212 #define SAVED_CMDLINES 128
1213 #define NO_CMDLINE_MAP UINT_MAX
1214 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1215 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1216 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1217 static int cmdline_idx;
1218 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1220 /* temporary disable recording */
1221 static atomic_t trace_record_cmdline_disabled __read_mostly;
1223 static void trace_init_cmdlines(void)
1225 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1226 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
1230 int is_tracing_stopped(void)
1232 return global_trace.stop_count;
1236 * ftrace_off_permanent - disable all ftrace code permanently
1238 * This should only be called when a serious anomally has
1239 * been detected. This will turn off the function tracing,
1240 * ring buffers, and other tracing utilites. It takes no
1241 * locks and can be called from any context.
1243 void ftrace_off_permanent(void)
1245 tracing_disabled = 1;
1247 tracing_off_permanent();
1251 * tracing_start - quick start of the tracer
1253 * If tracing is enabled but was stopped by tracing_stop,
1254 * this will start the tracer back up.
1256 void tracing_start(void)
1258 struct ring_buffer *buffer;
1259 unsigned long flags;
1261 if (tracing_disabled)
1264 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1265 if (--global_trace.stop_count) {
1266 if (global_trace.stop_count < 0) {
1267 /* Someone screwed up their debugging */
1269 global_trace.stop_count = 0;
1274 /* Prevent the buffers from switching */
1275 arch_spin_lock(&ftrace_max_lock);
1277 buffer = global_trace.trace_buffer.buffer;
1279 ring_buffer_record_enable(buffer);
1281 #ifdef CONFIG_TRACER_MAX_TRACE
1282 buffer = global_trace.max_buffer.buffer;
1284 ring_buffer_record_enable(buffer);
1287 arch_spin_unlock(&ftrace_max_lock);
1291 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1294 static void tracing_start_tr(struct trace_array *tr)
1296 struct ring_buffer *buffer;
1297 unsigned long flags;
1299 if (tracing_disabled)
1302 /* If global, we need to also start the max tracer */
1303 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1304 return tracing_start();
1306 raw_spin_lock_irqsave(&tr->start_lock, flags);
1308 if (--tr->stop_count) {
1309 if (tr->stop_count < 0) {
1310 /* Someone screwed up their debugging */
1317 buffer = tr->trace_buffer.buffer;
1319 ring_buffer_record_enable(buffer);
1322 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1326 * tracing_stop - quick stop of the tracer
1328 * Light weight way to stop tracing. Use in conjunction with
1331 void tracing_stop(void)
1333 struct ring_buffer *buffer;
1334 unsigned long flags;
1337 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1338 if (global_trace.stop_count++)
1341 /* Prevent the buffers from switching */
1342 arch_spin_lock(&ftrace_max_lock);
1344 buffer = global_trace.trace_buffer.buffer;
1346 ring_buffer_record_disable(buffer);
1348 #ifdef CONFIG_TRACER_MAX_TRACE
1349 buffer = global_trace.max_buffer.buffer;
1351 ring_buffer_record_disable(buffer);
1354 arch_spin_unlock(&ftrace_max_lock);
1357 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1360 static void tracing_stop_tr(struct trace_array *tr)
1362 struct ring_buffer *buffer;
1363 unsigned long flags;
1365 /* If global, we need to also stop the max tracer */
1366 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1367 return tracing_stop();
1369 raw_spin_lock_irqsave(&tr->start_lock, flags);
1370 if (tr->stop_count++)
1373 buffer = tr->trace_buffer.buffer;
1375 ring_buffer_record_disable(buffer);
1378 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1381 void trace_stop_cmdline_recording(void);
1383 static void trace_save_cmdline(struct task_struct *tsk)
1387 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1391 * It's not the end of the world if we don't get
1392 * the lock, but we also don't want to spin
1393 * nor do we want to disable interrupts,
1394 * so if we miss here, then better luck next time.
1396 if (!arch_spin_trylock(&trace_cmdline_lock))
1399 idx = map_pid_to_cmdline[tsk->pid];
1400 if (idx == NO_CMDLINE_MAP) {
1401 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1404 * Check whether the cmdline buffer at idx has a pid
1405 * mapped. We are going to overwrite that entry so we
1406 * need to clear the map_pid_to_cmdline. Otherwise we
1407 * would read the new comm for the old pid.
1409 pid = map_cmdline_to_pid[idx];
1410 if (pid != NO_CMDLINE_MAP)
1411 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1413 map_cmdline_to_pid[idx] = tsk->pid;
1414 map_pid_to_cmdline[tsk->pid] = idx;
1419 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1421 arch_spin_unlock(&trace_cmdline_lock);
1424 void trace_find_cmdline(int pid, char comm[])
1429 strcpy(comm, "<idle>");
1433 if (WARN_ON_ONCE(pid < 0)) {
1434 strcpy(comm, "<XXX>");
1438 if (pid > PID_MAX_DEFAULT) {
1439 strcpy(comm, "<...>");
1444 arch_spin_lock(&trace_cmdline_lock);
1445 map = map_pid_to_cmdline[pid];
1446 if (map != NO_CMDLINE_MAP)
1447 strcpy(comm, saved_cmdlines[map]);
1449 strcpy(comm, "<...>");
1451 arch_spin_unlock(&trace_cmdline_lock);
1455 void tracing_record_cmdline(struct task_struct *tsk)
1457 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1460 if (!__this_cpu_read(trace_cmdline_save))
1463 __this_cpu_write(trace_cmdline_save, false);
1465 trace_save_cmdline(tsk);
1469 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1472 struct task_struct *tsk = current;
1474 entry->preempt_count = pc & 0xff;
1475 entry->pid = (tsk) ? tsk->pid : 0;
1477 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1478 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1480 TRACE_FLAG_IRQS_NOSUPPORT |
1482 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1483 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1484 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1486 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1488 struct ring_buffer_event *
1489 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1492 unsigned long flags, int pc)
1494 struct ring_buffer_event *event;
1496 event = ring_buffer_lock_reserve(buffer, len);
1497 if (event != NULL) {
1498 struct trace_entry *ent = ring_buffer_event_data(event);
1500 tracing_generic_entry_update(ent, flags, pc);
1508 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1510 __this_cpu_write(trace_cmdline_save, true);
1511 ring_buffer_unlock_commit(buffer, event);
1515 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1516 struct ring_buffer_event *event,
1517 unsigned long flags, int pc)
1519 __buffer_unlock_commit(buffer, event);
1521 ftrace_trace_stack(buffer, flags, 6, pc);
1522 ftrace_trace_userstack(buffer, flags, pc);
1525 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1526 struct ring_buffer_event *event,
1527 unsigned long flags, int pc)
1529 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1531 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1533 struct ring_buffer_event *
1534 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1535 struct ftrace_event_file *ftrace_file,
1536 int type, unsigned long len,
1537 unsigned long flags, int pc)
1539 *current_rb = ftrace_file->tr->trace_buffer.buffer;
1540 return trace_buffer_lock_reserve(*current_rb,
1541 type, len, flags, pc);
1543 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1545 struct ring_buffer_event *
1546 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1547 int type, unsigned long len,
1548 unsigned long flags, int pc)
1550 *current_rb = global_trace.trace_buffer.buffer;
1551 return trace_buffer_lock_reserve(*current_rb,
1552 type, len, flags, pc);
1554 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1556 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1557 struct ring_buffer_event *event,
1558 unsigned long flags, int pc)
1560 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1562 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1564 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1565 struct ring_buffer_event *event,
1566 unsigned long flags, int pc,
1567 struct pt_regs *regs)
1569 __buffer_unlock_commit(buffer, event);
1571 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1572 ftrace_trace_userstack(buffer, flags, pc);
1574 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1576 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1577 struct ring_buffer_event *event)
1579 ring_buffer_discard_commit(buffer, event);
1581 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1584 trace_function(struct trace_array *tr,
1585 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1588 struct ftrace_event_call *call = &event_function;
1589 struct ring_buffer *buffer = tr->trace_buffer.buffer;
1590 struct ring_buffer_event *event;
1591 struct ftrace_entry *entry;
1593 /* If we are reading the ring buffer, don't trace */
1594 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1597 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1601 entry = ring_buffer_event_data(event);
1603 entry->parent_ip = parent_ip;
1605 if (!filter_check_discard(call, entry, buffer, event))
1606 __buffer_unlock_commit(buffer, event);
1610 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1611 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1614 if (likely(!atomic_read(&data->disabled)))
1615 trace_function(tr, ip, parent_ip, flags, pc);
1618 #ifdef CONFIG_STACKTRACE
1620 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1621 struct ftrace_stack {
1622 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1625 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1626 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1628 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1629 unsigned long flags,
1630 int skip, int pc, struct pt_regs *regs)
1632 struct ftrace_event_call *call = &event_kernel_stack;
1633 struct ring_buffer_event *event;
1634 struct stack_entry *entry;
1635 struct stack_trace trace;
1637 int size = FTRACE_STACK_ENTRIES;
1639 trace.nr_entries = 0;
1643 * Since events can happen in NMIs there's no safe way to
1644 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1645 * or NMI comes in, it will just have to use the default
1646 * FTRACE_STACK_SIZE.
1648 preempt_disable_notrace();
1650 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1652 * We don't need any atomic variables, just a barrier.
1653 * If an interrupt comes in, we don't care, because it would
1654 * have exited and put the counter back to what we want.
1655 * We just need a barrier to keep gcc from moving things
1659 if (use_stack == 1) {
1660 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1661 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1664 save_stack_trace_regs(regs, &trace);
1666 save_stack_trace(&trace);
1668 if (trace.nr_entries > size)
1669 size = trace.nr_entries;
1671 /* From now on, use_stack is a boolean */
1674 size *= sizeof(unsigned long);
1676 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1677 sizeof(*entry) + size, flags, pc);
1680 entry = ring_buffer_event_data(event);
1682 memset(&entry->caller, 0, size);
1685 memcpy(&entry->caller, trace.entries,
1686 trace.nr_entries * sizeof(unsigned long));
1688 trace.max_entries = FTRACE_STACK_ENTRIES;
1689 trace.entries = entry->caller;
1691 save_stack_trace_regs(regs, &trace);
1693 save_stack_trace(&trace);
1696 entry->size = trace.nr_entries;
1698 if (!filter_check_discard(call, entry, buffer, event))
1699 __buffer_unlock_commit(buffer, event);
1702 /* Again, don't let gcc optimize things here */
1704 __this_cpu_dec(ftrace_stack_reserve);
1705 preempt_enable_notrace();
1709 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1710 int skip, int pc, struct pt_regs *regs)
1712 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1715 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1718 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1721 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1724 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1727 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1730 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
1734 * trace_dump_stack - record a stack back trace in the trace buffer
1735 * @skip: Number of functions to skip (helper handlers)
1737 void trace_dump_stack(int skip)
1739 unsigned long flags;
1741 if (tracing_disabled || tracing_selftest_running)
1744 local_save_flags(flags);
1747 * Skip 3 more, seems to get us at the caller of
1751 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1752 flags, skip, preempt_count(), NULL);
1755 static DEFINE_PER_CPU(int, user_stack_count);
1758 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1760 struct ftrace_event_call *call = &event_user_stack;
1761 struct ring_buffer_event *event;
1762 struct userstack_entry *entry;
1763 struct stack_trace trace;
1765 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1769 * NMIs can not handle page faults, even with fix ups.
1770 * The save user stack can (and often does) fault.
1772 if (unlikely(in_nmi()))
1776 * prevent recursion, since the user stack tracing may
1777 * trigger other kernel events.
1780 if (__this_cpu_read(user_stack_count))
1783 __this_cpu_inc(user_stack_count);
1785 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1786 sizeof(*entry), flags, pc);
1788 goto out_drop_count;
1789 entry = ring_buffer_event_data(event);
1791 entry->tgid = current->tgid;
1792 memset(&entry->caller, 0, sizeof(entry->caller));
1794 trace.nr_entries = 0;
1795 trace.max_entries = FTRACE_STACK_ENTRIES;
1797 trace.entries = entry->caller;
1799 save_stack_trace_user(&trace);
1800 if (!filter_check_discard(call, entry, buffer, event))
1801 __buffer_unlock_commit(buffer, event);
1804 __this_cpu_dec(user_stack_count);
1810 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1812 ftrace_trace_userstack(tr, flags, preempt_count());
1816 #endif /* CONFIG_STACKTRACE */
1818 /* created for use with alloc_percpu */
1819 struct trace_buffer_struct {
1820 char buffer[TRACE_BUF_SIZE];
1823 static struct trace_buffer_struct *trace_percpu_buffer;
1824 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1825 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1826 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1829 * The buffer used is dependent on the context. There is a per cpu
1830 * buffer for normal context, softirq contex, hard irq context and
1831 * for NMI context. Thise allows for lockless recording.
1833 * Note, if the buffers failed to be allocated, then this returns NULL
1835 static char *get_trace_buf(void)
1837 struct trace_buffer_struct *percpu_buffer;
1840 * If we have allocated per cpu buffers, then we do not
1841 * need to do any locking.
1844 percpu_buffer = trace_percpu_nmi_buffer;
1846 percpu_buffer = trace_percpu_irq_buffer;
1847 else if (in_softirq())
1848 percpu_buffer = trace_percpu_sirq_buffer;
1850 percpu_buffer = trace_percpu_buffer;
1855 return this_cpu_ptr(&percpu_buffer->buffer[0]);
1858 static int alloc_percpu_trace_buffer(void)
1860 struct trace_buffer_struct *buffers;
1861 struct trace_buffer_struct *sirq_buffers;
1862 struct trace_buffer_struct *irq_buffers;
1863 struct trace_buffer_struct *nmi_buffers;
1865 buffers = alloc_percpu(struct trace_buffer_struct);
1869 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1873 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1877 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1881 trace_percpu_buffer = buffers;
1882 trace_percpu_sirq_buffer = sirq_buffers;
1883 trace_percpu_irq_buffer = irq_buffers;
1884 trace_percpu_nmi_buffer = nmi_buffers;
1889 free_percpu(irq_buffers);
1891 free_percpu(sirq_buffers);
1893 free_percpu(buffers);
1895 WARN(1, "Could not allocate percpu trace_printk buffer");
1899 static int buffers_allocated;
1901 void trace_printk_init_buffers(void)
1903 if (buffers_allocated)
1906 if (alloc_percpu_trace_buffer())
1909 pr_info("ftrace: Allocated trace_printk buffers\n");
1911 /* Expand the buffers to set size */
1912 tracing_update_buffers();
1914 buffers_allocated = 1;
1917 * trace_printk_init_buffers() can be called by modules.
1918 * If that happens, then we need to start cmdline recording
1919 * directly here. If the global_trace.buffer is already
1920 * allocated here, then this was called by module code.
1922 if (global_trace.trace_buffer.buffer)
1923 tracing_start_cmdline_record();
1926 void trace_printk_start_comm(void)
1928 /* Start tracing comms if trace printk is set */
1929 if (!buffers_allocated)
1931 tracing_start_cmdline_record();
1934 static void trace_printk_start_stop_comm(int enabled)
1936 if (!buffers_allocated)
1940 tracing_start_cmdline_record();
1942 tracing_stop_cmdline_record();
1946 * trace_vbprintk - write binary msg to tracing buffer
1949 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1951 struct ftrace_event_call *call = &event_bprint;
1952 struct ring_buffer_event *event;
1953 struct ring_buffer *buffer;
1954 struct trace_array *tr = &global_trace;
1955 struct bprint_entry *entry;
1956 unsigned long flags;
1958 int len = 0, size, pc;
1960 if (unlikely(tracing_selftest_running || tracing_disabled))
1963 /* Don't pollute graph traces with trace_vprintk internals */
1964 pause_graph_tracing();
1966 pc = preempt_count();
1967 preempt_disable_notrace();
1969 tbuffer = get_trace_buf();
1975 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1977 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1980 local_save_flags(flags);
1981 size = sizeof(*entry) + sizeof(u32) * len;
1982 buffer = tr->trace_buffer.buffer;
1983 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1987 entry = ring_buffer_event_data(event);
1991 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1992 if (!filter_check_discard(call, entry, buffer, event)) {
1993 __buffer_unlock_commit(buffer, event);
1994 ftrace_trace_stack(buffer, flags, 6, pc);
1998 preempt_enable_notrace();
1999 unpause_graph_tracing();
2003 EXPORT_SYMBOL_GPL(trace_vbprintk);
2006 __trace_array_vprintk(struct ring_buffer *buffer,
2007 unsigned long ip, const char *fmt, va_list args)
2009 struct ftrace_event_call *call = &event_print;
2010 struct ring_buffer_event *event;
2011 int len = 0, size, pc;
2012 struct print_entry *entry;
2013 unsigned long flags;
2016 if (tracing_disabled || tracing_selftest_running)
2019 /* Don't pollute graph traces with trace_vprintk internals */
2020 pause_graph_tracing();
2022 pc = preempt_count();
2023 preempt_disable_notrace();
2026 tbuffer = get_trace_buf();
2032 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
2033 if (len > TRACE_BUF_SIZE)
2036 local_save_flags(flags);
2037 size = sizeof(*entry) + len + 1;
2038 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
2042 entry = ring_buffer_event_data(event);
2045 memcpy(&entry->buf, tbuffer, len);
2046 entry->buf[len] = '\0';
2047 if (!filter_check_discard(call, entry, buffer, event)) {
2048 __buffer_unlock_commit(buffer, event);
2049 ftrace_trace_stack(buffer, flags, 6, pc);
2052 preempt_enable_notrace();
2053 unpause_graph_tracing();
2058 int trace_array_vprintk(struct trace_array *tr,
2059 unsigned long ip, const char *fmt, va_list args)
2061 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2064 int trace_array_printk(struct trace_array *tr,
2065 unsigned long ip, const char *fmt, ...)
2070 if (!(trace_flags & TRACE_ITER_PRINTK))
2074 ret = trace_array_vprintk(tr, ip, fmt, ap);
2079 int trace_array_printk_buf(struct ring_buffer *buffer,
2080 unsigned long ip, const char *fmt, ...)
2085 if (!(trace_flags & TRACE_ITER_PRINTK))
2089 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2094 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2096 return trace_array_vprintk(&global_trace, ip, fmt, args);
2098 EXPORT_SYMBOL_GPL(trace_vprintk);
2100 static void trace_iterator_increment(struct trace_iterator *iter)
2102 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2106 ring_buffer_read(buf_iter, NULL);
2109 static struct trace_entry *
2110 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2111 unsigned long *lost_events)
2113 struct ring_buffer_event *event;
2114 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
2117 event = ring_buffer_iter_peek(buf_iter, ts);
2119 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
2123 iter->ent_size = ring_buffer_event_length(event);
2124 return ring_buffer_event_data(event);
2130 static struct trace_entry *
2131 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2132 unsigned long *missing_events, u64 *ent_ts)
2134 struct ring_buffer *buffer = iter->trace_buffer->buffer;
2135 struct trace_entry *ent, *next = NULL;
2136 unsigned long lost_events = 0, next_lost = 0;
2137 int cpu_file = iter->cpu_file;
2138 u64 next_ts = 0, ts;
2144 * If we are in a per_cpu trace file, don't bother by iterating over
2145 * all cpu and peek directly.
2147 if (cpu_file > RING_BUFFER_ALL_CPUS) {
2148 if (ring_buffer_empty_cpu(buffer, cpu_file))
2150 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
2152 *ent_cpu = cpu_file;
2157 for_each_tracing_cpu(cpu) {
2159 if (ring_buffer_empty_cpu(buffer, cpu))
2162 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
2165 * Pick the entry with the smallest timestamp:
2167 if (ent && (!next || ts < next_ts)) {
2171 next_lost = lost_events;
2172 next_size = iter->ent_size;
2176 iter->ent_size = next_size;
2179 *ent_cpu = next_cpu;
2185 *missing_events = next_lost;
2190 /* Find the next real entry, without updating the iterator itself */
2191 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2192 int *ent_cpu, u64 *ent_ts)
2194 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
2197 /* Find the next real entry, and increment the iterator to the next entry */
2198 void *trace_find_next_entry_inc(struct trace_iterator *iter)
2200 iter->ent = __find_next_entry(iter, &iter->cpu,
2201 &iter->lost_events, &iter->ts);
2204 trace_iterator_increment(iter);
2206 return iter->ent ? iter : NULL;
2209 static void trace_consume(struct trace_iterator *iter)
2211 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
2212 &iter->lost_events);
2215 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
2217 struct trace_iterator *iter = m->private;
2221 WARN_ON_ONCE(iter->leftover);
2225 /* can't go backwards */
2230 ent = trace_find_next_entry_inc(iter);
2234 while (ent && iter->idx < i)
2235 ent = trace_find_next_entry_inc(iter);
2242 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2244 struct ring_buffer_event *event;
2245 struct ring_buffer_iter *buf_iter;
2246 unsigned long entries = 0;
2249 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2251 buf_iter = trace_buffer_iter(iter, cpu);
2255 ring_buffer_iter_reset(buf_iter);
2258 * We could have the case with the max latency tracers
2259 * that a reset never took place on a cpu. This is evident
2260 * by the timestamp being before the start of the buffer.
2262 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
2263 if (ts >= iter->trace_buffer->time_start)
2266 ring_buffer_read(buf_iter, NULL);
2269 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2273 * The current tracer is copied to avoid a global locking
2276 static void *s_start(struct seq_file *m, loff_t *pos)
2278 struct trace_iterator *iter = m->private;
2279 struct trace_array *tr = iter->tr;
2280 int cpu_file = iter->cpu_file;
2286 * copy the tracer to avoid using a global lock all around.
2287 * iter->trace is a copy of current_trace, the pointer to the
2288 * name may be used instead of a strcmp(), as iter->trace->name
2289 * will point to the same string as current_trace->name.
2291 mutex_lock(&trace_types_lock);
2292 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2293 *iter->trace = *tr->current_trace;
2294 mutex_unlock(&trace_types_lock);
2296 #ifdef CONFIG_TRACER_MAX_TRACE
2297 if (iter->snapshot && iter->trace->use_max_tr)
2298 return ERR_PTR(-EBUSY);
2301 if (!iter->snapshot)
2302 atomic_inc(&trace_record_cmdline_disabled);
2304 if (*pos != iter->pos) {
2309 if (cpu_file == RING_BUFFER_ALL_CPUS) {
2310 for_each_tracing_cpu(cpu)
2311 tracing_iter_reset(iter, cpu);
2313 tracing_iter_reset(iter, cpu_file);
2316 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2321 * If we overflowed the seq_file before, then we want
2322 * to just reuse the trace_seq buffer again.
2328 p = s_next(m, p, &l);
2332 trace_event_read_lock();
2333 trace_access_lock(cpu_file);
2337 static void s_stop(struct seq_file *m, void *p)
2339 struct trace_iterator *iter = m->private;
2341 #ifdef CONFIG_TRACER_MAX_TRACE
2342 if (iter->snapshot && iter->trace->use_max_tr)
2346 if (!iter->snapshot)
2347 atomic_dec(&trace_record_cmdline_disabled);
2349 trace_access_unlock(iter->cpu_file);
2350 trace_event_read_unlock();
2354 get_total_entries(struct trace_buffer *buf,
2355 unsigned long *total, unsigned long *entries)
2357 unsigned long count;
2363 for_each_tracing_cpu(cpu) {
2364 count = ring_buffer_entries_cpu(buf->buffer, cpu);
2366 * If this buffer has skipped entries, then we hold all
2367 * entries for the trace and we need to ignore the
2368 * ones before the time stamp.
2370 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2371 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
2372 /* total is the same as the entries */
2376 ring_buffer_overrun_cpu(buf->buffer, cpu);
2381 static void print_lat_help_header(struct seq_file *m)
2383 seq_puts(m, "# _------=> CPU# \n");
2384 seq_puts(m, "# / _-----=> irqs-off \n");
2385 seq_puts(m, "# | / _----=> need-resched \n");
2386 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2387 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2388 seq_puts(m, "# |||| / delay \n");
2389 seq_puts(m, "# cmd pid ||||| time | caller \n");
2390 seq_puts(m, "# \\ / ||||| \\ | / \n");
2393 static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
2395 unsigned long total;
2396 unsigned long entries;
2398 get_total_entries(buf, &total, &entries);
2399 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2400 entries, total, num_online_cpus());
2404 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
2406 print_event_info(buf, m);
2407 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2408 seq_puts(m, "# | | | | |\n");
2411 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
2413 print_event_info(buf, m);
2414 seq_puts(m, "# _-----=> irqs-off\n");
2415 seq_puts(m, "# / _----=> need-resched\n");
2416 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2417 seq_puts(m, "# || / _--=> preempt-depth\n");
2418 seq_puts(m, "# ||| / delay\n");
2419 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2420 seq_puts(m, "# | | | |||| | |\n");
2424 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2426 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2427 struct trace_buffer *buf = iter->trace_buffer;
2428 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2429 struct tracer *type = iter->trace;
2430 unsigned long entries;
2431 unsigned long total;
2432 const char *name = "preemption";
2436 get_total_entries(buf, &total, &entries);
2438 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2440 seq_puts(m, "# -----------------------------------"
2441 "---------------------------------\n");
2442 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2443 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2444 nsecs_to_usecs(data->saved_latency),
2448 #if defined(CONFIG_PREEMPT_NONE)
2450 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2452 #elif defined(CONFIG_PREEMPT)
2457 /* These are reserved for later use */
2460 seq_printf(m, " #P:%d)\n", num_online_cpus());
2464 seq_puts(m, "# -----------------\n");
2465 seq_printf(m, "# | task: %.16s-%d "
2466 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2467 data->comm, data->pid,
2468 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2469 data->policy, data->rt_priority);
2470 seq_puts(m, "# -----------------\n");
2472 if (data->critical_start) {
2473 seq_puts(m, "# => started at: ");
2474 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2475 trace_print_seq(m, &iter->seq);
2476 seq_puts(m, "\n# => ended at: ");
2477 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2478 trace_print_seq(m, &iter->seq);
2479 seq_puts(m, "\n#\n");
2485 static void test_cpu_buff_start(struct trace_iterator *iter)
2487 struct trace_seq *s = &iter->seq;
2489 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2492 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2495 if (cpumask_test_cpu(iter->cpu, iter->started))
2498 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2501 cpumask_set_cpu(iter->cpu, iter->started);
2503 /* Don't print started cpu buffer for the first entry of the trace */
2505 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2509 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2511 struct trace_seq *s = &iter->seq;
2512 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2513 struct trace_entry *entry;
2514 struct trace_event *event;
2518 test_cpu_buff_start(iter);
2520 event = ftrace_find_event(entry->type);
2522 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2523 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2524 if (!trace_print_lat_context(iter))
2527 if (!trace_print_context(iter))
2533 return event->funcs->trace(iter, sym_flags, event);
2535 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2538 return TRACE_TYPE_HANDLED;
2540 return TRACE_TYPE_PARTIAL_LINE;
2543 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2545 struct trace_seq *s = &iter->seq;
2546 struct trace_entry *entry;
2547 struct trace_event *event;
2551 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2552 if (!trace_seq_printf(s, "%d %d %llu ",
2553 entry->pid, iter->cpu, iter->ts))
2557 event = ftrace_find_event(entry->type);
2559 return event->funcs->raw(iter, 0, event);
2561 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2564 return TRACE_TYPE_HANDLED;
2566 return TRACE_TYPE_PARTIAL_LINE;
2569 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2571 struct trace_seq *s = &iter->seq;
2572 unsigned char newline = '\n';
2573 struct trace_entry *entry;
2574 struct trace_event *event;
2578 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2579 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2580 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2581 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2584 event = ftrace_find_event(entry->type);
2586 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2587 if (ret != TRACE_TYPE_HANDLED)
2591 SEQ_PUT_FIELD_RET(s, newline);
2593 return TRACE_TYPE_HANDLED;
2596 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2598 struct trace_seq *s = &iter->seq;
2599 struct trace_entry *entry;
2600 struct trace_event *event;
2604 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2605 SEQ_PUT_FIELD_RET(s, entry->pid);
2606 SEQ_PUT_FIELD_RET(s, iter->cpu);
2607 SEQ_PUT_FIELD_RET(s, iter->ts);
2610 event = ftrace_find_event(entry->type);
2611 return event ? event->funcs->binary(iter, 0, event) :
2615 int trace_empty(struct trace_iterator *iter)
2617 struct ring_buffer_iter *buf_iter;
2620 /* If we are looking at one CPU buffer, only check that one */
2621 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2622 cpu = iter->cpu_file;
2623 buf_iter = trace_buffer_iter(iter, cpu);
2625 if (!ring_buffer_iter_empty(buf_iter))
2628 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2634 for_each_tracing_cpu(cpu) {
2635 buf_iter = trace_buffer_iter(iter, cpu);
2637 if (!ring_buffer_iter_empty(buf_iter))
2640 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2648 /* Called with trace_event_read_lock() held. */
2649 enum print_line_t print_trace_line(struct trace_iterator *iter)
2651 enum print_line_t ret;
2653 if (iter->lost_events &&
2654 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2655 iter->cpu, iter->lost_events))
2656 return TRACE_TYPE_PARTIAL_LINE;
2658 if (iter->trace && iter->trace->print_line) {
2659 ret = iter->trace->print_line(iter);
2660 if (ret != TRACE_TYPE_UNHANDLED)
2664 if (iter->ent->type == TRACE_BPUTS &&
2665 trace_flags & TRACE_ITER_PRINTK &&
2666 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2667 return trace_print_bputs_msg_only(iter);
2669 if (iter->ent->type == TRACE_BPRINT &&
2670 trace_flags & TRACE_ITER_PRINTK &&
2671 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2672 return trace_print_bprintk_msg_only(iter);
2674 if (iter->ent->type == TRACE_PRINT &&
2675 trace_flags & TRACE_ITER_PRINTK &&
2676 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2677 return trace_print_printk_msg_only(iter);
2679 if (trace_flags & TRACE_ITER_BIN)
2680 return print_bin_fmt(iter);
2682 if (trace_flags & TRACE_ITER_HEX)
2683 return print_hex_fmt(iter);
2685 if (trace_flags & TRACE_ITER_RAW)
2686 return print_raw_fmt(iter);
2688 return print_trace_fmt(iter);
2691 void trace_latency_header(struct seq_file *m)
2693 struct trace_iterator *iter = m->private;
2695 /* print nothing if the buffers are empty */
2696 if (trace_empty(iter))
2699 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2700 print_trace_header(m, iter);
2702 if (!(trace_flags & TRACE_ITER_VERBOSE))
2703 print_lat_help_header(m);
2706 void trace_default_header(struct seq_file *m)
2708 struct trace_iterator *iter = m->private;
2710 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2713 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2714 /* print nothing if the buffers are empty */
2715 if (trace_empty(iter))
2717 print_trace_header(m, iter);
2718 if (!(trace_flags & TRACE_ITER_VERBOSE))
2719 print_lat_help_header(m);
2721 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2722 if (trace_flags & TRACE_ITER_IRQ_INFO)
2723 print_func_help_header_irq(iter->trace_buffer, m);
2725 print_func_help_header(iter->trace_buffer, m);
2730 static void test_ftrace_alive(struct seq_file *m)
2732 if (!ftrace_is_dead())
2734 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2735 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2738 #ifdef CONFIG_TRACER_MAX_TRACE
2739 static void show_snapshot_main_help(struct seq_file *m)
2741 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2742 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2743 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2744 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2745 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2746 seq_printf(m, "# is not a '0' or '1')\n");
2749 static void show_snapshot_percpu_help(struct seq_file *m)
2751 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2752 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2753 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2754 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2756 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2757 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2759 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2760 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2761 seq_printf(m, "# is not a '0' or '1')\n");
2764 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2766 if (iter->tr->allocated_snapshot)
2767 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2769 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2771 seq_printf(m, "# Snapshot commands:\n");
2772 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2773 show_snapshot_main_help(m);
2775 show_snapshot_percpu_help(m);
2778 /* Should never be called */
2779 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2782 static int s_show(struct seq_file *m, void *v)
2784 struct trace_iterator *iter = v;
2787 if (iter->ent == NULL) {
2789 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2791 test_ftrace_alive(m);
2793 if (iter->snapshot && trace_empty(iter))
2794 print_snapshot_help(m, iter);
2795 else if (iter->trace && iter->trace->print_header)
2796 iter->trace->print_header(m);
2798 trace_default_header(m);
2800 } else if (iter->leftover) {
2802 * If we filled the seq_file buffer earlier, we
2803 * want to just show it now.
2805 ret = trace_print_seq(m, &iter->seq);
2807 /* ret should this time be zero, but you never know */
2808 iter->leftover = ret;
2811 print_trace_line(iter);
2812 ret = trace_print_seq(m, &iter->seq);
2814 * If we overflow the seq_file buffer, then it will
2815 * ask us for this data again at start up.
2817 * ret is 0 if seq_file write succeeded.
2820 iter->leftover = ret;
2826 static const struct seq_operations tracer_seq_ops = {
2833 static struct trace_iterator *
2834 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2836 struct trace_cpu *tc = inode->i_private;
2837 struct trace_array *tr = tc->tr;
2838 struct trace_iterator *iter;
2841 if (tracing_disabled)
2842 return ERR_PTR(-ENODEV);
2844 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2846 return ERR_PTR(-ENOMEM);
2848 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2850 if (!iter->buffer_iter)
2854 * We make a copy of the current tracer to avoid concurrent
2855 * changes on it while we are reading.
2857 mutex_lock(&trace_types_lock);
2858 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2862 *iter->trace = *tr->current_trace;
2864 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2869 #ifdef CONFIG_TRACER_MAX_TRACE
2870 /* Currently only the top directory has a snapshot */
2871 if (tr->current_trace->print_max || snapshot)
2872 iter->trace_buffer = &tr->max_buffer;
2875 iter->trace_buffer = &tr->trace_buffer;
2876 iter->snapshot = snapshot;
2878 mutex_init(&iter->mutex);
2879 iter->cpu_file = tc->cpu;
2881 /* Notify the tracer early; before we stop tracing. */
2882 if (iter->trace && iter->trace->open)
2883 iter->trace->open(iter);
2885 /* Annotate start of buffers if we had overruns */
2886 if (ring_buffer_overruns(iter->trace_buffer->buffer))
2887 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2889 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2890 if (trace_clocks[trace_clock_id].in_ns)
2891 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2893 /* stop the trace while dumping if we are not opening "snapshot" */
2894 if (!iter->snapshot)
2895 tracing_stop_tr(tr);
2897 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
2898 for_each_tracing_cpu(cpu) {
2899 iter->buffer_iter[cpu] =
2900 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2902 ring_buffer_read_prepare_sync();
2903 for_each_tracing_cpu(cpu) {
2904 ring_buffer_read_start(iter->buffer_iter[cpu]);
2905 tracing_iter_reset(iter, cpu);
2908 cpu = iter->cpu_file;
2909 iter->buffer_iter[cpu] =
2910 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2911 ring_buffer_read_prepare_sync();
2912 ring_buffer_read_start(iter->buffer_iter[cpu]);
2913 tracing_iter_reset(iter, cpu);
2918 mutex_unlock(&trace_types_lock);
2923 mutex_unlock(&trace_types_lock);
2925 kfree(iter->buffer_iter);
2927 seq_release_private(inode, file);
2928 return ERR_PTR(-ENOMEM);
2931 int tracing_open_generic(struct inode *inode, struct file *filp)
2933 if (tracing_disabled)
2936 filp->private_data = inode->i_private;
2940 static int tracing_release(struct inode *inode, struct file *file)
2942 struct seq_file *m = file->private_data;
2943 struct trace_iterator *iter;
2944 struct trace_array *tr;
2947 if (!(file->f_mode & FMODE_READ))
2953 mutex_lock(&trace_types_lock);
2958 for_each_tracing_cpu(cpu) {
2959 if (iter->buffer_iter[cpu])
2960 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2963 if (iter->trace && iter->trace->close)
2964 iter->trace->close(iter);
2966 if (!iter->snapshot)
2967 /* reenable tracing if it was previously enabled */
2968 tracing_start_tr(tr);
2969 mutex_unlock(&trace_types_lock);
2971 mutex_destroy(&iter->mutex);
2972 free_cpumask_var(iter->started);
2974 kfree(iter->buffer_iter);
2975 seq_release_private(inode, file);
2979 static int tracing_open(struct inode *inode, struct file *file)
2981 struct trace_iterator *iter;
2984 /* If this file was open for write, then erase contents */
2985 if ((file->f_mode & FMODE_WRITE) &&
2986 (file->f_flags & O_TRUNC)) {
2987 struct trace_cpu *tc = inode->i_private;
2988 struct trace_array *tr = tc->tr;
2990 if (tc->cpu == RING_BUFFER_ALL_CPUS)
2991 tracing_reset_online_cpus(&tr->trace_buffer);
2993 tracing_reset(&tr->trace_buffer, tc->cpu);
2996 if (file->f_mode & FMODE_READ) {
2997 iter = __tracing_open(inode, file, false);
2999 ret = PTR_ERR(iter);
3000 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
3001 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3007 t_next(struct seq_file *m, void *v, loff_t *pos)
3009 struct tracer *t = v;
3019 static void *t_start(struct seq_file *m, loff_t *pos)
3024 mutex_lock(&trace_types_lock);
3025 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
3031 static void t_stop(struct seq_file *m, void *p)
3033 mutex_unlock(&trace_types_lock);
3036 static int t_show(struct seq_file *m, void *v)
3038 struct tracer *t = v;
3043 seq_printf(m, "%s", t->name);
3052 static const struct seq_operations show_traces_seq_ops = {
3059 static int show_traces_open(struct inode *inode, struct file *file)
3061 if (tracing_disabled)
3064 return seq_open(file, &show_traces_seq_ops);
3068 tracing_write_stub(struct file *filp, const char __user *ubuf,
3069 size_t count, loff_t *ppos)
3074 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
3076 if (file->f_mode & FMODE_READ)
3077 return seq_lseek(file, offset, origin);
3082 static const struct file_operations tracing_fops = {
3083 .open = tracing_open,
3085 .write = tracing_write_stub,
3086 .llseek = tracing_seek,
3087 .release = tracing_release,
3090 static const struct file_operations show_traces_fops = {
3091 .open = show_traces_open,
3093 .release = seq_release,
3094 .llseek = seq_lseek,
3098 * Only trace on a CPU if the bitmask is set:
3100 static cpumask_var_t tracing_cpumask;
3103 * The tracer itself will not take this lock, but still we want
3104 * to provide a consistent cpumask to user-space:
3106 static DEFINE_MUTEX(tracing_cpumask_update_lock);
3109 * Temporary storage for the character representation of the
3110 * CPU bitmask (and one more byte for the newline):
3112 static char mask_str[NR_CPUS + 1];
3115 tracing_cpumask_read(struct file *filp, char __user *ubuf,
3116 size_t count, loff_t *ppos)
3120 mutex_lock(&tracing_cpumask_update_lock);
3122 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
3123 if (count - len < 2) {
3127 len += sprintf(mask_str + len, "\n");
3128 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3131 mutex_unlock(&tracing_cpumask_update_lock);
3137 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3138 size_t count, loff_t *ppos)
3140 struct trace_array *tr = filp->private_data;
3141 cpumask_var_t tracing_cpumask_new;
3144 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3147 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
3151 mutex_lock(&tracing_cpumask_update_lock);
3153 local_irq_disable();
3154 arch_spin_lock(&ftrace_max_lock);
3155 for_each_tracing_cpu(cpu) {
3157 * Increase/decrease the disabled counter if we are
3158 * about to flip a bit in the cpumask:
3160 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
3161 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3162 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3163 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
3165 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
3166 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3167 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3168 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
3171 arch_spin_unlock(&ftrace_max_lock);
3174 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
3176 mutex_unlock(&tracing_cpumask_update_lock);
3177 free_cpumask_var(tracing_cpumask_new);
3182 free_cpumask_var(tracing_cpumask_new);
3187 static const struct file_operations tracing_cpumask_fops = {
3188 .open = tracing_open_generic,
3189 .read = tracing_cpumask_read,
3190 .write = tracing_cpumask_write,
3191 .llseek = generic_file_llseek,
3194 static int tracing_trace_options_show(struct seq_file *m, void *v)
3196 struct tracer_opt *trace_opts;
3197 struct trace_array *tr = m->private;
3201 mutex_lock(&trace_types_lock);
3202 tracer_flags = tr->current_trace->flags->val;
3203 trace_opts = tr->current_trace->flags->opts;
3205 for (i = 0; trace_options[i]; i++) {
3206 if (trace_flags & (1 << i))
3207 seq_printf(m, "%s\n", trace_options[i]);
3209 seq_printf(m, "no%s\n", trace_options[i]);
3212 for (i = 0; trace_opts[i].name; i++) {
3213 if (tracer_flags & trace_opts[i].bit)
3214 seq_printf(m, "%s\n", trace_opts[i].name);
3216 seq_printf(m, "no%s\n", trace_opts[i].name);
3218 mutex_unlock(&trace_types_lock);
3223 static int __set_tracer_option(struct tracer *trace,
3224 struct tracer_flags *tracer_flags,
3225 struct tracer_opt *opts, int neg)
3229 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
3234 tracer_flags->val &= ~opts->bit;
3236 tracer_flags->val |= opts->bit;
3240 /* Try to assign a tracer specific option */
3241 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3243 struct tracer_flags *tracer_flags = trace->flags;
3244 struct tracer_opt *opts = NULL;
3247 for (i = 0; tracer_flags->opts[i].name; i++) {
3248 opts = &tracer_flags->opts[i];
3250 if (strcmp(cmp, opts->name) == 0)
3251 return __set_tracer_option(trace, trace->flags,
3258 /* Some tracers require overwrite to stay enabled */
3259 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3261 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3267 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
3269 /* do nothing if flag is already set */
3270 if (!!(trace_flags & mask) == !!enabled)
3273 /* Give the tracer a chance to approve the change */
3274 if (tr->current_trace->flag_changed)
3275 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
3279 trace_flags |= mask;
3281 trace_flags &= ~mask;
3283 if (mask == TRACE_ITER_RECORD_CMD)
3284 trace_event_enable_cmd_record(enabled);
3286 if (mask == TRACE_ITER_OVERWRITE) {
3287 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
3288 #ifdef CONFIG_TRACER_MAX_TRACE
3289 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
3293 if (mask == TRACE_ITER_PRINTK)
3294 trace_printk_start_stop_comm(enabled);
3299 static int trace_set_options(struct trace_array *tr, char *option)
3306 cmp = strstrip(option);
3308 if (strncmp(cmp, "no", 2) == 0) {
3313 mutex_lock(&trace_types_lock);
3315 for (i = 0; trace_options[i]; i++) {
3316 if (strcmp(cmp, trace_options[i]) == 0) {
3317 ret = set_tracer_flag(tr, 1 << i, !neg);
3322 /* If no option could be set, test the specific tracer options */
3323 if (!trace_options[i])
3324 ret = set_tracer_option(tr->current_trace, cmp, neg);
3326 mutex_unlock(&trace_types_lock);
3332 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3333 size_t cnt, loff_t *ppos)
3335 struct seq_file *m = filp->private_data;
3336 struct trace_array *tr = m->private;
3340 if (cnt >= sizeof(buf))
3343 if (copy_from_user(&buf, ubuf, cnt))
3348 ret = trace_set_options(tr, buf);
3357 static int tracing_trace_options_open(struct inode *inode, struct file *file)
3359 if (tracing_disabled)
3362 return single_open(file, tracing_trace_options_show, inode->i_private);
3365 static const struct file_operations tracing_iter_fops = {
3366 .open = tracing_trace_options_open,
3368 .llseek = seq_lseek,
3369 .release = single_release,
3370 .write = tracing_trace_options_write,
3373 static const char readme_msg[] =
3374 "tracing mini-HOWTO:\n\n"
3375 "# echo 0 > tracing_on : quick way to disable tracing\n"
3376 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3377 " Important files:\n"
3378 " trace\t\t\t- The static contents of the buffer\n"
3379 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3380 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3381 " current_tracer\t- function and latency tracers\n"
3382 " available_tracers\t- list of configured tracers for current_tracer\n"
3383 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3384 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3385 " trace_clock\t\t-change the clock used to order events\n"
3386 " local: Per cpu clock but may not be synced across CPUs\n"
3387 " global: Synced across CPUs but slows tracing down.\n"
3388 " counter: Not a clock, but just an increment\n"
3389 " uptime: Jiffy counter from time of boot\n"
3390 " perf: Same clock that perf events use\n"
3391 #ifdef CONFIG_X86_64
3392 " x86-tsc: TSC cycle counter\n"
3394 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3395 " tracing_cpumask\t- Limit which CPUs to trace\n"
3396 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3397 "\t\t\t Remove sub-buffer with rmdir\n"
3398 " trace_options\t\t- Set format or modify how tracing happens\n"
3399 "\t\t\t Disable an option by adding a suffix 'no' to the option name\n"
3400 #ifdef CONFIG_DYNAMIC_FTRACE
3401 "\n available_filter_functions - list of functions that can be filtered on\n"
3402 " set_ftrace_filter\t- echo function name in here to only trace these functions\n"
3403 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3404 " modules: Can select a group via module\n"
3405 " Format: :mod:<module-name>\n"
3406 " example: echo :mod:ext3 > set_ftrace_filter\n"
3407 " triggers: a command to perform when function is hit\n"
3408 " Format: <function>:<trigger>[:count]\n"
3409 " trigger: traceon, traceoff\n"
3410 " enable_event:<system>:<event>\n"
3411 " disable_event:<system>:<event>\n"
3412 #ifdef CONFIG_STACKTRACE
3415 #ifdef CONFIG_TRACER_SNAPSHOT
3418 " example: echo do_fault:traceoff > set_ftrace_filter\n"
3419 " echo do_trap:traceoff:3 > set_ftrace_filter\n"
3420 " The first one will disable tracing every time do_fault is hit\n"
3421 " The second will disable tracing at most 3 times when do_trap is hit\n"
3422 " The first time do trap is hit and it disables tracing, the counter\n"
3423 " will decrement to 2. If tracing is already disabled, the counter\n"
3424 " will not decrement. It only decrements when the trigger did work\n"
3425 " To remove trigger without count:\n"
3426 " echo '!<function>:<trigger> > set_ftrace_filter\n"
3427 " To remove trigger with a count:\n"
3428 " echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
3429 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
3430 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3431 " modules: Can select a group via module command :mod:\n"
3432 " Does not accept triggers\n"
3433 #endif /* CONFIG_DYNAMIC_FTRACE */
3434 #ifdef CONFIG_FUNCTION_TRACER
3435 " set_ftrace_pid\t- Write pid(s) to only function trace those pids (function)\n"
3437 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3438 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3439 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3441 #ifdef CONFIG_TRACER_SNAPSHOT
3442 "\n snapshot\t\t- Like 'trace' but shows the content of the static snapshot buffer\n"
3443 "\t\t\t Read the contents for more information\n"
3445 #ifdef CONFIG_STACKTRACE
3446 " stack_trace\t\t- Shows the max stack trace when active\n"
3447 " stack_max_size\t- Shows current max stack size that was traced\n"
3448 "\t\t\t Write into this file to reset the max size (trigger a new trace)\n"
3449 #ifdef CONFIG_DYNAMIC_FTRACE
3450 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace traces\n"
3452 #endif /* CONFIG_STACKTRACE */
3456 tracing_readme_read(struct file *filp, char __user *ubuf,
3457 size_t cnt, loff_t *ppos)
3459 return simple_read_from_buffer(ubuf, cnt, ppos,
3460 readme_msg, strlen(readme_msg));
3463 static const struct file_operations tracing_readme_fops = {
3464 .open = tracing_open_generic,
3465 .read = tracing_readme_read,
3466 .llseek = generic_file_llseek,
3470 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3471 size_t cnt, loff_t *ppos)
3480 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3484 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3492 for (i = 0; i < SAVED_CMDLINES; i++) {
3495 pid = map_cmdline_to_pid[i];
3496 if (pid == -1 || pid == NO_CMDLINE_MAP)
3499 trace_find_cmdline(pid, buf_comm);
3500 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3505 len = simple_read_from_buffer(ubuf, cnt, ppos,
3514 static const struct file_operations tracing_saved_cmdlines_fops = {
3515 .open = tracing_open_generic,
3516 .read = tracing_saved_cmdlines_read,
3517 .llseek = generic_file_llseek,
3521 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3522 size_t cnt, loff_t *ppos)
3524 struct trace_array *tr = filp->private_data;
3525 char buf[MAX_TRACER_SIZE+2];
3528 mutex_lock(&trace_types_lock);
3529 r = sprintf(buf, "%s\n", tr->current_trace->name);
3530 mutex_unlock(&trace_types_lock);
3532 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3535 int tracer_init(struct tracer *t, struct trace_array *tr)
3537 tracing_reset_online_cpus(&tr->trace_buffer);
3541 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
3545 for_each_tracing_cpu(cpu)
3546 per_cpu_ptr(buf->data, cpu)->entries = val;
3549 #ifdef CONFIG_TRACER_MAX_TRACE
3550 /* resize @tr's buffer to the size of @size_tr's entries */
3551 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3552 struct trace_buffer *size_buf, int cpu_id)
3556 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3557 for_each_tracing_cpu(cpu) {
3558 ret = ring_buffer_resize(trace_buf->buffer,
3559 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
3562 per_cpu_ptr(trace_buf->data, cpu)->entries =
3563 per_cpu_ptr(size_buf->data, cpu)->entries;
3566 ret = ring_buffer_resize(trace_buf->buffer,
3567 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
3569 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3570 per_cpu_ptr(size_buf->data, cpu_id)->entries;
3575 #endif /* CONFIG_TRACER_MAX_TRACE */
3577 static int __tracing_resize_ring_buffer(struct trace_array *tr,
3578 unsigned long size, int cpu)
3583 * If kernel or user changes the size of the ring buffer
3584 * we use the size that was given, and we can forget about
3585 * expanding it later.
3587 ring_buffer_expanded = true;
3589 /* May be called before buffers are initialized */
3590 if (!tr->trace_buffer.buffer)
3593 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
3597 #ifdef CONFIG_TRACER_MAX_TRACE
3598 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3599 !tr->current_trace->use_max_tr)
3602 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
3604 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3605 &tr->trace_buffer, cpu);
3608 * AARGH! We are left with different
3609 * size max buffer!!!!
3610 * The max buffer is our "snapshot" buffer.
3611 * When a tracer needs a snapshot (one of the
3612 * latency tracers), it swaps the max buffer
3613 * with the saved snap shot. We succeeded to
3614 * update the size of the main buffer, but failed to
3615 * update the size of the max buffer. But when we tried
3616 * to reset the main buffer to the original size, we
3617 * failed there too. This is very unlikely to
3618 * happen, but if it does, warn and kill all
3622 tracing_disabled = 1;
3627 if (cpu == RING_BUFFER_ALL_CPUS)
3628 set_buffer_entries(&tr->max_buffer, size);
3630 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
3633 #endif /* CONFIG_TRACER_MAX_TRACE */
3635 if (cpu == RING_BUFFER_ALL_CPUS)
3636 set_buffer_entries(&tr->trace_buffer, size);
3638 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
3643 static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3644 unsigned long size, int cpu_id)
3648 mutex_lock(&trace_types_lock);
3650 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3651 /* make sure, this cpu is enabled in the mask */
3652 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3658 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
3663 mutex_unlock(&trace_types_lock);
3670 * tracing_update_buffers - used by tracing facility to expand ring buffers
3672 * To save on memory when the tracing is never used on a system with it
3673 * configured in. The ring buffers are set to a minimum size. But once
3674 * a user starts to use the tracing facility, then they need to grow
3675 * to their default size.
3677 * This function is to be called when a tracer is about to be used.
3679 int tracing_update_buffers(void)
3683 mutex_lock(&trace_types_lock);
3684 if (!ring_buffer_expanded)
3685 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
3686 RING_BUFFER_ALL_CPUS);
3687 mutex_unlock(&trace_types_lock);
3692 struct trace_option_dentry;
3694 static struct trace_option_dentry *
3695 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
3698 destroy_trace_option_files(struct trace_option_dentry *topts);
3700 static int tracing_set_tracer(const char *buf)
3702 static struct trace_option_dentry *topts;
3703 struct trace_array *tr = &global_trace;
3705 #ifdef CONFIG_TRACER_MAX_TRACE
3710 mutex_lock(&trace_types_lock);
3712 if (!ring_buffer_expanded) {
3713 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
3714 RING_BUFFER_ALL_CPUS);
3720 for (t = trace_types; t; t = t->next) {
3721 if (strcmp(t->name, buf) == 0)
3728 if (t == tr->current_trace)
3731 trace_branch_disable();
3733 tr->current_trace->enabled = false;
3735 if (tr->current_trace->reset)
3736 tr->current_trace->reset(tr);
3738 /* Current trace needs to be nop_trace before synchronize_sched */
3739 tr->current_trace = &nop_trace;
3741 #ifdef CONFIG_TRACER_MAX_TRACE
3742 had_max_tr = tr->allocated_snapshot;
3744 if (had_max_tr && !t->use_max_tr) {
3746 * We need to make sure that the update_max_tr sees that
3747 * current_trace changed to nop_trace to keep it from
3748 * swapping the buffers after we resize it.
3749 * The update_max_tr is called from interrupts disabled
3750 * so a synchronized_sched() is sufficient.
3752 synchronize_sched();
3756 destroy_trace_option_files(topts);
3758 topts = create_trace_option_files(tr, t);
3760 #ifdef CONFIG_TRACER_MAX_TRACE
3761 if (t->use_max_tr && !had_max_tr) {
3762 ret = alloc_snapshot(tr);
3769 ret = tracer_init(t, tr);
3774 tr->current_trace = t;
3775 tr->current_trace->enabled = true;
3776 trace_branch_enable(tr);
3778 mutex_unlock(&trace_types_lock);
3784 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3785 size_t cnt, loff_t *ppos)
3787 char buf[MAX_TRACER_SIZE+1];
3794 if (cnt > MAX_TRACER_SIZE)
3795 cnt = MAX_TRACER_SIZE;
3797 if (copy_from_user(&buf, ubuf, cnt))
3802 /* strip ending whitespace. */
3803 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3806 err = tracing_set_tracer(buf);
3816 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3817 size_t cnt, loff_t *ppos)
3819 unsigned long *ptr = filp->private_data;
3823 r = snprintf(buf, sizeof(buf), "%ld\n",
3824 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3825 if (r > sizeof(buf))
3827 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3831 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3832 size_t cnt, loff_t *ppos)
3834 unsigned long *ptr = filp->private_data;
3838 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3847 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3849 struct trace_cpu *tc = inode->i_private;
3850 struct trace_array *tr = tc->tr;
3851 struct trace_iterator *iter;
3854 if (tracing_disabled)
3857 mutex_lock(&trace_types_lock);
3859 /* create a buffer to store the information to pass to userspace */
3860 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3867 * We make a copy of the current tracer to avoid concurrent
3868 * changes on it while we are reading.
3870 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3875 *iter->trace = *tr->current_trace;
3877 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3882 /* trace pipe does not show start of buffer */
3883 cpumask_setall(iter->started);
3885 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3886 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3888 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3889 if (trace_clocks[trace_clock_id].in_ns)
3890 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3892 iter->cpu_file = tc->cpu;
3894 iter->trace_buffer = &tc->tr->trace_buffer;
3895 mutex_init(&iter->mutex);
3896 filp->private_data = iter;
3898 if (iter->trace->pipe_open)
3899 iter->trace->pipe_open(iter);
3901 nonseekable_open(inode, filp);
3903 mutex_unlock(&trace_types_lock);
3909 mutex_unlock(&trace_types_lock);
3913 static int tracing_release_pipe(struct inode *inode, struct file *file)
3915 struct trace_iterator *iter = file->private_data;
3917 mutex_lock(&trace_types_lock);
3919 if (iter->trace->pipe_close)
3920 iter->trace->pipe_close(iter);
3922 mutex_unlock(&trace_types_lock);
3924 free_cpumask_var(iter->started);
3925 mutex_destroy(&iter->mutex);
3933 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
3935 /* Iterators are static, they should be filled or empty */
3936 if (trace_buffer_iter(iter, iter->cpu_file))
3937 return POLLIN | POLLRDNORM;
3939 if (trace_flags & TRACE_ITER_BLOCK)
3941 * Always select as readable when in blocking mode
3943 return POLLIN | POLLRDNORM;
3945 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
3950 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3952 struct trace_iterator *iter = filp->private_data;
3954 return trace_poll(iter, filp, poll_table);
3958 * This is a make-shift waitqueue.
3959 * A tracer might use this callback on some rare cases:
3961 * 1) the current tracer might hold the runqueue lock when it wakes up
3962 * a reader, hence a deadlock (sched, function, and function graph tracers)
3963 * 2) the function tracers, trace all functions, we don't want
3964 * the overhead of calling wake_up and friends
3965 * (and tracing them too)
3967 * Anyway, this is really very primitive wakeup.
3969 void poll_wait_pipe(struct trace_iterator *iter)
3971 set_current_state(TASK_INTERRUPTIBLE);
3972 /* sleep for 100 msecs, and try again. */
3973 schedule_timeout(HZ / 10);
3976 /* Must be called with trace_types_lock mutex held. */
3977 static int tracing_wait_pipe(struct file *filp)
3979 struct trace_iterator *iter = filp->private_data;
3981 while (trace_empty(iter)) {
3983 if ((filp->f_flags & O_NONBLOCK)) {
3987 mutex_unlock(&iter->mutex);
3989 iter->trace->wait_pipe(iter);
3991 mutex_lock(&iter->mutex);
3993 if (signal_pending(current))
3997 * We block until we read something and tracing is disabled.
3998 * We still block if tracing is disabled, but we have never
3999 * read anything. This allows a user to cat this file, and
4000 * then enable tracing. But after we have read something,
4001 * we give an EOF when tracing is again disabled.
4003 * iter->pos will be 0 if we haven't read anything.
4005 if (!tracing_is_on() && iter->pos)
4016 tracing_read_pipe(struct file *filp, char __user *ubuf,
4017 size_t cnt, loff_t *ppos)
4019 struct trace_iterator *iter = filp->private_data;
4020 struct trace_array *tr = iter->tr;
4023 /* return any leftover data */
4024 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4028 trace_seq_init(&iter->seq);
4030 /* copy the tracer to avoid using a global lock all around */
4031 mutex_lock(&trace_types_lock);
4032 if (unlikely(iter->trace->name != tr->current_trace->name))
4033 *iter->trace = *tr->current_trace;
4034 mutex_unlock(&trace_types_lock);
4037 * Avoid more than one consumer on a single file descriptor
4038 * This is just a matter of traces coherency, the ring buffer itself
4041 mutex_lock(&iter->mutex);
4042 if (iter->trace->read) {
4043 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
4049 sret = tracing_wait_pipe(filp);
4053 /* stop when tracing is finished */
4054 if (trace_empty(iter)) {
4059 if (cnt >= PAGE_SIZE)
4060 cnt = PAGE_SIZE - 1;
4062 /* reset all but tr, trace, and overruns */
4063 memset(&iter->seq, 0,
4064 sizeof(struct trace_iterator) -
4065 offsetof(struct trace_iterator, seq));
4068 trace_event_read_lock();
4069 trace_access_lock(iter->cpu_file);
4070 while (trace_find_next_entry_inc(iter) != NULL) {
4071 enum print_line_t ret;
4072 int len = iter->seq.len;
4074 ret = print_trace_line(iter);
4075 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4076 /* don't print partial lines */
4077 iter->seq.len = len;
4080 if (ret != TRACE_TYPE_NO_CONSUME)
4081 trace_consume(iter);
4083 if (iter->seq.len >= cnt)
4087 * Setting the full flag means we reached the trace_seq buffer
4088 * size and we should leave by partial output condition above.
4089 * One of the trace_seq_* functions is not used properly.
4091 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
4094 trace_access_unlock(iter->cpu_file);
4095 trace_event_read_unlock();
4097 /* Now copy what we have to the user */
4098 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4099 if (iter->seq.readpos >= iter->seq.len)
4100 trace_seq_init(&iter->seq);
4103 * If there was nothing to send to user, in spite of consuming trace
4104 * entries, go back to wait for more entries.
4110 mutex_unlock(&iter->mutex);
4115 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
4116 struct pipe_buffer *buf)
4118 __free_page(buf->page);
4121 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
4124 __free_page(spd->pages[idx]);
4127 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
4129 .map = generic_pipe_buf_map,
4130 .unmap = generic_pipe_buf_unmap,
4131 .confirm = generic_pipe_buf_confirm,
4132 .release = tracing_pipe_buf_release,
4133 .steal = generic_pipe_buf_steal,
4134 .get = generic_pipe_buf_get,
4138 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
4143 /* Seq buffer is page-sized, exactly what we need. */
4145 count = iter->seq.len;
4146 ret = print_trace_line(iter);
4147 count = iter->seq.len - count;
4150 iter->seq.len -= count;
4153 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4154 iter->seq.len -= count;
4158 if (ret != TRACE_TYPE_NO_CONSUME)
4159 trace_consume(iter);
4161 if (!trace_find_next_entry_inc(iter)) {
4171 static ssize_t tracing_splice_read_pipe(struct file *filp,
4173 struct pipe_inode_info *pipe,
4177 struct page *pages_def[PIPE_DEF_BUFFERS];
4178 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4179 struct trace_iterator *iter = filp->private_data;
4180 struct splice_pipe_desc spd = {
4182 .partial = partial_def,
4183 .nr_pages = 0, /* This gets updated below. */
4184 .nr_pages_max = PIPE_DEF_BUFFERS,
4186 .ops = &tracing_pipe_buf_ops,
4187 .spd_release = tracing_spd_release_pipe,
4189 struct trace_array *tr = iter->tr;
4194 if (splice_grow_spd(pipe, &spd))
4197 /* copy the tracer to avoid using a global lock all around */
4198 mutex_lock(&trace_types_lock);
4199 if (unlikely(iter->trace->name != tr->current_trace->name))
4200 *iter->trace = *tr->current_trace;
4201 mutex_unlock(&trace_types_lock);
4203 mutex_lock(&iter->mutex);
4205 if (iter->trace->splice_read) {
4206 ret = iter->trace->splice_read(iter, filp,
4207 ppos, pipe, len, flags);
4212 ret = tracing_wait_pipe(filp);
4216 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
4221 trace_event_read_lock();
4222 trace_access_lock(iter->cpu_file);
4224 /* Fill as many pages as possible. */
4225 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
4226 spd.pages[i] = alloc_page(GFP_KERNEL);
4230 rem = tracing_fill_pipe_page(rem, iter);
4232 /* Copy the data into the page, so we can start over. */
4233 ret = trace_seq_to_buffer(&iter->seq,
4234 page_address(spd.pages[i]),
4237 __free_page(spd.pages[i]);
4240 spd.partial[i].offset = 0;
4241 spd.partial[i].len = iter->seq.len;
4243 trace_seq_init(&iter->seq);
4246 trace_access_unlock(iter->cpu_file);
4247 trace_event_read_unlock();
4248 mutex_unlock(&iter->mutex);
4252 ret = splice_to_pipe(pipe, &spd);
4254 splice_shrink_spd(&spd);
4258 mutex_unlock(&iter->mutex);
4263 tracing_entries_read(struct file *filp, char __user *ubuf,
4264 size_t cnt, loff_t *ppos)
4266 struct trace_cpu *tc = filp->private_data;
4267 struct trace_array *tr = tc->tr;
4272 mutex_lock(&trace_types_lock);
4274 if (tc->cpu == RING_BUFFER_ALL_CPUS) {
4275 int cpu, buf_size_same;
4280 /* check if all cpu sizes are same */
4281 for_each_tracing_cpu(cpu) {
4282 /* fill in the size from first enabled cpu */
4284 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4285 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
4291 if (buf_size_same) {
4292 if (!ring_buffer_expanded)
4293 r = sprintf(buf, "%lu (expanded: %lu)\n",
4295 trace_buf_size >> 10);
4297 r = sprintf(buf, "%lu\n", size >> 10);
4299 r = sprintf(buf, "X\n");
4301 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, tc->cpu)->entries >> 10);
4303 mutex_unlock(&trace_types_lock);
4305 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4310 tracing_entries_write(struct file *filp, const char __user *ubuf,
4311 size_t cnt, loff_t *ppos)
4313 struct trace_cpu *tc = filp->private_data;
4317 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4321 /* must have at least 1 entry */
4325 /* value is in KB */
4328 ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
4338 tracing_total_entries_read(struct file *filp, char __user *ubuf,
4339 size_t cnt, loff_t *ppos)
4341 struct trace_array *tr = filp->private_data;
4344 unsigned long size = 0, expanded_size = 0;
4346 mutex_lock(&trace_types_lock);
4347 for_each_tracing_cpu(cpu) {
4348 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
4349 if (!ring_buffer_expanded)
4350 expanded_size += trace_buf_size >> 10;
4352 if (ring_buffer_expanded)
4353 r = sprintf(buf, "%lu\n", size);
4355 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4356 mutex_unlock(&trace_types_lock);
4358 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4362 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4363 size_t cnt, loff_t *ppos)
4366 * There is no need to read what the user has written, this function
4367 * is just to make sure that there is no error when "echo" is used
4376 tracing_free_buffer_release(struct inode *inode, struct file *filp)
4378 struct trace_array *tr = inode->i_private;
4380 /* disable tracing ? */
4381 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4383 /* resize the ring buffer to 0 */
4384 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4390 tracing_mark_write(struct file *filp, const char __user *ubuf,
4391 size_t cnt, loff_t *fpos)
4393 unsigned long addr = (unsigned long)ubuf;
4394 struct trace_array *tr = filp->private_data;
4395 struct ring_buffer_event *event;
4396 struct ring_buffer *buffer;
4397 struct print_entry *entry;
4398 unsigned long irq_flags;
4399 struct page *pages[2];
4409 if (tracing_disabled)
4412 if (!(trace_flags & TRACE_ITER_MARKERS))
4415 if (cnt > TRACE_BUF_SIZE)
4416 cnt = TRACE_BUF_SIZE;
4419 * Userspace is injecting traces into the kernel trace buffer.
4420 * We want to be as non intrusive as possible.
4421 * To do so, we do not want to allocate any special buffers
4422 * or take any locks, but instead write the userspace data
4423 * straight into the ring buffer.
4425 * First we need to pin the userspace buffer into memory,
4426 * which, most likely it is, because it just referenced it.
4427 * But there's no guarantee that it is. By using get_user_pages_fast()
4428 * and kmap_atomic/kunmap_atomic() we can get access to the
4429 * pages directly. We then write the data directly into the
4432 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4434 /* check if we cross pages */
4435 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4438 offset = addr & (PAGE_SIZE - 1);
4441 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4442 if (ret < nr_pages) {
4444 put_page(pages[ret]);
4449 for (i = 0; i < nr_pages; i++)
4450 map_page[i] = kmap_atomic(pages[i]);
4452 local_save_flags(irq_flags);
4453 size = sizeof(*entry) + cnt + 2; /* possible \n added */
4454 buffer = tr->trace_buffer.buffer;
4455 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4456 irq_flags, preempt_count());
4458 /* Ring buffer disabled, return as if not open for write */
4463 entry = ring_buffer_event_data(event);
4464 entry->ip = _THIS_IP_;
4466 if (nr_pages == 2) {
4467 len = PAGE_SIZE - offset;
4468 memcpy(&entry->buf, map_page[0] + offset, len);
4469 memcpy(&entry->buf[len], map_page[1], cnt - len);
4471 memcpy(&entry->buf, map_page[0] + offset, cnt);
4473 if (entry->buf[cnt - 1] != '\n') {
4474 entry->buf[cnt] = '\n';
4475 entry->buf[cnt + 1] = '\0';
4477 entry->buf[cnt] = '\0';
4479 __buffer_unlock_commit(buffer, event);
4486 for (i = 0; i < nr_pages; i++){
4487 kunmap_atomic(map_page[i]);
4494 static int tracing_clock_show(struct seq_file *m, void *v)
4496 struct trace_array *tr = m->private;
4499 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4501 "%s%s%s%s", i ? " " : "",
4502 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4503 i == tr->clock_id ? "]" : "");
4509 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4510 size_t cnt, loff_t *fpos)
4512 struct seq_file *m = filp->private_data;
4513 struct trace_array *tr = m->private;
4515 const char *clockstr;
4518 if (cnt >= sizeof(buf))
4521 if (copy_from_user(&buf, ubuf, cnt))
4526 clockstr = strstrip(buf);
4528 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4529 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4532 if (i == ARRAY_SIZE(trace_clocks))
4535 mutex_lock(&trace_types_lock);
4539 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
4542 * New clock may not be consistent with the previous clock.
4543 * Reset the buffer so that it doesn't have incomparable timestamps.
4545 tracing_reset_online_cpus(&global_trace.trace_buffer);
4547 #ifdef CONFIG_TRACER_MAX_TRACE
4548 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4549 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4550 tracing_reset_online_cpus(&global_trace.max_buffer);
4553 mutex_unlock(&trace_types_lock);
4560 static int tracing_clock_open(struct inode *inode, struct file *file)
4562 if (tracing_disabled)
4565 return single_open(file, tracing_clock_show, inode->i_private);
4568 struct ftrace_buffer_info {
4569 struct trace_iterator iter;
4574 #ifdef CONFIG_TRACER_SNAPSHOT
4575 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4577 struct trace_cpu *tc = inode->i_private;
4578 struct trace_iterator *iter;
4582 if (file->f_mode & FMODE_READ) {
4583 iter = __tracing_open(inode, file, true);
4585 ret = PTR_ERR(iter);
4587 /* Writes still need the seq_file to hold the private data */
4588 m = kzalloc(sizeof(*m), GFP_KERNEL);
4591 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4597 iter->trace_buffer = &tc->tr->max_buffer;
4598 iter->cpu_file = tc->cpu;
4600 file->private_data = m;
4607 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4610 struct seq_file *m = filp->private_data;
4611 struct trace_iterator *iter = m->private;
4612 struct trace_array *tr = iter->tr;
4616 ret = tracing_update_buffers();
4620 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4624 mutex_lock(&trace_types_lock);
4626 if (tr->current_trace->use_max_tr) {
4633 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4637 if (tr->allocated_snapshot)
4641 /* Only allow per-cpu swap if the ring buffer supports it */
4642 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4643 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4648 if (!tr->allocated_snapshot) {
4649 ret = alloc_snapshot(tr);
4653 local_irq_disable();
4654 /* Now, we're going to swap */
4655 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4656 update_max_tr(tr, current, smp_processor_id());
4658 update_max_tr_single(tr, current, iter->cpu_file);
4662 if (tr->allocated_snapshot) {
4663 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4664 tracing_reset_online_cpus(&tr->max_buffer);
4666 tracing_reset(&tr->max_buffer, iter->cpu_file);
4676 mutex_unlock(&trace_types_lock);
4680 static int tracing_snapshot_release(struct inode *inode, struct file *file)
4682 struct seq_file *m = file->private_data;
4684 if (file->f_mode & FMODE_READ)
4685 return tracing_release(inode, file);
4687 /* If write only, the seq_file is just a stub */
4695 static int tracing_buffers_open(struct inode *inode, struct file *filp);
4696 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
4697 size_t count, loff_t *ppos);
4698 static int tracing_buffers_release(struct inode *inode, struct file *file);
4699 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4700 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
4702 static int snapshot_raw_open(struct inode *inode, struct file *filp)
4704 struct ftrace_buffer_info *info;
4707 ret = tracing_buffers_open(inode, filp);
4711 info = filp->private_data;
4713 if (info->iter.trace->use_max_tr) {
4714 tracing_buffers_release(inode, filp);
4718 info->iter.snapshot = true;
4719 info->iter.trace_buffer = &info->iter.tr->max_buffer;
4724 #endif /* CONFIG_TRACER_SNAPSHOT */
4727 static const struct file_operations tracing_max_lat_fops = {
4728 .open = tracing_open_generic,
4729 .read = tracing_max_lat_read,
4730 .write = tracing_max_lat_write,
4731 .llseek = generic_file_llseek,
4734 static const struct file_operations set_tracer_fops = {
4735 .open = tracing_open_generic,
4736 .read = tracing_set_trace_read,
4737 .write = tracing_set_trace_write,
4738 .llseek = generic_file_llseek,
4741 static const struct file_operations tracing_pipe_fops = {
4742 .open = tracing_open_pipe,
4743 .poll = tracing_poll_pipe,
4744 .read = tracing_read_pipe,
4745 .splice_read = tracing_splice_read_pipe,
4746 .release = tracing_release_pipe,
4747 .llseek = no_llseek,
4750 static const struct file_operations tracing_entries_fops = {
4751 .open = tracing_open_generic,
4752 .read = tracing_entries_read,
4753 .write = tracing_entries_write,
4754 .llseek = generic_file_llseek,
4757 static const struct file_operations tracing_total_entries_fops = {
4758 .open = tracing_open_generic,
4759 .read = tracing_total_entries_read,
4760 .llseek = generic_file_llseek,
4763 static const struct file_operations tracing_free_buffer_fops = {
4764 .write = tracing_free_buffer_write,
4765 .release = tracing_free_buffer_release,
4768 static const struct file_operations tracing_mark_fops = {
4769 .open = tracing_open_generic,
4770 .write = tracing_mark_write,
4771 .llseek = generic_file_llseek,
4774 static const struct file_operations trace_clock_fops = {
4775 .open = tracing_clock_open,
4777 .llseek = seq_lseek,
4778 .release = single_release,
4779 .write = tracing_clock_write,
4782 #ifdef CONFIG_TRACER_SNAPSHOT
4783 static const struct file_operations snapshot_fops = {
4784 .open = tracing_snapshot_open,
4786 .write = tracing_snapshot_write,
4787 .llseek = tracing_seek,
4788 .release = tracing_snapshot_release,
4791 static const struct file_operations snapshot_raw_fops = {
4792 .open = snapshot_raw_open,
4793 .read = tracing_buffers_read,
4794 .release = tracing_buffers_release,
4795 .splice_read = tracing_buffers_splice_read,
4796 .llseek = no_llseek,
4799 #endif /* CONFIG_TRACER_SNAPSHOT */
4801 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4803 struct trace_cpu *tc = inode->i_private;
4804 struct trace_array *tr = tc->tr;
4805 struct ftrace_buffer_info *info;
4807 if (tracing_disabled)
4810 info = kzalloc(sizeof(*info), GFP_KERNEL);
4814 mutex_lock(&trace_types_lock);
4819 info->iter.cpu_file = tc->cpu;
4820 info->iter.trace = tr->current_trace;
4821 info->iter.trace_buffer = &tr->trace_buffer;
4823 /* Force reading ring buffer for first read */
4824 info->read = (unsigned int)-1;
4826 filp->private_data = info;
4828 mutex_unlock(&trace_types_lock);
4830 return nonseekable_open(inode, filp);
4834 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
4836 struct ftrace_buffer_info *info = filp->private_data;
4837 struct trace_iterator *iter = &info->iter;
4839 return trace_poll(iter, filp, poll_table);
4843 tracing_buffers_read(struct file *filp, char __user *ubuf,
4844 size_t count, loff_t *ppos)
4846 struct ftrace_buffer_info *info = filp->private_data;
4847 struct trace_iterator *iter = &info->iter;
4854 mutex_lock(&trace_types_lock);
4856 #ifdef CONFIG_TRACER_MAX_TRACE
4857 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4864 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
4870 /* Do we have previous read data to read? */
4871 if (info->read < PAGE_SIZE)
4875 trace_access_lock(iter->cpu_file);
4876 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
4880 trace_access_unlock(iter->cpu_file);
4883 if (trace_empty(iter)) {
4884 if ((filp->f_flags & O_NONBLOCK)) {
4888 mutex_unlock(&trace_types_lock);
4889 iter->trace->wait_pipe(iter);
4890 mutex_lock(&trace_types_lock);
4891 if (signal_pending(current)) {
4903 size = PAGE_SIZE - info->read;
4907 ret = copy_to_user(ubuf, info->spare + info->read, size);
4918 mutex_unlock(&trace_types_lock);
4923 static int tracing_buffers_release(struct inode *inode, struct file *file)
4925 struct ftrace_buffer_info *info = file->private_data;
4926 struct trace_iterator *iter = &info->iter;
4928 mutex_lock(&trace_types_lock);
4930 WARN_ON(!iter->tr->ref);
4934 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
4937 mutex_unlock(&trace_types_lock);
4943 struct ring_buffer *buffer;
4948 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4949 struct pipe_buffer *buf)
4951 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4956 ring_buffer_free_read_page(ref->buffer, ref->page);
4961 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4962 struct pipe_buffer *buf)
4964 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4969 /* Pipe buffer operations for a buffer. */
4970 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4972 .map = generic_pipe_buf_map,
4973 .unmap = generic_pipe_buf_unmap,
4974 .confirm = generic_pipe_buf_confirm,
4975 .release = buffer_pipe_buf_release,
4976 .steal = generic_pipe_buf_steal,
4977 .get = buffer_pipe_buf_get,
4981 * Callback from splice_to_pipe(), if we need to release some pages
4982 * at the end of the spd in case we error'ed out in filling the pipe.
4984 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4986 struct buffer_ref *ref =
4987 (struct buffer_ref *)spd->partial[i].private;
4992 ring_buffer_free_read_page(ref->buffer, ref->page);
4994 spd->partial[i].private = 0;
4998 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4999 struct pipe_inode_info *pipe, size_t len,
5002 struct ftrace_buffer_info *info = file->private_data;
5003 struct trace_iterator *iter = &info->iter;
5004 struct partial_page partial_def[PIPE_DEF_BUFFERS];
5005 struct page *pages_def[PIPE_DEF_BUFFERS];
5006 struct splice_pipe_desc spd = {
5008 .partial = partial_def,
5009 .nr_pages_max = PIPE_DEF_BUFFERS,
5011 .ops = &buffer_pipe_buf_ops,
5012 .spd_release = buffer_spd_release,
5014 struct buffer_ref *ref;
5015 int entries, size, i;
5018 mutex_lock(&trace_types_lock);
5020 #ifdef CONFIG_TRACER_MAX_TRACE
5021 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5027 if (splice_grow_spd(pipe, &spd)) {
5032 if (*ppos & (PAGE_SIZE - 1)) {
5037 if (len & (PAGE_SIZE - 1)) {
5038 if (len < PAGE_SIZE) {
5046 trace_access_lock(iter->cpu_file);
5047 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5049 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
5053 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
5058 ref->buffer = iter->trace_buffer->buffer;
5059 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
5065 r = ring_buffer_read_page(ref->buffer, &ref->page,
5066 len, iter->cpu_file, 1);
5068 ring_buffer_free_read_page(ref->buffer, ref->page);
5074 * zero out any left over data, this is going to
5077 size = ring_buffer_page_len(ref->page);
5078 if (size < PAGE_SIZE)
5079 memset(ref->page + size, 0, PAGE_SIZE - size);
5081 page = virt_to_page(ref->page);
5083 spd.pages[i] = page;
5084 spd.partial[i].len = PAGE_SIZE;
5085 spd.partial[i].offset = 0;
5086 spd.partial[i].private = (unsigned long)ref;
5090 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5093 trace_access_unlock(iter->cpu_file);
5096 /* did we read anything? */
5097 if (!spd.nr_pages) {
5098 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
5102 mutex_unlock(&trace_types_lock);
5103 iter->trace->wait_pipe(iter);
5104 mutex_lock(&trace_types_lock);
5105 if (signal_pending(current)) {
5112 ret = splice_to_pipe(pipe, &spd);
5113 splice_shrink_spd(&spd);
5115 mutex_unlock(&trace_types_lock);
5120 static const struct file_operations tracing_buffers_fops = {
5121 .open = tracing_buffers_open,
5122 .read = tracing_buffers_read,
5123 .poll = tracing_buffers_poll,
5124 .release = tracing_buffers_release,
5125 .splice_read = tracing_buffers_splice_read,
5126 .llseek = no_llseek,
5130 tracing_stats_read(struct file *filp, char __user *ubuf,
5131 size_t count, loff_t *ppos)
5133 struct trace_cpu *tc = filp->private_data;
5134 struct trace_array *tr = tc->tr;
5135 struct trace_buffer *trace_buf = &tr->trace_buffer;
5136 struct trace_seq *s;
5138 unsigned long long t;
5139 unsigned long usec_rem;
5142 s = kmalloc(sizeof(*s), GFP_KERNEL);
5148 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
5149 trace_seq_printf(s, "entries: %ld\n", cnt);
5151 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
5152 trace_seq_printf(s, "overrun: %ld\n", cnt);
5154 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
5155 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5157 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
5158 trace_seq_printf(s, "bytes: %ld\n", cnt);
5160 if (trace_clocks[trace_clock_id].in_ns) {
5161 /* local or global for trace_clock */
5162 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5163 usec_rem = do_div(t, USEC_PER_SEC);
5164 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5167 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
5168 usec_rem = do_div(t, USEC_PER_SEC);
5169 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5171 /* counter or tsc mode for trace_clock */
5172 trace_seq_printf(s, "oldest event ts: %llu\n",
5173 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5175 trace_seq_printf(s, "now ts: %llu\n",
5176 ring_buffer_time_stamp(trace_buf->buffer, cpu));
5179 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
5180 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5182 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
5183 trace_seq_printf(s, "read events: %ld\n", cnt);
5185 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
5192 static const struct file_operations tracing_stats_fops = {
5193 .open = tracing_open_generic,
5194 .read = tracing_stats_read,
5195 .llseek = generic_file_llseek,
5198 #ifdef CONFIG_DYNAMIC_FTRACE
5200 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
5206 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
5207 size_t cnt, loff_t *ppos)
5209 static char ftrace_dyn_info_buffer[1024];
5210 static DEFINE_MUTEX(dyn_info_mutex);
5211 unsigned long *p = filp->private_data;
5212 char *buf = ftrace_dyn_info_buffer;
5213 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
5216 mutex_lock(&dyn_info_mutex);
5217 r = sprintf(buf, "%ld ", *p);
5219 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
5222 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5224 mutex_unlock(&dyn_info_mutex);
5229 static const struct file_operations tracing_dyn_info_fops = {
5230 .open = tracing_open_generic,
5231 .read = tracing_read_dyn_info,
5232 .llseek = generic_file_llseek,
5234 #endif /* CONFIG_DYNAMIC_FTRACE */
5236 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5238 ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5244 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5246 unsigned long *count = (long *)data;
5258 ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5259 struct ftrace_probe_ops *ops, void *data)
5261 long count = (long)data;
5263 seq_printf(m, "%ps:", (void *)ip);
5265 seq_printf(m, "snapshot");
5268 seq_printf(m, ":unlimited\n");
5270 seq_printf(m, ":count=%ld\n", count);
5275 static struct ftrace_probe_ops snapshot_probe_ops = {
5276 .func = ftrace_snapshot,
5277 .print = ftrace_snapshot_print,
5280 static struct ftrace_probe_ops snapshot_count_probe_ops = {
5281 .func = ftrace_count_snapshot,
5282 .print = ftrace_snapshot_print,
5286 ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5287 char *glob, char *cmd, char *param, int enable)
5289 struct ftrace_probe_ops *ops;
5290 void *count = (void *)-1;
5294 /* hash funcs only work with set_ftrace_filter */
5298 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
5300 if (glob[0] == '!') {
5301 unregister_ftrace_function_probe_func(glob+1, ops);
5308 number = strsep(¶m, ":");
5310 if (!strlen(number))
5314 * We use the callback data field (which is a pointer)
5317 ret = kstrtoul(number, 0, (unsigned long *)&count);
5322 ret = register_ftrace_function_probe(glob, ops, count);
5325 alloc_snapshot(&global_trace);
5327 return ret < 0 ? ret : 0;
5330 static struct ftrace_func_command ftrace_snapshot_cmd = {
5332 .func = ftrace_trace_snapshot_callback,
5335 static int register_snapshot_cmd(void)
5337 return register_ftrace_command(&ftrace_snapshot_cmd);
5340 static inline int register_snapshot_cmd(void) { return 0; }
5341 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
5343 struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
5348 if (!debugfs_initialized())
5351 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5352 tr->dir = debugfs_create_dir("tracing", NULL);
5355 pr_warn_once("Could not create debugfs directory 'tracing'\n");
5360 struct dentry *tracing_init_dentry(void)
5362 return tracing_init_dentry_tr(&global_trace);
5365 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
5367 struct dentry *d_tracer;
5370 return tr->percpu_dir;
5372 d_tracer = tracing_init_dentry_tr(tr);
5376 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
5378 WARN_ONCE(!tr->percpu_dir,
5379 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
5381 return tr->percpu_dir;
5385 tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
5387 struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
5388 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5389 struct dentry *d_cpu;
5390 char cpu_dir[30]; /* 30 characters should be more than enough */
5395 snprintf(cpu_dir, 30, "cpu%ld", cpu);
5396 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5398 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5402 /* per cpu trace_pipe */
5403 trace_create_file("trace_pipe", 0444, d_cpu,
5404 (void *)&data->trace_cpu, &tracing_pipe_fops);
5407 trace_create_file("trace", 0644, d_cpu,
5408 (void *)&data->trace_cpu, &tracing_fops);
5410 trace_create_file("trace_pipe_raw", 0444, d_cpu,
5411 (void *)&data->trace_cpu, &tracing_buffers_fops);
5413 trace_create_file("stats", 0444, d_cpu,
5414 (void *)&data->trace_cpu, &tracing_stats_fops);
5416 trace_create_file("buffer_size_kb", 0444, d_cpu,
5417 (void *)&data->trace_cpu, &tracing_entries_fops);
5419 #ifdef CONFIG_TRACER_SNAPSHOT
5420 trace_create_file("snapshot", 0644, d_cpu,
5421 (void *)&data->trace_cpu, &snapshot_fops);
5423 trace_create_file("snapshot_raw", 0444, d_cpu,
5424 (void *)&data->trace_cpu, &snapshot_raw_fops);
5428 #ifdef CONFIG_FTRACE_SELFTEST
5429 /* Let selftest have access to static functions in this file */
5430 #include "trace_selftest.c"
5433 struct trace_option_dentry {
5434 struct tracer_opt *opt;
5435 struct tracer_flags *flags;
5436 struct trace_array *tr;
5437 struct dentry *entry;
5441 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5444 struct trace_option_dentry *topt = filp->private_data;
5447 if (topt->flags->val & topt->opt->bit)
5452 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5456 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5459 struct trace_option_dentry *topt = filp->private_data;
5463 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5467 if (val != 0 && val != 1)
5470 if (!!(topt->flags->val & topt->opt->bit) != val) {
5471 mutex_lock(&trace_types_lock);
5472 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
5474 mutex_unlock(&trace_types_lock);
5485 static const struct file_operations trace_options_fops = {
5486 .open = tracing_open_generic,
5487 .read = trace_options_read,
5488 .write = trace_options_write,
5489 .llseek = generic_file_llseek,
5493 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5496 long index = (long)filp->private_data;
5499 if (trace_flags & (1 << index))
5504 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5508 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5511 struct trace_array *tr = &global_trace;
5512 long index = (long)filp->private_data;
5516 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5520 if (val != 0 && val != 1)
5523 mutex_lock(&trace_types_lock);
5524 ret = set_tracer_flag(tr, 1 << index, val);
5525 mutex_unlock(&trace_types_lock);
5535 static const struct file_operations trace_options_core_fops = {
5536 .open = tracing_open_generic,
5537 .read = trace_options_core_read,
5538 .write = trace_options_core_write,
5539 .llseek = generic_file_llseek,
5542 struct dentry *trace_create_file(const char *name,
5544 struct dentry *parent,
5546 const struct file_operations *fops)
5550 ret = debugfs_create_file(name, mode, parent, data, fops);
5552 pr_warning("Could not create debugfs '%s' entry\n", name);
5558 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
5560 struct dentry *d_tracer;
5565 d_tracer = tracing_init_dentry_tr(tr);
5569 tr->options = debugfs_create_dir("options", d_tracer);
5571 pr_warning("Could not create debugfs directory 'options'\n");
5579 create_trace_option_file(struct trace_array *tr,
5580 struct trace_option_dentry *topt,
5581 struct tracer_flags *flags,
5582 struct tracer_opt *opt)
5584 struct dentry *t_options;
5586 t_options = trace_options_init_dentry(tr);
5590 topt->flags = flags;
5594 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
5595 &trace_options_fops);
5599 static struct trace_option_dentry *
5600 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
5602 struct trace_option_dentry *topts;
5603 struct tracer_flags *flags;
5604 struct tracer_opt *opts;
5610 flags = tracer->flags;
5612 if (!flags || !flags->opts)
5617 for (cnt = 0; opts[cnt].name; cnt++)
5620 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
5624 for (cnt = 0; opts[cnt].name; cnt++)
5625 create_trace_option_file(tr, &topts[cnt], flags,
5632 destroy_trace_option_files(struct trace_option_dentry *topts)
5639 for (cnt = 0; topts[cnt].opt; cnt++) {
5640 if (topts[cnt].entry)
5641 debugfs_remove(topts[cnt].entry);
5647 static struct dentry *
5648 create_trace_option_core_file(struct trace_array *tr,
5649 const char *option, long index)
5651 struct dentry *t_options;
5653 t_options = trace_options_init_dentry(tr);
5657 return trace_create_file(option, 0644, t_options, (void *)index,
5658 &trace_options_core_fops);
5661 static __init void create_trace_options_dir(struct trace_array *tr)
5663 struct dentry *t_options;
5666 t_options = trace_options_init_dentry(tr);
5670 for (i = 0; trace_options[i]; i++)
5671 create_trace_option_core_file(tr, trace_options[i], i);
5675 rb_simple_read(struct file *filp, char __user *ubuf,
5676 size_t cnt, loff_t *ppos)
5678 struct trace_array *tr = filp->private_data;
5682 r = tracer_tracing_is_on(tr);
5683 r = sprintf(buf, "%d\n", r);
5685 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5689 rb_simple_write(struct file *filp, const char __user *ubuf,
5690 size_t cnt, loff_t *ppos)
5692 struct trace_array *tr = filp->private_data;
5693 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5697 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5702 mutex_lock(&trace_types_lock);
5704 tracer_tracing_on(tr);
5705 if (tr->current_trace->start)
5706 tr->current_trace->start(tr);
5708 tracer_tracing_off(tr);
5709 if (tr->current_trace->stop)
5710 tr->current_trace->stop(tr);
5712 mutex_unlock(&trace_types_lock);
5720 static const struct file_operations rb_simple_fops = {
5721 .open = tracing_open_generic,
5722 .read = rb_simple_read,
5723 .write = rb_simple_write,
5724 .llseek = default_llseek,
5727 struct dentry *trace_instance_dir;
5730 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5732 static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
5736 for_each_tracing_cpu(cpu) {
5737 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
5738 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
5739 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
5744 allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
5746 enum ring_buffer_flags rb_flags;
5748 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5750 buf->buffer = ring_buffer_alloc(size, rb_flags);
5754 buf->data = alloc_percpu(struct trace_array_cpu);
5756 ring_buffer_free(buf->buffer);
5760 init_trace_buffers(tr, buf);
5762 /* Allocate the first page for all buffers */
5763 set_buffer_entries(&tr->trace_buffer,
5764 ring_buffer_size(tr->trace_buffer.buffer, 0));
5769 static int allocate_trace_buffers(struct trace_array *tr, int size)
5773 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
5777 #ifdef CONFIG_TRACER_MAX_TRACE
5778 ret = allocate_trace_buffer(tr, &tr->max_buffer,
5779 allocate_snapshot ? size : 1);
5781 ring_buffer_free(tr->trace_buffer.buffer);
5782 free_percpu(tr->trace_buffer.data);
5785 tr->allocated_snapshot = allocate_snapshot;
5788 * Only the top level trace array gets its snapshot allocated
5789 * from the kernel command line.
5791 allocate_snapshot = false;
5796 static int new_instance_create(const char *name)
5798 struct trace_array *tr;
5801 mutex_lock(&trace_types_lock);
5804 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5805 if (tr->name && strcmp(tr->name, name) == 0)
5810 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
5814 tr->name = kstrdup(name, GFP_KERNEL);
5818 raw_spin_lock_init(&tr->start_lock);
5820 tr->current_trace = &nop_trace;
5822 INIT_LIST_HEAD(&tr->systems);
5823 INIT_LIST_HEAD(&tr->events);
5825 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
5828 /* Holder for file callbacks */
5829 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5830 tr->trace_cpu.tr = tr;
5832 tr->dir = debugfs_create_dir(name, trace_instance_dir);
5836 ret = event_trace_add_tracer(tr->dir, tr);
5840 init_tracer_debugfs(tr, tr->dir);
5842 list_add(&tr->list, &ftrace_trace_arrays);
5844 mutex_unlock(&trace_types_lock);
5849 if (tr->trace_buffer.buffer)
5850 ring_buffer_free(tr->trace_buffer.buffer);
5855 mutex_unlock(&trace_types_lock);
5861 static int instance_delete(const char *name)
5863 struct trace_array *tr;
5867 mutex_lock(&trace_types_lock);
5870 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5871 if (tr->name && strcmp(tr->name, name) == 0) {
5883 list_del(&tr->list);
5885 event_trace_del_tracer(tr);
5886 debugfs_remove_recursive(tr->dir);
5887 free_percpu(tr->trace_buffer.data);
5888 ring_buffer_free(tr->trace_buffer.buffer);
5896 mutex_unlock(&trace_types_lock);
5901 static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
5903 struct dentry *parent;
5906 /* Paranoid: Make sure the parent is the "instances" directory */
5907 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5908 if (WARN_ON_ONCE(parent != trace_instance_dir))
5912 * The inode mutex is locked, but debugfs_create_dir() will also
5913 * take the mutex. As the instances directory can not be destroyed
5914 * or changed in any other way, it is safe to unlock it, and
5915 * let the dentry try. If two users try to make the same dir at
5916 * the same time, then the new_instance_create() will determine the
5919 mutex_unlock(&inode->i_mutex);
5921 ret = new_instance_create(dentry->d_iname);
5923 mutex_lock(&inode->i_mutex);
5928 static int instance_rmdir(struct inode *inode, struct dentry *dentry)
5930 struct dentry *parent;
5933 /* Paranoid: Make sure the parent is the "instances" directory */
5934 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5935 if (WARN_ON_ONCE(parent != trace_instance_dir))
5938 /* The caller did a dget() on dentry */
5939 mutex_unlock(&dentry->d_inode->i_mutex);
5942 * The inode mutex is locked, but debugfs_create_dir() will also
5943 * take the mutex. As the instances directory can not be destroyed
5944 * or changed in any other way, it is safe to unlock it, and
5945 * let the dentry try. If two users try to make the same dir at
5946 * the same time, then the instance_delete() will determine the
5949 mutex_unlock(&inode->i_mutex);
5951 ret = instance_delete(dentry->d_iname);
5953 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
5954 mutex_lock(&dentry->d_inode->i_mutex);
5959 static const struct inode_operations instance_dir_inode_operations = {
5960 .lookup = simple_lookup,
5961 .mkdir = instance_mkdir,
5962 .rmdir = instance_rmdir,
5965 static __init void create_trace_instances(struct dentry *d_tracer)
5967 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
5968 if (WARN_ON(!trace_instance_dir))
5971 /* Hijack the dir inode operations, to allow mkdir */
5972 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
5976 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
5980 trace_create_file("trace_options", 0644, d_tracer,
5981 tr, &tracing_iter_fops);
5983 trace_create_file("trace", 0644, d_tracer,
5984 (void *)&tr->trace_cpu, &tracing_fops);
5986 trace_create_file("trace_pipe", 0444, d_tracer,
5987 (void *)&tr->trace_cpu, &tracing_pipe_fops);
5989 trace_create_file("buffer_size_kb", 0644, d_tracer,
5990 (void *)&tr->trace_cpu, &tracing_entries_fops);
5992 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
5993 tr, &tracing_total_entries_fops);
5995 trace_create_file("free_buffer", 0200, d_tracer,
5996 tr, &tracing_free_buffer_fops);
5998 trace_create_file("trace_marker", 0220, d_tracer,
5999 tr, &tracing_mark_fops);
6001 trace_create_file("trace_clock", 0644, d_tracer, tr,
6004 trace_create_file("tracing_on", 0644, d_tracer,
6005 tr, &rb_simple_fops);
6007 #ifdef CONFIG_TRACER_SNAPSHOT
6008 trace_create_file("snapshot", 0644, d_tracer,
6009 (void *)&tr->trace_cpu, &snapshot_fops);
6012 for_each_tracing_cpu(cpu)
6013 tracing_init_debugfs_percpu(tr, cpu);
6017 static __init int tracer_init_debugfs(void)
6019 struct dentry *d_tracer;
6021 trace_access_lock_init();
6023 d_tracer = tracing_init_dentry();
6027 init_tracer_debugfs(&global_trace, d_tracer);
6029 trace_create_file("tracing_cpumask", 0644, d_tracer,
6030 &global_trace, &tracing_cpumask_fops);
6032 trace_create_file("available_tracers", 0444, d_tracer,
6033 &global_trace, &show_traces_fops);
6035 trace_create_file("current_tracer", 0644, d_tracer,
6036 &global_trace, &set_tracer_fops);
6038 #ifdef CONFIG_TRACER_MAX_TRACE
6039 trace_create_file("tracing_max_latency", 0644, d_tracer,
6040 &tracing_max_latency, &tracing_max_lat_fops);
6043 trace_create_file("tracing_thresh", 0644, d_tracer,
6044 &tracing_thresh, &tracing_max_lat_fops);
6046 trace_create_file("README", 0444, d_tracer,
6047 NULL, &tracing_readme_fops);
6049 trace_create_file("saved_cmdlines", 0444, d_tracer,
6050 NULL, &tracing_saved_cmdlines_fops);
6052 #ifdef CONFIG_DYNAMIC_FTRACE
6053 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
6054 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
6057 create_trace_instances(d_tracer);
6059 create_trace_options_dir(&global_trace);
6064 static int trace_panic_handler(struct notifier_block *this,
6065 unsigned long event, void *unused)
6067 if (ftrace_dump_on_oops)
6068 ftrace_dump(ftrace_dump_on_oops);
6072 static struct notifier_block trace_panic_notifier = {
6073 .notifier_call = trace_panic_handler,
6075 .priority = 150 /* priority: INT_MAX >= x >= 0 */
6078 static int trace_die_handler(struct notifier_block *self,
6084 if (ftrace_dump_on_oops)
6085 ftrace_dump(ftrace_dump_on_oops);
6093 static struct notifier_block trace_die_notifier = {
6094 .notifier_call = trace_die_handler,
6099 * printk is set to max of 1024, we really don't need it that big.
6100 * Nothing should be printing 1000 characters anyway.
6102 #define TRACE_MAX_PRINT 1000
6105 * Define here KERN_TRACE so that we have one place to modify
6106 * it if we decide to change what log level the ftrace dump
6109 #define KERN_TRACE KERN_EMERG
6112 trace_printk_seq(struct trace_seq *s)
6114 /* Probably should print a warning here. */
6115 if (s->len >= TRACE_MAX_PRINT)
6116 s->len = TRACE_MAX_PRINT;
6118 /* should be zero ended, but we are paranoid. */
6119 s->buffer[s->len] = 0;
6121 printk(KERN_TRACE "%s", s->buffer);
6126 void trace_init_global_iter(struct trace_iterator *iter)
6128 iter->tr = &global_trace;
6129 iter->trace = iter->tr->current_trace;
6130 iter->cpu_file = RING_BUFFER_ALL_CPUS;
6131 iter->trace_buffer = &global_trace.trace_buffer;
6134 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
6136 /* use static because iter can be a bit big for the stack */
6137 static struct trace_iterator iter;
6138 static atomic_t dump_running;
6139 unsigned int old_userobj;
6140 unsigned long flags;
6143 /* Only allow one dump user at a time. */
6144 if (atomic_inc_return(&dump_running) != 1) {
6145 atomic_dec(&dump_running);
6150 * Always turn off tracing when we dump.
6151 * We don't need to show trace output of what happens
6152 * between multiple crashes.
6154 * If the user does a sysrq-z, then they can re-enable
6155 * tracing with echo 1 > tracing_on.
6159 local_irq_save(flags);
6161 /* Simulate the iterator */
6162 trace_init_global_iter(&iter);
6164 for_each_tracing_cpu(cpu) {
6165 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
6168 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
6170 /* don't look at user memory in panic mode */
6171 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
6173 switch (oops_dump_mode) {
6175 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6178 iter.cpu_file = raw_smp_processor_id();
6183 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
6184 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6187 printk(KERN_TRACE "Dumping ftrace buffer:\n");
6189 /* Did function tracer already get disabled? */
6190 if (ftrace_is_dead()) {
6191 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
6192 printk("# MAY BE MISSING FUNCTION EVENTS\n");
6196 * We need to stop all tracing on all CPUS to read the
6197 * the next buffer. This is a bit expensive, but is
6198 * not done often. We fill all what we can read,
6199 * and then release the locks again.
6202 while (!trace_empty(&iter)) {
6205 printk(KERN_TRACE "---------------------------------\n");
6209 /* reset all but tr, trace, and overruns */
6210 memset(&iter.seq, 0,
6211 sizeof(struct trace_iterator) -
6212 offsetof(struct trace_iterator, seq));
6213 iter.iter_flags |= TRACE_FILE_LAT_FMT;
6216 if (trace_find_next_entry_inc(&iter) != NULL) {
6219 ret = print_trace_line(&iter);
6220 if (ret != TRACE_TYPE_NO_CONSUME)
6221 trace_consume(&iter);
6223 touch_nmi_watchdog();
6225 trace_printk_seq(&iter.seq);
6229 printk(KERN_TRACE " (ftrace buffer empty)\n");
6231 printk(KERN_TRACE "---------------------------------\n");
6234 trace_flags |= old_userobj;
6236 for_each_tracing_cpu(cpu) {
6237 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
6239 atomic_dec(&dump_running);
6240 local_irq_restore(flags);
6242 EXPORT_SYMBOL_GPL(ftrace_dump);
6244 __init static int tracer_alloc_buffers(void)
6250 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
6253 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
6254 goto out_free_buffer_mask;
6256 /* Only allocate trace_printk buffers if a trace_printk exists */
6257 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
6258 /* Must be called before global_trace.buffer is allocated */
6259 trace_printk_init_buffers();
6261 /* To save memory, keep the ring buffer size to its minimum */
6262 if (ring_buffer_expanded)
6263 ring_buf_size = trace_buf_size;
6267 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
6268 cpumask_copy(tracing_cpumask, cpu_all_mask);
6270 raw_spin_lock_init(&global_trace.start_lock);
6272 /* TODO: make the number of buffers hot pluggable with CPUS */
6273 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
6274 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
6276 goto out_free_cpumask;
6279 if (global_trace.buffer_disabled)
6282 trace_init_cmdlines();
6285 * register_tracer() might reference current_trace, so it
6286 * needs to be set before we register anything. This is
6287 * just a bootstrap of current_trace anyway.
6289 global_trace.current_trace = &nop_trace;
6291 register_tracer(&nop_trace);
6293 /* All seems OK, enable tracing */
6294 tracing_disabled = 0;
6296 atomic_notifier_chain_register(&panic_notifier_list,
6297 &trace_panic_notifier);
6299 register_die_notifier(&trace_die_notifier);
6301 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
6303 /* Holder for file callbacks */
6304 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
6305 global_trace.trace_cpu.tr = &global_trace;
6307 INIT_LIST_HEAD(&global_trace.systems);
6308 INIT_LIST_HEAD(&global_trace.events);
6309 list_add(&global_trace.list, &ftrace_trace_arrays);
6311 while (trace_boot_options) {
6314 option = strsep(&trace_boot_options, ",");
6315 trace_set_options(&global_trace, option);
6318 register_snapshot_cmd();
6323 free_percpu(global_trace.trace_buffer.data);
6324 #ifdef CONFIG_TRACER_MAX_TRACE
6325 free_percpu(global_trace.max_buffer.data);
6327 free_cpumask_var(tracing_cpumask);
6328 out_free_buffer_mask:
6329 free_cpumask_var(tracing_buffer_mask);
6334 __init static int clear_boot_tracer(void)
6337 * The default tracer at boot buffer is an init section.
6338 * This function is called in lateinit. If we did not
6339 * find the boot tracer, then clear it out, to prevent
6340 * later registration from accessing the buffer that is
6341 * about to be freed.
6343 if (!default_bootup_tracer)
6346 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6347 default_bootup_tracer);
6348 default_bootup_tracer = NULL;
6353 early_initcall(tracer_alloc_buffers);
6354 fs_initcall(tracer_init_debugfs);
6355 late_initcall(clear_boot_tracer);