tracing: show that buffer size is not expanded
[linux-2.6/btrfs-unstable.git] / kernel / trace / trace.c
blob62a63b2b33dd30dce7be26b850c76efa27ff7c99
1 /*
2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/ring_buffer.h>
15 #include <linux/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/ctype.h>
34 #include <linux/init.h>
35 #include <linux/poll.h>
36 #include <linux/gfp.h>
37 #include <linux/fs.h>
39 #include "trace.h"
40 #include "trace_output.h"
42 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
44 unsigned long __read_mostly tracing_max_latency;
45 unsigned long __read_mostly tracing_thresh;
48 * On boot up, the ring buffer is set to the minimum size, so that
49 * we do not waste memory on systems that are not using tracing.
51 static int ring_buffer_expanded;
54 * We need to change this state when a selftest is running.
55 * A selftest will lurk into the ring-buffer to count the
56 * entries inserted during the selftest although some concurrent
57 * insertions into the ring-buffer such as trace_printk could occurred
58 * at the same time, giving false positive or negative results.
60 static bool __read_mostly tracing_selftest_running;
63 * If a tracer is running, we do not want to run SELFTEST.
65 static bool __read_mostly tracing_selftest_disabled;
67 /* For tracers that don't implement custom flags */
68 static struct tracer_opt dummy_tracer_opt[] = {
69 { }
72 static struct tracer_flags dummy_tracer_flags = {
73 .val = 0,
74 .opts = dummy_tracer_opt
77 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
79 return 0;
83 * Kill all tracing for good (never come back).
84 * It is initialized to 1 but will turn to zero if the initialization
85 * of the tracer is successful. But that is the only place that sets
86 * this back to zero.
88 static int tracing_disabled = 1;
90 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
92 static inline void ftrace_disable_cpu(void)
94 preempt_disable();
95 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
98 static inline void ftrace_enable_cpu(void)
100 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
101 preempt_enable();
104 static cpumask_var_t __read_mostly tracing_buffer_mask;
106 /* Define which cpu buffers are currently read in trace_pipe */
107 static cpumask_var_t tracing_reader_cpumask;
109 #define for_each_tracing_cpu(cpu) \
110 for_each_cpu(cpu, tracing_buffer_mask)
113 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
115 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
116 * is set, then ftrace_dump is called. This will output the contents
117 * of the ftrace buffers to the console. This is very useful for
118 * capturing traces that lead to crashes and outputing it to a
119 * serial console.
121 * It is default off, but you can enable it with either specifying
122 * "ftrace_dump_on_oops" in the kernel command line, or setting
123 * /proc/sys/kernel/ftrace_dump_on_oops to true.
125 int ftrace_dump_on_oops;
127 static int tracing_set_tracer(const char *buf);
129 #define BOOTUP_TRACER_SIZE 100
130 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
131 static char *default_bootup_tracer;
133 static int __init set_ftrace(char *str)
135 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
136 default_bootup_tracer = bootup_tracer_buf;
137 /* We are using ftrace early, expand it */
138 ring_buffer_expanded = 1;
139 return 1;
141 __setup("ftrace=", set_ftrace);
143 static int __init set_ftrace_dump_on_oops(char *str)
145 ftrace_dump_on_oops = 1;
146 return 1;
148 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
150 long
151 ns2usecs(cycle_t nsec)
153 nsec += 500;
154 do_div(nsec, 1000);
155 return nsec;
158 cycle_t ftrace_now(int cpu)
160 u64 ts = ring_buffer_time_stamp(cpu);
161 ring_buffer_normalize_time_stamp(cpu, &ts);
162 return ts;
166 * The global_trace is the descriptor that holds the tracing
167 * buffers for the live tracing. For each CPU, it contains
168 * a link list of pages that will store trace entries. The
169 * page descriptor of the pages in the memory is used to hold
170 * the link list by linking the lru item in the page descriptor
171 * to each of the pages in the buffer per CPU.
173 * For each active CPU there is a data field that holds the
174 * pages for the buffer for that CPU. Each CPU has the same number
175 * of pages allocated for its buffer.
177 static struct trace_array global_trace;
179 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
182 * The max_tr is used to snapshot the global_trace when a maximum
183 * latency is reached. Some tracers will use this to store a maximum
184 * trace while it continues examining live traces.
186 * The buffers for the max_tr are set up the same as the global_trace.
187 * When a snapshot is taken, the link list of the max_tr is swapped
188 * with the link list of the global_trace and the buffers are reset for
189 * the global_trace so the tracing can continue.
191 static struct trace_array max_tr;
193 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
195 /* tracer_enabled is used to toggle activation of a tracer */
196 static int tracer_enabled = 1;
199 * tracing_is_enabled - return tracer_enabled status
201 * This function is used by other tracers to know the status
202 * of the tracer_enabled flag. Tracers may use this function
203 * to know if it should enable their features when starting
204 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
206 int tracing_is_enabled(void)
208 return tracer_enabled;
212 * trace_buf_size is the size in bytes that is allocated
213 * for a buffer. Note, the number of bytes is always rounded
214 * to page size.
216 * This number is purposely set to a low number of 16384.
217 * If the dump on oops happens, it will be much appreciated
218 * to not have to wait for all that output. Anyway this can be
219 * boot time and run time configurable.
221 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
223 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
225 /* trace_types holds a link list of available tracers. */
226 static struct tracer *trace_types __read_mostly;
228 /* current_trace points to the tracer that is currently active */
229 static struct tracer *current_trace __read_mostly;
232 * max_tracer_type_len is used to simplify the allocating of
233 * buffers to read userspace tracer names. We keep track of
234 * the longest tracer name registered.
236 static int max_tracer_type_len;
239 * trace_types_lock is used to protect the trace_types list.
240 * This lock is also used to keep user access serialized.
241 * Accesses from userspace will grab this lock while userspace
242 * activities happen inside the kernel.
244 static DEFINE_MUTEX(trace_types_lock);
246 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
247 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
249 /* trace_flags holds trace_options default values */
250 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
251 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
254 * trace_wake_up - wake up tasks waiting for trace input
256 * Simply wakes up any task that is blocked on the trace_wait
257 * queue. These is used with trace_poll for tasks polling the trace.
259 void trace_wake_up(void)
262 * The runqueue_is_locked() can fail, but this is the best we
263 * have for now:
265 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
266 wake_up(&trace_wait);
269 static int __init set_buf_size(char *str)
271 unsigned long buf_size;
272 int ret;
274 if (!str)
275 return 0;
276 ret = strict_strtoul(str, 0, &buf_size);
277 /* nr_entries can not be zero */
278 if (ret < 0 || buf_size == 0)
279 return 0;
280 trace_buf_size = buf_size;
281 return 1;
283 __setup("trace_buf_size=", set_buf_size);
285 unsigned long nsecs_to_usecs(unsigned long nsecs)
287 return nsecs / 1000;
290 /* These must match the bit postions in trace_iterator_flags */
291 static const char *trace_options[] = {
292 "print-parent",
293 "sym-offset",
294 "sym-addr",
295 "verbose",
296 "raw",
297 "hex",
298 "bin",
299 "block",
300 "stacktrace",
301 "sched-tree",
302 "trace_printk",
303 "ftrace_preempt",
304 "branch",
305 "annotate",
306 "userstacktrace",
307 "sym-userobj",
308 "printk-msg-only",
309 "context-info",
310 "latency-format",
311 NULL
315 * ftrace_max_lock is used to protect the swapping of buffers
316 * when taking a max snapshot. The buffers themselves are
317 * protected by per_cpu spinlocks. But the action of the swap
318 * needs its own lock.
320 * This is defined as a raw_spinlock_t in order to help
321 * with performance when lockdep debugging is enabled.
323 static raw_spinlock_t ftrace_max_lock =
324 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
327 * Copy the new maximum trace into the separate maximum-trace
328 * structure. (this way the maximum trace is permanently saved,
329 * for later retrieval via /debugfs/tracing/latency_trace)
331 static void
332 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
334 struct trace_array_cpu *data = tr->data[cpu];
336 max_tr.cpu = cpu;
337 max_tr.time_start = data->preempt_timestamp;
339 data = max_tr.data[cpu];
340 data->saved_latency = tracing_max_latency;
342 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
343 data->pid = tsk->pid;
344 data->uid = task_uid(tsk);
345 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
346 data->policy = tsk->policy;
347 data->rt_priority = tsk->rt_priority;
349 /* record this tasks comm */
350 tracing_record_cmdline(tsk);
353 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
355 int len;
356 int ret;
358 if (!cnt)
359 return 0;
361 if (s->len <= s->readpos)
362 return -EBUSY;
364 len = s->len - s->readpos;
365 if (cnt > len)
366 cnt = len;
367 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
368 if (ret == cnt)
369 return -EFAULT;
371 cnt -= ret;
373 s->readpos += cnt;
374 return cnt;
377 ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
379 int len;
380 void *ret;
382 if (s->len <= s->readpos)
383 return -EBUSY;
385 len = s->len - s->readpos;
386 if (cnt > len)
387 cnt = len;
388 ret = memcpy(buf, s->buffer + s->readpos, cnt);
389 if (!ret)
390 return -EFAULT;
392 s->readpos += cnt;
393 return cnt;
396 static void
397 trace_print_seq(struct seq_file *m, struct trace_seq *s)
399 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
401 s->buffer[len] = 0;
402 seq_puts(m, s->buffer);
404 trace_seq_init(s);
408 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
409 * @tr: tracer
410 * @tsk: the task with the latency
411 * @cpu: The cpu that initiated the trace.
413 * Flip the buffers between the @tr and the max_tr and record information
414 * about which task was the cause of this latency.
416 void
417 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
419 struct ring_buffer *buf = tr->buffer;
421 WARN_ON_ONCE(!irqs_disabled());
422 __raw_spin_lock(&ftrace_max_lock);
424 tr->buffer = max_tr.buffer;
425 max_tr.buffer = buf;
427 ftrace_disable_cpu();
428 ring_buffer_reset(tr->buffer);
429 ftrace_enable_cpu();
431 __update_max_tr(tr, tsk, cpu);
432 __raw_spin_unlock(&ftrace_max_lock);
436 * update_max_tr_single - only copy one trace over, and reset the rest
437 * @tr - tracer
438 * @tsk - task with the latency
439 * @cpu - the cpu of the buffer to copy.
441 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
443 void
444 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
446 int ret;
448 WARN_ON_ONCE(!irqs_disabled());
449 __raw_spin_lock(&ftrace_max_lock);
451 ftrace_disable_cpu();
453 ring_buffer_reset(max_tr.buffer);
454 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
456 ftrace_enable_cpu();
458 WARN_ON_ONCE(ret && ret != -EAGAIN);
460 __update_max_tr(tr, tsk, cpu);
461 __raw_spin_unlock(&ftrace_max_lock);
465 * register_tracer - register a tracer with the ftrace system.
466 * @type - the plugin for the tracer
468 * Register a new plugin tracer.
470 int register_tracer(struct tracer *type)
471 __releases(kernel_lock)
472 __acquires(kernel_lock)
474 struct tracer *t;
475 int len;
476 int ret = 0;
478 if (!type->name) {
479 pr_info("Tracer must have a name\n");
480 return -1;
484 * When this gets called we hold the BKL which means that
485 * preemption is disabled. Various trace selftests however
486 * need to disable and enable preemption for successful tests.
487 * So we drop the BKL here and grab it after the tests again.
489 unlock_kernel();
490 mutex_lock(&trace_types_lock);
492 tracing_selftest_running = true;
494 for (t = trace_types; t; t = t->next) {
495 if (strcmp(type->name, t->name) == 0) {
496 /* already found */
497 pr_info("Trace %s already registered\n",
498 type->name);
499 ret = -1;
500 goto out;
504 if (!type->set_flag)
505 type->set_flag = &dummy_set_flag;
506 if (!type->flags)
507 type->flags = &dummy_tracer_flags;
508 else
509 if (!type->flags->opts)
510 type->flags->opts = dummy_tracer_opt;
511 if (!type->wait_pipe)
512 type->wait_pipe = default_wait_pipe;
515 #ifdef CONFIG_FTRACE_STARTUP_TEST
516 if (type->selftest && !tracing_selftest_disabled) {
517 struct tracer *saved_tracer = current_trace;
518 struct trace_array *tr = &global_trace;
519 int i;
522 * Run a selftest on this tracer.
523 * Here we reset the trace buffer, and set the current
524 * tracer to be this tracer. The tracer can then run some
525 * internal tracing to verify that everything is in order.
526 * If we fail, we do not register this tracer.
528 for_each_tracing_cpu(i)
529 tracing_reset(tr, i);
531 current_trace = type;
532 /* the test is responsible for initializing and enabling */
533 pr_info("Testing tracer %s: ", type->name);
534 ret = type->selftest(type, tr);
535 /* the test is responsible for resetting too */
536 current_trace = saved_tracer;
537 if (ret) {
538 printk(KERN_CONT "FAILED!\n");
539 goto out;
541 /* Only reset on passing, to avoid touching corrupted buffers */
542 for_each_tracing_cpu(i)
543 tracing_reset(tr, i);
545 printk(KERN_CONT "PASSED\n");
547 #endif
549 type->next = trace_types;
550 trace_types = type;
551 len = strlen(type->name);
552 if (len > max_tracer_type_len)
553 max_tracer_type_len = len;
555 out:
556 tracing_selftest_running = false;
557 mutex_unlock(&trace_types_lock);
559 if (ret || !default_bootup_tracer)
560 goto out_unlock;
562 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
563 goto out_unlock;
565 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
566 /* Do we want this tracer to start on bootup? */
567 tracing_set_tracer(type->name);
568 default_bootup_tracer = NULL;
569 /* disable other selftests, since this will break it. */
570 tracing_selftest_disabled = 1;
571 #ifdef CONFIG_FTRACE_STARTUP_TEST
572 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
573 type->name);
574 #endif
576 out_unlock:
577 lock_kernel();
578 return ret;
581 void unregister_tracer(struct tracer *type)
583 struct tracer **t;
584 int len;
586 mutex_lock(&trace_types_lock);
587 for (t = &trace_types; *t; t = &(*t)->next) {
588 if (*t == type)
589 goto found;
591 pr_info("Trace %s not registered\n", type->name);
592 goto out;
594 found:
595 *t = (*t)->next;
597 if (type == current_trace && tracer_enabled) {
598 tracer_enabled = 0;
599 tracing_stop();
600 if (current_trace->stop)
601 current_trace->stop(&global_trace);
602 current_trace = &nop_trace;
605 if (strlen(type->name) != max_tracer_type_len)
606 goto out;
608 max_tracer_type_len = 0;
609 for (t = &trace_types; *t; t = &(*t)->next) {
610 len = strlen((*t)->name);
611 if (len > max_tracer_type_len)
612 max_tracer_type_len = len;
614 out:
615 mutex_unlock(&trace_types_lock);
618 void tracing_reset(struct trace_array *tr, int cpu)
620 ftrace_disable_cpu();
621 ring_buffer_reset_cpu(tr->buffer, cpu);
622 ftrace_enable_cpu();
625 void tracing_reset_online_cpus(struct trace_array *tr)
627 int cpu;
629 tr->time_start = ftrace_now(tr->cpu);
631 for_each_online_cpu(cpu)
632 tracing_reset(tr, cpu);
635 #define SAVED_CMDLINES 128
636 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
637 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
638 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
639 static int cmdline_idx;
640 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
642 /* temporary disable recording */
643 static atomic_t trace_record_cmdline_disabled __read_mostly;
645 static void trace_init_cmdlines(void)
647 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
648 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
649 cmdline_idx = 0;
652 static int trace_stop_count;
653 static DEFINE_SPINLOCK(tracing_start_lock);
656 * ftrace_off_permanent - disable all ftrace code permanently
658 * This should only be called when a serious anomally has
659 * been detected. This will turn off the function tracing,
660 * ring buffers, and other tracing utilites. It takes no
661 * locks and can be called from any context.
663 void ftrace_off_permanent(void)
665 tracing_disabled = 1;
666 ftrace_stop();
667 tracing_off_permanent();
671 * tracing_start - quick start of the tracer
673 * If tracing is enabled but was stopped by tracing_stop,
674 * this will start the tracer back up.
676 void tracing_start(void)
678 struct ring_buffer *buffer;
679 unsigned long flags;
681 if (tracing_disabled)
682 return;
684 spin_lock_irqsave(&tracing_start_lock, flags);
685 if (--trace_stop_count) {
686 if (trace_stop_count < 0) {
687 /* Someone screwed up their debugging */
688 WARN_ON_ONCE(1);
689 trace_stop_count = 0;
691 goto out;
695 buffer = global_trace.buffer;
696 if (buffer)
697 ring_buffer_record_enable(buffer);
699 buffer = max_tr.buffer;
700 if (buffer)
701 ring_buffer_record_enable(buffer);
703 ftrace_start();
704 out:
705 spin_unlock_irqrestore(&tracing_start_lock, flags);
709 * tracing_stop - quick stop of the tracer
711 * Light weight way to stop tracing. Use in conjunction with
712 * tracing_start.
714 void tracing_stop(void)
716 struct ring_buffer *buffer;
717 unsigned long flags;
719 ftrace_stop();
720 spin_lock_irqsave(&tracing_start_lock, flags);
721 if (trace_stop_count++)
722 goto out;
724 buffer = global_trace.buffer;
725 if (buffer)
726 ring_buffer_record_disable(buffer);
728 buffer = max_tr.buffer;
729 if (buffer)
730 ring_buffer_record_disable(buffer);
732 out:
733 spin_unlock_irqrestore(&tracing_start_lock, flags);
736 void trace_stop_cmdline_recording(void);
738 static void trace_save_cmdline(struct task_struct *tsk)
740 unsigned map;
741 unsigned idx;
743 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
744 return;
747 * It's not the end of the world if we don't get
748 * the lock, but we also don't want to spin
749 * nor do we want to disable interrupts,
750 * so if we miss here, then better luck next time.
752 if (!__raw_spin_trylock(&trace_cmdline_lock))
753 return;
755 idx = map_pid_to_cmdline[tsk->pid];
756 if (idx >= SAVED_CMDLINES) {
757 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
759 map = map_cmdline_to_pid[idx];
760 if (map <= PID_MAX_DEFAULT)
761 map_pid_to_cmdline[map] = (unsigned)-1;
763 map_pid_to_cmdline[tsk->pid] = idx;
765 cmdline_idx = idx;
768 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
770 __raw_spin_unlock(&trace_cmdline_lock);
773 char *trace_find_cmdline(int pid)
775 char *cmdline = "<...>";
776 unsigned map;
778 if (!pid)
779 return "<idle>";
781 if (pid > PID_MAX_DEFAULT)
782 goto out;
784 map = map_pid_to_cmdline[pid];
785 if (map >= SAVED_CMDLINES)
786 goto out;
788 cmdline = saved_cmdlines[map];
790 out:
791 return cmdline;
794 void tracing_record_cmdline(struct task_struct *tsk)
796 if (atomic_read(&trace_record_cmdline_disabled))
797 return;
799 trace_save_cmdline(tsk);
802 void
803 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
804 int pc)
806 struct task_struct *tsk = current;
808 entry->preempt_count = pc & 0xff;
809 entry->pid = (tsk) ? tsk->pid : 0;
810 entry->tgid = (tsk) ? tsk->tgid : 0;
811 entry->flags =
812 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
813 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
814 #else
815 TRACE_FLAG_IRQS_NOSUPPORT |
816 #endif
817 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
818 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
819 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
822 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
823 unsigned char type,
824 unsigned long len,
825 unsigned long flags, int pc)
827 struct ring_buffer_event *event;
829 event = ring_buffer_lock_reserve(tr->buffer, len);
830 if (event != NULL) {
831 struct trace_entry *ent = ring_buffer_event_data(event);
833 tracing_generic_entry_update(ent, flags, pc);
834 ent->type = type;
837 return event;
839 static void ftrace_trace_stack(struct trace_array *tr,
840 unsigned long flags, int skip, int pc);
841 static void ftrace_trace_userstack(struct trace_array *tr,
842 unsigned long flags, int pc);
844 void trace_buffer_unlock_commit(struct trace_array *tr,
845 struct ring_buffer_event *event,
846 unsigned long flags, int pc)
848 ring_buffer_unlock_commit(tr->buffer, event);
850 ftrace_trace_stack(tr, flags, 6, pc);
851 ftrace_trace_userstack(tr, flags, pc);
852 trace_wake_up();
855 struct ring_buffer_event *
856 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
857 unsigned long flags, int pc)
859 return trace_buffer_lock_reserve(&global_trace,
860 type, len, flags, pc);
863 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
864 unsigned long flags, int pc)
866 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
869 void
870 trace_function(struct trace_array *tr,
871 unsigned long ip, unsigned long parent_ip, unsigned long flags,
872 int pc)
874 struct ring_buffer_event *event;
875 struct ftrace_entry *entry;
877 /* If we are reading the ring buffer, don't trace */
878 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
879 return;
881 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
882 flags, pc);
883 if (!event)
884 return;
885 entry = ring_buffer_event_data(event);
886 entry->ip = ip;
887 entry->parent_ip = parent_ip;
888 ring_buffer_unlock_commit(tr->buffer, event);
891 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
892 static void __trace_graph_entry(struct trace_array *tr,
893 struct ftrace_graph_ent *trace,
894 unsigned long flags,
895 int pc)
897 struct ring_buffer_event *event;
898 struct ftrace_graph_ent_entry *entry;
900 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
901 return;
903 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
904 sizeof(*entry), flags, pc);
905 if (!event)
906 return;
907 entry = ring_buffer_event_data(event);
908 entry->graph_ent = *trace;
909 ring_buffer_unlock_commit(global_trace.buffer, event);
912 static void __trace_graph_return(struct trace_array *tr,
913 struct ftrace_graph_ret *trace,
914 unsigned long flags,
915 int pc)
917 struct ring_buffer_event *event;
918 struct ftrace_graph_ret_entry *entry;
920 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
921 return;
923 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
924 sizeof(*entry), flags, pc);
925 if (!event)
926 return;
927 entry = ring_buffer_event_data(event);
928 entry->ret = *trace;
929 ring_buffer_unlock_commit(global_trace.buffer, event);
931 #endif
933 void
934 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
935 unsigned long ip, unsigned long parent_ip, unsigned long flags,
936 int pc)
938 if (likely(!atomic_read(&data->disabled)))
939 trace_function(tr, ip, parent_ip, flags, pc);
942 static void __ftrace_trace_stack(struct trace_array *tr,
943 unsigned long flags,
944 int skip, int pc)
946 #ifdef CONFIG_STACKTRACE
947 struct ring_buffer_event *event;
948 struct stack_entry *entry;
949 struct stack_trace trace;
951 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
952 sizeof(*entry), flags, pc);
953 if (!event)
954 return;
955 entry = ring_buffer_event_data(event);
956 memset(&entry->caller, 0, sizeof(entry->caller));
958 trace.nr_entries = 0;
959 trace.max_entries = FTRACE_STACK_ENTRIES;
960 trace.skip = skip;
961 trace.entries = entry->caller;
963 save_stack_trace(&trace);
964 ring_buffer_unlock_commit(tr->buffer, event);
965 #endif
968 static void ftrace_trace_stack(struct trace_array *tr,
969 unsigned long flags,
970 int skip, int pc)
972 if (!(trace_flags & TRACE_ITER_STACKTRACE))
973 return;
975 __ftrace_trace_stack(tr, flags, skip, pc);
978 void __trace_stack(struct trace_array *tr,
979 unsigned long flags,
980 int skip, int pc)
982 __ftrace_trace_stack(tr, flags, skip, pc);
985 static void ftrace_trace_userstack(struct trace_array *tr,
986 unsigned long flags, int pc)
988 #ifdef CONFIG_STACKTRACE
989 struct ring_buffer_event *event;
990 struct userstack_entry *entry;
991 struct stack_trace trace;
993 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
994 return;
996 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
997 sizeof(*entry), flags, pc);
998 if (!event)
999 return;
1000 entry = ring_buffer_event_data(event);
1002 memset(&entry->caller, 0, sizeof(entry->caller));
1004 trace.nr_entries = 0;
1005 trace.max_entries = FTRACE_STACK_ENTRIES;
1006 trace.skip = 0;
1007 trace.entries = entry->caller;
1009 save_stack_trace_user(&trace);
1010 ring_buffer_unlock_commit(tr->buffer, event);
1011 #endif
1014 #ifdef UNUSED
1015 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1017 ftrace_trace_userstack(tr, flags, preempt_count());
1019 #endif /* UNUSED */
1021 static void
1022 ftrace_trace_special(void *__tr,
1023 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1024 int pc)
1026 struct ring_buffer_event *event;
1027 struct trace_array *tr = __tr;
1028 struct special_entry *entry;
1030 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1031 sizeof(*entry), 0, pc);
1032 if (!event)
1033 return;
1034 entry = ring_buffer_event_data(event);
1035 entry->arg1 = arg1;
1036 entry->arg2 = arg2;
1037 entry->arg3 = arg3;
1038 trace_buffer_unlock_commit(tr, event, 0, pc);
1041 void
1042 __trace_special(void *__tr, void *__data,
1043 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1045 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1048 void
1049 tracing_sched_switch_trace(struct trace_array *tr,
1050 struct task_struct *prev,
1051 struct task_struct *next,
1052 unsigned long flags, int pc)
1054 struct ring_buffer_event *event;
1055 struct ctx_switch_entry *entry;
1057 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1058 sizeof(*entry), flags, pc);
1059 if (!event)
1060 return;
1061 entry = ring_buffer_event_data(event);
1062 entry->prev_pid = prev->pid;
1063 entry->prev_prio = prev->prio;
1064 entry->prev_state = prev->state;
1065 entry->next_pid = next->pid;
1066 entry->next_prio = next->prio;
1067 entry->next_state = next->state;
1068 entry->next_cpu = task_cpu(next);
1069 trace_buffer_unlock_commit(tr, event, flags, pc);
1072 void
1073 tracing_sched_wakeup_trace(struct trace_array *tr,
1074 struct task_struct *wakee,
1075 struct task_struct *curr,
1076 unsigned long flags, int pc)
1078 struct ring_buffer_event *event;
1079 struct ctx_switch_entry *entry;
1081 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1082 sizeof(*entry), flags, pc);
1083 if (!event)
1084 return;
1085 entry = ring_buffer_event_data(event);
1086 entry->prev_pid = curr->pid;
1087 entry->prev_prio = curr->prio;
1088 entry->prev_state = curr->state;
1089 entry->next_pid = wakee->pid;
1090 entry->next_prio = wakee->prio;
1091 entry->next_state = wakee->state;
1092 entry->next_cpu = task_cpu(wakee);
1094 ring_buffer_unlock_commit(tr->buffer, event);
1095 ftrace_trace_stack(tr, flags, 6, pc);
1096 ftrace_trace_userstack(tr, flags, pc);
1099 void
1100 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1102 struct trace_array *tr = &global_trace;
1103 struct trace_array_cpu *data;
1104 unsigned long flags;
1105 int cpu;
1106 int pc;
1108 if (tracing_disabled)
1109 return;
1111 pc = preempt_count();
1112 local_irq_save(flags);
1113 cpu = raw_smp_processor_id();
1114 data = tr->data[cpu];
1116 if (likely(atomic_inc_return(&data->disabled) == 1))
1117 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1119 atomic_dec(&data->disabled);
1120 local_irq_restore(flags);
1123 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1124 int trace_graph_entry(struct ftrace_graph_ent *trace)
1126 struct trace_array *tr = &global_trace;
1127 struct trace_array_cpu *data;
1128 unsigned long flags;
1129 long disabled;
1130 int cpu;
1131 int pc;
1133 if (!ftrace_trace_task(current))
1134 return 0;
1136 if (!ftrace_graph_addr(trace->func))
1137 return 0;
1139 local_irq_save(flags);
1140 cpu = raw_smp_processor_id();
1141 data = tr->data[cpu];
1142 disabled = atomic_inc_return(&data->disabled);
1143 if (likely(disabled == 1)) {
1144 pc = preempt_count();
1145 __trace_graph_entry(tr, trace, flags, pc);
1147 /* Only do the atomic if it is not already set */
1148 if (!test_tsk_trace_graph(current))
1149 set_tsk_trace_graph(current);
1150 atomic_dec(&data->disabled);
1151 local_irq_restore(flags);
1153 return 1;
1156 void trace_graph_return(struct ftrace_graph_ret *trace)
1158 struct trace_array *tr = &global_trace;
1159 struct trace_array_cpu *data;
1160 unsigned long flags;
1161 long disabled;
1162 int cpu;
1163 int pc;
1165 local_irq_save(flags);
1166 cpu = raw_smp_processor_id();
1167 data = tr->data[cpu];
1168 disabled = atomic_inc_return(&data->disabled);
1169 if (likely(disabled == 1)) {
1170 pc = preempt_count();
1171 __trace_graph_return(tr, trace, flags, pc);
1173 if (!trace->depth)
1174 clear_tsk_trace_graph(current);
1175 atomic_dec(&data->disabled);
1176 local_irq_restore(flags);
1178 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1182 * trace_vprintk - write binary msg to tracing buffer
1185 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1187 static raw_spinlock_t trace_buf_lock =
1188 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1189 static u32 trace_buf[TRACE_BUF_SIZE];
1191 struct ring_buffer_event *event;
1192 struct trace_array *tr = &global_trace;
1193 struct trace_array_cpu *data;
1194 struct print_entry *entry;
1195 unsigned long flags;
1196 int resched;
1197 int cpu, len = 0, size, pc;
1199 if (unlikely(tracing_selftest_running || tracing_disabled))
1200 return 0;
1202 /* Don't pollute graph traces with trace_vprintk internals */
1203 pause_graph_tracing();
1205 pc = preempt_count();
1206 resched = ftrace_preempt_disable();
1207 cpu = raw_smp_processor_id();
1208 data = tr->data[cpu];
1210 if (unlikely(atomic_read(&data->disabled)))
1211 goto out;
1213 /* Lockdep uses trace_printk for lock tracing */
1214 local_irq_save(flags);
1215 __raw_spin_lock(&trace_buf_lock);
1216 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1218 if (len > TRACE_BUF_SIZE || len < 0)
1219 goto out_unlock;
1221 size = sizeof(*entry) + sizeof(u32) * len;
1222 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, flags, pc);
1223 if (!event)
1224 goto out_unlock;
1225 entry = ring_buffer_event_data(event);
1226 entry->ip = ip;
1227 entry->depth = depth;
1228 entry->fmt = fmt;
1230 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1231 ring_buffer_unlock_commit(tr->buffer, event);
1233 out_unlock:
1234 __raw_spin_unlock(&trace_buf_lock);
1235 local_irq_restore(flags);
1237 out:
1238 ftrace_preempt_enable(resched);
1239 unpause_graph_tracing();
1241 return len;
1243 EXPORT_SYMBOL_GPL(trace_vprintk);
1245 enum trace_file_type {
1246 TRACE_FILE_LAT_FMT = 1,
1247 TRACE_FILE_ANNOTATE = 2,
1250 static void trace_iterator_increment(struct trace_iterator *iter)
1252 /* Don't allow ftrace to trace into the ring buffers */
1253 ftrace_disable_cpu();
1255 iter->idx++;
1256 if (iter->buffer_iter[iter->cpu])
1257 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1259 ftrace_enable_cpu();
1262 static struct trace_entry *
1263 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1265 struct ring_buffer_event *event;
1266 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1268 /* Don't allow ftrace to trace into the ring buffers */
1269 ftrace_disable_cpu();
1271 if (buf_iter)
1272 event = ring_buffer_iter_peek(buf_iter, ts);
1273 else
1274 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1276 ftrace_enable_cpu();
1278 return event ? ring_buffer_event_data(event) : NULL;
1281 static struct trace_entry *
1282 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1284 struct ring_buffer *buffer = iter->tr->buffer;
1285 struct trace_entry *ent, *next = NULL;
1286 int cpu_file = iter->cpu_file;
1287 u64 next_ts = 0, ts;
1288 int next_cpu = -1;
1289 int cpu;
1292 * If we are in a per_cpu trace file, don't bother by iterating over
1293 * all cpu and peek directly.
1295 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1296 if (ring_buffer_empty_cpu(buffer, cpu_file))
1297 return NULL;
1298 ent = peek_next_entry(iter, cpu_file, ent_ts);
1299 if (ent_cpu)
1300 *ent_cpu = cpu_file;
1302 return ent;
1305 for_each_tracing_cpu(cpu) {
1307 if (ring_buffer_empty_cpu(buffer, cpu))
1308 continue;
1310 ent = peek_next_entry(iter, cpu, &ts);
1313 * Pick the entry with the smallest timestamp:
1315 if (ent && (!next || ts < next_ts)) {
1316 next = ent;
1317 next_cpu = cpu;
1318 next_ts = ts;
1322 if (ent_cpu)
1323 *ent_cpu = next_cpu;
1325 if (ent_ts)
1326 *ent_ts = next_ts;
1328 return next;
1331 /* Find the next real entry, without updating the iterator itself */
1332 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1333 int *ent_cpu, u64 *ent_ts)
1335 return __find_next_entry(iter, ent_cpu, ent_ts);
1338 /* Find the next real entry, and increment the iterator to the next entry */
1339 static void *find_next_entry_inc(struct trace_iterator *iter)
1341 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1343 if (iter->ent)
1344 trace_iterator_increment(iter);
1346 return iter->ent ? iter : NULL;
1349 static void trace_consume(struct trace_iterator *iter)
1351 /* Don't allow ftrace to trace into the ring buffers */
1352 ftrace_disable_cpu();
1353 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1354 ftrace_enable_cpu();
1357 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1359 struct trace_iterator *iter = m->private;
1360 int i = (int)*pos;
1361 void *ent;
1363 (*pos)++;
1365 /* can't go backwards */
1366 if (iter->idx > i)
1367 return NULL;
1369 if (iter->idx < 0)
1370 ent = find_next_entry_inc(iter);
1371 else
1372 ent = iter;
1374 while (ent && iter->idx < i)
1375 ent = find_next_entry_inc(iter);
1377 iter->pos = *pos;
1379 return ent;
1383 * No necessary locking here. The worst thing which can
1384 * happen is loosing events consumed at the same time
1385 * by a trace_pipe reader.
1386 * Other than that, we don't risk to crash the ring buffer
1387 * because it serializes the readers.
1389 * The current tracer is copied to avoid a global locking
1390 * all around.
1392 static void *s_start(struct seq_file *m, loff_t *pos)
1394 struct trace_iterator *iter = m->private;
1395 static struct tracer *old_tracer;
1396 int cpu_file = iter->cpu_file;
1397 void *p = NULL;
1398 loff_t l = 0;
1399 int cpu;
1401 /* copy the tracer to avoid using a global lock all around */
1402 mutex_lock(&trace_types_lock);
1403 if (unlikely(old_tracer != current_trace && current_trace)) {
1404 old_tracer = current_trace;
1405 *iter->trace = *current_trace;
1407 mutex_unlock(&trace_types_lock);
1409 atomic_inc(&trace_record_cmdline_disabled);
1411 if (*pos != iter->pos) {
1412 iter->ent = NULL;
1413 iter->cpu = 0;
1414 iter->idx = -1;
1416 ftrace_disable_cpu();
1418 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1419 for_each_tracing_cpu(cpu)
1420 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1421 } else
1422 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1425 ftrace_enable_cpu();
1427 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1430 } else {
1431 l = *pos - 1;
1432 p = s_next(m, p, &l);
1435 return p;
1438 static void s_stop(struct seq_file *m, void *p)
1440 atomic_dec(&trace_record_cmdline_disabled);
1443 static void print_lat_help_header(struct seq_file *m)
1445 seq_puts(m, "# _------=> CPU# \n");
1446 seq_puts(m, "# / _-----=> irqs-off \n");
1447 seq_puts(m, "# | / _----=> need-resched \n");
1448 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1449 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1450 seq_puts(m, "# |||| / \n");
1451 seq_puts(m, "# ||||| delay \n");
1452 seq_puts(m, "# cmd pid ||||| time | caller \n");
1453 seq_puts(m, "# \\ / ||||| \\ | / \n");
1456 static void print_func_help_header(struct seq_file *m)
1458 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1459 seq_puts(m, "# | | | | |\n");
1463 static void
1464 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1466 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1467 struct trace_array *tr = iter->tr;
1468 struct trace_array_cpu *data = tr->data[tr->cpu];
1469 struct tracer *type = current_trace;
1470 unsigned long total;
1471 unsigned long entries;
1472 const char *name = "preemption";
1474 if (type)
1475 name = type->name;
1477 entries = ring_buffer_entries(iter->tr->buffer);
1478 total = entries +
1479 ring_buffer_overruns(iter->tr->buffer);
1481 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1482 name, UTS_RELEASE);
1483 seq_puts(m, "# -----------------------------------"
1484 "---------------------------------\n");
1485 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1486 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1487 nsecs_to_usecs(data->saved_latency),
1488 entries,
1489 total,
1490 tr->cpu,
1491 #if defined(CONFIG_PREEMPT_NONE)
1492 "server",
1493 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1494 "desktop",
1495 #elif defined(CONFIG_PREEMPT)
1496 "preempt",
1497 #else
1498 "unknown",
1499 #endif
1500 /* These are reserved for later use */
1501 0, 0, 0, 0);
1502 #ifdef CONFIG_SMP
1503 seq_printf(m, " #P:%d)\n", num_online_cpus());
1504 #else
1505 seq_puts(m, ")\n");
1506 #endif
1507 seq_puts(m, "# -----------------\n");
1508 seq_printf(m, "# | task: %.16s-%d "
1509 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1510 data->comm, data->pid, data->uid, data->nice,
1511 data->policy, data->rt_priority);
1512 seq_puts(m, "# -----------------\n");
1514 if (data->critical_start) {
1515 seq_puts(m, "# => started at: ");
1516 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1517 trace_print_seq(m, &iter->seq);
1518 seq_puts(m, "\n# => ended at: ");
1519 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1520 trace_print_seq(m, &iter->seq);
1521 seq_puts(m, "#\n");
1524 seq_puts(m, "#\n");
1527 static void test_cpu_buff_start(struct trace_iterator *iter)
1529 struct trace_seq *s = &iter->seq;
1531 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1532 return;
1534 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1535 return;
1537 if (cpumask_test_cpu(iter->cpu, iter->started))
1538 return;
1540 cpumask_set_cpu(iter->cpu, iter->started);
1541 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1544 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1546 struct trace_seq *s = &iter->seq;
1547 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1548 struct trace_entry *entry;
1549 struct trace_event *event;
1551 entry = iter->ent;
1553 test_cpu_buff_start(iter);
1555 event = ftrace_find_event(entry->type);
1557 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1558 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1559 if (!trace_print_lat_context(iter))
1560 goto partial;
1561 } else {
1562 if (!trace_print_context(iter))
1563 goto partial;
1567 if (event)
1568 return event->trace(iter, sym_flags);
1570 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1571 goto partial;
1573 return TRACE_TYPE_HANDLED;
1574 partial:
1575 return TRACE_TYPE_PARTIAL_LINE;
1578 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1580 struct trace_seq *s = &iter->seq;
1581 struct trace_entry *entry;
1582 struct trace_event *event;
1584 entry = iter->ent;
1586 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1587 if (!trace_seq_printf(s, "%d %d %llu ",
1588 entry->pid, iter->cpu, iter->ts))
1589 goto partial;
1592 event = ftrace_find_event(entry->type);
1593 if (event)
1594 return event->raw(iter, 0);
1596 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1597 goto partial;
1599 return TRACE_TYPE_HANDLED;
1600 partial:
1601 return TRACE_TYPE_PARTIAL_LINE;
1604 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1606 struct trace_seq *s = &iter->seq;
1607 unsigned char newline = '\n';
1608 struct trace_entry *entry;
1609 struct trace_event *event;
1611 entry = iter->ent;
1613 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1614 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1615 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1616 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1619 event = ftrace_find_event(entry->type);
1620 if (event) {
1621 enum print_line_t ret = event->hex(iter, 0);
1622 if (ret != TRACE_TYPE_HANDLED)
1623 return ret;
1626 SEQ_PUT_FIELD_RET(s, newline);
1628 return TRACE_TYPE_HANDLED;
1631 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1633 struct trace_seq *s = &iter->seq;
1634 struct trace_entry *entry = iter->ent;
1635 struct print_entry *field;
1636 int ret;
1638 trace_assign_type(field, entry);
1640 ret = trace_seq_bprintf(s, field->fmt, field->buf);
1641 if (!ret)
1642 return TRACE_TYPE_PARTIAL_LINE;
1644 return TRACE_TYPE_HANDLED;
1647 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1649 struct trace_seq *s = &iter->seq;
1650 struct trace_entry *entry;
1651 struct trace_event *event;
1653 entry = iter->ent;
1655 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1656 SEQ_PUT_FIELD_RET(s, entry->pid);
1657 SEQ_PUT_FIELD_RET(s, iter->cpu);
1658 SEQ_PUT_FIELD_RET(s, iter->ts);
1661 event = ftrace_find_event(entry->type);
1662 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1665 static int trace_empty(struct trace_iterator *iter)
1667 int cpu;
1669 /* If we are looking at one CPU buffer, only check that one */
1670 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1671 cpu = iter->cpu_file;
1672 if (iter->buffer_iter[cpu]) {
1673 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1674 return 0;
1675 } else {
1676 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1677 return 0;
1679 return 1;
1682 for_each_tracing_cpu(cpu) {
1683 if (iter->buffer_iter[cpu]) {
1684 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1685 return 0;
1686 } else {
1687 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1688 return 0;
1692 return 1;
1695 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1697 enum print_line_t ret;
1699 if (iter->trace && iter->trace->print_line) {
1700 ret = iter->trace->print_line(iter);
1701 if (ret != TRACE_TYPE_UNHANDLED)
1702 return ret;
1705 if (iter->ent->type == TRACE_PRINT &&
1706 trace_flags & TRACE_ITER_PRINTK &&
1707 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1708 return print_printk_msg_only(iter);
1710 if (trace_flags & TRACE_ITER_BIN)
1711 return print_bin_fmt(iter);
1713 if (trace_flags & TRACE_ITER_HEX)
1714 return print_hex_fmt(iter);
1716 if (trace_flags & TRACE_ITER_RAW)
1717 return print_raw_fmt(iter);
1719 return print_trace_fmt(iter);
1722 static int s_show(struct seq_file *m, void *v)
1724 struct trace_iterator *iter = v;
1726 if (iter->ent == NULL) {
1727 if (iter->tr) {
1728 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1729 seq_puts(m, "#\n");
1731 if (iter->trace && iter->trace->print_header)
1732 iter->trace->print_header(m);
1733 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1734 /* print nothing if the buffers are empty */
1735 if (trace_empty(iter))
1736 return 0;
1737 print_trace_header(m, iter);
1738 if (!(trace_flags & TRACE_ITER_VERBOSE))
1739 print_lat_help_header(m);
1740 } else {
1741 if (!(trace_flags & TRACE_ITER_VERBOSE))
1742 print_func_help_header(m);
1744 } else {
1745 print_trace_line(iter);
1746 trace_print_seq(m, &iter->seq);
1749 return 0;
1752 static struct seq_operations tracer_seq_ops = {
1753 .start = s_start,
1754 .next = s_next,
1755 .stop = s_stop,
1756 .show = s_show,
1759 static struct trace_iterator *
1760 __tracing_open(struct inode *inode, struct file *file)
1762 long cpu_file = (long) inode->i_private;
1763 void *fail_ret = ERR_PTR(-ENOMEM);
1764 struct trace_iterator *iter;
1765 struct seq_file *m;
1766 int cpu, ret;
1768 if (tracing_disabled)
1769 return ERR_PTR(-ENODEV);
1771 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1772 if (!iter)
1773 return ERR_PTR(-ENOMEM);
1776 * We make a copy of the current tracer to avoid concurrent
1777 * changes on it while we are reading.
1779 mutex_lock(&trace_types_lock);
1780 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1781 if (!iter->trace)
1782 goto fail;
1784 if (current_trace)
1785 *iter->trace = *current_trace;
1787 if (current_trace && current_trace->print_max)
1788 iter->tr = &max_tr;
1789 else
1790 iter->tr = &global_trace;
1791 iter->pos = -1;
1792 mutex_init(&iter->mutex);
1793 iter->cpu_file = cpu_file;
1795 /* Notify the tracer early; before we stop tracing. */
1796 if (iter->trace && iter->trace->open)
1797 iter->trace->open(iter);
1799 /* Annotate start of buffers if we had overruns */
1800 if (ring_buffer_overruns(iter->tr->buffer))
1801 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1803 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1804 for_each_tracing_cpu(cpu) {
1806 iter->buffer_iter[cpu] =
1807 ring_buffer_read_start(iter->tr->buffer, cpu);
1809 } else {
1810 cpu = iter->cpu_file;
1811 iter->buffer_iter[cpu] =
1812 ring_buffer_read_start(iter->tr->buffer, cpu);
1815 /* TODO stop tracer */
1816 ret = seq_open(file, &tracer_seq_ops);
1817 if (ret < 0) {
1818 fail_ret = ERR_PTR(ret);
1819 goto fail_buffer;
1822 m = file->private_data;
1823 m->private = iter;
1825 /* stop the trace while dumping */
1826 tracing_stop();
1828 mutex_unlock(&trace_types_lock);
1830 return iter;
1832 fail_buffer:
1833 for_each_tracing_cpu(cpu) {
1834 if (iter->buffer_iter[cpu])
1835 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1837 fail:
1838 mutex_unlock(&trace_types_lock);
1839 kfree(iter->trace);
1840 kfree(iter);
1842 return fail_ret;
1845 int tracing_open_generic(struct inode *inode, struct file *filp)
1847 if (tracing_disabled)
1848 return -ENODEV;
1850 filp->private_data = inode->i_private;
1851 return 0;
1854 static int tracing_release(struct inode *inode, struct file *file)
1856 struct seq_file *m = (struct seq_file *)file->private_data;
1857 struct trace_iterator *iter = m->private;
1858 int cpu;
1860 mutex_lock(&trace_types_lock);
1861 for_each_tracing_cpu(cpu) {
1862 if (iter->buffer_iter[cpu])
1863 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1866 if (iter->trace && iter->trace->close)
1867 iter->trace->close(iter);
1869 /* reenable tracing if it was previously enabled */
1870 tracing_start();
1871 mutex_unlock(&trace_types_lock);
1873 seq_release(inode, file);
1874 mutex_destroy(&iter->mutex);
1875 kfree(iter->trace);
1876 kfree(iter);
1877 return 0;
1880 static int tracing_open(struct inode *inode, struct file *file)
1882 struct trace_iterator *iter;
1883 int ret = 0;
1885 iter = __tracing_open(inode, file);
1886 if (IS_ERR(iter))
1887 ret = PTR_ERR(iter);
1888 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1889 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1891 return ret;
1894 static void *
1895 t_next(struct seq_file *m, void *v, loff_t *pos)
1897 struct tracer *t = m->private;
1899 (*pos)++;
1901 if (t)
1902 t = t->next;
1904 m->private = t;
1906 return t;
1909 static void *t_start(struct seq_file *m, loff_t *pos)
1911 struct tracer *t = m->private;
1912 loff_t l = 0;
1914 mutex_lock(&trace_types_lock);
1915 for (; t && l < *pos; t = t_next(m, t, &l))
1918 return t;
1921 static void t_stop(struct seq_file *m, void *p)
1923 mutex_unlock(&trace_types_lock);
1926 static int t_show(struct seq_file *m, void *v)
1928 struct tracer *t = v;
1930 if (!t)
1931 return 0;
1933 seq_printf(m, "%s", t->name);
1934 if (t->next)
1935 seq_putc(m, ' ');
1936 else
1937 seq_putc(m, '\n');
1939 return 0;
1942 static struct seq_operations show_traces_seq_ops = {
1943 .start = t_start,
1944 .next = t_next,
1945 .stop = t_stop,
1946 .show = t_show,
1949 static int show_traces_open(struct inode *inode, struct file *file)
1951 int ret;
1953 if (tracing_disabled)
1954 return -ENODEV;
1956 ret = seq_open(file, &show_traces_seq_ops);
1957 if (!ret) {
1958 struct seq_file *m = file->private_data;
1959 m->private = trace_types;
1962 return ret;
1965 static const struct file_operations tracing_fops = {
1966 .open = tracing_open,
1967 .read = seq_read,
1968 .llseek = seq_lseek,
1969 .release = tracing_release,
1972 static const struct file_operations show_traces_fops = {
1973 .open = show_traces_open,
1974 .read = seq_read,
1975 .release = seq_release,
1979 * Only trace on a CPU if the bitmask is set:
1981 static cpumask_var_t tracing_cpumask;
1984 * The tracer itself will not take this lock, but still we want
1985 * to provide a consistent cpumask to user-space:
1987 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1990 * Temporary storage for the character representation of the
1991 * CPU bitmask (and one more byte for the newline):
1993 static char mask_str[NR_CPUS + 1];
1995 static ssize_t
1996 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1997 size_t count, loff_t *ppos)
1999 int len;
2001 mutex_lock(&tracing_cpumask_update_lock);
2003 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2004 if (count - len < 2) {
2005 count = -EINVAL;
2006 goto out_err;
2008 len += sprintf(mask_str + len, "\n");
2009 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2011 out_err:
2012 mutex_unlock(&tracing_cpumask_update_lock);
2014 return count;
2017 static ssize_t
2018 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2019 size_t count, loff_t *ppos)
2021 int err, cpu;
2022 cpumask_var_t tracing_cpumask_new;
2024 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2025 return -ENOMEM;
2027 mutex_lock(&tracing_cpumask_update_lock);
2028 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2029 if (err)
2030 goto err_unlock;
2032 local_irq_disable();
2033 __raw_spin_lock(&ftrace_max_lock);
2034 for_each_tracing_cpu(cpu) {
2036 * Increase/decrease the disabled counter if we are
2037 * about to flip a bit in the cpumask:
2039 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2040 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2041 atomic_inc(&global_trace.data[cpu]->disabled);
2043 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2044 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2045 atomic_dec(&global_trace.data[cpu]->disabled);
2048 __raw_spin_unlock(&ftrace_max_lock);
2049 local_irq_enable();
2051 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2053 mutex_unlock(&tracing_cpumask_update_lock);
2054 free_cpumask_var(tracing_cpumask_new);
2056 return count;
2058 err_unlock:
2059 mutex_unlock(&tracing_cpumask_update_lock);
2060 free_cpumask_var(tracing_cpumask);
2062 return err;
2065 static const struct file_operations tracing_cpumask_fops = {
2066 .open = tracing_open_generic,
2067 .read = tracing_cpumask_read,
2068 .write = tracing_cpumask_write,
2071 static ssize_t
2072 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2073 size_t cnt, loff_t *ppos)
2075 struct tracer_opt *trace_opts;
2076 u32 tracer_flags;
2077 int len = 0;
2078 char *buf;
2079 int r = 0;
2080 int i;
2083 /* calculate max size */
2084 for (i = 0; trace_options[i]; i++) {
2085 len += strlen(trace_options[i]);
2086 len += 3; /* "no" and newline */
2089 mutex_lock(&trace_types_lock);
2090 tracer_flags = current_trace->flags->val;
2091 trace_opts = current_trace->flags->opts;
2094 * Increase the size with names of options specific
2095 * of the current tracer.
2097 for (i = 0; trace_opts[i].name; i++) {
2098 len += strlen(trace_opts[i].name);
2099 len += 3; /* "no" and newline */
2102 /* +2 for \n and \0 */
2103 buf = kmalloc(len + 2, GFP_KERNEL);
2104 if (!buf) {
2105 mutex_unlock(&trace_types_lock);
2106 return -ENOMEM;
2109 for (i = 0; trace_options[i]; i++) {
2110 if (trace_flags & (1 << i))
2111 r += sprintf(buf + r, "%s\n", trace_options[i]);
2112 else
2113 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2116 for (i = 0; trace_opts[i].name; i++) {
2117 if (tracer_flags & trace_opts[i].bit)
2118 r += sprintf(buf + r, "%s\n",
2119 trace_opts[i].name);
2120 else
2121 r += sprintf(buf + r, "no%s\n",
2122 trace_opts[i].name);
2124 mutex_unlock(&trace_types_lock);
2126 WARN_ON(r >= len + 2);
2128 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2130 kfree(buf);
2131 return r;
2134 /* Try to assign a tracer specific option */
2135 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2137 struct tracer_flags *trace_flags = trace->flags;
2138 struct tracer_opt *opts = NULL;
2139 int ret = 0, i = 0;
2140 int len;
2142 for (i = 0; trace_flags->opts[i].name; i++) {
2143 opts = &trace_flags->opts[i];
2144 len = strlen(opts->name);
2146 if (strncmp(cmp, opts->name, len) == 0) {
2147 ret = trace->set_flag(trace_flags->val,
2148 opts->bit, !neg);
2149 break;
2152 /* Not found */
2153 if (!trace_flags->opts[i].name)
2154 return -EINVAL;
2156 /* Refused to handle */
2157 if (ret)
2158 return ret;
2160 if (neg)
2161 trace_flags->val &= ~opts->bit;
2162 else
2163 trace_flags->val |= opts->bit;
2165 return 0;
2168 static ssize_t
2169 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2170 size_t cnt, loff_t *ppos)
2172 char buf[64];
2173 char *cmp = buf;
2174 int neg = 0;
2175 int ret;
2176 int i;
2178 if (cnt >= sizeof(buf))
2179 return -EINVAL;
2181 if (copy_from_user(&buf, ubuf, cnt))
2182 return -EFAULT;
2184 buf[cnt] = 0;
2186 if (strncmp(buf, "no", 2) == 0) {
2187 neg = 1;
2188 cmp += 2;
2191 for (i = 0; trace_options[i]; i++) {
2192 int len = strlen(trace_options[i]);
2194 if (strncmp(cmp, trace_options[i], len) == 0) {
2195 if (neg)
2196 trace_flags &= ~(1 << i);
2197 else
2198 trace_flags |= (1 << i);
2199 break;
2203 /* If no option could be set, test the specific tracer options */
2204 if (!trace_options[i]) {
2205 mutex_lock(&trace_types_lock);
2206 ret = set_tracer_option(current_trace, cmp, neg);
2207 mutex_unlock(&trace_types_lock);
2208 if (ret)
2209 return ret;
2212 filp->f_pos += cnt;
2214 return cnt;
2217 static const struct file_operations tracing_iter_fops = {
2218 .open = tracing_open_generic,
2219 .read = tracing_trace_options_read,
2220 .write = tracing_trace_options_write,
2223 static const char readme_msg[] =
2224 "tracing mini-HOWTO:\n\n"
2225 "# mkdir /debug\n"
2226 "# mount -t debugfs nodev /debug\n\n"
2227 "# cat /debug/tracing/available_tracers\n"
2228 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2229 "# cat /debug/tracing/current_tracer\n"
2230 "none\n"
2231 "# echo sched_switch > /debug/tracing/current_tracer\n"
2232 "# cat /debug/tracing/current_tracer\n"
2233 "sched_switch\n"
2234 "# cat /debug/tracing/trace_options\n"
2235 "noprint-parent nosym-offset nosym-addr noverbose\n"
2236 "# echo print-parent > /debug/tracing/trace_options\n"
2237 "# echo 1 > /debug/tracing/tracing_enabled\n"
2238 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2239 "echo 0 > /debug/tracing/tracing_enabled\n"
2242 static ssize_t
2243 tracing_readme_read(struct file *filp, char __user *ubuf,
2244 size_t cnt, loff_t *ppos)
2246 return simple_read_from_buffer(ubuf, cnt, ppos,
2247 readme_msg, strlen(readme_msg));
2250 static const struct file_operations tracing_readme_fops = {
2251 .open = tracing_open_generic,
2252 .read = tracing_readme_read,
2255 static ssize_t
2256 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2257 size_t cnt, loff_t *ppos)
2259 char buf[64];
2260 int r;
2262 r = sprintf(buf, "%u\n", tracer_enabled);
2263 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2266 static ssize_t
2267 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2268 size_t cnt, loff_t *ppos)
2270 struct trace_array *tr = filp->private_data;
2271 char buf[64];
2272 unsigned long val;
2273 int ret;
2275 if (cnt >= sizeof(buf))
2276 return -EINVAL;
2278 if (copy_from_user(&buf, ubuf, cnt))
2279 return -EFAULT;
2281 buf[cnt] = 0;
2283 ret = strict_strtoul(buf, 10, &val);
2284 if (ret < 0)
2285 return ret;
2287 val = !!val;
2289 mutex_lock(&trace_types_lock);
2290 if (tracer_enabled ^ val) {
2291 if (val) {
2292 tracer_enabled = 1;
2293 if (current_trace->start)
2294 current_trace->start(tr);
2295 tracing_start();
2296 } else {
2297 tracer_enabled = 0;
2298 tracing_stop();
2299 if (current_trace->stop)
2300 current_trace->stop(tr);
2303 mutex_unlock(&trace_types_lock);
2305 filp->f_pos += cnt;
2307 return cnt;
2310 static ssize_t
2311 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2312 size_t cnt, loff_t *ppos)
2314 char buf[max_tracer_type_len+2];
2315 int r;
2317 mutex_lock(&trace_types_lock);
2318 if (current_trace)
2319 r = sprintf(buf, "%s\n", current_trace->name);
2320 else
2321 r = sprintf(buf, "\n");
2322 mutex_unlock(&trace_types_lock);
2324 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2327 int tracer_init(struct tracer *t, struct trace_array *tr)
2329 tracing_reset_online_cpus(tr);
2330 return t->init(tr);
2333 static int tracing_resize_ring_buffer(unsigned long size)
2335 int ret;
2338 * If kernel or user changes the size of the ring buffer
2339 * we use the size that was given, and we can forget about
2340 * expanding it later.
2342 ring_buffer_expanded = 1;
2344 ret = ring_buffer_resize(global_trace.buffer, size);
2345 if (ret < 0)
2346 return ret;
2348 ret = ring_buffer_resize(max_tr.buffer, size);
2349 if (ret < 0) {
2350 int r;
2352 r = ring_buffer_resize(global_trace.buffer,
2353 global_trace.entries);
2354 if (r < 0) {
2356 * AARGH! We are left with different
2357 * size max buffer!!!!
2358 * The max buffer is our "snapshot" buffer.
2359 * When a tracer needs a snapshot (one of the
2360 * latency tracers), it swaps the max buffer
2361 * with the saved snap shot. We succeeded to
2362 * update the size of the main buffer, but failed to
2363 * update the size of the max buffer. But when we tried
2364 * to reset the main buffer to the original size, we
2365 * failed there too. This is very unlikely to
2366 * happen, but if it does, warn and kill all
2367 * tracing.
2369 WARN_ON(1);
2370 tracing_disabled = 1;
2372 return ret;
2375 global_trace.entries = size;
2377 return ret;
2381 * tracing_update_buffers - used by tracing facility to expand ring buffers
2383 * To save on memory when the tracing is never used on a system with it
2384 * configured in. The ring buffers are set to a minimum size. But once
2385 * a user starts to use the tracing facility, then they need to grow
2386 * to their default size.
2388 * This function is to be called when a tracer is about to be used.
2390 int tracing_update_buffers(void)
2392 int ret = 0;
2394 mutex_lock(&trace_types_lock);
2395 if (!ring_buffer_expanded)
2396 ret = tracing_resize_ring_buffer(trace_buf_size);
2397 mutex_unlock(&trace_types_lock);
2399 return ret;
2402 struct trace_option_dentry;
2404 static struct trace_option_dentry *
2405 create_trace_option_files(struct tracer *tracer);
2407 static void
2408 destroy_trace_option_files(struct trace_option_dentry *topts);
2410 static int tracing_set_tracer(const char *buf)
2412 static struct trace_option_dentry *topts;
2413 struct trace_array *tr = &global_trace;
2414 struct tracer *t;
2415 int ret = 0;
2417 mutex_lock(&trace_types_lock);
2419 if (!ring_buffer_expanded) {
2420 ret = tracing_resize_ring_buffer(trace_buf_size);
2421 if (ret < 0)
2422 return ret;
2423 ret = 0;
2426 for (t = trace_types; t; t = t->next) {
2427 if (strcmp(t->name, buf) == 0)
2428 break;
2430 if (!t) {
2431 ret = -EINVAL;
2432 goto out;
2434 if (t == current_trace)
2435 goto out;
2437 trace_branch_disable();
2438 if (current_trace && current_trace->reset)
2439 current_trace->reset(tr);
2441 destroy_trace_option_files(topts);
2443 current_trace = t;
2445 topts = create_trace_option_files(current_trace);
2447 if (t->init) {
2448 ret = tracer_init(t, tr);
2449 if (ret)
2450 goto out;
2453 trace_branch_enable(tr);
2454 out:
2455 mutex_unlock(&trace_types_lock);
2457 return ret;
2460 static ssize_t
2461 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2462 size_t cnt, loff_t *ppos)
2464 char buf[max_tracer_type_len+1];
2465 int i;
2466 size_t ret;
2467 int err;
2469 ret = cnt;
2471 if (cnt > max_tracer_type_len)
2472 cnt = max_tracer_type_len;
2474 if (copy_from_user(&buf, ubuf, cnt))
2475 return -EFAULT;
2477 buf[cnt] = 0;
2479 /* strip ending whitespace. */
2480 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2481 buf[i] = 0;
2483 err = tracing_set_tracer(buf);
2484 if (err)
2485 return err;
2487 filp->f_pos += ret;
2489 return ret;
2492 static ssize_t
2493 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2494 size_t cnt, loff_t *ppos)
2496 unsigned long *ptr = filp->private_data;
2497 char buf[64];
2498 int r;
2500 r = snprintf(buf, sizeof(buf), "%ld\n",
2501 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2502 if (r > sizeof(buf))
2503 r = sizeof(buf);
2504 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2507 static ssize_t
2508 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2509 size_t cnt, loff_t *ppos)
2511 unsigned long *ptr = filp->private_data;
2512 char buf[64];
2513 unsigned long val;
2514 int ret;
2516 if (cnt >= sizeof(buf))
2517 return -EINVAL;
2519 if (copy_from_user(&buf, ubuf, cnt))
2520 return -EFAULT;
2522 buf[cnt] = 0;
2524 ret = strict_strtoul(buf, 10, &val);
2525 if (ret < 0)
2526 return ret;
2528 *ptr = val * 1000;
2530 return cnt;
2533 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2535 long cpu_file = (long) inode->i_private;
2536 struct trace_iterator *iter;
2537 int ret = 0;
2539 if (tracing_disabled)
2540 return -ENODEV;
2542 mutex_lock(&trace_types_lock);
2544 /* We only allow one reader per cpu */
2545 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2546 if (!cpumask_empty(tracing_reader_cpumask)) {
2547 ret = -EBUSY;
2548 goto out;
2550 cpumask_setall(tracing_reader_cpumask);
2551 } else {
2552 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2553 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2554 else {
2555 ret = -EBUSY;
2556 goto out;
2560 /* create a buffer to store the information to pass to userspace */
2561 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2562 if (!iter) {
2563 ret = -ENOMEM;
2564 goto out;
2568 * We make a copy of the current tracer to avoid concurrent
2569 * changes on it while we are reading.
2571 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2572 if (!iter->trace) {
2573 ret = -ENOMEM;
2574 goto fail;
2576 if (current_trace)
2577 *iter->trace = *current_trace;
2579 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2580 ret = -ENOMEM;
2581 goto fail;
2584 /* trace pipe does not show start of buffer */
2585 cpumask_setall(iter->started);
2587 iter->cpu_file = cpu_file;
2588 iter->tr = &global_trace;
2589 mutex_init(&iter->mutex);
2590 filp->private_data = iter;
2592 if (iter->trace->pipe_open)
2593 iter->trace->pipe_open(iter);
2595 out:
2596 mutex_unlock(&trace_types_lock);
2597 return ret;
2599 fail:
2600 kfree(iter->trace);
2601 kfree(iter);
2602 mutex_unlock(&trace_types_lock);
2603 return ret;
2606 static int tracing_release_pipe(struct inode *inode, struct file *file)
2608 struct trace_iterator *iter = file->private_data;
2610 mutex_lock(&trace_types_lock);
2612 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2613 cpumask_clear(tracing_reader_cpumask);
2614 else
2615 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2617 mutex_unlock(&trace_types_lock);
2619 free_cpumask_var(iter->started);
2620 mutex_destroy(&iter->mutex);
2621 kfree(iter->trace);
2622 kfree(iter);
2624 return 0;
2627 static unsigned int
2628 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2630 struct trace_iterator *iter = filp->private_data;
2632 if (trace_flags & TRACE_ITER_BLOCK) {
2634 * Always select as readable when in blocking mode
2636 return POLLIN | POLLRDNORM;
2637 } else {
2638 if (!trace_empty(iter))
2639 return POLLIN | POLLRDNORM;
2640 poll_wait(filp, &trace_wait, poll_table);
2641 if (!trace_empty(iter))
2642 return POLLIN | POLLRDNORM;
2644 return 0;
2649 void default_wait_pipe(struct trace_iterator *iter)
2651 DEFINE_WAIT(wait);
2653 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2655 if (trace_empty(iter))
2656 schedule();
2658 finish_wait(&trace_wait, &wait);
2662 * This is a make-shift waitqueue.
2663 * A tracer might use this callback on some rare cases:
2665 * 1) the current tracer might hold the runqueue lock when it wakes up
2666 * a reader, hence a deadlock (sched, function, and function graph tracers)
2667 * 2) the function tracers, trace all functions, we don't want
2668 * the overhead of calling wake_up and friends
2669 * (and tracing them too)
2671 * Anyway, this is really very primitive wakeup.
2673 void poll_wait_pipe(struct trace_iterator *iter)
2675 set_current_state(TASK_INTERRUPTIBLE);
2676 /* sleep for 100 msecs, and try again. */
2677 schedule_timeout(HZ / 10);
2680 /* Must be called with trace_types_lock mutex held. */
2681 static int tracing_wait_pipe(struct file *filp)
2683 struct trace_iterator *iter = filp->private_data;
2685 while (trace_empty(iter)) {
2687 if ((filp->f_flags & O_NONBLOCK)) {
2688 return -EAGAIN;
2691 mutex_unlock(&iter->mutex);
2693 iter->trace->wait_pipe(iter);
2695 mutex_lock(&iter->mutex);
2697 if (signal_pending(current))
2698 return -EINTR;
2701 * We block until we read something and tracing is disabled.
2702 * We still block if tracing is disabled, but we have never
2703 * read anything. This allows a user to cat this file, and
2704 * then enable tracing. But after we have read something,
2705 * we give an EOF when tracing is again disabled.
2707 * iter->pos will be 0 if we haven't read anything.
2709 if (!tracer_enabled && iter->pos)
2710 break;
2713 return 1;
2717 * Consumer reader.
2719 static ssize_t
2720 tracing_read_pipe(struct file *filp, char __user *ubuf,
2721 size_t cnt, loff_t *ppos)
2723 struct trace_iterator *iter = filp->private_data;
2724 static struct tracer *old_tracer;
2725 ssize_t sret;
2727 /* return any leftover data */
2728 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2729 if (sret != -EBUSY)
2730 return sret;
2732 trace_seq_init(&iter->seq);
2734 /* copy the tracer to avoid using a global lock all around */
2735 mutex_lock(&trace_types_lock);
2736 if (unlikely(old_tracer != current_trace && current_trace)) {
2737 old_tracer = current_trace;
2738 *iter->trace = *current_trace;
2740 mutex_unlock(&trace_types_lock);
2743 * Avoid more than one consumer on a single file descriptor
2744 * This is just a matter of traces coherency, the ring buffer itself
2745 * is protected.
2747 mutex_lock(&iter->mutex);
2748 if (iter->trace->read) {
2749 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2750 if (sret)
2751 goto out;
2754 waitagain:
2755 sret = tracing_wait_pipe(filp);
2756 if (sret <= 0)
2757 goto out;
2759 /* stop when tracing is finished */
2760 if (trace_empty(iter)) {
2761 sret = 0;
2762 goto out;
2765 if (cnt >= PAGE_SIZE)
2766 cnt = PAGE_SIZE - 1;
2768 /* reset all but tr, trace, and overruns */
2769 memset(&iter->seq, 0,
2770 sizeof(struct trace_iterator) -
2771 offsetof(struct trace_iterator, seq));
2772 iter->pos = -1;
2774 while (find_next_entry_inc(iter) != NULL) {
2775 enum print_line_t ret;
2776 int len = iter->seq.len;
2778 ret = print_trace_line(iter);
2779 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2780 /* don't print partial lines */
2781 iter->seq.len = len;
2782 break;
2784 if (ret != TRACE_TYPE_NO_CONSUME)
2785 trace_consume(iter);
2787 if (iter->seq.len >= cnt)
2788 break;
2791 /* Now copy what we have to the user */
2792 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2793 if (iter->seq.readpos >= iter->seq.len)
2794 trace_seq_init(&iter->seq);
2797 * If there was nothing to send to user, inspite of consuming trace
2798 * entries, go back to wait for more entries.
2800 if (sret == -EBUSY)
2801 goto waitagain;
2803 out:
2804 mutex_unlock(&iter->mutex);
2806 return sret;
2809 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2810 struct pipe_buffer *buf)
2812 __free_page(buf->page);
2815 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2816 unsigned int idx)
2818 __free_page(spd->pages[idx]);
2821 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2822 .can_merge = 0,
2823 .map = generic_pipe_buf_map,
2824 .unmap = generic_pipe_buf_unmap,
2825 .confirm = generic_pipe_buf_confirm,
2826 .release = tracing_pipe_buf_release,
2827 .steal = generic_pipe_buf_steal,
2828 .get = generic_pipe_buf_get,
2831 static size_t
2832 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2834 size_t count;
2835 int ret;
2837 /* Seq buffer is page-sized, exactly what we need. */
2838 for (;;) {
2839 count = iter->seq.len;
2840 ret = print_trace_line(iter);
2841 count = iter->seq.len - count;
2842 if (rem < count) {
2843 rem = 0;
2844 iter->seq.len -= count;
2845 break;
2847 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2848 iter->seq.len -= count;
2849 break;
2852 trace_consume(iter);
2853 rem -= count;
2854 if (!find_next_entry_inc(iter)) {
2855 rem = 0;
2856 iter->ent = NULL;
2857 break;
2861 return rem;
2864 static ssize_t tracing_splice_read_pipe(struct file *filp,
2865 loff_t *ppos,
2866 struct pipe_inode_info *pipe,
2867 size_t len,
2868 unsigned int flags)
2870 struct page *pages[PIPE_BUFFERS];
2871 struct partial_page partial[PIPE_BUFFERS];
2872 struct trace_iterator *iter = filp->private_data;
2873 struct splice_pipe_desc spd = {
2874 .pages = pages,
2875 .partial = partial,
2876 .nr_pages = 0, /* This gets updated below. */
2877 .flags = flags,
2878 .ops = &tracing_pipe_buf_ops,
2879 .spd_release = tracing_spd_release_pipe,
2881 static struct tracer *old_tracer;
2882 ssize_t ret;
2883 size_t rem;
2884 unsigned int i;
2886 /* copy the tracer to avoid using a global lock all around */
2887 mutex_lock(&trace_types_lock);
2888 if (unlikely(old_tracer != current_trace && current_trace)) {
2889 old_tracer = current_trace;
2890 *iter->trace = *current_trace;
2892 mutex_unlock(&trace_types_lock);
2894 mutex_lock(&iter->mutex);
2896 if (iter->trace->splice_read) {
2897 ret = iter->trace->splice_read(iter, filp,
2898 ppos, pipe, len, flags);
2899 if (ret)
2900 goto out_err;
2903 ret = tracing_wait_pipe(filp);
2904 if (ret <= 0)
2905 goto out_err;
2907 if (!iter->ent && !find_next_entry_inc(iter)) {
2908 ret = -EFAULT;
2909 goto out_err;
2912 /* Fill as many pages as possible. */
2913 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2914 pages[i] = alloc_page(GFP_KERNEL);
2915 if (!pages[i])
2916 break;
2918 rem = tracing_fill_pipe_page(rem, iter);
2920 /* Copy the data into the page, so we can start over. */
2921 ret = trace_seq_to_buffer(&iter->seq,
2922 page_address(pages[i]),
2923 iter->seq.len);
2924 if (ret < 0) {
2925 __free_page(pages[i]);
2926 break;
2928 partial[i].offset = 0;
2929 partial[i].len = iter->seq.len;
2931 trace_seq_init(&iter->seq);
2934 mutex_unlock(&iter->mutex);
2936 spd.nr_pages = i;
2938 return splice_to_pipe(pipe, &spd);
2940 out_err:
2941 mutex_unlock(&iter->mutex);
2943 return ret;
2946 static ssize_t
2947 tracing_entries_read(struct file *filp, char __user *ubuf,
2948 size_t cnt, loff_t *ppos)
2950 struct trace_array *tr = filp->private_data;
2951 char buf[96];
2952 int r;
2954 mutex_lock(&trace_types_lock);
2955 if (!ring_buffer_expanded)
2956 r = sprintf(buf, "%lu (expanded: %lu)\n",
2957 tr->entries >> 10,
2958 trace_buf_size >> 10);
2959 else
2960 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2961 mutex_unlock(&trace_types_lock);
2963 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2966 static ssize_t
2967 tracing_entries_write(struct file *filp, const char __user *ubuf,
2968 size_t cnt, loff_t *ppos)
2970 unsigned long val;
2971 char buf[64];
2972 int ret, cpu;
2974 if (cnt >= sizeof(buf))
2975 return -EINVAL;
2977 if (copy_from_user(&buf, ubuf, cnt))
2978 return -EFAULT;
2980 buf[cnt] = 0;
2982 ret = strict_strtoul(buf, 10, &val);
2983 if (ret < 0)
2984 return ret;
2986 /* must have at least 1 entry */
2987 if (!val)
2988 return -EINVAL;
2990 mutex_lock(&trace_types_lock);
2992 tracing_stop();
2994 /* disable all cpu buffers */
2995 for_each_tracing_cpu(cpu) {
2996 if (global_trace.data[cpu])
2997 atomic_inc(&global_trace.data[cpu]->disabled);
2998 if (max_tr.data[cpu])
2999 atomic_inc(&max_tr.data[cpu]->disabled);
3002 /* value is in KB */
3003 val <<= 10;
3005 if (val != global_trace.entries) {
3006 ret = tracing_resize_ring_buffer(val);
3007 if (ret < 0) {
3008 cnt = ret;
3009 goto out;
3013 filp->f_pos += cnt;
3015 /* If check pages failed, return ENOMEM */
3016 if (tracing_disabled)
3017 cnt = -ENOMEM;
3018 out:
3019 for_each_tracing_cpu(cpu) {
3020 if (global_trace.data[cpu])
3021 atomic_dec(&global_trace.data[cpu]->disabled);
3022 if (max_tr.data[cpu])
3023 atomic_dec(&max_tr.data[cpu]->disabled);
3026 tracing_start();
3027 max_tr.entries = global_trace.entries;
3028 mutex_unlock(&trace_types_lock);
3030 return cnt;
3033 static int mark_printk(const char *fmt, ...)
3035 int ret;
3036 va_list args;
3037 va_start(args, fmt);
3038 ret = trace_vprintk(0, -1, fmt, args);
3039 va_end(args);
3040 return ret;
3043 static ssize_t
3044 tracing_mark_write(struct file *filp, const char __user *ubuf,
3045 size_t cnt, loff_t *fpos)
3047 char *buf;
3048 char *end;
3050 if (tracing_disabled)
3051 return -EINVAL;
3053 if (cnt > TRACE_BUF_SIZE)
3054 cnt = TRACE_BUF_SIZE;
3056 buf = kmalloc(cnt + 1, GFP_KERNEL);
3057 if (buf == NULL)
3058 return -ENOMEM;
3060 if (copy_from_user(buf, ubuf, cnt)) {
3061 kfree(buf);
3062 return -EFAULT;
3065 /* Cut from the first nil or newline. */
3066 buf[cnt] = '\0';
3067 end = strchr(buf, '\n');
3068 if (end)
3069 *end = '\0';
3071 cnt = mark_printk("%s\n", buf);
3072 kfree(buf);
3073 *fpos += cnt;
3075 return cnt;
3078 static const struct file_operations tracing_max_lat_fops = {
3079 .open = tracing_open_generic,
3080 .read = tracing_max_lat_read,
3081 .write = tracing_max_lat_write,
3084 static const struct file_operations tracing_ctrl_fops = {
3085 .open = tracing_open_generic,
3086 .read = tracing_ctrl_read,
3087 .write = tracing_ctrl_write,
3090 static const struct file_operations set_tracer_fops = {
3091 .open = tracing_open_generic,
3092 .read = tracing_set_trace_read,
3093 .write = tracing_set_trace_write,
3096 static const struct file_operations tracing_pipe_fops = {
3097 .open = tracing_open_pipe,
3098 .poll = tracing_poll_pipe,
3099 .read = tracing_read_pipe,
3100 .splice_read = tracing_splice_read_pipe,
3101 .release = tracing_release_pipe,
3104 static const struct file_operations tracing_entries_fops = {
3105 .open = tracing_open_generic,
3106 .read = tracing_entries_read,
3107 .write = tracing_entries_write,
3110 static const struct file_operations tracing_mark_fops = {
3111 .open = tracing_open_generic,
3112 .write = tracing_mark_write,
3115 struct ftrace_buffer_info {
3116 struct trace_array *tr;
3117 void *spare;
3118 int cpu;
3119 unsigned int read;
3122 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3124 int cpu = (int)(long)inode->i_private;
3125 struct ftrace_buffer_info *info;
3127 if (tracing_disabled)
3128 return -ENODEV;
3130 info = kzalloc(sizeof(*info), GFP_KERNEL);
3131 if (!info)
3132 return -ENOMEM;
3134 info->tr = &global_trace;
3135 info->cpu = cpu;
3136 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3137 /* Force reading ring buffer for first read */
3138 info->read = (unsigned int)-1;
3139 if (!info->spare)
3140 goto out;
3142 filp->private_data = info;
3144 return 0;
3146 out:
3147 kfree(info);
3148 return -ENOMEM;
3151 static ssize_t
3152 tracing_buffers_read(struct file *filp, char __user *ubuf,
3153 size_t count, loff_t *ppos)
3155 struct ftrace_buffer_info *info = filp->private_data;
3156 unsigned int pos;
3157 ssize_t ret;
3158 size_t size;
3160 if (!count)
3161 return 0;
3163 /* Do we have previous read data to read? */
3164 if (info->read < PAGE_SIZE)
3165 goto read;
3167 info->read = 0;
3169 ret = ring_buffer_read_page(info->tr->buffer,
3170 &info->spare,
3171 count,
3172 info->cpu, 0);
3173 if (ret < 0)
3174 return 0;
3176 pos = ring_buffer_page_len(info->spare);
3178 if (pos < PAGE_SIZE)
3179 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3181 read:
3182 size = PAGE_SIZE - info->read;
3183 if (size > count)
3184 size = count;
3186 ret = copy_to_user(ubuf, info->spare + info->read, size);
3187 if (ret == size)
3188 return -EFAULT;
3189 size -= ret;
3191 *ppos += size;
3192 info->read += size;
3194 return size;
3197 static int tracing_buffers_release(struct inode *inode, struct file *file)
3199 struct ftrace_buffer_info *info = file->private_data;
3201 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3202 kfree(info);
3204 return 0;
3207 struct buffer_ref {
3208 struct ring_buffer *buffer;
3209 void *page;
3210 int ref;
3213 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3214 struct pipe_buffer *buf)
3216 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3218 if (--ref->ref)
3219 return;
3221 ring_buffer_free_read_page(ref->buffer, ref->page);
3222 kfree(ref);
3223 buf->private = 0;
3226 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3227 struct pipe_buffer *buf)
3229 return 1;
3232 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3233 struct pipe_buffer *buf)
3235 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3237 ref->ref++;
3240 /* Pipe buffer operations for a buffer. */
3241 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3242 .can_merge = 0,
3243 .map = generic_pipe_buf_map,
3244 .unmap = generic_pipe_buf_unmap,
3245 .confirm = generic_pipe_buf_confirm,
3246 .release = buffer_pipe_buf_release,
3247 .steal = buffer_pipe_buf_steal,
3248 .get = buffer_pipe_buf_get,
3252 * Callback from splice_to_pipe(), if we need to release some pages
3253 * at the end of the spd in case we error'ed out in filling the pipe.
3255 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3257 struct buffer_ref *ref =
3258 (struct buffer_ref *)spd->partial[i].private;
3260 if (--ref->ref)
3261 return;
3263 ring_buffer_free_read_page(ref->buffer, ref->page);
3264 kfree(ref);
3265 spd->partial[i].private = 0;
3268 static ssize_t
3269 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3270 struct pipe_inode_info *pipe, size_t len,
3271 unsigned int flags)
3273 struct ftrace_buffer_info *info = file->private_data;
3274 struct partial_page partial[PIPE_BUFFERS];
3275 struct page *pages[PIPE_BUFFERS];
3276 struct splice_pipe_desc spd = {
3277 .pages = pages,
3278 .partial = partial,
3279 .flags = flags,
3280 .ops = &buffer_pipe_buf_ops,
3281 .spd_release = buffer_spd_release,
3283 struct buffer_ref *ref;
3284 int size, i;
3285 size_t ret;
3288 * We can't seek on a buffer input
3290 if (unlikely(*ppos))
3291 return -ESPIPE;
3294 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3295 struct page *page;
3296 int r;
3298 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3299 if (!ref)
3300 break;
3302 ref->buffer = info->tr->buffer;
3303 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3304 if (!ref->page) {
3305 kfree(ref);
3306 break;
3309 r = ring_buffer_read_page(ref->buffer, &ref->page,
3310 len, info->cpu, 0);
3311 if (r < 0) {
3312 ring_buffer_free_read_page(ref->buffer,
3313 ref->page);
3314 kfree(ref);
3315 break;
3319 * zero out any left over data, this is going to
3320 * user land.
3322 size = ring_buffer_page_len(ref->page);
3323 if (size < PAGE_SIZE)
3324 memset(ref->page + size, 0, PAGE_SIZE - size);
3326 page = virt_to_page(ref->page);
3328 spd.pages[i] = page;
3329 spd.partial[i].len = PAGE_SIZE;
3330 spd.partial[i].offset = 0;
3331 spd.partial[i].private = (unsigned long)ref;
3332 spd.nr_pages++;
3335 spd.nr_pages = i;
3337 /* did we read anything? */
3338 if (!spd.nr_pages) {
3339 if (flags & SPLICE_F_NONBLOCK)
3340 ret = -EAGAIN;
3341 else
3342 ret = 0;
3343 /* TODO: block */
3344 return ret;
3347 ret = splice_to_pipe(pipe, &spd);
3349 return ret;
3352 static const struct file_operations tracing_buffers_fops = {
3353 .open = tracing_buffers_open,
3354 .read = tracing_buffers_read,
3355 .release = tracing_buffers_release,
3356 .splice_read = tracing_buffers_splice_read,
3357 .llseek = no_llseek,
3360 #ifdef CONFIG_DYNAMIC_FTRACE
3362 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3364 return 0;
3367 static ssize_t
3368 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3369 size_t cnt, loff_t *ppos)
3371 static char ftrace_dyn_info_buffer[1024];
3372 static DEFINE_MUTEX(dyn_info_mutex);
3373 unsigned long *p = filp->private_data;
3374 char *buf = ftrace_dyn_info_buffer;
3375 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3376 int r;
3378 mutex_lock(&dyn_info_mutex);
3379 r = sprintf(buf, "%ld ", *p);
3381 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3382 buf[r++] = '\n';
3384 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3386 mutex_unlock(&dyn_info_mutex);
3388 return r;
3391 static const struct file_operations tracing_dyn_info_fops = {
3392 .open = tracing_open_generic,
3393 .read = tracing_read_dyn_info,
3395 #endif
3397 static struct dentry *d_tracer;
3399 struct dentry *tracing_init_dentry(void)
3401 static int once;
3403 if (d_tracer)
3404 return d_tracer;
3406 d_tracer = debugfs_create_dir("tracing", NULL);
3408 if (!d_tracer && !once) {
3409 once = 1;
3410 pr_warning("Could not create debugfs directory 'tracing'\n");
3411 return NULL;
3414 return d_tracer;
3417 static struct dentry *d_percpu;
3419 struct dentry *tracing_dentry_percpu(void)
3421 static int once;
3422 struct dentry *d_tracer;
3424 if (d_percpu)
3425 return d_percpu;
3427 d_tracer = tracing_init_dentry();
3429 if (!d_tracer)
3430 return NULL;
3432 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3434 if (!d_percpu && !once) {
3435 once = 1;
3436 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3437 return NULL;
3440 return d_percpu;
3443 static void tracing_init_debugfs_percpu(long cpu)
3445 struct dentry *d_percpu = tracing_dentry_percpu();
3446 struct dentry *entry, *d_cpu;
3447 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3448 char cpu_dir[7];
3450 if (cpu > 999 || cpu < 0)
3451 return;
3453 sprintf(cpu_dir, "cpu%ld", cpu);
3454 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3455 if (!d_cpu) {
3456 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3457 return;
3460 /* per cpu trace_pipe */
3461 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
3462 (void *) cpu, &tracing_pipe_fops);
3463 if (!entry)
3464 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3466 /* per cpu trace */
3467 entry = debugfs_create_file("trace", 0444, d_cpu,
3468 (void *) cpu, &tracing_fops);
3469 if (!entry)
3470 pr_warning("Could not create debugfs 'trace' entry\n");
3473 #ifdef CONFIG_FTRACE_SELFTEST
3474 /* Let selftest have access to static functions in this file */
3475 #include "trace_selftest.c"
3476 #endif
3478 struct trace_option_dentry {
3479 struct tracer_opt *opt;
3480 struct tracer_flags *flags;
3481 struct dentry *entry;
3484 static ssize_t
3485 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3486 loff_t *ppos)
3488 struct trace_option_dentry *topt = filp->private_data;
3489 char *buf;
3491 if (topt->flags->val & topt->opt->bit)
3492 buf = "1\n";
3493 else
3494 buf = "0\n";
3496 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3499 static ssize_t
3500 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3501 loff_t *ppos)
3503 struct trace_option_dentry *topt = filp->private_data;
3504 unsigned long val;
3505 char buf[64];
3506 int ret;
3508 if (cnt >= sizeof(buf))
3509 return -EINVAL;
3511 if (copy_from_user(&buf, ubuf, cnt))
3512 return -EFAULT;
3514 buf[cnt] = 0;
3516 ret = strict_strtoul(buf, 10, &val);
3517 if (ret < 0)
3518 return ret;
3520 ret = 0;
3521 switch (val) {
3522 case 0:
3523 /* do nothing if already cleared */
3524 if (!(topt->flags->val & topt->opt->bit))
3525 break;
3527 mutex_lock(&trace_types_lock);
3528 if (current_trace->set_flag)
3529 ret = current_trace->set_flag(topt->flags->val,
3530 topt->opt->bit, 0);
3531 mutex_unlock(&trace_types_lock);
3532 if (ret)
3533 return ret;
3534 topt->flags->val &= ~topt->opt->bit;
3535 break;
3536 case 1:
3537 /* do nothing if already set */
3538 if (topt->flags->val & topt->opt->bit)
3539 break;
3541 mutex_lock(&trace_types_lock);
3542 if (current_trace->set_flag)
3543 ret = current_trace->set_flag(topt->flags->val,
3544 topt->opt->bit, 1);
3545 mutex_unlock(&trace_types_lock);
3546 if (ret)
3547 return ret;
3548 topt->flags->val |= topt->opt->bit;
3549 break;
3551 default:
3552 return -EINVAL;
3555 *ppos += cnt;
3557 return cnt;
3561 static const struct file_operations trace_options_fops = {
3562 .open = tracing_open_generic,
3563 .read = trace_options_read,
3564 .write = trace_options_write,
3567 static ssize_t
3568 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3569 loff_t *ppos)
3571 long index = (long)filp->private_data;
3572 char *buf;
3574 if (trace_flags & (1 << index))
3575 buf = "1\n";
3576 else
3577 buf = "0\n";
3579 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3582 static ssize_t
3583 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3584 loff_t *ppos)
3586 long index = (long)filp->private_data;
3587 char buf[64];
3588 unsigned long val;
3589 int ret;
3591 if (cnt >= sizeof(buf))
3592 return -EINVAL;
3594 if (copy_from_user(&buf, ubuf, cnt))
3595 return -EFAULT;
3597 buf[cnt] = 0;
3599 ret = strict_strtoul(buf, 10, &val);
3600 if (ret < 0)
3601 return ret;
3603 switch (val) {
3604 case 0:
3605 trace_flags &= ~(1 << index);
3606 break;
3607 case 1:
3608 trace_flags |= 1 << index;
3609 break;
3611 default:
3612 return -EINVAL;
3615 *ppos += cnt;
3617 return cnt;
3620 static const struct file_operations trace_options_core_fops = {
3621 .open = tracing_open_generic,
3622 .read = trace_options_core_read,
3623 .write = trace_options_core_write,
3626 static struct dentry *trace_options_init_dentry(void)
3628 struct dentry *d_tracer;
3629 static struct dentry *t_options;
3631 if (t_options)
3632 return t_options;
3634 d_tracer = tracing_init_dentry();
3635 if (!d_tracer)
3636 return NULL;
3638 t_options = debugfs_create_dir("options", d_tracer);
3639 if (!t_options) {
3640 pr_warning("Could not create debugfs directory 'options'\n");
3641 return NULL;
3644 return t_options;
3647 static void
3648 create_trace_option_file(struct trace_option_dentry *topt,
3649 struct tracer_flags *flags,
3650 struct tracer_opt *opt)
3652 struct dentry *t_options;
3653 struct dentry *entry;
3655 t_options = trace_options_init_dentry();
3656 if (!t_options)
3657 return;
3659 topt->flags = flags;
3660 topt->opt = opt;
3662 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3663 &trace_options_fops);
3665 topt->entry = entry;
3669 static struct trace_option_dentry *
3670 create_trace_option_files(struct tracer *tracer)
3672 struct trace_option_dentry *topts;
3673 struct tracer_flags *flags;
3674 struct tracer_opt *opts;
3675 int cnt;
3677 if (!tracer)
3678 return NULL;
3680 flags = tracer->flags;
3682 if (!flags || !flags->opts)
3683 return NULL;
3685 opts = flags->opts;
3687 for (cnt = 0; opts[cnt].name; cnt++)
3690 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3691 if (!topts)
3692 return NULL;
3694 for (cnt = 0; opts[cnt].name; cnt++)
3695 create_trace_option_file(&topts[cnt], flags,
3696 &opts[cnt]);
3698 return topts;
3701 static void
3702 destroy_trace_option_files(struct trace_option_dentry *topts)
3704 int cnt;
3706 if (!topts)
3707 return;
3709 for (cnt = 0; topts[cnt].opt; cnt++) {
3710 if (topts[cnt].entry)
3711 debugfs_remove(topts[cnt].entry);
3714 kfree(topts);
3717 static struct dentry *
3718 create_trace_option_core_file(const char *option, long index)
3720 struct dentry *t_options;
3721 struct dentry *entry;
3723 t_options = trace_options_init_dentry();
3724 if (!t_options)
3725 return NULL;
3727 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3728 &trace_options_core_fops);
3730 return entry;
3733 static __init void create_trace_options_dir(void)
3735 struct dentry *t_options;
3736 struct dentry *entry;
3737 int i;
3739 t_options = trace_options_init_dentry();
3740 if (!t_options)
3741 return;
3743 for (i = 0; trace_options[i]; i++) {
3744 entry = create_trace_option_core_file(trace_options[i], i);
3745 if (!entry)
3746 pr_warning("Could not create debugfs %s entry\n",
3747 trace_options[i]);
3751 static __init int tracer_init_debugfs(void)
3753 struct dentry *d_tracer;
3754 struct dentry *buffers;
3755 struct dentry *entry;
3756 int cpu;
3758 d_tracer = tracing_init_dentry();
3760 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3761 &global_trace, &tracing_ctrl_fops);
3762 if (!entry)
3763 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3765 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3766 NULL, &tracing_iter_fops);
3767 if (!entry)
3768 pr_warning("Could not create debugfs 'trace_options' entry\n");
3770 create_trace_options_dir();
3772 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3773 NULL, &tracing_cpumask_fops);
3774 if (!entry)
3775 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3777 entry = debugfs_create_file("trace", 0444, d_tracer,
3778 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3779 if (!entry)
3780 pr_warning("Could not create debugfs 'trace' entry\n");
3782 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3783 &global_trace, &show_traces_fops);
3784 if (!entry)
3785 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3787 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3788 &global_trace, &set_tracer_fops);
3789 if (!entry)
3790 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3792 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3793 &tracing_max_latency,
3794 &tracing_max_lat_fops);
3795 if (!entry)
3796 pr_warning("Could not create debugfs "
3797 "'tracing_max_latency' entry\n");
3799 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3800 &tracing_thresh, &tracing_max_lat_fops);
3801 if (!entry)
3802 pr_warning("Could not create debugfs "
3803 "'tracing_thresh' entry\n");
3804 entry = debugfs_create_file("README", 0644, d_tracer,
3805 NULL, &tracing_readme_fops);
3806 if (!entry)
3807 pr_warning("Could not create debugfs 'README' entry\n");
3809 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3810 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3811 if (!entry)
3812 pr_warning("Could not create debugfs "
3813 "'trace_pipe' entry\n");
3815 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3816 &global_trace, &tracing_entries_fops);
3817 if (!entry)
3818 pr_warning("Could not create debugfs "
3819 "'buffer_size_kb' entry\n");
3821 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3822 NULL, &tracing_mark_fops);
3823 if (!entry)
3824 pr_warning("Could not create debugfs "
3825 "'trace_marker' entry\n");
3827 buffers = debugfs_create_dir("binary_buffers", d_tracer);
3829 if (!buffers)
3830 pr_warning("Could not create buffers directory\n");
3831 else {
3832 int cpu;
3833 char buf[64];
3835 for_each_tracing_cpu(cpu) {
3836 sprintf(buf, "%d", cpu);
3838 entry = debugfs_create_file(buf, 0444, buffers,
3839 (void *)(long)cpu,
3840 &tracing_buffers_fops);
3841 if (!entry)
3842 pr_warning("Could not create debugfs buffers "
3843 "'%s' entry\n", buf);
3847 #ifdef CONFIG_DYNAMIC_FTRACE
3848 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3849 &ftrace_update_tot_cnt,
3850 &tracing_dyn_info_fops);
3851 if (!entry)
3852 pr_warning("Could not create debugfs "
3853 "'dyn_ftrace_total_info' entry\n");
3854 #endif
3855 #ifdef CONFIG_SYSPROF_TRACER
3856 init_tracer_sysprof_debugfs(d_tracer);
3857 #endif
3859 for_each_tracing_cpu(cpu)
3860 tracing_init_debugfs_percpu(cpu);
3862 return 0;
3865 static int trace_panic_handler(struct notifier_block *this,
3866 unsigned long event, void *unused)
3868 if (ftrace_dump_on_oops)
3869 ftrace_dump();
3870 return NOTIFY_OK;
3873 static struct notifier_block trace_panic_notifier = {
3874 .notifier_call = trace_panic_handler,
3875 .next = NULL,
3876 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3879 static int trace_die_handler(struct notifier_block *self,
3880 unsigned long val,
3881 void *data)
3883 switch (val) {
3884 case DIE_OOPS:
3885 if (ftrace_dump_on_oops)
3886 ftrace_dump();
3887 break;
3888 default:
3889 break;
3891 return NOTIFY_OK;
3894 static struct notifier_block trace_die_notifier = {
3895 .notifier_call = trace_die_handler,
3896 .priority = 200
3900 * printk is set to max of 1024, we really don't need it that big.
3901 * Nothing should be printing 1000 characters anyway.
3903 #define TRACE_MAX_PRINT 1000
3906 * Define here KERN_TRACE so that we have one place to modify
3907 * it if we decide to change what log level the ftrace dump
3908 * should be at.
3910 #define KERN_TRACE KERN_EMERG
3912 static void
3913 trace_printk_seq(struct trace_seq *s)
3915 /* Probably should print a warning here. */
3916 if (s->len >= 1000)
3917 s->len = 1000;
3919 /* should be zero ended, but we are paranoid. */
3920 s->buffer[s->len] = 0;
3922 printk(KERN_TRACE "%s", s->buffer);
3924 trace_seq_init(s);
3927 void ftrace_dump(void)
3929 static DEFINE_SPINLOCK(ftrace_dump_lock);
3930 /* use static because iter can be a bit big for the stack */
3931 static struct trace_iterator iter;
3932 static int dump_ran;
3933 unsigned long flags;
3934 int cnt = 0, cpu;
3936 /* only one dump */
3937 spin_lock_irqsave(&ftrace_dump_lock, flags);
3938 if (dump_ran)
3939 goto out;
3941 dump_ran = 1;
3943 /* No turning back! */
3944 tracing_off();
3945 ftrace_kill();
3947 for_each_tracing_cpu(cpu) {
3948 atomic_inc(&global_trace.data[cpu]->disabled);
3951 /* don't look at user memory in panic mode */
3952 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3954 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3956 /* Simulate the iterator */
3957 iter.tr = &global_trace;
3958 iter.trace = current_trace;
3959 iter.cpu_file = TRACE_PIPE_ALL_CPU;
3962 * We need to stop all tracing on all CPUS to read the
3963 * the next buffer. This is a bit expensive, but is
3964 * not done often. We fill all what we can read,
3965 * and then release the locks again.
3968 while (!trace_empty(&iter)) {
3970 if (!cnt)
3971 printk(KERN_TRACE "---------------------------------\n");
3973 cnt++;
3975 /* reset all but tr, trace, and overruns */
3976 memset(&iter.seq, 0,
3977 sizeof(struct trace_iterator) -
3978 offsetof(struct trace_iterator, seq));
3979 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3980 iter.pos = -1;
3982 if (find_next_entry_inc(&iter) != NULL) {
3983 print_trace_line(&iter);
3984 trace_consume(&iter);
3987 trace_printk_seq(&iter.seq);
3990 if (!cnt)
3991 printk(KERN_TRACE " (ftrace buffer empty)\n");
3992 else
3993 printk(KERN_TRACE "---------------------------------\n");
3995 out:
3996 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3999 __init static int tracer_alloc_buffers(void)
4001 struct trace_array_cpu *data;
4002 int ring_buf_size;
4003 int i;
4004 int ret = -ENOMEM;
4006 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4007 goto out;
4009 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4010 goto out_free_buffer_mask;
4012 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4013 goto out_free_tracing_cpumask;
4015 /* To save memory, keep the ring buffer size to its minimum */
4016 if (ring_buffer_expanded)
4017 ring_buf_size = trace_buf_size;
4018 else
4019 ring_buf_size = 1;
4021 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4022 cpumask_copy(tracing_cpumask, cpu_all_mask);
4023 cpumask_clear(tracing_reader_cpumask);
4025 /* TODO: make the number of buffers hot pluggable with CPUS */
4026 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4027 TRACE_BUFFER_FLAGS);
4028 if (!global_trace.buffer) {
4029 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4030 WARN_ON(1);
4031 goto out_free_cpumask;
4033 global_trace.entries = ring_buffer_size(global_trace.buffer);
4036 #ifdef CONFIG_TRACER_MAX_TRACE
4037 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4038 TRACE_BUFFER_FLAGS);
4039 if (!max_tr.buffer) {
4040 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4041 WARN_ON(1);
4042 ring_buffer_free(global_trace.buffer);
4043 goto out_free_cpumask;
4045 max_tr.entries = ring_buffer_size(max_tr.buffer);
4046 WARN_ON(max_tr.entries != global_trace.entries);
4047 #endif
4049 /* Allocate the first page for all buffers */
4050 for_each_tracing_cpu(i) {
4051 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4052 max_tr.data[i] = &per_cpu(max_data, i);
4055 trace_init_cmdlines();
4057 register_tracer(&nop_trace);
4058 current_trace = &nop_trace;
4059 #ifdef CONFIG_BOOT_TRACER
4060 register_tracer(&boot_tracer);
4061 #endif
4062 /* All seems OK, enable tracing */
4063 tracing_disabled = 0;
4065 atomic_notifier_chain_register(&panic_notifier_list,
4066 &trace_panic_notifier);
4068 register_die_notifier(&trace_die_notifier);
4069 ret = 0;
4071 out_free_cpumask:
4072 free_cpumask_var(tracing_reader_cpumask);
4073 out_free_tracing_cpumask:
4074 free_cpumask_var(tracing_cpumask);
4075 out_free_buffer_mask:
4076 free_cpumask_var(tracing_buffer_mask);
4077 out:
4078 return ret;
4081 __init static int clear_boot_tracer(void)
4084 * The default tracer at boot buffer is an init section.
4085 * This function is called in lateinit. If we did not
4086 * find the boot tracer, then clear it out, to prevent
4087 * later registration from accessing the buffer that is
4088 * about to be freed.
4090 if (!default_bootup_tracer)
4091 return 0;
4093 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4094 default_bootup_tracer);
4095 default_bootup_tracer = NULL;
4097 return 0;
4100 early_initcall(tracer_alloc_buffers);
4101 fs_initcall(tracer_init_debugfs);
4102 late_initcall(clear_boot_tracer);