Linux 4.1.18
[linux/fpc-iii.git] / kernel / trace / trace.c
blob05330494a0dffde9824ab8659358e55a789ba286
1 /*
2 * ring buffer based function tracer
4 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 Nadia Yvette Chambers
14 #include <linux/ring_buffer.h>
15 #include <generated/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/tracefs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/kprobes.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/mount.h>
36 #include <linux/rwsem.h>
37 #include <linux/slab.h>
38 #include <linux/ctype.h>
39 #include <linux/init.h>
40 #include <linux/poll.h>
41 #include <linux/nmi.h>
42 #include <linux/fs.h>
43 #include <linux/sched/rt.h>
45 #include "trace.h"
46 #include "trace_output.h"
49 * On boot up, the ring buffer is set to the minimum size, so that
50 * we do not waste memory on systems that are not using tracing.
52 bool ring_buffer_expanded;
55 * We need to change this state when a selftest is running.
56 * A selftest will lurk into the ring-buffer to count the
57 * entries inserted during the selftest although some concurrent
58 * insertions into the ring-buffer such as trace_printk could occurred
59 * at the same time, giving false positive or negative results.
61 static bool __read_mostly tracing_selftest_running;
64 * If a tracer is running, we do not want to run SELFTEST.
66 bool __read_mostly tracing_selftest_disabled;
68 /* Pipe tracepoints to printk */
69 struct trace_iterator *tracepoint_print_iter;
70 int tracepoint_printk;
72 /* For tracers that don't implement custom flags */
73 static struct tracer_opt dummy_tracer_opt[] = {
74 { }
77 static struct tracer_flags dummy_tracer_flags = {
78 .val = 0,
79 .opts = dummy_tracer_opt
82 static int
83 dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
85 return 0;
89 * To prevent the comm cache from being overwritten when no
90 * tracing is active, only save the comm when a trace event
91 * occurred.
93 static DEFINE_PER_CPU(bool, trace_cmdline_save);
96 * Kill all tracing for good (never come back).
97 * It is initialized to 1 but will turn to zero if the initialization
98 * of the tracer is successful. But that is the only place that sets
99 * this back to zero.
101 static int tracing_disabled = 1;
103 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
105 cpumask_var_t __read_mostly tracing_buffer_mask;
108 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
110 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
111 * is set, then ftrace_dump is called. This will output the contents
112 * of the ftrace buffers to the console. This is very useful for
113 * capturing traces that lead to crashes and outputing it to a
114 * serial console.
116 * It is default off, but you can enable it with either specifying
117 * "ftrace_dump_on_oops" in the kernel command line, or setting
118 * /proc/sys/kernel/ftrace_dump_on_oops
119 * Set 1 if you want to dump buffers of all CPUs
120 * Set 2 if you want to dump the buffer of the CPU that triggered oops
123 enum ftrace_dump_mode ftrace_dump_on_oops;
125 /* When set, tracing will stop when a WARN*() is hit */
126 int __disable_trace_on_warning;
128 #ifdef CONFIG_TRACE_ENUM_MAP_FILE
129 /* Map of enums to their values, for "enum_map" file */
130 struct trace_enum_map_head {
131 struct module *mod;
132 unsigned long length;
135 union trace_enum_map_item;
137 struct trace_enum_map_tail {
139 * "end" is first and points to NULL as it must be different
140 * than "mod" or "enum_string"
142 union trace_enum_map_item *next;
143 const char *end; /* points to NULL */
146 static DEFINE_MUTEX(trace_enum_mutex);
149 * The trace_enum_maps are saved in an array with two extra elements,
150 * one at the beginning, and one at the end. The beginning item contains
151 * the count of the saved maps (head.length), and the module they
152 * belong to if not built in (head.mod). The ending item contains a
153 * pointer to the next array of saved enum_map items.
155 union trace_enum_map_item {
156 struct trace_enum_map map;
157 struct trace_enum_map_head head;
158 struct trace_enum_map_tail tail;
161 static union trace_enum_map_item *trace_enum_maps;
162 #endif /* CONFIG_TRACE_ENUM_MAP_FILE */
164 static int tracing_set_tracer(struct trace_array *tr, const char *buf);
166 #define MAX_TRACER_SIZE 100
167 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
168 static char *default_bootup_tracer;
170 static bool allocate_snapshot;
172 static int __init set_cmdline_ftrace(char *str)
174 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
175 default_bootup_tracer = bootup_tracer_buf;
176 /* We are using ftrace early, expand it */
177 ring_buffer_expanded = true;
178 return 1;
180 __setup("ftrace=", set_cmdline_ftrace);
182 static int __init set_ftrace_dump_on_oops(char *str)
184 if (*str++ != '=' || !*str) {
185 ftrace_dump_on_oops = DUMP_ALL;
186 return 1;
189 if (!strcmp("orig_cpu", str)) {
190 ftrace_dump_on_oops = DUMP_ORIG;
191 return 1;
194 return 0;
196 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
198 static int __init stop_trace_on_warning(char *str)
200 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
201 __disable_trace_on_warning = 1;
202 return 1;
204 __setup("traceoff_on_warning", stop_trace_on_warning);
206 static int __init boot_alloc_snapshot(char *str)
208 allocate_snapshot = true;
209 /* We also need the main ring buffer expanded */
210 ring_buffer_expanded = true;
211 return 1;
213 __setup("alloc_snapshot", boot_alloc_snapshot);
216 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
217 static char *trace_boot_options __initdata;
219 static int __init set_trace_boot_options(char *str)
221 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
222 trace_boot_options = trace_boot_options_buf;
223 return 0;
225 __setup("trace_options=", set_trace_boot_options);
227 static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
228 static char *trace_boot_clock __initdata;
230 static int __init set_trace_boot_clock(char *str)
232 strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
233 trace_boot_clock = trace_boot_clock_buf;
234 return 0;
236 __setup("trace_clock=", set_trace_boot_clock);
238 static int __init set_tracepoint_printk(char *str)
240 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
241 tracepoint_printk = 1;
242 return 1;
244 __setup("tp_printk", set_tracepoint_printk);
246 unsigned long long ns2usecs(cycle_t nsec)
248 nsec += 500;
249 do_div(nsec, 1000);
250 return nsec;
254 * The global_trace is the descriptor that holds the tracing
255 * buffers for the live tracing. For each CPU, it contains
256 * a link list of pages that will store trace entries. The
257 * page descriptor of the pages in the memory is used to hold
258 * the link list by linking the lru item in the page descriptor
259 * to each of the pages in the buffer per CPU.
261 * For each active CPU there is a data field that holds the
262 * pages for the buffer for that CPU. Each CPU has the same number
263 * of pages allocated for its buffer.
265 static struct trace_array global_trace;
267 LIST_HEAD(ftrace_trace_arrays);
269 int trace_array_get(struct trace_array *this_tr)
271 struct trace_array *tr;
272 int ret = -ENODEV;
274 mutex_lock(&trace_types_lock);
275 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
276 if (tr == this_tr) {
277 tr->ref++;
278 ret = 0;
279 break;
282 mutex_unlock(&trace_types_lock);
284 return ret;
287 static void __trace_array_put(struct trace_array *this_tr)
289 WARN_ON(!this_tr->ref);
290 this_tr->ref--;
293 void trace_array_put(struct trace_array *this_tr)
295 mutex_lock(&trace_types_lock);
296 __trace_array_put(this_tr);
297 mutex_unlock(&trace_types_lock);
300 int filter_check_discard(struct ftrace_event_file *file, void *rec,
301 struct ring_buffer *buffer,
302 struct ring_buffer_event *event)
304 if (unlikely(file->flags & FTRACE_EVENT_FL_FILTERED) &&
305 !filter_match_preds(file->filter, rec)) {
306 ring_buffer_discard_commit(buffer, event);
307 return 1;
310 return 0;
312 EXPORT_SYMBOL_GPL(filter_check_discard);
314 int call_filter_check_discard(struct ftrace_event_call *call, void *rec,
315 struct ring_buffer *buffer,
316 struct ring_buffer_event *event)
318 if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
319 !filter_match_preds(call->filter, rec)) {
320 ring_buffer_discard_commit(buffer, event);
321 return 1;
324 return 0;
326 EXPORT_SYMBOL_GPL(call_filter_check_discard);
328 static cycle_t buffer_ftrace_now(struct trace_buffer *buf, int cpu)
330 u64 ts;
332 /* Early boot up does not have a buffer yet */
333 if (!buf->buffer)
334 return trace_clock_local();
336 ts = ring_buffer_time_stamp(buf->buffer, cpu);
337 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
339 return ts;
342 cycle_t ftrace_now(int cpu)
344 return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
348 * tracing_is_enabled - Show if global_trace has been disabled
350 * Shows if the global trace has been enabled or not. It uses the
351 * mirror flag "buffer_disabled" to be used in fast paths such as for
352 * the irqsoff tracer. But it may be inaccurate due to races. If you
353 * need to know the accurate state, use tracing_is_on() which is a little
354 * slower, but accurate.
356 int tracing_is_enabled(void)
359 * For quick access (irqsoff uses this in fast path), just
360 * return the mirror variable of the state of the ring buffer.
361 * It's a little racy, but we don't really care.
363 smp_rmb();
364 return !global_trace.buffer_disabled;
368 * trace_buf_size is the size in bytes that is allocated
369 * for a buffer. Note, the number of bytes is always rounded
370 * to page size.
372 * This number is purposely set to a low number of 16384.
373 * If the dump on oops happens, it will be much appreciated
374 * to not have to wait for all that output. Anyway this can be
375 * boot time and run time configurable.
377 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
379 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
381 /* trace_types holds a link list of available tracers. */
382 static struct tracer *trace_types __read_mostly;
385 * trace_types_lock is used to protect the trace_types list.
387 DEFINE_MUTEX(trace_types_lock);
390 * serialize the access of the ring buffer
392 * ring buffer serializes readers, but it is low level protection.
393 * The validity of the events (which returns by ring_buffer_peek() ..etc)
394 * are not protected by ring buffer.
396 * The content of events may become garbage if we allow other process consumes
397 * these events concurrently:
398 * A) the page of the consumed events may become a normal page
399 * (not reader page) in ring buffer, and this page will be rewrited
400 * by events producer.
401 * B) The page of the consumed events may become a page for splice_read,
402 * and this page will be returned to system.
404 * These primitives allow multi process access to different cpu ring buffer
405 * concurrently.
407 * These primitives don't distinguish read-only and read-consume access.
408 * Multi read-only access are also serialized.
411 #ifdef CONFIG_SMP
412 static DECLARE_RWSEM(all_cpu_access_lock);
413 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
415 static inline void trace_access_lock(int cpu)
417 if (cpu == RING_BUFFER_ALL_CPUS) {
418 /* gain it for accessing the whole ring buffer. */
419 down_write(&all_cpu_access_lock);
420 } else {
421 /* gain it for accessing a cpu ring buffer. */
423 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
424 down_read(&all_cpu_access_lock);
426 /* Secondly block other access to this @cpu ring buffer. */
427 mutex_lock(&per_cpu(cpu_access_lock, cpu));
431 static inline void trace_access_unlock(int cpu)
433 if (cpu == RING_BUFFER_ALL_CPUS) {
434 up_write(&all_cpu_access_lock);
435 } else {
436 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
437 up_read(&all_cpu_access_lock);
441 static inline void trace_access_lock_init(void)
443 int cpu;
445 for_each_possible_cpu(cpu)
446 mutex_init(&per_cpu(cpu_access_lock, cpu));
449 #else
451 static DEFINE_MUTEX(access_lock);
453 static inline void trace_access_lock(int cpu)
455 (void)cpu;
456 mutex_lock(&access_lock);
459 static inline void trace_access_unlock(int cpu)
461 (void)cpu;
462 mutex_unlock(&access_lock);
465 static inline void trace_access_lock_init(void)
469 #endif
471 /* trace_flags holds trace_options default values */
472 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
473 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
474 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
475 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
477 static void tracer_tracing_on(struct trace_array *tr)
479 if (tr->trace_buffer.buffer)
480 ring_buffer_record_on(tr->trace_buffer.buffer);
482 * This flag is looked at when buffers haven't been allocated
483 * yet, or by some tracers (like irqsoff), that just want to
484 * know if the ring buffer has been disabled, but it can handle
485 * races of where it gets disabled but we still do a record.
486 * As the check is in the fast path of the tracers, it is more
487 * important to be fast than accurate.
489 tr->buffer_disabled = 0;
490 /* Make the flag seen by readers */
491 smp_wmb();
495 * tracing_on - enable tracing buffers
497 * This function enables tracing buffers that may have been
498 * disabled with tracing_off.
500 void tracing_on(void)
502 tracer_tracing_on(&global_trace);
504 EXPORT_SYMBOL_GPL(tracing_on);
507 * __trace_puts - write a constant string into the trace buffer.
508 * @ip: The address of the caller
509 * @str: The constant string to write
510 * @size: The size of the string.
512 int __trace_puts(unsigned long ip, const char *str, int size)
514 struct ring_buffer_event *event;
515 struct ring_buffer *buffer;
516 struct print_entry *entry;
517 unsigned long irq_flags;
518 int alloc;
519 int pc;
521 if (!(trace_flags & TRACE_ITER_PRINTK))
522 return 0;
524 pc = preempt_count();
526 if (unlikely(tracing_selftest_running || tracing_disabled))
527 return 0;
529 alloc = sizeof(*entry) + size + 2; /* possible \n added */
531 local_save_flags(irq_flags);
532 buffer = global_trace.trace_buffer.buffer;
533 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
534 irq_flags, pc);
535 if (!event)
536 return 0;
538 entry = ring_buffer_event_data(event);
539 entry->ip = ip;
541 memcpy(&entry->buf, str, size);
543 /* Add a newline if necessary */
544 if (entry->buf[size - 1] != '\n') {
545 entry->buf[size] = '\n';
546 entry->buf[size + 1] = '\0';
547 } else
548 entry->buf[size] = '\0';
550 __buffer_unlock_commit(buffer, event);
551 ftrace_trace_stack(buffer, irq_flags, 4, pc);
553 return size;
555 EXPORT_SYMBOL_GPL(__trace_puts);
558 * __trace_bputs - write the pointer to a constant string into trace buffer
559 * @ip: The address of the caller
560 * @str: The constant string to write to the buffer to
562 int __trace_bputs(unsigned long ip, const char *str)
564 struct ring_buffer_event *event;
565 struct ring_buffer *buffer;
566 struct bputs_entry *entry;
567 unsigned long irq_flags;
568 int size = sizeof(struct bputs_entry);
569 int pc;
571 if (!(trace_flags & TRACE_ITER_PRINTK))
572 return 0;
574 pc = preempt_count();
576 if (unlikely(tracing_selftest_running || tracing_disabled))
577 return 0;
579 local_save_flags(irq_flags);
580 buffer = global_trace.trace_buffer.buffer;
581 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
582 irq_flags, pc);
583 if (!event)
584 return 0;
586 entry = ring_buffer_event_data(event);
587 entry->ip = ip;
588 entry->str = str;
590 __buffer_unlock_commit(buffer, event);
591 ftrace_trace_stack(buffer, irq_flags, 4, pc);
593 return 1;
595 EXPORT_SYMBOL_GPL(__trace_bputs);
597 #ifdef CONFIG_TRACER_SNAPSHOT
599 * trace_snapshot - take a snapshot of the current buffer.
601 * This causes a swap between the snapshot buffer and the current live
602 * tracing buffer. You can use this to take snapshots of the live
603 * trace when some condition is triggered, but continue to trace.
605 * Note, make sure to allocate the snapshot with either
606 * a tracing_snapshot_alloc(), or by doing it manually
607 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
609 * If the snapshot buffer is not allocated, it will stop tracing.
610 * Basically making a permanent snapshot.
612 void tracing_snapshot(void)
614 struct trace_array *tr = &global_trace;
615 struct tracer *tracer = tr->current_trace;
616 unsigned long flags;
618 if (in_nmi()) {
619 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
620 internal_trace_puts("*** snapshot is being ignored ***\n");
621 return;
624 if (!tr->allocated_snapshot) {
625 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
626 internal_trace_puts("*** stopping trace here! ***\n");
627 tracing_off();
628 return;
631 /* Note, snapshot can not be used when the tracer uses it */
632 if (tracer->use_max_tr) {
633 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
634 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
635 return;
638 local_irq_save(flags);
639 update_max_tr(tr, current, smp_processor_id());
640 local_irq_restore(flags);
642 EXPORT_SYMBOL_GPL(tracing_snapshot);
644 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
645 struct trace_buffer *size_buf, int cpu_id);
646 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
648 static int alloc_snapshot(struct trace_array *tr)
650 int ret;
652 if (!tr->allocated_snapshot) {
654 /* allocate spare buffer */
655 ret = resize_buffer_duplicate_size(&tr->max_buffer,
656 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
657 if (ret < 0)
658 return ret;
660 tr->allocated_snapshot = true;
663 return 0;
666 static void free_snapshot(struct trace_array *tr)
669 * We don't free the ring buffer. instead, resize it because
670 * The max_tr ring buffer has some state (e.g. ring->clock) and
671 * we want preserve it.
673 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
674 set_buffer_entries(&tr->max_buffer, 1);
675 tracing_reset_online_cpus(&tr->max_buffer);
676 tr->allocated_snapshot = false;
680 * tracing_alloc_snapshot - allocate snapshot buffer.
682 * This only allocates the snapshot buffer if it isn't already
683 * allocated - it doesn't also take a snapshot.
685 * This is meant to be used in cases where the snapshot buffer needs
686 * to be set up for events that can't sleep but need to be able to
687 * trigger a snapshot.
689 int tracing_alloc_snapshot(void)
691 struct trace_array *tr = &global_trace;
692 int ret;
694 ret = alloc_snapshot(tr);
695 WARN_ON(ret < 0);
697 return ret;
699 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
702 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
704 * This is similar to trace_snapshot(), but it will allocate the
705 * snapshot buffer if it isn't already allocated. Use this only
706 * where it is safe to sleep, as the allocation may sleep.
708 * This causes a swap between the snapshot buffer and the current live
709 * tracing buffer. You can use this to take snapshots of the live
710 * trace when some condition is triggered, but continue to trace.
712 void tracing_snapshot_alloc(void)
714 int ret;
716 ret = tracing_alloc_snapshot();
717 if (ret < 0)
718 return;
720 tracing_snapshot();
722 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
723 #else
724 void tracing_snapshot(void)
726 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
728 EXPORT_SYMBOL_GPL(tracing_snapshot);
729 int tracing_alloc_snapshot(void)
731 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
732 return -ENODEV;
734 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
735 void tracing_snapshot_alloc(void)
737 /* Give warning */
738 tracing_snapshot();
740 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
741 #endif /* CONFIG_TRACER_SNAPSHOT */
743 static void tracer_tracing_off(struct trace_array *tr)
745 if (tr->trace_buffer.buffer)
746 ring_buffer_record_off(tr->trace_buffer.buffer);
748 * This flag is looked at when buffers haven't been allocated
749 * yet, or by some tracers (like irqsoff), that just want to
750 * know if the ring buffer has been disabled, but it can handle
751 * races of where it gets disabled but we still do a record.
752 * As the check is in the fast path of the tracers, it is more
753 * important to be fast than accurate.
755 tr->buffer_disabled = 1;
756 /* Make the flag seen by readers */
757 smp_wmb();
761 * tracing_off - turn off tracing buffers
763 * This function stops the tracing buffers from recording data.
764 * It does not disable any overhead the tracers themselves may
765 * be causing. This function simply causes all recording to
766 * the ring buffers to fail.
768 void tracing_off(void)
770 tracer_tracing_off(&global_trace);
772 EXPORT_SYMBOL_GPL(tracing_off);
774 void disable_trace_on_warning(void)
776 if (__disable_trace_on_warning)
777 tracing_off();
781 * tracer_tracing_is_on - show real state of ring buffer enabled
782 * @tr : the trace array to know if ring buffer is enabled
784 * Shows real state of the ring buffer if it is enabled or not.
786 static int tracer_tracing_is_on(struct trace_array *tr)
788 if (tr->trace_buffer.buffer)
789 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
790 return !tr->buffer_disabled;
794 * tracing_is_on - show state of ring buffers enabled
796 int tracing_is_on(void)
798 return tracer_tracing_is_on(&global_trace);
800 EXPORT_SYMBOL_GPL(tracing_is_on);
802 static int __init set_buf_size(char *str)
804 unsigned long buf_size;
806 if (!str)
807 return 0;
808 buf_size = memparse(str, &str);
809 /* nr_entries can not be zero */
810 if (buf_size == 0)
811 return 0;
812 trace_buf_size = buf_size;
813 return 1;
815 __setup("trace_buf_size=", set_buf_size);
817 static int __init set_tracing_thresh(char *str)
819 unsigned long threshold;
820 int ret;
822 if (!str)
823 return 0;
824 ret = kstrtoul(str, 0, &threshold);
825 if (ret < 0)
826 return 0;
827 tracing_thresh = threshold * 1000;
828 return 1;
830 __setup("tracing_thresh=", set_tracing_thresh);
832 unsigned long nsecs_to_usecs(unsigned long nsecs)
834 return nsecs / 1000;
837 /* These must match the bit postions in trace_iterator_flags */
838 static const char *trace_options[] = {
839 "print-parent",
840 "sym-offset",
841 "sym-addr",
842 "verbose",
843 "raw",
844 "hex",
845 "bin",
846 "block",
847 "stacktrace",
848 "trace_printk",
849 "ftrace_preempt",
850 "branch",
851 "annotate",
852 "userstacktrace",
853 "sym-userobj",
854 "printk-msg-only",
855 "context-info",
856 "latency-format",
857 "sleep-time",
858 "graph-time",
859 "record-cmd",
860 "overwrite",
861 "disable_on_free",
862 "irq-info",
863 "markers",
864 "function-trace",
865 NULL
868 static struct {
869 u64 (*func)(void);
870 const char *name;
871 int in_ns; /* is this clock in nanoseconds? */
872 } trace_clocks[] = {
873 { trace_clock_local, "local", 1 },
874 { trace_clock_global, "global", 1 },
875 { trace_clock_counter, "counter", 0 },
876 { trace_clock_jiffies, "uptime", 0 },
877 { trace_clock, "perf", 1 },
878 { ktime_get_mono_fast_ns, "mono", 1 },
879 ARCH_TRACE_CLOCKS
883 * trace_parser_get_init - gets the buffer for trace parser
885 int trace_parser_get_init(struct trace_parser *parser, int size)
887 memset(parser, 0, sizeof(*parser));
889 parser->buffer = kmalloc(size, GFP_KERNEL);
890 if (!parser->buffer)
891 return 1;
893 parser->size = size;
894 return 0;
898 * trace_parser_put - frees the buffer for trace parser
900 void trace_parser_put(struct trace_parser *parser)
902 kfree(parser->buffer);
906 * trace_get_user - reads the user input string separated by space
907 * (matched by isspace(ch))
909 * For each string found the 'struct trace_parser' is updated,
910 * and the function returns.
912 * Returns number of bytes read.
914 * See kernel/trace/trace.h for 'struct trace_parser' details.
916 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
917 size_t cnt, loff_t *ppos)
919 char ch;
920 size_t read = 0;
921 ssize_t ret;
923 if (!*ppos)
924 trace_parser_clear(parser);
926 ret = get_user(ch, ubuf++);
927 if (ret)
928 goto out;
930 read++;
931 cnt--;
934 * The parser is not finished with the last write,
935 * continue reading the user input without skipping spaces.
937 if (!parser->cont) {
938 /* skip white space */
939 while (cnt && isspace(ch)) {
940 ret = get_user(ch, ubuf++);
941 if (ret)
942 goto out;
943 read++;
944 cnt--;
947 /* only spaces were written */
948 if (isspace(ch)) {
949 *ppos += read;
950 ret = read;
951 goto out;
954 parser->idx = 0;
957 /* read the non-space input */
958 while (cnt && !isspace(ch)) {
959 if (parser->idx < parser->size - 1)
960 parser->buffer[parser->idx++] = ch;
961 else {
962 ret = -EINVAL;
963 goto out;
965 ret = get_user(ch, ubuf++);
966 if (ret)
967 goto out;
968 read++;
969 cnt--;
972 /* We either got finished input or we have to wait for another call. */
973 if (isspace(ch)) {
974 parser->buffer[parser->idx] = 0;
975 parser->cont = false;
976 } else if (parser->idx < parser->size - 1) {
977 parser->cont = true;
978 parser->buffer[parser->idx++] = ch;
979 } else {
980 ret = -EINVAL;
981 goto out;
984 *ppos += read;
985 ret = read;
987 out:
988 return ret;
991 /* TODO add a seq_buf_to_buffer() */
992 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
994 int len;
996 if (trace_seq_used(s) <= s->seq.readpos)
997 return -EBUSY;
999 len = trace_seq_used(s) - s->seq.readpos;
1000 if (cnt > len)
1001 cnt = len;
1002 memcpy(buf, s->buffer + s->seq.readpos, cnt);
1004 s->seq.readpos += cnt;
1005 return cnt;
1008 unsigned long __read_mostly tracing_thresh;
1010 #ifdef CONFIG_TRACER_MAX_TRACE
1012 * Copy the new maximum trace into the separate maximum-trace
1013 * structure. (this way the maximum trace is permanently saved,
1014 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
1016 static void
1017 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1019 struct trace_buffer *trace_buf = &tr->trace_buffer;
1020 struct trace_buffer *max_buf = &tr->max_buffer;
1021 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
1022 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
1024 max_buf->cpu = cpu;
1025 max_buf->time_start = data->preempt_timestamp;
1027 max_data->saved_latency = tr->max_latency;
1028 max_data->critical_start = data->critical_start;
1029 max_data->critical_end = data->critical_end;
1031 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
1032 max_data->pid = tsk->pid;
1034 * If tsk == current, then use current_uid(), as that does not use
1035 * RCU. The irq tracer can be called out of RCU scope.
1037 if (tsk == current)
1038 max_data->uid = current_uid();
1039 else
1040 max_data->uid = task_uid(tsk);
1042 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1043 max_data->policy = tsk->policy;
1044 max_data->rt_priority = tsk->rt_priority;
1046 /* record this tasks comm */
1047 tracing_record_cmdline(tsk);
1051 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1052 * @tr: tracer
1053 * @tsk: the task with the latency
1054 * @cpu: The cpu that initiated the trace.
1056 * Flip the buffers between the @tr and the max_tr and record information
1057 * about which task was the cause of this latency.
1059 void
1060 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1062 struct ring_buffer *buf;
1064 if (tr->stop_count)
1065 return;
1067 WARN_ON_ONCE(!irqs_disabled());
1069 if (!tr->allocated_snapshot) {
1070 /* Only the nop tracer should hit this when disabling */
1071 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1072 return;
1075 arch_spin_lock(&tr->max_lock);
1077 buf = tr->trace_buffer.buffer;
1078 tr->trace_buffer.buffer = tr->max_buffer.buffer;
1079 tr->max_buffer.buffer = buf;
1081 __update_max_tr(tr, tsk, cpu);
1082 arch_spin_unlock(&tr->max_lock);
1086 * update_max_tr_single - only copy one trace over, and reset the rest
1087 * @tr - tracer
1088 * @tsk - task with the latency
1089 * @cpu - the cpu of the buffer to copy.
1091 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
1093 void
1094 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1096 int ret;
1098 if (tr->stop_count)
1099 return;
1101 WARN_ON_ONCE(!irqs_disabled());
1102 if (!tr->allocated_snapshot) {
1103 /* Only the nop tracer should hit this when disabling */
1104 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1105 return;
1108 arch_spin_lock(&tr->max_lock);
1110 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
1112 if (ret == -EBUSY) {
1114 * We failed to swap the buffer due to a commit taking
1115 * place on this CPU. We fail to record, but we reset
1116 * the max trace buffer (no one writes directly to it)
1117 * and flag that it failed.
1119 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
1120 "Failed to swap buffers due to commit in progress\n");
1123 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
1125 __update_max_tr(tr, tsk, cpu);
1126 arch_spin_unlock(&tr->max_lock);
1128 #endif /* CONFIG_TRACER_MAX_TRACE */
1130 static int wait_on_pipe(struct trace_iterator *iter, bool full)
1132 /* Iterators are static, they should be filled or empty */
1133 if (trace_buffer_iter(iter, iter->cpu_file))
1134 return 0;
1136 return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file,
1137 full);
1140 #ifdef CONFIG_FTRACE_STARTUP_TEST
1141 static int run_tracer_selftest(struct tracer *type)
1143 struct trace_array *tr = &global_trace;
1144 struct tracer *saved_tracer = tr->current_trace;
1145 int ret;
1147 if (!type->selftest || tracing_selftest_disabled)
1148 return 0;
1151 * Run a selftest on this tracer.
1152 * Here we reset the trace buffer, and set the current
1153 * tracer to be this tracer. The tracer can then run some
1154 * internal tracing to verify that everything is in order.
1155 * If we fail, we do not register this tracer.
1157 tracing_reset_online_cpus(&tr->trace_buffer);
1159 tr->current_trace = type;
1161 #ifdef CONFIG_TRACER_MAX_TRACE
1162 if (type->use_max_tr) {
1163 /* If we expanded the buffers, make sure the max is expanded too */
1164 if (ring_buffer_expanded)
1165 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1166 RING_BUFFER_ALL_CPUS);
1167 tr->allocated_snapshot = true;
1169 #endif
1171 /* the test is responsible for initializing and enabling */
1172 pr_info("Testing tracer %s: ", type->name);
1173 ret = type->selftest(type, tr);
1174 /* the test is responsible for resetting too */
1175 tr->current_trace = saved_tracer;
1176 if (ret) {
1177 printk(KERN_CONT "FAILED!\n");
1178 /* Add the warning after printing 'FAILED' */
1179 WARN_ON(1);
1180 return -1;
1182 /* Only reset on passing, to avoid touching corrupted buffers */
1183 tracing_reset_online_cpus(&tr->trace_buffer);
1185 #ifdef CONFIG_TRACER_MAX_TRACE
1186 if (type->use_max_tr) {
1187 tr->allocated_snapshot = false;
1189 /* Shrink the max buffer again */
1190 if (ring_buffer_expanded)
1191 ring_buffer_resize(tr->max_buffer.buffer, 1,
1192 RING_BUFFER_ALL_CPUS);
1194 #endif
1196 printk(KERN_CONT "PASSED\n");
1197 return 0;
1199 #else
1200 static inline int run_tracer_selftest(struct tracer *type)
1202 return 0;
1204 #endif /* CONFIG_FTRACE_STARTUP_TEST */
1207 * register_tracer - register a tracer with the ftrace system.
1208 * @type - the plugin for the tracer
1210 * Register a new plugin tracer.
1212 int register_tracer(struct tracer *type)
1214 struct tracer *t;
1215 int ret = 0;
1217 if (!type->name) {
1218 pr_info("Tracer must have a name\n");
1219 return -1;
1222 if (strlen(type->name) >= MAX_TRACER_SIZE) {
1223 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1224 return -1;
1227 mutex_lock(&trace_types_lock);
1229 tracing_selftest_running = true;
1231 for (t = trace_types; t; t = t->next) {
1232 if (strcmp(type->name, t->name) == 0) {
1233 /* already found */
1234 pr_info("Tracer %s already registered\n",
1235 type->name);
1236 ret = -1;
1237 goto out;
1241 if (!type->set_flag)
1242 type->set_flag = &dummy_set_flag;
1243 if (!type->flags)
1244 type->flags = &dummy_tracer_flags;
1245 else
1246 if (!type->flags->opts)
1247 type->flags->opts = dummy_tracer_opt;
1249 ret = run_tracer_selftest(type);
1250 if (ret < 0)
1251 goto out;
1253 type->next = trace_types;
1254 trace_types = type;
1256 out:
1257 tracing_selftest_running = false;
1258 mutex_unlock(&trace_types_lock);
1260 if (ret || !default_bootup_tracer)
1261 goto out_unlock;
1263 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
1264 goto out_unlock;
1266 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1267 /* Do we want this tracer to start on bootup? */
1268 tracing_set_tracer(&global_trace, type->name);
1269 default_bootup_tracer = NULL;
1270 /* disable other selftests, since this will break it. */
1271 tracing_selftest_disabled = true;
1272 #ifdef CONFIG_FTRACE_STARTUP_TEST
1273 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1274 type->name);
1275 #endif
1277 out_unlock:
1278 return ret;
1281 void tracing_reset(struct trace_buffer *buf, int cpu)
1283 struct ring_buffer *buffer = buf->buffer;
1285 if (!buffer)
1286 return;
1288 ring_buffer_record_disable(buffer);
1290 /* Make sure all commits have finished */
1291 synchronize_sched();
1292 ring_buffer_reset_cpu(buffer, cpu);
1294 ring_buffer_record_enable(buffer);
1297 void tracing_reset_online_cpus(struct trace_buffer *buf)
1299 struct ring_buffer *buffer = buf->buffer;
1300 int cpu;
1302 if (!buffer)
1303 return;
1305 ring_buffer_record_disable(buffer);
1307 /* Make sure all commits have finished */
1308 synchronize_sched();
1310 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
1312 for_each_online_cpu(cpu)
1313 ring_buffer_reset_cpu(buffer, cpu);
1315 ring_buffer_record_enable(buffer);
1318 /* Must have trace_types_lock held */
1319 void tracing_reset_all_online_cpus(void)
1321 struct trace_array *tr;
1323 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1324 tracing_reset_online_cpus(&tr->trace_buffer);
1325 #ifdef CONFIG_TRACER_MAX_TRACE
1326 tracing_reset_online_cpus(&tr->max_buffer);
1327 #endif
1331 #define SAVED_CMDLINES_DEFAULT 128
1332 #define NO_CMDLINE_MAP UINT_MAX
1333 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1334 struct saved_cmdlines_buffer {
1335 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1336 unsigned *map_cmdline_to_pid;
1337 unsigned cmdline_num;
1338 int cmdline_idx;
1339 char *saved_cmdlines;
1341 static struct saved_cmdlines_buffer *savedcmd;
1343 /* temporary disable recording */
1344 static atomic_t trace_record_cmdline_disabled __read_mostly;
1346 static inline char *get_saved_cmdlines(int idx)
1348 return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
1351 static inline void set_cmdline(int idx, const char *cmdline)
1353 memcpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
1356 static int allocate_cmdlines_buffer(unsigned int val,
1357 struct saved_cmdlines_buffer *s)
1359 s->map_cmdline_to_pid = kmalloc(val * sizeof(*s->map_cmdline_to_pid),
1360 GFP_KERNEL);
1361 if (!s->map_cmdline_to_pid)
1362 return -ENOMEM;
1364 s->saved_cmdlines = kmalloc(val * TASK_COMM_LEN, GFP_KERNEL);
1365 if (!s->saved_cmdlines) {
1366 kfree(s->map_cmdline_to_pid);
1367 return -ENOMEM;
1370 s->cmdline_idx = 0;
1371 s->cmdline_num = val;
1372 memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
1373 sizeof(s->map_pid_to_cmdline));
1374 memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
1375 val * sizeof(*s->map_cmdline_to_pid));
1377 return 0;
1380 static int trace_create_savedcmd(void)
1382 int ret;
1384 savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
1385 if (!savedcmd)
1386 return -ENOMEM;
1388 ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
1389 if (ret < 0) {
1390 kfree(savedcmd);
1391 savedcmd = NULL;
1392 return -ENOMEM;
1395 return 0;
1398 int is_tracing_stopped(void)
1400 return global_trace.stop_count;
1404 * tracing_start - quick start of the tracer
1406 * If tracing is enabled but was stopped by tracing_stop,
1407 * this will start the tracer back up.
1409 void tracing_start(void)
1411 struct ring_buffer *buffer;
1412 unsigned long flags;
1414 if (tracing_disabled)
1415 return;
1417 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1418 if (--global_trace.stop_count) {
1419 if (global_trace.stop_count < 0) {
1420 /* Someone screwed up their debugging */
1421 WARN_ON_ONCE(1);
1422 global_trace.stop_count = 0;
1424 goto out;
1427 /* Prevent the buffers from switching */
1428 arch_spin_lock(&global_trace.max_lock);
1430 buffer = global_trace.trace_buffer.buffer;
1431 if (buffer)
1432 ring_buffer_record_enable(buffer);
1434 #ifdef CONFIG_TRACER_MAX_TRACE
1435 buffer = global_trace.max_buffer.buffer;
1436 if (buffer)
1437 ring_buffer_record_enable(buffer);
1438 #endif
1440 arch_spin_unlock(&global_trace.max_lock);
1442 out:
1443 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1446 static void tracing_start_tr(struct trace_array *tr)
1448 struct ring_buffer *buffer;
1449 unsigned long flags;
1451 if (tracing_disabled)
1452 return;
1454 /* If global, we need to also start the max tracer */
1455 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1456 return tracing_start();
1458 raw_spin_lock_irqsave(&tr->start_lock, flags);
1460 if (--tr->stop_count) {
1461 if (tr->stop_count < 0) {
1462 /* Someone screwed up their debugging */
1463 WARN_ON_ONCE(1);
1464 tr->stop_count = 0;
1466 goto out;
1469 buffer = tr->trace_buffer.buffer;
1470 if (buffer)
1471 ring_buffer_record_enable(buffer);
1473 out:
1474 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1478 * tracing_stop - quick stop of the tracer
1480 * Light weight way to stop tracing. Use in conjunction with
1481 * tracing_start.
1483 void tracing_stop(void)
1485 struct ring_buffer *buffer;
1486 unsigned long flags;
1488 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1489 if (global_trace.stop_count++)
1490 goto out;
1492 /* Prevent the buffers from switching */
1493 arch_spin_lock(&global_trace.max_lock);
1495 buffer = global_trace.trace_buffer.buffer;
1496 if (buffer)
1497 ring_buffer_record_disable(buffer);
1499 #ifdef CONFIG_TRACER_MAX_TRACE
1500 buffer = global_trace.max_buffer.buffer;
1501 if (buffer)
1502 ring_buffer_record_disable(buffer);
1503 #endif
1505 arch_spin_unlock(&global_trace.max_lock);
1507 out:
1508 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1511 static void tracing_stop_tr(struct trace_array *tr)
1513 struct ring_buffer *buffer;
1514 unsigned long flags;
1516 /* If global, we need to also stop the max tracer */
1517 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1518 return tracing_stop();
1520 raw_spin_lock_irqsave(&tr->start_lock, flags);
1521 if (tr->stop_count++)
1522 goto out;
1524 buffer = tr->trace_buffer.buffer;
1525 if (buffer)
1526 ring_buffer_record_disable(buffer);
1528 out:
1529 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1532 void trace_stop_cmdline_recording(void);
1534 static int trace_save_cmdline(struct task_struct *tsk)
1536 unsigned pid, idx;
1538 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1539 return 0;
1542 * It's not the end of the world if we don't get
1543 * the lock, but we also don't want to spin
1544 * nor do we want to disable interrupts,
1545 * so if we miss here, then better luck next time.
1547 if (!arch_spin_trylock(&trace_cmdline_lock))
1548 return 0;
1550 idx = savedcmd->map_pid_to_cmdline[tsk->pid];
1551 if (idx == NO_CMDLINE_MAP) {
1552 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
1555 * Check whether the cmdline buffer at idx has a pid
1556 * mapped. We are going to overwrite that entry so we
1557 * need to clear the map_pid_to_cmdline. Otherwise we
1558 * would read the new comm for the old pid.
1560 pid = savedcmd->map_cmdline_to_pid[idx];
1561 if (pid != NO_CMDLINE_MAP)
1562 savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1564 savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
1565 savedcmd->map_pid_to_cmdline[tsk->pid] = idx;
1567 savedcmd->cmdline_idx = idx;
1570 set_cmdline(idx, tsk->comm);
1572 arch_spin_unlock(&trace_cmdline_lock);
1574 return 1;
1577 static void __trace_find_cmdline(int pid, char comm[])
1579 unsigned map;
1581 if (!pid) {
1582 strcpy(comm, "<idle>");
1583 return;
1586 if (WARN_ON_ONCE(pid < 0)) {
1587 strcpy(comm, "<XXX>");
1588 return;
1591 if (pid > PID_MAX_DEFAULT) {
1592 strcpy(comm, "<...>");
1593 return;
1596 map = savedcmd->map_pid_to_cmdline[pid];
1597 if (map != NO_CMDLINE_MAP)
1598 strcpy(comm, get_saved_cmdlines(map));
1599 else
1600 strcpy(comm, "<...>");
1603 void trace_find_cmdline(int pid, char comm[])
1605 preempt_disable();
1606 arch_spin_lock(&trace_cmdline_lock);
1608 __trace_find_cmdline(pid, comm);
1610 arch_spin_unlock(&trace_cmdline_lock);
1611 preempt_enable();
1614 void tracing_record_cmdline(struct task_struct *tsk)
1616 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1617 return;
1619 if (!__this_cpu_read(trace_cmdline_save))
1620 return;
1622 if (trace_save_cmdline(tsk))
1623 __this_cpu_write(trace_cmdline_save, false);
1626 void
1627 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1628 int pc)
1630 struct task_struct *tsk = current;
1632 entry->preempt_count = pc & 0xff;
1633 entry->pid = (tsk) ? tsk->pid : 0;
1634 entry->flags =
1635 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1636 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1637 #else
1638 TRACE_FLAG_IRQS_NOSUPPORT |
1639 #endif
1640 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1641 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1642 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
1643 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
1645 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1647 struct ring_buffer_event *
1648 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1649 int type,
1650 unsigned long len,
1651 unsigned long flags, int pc)
1653 struct ring_buffer_event *event;
1655 event = ring_buffer_lock_reserve(buffer, len);
1656 if (event != NULL) {
1657 struct trace_entry *ent = ring_buffer_event_data(event);
1659 tracing_generic_entry_update(ent, flags, pc);
1660 ent->type = type;
1663 return event;
1666 void
1667 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1669 __this_cpu_write(trace_cmdline_save, true);
1670 ring_buffer_unlock_commit(buffer, event);
1673 static inline void
1674 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1675 struct ring_buffer_event *event,
1676 unsigned long flags, int pc)
1678 __buffer_unlock_commit(buffer, event);
1680 ftrace_trace_stack(buffer, flags, 6, pc);
1681 ftrace_trace_userstack(buffer, flags, pc);
1684 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1685 struct ring_buffer_event *event,
1686 unsigned long flags, int pc)
1688 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1690 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1692 static struct ring_buffer *temp_buffer;
1694 struct ring_buffer_event *
1695 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1696 struct ftrace_event_file *ftrace_file,
1697 int type, unsigned long len,
1698 unsigned long flags, int pc)
1700 struct ring_buffer_event *entry;
1702 *current_rb = ftrace_file->tr->trace_buffer.buffer;
1703 entry = trace_buffer_lock_reserve(*current_rb,
1704 type, len, flags, pc);
1706 * If tracing is off, but we have triggers enabled
1707 * we still need to look at the event data. Use the temp_buffer
1708 * to store the trace event for the tigger to use. It's recusive
1709 * safe and will not be recorded anywhere.
1711 if (!entry && ftrace_file->flags & FTRACE_EVENT_FL_TRIGGER_COND) {
1712 *current_rb = temp_buffer;
1713 entry = trace_buffer_lock_reserve(*current_rb,
1714 type, len, flags, pc);
1716 return entry;
1718 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1720 struct ring_buffer_event *
1721 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1722 int type, unsigned long len,
1723 unsigned long flags, int pc)
1725 *current_rb = global_trace.trace_buffer.buffer;
1726 return trace_buffer_lock_reserve(*current_rb,
1727 type, len, flags, pc);
1729 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1731 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1732 struct ring_buffer_event *event,
1733 unsigned long flags, int pc)
1735 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1737 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1739 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1740 struct ring_buffer_event *event,
1741 unsigned long flags, int pc,
1742 struct pt_regs *regs)
1744 __buffer_unlock_commit(buffer, event);
1746 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1747 ftrace_trace_userstack(buffer, flags, pc);
1749 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1751 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1752 struct ring_buffer_event *event)
1754 ring_buffer_discard_commit(buffer, event);
1756 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1758 void
1759 trace_function(struct trace_array *tr,
1760 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1761 int pc)
1763 struct ftrace_event_call *call = &event_function;
1764 struct ring_buffer *buffer = tr->trace_buffer.buffer;
1765 struct ring_buffer_event *event;
1766 struct ftrace_entry *entry;
1768 /* If we are reading the ring buffer, don't trace */
1769 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1770 return;
1772 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1773 flags, pc);
1774 if (!event)
1775 return;
1776 entry = ring_buffer_event_data(event);
1777 entry->ip = ip;
1778 entry->parent_ip = parent_ip;
1780 if (!call_filter_check_discard(call, entry, buffer, event))
1781 __buffer_unlock_commit(buffer, event);
1784 #ifdef CONFIG_STACKTRACE
1786 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1787 struct ftrace_stack {
1788 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1791 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1792 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1794 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1795 unsigned long flags,
1796 int skip, int pc, struct pt_regs *regs)
1798 struct ftrace_event_call *call = &event_kernel_stack;
1799 struct ring_buffer_event *event;
1800 struct stack_entry *entry;
1801 struct stack_trace trace;
1802 int use_stack;
1803 int size = FTRACE_STACK_ENTRIES;
1805 trace.nr_entries = 0;
1806 trace.skip = skip;
1809 * Since events can happen in NMIs there's no safe way to
1810 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1811 * or NMI comes in, it will just have to use the default
1812 * FTRACE_STACK_SIZE.
1814 preempt_disable_notrace();
1816 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1818 * We don't need any atomic variables, just a barrier.
1819 * If an interrupt comes in, we don't care, because it would
1820 * have exited and put the counter back to what we want.
1821 * We just need a barrier to keep gcc from moving things
1822 * around.
1824 barrier();
1825 if (use_stack == 1) {
1826 trace.entries = this_cpu_ptr(ftrace_stack.calls);
1827 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1829 if (regs)
1830 save_stack_trace_regs(regs, &trace);
1831 else
1832 save_stack_trace(&trace);
1834 if (trace.nr_entries > size)
1835 size = trace.nr_entries;
1836 } else
1837 /* From now on, use_stack is a boolean */
1838 use_stack = 0;
1840 size *= sizeof(unsigned long);
1842 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1843 sizeof(*entry) + size, flags, pc);
1844 if (!event)
1845 goto out;
1846 entry = ring_buffer_event_data(event);
1848 memset(&entry->caller, 0, size);
1850 if (use_stack)
1851 memcpy(&entry->caller, trace.entries,
1852 trace.nr_entries * sizeof(unsigned long));
1853 else {
1854 trace.max_entries = FTRACE_STACK_ENTRIES;
1855 trace.entries = entry->caller;
1856 if (regs)
1857 save_stack_trace_regs(regs, &trace);
1858 else
1859 save_stack_trace(&trace);
1862 entry->size = trace.nr_entries;
1864 if (!call_filter_check_discard(call, entry, buffer, event))
1865 __buffer_unlock_commit(buffer, event);
1867 out:
1868 /* Again, don't let gcc optimize things here */
1869 barrier();
1870 __this_cpu_dec(ftrace_stack_reserve);
1871 preempt_enable_notrace();
1875 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1876 int skip, int pc, struct pt_regs *regs)
1878 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1879 return;
1881 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1884 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1885 int skip, int pc)
1887 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1888 return;
1890 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1893 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1894 int pc)
1896 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
1900 * trace_dump_stack - record a stack back trace in the trace buffer
1901 * @skip: Number of functions to skip (helper handlers)
1903 void trace_dump_stack(int skip)
1905 unsigned long flags;
1907 if (tracing_disabled || tracing_selftest_running)
1908 return;
1910 local_save_flags(flags);
1913 * Skip 3 more, seems to get us at the caller of
1914 * this function.
1916 skip += 3;
1917 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1918 flags, skip, preempt_count(), NULL);
1921 static DEFINE_PER_CPU(int, user_stack_count);
1923 void
1924 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1926 struct ftrace_event_call *call = &event_user_stack;
1927 struct ring_buffer_event *event;
1928 struct userstack_entry *entry;
1929 struct stack_trace trace;
1931 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1932 return;
1935 * NMIs can not handle page faults, even with fix ups.
1936 * The save user stack can (and often does) fault.
1938 if (unlikely(in_nmi()))
1939 return;
1942 * prevent recursion, since the user stack tracing may
1943 * trigger other kernel events.
1945 preempt_disable();
1946 if (__this_cpu_read(user_stack_count))
1947 goto out;
1949 __this_cpu_inc(user_stack_count);
1951 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1952 sizeof(*entry), flags, pc);
1953 if (!event)
1954 goto out_drop_count;
1955 entry = ring_buffer_event_data(event);
1957 entry->tgid = current->tgid;
1958 memset(&entry->caller, 0, sizeof(entry->caller));
1960 trace.nr_entries = 0;
1961 trace.max_entries = FTRACE_STACK_ENTRIES;
1962 trace.skip = 0;
1963 trace.entries = entry->caller;
1965 save_stack_trace_user(&trace);
1966 if (!call_filter_check_discard(call, entry, buffer, event))
1967 __buffer_unlock_commit(buffer, event);
1969 out_drop_count:
1970 __this_cpu_dec(user_stack_count);
1971 out:
1972 preempt_enable();
1975 #ifdef UNUSED
1976 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1978 ftrace_trace_userstack(tr, flags, preempt_count());
1980 #endif /* UNUSED */
1982 #endif /* CONFIG_STACKTRACE */
1984 /* created for use with alloc_percpu */
1985 struct trace_buffer_struct {
1986 char buffer[TRACE_BUF_SIZE];
1989 static struct trace_buffer_struct *trace_percpu_buffer;
1990 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1991 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1992 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1995 * The buffer used is dependent on the context. There is a per cpu
1996 * buffer for normal context, softirq contex, hard irq context and
1997 * for NMI context. Thise allows for lockless recording.
1999 * Note, if the buffers failed to be allocated, then this returns NULL
2001 static char *get_trace_buf(void)
2003 struct trace_buffer_struct *percpu_buffer;
2006 * If we have allocated per cpu buffers, then we do not
2007 * need to do any locking.
2009 if (in_nmi())
2010 percpu_buffer = trace_percpu_nmi_buffer;
2011 else if (in_irq())
2012 percpu_buffer = trace_percpu_irq_buffer;
2013 else if (in_softirq())
2014 percpu_buffer = trace_percpu_sirq_buffer;
2015 else
2016 percpu_buffer = trace_percpu_buffer;
2018 if (!percpu_buffer)
2019 return NULL;
2021 return this_cpu_ptr(&percpu_buffer->buffer[0]);
2024 static int alloc_percpu_trace_buffer(void)
2026 struct trace_buffer_struct *buffers;
2027 struct trace_buffer_struct *sirq_buffers;
2028 struct trace_buffer_struct *irq_buffers;
2029 struct trace_buffer_struct *nmi_buffers;
2031 buffers = alloc_percpu(struct trace_buffer_struct);
2032 if (!buffers)
2033 goto err_warn;
2035 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
2036 if (!sirq_buffers)
2037 goto err_sirq;
2039 irq_buffers = alloc_percpu(struct trace_buffer_struct);
2040 if (!irq_buffers)
2041 goto err_irq;
2043 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
2044 if (!nmi_buffers)
2045 goto err_nmi;
2047 trace_percpu_buffer = buffers;
2048 trace_percpu_sirq_buffer = sirq_buffers;
2049 trace_percpu_irq_buffer = irq_buffers;
2050 trace_percpu_nmi_buffer = nmi_buffers;
2052 return 0;
2054 err_nmi:
2055 free_percpu(irq_buffers);
2056 err_irq:
2057 free_percpu(sirq_buffers);
2058 err_sirq:
2059 free_percpu(buffers);
2060 err_warn:
2061 WARN(1, "Could not allocate percpu trace_printk buffer");
2062 return -ENOMEM;
2065 static int buffers_allocated;
2067 void trace_printk_init_buffers(void)
2069 if (buffers_allocated)
2070 return;
2072 if (alloc_percpu_trace_buffer())
2073 return;
2075 /* trace_printk() is for debug use only. Don't use it in production. */
2077 pr_warning("\n");
2078 pr_warning("**********************************************************\n");
2079 pr_warning("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2080 pr_warning("** **\n");
2081 pr_warning("** trace_printk() being used. Allocating extra memory. **\n");
2082 pr_warning("** **\n");
2083 pr_warning("** This means that this is a DEBUG kernel and it is **\n");
2084 pr_warning("** unsafe for production use. **\n");
2085 pr_warning("** **\n");
2086 pr_warning("** If you see this message and you are not debugging **\n");
2087 pr_warning("** the kernel, report this immediately to your vendor! **\n");
2088 pr_warning("** **\n");
2089 pr_warning("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2090 pr_warning("**********************************************************\n");
2092 /* Expand the buffers to set size */
2093 tracing_update_buffers();
2095 buffers_allocated = 1;
2098 * trace_printk_init_buffers() can be called by modules.
2099 * If that happens, then we need to start cmdline recording
2100 * directly here. If the global_trace.buffer is already
2101 * allocated here, then this was called by module code.
2103 if (global_trace.trace_buffer.buffer)
2104 tracing_start_cmdline_record();
2107 void trace_printk_start_comm(void)
2109 /* Start tracing comms if trace printk is set */
2110 if (!buffers_allocated)
2111 return;
2112 tracing_start_cmdline_record();
2115 static void trace_printk_start_stop_comm(int enabled)
2117 if (!buffers_allocated)
2118 return;
2120 if (enabled)
2121 tracing_start_cmdline_record();
2122 else
2123 tracing_stop_cmdline_record();
2127 * trace_vbprintk - write binary msg to tracing buffer
2130 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
2132 struct ftrace_event_call *call = &event_bprint;
2133 struct ring_buffer_event *event;
2134 struct ring_buffer *buffer;
2135 struct trace_array *tr = &global_trace;
2136 struct bprint_entry *entry;
2137 unsigned long flags;
2138 char *tbuffer;
2139 int len = 0, size, pc;
2141 if (unlikely(tracing_selftest_running || tracing_disabled))
2142 return 0;
2144 /* Don't pollute graph traces with trace_vprintk internals */
2145 pause_graph_tracing();
2147 pc = preempt_count();
2148 preempt_disable_notrace();
2150 tbuffer = get_trace_buf();
2151 if (!tbuffer) {
2152 len = 0;
2153 goto out;
2156 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
2158 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
2159 goto out;
2161 local_save_flags(flags);
2162 size = sizeof(*entry) + sizeof(u32) * len;
2163 buffer = tr->trace_buffer.buffer;
2164 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
2165 flags, pc);
2166 if (!event)
2167 goto out;
2168 entry = ring_buffer_event_data(event);
2169 entry->ip = ip;
2170 entry->fmt = fmt;
2172 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
2173 if (!call_filter_check_discard(call, entry, buffer, event)) {
2174 __buffer_unlock_commit(buffer, event);
2175 ftrace_trace_stack(buffer, flags, 6, pc);
2178 out:
2179 preempt_enable_notrace();
2180 unpause_graph_tracing();
2182 return len;
2184 EXPORT_SYMBOL_GPL(trace_vbprintk);
2186 static int
2187 __trace_array_vprintk(struct ring_buffer *buffer,
2188 unsigned long ip, const char *fmt, va_list args)
2190 struct ftrace_event_call *call = &event_print;
2191 struct ring_buffer_event *event;
2192 int len = 0, size, pc;
2193 struct print_entry *entry;
2194 unsigned long flags;
2195 char *tbuffer;
2197 if (tracing_disabled || tracing_selftest_running)
2198 return 0;
2200 /* Don't pollute graph traces with trace_vprintk internals */
2201 pause_graph_tracing();
2203 pc = preempt_count();
2204 preempt_disable_notrace();
2207 tbuffer = get_trace_buf();
2208 if (!tbuffer) {
2209 len = 0;
2210 goto out;
2213 len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
2215 local_save_flags(flags);
2216 size = sizeof(*entry) + len + 1;
2217 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
2218 flags, pc);
2219 if (!event)
2220 goto out;
2221 entry = ring_buffer_event_data(event);
2222 entry->ip = ip;
2224 memcpy(&entry->buf, tbuffer, len + 1);
2225 if (!call_filter_check_discard(call, entry, buffer, event)) {
2226 __buffer_unlock_commit(buffer, event);
2227 ftrace_trace_stack(buffer, flags, 6, pc);
2229 out:
2230 preempt_enable_notrace();
2231 unpause_graph_tracing();
2233 return len;
2236 int trace_array_vprintk(struct trace_array *tr,
2237 unsigned long ip, const char *fmt, va_list args)
2239 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2242 int trace_array_printk(struct trace_array *tr,
2243 unsigned long ip, const char *fmt, ...)
2245 int ret;
2246 va_list ap;
2248 if (!(trace_flags & TRACE_ITER_PRINTK))
2249 return 0;
2251 va_start(ap, fmt);
2252 ret = trace_array_vprintk(tr, ip, fmt, ap);
2253 va_end(ap);
2254 return ret;
2257 int trace_array_printk_buf(struct ring_buffer *buffer,
2258 unsigned long ip, const char *fmt, ...)
2260 int ret;
2261 va_list ap;
2263 if (!(trace_flags & TRACE_ITER_PRINTK))
2264 return 0;
2266 va_start(ap, fmt);
2267 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2268 va_end(ap);
2269 return ret;
2272 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2274 return trace_array_vprintk(&global_trace, ip, fmt, args);
2276 EXPORT_SYMBOL_GPL(trace_vprintk);
2278 static void trace_iterator_increment(struct trace_iterator *iter)
2280 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2282 iter->idx++;
2283 if (buf_iter)
2284 ring_buffer_read(buf_iter, NULL);
2287 static struct trace_entry *
2288 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2289 unsigned long *lost_events)
2291 struct ring_buffer_event *event;
2292 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
2294 if (buf_iter)
2295 event = ring_buffer_iter_peek(buf_iter, ts);
2296 else
2297 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
2298 lost_events);
2300 if (event) {
2301 iter->ent_size = ring_buffer_event_length(event);
2302 return ring_buffer_event_data(event);
2304 iter->ent_size = 0;
2305 return NULL;
2308 static struct trace_entry *
2309 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2310 unsigned long *missing_events, u64 *ent_ts)
2312 struct ring_buffer *buffer = iter->trace_buffer->buffer;
2313 struct trace_entry *ent, *next = NULL;
2314 unsigned long lost_events = 0, next_lost = 0;
2315 int cpu_file = iter->cpu_file;
2316 u64 next_ts = 0, ts;
2317 int next_cpu = -1;
2318 int next_size = 0;
2319 int cpu;
2322 * If we are in a per_cpu trace file, don't bother by iterating over
2323 * all cpu and peek directly.
2325 if (cpu_file > RING_BUFFER_ALL_CPUS) {
2326 if (ring_buffer_empty_cpu(buffer, cpu_file))
2327 return NULL;
2328 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
2329 if (ent_cpu)
2330 *ent_cpu = cpu_file;
2332 return ent;
2335 for_each_tracing_cpu(cpu) {
2337 if (ring_buffer_empty_cpu(buffer, cpu))
2338 continue;
2340 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
2343 * Pick the entry with the smallest timestamp:
2345 if (ent && (!next || ts < next_ts)) {
2346 next = ent;
2347 next_cpu = cpu;
2348 next_ts = ts;
2349 next_lost = lost_events;
2350 next_size = iter->ent_size;
2354 iter->ent_size = next_size;
2356 if (ent_cpu)
2357 *ent_cpu = next_cpu;
2359 if (ent_ts)
2360 *ent_ts = next_ts;
2362 if (missing_events)
2363 *missing_events = next_lost;
2365 return next;
2368 /* Find the next real entry, without updating the iterator itself */
2369 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2370 int *ent_cpu, u64 *ent_ts)
2372 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
2375 /* Find the next real entry, and increment the iterator to the next entry */
2376 void *trace_find_next_entry_inc(struct trace_iterator *iter)
2378 iter->ent = __find_next_entry(iter, &iter->cpu,
2379 &iter->lost_events, &iter->ts);
2381 if (iter->ent)
2382 trace_iterator_increment(iter);
2384 return iter->ent ? iter : NULL;
2387 static void trace_consume(struct trace_iterator *iter)
2389 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
2390 &iter->lost_events);
2393 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
2395 struct trace_iterator *iter = m->private;
2396 int i = (int)*pos;
2397 void *ent;
2399 WARN_ON_ONCE(iter->leftover);
2401 (*pos)++;
2403 /* can't go backwards */
2404 if (iter->idx > i)
2405 return NULL;
2407 if (iter->idx < 0)
2408 ent = trace_find_next_entry_inc(iter);
2409 else
2410 ent = iter;
2412 while (ent && iter->idx < i)
2413 ent = trace_find_next_entry_inc(iter);
2415 iter->pos = *pos;
2417 return ent;
2420 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2422 struct ring_buffer_event *event;
2423 struct ring_buffer_iter *buf_iter;
2424 unsigned long entries = 0;
2425 u64 ts;
2427 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2429 buf_iter = trace_buffer_iter(iter, cpu);
2430 if (!buf_iter)
2431 return;
2433 ring_buffer_iter_reset(buf_iter);
2436 * We could have the case with the max latency tracers
2437 * that a reset never took place on a cpu. This is evident
2438 * by the timestamp being before the start of the buffer.
2440 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
2441 if (ts >= iter->trace_buffer->time_start)
2442 break;
2443 entries++;
2444 ring_buffer_read(buf_iter, NULL);
2447 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2451 * The current tracer is copied to avoid a global locking
2452 * all around.
2454 static void *s_start(struct seq_file *m, loff_t *pos)
2456 struct trace_iterator *iter = m->private;
2457 struct trace_array *tr = iter->tr;
2458 int cpu_file = iter->cpu_file;
2459 void *p = NULL;
2460 loff_t l = 0;
2461 int cpu;
2464 * copy the tracer to avoid using a global lock all around.
2465 * iter->trace is a copy of current_trace, the pointer to the
2466 * name may be used instead of a strcmp(), as iter->trace->name
2467 * will point to the same string as current_trace->name.
2469 mutex_lock(&trace_types_lock);
2470 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2471 *iter->trace = *tr->current_trace;
2472 mutex_unlock(&trace_types_lock);
2474 #ifdef CONFIG_TRACER_MAX_TRACE
2475 if (iter->snapshot && iter->trace->use_max_tr)
2476 return ERR_PTR(-EBUSY);
2477 #endif
2479 if (!iter->snapshot)
2480 atomic_inc(&trace_record_cmdline_disabled);
2482 if (*pos != iter->pos) {
2483 iter->ent = NULL;
2484 iter->cpu = 0;
2485 iter->idx = -1;
2487 if (cpu_file == RING_BUFFER_ALL_CPUS) {
2488 for_each_tracing_cpu(cpu)
2489 tracing_iter_reset(iter, cpu);
2490 } else
2491 tracing_iter_reset(iter, cpu_file);
2493 iter->leftover = 0;
2494 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2497 } else {
2499 * If we overflowed the seq_file before, then we want
2500 * to just reuse the trace_seq buffer again.
2502 if (iter->leftover)
2503 p = iter;
2504 else {
2505 l = *pos - 1;
2506 p = s_next(m, p, &l);
2510 trace_event_read_lock();
2511 trace_access_lock(cpu_file);
2512 return p;
2515 static void s_stop(struct seq_file *m, void *p)
2517 struct trace_iterator *iter = m->private;
2519 #ifdef CONFIG_TRACER_MAX_TRACE
2520 if (iter->snapshot && iter->trace->use_max_tr)
2521 return;
2522 #endif
2524 if (!iter->snapshot)
2525 atomic_dec(&trace_record_cmdline_disabled);
2527 trace_access_unlock(iter->cpu_file);
2528 trace_event_read_unlock();
2531 static void
2532 get_total_entries(struct trace_buffer *buf,
2533 unsigned long *total, unsigned long *entries)
2535 unsigned long count;
2536 int cpu;
2538 *total = 0;
2539 *entries = 0;
2541 for_each_tracing_cpu(cpu) {
2542 count = ring_buffer_entries_cpu(buf->buffer, cpu);
2544 * If this buffer has skipped entries, then we hold all
2545 * entries for the trace and we need to ignore the
2546 * ones before the time stamp.
2548 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2549 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
2550 /* total is the same as the entries */
2551 *total += count;
2552 } else
2553 *total += count +
2554 ring_buffer_overrun_cpu(buf->buffer, cpu);
2555 *entries += count;
2559 static void print_lat_help_header(struct seq_file *m)
2561 seq_puts(m, "# _------=> CPU# \n"
2562 "# / _-----=> irqs-off \n"
2563 "# | / _----=> need-resched \n"
2564 "# || / _---=> hardirq/softirq \n"
2565 "# ||| / _--=> preempt-depth \n"
2566 "# |||| / delay \n"
2567 "# cmd pid ||||| time | caller \n"
2568 "# \\ / ||||| \\ | / \n");
2571 static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
2573 unsigned long total;
2574 unsigned long entries;
2576 get_total_entries(buf, &total, &entries);
2577 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2578 entries, total, num_online_cpus());
2579 seq_puts(m, "#\n");
2582 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
2584 print_event_info(buf, m);
2585 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n"
2586 "# | | | | |\n");
2589 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
2591 print_event_info(buf, m);
2592 seq_puts(m, "# _-----=> irqs-off\n"
2593 "# / _----=> need-resched\n"
2594 "# | / _---=> hardirq/softirq\n"
2595 "# || / _--=> preempt-depth\n"
2596 "# ||| / delay\n"
2597 "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
2598 "# | | | |||| | |\n");
2601 void
2602 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2604 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2605 struct trace_buffer *buf = iter->trace_buffer;
2606 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2607 struct tracer *type = iter->trace;
2608 unsigned long entries;
2609 unsigned long total;
2610 const char *name = "preemption";
2612 name = type->name;
2614 get_total_entries(buf, &total, &entries);
2616 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2617 name, UTS_RELEASE);
2618 seq_puts(m, "# -----------------------------------"
2619 "---------------------------------\n");
2620 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2621 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2622 nsecs_to_usecs(data->saved_latency),
2623 entries,
2624 total,
2625 buf->cpu,
2626 #if defined(CONFIG_PREEMPT_NONE)
2627 "server",
2628 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2629 "desktop",
2630 #elif defined(CONFIG_PREEMPT)
2631 "preempt",
2632 #else
2633 "unknown",
2634 #endif
2635 /* These are reserved for later use */
2636 0, 0, 0, 0);
2637 #ifdef CONFIG_SMP
2638 seq_printf(m, " #P:%d)\n", num_online_cpus());
2639 #else
2640 seq_puts(m, ")\n");
2641 #endif
2642 seq_puts(m, "# -----------------\n");
2643 seq_printf(m, "# | task: %.16s-%d "
2644 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2645 data->comm, data->pid,
2646 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2647 data->policy, data->rt_priority);
2648 seq_puts(m, "# -----------------\n");
2650 if (data->critical_start) {
2651 seq_puts(m, "# => started at: ");
2652 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2653 trace_print_seq(m, &iter->seq);
2654 seq_puts(m, "\n# => ended at: ");
2655 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2656 trace_print_seq(m, &iter->seq);
2657 seq_puts(m, "\n#\n");
2660 seq_puts(m, "#\n");
2663 static void test_cpu_buff_start(struct trace_iterator *iter)
2665 struct trace_seq *s = &iter->seq;
2667 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2668 return;
2670 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2671 return;
2673 if (cpumask_test_cpu(iter->cpu, iter->started))
2674 return;
2676 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2677 return;
2679 cpumask_set_cpu(iter->cpu, iter->started);
2681 /* Don't print started cpu buffer for the first entry of the trace */
2682 if (iter->idx > 1)
2683 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2684 iter->cpu);
2687 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2689 struct trace_seq *s = &iter->seq;
2690 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2691 struct trace_entry *entry;
2692 struct trace_event *event;
2694 entry = iter->ent;
2696 test_cpu_buff_start(iter);
2698 event = ftrace_find_event(entry->type);
2700 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2701 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2702 trace_print_lat_context(iter);
2703 else
2704 trace_print_context(iter);
2707 if (trace_seq_has_overflowed(s))
2708 return TRACE_TYPE_PARTIAL_LINE;
2710 if (event)
2711 return event->funcs->trace(iter, sym_flags, event);
2713 trace_seq_printf(s, "Unknown type %d\n", entry->type);
2715 return trace_handle_return(s);
2718 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2720 struct trace_seq *s = &iter->seq;
2721 struct trace_entry *entry;
2722 struct trace_event *event;
2724 entry = iter->ent;
2726 if (trace_flags & TRACE_ITER_CONTEXT_INFO)
2727 trace_seq_printf(s, "%d %d %llu ",
2728 entry->pid, iter->cpu, iter->ts);
2730 if (trace_seq_has_overflowed(s))
2731 return TRACE_TYPE_PARTIAL_LINE;
2733 event = ftrace_find_event(entry->type);
2734 if (event)
2735 return event->funcs->raw(iter, 0, event);
2737 trace_seq_printf(s, "%d ?\n", entry->type);
2739 return trace_handle_return(s);
2742 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2744 struct trace_seq *s = &iter->seq;
2745 unsigned char newline = '\n';
2746 struct trace_entry *entry;
2747 struct trace_event *event;
2749 entry = iter->ent;
2751 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2752 SEQ_PUT_HEX_FIELD(s, entry->pid);
2753 SEQ_PUT_HEX_FIELD(s, iter->cpu);
2754 SEQ_PUT_HEX_FIELD(s, iter->ts);
2755 if (trace_seq_has_overflowed(s))
2756 return TRACE_TYPE_PARTIAL_LINE;
2759 event = ftrace_find_event(entry->type);
2760 if (event) {
2761 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2762 if (ret != TRACE_TYPE_HANDLED)
2763 return ret;
2766 SEQ_PUT_FIELD(s, newline);
2768 return trace_handle_return(s);
2771 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2773 struct trace_seq *s = &iter->seq;
2774 struct trace_entry *entry;
2775 struct trace_event *event;
2777 entry = iter->ent;
2779 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2780 SEQ_PUT_FIELD(s, entry->pid);
2781 SEQ_PUT_FIELD(s, iter->cpu);
2782 SEQ_PUT_FIELD(s, iter->ts);
2783 if (trace_seq_has_overflowed(s))
2784 return TRACE_TYPE_PARTIAL_LINE;
2787 event = ftrace_find_event(entry->type);
2788 return event ? event->funcs->binary(iter, 0, event) :
2789 TRACE_TYPE_HANDLED;
2792 int trace_empty(struct trace_iterator *iter)
2794 struct ring_buffer_iter *buf_iter;
2795 int cpu;
2797 /* If we are looking at one CPU buffer, only check that one */
2798 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2799 cpu = iter->cpu_file;
2800 buf_iter = trace_buffer_iter(iter, cpu);
2801 if (buf_iter) {
2802 if (!ring_buffer_iter_empty(buf_iter))
2803 return 0;
2804 } else {
2805 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2806 return 0;
2808 return 1;
2811 for_each_tracing_cpu(cpu) {
2812 buf_iter = trace_buffer_iter(iter, cpu);
2813 if (buf_iter) {
2814 if (!ring_buffer_iter_empty(buf_iter))
2815 return 0;
2816 } else {
2817 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2818 return 0;
2822 return 1;
2825 /* Called with trace_event_read_lock() held. */
2826 enum print_line_t print_trace_line(struct trace_iterator *iter)
2828 enum print_line_t ret;
2830 if (iter->lost_events) {
2831 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2832 iter->cpu, iter->lost_events);
2833 if (trace_seq_has_overflowed(&iter->seq))
2834 return TRACE_TYPE_PARTIAL_LINE;
2837 if (iter->trace && iter->trace->print_line) {
2838 ret = iter->trace->print_line(iter);
2839 if (ret != TRACE_TYPE_UNHANDLED)
2840 return ret;
2843 if (iter->ent->type == TRACE_BPUTS &&
2844 trace_flags & TRACE_ITER_PRINTK &&
2845 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2846 return trace_print_bputs_msg_only(iter);
2848 if (iter->ent->type == TRACE_BPRINT &&
2849 trace_flags & TRACE_ITER_PRINTK &&
2850 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2851 return trace_print_bprintk_msg_only(iter);
2853 if (iter->ent->type == TRACE_PRINT &&
2854 trace_flags & TRACE_ITER_PRINTK &&
2855 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2856 return trace_print_printk_msg_only(iter);
2858 if (trace_flags & TRACE_ITER_BIN)
2859 return print_bin_fmt(iter);
2861 if (trace_flags & TRACE_ITER_HEX)
2862 return print_hex_fmt(iter);
2864 if (trace_flags & TRACE_ITER_RAW)
2865 return print_raw_fmt(iter);
2867 return print_trace_fmt(iter);
2870 void trace_latency_header(struct seq_file *m)
2872 struct trace_iterator *iter = m->private;
2874 /* print nothing if the buffers are empty */
2875 if (trace_empty(iter))
2876 return;
2878 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2879 print_trace_header(m, iter);
2881 if (!(trace_flags & TRACE_ITER_VERBOSE))
2882 print_lat_help_header(m);
2885 void trace_default_header(struct seq_file *m)
2887 struct trace_iterator *iter = m->private;
2889 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2890 return;
2892 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2893 /* print nothing if the buffers are empty */
2894 if (trace_empty(iter))
2895 return;
2896 print_trace_header(m, iter);
2897 if (!(trace_flags & TRACE_ITER_VERBOSE))
2898 print_lat_help_header(m);
2899 } else {
2900 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2901 if (trace_flags & TRACE_ITER_IRQ_INFO)
2902 print_func_help_header_irq(iter->trace_buffer, m);
2903 else
2904 print_func_help_header(iter->trace_buffer, m);
2909 static void test_ftrace_alive(struct seq_file *m)
2911 if (!ftrace_is_dead())
2912 return;
2913 seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
2914 "# MAY BE MISSING FUNCTION EVENTS\n");
2917 #ifdef CONFIG_TRACER_MAX_TRACE
2918 static void show_snapshot_main_help(struct seq_file *m)
2920 seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
2921 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
2922 "# Takes a snapshot of the main buffer.\n"
2923 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
2924 "# (Doesn't have to be '2' works with any number that\n"
2925 "# is not a '0' or '1')\n");
2928 static void show_snapshot_percpu_help(struct seq_file *m)
2930 seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2931 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2932 seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
2933 "# Takes a snapshot of the main buffer for this cpu.\n");
2934 #else
2935 seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
2936 "# Must use main snapshot file to allocate.\n");
2937 #endif
2938 seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
2939 "# (Doesn't have to be '2' works with any number that\n"
2940 "# is not a '0' or '1')\n");
2943 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2945 if (iter->tr->allocated_snapshot)
2946 seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
2947 else
2948 seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
2950 seq_puts(m, "# Snapshot commands:\n");
2951 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2952 show_snapshot_main_help(m);
2953 else
2954 show_snapshot_percpu_help(m);
2956 #else
2957 /* Should never be called */
2958 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2959 #endif
2961 static int s_show(struct seq_file *m, void *v)
2963 struct trace_iterator *iter = v;
2964 int ret;
2966 if (iter->ent == NULL) {
2967 if (iter->tr) {
2968 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2969 seq_puts(m, "#\n");
2970 test_ftrace_alive(m);
2972 if (iter->snapshot && trace_empty(iter))
2973 print_snapshot_help(m, iter);
2974 else if (iter->trace && iter->trace->print_header)
2975 iter->trace->print_header(m);
2976 else
2977 trace_default_header(m);
2979 } else if (iter->leftover) {
2981 * If we filled the seq_file buffer earlier, we
2982 * want to just show it now.
2984 ret = trace_print_seq(m, &iter->seq);
2986 /* ret should this time be zero, but you never know */
2987 iter->leftover = ret;
2989 } else {
2990 print_trace_line(iter);
2991 ret = trace_print_seq(m, &iter->seq);
2993 * If we overflow the seq_file buffer, then it will
2994 * ask us for this data again at start up.
2995 * Use that instead.
2996 * ret is 0 if seq_file write succeeded.
2997 * -1 otherwise.
2999 iter->leftover = ret;
3002 return 0;
3006 * Should be used after trace_array_get(), trace_types_lock
3007 * ensures that i_cdev was already initialized.
3009 static inline int tracing_get_cpu(struct inode *inode)
3011 if (inode->i_cdev) /* See trace_create_cpu_file() */
3012 return (long)inode->i_cdev - 1;
3013 return RING_BUFFER_ALL_CPUS;
3016 static const struct seq_operations tracer_seq_ops = {
3017 .start = s_start,
3018 .next = s_next,
3019 .stop = s_stop,
3020 .show = s_show,
3023 static struct trace_iterator *
3024 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
3026 struct trace_array *tr = inode->i_private;
3027 struct trace_iterator *iter;
3028 int cpu;
3030 if (tracing_disabled)
3031 return ERR_PTR(-ENODEV);
3033 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
3034 if (!iter)
3035 return ERR_PTR(-ENOMEM);
3037 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
3038 GFP_KERNEL);
3039 if (!iter->buffer_iter)
3040 goto release;
3043 * We make a copy of the current tracer to avoid concurrent
3044 * changes on it while we are reading.
3046 mutex_lock(&trace_types_lock);
3047 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
3048 if (!iter->trace)
3049 goto fail;
3051 *iter->trace = *tr->current_trace;
3053 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
3054 goto fail;
3056 iter->tr = tr;
3058 #ifdef CONFIG_TRACER_MAX_TRACE
3059 /* Currently only the top directory has a snapshot */
3060 if (tr->current_trace->print_max || snapshot)
3061 iter->trace_buffer = &tr->max_buffer;
3062 else
3063 #endif
3064 iter->trace_buffer = &tr->trace_buffer;
3065 iter->snapshot = snapshot;
3066 iter->pos = -1;
3067 iter->cpu_file = tracing_get_cpu(inode);
3068 mutex_init(&iter->mutex);
3070 /* Notify the tracer early; before we stop tracing. */
3071 if (iter->trace && iter->trace->open)
3072 iter->trace->open(iter);
3074 /* Annotate start of buffers if we had overruns */
3075 if (ring_buffer_overruns(iter->trace_buffer->buffer))
3076 iter->iter_flags |= TRACE_FILE_ANNOTATE;
3078 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3079 if (trace_clocks[tr->clock_id].in_ns)
3080 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3082 /* stop the trace while dumping if we are not opening "snapshot" */
3083 if (!iter->snapshot)
3084 tracing_stop_tr(tr);
3086 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
3087 for_each_tracing_cpu(cpu) {
3088 iter->buffer_iter[cpu] =
3089 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
3091 ring_buffer_read_prepare_sync();
3092 for_each_tracing_cpu(cpu) {
3093 ring_buffer_read_start(iter->buffer_iter[cpu]);
3094 tracing_iter_reset(iter, cpu);
3096 } else {
3097 cpu = iter->cpu_file;
3098 iter->buffer_iter[cpu] =
3099 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
3100 ring_buffer_read_prepare_sync();
3101 ring_buffer_read_start(iter->buffer_iter[cpu]);
3102 tracing_iter_reset(iter, cpu);
3105 mutex_unlock(&trace_types_lock);
3107 return iter;
3109 fail:
3110 mutex_unlock(&trace_types_lock);
3111 kfree(iter->trace);
3112 kfree(iter->buffer_iter);
3113 release:
3114 seq_release_private(inode, file);
3115 return ERR_PTR(-ENOMEM);
3118 int tracing_open_generic(struct inode *inode, struct file *filp)
3120 if (tracing_disabled)
3121 return -ENODEV;
3123 filp->private_data = inode->i_private;
3124 return 0;
3127 bool tracing_is_disabled(void)
3129 return (tracing_disabled) ? true: false;
3133 * Open and update trace_array ref count.
3134 * Must have the current trace_array passed to it.
3136 static int tracing_open_generic_tr(struct inode *inode, struct file *filp)
3138 struct trace_array *tr = inode->i_private;
3140 if (tracing_disabled)
3141 return -ENODEV;
3143 if (trace_array_get(tr) < 0)
3144 return -ENODEV;
3146 filp->private_data = inode->i_private;
3148 return 0;
3151 static int tracing_release(struct inode *inode, struct file *file)
3153 struct trace_array *tr = inode->i_private;
3154 struct seq_file *m = file->private_data;
3155 struct trace_iterator *iter;
3156 int cpu;
3158 if (!(file->f_mode & FMODE_READ)) {
3159 trace_array_put(tr);
3160 return 0;
3163 /* Writes do not use seq_file */
3164 iter = m->private;
3165 mutex_lock(&trace_types_lock);
3167 for_each_tracing_cpu(cpu) {
3168 if (iter->buffer_iter[cpu])
3169 ring_buffer_read_finish(iter->buffer_iter[cpu]);
3172 if (iter->trace && iter->trace->close)
3173 iter->trace->close(iter);
3175 if (!iter->snapshot)
3176 /* reenable tracing if it was previously enabled */
3177 tracing_start_tr(tr);
3179 __trace_array_put(tr);
3181 mutex_unlock(&trace_types_lock);
3183 mutex_destroy(&iter->mutex);
3184 free_cpumask_var(iter->started);
3185 kfree(iter->trace);
3186 kfree(iter->buffer_iter);
3187 seq_release_private(inode, file);
3189 return 0;
3192 static int tracing_release_generic_tr(struct inode *inode, struct file *file)
3194 struct trace_array *tr = inode->i_private;
3196 trace_array_put(tr);
3197 return 0;
3200 static int tracing_single_release_tr(struct inode *inode, struct file *file)
3202 struct trace_array *tr = inode->i_private;
3204 trace_array_put(tr);
3206 return single_release(inode, file);
3209 static int tracing_open(struct inode *inode, struct file *file)
3211 struct trace_array *tr = inode->i_private;
3212 struct trace_iterator *iter;
3213 int ret = 0;
3215 if (trace_array_get(tr) < 0)
3216 return -ENODEV;
3218 /* If this file was open for write, then erase contents */
3219 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
3220 int cpu = tracing_get_cpu(inode);
3222 if (cpu == RING_BUFFER_ALL_CPUS)
3223 tracing_reset_online_cpus(&tr->trace_buffer);
3224 else
3225 tracing_reset(&tr->trace_buffer, cpu);
3228 if (file->f_mode & FMODE_READ) {
3229 iter = __tracing_open(inode, file, false);
3230 if (IS_ERR(iter))
3231 ret = PTR_ERR(iter);
3232 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
3233 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3236 if (ret < 0)
3237 trace_array_put(tr);
3239 return ret;
3243 * Some tracers are not suitable for instance buffers.
3244 * A tracer is always available for the global array (toplevel)
3245 * or if it explicitly states that it is.
3247 static bool
3248 trace_ok_for_array(struct tracer *t, struct trace_array *tr)
3250 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
3253 /* Find the next tracer that this trace array may use */
3254 static struct tracer *
3255 get_tracer_for_array(struct trace_array *tr, struct tracer *t)
3257 while (t && !trace_ok_for_array(t, tr))
3258 t = t->next;
3260 return t;
3263 static void *
3264 t_next(struct seq_file *m, void *v, loff_t *pos)
3266 struct trace_array *tr = m->private;
3267 struct tracer *t = v;
3269 (*pos)++;
3271 if (t)
3272 t = get_tracer_for_array(tr, t->next);
3274 return t;
3277 static void *t_start(struct seq_file *m, loff_t *pos)
3279 struct trace_array *tr = m->private;
3280 struct tracer *t;
3281 loff_t l = 0;
3283 mutex_lock(&trace_types_lock);
3285 t = get_tracer_for_array(tr, trace_types);
3286 for (; t && l < *pos; t = t_next(m, t, &l))
3289 return t;
3292 static void t_stop(struct seq_file *m, void *p)
3294 mutex_unlock(&trace_types_lock);
3297 static int t_show(struct seq_file *m, void *v)
3299 struct tracer *t = v;
3301 if (!t)
3302 return 0;
3304 seq_puts(m, t->name);
3305 if (t->next)
3306 seq_putc(m, ' ');
3307 else
3308 seq_putc(m, '\n');
3310 return 0;
3313 static const struct seq_operations show_traces_seq_ops = {
3314 .start = t_start,
3315 .next = t_next,
3316 .stop = t_stop,
3317 .show = t_show,
3320 static int show_traces_open(struct inode *inode, struct file *file)
3322 struct trace_array *tr = inode->i_private;
3323 struct seq_file *m;
3324 int ret;
3326 if (tracing_disabled)
3327 return -ENODEV;
3329 ret = seq_open(file, &show_traces_seq_ops);
3330 if (ret)
3331 return ret;
3333 m = file->private_data;
3334 m->private = tr;
3336 return 0;
3339 static ssize_t
3340 tracing_write_stub(struct file *filp, const char __user *ubuf,
3341 size_t count, loff_t *ppos)
3343 return count;
3346 loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
3348 int ret;
3350 if (file->f_mode & FMODE_READ)
3351 ret = seq_lseek(file, offset, whence);
3352 else
3353 file->f_pos = ret = 0;
3355 return ret;
3358 static const struct file_operations tracing_fops = {
3359 .open = tracing_open,
3360 .read = seq_read,
3361 .write = tracing_write_stub,
3362 .llseek = tracing_lseek,
3363 .release = tracing_release,
3366 static const struct file_operations show_traces_fops = {
3367 .open = show_traces_open,
3368 .read = seq_read,
3369 .release = seq_release,
3370 .llseek = seq_lseek,
3374 * The tracer itself will not take this lock, but still we want
3375 * to provide a consistent cpumask to user-space:
3377 static DEFINE_MUTEX(tracing_cpumask_update_lock);
3380 * Temporary storage for the character representation of the
3381 * CPU bitmask (and one more byte for the newline):
3383 static char mask_str[NR_CPUS + 1];
3385 static ssize_t
3386 tracing_cpumask_read(struct file *filp, char __user *ubuf,
3387 size_t count, loff_t *ppos)
3389 struct trace_array *tr = file_inode(filp)->i_private;
3390 int len;
3392 mutex_lock(&tracing_cpumask_update_lock);
3394 len = snprintf(mask_str, count, "%*pb\n",
3395 cpumask_pr_args(tr->tracing_cpumask));
3396 if (len >= count) {
3397 count = -EINVAL;
3398 goto out_err;
3400 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3402 out_err:
3403 mutex_unlock(&tracing_cpumask_update_lock);
3405 return count;
3408 static ssize_t
3409 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3410 size_t count, loff_t *ppos)
3412 struct trace_array *tr = file_inode(filp)->i_private;
3413 cpumask_var_t tracing_cpumask_new;
3414 int err, cpu;
3416 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3417 return -ENOMEM;
3419 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
3420 if (err)
3421 goto err_unlock;
3423 mutex_lock(&tracing_cpumask_update_lock);
3425 local_irq_disable();
3426 arch_spin_lock(&tr->max_lock);
3427 for_each_tracing_cpu(cpu) {
3429 * Increase/decrease the disabled counter if we are
3430 * about to flip a bit in the cpumask:
3432 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
3433 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3434 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3435 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
3437 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
3438 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3439 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3440 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
3443 arch_spin_unlock(&tr->max_lock);
3444 local_irq_enable();
3446 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
3448 mutex_unlock(&tracing_cpumask_update_lock);
3449 free_cpumask_var(tracing_cpumask_new);
3451 return count;
3453 err_unlock:
3454 free_cpumask_var(tracing_cpumask_new);
3456 return err;
3459 static const struct file_operations tracing_cpumask_fops = {
3460 .open = tracing_open_generic_tr,
3461 .read = tracing_cpumask_read,
3462 .write = tracing_cpumask_write,
3463 .release = tracing_release_generic_tr,
3464 .llseek = generic_file_llseek,
3467 static int tracing_trace_options_show(struct seq_file *m, void *v)
3469 struct tracer_opt *trace_opts;
3470 struct trace_array *tr = m->private;
3471 u32 tracer_flags;
3472 int i;
3474 mutex_lock(&trace_types_lock);
3475 tracer_flags = tr->current_trace->flags->val;
3476 trace_opts = tr->current_trace->flags->opts;
3478 for (i = 0; trace_options[i]; i++) {
3479 if (trace_flags & (1 << i))
3480 seq_printf(m, "%s\n", trace_options[i]);
3481 else
3482 seq_printf(m, "no%s\n", trace_options[i]);
3485 for (i = 0; trace_opts[i].name; i++) {
3486 if (tracer_flags & trace_opts[i].bit)
3487 seq_printf(m, "%s\n", trace_opts[i].name);
3488 else
3489 seq_printf(m, "no%s\n", trace_opts[i].name);
3491 mutex_unlock(&trace_types_lock);
3493 return 0;
3496 static int __set_tracer_option(struct trace_array *tr,
3497 struct tracer_flags *tracer_flags,
3498 struct tracer_opt *opts, int neg)
3500 struct tracer *trace = tr->current_trace;
3501 int ret;
3503 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
3504 if (ret)
3505 return ret;
3507 if (neg)
3508 tracer_flags->val &= ~opts->bit;
3509 else
3510 tracer_flags->val |= opts->bit;
3511 return 0;
3514 /* Try to assign a tracer specific option */
3515 static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
3517 struct tracer *trace = tr->current_trace;
3518 struct tracer_flags *tracer_flags = trace->flags;
3519 struct tracer_opt *opts = NULL;
3520 int i;
3522 for (i = 0; tracer_flags->opts[i].name; i++) {
3523 opts = &tracer_flags->opts[i];
3525 if (strcmp(cmp, opts->name) == 0)
3526 return __set_tracer_option(tr, trace->flags, opts, neg);
3529 return -EINVAL;
3532 /* Some tracers require overwrite to stay enabled */
3533 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3535 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3536 return -1;
3538 return 0;
3541 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
3543 /* do nothing if flag is already set */
3544 if (!!(trace_flags & mask) == !!enabled)
3545 return 0;
3547 /* Give the tracer a chance to approve the change */
3548 if (tr->current_trace->flag_changed)
3549 if (tr->current_trace->flag_changed(tr, mask, !!enabled))
3550 return -EINVAL;
3552 if (enabled)
3553 trace_flags |= mask;
3554 else
3555 trace_flags &= ~mask;
3557 if (mask == TRACE_ITER_RECORD_CMD)
3558 trace_event_enable_cmd_record(enabled);
3560 if (mask == TRACE_ITER_OVERWRITE) {
3561 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
3562 #ifdef CONFIG_TRACER_MAX_TRACE
3563 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
3564 #endif
3567 if (mask == TRACE_ITER_PRINTK)
3568 trace_printk_start_stop_comm(enabled);
3570 return 0;
3573 static int trace_set_options(struct trace_array *tr, char *option)
3575 char *cmp;
3576 int neg = 0;
3577 int ret = -ENODEV;
3578 int i;
3580 cmp = strstrip(option);
3582 if (strncmp(cmp, "no", 2) == 0) {
3583 neg = 1;
3584 cmp += 2;
3587 mutex_lock(&trace_types_lock);
3589 for (i = 0; trace_options[i]; i++) {
3590 if (strcmp(cmp, trace_options[i]) == 0) {
3591 ret = set_tracer_flag(tr, 1 << i, !neg);
3592 break;
3596 /* If no option could be set, test the specific tracer options */
3597 if (!trace_options[i])
3598 ret = set_tracer_option(tr, cmp, neg);
3600 mutex_unlock(&trace_types_lock);
3602 return ret;
3605 static ssize_t
3606 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3607 size_t cnt, loff_t *ppos)
3609 struct seq_file *m = filp->private_data;
3610 struct trace_array *tr = m->private;
3611 char buf[64];
3612 int ret;
3614 if (cnt >= sizeof(buf))
3615 return -EINVAL;
3617 if (copy_from_user(&buf, ubuf, cnt))
3618 return -EFAULT;
3620 buf[cnt] = 0;
3622 ret = trace_set_options(tr, buf);
3623 if (ret < 0)
3624 return ret;
3626 *ppos += cnt;
3628 return cnt;
3631 static int tracing_trace_options_open(struct inode *inode, struct file *file)
3633 struct trace_array *tr = inode->i_private;
3634 int ret;
3636 if (tracing_disabled)
3637 return -ENODEV;
3639 if (trace_array_get(tr) < 0)
3640 return -ENODEV;
3642 ret = single_open(file, tracing_trace_options_show, inode->i_private);
3643 if (ret < 0)
3644 trace_array_put(tr);
3646 return ret;
3649 static const struct file_operations tracing_iter_fops = {
3650 .open = tracing_trace_options_open,
3651 .read = seq_read,
3652 .llseek = seq_lseek,
3653 .release = tracing_single_release_tr,
3654 .write = tracing_trace_options_write,
3657 static const char readme_msg[] =
3658 "tracing mini-HOWTO:\n\n"
3659 "# echo 0 > tracing_on : quick way to disable tracing\n"
3660 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3661 " Important files:\n"
3662 " trace\t\t\t- The static contents of the buffer\n"
3663 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3664 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3665 " current_tracer\t- function and latency tracers\n"
3666 " available_tracers\t- list of configured tracers for current_tracer\n"
3667 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3668 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3669 " trace_clock\t\t-change the clock used to order events\n"
3670 " local: Per cpu clock but may not be synced across CPUs\n"
3671 " global: Synced across CPUs but slows tracing down.\n"
3672 " counter: Not a clock, but just an increment\n"
3673 " uptime: Jiffy counter from time of boot\n"
3674 " perf: Same clock that perf events use\n"
3675 #ifdef CONFIG_X86_64
3676 " x86-tsc: TSC cycle counter\n"
3677 #endif
3678 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3679 " tracing_cpumask\t- Limit which CPUs to trace\n"
3680 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3681 "\t\t\t Remove sub-buffer with rmdir\n"
3682 " trace_options\t\t- Set format or modify how tracing happens\n"
3683 "\t\t\t Disable an option by adding a suffix 'no' to the\n"
3684 "\t\t\t option name\n"
3685 " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
3686 #ifdef CONFIG_DYNAMIC_FTRACE
3687 "\n available_filter_functions - list of functions that can be filtered on\n"
3688 " set_ftrace_filter\t- echo function name in here to only trace these\n"
3689 "\t\t\t functions\n"
3690 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3691 "\t modules: Can select a group via module\n"
3692 "\t Format: :mod:<module-name>\n"
3693 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
3694 "\t triggers: a command to perform when function is hit\n"
3695 "\t Format: <function>:<trigger>[:count]\n"
3696 "\t trigger: traceon, traceoff\n"
3697 "\t\t enable_event:<system>:<event>\n"
3698 "\t\t disable_event:<system>:<event>\n"
3699 #ifdef CONFIG_STACKTRACE
3700 "\t\t stacktrace\n"
3701 #endif
3702 #ifdef CONFIG_TRACER_SNAPSHOT
3703 "\t\t snapshot\n"
3704 #endif
3705 "\t\t dump\n"
3706 "\t\t cpudump\n"
3707 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
3708 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
3709 "\t The first one will disable tracing every time do_fault is hit\n"
3710 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
3711 "\t The first time do trap is hit and it disables tracing, the\n"
3712 "\t counter will decrement to 2. If tracing is already disabled,\n"
3713 "\t the counter will not decrement. It only decrements when the\n"
3714 "\t trigger did work\n"
3715 "\t To remove trigger without count:\n"
3716 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
3717 "\t To remove trigger with a count:\n"
3718 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
3719 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
3720 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3721 "\t modules: Can select a group via module command :mod:\n"
3722 "\t Does not accept triggers\n"
3723 #endif /* CONFIG_DYNAMIC_FTRACE */
3724 #ifdef CONFIG_FUNCTION_TRACER
3725 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
3726 "\t\t (function)\n"
3727 #endif
3728 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3729 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3730 " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n"
3731 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3732 #endif
3733 #ifdef CONFIG_TRACER_SNAPSHOT
3734 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
3735 "\t\t\t snapshot buffer. Read the contents for more\n"
3736 "\t\t\t information\n"
3737 #endif
3738 #ifdef CONFIG_STACK_TRACER
3739 " stack_trace\t\t- Shows the max stack trace when active\n"
3740 " stack_max_size\t- Shows current max stack size that was traced\n"
3741 "\t\t\t Write into this file to reset the max size (trigger a\n"
3742 "\t\t\t new trace)\n"
3743 #ifdef CONFIG_DYNAMIC_FTRACE
3744 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
3745 "\t\t\t traces\n"
3746 #endif
3747 #endif /* CONFIG_STACK_TRACER */
3748 " events/\t\t- Directory containing all trace event subsystems:\n"
3749 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
3750 " events/<system>/\t- Directory containing all trace events for <system>:\n"
3751 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
3752 "\t\t\t events\n"
3753 " filter\t\t- If set, only events passing filter are traced\n"
3754 " events/<system>/<event>/\t- Directory containing control files for\n"
3755 "\t\t\t <event>:\n"
3756 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
3757 " filter\t\t- If set, only events passing filter are traced\n"
3758 " trigger\t\t- If set, a command to perform when event is hit\n"
3759 "\t Format: <trigger>[:count][if <filter>]\n"
3760 "\t trigger: traceon, traceoff\n"
3761 "\t enable_event:<system>:<event>\n"
3762 "\t disable_event:<system>:<event>\n"
3763 #ifdef CONFIG_STACKTRACE
3764 "\t\t stacktrace\n"
3765 #endif
3766 #ifdef CONFIG_TRACER_SNAPSHOT
3767 "\t\t snapshot\n"
3768 #endif
3769 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
3770 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
3771 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
3772 "\t events/block/block_unplug/trigger\n"
3773 "\t The first disables tracing every time block_unplug is hit.\n"
3774 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
3775 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
3776 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
3777 "\t Like function triggers, the counter is only decremented if it\n"
3778 "\t enabled or disabled tracing.\n"
3779 "\t To remove a trigger without a count:\n"
3780 "\t echo '!<trigger> > <system>/<event>/trigger\n"
3781 "\t To remove a trigger with a count:\n"
3782 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
3783 "\t Filters can be ignored when removing a trigger.\n"
3786 static ssize_t
3787 tracing_readme_read(struct file *filp, char __user *ubuf,
3788 size_t cnt, loff_t *ppos)
3790 return simple_read_from_buffer(ubuf, cnt, ppos,
3791 readme_msg, strlen(readme_msg));
3794 static const struct file_operations tracing_readme_fops = {
3795 .open = tracing_open_generic,
3796 .read = tracing_readme_read,
3797 .llseek = generic_file_llseek,
3800 static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos)
3802 unsigned int *ptr = v;
3804 if (*pos || m->count)
3805 ptr++;
3807 (*pos)++;
3809 for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num];
3810 ptr++) {
3811 if (*ptr == -1 || *ptr == NO_CMDLINE_MAP)
3812 continue;
3814 return ptr;
3817 return NULL;
3820 static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos)
3822 void *v;
3823 loff_t l = 0;
3825 preempt_disable();
3826 arch_spin_lock(&trace_cmdline_lock);
3828 v = &savedcmd->map_cmdline_to_pid[0];
3829 while (l <= *pos) {
3830 v = saved_cmdlines_next(m, v, &l);
3831 if (!v)
3832 return NULL;
3835 return v;
3838 static void saved_cmdlines_stop(struct seq_file *m, void *v)
3840 arch_spin_unlock(&trace_cmdline_lock);
3841 preempt_enable();
3844 static int saved_cmdlines_show(struct seq_file *m, void *v)
3846 char buf[TASK_COMM_LEN];
3847 unsigned int *pid = v;
3849 __trace_find_cmdline(*pid, buf);
3850 seq_printf(m, "%d %s\n", *pid, buf);
3851 return 0;
3854 static const struct seq_operations tracing_saved_cmdlines_seq_ops = {
3855 .start = saved_cmdlines_start,
3856 .next = saved_cmdlines_next,
3857 .stop = saved_cmdlines_stop,
3858 .show = saved_cmdlines_show,
3861 static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
3863 if (tracing_disabled)
3864 return -ENODEV;
3866 return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
3869 static const struct file_operations tracing_saved_cmdlines_fops = {
3870 .open = tracing_saved_cmdlines_open,
3871 .read = seq_read,
3872 .llseek = seq_lseek,
3873 .release = seq_release,
3876 static ssize_t
3877 tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf,
3878 size_t cnt, loff_t *ppos)
3880 char buf[64];
3881 int r;
3883 arch_spin_lock(&trace_cmdline_lock);
3884 r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
3885 arch_spin_unlock(&trace_cmdline_lock);
3887 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3890 static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
3892 kfree(s->saved_cmdlines);
3893 kfree(s->map_cmdline_to_pid);
3894 kfree(s);
3897 static int tracing_resize_saved_cmdlines(unsigned int val)
3899 struct saved_cmdlines_buffer *s, *savedcmd_temp;
3901 s = kmalloc(sizeof(*s), GFP_KERNEL);
3902 if (!s)
3903 return -ENOMEM;
3905 if (allocate_cmdlines_buffer(val, s) < 0) {
3906 kfree(s);
3907 return -ENOMEM;
3910 arch_spin_lock(&trace_cmdline_lock);
3911 savedcmd_temp = savedcmd;
3912 savedcmd = s;
3913 arch_spin_unlock(&trace_cmdline_lock);
3914 free_saved_cmdlines_buffer(savedcmd_temp);
3916 return 0;
3919 static ssize_t
3920 tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf,
3921 size_t cnt, loff_t *ppos)
3923 unsigned long val;
3924 int ret;
3926 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3927 if (ret)
3928 return ret;
3930 /* must have at least 1 entry or less than PID_MAX_DEFAULT */
3931 if (!val || val > PID_MAX_DEFAULT)
3932 return -EINVAL;
3934 ret = tracing_resize_saved_cmdlines((unsigned int)val);
3935 if (ret < 0)
3936 return ret;
3938 *ppos += cnt;
3940 return cnt;
3943 static const struct file_operations tracing_saved_cmdlines_size_fops = {
3944 .open = tracing_open_generic,
3945 .read = tracing_saved_cmdlines_size_read,
3946 .write = tracing_saved_cmdlines_size_write,
3949 #ifdef CONFIG_TRACE_ENUM_MAP_FILE
3950 static union trace_enum_map_item *
3951 update_enum_map(union trace_enum_map_item *ptr)
3953 if (!ptr->map.enum_string) {
3954 if (ptr->tail.next) {
3955 ptr = ptr->tail.next;
3956 /* Set ptr to the next real item (skip head) */
3957 ptr++;
3958 } else
3959 return NULL;
3961 return ptr;
3964 static void *enum_map_next(struct seq_file *m, void *v, loff_t *pos)
3966 union trace_enum_map_item *ptr = v;
3969 * Paranoid! If ptr points to end, we don't want to increment past it.
3970 * This really should never happen.
3972 ptr = update_enum_map(ptr);
3973 if (WARN_ON_ONCE(!ptr))
3974 return NULL;
3976 ptr++;
3978 (*pos)++;
3980 ptr = update_enum_map(ptr);
3982 return ptr;
3985 static void *enum_map_start(struct seq_file *m, loff_t *pos)
3987 union trace_enum_map_item *v;
3988 loff_t l = 0;
3990 mutex_lock(&trace_enum_mutex);
3992 v = trace_enum_maps;
3993 if (v)
3994 v++;
3996 while (v && l < *pos) {
3997 v = enum_map_next(m, v, &l);
4000 return v;
4003 static void enum_map_stop(struct seq_file *m, void *v)
4005 mutex_unlock(&trace_enum_mutex);
4008 static int enum_map_show(struct seq_file *m, void *v)
4010 union trace_enum_map_item *ptr = v;
4012 seq_printf(m, "%s %ld (%s)\n",
4013 ptr->map.enum_string, ptr->map.enum_value,
4014 ptr->map.system);
4016 return 0;
4019 static const struct seq_operations tracing_enum_map_seq_ops = {
4020 .start = enum_map_start,
4021 .next = enum_map_next,
4022 .stop = enum_map_stop,
4023 .show = enum_map_show,
4026 static int tracing_enum_map_open(struct inode *inode, struct file *filp)
4028 if (tracing_disabled)
4029 return -ENODEV;
4031 return seq_open(filp, &tracing_enum_map_seq_ops);
4034 static const struct file_operations tracing_enum_map_fops = {
4035 .open = tracing_enum_map_open,
4036 .read = seq_read,
4037 .llseek = seq_lseek,
4038 .release = seq_release,
4041 static inline union trace_enum_map_item *
4042 trace_enum_jmp_to_tail(union trace_enum_map_item *ptr)
4044 /* Return tail of array given the head */
4045 return ptr + ptr->head.length + 1;
4048 static void
4049 trace_insert_enum_map_file(struct module *mod, struct trace_enum_map **start,
4050 int len)
4052 struct trace_enum_map **stop;
4053 struct trace_enum_map **map;
4054 union trace_enum_map_item *map_array;
4055 union trace_enum_map_item *ptr;
4057 stop = start + len;
4060 * The trace_enum_maps contains the map plus a head and tail item,
4061 * where the head holds the module and length of array, and the
4062 * tail holds a pointer to the next list.
4064 map_array = kmalloc(sizeof(*map_array) * (len + 2), GFP_KERNEL);
4065 if (!map_array) {
4066 pr_warning("Unable to allocate trace enum mapping\n");
4067 return;
4070 mutex_lock(&trace_enum_mutex);
4072 if (!trace_enum_maps)
4073 trace_enum_maps = map_array;
4074 else {
4075 ptr = trace_enum_maps;
4076 for (;;) {
4077 ptr = trace_enum_jmp_to_tail(ptr);
4078 if (!ptr->tail.next)
4079 break;
4080 ptr = ptr->tail.next;
4083 ptr->tail.next = map_array;
4085 map_array->head.mod = mod;
4086 map_array->head.length = len;
4087 map_array++;
4089 for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
4090 map_array->map = **map;
4091 map_array++;
4093 memset(map_array, 0, sizeof(*map_array));
4095 mutex_unlock(&trace_enum_mutex);
4098 static void trace_create_enum_file(struct dentry *d_tracer)
4100 trace_create_file("enum_map", 0444, d_tracer,
4101 NULL, &tracing_enum_map_fops);
4104 #else /* CONFIG_TRACE_ENUM_MAP_FILE */
4105 static inline void trace_create_enum_file(struct dentry *d_tracer) { }
4106 static inline void trace_insert_enum_map_file(struct module *mod,
4107 struct trace_enum_map **start, int len) { }
4108 #endif /* !CONFIG_TRACE_ENUM_MAP_FILE */
4110 static void trace_insert_enum_map(struct module *mod,
4111 struct trace_enum_map **start, int len)
4113 struct trace_enum_map **map;
4115 if (len <= 0)
4116 return;
4118 map = start;
4120 trace_event_enum_update(map, len);
4122 trace_insert_enum_map_file(mod, start, len);
4125 static ssize_t
4126 tracing_set_trace_read(struct file *filp, char __user *ubuf,
4127 size_t cnt, loff_t *ppos)
4129 struct trace_array *tr = filp->private_data;
4130 char buf[MAX_TRACER_SIZE+2];
4131 int r;
4133 mutex_lock(&trace_types_lock);
4134 r = sprintf(buf, "%s\n", tr->current_trace->name);
4135 mutex_unlock(&trace_types_lock);
4137 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4140 int tracer_init(struct tracer *t, struct trace_array *tr)
4142 tracing_reset_online_cpus(&tr->trace_buffer);
4143 return t->init(tr);
4146 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
4148 int cpu;
4150 for_each_tracing_cpu(cpu)
4151 per_cpu_ptr(buf->data, cpu)->entries = val;
4154 #ifdef CONFIG_TRACER_MAX_TRACE
4155 /* resize @tr's buffer to the size of @size_tr's entries */
4156 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
4157 struct trace_buffer *size_buf, int cpu_id)
4159 int cpu, ret = 0;
4161 if (cpu_id == RING_BUFFER_ALL_CPUS) {
4162 for_each_tracing_cpu(cpu) {
4163 ret = ring_buffer_resize(trace_buf->buffer,
4164 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
4165 if (ret < 0)
4166 break;
4167 per_cpu_ptr(trace_buf->data, cpu)->entries =
4168 per_cpu_ptr(size_buf->data, cpu)->entries;
4170 } else {
4171 ret = ring_buffer_resize(trace_buf->buffer,
4172 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
4173 if (ret == 0)
4174 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
4175 per_cpu_ptr(size_buf->data, cpu_id)->entries;
4178 return ret;
4180 #endif /* CONFIG_TRACER_MAX_TRACE */
4182 static int __tracing_resize_ring_buffer(struct trace_array *tr,
4183 unsigned long size, int cpu)
4185 int ret;
4188 * If kernel or user changes the size of the ring buffer
4189 * we use the size that was given, and we can forget about
4190 * expanding it later.
4192 ring_buffer_expanded = true;
4194 /* May be called before buffers are initialized */
4195 if (!tr->trace_buffer.buffer)
4196 return 0;
4198 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
4199 if (ret < 0)
4200 return ret;
4202 #ifdef CONFIG_TRACER_MAX_TRACE
4203 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
4204 !tr->current_trace->use_max_tr)
4205 goto out;
4207 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
4208 if (ret < 0) {
4209 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
4210 &tr->trace_buffer, cpu);
4211 if (r < 0) {
4213 * AARGH! We are left with different
4214 * size max buffer!!!!
4215 * The max buffer is our "snapshot" buffer.
4216 * When a tracer needs a snapshot (one of the
4217 * latency tracers), it swaps the max buffer
4218 * with the saved snap shot. We succeeded to
4219 * update the size of the main buffer, but failed to
4220 * update the size of the max buffer. But when we tried
4221 * to reset the main buffer to the original size, we
4222 * failed there too. This is very unlikely to
4223 * happen, but if it does, warn and kill all
4224 * tracing.
4226 WARN_ON(1);
4227 tracing_disabled = 1;
4229 return ret;
4232 if (cpu == RING_BUFFER_ALL_CPUS)
4233 set_buffer_entries(&tr->max_buffer, size);
4234 else
4235 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
4237 out:
4238 #endif /* CONFIG_TRACER_MAX_TRACE */
4240 if (cpu == RING_BUFFER_ALL_CPUS)
4241 set_buffer_entries(&tr->trace_buffer, size);
4242 else
4243 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
4245 return ret;
4248 static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
4249 unsigned long size, int cpu_id)
4251 int ret = size;
4253 mutex_lock(&trace_types_lock);
4255 if (cpu_id != RING_BUFFER_ALL_CPUS) {
4256 /* make sure, this cpu is enabled in the mask */
4257 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
4258 ret = -EINVAL;
4259 goto out;
4263 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
4264 if (ret < 0)
4265 ret = -ENOMEM;
4267 out:
4268 mutex_unlock(&trace_types_lock);
4270 return ret;
4275 * tracing_update_buffers - used by tracing facility to expand ring buffers
4277 * To save on memory when the tracing is never used on a system with it
4278 * configured in. The ring buffers are set to a minimum size. But once
4279 * a user starts to use the tracing facility, then they need to grow
4280 * to their default size.
4282 * This function is to be called when a tracer is about to be used.
4284 int tracing_update_buffers(void)
4286 int ret = 0;
4288 mutex_lock(&trace_types_lock);
4289 if (!ring_buffer_expanded)
4290 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
4291 RING_BUFFER_ALL_CPUS);
4292 mutex_unlock(&trace_types_lock);
4294 return ret;
4297 struct trace_option_dentry;
4299 static struct trace_option_dentry *
4300 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
4302 static void
4303 destroy_trace_option_files(struct trace_option_dentry *topts);
4306 * Used to clear out the tracer before deletion of an instance.
4307 * Must have trace_types_lock held.
4309 static void tracing_set_nop(struct trace_array *tr)
4311 if (tr->current_trace == &nop_trace)
4312 return;
4314 tr->current_trace->enabled--;
4316 if (tr->current_trace->reset)
4317 tr->current_trace->reset(tr);
4319 tr->current_trace = &nop_trace;
4322 static void update_tracer_options(struct trace_array *tr, struct tracer *t)
4324 static struct trace_option_dentry *topts;
4326 /* Only enable if the directory has been created already. */
4327 if (!tr->dir)
4328 return;
4330 /* Currently, only the top instance has options */
4331 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL))
4332 return;
4334 destroy_trace_option_files(topts);
4335 topts = create_trace_option_files(tr, t);
4338 static int tracing_set_tracer(struct trace_array *tr, const char *buf)
4340 struct tracer *t;
4341 #ifdef CONFIG_TRACER_MAX_TRACE
4342 bool had_max_tr;
4343 #endif
4344 int ret = 0;
4346 mutex_lock(&trace_types_lock);
4348 if (!ring_buffer_expanded) {
4349 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
4350 RING_BUFFER_ALL_CPUS);
4351 if (ret < 0)
4352 goto out;
4353 ret = 0;
4356 for (t = trace_types; t; t = t->next) {
4357 if (strcmp(t->name, buf) == 0)
4358 break;
4360 if (!t) {
4361 ret = -EINVAL;
4362 goto out;
4364 if (t == tr->current_trace)
4365 goto out;
4367 /* Some tracers are only allowed for the top level buffer */
4368 if (!trace_ok_for_array(t, tr)) {
4369 ret = -EINVAL;
4370 goto out;
4373 /* If trace pipe files are being read, we can't change the tracer */
4374 if (tr->current_trace->ref) {
4375 ret = -EBUSY;
4376 goto out;
4379 trace_branch_disable();
4381 tr->current_trace->enabled--;
4383 if (tr->current_trace->reset)
4384 tr->current_trace->reset(tr);
4386 /* Current trace needs to be nop_trace before synchronize_sched */
4387 tr->current_trace = &nop_trace;
4389 #ifdef CONFIG_TRACER_MAX_TRACE
4390 had_max_tr = tr->allocated_snapshot;
4392 if (had_max_tr && !t->use_max_tr) {
4394 * We need to make sure that the update_max_tr sees that
4395 * current_trace changed to nop_trace to keep it from
4396 * swapping the buffers after we resize it.
4397 * The update_max_tr is called from interrupts disabled
4398 * so a synchronized_sched() is sufficient.
4400 synchronize_sched();
4401 free_snapshot(tr);
4403 #endif
4404 update_tracer_options(tr, t);
4406 #ifdef CONFIG_TRACER_MAX_TRACE
4407 if (t->use_max_tr && !had_max_tr) {
4408 ret = alloc_snapshot(tr);
4409 if (ret < 0)
4410 goto out;
4412 #endif
4414 if (t->init) {
4415 ret = tracer_init(t, tr);
4416 if (ret)
4417 goto out;
4420 tr->current_trace = t;
4421 tr->current_trace->enabled++;
4422 trace_branch_enable(tr);
4423 out:
4424 mutex_unlock(&trace_types_lock);
4426 return ret;
4429 static ssize_t
4430 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
4431 size_t cnt, loff_t *ppos)
4433 struct trace_array *tr = filp->private_data;
4434 char buf[MAX_TRACER_SIZE+1];
4435 int i;
4436 size_t ret;
4437 int err;
4439 ret = cnt;
4441 if (cnt > MAX_TRACER_SIZE)
4442 cnt = MAX_TRACER_SIZE;
4444 if (copy_from_user(&buf, ubuf, cnt))
4445 return -EFAULT;
4447 buf[cnt] = 0;
4449 /* strip ending whitespace. */
4450 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
4451 buf[i] = 0;
4453 err = tracing_set_tracer(tr, buf);
4454 if (err)
4455 return err;
4457 *ppos += ret;
4459 return ret;
4462 static ssize_t
4463 tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
4464 size_t cnt, loff_t *ppos)
4466 char buf[64];
4467 int r;
4469 r = snprintf(buf, sizeof(buf), "%ld\n",
4470 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
4471 if (r > sizeof(buf))
4472 r = sizeof(buf);
4473 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4476 static ssize_t
4477 tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
4478 size_t cnt, loff_t *ppos)
4480 unsigned long val;
4481 int ret;
4483 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4484 if (ret)
4485 return ret;
4487 *ptr = val * 1000;
4489 return cnt;
4492 static ssize_t
4493 tracing_thresh_read(struct file *filp, char __user *ubuf,
4494 size_t cnt, loff_t *ppos)
4496 return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
4499 static ssize_t
4500 tracing_thresh_write(struct file *filp, const char __user *ubuf,
4501 size_t cnt, loff_t *ppos)
4503 struct trace_array *tr = filp->private_data;
4504 int ret;
4506 mutex_lock(&trace_types_lock);
4507 ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
4508 if (ret < 0)
4509 goto out;
4511 if (tr->current_trace->update_thresh) {
4512 ret = tr->current_trace->update_thresh(tr);
4513 if (ret < 0)
4514 goto out;
4517 ret = cnt;
4518 out:
4519 mutex_unlock(&trace_types_lock);
4521 return ret;
4524 static ssize_t
4525 tracing_max_lat_read(struct file *filp, char __user *ubuf,
4526 size_t cnt, loff_t *ppos)
4528 return tracing_nsecs_read(filp->private_data, ubuf, cnt, ppos);
4531 static ssize_t
4532 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
4533 size_t cnt, loff_t *ppos)
4535 return tracing_nsecs_write(filp->private_data, ubuf, cnt, ppos);
4538 static int tracing_open_pipe(struct inode *inode, struct file *filp)
4540 struct trace_array *tr = inode->i_private;
4541 struct trace_iterator *iter;
4542 int ret = 0;
4544 if (tracing_disabled)
4545 return -ENODEV;
4547 if (trace_array_get(tr) < 0)
4548 return -ENODEV;
4550 mutex_lock(&trace_types_lock);
4552 /* create a buffer to store the information to pass to userspace */
4553 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4554 if (!iter) {
4555 ret = -ENOMEM;
4556 __trace_array_put(tr);
4557 goto out;
4560 trace_seq_init(&iter->seq);
4561 iter->trace = tr->current_trace;
4563 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
4564 ret = -ENOMEM;
4565 goto fail;
4568 /* trace pipe does not show start of buffer */
4569 cpumask_setall(iter->started);
4571 if (trace_flags & TRACE_ITER_LATENCY_FMT)
4572 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4574 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
4575 if (trace_clocks[tr->clock_id].in_ns)
4576 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4578 iter->tr = tr;
4579 iter->trace_buffer = &tr->trace_buffer;
4580 iter->cpu_file = tracing_get_cpu(inode);
4581 mutex_init(&iter->mutex);
4582 filp->private_data = iter;
4584 if (iter->trace->pipe_open)
4585 iter->trace->pipe_open(iter);
4587 nonseekable_open(inode, filp);
4589 tr->current_trace->ref++;
4590 out:
4591 mutex_unlock(&trace_types_lock);
4592 return ret;
4594 fail:
4595 kfree(iter->trace);
4596 kfree(iter);
4597 __trace_array_put(tr);
4598 mutex_unlock(&trace_types_lock);
4599 return ret;
4602 static int tracing_release_pipe(struct inode *inode, struct file *file)
4604 struct trace_iterator *iter = file->private_data;
4605 struct trace_array *tr = inode->i_private;
4607 mutex_lock(&trace_types_lock);
4609 tr->current_trace->ref--;
4611 if (iter->trace->pipe_close)
4612 iter->trace->pipe_close(iter);
4614 mutex_unlock(&trace_types_lock);
4616 free_cpumask_var(iter->started);
4617 mutex_destroy(&iter->mutex);
4618 kfree(iter);
4620 trace_array_put(tr);
4622 return 0;
4625 static unsigned int
4626 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
4628 /* Iterators are static, they should be filled or empty */
4629 if (trace_buffer_iter(iter, iter->cpu_file))
4630 return POLLIN | POLLRDNORM;
4632 if (trace_flags & TRACE_ITER_BLOCK)
4634 * Always select as readable when in blocking mode
4636 return POLLIN | POLLRDNORM;
4637 else
4638 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
4639 filp, poll_table);
4642 static unsigned int
4643 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
4645 struct trace_iterator *iter = filp->private_data;
4647 return trace_poll(iter, filp, poll_table);
4650 /* Must be called with iter->mutex held. */
4651 static int tracing_wait_pipe(struct file *filp)
4653 struct trace_iterator *iter = filp->private_data;
4654 int ret;
4656 while (trace_empty(iter)) {
4658 if ((filp->f_flags & O_NONBLOCK)) {
4659 return -EAGAIN;
4663 * We block until we read something and tracing is disabled.
4664 * We still block if tracing is disabled, but we have never
4665 * read anything. This allows a user to cat this file, and
4666 * then enable tracing. But after we have read something,
4667 * we give an EOF when tracing is again disabled.
4669 * iter->pos will be 0 if we haven't read anything.
4671 if (!tracing_is_on() && iter->pos)
4672 break;
4674 mutex_unlock(&iter->mutex);
4676 ret = wait_on_pipe(iter, false);
4678 mutex_lock(&iter->mutex);
4680 if (ret)
4681 return ret;
4684 return 1;
4688 * Consumer reader.
4690 static ssize_t
4691 tracing_read_pipe(struct file *filp, char __user *ubuf,
4692 size_t cnt, loff_t *ppos)
4694 struct trace_iterator *iter = filp->private_data;
4695 ssize_t sret;
4697 /* return any leftover data */
4698 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4699 if (sret != -EBUSY)
4700 return sret;
4702 trace_seq_init(&iter->seq);
4705 * Avoid more than one consumer on a single file descriptor
4706 * This is just a matter of traces coherency, the ring buffer itself
4707 * is protected.
4709 mutex_lock(&iter->mutex);
4710 if (iter->trace->read) {
4711 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
4712 if (sret)
4713 goto out;
4716 waitagain:
4717 sret = tracing_wait_pipe(filp);
4718 if (sret <= 0)
4719 goto out;
4721 /* stop when tracing is finished */
4722 if (trace_empty(iter)) {
4723 sret = 0;
4724 goto out;
4727 if (cnt >= PAGE_SIZE)
4728 cnt = PAGE_SIZE - 1;
4730 /* reset all but tr, trace, and overruns */
4731 memset(&iter->seq, 0,
4732 sizeof(struct trace_iterator) -
4733 offsetof(struct trace_iterator, seq));
4734 cpumask_clear(iter->started);
4735 iter->pos = -1;
4737 trace_event_read_lock();
4738 trace_access_lock(iter->cpu_file);
4739 while (trace_find_next_entry_inc(iter) != NULL) {
4740 enum print_line_t ret;
4741 int save_len = iter->seq.seq.len;
4743 ret = print_trace_line(iter);
4744 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4745 /* don't print partial lines */
4746 iter->seq.seq.len = save_len;
4747 break;
4749 if (ret != TRACE_TYPE_NO_CONSUME)
4750 trace_consume(iter);
4752 if (trace_seq_used(&iter->seq) >= cnt)
4753 break;
4756 * Setting the full flag means we reached the trace_seq buffer
4757 * size and we should leave by partial output condition above.
4758 * One of the trace_seq_* functions is not used properly.
4760 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
4761 iter->ent->type);
4763 trace_access_unlock(iter->cpu_file);
4764 trace_event_read_unlock();
4766 /* Now copy what we have to the user */
4767 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4768 if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq))
4769 trace_seq_init(&iter->seq);
4772 * If there was nothing to send to user, in spite of consuming trace
4773 * entries, go back to wait for more entries.
4775 if (sret == -EBUSY)
4776 goto waitagain;
4778 out:
4779 mutex_unlock(&iter->mutex);
4781 return sret;
4784 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
4785 unsigned int idx)
4787 __free_page(spd->pages[idx]);
4790 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
4791 .can_merge = 0,
4792 .confirm = generic_pipe_buf_confirm,
4793 .release = generic_pipe_buf_release,
4794 .steal = generic_pipe_buf_steal,
4795 .get = generic_pipe_buf_get,
4798 static size_t
4799 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
4801 size_t count;
4802 int save_len;
4803 int ret;
4805 /* Seq buffer is page-sized, exactly what we need. */
4806 for (;;) {
4807 save_len = iter->seq.seq.len;
4808 ret = print_trace_line(iter);
4810 if (trace_seq_has_overflowed(&iter->seq)) {
4811 iter->seq.seq.len = save_len;
4812 break;
4816 * This should not be hit, because it should only
4817 * be set if the iter->seq overflowed. But check it
4818 * anyway to be safe.
4820 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4821 iter->seq.seq.len = save_len;
4822 break;
4825 count = trace_seq_used(&iter->seq) - save_len;
4826 if (rem < count) {
4827 rem = 0;
4828 iter->seq.seq.len = save_len;
4829 break;
4832 if (ret != TRACE_TYPE_NO_CONSUME)
4833 trace_consume(iter);
4834 rem -= count;
4835 if (!trace_find_next_entry_inc(iter)) {
4836 rem = 0;
4837 iter->ent = NULL;
4838 break;
4842 return rem;
4845 static ssize_t tracing_splice_read_pipe(struct file *filp,
4846 loff_t *ppos,
4847 struct pipe_inode_info *pipe,
4848 size_t len,
4849 unsigned int flags)
4851 struct page *pages_def[PIPE_DEF_BUFFERS];
4852 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4853 struct trace_iterator *iter = filp->private_data;
4854 struct splice_pipe_desc spd = {
4855 .pages = pages_def,
4856 .partial = partial_def,
4857 .nr_pages = 0, /* This gets updated below. */
4858 .nr_pages_max = PIPE_DEF_BUFFERS,
4859 .flags = flags,
4860 .ops = &tracing_pipe_buf_ops,
4861 .spd_release = tracing_spd_release_pipe,
4863 ssize_t ret;
4864 size_t rem;
4865 unsigned int i;
4867 if (splice_grow_spd(pipe, &spd))
4868 return -ENOMEM;
4870 mutex_lock(&iter->mutex);
4872 if (iter->trace->splice_read) {
4873 ret = iter->trace->splice_read(iter, filp,
4874 ppos, pipe, len, flags);
4875 if (ret)
4876 goto out_err;
4879 ret = tracing_wait_pipe(filp);
4880 if (ret <= 0)
4881 goto out_err;
4883 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
4884 ret = -EFAULT;
4885 goto out_err;
4888 trace_event_read_lock();
4889 trace_access_lock(iter->cpu_file);
4891 /* Fill as many pages as possible. */
4892 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
4893 spd.pages[i] = alloc_page(GFP_KERNEL);
4894 if (!spd.pages[i])
4895 break;
4897 rem = tracing_fill_pipe_page(rem, iter);
4899 /* Copy the data into the page, so we can start over. */
4900 ret = trace_seq_to_buffer(&iter->seq,
4901 page_address(spd.pages[i]),
4902 trace_seq_used(&iter->seq));
4903 if (ret < 0) {
4904 __free_page(spd.pages[i]);
4905 break;
4907 spd.partial[i].offset = 0;
4908 spd.partial[i].len = trace_seq_used(&iter->seq);
4910 trace_seq_init(&iter->seq);
4913 trace_access_unlock(iter->cpu_file);
4914 trace_event_read_unlock();
4915 mutex_unlock(&iter->mutex);
4917 spd.nr_pages = i;
4919 ret = splice_to_pipe(pipe, &spd);
4920 out:
4921 splice_shrink_spd(&spd);
4922 return ret;
4924 out_err:
4925 mutex_unlock(&iter->mutex);
4926 goto out;
4929 static ssize_t
4930 tracing_entries_read(struct file *filp, char __user *ubuf,
4931 size_t cnt, loff_t *ppos)
4933 struct inode *inode = file_inode(filp);
4934 struct trace_array *tr = inode->i_private;
4935 int cpu = tracing_get_cpu(inode);
4936 char buf[64];
4937 int r = 0;
4938 ssize_t ret;
4940 mutex_lock(&trace_types_lock);
4942 if (cpu == RING_BUFFER_ALL_CPUS) {
4943 int cpu, buf_size_same;
4944 unsigned long size;
4946 size = 0;
4947 buf_size_same = 1;
4948 /* check if all cpu sizes are same */
4949 for_each_tracing_cpu(cpu) {
4950 /* fill in the size from first enabled cpu */
4951 if (size == 0)
4952 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4953 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
4954 buf_size_same = 0;
4955 break;
4959 if (buf_size_same) {
4960 if (!ring_buffer_expanded)
4961 r = sprintf(buf, "%lu (expanded: %lu)\n",
4962 size >> 10,
4963 trace_buf_size >> 10);
4964 else
4965 r = sprintf(buf, "%lu\n", size >> 10);
4966 } else
4967 r = sprintf(buf, "X\n");
4968 } else
4969 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
4971 mutex_unlock(&trace_types_lock);
4973 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4974 return ret;
4977 static ssize_t
4978 tracing_entries_write(struct file *filp, const char __user *ubuf,
4979 size_t cnt, loff_t *ppos)
4981 struct inode *inode = file_inode(filp);
4982 struct trace_array *tr = inode->i_private;
4983 unsigned long val;
4984 int ret;
4986 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4987 if (ret)
4988 return ret;
4990 /* must have at least 1 entry */
4991 if (!val)
4992 return -EINVAL;
4994 /* value is in KB */
4995 val <<= 10;
4996 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
4997 if (ret < 0)
4998 return ret;
5000 *ppos += cnt;
5002 return cnt;
5005 static ssize_t
5006 tracing_total_entries_read(struct file *filp, char __user *ubuf,
5007 size_t cnt, loff_t *ppos)
5009 struct trace_array *tr = filp->private_data;
5010 char buf[64];
5011 int r, cpu;
5012 unsigned long size = 0, expanded_size = 0;
5014 mutex_lock(&trace_types_lock);
5015 for_each_tracing_cpu(cpu) {
5016 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
5017 if (!ring_buffer_expanded)
5018 expanded_size += trace_buf_size >> 10;
5020 if (ring_buffer_expanded)
5021 r = sprintf(buf, "%lu\n", size);
5022 else
5023 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
5024 mutex_unlock(&trace_types_lock);
5026 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5029 static ssize_t
5030 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
5031 size_t cnt, loff_t *ppos)
5034 * There is no need to read what the user has written, this function
5035 * is just to make sure that there is no error when "echo" is used
5038 *ppos += cnt;
5040 return cnt;
5043 static int
5044 tracing_free_buffer_release(struct inode *inode, struct file *filp)
5046 struct trace_array *tr = inode->i_private;
5048 /* disable tracing ? */
5049 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
5050 tracer_tracing_off(tr);
5051 /* resize the ring buffer to 0 */
5052 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
5054 trace_array_put(tr);
5056 return 0;
5059 static ssize_t
5060 tracing_mark_write(struct file *filp, const char __user *ubuf,
5061 size_t cnt, loff_t *fpos)
5063 unsigned long addr = (unsigned long)ubuf;
5064 struct trace_array *tr = filp->private_data;
5065 struct ring_buffer_event *event;
5066 struct ring_buffer *buffer;
5067 struct print_entry *entry;
5068 unsigned long irq_flags;
5069 struct page *pages[2];
5070 void *map_page[2];
5071 int nr_pages = 1;
5072 ssize_t written;
5073 int offset;
5074 int size;
5075 int len;
5076 int ret;
5077 int i;
5079 if (tracing_disabled)
5080 return -EINVAL;
5082 if (!(trace_flags & TRACE_ITER_MARKERS))
5083 return -EINVAL;
5085 if (cnt > TRACE_BUF_SIZE)
5086 cnt = TRACE_BUF_SIZE;
5089 * Userspace is injecting traces into the kernel trace buffer.
5090 * We want to be as non intrusive as possible.
5091 * To do so, we do not want to allocate any special buffers
5092 * or take any locks, but instead write the userspace data
5093 * straight into the ring buffer.
5095 * First we need to pin the userspace buffer into memory,
5096 * which, most likely it is, because it just referenced it.
5097 * But there's no guarantee that it is. By using get_user_pages_fast()
5098 * and kmap_atomic/kunmap_atomic() we can get access to the
5099 * pages directly. We then write the data directly into the
5100 * ring buffer.
5102 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
5104 /* check if we cross pages */
5105 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
5106 nr_pages = 2;
5108 offset = addr & (PAGE_SIZE - 1);
5109 addr &= PAGE_MASK;
5111 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
5112 if (ret < nr_pages) {
5113 while (--ret >= 0)
5114 put_page(pages[ret]);
5115 written = -EFAULT;
5116 goto out;
5119 for (i = 0; i < nr_pages; i++)
5120 map_page[i] = kmap_atomic(pages[i]);
5122 local_save_flags(irq_flags);
5123 size = sizeof(*entry) + cnt + 2; /* possible \n added */
5124 buffer = tr->trace_buffer.buffer;
5125 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
5126 irq_flags, preempt_count());
5127 if (!event) {
5128 /* Ring buffer disabled, return as if not open for write */
5129 written = -EBADF;
5130 goto out_unlock;
5133 entry = ring_buffer_event_data(event);
5134 entry->ip = _THIS_IP_;
5136 if (nr_pages == 2) {
5137 len = PAGE_SIZE - offset;
5138 memcpy(&entry->buf, map_page[0] + offset, len);
5139 memcpy(&entry->buf[len], map_page[1], cnt - len);
5140 } else
5141 memcpy(&entry->buf, map_page[0] + offset, cnt);
5143 if (entry->buf[cnt - 1] != '\n') {
5144 entry->buf[cnt] = '\n';
5145 entry->buf[cnt + 1] = '\0';
5146 } else
5147 entry->buf[cnt] = '\0';
5149 __buffer_unlock_commit(buffer, event);
5151 written = cnt;
5153 *fpos += written;
5155 out_unlock:
5156 for (i = nr_pages - 1; i >= 0; i--) {
5157 kunmap_atomic(map_page[i]);
5158 put_page(pages[i]);
5160 out:
5161 return written;
5164 static int tracing_clock_show(struct seq_file *m, void *v)
5166 struct trace_array *tr = m->private;
5167 int i;
5169 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
5170 seq_printf(m,
5171 "%s%s%s%s", i ? " " : "",
5172 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
5173 i == tr->clock_id ? "]" : "");
5174 seq_putc(m, '\n');
5176 return 0;
5179 static int tracing_set_clock(struct trace_array *tr, const char *clockstr)
5181 int i;
5183 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
5184 if (strcmp(trace_clocks[i].name, clockstr) == 0)
5185 break;
5187 if (i == ARRAY_SIZE(trace_clocks))
5188 return -EINVAL;
5190 mutex_lock(&trace_types_lock);
5192 tr->clock_id = i;
5194 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
5197 * New clock may not be consistent with the previous clock.
5198 * Reset the buffer so that it doesn't have incomparable timestamps.
5200 tracing_reset_online_cpus(&tr->trace_buffer);
5202 #ifdef CONFIG_TRACER_MAX_TRACE
5203 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
5204 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
5205 tracing_reset_online_cpus(&tr->max_buffer);
5206 #endif
5208 mutex_unlock(&trace_types_lock);
5210 return 0;
5213 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
5214 size_t cnt, loff_t *fpos)
5216 struct seq_file *m = filp->private_data;
5217 struct trace_array *tr = m->private;
5218 char buf[64];
5219 const char *clockstr;
5220 int ret;
5222 if (cnt >= sizeof(buf))
5223 return -EINVAL;
5225 if (copy_from_user(&buf, ubuf, cnt))
5226 return -EFAULT;
5228 buf[cnt] = 0;
5230 clockstr = strstrip(buf);
5232 ret = tracing_set_clock(tr, clockstr);
5233 if (ret)
5234 return ret;
5236 *fpos += cnt;
5238 return cnt;
5241 static int tracing_clock_open(struct inode *inode, struct file *file)
5243 struct trace_array *tr = inode->i_private;
5244 int ret;
5246 if (tracing_disabled)
5247 return -ENODEV;
5249 if (trace_array_get(tr))
5250 return -ENODEV;
5252 ret = single_open(file, tracing_clock_show, inode->i_private);
5253 if (ret < 0)
5254 trace_array_put(tr);
5256 return ret;
5259 struct ftrace_buffer_info {
5260 struct trace_iterator iter;
5261 void *spare;
5262 unsigned int read;
5265 #ifdef CONFIG_TRACER_SNAPSHOT
5266 static int tracing_snapshot_open(struct inode *inode, struct file *file)
5268 struct trace_array *tr = inode->i_private;
5269 struct trace_iterator *iter;
5270 struct seq_file *m;
5271 int ret = 0;
5273 if (trace_array_get(tr) < 0)
5274 return -ENODEV;
5276 if (file->f_mode & FMODE_READ) {
5277 iter = __tracing_open(inode, file, true);
5278 if (IS_ERR(iter))
5279 ret = PTR_ERR(iter);
5280 } else {
5281 /* Writes still need the seq_file to hold the private data */
5282 ret = -ENOMEM;
5283 m = kzalloc(sizeof(*m), GFP_KERNEL);
5284 if (!m)
5285 goto out;
5286 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
5287 if (!iter) {
5288 kfree(m);
5289 goto out;
5291 ret = 0;
5293 iter->tr = tr;
5294 iter->trace_buffer = &tr->max_buffer;
5295 iter->cpu_file = tracing_get_cpu(inode);
5296 m->private = iter;
5297 file->private_data = m;
5299 out:
5300 if (ret < 0)
5301 trace_array_put(tr);
5303 return ret;
5306 static ssize_t
5307 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
5308 loff_t *ppos)
5310 struct seq_file *m = filp->private_data;
5311 struct trace_iterator *iter = m->private;
5312 struct trace_array *tr = iter->tr;
5313 unsigned long val;
5314 int ret;
5316 ret = tracing_update_buffers();
5317 if (ret < 0)
5318 return ret;
5320 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5321 if (ret)
5322 return ret;
5324 mutex_lock(&trace_types_lock);
5326 if (tr->current_trace->use_max_tr) {
5327 ret = -EBUSY;
5328 goto out;
5331 switch (val) {
5332 case 0:
5333 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
5334 ret = -EINVAL;
5335 break;
5337 if (tr->allocated_snapshot)
5338 free_snapshot(tr);
5339 break;
5340 case 1:
5341 /* Only allow per-cpu swap if the ring buffer supports it */
5342 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
5343 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
5344 ret = -EINVAL;
5345 break;
5347 #endif
5348 if (!tr->allocated_snapshot) {
5349 ret = alloc_snapshot(tr);
5350 if (ret < 0)
5351 break;
5353 local_irq_disable();
5354 /* Now, we're going to swap */
5355 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
5356 update_max_tr(tr, current, smp_processor_id());
5357 else
5358 update_max_tr_single(tr, current, iter->cpu_file);
5359 local_irq_enable();
5360 break;
5361 default:
5362 if (tr->allocated_snapshot) {
5363 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
5364 tracing_reset_online_cpus(&tr->max_buffer);
5365 else
5366 tracing_reset(&tr->max_buffer, iter->cpu_file);
5368 break;
5371 if (ret >= 0) {
5372 *ppos += cnt;
5373 ret = cnt;
5375 out:
5376 mutex_unlock(&trace_types_lock);
5377 return ret;
5380 static int tracing_snapshot_release(struct inode *inode, struct file *file)
5382 struct seq_file *m = file->private_data;
5383 int ret;
5385 ret = tracing_release(inode, file);
5387 if (file->f_mode & FMODE_READ)
5388 return ret;
5390 /* If write only, the seq_file is just a stub */
5391 if (m)
5392 kfree(m->private);
5393 kfree(m);
5395 return 0;
5398 static int tracing_buffers_open(struct inode *inode, struct file *filp);
5399 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
5400 size_t count, loff_t *ppos);
5401 static int tracing_buffers_release(struct inode *inode, struct file *file);
5402 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5403 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
5405 static int snapshot_raw_open(struct inode *inode, struct file *filp)
5407 struct ftrace_buffer_info *info;
5408 int ret;
5410 ret = tracing_buffers_open(inode, filp);
5411 if (ret < 0)
5412 return ret;
5414 info = filp->private_data;
5416 if (info->iter.trace->use_max_tr) {
5417 tracing_buffers_release(inode, filp);
5418 return -EBUSY;
5421 info->iter.snapshot = true;
5422 info->iter.trace_buffer = &info->iter.tr->max_buffer;
5424 return ret;
5427 #endif /* CONFIG_TRACER_SNAPSHOT */
5430 static const struct file_operations tracing_thresh_fops = {
5431 .open = tracing_open_generic,
5432 .read = tracing_thresh_read,
5433 .write = tracing_thresh_write,
5434 .llseek = generic_file_llseek,
5437 static const struct file_operations tracing_max_lat_fops = {
5438 .open = tracing_open_generic,
5439 .read = tracing_max_lat_read,
5440 .write = tracing_max_lat_write,
5441 .llseek = generic_file_llseek,
5444 static const struct file_operations set_tracer_fops = {
5445 .open = tracing_open_generic,
5446 .read = tracing_set_trace_read,
5447 .write = tracing_set_trace_write,
5448 .llseek = generic_file_llseek,
5451 static const struct file_operations tracing_pipe_fops = {
5452 .open = tracing_open_pipe,
5453 .poll = tracing_poll_pipe,
5454 .read = tracing_read_pipe,
5455 .splice_read = tracing_splice_read_pipe,
5456 .release = tracing_release_pipe,
5457 .llseek = no_llseek,
5460 static const struct file_operations tracing_entries_fops = {
5461 .open = tracing_open_generic_tr,
5462 .read = tracing_entries_read,
5463 .write = tracing_entries_write,
5464 .llseek = generic_file_llseek,
5465 .release = tracing_release_generic_tr,
5468 static const struct file_operations tracing_total_entries_fops = {
5469 .open = tracing_open_generic_tr,
5470 .read = tracing_total_entries_read,
5471 .llseek = generic_file_llseek,
5472 .release = tracing_release_generic_tr,
5475 static const struct file_operations tracing_free_buffer_fops = {
5476 .open = tracing_open_generic_tr,
5477 .write = tracing_free_buffer_write,
5478 .release = tracing_free_buffer_release,
5481 static const struct file_operations tracing_mark_fops = {
5482 .open = tracing_open_generic_tr,
5483 .write = tracing_mark_write,
5484 .llseek = generic_file_llseek,
5485 .release = tracing_release_generic_tr,
5488 static const struct file_operations trace_clock_fops = {
5489 .open = tracing_clock_open,
5490 .read = seq_read,
5491 .llseek = seq_lseek,
5492 .release = tracing_single_release_tr,
5493 .write = tracing_clock_write,
5496 #ifdef CONFIG_TRACER_SNAPSHOT
5497 static const struct file_operations snapshot_fops = {
5498 .open = tracing_snapshot_open,
5499 .read = seq_read,
5500 .write = tracing_snapshot_write,
5501 .llseek = tracing_lseek,
5502 .release = tracing_snapshot_release,
5505 static const struct file_operations snapshot_raw_fops = {
5506 .open = snapshot_raw_open,
5507 .read = tracing_buffers_read,
5508 .release = tracing_buffers_release,
5509 .splice_read = tracing_buffers_splice_read,
5510 .llseek = no_llseek,
5513 #endif /* CONFIG_TRACER_SNAPSHOT */
5515 static int tracing_buffers_open(struct inode *inode, struct file *filp)
5517 struct trace_array *tr = inode->i_private;
5518 struct ftrace_buffer_info *info;
5519 int ret;
5521 if (tracing_disabled)
5522 return -ENODEV;
5524 if (trace_array_get(tr) < 0)
5525 return -ENODEV;
5527 info = kzalloc(sizeof(*info), GFP_KERNEL);
5528 if (!info) {
5529 trace_array_put(tr);
5530 return -ENOMEM;
5533 mutex_lock(&trace_types_lock);
5535 info->iter.tr = tr;
5536 info->iter.cpu_file = tracing_get_cpu(inode);
5537 info->iter.trace = tr->current_trace;
5538 info->iter.trace_buffer = &tr->trace_buffer;
5539 info->spare = NULL;
5540 /* Force reading ring buffer for first read */
5541 info->read = (unsigned int)-1;
5543 filp->private_data = info;
5545 tr->current_trace->ref++;
5547 mutex_unlock(&trace_types_lock);
5549 ret = nonseekable_open(inode, filp);
5550 if (ret < 0)
5551 trace_array_put(tr);
5553 return ret;
5556 static unsigned int
5557 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
5559 struct ftrace_buffer_info *info = filp->private_data;
5560 struct trace_iterator *iter = &info->iter;
5562 return trace_poll(iter, filp, poll_table);
5565 static ssize_t
5566 tracing_buffers_read(struct file *filp, char __user *ubuf,
5567 size_t count, loff_t *ppos)
5569 struct ftrace_buffer_info *info = filp->private_data;
5570 struct trace_iterator *iter = &info->iter;
5571 ssize_t ret;
5572 ssize_t size;
5574 if (!count)
5575 return 0;
5577 #ifdef CONFIG_TRACER_MAX_TRACE
5578 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
5579 return -EBUSY;
5580 #endif
5582 if (!info->spare)
5583 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
5584 iter->cpu_file);
5585 if (!info->spare)
5586 return -ENOMEM;
5588 /* Do we have previous read data to read? */
5589 if (info->read < PAGE_SIZE)
5590 goto read;
5592 again:
5593 trace_access_lock(iter->cpu_file);
5594 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
5595 &info->spare,
5596 count,
5597 iter->cpu_file, 0);
5598 trace_access_unlock(iter->cpu_file);
5600 if (ret < 0) {
5601 if (trace_empty(iter)) {
5602 if ((filp->f_flags & O_NONBLOCK))
5603 return -EAGAIN;
5605 ret = wait_on_pipe(iter, false);
5606 if (ret)
5607 return ret;
5609 goto again;
5611 return 0;
5614 info->read = 0;
5615 read:
5616 size = PAGE_SIZE - info->read;
5617 if (size > count)
5618 size = count;
5620 ret = copy_to_user(ubuf, info->spare + info->read, size);
5621 if (ret == size)
5622 return -EFAULT;
5624 size -= ret;
5626 *ppos += size;
5627 info->read += size;
5629 return size;
5632 static int tracing_buffers_release(struct inode *inode, struct file *file)
5634 struct ftrace_buffer_info *info = file->private_data;
5635 struct trace_iterator *iter = &info->iter;
5637 mutex_lock(&trace_types_lock);
5639 iter->tr->current_trace->ref--;
5641 __trace_array_put(iter->tr);
5643 if (info->spare)
5644 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
5645 kfree(info);
5647 mutex_unlock(&trace_types_lock);
5649 return 0;
5652 struct buffer_ref {
5653 struct ring_buffer *buffer;
5654 void *page;
5655 int ref;
5658 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
5659 struct pipe_buffer *buf)
5661 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5663 if (--ref->ref)
5664 return;
5666 ring_buffer_free_read_page(ref->buffer, ref->page);
5667 kfree(ref);
5668 buf->private = 0;
5671 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
5672 struct pipe_buffer *buf)
5674 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5676 ref->ref++;
5679 /* Pipe buffer operations for a buffer. */
5680 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
5681 .can_merge = 0,
5682 .confirm = generic_pipe_buf_confirm,
5683 .release = buffer_pipe_buf_release,
5684 .steal = generic_pipe_buf_steal,
5685 .get = buffer_pipe_buf_get,
5689 * Callback from splice_to_pipe(), if we need to release some pages
5690 * at the end of the spd in case we error'ed out in filling the pipe.
5692 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
5694 struct buffer_ref *ref =
5695 (struct buffer_ref *)spd->partial[i].private;
5697 if (--ref->ref)
5698 return;
5700 ring_buffer_free_read_page(ref->buffer, ref->page);
5701 kfree(ref);
5702 spd->partial[i].private = 0;
5705 static ssize_t
5706 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5707 struct pipe_inode_info *pipe, size_t len,
5708 unsigned int flags)
5710 struct ftrace_buffer_info *info = file->private_data;
5711 struct trace_iterator *iter = &info->iter;
5712 struct partial_page partial_def[PIPE_DEF_BUFFERS];
5713 struct page *pages_def[PIPE_DEF_BUFFERS];
5714 struct splice_pipe_desc spd = {
5715 .pages = pages_def,
5716 .partial = partial_def,
5717 .nr_pages_max = PIPE_DEF_BUFFERS,
5718 .flags = flags,
5719 .ops = &buffer_pipe_buf_ops,
5720 .spd_release = buffer_spd_release,
5722 struct buffer_ref *ref;
5723 int entries, size, i;
5724 ssize_t ret = 0;
5726 #ifdef CONFIG_TRACER_MAX_TRACE
5727 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
5728 return -EBUSY;
5729 #endif
5731 if (splice_grow_spd(pipe, &spd))
5732 return -ENOMEM;
5734 if (*ppos & (PAGE_SIZE - 1))
5735 return -EINVAL;
5737 if (len & (PAGE_SIZE - 1)) {
5738 if (len < PAGE_SIZE)
5739 return -EINVAL;
5740 len &= PAGE_MASK;
5743 again:
5744 trace_access_lock(iter->cpu_file);
5745 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5747 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
5748 struct page *page;
5749 int r;
5751 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
5752 if (!ref) {
5753 ret = -ENOMEM;
5754 break;
5757 ref->ref = 1;
5758 ref->buffer = iter->trace_buffer->buffer;
5759 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
5760 if (!ref->page) {
5761 ret = -ENOMEM;
5762 kfree(ref);
5763 break;
5766 r = ring_buffer_read_page(ref->buffer, &ref->page,
5767 len, iter->cpu_file, 1);
5768 if (r < 0) {
5769 ring_buffer_free_read_page(ref->buffer, ref->page);
5770 kfree(ref);
5771 break;
5775 * zero out any left over data, this is going to
5776 * user land.
5778 size = ring_buffer_page_len(ref->page);
5779 if (size < PAGE_SIZE)
5780 memset(ref->page + size, 0, PAGE_SIZE - size);
5782 page = virt_to_page(ref->page);
5784 spd.pages[i] = page;
5785 spd.partial[i].len = PAGE_SIZE;
5786 spd.partial[i].offset = 0;
5787 spd.partial[i].private = (unsigned long)ref;
5788 spd.nr_pages++;
5789 *ppos += PAGE_SIZE;
5791 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5794 trace_access_unlock(iter->cpu_file);
5795 spd.nr_pages = i;
5797 /* did we read anything? */
5798 if (!spd.nr_pages) {
5799 if (ret)
5800 return ret;
5802 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
5803 return -EAGAIN;
5805 ret = wait_on_pipe(iter, true);
5806 if (ret)
5807 return ret;
5809 goto again;
5812 ret = splice_to_pipe(pipe, &spd);
5813 splice_shrink_spd(&spd);
5815 return ret;
5818 static const struct file_operations tracing_buffers_fops = {
5819 .open = tracing_buffers_open,
5820 .read = tracing_buffers_read,
5821 .poll = tracing_buffers_poll,
5822 .release = tracing_buffers_release,
5823 .splice_read = tracing_buffers_splice_read,
5824 .llseek = no_llseek,
5827 static ssize_t
5828 tracing_stats_read(struct file *filp, char __user *ubuf,
5829 size_t count, loff_t *ppos)
5831 struct inode *inode = file_inode(filp);
5832 struct trace_array *tr = inode->i_private;
5833 struct trace_buffer *trace_buf = &tr->trace_buffer;
5834 int cpu = tracing_get_cpu(inode);
5835 struct trace_seq *s;
5836 unsigned long cnt;
5837 unsigned long long t;
5838 unsigned long usec_rem;
5840 s = kmalloc(sizeof(*s), GFP_KERNEL);
5841 if (!s)
5842 return -ENOMEM;
5844 trace_seq_init(s);
5846 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
5847 trace_seq_printf(s, "entries: %ld\n", cnt);
5849 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
5850 trace_seq_printf(s, "overrun: %ld\n", cnt);
5852 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
5853 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5855 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
5856 trace_seq_printf(s, "bytes: %ld\n", cnt);
5858 if (trace_clocks[tr->clock_id].in_ns) {
5859 /* local or global for trace_clock */
5860 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5861 usec_rem = do_div(t, USEC_PER_SEC);
5862 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5863 t, usec_rem);
5865 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
5866 usec_rem = do_div(t, USEC_PER_SEC);
5867 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5868 } else {
5869 /* counter or tsc mode for trace_clock */
5870 trace_seq_printf(s, "oldest event ts: %llu\n",
5871 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5873 trace_seq_printf(s, "now ts: %llu\n",
5874 ring_buffer_time_stamp(trace_buf->buffer, cpu));
5877 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
5878 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5880 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
5881 trace_seq_printf(s, "read events: %ld\n", cnt);
5883 count = simple_read_from_buffer(ubuf, count, ppos,
5884 s->buffer, trace_seq_used(s));
5886 kfree(s);
5888 return count;
5891 static const struct file_operations tracing_stats_fops = {
5892 .open = tracing_open_generic_tr,
5893 .read = tracing_stats_read,
5894 .llseek = generic_file_llseek,
5895 .release = tracing_release_generic_tr,
5898 #ifdef CONFIG_DYNAMIC_FTRACE
5900 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
5902 return 0;
5905 static ssize_t
5906 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
5907 size_t cnt, loff_t *ppos)
5909 static char ftrace_dyn_info_buffer[1024];
5910 static DEFINE_MUTEX(dyn_info_mutex);
5911 unsigned long *p = filp->private_data;
5912 char *buf = ftrace_dyn_info_buffer;
5913 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
5914 int r;
5916 mutex_lock(&dyn_info_mutex);
5917 r = sprintf(buf, "%ld ", *p);
5919 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
5920 buf[r++] = '\n';
5922 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5924 mutex_unlock(&dyn_info_mutex);
5926 return r;
5929 static const struct file_operations tracing_dyn_info_fops = {
5930 .open = tracing_open_generic,
5931 .read = tracing_read_dyn_info,
5932 .llseek = generic_file_llseek,
5934 #endif /* CONFIG_DYNAMIC_FTRACE */
5936 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5937 static void
5938 ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5940 tracing_snapshot();
5943 static void
5944 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5946 unsigned long *count = (long *)data;
5948 if (!*count)
5949 return;
5951 if (*count != -1)
5952 (*count)--;
5954 tracing_snapshot();
5957 static int
5958 ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5959 struct ftrace_probe_ops *ops, void *data)
5961 long count = (long)data;
5963 seq_printf(m, "%ps:", (void *)ip);
5965 seq_puts(m, "snapshot");
5967 if (count == -1)
5968 seq_puts(m, ":unlimited\n");
5969 else
5970 seq_printf(m, ":count=%ld\n", count);
5972 return 0;
5975 static struct ftrace_probe_ops snapshot_probe_ops = {
5976 .func = ftrace_snapshot,
5977 .print = ftrace_snapshot_print,
5980 static struct ftrace_probe_ops snapshot_count_probe_ops = {
5981 .func = ftrace_count_snapshot,
5982 .print = ftrace_snapshot_print,
5985 static int
5986 ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5987 char *glob, char *cmd, char *param, int enable)
5989 struct ftrace_probe_ops *ops;
5990 void *count = (void *)-1;
5991 char *number;
5992 int ret;
5994 /* hash funcs only work with set_ftrace_filter */
5995 if (!enable)
5996 return -EINVAL;
5998 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
6000 if (glob[0] == '!') {
6001 unregister_ftrace_function_probe_func(glob+1, ops);
6002 return 0;
6005 if (!param)
6006 goto out_reg;
6008 number = strsep(&param, ":");
6010 if (!strlen(number))
6011 goto out_reg;
6014 * We use the callback data field (which is a pointer)
6015 * as our counter.
6017 ret = kstrtoul(number, 0, (unsigned long *)&count);
6018 if (ret)
6019 return ret;
6021 out_reg:
6022 ret = register_ftrace_function_probe(glob, ops, count);
6024 if (ret >= 0)
6025 alloc_snapshot(&global_trace);
6027 return ret < 0 ? ret : 0;
6030 static struct ftrace_func_command ftrace_snapshot_cmd = {
6031 .name = "snapshot",
6032 .func = ftrace_trace_snapshot_callback,
6035 static __init int register_snapshot_cmd(void)
6037 return register_ftrace_command(&ftrace_snapshot_cmd);
6039 #else
6040 static inline __init int register_snapshot_cmd(void) { return 0; }
6041 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
6043 static struct dentry *tracing_get_dentry(struct trace_array *tr)
6045 if (WARN_ON(!tr->dir))
6046 return ERR_PTR(-ENODEV);
6048 /* Top directory uses NULL as the parent */
6049 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
6050 return NULL;
6052 /* All sub buffers have a descriptor */
6053 return tr->dir;
6056 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
6058 struct dentry *d_tracer;
6060 if (tr->percpu_dir)
6061 return tr->percpu_dir;
6063 d_tracer = tracing_get_dentry(tr);
6064 if (IS_ERR(d_tracer))
6065 return NULL;
6067 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
6069 WARN_ONCE(!tr->percpu_dir,
6070 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
6072 return tr->percpu_dir;
6075 static struct dentry *
6076 trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
6077 void *data, long cpu, const struct file_operations *fops)
6079 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
6081 if (ret) /* See tracing_get_cpu() */
6082 d_inode(ret)->i_cdev = (void *)(cpu + 1);
6083 return ret;
6086 static void
6087 tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
6089 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
6090 struct dentry *d_cpu;
6091 char cpu_dir[30]; /* 30 characters should be more than enough */
6093 if (!d_percpu)
6094 return;
6096 snprintf(cpu_dir, 30, "cpu%ld", cpu);
6097 d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
6098 if (!d_cpu) {
6099 pr_warning("Could not create tracefs '%s' entry\n", cpu_dir);
6100 return;
6103 /* per cpu trace_pipe */
6104 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
6105 tr, cpu, &tracing_pipe_fops);
6107 /* per cpu trace */
6108 trace_create_cpu_file("trace", 0644, d_cpu,
6109 tr, cpu, &tracing_fops);
6111 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
6112 tr, cpu, &tracing_buffers_fops);
6114 trace_create_cpu_file("stats", 0444, d_cpu,
6115 tr, cpu, &tracing_stats_fops);
6117 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
6118 tr, cpu, &tracing_entries_fops);
6120 #ifdef CONFIG_TRACER_SNAPSHOT
6121 trace_create_cpu_file("snapshot", 0644, d_cpu,
6122 tr, cpu, &snapshot_fops);
6124 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
6125 tr, cpu, &snapshot_raw_fops);
6126 #endif
6129 #ifdef CONFIG_FTRACE_SELFTEST
6130 /* Let selftest have access to static functions in this file */
6131 #include "trace_selftest.c"
6132 #endif
6134 struct trace_option_dentry {
6135 struct tracer_opt *opt;
6136 struct tracer_flags *flags;
6137 struct trace_array *tr;
6138 struct dentry *entry;
6141 static ssize_t
6142 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
6143 loff_t *ppos)
6145 struct trace_option_dentry *topt = filp->private_data;
6146 char *buf;
6148 if (topt->flags->val & topt->opt->bit)
6149 buf = "1\n";
6150 else
6151 buf = "0\n";
6153 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
6156 static ssize_t
6157 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
6158 loff_t *ppos)
6160 struct trace_option_dentry *topt = filp->private_data;
6161 unsigned long val;
6162 int ret;
6164 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6165 if (ret)
6166 return ret;
6168 if (val != 0 && val != 1)
6169 return -EINVAL;
6171 if (!!(topt->flags->val & topt->opt->bit) != val) {
6172 mutex_lock(&trace_types_lock);
6173 ret = __set_tracer_option(topt->tr, topt->flags,
6174 topt->opt, !val);
6175 mutex_unlock(&trace_types_lock);
6176 if (ret)
6177 return ret;
6180 *ppos += cnt;
6182 return cnt;
6186 static const struct file_operations trace_options_fops = {
6187 .open = tracing_open_generic,
6188 .read = trace_options_read,
6189 .write = trace_options_write,
6190 .llseek = generic_file_llseek,
6193 static ssize_t
6194 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
6195 loff_t *ppos)
6197 long index = (long)filp->private_data;
6198 char *buf;
6200 if (trace_flags & (1 << index))
6201 buf = "1\n";
6202 else
6203 buf = "0\n";
6205 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
6208 static ssize_t
6209 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
6210 loff_t *ppos)
6212 struct trace_array *tr = &global_trace;
6213 long index = (long)filp->private_data;
6214 unsigned long val;
6215 int ret;
6217 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6218 if (ret)
6219 return ret;
6221 if (val != 0 && val != 1)
6222 return -EINVAL;
6224 mutex_lock(&trace_types_lock);
6225 ret = set_tracer_flag(tr, 1 << index, val);
6226 mutex_unlock(&trace_types_lock);
6228 if (ret < 0)
6229 return ret;
6231 *ppos += cnt;
6233 return cnt;
6236 static const struct file_operations trace_options_core_fops = {
6237 .open = tracing_open_generic,
6238 .read = trace_options_core_read,
6239 .write = trace_options_core_write,
6240 .llseek = generic_file_llseek,
6243 struct dentry *trace_create_file(const char *name,
6244 umode_t mode,
6245 struct dentry *parent,
6246 void *data,
6247 const struct file_operations *fops)
6249 struct dentry *ret;
6251 ret = tracefs_create_file(name, mode, parent, data, fops);
6252 if (!ret)
6253 pr_warning("Could not create tracefs '%s' entry\n", name);
6255 return ret;
6259 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
6261 struct dentry *d_tracer;
6263 if (tr->options)
6264 return tr->options;
6266 d_tracer = tracing_get_dentry(tr);
6267 if (IS_ERR(d_tracer))
6268 return NULL;
6270 tr->options = tracefs_create_dir("options", d_tracer);
6271 if (!tr->options) {
6272 pr_warning("Could not create tracefs directory 'options'\n");
6273 return NULL;
6276 return tr->options;
6279 static void
6280 create_trace_option_file(struct trace_array *tr,
6281 struct trace_option_dentry *topt,
6282 struct tracer_flags *flags,
6283 struct tracer_opt *opt)
6285 struct dentry *t_options;
6287 t_options = trace_options_init_dentry(tr);
6288 if (!t_options)
6289 return;
6291 topt->flags = flags;
6292 topt->opt = opt;
6293 topt->tr = tr;
6295 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
6296 &trace_options_fops);
6300 static struct trace_option_dentry *
6301 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
6303 struct trace_option_dentry *topts;
6304 struct tracer_flags *flags;
6305 struct tracer_opt *opts;
6306 int cnt;
6308 if (!tracer)
6309 return NULL;
6311 flags = tracer->flags;
6313 if (!flags || !flags->opts)
6314 return NULL;
6316 opts = flags->opts;
6318 for (cnt = 0; opts[cnt].name; cnt++)
6321 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
6322 if (!topts)
6323 return NULL;
6325 for (cnt = 0; opts[cnt].name; cnt++)
6326 create_trace_option_file(tr, &topts[cnt], flags,
6327 &opts[cnt]);
6329 return topts;
6332 static void
6333 destroy_trace_option_files(struct trace_option_dentry *topts)
6335 int cnt;
6337 if (!topts)
6338 return;
6340 for (cnt = 0; topts[cnt].opt; cnt++)
6341 tracefs_remove(topts[cnt].entry);
6343 kfree(topts);
6346 static struct dentry *
6347 create_trace_option_core_file(struct trace_array *tr,
6348 const char *option, long index)
6350 struct dentry *t_options;
6352 t_options = trace_options_init_dentry(tr);
6353 if (!t_options)
6354 return NULL;
6356 return trace_create_file(option, 0644, t_options, (void *)index,
6357 &trace_options_core_fops);
6360 static __init void create_trace_options_dir(struct trace_array *tr)
6362 struct dentry *t_options;
6363 int i;
6365 t_options = trace_options_init_dentry(tr);
6366 if (!t_options)
6367 return;
6369 for (i = 0; trace_options[i]; i++)
6370 create_trace_option_core_file(tr, trace_options[i], i);
6373 static ssize_t
6374 rb_simple_read(struct file *filp, char __user *ubuf,
6375 size_t cnt, loff_t *ppos)
6377 struct trace_array *tr = filp->private_data;
6378 char buf[64];
6379 int r;
6381 r = tracer_tracing_is_on(tr);
6382 r = sprintf(buf, "%d\n", r);
6384 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6387 static ssize_t
6388 rb_simple_write(struct file *filp, const char __user *ubuf,
6389 size_t cnt, loff_t *ppos)
6391 struct trace_array *tr = filp->private_data;
6392 struct ring_buffer *buffer = tr->trace_buffer.buffer;
6393 unsigned long val;
6394 int ret;
6396 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6397 if (ret)
6398 return ret;
6400 if (buffer) {
6401 mutex_lock(&trace_types_lock);
6402 if (val) {
6403 tracer_tracing_on(tr);
6404 if (tr->current_trace->start)
6405 tr->current_trace->start(tr);
6406 } else {
6407 tracer_tracing_off(tr);
6408 if (tr->current_trace->stop)
6409 tr->current_trace->stop(tr);
6411 mutex_unlock(&trace_types_lock);
6414 (*ppos)++;
6416 return cnt;
6419 static const struct file_operations rb_simple_fops = {
6420 .open = tracing_open_generic_tr,
6421 .read = rb_simple_read,
6422 .write = rb_simple_write,
6423 .release = tracing_release_generic_tr,
6424 .llseek = default_llseek,
6427 struct dentry *trace_instance_dir;
6429 static void
6430 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
6432 static int
6433 allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
6435 enum ring_buffer_flags rb_flags;
6437 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
6439 buf->tr = tr;
6441 buf->buffer = ring_buffer_alloc(size, rb_flags);
6442 if (!buf->buffer)
6443 return -ENOMEM;
6445 buf->data = alloc_percpu(struct trace_array_cpu);
6446 if (!buf->data) {
6447 ring_buffer_free(buf->buffer);
6448 return -ENOMEM;
6451 /* Allocate the first page for all buffers */
6452 set_buffer_entries(&tr->trace_buffer,
6453 ring_buffer_size(tr->trace_buffer.buffer, 0));
6455 return 0;
6458 static int allocate_trace_buffers(struct trace_array *tr, int size)
6460 int ret;
6462 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
6463 if (ret)
6464 return ret;
6466 #ifdef CONFIG_TRACER_MAX_TRACE
6467 ret = allocate_trace_buffer(tr, &tr->max_buffer,
6468 allocate_snapshot ? size : 1);
6469 if (WARN_ON(ret)) {
6470 ring_buffer_free(tr->trace_buffer.buffer);
6471 free_percpu(tr->trace_buffer.data);
6472 return -ENOMEM;
6474 tr->allocated_snapshot = allocate_snapshot;
6477 * Only the top level trace array gets its snapshot allocated
6478 * from the kernel command line.
6480 allocate_snapshot = false;
6481 #endif
6482 return 0;
6485 static void free_trace_buffer(struct trace_buffer *buf)
6487 if (buf->buffer) {
6488 ring_buffer_free(buf->buffer);
6489 buf->buffer = NULL;
6490 free_percpu(buf->data);
6491 buf->data = NULL;
6495 static void free_trace_buffers(struct trace_array *tr)
6497 if (!tr)
6498 return;
6500 free_trace_buffer(&tr->trace_buffer);
6502 #ifdef CONFIG_TRACER_MAX_TRACE
6503 free_trace_buffer(&tr->max_buffer);
6504 #endif
6507 static int instance_mkdir(const char *name)
6509 struct trace_array *tr;
6510 int ret;
6512 mutex_lock(&trace_types_lock);
6514 ret = -EEXIST;
6515 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6516 if (tr->name && strcmp(tr->name, name) == 0)
6517 goto out_unlock;
6520 ret = -ENOMEM;
6521 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
6522 if (!tr)
6523 goto out_unlock;
6525 tr->name = kstrdup(name, GFP_KERNEL);
6526 if (!tr->name)
6527 goto out_free_tr;
6529 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
6530 goto out_free_tr;
6532 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
6534 raw_spin_lock_init(&tr->start_lock);
6536 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
6538 tr->current_trace = &nop_trace;
6540 INIT_LIST_HEAD(&tr->systems);
6541 INIT_LIST_HEAD(&tr->events);
6543 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
6544 goto out_free_tr;
6546 tr->dir = tracefs_create_dir(name, trace_instance_dir);
6547 if (!tr->dir)
6548 goto out_free_tr;
6550 ret = event_trace_add_tracer(tr->dir, tr);
6551 if (ret) {
6552 tracefs_remove_recursive(tr->dir);
6553 goto out_free_tr;
6556 init_tracer_tracefs(tr, tr->dir);
6558 list_add(&tr->list, &ftrace_trace_arrays);
6560 mutex_unlock(&trace_types_lock);
6562 return 0;
6564 out_free_tr:
6565 free_trace_buffers(tr);
6566 free_cpumask_var(tr->tracing_cpumask);
6567 kfree(tr->name);
6568 kfree(tr);
6570 out_unlock:
6571 mutex_unlock(&trace_types_lock);
6573 return ret;
6577 static int instance_rmdir(const char *name)
6579 struct trace_array *tr;
6580 int found = 0;
6581 int ret;
6583 mutex_lock(&trace_types_lock);
6585 ret = -ENODEV;
6586 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6587 if (tr->name && strcmp(tr->name, name) == 0) {
6588 found = 1;
6589 break;
6592 if (!found)
6593 goto out_unlock;
6595 ret = -EBUSY;
6596 if (tr->ref || (tr->current_trace && tr->current_trace->ref))
6597 goto out_unlock;
6599 list_del(&tr->list);
6601 tracing_set_nop(tr);
6602 event_trace_del_tracer(tr);
6603 ftrace_destroy_function_files(tr);
6604 debugfs_remove_recursive(tr->dir);
6605 free_trace_buffers(tr);
6607 kfree(tr->name);
6608 kfree(tr);
6610 ret = 0;
6612 out_unlock:
6613 mutex_unlock(&trace_types_lock);
6615 return ret;
6618 static __init void create_trace_instances(struct dentry *d_tracer)
6620 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
6621 instance_mkdir,
6622 instance_rmdir);
6623 if (WARN_ON(!trace_instance_dir))
6624 return;
6627 static void
6628 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
6630 int cpu;
6632 trace_create_file("available_tracers", 0444, d_tracer,
6633 tr, &show_traces_fops);
6635 trace_create_file("current_tracer", 0644, d_tracer,
6636 tr, &set_tracer_fops);
6638 trace_create_file("tracing_cpumask", 0644, d_tracer,
6639 tr, &tracing_cpumask_fops);
6641 trace_create_file("trace_options", 0644, d_tracer,
6642 tr, &tracing_iter_fops);
6644 trace_create_file("trace", 0644, d_tracer,
6645 tr, &tracing_fops);
6647 trace_create_file("trace_pipe", 0444, d_tracer,
6648 tr, &tracing_pipe_fops);
6650 trace_create_file("buffer_size_kb", 0644, d_tracer,
6651 tr, &tracing_entries_fops);
6653 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
6654 tr, &tracing_total_entries_fops);
6656 trace_create_file("free_buffer", 0200, d_tracer,
6657 tr, &tracing_free_buffer_fops);
6659 trace_create_file("trace_marker", 0220, d_tracer,
6660 tr, &tracing_mark_fops);
6662 trace_create_file("trace_clock", 0644, d_tracer, tr,
6663 &trace_clock_fops);
6665 trace_create_file("tracing_on", 0644, d_tracer,
6666 tr, &rb_simple_fops);
6668 #ifdef CONFIG_TRACER_MAX_TRACE
6669 trace_create_file("tracing_max_latency", 0644, d_tracer,
6670 &tr->max_latency, &tracing_max_lat_fops);
6671 #endif
6673 if (ftrace_create_function_files(tr, d_tracer))
6674 WARN(1, "Could not allocate function filter files");
6676 #ifdef CONFIG_TRACER_SNAPSHOT
6677 trace_create_file("snapshot", 0644, d_tracer,
6678 tr, &snapshot_fops);
6679 #endif
6681 for_each_tracing_cpu(cpu)
6682 tracing_init_tracefs_percpu(tr, cpu);
6686 static struct vfsmount *trace_automount(void *ingore)
6688 struct vfsmount *mnt;
6689 struct file_system_type *type;
6692 * To maintain backward compatibility for tools that mount
6693 * debugfs to get to the tracing facility, tracefs is automatically
6694 * mounted to the debugfs/tracing directory.
6696 type = get_fs_type("tracefs");
6697 if (!type)
6698 return NULL;
6699 mnt = vfs_kern_mount(type, 0, "tracefs", NULL);
6700 put_filesystem(type);
6701 if (IS_ERR(mnt))
6702 return NULL;
6703 mntget(mnt);
6705 return mnt;
6709 * tracing_init_dentry - initialize top level trace array
6711 * This is called when creating files or directories in the tracing
6712 * directory. It is called via fs_initcall() by any of the boot up code
6713 * and expects to return the dentry of the top level tracing directory.
6715 struct dentry *tracing_init_dentry(void)
6717 struct trace_array *tr = &global_trace;
6719 /* The top level trace array uses NULL as parent */
6720 if (tr->dir)
6721 return NULL;
6723 if (WARN_ON(!debugfs_initialized()))
6724 return ERR_PTR(-ENODEV);
6727 * As there may still be users that expect the tracing
6728 * files to exist in debugfs/tracing, we must automount
6729 * the tracefs file system there, so older tools still
6730 * work with the newer kerenl.
6732 tr->dir = debugfs_create_automount("tracing", NULL,
6733 trace_automount, NULL);
6734 if (!tr->dir) {
6735 pr_warn_once("Could not create debugfs directory 'tracing'\n");
6736 return ERR_PTR(-ENOMEM);
6739 return NULL;
6742 extern struct trace_enum_map *__start_ftrace_enum_maps[];
6743 extern struct trace_enum_map *__stop_ftrace_enum_maps[];
6745 static void __init trace_enum_init(void)
6747 int len;
6749 len = __stop_ftrace_enum_maps - __start_ftrace_enum_maps;
6750 trace_insert_enum_map(NULL, __start_ftrace_enum_maps, len);
6753 #ifdef CONFIG_MODULES
6754 static void trace_module_add_enums(struct module *mod)
6756 if (!mod->num_trace_enums)
6757 return;
6760 * Modules with bad taint do not have events created, do
6761 * not bother with enums either.
6763 if (trace_module_has_bad_taint(mod))
6764 return;
6766 trace_insert_enum_map(mod, mod->trace_enums, mod->num_trace_enums);
6769 #ifdef CONFIG_TRACE_ENUM_MAP_FILE
6770 static void trace_module_remove_enums(struct module *mod)
6772 union trace_enum_map_item *map;
6773 union trace_enum_map_item **last = &trace_enum_maps;
6775 if (!mod->num_trace_enums)
6776 return;
6778 mutex_lock(&trace_enum_mutex);
6780 map = trace_enum_maps;
6782 while (map) {
6783 if (map->head.mod == mod)
6784 break;
6785 map = trace_enum_jmp_to_tail(map);
6786 last = &map->tail.next;
6787 map = map->tail.next;
6789 if (!map)
6790 goto out;
6792 *last = trace_enum_jmp_to_tail(map)->tail.next;
6793 kfree(map);
6794 out:
6795 mutex_unlock(&trace_enum_mutex);
6797 #else
6798 static inline void trace_module_remove_enums(struct module *mod) { }
6799 #endif /* CONFIG_TRACE_ENUM_MAP_FILE */
6801 static int trace_module_notify(struct notifier_block *self,
6802 unsigned long val, void *data)
6804 struct module *mod = data;
6806 switch (val) {
6807 case MODULE_STATE_COMING:
6808 trace_module_add_enums(mod);
6809 break;
6810 case MODULE_STATE_GOING:
6811 trace_module_remove_enums(mod);
6812 break;
6815 return 0;
6818 static struct notifier_block trace_module_nb = {
6819 .notifier_call = trace_module_notify,
6820 .priority = 0,
6822 #endif /* CONFIG_MODULES */
6824 static __init int tracer_init_tracefs(void)
6826 struct dentry *d_tracer;
6828 trace_access_lock_init();
6830 d_tracer = tracing_init_dentry();
6831 if (IS_ERR(d_tracer))
6832 return 0;
6834 init_tracer_tracefs(&global_trace, d_tracer);
6836 trace_create_file("tracing_thresh", 0644, d_tracer,
6837 &global_trace, &tracing_thresh_fops);
6839 trace_create_file("README", 0444, d_tracer,
6840 NULL, &tracing_readme_fops);
6842 trace_create_file("saved_cmdlines", 0444, d_tracer,
6843 NULL, &tracing_saved_cmdlines_fops);
6845 trace_create_file("saved_cmdlines_size", 0644, d_tracer,
6846 NULL, &tracing_saved_cmdlines_size_fops);
6848 trace_enum_init();
6850 trace_create_enum_file(d_tracer);
6852 #ifdef CONFIG_MODULES
6853 register_module_notifier(&trace_module_nb);
6854 #endif
6856 #ifdef CONFIG_DYNAMIC_FTRACE
6857 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
6858 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
6859 #endif
6861 create_trace_instances(d_tracer);
6863 create_trace_options_dir(&global_trace);
6865 /* If the tracer was started via cmdline, create options for it here */
6866 if (global_trace.current_trace != &nop_trace)
6867 update_tracer_options(&global_trace, global_trace.current_trace);
6869 return 0;
6872 static int trace_panic_handler(struct notifier_block *this,
6873 unsigned long event, void *unused)
6875 if (ftrace_dump_on_oops)
6876 ftrace_dump(ftrace_dump_on_oops);
6877 return NOTIFY_OK;
6880 static struct notifier_block trace_panic_notifier = {
6881 .notifier_call = trace_panic_handler,
6882 .next = NULL,
6883 .priority = 150 /* priority: INT_MAX >= x >= 0 */
6886 static int trace_die_handler(struct notifier_block *self,
6887 unsigned long val,
6888 void *data)
6890 switch (val) {
6891 case DIE_OOPS:
6892 if (ftrace_dump_on_oops)
6893 ftrace_dump(ftrace_dump_on_oops);
6894 break;
6895 default:
6896 break;
6898 return NOTIFY_OK;
6901 static struct notifier_block trace_die_notifier = {
6902 .notifier_call = trace_die_handler,
6903 .priority = 200
6907 * printk is set to max of 1024, we really don't need it that big.
6908 * Nothing should be printing 1000 characters anyway.
6910 #define TRACE_MAX_PRINT 1000
6913 * Define here KERN_TRACE so that we have one place to modify
6914 * it if we decide to change what log level the ftrace dump
6915 * should be at.
6917 #define KERN_TRACE KERN_EMERG
6919 void
6920 trace_printk_seq(struct trace_seq *s)
6922 /* Probably should print a warning here. */
6923 if (s->seq.len >= TRACE_MAX_PRINT)
6924 s->seq.len = TRACE_MAX_PRINT;
6927 * More paranoid code. Although the buffer size is set to
6928 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
6929 * an extra layer of protection.
6931 if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
6932 s->seq.len = s->seq.size - 1;
6934 /* should be zero ended, but we are paranoid. */
6935 s->buffer[s->seq.len] = 0;
6937 printk(KERN_TRACE "%s", s->buffer);
6939 trace_seq_init(s);
6942 void trace_init_global_iter(struct trace_iterator *iter)
6944 iter->tr = &global_trace;
6945 iter->trace = iter->tr->current_trace;
6946 iter->cpu_file = RING_BUFFER_ALL_CPUS;
6947 iter->trace_buffer = &global_trace.trace_buffer;
6949 if (iter->trace && iter->trace->open)
6950 iter->trace->open(iter);
6952 /* Annotate start of buffers if we had overruns */
6953 if (ring_buffer_overruns(iter->trace_buffer->buffer))
6954 iter->iter_flags |= TRACE_FILE_ANNOTATE;
6956 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
6957 if (trace_clocks[iter->tr->clock_id].in_ns)
6958 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
6961 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
6963 /* use static because iter can be a bit big for the stack */
6964 static struct trace_iterator iter;
6965 static atomic_t dump_running;
6966 unsigned int old_userobj;
6967 unsigned long flags;
6968 int cnt = 0, cpu;
6970 /* Only allow one dump user at a time. */
6971 if (atomic_inc_return(&dump_running) != 1) {
6972 atomic_dec(&dump_running);
6973 return;
6977 * Always turn off tracing when we dump.
6978 * We don't need to show trace output of what happens
6979 * between multiple crashes.
6981 * If the user does a sysrq-z, then they can re-enable
6982 * tracing with echo 1 > tracing_on.
6984 tracing_off();
6986 local_irq_save(flags);
6988 /* Simulate the iterator */
6989 trace_init_global_iter(&iter);
6991 for_each_tracing_cpu(cpu) {
6992 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
6995 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
6997 /* don't look at user memory in panic mode */
6998 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
7000 switch (oops_dump_mode) {
7001 case DUMP_ALL:
7002 iter.cpu_file = RING_BUFFER_ALL_CPUS;
7003 break;
7004 case DUMP_ORIG:
7005 iter.cpu_file = raw_smp_processor_id();
7006 break;
7007 case DUMP_NONE:
7008 goto out_enable;
7009 default:
7010 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
7011 iter.cpu_file = RING_BUFFER_ALL_CPUS;
7014 printk(KERN_TRACE "Dumping ftrace buffer:\n");
7016 /* Did function tracer already get disabled? */
7017 if (ftrace_is_dead()) {
7018 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
7019 printk("# MAY BE MISSING FUNCTION EVENTS\n");
7023 * We need to stop all tracing on all CPUS to read the
7024 * the next buffer. This is a bit expensive, but is
7025 * not done often. We fill all what we can read,
7026 * and then release the locks again.
7029 while (!trace_empty(&iter)) {
7031 if (!cnt)
7032 printk(KERN_TRACE "---------------------------------\n");
7034 cnt++;
7036 /* reset all but tr, trace, and overruns */
7037 memset(&iter.seq, 0,
7038 sizeof(struct trace_iterator) -
7039 offsetof(struct trace_iterator, seq));
7040 iter.iter_flags |= TRACE_FILE_LAT_FMT;
7041 iter.pos = -1;
7043 if (trace_find_next_entry_inc(&iter) != NULL) {
7044 int ret;
7046 ret = print_trace_line(&iter);
7047 if (ret != TRACE_TYPE_NO_CONSUME)
7048 trace_consume(&iter);
7050 touch_nmi_watchdog();
7052 trace_printk_seq(&iter.seq);
7055 if (!cnt)
7056 printk(KERN_TRACE " (ftrace buffer empty)\n");
7057 else
7058 printk(KERN_TRACE "---------------------------------\n");
7060 out_enable:
7061 trace_flags |= old_userobj;
7063 for_each_tracing_cpu(cpu) {
7064 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
7066 atomic_dec(&dump_running);
7067 local_irq_restore(flags);
7069 EXPORT_SYMBOL_GPL(ftrace_dump);
7071 __init static int tracer_alloc_buffers(void)
7073 int ring_buf_size;
7074 int ret = -ENOMEM;
7076 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
7077 goto out;
7079 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
7080 goto out_free_buffer_mask;
7082 /* Only allocate trace_printk buffers if a trace_printk exists */
7083 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
7084 /* Must be called before global_trace.buffer is allocated */
7085 trace_printk_init_buffers();
7087 /* To save memory, keep the ring buffer size to its minimum */
7088 if (ring_buffer_expanded)
7089 ring_buf_size = trace_buf_size;
7090 else
7091 ring_buf_size = 1;
7093 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
7094 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
7096 raw_spin_lock_init(&global_trace.start_lock);
7098 /* Used for event triggers */
7099 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
7100 if (!temp_buffer)
7101 goto out_free_cpumask;
7103 if (trace_create_savedcmd() < 0)
7104 goto out_free_temp_buffer;
7106 /* TODO: make the number of buffers hot pluggable with CPUS */
7107 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
7108 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
7109 WARN_ON(1);
7110 goto out_free_savedcmd;
7113 if (global_trace.buffer_disabled)
7114 tracing_off();
7116 if (trace_boot_clock) {
7117 ret = tracing_set_clock(&global_trace, trace_boot_clock);
7118 if (ret < 0)
7119 pr_warning("Trace clock %s not defined, going back to default\n",
7120 trace_boot_clock);
7124 * register_tracer() might reference current_trace, so it
7125 * needs to be set before we register anything. This is
7126 * just a bootstrap of current_trace anyway.
7128 global_trace.current_trace = &nop_trace;
7130 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
7132 ftrace_init_global_array_ops(&global_trace);
7134 register_tracer(&nop_trace);
7136 /* All seems OK, enable tracing */
7137 tracing_disabled = 0;
7139 atomic_notifier_chain_register(&panic_notifier_list,
7140 &trace_panic_notifier);
7142 register_die_notifier(&trace_die_notifier);
7144 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
7146 INIT_LIST_HEAD(&global_trace.systems);
7147 INIT_LIST_HEAD(&global_trace.events);
7148 list_add(&global_trace.list, &ftrace_trace_arrays);
7150 while (trace_boot_options) {
7151 char *option;
7153 option = strsep(&trace_boot_options, ",");
7154 trace_set_options(&global_trace, option);
7157 register_snapshot_cmd();
7159 return 0;
7161 out_free_savedcmd:
7162 free_saved_cmdlines_buffer(savedcmd);
7163 out_free_temp_buffer:
7164 ring_buffer_free(temp_buffer);
7165 out_free_cpumask:
7166 free_cpumask_var(global_trace.tracing_cpumask);
7167 out_free_buffer_mask:
7168 free_cpumask_var(tracing_buffer_mask);
7169 out:
7170 return ret;
7173 void __init trace_init(void)
7175 if (tracepoint_printk) {
7176 tracepoint_print_iter =
7177 kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
7178 if (WARN_ON(!tracepoint_print_iter))
7179 tracepoint_printk = 0;
7181 tracer_alloc_buffers();
7182 trace_event_init();
7185 __init static int clear_boot_tracer(void)
7188 * The default tracer at boot buffer is an init section.
7189 * This function is called in lateinit. If we did not
7190 * find the boot tracer, then clear it out, to prevent
7191 * later registration from accessing the buffer that is
7192 * about to be freed.
7194 if (!default_bootup_tracer)
7195 return 0;
7197 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
7198 default_bootup_tracer);
7199 default_bootup_tracer = NULL;
7201 return 0;
7204 fs_initcall(tracer_init_tracefs);
7205 late_initcall(clear_boot_tracer);