tty-ldisc: be more careful in 'put_ldisc' locking
[linux-2.6/verdex.git] / kernel / trace / trace.c
blob8bc8d8afea6acd9f6dca1131dcd6b8ca3e2084d8
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/smp_lock.h>
21 #include <linux/notifier.h>
22 #include <linux/irqflags.h>
23 #include <linux/debugfs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/kprobes.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/ctype.h>
36 #include <linux/init.h>
37 #include <linux/poll.h>
38 #include <linux/gfp.h>
39 #include <linux/fs.h>
41 #include "trace.h"
42 #include "trace_output.h"
44 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
46 unsigned long __read_mostly tracing_max_latency;
47 unsigned long __read_mostly tracing_thresh;
50 * On boot up, the ring buffer is set to the minimum size, so that
51 * we do not waste memory on systems that are not using tracing.
53 static int ring_buffer_expanded;
56 * We need to change this state when a selftest is running.
57 * A selftest will lurk into the ring-buffer to count the
58 * entries inserted during the selftest although some concurrent
59 * insertions into the ring-buffer such as trace_printk could occurred
60 * at the same time, giving false positive or negative results.
62 static bool __read_mostly tracing_selftest_running;
65 * If a tracer is running, we do not want to run SELFTEST.
67 static bool __read_mostly tracing_selftest_disabled;
69 /* For tracers that don't implement custom flags */
70 static struct tracer_opt dummy_tracer_opt[] = {
71 { }
74 static struct tracer_flags dummy_tracer_flags = {
75 .val = 0,
76 .opts = dummy_tracer_opt
79 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
81 return 0;
85 * Kill all tracing for good (never come back).
86 * It is initialized to 1 but will turn to zero if the initialization
87 * of the tracer is successful. But that is the only place that sets
88 * this back to zero.
90 static int tracing_disabled = 1;
92 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
94 static inline void ftrace_disable_cpu(void)
96 preempt_disable();
97 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
100 static inline void ftrace_enable_cpu(void)
102 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
103 preempt_enable();
106 static cpumask_var_t __read_mostly tracing_buffer_mask;
108 /* Define which cpu buffers are currently read in trace_pipe */
109 static cpumask_var_t tracing_reader_cpumask;
111 #define for_each_tracing_cpu(cpu) \
112 for_each_cpu(cpu, tracing_buffer_mask)
115 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
117 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
118 * is set, then ftrace_dump is called. This will output the contents
119 * of the ftrace buffers to the console. This is very useful for
120 * capturing traces that lead to crashes and outputing it to a
121 * serial console.
123 * It is default off, but you can enable it with either specifying
124 * "ftrace_dump_on_oops" in the kernel command line, or setting
125 * /proc/sys/kernel/ftrace_dump_on_oops to true.
127 int ftrace_dump_on_oops;
129 static int tracing_set_tracer(const char *buf);
131 #define BOOTUP_TRACER_SIZE 100
132 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
133 static char *default_bootup_tracer;
135 static int __init set_ftrace(char *str)
137 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
138 default_bootup_tracer = bootup_tracer_buf;
139 /* We are using ftrace early, expand it */
140 ring_buffer_expanded = 1;
141 return 1;
143 __setup("ftrace=", set_ftrace);
145 static int __init set_ftrace_dump_on_oops(char *str)
147 ftrace_dump_on_oops = 1;
148 return 1;
150 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
152 unsigned long long ns2usecs(cycle_t nsec)
154 nsec += 500;
155 do_div(nsec, 1000);
156 return nsec;
160 * The global_trace is the descriptor that holds the tracing
161 * buffers for the live tracing. For each CPU, it contains
162 * a link list of pages that will store trace entries. The
163 * page descriptor of the pages in the memory is used to hold
164 * the link list by linking the lru item in the page descriptor
165 * to each of the pages in the buffer per CPU.
167 * For each active CPU there is a data field that holds the
168 * pages for the buffer for that CPU. Each CPU has the same number
169 * of pages allocated for its buffer.
171 static struct trace_array global_trace;
173 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
175 int filter_current_check_discard(struct ftrace_event_call *call, void *rec,
176 struct ring_buffer_event *event)
178 return filter_check_discard(call, rec, global_trace.buffer, event);
180 EXPORT_SYMBOL_GPL(filter_current_check_discard);
182 cycle_t ftrace_now(int cpu)
184 u64 ts;
186 /* Early boot up does not have a buffer yet */
187 if (!global_trace.buffer)
188 return trace_clock_local();
190 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
191 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
193 return ts;
197 * The max_tr is used to snapshot the global_trace when a maximum
198 * latency is reached. Some tracers will use this to store a maximum
199 * trace while it continues examining live traces.
201 * The buffers for the max_tr are set up the same as the global_trace.
202 * When a snapshot is taken, the link list of the max_tr is swapped
203 * with the link list of the global_trace and the buffers are reset for
204 * the global_trace so the tracing can continue.
206 static struct trace_array max_tr;
208 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
210 /* tracer_enabled is used to toggle activation of a tracer */
211 static int tracer_enabled = 1;
214 * tracing_is_enabled - return tracer_enabled status
216 * This function is used by other tracers to know the status
217 * of the tracer_enabled flag. Tracers may use this function
218 * to know if it should enable their features when starting
219 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
221 int tracing_is_enabled(void)
223 return tracer_enabled;
227 * trace_buf_size is the size in bytes that is allocated
228 * for a buffer. Note, the number of bytes is always rounded
229 * to page size.
231 * This number is purposely set to a low number of 16384.
232 * If the dump on oops happens, it will be much appreciated
233 * to not have to wait for all that output. Anyway this can be
234 * boot time and run time configurable.
236 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
238 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
240 /* trace_types holds a link list of available tracers. */
241 static struct tracer *trace_types __read_mostly;
243 /* current_trace points to the tracer that is currently active */
244 static struct tracer *current_trace __read_mostly;
247 * max_tracer_type_len is used to simplify the allocating of
248 * buffers to read userspace tracer names. We keep track of
249 * the longest tracer name registered.
251 static int max_tracer_type_len;
254 * trace_types_lock is used to protect the trace_types list.
255 * This lock is also used to keep user access serialized.
256 * Accesses from userspace will grab this lock while userspace
257 * activities happen inside the kernel.
259 static DEFINE_MUTEX(trace_types_lock);
261 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
262 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
264 /* trace_flags holds trace_options default values */
265 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
266 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
267 TRACE_ITER_GRAPH_TIME;
270 * trace_wake_up - wake up tasks waiting for trace input
272 * Simply wakes up any task that is blocked on the trace_wait
273 * queue. These is used with trace_poll for tasks polling the trace.
275 void trace_wake_up(void)
278 * The runqueue_is_locked() can fail, but this is the best we
279 * have for now:
281 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
282 wake_up(&trace_wait);
285 static int __init set_buf_size(char *str)
287 unsigned long buf_size;
289 if (!str)
290 return 0;
291 buf_size = memparse(str, &str);
292 /* nr_entries can not be zero */
293 if (buf_size == 0)
294 return 0;
295 trace_buf_size = buf_size;
296 return 1;
298 __setup("trace_buf_size=", set_buf_size);
300 unsigned long nsecs_to_usecs(unsigned long nsecs)
302 return nsecs / 1000;
305 /* These must match the bit postions in trace_iterator_flags */
306 static const char *trace_options[] = {
307 "print-parent",
308 "sym-offset",
309 "sym-addr",
310 "verbose",
311 "raw",
312 "hex",
313 "bin",
314 "block",
315 "stacktrace",
316 "sched-tree",
317 "trace_printk",
318 "ftrace_preempt",
319 "branch",
320 "annotate",
321 "userstacktrace",
322 "sym-userobj",
323 "printk-msg-only",
324 "context-info",
325 "latency-format",
326 "global-clock",
327 "sleep-time",
328 "graph-time",
329 NULL
333 * ftrace_max_lock is used to protect the swapping of buffers
334 * when taking a max snapshot. The buffers themselves are
335 * protected by per_cpu spinlocks. But the action of the swap
336 * needs its own lock.
338 * This is defined as a raw_spinlock_t in order to help
339 * with performance when lockdep debugging is enabled.
341 static raw_spinlock_t ftrace_max_lock =
342 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
345 * Copy the new maximum trace into the separate maximum-trace
346 * structure. (this way the maximum trace is permanently saved,
347 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
349 static void
350 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
352 struct trace_array_cpu *data = tr->data[cpu];
354 max_tr.cpu = cpu;
355 max_tr.time_start = data->preempt_timestamp;
357 data = max_tr.data[cpu];
358 data->saved_latency = tracing_max_latency;
360 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
361 data->pid = tsk->pid;
362 data->uid = task_uid(tsk);
363 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
364 data->policy = tsk->policy;
365 data->rt_priority = tsk->rt_priority;
367 /* record this tasks comm */
368 tracing_record_cmdline(tsk);
371 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
373 int len;
374 int ret;
376 if (!cnt)
377 return 0;
379 if (s->len <= s->readpos)
380 return -EBUSY;
382 len = s->len - s->readpos;
383 if (cnt > len)
384 cnt = len;
385 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
386 if (ret == cnt)
387 return -EFAULT;
389 cnt -= ret;
391 s->readpos += cnt;
392 return cnt;
395 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
397 int len;
398 void *ret;
400 if (s->len <= s->readpos)
401 return -EBUSY;
403 len = s->len - s->readpos;
404 if (cnt > len)
405 cnt = len;
406 ret = memcpy(buf, s->buffer + s->readpos, cnt);
407 if (!ret)
408 return -EFAULT;
410 s->readpos += cnt;
411 return cnt;
415 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
416 * @tr: tracer
417 * @tsk: the task with the latency
418 * @cpu: The cpu that initiated the trace.
420 * Flip the buffers between the @tr and the max_tr and record information
421 * about which task was the cause of this latency.
423 void
424 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
426 struct ring_buffer *buf = tr->buffer;
428 WARN_ON_ONCE(!irqs_disabled());
429 __raw_spin_lock(&ftrace_max_lock);
431 tr->buffer = max_tr.buffer;
432 max_tr.buffer = buf;
434 ftrace_disable_cpu();
435 ring_buffer_reset(tr->buffer);
436 ftrace_enable_cpu();
438 __update_max_tr(tr, tsk, cpu);
439 __raw_spin_unlock(&ftrace_max_lock);
443 * update_max_tr_single - only copy one trace over, and reset the rest
444 * @tr - tracer
445 * @tsk - task with the latency
446 * @cpu - the cpu of the buffer to copy.
448 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
450 void
451 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
453 int ret;
455 WARN_ON_ONCE(!irqs_disabled());
456 __raw_spin_lock(&ftrace_max_lock);
458 ftrace_disable_cpu();
460 ring_buffer_reset(max_tr.buffer);
461 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
463 ftrace_enable_cpu();
465 WARN_ON_ONCE(ret && ret != -EAGAIN);
467 __update_max_tr(tr, tsk, cpu);
468 __raw_spin_unlock(&ftrace_max_lock);
472 * register_tracer - register a tracer with the ftrace system.
473 * @type - the plugin for the tracer
475 * Register a new plugin tracer.
477 int register_tracer(struct tracer *type)
478 __releases(kernel_lock)
479 __acquires(kernel_lock)
481 struct tracer *t;
482 int len;
483 int ret = 0;
485 if (!type->name) {
486 pr_info("Tracer must have a name\n");
487 return -1;
491 * When this gets called we hold the BKL which means that
492 * preemption is disabled. Various trace selftests however
493 * need to disable and enable preemption for successful tests.
494 * So we drop the BKL here and grab it after the tests again.
496 unlock_kernel();
497 mutex_lock(&trace_types_lock);
499 tracing_selftest_running = true;
501 for (t = trace_types; t; t = t->next) {
502 if (strcmp(type->name, t->name) == 0) {
503 /* already found */
504 pr_info("Trace %s already registered\n",
505 type->name);
506 ret = -1;
507 goto out;
511 if (!type->set_flag)
512 type->set_flag = &dummy_set_flag;
513 if (!type->flags)
514 type->flags = &dummy_tracer_flags;
515 else
516 if (!type->flags->opts)
517 type->flags->opts = dummy_tracer_opt;
518 if (!type->wait_pipe)
519 type->wait_pipe = default_wait_pipe;
522 #ifdef CONFIG_FTRACE_STARTUP_TEST
523 if (type->selftest && !tracing_selftest_disabled) {
524 struct tracer *saved_tracer = current_trace;
525 struct trace_array *tr = &global_trace;
526 int i;
529 * Run a selftest on this tracer.
530 * Here we reset the trace buffer, and set the current
531 * tracer to be this tracer. The tracer can then run some
532 * internal tracing to verify that everything is in order.
533 * If we fail, we do not register this tracer.
535 for_each_tracing_cpu(i)
536 tracing_reset(tr, i);
538 current_trace = type;
539 /* the test is responsible for initializing and enabling */
540 pr_info("Testing tracer %s: ", type->name);
541 ret = type->selftest(type, tr);
542 /* the test is responsible for resetting too */
543 current_trace = saved_tracer;
544 if (ret) {
545 printk(KERN_CONT "FAILED!\n");
546 goto out;
548 /* Only reset on passing, to avoid touching corrupted buffers */
549 for_each_tracing_cpu(i)
550 tracing_reset(tr, i);
552 printk(KERN_CONT "PASSED\n");
554 #endif
556 type->next = trace_types;
557 trace_types = type;
558 len = strlen(type->name);
559 if (len > max_tracer_type_len)
560 max_tracer_type_len = len;
562 out:
563 tracing_selftest_running = false;
564 mutex_unlock(&trace_types_lock);
566 if (ret || !default_bootup_tracer)
567 goto out_unlock;
569 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
570 goto out_unlock;
572 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
573 /* Do we want this tracer to start on bootup? */
574 tracing_set_tracer(type->name);
575 default_bootup_tracer = NULL;
576 /* disable other selftests, since this will break it. */
577 tracing_selftest_disabled = 1;
578 #ifdef CONFIG_FTRACE_STARTUP_TEST
579 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
580 type->name);
581 #endif
583 out_unlock:
584 lock_kernel();
585 return ret;
588 void unregister_tracer(struct tracer *type)
590 struct tracer **t;
591 int len;
593 mutex_lock(&trace_types_lock);
594 for (t = &trace_types; *t; t = &(*t)->next) {
595 if (*t == type)
596 goto found;
598 pr_info("Trace %s not registered\n", type->name);
599 goto out;
601 found:
602 *t = (*t)->next;
604 if (type == current_trace && tracer_enabled) {
605 tracer_enabled = 0;
606 tracing_stop();
607 if (current_trace->stop)
608 current_trace->stop(&global_trace);
609 current_trace = &nop_trace;
612 if (strlen(type->name) != max_tracer_type_len)
613 goto out;
615 max_tracer_type_len = 0;
616 for (t = &trace_types; *t; t = &(*t)->next) {
617 len = strlen((*t)->name);
618 if (len > max_tracer_type_len)
619 max_tracer_type_len = len;
621 out:
622 mutex_unlock(&trace_types_lock);
625 void tracing_reset(struct trace_array *tr, int cpu)
627 ftrace_disable_cpu();
628 ring_buffer_reset_cpu(tr->buffer, cpu);
629 ftrace_enable_cpu();
632 void tracing_reset_online_cpus(struct trace_array *tr)
634 int cpu;
636 tr->time_start = ftrace_now(tr->cpu);
638 for_each_online_cpu(cpu)
639 tracing_reset(tr, cpu);
642 void tracing_reset_current(int cpu)
644 tracing_reset(&global_trace, cpu);
647 void tracing_reset_current_online_cpus(void)
649 tracing_reset_online_cpus(&global_trace);
652 #define SAVED_CMDLINES 128
653 #define NO_CMDLINE_MAP UINT_MAX
654 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
655 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
656 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
657 static int cmdline_idx;
658 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
660 /* temporary disable recording */
661 static atomic_t trace_record_cmdline_disabled __read_mostly;
663 static void trace_init_cmdlines(void)
665 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
666 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
667 cmdline_idx = 0;
670 static int trace_stop_count;
671 static DEFINE_SPINLOCK(tracing_start_lock);
674 * ftrace_off_permanent - disable all ftrace code permanently
676 * This should only be called when a serious anomally has
677 * been detected. This will turn off the function tracing,
678 * ring buffers, and other tracing utilites. It takes no
679 * locks and can be called from any context.
681 void ftrace_off_permanent(void)
683 tracing_disabled = 1;
684 ftrace_stop();
685 tracing_off_permanent();
689 * tracing_start - quick start of the tracer
691 * If tracing is enabled but was stopped by tracing_stop,
692 * this will start the tracer back up.
694 void tracing_start(void)
696 struct ring_buffer *buffer;
697 unsigned long flags;
699 if (tracing_disabled)
700 return;
702 spin_lock_irqsave(&tracing_start_lock, flags);
703 if (--trace_stop_count) {
704 if (trace_stop_count < 0) {
705 /* Someone screwed up their debugging */
706 WARN_ON_ONCE(1);
707 trace_stop_count = 0;
709 goto out;
713 buffer = global_trace.buffer;
714 if (buffer)
715 ring_buffer_record_enable(buffer);
717 buffer = max_tr.buffer;
718 if (buffer)
719 ring_buffer_record_enable(buffer);
721 ftrace_start();
722 out:
723 spin_unlock_irqrestore(&tracing_start_lock, flags);
727 * tracing_stop - quick stop of the tracer
729 * Light weight way to stop tracing. Use in conjunction with
730 * tracing_start.
732 void tracing_stop(void)
734 struct ring_buffer *buffer;
735 unsigned long flags;
737 ftrace_stop();
738 spin_lock_irqsave(&tracing_start_lock, flags);
739 if (trace_stop_count++)
740 goto out;
742 buffer = global_trace.buffer;
743 if (buffer)
744 ring_buffer_record_disable(buffer);
746 buffer = max_tr.buffer;
747 if (buffer)
748 ring_buffer_record_disable(buffer);
750 out:
751 spin_unlock_irqrestore(&tracing_start_lock, flags);
754 void trace_stop_cmdline_recording(void);
756 static void trace_save_cmdline(struct task_struct *tsk)
758 unsigned pid, idx;
760 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
761 return;
764 * It's not the end of the world if we don't get
765 * the lock, but we also don't want to spin
766 * nor do we want to disable interrupts,
767 * so if we miss here, then better luck next time.
769 if (!__raw_spin_trylock(&trace_cmdline_lock))
770 return;
772 idx = map_pid_to_cmdline[tsk->pid];
773 if (idx == NO_CMDLINE_MAP) {
774 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
777 * Check whether the cmdline buffer at idx has a pid
778 * mapped. We are going to overwrite that entry so we
779 * need to clear the map_pid_to_cmdline. Otherwise we
780 * would read the new comm for the old pid.
782 pid = map_cmdline_to_pid[idx];
783 if (pid != NO_CMDLINE_MAP)
784 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
786 map_cmdline_to_pid[idx] = tsk->pid;
787 map_pid_to_cmdline[tsk->pid] = idx;
789 cmdline_idx = idx;
792 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
794 __raw_spin_unlock(&trace_cmdline_lock);
797 void trace_find_cmdline(int pid, char comm[])
799 unsigned map;
801 if (!pid) {
802 strcpy(comm, "<idle>");
803 return;
806 if (pid > PID_MAX_DEFAULT) {
807 strcpy(comm, "<...>");
808 return;
811 preempt_disable();
812 __raw_spin_lock(&trace_cmdline_lock);
813 map = map_pid_to_cmdline[pid];
814 if (map != NO_CMDLINE_MAP)
815 strcpy(comm, saved_cmdlines[map]);
816 else
817 strcpy(comm, "<...>");
819 __raw_spin_unlock(&trace_cmdline_lock);
820 preempt_enable();
823 void tracing_record_cmdline(struct task_struct *tsk)
825 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
826 !tracing_is_on())
827 return;
829 trace_save_cmdline(tsk);
832 void
833 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
834 int pc)
836 struct task_struct *tsk = current;
838 entry->preempt_count = pc & 0xff;
839 entry->pid = (tsk) ? tsk->pid : 0;
840 entry->tgid = (tsk) ? tsk->tgid : 0;
841 entry->flags =
842 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
843 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
844 #else
845 TRACE_FLAG_IRQS_NOSUPPORT |
846 #endif
847 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
848 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
849 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
852 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
853 int type,
854 unsigned long len,
855 unsigned long flags, int pc)
857 struct ring_buffer_event *event;
859 event = ring_buffer_lock_reserve(tr->buffer, len);
860 if (event != NULL) {
861 struct trace_entry *ent = ring_buffer_event_data(event);
863 tracing_generic_entry_update(ent, flags, pc);
864 ent->type = type;
867 return event;
869 static void ftrace_trace_stack(struct trace_array *tr,
870 unsigned long flags, int skip, int pc);
871 static void ftrace_trace_userstack(struct trace_array *tr,
872 unsigned long flags, int pc);
874 static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
875 struct ring_buffer_event *event,
876 unsigned long flags, int pc,
877 int wake)
879 ring_buffer_unlock_commit(tr->buffer, event);
881 ftrace_trace_stack(tr, flags, 6, pc);
882 ftrace_trace_userstack(tr, flags, pc);
884 if (wake)
885 trace_wake_up();
888 void trace_buffer_unlock_commit(struct trace_array *tr,
889 struct ring_buffer_event *event,
890 unsigned long flags, int pc)
892 __trace_buffer_unlock_commit(tr, event, flags, pc, 1);
895 struct ring_buffer_event *
896 trace_current_buffer_lock_reserve(int type, unsigned long len,
897 unsigned long flags, int pc)
899 return trace_buffer_lock_reserve(&global_trace,
900 type, len, flags, pc);
902 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
904 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
905 unsigned long flags, int pc)
907 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
909 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
911 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
912 unsigned long flags, int pc)
914 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
916 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
918 void trace_current_buffer_discard_commit(struct ring_buffer_event *event)
920 ring_buffer_discard_commit(global_trace.buffer, event);
922 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
924 void
925 trace_function(struct trace_array *tr,
926 unsigned long ip, unsigned long parent_ip, unsigned long flags,
927 int pc)
929 struct ftrace_event_call *call = &event_function;
930 struct ring_buffer_event *event;
931 struct ftrace_entry *entry;
933 /* If we are reading the ring buffer, don't trace */
934 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
935 return;
937 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
938 flags, pc);
939 if (!event)
940 return;
941 entry = ring_buffer_event_data(event);
942 entry->ip = ip;
943 entry->parent_ip = parent_ip;
945 if (!filter_check_discard(call, entry, tr->buffer, event))
946 ring_buffer_unlock_commit(tr->buffer, event);
949 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
950 static int __trace_graph_entry(struct trace_array *tr,
951 struct ftrace_graph_ent *trace,
952 unsigned long flags,
953 int pc)
955 struct ftrace_event_call *call = &event_funcgraph_entry;
956 struct ring_buffer_event *event;
957 struct ftrace_graph_ent_entry *entry;
959 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
960 return 0;
962 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
963 sizeof(*entry), flags, pc);
964 if (!event)
965 return 0;
966 entry = ring_buffer_event_data(event);
967 entry->graph_ent = *trace;
968 if (!filter_current_check_discard(call, entry, event))
969 ring_buffer_unlock_commit(global_trace.buffer, event);
971 return 1;
974 static void __trace_graph_return(struct trace_array *tr,
975 struct ftrace_graph_ret *trace,
976 unsigned long flags,
977 int pc)
979 struct ftrace_event_call *call = &event_funcgraph_exit;
980 struct ring_buffer_event *event;
981 struct ftrace_graph_ret_entry *entry;
983 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
984 return;
986 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
987 sizeof(*entry), flags, pc);
988 if (!event)
989 return;
990 entry = ring_buffer_event_data(event);
991 entry->ret = *trace;
992 if (!filter_current_check_discard(call, entry, event))
993 ring_buffer_unlock_commit(global_trace.buffer, event);
995 #endif
997 void
998 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
999 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1000 int pc)
1002 if (likely(!atomic_read(&data->disabled)))
1003 trace_function(tr, ip, parent_ip, flags, pc);
1006 static void __ftrace_trace_stack(struct trace_array *tr,
1007 unsigned long flags,
1008 int skip, int pc)
1010 #ifdef CONFIG_STACKTRACE
1011 struct ftrace_event_call *call = &event_kernel_stack;
1012 struct ring_buffer_event *event;
1013 struct stack_entry *entry;
1014 struct stack_trace trace;
1016 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
1017 sizeof(*entry), flags, pc);
1018 if (!event)
1019 return;
1020 entry = ring_buffer_event_data(event);
1021 memset(&entry->caller, 0, sizeof(entry->caller));
1023 trace.nr_entries = 0;
1024 trace.max_entries = FTRACE_STACK_ENTRIES;
1025 trace.skip = skip;
1026 trace.entries = entry->caller;
1028 save_stack_trace(&trace);
1029 if (!filter_check_discard(call, entry, tr->buffer, event))
1030 ring_buffer_unlock_commit(tr->buffer, event);
1031 #endif
1034 static void ftrace_trace_stack(struct trace_array *tr,
1035 unsigned long flags,
1036 int skip, int pc)
1038 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1039 return;
1041 __ftrace_trace_stack(tr, flags, skip, pc);
1044 void __trace_stack(struct trace_array *tr,
1045 unsigned long flags,
1046 int skip, int pc)
1048 __ftrace_trace_stack(tr, flags, skip, pc);
1051 static void ftrace_trace_userstack(struct trace_array *tr,
1052 unsigned long flags, int pc)
1054 #ifdef CONFIG_STACKTRACE
1055 struct ftrace_event_call *call = &event_user_stack;
1056 struct ring_buffer_event *event;
1057 struct userstack_entry *entry;
1058 struct stack_trace trace;
1060 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1061 return;
1063 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1064 sizeof(*entry), flags, pc);
1065 if (!event)
1066 return;
1067 entry = ring_buffer_event_data(event);
1069 memset(&entry->caller, 0, sizeof(entry->caller));
1071 trace.nr_entries = 0;
1072 trace.max_entries = FTRACE_STACK_ENTRIES;
1073 trace.skip = 0;
1074 trace.entries = entry->caller;
1076 save_stack_trace_user(&trace);
1077 if (!filter_check_discard(call, entry, tr->buffer, event))
1078 ring_buffer_unlock_commit(tr->buffer, event);
1079 #endif
1082 #ifdef UNUSED
1083 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1085 ftrace_trace_userstack(tr, flags, preempt_count());
1087 #endif /* UNUSED */
1089 static void
1090 ftrace_trace_special(void *__tr,
1091 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1092 int pc)
1094 struct ring_buffer_event *event;
1095 struct trace_array *tr = __tr;
1096 struct special_entry *entry;
1098 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1099 sizeof(*entry), 0, pc);
1100 if (!event)
1101 return;
1102 entry = ring_buffer_event_data(event);
1103 entry->arg1 = arg1;
1104 entry->arg2 = arg2;
1105 entry->arg3 = arg3;
1106 trace_buffer_unlock_commit(tr, event, 0, pc);
1109 void
1110 __trace_special(void *__tr, void *__data,
1111 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1113 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1116 void
1117 tracing_sched_switch_trace(struct trace_array *tr,
1118 struct task_struct *prev,
1119 struct task_struct *next,
1120 unsigned long flags, int pc)
1122 struct ftrace_event_call *call = &event_context_switch;
1123 struct ring_buffer_event *event;
1124 struct ctx_switch_entry *entry;
1126 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1127 sizeof(*entry), flags, pc);
1128 if (!event)
1129 return;
1130 entry = ring_buffer_event_data(event);
1131 entry->prev_pid = prev->pid;
1132 entry->prev_prio = prev->prio;
1133 entry->prev_state = prev->state;
1134 entry->next_pid = next->pid;
1135 entry->next_prio = next->prio;
1136 entry->next_state = next->state;
1137 entry->next_cpu = task_cpu(next);
1139 if (!filter_check_discard(call, entry, tr->buffer, event))
1140 trace_buffer_unlock_commit(tr, event, flags, pc);
1143 void
1144 tracing_sched_wakeup_trace(struct trace_array *tr,
1145 struct task_struct *wakee,
1146 struct task_struct *curr,
1147 unsigned long flags, int pc)
1149 struct ftrace_event_call *call = &event_wakeup;
1150 struct ring_buffer_event *event;
1151 struct ctx_switch_entry *entry;
1153 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1154 sizeof(*entry), flags, pc);
1155 if (!event)
1156 return;
1157 entry = ring_buffer_event_data(event);
1158 entry->prev_pid = curr->pid;
1159 entry->prev_prio = curr->prio;
1160 entry->prev_state = curr->state;
1161 entry->next_pid = wakee->pid;
1162 entry->next_prio = wakee->prio;
1163 entry->next_state = wakee->state;
1164 entry->next_cpu = task_cpu(wakee);
1166 if (!filter_check_discard(call, entry, tr->buffer, event))
1167 ring_buffer_unlock_commit(tr->buffer, event);
1168 ftrace_trace_stack(tr, flags, 6, pc);
1169 ftrace_trace_userstack(tr, flags, pc);
1172 void
1173 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1175 struct trace_array *tr = &global_trace;
1176 struct trace_array_cpu *data;
1177 unsigned long flags;
1178 int cpu;
1179 int pc;
1181 if (tracing_disabled)
1182 return;
1184 pc = preempt_count();
1185 local_irq_save(flags);
1186 cpu = raw_smp_processor_id();
1187 data = tr->data[cpu];
1189 if (likely(atomic_inc_return(&data->disabled) == 1))
1190 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1192 atomic_dec(&data->disabled);
1193 local_irq_restore(flags);
1196 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1197 int trace_graph_entry(struct ftrace_graph_ent *trace)
1199 struct trace_array *tr = &global_trace;
1200 struct trace_array_cpu *data;
1201 unsigned long flags;
1202 long disabled;
1203 int ret;
1204 int cpu;
1205 int pc;
1207 if (!ftrace_trace_task(current))
1208 return 0;
1210 if (!ftrace_graph_addr(trace->func))
1211 return 0;
1213 local_irq_save(flags);
1214 cpu = raw_smp_processor_id();
1215 data = tr->data[cpu];
1216 disabled = atomic_inc_return(&data->disabled);
1217 if (likely(disabled == 1)) {
1218 pc = preempt_count();
1219 ret = __trace_graph_entry(tr, trace, flags, pc);
1220 } else {
1221 ret = 0;
1223 /* Only do the atomic if it is not already set */
1224 if (!test_tsk_trace_graph(current))
1225 set_tsk_trace_graph(current);
1227 atomic_dec(&data->disabled);
1228 local_irq_restore(flags);
1230 return ret;
1233 void trace_graph_return(struct ftrace_graph_ret *trace)
1235 struct trace_array *tr = &global_trace;
1236 struct trace_array_cpu *data;
1237 unsigned long flags;
1238 long disabled;
1239 int cpu;
1240 int pc;
1242 local_irq_save(flags);
1243 cpu = raw_smp_processor_id();
1244 data = tr->data[cpu];
1245 disabled = atomic_inc_return(&data->disabled);
1246 if (likely(disabled == 1)) {
1247 pc = preempt_count();
1248 __trace_graph_return(tr, trace, flags, pc);
1250 if (!trace->depth)
1251 clear_tsk_trace_graph(current);
1252 atomic_dec(&data->disabled);
1253 local_irq_restore(flags);
1255 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1259 * trace_vbprintk - write binary msg to tracing buffer
1262 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1264 static raw_spinlock_t trace_buf_lock =
1265 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1266 static u32 trace_buf[TRACE_BUF_SIZE];
1268 struct ftrace_event_call *call = &event_bprint;
1269 struct ring_buffer_event *event;
1270 struct trace_array *tr = &global_trace;
1271 struct trace_array_cpu *data;
1272 struct bprint_entry *entry;
1273 unsigned long flags;
1274 int disable;
1275 int resched;
1276 int cpu, len = 0, size, pc;
1278 if (unlikely(tracing_selftest_running || tracing_disabled))
1279 return 0;
1281 /* Don't pollute graph traces with trace_vprintk internals */
1282 pause_graph_tracing();
1284 pc = preempt_count();
1285 resched = ftrace_preempt_disable();
1286 cpu = raw_smp_processor_id();
1287 data = tr->data[cpu];
1289 disable = atomic_inc_return(&data->disabled);
1290 if (unlikely(disable != 1))
1291 goto out;
1293 /* Lockdep uses trace_printk for lock tracing */
1294 local_irq_save(flags);
1295 __raw_spin_lock(&trace_buf_lock);
1296 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1298 if (len > TRACE_BUF_SIZE || len < 0)
1299 goto out_unlock;
1301 size = sizeof(*entry) + sizeof(u32) * len;
1302 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1303 if (!event)
1304 goto out_unlock;
1305 entry = ring_buffer_event_data(event);
1306 entry->ip = ip;
1307 entry->fmt = fmt;
1309 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1310 if (!filter_check_discard(call, entry, tr->buffer, event))
1311 ring_buffer_unlock_commit(tr->buffer, event);
1313 out_unlock:
1314 __raw_spin_unlock(&trace_buf_lock);
1315 local_irq_restore(flags);
1317 out:
1318 atomic_dec_return(&data->disabled);
1319 ftrace_preempt_enable(resched);
1320 unpause_graph_tracing();
1322 return len;
1324 EXPORT_SYMBOL_GPL(trace_vbprintk);
1326 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1328 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1329 static char trace_buf[TRACE_BUF_SIZE];
1331 struct ftrace_event_call *call = &event_print;
1332 struct ring_buffer_event *event;
1333 struct trace_array *tr = &global_trace;
1334 struct trace_array_cpu *data;
1335 int cpu, len = 0, size, pc;
1336 struct print_entry *entry;
1337 unsigned long irq_flags;
1338 int disable;
1340 if (tracing_disabled || tracing_selftest_running)
1341 return 0;
1343 pc = preempt_count();
1344 preempt_disable_notrace();
1345 cpu = raw_smp_processor_id();
1346 data = tr->data[cpu];
1348 disable = atomic_inc_return(&data->disabled);
1349 if (unlikely(disable != 1))
1350 goto out;
1352 pause_graph_tracing();
1353 raw_local_irq_save(irq_flags);
1354 __raw_spin_lock(&trace_buf_lock);
1355 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1357 len = min(len, TRACE_BUF_SIZE-1);
1358 trace_buf[len] = 0;
1360 size = sizeof(*entry) + len + 1;
1361 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1362 if (!event)
1363 goto out_unlock;
1364 entry = ring_buffer_event_data(event);
1365 entry->ip = ip;
1367 memcpy(&entry->buf, trace_buf, len);
1368 entry->buf[len] = 0;
1369 if (!filter_check_discard(call, entry, tr->buffer, event))
1370 ring_buffer_unlock_commit(tr->buffer, event);
1372 out_unlock:
1373 __raw_spin_unlock(&trace_buf_lock);
1374 raw_local_irq_restore(irq_flags);
1375 unpause_graph_tracing();
1376 out:
1377 atomic_dec_return(&data->disabled);
1378 preempt_enable_notrace();
1380 return len;
1382 EXPORT_SYMBOL_GPL(trace_vprintk);
1384 enum trace_file_type {
1385 TRACE_FILE_LAT_FMT = 1,
1386 TRACE_FILE_ANNOTATE = 2,
1389 static void trace_iterator_increment(struct trace_iterator *iter)
1391 /* Don't allow ftrace to trace into the ring buffers */
1392 ftrace_disable_cpu();
1394 iter->idx++;
1395 if (iter->buffer_iter[iter->cpu])
1396 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1398 ftrace_enable_cpu();
1401 static struct trace_entry *
1402 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1404 struct ring_buffer_event *event;
1405 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1407 /* Don't allow ftrace to trace into the ring buffers */
1408 ftrace_disable_cpu();
1410 if (buf_iter)
1411 event = ring_buffer_iter_peek(buf_iter, ts);
1412 else
1413 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1415 ftrace_enable_cpu();
1417 return event ? ring_buffer_event_data(event) : NULL;
1420 static struct trace_entry *
1421 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1423 struct ring_buffer *buffer = iter->tr->buffer;
1424 struct trace_entry *ent, *next = NULL;
1425 int cpu_file = iter->cpu_file;
1426 u64 next_ts = 0, ts;
1427 int next_cpu = -1;
1428 int cpu;
1431 * If we are in a per_cpu trace file, don't bother by iterating over
1432 * all cpu and peek directly.
1434 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1435 if (ring_buffer_empty_cpu(buffer, cpu_file))
1436 return NULL;
1437 ent = peek_next_entry(iter, cpu_file, ent_ts);
1438 if (ent_cpu)
1439 *ent_cpu = cpu_file;
1441 return ent;
1444 for_each_tracing_cpu(cpu) {
1446 if (ring_buffer_empty_cpu(buffer, cpu))
1447 continue;
1449 ent = peek_next_entry(iter, cpu, &ts);
1452 * Pick the entry with the smallest timestamp:
1454 if (ent && (!next || ts < next_ts)) {
1455 next = ent;
1456 next_cpu = cpu;
1457 next_ts = ts;
1461 if (ent_cpu)
1462 *ent_cpu = next_cpu;
1464 if (ent_ts)
1465 *ent_ts = next_ts;
1467 return next;
1470 /* Find the next real entry, without updating the iterator itself */
1471 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1472 int *ent_cpu, u64 *ent_ts)
1474 return __find_next_entry(iter, ent_cpu, ent_ts);
1477 /* Find the next real entry, and increment the iterator to the next entry */
1478 static void *find_next_entry_inc(struct trace_iterator *iter)
1480 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1482 if (iter->ent)
1483 trace_iterator_increment(iter);
1485 return iter->ent ? iter : NULL;
1488 static void trace_consume(struct trace_iterator *iter)
1490 /* Don't allow ftrace to trace into the ring buffers */
1491 ftrace_disable_cpu();
1492 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1493 ftrace_enable_cpu();
1496 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1498 struct trace_iterator *iter = m->private;
1499 int i = (int)*pos;
1500 void *ent;
1502 (*pos)++;
1504 /* can't go backwards */
1505 if (iter->idx > i)
1506 return NULL;
1508 if (iter->idx < 0)
1509 ent = find_next_entry_inc(iter);
1510 else
1511 ent = iter;
1513 while (ent && iter->idx < i)
1514 ent = find_next_entry_inc(iter);
1516 iter->pos = *pos;
1518 return ent;
1522 * No necessary locking here. The worst thing which can
1523 * happen is loosing events consumed at the same time
1524 * by a trace_pipe reader.
1525 * Other than that, we don't risk to crash the ring buffer
1526 * because it serializes the readers.
1528 * The current tracer is copied to avoid a global locking
1529 * all around.
1531 static void *s_start(struct seq_file *m, loff_t *pos)
1533 struct trace_iterator *iter = m->private;
1534 static struct tracer *old_tracer;
1535 int cpu_file = iter->cpu_file;
1536 void *p = NULL;
1537 loff_t l = 0;
1538 int cpu;
1540 /* copy the tracer to avoid using a global lock all around */
1541 mutex_lock(&trace_types_lock);
1542 if (unlikely(old_tracer != current_trace && current_trace)) {
1543 old_tracer = current_trace;
1544 *iter->trace = *current_trace;
1546 mutex_unlock(&trace_types_lock);
1548 atomic_inc(&trace_record_cmdline_disabled);
1550 if (*pos != iter->pos) {
1551 iter->ent = NULL;
1552 iter->cpu = 0;
1553 iter->idx = -1;
1555 ftrace_disable_cpu();
1557 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1558 for_each_tracing_cpu(cpu)
1559 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1560 } else
1561 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1564 ftrace_enable_cpu();
1566 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1569 } else {
1570 l = *pos - 1;
1571 p = s_next(m, p, &l);
1574 trace_event_read_lock();
1575 return p;
1578 static void s_stop(struct seq_file *m, void *p)
1580 atomic_dec(&trace_record_cmdline_disabled);
1581 trace_event_read_unlock();
1584 static void print_lat_help_header(struct seq_file *m)
1586 seq_puts(m, "# _------=> CPU# \n");
1587 seq_puts(m, "# / _-----=> irqs-off \n");
1588 seq_puts(m, "# | / _----=> need-resched \n");
1589 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1590 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1591 seq_puts(m, "# |||| / \n");
1592 seq_puts(m, "# ||||| delay \n");
1593 seq_puts(m, "# cmd pid ||||| time | caller \n");
1594 seq_puts(m, "# \\ / ||||| \\ | / \n");
1597 static void print_func_help_header(struct seq_file *m)
1599 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1600 seq_puts(m, "# | | | | |\n");
1604 static void
1605 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1607 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1608 struct trace_array *tr = iter->tr;
1609 struct trace_array_cpu *data = tr->data[tr->cpu];
1610 struct tracer *type = current_trace;
1611 unsigned long total;
1612 unsigned long entries;
1613 const char *name = "preemption";
1615 if (type)
1616 name = type->name;
1618 entries = ring_buffer_entries(iter->tr->buffer);
1619 total = entries +
1620 ring_buffer_overruns(iter->tr->buffer);
1622 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1623 name, UTS_RELEASE);
1624 seq_puts(m, "# -----------------------------------"
1625 "---------------------------------\n");
1626 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1627 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1628 nsecs_to_usecs(data->saved_latency),
1629 entries,
1630 total,
1631 tr->cpu,
1632 #if defined(CONFIG_PREEMPT_NONE)
1633 "server",
1634 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1635 "desktop",
1636 #elif defined(CONFIG_PREEMPT)
1637 "preempt",
1638 #else
1639 "unknown",
1640 #endif
1641 /* These are reserved for later use */
1642 0, 0, 0, 0);
1643 #ifdef CONFIG_SMP
1644 seq_printf(m, " #P:%d)\n", num_online_cpus());
1645 #else
1646 seq_puts(m, ")\n");
1647 #endif
1648 seq_puts(m, "# -----------------\n");
1649 seq_printf(m, "# | task: %.16s-%d "
1650 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1651 data->comm, data->pid, data->uid, data->nice,
1652 data->policy, data->rt_priority);
1653 seq_puts(m, "# -----------------\n");
1655 if (data->critical_start) {
1656 seq_puts(m, "# => started at: ");
1657 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1658 trace_print_seq(m, &iter->seq);
1659 seq_puts(m, "\n# => ended at: ");
1660 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1661 trace_print_seq(m, &iter->seq);
1662 seq_puts(m, "#\n");
1665 seq_puts(m, "#\n");
1668 static void test_cpu_buff_start(struct trace_iterator *iter)
1670 struct trace_seq *s = &iter->seq;
1672 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1673 return;
1675 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1676 return;
1678 if (cpumask_test_cpu(iter->cpu, iter->started))
1679 return;
1681 cpumask_set_cpu(iter->cpu, iter->started);
1683 /* Don't print started cpu buffer for the first entry of the trace */
1684 if (iter->idx > 1)
1685 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1686 iter->cpu);
1689 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1691 struct trace_seq *s = &iter->seq;
1692 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1693 struct trace_entry *entry;
1694 struct trace_event *event;
1696 entry = iter->ent;
1698 test_cpu_buff_start(iter);
1700 event = ftrace_find_event(entry->type);
1702 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1703 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1704 if (!trace_print_lat_context(iter))
1705 goto partial;
1706 } else {
1707 if (!trace_print_context(iter))
1708 goto partial;
1712 if (event)
1713 return event->trace(iter, sym_flags);
1715 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1716 goto partial;
1718 return TRACE_TYPE_HANDLED;
1719 partial:
1720 return TRACE_TYPE_PARTIAL_LINE;
1723 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1725 struct trace_seq *s = &iter->seq;
1726 struct trace_entry *entry;
1727 struct trace_event *event;
1729 entry = iter->ent;
1731 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1732 if (!trace_seq_printf(s, "%d %d %llu ",
1733 entry->pid, iter->cpu, iter->ts))
1734 goto partial;
1737 event = ftrace_find_event(entry->type);
1738 if (event)
1739 return event->raw(iter, 0);
1741 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1742 goto partial;
1744 return TRACE_TYPE_HANDLED;
1745 partial:
1746 return TRACE_TYPE_PARTIAL_LINE;
1749 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1751 struct trace_seq *s = &iter->seq;
1752 unsigned char newline = '\n';
1753 struct trace_entry *entry;
1754 struct trace_event *event;
1756 entry = iter->ent;
1758 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1759 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1760 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1761 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1764 event = ftrace_find_event(entry->type);
1765 if (event) {
1766 enum print_line_t ret = event->hex(iter, 0);
1767 if (ret != TRACE_TYPE_HANDLED)
1768 return ret;
1771 SEQ_PUT_FIELD_RET(s, newline);
1773 return TRACE_TYPE_HANDLED;
1776 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1778 struct trace_seq *s = &iter->seq;
1779 struct trace_entry *entry;
1780 struct trace_event *event;
1782 entry = iter->ent;
1784 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1785 SEQ_PUT_FIELD_RET(s, entry->pid);
1786 SEQ_PUT_FIELD_RET(s, iter->cpu);
1787 SEQ_PUT_FIELD_RET(s, iter->ts);
1790 event = ftrace_find_event(entry->type);
1791 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1794 static int trace_empty(struct trace_iterator *iter)
1796 int cpu;
1798 /* If we are looking at one CPU buffer, only check that one */
1799 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1800 cpu = iter->cpu_file;
1801 if (iter->buffer_iter[cpu]) {
1802 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1803 return 0;
1804 } else {
1805 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1806 return 0;
1808 return 1;
1811 for_each_tracing_cpu(cpu) {
1812 if (iter->buffer_iter[cpu]) {
1813 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1814 return 0;
1815 } else {
1816 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1817 return 0;
1821 return 1;
1824 /* Called with trace_event_read_lock() held. */
1825 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1827 enum print_line_t ret;
1829 if (iter->trace && iter->trace->print_line) {
1830 ret = iter->trace->print_line(iter);
1831 if (ret != TRACE_TYPE_UNHANDLED)
1832 return ret;
1835 if (iter->ent->type == TRACE_BPRINT &&
1836 trace_flags & TRACE_ITER_PRINTK &&
1837 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1838 return trace_print_bprintk_msg_only(iter);
1840 if (iter->ent->type == TRACE_PRINT &&
1841 trace_flags & TRACE_ITER_PRINTK &&
1842 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1843 return trace_print_printk_msg_only(iter);
1845 if (trace_flags & TRACE_ITER_BIN)
1846 return print_bin_fmt(iter);
1848 if (trace_flags & TRACE_ITER_HEX)
1849 return print_hex_fmt(iter);
1851 if (trace_flags & TRACE_ITER_RAW)
1852 return print_raw_fmt(iter);
1854 return print_trace_fmt(iter);
1857 static int s_show(struct seq_file *m, void *v)
1859 struct trace_iterator *iter = v;
1861 if (iter->ent == NULL) {
1862 if (iter->tr) {
1863 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1864 seq_puts(m, "#\n");
1866 if (iter->trace && iter->trace->print_header)
1867 iter->trace->print_header(m);
1868 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1869 /* print nothing if the buffers are empty */
1870 if (trace_empty(iter))
1871 return 0;
1872 print_trace_header(m, iter);
1873 if (!(trace_flags & TRACE_ITER_VERBOSE))
1874 print_lat_help_header(m);
1875 } else {
1876 if (!(trace_flags & TRACE_ITER_VERBOSE))
1877 print_func_help_header(m);
1879 } else {
1880 print_trace_line(iter);
1881 trace_print_seq(m, &iter->seq);
1884 return 0;
1887 static struct seq_operations tracer_seq_ops = {
1888 .start = s_start,
1889 .next = s_next,
1890 .stop = s_stop,
1891 .show = s_show,
1894 static struct trace_iterator *
1895 __tracing_open(struct inode *inode, struct file *file)
1897 long cpu_file = (long) inode->i_private;
1898 void *fail_ret = ERR_PTR(-ENOMEM);
1899 struct trace_iterator *iter;
1900 struct seq_file *m;
1901 int cpu, ret;
1903 if (tracing_disabled)
1904 return ERR_PTR(-ENODEV);
1906 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1907 if (!iter)
1908 return ERR_PTR(-ENOMEM);
1911 * We make a copy of the current tracer to avoid concurrent
1912 * changes on it while we are reading.
1914 mutex_lock(&trace_types_lock);
1915 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1916 if (!iter->trace)
1917 goto fail;
1919 if (current_trace)
1920 *iter->trace = *current_trace;
1922 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1923 goto fail;
1925 cpumask_clear(iter->started);
1927 if (current_trace && current_trace->print_max)
1928 iter->tr = &max_tr;
1929 else
1930 iter->tr = &global_trace;
1931 iter->pos = -1;
1932 mutex_init(&iter->mutex);
1933 iter->cpu_file = cpu_file;
1935 /* Notify the tracer early; before we stop tracing. */
1936 if (iter->trace && iter->trace->open)
1937 iter->trace->open(iter);
1939 /* Annotate start of buffers if we had overruns */
1940 if (ring_buffer_overruns(iter->tr->buffer))
1941 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1943 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1944 for_each_tracing_cpu(cpu) {
1946 iter->buffer_iter[cpu] =
1947 ring_buffer_read_start(iter->tr->buffer, cpu);
1949 } else {
1950 cpu = iter->cpu_file;
1951 iter->buffer_iter[cpu] =
1952 ring_buffer_read_start(iter->tr->buffer, cpu);
1955 /* TODO stop tracer */
1956 ret = seq_open(file, &tracer_seq_ops);
1957 if (ret < 0) {
1958 fail_ret = ERR_PTR(ret);
1959 goto fail_buffer;
1962 m = file->private_data;
1963 m->private = iter;
1965 /* stop the trace while dumping */
1966 tracing_stop();
1968 mutex_unlock(&trace_types_lock);
1970 return iter;
1972 fail_buffer:
1973 for_each_tracing_cpu(cpu) {
1974 if (iter->buffer_iter[cpu])
1975 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1977 free_cpumask_var(iter->started);
1978 fail:
1979 mutex_unlock(&trace_types_lock);
1980 kfree(iter->trace);
1981 kfree(iter);
1983 return fail_ret;
1986 int tracing_open_generic(struct inode *inode, struct file *filp)
1988 if (tracing_disabled)
1989 return -ENODEV;
1991 filp->private_data = inode->i_private;
1992 return 0;
1995 static int tracing_release(struct inode *inode, struct file *file)
1997 struct seq_file *m = (struct seq_file *)file->private_data;
1998 struct trace_iterator *iter;
1999 int cpu;
2001 if (!(file->f_mode & FMODE_READ))
2002 return 0;
2004 iter = m->private;
2006 mutex_lock(&trace_types_lock);
2007 for_each_tracing_cpu(cpu) {
2008 if (iter->buffer_iter[cpu])
2009 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2012 if (iter->trace && iter->trace->close)
2013 iter->trace->close(iter);
2015 /* reenable tracing if it was previously enabled */
2016 tracing_start();
2017 mutex_unlock(&trace_types_lock);
2019 seq_release(inode, file);
2020 mutex_destroy(&iter->mutex);
2021 free_cpumask_var(iter->started);
2022 kfree(iter->trace);
2023 kfree(iter);
2024 return 0;
2027 static int tracing_open(struct inode *inode, struct file *file)
2029 struct trace_iterator *iter;
2030 int ret = 0;
2032 /* If this file was open for write, then erase contents */
2033 if ((file->f_mode & FMODE_WRITE) &&
2034 !(file->f_flags & O_APPEND)) {
2035 long cpu = (long) inode->i_private;
2037 if (cpu == TRACE_PIPE_ALL_CPU)
2038 tracing_reset_online_cpus(&global_trace);
2039 else
2040 tracing_reset(&global_trace, cpu);
2043 if (file->f_mode & FMODE_READ) {
2044 iter = __tracing_open(inode, file);
2045 if (IS_ERR(iter))
2046 ret = PTR_ERR(iter);
2047 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2048 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2050 return ret;
2053 static void *
2054 t_next(struct seq_file *m, void *v, loff_t *pos)
2056 struct tracer *t = v;
2058 (*pos)++;
2060 if (t)
2061 t = t->next;
2063 return t;
2066 static void *t_start(struct seq_file *m, loff_t *pos)
2068 struct tracer *t;
2069 loff_t l = 0;
2071 mutex_lock(&trace_types_lock);
2072 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2075 return t;
2078 static void t_stop(struct seq_file *m, void *p)
2080 mutex_unlock(&trace_types_lock);
2083 static int t_show(struct seq_file *m, void *v)
2085 struct tracer *t = v;
2087 if (!t)
2088 return 0;
2090 seq_printf(m, "%s", t->name);
2091 if (t->next)
2092 seq_putc(m, ' ');
2093 else
2094 seq_putc(m, '\n');
2096 return 0;
2099 static struct seq_operations show_traces_seq_ops = {
2100 .start = t_start,
2101 .next = t_next,
2102 .stop = t_stop,
2103 .show = t_show,
2106 static int show_traces_open(struct inode *inode, struct file *file)
2108 if (tracing_disabled)
2109 return -ENODEV;
2111 return seq_open(file, &show_traces_seq_ops);
2114 static ssize_t
2115 tracing_write_stub(struct file *filp, const char __user *ubuf,
2116 size_t count, loff_t *ppos)
2118 return count;
2121 static const struct file_operations tracing_fops = {
2122 .open = tracing_open,
2123 .read = seq_read,
2124 .write = tracing_write_stub,
2125 .llseek = seq_lseek,
2126 .release = tracing_release,
2129 static const struct file_operations show_traces_fops = {
2130 .open = show_traces_open,
2131 .read = seq_read,
2132 .release = seq_release,
2136 * Only trace on a CPU if the bitmask is set:
2138 static cpumask_var_t tracing_cpumask;
2141 * The tracer itself will not take this lock, but still we want
2142 * to provide a consistent cpumask to user-space:
2144 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2147 * Temporary storage for the character representation of the
2148 * CPU bitmask (and one more byte for the newline):
2150 static char mask_str[NR_CPUS + 1];
2152 static ssize_t
2153 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2154 size_t count, loff_t *ppos)
2156 int len;
2158 mutex_lock(&tracing_cpumask_update_lock);
2160 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2161 if (count - len < 2) {
2162 count = -EINVAL;
2163 goto out_err;
2165 len += sprintf(mask_str + len, "\n");
2166 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2168 out_err:
2169 mutex_unlock(&tracing_cpumask_update_lock);
2171 return count;
2174 static ssize_t
2175 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2176 size_t count, loff_t *ppos)
2178 int err, cpu;
2179 cpumask_var_t tracing_cpumask_new;
2181 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2182 return -ENOMEM;
2184 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2185 if (err)
2186 goto err_unlock;
2188 mutex_lock(&tracing_cpumask_update_lock);
2190 local_irq_disable();
2191 __raw_spin_lock(&ftrace_max_lock);
2192 for_each_tracing_cpu(cpu) {
2194 * Increase/decrease the disabled counter if we are
2195 * about to flip a bit in the cpumask:
2197 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2198 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2199 atomic_inc(&global_trace.data[cpu]->disabled);
2201 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2202 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2203 atomic_dec(&global_trace.data[cpu]->disabled);
2206 __raw_spin_unlock(&ftrace_max_lock);
2207 local_irq_enable();
2209 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2211 mutex_unlock(&tracing_cpumask_update_lock);
2212 free_cpumask_var(tracing_cpumask_new);
2214 return count;
2216 err_unlock:
2217 free_cpumask_var(tracing_cpumask_new);
2219 return err;
2222 static const struct file_operations tracing_cpumask_fops = {
2223 .open = tracing_open_generic,
2224 .read = tracing_cpumask_read,
2225 .write = tracing_cpumask_write,
2228 static ssize_t
2229 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2230 size_t cnt, loff_t *ppos)
2232 struct tracer_opt *trace_opts;
2233 u32 tracer_flags;
2234 int len = 0;
2235 char *buf;
2236 int r = 0;
2237 int i;
2240 /* calculate max size */
2241 for (i = 0; trace_options[i]; i++) {
2242 len += strlen(trace_options[i]);
2243 len += 3; /* "no" and newline */
2246 mutex_lock(&trace_types_lock);
2247 tracer_flags = current_trace->flags->val;
2248 trace_opts = current_trace->flags->opts;
2251 * Increase the size with names of options specific
2252 * of the current tracer.
2254 for (i = 0; trace_opts[i].name; i++) {
2255 len += strlen(trace_opts[i].name);
2256 len += 3; /* "no" and newline */
2259 /* +2 for \n and \0 */
2260 buf = kmalloc(len + 2, GFP_KERNEL);
2261 if (!buf) {
2262 mutex_unlock(&trace_types_lock);
2263 return -ENOMEM;
2266 for (i = 0; trace_options[i]; i++) {
2267 if (trace_flags & (1 << i))
2268 r += sprintf(buf + r, "%s\n", trace_options[i]);
2269 else
2270 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2273 for (i = 0; trace_opts[i].name; i++) {
2274 if (tracer_flags & trace_opts[i].bit)
2275 r += sprintf(buf + r, "%s\n",
2276 trace_opts[i].name);
2277 else
2278 r += sprintf(buf + r, "no%s\n",
2279 trace_opts[i].name);
2281 mutex_unlock(&trace_types_lock);
2283 WARN_ON(r >= len + 2);
2285 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2287 kfree(buf);
2288 return r;
2291 /* Try to assign a tracer specific option */
2292 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2294 struct tracer_flags *trace_flags = trace->flags;
2295 struct tracer_opt *opts = NULL;
2296 int ret = 0, i = 0;
2297 int len;
2299 for (i = 0; trace_flags->opts[i].name; i++) {
2300 opts = &trace_flags->opts[i];
2301 len = strlen(opts->name);
2303 if (strncmp(cmp, opts->name, len) == 0) {
2304 ret = trace->set_flag(trace_flags->val,
2305 opts->bit, !neg);
2306 break;
2309 /* Not found */
2310 if (!trace_flags->opts[i].name)
2311 return -EINVAL;
2313 /* Refused to handle */
2314 if (ret)
2315 return ret;
2317 if (neg)
2318 trace_flags->val &= ~opts->bit;
2319 else
2320 trace_flags->val |= opts->bit;
2322 return 0;
2325 static void set_tracer_flags(unsigned int mask, int enabled)
2327 /* do nothing if flag is already set */
2328 if (!!(trace_flags & mask) == !!enabled)
2329 return;
2331 if (enabled)
2332 trace_flags |= mask;
2333 else
2334 trace_flags &= ~mask;
2336 if (mask == TRACE_ITER_GLOBAL_CLK) {
2337 u64 (*func)(void);
2339 if (enabled)
2340 func = trace_clock_global;
2341 else
2342 func = trace_clock_local;
2344 mutex_lock(&trace_types_lock);
2345 ring_buffer_set_clock(global_trace.buffer, func);
2347 if (max_tr.buffer)
2348 ring_buffer_set_clock(max_tr.buffer, func);
2349 mutex_unlock(&trace_types_lock);
2353 static ssize_t
2354 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2355 size_t cnt, loff_t *ppos)
2357 char buf[64];
2358 char *cmp = buf;
2359 int neg = 0;
2360 int ret;
2361 int i;
2363 if (cnt >= sizeof(buf))
2364 return -EINVAL;
2366 if (copy_from_user(&buf, ubuf, cnt))
2367 return -EFAULT;
2369 buf[cnt] = 0;
2371 if (strncmp(buf, "no", 2) == 0) {
2372 neg = 1;
2373 cmp += 2;
2376 for (i = 0; trace_options[i]; i++) {
2377 int len = strlen(trace_options[i]);
2379 if (strncmp(cmp, trace_options[i], len) == 0) {
2380 set_tracer_flags(1 << i, !neg);
2381 break;
2385 /* If no option could be set, test the specific tracer options */
2386 if (!trace_options[i]) {
2387 mutex_lock(&trace_types_lock);
2388 ret = set_tracer_option(current_trace, cmp, neg);
2389 mutex_unlock(&trace_types_lock);
2390 if (ret)
2391 return ret;
2394 filp->f_pos += cnt;
2396 return cnt;
2399 static const struct file_operations tracing_iter_fops = {
2400 .open = tracing_open_generic,
2401 .read = tracing_trace_options_read,
2402 .write = tracing_trace_options_write,
2405 static const char readme_msg[] =
2406 "tracing mini-HOWTO:\n\n"
2407 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2408 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2409 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2410 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2411 "nop\n"
2412 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2413 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2414 "sched_switch\n"
2415 "# cat /sys/kernel/debug/tracing/trace_options\n"
2416 "noprint-parent nosym-offset nosym-addr noverbose\n"
2417 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2418 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2419 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2420 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2423 static ssize_t
2424 tracing_readme_read(struct file *filp, char __user *ubuf,
2425 size_t cnt, loff_t *ppos)
2427 return simple_read_from_buffer(ubuf, cnt, ppos,
2428 readme_msg, strlen(readme_msg));
2431 static const struct file_operations tracing_readme_fops = {
2432 .open = tracing_open_generic,
2433 .read = tracing_readme_read,
2436 static ssize_t
2437 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2438 size_t cnt, loff_t *ppos)
2440 char *buf_comm;
2441 char *file_buf;
2442 char *buf;
2443 int len = 0;
2444 int pid;
2445 int i;
2447 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2448 if (!file_buf)
2449 return -ENOMEM;
2451 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2452 if (!buf_comm) {
2453 kfree(file_buf);
2454 return -ENOMEM;
2457 buf = file_buf;
2459 for (i = 0; i < SAVED_CMDLINES; i++) {
2460 int r;
2462 pid = map_cmdline_to_pid[i];
2463 if (pid == -1 || pid == NO_CMDLINE_MAP)
2464 continue;
2466 trace_find_cmdline(pid, buf_comm);
2467 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2468 buf += r;
2469 len += r;
2472 len = simple_read_from_buffer(ubuf, cnt, ppos,
2473 file_buf, len);
2475 kfree(file_buf);
2476 kfree(buf_comm);
2478 return len;
2481 static const struct file_operations tracing_saved_cmdlines_fops = {
2482 .open = tracing_open_generic,
2483 .read = tracing_saved_cmdlines_read,
2486 static ssize_t
2487 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2488 size_t cnt, loff_t *ppos)
2490 char buf[64];
2491 int r;
2493 r = sprintf(buf, "%u\n", tracer_enabled);
2494 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2497 static ssize_t
2498 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2499 size_t cnt, loff_t *ppos)
2501 struct trace_array *tr = filp->private_data;
2502 char buf[64];
2503 unsigned long val;
2504 int ret;
2506 if (cnt >= sizeof(buf))
2507 return -EINVAL;
2509 if (copy_from_user(&buf, ubuf, cnt))
2510 return -EFAULT;
2512 buf[cnt] = 0;
2514 ret = strict_strtoul(buf, 10, &val);
2515 if (ret < 0)
2516 return ret;
2518 val = !!val;
2520 mutex_lock(&trace_types_lock);
2521 if (tracer_enabled ^ val) {
2522 if (val) {
2523 tracer_enabled = 1;
2524 if (current_trace->start)
2525 current_trace->start(tr);
2526 tracing_start();
2527 } else {
2528 tracer_enabled = 0;
2529 tracing_stop();
2530 if (current_trace->stop)
2531 current_trace->stop(tr);
2534 mutex_unlock(&trace_types_lock);
2536 filp->f_pos += cnt;
2538 return cnt;
2541 static ssize_t
2542 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2543 size_t cnt, loff_t *ppos)
2545 char buf[max_tracer_type_len+2];
2546 int r;
2548 mutex_lock(&trace_types_lock);
2549 if (current_trace)
2550 r = sprintf(buf, "%s\n", current_trace->name);
2551 else
2552 r = sprintf(buf, "\n");
2553 mutex_unlock(&trace_types_lock);
2555 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2558 int tracer_init(struct tracer *t, struct trace_array *tr)
2560 tracing_reset_online_cpus(tr);
2561 return t->init(tr);
2564 static int tracing_resize_ring_buffer(unsigned long size)
2566 int ret;
2569 * If kernel or user changes the size of the ring buffer
2570 * we use the size that was given, and we can forget about
2571 * expanding it later.
2573 ring_buffer_expanded = 1;
2575 ret = ring_buffer_resize(global_trace.buffer, size);
2576 if (ret < 0)
2577 return ret;
2579 ret = ring_buffer_resize(max_tr.buffer, size);
2580 if (ret < 0) {
2581 int r;
2583 r = ring_buffer_resize(global_trace.buffer,
2584 global_trace.entries);
2585 if (r < 0) {
2587 * AARGH! We are left with different
2588 * size max buffer!!!!
2589 * The max buffer is our "snapshot" buffer.
2590 * When a tracer needs a snapshot (one of the
2591 * latency tracers), it swaps the max buffer
2592 * with the saved snap shot. We succeeded to
2593 * update the size of the main buffer, but failed to
2594 * update the size of the max buffer. But when we tried
2595 * to reset the main buffer to the original size, we
2596 * failed there too. This is very unlikely to
2597 * happen, but if it does, warn and kill all
2598 * tracing.
2600 WARN_ON(1);
2601 tracing_disabled = 1;
2603 return ret;
2606 global_trace.entries = size;
2608 return ret;
2612 * tracing_update_buffers - used by tracing facility to expand ring buffers
2614 * To save on memory when the tracing is never used on a system with it
2615 * configured in. The ring buffers are set to a minimum size. But once
2616 * a user starts to use the tracing facility, then they need to grow
2617 * to their default size.
2619 * This function is to be called when a tracer is about to be used.
2621 int tracing_update_buffers(void)
2623 int ret = 0;
2625 mutex_lock(&trace_types_lock);
2626 if (!ring_buffer_expanded)
2627 ret = tracing_resize_ring_buffer(trace_buf_size);
2628 mutex_unlock(&trace_types_lock);
2630 return ret;
2633 struct trace_option_dentry;
2635 static struct trace_option_dentry *
2636 create_trace_option_files(struct tracer *tracer);
2638 static void
2639 destroy_trace_option_files(struct trace_option_dentry *topts);
2641 static int tracing_set_tracer(const char *buf)
2643 static struct trace_option_dentry *topts;
2644 struct trace_array *tr = &global_trace;
2645 struct tracer *t;
2646 int ret = 0;
2648 mutex_lock(&trace_types_lock);
2650 if (!ring_buffer_expanded) {
2651 ret = tracing_resize_ring_buffer(trace_buf_size);
2652 if (ret < 0)
2653 goto out;
2654 ret = 0;
2657 for (t = trace_types; t; t = t->next) {
2658 if (strcmp(t->name, buf) == 0)
2659 break;
2661 if (!t) {
2662 ret = -EINVAL;
2663 goto out;
2665 if (t == current_trace)
2666 goto out;
2668 trace_branch_disable();
2669 if (current_trace && current_trace->reset)
2670 current_trace->reset(tr);
2672 destroy_trace_option_files(topts);
2674 current_trace = t;
2676 topts = create_trace_option_files(current_trace);
2678 if (t->init) {
2679 ret = tracer_init(t, tr);
2680 if (ret)
2681 goto out;
2684 trace_branch_enable(tr);
2685 out:
2686 mutex_unlock(&trace_types_lock);
2688 return ret;
2691 static ssize_t
2692 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2693 size_t cnt, loff_t *ppos)
2695 char buf[max_tracer_type_len+1];
2696 int i;
2697 size_t ret;
2698 int err;
2700 ret = cnt;
2702 if (cnt > max_tracer_type_len)
2703 cnt = max_tracer_type_len;
2705 if (copy_from_user(&buf, ubuf, cnt))
2706 return -EFAULT;
2708 buf[cnt] = 0;
2710 /* strip ending whitespace. */
2711 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2712 buf[i] = 0;
2714 err = tracing_set_tracer(buf);
2715 if (err)
2716 return err;
2718 filp->f_pos += ret;
2720 return ret;
2723 static ssize_t
2724 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2725 size_t cnt, loff_t *ppos)
2727 unsigned long *ptr = filp->private_data;
2728 char buf[64];
2729 int r;
2731 r = snprintf(buf, sizeof(buf), "%ld\n",
2732 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2733 if (r > sizeof(buf))
2734 r = sizeof(buf);
2735 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2738 static ssize_t
2739 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2740 size_t cnt, loff_t *ppos)
2742 unsigned long *ptr = filp->private_data;
2743 char buf[64];
2744 unsigned long val;
2745 int ret;
2747 if (cnt >= sizeof(buf))
2748 return -EINVAL;
2750 if (copy_from_user(&buf, ubuf, cnt))
2751 return -EFAULT;
2753 buf[cnt] = 0;
2755 ret = strict_strtoul(buf, 10, &val);
2756 if (ret < 0)
2757 return ret;
2759 *ptr = val * 1000;
2761 return cnt;
2764 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2766 long cpu_file = (long) inode->i_private;
2767 struct trace_iterator *iter;
2768 int ret = 0;
2770 if (tracing_disabled)
2771 return -ENODEV;
2773 mutex_lock(&trace_types_lock);
2775 /* We only allow one reader per cpu */
2776 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2777 if (!cpumask_empty(tracing_reader_cpumask)) {
2778 ret = -EBUSY;
2779 goto out;
2781 cpumask_setall(tracing_reader_cpumask);
2782 } else {
2783 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2784 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2785 else {
2786 ret = -EBUSY;
2787 goto out;
2791 /* create a buffer to store the information to pass to userspace */
2792 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2793 if (!iter) {
2794 ret = -ENOMEM;
2795 goto out;
2799 * We make a copy of the current tracer to avoid concurrent
2800 * changes on it while we are reading.
2802 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2803 if (!iter->trace) {
2804 ret = -ENOMEM;
2805 goto fail;
2807 if (current_trace)
2808 *iter->trace = *current_trace;
2810 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2811 ret = -ENOMEM;
2812 goto fail;
2815 /* trace pipe does not show start of buffer */
2816 cpumask_setall(iter->started);
2818 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2819 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2821 iter->cpu_file = cpu_file;
2822 iter->tr = &global_trace;
2823 mutex_init(&iter->mutex);
2824 filp->private_data = iter;
2826 if (iter->trace->pipe_open)
2827 iter->trace->pipe_open(iter);
2829 out:
2830 mutex_unlock(&trace_types_lock);
2831 return ret;
2833 fail:
2834 kfree(iter->trace);
2835 kfree(iter);
2836 mutex_unlock(&trace_types_lock);
2837 return ret;
2840 static int tracing_release_pipe(struct inode *inode, struct file *file)
2842 struct trace_iterator *iter = file->private_data;
2844 mutex_lock(&trace_types_lock);
2846 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2847 cpumask_clear(tracing_reader_cpumask);
2848 else
2849 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2851 mutex_unlock(&trace_types_lock);
2853 free_cpumask_var(iter->started);
2854 mutex_destroy(&iter->mutex);
2855 kfree(iter->trace);
2856 kfree(iter);
2858 return 0;
2861 static unsigned int
2862 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2864 struct trace_iterator *iter = filp->private_data;
2866 if (trace_flags & TRACE_ITER_BLOCK) {
2868 * Always select as readable when in blocking mode
2870 return POLLIN | POLLRDNORM;
2871 } else {
2872 if (!trace_empty(iter))
2873 return POLLIN | POLLRDNORM;
2874 poll_wait(filp, &trace_wait, poll_table);
2875 if (!trace_empty(iter))
2876 return POLLIN | POLLRDNORM;
2878 return 0;
2883 void default_wait_pipe(struct trace_iterator *iter)
2885 DEFINE_WAIT(wait);
2887 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2889 if (trace_empty(iter))
2890 schedule();
2892 finish_wait(&trace_wait, &wait);
2896 * This is a make-shift waitqueue.
2897 * A tracer might use this callback on some rare cases:
2899 * 1) the current tracer might hold the runqueue lock when it wakes up
2900 * a reader, hence a deadlock (sched, function, and function graph tracers)
2901 * 2) the function tracers, trace all functions, we don't want
2902 * the overhead of calling wake_up and friends
2903 * (and tracing them too)
2905 * Anyway, this is really very primitive wakeup.
2907 void poll_wait_pipe(struct trace_iterator *iter)
2909 set_current_state(TASK_INTERRUPTIBLE);
2910 /* sleep for 100 msecs, and try again. */
2911 schedule_timeout(HZ / 10);
2914 /* Must be called with trace_types_lock mutex held. */
2915 static int tracing_wait_pipe(struct file *filp)
2917 struct trace_iterator *iter = filp->private_data;
2919 while (trace_empty(iter)) {
2921 if ((filp->f_flags & O_NONBLOCK)) {
2922 return -EAGAIN;
2925 mutex_unlock(&iter->mutex);
2927 iter->trace->wait_pipe(iter);
2929 mutex_lock(&iter->mutex);
2931 if (signal_pending(current))
2932 return -EINTR;
2935 * We block until we read something and tracing is disabled.
2936 * We still block if tracing is disabled, but we have never
2937 * read anything. This allows a user to cat this file, and
2938 * then enable tracing. But after we have read something,
2939 * we give an EOF when tracing is again disabled.
2941 * iter->pos will be 0 if we haven't read anything.
2943 if (!tracer_enabled && iter->pos)
2944 break;
2947 return 1;
2951 * Consumer reader.
2953 static ssize_t
2954 tracing_read_pipe(struct file *filp, char __user *ubuf,
2955 size_t cnt, loff_t *ppos)
2957 struct trace_iterator *iter = filp->private_data;
2958 static struct tracer *old_tracer;
2959 ssize_t sret;
2961 /* return any leftover data */
2962 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2963 if (sret != -EBUSY)
2964 return sret;
2966 trace_seq_init(&iter->seq);
2968 /* copy the tracer to avoid using a global lock all around */
2969 mutex_lock(&trace_types_lock);
2970 if (unlikely(old_tracer != current_trace && current_trace)) {
2971 old_tracer = current_trace;
2972 *iter->trace = *current_trace;
2974 mutex_unlock(&trace_types_lock);
2977 * Avoid more than one consumer on a single file descriptor
2978 * This is just a matter of traces coherency, the ring buffer itself
2979 * is protected.
2981 mutex_lock(&iter->mutex);
2982 if (iter->trace->read) {
2983 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2984 if (sret)
2985 goto out;
2988 waitagain:
2989 sret = tracing_wait_pipe(filp);
2990 if (sret <= 0)
2991 goto out;
2993 /* stop when tracing is finished */
2994 if (trace_empty(iter)) {
2995 sret = 0;
2996 goto out;
2999 if (cnt >= PAGE_SIZE)
3000 cnt = PAGE_SIZE - 1;
3002 /* reset all but tr, trace, and overruns */
3003 memset(&iter->seq, 0,
3004 sizeof(struct trace_iterator) -
3005 offsetof(struct trace_iterator, seq));
3006 iter->pos = -1;
3008 trace_event_read_lock();
3009 while (find_next_entry_inc(iter) != NULL) {
3010 enum print_line_t ret;
3011 int len = iter->seq.len;
3013 ret = print_trace_line(iter);
3014 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3015 /* don't print partial lines */
3016 iter->seq.len = len;
3017 break;
3019 if (ret != TRACE_TYPE_NO_CONSUME)
3020 trace_consume(iter);
3022 if (iter->seq.len >= cnt)
3023 break;
3025 trace_event_read_unlock();
3027 /* Now copy what we have to the user */
3028 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3029 if (iter->seq.readpos >= iter->seq.len)
3030 trace_seq_init(&iter->seq);
3033 * If there was nothing to send to user, inspite of consuming trace
3034 * entries, go back to wait for more entries.
3036 if (sret == -EBUSY)
3037 goto waitagain;
3039 out:
3040 mutex_unlock(&iter->mutex);
3042 return sret;
3045 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3046 struct pipe_buffer *buf)
3048 __free_page(buf->page);
3051 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3052 unsigned int idx)
3054 __free_page(spd->pages[idx]);
3057 static struct pipe_buf_operations tracing_pipe_buf_ops = {
3058 .can_merge = 0,
3059 .map = generic_pipe_buf_map,
3060 .unmap = generic_pipe_buf_unmap,
3061 .confirm = generic_pipe_buf_confirm,
3062 .release = tracing_pipe_buf_release,
3063 .steal = generic_pipe_buf_steal,
3064 .get = generic_pipe_buf_get,
3067 static size_t
3068 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3070 size_t count;
3071 int ret;
3073 /* Seq buffer is page-sized, exactly what we need. */
3074 for (;;) {
3075 count = iter->seq.len;
3076 ret = print_trace_line(iter);
3077 count = iter->seq.len - count;
3078 if (rem < count) {
3079 rem = 0;
3080 iter->seq.len -= count;
3081 break;
3083 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3084 iter->seq.len -= count;
3085 break;
3088 trace_consume(iter);
3089 rem -= count;
3090 if (!find_next_entry_inc(iter)) {
3091 rem = 0;
3092 iter->ent = NULL;
3093 break;
3097 return rem;
3100 static ssize_t tracing_splice_read_pipe(struct file *filp,
3101 loff_t *ppos,
3102 struct pipe_inode_info *pipe,
3103 size_t len,
3104 unsigned int flags)
3106 struct page *pages[PIPE_BUFFERS];
3107 struct partial_page partial[PIPE_BUFFERS];
3108 struct trace_iterator *iter = filp->private_data;
3109 struct splice_pipe_desc spd = {
3110 .pages = pages,
3111 .partial = partial,
3112 .nr_pages = 0, /* This gets updated below. */
3113 .flags = flags,
3114 .ops = &tracing_pipe_buf_ops,
3115 .spd_release = tracing_spd_release_pipe,
3117 static struct tracer *old_tracer;
3118 ssize_t ret;
3119 size_t rem;
3120 unsigned int i;
3122 /* copy the tracer to avoid using a global lock all around */
3123 mutex_lock(&trace_types_lock);
3124 if (unlikely(old_tracer != current_trace && current_trace)) {
3125 old_tracer = current_trace;
3126 *iter->trace = *current_trace;
3128 mutex_unlock(&trace_types_lock);
3130 mutex_lock(&iter->mutex);
3132 if (iter->trace->splice_read) {
3133 ret = iter->trace->splice_read(iter, filp,
3134 ppos, pipe, len, flags);
3135 if (ret)
3136 goto out_err;
3139 ret = tracing_wait_pipe(filp);
3140 if (ret <= 0)
3141 goto out_err;
3143 if (!iter->ent && !find_next_entry_inc(iter)) {
3144 ret = -EFAULT;
3145 goto out_err;
3148 trace_event_read_lock();
3150 /* Fill as many pages as possible. */
3151 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3152 pages[i] = alloc_page(GFP_KERNEL);
3153 if (!pages[i])
3154 break;
3156 rem = tracing_fill_pipe_page(rem, iter);
3158 /* Copy the data into the page, so we can start over. */
3159 ret = trace_seq_to_buffer(&iter->seq,
3160 page_address(pages[i]),
3161 iter->seq.len);
3162 if (ret < 0) {
3163 __free_page(pages[i]);
3164 break;
3166 partial[i].offset = 0;
3167 partial[i].len = iter->seq.len;
3169 trace_seq_init(&iter->seq);
3172 trace_event_read_unlock();
3173 mutex_unlock(&iter->mutex);
3175 spd.nr_pages = i;
3177 return splice_to_pipe(pipe, &spd);
3179 out_err:
3180 mutex_unlock(&iter->mutex);
3182 return ret;
3185 static ssize_t
3186 tracing_entries_read(struct file *filp, char __user *ubuf,
3187 size_t cnt, loff_t *ppos)
3189 struct trace_array *tr = filp->private_data;
3190 char buf[96];
3191 int r;
3193 mutex_lock(&trace_types_lock);
3194 if (!ring_buffer_expanded)
3195 r = sprintf(buf, "%lu (expanded: %lu)\n",
3196 tr->entries >> 10,
3197 trace_buf_size >> 10);
3198 else
3199 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3200 mutex_unlock(&trace_types_lock);
3202 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3205 static ssize_t
3206 tracing_entries_write(struct file *filp, const char __user *ubuf,
3207 size_t cnt, loff_t *ppos)
3209 unsigned long val;
3210 char buf[64];
3211 int ret, cpu;
3213 if (cnt >= sizeof(buf))
3214 return -EINVAL;
3216 if (copy_from_user(&buf, ubuf, cnt))
3217 return -EFAULT;
3219 buf[cnt] = 0;
3221 ret = strict_strtoul(buf, 10, &val);
3222 if (ret < 0)
3223 return ret;
3225 /* must have at least 1 entry */
3226 if (!val)
3227 return -EINVAL;
3229 mutex_lock(&trace_types_lock);
3231 tracing_stop();
3233 /* disable all cpu buffers */
3234 for_each_tracing_cpu(cpu) {
3235 if (global_trace.data[cpu])
3236 atomic_inc(&global_trace.data[cpu]->disabled);
3237 if (max_tr.data[cpu])
3238 atomic_inc(&max_tr.data[cpu]->disabled);
3241 /* value is in KB */
3242 val <<= 10;
3244 if (val != global_trace.entries) {
3245 ret = tracing_resize_ring_buffer(val);
3246 if (ret < 0) {
3247 cnt = ret;
3248 goto out;
3252 filp->f_pos += cnt;
3254 /* If check pages failed, return ENOMEM */
3255 if (tracing_disabled)
3256 cnt = -ENOMEM;
3257 out:
3258 for_each_tracing_cpu(cpu) {
3259 if (global_trace.data[cpu])
3260 atomic_dec(&global_trace.data[cpu]->disabled);
3261 if (max_tr.data[cpu])
3262 atomic_dec(&max_tr.data[cpu]->disabled);
3265 tracing_start();
3266 max_tr.entries = global_trace.entries;
3267 mutex_unlock(&trace_types_lock);
3269 return cnt;
3272 static int mark_printk(const char *fmt, ...)
3274 int ret;
3275 va_list args;
3276 va_start(args, fmt);
3277 ret = trace_vprintk(0, fmt, args);
3278 va_end(args);
3279 return ret;
3282 static ssize_t
3283 tracing_mark_write(struct file *filp, const char __user *ubuf,
3284 size_t cnt, loff_t *fpos)
3286 char *buf;
3287 char *end;
3289 if (tracing_disabled)
3290 return -EINVAL;
3292 if (cnt > TRACE_BUF_SIZE)
3293 cnt = TRACE_BUF_SIZE;
3295 buf = kmalloc(cnt + 1, GFP_KERNEL);
3296 if (buf == NULL)
3297 return -ENOMEM;
3299 if (copy_from_user(buf, ubuf, cnt)) {
3300 kfree(buf);
3301 return -EFAULT;
3304 /* Cut from the first nil or newline. */
3305 buf[cnt] = '\0';
3306 end = strchr(buf, '\n');
3307 if (end)
3308 *end = '\0';
3310 cnt = mark_printk("%s\n", buf);
3311 kfree(buf);
3312 *fpos += cnt;
3314 return cnt;
3317 static const struct file_operations tracing_max_lat_fops = {
3318 .open = tracing_open_generic,
3319 .read = tracing_max_lat_read,
3320 .write = tracing_max_lat_write,
3323 static const struct file_operations tracing_ctrl_fops = {
3324 .open = tracing_open_generic,
3325 .read = tracing_ctrl_read,
3326 .write = tracing_ctrl_write,
3329 static const struct file_operations set_tracer_fops = {
3330 .open = tracing_open_generic,
3331 .read = tracing_set_trace_read,
3332 .write = tracing_set_trace_write,
3335 static const struct file_operations tracing_pipe_fops = {
3336 .open = tracing_open_pipe,
3337 .poll = tracing_poll_pipe,
3338 .read = tracing_read_pipe,
3339 .splice_read = tracing_splice_read_pipe,
3340 .release = tracing_release_pipe,
3343 static const struct file_operations tracing_entries_fops = {
3344 .open = tracing_open_generic,
3345 .read = tracing_entries_read,
3346 .write = tracing_entries_write,
3349 static const struct file_operations tracing_mark_fops = {
3350 .open = tracing_open_generic,
3351 .write = tracing_mark_write,
3354 struct ftrace_buffer_info {
3355 struct trace_array *tr;
3356 void *spare;
3357 int cpu;
3358 unsigned int read;
3361 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3363 int cpu = (int)(long)inode->i_private;
3364 struct ftrace_buffer_info *info;
3366 if (tracing_disabled)
3367 return -ENODEV;
3369 info = kzalloc(sizeof(*info), GFP_KERNEL);
3370 if (!info)
3371 return -ENOMEM;
3373 info->tr = &global_trace;
3374 info->cpu = cpu;
3375 info->spare = NULL;
3376 /* Force reading ring buffer for first read */
3377 info->read = (unsigned int)-1;
3379 filp->private_data = info;
3381 return nonseekable_open(inode, filp);
3384 static ssize_t
3385 tracing_buffers_read(struct file *filp, char __user *ubuf,
3386 size_t count, loff_t *ppos)
3388 struct ftrace_buffer_info *info = filp->private_data;
3389 unsigned int pos;
3390 ssize_t ret;
3391 size_t size;
3393 if (!count)
3394 return 0;
3396 if (!info->spare)
3397 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3398 if (!info->spare)
3399 return -ENOMEM;
3401 /* Do we have previous read data to read? */
3402 if (info->read < PAGE_SIZE)
3403 goto read;
3405 info->read = 0;
3407 ret = ring_buffer_read_page(info->tr->buffer,
3408 &info->spare,
3409 count,
3410 info->cpu, 0);
3411 if (ret < 0)
3412 return 0;
3414 pos = ring_buffer_page_len(info->spare);
3416 if (pos < PAGE_SIZE)
3417 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3419 read:
3420 size = PAGE_SIZE - info->read;
3421 if (size > count)
3422 size = count;
3424 ret = copy_to_user(ubuf, info->spare + info->read, size);
3425 if (ret == size)
3426 return -EFAULT;
3427 size -= ret;
3429 *ppos += size;
3430 info->read += size;
3432 return size;
3435 static int tracing_buffers_release(struct inode *inode, struct file *file)
3437 struct ftrace_buffer_info *info = file->private_data;
3439 if (info->spare)
3440 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3441 kfree(info);
3443 return 0;
3446 struct buffer_ref {
3447 struct ring_buffer *buffer;
3448 void *page;
3449 int ref;
3452 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3453 struct pipe_buffer *buf)
3455 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3457 if (--ref->ref)
3458 return;
3460 ring_buffer_free_read_page(ref->buffer, ref->page);
3461 kfree(ref);
3462 buf->private = 0;
3465 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3466 struct pipe_buffer *buf)
3468 return 1;
3471 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3472 struct pipe_buffer *buf)
3474 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3476 ref->ref++;
3479 /* Pipe buffer operations for a buffer. */
3480 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3481 .can_merge = 0,
3482 .map = generic_pipe_buf_map,
3483 .unmap = generic_pipe_buf_unmap,
3484 .confirm = generic_pipe_buf_confirm,
3485 .release = buffer_pipe_buf_release,
3486 .steal = buffer_pipe_buf_steal,
3487 .get = buffer_pipe_buf_get,
3491 * Callback from splice_to_pipe(), if we need to release some pages
3492 * at the end of the spd in case we error'ed out in filling the pipe.
3494 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3496 struct buffer_ref *ref =
3497 (struct buffer_ref *)spd->partial[i].private;
3499 if (--ref->ref)
3500 return;
3502 ring_buffer_free_read_page(ref->buffer, ref->page);
3503 kfree(ref);
3504 spd->partial[i].private = 0;
3507 static ssize_t
3508 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3509 struct pipe_inode_info *pipe, size_t len,
3510 unsigned int flags)
3512 struct ftrace_buffer_info *info = file->private_data;
3513 struct partial_page partial[PIPE_BUFFERS];
3514 struct page *pages[PIPE_BUFFERS];
3515 struct splice_pipe_desc spd = {
3516 .pages = pages,
3517 .partial = partial,
3518 .flags = flags,
3519 .ops = &buffer_pipe_buf_ops,
3520 .spd_release = buffer_spd_release,
3522 struct buffer_ref *ref;
3523 int entries, size, i;
3524 size_t ret;
3526 if (*ppos & (PAGE_SIZE - 1)) {
3527 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3528 return -EINVAL;
3531 if (len & (PAGE_SIZE - 1)) {
3532 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3533 if (len < PAGE_SIZE)
3534 return -EINVAL;
3535 len &= PAGE_MASK;
3538 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3540 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
3541 struct page *page;
3542 int r;
3544 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3545 if (!ref)
3546 break;
3548 ref->ref = 1;
3549 ref->buffer = info->tr->buffer;
3550 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3551 if (!ref->page) {
3552 kfree(ref);
3553 break;
3556 r = ring_buffer_read_page(ref->buffer, &ref->page,
3557 len, info->cpu, 1);
3558 if (r < 0) {
3559 ring_buffer_free_read_page(ref->buffer,
3560 ref->page);
3561 kfree(ref);
3562 break;
3566 * zero out any left over data, this is going to
3567 * user land.
3569 size = ring_buffer_page_len(ref->page);
3570 if (size < PAGE_SIZE)
3571 memset(ref->page + size, 0, PAGE_SIZE - size);
3573 page = virt_to_page(ref->page);
3575 spd.pages[i] = page;
3576 spd.partial[i].len = PAGE_SIZE;
3577 spd.partial[i].offset = 0;
3578 spd.partial[i].private = (unsigned long)ref;
3579 spd.nr_pages++;
3580 *ppos += PAGE_SIZE;
3582 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3585 spd.nr_pages = i;
3587 /* did we read anything? */
3588 if (!spd.nr_pages) {
3589 if (flags & SPLICE_F_NONBLOCK)
3590 ret = -EAGAIN;
3591 else
3592 ret = 0;
3593 /* TODO: block */
3594 return ret;
3597 ret = splice_to_pipe(pipe, &spd);
3599 return ret;
3602 static const struct file_operations tracing_buffers_fops = {
3603 .open = tracing_buffers_open,
3604 .read = tracing_buffers_read,
3605 .release = tracing_buffers_release,
3606 .splice_read = tracing_buffers_splice_read,
3607 .llseek = no_llseek,
3610 static ssize_t
3611 tracing_stats_read(struct file *filp, char __user *ubuf,
3612 size_t count, loff_t *ppos)
3614 unsigned long cpu = (unsigned long)filp->private_data;
3615 struct trace_array *tr = &global_trace;
3616 struct trace_seq *s;
3617 unsigned long cnt;
3619 s = kmalloc(sizeof(*s), GFP_KERNEL);
3620 if (!s)
3621 return ENOMEM;
3623 trace_seq_init(s);
3625 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3626 trace_seq_printf(s, "entries: %ld\n", cnt);
3628 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3629 trace_seq_printf(s, "overrun: %ld\n", cnt);
3631 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3632 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3634 cnt = ring_buffer_nmi_dropped_cpu(tr->buffer, cpu);
3635 trace_seq_printf(s, "nmi dropped: %ld\n", cnt);
3637 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3639 kfree(s);
3641 return count;
3644 static const struct file_operations tracing_stats_fops = {
3645 .open = tracing_open_generic,
3646 .read = tracing_stats_read,
3649 #ifdef CONFIG_DYNAMIC_FTRACE
3651 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3653 return 0;
3656 static ssize_t
3657 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3658 size_t cnt, loff_t *ppos)
3660 static char ftrace_dyn_info_buffer[1024];
3661 static DEFINE_MUTEX(dyn_info_mutex);
3662 unsigned long *p = filp->private_data;
3663 char *buf = ftrace_dyn_info_buffer;
3664 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3665 int r;
3667 mutex_lock(&dyn_info_mutex);
3668 r = sprintf(buf, "%ld ", *p);
3670 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3671 buf[r++] = '\n';
3673 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3675 mutex_unlock(&dyn_info_mutex);
3677 return r;
3680 static const struct file_operations tracing_dyn_info_fops = {
3681 .open = tracing_open_generic,
3682 .read = tracing_read_dyn_info,
3684 #endif
3686 static struct dentry *d_tracer;
3688 struct dentry *tracing_init_dentry(void)
3690 static int once;
3692 if (d_tracer)
3693 return d_tracer;
3695 if (!debugfs_initialized())
3696 return NULL;
3698 d_tracer = debugfs_create_dir("tracing", NULL);
3700 if (!d_tracer && !once) {
3701 once = 1;
3702 pr_warning("Could not create debugfs directory 'tracing'\n");
3703 return NULL;
3706 return d_tracer;
3709 static struct dentry *d_percpu;
3711 struct dentry *tracing_dentry_percpu(void)
3713 static int once;
3714 struct dentry *d_tracer;
3716 if (d_percpu)
3717 return d_percpu;
3719 d_tracer = tracing_init_dentry();
3721 if (!d_tracer)
3722 return NULL;
3724 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3726 if (!d_percpu && !once) {
3727 once = 1;
3728 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3729 return NULL;
3732 return d_percpu;
3735 static void tracing_init_debugfs_percpu(long cpu)
3737 struct dentry *d_percpu = tracing_dentry_percpu();
3738 struct dentry *d_cpu;
3739 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3740 char cpu_dir[7];
3742 if (cpu > 999 || cpu < 0)
3743 return;
3745 sprintf(cpu_dir, "cpu%ld", cpu);
3746 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3747 if (!d_cpu) {
3748 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3749 return;
3752 /* per cpu trace_pipe */
3753 trace_create_file("trace_pipe", 0444, d_cpu,
3754 (void *) cpu, &tracing_pipe_fops);
3756 /* per cpu trace */
3757 trace_create_file("trace", 0644, d_cpu,
3758 (void *) cpu, &tracing_fops);
3760 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3761 (void *) cpu, &tracing_buffers_fops);
3763 trace_create_file("stats", 0444, d_cpu,
3764 (void *) cpu, &tracing_stats_fops);
3767 #ifdef CONFIG_FTRACE_SELFTEST
3768 /* Let selftest have access to static functions in this file */
3769 #include "trace_selftest.c"
3770 #endif
3772 struct trace_option_dentry {
3773 struct tracer_opt *opt;
3774 struct tracer_flags *flags;
3775 struct dentry *entry;
3778 static ssize_t
3779 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3780 loff_t *ppos)
3782 struct trace_option_dentry *topt = filp->private_data;
3783 char *buf;
3785 if (topt->flags->val & topt->opt->bit)
3786 buf = "1\n";
3787 else
3788 buf = "0\n";
3790 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3793 static ssize_t
3794 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3795 loff_t *ppos)
3797 struct trace_option_dentry *topt = filp->private_data;
3798 unsigned long val;
3799 char buf[64];
3800 int ret;
3802 if (cnt >= sizeof(buf))
3803 return -EINVAL;
3805 if (copy_from_user(&buf, ubuf, cnt))
3806 return -EFAULT;
3808 buf[cnt] = 0;
3810 ret = strict_strtoul(buf, 10, &val);
3811 if (ret < 0)
3812 return ret;
3814 ret = 0;
3815 switch (val) {
3816 case 0:
3817 /* do nothing if already cleared */
3818 if (!(topt->flags->val & topt->opt->bit))
3819 break;
3821 mutex_lock(&trace_types_lock);
3822 if (current_trace->set_flag)
3823 ret = current_trace->set_flag(topt->flags->val,
3824 topt->opt->bit, 0);
3825 mutex_unlock(&trace_types_lock);
3826 if (ret)
3827 return ret;
3828 topt->flags->val &= ~topt->opt->bit;
3829 break;
3830 case 1:
3831 /* do nothing if already set */
3832 if (topt->flags->val & topt->opt->bit)
3833 break;
3835 mutex_lock(&trace_types_lock);
3836 if (current_trace->set_flag)
3837 ret = current_trace->set_flag(topt->flags->val,
3838 topt->opt->bit, 1);
3839 mutex_unlock(&trace_types_lock);
3840 if (ret)
3841 return ret;
3842 topt->flags->val |= topt->opt->bit;
3843 break;
3845 default:
3846 return -EINVAL;
3849 *ppos += cnt;
3851 return cnt;
3855 static const struct file_operations trace_options_fops = {
3856 .open = tracing_open_generic,
3857 .read = trace_options_read,
3858 .write = trace_options_write,
3861 static ssize_t
3862 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3863 loff_t *ppos)
3865 long index = (long)filp->private_data;
3866 char *buf;
3868 if (trace_flags & (1 << index))
3869 buf = "1\n";
3870 else
3871 buf = "0\n";
3873 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3876 static ssize_t
3877 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3878 loff_t *ppos)
3880 long index = (long)filp->private_data;
3881 char buf[64];
3882 unsigned long val;
3883 int ret;
3885 if (cnt >= sizeof(buf))
3886 return -EINVAL;
3888 if (copy_from_user(&buf, ubuf, cnt))
3889 return -EFAULT;
3891 buf[cnt] = 0;
3893 ret = strict_strtoul(buf, 10, &val);
3894 if (ret < 0)
3895 return ret;
3897 switch (val) {
3898 case 0:
3899 trace_flags &= ~(1 << index);
3900 break;
3901 case 1:
3902 trace_flags |= 1 << index;
3903 break;
3905 default:
3906 return -EINVAL;
3909 *ppos += cnt;
3911 return cnt;
3914 static const struct file_operations trace_options_core_fops = {
3915 .open = tracing_open_generic,
3916 .read = trace_options_core_read,
3917 .write = trace_options_core_write,
3920 struct dentry *trace_create_file(const char *name,
3921 mode_t mode,
3922 struct dentry *parent,
3923 void *data,
3924 const struct file_operations *fops)
3926 struct dentry *ret;
3928 ret = debugfs_create_file(name, mode, parent, data, fops);
3929 if (!ret)
3930 pr_warning("Could not create debugfs '%s' entry\n", name);
3932 return ret;
3936 static struct dentry *trace_options_init_dentry(void)
3938 struct dentry *d_tracer;
3939 static struct dentry *t_options;
3941 if (t_options)
3942 return t_options;
3944 d_tracer = tracing_init_dentry();
3945 if (!d_tracer)
3946 return NULL;
3948 t_options = debugfs_create_dir("options", d_tracer);
3949 if (!t_options) {
3950 pr_warning("Could not create debugfs directory 'options'\n");
3951 return NULL;
3954 return t_options;
3957 static void
3958 create_trace_option_file(struct trace_option_dentry *topt,
3959 struct tracer_flags *flags,
3960 struct tracer_opt *opt)
3962 struct dentry *t_options;
3964 t_options = trace_options_init_dentry();
3965 if (!t_options)
3966 return;
3968 topt->flags = flags;
3969 topt->opt = opt;
3971 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
3972 &trace_options_fops);
3976 static struct trace_option_dentry *
3977 create_trace_option_files(struct tracer *tracer)
3979 struct trace_option_dentry *topts;
3980 struct tracer_flags *flags;
3981 struct tracer_opt *opts;
3982 int cnt;
3984 if (!tracer)
3985 return NULL;
3987 flags = tracer->flags;
3989 if (!flags || !flags->opts)
3990 return NULL;
3992 opts = flags->opts;
3994 for (cnt = 0; opts[cnt].name; cnt++)
3997 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3998 if (!topts)
3999 return NULL;
4001 for (cnt = 0; opts[cnt].name; cnt++)
4002 create_trace_option_file(&topts[cnt], flags,
4003 &opts[cnt]);
4005 return topts;
4008 static void
4009 destroy_trace_option_files(struct trace_option_dentry *topts)
4011 int cnt;
4013 if (!topts)
4014 return;
4016 for (cnt = 0; topts[cnt].opt; cnt++) {
4017 if (topts[cnt].entry)
4018 debugfs_remove(topts[cnt].entry);
4021 kfree(topts);
4024 static struct dentry *
4025 create_trace_option_core_file(const char *option, long index)
4027 struct dentry *t_options;
4029 t_options = trace_options_init_dentry();
4030 if (!t_options)
4031 return NULL;
4033 return trace_create_file(option, 0644, t_options, (void *)index,
4034 &trace_options_core_fops);
4037 static __init void create_trace_options_dir(void)
4039 struct dentry *t_options;
4040 int i;
4042 t_options = trace_options_init_dentry();
4043 if (!t_options)
4044 return;
4046 for (i = 0; trace_options[i]; i++)
4047 create_trace_option_core_file(trace_options[i], i);
4050 static __init int tracer_init_debugfs(void)
4052 struct dentry *d_tracer;
4053 int cpu;
4055 d_tracer = tracing_init_dentry();
4057 trace_create_file("tracing_enabled", 0644, d_tracer,
4058 &global_trace, &tracing_ctrl_fops);
4060 trace_create_file("trace_options", 0644, d_tracer,
4061 NULL, &tracing_iter_fops);
4063 trace_create_file("tracing_cpumask", 0644, d_tracer,
4064 NULL, &tracing_cpumask_fops);
4066 trace_create_file("trace", 0644, d_tracer,
4067 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4069 trace_create_file("available_tracers", 0444, d_tracer,
4070 &global_trace, &show_traces_fops);
4072 trace_create_file("current_tracer", 0644, d_tracer,
4073 &global_trace, &set_tracer_fops);
4075 trace_create_file("tracing_max_latency", 0644, d_tracer,
4076 &tracing_max_latency, &tracing_max_lat_fops);
4078 trace_create_file("tracing_thresh", 0644, d_tracer,
4079 &tracing_thresh, &tracing_max_lat_fops);
4081 trace_create_file("README", 0444, d_tracer,
4082 NULL, &tracing_readme_fops);
4084 trace_create_file("trace_pipe", 0444, d_tracer,
4085 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4087 trace_create_file("buffer_size_kb", 0644, d_tracer,
4088 &global_trace, &tracing_entries_fops);
4090 trace_create_file("trace_marker", 0220, d_tracer,
4091 NULL, &tracing_mark_fops);
4093 trace_create_file("saved_cmdlines", 0444, d_tracer,
4094 NULL, &tracing_saved_cmdlines_fops);
4096 #ifdef CONFIG_DYNAMIC_FTRACE
4097 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4098 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4099 #endif
4100 #ifdef CONFIG_SYSPROF_TRACER
4101 init_tracer_sysprof_debugfs(d_tracer);
4102 #endif
4104 create_trace_options_dir();
4106 for_each_tracing_cpu(cpu)
4107 tracing_init_debugfs_percpu(cpu);
4109 return 0;
4112 static int trace_panic_handler(struct notifier_block *this,
4113 unsigned long event, void *unused)
4115 if (ftrace_dump_on_oops)
4116 ftrace_dump();
4117 return NOTIFY_OK;
4120 static struct notifier_block trace_panic_notifier = {
4121 .notifier_call = trace_panic_handler,
4122 .next = NULL,
4123 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4126 static int trace_die_handler(struct notifier_block *self,
4127 unsigned long val,
4128 void *data)
4130 switch (val) {
4131 case DIE_OOPS:
4132 if (ftrace_dump_on_oops)
4133 ftrace_dump();
4134 break;
4135 default:
4136 break;
4138 return NOTIFY_OK;
4141 static struct notifier_block trace_die_notifier = {
4142 .notifier_call = trace_die_handler,
4143 .priority = 200
4147 * printk is set to max of 1024, we really don't need it that big.
4148 * Nothing should be printing 1000 characters anyway.
4150 #define TRACE_MAX_PRINT 1000
4153 * Define here KERN_TRACE so that we have one place to modify
4154 * it if we decide to change what log level the ftrace dump
4155 * should be at.
4157 #define KERN_TRACE KERN_EMERG
4159 static void
4160 trace_printk_seq(struct trace_seq *s)
4162 /* Probably should print a warning here. */
4163 if (s->len >= 1000)
4164 s->len = 1000;
4166 /* should be zero ended, but we are paranoid. */
4167 s->buffer[s->len] = 0;
4169 printk(KERN_TRACE "%s", s->buffer);
4171 trace_seq_init(s);
4174 static void __ftrace_dump(bool disable_tracing)
4176 static raw_spinlock_t ftrace_dump_lock =
4177 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
4178 /* use static because iter can be a bit big for the stack */
4179 static struct trace_iterator iter;
4180 unsigned int old_userobj;
4181 static int dump_ran;
4182 unsigned long flags;
4183 int cnt = 0, cpu;
4185 /* only one dump */
4186 local_irq_save(flags);
4187 __raw_spin_lock(&ftrace_dump_lock);
4188 if (dump_ran)
4189 goto out;
4191 dump_ran = 1;
4193 tracing_off();
4195 if (disable_tracing)
4196 ftrace_kill();
4198 for_each_tracing_cpu(cpu) {
4199 atomic_inc(&global_trace.data[cpu]->disabled);
4202 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4204 /* don't look at user memory in panic mode */
4205 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4207 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4209 /* Simulate the iterator */
4210 iter.tr = &global_trace;
4211 iter.trace = current_trace;
4212 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4215 * We need to stop all tracing on all CPUS to read the
4216 * the next buffer. This is a bit expensive, but is
4217 * not done often. We fill all what we can read,
4218 * and then release the locks again.
4221 while (!trace_empty(&iter)) {
4223 if (!cnt)
4224 printk(KERN_TRACE "---------------------------------\n");
4226 cnt++;
4228 /* reset all but tr, trace, and overruns */
4229 memset(&iter.seq, 0,
4230 sizeof(struct trace_iterator) -
4231 offsetof(struct trace_iterator, seq));
4232 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4233 iter.pos = -1;
4235 if (find_next_entry_inc(&iter) != NULL) {
4236 print_trace_line(&iter);
4237 trace_consume(&iter);
4240 trace_printk_seq(&iter.seq);
4243 if (!cnt)
4244 printk(KERN_TRACE " (ftrace buffer empty)\n");
4245 else
4246 printk(KERN_TRACE "---------------------------------\n");
4248 /* Re-enable tracing if requested */
4249 if (!disable_tracing) {
4250 trace_flags |= old_userobj;
4252 for_each_tracing_cpu(cpu) {
4253 atomic_dec(&global_trace.data[cpu]->disabled);
4255 tracing_on();
4258 out:
4259 __raw_spin_unlock(&ftrace_dump_lock);
4260 local_irq_restore(flags);
4263 /* By default: disable tracing after the dump */
4264 void ftrace_dump(void)
4266 __ftrace_dump(true);
4269 __init static int tracer_alloc_buffers(void)
4271 struct trace_array_cpu *data;
4272 int ring_buf_size;
4273 int i;
4274 int ret = -ENOMEM;
4276 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4277 goto out;
4279 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4280 goto out_free_buffer_mask;
4282 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4283 goto out_free_tracing_cpumask;
4285 /* To save memory, keep the ring buffer size to its minimum */
4286 if (ring_buffer_expanded)
4287 ring_buf_size = trace_buf_size;
4288 else
4289 ring_buf_size = 1;
4291 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4292 cpumask_copy(tracing_cpumask, cpu_all_mask);
4293 cpumask_clear(tracing_reader_cpumask);
4295 /* TODO: make the number of buffers hot pluggable with CPUS */
4296 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4297 TRACE_BUFFER_FLAGS);
4298 if (!global_trace.buffer) {
4299 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4300 WARN_ON(1);
4301 goto out_free_cpumask;
4303 global_trace.entries = ring_buffer_size(global_trace.buffer);
4306 #ifdef CONFIG_TRACER_MAX_TRACE
4307 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4308 TRACE_BUFFER_FLAGS);
4309 if (!max_tr.buffer) {
4310 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4311 WARN_ON(1);
4312 ring_buffer_free(global_trace.buffer);
4313 goto out_free_cpumask;
4315 max_tr.entries = ring_buffer_size(max_tr.buffer);
4316 WARN_ON(max_tr.entries != global_trace.entries);
4317 #endif
4319 /* Allocate the first page for all buffers */
4320 for_each_tracing_cpu(i) {
4321 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4322 max_tr.data[i] = &per_cpu(max_data, i);
4325 trace_init_cmdlines();
4327 register_tracer(&nop_trace);
4328 current_trace = &nop_trace;
4329 #ifdef CONFIG_BOOT_TRACER
4330 register_tracer(&boot_tracer);
4331 #endif
4332 /* All seems OK, enable tracing */
4333 tracing_disabled = 0;
4335 atomic_notifier_chain_register(&panic_notifier_list,
4336 &trace_panic_notifier);
4338 register_die_notifier(&trace_die_notifier);
4340 return 0;
4342 out_free_cpumask:
4343 free_cpumask_var(tracing_reader_cpumask);
4344 out_free_tracing_cpumask:
4345 free_cpumask_var(tracing_cpumask);
4346 out_free_buffer_mask:
4347 free_cpumask_var(tracing_buffer_mask);
4348 out:
4349 return ret;
4352 __init static int clear_boot_tracer(void)
4355 * The default tracer at boot buffer is an init section.
4356 * This function is called in lateinit. If we did not
4357 * find the boot tracer, then clear it out, to prevent
4358 * later registration from accessing the buffer that is
4359 * about to be freed.
4361 if (!default_bootup_tracer)
4362 return 0;
4364 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4365 default_bootup_tracer);
4366 default_bootup_tracer = NULL;
4368 return 0;
4371 early_initcall(tracer_alloc_buffers);
4372 fs_initcall(tracer_init_debugfs);
4373 late_initcall(clear_boot_tracer);