tracing: Make a snapshot feature available from userspace
[linux-2.6.git] / kernel / trace / trace.c
blob70dce64b9ecf12f8f99d605cecf62a8016952059
1 /*
2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 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/irq_work.h>
23 #include <linux/debugfs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/kprobes.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/rwsem.h>
36 #include <linux/slab.h>
37 #include <linux/ctype.h>
38 #include <linux/init.h>
39 #include <linux/poll.h>
40 #include <linux/nmi.h>
41 #include <linux/fs.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 int 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 * When a reader is waiting for data, then this variable is
90 * set to true.
92 static bool trace_wakeup_needed;
94 static struct irq_work trace_work_wakeup;
97 * Kill all tracing for good (never come back).
98 * It is initialized to 1 but will turn to zero if the initialization
99 * of the tracer is successful. But that is the only place that sets
100 * this back to zero.
102 static int tracing_disabled = 1;
104 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
106 cpumask_var_t __read_mostly tracing_buffer_mask;
109 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
111 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
112 * is set, then ftrace_dump is called. This will output the contents
113 * of the ftrace buffers to the console. This is very useful for
114 * capturing traces that lead to crashes and outputing it to a
115 * serial console.
117 * It is default off, but you can enable it with either specifying
118 * "ftrace_dump_on_oops" in the kernel command line, or setting
119 * /proc/sys/kernel/ftrace_dump_on_oops
120 * Set 1 if you want to dump buffers of all CPUs
121 * Set 2 if you want to dump the buffer of the CPU that triggered oops
124 enum ftrace_dump_mode ftrace_dump_on_oops;
126 static int tracing_set_tracer(const char *buf);
128 #define MAX_TRACER_SIZE 100
129 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
130 static char *default_bootup_tracer;
132 static int __init set_cmdline_ftrace(char *str)
134 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
135 default_bootup_tracer = bootup_tracer_buf;
136 /* We are using ftrace early, expand it */
137 ring_buffer_expanded = 1;
138 return 1;
140 __setup("ftrace=", set_cmdline_ftrace);
142 static int __init set_ftrace_dump_on_oops(char *str)
144 if (*str++ != '=' || !*str) {
145 ftrace_dump_on_oops = DUMP_ALL;
146 return 1;
149 if (!strcmp("orig_cpu", str)) {
150 ftrace_dump_on_oops = DUMP_ORIG;
151 return 1;
154 return 0;
156 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
159 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
160 static char *trace_boot_options __initdata;
162 static int __init set_trace_boot_options(char *str)
164 strncpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
165 trace_boot_options = trace_boot_options_buf;
166 return 0;
168 __setup("trace_options=", set_trace_boot_options);
170 unsigned long long ns2usecs(cycle_t nsec)
172 nsec += 500;
173 do_div(nsec, 1000);
174 return nsec;
178 * The global_trace is the descriptor that holds the tracing
179 * buffers for the live tracing. For each CPU, it contains
180 * a link list of pages that will store trace entries. The
181 * page descriptor of the pages in the memory is used to hold
182 * the link list by linking the lru item in the page descriptor
183 * to each of the pages in the buffer per CPU.
185 * For each active CPU there is a data field that holds the
186 * pages for the buffer for that CPU. Each CPU has the same number
187 * of pages allocated for its buffer.
189 static struct trace_array global_trace;
191 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
193 int filter_current_check_discard(struct ring_buffer *buffer,
194 struct ftrace_event_call *call, void *rec,
195 struct ring_buffer_event *event)
197 return filter_check_discard(call, rec, buffer, event);
199 EXPORT_SYMBOL_GPL(filter_current_check_discard);
201 cycle_t ftrace_now(int cpu)
203 u64 ts;
205 /* Early boot up does not have a buffer yet */
206 if (!global_trace.buffer)
207 return trace_clock_local();
209 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
210 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
212 return ts;
216 * The max_tr is used to snapshot the global_trace when a maximum
217 * latency is reached. Some tracers will use this to store a maximum
218 * trace while it continues examining live traces.
220 * The buffers for the max_tr are set up the same as the global_trace.
221 * When a snapshot is taken, the link list of the max_tr is swapped
222 * with the link list of the global_trace and the buffers are reset for
223 * the global_trace so the tracing can continue.
225 static struct trace_array max_tr;
227 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
229 int tracing_is_enabled(void)
231 return tracing_is_on();
235 * trace_buf_size is the size in bytes that is allocated
236 * for a buffer. Note, the number of bytes is always rounded
237 * to page size.
239 * This number is purposely set to a low number of 16384.
240 * If the dump on oops happens, it will be much appreciated
241 * to not have to wait for all that output. Anyway this can be
242 * boot time and run time configurable.
244 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
246 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
248 /* trace_types holds a link list of available tracers. */
249 static struct tracer *trace_types __read_mostly;
251 /* current_trace points to the tracer that is currently active */
252 static struct tracer *current_trace __read_mostly;
255 * trace_types_lock is used to protect the trace_types list.
257 static DEFINE_MUTEX(trace_types_lock);
260 * serialize the access of the ring buffer
262 * ring buffer serializes readers, but it is low level protection.
263 * The validity of the events (which returns by ring_buffer_peek() ..etc)
264 * are not protected by ring buffer.
266 * The content of events may become garbage if we allow other process consumes
267 * these events concurrently:
268 * A) the page of the consumed events may become a normal page
269 * (not reader page) in ring buffer, and this page will be rewrited
270 * by events producer.
271 * B) The page of the consumed events may become a page for splice_read,
272 * and this page will be returned to system.
274 * These primitives allow multi process access to different cpu ring buffer
275 * concurrently.
277 * These primitives don't distinguish read-only and read-consume access.
278 * Multi read-only access are also serialized.
281 #ifdef CONFIG_SMP
282 static DECLARE_RWSEM(all_cpu_access_lock);
283 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
285 static inline void trace_access_lock(int cpu)
287 if (cpu == TRACE_PIPE_ALL_CPU) {
288 /* gain it for accessing the whole ring buffer. */
289 down_write(&all_cpu_access_lock);
290 } else {
291 /* gain it for accessing a cpu ring buffer. */
293 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
294 down_read(&all_cpu_access_lock);
296 /* Secondly block other access to this @cpu ring buffer. */
297 mutex_lock(&per_cpu(cpu_access_lock, cpu));
301 static inline void trace_access_unlock(int cpu)
303 if (cpu == TRACE_PIPE_ALL_CPU) {
304 up_write(&all_cpu_access_lock);
305 } else {
306 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
307 up_read(&all_cpu_access_lock);
311 static inline void trace_access_lock_init(void)
313 int cpu;
315 for_each_possible_cpu(cpu)
316 mutex_init(&per_cpu(cpu_access_lock, cpu));
319 #else
321 static DEFINE_MUTEX(access_lock);
323 static inline void trace_access_lock(int cpu)
325 (void)cpu;
326 mutex_lock(&access_lock);
329 static inline void trace_access_unlock(int cpu)
331 (void)cpu;
332 mutex_unlock(&access_lock);
335 static inline void trace_access_lock_init(void)
339 #endif
341 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
342 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
344 /* trace_flags holds trace_options default values */
345 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
346 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
347 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
348 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS;
350 static int trace_stop_count;
351 static DEFINE_RAW_SPINLOCK(tracing_start_lock);
354 * trace_wake_up - wake up tasks waiting for trace input
356 * Schedules a delayed work to wake up any task that is blocked on the
357 * trace_wait queue. These is used with trace_poll for tasks polling the
358 * trace.
360 static void trace_wake_up(struct irq_work *work)
362 wake_up_all(&trace_wait);
367 * tracing_on - enable tracing buffers
369 * This function enables tracing buffers that may have been
370 * disabled with tracing_off.
372 void tracing_on(void)
374 if (global_trace.buffer)
375 ring_buffer_record_on(global_trace.buffer);
377 * This flag is only looked at when buffers haven't been
378 * allocated yet. We don't really care about the race
379 * between setting this flag and actually turning
380 * on the buffer.
382 global_trace.buffer_disabled = 0;
384 EXPORT_SYMBOL_GPL(tracing_on);
387 * tracing_off - turn off tracing buffers
389 * This function stops the tracing buffers from recording data.
390 * It does not disable any overhead the tracers themselves may
391 * be causing. This function simply causes all recording to
392 * the ring buffers to fail.
394 void tracing_off(void)
396 if (global_trace.buffer)
397 ring_buffer_record_off(global_trace.buffer);
399 * This flag is only looked at when buffers haven't been
400 * allocated yet. We don't really care about the race
401 * between setting this flag and actually turning
402 * on the buffer.
404 global_trace.buffer_disabled = 1;
406 EXPORT_SYMBOL_GPL(tracing_off);
409 * tracing_is_on - show state of ring buffers enabled
411 int tracing_is_on(void)
413 if (global_trace.buffer)
414 return ring_buffer_record_is_on(global_trace.buffer);
415 return !global_trace.buffer_disabled;
417 EXPORT_SYMBOL_GPL(tracing_is_on);
419 static int __init set_buf_size(char *str)
421 unsigned long buf_size;
423 if (!str)
424 return 0;
425 buf_size = memparse(str, &str);
426 /* nr_entries can not be zero */
427 if (buf_size == 0)
428 return 0;
429 trace_buf_size = buf_size;
430 return 1;
432 __setup("trace_buf_size=", set_buf_size);
434 static int __init set_tracing_thresh(char *str)
436 unsigned long threshold;
437 int ret;
439 if (!str)
440 return 0;
441 ret = kstrtoul(str, 0, &threshold);
442 if (ret < 0)
443 return 0;
444 tracing_thresh = threshold * 1000;
445 return 1;
447 __setup("tracing_thresh=", set_tracing_thresh);
449 unsigned long nsecs_to_usecs(unsigned long nsecs)
451 return nsecs / 1000;
454 /* These must match the bit postions in trace_iterator_flags */
455 static const char *trace_options[] = {
456 "print-parent",
457 "sym-offset",
458 "sym-addr",
459 "verbose",
460 "raw",
461 "hex",
462 "bin",
463 "block",
464 "stacktrace",
465 "trace_printk",
466 "ftrace_preempt",
467 "branch",
468 "annotate",
469 "userstacktrace",
470 "sym-userobj",
471 "printk-msg-only",
472 "context-info",
473 "latency-format",
474 "sleep-time",
475 "graph-time",
476 "record-cmd",
477 "overwrite",
478 "disable_on_free",
479 "irq-info",
480 "markers",
481 NULL
484 static struct {
485 u64 (*func)(void);
486 const char *name;
487 int in_ns; /* is this clock in nanoseconds? */
488 } trace_clocks[] = {
489 { trace_clock_local, "local", 1 },
490 { trace_clock_global, "global", 1 },
491 { trace_clock_counter, "counter", 0 },
492 ARCH_TRACE_CLOCKS
495 int trace_clock_id;
498 * trace_parser_get_init - gets the buffer for trace parser
500 int trace_parser_get_init(struct trace_parser *parser, int size)
502 memset(parser, 0, sizeof(*parser));
504 parser->buffer = kmalloc(size, GFP_KERNEL);
505 if (!parser->buffer)
506 return 1;
508 parser->size = size;
509 return 0;
513 * trace_parser_put - frees the buffer for trace parser
515 void trace_parser_put(struct trace_parser *parser)
517 kfree(parser->buffer);
521 * trace_get_user - reads the user input string separated by space
522 * (matched by isspace(ch))
524 * For each string found the 'struct trace_parser' is updated,
525 * and the function returns.
527 * Returns number of bytes read.
529 * See kernel/trace/trace.h for 'struct trace_parser' details.
531 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
532 size_t cnt, loff_t *ppos)
534 char ch;
535 size_t read = 0;
536 ssize_t ret;
538 if (!*ppos)
539 trace_parser_clear(parser);
541 ret = get_user(ch, ubuf++);
542 if (ret)
543 goto out;
545 read++;
546 cnt--;
549 * The parser is not finished with the last write,
550 * continue reading the user input without skipping spaces.
552 if (!parser->cont) {
553 /* skip white space */
554 while (cnt && isspace(ch)) {
555 ret = get_user(ch, ubuf++);
556 if (ret)
557 goto out;
558 read++;
559 cnt--;
562 /* only spaces were written */
563 if (isspace(ch)) {
564 *ppos += read;
565 ret = read;
566 goto out;
569 parser->idx = 0;
572 /* read the non-space input */
573 while (cnt && !isspace(ch)) {
574 if (parser->idx < parser->size - 1)
575 parser->buffer[parser->idx++] = ch;
576 else {
577 ret = -EINVAL;
578 goto out;
580 ret = get_user(ch, ubuf++);
581 if (ret)
582 goto out;
583 read++;
584 cnt--;
587 /* We either got finished input or we have to wait for another call. */
588 if (isspace(ch)) {
589 parser->buffer[parser->idx] = 0;
590 parser->cont = false;
591 } else {
592 parser->cont = true;
593 parser->buffer[parser->idx++] = ch;
596 *ppos += read;
597 ret = read;
599 out:
600 return ret;
603 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
605 int len;
606 int ret;
608 if (!cnt)
609 return 0;
611 if (s->len <= s->readpos)
612 return -EBUSY;
614 len = s->len - s->readpos;
615 if (cnt > len)
616 cnt = len;
617 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
618 if (ret == cnt)
619 return -EFAULT;
621 cnt -= ret;
623 s->readpos += cnt;
624 return cnt;
627 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
629 int len;
631 if (s->len <= s->readpos)
632 return -EBUSY;
634 len = s->len - s->readpos;
635 if (cnt > len)
636 cnt = len;
637 memcpy(buf, s->buffer + s->readpos, cnt);
639 s->readpos += cnt;
640 return cnt;
644 * ftrace_max_lock is used to protect the swapping of buffers
645 * when taking a max snapshot. The buffers themselves are
646 * protected by per_cpu spinlocks. But the action of the swap
647 * needs its own lock.
649 * This is defined as a arch_spinlock_t in order to help
650 * with performance when lockdep debugging is enabled.
652 * It is also used in other places outside the update_max_tr
653 * so it needs to be defined outside of the
654 * CONFIG_TRACER_MAX_TRACE.
656 static arch_spinlock_t ftrace_max_lock =
657 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
659 unsigned long __read_mostly tracing_thresh;
661 #ifdef CONFIG_TRACER_MAX_TRACE
662 unsigned long __read_mostly tracing_max_latency;
665 * Copy the new maximum trace into the separate maximum-trace
666 * structure. (this way the maximum trace is permanently saved,
667 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
669 static void
670 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
672 struct trace_array_cpu *data = tr->data[cpu];
673 struct trace_array_cpu *max_data;
675 max_tr.cpu = cpu;
676 max_tr.time_start = data->preempt_timestamp;
678 max_data = max_tr.data[cpu];
679 max_data->saved_latency = tracing_max_latency;
680 max_data->critical_start = data->critical_start;
681 max_data->critical_end = data->critical_end;
683 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
684 max_data->pid = tsk->pid;
685 max_data->uid = task_uid(tsk);
686 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
687 max_data->policy = tsk->policy;
688 max_data->rt_priority = tsk->rt_priority;
690 /* record this tasks comm */
691 tracing_record_cmdline(tsk);
695 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
696 * @tr: tracer
697 * @tsk: the task with the latency
698 * @cpu: The cpu that initiated the trace.
700 * Flip the buffers between the @tr and the max_tr and record information
701 * about which task was the cause of this latency.
703 void
704 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
706 struct ring_buffer *buf = tr->buffer;
708 if (trace_stop_count)
709 return;
711 WARN_ON_ONCE(!irqs_disabled());
713 if (!current_trace->allocated_snapshot) {
714 /* Only the nop tracer should hit this when disabling */
715 WARN_ON_ONCE(current_trace != &nop_trace);
716 return;
719 arch_spin_lock(&ftrace_max_lock);
721 tr->buffer = max_tr.buffer;
722 max_tr.buffer = buf;
724 __update_max_tr(tr, tsk, cpu);
725 arch_spin_unlock(&ftrace_max_lock);
729 * update_max_tr_single - only copy one trace over, and reset the rest
730 * @tr - tracer
731 * @tsk - task with the latency
732 * @cpu - the cpu of the buffer to copy.
734 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
736 void
737 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
739 int ret;
741 if (trace_stop_count)
742 return;
744 WARN_ON_ONCE(!irqs_disabled());
745 if (WARN_ON_ONCE(!current_trace->allocated_snapshot))
746 return;
748 arch_spin_lock(&ftrace_max_lock);
750 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
752 if (ret == -EBUSY) {
754 * We failed to swap the buffer due to a commit taking
755 * place on this CPU. We fail to record, but we reset
756 * the max trace buffer (no one writes directly to it)
757 * and flag that it failed.
759 trace_array_printk(&max_tr, _THIS_IP_,
760 "Failed to swap buffers due to commit in progress\n");
763 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
765 __update_max_tr(tr, tsk, cpu);
766 arch_spin_unlock(&ftrace_max_lock);
768 #endif /* CONFIG_TRACER_MAX_TRACE */
770 static void default_wait_pipe(struct trace_iterator *iter)
772 DEFINE_WAIT(wait);
774 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
777 * The events can happen in critical sections where
778 * checking a work queue can cause deadlocks.
779 * After adding a task to the queue, this flag is set
780 * only to notify events to try to wake up the queue
781 * using irq_work.
783 * We don't clear it even if the buffer is no longer
784 * empty. The flag only causes the next event to run
785 * irq_work to do the work queue wake up. The worse
786 * that can happen if we race with !trace_empty() is that
787 * an event will cause an irq_work to try to wake up
788 * an empty queue.
790 * There's no reason to protect this flag either, as
791 * the work queue and irq_work logic will do the necessary
792 * synchronization for the wake ups. The only thing
793 * that is necessary is that the wake up happens after
794 * a task has been queued. It's OK for spurious wake ups.
796 trace_wakeup_needed = true;
798 if (trace_empty(iter))
799 schedule();
801 finish_wait(&trace_wait, &wait);
805 * register_tracer - register a tracer with the ftrace system.
806 * @type - the plugin for the tracer
808 * Register a new plugin tracer.
810 int register_tracer(struct tracer *type)
812 struct tracer *t;
813 int ret = 0;
815 if (!type->name) {
816 pr_info("Tracer must have a name\n");
817 return -1;
820 if (strlen(type->name) >= MAX_TRACER_SIZE) {
821 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
822 return -1;
825 mutex_lock(&trace_types_lock);
827 tracing_selftest_running = true;
829 for (t = trace_types; t; t = t->next) {
830 if (strcmp(type->name, t->name) == 0) {
831 /* already found */
832 pr_info("Tracer %s already registered\n",
833 type->name);
834 ret = -1;
835 goto out;
839 if (!type->set_flag)
840 type->set_flag = &dummy_set_flag;
841 if (!type->flags)
842 type->flags = &dummy_tracer_flags;
843 else
844 if (!type->flags->opts)
845 type->flags->opts = dummy_tracer_opt;
846 if (!type->wait_pipe)
847 type->wait_pipe = default_wait_pipe;
850 #ifdef CONFIG_FTRACE_STARTUP_TEST
851 if (type->selftest && !tracing_selftest_disabled) {
852 struct tracer *saved_tracer = current_trace;
853 struct trace_array *tr = &global_trace;
856 * Run a selftest on this tracer.
857 * Here we reset the trace buffer, and set the current
858 * tracer to be this tracer. The tracer can then run some
859 * internal tracing to verify that everything is in order.
860 * If we fail, we do not register this tracer.
862 tracing_reset_online_cpus(tr);
864 current_trace = type;
866 if (type->use_max_tr) {
867 /* If we expanded the buffers, make sure the max is expanded too */
868 if (ring_buffer_expanded)
869 ring_buffer_resize(max_tr.buffer, trace_buf_size,
870 RING_BUFFER_ALL_CPUS);
871 type->allocated_snapshot = true;
874 /* the test is responsible for initializing and enabling */
875 pr_info("Testing tracer %s: ", type->name);
876 ret = type->selftest(type, tr);
877 /* the test is responsible for resetting too */
878 current_trace = saved_tracer;
879 if (ret) {
880 printk(KERN_CONT "FAILED!\n");
881 /* Add the warning after printing 'FAILED' */
882 WARN_ON(1);
883 goto out;
885 /* Only reset on passing, to avoid touching corrupted buffers */
886 tracing_reset_online_cpus(tr);
888 if (type->use_max_tr) {
889 type->allocated_snapshot = false;
891 /* Shrink the max buffer again */
892 if (ring_buffer_expanded)
893 ring_buffer_resize(max_tr.buffer, 1,
894 RING_BUFFER_ALL_CPUS);
897 printk(KERN_CONT "PASSED\n");
899 #endif
901 type->next = trace_types;
902 trace_types = type;
904 out:
905 tracing_selftest_running = false;
906 mutex_unlock(&trace_types_lock);
908 if (ret || !default_bootup_tracer)
909 goto out_unlock;
911 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
912 goto out_unlock;
914 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
915 /* Do we want this tracer to start on bootup? */
916 tracing_set_tracer(type->name);
917 default_bootup_tracer = NULL;
918 /* disable other selftests, since this will break it. */
919 tracing_selftest_disabled = 1;
920 #ifdef CONFIG_FTRACE_STARTUP_TEST
921 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
922 type->name);
923 #endif
925 out_unlock:
926 return ret;
929 void tracing_reset(struct trace_array *tr, int cpu)
931 struct ring_buffer *buffer = tr->buffer;
933 if (!buffer)
934 return;
936 ring_buffer_record_disable(buffer);
938 /* Make sure all commits have finished */
939 synchronize_sched();
940 ring_buffer_reset_cpu(buffer, cpu);
942 ring_buffer_record_enable(buffer);
945 void tracing_reset_online_cpus(struct trace_array *tr)
947 struct ring_buffer *buffer = tr->buffer;
948 int cpu;
950 if (!buffer)
951 return;
953 ring_buffer_record_disable(buffer);
955 /* Make sure all commits have finished */
956 synchronize_sched();
958 tr->time_start = ftrace_now(tr->cpu);
960 for_each_online_cpu(cpu)
961 ring_buffer_reset_cpu(buffer, cpu);
963 ring_buffer_record_enable(buffer);
966 void tracing_reset_current(int cpu)
968 tracing_reset(&global_trace, cpu);
971 void tracing_reset_current_online_cpus(void)
973 tracing_reset_online_cpus(&global_trace);
976 #define SAVED_CMDLINES 128
977 #define NO_CMDLINE_MAP UINT_MAX
978 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
979 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
980 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
981 static int cmdline_idx;
982 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
984 /* temporary disable recording */
985 static atomic_t trace_record_cmdline_disabled __read_mostly;
987 static void trace_init_cmdlines(void)
989 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
990 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
991 cmdline_idx = 0;
994 int is_tracing_stopped(void)
996 return trace_stop_count;
1000 * ftrace_off_permanent - disable all ftrace code permanently
1002 * This should only be called when a serious anomally has
1003 * been detected. This will turn off the function tracing,
1004 * ring buffers, and other tracing utilites. It takes no
1005 * locks and can be called from any context.
1007 void ftrace_off_permanent(void)
1009 tracing_disabled = 1;
1010 ftrace_stop();
1011 tracing_off_permanent();
1015 * tracing_start - quick start of the tracer
1017 * If tracing is enabled but was stopped by tracing_stop,
1018 * this will start the tracer back up.
1020 void tracing_start(void)
1022 struct ring_buffer *buffer;
1023 unsigned long flags;
1025 if (tracing_disabled)
1026 return;
1028 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1029 if (--trace_stop_count) {
1030 if (trace_stop_count < 0) {
1031 /* Someone screwed up their debugging */
1032 WARN_ON_ONCE(1);
1033 trace_stop_count = 0;
1035 goto out;
1038 /* Prevent the buffers from switching */
1039 arch_spin_lock(&ftrace_max_lock);
1041 buffer = global_trace.buffer;
1042 if (buffer)
1043 ring_buffer_record_enable(buffer);
1045 buffer = max_tr.buffer;
1046 if (buffer)
1047 ring_buffer_record_enable(buffer);
1049 arch_spin_unlock(&ftrace_max_lock);
1051 ftrace_start();
1052 out:
1053 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1057 * tracing_stop - quick stop of the tracer
1059 * Light weight way to stop tracing. Use in conjunction with
1060 * tracing_start.
1062 void tracing_stop(void)
1064 struct ring_buffer *buffer;
1065 unsigned long flags;
1067 ftrace_stop();
1068 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1069 if (trace_stop_count++)
1070 goto out;
1072 /* Prevent the buffers from switching */
1073 arch_spin_lock(&ftrace_max_lock);
1075 buffer = global_trace.buffer;
1076 if (buffer)
1077 ring_buffer_record_disable(buffer);
1079 buffer = max_tr.buffer;
1080 if (buffer)
1081 ring_buffer_record_disable(buffer);
1083 arch_spin_unlock(&ftrace_max_lock);
1085 out:
1086 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1089 void trace_stop_cmdline_recording(void);
1091 static void trace_save_cmdline(struct task_struct *tsk)
1093 unsigned pid, idx;
1095 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1096 return;
1099 * It's not the end of the world if we don't get
1100 * the lock, but we also don't want to spin
1101 * nor do we want to disable interrupts,
1102 * so if we miss here, then better luck next time.
1104 if (!arch_spin_trylock(&trace_cmdline_lock))
1105 return;
1107 idx = map_pid_to_cmdline[tsk->pid];
1108 if (idx == NO_CMDLINE_MAP) {
1109 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1112 * Check whether the cmdline buffer at idx has a pid
1113 * mapped. We are going to overwrite that entry so we
1114 * need to clear the map_pid_to_cmdline. Otherwise we
1115 * would read the new comm for the old pid.
1117 pid = map_cmdline_to_pid[idx];
1118 if (pid != NO_CMDLINE_MAP)
1119 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1121 map_cmdline_to_pid[idx] = tsk->pid;
1122 map_pid_to_cmdline[tsk->pid] = idx;
1124 cmdline_idx = idx;
1127 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1129 arch_spin_unlock(&trace_cmdline_lock);
1132 void trace_find_cmdline(int pid, char comm[])
1134 unsigned map;
1136 if (!pid) {
1137 strcpy(comm, "<idle>");
1138 return;
1141 if (WARN_ON_ONCE(pid < 0)) {
1142 strcpy(comm, "<XXX>");
1143 return;
1146 if (pid > PID_MAX_DEFAULT) {
1147 strcpy(comm, "<...>");
1148 return;
1151 preempt_disable();
1152 arch_spin_lock(&trace_cmdline_lock);
1153 map = map_pid_to_cmdline[pid];
1154 if (map != NO_CMDLINE_MAP)
1155 strcpy(comm, saved_cmdlines[map]);
1156 else
1157 strcpy(comm, "<...>");
1159 arch_spin_unlock(&trace_cmdline_lock);
1160 preempt_enable();
1163 void tracing_record_cmdline(struct task_struct *tsk)
1165 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1166 return;
1168 if (!__this_cpu_read(trace_cmdline_save))
1169 return;
1171 __this_cpu_write(trace_cmdline_save, false);
1173 trace_save_cmdline(tsk);
1176 void
1177 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1178 int pc)
1180 struct task_struct *tsk = current;
1182 entry->preempt_count = pc & 0xff;
1183 entry->pid = (tsk) ? tsk->pid : 0;
1184 entry->flags =
1185 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1186 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1187 #else
1188 TRACE_FLAG_IRQS_NOSUPPORT |
1189 #endif
1190 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1191 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1192 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1194 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1196 struct ring_buffer_event *
1197 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1198 int type,
1199 unsigned long len,
1200 unsigned long flags, int pc)
1202 struct ring_buffer_event *event;
1204 event = ring_buffer_lock_reserve(buffer, len);
1205 if (event != NULL) {
1206 struct trace_entry *ent = ring_buffer_event_data(event);
1208 tracing_generic_entry_update(ent, flags, pc);
1209 ent->type = type;
1212 return event;
1215 void
1216 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1218 __this_cpu_write(trace_cmdline_save, true);
1219 if (trace_wakeup_needed) {
1220 trace_wakeup_needed = false;
1221 /* irq_work_queue() supplies it's own memory barriers */
1222 irq_work_queue(&trace_work_wakeup);
1224 ring_buffer_unlock_commit(buffer, event);
1227 static inline void
1228 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1229 struct ring_buffer_event *event,
1230 unsigned long flags, int pc)
1232 __buffer_unlock_commit(buffer, event);
1234 ftrace_trace_stack(buffer, flags, 6, pc);
1235 ftrace_trace_userstack(buffer, flags, pc);
1238 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1239 struct ring_buffer_event *event,
1240 unsigned long flags, int pc)
1242 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1244 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1246 struct ring_buffer_event *
1247 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1248 int type, unsigned long len,
1249 unsigned long flags, int pc)
1251 *current_rb = global_trace.buffer;
1252 return trace_buffer_lock_reserve(*current_rb,
1253 type, len, flags, pc);
1255 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1257 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1258 struct ring_buffer_event *event,
1259 unsigned long flags, int pc)
1261 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1263 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1265 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1266 struct ring_buffer_event *event,
1267 unsigned long flags, int pc,
1268 struct pt_regs *regs)
1270 __buffer_unlock_commit(buffer, event);
1272 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1273 ftrace_trace_userstack(buffer, flags, pc);
1275 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1277 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1278 struct ring_buffer_event *event)
1280 ring_buffer_discard_commit(buffer, event);
1282 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1284 void
1285 trace_function(struct trace_array *tr,
1286 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1287 int pc)
1289 struct ftrace_event_call *call = &event_function;
1290 struct ring_buffer *buffer = tr->buffer;
1291 struct ring_buffer_event *event;
1292 struct ftrace_entry *entry;
1294 /* If we are reading the ring buffer, don't trace */
1295 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1296 return;
1298 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1299 flags, pc);
1300 if (!event)
1301 return;
1302 entry = ring_buffer_event_data(event);
1303 entry->ip = ip;
1304 entry->parent_ip = parent_ip;
1306 if (!filter_check_discard(call, entry, buffer, event))
1307 __buffer_unlock_commit(buffer, event);
1310 void
1311 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1312 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1313 int pc)
1315 if (likely(!atomic_read(&data->disabled)))
1316 trace_function(tr, ip, parent_ip, flags, pc);
1319 #ifdef CONFIG_STACKTRACE
1321 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1322 struct ftrace_stack {
1323 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1326 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1327 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1329 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1330 unsigned long flags,
1331 int skip, int pc, struct pt_regs *regs)
1333 struct ftrace_event_call *call = &event_kernel_stack;
1334 struct ring_buffer_event *event;
1335 struct stack_entry *entry;
1336 struct stack_trace trace;
1337 int use_stack;
1338 int size = FTRACE_STACK_ENTRIES;
1340 trace.nr_entries = 0;
1341 trace.skip = skip;
1344 * Since events can happen in NMIs there's no safe way to
1345 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1346 * or NMI comes in, it will just have to use the default
1347 * FTRACE_STACK_SIZE.
1349 preempt_disable_notrace();
1351 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1353 * We don't need any atomic variables, just a barrier.
1354 * If an interrupt comes in, we don't care, because it would
1355 * have exited and put the counter back to what we want.
1356 * We just need a barrier to keep gcc from moving things
1357 * around.
1359 barrier();
1360 if (use_stack == 1) {
1361 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1362 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1364 if (regs)
1365 save_stack_trace_regs(regs, &trace);
1366 else
1367 save_stack_trace(&trace);
1369 if (trace.nr_entries > size)
1370 size = trace.nr_entries;
1371 } else
1372 /* From now on, use_stack is a boolean */
1373 use_stack = 0;
1375 size *= sizeof(unsigned long);
1377 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1378 sizeof(*entry) + size, flags, pc);
1379 if (!event)
1380 goto out;
1381 entry = ring_buffer_event_data(event);
1383 memset(&entry->caller, 0, size);
1385 if (use_stack)
1386 memcpy(&entry->caller, trace.entries,
1387 trace.nr_entries * sizeof(unsigned long));
1388 else {
1389 trace.max_entries = FTRACE_STACK_ENTRIES;
1390 trace.entries = entry->caller;
1391 if (regs)
1392 save_stack_trace_regs(regs, &trace);
1393 else
1394 save_stack_trace(&trace);
1397 entry->size = trace.nr_entries;
1399 if (!filter_check_discard(call, entry, buffer, event))
1400 __buffer_unlock_commit(buffer, event);
1402 out:
1403 /* Again, don't let gcc optimize things here */
1404 barrier();
1405 __this_cpu_dec(ftrace_stack_reserve);
1406 preempt_enable_notrace();
1410 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1411 int skip, int pc, struct pt_regs *regs)
1413 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1414 return;
1416 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1419 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1420 int skip, int pc)
1422 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1423 return;
1425 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1428 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1429 int pc)
1431 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
1435 * trace_dump_stack - record a stack back trace in the trace buffer
1437 void trace_dump_stack(void)
1439 unsigned long flags;
1441 if (tracing_disabled || tracing_selftest_running)
1442 return;
1444 local_save_flags(flags);
1446 /* skipping 3 traces, seems to get us at the caller of this function */
1447 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
1450 static DEFINE_PER_CPU(int, user_stack_count);
1452 void
1453 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1455 struct ftrace_event_call *call = &event_user_stack;
1456 struct ring_buffer_event *event;
1457 struct userstack_entry *entry;
1458 struct stack_trace trace;
1460 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1461 return;
1464 * NMIs can not handle page faults, even with fix ups.
1465 * The save user stack can (and often does) fault.
1467 if (unlikely(in_nmi()))
1468 return;
1471 * prevent recursion, since the user stack tracing may
1472 * trigger other kernel events.
1474 preempt_disable();
1475 if (__this_cpu_read(user_stack_count))
1476 goto out;
1478 __this_cpu_inc(user_stack_count);
1480 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1481 sizeof(*entry), flags, pc);
1482 if (!event)
1483 goto out_drop_count;
1484 entry = ring_buffer_event_data(event);
1486 entry->tgid = current->tgid;
1487 memset(&entry->caller, 0, sizeof(entry->caller));
1489 trace.nr_entries = 0;
1490 trace.max_entries = FTRACE_STACK_ENTRIES;
1491 trace.skip = 0;
1492 trace.entries = entry->caller;
1494 save_stack_trace_user(&trace);
1495 if (!filter_check_discard(call, entry, buffer, event))
1496 __buffer_unlock_commit(buffer, event);
1498 out_drop_count:
1499 __this_cpu_dec(user_stack_count);
1500 out:
1501 preempt_enable();
1504 #ifdef UNUSED
1505 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1507 ftrace_trace_userstack(tr, flags, preempt_count());
1509 #endif /* UNUSED */
1511 #endif /* CONFIG_STACKTRACE */
1513 /* created for use with alloc_percpu */
1514 struct trace_buffer_struct {
1515 char buffer[TRACE_BUF_SIZE];
1518 static struct trace_buffer_struct *trace_percpu_buffer;
1519 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1520 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1521 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1524 * The buffer used is dependent on the context. There is a per cpu
1525 * buffer for normal context, softirq contex, hard irq context and
1526 * for NMI context. Thise allows for lockless recording.
1528 * Note, if the buffers failed to be allocated, then this returns NULL
1530 static char *get_trace_buf(void)
1532 struct trace_buffer_struct *percpu_buffer;
1535 * If we have allocated per cpu buffers, then we do not
1536 * need to do any locking.
1538 if (in_nmi())
1539 percpu_buffer = trace_percpu_nmi_buffer;
1540 else if (in_irq())
1541 percpu_buffer = trace_percpu_irq_buffer;
1542 else if (in_softirq())
1543 percpu_buffer = trace_percpu_sirq_buffer;
1544 else
1545 percpu_buffer = trace_percpu_buffer;
1547 if (!percpu_buffer)
1548 return NULL;
1550 return this_cpu_ptr(&percpu_buffer->buffer[0]);
1553 static int alloc_percpu_trace_buffer(void)
1555 struct trace_buffer_struct *buffers;
1556 struct trace_buffer_struct *sirq_buffers;
1557 struct trace_buffer_struct *irq_buffers;
1558 struct trace_buffer_struct *nmi_buffers;
1560 buffers = alloc_percpu(struct trace_buffer_struct);
1561 if (!buffers)
1562 goto err_warn;
1564 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1565 if (!sirq_buffers)
1566 goto err_sirq;
1568 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1569 if (!irq_buffers)
1570 goto err_irq;
1572 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1573 if (!nmi_buffers)
1574 goto err_nmi;
1576 trace_percpu_buffer = buffers;
1577 trace_percpu_sirq_buffer = sirq_buffers;
1578 trace_percpu_irq_buffer = irq_buffers;
1579 trace_percpu_nmi_buffer = nmi_buffers;
1581 return 0;
1583 err_nmi:
1584 free_percpu(irq_buffers);
1585 err_irq:
1586 free_percpu(sirq_buffers);
1587 err_sirq:
1588 free_percpu(buffers);
1589 err_warn:
1590 WARN(1, "Could not allocate percpu trace_printk buffer");
1591 return -ENOMEM;
1594 static int buffers_allocated;
1596 void trace_printk_init_buffers(void)
1598 if (buffers_allocated)
1599 return;
1601 if (alloc_percpu_trace_buffer())
1602 return;
1604 pr_info("ftrace: Allocated trace_printk buffers\n");
1606 /* Expand the buffers to set size */
1607 tracing_update_buffers();
1609 buffers_allocated = 1;
1612 * trace_printk_init_buffers() can be called by modules.
1613 * If that happens, then we need to start cmdline recording
1614 * directly here. If the global_trace.buffer is already
1615 * allocated here, then this was called by module code.
1617 if (global_trace.buffer)
1618 tracing_start_cmdline_record();
1621 void trace_printk_start_comm(void)
1623 /* Start tracing comms if trace printk is set */
1624 if (!buffers_allocated)
1625 return;
1626 tracing_start_cmdline_record();
1629 static void trace_printk_start_stop_comm(int enabled)
1631 if (!buffers_allocated)
1632 return;
1634 if (enabled)
1635 tracing_start_cmdline_record();
1636 else
1637 tracing_stop_cmdline_record();
1641 * trace_vbprintk - write binary msg to tracing buffer
1644 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1646 struct ftrace_event_call *call = &event_bprint;
1647 struct ring_buffer_event *event;
1648 struct ring_buffer *buffer;
1649 struct trace_array *tr = &global_trace;
1650 struct bprint_entry *entry;
1651 unsigned long flags;
1652 char *tbuffer;
1653 int len = 0, size, pc;
1655 if (unlikely(tracing_selftest_running || tracing_disabled))
1656 return 0;
1658 /* Don't pollute graph traces with trace_vprintk internals */
1659 pause_graph_tracing();
1661 pc = preempt_count();
1662 preempt_disable_notrace();
1664 tbuffer = get_trace_buf();
1665 if (!tbuffer) {
1666 len = 0;
1667 goto out;
1670 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1672 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1673 goto out;
1675 local_save_flags(flags);
1676 size = sizeof(*entry) + sizeof(u32) * len;
1677 buffer = tr->buffer;
1678 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1679 flags, pc);
1680 if (!event)
1681 goto out;
1682 entry = ring_buffer_event_data(event);
1683 entry->ip = ip;
1684 entry->fmt = fmt;
1686 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1687 if (!filter_check_discard(call, entry, buffer, event)) {
1688 __buffer_unlock_commit(buffer, event);
1689 ftrace_trace_stack(buffer, flags, 6, pc);
1692 out:
1693 preempt_enable_notrace();
1694 unpause_graph_tracing();
1696 return len;
1698 EXPORT_SYMBOL_GPL(trace_vbprintk);
1700 int trace_array_printk(struct trace_array *tr,
1701 unsigned long ip, const char *fmt, ...)
1703 int ret;
1704 va_list ap;
1706 if (!(trace_flags & TRACE_ITER_PRINTK))
1707 return 0;
1709 va_start(ap, fmt);
1710 ret = trace_array_vprintk(tr, ip, fmt, ap);
1711 va_end(ap);
1712 return ret;
1715 int trace_array_vprintk(struct trace_array *tr,
1716 unsigned long ip, const char *fmt, va_list args)
1718 struct ftrace_event_call *call = &event_print;
1719 struct ring_buffer_event *event;
1720 struct ring_buffer *buffer;
1721 int len = 0, size, pc;
1722 struct print_entry *entry;
1723 unsigned long flags;
1724 char *tbuffer;
1726 if (tracing_disabled || tracing_selftest_running)
1727 return 0;
1729 /* Don't pollute graph traces with trace_vprintk internals */
1730 pause_graph_tracing();
1732 pc = preempt_count();
1733 preempt_disable_notrace();
1736 tbuffer = get_trace_buf();
1737 if (!tbuffer) {
1738 len = 0;
1739 goto out;
1742 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1743 if (len > TRACE_BUF_SIZE)
1744 goto out;
1746 local_save_flags(flags);
1747 size = sizeof(*entry) + len + 1;
1748 buffer = tr->buffer;
1749 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1750 flags, pc);
1751 if (!event)
1752 goto out;
1753 entry = ring_buffer_event_data(event);
1754 entry->ip = ip;
1756 memcpy(&entry->buf, tbuffer, len);
1757 entry->buf[len] = '\0';
1758 if (!filter_check_discard(call, entry, buffer, event)) {
1759 __buffer_unlock_commit(buffer, event);
1760 ftrace_trace_stack(buffer, flags, 6, pc);
1762 out:
1763 preempt_enable_notrace();
1764 unpause_graph_tracing();
1766 return len;
1769 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1771 return trace_array_vprintk(&global_trace, ip, fmt, args);
1773 EXPORT_SYMBOL_GPL(trace_vprintk);
1775 static void trace_iterator_increment(struct trace_iterator *iter)
1777 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
1779 iter->idx++;
1780 if (buf_iter)
1781 ring_buffer_read(buf_iter, NULL);
1784 static struct trace_entry *
1785 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1786 unsigned long *lost_events)
1788 struct ring_buffer_event *event;
1789 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
1791 if (buf_iter)
1792 event = ring_buffer_iter_peek(buf_iter, ts);
1793 else
1794 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1795 lost_events);
1797 if (event) {
1798 iter->ent_size = ring_buffer_event_length(event);
1799 return ring_buffer_event_data(event);
1801 iter->ent_size = 0;
1802 return NULL;
1805 static struct trace_entry *
1806 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1807 unsigned long *missing_events, u64 *ent_ts)
1809 struct ring_buffer *buffer = iter->tr->buffer;
1810 struct trace_entry *ent, *next = NULL;
1811 unsigned long lost_events = 0, next_lost = 0;
1812 int cpu_file = iter->cpu_file;
1813 u64 next_ts = 0, ts;
1814 int next_cpu = -1;
1815 int next_size = 0;
1816 int cpu;
1819 * If we are in a per_cpu trace file, don't bother by iterating over
1820 * all cpu and peek directly.
1822 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1823 if (ring_buffer_empty_cpu(buffer, cpu_file))
1824 return NULL;
1825 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1826 if (ent_cpu)
1827 *ent_cpu = cpu_file;
1829 return ent;
1832 for_each_tracing_cpu(cpu) {
1834 if (ring_buffer_empty_cpu(buffer, cpu))
1835 continue;
1837 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1840 * Pick the entry with the smallest timestamp:
1842 if (ent && (!next || ts < next_ts)) {
1843 next = ent;
1844 next_cpu = cpu;
1845 next_ts = ts;
1846 next_lost = lost_events;
1847 next_size = iter->ent_size;
1851 iter->ent_size = next_size;
1853 if (ent_cpu)
1854 *ent_cpu = next_cpu;
1856 if (ent_ts)
1857 *ent_ts = next_ts;
1859 if (missing_events)
1860 *missing_events = next_lost;
1862 return next;
1865 /* Find the next real entry, without updating the iterator itself */
1866 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1867 int *ent_cpu, u64 *ent_ts)
1869 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1872 /* Find the next real entry, and increment the iterator to the next entry */
1873 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1875 iter->ent = __find_next_entry(iter, &iter->cpu,
1876 &iter->lost_events, &iter->ts);
1878 if (iter->ent)
1879 trace_iterator_increment(iter);
1881 return iter->ent ? iter : NULL;
1884 static void trace_consume(struct trace_iterator *iter)
1886 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1887 &iter->lost_events);
1890 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1892 struct trace_iterator *iter = m->private;
1893 int i = (int)*pos;
1894 void *ent;
1896 WARN_ON_ONCE(iter->leftover);
1898 (*pos)++;
1900 /* can't go backwards */
1901 if (iter->idx > i)
1902 return NULL;
1904 if (iter->idx < 0)
1905 ent = trace_find_next_entry_inc(iter);
1906 else
1907 ent = iter;
1909 while (ent && iter->idx < i)
1910 ent = trace_find_next_entry_inc(iter);
1912 iter->pos = *pos;
1914 return ent;
1917 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1919 struct trace_array *tr = iter->tr;
1920 struct ring_buffer_event *event;
1921 struct ring_buffer_iter *buf_iter;
1922 unsigned long entries = 0;
1923 u64 ts;
1925 tr->data[cpu]->skipped_entries = 0;
1927 buf_iter = trace_buffer_iter(iter, cpu);
1928 if (!buf_iter)
1929 return;
1931 ring_buffer_iter_reset(buf_iter);
1934 * We could have the case with the max latency tracers
1935 * that a reset never took place on a cpu. This is evident
1936 * by the timestamp being before the start of the buffer.
1938 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1939 if (ts >= iter->tr->time_start)
1940 break;
1941 entries++;
1942 ring_buffer_read(buf_iter, NULL);
1945 tr->data[cpu]->skipped_entries = entries;
1949 * The current tracer is copied to avoid a global locking
1950 * all around.
1952 static void *s_start(struct seq_file *m, loff_t *pos)
1954 struct trace_iterator *iter = m->private;
1955 int cpu_file = iter->cpu_file;
1956 void *p = NULL;
1957 loff_t l = 0;
1958 int cpu;
1961 * copy the tracer to avoid using a global lock all around.
1962 * iter->trace is a copy of current_trace, the pointer to the
1963 * name may be used instead of a strcmp(), as iter->trace->name
1964 * will point to the same string as current_trace->name.
1966 mutex_lock(&trace_types_lock);
1967 if (unlikely(current_trace && iter->trace->name != current_trace->name))
1968 *iter->trace = *current_trace;
1969 mutex_unlock(&trace_types_lock);
1971 if (iter->snapshot && iter->trace->use_max_tr)
1972 return ERR_PTR(-EBUSY);
1974 if (!iter->snapshot)
1975 atomic_inc(&trace_record_cmdline_disabled);
1977 if (*pos != iter->pos) {
1978 iter->ent = NULL;
1979 iter->cpu = 0;
1980 iter->idx = -1;
1982 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1983 for_each_tracing_cpu(cpu)
1984 tracing_iter_reset(iter, cpu);
1985 } else
1986 tracing_iter_reset(iter, cpu_file);
1988 iter->leftover = 0;
1989 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1992 } else {
1994 * If we overflowed the seq_file before, then we want
1995 * to just reuse the trace_seq buffer again.
1997 if (iter->leftover)
1998 p = iter;
1999 else {
2000 l = *pos - 1;
2001 p = s_next(m, p, &l);
2005 trace_event_read_lock();
2006 trace_access_lock(cpu_file);
2007 return p;
2010 static void s_stop(struct seq_file *m, void *p)
2012 struct trace_iterator *iter = m->private;
2014 if (iter->snapshot && iter->trace->use_max_tr)
2015 return;
2017 if (!iter->snapshot)
2018 atomic_dec(&trace_record_cmdline_disabled);
2019 trace_access_unlock(iter->cpu_file);
2020 trace_event_read_unlock();
2023 static void
2024 get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *entries)
2026 unsigned long count;
2027 int cpu;
2029 *total = 0;
2030 *entries = 0;
2032 for_each_tracing_cpu(cpu) {
2033 count = ring_buffer_entries_cpu(tr->buffer, cpu);
2035 * If this buffer has skipped entries, then we hold all
2036 * entries for the trace and we need to ignore the
2037 * ones before the time stamp.
2039 if (tr->data[cpu]->skipped_entries) {
2040 count -= tr->data[cpu]->skipped_entries;
2041 /* total is the same as the entries */
2042 *total += count;
2043 } else
2044 *total += count +
2045 ring_buffer_overrun_cpu(tr->buffer, cpu);
2046 *entries += count;
2050 static void print_lat_help_header(struct seq_file *m)
2052 seq_puts(m, "# _------=> CPU# \n");
2053 seq_puts(m, "# / _-----=> irqs-off \n");
2054 seq_puts(m, "# | / _----=> need-resched \n");
2055 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2056 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2057 seq_puts(m, "# |||| / delay \n");
2058 seq_puts(m, "# cmd pid ||||| time | caller \n");
2059 seq_puts(m, "# \\ / ||||| \\ | / \n");
2062 static void print_event_info(struct trace_array *tr, struct seq_file *m)
2064 unsigned long total;
2065 unsigned long entries;
2067 get_total_entries(tr, &total, &entries);
2068 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2069 entries, total, num_online_cpus());
2070 seq_puts(m, "#\n");
2073 static void print_func_help_header(struct trace_array *tr, struct seq_file *m)
2075 print_event_info(tr, m);
2076 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2077 seq_puts(m, "# | | | | |\n");
2080 static void print_func_help_header_irq(struct trace_array *tr, struct seq_file *m)
2082 print_event_info(tr, m);
2083 seq_puts(m, "# _-----=> irqs-off\n");
2084 seq_puts(m, "# / _----=> need-resched\n");
2085 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2086 seq_puts(m, "# || / _--=> preempt-depth\n");
2087 seq_puts(m, "# ||| / delay\n");
2088 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2089 seq_puts(m, "# | | | |||| | |\n");
2092 void
2093 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2095 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2096 struct trace_array *tr = iter->tr;
2097 struct trace_array_cpu *data = tr->data[tr->cpu];
2098 struct tracer *type = current_trace;
2099 unsigned long entries;
2100 unsigned long total;
2101 const char *name = "preemption";
2103 if (type)
2104 name = type->name;
2106 get_total_entries(tr, &total, &entries);
2108 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2109 name, UTS_RELEASE);
2110 seq_puts(m, "# -----------------------------------"
2111 "---------------------------------\n");
2112 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2113 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2114 nsecs_to_usecs(data->saved_latency),
2115 entries,
2116 total,
2117 tr->cpu,
2118 #if defined(CONFIG_PREEMPT_NONE)
2119 "server",
2120 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2121 "desktop",
2122 #elif defined(CONFIG_PREEMPT)
2123 "preempt",
2124 #else
2125 "unknown",
2126 #endif
2127 /* These are reserved for later use */
2128 0, 0, 0, 0);
2129 #ifdef CONFIG_SMP
2130 seq_printf(m, " #P:%d)\n", num_online_cpus());
2131 #else
2132 seq_puts(m, ")\n");
2133 #endif
2134 seq_puts(m, "# -----------------\n");
2135 seq_printf(m, "# | task: %.16s-%d "
2136 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2137 data->comm, data->pid,
2138 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2139 data->policy, data->rt_priority);
2140 seq_puts(m, "# -----------------\n");
2142 if (data->critical_start) {
2143 seq_puts(m, "# => started at: ");
2144 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2145 trace_print_seq(m, &iter->seq);
2146 seq_puts(m, "\n# => ended at: ");
2147 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2148 trace_print_seq(m, &iter->seq);
2149 seq_puts(m, "\n#\n");
2152 seq_puts(m, "#\n");
2155 static void test_cpu_buff_start(struct trace_iterator *iter)
2157 struct trace_seq *s = &iter->seq;
2159 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2160 return;
2162 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2163 return;
2165 if (cpumask_test_cpu(iter->cpu, iter->started))
2166 return;
2168 if (iter->tr->data[iter->cpu]->skipped_entries)
2169 return;
2171 cpumask_set_cpu(iter->cpu, iter->started);
2173 /* Don't print started cpu buffer for the first entry of the trace */
2174 if (iter->idx > 1)
2175 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2176 iter->cpu);
2179 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2181 struct trace_seq *s = &iter->seq;
2182 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2183 struct trace_entry *entry;
2184 struct trace_event *event;
2186 entry = iter->ent;
2188 test_cpu_buff_start(iter);
2190 event = ftrace_find_event(entry->type);
2192 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2193 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2194 if (!trace_print_lat_context(iter))
2195 goto partial;
2196 } else {
2197 if (!trace_print_context(iter))
2198 goto partial;
2202 if (event)
2203 return event->funcs->trace(iter, sym_flags, event);
2205 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2206 goto partial;
2208 return TRACE_TYPE_HANDLED;
2209 partial:
2210 return TRACE_TYPE_PARTIAL_LINE;
2213 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2215 struct trace_seq *s = &iter->seq;
2216 struct trace_entry *entry;
2217 struct trace_event *event;
2219 entry = iter->ent;
2221 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2222 if (!trace_seq_printf(s, "%d %d %llu ",
2223 entry->pid, iter->cpu, iter->ts))
2224 goto partial;
2227 event = ftrace_find_event(entry->type);
2228 if (event)
2229 return event->funcs->raw(iter, 0, event);
2231 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2232 goto partial;
2234 return TRACE_TYPE_HANDLED;
2235 partial:
2236 return TRACE_TYPE_PARTIAL_LINE;
2239 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2241 struct trace_seq *s = &iter->seq;
2242 unsigned char newline = '\n';
2243 struct trace_entry *entry;
2244 struct trace_event *event;
2246 entry = iter->ent;
2248 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2249 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2250 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2251 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2254 event = ftrace_find_event(entry->type);
2255 if (event) {
2256 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2257 if (ret != TRACE_TYPE_HANDLED)
2258 return ret;
2261 SEQ_PUT_FIELD_RET(s, newline);
2263 return TRACE_TYPE_HANDLED;
2266 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2268 struct trace_seq *s = &iter->seq;
2269 struct trace_entry *entry;
2270 struct trace_event *event;
2272 entry = iter->ent;
2274 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2275 SEQ_PUT_FIELD_RET(s, entry->pid);
2276 SEQ_PUT_FIELD_RET(s, iter->cpu);
2277 SEQ_PUT_FIELD_RET(s, iter->ts);
2280 event = ftrace_find_event(entry->type);
2281 return event ? event->funcs->binary(iter, 0, event) :
2282 TRACE_TYPE_HANDLED;
2285 int trace_empty(struct trace_iterator *iter)
2287 struct ring_buffer_iter *buf_iter;
2288 int cpu;
2290 /* If we are looking at one CPU buffer, only check that one */
2291 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2292 cpu = iter->cpu_file;
2293 buf_iter = trace_buffer_iter(iter, cpu);
2294 if (buf_iter) {
2295 if (!ring_buffer_iter_empty(buf_iter))
2296 return 0;
2297 } else {
2298 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2299 return 0;
2301 return 1;
2304 for_each_tracing_cpu(cpu) {
2305 buf_iter = trace_buffer_iter(iter, cpu);
2306 if (buf_iter) {
2307 if (!ring_buffer_iter_empty(buf_iter))
2308 return 0;
2309 } else {
2310 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2311 return 0;
2315 return 1;
2318 /* Called with trace_event_read_lock() held. */
2319 enum print_line_t print_trace_line(struct trace_iterator *iter)
2321 enum print_line_t ret;
2323 if (iter->lost_events &&
2324 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2325 iter->cpu, iter->lost_events))
2326 return TRACE_TYPE_PARTIAL_LINE;
2328 if (iter->trace && iter->trace->print_line) {
2329 ret = iter->trace->print_line(iter);
2330 if (ret != TRACE_TYPE_UNHANDLED)
2331 return ret;
2334 if (iter->ent->type == TRACE_BPRINT &&
2335 trace_flags & TRACE_ITER_PRINTK &&
2336 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2337 return trace_print_bprintk_msg_only(iter);
2339 if (iter->ent->type == TRACE_PRINT &&
2340 trace_flags & TRACE_ITER_PRINTK &&
2341 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2342 return trace_print_printk_msg_only(iter);
2344 if (trace_flags & TRACE_ITER_BIN)
2345 return print_bin_fmt(iter);
2347 if (trace_flags & TRACE_ITER_HEX)
2348 return print_hex_fmt(iter);
2350 if (trace_flags & TRACE_ITER_RAW)
2351 return print_raw_fmt(iter);
2353 return print_trace_fmt(iter);
2356 void trace_latency_header(struct seq_file *m)
2358 struct trace_iterator *iter = m->private;
2360 /* print nothing if the buffers are empty */
2361 if (trace_empty(iter))
2362 return;
2364 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2365 print_trace_header(m, iter);
2367 if (!(trace_flags & TRACE_ITER_VERBOSE))
2368 print_lat_help_header(m);
2371 void trace_default_header(struct seq_file *m)
2373 struct trace_iterator *iter = m->private;
2375 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2376 return;
2378 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2379 /* print nothing if the buffers are empty */
2380 if (trace_empty(iter))
2381 return;
2382 print_trace_header(m, iter);
2383 if (!(trace_flags & TRACE_ITER_VERBOSE))
2384 print_lat_help_header(m);
2385 } else {
2386 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2387 if (trace_flags & TRACE_ITER_IRQ_INFO)
2388 print_func_help_header_irq(iter->tr, m);
2389 else
2390 print_func_help_header(iter->tr, m);
2395 static void test_ftrace_alive(struct seq_file *m)
2397 if (!ftrace_is_dead())
2398 return;
2399 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2400 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2403 static int s_show(struct seq_file *m, void *v)
2405 struct trace_iterator *iter = v;
2406 int ret;
2408 if (iter->ent == NULL) {
2409 if (iter->tr) {
2410 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2411 seq_puts(m, "#\n");
2412 test_ftrace_alive(m);
2414 if (iter->trace && iter->trace->print_header)
2415 iter->trace->print_header(m);
2416 else
2417 trace_default_header(m);
2419 } else if (iter->leftover) {
2421 * If we filled the seq_file buffer earlier, we
2422 * want to just show it now.
2424 ret = trace_print_seq(m, &iter->seq);
2426 /* ret should this time be zero, but you never know */
2427 iter->leftover = ret;
2429 } else {
2430 print_trace_line(iter);
2431 ret = trace_print_seq(m, &iter->seq);
2433 * If we overflow the seq_file buffer, then it will
2434 * ask us for this data again at start up.
2435 * Use that instead.
2436 * ret is 0 if seq_file write succeeded.
2437 * -1 otherwise.
2439 iter->leftover = ret;
2442 return 0;
2445 static const struct seq_operations tracer_seq_ops = {
2446 .start = s_start,
2447 .next = s_next,
2448 .stop = s_stop,
2449 .show = s_show,
2452 static struct trace_iterator *
2453 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2455 long cpu_file = (long) inode->i_private;
2456 struct trace_iterator *iter;
2457 int cpu;
2459 if (tracing_disabled)
2460 return ERR_PTR(-ENODEV);
2462 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2463 if (!iter)
2464 return ERR_PTR(-ENOMEM);
2466 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2467 GFP_KERNEL);
2468 if (!iter->buffer_iter)
2469 goto release;
2472 * We make a copy of the current tracer to avoid concurrent
2473 * changes on it while we are reading.
2475 mutex_lock(&trace_types_lock);
2476 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2477 if (!iter->trace)
2478 goto fail;
2480 if (current_trace)
2481 *iter->trace = *current_trace;
2483 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2484 goto fail;
2486 if ((current_trace && current_trace->print_max) || snapshot)
2487 iter->tr = &max_tr;
2488 else
2489 iter->tr = &global_trace;
2490 iter->snapshot = snapshot;
2491 iter->pos = -1;
2492 mutex_init(&iter->mutex);
2493 iter->cpu_file = cpu_file;
2495 /* Notify the tracer early; before we stop tracing. */
2496 if (iter->trace && iter->trace->open)
2497 iter->trace->open(iter);
2499 /* Annotate start of buffers if we had overruns */
2500 if (ring_buffer_overruns(iter->tr->buffer))
2501 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2503 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2504 if (trace_clocks[trace_clock_id].in_ns)
2505 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2507 /* stop the trace while dumping if we are not opening "snapshot" */
2508 if (!iter->snapshot)
2509 tracing_stop();
2511 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2512 for_each_tracing_cpu(cpu) {
2513 iter->buffer_iter[cpu] =
2514 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2516 ring_buffer_read_prepare_sync();
2517 for_each_tracing_cpu(cpu) {
2518 ring_buffer_read_start(iter->buffer_iter[cpu]);
2519 tracing_iter_reset(iter, cpu);
2521 } else {
2522 cpu = iter->cpu_file;
2523 iter->buffer_iter[cpu] =
2524 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2525 ring_buffer_read_prepare_sync();
2526 ring_buffer_read_start(iter->buffer_iter[cpu]);
2527 tracing_iter_reset(iter, cpu);
2530 mutex_unlock(&trace_types_lock);
2532 return iter;
2534 fail:
2535 mutex_unlock(&trace_types_lock);
2536 kfree(iter->trace);
2537 kfree(iter->buffer_iter);
2538 release:
2539 seq_release_private(inode, file);
2540 return ERR_PTR(-ENOMEM);
2543 int tracing_open_generic(struct inode *inode, struct file *filp)
2545 if (tracing_disabled)
2546 return -ENODEV;
2548 filp->private_data = inode->i_private;
2549 return 0;
2552 static int tracing_release(struct inode *inode, struct file *file)
2554 struct seq_file *m = file->private_data;
2555 struct trace_iterator *iter;
2556 int cpu;
2558 if (!(file->f_mode & FMODE_READ))
2559 return 0;
2561 iter = m->private;
2563 mutex_lock(&trace_types_lock);
2564 for_each_tracing_cpu(cpu) {
2565 if (iter->buffer_iter[cpu])
2566 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2569 if (iter->trace && iter->trace->close)
2570 iter->trace->close(iter);
2572 if (!iter->snapshot)
2573 /* reenable tracing if it was previously enabled */
2574 tracing_start();
2575 mutex_unlock(&trace_types_lock);
2577 mutex_destroy(&iter->mutex);
2578 free_cpumask_var(iter->started);
2579 kfree(iter->trace);
2580 kfree(iter->buffer_iter);
2581 seq_release_private(inode, file);
2582 return 0;
2585 static int tracing_open(struct inode *inode, struct file *file)
2587 struct trace_iterator *iter;
2588 int ret = 0;
2590 /* If this file was open for write, then erase contents */
2591 if ((file->f_mode & FMODE_WRITE) &&
2592 (file->f_flags & O_TRUNC)) {
2593 long cpu = (long) inode->i_private;
2595 if (cpu == TRACE_PIPE_ALL_CPU)
2596 tracing_reset_online_cpus(&global_trace);
2597 else
2598 tracing_reset(&global_trace, cpu);
2601 if (file->f_mode & FMODE_READ) {
2602 iter = __tracing_open(inode, file, false);
2603 if (IS_ERR(iter))
2604 ret = PTR_ERR(iter);
2605 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2606 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2608 return ret;
2611 static void *
2612 t_next(struct seq_file *m, void *v, loff_t *pos)
2614 struct tracer *t = v;
2616 (*pos)++;
2618 if (t)
2619 t = t->next;
2621 return t;
2624 static void *t_start(struct seq_file *m, loff_t *pos)
2626 struct tracer *t;
2627 loff_t l = 0;
2629 mutex_lock(&trace_types_lock);
2630 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2633 return t;
2636 static void t_stop(struct seq_file *m, void *p)
2638 mutex_unlock(&trace_types_lock);
2641 static int t_show(struct seq_file *m, void *v)
2643 struct tracer *t = v;
2645 if (!t)
2646 return 0;
2648 seq_printf(m, "%s", t->name);
2649 if (t->next)
2650 seq_putc(m, ' ');
2651 else
2652 seq_putc(m, '\n');
2654 return 0;
2657 static const struct seq_operations show_traces_seq_ops = {
2658 .start = t_start,
2659 .next = t_next,
2660 .stop = t_stop,
2661 .show = t_show,
2664 static int show_traces_open(struct inode *inode, struct file *file)
2666 if (tracing_disabled)
2667 return -ENODEV;
2669 return seq_open(file, &show_traces_seq_ops);
2672 static ssize_t
2673 tracing_write_stub(struct file *filp, const char __user *ubuf,
2674 size_t count, loff_t *ppos)
2676 return count;
2679 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2681 if (file->f_mode & FMODE_READ)
2682 return seq_lseek(file, offset, origin);
2683 else
2684 return 0;
2687 static const struct file_operations tracing_fops = {
2688 .open = tracing_open,
2689 .read = seq_read,
2690 .write = tracing_write_stub,
2691 .llseek = tracing_seek,
2692 .release = tracing_release,
2695 static const struct file_operations show_traces_fops = {
2696 .open = show_traces_open,
2697 .read = seq_read,
2698 .release = seq_release,
2699 .llseek = seq_lseek,
2703 * Only trace on a CPU if the bitmask is set:
2705 static cpumask_var_t tracing_cpumask;
2708 * The tracer itself will not take this lock, but still we want
2709 * to provide a consistent cpumask to user-space:
2711 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2714 * Temporary storage for the character representation of the
2715 * CPU bitmask (and one more byte for the newline):
2717 static char mask_str[NR_CPUS + 1];
2719 static ssize_t
2720 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2721 size_t count, loff_t *ppos)
2723 int len;
2725 mutex_lock(&tracing_cpumask_update_lock);
2727 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2728 if (count - len < 2) {
2729 count = -EINVAL;
2730 goto out_err;
2732 len += sprintf(mask_str + len, "\n");
2733 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2735 out_err:
2736 mutex_unlock(&tracing_cpumask_update_lock);
2738 return count;
2741 static ssize_t
2742 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2743 size_t count, loff_t *ppos)
2745 int err, cpu;
2746 cpumask_var_t tracing_cpumask_new;
2748 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2749 return -ENOMEM;
2751 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2752 if (err)
2753 goto err_unlock;
2755 mutex_lock(&tracing_cpumask_update_lock);
2757 local_irq_disable();
2758 arch_spin_lock(&ftrace_max_lock);
2759 for_each_tracing_cpu(cpu) {
2761 * Increase/decrease the disabled counter if we are
2762 * about to flip a bit in the cpumask:
2764 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2765 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2766 atomic_inc(&global_trace.data[cpu]->disabled);
2767 ring_buffer_record_disable_cpu(global_trace.buffer, cpu);
2769 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2770 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2771 atomic_dec(&global_trace.data[cpu]->disabled);
2772 ring_buffer_record_enable_cpu(global_trace.buffer, cpu);
2775 arch_spin_unlock(&ftrace_max_lock);
2776 local_irq_enable();
2778 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2780 mutex_unlock(&tracing_cpumask_update_lock);
2781 free_cpumask_var(tracing_cpumask_new);
2783 return count;
2785 err_unlock:
2786 free_cpumask_var(tracing_cpumask_new);
2788 return err;
2791 static const struct file_operations tracing_cpumask_fops = {
2792 .open = tracing_open_generic,
2793 .read = tracing_cpumask_read,
2794 .write = tracing_cpumask_write,
2795 .llseek = generic_file_llseek,
2798 static int tracing_trace_options_show(struct seq_file *m, void *v)
2800 struct tracer_opt *trace_opts;
2801 u32 tracer_flags;
2802 int i;
2804 mutex_lock(&trace_types_lock);
2805 tracer_flags = current_trace->flags->val;
2806 trace_opts = current_trace->flags->opts;
2808 for (i = 0; trace_options[i]; i++) {
2809 if (trace_flags & (1 << i))
2810 seq_printf(m, "%s\n", trace_options[i]);
2811 else
2812 seq_printf(m, "no%s\n", trace_options[i]);
2815 for (i = 0; trace_opts[i].name; i++) {
2816 if (tracer_flags & trace_opts[i].bit)
2817 seq_printf(m, "%s\n", trace_opts[i].name);
2818 else
2819 seq_printf(m, "no%s\n", trace_opts[i].name);
2821 mutex_unlock(&trace_types_lock);
2823 return 0;
2826 static int __set_tracer_option(struct tracer *trace,
2827 struct tracer_flags *tracer_flags,
2828 struct tracer_opt *opts, int neg)
2830 int ret;
2832 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2833 if (ret)
2834 return ret;
2836 if (neg)
2837 tracer_flags->val &= ~opts->bit;
2838 else
2839 tracer_flags->val |= opts->bit;
2840 return 0;
2843 /* Try to assign a tracer specific option */
2844 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2846 struct tracer_flags *tracer_flags = trace->flags;
2847 struct tracer_opt *opts = NULL;
2848 int i;
2850 for (i = 0; tracer_flags->opts[i].name; i++) {
2851 opts = &tracer_flags->opts[i];
2853 if (strcmp(cmp, opts->name) == 0)
2854 return __set_tracer_option(trace, trace->flags,
2855 opts, neg);
2858 return -EINVAL;
2861 static void set_tracer_flags(unsigned int mask, int enabled)
2863 /* do nothing if flag is already set */
2864 if (!!(trace_flags & mask) == !!enabled)
2865 return;
2867 if (enabled)
2868 trace_flags |= mask;
2869 else
2870 trace_flags &= ~mask;
2872 if (mask == TRACE_ITER_RECORD_CMD)
2873 trace_event_enable_cmd_record(enabled);
2875 if (mask == TRACE_ITER_OVERWRITE)
2876 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2878 if (mask == TRACE_ITER_PRINTK)
2879 trace_printk_start_stop_comm(enabled);
2882 static int trace_set_options(char *option)
2884 char *cmp;
2885 int neg = 0;
2886 int ret = 0;
2887 int i;
2889 cmp = strstrip(option);
2891 if (strncmp(cmp, "no", 2) == 0) {
2892 neg = 1;
2893 cmp += 2;
2896 for (i = 0; trace_options[i]; i++) {
2897 if (strcmp(cmp, trace_options[i]) == 0) {
2898 set_tracer_flags(1 << i, !neg);
2899 break;
2903 /* If no option could be set, test the specific tracer options */
2904 if (!trace_options[i]) {
2905 mutex_lock(&trace_types_lock);
2906 ret = set_tracer_option(current_trace, cmp, neg);
2907 mutex_unlock(&trace_types_lock);
2910 return ret;
2913 static ssize_t
2914 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2915 size_t cnt, loff_t *ppos)
2917 char buf[64];
2919 if (cnt >= sizeof(buf))
2920 return -EINVAL;
2922 if (copy_from_user(&buf, ubuf, cnt))
2923 return -EFAULT;
2925 buf[cnt] = 0;
2927 trace_set_options(buf);
2929 *ppos += cnt;
2931 return cnt;
2934 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2936 if (tracing_disabled)
2937 return -ENODEV;
2938 return single_open(file, tracing_trace_options_show, NULL);
2941 static const struct file_operations tracing_iter_fops = {
2942 .open = tracing_trace_options_open,
2943 .read = seq_read,
2944 .llseek = seq_lseek,
2945 .release = single_release,
2946 .write = tracing_trace_options_write,
2949 static const char readme_msg[] =
2950 "tracing mini-HOWTO:\n\n"
2951 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2952 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2953 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
2954 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2955 "nop\n"
2956 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
2957 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2958 "wakeup\n"
2959 "# cat /sys/kernel/debug/tracing/trace_options\n"
2960 "noprint-parent nosym-offset nosym-addr noverbose\n"
2961 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2962 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
2963 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2964 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
2967 static ssize_t
2968 tracing_readme_read(struct file *filp, char __user *ubuf,
2969 size_t cnt, loff_t *ppos)
2971 return simple_read_from_buffer(ubuf, cnt, ppos,
2972 readme_msg, strlen(readme_msg));
2975 static const struct file_operations tracing_readme_fops = {
2976 .open = tracing_open_generic,
2977 .read = tracing_readme_read,
2978 .llseek = generic_file_llseek,
2981 static ssize_t
2982 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2983 size_t cnt, loff_t *ppos)
2985 char *buf_comm;
2986 char *file_buf;
2987 char *buf;
2988 int len = 0;
2989 int pid;
2990 int i;
2992 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2993 if (!file_buf)
2994 return -ENOMEM;
2996 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2997 if (!buf_comm) {
2998 kfree(file_buf);
2999 return -ENOMEM;
3002 buf = file_buf;
3004 for (i = 0; i < SAVED_CMDLINES; i++) {
3005 int r;
3007 pid = map_cmdline_to_pid[i];
3008 if (pid == -1 || pid == NO_CMDLINE_MAP)
3009 continue;
3011 trace_find_cmdline(pid, buf_comm);
3012 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3013 buf += r;
3014 len += r;
3017 len = simple_read_from_buffer(ubuf, cnt, ppos,
3018 file_buf, len);
3020 kfree(file_buf);
3021 kfree(buf_comm);
3023 return len;
3026 static const struct file_operations tracing_saved_cmdlines_fops = {
3027 .open = tracing_open_generic,
3028 .read = tracing_saved_cmdlines_read,
3029 .llseek = generic_file_llseek,
3032 static ssize_t
3033 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3034 size_t cnt, loff_t *ppos)
3036 char buf[MAX_TRACER_SIZE+2];
3037 int r;
3039 mutex_lock(&trace_types_lock);
3040 if (current_trace)
3041 r = sprintf(buf, "%s\n", current_trace->name);
3042 else
3043 r = sprintf(buf, "\n");
3044 mutex_unlock(&trace_types_lock);
3046 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3049 int tracer_init(struct tracer *t, struct trace_array *tr)
3051 tracing_reset_online_cpus(tr);
3052 return t->init(tr);
3055 static void set_buffer_entries(struct trace_array *tr, unsigned long val)
3057 int cpu;
3058 for_each_tracing_cpu(cpu)
3059 tr->data[cpu]->entries = val;
3062 /* resize @tr's buffer to the size of @size_tr's entries */
3063 static int resize_buffer_duplicate_size(struct trace_array *tr,
3064 struct trace_array *size_tr, int cpu_id)
3066 int cpu, ret = 0;
3068 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3069 for_each_tracing_cpu(cpu) {
3070 ret = ring_buffer_resize(tr->buffer,
3071 size_tr->data[cpu]->entries, cpu);
3072 if (ret < 0)
3073 break;
3074 tr->data[cpu]->entries = size_tr->data[cpu]->entries;
3076 } else {
3077 ret = ring_buffer_resize(tr->buffer,
3078 size_tr->data[cpu_id]->entries, cpu_id);
3079 if (ret == 0)
3080 tr->data[cpu_id]->entries =
3081 size_tr->data[cpu_id]->entries;
3084 return ret;
3087 static int __tracing_resize_ring_buffer(unsigned long size, int cpu)
3089 int ret;
3092 * If kernel or user changes the size of the ring buffer
3093 * we use the size that was given, and we can forget about
3094 * expanding it later.
3096 ring_buffer_expanded = 1;
3098 /* May be called before buffers are initialized */
3099 if (!global_trace.buffer)
3100 return 0;
3102 ret = ring_buffer_resize(global_trace.buffer, size, cpu);
3103 if (ret < 0)
3104 return ret;
3106 if (!current_trace->use_max_tr)
3107 goto out;
3109 ret = ring_buffer_resize(max_tr.buffer, size, cpu);
3110 if (ret < 0) {
3111 int r = resize_buffer_duplicate_size(&global_trace,
3112 &global_trace, cpu);
3113 if (r < 0) {
3115 * AARGH! We are left with different
3116 * size max buffer!!!!
3117 * The max buffer is our "snapshot" buffer.
3118 * When a tracer needs a snapshot (one of the
3119 * latency tracers), it swaps the max buffer
3120 * with the saved snap shot. We succeeded to
3121 * update the size of the main buffer, but failed to
3122 * update the size of the max buffer. But when we tried
3123 * to reset the main buffer to the original size, we
3124 * failed there too. This is very unlikely to
3125 * happen, but if it does, warn and kill all
3126 * tracing.
3128 WARN_ON(1);
3129 tracing_disabled = 1;
3131 return ret;
3134 if (cpu == RING_BUFFER_ALL_CPUS)
3135 set_buffer_entries(&max_tr, size);
3136 else
3137 max_tr.data[cpu]->entries = size;
3139 out:
3140 if (cpu == RING_BUFFER_ALL_CPUS)
3141 set_buffer_entries(&global_trace, size);
3142 else
3143 global_trace.data[cpu]->entries = size;
3145 return ret;
3148 static ssize_t tracing_resize_ring_buffer(unsigned long size, int cpu_id)
3150 int ret = size;
3152 mutex_lock(&trace_types_lock);
3154 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3155 /* make sure, this cpu is enabled in the mask */
3156 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3157 ret = -EINVAL;
3158 goto out;
3162 ret = __tracing_resize_ring_buffer(size, cpu_id);
3163 if (ret < 0)
3164 ret = -ENOMEM;
3166 out:
3167 mutex_unlock(&trace_types_lock);
3169 return ret;
3174 * tracing_update_buffers - used by tracing facility to expand ring buffers
3176 * To save on memory when the tracing is never used on a system with it
3177 * configured in. The ring buffers are set to a minimum size. But once
3178 * a user starts to use the tracing facility, then they need to grow
3179 * to their default size.
3181 * This function is to be called when a tracer is about to be used.
3183 int tracing_update_buffers(void)
3185 int ret = 0;
3187 mutex_lock(&trace_types_lock);
3188 if (!ring_buffer_expanded)
3189 ret = __tracing_resize_ring_buffer(trace_buf_size,
3190 RING_BUFFER_ALL_CPUS);
3191 mutex_unlock(&trace_types_lock);
3193 return ret;
3196 struct trace_option_dentry;
3198 static struct trace_option_dentry *
3199 create_trace_option_files(struct tracer *tracer);
3201 static void
3202 destroy_trace_option_files(struct trace_option_dentry *topts);
3204 static int tracing_set_tracer(const char *buf)
3206 static struct trace_option_dentry *topts;
3207 struct trace_array *tr = &global_trace;
3208 struct tracer *t;
3209 bool had_max_tr;
3210 int ret = 0;
3212 mutex_lock(&trace_types_lock);
3214 if (!ring_buffer_expanded) {
3215 ret = __tracing_resize_ring_buffer(trace_buf_size,
3216 RING_BUFFER_ALL_CPUS);
3217 if (ret < 0)
3218 goto out;
3219 ret = 0;
3222 for (t = trace_types; t; t = t->next) {
3223 if (strcmp(t->name, buf) == 0)
3224 break;
3226 if (!t) {
3227 ret = -EINVAL;
3228 goto out;
3230 if (t == current_trace)
3231 goto out;
3233 trace_branch_disable();
3234 if (current_trace && current_trace->reset)
3235 current_trace->reset(tr);
3237 had_max_tr = current_trace && current_trace->allocated_snapshot;
3238 current_trace = &nop_trace;
3240 if (had_max_tr && !t->use_max_tr) {
3242 * We need to make sure that the update_max_tr sees that
3243 * current_trace changed to nop_trace to keep it from
3244 * swapping the buffers after we resize it.
3245 * The update_max_tr is called from interrupts disabled
3246 * so a synchronized_sched() is sufficient.
3248 synchronize_sched();
3250 * We don't free the ring buffer. instead, resize it because
3251 * The max_tr ring buffer has some state (e.g. ring->clock) and
3252 * we want preserve it.
3254 ring_buffer_resize(max_tr.buffer, 1, RING_BUFFER_ALL_CPUS);
3255 set_buffer_entries(&max_tr, 1);
3256 tracing_reset_online_cpus(&max_tr);
3257 current_trace->allocated_snapshot = false;
3259 destroy_trace_option_files(topts);
3261 topts = create_trace_option_files(t);
3262 if (t->use_max_tr && !had_max_tr) {
3263 /* we need to make per cpu buffer sizes equivalent */
3264 ret = resize_buffer_duplicate_size(&max_tr, &global_trace,
3265 RING_BUFFER_ALL_CPUS);
3266 if (ret < 0)
3267 goto out;
3268 t->allocated_snapshot = true;
3271 if (t->init) {
3272 ret = tracer_init(t, tr);
3273 if (ret)
3274 goto out;
3277 current_trace = t;
3278 trace_branch_enable(tr);
3279 out:
3280 mutex_unlock(&trace_types_lock);
3282 return ret;
3285 static ssize_t
3286 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3287 size_t cnt, loff_t *ppos)
3289 char buf[MAX_TRACER_SIZE+1];
3290 int i;
3291 size_t ret;
3292 int err;
3294 ret = cnt;
3296 if (cnt > MAX_TRACER_SIZE)
3297 cnt = MAX_TRACER_SIZE;
3299 if (copy_from_user(&buf, ubuf, cnt))
3300 return -EFAULT;
3302 buf[cnt] = 0;
3304 /* strip ending whitespace. */
3305 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3306 buf[i] = 0;
3308 err = tracing_set_tracer(buf);
3309 if (err)
3310 return err;
3312 *ppos += ret;
3314 return ret;
3317 static ssize_t
3318 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3319 size_t cnt, loff_t *ppos)
3321 unsigned long *ptr = filp->private_data;
3322 char buf[64];
3323 int r;
3325 r = snprintf(buf, sizeof(buf), "%ld\n",
3326 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3327 if (r > sizeof(buf))
3328 r = sizeof(buf);
3329 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3332 static ssize_t
3333 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3334 size_t cnt, loff_t *ppos)
3336 unsigned long *ptr = filp->private_data;
3337 unsigned long val;
3338 int ret;
3340 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3341 if (ret)
3342 return ret;
3344 *ptr = val * 1000;
3346 return cnt;
3349 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3351 long cpu_file = (long) inode->i_private;
3352 struct trace_iterator *iter;
3353 int ret = 0;
3355 if (tracing_disabled)
3356 return -ENODEV;
3358 mutex_lock(&trace_types_lock);
3360 /* create a buffer to store the information to pass to userspace */
3361 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3362 if (!iter) {
3363 ret = -ENOMEM;
3364 goto out;
3368 * We make a copy of the current tracer to avoid concurrent
3369 * changes on it while we are reading.
3371 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3372 if (!iter->trace) {
3373 ret = -ENOMEM;
3374 goto fail;
3376 if (current_trace)
3377 *iter->trace = *current_trace;
3379 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3380 ret = -ENOMEM;
3381 goto fail;
3384 /* trace pipe does not show start of buffer */
3385 cpumask_setall(iter->started);
3387 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3388 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3390 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3391 if (trace_clocks[trace_clock_id].in_ns)
3392 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3394 iter->cpu_file = cpu_file;
3395 iter->tr = &global_trace;
3396 mutex_init(&iter->mutex);
3397 filp->private_data = iter;
3399 if (iter->trace->pipe_open)
3400 iter->trace->pipe_open(iter);
3402 nonseekable_open(inode, filp);
3403 out:
3404 mutex_unlock(&trace_types_lock);
3405 return ret;
3407 fail:
3408 kfree(iter->trace);
3409 kfree(iter);
3410 mutex_unlock(&trace_types_lock);
3411 return ret;
3414 static int tracing_release_pipe(struct inode *inode, struct file *file)
3416 struct trace_iterator *iter = file->private_data;
3418 mutex_lock(&trace_types_lock);
3420 if (iter->trace->pipe_close)
3421 iter->trace->pipe_close(iter);
3423 mutex_unlock(&trace_types_lock);
3425 free_cpumask_var(iter->started);
3426 mutex_destroy(&iter->mutex);
3427 kfree(iter->trace);
3428 kfree(iter);
3430 return 0;
3433 static unsigned int
3434 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3436 struct trace_iterator *iter = filp->private_data;
3438 if (trace_flags & TRACE_ITER_BLOCK) {
3440 * Always select as readable when in blocking mode
3442 return POLLIN | POLLRDNORM;
3443 } else {
3444 if (!trace_empty(iter))
3445 return POLLIN | POLLRDNORM;
3446 poll_wait(filp, &trace_wait, poll_table);
3447 if (!trace_empty(iter))
3448 return POLLIN | POLLRDNORM;
3450 return 0;
3455 * This is a make-shift waitqueue.
3456 * A tracer might use this callback on some rare cases:
3458 * 1) the current tracer might hold the runqueue lock when it wakes up
3459 * a reader, hence a deadlock (sched, function, and function graph tracers)
3460 * 2) the function tracers, trace all functions, we don't want
3461 * the overhead of calling wake_up and friends
3462 * (and tracing them too)
3464 * Anyway, this is really very primitive wakeup.
3466 void poll_wait_pipe(struct trace_iterator *iter)
3468 set_current_state(TASK_INTERRUPTIBLE);
3469 /* sleep for 100 msecs, and try again. */
3470 schedule_timeout(HZ / 10);
3473 /* Must be called with trace_types_lock mutex held. */
3474 static int tracing_wait_pipe(struct file *filp)
3476 struct trace_iterator *iter = filp->private_data;
3478 while (trace_empty(iter)) {
3480 if ((filp->f_flags & O_NONBLOCK)) {
3481 return -EAGAIN;
3484 mutex_unlock(&iter->mutex);
3486 iter->trace->wait_pipe(iter);
3488 mutex_lock(&iter->mutex);
3490 if (signal_pending(current))
3491 return -EINTR;
3494 * We block until we read something and tracing is disabled.
3495 * We still block if tracing is disabled, but we have never
3496 * read anything. This allows a user to cat this file, and
3497 * then enable tracing. But after we have read something,
3498 * we give an EOF when tracing is again disabled.
3500 * iter->pos will be 0 if we haven't read anything.
3502 if (!tracing_is_enabled() && iter->pos)
3503 break;
3506 return 1;
3510 * Consumer reader.
3512 static ssize_t
3513 tracing_read_pipe(struct file *filp, char __user *ubuf,
3514 size_t cnt, loff_t *ppos)
3516 struct trace_iterator *iter = filp->private_data;
3517 ssize_t sret;
3519 /* return any leftover data */
3520 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3521 if (sret != -EBUSY)
3522 return sret;
3524 trace_seq_init(&iter->seq);
3526 /* copy the tracer to avoid using a global lock all around */
3527 mutex_lock(&trace_types_lock);
3528 if (unlikely(current_trace && iter->trace->name != current_trace->name))
3529 *iter->trace = *current_trace;
3530 mutex_unlock(&trace_types_lock);
3533 * Avoid more than one consumer on a single file descriptor
3534 * This is just a matter of traces coherency, the ring buffer itself
3535 * is protected.
3537 mutex_lock(&iter->mutex);
3538 if (iter->trace->read) {
3539 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3540 if (sret)
3541 goto out;
3544 waitagain:
3545 sret = tracing_wait_pipe(filp);
3546 if (sret <= 0)
3547 goto out;
3549 /* stop when tracing is finished */
3550 if (trace_empty(iter)) {
3551 sret = 0;
3552 goto out;
3555 if (cnt >= PAGE_SIZE)
3556 cnt = PAGE_SIZE - 1;
3558 /* reset all but tr, trace, and overruns */
3559 memset(&iter->seq, 0,
3560 sizeof(struct trace_iterator) -
3561 offsetof(struct trace_iterator, seq));
3562 iter->pos = -1;
3564 trace_event_read_lock();
3565 trace_access_lock(iter->cpu_file);
3566 while (trace_find_next_entry_inc(iter) != NULL) {
3567 enum print_line_t ret;
3568 int len = iter->seq.len;
3570 ret = print_trace_line(iter);
3571 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3572 /* don't print partial lines */
3573 iter->seq.len = len;
3574 break;
3576 if (ret != TRACE_TYPE_NO_CONSUME)
3577 trace_consume(iter);
3579 if (iter->seq.len >= cnt)
3580 break;
3583 * Setting the full flag means we reached the trace_seq buffer
3584 * size and we should leave by partial output condition above.
3585 * One of the trace_seq_* functions is not used properly.
3587 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3588 iter->ent->type);
3590 trace_access_unlock(iter->cpu_file);
3591 trace_event_read_unlock();
3593 /* Now copy what we have to the user */
3594 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3595 if (iter->seq.readpos >= iter->seq.len)
3596 trace_seq_init(&iter->seq);
3599 * If there was nothing to send to user, in spite of consuming trace
3600 * entries, go back to wait for more entries.
3602 if (sret == -EBUSY)
3603 goto waitagain;
3605 out:
3606 mutex_unlock(&iter->mutex);
3608 return sret;
3611 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3612 struct pipe_buffer *buf)
3614 __free_page(buf->page);
3617 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3618 unsigned int idx)
3620 __free_page(spd->pages[idx]);
3623 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3624 .can_merge = 0,
3625 .map = generic_pipe_buf_map,
3626 .unmap = generic_pipe_buf_unmap,
3627 .confirm = generic_pipe_buf_confirm,
3628 .release = tracing_pipe_buf_release,
3629 .steal = generic_pipe_buf_steal,
3630 .get = generic_pipe_buf_get,
3633 static size_t
3634 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3636 size_t count;
3637 int ret;
3639 /* Seq buffer is page-sized, exactly what we need. */
3640 for (;;) {
3641 count = iter->seq.len;
3642 ret = print_trace_line(iter);
3643 count = iter->seq.len - count;
3644 if (rem < count) {
3645 rem = 0;
3646 iter->seq.len -= count;
3647 break;
3649 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3650 iter->seq.len -= count;
3651 break;
3654 if (ret != TRACE_TYPE_NO_CONSUME)
3655 trace_consume(iter);
3656 rem -= count;
3657 if (!trace_find_next_entry_inc(iter)) {
3658 rem = 0;
3659 iter->ent = NULL;
3660 break;
3664 return rem;
3667 static ssize_t tracing_splice_read_pipe(struct file *filp,
3668 loff_t *ppos,
3669 struct pipe_inode_info *pipe,
3670 size_t len,
3671 unsigned int flags)
3673 struct page *pages_def[PIPE_DEF_BUFFERS];
3674 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3675 struct trace_iterator *iter = filp->private_data;
3676 struct splice_pipe_desc spd = {
3677 .pages = pages_def,
3678 .partial = partial_def,
3679 .nr_pages = 0, /* This gets updated below. */
3680 .nr_pages_max = PIPE_DEF_BUFFERS,
3681 .flags = flags,
3682 .ops = &tracing_pipe_buf_ops,
3683 .spd_release = tracing_spd_release_pipe,
3685 ssize_t ret;
3686 size_t rem;
3687 unsigned int i;
3689 if (splice_grow_spd(pipe, &spd))
3690 return -ENOMEM;
3692 /* copy the tracer to avoid using a global lock all around */
3693 mutex_lock(&trace_types_lock);
3694 if (unlikely(current_trace && iter->trace->name != current_trace->name))
3695 *iter->trace = *current_trace;
3696 mutex_unlock(&trace_types_lock);
3698 mutex_lock(&iter->mutex);
3700 if (iter->trace->splice_read) {
3701 ret = iter->trace->splice_read(iter, filp,
3702 ppos, pipe, len, flags);
3703 if (ret)
3704 goto out_err;
3707 ret = tracing_wait_pipe(filp);
3708 if (ret <= 0)
3709 goto out_err;
3711 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3712 ret = -EFAULT;
3713 goto out_err;
3716 trace_event_read_lock();
3717 trace_access_lock(iter->cpu_file);
3719 /* Fill as many pages as possible. */
3720 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3721 spd.pages[i] = alloc_page(GFP_KERNEL);
3722 if (!spd.pages[i])
3723 break;
3725 rem = tracing_fill_pipe_page(rem, iter);
3727 /* Copy the data into the page, so we can start over. */
3728 ret = trace_seq_to_buffer(&iter->seq,
3729 page_address(spd.pages[i]),
3730 iter->seq.len);
3731 if (ret < 0) {
3732 __free_page(spd.pages[i]);
3733 break;
3735 spd.partial[i].offset = 0;
3736 spd.partial[i].len = iter->seq.len;
3738 trace_seq_init(&iter->seq);
3741 trace_access_unlock(iter->cpu_file);
3742 trace_event_read_unlock();
3743 mutex_unlock(&iter->mutex);
3745 spd.nr_pages = i;
3747 ret = splice_to_pipe(pipe, &spd);
3748 out:
3749 splice_shrink_spd(&spd);
3750 return ret;
3752 out_err:
3753 mutex_unlock(&iter->mutex);
3754 goto out;
3757 struct ftrace_entries_info {
3758 struct trace_array *tr;
3759 int cpu;
3762 static int tracing_entries_open(struct inode *inode, struct file *filp)
3764 struct ftrace_entries_info *info;
3766 if (tracing_disabled)
3767 return -ENODEV;
3769 info = kzalloc(sizeof(*info), GFP_KERNEL);
3770 if (!info)
3771 return -ENOMEM;
3773 info->tr = &global_trace;
3774 info->cpu = (unsigned long)inode->i_private;
3776 filp->private_data = info;
3778 return 0;
3781 static ssize_t
3782 tracing_entries_read(struct file *filp, char __user *ubuf,
3783 size_t cnt, loff_t *ppos)
3785 struct ftrace_entries_info *info = filp->private_data;
3786 struct trace_array *tr = info->tr;
3787 char buf[64];
3788 int r = 0;
3789 ssize_t ret;
3791 mutex_lock(&trace_types_lock);
3793 if (info->cpu == RING_BUFFER_ALL_CPUS) {
3794 int cpu, buf_size_same;
3795 unsigned long size;
3797 size = 0;
3798 buf_size_same = 1;
3799 /* check if all cpu sizes are same */
3800 for_each_tracing_cpu(cpu) {
3801 /* fill in the size from first enabled cpu */
3802 if (size == 0)
3803 size = tr->data[cpu]->entries;
3804 if (size != tr->data[cpu]->entries) {
3805 buf_size_same = 0;
3806 break;
3810 if (buf_size_same) {
3811 if (!ring_buffer_expanded)
3812 r = sprintf(buf, "%lu (expanded: %lu)\n",
3813 size >> 10,
3814 trace_buf_size >> 10);
3815 else
3816 r = sprintf(buf, "%lu\n", size >> 10);
3817 } else
3818 r = sprintf(buf, "X\n");
3819 } else
3820 r = sprintf(buf, "%lu\n", tr->data[info->cpu]->entries >> 10);
3822 mutex_unlock(&trace_types_lock);
3824 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3825 return ret;
3828 static ssize_t
3829 tracing_entries_write(struct file *filp, const char __user *ubuf,
3830 size_t cnt, loff_t *ppos)
3832 struct ftrace_entries_info *info = filp->private_data;
3833 unsigned long val;
3834 int ret;
3836 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3837 if (ret)
3838 return ret;
3840 /* must have at least 1 entry */
3841 if (!val)
3842 return -EINVAL;
3844 /* value is in KB */
3845 val <<= 10;
3847 ret = tracing_resize_ring_buffer(val, info->cpu);
3848 if (ret < 0)
3849 return ret;
3851 *ppos += cnt;
3853 return cnt;
3856 static int
3857 tracing_entries_release(struct inode *inode, struct file *filp)
3859 struct ftrace_entries_info *info = filp->private_data;
3861 kfree(info);
3863 return 0;
3866 static ssize_t
3867 tracing_total_entries_read(struct file *filp, char __user *ubuf,
3868 size_t cnt, loff_t *ppos)
3870 struct trace_array *tr = filp->private_data;
3871 char buf[64];
3872 int r, cpu;
3873 unsigned long size = 0, expanded_size = 0;
3875 mutex_lock(&trace_types_lock);
3876 for_each_tracing_cpu(cpu) {
3877 size += tr->data[cpu]->entries >> 10;
3878 if (!ring_buffer_expanded)
3879 expanded_size += trace_buf_size >> 10;
3881 if (ring_buffer_expanded)
3882 r = sprintf(buf, "%lu\n", size);
3883 else
3884 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3885 mutex_unlock(&trace_types_lock);
3887 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3890 static ssize_t
3891 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3892 size_t cnt, loff_t *ppos)
3895 * There is no need to read what the user has written, this function
3896 * is just to make sure that there is no error when "echo" is used
3899 *ppos += cnt;
3901 return cnt;
3904 static int
3905 tracing_free_buffer_release(struct inode *inode, struct file *filp)
3907 /* disable tracing ? */
3908 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3909 tracing_off();
3910 /* resize the ring buffer to 0 */
3911 tracing_resize_ring_buffer(0, RING_BUFFER_ALL_CPUS);
3913 return 0;
3916 static ssize_t
3917 tracing_mark_write(struct file *filp, const char __user *ubuf,
3918 size_t cnt, loff_t *fpos)
3920 unsigned long addr = (unsigned long)ubuf;
3921 struct ring_buffer_event *event;
3922 struct ring_buffer *buffer;
3923 struct print_entry *entry;
3924 unsigned long irq_flags;
3925 struct page *pages[2];
3926 void *map_page[2];
3927 int nr_pages = 1;
3928 ssize_t written;
3929 int offset;
3930 int size;
3931 int len;
3932 int ret;
3933 int i;
3935 if (tracing_disabled)
3936 return -EINVAL;
3938 if (!(trace_flags & TRACE_ITER_MARKERS))
3939 return -EINVAL;
3941 if (cnt > TRACE_BUF_SIZE)
3942 cnt = TRACE_BUF_SIZE;
3945 * Userspace is injecting traces into the kernel trace buffer.
3946 * We want to be as non intrusive as possible.
3947 * To do so, we do not want to allocate any special buffers
3948 * or take any locks, but instead write the userspace data
3949 * straight into the ring buffer.
3951 * First we need to pin the userspace buffer into memory,
3952 * which, most likely it is, because it just referenced it.
3953 * But there's no guarantee that it is. By using get_user_pages_fast()
3954 * and kmap_atomic/kunmap_atomic() we can get access to the
3955 * pages directly. We then write the data directly into the
3956 * ring buffer.
3958 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
3960 /* check if we cross pages */
3961 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
3962 nr_pages = 2;
3964 offset = addr & (PAGE_SIZE - 1);
3965 addr &= PAGE_MASK;
3967 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
3968 if (ret < nr_pages) {
3969 while (--ret >= 0)
3970 put_page(pages[ret]);
3971 written = -EFAULT;
3972 goto out;
3975 for (i = 0; i < nr_pages; i++)
3976 map_page[i] = kmap_atomic(pages[i]);
3978 local_save_flags(irq_flags);
3979 size = sizeof(*entry) + cnt + 2; /* possible \n added */
3980 buffer = global_trace.buffer;
3981 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3982 irq_flags, preempt_count());
3983 if (!event) {
3984 /* Ring buffer disabled, return as if not open for write */
3985 written = -EBADF;
3986 goto out_unlock;
3989 entry = ring_buffer_event_data(event);
3990 entry->ip = _THIS_IP_;
3992 if (nr_pages == 2) {
3993 len = PAGE_SIZE - offset;
3994 memcpy(&entry->buf, map_page[0] + offset, len);
3995 memcpy(&entry->buf[len], map_page[1], cnt - len);
3996 } else
3997 memcpy(&entry->buf, map_page[0] + offset, cnt);
3999 if (entry->buf[cnt - 1] != '\n') {
4000 entry->buf[cnt] = '\n';
4001 entry->buf[cnt + 1] = '\0';
4002 } else
4003 entry->buf[cnt] = '\0';
4005 __buffer_unlock_commit(buffer, event);
4007 written = cnt;
4009 *fpos += written;
4011 out_unlock:
4012 for (i = 0; i < nr_pages; i++){
4013 kunmap_atomic(map_page[i]);
4014 put_page(pages[i]);
4016 out:
4017 return written;
4020 static int tracing_clock_show(struct seq_file *m, void *v)
4022 int i;
4024 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4025 seq_printf(m,
4026 "%s%s%s%s", i ? " " : "",
4027 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
4028 i == trace_clock_id ? "]" : "");
4029 seq_putc(m, '\n');
4031 return 0;
4034 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4035 size_t cnt, loff_t *fpos)
4037 char buf[64];
4038 const char *clockstr;
4039 int i;
4041 if (cnt >= sizeof(buf))
4042 return -EINVAL;
4044 if (copy_from_user(&buf, ubuf, cnt))
4045 return -EFAULT;
4047 buf[cnt] = 0;
4049 clockstr = strstrip(buf);
4051 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4052 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4053 break;
4055 if (i == ARRAY_SIZE(trace_clocks))
4056 return -EINVAL;
4058 trace_clock_id = i;
4060 mutex_lock(&trace_types_lock);
4062 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
4063 if (max_tr.buffer)
4064 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
4067 * New clock may not be consistent with the previous clock.
4068 * Reset the buffer so that it doesn't have incomparable timestamps.
4070 tracing_reset_online_cpus(&global_trace);
4071 tracing_reset_online_cpus(&max_tr);
4073 mutex_unlock(&trace_types_lock);
4075 *fpos += cnt;
4077 return cnt;
4080 static int tracing_clock_open(struct inode *inode, struct file *file)
4082 if (tracing_disabled)
4083 return -ENODEV;
4084 return single_open(file, tracing_clock_show, NULL);
4087 #ifdef CONFIG_TRACER_SNAPSHOT
4088 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4090 struct trace_iterator *iter;
4091 int ret = 0;
4093 if (file->f_mode & FMODE_READ) {
4094 iter = __tracing_open(inode, file, true);
4095 if (IS_ERR(iter))
4096 ret = PTR_ERR(iter);
4098 return ret;
4101 static ssize_t
4102 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4103 loff_t *ppos)
4105 unsigned long val;
4106 int ret;
4108 ret = tracing_update_buffers();
4109 if (ret < 0)
4110 return ret;
4112 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4113 if (ret)
4114 return ret;
4116 mutex_lock(&trace_types_lock);
4118 if (current_trace && current_trace->use_max_tr) {
4119 ret = -EBUSY;
4120 goto out;
4123 switch (val) {
4124 case 0:
4125 if (current_trace->allocated_snapshot) {
4126 /* free spare buffer */
4127 ring_buffer_resize(max_tr.buffer, 1,
4128 RING_BUFFER_ALL_CPUS);
4129 set_buffer_entries(&max_tr, 1);
4130 tracing_reset_online_cpus(&max_tr);
4131 current_trace->allocated_snapshot = false;
4133 break;
4134 case 1:
4135 if (!current_trace->allocated_snapshot) {
4136 /* allocate spare buffer */
4137 ret = resize_buffer_duplicate_size(&max_tr,
4138 &global_trace, RING_BUFFER_ALL_CPUS);
4139 if (ret < 0)
4140 break;
4141 current_trace->allocated_snapshot = true;
4144 local_irq_disable();
4145 /* Now, we're going to swap */
4146 update_max_tr(&global_trace, current, smp_processor_id());
4147 local_irq_enable();
4148 break;
4149 default:
4150 if (current_trace->allocated_snapshot)
4151 tracing_reset_online_cpus(&max_tr);
4152 else
4153 ret = -EINVAL;
4154 break;
4157 if (ret >= 0) {
4158 *ppos += cnt;
4159 ret = cnt;
4161 out:
4162 mutex_unlock(&trace_types_lock);
4163 return ret;
4165 #endif /* CONFIG_TRACER_SNAPSHOT */
4168 static const struct file_operations tracing_max_lat_fops = {
4169 .open = tracing_open_generic,
4170 .read = tracing_max_lat_read,
4171 .write = tracing_max_lat_write,
4172 .llseek = generic_file_llseek,
4175 static const struct file_operations set_tracer_fops = {
4176 .open = tracing_open_generic,
4177 .read = tracing_set_trace_read,
4178 .write = tracing_set_trace_write,
4179 .llseek = generic_file_llseek,
4182 static const struct file_operations tracing_pipe_fops = {
4183 .open = tracing_open_pipe,
4184 .poll = tracing_poll_pipe,
4185 .read = tracing_read_pipe,
4186 .splice_read = tracing_splice_read_pipe,
4187 .release = tracing_release_pipe,
4188 .llseek = no_llseek,
4191 static const struct file_operations tracing_entries_fops = {
4192 .open = tracing_entries_open,
4193 .read = tracing_entries_read,
4194 .write = tracing_entries_write,
4195 .release = tracing_entries_release,
4196 .llseek = generic_file_llseek,
4199 static const struct file_operations tracing_total_entries_fops = {
4200 .open = tracing_open_generic,
4201 .read = tracing_total_entries_read,
4202 .llseek = generic_file_llseek,
4205 static const struct file_operations tracing_free_buffer_fops = {
4206 .write = tracing_free_buffer_write,
4207 .release = tracing_free_buffer_release,
4210 static const struct file_operations tracing_mark_fops = {
4211 .open = tracing_open_generic,
4212 .write = tracing_mark_write,
4213 .llseek = generic_file_llseek,
4216 static const struct file_operations trace_clock_fops = {
4217 .open = tracing_clock_open,
4218 .read = seq_read,
4219 .llseek = seq_lseek,
4220 .release = single_release,
4221 .write = tracing_clock_write,
4224 #ifdef CONFIG_TRACER_SNAPSHOT
4225 static const struct file_operations snapshot_fops = {
4226 .open = tracing_snapshot_open,
4227 .read = seq_read,
4228 .write = tracing_snapshot_write,
4229 .llseek = tracing_seek,
4230 .release = tracing_release,
4232 #endif /* CONFIG_TRACER_SNAPSHOT */
4234 struct ftrace_buffer_info {
4235 struct trace_array *tr;
4236 void *spare;
4237 int cpu;
4238 unsigned int read;
4241 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4243 int cpu = (int)(long)inode->i_private;
4244 struct ftrace_buffer_info *info;
4246 if (tracing_disabled)
4247 return -ENODEV;
4249 info = kzalloc(sizeof(*info), GFP_KERNEL);
4250 if (!info)
4251 return -ENOMEM;
4253 info->tr = &global_trace;
4254 info->cpu = cpu;
4255 info->spare = NULL;
4256 /* Force reading ring buffer for first read */
4257 info->read = (unsigned int)-1;
4259 filp->private_data = info;
4261 return nonseekable_open(inode, filp);
4264 static ssize_t
4265 tracing_buffers_read(struct file *filp, char __user *ubuf,
4266 size_t count, loff_t *ppos)
4268 struct ftrace_buffer_info *info = filp->private_data;
4269 ssize_t ret;
4270 size_t size;
4272 if (!count)
4273 return 0;
4275 if (!info->spare)
4276 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
4277 if (!info->spare)
4278 return -ENOMEM;
4280 /* Do we have previous read data to read? */
4281 if (info->read < PAGE_SIZE)
4282 goto read;
4284 trace_access_lock(info->cpu);
4285 ret = ring_buffer_read_page(info->tr->buffer,
4286 &info->spare,
4287 count,
4288 info->cpu, 0);
4289 trace_access_unlock(info->cpu);
4290 if (ret < 0)
4291 return 0;
4293 info->read = 0;
4295 read:
4296 size = PAGE_SIZE - info->read;
4297 if (size > count)
4298 size = count;
4300 ret = copy_to_user(ubuf, info->spare + info->read, size);
4301 if (ret == size)
4302 return -EFAULT;
4303 size -= ret;
4305 *ppos += size;
4306 info->read += size;
4308 return size;
4311 static int tracing_buffers_release(struct inode *inode, struct file *file)
4313 struct ftrace_buffer_info *info = file->private_data;
4315 if (info->spare)
4316 ring_buffer_free_read_page(info->tr->buffer, info->spare);
4317 kfree(info);
4319 return 0;
4322 struct buffer_ref {
4323 struct ring_buffer *buffer;
4324 void *page;
4325 int ref;
4328 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4329 struct pipe_buffer *buf)
4331 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4333 if (--ref->ref)
4334 return;
4336 ring_buffer_free_read_page(ref->buffer, ref->page);
4337 kfree(ref);
4338 buf->private = 0;
4341 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4342 struct pipe_buffer *buf)
4344 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4346 ref->ref++;
4349 /* Pipe buffer operations for a buffer. */
4350 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4351 .can_merge = 0,
4352 .map = generic_pipe_buf_map,
4353 .unmap = generic_pipe_buf_unmap,
4354 .confirm = generic_pipe_buf_confirm,
4355 .release = buffer_pipe_buf_release,
4356 .steal = generic_pipe_buf_steal,
4357 .get = buffer_pipe_buf_get,
4361 * Callback from splice_to_pipe(), if we need to release some pages
4362 * at the end of the spd in case we error'ed out in filling the pipe.
4364 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4366 struct buffer_ref *ref =
4367 (struct buffer_ref *)spd->partial[i].private;
4369 if (--ref->ref)
4370 return;
4372 ring_buffer_free_read_page(ref->buffer, ref->page);
4373 kfree(ref);
4374 spd->partial[i].private = 0;
4377 static ssize_t
4378 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4379 struct pipe_inode_info *pipe, size_t len,
4380 unsigned int flags)
4382 struct ftrace_buffer_info *info = file->private_data;
4383 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4384 struct page *pages_def[PIPE_DEF_BUFFERS];
4385 struct splice_pipe_desc spd = {
4386 .pages = pages_def,
4387 .partial = partial_def,
4388 .nr_pages_max = PIPE_DEF_BUFFERS,
4389 .flags = flags,
4390 .ops = &buffer_pipe_buf_ops,
4391 .spd_release = buffer_spd_release,
4393 struct buffer_ref *ref;
4394 int entries, size, i;
4395 size_t ret;
4397 if (splice_grow_spd(pipe, &spd))
4398 return -ENOMEM;
4400 if (*ppos & (PAGE_SIZE - 1)) {
4401 ret = -EINVAL;
4402 goto out;
4405 if (len & (PAGE_SIZE - 1)) {
4406 if (len < PAGE_SIZE) {
4407 ret = -EINVAL;
4408 goto out;
4410 len &= PAGE_MASK;
4413 trace_access_lock(info->cpu);
4414 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4416 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4417 struct page *page;
4418 int r;
4420 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4421 if (!ref)
4422 break;
4424 ref->ref = 1;
4425 ref->buffer = info->tr->buffer;
4426 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
4427 if (!ref->page) {
4428 kfree(ref);
4429 break;
4432 r = ring_buffer_read_page(ref->buffer, &ref->page,
4433 len, info->cpu, 1);
4434 if (r < 0) {
4435 ring_buffer_free_read_page(ref->buffer, ref->page);
4436 kfree(ref);
4437 break;
4441 * zero out any left over data, this is going to
4442 * user land.
4444 size = ring_buffer_page_len(ref->page);
4445 if (size < PAGE_SIZE)
4446 memset(ref->page + size, 0, PAGE_SIZE - size);
4448 page = virt_to_page(ref->page);
4450 spd.pages[i] = page;
4451 spd.partial[i].len = PAGE_SIZE;
4452 spd.partial[i].offset = 0;
4453 spd.partial[i].private = (unsigned long)ref;
4454 spd.nr_pages++;
4455 *ppos += PAGE_SIZE;
4457 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4460 trace_access_unlock(info->cpu);
4461 spd.nr_pages = i;
4463 /* did we read anything? */
4464 if (!spd.nr_pages) {
4465 if (flags & SPLICE_F_NONBLOCK)
4466 ret = -EAGAIN;
4467 else
4468 ret = 0;
4469 /* TODO: block */
4470 goto out;
4473 ret = splice_to_pipe(pipe, &spd);
4474 splice_shrink_spd(&spd);
4475 out:
4476 return ret;
4479 static const struct file_operations tracing_buffers_fops = {
4480 .open = tracing_buffers_open,
4481 .read = tracing_buffers_read,
4482 .release = tracing_buffers_release,
4483 .splice_read = tracing_buffers_splice_read,
4484 .llseek = no_llseek,
4487 static ssize_t
4488 tracing_stats_read(struct file *filp, char __user *ubuf,
4489 size_t count, loff_t *ppos)
4491 unsigned long cpu = (unsigned long)filp->private_data;
4492 struct trace_array *tr = &global_trace;
4493 struct trace_seq *s;
4494 unsigned long cnt;
4495 unsigned long long t;
4496 unsigned long usec_rem;
4498 s = kmalloc(sizeof(*s), GFP_KERNEL);
4499 if (!s)
4500 return -ENOMEM;
4502 trace_seq_init(s);
4504 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4505 trace_seq_printf(s, "entries: %ld\n", cnt);
4507 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4508 trace_seq_printf(s, "overrun: %ld\n", cnt);
4510 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4511 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4513 cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4514 trace_seq_printf(s, "bytes: %ld\n", cnt);
4516 if (trace_clocks[trace_clock_id].in_ns) {
4517 /* local or global for trace_clock */
4518 t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4519 usec_rem = do_div(t, USEC_PER_SEC);
4520 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
4521 t, usec_rem);
4523 t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4524 usec_rem = do_div(t, USEC_PER_SEC);
4525 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4526 } else {
4527 /* counter or tsc mode for trace_clock */
4528 trace_seq_printf(s, "oldest event ts: %llu\n",
4529 ring_buffer_oldest_event_ts(tr->buffer, cpu));
4531 trace_seq_printf(s, "now ts: %llu\n",
4532 ring_buffer_time_stamp(tr->buffer, cpu));
4535 cnt = ring_buffer_dropped_events_cpu(tr->buffer, cpu);
4536 trace_seq_printf(s, "dropped events: %ld\n", cnt);
4538 cnt = ring_buffer_read_events_cpu(tr->buffer, cpu);
4539 trace_seq_printf(s, "read events: %ld\n", cnt);
4541 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4543 kfree(s);
4545 return count;
4548 static const struct file_operations tracing_stats_fops = {
4549 .open = tracing_open_generic,
4550 .read = tracing_stats_read,
4551 .llseek = generic_file_llseek,
4554 #ifdef CONFIG_DYNAMIC_FTRACE
4556 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4558 return 0;
4561 static ssize_t
4562 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
4563 size_t cnt, loff_t *ppos)
4565 static char ftrace_dyn_info_buffer[1024];
4566 static DEFINE_MUTEX(dyn_info_mutex);
4567 unsigned long *p = filp->private_data;
4568 char *buf = ftrace_dyn_info_buffer;
4569 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
4570 int r;
4572 mutex_lock(&dyn_info_mutex);
4573 r = sprintf(buf, "%ld ", *p);
4575 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
4576 buf[r++] = '\n';
4578 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4580 mutex_unlock(&dyn_info_mutex);
4582 return r;
4585 static const struct file_operations tracing_dyn_info_fops = {
4586 .open = tracing_open_generic,
4587 .read = tracing_read_dyn_info,
4588 .llseek = generic_file_llseek,
4590 #endif
4592 static struct dentry *d_tracer;
4594 struct dentry *tracing_init_dentry(void)
4596 static int once;
4598 if (d_tracer)
4599 return d_tracer;
4601 if (!debugfs_initialized())
4602 return NULL;
4604 d_tracer = debugfs_create_dir("tracing", NULL);
4606 if (!d_tracer && !once) {
4607 once = 1;
4608 pr_warning("Could not create debugfs directory 'tracing'\n");
4609 return NULL;
4612 return d_tracer;
4615 static struct dentry *d_percpu;
4617 static struct dentry *tracing_dentry_percpu(void)
4619 static int once;
4620 struct dentry *d_tracer;
4622 if (d_percpu)
4623 return d_percpu;
4625 d_tracer = tracing_init_dentry();
4627 if (!d_tracer)
4628 return NULL;
4630 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4632 if (!d_percpu && !once) {
4633 once = 1;
4634 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4635 return NULL;
4638 return d_percpu;
4641 static void tracing_init_debugfs_percpu(long cpu)
4643 struct dentry *d_percpu = tracing_dentry_percpu();
4644 struct dentry *d_cpu;
4645 char cpu_dir[30]; /* 30 characters should be more than enough */
4647 if (!d_percpu)
4648 return;
4650 snprintf(cpu_dir, 30, "cpu%ld", cpu);
4651 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4652 if (!d_cpu) {
4653 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4654 return;
4657 /* per cpu trace_pipe */
4658 trace_create_file("trace_pipe", 0444, d_cpu,
4659 (void *) cpu, &tracing_pipe_fops);
4661 /* per cpu trace */
4662 trace_create_file("trace", 0644, d_cpu,
4663 (void *) cpu, &tracing_fops);
4665 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4666 (void *) cpu, &tracing_buffers_fops);
4668 trace_create_file("stats", 0444, d_cpu,
4669 (void *) cpu, &tracing_stats_fops);
4671 trace_create_file("buffer_size_kb", 0444, d_cpu,
4672 (void *) cpu, &tracing_entries_fops);
4675 #ifdef CONFIG_FTRACE_SELFTEST
4676 /* Let selftest have access to static functions in this file */
4677 #include "trace_selftest.c"
4678 #endif
4680 struct trace_option_dentry {
4681 struct tracer_opt *opt;
4682 struct tracer_flags *flags;
4683 struct dentry *entry;
4686 static ssize_t
4687 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4688 loff_t *ppos)
4690 struct trace_option_dentry *topt = filp->private_data;
4691 char *buf;
4693 if (topt->flags->val & topt->opt->bit)
4694 buf = "1\n";
4695 else
4696 buf = "0\n";
4698 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4701 static ssize_t
4702 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4703 loff_t *ppos)
4705 struct trace_option_dentry *topt = filp->private_data;
4706 unsigned long val;
4707 int ret;
4709 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4710 if (ret)
4711 return ret;
4713 if (val != 0 && val != 1)
4714 return -EINVAL;
4716 if (!!(topt->flags->val & topt->opt->bit) != val) {
4717 mutex_lock(&trace_types_lock);
4718 ret = __set_tracer_option(current_trace, topt->flags,
4719 topt->opt, !val);
4720 mutex_unlock(&trace_types_lock);
4721 if (ret)
4722 return ret;
4725 *ppos += cnt;
4727 return cnt;
4731 static const struct file_operations trace_options_fops = {
4732 .open = tracing_open_generic,
4733 .read = trace_options_read,
4734 .write = trace_options_write,
4735 .llseek = generic_file_llseek,
4738 static ssize_t
4739 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4740 loff_t *ppos)
4742 long index = (long)filp->private_data;
4743 char *buf;
4745 if (trace_flags & (1 << index))
4746 buf = "1\n";
4747 else
4748 buf = "0\n";
4750 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4753 static ssize_t
4754 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4755 loff_t *ppos)
4757 long index = (long)filp->private_data;
4758 unsigned long val;
4759 int ret;
4761 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4762 if (ret)
4763 return ret;
4765 if (val != 0 && val != 1)
4766 return -EINVAL;
4767 set_tracer_flags(1 << index, val);
4769 *ppos += cnt;
4771 return cnt;
4774 static const struct file_operations trace_options_core_fops = {
4775 .open = tracing_open_generic,
4776 .read = trace_options_core_read,
4777 .write = trace_options_core_write,
4778 .llseek = generic_file_llseek,
4781 struct dentry *trace_create_file(const char *name,
4782 umode_t mode,
4783 struct dentry *parent,
4784 void *data,
4785 const struct file_operations *fops)
4787 struct dentry *ret;
4789 ret = debugfs_create_file(name, mode, parent, data, fops);
4790 if (!ret)
4791 pr_warning("Could not create debugfs '%s' entry\n", name);
4793 return ret;
4797 static struct dentry *trace_options_init_dentry(void)
4799 struct dentry *d_tracer;
4800 static struct dentry *t_options;
4802 if (t_options)
4803 return t_options;
4805 d_tracer = tracing_init_dentry();
4806 if (!d_tracer)
4807 return NULL;
4809 t_options = debugfs_create_dir("options", d_tracer);
4810 if (!t_options) {
4811 pr_warning("Could not create debugfs directory 'options'\n");
4812 return NULL;
4815 return t_options;
4818 static void
4819 create_trace_option_file(struct trace_option_dentry *topt,
4820 struct tracer_flags *flags,
4821 struct tracer_opt *opt)
4823 struct dentry *t_options;
4825 t_options = trace_options_init_dentry();
4826 if (!t_options)
4827 return;
4829 topt->flags = flags;
4830 topt->opt = opt;
4832 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4833 &trace_options_fops);
4837 static struct trace_option_dentry *
4838 create_trace_option_files(struct tracer *tracer)
4840 struct trace_option_dentry *topts;
4841 struct tracer_flags *flags;
4842 struct tracer_opt *opts;
4843 int cnt;
4845 if (!tracer)
4846 return NULL;
4848 flags = tracer->flags;
4850 if (!flags || !flags->opts)
4851 return NULL;
4853 opts = flags->opts;
4855 for (cnt = 0; opts[cnt].name; cnt++)
4858 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4859 if (!topts)
4860 return NULL;
4862 for (cnt = 0; opts[cnt].name; cnt++)
4863 create_trace_option_file(&topts[cnt], flags,
4864 &opts[cnt]);
4866 return topts;
4869 static void
4870 destroy_trace_option_files(struct trace_option_dentry *topts)
4872 int cnt;
4874 if (!topts)
4875 return;
4877 for (cnt = 0; topts[cnt].opt; cnt++) {
4878 if (topts[cnt].entry)
4879 debugfs_remove(topts[cnt].entry);
4882 kfree(topts);
4885 static struct dentry *
4886 create_trace_option_core_file(const char *option, long index)
4888 struct dentry *t_options;
4890 t_options = trace_options_init_dentry();
4891 if (!t_options)
4892 return NULL;
4894 return trace_create_file(option, 0644, t_options, (void *)index,
4895 &trace_options_core_fops);
4898 static __init void create_trace_options_dir(void)
4900 struct dentry *t_options;
4901 int i;
4903 t_options = trace_options_init_dentry();
4904 if (!t_options)
4905 return;
4907 for (i = 0; trace_options[i]; i++)
4908 create_trace_option_core_file(trace_options[i], i);
4911 static ssize_t
4912 rb_simple_read(struct file *filp, char __user *ubuf,
4913 size_t cnt, loff_t *ppos)
4915 struct trace_array *tr = filp->private_data;
4916 struct ring_buffer *buffer = tr->buffer;
4917 char buf[64];
4918 int r;
4920 if (buffer)
4921 r = ring_buffer_record_is_on(buffer);
4922 else
4923 r = 0;
4925 r = sprintf(buf, "%d\n", r);
4927 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4930 static ssize_t
4931 rb_simple_write(struct file *filp, const char __user *ubuf,
4932 size_t cnt, loff_t *ppos)
4934 struct trace_array *tr = filp->private_data;
4935 struct ring_buffer *buffer = tr->buffer;
4936 unsigned long val;
4937 int ret;
4939 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4940 if (ret)
4941 return ret;
4943 if (buffer) {
4944 mutex_lock(&trace_types_lock);
4945 if (val) {
4946 ring_buffer_record_on(buffer);
4947 if (current_trace->start)
4948 current_trace->start(tr);
4949 } else {
4950 ring_buffer_record_off(buffer);
4951 if (current_trace->stop)
4952 current_trace->stop(tr);
4954 mutex_unlock(&trace_types_lock);
4957 (*ppos)++;
4959 return cnt;
4962 static const struct file_operations rb_simple_fops = {
4963 .open = tracing_open_generic,
4964 .read = rb_simple_read,
4965 .write = rb_simple_write,
4966 .llseek = default_llseek,
4969 static __init int tracer_init_debugfs(void)
4971 struct dentry *d_tracer;
4972 int cpu;
4974 trace_access_lock_init();
4976 d_tracer = tracing_init_dentry();
4978 trace_create_file("trace_options", 0644, d_tracer,
4979 NULL, &tracing_iter_fops);
4981 trace_create_file("tracing_cpumask", 0644, d_tracer,
4982 NULL, &tracing_cpumask_fops);
4984 trace_create_file("trace", 0644, d_tracer,
4985 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4987 trace_create_file("available_tracers", 0444, d_tracer,
4988 &global_trace, &show_traces_fops);
4990 trace_create_file("current_tracer", 0644, d_tracer,
4991 &global_trace, &set_tracer_fops);
4993 #ifdef CONFIG_TRACER_MAX_TRACE
4994 trace_create_file("tracing_max_latency", 0644, d_tracer,
4995 &tracing_max_latency, &tracing_max_lat_fops);
4996 #endif
4998 trace_create_file("tracing_thresh", 0644, d_tracer,
4999 &tracing_thresh, &tracing_max_lat_fops);
5001 trace_create_file("README", 0444, d_tracer,
5002 NULL, &tracing_readme_fops);
5004 trace_create_file("trace_pipe", 0444, d_tracer,
5005 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
5007 trace_create_file("buffer_size_kb", 0644, d_tracer,
5008 (void *) RING_BUFFER_ALL_CPUS, &tracing_entries_fops);
5010 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
5011 &global_trace, &tracing_total_entries_fops);
5013 trace_create_file("free_buffer", 0644, d_tracer,
5014 &global_trace, &tracing_free_buffer_fops);
5016 trace_create_file("trace_marker", 0220, d_tracer,
5017 NULL, &tracing_mark_fops);
5019 trace_create_file("saved_cmdlines", 0444, d_tracer,
5020 NULL, &tracing_saved_cmdlines_fops);
5022 trace_create_file("trace_clock", 0644, d_tracer, NULL,
5023 &trace_clock_fops);
5025 trace_create_file("tracing_on", 0644, d_tracer,
5026 &global_trace, &rb_simple_fops);
5028 #ifdef CONFIG_DYNAMIC_FTRACE
5029 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
5030 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
5031 #endif
5033 #ifdef CONFIG_TRACER_SNAPSHOT
5034 trace_create_file("snapshot", 0644, d_tracer,
5035 (void *) TRACE_PIPE_ALL_CPU, &snapshot_fops);
5036 #endif
5038 create_trace_options_dir();
5040 for_each_tracing_cpu(cpu)
5041 tracing_init_debugfs_percpu(cpu);
5043 return 0;
5046 static int trace_panic_handler(struct notifier_block *this,
5047 unsigned long event, void *unused)
5049 if (ftrace_dump_on_oops)
5050 ftrace_dump(ftrace_dump_on_oops);
5051 return NOTIFY_OK;
5054 static struct notifier_block trace_panic_notifier = {
5055 .notifier_call = trace_panic_handler,
5056 .next = NULL,
5057 .priority = 150 /* priority: INT_MAX >= x >= 0 */
5060 static int trace_die_handler(struct notifier_block *self,
5061 unsigned long val,
5062 void *data)
5064 switch (val) {
5065 case DIE_OOPS:
5066 if (ftrace_dump_on_oops)
5067 ftrace_dump(ftrace_dump_on_oops);
5068 break;
5069 default:
5070 break;
5072 return NOTIFY_OK;
5075 static struct notifier_block trace_die_notifier = {
5076 .notifier_call = trace_die_handler,
5077 .priority = 200
5081 * printk is set to max of 1024, we really don't need it that big.
5082 * Nothing should be printing 1000 characters anyway.
5084 #define TRACE_MAX_PRINT 1000
5087 * Define here KERN_TRACE so that we have one place to modify
5088 * it if we decide to change what log level the ftrace dump
5089 * should be at.
5091 #define KERN_TRACE KERN_EMERG
5093 void
5094 trace_printk_seq(struct trace_seq *s)
5096 /* Probably should print a warning here. */
5097 if (s->len >= 1000)
5098 s->len = 1000;
5100 /* should be zero ended, but we are paranoid. */
5101 s->buffer[s->len] = 0;
5103 printk(KERN_TRACE "%s", s->buffer);
5105 trace_seq_init(s);
5108 void trace_init_global_iter(struct trace_iterator *iter)
5110 iter->tr = &global_trace;
5111 iter->trace = current_trace;
5112 iter->cpu_file = TRACE_PIPE_ALL_CPU;
5115 static void
5116 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
5118 static arch_spinlock_t ftrace_dump_lock =
5119 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
5120 /* use static because iter can be a bit big for the stack */
5121 static struct trace_iterator iter;
5122 unsigned int old_userobj;
5123 static int dump_ran;
5124 unsigned long flags;
5125 int cnt = 0, cpu;
5127 /* only one dump */
5128 local_irq_save(flags);
5129 arch_spin_lock(&ftrace_dump_lock);
5130 if (dump_ran)
5131 goto out;
5133 dump_ran = 1;
5135 tracing_off();
5137 /* Did function tracer already get disabled? */
5138 if (ftrace_is_dead()) {
5139 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
5140 printk("# MAY BE MISSING FUNCTION EVENTS\n");
5143 if (disable_tracing)
5144 ftrace_kill();
5146 /* Simulate the iterator */
5147 trace_init_global_iter(&iter);
5149 for_each_tracing_cpu(cpu) {
5150 atomic_inc(&iter.tr->data[cpu]->disabled);
5153 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
5155 /* don't look at user memory in panic mode */
5156 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
5158 switch (oops_dump_mode) {
5159 case DUMP_ALL:
5160 iter.cpu_file = TRACE_PIPE_ALL_CPU;
5161 break;
5162 case DUMP_ORIG:
5163 iter.cpu_file = raw_smp_processor_id();
5164 break;
5165 case DUMP_NONE:
5166 goto out_enable;
5167 default:
5168 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
5169 iter.cpu_file = TRACE_PIPE_ALL_CPU;
5172 printk(KERN_TRACE "Dumping ftrace buffer:\n");
5175 * We need to stop all tracing on all CPUS to read the
5176 * the next buffer. This is a bit expensive, but is
5177 * not done often. We fill all what we can read,
5178 * and then release the locks again.
5181 while (!trace_empty(&iter)) {
5183 if (!cnt)
5184 printk(KERN_TRACE "---------------------------------\n");
5186 cnt++;
5188 /* reset all but tr, trace, and overruns */
5189 memset(&iter.seq, 0,
5190 sizeof(struct trace_iterator) -
5191 offsetof(struct trace_iterator, seq));
5192 iter.iter_flags |= TRACE_FILE_LAT_FMT;
5193 iter.pos = -1;
5195 if (trace_find_next_entry_inc(&iter) != NULL) {
5196 int ret;
5198 ret = print_trace_line(&iter);
5199 if (ret != TRACE_TYPE_NO_CONSUME)
5200 trace_consume(&iter);
5202 touch_nmi_watchdog();
5204 trace_printk_seq(&iter.seq);
5207 if (!cnt)
5208 printk(KERN_TRACE " (ftrace buffer empty)\n");
5209 else
5210 printk(KERN_TRACE "---------------------------------\n");
5212 out_enable:
5213 /* Re-enable tracing if requested */
5214 if (!disable_tracing) {
5215 trace_flags |= old_userobj;
5217 for_each_tracing_cpu(cpu) {
5218 atomic_dec(&iter.tr->data[cpu]->disabled);
5220 tracing_on();
5223 out:
5224 arch_spin_unlock(&ftrace_dump_lock);
5225 local_irq_restore(flags);
5228 /* By default: disable tracing after the dump */
5229 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
5231 __ftrace_dump(true, oops_dump_mode);
5233 EXPORT_SYMBOL_GPL(ftrace_dump);
5235 __init static int tracer_alloc_buffers(void)
5237 int ring_buf_size;
5238 enum ring_buffer_flags rb_flags;
5239 int i;
5240 int ret = -ENOMEM;
5243 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
5244 goto out;
5246 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
5247 goto out_free_buffer_mask;
5249 /* Only allocate trace_printk buffers if a trace_printk exists */
5250 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
5251 /* Must be called before global_trace.buffer is allocated */
5252 trace_printk_init_buffers();
5254 /* To save memory, keep the ring buffer size to its minimum */
5255 if (ring_buffer_expanded)
5256 ring_buf_size = trace_buf_size;
5257 else
5258 ring_buf_size = 1;
5260 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5262 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
5263 cpumask_copy(tracing_cpumask, cpu_all_mask);
5265 /* TODO: make the number of buffers hot pluggable with CPUS */
5266 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
5267 if (!global_trace.buffer) {
5268 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
5269 WARN_ON(1);
5270 goto out_free_cpumask;
5272 if (global_trace.buffer_disabled)
5273 tracing_off();
5276 #ifdef CONFIG_TRACER_MAX_TRACE
5277 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
5278 if (!max_tr.buffer) {
5279 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
5280 WARN_ON(1);
5281 ring_buffer_free(global_trace.buffer);
5282 goto out_free_cpumask;
5284 #endif
5286 /* Allocate the first page for all buffers */
5287 for_each_tracing_cpu(i) {
5288 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
5289 max_tr.data[i] = &per_cpu(max_tr_data, i);
5292 set_buffer_entries(&global_trace,
5293 ring_buffer_size(global_trace.buffer, 0));
5294 #ifdef CONFIG_TRACER_MAX_TRACE
5295 set_buffer_entries(&max_tr, 1);
5296 #endif
5298 trace_init_cmdlines();
5299 init_irq_work(&trace_work_wakeup, trace_wake_up);
5301 register_tracer(&nop_trace);
5302 current_trace = &nop_trace;
5303 /* All seems OK, enable tracing */
5304 tracing_disabled = 0;
5306 atomic_notifier_chain_register(&panic_notifier_list,
5307 &trace_panic_notifier);
5309 register_die_notifier(&trace_die_notifier);
5311 while (trace_boot_options) {
5312 char *option;
5314 option = strsep(&trace_boot_options, ",");
5315 trace_set_options(option);
5318 return 0;
5320 out_free_cpumask:
5321 free_cpumask_var(tracing_cpumask);
5322 out_free_buffer_mask:
5323 free_cpumask_var(tracing_buffer_mask);
5324 out:
5325 return ret;
5328 __init static int clear_boot_tracer(void)
5331 * The default tracer at boot buffer is an init section.
5332 * This function is called in lateinit. If we did not
5333 * find the boot tracer, then clear it out, to prevent
5334 * later registration from accessing the buffer that is
5335 * about to be freed.
5337 if (!default_bootup_tracer)
5338 return 0;
5340 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
5341 default_bootup_tracer);
5342 default_bootup_tracer = NULL;
5344 return 0;
5347 early_initcall(tracer_alloc_buffers);
5348 fs_initcall(tracer_init_debugfs);
5349 late_initcall(clear_boot_tracer);