Merge branch 'tip/tracing/ftrace' of git://git.kernel.org/pub/scm/linux/kernel/git...
[wandboard.git] / kernel / trace / trace.c
blobe3dfefe69348cbeb6e24b69bb74ecde40e7bc7cc
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;
159 * The global_trace is the descriptor that holds the tracing
160 * buffers for the live tracing. For each CPU, it contains
161 * a link list of pages that will store trace entries. The
162 * page descriptor of the pages in the memory is used to hold
163 * the link list by linking the lru item in the page descriptor
164 * to each of the pages in the buffer per CPU.
166 * For each active CPU there is a data field that holds the
167 * pages for the buffer for that CPU. Each CPU has the same number
168 * of pages allocated for its buffer.
170 static struct trace_array global_trace;
172 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
174 cycle_t ftrace_now(int cpu)
176 u64 ts;
178 /* Early boot up does not have a buffer yet */
179 if (!global_trace.buffer)
180 return trace_clock_local();
182 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
183 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
185 return ts;
189 * The max_tr is used to snapshot the global_trace when a maximum
190 * latency is reached. Some tracers will use this to store a maximum
191 * trace while it continues examining live traces.
193 * The buffers for the max_tr are set up the same as the global_trace.
194 * When a snapshot is taken, the link list of the max_tr is swapped
195 * with the link list of the global_trace and the buffers are reset for
196 * the global_trace so the tracing can continue.
198 static struct trace_array max_tr;
200 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
202 /* tracer_enabled is used to toggle activation of a tracer */
203 static int tracer_enabled = 1;
206 * tracing_is_enabled - return tracer_enabled status
208 * This function is used by other tracers to know the status
209 * of the tracer_enabled flag. Tracers may use this function
210 * to know if it should enable their features when starting
211 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
213 int tracing_is_enabled(void)
215 return tracer_enabled;
219 * trace_buf_size is the size in bytes that is allocated
220 * for a buffer. Note, the number of bytes is always rounded
221 * to page size.
223 * This number is purposely set to a low number of 16384.
224 * If the dump on oops happens, it will be much appreciated
225 * to not have to wait for all that output. Anyway this can be
226 * boot time and run time configurable.
228 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
230 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
232 /* trace_types holds a link list of available tracers. */
233 static struct tracer *trace_types __read_mostly;
235 /* current_trace points to the tracer that is currently active */
236 static struct tracer *current_trace __read_mostly;
239 * max_tracer_type_len is used to simplify the allocating of
240 * buffers to read userspace tracer names. We keep track of
241 * the longest tracer name registered.
243 static int max_tracer_type_len;
246 * trace_types_lock is used to protect the trace_types list.
247 * This lock is also used to keep user access serialized.
248 * Accesses from userspace will grab this lock while userspace
249 * activities happen inside the kernel.
251 static DEFINE_MUTEX(trace_types_lock);
253 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
254 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
256 /* trace_flags holds trace_options default values */
257 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
258 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
261 * trace_wake_up - wake up tasks waiting for trace input
263 * Simply wakes up any task that is blocked on the trace_wait
264 * queue. These is used with trace_poll for tasks polling the trace.
266 void trace_wake_up(void)
269 * The runqueue_is_locked() can fail, but this is the best we
270 * have for now:
272 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
273 wake_up(&trace_wait);
276 static int __init set_buf_size(char *str)
278 unsigned long buf_size;
279 int ret;
281 if (!str)
282 return 0;
283 ret = strict_strtoul(str, 0, &buf_size);
284 /* nr_entries can not be zero */
285 if (ret < 0 || buf_size == 0)
286 return 0;
287 trace_buf_size = buf_size;
288 return 1;
290 __setup("trace_buf_size=", set_buf_size);
292 unsigned long nsecs_to_usecs(unsigned long nsecs)
294 return nsecs / 1000;
297 /* These must match the bit postions in trace_iterator_flags */
298 static const char *trace_options[] = {
299 "print-parent",
300 "sym-offset",
301 "sym-addr",
302 "verbose",
303 "raw",
304 "hex",
305 "bin",
306 "block",
307 "stacktrace",
308 "sched-tree",
309 "trace_printk",
310 "ftrace_preempt",
311 "branch",
312 "annotate",
313 "userstacktrace",
314 "sym-userobj",
315 "printk-msg-only",
316 "context-info",
317 "latency-format",
318 "global-clock",
319 NULL
323 * ftrace_max_lock is used to protect the swapping of buffers
324 * when taking a max snapshot. The buffers themselves are
325 * protected by per_cpu spinlocks. But the action of the swap
326 * needs its own lock.
328 * This is defined as a raw_spinlock_t in order to help
329 * with performance when lockdep debugging is enabled.
331 static raw_spinlock_t ftrace_max_lock =
332 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
335 * Copy the new maximum trace into the separate maximum-trace
336 * structure. (this way the maximum trace is permanently saved,
337 * for later retrieval via /debugfs/tracing/latency_trace)
339 static void
340 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
342 struct trace_array_cpu *data = tr->data[cpu];
344 max_tr.cpu = cpu;
345 max_tr.time_start = data->preempt_timestamp;
347 data = max_tr.data[cpu];
348 data->saved_latency = tracing_max_latency;
350 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
351 data->pid = tsk->pid;
352 data->uid = task_uid(tsk);
353 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
354 data->policy = tsk->policy;
355 data->rt_priority = tsk->rt_priority;
357 /* record this tasks comm */
358 tracing_record_cmdline(tsk);
361 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
363 int len;
364 int ret;
366 if (!cnt)
367 return 0;
369 if (s->len <= s->readpos)
370 return -EBUSY;
372 len = s->len - s->readpos;
373 if (cnt > len)
374 cnt = len;
375 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
376 if (ret == cnt)
377 return -EFAULT;
379 cnt -= ret;
381 s->readpos += cnt;
382 return cnt;
385 ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
387 int len;
388 void *ret;
390 if (s->len <= s->readpos)
391 return -EBUSY;
393 len = s->len - s->readpos;
394 if (cnt > len)
395 cnt = len;
396 ret = memcpy(buf, s->buffer + s->readpos, cnt);
397 if (!ret)
398 return -EFAULT;
400 s->readpos += cnt;
401 return cnt;
404 static void
405 trace_print_seq(struct seq_file *m, struct trace_seq *s)
407 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
409 s->buffer[len] = 0;
410 seq_puts(m, s->buffer);
412 trace_seq_init(s);
416 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
417 * @tr: tracer
418 * @tsk: the task with the latency
419 * @cpu: The cpu that initiated the trace.
421 * Flip the buffers between the @tr and the max_tr and record information
422 * about which task was the cause of this latency.
424 void
425 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
427 struct ring_buffer *buf = tr->buffer;
429 WARN_ON_ONCE(!irqs_disabled());
430 __raw_spin_lock(&ftrace_max_lock);
432 tr->buffer = max_tr.buffer;
433 max_tr.buffer = buf;
435 ftrace_disable_cpu();
436 ring_buffer_reset(tr->buffer);
437 ftrace_enable_cpu();
439 __update_max_tr(tr, tsk, cpu);
440 __raw_spin_unlock(&ftrace_max_lock);
444 * update_max_tr_single - only copy one trace over, and reset the rest
445 * @tr - tracer
446 * @tsk - task with the latency
447 * @cpu - the cpu of the buffer to copy.
449 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
451 void
452 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
454 int ret;
456 WARN_ON_ONCE(!irqs_disabled());
457 __raw_spin_lock(&ftrace_max_lock);
459 ftrace_disable_cpu();
461 ring_buffer_reset(max_tr.buffer);
462 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
464 ftrace_enable_cpu();
466 WARN_ON_ONCE(ret && ret != -EAGAIN);
468 __update_max_tr(tr, tsk, cpu);
469 __raw_spin_unlock(&ftrace_max_lock);
473 * register_tracer - register a tracer with the ftrace system.
474 * @type - the plugin for the tracer
476 * Register a new plugin tracer.
478 int register_tracer(struct tracer *type)
479 __releases(kernel_lock)
480 __acquires(kernel_lock)
482 struct tracer *t;
483 int len;
484 int ret = 0;
486 if (!type->name) {
487 pr_info("Tracer must have a name\n");
488 return -1;
492 * When this gets called we hold the BKL which means that
493 * preemption is disabled. Various trace selftests however
494 * need to disable and enable preemption for successful tests.
495 * So we drop the BKL here and grab it after the tests again.
497 unlock_kernel();
498 mutex_lock(&trace_types_lock);
500 tracing_selftest_running = true;
502 for (t = trace_types; t; t = t->next) {
503 if (strcmp(type->name, t->name) == 0) {
504 /* already found */
505 pr_info("Trace %s already registered\n",
506 type->name);
507 ret = -1;
508 goto out;
512 if (!type->set_flag)
513 type->set_flag = &dummy_set_flag;
514 if (!type->flags)
515 type->flags = &dummy_tracer_flags;
516 else
517 if (!type->flags->opts)
518 type->flags->opts = dummy_tracer_opt;
519 if (!type->wait_pipe)
520 type->wait_pipe = default_wait_pipe;
523 #ifdef CONFIG_FTRACE_STARTUP_TEST
524 if (type->selftest && !tracing_selftest_disabled) {
525 struct tracer *saved_tracer = current_trace;
526 struct trace_array *tr = &global_trace;
527 int i;
530 * Run a selftest on this tracer.
531 * Here we reset the trace buffer, and set the current
532 * tracer to be this tracer. The tracer can then run some
533 * internal tracing to verify that everything is in order.
534 * If we fail, we do not register this tracer.
536 for_each_tracing_cpu(i)
537 tracing_reset(tr, i);
539 current_trace = type;
540 /* the test is responsible for initializing and enabling */
541 pr_info("Testing tracer %s: ", type->name);
542 ret = type->selftest(type, tr);
543 /* the test is responsible for resetting too */
544 current_trace = saved_tracer;
545 if (ret) {
546 printk(KERN_CONT "FAILED!\n");
547 goto out;
549 /* Only reset on passing, to avoid touching corrupted buffers */
550 for_each_tracing_cpu(i)
551 tracing_reset(tr, i);
553 printk(KERN_CONT "PASSED\n");
555 #endif
557 type->next = trace_types;
558 trace_types = type;
559 len = strlen(type->name);
560 if (len > max_tracer_type_len)
561 max_tracer_type_len = len;
563 out:
564 tracing_selftest_running = false;
565 mutex_unlock(&trace_types_lock);
567 if (ret || !default_bootup_tracer)
568 goto out_unlock;
570 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
571 goto out_unlock;
573 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
574 /* Do we want this tracer to start on bootup? */
575 tracing_set_tracer(type->name);
576 default_bootup_tracer = NULL;
577 /* disable other selftests, since this will break it. */
578 tracing_selftest_disabled = 1;
579 #ifdef CONFIG_FTRACE_STARTUP_TEST
580 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
581 type->name);
582 #endif
584 out_unlock:
585 lock_kernel();
586 return ret;
589 void unregister_tracer(struct tracer *type)
591 struct tracer **t;
592 int len;
594 mutex_lock(&trace_types_lock);
595 for (t = &trace_types; *t; t = &(*t)->next) {
596 if (*t == type)
597 goto found;
599 pr_info("Trace %s not registered\n", type->name);
600 goto out;
602 found:
603 *t = (*t)->next;
605 if (type == current_trace && tracer_enabled) {
606 tracer_enabled = 0;
607 tracing_stop();
608 if (current_trace->stop)
609 current_trace->stop(&global_trace);
610 current_trace = &nop_trace;
613 if (strlen(type->name) != max_tracer_type_len)
614 goto out;
616 max_tracer_type_len = 0;
617 for (t = &trace_types; *t; t = &(*t)->next) {
618 len = strlen((*t)->name);
619 if (len > max_tracer_type_len)
620 max_tracer_type_len = len;
622 out:
623 mutex_unlock(&trace_types_lock);
626 void tracing_reset(struct trace_array *tr, int cpu)
628 ftrace_disable_cpu();
629 ring_buffer_reset_cpu(tr->buffer, cpu);
630 ftrace_enable_cpu();
633 void tracing_reset_online_cpus(struct trace_array *tr)
635 int cpu;
637 tr->time_start = ftrace_now(tr->cpu);
639 for_each_online_cpu(cpu)
640 tracing_reset(tr, cpu);
643 #define SAVED_CMDLINES 128
644 #define NO_CMDLINE_MAP UINT_MAX
645 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
646 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
647 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
648 static int cmdline_idx;
649 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
651 /* temporary disable recording */
652 static atomic_t trace_record_cmdline_disabled __read_mostly;
654 static void trace_init_cmdlines(void)
656 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
657 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
658 cmdline_idx = 0;
661 static int trace_stop_count;
662 static DEFINE_SPINLOCK(tracing_start_lock);
665 * ftrace_off_permanent - disable all ftrace code permanently
667 * This should only be called when a serious anomally has
668 * been detected. This will turn off the function tracing,
669 * ring buffers, and other tracing utilites. It takes no
670 * locks and can be called from any context.
672 void ftrace_off_permanent(void)
674 tracing_disabled = 1;
675 ftrace_stop();
676 tracing_off_permanent();
680 * tracing_start - quick start of the tracer
682 * If tracing is enabled but was stopped by tracing_stop,
683 * this will start the tracer back up.
685 void tracing_start(void)
687 struct ring_buffer *buffer;
688 unsigned long flags;
690 if (tracing_disabled)
691 return;
693 spin_lock_irqsave(&tracing_start_lock, flags);
694 if (--trace_stop_count) {
695 if (trace_stop_count < 0) {
696 /* Someone screwed up their debugging */
697 WARN_ON_ONCE(1);
698 trace_stop_count = 0;
700 goto out;
704 buffer = global_trace.buffer;
705 if (buffer)
706 ring_buffer_record_enable(buffer);
708 buffer = max_tr.buffer;
709 if (buffer)
710 ring_buffer_record_enable(buffer);
712 ftrace_start();
713 out:
714 spin_unlock_irqrestore(&tracing_start_lock, flags);
718 * tracing_stop - quick stop of the tracer
720 * Light weight way to stop tracing. Use in conjunction with
721 * tracing_start.
723 void tracing_stop(void)
725 struct ring_buffer *buffer;
726 unsigned long flags;
728 ftrace_stop();
729 spin_lock_irqsave(&tracing_start_lock, flags);
730 if (trace_stop_count++)
731 goto out;
733 buffer = global_trace.buffer;
734 if (buffer)
735 ring_buffer_record_disable(buffer);
737 buffer = max_tr.buffer;
738 if (buffer)
739 ring_buffer_record_disable(buffer);
741 out:
742 spin_unlock_irqrestore(&tracing_start_lock, flags);
745 void trace_stop_cmdline_recording(void);
747 static void trace_save_cmdline(struct task_struct *tsk)
749 unsigned pid, idx;
751 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
752 return;
755 * It's not the end of the world if we don't get
756 * the lock, but we also don't want to spin
757 * nor do we want to disable interrupts,
758 * so if we miss here, then better luck next time.
760 if (!__raw_spin_trylock(&trace_cmdline_lock))
761 return;
763 idx = map_pid_to_cmdline[tsk->pid];
764 if (idx == NO_CMDLINE_MAP) {
765 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
768 * Check whether the cmdline buffer at idx has a pid
769 * mapped. We are going to overwrite that entry so we
770 * need to clear the map_pid_to_cmdline. Otherwise we
771 * would read the new comm for the old pid.
773 pid = map_cmdline_to_pid[idx];
774 if (pid != NO_CMDLINE_MAP)
775 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
777 map_cmdline_to_pid[idx] = tsk->pid;
778 map_pid_to_cmdline[tsk->pid] = idx;
780 cmdline_idx = idx;
783 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
785 __raw_spin_unlock(&trace_cmdline_lock);
788 void trace_find_cmdline(int pid, char comm[])
790 unsigned map;
792 if (!pid) {
793 strcpy(comm, "<idle>");
794 return;
797 if (pid > PID_MAX_DEFAULT) {
798 strcpy(comm, "<...>");
799 return;
802 __raw_spin_lock(&trace_cmdline_lock);
803 map = map_pid_to_cmdline[pid];
804 if (map != NO_CMDLINE_MAP)
805 strcpy(comm, saved_cmdlines[map]);
806 else
807 strcpy(comm, "<...>");
809 __raw_spin_unlock(&trace_cmdline_lock);
812 void tracing_record_cmdline(struct task_struct *tsk)
814 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
815 !tracing_is_on())
816 return;
818 trace_save_cmdline(tsk);
821 void
822 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
823 int pc)
825 struct task_struct *tsk = current;
827 entry->preempt_count = pc & 0xff;
828 entry->pid = (tsk) ? tsk->pid : 0;
829 entry->tgid = (tsk) ? tsk->tgid : 0;
830 entry->flags =
831 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
832 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
833 #else
834 TRACE_FLAG_IRQS_NOSUPPORT |
835 #endif
836 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
837 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
838 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
841 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
842 unsigned char type,
843 unsigned long len,
844 unsigned long flags, int pc)
846 struct ring_buffer_event *event;
848 event = ring_buffer_lock_reserve(tr->buffer, len);
849 if (event != NULL) {
850 struct trace_entry *ent = ring_buffer_event_data(event);
852 tracing_generic_entry_update(ent, flags, pc);
853 ent->type = type;
856 return event;
858 static void ftrace_trace_stack(struct trace_array *tr,
859 unsigned long flags, int skip, int pc);
860 static void ftrace_trace_userstack(struct trace_array *tr,
861 unsigned long flags, int pc);
863 void trace_buffer_unlock_commit(struct trace_array *tr,
864 struct ring_buffer_event *event,
865 unsigned long flags, int pc)
867 ring_buffer_unlock_commit(tr->buffer, event);
869 ftrace_trace_stack(tr, flags, 6, pc);
870 ftrace_trace_userstack(tr, flags, pc);
871 trace_wake_up();
874 struct ring_buffer_event *
875 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
876 unsigned long flags, int pc)
878 return trace_buffer_lock_reserve(&global_trace,
879 type, len, flags, pc);
882 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
883 unsigned long flags, int pc)
885 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
888 void
889 trace_function(struct trace_array *tr,
890 unsigned long ip, unsigned long parent_ip, unsigned long flags,
891 int pc)
893 struct ring_buffer_event *event;
894 struct ftrace_entry *entry;
896 /* If we are reading the ring buffer, don't trace */
897 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
898 return;
900 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
901 flags, pc);
902 if (!event)
903 return;
904 entry = ring_buffer_event_data(event);
905 entry->ip = ip;
906 entry->parent_ip = parent_ip;
907 ring_buffer_unlock_commit(tr->buffer, event);
910 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
911 static void __trace_graph_entry(struct trace_array *tr,
912 struct ftrace_graph_ent *trace,
913 unsigned long flags,
914 int pc)
916 struct ring_buffer_event *event;
917 struct ftrace_graph_ent_entry *entry;
919 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
920 return;
922 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
923 sizeof(*entry), flags, pc);
924 if (!event)
925 return;
926 entry = ring_buffer_event_data(event);
927 entry->graph_ent = *trace;
928 ring_buffer_unlock_commit(global_trace.buffer, event);
931 static void __trace_graph_return(struct trace_array *tr,
932 struct ftrace_graph_ret *trace,
933 unsigned long flags,
934 int pc)
936 struct ring_buffer_event *event;
937 struct ftrace_graph_ret_entry *entry;
939 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
940 return;
942 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
943 sizeof(*entry), flags, pc);
944 if (!event)
945 return;
946 entry = ring_buffer_event_data(event);
947 entry->ret = *trace;
948 ring_buffer_unlock_commit(global_trace.buffer, event);
950 #endif
952 void
953 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
954 unsigned long ip, unsigned long parent_ip, unsigned long flags,
955 int pc)
957 if (likely(!atomic_read(&data->disabled)))
958 trace_function(tr, ip, parent_ip, flags, pc);
961 static void __ftrace_trace_stack(struct trace_array *tr,
962 unsigned long flags,
963 int skip, int pc)
965 #ifdef CONFIG_STACKTRACE
966 struct ring_buffer_event *event;
967 struct stack_entry *entry;
968 struct stack_trace trace;
970 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
971 sizeof(*entry), flags, pc);
972 if (!event)
973 return;
974 entry = ring_buffer_event_data(event);
975 memset(&entry->caller, 0, sizeof(entry->caller));
977 trace.nr_entries = 0;
978 trace.max_entries = FTRACE_STACK_ENTRIES;
979 trace.skip = skip;
980 trace.entries = entry->caller;
982 save_stack_trace(&trace);
983 ring_buffer_unlock_commit(tr->buffer, event);
984 #endif
987 static void ftrace_trace_stack(struct trace_array *tr,
988 unsigned long flags,
989 int skip, int pc)
991 if (!(trace_flags & TRACE_ITER_STACKTRACE))
992 return;
994 __ftrace_trace_stack(tr, flags, skip, pc);
997 void __trace_stack(struct trace_array *tr,
998 unsigned long flags,
999 int skip, int pc)
1001 __ftrace_trace_stack(tr, flags, skip, pc);
1004 static void ftrace_trace_userstack(struct trace_array *tr,
1005 unsigned long flags, int pc)
1007 #ifdef CONFIG_STACKTRACE
1008 struct ring_buffer_event *event;
1009 struct userstack_entry *entry;
1010 struct stack_trace trace;
1012 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1013 return;
1015 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1016 sizeof(*entry), flags, pc);
1017 if (!event)
1018 return;
1019 entry = ring_buffer_event_data(event);
1021 memset(&entry->caller, 0, sizeof(entry->caller));
1023 trace.nr_entries = 0;
1024 trace.max_entries = FTRACE_STACK_ENTRIES;
1025 trace.skip = 0;
1026 trace.entries = entry->caller;
1028 save_stack_trace_user(&trace);
1029 ring_buffer_unlock_commit(tr->buffer, event);
1030 #endif
1033 #ifdef UNUSED
1034 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1036 ftrace_trace_userstack(tr, flags, preempt_count());
1038 #endif /* UNUSED */
1040 static void
1041 ftrace_trace_special(void *__tr,
1042 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1043 int pc)
1045 struct ring_buffer_event *event;
1046 struct trace_array *tr = __tr;
1047 struct special_entry *entry;
1049 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1050 sizeof(*entry), 0, pc);
1051 if (!event)
1052 return;
1053 entry = ring_buffer_event_data(event);
1054 entry->arg1 = arg1;
1055 entry->arg2 = arg2;
1056 entry->arg3 = arg3;
1057 trace_buffer_unlock_commit(tr, event, 0, pc);
1060 void
1061 __trace_special(void *__tr, void *__data,
1062 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1064 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1067 void
1068 tracing_sched_switch_trace(struct trace_array *tr,
1069 struct task_struct *prev,
1070 struct task_struct *next,
1071 unsigned long flags, int pc)
1073 struct ring_buffer_event *event;
1074 struct ctx_switch_entry *entry;
1076 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1077 sizeof(*entry), flags, pc);
1078 if (!event)
1079 return;
1080 entry = ring_buffer_event_data(event);
1081 entry->prev_pid = prev->pid;
1082 entry->prev_prio = prev->prio;
1083 entry->prev_state = prev->state;
1084 entry->next_pid = next->pid;
1085 entry->next_prio = next->prio;
1086 entry->next_state = next->state;
1087 entry->next_cpu = task_cpu(next);
1088 trace_buffer_unlock_commit(tr, event, flags, pc);
1091 void
1092 tracing_sched_wakeup_trace(struct trace_array *tr,
1093 struct task_struct *wakee,
1094 struct task_struct *curr,
1095 unsigned long flags, int pc)
1097 struct ring_buffer_event *event;
1098 struct ctx_switch_entry *entry;
1100 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1101 sizeof(*entry), flags, pc);
1102 if (!event)
1103 return;
1104 entry = ring_buffer_event_data(event);
1105 entry->prev_pid = curr->pid;
1106 entry->prev_prio = curr->prio;
1107 entry->prev_state = curr->state;
1108 entry->next_pid = wakee->pid;
1109 entry->next_prio = wakee->prio;
1110 entry->next_state = wakee->state;
1111 entry->next_cpu = task_cpu(wakee);
1113 ring_buffer_unlock_commit(tr->buffer, event);
1114 ftrace_trace_stack(tr, flags, 6, pc);
1115 ftrace_trace_userstack(tr, flags, pc);
1118 void
1119 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1121 struct trace_array *tr = &global_trace;
1122 struct trace_array_cpu *data;
1123 unsigned long flags;
1124 int cpu;
1125 int pc;
1127 if (tracing_disabled)
1128 return;
1130 pc = preempt_count();
1131 local_irq_save(flags);
1132 cpu = raw_smp_processor_id();
1133 data = tr->data[cpu];
1135 if (likely(atomic_inc_return(&data->disabled) == 1))
1136 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1138 atomic_dec(&data->disabled);
1139 local_irq_restore(flags);
1142 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1143 int trace_graph_entry(struct ftrace_graph_ent *trace)
1145 struct trace_array *tr = &global_trace;
1146 struct trace_array_cpu *data;
1147 unsigned long flags;
1148 long disabled;
1149 int cpu;
1150 int pc;
1152 if (!ftrace_trace_task(current))
1153 return 0;
1155 if (!ftrace_graph_addr(trace->func))
1156 return 0;
1158 local_irq_save(flags);
1159 cpu = raw_smp_processor_id();
1160 data = tr->data[cpu];
1161 disabled = atomic_inc_return(&data->disabled);
1162 if (likely(disabled == 1)) {
1163 pc = preempt_count();
1164 __trace_graph_entry(tr, trace, flags, pc);
1166 /* Only do the atomic if it is not already set */
1167 if (!test_tsk_trace_graph(current))
1168 set_tsk_trace_graph(current);
1169 atomic_dec(&data->disabled);
1170 local_irq_restore(flags);
1172 return 1;
1175 void trace_graph_return(struct ftrace_graph_ret *trace)
1177 struct trace_array *tr = &global_trace;
1178 struct trace_array_cpu *data;
1179 unsigned long flags;
1180 long disabled;
1181 int cpu;
1182 int pc;
1184 local_irq_save(flags);
1185 cpu = raw_smp_processor_id();
1186 data = tr->data[cpu];
1187 disabled = atomic_inc_return(&data->disabled);
1188 if (likely(disabled == 1)) {
1189 pc = preempt_count();
1190 __trace_graph_return(tr, trace, flags, pc);
1192 if (!trace->depth)
1193 clear_tsk_trace_graph(current);
1194 atomic_dec(&data->disabled);
1195 local_irq_restore(flags);
1197 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1201 * trace_vbprintk - write binary msg to tracing buffer
1204 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1206 static raw_spinlock_t trace_buf_lock =
1207 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1208 static u32 trace_buf[TRACE_BUF_SIZE];
1210 struct ring_buffer_event *event;
1211 struct trace_array *tr = &global_trace;
1212 struct trace_array_cpu *data;
1213 struct bprint_entry *entry;
1214 unsigned long flags;
1215 int resched;
1216 int cpu, len = 0, size, pc;
1218 if (unlikely(tracing_selftest_running || tracing_disabled))
1219 return 0;
1221 /* Don't pollute graph traces with trace_vprintk internals */
1222 pause_graph_tracing();
1224 pc = preempt_count();
1225 resched = ftrace_preempt_disable();
1226 cpu = raw_smp_processor_id();
1227 data = tr->data[cpu];
1229 if (unlikely(atomic_read(&data->disabled)))
1230 goto out;
1232 /* Lockdep uses trace_printk for lock tracing */
1233 local_irq_save(flags);
1234 __raw_spin_lock(&trace_buf_lock);
1235 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1237 if (len > TRACE_BUF_SIZE || len < 0)
1238 goto out_unlock;
1240 size = sizeof(*entry) + sizeof(u32) * len;
1241 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1242 if (!event)
1243 goto out_unlock;
1244 entry = ring_buffer_event_data(event);
1245 entry->ip = ip;
1246 entry->fmt = fmt;
1248 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1249 ring_buffer_unlock_commit(tr->buffer, event);
1251 out_unlock:
1252 __raw_spin_unlock(&trace_buf_lock);
1253 local_irq_restore(flags);
1255 out:
1256 ftrace_preempt_enable(resched);
1257 unpause_graph_tracing();
1259 return len;
1261 EXPORT_SYMBOL_GPL(trace_vbprintk);
1263 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1265 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1266 static char trace_buf[TRACE_BUF_SIZE];
1268 struct ring_buffer_event *event;
1269 struct trace_array *tr = &global_trace;
1270 struct trace_array_cpu *data;
1271 int cpu, len = 0, size, pc;
1272 struct print_entry *entry;
1273 unsigned long irq_flags;
1275 if (tracing_disabled || tracing_selftest_running)
1276 return 0;
1278 pc = preempt_count();
1279 preempt_disable_notrace();
1280 cpu = raw_smp_processor_id();
1281 data = tr->data[cpu];
1283 if (unlikely(atomic_read(&data->disabled)))
1284 goto out;
1286 pause_graph_tracing();
1287 raw_local_irq_save(irq_flags);
1288 __raw_spin_lock(&trace_buf_lock);
1289 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1291 len = min(len, TRACE_BUF_SIZE-1);
1292 trace_buf[len] = 0;
1294 size = sizeof(*entry) + len + 1;
1295 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1296 if (!event)
1297 goto out_unlock;
1298 entry = ring_buffer_event_data(event);
1299 entry->ip = ip;
1301 memcpy(&entry->buf, trace_buf, len);
1302 entry->buf[len] = 0;
1303 ring_buffer_unlock_commit(tr->buffer, event);
1305 out_unlock:
1306 __raw_spin_unlock(&trace_buf_lock);
1307 raw_local_irq_restore(irq_flags);
1308 unpause_graph_tracing();
1309 out:
1310 preempt_enable_notrace();
1312 return len;
1314 EXPORT_SYMBOL_GPL(trace_vprintk);
1316 enum trace_file_type {
1317 TRACE_FILE_LAT_FMT = 1,
1318 TRACE_FILE_ANNOTATE = 2,
1321 static void trace_iterator_increment(struct trace_iterator *iter)
1323 /* Don't allow ftrace to trace into the ring buffers */
1324 ftrace_disable_cpu();
1326 iter->idx++;
1327 if (iter->buffer_iter[iter->cpu])
1328 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1330 ftrace_enable_cpu();
1333 static struct trace_entry *
1334 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1336 struct ring_buffer_event *event;
1337 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1339 /* Don't allow ftrace to trace into the ring buffers */
1340 ftrace_disable_cpu();
1342 if (buf_iter)
1343 event = ring_buffer_iter_peek(buf_iter, ts);
1344 else
1345 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1347 ftrace_enable_cpu();
1349 return event ? ring_buffer_event_data(event) : NULL;
1352 static struct trace_entry *
1353 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1355 struct ring_buffer *buffer = iter->tr->buffer;
1356 struct trace_entry *ent, *next = NULL;
1357 int cpu_file = iter->cpu_file;
1358 u64 next_ts = 0, ts;
1359 int next_cpu = -1;
1360 int cpu;
1363 * If we are in a per_cpu trace file, don't bother by iterating over
1364 * all cpu and peek directly.
1366 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1367 if (ring_buffer_empty_cpu(buffer, cpu_file))
1368 return NULL;
1369 ent = peek_next_entry(iter, cpu_file, ent_ts);
1370 if (ent_cpu)
1371 *ent_cpu = cpu_file;
1373 return ent;
1376 for_each_tracing_cpu(cpu) {
1378 if (ring_buffer_empty_cpu(buffer, cpu))
1379 continue;
1381 ent = peek_next_entry(iter, cpu, &ts);
1384 * Pick the entry with the smallest timestamp:
1386 if (ent && (!next || ts < next_ts)) {
1387 next = ent;
1388 next_cpu = cpu;
1389 next_ts = ts;
1393 if (ent_cpu)
1394 *ent_cpu = next_cpu;
1396 if (ent_ts)
1397 *ent_ts = next_ts;
1399 return next;
1402 /* Find the next real entry, without updating the iterator itself */
1403 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1404 int *ent_cpu, u64 *ent_ts)
1406 return __find_next_entry(iter, ent_cpu, ent_ts);
1409 /* Find the next real entry, and increment the iterator to the next entry */
1410 static void *find_next_entry_inc(struct trace_iterator *iter)
1412 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1414 if (iter->ent)
1415 trace_iterator_increment(iter);
1417 return iter->ent ? iter : NULL;
1420 static void trace_consume(struct trace_iterator *iter)
1422 /* Don't allow ftrace to trace into the ring buffers */
1423 ftrace_disable_cpu();
1424 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1425 ftrace_enable_cpu();
1428 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1430 struct trace_iterator *iter = m->private;
1431 int i = (int)*pos;
1432 void *ent;
1434 (*pos)++;
1436 /* can't go backwards */
1437 if (iter->idx > i)
1438 return NULL;
1440 if (iter->idx < 0)
1441 ent = find_next_entry_inc(iter);
1442 else
1443 ent = iter;
1445 while (ent && iter->idx < i)
1446 ent = find_next_entry_inc(iter);
1448 iter->pos = *pos;
1450 return ent;
1454 * No necessary locking here. The worst thing which can
1455 * happen is loosing events consumed at the same time
1456 * by a trace_pipe reader.
1457 * Other than that, we don't risk to crash the ring buffer
1458 * because it serializes the readers.
1460 * The current tracer is copied to avoid a global locking
1461 * all around.
1463 static void *s_start(struct seq_file *m, loff_t *pos)
1465 struct trace_iterator *iter = m->private;
1466 static struct tracer *old_tracer;
1467 int cpu_file = iter->cpu_file;
1468 void *p = NULL;
1469 loff_t l = 0;
1470 int cpu;
1472 /* copy the tracer to avoid using a global lock all around */
1473 mutex_lock(&trace_types_lock);
1474 if (unlikely(old_tracer != current_trace && current_trace)) {
1475 old_tracer = current_trace;
1476 *iter->trace = *current_trace;
1478 mutex_unlock(&trace_types_lock);
1480 atomic_inc(&trace_record_cmdline_disabled);
1482 if (*pos != iter->pos) {
1483 iter->ent = NULL;
1484 iter->cpu = 0;
1485 iter->idx = -1;
1487 ftrace_disable_cpu();
1489 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1490 for_each_tracing_cpu(cpu)
1491 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1492 } else
1493 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1496 ftrace_enable_cpu();
1498 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1501 } else {
1502 l = *pos - 1;
1503 p = s_next(m, p, &l);
1506 return p;
1509 static void s_stop(struct seq_file *m, void *p)
1511 atomic_dec(&trace_record_cmdline_disabled);
1514 static void print_lat_help_header(struct seq_file *m)
1516 seq_puts(m, "# _------=> CPU# \n");
1517 seq_puts(m, "# / _-----=> irqs-off \n");
1518 seq_puts(m, "# | / _----=> need-resched \n");
1519 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1520 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1521 seq_puts(m, "# |||| / \n");
1522 seq_puts(m, "# ||||| delay \n");
1523 seq_puts(m, "# cmd pid ||||| time | caller \n");
1524 seq_puts(m, "# \\ / ||||| \\ | / \n");
1527 static void print_func_help_header(struct seq_file *m)
1529 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1530 seq_puts(m, "# | | | | |\n");
1534 static void
1535 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1537 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1538 struct trace_array *tr = iter->tr;
1539 struct trace_array_cpu *data = tr->data[tr->cpu];
1540 struct tracer *type = current_trace;
1541 unsigned long total;
1542 unsigned long entries;
1543 const char *name = "preemption";
1545 if (type)
1546 name = type->name;
1548 entries = ring_buffer_entries(iter->tr->buffer);
1549 total = entries +
1550 ring_buffer_overruns(iter->tr->buffer);
1552 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1553 name, UTS_RELEASE);
1554 seq_puts(m, "# -----------------------------------"
1555 "---------------------------------\n");
1556 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1557 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1558 nsecs_to_usecs(data->saved_latency),
1559 entries,
1560 total,
1561 tr->cpu,
1562 #if defined(CONFIG_PREEMPT_NONE)
1563 "server",
1564 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1565 "desktop",
1566 #elif defined(CONFIG_PREEMPT)
1567 "preempt",
1568 #else
1569 "unknown",
1570 #endif
1571 /* These are reserved for later use */
1572 0, 0, 0, 0);
1573 #ifdef CONFIG_SMP
1574 seq_printf(m, " #P:%d)\n", num_online_cpus());
1575 #else
1576 seq_puts(m, ")\n");
1577 #endif
1578 seq_puts(m, "# -----------------\n");
1579 seq_printf(m, "# | task: %.16s-%d "
1580 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1581 data->comm, data->pid, data->uid, data->nice,
1582 data->policy, data->rt_priority);
1583 seq_puts(m, "# -----------------\n");
1585 if (data->critical_start) {
1586 seq_puts(m, "# => started at: ");
1587 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1588 trace_print_seq(m, &iter->seq);
1589 seq_puts(m, "\n# => ended at: ");
1590 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1591 trace_print_seq(m, &iter->seq);
1592 seq_puts(m, "#\n");
1595 seq_puts(m, "#\n");
1598 static void test_cpu_buff_start(struct trace_iterator *iter)
1600 struct trace_seq *s = &iter->seq;
1602 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1603 return;
1605 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1606 return;
1608 if (cpumask_test_cpu(iter->cpu, iter->started))
1609 return;
1611 cpumask_set_cpu(iter->cpu, iter->started);
1612 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1615 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1617 struct trace_seq *s = &iter->seq;
1618 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1619 struct trace_entry *entry;
1620 struct trace_event *event;
1622 entry = iter->ent;
1624 test_cpu_buff_start(iter);
1626 event = ftrace_find_event(entry->type);
1628 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1629 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1630 if (!trace_print_lat_context(iter))
1631 goto partial;
1632 } else {
1633 if (!trace_print_context(iter))
1634 goto partial;
1638 if (event)
1639 return event->trace(iter, sym_flags);
1641 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1642 goto partial;
1644 return TRACE_TYPE_HANDLED;
1645 partial:
1646 return TRACE_TYPE_PARTIAL_LINE;
1649 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1651 struct trace_seq *s = &iter->seq;
1652 struct trace_entry *entry;
1653 struct trace_event *event;
1655 entry = iter->ent;
1657 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1658 if (!trace_seq_printf(s, "%d %d %llu ",
1659 entry->pid, iter->cpu, iter->ts))
1660 goto partial;
1663 event = ftrace_find_event(entry->type);
1664 if (event)
1665 return event->raw(iter, 0);
1667 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1668 goto partial;
1670 return TRACE_TYPE_HANDLED;
1671 partial:
1672 return TRACE_TYPE_PARTIAL_LINE;
1675 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1677 struct trace_seq *s = &iter->seq;
1678 unsigned char newline = '\n';
1679 struct trace_entry *entry;
1680 struct trace_event *event;
1682 entry = iter->ent;
1684 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1685 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1686 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1687 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1690 event = ftrace_find_event(entry->type);
1691 if (event) {
1692 enum print_line_t ret = event->hex(iter, 0);
1693 if (ret != TRACE_TYPE_HANDLED)
1694 return ret;
1697 SEQ_PUT_FIELD_RET(s, newline);
1699 return TRACE_TYPE_HANDLED;
1702 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1704 struct trace_seq *s = &iter->seq;
1705 struct trace_entry *entry;
1706 struct trace_event *event;
1708 entry = iter->ent;
1710 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1711 SEQ_PUT_FIELD_RET(s, entry->pid);
1712 SEQ_PUT_FIELD_RET(s, iter->cpu);
1713 SEQ_PUT_FIELD_RET(s, iter->ts);
1716 event = ftrace_find_event(entry->type);
1717 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1720 static int trace_empty(struct trace_iterator *iter)
1722 int cpu;
1724 /* If we are looking at one CPU buffer, only check that one */
1725 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1726 cpu = iter->cpu_file;
1727 if (iter->buffer_iter[cpu]) {
1728 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1729 return 0;
1730 } else {
1731 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1732 return 0;
1734 return 1;
1737 for_each_tracing_cpu(cpu) {
1738 if (iter->buffer_iter[cpu]) {
1739 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1740 return 0;
1741 } else {
1742 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1743 return 0;
1747 return 1;
1750 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1752 enum print_line_t ret;
1754 if (iter->trace && iter->trace->print_line) {
1755 ret = iter->trace->print_line(iter);
1756 if (ret != TRACE_TYPE_UNHANDLED)
1757 return ret;
1760 if (iter->ent->type == TRACE_BPRINT &&
1761 trace_flags & TRACE_ITER_PRINTK &&
1762 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1763 return trace_print_bprintk_msg_only(iter);
1765 if (iter->ent->type == TRACE_PRINT &&
1766 trace_flags & TRACE_ITER_PRINTK &&
1767 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1768 return trace_print_printk_msg_only(iter);
1770 if (trace_flags & TRACE_ITER_BIN)
1771 return print_bin_fmt(iter);
1773 if (trace_flags & TRACE_ITER_HEX)
1774 return print_hex_fmt(iter);
1776 if (trace_flags & TRACE_ITER_RAW)
1777 return print_raw_fmt(iter);
1779 return print_trace_fmt(iter);
1782 static int s_show(struct seq_file *m, void *v)
1784 struct trace_iterator *iter = v;
1786 if (iter->ent == NULL) {
1787 if (iter->tr) {
1788 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1789 seq_puts(m, "#\n");
1791 if (iter->trace && iter->trace->print_header)
1792 iter->trace->print_header(m);
1793 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1794 /* print nothing if the buffers are empty */
1795 if (trace_empty(iter))
1796 return 0;
1797 print_trace_header(m, iter);
1798 if (!(trace_flags & TRACE_ITER_VERBOSE))
1799 print_lat_help_header(m);
1800 } else {
1801 if (!(trace_flags & TRACE_ITER_VERBOSE))
1802 print_func_help_header(m);
1804 } else {
1805 print_trace_line(iter);
1806 trace_print_seq(m, &iter->seq);
1809 return 0;
1812 static struct seq_operations tracer_seq_ops = {
1813 .start = s_start,
1814 .next = s_next,
1815 .stop = s_stop,
1816 .show = s_show,
1819 static struct trace_iterator *
1820 __tracing_open(struct inode *inode, struct file *file)
1822 long cpu_file = (long) inode->i_private;
1823 void *fail_ret = ERR_PTR(-ENOMEM);
1824 struct trace_iterator *iter;
1825 struct seq_file *m;
1826 int cpu, ret;
1828 if (tracing_disabled)
1829 return ERR_PTR(-ENODEV);
1831 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1832 if (!iter)
1833 return ERR_PTR(-ENOMEM);
1836 * We make a copy of the current tracer to avoid concurrent
1837 * changes on it while we are reading.
1839 mutex_lock(&trace_types_lock);
1840 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1841 if (!iter->trace)
1842 goto fail;
1844 if (current_trace)
1845 *iter->trace = *current_trace;
1847 if (current_trace && current_trace->print_max)
1848 iter->tr = &max_tr;
1849 else
1850 iter->tr = &global_trace;
1851 iter->pos = -1;
1852 mutex_init(&iter->mutex);
1853 iter->cpu_file = cpu_file;
1855 /* Notify the tracer early; before we stop tracing. */
1856 if (iter->trace && iter->trace->open)
1857 iter->trace->open(iter);
1859 /* Annotate start of buffers if we had overruns */
1860 if (ring_buffer_overruns(iter->tr->buffer))
1861 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1863 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1864 for_each_tracing_cpu(cpu) {
1866 iter->buffer_iter[cpu] =
1867 ring_buffer_read_start(iter->tr->buffer, cpu);
1869 } else {
1870 cpu = iter->cpu_file;
1871 iter->buffer_iter[cpu] =
1872 ring_buffer_read_start(iter->tr->buffer, cpu);
1875 /* TODO stop tracer */
1876 ret = seq_open(file, &tracer_seq_ops);
1877 if (ret < 0) {
1878 fail_ret = ERR_PTR(ret);
1879 goto fail_buffer;
1882 m = file->private_data;
1883 m->private = iter;
1885 /* stop the trace while dumping */
1886 tracing_stop();
1888 mutex_unlock(&trace_types_lock);
1890 return iter;
1892 fail_buffer:
1893 for_each_tracing_cpu(cpu) {
1894 if (iter->buffer_iter[cpu])
1895 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1897 fail:
1898 mutex_unlock(&trace_types_lock);
1899 kfree(iter->trace);
1900 kfree(iter);
1902 return fail_ret;
1905 int tracing_open_generic(struct inode *inode, struct file *filp)
1907 if (tracing_disabled)
1908 return -ENODEV;
1910 filp->private_data = inode->i_private;
1911 return 0;
1914 static int tracing_release(struct inode *inode, struct file *file)
1916 struct seq_file *m = (struct seq_file *)file->private_data;
1917 struct trace_iterator *iter;
1918 int cpu;
1920 if (!(file->f_mode & FMODE_READ))
1921 return 0;
1923 iter = m->private;
1925 mutex_lock(&trace_types_lock);
1926 for_each_tracing_cpu(cpu) {
1927 if (iter->buffer_iter[cpu])
1928 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1931 if (iter->trace && iter->trace->close)
1932 iter->trace->close(iter);
1934 /* reenable tracing if it was previously enabled */
1935 tracing_start();
1936 mutex_unlock(&trace_types_lock);
1938 seq_release(inode, file);
1939 mutex_destroy(&iter->mutex);
1940 kfree(iter->trace);
1941 kfree(iter);
1942 return 0;
1945 static int tracing_open(struct inode *inode, struct file *file)
1947 struct trace_iterator *iter;
1948 int ret = 0;
1950 /* If this file was open for write, then erase contents */
1951 if ((file->f_mode & FMODE_WRITE) &&
1952 !(file->f_flags & O_APPEND)) {
1953 long cpu = (long) inode->i_private;
1955 if (cpu == TRACE_PIPE_ALL_CPU)
1956 tracing_reset_online_cpus(&global_trace);
1957 else
1958 tracing_reset(&global_trace, cpu);
1961 if (file->f_mode & FMODE_READ) {
1962 iter = __tracing_open(inode, file);
1963 if (IS_ERR(iter))
1964 ret = PTR_ERR(iter);
1965 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1966 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1968 return ret;
1971 static void *
1972 t_next(struct seq_file *m, void *v, loff_t *pos)
1974 struct tracer *t = m->private;
1976 (*pos)++;
1978 if (t)
1979 t = t->next;
1981 m->private = t;
1983 return t;
1986 static void *t_start(struct seq_file *m, loff_t *pos)
1988 struct tracer *t = m->private;
1989 loff_t l = 0;
1991 mutex_lock(&trace_types_lock);
1992 for (; t && l < *pos; t = t_next(m, t, &l))
1995 return t;
1998 static void t_stop(struct seq_file *m, void *p)
2000 mutex_unlock(&trace_types_lock);
2003 static int t_show(struct seq_file *m, void *v)
2005 struct tracer *t = v;
2007 if (!t)
2008 return 0;
2010 seq_printf(m, "%s", t->name);
2011 if (t->next)
2012 seq_putc(m, ' ');
2013 else
2014 seq_putc(m, '\n');
2016 return 0;
2019 static struct seq_operations show_traces_seq_ops = {
2020 .start = t_start,
2021 .next = t_next,
2022 .stop = t_stop,
2023 .show = t_show,
2026 static int show_traces_open(struct inode *inode, struct file *file)
2028 int ret;
2030 if (tracing_disabled)
2031 return -ENODEV;
2033 ret = seq_open(file, &show_traces_seq_ops);
2034 if (!ret) {
2035 struct seq_file *m = file->private_data;
2036 m->private = trace_types;
2039 return ret;
2042 static ssize_t
2043 tracing_write_stub(struct file *filp, const char __user *ubuf,
2044 size_t count, loff_t *ppos)
2046 return count;
2049 static const struct file_operations tracing_fops = {
2050 .open = tracing_open,
2051 .read = seq_read,
2052 .write = tracing_write_stub,
2053 .llseek = seq_lseek,
2054 .release = tracing_release,
2057 static const struct file_operations show_traces_fops = {
2058 .open = show_traces_open,
2059 .read = seq_read,
2060 .release = seq_release,
2064 * Only trace on a CPU if the bitmask is set:
2066 static cpumask_var_t tracing_cpumask;
2069 * The tracer itself will not take this lock, but still we want
2070 * to provide a consistent cpumask to user-space:
2072 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2075 * Temporary storage for the character representation of the
2076 * CPU bitmask (and one more byte for the newline):
2078 static char mask_str[NR_CPUS + 1];
2080 static ssize_t
2081 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2082 size_t count, loff_t *ppos)
2084 int len;
2086 mutex_lock(&tracing_cpumask_update_lock);
2088 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2089 if (count - len < 2) {
2090 count = -EINVAL;
2091 goto out_err;
2093 len += sprintf(mask_str + len, "\n");
2094 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2096 out_err:
2097 mutex_unlock(&tracing_cpumask_update_lock);
2099 return count;
2102 static ssize_t
2103 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2104 size_t count, loff_t *ppos)
2106 int err, cpu;
2107 cpumask_var_t tracing_cpumask_new;
2109 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2110 return -ENOMEM;
2112 mutex_lock(&tracing_cpumask_update_lock);
2113 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2114 if (err)
2115 goto err_unlock;
2117 local_irq_disable();
2118 __raw_spin_lock(&ftrace_max_lock);
2119 for_each_tracing_cpu(cpu) {
2121 * Increase/decrease the disabled counter if we are
2122 * about to flip a bit in the cpumask:
2124 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2125 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2126 atomic_inc(&global_trace.data[cpu]->disabled);
2128 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2129 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2130 atomic_dec(&global_trace.data[cpu]->disabled);
2133 __raw_spin_unlock(&ftrace_max_lock);
2134 local_irq_enable();
2136 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2138 mutex_unlock(&tracing_cpumask_update_lock);
2139 free_cpumask_var(tracing_cpumask_new);
2141 return count;
2143 err_unlock:
2144 mutex_unlock(&tracing_cpumask_update_lock);
2145 free_cpumask_var(tracing_cpumask);
2147 return err;
2150 static const struct file_operations tracing_cpumask_fops = {
2151 .open = tracing_open_generic,
2152 .read = tracing_cpumask_read,
2153 .write = tracing_cpumask_write,
2156 static ssize_t
2157 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2158 size_t cnt, loff_t *ppos)
2160 struct tracer_opt *trace_opts;
2161 u32 tracer_flags;
2162 int len = 0;
2163 char *buf;
2164 int r = 0;
2165 int i;
2168 /* calculate max size */
2169 for (i = 0; trace_options[i]; i++) {
2170 len += strlen(trace_options[i]);
2171 len += 3; /* "no" and newline */
2174 mutex_lock(&trace_types_lock);
2175 tracer_flags = current_trace->flags->val;
2176 trace_opts = current_trace->flags->opts;
2179 * Increase the size with names of options specific
2180 * of the current tracer.
2182 for (i = 0; trace_opts[i].name; i++) {
2183 len += strlen(trace_opts[i].name);
2184 len += 3; /* "no" and newline */
2187 /* +2 for \n and \0 */
2188 buf = kmalloc(len + 2, GFP_KERNEL);
2189 if (!buf) {
2190 mutex_unlock(&trace_types_lock);
2191 return -ENOMEM;
2194 for (i = 0; trace_options[i]; i++) {
2195 if (trace_flags & (1 << i))
2196 r += sprintf(buf + r, "%s\n", trace_options[i]);
2197 else
2198 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2201 for (i = 0; trace_opts[i].name; i++) {
2202 if (tracer_flags & trace_opts[i].bit)
2203 r += sprintf(buf + r, "%s\n",
2204 trace_opts[i].name);
2205 else
2206 r += sprintf(buf + r, "no%s\n",
2207 trace_opts[i].name);
2209 mutex_unlock(&trace_types_lock);
2211 WARN_ON(r >= len + 2);
2213 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2215 kfree(buf);
2216 return r;
2219 /* Try to assign a tracer specific option */
2220 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2222 struct tracer_flags *trace_flags = trace->flags;
2223 struct tracer_opt *opts = NULL;
2224 int ret = 0, i = 0;
2225 int len;
2227 for (i = 0; trace_flags->opts[i].name; i++) {
2228 opts = &trace_flags->opts[i];
2229 len = strlen(opts->name);
2231 if (strncmp(cmp, opts->name, len) == 0) {
2232 ret = trace->set_flag(trace_flags->val,
2233 opts->bit, !neg);
2234 break;
2237 /* Not found */
2238 if (!trace_flags->opts[i].name)
2239 return -EINVAL;
2241 /* Refused to handle */
2242 if (ret)
2243 return ret;
2245 if (neg)
2246 trace_flags->val &= ~opts->bit;
2247 else
2248 trace_flags->val |= opts->bit;
2250 return 0;
2253 static void set_tracer_flags(unsigned int mask, int enabled)
2255 /* do nothing if flag is already set */
2256 if (!!(trace_flags & mask) == !!enabled)
2257 return;
2259 if (enabled)
2260 trace_flags |= mask;
2261 else
2262 trace_flags &= ~mask;
2264 if (mask == TRACE_ITER_GLOBAL_CLK) {
2265 u64 (*func)(void);
2267 if (enabled)
2268 func = trace_clock_global;
2269 else
2270 func = trace_clock_local;
2272 mutex_lock(&trace_types_lock);
2273 ring_buffer_set_clock(global_trace.buffer, func);
2275 if (max_tr.buffer)
2276 ring_buffer_set_clock(max_tr.buffer, func);
2277 mutex_unlock(&trace_types_lock);
2281 static ssize_t
2282 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2283 size_t cnt, loff_t *ppos)
2285 char buf[64];
2286 char *cmp = buf;
2287 int neg = 0;
2288 int ret;
2289 int i;
2291 if (cnt >= sizeof(buf))
2292 return -EINVAL;
2294 if (copy_from_user(&buf, ubuf, cnt))
2295 return -EFAULT;
2297 buf[cnt] = 0;
2299 if (strncmp(buf, "no", 2) == 0) {
2300 neg = 1;
2301 cmp += 2;
2304 for (i = 0; trace_options[i]; i++) {
2305 int len = strlen(trace_options[i]);
2307 if (strncmp(cmp, trace_options[i], len) == 0) {
2308 set_tracer_flags(1 << i, !neg);
2309 break;
2313 /* If no option could be set, test the specific tracer options */
2314 if (!trace_options[i]) {
2315 mutex_lock(&trace_types_lock);
2316 ret = set_tracer_option(current_trace, cmp, neg);
2317 mutex_unlock(&trace_types_lock);
2318 if (ret)
2319 return ret;
2322 filp->f_pos += cnt;
2324 return cnt;
2327 static const struct file_operations tracing_iter_fops = {
2328 .open = tracing_open_generic,
2329 .read = tracing_trace_options_read,
2330 .write = tracing_trace_options_write,
2333 static const char readme_msg[] =
2334 "tracing mini-HOWTO:\n\n"
2335 "# mkdir /debug\n"
2336 "# mount -t debugfs nodev /debug\n\n"
2337 "# cat /debug/tracing/available_tracers\n"
2338 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2339 "# cat /debug/tracing/current_tracer\n"
2340 "none\n"
2341 "# echo sched_switch > /debug/tracing/current_tracer\n"
2342 "# cat /debug/tracing/current_tracer\n"
2343 "sched_switch\n"
2344 "# cat /debug/tracing/trace_options\n"
2345 "noprint-parent nosym-offset nosym-addr noverbose\n"
2346 "# echo print-parent > /debug/tracing/trace_options\n"
2347 "# echo 1 > /debug/tracing/tracing_enabled\n"
2348 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2349 "echo 0 > /debug/tracing/tracing_enabled\n"
2352 static ssize_t
2353 tracing_readme_read(struct file *filp, char __user *ubuf,
2354 size_t cnt, loff_t *ppos)
2356 return simple_read_from_buffer(ubuf, cnt, ppos,
2357 readme_msg, strlen(readme_msg));
2360 static const struct file_operations tracing_readme_fops = {
2361 .open = tracing_open_generic,
2362 .read = tracing_readme_read,
2365 static ssize_t
2366 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2367 size_t cnt, loff_t *ppos)
2369 char buf[64];
2370 int r;
2372 r = sprintf(buf, "%u\n", tracer_enabled);
2373 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2376 static ssize_t
2377 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2378 size_t cnt, loff_t *ppos)
2380 struct trace_array *tr = filp->private_data;
2381 char buf[64];
2382 unsigned long val;
2383 int ret;
2385 if (cnt >= sizeof(buf))
2386 return -EINVAL;
2388 if (copy_from_user(&buf, ubuf, cnt))
2389 return -EFAULT;
2391 buf[cnt] = 0;
2393 ret = strict_strtoul(buf, 10, &val);
2394 if (ret < 0)
2395 return ret;
2397 val = !!val;
2399 mutex_lock(&trace_types_lock);
2400 if (tracer_enabled ^ val) {
2401 if (val) {
2402 tracer_enabled = 1;
2403 if (current_trace->start)
2404 current_trace->start(tr);
2405 tracing_start();
2406 } else {
2407 tracer_enabled = 0;
2408 tracing_stop();
2409 if (current_trace->stop)
2410 current_trace->stop(tr);
2413 mutex_unlock(&trace_types_lock);
2415 filp->f_pos += cnt;
2417 return cnt;
2420 static ssize_t
2421 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2422 size_t cnt, loff_t *ppos)
2424 char buf[max_tracer_type_len+2];
2425 int r;
2427 mutex_lock(&trace_types_lock);
2428 if (current_trace)
2429 r = sprintf(buf, "%s\n", current_trace->name);
2430 else
2431 r = sprintf(buf, "\n");
2432 mutex_unlock(&trace_types_lock);
2434 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2437 int tracer_init(struct tracer *t, struct trace_array *tr)
2439 tracing_reset_online_cpus(tr);
2440 return t->init(tr);
2443 static int tracing_resize_ring_buffer(unsigned long size)
2445 int ret;
2448 * If kernel or user changes the size of the ring buffer
2449 * we use the size that was given, and we can forget about
2450 * expanding it later.
2452 ring_buffer_expanded = 1;
2454 ret = ring_buffer_resize(global_trace.buffer, size);
2455 if (ret < 0)
2456 return ret;
2458 ret = ring_buffer_resize(max_tr.buffer, size);
2459 if (ret < 0) {
2460 int r;
2462 r = ring_buffer_resize(global_trace.buffer,
2463 global_trace.entries);
2464 if (r < 0) {
2466 * AARGH! We are left with different
2467 * size max buffer!!!!
2468 * The max buffer is our "snapshot" buffer.
2469 * When a tracer needs a snapshot (one of the
2470 * latency tracers), it swaps the max buffer
2471 * with the saved snap shot. We succeeded to
2472 * update the size of the main buffer, but failed to
2473 * update the size of the max buffer. But when we tried
2474 * to reset the main buffer to the original size, we
2475 * failed there too. This is very unlikely to
2476 * happen, but if it does, warn and kill all
2477 * tracing.
2479 WARN_ON(1);
2480 tracing_disabled = 1;
2482 return ret;
2485 global_trace.entries = size;
2487 return ret;
2491 * tracing_update_buffers - used by tracing facility to expand ring buffers
2493 * To save on memory when the tracing is never used on a system with it
2494 * configured in. The ring buffers are set to a minimum size. But once
2495 * a user starts to use the tracing facility, then they need to grow
2496 * to their default size.
2498 * This function is to be called when a tracer is about to be used.
2500 int tracing_update_buffers(void)
2502 int ret = 0;
2504 mutex_lock(&trace_types_lock);
2505 if (!ring_buffer_expanded)
2506 ret = tracing_resize_ring_buffer(trace_buf_size);
2507 mutex_unlock(&trace_types_lock);
2509 return ret;
2512 struct trace_option_dentry;
2514 static struct trace_option_dentry *
2515 create_trace_option_files(struct tracer *tracer);
2517 static void
2518 destroy_trace_option_files(struct trace_option_dentry *topts);
2520 static int tracing_set_tracer(const char *buf)
2522 static struct trace_option_dentry *topts;
2523 struct trace_array *tr = &global_trace;
2524 struct tracer *t;
2525 int ret = 0;
2527 mutex_lock(&trace_types_lock);
2529 if (!ring_buffer_expanded) {
2530 ret = tracing_resize_ring_buffer(trace_buf_size);
2531 if (ret < 0)
2532 goto out;
2533 ret = 0;
2536 for (t = trace_types; t; t = t->next) {
2537 if (strcmp(t->name, buf) == 0)
2538 break;
2540 if (!t) {
2541 ret = -EINVAL;
2542 goto out;
2544 if (t == current_trace)
2545 goto out;
2547 trace_branch_disable();
2548 if (current_trace && current_trace->reset)
2549 current_trace->reset(tr);
2551 destroy_trace_option_files(topts);
2553 current_trace = t;
2555 topts = create_trace_option_files(current_trace);
2557 if (t->init) {
2558 ret = tracer_init(t, tr);
2559 if (ret)
2560 goto out;
2563 trace_branch_enable(tr);
2564 out:
2565 mutex_unlock(&trace_types_lock);
2567 return ret;
2570 static ssize_t
2571 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2572 size_t cnt, loff_t *ppos)
2574 char buf[max_tracer_type_len+1];
2575 int i;
2576 size_t ret;
2577 int err;
2579 ret = cnt;
2581 if (cnt > max_tracer_type_len)
2582 cnt = max_tracer_type_len;
2584 if (copy_from_user(&buf, ubuf, cnt))
2585 return -EFAULT;
2587 buf[cnt] = 0;
2589 /* strip ending whitespace. */
2590 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2591 buf[i] = 0;
2593 err = tracing_set_tracer(buf);
2594 if (err)
2595 return err;
2597 filp->f_pos += ret;
2599 return ret;
2602 static ssize_t
2603 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2604 size_t cnt, loff_t *ppos)
2606 unsigned long *ptr = filp->private_data;
2607 char buf[64];
2608 int r;
2610 r = snprintf(buf, sizeof(buf), "%ld\n",
2611 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2612 if (r > sizeof(buf))
2613 r = sizeof(buf);
2614 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2617 static ssize_t
2618 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2619 size_t cnt, loff_t *ppos)
2621 unsigned long *ptr = filp->private_data;
2622 char buf[64];
2623 unsigned long val;
2624 int ret;
2626 if (cnt >= sizeof(buf))
2627 return -EINVAL;
2629 if (copy_from_user(&buf, ubuf, cnt))
2630 return -EFAULT;
2632 buf[cnt] = 0;
2634 ret = strict_strtoul(buf, 10, &val);
2635 if (ret < 0)
2636 return ret;
2638 *ptr = val * 1000;
2640 return cnt;
2643 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2645 long cpu_file = (long) inode->i_private;
2646 struct trace_iterator *iter;
2647 int ret = 0;
2649 if (tracing_disabled)
2650 return -ENODEV;
2652 mutex_lock(&trace_types_lock);
2654 /* We only allow one reader per cpu */
2655 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2656 if (!cpumask_empty(tracing_reader_cpumask)) {
2657 ret = -EBUSY;
2658 goto out;
2660 cpumask_setall(tracing_reader_cpumask);
2661 } else {
2662 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2663 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2664 else {
2665 ret = -EBUSY;
2666 goto out;
2670 /* create a buffer to store the information to pass to userspace */
2671 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2672 if (!iter) {
2673 ret = -ENOMEM;
2674 goto out;
2678 * We make a copy of the current tracer to avoid concurrent
2679 * changes on it while we are reading.
2681 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2682 if (!iter->trace) {
2683 ret = -ENOMEM;
2684 goto fail;
2686 if (current_trace)
2687 *iter->trace = *current_trace;
2689 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2690 ret = -ENOMEM;
2691 goto fail;
2694 /* trace pipe does not show start of buffer */
2695 cpumask_setall(iter->started);
2697 iter->cpu_file = cpu_file;
2698 iter->tr = &global_trace;
2699 mutex_init(&iter->mutex);
2700 filp->private_data = iter;
2702 if (iter->trace->pipe_open)
2703 iter->trace->pipe_open(iter);
2705 out:
2706 mutex_unlock(&trace_types_lock);
2707 return ret;
2709 fail:
2710 kfree(iter->trace);
2711 kfree(iter);
2712 mutex_unlock(&trace_types_lock);
2713 return ret;
2716 static int tracing_release_pipe(struct inode *inode, struct file *file)
2718 struct trace_iterator *iter = file->private_data;
2720 mutex_lock(&trace_types_lock);
2722 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2723 cpumask_clear(tracing_reader_cpumask);
2724 else
2725 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2727 mutex_unlock(&trace_types_lock);
2729 free_cpumask_var(iter->started);
2730 mutex_destroy(&iter->mutex);
2731 kfree(iter->trace);
2732 kfree(iter);
2734 return 0;
2737 static unsigned int
2738 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2740 struct trace_iterator *iter = filp->private_data;
2742 if (trace_flags & TRACE_ITER_BLOCK) {
2744 * Always select as readable when in blocking mode
2746 return POLLIN | POLLRDNORM;
2747 } else {
2748 if (!trace_empty(iter))
2749 return POLLIN | POLLRDNORM;
2750 poll_wait(filp, &trace_wait, poll_table);
2751 if (!trace_empty(iter))
2752 return POLLIN | POLLRDNORM;
2754 return 0;
2759 void default_wait_pipe(struct trace_iterator *iter)
2761 DEFINE_WAIT(wait);
2763 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2765 if (trace_empty(iter))
2766 schedule();
2768 finish_wait(&trace_wait, &wait);
2772 * This is a make-shift waitqueue.
2773 * A tracer might use this callback on some rare cases:
2775 * 1) the current tracer might hold the runqueue lock when it wakes up
2776 * a reader, hence a deadlock (sched, function, and function graph tracers)
2777 * 2) the function tracers, trace all functions, we don't want
2778 * the overhead of calling wake_up and friends
2779 * (and tracing them too)
2781 * Anyway, this is really very primitive wakeup.
2783 void poll_wait_pipe(struct trace_iterator *iter)
2785 set_current_state(TASK_INTERRUPTIBLE);
2786 /* sleep for 100 msecs, and try again. */
2787 schedule_timeout(HZ / 10);
2790 /* Must be called with trace_types_lock mutex held. */
2791 static int tracing_wait_pipe(struct file *filp)
2793 struct trace_iterator *iter = filp->private_data;
2795 while (trace_empty(iter)) {
2797 if ((filp->f_flags & O_NONBLOCK)) {
2798 return -EAGAIN;
2801 mutex_unlock(&iter->mutex);
2803 iter->trace->wait_pipe(iter);
2805 mutex_lock(&iter->mutex);
2807 if (signal_pending(current))
2808 return -EINTR;
2811 * We block until we read something and tracing is disabled.
2812 * We still block if tracing is disabled, but we have never
2813 * read anything. This allows a user to cat this file, and
2814 * then enable tracing. But after we have read something,
2815 * we give an EOF when tracing is again disabled.
2817 * iter->pos will be 0 if we haven't read anything.
2819 if (!tracer_enabled && iter->pos)
2820 break;
2823 return 1;
2827 * Consumer reader.
2829 static ssize_t
2830 tracing_read_pipe(struct file *filp, char __user *ubuf,
2831 size_t cnt, loff_t *ppos)
2833 struct trace_iterator *iter = filp->private_data;
2834 static struct tracer *old_tracer;
2835 ssize_t sret;
2837 /* return any leftover data */
2838 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2839 if (sret != -EBUSY)
2840 return sret;
2842 trace_seq_init(&iter->seq);
2844 /* copy the tracer to avoid using a global lock all around */
2845 mutex_lock(&trace_types_lock);
2846 if (unlikely(old_tracer != current_trace && current_trace)) {
2847 old_tracer = current_trace;
2848 *iter->trace = *current_trace;
2850 mutex_unlock(&trace_types_lock);
2853 * Avoid more than one consumer on a single file descriptor
2854 * This is just a matter of traces coherency, the ring buffer itself
2855 * is protected.
2857 mutex_lock(&iter->mutex);
2858 if (iter->trace->read) {
2859 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2860 if (sret)
2861 goto out;
2864 waitagain:
2865 sret = tracing_wait_pipe(filp);
2866 if (sret <= 0)
2867 goto out;
2869 /* stop when tracing is finished */
2870 if (trace_empty(iter)) {
2871 sret = 0;
2872 goto out;
2875 if (cnt >= PAGE_SIZE)
2876 cnt = PAGE_SIZE - 1;
2878 /* reset all but tr, trace, and overruns */
2879 memset(&iter->seq, 0,
2880 sizeof(struct trace_iterator) -
2881 offsetof(struct trace_iterator, seq));
2882 iter->pos = -1;
2884 while (find_next_entry_inc(iter) != NULL) {
2885 enum print_line_t ret;
2886 int len = iter->seq.len;
2888 ret = print_trace_line(iter);
2889 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2890 /* don't print partial lines */
2891 iter->seq.len = len;
2892 break;
2894 if (ret != TRACE_TYPE_NO_CONSUME)
2895 trace_consume(iter);
2897 if (iter->seq.len >= cnt)
2898 break;
2901 /* Now copy what we have to the user */
2902 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2903 if (iter->seq.readpos >= iter->seq.len)
2904 trace_seq_init(&iter->seq);
2907 * If there was nothing to send to user, inspite of consuming trace
2908 * entries, go back to wait for more entries.
2910 if (sret == -EBUSY)
2911 goto waitagain;
2913 out:
2914 mutex_unlock(&iter->mutex);
2916 return sret;
2919 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2920 struct pipe_buffer *buf)
2922 __free_page(buf->page);
2925 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2926 unsigned int idx)
2928 __free_page(spd->pages[idx]);
2931 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2932 .can_merge = 0,
2933 .map = generic_pipe_buf_map,
2934 .unmap = generic_pipe_buf_unmap,
2935 .confirm = generic_pipe_buf_confirm,
2936 .release = tracing_pipe_buf_release,
2937 .steal = generic_pipe_buf_steal,
2938 .get = generic_pipe_buf_get,
2941 static size_t
2942 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2944 size_t count;
2945 int ret;
2947 /* Seq buffer is page-sized, exactly what we need. */
2948 for (;;) {
2949 count = iter->seq.len;
2950 ret = print_trace_line(iter);
2951 count = iter->seq.len - count;
2952 if (rem < count) {
2953 rem = 0;
2954 iter->seq.len -= count;
2955 break;
2957 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2958 iter->seq.len -= count;
2959 break;
2962 trace_consume(iter);
2963 rem -= count;
2964 if (!find_next_entry_inc(iter)) {
2965 rem = 0;
2966 iter->ent = NULL;
2967 break;
2971 return rem;
2974 static ssize_t tracing_splice_read_pipe(struct file *filp,
2975 loff_t *ppos,
2976 struct pipe_inode_info *pipe,
2977 size_t len,
2978 unsigned int flags)
2980 struct page *pages[PIPE_BUFFERS];
2981 struct partial_page partial[PIPE_BUFFERS];
2982 struct trace_iterator *iter = filp->private_data;
2983 struct splice_pipe_desc spd = {
2984 .pages = pages,
2985 .partial = partial,
2986 .nr_pages = 0, /* This gets updated below. */
2987 .flags = flags,
2988 .ops = &tracing_pipe_buf_ops,
2989 .spd_release = tracing_spd_release_pipe,
2991 static struct tracer *old_tracer;
2992 ssize_t ret;
2993 size_t rem;
2994 unsigned int i;
2996 /* copy the tracer to avoid using a global lock all around */
2997 mutex_lock(&trace_types_lock);
2998 if (unlikely(old_tracer != current_trace && current_trace)) {
2999 old_tracer = current_trace;
3000 *iter->trace = *current_trace;
3002 mutex_unlock(&trace_types_lock);
3004 mutex_lock(&iter->mutex);
3006 if (iter->trace->splice_read) {
3007 ret = iter->trace->splice_read(iter, filp,
3008 ppos, pipe, len, flags);
3009 if (ret)
3010 goto out_err;
3013 ret = tracing_wait_pipe(filp);
3014 if (ret <= 0)
3015 goto out_err;
3017 if (!iter->ent && !find_next_entry_inc(iter)) {
3018 ret = -EFAULT;
3019 goto out_err;
3022 /* Fill as many pages as possible. */
3023 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3024 pages[i] = alloc_page(GFP_KERNEL);
3025 if (!pages[i])
3026 break;
3028 rem = tracing_fill_pipe_page(rem, iter);
3030 /* Copy the data into the page, so we can start over. */
3031 ret = trace_seq_to_buffer(&iter->seq,
3032 page_address(pages[i]),
3033 iter->seq.len);
3034 if (ret < 0) {
3035 __free_page(pages[i]);
3036 break;
3038 partial[i].offset = 0;
3039 partial[i].len = iter->seq.len;
3041 trace_seq_init(&iter->seq);
3044 mutex_unlock(&iter->mutex);
3046 spd.nr_pages = i;
3048 return splice_to_pipe(pipe, &spd);
3050 out_err:
3051 mutex_unlock(&iter->mutex);
3053 return ret;
3056 static ssize_t
3057 tracing_entries_read(struct file *filp, char __user *ubuf,
3058 size_t cnt, loff_t *ppos)
3060 struct trace_array *tr = filp->private_data;
3061 char buf[96];
3062 int r;
3064 mutex_lock(&trace_types_lock);
3065 if (!ring_buffer_expanded)
3066 r = sprintf(buf, "%lu (expanded: %lu)\n",
3067 tr->entries >> 10,
3068 trace_buf_size >> 10);
3069 else
3070 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3071 mutex_unlock(&trace_types_lock);
3073 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3076 static ssize_t
3077 tracing_entries_write(struct file *filp, const char __user *ubuf,
3078 size_t cnt, loff_t *ppos)
3080 unsigned long val;
3081 char buf[64];
3082 int ret, cpu;
3084 if (cnt >= sizeof(buf))
3085 return -EINVAL;
3087 if (copy_from_user(&buf, ubuf, cnt))
3088 return -EFAULT;
3090 buf[cnt] = 0;
3092 ret = strict_strtoul(buf, 10, &val);
3093 if (ret < 0)
3094 return ret;
3096 /* must have at least 1 entry */
3097 if (!val)
3098 return -EINVAL;
3100 mutex_lock(&trace_types_lock);
3102 tracing_stop();
3104 /* disable all cpu buffers */
3105 for_each_tracing_cpu(cpu) {
3106 if (global_trace.data[cpu])
3107 atomic_inc(&global_trace.data[cpu]->disabled);
3108 if (max_tr.data[cpu])
3109 atomic_inc(&max_tr.data[cpu]->disabled);
3112 /* value is in KB */
3113 val <<= 10;
3115 if (val != global_trace.entries) {
3116 ret = tracing_resize_ring_buffer(val);
3117 if (ret < 0) {
3118 cnt = ret;
3119 goto out;
3123 filp->f_pos += cnt;
3125 /* If check pages failed, return ENOMEM */
3126 if (tracing_disabled)
3127 cnt = -ENOMEM;
3128 out:
3129 for_each_tracing_cpu(cpu) {
3130 if (global_trace.data[cpu])
3131 atomic_dec(&global_trace.data[cpu]->disabled);
3132 if (max_tr.data[cpu])
3133 atomic_dec(&max_tr.data[cpu]->disabled);
3136 tracing_start();
3137 max_tr.entries = global_trace.entries;
3138 mutex_unlock(&trace_types_lock);
3140 return cnt;
3143 static int mark_printk(const char *fmt, ...)
3145 int ret;
3146 va_list args;
3147 va_start(args, fmt);
3148 ret = trace_vprintk(0, fmt, args);
3149 va_end(args);
3150 return ret;
3153 static ssize_t
3154 tracing_mark_write(struct file *filp, const char __user *ubuf,
3155 size_t cnt, loff_t *fpos)
3157 char *buf;
3158 char *end;
3160 if (tracing_disabled)
3161 return -EINVAL;
3163 if (cnt > TRACE_BUF_SIZE)
3164 cnt = TRACE_BUF_SIZE;
3166 buf = kmalloc(cnt + 1, GFP_KERNEL);
3167 if (buf == NULL)
3168 return -ENOMEM;
3170 if (copy_from_user(buf, ubuf, cnt)) {
3171 kfree(buf);
3172 return -EFAULT;
3175 /* Cut from the first nil or newline. */
3176 buf[cnt] = '\0';
3177 end = strchr(buf, '\n');
3178 if (end)
3179 *end = '\0';
3181 cnt = mark_printk("%s\n", buf);
3182 kfree(buf);
3183 *fpos += cnt;
3185 return cnt;
3188 static const struct file_operations tracing_max_lat_fops = {
3189 .open = tracing_open_generic,
3190 .read = tracing_max_lat_read,
3191 .write = tracing_max_lat_write,
3194 static const struct file_operations tracing_ctrl_fops = {
3195 .open = tracing_open_generic,
3196 .read = tracing_ctrl_read,
3197 .write = tracing_ctrl_write,
3200 static const struct file_operations set_tracer_fops = {
3201 .open = tracing_open_generic,
3202 .read = tracing_set_trace_read,
3203 .write = tracing_set_trace_write,
3206 static const struct file_operations tracing_pipe_fops = {
3207 .open = tracing_open_pipe,
3208 .poll = tracing_poll_pipe,
3209 .read = tracing_read_pipe,
3210 .splice_read = tracing_splice_read_pipe,
3211 .release = tracing_release_pipe,
3214 static const struct file_operations tracing_entries_fops = {
3215 .open = tracing_open_generic,
3216 .read = tracing_entries_read,
3217 .write = tracing_entries_write,
3220 static const struct file_operations tracing_mark_fops = {
3221 .open = tracing_open_generic,
3222 .write = tracing_mark_write,
3225 struct ftrace_buffer_info {
3226 struct trace_array *tr;
3227 void *spare;
3228 int cpu;
3229 unsigned int read;
3232 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3234 int cpu = (int)(long)inode->i_private;
3235 struct ftrace_buffer_info *info;
3237 if (tracing_disabled)
3238 return -ENODEV;
3240 info = kzalloc(sizeof(*info), GFP_KERNEL);
3241 if (!info)
3242 return -ENOMEM;
3244 info->tr = &global_trace;
3245 info->cpu = cpu;
3246 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3247 /* Force reading ring buffer for first read */
3248 info->read = (unsigned int)-1;
3249 if (!info->spare)
3250 goto out;
3252 filp->private_data = info;
3254 return 0;
3256 out:
3257 kfree(info);
3258 return -ENOMEM;
3261 static ssize_t
3262 tracing_buffers_read(struct file *filp, char __user *ubuf,
3263 size_t count, loff_t *ppos)
3265 struct ftrace_buffer_info *info = filp->private_data;
3266 unsigned int pos;
3267 ssize_t ret;
3268 size_t size;
3270 if (!count)
3271 return 0;
3273 /* Do we have previous read data to read? */
3274 if (info->read < PAGE_SIZE)
3275 goto read;
3277 info->read = 0;
3279 ret = ring_buffer_read_page(info->tr->buffer,
3280 &info->spare,
3281 count,
3282 info->cpu, 0);
3283 if (ret < 0)
3284 return 0;
3286 pos = ring_buffer_page_len(info->spare);
3288 if (pos < PAGE_SIZE)
3289 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3291 read:
3292 size = PAGE_SIZE - info->read;
3293 if (size > count)
3294 size = count;
3296 ret = copy_to_user(ubuf, info->spare + info->read, size);
3297 if (ret == size)
3298 return -EFAULT;
3299 size -= ret;
3301 *ppos += size;
3302 info->read += size;
3304 return size;
3307 static int tracing_buffers_release(struct inode *inode, struct file *file)
3309 struct ftrace_buffer_info *info = file->private_data;
3311 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3312 kfree(info);
3314 return 0;
3317 struct buffer_ref {
3318 struct ring_buffer *buffer;
3319 void *page;
3320 int ref;
3323 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3324 struct pipe_buffer *buf)
3326 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3328 if (--ref->ref)
3329 return;
3331 ring_buffer_free_read_page(ref->buffer, ref->page);
3332 kfree(ref);
3333 buf->private = 0;
3336 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3337 struct pipe_buffer *buf)
3339 return 1;
3342 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3343 struct pipe_buffer *buf)
3345 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3347 ref->ref++;
3350 /* Pipe buffer operations for a buffer. */
3351 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3352 .can_merge = 0,
3353 .map = generic_pipe_buf_map,
3354 .unmap = generic_pipe_buf_unmap,
3355 .confirm = generic_pipe_buf_confirm,
3356 .release = buffer_pipe_buf_release,
3357 .steal = buffer_pipe_buf_steal,
3358 .get = buffer_pipe_buf_get,
3362 * Callback from splice_to_pipe(), if we need to release some pages
3363 * at the end of the spd in case we error'ed out in filling the pipe.
3365 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3367 struct buffer_ref *ref =
3368 (struct buffer_ref *)spd->partial[i].private;
3370 if (--ref->ref)
3371 return;
3373 ring_buffer_free_read_page(ref->buffer, ref->page);
3374 kfree(ref);
3375 spd->partial[i].private = 0;
3378 static ssize_t
3379 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3380 struct pipe_inode_info *pipe, size_t len,
3381 unsigned int flags)
3383 struct ftrace_buffer_info *info = file->private_data;
3384 struct partial_page partial[PIPE_BUFFERS];
3385 struct page *pages[PIPE_BUFFERS];
3386 struct splice_pipe_desc spd = {
3387 .pages = pages,
3388 .partial = partial,
3389 .flags = flags,
3390 .ops = &buffer_pipe_buf_ops,
3391 .spd_release = buffer_spd_release,
3393 struct buffer_ref *ref;
3394 int size, i;
3395 size_t ret;
3398 * We can't seek on a buffer input
3400 if (unlikely(*ppos))
3401 return -ESPIPE;
3404 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3405 struct page *page;
3406 int r;
3408 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3409 if (!ref)
3410 break;
3412 ref->buffer = info->tr->buffer;
3413 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3414 if (!ref->page) {
3415 kfree(ref);
3416 break;
3419 r = ring_buffer_read_page(ref->buffer, &ref->page,
3420 len, info->cpu, 0);
3421 if (r < 0) {
3422 ring_buffer_free_read_page(ref->buffer,
3423 ref->page);
3424 kfree(ref);
3425 break;
3429 * zero out any left over data, this is going to
3430 * user land.
3432 size = ring_buffer_page_len(ref->page);
3433 if (size < PAGE_SIZE)
3434 memset(ref->page + size, 0, PAGE_SIZE - size);
3436 page = virt_to_page(ref->page);
3438 spd.pages[i] = page;
3439 spd.partial[i].len = PAGE_SIZE;
3440 spd.partial[i].offset = 0;
3441 spd.partial[i].private = (unsigned long)ref;
3442 spd.nr_pages++;
3445 spd.nr_pages = i;
3447 /* did we read anything? */
3448 if (!spd.nr_pages) {
3449 if (flags & SPLICE_F_NONBLOCK)
3450 ret = -EAGAIN;
3451 else
3452 ret = 0;
3453 /* TODO: block */
3454 return ret;
3457 ret = splice_to_pipe(pipe, &spd);
3459 return ret;
3462 static const struct file_operations tracing_buffers_fops = {
3463 .open = tracing_buffers_open,
3464 .read = tracing_buffers_read,
3465 .release = tracing_buffers_release,
3466 .splice_read = tracing_buffers_splice_read,
3467 .llseek = no_llseek,
3470 #ifdef CONFIG_DYNAMIC_FTRACE
3472 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3474 return 0;
3477 static ssize_t
3478 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3479 size_t cnt, loff_t *ppos)
3481 static char ftrace_dyn_info_buffer[1024];
3482 static DEFINE_MUTEX(dyn_info_mutex);
3483 unsigned long *p = filp->private_data;
3484 char *buf = ftrace_dyn_info_buffer;
3485 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3486 int r;
3488 mutex_lock(&dyn_info_mutex);
3489 r = sprintf(buf, "%ld ", *p);
3491 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3492 buf[r++] = '\n';
3494 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3496 mutex_unlock(&dyn_info_mutex);
3498 return r;
3501 static const struct file_operations tracing_dyn_info_fops = {
3502 .open = tracing_open_generic,
3503 .read = tracing_read_dyn_info,
3505 #endif
3507 static struct dentry *d_tracer;
3509 struct dentry *tracing_init_dentry(void)
3511 static int once;
3513 if (d_tracer)
3514 return d_tracer;
3516 d_tracer = debugfs_create_dir("tracing", NULL);
3518 if (!d_tracer && !once) {
3519 once = 1;
3520 pr_warning("Could not create debugfs directory 'tracing'\n");
3521 return NULL;
3524 return d_tracer;
3527 static struct dentry *d_percpu;
3529 struct dentry *tracing_dentry_percpu(void)
3531 static int once;
3532 struct dentry *d_tracer;
3534 if (d_percpu)
3535 return d_percpu;
3537 d_tracer = tracing_init_dentry();
3539 if (!d_tracer)
3540 return NULL;
3542 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3544 if (!d_percpu && !once) {
3545 once = 1;
3546 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3547 return NULL;
3550 return d_percpu;
3553 static void tracing_init_debugfs_percpu(long cpu)
3555 struct dentry *d_percpu = tracing_dentry_percpu();
3556 struct dentry *entry, *d_cpu;
3557 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3558 char cpu_dir[7];
3560 if (cpu > 999 || cpu < 0)
3561 return;
3563 sprintf(cpu_dir, "cpu%ld", cpu);
3564 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3565 if (!d_cpu) {
3566 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3567 return;
3570 /* per cpu trace_pipe */
3571 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
3572 (void *) cpu, &tracing_pipe_fops);
3573 if (!entry)
3574 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3576 /* per cpu trace */
3577 entry = debugfs_create_file("trace", 0644, d_cpu,
3578 (void *) cpu, &tracing_fops);
3579 if (!entry)
3580 pr_warning("Could not create debugfs 'trace' entry\n");
3582 entry = debugfs_create_file("trace_pipe_raw", 0444, d_cpu,
3583 (void *) cpu, &tracing_buffers_fops);
3584 if (!entry)
3585 pr_warning("Could not create debugfs 'trace_pipe_raw' entry\n");
3588 #ifdef CONFIG_FTRACE_SELFTEST
3589 /* Let selftest have access to static functions in this file */
3590 #include "trace_selftest.c"
3591 #endif
3593 struct trace_option_dentry {
3594 struct tracer_opt *opt;
3595 struct tracer_flags *flags;
3596 struct dentry *entry;
3599 static ssize_t
3600 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3601 loff_t *ppos)
3603 struct trace_option_dentry *topt = filp->private_data;
3604 char *buf;
3606 if (topt->flags->val & topt->opt->bit)
3607 buf = "1\n";
3608 else
3609 buf = "0\n";
3611 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3614 static ssize_t
3615 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3616 loff_t *ppos)
3618 struct trace_option_dentry *topt = filp->private_data;
3619 unsigned long val;
3620 char buf[64];
3621 int ret;
3623 if (cnt >= sizeof(buf))
3624 return -EINVAL;
3626 if (copy_from_user(&buf, ubuf, cnt))
3627 return -EFAULT;
3629 buf[cnt] = 0;
3631 ret = strict_strtoul(buf, 10, &val);
3632 if (ret < 0)
3633 return ret;
3635 ret = 0;
3636 switch (val) {
3637 case 0:
3638 /* do nothing if already cleared */
3639 if (!(topt->flags->val & topt->opt->bit))
3640 break;
3642 mutex_lock(&trace_types_lock);
3643 if (current_trace->set_flag)
3644 ret = current_trace->set_flag(topt->flags->val,
3645 topt->opt->bit, 0);
3646 mutex_unlock(&trace_types_lock);
3647 if (ret)
3648 return ret;
3649 topt->flags->val &= ~topt->opt->bit;
3650 break;
3651 case 1:
3652 /* do nothing if already set */
3653 if (topt->flags->val & topt->opt->bit)
3654 break;
3656 mutex_lock(&trace_types_lock);
3657 if (current_trace->set_flag)
3658 ret = current_trace->set_flag(topt->flags->val,
3659 topt->opt->bit, 1);
3660 mutex_unlock(&trace_types_lock);
3661 if (ret)
3662 return ret;
3663 topt->flags->val |= topt->opt->bit;
3664 break;
3666 default:
3667 return -EINVAL;
3670 *ppos += cnt;
3672 return cnt;
3676 static const struct file_operations trace_options_fops = {
3677 .open = tracing_open_generic,
3678 .read = trace_options_read,
3679 .write = trace_options_write,
3682 static ssize_t
3683 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3684 loff_t *ppos)
3686 long index = (long)filp->private_data;
3687 char *buf;
3689 if (trace_flags & (1 << index))
3690 buf = "1\n";
3691 else
3692 buf = "0\n";
3694 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3697 static ssize_t
3698 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3699 loff_t *ppos)
3701 long index = (long)filp->private_data;
3702 char buf[64];
3703 unsigned long val;
3704 int ret;
3706 if (cnt >= sizeof(buf))
3707 return -EINVAL;
3709 if (copy_from_user(&buf, ubuf, cnt))
3710 return -EFAULT;
3712 buf[cnt] = 0;
3714 ret = strict_strtoul(buf, 10, &val);
3715 if (ret < 0)
3716 return ret;
3718 switch (val) {
3719 case 0:
3720 trace_flags &= ~(1 << index);
3721 break;
3722 case 1:
3723 trace_flags |= 1 << index;
3724 break;
3726 default:
3727 return -EINVAL;
3730 *ppos += cnt;
3732 return cnt;
3735 static const struct file_operations trace_options_core_fops = {
3736 .open = tracing_open_generic,
3737 .read = trace_options_core_read,
3738 .write = trace_options_core_write,
3741 static struct dentry *trace_options_init_dentry(void)
3743 struct dentry *d_tracer;
3744 static struct dentry *t_options;
3746 if (t_options)
3747 return t_options;
3749 d_tracer = tracing_init_dentry();
3750 if (!d_tracer)
3751 return NULL;
3753 t_options = debugfs_create_dir("options", d_tracer);
3754 if (!t_options) {
3755 pr_warning("Could not create debugfs directory 'options'\n");
3756 return NULL;
3759 return t_options;
3762 static void
3763 create_trace_option_file(struct trace_option_dentry *topt,
3764 struct tracer_flags *flags,
3765 struct tracer_opt *opt)
3767 struct dentry *t_options;
3768 struct dentry *entry;
3770 t_options = trace_options_init_dentry();
3771 if (!t_options)
3772 return;
3774 topt->flags = flags;
3775 topt->opt = opt;
3777 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3778 &trace_options_fops);
3780 topt->entry = entry;
3784 static struct trace_option_dentry *
3785 create_trace_option_files(struct tracer *tracer)
3787 struct trace_option_dentry *topts;
3788 struct tracer_flags *flags;
3789 struct tracer_opt *opts;
3790 int cnt;
3792 if (!tracer)
3793 return NULL;
3795 flags = tracer->flags;
3797 if (!flags || !flags->opts)
3798 return NULL;
3800 opts = flags->opts;
3802 for (cnt = 0; opts[cnt].name; cnt++)
3805 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3806 if (!topts)
3807 return NULL;
3809 for (cnt = 0; opts[cnt].name; cnt++)
3810 create_trace_option_file(&topts[cnt], flags,
3811 &opts[cnt]);
3813 return topts;
3816 static void
3817 destroy_trace_option_files(struct trace_option_dentry *topts)
3819 int cnt;
3821 if (!topts)
3822 return;
3824 for (cnt = 0; topts[cnt].opt; cnt++) {
3825 if (topts[cnt].entry)
3826 debugfs_remove(topts[cnt].entry);
3829 kfree(topts);
3832 static struct dentry *
3833 create_trace_option_core_file(const char *option, long index)
3835 struct dentry *t_options;
3836 struct dentry *entry;
3838 t_options = trace_options_init_dentry();
3839 if (!t_options)
3840 return NULL;
3842 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3843 &trace_options_core_fops);
3845 return entry;
3848 static __init void create_trace_options_dir(void)
3850 struct dentry *t_options;
3851 struct dentry *entry;
3852 int i;
3854 t_options = trace_options_init_dentry();
3855 if (!t_options)
3856 return;
3858 for (i = 0; trace_options[i]; i++) {
3859 entry = create_trace_option_core_file(trace_options[i], i);
3860 if (!entry)
3861 pr_warning("Could not create debugfs %s entry\n",
3862 trace_options[i]);
3866 static __init int tracer_init_debugfs(void)
3868 struct dentry *d_tracer;
3869 struct dentry *entry;
3870 int cpu;
3872 d_tracer = tracing_init_dentry();
3874 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3875 &global_trace, &tracing_ctrl_fops);
3876 if (!entry)
3877 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3879 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3880 NULL, &tracing_iter_fops);
3881 if (!entry)
3882 pr_warning("Could not create debugfs 'trace_options' entry\n");
3884 create_trace_options_dir();
3886 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3887 NULL, &tracing_cpumask_fops);
3888 if (!entry)
3889 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3891 entry = debugfs_create_file("trace", 0644, d_tracer,
3892 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3893 if (!entry)
3894 pr_warning("Could not create debugfs 'trace' entry\n");
3896 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3897 &global_trace, &show_traces_fops);
3898 if (!entry)
3899 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3901 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3902 &global_trace, &set_tracer_fops);
3903 if (!entry)
3904 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3906 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3907 &tracing_max_latency,
3908 &tracing_max_lat_fops);
3909 if (!entry)
3910 pr_warning("Could not create debugfs "
3911 "'tracing_max_latency' entry\n");
3913 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3914 &tracing_thresh, &tracing_max_lat_fops);
3915 if (!entry)
3916 pr_warning("Could not create debugfs "
3917 "'tracing_thresh' entry\n");
3918 entry = debugfs_create_file("README", 0644, d_tracer,
3919 NULL, &tracing_readme_fops);
3920 if (!entry)
3921 pr_warning("Could not create debugfs 'README' entry\n");
3923 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3924 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3925 if (!entry)
3926 pr_warning("Could not create debugfs "
3927 "'trace_pipe' entry\n");
3929 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3930 &global_trace, &tracing_entries_fops);
3931 if (!entry)
3932 pr_warning("Could not create debugfs "
3933 "'buffer_size_kb' entry\n");
3935 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3936 NULL, &tracing_mark_fops);
3937 if (!entry)
3938 pr_warning("Could not create debugfs "
3939 "'trace_marker' entry\n");
3941 #ifdef CONFIG_DYNAMIC_FTRACE
3942 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3943 &ftrace_update_tot_cnt,
3944 &tracing_dyn_info_fops);
3945 if (!entry)
3946 pr_warning("Could not create debugfs "
3947 "'dyn_ftrace_total_info' entry\n");
3948 #endif
3949 #ifdef CONFIG_SYSPROF_TRACER
3950 init_tracer_sysprof_debugfs(d_tracer);
3951 #endif
3953 for_each_tracing_cpu(cpu)
3954 tracing_init_debugfs_percpu(cpu);
3956 return 0;
3959 static int trace_panic_handler(struct notifier_block *this,
3960 unsigned long event, void *unused)
3962 if (ftrace_dump_on_oops)
3963 ftrace_dump();
3964 return NOTIFY_OK;
3967 static struct notifier_block trace_panic_notifier = {
3968 .notifier_call = trace_panic_handler,
3969 .next = NULL,
3970 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3973 static int trace_die_handler(struct notifier_block *self,
3974 unsigned long val,
3975 void *data)
3977 switch (val) {
3978 case DIE_OOPS:
3979 if (ftrace_dump_on_oops)
3980 ftrace_dump();
3981 break;
3982 default:
3983 break;
3985 return NOTIFY_OK;
3988 static struct notifier_block trace_die_notifier = {
3989 .notifier_call = trace_die_handler,
3990 .priority = 200
3994 * printk is set to max of 1024, we really don't need it that big.
3995 * Nothing should be printing 1000 characters anyway.
3997 #define TRACE_MAX_PRINT 1000
4000 * Define here KERN_TRACE so that we have one place to modify
4001 * it if we decide to change what log level the ftrace dump
4002 * should be at.
4004 #define KERN_TRACE KERN_EMERG
4006 static void
4007 trace_printk_seq(struct trace_seq *s)
4009 /* Probably should print a warning here. */
4010 if (s->len >= 1000)
4011 s->len = 1000;
4013 /* should be zero ended, but we are paranoid. */
4014 s->buffer[s->len] = 0;
4016 printk(KERN_TRACE "%s", s->buffer);
4018 trace_seq_init(s);
4021 void ftrace_dump(void)
4023 static DEFINE_SPINLOCK(ftrace_dump_lock);
4024 /* use static because iter can be a bit big for the stack */
4025 static struct trace_iterator iter;
4026 static int dump_ran;
4027 unsigned long flags;
4028 int cnt = 0, cpu;
4030 /* only one dump */
4031 spin_lock_irqsave(&ftrace_dump_lock, flags);
4032 if (dump_ran)
4033 goto out;
4035 dump_ran = 1;
4037 /* No turning back! */
4038 tracing_off();
4039 ftrace_kill();
4041 for_each_tracing_cpu(cpu) {
4042 atomic_inc(&global_trace.data[cpu]->disabled);
4045 /* don't look at user memory in panic mode */
4046 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4048 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4050 /* Simulate the iterator */
4051 iter.tr = &global_trace;
4052 iter.trace = current_trace;
4053 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4056 * We need to stop all tracing on all CPUS to read the
4057 * the next buffer. This is a bit expensive, but is
4058 * not done often. We fill all what we can read,
4059 * and then release the locks again.
4062 while (!trace_empty(&iter)) {
4064 if (!cnt)
4065 printk(KERN_TRACE "---------------------------------\n");
4067 cnt++;
4069 /* reset all but tr, trace, and overruns */
4070 memset(&iter.seq, 0,
4071 sizeof(struct trace_iterator) -
4072 offsetof(struct trace_iterator, seq));
4073 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4074 iter.pos = -1;
4076 if (find_next_entry_inc(&iter) != NULL) {
4077 print_trace_line(&iter);
4078 trace_consume(&iter);
4081 trace_printk_seq(&iter.seq);
4084 if (!cnt)
4085 printk(KERN_TRACE " (ftrace buffer empty)\n");
4086 else
4087 printk(KERN_TRACE "---------------------------------\n");
4089 out:
4090 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
4093 __init static int tracer_alloc_buffers(void)
4095 struct trace_array_cpu *data;
4096 int ring_buf_size;
4097 int i;
4098 int ret = -ENOMEM;
4100 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4101 goto out;
4103 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4104 goto out_free_buffer_mask;
4106 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4107 goto out_free_tracing_cpumask;
4109 /* To save memory, keep the ring buffer size to its minimum */
4110 if (ring_buffer_expanded)
4111 ring_buf_size = trace_buf_size;
4112 else
4113 ring_buf_size = 1;
4115 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4116 cpumask_copy(tracing_cpumask, cpu_all_mask);
4117 cpumask_clear(tracing_reader_cpumask);
4119 /* TODO: make the number of buffers hot pluggable with CPUS */
4120 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4121 TRACE_BUFFER_FLAGS);
4122 if (!global_trace.buffer) {
4123 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4124 WARN_ON(1);
4125 goto out_free_cpumask;
4127 global_trace.entries = ring_buffer_size(global_trace.buffer);
4130 #ifdef CONFIG_TRACER_MAX_TRACE
4131 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4132 TRACE_BUFFER_FLAGS);
4133 if (!max_tr.buffer) {
4134 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4135 WARN_ON(1);
4136 ring_buffer_free(global_trace.buffer);
4137 goto out_free_cpumask;
4139 max_tr.entries = ring_buffer_size(max_tr.buffer);
4140 WARN_ON(max_tr.entries != global_trace.entries);
4141 #endif
4143 /* Allocate the first page for all buffers */
4144 for_each_tracing_cpu(i) {
4145 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4146 max_tr.data[i] = &per_cpu(max_data, i);
4149 trace_init_cmdlines();
4151 register_tracer(&nop_trace);
4152 current_trace = &nop_trace;
4153 #ifdef CONFIG_BOOT_TRACER
4154 register_tracer(&boot_tracer);
4155 #endif
4156 /* All seems OK, enable tracing */
4157 tracing_disabled = 0;
4159 atomic_notifier_chain_register(&panic_notifier_list,
4160 &trace_panic_notifier);
4162 register_die_notifier(&trace_die_notifier);
4164 return 0;
4166 out_free_cpumask:
4167 free_cpumask_var(tracing_reader_cpumask);
4168 out_free_tracing_cpumask:
4169 free_cpumask_var(tracing_cpumask);
4170 out_free_buffer_mask:
4171 free_cpumask_var(tracing_buffer_mask);
4172 out:
4173 return ret;
4176 __init static int clear_boot_tracer(void)
4179 * The default tracer at boot buffer is an init section.
4180 * This function is called in lateinit. If we did not
4181 * find the boot tracer, then clear it out, to prevent
4182 * later registration from accessing the buffer that is
4183 * about to be freed.
4185 if (!default_bootup_tracer)
4186 return 0;
4188 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4189 default_bootup_tracer);
4190 default_bootup_tracer = NULL;
4192 return 0;
4195 early_initcall(tracer_alloc_buffers);
4196 fs_initcall(tracer_init_debugfs);
4197 late_initcall(clear_boot_tracer);