sched: Fix wake_affine() vs RT tasks
[linux/fpc-iii.git] / kernel / trace / trace.c
bloba2a2d1f02c833c2e1fb905e5c60ce69a675849aa
1 /*
2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/ring_buffer.h>
15 #include <linux/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/smp_lock.h>
21 #include <linux/notifier.h>
22 #include <linux/irqflags.h>
23 #include <linux/debugfs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/kprobes.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/ctype.h>
36 #include <linux/init.h>
37 #include <linux/poll.h>
38 #include <linux/gfp.h>
39 #include <linux/fs.h>
41 #include "trace.h"
42 #include "trace_output.h"
44 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
47 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
50 int ring_buffer_expanded;
53 * We need to change this state when a selftest is running.
54 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
56 * insertions into the ring-buffer such as trace_printk could occurred
57 * at the same time, giving false positive or negative results.
59 static bool __read_mostly tracing_selftest_running;
62 * If a tracer is running, we do not want to run SELFTEST.
64 bool __read_mostly tracing_selftest_disabled;
66 /* For tracers that don't implement custom flags */
67 static struct tracer_opt dummy_tracer_opt[] = {
68 { }
71 static struct tracer_flags dummy_tracer_flags = {
72 .val = 0,
73 .opts = dummy_tracer_opt
76 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
78 return 0;
82 * Kill all tracing for good (never come back).
83 * It is initialized to 1 but will turn to zero if the initialization
84 * of the tracer is successful. But that is the only place that sets
85 * this back to zero.
87 static int tracing_disabled = 1;
89 DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
91 static inline void ftrace_disable_cpu(void)
93 preempt_disable();
94 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
97 static inline void ftrace_enable_cpu(void)
99 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
100 preempt_enable();
103 static cpumask_var_t __read_mostly tracing_buffer_mask;
105 /* Define which cpu buffers are currently read in trace_pipe */
106 static cpumask_var_t tracing_reader_cpumask;
108 #define for_each_tracing_cpu(cpu) \
109 for_each_cpu(cpu, tracing_buffer_mask)
112 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
114 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
115 * is set, then ftrace_dump is called. This will output the contents
116 * of the ftrace buffers to the console. This is very useful for
117 * capturing traces that lead to crashes and outputing it to a
118 * serial console.
120 * It is default off, but you can enable it with either specifying
121 * "ftrace_dump_on_oops" in the kernel command line, or setting
122 * /proc/sys/kernel/ftrace_dump_on_oops to true.
124 int ftrace_dump_on_oops;
126 static int tracing_set_tracer(const char *buf);
128 #define MAX_TRACER_SIZE 100
129 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
130 static char *default_bootup_tracer;
132 static int __init set_ftrace(char *str)
134 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
135 default_bootup_tracer = bootup_tracer_buf;
136 /* We are using ftrace early, expand it */
137 ring_buffer_expanded = 1;
138 return 1;
140 __setup("ftrace=", set_ftrace);
142 static int __init set_ftrace_dump_on_oops(char *str)
144 ftrace_dump_on_oops = 1;
145 return 1;
147 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
149 unsigned long long ns2usecs(cycle_t nsec)
151 nsec += 500;
152 do_div(nsec, 1000);
153 return nsec;
157 * The global_trace is the descriptor that holds the tracing
158 * buffers for the live tracing. For each CPU, it contains
159 * a link list of pages that will store trace entries. The
160 * page descriptor of the pages in the memory is used to hold
161 * the link list by linking the lru item in the page descriptor
162 * to each of the pages in the buffer per CPU.
164 * For each active CPU there is a data field that holds the
165 * pages for the buffer for that CPU. Each CPU has the same number
166 * of pages allocated for its buffer.
168 static struct trace_array global_trace;
170 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
172 int filter_current_check_discard(struct ring_buffer *buffer,
173 struct ftrace_event_call *call, void *rec,
174 struct ring_buffer_event *event)
176 return filter_check_discard(call, rec, buffer, event);
178 EXPORT_SYMBOL_GPL(filter_current_check_discard);
180 cycle_t ftrace_now(int cpu)
182 u64 ts;
184 /* Early boot up does not have a buffer yet */
185 if (!global_trace.buffer)
186 return trace_clock_local();
188 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
189 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
191 return ts;
195 * The max_tr is used to snapshot the global_trace when a maximum
196 * latency is reached. Some tracers will use this to store a maximum
197 * trace while it continues examining live traces.
199 * The buffers for the max_tr are set up the same as the global_trace.
200 * When a snapshot is taken, the link list of the max_tr is swapped
201 * with the link list of the global_trace and the buffers are reset for
202 * the global_trace so the tracing can continue.
204 static struct trace_array max_tr;
206 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
208 /* tracer_enabled is used to toggle activation of a tracer */
209 static int tracer_enabled = 1;
212 * tracing_is_enabled - return tracer_enabled status
214 * This function is used by other tracers to know the status
215 * of the tracer_enabled flag. Tracers may use this function
216 * to know if it should enable their features when starting
217 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
219 int tracing_is_enabled(void)
221 return tracer_enabled;
225 * trace_buf_size is the size in bytes that is allocated
226 * for a buffer. Note, the number of bytes is always rounded
227 * to page size.
229 * This number is purposely set to a low number of 16384.
230 * If the dump on oops happens, it will be much appreciated
231 * to not have to wait for all that output. Anyway this can be
232 * boot time and run time configurable.
234 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
236 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
238 /* trace_types holds a link list of available tracers. */
239 static struct tracer *trace_types __read_mostly;
241 /* current_trace points to the tracer that is currently active */
242 static struct tracer *current_trace __read_mostly;
245 * trace_types_lock is used to protect the trace_types list.
246 * This lock is also used to keep user access serialized.
247 * Accesses from userspace will grab this lock while userspace
248 * activities happen inside the kernel.
250 static DEFINE_MUTEX(trace_types_lock);
252 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
253 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
255 /* trace_flags holds trace_options default values */
256 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
257 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
258 TRACE_ITER_GRAPH_TIME;
260 static int trace_stop_count;
261 static DEFINE_SPINLOCK(tracing_start_lock);
264 * trace_wake_up - wake up tasks waiting for trace input
266 * Simply wakes up any task that is blocked on the trace_wait
267 * queue. These is used with trace_poll for tasks polling the trace.
269 void trace_wake_up(void)
271 int cpu;
273 if (trace_flags & TRACE_ITER_BLOCK)
274 return;
276 * The runqueue_is_locked() can fail, but this is the best we
277 * have for now:
279 cpu = get_cpu();
280 if (!runqueue_is_locked(cpu))
281 wake_up(&trace_wait);
282 put_cpu();
285 static int __init set_buf_size(char *str)
287 unsigned long buf_size;
289 if (!str)
290 return 0;
291 buf_size = memparse(str, &str);
292 /* nr_entries can not be zero */
293 if (buf_size == 0)
294 return 0;
295 trace_buf_size = buf_size;
296 return 1;
298 __setup("trace_buf_size=", set_buf_size);
300 unsigned long nsecs_to_usecs(unsigned long nsecs)
302 return nsecs / 1000;
305 /* These must match the bit postions in trace_iterator_flags */
306 static const char *trace_options[] = {
307 "print-parent",
308 "sym-offset",
309 "sym-addr",
310 "verbose",
311 "raw",
312 "hex",
313 "bin",
314 "block",
315 "stacktrace",
316 "sched-tree",
317 "trace_printk",
318 "ftrace_preempt",
319 "branch",
320 "annotate",
321 "userstacktrace",
322 "sym-userobj",
323 "printk-msg-only",
324 "context-info",
325 "latency-format",
326 "sleep-time",
327 "graph-time",
328 NULL
331 static struct {
332 u64 (*func)(void);
333 const char *name;
334 } trace_clocks[] = {
335 { trace_clock_local, "local" },
336 { trace_clock_global, "global" },
339 int trace_clock_id;
342 * trace_parser_get_init - gets the buffer for trace parser
344 int trace_parser_get_init(struct trace_parser *parser, int size)
346 memset(parser, 0, sizeof(*parser));
348 parser->buffer = kmalloc(size, GFP_KERNEL);
349 if (!parser->buffer)
350 return 1;
352 parser->size = size;
353 return 0;
357 * trace_parser_put - frees the buffer for trace parser
359 void trace_parser_put(struct trace_parser *parser)
361 kfree(parser->buffer);
365 * trace_get_user - reads the user input string separated by space
366 * (matched by isspace(ch))
368 * For each string found the 'struct trace_parser' is updated,
369 * and the function returns.
371 * Returns number of bytes read.
373 * See kernel/trace/trace.h for 'struct trace_parser' details.
375 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
376 size_t cnt, loff_t *ppos)
378 char ch;
379 size_t read = 0;
380 ssize_t ret;
382 if (!*ppos)
383 trace_parser_clear(parser);
385 ret = get_user(ch, ubuf++);
386 if (ret)
387 goto out;
389 read++;
390 cnt--;
393 * The parser is not finished with the last write,
394 * continue reading the user input without skipping spaces.
396 if (!parser->cont) {
397 /* skip white space */
398 while (cnt && isspace(ch)) {
399 ret = get_user(ch, ubuf++);
400 if (ret)
401 goto out;
402 read++;
403 cnt--;
406 /* only spaces were written */
407 if (isspace(ch)) {
408 *ppos += read;
409 ret = read;
410 goto out;
413 parser->idx = 0;
416 /* read the non-space input */
417 while (cnt && !isspace(ch)) {
418 if (parser->idx < parser->size - 1)
419 parser->buffer[parser->idx++] = ch;
420 else {
421 ret = -EINVAL;
422 goto out;
424 ret = get_user(ch, ubuf++);
425 if (ret)
426 goto out;
427 read++;
428 cnt--;
431 /* We either got finished input or we have to wait for another call. */
432 if (isspace(ch)) {
433 parser->buffer[parser->idx] = 0;
434 parser->cont = false;
435 } else {
436 parser->cont = true;
437 parser->buffer[parser->idx++] = ch;
440 *ppos += read;
441 ret = read;
443 out:
444 return ret;
447 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
449 int len;
450 int ret;
452 if (!cnt)
453 return 0;
455 if (s->len <= s->readpos)
456 return -EBUSY;
458 len = s->len - s->readpos;
459 if (cnt > len)
460 cnt = len;
461 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
462 if (ret == cnt)
463 return -EFAULT;
465 cnt -= ret;
467 s->readpos += cnt;
468 return cnt;
471 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
473 int len;
474 void *ret;
476 if (s->len <= s->readpos)
477 return -EBUSY;
479 len = s->len - s->readpos;
480 if (cnt > len)
481 cnt = len;
482 ret = memcpy(buf, s->buffer + s->readpos, cnt);
483 if (!ret)
484 return -EFAULT;
486 s->readpos += cnt;
487 return cnt;
491 * ftrace_max_lock is used to protect the swapping of buffers
492 * when taking a max snapshot. The buffers themselves are
493 * protected by per_cpu spinlocks. But the action of the swap
494 * needs its own lock.
496 * This is defined as a raw_spinlock_t in order to help
497 * with performance when lockdep debugging is enabled.
499 * It is also used in other places outside the update_max_tr
500 * so it needs to be defined outside of the
501 * CONFIG_TRACER_MAX_TRACE.
503 static raw_spinlock_t ftrace_max_lock =
504 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
506 #ifdef CONFIG_TRACER_MAX_TRACE
507 unsigned long __read_mostly tracing_max_latency;
508 unsigned long __read_mostly tracing_thresh;
511 * Copy the new maximum trace into the separate maximum-trace
512 * structure. (this way the maximum trace is permanently saved,
513 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
515 static void
516 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
518 struct trace_array_cpu *data = tr->data[cpu];
519 struct trace_array_cpu *max_data = tr->data[cpu];
521 max_tr.cpu = cpu;
522 max_tr.time_start = data->preempt_timestamp;
524 max_data = max_tr.data[cpu];
525 max_data->saved_latency = tracing_max_latency;
526 max_data->critical_start = data->critical_start;
527 max_data->critical_end = data->critical_end;
529 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
530 max_data->pid = tsk->pid;
531 max_data->uid = task_uid(tsk);
532 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
533 max_data->policy = tsk->policy;
534 max_data->rt_priority = tsk->rt_priority;
536 /* record this tasks comm */
537 tracing_record_cmdline(tsk);
541 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
542 * @tr: tracer
543 * @tsk: the task with the latency
544 * @cpu: The cpu that initiated the trace.
546 * Flip the buffers between the @tr and the max_tr and record information
547 * about which task was the cause of this latency.
549 void
550 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
552 struct ring_buffer *buf = tr->buffer;
554 if (trace_stop_count)
555 return;
557 WARN_ON_ONCE(!irqs_disabled());
558 __raw_spin_lock(&ftrace_max_lock);
560 tr->buffer = max_tr.buffer;
561 max_tr.buffer = buf;
563 __update_max_tr(tr, tsk, cpu);
564 __raw_spin_unlock(&ftrace_max_lock);
568 * update_max_tr_single - only copy one trace over, and reset the rest
569 * @tr - tracer
570 * @tsk - task with the latency
571 * @cpu - the cpu of the buffer to copy.
573 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
575 void
576 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
578 int ret;
580 if (trace_stop_count)
581 return;
583 WARN_ON_ONCE(!irqs_disabled());
584 __raw_spin_lock(&ftrace_max_lock);
586 ftrace_disable_cpu();
588 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
590 if (ret == -EBUSY) {
592 * We failed to swap the buffer due to a commit taking
593 * place on this CPU. We fail to record, but we reset
594 * the max trace buffer (no one writes directly to it)
595 * and flag that it failed.
597 trace_array_printk(&max_tr, _THIS_IP_,
598 "Failed to swap buffers due to commit in progress\n");
601 ftrace_enable_cpu();
603 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
605 __update_max_tr(tr, tsk, cpu);
606 __raw_spin_unlock(&ftrace_max_lock);
608 #endif /* CONFIG_TRACER_MAX_TRACE */
611 * register_tracer - register a tracer with the ftrace system.
612 * @type - the plugin for the tracer
614 * Register a new plugin tracer.
616 int register_tracer(struct tracer *type)
617 __releases(kernel_lock)
618 __acquires(kernel_lock)
620 struct tracer *t;
621 int ret = 0;
623 if (!type->name) {
624 pr_info("Tracer must have a name\n");
625 return -1;
628 if (strlen(type->name) > MAX_TRACER_SIZE) {
629 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
630 return -1;
634 * When this gets called we hold the BKL which means that
635 * preemption is disabled. Various trace selftests however
636 * need to disable and enable preemption for successful tests.
637 * So we drop the BKL here and grab it after the tests again.
639 unlock_kernel();
640 mutex_lock(&trace_types_lock);
642 tracing_selftest_running = true;
644 for (t = trace_types; t; t = t->next) {
645 if (strcmp(type->name, t->name) == 0) {
646 /* already found */
647 pr_info("Tracer %s already registered\n",
648 type->name);
649 ret = -1;
650 goto out;
654 if (!type->set_flag)
655 type->set_flag = &dummy_set_flag;
656 if (!type->flags)
657 type->flags = &dummy_tracer_flags;
658 else
659 if (!type->flags->opts)
660 type->flags->opts = dummy_tracer_opt;
661 if (!type->wait_pipe)
662 type->wait_pipe = default_wait_pipe;
665 #ifdef CONFIG_FTRACE_STARTUP_TEST
666 if (type->selftest && !tracing_selftest_disabled) {
667 struct tracer *saved_tracer = current_trace;
668 struct trace_array *tr = &global_trace;
671 * Run a selftest on this tracer.
672 * Here we reset the trace buffer, and set the current
673 * tracer to be this tracer. The tracer can then run some
674 * internal tracing to verify that everything is in order.
675 * If we fail, we do not register this tracer.
677 tracing_reset_online_cpus(tr);
679 current_trace = type;
680 /* the test is responsible for initializing and enabling */
681 pr_info("Testing tracer %s: ", type->name);
682 ret = type->selftest(type, tr);
683 /* the test is responsible for resetting too */
684 current_trace = saved_tracer;
685 if (ret) {
686 printk(KERN_CONT "FAILED!\n");
687 goto out;
689 /* Only reset on passing, to avoid touching corrupted buffers */
690 tracing_reset_online_cpus(tr);
692 printk(KERN_CONT "PASSED\n");
694 #endif
696 type->next = trace_types;
697 trace_types = type;
699 out:
700 tracing_selftest_running = false;
701 mutex_unlock(&trace_types_lock);
703 if (ret || !default_bootup_tracer)
704 goto out_unlock;
706 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
707 goto out_unlock;
709 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
710 /* Do we want this tracer to start on bootup? */
711 tracing_set_tracer(type->name);
712 default_bootup_tracer = NULL;
713 /* disable other selftests, since this will break it. */
714 tracing_selftest_disabled = 1;
715 #ifdef CONFIG_FTRACE_STARTUP_TEST
716 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
717 type->name);
718 #endif
720 out_unlock:
721 lock_kernel();
722 return ret;
725 void unregister_tracer(struct tracer *type)
727 struct tracer **t;
729 mutex_lock(&trace_types_lock);
730 for (t = &trace_types; *t; t = &(*t)->next) {
731 if (*t == type)
732 goto found;
734 pr_info("Tracer %s not registered\n", type->name);
735 goto out;
737 found:
738 *t = (*t)->next;
740 if (type == current_trace && tracer_enabled) {
741 tracer_enabled = 0;
742 tracing_stop();
743 if (current_trace->stop)
744 current_trace->stop(&global_trace);
745 current_trace = &nop_trace;
747 out:
748 mutex_unlock(&trace_types_lock);
751 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
753 ftrace_disable_cpu();
754 ring_buffer_reset_cpu(buffer, cpu);
755 ftrace_enable_cpu();
758 void tracing_reset(struct trace_array *tr, int cpu)
760 struct ring_buffer *buffer = tr->buffer;
762 ring_buffer_record_disable(buffer);
764 /* Make sure all commits have finished */
765 synchronize_sched();
766 __tracing_reset(buffer, cpu);
768 ring_buffer_record_enable(buffer);
771 void tracing_reset_online_cpus(struct trace_array *tr)
773 struct ring_buffer *buffer = tr->buffer;
774 int cpu;
776 ring_buffer_record_disable(buffer);
778 /* Make sure all commits have finished */
779 synchronize_sched();
781 tr->time_start = ftrace_now(tr->cpu);
783 for_each_online_cpu(cpu)
784 __tracing_reset(buffer, cpu);
786 ring_buffer_record_enable(buffer);
789 void tracing_reset_current(int cpu)
791 tracing_reset(&global_trace, cpu);
794 void tracing_reset_current_online_cpus(void)
796 tracing_reset_online_cpus(&global_trace);
799 #define SAVED_CMDLINES 128
800 #define NO_CMDLINE_MAP UINT_MAX
801 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
802 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
803 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
804 static int cmdline_idx;
805 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
807 /* temporary disable recording */
808 static atomic_t trace_record_cmdline_disabled __read_mostly;
810 static void trace_init_cmdlines(void)
812 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
813 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
814 cmdline_idx = 0;
817 int is_tracing_stopped(void)
819 return trace_stop_count;
823 * ftrace_off_permanent - disable all ftrace code permanently
825 * This should only be called when a serious anomally has
826 * been detected. This will turn off the function tracing,
827 * ring buffers, and other tracing utilites. It takes no
828 * locks and can be called from any context.
830 void ftrace_off_permanent(void)
832 tracing_disabled = 1;
833 ftrace_stop();
834 tracing_off_permanent();
838 * tracing_start - quick start of the tracer
840 * If tracing is enabled but was stopped by tracing_stop,
841 * this will start the tracer back up.
843 void tracing_start(void)
845 struct ring_buffer *buffer;
846 unsigned long flags;
848 if (tracing_disabled)
849 return;
851 spin_lock_irqsave(&tracing_start_lock, flags);
852 if (--trace_stop_count) {
853 if (trace_stop_count < 0) {
854 /* Someone screwed up their debugging */
855 WARN_ON_ONCE(1);
856 trace_stop_count = 0;
858 goto out;
861 /* Prevent the buffers from switching */
862 __raw_spin_lock(&ftrace_max_lock);
864 buffer = global_trace.buffer;
865 if (buffer)
866 ring_buffer_record_enable(buffer);
868 buffer = max_tr.buffer;
869 if (buffer)
870 ring_buffer_record_enable(buffer);
872 __raw_spin_unlock(&ftrace_max_lock);
874 ftrace_start();
875 out:
876 spin_unlock_irqrestore(&tracing_start_lock, flags);
880 * tracing_stop - quick stop of the tracer
882 * Light weight way to stop tracing. Use in conjunction with
883 * tracing_start.
885 void tracing_stop(void)
887 struct ring_buffer *buffer;
888 unsigned long flags;
890 ftrace_stop();
891 spin_lock_irqsave(&tracing_start_lock, flags);
892 if (trace_stop_count++)
893 goto out;
895 /* Prevent the buffers from switching */
896 __raw_spin_lock(&ftrace_max_lock);
898 buffer = global_trace.buffer;
899 if (buffer)
900 ring_buffer_record_disable(buffer);
902 buffer = max_tr.buffer;
903 if (buffer)
904 ring_buffer_record_disable(buffer);
906 __raw_spin_unlock(&ftrace_max_lock);
908 out:
909 spin_unlock_irqrestore(&tracing_start_lock, flags);
912 void trace_stop_cmdline_recording(void);
914 static void trace_save_cmdline(struct task_struct *tsk)
916 unsigned pid, idx;
918 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
919 return;
922 * It's not the end of the world if we don't get
923 * the lock, but we also don't want to spin
924 * nor do we want to disable interrupts,
925 * so if we miss here, then better luck next time.
927 if (!__raw_spin_trylock(&trace_cmdline_lock))
928 return;
930 idx = map_pid_to_cmdline[tsk->pid];
931 if (idx == NO_CMDLINE_MAP) {
932 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
935 * Check whether the cmdline buffer at idx has a pid
936 * mapped. We are going to overwrite that entry so we
937 * need to clear the map_pid_to_cmdline. Otherwise we
938 * would read the new comm for the old pid.
940 pid = map_cmdline_to_pid[idx];
941 if (pid != NO_CMDLINE_MAP)
942 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
944 map_cmdline_to_pid[idx] = tsk->pid;
945 map_pid_to_cmdline[tsk->pid] = idx;
947 cmdline_idx = idx;
950 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
952 __raw_spin_unlock(&trace_cmdline_lock);
955 void trace_find_cmdline(int pid, char comm[])
957 unsigned map;
959 if (!pid) {
960 strcpy(comm, "<idle>");
961 return;
964 if (pid > PID_MAX_DEFAULT) {
965 strcpy(comm, "<...>");
966 return;
969 preempt_disable();
970 __raw_spin_lock(&trace_cmdline_lock);
971 map = map_pid_to_cmdline[pid];
972 if (map != NO_CMDLINE_MAP)
973 strcpy(comm, saved_cmdlines[map]);
974 else
975 strcpy(comm, "<...>");
977 __raw_spin_unlock(&trace_cmdline_lock);
978 preempt_enable();
981 void tracing_record_cmdline(struct task_struct *tsk)
983 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
984 !tracing_is_on())
985 return;
987 trace_save_cmdline(tsk);
990 void
991 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
992 int pc)
994 struct task_struct *tsk = current;
996 entry->preempt_count = pc & 0xff;
997 entry->pid = (tsk) ? tsk->pid : 0;
998 entry->lock_depth = (tsk) ? tsk->lock_depth : 0;
999 entry->flags =
1000 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1001 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1002 #else
1003 TRACE_FLAG_IRQS_NOSUPPORT |
1004 #endif
1005 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1006 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1007 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1009 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1011 struct ring_buffer_event *
1012 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1013 int type,
1014 unsigned long len,
1015 unsigned long flags, int pc)
1017 struct ring_buffer_event *event;
1019 event = ring_buffer_lock_reserve(buffer, len);
1020 if (event != NULL) {
1021 struct trace_entry *ent = ring_buffer_event_data(event);
1023 tracing_generic_entry_update(ent, flags, pc);
1024 ent->type = type;
1027 return event;
1030 static inline void
1031 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1032 struct ring_buffer_event *event,
1033 unsigned long flags, int pc,
1034 int wake)
1036 ring_buffer_unlock_commit(buffer, event);
1038 ftrace_trace_stack(buffer, flags, 6, pc);
1039 ftrace_trace_userstack(buffer, flags, pc);
1041 if (wake)
1042 trace_wake_up();
1045 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1046 struct ring_buffer_event *event,
1047 unsigned long flags, int pc)
1049 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1052 struct ring_buffer_event *
1053 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1054 int type, unsigned long len,
1055 unsigned long flags, int pc)
1057 *current_rb = global_trace.buffer;
1058 return trace_buffer_lock_reserve(*current_rb,
1059 type, len, flags, pc);
1061 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1063 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1064 struct ring_buffer_event *event,
1065 unsigned long flags, int pc)
1067 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1069 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1071 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1072 struct ring_buffer_event *event,
1073 unsigned long flags, int pc)
1075 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1077 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1079 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1080 struct ring_buffer_event *event)
1082 ring_buffer_discard_commit(buffer, event);
1084 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1086 void
1087 trace_function(struct trace_array *tr,
1088 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1089 int pc)
1091 struct ftrace_event_call *call = &event_function;
1092 struct ring_buffer *buffer = tr->buffer;
1093 struct ring_buffer_event *event;
1094 struct ftrace_entry *entry;
1096 /* If we are reading the ring buffer, don't trace */
1097 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
1098 return;
1100 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1101 flags, pc);
1102 if (!event)
1103 return;
1104 entry = ring_buffer_event_data(event);
1105 entry->ip = ip;
1106 entry->parent_ip = parent_ip;
1108 if (!filter_check_discard(call, entry, buffer, event))
1109 ring_buffer_unlock_commit(buffer, event);
1112 void
1113 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1114 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1115 int pc)
1117 if (likely(!atomic_read(&data->disabled)))
1118 trace_function(tr, ip, parent_ip, flags, pc);
1121 #ifdef CONFIG_STACKTRACE
1122 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1123 unsigned long flags,
1124 int skip, int pc)
1126 struct ftrace_event_call *call = &event_kernel_stack;
1127 struct ring_buffer_event *event;
1128 struct stack_entry *entry;
1129 struct stack_trace trace;
1131 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1132 sizeof(*entry), flags, pc);
1133 if (!event)
1134 return;
1135 entry = ring_buffer_event_data(event);
1136 memset(&entry->caller, 0, sizeof(entry->caller));
1138 trace.nr_entries = 0;
1139 trace.max_entries = FTRACE_STACK_ENTRIES;
1140 trace.skip = skip;
1141 trace.entries = entry->caller;
1143 save_stack_trace(&trace);
1144 if (!filter_check_discard(call, entry, buffer, event))
1145 ring_buffer_unlock_commit(buffer, event);
1148 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1149 int skip, int pc)
1151 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1152 return;
1154 __ftrace_trace_stack(buffer, flags, skip, pc);
1157 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1158 int pc)
1160 __ftrace_trace_stack(tr->buffer, flags, skip, pc);
1163 void
1164 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1166 struct ftrace_event_call *call = &event_user_stack;
1167 struct ring_buffer_event *event;
1168 struct userstack_entry *entry;
1169 struct stack_trace trace;
1171 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1172 return;
1175 * NMIs can not handle page faults, even with fix ups.
1176 * The save user stack can (and often does) fault.
1178 if (unlikely(in_nmi()))
1179 return;
1181 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1182 sizeof(*entry), flags, pc);
1183 if (!event)
1184 return;
1185 entry = ring_buffer_event_data(event);
1187 entry->tgid = current->tgid;
1188 memset(&entry->caller, 0, sizeof(entry->caller));
1190 trace.nr_entries = 0;
1191 trace.max_entries = FTRACE_STACK_ENTRIES;
1192 trace.skip = 0;
1193 trace.entries = entry->caller;
1195 save_stack_trace_user(&trace);
1196 if (!filter_check_discard(call, entry, buffer, event))
1197 ring_buffer_unlock_commit(buffer, event);
1200 #ifdef UNUSED
1201 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1203 ftrace_trace_userstack(tr, flags, preempt_count());
1205 #endif /* UNUSED */
1207 #endif /* CONFIG_STACKTRACE */
1209 static void
1210 ftrace_trace_special(void *__tr,
1211 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1212 int pc)
1214 struct ftrace_event_call *call = &event_special;
1215 struct ring_buffer_event *event;
1216 struct trace_array *tr = __tr;
1217 struct ring_buffer *buffer = tr->buffer;
1218 struct special_entry *entry;
1220 event = trace_buffer_lock_reserve(buffer, TRACE_SPECIAL,
1221 sizeof(*entry), 0, pc);
1222 if (!event)
1223 return;
1224 entry = ring_buffer_event_data(event);
1225 entry->arg1 = arg1;
1226 entry->arg2 = arg2;
1227 entry->arg3 = arg3;
1229 if (!filter_check_discard(call, entry, buffer, event))
1230 trace_buffer_unlock_commit(buffer, event, 0, pc);
1233 void
1234 __trace_special(void *__tr, void *__data,
1235 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1237 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1240 void
1241 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1243 struct trace_array *tr = &global_trace;
1244 struct trace_array_cpu *data;
1245 unsigned long flags;
1246 int cpu;
1247 int pc;
1249 if (tracing_disabled)
1250 return;
1252 pc = preempt_count();
1253 local_irq_save(flags);
1254 cpu = raw_smp_processor_id();
1255 data = tr->data[cpu];
1257 if (likely(atomic_inc_return(&data->disabled) == 1))
1258 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1260 atomic_dec(&data->disabled);
1261 local_irq_restore(flags);
1265 * trace_vbprintk - write binary msg to tracing buffer
1268 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1270 static raw_spinlock_t trace_buf_lock =
1271 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1272 static u32 trace_buf[TRACE_BUF_SIZE];
1274 struct ftrace_event_call *call = &event_bprint;
1275 struct ring_buffer_event *event;
1276 struct ring_buffer *buffer;
1277 struct trace_array *tr = &global_trace;
1278 struct trace_array_cpu *data;
1279 struct bprint_entry *entry;
1280 unsigned long flags;
1281 int disable;
1282 int resched;
1283 int cpu, len = 0, size, pc;
1285 if (unlikely(tracing_selftest_running || tracing_disabled))
1286 return 0;
1288 /* Don't pollute graph traces with trace_vprintk internals */
1289 pause_graph_tracing();
1291 pc = preempt_count();
1292 resched = ftrace_preempt_disable();
1293 cpu = raw_smp_processor_id();
1294 data = tr->data[cpu];
1296 disable = atomic_inc_return(&data->disabled);
1297 if (unlikely(disable != 1))
1298 goto out;
1300 /* Lockdep uses trace_printk for lock tracing */
1301 local_irq_save(flags);
1302 __raw_spin_lock(&trace_buf_lock);
1303 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1305 if (len > TRACE_BUF_SIZE || len < 0)
1306 goto out_unlock;
1308 size = sizeof(*entry) + sizeof(u32) * len;
1309 buffer = tr->buffer;
1310 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1311 flags, pc);
1312 if (!event)
1313 goto out_unlock;
1314 entry = ring_buffer_event_data(event);
1315 entry->ip = ip;
1316 entry->fmt = fmt;
1318 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1319 if (!filter_check_discard(call, entry, buffer, event))
1320 ring_buffer_unlock_commit(buffer, event);
1322 out_unlock:
1323 __raw_spin_unlock(&trace_buf_lock);
1324 local_irq_restore(flags);
1326 out:
1327 atomic_dec_return(&data->disabled);
1328 ftrace_preempt_enable(resched);
1329 unpause_graph_tracing();
1331 return len;
1333 EXPORT_SYMBOL_GPL(trace_vbprintk);
1335 int trace_array_printk(struct trace_array *tr,
1336 unsigned long ip, const char *fmt, ...)
1338 int ret;
1339 va_list ap;
1341 if (!(trace_flags & TRACE_ITER_PRINTK))
1342 return 0;
1344 va_start(ap, fmt);
1345 ret = trace_array_vprintk(tr, ip, fmt, ap);
1346 va_end(ap);
1347 return ret;
1350 int trace_array_vprintk(struct trace_array *tr,
1351 unsigned long ip, const char *fmt, va_list args)
1353 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1354 static char trace_buf[TRACE_BUF_SIZE];
1356 struct ftrace_event_call *call = &event_print;
1357 struct ring_buffer_event *event;
1358 struct ring_buffer *buffer;
1359 struct trace_array_cpu *data;
1360 int cpu, len = 0, size, pc;
1361 struct print_entry *entry;
1362 unsigned long irq_flags;
1363 int disable;
1365 if (tracing_disabled || tracing_selftest_running)
1366 return 0;
1368 pc = preempt_count();
1369 preempt_disable_notrace();
1370 cpu = raw_smp_processor_id();
1371 data = tr->data[cpu];
1373 disable = atomic_inc_return(&data->disabled);
1374 if (unlikely(disable != 1))
1375 goto out;
1377 pause_graph_tracing();
1378 raw_local_irq_save(irq_flags);
1379 __raw_spin_lock(&trace_buf_lock);
1380 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1382 len = min(len, TRACE_BUF_SIZE-1);
1383 trace_buf[len] = 0;
1385 size = sizeof(*entry) + len + 1;
1386 buffer = tr->buffer;
1387 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1388 irq_flags, pc);
1389 if (!event)
1390 goto out_unlock;
1391 entry = ring_buffer_event_data(event);
1392 entry->ip = ip;
1394 memcpy(&entry->buf, trace_buf, len);
1395 entry->buf[len] = 0;
1396 if (!filter_check_discard(call, entry, buffer, event))
1397 ring_buffer_unlock_commit(buffer, event);
1399 out_unlock:
1400 __raw_spin_unlock(&trace_buf_lock);
1401 raw_local_irq_restore(irq_flags);
1402 unpause_graph_tracing();
1403 out:
1404 atomic_dec_return(&data->disabled);
1405 preempt_enable_notrace();
1407 return len;
1410 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1412 return trace_array_vprintk(&global_trace, ip, fmt, args);
1414 EXPORT_SYMBOL_GPL(trace_vprintk);
1416 enum trace_file_type {
1417 TRACE_FILE_LAT_FMT = 1,
1418 TRACE_FILE_ANNOTATE = 2,
1421 static void trace_iterator_increment(struct trace_iterator *iter)
1423 /* Don't allow ftrace to trace into the ring buffers */
1424 ftrace_disable_cpu();
1426 iter->idx++;
1427 if (iter->buffer_iter[iter->cpu])
1428 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1430 ftrace_enable_cpu();
1433 static struct trace_entry *
1434 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1436 struct ring_buffer_event *event;
1437 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1439 /* Don't allow ftrace to trace into the ring buffers */
1440 ftrace_disable_cpu();
1442 if (buf_iter)
1443 event = ring_buffer_iter_peek(buf_iter, ts);
1444 else
1445 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1447 ftrace_enable_cpu();
1449 return event ? ring_buffer_event_data(event) : NULL;
1452 static struct trace_entry *
1453 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1455 struct ring_buffer *buffer = iter->tr->buffer;
1456 struct trace_entry *ent, *next = NULL;
1457 int cpu_file = iter->cpu_file;
1458 u64 next_ts = 0, ts;
1459 int next_cpu = -1;
1460 int cpu;
1463 * If we are in a per_cpu trace file, don't bother by iterating over
1464 * all cpu and peek directly.
1466 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1467 if (ring_buffer_empty_cpu(buffer, cpu_file))
1468 return NULL;
1469 ent = peek_next_entry(iter, cpu_file, ent_ts);
1470 if (ent_cpu)
1471 *ent_cpu = cpu_file;
1473 return ent;
1476 for_each_tracing_cpu(cpu) {
1478 if (ring_buffer_empty_cpu(buffer, cpu))
1479 continue;
1481 ent = peek_next_entry(iter, cpu, &ts);
1484 * Pick the entry with the smallest timestamp:
1486 if (ent && (!next || ts < next_ts)) {
1487 next = ent;
1488 next_cpu = cpu;
1489 next_ts = ts;
1493 if (ent_cpu)
1494 *ent_cpu = next_cpu;
1496 if (ent_ts)
1497 *ent_ts = next_ts;
1499 return next;
1502 /* Find the next real entry, without updating the iterator itself */
1503 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1504 int *ent_cpu, u64 *ent_ts)
1506 return __find_next_entry(iter, ent_cpu, ent_ts);
1509 /* Find the next real entry, and increment the iterator to the next entry */
1510 static void *find_next_entry_inc(struct trace_iterator *iter)
1512 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1514 if (iter->ent)
1515 trace_iterator_increment(iter);
1517 return iter->ent ? iter : NULL;
1520 static void trace_consume(struct trace_iterator *iter)
1522 /* Don't allow ftrace to trace into the ring buffers */
1523 ftrace_disable_cpu();
1524 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1525 ftrace_enable_cpu();
1528 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1530 struct trace_iterator *iter = m->private;
1531 int i = (int)*pos;
1532 void *ent;
1534 (*pos)++;
1536 /* can't go backwards */
1537 if (iter->idx > i)
1538 return NULL;
1540 if (iter->idx < 0)
1541 ent = find_next_entry_inc(iter);
1542 else
1543 ent = iter;
1545 while (ent && iter->idx < i)
1546 ent = find_next_entry_inc(iter);
1548 iter->pos = *pos;
1550 return ent;
1553 static void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1555 struct trace_array *tr = iter->tr;
1556 struct ring_buffer_event *event;
1557 struct ring_buffer_iter *buf_iter;
1558 unsigned long entries = 0;
1559 u64 ts;
1561 tr->data[cpu]->skipped_entries = 0;
1563 if (!iter->buffer_iter[cpu])
1564 return;
1566 buf_iter = iter->buffer_iter[cpu];
1567 ring_buffer_iter_reset(buf_iter);
1570 * We could have the case with the max latency tracers
1571 * that a reset never took place on a cpu. This is evident
1572 * by the timestamp being before the start of the buffer.
1574 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1575 if (ts >= iter->tr->time_start)
1576 break;
1577 entries++;
1578 ring_buffer_read(buf_iter, NULL);
1581 tr->data[cpu]->skipped_entries = entries;
1585 * No necessary locking here. The worst thing which can
1586 * happen is loosing events consumed at the same time
1587 * by a trace_pipe reader.
1588 * Other than that, we don't risk to crash the ring buffer
1589 * because it serializes the readers.
1591 * The current tracer is copied to avoid a global locking
1592 * all around.
1594 static void *s_start(struct seq_file *m, loff_t *pos)
1596 struct trace_iterator *iter = m->private;
1597 static struct tracer *old_tracer;
1598 int cpu_file = iter->cpu_file;
1599 void *p = NULL;
1600 loff_t l = 0;
1601 int cpu;
1603 /* copy the tracer to avoid using a global lock all around */
1604 mutex_lock(&trace_types_lock);
1605 if (unlikely(old_tracer != current_trace && current_trace)) {
1606 old_tracer = current_trace;
1607 *iter->trace = *current_trace;
1609 mutex_unlock(&trace_types_lock);
1611 atomic_inc(&trace_record_cmdline_disabled);
1613 if (*pos != iter->pos) {
1614 iter->ent = NULL;
1615 iter->cpu = 0;
1616 iter->idx = -1;
1618 ftrace_disable_cpu();
1620 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1621 for_each_tracing_cpu(cpu)
1622 tracing_iter_reset(iter, cpu);
1623 } else
1624 tracing_iter_reset(iter, cpu_file);
1626 ftrace_enable_cpu();
1628 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1631 } else {
1632 l = *pos - 1;
1633 p = s_next(m, p, &l);
1636 trace_event_read_lock();
1637 return p;
1640 static void s_stop(struct seq_file *m, void *p)
1642 atomic_dec(&trace_record_cmdline_disabled);
1643 trace_event_read_unlock();
1646 static void print_lat_help_header(struct seq_file *m)
1648 seq_puts(m, "# _------=> CPU# \n");
1649 seq_puts(m, "# / _-----=> irqs-off \n");
1650 seq_puts(m, "# | / _----=> need-resched \n");
1651 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1652 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1653 seq_puts(m, "# |||| /_--=> lock-depth \n");
1654 seq_puts(m, "# |||||/ delay \n");
1655 seq_puts(m, "# cmd pid |||||| time | caller \n");
1656 seq_puts(m, "# \\ / |||||| \\ | / \n");
1659 static void print_func_help_header(struct seq_file *m)
1661 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1662 seq_puts(m, "# | | | | |\n");
1666 static void
1667 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1669 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1670 struct trace_array *tr = iter->tr;
1671 struct trace_array_cpu *data = tr->data[tr->cpu];
1672 struct tracer *type = current_trace;
1673 unsigned long entries = 0;
1674 unsigned long total = 0;
1675 unsigned long count;
1676 const char *name = "preemption";
1677 int cpu;
1679 if (type)
1680 name = type->name;
1683 for_each_tracing_cpu(cpu) {
1684 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1686 * If this buffer has skipped entries, then we hold all
1687 * entries for the trace and we need to ignore the
1688 * ones before the time stamp.
1690 if (tr->data[cpu]->skipped_entries) {
1691 count -= tr->data[cpu]->skipped_entries;
1692 /* total is the same as the entries */
1693 total += count;
1694 } else
1695 total += count +
1696 ring_buffer_overrun_cpu(tr->buffer, cpu);
1697 entries += count;
1700 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1701 name, UTS_RELEASE);
1702 seq_puts(m, "# -----------------------------------"
1703 "---------------------------------\n");
1704 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1705 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1706 nsecs_to_usecs(data->saved_latency),
1707 entries,
1708 total,
1709 tr->cpu,
1710 #if defined(CONFIG_PREEMPT_NONE)
1711 "server",
1712 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1713 "desktop",
1714 #elif defined(CONFIG_PREEMPT)
1715 "preempt",
1716 #else
1717 "unknown",
1718 #endif
1719 /* These are reserved for later use */
1720 0, 0, 0, 0);
1721 #ifdef CONFIG_SMP
1722 seq_printf(m, " #P:%d)\n", num_online_cpus());
1723 #else
1724 seq_puts(m, ")\n");
1725 #endif
1726 seq_puts(m, "# -----------------\n");
1727 seq_printf(m, "# | task: %.16s-%d "
1728 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1729 data->comm, data->pid, data->uid, data->nice,
1730 data->policy, data->rt_priority);
1731 seq_puts(m, "# -----------------\n");
1733 if (data->critical_start) {
1734 seq_puts(m, "# => started at: ");
1735 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1736 trace_print_seq(m, &iter->seq);
1737 seq_puts(m, "\n# => ended at: ");
1738 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1739 trace_print_seq(m, &iter->seq);
1740 seq_puts(m, "\n#\n");
1743 seq_puts(m, "#\n");
1746 static void test_cpu_buff_start(struct trace_iterator *iter)
1748 struct trace_seq *s = &iter->seq;
1750 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1751 return;
1753 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1754 return;
1756 if (cpumask_test_cpu(iter->cpu, iter->started))
1757 return;
1759 if (iter->tr->data[iter->cpu]->skipped_entries)
1760 return;
1762 cpumask_set_cpu(iter->cpu, iter->started);
1764 /* Don't print started cpu buffer for the first entry of the trace */
1765 if (iter->idx > 1)
1766 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1767 iter->cpu);
1770 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1772 struct trace_seq *s = &iter->seq;
1773 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1774 struct trace_entry *entry;
1775 struct trace_event *event;
1777 entry = iter->ent;
1779 test_cpu_buff_start(iter);
1781 event = ftrace_find_event(entry->type);
1783 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1784 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1785 if (!trace_print_lat_context(iter))
1786 goto partial;
1787 } else {
1788 if (!trace_print_context(iter))
1789 goto partial;
1793 if (event)
1794 return event->trace(iter, sym_flags);
1796 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1797 goto partial;
1799 return TRACE_TYPE_HANDLED;
1800 partial:
1801 return TRACE_TYPE_PARTIAL_LINE;
1804 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1806 struct trace_seq *s = &iter->seq;
1807 struct trace_entry *entry;
1808 struct trace_event *event;
1810 entry = iter->ent;
1812 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1813 if (!trace_seq_printf(s, "%d %d %llu ",
1814 entry->pid, iter->cpu, iter->ts))
1815 goto partial;
1818 event = ftrace_find_event(entry->type);
1819 if (event)
1820 return event->raw(iter, 0);
1822 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1823 goto partial;
1825 return TRACE_TYPE_HANDLED;
1826 partial:
1827 return TRACE_TYPE_PARTIAL_LINE;
1830 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1832 struct trace_seq *s = &iter->seq;
1833 unsigned char newline = '\n';
1834 struct trace_entry *entry;
1835 struct trace_event *event;
1837 entry = iter->ent;
1839 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1840 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1841 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1842 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1845 event = ftrace_find_event(entry->type);
1846 if (event) {
1847 enum print_line_t ret = event->hex(iter, 0);
1848 if (ret != TRACE_TYPE_HANDLED)
1849 return ret;
1852 SEQ_PUT_FIELD_RET(s, newline);
1854 return TRACE_TYPE_HANDLED;
1857 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1859 struct trace_seq *s = &iter->seq;
1860 struct trace_entry *entry;
1861 struct trace_event *event;
1863 entry = iter->ent;
1865 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1866 SEQ_PUT_FIELD_RET(s, entry->pid);
1867 SEQ_PUT_FIELD_RET(s, iter->cpu);
1868 SEQ_PUT_FIELD_RET(s, iter->ts);
1871 event = ftrace_find_event(entry->type);
1872 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1875 static int trace_empty(struct trace_iterator *iter)
1877 int cpu;
1879 /* If we are looking at one CPU buffer, only check that one */
1880 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1881 cpu = iter->cpu_file;
1882 if (iter->buffer_iter[cpu]) {
1883 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1884 return 0;
1885 } else {
1886 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1887 return 0;
1889 return 1;
1892 for_each_tracing_cpu(cpu) {
1893 if (iter->buffer_iter[cpu]) {
1894 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1895 return 0;
1896 } else {
1897 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1898 return 0;
1902 return 1;
1905 /* Called with trace_event_read_lock() held. */
1906 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1908 enum print_line_t ret;
1910 if (iter->trace && iter->trace->print_line) {
1911 ret = iter->trace->print_line(iter);
1912 if (ret != TRACE_TYPE_UNHANDLED)
1913 return ret;
1916 if (iter->ent->type == TRACE_BPRINT &&
1917 trace_flags & TRACE_ITER_PRINTK &&
1918 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1919 return trace_print_bprintk_msg_only(iter);
1921 if (iter->ent->type == TRACE_PRINT &&
1922 trace_flags & TRACE_ITER_PRINTK &&
1923 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1924 return trace_print_printk_msg_only(iter);
1926 if (trace_flags & TRACE_ITER_BIN)
1927 return print_bin_fmt(iter);
1929 if (trace_flags & TRACE_ITER_HEX)
1930 return print_hex_fmt(iter);
1932 if (trace_flags & TRACE_ITER_RAW)
1933 return print_raw_fmt(iter);
1935 return print_trace_fmt(iter);
1938 static int s_show(struct seq_file *m, void *v)
1940 struct trace_iterator *iter = v;
1942 if (iter->ent == NULL) {
1943 if (iter->tr) {
1944 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1945 seq_puts(m, "#\n");
1947 if (iter->trace && iter->trace->print_header)
1948 iter->trace->print_header(m);
1949 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1950 /* print nothing if the buffers are empty */
1951 if (trace_empty(iter))
1952 return 0;
1953 print_trace_header(m, iter);
1954 if (!(trace_flags & TRACE_ITER_VERBOSE))
1955 print_lat_help_header(m);
1956 } else {
1957 if (!(trace_flags & TRACE_ITER_VERBOSE))
1958 print_func_help_header(m);
1960 } else {
1961 print_trace_line(iter);
1962 trace_print_seq(m, &iter->seq);
1965 return 0;
1968 static const struct seq_operations tracer_seq_ops = {
1969 .start = s_start,
1970 .next = s_next,
1971 .stop = s_stop,
1972 .show = s_show,
1975 static struct trace_iterator *
1976 __tracing_open(struct inode *inode, struct file *file)
1978 long cpu_file = (long) inode->i_private;
1979 void *fail_ret = ERR_PTR(-ENOMEM);
1980 struct trace_iterator *iter;
1981 struct seq_file *m;
1982 int cpu, ret;
1984 if (tracing_disabled)
1985 return ERR_PTR(-ENODEV);
1987 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1988 if (!iter)
1989 return ERR_PTR(-ENOMEM);
1992 * We make a copy of the current tracer to avoid concurrent
1993 * changes on it while we are reading.
1995 mutex_lock(&trace_types_lock);
1996 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1997 if (!iter->trace)
1998 goto fail;
2000 if (current_trace)
2001 *iter->trace = *current_trace;
2003 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2004 goto fail;
2006 if (current_trace && current_trace->print_max)
2007 iter->tr = &max_tr;
2008 else
2009 iter->tr = &global_trace;
2010 iter->pos = -1;
2011 mutex_init(&iter->mutex);
2012 iter->cpu_file = cpu_file;
2014 /* Notify the tracer early; before we stop tracing. */
2015 if (iter->trace && iter->trace->open)
2016 iter->trace->open(iter);
2018 /* Annotate start of buffers if we had overruns */
2019 if (ring_buffer_overruns(iter->tr->buffer))
2020 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2022 /* stop the trace while dumping */
2023 tracing_stop();
2025 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2026 for_each_tracing_cpu(cpu) {
2028 iter->buffer_iter[cpu] =
2029 ring_buffer_read_start(iter->tr->buffer, cpu);
2030 tracing_iter_reset(iter, cpu);
2032 } else {
2033 cpu = iter->cpu_file;
2034 iter->buffer_iter[cpu] =
2035 ring_buffer_read_start(iter->tr->buffer, cpu);
2036 tracing_iter_reset(iter, cpu);
2039 ret = seq_open(file, &tracer_seq_ops);
2040 if (ret < 0) {
2041 fail_ret = ERR_PTR(ret);
2042 goto fail_buffer;
2045 m = file->private_data;
2046 m->private = iter;
2048 mutex_unlock(&trace_types_lock);
2050 return iter;
2052 fail_buffer:
2053 for_each_tracing_cpu(cpu) {
2054 if (iter->buffer_iter[cpu])
2055 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2057 free_cpumask_var(iter->started);
2058 tracing_start();
2059 fail:
2060 mutex_unlock(&trace_types_lock);
2061 kfree(iter->trace);
2062 kfree(iter);
2064 return fail_ret;
2067 int tracing_open_generic(struct inode *inode, struct file *filp)
2069 if (tracing_disabled)
2070 return -ENODEV;
2072 filp->private_data = inode->i_private;
2073 return 0;
2076 static int tracing_release(struct inode *inode, struct file *file)
2078 struct seq_file *m = (struct seq_file *)file->private_data;
2079 struct trace_iterator *iter;
2080 int cpu;
2082 if (!(file->f_mode & FMODE_READ))
2083 return 0;
2085 iter = m->private;
2087 mutex_lock(&trace_types_lock);
2088 for_each_tracing_cpu(cpu) {
2089 if (iter->buffer_iter[cpu])
2090 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2093 if (iter->trace && iter->trace->close)
2094 iter->trace->close(iter);
2096 /* reenable tracing if it was previously enabled */
2097 tracing_start();
2098 mutex_unlock(&trace_types_lock);
2100 seq_release(inode, file);
2101 mutex_destroy(&iter->mutex);
2102 free_cpumask_var(iter->started);
2103 kfree(iter->trace);
2104 kfree(iter);
2105 return 0;
2108 static int tracing_open(struct inode *inode, struct file *file)
2110 struct trace_iterator *iter;
2111 int ret = 0;
2113 /* If this file was open for write, then erase contents */
2114 if ((file->f_mode & FMODE_WRITE) &&
2115 (file->f_flags & O_TRUNC)) {
2116 long cpu = (long) inode->i_private;
2118 if (cpu == TRACE_PIPE_ALL_CPU)
2119 tracing_reset_online_cpus(&global_trace);
2120 else
2121 tracing_reset(&global_trace, cpu);
2124 if (file->f_mode & FMODE_READ) {
2125 iter = __tracing_open(inode, file);
2126 if (IS_ERR(iter))
2127 ret = PTR_ERR(iter);
2128 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2129 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2131 return ret;
2134 static void *
2135 t_next(struct seq_file *m, void *v, loff_t *pos)
2137 struct tracer *t = v;
2139 (*pos)++;
2141 if (t)
2142 t = t->next;
2144 return t;
2147 static void *t_start(struct seq_file *m, loff_t *pos)
2149 struct tracer *t;
2150 loff_t l = 0;
2152 mutex_lock(&trace_types_lock);
2153 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2156 return t;
2159 static void t_stop(struct seq_file *m, void *p)
2161 mutex_unlock(&trace_types_lock);
2164 static int t_show(struct seq_file *m, void *v)
2166 struct tracer *t = v;
2168 if (!t)
2169 return 0;
2171 seq_printf(m, "%s", t->name);
2172 if (t->next)
2173 seq_putc(m, ' ');
2174 else
2175 seq_putc(m, '\n');
2177 return 0;
2180 static const struct seq_operations show_traces_seq_ops = {
2181 .start = t_start,
2182 .next = t_next,
2183 .stop = t_stop,
2184 .show = t_show,
2187 static int show_traces_open(struct inode *inode, struct file *file)
2189 if (tracing_disabled)
2190 return -ENODEV;
2192 return seq_open(file, &show_traces_seq_ops);
2195 static ssize_t
2196 tracing_write_stub(struct file *filp, const char __user *ubuf,
2197 size_t count, loff_t *ppos)
2199 return count;
2202 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2204 if (file->f_mode & FMODE_READ)
2205 return seq_lseek(file, offset, origin);
2206 else
2207 return 0;
2210 static const struct file_operations tracing_fops = {
2211 .open = tracing_open,
2212 .read = seq_read,
2213 .write = tracing_write_stub,
2214 .llseek = tracing_seek,
2215 .release = tracing_release,
2218 static const struct file_operations show_traces_fops = {
2219 .open = show_traces_open,
2220 .read = seq_read,
2221 .release = seq_release,
2225 * Only trace on a CPU if the bitmask is set:
2227 static cpumask_var_t tracing_cpumask;
2230 * The tracer itself will not take this lock, but still we want
2231 * to provide a consistent cpumask to user-space:
2233 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2236 * Temporary storage for the character representation of the
2237 * CPU bitmask (and one more byte for the newline):
2239 static char mask_str[NR_CPUS + 1];
2241 static ssize_t
2242 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2243 size_t count, loff_t *ppos)
2245 int len;
2247 mutex_lock(&tracing_cpumask_update_lock);
2249 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2250 if (count - len < 2) {
2251 count = -EINVAL;
2252 goto out_err;
2254 len += sprintf(mask_str + len, "\n");
2255 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2257 out_err:
2258 mutex_unlock(&tracing_cpumask_update_lock);
2260 return count;
2263 static ssize_t
2264 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2265 size_t count, loff_t *ppos)
2267 int err, cpu;
2268 cpumask_var_t tracing_cpumask_new;
2270 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2271 return -ENOMEM;
2273 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2274 if (err)
2275 goto err_unlock;
2277 mutex_lock(&tracing_cpumask_update_lock);
2279 local_irq_disable();
2280 __raw_spin_lock(&ftrace_max_lock);
2281 for_each_tracing_cpu(cpu) {
2283 * Increase/decrease the disabled counter if we are
2284 * about to flip a bit in the cpumask:
2286 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2287 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2288 atomic_inc(&global_trace.data[cpu]->disabled);
2290 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2291 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2292 atomic_dec(&global_trace.data[cpu]->disabled);
2295 __raw_spin_unlock(&ftrace_max_lock);
2296 local_irq_enable();
2298 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2300 mutex_unlock(&tracing_cpumask_update_lock);
2301 free_cpumask_var(tracing_cpumask_new);
2303 return count;
2305 err_unlock:
2306 free_cpumask_var(tracing_cpumask_new);
2308 return err;
2311 static const struct file_operations tracing_cpumask_fops = {
2312 .open = tracing_open_generic,
2313 .read = tracing_cpumask_read,
2314 .write = tracing_cpumask_write,
2317 static ssize_t
2318 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2319 size_t cnt, loff_t *ppos)
2321 struct tracer_opt *trace_opts;
2322 u32 tracer_flags;
2323 int len = 0;
2324 char *buf;
2325 int r = 0;
2326 int i;
2329 /* calculate max size */
2330 for (i = 0; trace_options[i]; i++) {
2331 len += strlen(trace_options[i]);
2332 len += 3; /* "no" and newline */
2335 mutex_lock(&trace_types_lock);
2336 tracer_flags = current_trace->flags->val;
2337 trace_opts = current_trace->flags->opts;
2340 * Increase the size with names of options specific
2341 * of the current tracer.
2343 for (i = 0; trace_opts[i].name; i++) {
2344 len += strlen(trace_opts[i].name);
2345 len += 3; /* "no" and newline */
2348 /* +1 for \0 */
2349 buf = kmalloc(len + 1, GFP_KERNEL);
2350 if (!buf) {
2351 mutex_unlock(&trace_types_lock);
2352 return -ENOMEM;
2355 for (i = 0; trace_options[i]; i++) {
2356 if (trace_flags & (1 << i))
2357 r += sprintf(buf + r, "%s\n", trace_options[i]);
2358 else
2359 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2362 for (i = 0; trace_opts[i].name; i++) {
2363 if (tracer_flags & trace_opts[i].bit)
2364 r += sprintf(buf + r, "%s\n",
2365 trace_opts[i].name);
2366 else
2367 r += sprintf(buf + r, "no%s\n",
2368 trace_opts[i].name);
2370 mutex_unlock(&trace_types_lock);
2372 WARN_ON(r >= len + 1);
2374 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2376 kfree(buf);
2377 return r;
2380 /* Try to assign a tracer specific option */
2381 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2383 struct tracer_flags *tracer_flags = trace->flags;
2384 struct tracer_opt *opts = NULL;
2385 int ret = 0, i = 0;
2386 int len;
2388 for (i = 0; tracer_flags->opts[i].name; i++) {
2389 opts = &tracer_flags->opts[i];
2390 len = strlen(opts->name);
2392 if (strncmp(cmp, opts->name, len) == 0) {
2393 ret = trace->set_flag(tracer_flags->val,
2394 opts->bit, !neg);
2395 break;
2398 /* Not found */
2399 if (!tracer_flags->opts[i].name)
2400 return -EINVAL;
2402 /* Refused to handle */
2403 if (ret)
2404 return ret;
2406 if (neg)
2407 tracer_flags->val &= ~opts->bit;
2408 else
2409 tracer_flags->val |= opts->bit;
2411 return 0;
2414 static void set_tracer_flags(unsigned int mask, int enabled)
2416 /* do nothing if flag is already set */
2417 if (!!(trace_flags & mask) == !!enabled)
2418 return;
2420 if (enabled)
2421 trace_flags |= mask;
2422 else
2423 trace_flags &= ~mask;
2426 static ssize_t
2427 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2428 size_t cnt, loff_t *ppos)
2430 char buf[64];
2431 char *cmp = buf;
2432 int neg = 0;
2433 int ret;
2434 int i;
2436 if (cnt >= sizeof(buf))
2437 return -EINVAL;
2439 if (copy_from_user(&buf, ubuf, cnt))
2440 return -EFAULT;
2442 buf[cnt] = 0;
2444 if (strncmp(buf, "no", 2) == 0) {
2445 neg = 1;
2446 cmp += 2;
2449 for (i = 0; trace_options[i]; i++) {
2450 int len = strlen(trace_options[i]);
2452 if (strncmp(cmp, trace_options[i], len) == 0) {
2453 set_tracer_flags(1 << i, !neg);
2454 break;
2458 /* If no option could be set, test the specific tracer options */
2459 if (!trace_options[i]) {
2460 mutex_lock(&trace_types_lock);
2461 ret = set_tracer_option(current_trace, cmp, neg);
2462 mutex_unlock(&trace_types_lock);
2463 if (ret)
2464 return ret;
2467 *ppos += cnt;
2469 return cnt;
2472 static const struct file_operations tracing_iter_fops = {
2473 .open = tracing_open_generic,
2474 .read = tracing_trace_options_read,
2475 .write = tracing_trace_options_write,
2478 static const char readme_msg[] =
2479 "tracing mini-HOWTO:\n\n"
2480 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2481 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2482 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2483 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2484 "nop\n"
2485 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2486 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2487 "sched_switch\n"
2488 "# cat /sys/kernel/debug/tracing/trace_options\n"
2489 "noprint-parent nosym-offset nosym-addr noverbose\n"
2490 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2491 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2492 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2493 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2496 static ssize_t
2497 tracing_readme_read(struct file *filp, char __user *ubuf,
2498 size_t cnt, loff_t *ppos)
2500 return simple_read_from_buffer(ubuf, cnt, ppos,
2501 readme_msg, strlen(readme_msg));
2504 static const struct file_operations tracing_readme_fops = {
2505 .open = tracing_open_generic,
2506 .read = tracing_readme_read,
2509 static ssize_t
2510 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2511 size_t cnt, loff_t *ppos)
2513 char *buf_comm;
2514 char *file_buf;
2515 char *buf;
2516 int len = 0;
2517 int pid;
2518 int i;
2520 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2521 if (!file_buf)
2522 return -ENOMEM;
2524 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2525 if (!buf_comm) {
2526 kfree(file_buf);
2527 return -ENOMEM;
2530 buf = file_buf;
2532 for (i = 0; i < SAVED_CMDLINES; i++) {
2533 int r;
2535 pid = map_cmdline_to_pid[i];
2536 if (pid == -1 || pid == NO_CMDLINE_MAP)
2537 continue;
2539 trace_find_cmdline(pid, buf_comm);
2540 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2541 buf += r;
2542 len += r;
2545 len = simple_read_from_buffer(ubuf, cnt, ppos,
2546 file_buf, len);
2548 kfree(file_buf);
2549 kfree(buf_comm);
2551 return len;
2554 static const struct file_operations tracing_saved_cmdlines_fops = {
2555 .open = tracing_open_generic,
2556 .read = tracing_saved_cmdlines_read,
2559 static ssize_t
2560 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2561 size_t cnt, loff_t *ppos)
2563 char buf[64];
2564 int r;
2566 r = sprintf(buf, "%u\n", tracer_enabled);
2567 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2570 static ssize_t
2571 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2572 size_t cnt, loff_t *ppos)
2574 struct trace_array *tr = filp->private_data;
2575 char buf[64];
2576 unsigned long val;
2577 int ret;
2579 if (cnt >= sizeof(buf))
2580 return -EINVAL;
2582 if (copy_from_user(&buf, ubuf, cnt))
2583 return -EFAULT;
2585 buf[cnt] = 0;
2587 ret = strict_strtoul(buf, 10, &val);
2588 if (ret < 0)
2589 return ret;
2591 val = !!val;
2593 mutex_lock(&trace_types_lock);
2594 if (tracer_enabled ^ val) {
2595 if (val) {
2596 tracer_enabled = 1;
2597 if (current_trace->start)
2598 current_trace->start(tr);
2599 tracing_start();
2600 } else {
2601 tracer_enabled = 0;
2602 tracing_stop();
2603 if (current_trace->stop)
2604 current_trace->stop(tr);
2607 mutex_unlock(&trace_types_lock);
2609 *ppos += cnt;
2611 return cnt;
2614 static ssize_t
2615 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2616 size_t cnt, loff_t *ppos)
2618 char buf[MAX_TRACER_SIZE+2];
2619 int r;
2621 mutex_lock(&trace_types_lock);
2622 if (current_trace)
2623 r = sprintf(buf, "%s\n", current_trace->name);
2624 else
2625 r = sprintf(buf, "\n");
2626 mutex_unlock(&trace_types_lock);
2628 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2631 int tracer_init(struct tracer *t, struct trace_array *tr)
2633 tracing_reset_online_cpus(tr);
2634 return t->init(tr);
2637 static int tracing_resize_ring_buffer(unsigned long size)
2639 int ret;
2642 * If kernel or user changes the size of the ring buffer
2643 * we use the size that was given, and we can forget about
2644 * expanding it later.
2646 ring_buffer_expanded = 1;
2648 ret = ring_buffer_resize(global_trace.buffer, size);
2649 if (ret < 0)
2650 return ret;
2652 ret = ring_buffer_resize(max_tr.buffer, size);
2653 if (ret < 0) {
2654 int r;
2656 r = ring_buffer_resize(global_trace.buffer,
2657 global_trace.entries);
2658 if (r < 0) {
2660 * AARGH! We are left with different
2661 * size max buffer!!!!
2662 * The max buffer is our "snapshot" buffer.
2663 * When a tracer needs a snapshot (one of the
2664 * latency tracers), it swaps the max buffer
2665 * with the saved snap shot. We succeeded to
2666 * update the size of the main buffer, but failed to
2667 * update the size of the max buffer. But when we tried
2668 * to reset the main buffer to the original size, we
2669 * failed there too. This is very unlikely to
2670 * happen, but if it does, warn and kill all
2671 * tracing.
2673 WARN_ON(1);
2674 tracing_disabled = 1;
2676 return ret;
2679 global_trace.entries = size;
2681 return ret;
2685 * tracing_update_buffers - used by tracing facility to expand ring buffers
2687 * To save on memory when the tracing is never used on a system with it
2688 * configured in. The ring buffers are set to a minimum size. But once
2689 * a user starts to use the tracing facility, then they need to grow
2690 * to their default size.
2692 * This function is to be called when a tracer is about to be used.
2694 int tracing_update_buffers(void)
2696 int ret = 0;
2698 mutex_lock(&trace_types_lock);
2699 if (!ring_buffer_expanded)
2700 ret = tracing_resize_ring_buffer(trace_buf_size);
2701 mutex_unlock(&trace_types_lock);
2703 return ret;
2706 struct trace_option_dentry;
2708 static struct trace_option_dentry *
2709 create_trace_option_files(struct tracer *tracer);
2711 static void
2712 destroy_trace_option_files(struct trace_option_dentry *topts);
2714 static int tracing_set_tracer(const char *buf)
2716 static struct trace_option_dentry *topts;
2717 struct trace_array *tr = &global_trace;
2718 struct tracer *t;
2719 int ret = 0;
2721 mutex_lock(&trace_types_lock);
2723 if (!ring_buffer_expanded) {
2724 ret = tracing_resize_ring_buffer(trace_buf_size);
2725 if (ret < 0)
2726 goto out;
2727 ret = 0;
2730 for (t = trace_types; t; t = t->next) {
2731 if (strcmp(t->name, buf) == 0)
2732 break;
2734 if (!t) {
2735 ret = -EINVAL;
2736 goto out;
2738 if (t == current_trace)
2739 goto out;
2741 trace_branch_disable();
2742 if (current_trace && current_trace->reset)
2743 current_trace->reset(tr);
2745 destroy_trace_option_files(topts);
2747 current_trace = t;
2749 topts = create_trace_option_files(current_trace);
2751 if (t->init) {
2752 ret = tracer_init(t, tr);
2753 if (ret)
2754 goto out;
2757 trace_branch_enable(tr);
2758 out:
2759 mutex_unlock(&trace_types_lock);
2761 return ret;
2764 static ssize_t
2765 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2766 size_t cnt, loff_t *ppos)
2768 char buf[MAX_TRACER_SIZE+1];
2769 int i;
2770 size_t ret;
2771 int err;
2773 ret = cnt;
2775 if (cnt > MAX_TRACER_SIZE)
2776 cnt = MAX_TRACER_SIZE;
2778 if (copy_from_user(&buf, ubuf, cnt))
2779 return -EFAULT;
2781 buf[cnt] = 0;
2783 /* strip ending whitespace. */
2784 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2785 buf[i] = 0;
2787 err = tracing_set_tracer(buf);
2788 if (err)
2789 return err;
2791 *ppos += ret;
2793 return ret;
2796 static ssize_t
2797 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2798 size_t cnt, loff_t *ppos)
2800 unsigned long *ptr = filp->private_data;
2801 char buf[64];
2802 int r;
2804 r = snprintf(buf, sizeof(buf), "%ld\n",
2805 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2806 if (r > sizeof(buf))
2807 r = sizeof(buf);
2808 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2811 static ssize_t
2812 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2813 size_t cnt, loff_t *ppos)
2815 unsigned long *ptr = filp->private_data;
2816 char buf[64];
2817 unsigned long val;
2818 int ret;
2820 if (cnt >= sizeof(buf))
2821 return -EINVAL;
2823 if (copy_from_user(&buf, ubuf, cnt))
2824 return -EFAULT;
2826 buf[cnt] = 0;
2828 ret = strict_strtoul(buf, 10, &val);
2829 if (ret < 0)
2830 return ret;
2832 *ptr = val * 1000;
2834 return cnt;
2837 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2839 long cpu_file = (long) inode->i_private;
2840 struct trace_iterator *iter;
2841 int ret = 0;
2843 if (tracing_disabled)
2844 return -ENODEV;
2846 mutex_lock(&trace_types_lock);
2848 /* We only allow one reader per cpu */
2849 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2850 if (!cpumask_empty(tracing_reader_cpumask)) {
2851 ret = -EBUSY;
2852 goto out;
2854 cpumask_setall(tracing_reader_cpumask);
2855 } else {
2856 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2857 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2858 else {
2859 ret = -EBUSY;
2860 goto out;
2864 /* create a buffer to store the information to pass to userspace */
2865 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2866 if (!iter) {
2867 ret = -ENOMEM;
2868 goto out;
2872 * We make a copy of the current tracer to avoid concurrent
2873 * changes on it while we are reading.
2875 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2876 if (!iter->trace) {
2877 ret = -ENOMEM;
2878 goto fail;
2880 if (current_trace)
2881 *iter->trace = *current_trace;
2883 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2884 ret = -ENOMEM;
2885 goto fail;
2888 /* trace pipe does not show start of buffer */
2889 cpumask_setall(iter->started);
2891 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2892 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2894 iter->cpu_file = cpu_file;
2895 iter->tr = &global_trace;
2896 mutex_init(&iter->mutex);
2897 filp->private_data = iter;
2899 if (iter->trace->pipe_open)
2900 iter->trace->pipe_open(iter);
2902 out:
2903 mutex_unlock(&trace_types_lock);
2904 return ret;
2906 fail:
2907 kfree(iter->trace);
2908 kfree(iter);
2909 mutex_unlock(&trace_types_lock);
2910 return ret;
2913 static int tracing_release_pipe(struct inode *inode, struct file *file)
2915 struct trace_iterator *iter = file->private_data;
2917 mutex_lock(&trace_types_lock);
2919 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2920 cpumask_clear(tracing_reader_cpumask);
2921 else
2922 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2924 mutex_unlock(&trace_types_lock);
2926 free_cpumask_var(iter->started);
2927 mutex_destroy(&iter->mutex);
2928 kfree(iter->trace);
2929 kfree(iter);
2931 return 0;
2934 static unsigned int
2935 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2937 struct trace_iterator *iter = filp->private_data;
2939 if (trace_flags & TRACE_ITER_BLOCK) {
2941 * Always select as readable when in blocking mode
2943 return POLLIN | POLLRDNORM;
2944 } else {
2945 if (!trace_empty(iter))
2946 return POLLIN | POLLRDNORM;
2947 poll_wait(filp, &trace_wait, poll_table);
2948 if (!trace_empty(iter))
2949 return POLLIN | POLLRDNORM;
2951 return 0;
2956 void default_wait_pipe(struct trace_iterator *iter)
2958 DEFINE_WAIT(wait);
2960 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2962 if (trace_empty(iter))
2963 schedule();
2965 finish_wait(&trace_wait, &wait);
2969 * This is a make-shift waitqueue.
2970 * A tracer might use this callback on some rare cases:
2972 * 1) the current tracer might hold the runqueue lock when it wakes up
2973 * a reader, hence a deadlock (sched, function, and function graph tracers)
2974 * 2) the function tracers, trace all functions, we don't want
2975 * the overhead of calling wake_up and friends
2976 * (and tracing them too)
2978 * Anyway, this is really very primitive wakeup.
2980 void poll_wait_pipe(struct trace_iterator *iter)
2982 set_current_state(TASK_INTERRUPTIBLE);
2983 /* sleep for 100 msecs, and try again. */
2984 schedule_timeout(HZ / 10);
2987 /* Must be called with trace_types_lock mutex held. */
2988 static int tracing_wait_pipe(struct file *filp)
2990 struct trace_iterator *iter = filp->private_data;
2992 while (trace_empty(iter)) {
2994 if ((filp->f_flags & O_NONBLOCK)) {
2995 return -EAGAIN;
2998 mutex_unlock(&iter->mutex);
3000 iter->trace->wait_pipe(iter);
3002 mutex_lock(&iter->mutex);
3004 if (signal_pending(current))
3005 return -EINTR;
3008 * We block until we read something and tracing is disabled.
3009 * We still block if tracing is disabled, but we have never
3010 * read anything. This allows a user to cat this file, and
3011 * then enable tracing. But after we have read something,
3012 * we give an EOF when tracing is again disabled.
3014 * iter->pos will be 0 if we haven't read anything.
3016 if (!tracer_enabled && iter->pos)
3017 break;
3020 return 1;
3024 * Consumer reader.
3026 static ssize_t
3027 tracing_read_pipe(struct file *filp, char __user *ubuf,
3028 size_t cnt, loff_t *ppos)
3030 struct trace_iterator *iter = filp->private_data;
3031 static struct tracer *old_tracer;
3032 ssize_t sret;
3034 /* return any leftover data */
3035 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3036 if (sret != -EBUSY)
3037 return sret;
3039 trace_seq_init(&iter->seq);
3041 /* copy the tracer to avoid using a global lock all around */
3042 mutex_lock(&trace_types_lock);
3043 if (unlikely(old_tracer != current_trace && current_trace)) {
3044 old_tracer = current_trace;
3045 *iter->trace = *current_trace;
3047 mutex_unlock(&trace_types_lock);
3050 * Avoid more than one consumer on a single file descriptor
3051 * This is just a matter of traces coherency, the ring buffer itself
3052 * is protected.
3054 mutex_lock(&iter->mutex);
3055 if (iter->trace->read) {
3056 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3057 if (sret)
3058 goto out;
3061 waitagain:
3062 sret = tracing_wait_pipe(filp);
3063 if (sret <= 0)
3064 goto out;
3066 /* stop when tracing is finished */
3067 if (trace_empty(iter)) {
3068 sret = 0;
3069 goto out;
3072 if (cnt >= PAGE_SIZE)
3073 cnt = PAGE_SIZE - 1;
3075 /* reset all but tr, trace, and overruns */
3076 memset(&iter->seq, 0,
3077 sizeof(struct trace_iterator) -
3078 offsetof(struct trace_iterator, seq));
3079 iter->pos = -1;
3081 trace_event_read_lock();
3082 while (find_next_entry_inc(iter) != NULL) {
3083 enum print_line_t ret;
3084 int len = iter->seq.len;
3086 ret = print_trace_line(iter);
3087 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3088 /* don't print partial lines */
3089 iter->seq.len = len;
3090 break;
3092 if (ret != TRACE_TYPE_NO_CONSUME)
3093 trace_consume(iter);
3095 if (iter->seq.len >= cnt)
3096 break;
3098 trace_event_read_unlock();
3100 /* Now copy what we have to the user */
3101 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3102 if (iter->seq.readpos >= iter->seq.len)
3103 trace_seq_init(&iter->seq);
3106 * If there was nothing to send to user, inspite of consuming trace
3107 * entries, go back to wait for more entries.
3109 if (sret == -EBUSY)
3110 goto waitagain;
3112 out:
3113 mutex_unlock(&iter->mutex);
3115 return sret;
3118 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3119 struct pipe_buffer *buf)
3121 __free_page(buf->page);
3124 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3125 unsigned int idx)
3127 __free_page(spd->pages[idx]);
3130 static struct pipe_buf_operations tracing_pipe_buf_ops = {
3131 .can_merge = 0,
3132 .map = generic_pipe_buf_map,
3133 .unmap = generic_pipe_buf_unmap,
3134 .confirm = generic_pipe_buf_confirm,
3135 .release = tracing_pipe_buf_release,
3136 .steal = generic_pipe_buf_steal,
3137 .get = generic_pipe_buf_get,
3140 static size_t
3141 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3143 size_t count;
3144 int ret;
3146 /* Seq buffer is page-sized, exactly what we need. */
3147 for (;;) {
3148 count = iter->seq.len;
3149 ret = print_trace_line(iter);
3150 count = iter->seq.len - count;
3151 if (rem < count) {
3152 rem = 0;
3153 iter->seq.len -= count;
3154 break;
3156 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3157 iter->seq.len -= count;
3158 break;
3161 if (ret != TRACE_TYPE_NO_CONSUME)
3162 trace_consume(iter);
3163 rem -= count;
3164 if (!find_next_entry_inc(iter)) {
3165 rem = 0;
3166 iter->ent = NULL;
3167 break;
3171 return rem;
3174 static ssize_t tracing_splice_read_pipe(struct file *filp,
3175 loff_t *ppos,
3176 struct pipe_inode_info *pipe,
3177 size_t len,
3178 unsigned int flags)
3180 struct page *pages[PIPE_BUFFERS];
3181 struct partial_page partial[PIPE_BUFFERS];
3182 struct trace_iterator *iter = filp->private_data;
3183 struct splice_pipe_desc spd = {
3184 .pages = pages,
3185 .partial = partial,
3186 .nr_pages = 0, /* This gets updated below. */
3187 .flags = flags,
3188 .ops = &tracing_pipe_buf_ops,
3189 .spd_release = tracing_spd_release_pipe,
3191 static struct tracer *old_tracer;
3192 ssize_t ret;
3193 size_t rem;
3194 unsigned int i;
3196 /* copy the tracer to avoid using a global lock all around */
3197 mutex_lock(&trace_types_lock);
3198 if (unlikely(old_tracer != current_trace && current_trace)) {
3199 old_tracer = current_trace;
3200 *iter->trace = *current_trace;
3202 mutex_unlock(&trace_types_lock);
3204 mutex_lock(&iter->mutex);
3206 if (iter->trace->splice_read) {
3207 ret = iter->trace->splice_read(iter, filp,
3208 ppos, pipe, len, flags);
3209 if (ret)
3210 goto out_err;
3213 ret = tracing_wait_pipe(filp);
3214 if (ret <= 0)
3215 goto out_err;
3217 if (!iter->ent && !find_next_entry_inc(iter)) {
3218 ret = -EFAULT;
3219 goto out_err;
3222 trace_event_read_lock();
3224 /* Fill as many pages as possible. */
3225 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3226 pages[i] = alloc_page(GFP_KERNEL);
3227 if (!pages[i])
3228 break;
3230 rem = tracing_fill_pipe_page(rem, iter);
3232 /* Copy the data into the page, so we can start over. */
3233 ret = trace_seq_to_buffer(&iter->seq,
3234 page_address(pages[i]),
3235 iter->seq.len);
3236 if (ret < 0) {
3237 __free_page(pages[i]);
3238 break;
3240 partial[i].offset = 0;
3241 partial[i].len = iter->seq.len;
3243 trace_seq_init(&iter->seq);
3246 trace_event_read_unlock();
3247 mutex_unlock(&iter->mutex);
3249 spd.nr_pages = i;
3251 return splice_to_pipe(pipe, &spd);
3253 out_err:
3254 mutex_unlock(&iter->mutex);
3256 return ret;
3259 static ssize_t
3260 tracing_entries_read(struct file *filp, char __user *ubuf,
3261 size_t cnt, loff_t *ppos)
3263 struct trace_array *tr = filp->private_data;
3264 char buf[96];
3265 int r;
3267 mutex_lock(&trace_types_lock);
3268 if (!ring_buffer_expanded)
3269 r = sprintf(buf, "%lu (expanded: %lu)\n",
3270 tr->entries >> 10,
3271 trace_buf_size >> 10);
3272 else
3273 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3274 mutex_unlock(&trace_types_lock);
3276 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3279 static ssize_t
3280 tracing_entries_write(struct file *filp, const char __user *ubuf,
3281 size_t cnt, loff_t *ppos)
3283 unsigned long val;
3284 char buf[64];
3285 int ret, cpu;
3287 if (cnt >= sizeof(buf))
3288 return -EINVAL;
3290 if (copy_from_user(&buf, ubuf, cnt))
3291 return -EFAULT;
3293 buf[cnt] = 0;
3295 ret = strict_strtoul(buf, 10, &val);
3296 if (ret < 0)
3297 return ret;
3299 /* must have at least 1 entry */
3300 if (!val)
3301 return -EINVAL;
3303 mutex_lock(&trace_types_lock);
3305 tracing_stop();
3307 /* disable all cpu buffers */
3308 for_each_tracing_cpu(cpu) {
3309 if (global_trace.data[cpu])
3310 atomic_inc(&global_trace.data[cpu]->disabled);
3311 if (max_tr.data[cpu])
3312 atomic_inc(&max_tr.data[cpu]->disabled);
3315 /* value is in KB */
3316 val <<= 10;
3318 if (val != global_trace.entries) {
3319 ret = tracing_resize_ring_buffer(val);
3320 if (ret < 0) {
3321 cnt = ret;
3322 goto out;
3326 *ppos += cnt;
3328 /* If check pages failed, return ENOMEM */
3329 if (tracing_disabled)
3330 cnt = -ENOMEM;
3331 out:
3332 for_each_tracing_cpu(cpu) {
3333 if (global_trace.data[cpu])
3334 atomic_dec(&global_trace.data[cpu]->disabled);
3335 if (max_tr.data[cpu])
3336 atomic_dec(&max_tr.data[cpu]->disabled);
3339 tracing_start();
3340 max_tr.entries = global_trace.entries;
3341 mutex_unlock(&trace_types_lock);
3343 return cnt;
3346 static int mark_printk(const char *fmt, ...)
3348 int ret;
3349 va_list args;
3350 va_start(args, fmt);
3351 ret = trace_vprintk(0, fmt, args);
3352 va_end(args);
3353 return ret;
3356 static ssize_t
3357 tracing_mark_write(struct file *filp, const char __user *ubuf,
3358 size_t cnt, loff_t *fpos)
3360 char *buf;
3361 char *end;
3363 if (tracing_disabled)
3364 return -EINVAL;
3366 if (cnt > TRACE_BUF_SIZE)
3367 cnt = TRACE_BUF_SIZE;
3369 buf = kmalloc(cnt + 1, GFP_KERNEL);
3370 if (buf == NULL)
3371 return -ENOMEM;
3373 if (copy_from_user(buf, ubuf, cnt)) {
3374 kfree(buf);
3375 return -EFAULT;
3378 /* Cut from the first nil or newline. */
3379 buf[cnt] = '\0';
3380 end = strchr(buf, '\n');
3381 if (end)
3382 *end = '\0';
3384 cnt = mark_printk("%s\n", buf);
3385 kfree(buf);
3386 *fpos += cnt;
3388 return cnt;
3391 static ssize_t tracing_clock_read(struct file *filp, char __user *ubuf,
3392 size_t cnt, loff_t *ppos)
3394 char buf[64];
3395 int bufiter = 0;
3396 int i;
3398 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3399 bufiter += snprintf(buf + bufiter, sizeof(buf) - bufiter,
3400 "%s%s%s%s", i ? " " : "",
3401 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3402 i == trace_clock_id ? "]" : "");
3403 bufiter += snprintf(buf + bufiter, sizeof(buf) - bufiter, "\n");
3405 return simple_read_from_buffer(ubuf, cnt, ppos, buf, bufiter);
3408 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3409 size_t cnt, loff_t *fpos)
3411 char buf[64];
3412 const char *clockstr;
3413 int i;
3415 if (cnt >= sizeof(buf))
3416 return -EINVAL;
3418 if (copy_from_user(&buf, ubuf, cnt))
3419 return -EFAULT;
3421 buf[cnt] = 0;
3423 clockstr = strstrip(buf);
3425 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3426 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3427 break;
3429 if (i == ARRAY_SIZE(trace_clocks))
3430 return -EINVAL;
3432 trace_clock_id = i;
3434 mutex_lock(&trace_types_lock);
3436 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3437 if (max_tr.buffer)
3438 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3440 mutex_unlock(&trace_types_lock);
3442 *fpos += cnt;
3444 return cnt;
3447 static const struct file_operations tracing_max_lat_fops = {
3448 .open = tracing_open_generic,
3449 .read = tracing_max_lat_read,
3450 .write = tracing_max_lat_write,
3453 static const struct file_operations tracing_ctrl_fops = {
3454 .open = tracing_open_generic,
3455 .read = tracing_ctrl_read,
3456 .write = tracing_ctrl_write,
3459 static const struct file_operations set_tracer_fops = {
3460 .open = tracing_open_generic,
3461 .read = tracing_set_trace_read,
3462 .write = tracing_set_trace_write,
3465 static const struct file_operations tracing_pipe_fops = {
3466 .open = tracing_open_pipe,
3467 .poll = tracing_poll_pipe,
3468 .read = tracing_read_pipe,
3469 .splice_read = tracing_splice_read_pipe,
3470 .release = tracing_release_pipe,
3473 static const struct file_operations tracing_entries_fops = {
3474 .open = tracing_open_generic,
3475 .read = tracing_entries_read,
3476 .write = tracing_entries_write,
3479 static const struct file_operations tracing_mark_fops = {
3480 .open = tracing_open_generic,
3481 .write = tracing_mark_write,
3484 static const struct file_operations trace_clock_fops = {
3485 .open = tracing_open_generic,
3486 .read = tracing_clock_read,
3487 .write = tracing_clock_write,
3490 struct ftrace_buffer_info {
3491 struct trace_array *tr;
3492 void *spare;
3493 int cpu;
3494 unsigned int read;
3497 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3499 int cpu = (int)(long)inode->i_private;
3500 struct ftrace_buffer_info *info;
3502 if (tracing_disabled)
3503 return -ENODEV;
3505 info = kzalloc(sizeof(*info), GFP_KERNEL);
3506 if (!info)
3507 return -ENOMEM;
3509 info->tr = &global_trace;
3510 info->cpu = cpu;
3511 info->spare = NULL;
3512 /* Force reading ring buffer for first read */
3513 info->read = (unsigned int)-1;
3515 filp->private_data = info;
3517 return nonseekable_open(inode, filp);
3520 static ssize_t
3521 tracing_buffers_read(struct file *filp, char __user *ubuf,
3522 size_t count, loff_t *ppos)
3524 struct ftrace_buffer_info *info = filp->private_data;
3525 unsigned int pos;
3526 ssize_t ret;
3527 size_t size;
3529 if (!count)
3530 return 0;
3532 if (!info->spare)
3533 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3534 if (!info->spare)
3535 return -ENOMEM;
3537 /* Do we have previous read data to read? */
3538 if (info->read < PAGE_SIZE)
3539 goto read;
3541 info->read = 0;
3543 ret = ring_buffer_read_page(info->tr->buffer,
3544 &info->spare,
3545 count,
3546 info->cpu, 0);
3547 if (ret < 0)
3548 return 0;
3550 pos = ring_buffer_page_len(info->spare);
3552 if (pos < PAGE_SIZE)
3553 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3555 read:
3556 size = PAGE_SIZE - info->read;
3557 if (size > count)
3558 size = count;
3560 ret = copy_to_user(ubuf, info->spare + info->read, size);
3561 if (ret == size)
3562 return -EFAULT;
3563 size -= ret;
3565 *ppos += size;
3566 info->read += size;
3568 return size;
3571 static int tracing_buffers_release(struct inode *inode, struct file *file)
3573 struct ftrace_buffer_info *info = file->private_data;
3575 if (info->spare)
3576 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3577 kfree(info);
3579 return 0;
3582 struct buffer_ref {
3583 struct ring_buffer *buffer;
3584 void *page;
3585 int ref;
3588 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3589 struct pipe_buffer *buf)
3591 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3593 if (--ref->ref)
3594 return;
3596 ring_buffer_free_read_page(ref->buffer, ref->page);
3597 kfree(ref);
3598 buf->private = 0;
3601 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3602 struct pipe_buffer *buf)
3604 return 1;
3607 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3608 struct pipe_buffer *buf)
3610 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3612 ref->ref++;
3615 /* Pipe buffer operations for a buffer. */
3616 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3617 .can_merge = 0,
3618 .map = generic_pipe_buf_map,
3619 .unmap = generic_pipe_buf_unmap,
3620 .confirm = generic_pipe_buf_confirm,
3621 .release = buffer_pipe_buf_release,
3622 .steal = buffer_pipe_buf_steal,
3623 .get = buffer_pipe_buf_get,
3627 * Callback from splice_to_pipe(), if we need to release some pages
3628 * at the end of the spd in case we error'ed out in filling the pipe.
3630 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3632 struct buffer_ref *ref =
3633 (struct buffer_ref *)spd->partial[i].private;
3635 if (--ref->ref)
3636 return;
3638 ring_buffer_free_read_page(ref->buffer, ref->page);
3639 kfree(ref);
3640 spd->partial[i].private = 0;
3643 static ssize_t
3644 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3645 struct pipe_inode_info *pipe, size_t len,
3646 unsigned int flags)
3648 struct ftrace_buffer_info *info = file->private_data;
3649 struct partial_page partial[PIPE_BUFFERS];
3650 struct page *pages[PIPE_BUFFERS];
3651 struct splice_pipe_desc spd = {
3652 .pages = pages,
3653 .partial = partial,
3654 .flags = flags,
3655 .ops = &buffer_pipe_buf_ops,
3656 .spd_release = buffer_spd_release,
3658 struct buffer_ref *ref;
3659 int entries, size, i;
3660 size_t ret;
3662 if (*ppos & (PAGE_SIZE - 1)) {
3663 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3664 return -EINVAL;
3667 if (len & (PAGE_SIZE - 1)) {
3668 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3669 if (len < PAGE_SIZE)
3670 return -EINVAL;
3671 len &= PAGE_MASK;
3674 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3676 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
3677 struct page *page;
3678 int r;
3680 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3681 if (!ref)
3682 break;
3684 ref->ref = 1;
3685 ref->buffer = info->tr->buffer;
3686 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3687 if (!ref->page) {
3688 kfree(ref);
3689 break;
3692 r = ring_buffer_read_page(ref->buffer, &ref->page,
3693 len, info->cpu, 1);
3694 if (r < 0) {
3695 ring_buffer_free_read_page(ref->buffer,
3696 ref->page);
3697 kfree(ref);
3698 break;
3702 * zero out any left over data, this is going to
3703 * user land.
3705 size = ring_buffer_page_len(ref->page);
3706 if (size < PAGE_SIZE)
3707 memset(ref->page + size, 0, PAGE_SIZE - size);
3709 page = virt_to_page(ref->page);
3711 spd.pages[i] = page;
3712 spd.partial[i].len = PAGE_SIZE;
3713 spd.partial[i].offset = 0;
3714 spd.partial[i].private = (unsigned long)ref;
3715 spd.nr_pages++;
3716 *ppos += PAGE_SIZE;
3718 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3721 spd.nr_pages = i;
3723 /* did we read anything? */
3724 if (!spd.nr_pages) {
3725 if (flags & SPLICE_F_NONBLOCK)
3726 ret = -EAGAIN;
3727 else
3728 ret = 0;
3729 /* TODO: block */
3730 return ret;
3733 ret = splice_to_pipe(pipe, &spd);
3735 return ret;
3738 static const struct file_operations tracing_buffers_fops = {
3739 .open = tracing_buffers_open,
3740 .read = tracing_buffers_read,
3741 .release = tracing_buffers_release,
3742 .splice_read = tracing_buffers_splice_read,
3743 .llseek = no_llseek,
3746 static ssize_t
3747 tracing_stats_read(struct file *filp, char __user *ubuf,
3748 size_t count, loff_t *ppos)
3750 unsigned long cpu = (unsigned long)filp->private_data;
3751 struct trace_array *tr = &global_trace;
3752 struct trace_seq *s;
3753 unsigned long cnt;
3755 s = kmalloc(sizeof(*s), GFP_KERNEL);
3756 if (!s)
3757 return ENOMEM;
3759 trace_seq_init(s);
3761 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3762 trace_seq_printf(s, "entries: %ld\n", cnt);
3764 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3765 trace_seq_printf(s, "overrun: %ld\n", cnt);
3767 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3768 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3770 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3772 kfree(s);
3774 return count;
3777 static const struct file_operations tracing_stats_fops = {
3778 .open = tracing_open_generic,
3779 .read = tracing_stats_read,
3782 #ifdef CONFIG_DYNAMIC_FTRACE
3784 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3786 return 0;
3789 static ssize_t
3790 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3791 size_t cnt, loff_t *ppos)
3793 static char ftrace_dyn_info_buffer[1024];
3794 static DEFINE_MUTEX(dyn_info_mutex);
3795 unsigned long *p = filp->private_data;
3796 char *buf = ftrace_dyn_info_buffer;
3797 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3798 int r;
3800 mutex_lock(&dyn_info_mutex);
3801 r = sprintf(buf, "%ld ", *p);
3803 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3804 buf[r++] = '\n';
3806 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3808 mutex_unlock(&dyn_info_mutex);
3810 return r;
3813 static const struct file_operations tracing_dyn_info_fops = {
3814 .open = tracing_open_generic,
3815 .read = tracing_read_dyn_info,
3817 #endif
3819 static struct dentry *d_tracer;
3821 struct dentry *tracing_init_dentry(void)
3823 static int once;
3825 if (d_tracer)
3826 return d_tracer;
3828 if (!debugfs_initialized())
3829 return NULL;
3831 d_tracer = debugfs_create_dir("tracing", NULL);
3833 if (!d_tracer && !once) {
3834 once = 1;
3835 pr_warning("Could not create debugfs directory 'tracing'\n");
3836 return NULL;
3839 return d_tracer;
3842 static struct dentry *d_percpu;
3844 struct dentry *tracing_dentry_percpu(void)
3846 static int once;
3847 struct dentry *d_tracer;
3849 if (d_percpu)
3850 return d_percpu;
3852 d_tracer = tracing_init_dentry();
3854 if (!d_tracer)
3855 return NULL;
3857 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3859 if (!d_percpu && !once) {
3860 once = 1;
3861 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3862 return NULL;
3865 return d_percpu;
3868 static void tracing_init_debugfs_percpu(long cpu)
3870 struct dentry *d_percpu = tracing_dentry_percpu();
3871 struct dentry *d_cpu;
3872 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3873 char cpu_dir[7];
3875 if (cpu > 999 || cpu < 0)
3876 return;
3878 sprintf(cpu_dir, "cpu%ld", cpu);
3879 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3880 if (!d_cpu) {
3881 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3882 return;
3885 /* per cpu trace_pipe */
3886 trace_create_file("trace_pipe", 0444, d_cpu,
3887 (void *) cpu, &tracing_pipe_fops);
3889 /* per cpu trace */
3890 trace_create_file("trace", 0644, d_cpu,
3891 (void *) cpu, &tracing_fops);
3893 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3894 (void *) cpu, &tracing_buffers_fops);
3896 trace_create_file("stats", 0444, d_cpu,
3897 (void *) cpu, &tracing_stats_fops);
3900 #ifdef CONFIG_FTRACE_SELFTEST
3901 /* Let selftest have access to static functions in this file */
3902 #include "trace_selftest.c"
3903 #endif
3905 struct trace_option_dentry {
3906 struct tracer_opt *opt;
3907 struct tracer_flags *flags;
3908 struct dentry *entry;
3911 static ssize_t
3912 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3913 loff_t *ppos)
3915 struct trace_option_dentry *topt = filp->private_data;
3916 char *buf;
3918 if (topt->flags->val & topt->opt->bit)
3919 buf = "1\n";
3920 else
3921 buf = "0\n";
3923 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3926 static ssize_t
3927 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3928 loff_t *ppos)
3930 struct trace_option_dentry *topt = filp->private_data;
3931 unsigned long val;
3932 char buf[64];
3933 int ret;
3935 if (cnt >= sizeof(buf))
3936 return -EINVAL;
3938 if (copy_from_user(&buf, ubuf, cnt))
3939 return -EFAULT;
3941 buf[cnt] = 0;
3943 ret = strict_strtoul(buf, 10, &val);
3944 if (ret < 0)
3945 return ret;
3947 ret = 0;
3948 switch (val) {
3949 case 0:
3950 /* do nothing if already cleared */
3951 if (!(topt->flags->val & topt->opt->bit))
3952 break;
3954 mutex_lock(&trace_types_lock);
3955 if (current_trace->set_flag)
3956 ret = current_trace->set_flag(topt->flags->val,
3957 topt->opt->bit, 0);
3958 mutex_unlock(&trace_types_lock);
3959 if (ret)
3960 return ret;
3961 topt->flags->val &= ~topt->opt->bit;
3962 break;
3963 case 1:
3964 /* do nothing if already set */
3965 if (topt->flags->val & topt->opt->bit)
3966 break;
3968 mutex_lock(&trace_types_lock);
3969 if (current_trace->set_flag)
3970 ret = current_trace->set_flag(topt->flags->val,
3971 topt->opt->bit, 1);
3972 mutex_unlock(&trace_types_lock);
3973 if (ret)
3974 return ret;
3975 topt->flags->val |= topt->opt->bit;
3976 break;
3978 default:
3979 return -EINVAL;
3982 *ppos += cnt;
3984 return cnt;
3988 static const struct file_operations trace_options_fops = {
3989 .open = tracing_open_generic,
3990 .read = trace_options_read,
3991 .write = trace_options_write,
3994 static ssize_t
3995 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3996 loff_t *ppos)
3998 long index = (long)filp->private_data;
3999 char *buf;
4001 if (trace_flags & (1 << index))
4002 buf = "1\n";
4003 else
4004 buf = "0\n";
4006 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4009 static ssize_t
4010 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4011 loff_t *ppos)
4013 long index = (long)filp->private_data;
4014 char buf[64];
4015 unsigned long val;
4016 int ret;
4018 if (cnt >= sizeof(buf))
4019 return -EINVAL;
4021 if (copy_from_user(&buf, ubuf, cnt))
4022 return -EFAULT;
4024 buf[cnt] = 0;
4026 ret = strict_strtoul(buf, 10, &val);
4027 if (ret < 0)
4028 return ret;
4030 if (val != 0 && val != 1)
4031 return -EINVAL;
4032 set_tracer_flags(1 << index, val);
4034 *ppos += cnt;
4036 return cnt;
4039 static const struct file_operations trace_options_core_fops = {
4040 .open = tracing_open_generic,
4041 .read = trace_options_core_read,
4042 .write = trace_options_core_write,
4045 struct dentry *trace_create_file(const char *name,
4046 mode_t mode,
4047 struct dentry *parent,
4048 void *data,
4049 const struct file_operations *fops)
4051 struct dentry *ret;
4053 ret = debugfs_create_file(name, mode, parent, data, fops);
4054 if (!ret)
4055 pr_warning("Could not create debugfs '%s' entry\n", name);
4057 return ret;
4061 static struct dentry *trace_options_init_dentry(void)
4063 struct dentry *d_tracer;
4064 static struct dentry *t_options;
4066 if (t_options)
4067 return t_options;
4069 d_tracer = tracing_init_dentry();
4070 if (!d_tracer)
4071 return NULL;
4073 t_options = debugfs_create_dir("options", d_tracer);
4074 if (!t_options) {
4075 pr_warning("Could not create debugfs directory 'options'\n");
4076 return NULL;
4079 return t_options;
4082 static void
4083 create_trace_option_file(struct trace_option_dentry *topt,
4084 struct tracer_flags *flags,
4085 struct tracer_opt *opt)
4087 struct dentry *t_options;
4089 t_options = trace_options_init_dentry();
4090 if (!t_options)
4091 return;
4093 topt->flags = flags;
4094 topt->opt = opt;
4096 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4097 &trace_options_fops);
4101 static struct trace_option_dentry *
4102 create_trace_option_files(struct tracer *tracer)
4104 struct trace_option_dentry *topts;
4105 struct tracer_flags *flags;
4106 struct tracer_opt *opts;
4107 int cnt;
4109 if (!tracer)
4110 return NULL;
4112 flags = tracer->flags;
4114 if (!flags || !flags->opts)
4115 return NULL;
4117 opts = flags->opts;
4119 for (cnt = 0; opts[cnt].name; cnt++)
4122 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4123 if (!topts)
4124 return NULL;
4126 for (cnt = 0; opts[cnt].name; cnt++)
4127 create_trace_option_file(&topts[cnt], flags,
4128 &opts[cnt]);
4130 return topts;
4133 static void
4134 destroy_trace_option_files(struct trace_option_dentry *topts)
4136 int cnt;
4138 if (!topts)
4139 return;
4141 for (cnt = 0; topts[cnt].opt; cnt++) {
4142 if (topts[cnt].entry)
4143 debugfs_remove(topts[cnt].entry);
4146 kfree(topts);
4149 static struct dentry *
4150 create_trace_option_core_file(const char *option, long index)
4152 struct dentry *t_options;
4154 t_options = trace_options_init_dentry();
4155 if (!t_options)
4156 return NULL;
4158 return trace_create_file(option, 0644, t_options, (void *)index,
4159 &trace_options_core_fops);
4162 static __init void create_trace_options_dir(void)
4164 struct dentry *t_options;
4165 int i;
4167 t_options = trace_options_init_dentry();
4168 if (!t_options)
4169 return;
4171 for (i = 0; trace_options[i]; i++)
4172 create_trace_option_core_file(trace_options[i], i);
4175 static __init int tracer_init_debugfs(void)
4177 struct dentry *d_tracer;
4178 int cpu;
4180 d_tracer = tracing_init_dentry();
4182 trace_create_file("tracing_enabled", 0644, d_tracer,
4183 &global_trace, &tracing_ctrl_fops);
4185 trace_create_file("trace_options", 0644, d_tracer,
4186 NULL, &tracing_iter_fops);
4188 trace_create_file("tracing_cpumask", 0644, d_tracer,
4189 NULL, &tracing_cpumask_fops);
4191 trace_create_file("trace", 0644, d_tracer,
4192 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4194 trace_create_file("available_tracers", 0444, d_tracer,
4195 &global_trace, &show_traces_fops);
4197 trace_create_file("current_tracer", 0644, d_tracer,
4198 &global_trace, &set_tracer_fops);
4200 #ifdef CONFIG_TRACER_MAX_TRACE
4201 trace_create_file("tracing_max_latency", 0644, d_tracer,
4202 &tracing_max_latency, &tracing_max_lat_fops);
4204 trace_create_file("tracing_thresh", 0644, d_tracer,
4205 &tracing_thresh, &tracing_max_lat_fops);
4206 #endif
4208 trace_create_file("README", 0444, d_tracer,
4209 NULL, &tracing_readme_fops);
4211 trace_create_file("trace_pipe", 0444, d_tracer,
4212 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4214 trace_create_file("buffer_size_kb", 0644, d_tracer,
4215 &global_trace, &tracing_entries_fops);
4217 trace_create_file("trace_marker", 0220, d_tracer,
4218 NULL, &tracing_mark_fops);
4220 trace_create_file("saved_cmdlines", 0444, d_tracer,
4221 NULL, &tracing_saved_cmdlines_fops);
4223 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4224 &trace_clock_fops);
4226 #ifdef CONFIG_DYNAMIC_FTRACE
4227 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4228 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4229 #endif
4230 #ifdef CONFIG_SYSPROF_TRACER
4231 init_tracer_sysprof_debugfs(d_tracer);
4232 #endif
4234 create_trace_options_dir();
4236 for_each_tracing_cpu(cpu)
4237 tracing_init_debugfs_percpu(cpu);
4239 return 0;
4242 static int trace_panic_handler(struct notifier_block *this,
4243 unsigned long event, void *unused)
4245 if (ftrace_dump_on_oops)
4246 ftrace_dump();
4247 return NOTIFY_OK;
4250 static struct notifier_block trace_panic_notifier = {
4251 .notifier_call = trace_panic_handler,
4252 .next = NULL,
4253 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4256 static int trace_die_handler(struct notifier_block *self,
4257 unsigned long val,
4258 void *data)
4260 switch (val) {
4261 case DIE_OOPS:
4262 if (ftrace_dump_on_oops)
4263 ftrace_dump();
4264 break;
4265 default:
4266 break;
4268 return NOTIFY_OK;
4271 static struct notifier_block trace_die_notifier = {
4272 .notifier_call = trace_die_handler,
4273 .priority = 200
4277 * printk is set to max of 1024, we really don't need it that big.
4278 * Nothing should be printing 1000 characters anyway.
4280 #define TRACE_MAX_PRINT 1000
4283 * Define here KERN_TRACE so that we have one place to modify
4284 * it if we decide to change what log level the ftrace dump
4285 * should be at.
4287 #define KERN_TRACE KERN_EMERG
4289 static void
4290 trace_printk_seq(struct trace_seq *s)
4292 /* Probably should print a warning here. */
4293 if (s->len >= 1000)
4294 s->len = 1000;
4296 /* should be zero ended, but we are paranoid. */
4297 s->buffer[s->len] = 0;
4299 printk(KERN_TRACE "%s", s->buffer);
4301 trace_seq_init(s);
4304 static void __ftrace_dump(bool disable_tracing)
4306 static raw_spinlock_t ftrace_dump_lock =
4307 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
4308 /* use static because iter can be a bit big for the stack */
4309 static struct trace_iterator iter;
4310 unsigned int old_userobj;
4311 static int dump_ran;
4312 unsigned long flags;
4313 int cnt = 0, cpu;
4315 /* only one dump */
4316 local_irq_save(flags);
4317 __raw_spin_lock(&ftrace_dump_lock);
4318 if (dump_ran)
4319 goto out;
4321 dump_ran = 1;
4323 tracing_off();
4325 if (disable_tracing)
4326 ftrace_kill();
4328 for_each_tracing_cpu(cpu) {
4329 atomic_inc(&global_trace.data[cpu]->disabled);
4332 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4334 /* don't look at user memory in panic mode */
4335 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4337 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4339 /* Simulate the iterator */
4340 iter.tr = &global_trace;
4341 iter.trace = current_trace;
4342 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4345 * We need to stop all tracing on all CPUS to read the
4346 * the next buffer. This is a bit expensive, but is
4347 * not done often. We fill all what we can read,
4348 * and then release the locks again.
4351 while (!trace_empty(&iter)) {
4353 if (!cnt)
4354 printk(KERN_TRACE "---------------------------------\n");
4356 cnt++;
4358 /* reset all but tr, trace, and overruns */
4359 memset(&iter.seq, 0,
4360 sizeof(struct trace_iterator) -
4361 offsetof(struct trace_iterator, seq));
4362 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4363 iter.pos = -1;
4365 if (find_next_entry_inc(&iter) != NULL) {
4366 int ret;
4368 ret = print_trace_line(&iter);
4369 if (ret != TRACE_TYPE_NO_CONSUME)
4370 trace_consume(&iter);
4373 trace_printk_seq(&iter.seq);
4376 if (!cnt)
4377 printk(KERN_TRACE " (ftrace buffer empty)\n");
4378 else
4379 printk(KERN_TRACE "---------------------------------\n");
4381 /* Re-enable tracing if requested */
4382 if (!disable_tracing) {
4383 trace_flags |= old_userobj;
4385 for_each_tracing_cpu(cpu) {
4386 atomic_dec(&global_trace.data[cpu]->disabled);
4388 tracing_on();
4391 out:
4392 __raw_spin_unlock(&ftrace_dump_lock);
4393 local_irq_restore(flags);
4396 /* By default: disable tracing after the dump */
4397 void ftrace_dump(void)
4399 __ftrace_dump(true);
4402 __init static int tracer_alloc_buffers(void)
4404 int ring_buf_size;
4405 int i;
4406 int ret = -ENOMEM;
4408 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4409 goto out;
4411 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4412 goto out_free_buffer_mask;
4414 if (!zalloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4415 goto out_free_tracing_cpumask;
4417 /* To save memory, keep the ring buffer size to its minimum */
4418 if (ring_buffer_expanded)
4419 ring_buf_size = trace_buf_size;
4420 else
4421 ring_buf_size = 1;
4423 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4424 cpumask_copy(tracing_cpumask, cpu_all_mask);
4426 /* TODO: make the number of buffers hot pluggable with CPUS */
4427 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4428 TRACE_BUFFER_FLAGS);
4429 if (!global_trace.buffer) {
4430 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4431 WARN_ON(1);
4432 goto out_free_cpumask;
4434 global_trace.entries = ring_buffer_size(global_trace.buffer);
4437 #ifdef CONFIG_TRACER_MAX_TRACE
4438 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4439 TRACE_BUFFER_FLAGS);
4440 if (!max_tr.buffer) {
4441 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4442 WARN_ON(1);
4443 ring_buffer_free(global_trace.buffer);
4444 goto out_free_cpumask;
4446 max_tr.entries = ring_buffer_size(max_tr.buffer);
4447 WARN_ON(max_tr.entries != global_trace.entries);
4448 #endif
4450 /* Allocate the first page for all buffers */
4451 for_each_tracing_cpu(i) {
4452 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4453 max_tr.data[i] = &per_cpu(max_data, i);
4456 trace_init_cmdlines();
4458 register_tracer(&nop_trace);
4459 current_trace = &nop_trace;
4460 #ifdef CONFIG_BOOT_TRACER
4461 register_tracer(&boot_tracer);
4462 #endif
4463 /* All seems OK, enable tracing */
4464 tracing_disabled = 0;
4466 atomic_notifier_chain_register(&panic_notifier_list,
4467 &trace_panic_notifier);
4469 register_die_notifier(&trace_die_notifier);
4471 return 0;
4473 out_free_cpumask:
4474 free_cpumask_var(tracing_reader_cpumask);
4475 out_free_tracing_cpumask:
4476 free_cpumask_var(tracing_cpumask);
4477 out_free_buffer_mask:
4478 free_cpumask_var(tracing_buffer_mask);
4479 out:
4480 return ret;
4483 __init static int clear_boot_tracer(void)
4486 * The default tracer at boot buffer is an init section.
4487 * This function is called in lateinit. If we did not
4488 * find the boot tracer, then clear it out, to prevent
4489 * later registration from accessing the buffer that is
4490 * about to be freed.
4492 if (!default_bootup_tracer)
4493 return 0;
4495 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4496 default_bootup_tracer);
4497 default_bootup_tracer = NULL;
4499 return 0;
4502 early_initcall(tracer_alloc_buffers);
4503 fs_initcall(tracer_init_debugfs);
4504 late_initcall(clear_boot_tracer);