tracing: fix trace_find_cmdline()
[linux-2.6/x86.git] / kernel / trace / trace.c
blob06c69a2603280698a00ab5b5d23464af89c52e30
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 <linux/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/ctype.h>
34 #include <linux/init.h>
35 #include <linux/poll.h>
36 #include <linux/gfp.h>
37 #include <linux/fs.h>
39 #include "trace.h"
40 #include "trace_output.h"
42 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
44 unsigned long __read_mostly tracing_max_latency;
45 unsigned long __read_mostly tracing_thresh;
48 * On boot up, the ring buffer is set to the minimum size, so that
49 * we do not waste memory on systems that are not using tracing.
51 static int ring_buffer_expanded;
54 * We need to change this state when a selftest is running.
55 * A selftest will lurk into the ring-buffer to count the
56 * entries inserted during the selftest although some concurrent
57 * insertions into the ring-buffer such as trace_printk could occurred
58 * at the same time, giving false positive or negative results.
60 static bool __read_mostly tracing_selftest_running;
63 * If a tracer is running, we do not want to run SELFTEST.
65 static bool __read_mostly tracing_selftest_disabled;
67 /* For tracers that don't implement custom flags */
68 static struct tracer_opt dummy_tracer_opt[] = {
69 { }
72 static struct tracer_flags dummy_tracer_flags = {
73 .val = 0,
74 .opts = dummy_tracer_opt
77 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
79 return 0;
83 * Kill all tracing for good (never come back).
84 * It is initialized to 1 but will turn to zero if the initialization
85 * of the tracer is successful. But that is the only place that sets
86 * this back to zero.
88 static int tracing_disabled = 1;
90 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
92 static inline void ftrace_disable_cpu(void)
94 preempt_disable();
95 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
98 static inline void ftrace_enable_cpu(void)
100 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
101 preempt_enable();
104 static cpumask_var_t __read_mostly tracing_buffer_mask;
106 /* Define which cpu buffers are currently read in trace_pipe */
107 static cpumask_var_t tracing_reader_cpumask;
109 #define for_each_tracing_cpu(cpu) \
110 for_each_cpu(cpu, tracing_buffer_mask)
113 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
115 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
116 * is set, then ftrace_dump is called. This will output the contents
117 * of the ftrace buffers to the console. This is very useful for
118 * capturing traces that lead to crashes and outputing it to a
119 * serial console.
121 * It is default off, but you can enable it with either specifying
122 * "ftrace_dump_on_oops" in the kernel command line, or setting
123 * /proc/sys/kernel/ftrace_dump_on_oops to true.
125 int ftrace_dump_on_oops;
127 static int tracing_set_tracer(const char *buf);
129 #define BOOTUP_TRACER_SIZE 100
130 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
131 static char *default_bootup_tracer;
133 static int __init set_ftrace(char *str)
135 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
136 default_bootup_tracer = bootup_tracer_buf;
137 /* We are using ftrace early, expand it */
138 ring_buffer_expanded = 1;
139 return 1;
141 __setup("ftrace=", set_ftrace);
143 static int __init set_ftrace_dump_on_oops(char *str)
145 ftrace_dump_on_oops = 1;
146 return 1;
148 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
150 long
151 ns2usecs(cycle_t nsec)
153 nsec += 500;
154 do_div(nsec, 1000);
155 return nsec;
158 cycle_t ftrace_now(int cpu)
160 u64 ts = ring_buffer_time_stamp(cpu);
161 ring_buffer_normalize_time_stamp(cpu, &ts);
162 return ts;
166 * The global_trace is the descriptor that holds the tracing
167 * buffers for the live tracing. For each CPU, it contains
168 * a link list of pages that will store trace entries. The
169 * page descriptor of the pages in the memory is used to hold
170 * the link list by linking the lru item in the page descriptor
171 * to each of the pages in the buffer per CPU.
173 * For each active CPU there is a data field that holds the
174 * pages for the buffer for that CPU. Each CPU has the same number
175 * of pages allocated for its buffer.
177 static struct trace_array global_trace;
179 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
182 * The max_tr is used to snapshot the global_trace when a maximum
183 * latency is reached. Some tracers will use this to store a maximum
184 * trace while it continues examining live traces.
186 * The buffers for the max_tr are set up the same as the global_trace.
187 * When a snapshot is taken, the link list of the max_tr is swapped
188 * with the link list of the global_trace and the buffers are reset for
189 * the global_trace so the tracing can continue.
191 static struct trace_array max_tr;
193 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
195 /* tracer_enabled is used to toggle activation of a tracer */
196 static int tracer_enabled = 1;
199 * tracing_is_enabled - return tracer_enabled status
201 * This function is used by other tracers to know the status
202 * of the tracer_enabled flag. Tracers may use this function
203 * to know if it should enable their features when starting
204 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
206 int tracing_is_enabled(void)
208 return tracer_enabled;
212 * trace_buf_size is the size in bytes that is allocated
213 * for a buffer. Note, the number of bytes is always rounded
214 * to page size.
216 * This number is purposely set to a low number of 16384.
217 * If the dump on oops happens, it will be much appreciated
218 * to not have to wait for all that output. Anyway this can be
219 * boot time and run time configurable.
221 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
223 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
225 /* trace_types holds a link list of available tracers. */
226 static struct tracer *trace_types __read_mostly;
228 /* current_trace points to the tracer that is currently active */
229 static struct tracer *current_trace __read_mostly;
232 * max_tracer_type_len is used to simplify the allocating of
233 * buffers to read userspace tracer names. We keep track of
234 * the longest tracer name registered.
236 static int max_tracer_type_len;
239 * trace_types_lock is used to protect the trace_types list.
240 * This lock is also used to keep user access serialized.
241 * Accesses from userspace will grab this lock while userspace
242 * activities happen inside the kernel.
244 static DEFINE_MUTEX(trace_types_lock);
246 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
247 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
249 /* trace_flags holds trace_options default values */
250 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
251 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
254 * trace_wake_up - wake up tasks waiting for trace input
256 * Simply wakes up any task that is blocked on the trace_wait
257 * queue. These is used with trace_poll for tasks polling the trace.
259 void trace_wake_up(void)
262 * The runqueue_is_locked() can fail, but this is the best we
263 * have for now:
265 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
266 wake_up(&trace_wait);
269 static int __init set_buf_size(char *str)
271 unsigned long buf_size;
272 int ret;
274 if (!str)
275 return 0;
276 ret = strict_strtoul(str, 0, &buf_size);
277 /* nr_entries can not be zero */
278 if (ret < 0 || buf_size == 0)
279 return 0;
280 trace_buf_size = buf_size;
281 return 1;
283 __setup("trace_buf_size=", set_buf_size);
285 unsigned long nsecs_to_usecs(unsigned long nsecs)
287 return nsecs / 1000;
290 /* These must match the bit postions in trace_iterator_flags */
291 static const char *trace_options[] = {
292 "print-parent",
293 "sym-offset",
294 "sym-addr",
295 "verbose",
296 "raw",
297 "hex",
298 "bin",
299 "block",
300 "stacktrace",
301 "sched-tree",
302 "trace_printk",
303 "ftrace_preempt",
304 "branch",
305 "annotate",
306 "userstacktrace",
307 "sym-userobj",
308 "printk-msg-only",
309 "context-info",
310 "latency-format",
311 NULL
315 * ftrace_max_lock is used to protect the swapping of buffers
316 * when taking a max snapshot. The buffers themselves are
317 * protected by per_cpu spinlocks. But the action of the swap
318 * needs its own lock.
320 * This is defined as a raw_spinlock_t in order to help
321 * with performance when lockdep debugging is enabled.
323 static raw_spinlock_t ftrace_max_lock =
324 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
327 * Copy the new maximum trace into the separate maximum-trace
328 * structure. (this way the maximum trace is permanently saved,
329 * for later retrieval via /debugfs/tracing/latency_trace)
331 static void
332 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
334 struct trace_array_cpu *data = tr->data[cpu];
336 max_tr.cpu = cpu;
337 max_tr.time_start = data->preempt_timestamp;
339 data = max_tr.data[cpu];
340 data->saved_latency = tracing_max_latency;
342 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
343 data->pid = tsk->pid;
344 data->uid = task_uid(tsk);
345 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
346 data->policy = tsk->policy;
347 data->rt_priority = tsk->rt_priority;
349 /* record this tasks comm */
350 tracing_record_cmdline(tsk);
353 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
355 int len;
356 int ret;
358 if (!cnt)
359 return 0;
361 if (s->len <= s->readpos)
362 return -EBUSY;
364 len = s->len - s->readpos;
365 if (cnt > len)
366 cnt = len;
367 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
368 if (ret == cnt)
369 return -EFAULT;
371 cnt -= ret;
373 s->readpos += cnt;
374 return cnt;
377 ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
379 int len;
380 void *ret;
382 if (s->len <= s->readpos)
383 return -EBUSY;
385 len = s->len - s->readpos;
386 if (cnt > len)
387 cnt = len;
388 ret = memcpy(buf, s->buffer + s->readpos, cnt);
389 if (!ret)
390 return -EFAULT;
392 s->readpos += cnt;
393 return cnt;
396 static void
397 trace_print_seq(struct seq_file *m, struct trace_seq *s)
399 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
401 s->buffer[len] = 0;
402 seq_puts(m, s->buffer);
404 trace_seq_init(s);
408 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
409 * @tr: tracer
410 * @tsk: the task with the latency
411 * @cpu: The cpu that initiated the trace.
413 * Flip the buffers between the @tr and the max_tr and record information
414 * about which task was the cause of this latency.
416 void
417 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
419 struct ring_buffer *buf = tr->buffer;
421 WARN_ON_ONCE(!irqs_disabled());
422 __raw_spin_lock(&ftrace_max_lock);
424 tr->buffer = max_tr.buffer;
425 max_tr.buffer = buf;
427 ftrace_disable_cpu();
428 ring_buffer_reset(tr->buffer);
429 ftrace_enable_cpu();
431 __update_max_tr(tr, tsk, cpu);
432 __raw_spin_unlock(&ftrace_max_lock);
436 * update_max_tr_single - only copy one trace over, and reset the rest
437 * @tr - tracer
438 * @tsk - task with the latency
439 * @cpu - the cpu of the buffer to copy.
441 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
443 void
444 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
446 int ret;
448 WARN_ON_ONCE(!irqs_disabled());
449 __raw_spin_lock(&ftrace_max_lock);
451 ftrace_disable_cpu();
453 ring_buffer_reset(max_tr.buffer);
454 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
456 ftrace_enable_cpu();
458 WARN_ON_ONCE(ret && ret != -EAGAIN);
460 __update_max_tr(tr, tsk, cpu);
461 __raw_spin_unlock(&ftrace_max_lock);
465 * register_tracer - register a tracer with the ftrace system.
466 * @type - the plugin for the tracer
468 * Register a new plugin tracer.
470 int register_tracer(struct tracer *type)
471 __releases(kernel_lock)
472 __acquires(kernel_lock)
474 struct tracer *t;
475 int len;
476 int ret = 0;
478 if (!type->name) {
479 pr_info("Tracer must have a name\n");
480 return -1;
484 * When this gets called we hold the BKL which means that
485 * preemption is disabled. Various trace selftests however
486 * need to disable and enable preemption for successful tests.
487 * So we drop the BKL here and grab it after the tests again.
489 unlock_kernel();
490 mutex_lock(&trace_types_lock);
492 tracing_selftest_running = true;
494 for (t = trace_types; t; t = t->next) {
495 if (strcmp(type->name, t->name) == 0) {
496 /* already found */
497 pr_info("Trace %s already registered\n",
498 type->name);
499 ret = -1;
500 goto out;
504 if (!type->set_flag)
505 type->set_flag = &dummy_set_flag;
506 if (!type->flags)
507 type->flags = &dummy_tracer_flags;
508 else
509 if (!type->flags->opts)
510 type->flags->opts = dummy_tracer_opt;
511 if (!type->wait_pipe)
512 type->wait_pipe = default_wait_pipe;
515 #ifdef CONFIG_FTRACE_STARTUP_TEST
516 if (type->selftest && !tracing_selftest_disabled) {
517 struct tracer *saved_tracer = current_trace;
518 struct trace_array *tr = &global_trace;
519 int i;
522 * Run a selftest on this tracer.
523 * Here we reset the trace buffer, and set the current
524 * tracer to be this tracer. The tracer can then run some
525 * internal tracing to verify that everything is in order.
526 * If we fail, we do not register this tracer.
528 for_each_tracing_cpu(i)
529 tracing_reset(tr, i);
531 current_trace = type;
532 /* the test is responsible for initializing and enabling */
533 pr_info("Testing tracer %s: ", type->name);
534 ret = type->selftest(type, tr);
535 /* the test is responsible for resetting too */
536 current_trace = saved_tracer;
537 if (ret) {
538 printk(KERN_CONT "FAILED!\n");
539 goto out;
541 /* Only reset on passing, to avoid touching corrupted buffers */
542 for_each_tracing_cpu(i)
543 tracing_reset(tr, i);
545 printk(KERN_CONT "PASSED\n");
547 #endif
549 type->next = trace_types;
550 trace_types = type;
551 len = strlen(type->name);
552 if (len > max_tracer_type_len)
553 max_tracer_type_len = len;
555 out:
556 tracing_selftest_running = false;
557 mutex_unlock(&trace_types_lock);
559 if (ret || !default_bootup_tracer)
560 goto out_unlock;
562 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
563 goto out_unlock;
565 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
566 /* Do we want this tracer to start on bootup? */
567 tracing_set_tracer(type->name);
568 default_bootup_tracer = NULL;
569 /* disable other selftests, since this will break it. */
570 tracing_selftest_disabled = 1;
571 #ifdef CONFIG_FTRACE_STARTUP_TEST
572 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
573 type->name);
574 #endif
576 out_unlock:
577 lock_kernel();
578 return ret;
581 void unregister_tracer(struct tracer *type)
583 struct tracer **t;
584 int len;
586 mutex_lock(&trace_types_lock);
587 for (t = &trace_types; *t; t = &(*t)->next) {
588 if (*t == type)
589 goto found;
591 pr_info("Trace %s not registered\n", type->name);
592 goto out;
594 found:
595 *t = (*t)->next;
597 if (type == current_trace && tracer_enabled) {
598 tracer_enabled = 0;
599 tracing_stop();
600 if (current_trace->stop)
601 current_trace->stop(&global_trace);
602 current_trace = &nop_trace;
605 if (strlen(type->name) != max_tracer_type_len)
606 goto out;
608 max_tracer_type_len = 0;
609 for (t = &trace_types; *t; t = &(*t)->next) {
610 len = strlen((*t)->name);
611 if (len > max_tracer_type_len)
612 max_tracer_type_len = len;
614 out:
615 mutex_unlock(&trace_types_lock);
618 void tracing_reset(struct trace_array *tr, int cpu)
620 ftrace_disable_cpu();
621 ring_buffer_reset_cpu(tr->buffer, cpu);
622 ftrace_enable_cpu();
625 void tracing_reset_online_cpus(struct trace_array *tr)
627 int cpu;
629 tr->time_start = ftrace_now(tr->cpu);
631 for_each_online_cpu(cpu)
632 tracing_reset(tr, cpu);
635 #define SAVED_CMDLINES 128
636 #define NO_CMDLINE_MAP UINT_MAX
637 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
638 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
639 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
640 static int cmdline_idx;
641 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
643 /* temporary disable recording */
644 static atomic_t trace_record_cmdline_disabled __read_mostly;
646 static void trace_init_cmdlines(void)
648 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
649 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
650 cmdline_idx = 0;
653 static int trace_stop_count;
654 static DEFINE_SPINLOCK(tracing_start_lock);
657 * ftrace_off_permanent - disable all ftrace code permanently
659 * This should only be called when a serious anomally has
660 * been detected. This will turn off the function tracing,
661 * ring buffers, and other tracing utilites. It takes no
662 * locks and can be called from any context.
664 void ftrace_off_permanent(void)
666 tracing_disabled = 1;
667 ftrace_stop();
668 tracing_off_permanent();
672 * tracing_start - quick start of the tracer
674 * If tracing is enabled but was stopped by tracing_stop,
675 * this will start the tracer back up.
677 void tracing_start(void)
679 struct ring_buffer *buffer;
680 unsigned long flags;
682 if (tracing_disabled)
683 return;
685 spin_lock_irqsave(&tracing_start_lock, flags);
686 if (--trace_stop_count) {
687 if (trace_stop_count < 0) {
688 /* Someone screwed up their debugging */
689 WARN_ON_ONCE(1);
690 trace_stop_count = 0;
692 goto out;
696 buffer = global_trace.buffer;
697 if (buffer)
698 ring_buffer_record_enable(buffer);
700 buffer = max_tr.buffer;
701 if (buffer)
702 ring_buffer_record_enable(buffer);
704 ftrace_start();
705 out:
706 spin_unlock_irqrestore(&tracing_start_lock, flags);
710 * tracing_stop - quick stop of the tracer
712 * Light weight way to stop tracing. Use in conjunction with
713 * tracing_start.
715 void tracing_stop(void)
717 struct ring_buffer *buffer;
718 unsigned long flags;
720 ftrace_stop();
721 spin_lock_irqsave(&tracing_start_lock, flags);
722 if (trace_stop_count++)
723 goto out;
725 buffer = global_trace.buffer;
726 if (buffer)
727 ring_buffer_record_disable(buffer);
729 buffer = max_tr.buffer;
730 if (buffer)
731 ring_buffer_record_disable(buffer);
733 out:
734 spin_unlock_irqrestore(&tracing_start_lock, flags);
737 void trace_stop_cmdline_recording(void);
739 static void trace_save_cmdline(struct task_struct *tsk)
741 unsigned map;
742 unsigned idx;
744 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
745 return;
748 * It's not the end of the world if we don't get
749 * the lock, but we also don't want to spin
750 * nor do we want to disable interrupts,
751 * so if we miss here, then better luck next time.
753 if (!__raw_spin_trylock(&trace_cmdline_lock))
754 return;
756 idx = map_pid_to_cmdline[tsk->pid];
757 if (idx == NO_CMDLINE_MAP) {
758 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
760 map = map_cmdline_to_pid[idx];
761 if (map != NO_CMDLINE_MAP)
762 map_pid_to_cmdline[map] = NO_CMDLINE_MAP;
764 map_pid_to_cmdline[tsk->pid] = idx;
766 cmdline_idx = idx;
769 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
771 __raw_spin_unlock(&trace_cmdline_lock);
774 void trace_find_cmdline(int pid, char comm[])
776 unsigned map;
778 if (!pid) {
779 strcpy(comm, "<idle>");
780 return;
783 if (pid > PID_MAX_DEFAULT) {
784 strcpy(comm, "<...>");
785 return;
788 __raw_spin_lock(&trace_cmdline_lock);
789 map = map_pid_to_cmdline[pid];
790 if (map != NO_CMDLINE_MAP)
791 strcpy(comm, saved_cmdlines[map]);
792 else
793 strcpy(comm, "<...>");
795 __raw_spin_unlock(&trace_cmdline_lock);
798 void tracing_record_cmdline(struct task_struct *tsk)
800 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
801 !tracing_is_on())
802 return;
804 trace_save_cmdline(tsk);
807 void
808 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
809 int pc)
811 struct task_struct *tsk = current;
813 entry->preempt_count = pc & 0xff;
814 entry->pid = (tsk) ? tsk->pid : 0;
815 entry->tgid = (tsk) ? tsk->tgid : 0;
816 entry->flags =
817 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
818 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
819 #else
820 TRACE_FLAG_IRQS_NOSUPPORT |
821 #endif
822 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
823 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
824 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
827 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
828 unsigned char type,
829 unsigned long len,
830 unsigned long flags, int pc)
832 struct ring_buffer_event *event;
834 event = ring_buffer_lock_reserve(tr->buffer, len);
835 if (event != NULL) {
836 struct trace_entry *ent = ring_buffer_event_data(event);
838 tracing_generic_entry_update(ent, flags, pc);
839 ent->type = type;
842 return event;
844 static void ftrace_trace_stack(struct trace_array *tr,
845 unsigned long flags, int skip, int pc);
846 static void ftrace_trace_userstack(struct trace_array *tr,
847 unsigned long flags, int pc);
849 void trace_buffer_unlock_commit(struct trace_array *tr,
850 struct ring_buffer_event *event,
851 unsigned long flags, int pc)
853 ring_buffer_unlock_commit(tr->buffer, event);
855 ftrace_trace_stack(tr, flags, 6, pc);
856 ftrace_trace_userstack(tr, flags, pc);
857 trace_wake_up();
860 struct ring_buffer_event *
861 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
862 unsigned long flags, int pc)
864 return trace_buffer_lock_reserve(&global_trace,
865 type, len, flags, pc);
868 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
869 unsigned long flags, int pc)
871 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
874 void
875 trace_function(struct trace_array *tr,
876 unsigned long ip, unsigned long parent_ip, unsigned long flags,
877 int pc)
879 struct ring_buffer_event *event;
880 struct ftrace_entry *entry;
882 /* If we are reading the ring buffer, don't trace */
883 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
884 return;
886 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
887 flags, pc);
888 if (!event)
889 return;
890 entry = ring_buffer_event_data(event);
891 entry->ip = ip;
892 entry->parent_ip = parent_ip;
893 ring_buffer_unlock_commit(tr->buffer, event);
896 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
897 static void __trace_graph_entry(struct trace_array *tr,
898 struct ftrace_graph_ent *trace,
899 unsigned long flags,
900 int pc)
902 struct ring_buffer_event *event;
903 struct ftrace_graph_ent_entry *entry;
905 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
906 return;
908 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
909 sizeof(*entry), flags, pc);
910 if (!event)
911 return;
912 entry = ring_buffer_event_data(event);
913 entry->graph_ent = *trace;
914 ring_buffer_unlock_commit(global_trace.buffer, event);
917 static void __trace_graph_return(struct trace_array *tr,
918 struct ftrace_graph_ret *trace,
919 unsigned long flags,
920 int pc)
922 struct ring_buffer_event *event;
923 struct ftrace_graph_ret_entry *entry;
925 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
926 return;
928 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
929 sizeof(*entry), flags, pc);
930 if (!event)
931 return;
932 entry = ring_buffer_event_data(event);
933 entry->ret = *trace;
934 ring_buffer_unlock_commit(global_trace.buffer, event);
936 #endif
938 void
939 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
940 unsigned long ip, unsigned long parent_ip, unsigned long flags,
941 int pc)
943 if (likely(!atomic_read(&data->disabled)))
944 trace_function(tr, ip, parent_ip, flags, pc);
947 static void __ftrace_trace_stack(struct trace_array *tr,
948 unsigned long flags,
949 int skip, int pc)
951 #ifdef CONFIG_STACKTRACE
952 struct ring_buffer_event *event;
953 struct stack_entry *entry;
954 struct stack_trace trace;
956 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
957 sizeof(*entry), flags, pc);
958 if (!event)
959 return;
960 entry = ring_buffer_event_data(event);
961 memset(&entry->caller, 0, sizeof(entry->caller));
963 trace.nr_entries = 0;
964 trace.max_entries = FTRACE_STACK_ENTRIES;
965 trace.skip = skip;
966 trace.entries = entry->caller;
968 save_stack_trace(&trace);
969 ring_buffer_unlock_commit(tr->buffer, event);
970 #endif
973 static void ftrace_trace_stack(struct trace_array *tr,
974 unsigned long flags,
975 int skip, int pc)
977 if (!(trace_flags & TRACE_ITER_STACKTRACE))
978 return;
980 __ftrace_trace_stack(tr, flags, skip, pc);
983 void __trace_stack(struct trace_array *tr,
984 unsigned long flags,
985 int skip, int pc)
987 __ftrace_trace_stack(tr, flags, skip, pc);
990 static void ftrace_trace_userstack(struct trace_array *tr,
991 unsigned long flags, int pc)
993 #ifdef CONFIG_STACKTRACE
994 struct ring_buffer_event *event;
995 struct userstack_entry *entry;
996 struct stack_trace trace;
998 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
999 return;
1001 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1002 sizeof(*entry), flags, pc);
1003 if (!event)
1004 return;
1005 entry = ring_buffer_event_data(event);
1007 memset(&entry->caller, 0, sizeof(entry->caller));
1009 trace.nr_entries = 0;
1010 trace.max_entries = FTRACE_STACK_ENTRIES;
1011 trace.skip = 0;
1012 trace.entries = entry->caller;
1014 save_stack_trace_user(&trace);
1015 ring_buffer_unlock_commit(tr->buffer, event);
1016 #endif
1019 #ifdef UNUSED
1020 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1022 ftrace_trace_userstack(tr, flags, preempt_count());
1024 #endif /* UNUSED */
1026 static void
1027 ftrace_trace_special(void *__tr,
1028 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1029 int pc)
1031 struct ring_buffer_event *event;
1032 struct trace_array *tr = __tr;
1033 struct special_entry *entry;
1035 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1036 sizeof(*entry), 0, pc);
1037 if (!event)
1038 return;
1039 entry = ring_buffer_event_data(event);
1040 entry->arg1 = arg1;
1041 entry->arg2 = arg2;
1042 entry->arg3 = arg3;
1043 trace_buffer_unlock_commit(tr, event, 0, pc);
1046 void
1047 __trace_special(void *__tr, void *__data,
1048 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1050 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1053 void
1054 tracing_sched_switch_trace(struct trace_array *tr,
1055 struct task_struct *prev,
1056 struct task_struct *next,
1057 unsigned long flags, int pc)
1059 struct ring_buffer_event *event;
1060 struct ctx_switch_entry *entry;
1062 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1063 sizeof(*entry), flags, pc);
1064 if (!event)
1065 return;
1066 entry = ring_buffer_event_data(event);
1067 entry->prev_pid = prev->pid;
1068 entry->prev_prio = prev->prio;
1069 entry->prev_state = prev->state;
1070 entry->next_pid = next->pid;
1071 entry->next_prio = next->prio;
1072 entry->next_state = next->state;
1073 entry->next_cpu = task_cpu(next);
1074 trace_buffer_unlock_commit(tr, event, flags, pc);
1077 void
1078 tracing_sched_wakeup_trace(struct trace_array *tr,
1079 struct task_struct *wakee,
1080 struct task_struct *curr,
1081 unsigned long flags, int pc)
1083 struct ring_buffer_event *event;
1084 struct ctx_switch_entry *entry;
1086 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1087 sizeof(*entry), flags, pc);
1088 if (!event)
1089 return;
1090 entry = ring_buffer_event_data(event);
1091 entry->prev_pid = curr->pid;
1092 entry->prev_prio = curr->prio;
1093 entry->prev_state = curr->state;
1094 entry->next_pid = wakee->pid;
1095 entry->next_prio = wakee->prio;
1096 entry->next_state = wakee->state;
1097 entry->next_cpu = task_cpu(wakee);
1099 ring_buffer_unlock_commit(tr->buffer, event);
1100 ftrace_trace_stack(tr, flags, 6, pc);
1101 ftrace_trace_userstack(tr, flags, pc);
1104 void
1105 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1107 struct trace_array *tr = &global_trace;
1108 struct trace_array_cpu *data;
1109 unsigned long flags;
1110 int cpu;
1111 int pc;
1113 if (tracing_disabled)
1114 return;
1116 pc = preempt_count();
1117 local_irq_save(flags);
1118 cpu = raw_smp_processor_id();
1119 data = tr->data[cpu];
1121 if (likely(atomic_inc_return(&data->disabled) == 1))
1122 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1124 atomic_dec(&data->disabled);
1125 local_irq_restore(flags);
1128 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1129 int trace_graph_entry(struct ftrace_graph_ent *trace)
1131 struct trace_array *tr = &global_trace;
1132 struct trace_array_cpu *data;
1133 unsigned long flags;
1134 long disabled;
1135 int cpu;
1136 int pc;
1138 if (!ftrace_trace_task(current))
1139 return 0;
1141 if (!ftrace_graph_addr(trace->func))
1142 return 0;
1144 local_irq_save(flags);
1145 cpu = raw_smp_processor_id();
1146 data = tr->data[cpu];
1147 disabled = atomic_inc_return(&data->disabled);
1148 if (likely(disabled == 1)) {
1149 pc = preempt_count();
1150 __trace_graph_entry(tr, trace, flags, pc);
1152 /* Only do the atomic if it is not already set */
1153 if (!test_tsk_trace_graph(current))
1154 set_tsk_trace_graph(current);
1155 atomic_dec(&data->disabled);
1156 local_irq_restore(flags);
1158 return 1;
1161 void trace_graph_return(struct ftrace_graph_ret *trace)
1163 struct trace_array *tr = &global_trace;
1164 struct trace_array_cpu *data;
1165 unsigned long flags;
1166 long disabled;
1167 int cpu;
1168 int pc;
1170 local_irq_save(flags);
1171 cpu = raw_smp_processor_id();
1172 data = tr->data[cpu];
1173 disabled = atomic_inc_return(&data->disabled);
1174 if (likely(disabled == 1)) {
1175 pc = preempt_count();
1176 __trace_graph_return(tr, trace, flags, pc);
1178 if (!trace->depth)
1179 clear_tsk_trace_graph(current);
1180 atomic_dec(&data->disabled);
1181 local_irq_restore(flags);
1183 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1187 * trace_vbprintk - write binary msg to tracing buffer
1190 int trace_vbprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1192 static raw_spinlock_t trace_buf_lock =
1193 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1194 static u32 trace_buf[TRACE_BUF_SIZE];
1196 struct ring_buffer_event *event;
1197 struct trace_array *tr = &global_trace;
1198 struct trace_array_cpu *data;
1199 struct bprint_entry *entry;
1200 unsigned long flags;
1201 int resched;
1202 int cpu, len = 0, size, pc;
1204 if (unlikely(tracing_selftest_running || tracing_disabled))
1205 return 0;
1207 /* Don't pollute graph traces with trace_vprintk internals */
1208 pause_graph_tracing();
1210 pc = preempt_count();
1211 resched = ftrace_preempt_disable();
1212 cpu = raw_smp_processor_id();
1213 data = tr->data[cpu];
1215 if (unlikely(atomic_read(&data->disabled)))
1216 goto out;
1218 /* Lockdep uses trace_printk for lock tracing */
1219 local_irq_save(flags);
1220 __raw_spin_lock(&trace_buf_lock);
1221 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1223 if (len > TRACE_BUF_SIZE || len < 0)
1224 goto out_unlock;
1226 size = sizeof(*entry) + sizeof(u32) * len;
1227 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1228 if (!event)
1229 goto out_unlock;
1230 entry = ring_buffer_event_data(event);
1231 entry->ip = ip;
1232 entry->depth = depth;
1233 entry->fmt = fmt;
1235 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1236 ring_buffer_unlock_commit(tr->buffer, event);
1238 out_unlock:
1239 __raw_spin_unlock(&trace_buf_lock);
1240 local_irq_restore(flags);
1242 out:
1243 ftrace_preempt_enable(resched);
1244 unpause_graph_tracing();
1246 return len;
1248 EXPORT_SYMBOL_GPL(trace_vbprintk);
1250 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1252 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1253 static char trace_buf[TRACE_BUF_SIZE];
1255 struct ring_buffer_event *event;
1256 struct trace_array *tr = &global_trace;
1257 struct trace_array_cpu *data;
1258 int cpu, len = 0, size, pc;
1259 struct print_entry *entry;
1260 unsigned long irq_flags;
1262 if (tracing_disabled || tracing_selftest_running)
1263 return 0;
1265 pc = preempt_count();
1266 preempt_disable_notrace();
1267 cpu = raw_smp_processor_id();
1268 data = tr->data[cpu];
1270 if (unlikely(atomic_read(&data->disabled)))
1271 goto out;
1273 pause_graph_tracing();
1274 raw_local_irq_save(irq_flags);
1275 __raw_spin_lock(&trace_buf_lock);
1276 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1278 len = min(len, TRACE_BUF_SIZE-1);
1279 trace_buf[len] = 0;
1281 size = sizeof(*entry) + len + 1;
1282 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1283 if (!event)
1284 goto out_unlock;
1285 entry = ring_buffer_event_data(event);
1286 entry->ip = ip;
1287 entry->depth = depth;
1289 memcpy(&entry->buf, trace_buf, len);
1290 entry->buf[len] = 0;
1291 ring_buffer_unlock_commit(tr->buffer, event);
1293 out_unlock:
1294 __raw_spin_unlock(&trace_buf_lock);
1295 raw_local_irq_restore(irq_flags);
1296 unpause_graph_tracing();
1297 out:
1298 preempt_enable_notrace();
1300 return len;
1302 EXPORT_SYMBOL_GPL(trace_vprintk);
1304 enum trace_file_type {
1305 TRACE_FILE_LAT_FMT = 1,
1306 TRACE_FILE_ANNOTATE = 2,
1309 static void trace_iterator_increment(struct trace_iterator *iter)
1311 /* Don't allow ftrace to trace into the ring buffers */
1312 ftrace_disable_cpu();
1314 iter->idx++;
1315 if (iter->buffer_iter[iter->cpu])
1316 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1318 ftrace_enable_cpu();
1321 static struct trace_entry *
1322 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1324 struct ring_buffer_event *event;
1325 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1327 /* Don't allow ftrace to trace into the ring buffers */
1328 ftrace_disable_cpu();
1330 if (buf_iter)
1331 event = ring_buffer_iter_peek(buf_iter, ts);
1332 else
1333 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1335 ftrace_enable_cpu();
1337 return event ? ring_buffer_event_data(event) : NULL;
1340 static struct trace_entry *
1341 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1343 struct ring_buffer *buffer = iter->tr->buffer;
1344 struct trace_entry *ent, *next = NULL;
1345 int cpu_file = iter->cpu_file;
1346 u64 next_ts = 0, ts;
1347 int next_cpu = -1;
1348 int cpu;
1351 * If we are in a per_cpu trace file, don't bother by iterating over
1352 * all cpu and peek directly.
1354 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1355 if (ring_buffer_empty_cpu(buffer, cpu_file))
1356 return NULL;
1357 ent = peek_next_entry(iter, cpu_file, ent_ts);
1358 if (ent_cpu)
1359 *ent_cpu = cpu_file;
1361 return ent;
1364 for_each_tracing_cpu(cpu) {
1366 if (ring_buffer_empty_cpu(buffer, cpu))
1367 continue;
1369 ent = peek_next_entry(iter, cpu, &ts);
1372 * Pick the entry with the smallest timestamp:
1374 if (ent && (!next || ts < next_ts)) {
1375 next = ent;
1376 next_cpu = cpu;
1377 next_ts = ts;
1381 if (ent_cpu)
1382 *ent_cpu = next_cpu;
1384 if (ent_ts)
1385 *ent_ts = next_ts;
1387 return next;
1390 /* Find the next real entry, without updating the iterator itself */
1391 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1392 int *ent_cpu, u64 *ent_ts)
1394 return __find_next_entry(iter, ent_cpu, ent_ts);
1397 /* Find the next real entry, and increment the iterator to the next entry */
1398 static void *find_next_entry_inc(struct trace_iterator *iter)
1400 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1402 if (iter->ent)
1403 trace_iterator_increment(iter);
1405 return iter->ent ? iter : NULL;
1408 static void trace_consume(struct trace_iterator *iter)
1410 /* Don't allow ftrace to trace into the ring buffers */
1411 ftrace_disable_cpu();
1412 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1413 ftrace_enable_cpu();
1416 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1418 struct trace_iterator *iter = m->private;
1419 int i = (int)*pos;
1420 void *ent;
1422 (*pos)++;
1424 /* can't go backwards */
1425 if (iter->idx > i)
1426 return NULL;
1428 if (iter->idx < 0)
1429 ent = find_next_entry_inc(iter);
1430 else
1431 ent = iter;
1433 while (ent && iter->idx < i)
1434 ent = find_next_entry_inc(iter);
1436 iter->pos = *pos;
1438 return ent;
1442 * No necessary locking here. The worst thing which can
1443 * happen is loosing events consumed at the same time
1444 * by a trace_pipe reader.
1445 * Other than that, we don't risk to crash the ring buffer
1446 * because it serializes the readers.
1448 * The current tracer is copied to avoid a global locking
1449 * all around.
1451 static void *s_start(struct seq_file *m, loff_t *pos)
1453 struct trace_iterator *iter = m->private;
1454 static struct tracer *old_tracer;
1455 int cpu_file = iter->cpu_file;
1456 void *p = NULL;
1457 loff_t l = 0;
1458 int cpu;
1460 /* copy the tracer to avoid using a global lock all around */
1461 mutex_lock(&trace_types_lock);
1462 if (unlikely(old_tracer != current_trace && current_trace)) {
1463 old_tracer = current_trace;
1464 *iter->trace = *current_trace;
1466 mutex_unlock(&trace_types_lock);
1468 atomic_inc(&trace_record_cmdline_disabled);
1470 if (*pos != iter->pos) {
1471 iter->ent = NULL;
1472 iter->cpu = 0;
1473 iter->idx = -1;
1475 ftrace_disable_cpu();
1477 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1478 for_each_tracing_cpu(cpu)
1479 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1480 } else
1481 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1484 ftrace_enable_cpu();
1486 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1489 } else {
1490 l = *pos - 1;
1491 p = s_next(m, p, &l);
1494 return p;
1497 static void s_stop(struct seq_file *m, void *p)
1499 atomic_dec(&trace_record_cmdline_disabled);
1502 static void print_lat_help_header(struct seq_file *m)
1504 seq_puts(m, "# _------=> CPU# \n");
1505 seq_puts(m, "# / _-----=> irqs-off \n");
1506 seq_puts(m, "# | / _----=> need-resched \n");
1507 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1508 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1509 seq_puts(m, "# |||| / \n");
1510 seq_puts(m, "# ||||| delay \n");
1511 seq_puts(m, "# cmd pid ||||| time | caller \n");
1512 seq_puts(m, "# \\ / ||||| \\ | / \n");
1515 static void print_func_help_header(struct seq_file *m)
1517 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1518 seq_puts(m, "# | | | | |\n");
1522 static void
1523 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1525 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1526 struct trace_array *tr = iter->tr;
1527 struct trace_array_cpu *data = tr->data[tr->cpu];
1528 struct tracer *type = current_trace;
1529 unsigned long total;
1530 unsigned long entries;
1531 const char *name = "preemption";
1533 if (type)
1534 name = type->name;
1536 entries = ring_buffer_entries(iter->tr->buffer);
1537 total = entries +
1538 ring_buffer_overruns(iter->tr->buffer);
1540 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1541 name, UTS_RELEASE);
1542 seq_puts(m, "# -----------------------------------"
1543 "---------------------------------\n");
1544 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1545 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1546 nsecs_to_usecs(data->saved_latency),
1547 entries,
1548 total,
1549 tr->cpu,
1550 #if defined(CONFIG_PREEMPT_NONE)
1551 "server",
1552 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1553 "desktop",
1554 #elif defined(CONFIG_PREEMPT)
1555 "preempt",
1556 #else
1557 "unknown",
1558 #endif
1559 /* These are reserved for later use */
1560 0, 0, 0, 0);
1561 #ifdef CONFIG_SMP
1562 seq_printf(m, " #P:%d)\n", num_online_cpus());
1563 #else
1564 seq_puts(m, ")\n");
1565 #endif
1566 seq_puts(m, "# -----------------\n");
1567 seq_printf(m, "# | task: %.16s-%d "
1568 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1569 data->comm, data->pid, data->uid, data->nice,
1570 data->policy, data->rt_priority);
1571 seq_puts(m, "# -----------------\n");
1573 if (data->critical_start) {
1574 seq_puts(m, "# => started at: ");
1575 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1576 trace_print_seq(m, &iter->seq);
1577 seq_puts(m, "\n# => ended at: ");
1578 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1579 trace_print_seq(m, &iter->seq);
1580 seq_puts(m, "#\n");
1583 seq_puts(m, "#\n");
1586 static void test_cpu_buff_start(struct trace_iterator *iter)
1588 struct trace_seq *s = &iter->seq;
1590 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1591 return;
1593 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1594 return;
1596 if (cpumask_test_cpu(iter->cpu, iter->started))
1597 return;
1599 cpumask_set_cpu(iter->cpu, iter->started);
1600 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1603 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1605 struct trace_seq *s = &iter->seq;
1606 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1607 struct trace_entry *entry;
1608 struct trace_event *event;
1610 entry = iter->ent;
1612 test_cpu_buff_start(iter);
1614 event = ftrace_find_event(entry->type);
1616 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1617 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1618 if (!trace_print_lat_context(iter))
1619 goto partial;
1620 } else {
1621 if (!trace_print_context(iter))
1622 goto partial;
1626 if (event)
1627 return event->trace(iter, sym_flags);
1629 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1630 goto partial;
1632 return TRACE_TYPE_HANDLED;
1633 partial:
1634 return TRACE_TYPE_PARTIAL_LINE;
1637 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1639 struct trace_seq *s = &iter->seq;
1640 struct trace_entry *entry;
1641 struct trace_event *event;
1643 entry = iter->ent;
1645 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1646 if (!trace_seq_printf(s, "%d %d %llu ",
1647 entry->pid, iter->cpu, iter->ts))
1648 goto partial;
1651 event = ftrace_find_event(entry->type);
1652 if (event)
1653 return event->raw(iter, 0);
1655 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1656 goto partial;
1658 return TRACE_TYPE_HANDLED;
1659 partial:
1660 return TRACE_TYPE_PARTIAL_LINE;
1663 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1665 struct trace_seq *s = &iter->seq;
1666 unsigned char newline = '\n';
1667 struct trace_entry *entry;
1668 struct trace_event *event;
1670 entry = iter->ent;
1672 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1673 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1674 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1675 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1678 event = ftrace_find_event(entry->type);
1679 if (event) {
1680 enum print_line_t ret = event->hex(iter, 0);
1681 if (ret != TRACE_TYPE_HANDLED)
1682 return ret;
1685 SEQ_PUT_FIELD_RET(s, newline);
1687 return TRACE_TYPE_HANDLED;
1690 static enum print_line_t print_bprintk_msg_only(struct trace_iterator *iter)
1692 struct trace_seq *s = &iter->seq;
1693 struct trace_entry *entry = iter->ent;
1694 struct bprint_entry *field;
1695 int ret;
1697 trace_assign_type(field, entry);
1699 ret = trace_seq_bprintf(s, field->fmt, field->buf);
1700 if (!ret)
1701 return TRACE_TYPE_PARTIAL_LINE;
1703 return TRACE_TYPE_HANDLED;
1706 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1708 struct trace_seq *s = &iter->seq;
1709 struct trace_entry *entry = iter->ent;
1710 struct print_entry *field;
1711 int ret;
1713 trace_assign_type(field, entry);
1715 ret = trace_seq_printf(s, "%s", field->buf);
1716 if (!ret)
1717 return TRACE_TYPE_PARTIAL_LINE;
1719 return TRACE_TYPE_HANDLED;
1722 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1724 struct trace_seq *s = &iter->seq;
1725 struct trace_entry *entry;
1726 struct trace_event *event;
1728 entry = iter->ent;
1730 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1731 SEQ_PUT_FIELD_RET(s, entry->pid);
1732 SEQ_PUT_FIELD_RET(s, iter->cpu);
1733 SEQ_PUT_FIELD_RET(s, iter->ts);
1736 event = ftrace_find_event(entry->type);
1737 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1740 static int trace_empty(struct trace_iterator *iter)
1742 int cpu;
1744 /* If we are looking at one CPU buffer, only check that one */
1745 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1746 cpu = iter->cpu_file;
1747 if (iter->buffer_iter[cpu]) {
1748 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1749 return 0;
1750 } else {
1751 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1752 return 0;
1754 return 1;
1757 for_each_tracing_cpu(cpu) {
1758 if (iter->buffer_iter[cpu]) {
1759 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1760 return 0;
1761 } else {
1762 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1763 return 0;
1767 return 1;
1770 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1772 enum print_line_t ret;
1774 if (iter->trace && iter->trace->print_line) {
1775 ret = iter->trace->print_line(iter);
1776 if (ret != TRACE_TYPE_UNHANDLED)
1777 return ret;
1780 if (iter->ent->type == TRACE_BPRINT &&
1781 trace_flags & TRACE_ITER_PRINTK &&
1782 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1783 return print_bprintk_msg_only(iter);
1785 if (iter->ent->type == TRACE_PRINT &&
1786 trace_flags & TRACE_ITER_PRINTK &&
1787 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1788 return print_printk_msg_only(iter);
1790 if (trace_flags & TRACE_ITER_BIN)
1791 return print_bin_fmt(iter);
1793 if (trace_flags & TRACE_ITER_HEX)
1794 return print_hex_fmt(iter);
1796 if (trace_flags & TRACE_ITER_RAW)
1797 return print_raw_fmt(iter);
1799 return print_trace_fmt(iter);
1802 static int s_show(struct seq_file *m, void *v)
1804 struct trace_iterator *iter = v;
1806 if (iter->ent == NULL) {
1807 if (iter->tr) {
1808 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1809 seq_puts(m, "#\n");
1811 if (iter->trace && iter->trace->print_header)
1812 iter->trace->print_header(m);
1813 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1814 /* print nothing if the buffers are empty */
1815 if (trace_empty(iter))
1816 return 0;
1817 print_trace_header(m, iter);
1818 if (!(trace_flags & TRACE_ITER_VERBOSE))
1819 print_lat_help_header(m);
1820 } else {
1821 if (!(trace_flags & TRACE_ITER_VERBOSE))
1822 print_func_help_header(m);
1824 } else {
1825 print_trace_line(iter);
1826 trace_print_seq(m, &iter->seq);
1829 return 0;
1832 static struct seq_operations tracer_seq_ops = {
1833 .start = s_start,
1834 .next = s_next,
1835 .stop = s_stop,
1836 .show = s_show,
1839 static struct trace_iterator *
1840 __tracing_open(struct inode *inode, struct file *file)
1842 long cpu_file = (long) inode->i_private;
1843 void *fail_ret = ERR_PTR(-ENOMEM);
1844 struct trace_iterator *iter;
1845 struct seq_file *m;
1846 int cpu, ret;
1848 if (tracing_disabled)
1849 return ERR_PTR(-ENODEV);
1851 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1852 if (!iter)
1853 return ERR_PTR(-ENOMEM);
1856 * We make a copy of the current tracer to avoid concurrent
1857 * changes on it while we are reading.
1859 mutex_lock(&trace_types_lock);
1860 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1861 if (!iter->trace)
1862 goto fail;
1864 if (current_trace)
1865 *iter->trace = *current_trace;
1867 if (current_trace && current_trace->print_max)
1868 iter->tr = &max_tr;
1869 else
1870 iter->tr = &global_trace;
1871 iter->pos = -1;
1872 mutex_init(&iter->mutex);
1873 iter->cpu_file = cpu_file;
1875 /* Notify the tracer early; before we stop tracing. */
1876 if (iter->trace && iter->trace->open)
1877 iter->trace->open(iter);
1879 /* Annotate start of buffers if we had overruns */
1880 if (ring_buffer_overruns(iter->tr->buffer))
1881 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1883 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1884 for_each_tracing_cpu(cpu) {
1886 iter->buffer_iter[cpu] =
1887 ring_buffer_read_start(iter->tr->buffer, cpu);
1889 } else {
1890 cpu = iter->cpu_file;
1891 iter->buffer_iter[cpu] =
1892 ring_buffer_read_start(iter->tr->buffer, cpu);
1895 /* TODO stop tracer */
1896 ret = seq_open(file, &tracer_seq_ops);
1897 if (ret < 0) {
1898 fail_ret = ERR_PTR(ret);
1899 goto fail_buffer;
1902 m = file->private_data;
1903 m->private = iter;
1905 /* stop the trace while dumping */
1906 tracing_stop();
1908 mutex_unlock(&trace_types_lock);
1910 return iter;
1912 fail_buffer:
1913 for_each_tracing_cpu(cpu) {
1914 if (iter->buffer_iter[cpu])
1915 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1917 fail:
1918 mutex_unlock(&trace_types_lock);
1919 kfree(iter->trace);
1920 kfree(iter);
1922 return fail_ret;
1925 int tracing_open_generic(struct inode *inode, struct file *filp)
1927 if (tracing_disabled)
1928 return -ENODEV;
1930 filp->private_data = inode->i_private;
1931 return 0;
1934 static int tracing_release(struct inode *inode, struct file *file)
1936 struct seq_file *m = (struct seq_file *)file->private_data;
1937 struct trace_iterator *iter = m->private;
1938 int cpu;
1940 mutex_lock(&trace_types_lock);
1941 for_each_tracing_cpu(cpu) {
1942 if (iter->buffer_iter[cpu])
1943 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1946 if (iter->trace && iter->trace->close)
1947 iter->trace->close(iter);
1949 /* reenable tracing if it was previously enabled */
1950 tracing_start();
1951 mutex_unlock(&trace_types_lock);
1953 seq_release(inode, file);
1954 mutex_destroy(&iter->mutex);
1955 kfree(iter->trace);
1956 kfree(iter);
1957 return 0;
1960 static int tracing_open(struct inode *inode, struct file *file)
1962 struct trace_iterator *iter;
1963 int ret = 0;
1965 iter = __tracing_open(inode, file);
1966 if (IS_ERR(iter))
1967 ret = PTR_ERR(iter);
1968 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1969 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1971 return ret;
1974 static void *
1975 t_next(struct seq_file *m, void *v, loff_t *pos)
1977 struct tracer *t = m->private;
1979 (*pos)++;
1981 if (t)
1982 t = t->next;
1984 m->private = t;
1986 return t;
1989 static void *t_start(struct seq_file *m, loff_t *pos)
1991 struct tracer *t = m->private;
1992 loff_t l = 0;
1994 mutex_lock(&trace_types_lock);
1995 for (; t && l < *pos; t = t_next(m, t, &l))
1998 return t;
2001 static void t_stop(struct seq_file *m, void *p)
2003 mutex_unlock(&trace_types_lock);
2006 static int t_show(struct seq_file *m, void *v)
2008 struct tracer *t = v;
2010 if (!t)
2011 return 0;
2013 seq_printf(m, "%s", t->name);
2014 if (t->next)
2015 seq_putc(m, ' ');
2016 else
2017 seq_putc(m, '\n');
2019 return 0;
2022 static struct seq_operations show_traces_seq_ops = {
2023 .start = t_start,
2024 .next = t_next,
2025 .stop = t_stop,
2026 .show = t_show,
2029 static int show_traces_open(struct inode *inode, struct file *file)
2031 int ret;
2033 if (tracing_disabled)
2034 return -ENODEV;
2036 ret = seq_open(file, &show_traces_seq_ops);
2037 if (!ret) {
2038 struct seq_file *m = file->private_data;
2039 m->private = trace_types;
2042 return ret;
2045 static const struct file_operations tracing_fops = {
2046 .open = tracing_open,
2047 .read = seq_read,
2048 .llseek = seq_lseek,
2049 .release = tracing_release,
2052 static const struct file_operations show_traces_fops = {
2053 .open = show_traces_open,
2054 .read = seq_read,
2055 .release = seq_release,
2059 * Only trace on a CPU if the bitmask is set:
2061 static cpumask_var_t tracing_cpumask;
2064 * The tracer itself will not take this lock, but still we want
2065 * to provide a consistent cpumask to user-space:
2067 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2070 * Temporary storage for the character representation of the
2071 * CPU bitmask (and one more byte for the newline):
2073 static char mask_str[NR_CPUS + 1];
2075 static ssize_t
2076 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2077 size_t count, loff_t *ppos)
2079 int len;
2081 mutex_lock(&tracing_cpumask_update_lock);
2083 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2084 if (count - len < 2) {
2085 count = -EINVAL;
2086 goto out_err;
2088 len += sprintf(mask_str + len, "\n");
2089 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2091 out_err:
2092 mutex_unlock(&tracing_cpumask_update_lock);
2094 return count;
2097 static ssize_t
2098 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2099 size_t count, loff_t *ppos)
2101 int err, cpu;
2102 cpumask_var_t tracing_cpumask_new;
2104 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2105 return -ENOMEM;
2107 mutex_lock(&tracing_cpumask_update_lock);
2108 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2109 if (err)
2110 goto err_unlock;
2112 local_irq_disable();
2113 __raw_spin_lock(&ftrace_max_lock);
2114 for_each_tracing_cpu(cpu) {
2116 * Increase/decrease the disabled counter if we are
2117 * about to flip a bit in the cpumask:
2119 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2120 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2121 atomic_inc(&global_trace.data[cpu]->disabled);
2123 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2124 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2125 atomic_dec(&global_trace.data[cpu]->disabled);
2128 __raw_spin_unlock(&ftrace_max_lock);
2129 local_irq_enable();
2131 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2133 mutex_unlock(&tracing_cpumask_update_lock);
2134 free_cpumask_var(tracing_cpumask_new);
2136 return count;
2138 err_unlock:
2139 mutex_unlock(&tracing_cpumask_update_lock);
2140 free_cpumask_var(tracing_cpumask);
2142 return err;
2145 static const struct file_operations tracing_cpumask_fops = {
2146 .open = tracing_open_generic,
2147 .read = tracing_cpumask_read,
2148 .write = tracing_cpumask_write,
2151 static ssize_t
2152 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2153 size_t cnt, loff_t *ppos)
2155 struct tracer_opt *trace_opts;
2156 u32 tracer_flags;
2157 int len = 0;
2158 char *buf;
2159 int r = 0;
2160 int i;
2163 /* calculate max size */
2164 for (i = 0; trace_options[i]; i++) {
2165 len += strlen(trace_options[i]);
2166 len += 3; /* "no" and newline */
2169 mutex_lock(&trace_types_lock);
2170 tracer_flags = current_trace->flags->val;
2171 trace_opts = current_trace->flags->opts;
2174 * Increase the size with names of options specific
2175 * of the current tracer.
2177 for (i = 0; trace_opts[i].name; i++) {
2178 len += strlen(trace_opts[i].name);
2179 len += 3; /* "no" and newline */
2182 /* +2 for \n and \0 */
2183 buf = kmalloc(len + 2, GFP_KERNEL);
2184 if (!buf) {
2185 mutex_unlock(&trace_types_lock);
2186 return -ENOMEM;
2189 for (i = 0; trace_options[i]; i++) {
2190 if (trace_flags & (1 << i))
2191 r += sprintf(buf + r, "%s\n", trace_options[i]);
2192 else
2193 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2196 for (i = 0; trace_opts[i].name; i++) {
2197 if (tracer_flags & trace_opts[i].bit)
2198 r += sprintf(buf + r, "%s\n",
2199 trace_opts[i].name);
2200 else
2201 r += sprintf(buf + r, "no%s\n",
2202 trace_opts[i].name);
2204 mutex_unlock(&trace_types_lock);
2206 WARN_ON(r >= len + 2);
2208 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2210 kfree(buf);
2211 return r;
2214 /* Try to assign a tracer specific option */
2215 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2217 struct tracer_flags *trace_flags = trace->flags;
2218 struct tracer_opt *opts = NULL;
2219 int ret = 0, i = 0;
2220 int len;
2222 for (i = 0; trace_flags->opts[i].name; i++) {
2223 opts = &trace_flags->opts[i];
2224 len = strlen(opts->name);
2226 if (strncmp(cmp, opts->name, len) == 0) {
2227 ret = trace->set_flag(trace_flags->val,
2228 opts->bit, !neg);
2229 break;
2232 /* Not found */
2233 if (!trace_flags->opts[i].name)
2234 return -EINVAL;
2236 /* Refused to handle */
2237 if (ret)
2238 return ret;
2240 if (neg)
2241 trace_flags->val &= ~opts->bit;
2242 else
2243 trace_flags->val |= opts->bit;
2245 return 0;
2248 static ssize_t
2249 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2250 size_t cnt, loff_t *ppos)
2252 char buf[64];
2253 char *cmp = buf;
2254 int neg = 0;
2255 int ret;
2256 int i;
2258 if (cnt >= sizeof(buf))
2259 return -EINVAL;
2261 if (copy_from_user(&buf, ubuf, cnt))
2262 return -EFAULT;
2264 buf[cnt] = 0;
2266 if (strncmp(buf, "no", 2) == 0) {
2267 neg = 1;
2268 cmp += 2;
2271 for (i = 0; trace_options[i]; i++) {
2272 int len = strlen(trace_options[i]);
2274 if (strncmp(cmp, trace_options[i], len) == 0) {
2275 if (neg)
2276 trace_flags &= ~(1 << i);
2277 else
2278 trace_flags |= (1 << i);
2279 break;
2283 /* If no option could be set, test the specific tracer options */
2284 if (!trace_options[i]) {
2285 mutex_lock(&trace_types_lock);
2286 ret = set_tracer_option(current_trace, cmp, neg);
2287 mutex_unlock(&trace_types_lock);
2288 if (ret)
2289 return ret;
2292 filp->f_pos += cnt;
2294 return cnt;
2297 static const struct file_operations tracing_iter_fops = {
2298 .open = tracing_open_generic,
2299 .read = tracing_trace_options_read,
2300 .write = tracing_trace_options_write,
2303 static const char readme_msg[] =
2304 "tracing mini-HOWTO:\n\n"
2305 "# mkdir /debug\n"
2306 "# mount -t debugfs nodev /debug\n\n"
2307 "# cat /debug/tracing/available_tracers\n"
2308 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2309 "# cat /debug/tracing/current_tracer\n"
2310 "none\n"
2311 "# echo sched_switch > /debug/tracing/current_tracer\n"
2312 "# cat /debug/tracing/current_tracer\n"
2313 "sched_switch\n"
2314 "# cat /debug/tracing/trace_options\n"
2315 "noprint-parent nosym-offset nosym-addr noverbose\n"
2316 "# echo print-parent > /debug/tracing/trace_options\n"
2317 "# echo 1 > /debug/tracing/tracing_enabled\n"
2318 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2319 "echo 0 > /debug/tracing/tracing_enabled\n"
2322 static ssize_t
2323 tracing_readme_read(struct file *filp, char __user *ubuf,
2324 size_t cnt, loff_t *ppos)
2326 return simple_read_from_buffer(ubuf, cnt, ppos,
2327 readme_msg, strlen(readme_msg));
2330 static const struct file_operations tracing_readme_fops = {
2331 .open = tracing_open_generic,
2332 .read = tracing_readme_read,
2335 static ssize_t
2336 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2337 size_t cnt, loff_t *ppos)
2339 char buf[64];
2340 int r;
2342 r = sprintf(buf, "%u\n", tracer_enabled);
2343 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2346 static ssize_t
2347 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2348 size_t cnt, loff_t *ppos)
2350 struct trace_array *tr = filp->private_data;
2351 char buf[64];
2352 unsigned long val;
2353 int ret;
2355 if (cnt >= sizeof(buf))
2356 return -EINVAL;
2358 if (copy_from_user(&buf, ubuf, cnt))
2359 return -EFAULT;
2361 buf[cnt] = 0;
2363 ret = strict_strtoul(buf, 10, &val);
2364 if (ret < 0)
2365 return ret;
2367 val = !!val;
2369 mutex_lock(&trace_types_lock);
2370 if (tracer_enabled ^ val) {
2371 if (val) {
2372 tracer_enabled = 1;
2373 if (current_trace->start)
2374 current_trace->start(tr);
2375 tracing_start();
2376 } else {
2377 tracer_enabled = 0;
2378 tracing_stop();
2379 if (current_trace->stop)
2380 current_trace->stop(tr);
2383 mutex_unlock(&trace_types_lock);
2385 filp->f_pos += cnt;
2387 return cnt;
2390 static ssize_t
2391 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2392 size_t cnt, loff_t *ppos)
2394 char buf[max_tracer_type_len+2];
2395 int r;
2397 mutex_lock(&trace_types_lock);
2398 if (current_trace)
2399 r = sprintf(buf, "%s\n", current_trace->name);
2400 else
2401 r = sprintf(buf, "\n");
2402 mutex_unlock(&trace_types_lock);
2404 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2407 int tracer_init(struct tracer *t, struct trace_array *tr)
2409 tracing_reset_online_cpus(tr);
2410 return t->init(tr);
2413 static int tracing_resize_ring_buffer(unsigned long size)
2415 int ret;
2418 * If kernel or user changes the size of the ring buffer
2419 * we use the size that was given, and we can forget about
2420 * expanding it later.
2422 ring_buffer_expanded = 1;
2424 ret = ring_buffer_resize(global_trace.buffer, size);
2425 if (ret < 0)
2426 return ret;
2428 ret = ring_buffer_resize(max_tr.buffer, size);
2429 if (ret < 0) {
2430 int r;
2432 r = ring_buffer_resize(global_trace.buffer,
2433 global_trace.entries);
2434 if (r < 0) {
2436 * AARGH! We are left with different
2437 * size max buffer!!!!
2438 * The max buffer is our "snapshot" buffer.
2439 * When a tracer needs a snapshot (one of the
2440 * latency tracers), it swaps the max buffer
2441 * with the saved snap shot. We succeeded to
2442 * update the size of the main buffer, but failed to
2443 * update the size of the max buffer. But when we tried
2444 * to reset the main buffer to the original size, we
2445 * failed there too. This is very unlikely to
2446 * happen, but if it does, warn and kill all
2447 * tracing.
2449 WARN_ON(1);
2450 tracing_disabled = 1;
2452 return ret;
2455 global_trace.entries = size;
2457 return ret;
2461 * tracing_update_buffers - used by tracing facility to expand ring buffers
2463 * To save on memory when the tracing is never used on a system with it
2464 * configured in. The ring buffers are set to a minimum size. But once
2465 * a user starts to use the tracing facility, then they need to grow
2466 * to their default size.
2468 * This function is to be called when a tracer is about to be used.
2470 int tracing_update_buffers(void)
2472 int ret = 0;
2474 mutex_lock(&trace_types_lock);
2475 if (!ring_buffer_expanded)
2476 ret = tracing_resize_ring_buffer(trace_buf_size);
2477 mutex_unlock(&trace_types_lock);
2479 return ret;
2482 struct trace_option_dentry;
2484 static struct trace_option_dentry *
2485 create_trace_option_files(struct tracer *tracer);
2487 static void
2488 destroy_trace_option_files(struct trace_option_dentry *topts);
2490 static int tracing_set_tracer(const char *buf)
2492 static struct trace_option_dentry *topts;
2493 struct trace_array *tr = &global_trace;
2494 struct tracer *t;
2495 int ret = 0;
2497 mutex_lock(&trace_types_lock);
2499 if (!ring_buffer_expanded) {
2500 ret = tracing_resize_ring_buffer(trace_buf_size);
2501 if (ret < 0)
2502 goto out;
2503 ret = 0;
2506 for (t = trace_types; t; t = t->next) {
2507 if (strcmp(t->name, buf) == 0)
2508 break;
2510 if (!t) {
2511 ret = -EINVAL;
2512 goto out;
2514 if (t == current_trace)
2515 goto out;
2517 trace_branch_disable();
2518 if (current_trace && current_trace->reset)
2519 current_trace->reset(tr);
2521 destroy_trace_option_files(topts);
2523 current_trace = t;
2525 topts = create_trace_option_files(current_trace);
2527 if (t->init) {
2528 ret = tracer_init(t, tr);
2529 if (ret)
2530 goto out;
2533 trace_branch_enable(tr);
2534 out:
2535 mutex_unlock(&trace_types_lock);
2537 return ret;
2540 static ssize_t
2541 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2542 size_t cnt, loff_t *ppos)
2544 char buf[max_tracer_type_len+1];
2545 int i;
2546 size_t ret;
2547 int err;
2549 ret = cnt;
2551 if (cnt > max_tracer_type_len)
2552 cnt = max_tracer_type_len;
2554 if (copy_from_user(&buf, ubuf, cnt))
2555 return -EFAULT;
2557 buf[cnt] = 0;
2559 /* strip ending whitespace. */
2560 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2561 buf[i] = 0;
2563 err = tracing_set_tracer(buf);
2564 if (err)
2565 return err;
2567 filp->f_pos += ret;
2569 return ret;
2572 static ssize_t
2573 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2574 size_t cnt, loff_t *ppos)
2576 unsigned long *ptr = filp->private_data;
2577 char buf[64];
2578 int r;
2580 r = snprintf(buf, sizeof(buf), "%ld\n",
2581 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2582 if (r > sizeof(buf))
2583 r = sizeof(buf);
2584 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2587 static ssize_t
2588 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2589 size_t cnt, loff_t *ppos)
2591 unsigned long *ptr = filp->private_data;
2592 char buf[64];
2593 unsigned long val;
2594 int ret;
2596 if (cnt >= sizeof(buf))
2597 return -EINVAL;
2599 if (copy_from_user(&buf, ubuf, cnt))
2600 return -EFAULT;
2602 buf[cnt] = 0;
2604 ret = strict_strtoul(buf, 10, &val);
2605 if (ret < 0)
2606 return ret;
2608 *ptr = val * 1000;
2610 return cnt;
2613 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2615 long cpu_file = (long) inode->i_private;
2616 struct trace_iterator *iter;
2617 int ret = 0;
2619 if (tracing_disabled)
2620 return -ENODEV;
2622 mutex_lock(&trace_types_lock);
2624 /* We only allow one reader per cpu */
2625 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2626 if (!cpumask_empty(tracing_reader_cpumask)) {
2627 ret = -EBUSY;
2628 goto out;
2630 cpumask_setall(tracing_reader_cpumask);
2631 } else {
2632 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2633 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2634 else {
2635 ret = -EBUSY;
2636 goto out;
2640 /* create a buffer to store the information to pass to userspace */
2641 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2642 if (!iter) {
2643 ret = -ENOMEM;
2644 goto out;
2648 * We make a copy of the current tracer to avoid concurrent
2649 * changes on it while we are reading.
2651 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2652 if (!iter->trace) {
2653 ret = -ENOMEM;
2654 goto fail;
2656 if (current_trace)
2657 *iter->trace = *current_trace;
2659 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2660 ret = -ENOMEM;
2661 goto fail;
2664 /* trace pipe does not show start of buffer */
2665 cpumask_setall(iter->started);
2667 iter->cpu_file = cpu_file;
2668 iter->tr = &global_trace;
2669 mutex_init(&iter->mutex);
2670 filp->private_data = iter;
2672 if (iter->trace->pipe_open)
2673 iter->trace->pipe_open(iter);
2675 out:
2676 mutex_unlock(&trace_types_lock);
2677 return ret;
2679 fail:
2680 kfree(iter->trace);
2681 kfree(iter);
2682 mutex_unlock(&trace_types_lock);
2683 return ret;
2686 static int tracing_release_pipe(struct inode *inode, struct file *file)
2688 struct trace_iterator *iter = file->private_data;
2690 mutex_lock(&trace_types_lock);
2692 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2693 cpumask_clear(tracing_reader_cpumask);
2694 else
2695 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2697 mutex_unlock(&trace_types_lock);
2699 free_cpumask_var(iter->started);
2700 mutex_destroy(&iter->mutex);
2701 kfree(iter->trace);
2702 kfree(iter);
2704 return 0;
2707 static unsigned int
2708 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2710 struct trace_iterator *iter = filp->private_data;
2712 if (trace_flags & TRACE_ITER_BLOCK) {
2714 * Always select as readable when in blocking mode
2716 return POLLIN | POLLRDNORM;
2717 } else {
2718 if (!trace_empty(iter))
2719 return POLLIN | POLLRDNORM;
2720 poll_wait(filp, &trace_wait, poll_table);
2721 if (!trace_empty(iter))
2722 return POLLIN | POLLRDNORM;
2724 return 0;
2729 void default_wait_pipe(struct trace_iterator *iter)
2731 DEFINE_WAIT(wait);
2733 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2735 if (trace_empty(iter))
2736 schedule();
2738 finish_wait(&trace_wait, &wait);
2742 * This is a make-shift waitqueue.
2743 * A tracer might use this callback on some rare cases:
2745 * 1) the current tracer might hold the runqueue lock when it wakes up
2746 * a reader, hence a deadlock (sched, function, and function graph tracers)
2747 * 2) the function tracers, trace all functions, we don't want
2748 * the overhead of calling wake_up and friends
2749 * (and tracing them too)
2751 * Anyway, this is really very primitive wakeup.
2753 void poll_wait_pipe(struct trace_iterator *iter)
2755 set_current_state(TASK_INTERRUPTIBLE);
2756 /* sleep for 100 msecs, and try again. */
2757 schedule_timeout(HZ / 10);
2760 /* Must be called with trace_types_lock mutex held. */
2761 static int tracing_wait_pipe(struct file *filp)
2763 struct trace_iterator *iter = filp->private_data;
2765 while (trace_empty(iter)) {
2767 if ((filp->f_flags & O_NONBLOCK)) {
2768 return -EAGAIN;
2771 mutex_unlock(&iter->mutex);
2773 iter->trace->wait_pipe(iter);
2775 mutex_lock(&iter->mutex);
2777 if (signal_pending(current))
2778 return -EINTR;
2781 * We block until we read something and tracing is disabled.
2782 * We still block if tracing is disabled, but we have never
2783 * read anything. This allows a user to cat this file, and
2784 * then enable tracing. But after we have read something,
2785 * we give an EOF when tracing is again disabled.
2787 * iter->pos will be 0 if we haven't read anything.
2789 if (!tracer_enabled && iter->pos)
2790 break;
2793 return 1;
2797 * Consumer reader.
2799 static ssize_t
2800 tracing_read_pipe(struct file *filp, char __user *ubuf,
2801 size_t cnt, loff_t *ppos)
2803 struct trace_iterator *iter = filp->private_data;
2804 static struct tracer *old_tracer;
2805 ssize_t sret;
2807 /* return any leftover data */
2808 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2809 if (sret != -EBUSY)
2810 return sret;
2812 trace_seq_init(&iter->seq);
2814 /* copy the tracer to avoid using a global lock all around */
2815 mutex_lock(&trace_types_lock);
2816 if (unlikely(old_tracer != current_trace && current_trace)) {
2817 old_tracer = current_trace;
2818 *iter->trace = *current_trace;
2820 mutex_unlock(&trace_types_lock);
2823 * Avoid more than one consumer on a single file descriptor
2824 * This is just a matter of traces coherency, the ring buffer itself
2825 * is protected.
2827 mutex_lock(&iter->mutex);
2828 if (iter->trace->read) {
2829 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2830 if (sret)
2831 goto out;
2834 waitagain:
2835 sret = tracing_wait_pipe(filp);
2836 if (sret <= 0)
2837 goto out;
2839 /* stop when tracing is finished */
2840 if (trace_empty(iter)) {
2841 sret = 0;
2842 goto out;
2845 if (cnt >= PAGE_SIZE)
2846 cnt = PAGE_SIZE - 1;
2848 /* reset all but tr, trace, and overruns */
2849 memset(&iter->seq, 0,
2850 sizeof(struct trace_iterator) -
2851 offsetof(struct trace_iterator, seq));
2852 iter->pos = -1;
2854 while (find_next_entry_inc(iter) != NULL) {
2855 enum print_line_t ret;
2856 int len = iter->seq.len;
2858 ret = print_trace_line(iter);
2859 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2860 /* don't print partial lines */
2861 iter->seq.len = len;
2862 break;
2864 if (ret != TRACE_TYPE_NO_CONSUME)
2865 trace_consume(iter);
2867 if (iter->seq.len >= cnt)
2868 break;
2871 /* Now copy what we have to the user */
2872 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2873 if (iter->seq.readpos >= iter->seq.len)
2874 trace_seq_init(&iter->seq);
2877 * If there was nothing to send to user, inspite of consuming trace
2878 * entries, go back to wait for more entries.
2880 if (sret == -EBUSY)
2881 goto waitagain;
2883 out:
2884 mutex_unlock(&iter->mutex);
2886 return sret;
2889 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2890 struct pipe_buffer *buf)
2892 __free_page(buf->page);
2895 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2896 unsigned int idx)
2898 __free_page(spd->pages[idx]);
2901 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2902 .can_merge = 0,
2903 .map = generic_pipe_buf_map,
2904 .unmap = generic_pipe_buf_unmap,
2905 .confirm = generic_pipe_buf_confirm,
2906 .release = tracing_pipe_buf_release,
2907 .steal = generic_pipe_buf_steal,
2908 .get = generic_pipe_buf_get,
2911 static size_t
2912 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2914 size_t count;
2915 int ret;
2917 /* Seq buffer is page-sized, exactly what we need. */
2918 for (;;) {
2919 count = iter->seq.len;
2920 ret = print_trace_line(iter);
2921 count = iter->seq.len - count;
2922 if (rem < count) {
2923 rem = 0;
2924 iter->seq.len -= count;
2925 break;
2927 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2928 iter->seq.len -= count;
2929 break;
2932 trace_consume(iter);
2933 rem -= count;
2934 if (!find_next_entry_inc(iter)) {
2935 rem = 0;
2936 iter->ent = NULL;
2937 break;
2941 return rem;
2944 static ssize_t tracing_splice_read_pipe(struct file *filp,
2945 loff_t *ppos,
2946 struct pipe_inode_info *pipe,
2947 size_t len,
2948 unsigned int flags)
2950 struct page *pages[PIPE_BUFFERS];
2951 struct partial_page partial[PIPE_BUFFERS];
2952 struct trace_iterator *iter = filp->private_data;
2953 struct splice_pipe_desc spd = {
2954 .pages = pages,
2955 .partial = partial,
2956 .nr_pages = 0, /* This gets updated below. */
2957 .flags = flags,
2958 .ops = &tracing_pipe_buf_ops,
2959 .spd_release = tracing_spd_release_pipe,
2961 static struct tracer *old_tracer;
2962 ssize_t ret;
2963 size_t rem;
2964 unsigned int i;
2966 /* copy the tracer to avoid using a global lock all around */
2967 mutex_lock(&trace_types_lock);
2968 if (unlikely(old_tracer != current_trace && current_trace)) {
2969 old_tracer = current_trace;
2970 *iter->trace = *current_trace;
2972 mutex_unlock(&trace_types_lock);
2974 mutex_lock(&iter->mutex);
2976 if (iter->trace->splice_read) {
2977 ret = iter->trace->splice_read(iter, filp,
2978 ppos, pipe, len, flags);
2979 if (ret)
2980 goto out_err;
2983 ret = tracing_wait_pipe(filp);
2984 if (ret <= 0)
2985 goto out_err;
2987 if (!iter->ent && !find_next_entry_inc(iter)) {
2988 ret = -EFAULT;
2989 goto out_err;
2992 /* Fill as many pages as possible. */
2993 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2994 pages[i] = alloc_page(GFP_KERNEL);
2995 if (!pages[i])
2996 break;
2998 rem = tracing_fill_pipe_page(rem, iter);
3000 /* Copy the data into the page, so we can start over. */
3001 ret = trace_seq_to_buffer(&iter->seq,
3002 page_address(pages[i]),
3003 iter->seq.len);
3004 if (ret < 0) {
3005 __free_page(pages[i]);
3006 break;
3008 partial[i].offset = 0;
3009 partial[i].len = iter->seq.len;
3011 trace_seq_init(&iter->seq);
3014 mutex_unlock(&iter->mutex);
3016 spd.nr_pages = i;
3018 return splice_to_pipe(pipe, &spd);
3020 out_err:
3021 mutex_unlock(&iter->mutex);
3023 return ret;
3026 static ssize_t
3027 tracing_entries_read(struct file *filp, char __user *ubuf,
3028 size_t cnt, loff_t *ppos)
3030 struct trace_array *tr = filp->private_data;
3031 char buf[96];
3032 int r;
3034 mutex_lock(&trace_types_lock);
3035 if (!ring_buffer_expanded)
3036 r = sprintf(buf, "%lu (expanded: %lu)\n",
3037 tr->entries >> 10,
3038 trace_buf_size >> 10);
3039 else
3040 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3041 mutex_unlock(&trace_types_lock);
3043 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3046 static ssize_t
3047 tracing_entries_write(struct file *filp, const char __user *ubuf,
3048 size_t cnt, loff_t *ppos)
3050 unsigned long val;
3051 char buf[64];
3052 int ret, cpu;
3054 if (cnt >= sizeof(buf))
3055 return -EINVAL;
3057 if (copy_from_user(&buf, ubuf, cnt))
3058 return -EFAULT;
3060 buf[cnt] = 0;
3062 ret = strict_strtoul(buf, 10, &val);
3063 if (ret < 0)
3064 return ret;
3066 /* must have at least 1 entry */
3067 if (!val)
3068 return -EINVAL;
3070 mutex_lock(&trace_types_lock);
3072 tracing_stop();
3074 /* disable all cpu buffers */
3075 for_each_tracing_cpu(cpu) {
3076 if (global_trace.data[cpu])
3077 atomic_inc(&global_trace.data[cpu]->disabled);
3078 if (max_tr.data[cpu])
3079 atomic_inc(&max_tr.data[cpu]->disabled);
3082 /* value is in KB */
3083 val <<= 10;
3085 if (val != global_trace.entries) {
3086 ret = tracing_resize_ring_buffer(val);
3087 if (ret < 0) {
3088 cnt = ret;
3089 goto out;
3093 filp->f_pos += cnt;
3095 /* If check pages failed, return ENOMEM */
3096 if (tracing_disabled)
3097 cnt = -ENOMEM;
3098 out:
3099 for_each_tracing_cpu(cpu) {
3100 if (global_trace.data[cpu])
3101 atomic_dec(&global_trace.data[cpu]->disabled);
3102 if (max_tr.data[cpu])
3103 atomic_dec(&max_tr.data[cpu]->disabled);
3106 tracing_start();
3107 max_tr.entries = global_trace.entries;
3108 mutex_unlock(&trace_types_lock);
3110 return cnt;
3113 static int mark_printk(const char *fmt, ...)
3115 int ret;
3116 va_list args;
3117 va_start(args, fmt);
3118 ret = trace_vprintk(0, -1, fmt, args);
3119 va_end(args);
3120 return ret;
3123 static ssize_t
3124 tracing_mark_write(struct file *filp, const char __user *ubuf,
3125 size_t cnt, loff_t *fpos)
3127 char *buf;
3128 char *end;
3130 if (tracing_disabled)
3131 return -EINVAL;
3133 if (cnt > TRACE_BUF_SIZE)
3134 cnt = TRACE_BUF_SIZE;
3136 buf = kmalloc(cnt + 1, GFP_KERNEL);
3137 if (buf == NULL)
3138 return -ENOMEM;
3140 if (copy_from_user(buf, ubuf, cnt)) {
3141 kfree(buf);
3142 return -EFAULT;
3145 /* Cut from the first nil or newline. */
3146 buf[cnt] = '\0';
3147 end = strchr(buf, '\n');
3148 if (end)
3149 *end = '\0';
3151 cnt = mark_printk("%s\n", buf);
3152 kfree(buf);
3153 *fpos += cnt;
3155 return cnt;
3158 static const struct file_operations tracing_max_lat_fops = {
3159 .open = tracing_open_generic,
3160 .read = tracing_max_lat_read,
3161 .write = tracing_max_lat_write,
3164 static const struct file_operations tracing_ctrl_fops = {
3165 .open = tracing_open_generic,
3166 .read = tracing_ctrl_read,
3167 .write = tracing_ctrl_write,
3170 static const struct file_operations set_tracer_fops = {
3171 .open = tracing_open_generic,
3172 .read = tracing_set_trace_read,
3173 .write = tracing_set_trace_write,
3176 static const struct file_operations tracing_pipe_fops = {
3177 .open = tracing_open_pipe,
3178 .poll = tracing_poll_pipe,
3179 .read = tracing_read_pipe,
3180 .splice_read = tracing_splice_read_pipe,
3181 .release = tracing_release_pipe,
3184 static const struct file_operations tracing_entries_fops = {
3185 .open = tracing_open_generic,
3186 .read = tracing_entries_read,
3187 .write = tracing_entries_write,
3190 static const struct file_operations tracing_mark_fops = {
3191 .open = tracing_open_generic,
3192 .write = tracing_mark_write,
3195 struct ftrace_buffer_info {
3196 struct trace_array *tr;
3197 void *spare;
3198 int cpu;
3199 unsigned int read;
3202 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3204 int cpu = (int)(long)inode->i_private;
3205 struct ftrace_buffer_info *info;
3207 if (tracing_disabled)
3208 return -ENODEV;
3210 info = kzalloc(sizeof(*info), GFP_KERNEL);
3211 if (!info)
3212 return -ENOMEM;
3214 info->tr = &global_trace;
3215 info->cpu = cpu;
3216 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3217 /* Force reading ring buffer for first read */
3218 info->read = (unsigned int)-1;
3219 if (!info->spare)
3220 goto out;
3222 filp->private_data = info;
3224 return 0;
3226 out:
3227 kfree(info);
3228 return -ENOMEM;
3231 static ssize_t
3232 tracing_buffers_read(struct file *filp, char __user *ubuf,
3233 size_t count, loff_t *ppos)
3235 struct ftrace_buffer_info *info = filp->private_data;
3236 unsigned int pos;
3237 ssize_t ret;
3238 size_t size;
3240 if (!count)
3241 return 0;
3243 /* Do we have previous read data to read? */
3244 if (info->read < PAGE_SIZE)
3245 goto read;
3247 info->read = 0;
3249 ret = ring_buffer_read_page(info->tr->buffer,
3250 &info->spare,
3251 count,
3252 info->cpu, 0);
3253 if (ret < 0)
3254 return 0;
3256 pos = ring_buffer_page_len(info->spare);
3258 if (pos < PAGE_SIZE)
3259 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3261 read:
3262 size = PAGE_SIZE - info->read;
3263 if (size > count)
3264 size = count;
3266 ret = copy_to_user(ubuf, info->spare + info->read, size);
3267 if (ret == size)
3268 return -EFAULT;
3269 size -= ret;
3271 *ppos += size;
3272 info->read += size;
3274 return size;
3277 static int tracing_buffers_release(struct inode *inode, struct file *file)
3279 struct ftrace_buffer_info *info = file->private_data;
3281 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3282 kfree(info);
3284 return 0;
3287 struct buffer_ref {
3288 struct ring_buffer *buffer;
3289 void *page;
3290 int ref;
3293 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3294 struct pipe_buffer *buf)
3296 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3298 if (--ref->ref)
3299 return;
3301 ring_buffer_free_read_page(ref->buffer, ref->page);
3302 kfree(ref);
3303 buf->private = 0;
3306 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3307 struct pipe_buffer *buf)
3309 return 1;
3312 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3313 struct pipe_buffer *buf)
3315 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3317 ref->ref++;
3320 /* Pipe buffer operations for a buffer. */
3321 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3322 .can_merge = 0,
3323 .map = generic_pipe_buf_map,
3324 .unmap = generic_pipe_buf_unmap,
3325 .confirm = generic_pipe_buf_confirm,
3326 .release = buffer_pipe_buf_release,
3327 .steal = buffer_pipe_buf_steal,
3328 .get = buffer_pipe_buf_get,
3332 * Callback from splice_to_pipe(), if we need to release some pages
3333 * at the end of the spd in case we error'ed out in filling the pipe.
3335 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3337 struct buffer_ref *ref =
3338 (struct buffer_ref *)spd->partial[i].private;
3340 if (--ref->ref)
3341 return;
3343 ring_buffer_free_read_page(ref->buffer, ref->page);
3344 kfree(ref);
3345 spd->partial[i].private = 0;
3348 static ssize_t
3349 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3350 struct pipe_inode_info *pipe, size_t len,
3351 unsigned int flags)
3353 struct ftrace_buffer_info *info = file->private_data;
3354 struct partial_page partial[PIPE_BUFFERS];
3355 struct page *pages[PIPE_BUFFERS];
3356 struct splice_pipe_desc spd = {
3357 .pages = pages,
3358 .partial = partial,
3359 .flags = flags,
3360 .ops = &buffer_pipe_buf_ops,
3361 .spd_release = buffer_spd_release,
3363 struct buffer_ref *ref;
3364 int size, i;
3365 size_t ret;
3368 * We can't seek on a buffer input
3370 if (unlikely(*ppos))
3371 return -ESPIPE;
3374 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3375 struct page *page;
3376 int r;
3378 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3379 if (!ref)
3380 break;
3382 ref->buffer = info->tr->buffer;
3383 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3384 if (!ref->page) {
3385 kfree(ref);
3386 break;
3389 r = ring_buffer_read_page(ref->buffer, &ref->page,
3390 len, info->cpu, 0);
3391 if (r < 0) {
3392 ring_buffer_free_read_page(ref->buffer,
3393 ref->page);
3394 kfree(ref);
3395 break;
3399 * zero out any left over data, this is going to
3400 * user land.
3402 size = ring_buffer_page_len(ref->page);
3403 if (size < PAGE_SIZE)
3404 memset(ref->page + size, 0, PAGE_SIZE - size);
3406 page = virt_to_page(ref->page);
3408 spd.pages[i] = page;
3409 spd.partial[i].len = PAGE_SIZE;
3410 spd.partial[i].offset = 0;
3411 spd.partial[i].private = (unsigned long)ref;
3412 spd.nr_pages++;
3415 spd.nr_pages = i;
3417 /* did we read anything? */
3418 if (!spd.nr_pages) {
3419 if (flags & SPLICE_F_NONBLOCK)
3420 ret = -EAGAIN;
3421 else
3422 ret = 0;
3423 /* TODO: block */
3424 return ret;
3427 ret = splice_to_pipe(pipe, &spd);
3429 return ret;
3432 static const struct file_operations tracing_buffers_fops = {
3433 .open = tracing_buffers_open,
3434 .read = tracing_buffers_read,
3435 .release = tracing_buffers_release,
3436 .splice_read = tracing_buffers_splice_read,
3437 .llseek = no_llseek,
3440 #ifdef CONFIG_DYNAMIC_FTRACE
3442 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3444 return 0;
3447 static ssize_t
3448 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3449 size_t cnt, loff_t *ppos)
3451 static char ftrace_dyn_info_buffer[1024];
3452 static DEFINE_MUTEX(dyn_info_mutex);
3453 unsigned long *p = filp->private_data;
3454 char *buf = ftrace_dyn_info_buffer;
3455 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3456 int r;
3458 mutex_lock(&dyn_info_mutex);
3459 r = sprintf(buf, "%ld ", *p);
3461 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3462 buf[r++] = '\n';
3464 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3466 mutex_unlock(&dyn_info_mutex);
3468 return r;
3471 static const struct file_operations tracing_dyn_info_fops = {
3472 .open = tracing_open_generic,
3473 .read = tracing_read_dyn_info,
3475 #endif
3477 static struct dentry *d_tracer;
3479 struct dentry *tracing_init_dentry(void)
3481 static int once;
3483 if (d_tracer)
3484 return d_tracer;
3486 d_tracer = debugfs_create_dir("tracing", NULL);
3488 if (!d_tracer && !once) {
3489 once = 1;
3490 pr_warning("Could not create debugfs directory 'tracing'\n");
3491 return NULL;
3494 return d_tracer;
3497 static struct dentry *d_percpu;
3499 struct dentry *tracing_dentry_percpu(void)
3501 static int once;
3502 struct dentry *d_tracer;
3504 if (d_percpu)
3505 return d_percpu;
3507 d_tracer = tracing_init_dentry();
3509 if (!d_tracer)
3510 return NULL;
3512 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3514 if (!d_percpu && !once) {
3515 once = 1;
3516 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3517 return NULL;
3520 return d_percpu;
3523 static void tracing_init_debugfs_percpu(long cpu)
3525 struct dentry *d_percpu = tracing_dentry_percpu();
3526 struct dentry *entry, *d_cpu;
3527 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3528 char cpu_dir[7];
3530 if (cpu > 999 || cpu < 0)
3531 return;
3533 sprintf(cpu_dir, "cpu%ld", cpu);
3534 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3535 if (!d_cpu) {
3536 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3537 return;
3540 /* per cpu trace_pipe */
3541 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
3542 (void *) cpu, &tracing_pipe_fops);
3543 if (!entry)
3544 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3546 /* per cpu trace */
3547 entry = debugfs_create_file("trace", 0444, d_cpu,
3548 (void *) cpu, &tracing_fops);
3549 if (!entry)
3550 pr_warning("Could not create debugfs 'trace' entry\n");
3552 entry = debugfs_create_file("trace_pipe_raw", 0444, d_cpu,
3553 (void *) cpu, &tracing_buffers_fops);
3554 if (!entry)
3555 pr_warning("Could not create debugfs 'trace_pipe_raw' entry\n");
3558 #ifdef CONFIG_FTRACE_SELFTEST
3559 /* Let selftest have access to static functions in this file */
3560 #include "trace_selftest.c"
3561 #endif
3563 struct trace_option_dentry {
3564 struct tracer_opt *opt;
3565 struct tracer_flags *flags;
3566 struct dentry *entry;
3569 static ssize_t
3570 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3571 loff_t *ppos)
3573 struct trace_option_dentry *topt = filp->private_data;
3574 char *buf;
3576 if (topt->flags->val & topt->opt->bit)
3577 buf = "1\n";
3578 else
3579 buf = "0\n";
3581 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3584 static ssize_t
3585 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3586 loff_t *ppos)
3588 struct trace_option_dentry *topt = filp->private_data;
3589 unsigned long val;
3590 char buf[64];
3591 int ret;
3593 if (cnt >= sizeof(buf))
3594 return -EINVAL;
3596 if (copy_from_user(&buf, ubuf, cnt))
3597 return -EFAULT;
3599 buf[cnt] = 0;
3601 ret = strict_strtoul(buf, 10, &val);
3602 if (ret < 0)
3603 return ret;
3605 ret = 0;
3606 switch (val) {
3607 case 0:
3608 /* do nothing if already cleared */
3609 if (!(topt->flags->val & topt->opt->bit))
3610 break;
3612 mutex_lock(&trace_types_lock);
3613 if (current_trace->set_flag)
3614 ret = current_trace->set_flag(topt->flags->val,
3615 topt->opt->bit, 0);
3616 mutex_unlock(&trace_types_lock);
3617 if (ret)
3618 return ret;
3619 topt->flags->val &= ~topt->opt->bit;
3620 break;
3621 case 1:
3622 /* do nothing if already set */
3623 if (topt->flags->val & topt->opt->bit)
3624 break;
3626 mutex_lock(&trace_types_lock);
3627 if (current_trace->set_flag)
3628 ret = current_trace->set_flag(topt->flags->val,
3629 topt->opt->bit, 1);
3630 mutex_unlock(&trace_types_lock);
3631 if (ret)
3632 return ret;
3633 topt->flags->val |= topt->opt->bit;
3634 break;
3636 default:
3637 return -EINVAL;
3640 *ppos += cnt;
3642 return cnt;
3646 static const struct file_operations trace_options_fops = {
3647 .open = tracing_open_generic,
3648 .read = trace_options_read,
3649 .write = trace_options_write,
3652 static ssize_t
3653 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3654 loff_t *ppos)
3656 long index = (long)filp->private_data;
3657 char *buf;
3659 if (trace_flags & (1 << index))
3660 buf = "1\n";
3661 else
3662 buf = "0\n";
3664 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3667 static ssize_t
3668 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3669 loff_t *ppos)
3671 long index = (long)filp->private_data;
3672 char buf[64];
3673 unsigned long val;
3674 int ret;
3676 if (cnt >= sizeof(buf))
3677 return -EINVAL;
3679 if (copy_from_user(&buf, ubuf, cnt))
3680 return -EFAULT;
3682 buf[cnt] = 0;
3684 ret = strict_strtoul(buf, 10, &val);
3685 if (ret < 0)
3686 return ret;
3688 switch (val) {
3689 case 0:
3690 trace_flags &= ~(1 << index);
3691 break;
3692 case 1:
3693 trace_flags |= 1 << index;
3694 break;
3696 default:
3697 return -EINVAL;
3700 *ppos += cnt;
3702 return cnt;
3705 static const struct file_operations trace_options_core_fops = {
3706 .open = tracing_open_generic,
3707 .read = trace_options_core_read,
3708 .write = trace_options_core_write,
3711 static struct dentry *trace_options_init_dentry(void)
3713 struct dentry *d_tracer;
3714 static struct dentry *t_options;
3716 if (t_options)
3717 return t_options;
3719 d_tracer = tracing_init_dentry();
3720 if (!d_tracer)
3721 return NULL;
3723 t_options = debugfs_create_dir("options", d_tracer);
3724 if (!t_options) {
3725 pr_warning("Could not create debugfs directory 'options'\n");
3726 return NULL;
3729 return t_options;
3732 static void
3733 create_trace_option_file(struct trace_option_dentry *topt,
3734 struct tracer_flags *flags,
3735 struct tracer_opt *opt)
3737 struct dentry *t_options;
3738 struct dentry *entry;
3740 t_options = trace_options_init_dentry();
3741 if (!t_options)
3742 return;
3744 topt->flags = flags;
3745 topt->opt = opt;
3747 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3748 &trace_options_fops);
3750 topt->entry = entry;
3754 static struct trace_option_dentry *
3755 create_trace_option_files(struct tracer *tracer)
3757 struct trace_option_dentry *topts;
3758 struct tracer_flags *flags;
3759 struct tracer_opt *opts;
3760 int cnt;
3762 if (!tracer)
3763 return NULL;
3765 flags = tracer->flags;
3767 if (!flags || !flags->opts)
3768 return NULL;
3770 opts = flags->opts;
3772 for (cnt = 0; opts[cnt].name; cnt++)
3775 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3776 if (!topts)
3777 return NULL;
3779 for (cnt = 0; opts[cnt].name; cnt++)
3780 create_trace_option_file(&topts[cnt], flags,
3781 &opts[cnt]);
3783 return topts;
3786 static void
3787 destroy_trace_option_files(struct trace_option_dentry *topts)
3789 int cnt;
3791 if (!topts)
3792 return;
3794 for (cnt = 0; topts[cnt].opt; cnt++) {
3795 if (topts[cnt].entry)
3796 debugfs_remove(topts[cnt].entry);
3799 kfree(topts);
3802 static struct dentry *
3803 create_trace_option_core_file(const char *option, long index)
3805 struct dentry *t_options;
3806 struct dentry *entry;
3808 t_options = trace_options_init_dentry();
3809 if (!t_options)
3810 return NULL;
3812 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3813 &trace_options_core_fops);
3815 return entry;
3818 static __init void create_trace_options_dir(void)
3820 struct dentry *t_options;
3821 struct dentry *entry;
3822 int i;
3824 t_options = trace_options_init_dentry();
3825 if (!t_options)
3826 return;
3828 for (i = 0; trace_options[i]; i++) {
3829 entry = create_trace_option_core_file(trace_options[i], i);
3830 if (!entry)
3831 pr_warning("Could not create debugfs %s entry\n",
3832 trace_options[i]);
3836 static __init int tracer_init_debugfs(void)
3838 struct dentry *d_tracer;
3839 struct dentry *entry;
3840 int cpu;
3842 d_tracer = tracing_init_dentry();
3844 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3845 &global_trace, &tracing_ctrl_fops);
3846 if (!entry)
3847 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3849 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3850 NULL, &tracing_iter_fops);
3851 if (!entry)
3852 pr_warning("Could not create debugfs 'trace_options' entry\n");
3854 create_trace_options_dir();
3856 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3857 NULL, &tracing_cpumask_fops);
3858 if (!entry)
3859 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3861 entry = debugfs_create_file("trace", 0444, d_tracer,
3862 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3863 if (!entry)
3864 pr_warning("Could not create debugfs 'trace' entry\n");
3866 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3867 &global_trace, &show_traces_fops);
3868 if (!entry)
3869 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3871 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3872 &global_trace, &set_tracer_fops);
3873 if (!entry)
3874 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3876 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3877 &tracing_max_latency,
3878 &tracing_max_lat_fops);
3879 if (!entry)
3880 pr_warning("Could not create debugfs "
3881 "'tracing_max_latency' entry\n");
3883 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3884 &tracing_thresh, &tracing_max_lat_fops);
3885 if (!entry)
3886 pr_warning("Could not create debugfs "
3887 "'tracing_thresh' entry\n");
3888 entry = debugfs_create_file("README", 0644, d_tracer,
3889 NULL, &tracing_readme_fops);
3890 if (!entry)
3891 pr_warning("Could not create debugfs 'README' entry\n");
3893 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3894 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3895 if (!entry)
3896 pr_warning("Could not create debugfs "
3897 "'trace_pipe' entry\n");
3899 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3900 &global_trace, &tracing_entries_fops);
3901 if (!entry)
3902 pr_warning("Could not create debugfs "
3903 "'buffer_size_kb' entry\n");
3905 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3906 NULL, &tracing_mark_fops);
3907 if (!entry)
3908 pr_warning("Could not create debugfs "
3909 "'trace_marker' entry\n");
3911 #ifdef CONFIG_DYNAMIC_FTRACE
3912 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3913 &ftrace_update_tot_cnt,
3914 &tracing_dyn_info_fops);
3915 if (!entry)
3916 pr_warning("Could not create debugfs "
3917 "'dyn_ftrace_total_info' entry\n");
3918 #endif
3919 #ifdef CONFIG_SYSPROF_TRACER
3920 init_tracer_sysprof_debugfs(d_tracer);
3921 #endif
3923 for_each_tracing_cpu(cpu)
3924 tracing_init_debugfs_percpu(cpu);
3926 return 0;
3929 static int trace_panic_handler(struct notifier_block *this,
3930 unsigned long event, void *unused)
3932 if (ftrace_dump_on_oops)
3933 ftrace_dump();
3934 return NOTIFY_OK;
3937 static struct notifier_block trace_panic_notifier = {
3938 .notifier_call = trace_panic_handler,
3939 .next = NULL,
3940 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3943 static int trace_die_handler(struct notifier_block *self,
3944 unsigned long val,
3945 void *data)
3947 switch (val) {
3948 case DIE_OOPS:
3949 if (ftrace_dump_on_oops)
3950 ftrace_dump();
3951 break;
3952 default:
3953 break;
3955 return NOTIFY_OK;
3958 static struct notifier_block trace_die_notifier = {
3959 .notifier_call = trace_die_handler,
3960 .priority = 200
3964 * printk is set to max of 1024, we really don't need it that big.
3965 * Nothing should be printing 1000 characters anyway.
3967 #define TRACE_MAX_PRINT 1000
3970 * Define here KERN_TRACE so that we have one place to modify
3971 * it if we decide to change what log level the ftrace dump
3972 * should be at.
3974 #define KERN_TRACE KERN_EMERG
3976 static void
3977 trace_printk_seq(struct trace_seq *s)
3979 /* Probably should print a warning here. */
3980 if (s->len >= 1000)
3981 s->len = 1000;
3983 /* should be zero ended, but we are paranoid. */
3984 s->buffer[s->len] = 0;
3986 printk(KERN_TRACE "%s", s->buffer);
3988 trace_seq_init(s);
3991 void ftrace_dump(void)
3993 static DEFINE_SPINLOCK(ftrace_dump_lock);
3994 /* use static because iter can be a bit big for the stack */
3995 static struct trace_iterator iter;
3996 static int dump_ran;
3997 unsigned long flags;
3998 int cnt = 0, cpu;
4000 /* only one dump */
4001 spin_lock_irqsave(&ftrace_dump_lock, flags);
4002 if (dump_ran)
4003 goto out;
4005 dump_ran = 1;
4007 /* No turning back! */
4008 tracing_off();
4009 ftrace_kill();
4011 for_each_tracing_cpu(cpu) {
4012 atomic_inc(&global_trace.data[cpu]->disabled);
4015 /* don't look at user memory in panic mode */
4016 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4018 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4020 /* Simulate the iterator */
4021 iter.tr = &global_trace;
4022 iter.trace = current_trace;
4023 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4026 * We need to stop all tracing on all CPUS to read the
4027 * the next buffer. This is a bit expensive, but is
4028 * not done often. We fill all what we can read,
4029 * and then release the locks again.
4032 while (!trace_empty(&iter)) {
4034 if (!cnt)
4035 printk(KERN_TRACE "---------------------------------\n");
4037 cnt++;
4039 /* reset all but tr, trace, and overruns */
4040 memset(&iter.seq, 0,
4041 sizeof(struct trace_iterator) -
4042 offsetof(struct trace_iterator, seq));
4043 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4044 iter.pos = -1;
4046 if (find_next_entry_inc(&iter) != NULL) {
4047 print_trace_line(&iter);
4048 trace_consume(&iter);
4051 trace_printk_seq(&iter.seq);
4054 if (!cnt)
4055 printk(KERN_TRACE " (ftrace buffer empty)\n");
4056 else
4057 printk(KERN_TRACE "---------------------------------\n");
4059 out:
4060 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
4063 __init static int tracer_alloc_buffers(void)
4065 struct trace_array_cpu *data;
4066 int ring_buf_size;
4067 int i;
4068 int ret = -ENOMEM;
4070 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4071 goto out;
4073 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4074 goto out_free_buffer_mask;
4076 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4077 goto out_free_tracing_cpumask;
4079 /* To save memory, keep the ring buffer size to its minimum */
4080 if (ring_buffer_expanded)
4081 ring_buf_size = trace_buf_size;
4082 else
4083 ring_buf_size = 1;
4085 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4086 cpumask_copy(tracing_cpumask, cpu_all_mask);
4087 cpumask_clear(tracing_reader_cpumask);
4089 /* TODO: make the number of buffers hot pluggable with CPUS */
4090 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4091 TRACE_BUFFER_FLAGS);
4092 if (!global_trace.buffer) {
4093 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4094 WARN_ON(1);
4095 goto out_free_cpumask;
4097 global_trace.entries = ring_buffer_size(global_trace.buffer);
4100 #ifdef CONFIG_TRACER_MAX_TRACE
4101 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4102 TRACE_BUFFER_FLAGS);
4103 if (!max_tr.buffer) {
4104 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4105 WARN_ON(1);
4106 ring_buffer_free(global_trace.buffer);
4107 goto out_free_cpumask;
4109 max_tr.entries = ring_buffer_size(max_tr.buffer);
4110 WARN_ON(max_tr.entries != global_trace.entries);
4111 #endif
4113 /* Allocate the first page for all buffers */
4114 for_each_tracing_cpu(i) {
4115 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4116 max_tr.data[i] = &per_cpu(max_data, i);
4119 trace_init_cmdlines();
4121 register_tracer(&nop_trace);
4122 current_trace = &nop_trace;
4123 #ifdef CONFIG_BOOT_TRACER
4124 register_tracer(&boot_tracer);
4125 #endif
4126 /* All seems OK, enable tracing */
4127 tracing_disabled = 0;
4129 atomic_notifier_chain_register(&panic_notifier_list,
4130 &trace_panic_notifier);
4132 register_die_notifier(&trace_die_notifier);
4134 return 0;
4136 out_free_cpumask:
4137 free_cpumask_var(tracing_reader_cpumask);
4138 out_free_tracing_cpumask:
4139 free_cpumask_var(tracing_cpumask);
4140 out_free_buffer_mask:
4141 free_cpumask_var(tracing_buffer_mask);
4142 out:
4143 return ret;
4146 __init static int clear_boot_tracer(void)
4149 * The default tracer at boot buffer is an init section.
4150 * This function is called in lateinit. If we did not
4151 * find the boot tracer, then clear it out, to prevent
4152 * later registration from accessing the buffer that is
4153 * about to be freed.
4155 if (!default_bootup_tracer)
4156 return 0;
4158 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4159 default_bootup_tracer);
4160 default_bootup_tracer = NULL;
4162 return 0;
4165 early_initcall(tracer_alloc_buffers);
4166 fs_initcall(tracer_init_debugfs);
4167 late_initcall(clear_boot_tracer);