x86/efi: Enforce CONFIG_RELOCATABLE for EFI boot stub
[linux/fpc-iii.git] / kernel / trace / trace.c
blobdcdf4e682dd486646b12d26b55d913fe49e1412a
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/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/rwsem.h>
35 #include <linux/slab.h>
36 #include <linux/ctype.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
39 #include <linux/nmi.h>
40 #include <linux/fs.h>
41 #include <linux/sched/rt.h>
43 #include "trace.h"
44 #include "trace_output.h"
47 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
50 bool ring_buffer_expanded;
53 * We need to change this state when a selftest is running.
54 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
56 * insertions into the ring-buffer such as trace_printk could occurred
57 * at the same time, giving false positive or negative results.
59 static bool __read_mostly tracing_selftest_running;
62 * If a tracer is running, we do not want to run SELFTEST.
64 bool __read_mostly tracing_selftest_disabled;
66 /* For tracers that don't implement custom flags */
67 static struct tracer_opt dummy_tracer_opt[] = {
68 { }
71 static struct tracer_flags dummy_tracer_flags = {
72 .val = 0,
73 .opts = dummy_tracer_opt
76 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
78 return 0;
82 * To prevent the comm cache from being overwritten when no
83 * tracing is active, only save the comm when a trace event
84 * occurred.
86 static DEFINE_PER_CPU(bool, trace_cmdline_save);
89 * Kill all tracing for good (never come back).
90 * It is initialized to 1 but will turn to zero if the initialization
91 * of the tracer is successful. But that is the only place that sets
92 * this back to zero.
94 static int tracing_disabled = 1;
96 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
98 cpumask_var_t __read_mostly tracing_buffer_mask;
101 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
103 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
104 * is set, then ftrace_dump is called. This will output the contents
105 * of the ftrace buffers to the console. This is very useful for
106 * capturing traces that lead to crashes and outputing it to a
107 * serial console.
109 * It is default off, but you can enable it with either specifying
110 * "ftrace_dump_on_oops" in the kernel command line, or setting
111 * /proc/sys/kernel/ftrace_dump_on_oops
112 * Set 1 if you want to dump buffers of all CPUs
113 * Set 2 if you want to dump the buffer of the CPU that triggered oops
116 enum ftrace_dump_mode ftrace_dump_on_oops;
118 /* When set, tracing will stop when a WARN*() is hit */
119 int __disable_trace_on_warning;
121 static int tracing_set_tracer(const char *buf);
123 #define MAX_TRACER_SIZE 100
124 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
125 static char *default_bootup_tracer;
127 static bool allocate_snapshot;
129 static int __init set_cmdline_ftrace(char *str)
131 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
132 default_bootup_tracer = bootup_tracer_buf;
133 /* We are using ftrace early, expand it */
134 ring_buffer_expanded = true;
135 return 1;
137 __setup("ftrace=", set_cmdline_ftrace);
139 static int __init set_ftrace_dump_on_oops(char *str)
141 if (*str++ != '=' || !*str) {
142 ftrace_dump_on_oops = DUMP_ALL;
143 return 1;
146 if (!strcmp("orig_cpu", str)) {
147 ftrace_dump_on_oops = DUMP_ORIG;
148 return 1;
151 return 0;
153 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
155 static int __init stop_trace_on_warning(char *str)
157 __disable_trace_on_warning = 1;
158 return 1;
160 __setup("traceoff_on_warning=", stop_trace_on_warning);
162 static int __init boot_alloc_snapshot(char *str)
164 allocate_snapshot = true;
165 /* We also need the main ring buffer expanded */
166 ring_buffer_expanded = true;
167 return 1;
169 __setup("alloc_snapshot", boot_alloc_snapshot);
172 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
173 static char *trace_boot_options __initdata;
175 static int __init set_trace_boot_options(char *str)
177 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
178 trace_boot_options = trace_boot_options_buf;
179 return 0;
181 __setup("trace_options=", set_trace_boot_options);
184 unsigned long long ns2usecs(cycle_t nsec)
186 nsec += 500;
187 do_div(nsec, 1000);
188 return nsec;
192 * The global_trace is the descriptor that holds the tracing
193 * buffers for the live tracing. For each CPU, it contains
194 * a link list of pages that will store trace entries. The
195 * page descriptor of the pages in the memory is used to hold
196 * the link list by linking the lru item in the page descriptor
197 * to each of the pages in the buffer per CPU.
199 * For each active CPU there is a data field that holds the
200 * pages for the buffer for that CPU. Each CPU has the same number
201 * of pages allocated for its buffer.
203 static struct trace_array global_trace;
205 LIST_HEAD(ftrace_trace_arrays);
207 int trace_array_get(struct trace_array *this_tr)
209 struct trace_array *tr;
210 int ret = -ENODEV;
212 mutex_lock(&trace_types_lock);
213 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
214 if (tr == this_tr) {
215 tr->ref++;
216 ret = 0;
217 break;
220 mutex_unlock(&trace_types_lock);
222 return ret;
225 static void __trace_array_put(struct trace_array *this_tr)
227 WARN_ON(!this_tr->ref);
228 this_tr->ref--;
231 void trace_array_put(struct trace_array *this_tr)
233 mutex_lock(&trace_types_lock);
234 __trace_array_put(this_tr);
235 mutex_unlock(&trace_types_lock);
238 int filter_current_check_discard(struct ring_buffer *buffer,
239 struct ftrace_event_call *call, void *rec,
240 struct ring_buffer_event *event)
242 return filter_check_discard(call, rec, buffer, event);
244 EXPORT_SYMBOL_GPL(filter_current_check_discard);
246 cycle_t buffer_ftrace_now(struct trace_buffer *buf, int cpu)
248 u64 ts;
250 /* Early boot up does not have a buffer yet */
251 if (!buf->buffer)
252 return trace_clock_local();
254 ts = ring_buffer_time_stamp(buf->buffer, cpu);
255 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
257 return ts;
260 cycle_t ftrace_now(int cpu)
262 return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
266 * tracing_is_enabled - Show if global_trace has been disabled
268 * Shows if the global trace has been enabled or not. It uses the
269 * mirror flag "buffer_disabled" to be used in fast paths such as for
270 * the irqsoff tracer. But it may be inaccurate due to races. If you
271 * need to know the accurate state, use tracing_is_on() which is a little
272 * slower, but accurate.
274 int tracing_is_enabled(void)
277 * For quick access (irqsoff uses this in fast path), just
278 * return the mirror variable of the state of the ring buffer.
279 * It's a little racy, but we don't really care.
281 smp_rmb();
282 return !global_trace.buffer_disabled;
286 * trace_buf_size is the size in bytes that is allocated
287 * for a buffer. Note, the number of bytes is always rounded
288 * to page size.
290 * This number is purposely set to a low number of 16384.
291 * If the dump on oops happens, it will be much appreciated
292 * to not have to wait for all that output. Anyway this can be
293 * boot time and run time configurable.
295 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
297 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
299 /* trace_types holds a link list of available tracers. */
300 static struct tracer *trace_types __read_mostly;
303 * trace_types_lock is used to protect the trace_types list.
305 DEFINE_MUTEX(trace_types_lock);
308 * serialize the access of the ring buffer
310 * ring buffer serializes readers, but it is low level protection.
311 * The validity of the events (which returns by ring_buffer_peek() ..etc)
312 * are not protected by ring buffer.
314 * The content of events may become garbage if we allow other process consumes
315 * these events concurrently:
316 * A) the page of the consumed events may become a normal page
317 * (not reader page) in ring buffer, and this page will be rewrited
318 * by events producer.
319 * B) The page of the consumed events may become a page for splice_read,
320 * and this page will be returned to system.
322 * These primitives allow multi process access to different cpu ring buffer
323 * concurrently.
325 * These primitives don't distinguish read-only and read-consume access.
326 * Multi read-only access are also serialized.
329 #ifdef CONFIG_SMP
330 static DECLARE_RWSEM(all_cpu_access_lock);
331 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
333 static inline void trace_access_lock(int cpu)
335 if (cpu == RING_BUFFER_ALL_CPUS) {
336 /* gain it for accessing the whole ring buffer. */
337 down_write(&all_cpu_access_lock);
338 } else {
339 /* gain it for accessing a cpu ring buffer. */
341 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
342 down_read(&all_cpu_access_lock);
344 /* Secondly block other access to this @cpu ring buffer. */
345 mutex_lock(&per_cpu(cpu_access_lock, cpu));
349 static inline void trace_access_unlock(int cpu)
351 if (cpu == RING_BUFFER_ALL_CPUS) {
352 up_write(&all_cpu_access_lock);
353 } else {
354 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
355 up_read(&all_cpu_access_lock);
359 static inline void trace_access_lock_init(void)
361 int cpu;
363 for_each_possible_cpu(cpu)
364 mutex_init(&per_cpu(cpu_access_lock, cpu));
367 #else
369 static DEFINE_MUTEX(access_lock);
371 static inline void trace_access_lock(int cpu)
373 (void)cpu;
374 mutex_lock(&access_lock);
377 static inline void trace_access_unlock(int cpu)
379 (void)cpu;
380 mutex_unlock(&access_lock);
383 static inline void trace_access_lock_init(void)
387 #endif
389 /* trace_flags holds trace_options default values */
390 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
391 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
392 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
393 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
395 static void tracer_tracing_on(struct trace_array *tr)
397 if (tr->trace_buffer.buffer)
398 ring_buffer_record_on(tr->trace_buffer.buffer);
400 * This flag is looked at when buffers haven't been allocated
401 * yet, or by some tracers (like irqsoff), that just want to
402 * know if the ring buffer has been disabled, but it can handle
403 * races of where it gets disabled but we still do a record.
404 * As the check is in the fast path of the tracers, it is more
405 * important to be fast than accurate.
407 tr->buffer_disabled = 0;
408 /* Make the flag seen by readers */
409 smp_wmb();
413 * tracing_on - enable tracing buffers
415 * This function enables tracing buffers that may have been
416 * disabled with tracing_off.
418 void tracing_on(void)
420 tracer_tracing_on(&global_trace);
422 EXPORT_SYMBOL_GPL(tracing_on);
425 * __trace_puts - write a constant string into the trace buffer.
426 * @ip: The address of the caller
427 * @str: The constant string to write
428 * @size: The size of the string.
430 int __trace_puts(unsigned long ip, const char *str, int size)
432 struct ring_buffer_event *event;
433 struct ring_buffer *buffer;
434 struct print_entry *entry;
435 unsigned long irq_flags;
436 int alloc;
437 int pc;
439 if (!(trace_flags & TRACE_ITER_PRINTK))
440 return 0;
442 pc = preempt_count();
444 if (unlikely(tracing_selftest_running || tracing_disabled))
445 return 0;
447 alloc = sizeof(*entry) + size + 2; /* possible \n added */
449 local_save_flags(irq_flags);
450 buffer = global_trace.trace_buffer.buffer;
451 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
452 irq_flags, pc);
453 if (!event)
454 return 0;
456 entry = ring_buffer_event_data(event);
457 entry->ip = ip;
459 memcpy(&entry->buf, str, size);
461 /* Add a newline if necessary */
462 if (entry->buf[size - 1] != '\n') {
463 entry->buf[size] = '\n';
464 entry->buf[size + 1] = '\0';
465 } else
466 entry->buf[size] = '\0';
468 __buffer_unlock_commit(buffer, event);
469 ftrace_trace_stack(buffer, irq_flags, 4, pc);
471 return size;
473 EXPORT_SYMBOL_GPL(__trace_puts);
476 * __trace_bputs - write the pointer to a constant string into trace buffer
477 * @ip: The address of the caller
478 * @str: The constant string to write to the buffer to
480 int __trace_bputs(unsigned long ip, const char *str)
482 struct ring_buffer_event *event;
483 struct ring_buffer *buffer;
484 struct bputs_entry *entry;
485 unsigned long irq_flags;
486 int size = sizeof(struct bputs_entry);
487 int pc;
489 if (!(trace_flags & TRACE_ITER_PRINTK))
490 return 0;
492 pc = preempt_count();
494 if (unlikely(tracing_selftest_running || tracing_disabled))
495 return 0;
497 local_save_flags(irq_flags);
498 buffer = global_trace.trace_buffer.buffer;
499 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
500 irq_flags, pc);
501 if (!event)
502 return 0;
504 entry = ring_buffer_event_data(event);
505 entry->ip = ip;
506 entry->str = str;
508 __buffer_unlock_commit(buffer, event);
509 ftrace_trace_stack(buffer, irq_flags, 4, pc);
511 return 1;
513 EXPORT_SYMBOL_GPL(__trace_bputs);
515 #ifdef CONFIG_TRACER_SNAPSHOT
517 * trace_snapshot - take a snapshot of the current buffer.
519 * This causes a swap between the snapshot buffer and the current live
520 * tracing buffer. You can use this to take snapshots of the live
521 * trace when some condition is triggered, but continue to trace.
523 * Note, make sure to allocate the snapshot with either
524 * a tracing_snapshot_alloc(), or by doing it manually
525 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
527 * If the snapshot buffer is not allocated, it will stop tracing.
528 * Basically making a permanent snapshot.
530 void tracing_snapshot(void)
532 struct trace_array *tr = &global_trace;
533 struct tracer *tracer = tr->current_trace;
534 unsigned long flags;
536 if (in_nmi()) {
537 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
538 internal_trace_puts("*** snapshot is being ignored ***\n");
539 return;
542 if (!tr->allocated_snapshot) {
543 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
544 internal_trace_puts("*** stopping trace here! ***\n");
545 tracing_off();
546 return;
549 /* Note, snapshot can not be used when the tracer uses it */
550 if (tracer->use_max_tr) {
551 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
552 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
553 return;
556 local_irq_save(flags);
557 update_max_tr(tr, current, smp_processor_id());
558 local_irq_restore(flags);
560 EXPORT_SYMBOL_GPL(tracing_snapshot);
562 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
563 struct trace_buffer *size_buf, int cpu_id);
564 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
566 static int alloc_snapshot(struct trace_array *tr)
568 int ret;
570 if (!tr->allocated_snapshot) {
572 /* allocate spare buffer */
573 ret = resize_buffer_duplicate_size(&tr->max_buffer,
574 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
575 if (ret < 0)
576 return ret;
578 tr->allocated_snapshot = true;
581 return 0;
584 void free_snapshot(struct trace_array *tr)
587 * We don't free the ring buffer. instead, resize it because
588 * The max_tr ring buffer has some state (e.g. ring->clock) and
589 * we want preserve it.
591 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
592 set_buffer_entries(&tr->max_buffer, 1);
593 tracing_reset_online_cpus(&tr->max_buffer);
594 tr->allocated_snapshot = false;
598 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
600 * This is similar to trace_snapshot(), but it will allocate the
601 * snapshot buffer if it isn't already allocated. Use this only
602 * where it is safe to sleep, as the allocation may sleep.
604 * This causes a swap between the snapshot buffer and the current live
605 * tracing buffer. You can use this to take snapshots of the live
606 * trace when some condition is triggered, but continue to trace.
608 void tracing_snapshot_alloc(void)
610 struct trace_array *tr = &global_trace;
611 int ret;
613 ret = alloc_snapshot(tr);
614 if (WARN_ON(ret < 0))
615 return;
617 tracing_snapshot();
619 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
620 #else
621 void tracing_snapshot(void)
623 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
625 EXPORT_SYMBOL_GPL(tracing_snapshot);
626 void tracing_snapshot_alloc(void)
628 /* Give warning */
629 tracing_snapshot();
631 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
632 #endif /* CONFIG_TRACER_SNAPSHOT */
634 static void tracer_tracing_off(struct trace_array *tr)
636 if (tr->trace_buffer.buffer)
637 ring_buffer_record_off(tr->trace_buffer.buffer);
639 * This flag is looked at when buffers haven't been allocated
640 * yet, or by some tracers (like irqsoff), that just want to
641 * know if the ring buffer has been disabled, but it can handle
642 * races of where it gets disabled but we still do a record.
643 * As the check is in the fast path of the tracers, it is more
644 * important to be fast than accurate.
646 tr->buffer_disabled = 1;
647 /* Make the flag seen by readers */
648 smp_wmb();
652 * tracing_off - turn off tracing buffers
654 * This function stops the tracing buffers from recording data.
655 * It does not disable any overhead the tracers themselves may
656 * be causing. This function simply causes all recording to
657 * the ring buffers to fail.
659 void tracing_off(void)
661 tracer_tracing_off(&global_trace);
663 EXPORT_SYMBOL_GPL(tracing_off);
665 void disable_trace_on_warning(void)
667 if (__disable_trace_on_warning)
668 tracing_off();
672 * tracer_tracing_is_on - show real state of ring buffer enabled
673 * @tr : the trace array to know if ring buffer is enabled
675 * Shows real state of the ring buffer if it is enabled or not.
677 static int tracer_tracing_is_on(struct trace_array *tr)
679 if (tr->trace_buffer.buffer)
680 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
681 return !tr->buffer_disabled;
685 * tracing_is_on - show state of ring buffers enabled
687 int tracing_is_on(void)
689 return tracer_tracing_is_on(&global_trace);
691 EXPORT_SYMBOL_GPL(tracing_is_on);
693 static int __init set_buf_size(char *str)
695 unsigned long buf_size;
697 if (!str)
698 return 0;
699 buf_size = memparse(str, &str);
700 /* nr_entries can not be zero */
701 if (buf_size == 0)
702 return 0;
703 trace_buf_size = buf_size;
704 return 1;
706 __setup("trace_buf_size=", set_buf_size);
708 static int __init set_tracing_thresh(char *str)
710 unsigned long threshold;
711 int ret;
713 if (!str)
714 return 0;
715 ret = kstrtoul(str, 0, &threshold);
716 if (ret < 0)
717 return 0;
718 tracing_thresh = threshold * 1000;
719 return 1;
721 __setup("tracing_thresh=", set_tracing_thresh);
723 unsigned long nsecs_to_usecs(unsigned long nsecs)
725 return nsecs / 1000;
728 /* These must match the bit postions in trace_iterator_flags */
729 static const char *trace_options[] = {
730 "print-parent",
731 "sym-offset",
732 "sym-addr",
733 "verbose",
734 "raw",
735 "hex",
736 "bin",
737 "block",
738 "stacktrace",
739 "trace_printk",
740 "ftrace_preempt",
741 "branch",
742 "annotate",
743 "userstacktrace",
744 "sym-userobj",
745 "printk-msg-only",
746 "context-info",
747 "latency-format",
748 "sleep-time",
749 "graph-time",
750 "record-cmd",
751 "overwrite",
752 "disable_on_free",
753 "irq-info",
754 "markers",
755 "function-trace",
756 NULL
759 static struct {
760 u64 (*func)(void);
761 const char *name;
762 int in_ns; /* is this clock in nanoseconds? */
763 } trace_clocks[] = {
764 { trace_clock_local, "local", 1 },
765 { trace_clock_global, "global", 1 },
766 { trace_clock_counter, "counter", 0 },
767 { trace_clock_jiffies, "uptime", 0 },
768 { trace_clock, "perf", 1 },
769 ARCH_TRACE_CLOCKS
773 * trace_parser_get_init - gets the buffer for trace parser
775 int trace_parser_get_init(struct trace_parser *parser, int size)
777 memset(parser, 0, sizeof(*parser));
779 parser->buffer = kmalloc(size, GFP_KERNEL);
780 if (!parser->buffer)
781 return 1;
783 parser->size = size;
784 return 0;
788 * trace_parser_put - frees the buffer for trace parser
790 void trace_parser_put(struct trace_parser *parser)
792 kfree(parser->buffer);
796 * trace_get_user - reads the user input string separated by space
797 * (matched by isspace(ch))
799 * For each string found the 'struct trace_parser' is updated,
800 * and the function returns.
802 * Returns number of bytes read.
804 * See kernel/trace/trace.h for 'struct trace_parser' details.
806 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
807 size_t cnt, loff_t *ppos)
809 char ch;
810 size_t read = 0;
811 ssize_t ret;
813 if (!*ppos)
814 trace_parser_clear(parser);
816 ret = get_user(ch, ubuf++);
817 if (ret)
818 goto out;
820 read++;
821 cnt--;
824 * The parser is not finished with the last write,
825 * continue reading the user input without skipping spaces.
827 if (!parser->cont) {
828 /* skip white space */
829 while (cnt && isspace(ch)) {
830 ret = get_user(ch, ubuf++);
831 if (ret)
832 goto out;
833 read++;
834 cnt--;
837 /* only spaces were written */
838 if (isspace(ch)) {
839 *ppos += read;
840 ret = read;
841 goto out;
844 parser->idx = 0;
847 /* read the non-space input */
848 while (cnt && !isspace(ch)) {
849 if (parser->idx < parser->size - 1)
850 parser->buffer[parser->idx++] = ch;
851 else {
852 ret = -EINVAL;
853 goto out;
855 ret = get_user(ch, ubuf++);
856 if (ret)
857 goto out;
858 read++;
859 cnt--;
862 /* We either got finished input or we have to wait for another call. */
863 if (isspace(ch)) {
864 parser->buffer[parser->idx] = 0;
865 parser->cont = false;
866 } else if (parser->idx < parser->size - 1) {
867 parser->cont = true;
868 parser->buffer[parser->idx++] = ch;
869 } else {
870 ret = -EINVAL;
871 goto out;
874 *ppos += read;
875 ret = read;
877 out:
878 return ret;
881 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
883 int len;
884 int ret;
886 if (!cnt)
887 return 0;
889 if (s->len <= s->readpos)
890 return -EBUSY;
892 len = s->len - s->readpos;
893 if (cnt > len)
894 cnt = len;
895 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
896 if (ret == cnt)
897 return -EFAULT;
899 cnt -= ret;
901 s->readpos += cnt;
902 return cnt;
905 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
907 int len;
909 if (s->len <= s->readpos)
910 return -EBUSY;
912 len = s->len - s->readpos;
913 if (cnt > len)
914 cnt = len;
915 memcpy(buf, s->buffer + s->readpos, cnt);
917 s->readpos += cnt;
918 return cnt;
922 * ftrace_max_lock is used to protect the swapping of buffers
923 * when taking a max snapshot. The buffers themselves are
924 * protected by per_cpu spinlocks. But the action of the swap
925 * needs its own lock.
927 * This is defined as a arch_spinlock_t in order to help
928 * with performance when lockdep debugging is enabled.
930 * It is also used in other places outside the update_max_tr
931 * so it needs to be defined outside of the
932 * CONFIG_TRACER_MAX_TRACE.
934 static arch_spinlock_t ftrace_max_lock =
935 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
937 unsigned long __read_mostly tracing_thresh;
939 #ifdef CONFIG_TRACER_MAX_TRACE
940 unsigned long __read_mostly tracing_max_latency;
943 * Copy the new maximum trace into the separate maximum-trace
944 * structure. (this way the maximum trace is permanently saved,
945 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
947 static void
948 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
950 struct trace_buffer *trace_buf = &tr->trace_buffer;
951 struct trace_buffer *max_buf = &tr->max_buffer;
952 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
953 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
955 max_buf->cpu = cpu;
956 max_buf->time_start = data->preempt_timestamp;
958 max_data->saved_latency = tracing_max_latency;
959 max_data->critical_start = data->critical_start;
960 max_data->critical_end = data->critical_end;
962 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
963 max_data->pid = tsk->pid;
965 * If tsk == current, then use current_uid(), as that does not use
966 * RCU. The irq tracer can be called out of RCU scope.
968 if (tsk == current)
969 max_data->uid = current_uid();
970 else
971 max_data->uid = task_uid(tsk);
973 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
974 max_data->policy = tsk->policy;
975 max_data->rt_priority = tsk->rt_priority;
977 /* record this tasks comm */
978 tracing_record_cmdline(tsk);
982 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
983 * @tr: tracer
984 * @tsk: the task with the latency
985 * @cpu: The cpu that initiated the trace.
987 * Flip the buffers between the @tr and the max_tr and record information
988 * about which task was the cause of this latency.
990 void
991 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
993 struct ring_buffer *buf;
995 if (tr->stop_count)
996 return;
998 WARN_ON_ONCE(!irqs_disabled());
1000 if (!tr->allocated_snapshot) {
1001 /* Only the nop tracer should hit this when disabling */
1002 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1003 return;
1006 arch_spin_lock(&ftrace_max_lock);
1008 buf = tr->trace_buffer.buffer;
1009 tr->trace_buffer.buffer = tr->max_buffer.buffer;
1010 tr->max_buffer.buffer = buf;
1012 __update_max_tr(tr, tsk, cpu);
1013 arch_spin_unlock(&ftrace_max_lock);
1017 * update_max_tr_single - only copy one trace over, and reset the rest
1018 * @tr - tracer
1019 * @tsk - task with the latency
1020 * @cpu - the cpu of the buffer to copy.
1022 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
1024 void
1025 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1027 int ret;
1029 if (tr->stop_count)
1030 return;
1032 WARN_ON_ONCE(!irqs_disabled());
1033 if (!tr->allocated_snapshot) {
1034 /* Only the nop tracer should hit this when disabling */
1035 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1036 return;
1039 arch_spin_lock(&ftrace_max_lock);
1041 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
1043 if (ret == -EBUSY) {
1045 * We failed to swap the buffer due to a commit taking
1046 * place on this CPU. We fail to record, but we reset
1047 * the max trace buffer (no one writes directly to it)
1048 * and flag that it failed.
1050 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
1051 "Failed to swap buffers due to commit in progress\n");
1054 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
1056 __update_max_tr(tr, tsk, cpu);
1057 arch_spin_unlock(&ftrace_max_lock);
1059 #endif /* CONFIG_TRACER_MAX_TRACE */
1061 static int default_wait_pipe(struct trace_iterator *iter)
1063 /* Iterators are static, they should be filled or empty */
1064 if (trace_buffer_iter(iter, iter->cpu_file))
1065 return 0;
1067 return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
1070 #ifdef CONFIG_FTRACE_STARTUP_TEST
1071 static int run_tracer_selftest(struct tracer *type)
1073 struct trace_array *tr = &global_trace;
1074 struct tracer *saved_tracer = tr->current_trace;
1075 int ret;
1077 if (!type->selftest || tracing_selftest_disabled)
1078 return 0;
1081 * Run a selftest on this tracer.
1082 * Here we reset the trace buffer, and set the current
1083 * tracer to be this tracer. The tracer can then run some
1084 * internal tracing to verify that everything is in order.
1085 * If we fail, we do not register this tracer.
1087 tracing_reset_online_cpus(&tr->trace_buffer);
1089 tr->current_trace = type;
1091 #ifdef CONFIG_TRACER_MAX_TRACE
1092 if (type->use_max_tr) {
1093 /* If we expanded the buffers, make sure the max is expanded too */
1094 if (ring_buffer_expanded)
1095 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1096 RING_BUFFER_ALL_CPUS);
1097 tr->allocated_snapshot = true;
1099 #endif
1101 /* the test is responsible for initializing and enabling */
1102 pr_info("Testing tracer %s: ", type->name);
1103 ret = type->selftest(type, tr);
1104 /* the test is responsible for resetting too */
1105 tr->current_trace = saved_tracer;
1106 if (ret) {
1107 printk(KERN_CONT "FAILED!\n");
1108 /* Add the warning after printing 'FAILED' */
1109 WARN_ON(1);
1110 return -1;
1112 /* Only reset on passing, to avoid touching corrupted buffers */
1113 tracing_reset_online_cpus(&tr->trace_buffer);
1115 #ifdef CONFIG_TRACER_MAX_TRACE
1116 if (type->use_max_tr) {
1117 tr->allocated_snapshot = false;
1119 /* Shrink the max buffer again */
1120 if (ring_buffer_expanded)
1121 ring_buffer_resize(tr->max_buffer.buffer, 1,
1122 RING_BUFFER_ALL_CPUS);
1124 #endif
1126 printk(KERN_CONT "PASSED\n");
1127 return 0;
1129 #else
1130 static inline int run_tracer_selftest(struct tracer *type)
1132 return 0;
1134 #endif /* CONFIG_FTRACE_STARTUP_TEST */
1137 * register_tracer - register a tracer with the ftrace system.
1138 * @type - the plugin for the tracer
1140 * Register a new plugin tracer.
1142 int register_tracer(struct tracer *type)
1144 struct tracer *t;
1145 int ret = 0;
1147 if (!type->name) {
1148 pr_info("Tracer must have a name\n");
1149 return -1;
1152 if (strlen(type->name) >= MAX_TRACER_SIZE) {
1153 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1154 return -1;
1157 mutex_lock(&trace_types_lock);
1159 tracing_selftest_running = true;
1161 for (t = trace_types; t; t = t->next) {
1162 if (strcmp(type->name, t->name) == 0) {
1163 /* already found */
1164 pr_info("Tracer %s already registered\n",
1165 type->name);
1166 ret = -1;
1167 goto out;
1171 if (!type->set_flag)
1172 type->set_flag = &dummy_set_flag;
1173 if (!type->flags)
1174 type->flags = &dummy_tracer_flags;
1175 else
1176 if (!type->flags->opts)
1177 type->flags->opts = dummy_tracer_opt;
1178 if (!type->wait_pipe)
1179 type->wait_pipe = default_wait_pipe;
1181 ret = run_tracer_selftest(type);
1182 if (ret < 0)
1183 goto out;
1185 type->next = trace_types;
1186 trace_types = type;
1188 out:
1189 tracing_selftest_running = false;
1190 mutex_unlock(&trace_types_lock);
1192 if (ret || !default_bootup_tracer)
1193 goto out_unlock;
1195 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
1196 goto out_unlock;
1198 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1199 /* Do we want this tracer to start on bootup? */
1200 tracing_set_tracer(type->name);
1201 default_bootup_tracer = NULL;
1202 /* disable other selftests, since this will break it. */
1203 tracing_selftest_disabled = true;
1204 #ifdef CONFIG_FTRACE_STARTUP_TEST
1205 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1206 type->name);
1207 #endif
1209 out_unlock:
1210 return ret;
1213 void tracing_reset(struct trace_buffer *buf, int cpu)
1215 struct ring_buffer *buffer = buf->buffer;
1217 if (!buffer)
1218 return;
1220 ring_buffer_record_disable(buffer);
1222 /* Make sure all commits have finished */
1223 synchronize_sched();
1224 ring_buffer_reset_cpu(buffer, cpu);
1226 ring_buffer_record_enable(buffer);
1229 void tracing_reset_online_cpus(struct trace_buffer *buf)
1231 struct ring_buffer *buffer = buf->buffer;
1232 int cpu;
1234 if (!buffer)
1235 return;
1237 ring_buffer_record_disable(buffer);
1239 /* Make sure all commits have finished */
1240 synchronize_sched();
1242 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
1244 for_each_online_cpu(cpu)
1245 ring_buffer_reset_cpu(buffer, cpu);
1247 ring_buffer_record_enable(buffer);
1250 /* Must have trace_types_lock held */
1251 void tracing_reset_all_online_cpus(void)
1253 struct trace_array *tr;
1255 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1256 tracing_reset_online_cpus(&tr->trace_buffer);
1257 #ifdef CONFIG_TRACER_MAX_TRACE
1258 tracing_reset_online_cpus(&tr->max_buffer);
1259 #endif
1263 #define SAVED_CMDLINES 128
1264 #define NO_CMDLINE_MAP UINT_MAX
1265 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1266 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1267 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1268 static int cmdline_idx;
1269 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1271 /* temporary disable recording */
1272 static atomic_t trace_record_cmdline_disabled __read_mostly;
1274 static void trace_init_cmdlines(void)
1276 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1277 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
1278 cmdline_idx = 0;
1281 int is_tracing_stopped(void)
1283 return global_trace.stop_count;
1287 * ftrace_off_permanent - disable all ftrace code permanently
1289 * This should only be called when a serious anomally has
1290 * been detected. This will turn off the function tracing,
1291 * ring buffers, and other tracing utilites. It takes no
1292 * locks and can be called from any context.
1294 void ftrace_off_permanent(void)
1296 tracing_disabled = 1;
1297 ftrace_stop();
1298 tracing_off_permanent();
1302 * tracing_start - quick start of the tracer
1304 * If tracing is enabled but was stopped by tracing_stop,
1305 * this will start the tracer back up.
1307 void tracing_start(void)
1309 struct ring_buffer *buffer;
1310 unsigned long flags;
1312 if (tracing_disabled)
1313 return;
1315 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1316 if (--global_trace.stop_count) {
1317 if (global_trace.stop_count < 0) {
1318 /* Someone screwed up their debugging */
1319 WARN_ON_ONCE(1);
1320 global_trace.stop_count = 0;
1322 goto out;
1325 /* Prevent the buffers from switching */
1326 arch_spin_lock(&ftrace_max_lock);
1328 buffer = global_trace.trace_buffer.buffer;
1329 if (buffer)
1330 ring_buffer_record_enable(buffer);
1332 #ifdef CONFIG_TRACER_MAX_TRACE
1333 buffer = global_trace.max_buffer.buffer;
1334 if (buffer)
1335 ring_buffer_record_enable(buffer);
1336 #endif
1338 arch_spin_unlock(&ftrace_max_lock);
1340 out:
1341 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1344 static void tracing_start_tr(struct trace_array *tr)
1346 struct ring_buffer *buffer;
1347 unsigned long flags;
1349 if (tracing_disabled)
1350 return;
1352 /* If global, we need to also start the max tracer */
1353 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1354 return tracing_start();
1356 raw_spin_lock_irqsave(&tr->start_lock, flags);
1358 if (--tr->stop_count) {
1359 if (tr->stop_count < 0) {
1360 /* Someone screwed up their debugging */
1361 WARN_ON_ONCE(1);
1362 tr->stop_count = 0;
1364 goto out;
1367 buffer = tr->trace_buffer.buffer;
1368 if (buffer)
1369 ring_buffer_record_enable(buffer);
1371 out:
1372 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1376 * tracing_stop - quick stop of the tracer
1378 * Light weight way to stop tracing. Use in conjunction with
1379 * tracing_start.
1381 void tracing_stop(void)
1383 struct ring_buffer *buffer;
1384 unsigned long flags;
1386 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1387 if (global_trace.stop_count++)
1388 goto out;
1390 /* Prevent the buffers from switching */
1391 arch_spin_lock(&ftrace_max_lock);
1393 buffer = global_trace.trace_buffer.buffer;
1394 if (buffer)
1395 ring_buffer_record_disable(buffer);
1397 #ifdef CONFIG_TRACER_MAX_TRACE
1398 buffer = global_trace.max_buffer.buffer;
1399 if (buffer)
1400 ring_buffer_record_disable(buffer);
1401 #endif
1403 arch_spin_unlock(&ftrace_max_lock);
1405 out:
1406 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1409 static void tracing_stop_tr(struct trace_array *tr)
1411 struct ring_buffer *buffer;
1412 unsigned long flags;
1414 /* If global, we need to also stop the max tracer */
1415 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1416 return tracing_stop();
1418 raw_spin_lock_irqsave(&tr->start_lock, flags);
1419 if (tr->stop_count++)
1420 goto out;
1422 buffer = tr->trace_buffer.buffer;
1423 if (buffer)
1424 ring_buffer_record_disable(buffer);
1426 out:
1427 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1430 void trace_stop_cmdline_recording(void);
1432 static int trace_save_cmdline(struct task_struct *tsk)
1434 unsigned pid, idx;
1436 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1437 return 0;
1440 * It's not the end of the world if we don't get
1441 * the lock, but we also don't want to spin
1442 * nor do we want to disable interrupts,
1443 * so if we miss here, then better luck next time.
1445 if (!arch_spin_trylock(&trace_cmdline_lock))
1446 return 0;
1448 idx = map_pid_to_cmdline[tsk->pid];
1449 if (idx == NO_CMDLINE_MAP) {
1450 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1453 * Check whether the cmdline buffer at idx has a pid
1454 * mapped. We are going to overwrite that entry so we
1455 * need to clear the map_pid_to_cmdline. Otherwise we
1456 * would read the new comm for the old pid.
1458 pid = map_cmdline_to_pid[idx];
1459 if (pid != NO_CMDLINE_MAP)
1460 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1462 map_cmdline_to_pid[idx] = tsk->pid;
1463 map_pid_to_cmdline[tsk->pid] = idx;
1465 cmdline_idx = idx;
1468 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1470 arch_spin_unlock(&trace_cmdline_lock);
1472 return 1;
1475 void trace_find_cmdline(int pid, char comm[])
1477 unsigned map;
1479 if (!pid) {
1480 strcpy(comm, "<idle>");
1481 return;
1484 if (WARN_ON_ONCE(pid < 0)) {
1485 strcpy(comm, "<XXX>");
1486 return;
1489 if (pid > PID_MAX_DEFAULT) {
1490 strcpy(comm, "<...>");
1491 return;
1494 preempt_disable();
1495 arch_spin_lock(&trace_cmdline_lock);
1496 map = map_pid_to_cmdline[pid];
1497 if (map != NO_CMDLINE_MAP)
1498 strcpy(comm, saved_cmdlines[map]);
1499 else
1500 strcpy(comm, "<...>");
1502 arch_spin_unlock(&trace_cmdline_lock);
1503 preempt_enable();
1506 void tracing_record_cmdline(struct task_struct *tsk)
1508 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1509 return;
1511 if (!__this_cpu_read(trace_cmdline_save))
1512 return;
1514 if (trace_save_cmdline(tsk))
1515 __this_cpu_write(trace_cmdline_save, false);
1518 void
1519 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1520 int pc)
1522 struct task_struct *tsk = current;
1524 entry->preempt_count = pc & 0xff;
1525 entry->pid = (tsk) ? tsk->pid : 0;
1526 entry->flags =
1527 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1528 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1529 #else
1530 TRACE_FLAG_IRQS_NOSUPPORT |
1531 #endif
1532 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1533 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1534 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1536 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1538 struct ring_buffer_event *
1539 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1540 int type,
1541 unsigned long len,
1542 unsigned long flags, int pc)
1544 struct ring_buffer_event *event;
1546 event = ring_buffer_lock_reserve(buffer, len);
1547 if (event != NULL) {
1548 struct trace_entry *ent = ring_buffer_event_data(event);
1550 tracing_generic_entry_update(ent, flags, pc);
1551 ent->type = type;
1554 return event;
1557 void
1558 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1560 __this_cpu_write(trace_cmdline_save, true);
1561 ring_buffer_unlock_commit(buffer, event);
1564 static inline void
1565 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1566 struct ring_buffer_event *event,
1567 unsigned long flags, int pc)
1569 __buffer_unlock_commit(buffer, event);
1571 ftrace_trace_stack(buffer, flags, 6, pc);
1572 ftrace_trace_userstack(buffer, flags, pc);
1575 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1576 struct ring_buffer_event *event,
1577 unsigned long flags, int pc)
1579 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1581 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1583 struct ring_buffer_event *
1584 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1585 struct ftrace_event_file *ftrace_file,
1586 int type, unsigned long len,
1587 unsigned long flags, int pc)
1589 *current_rb = ftrace_file->tr->trace_buffer.buffer;
1590 return trace_buffer_lock_reserve(*current_rb,
1591 type, len, flags, pc);
1593 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1595 struct ring_buffer_event *
1596 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1597 int type, unsigned long len,
1598 unsigned long flags, int pc)
1600 *current_rb = global_trace.trace_buffer.buffer;
1601 return trace_buffer_lock_reserve(*current_rb,
1602 type, len, flags, pc);
1604 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1606 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1607 struct ring_buffer_event *event,
1608 unsigned long flags, int pc)
1610 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1612 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1614 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1615 struct ring_buffer_event *event,
1616 unsigned long flags, int pc,
1617 struct pt_regs *regs)
1619 __buffer_unlock_commit(buffer, event);
1621 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1622 ftrace_trace_userstack(buffer, flags, pc);
1624 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1626 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1627 struct ring_buffer_event *event)
1629 ring_buffer_discard_commit(buffer, event);
1631 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1633 void
1634 trace_function(struct trace_array *tr,
1635 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1636 int pc)
1638 struct ftrace_event_call *call = &event_function;
1639 struct ring_buffer *buffer = tr->trace_buffer.buffer;
1640 struct ring_buffer_event *event;
1641 struct ftrace_entry *entry;
1643 /* If we are reading the ring buffer, don't trace */
1644 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1645 return;
1647 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1648 flags, pc);
1649 if (!event)
1650 return;
1651 entry = ring_buffer_event_data(event);
1652 entry->ip = ip;
1653 entry->parent_ip = parent_ip;
1655 if (!filter_check_discard(call, entry, buffer, event))
1656 __buffer_unlock_commit(buffer, event);
1659 #ifdef CONFIG_STACKTRACE
1661 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1662 struct ftrace_stack {
1663 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1666 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1667 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1669 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1670 unsigned long flags,
1671 int skip, int pc, struct pt_regs *regs)
1673 struct ftrace_event_call *call = &event_kernel_stack;
1674 struct ring_buffer_event *event;
1675 struct stack_entry *entry;
1676 struct stack_trace trace;
1677 int use_stack;
1678 int size = FTRACE_STACK_ENTRIES;
1680 trace.nr_entries = 0;
1681 trace.skip = skip;
1684 * Since events can happen in NMIs there's no safe way to
1685 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1686 * or NMI comes in, it will just have to use the default
1687 * FTRACE_STACK_SIZE.
1689 preempt_disable_notrace();
1691 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1693 * We don't need any atomic variables, just a barrier.
1694 * If an interrupt comes in, we don't care, because it would
1695 * have exited and put the counter back to what we want.
1696 * We just need a barrier to keep gcc from moving things
1697 * around.
1699 barrier();
1700 if (use_stack == 1) {
1701 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1702 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1704 if (regs)
1705 save_stack_trace_regs(regs, &trace);
1706 else
1707 save_stack_trace(&trace);
1709 if (trace.nr_entries > size)
1710 size = trace.nr_entries;
1711 } else
1712 /* From now on, use_stack is a boolean */
1713 use_stack = 0;
1715 size *= sizeof(unsigned long);
1717 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1718 sizeof(*entry) + size, flags, pc);
1719 if (!event)
1720 goto out;
1721 entry = ring_buffer_event_data(event);
1723 memset(&entry->caller, 0, size);
1725 if (use_stack)
1726 memcpy(&entry->caller, trace.entries,
1727 trace.nr_entries * sizeof(unsigned long));
1728 else {
1729 trace.max_entries = FTRACE_STACK_ENTRIES;
1730 trace.entries = entry->caller;
1731 if (regs)
1732 save_stack_trace_regs(regs, &trace);
1733 else
1734 save_stack_trace(&trace);
1737 entry->size = trace.nr_entries;
1739 if (!filter_check_discard(call, entry, buffer, event))
1740 __buffer_unlock_commit(buffer, event);
1742 out:
1743 /* Again, don't let gcc optimize things here */
1744 barrier();
1745 __this_cpu_dec(ftrace_stack_reserve);
1746 preempt_enable_notrace();
1750 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1751 int skip, int pc, struct pt_regs *regs)
1753 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1754 return;
1756 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1759 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1760 int skip, int pc)
1762 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1763 return;
1765 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1768 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1769 int pc)
1771 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
1775 * trace_dump_stack - record a stack back trace in the trace buffer
1776 * @skip: Number of functions to skip (helper handlers)
1778 void trace_dump_stack(int skip)
1780 unsigned long flags;
1782 if (tracing_disabled || tracing_selftest_running)
1783 return;
1785 local_save_flags(flags);
1788 * Skip 3 more, seems to get us at the caller of
1789 * this function.
1791 skip += 3;
1792 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1793 flags, skip, preempt_count(), NULL);
1796 static DEFINE_PER_CPU(int, user_stack_count);
1798 void
1799 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1801 struct ftrace_event_call *call = &event_user_stack;
1802 struct ring_buffer_event *event;
1803 struct userstack_entry *entry;
1804 struct stack_trace trace;
1806 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1807 return;
1810 * NMIs can not handle page faults, even with fix ups.
1811 * The save user stack can (and often does) fault.
1813 if (unlikely(in_nmi()))
1814 return;
1817 * prevent recursion, since the user stack tracing may
1818 * trigger other kernel events.
1820 preempt_disable();
1821 if (__this_cpu_read(user_stack_count))
1822 goto out;
1824 __this_cpu_inc(user_stack_count);
1826 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1827 sizeof(*entry), flags, pc);
1828 if (!event)
1829 goto out_drop_count;
1830 entry = ring_buffer_event_data(event);
1832 entry->tgid = current->tgid;
1833 memset(&entry->caller, 0, sizeof(entry->caller));
1835 trace.nr_entries = 0;
1836 trace.max_entries = FTRACE_STACK_ENTRIES;
1837 trace.skip = 0;
1838 trace.entries = entry->caller;
1840 save_stack_trace_user(&trace);
1841 if (!filter_check_discard(call, entry, buffer, event))
1842 __buffer_unlock_commit(buffer, event);
1844 out_drop_count:
1845 __this_cpu_dec(user_stack_count);
1846 out:
1847 preempt_enable();
1850 #ifdef UNUSED
1851 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1853 ftrace_trace_userstack(tr, flags, preempt_count());
1855 #endif /* UNUSED */
1857 #endif /* CONFIG_STACKTRACE */
1859 /* created for use with alloc_percpu */
1860 struct trace_buffer_struct {
1861 char buffer[TRACE_BUF_SIZE];
1864 static struct trace_buffer_struct *trace_percpu_buffer;
1865 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1866 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1867 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1870 * The buffer used is dependent on the context. There is a per cpu
1871 * buffer for normal context, softirq contex, hard irq context and
1872 * for NMI context. Thise allows for lockless recording.
1874 * Note, if the buffers failed to be allocated, then this returns NULL
1876 static char *get_trace_buf(void)
1878 struct trace_buffer_struct *percpu_buffer;
1881 * If we have allocated per cpu buffers, then we do not
1882 * need to do any locking.
1884 if (in_nmi())
1885 percpu_buffer = trace_percpu_nmi_buffer;
1886 else if (in_irq())
1887 percpu_buffer = trace_percpu_irq_buffer;
1888 else if (in_softirq())
1889 percpu_buffer = trace_percpu_sirq_buffer;
1890 else
1891 percpu_buffer = trace_percpu_buffer;
1893 if (!percpu_buffer)
1894 return NULL;
1896 return this_cpu_ptr(&percpu_buffer->buffer[0]);
1899 static int alloc_percpu_trace_buffer(void)
1901 struct trace_buffer_struct *buffers;
1902 struct trace_buffer_struct *sirq_buffers;
1903 struct trace_buffer_struct *irq_buffers;
1904 struct trace_buffer_struct *nmi_buffers;
1906 buffers = alloc_percpu(struct trace_buffer_struct);
1907 if (!buffers)
1908 goto err_warn;
1910 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1911 if (!sirq_buffers)
1912 goto err_sirq;
1914 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1915 if (!irq_buffers)
1916 goto err_irq;
1918 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1919 if (!nmi_buffers)
1920 goto err_nmi;
1922 trace_percpu_buffer = buffers;
1923 trace_percpu_sirq_buffer = sirq_buffers;
1924 trace_percpu_irq_buffer = irq_buffers;
1925 trace_percpu_nmi_buffer = nmi_buffers;
1927 return 0;
1929 err_nmi:
1930 free_percpu(irq_buffers);
1931 err_irq:
1932 free_percpu(sirq_buffers);
1933 err_sirq:
1934 free_percpu(buffers);
1935 err_warn:
1936 WARN(1, "Could not allocate percpu trace_printk buffer");
1937 return -ENOMEM;
1940 static int buffers_allocated;
1942 void trace_printk_init_buffers(void)
1944 if (buffers_allocated)
1945 return;
1947 if (alloc_percpu_trace_buffer())
1948 return;
1950 pr_info("ftrace: Allocated trace_printk buffers\n");
1952 /* Expand the buffers to set size */
1953 tracing_update_buffers();
1955 buffers_allocated = 1;
1958 * trace_printk_init_buffers() can be called by modules.
1959 * If that happens, then we need to start cmdline recording
1960 * directly here. If the global_trace.buffer is already
1961 * allocated here, then this was called by module code.
1963 if (global_trace.trace_buffer.buffer)
1964 tracing_start_cmdline_record();
1967 void trace_printk_start_comm(void)
1969 /* Start tracing comms if trace printk is set */
1970 if (!buffers_allocated)
1971 return;
1972 tracing_start_cmdline_record();
1975 static void trace_printk_start_stop_comm(int enabled)
1977 if (!buffers_allocated)
1978 return;
1980 if (enabled)
1981 tracing_start_cmdline_record();
1982 else
1983 tracing_stop_cmdline_record();
1987 * trace_vbprintk - write binary msg to tracing buffer
1990 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1992 struct ftrace_event_call *call = &event_bprint;
1993 struct ring_buffer_event *event;
1994 struct ring_buffer *buffer;
1995 struct trace_array *tr = &global_trace;
1996 struct bprint_entry *entry;
1997 unsigned long flags;
1998 char *tbuffer;
1999 int len = 0, size, pc;
2001 if (unlikely(tracing_selftest_running || tracing_disabled))
2002 return 0;
2004 /* Don't pollute graph traces with trace_vprintk internals */
2005 pause_graph_tracing();
2007 pc = preempt_count();
2008 preempt_disable_notrace();
2010 tbuffer = get_trace_buf();
2011 if (!tbuffer) {
2012 len = 0;
2013 goto out;
2016 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
2018 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
2019 goto out;
2021 local_save_flags(flags);
2022 size = sizeof(*entry) + sizeof(u32) * len;
2023 buffer = tr->trace_buffer.buffer;
2024 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
2025 flags, pc);
2026 if (!event)
2027 goto out;
2028 entry = ring_buffer_event_data(event);
2029 entry->ip = ip;
2030 entry->fmt = fmt;
2032 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
2033 if (!filter_check_discard(call, entry, buffer, event)) {
2034 __buffer_unlock_commit(buffer, event);
2035 ftrace_trace_stack(buffer, flags, 6, pc);
2038 out:
2039 preempt_enable_notrace();
2040 unpause_graph_tracing();
2042 return len;
2044 EXPORT_SYMBOL_GPL(trace_vbprintk);
2046 static int
2047 __trace_array_vprintk(struct ring_buffer *buffer,
2048 unsigned long ip, const char *fmt, va_list args)
2050 struct ftrace_event_call *call = &event_print;
2051 struct ring_buffer_event *event;
2052 int len = 0, size, pc;
2053 struct print_entry *entry;
2054 unsigned long flags;
2055 char *tbuffer;
2057 if (tracing_disabled || tracing_selftest_running)
2058 return 0;
2060 /* Don't pollute graph traces with trace_vprintk internals */
2061 pause_graph_tracing();
2063 pc = preempt_count();
2064 preempt_disable_notrace();
2067 tbuffer = get_trace_buf();
2068 if (!tbuffer) {
2069 len = 0;
2070 goto out;
2073 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
2074 if (len > TRACE_BUF_SIZE)
2075 goto out;
2077 local_save_flags(flags);
2078 size = sizeof(*entry) + len + 1;
2079 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
2080 flags, pc);
2081 if (!event)
2082 goto out;
2083 entry = ring_buffer_event_data(event);
2084 entry->ip = ip;
2086 memcpy(&entry->buf, tbuffer, len);
2087 entry->buf[len] = '\0';
2088 if (!filter_check_discard(call, entry, buffer, event)) {
2089 __buffer_unlock_commit(buffer, event);
2090 ftrace_trace_stack(buffer, flags, 6, pc);
2092 out:
2093 preempt_enable_notrace();
2094 unpause_graph_tracing();
2096 return len;
2099 int trace_array_vprintk(struct trace_array *tr,
2100 unsigned long ip, const char *fmt, va_list args)
2102 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2105 int trace_array_printk(struct trace_array *tr,
2106 unsigned long ip, const char *fmt, ...)
2108 int ret;
2109 va_list ap;
2111 if (!(trace_flags & TRACE_ITER_PRINTK))
2112 return 0;
2114 va_start(ap, fmt);
2115 ret = trace_array_vprintk(tr, ip, fmt, ap);
2116 va_end(ap);
2117 return ret;
2120 int trace_array_printk_buf(struct ring_buffer *buffer,
2121 unsigned long ip, const char *fmt, ...)
2123 int ret;
2124 va_list ap;
2126 if (!(trace_flags & TRACE_ITER_PRINTK))
2127 return 0;
2129 va_start(ap, fmt);
2130 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2131 va_end(ap);
2132 return ret;
2135 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2137 return trace_array_vprintk(&global_trace, ip, fmt, args);
2139 EXPORT_SYMBOL_GPL(trace_vprintk);
2141 static void trace_iterator_increment(struct trace_iterator *iter)
2143 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2145 iter->idx++;
2146 if (buf_iter)
2147 ring_buffer_read(buf_iter, NULL);
2150 static struct trace_entry *
2151 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2152 unsigned long *lost_events)
2154 struct ring_buffer_event *event;
2155 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
2157 if (buf_iter)
2158 event = ring_buffer_iter_peek(buf_iter, ts);
2159 else
2160 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
2161 lost_events);
2163 if (event) {
2164 iter->ent_size = ring_buffer_event_length(event);
2165 return ring_buffer_event_data(event);
2167 iter->ent_size = 0;
2168 return NULL;
2171 static struct trace_entry *
2172 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2173 unsigned long *missing_events, u64 *ent_ts)
2175 struct ring_buffer *buffer = iter->trace_buffer->buffer;
2176 struct trace_entry *ent, *next = NULL;
2177 unsigned long lost_events = 0, next_lost = 0;
2178 int cpu_file = iter->cpu_file;
2179 u64 next_ts = 0, ts;
2180 int next_cpu = -1;
2181 int next_size = 0;
2182 int cpu;
2185 * If we are in a per_cpu trace file, don't bother by iterating over
2186 * all cpu and peek directly.
2188 if (cpu_file > RING_BUFFER_ALL_CPUS) {
2189 if (ring_buffer_empty_cpu(buffer, cpu_file))
2190 return NULL;
2191 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
2192 if (ent_cpu)
2193 *ent_cpu = cpu_file;
2195 return ent;
2198 for_each_tracing_cpu(cpu) {
2200 if (ring_buffer_empty_cpu(buffer, cpu))
2201 continue;
2203 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
2206 * Pick the entry with the smallest timestamp:
2208 if (ent && (!next || ts < next_ts)) {
2209 next = ent;
2210 next_cpu = cpu;
2211 next_ts = ts;
2212 next_lost = lost_events;
2213 next_size = iter->ent_size;
2217 iter->ent_size = next_size;
2219 if (ent_cpu)
2220 *ent_cpu = next_cpu;
2222 if (ent_ts)
2223 *ent_ts = next_ts;
2225 if (missing_events)
2226 *missing_events = next_lost;
2228 return next;
2231 /* Find the next real entry, without updating the iterator itself */
2232 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2233 int *ent_cpu, u64 *ent_ts)
2235 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
2238 /* Find the next real entry, and increment the iterator to the next entry */
2239 void *trace_find_next_entry_inc(struct trace_iterator *iter)
2241 iter->ent = __find_next_entry(iter, &iter->cpu,
2242 &iter->lost_events, &iter->ts);
2244 if (iter->ent)
2245 trace_iterator_increment(iter);
2247 return iter->ent ? iter : NULL;
2250 static void trace_consume(struct trace_iterator *iter)
2252 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
2253 &iter->lost_events);
2256 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
2258 struct trace_iterator *iter = m->private;
2259 int i = (int)*pos;
2260 void *ent;
2262 WARN_ON_ONCE(iter->leftover);
2264 (*pos)++;
2266 /* can't go backwards */
2267 if (iter->idx > i)
2268 return NULL;
2270 if (iter->idx < 0)
2271 ent = trace_find_next_entry_inc(iter);
2272 else
2273 ent = iter;
2275 while (ent && iter->idx < i)
2276 ent = trace_find_next_entry_inc(iter);
2278 iter->pos = *pos;
2280 return ent;
2283 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2285 struct ring_buffer_event *event;
2286 struct ring_buffer_iter *buf_iter;
2287 unsigned long entries = 0;
2288 u64 ts;
2290 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2292 buf_iter = trace_buffer_iter(iter, cpu);
2293 if (!buf_iter)
2294 return;
2296 ring_buffer_iter_reset(buf_iter);
2299 * We could have the case with the max latency tracers
2300 * that a reset never took place on a cpu. This is evident
2301 * by the timestamp being before the start of the buffer.
2303 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
2304 if (ts >= iter->trace_buffer->time_start)
2305 break;
2306 entries++;
2307 ring_buffer_read(buf_iter, NULL);
2310 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2314 * The current tracer is copied to avoid a global locking
2315 * all around.
2317 static void *s_start(struct seq_file *m, loff_t *pos)
2319 struct trace_iterator *iter = m->private;
2320 struct trace_array *tr = iter->tr;
2321 int cpu_file = iter->cpu_file;
2322 void *p = NULL;
2323 loff_t l = 0;
2324 int cpu;
2327 * copy the tracer to avoid using a global lock all around.
2328 * iter->trace is a copy of current_trace, the pointer to the
2329 * name may be used instead of a strcmp(), as iter->trace->name
2330 * will point to the same string as current_trace->name.
2332 mutex_lock(&trace_types_lock);
2333 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2334 *iter->trace = *tr->current_trace;
2335 mutex_unlock(&trace_types_lock);
2337 #ifdef CONFIG_TRACER_MAX_TRACE
2338 if (iter->snapshot && iter->trace->use_max_tr)
2339 return ERR_PTR(-EBUSY);
2340 #endif
2342 if (!iter->snapshot)
2343 atomic_inc(&trace_record_cmdline_disabled);
2345 if (*pos != iter->pos) {
2346 iter->ent = NULL;
2347 iter->cpu = 0;
2348 iter->idx = -1;
2350 if (cpu_file == RING_BUFFER_ALL_CPUS) {
2351 for_each_tracing_cpu(cpu)
2352 tracing_iter_reset(iter, cpu);
2353 } else
2354 tracing_iter_reset(iter, cpu_file);
2356 iter->leftover = 0;
2357 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2360 } else {
2362 * If we overflowed the seq_file before, then we want
2363 * to just reuse the trace_seq buffer again.
2365 if (iter->leftover)
2366 p = iter;
2367 else {
2368 l = *pos - 1;
2369 p = s_next(m, p, &l);
2373 trace_event_read_lock();
2374 trace_access_lock(cpu_file);
2375 return p;
2378 static void s_stop(struct seq_file *m, void *p)
2380 struct trace_iterator *iter = m->private;
2382 #ifdef CONFIG_TRACER_MAX_TRACE
2383 if (iter->snapshot && iter->trace->use_max_tr)
2384 return;
2385 #endif
2387 if (!iter->snapshot)
2388 atomic_dec(&trace_record_cmdline_disabled);
2390 trace_access_unlock(iter->cpu_file);
2391 trace_event_read_unlock();
2394 static void
2395 get_total_entries(struct trace_buffer *buf,
2396 unsigned long *total, unsigned long *entries)
2398 unsigned long count;
2399 int cpu;
2401 *total = 0;
2402 *entries = 0;
2404 for_each_tracing_cpu(cpu) {
2405 count = ring_buffer_entries_cpu(buf->buffer, cpu);
2407 * If this buffer has skipped entries, then we hold all
2408 * entries for the trace and we need to ignore the
2409 * ones before the time stamp.
2411 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2412 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
2413 /* total is the same as the entries */
2414 *total += count;
2415 } else
2416 *total += count +
2417 ring_buffer_overrun_cpu(buf->buffer, cpu);
2418 *entries += count;
2422 static void print_lat_help_header(struct seq_file *m)
2424 seq_puts(m, "# _------=> CPU# \n");
2425 seq_puts(m, "# / _-----=> irqs-off \n");
2426 seq_puts(m, "# | / _----=> need-resched \n");
2427 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2428 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2429 seq_puts(m, "# |||| / delay \n");
2430 seq_puts(m, "# cmd pid ||||| time | caller \n");
2431 seq_puts(m, "# \\ / ||||| \\ | / \n");
2434 static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
2436 unsigned long total;
2437 unsigned long entries;
2439 get_total_entries(buf, &total, &entries);
2440 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2441 entries, total, num_online_cpus());
2442 seq_puts(m, "#\n");
2445 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
2447 print_event_info(buf, m);
2448 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2449 seq_puts(m, "# | | | | |\n");
2452 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
2454 print_event_info(buf, m);
2455 seq_puts(m, "# _-----=> irqs-off\n");
2456 seq_puts(m, "# / _----=> need-resched\n");
2457 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2458 seq_puts(m, "# || / _--=> preempt-depth\n");
2459 seq_puts(m, "# ||| / delay\n");
2460 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2461 seq_puts(m, "# | | | |||| | |\n");
2464 void
2465 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2467 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2468 struct trace_buffer *buf = iter->trace_buffer;
2469 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2470 struct tracer *type = iter->trace;
2471 unsigned long entries;
2472 unsigned long total;
2473 const char *name = "preemption";
2475 name = type->name;
2477 get_total_entries(buf, &total, &entries);
2479 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2480 name, UTS_RELEASE);
2481 seq_puts(m, "# -----------------------------------"
2482 "---------------------------------\n");
2483 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2484 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2485 nsecs_to_usecs(data->saved_latency),
2486 entries,
2487 total,
2488 buf->cpu,
2489 #if defined(CONFIG_PREEMPT_NONE)
2490 "server",
2491 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2492 "desktop",
2493 #elif defined(CONFIG_PREEMPT)
2494 "preempt",
2495 #else
2496 "unknown",
2497 #endif
2498 /* These are reserved for later use */
2499 0, 0, 0, 0);
2500 #ifdef CONFIG_SMP
2501 seq_printf(m, " #P:%d)\n", num_online_cpus());
2502 #else
2503 seq_puts(m, ")\n");
2504 #endif
2505 seq_puts(m, "# -----------------\n");
2506 seq_printf(m, "# | task: %.16s-%d "
2507 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2508 data->comm, data->pid,
2509 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2510 data->policy, data->rt_priority);
2511 seq_puts(m, "# -----------------\n");
2513 if (data->critical_start) {
2514 seq_puts(m, "# => started at: ");
2515 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2516 trace_print_seq(m, &iter->seq);
2517 seq_puts(m, "\n# => ended at: ");
2518 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2519 trace_print_seq(m, &iter->seq);
2520 seq_puts(m, "\n#\n");
2523 seq_puts(m, "#\n");
2526 static void test_cpu_buff_start(struct trace_iterator *iter)
2528 struct trace_seq *s = &iter->seq;
2530 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2531 return;
2533 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2534 return;
2536 if (cpumask_test_cpu(iter->cpu, iter->started))
2537 return;
2539 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2540 return;
2542 cpumask_set_cpu(iter->cpu, iter->started);
2544 /* Don't print started cpu buffer for the first entry of the trace */
2545 if (iter->idx > 1)
2546 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2547 iter->cpu);
2550 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2552 struct trace_seq *s = &iter->seq;
2553 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2554 struct trace_entry *entry;
2555 struct trace_event *event;
2557 entry = iter->ent;
2559 test_cpu_buff_start(iter);
2561 event = ftrace_find_event(entry->type);
2563 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2564 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2565 if (!trace_print_lat_context(iter))
2566 goto partial;
2567 } else {
2568 if (!trace_print_context(iter))
2569 goto partial;
2573 if (event)
2574 return event->funcs->trace(iter, sym_flags, event);
2576 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2577 goto partial;
2579 return TRACE_TYPE_HANDLED;
2580 partial:
2581 return TRACE_TYPE_PARTIAL_LINE;
2584 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2586 struct trace_seq *s = &iter->seq;
2587 struct trace_entry *entry;
2588 struct trace_event *event;
2590 entry = iter->ent;
2592 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2593 if (!trace_seq_printf(s, "%d %d %llu ",
2594 entry->pid, iter->cpu, iter->ts))
2595 goto partial;
2598 event = ftrace_find_event(entry->type);
2599 if (event)
2600 return event->funcs->raw(iter, 0, event);
2602 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2603 goto partial;
2605 return TRACE_TYPE_HANDLED;
2606 partial:
2607 return TRACE_TYPE_PARTIAL_LINE;
2610 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2612 struct trace_seq *s = &iter->seq;
2613 unsigned char newline = '\n';
2614 struct trace_entry *entry;
2615 struct trace_event *event;
2617 entry = iter->ent;
2619 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2620 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2621 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2622 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2625 event = ftrace_find_event(entry->type);
2626 if (event) {
2627 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2628 if (ret != TRACE_TYPE_HANDLED)
2629 return ret;
2632 SEQ_PUT_FIELD_RET(s, newline);
2634 return TRACE_TYPE_HANDLED;
2637 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2639 struct trace_seq *s = &iter->seq;
2640 struct trace_entry *entry;
2641 struct trace_event *event;
2643 entry = iter->ent;
2645 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2646 SEQ_PUT_FIELD_RET(s, entry->pid);
2647 SEQ_PUT_FIELD_RET(s, iter->cpu);
2648 SEQ_PUT_FIELD_RET(s, iter->ts);
2651 event = ftrace_find_event(entry->type);
2652 return event ? event->funcs->binary(iter, 0, event) :
2653 TRACE_TYPE_HANDLED;
2656 int trace_empty(struct trace_iterator *iter)
2658 struct ring_buffer_iter *buf_iter;
2659 int cpu;
2661 /* If we are looking at one CPU buffer, only check that one */
2662 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2663 cpu = iter->cpu_file;
2664 buf_iter = trace_buffer_iter(iter, cpu);
2665 if (buf_iter) {
2666 if (!ring_buffer_iter_empty(buf_iter))
2667 return 0;
2668 } else {
2669 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2670 return 0;
2672 return 1;
2675 for_each_tracing_cpu(cpu) {
2676 buf_iter = trace_buffer_iter(iter, cpu);
2677 if (buf_iter) {
2678 if (!ring_buffer_iter_empty(buf_iter))
2679 return 0;
2680 } else {
2681 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2682 return 0;
2686 return 1;
2689 /* Called with trace_event_read_lock() held. */
2690 enum print_line_t print_trace_line(struct trace_iterator *iter)
2692 enum print_line_t ret;
2694 if (iter->lost_events &&
2695 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2696 iter->cpu, iter->lost_events))
2697 return TRACE_TYPE_PARTIAL_LINE;
2699 if (iter->trace && iter->trace->print_line) {
2700 ret = iter->trace->print_line(iter);
2701 if (ret != TRACE_TYPE_UNHANDLED)
2702 return ret;
2705 if (iter->ent->type == TRACE_BPUTS &&
2706 trace_flags & TRACE_ITER_PRINTK &&
2707 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2708 return trace_print_bputs_msg_only(iter);
2710 if (iter->ent->type == TRACE_BPRINT &&
2711 trace_flags & TRACE_ITER_PRINTK &&
2712 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2713 return trace_print_bprintk_msg_only(iter);
2715 if (iter->ent->type == TRACE_PRINT &&
2716 trace_flags & TRACE_ITER_PRINTK &&
2717 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2718 return trace_print_printk_msg_only(iter);
2720 if (trace_flags & TRACE_ITER_BIN)
2721 return print_bin_fmt(iter);
2723 if (trace_flags & TRACE_ITER_HEX)
2724 return print_hex_fmt(iter);
2726 if (trace_flags & TRACE_ITER_RAW)
2727 return print_raw_fmt(iter);
2729 return print_trace_fmt(iter);
2732 void trace_latency_header(struct seq_file *m)
2734 struct trace_iterator *iter = m->private;
2736 /* print nothing if the buffers are empty */
2737 if (trace_empty(iter))
2738 return;
2740 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2741 print_trace_header(m, iter);
2743 if (!(trace_flags & TRACE_ITER_VERBOSE))
2744 print_lat_help_header(m);
2747 void trace_default_header(struct seq_file *m)
2749 struct trace_iterator *iter = m->private;
2751 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2752 return;
2754 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2755 /* print nothing if the buffers are empty */
2756 if (trace_empty(iter))
2757 return;
2758 print_trace_header(m, iter);
2759 if (!(trace_flags & TRACE_ITER_VERBOSE))
2760 print_lat_help_header(m);
2761 } else {
2762 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2763 if (trace_flags & TRACE_ITER_IRQ_INFO)
2764 print_func_help_header_irq(iter->trace_buffer, m);
2765 else
2766 print_func_help_header(iter->trace_buffer, m);
2771 static void test_ftrace_alive(struct seq_file *m)
2773 if (!ftrace_is_dead())
2774 return;
2775 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2776 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2779 #ifdef CONFIG_TRACER_MAX_TRACE
2780 static void show_snapshot_main_help(struct seq_file *m)
2782 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2783 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2784 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2785 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2786 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2787 seq_printf(m, "# is not a '0' or '1')\n");
2790 static void show_snapshot_percpu_help(struct seq_file *m)
2792 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2793 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2794 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2795 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2796 #else
2797 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2798 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2799 #endif
2800 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2801 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2802 seq_printf(m, "# is not a '0' or '1')\n");
2805 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2807 if (iter->tr->allocated_snapshot)
2808 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2809 else
2810 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2812 seq_printf(m, "# Snapshot commands:\n");
2813 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2814 show_snapshot_main_help(m);
2815 else
2816 show_snapshot_percpu_help(m);
2818 #else
2819 /* Should never be called */
2820 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2821 #endif
2823 static int s_show(struct seq_file *m, void *v)
2825 struct trace_iterator *iter = v;
2826 int ret;
2828 if (iter->ent == NULL) {
2829 if (iter->tr) {
2830 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2831 seq_puts(m, "#\n");
2832 test_ftrace_alive(m);
2834 if (iter->snapshot && trace_empty(iter))
2835 print_snapshot_help(m, iter);
2836 else if (iter->trace && iter->trace->print_header)
2837 iter->trace->print_header(m);
2838 else
2839 trace_default_header(m);
2841 } else if (iter->leftover) {
2843 * If we filled the seq_file buffer earlier, we
2844 * want to just show it now.
2846 ret = trace_print_seq(m, &iter->seq);
2848 /* ret should this time be zero, but you never know */
2849 iter->leftover = ret;
2851 } else {
2852 print_trace_line(iter);
2853 ret = trace_print_seq(m, &iter->seq);
2855 * If we overflow the seq_file buffer, then it will
2856 * ask us for this data again at start up.
2857 * Use that instead.
2858 * ret is 0 if seq_file write succeeded.
2859 * -1 otherwise.
2861 iter->leftover = ret;
2864 return 0;
2868 * Should be used after trace_array_get(), trace_types_lock
2869 * ensures that i_cdev was already initialized.
2871 static inline int tracing_get_cpu(struct inode *inode)
2873 if (inode->i_cdev) /* See trace_create_cpu_file() */
2874 return (long)inode->i_cdev - 1;
2875 return RING_BUFFER_ALL_CPUS;
2878 static const struct seq_operations tracer_seq_ops = {
2879 .start = s_start,
2880 .next = s_next,
2881 .stop = s_stop,
2882 .show = s_show,
2885 static struct trace_iterator *
2886 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2888 struct trace_array *tr = inode->i_private;
2889 struct trace_iterator *iter;
2890 int cpu;
2892 if (tracing_disabled)
2893 return ERR_PTR(-ENODEV);
2895 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2896 if (!iter)
2897 return ERR_PTR(-ENOMEM);
2899 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2900 GFP_KERNEL);
2901 if (!iter->buffer_iter)
2902 goto release;
2905 * We make a copy of the current tracer to avoid concurrent
2906 * changes on it while we are reading.
2908 mutex_lock(&trace_types_lock);
2909 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2910 if (!iter->trace)
2911 goto fail;
2913 *iter->trace = *tr->current_trace;
2915 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2916 goto fail;
2918 iter->tr = tr;
2920 #ifdef CONFIG_TRACER_MAX_TRACE
2921 /* Currently only the top directory has a snapshot */
2922 if (tr->current_trace->print_max || snapshot)
2923 iter->trace_buffer = &tr->max_buffer;
2924 else
2925 #endif
2926 iter->trace_buffer = &tr->trace_buffer;
2927 iter->snapshot = snapshot;
2928 iter->pos = -1;
2929 iter->cpu_file = tracing_get_cpu(inode);
2930 mutex_init(&iter->mutex);
2932 /* Notify the tracer early; before we stop tracing. */
2933 if (iter->trace && iter->trace->open)
2934 iter->trace->open(iter);
2936 /* Annotate start of buffers if we had overruns */
2937 if (ring_buffer_overruns(iter->trace_buffer->buffer))
2938 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2940 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2941 if (trace_clocks[tr->clock_id].in_ns)
2942 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2944 /* stop the trace while dumping if we are not opening "snapshot" */
2945 if (!iter->snapshot)
2946 tracing_stop_tr(tr);
2948 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
2949 for_each_tracing_cpu(cpu) {
2950 iter->buffer_iter[cpu] =
2951 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2953 ring_buffer_read_prepare_sync();
2954 for_each_tracing_cpu(cpu) {
2955 ring_buffer_read_start(iter->buffer_iter[cpu]);
2956 tracing_iter_reset(iter, cpu);
2958 } else {
2959 cpu = iter->cpu_file;
2960 iter->buffer_iter[cpu] =
2961 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2962 ring_buffer_read_prepare_sync();
2963 ring_buffer_read_start(iter->buffer_iter[cpu]);
2964 tracing_iter_reset(iter, cpu);
2967 mutex_unlock(&trace_types_lock);
2969 return iter;
2971 fail:
2972 mutex_unlock(&trace_types_lock);
2973 kfree(iter->trace);
2974 kfree(iter->buffer_iter);
2975 release:
2976 seq_release_private(inode, file);
2977 return ERR_PTR(-ENOMEM);
2980 int tracing_open_generic(struct inode *inode, struct file *filp)
2982 if (tracing_disabled)
2983 return -ENODEV;
2985 filp->private_data = inode->i_private;
2986 return 0;
2990 * Open and update trace_array ref count.
2991 * Must have the current trace_array passed to it.
2993 static int tracing_open_generic_tr(struct inode *inode, struct file *filp)
2995 struct trace_array *tr = inode->i_private;
2997 if (tracing_disabled)
2998 return -ENODEV;
3000 if (trace_array_get(tr) < 0)
3001 return -ENODEV;
3003 filp->private_data = inode->i_private;
3005 return 0;
3008 static int tracing_release(struct inode *inode, struct file *file)
3010 struct trace_array *tr = inode->i_private;
3011 struct seq_file *m = file->private_data;
3012 struct trace_iterator *iter;
3013 int cpu;
3015 if (!(file->f_mode & FMODE_READ)) {
3016 trace_array_put(tr);
3017 return 0;
3020 /* Writes do not use seq_file */
3021 iter = m->private;
3022 mutex_lock(&trace_types_lock);
3024 for_each_tracing_cpu(cpu) {
3025 if (iter->buffer_iter[cpu])
3026 ring_buffer_read_finish(iter->buffer_iter[cpu]);
3029 if (iter->trace && iter->trace->close)
3030 iter->trace->close(iter);
3032 if (!iter->snapshot)
3033 /* reenable tracing if it was previously enabled */
3034 tracing_start_tr(tr);
3036 __trace_array_put(tr);
3038 mutex_unlock(&trace_types_lock);
3040 mutex_destroy(&iter->mutex);
3041 free_cpumask_var(iter->started);
3042 kfree(iter->trace);
3043 kfree(iter->buffer_iter);
3044 seq_release_private(inode, file);
3046 return 0;
3049 static int tracing_release_generic_tr(struct inode *inode, struct file *file)
3051 struct trace_array *tr = inode->i_private;
3053 trace_array_put(tr);
3054 return 0;
3057 static int tracing_single_release_tr(struct inode *inode, struct file *file)
3059 struct trace_array *tr = inode->i_private;
3061 trace_array_put(tr);
3063 return single_release(inode, file);
3066 static int tracing_open(struct inode *inode, struct file *file)
3068 struct trace_array *tr = inode->i_private;
3069 struct trace_iterator *iter;
3070 int ret = 0;
3072 if (trace_array_get(tr) < 0)
3073 return -ENODEV;
3075 /* If this file was open for write, then erase contents */
3076 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
3077 int cpu = tracing_get_cpu(inode);
3079 if (cpu == RING_BUFFER_ALL_CPUS)
3080 tracing_reset_online_cpus(&tr->trace_buffer);
3081 else
3082 tracing_reset(&tr->trace_buffer, cpu);
3085 if (file->f_mode & FMODE_READ) {
3086 iter = __tracing_open(inode, file, false);
3087 if (IS_ERR(iter))
3088 ret = PTR_ERR(iter);
3089 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
3090 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3093 if (ret < 0)
3094 trace_array_put(tr);
3096 return ret;
3099 static void *
3100 t_next(struct seq_file *m, void *v, loff_t *pos)
3102 struct tracer *t = v;
3104 (*pos)++;
3106 if (t)
3107 t = t->next;
3109 return t;
3112 static void *t_start(struct seq_file *m, loff_t *pos)
3114 struct tracer *t;
3115 loff_t l = 0;
3117 mutex_lock(&trace_types_lock);
3118 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
3121 return t;
3124 static void t_stop(struct seq_file *m, void *p)
3126 mutex_unlock(&trace_types_lock);
3129 static int t_show(struct seq_file *m, void *v)
3131 struct tracer *t = v;
3133 if (!t)
3134 return 0;
3136 seq_printf(m, "%s", t->name);
3137 if (t->next)
3138 seq_putc(m, ' ');
3139 else
3140 seq_putc(m, '\n');
3142 return 0;
3145 static const struct seq_operations show_traces_seq_ops = {
3146 .start = t_start,
3147 .next = t_next,
3148 .stop = t_stop,
3149 .show = t_show,
3152 static int show_traces_open(struct inode *inode, struct file *file)
3154 if (tracing_disabled)
3155 return -ENODEV;
3157 return seq_open(file, &show_traces_seq_ops);
3160 static ssize_t
3161 tracing_write_stub(struct file *filp, const char __user *ubuf,
3162 size_t count, loff_t *ppos)
3164 return count;
3167 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
3169 if (file->f_mode & FMODE_READ)
3170 return seq_lseek(file, offset, origin);
3171 else
3172 return 0;
3175 static const struct file_operations tracing_fops = {
3176 .open = tracing_open,
3177 .read = seq_read,
3178 .write = tracing_write_stub,
3179 .llseek = tracing_seek,
3180 .release = tracing_release,
3183 static const struct file_operations show_traces_fops = {
3184 .open = show_traces_open,
3185 .read = seq_read,
3186 .release = seq_release,
3187 .llseek = seq_lseek,
3191 * The tracer itself will not take this lock, but still we want
3192 * to provide a consistent cpumask to user-space:
3194 static DEFINE_MUTEX(tracing_cpumask_update_lock);
3197 * Temporary storage for the character representation of the
3198 * CPU bitmask (and one more byte for the newline):
3200 static char mask_str[NR_CPUS + 1];
3202 static ssize_t
3203 tracing_cpumask_read(struct file *filp, char __user *ubuf,
3204 size_t count, loff_t *ppos)
3206 struct trace_array *tr = file_inode(filp)->i_private;
3207 int len;
3209 mutex_lock(&tracing_cpumask_update_lock);
3211 len = cpumask_scnprintf(mask_str, count, tr->tracing_cpumask);
3212 if (count - len < 2) {
3213 count = -EINVAL;
3214 goto out_err;
3216 len += sprintf(mask_str + len, "\n");
3217 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3219 out_err:
3220 mutex_unlock(&tracing_cpumask_update_lock);
3222 return count;
3225 static ssize_t
3226 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3227 size_t count, loff_t *ppos)
3229 struct trace_array *tr = file_inode(filp)->i_private;
3230 cpumask_var_t tracing_cpumask_new;
3231 int err, cpu;
3233 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3234 return -ENOMEM;
3236 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
3237 if (err)
3238 goto err_unlock;
3240 mutex_lock(&tracing_cpumask_update_lock);
3242 local_irq_disable();
3243 arch_spin_lock(&ftrace_max_lock);
3244 for_each_tracing_cpu(cpu) {
3246 * Increase/decrease the disabled counter if we are
3247 * about to flip a bit in the cpumask:
3249 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
3250 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3251 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3252 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
3254 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
3255 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3256 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3257 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
3260 arch_spin_unlock(&ftrace_max_lock);
3261 local_irq_enable();
3263 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
3265 mutex_unlock(&tracing_cpumask_update_lock);
3266 free_cpumask_var(tracing_cpumask_new);
3268 return count;
3270 err_unlock:
3271 free_cpumask_var(tracing_cpumask_new);
3273 return err;
3276 static const struct file_operations tracing_cpumask_fops = {
3277 .open = tracing_open_generic_tr,
3278 .read = tracing_cpumask_read,
3279 .write = tracing_cpumask_write,
3280 .release = tracing_release_generic_tr,
3281 .llseek = generic_file_llseek,
3284 static int tracing_trace_options_show(struct seq_file *m, void *v)
3286 struct tracer_opt *trace_opts;
3287 struct trace_array *tr = m->private;
3288 u32 tracer_flags;
3289 int i;
3291 mutex_lock(&trace_types_lock);
3292 tracer_flags = tr->current_trace->flags->val;
3293 trace_opts = tr->current_trace->flags->opts;
3295 for (i = 0; trace_options[i]; i++) {
3296 if (trace_flags & (1 << i))
3297 seq_printf(m, "%s\n", trace_options[i]);
3298 else
3299 seq_printf(m, "no%s\n", trace_options[i]);
3302 for (i = 0; trace_opts[i].name; i++) {
3303 if (tracer_flags & trace_opts[i].bit)
3304 seq_printf(m, "%s\n", trace_opts[i].name);
3305 else
3306 seq_printf(m, "no%s\n", trace_opts[i].name);
3308 mutex_unlock(&trace_types_lock);
3310 return 0;
3313 static int __set_tracer_option(struct tracer *trace,
3314 struct tracer_flags *tracer_flags,
3315 struct tracer_opt *opts, int neg)
3317 int ret;
3319 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
3320 if (ret)
3321 return ret;
3323 if (neg)
3324 tracer_flags->val &= ~opts->bit;
3325 else
3326 tracer_flags->val |= opts->bit;
3327 return 0;
3330 /* Try to assign a tracer specific option */
3331 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3333 struct tracer_flags *tracer_flags = trace->flags;
3334 struct tracer_opt *opts = NULL;
3335 int i;
3337 for (i = 0; tracer_flags->opts[i].name; i++) {
3338 opts = &tracer_flags->opts[i];
3340 if (strcmp(cmp, opts->name) == 0)
3341 return __set_tracer_option(trace, trace->flags,
3342 opts, neg);
3345 return -EINVAL;
3348 /* Some tracers require overwrite to stay enabled */
3349 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3351 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3352 return -1;
3354 return 0;
3357 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
3359 /* do nothing if flag is already set */
3360 if (!!(trace_flags & mask) == !!enabled)
3361 return 0;
3363 /* Give the tracer a chance to approve the change */
3364 if (tr->current_trace->flag_changed)
3365 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
3366 return -EINVAL;
3368 if (enabled)
3369 trace_flags |= mask;
3370 else
3371 trace_flags &= ~mask;
3373 if (mask == TRACE_ITER_RECORD_CMD)
3374 trace_event_enable_cmd_record(enabled);
3376 if (mask == TRACE_ITER_OVERWRITE) {
3377 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
3378 #ifdef CONFIG_TRACER_MAX_TRACE
3379 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
3380 #endif
3383 if (mask == TRACE_ITER_PRINTK)
3384 trace_printk_start_stop_comm(enabled);
3386 return 0;
3389 static int trace_set_options(struct trace_array *tr, char *option)
3391 char *cmp;
3392 int neg = 0;
3393 int ret = -ENODEV;
3394 int i;
3396 cmp = strstrip(option);
3398 if (strncmp(cmp, "no", 2) == 0) {
3399 neg = 1;
3400 cmp += 2;
3403 mutex_lock(&trace_types_lock);
3405 for (i = 0; trace_options[i]; i++) {
3406 if (strcmp(cmp, trace_options[i]) == 0) {
3407 ret = set_tracer_flag(tr, 1 << i, !neg);
3408 break;
3412 /* If no option could be set, test the specific tracer options */
3413 if (!trace_options[i])
3414 ret = set_tracer_option(tr->current_trace, cmp, neg);
3416 mutex_unlock(&trace_types_lock);
3418 return ret;
3421 static ssize_t
3422 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3423 size_t cnt, loff_t *ppos)
3425 struct seq_file *m = filp->private_data;
3426 struct trace_array *tr = m->private;
3427 char buf[64];
3428 int ret;
3430 if (cnt >= sizeof(buf))
3431 return -EINVAL;
3433 if (copy_from_user(&buf, ubuf, cnt))
3434 return -EFAULT;
3436 buf[cnt] = 0;
3438 ret = trace_set_options(tr, buf);
3439 if (ret < 0)
3440 return ret;
3442 *ppos += cnt;
3444 return cnt;
3447 static int tracing_trace_options_open(struct inode *inode, struct file *file)
3449 struct trace_array *tr = inode->i_private;
3450 int ret;
3452 if (tracing_disabled)
3453 return -ENODEV;
3455 if (trace_array_get(tr) < 0)
3456 return -ENODEV;
3458 ret = single_open(file, tracing_trace_options_show, inode->i_private);
3459 if (ret < 0)
3460 trace_array_put(tr);
3462 return ret;
3465 static const struct file_operations tracing_iter_fops = {
3466 .open = tracing_trace_options_open,
3467 .read = seq_read,
3468 .llseek = seq_lseek,
3469 .release = tracing_single_release_tr,
3470 .write = tracing_trace_options_write,
3473 static const char readme_msg[] =
3474 "tracing mini-HOWTO:\n\n"
3475 "# echo 0 > tracing_on : quick way to disable tracing\n"
3476 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3477 " Important files:\n"
3478 " trace\t\t\t- The static contents of the buffer\n"
3479 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3480 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3481 " current_tracer\t- function and latency tracers\n"
3482 " available_tracers\t- list of configured tracers for current_tracer\n"
3483 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3484 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3485 " trace_clock\t\t-change the clock used to order events\n"
3486 " local: Per cpu clock but may not be synced across CPUs\n"
3487 " global: Synced across CPUs but slows tracing down.\n"
3488 " counter: Not a clock, but just an increment\n"
3489 " uptime: Jiffy counter from time of boot\n"
3490 " perf: Same clock that perf events use\n"
3491 #ifdef CONFIG_X86_64
3492 " x86-tsc: TSC cycle counter\n"
3493 #endif
3494 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3495 " tracing_cpumask\t- Limit which CPUs to trace\n"
3496 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3497 "\t\t\t Remove sub-buffer with rmdir\n"
3498 " trace_options\t\t- Set format or modify how tracing happens\n"
3499 "\t\t\t Disable an option by adding a suffix 'no' to the option name\n"
3500 #ifdef CONFIG_DYNAMIC_FTRACE
3501 "\n available_filter_functions - list of functions that can be filtered on\n"
3502 " set_ftrace_filter\t- echo function name in here to only trace these functions\n"
3503 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3504 " modules: Can select a group via module\n"
3505 " Format: :mod:<module-name>\n"
3506 " example: echo :mod:ext3 > set_ftrace_filter\n"
3507 " triggers: a command to perform when function is hit\n"
3508 " Format: <function>:<trigger>[:count]\n"
3509 " trigger: traceon, traceoff\n"
3510 " enable_event:<system>:<event>\n"
3511 " disable_event:<system>:<event>\n"
3512 #ifdef CONFIG_STACKTRACE
3513 " stacktrace\n"
3514 #endif
3515 #ifdef CONFIG_TRACER_SNAPSHOT
3516 " snapshot\n"
3517 #endif
3518 " example: echo do_fault:traceoff > set_ftrace_filter\n"
3519 " echo do_trap:traceoff:3 > set_ftrace_filter\n"
3520 " The first one will disable tracing every time do_fault is hit\n"
3521 " The second will disable tracing at most 3 times when do_trap is hit\n"
3522 " The first time do trap is hit and it disables tracing, the counter\n"
3523 " will decrement to 2. If tracing is already disabled, the counter\n"
3524 " will not decrement. It only decrements when the trigger did work\n"
3525 " To remove trigger without count:\n"
3526 " echo '!<function>:<trigger> > set_ftrace_filter\n"
3527 " To remove trigger with a count:\n"
3528 " echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
3529 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
3530 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3531 " modules: Can select a group via module command :mod:\n"
3532 " Does not accept triggers\n"
3533 #endif /* CONFIG_DYNAMIC_FTRACE */
3534 #ifdef CONFIG_FUNCTION_TRACER
3535 " set_ftrace_pid\t- Write pid(s) to only function trace those pids (function)\n"
3536 #endif
3537 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3538 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3539 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3540 #endif
3541 #ifdef CONFIG_TRACER_SNAPSHOT
3542 "\n snapshot\t\t- Like 'trace' but shows the content of the static snapshot buffer\n"
3543 "\t\t\t Read the contents for more information\n"
3544 #endif
3545 #ifdef CONFIG_STACK_TRACER
3546 " stack_trace\t\t- Shows the max stack trace when active\n"
3547 " stack_max_size\t- Shows current max stack size that was traced\n"
3548 "\t\t\t Write into this file to reset the max size (trigger a new trace)\n"
3549 #ifdef CONFIG_DYNAMIC_FTRACE
3550 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace traces\n"
3551 #endif
3552 #endif /* CONFIG_STACK_TRACER */
3555 static ssize_t
3556 tracing_readme_read(struct file *filp, char __user *ubuf,
3557 size_t cnt, loff_t *ppos)
3559 return simple_read_from_buffer(ubuf, cnt, ppos,
3560 readme_msg, strlen(readme_msg));
3563 static const struct file_operations tracing_readme_fops = {
3564 .open = tracing_open_generic,
3565 .read = tracing_readme_read,
3566 .llseek = generic_file_llseek,
3569 static ssize_t
3570 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3571 size_t cnt, loff_t *ppos)
3573 char *buf_comm;
3574 char *file_buf;
3575 char *buf;
3576 int len = 0;
3577 int pid;
3578 int i;
3580 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3581 if (!file_buf)
3582 return -ENOMEM;
3584 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3585 if (!buf_comm) {
3586 kfree(file_buf);
3587 return -ENOMEM;
3590 buf = file_buf;
3592 for (i = 0; i < SAVED_CMDLINES; i++) {
3593 int r;
3595 pid = map_cmdline_to_pid[i];
3596 if (pid == -1 || pid == NO_CMDLINE_MAP)
3597 continue;
3599 trace_find_cmdline(pid, buf_comm);
3600 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3601 buf += r;
3602 len += r;
3605 len = simple_read_from_buffer(ubuf, cnt, ppos,
3606 file_buf, len);
3608 kfree(file_buf);
3609 kfree(buf_comm);
3611 return len;
3614 static const struct file_operations tracing_saved_cmdlines_fops = {
3615 .open = tracing_open_generic,
3616 .read = tracing_saved_cmdlines_read,
3617 .llseek = generic_file_llseek,
3620 static ssize_t
3621 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3622 size_t cnt, loff_t *ppos)
3624 struct trace_array *tr = filp->private_data;
3625 char buf[MAX_TRACER_SIZE+2];
3626 int r;
3628 mutex_lock(&trace_types_lock);
3629 r = sprintf(buf, "%s\n", tr->current_trace->name);
3630 mutex_unlock(&trace_types_lock);
3632 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3635 int tracer_init(struct tracer *t, struct trace_array *tr)
3637 tracing_reset_online_cpus(&tr->trace_buffer);
3638 return t->init(tr);
3641 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
3643 int cpu;
3645 for_each_tracing_cpu(cpu)
3646 per_cpu_ptr(buf->data, cpu)->entries = val;
3649 #ifdef CONFIG_TRACER_MAX_TRACE
3650 /* resize @tr's buffer to the size of @size_tr's entries */
3651 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3652 struct trace_buffer *size_buf, int cpu_id)
3654 int cpu, ret = 0;
3656 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3657 for_each_tracing_cpu(cpu) {
3658 ret = ring_buffer_resize(trace_buf->buffer,
3659 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
3660 if (ret < 0)
3661 break;
3662 per_cpu_ptr(trace_buf->data, cpu)->entries =
3663 per_cpu_ptr(size_buf->data, cpu)->entries;
3665 } else {
3666 ret = ring_buffer_resize(trace_buf->buffer,
3667 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
3668 if (ret == 0)
3669 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3670 per_cpu_ptr(size_buf->data, cpu_id)->entries;
3673 return ret;
3675 #endif /* CONFIG_TRACER_MAX_TRACE */
3677 static int __tracing_resize_ring_buffer(struct trace_array *tr,
3678 unsigned long size, int cpu)
3680 int ret;
3683 * If kernel or user changes the size of the ring buffer
3684 * we use the size that was given, and we can forget about
3685 * expanding it later.
3687 ring_buffer_expanded = true;
3689 /* May be called before buffers are initialized */
3690 if (!tr->trace_buffer.buffer)
3691 return 0;
3693 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
3694 if (ret < 0)
3695 return ret;
3697 #ifdef CONFIG_TRACER_MAX_TRACE
3698 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3699 !tr->current_trace->use_max_tr)
3700 goto out;
3702 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
3703 if (ret < 0) {
3704 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3705 &tr->trace_buffer, cpu);
3706 if (r < 0) {
3708 * AARGH! We are left with different
3709 * size max buffer!!!!
3710 * The max buffer is our "snapshot" buffer.
3711 * When a tracer needs a snapshot (one of the
3712 * latency tracers), it swaps the max buffer
3713 * with the saved snap shot. We succeeded to
3714 * update the size of the main buffer, but failed to
3715 * update the size of the max buffer. But when we tried
3716 * to reset the main buffer to the original size, we
3717 * failed there too. This is very unlikely to
3718 * happen, but if it does, warn and kill all
3719 * tracing.
3721 WARN_ON(1);
3722 tracing_disabled = 1;
3724 return ret;
3727 if (cpu == RING_BUFFER_ALL_CPUS)
3728 set_buffer_entries(&tr->max_buffer, size);
3729 else
3730 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
3732 out:
3733 #endif /* CONFIG_TRACER_MAX_TRACE */
3735 if (cpu == RING_BUFFER_ALL_CPUS)
3736 set_buffer_entries(&tr->trace_buffer, size);
3737 else
3738 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
3740 return ret;
3743 static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3744 unsigned long size, int cpu_id)
3746 int ret = size;
3748 mutex_lock(&trace_types_lock);
3750 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3751 /* make sure, this cpu is enabled in the mask */
3752 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3753 ret = -EINVAL;
3754 goto out;
3758 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
3759 if (ret < 0)
3760 ret = -ENOMEM;
3762 out:
3763 mutex_unlock(&trace_types_lock);
3765 return ret;
3770 * tracing_update_buffers - used by tracing facility to expand ring buffers
3772 * To save on memory when the tracing is never used on a system with it
3773 * configured in. The ring buffers are set to a minimum size. But once
3774 * a user starts to use the tracing facility, then they need to grow
3775 * to their default size.
3777 * This function is to be called when a tracer is about to be used.
3779 int tracing_update_buffers(void)
3781 int ret = 0;
3783 mutex_lock(&trace_types_lock);
3784 if (!ring_buffer_expanded)
3785 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
3786 RING_BUFFER_ALL_CPUS);
3787 mutex_unlock(&trace_types_lock);
3789 return ret;
3792 struct trace_option_dentry;
3794 static struct trace_option_dentry *
3795 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
3797 static void
3798 destroy_trace_option_files(struct trace_option_dentry *topts);
3800 static int tracing_set_tracer(const char *buf)
3802 static struct trace_option_dentry *topts;
3803 struct trace_array *tr = &global_trace;
3804 struct tracer *t;
3805 #ifdef CONFIG_TRACER_MAX_TRACE
3806 bool had_max_tr;
3807 #endif
3808 int ret = 0;
3810 mutex_lock(&trace_types_lock);
3812 if (!ring_buffer_expanded) {
3813 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
3814 RING_BUFFER_ALL_CPUS);
3815 if (ret < 0)
3816 goto out;
3817 ret = 0;
3820 for (t = trace_types; t; t = t->next) {
3821 if (strcmp(t->name, buf) == 0)
3822 break;
3824 if (!t) {
3825 ret = -EINVAL;
3826 goto out;
3828 if (t == tr->current_trace)
3829 goto out;
3831 trace_branch_disable();
3833 tr->current_trace->enabled = false;
3835 if (tr->current_trace->reset)
3836 tr->current_trace->reset(tr);
3838 /* Current trace needs to be nop_trace before synchronize_sched */
3839 tr->current_trace = &nop_trace;
3841 #ifdef CONFIG_TRACER_MAX_TRACE
3842 had_max_tr = tr->allocated_snapshot;
3844 if (had_max_tr && !t->use_max_tr) {
3846 * We need to make sure that the update_max_tr sees that
3847 * current_trace changed to nop_trace to keep it from
3848 * swapping the buffers after we resize it.
3849 * The update_max_tr is called from interrupts disabled
3850 * so a synchronized_sched() is sufficient.
3852 synchronize_sched();
3853 free_snapshot(tr);
3855 #endif
3856 destroy_trace_option_files(topts);
3858 topts = create_trace_option_files(tr, t);
3860 #ifdef CONFIG_TRACER_MAX_TRACE
3861 if (t->use_max_tr && !had_max_tr) {
3862 ret = alloc_snapshot(tr);
3863 if (ret < 0)
3864 goto out;
3866 #endif
3868 if (t->init) {
3869 ret = tracer_init(t, tr);
3870 if (ret)
3871 goto out;
3874 tr->current_trace = t;
3875 tr->current_trace->enabled = true;
3876 trace_branch_enable(tr);
3877 out:
3878 mutex_unlock(&trace_types_lock);
3880 return ret;
3883 static ssize_t
3884 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3885 size_t cnt, loff_t *ppos)
3887 char buf[MAX_TRACER_SIZE+1];
3888 int i;
3889 size_t ret;
3890 int err;
3892 ret = cnt;
3894 if (cnt > MAX_TRACER_SIZE)
3895 cnt = MAX_TRACER_SIZE;
3897 if (copy_from_user(&buf, ubuf, cnt))
3898 return -EFAULT;
3900 buf[cnt] = 0;
3902 /* strip ending whitespace. */
3903 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3904 buf[i] = 0;
3906 err = tracing_set_tracer(buf);
3907 if (err)
3908 return err;
3910 *ppos += ret;
3912 return ret;
3915 static ssize_t
3916 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3917 size_t cnt, loff_t *ppos)
3919 unsigned long *ptr = filp->private_data;
3920 char buf[64];
3921 int r;
3923 r = snprintf(buf, sizeof(buf), "%ld\n",
3924 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3925 if (r > sizeof(buf))
3926 r = sizeof(buf);
3927 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3930 static ssize_t
3931 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3932 size_t cnt, loff_t *ppos)
3934 unsigned long *ptr = filp->private_data;
3935 unsigned long val;
3936 int ret;
3938 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3939 if (ret)
3940 return ret;
3942 *ptr = val * 1000;
3944 return cnt;
3947 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3949 struct trace_array *tr = inode->i_private;
3950 struct trace_iterator *iter;
3951 int ret = 0;
3953 if (tracing_disabled)
3954 return -ENODEV;
3956 if (trace_array_get(tr) < 0)
3957 return -ENODEV;
3959 mutex_lock(&trace_types_lock);
3961 /* create a buffer to store the information to pass to userspace */
3962 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3963 if (!iter) {
3964 ret = -ENOMEM;
3965 __trace_array_put(tr);
3966 goto out;
3970 * We make a copy of the current tracer to avoid concurrent
3971 * changes on it while we are reading.
3973 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3974 if (!iter->trace) {
3975 ret = -ENOMEM;
3976 goto fail;
3978 *iter->trace = *tr->current_trace;
3980 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3981 ret = -ENOMEM;
3982 goto fail;
3985 /* trace pipe does not show start of buffer */
3986 cpumask_setall(iter->started);
3988 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3989 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3991 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3992 if (trace_clocks[tr->clock_id].in_ns)
3993 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3995 iter->tr = tr;
3996 iter->trace_buffer = &tr->trace_buffer;
3997 iter->cpu_file = tracing_get_cpu(inode);
3998 mutex_init(&iter->mutex);
3999 filp->private_data = iter;
4001 if (iter->trace->pipe_open)
4002 iter->trace->pipe_open(iter);
4004 nonseekable_open(inode, filp);
4005 out:
4006 mutex_unlock(&trace_types_lock);
4007 return ret;
4009 fail:
4010 kfree(iter->trace);
4011 kfree(iter);
4012 __trace_array_put(tr);
4013 mutex_unlock(&trace_types_lock);
4014 return ret;
4017 static int tracing_release_pipe(struct inode *inode, struct file *file)
4019 struct trace_iterator *iter = file->private_data;
4020 struct trace_array *tr = inode->i_private;
4022 mutex_lock(&trace_types_lock);
4024 if (iter->trace->pipe_close)
4025 iter->trace->pipe_close(iter);
4027 mutex_unlock(&trace_types_lock);
4029 free_cpumask_var(iter->started);
4030 mutex_destroy(&iter->mutex);
4031 kfree(iter->trace);
4032 kfree(iter);
4034 trace_array_put(tr);
4036 return 0;
4039 static unsigned int
4040 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
4042 /* Iterators are static, they should be filled or empty */
4043 if (trace_buffer_iter(iter, iter->cpu_file))
4044 return POLLIN | POLLRDNORM;
4046 if (trace_flags & TRACE_ITER_BLOCK)
4048 * Always select as readable when in blocking mode
4050 return POLLIN | POLLRDNORM;
4051 else
4052 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
4053 filp, poll_table);
4056 static unsigned int
4057 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
4059 struct trace_iterator *iter = filp->private_data;
4061 return trace_poll(iter, filp, poll_table);
4065 * This is a make-shift waitqueue.
4066 * A tracer might use this callback on some rare cases:
4068 * 1) the current tracer might hold the runqueue lock when it wakes up
4069 * a reader, hence a deadlock (sched, function, and function graph tracers)
4070 * 2) the function tracers, trace all functions, we don't want
4071 * the overhead of calling wake_up and friends
4072 * (and tracing them too)
4074 * Anyway, this is really very primitive wakeup.
4076 int poll_wait_pipe(struct trace_iterator *iter)
4078 set_current_state(TASK_INTERRUPTIBLE);
4079 /* sleep for 100 msecs, and try again. */
4080 schedule_timeout(HZ / 10);
4081 return 0;
4084 /* Must be called with trace_types_lock mutex held. */
4085 static int tracing_wait_pipe(struct file *filp)
4087 struct trace_iterator *iter = filp->private_data;
4088 int ret;
4090 while (trace_empty(iter)) {
4092 if ((filp->f_flags & O_NONBLOCK)) {
4093 return -EAGAIN;
4096 mutex_unlock(&iter->mutex);
4098 ret = iter->trace->wait_pipe(iter);
4100 mutex_lock(&iter->mutex);
4102 if (ret)
4103 return ret;
4105 if (signal_pending(current))
4106 return -EINTR;
4109 * We block until we read something and tracing is disabled.
4110 * We still block if tracing is disabled, but we have never
4111 * read anything. This allows a user to cat this file, and
4112 * then enable tracing. But after we have read something,
4113 * we give an EOF when tracing is again disabled.
4115 * iter->pos will be 0 if we haven't read anything.
4117 if (!tracing_is_on() && iter->pos)
4118 break;
4121 return 1;
4125 * Consumer reader.
4127 static ssize_t
4128 tracing_read_pipe(struct file *filp, char __user *ubuf,
4129 size_t cnt, loff_t *ppos)
4131 struct trace_iterator *iter = filp->private_data;
4132 struct trace_array *tr = iter->tr;
4133 ssize_t sret;
4135 /* return any leftover data */
4136 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4137 if (sret != -EBUSY)
4138 return sret;
4140 trace_seq_init(&iter->seq);
4142 /* copy the tracer to avoid using a global lock all around */
4143 mutex_lock(&trace_types_lock);
4144 if (unlikely(iter->trace->name != tr->current_trace->name))
4145 *iter->trace = *tr->current_trace;
4146 mutex_unlock(&trace_types_lock);
4149 * Avoid more than one consumer on a single file descriptor
4150 * This is just a matter of traces coherency, the ring buffer itself
4151 * is protected.
4153 mutex_lock(&iter->mutex);
4154 if (iter->trace->read) {
4155 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
4156 if (sret)
4157 goto out;
4160 waitagain:
4161 sret = tracing_wait_pipe(filp);
4162 if (sret <= 0)
4163 goto out;
4165 /* stop when tracing is finished */
4166 if (trace_empty(iter)) {
4167 sret = 0;
4168 goto out;
4171 if (cnt >= PAGE_SIZE)
4172 cnt = PAGE_SIZE - 1;
4174 /* reset all but tr, trace, and overruns */
4175 memset(&iter->seq, 0,
4176 sizeof(struct trace_iterator) -
4177 offsetof(struct trace_iterator, seq));
4178 cpumask_clear(iter->started);
4179 iter->pos = -1;
4181 trace_event_read_lock();
4182 trace_access_lock(iter->cpu_file);
4183 while (trace_find_next_entry_inc(iter) != NULL) {
4184 enum print_line_t ret;
4185 int len = iter->seq.len;
4187 ret = print_trace_line(iter);
4188 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4189 /* don't print partial lines */
4190 iter->seq.len = len;
4191 break;
4193 if (ret != TRACE_TYPE_NO_CONSUME)
4194 trace_consume(iter);
4196 if (iter->seq.len >= cnt)
4197 break;
4200 * Setting the full flag means we reached the trace_seq buffer
4201 * size and we should leave by partial output condition above.
4202 * One of the trace_seq_* functions is not used properly.
4204 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
4205 iter->ent->type);
4207 trace_access_unlock(iter->cpu_file);
4208 trace_event_read_unlock();
4210 /* Now copy what we have to the user */
4211 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4212 if (iter->seq.readpos >= iter->seq.len)
4213 trace_seq_init(&iter->seq);
4216 * If there was nothing to send to user, in spite of consuming trace
4217 * entries, go back to wait for more entries.
4219 if (sret == -EBUSY)
4220 goto waitagain;
4222 out:
4223 mutex_unlock(&iter->mutex);
4225 return sret;
4228 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
4229 struct pipe_buffer *buf)
4231 __free_page(buf->page);
4234 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
4235 unsigned int idx)
4237 __free_page(spd->pages[idx]);
4240 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
4241 .can_merge = 0,
4242 .map = generic_pipe_buf_map,
4243 .unmap = generic_pipe_buf_unmap,
4244 .confirm = generic_pipe_buf_confirm,
4245 .release = tracing_pipe_buf_release,
4246 .steal = generic_pipe_buf_steal,
4247 .get = generic_pipe_buf_get,
4250 static size_t
4251 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
4253 size_t count;
4254 int ret;
4256 /* Seq buffer is page-sized, exactly what we need. */
4257 for (;;) {
4258 count = iter->seq.len;
4259 ret = print_trace_line(iter);
4260 count = iter->seq.len - count;
4261 if (rem < count) {
4262 rem = 0;
4263 iter->seq.len -= count;
4264 break;
4266 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4267 iter->seq.len -= count;
4268 break;
4271 if (ret != TRACE_TYPE_NO_CONSUME)
4272 trace_consume(iter);
4273 rem -= count;
4274 if (!trace_find_next_entry_inc(iter)) {
4275 rem = 0;
4276 iter->ent = NULL;
4277 break;
4281 return rem;
4284 static ssize_t tracing_splice_read_pipe(struct file *filp,
4285 loff_t *ppos,
4286 struct pipe_inode_info *pipe,
4287 size_t len,
4288 unsigned int flags)
4290 struct page *pages_def[PIPE_DEF_BUFFERS];
4291 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4292 struct trace_iterator *iter = filp->private_data;
4293 struct splice_pipe_desc spd = {
4294 .pages = pages_def,
4295 .partial = partial_def,
4296 .nr_pages = 0, /* This gets updated below. */
4297 .nr_pages_max = PIPE_DEF_BUFFERS,
4298 .flags = flags,
4299 .ops = &tracing_pipe_buf_ops,
4300 .spd_release = tracing_spd_release_pipe,
4302 struct trace_array *tr = iter->tr;
4303 ssize_t ret;
4304 size_t rem;
4305 unsigned int i;
4307 if (splice_grow_spd(pipe, &spd))
4308 return -ENOMEM;
4310 /* copy the tracer to avoid using a global lock all around */
4311 mutex_lock(&trace_types_lock);
4312 if (unlikely(iter->trace->name != tr->current_trace->name))
4313 *iter->trace = *tr->current_trace;
4314 mutex_unlock(&trace_types_lock);
4316 mutex_lock(&iter->mutex);
4318 if (iter->trace->splice_read) {
4319 ret = iter->trace->splice_read(iter, filp,
4320 ppos, pipe, len, flags);
4321 if (ret)
4322 goto out_err;
4325 ret = tracing_wait_pipe(filp);
4326 if (ret <= 0)
4327 goto out_err;
4329 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
4330 ret = -EFAULT;
4331 goto out_err;
4334 trace_event_read_lock();
4335 trace_access_lock(iter->cpu_file);
4337 /* Fill as many pages as possible. */
4338 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
4339 spd.pages[i] = alloc_page(GFP_KERNEL);
4340 if (!spd.pages[i])
4341 break;
4343 rem = tracing_fill_pipe_page(rem, iter);
4345 /* Copy the data into the page, so we can start over. */
4346 ret = trace_seq_to_buffer(&iter->seq,
4347 page_address(spd.pages[i]),
4348 iter->seq.len);
4349 if (ret < 0) {
4350 __free_page(spd.pages[i]);
4351 break;
4353 spd.partial[i].offset = 0;
4354 spd.partial[i].len = iter->seq.len;
4356 trace_seq_init(&iter->seq);
4359 trace_access_unlock(iter->cpu_file);
4360 trace_event_read_unlock();
4361 mutex_unlock(&iter->mutex);
4363 spd.nr_pages = i;
4365 ret = splice_to_pipe(pipe, &spd);
4366 out:
4367 splice_shrink_spd(&spd);
4368 return ret;
4370 out_err:
4371 mutex_unlock(&iter->mutex);
4372 goto out;
4375 static ssize_t
4376 tracing_entries_read(struct file *filp, char __user *ubuf,
4377 size_t cnt, loff_t *ppos)
4379 struct inode *inode = file_inode(filp);
4380 struct trace_array *tr = inode->i_private;
4381 int cpu = tracing_get_cpu(inode);
4382 char buf[64];
4383 int r = 0;
4384 ssize_t ret;
4386 mutex_lock(&trace_types_lock);
4388 if (cpu == RING_BUFFER_ALL_CPUS) {
4389 int cpu, buf_size_same;
4390 unsigned long size;
4392 size = 0;
4393 buf_size_same = 1;
4394 /* check if all cpu sizes are same */
4395 for_each_tracing_cpu(cpu) {
4396 /* fill in the size from first enabled cpu */
4397 if (size == 0)
4398 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4399 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
4400 buf_size_same = 0;
4401 break;
4405 if (buf_size_same) {
4406 if (!ring_buffer_expanded)
4407 r = sprintf(buf, "%lu (expanded: %lu)\n",
4408 size >> 10,
4409 trace_buf_size >> 10);
4410 else
4411 r = sprintf(buf, "%lu\n", size >> 10);
4412 } else
4413 r = sprintf(buf, "X\n");
4414 } else
4415 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
4417 mutex_unlock(&trace_types_lock);
4419 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4420 return ret;
4423 static ssize_t
4424 tracing_entries_write(struct file *filp, const char __user *ubuf,
4425 size_t cnt, loff_t *ppos)
4427 struct inode *inode = file_inode(filp);
4428 struct trace_array *tr = inode->i_private;
4429 unsigned long val;
4430 int ret;
4432 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4433 if (ret)
4434 return ret;
4436 /* must have at least 1 entry */
4437 if (!val)
4438 return -EINVAL;
4440 /* value is in KB */
4441 val <<= 10;
4442 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
4443 if (ret < 0)
4444 return ret;
4446 *ppos += cnt;
4448 return cnt;
4451 static ssize_t
4452 tracing_total_entries_read(struct file *filp, char __user *ubuf,
4453 size_t cnt, loff_t *ppos)
4455 struct trace_array *tr = filp->private_data;
4456 char buf[64];
4457 int r, cpu;
4458 unsigned long size = 0, expanded_size = 0;
4460 mutex_lock(&trace_types_lock);
4461 for_each_tracing_cpu(cpu) {
4462 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
4463 if (!ring_buffer_expanded)
4464 expanded_size += trace_buf_size >> 10;
4466 if (ring_buffer_expanded)
4467 r = sprintf(buf, "%lu\n", size);
4468 else
4469 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4470 mutex_unlock(&trace_types_lock);
4472 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4475 static ssize_t
4476 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4477 size_t cnt, loff_t *ppos)
4480 * There is no need to read what the user has written, this function
4481 * is just to make sure that there is no error when "echo" is used
4484 *ppos += cnt;
4486 return cnt;
4489 static int
4490 tracing_free_buffer_release(struct inode *inode, struct file *filp)
4492 struct trace_array *tr = inode->i_private;
4494 /* disable tracing ? */
4495 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4496 tracer_tracing_off(tr);
4497 /* resize the ring buffer to 0 */
4498 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4500 trace_array_put(tr);
4502 return 0;
4505 static ssize_t
4506 tracing_mark_write(struct file *filp, const char __user *ubuf,
4507 size_t cnt, loff_t *fpos)
4509 unsigned long addr = (unsigned long)ubuf;
4510 struct trace_array *tr = filp->private_data;
4511 struct ring_buffer_event *event;
4512 struct ring_buffer *buffer;
4513 struct print_entry *entry;
4514 unsigned long irq_flags;
4515 struct page *pages[2];
4516 void *map_page[2];
4517 int nr_pages = 1;
4518 ssize_t written;
4519 int offset;
4520 int size;
4521 int len;
4522 int ret;
4523 int i;
4525 if (tracing_disabled)
4526 return -EINVAL;
4528 if (!(trace_flags & TRACE_ITER_MARKERS))
4529 return -EINVAL;
4531 if (cnt > TRACE_BUF_SIZE)
4532 cnt = TRACE_BUF_SIZE;
4535 * Userspace is injecting traces into the kernel trace buffer.
4536 * We want to be as non intrusive as possible.
4537 * To do so, we do not want to allocate any special buffers
4538 * or take any locks, but instead write the userspace data
4539 * straight into the ring buffer.
4541 * First we need to pin the userspace buffer into memory,
4542 * which, most likely it is, because it just referenced it.
4543 * But there's no guarantee that it is. By using get_user_pages_fast()
4544 * and kmap_atomic/kunmap_atomic() we can get access to the
4545 * pages directly. We then write the data directly into the
4546 * ring buffer.
4548 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4550 /* check if we cross pages */
4551 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4552 nr_pages = 2;
4554 offset = addr & (PAGE_SIZE - 1);
4555 addr &= PAGE_MASK;
4557 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4558 if (ret < nr_pages) {
4559 while (--ret >= 0)
4560 put_page(pages[ret]);
4561 written = -EFAULT;
4562 goto out;
4565 for (i = 0; i < nr_pages; i++)
4566 map_page[i] = kmap_atomic(pages[i]);
4568 local_save_flags(irq_flags);
4569 size = sizeof(*entry) + cnt + 2; /* possible \n added */
4570 buffer = tr->trace_buffer.buffer;
4571 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4572 irq_flags, preempt_count());
4573 if (!event) {
4574 /* Ring buffer disabled, return as if not open for write */
4575 written = -EBADF;
4576 goto out_unlock;
4579 entry = ring_buffer_event_data(event);
4580 entry->ip = _THIS_IP_;
4582 if (nr_pages == 2) {
4583 len = PAGE_SIZE - offset;
4584 memcpy(&entry->buf, map_page[0] + offset, len);
4585 memcpy(&entry->buf[len], map_page[1], cnt - len);
4586 } else
4587 memcpy(&entry->buf, map_page[0] + offset, cnt);
4589 if (entry->buf[cnt - 1] != '\n') {
4590 entry->buf[cnt] = '\n';
4591 entry->buf[cnt + 1] = '\0';
4592 } else
4593 entry->buf[cnt] = '\0';
4595 __buffer_unlock_commit(buffer, event);
4597 written = cnt;
4599 *fpos += written;
4601 out_unlock:
4602 for (i = 0; i < nr_pages; i++){
4603 kunmap_atomic(map_page[i]);
4604 put_page(pages[i]);
4606 out:
4607 return written;
4610 static int tracing_clock_show(struct seq_file *m, void *v)
4612 struct trace_array *tr = m->private;
4613 int i;
4615 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4616 seq_printf(m,
4617 "%s%s%s%s", i ? " " : "",
4618 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4619 i == tr->clock_id ? "]" : "");
4620 seq_putc(m, '\n');
4622 return 0;
4625 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4626 size_t cnt, loff_t *fpos)
4628 struct seq_file *m = filp->private_data;
4629 struct trace_array *tr = m->private;
4630 char buf[64];
4631 const char *clockstr;
4632 int i;
4634 if (cnt >= sizeof(buf))
4635 return -EINVAL;
4637 if (copy_from_user(&buf, ubuf, cnt))
4638 return -EFAULT;
4640 buf[cnt] = 0;
4642 clockstr = strstrip(buf);
4644 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4645 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4646 break;
4648 if (i == ARRAY_SIZE(trace_clocks))
4649 return -EINVAL;
4651 mutex_lock(&trace_types_lock);
4653 tr->clock_id = i;
4655 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
4658 * New clock may not be consistent with the previous clock.
4659 * Reset the buffer so that it doesn't have incomparable timestamps.
4661 tracing_reset_online_cpus(&tr->trace_buffer);
4663 #ifdef CONFIG_TRACER_MAX_TRACE
4664 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4665 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4666 tracing_reset_online_cpus(&tr->max_buffer);
4667 #endif
4669 mutex_unlock(&trace_types_lock);
4671 *fpos += cnt;
4673 return cnt;
4676 static int tracing_clock_open(struct inode *inode, struct file *file)
4678 struct trace_array *tr = inode->i_private;
4679 int ret;
4681 if (tracing_disabled)
4682 return -ENODEV;
4684 if (trace_array_get(tr))
4685 return -ENODEV;
4687 ret = single_open(file, tracing_clock_show, inode->i_private);
4688 if (ret < 0)
4689 trace_array_put(tr);
4691 return ret;
4694 struct ftrace_buffer_info {
4695 struct trace_iterator iter;
4696 void *spare;
4697 unsigned int read;
4700 #ifdef CONFIG_TRACER_SNAPSHOT
4701 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4703 struct trace_array *tr = inode->i_private;
4704 struct trace_iterator *iter;
4705 struct seq_file *m;
4706 int ret = 0;
4708 if (trace_array_get(tr) < 0)
4709 return -ENODEV;
4711 if (file->f_mode & FMODE_READ) {
4712 iter = __tracing_open(inode, file, true);
4713 if (IS_ERR(iter))
4714 ret = PTR_ERR(iter);
4715 } else {
4716 /* Writes still need the seq_file to hold the private data */
4717 ret = -ENOMEM;
4718 m = kzalloc(sizeof(*m), GFP_KERNEL);
4719 if (!m)
4720 goto out;
4721 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4722 if (!iter) {
4723 kfree(m);
4724 goto out;
4726 ret = 0;
4728 iter->tr = tr;
4729 iter->trace_buffer = &tr->max_buffer;
4730 iter->cpu_file = tracing_get_cpu(inode);
4731 m->private = iter;
4732 file->private_data = m;
4734 out:
4735 if (ret < 0)
4736 trace_array_put(tr);
4738 return ret;
4741 static ssize_t
4742 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4743 loff_t *ppos)
4745 struct seq_file *m = filp->private_data;
4746 struct trace_iterator *iter = m->private;
4747 struct trace_array *tr = iter->tr;
4748 unsigned long val;
4749 int ret;
4751 ret = tracing_update_buffers();
4752 if (ret < 0)
4753 return ret;
4755 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4756 if (ret)
4757 return ret;
4759 mutex_lock(&trace_types_lock);
4761 if (tr->current_trace->use_max_tr) {
4762 ret = -EBUSY;
4763 goto out;
4766 switch (val) {
4767 case 0:
4768 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4769 ret = -EINVAL;
4770 break;
4772 if (tr->allocated_snapshot)
4773 free_snapshot(tr);
4774 break;
4775 case 1:
4776 /* Only allow per-cpu swap if the ring buffer supports it */
4777 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4778 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4779 ret = -EINVAL;
4780 break;
4782 #endif
4783 if (!tr->allocated_snapshot) {
4784 ret = alloc_snapshot(tr);
4785 if (ret < 0)
4786 break;
4788 local_irq_disable();
4789 /* Now, we're going to swap */
4790 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4791 update_max_tr(tr, current, smp_processor_id());
4792 else
4793 update_max_tr_single(tr, current, iter->cpu_file);
4794 local_irq_enable();
4795 break;
4796 default:
4797 if (tr->allocated_snapshot) {
4798 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4799 tracing_reset_online_cpus(&tr->max_buffer);
4800 else
4801 tracing_reset(&tr->max_buffer, iter->cpu_file);
4803 break;
4806 if (ret >= 0) {
4807 *ppos += cnt;
4808 ret = cnt;
4810 out:
4811 mutex_unlock(&trace_types_lock);
4812 return ret;
4815 static int tracing_snapshot_release(struct inode *inode, struct file *file)
4817 struct seq_file *m = file->private_data;
4818 int ret;
4820 ret = tracing_release(inode, file);
4822 if (file->f_mode & FMODE_READ)
4823 return ret;
4825 /* If write only, the seq_file is just a stub */
4826 if (m)
4827 kfree(m->private);
4828 kfree(m);
4830 return 0;
4833 static int tracing_buffers_open(struct inode *inode, struct file *filp);
4834 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
4835 size_t count, loff_t *ppos);
4836 static int tracing_buffers_release(struct inode *inode, struct file *file);
4837 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4838 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
4840 static int snapshot_raw_open(struct inode *inode, struct file *filp)
4842 struct ftrace_buffer_info *info;
4843 int ret;
4845 ret = tracing_buffers_open(inode, filp);
4846 if (ret < 0)
4847 return ret;
4849 info = filp->private_data;
4851 if (info->iter.trace->use_max_tr) {
4852 tracing_buffers_release(inode, filp);
4853 return -EBUSY;
4856 info->iter.snapshot = true;
4857 info->iter.trace_buffer = &info->iter.tr->max_buffer;
4859 return ret;
4862 #endif /* CONFIG_TRACER_SNAPSHOT */
4865 static const struct file_operations tracing_max_lat_fops = {
4866 .open = tracing_open_generic,
4867 .read = tracing_max_lat_read,
4868 .write = tracing_max_lat_write,
4869 .llseek = generic_file_llseek,
4872 static const struct file_operations set_tracer_fops = {
4873 .open = tracing_open_generic,
4874 .read = tracing_set_trace_read,
4875 .write = tracing_set_trace_write,
4876 .llseek = generic_file_llseek,
4879 static const struct file_operations tracing_pipe_fops = {
4880 .open = tracing_open_pipe,
4881 .poll = tracing_poll_pipe,
4882 .read = tracing_read_pipe,
4883 .splice_read = tracing_splice_read_pipe,
4884 .release = tracing_release_pipe,
4885 .llseek = no_llseek,
4888 static const struct file_operations tracing_entries_fops = {
4889 .open = tracing_open_generic_tr,
4890 .read = tracing_entries_read,
4891 .write = tracing_entries_write,
4892 .llseek = generic_file_llseek,
4893 .release = tracing_release_generic_tr,
4896 static const struct file_operations tracing_total_entries_fops = {
4897 .open = tracing_open_generic_tr,
4898 .read = tracing_total_entries_read,
4899 .llseek = generic_file_llseek,
4900 .release = tracing_release_generic_tr,
4903 static const struct file_operations tracing_free_buffer_fops = {
4904 .open = tracing_open_generic_tr,
4905 .write = tracing_free_buffer_write,
4906 .release = tracing_free_buffer_release,
4909 static const struct file_operations tracing_mark_fops = {
4910 .open = tracing_open_generic_tr,
4911 .write = tracing_mark_write,
4912 .llseek = generic_file_llseek,
4913 .release = tracing_release_generic_tr,
4916 static const struct file_operations trace_clock_fops = {
4917 .open = tracing_clock_open,
4918 .read = seq_read,
4919 .llseek = seq_lseek,
4920 .release = tracing_single_release_tr,
4921 .write = tracing_clock_write,
4924 #ifdef CONFIG_TRACER_SNAPSHOT
4925 static const struct file_operations snapshot_fops = {
4926 .open = tracing_snapshot_open,
4927 .read = seq_read,
4928 .write = tracing_snapshot_write,
4929 .llseek = tracing_seek,
4930 .release = tracing_snapshot_release,
4933 static const struct file_operations snapshot_raw_fops = {
4934 .open = snapshot_raw_open,
4935 .read = tracing_buffers_read,
4936 .release = tracing_buffers_release,
4937 .splice_read = tracing_buffers_splice_read,
4938 .llseek = no_llseek,
4941 #endif /* CONFIG_TRACER_SNAPSHOT */
4943 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4945 struct trace_array *tr = inode->i_private;
4946 struct ftrace_buffer_info *info;
4947 int ret;
4949 if (tracing_disabled)
4950 return -ENODEV;
4952 if (trace_array_get(tr) < 0)
4953 return -ENODEV;
4955 info = kzalloc(sizeof(*info), GFP_KERNEL);
4956 if (!info) {
4957 trace_array_put(tr);
4958 return -ENOMEM;
4961 mutex_lock(&trace_types_lock);
4963 info->iter.tr = tr;
4964 info->iter.cpu_file = tracing_get_cpu(inode);
4965 info->iter.trace = tr->current_trace;
4966 info->iter.trace_buffer = &tr->trace_buffer;
4967 info->spare = NULL;
4968 /* Force reading ring buffer for first read */
4969 info->read = (unsigned int)-1;
4971 filp->private_data = info;
4973 mutex_unlock(&trace_types_lock);
4975 ret = nonseekable_open(inode, filp);
4976 if (ret < 0)
4977 trace_array_put(tr);
4979 return ret;
4982 static unsigned int
4983 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
4985 struct ftrace_buffer_info *info = filp->private_data;
4986 struct trace_iterator *iter = &info->iter;
4988 return trace_poll(iter, filp, poll_table);
4991 static ssize_t
4992 tracing_buffers_read(struct file *filp, char __user *ubuf,
4993 size_t count, loff_t *ppos)
4995 struct ftrace_buffer_info *info = filp->private_data;
4996 struct trace_iterator *iter = &info->iter;
4997 ssize_t ret;
4998 ssize_t size;
5000 if (!count)
5001 return 0;
5003 mutex_lock(&trace_types_lock);
5005 #ifdef CONFIG_TRACER_MAX_TRACE
5006 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5007 size = -EBUSY;
5008 goto out_unlock;
5010 #endif
5012 if (!info->spare)
5013 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
5014 iter->cpu_file);
5015 size = -ENOMEM;
5016 if (!info->spare)
5017 goto out_unlock;
5019 /* Do we have previous read data to read? */
5020 if (info->read < PAGE_SIZE)
5021 goto read;
5023 again:
5024 trace_access_lock(iter->cpu_file);
5025 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
5026 &info->spare,
5027 count,
5028 iter->cpu_file, 0);
5029 trace_access_unlock(iter->cpu_file);
5031 if (ret < 0) {
5032 if (trace_empty(iter)) {
5033 if ((filp->f_flags & O_NONBLOCK)) {
5034 size = -EAGAIN;
5035 goto out_unlock;
5037 mutex_unlock(&trace_types_lock);
5038 ret = iter->trace->wait_pipe(iter);
5039 mutex_lock(&trace_types_lock);
5040 if (ret) {
5041 size = ret;
5042 goto out_unlock;
5044 if (signal_pending(current)) {
5045 size = -EINTR;
5046 goto out_unlock;
5048 goto again;
5050 size = 0;
5051 goto out_unlock;
5054 info->read = 0;
5055 read:
5056 size = PAGE_SIZE - info->read;
5057 if (size > count)
5058 size = count;
5060 ret = copy_to_user(ubuf, info->spare + info->read, size);
5061 if (ret == size) {
5062 size = -EFAULT;
5063 goto out_unlock;
5065 size -= ret;
5067 *ppos += size;
5068 info->read += size;
5070 out_unlock:
5071 mutex_unlock(&trace_types_lock);
5073 return size;
5076 static int tracing_buffers_release(struct inode *inode, struct file *file)
5078 struct ftrace_buffer_info *info = file->private_data;
5079 struct trace_iterator *iter = &info->iter;
5081 mutex_lock(&trace_types_lock);
5083 __trace_array_put(iter->tr);
5085 if (info->spare)
5086 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
5087 kfree(info);
5089 mutex_unlock(&trace_types_lock);
5091 return 0;
5094 struct buffer_ref {
5095 struct ring_buffer *buffer;
5096 void *page;
5097 int ref;
5100 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
5101 struct pipe_buffer *buf)
5103 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5105 if (--ref->ref)
5106 return;
5108 ring_buffer_free_read_page(ref->buffer, ref->page);
5109 kfree(ref);
5110 buf->private = 0;
5113 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
5114 struct pipe_buffer *buf)
5116 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5118 ref->ref++;
5121 /* Pipe buffer operations for a buffer. */
5122 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
5123 .can_merge = 0,
5124 .map = generic_pipe_buf_map,
5125 .unmap = generic_pipe_buf_unmap,
5126 .confirm = generic_pipe_buf_confirm,
5127 .release = buffer_pipe_buf_release,
5128 .steal = generic_pipe_buf_steal,
5129 .get = buffer_pipe_buf_get,
5133 * Callback from splice_to_pipe(), if we need to release some pages
5134 * at the end of the spd in case we error'ed out in filling the pipe.
5136 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
5138 struct buffer_ref *ref =
5139 (struct buffer_ref *)spd->partial[i].private;
5141 if (--ref->ref)
5142 return;
5144 ring_buffer_free_read_page(ref->buffer, ref->page);
5145 kfree(ref);
5146 spd->partial[i].private = 0;
5149 static ssize_t
5150 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5151 struct pipe_inode_info *pipe, size_t len,
5152 unsigned int flags)
5154 struct ftrace_buffer_info *info = file->private_data;
5155 struct trace_iterator *iter = &info->iter;
5156 struct partial_page partial_def[PIPE_DEF_BUFFERS];
5157 struct page *pages_def[PIPE_DEF_BUFFERS];
5158 struct splice_pipe_desc spd = {
5159 .pages = pages_def,
5160 .partial = partial_def,
5161 .nr_pages_max = PIPE_DEF_BUFFERS,
5162 .flags = flags,
5163 .ops = &buffer_pipe_buf_ops,
5164 .spd_release = buffer_spd_release,
5166 struct buffer_ref *ref;
5167 int entries, size, i;
5168 ssize_t ret;
5170 mutex_lock(&trace_types_lock);
5172 #ifdef CONFIG_TRACER_MAX_TRACE
5173 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5174 ret = -EBUSY;
5175 goto out;
5177 #endif
5179 if (splice_grow_spd(pipe, &spd)) {
5180 ret = -ENOMEM;
5181 goto out;
5184 if (*ppos & (PAGE_SIZE - 1)) {
5185 ret = -EINVAL;
5186 goto out;
5189 if (len & (PAGE_SIZE - 1)) {
5190 if (len < PAGE_SIZE) {
5191 ret = -EINVAL;
5192 goto out;
5194 len &= PAGE_MASK;
5197 again:
5198 trace_access_lock(iter->cpu_file);
5199 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5201 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
5202 struct page *page;
5203 int r;
5205 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
5206 if (!ref)
5207 break;
5209 ref->ref = 1;
5210 ref->buffer = iter->trace_buffer->buffer;
5211 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
5212 if (!ref->page) {
5213 kfree(ref);
5214 break;
5217 r = ring_buffer_read_page(ref->buffer, &ref->page,
5218 len, iter->cpu_file, 1);
5219 if (r < 0) {
5220 ring_buffer_free_read_page(ref->buffer, ref->page);
5221 kfree(ref);
5222 break;
5226 * zero out any left over data, this is going to
5227 * user land.
5229 size = ring_buffer_page_len(ref->page);
5230 if (size < PAGE_SIZE)
5231 memset(ref->page + size, 0, PAGE_SIZE - size);
5233 page = virt_to_page(ref->page);
5235 spd.pages[i] = page;
5236 spd.partial[i].len = PAGE_SIZE;
5237 spd.partial[i].offset = 0;
5238 spd.partial[i].private = (unsigned long)ref;
5239 spd.nr_pages++;
5240 *ppos += PAGE_SIZE;
5242 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5245 trace_access_unlock(iter->cpu_file);
5246 spd.nr_pages = i;
5248 /* did we read anything? */
5249 if (!spd.nr_pages) {
5250 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
5251 ret = -EAGAIN;
5252 goto out;
5254 mutex_unlock(&trace_types_lock);
5255 ret = iter->trace->wait_pipe(iter);
5256 mutex_lock(&trace_types_lock);
5257 if (ret)
5258 goto out;
5259 if (signal_pending(current)) {
5260 ret = -EINTR;
5261 goto out;
5263 goto again;
5266 ret = splice_to_pipe(pipe, &spd);
5267 splice_shrink_spd(&spd);
5268 out:
5269 mutex_unlock(&trace_types_lock);
5271 return ret;
5274 static const struct file_operations tracing_buffers_fops = {
5275 .open = tracing_buffers_open,
5276 .read = tracing_buffers_read,
5277 .poll = tracing_buffers_poll,
5278 .release = tracing_buffers_release,
5279 .splice_read = tracing_buffers_splice_read,
5280 .llseek = no_llseek,
5283 static ssize_t
5284 tracing_stats_read(struct file *filp, char __user *ubuf,
5285 size_t count, loff_t *ppos)
5287 struct inode *inode = file_inode(filp);
5288 struct trace_array *tr = inode->i_private;
5289 struct trace_buffer *trace_buf = &tr->trace_buffer;
5290 int cpu = tracing_get_cpu(inode);
5291 struct trace_seq *s;
5292 unsigned long cnt;
5293 unsigned long long t;
5294 unsigned long usec_rem;
5296 s = kmalloc(sizeof(*s), GFP_KERNEL);
5297 if (!s)
5298 return -ENOMEM;
5300 trace_seq_init(s);
5302 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
5303 trace_seq_printf(s, "entries: %ld\n", cnt);
5305 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
5306 trace_seq_printf(s, "overrun: %ld\n", cnt);
5308 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
5309 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5311 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
5312 trace_seq_printf(s, "bytes: %ld\n", cnt);
5314 if (trace_clocks[tr->clock_id].in_ns) {
5315 /* local or global for trace_clock */
5316 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5317 usec_rem = do_div(t, USEC_PER_SEC);
5318 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5319 t, usec_rem);
5321 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
5322 usec_rem = do_div(t, USEC_PER_SEC);
5323 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5324 } else {
5325 /* counter or tsc mode for trace_clock */
5326 trace_seq_printf(s, "oldest event ts: %llu\n",
5327 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5329 trace_seq_printf(s, "now ts: %llu\n",
5330 ring_buffer_time_stamp(trace_buf->buffer, cpu));
5333 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
5334 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5336 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
5337 trace_seq_printf(s, "read events: %ld\n", cnt);
5339 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
5341 kfree(s);
5343 return count;
5346 static const struct file_operations tracing_stats_fops = {
5347 .open = tracing_open_generic_tr,
5348 .read = tracing_stats_read,
5349 .llseek = generic_file_llseek,
5350 .release = tracing_release_generic_tr,
5353 #ifdef CONFIG_DYNAMIC_FTRACE
5355 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
5357 return 0;
5360 static ssize_t
5361 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
5362 size_t cnt, loff_t *ppos)
5364 static char ftrace_dyn_info_buffer[1024];
5365 static DEFINE_MUTEX(dyn_info_mutex);
5366 unsigned long *p = filp->private_data;
5367 char *buf = ftrace_dyn_info_buffer;
5368 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
5369 int r;
5371 mutex_lock(&dyn_info_mutex);
5372 r = sprintf(buf, "%ld ", *p);
5374 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
5375 buf[r++] = '\n';
5377 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5379 mutex_unlock(&dyn_info_mutex);
5381 return r;
5384 static const struct file_operations tracing_dyn_info_fops = {
5385 .open = tracing_open_generic,
5386 .read = tracing_read_dyn_info,
5387 .llseek = generic_file_llseek,
5389 #endif /* CONFIG_DYNAMIC_FTRACE */
5391 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5392 static void
5393 ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5395 tracing_snapshot();
5398 static void
5399 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5401 unsigned long *count = (long *)data;
5403 if (!*count)
5404 return;
5406 if (*count != -1)
5407 (*count)--;
5409 tracing_snapshot();
5412 static int
5413 ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5414 struct ftrace_probe_ops *ops, void *data)
5416 long count = (long)data;
5418 seq_printf(m, "%ps:", (void *)ip);
5420 seq_printf(m, "snapshot");
5422 if (count == -1)
5423 seq_printf(m, ":unlimited\n");
5424 else
5425 seq_printf(m, ":count=%ld\n", count);
5427 return 0;
5430 static struct ftrace_probe_ops snapshot_probe_ops = {
5431 .func = ftrace_snapshot,
5432 .print = ftrace_snapshot_print,
5435 static struct ftrace_probe_ops snapshot_count_probe_ops = {
5436 .func = ftrace_count_snapshot,
5437 .print = ftrace_snapshot_print,
5440 static int
5441 ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5442 char *glob, char *cmd, char *param, int enable)
5444 struct ftrace_probe_ops *ops;
5445 void *count = (void *)-1;
5446 char *number;
5447 int ret;
5449 /* hash funcs only work with set_ftrace_filter */
5450 if (!enable)
5451 return -EINVAL;
5453 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
5455 if (glob[0] == '!') {
5456 unregister_ftrace_function_probe_func(glob+1, ops);
5457 return 0;
5460 if (!param)
5461 goto out_reg;
5463 number = strsep(&param, ":");
5465 if (!strlen(number))
5466 goto out_reg;
5469 * We use the callback data field (which is a pointer)
5470 * as our counter.
5472 ret = kstrtoul(number, 0, (unsigned long *)&count);
5473 if (ret)
5474 return ret;
5476 out_reg:
5477 ret = register_ftrace_function_probe(glob, ops, count);
5479 if (ret >= 0)
5480 alloc_snapshot(&global_trace);
5482 return ret < 0 ? ret : 0;
5485 static struct ftrace_func_command ftrace_snapshot_cmd = {
5486 .name = "snapshot",
5487 .func = ftrace_trace_snapshot_callback,
5490 static int register_snapshot_cmd(void)
5492 return register_ftrace_command(&ftrace_snapshot_cmd);
5494 #else
5495 static inline int register_snapshot_cmd(void) { return 0; }
5496 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
5498 struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
5500 if (tr->dir)
5501 return tr->dir;
5503 if (!debugfs_initialized())
5504 return NULL;
5506 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5507 tr->dir = debugfs_create_dir("tracing", NULL);
5509 if (!tr->dir)
5510 pr_warn_once("Could not create debugfs directory 'tracing'\n");
5512 return tr->dir;
5515 struct dentry *tracing_init_dentry(void)
5517 return tracing_init_dentry_tr(&global_trace);
5520 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
5522 struct dentry *d_tracer;
5524 if (tr->percpu_dir)
5525 return tr->percpu_dir;
5527 d_tracer = tracing_init_dentry_tr(tr);
5528 if (!d_tracer)
5529 return NULL;
5531 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
5533 WARN_ONCE(!tr->percpu_dir,
5534 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
5536 return tr->percpu_dir;
5539 static struct dentry *
5540 trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
5541 void *data, long cpu, const struct file_operations *fops)
5543 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
5545 if (ret) /* See tracing_get_cpu() */
5546 ret->d_inode->i_cdev = (void *)(cpu + 1);
5547 return ret;
5550 static void
5551 tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
5553 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5554 struct dentry *d_cpu;
5555 char cpu_dir[30]; /* 30 characters should be more than enough */
5557 if (!d_percpu)
5558 return;
5560 snprintf(cpu_dir, 30, "cpu%ld", cpu);
5561 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5562 if (!d_cpu) {
5563 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5564 return;
5567 /* per cpu trace_pipe */
5568 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
5569 tr, cpu, &tracing_pipe_fops);
5571 /* per cpu trace */
5572 trace_create_cpu_file("trace", 0644, d_cpu,
5573 tr, cpu, &tracing_fops);
5575 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
5576 tr, cpu, &tracing_buffers_fops);
5578 trace_create_cpu_file("stats", 0444, d_cpu,
5579 tr, cpu, &tracing_stats_fops);
5581 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
5582 tr, cpu, &tracing_entries_fops);
5584 #ifdef CONFIG_TRACER_SNAPSHOT
5585 trace_create_cpu_file("snapshot", 0644, d_cpu,
5586 tr, cpu, &snapshot_fops);
5588 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
5589 tr, cpu, &snapshot_raw_fops);
5590 #endif
5593 #ifdef CONFIG_FTRACE_SELFTEST
5594 /* Let selftest have access to static functions in this file */
5595 #include "trace_selftest.c"
5596 #endif
5598 struct trace_option_dentry {
5599 struct tracer_opt *opt;
5600 struct tracer_flags *flags;
5601 struct trace_array *tr;
5602 struct dentry *entry;
5605 static ssize_t
5606 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5607 loff_t *ppos)
5609 struct trace_option_dentry *topt = filp->private_data;
5610 char *buf;
5612 if (topt->flags->val & topt->opt->bit)
5613 buf = "1\n";
5614 else
5615 buf = "0\n";
5617 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5620 static ssize_t
5621 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5622 loff_t *ppos)
5624 struct trace_option_dentry *topt = filp->private_data;
5625 unsigned long val;
5626 int ret;
5628 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5629 if (ret)
5630 return ret;
5632 if (val != 0 && val != 1)
5633 return -EINVAL;
5635 if (!!(topt->flags->val & topt->opt->bit) != val) {
5636 mutex_lock(&trace_types_lock);
5637 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
5638 topt->opt, !val);
5639 mutex_unlock(&trace_types_lock);
5640 if (ret)
5641 return ret;
5644 *ppos += cnt;
5646 return cnt;
5650 static const struct file_operations trace_options_fops = {
5651 .open = tracing_open_generic,
5652 .read = trace_options_read,
5653 .write = trace_options_write,
5654 .llseek = generic_file_llseek,
5657 static ssize_t
5658 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5659 loff_t *ppos)
5661 long index = (long)filp->private_data;
5662 char *buf;
5664 if (trace_flags & (1 << index))
5665 buf = "1\n";
5666 else
5667 buf = "0\n";
5669 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5672 static ssize_t
5673 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5674 loff_t *ppos)
5676 struct trace_array *tr = &global_trace;
5677 long index = (long)filp->private_data;
5678 unsigned long val;
5679 int ret;
5681 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5682 if (ret)
5683 return ret;
5685 if (val != 0 && val != 1)
5686 return -EINVAL;
5688 mutex_lock(&trace_types_lock);
5689 ret = set_tracer_flag(tr, 1 << index, val);
5690 mutex_unlock(&trace_types_lock);
5692 if (ret < 0)
5693 return ret;
5695 *ppos += cnt;
5697 return cnt;
5700 static const struct file_operations trace_options_core_fops = {
5701 .open = tracing_open_generic,
5702 .read = trace_options_core_read,
5703 .write = trace_options_core_write,
5704 .llseek = generic_file_llseek,
5707 struct dentry *trace_create_file(const char *name,
5708 umode_t mode,
5709 struct dentry *parent,
5710 void *data,
5711 const struct file_operations *fops)
5713 struct dentry *ret;
5715 ret = debugfs_create_file(name, mode, parent, data, fops);
5716 if (!ret)
5717 pr_warning("Could not create debugfs '%s' entry\n", name);
5719 return ret;
5723 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
5725 struct dentry *d_tracer;
5727 if (tr->options)
5728 return tr->options;
5730 d_tracer = tracing_init_dentry_tr(tr);
5731 if (!d_tracer)
5732 return NULL;
5734 tr->options = debugfs_create_dir("options", d_tracer);
5735 if (!tr->options) {
5736 pr_warning("Could not create debugfs directory 'options'\n");
5737 return NULL;
5740 return tr->options;
5743 static void
5744 create_trace_option_file(struct trace_array *tr,
5745 struct trace_option_dentry *topt,
5746 struct tracer_flags *flags,
5747 struct tracer_opt *opt)
5749 struct dentry *t_options;
5751 t_options = trace_options_init_dentry(tr);
5752 if (!t_options)
5753 return;
5755 topt->flags = flags;
5756 topt->opt = opt;
5757 topt->tr = tr;
5759 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
5760 &trace_options_fops);
5764 static struct trace_option_dentry *
5765 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
5767 struct trace_option_dentry *topts;
5768 struct tracer_flags *flags;
5769 struct tracer_opt *opts;
5770 int cnt;
5772 if (!tracer)
5773 return NULL;
5775 flags = tracer->flags;
5777 if (!flags || !flags->opts)
5778 return NULL;
5780 opts = flags->opts;
5782 for (cnt = 0; opts[cnt].name; cnt++)
5785 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
5786 if (!topts)
5787 return NULL;
5789 for (cnt = 0; opts[cnt].name; cnt++)
5790 create_trace_option_file(tr, &topts[cnt], flags,
5791 &opts[cnt]);
5793 return topts;
5796 static void
5797 destroy_trace_option_files(struct trace_option_dentry *topts)
5799 int cnt;
5801 if (!topts)
5802 return;
5804 for (cnt = 0; topts[cnt].opt; cnt++) {
5805 if (topts[cnt].entry)
5806 debugfs_remove(topts[cnt].entry);
5809 kfree(topts);
5812 static struct dentry *
5813 create_trace_option_core_file(struct trace_array *tr,
5814 const char *option, long index)
5816 struct dentry *t_options;
5818 t_options = trace_options_init_dentry(tr);
5819 if (!t_options)
5820 return NULL;
5822 return trace_create_file(option, 0644, t_options, (void *)index,
5823 &trace_options_core_fops);
5826 static __init void create_trace_options_dir(struct trace_array *tr)
5828 struct dentry *t_options;
5829 int i;
5831 t_options = trace_options_init_dentry(tr);
5832 if (!t_options)
5833 return;
5835 for (i = 0; trace_options[i]; i++)
5836 create_trace_option_core_file(tr, trace_options[i], i);
5839 static ssize_t
5840 rb_simple_read(struct file *filp, char __user *ubuf,
5841 size_t cnt, loff_t *ppos)
5843 struct trace_array *tr = filp->private_data;
5844 char buf[64];
5845 int r;
5847 r = tracer_tracing_is_on(tr);
5848 r = sprintf(buf, "%d\n", r);
5850 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5853 static ssize_t
5854 rb_simple_write(struct file *filp, const char __user *ubuf,
5855 size_t cnt, loff_t *ppos)
5857 struct trace_array *tr = filp->private_data;
5858 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5859 unsigned long val;
5860 int ret;
5862 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5863 if (ret)
5864 return ret;
5866 if (buffer) {
5867 mutex_lock(&trace_types_lock);
5868 if (val) {
5869 tracer_tracing_on(tr);
5870 if (tr->current_trace->start)
5871 tr->current_trace->start(tr);
5872 } else {
5873 tracer_tracing_off(tr);
5874 if (tr->current_trace->stop)
5875 tr->current_trace->stop(tr);
5877 mutex_unlock(&trace_types_lock);
5880 (*ppos)++;
5882 return cnt;
5885 static const struct file_operations rb_simple_fops = {
5886 .open = tracing_open_generic_tr,
5887 .read = rb_simple_read,
5888 .write = rb_simple_write,
5889 .release = tracing_release_generic_tr,
5890 .llseek = default_llseek,
5893 struct dentry *trace_instance_dir;
5895 static void
5896 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5898 static int
5899 allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
5901 enum ring_buffer_flags rb_flags;
5903 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5905 buf->tr = tr;
5907 buf->buffer = ring_buffer_alloc(size, rb_flags);
5908 if (!buf->buffer)
5909 return -ENOMEM;
5911 buf->data = alloc_percpu(struct trace_array_cpu);
5912 if (!buf->data) {
5913 ring_buffer_free(buf->buffer);
5914 return -ENOMEM;
5917 /* Allocate the first page for all buffers */
5918 set_buffer_entries(&tr->trace_buffer,
5919 ring_buffer_size(tr->trace_buffer.buffer, 0));
5921 return 0;
5924 static int allocate_trace_buffers(struct trace_array *tr, int size)
5926 int ret;
5928 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
5929 if (ret)
5930 return ret;
5932 #ifdef CONFIG_TRACER_MAX_TRACE
5933 ret = allocate_trace_buffer(tr, &tr->max_buffer,
5934 allocate_snapshot ? size : 1);
5935 if (WARN_ON(ret)) {
5936 ring_buffer_free(tr->trace_buffer.buffer);
5937 free_percpu(tr->trace_buffer.data);
5938 return -ENOMEM;
5940 tr->allocated_snapshot = allocate_snapshot;
5943 * Only the top level trace array gets its snapshot allocated
5944 * from the kernel command line.
5946 allocate_snapshot = false;
5947 #endif
5948 return 0;
5951 static int new_instance_create(const char *name)
5953 struct trace_array *tr;
5954 int ret;
5956 mutex_lock(&trace_types_lock);
5958 ret = -EEXIST;
5959 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5960 if (tr->name && strcmp(tr->name, name) == 0)
5961 goto out_unlock;
5964 ret = -ENOMEM;
5965 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
5966 if (!tr)
5967 goto out_unlock;
5969 tr->name = kstrdup(name, GFP_KERNEL);
5970 if (!tr->name)
5971 goto out_free_tr;
5973 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
5974 goto out_free_tr;
5976 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
5978 raw_spin_lock_init(&tr->start_lock);
5980 tr->current_trace = &nop_trace;
5982 INIT_LIST_HEAD(&tr->systems);
5983 INIT_LIST_HEAD(&tr->events);
5985 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
5986 goto out_free_tr;
5988 tr->dir = debugfs_create_dir(name, trace_instance_dir);
5989 if (!tr->dir)
5990 goto out_free_tr;
5992 ret = event_trace_add_tracer(tr->dir, tr);
5993 if (ret) {
5994 debugfs_remove_recursive(tr->dir);
5995 goto out_free_tr;
5998 init_tracer_debugfs(tr, tr->dir);
6000 list_add(&tr->list, &ftrace_trace_arrays);
6002 mutex_unlock(&trace_types_lock);
6004 return 0;
6006 out_free_tr:
6007 if (tr->trace_buffer.buffer)
6008 ring_buffer_free(tr->trace_buffer.buffer);
6009 free_cpumask_var(tr->tracing_cpumask);
6010 kfree(tr->name);
6011 kfree(tr);
6013 out_unlock:
6014 mutex_unlock(&trace_types_lock);
6016 return ret;
6020 static int instance_delete(const char *name)
6022 struct trace_array *tr;
6023 int found = 0;
6024 int ret;
6026 mutex_lock(&trace_types_lock);
6028 ret = -ENODEV;
6029 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6030 if (tr->name && strcmp(tr->name, name) == 0) {
6031 found = 1;
6032 break;
6035 if (!found)
6036 goto out_unlock;
6038 ret = -EBUSY;
6039 if (tr->ref)
6040 goto out_unlock;
6042 list_del(&tr->list);
6044 event_trace_del_tracer(tr);
6045 debugfs_remove_recursive(tr->dir);
6046 free_percpu(tr->trace_buffer.data);
6047 ring_buffer_free(tr->trace_buffer.buffer);
6049 kfree(tr->name);
6050 kfree(tr);
6052 ret = 0;
6054 out_unlock:
6055 mutex_unlock(&trace_types_lock);
6057 return ret;
6060 static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
6062 struct dentry *parent;
6063 int ret;
6065 /* Paranoid: Make sure the parent is the "instances" directory */
6066 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
6067 if (WARN_ON_ONCE(parent != trace_instance_dir))
6068 return -ENOENT;
6071 * The inode mutex is locked, but debugfs_create_dir() will also
6072 * take the mutex. As the instances directory can not be destroyed
6073 * or changed in any other way, it is safe to unlock it, and
6074 * let the dentry try. If two users try to make the same dir at
6075 * the same time, then the new_instance_create() will determine the
6076 * winner.
6078 mutex_unlock(&inode->i_mutex);
6080 ret = new_instance_create(dentry->d_iname);
6082 mutex_lock(&inode->i_mutex);
6084 return ret;
6087 static int instance_rmdir(struct inode *inode, struct dentry *dentry)
6089 struct dentry *parent;
6090 int ret;
6092 /* Paranoid: Make sure the parent is the "instances" directory */
6093 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
6094 if (WARN_ON_ONCE(parent != trace_instance_dir))
6095 return -ENOENT;
6097 /* The caller did a dget() on dentry */
6098 mutex_unlock(&dentry->d_inode->i_mutex);
6101 * The inode mutex is locked, but debugfs_create_dir() will also
6102 * take the mutex. As the instances directory can not be destroyed
6103 * or changed in any other way, it is safe to unlock it, and
6104 * let the dentry try. If two users try to make the same dir at
6105 * the same time, then the instance_delete() will determine the
6106 * winner.
6108 mutex_unlock(&inode->i_mutex);
6110 ret = instance_delete(dentry->d_iname);
6112 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
6113 mutex_lock(&dentry->d_inode->i_mutex);
6115 return ret;
6118 static const struct inode_operations instance_dir_inode_operations = {
6119 .lookup = simple_lookup,
6120 .mkdir = instance_mkdir,
6121 .rmdir = instance_rmdir,
6124 static __init void create_trace_instances(struct dentry *d_tracer)
6126 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
6127 if (WARN_ON(!trace_instance_dir))
6128 return;
6130 /* Hijack the dir inode operations, to allow mkdir */
6131 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
6134 static void
6135 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
6137 int cpu;
6139 trace_create_file("tracing_cpumask", 0644, d_tracer,
6140 tr, &tracing_cpumask_fops);
6142 trace_create_file("trace_options", 0644, d_tracer,
6143 tr, &tracing_iter_fops);
6145 trace_create_file("trace", 0644, d_tracer,
6146 tr, &tracing_fops);
6148 trace_create_file("trace_pipe", 0444, d_tracer,
6149 tr, &tracing_pipe_fops);
6151 trace_create_file("buffer_size_kb", 0644, d_tracer,
6152 tr, &tracing_entries_fops);
6154 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
6155 tr, &tracing_total_entries_fops);
6157 trace_create_file("free_buffer", 0200, d_tracer,
6158 tr, &tracing_free_buffer_fops);
6160 trace_create_file("trace_marker", 0220, d_tracer,
6161 tr, &tracing_mark_fops);
6163 trace_create_file("trace_clock", 0644, d_tracer, tr,
6164 &trace_clock_fops);
6166 trace_create_file("tracing_on", 0644, d_tracer,
6167 tr, &rb_simple_fops);
6169 #ifdef CONFIG_TRACER_SNAPSHOT
6170 trace_create_file("snapshot", 0644, d_tracer,
6171 tr, &snapshot_fops);
6172 #endif
6174 for_each_tracing_cpu(cpu)
6175 tracing_init_debugfs_percpu(tr, cpu);
6179 static __init int tracer_init_debugfs(void)
6181 struct dentry *d_tracer;
6183 trace_access_lock_init();
6185 d_tracer = tracing_init_dentry();
6186 if (!d_tracer)
6187 return 0;
6189 init_tracer_debugfs(&global_trace, d_tracer);
6191 trace_create_file("available_tracers", 0444, d_tracer,
6192 &global_trace, &show_traces_fops);
6194 trace_create_file("current_tracer", 0644, d_tracer,
6195 &global_trace, &set_tracer_fops);
6197 #ifdef CONFIG_TRACER_MAX_TRACE
6198 trace_create_file("tracing_max_latency", 0644, d_tracer,
6199 &tracing_max_latency, &tracing_max_lat_fops);
6200 #endif
6202 trace_create_file("tracing_thresh", 0644, d_tracer,
6203 &tracing_thresh, &tracing_max_lat_fops);
6205 trace_create_file("README", 0444, d_tracer,
6206 NULL, &tracing_readme_fops);
6208 trace_create_file("saved_cmdlines", 0444, d_tracer,
6209 NULL, &tracing_saved_cmdlines_fops);
6211 #ifdef CONFIG_DYNAMIC_FTRACE
6212 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
6213 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
6214 #endif
6216 create_trace_instances(d_tracer);
6218 create_trace_options_dir(&global_trace);
6220 return 0;
6223 static int trace_panic_handler(struct notifier_block *this,
6224 unsigned long event, void *unused)
6226 if (ftrace_dump_on_oops)
6227 ftrace_dump(ftrace_dump_on_oops);
6228 return NOTIFY_OK;
6231 static struct notifier_block trace_panic_notifier = {
6232 .notifier_call = trace_panic_handler,
6233 .next = NULL,
6234 .priority = 150 /* priority: INT_MAX >= x >= 0 */
6237 static int trace_die_handler(struct notifier_block *self,
6238 unsigned long val,
6239 void *data)
6241 switch (val) {
6242 case DIE_OOPS:
6243 if (ftrace_dump_on_oops)
6244 ftrace_dump(ftrace_dump_on_oops);
6245 break;
6246 default:
6247 break;
6249 return NOTIFY_OK;
6252 static struct notifier_block trace_die_notifier = {
6253 .notifier_call = trace_die_handler,
6254 .priority = 200
6258 * printk is set to max of 1024, we really don't need it that big.
6259 * Nothing should be printing 1000 characters anyway.
6261 #define TRACE_MAX_PRINT 1000
6264 * Define here KERN_TRACE so that we have one place to modify
6265 * it if we decide to change what log level the ftrace dump
6266 * should be at.
6268 #define KERN_TRACE KERN_EMERG
6270 void
6271 trace_printk_seq(struct trace_seq *s)
6273 /* Probably should print a warning here. */
6274 if (s->len >= TRACE_MAX_PRINT)
6275 s->len = TRACE_MAX_PRINT;
6277 /* should be zero ended, but we are paranoid. */
6278 s->buffer[s->len] = 0;
6280 printk(KERN_TRACE "%s", s->buffer);
6282 trace_seq_init(s);
6285 void trace_init_global_iter(struct trace_iterator *iter)
6287 iter->tr = &global_trace;
6288 iter->trace = iter->tr->current_trace;
6289 iter->cpu_file = RING_BUFFER_ALL_CPUS;
6290 iter->trace_buffer = &global_trace.trace_buffer;
6293 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
6295 /* use static because iter can be a bit big for the stack */
6296 static struct trace_iterator iter;
6297 static atomic_t dump_running;
6298 unsigned int old_userobj;
6299 unsigned long flags;
6300 int cnt = 0, cpu;
6302 /* Only allow one dump user at a time. */
6303 if (atomic_inc_return(&dump_running) != 1) {
6304 atomic_dec(&dump_running);
6305 return;
6309 * Always turn off tracing when we dump.
6310 * We don't need to show trace output of what happens
6311 * between multiple crashes.
6313 * If the user does a sysrq-z, then they can re-enable
6314 * tracing with echo 1 > tracing_on.
6316 tracing_off();
6318 local_irq_save(flags);
6320 /* Simulate the iterator */
6321 trace_init_global_iter(&iter);
6323 for_each_tracing_cpu(cpu) {
6324 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
6327 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
6329 /* don't look at user memory in panic mode */
6330 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
6332 switch (oops_dump_mode) {
6333 case DUMP_ALL:
6334 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6335 break;
6336 case DUMP_ORIG:
6337 iter.cpu_file = raw_smp_processor_id();
6338 break;
6339 case DUMP_NONE:
6340 goto out_enable;
6341 default:
6342 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
6343 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6346 printk(KERN_TRACE "Dumping ftrace buffer:\n");
6348 /* Did function tracer already get disabled? */
6349 if (ftrace_is_dead()) {
6350 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
6351 printk("# MAY BE MISSING FUNCTION EVENTS\n");
6355 * We need to stop all tracing on all CPUS to read the
6356 * the next buffer. This is a bit expensive, but is
6357 * not done often. We fill all what we can read,
6358 * and then release the locks again.
6361 while (!trace_empty(&iter)) {
6363 if (!cnt)
6364 printk(KERN_TRACE "---------------------------------\n");
6366 cnt++;
6368 /* reset all but tr, trace, and overruns */
6369 memset(&iter.seq, 0,
6370 sizeof(struct trace_iterator) -
6371 offsetof(struct trace_iterator, seq));
6372 iter.iter_flags |= TRACE_FILE_LAT_FMT;
6373 iter.pos = -1;
6375 if (trace_find_next_entry_inc(&iter) != NULL) {
6376 int ret;
6378 ret = print_trace_line(&iter);
6379 if (ret != TRACE_TYPE_NO_CONSUME)
6380 trace_consume(&iter);
6382 touch_nmi_watchdog();
6384 trace_printk_seq(&iter.seq);
6387 if (!cnt)
6388 printk(KERN_TRACE " (ftrace buffer empty)\n");
6389 else
6390 printk(KERN_TRACE "---------------------------------\n");
6392 out_enable:
6393 trace_flags |= old_userobj;
6395 for_each_tracing_cpu(cpu) {
6396 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
6398 atomic_dec(&dump_running);
6399 local_irq_restore(flags);
6401 EXPORT_SYMBOL_GPL(ftrace_dump);
6403 __init static int tracer_alloc_buffers(void)
6405 int ring_buf_size;
6406 int ret = -ENOMEM;
6409 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
6410 goto out;
6412 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
6413 goto out_free_buffer_mask;
6415 /* Only allocate trace_printk buffers if a trace_printk exists */
6416 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
6417 /* Must be called before global_trace.buffer is allocated */
6418 trace_printk_init_buffers();
6420 /* To save memory, keep the ring buffer size to its minimum */
6421 if (ring_buffer_expanded)
6422 ring_buf_size = trace_buf_size;
6423 else
6424 ring_buf_size = 1;
6426 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
6427 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
6429 raw_spin_lock_init(&global_trace.start_lock);
6431 /* TODO: make the number of buffers hot pluggable with CPUS */
6432 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
6433 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
6434 WARN_ON(1);
6435 goto out_free_cpumask;
6438 if (global_trace.buffer_disabled)
6439 tracing_off();
6441 trace_init_cmdlines();
6444 * register_tracer() might reference current_trace, so it
6445 * needs to be set before we register anything. This is
6446 * just a bootstrap of current_trace anyway.
6448 global_trace.current_trace = &nop_trace;
6450 register_tracer(&nop_trace);
6452 /* All seems OK, enable tracing */
6453 tracing_disabled = 0;
6455 atomic_notifier_chain_register(&panic_notifier_list,
6456 &trace_panic_notifier);
6458 register_die_notifier(&trace_die_notifier);
6460 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
6462 INIT_LIST_HEAD(&global_trace.systems);
6463 INIT_LIST_HEAD(&global_trace.events);
6464 list_add(&global_trace.list, &ftrace_trace_arrays);
6466 while (trace_boot_options) {
6467 char *option;
6469 option = strsep(&trace_boot_options, ",");
6470 trace_set_options(&global_trace, option);
6473 register_snapshot_cmd();
6475 return 0;
6477 out_free_cpumask:
6478 free_percpu(global_trace.trace_buffer.data);
6479 #ifdef CONFIG_TRACER_MAX_TRACE
6480 free_percpu(global_trace.max_buffer.data);
6481 #endif
6482 free_cpumask_var(global_trace.tracing_cpumask);
6483 out_free_buffer_mask:
6484 free_cpumask_var(tracing_buffer_mask);
6485 out:
6486 return ret;
6489 __init static int clear_boot_tracer(void)
6492 * The default tracer at boot buffer is an init section.
6493 * This function is called in lateinit. If we did not
6494 * find the boot tracer, then clear it out, to prevent
6495 * later registration from accessing the buffer that is
6496 * about to be freed.
6498 if (!default_bootup_tracer)
6499 return 0;
6501 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6502 default_bootup_tracer);
6503 default_bootup_tracer = NULL;
6505 return 0;
6508 early_initcall(tracer_alloc_buffers);
6509 fs_initcall(tracer_init_debugfs);
6510 late_initcall(clear_boot_tracer);