Merge branch 'tip/tracing/ftrace' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6/mini2440.git] / kernel / trace / trace.c
blob5c9c6d9070547724ea979604648f89ed4c424244
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 * We need to change this state when a selftest is running.
49 * A selftest will lurk into the ring-buffer to count the
50 * entries inserted during the selftest although some concurrent
51 * insertions into the ring-buffer such as trace_printk could occurred
52 * at the same time, giving false positive or negative results.
54 static bool __read_mostly tracing_selftest_running;
57 * If a tracer is running, we do not want to run SELFTEST.
59 static bool __read_mostly tracing_selftest_disabled;
61 /* For tracers that don't implement custom flags */
62 static struct tracer_opt dummy_tracer_opt[] = {
63 { }
66 static struct tracer_flags dummy_tracer_flags = {
67 .val = 0,
68 .opts = dummy_tracer_opt
71 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
73 return 0;
77 * Kill all tracing for good (never come back).
78 * It is initialized to 1 but will turn to zero if the initialization
79 * of the tracer is successful. But that is the only place that sets
80 * this back to zero.
82 static int tracing_disabled = 1;
84 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
86 static inline void ftrace_disable_cpu(void)
88 preempt_disable();
89 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
92 static inline void ftrace_enable_cpu(void)
94 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
95 preempt_enable();
98 static cpumask_var_t __read_mostly tracing_buffer_mask;
100 /* Define which cpu buffers are currently read in trace_pipe */
101 static cpumask_var_t tracing_reader_cpumask;
103 #define for_each_tracing_cpu(cpu) \
104 for_each_cpu(cpu, tracing_buffer_mask)
107 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
109 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
110 * is set, then ftrace_dump is called. This will output the contents
111 * of the ftrace buffers to the console. This is very useful for
112 * capturing traces that lead to crashes and outputing it to a
113 * serial console.
115 * It is default off, but you can enable it with either specifying
116 * "ftrace_dump_on_oops" in the kernel command line, or setting
117 * /proc/sys/kernel/ftrace_dump_on_oops to true.
119 int ftrace_dump_on_oops;
121 static int tracing_set_tracer(const char *buf);
123 #define BOOTUP_TRACER_SIZE 100
124 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
125 static char *default_bootup_tracer;
127 static int __init set_ftrace(char *str)
129 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
130 default_bootup_tracer = bootup_tracer_buf;
131 return 1;
133 __setup("ftrace=", set_ftrace);
135 static int __init set_ftrace_dump_on_oops(char *str)
137 ftrace_dump_on_oops = 1;
138 return 1;
140 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
142 long
143 ns2usecs(cycle_t nsec)
145 nsec += 500;
146 do_div(nsec, 1000);
147 return nsec;
150 cycle_t ftrace_now(int cpu)
152 u64 ts = ring_buffer_time_stamp(cpu);
153 ring_buffer_normalize_time_stamp(cpu, &ts);
154 return ts;
158 * The global_trace is the descriptor that holds the tracing
159 * buffers for the live tracing. For each CPU, it contains
160 * a link list of pages that will store trace entries. The
161 * page descriptor of the pages in the memory is used to hold
162 * the link list by linking the lru item in the page descriptor
163 * to each of the pages in the buffer per CPU.
165 * For each active CPU there is a data field that holds the
166 * pages for the buffer for that CPU. Each CPU has the same number
167 * of pages allocated for its buffer.
169 static struct trace_array global_trace;
171 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
174 * The max_tr is used to snapshot the global_trace when a maximum
175 * latency is reached. Some tracers will use this to store a maximum
176 * trace while it continues examining live traces.
178 * The buffers for the max_tr are set up the same as the global_trace.
179 * When a snapshot is taken, the link list of the max_tr is swapped
180 * with the link list of the global_trace and the buffers are reset for
181 * the global_trace so the tracing can continue.
183 static struct trace_array max_tr;
185 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
187 /* tracer_enabled is used to toggle activation of a tracer */
188 static int tracer_enabled = 1;
191 * tracing_is_enabled - return tracer_enabled status
193 * This function is used by other tracers to know the status
194 * of the tracer_enabled flag. Tracers may use this function
195 * to know if it should enable their features when starting
196 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
198 int tracing_is_enabled(void)
200 return tracer_enabled;
204 * trace_buf_size is the size in bytes that is allocated
205 * for a buffer. Note, the number of bytes is always rounded
206 * to page size.
208 * This number is purposely set to a low number of 16384.
209 * If the dump on oops happens, it will be much appreciated
210 * to not have to wait for all that output. Anyway this can be
211 * boot time and run time configurable.
213 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
215 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
217 /* trace_types holds a link list of available tracers. */
218 static struct tracer *trace_types __read_mostly;
220 /* current_trace points to the tracer that is currently active */
221 static struct tracer *current_trace __read_mostly;
224 * max_tracer_type_len is used to simplify the allocating of
225 * buffers to read userspace tracer names. We keep track of
226 * the longest tracer name registered.
228 static int max_tracer_type_len;
231 * trace_types_lock is used to protect the trace_types list.
232 * This lock is also used to keep user access serialized.
233 * Accesses from userspace will grab this lock while userspace
234 * activities happen inside the kernel.
236 static DEFINE_MUTEX(trace_types_lock);
238 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
239 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
241 /* trace_flags holds trace_options default values */
242 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
243 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
246 * trace_wake_up - wake up tasks waiting for trace input
248 * Simply wakes up any task that is blocked on the trace_wait
249 * queue. These is used with trace_poll for tasks polling the trace.
251 void trace_wake_up(void)
254 * The runqueue_is_locked() can fail, but this is the best we
255 * have for now:
257 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
258 wake_up(&trace_wait);
261 static int __init set_buf_size(char *str)
263 unsigned long buf_size;
264 int ret;
266 if (!str)
267 return 0;
268 ret = strict_strtoul(str, 0, &buf_size);
269 /* nr_entries can not be zero */
270 if (ret < 0 || buf_size == 0)
271 return 0;
272 trace_buf_size = buf_size;
273 return 1;
275 __setup("trace_buf_size=", set_buf_size);
277 unsigned long nsecs_to_usecs(unsigned long nsecs)
279 return nsecs / 1000;
282 /* These must match the bit postions in trace_iterator_flags */
283 static const char *trace_options[] = {
284 "print-parent",
285 "sym-offset",
286 "sym-addr",
287 "verbose",
288 "raw",
289 "hex",
290 "bin",
291 "block",
292 "stacktrace",
293 "sched-tree",
294 "trace_printk",
295 "ftrace_preempt",
296 "branch",
297 "annotate",
298 "userstacktrace",
299 "sym-userobj",
300 "printk-msg-only",
301 "context-info",
302 "latency-format",
303 NULL
307 * ftrace_max_lock is used to protect the swapping of buffers
308 * when taking a max snapshot. The buffers themselves are
309 * protected by per_cpu spinlocks. But the action of the swap
310 * needs its own lock.
312 * This is defined as a raw_spinlock_t in order to help
313 * with performance when lockdep debugging is enabled.
315 static raw_spinlock_t ftrace_max_lock =
316 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
319 * Copy the new maximum trace into the separate maximum-trace
320 * structure. (this way the maximum trace is permanently saved,
321 * for later retrieval via /debugfs/tracing/latency_trace)
323 static void
324 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
326 struct trace_array_cpu *data = tr->data[cpu];
328 max_tr.cpu = cpu;
329 max_tr.time_start = data->preempt_timestamp;
331 data = max_tr.data[cpu];
332 data->saved_latency = tracing_max_latency;
334 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
335 data->pid = tsk->pid;
336 data->uid = task_uid(tsk);
337 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
338 data->policy = tsk->policy;
339 data->rt_priority = tsk->rt_priority;
341 /* record this tasks comm */
342 tracing_record_cmdline(tsk);
345 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
347 int len;
348 int ret;
350 if (!cnt)
351 return 0;
353 if (s->len <= s->readpos)
354 return -EBUSY;
356 len = s->len - s->readpos;
357 if (cnt > len)
358 cnt = len;
359 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
360 if (ret == cnt)
361 return -EFAULT;
363 cnt -= ret;
365 s->readpos += cnt;
366 return cnt;
369 ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
371 int len;
372 void *ret;
374 if (s->len <= s->readpos)
375 return -EBUSY;
377 len = s->len - s->readpos;
378 if (cnt > len)
379 cnt = len;
380 ret = memcpy(buf, s->buffer + s->readpos, cnt);
381 if (!ret)
382 return -EFAULT;
384 s->readpos += cnt;
385 return cnt;
388 static void
389 trace_print_seq(struct seq_file *m, struct trace_seq *s)
391 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
393 s->buffer[len] = 0;
394 seq_puts(m, s->buffer);
396 trace_seq_init(s);
400 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
401 * @tr: tracer
402 * @tsk: the task with the latency
403 * @cpu: The cpu that initiated the trace.
405 * Flip the buffers between the @tr and the max_tr and record information
406 * about which task was the cause of this latency.
408 void
409 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
411 struct ring_buffer *buf = tr->buffer;
413 WARN_ON_ONCE(!irqs_disabled());
414 __raw_spin_lock(&ftrace_max_lock);
416 tr->buffer = max_tr.buffer;
417 max_tr.buffer = buf;
419 ftrace_disable_cpu();
420 ring_buffer_reset(tr->buffer);
421 ftrace_enable_cpu();
423 __update_max_tr(tr, tsk, cpu);
424 __raw_spin_unlock(&ftrace_max_lock);
428 * update_max_tr_single - only copy one trace over, and reset the rest
429 * @tr - tracer
430 * @tsk - task with the latency
431 * @cpu - the cpu of the buffer to copy.
433 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
435 void
436 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
438 int ret;
440 WARN_ON_ONCE(!irqs_disabled());
441 __raw_spin_lock(&ftrace_max_lock);
443 ftrace_disable_cpu();
445 ring_buffer_reset(max_tr.buffer);
446 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
448 ftrace_enable_cpu();
450 WARN_ON_ONCE(ret && ret != -EAGAIN);
452 __update_max_tr(tr, tsk, cpu);
453 __raw_spin_unlock(&ftrace_max_lock);
457 * register_tracer - register a tracer with the ftrace system.
458 * @type - the plugin for the tracer
460 * Register a new plugin tracer.
462 int register_tracer(struct tracer *type)
463 __releases(kernel_lock)
464 __acquires(kernel_lock)
466 struct tracer *t;
467 int len;
468 int ret = 0;
470 if (!type->name) {
471 pr_info("Tracer must have a name\n");
472 return -1;
476 * When this gets called we hold the BKL which means that
477 * preemption is disabled. Various trace selftests however
478 * need to disable and enable preemption for successful tests.
479 * So we drop the BKL here and grab it after the tests again.
481 unlock_kernel();
482 mutex_lock(&trace_types_lock);
484 tracing_selftest_running = true;
486 for (t = trace_types; t; t = t->next) {
487 if (strcmp(type->name, t->name) == 0) {
488 /* already found */
489 pr_info("Trace %s already registered\n",
490 type->name);
491 ret = -1;
492 goto out;
496 if (!type->set_flag)
497 type->set_flag = &dummy_set_flag;
498 if (!type->flags)
499 type->flags = &dummy_tracer_flags;
500 else
501 if (!type->flags->opts)
502 type->flags->opts = dummy_tracer_opt;
503 if (!type->wait_pipe)
504 type->wait_pipe = default_wait_pipe;
507 #ifdef CONFIG_FTRACE_STARTUP_TEST
508 if (type->selftest && !tracing_selftest_disabled) {
509 struct tracer *saved_tracer = current_trace;
510 struct trace_array *tr = &global_trace;
511 int i;
514 * Run a selftest on this tracer.
515 * Here we reset the trace buffer, and set the current
516 * tracer to be this tracer. The tracer can then run some
517 * internal tracing to verify that everything is in order.
518 * If we fail, we do not register this tracer.
520 for_each_tracing_cpu(i)
521 tracing_reset(tr, i);
523 current_trace = type;
524 /* the test is responsible for initializing and enabling */
525 pr_info("Testing tracer %s: ", type->name);
526 ret = type->selftest(type, tr);
527 /* the test is responsible for resetting too */
528 current_trace = saved_tracer;
529 if (ret) {
530 printk(KERN_CONT "FAILED!\n");
531 goto out;
533 /* Only reset on passing, to avoid touching corrupted buffers */
534 for_each_tracing_cpu(i)
535 tracing_reset(tr, i);
537 printk(KERN_CONT "PASSED\n");
539 #endif
541 type->next = trace_types;
542 trace_types = type;
543 len = strlen(type->name);
544 if (len > max_tracer_type_len)
545 max_tracer_type_len = len;
547 out:
548 tracing_selftest_running = false;
549 mutex_unlock(&trace_types_lock);
551 if (ret || !default_bootup_tracer)
552 goto out_unlock;
554 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
555 goto out_unlock;
557 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
558 /* Do we want this tracer to start on bootup? */
559 tracing_set_tracer(type->name);
560 default_bootup_tracer = NULL;
561 /* disable other selftests, since this will break it. */
562 tracing_selftest_disabled = 1;
563 #ifdef CONFIG_FTRACE_STARTUP_TEST
564 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
565 type->name);
566 #endif
568 out_unlock:
569 lock_kernel();
570 return ret;
573 void unregister_tracer(struct tracer *type)
575 struct tracer **t;
576 int len;
578 mutex_lock(&trace_types_lock);
579 for (t = &trace_types; *t; t = &(*t)->next) {
580 if (*t == type)
581 goto found;
583 pr_info("Trace %s not registered\n", type->name);
584 goto out;
586 found:
587 *t = (*t)->next;
589 if (type == current_trace && tracer_enabled) {
590 tracer_enabled = 0;
591 tracing_stop();
592 if (current_trace->stop)
593 current_trace->stop(&global_trace);
594 current_trace = &nop_trace;
597 if (strlen(type->name) != max_tracer_type_len)
598 goto out;
600 max_tracer_type_len = 0;
601 for (t = &trace_types; *t; t = &(*t)->next) {
602 len = strlen((*t)->name);
603 if (len > max_tracer_type_len)
604 max_tracer_type_len = len;
606 out:
607 mutex_unlock(&trace_types_lock);
610 void tracing_reset(struct trace_array *tr, int cpu)
612 ftrace_disable_cpu();
613 ring_buffer_reset_cpu(tr->buffer, cpu);
614 ftrace_enable_cpu();
617 void tracing_reset_online_cpus(struct trace_array *tr)
619 int cpu;
621 tr->time_start = ftrace_now(tr->cpu);
623 for_each_online_cpu(cpu)
624 tracing_reset(tr, cpu);
627 #define SAVED_CMDLINES 128
628 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
629 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
630 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
631 static int cmdline_idx;
632 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
634 /* temporary disable recording */
635 static atomic_t trace_record_cmdline_disabled __read_mostly;
637 static void trace_init_cmdlines(void)
639 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
640 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
641 cmdline_idx = 0;
644 static int trace_stop_count;
645 static DEFINE_SPINLOCK(tracing_start_lock);
648 * ftrace_off_permanent - disable all ftrace code permanently
650 * This should only be called when a serious anomally has
651 * been detected. This will turn off the function tracing,
652 * ring buffers, and other tracing utilites. It takes no
653 * locks and can be called from any context.
655 void ftrace_off_permanent(void)
657 tracing_disabled = 1;
658 ftrace_stop();
659 tracing_off_permanent();
663 * tracing_start - quick start of the tracer
665 * If tracing is enabled but was stopped by tracing_stop,
666 * this will start the tracer back up.
668 void tracing_start(void)
670 struct ring_buffer *buffer;
671 unsigned long flags;
673 if (tracing_disabled)
674 return;
676 spin_lock_irqsave(&tracing_start_lock, flags);
677 if (--trace_stop_count) {
678 if (trace_stop_count < 0) {
679 /* Someone screwed up their debugging */
680 WARN_ON_ONCE(1);
681 trace_stop_count = 0;
683 goto out;
687 buffer = global_trace.buffer;
688 if (buffer)
689 ring_buffer_record_enable(buffer);
691 buffer = max_tr.buffer;
692 if (buffer)
693 ring_buffer_record_enable(buffer);
695 ftrace_start();
696 out:
697 spin_unlock_irqrestore(&tracing_start_lock, flags);
701 * tracing_stop - quick stop of the tracer
703 * Light weight way to stop tracing. Use in conjunction with
704 * tracing_start.
706 void tracing_stop(void)
708 struct ring_buffer *buffer;
709 unsigned long flags;
711 ftrace_stop();
712 spin_lock_irqsave(&tracing_start_lock, flags);
713 if (trace_stop_count++)
714 goto out;
716 buffer = global_trace.buffer;
717 if (buffer)
718 ring_buffer_record_disable(buffer);
720 buffer = max_tr.buffer;
721 if (buffer)
722 ring_buffer_record_disable(buffer);
724 out:
725 spin_unlock_irqrestore(&tracing_start_lock, flags);
728 void trace_stop_cmdline_recording(void);
730 static void trace_save_cmdline(struct task_struct *tsk)
732 unsigned map;
733 unsigned idx;
735 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
736 return;
739 * It's not the end of the world if we don't get
740 * the lock, but we also don't want to spin
741 * nor do we want to disable interrupts,
742 * so if we miss here, then better luck next time.
744 if (!__raw_spin_trylock(&trace_cmdline_lock))
745 return;
747 idx = map_pid_to_cmdline[tsk->pid];
748 if (idx >= SAVED_CMDLINES) {
749 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
751 map = map_cmdline_to_pid[idx];
752 if (map <= PID_MAX_DEFAULT)
753 map_pid_to_cmdline[map] = (unsigned)-1;
755 map_pid_to_cmdline[tsk->pid] = idx;
757 cmdline_idx = idx;
760 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
762 __raw_spin_unlock(&trace_cmdline_lock);
765 char *trace_find_cmdline(int pid)
767 char *cmdline = "<...>";
768 unsigned map;
770 if (!pid)
771 return "<idle>";
773 if (pid > PID_MAX_DEFAULT)
774 goto out;
776 map = map_pid_to_cmdline[pid];
777 if (map >= SAVED_CMDLINES)
778 goto out;
780 cmdline = saved_cmdlines[map];
782 out:
783 return cmdline;
786 void tracing_record_cmdline(struct task_struct *tsk)
788 if (atomic_read(&trace_record_cmdline_disabled))
789 return;
791 trace_save_cmdline(tsk);
794 void
795 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
796 int pc)
798 struct task_struct *tsk = current;
800 entry->preempt_count = pc & 0xff;
801 entry->pid = (tsk) ? tsk->pid : 0;
802 entry->tgid = (tsk) ? tsk->tgid : 0;
803 entry->flags =
804 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
805 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
806 #else
807 TRACE_FLAG_IRQS_NOSUPPORT |
808 #endif
809 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
810 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
811 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
814 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
815 unsigned char type,
816 unsigned long len,
817 unsigned long flags, int pc)
819 struct ring_buffer_event *event;
821 event = ring_buffer_lock_reserve(tr->buffer, len);
822 if (event != NULL) {
823 struct trace_entry *ent = ring_buffer_event_data(event);
825 tracing_generic_entry_update(ent, flags, pc);
826 ent->type = type;
829 return event;
831 static void ftrace_trace_stack(struct trace_array *tr,
832 unsigned long flags, int skip, int pc);
833 static void ftrace_trace_userstack(struct trace_array *tr,
834 unsigned long flags, int pc);
836 void trace_buffer_unlock_commit(struct trace_array *tr,
837 struct ring_buffer_event *event,
838 unsigned long flags, int pc)
840 ring_buffer_unlock_commit(tr->buffer, event);
842 ftrace_trace_stack(tr, flags, 6, pc);
843 ftrace_trace_userstack(tr, flags, pc);
844 trace_wake_up();
847 struct ring_buffer_event *
848 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
849 unsigned long flags, int pc)
851 return trace_buffer_lock_reserve(&global_trace,
852 type, len, flags, pc);
855 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
856 unsigned long flags, int pc)
858 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
861 void
862 trace_function(struct trace_array *tr,
863 unsigned long ip, unsigned long parent_ip, unsigned long flags,
864 int pc)
866 struct ring_buffer_event *event;
867 struct ftrace_entry *entry;
869 /* If we are reading the ring buffer, don't trace */
870 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
871 return;
873 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
874 flags, pc);
875 if (!event)
876 return;
877 entry = ring_buffer_event_data(event);
878 entry->ip = ip;
879 entry->parent_ip = parent_ip;
880 ring_buffer_unlock_commit(tr->buffer, event);
883 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
884 static void __trace_graph_entry(struct trace_array *tr,
885 struct ftrace_graph_ent *trace,
886 unsigned long flags,
887 int pc)
889 struct ring_buffer_event *event;
890 struct ftrace_graph_ent_entry *entry;
892 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
893 return;
895 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
896 sizeof(*entry), flags, pc);
897 if (!event)
898 return;
899 entry = ring_buffer_event_data(event);
900 entry->graph_ent = *trace;
901 ring_buffer_unlock_commit(global_trace.buffer, event);
904 static void __trace_graph_return(struct trace_array *tr,
905 struct ftrace_graph_ret *trace,
906 unsigned long flags,
907 int pc)
909 struct ring_buffer_event *event;
910 struct ftrace_graph_ret_entry *entry;
912 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
913 return;
915 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
916 sizeof(*entry), flags, pc);
917 if (!event)
918 return;
919 entry = ring_buffer_event_data(event);
920 entry->ret = *trace;
921 ring_buffer_unlock_commit(global_trace.buffer, event);
923 #endif
925 void
926 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
927 unsigned long ip, unsigned long parent_ip, unsigned long flags,
928 int pc)
930 if (likely(!atomic_read(&data->disabled)))
931 trace_function(tr, ip, parent_ip, flags, pc);
934 static void __ftrace_trace_stack(struct trace_array *tr,
935 unsigned long flags,
936 int skip, int pc)
938 #ifdef CONFIG_STACKTRACE
939 struct ring_buffer_event *event;
940 struct stack_entry *entry;
941 struct stack_trace trace;
943 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
944 sizeof(*entry), flags, pc);
945 if (!event)
946 return;
947 entry = ring_buffer_event_data(event);
948 memset(&entry->caller, 0, sizeof(entry->caller));
950 trace.nr_entries = 0;
951 trace.max_entries = FTRACE_STACK_ENTRIES;
952 trace.skip = skip;
953 trace.entries = entry->caller;
955 save_stack_trace(&trace);
956 ring_buffer_unlock_commit(tr->buffer, event);
957 #endif
960 static void ftrace_trace_stack(struct trace_array *tr,
961 unsigned long flags,
962 int skip, int pc)
964 if (!(trace_flags & TRACE_ITER_STACKTRACE))
965 return;
967 __ftrace_trace_stack(tr, flags, skip, pc);
970 void __trace_stack(struct trace_array *tr,
971 unsigned long flags,
972 int skip, int pc)
974 __ftrace_trace_stack(tr, flags, skip, pc);
977 static void ftrace_trace_userstack(struct trace_array *tr,
978 unsigned long flags, int pc)
980 #ifdef CONFIG_STACKTRACE
981 struct ring_buffer_event *event;
982 struct userstack_entry *entry;
983 struct stack_trace trace;
985 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
986 return;
988 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
989 sizeof(*entry), flags, pc);
990 if (!event)
991 return;
992 entry = ring_buffer_event_data(event);
994 memset(&entry->caller, 0, sizeof(entry->caller));
996 trace.nr_entries = 0;
997 trace.max_entries = FTRACE_STACK_ENTRIES;
998 trace.skip = 0;
999 trace.entries = entry->caller;
1001 save_stack_trace_user(&trace);
1002 ring_buffer_unlock_commit(tr->buffer, event);
1003 #endif
1006 #ifdef UNUSED
1007 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1009 ftrace_trace_userstack(tr, flags, preempt_count());
1011 #endif /* UNUSED */
1013 static void
1014 ftrace_trace_special(void *__tr,
1015 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1016 int pc)
1018 struct ring_buffer_event *event;
1019 struct trace_array *tr = __tr;
1020 struct special_entry *entry;
1022 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1023 sizeof(*entry), 0, pc);
1024 if (!event)
1025 return;
1026 entry = ring_buffer_event_data(event);
1027 entry->arg1 = arg1;
1028 entry->arg2 = arg2;
1029 entry->arg3 = arg3;
1030 trace_buffer_unlock_commit(tr, event, 0, pc);
1033 void
1034 __trace_special(void *__tr, void *__data,
1035 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1037 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1040 void
1041 tracing_sched_switch_trace(struct trace_array *tr,
1042 struct task_struct *prev,
1043 struct task_struct *next,
1044 unsigned long flags, int pc)
1046 struct ring_buffer_event *event;
1047 struct ctx_switch_entry *entry;
1049 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1050 sizeof(*entry), flags, pc);
1051 if (!event)
1052 return;
1053 entry = ring_buffer_event_data(event);
1054 entry->prev_pid = prev->pid;
1055 entry->prev_prio = prev->prio;
1056 entry->prev_state = prev->state;
1057 entry->next_pid = next->pid;
1058 entry->next_prio = next->prio;
1059 entry->next_state = next->state;
1060 entry->next_cpu = task_cpu(next);
1061 trace_buffer_unlock_commit(tr, event, flags, pc);
1064 void
1065 tracing_sched_wakeup_trace(struct trace_array *tr,
1066 struct task_struct *wakee,
1067 struct task_struct *curr,
1068 unsigned long flags, int pc)
1070 struct ring_buffer_event *event;
1071 struct ctx_switch_entry *entry;
1073 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1074 sizeof(*entry), flags, pc);
1075 if (!event)
1076 return;
1077 entry = ring_buffer_event_data(event);
1078 entry->prev_pid = curr->pid;
1079 entry->prev_prio = curr->prio;
1080 entry->prev_state = curr->state;
1081 entry->next_pid = wakee->pid;
1082 entry->next_prio = wakee->prio;
1083 entry->next_state = wakee->state;
1084 entry->next_cpu = task_cpu(wakee);
1086 ring_buffer_unlock_commit(tr->buffer, event);
1087 ftrace_trace_stack(tr, flags, 6, pc);
1088 ftrace_trace_userstack(tr, flags, pc);
1091 void
1092 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1094 struct trace_array *tr = &global_trace;
1095 struct trace_array_cpu *data;
1096 unsigned long flags;
1097 int cpu;
1098 int pc;
1100 if (tracing_disabled)
1101 return;
1103 pc = preempt_count();
1104 local_irq_save(flags);
1105 cpu = raw_smp_processor_id();
1106 data = tr->data[cpu];
1108 if (likely(atomic_inc_return(&data->disabled) == 1))
1109 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1111 atomic_dec(&data->disabled);
1112 local_irq_restore(flags);
1115 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1116 int trace_graph_entry(struct ftrace_graph_ent *trace)
1118 struct trace_array *tr = &global_trace;
1119 struct trace_array_cpu *data;
1120 unsigned long flags;
1121 long disabled;
1122 int cpu;
1123 int pc;
1125 if (!ftrace_trace_task(current))
1126 return 0;
1128 if (!ftrace_graph_addr(trace->func))
1129 return 0;
1131 local_irq_save(flags);
1132 cpu = raw_smp_processor_id();
1133 data = tr->data[cpu];
1134 disabled = atomic_inc_return(&data->disabled);
1135 if (likely(disabled == 1)) {
1136 pc = preempt_count();
1137 __trace_graph_entry(tr, trace, flags, pc);
1139 /* Only do the atomic if it is not already set */
1140 if (!test_tsk_trace_graph(current))
1141 set_tsk_trace_graph(current);
1142 atomic_dec(&data->disabled);
1143 local_irq_restore(flags);
1145 return 1;
1148 void trace_graph_return(struct ftrace_graph_ret *trace)
1150 struct trace_array *tr = &global_trace;
1151 struct trace_array_cpu *data;
1152 unsigned long flags;
1153 long disabled;
1154 int cpu;
1155 int pc;
1157 local_irq_save(flags);
1158 cpu = raw_smp_processor_id();
1159 data = tr->data[cpu];
1160 disabled = atomic_inc_return(&data->disabled);
1161 if (likely(disabled == 1)) {
1162 pc = preempt_count();
1163 __trace_graph_return(tr, trace, flags, pc);
1165 if (!trace->depth)
1166 clear_tsk_trace_graph(current);
1167 atomic_dec(&data->disabled);
1168 local_irq_restore(flags);
1170 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1174 * trace_vprintk - write binary msg to tracing buffer
1177 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1179 static raw_spinlock_t trace_buf_lock =
1180 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1181 static u32 trace_buf[TRACE_BUF_SIZE];
1183 struct ring_buffer_event *event;
1184 struct trace_array *tr = &global_trace;
1185 struct trace_array_cpu *data;
1186 struct print_entry *entry;
1187 unsigned long flags;
1188 int resched;
1189 int cpu, len = 0, size, pc;
1191 if (unlikely(tracing_selftest_running || tracing_disabled))
1192 return 0;
1194 /* Don't pollute graph traces with trace_vprintk internals */
1195 pause_graph_tracing();
1197 pc = preempt_count();
1198 resched = ftrace_preempt_disable();
1199 cpu = raw_smp_processor_id();
1200 data = tr->data[cpu];
1202 if (unlikely(atomic_read(&data->disabled)))
1203 goto out;
1205 /* Lockdep uses trace_printk for lock tracing */
1206 local_irq_save(flags);
1207 __raw_spin_lock(&trace_buf_lock);
1208 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1210 if (len > TRACE_BUF_SIZE || len < 0)
1211 goto out_unlock;
1213 size = sizeof(*entry) + sizeof(u32) * len;
1214 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, flags, pc);
1215 if (!event)
1216 goto out_unlock;
1217 entry = ring_buffer_event_data(event);
1218 entry->ip = ip;
1219 entry->depth = depth;
1220 entry->fmt = fmt;
1222 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1223 ring_buffer_unlock_commit(tr->buffer, event);
1225 out_unlock:
1226 __raw_spin_unlock(&trace_buf_lock);
1227 local_irq_restore(flags);
1229 out:
1230 ftrace_preempt_enable(resched);
1231 unpause_graph_tracing();
1233 return len;
1235 EXPORT_SYMBOL_GPL(trace_vprintk);
1237 enum trace_file_type {
1238 TRACE_FILE_LAT_FMT = 1,
1239 TRACE_FILE_ANNOTATE = 2,
1242 static void trace_iterator_increment(struct trace_iterator *iter)
1244 /* Don't allow ftrace to trace into the ring buffers */
1245 ftrace_disable_cpu();
1247 iter->idx++;
1248 if (iter->buffer_iter[iter->cpu])
1249 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1251 ftrace_enable_cpu();
1254 static struct trace_entry *
1255 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1257 struct ring_buffer_event *event;
1258 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1260 /* Don't allow ftrace to trace into the ring buffers */
1261 ftrace_disable_cpu();
1263 if (buf_iter)
1264 event = ring_buffer_iter_peek(buf_iter, ts);
1265 else
1266 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1268 ftrace_enable_cpu();
1270 return event ? ring_buffer_event_data(event) : NULL;
1273 static struct trace_entry *
1274 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1276 struct ring_buffer *buffer = iter->tr->buffer;
1277 struct trace_entry *ent, *next = NULL;
1278 int cpu_file = iter->cpu_file;
1279 u64 next_ts = 0, ts;
1280 int next_cpu = -1;
1281 int cpu;
1284 * If we are in a per_cpu trace file, don't bother by iterating over
1285 * all cpu and peek directly.
1287 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1288 if (ring_buffer_empty_cpu(buffer, cpu_file))
1289 return NULL;
1290 ent = peek_next_entry(iter, cpu_file, ent_ts);
1291 if (ent_cpu)
1292 *ent_cpu = cpu_file;
1294 return ent;
1297 for_each_tracing_cpu(cpu) {
1299 if (ring_buffer_empty_cpu(buffer, cpu))
1300 continue;
1302 ent = peek_next_entry(iter, cpu, &ts);
1305 * Pick the entry with the smallest timestamp:
1307 if (ent && (!next || ts < next_ts)) {
1308 next = ent;
1309 next_cpu = cpu;
1310 next_ts = ts;
1314 if (ent_cpu)
1315 *ent_cpu = next_cpu;
1317 if (ent_ts)
1318 *ent_ts = next_ts;
1320 return next;
1323 /* Find the next real entry, without updating the iterator itself */
1324 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1325 int *ent_cpu, u64 *ent_ts)
1327 return __find_next_entry(iter, ent_cpu, ent_ts);
1330 /* Find the next real entry, and increment the iterator to the next entry */
1331 static void *find_next_entry_inc(struct trace_iterator *iter)
1333 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1335 if (iter->ent)
1336 trace_iterator_increment(iter);
1338 return iter->ent ? iter : NULL;
1341 static void trace_consume(struct trace_iterator *iter)
1343 /* Don't allow ftrace to trace into the ring buffers */
1344 ftrace_disable_cpu();
1345 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1346 ftrace_enable_cpu();
1349 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1351 struct trace_iterator *iter = m->private;
1352 int i = (int)*pos;
1353 void *ent;
1355 (*pos)++;
1357 /* can't go backwards */
1358 if (iter->idx > i)
1359 return NULL;
1361 if (iter->idx < 0)
1362 ent = find_next_entry_inc(iter);
1363 else
1364 ent = iter;
1366 while (ent && iter->idx < i)
1367 ent = find_next_entry_inc(iter);
1369 iter->pos = *pos;
1371 return ent;
1375 * No necessary locking here. The worst thing which can
1376 * happen is loosing events consumed at the same time
1377 * by a trace_pipe reader.
1378 * Other than that, we don't risk to crash the ring buffer
1379 * because it serializes the readers.
1381 * The current tracer is copied to avoid a global locking
1382 * all around.
1384 static void *s_start(struct seq_file *m, loff_t *pos)
1386 struct trace_iterator *iter = m->private;
1387 static struct tracer *old_tracer;
1388 int cpu_file = iter->cpu_file;
1389 void *p = NULL;
1390 loff_t l = 0;
1391 int cpu;
1393 /* copy the tracer to avoid using a global lock all around */
1394 mutex_lock(&trace_types_lock);
1395 if (unlikely(old_tracer != current_trace && current_trace)) {
1396 old_tracer = current_trace;
1397 *iter->trace = *current_trace;
1399 mutex_unlock(&trace_types_lock);
1401 atomic_inc(&trace_record_cmdline_disabled);
1403 if (*pos != iter->pos) {
1404 iter->ent = NULL;
1405 iter->cpu = 0;
1406 iter->idx = -1;
1408 ftrace_disable_cpu();
1410 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1411 for_each_tracing_cpu(cpu)
1412 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1413 } else
1414 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1417 ftrace_enable_cpu();
1419 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1422 } else {
1423 l = *pos - 1;
1424 p = s_next(m, p, &l);
1427 return p;
1430 static void s_stop(struct seq_file *m, void *p)
1432 atomic_dec(&trace_record_cmdline_disabled);
1435 static void print_lat_help_header(struct seq_file *m)
1437 seq_puts(m, "# _------=> CPU# \n");
1438 seq_puts(m, "# / _-----=> irqs-off \n");
1439 seq_puts(m, "# | / _----=> need-resched \n");
1440 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1441 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1442 seq_puts(m, "# |||| / \n");
1443 seq_puts(m, "# ||||| delay \n");
1444 seq_puts(m, "# cmd pid ||||| time | caller \n");
1445 seq_puts(m, "# \\ / ||||| \\ | / \n");
1448 static void print_func_help_header(struct seq_file *m)
1450 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1451 seq_puts(m, "# | | | | |\n");
1455 static void
1456 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1458 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1459 struct trace_array *tr = iter->tr;
1460 struct trace_array_cpu *data = tr->data[tr->cpu];
1461 struct tracer *type = current_trace;
1462 unsigned long total;
1463 unsigned long entries;
1464 const char *name = "preemption";
1466 if (type)
1467 name = type->name;
1469 entries = ring_buffer_entries(iter->tr->buffer);
1470 total = entries +
1471 ring_buffer_overruns(iter->tr->buffer);
1473 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1474 name, UTS_RELEASE);
1475 seq_puts(m, "# -----------------------------------"
1476 "---------------------------------\n");
1477 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1478 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1479 nsecs_to_usecs(data->saved_latency),
1480 entries,
1481 total,
1482 tr->cpu,
1483 #if defined(CONFIG_PREEMPT_NONE)
1484 "server",
1485 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1486 "desktop",
1487 #elif defined(CONFIG_PREEMPT)
1488 "preempt",
1489 #else
1490 "unknown",
1491 #endif
1492 /* These are reserved for later use */
1493 0, 0, 0, 0);
1494 #ifdef CONFIG_SMP
1495 seq_printf(m, " #P:%d)\n", num_online_cpus());
1496 #else
1497 seq_puts(m, ")\n");
1498 #endif
1499 seq_puts(m, "# -----------------\n");
1500 seq_printf(m, "# | task: %.16s-%d "
1501 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1502 data->comm, data->pid, data->uid, data->nice,
1503 data->policy, data->rt_priority);
1504 seq_puts(m, "# -----------------\n");
1506 if (data->critical_start) {
1507 seq_puts(m, "# => started at: ");
1508 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1509 trace_print_seq(m, &iter->seq);
1510 seq_puts(m, "\n# => ended at: ");
1511 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1512 trace_print_seq(m, &iter->seq);
1513 seq_puts(m, "#\n");
1516 seq_puts(m, "#\n");
1519 static void test_cpu_buff_start(struct trace_iterator *iter)
1521 struct trace_seq *s = &iter->seq;
1523 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1524 return;
1526 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1527 return;
1529 if (cpumask_test_cpu(iter->cpu, iter->started))
1530 return;
1532 cpumask_set_cpu(iter->cpu, iter->started);
1533 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1536 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1538 struct trace_seq *s = &iter->seq;
1539 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1540 struct trace_entry *entry;
1541 struct trace_event *event;
1543 entry = iter->ent;
1545 test_cpu_buff_start(iter);
1547 event = ftrace_find_event(entry->type);
1549 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1550 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1551 if (!trace_print_lat_context(iter))
1552 goto partial;
1553 } else {
1554 if (!trace_print_context(iter))
1555 goto partial;
1559 if (event)
1560 return event->trace(iter, sym_flags);
1562 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1563 goto partial;
1565 return TRACE_TYPE_HANDLED;
1566 partial:
1567 return TRACE_TYPE_PARTIAL_LINE;
1570 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1572 struct trace_seq *s = &iter->seq;
1573 struct trace_entry *entry;
1574 struct trace_event *event;
1576 entry = iter->ent;
1578 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1579 if (!trace_seq_printf(s, "%d %d %llu ",
1580 entry->pid, iter->cpu, iter->ts))
1581 goto partial;
1584 event = ftrace_find_event(entry->type);
1585 if (event)
1586 return event->raw(iter, 0);
1588 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1589 goto partial;
1591 return TRACE_TYPE_HANDLED;
1592 partial:
1593 return TRACE_TYPE_PARTIAL_LINE;
1596 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1598 struct trace_seq *s = &iter->seq;
1599 unsigned char newline = '\n';
1600 struct trace_entry *entry;
1601 struct trace_event *event;
1603 entry = iter->ent;
1605 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1606 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1607 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1608 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1611 event = ftrace_find_event(entry->type);
1612 if (event) {
1613 enum print_line_t ret = event->hex(iter, 0);
1614 if (ret != TRACE_TYPE_HANDLED)
1615 return ret;
1618 SEQ_PUT_FIELD_RET(s, newline);
1620 return TRACE_TYPE_HANDLED;
1623 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1625 struct trace_seq *s = &iter->seq;
1626 struct trace_entry *entry = iter->ent;
1627 struct print_entry *field;
1628 int ret;
1630 trace_assign_type(field, entry);
1632 ret = trace_seq_bprintf(s, field->fmt, field->buf);
1633 if (!ret)
1634 return TRACE_TYPE_PARTIAL_LINE;
1636 return TRACE_TYPE_HANDLED;
1639 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1641 struct trace_seq *s = &iter->seq;
1642 struct trace_entry *entry;
1643 struct trace_event *event;
1645 entry = iter->ent;
1647 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1648 SEQ_PUT_FIELD_RET(s, entry->pid);
1649 SEQ_PUT_FIELD_RET(s, iter->cpu);
1650 SEQ_PUT_FIELD_RET(s, iter->ts);
1653 event = ftrace_find_event(entry->type);
1654 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1657 static int trace_empty(struct trace_iterator *iter)
1659 int cpu;
1661 for_each_tracing_cpu(cpu) {
1662 if (iter->buffer_iter[cpu]) {
1663 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1664 return 0;
1665 } else {
1666 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1667 return 0;
1671 return 1;
1674 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1676 enum print_line_t ret;
1678 if (iter->trace && iter->trace->print_line) {
1679 ret = iter->trace->print_line(iter);
1680 if (ret != TRACE_TYPE_UNHANDLED)
1681 return ret;
1684 if (iter->ent->type == TRACE_PRINT &&
1685 trace_flags & TRACE_ITER_PRINTK &&
1686 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1687 return print_printk_msg_only(iter);
1689 if (trace_flags & TRACE_ITER_BIN)
1690 return print_bin_fmt(iter);
1692 if (trace_flags & TRACE_ITER_HEX)
1693 return print_hex_fmt(iter);
1695 if (trace_flags & TRACE_ITER_RAW)
1696 return print_raw_fmt(iter);
1698 return print_trace_fmt(iter);
1701 static int s_show(struct seq_file *m, void *v)
1703 struct trace_iterator *iter = v;
1705 if (iter->ent == NULL) {
1706 if (iter->tr) {
1707 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1708 seq_puts(m, "#\n");
1710 if (iter->trace && iter->trace->print_header)
1711 iter->trace->print_header(m);
1712 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1713 /* print nothing if the buffers are empty */
1714 if (trace_empty(iter))
1715 return 0;
1716 print_trace_header(m, iter);
1717 if (!(trace_flags & TRACE_ITER_VERBOSE))
1718 print_lat_help_header(m);
1719 } else {
1720 if (!(trace_flags & TRACE_ITER_VERBOSE))
1721 print_func_help_header(m);
1723 } else {
1724 print_trace_line(iter);
1725 trace_print_seq(m, &iter->seq);
1728 return 0;
1731 static struct seq_operations tracer_seq_ops = {
1732 .start = s_start,
1733 .next = s_next,
1734 .stop = s_stop,
1735 .show = s_show,
1738 static struct trace_iterator *
1739 __tracing_open(struct inode *inode, struct file *file)
1741 long cpu_file = (long) inode->i_private;
1742 void *fail_ret = ERR_PTR(-ENOMEM);
1743 struct trace_iterator *iter;
1744 struct seq_file *m;
1745 int cpu, ret;
1747 if (tracing_disabled)
1748 return ERR_PTR(-ENODEV);
1750 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1751 if (!iter)
1752 return ERR_PTR(-ENOMEM);
1755 * We make a copy of the current tracer to avoid concurrent
1756 * changes on it while we are reading.
1758 mutex_lock(&trace_types_lock);
1759 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1760 if (!iter->trace)
1761 goto fail;
1763 if (current_trace)
1764 *iter->trace = *current_trace;
1766 if (current_trace && current_trace->print_max)
1767 iter->tr = &max_tr;
1768 else
1769 iter->tr = &global_trace;
1770 iter->pos = -1;
1771 mutex_init(&iter->mutex);
1772 iter->cpu_file = cpu_file;
1774 /* Notify the tracer early; before we stop tracing. */
1775 if (iter->trace && iter->trace->open)
1776 iter->trace->open(iter);
1778 /* Annotate start of buffers if we had overruns */
1779 if (ring_buffer_overruns(iter->tr->buffer))
1780 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1782 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1783 for_each_tracing_cpu(cpu) {
1785 iter->buffer_iter[cpu] =
1786 ring_buffer_read_start(iter->tr->buffer, cpu);
1788 if (!iter->buffer_iter[cpu])
1789 goto fail_buffer;
1791 } else {
1792 cpu = iter->cpu_file;
1793 iter->buffer_iter[cpu] =
1794 ring_buffer_read_start(iter->tr->buffer, cpu);
1796 if (!iter->buffer_iter[cpu])
1797 goto fail;
1800 /* TODO stop tracer */
1801 ret = seq_open(file, &tracer_seq_ops);
1802 if (ret < 0) {
1803 fail_ret = ERR_PTR(ret);
1804 goto fail_buffer;
1807 m = file->private_data;
1808 m->private = iter;
1810 /* stop the trace while dumping */
1811 tracing_stop();
1813 mutex_unlock(&trace_types_lock);
1815 return iter;
1817 fail_buffer:
1818 for_each_tracing_cpu(cpu) {
1819 if (iter->buffer_iter[cpu])
1820 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1822 fail:
1823 mutex_unlock(&trace_types_lock);
1824 kfree(iter->trace);
1825 kfree(iter);
1827 return fail_ret;
1830 int tracing_open_generic(struct inode *inode, struct file *filp)
1832 if (tracing_disabled)
1833 return -ENODEV;
1835 filp->private_data = inode->i_private;
1836 return 0;
1839 static int tracing_release(struct inode *inode, struct file *file)
1841 struct seq_file *m = (struct seq_file *)file->private_data;
1842 struct trace_iterator *iter = m->private;
1843 int cpu;
1845 mutex_lock(&trace_types_lock);
1846 for_each_tracing_cpu(cpu) {
1847 if (iter->buffer_iter[cpu])
1848 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1851 if (iter->trace && iter->trace->close)
1852 iter->trace->close(iter);
1854 /* reenable tracing if it was previously enabled */
1855 tracing_start();
1856 mutex_unlock(&trace_types_lock);
1858 seq_release(inode, file);
1859 mutex_destroy(&iter->mutex);
1860 kfree(iter->trace);
1861 kfree(iter);
1862 return 0;
1865 static int tracing_open(struct inode *inode, struct file *file)
1867 struct trace_iterator *iter;
1868 int ret = 0;
1870 iter = __tracing_open(inode, file);
1871 if (IS_ERR(iter))
1872 ret = PTR_ERR(iter);
1873 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1874 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1876 return ret;
1879 static void *
1880 t_next(struct seq_file *m, void *v, loff_t *pos)
1882 struct tracer *t = m->private;
1884 (*pos)++;
1886 if (t)
1887 t = t->next;
1889 m->private = t;
1891 return t;
1894 static void *t_start(struct seq_file *m, loff_t *pos)
1896 struct tracer *t = m->private;
1897 loff_t l = 0;
1899 mutex_lock(&trace_types_lock);
1900 for (; t && l < *pos; t = t_next(m, t, &l))
1903 return t;
1906 static void t_stop(struct seq_file *m, void *p)
1908 mutex_unlock(&trace_types_lock);
1911 static int t_show(struct seq_file *m, void *v)
1913 struct tracer *t = v;
1915 if (!t)
1916 return 0;
1918 seq_printf(m, "%s", t->name);
1919 if (t->next)
1920 seq_putc(m, ' ');
1921 else
1922 seq_putc(m, '\n');
1924 return 0;
1927 static struct seq_operations show_traces_seq_ops = {
1928 .start = t_start,
1929 .next = t_next,
1930 .stop = t_stop,
1931 .show = t_show,
1934 static int show_traces_open(struct inode *inode, struct file *file)
1936 int ret;
1938 if (tracing_disabled)
1939 return -ENODEV;
1941 ret = seq_open(file, &show_traces_seq_ops);
1942 if (!ret) {
1943 struct seq_file *m = file->private_data;
1944 m->private = trace_types;
1947 return ret;
1950 static const struct file_operations tracing_fops = {
1951 .open = tracing_open,
1952 .read = seq_read,
1953 .llseek = seq_lseek,
1954 .release = tracing_release,
1957 static const struct file_operations show_traces_fops = {
1958 .open = show_traces_open,
1959 .read = seq_read,
1960 .release = seq_release,
1964 * Only trace on a CPU if the bitmask is set:
1966 static cpumask_var_t tracing_cpumask;
1969 * The tracer itself will not take this lock, but still we want
1970 * to provide a consistent cpumask to user-space:
1972 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1975 * Temporary storage for the character representation of the
1976 * CPU bitmask (and one more byte for the newline):
1978 static char mask_str[NR_CPUS + 1];
1980 static ssize_t
1981 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1982 size_t count, loff_t *ppos)
1984 int len;
1986 mutex_lock(&tracing_cpumask_update_lock);
1988 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1989 if (count - len < 2) {
1990 count = -EINVAL;
1991 goto out_err;
1993 len += sprintf(mask_str + len, "\n");
1994 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1996 out_err:
1997 mutex_unlock(&tracing_cpumask_update_lock);
1999 return count;
2002 static ssize_t
2003 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2004 size_t count, loff_t *ppos)
2006 int err, cpu;
2007 cpumask_var_t tracing_cpumask_new;
2009 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2010 return -ENOMEM;
2012 mutex_lock(&tracing_cpumask_update_lock);
2013 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2014 if (err)
2015 goto err_unlock;
2017 local_irq_disable();
2018 __raw_spin_lock(&ftrace_max_lock);
2019 for_each_tracing_cpu(cpu) {
2021 * Increase/decrease the disabled counter if we are
2022 * about to flip a bit in the cpumask:
2024 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2025 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2026 atomic_inc(&global_trace.data[cpu]->disabled);
2028 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2029 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2030 atomic_dec(&global_trace.data[cpu]->disabled);
2033 __raw_spin_unlock(&ftrace_max_lock);
2034 local_irq_enable();
2036 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2038 mutex_unlock(&tracing_cpumask_update_lock);
2039 free_cpumask_var(tracing_cpumask_new);
2041 return count;
2043 err_unlock:
2044 mutex_unlock(&tracing_cpumask_update_lock);
2045 free_cpumask_var(tracing_cpumask);
2047 return err;
2050 static const struct file_operations tracing_cpumask_fops = {
2051 .open = tracing_open_generic,
2052 .read = tracing_cpumask_read,
2053 .write = tracing_cpumask_write,
2056 static ssize_t
2057 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2058 size_t cnt, loff_t *ppos)
2060 struct tracer_opt *trace_opts;
2061 u32 tracer_flags;
2062 int len = 0;
2063 char *buf;
2064 int r = 0;
2065 int i;
2068 /* calculate max size */
2069 for (i = 0; trace_options[i]; i++) {
2070 len += strlen(trace_options[i]);
2071 len += 3; /* "no" and newline */
2074 mutex_lock(&trace_types_lock);
2075 tracer_flags = current_trace->flags->val;
2076 trace_opts = current_trace->flags->opts;
2079 * Increase the size with names of options specific
2080 * of the current tracer.
2082 for (i = 0; trace_opts[i].name; i++) {
2083 len += strlen(trace_opts[i].name);
2084 len += 3; /* "no" and newline */
2087 /* +2 for \n and \0 */
2088 buf = kmalloc(len + 2, GFP_KERNEL);
2089 if (!buf) {
2090 mutex_unlock(&trace_types_lock);
2091 return -ENOMEM;
2094 for (i = 0; trace_options[i]; i++) {
2095 if (trace_flags & (1 << i))
2096 r += sprintf(buf + r, "%s\n", trace_options[i]);
2097 else
2098 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2101 for (i = 0; trace_opts[i].name; i++) {
2102 if (tracer_flags & trace_opts[i].bit)
2103 r += sprintf(buf + r, "%s\n",
2104 trace_opts[i].name);
2105 else
2106 r += sprintf(buf + r, "no%s\n",
2107 trace_opts[i].name);
2109 mutex_unlock(&trace_types_lock);
2111 WARN_ON(r >= len + 2);
2113 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2115 kfree(buf);
2116 return r;
2119 /* Try to assign a tracer specific option */
2120 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2122 struct tracer_flags *trace_flags = trace->flags;
2123 struct tracer_opt *opts = NULL;
2124 int ret = 0, i = 0;
2125 int len;
2127 for (i = 0; trace_flags->opts[i].name; i++) {
2128 opts = &trace_flags->opts[i];
2129 len = strlen(opts->name);
2131 if (strncmp(cmp, opts->name, len) == 0) {
2132 ret = trace->set_flag(trace_flags->val,
2133 opts->bit, !neg);
2134 break;
2137 /* Not found */
2138 if (!trace_flags->opts[i].name)
2139 return -EINVAL;
2141 /* Refused to handle */
2142 if (ret)
2143 return ret;
2145 if (neg)
2146 trace_flags->val &= ~opts->bit;
2147 else
2148 trace_flags->val |= opts->bit;
2150 return 0;
2153 static ssize_t
2154 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2155 size_t cnt, loff_t *ppos)
2157 char buf[64];
2158 char *cmp = buf;
2159 int neg = 0;
2160 int ret;
2161 int i;
2163 if (cnt >= sizeof(buf))
2164 return -EINVAL;
2166 if (copy_from_user(&buf, ubuf, cnt))
2167 return -EFAULT;
2169 buf[cnt] = 0;
2171 if (strncmp(buf, "no", 2) == 0) {
2172 neg = 1;
2173 cmp += 2;
2176 for (i = 0; trace_options[i]; i++) {
2177 int len = strlen(trace_options[i]);
2179 if (strncmp(cmp, trace_options[i], len) == 0) {
2180 if (neg)
2181 trace_flags &= ~(1 << i);
2182 else
2183 trace_flags |= (1 << i);
2184 break;
2188 /* If no option could be set, test the specific tracer options */
2189 if (!trace_options[i]) {
2190 mutex_lock(&trace_types_lock);
2191 ret = set_tracer_option(current_trace, cmp, neg);
2192 mutex_unlock(&trace_types_lock);
2193 if (ret)
2194 return ret;
2197 filp->f_pos += cnt;
2199 return cnt;
2202 static const struct file_operations tracing_iter_fops = {
2203 .open = tracing_open_generic,
2204 .read = tracing_trace_options_read,
2205 .write = tracing_trace_options_write,
2208 static const char readme_msg[] =
2209 "tracing mini-HOWTO:\n\n"
2210 "# mkdir /debug\n"
2211 "# mount -t debugfs nodev /debug\n\n"
2212 "# cat /debug/tracing/available_tracers\n"
2213 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2214 "# cat /debug/tracing/current_tracer\n"
2215 "none\n"
2216 "# echo sched_switch > /debug/tracing/current_tracer\n"
2217 "# cat /debug/tracing/current_tracer\n"
2218 "sched_switch\n"
2219 "# cat /debug/tracing/trace_options\n"
2220 "noprint-parent nosym-offset nosym-addr noverbose\n"
2221 "# echo print-parent > /debug/tracing/trace_options\n"
2222 "# echo 1 > /debug/tracing/tracing_enabled\n"
2223 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2224 "echo 0 > /debug/tracing/tracing_enabled\n"
2227 static ssize_t
2228 tracing_readme_read(struct file *filp, char __user *ubuf,
2229 size_t cnt, loff_t *ppos)
2231 return simple_read_from_buffer(ubuf, cnt, ppos,
2232 readme_msg, strlen(readme_msg));
2235 static const struct file_operations tracing_readme_fops = {
2236 .open = tracing_open_generic,
2237 .read = tracing_readme_read,
2240 static ssize_t
2241 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2242 size_t cnt, loff_t *ppos)
2244 char buf[64];
2245 int r;
2247 r = sprintf(buf, "%u\n", tracer_enabled);
2248 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2251 static ssize_t
2252 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2253 size_t cnt, loff_t *ppos)
2255 struct trace_array *tr = filp->private_data;
2256 char buf[64];
2257 unsigned long val;
2258 int ret;
2260 if (cnt >= sizeof(buf))
2261 return -EINVAL;
2263 if (copy_from_user(&buf, ubuf, cnt))
2264 return -EFAULT;
2266 buf[cnt] = 0;
2268 ret = strict_strtoul(buf, 10, &val);
2269 if (ret < 0)
2270 return ret;
2272 val = !!val;
2274 mutex_lock(&trace_types_lock);
2275 if (tracer_enabled ^ val) {
2276 if (val) {
2277 tracer_enabled = 1;
2278 if (current_trace->start)
2279 current_trace->start(tr);
2280 tracing_start();
2281 } else {
2282 tracer_enabled = 0;
2283 tracing_stop();
2284 if (current_trace->stop)
2285 current_trace->stop(tr);
2288 mutex_unlock(&trace_types_lock);
2290 filp->f_pos += cnt;
2292 return cnt;
2295 static ssize_t
2296 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2297 size_t cnt, loff_t *ppos)
2299 char buf[max_tracer_type_len+2];
2300 int r;
2302 mutex_lock(&trace_types_lock);
2303 if (current_trace)
2304 r = sprintf(buf, "%s\n", current_trace->name);
2305 else
2306 r = sprintf(buf, "\n");
2307 mutex_unlock(&trace_types_lock);
2309 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2312 int tracer_init(struct tracer *t, struct trace_array *tr)
2314 tracing_reset_online_cpus(tr);
2315 return t->init(tr);
2318 struct trace_option_dentry;
2320 static struct trace_option_dentry *
2321 create_trace_option_files(struct tracer *tracer);
2323 static void
2324 destroy_trace_option_files(struct trace_option_dentry *topts);
2326 static int tracing_set_tracer(const char *buf)
2328 static struct trace_option_dentry *topts;
2329 struct trace_array *tr = &global_trace;
2330 struct tracer *t;
2331 int ret = 0;
2333 mutex_lock(&trace_types_lock);
2334 for (t = trace_types; t; t = t->next) {
2335 if (strcmp(t->name, buf) == 0)
2336 break;
2338 if (!t) {
2339 ret = -EINVAL;
2340 goto out;
2342 if (t == current_trace)
2343 goto out;
2345 trace_branch_disable();
2346 if (current_trace && current_trace->reset)
2347 current_trace->reset(tr);
2349 destroy_trace_option_files(topts);
2351 current_trace = t;
2353 topts = create_trace_option_files(current_trace);
2355 if (t->init) {
2356 ret = tracer_init(t, tr);
2357 if (ret)
2358 goto out;
2361 trace_branch_enable(tr);
2362 out:
2363 mutex_unlock(&trace_types_lock);
2365 return ret;
2368 static ssize_t
2369 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2370 size_t cnt, loff_t *ppos)
2372 char buf[max_tracer_type_len+1];
2373 int i;
2374 size_t ret;
2375 int err;
2377 ret = cnt;
2379 if (cnt > max_tracer_type_len)
2380 cnt = max_tracer_type_len;
2382 if (copy_from_user(&buf, ubuf, cnt))
2383 return -EFAULT;
2385 buf[cnt] = 0;
2387 /* strip ending whitespace. */
2388 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2389 buf[i] = 0;
2391 err = tracing_set_tracer(buf);
2392 if (err)
2393 return err;
2395 filp->f_pos += ret;
2397 return ret;
2400 static ssize_t
2401 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2402 size_t cnt, loff_t *ppos)
2404 unsigned long *ptr = filp->private_data;
2405 char buf[64];
2406 int r;
2408 r = snprintf(buf, sizeof(buf), "%ld\n",
2409 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2410 if (r > sizeof(buf))
2411 r = sizeof(buf);
2412 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2415 static ssize_t
2416 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2417 size_t cnt, loff_t *ppos)
2419 unsigned long *ptr = filp->private_data;
2420 char buf[64];
2421 unsigned long val;
2422 int ret;
2424 if (cnt >= sizeof(buf))
2425 return -EINVAL;
2427 if (copy_from_user(&buf, ubuf, cnt))
2428 return -EFAULT;
2430 buf[cnt] = 0;
2432 ret = strict_strtoul(buf, 10, &val);
2433 if (ret < 0)
2434 return ret;
2436 *ptr = val * 1000;
2438 return cnt;
2441 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2443 long cpu_file = (long) inode->i_private;
2444 struct trace_iterator *iter;
2445 int ret = 0;
2447 if (tracing_disabled)
2448 return -ENODEV;
2450 mutex_lock(&trace_types_lock);
2452 /* We only allow one reader per cpu */
2453 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2454 if (!cpumask_empty(tracing_reader_cpumask)) {
2455 ret = -EBUSY;
2456 goto out;
2458 cpumask_setall(tracing_reader_cpumask);
2459 } else {
2460 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2461 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2462 else {
2463 ret = -EBUSY;
2464 goto out;
2468 /* create a buffer to store the information to pass to userspace */
2469 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2470 if (!iter) {
2471 ret = -ENOMEM;
2472 goto out;
2476 * We make a copy of the current tracer to avoid concurrent
2477 * changes on it while we are reading.
2479 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2480 if (!iter->trace) {
2481 ret = -ENOMEM;
2482 goto fail;
2484 if (current_trace)
2485 *iter->trace = *current_trace;
2487 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2488 ret = -ENOMEM;
2489 goto fail;
2492 /* trace pipe does not show start of buffer */
2493 cpumask_setall(iter->started);
2495 iter->cpu_file = cpu_file;
2496 iter->tr = &global_trace;
2497 mutex_init(&iter->mutex);
2498 filp->private_data = iter;
2500 if (iter->trace->pipe_open)
2501 iter->trace->pipe_open(iter);
2503 out:
2504 mutex_unlock(&trace_types_lock);
2505 return ret;
2507 fail:
2508 kfree(iter->trace);
2509 kfree(iter);
2510 mutex_unlock(&trace_types_lock);
2511 return ret;
2514 static int tracing_release_pipe(struct inode *inode, struct file *file)
2516 struct trace_iterator *iter = file->private_data;
2518 mutex_lock(&trace_types_lock);
2520 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2521 cpumask_clear(tracing_reader_cpumask);
2522 else
2523 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2525 mutex_unlock(&trace_types_lock);
2527 free_cpumask_var(iter->started);
2528 mutex_destroy(&iter->mutex);
2529 kfree(iter->trace);
2530 kfree(iter);
2532 return 0;
2535 static unsigned int
2536 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2538 struct trace_iterator *iter = filp->private_data;
2540 if (trace_flags & TRACE_ITER_BLOCK) {
2542 * Always select as readable when in blocking mode
2544 return POLLIN | POLLRDNORM;
2545 } else {
2546 if (!trace_empty(iter))
2547 return POLLIN | POLLRDNORM;
2548 poll_wait(filp, &trace_wait, poll_table);
2549 if (!trace_empty(iter))
2550 return POLLIN | POLLRDNORM;
2552 return 0;
2557 void default_wait_pipe(struct trace_iterator *iter)
2559 DEFINE_WAIT(wait);
2561 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2563 if (trace_empty(iter))
2564 schedule();
2566 finish_wait(&trace_wait, &wait);
2570 * This is a make-shift waitqueue.
2571 * A tracer might use this callback on some rare cases:
2573 * 1) the current tracer might hold the runqueue lock when it wakes up
2574 * a reader, hence a deadlock (sched, function, and function graph tracers)
2575 * 2) the function tracers, trace all functions, we don't want
2576 * the overhead of calling wake_up and friends
2577 * (and tracing them too)
2579 * Anyway, this is really very primitive wakeup.
2581 void poll_wait_pipe(struct trace_iterator *iter)
2583 set_current_state(TASK_INTERRUPTIBLE);
2584 /* sleep for 100 msecs, and try again. */
2585 schedule_timeout(HZ / 10);
2588 /* Must be called with trace_types_lock mutex held. */
2589 static int tracing_wait_pipe(struct file *filp)
2591 struct trace_iterator *iter = filp->private_data;
2593 while (trace_empty(iter)) {
2595 if ((filp->f_flags & O_NONBLOCK)) {
2596 return -EAGAIN;
2599 mutex_unlock(&iter->mutex);
2601 iter->trace->wait_pipe(iter);
2603 mutex_lock(&iter->mutex);
2605 if (signal_pending(current))
2606 return -EINTR;
2609 * We block until we read something and tracing is disabled.
2610 * We still block if tracing is disabled, but we have never
2611 * read anything. This allows a user to cat this file, and
2612 * then enable tracing. But after we have read something,
2613 * we give an EOF when tracing is again disabled.
2615 * iter->pos will be 0 if we haven't read anything.
2617 if (!tracer_enabled && iter->pos)
2618 break;
2621 return 1;
2625 * Consumer reader.
2627 static ssize_t
2628 tracing_read_pipe(struct file *filp, char __user *ubuf,
2629 size_t cnt, loff_t *ppos)
2631 struct trace_iterator *iter = filp->private_data;
2632 static struct tracer *old_tracer;
2633 ssize_t sret;
2635 /* return any leftover data */
2636 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2637 if (sret != -EBUSY)
2638 return sret;
2640 trace_seq_init(&iter->seq);
2642 /* copy the tracer to avoid using a global lock all around */
2643 mutex_lock(&trace_types_lock);
2644 if (unlikely(old_tracer != current_trace && current_trace)) {
2645 old_tracer = current_trace;
2646 *iter->trace = *current_trace;
2648 mutex_unlock(&trace_types_lock);
2651 * Avoid more than one consumer on a single file descriptor
2652 * This is just a matter of traces coherency, the ring buffer itself
2653 * is protected.
2655 mutex_lock(&iter->mutex);
2656 if (iter->trace->read) {
2657 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2658 if (sret)
2659 goto out;
2662 waitagain:
2663 sret = tracing_wait_pipe(filp);
2664 if (sret <= 0)
2665 goto out;
2667 /* stop when tracing is finished */
2668 if (trace_empty(iter)) {
2669 sret = 0;
2670 goto out;
2673 if (cnt >= PAGE_SIZE)
2674 cnt = PAGE_SIZE - 1;
2676 /* reset all but tr, trace, and overruns */
2677 memset(&iter->seq, 0,
2678 sizeof(struct trace_iterator) -
2679 offsetof(struct trace_iterator, seq));
2680 iter->pos = -1;
2682 while (find_next_entry_inc(iter) != NULL) {
2683 enum print_line_t ret;
2684 int len = iter->seq.len;
2686 ret = print_trace_line(iter);
2687 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2688 /* don't print partial lines */
2689 iter->seq.len = len;
2690 break;
2692 if (ret != TRACE_TYPE_NO_CONSUME)
2693 trace_consume(iter);
2695 if (iter->seq.len >= cnt)
2696 break;
2699 /* Now copy what we have to the user */
2700 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2701 if (iter->seq.readpos >= iter->seq.len)
2702 trace_seq_init(&iter->seq);
2705 * If there was nothing to send to user, inspite of consuming trace
2706 * entries, go back to wait for more entries.
2708 if (sret == -EBUSY)
2709 goto waitagain;
2711 out:
2712 mutex_unlock(&iter->mutex);
2714 return sret;
2717 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2718 struct pipe_buffer *buf)
2720 __free_page(buf->page);
2723 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2724 unsigned int idx)
2726 __free_page(spd->pages[idx]);
2729 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2730 .can_merge = 0,
2731 .map = generic_pipe_buf_map,
2732 .unmap = generic_pipe_buf_unmap,
2733 .confirm = generic_pipe_buf_confirm,
2734 .release = tracing_pipe_buf_release,
2735 .steal = generic_pipe_buf_steal,
2736 .get = generic_pipe_buf_get,
2739 static size_t
2740 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2742 size_t count;
2743 int ret;
2745 /* Seq buffer is page-sized, exactly what we need. */
2746 for (;;) {
2747 count = iter->seq.len;
2748 ret = print_trace_line(iter);
2749 count = iter->seq.len - count;
2750 if (rem < count) {
2751 rem = 0;
2752 iter->seq.len -= count;
2753 break;
2755 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2756 iter->seq.len -= count;
2757 break;
2760 trace_consume(iter);
2761 rem -= count;
2762 if (!find_next_entry_inc(iter)) {
2763 rem = 0;
2764 iter->ent = NULL;
2765 break;
2769 return rem;
2772 static ssize_t tracing_splice_read_pipe(struct file *filp,
2773 loff_t *ppos,
2774 struct pipe_inode_info *pipe,
2775 size_t len,
2776 unsigned int flags)
2778 struct page *pages[PIPE_BUFFERS];
2779 struct partial_page partial[PIPE_BUFFERS];
2780 struct trace_iterator *iter = filp->private_data;
2781 struct splice_pipe_desc spd = {
2782 .pages = pages,
2783 .partial = partial,
2784 .nr_pages = 0, /* This gets updated below. */
2785 .flags = flags,
2786 .ops = &tracing_pipe_buf_ops,
2787 .spd_release = tracing_spd_release_pipe,
2789 static struct tracer *old_tracer;
2790 ssize_t ret;
2791 size_t rem;
2792 unsigned int i;
2794 /* copy the tracer to avoid using a global lock all around */
2795 mutex_lock(&trace_types_lock);
2796 if (unlikely(old_tracer != current_trace && current_trace)) {
2797 old_tracer = current_trace;
2798 *iter->trace = *current_trace;
2800 mutex_unlock(&trace_types_lock);
2802 mutex_lock(&iter->mutex);
2804 if (iter->trace->splice_read) {
2805 ret = iter->trace->splice_read(iter, filp,
2806 ppos, pipe, len, flags);
2807 if (ret)
2808 goto out_err;
2811 ret = tracing_wait_pipe(filp);
2812 if (ret <= 0)
2813 goto out_err;
2815 if (!iter->ent && !find_next_entry_inc(iter)) {
2816 ret = -EFAULT;
2817 goto out_err;
2820 /* Fill as many pages as possible. */
2821 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2822 pages[i] = alloc_page(GFP_KERNEL);
2823 if (!pages[i])
2824 break;
2826 rem = tracing_fill_pipe_page(rem, iter);
2828 /* Copy the data into the page, so we can start over. */
2829 ret = trace_seq_to_buffer(&iter->seq,
2830 page_address(pages[i]),
2831 iter->seq.len);
2832 if (ret < 0) {
2833 __free_page(pages[i]);
2834 break;
2836 partial[i].offset = 0;
2837 partial[i].len = iter->seq.len;
2839 trace_seq_init(&iter->seq);
2842 mutex_unlock(&iter->mutex);
2844 spd.nr_pages = i;
2846 return splice_to_pipe(pipe, &spd);
2848 out_err:
2849 mutex_unlock(&iter->mutex);
2851 return ret;
2854 static ssize_t
2855 tracing_entries_read(struct file *filp, char __user *ubuf,
2856 size_t cnt, loff_t *ppos)
2858 struct trace_array *tr = filp->private_data;
2859 char buf[64];
2860 int r;
2862 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2863 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2866 static ssize_t
2867 tracing_entries_write(struct file *filp, const char __user *ubuf,
2868 size_t cnt, loff_t *ppos)
2870 unsigned long val;
2871 char buf[64];
2872 int ret, cpu;
2874 if (cnt >= sizeof(buf))
2875 return -EINVAL;
2877 if (copy_from_user(&buf, ubuf, cnt))
2878 return -EFAULT;
2880 buf[cnt] = 0;
2882 ret = strict_strtoul(buf, 10, &val);
2883 if (ret < 0)
2884 return ret;
2886 /* must have at least 1 entry */
2887 if (!val)
2888 return -EINVAL;
2890 mutex_lock(&trace_types_lock);
2892 tracing_stop();
2894 /* disable all cpu buffers */
2895 for_each_tracing_cpu(cpu) {
2896 if (global_trace.data[cpu])
2897 atomic_inc(&global_trace.data[cpu]->disabled);
2898 if (max_tr.data[cpu])
2899 atomic_inc(&max_tr.data[cpu]->disabled);
2902 /* value is in KB */
2903 val <<= 10;
2905 if (val != global_trace.entries) {
2906 ret = ring_buffer_resize(global_trace.buffer, val);
2907 if (ret < 0) {
2908 cnt = ret;
2909 goto out;
2912 ret = ring_buffer_resize(max_tr.buffer, val);
2913 if (ret < 0) {
2914 int r;
2915 cnt = ret;
2916 r = ring_buffer_resize(global_trace.buffer,
2917 global_trace.entries);
2918 if (r < 0) {
2919 /* AARGH! We are left with different
2920 * size max buffer!!!! */
2921 WARN_ON(1);
2922 tracing_disabled = 1;
2924 goto out;
2927 global_trace.entries = val;
2930 filp->f_pos += cnt;
2932 /* If check pages failed, return ENOMEM */
2933 if (tracing_disabled)
2934 cnt = -ENOMEM;
2935 out:
2936 for_each_tracing_cpu(cpu) {
2937 if (global_trace.data[cpu])
2938 atomic_dec(&global_trace.data[cpu]->disabled);
2939 if (max_tr.data[cpu])
2940 atomic_dec(&max_tr.data[cpu]->disabled);
2943 tracing_start();
2944 max_tr.entries = global_trace.entries;
2945 mutex_unlock(&trace_types_lock);
2947 return cnt;
2950 static int mark_printk(const char *fmt, ...)
2952 int ret;
2953 va_list args;
2954 va_start(args, fmt);
2955 ret = trace_vprintk(0, -1, fmt, args);
2956 va_end(args);
2957 return ret;
2960 static ssize_t
2961 tracing_mark_write(struct file *filp, const char __user *ubuf,
2962 size_t cnt, loff_t *fpos)
2964 char *buf;
2965 char *end;
2967 if (tracing_disabled)
2968 return -EINVAL;
2970 if (cnt > TRACE_BUF_SIZE)
2971 cnt = TRACE_BUF_SIZE;
2973 buf = kmalloc(cnt + 1, GFP_KERNEL);
2974 if (buf == NULL)
2975 return -ENOMEM;
2977 if (copy_from_user(buf, ubuf, cnt)) {
2978 kfree(buf);
2979 return -EFAULT;
2982 /* Cut from the first nil or newline. */
2983 buf[cnt] = '\0';
2984 end = strchr(buf, '\n');
2985 if (end)
2986 *end = '\0';
2988 cnt = mark_printk("%s\n", buf);
2989 kfree(buf);
2990 *fpos += cnt;
2992 return cnt;
2995 static const struct file_operations tracing_max_lat_fops = {
2996 .open = tracing_open_generic,
2997 .read = tracing_max_lat_read,
2998 .write = tracing_max_lat_write,
3001 static const struct file_operations tracing_ctrl_fops = {
3002 .open = tracing_open_generic,
3003 .read = tracing_ctrl_read,
3004 .write = tracing_ctrl_write,
3007 static const struct file_operations set_tracer_fops = {
3008 .open = tracing_open_generic,
3009 .read = tracing_set_trace_read,
3010 .write = tracing_set_trace_write,
3013 static const struct file_operations tracing_pipe_fops = {
3014 .open = tracing_open_pipe,
3015 .poll = tracing_poll_pipe,
3016 .read = tracing_read_pipe,
3017 .splice_read = tracing_splice_read_pipe,
3018 .release = tracing_release_pipe,
3021 static const struct file_operations tracing_entries_fops = {
3022 .open = tracing_open_generic,
3023 .read = tracing_entries_read,
3024 .write = tracing_entries_write,
3027 static const struct file_operations tracing_mark_fops = {
3028 .open = tracing_open_generic,
3029 .write = tracing_mark_write,
3032 struct ftrace_buffer_info {
3033 struct trace_array *tr;
3034 void *spare;
3035 int cpu;
3036 unsigned int read;
3039 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3041 int cpu = (int)(long)inode->i_private;
3042 struct ftrace_buffer_info *info;
3044 if (tracing_disabled)
3045 return -ENODEV;
3047 info = kzalloc(sizeof(*info), GFP_KERNEL);
3048 if (!info)
3049 return -ENOMEM;
3051 info->tr = &global_trace;
3052 info->cpu = cpu;
3053 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3054 /* Force reading ring buffer for first read */
3055 info->read = (unsigned int)-1;
3056 if (!info->spare)
3057 goto out;
3059 filp->private_data = info;
3061 return 0;
3063 out:
3064 kfree(info);
3065 return -ENOMEM;
3068 static ssize_t
3069 tracing_buffers_read(struct file *filp, char __user *ubuf,
3070 size_t count, loff_t *ppos)
3072 struct ftrace_buffer_info *info = filp->private_data;
3073 unsigned int pos;
3074 ssize_t ret;
3075 size_t size;
3077 if (!count)
3078 return 0;
3080 /* Do we have previous read data to read? */
3081 if (info->read < PAGE_SIZE)
3082 goto read;
3084 info->read = 0;
3086 ret = ring_buffer_read_page(info->tr->buffer,
3087 &info->spare,
3088 count,
3089 info->cpu, 0);
3090 if (ret < 0)
3091 return 0;
3093 pos = ring_buffer_page_len(info->spare);
3095 if (pos < PAGE_SIZE)
3096 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3098 read:
3099 size = PAGE_SIZE - info->read;
3100 if (size > count)
3101 size = count;
3103 ret = copy_to_user(ubuf, info->spare + info->read, size);
3104 if (ret == size)
3105 return -EFAULT;
3106 size -= ret;
3108 *ppos += size;
3109 info->read += size;
3111 return size;
3114 static int tracing_buffers_release(struct inode *inode, struct file *file)
3116 struct ftrace_buffer_info *info = file->private_data;
3118 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3119 kfree(info);
3121 return 0;
3124 struct buffer_ref {
3125 struct ring_buffer *buffer;
3126 void *page;
3127 int ref;
3130 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3131 struct pipe_buffer *buf)
3133 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3135 if (--ref->ref)
3136 return;
3138 ring_buffer_free_read_page(ref->buffer, ref->page);
3139 kfree(ref);
3140 buf->private = 0;
3143 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3144 struct pipe_buffer *buf)
3146 return 1;
3149 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3150 struct pipe_buffer *buf)
3152 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3154 ref->ref++;
3157 /* Pipe buffer operations for a buffer. */
3158 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3159 .can_merge = 0,
3160 .map = generic_pipe_buf_map,
3161 .unmap = generic_pipe_buf_unmap,
3162 .confirm = generic_pipe_buf_confirm,
3163 .release = buffer_pipe_buf_release,
3164 .steal = buffer_pipe_buf_steal,
3165 .get = buffer_pipe_buf_get,
3169 * Callback from splice_to_pipe(), if we need to release some pages
3170 * at the end of the spd in case we error'ed out in filling the pipe.
3172 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3174 struct buffer_ref *ref =
3175 (struct buffer_ref *)spd->partial[i].private;
3177 if (--ref->ref)
3178 return;
3180 ring_buffer_free_read_page(ref->buffer, ref->page);
3181 kfree(ref);
3182 spd->partial[i].private = 0;
3185 static ssize_t
3186 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3187 struct pipe_inode_info *pipe, size_t len,
3188 unsigned int flags)
3190 struct ftrace_buffer_info *info = file->private_data;
3191 struct partial_page partial[PIPE_BUFFERS];
3192 struct page *pages[PIPE_BUFFERS];
3193 struct splice_pipe_desc spd = {
3194 .pages = pages,
3195 .partial = partial,
3196 .flags = flags,
3197 .ops = &buffer_pipe_buf_ops,
3198 .spd_release = buffer_spd_release,
3200 struct buffer_ref *ref;
3201 int size, i;
3202 size_t ret;
3205 * We can't seek on a buffer input
3207 if (unlikely(*ppos))
3208 return -ESPIPE;
3211 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3212 struct page *page;
3213 int r;
3215 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3216 if (!ref)
3217 break;
3219 ref->buffer = info->tr->buffer;
3220 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3221 if (!ref->page) {
3222 kfree(ref);
3223 break;
3226 r = ring_buffer_read_page(ref->buffer, &ref->page,
3227 len, info->cpu, 0);
3228 if (r < 0) {
3229 ring_buffer_free_read_page(ref->buffer,
3230 ref->page);
3231 kfree(ref);
3232 break;
3236 * zero out any left over data, this is going to
3237 * user land.
3239 size = ring_buffer_page_len(ref->page);
3240 if (size < PAGE_SIZE)
3241 memset(ref->page + size, 0, PAGE_SIZE - size);
3243 page = virt_to_page(ref->page);
3245 spd.pages[i] = page;
3246 spd.partial[i].len = PAGE_SIZE;
3247 spd.partial[i].offset = 0;
3248 spd.partial[i].private = (unsigned long)ref;
3249 spd.nr_pages++;
3252 spd.nr_pages = i;
3254 /* did we read anything? */
3255 if (!spd.nr_pages) {
3256 if (flags & SPLICE_F_NONBLOCK)
3257 ret = -EAGAIN;
3258 else
3259 ret = 0;
3260 /* TODO: block */
3261 return ret;
3264 ret = splice_to_pipe(pipe, &spd);
3266 return ret;
3269 static const struct file_operations tracing_buffers_fops = {
3270 .open = tracing_buffers_open,
3271 .read = tracing_buffers_read,
3272 .release = tracing_buffers_release,
3273 .splice_read = tracing_buffers_splice_read,
3274 .llseek = no_llseek,
3277 #ifdef CONFIG_DYNAMIC_FTRACE
3279 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3281 return 0;
3284 static ssize_t
3285 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3286 size_t cnt, loff_t *ppos)
3288 static char ftrace_dyn_info_buffer[1024];
3289 static DEFINE_MUTEX(dyn_info_mutex);
3290 unsigned long *p = filp->private_data;
3291 char *buf = ftrace_dyn_info_buffer;
3292 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3293 int r;
3295 mutex_lock(&dyn_info_mutex);
3296 r = sprintf(buf, "%ld ", *p);
3298 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3299 buf[r++] = '\n';
3301 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3303 mutex_unlock(&dyn_info_mutex);
3305 return r;
3308 static const struct file_operations tracing_dyn_info_fops = {
3309 .open = tracing_open_generic,
3310 .read = tracing_read_dyn_info,
3312 #endif
3314 static struct dentry *d_tracer;
3316 struct dentry *tracing_init_dentry(void)
3318 static int once;
3320 if (d_tracer)
3321 return d_tracer;
3323 d_tracer = debugfs_create_dir("tracing", NULL);
3325 if (!d_tracer && !once) {
3326 once = 1;
3327 pr_warning("Could not create debugfs directory 'tracing'\n");
3328 return NULL;
3331 return d_tracer;
3334 static struct dentry *d_percpu;
3336 struct dentry *tracing_dentry_percpu(void)
3338 static int once;
3339 struct dentry *d_tracer;
3341 if (d_percpu)
3342 return d_percpu;
3344 d_tracer = tracing_init_dentry();
3346 if (!d_tracer)
3347 return NULL;
3349 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3351 if (!d_percpu && !once) {
3352 once = 1;
3353 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3354 return NULL;
3357 return d_percpu;
3360 static void tracing_init_debugfs_percpu(long cpu)
3362 struct dentry *d_percpu = tracing_dentry_percpu();
3363 struct dentry *entry, *d_cpu;
3364 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3365 char cpu_dir[7];
3367 if (cpu > 999 || cpu < 0)
3368 return;
3370 sprintf(cpu_dir, "cpu%ld", cpu);
3371 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3372 if (!d_cpu) {
3373 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3374 return;
3377 /* per cpu trace_pipe */
3378 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
3379 (void *) cpu, &tracing_pipe_fops);
3380 if (!entry)
3381 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3383 /* per cpu trace */
3384 entry = debugfs_create_file("trace", 0444, d_cpu,
3385 (void *) cpu, &tracing_fops);
3386 if (!entry)
3387 pr_warning("Could not create debugfs 'trace' entry\n");
3390 #ifdef CONFIG_FTRACE_SELFTEST
3391 /* Let selftest have access to static functions in this file */
3392 #include "trace_selftest.c"
3393 #endif
3395 struct trace_option_dentry {
3396 struct tracer_opt *opt;
3397 struct tracer_flags *flags;
3398 struct dentry *entry;
3401 static ssize_t
3402 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3403 loff_t *ppos)
3405 struct trace_option_dentry *topt = filp->private_data;
3406 char *buf;
3408 if (topt->flags->val & topt->opt->bit)
3409 buf = "1\n";
3410 else
3411 buf = "0\n";
3413 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3416 static ssize_t
3417 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3418 loff_t *ppos)
3420 struct trace_option_dentry *topt = filp->private_data;
3421 unsigned long val;
3422 char buf[64];
3423 int ret;
3425 if (cnt >= sizeof(buf))
3426 return -EINVAL;
3428 if (copy_from_user(&buf, ubuf, cnt))
3429 return -EFAULT;
3431 buf[cnt] = 0;
3433 ret = strict_strtoul(buf, 10, &val);
3434 if (ret < 0)
3435 return ret;
3437 ret = 0;
3438 switch (val) {
3439 case 0:
3440 /* do nothing if already cleared */
3441 if (!(topt->flags->val & topt->opt->bit))
3442 break;
3444 mutex_lock(&trace_types_lock);
3445 if (current_trace->set_flag)
3446 ret = current_trace->set_flag(topt->flags->val,
3447 topt->opt->bit, 0);
3448 mutex_unlock(&trace_types_lock);
3449 if (ret)
3450 return ret;
3451 topt->flags->val &= ~topt->opt->bit;
3452 break;
3453 case 1:
3454 /* do nothing if already set */
3455 if (topt->flags->val & topt->opt->bit)
3456 break;
3458 mutex_lock(&trace_types_lock);
3459 if (current_trace->set_flag)
3460 ret = current_trace->set_flag(topt->flags->val,
3461 topt->opt->bit, 1);
3462 mutex_unlock(&trace_types_lock);
3463 if (ret)
3464 return ret;
3465 topt->flags->val |= topt->opt->bit;
3466 break;
3468 default:
3469 return -EINVAL;
3472 *ppos += cnt;
3474 return cnt;
3478 static const struct file_operations trace_options_fops = {
3479 .open = tracing_open_generic,
3480 .read = trace_options_read,
3481 .write = trace_options_write,
3484 static ssize_t
3485 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3486 loff_t *ppos)
3488 long index = (long)filp->private_data;
3489 char *buf;
3491 if (trace_flags & (1 << index))
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_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3501 loff_t *ppos)
3503 long index = (long)filp->private_data;
3504 char buf[64];
3505 unsigned long val;
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 switch (val) {
3521 case 0:
3522 trace_flags &= ~(1 << index);
3523 break;
3524 case 1:
3525 trace_flags |= 1 << index;
3526 break;
3528 default:
3529 return -EINVAL;
3532 *ppos += cnt;
3534 return cnt;
3537 static const struct file_operations trace_options_core_fops = {
3538 .open = tracing_open_generic,
3539 .read = trace_options_core_read,
3540 .write = trace_options_core_write,
3543 static struct dentry *trace_options_init_dentry(void)
3545 struct dentry *d_tracer;
3546 static struct dentry *t_options;
3548 if (t_options)
3549 return t_options;
3551 d_tracer = tracing_init_dentry();
3552 if (!d_tracer)
3553 return NULL;
3555 t_options = debugfs_create_dir("options", d_tracer);
3556 if (!t_options) {
3557 pr_warning("Could not create debugfs directory 'options'\n");
3558 return NULL;
3561 return t_options;
3564 static void
3565 create_trace_option_file(struct trace_option_dentry *topt,
3566 struct tracer_flags *flags,
3567 struct tracer_opt *opt)
3569 struct dentry *t_options;
3570 struct dentry *entry;
3572 t_options = trace_options_init_dentry();
3573 if (!t_options)
3574 return;
3576 topt->flags = flags;
3577 topt->opt = opt;
3579 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3580 &trace_options_fops);
3582 topt->entry = entry;
3586 static struct trace_option_dentry *
3587 create_trace_option_files(struct tracer *tracer)
3589 struct trace_option_dentry *topts;
3590 struct tracer_flags *flags;
3591 struct tracer_opt *opts;
3592 int cnt;
3594 if (!tracer)
3595 return NULL;
3597 flags = tracer->flags;
3599 if (!flags || !flags->opts)
3600 return NULL;
3602 opts = flags->opts;
3604 for (cnt = 0; opts[cnt].name; cnt++)
3607 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3608 if (!topts)
3609 return NULL;
3611 for (cnt = 0; opts[cnt].name; cnt++)
3612 create_trace_option_file(&topts[cnt], flags,
3613 &opts[cnt]);
3615 return topts;
3618 static void
3619 destroy_trace_option_files(struct trace_option_dentry *topts)
3621 int cnt;
3623 if (!topts)
3624 return;
3626 for (cnt = 0; topts[cnt].opt; cnt++) {
3627 if (topts[cnt].entry)
3628 debugfs_remove(topts[cnt].entry);
3631 kfree(topts);
3634 static struct dentry *
3635 create_trace_option_core_file(const char *option, long index)
3637 struct dentry *t_options;
3638 struct dentry *entry;
3640 t_options = trace_options_init_dentry();
3641 if (!t_options)
3642 return NULL;
3644 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3645 &trace_options_core_fops);
3647 return entry;
3650 static __init void create_trace_options_dir(void)
3652 struct dentry *t_options;
3653 struct dentry *entry;
3654 int i;
3656 t_options = trace_options_init_dentry();
3657 if (!t_options)
3658 return;
3660 for (i = 0; trace_options[i]; i++) {
3661 entry = create_trace_option_core_file(trace_options[i], i);
3662 if (!entry)
3663 pr_warning("Could not create debugfs %s entry\n",
3664 trace_options[i]);
3668 static __init int tracer_init_debugfs(void)
3670 struct dentry *d_tracer;
3671 struct dentry *buffers;
3672 struct dentry *entry;
3673 int cpu;
3675 d_tracer = tracing_init_dentry();
3677 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3678 &global_trace, &tracing_ctrl_fops);
3679 if (!entry)
3680 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3682 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3683 NULL, &tracing_iter_fops);
3684 if (!entry)
3685 pr_warning("Could not create debugfs 'trace_options' entry\n");
3687 create_trace_options_dir();
3689 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3690 NULL, &tracing_cpumask_fops);
3691 if (!entry)
3692 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3694 entry = debugfs_create_file("trace", 0444, d_tracer,
3695 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3696 if (!entry)
3697 pr_warning("Could not create debugfs 'trace' entry\n");
3699 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3700 &global_trace, &show_traces_fops);
3701 if (!entry)
3702 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3704 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3705 &global_trace, &set_tracer_fops);
3706 if (!entry)
3707 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3709 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3710 &tracing_max_latency,
3711 &tracing_max_lat_fops);
3712 if (!entry)
3713 pr_warning("Could not create debugfs "
3714 "'tracing_max_latency' entry\n");
3716 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3717 &tracing_thresh, &tracing_max_lat_fops);
3718 if (!entry)
3719 pr_warning("Could not create debugfs "
3720 "'tracing_thresh' entry\n");
3721 entry = debugfs_create_file("README", 0644, d_tracer,
3722 NULL, &tracing_readme_fops);
3723 if (!entry)
3724 pr_warning("Could not create debugfs 'README' entry\n");
3726 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3727 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3728 if (!entry)
3729 pr_warning("Could not create debugfs "
3730 "'trace_pipe' entry\n");
3732 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3733 &global_trace, &tracing_entries_fops);
3734 if (!entry)
3735 pr_warning("Could not create debugfs "
3736 "'buffer_size_kb' entry\n");
3738 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3739 NULL, &tracing_mark_fops);
3740 if (!entry)
3741 pr_warning("Could not create debugfs "
3742 "'trace_marker' entry\n");
3744 buffers = debugfs_create_dir("binary_buffers", d_tracer);
3746 if (!buffers)
3747 pr_warning("Could not create buffers directory\n");
3748 else {
3749 int cpu;
3750 char buf[64];
3752 for_each_tracing_cpu(cpu) {
3753 sprintf(buf, "%d", cpu);
3755 entry = debugfs_create_file(buf, 0444, buffers,
3756 (void *)(long)cpu,
3757 &tracing_buffers_fops);
3758 if (!entry)
3759 pr_warning("Could not create debugfs buffers "
3760 "'%s' entry\n", buf);
3764 #ifdef CONFIG_DYNAMIC_FTRACE
3765 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3766 &ftrace_update_tot_cnt,
3767 &tracing_dyn_info_fops);
3768 if (!entry)
3769 pr_warning("Could not create debugfs "
3770 "'dyn_ftrace_total_info' entry\n");
3771 #endif
3772 #ifdef CONFIG_SYSPROF_TRACER
3773 init_tracer_sysprof_debugfs(d_tracer);
3774 #endif
3776 for_each_tracing_cpu(cpu)
3777 tracing_init_debugfs_percpu(cpu);
3779 return 0;
3782 static int trace_panic_handler(struct notifier_block *this,
3783 unsigned long event, void *unused)
3785 if (ftrace_dump_on_oops)
3786 ftrace_dump();
3787 return NOTIFY_OK;
3790 static struct notifier_block trace_panic_notifier = {
3791 .notifier_call = trace_panic_handler,
3792 .next = NULL,
3793 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3796 static int trace_die_handler(struct notifier_block *self,
3797 unsigned long val,
3798 void *data)
3800 switch (val) {
3801 case DIE_OOPS:
3802 if (ftrace_dump_on_oops)
3803 ftrace_dump();
3804 break;
3805 default:
3806 break;
3808 return NOTIFY_OK;
3811 static struct notifier_block trace_die_notifier = {
3812 .notifier_call = trace_die_handler,
3813 .priority = 200
3817 * printk is set to max of 1024, we really don't need it that big.
3818 * Nothing should be printing 1000 characters anyway.
3820 #define TRACE_MAX_PRINT 1000
3823 * Define here KERN_TRACE so that we have one place to modify
3824 * it if we decide to change what log level the ftrace dump
3825 * should be at.
3827 #define KERN_TRACE KERN_EMERG
3829 static void
3830 trace_printk_seq(struct trace_seq *s)
3832 /* Probably should print a warning here. */
3833 if (s->len >= 1000)
3834 s->len = 1000;
3836 /* should be zero ended, but we are paranoid. */
3837 s->buffer[s->len] = 0;
3839 printk(KERN_TRACE "%s", s->buffer);
3841 trace_seq_init(s);
3844 void ftrace_dump(void)
3846 static DEFINE_SPINLOCK(ftrace_dump_lock);
3847 /* use static because iter can be a bit big for the stack */
3848 static struct trace_iterator iter;
3849 static int dump_ran;
3850 unsigned long flags;
3851 int cnt = 0, cpu;
3853 /* only one dump */
3854 spin_lock_irqsave(&ftrace_dump_lock, flags);
3855 if (dump_ran)
3856 goto out;
3858 dump_ran = 1;
3860 /* No turning back! */
3861 tracing_off();
3862 ftrace_kill();
3864 for_each_tracing_cpu(cpu) {
3865 atomic_inc(&global_trace.data[cpu]->disabled);
3868 /* don't look at user memory in panic mode */
3869 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3871 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3873 /* Simulate the iterator */
3874 iter.tr = &global_trace;
3875 iter.trace = current_trace;
3876 iter.cpu_file = TRACE_PIPE_ALL_CPU;
3879 * We need to stop all tracing on all CPUS to read the
3880 * the next buffer. This is a bit expensive, but is
3881 * not done often. We fill all what we can read,
3882 * and then release the locks again.
3885 while (!trace_empty(&iter)) {
3887 if (!cnt)
3888 printk(KERN_TRACE "---------------------------------\n");
3890 cnt++;
3892 /* reset all but tr, trace, and overruns */
3893 memset(&iter.seq, 0,
3894 sizeof(struct trace_iterator) -
3895 offsetof(struct trace_iterator, seq));
3896 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3897 iter.pos = -1;
3899 if (find_next_entry_inc(&iter) != NULL) {
3900 print_trace_line(&iter);
3901 trace_consume(&iter);
3904 trace_printk_seq(&iter.seq);
3907 if (!cnt)
3908 printk(KERN_TRACE " (ftrace buffer empty)\n");
3909 else
3910 printk(KERN_TRACE "---------------------------------\n");
3912 out:
3913 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3916 __init static int tracer_alloc_buffers(void)
3918 struct trace_array_cpu *data;
3919 int i;
3920 int ret = -ENOMEM;
3922 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3923 goto out;
3925 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3926 goto out_free_buffer_mask;
3928 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
3929 goto out_free_tracing_cpumask;
3931 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3932 cpumask_copy(tracing_cpumask, cpu_all_mask);
3933 cpumask_clear(tracing_reader_cpumask);
3935 /* TODO: make the number of buffers hot pluggable with CPUS */
3936 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3937 TRACE_BUFFER_FLAGS);
3938 if (!global_trace.buffer) {
3939 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3940 WARN_ON(1);
3941 goto out_free_cpumask;
3943 global_trace.entries = ring_buffer_size(global_trace.buffer);
3946 #ifdef CONFIG_TRACER_MAX_TRACE
3947 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3948 TRACE_BUFFER_FLAGS);
3949 if (!max_tr.buffer) {
3950 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3951 WARN_ON(1);
3952 ring_buffer_free(global_trace.buffer);
3953 goto out_free_cpumask;
3955 max_tr.entries = ring_buffer_size(max_tr.buffer);
3956 WARN_ON(max_tr.entries != global_trace.entries);
3957 #endif
3959 /* Allocate the first page for all buffers */
3960 for_each_tracing_cpu(i) {
3961 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3962 max_tr.data[i] = &per_cpu(max_data, i);
3965 trace_init_cmdlines();
3967 register_tracer(&nop_trace);
3968 current_trace = &nop_trace;
3969 #ifdef CONFIG_BOOT_TRACER
3970 register_tracer(&boot_tracer);
3971 #endif
3972 /* All seems OK, enable tracing */
3973 tracing_disabled = 0;
3975 atomic_notifier_chain_register(&panic_notifier_list,
3976 &trace_panic_notifier);
3978 register_die_notifier(&trace_die_notifier);
3979 ret = 0;
3981 out_free_cpumask:
3982 free_cpumask_var(tracing_reader_cpumask);
3983 out_free_tracing_cpumask:
3984 free_cpumask_var(tracing_cpumask);
3985 out_free_buffer_mask:
3986 free_cpumask_var(tracing_buffer_mask);
3987 out:
3988 return ret;
3991 __init static int clear_boot_tracer(void)
3994 * The default tracer at boot buffer is an init section.
3995 * This function is called in lateinit. If we did not
3996 * find the boot tracer, then clear it out, to prevent
3997 * later registration from accessing the buffer that is
3998 * about to be freed.
4000 if (!default_bootup_tracer)
4001 return 0;
4003 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4004 default_bootup_tracer);
4005 default_bootup_tracer = NULL;
4007 return 0;
4010 early_initcall(tracer_alloc_buffers);
4011 fs_initcall(tracer_init_debugfs);
4012 late_initcall(clear_boot_tracer);