tracing: Fix trace_buf_size boot option
[linux-2.6/libata-dev.git] / kernel / trace / trace.c
blob3aa0a0dfdfa818ccada26954797f6e933b592222
1 /*
2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/ring_buffer.h>
15 #include <linux/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/ctype.h>
35 #include <linux/init.h>
36 #include <linux/poll.h>
37 #include <linux/gfp.h>
38 #include <linux/fs.h>
40 #include "trace.h"
41 #include "trace_output.h"
43 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
45 unsigned long __read_mostly tracing_max_latency;
46 unsigned long __read_mostly tracing_thresh;
49 * On boot up, the ring buffer is set to the minimum size, so that
50 * we do not waste memory on systems that are not using tracing.
52 static int ring_buffer_expanded;
55 * We need to change this state when a selftest is running.
56 * A selftest will lurk into the ring-buffer to count the
57 * entries inserted during the selftest although some concurrent
58 * insertions into the ring-buffer such as trace_printk could occurred
59 * at the same time, giving false positive or negative results.
61 static bool __read_mostly tracing_selftest_running;
64 * If a tracer is running, we do not want to run SELFTEST.
66 static bool __read_mostly tracing_selftest_disabled;
68 /* For tracers that don't implement custom flags */
69 static struct tracer_opt dummy_tracer_opt[] = {
70 { }
73 static struct tracer_flags dummy_tracer_flags = {
74 .val = 0,
75 .opts = dummy_tracer_opt
78 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
80 return 0;
84 * Kill all tracing for good (never come back).
85 * It is initialized to 1 but will turn to zero if the initialization
86 * of the tracer is successful. But that is the only place that sets
87 * this back to zero.
89 static int tracing_disabled = 1;
91 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
93 static inline void ftrace_disable_cpu(void)
95 preempt_disable();
96 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
99 static inline void ftrace_enable_cpu(void)
101 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
102 preempt_enable();
105 static cpumask_var_t __read_mostly tracing_buffer_mask;
107 /* Define which cpu buffers are currently read in trace_pipe */
108 static cpumask_var_t tracing_reader_cpumask;
110 #define for_each_tracing_cpu(cpu) \
111 for_each_cpu(cpu, tracing_buffer_mask)
114 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
116 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
117 * is set, then ftrace_dump is called. This will output the contents
118 * of the ftrace buffers to the console. This is very useful for
119 * capturing traces that lead to crashes and outputing it to a
120 * serial console.
122 * It is default off, but you can enable it with either specifying
123 * "ftrace_dump_on_oops" in the kernel command line, or setting
124 * /proc/sys/kernel/ftrace_dump_on_oops to true.
126 int ftrace_dump_on_oops;
128 static int tracing_set_tracer(const char *buf);
130 #define BOOTUP_TRACER_SIZE 100
131 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
132 static char *default_bootup_tracer;
134 static int __init set_ftrace(char *str)
136 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
137 default_bootup_tracer = bootup_tracer_buf;
138 /* We are using ftrace early, expand it */
139 ring_buffer_expanded = 1;
140 return 1;
142 __setup("ftrace=", set_ftrace);
144 static int __init set_ftrace_dump_on_oops(char *str)
146 ftrace_dump_on_oops = 1;
147 return 1;
149 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
151 unsigned long long ns2usecs(cycle_t nsec)
153 nsec += 500;
154 do_div(nsec, 1000);
155 return nsec;
159 * The global_trace is the descriptor that holds the tracing
160 * buffers for the live tracing. For each CPU, it contains
161 * a link list of pages that will store trace entries. The
162 * page descriptor of the pages in the memory is used to hold
163 * the link list by linking the lru item in the page descriptor
164 * to each of the pages in the buffer per CPU.
166 * For each active CPU there is a data field that holds the
167 * pages for the buffer for that CPU. Each CPU has the same number
168 * of pages allocated for its buffer.
170 static struct trace_array global_trace;
172 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
174 int filter_current_check_discard(struct ftrace_event_call *call, void *rec,
175 struct ring_buffer_event *event)
177 return filter_check_discard(call, rec, global_trace.buffer, event);
179 EXPORT_SYMBOL_GPL(filter_current_check_discard);
181 cycle_t ftrace_now(int cpu)
183 u64 ts;
185 /* Early boot up does not have a buffer yet */
186 if (!global_trace.buffer)
187 return trace_clock_local();
189 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
190 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
192 return ts;
196 * The max_tr is used to snapshot the global_trace when a maximum
197 * latency is reached. Some tracers will use this to store a maximum
198 * trace while it continues examining live traces.
200 * The buffers for the max_tr are set up the same as the global_trace.
201 * When a snapshot is taken, the link list of the max_tr is swapped
202 * with the link list of the global_trace and the buffers are reset for
203 * the global_trace so the tracing can continue.
205 static struct trace_array max_tr;
207 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
209 /* tracer_enabled is used to toggle activation of a tracer */
210 static int tracer_enabled = 1;
213 * tracing_is_enabled - return tracer_enabled status
215 * This function is used by other tracers to know the status
216 * of the tracer_enabled flag. Tracers may use this function
217 * to know if it should enable their features when starting
218 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
220 int tracing_is_enabled(void)
222 return tracer_enabled;
226 * trace_buf_size is the size in bytes that is allocated
227 * for a buffer. Note, the number of bytes is always rounded
228 * to page size.
230 * This number is purposely set to a low number of 16384.
231 * If the dump on oops happens, it will be much appreciated
232 * to not have to wait for all that output. Anyway this can be
233 * boot time and run time configurable.
235 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
237 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
239 /* trace_types holds a link list of available tracers. */
240 static struct tracer *trace_types __read_mostly;
242 /* current_trace points to the tracer that is currently active */
243 static struct tracer *current_trace __read_mostly;
246 * max_tracer_type_len is used to simplify the allocating of
247 * buffers to read userspace tracer names. We keep track of
248 * the longest tracer name registered.
250 static int max_tracer_type_len;
253 * trace_types_lock is used to protect the trace_types list.
254 * This lock is also used to keep user access serialized.
255 * Accesses from userspace will grab this lock while userspace
256 * activities happen inside the kernel.
258 static DEFINE_MUTEX(trace_types_lock);
260 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
261 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
263 /* trace_flags holds trace_options default values */
264 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
265 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
266 TRACE_ITER_GRAPH_TIME;
269 * trace_wake_up - wake up tasks waiting for trace input
271 * Simply wakes up any task that is blocked on the trace_wait
272 * queue. These is used with trace_poll for tasks polling the trace.
274 void trace_wake_up(void)
277 * The runqueue_is_locked() can fail, but this is the best we
278 * have for now:
280 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
281 wake_up(&trace_wait);
284 static int __init set_buf_size(char *str)
286 unsigned long buf_size;
288 if (!str)
289 return 0;
290 buf_size = memparse(str, &str);
291 /* nr_entries can not be zero */
292 if (buf_size == 0)
293 return 0;
294 trace_buf_size = buf_size;
295 return 1;
297 __setup("trace_buf_size=", set_buf_size);
299 unsigned long nsecs_to_usecs(unsigned long nsecs)
301 return nsecs / 1000;
304 /* These must match the bit postions in trace_iterator_flags */
305 static const char *trace_options[] = {
306 "print-parent",
307 "sym-offset",
308 "sym-addr",
309 "verbose",
310 "raw",
311 "hex",
312 "bin",
313 "block",
314 "stacktrace",
315 "sched-tree",
316 "trace_printk",
317 "ftrace_preempt",
318 "branch",
319 "annotate",
320 "userstacktrace",
321 "sym-userobj",
322 "printk-msg-only",
323 "context-info",
324 "latency-format",
325 "global-clock",
326 "sleep-time",
327 "graph-time",
328 NULL
332 * ftrace_max_lock is used to protect the swapping of buffers
333 * when taking a max snapshot. The buffers themselves are
334 * protected by per_cpu spinlocks. But the action of the swap
335 * needs its own lock.
337 * This is defined as a raw_spinlock_t in order to help
338 * with performance when lockdep debugging is enabled.
340 static raw_spinlock_t ftrace_max_lock =
341 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
344 * Copy the new maximum trace into the separate maximum-trace
345 * structure. (this way the maximum trace is permanently saved,
346 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
348 static void
349 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
351 struct trace_array_cpu *data = tr->data[cpu];
353 max_tr.cpu = cpu;
354 max_tr.time_start = data->preempt_timestamp;
356 data = max_tr.data[cpu];
357 data->saved_latency = tracing_max_latency;
359 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
360 data->pid = tsk->pid;
361 data->uid = task_uid(tsk);
362 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
363 data->policy = tsk->policy;
364 data->rt_priority = tsk->rt_priority;
366 /* record this tasks comm */
367 tracing_record_cmdline(tsk);
370 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
372 int len;
373 int ret;
375 if (!cnt)
376 return 0;
378 if (s->len <= s->readpos)
379 return -EBUSY;
381 len = s->len - s->readpos;
382 if (cnt > len)
383 cnt = len;
384 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
385 if (ret == cnt)
386 return -EFAULT;
388 cnt -= ret;
390 s->readpos += cnt;
391 return cnt;
394 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
396 int len;
397 void *ret;
399 if (s->len <= s->readpos)
400 return -EBUSY;
402 len = s->len - s->readpos;
403 if (cnt > len)
404 cnt = len;
405 ret = memcpy(buf, s->buffer + s->readpos, cnt);
406 if (!ret)
407 return -EFAULT;
409 s->readpos += cnt;
410 return cnt;
414 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
415 * @tr: tracer
416 * @tsk: the task with the latency
417 * @cpu: The cpu that initiated the trace.
419 * Flip the buffers between the @tr and the max_tr and record information
420 * about which task was the cause of this latency.
422 void
423 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
425 struct ring_buffer *buf = tr->buffer;
427 WARN_ON_ONCE(!irqs_disabled());
428 __raw_spin_lock(&ftrace_max_lock);
430 tr->buffer = max_tr.buffer;
431 max_tr.buffer = buf;
433 ftrace_disable_cpu();
434 ring_buffer_reset(tr->buffer);
435 ftrace_enable_cpu();
437 __update_max_tr(tr, tsk, cpu);
438 __raw_spin_unlock(&ftrace_max_lock);
442 * update_max_tr_single - only copy one trace over, and reset the rest
443 * @tr - tracer
444 * @tsk - task with the latency
445 * @cpu - the cpu of the buffer to copy.
447 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
449 void
450 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
452 int ret;
454 WARN_ON_ONCE(!irqs_disabled());
455 __raw_spin_lock(&ftrace_max_lock);
457 ftrace_disable_cpu();
459 ring_buffer_reset(max_tr.buffer);
460 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
462 ftrace_enable_cpu();
464 WARN_ON_ONCE(ret && ret != -EAGAIN);
466 __update_max_tr(tr, tsk, cpu);
467 __raw_spin_unlock(&ftrace_max_lock);
471 * register_tracer - register a tracer with the ftrace system.
472 * @type - the plugin for the tracer
474 * Register a new plugin tracer.
476 int register_tracer(struct tracer *type)
477 __releases(kernel_lock)
478 __acquires(kernel_lock)
480 struct tracer *t;
481 int len;
482 int ret = 0;
484 if (!type->name) {
485 pr_info("Tracer must have a name\n");
486 return -1;
490 * When this gets called we hold the BKL which means that
491 * preemption is disabled. Various trace selftests however
492 * need to disable and enable preemption for successful tests.
493 * So we drop the BKL here and grab it after the tests again.
495 unlock_kernel();
496 mutex_lock(&trace_types_lock);
498 tracing_selftest_running = true;
500 for (t = trace_types; t; t = t->next) {
501 if (strcmp(type->name, t->name) == 0) {
502 /* already found */
503 pr_info("Trace %s already registered\n",
504 type->name);
505 ret = -1;
506 goto out;
510 if (!type->set_flag)
511 type->set_flag = &dummy_set_flag;
512 if (!type->flags)
513 type->flags = &dummy_tracer_flags;
514 else
515 if (!type->flags->opts)
516 type->flags->opts = dummy_tracer_opt;
517 if (!type->wait_pipe)
518 type->wait_pipe = default_wait_pipe;
521 #ifdef CONFIG_FTRACE_STARTUP_TEST
522 if (type->selftest && !tracing_selftest_disabled) {
523 struct tracer *saved_tracer = current_trace;
524 struct trace_array *tr = &global_trace;
525 int i;
528 * Run a selftest on this tracer.
529 * Here we reset the trace buffer, and set the current
530 * tracer to be this tracer. The tracer can then run some
531 * internal tracing to verify that everything is in order.
532 * If we fail, we do not register this tracer.
534 for_each_tracing_cpu(i)
535 tracing_reset(tr, i);
537 current_trace = type;
538 /* the test is responsible for initializing and enabling */
539 pr_info("Testing tracer %s: ", type->name);
540 ret = type->selftest(type, tr);
541 /* the test is responsible for resetting too */
542 current_trace = saved_tracer;
543 if (ret) {
544 printk(KERN_CONT "FAILED!\n");
545 goto out;
547 /* Only reset on passing, to avoid touching corrupted buffers */
548 for_each_tracing_cpu(i)
549 tracing_reset(tr, i);
551 printk(KERN_CONT "PASSED\n");
553 #endif
555 type->next = trace_types;
556 trace_types = type;
557 len = strlen(type->name);
558 if (len > max_tracer_type_len)
559 max_tracer_type_len = len;
561 out:
562 tracing_selftest_running = false;
563 mutex_unlock(&trace_types_lock);
565 if (ret || !default_bootup_tracer)
566 goto out_unlock;
568 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
569 goto out_unlock;
571 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
572 /* Do we want this tracer to start on bootup? */
573 tracing_set_tracer(type->name);
574 default_bootup_tracer = NULL;
575 /* disable other selftests, since this will break it. */
576 tracing_selftest_disabled = 1;
577 #ifdef CONFIG_FTRACE_STARTUP_TEST
578 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
579 type->name);
580 #endif
582 out_unlock:
583 lock_kernel();
584 return ret;
587 void unregister_tracer(struct tracer *type)
589 struct tracer **t;
590 int len;
592 mutex_lock(&trace_types_lock);
593 for (t = &trace_types; *t; t = &(*t)->next) {
594 if (*t == type)
595 goto found;
597 pr_info("Trace %s not registered\n", type->name);
598 goto out;
600 found:
601 *t = (*t)->next;
603 if (type == current_trace && tracer_enabled) {
604 tracer_enabled = 0;
605 tracing_stop();
606 if (current_trace->stop)
607 current_trace->stop(&global_trace);
608 current_trace = &nop_trace;
611 if (strlen(type->name) != max_tracer_type_len)
612 goto out;
614 max_tracer_type_len = 0;
615 for (t = &trace_types; *t; t = &(*t)->next) {
616 len = strlen((*t)->name);
617 if (len > max_tracer_type_len)
618 max_tracer_type_len = len;
620 out:
621 mutex_unlock(&trace_types_lock);
624 void tracing_reset(struct trace_array *tr, int cpu)
626 ftrace_disable_cpu();
627 ring_buffer_reset_cpu(tr->buffer, cpu);
628 ftrace_enable_cpu();
631 void tracing_reset_online_cpus(struct trace_array *tr)
633 int cpu;
635 tr->time_start = ftrace_now(tr->cpu);
637 for_each_online_cpu(cpu)
638 tracing_reset(tr, cpu);
641 void tracing_reset_current(int cpu)
643 tracing_reset(&global_trace, cpu);
646 void tracing_reset_current_online_cpus(void)
648 tracing_reset_online_cpus(&global_trace);
651 #define SAVED_CMDLINES 128
652 #define NO_CMDLINE_MAP UINT_MAX
653 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
654 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
655 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
656 static int cmdline_idx;
657 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
659 /* temporary disable recording */
660 static atomic_t trace_record_cmdline_disabled __read_mostly;
662 static void trace_init_cmdlines(void)
664 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
665 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
666 cmdline_idx = 0;
669 static int trace_stop_count;
670 static DEFINE_SPINLOCK(tracing_start_lock);
673 * ftrace_off_permanent - disable all ftrace code permanently
675 * This should only be called when a serious anomally has
676 * been detected. This will turn off the function tracing,
677 * ring buffers, and other tracing utilites. It takes no
678 * locks and can be called from any context.
680 void ftrace_off_permanent(void)
682 tracing_disabled = 1;
683 ftrace_stop();
684 tracing_off_permanent();
688 * tracing_start - quick start of the tracer
690 * If tracing is enabled but was stopped by tracing_stop,
691 * this will start the tracer back up.
693 void tracing_start(void)
695 struct ring_buffer *buffer;
696 unsigned long flags;
698 if (tracing_disabled)
699 return;
701 spin_lock_irqsave(&tracing_start_lock, flags);
702 if (--trace_stop_count) {
703 if (trace_stop_count < 0) {
704 /* Someone screwed up their debugging */
705 WARN_ON_ONCE(1);
706 trace_stop_count = 0;
708 goto out;
712 buffer = global_trace.buffer;
713 if (buffer)
714 ring_buffer_record_enable(buffer);
716 buffer = max_tr.buffer;
717 if (buffer)
718 ring_buffer_record_enable(buffer);
720 ftrace_start();
721 out:
722 spin_unlock_irqrestore(&tracing_start_lock, flags);
726 * tracing_stop - quick stop of the tracer
728 * Light weight way to stop tracing. Use in conjunction with
729 * tracing_start.
731 void tracing_stop(void)
733 struct ring_buffer *buffer;
734 unsigned long flags;
736 ftrace_stop();
737 spin_lock_irqsave(&tracing_start_lock, flags);
738 if (trace_stop_count++)
739 goto out;
741 buffer = global_trace.buffer;
742 if (buffer)
743 ring_buffer_record_disable(buffer);
745 buffer = max_tr.buffer;
746 if (buffer)
747 ring_buffer_record_disable(buffer);
749 out:
750 spin_unlock_irqrestore(&tracing_start_lock, flags);
753 void trace_stop_cmdline_recording(void);
755 static void trace_save_cmdline(struct task_struct *tsk)
757 unsigned pid, idx;
759 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
760 return;
763 * It's not the end of the world if we don't get
764 * the lock, but we also don't want to spin
765 * nor do we want to disable interrupts,
766 * so if we miss here, then better luck next time.
768 if (!__raw_spin_trylock(&trace_cmdline_lock))
769 return;
771 idx = map_pid_to_cmdline[tsk->pid];
772 if (idx == NO_CMDLINE_MAP) {
773 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
776 * Check whether the cmdline buffer at idx has a pid
777 * mapped. We are going to overwrite that entry so we
778 * need to clear the map_pid_to_cmdline. Otherwise we
779 * would read the new comm for the old pid.
781 pid = map_cmdline_to_pid[idx];
782 if (pid != NO_CMDLINE_MAP)
783 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
785 map_cmdline_to_pid[idx] = tsk->pid;
786 map_pid_to_cmdline[tsk->pid] = idx;
788 cmdline_idx = idx;
791 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
793 __raw_spin_unlock(&trace_cmdline_lock);
796 void trace_find_cmdline(int pid, char comm[])
798 unsigned map;
800 if (!pid) {
801 strcpy(comm, "<idle>");
802 return;
805 if (pid > PID_MAX_DEFAULT) {
806 strcpy(comm, "<...>");
807 return;
810 preempt_disable();
811 __raw_spin_lock(&trace_cmdline_lock);
812 map = map_pid_to_cmdline[pid];
813 if (map != NO_CMDLINE_MAP)
814 strcpy(comm, saved_cmdlines[map]);
815 else
816 strcpy(comm, "<...>");
818 __raw_spin_unlock(&trace_cmdline_lock);
819 preempt_enable();
822 void tracing_record_cmdline(struct task_struct *tsk)
824 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
825 !tracing_is_on())
826 return;
828 trace_save_cmdline(tsk);
831 void
832 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
833 int pc)
835 struct task_struct *tsk = current;
837 entry->preempt_count = pc & 0xff;
838 entry->pid = (tsk) ? tsk->pid : 0;
839 entry->tgid = (tsk) ? tsk->tgid : 0;
840 entry->flags =
841 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
842 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
843 #else
844 TRACE_FLAG_IRQS_NOSUPPORT |
845 #endif
846 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
847 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
848 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
851 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
852 int type,
853 unsigned long len,
854 unsigned long flags, int pc)
856 struct ring_buffer_event *event;
858 event = ring_buffer_lock_reserve(tr->buffer, len);
859 if (event != NULL) {
860 struct trace_entry *ent = ring_buffer_event_data(event);
862 tracing_generic_entry_update(ent, flags, pc);
863 ent->type = type;
866 return event;
868 static void ftrace_trace_stack(struct trace_array *tr,
869 unsigned long flags, int skip, int pc);
870 static void ftrace_trace_userstack(struct trace_array *tr,
871 unsigned long flags, int pc);
873 static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
874 struct ring_buffer_event *event,
875 unsigned long flags, int pc,
876 int wake)
878 ring_buffer_unlock_commit(tr->buffer, event);
880 ftrace_trace_stack(tr, flags, 6, pc);
881 ftrace_trace_userstack(tr, flags, pc);
883 if (wake)
884 trace_wake_up();
887 void trace_buffer_unlock_commit(struct trace_array *tr,
888 struct ring_buffer_event *event,
889 unsigned long flags, int pc)
891 __trace_buffer_unlock_commit(tr, event, flags, pc, 1);
894 struct ring_buffer_event *
895 trace_current_buffer_lock_reserve(int type, unsigned long len,
896 unsigned long flags, int pc)
898 return trace_buffer_lock_reserve(&global_trace,
899 type, len, flags, pc);
901 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
903 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
904 unsigned long flags, int pc)
906 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
908 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
910 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
911 unsigned long flags, int pc)
913 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
915 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
917 void trace_current_buffer_discard_commit(struct ring_buffer_event *event)
919 ring_buffer_discard_commit(global_trace.buffer, event);
921 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
923 void
924 trace_function(struct trace_array *tr,
925 unsigned long ip, unsigned long parent_ip, unsigned long flags,
926 int pc)
928 struct ftrace_event_call *call = &event_function;
929 struct ring_buffer_event *event;
930 struct ftrace_entry *entry;
932 /* If we are reading the ring buffer, don't trace */
933 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
934 return;
936 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
937 flags, pc);
938 if (!event)
939 return;
940 entry = ring_buffer_event_data(event);
941 entry->ip = ip;
942 entry->parent_ip = parent_ip;
944 if (!filter_check_discard(call, entry, tr->buffer, event))
945 ring_buffer_unlock_commit(tr->buffer, event);
948 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
949 static int __trace_graph_entry(struct trace_array *tr,
950 struct ftrace_graph_ent *trace,
951 unsigned long flags,
952 int pc)
954 struct ftrace_event_call *call = &event_funcgraph_entry;
955 struct ring_buffer_event *event;
956 struct ftrace_graph_ent_entry *entry;
958 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
959 return 0;
961 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
962 sizeof(*entry), flags, pc);
963 if (!event)
964 return 0;
965 entry = ring_buffer_event_data(event);
966 entry->graph_ent = *trace;
967 if (!filter_current_check_discard(call, entry, event))
968 ring_buffer_unlock_commit(global_trace.buffer, event);
970 return 1;
973 static void __trace_graph_return(struct trace_array *tr,
974 struct ftrace_graph_ret *trace,
975 unsigned long flags,
976 int pc)
978 struct ftrace_event_call *call = &event_funcgraph_exit;
979 struct ring_buffer_event *event;
980 struct ftrace_graph_ret_entry *entry;
982 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
983 return;
985 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
986 sizeof(*entry), flags, pc);
987 if (!event)
988 return;
989 entry = ring_buffer_event_data(event);
990 entry->ret = *trace;
991 if (!filter_current_check_discard(call, entry, event))
992 ring_buffer_unlock_commit(global_trace.buffer, event);
994 #endif
996 void
997 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
998 unsigned long ip, unsigned long parent_ip, unsigned long flags,
999 int pc)
1001 if (likely(!atomic_read(&data->disabled)))
1002 trace_function(tr, ip, parent_ip, flags, pc);
1005 static void __ftrace_trace_stack(struct trace_array *tr,
1006 unsigned long flags,
1007 int skip, int pc)
1009 #ifdef CONFIG_STACKTRACE
1010 struct ftrace_event_call *call = &event_kernel_stack;
1011 struct ring_buffer_event *event;
1012 struct stack_entry *entry;
1013 struct stack_trace trace;
1015 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
1016 sizeof(*entry), flags, pc);
1017 if (!event)
1018 return;
1019 entry = ring_buffer_event_data(event);
1020 memset(&entry->caller, 0, sizeof(entry->caller));
1022 trace.nr_entries = 0;
1023 trace.max_entries = FTRACE_STACK_ENTRIES;
1024 trace.skip = skip;
1025 trace.entries = entry->caller;
1027 save_stack_trace(&trace);
1028 if (!filter_check_discard(call, entry, tr->buffer, event))
1029 ring_buffer_unlock_commit(tr->buffer, event);
1030 #endif
1033 static void ftrace_trace_stack(struct trace_array *tr,
1034 unsigned long flags,
1035 int skip, int pc)
1037 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1038 return;
1040 __ftrace_trace_stack(tr, flags, skip, pc);
1043 void __trace_stack(struct trace_array *tr,
1044 unsigned long flags,
1045 int skip, int pc)
1047 __ftrace_trace_stack(tr, flags, skip, pc);
1050 static void ftrace_trace_userstack(struct trace_array *tr,
1051 unsigned long flags, int pc)
1053 #ifdef CONFIG_STACKTRACE
1054 struct ftrace_event_call *call = &event_user_stack;
1055 struct ring_buffer_event *event;
1056 struct userstack_entry *entry;
1057 struct stack_trace trace;
1059 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1060 return;
1062 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1063 sizeof(*entry), flags, pc);
1064 if (!event)
1065 return;
1066 entry = ring_buffer_event_data(event);
1068 memset(&entry->caller, 0, sizeof(entry->caller));
1070 trace.nr_entries = 0;
1071 trace.max_entries = FTRACE_STACK_ENTRIES;
1072 trace.skip = 0;
1073 trace.entries = entry->caller;
1075 save_stack_trace_user(&trace);
1076 if (!filter_check_discard(call, entry, tr->buffer, event))
1077 ring_buffer_unlock_commit(tr->buffer, event);
1078 #endif
1081 #ifdef UNUSED
1082 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1084 ftrace_trace_userstack(tr, flags, preempt_count());
1086 #endif /* UNUSED */
1088 static void
1089 ftrace_trace_special(void *__tr,
1090 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1091 int pc)
1093 struct ring_buffer_event *event;
1094 struct trace_array *tr = __tr;
1095 struct special_entry *entry;
1097 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1098 sizeof(*entry), 0, pc);
1099 if (!event)
1100 return;
1101 entry = ring_buffer_event_data(event);
1102 entry->arg1 = arg1;
1103 entry->arg2 = arg2;
1104 entry->arg3 = arg3;
1105 trace_buffer_unlock_commit(tr, event, 0, pc);
1108 void
1109 __trace_special(void *__tr, void *__data,
1110 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1112 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1115 void
1116 tracing_sched_switch_trace(struct trace_array *tr,
1117 struct task_struct *prev,
1118 struct task_struct *next,
1119 unsigned long flags, int pc)
1121 struct ftrace_event_call *call = &event_context_switch;
1122 struct ring_buffer_event *event;
1123 struct ctx_switch_entry *entry;
1125 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1126 sizeof(*entry), flags, pc);
1127 if (!event)
1128 return;
1129 entry = ring_buffer_event_data(event);
1130 entry->prev_pid = prev->pid;
1131 entry->prev_prio = prev->prio;
1132 entry->prev_state = prev->state;
1133 entry->next_pid = next->pid;
1134 entry->next_prio = next->prio;
1135 entry->next_state = next->state;
1136 entry->next_cpu = task_cpu(next);
1138 if (!filter_check_discard(call, entry, tr->buffer, event))
1139 trace_buffer_unlock_commit(tr, event, flags, pc);
1142 void
1143 tracing_sched_wakeup_trace(struct trace_array *tr,
1144 struct task_struct *wakee,
1145 struct task_struct *curr,
1146 unsigned long flags, int pc)
1148 struct ftrace_event_call *call = &event_wakeup;
1149 struct ring_buffer_event *event;
1150 struct ctx_switch_entry *entry;
1152 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1153 sizeof(*entry), flags, pc);
1154 if (!event)
1155 return;
1156 entry = ring_buffer_event_data(event);
1157 entry->prev_pid = curr->pid;
1158 entry->prev_prio = curr->prio;
1159 entry->prev_state = curr->state;
1160 entry->next_pid = wakee->pid;
1161 entry->next_prio = wakee->prio;
1162 entry->next_state = wakee->state;
1163 entry->next_cpu = task_cpu(wakee);
1165 if (!filter_check_discard(call, entry, tr->buffer, event))
1166 ring_buffer_unlock_commit(tr->buffer, event);
1167 ftrace_trace_stack(tr, flags, 6, pc);
1168 ftrace_trace_userstack(tr, flags, pc);
1171 void
1172 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1174 struct trace_array *tr = &global_trace;
1175 struct trace_array_cpu *data;
1176 unsigned long flags;
1177 int cpu;
1178 int pc;
1180 if (tracing_disabled)
1181 return;
1183 pc = preempt_count();
1184 local_irq_save(flags);
1185 cpu = raw_smp_processor_id();
1186 data = tr->data[cpu];
1188 if (likely(atomic_inc_return(&data->disabled) == 1))
1189 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1191 atomic_dec(&data->disabled);
1192 local_irq_restore(flags);
1195 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1196 int trace_graph_entry(struct ftrace_graph_ent *trace)
1198 struct trace_array *tr = &global_trace;
1199 struct trace_array_cpu *data;
1200 unsigned long flags;
1201 long disabled;
1202 int ret;
1203 int cpu;
1204 int pc;
1206 if (!ftrace_trace_task(current))
1207 return 0;
1209 if (!ftrace_graph_addr(trace->func))
1210 return 0;
1212 local_irq_save(flags);
1213 cpu = raw_smp_processor_id();
1214 data = tr->data[cpu];
1215 disabled = atomic_inc_return(&data->disabled);
1216 if (likely(disabled == 1)) {
1217 pc = preempt_count();
1218 ret = __trace_graph_entry(tr, trace, flags, pc);
1219 } else {
1220 ret = 0;
1222 /* Only do the atomic if it is not already set */
1223 if (!test_tsk_trace_graph(current))
1224 set_tsk_trace_graph(current);
1226 atomic_dec(&data->disabled);
1227 local_irq_restore(flags);
1229 return ret;
1232 void trace_graph_return(struct ftrace_graph_ret *trace)
1234 struct trace_array *tr = &global_trace;
1235 struct trace_array_cpu *data;
1236 unsigned long flags;
1237 long disabled;
1238 int cpu;
1239 int pc;
1241 local_irq_save(flags);
1242 cpu = raw_smp_processor_id();
1243 data = tr->data[cpu];
1244 disabled = atomic_inc_return(&data->disabled);
1245 if (likely(disabled == 1)) {
1246 pc = preempt_count();
1247 __trace_graph_return(tr, trace, flags, pc);
1249 if (!trace->depth)
1250 clear_tsk_trace_graph(current);
1251 atomic_dec(&data->disabled);
1252 local_irq_restore(flags);
1254 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1258 * trace_vbprintk - write binary msg to tracing buffer
1261 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1263 static raw_spinlock_t trace_buf_lock =
1264 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1265 static u32 trace_buf[TRACE_BUF_SIZE];
1267 struct ftrace_event_call *call = &event_bprint;
1268 struct ring_buffer_event *event;
1269 struct trace_array *tr = &global_trace;
1270 struct trace_array_cpu *data;
1271 struct bprint_entry *entry;
1272 unsigned long flags;
1273 int disable;
1274 int resched;
1275 int cpu, len = 0, size, pc;
1277 if (unlikely(tracing_selftest_running || tracing_disabled))
1278 return 0;
1280 /* Don't pollute graph traces with trace_vprintk internals */
1281 pause_graph_tracing();
1283 pc = preempt_count();
1284 resched = ftrace_preempt_disable();
1285 cpu = raw_smp_processor_id();
1286 data = tr->data[cpu];
1288 disable = atomic_inc_return(&data->disabled);
1289 if (unlikely(disable != 1))
1290 goto out;
1292 /* Lockdep uses trace_printk for lock tracing */
1293 local_irq_save(flags);
1294 __raw_spin_lock(&trace_buf_lock);
1295 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1297 if (len > TRACE_BUF_SIZE || len < 0)
1298 goto out_unlock;
1300 size = sizeof(*entry) + sizeof(u32) * len;
1301 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1302 if (!event)
1303 goto out_unlock;
1304 entry = ring_buffer_event_data(event);
1305 entry->ip = ip;
1306 entry->fmt = fmt;
1308 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1309 if (!filter_check_discard(call, entry, tr->buffer, event))
1310 ring_buffer_unlock_commit(tr->buffer, event);
1312 out_unlock:
1313 __raw_spin_unlock(&trace_buf_lock);
1314 local_irq_restore(flags);
1316 out:
1317 atomic_dec_return(&data->disabled);
1318 ftrace_preempt_enable(resched);
1319 unpause_graph_tracing();
1321 return len;
1323 EXPORT_SYMBOL_GPL(trace_vbprintk);
1325 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1327 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1328 static char trace_buf[TRACE_BUF_SIZE];
1330 struct ftrace_event_call *call = &event_print;
1331 struct ring_buffer_event *event;
1332 struct trace_array *tr = &global_trace;
1333 struct trace_array_cpu *data;
1334 int cpu, len = 0, size, pc;
1335 struct print_entry *entry;
1336 unsigned long irq_flags;
1337 int disable;
1339 if (tracing_disabled || tracing_selftest_running)
1340 return 0;
1342 pc = preempt_count();
1343 preempt_disable_notrace();
1344 cpu = raw_smp_processor_id();
1345 data = tr->data[cpu];
1347 disable = atomic_inc_return(&data->disabled);
1348 if (unlikely(disable != 1))
1349 goto out;
1351 pause_graph_tracing();
1352 raw_local_irq_save(irq_flags);
1353 __raw_spin_lock(&trace_buf_lock);
1354 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1356 len = min(len, TRACE_BUF_SIZE-1);
1357 trace_buf[len] = 0;
1359 size = sizeof(*entry) + len + 1;
1360 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1361 if (!event)
1362 goto out_unlock;
1363 entry = ring_buffer_event_data(event);
1364 entry->ip = ip;
1366 memcpy(&entry->buf, trace_buf, len);
1367 entry->buf[len] = 0;
1368 if (!filter_check_discard(call, entry, tr->buffer, event))
1369 ring_buffer_unlock_commit(tr->buffer, event);
1371 out_unlock:
1372 __raw_spin_unlock(&trace_buf_lock);
1373 raw_local_irq_restore(irq_flags);
1374 unpause_graph_tracing();
1375 out:
1376 atomic_dec_return(&data->disabled);
1377 preempt_enable_notrace();
1379 return len;
1381 EXPORT_SYMBOL_GPL(trace_vprintk);
1383 enum trace_file_type {
1384 TRACE_FILE_LAT_FMT = 1,
1385 TRACE_FILE_ANNOTATE = 2,
1388 static void trace_iterator_increment(struct trace_iterator *iter)
1390 /* Don't allow ftrace to trace into the ring buffers */
1391 ftrace_disable_cpu();
1393 iter->idx++;
1394 if (iter->buffer_iter[iter->cpu])
1395 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1397 ftrace_enable_cpu();
1400 static struct trace_entry *
1401 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1403 struct ring_buffer_event *event;
1404 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1406 /* Don't allow ftrace to trace into the ring buffers */
1407 ftrace_disable_cpu();
1409 if (buf_iter)
1410 event = ring_buffer_iter_peek(buf_iter, ts);
1411 else
1412 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1414 ftrace_enable_cpu();
1416 return event ? ring_buffer_event_data(event) : NULL;
1419 static struct trace_entry *
1420 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1422 struct ring_buffer *buffer = iter->tr->buffer;
1423 struct trace_entry *ent, *next = NULL;
1424 int cpu_file = iter->cpu_file;
1425 u64 next_ts = 0, ts;
1426 int next_cpu = -1;
1427 int cpu;
1430 * If we are in a per_cpu trace file, don't bother by iterating over
1431 * all cpu and peek directly.
1433 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1434 if (ring_buffer_empty_cpu(buffer, cpu_file))
1435 return NULL;
1436 ent = peek_next_entry(iter, cpu_file, ent_ts);
1437 if (ent_cpu)
1438 *ent_cpu = cpu_file;
1440 return ent;
1443 for_each_tracing_cpu(cpu) {
1445 if (ring_buffer_empty_cpu(buffer, cpu))
1446 continue;
1448 ent = peek_next_entry(iter, cpu, &ts);
1451 * Pick the entry with the smallest timestamp:
1453 if (ent && (!next || ts < next_ts)) {
1454 next = ent;
1455 next_cpu = cpu;
1456 next_ts = ts;
1460 if (ent_cpu)
1461 *ent_cpu = next_cpu;
1463 if (ent_ts)
1464 *ent_ts = next_ts;
1466 return next;
1469 /* Find the next real entry, without updating the iterator itself */
1470 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1471 int *ent_cpu, u64 *ent_ts)
1473 return __find_next_entry(iter, ent_cpu, ent_ts);
1476 /* Find the next real entry, and increment the iterator to the next entry */
1477 static void *find_next_entry_inc(struct trace_iterator *iter)
1479 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1481 if (iter->ent)
1482 trace_iterator_increment(iter);
1484 return iter->ent ? iter : NULL;
1487 static void trace_consume(struct trace_iterator *iter)
1489 /* Don't allow ftrace to trace into the ring buffers */
1490 ftrace_disable_cpu();
1491 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1492 ftrace_enable_cpu();
1495 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1497 struct trace_iterator *iter = m->private;
1498 int i = (int)*pos;
1499 void *ent;
1501 (*pos)++;
1503 /* can't go backwards */
1504 if (iter->idx > i)
1505 return NULL;
1507 if (iter->idx < 0)
1508 ent = find_next_entry_inc(iter);
1509 else
1510 ent = iter;
1512 while (ent && iter->idx < i)
1513 ent = find_next_entry_inc(iter);
1515 iter->pos = *pos;
1517 return ent;
1521 * No necessary locking here. The worst thing which can
1522 * happen is loosing events consumed at the same time
1523 * by a trace_pipe reader.
1524 * Other than that, we don't risk to crash the ring buffer
1525 * because it serializes the readers.
1527 * The current tracer is copied to avoid a global locking
1528 * all around.
1530 static void *s_start(struct seq_file *m, loff_t *pos)
1532 struct trace_iterator *iter = m->private;
1533 static struct tracer *old_tracer;
1534 int cpu_file = iter->cpu_file;
1535 void *p = NULL;
1536 loff_t l = 0;
1537 int cpu;
1539 /* copy the tracer to avoid using a global lock all around */
1540 mutex_lock(&trace_types_lock);
1541 if (unlikely(old_tracer != current_trace && current_trace)) {
1542 old_tracer = current_trace;
1543 *iter->trace = *current_trace;
1545 mutex_unlock(&trace_types_lock);
1547 atomic_inc(&trace_record_cmdline_disabled);
1549 if (*pos != iter->pos) {
1550 iter->ent = NULL;
1551 iter->cpu = 0;
1552 iter->idx = -1;
1554 ftrace_disable_cpu();
1556 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1557 for_each_tracing_cpu(cpu)
1558 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1559 } else
1560 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1563 ftrace_enable_cpu();
1565 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1568 } else {
1569 l = *pos - 1;
1570 p = s_next(m, p, &l);
1573 trace_event_read_lock();
1574 return p;
1577 static void s_stop(struct seq_file *m, void *p)
1579 atomic_dec(&trace_record_cmdline_disabled);
1580 trace_event_read_unlock();
1583 static void print_lat_help_header(struct seq_file *m)
1585 seq_puts(m, "# _------=> CPU# \n");
1586 seq_puts(m, "# / _-----=> irqs-off \n");
1587 seq_puts(m, "# | / _----=> need-resched \n");
1588 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1589 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1590 seq_puts(m, "# |||| / \n");
1591 seq_puts(m, "# ||||| delay \n");
1592 seq_puts(m, "# cmd pid ||||| time | caller \n");
1593 seq_puts(m, "# \\ / ||||| \\ | / \n");
1596 static void print_func_help_header(struct seq_file *m)
1598 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1599 seq_puts(m, "# | | | | |\n");
1603 static void
1604 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1606 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1607 struct trace_array *tr = iter->tr;
1608 struct trace_array_cpu *data = tr->data[tr->cpu];
1609 struct tracer *type = current_trace;
1610 unsigned long total;
1611 unsigned long entries;
1612 const char *name = "preemption";
1614 if (type)
1615 name = type->name;
1617 entries = ring_buffer_entries(iter->tr->buffer);
1618 total = entries +
1619 ring_buffer_overruns(iter->tr->buffer);
1621 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1622 name, UTS_RELEASE);
1623 seq_puts(m, "# -----------------------------------"
1624 "---------------------------------\n");
1625 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1626 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1627 nsecs_to_usecs(data->saved_latency),
1628 entries,
1629 total,
1630 tr->cpu,
1631 #if defined(CONFIG_PREEMPT_NONE)
1632 "server",
1633 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1634 "desktop",
1635 #elif defined(CONFIG_PREEMPT)
1636 "preempt",
1637 #else
1638 "unknown",
1639 #endif
1640 /* These are reserved for later use */
1641 0, 0, 0, 0);
1642 #ifdef CONFIG_SMP
1643 seq_printf(m, " #P:%d)\n", num_online_cpus());
1644 #else
1645 seq_puts(m, ")\n");
1646 #endif
1647 seq_puts(m, "# -----------------\n");
1648 seq_printf(m, "# | task: %.16s-%d "
1649 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1650 data->comm, data->pid, data->uid, data->nice,
1651 data->policy, data->rt_priority);
1652 seq_puts(m, "# -----------------\n");
1654 if (data->critical_start) {
1655 seq_puts(m, "# => started at: ");
1656 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1657 trace_print_seq(m, &iter->seq);
1658 seq_puts(m, "\n# => ended at: ");
1659 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1660 trace_print_seq(m, &iter->seq);
1661 seq_puts(m, "#\n");
1664 seq_puts(m, "#\n");
1667 static void test_cpu_buff_start(struct trace_iterator *iter)
1669 struct trace_seq *s = &iter->seq;
1671 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1672 return;
1674 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1675 return;
1677 if (cpumask_test_cpu(iter->cpu, iter->started))
1678 return;
1680 cpumask_set_cpu(iter->cpu, iter->started);
1682 /* Don't print started cpu buffer for the first entry of the trace */
1683 if (iter->idx > 1)
1684 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1685 iter->cpu);
1688 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1690 struct trace_seq *s = &iter->seq;
1691 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1692 struct trace_entry *entry;
1693 struct trace_event *event;
1695 entry = iter->ent;
1697 test_cpu_buff_start(iter);
1699 event = ftrace_find_event(entry->type);
1701 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1702 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1703 if (!trace_print_lat_context(iter))
1704 goto partial;
1705 } else {
1706 if (!trace_print_context(iter))
1707 goto partial;
1711 if (event)
1712 return event->trace(iter, sym_flags);
1714 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1715 goto partial;
1717 return TRACE_TYPE_HANDLED;
1718 partial:
1719 return TRACE_TYPE_PARTIAL_LINE;
1722 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1724 struct trace_seq *s = &iter->seq;
1725 struct trace_entry *entry;
1726 struct trace_event *event;
1728 entry = iter->ent;
1730 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1731 if (!trace_seq_printf(s, "%d %d %llu ",
1732 entry->pid, iter->cpu, iter->ts))
1733 goto partial;
1736 event = ftrace_find_event(entry->type);
1737 if (event)
1738 return event->raw(iter, 0);
1740 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1741 goto partial;
1743 return TRACE_TYPE_HANDLED;
1744 partial:
1745 return TRACE_TYPE_PARTIAL_LINE;
1748 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1750 struct trace_seq *s = &iter->seq;
1751 unsigned char newline = '\n';
1752 struct trace_entry *entry;
1753 struct trace_event *event;
1755 entry = iter->ent;
1757 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1758 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1759 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1760 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1763 event = ftrace_find_event(entry->type);
1764 if (event) {
1765 enum print_line_t ret = event->hex(iter, 0);
1766 if (ret != TRACE_TYPE_HANDLED)
1767 return ret;
1770 SEQ_PUT_FIELD_RET(s, newline);
1772 return TRACE_TYPE_HANDLED;
1775 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1777 struct trace_seq *s = &iter->seq;
1778 struct trace_entry *entry;
1779 struct trace_event *event;
1781 entry = iter->ent;
1783 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1784 SEQ_PUT_FIELD_RET(s, entry->pid);
1785 SEQ_PUT_FIELD_RET(s, iter->cpu);
1786 SEQ_PUT_FIELD_RET(s, iter->ts);
1789 event = ftrace_find_event(entry->type);
1790 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1793 static int trace_empty(struct trace_iterator *iter)
1795 int cpu;
1797 /* If we are looking at one CPU buffer, only check that one */
1798 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1799 cpu = iter->cpu_file;
1800 if (iter->buffer_iter[cpu]) {
1801 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1802 return 0;
1803 } else {
1804 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1805 return 0;
1807 return 1;
1810 for_each_tracing_cpu(cpu) {
1811 if (iter->buffer_iter[cpu]) {
1812 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1813 return 0;
1814 } else {
1815 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1816 return 0;
1820 return 1;
1823 /* Called with trace_event_read_lock() held. */
1824 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1826 enum print_line_t ret;
1828 if (iter->trace && iter->trace->print_line) {
1829 ret = iter->trace->print_line(iter);
1830 if (ret != TRACE_TYPE_UNHANDLED)
1831 return ret;
1834 if (iter->ent->type == TRACE_BPRINT &&
1835 trace_flags & TRACE_ITER_PRINTK &&
1836 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1837 return trace_print_bprintk_msg_only(iter);
1839 if (iter->ent->type == TRACE_PRINT &&
1840 trace_flags & TRACE_ITER_PRINTK &&
1841 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1842 return trace_print_printk_msg_only(iter);
1844 if (trace_flags & TRACE_ITER_BIN)
1845 return print_bin_fmt(iter);
1847 if (trace_flags & TRACE_ITER_HEX)
1848 return print_hex_fmt(iter);
1850 if (trace_flags & TRACE_ITER_RAW)
1851 return print_raw_fmt(iter);
1853 return print_trace_fmt(iter);
1856 static int s_show(struct seq_file *m, void *v)
1858 struct trace_iterator *iter = v;
1860 if (iter->ent == NULL) {
1861 if (iter->tr) {
1862 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1863 seq_puts(m, "#\n");
1865 if (iter->trace && iter->trace->print_header)
1866 iter->trace->print_header(m);
1867 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1868 /* print nothing if the buffers are empty */
1869 if (trace_empty(iter))
1870 return 0;
1871 print_trace_header(m, iter);
1872 if (!(trace_flags & TRACE_ITER_VERBOSE))
1873 print_lat_help_header(m);
1874 } else {
1875 if (!(trace_flags & TRACE_ITER_VERBOSE))
1876 print_func_help_header(m);
1878 } else {
1879 print_trace_line(iter);
1880 trace_print_seq(m, &iter->seq);
1883 return 0;
1886 static struct seq_operations tracer_seq_ops = {
1887 .start = s_start,
1888 .next = s_next,
1889 .stop = s_stop,
1890 .show = s_show,
1893 static struct trace_iterator *
1894 __tracing_open(struct inode *inode, struct file *file)
1896 long cpu_file = (long) inode->i_private;
1897 void *fail_ret = ERR_PTR(-ENOMEM);
1898 struct trace_iterator *iter;
1899 struct seq_file *m;
1900 int cpu, ret;
1902 if (tracing_disabled)
1903 return ERR_PTR(-ENODEV);
1905 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1906 if (!iter)
1907 return ERR_PTR(-ENOMEM);
1910 * We make a copy of the current tracer to avoid concurrent
1911 * changes on it while we are reading.
1913 mutex_lock(&trace_types_lock);
1914 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1915 if (!iter->trace)
1916 goto fail;
1918 if (current_trace)
1919 *iter->trace = *current_trace;
1921 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1922 goto fail;
1924 cpumask_clear(iter->started);
1926 if (current_trace && current_trace->print_max)
1927 iter->tr = &max_tr;
1928 else
1929 iter->tr = &global_trace;
1930 iter->pos = -1;
1931 mutex_init(&iter->mutex);
1932 iter->cpu_file = cpu_file;
1934 /* Notify the tracer early; before we stop tracing. */
1935 if (iter->trace && iter->trace->open)
1936 iter->trace->open(iter);
1938 /* Annotate start of buffers if we had overruns */
1939 if (ring_buffer_overruns(iter->tr->buffer))
1940 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1942 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1943 for_each_tracing_cpu(cpu) {
1945 iter->buffer_iter[cpu] =
1946 ring_buffer_read_start(iter->tr->buffer, cpu);
1948 } else {
1949 cpu = iter->cpu_file;
1950 iter->buffer_iter[cpu] =
1951 ring_buffer_read_start(iter->tr->buffer, cpu);
1954 /* TODO stop tracer */
1955 ret = seq_open(file, &tracer_seq_ops);
1956 if (ret < 0) {
1957 fail_ret = ERR_PTR(ret);
1958 goto fail_buffer;
1961 m = file->private_data;
1962 m->private = iter;
1964 /* stop the trace while dumping */
1965 tracing_stop();
1967 mutex_unlock(&trace_types_lock);
1969 return iter;
1971 fail_buffer:
1972 for_each_tracing_cpu(cpu) {
1973 if (iter->buffer_iter[cpu])
1974 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1976 free_cpumask_var(iter->started);
1977 fail:
1978 mutex_unlock(&trace_types_lock);
1979 kfree(iter->trace);
1980 kfree(iter);
1982 return fail_ret;
1985 int tracing_open_generic(struct inode *inode, struct file *filp)
1987 if (tracing_disabled)
1988 return -ENODEV;
1990 filp->private_data = inode->i_private;
1991 return 0;
1994 static int tracing_release(struct inode *inode, struct file *file)
1996 struct seq_file *m = (struct seq_file *)file->private_data;
1997 struct trace_iterator *iter;
1998 int cpu;
2000 if (!(file->f_mode & FMODE_READ))
2001 return 0;
2003 iter = m->private;
2005 mutex_lock(&trace_types_lock);
2006 for_each_tracing_cpu(cpu) {
2007 if (iter->buffer_iter[cpu])
2008 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2011 if (iter->trace && iter->trace->close)
2012 iter->trace->close(iter);
2014 /* reenable tracing if it was previously enabled */
2015 tracing_start();
2016 mutex_unlock(&trace_types_lock);
2018 seq_release(inode, file);
2019 mutex_destroy(&iter->mutex);
2020 free_cpumask_var(iter->started);
2021 kfree(iter->trace);
2022 kfree(iter);
2023 return 0;
2026 static int tracing_open(struct inode *inode, struct file *file)
2028 struct trace_iterator *iter;
2029 int ret = 0;
2031 /* If this file was open for write, then erase contents */
2032 if ((file->f_mode & FMODE_WRITE) &&
2033 !(file->f_flags & O_APPEND)) {
2034 long cpu = (long) inode->i_private;
2036 if (cpu == TRACE_PIPE_ALL_CPU)
2037 tracing_reset_online_cpus(&global_trace);
2038 else
2039 tracing_reset(&global_trace, cpu);
2042 if (file->f_mode & FMODE_READ) {
2043 iter = __tracing_open(inode, file);
2044 if (IS_ERR(iter))
2045 ret = PTR_ERR(iter);
2046 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2047 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2049 return ret;
2052 static void *
2053 t_next(struct seq_file *m, void *v, loff_t *pos)
2055 struct tracer *t = v;
2057 (*pos)++;
2059 if (t)
2060 t = t->next;
2062 return t;
2065 static void *t_start(struct seq_file *m, loff_t *pos)
2067 struct tracer *t;
2068 loff_t l = 0;
2070 mutex_lock(&trace_types_lock);
2071 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2074 return t;
2077 static void t_stop(struct seq_file *m, void *p)
2079 mutex_unlock(&trace_types_lock);
2082 static int t_show(struct seq_file *m, void *v)
2084 struct tracer *t = v;
2086 if (!t)
2087 return 0;
2089 seq_printf(m, "%s", t->name);
2090 if (t->next)
2091 seq_putc(m, ' ');
2092 else
2093 seq_putc(m, '\n');
2095 return 0;
2098 static struct seq_operations show_traces_seq_ops = {
2099 .start = t_start,
2100 .next = t_next,
2101 .stop = t_stop,
2102 .show = t_show,
2105 static int show_traces_open(struct inode *inode, struct file *file)
2107 if (tracing_disabled)
2108 return -ENODEV;
2110 return seq_open(file, &show_traces_seq_ops);
2113 static ssize_t
2114 tracing_write_stub(struct file *filp, const char __user *ubuf,
2115 size_t count, loff_t *ppos)
2117 return count;
2120 static const struct file_operations tracing_fops = {
2121 .open = tracing_open,
2122 .read = seq_read,
2123 .write = tracing_write_stub,
2124 .llseek = seq_lseek,
2125 .release = tracing_release,
2128 static const struct file_operations show_traces_fops = {
2129 .open = show_traces_open,
2130 .read = seq_read,
2131 .release = seq_release,
2135 * Only trace on a CPU if the bitmask is set:
2137 static cpumask_var_t tracing_cpumask;
2140 * The tracer itself will not take this lock, but still we want
2141 * to provide a consistent cpumask to user-space:
2143 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2146 * Temporary storage for the character representation of the
2147 * CPU bitmask (and one more byte for the newline):
2149 static char mask_str[NR_CPUS + 1];
2151 static ssize_t
2152 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2153 size_t count, loff_t *ppos)
2155 int len;
2157 mutex_lock(&tracing_cpumask_update_lock);
2159 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2160 if (count - len < 2) {
2161 count = -EINVAL;
2162 goto out_err;
2164 len += sprintf(mask_str + len, "\n");
2165 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2167 out_err:
2168 mutex_unlock(&tracing_cpumask_update_lock);
2170 return count;
2173 static ssize_t
2174 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2175 size_t count, loff_t *ppos)
2177 int err, cpu;
2178 cpumask_var_t tracing_cpumask_new;
2180 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2181 return -ENOMEM;
2183 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2184 if (err)
2185 goto err_unlock;
2187 mutex_lock(&tracing_cpumask_update_lock);
2189 local_irq_disable();
2190 __raw_spin_lock(&ftrace_max_lock);
2191 for_each_tracing_cpu(cpu) {
2193 * Increase/decrease the disabled counter if we are
2194 * about to flip a bit in the cpumask:
2196 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2197 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2198 atomic_inc(&global_trace.data[cpu]->disabled);
2200 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2201 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2202 atomic_dec(&global_trace.data[cpu]->disabled);
2205 __raw_spin_unlock(&ftrace_max_lock);
2206 local_irq_enable();
2208 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2210 mutex_unlock(&tracing_cpumask_update_lock);
2211 free_cpumask_var(tracing_cpumask_new);
2213 return count;
2215 err_unlock:
2216 free_cpumask_var(tracing_cpumask_new);
2218 return err;
2221 static const struct file_operations tracing_cpumask_fops = {
2222 .open = tracing_open_generic,
2223 .read = tracing_cpumask_read,
2224 .write = tracing_cpumask_write,
2227 static ssize_t
2228 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2229 size_t cnt, loff_t *ppos)
2231 struct tracer_opt *trace_opts;
2232 u32 tracer_flags;
2233 int len = 0;
2234 char *buf;
2235 int r = 0;
2236 int i;
2239 /* calculate max size */
2240 for (i = 0; trace_options[i]; i++) {
2241 len += strlen(trace_options[i]);
2242 len += 3; /* "no" and newline */
2245 mutex_lock(&trace_types_lock);
2246 tracer_flags = current_trace->flags->val;
2247 trace_opts = current_trace->flags->opts;
2250 * Increase the size with names of options specific
2251 * of the current tracer.
2253 for (i = 0; trace_opts[i].name; i++) {
2254 len += strlen(trace_opts[i].name);
2255 len += 3; /* "no" and newline */
2258 /* +2 for \n and \0 */
2259 buf = kmalloc(len + 2, GFP_KERNEL);
2260 if (!buf) {
2261 mutex_unlock(&trace_types_lock);
2262 return -ENOMEM;
2265 for (i = 0; trace_options[i]; i++) {
2266 if (trace_flags & (1 << i))
2267 r += sprintf(buf + r, "%s\n", trace_options[i]);
2268 else
2269 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2272 for (i = 0; trace_opts[i].name; i++) {
2273 if (tracer_flags & trace_opts[i].bit)
2274 r += sprintf(buf + r, "%s\n",
2275 trace_opts[i].name);
2276 else
2277 r += sprintf(buf + r, "no%s\n",
2278 trace_opts[i].name);
2280 mutex_unlock(&trace_types_lock);
2282 WARN_ON(r >= len + 2);
2284 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2286 kfree(buf);
2287 return r;
2290 /* Try to assign a tracer specific option */
2291 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2293 struct tracer_flags *trace_flags = trace->flags;
2294 struct tracer_opt *opts = NULL;
2295 int ret = 0, i = 0;
2296 int len;
2298 for (i = 0; trace_flags->opts[i].name; i++) {
2299 opts = &trace_flags->opts[i];
2300 len = strlen(opts->name);
2302 if (strncmp(cmp, opts->name, len) == 0) {
2303 ret = trace->set_flag(trace_flags->val,
2304 opts->bit, !neg);
2305 break;
2308 /* Not found */
2309 if (!trace_flags->opts[i].name)
2310 return -EINVAL;
2312 /* Refused to handle */
2313 if (ret)
2314 return ret;
2316 if (neg)
2317 trace_flags->val &= ~opts->bit;
2318 else
2319 trace_flags->val |= opts->bit;
2321 return 0;
2324 static void set_tracer_flags(unsigned int mask, int enabled)
2326 /* do nothing if flag is already set */
2327 if (!!(trace_flags & mask) == !!enabled)
2328 return;
2330 if (enabled)
2331 trace_flags |= mask;
2332 else
2333 trace_flags &= ~mask;
2335 if (mask == TRACE_ITER_GLOBAL_CLK) {
2336 u64 (*func)(void);
2338 if (enabled)
2339 func = trace_clock_global;
2340 else
2341 func = trace_clock_local;
2343 mutex_lock(&trace_types_lock);
2344 ring_buffer_set_clock(global_trace.buffer, func);
2346 if (max_tr.buffer)
2347 ring_buffer_set_clock(max_tr.buffer, func);
2348 mutex_unlock(&trace_types_lock);
2352 static ssize_t
2353 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2354 size_t cnt, loff_t *ppos)
2356 char buf[64];
2357 char *cmp = buf;
2358 int neg = 0;
2359 int ret;
2360 int i;
2362 if (cnt >= sizeof(buf))
2363 return -EINVAL;
2365 if (copy_from_user(&buf, ubuf, cnt))
2366 return -EFAULT;
2368 buf[cnt] = 0;
2370 if (strncmp(buf, "no", 2) == 0) {
2371 neg = 1;
2372 cmp += 2;
2375 for (i = 0; trace_options[i]; i++) {
2376 int len = strlen(trace_options[i]);
2378 if (strncmp(cmp, trace_options[i], len) == 0) {
2379 set_tracer_flags(1 << i, !neg);
2380 break;
2384 /* If no option could be set, test the specific tracer options */
2385 if (!trace_options[i]) {
2386 mutex_lock(&trace_types_lock);
2387 ret = set_tracer_option(current_trace, cmp, neg);
2388 mutex_unlock(&trace_types_lock);
2389 if (ret)
2390 return ret;
2393 filp->f_pos += cnt;
2395 return cnt;
2398 static const struct file_operations tracing_iter_fops = {
2399 .open = tracing_open_generic,
2400 .read = tracing_trace_options_read,
2401 .write = tracing_trace_options_write,
2404 static const char readme_msg[] =
2405 "tracing mini-HOWTO:\n\n"
2406 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2407 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2408 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2409 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2410 "nop\n"
2411 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2412 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2413 "sched_switch\n"
2414 "# cat /sys/kernel/debug/tracing/trace_options\n"
2415 "noprint-parent nosym-offset nosym-addr noverbose\n"
2416 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2417 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2418 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2419 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2422 static ssize_t
2423 tracing_readme_read(struct file *filp, char __user *ubuf,
2424 size_t cnt, loff_t *ppos)
2426 return simple_read_from_buffer(ubuf, cnt, ppos,
2427 readme_msg, strlen(readme_msg));
2430 static const struct file_operations tracing_readme_fops = {
2431 .open = tracing_open_generic,
2432 .read = tracing_readme_read,
2435 static ssize_t
2436 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2437 size_t cnt, loff_t *ppos)
2439 char *buf_comm;
2440 char *file_buf;
2441 char *buf;
2442 int len = 0;
2443 int pid;
2444 int i;
2446 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2447 if (!file_buf)
2448 return -ENOMEM;
2450 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2451 if (!buf_comm) {
2452 kfree(file_buf);
2453 return -ENOMEM;
2456 buf = file_buf;
2458 for (i = 0; i < SAVED_CMDLINES; i++) {
2459 int r;
2461 pid = map_cmdline_to_pid[i];
2462 if (pid == -1 || pid == NO_CMDLINE_MAP)
2463 continue;
2465 trace_find_cmdline(pid, buf_comm);
2466 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2467 buf += r;
2468 len += r;
2471 len = simple_read_from_buffer(ubuf, cnt, ppos,
2472 file_buf, len);
2474 kfree(file_buf);
2475 kfree(buf_comm);
2477 return len;
2480 static const struct file_operations tracing_saved_cmdlines_fops = {
2481 .open = tracing_open_generic,
2482 .read = tracing_saved_cmdlines_read,
2485 static ssize_t
2486 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2487 size_t cnt, loff_t *ppos)
2489 char buf[64];
2490 int r;
2492 r = sprintf(buf, "%u\n", tracer_enabled);
2493 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2496 static ssize_t
2497 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2498 size_t cnt, loff_t *ppos)
2500 struct trace_array *tr = filp->private_data;
2501 char buf[64];
2502 unsigned long val;
2503 int ret;
2505 if (cnt >= sizeof(buf))
2506 return -EINVAL;
2508 if (copy_from_user(&buf, ubuf, cnt))
2509 return -EFAULT;
2511 buf[cnt] = 0;
2513 ret = strict_strtoul(buf, 10, &val);
2514 if (ret < 0)
2515 return ret;
2517 val = !!val;
2519 mutex_lock(&trace_types_lock);
2520 if (tracer_enabled ^ val) {
2521 if (val) {
2522 tracer_enabled = 1;
2523 if (current_trace->start)
2524 current_trace->start(tr);
2525 tracing_start();
2526 } else {
2527 tracer_enabled = 0;
2528 tracing_stop();
2529 if (current_trace->stop)
2530 current_trace->stop(tr);
2533 mutex_unlock(&trace_types_lock);
2535 filp->f_pos += cnt;
2537 return cnt;
2540 static ssize_t
2541 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2542 size_t cnt, loff_t *ppos)
2544 char buf[max_tracer_type_len+2];
2545 int r;
2547 mutex_lock(&trace_types_lock);
2548 if (current_trace)
2549 r = sprintf(buf, "%s\n", current_trace->name);
2550 else
2551 r = sprintf(buf, "\n");
2552 mutex_unlock(&trace_types_lock);
2554 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2557 int tracer_init(struct tracer *t, struct trace_array *tr)
2559 tracing_reset_online_cpus(tr);
2560 return t->init(tr);
2563 static int tracing_resize_ring_buffer(unsigned long size)
2565 int ret;
2568 * If kernel or user changes the size of the ring buffer
2569 * we use the size that was given, and we can forget about
2570 * expanding it later.
2572 ring_buffer_expanded = 1;
2574 ret = ring_buffer_resize(global_trace.buffer, size);
2575 if (ret < 0)
2576 return ret;
2578 ret = ring_buffer_resize(max_tr.buffer, size);
2579 if (ret < 0) {
2580 int r;
2582 r = ring_buffer_resize(global_trace.buffer,
2583 global_trace.entries);
2584 if (r < 0) {
2586 * AARGH! We are left with different
2587 * size max buffer!!!!
2588 * The max buffer is our "snapshot" buffer.
2589 * When a tracer needs a snapshot (one of the
2590 * latency tracers), it swaps the max buffer
2591 * with the saved snap shot. We succeeded to
2592 * update the size of the main buffer, but failed to
2593 * update the size of the max buffer. But when we tried
2594 * to reset the main buffer to the original size, we
2595 * failed there too. This is very unlikely to
2596 * happen, but if it does, warn and kill all
2597 * tracing.
2599 WARN_ON(1);
2600 tracing_disabled = 1;
2602 return ret;
2605 global_trace.entries = size;
2607 return ret;
2611 * tracing_update_buffers - used by tracing facility to expand ring buffers
2613 * To save on memory when the tracing is never used on a system with it
2614 * configured in. The ring buffers are set to a minimum size. But once
2615 * a user starts to use the tracing facility, then they need to grow
2616 * to their default size.
2618 * This function is to be called when a tracer is about to be used.
2620 int tracing_update_buffers(void)
2622 int ret = 0;
2624 mutex_lock(&trace_types_lock);
2625 if (!ring_buffer_expanded)
2626 ret = tracing_resize_ring_buffer(trace_buf_size);
2627 mutex_unlock(&trace_types_lock);
2629 return ret;
2632 struct trace_option_dentry;
2634 static struct trace_option_dentry *
2635 create_trace_option_files(struct tracer *tracer);
2637 static void
2638 destroy_trace_option_files(struct trace_option_dentry *topts);
2640 static int tracing_set_tracer(const char *buf)
2642 static struct trace_option_dentry *topts;
2643 struct trace_array *tr = &global_trace;
2644 struct tracer *t;
2645 int ret = 0;
2647 mutex_lock(&trace_types_lock);
2649 if (!ring_buffer_expanded) {
2650 ret = tracing_resize_ring_buffer(trace_buf_size);
2651 if (ret < 0)
2652 goto out;
2653 ret = 0;
2656 for (t = trace_types; t; t = t->next) {
2657 if (strcmp(t->name, buf) == 0)
2658 break;
2660 if (!t) {
2661 ret = -EINVAL;
2662 goto out;
2664 if (t == current_trace)
2665 goto out;
2667 trace_branch_disable();
2668 if (current_trace && current_trace->reset)
2669 current_trace->reset(tr);
2671 destroy_trace_option_files(topts);
2673 current_trace = t;
2675 topts = create_trace_option_files(current_trace);
2677 if (t->init) {
2678 ret = tracer_init(t, tr);
2679 if (ret)
2680 goto out;
2683 trace_branch_enable(tr);
2684 out:
2685 mutex_unlock(&trace_types_lock);
2687 return ret;
2690 static ssize_t
2691 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2692 size_t cnt, loff_t *ppos)
2694 char buf[max_tracer_type_len+1];
2695 int i;
2696 size_t ret;
2697 int err;
2699 ret = cnt;
2701 if (cnt > max_tracer_type_len)
2702 cnt = max_tracer_type_len;
2704 if (copy_from_user(&buf, ubuf, cnt))
2705 return -EFAULT;
2707 buf[cnt] = 0;
2709 /* strip ending whitespace. */
2710 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2711 buf[i] = 0;
2713 err = tracing_set_tracer(buf);
2714 if (err)
2715 return err;
2717 filp->f_pos += ret;
2719 return ret;
2722 static ssize_t
2723 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2724 size_t cnt, loff_t *ppos)
2726 unsigned long *ptr = filp->private_data;
2727 char buf[64];
2728 int r;
2730 r = snprintf(buf, sizeof(buf), "%ld\n",
2731 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2732 if (r > sizeof(buf))
2733 r = sizeof(buf);
2734 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2737 static ssize_t
2738 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2739 size_t cnt, loff_t *ppos)
2741 unsigned long *ptr = filp->private_data;
2742 char buf[64];
2743 unsigned long val;
2744 int ret;
2746 if (cnt >= sizeof(buf))
2747 return -EINVAL;
2749 if (copy_from_user(&buf, ubuf, cnt))
2750 return -EFAULT;
2752 buf[cnt] = 0;
2754 ret = strict_strtoul(buf, 10, &val);
2755 if (ret < 0)
2756 return ret;
2758 *ptr = val * 1000;
2760 return cnt;
2763 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2765 long cpu_file = (long) inode->i_private;
2766 struct trace_iterator *iter;
2767 int ret = 0;
2769 if (tracing_disabled)
2770 return -ENODEV;
2772 mutex_lock(&trace_types_lock);
2774 /* We only allow one reader per cpu */
2775 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2776 if (!cpumask_empty(tracing_reader_cpumask)) {
2777 ret = -EBUSY;
2778 goto out;
2780 cpumask_setall(tracing_reader_cpumask);
2781 } else {
2782 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2783 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2784 else {
2785 ret = -EBUSY;
2786 goto out;
2790 /* create a buffer to store the information to pass to userspace */
2791 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2792 if (!iter) {
2793 ret = -ENOMEM;
2794 goto out;
2798 * We make a copy of the current tracer to avoid concurrent
2799 * changes on it while we are reading.
2801 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2802 if (!iter->trace) {
2803 ret = -ENOMEM;
2804 goto fail;
2806 if (current_trace)
2807 *iter->trace = *current_trace;
2809 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2810 ret = -ENOMEM;
2811 goto fail;
2814 /* trace pipe does not show start of buffer */
2815 cpumask_setall(iter->started);
2817 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2818 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2820 iter->cpu_file = cpu_file;
2821 iter->tr = &global_trace;
2822 mutex_init(&iter->mutex);
2823 filp->private_data = iter;
2825 if (iter->trace->pipe_open)
2826 iter->trace->pipe_open(iter);
2828 out:
2829 mutex_unlock(&trace_types_lock);
2830 return ret;
2832 fail:
2833 kfree(iter->trace);
2834 kfree(iter);
2835 mutex_unlock(&trace_types_lock);
2836 return ret;
2839 static int tracing_release_pipe(struct inode *inode, struct file *file)
2841 struct trace_iterator *iter = file->private_data;
2843 mutex_lock(&trace_types_lock);
2845 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2846 cpumask_clear(tracing_reader_cpumask);
2847 else
2848 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2850 mutex_unlock(&trace_types_lock);
2852 free_cpumask_var(iter->started);
2853 mutex_destroy(&iter->mutex);
2854 kfree(iter->trace);
2855 kfree(iter);
2857 return 0;
2860 static unsigned int
2861 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2863 struct trace_iterator *iter = filp->private_data;
2865 if (trace_flags & TRACE_ITER_BLOCK) {
2867 * Always select as readable when in blocking mode
2869 return POLLIN | POLLRDNORM;
2870 } else {
2871 if (!trace_empty(iter))
2872 return POLLIN | POLLRDNORM;
2873 poll_wait(filp, &trace_wait, poll_table);
2874 if (!trace_empty(iter))
2875 return POLLIN | POLLRDNORM;
2877 return 0;
2882 void default_wait_pipe(struct trace_iterator *iter)
2884 DEFINE_WAIT(wait);
2886 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2888 if (trace_empty(iter))
2889 schedule();
2891 finish_wait(&trace_wait, &wait);
2895 * This is a make-shift waitqueue.
2896 * A tracer might use this callback on some rare cases:
2898 * 1) the current tracer might hold the runqueue lock when it wakes up
2899 * a reader, hence a deadlock (sched, function, and function graph tracers)
2900 * 2) the function tracers, trace all functions, we don't want
2901 * the overhead of calling wake_up and friends
2902 * (and tracing them too)
2904 * Anyway, this is really very primitive wakeup.
2906 void poll_wait_pipe(struct trace_iterator *iter)
2908 set_current_state(TASK_INTERRUPTIBLE);
2909 /* sleep for 100 msecs, and try again. */
2910 schedule_timeout(HZ / 10);
2913 /* Must be called with trace_types_lock mutex held. */
2914 static int tracing_wait_pipe(struct file *filp)
2916 struct trace_iterator *iter = filp->private_data;
2918 while (trace_empty(iter)) {
2920 if ((filp->f_flags & O_NONBLOCK)) {
2921 return -EAGAIN;
2924 mutex_unlock(&iter->mutex);
2926 iter->trace->wait_pipe(iter);
2928 mutex_lock(&iter->mutex);
2930 if (signal_pending(current))
2931 return -EINTR;
2934 * We block until we read something and tracing is disabled.
2935 * We still block if tracing is disabled, but we have never
2936 * read anything. This allows a user to cat this file, and
2937 * then enable tracing. But after we have read something,
2938 * we give an EOF when tracing is again disabled.
2940 * iter->pos will be 0 if we haven't read anything.
2942 if (!tracer_enabled && iter->pos)
2943 break;
2946 return 1;
2950 * Consumer reader.
2952 static ssize_t
2953 tracing_read_pipe(struct file *filp, char __user *ubuf,
2954 size_t cnt, loff_t *ppos)
2956 struct trace_iterator *iter = filp->private_data;
2957 static struct tracer *old_tracer;
2958 ssize_t sret;
2960 /* return any leftover data */
2961 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2962 if (sret != -EBUSY)
2963 return sret;
2965 trace_seq_init(&iter->seq);
2967 /* copy the tracer to avoid using a global lock all around */
2968 mutex_lock(&trace_types_lock);
2969 if (unlikely(old_tracer != current_trace && current_trace)) {
2970 old_tracer = current_trace;
2971 *iter->trace = *current_trace;
2973 mutex_unlock(&trace_types_lock);
2976 * Avoid more than one consumer on a single file descriptor
2977 * This is just a matter of traces coherency, the ring buffer itself
2978 * is protected.
2980 mutex_lock(&iter->mutex);
2981 if (iter->trace->read) {
2982 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2983 if (sret)
2984 goto out;
2987 waitagain:
2988 sret = tracing_wait_pipe(filp);
2989 if (sret <= 0)
2990 goto out;
2992 /* stop when tracing is finished */
2993 if (trace_empty(iter)) {
2994 sret = 0;
2995 goto out;
2998 if (cnt >= PAGE_SIZE)
2999 cnt = PAGE_SIZE - 1;
3001 /* reset all but tr, trace, and overruns */
3002 memset(&iter->seq, 0,
3003 sizeof(struct trace_iterator) -
3004 offsetof(struct trace_iterator, seq));
3005 iter->pos = -1;
3007 trace_event_read_lock();
3008 while (find_next_entry_inc(iter) != NULL) {
3009 enum print_line_t ret;
3010 int len = iter->seq.len;
3012 ret = print_trace_line(iter);
3013 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3014 /* don't print partial lines */
3015 iter->seq.len = len;
3016 break;
3018 if (ret != TRACE_TYPE_NO_CONSUME)
3019 trace_consume(iter);
3021 if (iter->seq.len >= cnt)
3022 break;
3024 trace_event_read_unlock();
3026 /* Now copy what we have to the user */
3027 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3028 if (iter->seq.readpos >= iter->seq.len)
3029 trace_seq_init(&iter->seq);
3032 * If there was nothing to send to user, inspite of consuming trace
3033 * entries, go back to wait for more entries.
3035 if (sret == -EBUSY)
3036 goto waitagain;
3038 out:
3039 mutex_unlock(&iter->mutex);
3041 return sret;
3044 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3045 struct pipe_buffer *buf)
3047 __free_page(buf->page);
3050 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3051 unsigned int idx)
3053 __free_page(spd->pages[idx]);
3056 static struct pipe_buf_operations tracing_pipe_buf_ops = {
3057 .can_merge = 0,
3058 .map = generic_pipe_buf_map,
3059 .unmap = generic_pipe_buf_unmap,
3060 .confirm = generic_pipe_buf_confirm,
3061 .release = tracing_pipe_buf_release,
3062 .steal = generic_pipe_buf_steal,
3063 .get = generic_pipe_buf_get,
3066 static size_t
3067 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3069 size_t count;
3070 int ret;
3072 /* Seq buffer is page-sized, exactly what we need. */
3073 for (;;) {
3074 count = iter->seq.len;
3075 ret = print_trace_line(iter);
3076 count = iter->seq.len - count;
3077 if (rem < count) {
3078 rem = 0;
3079 iter->seq.len -= count;
3080 break;
3082 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3083 iter->seq.len -= count;
3084 break;
3087 trace_consume(iter);
3088 rem -= count;
3089 if (!find_next_entry_inc(iter)) {
3090 rem = 0;
3091 iter->ent = NULL;
3092 break;
3096 return rem;
3099 static ssize_t tracing_splice_read_pipe(struct file *filp,
3100 loff_t *ppos,
3101 struct pipe_inode_info *pipe,
3102 size_t len,
3103 unsigned int flags)
3105 struct page *pages[PIPE_BUFFERS];
3106 struct partial_page partial[PIPE_BUFFERS];
3107 struct trace_iterator *iter = filp->private_data;
3108 struct splice_pipe_desc spd = {
3109 .pages = pages,
3110 .partial = partial,
3111 .nr_pages = 0, /* This gets updated below. */
3112 .flags = flags,
3113 .ops = &tracing_pipe_buf_ops,
3114 .spd_release = tracing_spd_release_pipe,
3116 static struct tracer *old_tracer;
3117 ssize_t ret;
3118 size_t rem;
3119 unsigned int i;
3121 /* copy the tracer to avoid using a global lock all around */
3122 mutex_lock(&trace_types_lock);
3123 if (unlikely(old_tracer != current_trace && current_trace)) {
3124 old_tracer = current_trace;
3125 *iter->trace = *current_trace;
3127 mutex_unlock(&trace_types_lock);
3129 mutex_lock(&iter->mutex);
3131 if (iter->trace->splice_read) {
3132 ret = iter->trace->splice_read(iter, filp,
3133 ppos, pipe, len, flags);
3134 if (ret)
3135 goto out_err;
3138 ret = tracing_wait_pipe(filp);
3139 if (ret <= 0)
3140 goto out_err;
3142 if (!iter->ent && !find_next_entry_inc(iter)) {
3143 ret = -EFAULT;
3144 goto out_err;
3147 trace_event_read_lock();
3149 /* Fill as many pages as possible. */
3150 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3151 pages[i] = alloc_page(GFP_KERNEL);
3152 if (!pages[i])
3153 break;
3155 rem = tracing_fill_pipe_page(rem, iter);
3157 /* Copy the data into the page, so we can start over. */
3158 ret = trace_seq_to_buffer(&iter->seq,
3159 page_address(pages[i]),
3160 iter->seq.len);
3161 if (ret < 0) {
3162 __free_page(pages[i]);
3163 break;
3165 partial[i].offset = 0;
3166 partial[i].len = iter->seq.len;
3168 trace_seq_init(&iter->seq);
3171 trace_event_read_unlock();
3172 mutex_unlock(&iter->mutex);
3174 spd.nr_pages = i;
3176 return splice_to_pipe(pipe, &spd);
3178 out_err:
3179 mutex_unlock(&iter->mutex);
3181 return ret;
3184 static ssize_t
3185 tracing_entries_read(struct file *filp, char __user *ubuf,
3186 size_t cnt, loff_t *ppos)
3188 struct trace_array *tr = filp->private_data;
3189 char buf[96];
3190 int r;
3192 mutex_lock(&trace_types_lock);
3193 if (!ring_buffer_expanded)
3194 r = sprintf(buf, "%lu (expanded: %lu)\n",
3195 tr->entries >> 10,
3196 trace_buf_size >> 10);
3197 else
3198 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3199 mutex_unlock(&trace_types_lock);
3201 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3204 static ssize_t
3205 tracing_entries_write(struct file *filp, const char __user *ubuf,
3206 size_t cnt, loff_t *ppos)
3208 unsigned long val;
3209 char buf[64];
3210 int ret, cpu;
3212 if (cnt >= sizeof(buf))
3213 return -EINVAL;
3215 if (copy_from_user(&buf, ubuf, cnt))
3216 return -EFAULT;
3218 buf[cnt] = 0;
3220 ret = strict_strtoul(buf, 10, &val);
3221 if (ret < 0)
3222 return ret;
3224 /* must have at least 1 entry */
3225 if (!val)
3226 return -EINVAL;
3228 mutex_lock(&trace_types_lock);
3230 tracing_stop();
3232 /* disable all cpu buffers */
3233 for_each_tracing_cpu(cpu) {
3234 if (global_trace.data[cpu])
3235 atomic_inc(&global_trace.data[cpu]->disabled);
3236 if (max_tr.data[cpu])
3237 atomic_inc(&max_tr.data[cpu]->disabled);
3240 /* value is in KB */
3241 val <<= 10;
3243 if (val != global_trace.entries) {
3244 ret = tracing_resize_ring_buffer(val);
3245 if (ret < 0) {
3246 cnt = ret;
3247 goto out;
3251 filp->f_pos += cnt;
3253 /* If check pages failed, return ENOMEM */
3254 if (tracing_disabled)
3255 cnt = -ENOMEM;
3256 out:
3257 for_each_tracing_cpu(cpu) {
3258 if (global_trace.data[cpu])
3259 atomic_dec(&global_trace.data[cpu]->disabled);
3260 if (max_tr.data[cpu])
3261 atomic_dec(&max_tr.data[cpu]->disabled);
3264 tracing_start();
3265 max_tr.entries = global_trace.entries;
3266 mutex_unlock(&trace_types_lock);
3268 return cnt;
3271 static int mark_printk(const char *fmt, ...)
3273 int ret;
3274 va_list args;
3275 va_start(args, fmt);
3276 ret = trace_vprintk(0, fmt, args);
3277 va_end(args);
3278 return ret;
3281 static ssize_t
3282 tracing_mark_write(struct file *filp, const char __user *ubuf,
3283 size_t cnt, loff_t *fpos)
3285 char *buf;
3286 char *end;
3288 if (tracing_disabled)
3289 return -EINVAL;
3291 if (cnt > TRACE_BUF_SIZE)
3292 cnt = TRACE_BUF_SIZE;
3294 buf = kmalloc(cnt + 1, GFP_KERNEL);
3295 if (buf == NULL)
3296 return -ENOMEM;
3298 if (copy_from_user(buf, ubuf, cnt)) {
3299 kfree(buf);
3300 return -EFAULT;
3303 /* Cut from the first nil or newline. */
3304 buf[cnt] = '\0';
3305 end = strchr(buf, '\n');
3306 if (end)
3307 *end = '\0';
3309 cnt = mark_printk("%s\n", buf);
3310 kfree(buf);
3311 *fpos += cnt;
3313 return cnt;
3316 static const struct file_operations tracing_max_lat_fops = {
3317 .open = tracing_open_generic,
3318 .read = tracing_max_lat_read,
3319 .write = tracing_max_lat_write,
3322 static const struct file_operations tracing_ctrl_fops = {
3323 .open = tracing_open_generic,
3324 .read = tracing_ctrl_read,
3325 .write = tracing_ctrl_write,
3328 static const struct file_operations set_tracer_fops = {
3329 .open = tracing_open_generic,
3330 .read = tracing_set_trace_read,
3331 .write = tracing_set_trace_write,
3334 static const struct file_operations tracing_pipe_fops = {
3335 .open = tracing_open_pipe,
3336 .poll = tracing_poll_pipe,
3337 .read = tracing_read_pipe,
3338 .splice_read = tracing_splice_read_pipe,
3339 .release = tracing_release_pipe,
3342 static const struct file_operations tracing_entries_fops = {
3343 .open = tracing_open_generic,
3344 .read = tracing_entries_read,
3345 .write = tracing_entries_write,
3348 static const struct file_operations tracing_mark_fops = {
3349 .open = tracing_open_generic,
3350 .write = tracing_mark_write,
3353 struct ftrace_buffer_info {
3354 struct trace_array *tr;
3355 void *spare;
3356 int cpu;
3357 unsigned int read;
3360 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3362 int cpu = (int)(long)inode->i_private;
3363 struct ftrace_buffer_info *info;
3365 if (tracing_disabled)
3366 return -ENODEV;
3368 info = kzalloc(sizeof(*info), GFP_KERNEL);
3369 if (!info)
3370 return -ENOMEM;
3372 info->tr = &global_trace;
3373 info->cpu = cpu;
3374 info->spare = NULL;
3375 /* Force reading ring buffer for first read */
3376 info->read = (unsigned int)-1;
3378 filp->private_data = info;
3380 return nonseekable_open(inode, filp);
3383 static ssize_t
3384 tracing_buffers_read(struct file *filp, char __user *ubuf,
3385 size_t count, loff_t *ppos)
3387 struct ftrace_buffer_info *info = filp->private_data;
3388 unsigned int pos;
3389 ssize_t ret;
3390 size_t size;
3392 if (!count)
3393 return 0;
3395 if (!info->spare)
3396 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3397 if (!info->spare)
3398 return -ENOMEM;
3400 /* Do we have previous read data to read? */
3401 if (info->read < PAGE_SIZE)
3402 goto read;
3404 info->read = 0;
3406 ret = ring_buffer_read_page(info->tr->buffer,
3407 &info->spare,
3408 count,
3409 info->cpu, 0);
3410 if (ret < 0)
3411 return 0;
3413 pos = ring_buffer_page_len(info->spare);
3415 if (pos < PAGE_SIZE)
3416 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3418 read:
3419 size = PAGE_SIZE - info->read;
3420 if (size > count)
3421 size = count;
3423 ret = copy_to_user(ubuf, info->spare + info->read, size);
3424 if (ret == size)
3425 return -EFAULT;
3426 size -= ret;
3428 *ppos += size;
3429 info->read += size;
3431 return size;
3434 static int tracing_buffers_release(struct inode *inode, struct file *file)
3436 struct ftrace_buffer_info *info = file->private_data;
3438 if (info->spare)
3439 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3440 kfree(info);
3442 return 0;
3445 struct buffer_ref {
3446 struct ring_buffer *buffer;
3447 void *page;
3448 int ref;
3451 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3452 struct pipe_buffer *buf)
3454 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3456 if (--ref->ref)
3457 return;
3459 ring_buffer_free_read_page(ref->buffer, ref->page);
3460 kfree(ref);
3461 buf->private = 0;
3464 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3465 struct pipe_buffer *buf)
3467 return 1;
3470 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3471 struct pipe_buffer *buf)
3473 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3475 ref->ref++;
3478 /* Pipe buffer operations for a buffer. */
3479 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3480 .can_merge = 0,
3481 .map = generic_pipe_buf_map,
3482 .unmap = generic_pipe_buf_unmap,
3483 .confirm = generic_pipe_buf_confirm,
3484 .release = buffer_pipe_buf_release,
3485 .steal = buffer_pipe_buf_steal,
3486 .get = buffer_pipe_buf_get,
3490 * Callback from splice_to_pipe(), if we need to release some pages
3491 * at the end of the spd in case we error'ed out in filling the pipe.
3493 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3495 struct buffer_ref *ref =
3496 (struct buffer_ref *)spd->partial[i].private;
3498 if (--ref->ref)
3499 return;
3501 ring_buffer_free_read_page(ref->buffer, ref->page);
3502 kfree(ref);
3503 spd->partial[i].private = 0;
3506 static ssize_t
3507 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3508 struct pipe_inode_info *pipe, size_t len,
3509 unsigned int flags)
3511 struct ftrace_buffer_info *info = file->private_data;
3512 struct partial_page partial[PIPE_BUFFERS];
3513 struct page *pages[PIPE_BUFFERS];
3514 struct splice_pipe_desc spd = {
3515 .pages = pages,
3516 .partial = partial,
3517 .flags = flags,
3518 .ops = &buffer_pipe_buf_ops,
3519 .spd_release = buffer_spd_release,
3521 struct buffer_ref *ref;
3522 int entries, size, i;
3523 size_t ret;
3525 if (*ppos & (PAGE_SIZE - 1)) {
3526 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3527 return -EINVAL;
3530 if (len & (PAGE_SIZE - 1)) {
3531 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3532 if (len < PAGE_SIZE)
3533 return -EINVAL;
3534 len &= PAGE_MASK;
3537 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3539 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
3540 struct page *page;
3541 int r;
3543 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3544 if (!ref)
3545 break;
3547 ref->ref = 1;
3548 ref->buffer = info->tr->buffer;
3549 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3550 if (!ref->page) {
3551 kfree(ref);
3552 break;
3555 r = ring_buffer_read_page(ref->buffer, &ref->page,
3556 len, info->cpu, 1);
3557 if (r < 0) {
3558 ring_buffer_free_read_page(ref->buffer,
3559 ref->page);
3560 kfree(ref);
3561 break;
3565 * zero out any left over data, this is going to
3566 * user land.
3568 size = ring_buffer_page_len(ref->page);
3569 if (size < PAGE_SIZE)
3570 memset(ref->page + size, 0, PAGE_SIZE - size);
3572 page = virt_to_page(ref->page);
3574 spd.pages[i] = page;
3575 spd.partial[i].len = PAGE_SIZE;
3576 spd.partial[i].offset = 0;
3577 spd.partial[i].private = (unsigned long)ref;
3578 spd.nr_pages++;
3579 *ppos += PAGE_SIZE;
3581 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3584 spd.nr_pages = i;
3586 /* did we read anything? */
3587 if (!spd.nr_pages) {
3588 if (flags & SPLICE_F_NONBLOCK)
3589 ret = -EAGAIN;
3590 else
3591 ret = 0;
3592 /* TODO: block */
3593 return ret;
3596 ret = splice_to_pipe(pipe, &spd);
3598 return ret;
3601 static const struct file_operations tracing_buffers_fops = {
3602 .open = tracing_buffers_open,
3603 .read = tracing_buffers_read,
3604 .release = tracing_buffers_release,
3605 .splice_read = tracing_buffers_splice_read,
3606 .llseek = no_llseek,
3609 static ssize_t
3610 tracing_stats_read(struct file *filp, char __user *ubuf,
3611 size_t count, loff_t *ppos)
3613 unsigned long cpu = (unsigned long)filp->private_data;
3614 struct trace_array *tr = &global_trace;
3615 struct trace_seq *s;
3616 unsigned long cnt;
3618 s = kmalloc(sizeof(*s), GFP_KERNEL);
3619 if (!s)
3620 return ENOMEM;
3622 trace_seq_init(s);
3624 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3625 trace_seq_printf(s, "entries: %ld\n", cnt);
3627 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3628 trace_seq_printf(s, "overrun: %ld\n", cnt);
3630 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3631 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3633 cnt = ring_buffer_nmi_dropped_cpu(tr->buffer, cpu);
3634 trace_seq_printf(s, "nmi dropped: %ld\n", cnt);
3636 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3638 kfree(s);
3640 return count;
3643 static const struct file_operations tracing_stats_fops = {
3644 .open = tracing_open_generic,
3645 .read = tracing_stats_read,
3648 #ifdef CONFIG_DYNAMIC_FTRACE
3650 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3652 return 0;
3655 static ssize_t
3656 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3657 size_t cnt, loff_t *ppos)
3659 static char ftrace_dyn_info_buffer[1024];
3660 static DEFINE_MUTEX(dyn_info_mutex);
3661 unsigned long *p = filp->private_data;
3662 char *buf = ftrace_dyn_info_buffer;
3663 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3664 int r;
3666 mutex_lock(&dyn_info_mutex);
3667 r = sprintf(buf, "%ld ", *p);
3669 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3670 buf[r++] = '\n';
3672 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3674 mutex_unlock(&dyn_info_mutex);
3676 return r;
3679 static const struct file_operations tracing_dyn_info_fops = {
3680 .open = tracing_open_generic,
3681 .read = tracing_read_dyn_info,
3683 #endif
3685 static struct dentry *d_tracer;
3687 struct dentry *tracing_init_dentry(void)
3689 static int once;
3691 if (d_tracer)
3692 return d_tracer;
3694 if (!debugfs_initialized())
3695 return NULL;
3697 d_tracer = debugfs_create_dir("tracing", NULL);
3699 if (!d_tracer && !once) {
3700 once = 1;
3701 pr_warning("Could not create debugfs directory 'tracing'\n");
3702 return NULL;
3705 return d_tracer;
3708 static struct dentry *d_percpu;
3710 struct dentry *tracing_dentry_percpu(void)
3712 static int once;
3713 struct dentry *d_tracer;
3715 if (d_percpu)
3716 return d_percpu;
3718 d_tracer = tracing_init_dentry();
3720 if (!d_tracer)
3721 return NULL;
3723 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3725 if (!d_percpu && !once) {
3726 once = 1;
3727 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3728 return NULL;
3731 return d_percpu;
3734 static void tracing_init_debugfs_percpu(long cpu)
3736 struct dentry *d_percpu = tracing_dentry_percpu();
3737 struct dentry *d_cpu;
3738 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3739 char cpu_dir[7];
3741 if (cpu > 999 || cpu < 0)
3742 return;
3744 sprintf(cpu_dir, "cpu%ld", cpu);
3745 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3746 if (!d_cpu) {
3747 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3748 return;
3751 /* per cpu trace_pipe */
3752 trace_create_file("trace_pipe", 0444, d_cpu,
3753 (void *) cpu, &tracing_pipe_fops);
3755 /* per cpu trace */
3756 trace_create_file("trace", 0644, d_cpu,
3757 (void *) cpu, &tracing_fops);
3759 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3760 (void *) cpu, &tracing_buffers_fops);
3762 trace_create_file("stats", 0444, d_cpu,
3763 (void *) cpu, &tracing_stats_fops);
3766 #ifdef CONFIG_FTRACE_SELFTEST
3767 /* Let selftest have access to static functions in this file */
3768 #include "trace_selftest.c"
3769 #endif
3771 struct trace_option_dentry {
3772 struct tracer_opt *opt;
3773 struct tracer_flags *flags;
3774 struct dentry *entry;
3777 static ssize_t
3778 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3779 loff_t *ppos)
3781 struct trace_option_dentry *topt = filp->private_data;
3782 char *buf;
3784 if (topt->flags->val & topt->opt->bit)
3785 buf = "1\n";
3786 else
3787 buf = "0\n";
3789 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3792 static ssize_t
3793 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3794 loff_t *ppos)
3796 struct trace_option_dentry *topt = filp->private_data;
3797 unsigned long val;
3798 char buf[64];
3799 int ret;
3801 if (cnt >= sizeof(buf))
3802 return -EINVAL;
3804 if (copy_from_user(&buf, ubuf, cnt))
3805 return -EFAULT;
3807 buf[cnt] = 0;
3809 ret = strict_strtoul(buf, 10, &val);
3810 if (ret < 0)
3811 return ret;
3813 ret = 0;
3814 switch (val) {
3815 case 0:
3816 /* do nothing if already cleared */
3817 if (!(topt->flags->val & topt->opt->bit))
3818 break;
3820 mutex_lock(&trace_types_lock);
3821 if (current_trace->set_flag)
3822 ret = current_trace->set_flag(topt->flags->val,
3823 topt->opt->bit, 0);
3824 mutex_unlock(&trace_types_lock);
3825 if (ret)
3826 return ret;
3827 topt->flags->val &= ~topt->opt->bit;
3828 break;
3829 case 1:
3830 /* do nothing if already set */
3831 if (topt->flags->val & topt->opt->bit)
3832 break;
3834 mutex_lock(&trace_types_lock);
3835 if (current_trace->set_flag)
3836 ret = current_trace->set_flag(topt->flags->val,
3837 topt->opt->bit, 1);
3838 mutex_unlock(&trace_types_lock);
3839 if (ret)
3840 return ret;
3841 topt->flags->val |= topt->opt->bit;
3842 break;
3844 default:
3845 return -EINVAL;
3848 *ppos += cnt;
3850 return cnt;
3854 static const struct file_operations trace_options_fops = {
3855 .open = tracing_open_generic,
3856 .read = trace_options_read,
3857 .write = trace_options_write,
3860 static ssize_t
3861 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3862 loff_t *ppos)
3864 long index = (long)filp->private_data;
3865 char *buf;
3867 if (trace_flags & (1 << index))
3868 buf = "1\n";
3869 else
3870 buf = "0\n";
3872 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3875 static ssize_t
3876 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3877 loff_t *ppos)
3879 long index = (long)filp->private_data;
3880 char buf[64];
3881 unsigned long val;
3882 int ret;
3884 if (cnt >= sizeof(buf))
3885 return -EINVAL;
3887 if (copy_from_user(&buf, ubuf, cnt))
3888 return -EFAULT;
3890 buf[cnt] = 0;
3892 ret = strict_strtoul(buf, 10, &val);
3893 if (ret < 0)
3894 return ret;
3896 switch (val) {
3897 case 0:
3898 trace_flags &= ~(1 << index);
3899 break;
3900 case 1:
3901 trace_flags |= 1 << index;
3902 break;
3904 default:
3905 return -EINVAL;
3908 *ppos += cnt;
3910 return cnt;
3913 static const struct file_operations trace_options_core_fops = {
3914 .open = tracing_open_generic,
3915 .read = trace_options_core_read,
3916 .write = trace_options_core_write,
3919 struct dentry *trace_create_file(const char *name,
3920 mode_t mode,
3921 struct dentry *parent,
3922 void *data,
3923 const struct file_operations *fops)
3925 struct dentry *ret;
3927 ret = debugfs_create_file(name, mode, parent, data, fops);
3928 if (!ret)
3929 pr_warning("Could not create debugfs '%s' entry\n", name);
3931 return ret;
3935 static struct dentry *trace_options_init_dentry(void)
3937 struct dentry *d_tracer;
3938 static struct dentry *t_options;
3940 if (t_options)
3941 return t_options;
3943 d_tracer = tracing_init_dentry();
3944 if (!d_tracer)
3945 return NULL;
3947 t_options = debugfs_create_dir("options", d_tracer);
3948 if (!t_options) {
3949 pr_warning("Could not create debugfs directory 'options'\n");
3950 return NULL;
3953 return t_options;
3956 static void
3957 create_trace_option_file(struct trace_option_dentry *topt,
3958 struct tracer_flags *flags,
3959 struct tracer_opt *opt)
3961 struct dentry *t_options;
3963 t_options = trace_options_init_dentry();
3964 if (!t_options)
3965 return;
3967 topt->flags = flags;
3968 topt->opt = opt;
3970 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
3971 &trace_options_fops);
3975 static struct trace_option_dentry *
3976 create_trace_option_files(struct tracer *tracer)
3978 struct trace_option_dentry *topts;
3979 struct tracer_flags *flags;
3980 struct tracer_opt *opts;
3981 int cnt;
3983 if (!tracer)
3984 return NULL;
3986 flags = tracer->flags;
3988 if (!flags || !flags->opts)
3989 return NULL;
3991 opts = flags->opts;
3993 for (cnt = 0; opts[cnt].name; cnt++)
3996 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3997 if (!topts)
3998 return NULL;
4000 for (cnt = 0; opts[cnt].name; cnt++)
4001 create_trace_option_file(&topts[cnt], flags,
4002 &opts[cnt]);
4004 return topts;
4007 static void
4008 destroy_trace_option_files(struct trace_option_dentry *topts)
4010 int cnt;
4012 if (!topts)
4013 return;
4015 for (cnt = 0; topts[cnt].opt; cnt++) {
4016 if (topts[cnt].entry)
4017 debugfs_remove(topts[cnt].entry);
4020 kfree(topts);
4023 static struct dentry *
4024 create_trace_option_core_file(const char *option, long index)
4026 struct dentry *t_options;
4028 t_options = trace_options_init_dentry();
4029 if (!t_options)
4030 return NULL;
4032 return trace_create_file(option, 0644, t_options, (void *)index,
4033 &trace_options_core_fops);
4036 static __init void create_trace_options_dir(void)
4038 struct dentry *t_options;
4039 int i;
4041 t_options = trace_options_init_dentry();
4042 if (!t_options)
4043 return;
4045 for (i = 0; trace_options[i]; i++)
4046 create_trace_option_core_file(trace_options[i], i);
4049 static __init int tracer_init_debugfs(void)
4051 struct dentry *d_tracer;
4052 int cpu;
4054 d_tracer = tracing_init_dentry();
4056 trace_create_file("tracing_enabled", 0644, d_tracer,
4057 &global_trace, &tracing_ctrl_fops);
4059 trace_create_file("trace_options", 0644, d_tracer,
4060 NULL, &tracing_iter_fops);
4062 trace_create_file("tracing_cpumask", 0644, d_tracer,
4063 NULL, &tracing_cpumask_fops);
4065 trace_create_file("trace", 0644, d_tracer,
4066 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4068 trace_create_file("available_tracers", 0444, d_tracer,
4069 &global_trace, &show_traces_fops);
4071 trace_create_file("current_tracer", 0644, d_tracer,
4072 &global_trace, &set_tracer_fops);
4074 trace_create_file("tracing_max_latency", 0644, d_tracer,
4075 &tracing_max_latency, &tracing_max_lat_fops);
4077 trace_create_file("tracing_thresh", 0644, d_tracer,
4078 &tracing_thresh, &tracing_max_lat_fops);
4080 trace_create_file("README", 0444, d_tracer,
4081 NULL, &tracing_readme_fops);
4083 trace_create_file("trace_pipe", 0444, d_tracer,
4084 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4086 trace_create_file("buffer_size_kb", 0644, d_tracer,
4087 &global_trace, &tracing_entries_fops);
4089 trace_create_file("trace_marker", 0220, d_tracer,
4090 NULL, &tracing_mark_fops);
4092 trace_create_file("saved_cmdlines", 0444, d_tracer,
4093 NULL, &tracing_saved_cmdlines_fops);
4095 #ifdef CONFIG_DYNAMIC_FTRACE
4096 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4097 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4098 #endif
4099 #ifdef CONFIG_SYSPROF_TRACER
4100 init_tracer_sysprof_debugfs(d_tracer);
4101 #endif
4103 create_trace_options_dir();
4105 for_each_tracing_cpu(cpu)
4106 tracing_init_debugfs_percpu(cpu);
4108 return 0;
4111 static int trace_panic_handler(struct notifier_block *this,
4112 unsigned long event, void *unused)
4114 if (ftrace_dump_on_oops)
4115 ftrace_dump();
4116 return NOTIFY_OK;
4119 static struct notifier_block trace_panic_notifier = {
4120 .notifier_call = trace_panic_handler,
4121 .next = NULL,
4122 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4125 static int trace_die_handler(struct notifier_block *self,
4126 unsigned long val,
4127 void *data)
4129 switch (val) {
4130 case DIE_OOPS:
4131 if (ftrace_dump_on_oops)
4132 ftrace_dump();
4133 break;
4134 default:
4135 break;
4137 return NOTIFY_OK;
4140 static struct notifier_block trace_die_notifier = {
4141 .notifier_call = trace_die_handler,
4142 .priority = 200
4146 * printk is set to max of 1024, we really don't need it that big.
4147 * Nothing should be printing 1000 characters anyway.
4149 #define TRACE_MAX_PRINT 1000
4152 * Define here KERN_TRACE so that we have one place to modify
4153 * it if we decide to change what log level the ftrace dump
4154 * should be at.
4156 #define KERN_TRACE KERN_EMERG
4158 static void
4159 trace_printk_seq(struct trace_seq *s)
4161 /* Probably should print a warning here. */
4162 if (s->len >= 1000)
4163 s->len = 1000;
4165 /* should be zero ended, but we are paranoid. */
4166 s->buffer[s->len] = 0;
4168 printk(KERN_TRACE "%s", s->buffer);
4170 trace_seq_init(s);
4173 static void __ftrace_dump(bool disable_tracing)
4175 static raw_spinlock_t ftrace_dump_lock =
4176 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
4177 /* use static because iter can be a bit big for the stack */
4178 static struct trace_iterator iter;
4179 unsigned int old_userobj;
4180 static int dump_ran;
4181 unsigned long flags;
4182 int cnt = 0, cpu;
4184 /* only one dump */
4185 local_irq_save(flags);
4186 __raw_spin_lock(&ftrace_dump_lock);
4187 if (dump_ran)
4188 goto out;
4190 dump_ran = 1;
4192 tracing_off();
4194 if (disable_tracing)
4195 ftrace_kill();
4197 for_each_tracing_cpu(cpu) {
4198 atomic_inc(&global_trace.data[cpu]->disabled);
4201 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4203 /* don't look at user memory in panic mode */
4204 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4206 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4208 /* Simulate the iterator */
4209 iter.tr = &global_trace;
4210 iter.trace = current_trace;
4211 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4214 * We need to stop all tracing on all CPUS to read the
4215 * the next buffer. This is a bit expensive, but is
4216 * not done often. We fill all what we can read,
4217 * and then release the locks again.
4220 while (!trace_empty(&iter)) {
4222 if (!cnt)
4223 printk(KERN_TRACE "---------------------------------\n");
4225 cnt++;
4227 /* reset all but tr, trace, and overruns */
4228 memset(&iter.seq, 0,
4229 sizeof(struct trace_iterator) -
4230 offsetof(struct trace_iterator, seq));
4231 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4232 iter.pos = -1;
4234 if (find_next_entry_inc(&iter) != NULL) {
4235 print_trace_line(&iter);
4236 trace_consume(&iter);
4239 trace_printk_seq(&iter.seq);
4242 if (!cnt)
4243 printk(KERN_TRACE " (ftrace buffer empty)\n");
4244 else
4245 printk(KERN_TRACE "---------------------------------\n");
4247 /* Re-enable tracing if requested */
4248 if (!disable_tracing) {
4249 trace_flags |= old_userobj;
4251 for_each_tracing_cpu(cpu) {
4252 atomic_dec(&global_trace.data[cpu]->disabled);
4254 tracing_on();
4257 out:
4258 __raw_spin_unlock(&ftrace_dump_lock);
4259 local_irq_restore(flags);
4262 /* By default: disable tracing after the dump */
4263 void ftrace_dump(void)
4265 __ftrace_dump(true);
4268 __init static int tracer_alloc_buffers(void)
4270 struct trace_array_cpu *data;
4271 int ring_buf_size;
4272 int i;
4273 int ret = -ENOMEM;
4275 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4276 goto out;
4278 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4279 goto out_free_buffer_mask;
4281 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4282 goto out_free_tracing_cpumask;
4284 /* To save memory, keep the ring buffer size to its minimum */
4285 if (ring_buffer_expanded)
4286 ring_buf_size = trace_buf_size;
4287 else
4288 ring_buf_size = 1;
4290 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4291 cpumask_copy(tracing_cpumask, cpu_all_mask);
4292 cpumask_clear(tracing_reader_cpumask);
4294 /* TODO: make the number of buffers hot pluggable with CPUS */
4295 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4296 TRACE_BUFFER_FLAGS);
4297 if (!global_trace.buffer) {
4298 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4299 WARN_ON(1);
4300 goto out_free_cpumask;
4302 global_trace.entries = ring_buffer_size(global_trace.buffer);
4305 #ifdef CONFIG_TRACER_MAX_TRACE
4306 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4307 TRACE_BUFFER_FLAGS);
4308 if (!max_tr.buffer) {
4309 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4310 WARN_ON(1);
4311 ring_buffer_free(global_trace.buffer);
4312 goto out_free_cpumask;
4314 max_tr.entries = ring_buffer_size(max_tr.buffer);
4315 WARN_ON(max_tr.entries != global_trace.entries);
4316 #endif
4318 /* Allocate the first page for all buffers */
4319 for_each_tracing_cpu(i) {
4320 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4321 max_tr.data[i] = &per_cpu(max_data, i);
4324 trace_init_cmdlines();
4326 register_tracer(&nop_trace);
4327 current_trace = &nop_trace;
4328 #ifdef CONFIG_BOOT_TRACER
4329 register_tracer(&boot_tracer);
4330 #endif
4331 /* All seems OK, enable tracing */
4332 tracing_disabled = 0;
4334 atomic_notifier_chain_register(&panic_notifier_list,
4335 &trace_panic_notifier);
4337 register_die_notifier(&trace_die_notifier);
4339 return 0;
4341 out_free_cpumask:
4342 free_cpumask_var(tracing_reader_cpumask);
4343 out_free_tracing_cpumask:
4344 free_cpumask_var(tracing_cpumask);
4345 out_free_buffer_mask:
4346 free_cpumask_var(tracing_buffer_mask);
4347 out:
4348 return ret;
4351 __init static int clear_boot_tracer(void)
4354 * The default tracer at boot buffer is an init section.
4355 * This function is called in lateinit. If we did not
4356 * find the boot tracer, then clear it out, to prevent
4357 * later registration from accessing the buffer that is
4358 * about to be freed.
4360 if (!default_bootup_tracer)
4361 return 0;
4363 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4364 default_bootup_tracer);
4365 default_bootup_tracer = NULL;
4367 return 0;
4370 early_initcall(tracer_alloc_buffers);
4371 fs_initcall(tracer_init_debugfs);
4372 late_initcall(clear_boot_tracer);