Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / kernel / trace / trace.c
blobc4579f169249b6deca2fa728de5ef9183e491370
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 William Lee Irwin III
14 #include <linux/ring_buffer.h>
15 #include <generated/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/rwsem.h>
35 #include <linux/slab.h>
36 #include <linux/ctype.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
39 #include <linux/fs.h>
41 #include "trace.h"
42 #include "trace_output.h"
45 * On boot up, the ring buffer is set to the minimum size, so that
46 * we do not waste memory on systems that are not using tracing.
48 int ring_buffer_expanded;
51 * We need to change this state when a selftest is running.
52 * A selftest will lurk into the ring-buffer to count the
53 * entries inserted during the selftest although some concurrent
54 * insertions into the ring-buffer such as trace_printk could occurred
55 * at the same time, giving false positive or negative results.
57 static bool __read_mostly tracing_selftest_running;
60 * If a tracer is running, we do not want to run SELFTEST.
62 bool __read_mostly tracing_selftest_disabled;
64 /* For tracers that don't implement custom flags */
65 static struct tracer_opt dummy_tracer_opt[] = {
66 { }
69 static struct tracer_flags dummy_tracer_flags = {
70 .val = 0,
71 .opts = dummy_tracer_opt
74 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
76 return 0;
80 * Kill all tracing for good (never come back).
81 * It is initialized to 1 but will turn to zero if the initialization
82 * of the tracer is successful. But that is the only place that sets
83 * this back to zero.
85 static int tracing_disabled = 1;
87 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
89 static inline void ftrace_disable_cpu(void)
91 preempt_disable();
92 __this_cpu_inc(ftrace_cpu_disabled);
95 static inline void ftrace_enable_cpu(void)
97 __this_cpu_dec(ftrace_cpu_disabled);
98 preempt_enable();
101 cpumask_var_t __read_mostly tracing_buffer_mask;
104 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
106 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
107 * is set, then ftrace_dump is called. This will output the contents
108 * of the ftrace buffers to the console. This is very useful for
109 * capturing traces that lead to crashes and outputing it to a
110 * serial console.
112 * It is default off, but you can enable it with either specifying
113 * "ftrace_dump_on_oops" in the kernel command line, or setting
114 * /proc/sys/kernel/ftrace_dump_on_oops
115 * Set 1 if you want to dump buffers of all CPUs
116 * Set 2 if you want to dump the buffer of the CPU that triggered oops
119 enum ftrace_dump_mode ftrace_dump_on_oops;
121 static int tracing_set_tracer(const char *buf);
123 #define MAX_TRACER_SIZE 100
124 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
125 static char *default_bootup_tracer;
127 static int __init set_cmdline_ftrace(char *str)
129 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
130 default_bootup_tracer = bootup_tracer_buf;
131 /* We are using ftrace early, expand it */
132 ring_buffer_expanded = 1;
133 return 1;
135 __setup("ftrace=", set_cmdline_ftrace);
137 static int __init set_ftrace_dump_on_oops(char *str)
139 if (*str++ != '=' || !*str) {
140 ftrace_dump_on_oops = DUMP_ALL;
141 return 1;
144 if (!strcmp("orig_cpu", str)) {
145 ftrace_dump_on_oops = DUMP_ORIG;
146 return 1;
149 return 0;
151 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
153 unsigned long long ns2usecs(cycle_t nsec)
155 nsec += 500;
156 do_div(nsec, 1000);
157 return nsec;
161 * The global_trace is the descriptor that holds the tracing
162 * buffers for the live tracing. For each CPU, it contains
163 * a link list of pages that will store trace entries. The
164 * page descriptor of the pages in the memory is used to hold
165 * the link list by linking the lru item in the page descriptor
166 * to each of the pages in the buffer per CPU.
168 * For each active CPU there is a data field that holds the
169 * pages for the buffer for that CPU. Each CPU has the same number
170 * of pages allocated for its buffer.
172 static struct trace_array global_trace;
174 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
176 int filter_current_check_discard(struct ring_buffer *buffer,
177 struct ftrace_event_call *call, void *rec,
178 struct ring_buffer_event *event)
180 return filter_check_discard(call, rec, buffer, event);
182 EXPORT_SYMBOL_GPL(filter_current_check_discard);
184 cycle_t ftrace_now(int cpu)
186 u64 ts;
188 /* Early boot up does not have a buffer yet */
189 if (!global_trace.buffer)
190 return trace_clock_local();
192 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
193 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
195 return ts;
199 * The max_tr is used to snapshot the global_trace when a maximum
200 * latency is reached. Some tracers will use this to store a maximum
201 * trace while it continues examining live traces.
203 * The buffers for the max_tr are set up the same as the global_trace.
204 * When a snapshot is taken, the link list of the max_tr is swapped
205 * with the link list of the global_trace and the buffers are reset for
206 * the global_trace so the tracing can continue.
208 static struct trace_array max_tr;
210 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
212 /* tracer_enabled is used to toggle activation of a tracer */
213 static int tracer_enabled = 1;
216 * tracing_is_enabled - return tracer_enabled status
218 * This function is used by other tracers to know the status
219 * of the tracer_enabled flag. Tracers may use this function
220 * to know if it should enable their features when starting
221 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
223 int tracing_is_enabled(void)
225 return tracer_enabled;
229 * trace_buf_size is the size in bytes that is allocated
230 * for a buffer. Note, the number of bytes is always rounded
231 * to page size.
233 * This number is purposely set to a low number of 16384.
234 * If the dump on oops happens, it will be much appreciated
235 * to not have to wait for all that output. Anyway this can be
236 * boot time and run time configurable.
238 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
240 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
242 /* trace_types holds a link list of available tracers. */
243 static struct tracer *trace_types __read_mostly;
245 /* current_trace points to the tracer that is currently active */
246 static struct tracer *current_trace __read_mostly;
249 * trace_types_lock is used to protect the trace_types list.
251 static DEFINE_MUTEX(trace_types_lock);
254 * serialize the access of the ring buffer
256 * ring buffer serializes readers, but it is low level protection.
257 * The validity of the events (which returns by ring_buffer_peek() ..etc)
258 * are not protected by ring buffer.
260 * The content of events may become garbage if we allow other process consumes
261 * these events concurrently:
262 * A) the page of the consumed events may become a normal page
263 * (not reader page) in ring buffer, and this page will be rewrited
264 * by events producer.
265 * B) The page of the consumed events may become a page for splice_read,
266 * and this page will be returned to system.
268 * These primitives allow multi process access to different cpu ring buffer
269 * concurrently.
271 * These primitives don't distinguish read-only and read-consume access.
272 * Multi read-only access are also serialized.
275 #ifdef CONFIG_SMP
276 static DECLARE_RWSEM(all_cpu_access_lock);
277 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
279 static inline void trace_access_lock(int cpu)
281 if (cpu == TRACE_PIPE_ALL_CPU) {
282 /* gain it for accessing the whole ring buffer. */
283 down_write(&all_cpu_access_lock);
284 } else {
285 /* gain it for accessing a cpu ring buffer. */
287 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
288 down_read(&all_cpu_access_lock);
290 /* Secondly block other access to this @cpu ring buffer. */
291 mutex_lock(&per_cpu(cpu_access_lock, cpu));
295 static inline void trace_access_unlock(int cpu)
297 if (cpu == TRACE_PIPE_ALL_CPU) {
298 up_write(&all_cpu_access_lock);
299 } else {
300 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
301 up_read(&all_cpu_access_lock);
305 static inline void trace_access_lock_init(void)
307 int cpu;
309 for_each_possible_cpu(cpu)
310 mutex_init(&per_cpu(cpu_access_lock, cpu));
313 #else
315 static DEFINE_MUTEX(access_lock);
317 static inline void trace_access_lock(int cpu)
319 (void)cpu;
320 mutex_lock(&access_lock);
323 static inline void trace_access_unlock(int cpu)
325 (void)cpu;
326 mutex_unlock(&access_lock);
329 static inline void trace_access_lock_init(void)
333 #endif
335 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
336 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
338 /* trace_flags holds trace_options default values */
339 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
340 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
341 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
342 TRACE_ITER_IRQ_INFO;
344 static int trace_stop_count;
345 static DEFINE_RAW_SPINLOCK(tracing_start_lock);
347 static void wakeup_work_handler(struct work_struct *work)
349 wake_up(&trace_wait);
352 static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler);
355 * trace_wake_up - wake up tasks waiting for trace input
357 * Schedules a delayed work to wake up any task that is blocked on the
358 * trace_wait queue. These is used with trace_poll for tasks polling the
359 * trace.
361 void trace_wake_up(void)
363 const unsigned long delay = msecs_to_jiffies(2);
365 if (trace_flags & TRACE_ITER_BLOCK)
366 return;
367 schedule_delayed_work(&wakeup_work, delay);
370 static int __init set_buf_size(char *str)
372 unsigned long buf_size;
374 if (!str)
375 return 0;
376 buf_size = memparse(str, &str);
377 /* nr_entries can not be zero */
378 if (buf_size == 0)
379 return 0;
380 trace_buf_size = buf_size;
381 return 1;
383 __setup("trace_buf_size=", set_buf_size);
385 static int __init set_tracing_thresh(char *str)
387 unsigned long threshhold;
388 int ret;
390 if (!str)
391 return 0;
392 ret = strict_strtoul(str, 0, &threshhold);
393 if (ret < 0)
394 return 0;
395 tracing_thresh = threshhold * 1000;
396 return 1;
398 __setup("tracing_thresh=", set_tracing_thresh);
400 unsigned long nsecs_to_usecs(unsigned long nsecs)
402 return nsecs / 1000;
405 /* These must match the bit postions in trace_iterator_flags */
406 static const char *trace_options[] = {
407 "print-parent",
408 "sym-offset",
409 "sym-addr",
410 "verbose",
411 "raw",
412 "hex",
413 "bin",
414 "block",
415 "stacktrace",
416 "trace_printk",
417 "ftrace_preempt",
418 "branch",
419 "annotate",
420 "userstacktrace",
421 "sym-userobj",
422 "printk-msg-only",
423 "context-info",
424 "latency-format",
425 "sleep-time",
426 "graph-time",
427 "record-cmd",
428 "overwrite",
429 "disable_on_free",
430 "irq-info",
431 NULL
434 static struct {
435 u64 (*func)(void);
436 const char *name;
437 } trace_clocks[] = {
438 { trace_clock_local, "local" },
439 { trace_clock_global, "global" },
440 { trace_clock_counter, "counter" },
443 int trace_clock_id;
446 * trace_parser_get_init - gets the buffer for trace parser
448 int trace_parser_get_init(struct trace_parser *parser, int size)
450 memset(parser, 0, sizeof(*parser));
452 parser->buffer = kmalloc(size, GFP_KERNEL);
453 if (!parser->buffer)
454 return 1;
456 parser->size = size;
457 return 0;
461 * trace_parser_put - frees the buffer for trace parser
463 void trace_parser_put(struct trace_parser *parser)
465 kfree(parser->buffer);
469 * trace_get_user - reads the user input string separated by space
470 * (matched by isspace(ch))
472 * For each string found the 'struct trace_parser' is updated,
473 * and the function returns.
475 * Returns number of bytes read.
477 * See kernel/trace/trace.h for 'struct trace_parser' details.
479 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
480 size_t cnt, loff_t *ppos)
482 char ch;
483 size_t read = 0;
484 ssize_t ret;
486 if (!*ppos)
487 trace_parser_clear(parser);
489 ret = get_user(ch, ubuf++);
490 if (ret)
491 goto out;
493 read++;
494 cnt--;
497 * The parser is not finished with the last write,
498 * continue reading the user input without skipping spaces.
500 if (!parser->cont) {
501 /* skip white space */
502 while (cnt && isspace(ch)) {
503 ret = get_user(ch, ubuf++);
504 if (ret)
505 goto out;
506 read++;
507 cnt--;
510 /* only spaces were written */
511 if (isspace(ch)) {
512 *ppos += read;
513 ret = read;
514 goto out;
517 parser->idx = 0;
520 /* read the non-space input */
521 while (cnt && !isspace(ch)) {
522 if (parser->idx < parser->size - 1)
523 parser->buffer[parser->idx++] = ch;
524 else {
525 ret = -EINVAL;
526 goto out;
528 ret = get_user(ch, ubuf++);
529 if (ret)
530 goto out;
531 read++;
532 cnt--;
535 /* We either got finished input or we have to wait for another call. */
536 if (isspace(ch)) {
537 parser->buffer[parser->idx] = 0;
538 parser->cont = false;
539 } else {
540 parser->cont = true;
541 parser->buffer[parser->idx++] = ch;
544 *ppos += read;
545 ret = read;
547 out:
548 return ret;
551 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
553 int len;
554 int ret;
556 if (!cnt)
557 return 0;
559 if (s->len <= s->readpos)
560 return -EBUSY;
562 len = s->len - s->readpos;
563 if (cnt > len)
564 cnt = len;
565 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
566 if (ret == cnt)
567 return -EFAULT;
569 cnt -= ret;
571 s->readpos += cnt;
572 return cnt;
575 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
577 int len;
578 void *ret;
580 if (s->len <= s->readpos)
581 return -EBUSY;
583 len = s->len - s->readpos;
584 if (cnt > len)
585 cnt = len;
586 ret = memcpy(buf, s->buffer + s->readpos, cnt);
587 if (!ret)
588 return -EFAULT;
590 s->readpos += cnt;
591 return cnt;
595 * ftrace_max_lock is used to protect the swapping of buffers
596 * when taking a max snapshot. The buffers themselves are
597 * protected by per_cpu spinlocks. But the action of the swap
598 * needs its own lock.
600 * This is defined as a arch_spinlock_t in order to help
601 * with performance when lockdep debugging is enabled.
603 * It is also used in other places outside the update_max_tr
604 * so it needs to be defined outside of the
605 * CONFIG_TRACER_MAX_TRACE.
607 static arch_spinlock_t ftrace_max_lock =
608 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
610 unsigned long __read_mostly tracing_thresh;
612 #ifdef CONFIG_TRACER_MAX_TRACE
613 unsigned long __read_mostly tracing_max_latency;
616 * Copy the new maximum trace into the separate maximum-trace
617 * structure. (this way the maximum trace is permanently saved,
618 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
620 static void
621 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
623 struct trace_array_cpu *data = tr->data[cpu];
624 struct trace_array_cpu *max_data;
626 max_tr.cpu = cpu;
627 max_tr.time_start = data->preempt_timestamp;
629 max_data = max_tr.data[cpu];
630 max_data->saved_latency = tracing_max_latency;
631 max_data->critical_start = data->critical_start;
632 max_data->critical_end = data->critical_end;
634 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
635 max_data->pid = tsk->pid;
636 max_data->uid = task_uid(tsk);
637 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
638 max_data->policy = tsk->policy;
639 max_data->rt_priority = tsk->rt_priority;
641 /* record this tasks comm */
642 tracing_record_cmdline(tsk);
646 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
647 * @tr: tracer
648 * @tsk: the task with the latency
649 * @cpu: The cpu that initiated the trace.
651 * Flip the buffers between the @tr and the max_tr and record information
652 * about which task was the cause of this latency.
654 void
655 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
657 struct ring_buffer *buf = tr->buffer;
659 if (trace_stop_count)
660 return;
662 WARN_ON_ONCE(!irqs_disabled());
663 if (!current_trace->use_max_tr) {
664 WARN_ON_ONCE(1);
665 return;
667 arch_spin_lock(&ftrace_max_lock);
669 tr->buffer = max_tr.buffer;
670 max_tr.buffer = buf;
672 __update_max_tr(tr, tsk, cpu);
673 arch_spin_unlock(&ftrace_max_lock);
677 * update_max_tr_single - only copy one trace over, and reset the rest
678 * @tr - tracer
679 * @tsk - task with the latency
680 * @cpu - the cpu of the buffer to copy.
682 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
684 void
685 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
687 int ret;
689 if (trace_stop_count)
690 return;
692 WARN_ON_ONCE(!irqs_disabled());
693 if (!current_trace->use_max_tr) {
694 WARN_ON_ONCE(1);
695 return;
698 arch_spin_lock(&ftrace_max_lock);
700 ftrace_disable_cpu();
702 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
704 if (ret == -EBUSY) {
706 * We failed to swap the buffer due to a commit taking
707 * place on this CPU. We fail to record, but we reset
708 * the max trace buffer (no one writes directly to it)
709 * and flag that it failed.
711 trace_array_printk(&max_tr, _THIS_IP_,
712 "Failed to swap buffers due to commit in progress\n");
715 ftrace_enable_cpu();
717 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
719 __update_max_tr(tr, tsk, cpu);
720 arch_spin_unlock(&ftrace_max_lock);
722 #endif /* CONFIG_TRACER_MAX_TRACE */
725 * register_tracer - register a tracer with the ftrace system.
726 * @type - the plugin for the tracer
728 * Register a new plugin tracer.
730 int register_tracer(struct tracer *type)
731 __releases(kernel_lock)
732 __acquires(kernel_lock)
734 struct tracer *t;
735 int ret = 0;
737 if (!type->name) {
738 pr_info("Tracer must have a name\n");
739 return -1;
742 if (strlen(type->name) >= MAX_TRACER_SIZE) {
743 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
744 return -1;
747 mutex_lock(&trace_types_lock);
749 tracing_selftest_running = true;
751 for (t = trace_types; t; t = t->next) {
752 if (strcmp(type->name, t->name) == 0) {
753 /* already found */
754 pr_info("Tracer %s already registered\n",
755 type->name);
756 ret = -1;
757 goto out;
761 if (!type->set_flag)
762 type->set_flag = &dummy_set_flag;
763 if (!type->flags)
764 type->flags = &dummy_tracer_flags;
765 else
766 if (!type->flags->opts)
767 type->flags->opts = dummy_tracer_opt;
768 if (!type->wait_pipe)
769 type->wait_pipe = default_wait_pipe;
772 #ifdef CONFIG_FTRACE_STARTUP_TEST
773 if (type->selftest && !tracing_selftest_disabled) {
774 struct tracer *saved_tracer = current_trace;
775 struct trace_array *tr = &global_trace;
778 * Run a selftest on this tracer.
779 * Here we reset the trace buffer, and set the current
780 * tracer to be this tracer. The tracer can then run some
781 * internal tracing to verify that everything is in order.
782 * If we fail, we do not register this tracer.
784 tracing_reset_online_cpus(tr);
786 current_trace = type;
788 /* If we expanded the buffers, make sure the max is expanded too */
789 if (ring_buffer_expanded && type->use_max_tr)
790 ring_buffer_resize(max_tr.buffer, trace_buf_size);
792 /* the test is responsible for initializing and enabling */
793 pr_info("Testing tracer %s: ", type->name);
794 ret = type->selftest(type, tr);
795 /* the test is responsible for resetting too */
796 current_trace = saved_tracer;
797 if (ret) {
798 printk(KERN_CONT "FAILED!\n");
799 goto out;
801 /* Only reset on passing, to avoid touching corrupted buffers */
802 tracing_reset_online_cpus(tr);
804 /* Shrink the max buffer again */
805 if (ring_buffer_expanded && type->use_max_tr)
806 ring_buffer_resize(max_tr.buffer, 1);
808 printk(KERN_CONT "PASSED\n");
810 #endif
812 type->next = trace_types;
813 trace_types = type;
815 out:
816 tracing_selftest_running = false;
817 mutex_unlock(&trace_types_lock);
819 if (ret || !default_bootup_tracer)
820 goto out_unlock;
822 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
823 goto out_unlock;
825 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
826 /* Do we want this tracer to start on bootup? */
827 tracing_set_tracer(type->name);
828 default_bootup_tracer = NULL;
829 /* disable other selftests, since this will break it. */
830 tracing_selftest_disabled = 1;
831 #ifdef CONFIG_FTRACE_STARTUP_TEST
832 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
833 type->name);
834 #endif
836 out_unlock:
837 return ret;
840 void unregister_tracer(struct tracer *type)
842 struct tracer **t;
844 mutex_lock(&trace_types_lock);
845 for (t = &trace_types; *t; t = &(*t)->next) {
846 if (*t == type)
847 goto found;
849 pr_info("Tracer %s not registered\n", type->name);
850 goto out;
852 found:
853 *t = (*t)->next;
855 if (type == current_trace && tracer_enabled) {
856 tracer_enabled = 0;
857 tracing_stop();
858 if (current_trace->stop)
859 current_trace->stop(&global_trace);
860 current_trace = &nop_trace;
862 out:
863 mutex_unlock(&trace_types_lock);
866 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
868 ftrace_disable_cpu();
869 ring_buffer_reset_cpu(buffer, cpu);
870 ftrace_enable_cpu();
873 void tracing_reset(struct trace_array *tr, int cpu)
875 struct ring_buffer *buffer = tr->buffer;
877 ring_buffer_record_disable(buffer);
879 /* Make sure all commits have finished */
880 synchronize_sched();
881 __tracing_reset(buffer, cpu);
883 ring_buffer_record_enable(buffer);
886 void tracing_reset_online_cpus(struct trace_array *tr)
888 struct ring_buffer *buffer = tr->buffer;
889 int cpu;
891 ring_buffer_record_disable(buffer);
893 /* Make sure all commits have finished */
894 synchronize_sched();
896 tr->time_start = ftrace_now(tr->cpu);
898 for_each_online_cpu(cpu)
899 __tracing_reset(buffer, cpu);
901 ring_buffer_record_enable(buffer);
904 void tracing_reset_current(int cpu)
906 tracing_reset(&global_trace, cpu);
909 void tracing_reset_current_online_cpus(void)
911 tracing_reset_online_cpus(&global_trace);
914 #define SAVED_CMDLINES 128
915 #define NO_CMDLINE_MAP UINT_MAX
916 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
917 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
918 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
919 static int cmdline_idx;
920 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
922 /* temporary disable recording */
923 static atomic_t trace_record_cmdline_disabled __read_mostly;
925 static void trace_init_cmdlines(void)
927 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
928 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
929 cmdline_idx = 0;
932 int is_tracing_stopped(void)
934 return trace_stop_count;
938 * ftrace_off_permanent - disable all ftrace code permanently
940 * This should only be called when a serious anomally has
941 * been detected. This will turn off the function tracing,
942 * ring buffers, and other tracing utilites. It takes no
943 * locks and can be called from any context.
945 void ftrace_off_permanent(void)
947 tracing_disabled = 1;
948 ftrace_stop();
949 tracing_off_permanent();
953 * tracing_start - quick start of the tracer
955 * If tracing is enabled but was stopped by tracing_stop,
956 * this will start the tracer back up.
958 void tracing_start(void)
960 struct ring_buffer *buffer;
961 unsigned long flags;
963 if (tracing_disabled)
964 return;
966 raw_spin_lock_irqsave(&tracing_start_lock, flags);
967 if (--trace_stop_count) {
968 if (trace_stop_count < 0) {
969 /* Someone screwed up their debugging */
970 WARN_ON_ONCE(1);
971 trace_stop_count = 0;
973 goto out;
976 /* Prevent the buffers from switching */
977 arch_spin_lock(&ftrace_max_lock);
979 buffer = global_trace.buffer;
980 if (buffer)
981 ring_buffer_record_enable(buffer);
983 buffer = max_tr.buffer;
984 if (buffer)
985 ring_buffer_record_enable(buffer);
987 arch_spin_unlock(&ftrace_max_lock);
989 ftrace_start();
990 out:
991 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
995 * tracing_stop - quick stop of the tracer
997 * Light weight way to stop tracing. Use in conjunction with
998 * tracing_start.
1000 void tracing_stop(void)
1002 struct ring_buffer *buffer;
1003 unsigned long flags;
1005 ftrace_stop();
1006 raw_spin_lock_irqsave(&tracing_start_lock, flags);
1007 if (trace_stop_count++)
1008 goto out;
1010 /* Prevent the buffers from switching */
1011 arch_spin_lock(&ftrace_max_lock);
1013 buffer = global_trace.buffer;
1014 if (buffer)
1015 ring_buffer_record_disable(buffer);
1017 buffer = max_tr.buffer;
1018 if (buffer)
1019 ring_buffer_record_disable(buffer);
1021 arch_spin_unlock(&ftrace_max_lock);
1023 out:
1024 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1027 void trace_stop_cmdline_recording(void);
1029 static void trace_save_cmdline(struct task_struct *tsk)
1031 unsigned pid, idx;
1033 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1034 return;
1037 * It's not the end of the world if we don't get
1038 * the lock, but we also don't want to spin
1039 * nor do we want to disable interrupts,
1040 * so if we miss here, then better luck next time.
1042 if (!arch_spin_trylock(&trace_cmdline_lock))
1043 return;
1045 idx = map_pid_to_cmdline[tsk->pid];
1046 if (idx == NO_CMDLINE_MAP) {
1047 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1050 * Check whether the cmdline buffer at idx has a pid
1051 * mapped. We are going to overwrite that entry so we
1052 * need to clear the map_pid_to_cmdline. Otherwise we
1053 * would read the new comm for the old pid.
1055 pid = map_cmdline_to_pid[idx];
1056 if (pid != NO_CMDLINE_MAP)
1057 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1059 map_cmdline_to_pid[idx] = tsk->pid;
1060 map_pid_to_cmdline[tsk->pid] = idx;
1062 cmdline_idx = idx;
1065 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1067 arch_spin_unlock(&trace_cmdline_lock);
1070 void trace_find_cmdline(int pid, char comm[])
1072 unsigned map;
1074 if (!pid) {
1075 strcpy(comm, "<idle>");
1076 return;
1079 if (WARN_ON_ONCE(pid < 0)) {
1080 strcpy(comm, "<XXX>");
1081 return;
1084 if (pid > PID_MAX_DEFAULT) {
1085 strcpy(comm, "<...>");
1086 return;
1089 preempt_disable();
1090 arch_spin_lock(&trace_cmdline_lock);
1091 map = map_pid_to_cmdline[pid];
1092 if (map != NO_CMDLINE_MAP)
1093 strcpy(comm, saved_cmdlines[map]);
1094 else
1095 strcpy(comm, "<...>");
1097 arch_spin_unlock(&trace_cmdline_lock);
1098 preempt_enable();
1101 void tracing_record_cmdline(struct task_struct *tsk)
1103 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1104 !tracing_is_on())
1105 return;
1107 trace_save_cmdline(tsk);
1110 void
1111 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1112 int pc)
1114 struct task_struct *tsk = current;
1116 entry->preempt_count = pc & 0xff;
1117 entry->pid = (tsk) ? tsk->pid : 0;
1118 entry->padding = 0;
1119 entry->flags =
1120 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1121 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1122 #else
1123 TRACE_FLAG_IRQS_NOSUPPORT |
1124 #endif
1125 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1126 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1127 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1129 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1131 struct ring_buffer_event *
1132 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1133 int type,
1134 unsigned long len,
1135 unsigned long flags, int pc)
1137 struct ring_buffer_event *event;
1139 event = ring_buffer_lock_reserve(buffer, len);
1140 if (event != NULL) {
1141 struct trace_entry *ent = ring_buffer_event_data(event);
1143 tracing_generic_entry_update(ent, flags, pc);
1144 ent->type = type;
1147 return event;
1150 static inline void
1151 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1152 struct ring_buffer_event *event,
1153 unsigned long flags, int pc,
1154 int wake)
1156 ring_buffer_unlock_commit(buffer, event);
1158 ftrace_trace_stack(buffer, flags, 6, pc);
1159 ftrace_trace_userstack(buffer, flags, pc);
1161 if (wake)
1162 trace_wake_up();
1165 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1166 struct ring_buffer_event *event,
1167 unsigned long flags, int pc)
1169 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1172 struct ring_buffer_event *
1173 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1174 int type, unsigned long len,
1175 unsigned long flags, int pc)
1177 *current_rb = global_trace.buffer;
1178 return trace_buffer_lock_reserve(*current_rb,
1179 type, len, flags, pc);
1181 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1183 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1184 struct ring_buffer_event *event,
1185 unsigned long flags, int pc)
1187 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1189 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1191 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1192 struct ring_buffer_event *event,
1193 unsigned long flags, int pc)
1195 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1197 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1199 void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1200 struct ring_buffer_event *event,
1201 unsigned long flags, int pc,
1202 struct pt_regs *regs)
1204 ring_buffer_unlock_commit(buffer, event);
1206 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1207 ftrace_trace_userstack(buffer, flags, pc);
1209 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs);
1211 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1212 struct ring_buffer_event *event)
1214 ring_buffer_discard_commit(buffer, event);
1216 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1218 void
1219 trace_function(struct trace_array *tr,
1220 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1221 int pc)
1223 struct ftrace_event_call *call = &event_function;
1224 struct ring_buffer *buffer = tr->buffer;
1225 struct ring_buffer_event *event;
1226 struct ftrace_entry *entry;
1228 /* If we are reading the ring buffer, don't trace */
1229 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1230 return;
1232 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1233 flags, pc);
1234 if (!event)
1235 return;
1236 entry = ring_buffer_event_data(event);
1237 entry->ip = ip;
1238 entry->parent_ip = parent_ip;
1240 if (!filter_check_discard(call, entry, buffer, event))
1241 ring_buffer_unlock_commit(buffer, event);
1244 void
1245 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1246 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1247 int pc)
1249 if (likely(!atomic_read(&data->disabled)))
1250 trace_function(tr, ip, parent_ip, flags, pc);
1253 #ifdef CONFIG_STACKTRACE
1255 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1256 struct ftrace_stack {
1257 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1260 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1261 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1263 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1264 unsigned long flags,
1265 int skip, int pc, struct pt_regs *regs)
1267 struct ftrace_event_call *call = &event_kernel_stack;
1268 struct ring_buffer_event *event;
1269 struct stack_entry *entry;
1270 struct stack_trace trace;
1271 int use_stack;
1272 int size = FTRACE_STACK_ENTRIES;
1274 trace.nr_entries = 0;
1275 trace.skip = skip;
1278 * Since events can happen in NMIs there's no safe way to
1279 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1280 * or NMI comes in, it will just have to use the default
1281 * FTRACE_STACK_SIZE.
1283 preempt_disable_notrace();
1285 use_stack = ++__get_cpu_var(ftrace_stack_reserve);
1287 * We don't need any atomic variables, just a barrier.
1288 * If an interrupt comes in, we don't care, because it would
1289 * have exited and put the counter back to what we want.
1290 * We just need a barrier to keep gcc from moving things
1291 * around.
1293 barrier();
1294 if (use_stack == 1) {
1295 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1296 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1298 if (regs)
1299 save_stack_trace_regs(regs, &trace);
1300 else
1301 save_stack_trace(&trace);
1303 if (trace.nr_entries > size)
1304 size = trace.nr_entries;
1305 } else
1306 /* From now on, use_stack is a boolean */
1307 use_stack = 0;
1309 size *= sizeof(unsigned long);
1311 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1312 sizeof(*entry) + size, flags, pc);
1313 if (!event)
1314 goto out;
1315 entry = ring_buffer_event_data(event);
1317 memset(&entry->caller, 0, size);
1319 if (use_stack)
1320 memcpy(&entry->caller, trace.entries,
1321 trace.nr_entries * sizeof(unsigned long));
1322 else {
1323 trace.max_entries = FTRACE_STACK_ENTRIES;
1324 trace.entries = entry->caller;
1325 if (regs)
1326 save_stack_trace_regs(regs, &trace);
1327 else
1328 save_stack_trace(&trace);
1331 entry->size = trace.nr_entries;
1333 if (!filter_check_discard(call, entry, buffer, event))
1334 ring_buffer_unlock_commit(buffer, event);
1336 out:
1337 /* Again, don't let gcc optimize things here */
1338 barrier();
1339 __get_cpu_var(ftrace_stack_reserve)--;
1340 preempt_enable_notrace();
1344 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1345 int skip, int pc, struct pt_regs *regs)
1347 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1348 return;
1350 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1353 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1354 int skip, int pc)
1356 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1357 return;
1359 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1362 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1363 int pc)
1365 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
1369 * trace_dump_stack - record a stack back trace in the trace buffer
1371 void trace_dump_stack(void)
1373 unsigned long flags;
1375 if (tracing_disabled || tracing_selftest_running)
1376 return;
1378 local_save_flags(flags);
1380 /* skipping 3 traces, seems to get us at the caller of this function */
1381 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
1384 static DEFINE_PER_CPU(int, user_stack_count);
1386 void
1387 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1389 struct ftrace_event_call *call = &event_user_stack;
1390 struct ring_buffer_event *event;
1391 struct userstack_entry *entry;
1392 struct stack_trace trace;
1394 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1395 return;
1398 * NMIs can not handle page faults, even with fix ups.
1399 * The save user stack can (and often does) fault.
1401 if (unlikely(in_nmi()))
1402 return;
1405 * prevent recursion, since the user stack tracing may
1406 * trigger other kernel events.
1408 preempt_disable();
1409 if (__this_cpu_read(user_stack_count))
1410 goto out;
1412 __this_cpu_inc(user_stack_count);
1414 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1415 sizeof(*entry), flags, pc);
1416 if (!event)
1417 goto out_drop_count;
1418 entry = ring_buffer_event_data(event);
1420 entry->tgid = current->tgid;
1421 memset(&entry->caller, 0, sizeof(entry->caller));
1423 trace.nr_entries = 0;
1424 trace.max_entries = FTRACE_STACK_ENTRIES;
1425 trace.skip = 0;
1426 trace.entries = entry->caller;
1428 save_stack_trace_user(&trace);
1429 if (!filter_check_discard(call, entry, buffer, event))
1430 ring_buffer_unlock_commit(buffer, event);
1432 out_drop_count:
1433 __this_cpu_dec(user_stack_count);
1434 out:
1435 preempt_enable();
1438 #ifdef UNUSED
1439 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1441 ftrace_trace_userstack(tr, flags, preempt_count());
1443 #endif /* UNUSED */
1445 #endif /* CONFIG_STACKTRACE */
1448 * trace_vbprintk - write binary msg to tracing buffer
1451 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1453 static arch_spinlock_t trace_buf_lock =
1454 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1455 static u32 trace_buf[TRACE_BUF_SIZE];
1457 struct ftrace_event_call *call = &event_bprint;
1458 struct ring_buffer_event *event;
1459 struct ring_buffer *buffer;
1460 struct trace_array *tr = &global_trace;
1461 struct trace_array_cpu *data;
1462 struct bprint_entry *entry;
1463 unsigned long flags;
1464 int disable;
1465 int cpu, len = 0, size, pc;
1467 if (unlikely(tracing_selftest_running || tracing_disabled))
1468 return 0;
1470 /* Don't pollute graph traces with trace_vprintk internals */
1471 pause_graph_tracing();
1473 pc = preempt_count();
1474 preempt_disable_notrace();
1475 cpu = raw_smp_processor_id();
1476 data = tr->data[cpu];
1478 disable = atomic_inc_return(&data->disabled);
1479 if (unlikely(disable != 1))
1480 goto out;
1482 /* Lockdep uses trace_printk for lock tracing */
1483 local_irq_save(flags);
1484 arch_spin_lock(&trace_buf_lock);
1485 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1487 if (len > TRACE_BUF_SIZE || len < 0)
1488 goto out_unlock;
1490 size = sizeof(*entry) + sizeof(u32) * len;
1491 buffer = tr->buffer;
1492 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1493 flags, pc);
1494 if (!event)
1495 goto out_unlock;
1496 entry = ring_buffer_event_data(event);
1497 entry->ip = ip;
1498 entry->fmt = fmt;
1500 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1501 if (!filter_check_discard(call, entry, buffer, event)) {
1502 ring_buffer_unlock_commit(buffer, event);
1503 ftrace_trace_stack(buffer, flags, 6, pc);
1506 out_unlock:
1507 arch_spin_unlock(&trace_buf_lock);
1508 local_irq_restore(flags);
1510 out:
1511 atomic_dec_return(&data->disabled);
1512 preempt_enable_notrace();
1513 unpause_graph_tracing();
1515 return len;
1517 EXPORT_SYMBOL_GPL(trace_vbprintk);
1519 int trace_array_printk(struct trace_array *tr,
1520 unsigned long ip, const char *fmt, ...)
1522 int ret;
1523 va_list ap;
1525 if (!(trace_flags & TRACE_ITER_PRINTK))
1526 return 0;
1528 va_start(ap, fmt);
1529 ret = trace_array_vprintk(tr, ip, fmt, ap);
1530 va_end(ap);
1531 return ret;
1534 int trace_array_vprintk(struct trace_array *tr,
1535 unsigned long ip, const char *fmt, va_list args)
1537 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1538 static char trace_buf[TRACE_BUF_SIZE];
1540 struct ftrace_event_call *call = &event_print;
1541 struct ring_buffer_event *event;
1542 struct ring_buffer *buffer;
1543 struct trace_array_cpu *data;
1544 int cpu, len = 0, size, pc;
1545 struct print_entry *entry;
1546 unsigned long irq_flags;
1547 int disable;
1549 if (tracing_disabled || tracing_selftest_running)
1550 return 0;
1552 pc = preempt_count();
1553 preempt_disable_notrace();
1554 cpu = raw_smp_processor_id();
1555 data = tr->data[cpu];
1557 disable = atomic_inc_return(&data->disabled);
1558 if (unlikely(disable != 1))
1559 goto out;
1561 pause_graph_tracing();
1562 raw_local_irq_save(irq_flags);
1563 arch_spin_lock(&trace_buf_lock);
1564 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1566 size = sizeof(*entry) + len + 1;
1567 buffer = tr->buffer;
1568 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1569 irq_flags, pc);
1570 if (!event)
1571 goto out_unlock;
1572 entry = ring_buffer_event_data(event);
1573 entry->ip = ip;
1575 memcpy(&entry->buf, trace_buf, len);
1576 entry->buf[len] = '\0';
1577 if (!filter_check_discard(call, entry, buffer, event)) {
1578 ring_buffer_unlock_commit(buffer, event);
1579 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1582 out_unlock:
1583 arch_spin_unlock(&trace_buf_lock);
1584 raw_local_irq_restore(irq_flags);
1585 unpause_graph_tracing();
1586 out:
1587 atomic_dec_return(&data->disabled);
1588 preempt_enable_notrace();
1590 return len;
1593 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1595 return trace_array_vprintk(&global_trace, ip, fmt, args);
1597 EXPORT_SYMBOL_GPL(trace_vprintk);
1599 static void trace_iterator_increment(struct trace_iterator *iter)
1601 /* Don't allow ftrace to trace into the ring buffers */
1602 ftrace_disable_cpu();
1604 iter->idx++;
1605 if (iter->buffer_iter[iter->cpu])
1606 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1608 ftrace_enable_cpu();
1611 static struct trace_entry *
1612 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1613 unsigned long *lost_events)
1615 struct ring_buffer_event *event;
1616 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1618 /* Don't allow ftrace to trace into the ring buffers */
1619 ftrace_disable_cpu();
1621 if (buf_iter)
1622 event = ring_buffer_iter_peek(buf_iter, ts);
1623 else
1624 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1625 lost_events);
1627 ftrace_enable_cpu();
1629 if (event) {
1630 iter->ent_size = ring_buffer_event_length(event);
1631 return ring_buffer_event_data(event);
1633 iter->ent_size = 0;
1634 return NULL;
1637 static struct trace_entry *
1638 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1639 unsigned long *missing_events, u64 *ent_ts)
1641 struct ring_buffer *buffer = iter->tr->buffer;
1642 struct trace_entry *ent, *next = NULL;
1643 unsigned long lost_events = 0, next_lost = 0;
1644 int cpu_file = iter->cpu_file;
1645 u64 next_ts = 0, ts;
1646 int next_cpu = -1;
1647 int next_size = 0;
1648 int cpu;
1651 * If we are in a per_cpu trace file, don't bother by iterating over
1652 * all cpu and peek directly.
1654 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1655 if (ring_buffer_empty_cpu(buffer, cpu_file))
1656 return NULL;
1657 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1658 if (ent_cpu)
1659 *ent_cpu = cpu_file;
1661 return ent;
1664 for_each_tracing_cpu(cpu) {
1666 if (ring_buffer_empty_cpu(buffer, cpu))
1667 continue;
1669 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1672 * Pick the entry with the smallest timestamp:
1674 if (ent && (!next || ts < next_ts)) {
1675 next = ent;
1676 next_cpu = cpu;
1677 next_ts = ts;
1678 next_lost = lost_events;
1679 next_size = iter->ent_size;
1683 iter->ent_size = next_size;
1685 if (ent_cpu)
1686 *ent_cpu = next_cpu;
1688 if (ent_ts)
1689 *ent_ts = next_ts;
1691 if (missing_events)
1692 *missing_events = next_lost;
1694 return next;
1697 /* Find the next real entry, without updating the iterator itself */
1698 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1699 int *ent_cpu, u64 *ent_ts)
1701 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1704 /* Find the next real entry, and increment the iterator to the next entry */
1705 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1707 iter->ent = __find_next_entry(iter, &iter->cpu,
1708 &iter->lost_events, &iter->ts);
1710 if (iter->ent)
1711 trace_iterator_increment(iter);
1713 return iter->ent ? iter : NULL;
1716 static void trace_consume(struct trace_iterator *iter)
1718 /* Don't allow ftrace to trace into the ring buffers */
1719 ftrace_disable_cpu();
1720 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1721 &iter->lost_events);
1722 ftrace_enable_cpu();
1725 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1727 struct trace_iterator *iter = m->private;
1728 int i = (int)*pos;
1729 void *ent;
1731 WARN_ON_ONCE(iter->leftover);
1733 (*pos)++;
1735 /* can't go backwards */
1736 if (iter->idx > i)
1737 return NULL;
1739 if (iter->idx < 0)
1740 ent = trace_find_next_entry_inc(iter);
1741 else
1742 ent = iter;
1744 while (ent && iter->idx < i)
1745 ent = trace_find_next_entry_inc(iter);
1747 iter->pos = *pos;
1749 return ent;
1752 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1754 struct trace_array *tr = iter->tr;
1755 struct ring_buffer_event *event;
1756 struct ring_buffer_iter *buf_iter;
1757 unsigned long entries = 0;
1758 u64 ts;
1760 tr->data[cpu]->skipped_entries = 0;
1762 if (!iter->buffer_iter[cpu])
1763 return;
1765 buf_iter = iter->buffer_iter[cpu];
1766 ring_buffer_iter_reset(buf_iter);
1769 * We could have the case with the max latency tracers
1770 * that a reset never took place on a cpu. This is evident
1771 * by the timestamp being before the start of the buffer.
1773 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1774 if (ts >= iter->tr->time_start)
1775 break;
1776 entries++;
1777 ring_buffer_read(buf_iter, NULL);
1780 tr->data[cpu]->skipped_entries = entries;
1784 * The current tracer is copied to avoid a global locking
1785 * all around.
1787 static void *s_start(struct seq_file *m, loff_t *pos)
1789 struct trace_iterator *iter = m->private;
1790 static struct tracer *old_tracer;
1791 int cpu_file = iter->cpu_file;
1792 void *p = NULL;
1793 loff_t l = 0;
1794 int cpu;
1796 /* copy the tracer to avoid using a global lock all around */
1797 mutex_lock(&trace_types_lock);
1798 if (unlikely(old_tracer != current_trace && current_trace)) {
1799 old_tracer = current_trace;
1800 *iter->trace = *current_trace;
1802 mutex_unlock(&trace_types_lock);
1804 atomic_inc(&trace_record_cmdline_disabled);
1806 if (*pos != iter->pos) {
1807 iter->ent = NULL;
1808 iter->cpu = 0;
1809 iter->idx = -1;
1811 ftrace_disable_cpu();
1813 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1814 for_each_tracing_cpu(cpu)
1815 tracing_iter_reset(iter, cpu);
1816 } else
1817 tracing_iter_reset(iter, cpu_file);
1819 ftrace_enable_cpu();
1821 iter->leftover = 0;
1822 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1825 } else {
1827 * If we overflowed the seq_file before, then we want
1828 * to just reuse the trace_seq buffer again.
1830 if (iter->leftover)
1831 p = iter;
1832 else {
1833 l = *pos - 1;
1834 p = s_next(m, p, &l);
1838 trace_event_read_lock();
1839 trace_access_lock(cpu_file);
1840 return p;
1843 static void s_stop(struct seq_file *m, void *p)
1845 struct trace_iterator *iter = m->private;
1847 atomic_dec(&trace_record_cmdline_disabled);
1848 trace_access_unlock(iter->cpu_file);
1849 trace_event_read_unlock();
1852 static void
1853 get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *entries)
1855 unsigned long count;
1856 int cpu;
1858 *total = 0;
1859 *entries = 0;
1861 for_each_tracing_cpu(cpu) {
1862 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1864 * If this buffer has skipped entries, then we hold all
1865 * entries for the trace and we need to ignore the
1866 * ones before the time stamp.
1868 if (tr->data[cpu]->skipped_entries) {
1869 count -= tr->data[cpu]->skipped_entries;
1870 /* total is the same as the entries */
1871 *total += count;
1872 } else
1873 *total += count +
1874 ring_buffer_overrun_cpu(tr->buffer, cpu);
1875 *entries += count;
1879 static void print_lat_help_header(struct seq_file *m)
1881 seq_puts(m, "# _------=> CPU# \n");
1882 seq_puts(m, "# / _-----=> irqs-off \n");
1883 seq_puts(m, "# | / _----=> need-resched \n");
1884 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1885 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1886 seq_puts(m, "# |||| / delay \n");
1887 seq_puts(m, "# cmd pid ||||| time | caller \n");
1888 seq_puts(m, "# \\ / ||||| \\ | / \n");
1891 static void print_event_info(struct trace_array *tr, struct seq_file *m)
1893 unsigned long total;
1894 unsigned long entries;
1896 get_total_entries(tr, &total, &entries);
1897 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
1898 entries, total, num_online_cpus());
1899 seq_puts(m, "#\n");
1902 static void print_func_help_header(struct trace_array *tr, struct seq_file *m)
1904 print_event_info(tr, m);
1905 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1906 seq_puts(m, "# | | | | |\n");
1909 static void print_func_help_header_irq(struct trace_array *tr, struct seq_file *m)
1911 print_event_info(tr, m);
1912 seq_puts(m, "# _-----=> irqs-off\n");
1913 seq_puts(m, "# / _----=> need-resched\n");
1914 seq_puts(m, "# | / _---=> hardirq/softirq\n");
1915 seq_puts(m, "# || / _--=> preempt-depth\n");
1916 seq_puts(m, "# ||| / delay\n");
1917 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
1918 seq_puts(m, "# | | | |||| | |\n");
1921 void
1922 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1924 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1925 struct trace_array *tr = iter->tr;
1926 struct trace_array_cpu *data = tr->data[tr->cpu];
1927 struct tracer *type = current_trace;
1928 unsigned long entries;
1929 unsigned long total;
1930 const char *name = "preemption";
1932 if (type)
1933 name = type->name;
1935 get_total_entries(tr, &total, &entries);
1937 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1938 name, UTS_RELEASE);
1939 seq_puts(m, "# -----------------------------------"
1940 "---------------------------------\n");
1941 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1942 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1943 nsecs_to_usecs(data->saved_latency),
1944 entries,
1945 total,
1946 tr->cpu,
1947 #if defined(CONFIG_PREEMPT_NONE)
1948 "server",
1949 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1950 "desktop",
1951 #elif defined(CONFIG_PREEMPT)
1952 "preempt",
1953 #else
1954 "unknown",
1955 #endif
1956 /* These are reserved for later use */
1957 0, 0, 0, 0);
1958 #ifdef CONFIG_SMP
1959 seq_printf(m, " #P:%d)\n", num_online_cpus());
1960 #else
1961 seq_puts(m, ")\n");
1962 #endif
1963 seq_puts(m, "# -----------------\n");
1964 seq_printf(m, "# | task: %.16s-%d "
1965 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1966 data->comm, data->pid, data->uid, data->nice,
1967 data->policy, data->rt_priority);
1968 seq_puts(m, "# -----------------\n");
1970 if (data->critical_start) {
1971 seq_puts(m, "# => started at: ");
1972 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1973 trace_print_seq(m, &iter->seq);
1974 seq_puts(m, "\n# => ended at: ");
1975 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1976 trace_print_seq(m, &iter->seq);
1977 seq_puts(m, "\n#\n");
1980 seq_puts(m, "#\n");
1983 static void test_cpu_buff_start(struct trace_iterator *iter)
1985 struct trace_seq *s = &iter->seq;
1987 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1988 return;
1990 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1991 return;
1993 if (cpumask_test_cpu(iter->cpu, iter->started))
1994 return;
1996 if (iter->tr->data[iter->cpu]->skipped_entries)
1997 return;
1999 cpumask_set_cpu(iter->cpu, iter->started);
2001 /* Don't print started cpu buffer for the first entry of the trace */
2002 if (iter->idx > 1)
2003 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2004 iter->cpu);
2007 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2009 struct trace_seq *s = &iter->seq;
2010 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2011 struct trace_entry *entry;
2012 struct trace_event *event;
2014 entry = iter->ent;
2016 test_cpu_buff_start(iter);
2018 event = ftrace_find_event(entry->type);
2020 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2021 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2022 if (!trace_print_lat_context(iter))
2023 goto partial;
2024 } else {
2025 if (!trace_print_context(iter))
2026 goto partial;
2030 if (event)
2031 return event->funcs->trace(iter, sym_flags, event);
2033 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2034 goto partial;
2036 return TRACE_TYPE_HANDLED;
2037 partial:
2038 return TRACE_TYPE_PARTIAL_LINE;
2041 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2043 struct trace_seq *s = &iter->seq;
2044 struct trace_entry *entry;
2045 struct trace_event *event;
2047 entry = iter->ent;
2049 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2050 if (!trace_seq_printf(s, "%d %d %llu ",
2051 entry->pid, iter->cpu, iter->ts))
2052 goto partial;
2055 event = ftrace_find_event(entry->type);
2056 if (event)
2057 return event->funcs->raw(iter, 0, event);
2059 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2060 goto partial;
2062 return TRACE_TYPE_HANDLED;
2063 partial:
2064 return TRACE_TYPE_PARTIAL_LINE;
2067 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2069 struct trace_seq *s = &iter->seq;
2070 unsigned char newline = '\n';
2071 struct trace_entry *entry;
2072 struct trace_event *event;
2074 entry = iter->ent;
2076 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2077 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2078 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2079 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2082 event = ftrace_find_event(entry->type);
2083 if (event) {
2084 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2085 if (ret != TRACE_TYPE_HANDLED)
2086 return ret;
2089 SEQ_PUT_FIELD_RET(s, newline);
2091 return TRACE_TYPE_HANDLED;
2094 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2096 struct trace_seq *s = &iter->seq;
2097 struct trace_entry *entry;
2098 struct trace_event *event;
2100 entry = iter->ent;
2102 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2103 SEQ_PUT_FIELD_RET(s, entry->pid);
2104 SEQ_PUT_FIELD_RET(s, iter->cpu);
2105 SEQ_PUT_FIELD_RET(s, iter->ts);
2108 event = ftrace_find_event(entry->type);
2109 return event ? event->funcs->binary(iter, 0, event) :
2110 TRACE_TYPE_HANDLED;
2113 int trace_empty(struct trace_iterator *iter)
2115 int cpu;
2117 /* If we are looking at one CPU buffer, only check that one */
2118 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2119 cpu = iter->cpu_file;
2120 if (iter->buffer_iter[cpu]) {
2121 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2122 return 0;
2123 } else {
2124 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2125 return 0;
2127 return 1;
2130 for_each_tracing_cpu(cpu) {
2131 if (iter->buffer_iter[cpu]) {
2132 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2133 return 0;
2134 } else {
2135 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2136 return 0;
2140 return 1;
2143 /* Called with trace_event_read_lock() held. */
2144 enum print_line_t print_trace_line(struct trace_iterator *iter)
2146 enum print_line_t ret;
2148 if (iter->lost_events &&
2149 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2150 iter->cpu, iter->lost_events))
2151 return TRACE_TYPE_PARTIAL_LINE;
2153 if (iter->trace && iter->trace->print_line) {
2154 ret = iter->trace->print_line(iter);
2155 if (ret != TRACE_TYPE_UNHANDLED)
2156 return ret;
2159 if (iter->ent->type == TRACE_BPRINT &&
2160 trace_flags & TRACE_ITER_PRINTK &&
2161 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2162 return trace_print_bprintk_msg_only(iter);
2164 if (iter->ent->type == TRACE_PRINT &&
2165 trace_flags & TRACE_ITER_PRINTK &&
2166 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2167 return trace_print_printk_msg_only(iter);
2169 if (trace_flags & TRACE_ITER_BIN)
2170 return print_bin_fmt(iter);
2172 if (trace_flags & TRACE_ITER_HEX)
2173 return print_hex_fmt(iter);
2175 if (trace_flags & TRACE_ITER_RAW)
2176 return print_raw_fmt(iter);
2178 return print_trace_fmt(iter);
2181 void trace_latency_header(struct seq_file *m)
2183 struct trace_iterator *iter = m->private;
2185 /* print nothing if the buffers are empty */
2186 if (trace_empty(iter))
2187 return;
2189 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2190 print_trace_header(m, iter);
2192 if (!(trace_flags & TRACE_ITER_VERBOSE))
2193 print_lat_help_header(m);
2196 void trace_default_header(struct seq_file *m)
2198 struct trace_iterator *iter = m->private;
2200 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2201 return;
2203 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2204 /* print nothing if the buffers are empty */
2205 if (trace_empty(iter))
2206 return;
2207 print_trace_header(m, iter);
2208 if (!(trace_flags & TRACE_ITER_VERBOSE))
2209 print_lat_help_header(m);
2210 } else {
2211 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2212 if (trace_flags & TRACE_ITER_IRQ_INFO)
2213 print_func_help_header_irq(iter->tr, m);
2214 else
2215 print_func_help_header(iter->tr, m);
2220 static void test_ftrace_alive(struct seq_file *m)
2222 if (!ftrace_is_dead())
2223 return;
2224 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2225 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2228 static int s_show(struct seq_file *m, void *v)
2230 struct trace_iterator *iter = v;
2231 int ret;
2233 if (iter->ent == NULL) {
2234 if (iter->tr) {
2235 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2236 seq_puts(m, "#\n");
2237 test_ftrace_alive(m);
2239 if (iter->trace && iter->trace->print_header)
2240 iter->trace->print_header(m);
2241 else
2242 trace_default_header(m);
2244 } else if (iter->leftover) {
2246 * If we filled the seq_file buffer earlier, we
2247 * want to just show it now.
2249 ret = trace_print_seq(m, &iter->seq);
2251 /* ret should this time be zero, but you never know */
2252 iter->leftover = ret;
2254 } else {
2255 print_trace_line(iter);
2256 ret = trace_print_seq(m, &iter->seq);
2258 * If we overflow the seq_file buffer, then it will
2259 * ask us for this data again at start up.
2260 * Use that instead.
2261 * ret is 0 if seq_file write succeeded.
2262 * -1 otherwise.
2264 iter->leftover = ret;
2267 return 0;
2270 static const struct seq_operations tracer_seq_ops = {
2271 .start = s_start,
2272 .next = s_next,
2273 .stop = s_stop,
2274 .show = s_show,
2277 static struct trace_iterator *
2278 __tracing_open(struct inode *inode, struct file *file)
2280 long cpu_file = (long) inode->i_private;
2281 void *fail_ret = ERR_PTR(-ENOMEM);
2282 struct trace_iterator *iter;
2283 struct seq_file *m;
2284 int cpu, ret;
2286 if (tracing_disabled)
2287 return ERR_PTR(-ENODEV);
2289 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2290 if (!iter)
2291 return ERR_PTR(-ENOMEM);
2294 * We make a copy of the current tracer to avoid concurrent
2295 * changes on it while we are reading.
2297 mutex_lock(&trace_types_lock);
2298 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2299 if (!iter->trace)
2300 goto fail;
2302 if (current_trace)
2303 *iter->trace = *current_trace;
2305 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2306 goto fail;
2308 if (current_trace && current_trace->print_max)
2309 iter->tr = &max_tr;
2310 else
2311 iter->tr = &global_trace;
2312 iter->pos = -1;
2313 mutex_init(&iter->mutex);
2314 iter->cpu_file = cpu_file;
2316 /* Notify the tracer early; before we stop tracing. */
2317 if (iter->trace && iter->trace->open)
2318 iter->trace->open(iter);
2320 /* Annotate start of buffers if we had overruns */
2321 if (ring_buffer_overruns(iter->tr->buffer))
2322 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2324 /* stop the trace while dumping */
2325 tracing_stop();
2327 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2328 for_each_tracing_cpu(cpu) {
2329 iter->buffer_iter[cpu] =
2330 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2332 ring_buffer_read_prepare_sync();
2333 for_each_tracing_cpu(cpu) {
2334 ring_buffer_read_start(iter->buffer_iter[cpu]);
2335 tracing_iter_reset(iter, cpu);
2337 } else {
2338 cpu = iter->cpu_file;
2339 iter->buffer_iter[cpu] =
2340 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2341 ring_buffer_read_prepare_sync();
2342 ring_buffer_read_start(iter->buffer_iter[cpu]);
2343 tracing_iter_reset(iter, cpu);
2346 ret = seq_open(file, &tracer_seq_ops);
2347 if (ret < 0) {
2348 fail_ret = ERR_PTR(ret);
2349 goto fail_buffer;
2352 m = file->private_data;
2353 m->private = iter;
2355 mutex_unlock(&trace_types_lock);
2357 return iter;
2359 fail_buffer:
2360 for_each_tracing_cpu(cpu) {
2361 if (iter->buffer_iter[cpu])
2362 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2364 free_cpumask_var(iter->started);
2365 tracing_start();
2366 fail:
2367 mutex_unlock(&trace_types_lock);
2368 kfree(iter->trace);
2369 kfree(iter);
2371 return fail_ret;
2374 int tracing_open_generic(struct inode *inode, struct file *filp)
2376 if (tracing_disabled)
2377 return -ENODEV;
2379 filp->private_data = inode->i_private;
2380 return 0;
2383 static int tracing_release(struct inode *inode, struct file *file)
2385 struct seq_file *m = file->private_data;
2386 struct trace_iterator *iter;
2387 int cpu;
2389 if (!(file->f_mode & FMODE_READ))
2390 return 0;
2392 iter = m->private;
2394 mutex_lock(&trace_types_lock);
2395 for_each_tracing_cpu(cpu) {
2396 if (iter->buffer_iter[cpu])
2397 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2400 if (iter->trace && iter->trace->close)
2401 iter->trace->close(iter);
2403 /* reenable tracing if it was previously enabled */
2404 tracing_start();
2405 mutex_unlock(&trace_types_lock);
2407 seq_release(inode, file);
2408 mutex_destroy(&iter->mutex);
2409 free_cpumask_var(iter->started);
2410 kfree(iter->trace);
2411 kfree(iter);
2412 return 0;
2415 static int tracing_open(struct inode *inode, struct file *file)
2417 struct trace_iterator *iter;
2418 int ret = 0;
2420 /* If this file was open for write, then erase contents */
2421 if ((file->f_mode & FMODE_WRITE) &&
2422 (file->f_flags & O_TRUNC)) {
2423 long cpu = (long) inode->i_private;
2425 if (cpu == TRACE_PIPE_ALL_CPU)
2426 tracing_reset_online_cpus(&global_trace);
2427 else
2428 tracing_reset(&global_trace, cpu);
2431 if (file->f_mode & FMODE_READ) {
2432 iter = __tracing_open(inode, file);
2433 if (IS_ERR(iter))
2434 ret = PTR_ERR(iter);
2435 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2436 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2438 return ret;
2441 static void *
2442 t_next(struct seq_file *m, void *v, loff_t *pos)
2444 struct tracer *t = v;
2446 (*pos)++;
2448 if (t)
2449 t = t->next;
2451 return t;
2454 static void *t_start(struct seq_file *m, loff_t *pos)
2456 struct tracer *t;
2457 loff_t l = 0;
2459 mutex_lock(&trace_types_lock);
2460 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2463 return t;
2466 static void t_stop(struct seq_file *m, void *p)
2468 mutex_unlock(&trace_types_lock);
2471 static int t_show(struct seq_file *m, void *v)
2473 struct tracer *t = v;
2475 if (!t)
2476 return 0;
2478 seq_printf(m, "%s", t->name);
2479 if (t->next)
2480 seq_putc(m, ' ');
2481 else
2482 seq_putc(m, '\n');
2484 return 0;
2487 static const struct seq_operations show_traces_seq_ops = {
2488 .start = t_start,
2489 .next = t_next,
2490 .stop = t_stop,
2491 .show = t_show,
2494 static int show_traces_open(struct inode *inode, struct file *file)
2496 if (tracing_disabled)
2497 return -ENODEV;
2499 return seq_open(file, &show_traces_seq_ops);
2502 static ssize_t
2503 tracing_write_stub(struct file *filp, const char __user *ubuf,
2504 size_t count, loff_t *ppos)
2506 return count;
2509 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2511 if (file->f_mode & FMODE_READ)
2512 return seq_lseek(file, offset, origin);
2513 else
2514 return 0;
2517 static const struct file_operations tracing_fops = {
2518 .open = tracing_open,
2519 .read = seq_read,
2520 .write = tracing_write_stub,
2521 .llseek = tracing_seek,
2522 .release = tracing_release,
2525 static const struct file_operations show_traces_fops = {
2526 .open = show_traces_open,
2527 .read = seq_read,
2528 .release = seq_release,
2529 .llseek = seq_lseek,
2533 * Only trace on a CPU if the bitmask is set:
2535 static cpumask_var_t tracing_cpumask;
2538 * The tracer itself will not take this lock, but still we want
2539 * to provide a consistent cpumask to user-space:
2541 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2544 * Temporary storage for the character representation of the
2545 * CPU bitmask (and one more byte for the newline):
2547 static char mask_str[NR_CPUS + 1];
2549 static ssize_t
2550 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2551 size_t count, loff_t *ppos)
2553 int len;
2555 mutex_lock(&tracing_cpumask_update_lock);
2557 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2558 if (count - len < 2) {
2559 count = -EINVAL;
2560 goto out_err;
2562 len += sprintf(mask_str + len, "\n");
2563 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2565 out_err:
2566 mutex_unlock(&tracing_cpumask_update_lock);
2568 return count;
2571 static ssize_t
2572 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2573 size_t count, loff_t *ppos)
2575 int err, cpu;
2576 cpumask_var_t tracing_cpumask_new;
2578 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2579 return -ENOMEM;
2581 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2582 if (err)
2583 goto err_unlock;
2585 mutex_lock(&tracing_cpumask_update_lock);
2587 local_irq_disable();
2588 arch_spin_lock(&ftrace_max_lock);
2589 for_each_tracing_cpu(cpu) {
2591 * Increase/decrease the disabled counter if we are
2592 * about to flip a bit in the cpumask:
2594 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2595 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2596 atomic_inc(&global_trace.data[cpu]->disabled);
2598 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2599 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2600 atomic_dec(&global_trace.data[cpu]->disabled);
2603 arch_spin_unlock(&ftrace_max_lock);
2604 local_irq_enable();
2606 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2608 mutex_unlock(&tracing_cpumask_update_lock);
2609 free_cpumask_var(tracing_cpumask_new);
2611 return count;
2613 err_unlock:
2614 free_cpumask_var(tracing_cpumask_new);
2616 return err;
2619 static const struct file_operations tracing_cpumask_fops = {
2620 .open = tracing_open_generic,
2621 .read = tracing_cpumask_read,
2622 .write = tracing_cpumask_write,
2623 .llseek = generic_file_llseek,
2626 static int tracing_trace_options_show(struct seq_file *m, void *v)
2628 struct tracer_opt *trace_opts;
2629 u32 tracer_flags;
2630 int i;
2632 mutex_lock(&trace_types_lock);
2633 tracer_flags = current_trace->flags->val;
2634 trace_opts = current_trace->flags->opts;
2636 for (i = 0; trace_options[i]; i++) {
2637 if (trace_flags & (1 << i))
2638 seq_printf(m, "%s\n", trace_options[i]);
2639 else
2640 seq_printf(m, "no%s\n", trace_options[i]);
2643 for (i = 0; trace_opts[i].name; i++) {
2644 if (tracer_flags & trace_opts[i].bit)
2645 seq_printf(m, "%s\n", trace_opts[i].name);
2646 else
2647 seq_printf(m, "no%s\n", trace_opts[i].name);
2649 mutex_unlock(&trace_types_lock);
2651 return 0;
2654 static int __set_tracer_option(struct tracer *trace,
2655 struct tracer_flags *tracer_flags,
2656 struct tracer_opt *opts, int neg)
2658 int ret;
2660 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2661 if (ret)
2662 return ret;
2664 if (neg)
2665 tracer_flags->val &= ~opts->bit;
2666 else
2667 tracer_flags->val |= opts->bit;
2668 return 0;
2671 /* Try to assign a tracer specific option */
2672 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2674 struct tracer_flags *tracer_flags = trace->flags;
2675 struct tracer_opt *opts = NULL;
2676 int i;
2678 for (i = 0; tracer_flags->opts[i].name; i++) {
2679 opts = &tracer_flags->opts[i];
2681 if (strcmp(cmp, opts->name) == 0)
2682 return __set_tracer_option(trace, trace->flags,
2683 opts, neg);
2686 return -EINVAL;
2689 static void set_tracer_flags(unsigned int mask, int enabled)
2691 /* do nothing if flag is already set */
2692 if (!!(trace_flags & mask) == !!enabled)
2693 return;
2695 if (enabled)
2696 trace_flags |= mask;
2697 else
2698 trace_flags &= ~mask;
2700 if (mask == TRACE_ITER_RECORD_CMD)
2701 trace_event_enable_cmd_record(enabled);
2703 if (mask == TRACE_ITER_OVERWRITE)
2704 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2707 static ssize_t
2708 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2709 size_t cnt, loff_t *ppos)
2711 char buf[64];
2712 char *cmp;
2713 int neg = 0;
2714 int ret;
2715 int i;
2717 if (cnt >= sizeof(buf))
2718 return -EINVAL;
2720 if (copy_from_user(&buf, ubuf, cnt))
2721 return -EFAULT;
2723 buf[cnt] = 0;
2724 cmp = strstrip(buf);
2726 if (strncmp(cmp, "no", 2) == 0) {
2727 neg = 1;
2728 cmp += 2;
2731 for (i = 0; trace_options[i]; i++) {
2732 if (strcmp(cmp, trace_options[i]) == 0) {
2733 set_tracer_flags(1 << i, !neg);
2734 break;
2738 /* If no option could be set, test the specific tracer options */
2739 if (!trace_options[i]) {
2740 mutex_lock(&trace_types_lock);
2741 ret = set_tracer_option(current_trace, cmp, neg);
2742 mutex_unlock(&trace_types_lock);
2743 if (ret)
2744 return ret;
2747 *ppos += cnt;
2749 return cnt;
2752 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2754 if (tracing_disabled)
2755 return -ENODEV;
2756 return single_open(file, tracing_trace_options_show, NULL);
2759 static const struct file_operations tracing_iter_fops = {
2760 .open = tracing_trace_options_open,
2761 .read = seq_read,
2762 .llseek = seq_lseek,
2763 .release = single_release,
2764 .write = tracing_trace_options_write,
2767 static const char readme_msg[] =
2768 "tracing mini-HOWTO:\n\n"
2769 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2770 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2771 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2772 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2773 "nop\n"
2774 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2775 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2776 "sched_switch\n"
2777 "# cat /sys/kernel/debug/tracing/trace_options\n"
2778 "noprint-parent nosym-offset nosym-addr noverbose\n"
2779 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2780 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
2781 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2782 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
2785 static ssize_t
2786 tracing_readme_read(struct file *filp, char __user *ubuf,
2787 size_t cnt, loff_t *ppos)
2789 return simple_read_from_buffer(ubuf, cnt, ppos,
2790 readme_msg, strlen(readme_msg));
2793 static const struct file_operations tracing_readme_fops = {
2794 .open = tracing_open_generic,
2795 .read = tracing_readme_read,
2796 .llseek = generic_file_llseek,
2799 static ssize_t
2800 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2801 size_t cnt, loff_t *ppos)
2803 char *buf_comm;
2804 char *file_buf;
2805 char *buf;
2806 int len = 0;
2807 int pid;
2808 int i;
2810 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2811 if (!file_buf)
2812 return -ENOMEM;
2814 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2815 if (!buf_comm) {
2816 kfree(file_buf);
2817 return -ENOMEM;
2820 buf = file_buf;
2822 for (i = 0; i < SAVED_CMDLINES; i++) {
2823 int r;
2825 pid = map_cmdline_to_pid[i];
2826 if (pid == -1 || pid == NO_CMDLINE_MAP)
2827 continue;
2829 trace_find_cmdline(pid, buf_comm);
2830 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2831 buf += r;
2832 len += r;
2835 len = simple_read_from_buffer(ubuf, cnt, ppos,
2836 file_buf, len);
2838 kfree(file_buf);
2839 kfree(buf_comm);
2841 return len;
2844 static const struct file_operations tracing_saved_cmdlines_fops = {
2845 .open = tracing_open_generic,
2846 .read = tracing_saved_cmdlines_read,
2847 .llseek = generic_file_llseek,
2850 static ssize_t
2851 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2852 size_t cnt, loff_t *ppos)
2854 char buf[64];
2855 int r;
2857 r = sprintf(buf, "%u\n", tracer_enabled);
2858 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2861 static ssize_t
2862 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2863 size_t cnt, loff_t *ppos)
2865 struct trace_array *tr = filp->private_data;
2866 unsigned long val;
2867 int ret;
2869 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
2870 if (ret)
2871 return ret;
2873 val = !!val;
2875 mutex_lock(&trace_types_lock);
2876 if (tracer_enabled ^ val) {
2878 /* Only need to warn if this is used to change the state */
2879 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
2881 if (val) {
2882 tracer_enabled = 1;
2883 if (current_trace->start)
2884 current_trace->start(tr);
2885 tracing_start();
2886 } else {
2887 tracer_enabled = 0;
2888 tracing_stop();
2889 if (current_trace->stop)
2890 current_trace->stop(tr);
2893 mutex_unlock(&trace_types_lock);
2895 *ppos += cnt;
2897 return cnt;
2900 static ssize_t
2901 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2902 size_t cnt, loff_t *ppos)
2904 char buf[MAX_TRACER_SIZE+2];
2905 int r;
2907 mutex_lock(&trace_types_lock);
2908 if (current_trace)
2909 r = sprintf(buf, "%s\n", current_trace->name);
2910 else
2911 r = sprintf(buf, "\n");
2912 mutex_unlock(&trace_types_lock);
2914 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2917 int tracer_init(struct tracer *t, struct trace_array *tr)
2919 tracing_reset_online_cpus(tr);
2920 return t->init(tr);
2923 static int __tracing_resize_ring_buffer(unsigned long size)
2925 int ret;
2928 * If kernel or user changes the size of the ring buffer
2929 * we use the size that was given, and we can forget about
2930 * expanding it later.
2932 ring_buffer_expanded = 1;
2934 ret = ring_buffer_resize(global_trace.buffer, size);
2935 if (ret < 0)
2936 return ret;
2938 if (!current_trace->use_max_tr)
2939 goto out;
2941 ret = ring_buffer_resize(max_tr.buffer, size);
2942 if (ret < 0) {
2943 int r;
2945 r = ring_buffer_resize(global_trace.buffer,
2946 global_trace.entries);
2947 if (r < 0) {
2949 * AARGH! We are left with different
2950 * size max buffer!!!!
2951 * The max buffer is our "snapshot" buffer.
2952 * When a tracer needs a snapshot (one of the
2953 * latency tracers), it swaps the max buffer
2954 * with the saved snap shot. We succeeded to
2955 * update the size of the main buffer, but failed to
2956 * update the size of the max buffer. But when we tried
2957 * to reset the main buffer to the original size, we
2958 * failed there too. This is very unlikely to
2959 * happen, but if it does, warn and kill all
2960 * tracing.
2962 WARN_ON(1);
2963 tracing_disabled = 1;
2965 return ret;
2968 max_tr.entries = size;
2969 out:
2970 global_trace.entries = size;
2972 return ret;
2975 static ssize_t tracing_resize_ring_buffer(unsigned long size)
2977 int cpu, ret = size;
2979 mutex_lock(&trace_types_lock);
2981 tracing_stop();
2983 /* disable all cpu buffers */
2984 for_each_tracing_cpu(cpu) {
2985 if (global_trace.data[cpu])
2986 atomic_inc(&global_trace.data[cpu]->disabled);
2987 if (max_tr.data[cpu])
2988 atomic_inc(&max_tr.data[cpu]->disabled);
2991 if (size != global_trace.entries)
2992 ret = __tracing_resize_ring_buffer(size);
2994 if (ret < 0)
2995 ret = -ENOMEM;
2997 for_each_tracing_cpu(cpu) {
2998 if (global_trace.data[cpu])
2999 atomic_dec(&global_trace.data[cpu]->disabled);
3000 if (max_tr.data[cpu])
3001 atomic_dec(&max_tr.data[cpu]->disabled);
3004 tracing_start();
3005 mutex_unlock(&trace_types_lock);
3007 return ret;
3012 * tracing_update_buffers - used by tracing facility to expand ring buffers
3014 * To save on memory when the tracing is never used on a system with it
3015 * configured in. The ring buffers are set to a minimum size. But once
3016 * a user starts to use the tracing facility, then they need to grow
3017 * to their default size.
3019 * This function is to be called when a tracer is about to be used.
3021 int tracing_update_buffers(void)
3023 int ret = 0;
3025 mutex_lock(&trace_types_lock);
3026 if (!ring_buffer_expanded)
3027 ret = __tracing_resize_ring_buffer(trace_buf_size);
3028 mutex_unlock(&trace_types_lock);
3030 return ret;
3033 struct trace_option_dentry;
3035 static struct trace_option_dentry *
3036 create_trace_option_files(struct tracer *tracer);
3038 static void
3039 destroy_trace_option_files(struct trace_option_dentry *topts);
3041 static int tracing_set_tracer(const char *buf)
3043 static struct trace_option_dentry *topts;
3044 struct trace_array *tr = &global_trace;
3045 struct tracer *t;
3046 int ret = 0;
3048 mutex_lock(&trace_types_lock);
3050 if (!ring_buffer_expanded) {
3051 ret = __tracing_resize_ring_buffer(trace_buf_size);
3052 if (ret < 0)
3053 goto out;
3054 ret = 0;
3057 for (t = trace_types; t; t = t->next) {
3058 if (strcmp(t->name, buf) == 0)
3059 break;
3061 if (!t) {
3062 ret = -EINVAL;
3063 goto out;
3065 if (t == current_trace)
3066 goto out;
3068 trace_branch_disable();
3069 if (current_trace && current_trace->reset)
3070 current_trace->reset(tr);
3071 if (current_trace && current_trace->use_max_tr) {
3073 * We don't free the ring buffer. instead, resize it because
3074 * The max_tr ring buffer has some state (e.g. ring->clock) and
3075 * we want preserve it.
3077 ring_buffer_resize(max_tr.buffer, 1);
3078 max_tr.entries = 1;
3080 destroy_trace_option_files(topts);
3082 current_trace = t;
3084 topts = create_trace_option_files(current_trace);
3085 if (current_trace->use_max_tr) {
3086 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
3087 if (ret < 0)
3088 goto out;
3089 max_tr.entries = global_trace.entries;
3092 if (t->init) {
3093 ret = tracer_init(t, tr);
3094 if (ret)
3095 goto out;
3098 trace_branch_enable(tr);
3099 out:
3100 mutex_unlock(&trace_types_lock);
3102 return ret;
3105 static ssize_t
3106 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3107 size_t cnt, loff_t *ppos)
3109 char buf[MAX_TRACER_SIZE+1];
3110 int i;
3111 size_t ret;
3112 int err;
3114 ret = cnt;
3116 if (cnt > MAX_TRACER_SIZE)
3117 cnt = MAX_TRACER_SIZE;
3119 if (copy_from_user(&buf, ubuf, cnt))
3120 return -EFAULT;
3122 buf[cnt] = 0;
3124 /* strip ending whitespace. */
3125 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3126 buf[i] = 0;
3128 err = tracing_set_tracer(buf);
3129 if (err)
3130 return err;
3132 *ppos += ret;
3134 return ret;
3137 static ssize_t
3138 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3139 size_t cnt, loff_t *ppos)
3141 unsigned long *ptr = filp->private_data;
3142 char buf[64];
3143 int r;
3145 r = snprintf(buf, sizeof(buf), "%ld\n",
3146 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3147 if (r > sizeof(buf))
3148 r = sizeof(buf);
3149 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3152 static ssize_t
3153 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3154 size_t cnt, loff_t *ppos)
3156 unsigned long *ptr = filp->private_data;
3157 unsigned long val;
3158 int ret;
3160 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3161 if (ret)
3162 return ret;
3164 *ptr = val * 1000;
3166 return cnt;
3169 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3171 long cpu_file = (long) inode->i_private;
3172 struct trace_iterator *iter;
3173 int ret = 0;
3175 if (tracing_disabled)
3176 return -ENODEV;
3178 mutex_lock(&trace_types_lock);
3180 /* create a buffer to store the information to pass to userspace */
3181 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3182 if (!iter) {
3183 ret = -ENOMEM;
3184 goto out;
3188 * We make a copy of the current tracer to avoid concurrent
3189 * changes on it while we are reading.
3191 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3192 if (!iter->trace) {
3193 ret = -ENOMEM;
3194 goto fail;
3196 if (current_trace)
3197 *iter->trace = *current_trace;
3199 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3200 ret = -ENOMEM;
3201 goto fail;
3204 /* trace pipe does not show start of buffer */
3205 cpumask_setall(iter->started);
3207 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3208 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3210 iter->cpu_file = cpu_file;
3211 iter->tr = &global_trace;
3212 mutex_init(&iter->mutex);
3213 filp->private_data = iter;
3215 if (iter->trace->pipe_open)
3216 iter->trace->pipe_open(iter);
3218 nonseekable_open(inode, filp);
3219 out:
3220 mutex_unlock(&trace_types_lock);
3221 return ret;
3223 fail:
3224 kfree(iter->trace);
3225 kfree(iter);
3226 mutex_unlock(&trace_types_lock);
3227 return ret;
3230 static int tracing_release_pipe(struct inode *inode, struct file *file)
3232 struct trace_iterator *iter = file->private_data;
3234 mutex_lock(&trace_types_lock);
3236 if (iter->trace->pipe_close)
3237 iter->trace->pipe_close(iter);
3239 mutex_unlock(&trace_types_lock);
3241 free_cpumask_var(iter->started);
3242 mutex_destroy(&iter->mutex);
3243 kfree(iter->trace);
3244 kfree(iter);
3246 return 0;
3249 static unsigned int
3250 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3252 struct trace_iterator *iter = filp->private_data;
3254 if (trace_flags & TRACE_ITER_BLOCK) {
3256 * Always select as readable when in blocking mode
3258 return POLLIN | POLLRDNORM;
3259 } else {
3260 if (!trace_empty(iter))
3261 return POLLIN | POLLRDNORM;
3262 poll_wait(filp, &trace_wait, poll_table);
3263 if (!trace_empty(iter))
3264 return POLLIN | POLLRDNORM;
3266 return 0;
3271 void default_wait_pipe(struct trace_iterator *iter)
3273 DEFINE_WAIT(wait);
3275 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3277 if (trace_empty(iter))
3278 schedule();
3280 finish_wait(&trace_wait, &wait);
3284 * This is a make-shift waitqueue.
3285 * A tracer might use this callback on some rare cases:
3287 * 1) the current tracer might hold the runqueue lock when it wakes up
3288 * a reader, hence a deadlock (sched, function, and function graph tracers)
3289 * 2) the function tracers, trace all functions, we don't want
3290 * the overhead of calling wake_up and friends
3291 * (and tracing them too)
3293 * Anyway, this is really very primitive wakeup.
3295 void poll_wait_pipe(struct trace_iterator *iter)
3297 set_current_state(TASK_INTERRUPTIBLE);
3298 /* sleep for 100 msecs, and try again. */
3299 schedule_timeout(HZ / 10);
3302 /* Must be called with trace_types_lock mutex held. */
3303 static int tracing_wait_pipe(struct file *filp)
3305 struct trace_iterator *iter = filp->private_data;
3307 while (trace_empty(iter)) {
3309 if ((filp->f_flags & O_NONBLOCK)) {
3310 return -EAGAIN;
3313 mutex_unlock(&iter->mutex);
3315 iter->trace->wait_pipe(iter);
3317 mutex_lock(&iter->mutex);
3319 if (signal_pending(current))
3320 return -EINTR;
3323 * We block until we read something and tracing is disabled.
3324 * We still block if tracing is disabled, but we have never
3325 * read anything. This allows a user to cat this file, and
3326 * then enable tracing. But after we have read something,
3327 * we give an EOF when tracing is again disabled.
3329 * iter->pos will be 0 if we haven't read anything.
3331 if (!tracer_enabled && iter->pos)
3332 break;
3335 return 1;
3339 * Consumer reader.
3341 static ssize_t
3342 tracing_read_pipe(struct file *filp, char __user *ubuf,
3343 size_t cnt, loff_t *ppos)
3345 struct trace_iterator *iter = filp->private_data;
3346 static struct tracer *old_tracer;
3347 ssize_t sret;
3349 /* return any leftover data */
3350 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3351 if (sret != -EBUSY)
3352 return sret;
3354 trace_seq_init(&iter->seq);
3356 /* copy the tracer to avoid using a global lock all around */
3357 mutex_lock(&trace_types_lock);
3358 if (unlikely(old_tracer != current_trace && current_trace)) {
3359 old_tracer = current_trace;
3360 *iter->trace = *current_trace;
3362 mutex_unlock(&trace_types_lock);
3365 * Avoid more than one consumer on a single file descriptor
3366 * This is just a matter of traces coherency, the ring buffer itself
3367 * is protected.
3369 mutex_lock(&iter->mutex);
3370 if (iter->trace->read) {
3371 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3372 if (sret)
3373 goto out;
3376 waitagain:
3377 sret = tracing_wait_pipe(filp);
3378 if (sret <= 0)
3379 goto out;
3381 /* stop when tracing is finished */
3382 if (trace_empty(iter)) {
3383 sret = 0;
3384 goto out;
3387 if (cnt >= PAGE_SIZE)
3388 cnt = PAGE_SIZE - 1;
3390 /* reset all but tr, trace, and overruns */
3391 memset(&iter->seq, 0,
3392 sizeof(struct trace_iterator) -
3393 offsetof(struct trace_iterator, seq));
3394 iter->pos = -1;
3396 trace_event_read_lock();
3397 trace_access_lock(iter->cpu_file);
3398 while (trace_find_next_entry_inc(iter) != NULL) {
3399 enum print_line_t ret;
3400 int len = iter->seq.len;
3402 ret = print_trace_line(iter);
3403 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3404 /* don't print partial lines */
3405 iter->seq.len = len;
3406 break;
3408 if (ret != TRACE_TYPE_NO_CONSUME)
3409 trace_consume(iter);
3411 if (iter->seq.len >= cnt)
3412 break;
3415 * Setting the full flag means we reached the trace_seq buffer
3416 * size and we should leave by partial output condition above.
3417 * One of the trace_seq_* functions is not used properly.
3419 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3420 iter->ent->type);
3422 trace_access_unlock(iter->cpu_file);
3423 trace_event_read_unlock();
3425 /* Now copy what we have to the user */
3426 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3427 if (iter->seq.readpos >= iter->seq.len)
3428 trace_seq_init(&iter->seq);
3431 * If there was nothing to send to user, in spite of consuming trace
3432 * entries, go back to wait for more entries.
3434 if (sret == -EBUSY)
3435 goto waitagain;
3437 out:
3438 mutex_unlock(&iter->mutex);
3440 return sret;
3443 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3444 struct pipe_buffer *buf)
3446 __free_page(buf->page);
3449 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3450 unsigned int idx)
3452 __free_page(spd->pages[idx]);
3455 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3456 .can_merge = 0,
3457 .map = generic_pipe_buf_map,
3458 .unmap = generic_pipe_buf_unmap,
3459 .confirm = generic_pipe_buf_confirm,
3460 .release = tracing_pipe_buf_release,
3461 .steal = generic_pipe_buf_steal,
3462 .get = generic_pipe_buf_get,
3465 static size_t
3466 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3468 size_t count;
3469 int ret;
3471 /* Seq buffer is page-sized, exactly what we need. */
3472 for (;;) {
3473 count = iter->seq.len;
3474 ret = print_trace_line(iter);
3475 count = iter->seq.len - count;
3476 if (rem < count) {
3477 rem = 0;
3478 iter->seq.len -= count;
3479 break;
3481 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3482 iter->seq.len -= count;
3483 break;
3486 if (ret != TRACE_TYPE_NO_CONSUME)
3487 trace_consume(iter);
3488 rem -= count;
3489 if (!trace_find_next_entry_inc(iter)) {
3490 rem = 0;
3491 iter->ent = NULL;
3492 break;
3496 return rem;
3499 static ssize_t tracing_splice_read_pipe(struct file *filp,
3500 loff_t *ppos,
3501 struct pipe_inode_info *pipe,
3502 size_t len,
3503 unsigned int flags)
3505 struct page *pages_def[PIPE_DEF_BUFFERS];
3506 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3507 struct trace_iterator *iter = filp->private_data;
3508 struct splice_pipe_desc spd = {
3509 .pages = pages_def,
3510 .partial = partial_def,
3511 .nr_pages = 0, /* This gets updated below. */
3512 .flags = flags,
3513 .ops = &tracing_pipe_buf_ops,
3514 .spd_release = tracing_spd_release_pipe,
3516 static struct tracer *old_tracer;
3517 ssize_t ret;
3518 size_t rem;
3519 unsigned int i;
3521 if (splice_grow_spd(pipe, &spd))
3522 return -ENOMEM;
3524 /* copy the tracer to avoid using a global lock all around */
3525 mutex_lock(&trace_types_lock);
3526 if (unlikely(old_tracer != current_trace && current_trace)) {
3527 old_tracer = current_trace;
3528 *iter->trace = *current_trace;
3530 mutex_unlock(&trace_types_lock);
3532 mutex_lock(&iter->mutex);
3534 if (iter->trace->splice_read) {
3535 ret = iter->trace->splice_read(iter, filp,
3536 ppos, pipe, len, flags);
3537 if (ret)
3538 goto out_err;
3541 ret = tracing_wait_pipe(filp);
3542 if (ret <= 0)
3543 goto out_err;
3545 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3546 ret = -EFAULT;
3547 goto out_err;
3550 trace_event_read_lock();
3551 trace_access_lock(iter->cpu_file);
3553 /* Fill as many pages as possible. */
3554 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3555 spd.pages[i] = alloc_page(GFP_KERNEL);
3556 if (!spd.pages[i])
3557 break;
3559 rem = tracing_fill_pipe_page(rem, iter);
3561 /* Copy the data into the page, so we can start over. */
3562 ret = trace_seq_to_buffer(&iter->seq,
3563 page_address(spd.pages[i]),
3564 iter->seq.len);
3565 if (ret < 0) {
3566 __free_page(spd.pages[i]);
3567 break;
3569 spd.partial[i].offset = 0;
3570 spd.partial[i].len = iter->seq.len;
3572 trace_seq_init(&iter->seq);
3575 trace_access_unlock(iter->cpu_file);
3576 trace_event_read_unlock();
3577 mutex_unlock(&iter->mutex);
3579 spd.nr_pages = i;
3581 ret = splice_to_pipe(pipe, &spd);
3582 out:
3583 splice_shrink_spd(pipe, &spd);
3584 return ret;
3586 out_err:
3587 mutex_unlock(&iter->mutex);
3588 goto out;
3591 static ssize_t
3592 tracing_entries_read(struct file *filp, char __user *ubuf,
3593 size_t cnt, loff_t *ppos)
3595 struct trace_array *tr = filp->private_data;
3596 char buf[96];
3597 int r;
3599 mutex_lock(&trace_types_lock);
3600 if (!ring_buffer_expanded)
3601 r = sprintf(buf, "%lu (expanded: %lu)\n",
3602 tr->entries >> 10,
3603 trace_buf_size >> 10);
3604 else
3605 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3606 mutex_unlock(&trace_types_lock);
3608 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3611 static ssize_t
3612 tracing_entries_write(struct file *filp, const char __user *ubuf,
3613 size_t cnt, loff_t *ppos)
3615 unsigned long val;
3616 int ret;
3618 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3619 if (ret)
3620 return ret;
3622 /* must have at least 1 entry */
3623 if (!val)
3624 return -EINVAL;
3626 /* value is in KB */
3627 val <<= 10;
3629 ret = tracing_resize_ring_buffer(val);
3630 if (ret < 0)
3631 return ret;
3633 *ppos += cnt;
3635 return cnt;
3638 static ssize_t
3639 tracing_total_entries_read(struct file *filp, char __user *ubuf,
3640 size_t cnt, loff_t *ppos)
3642 struct trace_array *tr = filp->private_data;
3643 char buf[64];
3644 int r, cpu;
3645 unsigned long size = 0, expanded_size = 0;
3647 mutex_lock(&trace_types_lock);
3648 for_each_tracing_cpu(cpu) {
3649 size += tr->entries >> 10;
3650 if (!ring_buffer_expanded)
3651 expanded_size += trace_buf_size >> 10;
3653 if (ring_buffer_expanded)
3654 r = sprintf(buf, "%lu\n", size);
3655 else
3656 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3657 mutex_unlock(&trace_types_lock);
3659 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3662 static ssize_t
3663 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3664 size_t cnt, loff_t *ppos)
3667 * There is no need to read what the user has written, this function
3668 * is just to make sure that there is no error when "echo" is used
3671 *ppos += cnt;
3673 return cnt;
3676 static int
3677 tracing_free_buffer_release(struct inode *inode, struct file *filp)
3679 /* disable tracing ? */
3680 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3681 tracing_off();
3682 /* resize the ring buffer to 0 */
3683 tracing_resize_ring_buffer(0);
3685 return 0;
3688 static ssize_t
3689 tracing_mark_write(struct file *filp, const char __user *ubuf,
3690 size_t cnt, loff_t *fpos)
3692 unsigned long addr = (unsigned long)ubuf;
3693 struct ring_buffer_event *event;
3694 struct ring_buffer *buffer;
3695 struct print_entry *entry;
3696 unsigned long irq_flags;
3697 struct page *pages[2];
3698 int nr_pages = 1;
3699 ssize_t written;
3700 void *page1;
3701 void *page2;
3702 int offset;
3703 int size;
3704 int len;
3705 int ret;
3707 if (tracing_disabled)
3708 return -EINVAL;
3710 if (cnt > TRACE_BUF_SIZE)
3711 cnt = TRACE_BUF_SIZE;
3714 * Userspace is injecting traces into the kernel trace buffer.
3715 * We want to be as non intrusive as possible.
3716 * To do so, we do not want to allocate any special buffers
3717 * or take any locks, but instead write the userspace data
3718 * straight into the ring buffer.
3720 * First we need to pin the userspace buffer into memory,
3721 * which, most likely it is, because it just referenced it.
3722 * But there's no guarantee that it is. By using get_user_pages_fast()
3723 * and kmap_atomic/kunmap_atomic() we can get access to the
3724 * pages directly. We then write the data directly into the
3725 * ring buffer.
3727 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
3729 /* check if we cross pages */
3730 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
3731 nr_pages = 2;
3733 offset = addr & (PAGE_SIZE - 1);
3734 addr &= PAGE_MASK;
3736 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
3737 if (ret < nr_pages) {
3738 while (--ret >= 0)
3739 put_page(pages[ret]);
3740 written = -EFAULT;
3741 goto out;
3744 page1 = kmap_atomic(pages[0]);
3745 if (nr_pages == 2)
3746 page2 = kmap_atomic(pages[1]);
3748 local_save_flags(irq_flags);
3749 size = sizeof(*entry) + cnt + 2; /* possible \n added */
3750 buffer = global_trace.buffer;
3751 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3752 irq_flags, preempt_count());
3753 if (!event) {
3754 /* Ring buffer disabled, return as if not open for write */
3755 written = -EBADF;
3756 goto out_unlock;
3759 entry = ring_buffer_event_data(event);
3760 entry->ip = _THIS_IP_;
3762 if (nr_pages == 2) {
3763 len = PAGE_SIZE - offset;
3764 memcpy(&entry->buf, page1 + offset, len);
3765 memcpy(&entry->buf[len], page2, cnt - len);
3766 } else
3767 memcpy(&entry->buf, page1 + offset, cnt);
3769 if (entry->buf[cnt - 1] != '\n') {
3770 entry->buf[cnt] = '\n';
3771 entry->buf[cnt + 1] = '\0';
3772 } else
3773 entry->buf[cnt] = '\0';
3775 ring_buffer_unlock_commit(buffer, event);
3777 written = cnt;
3779 *fpos += written;
3781 out_unlock:
3782 if (nr_pages == 2)
3783 kunmap_atomic(page2);
3784 kunmap_atomic(page1);
3785 while (nr_pages > 0)
3786 put_page(pages[--nr_pages]);
3787 out:
3788 return written;
3791 static int tracing_clock_show(struct seq_file *m, void *v)
3793 int i;
3795 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3796 seq_printf(m,
3797 "%s%s%s%s", i ? " " : "",
3798 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3799 i == trace_clock_id ? "]" : "");
3800 seq_putc(m, '\n');
3802 return 0;
3805 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3806 size_t cnt, loff_t *fpos)
3808 char buf[64];
3809 const char *clockstr;
3810 int i;
3812 if (cnt >= sizeof(buf))
3813 return -EINVAL;
3815 if (copy_from_user(&buf, ubuf, cnt))
3816 return -EFAULT;
3818 buf[cnt] = 0;
3820 clockstr = strstrip(buf);
3822 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3823 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3824 break;
3826 if (i == ARRAY_SIZE(trace_clocks))
3827 return -EINVAL;
3829 trace_clock_id = i;
3831 mutex_lock(&trace_types_lock);
3833 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3834 if (max_tr.buffer)
3835 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3837 mutex_unlock(&trace_types_lock);
3839 *fpos += cnt;
3841 return cnt;
3844 static int tracing_clock_open(struct inode *inode, struct file *file)
3846 if (tracing_disabled)
3847 return -ENODEV;
3848 return single_open(file, tracing_clock_show, NULL);
3851 static const struct file_operations tracing_max_lat_fops = {
3852 .open = tracing_open_generic,
3853 .read = tracing_max_lat_read,
3854 .write = tracing_max_lat_write,
3855 .llseek = generic_file_llseek,
3858 static const struct file_operations tracing_ctrl_fops = {
3859 .open = tracing_open_generic,
3860 .read = tracing_ctrl_read,
3861 .write = tracing_ctrl_write,
3862 .llseek = generic_file_llseek,
3865 static const struct file_operations set_tracer_fops = {
3866 .open = tracing_open_generic,
3867 .read = tracing_set_trace_read,
3868 .write = tracing_set_trace_write,
3869 .llseek = generic_file_llseek,
3872 static const struct file_operations tracing_pipe_fops = {
3873 .open = tracing_open_pipe,
3874 .poll = tracing_poll_pipe,
3875 .read = tracing_read_pipe,
3876 .splice_read = tracing_splice_read_pipe,
3877 .release = tracing_release_pipe,
3878 .llseek = no_llseek,
3881 static const struct file_operations tracing_entries_fops = {
3882 .open = tracing_open_generic,
3883 .read = tracing_entries_read,
3884 .write = tracing_entries_write,
3885 .llseek = generic_file_llseek,
3888 static const struct file_operations tracing_total_entries_fops = {
3889 .open = tracing_open_generic,
3890 .read = tracing_total_entries_read,
3891 .llseek = generic_file_llseek,
3894 static const struct file_operations tracing_free_buffer_fops = {
3895 .write = tracing_free_buffer_write,
3896 .release = tracing_free_buffer_release,
3899 static const struct file_operations tracing_mark_fops = {
3900 .open = tracing_open_generic,
3901 .write = tracing_mark_write,
3902 .llseek = generic_file_llseek,
3905 static const struct file_operations trace_clock_fops = {
3906 .open = tracing_clock_open,
3907 .read = seq_read,
3908 .llseek = seq_lseek,
3909 .release = single_release,
3910 .write = tracing_clock_write,
3913 struct ftrace_buffer_info {
3914 struct trace_array *tr;
3915 void *spare;
3916 int cpu;
3917 unsigned int read;
3920 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3922 int cpu = (int)(long)inode->i_private;
3923 struct ftrace_buffer_info *info;
3925 if (tracing_disabled)
3926 return -ENODEV;
3928 info = kzalloc(sizeof(*info), GFP_KERNEL);
3929 if (!info)
3930 return -ENOMEM;
3932 info->tr = &global_trace;
3933 info->cpu = cpu;
3934 info->spare = NULL;
3935 /* Force reading ring buffer for first read */
3936 info->read = (unsigned int)-1;
3938 filp->private_data = info;
3940 return nonseekable_open(inode, filp);
3943 static ssize_t
3944 tracing_buffers_read(struct file *filp, char __user *ubuf,
3945 size_t count, loff_t *ppos)
3947 struct ftrace_buffer_info *info = filp->private_data;
3948 ssize_t ret;
3949 size_t size;
3951 if (!count)
3952 return 0;
3954 if (!info->spare)
3955 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
3956 if (!info->spare)
3957 return -ENOMEM;
3959 /* Do we have previous read data to read? */
3960 if (info->read < PAGE_SIZE)
3961 goto read;
3963 trace_access_lock(info->cpu);
3964 ret = ring_buffer_read_page(info->tr->buffer,
3965 &info->spare,
3966 count,
3967 info->cpu, 0);
3968 trace_access_unlock(info->cpu);
3969 if (ret < 0)
3970 return 0;
3972 info->read = 0;
3974 read:
3975 size = PAGE_SIZE - info->read;
3976 if (size > count)
3977 size = count;
3979 ret = copy_to_user(ubuf, info->spare + info->read, size);
3980 if (ret == size)
3981 return -EFAULT;
3982 size -= ret;
3984 *ppos += size;
3985 info->read += size;
3987 return size;
3990 static int tracing_buffers_release(struct inode *inode, struct file *file)
3992 struct ftrace_buffer_info *info = file->private_data;
3994 if (info->spare)
3995 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3996 kfree(info);
3998 return 0;
4001 struct buffer_ref {
4002 struct ring_buffer *buffer;
4003 void *page;
4004 int ref;
4007 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4008 struct pipe_buffer *buf)
4010 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4012 if (--ref->ref)
4013 return;
4015 ring_buffer_free_read_page(ref->buffer, ref->page);
4016 kfree(ref);
4017 buf->private = 0;
4020 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
4021 struct pipe_buffer *buf)
4023 return 1;
4026 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4027 struct pipe_buffer *buf)
4029 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4031 ref->ref++;
4034 /* Pipe buffer operations for a buffer. */
4035 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4036 .can_merge = 0,
4037 .map = generic_pipe_buf_map,
4038 .unmap = generic_pipe_buf_unmap,
4039 .confirm = generic_pipe_buf_confirm,
4040 .release = buffer_pipe_buf_release,
4041 .steal = buffer_pipe_buf_steal,
4042 .get = buffer_pipe_buf_get,
4046 * Callback from splice_to_pipe(), if we need to release some pages
4047 * at the end of the spd in case we error'ed out in filling the pipe.
4049 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4051 struct buffer_ref *ref =
4052 (struct buffer_ref *)spd->partial[i].private;
4054 if (--ref->ref)
4055 return;
4057 ring_buffer_free_read_page(ref->buffer, ref->page);
4058 kfree(ref);
4059 spd->partial[i].private = 0;
4062 static ssize_t
4063 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4064 struct pipe_inode_info *pipe, size_t len,
4065 unsigned int flags)
4067 struct ftrace_buffer_info *info = file->private_data;
4068 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4069 struct page *pages_def[PIPE_DEF_BUFFERS];
4070 struct splice_pipe_desc spd = {
4071 .pages = pages_def,
4072 .partial = partial_def,
4073 .flags = flags,
4074 .ops = &buffer_pipe_buf_ops,
4075 .spd_release = buffer_spd_release,
4077 struct buffer_ref *ref;
4078 int entries, size, i;
4079 size_t ret;
4081 if (splice_grow_spd(pipe, &spd))
4082 return -ENOMEM;
4084 if (*ppos & (PAGE_SIZE - 1)) {
4085 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
4086 ret = -EINVAL;
4087 goto out;
4090 if (len & (PAGE_SIZE - 1)) {
4091 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
4092 if (len < PAGE_SIZE) {
4093 ret = -EINVAL;
4094 goto out;
4096 len &= PAGE_MASK;
4099 trace_access_lock(info->cpu);
4100 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4102 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4103 struct page *page;
4104 int r;
4106 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4107 if (!ref)
4108 break;
4110 ref->ref = 1;
4111 ref->buffer = info->tr->buffer;
4112 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
4113 if (!ref->page) {
4114 kfree(ref);
4115 break;
4118 r = ring_buffer_read_page(ref->buffer, &ref->page,
4119 len, info->cpu, 1);
4120 if (r < 0) {
4121 ring_buffer_free_read_page(ref->buffer, ref->page);
4122 kfree(ref);
4123 break;
4127 * zero out any left over data, this is going to
4128 * user land.
4130 size = ring_buffer_page_len(ref->page);
4131 if (size < PAGE_SIZE)
4132 memset(ref->page + size, 0, PAGE_SIZE - size);
4134 page = virt_to_page(ref->page);
4136 spd.pages[i] = page;
4137 spd.partial[i].len = PAGE_SIZE;
4138 spd.partial[i].offset = 0;
4139 spd.partial[i].private = (unsigned long)ref;
4140 spd.nr_pages++;
4141 *ppos += PAGE_SIZE;
4143 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4146 trace_access_unlock(info->cpu);
4147 spd.nr_pages = i;
4149 /* did we read anything? */
4150 if (!spd.nr_pages) {
4151 if (flags & SPLICE_F_NONBLOCK)
4152 ret = -EAGAIN;
4153 else
4154 ret = 0;
4155 /* TODO: block */
4156 goto out;
4159 ret = splice_to_pipe(pipe, &spd);
4160 splice_shrink_spd(pipe, &spd);
4161 out:
4162 return ret;
4165 static const struct file_operations tracing_buffers_fops = {
4166 .open = tracing_buffers_open,
4167 .read = tracing_buffers_read,
4168 .release = tracing_buffers_release,
4169 .splice_read = tracing_buffers_splice_read,
4170 .llseek = no_llseek,
4173 static ssize_t
4174 tracing_stats_read(struct file *filp, char __user *ubuf,
4175 size_t count, loff_t *ppos)
4177 unsigned long cpu = (unsigned long)filp->private_data;
4178 struct trace_array *tr = &global_trace;
4179 struct trace_seq *s;
4180 unsigned long cnt;
4181 unsigned long long t;
4182 unsigned long usec_rem;
4184 s = kmalloc(sizeof(*s), GFP_KERNEL);
4185 if (!s)
4186 return -ENOMEM;
4188 trace_seq_init(s);
4190 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4191 trace_seq_printf(s, "entries: %ld\n", cnt);
4193 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4194 trace_seq_printf(s, "overrun: %ld\n", cnt);
4196 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4197 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4199 cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4200 trace_seq_printf(s, "bytes: %ld\n", cnt);
4202 t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4203 usec_rem = do_div(t, USEC_PER_SEC);
4204 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", t, usec_rem);
4206 t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4207 usec_rem = do_div(t, USEC_PER_SEC);
4208 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4210 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4212 kfree(s);
4214 return count;
4217 static const struct file_operations tracing_stats_fops = {
4218 .open = tracing_open_generic,
4219 .read = tracing_stats_read,
4220 .llseek = generic_file_llseek,
4223 #ifdef CONFIG_DYNAMIC_FTRACE
4225 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4227 return 0;
4230 static ssize_t
4231 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
4232 size_t cnt, loff_t *ppos)
4234 static char ftrace_dyn_info_buffer[1024];
4235 static DEFINE_MUTEX(dyn_info_mutex);
4236 unsigned long *p = filp->private_data;
4237 char *buf = ftrace_dyn_info_buffer;
4238 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
4239 int r;
4241 mutex_lock(&dyn_info_mutex);
4242 r = sprintf(buf, "%ld ", *p);
4244 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
4245 buf[r++] = '\n';
4247 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4249 mutex_unlock(&dyn_info_mutex);
4251 return r;
4254 static const struct file_operations tracing_dyn_info_fops = {
4255 .open = tracing_open_generic,
4256 .read = tracing_read_dyn_info,
4257 .llseek = generic_file_llseek,
4259 #endif
4261 static struct dentry *d_tracer;
4263 struct dentry *tracing_init_dentry(void)
4265 static int once;
4267 if (d_tracer)
4268 return d_tracer;
4270 if (!debugfs_initialized())
4271 return NULL;
4273 d_tracer = debugfs_create_dir("tracing", NULL);
4275 if (!d_tracer && !once) {
4276 once = 1;
4277 pr_warning("Could not create debugfs directory 'tracing'\n");
4278 return NULL;
4281 return d_tracer;
4284 static struct dentry *d_percpu;
4286 struct dentry *tracing_dentry_percpu(void)
4288 static int once;
4289 struct dentry *d_tracer;
4291 if (d_percpu)
4292 return d_percpu;
4294 d_tracer = tracing_init_dentry();
4296 if (!d_tracer)
4297 return NULL;
4299 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4301 if (!d_percpu && !once) {
4302 once = 1;
4303 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4304 return NULL;
4307 return d_percpu;
4310 static void tracing_init_debugfs_percpu(long cpu)
4312 struct dentry *d_percpu = tracing_dentry_percpu();
4313 struct dentry *d_cpu;
4314 char cpu_dir[30]; /* 30 characters should be more than enough */
4316 snprintf(cpu_dir, 30, "cpu%ld", cpu);
4317 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4318 if (!d_cpu) {
4319 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4320 return;
4323 /* per cpu trace_pipe */
4324 trace_create_file("trace_pipe", 0444, d_cpu,
4325 (void *) cpu, &tracing_pipe_fops);
4327 /* per cpu trace */
4328 trace_create_file("trace", 0644, d_cpu,
4329 (void *) cpu, &tracing_fops);
4331 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4332 (void *) cpu, &tracing_buffers_fops);
4334 trace_create_file("stats", 0444, d_cpu,
4335 (void *) cpu, &tracing_stats_fops);
4338 #ifdef CONFIG_FTRACE_SELFTEST
4339 /* Let selftest have access to static functions in this file */
4340 #include "trace_selftest.c"
4341 #endif
4343 struct trace_option_dentry {
4344 struct tracer_opt *opt;
4345 struct tracer_flags *flags;
4346 struct dentry *entry;
4349 static ssize_t
4350 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4351 loff_t *ppos)
4353 struct trace_option_dentry *topt = filp->private_data;
4354 char *buf;
4356 if (topt->flags->val & topt->opt->bit)
4357 buf = "1\n";
4358 else
4359 buf = "0\n";
4361 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4364 static ssize_t
4365 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4366 loff_t *ppos)
4368 struct trace_option_dentry *topt = filp->private_data;
4369 unsigned long val;
4370 int ret;
4372 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4373 if (ret)
4374 return ret;
4376 if (val != 0 && val != 1)
4377 return -EINVAL;
4379 if (!!(topt->flags->val & topt->opt->bit) != val) {
4380 mutex_lock(&trace_types_lock);
4381 ret = __set_tracer_option(current_trace, topt->flags,
4382 topt->opt, !val);
4383 mutex_unlock(&trace_types_lock);
4384 if (ret)
4385 return ret;
4388 *ppos += cnt;
4390 return cnt;
4394 static const struct file_operations trace_options_fops = {
4395 .open = tracing_open_generic,
4396 .read = trace_options_read,
4397 .write = trace_options_write,
4398 .llseek = generic_file_llseek,
4401 static ssize_t
4402 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4403 loff_t *ppos)
4405 long index = (long)filp->private_data;
4406 char *buf;
4408 if (trace_flags & (1 << index))
4409 buf = "1\n";
4410 else
4411 buf = "0\n";
4413 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4416 static ssize_t
4417 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4418 loff_t *ppos)
4420 long index = (long)filp->private_data;
4421 unsigned long val;
4422 int ret;
4424 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4425 if (ret)
4426 return ret;
4428 if (val != 0 && val != 1)
4429 return -EINVAL;
4430 set_tracer_flags(1 << index, val);
4432 *ppos += cnt;
4434 return cnt;
4437 static const struct file_operations trace_options_core_fops = {
4438 .open = tracing_open_generic,
4439 .read = trace_options_core_read,
4440 .write = trace_options_core_write,
4441 .llseek = generic_file_llseek,
4444 struct dentry *trace_create_file(const char *name,
4445 umode_t mode,
4446 struct dentry *parent,
4447 void *data,
4448 const struct file_operations *fops)
4450 struct dentry *ret;
4452 ret = debugfs_create_file(name, mode, parent, data, fops);
4453 if (!ret)
4454 pr_warning("Could not create debugfs '%s' entry\n", name);
4456 return ret;
4460 static struct dentry *trace_options_init_dentry(void)
4462 struct dentry *d_tracer;
4463 static struct dentry *t_options;
4465 if (t_options)
4466 return t_options;
4468 d_tracer = tracing_init_dentry();
4469 if (!d_tracer)
4470 return NULL;
4472 t_options = debugfs_create_dir("options", d_tracer);
4473 if (!t_options) {
4474 pr_warning("Could not create debugfs directory 'options'\n");
4475 return NULL;
4478 return t_options;
4481 static void
4482 create_trace_option_file(struct trace_option_dentry *topt,
4483 struct tracer_flags *flags,
4484 struct tracer_opt *opt)
4486 struct dentry *t_options;
4488 t_options = trace_options_init_dentry();
4489 if (!t_options)
4490 return;
4492 topt->flags = flags;
4493 topt->opt = opt;
4495 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4496 &trace_options_fops);
4500 static struct trace_option_dentry *
4501 create_trace_option_files(struct tracer *tracer)
4503 struct trace_option_dentry *topts;
4504 struct tracer_flags *flags;
4505 struct tracer_opt *opts;
4506 int cnt;
4508 if (!tracer)
4509 return NULL;
4511 flags = tracer->flags;
4513 if (!flags || !flags->opts)
4514 return NULL;
4516 opts = flags->opts;
4518 for (cnt = 0; opts[cnt].name; cnt++)
4521 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4522 if (!topts)
4523 return NULL;
4525 for (cnt = 0; opts[cnt].name; cnt++)
4526 create_trace_option_file(&topts[cnt], flags,
4527 &opts[cnt]);
4529 return topts;
4532 static void
4533 destroy_trace_option_files(struct trace_option_dentry *topts)
4535 int cnt;
4537 if (!topts)
4538 return;
4540 for (cnt = 0; topts[cnt].opt; cnt++) {
4541 if (topts[cnt].entry)
4542 debugfs_remove(topts[cnt].entry);
4545 kfree(topts);
4548 static struct dentry *
4549 create_trace_option_core_file(const char *option, long index)
4551 struct dentry *t_options;
4553 t_options = trace_options_init_dentry();
4554 if (!t_options)
4555 return NULL;
4557 return trace_create_file(option, 0644, t_options, (void *)index,
4558 &trace_options_core_fops);
4561 static __init void create_trace_options_dir(void)
4563 struct dentry *t_options;
4564 int i;
4566 t_options = trace_options_init_dentry();
4567 if (!t_options)
4568 return;
4570 for (i = 0; trace_options[i]; i++)
4571 create_trace_option_core_file(trace_options[i], i);
4574 static __init int tracer_init_debugfs(void)
4576 struct dentry *d_tracer;
4577 int cpu;
4579 trace_access_lock_init();
4581 d_tracer = tracing_init_dentry();
4583 trace_create_file("tracing_enabled", 0644, d_tracer,
4584 &global_trace, &tracing_ctrl_fops);
4586 trace_create_file("trace_options", 0644, d_tracer,
4587 NULL, &tracing_iter_fops);
4589 trace_create_file("tracing_cpumask", 0644, d_tracer,
4590 NULL, &tracing_cpumask_fops);
4592 trace_create_file("trace", 0644, d_tracer,
4593 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4595 trace_create_file("available_tracers", 0444, d_tracer,
4596 &global_trace, &show_traces_fops);
4598 trace_create_file("current_tracer", 0644, d_tracer,
4599 &global_trace, &set_tracer_fops);
4601 #ifdef CONFIG_TRACER_MAX_TRACE
4602 trace_create_file("tracing_max_latency", 0644, d_tracer,
4603 &tracing_max_latency, &tracing_max_lat_fops);
4604 #endif
4606 trace_create_file("tracing_thresh", 0644, d_tracer,
4607 &tracing_thresh, &tracing_max_lat_fops);
4609 trace_create_file("README", 0444, d_tracer,
4610 NULL, &tracing_readme_fops);
4612 trace_create_file("trace_pipe", 0444, d_tracer,
4613 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4615 trace_create_file("buffer_size_kb", 0644, d_tracer,
4616 &global_trace, &tracing_entries_fops);
4618 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
4619 &global_trace, &tracing_total_entries_fops);
4621 trace_create_file("free_buffer", 0644, d_tracer,
4622 &global_trace, &tracing_free_buffer_fops);
4624 trace_create_file("trace_marker", 0220, d_tracer,
4625 NULL, &tracing_mark_fops);
4627 trace_create_file("saved_cmdlines", 0444, d_tracer,
4628 NULL, &tracing_saved_cmdlines_fops);
4630 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4631 &trace_clock_fops);
4633 #ifdef CONFIG_DYNAMIC_FTRACE
4634 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4635 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4636 #endif
4638 create_trace_options_dir();
4640 for_each_tracing_cpu(cpu)
4641 tracing_init_debugfs_percpu(cpu);
4643 return 0;
4646 static int trace_panic_handler(struct notifier_block *this,
4647 unsigned long event, void *unused)
4649 if (ftrace_dump_on_oops)
4650 ftrace_dump(ftrace_dump_on_oops);
4651 return NOTIFY_OK;
4654 static struct notifier_block trace_panic_notifier = {
4655 .notifier_call = trace_panic_handler,
4656 .next = NULL,
4657 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4660 static int trace_die_handler(struct notifier_block *self,
4661 unsigned long val,
4662 void *data)
4664 switch (val) {
4665 case DIE_OOPS:
4666 if (ftrace_dump_on_oops)
4667 ftrace_dump(ftrace_dump_on_oops);
4668 break;
4669 default:
4670 break;
4672 return NOTIFY_OK;
4675 static struct notifier_block trace_die_notifier = {
4676 .notifier_call = trace_die_handler,
4677 .priority = 200
4681 * printk is set to max of 1024, we really don't need it that big.
4682 * Nothing should be printing 1000 characters anyway.
4684 #define TRACE_MAX_PRINT 1000
4687 * Define here KERN_TRACE so that we have one place to modify
4688 * it if we decide to change what log level the ftrace dump
4689 * should be at.
4691 #define KERN_TRACE KERN_EMERG
4693 void
4694 trace_printk_seq(struct trace_seq *s)
4696 /* Probably should print a warning here. */
4697 if (s->len >= 1000)
4698 s->len = 1000;
4700 /* should be zero ended, but we are paranoid. */
4701 s->buffer[s->len] = 0;
4703 printk(KERN_TRACE "%s", s->buffer);
4705 trace_seq_init(s);
4708 void trace_init_global_iter(struct trace_iterator *iter)
4710 iter->tr = &global_trace;
4711 iter->trace = current_trace;
4712 iter->cpu_file = TRACE_PIPE_ALL_CPU;
4715 static void
4716 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
4718 static arch_spinlock_t ftrace_dump_lock =
4719 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
4720 /* use static because iter can be a bit big for the stack */
4721 static struct trace_iterator iter;
4722 unsigned int old_userobj;
4723 static int dump_ran;
4724 unsigned long flags;
4725 int cnt = 0, cpu;
4727 /* only one dump */
4728 local_irq_save(flags);
4729 arch_spin_lock(&ftrace_dump_lock);
4730 if (dump_ran)
4731 goto out;
4733 dump_ran = 1;
4735 tracing_off();
4737 /* Did function tracer already get disabled? */
4738 if (ftrace_is_dead()) {
4739 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
4740 printk("# MAY BE MISSING FUNCTION EVENTS\n");
4743 if (disable_tracing)
4744 ftrace_kill();
4746 trace_init_global_iter(&iter);
4748 for_each_tracing_cpu(cpu) {
4749 atomic_inc(&iter.tr->data[cpu]->disabled);
4752 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4754 /* don't look at user memory in panic mode */
4755 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4757 /* Simulate the iterator */
4758 iter.tr = &global_trace;
4759 iter.trace = current_trace;
4761 switch (oops_dump_mode) {
4762 case DUMP_ALL:
4763 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4764 break;
4765 case DUMP_ORIG:
4766 iter.cpu_file = raw_smp_processor_id();
4767 break;
4768 case DUMP_NONE:
4769 goto out_enable;
4770 default:
4771 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4772 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4775 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4778 * We need to stop all tracing on all CPUS to read the
4779 * the next buffer. This is a bit expensive, but is
4780 * not done often. We fill all what we can read,
4781 * and then release the locks again.
4784 while (!trace_empty(&iter)) {
4786 if (!cnt)
4787 printk(KERN_TRACE "---------------------------------\n");
4789 cnt++;
4791 /* reset all but tr, trace, and overruns */
4792 memset(&iter.seq, 0,
4793 sizeof(struct trace_iterator) -
4794 offsetof(struct trace_iterator, seq));
4795 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4796 iter.pos = -1;
4798 if (trace_find_next_entry_inc(&iter) != NULL) {
4799 int ret;
4801 ret = print_trace_line(&iter);
4802 if (ret != TRACE_TYPE_NO_CONSUME)
4803 trace_consume(&iter);
4806 trace_printk_seq(&iter.seq);
4809 if (!cnt)
4810 printk(KERN_TRACE " (ftrace buffer empty)\n");
4811 else
4812 printk(KERN_TRACE "---------------------------------\n");
4814 out_enable:
4815 /* Re-enable tracing if requested */
4816 if (!disable_tracing) {
4817 trace_flags |= old_userobj;
4819 for_each_tracing_cpu(cpu) {
4820 atomic_dec(&iter.tr->data[cpu]->disabled);
4822 tracing_on();
4825 out:
4826 arch_spin_unlock(&ftrace_dump_lock);
4827 local_irq_restore(flags);
4830 /* By default: disable tracing after the dump */
4831 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
4833 __ftrace_dump(true, oops_dump_mode);
4835 EXPORT_SYMBOL_GPL(ftrace_dump);
4837 __init static int tracer_alloc_buffers(void)
4839 int ring_buf_size;
4840 enum ring_buffer_flags rb_flags;
4841 int i;
4842 int ret = -ENOMEM;
4845 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4846 goto out;
4848 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4849 goto out_free_buffer_mask;
4851 /* To save memory, keep the ring buffer size to its minimum */
4852 if (ring_buffer_expanded)
4853 ring_buf_size = trace_buf_size;
4854 else
4855 ring_buf_size = 1;
4857 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
4859 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4860 cpumask_copy(tracing_cpumask, cpu_all_mask);
4862 /* TODO: make the number of buffers hot pluggable with CPUS */
4863 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
4864 if (!global_trace.buffer) {
4865 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4866 WARN_ON(1);
4867 goto out_free_cpumask;
4869 global_trace.entries = ring_buffer_size(global_trace.buffer);
4872 #ifdef CONFIG_TRACER_MAX_TRACE
4873 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
4874 if (!max_tr.buffer) {
4875 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4876 WARN_ON(1);
4877 ring_buffer_free(global_trace.buffer);
4878 goto out_free_cpumask;
4880 max_tr.entries = 1;
4881 #endif
4883 /* Allocate the first page for all buffers */
4884 for_each_tracing_cpu(i) {
4885 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4886 max_tr.data[i] = &per_cpu(max_tr_data, i);
4889 trace_init_cmdlines();
4891 register_tracer(&nop_trace);
4892 current_trace = &nop_trace;
4893 /* All seems OK, enable tracing */
4894 tracing_disabled = 0;
4896 atomic_notifier_chain_register(&panic_notifier_list,
4897 &trace_panic_notifier);
4899 register_die_notifier(&trace_die_notifier);
4901 return 0;
4903 out_free_cpumask:
4904 free_cpumask_var(tracing_cpumask);
4905 out_free_buffer_mask:
4906 free_cpumask_var(tracing_buffer_mask);
4907 out:
4908 return ret;
4911 __init static int clear_boot_tracer(void)
4914 * The default tracer at boot buffer is an init section.
4915 * This function is called in lateinit. If we did not
4916 * find the boot tracer, then clear it out, to prevent
4917 * later registration from accessing the buffer that is
4918 * about to be freed.
4920 if (!default_bootup_tracer)
4921 return 0;
4923 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4924 default_bootup_tracer);
4925 default_bootup_tracer = NULL;
4927 return 0;
4930 early_initcall(tracer_alloc_buffers);
4931 fs_initcall(tracer_init_debugfs);
4932 late_initcall(clear_boot_tracer);