code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.34.14-moxart.git] / kernel / trace / trace.c
blobeb76a22387e0eb5bf4e75d9b6910184e573731af
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 <generated/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/rwsem.h>
36 #include <linux/slab.h>
37 #include <linux/ctype.h>
38 #include <linux/init.h>
39 #include <linux/poll.h>
40 #include <linux/fs.h>
42 #include "trace.h"
43 #include "trace_output.h"
45 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
48 * On boot up, the ring buffer is set to the minimum size, so that
49 * we do not waste memory on systems that are not using tracing.
51 int ring_buffer_expanded;
54 * We need to change this state when a selftest is running.
55 * A selftest will lurk into the ring-buffer to count the
56 * entries inserted during the selftest although some concurrent
57 * insertions into the ring-buffer such as trace_printk could occurred
58 * at the same time, giving false positive or negative results.
60 static bool __read_mostly tracing_selftest_running;
63 * If a tracer is running, we do not want to run SELFTEST.
65 bool __read_mostly tracing_selftest_disabled;
67 /* For tracers that don't implement custom flags */
68 static struct tracer_opt dummy_tracer_opt[] = {
69 { }
72 static struct tracer_flags dummy_tracer_flags = {
73 .val = 0,
74 .opts = dummy_tracer_opt
77 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
79 return 0;
83 * Kill all tracing for good (never come back).
84 * It is initialized to 1 but will turn to zero if the initialization
85 * of the tracer is successful. But that is the only place that sets
86 * this back to zero.
88 static int tracing_disabled = 1;
90 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
92 static inline void ftrace_disable_cpu(void)
94 preempt_disable();
95 __this_cpu_inc(ftrace_cpu_disabled);
98 static inline void ftrace_enable_cpu(void)
100 __this_cpu_dec(ftrace_cpu_disabled);
101 preempt_enable();
104 static cpumask_var_t __read_mostly tracing_buffer_mask;
106 #define for_each_tracing_cpu(cpu) \
107 for_each_cpu(cpu, tracing_buffer_mask)
110 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
112 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
113 * is set, then ftrace_dump is called. This will output the contents
114 * of the ftrace buffers to the console. This is very useful for
115 * capturing traces that lead to crashes and outputing it to a
116 * serial console.
118 * It is default off, but you can enable it with either specifying
119 * "ftrace_dump_on_oops" in the kernel command line, or setting
120 * /proc/sys/kernel/ftrace_dump_on_oops to true.
122 int ftrace_dump_on_oops;
124 static int tracing_set_tracer(const char *buf);
126 #define MAX_TRACER_SIZE 100
127 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
128 static char *default_bootup_tracer;
130 static int __init set_cmdline_ftrace(char *str)
132 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
133 default_bootup_tracer = bootup_tracer_buf;
134 /* We are using ftrace early, expand it */
135 ring_buffer_expanded = 1;
136 return 1;
138 __setup("ftrace=", set_cmdline_ftrace);
140 static int __init set_ftrace_dump_on_oops(char *str)
142 ftrace_dump_on_oops = 1;
143 return 1;
145 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
147 unsigned long long ns2usecs(cycle_t nsec)
149 nsec += 500;
150 do_div(nsec, 1000);
151 return nsec;
155 * The global_trace is the descriptor that holds the tracing
156 * buffers for the live tracing. For each CPU, it contains
157 * a link list of pages that will store trace entries. The
158 * page descriptor of the pages in the memory is used to hold
159 * the link list by linking the lru item in the page descriptor
160 * to each of the pages in the buffer per CPU.
162 * For each active CPU there is a data field that holds the
163 * pages for the buffer for that CPU. Each CPU has the same number
164 * of pages allocated for its buffer.
166 static struct trace_array global_trace;
168 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
170 int filter_current_check_discard(struct ring_buffer *buffer,
171 struct ftrace_event_call *call, void *rec,
172 struct ring_buffer_event *event)
174 return filter_check_discard(call, rec, buffer, event);
176 EXPORT_SYMBOL_GPL(filter_current_check_discard);
178 cycle_t ftrace_now(int cpu)
180 u64 ts;
182 /* Early boot up does not have a buffer yet */
183 if (!global_trace.buffer)
184 return trace_clock_local();
186 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
187 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
189 return ts;
193 * The max_tr is used to snapshot the global_trace when a maximum
194 * latency is reached. Some tracers will use this to store a maximum
195 * trace while it continues examining live traces.
197 * The buffers for the max_tr are set up the same as the global_trace.
198 * When a snapshot is taken, the link list of the max_tr is swapped
199 * with the link list of the global_trace and the buffers are reset for
200 * the global_trace so the tracing can continue.
202 static struct trace_array max_tr;
204 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
206 /* tracer_enabled is used to toggle activation of a tracer */
207 static int tracer_enabled = 1;
210 * tracing_is_enabled - return tracer_enabled status
212 * This function is used by other tracers to know the status
213 * of the tracer_enabled flag. Tracers may use this function
214 * to know if it should enable their features when starting
215 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
217 int tracing_is_enabled(void)
219 return tracer_enabled;
223 * trace_buf_size is the size in bytes that is allocated
224 * for a buffer. Note, the number of bytes is always rounded
225 * to page size.
227 * This number is purposely set to a low number of 16384.
228 * If the dump on oops happens, it will be much appreciated
229 * to not have to wait for all that output. Anyway this can be
230 * boot time and run time configurable.
232 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
234 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
236 /* trace_types holds a link list of available tracers. */
237 static struct tracer *trace_types __read_mostly;
239 /* current_trace points to the tracer that is currently active */
240 static struct tracer *current_trace __read_mostly;
243 * trace_types_lock is used to protect the trace_types list.
245 static DEFINE_MUTEX(trace_types_lock);
248 * serialize the access of the ring buffer
250 * ring buffer serializes readers, but it is low level protection.
251 * The validity of the events (which returns by ring_buffer_peek() ..etc)
252 * are not protected by ring buffer.
254 * The content of events may become garbage if we allow other process consumes
255 * these events concurrently:
256 * A) the page of the consumed events may become a normal page
257 * (not reader page) in ring buffer, and this page will be rewrited
258 * by events producer.
259 * B) The page of the consumed events may become a page for splice_read,
260 * and this page will be returned to system.
262 * These primitives allow multi process access to different cpu ring buffer
263 * concurrently.
265 * These primitives don't distinguish read-only and read-consume access.
266 * Multi read-only access are also serialized.
269 #ifdef CONFIG_SMP
270 static DECLARE_RWSEM(all_cpu_access_lock);
271 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
273 static inline void trace_access_lock(int cpu)
275 if (cpu == TRACE_PIPE_ALL_CPU) {
276 /* gain it for accessing the whole ring buffer. */
277 down_write(&all_cpu_access_lock);
278 } else {
279 /* gain it for accessing a cpu ring buffer. */
281 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
282 down_read(&all_cpu_access_lock);
284 /* Secondly block other access to this @cpu ring buffer. */
285 mutex_lock(&per_cpu(cpu_access_lock, cpu));
289 static inline void trace_access_unlock(int cpu)
291 if (cpu == TRACE_PIPE_ALL_CPU) {
292 up_write(&all_cpu_access_lock);
293 } else {
294 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
295 up_read(&all_cpu_access_lock);
299 static inline void trace_access_lock_init(void)
301 int cpu;
303 for_each_possible_cpu(cpu)
304 mutex_init(&per_cpu(cpu_access_lock, cpu));
307 #else
309 static DEFINE_MUTEX(access_lock);
311 static inline void trace_access_lock(int cpu)
313 (void)cpu;
314 mutex_lock(&access_lock);
317 static inline void trace_access_unlock(int cpu)
319 (void)cpu;
320 mutex_unlock(&access_lock);
323 static inline void trace_access_lock_init(void)
327 #endif
329 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
330 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
332 /* trace_flags holds trace_options default values */
333 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
334 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
335 TRACE_ITER_GRAPH_TIME;
337 static int trace_stop_count;
338 static DEFINE_SPINLOCK(tracing_start_lock);
341 * trace_wake_up - wake up tasks waiting for trace input
343 * Simply wakes up any task that is blocked on the trace_wait
344 * queue. These is used with trace_poll for tasks polling the trace.
346 void trace_wake_up(void)
348 int cpu;
350 if (trace_flags & TRACE_ITER_BLOCK)
351 return;
353 * The runqueue_is_locked() can fail, but this is the best we
354 * have for now:
356 cpu = get_cpu();
357 if (!runqueue_is_locked(cpu))
358 wake_up(&trace_wait);
359 put_cpu();
362 static int __init set_buf_size(char *str)
364 unsigned long buf_size;
366 if (!str)
367 return 0;
368 buf_size = memparse(str, &str);
369 /* nr_entries can not be zero */
370 if (buf_size == 0)
371 return 0;
372 trace_buf_size = buf_size;
373 return 1;
375 __setup("trace_buf_size=", set_buf_size);
377 static int __init set_tracing_thresh(char *str)
379 unsigned long threshhold;
380 int ret;
382 if (!str)
383 return 0;
384 ret = strict_strtoul(str, 0, &threshhold);
385 if (ret < 0)
386 return 0;
387 tracing_thresh = threshhold * 1000;
388 return 1;
390 __setup("tracing_thresh=", set_tracing_thresh);
392 unsigned long nsecs_to_usecs(unsigned long nsecs)
394 return nsecs / 1000;
397 /* These must match the bit postions in trace_iterator_flags */
398 static const char *trace_options[] = {
399 "print-parent",
400 "sym-offset",
401 "sym-addr",
402 "verbose",
403 "raw",
404 "hex",
405 "bin",
406 "block",
407 "stacktrace",
408 "trace_printk",
409 "ftrace_preempt",
410 "branch",
411 "annotate",
412 "userstacktrace",
413 "sym-userobj",
414 "printk-msg-only",
415 "context-info",
416 "latency-format",
417 "sleep-time",
418 "graph-time",
419 NULL
422 static struct {
423 u64 (*func)(void);
424 const char *name;
425 } trace_clocks[] = {
426 { trace_clock_local, "local" },
427 { trace_clock_global, "global" },
430 int trace_clock_id;
433 * trace_parser_get_init - gets the buffer for trace parser
435 int trace_parser_get_init(struct trace_parser *parser, int size)
437 memset(parser, 0, sizeof(*parser));
439 parser->buffer = kmalloc(size, GFP_KERNEL);
440 if (!parser->buffer)
441 return 1;
443 parser->size = size;
444 return 0;
448 * trace_parser_put - frees the buffer for trace parser
450 void trace_parser_put(struct trace_parser *parser)
452 kfree(parser->buffer);
456 * trace_get_user - reads the user input string separated by space
457 * (matched by isspace(ch))
459 * For each string found the 'struct trace_parser' is updated,
460 * and the function returns.
462 * Returns number of bytes read.
464 * See kernel/trace/trace.h for 'struct trace_parser' details.
466 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
467 size_t cnt, loff_t *ppos)
469 char ch;
470 size_t read = 0;
471 ssize_t ret;
473 if (!*ppos)
474 trace_parser_clear(parser);
476 ret = get_user(ch, ubuf++);
477 if (ret)
478 goto out;
480 read++;
481 cnt--;
484 * The parser is not finished with the last write,
485 * continue reading the user input without skipping spaces.
487 if (!parser->cont) {
488 /* skip white space */
489 while (cnt && isspace(ch)) {
490 ret = get_user(ch, ubuf++);
491 if (ret)
492 goto out;
493 read++;
494 cnt--;
497 /* only spaces were written */
498 if (isspace(ch)) {
499 *ppos += read;
500 ret = read;
501 goto out;
504 parser->idx = 0;
507 /* read the non-space input */
508 while (cnt && !isspace(ch)) {
509 if (parser->idx < parser->size - 1)
510 parser->buffer[parser->idx++] = ch;
511 else {
512 ret = -EINVAL;
513 goto out;
515 ret = get_user(ch, ubuf++);
516 if (ret)
517 goto out;
518 read++;
519 cnt--;
522 /* We either got finished input or we have to wait for another call. */
523 if (isspace(ch)) {
524 parser->buffer[parser->idx] = 0;
525 parser->cont = false;
526 } else {
527 parser->cont = true;
528 parser->buffer[parser->idx++] = ch;
531 *ppos += read;
532 ret = read;
534 out:
535 return ret;
538 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
540 int len;
541 int ret;
543 if (!cnt)
544 return 0;
546 if (s->len <= s->readpos)
547 return -EBUSY;
549 len = s->len - s->readpos;
550 if (cnt > len)
551 cnt = len;
552 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
553 if (ret == cnt)
554 return -EFAULT;
556 cnt -= ret;
558 s->readpos += cnt;
559 return cnt;
562 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
564 int len;
565 void *ret;
567 if (s->len <= s->readpos)
568 return -EBUSY;
570 len = s->len - s->readpos;
571 if (cnt > len)
572 cnt = len;
573 ret = memcpy(buf, s->buffer + s->readpos, cnt);
574 if (!ret)
575 return -EFAULT;
577 s->readpos += cnt;
578 return cnt;
582 * ftrace_max_lock is used to protect the swapping of buffers
583 * when taking a max snapshot. The buffers themselves are
584 * protected by per_cpu spinlocks. But the action of the swap
585 * needs its own lock.
587 * This is defined as a arch_spinlock_t in order to help
588 * with performance when lockdep debugging is enabled.
590 * It is also used in other places outside the update_max_tr
591 * so it needs to be defined outside of the
592 * CONFIG_TRACER_MAX_TRACE.
594 static arch_spinlock_t ftrace_max_lock =
595 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
597 unsigned long __read_mostly tracing_thresh;
599 #ifdef CONFIG_TRACER_MAX_TRACE
600 unsigned long __read_mostly tracing_max_latency;
603 * Copy the new maximum trace into the separate maximum-trace
604 * structure. (this way the maximum trace is permanently saved,
605 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
607 static void
608 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
610 struct trace_array_cpu *data = tr->data[cpu];
611 struct trace_array_cpu *max_data;
613 max_tr.cpu = cpu;
614 max_tr.time_start = data->preempt_timestamp;
616 max_data = max_tr.data[cpu];
617 max_data->saved_latency = tracing_max_latency;
618 max_data->critical_start = data->critical_start;
619 max_data->critical_end = data->critical_end;
621 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
622 max_data->pid = tsk->pid;
623 max_data->uid = task_uid(tsk);
624 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
625 max_data->policy = tsk->policy;
626 max_data->rt_priority = tsk->rt_priority;
628 /* record this tasks comm */
629 tracing_record_cmdline(tsk);
633 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
634 * @tr: tracer
635 * @tsk: the task with the latency
636 * @cpu: The cpu that initiated the trace.
638 * Flip the buffers between the @tr and the max_tr and record information
639 * about which task was the cause of this latency.
641 void
642 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
644 struct ring_buffer *buf = tr->buffer;
646 if (trace_stop_count)
647 return;
649 WARN_ON_ONCE(!irqs_disabled());
650 arch_spin_lock(&ftrace_max_lock);
652 tr->buffer = max_tr.buffer;
653 max_tr.buffer = buf;
655 __update_max_tr(tr, tsk, cpu);
656 arch_spin_unlock(&ftrace_max_lock);
660 * update_max_tr_single - only copy one trace over, and reset the rest
661 * @tr - tracer
662 * @tsk - task with the latency
663 * @cpu - the cpu of the buffer to copy.
665 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
667 void
668 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
670 int ret;
672 if (trace_stop_count)
673 return;
675 WARN_ON_ONCE(!irqs_disabled());
676 arch_spin_lock(&ftrace_max_lock);
678 ftrace_disable_cpu();
680 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
682 if (ret == -EBUSY) {
684 * We failed to swap the buffer due to a commit taking
685 * place on this CPU. We fail to record, but we reset
686 * the max trace buffer (no one writes directly to it)
687 * and flag that it failed.
689 trace_array_printk(&max_tr, _THIS_IP_,
690 "Failed to swap buffers due to commit in progress\n");
693 ftrace_enable_cpu();
695 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
697 __update_max_tr(tr, tsk, cpu);
698 arch_spin_unlock(&ftrace_max_lock);
700 #endif /* CONFIG_TRACER_MAX_TRACE */
703 * register_tracer - register a tracer with the ftrace system.
704 * @type - the plugin for the tracer
706 * Register a new plugin tracer.
708 int register_tracer(struct tracer *type)
709 __releases(kernel_lock)
710 __acquires(kernel_lock)
712 struct tracer *t;
713 int ret = 0;
715 if (!type->name) {
716 pr_info("Tracer must have a name\n");
717 return -1;
720 if (strlen(type->name) > MAX_TRACER_SIZE) {
721 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
722 return -1;
726 * When this gets called we hold the BKL which means that
727 * preemption is disabled. Various trace selftests however
728 * need to disable and enable preemption for successful tests.
729 * So we drop the BKL here and grab it after the tests again.
731 unlock_kernel();
732 mutex_lock(&trace_types_lock);
734 tracing_selftest_running = true;
736 for (t = trace_types; t; t = t->next) {
737 if (strcmp(type->name, t->name) == 0) {
738 /* already found */
739 pr_info("Tracer %s already registered\n",
740 type->name);
741 ret = -1;
742 goto out;
746 if (!type->set_flag)
747 type->set_flag = &dummy_set_flag;
748 if (!type->flags)
749 type->flags = &dummy_tracer_flags;
750 else
751 if (!type->flags->opts)
752 type->flags->opts = dummy_tracer_opt;
753 if (!type->wait_pipe)
754 type->wait_pipe = default_wait_pipe;
757 #ifdef CONFIG_FTRACE_STARTUP_TEST
758 if (type->selftest && !tracing_selftest_disabled) {
759 struct tracer *saved_tracer = current_trace;
760 struct trace_array *tr = &global_trace;
763 * Run a selftest on this tracer.
764 * Here we reset the trace buffer, and set the current
765 * tracer to be this tracer. The tracer can then run some
766 * internal tracing to verify that everything is in order.
767 * If we fail, we do not register this tracer.
769 tracing_reset_online_cpus(tr);
771 current_trace = type;
772 /* the test is responsible for initializing and enabling */
773 pr_info("Testing tracer %s: ", type->name);
774 ret = type->selftest(type, tr);
775 /* the test is responsible for resetting too */
776 current_trace = saved_tracer;
777 if (ret) {
778 printk(KERN_CONT "FAILED!\n");
779 goto out;
781 /* Only reset on passing, to avoid touching corrupted buffers */
782 tracing_reset_online_cpus(tr);
784 printk(KERN_CONT "PASSED\n");
786 #endif
788 type->next = trace_types;
789 trace_types = type;
791 out:
792 tracing_selftest_running = false;
793 mutex_unlock(&trace_types_lock);
795 if (ret || !default_bootup_tracer)
796 goto out_unlock;
798 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
799 goto out_unlock;
801 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
802 /* Do we want this tracer to start on bootup? */
803 tracing_set_tracer(type->name);
804 default_bootup_tracer = NULL;
805 /* disable other selftests, since this will break it. */
806 tracing_selftest_disabled = 1;
807 #ifdef CONFIG_FTRACE_STARTUP_TEST
808 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
809 type->name);
810 #endif
812 out_unlock:
813 lock_kernel();
814 return ret;
817 void unregister_tracer(struct tracer *type)
819 struct tracer **t;
821 mutex_lock(&trace_types_lock);
822 for (t = &trace_types; *t; t = &(*t)->next) {
823 if (*t == type)
824 goto found;
826 pr_info("Tracer %s not registered\n", type->name);
827 goto out;
829 found:
830 *t = (*t)->next;
832 if (type == current_trace && tracer_enabled) {
833 tracer_enabled = 0;
834 tracing_stop();
835 if (current_trace->stop)
836 current_trace->stop(&global_trace);
837 current_trace = &nop_trace;
839 out:
840 mutex_unlock(&trace_types_lock);
843 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
845 ftrace_disable_cpu();
846 ring_buffer_reset_cpu(buffer, cpu);
847 ftrace_enable_cpu();
850 void tracing_reset(struct trace_array *tr, int cpu)
852 struct ring_buffer *buffer = tr->buffer;
854 ring_buffer_record_disable(buffer);
856 /* Make sure all commits have finished */
857 synchronize_sched();
858 __tracing_reset(buffer, cpu);
860 ring_buffer_record_enable(buffer);
863 void tracing_reset_online_cpus(struct trace_array *tr)
865 struct ring_buffer *buffer = tr->buffer;
866 int cpu;
868 ring_buffer_record_disable(buffer);
870 /* Make sure all commits have finished */
871 synchronize_sched();
873 tr->time_start = ftrace_now(tr->cpu);
875 for_each_online_cpu(cpu)
876 __tracing_reset(buffer, cpu);
878 ring_buffer_record_enable(buffer);
881 void tracing_reset_current(int cpu)
883 tracing_reset(&global_trace, cpu);
886 void tracing_reset_current_online_cpus(void)
888 tracing_reset_online_cpus(&global_trace);
891 #define SAVED_CMDLINES 128
892 #define NO_CMDLINE_MAP UINT_MAX
893 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
894 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
895 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
896 static int cmdline_idx;
897 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
899 /* temporary disable recording */
900 static atomic_t trace_record_cmdline_disabled __read_mostly;
902 static void trace_init_cmdlines(void)
904 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
905 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
906 cmdline_idx = 0;
909 int is_tracing_stopped(void)
911 return trace_stop_count;
915 * ftrace_off_permanent - disable all ftrace code permanently
917 * This should only be called when a serious anomally has
918 * been detected. This will turn off the function tracing,
919 * ring buffers, and other tracing utilites. It takes no
920 * locks and can be called from any context.
922 void ftrace_off_permanent(void)
924 tracing_disabled = 1;
925 ftrace_stop();
926 tracing_off_permanent();
930 * tracing_start - quick start of the tracer
932 * If tracing is enabled but was stopped by tracing_stop,
933 * this will start the tracer back up.
935 void tracing_start(void)
937 struct ring_buffer *buffer;
938 unsigned long flags;
940 if (tracing_disabled)
941 return;
943 spin_lock_irqsave(&tracing_start_lock, flags);
944 if (--trace_stop_count) {
945 if (trace_stop_count < 0) {
946 /* Someone screwed up their debugging */
947 WARN_ON_ONCE(1);
948 trace_stop_count = 0;
950 goto out;
953 /* Prevent the buffers from switching */
954 arch_spin_lock(&ftrace_max_lock);
956 buffer = global_trace.buffer;
957 if (buffer)
958 ring_buffer_record_enable(buffer);
960 buffer = max_tr.buffer;
961 if (buffer)
962 ring_buffer_record_enable(buffer);
964 arch_spin_unlock(&ftrace_max_lock);
966 ftrace_start();
967 out:
968 spin_unlock_irqrestore(&tracing_start_lock, flags);
972 * tracing_stop - quick stop of the tracer
974 * Light weight way to stop tracing. Use in conjunction with
975 * tracing_start.
977 void tracing_stop(void)
979 struct ring_buffer *buffer;
980 unsigned long flags;
982 ftrace_stop();
983 spin_lock_irqsave(&tracing_start_lock, flags);
984 if (trace_stop_count++)
985 goto out;
987 /* Prevent the buffers from switching */
988 arch_spin_lock(&ftrace_max_lock);
990 buffer = global_trace.buffer;
991 if (buffer)
992 ring_buffer_record_disable(buffer);
994 buffer = max_tr.buffer;
995 if (buffer)
996 ring_buffer_record_disable(buffer);
998 arch_spin_unlock(&ftrace_max_lock);
1000 out:
1001 spin_unlock_irqrestore(&tracing_start_lock, flags);
1004 void trace_stop_cmdline_recording(void);
1006 static void trace_save_cmdline(struct task_struct *tsk)
1008 unsigned pid, idx;
1010 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1011 return;
1014 * It's not the end of the world if we don't get
1015 * the lock, but we also don't want to spin
1016 * nor do we want to disable interrupts,
1017 * so if we miss here, then better luck next time.
1019 if (!arch_spin_trylock(&trace_cmdline_lock))
1020 return;
1022 idx = map_pid_to_cmdline[tsk->pid];
1023 if (idx == NO_CMDLINE_MAP) {
1024 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1027 * Check whether the cmdline buffer at idx has a pid
1028 * mapped. We are going to overwrite that entry so we
1029 * need to clear the map_pid_to_cmdline. Otherwise we
1030 * would read the new comm for the old pid.
1032 pid = map_cmdline_to_pid[idx];
1033 if (pid != NO_CMDLINE_MAP)
1034 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1036 map_cmdline_to_pid[idx] = tsk->pid;
1037 map_pid_to_cmdline[tsk->pid] = idx;
1039 cmdline_idx = idx;
1042 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1044 arch_spin_unlock(&trace_cmdline_lock);
1047 void trace_find_cmdline(int pid, char comm[])
1049 unsigned map;
1051 if (!pid) {
1052 strcpy(comm, "<idle>");
1053 return;
1056 if (WARN_ON_ONCE(pid < 0)) {
1057 strcpy(comm, "<XXX>");
1058 return;
1061 if (pid > PID_MAX_DEFAULT) {
1062 strcpy(comm, "<...>");
1063 return;
1066 preempt_disable();
1067 arch_spin_lock(&trace_cmdline_lock);
1068 map = map_pid_to_cmdline[pid];
1069 if (map != NO_CMDLINE_MAP)
1070 strcpy(comm, saved_cmdlines[map]);
1071 else
1072 strcpy(comm, "<...>");
1074 arch_spin_unlock(&trace_cmdline_lock);
1075 preempt_enable();
1078 void tracing_record_cmdline(struct task_struct *tsk)
1080 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1081 !tracing_is_on())
1082 return;
1084 trace_save_cmdline(tsk);
1087 void
1088 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1089 int pc)
1091 struct task_struct *tsk = current;
1093 entry->preempt_count = pc & 0xff;
1094 entry->pid = (tsk) ? tsk->pid : 0;
1095 entry->lock_depth = (tsk) ? tsk->lock_depth : 0;
1096 entry->flags =
1097 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1098 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1099 #else
1100 TRACE_FLAG_IRQS_NOSUPPORT |
1101 #endif
1102 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1103 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1104 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1106 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1108 struct ring_buffer_event *
1109 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1110 int type,
1111 unsigned long len,
1112 unsigned long flags, int pc)
1114 struct ring_buffer_event *event;
1116 event = ring_buffer_lock_reserve(buffer, len);
1117 if (event != NULL) {
1118 struct trace_entry *ent = ring_buffer_event_data(event);
1120 tracing_generic_entry_update(ent, flags, pc);
1121 ent->type = type;
1124 return event;
1127 static inline void
1128 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1129 struct ring_buffer_event *event,
1130 unsigned long flags, int pc,
1131 int wake)
1133 ring_buffer_unlock_commit(buffer, event);
1135 ftrace_trace_stack(buffer, flags, 6, pc);
1136 ftrace_trace_userstack(buffer, flags, pc);
1138 if (wake)
1139 trace_wake_up();
1142 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1143 struct ring_buffer_event *event,
1144 unsigned long flags, int pc)
1146 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1149 struct ring_buffer_event *
1150 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1151 int type, unsigned long len,
1152 unsigned long flags, int pc)
1154 *current_rb = global_trace.buffer;
1155 return trace_buffer_lock_reserve(*current_rb,
1156 type, len, flags, pc);
1158 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1160 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1161 struct ring_buffer_event *event,
1162 unsigned long flags, int pc)
1164 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1166 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1168 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1169 struct ring_buffer_event *event,
1170 unsigned long flags, int pc)
1172 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1174 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1176 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1177 struct ring_buffer_event *event)
1179 ring_buffer_discard_commit(buffer, event);
1181 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1183 void
1184 trace_function(struct trace_array *tr,
1185 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1186 int pc)
1188 struct ftrace_event_call *call = &event_function;
1189 struct ring_buffer *buffer = tr->buffer;
1190 struct ring_buffer_event *event;
1191 struct ftrace_entry *entry;
1193 /* If we are reading the ring buffer, don't trace */
1194 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1195 return;
1197 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1198 flags, pc);
1199 if (!event)
1200 return;
1201 entry = ring_buffer_event_data(event);
1202 entry->ip = ip;
1203 entry->parent_ip = parent_ip;
1205 if (!filter_check_discard(call, entry, buffer, event))
1206 ring_buffer_unlock_commit(buffer, event);
1209 void
1210 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1211 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1212 int pc)
1214 if (likely(!atomic_read(&data->disabled)))
1215 trace_function(tr, ip, parent_ip, flags, pc);
1218 #ifdef CONFIG_STACKTRACE
1219 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1220 unsigned long flags,
1221 int skip, int pc)
1223 struct ftrace_event_call *call = &event_kernel_stack;
1224 struct ring_buffer_event *event;
1225 struct stack_entry *entry;
1226 struct stack_trace trace;
1228 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1229 sizeof(*entry), flags, pc);
1230 if (!event)
1231 return;
1232 entry = ring_buffer_event_data(event);
1233 memset(&entry->caller, 0, sizeof(entry->caller));
1235 trace.nr_entries = 0;
1236 trace.max_entries = FTRACE_STACK_ENTRIES;
1237 trace.skip = skip;
1238 trace.entries = entry->caller;
1240 save_stack_trace(&trace);
1241 if (!filter_check_discard(call, entry, buffer, event))
1242 ring_buffer_unlock_commit(buffer, event);
1245 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1246 int skip, int pc)
1248 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1249 return;
1251 __ftrace_trace_stack(buffer, flags, skip, pc);
1254 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1255 int pc)
1257 __ftrace_trace_stack(tr->buffer, flags, skip, pc);
1261 * trace_dump_stack - record a stack back trace in the trace buffer
1263 void trace_dump_stack(void)
1265 unsigned long flags;
1267 if (tracing_disabled || tracing_selftest_running)
1268 return;
1270 local_save_flags(flags);
1272 /* skipping 3 traces, seems to get us at the caller of this function */
1273 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
1276 void
1277 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1279 struct ftrace_event_call *call = &event_user_stack;
1280 struct ring_buffer_event *event;
1281 struct userstack_entry *entry;
1282 struct stack_trace trace;
1284 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1285 return;
1288 * NMIs can not handle page faults, even with fix ups.
1289 * The save user stack can (and often does) fault.
1291 if (unlikely(in_nmi()))
1292 return;
1294 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1295 sizeof(*entry), flags, pc);
1296 if (!event)
1297 return;
1298 entry = ring_buffer_event_data(event);
1300 entry->tgid = current->tgid;
1301 memset(&entry->caller, 0, sizeof(entry->caller));
1303 trace.nr_entries = 0;
1304 trace.max_entries = FTRACE_STACK_ENTRIES;
1305 trace.skip = 0;
1306 trace.entries = entry->caller;
1308 save_stack_trace_user(&trace);
1309 if (!filter_check_discard(call, entry, buffer, event))
1310 ring_buffer_unlock_commit(buffer, event);
1313 #ifdef UNUSED
1314 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1316 ftrace_trace_userstack(tr, flags, preempt_count());
1318 #endif /* UNUSED */
1320 #endif /* CONFIG_STACKTRACE */
1322 static void
1323 ftrace_trace_special(void *__tr,
1324 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1325 int pc)
1327 struct ftrace_event_call *call = &event_special;
1328 struct ring_buffer_event *event;
1329 struct trace_array *tr = __tr;
1330 struct ring_buffer *buffer = tr->buffer;
1331 struct special_entry *entry;
1333 event = trace_buffer_lock_reserve(buffer, TRACE_SPECIAL,
1334 sizeof(*entry), 0, pc);
1335 if (!event)
1336 return;
1337 entry = ring_buffer_event_data(event);
1338 entry->arg1 = arg1;
1339 entry->arg2 = arg2;
1340 entry->arg3 = arg3;
1342 if (!filter_check_discard(call, entry, buffer, event))
1343 trace_buffer_unlock_commit(buffer, event, 0, pc);
1346 void
1347 __trace_special(void *__tr, void *__data,
1348 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1350 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1353 void
1354 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1356 struct trace_array *tr = &global_trace;
1357 struct trace_array_cpu *data;
1358 unsigned long flags;
1359 int cpu;
1360 int pc;
1362 if (tracing_disabled)
1363 return;
1365 pc = preempt_count();
1366 local_irq_save(flags);
1367 cpu = raw_smp_processor_id();
1368 data = tr->data[cpu];
1370 if (likely(atomic_inc_return(&data->disabled) == 1))
1371 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1373 atomic_dec(&data->disabled);
1374 local_irq_restore(flags);
1378 * trace_vbprintk - write binary msg to tracing buffer
1381 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1383 static arch_spinlock_t trace_buf_lock =
1384 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1385 static u32 trace_buf[TRACE_BUF_SIZE];
1387 struct ftrace_event_call *call = &event_bprint;
1388 struct ring_buffer_event *event;
1389 struct ring_buffer *buffer;
1390 struct trace_array *tr = &global_trace;
1391 struct trace_array_cpu *data;
1392 struct bprint_entry *entry;
1393 unsigned long flags;
1394 int disable;
1395 int resched;
1396 int cpu, len = 0, size, pc;
1398 if (unlikely(tracing_selftest_running || tracing_disabled))
1399 return 0;
1401 /* Don't pollute graph traces with trace_vprintk internals */
1402 pause_graph_tracing();
1404 pc = preempt_count();
1405 resched = ftrace_preempt_disable();
1406 cpu = raw_smp_processor_id();
1407 data = tr->data[cpu];
1409 disable = atomic_inc_return(&data->disabled);
1410 if (unlikely(disable != 1))
1411 goto out;
1413 /* Lockdep uses trace_printk for lock tracing */
1414 local_irq_save(flags);
1415 arch_spin_lock(&trace_buf_lock);
1416 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1418 if (len > TRACE_BUF_SIZE || len < 0)
1419 goto out_unlock;
1421 size = sizeof(*entry) + sizeof(u32) * len;
1422 buffer = tr->buffer;
1423 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1424 flags, pc);
1425 if (!event)
1426 goto out_unlock;
1427 entry = ring_buffer_event_data(event);
1428 entry->ip = ip;
1429 entry->fmt = fmt;
1431 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1432 if (!filter_check_discard(call, entry, buffer, event)) {
1433 ring_buffer_unlock_commit(buffer, event);
1434 ftrace_trace_stack(buffer, flags, 6, pc);
1437 out_unlock:
1438 arch_spin_unlock(&trace_buf_lock);
1439 local_irq_restore(flags);
1441 out:
1442 atomic_dec_return(&data->disabled);
1443 ftrace_preempt_enable(resched);
1444 unpause_graph_tracing();
1446 return len;
1448 EXPORT_SYMBOL_GPL(trace_vbprintk);
1450 int trace_array_printk(struct trace_array *tr,
1451 unsigned long ip, const char *fmt, ...)
1453 int ret;
1454 va_list ap;
1456 if (!(trace_flags & TRACE_ITER_PRINTK))
1457 return 0;
1459 va_start(ap, fmt);
1460 ret = trace_array_vprintk(tr, ip, fmt, ap);
1461 va_end(ap);
1462 return ret;
1465 int trace_array_vprintk(struct trace_array *tr,
1466 unsigned long ip, const char *fmt, va_list args)
1468 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1469 static char trace_buf[TRACE_BUF_SIZE];
1471 struct ftrace_event_call *call = &event_print;
1472 struct ring_buffer_event *event;
1473 struct ring_buffer *buffer;
1474 struct trace_array_cpu *data;
1475 int cpu, len = 0, size, pc;
1476 struct print_entry *entry;
1477 unsigned long irq_flags;
1478 int disable;
1480 if (tracing_disabled || tracing_selftest_running)
1481 return 0;
1483 pc = preempt_count();
1484 preempt_disable_notrace();
1485 cpu = raw_smp_processor_id();
1486 data = tr->data[cpu];
1488 disable = atomic_inc_return(&data->disabled);
1489 if (unlikely(disable != 1))
1490 goto out;
1492 pause_graph_tracing();
1493 raw_local_irq_save(irq_flags);
1494 arch_spin_lock(&trace_buf_lock);
1495 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1497 size = sizeof(*entry) + len + 1;
1498 buffer = tr->buffer;
1499 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1500 irq_flags, pc);
1501 if (!event)
1502 goto out_unlock;
1503 entry = ring_buffer_event_data(event);
1504 entry->ip = ip;
1506 memcpy(&entry->buf, trace_buf, len);
1507 entry->buf[len] = '\0';
1508 if (!filter_check_discard(call, entry, buffer, event)) {
1509 ring_buffer_unlock_commit(buffer, event);
1510 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1513 out_unlock:
1514 arch_spin_unlock(&trace_buf_lock);
1515 raw_local_irq_restore(irq_flags);
1516 unpause_graph_tracing();
1517 out:
1518 atomic_dec_return(&data->disabled);
1519 preempt_enable_notrace();
1521 return len;
1524 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1526 return trace_array_vprintk(&global_trace, ip, fmt, args);
1528 EXPORT_SYMBOL_GPL(trace_vprintk);
1530 enum trace_file_type {
1531 TRACE_FILE_LAT_FMT = 1,
1532 TRACE_FILE_ANNOTATE = 2,
1535 static void trace_iterator_increment(struct trace_iterator *iter)
1537 /* Don't allow ftrace to trace into the ring buffers */
1538 ftrace_disable_cpu();
1540 iter->idx++;
1541 if (iter->buffer_iter[iter->cpu])
1542 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1544 ftrace_enable_cpu();
1547 static struct trace_entry *
1548 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1550 struct ring_buffer_event *event;
1551 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1553 /* Don't allow ftrace to trace into the ring buffers */
1554 ftrace_disable_cpu();
1556 if (buf_iter)
1557 event = ring_buffer_iter_peek(buf_iter, ts);
1558 else
1559 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1561 ftrace_enable_cpu();
1563 return event ? ring_buffer_event_data(event) : NULL;
1566 static struct trace_entry *
1567 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1569 struct ring_buffer *buffer = iter->tr->buffer;
1570 struct trace_entry *ent, *next = NULL;
1571 int cpu_file = iter->cpu_file;
1572 u64 next_ts = 0, ts;
1573 int next_cpu = -1;
1574 int cpu;
1577 * If we are in a per_cpu trace file, don't bother by iterating over
1578 * all cpu and peek directly.
1580 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1581 if (ring_buffer_empty_cpu(buffer, cpu_file))
1582 return NULL;
1583 ent = peek_next_entry(iter, cpu_file, ent_ts);
1584 if (ent_cpu)
1585 *ent_cpu = cpu_file;
1587 return ent;
1590 for_each_tracing_cpu(cpu) {
1592 if (ring_buffer_empty_cpu(buffer, cpu))
1593 continue;
1595 ent = peek_next_entry(iter, cpu, &ts);
1598 * Pick the entry with the smallest timestamp:
1600 if (ent && (!next || ts < next_ts)) {
1601 next = ent;
1602 next_cpu = cpu;
1603 next_ts = ts;
1607 if (ent_cpu)
1608 *ent_cpu = next_cpu;
1610 if (ent_ts)
1611 *ent_ts = next_ts;
1613 return next;
1616 /* Find the next real entry, without updating the iterator itself */
1617 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1618 int *ent_cpu, u64 *ent_ts)
1620 return __find_next_entry(iter, ent_cpu, ent_ts);
1623 /* Find the next real entry, and increment the iterator to the next entry */
1624 static void *find_next_entry_inc(struct trace_iterator *iter)
1626 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1628 if (iter->ent)
1629 trace_iterator_increment(iter);
1631 return iter->ent ? iter : NULL;
1634 static void trace_consume(struct trace_iterator *iter)
1636 /* Don't allow ftrace to trace into the ring buffers */
1637 ftrace_disable_cpu();
1638 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1639 ftrace_enable_cpu();
1642 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1644 struct trace_iterator *iter = m->private;
1645 int i = (int)*pos;
1646 void *ent;
1648 WARN_ON_ONCE(iter->leftover);
1650 (*pos)++;
1652 /* can't go backwards */
1653 if (iter->idx > i)
1654 return NULL;
1656 if (iter->idx < 0)
1657 ent = find_next_entry_inc(iter);
1658 else
1659 ent = iter;
1661 while (ent && iter->idx < i)
1662 ent = find_next_entry_inc(iter);
1664 iter->pos = *pos;
1666 return ent;
1669 static void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1671 struct trace_array *tr = iter->tr;
1672 struct ring_buffer_event *event;
1673 struct ring_buffer_iter *buf_iter;
1674 unsigned long entries = 0;
1675 u64 ts;
1677 tr->data[cpu]->skipped_entries = 0;
1679 if (!iter->buffer_iter[cpu])
1680 return;
1682 buf_iter = iter->buffer_iter[cpu];
1683 ring_buffer_iter_reset(buf_iter);
1686 * We could have the case with the max latency tracers
1687 * that a reset never took place on a cpu. This is evident
1688 * by the timestamp being before the start of the buffer.
1690 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1691 if (ts >= iter->tr->time_start)
1692 break;
1693 entries++;
1694 ring_buffer_read(buf_iter, NULL);
1697 tr->data[cpu]->skipped_entries = entries;
1701 * The current tracer is copied to avoid a global locking
1702 * all around.
1704 static void *s_start(struct seq_file *m, loff_t *pos)
1706 struct trace_iterator *iter = m->private;
1707 static struct tracer *old_tracer;
1708 int cpu_file = iter->cpu_file;
1709 void *p = NULL;
1710 loff_t l = 0;
1711 int cpu;
1713 /* copy the tracer to avoid using a global lock all around */
1714 mutex_lock(&trace_types_lock);
1715 if (unlikely(old_tracer != current_trace && current_trace)) {
1716 old_tracer = current_trace;
1717 *iter->trace = *current_trace;
1719 mutex_unlock(&trace_types_lock);
1721 atomic_inc(&trace_record_cmdline_disabled);
1723 if (*pos != iter->pos) {
1724 iter->ent = NULL;
1725 iter->cpu = 0;
1726 iter->idx = -1;
1728 ftrace_disable_cpu();
1730 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1731 for_each_tracing_cpu(cpu)
1732 tracing_iter_reset(iter, cpu);
1733 } else
1734 tracing_iter_reset(iter, cpu_file);
1736 ftrace_enable_cpu();
1738 iter->leftover = 0;
1739 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1742 } else {
1744 * If we overflowed the seq_file before, then we want
1745 * to just reuse the trace_seq buffer again.
1747 if (iter->leftover)
1748 p = iter;
1749 else {
1750 l = *pos - 1;
1751 p = s_next(m, p, &l);
1755 trace_event_read_lock();
1756 trace_access_lock(cpu_file);
1757 return p;
1760 static void s_stop(struct seq_file *m, void *p)
1762 struct trace_iterator *iter = m->private;
1764 atomic_dec(&trace_record_cmdline_disabled);
1765 trace_access_unlock(iter->cpu_file);
1766 trace_event_read_unlock();
1769 static void print_lat_help_header(struct seq_file *m)
1771 seq_puts(m, "# _------=> CPU# \n");
1772 seq_puts(m, "# / _-----=> irqs-off \n");
1773 seq_puts(m, "# | / _----=> need-resched \n");
1774 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1775 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1776 seq_puts(m, "# |||| /_--=> lock-depth \n");
1777 seq_puts(m, "# |||||/ delay \n");
1778 seq_puts(m, "# cmd pid |||||| time | caller \n");
1779 seq_puts(m, "# \\ / |||||| \\ | / \n");
1782 static void print_func_help_header(struct seq_file *m)
1784 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1785 seq_puts(m, "# | | | | |\n");
1789 static void
1790 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1792 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1793 struct trace_array *tr = iter->tr;
1794 struct trace_array_cpu *data = tr->data[tr->cpu];
1795 struct tracer *type = current_trace;
1796 unsigned long entries = 0;
1797 unsigned long total = 0;
1798 unsigned long count;
1799 const char *name = "preemption";
1800 int cpu;
1802 if (type)
1803 name = type->name;
1806 for_each_tracing_cpu(cpu) {
1807 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1809 * If this buffer has skipped entries, then we hold all
1810 * entries for the trace and we need to ignore the
1811 * ones before the time stamp.
1813 if (tr->data[cpu]->skipped_entries) {
1814 count -= tr->data[cpu]->skipped_entries;
1815 /* total is the same as the entries */
1816 total += count;
1817 } else
1818 total += count +
1819 ring_buffer_overrun_cpu(tr->buffer, cpu);
1820 entries += count;
1823 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1824 name, UTS_RELEASE);
1825 seq_puts(m, "# -----------------------------------"
1826 "---------------------------------\n");
1827 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1828 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1829 nsecs_to_usecs(data->saved_latency),
1830 entries,
1831 total,
1832 tr->cpu,
1833 #if defined(CONFIG_PREEMPT_NONE)
1834 "server",
1835 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1836 "desktop",
1837 #elif defined(CONFIG_PREEMPT)
1838 "preempt",
1839 #else
1840 "unknown",
1841 #endif
1842 /* These are reserved for later use */
1843 0, 0, 0, 0);
1844 #ifdef CONFIG_SMP
1845 seq_printf(m, " #P:%d)\n", num_online_cpus());
1846 #else
1847 seq_puts(m, ")\n");
1848 #endif
1849 seq_puts(m, "# -----------------\n");
1850 seq_printf(m, "# | task: %.16s-%d "
1851 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1852 data->comm, data->pid, data->uid, data->nice,
1853 data->policy, data->rt_priority);
1854 seq_puts(m, "# -----------------\n");
1856 if (data->critical_start) {
1857 seq_puts(m, "# => started at: ");
1858 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1859 trace_print_seq(m, &iter->seq);
1860 seq_puts(m, "\n# => ended at: ");
1861 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1862 trace_print_seq(m, &iter->seq);
1863 seq_puts(m, "\n#\n");
1866 seq_puts(m, "#\n");
1869 static void test_cpu_buff_start(struct trace_iterator *iter)
1871 struct trace_seq *s = &iter->seq;
1873 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1874 return;
1876 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1877 return;
1879 if (cpumask_test_cpu(iter->cpu, iter->started))
1880 return;
1882 if (iter->tr->data[iter->cpu]->skipped_entries)
1883 return;
1885 cpumask_set_cpu(iter->cpu, iter->started);
1887 /* Don't print started cpu buffer for the first entry of the trace */
1888 if (iter->idx > 1)
1889 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1890 iter->cpu);
1893 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1895 struct trace_seq *s = &iter->seq;
1896 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1897 struct trace_entry *entry;
1898 struct trace_event *event;
1900 entry = iter->ent;
1902 test_cpu_buff_start(iter);
1904 event = ftrace_find_event(entry->type);
1906 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1907 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1908 if (!trace_print_lat_context(iter))
1909 goto partial;
1910 } else {
1911 if (!trace_print_context(iter))
1912 goto partial;
1916 if (event)
1917 return event->trace(iter, sym_flags);
1919 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1920 goto partial;
1922 return TRACE_TYPE_HANDLED;
1923 partial:
1924 return TRACE_TYPE_PARTIAL_LINE;
1927 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1929 struct trace_seq *s = &iter->seq;
1930 struct trace_entry *entry;
1931 struct trace_event *event;
1933 entry = iter->ent;
1935 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1936 if (!trace_seq_printf(s, "%d %d %llu ",
1937 entry->pid, iter->cpu, iter->ts))
1938 goto partial;
1941 event = ftrace_find_event(entry->type);
1942 if (event)
1943 return event->raw(iter, 0);
1945 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1946 goto partial;
1948 return TRACE_TYPE_HANDLED;
1949 partial:
1950 return TRACE_TYPE_PARTIAL_LINE;
1953 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1955 struct trace_seq *s = &iter->seq;
1956 unsigned char newline = '\n';
1957 struct trace_entry *entry;
1958 struct trace_event *event;
1960 entry = iter->ent;
1962 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1963 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1964 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1965 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1968 event = ftrace_find_event(entry->type);
1969 if (event) {
1970 enum print_line_t ret = event->hex(iter, 0);
1971 if (ret != TRACE_TYPE_HANDLED)
1972 return ret;
1975 SEQ_PUT_FIELD_RET(s, newline);
1977 return TRACE_TYPE_HANDLED;
1980 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1982 struct trace_seq *s = &iter->seq;
1983 struct trace_entry *entry;
1984 struct trace_event *event;
1986 entry = iter->ent;
1988 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1989 SEQ_PUT_FIELD_RET(s, entry->pid);
1990 SEQ_PUT_FIELD_RET(s, iter->cpu);
1991 SEQ_PUT_FIELD_RET(s, iter->ts);
1994 event = ftrace_find_event(entry->type);
1995 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1998 static int trace_empty(struct trace_iterator *iter)
2000 int cpu;
2002 /* If we are looking at one CPU buffer, only check that one */
2003 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2004 cpu = iter->cpu_file;
2005 if (iter->buffer_iter[cpu]) {
2006 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2007 return 0;
2008 } else {
2009 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2010 return 0;
2012 return 1;
2015 for_each_tracing_cpu(cpu) {
2016 if (iter->buffer_iter[cpu]) {
2017 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2018 return 0;
2019 } else {
2020 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2021 return 0;
2025 return 1;
2028 /* Called with trace_event_read_lock() held. */
2029 static enum print_line_t print_trace_line(struct trace_iterator *iter)
2031 enum print_line_t ret;
2033 if (iter->trace && iter->trace->print_line) {
2034 ret = iter->trace->print_line(iter);
2035 if (ret != TRACE_TYPE_UNHANDLED)
2036 return ret;
2039 if (iter->ent->type == TRACE_BPRINT &&
2040 trace_flags & TRACE_ITER_PRINTK &&
2041 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2042 return trace_print_bprintk_msg_only(iter);
2044 if (iter->ent->type == TRACE_PRINT &&
2045 trace_flags & TRACE_ITER_PRINTK &&
2046 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2047 return trace_print_printk_msg_only(iter);
2049 if (trace_flags & TRACE_ITER_BIN)
2050 return print_bin_fmt(iter);
2052 if (trace_flags & TRACE_ITER_HEX)
2053 return print_hex_fmt(iter);
2055 if (trace_flags & TRACE_ITER_RAW)
2056 return print_raw_fmt(iter);
2058 return print_trace_fmt(iter);
2061 static int s_show(struct seq_file *m, void *v)
2063 struct trace_iterator *iter = v;
2064 int ret;
2066 if (iter->ent == NULL) {
2067 if (iter->tr) {
2068 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2069 seq_puts(m, "#\n");
2071 if (iter->trace && iter->trace->print_header)
2072 iter->trace->print_header(m);
2073 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2074 /* print nothing if the buffers are empty */
2075 if (trace_empty(iter))
2076 return 0;
2077 print_trace_header(m, iter);
2078 if (!(trace_flags & TRACE_ITER_VERBOSE))
2079 print_lat_help_header(m);
2080 } else {
2081 if (!(trace_flags & TRACE_ITER_VERBOSE))
2082 print_func_help_header(m);
2084 } else if (iter->leftover) {
2086 * If we filled the seq_file buffer earlier, we
2087 * want to just show it now.
2089 ret = trace_print_seq(m, &iter->seq);
2091 /* ret should this time be zero, but you never know */
2092 iter->leftover = ret;
2094 } else {
2095 print_trace_line(iter);
2096 ret = trace_print_seq(m, &iter->seq);
2098 * If we overflow the seq_file buffer, then it will
2099 * ask us for this data again at start up.
2100 * Use that instead.
2101 * ret is 0 if seq_file write succeeded.
2102 * -1 otherwise.
2104 iter->leftover = ret;
2107 return 0;
2110 static const struct seq_operations tracer_seq_ops = {
2111 .start = s_start,
2112 .next = s_next,
2113 .stop = s_stop,
2114 .show = s_show,
2117 static struct trace_iterator *
2118 __tracing_open(struct inode *inode, struct file *file)
2120 long cpu_file = (long) inode->i_private;
2121 void *fail_ret = ERR_PTR(-ENOMEM);
2122 struct trace_iterator *iter;
2123 struct seq_file *m;
2124 int cpu, ret;
2126 if (tracing_disabled)
2127 return ERR_PTR(-ENODEV);
2129 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2130 if (!iter)
2131 return ERR_PTR(-ENOMEM);
2134 * We make a copy of the current tracer to avoid concurrent
2135 * changes on it while we are reading.
2137 mutex_lock(&trace_types_lock);
2138 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2139 if (!iter->trace)
2140 goto fail;
2142 if (current_trace)
2143 *iter->trace = *current_trace;
2145 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2146 goto fail;
2148 if (current_trace && current_trace->print_max)
2149 iter->tr = &max_tr;
2150 else
2151 iter->tr = &global_trace;
2152 iter->pos = -1;
2153 mutex_init(&iter->mutex);
2154 iter->cpu_file = cpu_file;
2156 /* Notify the tracer early; before we stop tracing. */
2157 if (iter->trace && iter->trace->open)
2158 iter->trace->open(iter);
2160 /* Annotate start of buffers if we had overruns */
2161 if (ring_buffer_overruns(iter->tr->buffer))
2162 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2164 /* stop the trace while dumping */
2165 tracing_stop();
2167 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2168 for_each_tracing_cpu(cpu) {
2170 iter->buffer_iter[cpu] =
2171 ring_buffer_read_start(iter->tr->buffer, cpu);
2172 tracing_iter_reset(iter, cpu);
2174 } else {
2175 cpu = iter->cpu_file;
2176 iter->buffer_iter[cpu] =
2177 ring_buffer_read_start(iter->tr->buffer, cpu);
2178 tracing_iter_reset(iter, cpu);
2181 ret = seq_open(file, &tracer_seq_ops);
2182 if (ret < 0) {
2183 fail_ret = ERR_PTR(ret);
2184 goto fail_buffer;
2187 m = file->private_data;
2188 m->private = iter;
2190 mutex_unlock(&trace_types_lock);
2192 return iter;
2194 fail_buffer:
2195 for_each_tracing_cpu(cpu) {
2196 if (iter->buffer_iter[cpu])
2197 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2199 free_cpumask_var(iter->started);
2200 tracing_start();
2201 fail:
2202 mutex_unlock(&trace_types_lock);
2203 kfree(iter->trace);
2204 kfree(iter);
2206 return fail_ret;
2209 int tracing_open_generic(struct inode *inode, struct file *filp)
2211 if (tracing_disabled)
2212 return -ENODEV;
2214 filp->private_data = inode->i_private;
2215 return 0;
2218 static int tracing_release(struct inode *inode, struct file *file)
2220 struct seq_file *m = (struct seq_file *)file->private_data;
2221 struct trace_iterator *iter;
2222 int cpu;
2224 if (!(file->f_mode & FMODE_READ))
2225 return 0;
2227 iter = m->private;
2229 mutex_lock(&trace_types_lock);
2230 for_each_tracing_cpu(cpu) {
2231 if (iter->buffer_iter[cpu])
2232 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2235 if (iter->trace && iter->trace->close)
2236 iter->trace->close(iter);
2238 /* reenable tracing if it was previously enabled */
2239 tracing_start();
2240 mutex_unlock(&trace_types_lock);
2242 seq_release(inode, file);
2243 mutex_destroy(&iter->mutex);
2244 free_cpumask_var(iter->started);
2245 kfree(iter->trace);
2246 kfree(iter);
2247 return 0;
2250 static int tracing_open(struct inode *inode, struct file *file)
2252 struct trace_iterator *iter;
2253 int ret = 0;
2255 /* If this file was open for write, then erase contents */
2256 if ((file->f_mode & FMODE_WRITE) &&
2257 (file->f_flags & O_TRUNC)) {
2258 long cpu = (long) inode->i_private;
2260 if (cpu == TRACE_PIPE_ALL_CPU)
2261 tracing_reset_online_cpus(&global_trace);
2262 else
2263 tracing_reset(&global_trace, cpu);
2266 if (file->f_mode & FMODE_READ) {
2267 iter = __tracing_open(inode, file);
2268 if (IS_ERR(iter))
2269 ret = PTR_ERR(iter);
2270 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2271 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2273 return ret;
2276 static void *
2277 t_next(struct seq_file *m, void *v, loff_t *pos)
2279 struct tracer *t = v;
2281 (*pos)++;
2283 if (t)
2284 t = t->next;
2286 return t;
2289 static void *t_start(struct seq_file *m, loff_t *pos)
2291 struct tracer *t;
2292 loff_t l = 0;
2294 mutex_lock(&trace_types_lock);
2295 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2298 return t;
2301 static void t_stop(struct seq_file *m, void *p)
2303 mutex_unlock(&trace_types_lock);
2306 static int t_show(struct seq_file *m, void *v)
2308 struct tracer *t = v;
2310 if (!t)
2311 return 0;
2313 seq_printf(m, "%s", t->name);
2314 if (t->next)
2315 seq_putc(m, ' ');
2316 else
2317 seq_putc(m, '\n');
2319 return 0;
2322 static const struct seq_operations show_traces_seq_ops = {
2323 .start = t_start,
2324 .next = t_next,
2325 .stop = t_stop,
2326 .show = t_show,
2329 static int show_traces_open(struct inode *inode, struct file *file)
2331 if (tracing_disabled)
2332 return -ENODEV;
2334 return seq_open(file, &show_traces_seq_ops);
2337 static ssize_t
2338 tracing_write_stub(struct file *filp, const char __user *ubuf,
2339 size_t count, loff_t *ppos)
2341 return count;
2344 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2346 if (file->f_mode & FMODE_READ)
2347 return seq_lseek(file, offset, origin);
2348 else
2349 return 0;
2352 static const struct file_operations tracing_fops = {
2353 .open = tracing_open,
2354 .read = seq_read,
2355 .write = tracing_write_stub,
2356 .llseek = tracing_seek,
2357 .release = tracing_release,
2360 static const struct file_operations show_traces_fops = {
2361 .open = show_traces_open,
2362 .read = seq_read,
2363 .release = seq_release,
2367 * Only trace on a CPU if the bitmask is set:
2369 static cpumask_var_t tracing_cpumask;
2372 * The tracer itself will not take this lock, but still we want
2373 * to provide a consistent cpumask to user-space:
2375 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2378 * Temporary storage for the character representation of the
2379 * CPU bitmask (and one more byte for the newline):
2381 static char mask_str[NR_CPUS + 1];
2383 static ssize_t
2384 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2385 size_t count, loff_t *ppos)
2387 int len;
2389 mutex_lock(&tracing_cpumask_update_lock);
2391 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2392 if (count - len < 2) {
2393 count = -EINVAL;
2394 goto out_err;
2396 len += sprintf(mask_str + len, "\n");
2397 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2399 out_err:
2400 mutex_unlock(&tracing_cpumask_update_lock);
2402 return count;
2405 static ssize_t
2406 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2407 size_t count, loff_t *ppos)
2409 int err, cpu;
2410 cpumask_var_t tracing_cpumask_new;
2412 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2413 return -ENOMEM;
2415 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2416 if (err)
2417 goto err_unlock;
2419 mutex_lock(&tracing_cpumask_update_lock);
2421 local_irq_disable();
2422 arch_spin_lock(&ftrace_max_lock);
2423 for_each_tracing_cpu(cpu) {
2425 * Increase/decrease the disabled counter if we are
2426 * about to flip a bit in the cpumask:
2428 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2429 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2430 atomic_inc(&global_trace.data[cpu]->disabled);
2432 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2433 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2434 atomic_dec(&global_trace.data[cpu]->disabled);
2437 arch_spin_unlock(&ftrace_max_lock);
2438 local_irq_enable();
2440 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2442 mutex_unlock(&tracing_cpumask_update_lock);
2443 free_cpumask_var(tracing_cpumask_new);
2445 return count;
2447 err_unlock:
2448 free_cpumask_var(tracing_cpumask_new);
2450 return err;
2453 static const struct file_operations tracing_cpumask_fops = {
2454 .open = tracing_open_generic,
2455 .read = tracing_cpumask_read,
2456 .write = tracing_cpumask_write,
2459 static int tracing_trace_options_show(struct seq_file *m, void *v)
2461 struct tracer_opt *trace_opts;
2462 u32 tracer_flags;
2463 int i;
2465 mutex_lock(&trace_types_lock);
2466 tracer_flags = current_trace->flags->val;
2467 trace_opts = current_trace->flags->opts;
2469 for (i = 0; trace_options[i]; i++) {
2470 if (trace_flags & (1 << i))
2471 seq_printf(m, "%s\n", trace_options[i]);
2472 else
2473 seq_printf(m, "no%s\n", trace_options[i]);
2476 for (i = 0; trace_opts[i].name; i++) {
2477 if (tracer_flags & trace_opts[i].bit)
2478 seq_printf(m, "%s\n", trace_opts[i].name);
2479 else
2480 seq_printf(m, "no%s\n", trace_opts[i].name);
2482 mutex_unlock(&trace_types_lock);
2484 return 0;
2487 static int __set_tracer_option(struct tracer *trace,
2488 struct tracer_flags *tracer_flags,
2489 struct tracer_opt *opts, int neg)
2491 int ret;
2493 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2494 if (ret)
2495 return ret;
2497 if (neg)
2498 tracer_flags->val &= ~opts->bit;
2499 else
2500 tracer_flags->val |= opts->bit;
2501 return 0;
2504 /* Try to assign a tracer specific option */
2505 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2507 struct tracer_flags *tracer_flags = trace->flags;
2508 struct tracer_opt *opts = NULL;
2509 int i;
2511 for (i = 0; tracer_flags->opts[i].name; i++) {
2512 opts = &tracer_flags->opts[i];
2514 if (strcmp(cmp, opts->name) == 0)
2515 return __set_tracer_option(trace, trace->flags,
2516 opts, neg);
2519 return -EINVAL;
2522 static void set_tracer_flags(unsigned int mask, int enabled)
2524 /* do nothing if flag is already set */
2525 if (!!(trace_flags & mask) == !!enabled)
2526 return;
2528 if (enabled)
2529 trace_flags |= mask;
2530 else
2531 trace_flags &= ~mask;
2534 static ssize_t
2535 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2536 size_t cnt, loff_t *ppos)
2538 char buf[64];
2539 char *cmp;
2540 int neg = 0;
2541 int ret;
2542 int i;
2544 if (cnt >= sizeof(buf))
2545 return -EINVAL;
2547 if (copy_from_user(&buf, ubuf, cnt))
2548 return -EFAULT;
2550 buf[cnt] = 0;
2551 cmp = strstrip(buf);
2553 if (strncmp(cmp, "no", 2) == 0) {
2554 neg = 1;
2555 cmp += 2;
2558 for (i = 0; trace_options[i]; i++) {
2559 if (strcmp(cmp, trace_options[i]) == 0) {
2560 set_tracer_flags(1 << i, !neg);
2561 break;
2565 /* If no option could be set, test the specific tracer options */
2566 if (!trace_options[i]) {
2567 mutex_lock(&trace_types_lock);
2568 ret = set_tracer_option(current_trace, cmp, neg);
2569 mutex_unlock(&trace_types_lock);
2570 if (ret)
2571 return ret;
2574 *ppos += cnt;
2576 return cnt;
2579 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2581 if (tracing_disabled)
2582 return -ENODEV;
2583 return single_open(file, tracing_trace_options_show, NULL);
2586 static const struct file_operations tracing_iter_fops = {
2587 .open = tracing_trace_options_open,
2588 .read = seq_read,
2589 .llseek = seq_lseek,
2590 .release = single_release,
2591 .write = tracing_trace_options_write,
2594 static const char readme_msg[] =
2595 "tracing mini-HOWTO:\n\n"
2596 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2597 "# cat /sys/kernel/debug/tracing/available_tracers\n"
2598 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2599 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2600 "nop\n"
2601 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2602 "# cat /sys/kernel/debug/tracing/current_tracer\n"
2603 "sched_switch\n"
2604 "# cat /sys/kernel/debug/tracing/trace_options\n"
2605 "noprint-parent nosym-offset nosym-addr noverbose\n"
2606 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2607 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2608 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2609 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2612 static ssize_t
2613 tracing_readme_read(struct file *filp, char __user *ubuf,
2614 size_t cnt, loff_t *ppos)
2616 return simple_read_from_buffer(ubuf, cnt, ppos,
2617 readme_msg, strlen(readme_msg));
2620 static const struct file_operations tracing_readme_fops = {
2621 .open = tracing_open_generic,
2622 .read = tracing_readme_read,
2625 static ssize_t
2626 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2627 size_t cnt, loff_t *ppos)
2629 char *buf_comm;
2630 char *file_buf;
2631 char *buf;
2632 int len = 0;
2633 int pid;
2634 int i;
2636 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2637 if (!file_buf)
2638 return -ENOMEM;
2640 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2641 if (!buf_comm) {
2642 kfree(file_buf);
2643 return -ENOMEM;
2646 buf = file_buf;
2648 for (i = 0; i < SAVED_CMDLINES; i++) {
2649 int r;
2651 pid = map_cmdline_to_pid[i];
2652 if (pid == -1 || pid == NO_CMDLINE_MAP)
2653 continue;
2655 trace_find_cmdline(pid, buf_comm);
2656 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2657 buf += r;
2658 len += r;
2661 len = simple_read_from_buffer(ubuf, cnt, ppos,
2662 file_buf, len);
2664 kfree(file_buf);
2665 kfree(buf_comm);
2667 return len;
2670 static const struct file_operations tracing_saved_cmdlines_fops = {
2671 .open = tracing_open_generic,
2672 .read = tracing_saved_cmdlines_read,
2675 static ssize_t
2676 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2677 size_t cnt, loff_t *ppos)
2679 char buf[64];
2680 int r;
2682 r = sprintf(buf, "%u\n", tracer_enabled);
2683 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2686 static ssize_t
2687 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2688 size_t cnt, loff_t *ppos)
2690 struct trace_array *tr = filp->private_data;
2691 char buf[64];
2692 unsigned long val;
2693 int ret;
2695 if (cnt >= sizeof(buf))
2696 return -EINVAL;
2698 if (copy_from_user(&buf, ubuf, cnt))
2699 return -EFAULT;
2701 buf[cnt] = 0;
2703 ret = strict_strtoul(buf, 10, &val);
2704 if (ret < 0)
2705 return ret;
2707 val = !!val;
2709 mutex_lock(&trace_types_lock);
2710 if (tracer_enabled ^ val) {
2711 if (val) {
2712 tracer_enabled = 1;
2713 if (current_trace->start)
2714 current_trace->start(tr);
2715 tracing_start();
2716 } else {
2717 tracer_enabled = 0;
2718 tracing_stop();
2719 if (current_trace->stop)
2720 current_trace->stop(tr);
2723 mutex_unlock(&trace_types_lock);
2725 *ppos += cnt;
2727 return cnt;
2730 static ssize_t
2731 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2732 size_t cnt, loff_t *ppos)
2734 char buf[MAX_TRACER_SIZE+2];
2735 int r;
2737 mutex_lock(&trace_types_lock);
2738 if (current_trace)
2739 r = sprintf(buf, "%s\n", current_trace->name);
2740 else
2741 r = sprintf(buf, "\n");
2742 mutex_unlock(&trace_types_lock);
2744 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2747 int tracer_init(struct tracer *t, struct trace_array *tr)
2749 tracing_reset_online_cpus(tr);
2750 return t->init(tr);
2753 static int tracing_resize_ring_buffer(unsigned long size)
2755 int ret;
2758 * If kernel or user changes the size of the ring buffer
2759 * we use the size that was given, and we can forget about
2760 * expanding it later.
2762 ring_buffer_expanded = 1;
2764 ret = ring_buffer_resize(global_trace.buffer, size);
2765 if (ret < 0)
2766 return ret;
2768 ret = ring_buffer_resize(max_tr.buffer, size);
2769 if (ret < 0) {
2770 int r;
2772 r = ring_buffer_resize(global_trace.buffer,
2773 global_trace.entries);
2774 if (r < 0) {
2776 * AARGH! We are left with different
2777 * size max buffer!!!!
2778 * The max buffer is our "snapshot" buffer.
2779 * When a tracer needs a snapshot (one of the
2780 * latency tracers), it swaps the max buffer
2781 * with the saved snap shot. We succeeded to
2782 * update the size of the main buffer, but failed to
2783 * update the size of the max buffer. But when we tried
2784 * to reset the main buffer to the original size, we
2785 * failed there too. This is very unlikely to
2786 * happen, but if it does, warn and kill all
2787 * tracing.
2789 WARN_ON(1);
2790 tracing_disabled = 1;
2792 return ret;
2795 global_trace.entries = size;
2797 return ret;
2801 * tracing_update_buffers - used by tracing facility to expand ring buffers
2803 * To save on memory when the tracing is never used on a system with it
2804 * configured in. The ring buffers are set to a minimum size. But once
2805 * a user starts to use the tracing facility, then they need to grow
2806 * to their default size.
2808 * This function is to be called when a tracer is about to be used.
2810 int tracing_update_buffers(void)
2812 int ret = 0;
2814 mutex_lock(&trace_types_lock);
2815 if (!ring_buffer_expanded)
2816 ret = tracing_resize_ring_buffer(trace_buf_size);
2817 mutex_unlock(&trace_types_lock);
2819 return ret;
2822 struct trace_option_dentry;
2824 static struct trace_option_dentry *
2825 create_trace_option_files(struct tracer *tracer);
2827 static void
2828 destroy_trace_option_files(struct trace_option_dentry *topts);
2830 static int tracing_set_tracer(const char *buf)
2832 static struct trace_option_dentry *topts;
2833 struct trace_array *tr = &global_trace;
2834 struct tracer *t;
2835 int ret = 0;
2837 mutex_lock(&trace_types_lock);
2839 if (!ring_buffer_expanded) {
2840 ret = tracing_resize_ring_buffer(trace_buf_size);
2841 if (ret < 0)
2842 goto out;
2843 ret = 0;
2846 for (t = trace_types; t; t = t->next) {
2847 if (strcmp(t->name, buf) == 0)
2848 break;
2850 if (!t) {
2851 ret = -EINVAL;
2852 goto out;
2854 if (t == current_trace)
2855 goto out;
2857 trace_branch_disable();
2858 if (current_trace && current_trace->reset)
2859 current_trace->reset(tr);
2861 destroy_trace_option_files(topts);
2863 current_trace = t;
2865 topts = create_trace_option_files(current_trace);
2867 if (t->init) {
2868 ret = tracer_init(t, tr);
2869 if (ret)
2870 goto out;
2873 trace_branch_enable(tr);
2874 out:
2875 mutex_unlock(&trace_types_lock);
2877 return ret;
2880 static ssize_t
2881 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2882 size_t cnt, loff_t *ppos)
2884 char buf[MAX_TRACER_SIZE+1];
2885 int i;
2886 size_t ret;
2887 int err;
2889 ret = cnt;
2891 if (cnt > MAX_TRACER_SIZE)
2892 cnt = MAX_TRACER_SIZE;
2894 if (copy_from_user(&buf, ubuf, cnt))
2895 return -EFAULT;
2897 buf[cnt] = 0;
2899 /* strip ending whitespace. */
2900 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2901 buf[i] = 0;
2903 err = tracing_set_tracer(buf);
2904 if (err)
2905 return err;
2907 *ppos += ret;
2909 return ret;
2912 static ssize_t
2913 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2914 size_t cnt, loff_t *ppos)
2916 unsigned long *ptr = filp->private_data;
2917 char buf[64];
2918 int r;
2920 r = snprintf(buf, sizeof(buf), "%ld\n",
2921 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2922 if (r > sizeof(buf))
2923 r = sizeof(buf);
2924 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2927 static ssize_t
2928 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2929 size_t cnt, loff_t *ppos)
2931 unsigned long *ptr = filp->private_data;
2932 char buf[64];
2933 unsigned long val;
2934 int ret;
2936 if (cnt >= sizeof(buf))
2937 return -EINVAL;
2939 if (copy_from_user(&buf, ubuf, cnt))
2940 return -EFAULT;
2942 buf[cnt] = 0;
2944 ret = strict_strtoul(buf, 10, &val);
2945 if (ret < 0)
2946 return ret;
2948 *ptr = val * 1000;
2950 return cnt;
2953 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2955 long cpu_file = (long) inode->i_private;
2956 struct trace_iterator *iter;
2957 int ret = 0;
2959 if (tracing_disabled)
2960 return -ENODEV;
2962 mutex_lock(&trace_types_lock);
2964 /* create a buffer to store the information to pass to userspace */
2965 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2966 if (!iter) {
2967 ret = -ENOMEM;
2968 goto out;
2972 * We make a copy of the current tracer to avoid concurrent
2973 * changes on it while we are reading.
2975 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2976 if (!iter->trace) {
2977 ret = -ENOMEM;
2978 goto fail;
2980 if (current_trace)
2981 *iter->trace = *current_trace;
2983 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2984 ret = -ENOMEM;
2985 goto fail;
2988 /* trace pipe does not show start of buffer */
2989 cpumask_setall(iter->started);
2991 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2992 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2994 iter->cpu_file = cpu_file;
2995 iter->tr = &global_trace;
2996 mutex_init(&iter->mutex);
2997 filp->private_data = iter;
2999 if (iter->trace->pipe_open)
3000 iter->trace->pipe_open(iter);
3002 out:
3003 mutex_unlock(&trace_types_lock);
3004 return ret;
3006 fail:
3007 kfree(iter->trace);
3008 kfree(iter);
3009 mutex_unlock(&trace_types_lock);
3010 return ret;
3013 static int tracing_release_pipe(struct inode *inode, struct file *file)
3015 struct trace_iterator *iter = file->private_data;
3017 mutex_lock(&trace_types_lock);
3019 if (iter->trace->pipe_close)
3020 iter->trace->pipe_close(iter);
3022 mutex_unlock(&trace_types_lock);
3024 free_cpumask_var(iter->started);
3025 mutex_destroy(&iter->mutex);
3026 kfree(iter->trace);
3027 kfree(iter);
3029 return 0;
3032 static unsigned int
3033 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3035 struct trace_iterator *iter = filp->private_data;
3037 if (trace_flags & TRACE_ITER_BLOCK) {
3039 * Always select as readable when in blocking mode
3041 return POLLIN | POLLRDNORM;
3042 } else {
3043 if (!trace_empty(iter))
3044 return POLLIN | POLLRDNORM;
3045 poll_wait(filp, &trace_wait, poll_table);
3046 if (!trace_empty(iter))
3047 return POLLIN | POLLRDNORM;
3049 return 0;
3054 void default_wait_pipe(struct trace_iterator *iter)
3056 DEFINE_WAIT(wait);
3058 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3060 if (trace_empty(iter))
3061 schedule();
3063 finish_wait(&trace_wait, &wait);
3067 * This is a make-shift waitqueue.
3068 * A tracer might use this callback on some rare cases:
3070 * 1) the current tracer might hold the runqueue lock when it wakes up
3071 * a reader, hence a deadlock (sched, function, and function graph tracers)
3072 * 2) the function tracers, trace all functions, we don't want
3073 * the overhead of calling wake_up and friends
3074 * (and tracing them too)
3076 * Anyway, this is really very primitive wakeup.
3078 void poll_wait_pipe(struct trace_iterator *iter)
3080 set_current_state(TASK_INTERRUPTIBLE);
3081 /* sleep for 100 msecs, and try again. */
3082 schedule_timeout(HZ / 10);
3085 /* Must be called with trace_types_lock mutex held. */
3086 static int tracing_wait_pipe(struct file *filp)
3088 struct trace_iterator *iter = filp->private_data;
3090 while (trace_empty(iter)) {
3092 if ((filp->f_flags & O_NONBLOCK)) {
3093 return -EAGAIN;
3096 mutex_unlock(&iter->mutex);
3098 iter->trace->wait_pipe(iter);
3100 mutex_lock(&iter->mutex);
3102 if (signal_pending(current))
3103 return -EINTR;
3106 * We block until we read something and tracing is disabled.
3107 * We still block if tracing is disabled, but we have never
3108 * read anything. This allows a user to cat this file, and
3109 * then enable tracing. But after we have read something,
3110 * we give an EOF when tracing is again disabled.
3112 * iter->pos will be 0 if we haven't read anything.
3114 if (!tracer_enabled && iter->pos)
3115 break;
3118 return 1;
3122 * Consumer reader.
3124 static ssize_t
3125 tracing_read_pipe(struct file *filp, char __user *ubuf,
3126 size_t cnt, loff_t *ppos)
3128 struct trace_iterator *iter = filp->private_data;
3129 static struct tracer *old_tracer;
3130 ssize_t sret;
3132 /* return any leftover data */
3133 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3134 if (sret != -EBUSY)
3135 return sret;
3137 trace_seq_init(&iter->seq);
3139 /* copy the tracer to avoid using a global lock all around */
3140 mutex_lock(&trace_types_lock);
3141 if (unlikely(old_tracer != current_trace && current_trace)) {
3142 old_tracer = current_trace;
3143 *iter->trace = *current_trace;
3145 mutex_unlock(&trace_types_lock);
3148 * Avoid more than one consumer on a single file descriptor
3149 * This is just a matter of traces coherency, the ring buffer itself
3150 * is protected.
3152 mutex_lock(&iter->mutex);
3153 if (iter->trace->read) {
3154 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3155 if (sret)
3156 goto out;
3159 waitagain:
3160 sret = tracing_wait_pipe(filp);
3161 if (sret <= 0)
3162 goto out;
3164 /* stop when tracing is finished */
3165 if (trace_empty(iter)) {
3166 sret = 0;
3167 goto out;
3170 if (cnt >= PAGE_SIZE)
3171 cnt = PAGE_SIZE - 1;
3173 /* reset all but tr, trace, and overruns */
3174 memset(&iter->seq, 0,
3175 sizeof(struct trace_iterator) -
3176 offsetof(struct trace_iterator, seq));
3177 iter->pos = -1;
3179 trace_event_read_lock();
3180 trace_access_lock(iter->cpu_file);
3181 while (find_next_entry_inc(iter) != NULL) {
3182 enum print_line_t ret;
3183 int len = iter->seq.len;
3185 ret = print_trace_line(iter);
3186 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3187 /* don't print partial lines */
3188 iter->seq.len = len;
3189 break;
3191 if (ret != TRACE_TYPE_NO_CONSUME)
3192 trace_consume(iter);
3194 if (iter->seq.len >= cnt)
3195 break;
3197 trace_access_unlock(iter->cpu_file);
3198 trace_event_read_unlock();
3200 /* Now copy what we have to the user */
3201 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3202 if (iter->seq.readpos >= iter->seq.len)
3203 trace_seq_init(&iter->seq);
3206 * If there was nothing to send to user, inspite of consuming trace
3207 * entries, go back to wait for more entries.
3209 if (sret == -EBUSY)
3210 goto waitagain;
3212 out:
3213 mutex_unlock(&iter->mutex);
3215 return sret;
3218 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3219 struct pipe_buffer *buf)
3221 __free_page(buf->page);
3224 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3225 unsigned int idx)
3227 __free_page(spd->pages[idx]);
3230 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3231 .can_merge = 0,
3232 .map = generic_pipe_buf_map,
3233 .unmap = generic_pipe_buf_unmap,
3234 .confirm = generic_pipe_buf_confirm,
3235 .release = tracing_pipe_buf_release,
3236 .steal = generic_pipe_buf_steal,
3237 .get = generic_pipe_buf_get,
3240 static size_t
3241 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3243 size_t count;
3244 int ret;
3246 /* Seq buffer is page-sized, exactly what we need. */
3247 for (;;) {
3248 count = iter->seq.len;
3249 ret = print_trace_line(iter);
3250 count = iter->seq.len - count;
3251 if (rem < count) {
3252 rem = 0;
3253 iter->seq.len -= count;
3254 break;
3256 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3257 iter->seq.len -= count;
3258 break;
3261 if (ret != TRACE_TYPE_NO_CONSUME)
3262 trace_consume(iter);
3263 rem -= count;
3264 if (!find_next_entry_inc(iter)) {
3265 rem = 0;
3266 iter->ent = NULL;
3267 break;
3271 return rem;
3274 static ssize_t tracing_splice_read_pipe(struct file *filp,
3275 loff_t *ppos,
3276 struct pipe_inode_info *pipe,
3277 size_t len,
3278 unsigned int flags)
3280 struct page *pages[PIPE_BUFFERS];
3281 struct partial_page partial[PIPE_BUFFERS];
3282 struct trace_iterator *iter = filp->private_data;
3283 struct splice_pipe_desc spd = {
3284 .pages = pages,
3285 .partial = partial,
3286 .nr_pages = 0, /* This gets updated below. */
3287 .flags = flags,
3288 .ops = &tracing_pipe_buf_ops,
3289 .spd_release = tracing_spd_release_pipe,
3291 static struct tracer *old_tracer;
3292 ssize_t ret;
3293 size_t rem;
3294 unsigned int i;
3296 /* copy the tracer to avoid using a global lock all around */
3297 mutex_lock(&trace_types_lock);
3298 if (unlikely(old_tracer != current_trace && current_trace)) {
3299 old_tracer = current_trace;
3300 *iter->trace = *current_trace;
3302 mutex_unlock(&trace_types_lock);
3304 mutex_lock(&iter->mutex);
3306 if (iter->trace->splice_read) {
3307 ret = iter->trace->splice_read(iter, filp,
3308 ppos, pipe, len, flags);
3309 if (ret)
3310 goto out_err;
3313 ret = tracing_wait_pipe(filp);
3314 if (ret <= 0)
3315 goto out_err;
3317 if (!iter->ent && !find_next_entry_inc(iter)) {
3318 ret = -EFAULT;
3319 goto out_err;
3322 trace_event_read_lock();
3323 trace_access_lock(iter->cpu_file);
3325 /* Fill as many pages as possible. */
3326 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3327 pages[i] = alloc_page(GFP_KERNEL);
3328 if (!pages[i])
3329 break;
3331 rem = tracing_fill_pipe_page(rem, iter);
3333 /* Copy the data into the page, so we can start over. */
3334 ret = trace_seq_to_buffer(&iter->seq,
3335 page_address(pages[i]),
3336 iter->seq.len);
3337 if (ret < 0) {
3338 __free_page(pages[i]);
3339 break;
3341 partial[i].offset = 0;
3342 partial[i].len = iter->seq.len;
3344 trace_seq_init(&iter->seq);
3347 trace_access_unlock(iter->cpu_file);
3348 trace_event_read_unlock();
3349 mutex_unlock(&iter->mutex);
3351 spd.nr_pages = i;
3353 return splice_to_pipe(pipe, &spd);
3355 out_err:
3356 mutex_unlock(&iter->mutex);
3358 return ret;
3361 static ssize_t
3362 tracing_entries_read(struct file *filp, char __user *ubuf,
3363 size_t cnt, loff_t *ppos)
3365 struct trace_array *tr = filp->private_data;
3366 char buf[96];
3367 int r;
3369 mutex_lock(&trace_types_lock);
3370 if (!ring_buffer_expanded)
3371 r = sprintf(buf, "%lu (expanded: %lu)\n",
3372 tr->entries >> 10,
3373 trace_buf_size >> 10);
3374 else
3375 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3376 mutex_unlock(&trace_types_lock);
3378 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3381 static ssize_t
3382 tracing_entries_write(struct file *filp, const char __user *ubuf,
3383 size_t cnt, loff_t *ppos)
3385 unsigned long val;
3386 char buf[64];
3387 int ret, cpu;
3389 if (cnt >= sizeof(buf))
3390 return -EINVAL;
3392 if (copy_from_user(&buf, ubuf, cnt))
3393 return -EFAULT;
3395 buf[cnt] = 0;
3397 ret = strict_strtoul(buf, 10, &val);
3398 if (ret < 0)
3399 return ret;
3401 /* must have at least 1 entry */
3402 if (!val)
3403 return -EINVAL;
3405 mutex_lock(&trace_types_lock);
3407 tracing_stop();
3409 /* disable all cpu buffers */
3410 for_each_tracing_cpu(cpu) {
3411 if (global_trace.data[cpu])
3412 atomic_inc(&global_trace.data[cpu]->disabled);
3413 if (max_tr.data[cpu])
3414 atomic_inc(&max_tr.data[cpu]->disabled);
3417 /* value is in KB */
3418 val <<= 10;
3420 if (val != global_trace.entries) {
3421 ret = tracing_resize_ring_buffer(val);
3422 if (ret < 0) {
3423 cnt = ret;
3424 goto out;
3428 *ppos += cnt;
3430 /* If check pages failed, return ENOMEM */
3431 if (tracing_disabled)
3432 cnt = -ENOMEM;
3433 out:
3434 for_each_tracing_cpu(cpu) {
3435 if (global_trace.data[cpu])
3436 atomic_dec(&global_trace.data[cpu]->disabled);
3437 if (max_tr.data[cpu])
3438 atomic_dec(&max_tr.data[cpu]->disabled);
3441 tracing_start();
3442 max_tr.entries = global_trace.entries;
3443 mutex_unlock(&trace_types_lock);
3445 return cnt;
3448 static int mark_printk(const char *fmt, ...)
3450 int ret;
3451 va_list args;
3452 va_start(args, fmt);
3453 ret = trace_vprintk(0, fmt, args);
3454 va_end(args);
3455 return ret;
3458 static ssize_t
3459 tracing_mark_write(struct file *filp, const char __user *ubuf,
3460 size_t cnt, loff_t *fpos)
3462 char *buf;
3464 if (tracing_disabled)
3465 return -EINVAL;
3467 if (cnt > TRACE_BUF_SIZE)
3468 cnt = TRACE_BUF_SIZE;
3470 buf = kmalloc(cnt + 2, GFP_KERNEL);
3471 if (buf == NULL)
3472 return -ENOMEM;
3474 if (copy_from_user(buf, ubuf, cnt)) {
3475 kfree(buf);
3476 return -EFAULT;
3478 if (buf[cnt-1] != '\n') {
3479 buf[cnt] = '\n';
3480 buf[cnt+1] = '\0';
3481 } else
3482 buf[cnt] = '\0';
3484 cnt = mark_printk("%s", buf);
3485 kfree(buf);
3486 *fpos += cnt;
3488 return cnt;
3491 static int tracing_clock_show(struct seq_file *m, void *v)
3493 int i;
3495 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3496 seq_printf(m,
3497 "%s%s%s%s", i ? " " : "",
3498 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3499 i == trace_clock_id ? "]" : "");
3500 seq_putc(m, '\n');
3502 return 0;
3505 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3506 size_t cnt, loff_t *fpos)
3508 char buf[64];
3509 const char *clockstr;
3510 int i;
3512 if (cnt >= sizeof(buf))
3513 return -EINVAL;
3515 if (copy_from_user(&buf, ubuf, cnt))
3516 return -EFAULT;
3518 buf[cnt] = 0;
3520 clockstr = strstrip(buf);
3522 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3523 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3524 break;
3526 if (i == ARRAY_SIZE(trace_clocks))
3527 return -EINVAL;
3529 trace_clock_id = i;
3531 mutex_lock(&trace_types_lock);
3533 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3534 if (max_tr.buffer)
3535 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3537 mutex_unlock(&trace_types_lock);
3539 *fpos += cnt;
3541 return cnt;
3544 static int tracing_clock_open(struct inode *inode, struct file *file)
3546 if (tracing_disabled)
3547 return -ENODEV;
3548 return single_open(file, tracing_clock_show, NULL);
3551 static const struct file_operations tracing_max_lat_fops = {
3552 .open = tracing_open_generic,
3553 .read = tracing_max_lat_read,
3554 .write = tracing_max_lat_write,
3557 static const struct file_operations tracing_ctrl_fops = {
3558 .open = tracing_open_generic,
3559 .read = tracing_ctrl_read,
3560 .write = tracing_ctrl_write,
3563 static const struct file_operations set_tracer_fops = {
3564 .open = tracing_open_generic,
3565 .read = tracing_set_trace_read,
3566 .write = tracing_set_trace_write,
3569 static const struct file_operations tracing_pipe_fops = {
3570 .open = tracing_open_pipe,
3571 .poll = tracing_poll_pipe,
3572 .read = tracing_read_pipe,
3573 .splice_read = tracing_splice_read_pipe,
3574 .release = tracing_release_pipe,
3577 static const struct file_operations tracing_entries_fops = {
3578 .open = tracing_open_generic,
3579 .read = tracing_entries_read,
3580 .write = tracing_entries_write,
3583 static const struct file_operations tracing_mark_fops = {
3584 .open = tracing_open_generic,
3585 .write = tracing_mark_write,
3588 static const struct file_operations trace_clock_fops = {
3589 .open = tracing_clock_open,
3590 .read = seq_read,
3591 .llseek = seq_lseek,
3592 .release = single_release,
3593 .write = tracing_clock_write,
3596 struct ftrace_buffer_info {
3597 struct trace_array *tr;
3598 void *spare;
3599 int cpu;
3600 unsigned int read;
3603 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3605 int cpu = (int)(long)inode->i_private;
3606 struct ftrace_buffer_info *info;
3608 if (tracing_disabled)
3609 return -ENODEV;
3611 info = kzalloc(sizeof(*info), GFP_KERNEL);
3612 if (!info)
3613 return -ENOMEM;
3615 info->tr = &global_trace;
3616 info->cpu = cpu;
3617 info->spare = NULL;
3618 /* Force reading ring buffer for first read */
3619 info->read = (unsigned int)-1;
3621 filp->private_data = info;
3623 return nonseekable_open(inode, filp);
3626 static ssize_t
3627 tracing_buffers_read(struct file *filp, char __user *ubuf,
3628 size_t count, loff_t *ppos)
3630 struct ftrace_buffer_info *info = filp->private_data;
3631 unsigned int pos;
3632 ssize_t ret;
3633 size_t size;
3635 if (!count)
3636 return 0;
3638 if (!info->spare)
3639 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3640 if (!info->spare)
3641 return -ENOMEM;
3643 /* Do we have previous read data to read? */
3644 if (info->read < PAGE_SIZE)
3645 goto read;
3647 info->read = 0;
3649 trace_access_lock(info->cpu);
3650 ret = ring_buffer_read_page(info->tr->buffer,
3651 &info->spare,
3652 count,
3653 info->cpu, 0);
3654 trace_access_unlock(info->cpu);
3655 if (ret < 0)
3656 return 0;
3658 pos = ring_buffer_page_len(info->spare);
3660 if (pos < PAGE_SIZE)
3661 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3663 read:
3664 size = PAGE_SIZE - info->read;
3665 if (size > count)
3666 size = count;
3668 ret = copy_to_user(ubuf, info->spare + info->read, size);
3669 if (ret == size)
3670 return -EFAULT;
3671 size -= ret;
3673 *ppos += size;
3674 info->read += size;
3676 return size;
3679 static int tracing_buffers_release(struct inode *inode, struct file *file)
3681 struct ftrace_buffer_info *info = file->private_data;
3683 if (info->spare)
3684 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3685 kfree(info);
3687 return 0;
3690 struct buffer_ref {
3691 struct ring_buffer *buffer;
3692 void *page;
3693 int ref;
3696 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3697 struct pipe_buffer *buf)
3699 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3701 if (--ref->ref)
3702 return;
3704 ring_buffer_free_read_page(ref->buffer, ref->page);
3705 kfree(ref);
3706 buf->private = 0;
3709 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3710 struct pipe_buffer *buf)
3712 return 1;
3715 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3716 struct pipe_buffer *buf)
3718 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3720 ref->ref++;
3723 /* Pipe buffer operations for a buffer. */
3724 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
3725 .can_merge = 0,
3726 .map = generic_pipe_buf_map,
3727 .unmap = generic_pipe_buf_unmap,
3728 .confirm = generic_pipe_buf_confirm,
3729 .release = buffer_pipe_buf_release,
3730 .steal = buffer_pipe_buf_steal,
3731 .get = buffer_pipe_buf_get,
3735 * Callback from splice_to_pipe(), if we need to release some pages
3736 * at the end of the spd in case we error'ed out in filling the pipe.
3738 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3740 struct buffer_ref *ref =
3741 (struct buffer_ref *)spd->partial[i].private;
3743 if (--ref->ref)
3744 return;
3746 ring_buffer_free_read_page(ref->buffer, ref->page);
3747 kfree(ref);
3748 spd->partial[i].private = 0;
3751 static ssize_t
3752 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3753 struct pipe_inode_info *pipe, size_t len,
3754 unsigned int flags)
3756 struct ftrace_buffer_info *info = file->private_data;
3757 struct partial_page partial[PIPE_BUFFERS];
3758 struct page *pages[PIPE_BUFFERS];
3759 struct splice_pipe_desc spd = {
3760 .pages = pages,
3761 .partial = partial,
3762 .flags = flags,
3763 .ops = &buffer_pipe_buf_ops,
3764 .spd_release = buffer_spd_release,
3766 struct buffer_ref *ref;
3767 int entries, size, i;
3768 size_t ret;
3770 if (*ppos & (PAGE_SIZE - 1)) {
3771 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3772 return -EINVAL;
3775 if (len & (PAGE_SIZE - 1)) {
3776 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3777 if (len < PAGE_SIZE)
3778 return -EINVAL;
3779 len &= PAGE_MASK;
3782 trace_access_lock(info->cpu);
3783 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3785 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
3786 struct page *page;
3787 int r;
3789 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3790 if (!ref)
3791 break;
3793 ref->ref = 1;
3794 ref->buffer = info->tr->buffer;
3795 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3796 if (!ref->page) {
3797 kfree(ref);
3798 break;
3801 r = ring_buffer_read_page(ref->buffer, &ref->page,
3802 len, info->cpu, 1);
3803 if (r < 0) {
3804 ring_buffer_free_read_page(ref->buffer,
3805 ref->page);
3806 kfree(ref);
3807 break;
3811 * zero out any left over data, this is going to
3812 * user land.
3814 size = ring_buffer_page_len(ref->page);
3815 if (size < PAGE_SIZE)
3816 memset(ref->page + size, 0, PAGE_SIZE - size);
3818 page = virt_to_page(ref->page);
3820 spd.pages[i] = page;
3821 spd.partial[i].len = PAGE_SIZE;
3822 spd.partial[i].offset = 0;
3823 spd.partial[i].private = (unsigned long)ref;
3824 spd.nr_pages++;
3825 *ppos += PAGE_SIZE;
3827 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3830 trace_access_unlock(info->cpu);
3831 spd.nr_pages = i;
3833 /* did we read anything? */
3834 if (!spd.nr_pages) {
3835 if (flags & SPLICE_F_NONBLOCK)
3836 ret = -EAGAIN;
3837 else
3838 ret = 0;
3839 /* TODO: block */
3840 return ret;
3843 ret = splice_to_pipe(pipe, &spd);
3845 return ret;
3848 static const struct file_operations tracing_buffers_fops = {
3849 .open = tracing_buffers_open,
3850 .read = tracing_buffers_read,
3851 .release = tracing_buffers_release,
3852 .splice_read = tracing_buffers_splice_read,
3853 .llseek = no_llseek,
3856 static ssize_t
3857 tracing_stats_read(struct file *filp, char __user *ubuf,
3858 size_t count, loff_t *ppos)
3860 unsigned long cpu = (unsigned long)filp->private_data;
3861 struct trace_array *tr = &global_trace;
3862 struct trace_seq *s;
3863 unsigned long cnt;
3865 s = kmalloc(sizeof(*s), GFP_KERNEL);
3866 if (!s)
3867 return -ENOMEM;
3869 trace_seq_init(s);
3871 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3872 trace_seq_printf(s, "entries: %ld\n", cnt);
3874 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3875 trace_seq_printf(s, "overrun: %ld\n", cnt);
3877 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3878 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3880 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3882 kfree(s);
3884 return count;
3887 static const struct file_operations tracing_stats_fops = {
3888 .open = tracing_open_generic,
3889 .read = tracing_stats_read,
3892 #ifdef CONFIG_DYNAMIC_FTRACE
3894 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3896 return 0;
3899 static ssize_t
3900 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3901 size_t cnt, loff_t *ppos)
3903 static char ftrace_dyn_info_buffer[1024];
3904 static DEFINE_MUTEX(dyn_info_mutex);
3905 unsigned long *p = filp->private_data;
3906 char *buf = ftrace_dyn_info_buffer;
3907 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3908 int r;
3910 mutex_lock(&dyn_info_mutex);
3911 r = sprintf(buf, "%ld ", *p);
3913 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3914 buf[r++] = '\n';
3916 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3918 mutex_unlock(&dyn_info_mutex);
3920 return r;
3923 static const struct file_operations tracing_dyn_info_fops = {
3924 .open = tracing_open_generic,
3925 .read = tracing_read_dyn_info,
3927 #endif
3929 static struct dentry *d_tracer;
3931 struct dentry *tracing_init_dentry(void)
3933 static int once;
3935 if (d_tracer)
3936 return d_tracer;
3938 if (!debugfs_initialized())
3939 return NULL;
3941 d_tracer = debugfs_create_dir("tracing", NULL);
3943 if (!d_tracer && !once) {
3944 once = 1;
3945 pr_warning("Could not create debugfs directory 'tracing'\n");
3946 return NULL;
3949 return d_tracer;
3952 static struct dentry *d_percpu;
3954 struct dentry *tracing_dentry_percpu(void)
3956 static int once;
3957 struct dentry *d_tracer;
3959 if (d_percpu)
3960 return d_percpu;
3962 d_tracer = tracing_init_dentry();
3964 if (!d_tracer)
3965 return NULL;
3967 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3969 if (!d_percpu && !once) {
3970 once = 1;
3971 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3972 return NULL;
3975 return d_percpu;
3978 static void tracing_init_debugfs_percpu(long cpu)
3980 struct dentry *d_percpu = tracing_dentry_percpu();
3981 struct dentry *d_cpu;
3982 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3983 char cpu_dir[7];
3985 if (cpu > 999 || cpu < 0)
3986 return;
3988 sprintf(cpu_dir, "cpu%ld", cpu);
3989 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3990 if (!d_cpu) {
3991 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3992 return;
3995 /* per cpu trace_pipe */
3996 trace_create_file("trace_pipe", 0444, d_cpu,
3997 (void *) cpu, &tracing_pipe_fops);
3999 /* per cpu trace */
4000 trace_create_file("trace", 0644, d_cpu,
4001 (void *) cpu, &tracing_fops);
4003 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4004 (void *) cpu, &tracing_buffers_fops);
4006 trace_create_file("stats", 0444, d_cpu,
4007 (void *) cpu, &tracing_stats_fops);
4010 #ifdef CONFIG_FTRACE_SELFTEST
4011 /* Let selftest have access to static functions in this file */
4012 #include "trace_selftest.c"
4013 #endif
4015 struct trace_option_dentry {
4016 struct tracer_opt *opt;
4017 struct tracer_flags *flags;
4018 struct dentry *entry;
4021 static ssize_t
4022 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4023 loff_t *ppos)
4025 struct trace_option_dentry *topt = filp->private_data;
4026 char *buf;
4028 if (topt->flags->val & topt->opt->bit)
4029 buf = "1\n";
4030 else
4031 buf = "0\n";
4033 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4036 static ssize_t
4037 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4038 loff_t *ppos)
4040 struct trace_option_dentry *topt = filp->private_data;
4041 unsigned long val;
4042 char buf[64];
4043 int ret;
4045 if (cnt >= sizeof(buf))
4046 return -EINVAL;
4048 if (copy_from_user(&buf, ubuf, cnt))
4049 return -EFAULT;
4051 buf[cnt] = 0;
4053 ret = strict_strtoul(buf, 10, &val);
4054 if (ret < 0)
4055 return ret;
4057 if (val != 0 && val != 1)
4058 return -EINVAL;
4060 if (!!(topt->flags->val & topt->opt->bit) != val) {
4061 mutex_lock(&trace_types_lock);
4062 ret = __set_tracer_option(current_trace, topt->flags,
4063 topt->opt, !val);
4064 mutex_unlock(&trace_types_lock);
4065 if (ret)
4066 return ret;
4069 *ppos += cnt;
4071 return cnt;
4075 static const struct file_operations trace_options_fops = {
4076 .open = tracing_open_generic,
4077 .read = trace_options_read,
4078 .write = trace_options_write,
4081 static ssize_t
4082 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4083 loff_t *ppos)
4085 long index = (long)filp->private_data;
4086 char *buf;
4088 if (trace_flags & (1 << index))
4089 buf = "1\n";
4090 else
4091 buf = "0\n";
4093 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4096 static ssize_t
4097 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4098 loff_t *ppos)
4100 long index = (long)filp->private_data;
4101 char buf[64];
4102 unsigned long val;
4103 int ret;
4105 if (cnt >= sizeof(buf))
4106 return -EINVAL;
4108 if (copy_from_user(&buf, ubuf, cnt))
4109 return -EFAULT;
4111 buf[cnt] = 0;
4113 ret = strict_strtoul(buf, 10, &val);
4114 if (ret < 0)
4115 return ret;
4117 if (val != 0 && val != 1)
4118 return -EINVAL;
4119 set_tracer_flags(1 << index, val);
4121 *ppos += cnt;
4123 return cnt;
4126 static const struct file_operations trace_options_core_fops = {
4127 .open = tracing_open_generic,
4128 .read = trace_options_core_read,
4129 .write = trace_options_core_write,
4132 struct dentry *trace_create_file(const char *name,
4133 mode_t mode,
4134 struct dentry *parent,
4135 void *data,
4136 const struct file_operations *fops)
4138 struct dentry *ret;
4140 ret = debugfs_create_file(name, mode, parent, data, fops);
4141 if (!ret)
4142 pr_warning("Could not create debugfs '%s' entry\n", name);
4144 return ret;
4148 static struct dentry *trace_options_init_dentry(void)
4150 struct dentry *d_tracer;
4151 static struct dentry *t_options;
4153 if (t_options)
4154 return t_options;
4156 d_tracer = tracing_init_dentry();
4157 if (!d_tracer)
4158 return NULL;
4160 t_options = debugfs_create_dir("options", d_tracer);
4161 if (!t_options) {
4162 pr_warning("Could not create debugfs directory 'options'\n");
4163 return NULL;
4166 return t_options;
4169 static void
4170 create_trace_option_file(struct trace_option_dentry *topt,
4171 struct tracer_flags *flags,
4172 struct tracer_opt *opt)
4174 struct dentry *t_options;
4176 t_options = trace_options_init_dentry();
4177 if (!t_options)
4178 return;
4180 topt->flags = flags;
4181 topt->opt = opt;
4183 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4184 &trace_options_fops);
4188 static struct trace_option_dentry *
4189 create_trace_option_files(struct tracer *tracer)
4191 struct trace_option_dentry *topts;
4192 struct tracer_flags *flags;
4193 struct tracer_opt *opts;
4194 int cnt;
4196 if (!tracer)
4197 return NULL;
4199 flags = tracer->flags;
4201 if (!flags || !flags->opts)
4202 return NULL;
4204 opts = flags->opts;
4206 for (cnt = 0; opts[cnt].name; cnt++)
4209 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4210 if (!topts)
4211 return NULL;
4213 for (cnt = 0; opts[cnt].name; cnt++)
4214 create_trace_option_file(&topts[cnt], flags,
4215 &opts[cnt]);
4217 return topts;
4220 static void
4221 destroy_trace_option_files(struct trace_option_dentry *topts)
4223 int cnt;
4225 if (!topts)
4226 return;
4228 for (cnt = 0; topts[cnt].opt; cnt++) {
4229 if (topts[cnt].entry)
4230 debugfs_remove(topts[cnt].entry);
4233 kfree(topts);
4236 static struct dentry *
4237 create_trace_option_core_file(const char *option, long index)
4239 struct dentry *t_options;
4241 t_options = trace_options_init_dentry();
4242 if (!t_options)
4243 return NULL;
4245 return trace_create_file(option, 0644, t_options, (void *)index,
4246 &trace_options_core_fops);
4249 static __init void create_trace_options_dir(void)
4251 struct dentry *t_options;
4252 int i;
4254 t_options = trace_options_init_dentry();
4255 if (!t_options)
4256 return;
4258 for (i = 0; trace_options[i]; i++)
4259 create_trace_option_core_file(trace_options[i], i);
4262 static __init int tracer_init_debugfs(void)
4264 struct dentry *d_tracer;
4265 int cpu;
4267 trace_access_lock_init();
4269 d_tracer = tracing_init_dentry();
4271 trace_create_file("tracing_enabled", 0644, d_tracer,
4272 &global_trace, &tracing_ctrl_fops);
4274 trace_create_file("trace_options", 0644, d_tracer,
4275 NULL, &tracing_iter_fops);
4277 trace_create_file("tracing_cpumask", 0644, d_tracer,
4278 NULL, &tracing_cpumask_fops);
4280 trace_create_file("trace", 0644, d_tracer,
4281 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4283 trace_create_file("available_tracers", 0444, d_tracer,
4284 &global_trace, &show_traces_fops);
4286 trace_create_file("current_tracer", 0644, d_tracer,
4287 &global_trace, &set_tracer_fops);
4289 #ifdef CONFIG_TRACER_MAX_TRACE
4290 trace_create_file("tracing_max_latency", 0644, d_tracer,
4291 &tracing_max_latency, &tracing_max_lat_fops);
4292 #endif
4294 trace_create_file("tracing_thresh", 0644, d_tracer,
4295 &tracing_thresh, &tracing_max_lat_fops);
4297 trace_create_file("README", 0444, d_tracer,
4298 NULL, &tracing_readme_fops);
4300 trace_create_file("trace_pipe", 0444, d_tracer,
4301 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4303 trace_create_file("buffer_size_kb", 0644, d_tracer,
4304 &global_trace, &tracing_entries_fops);
4306 trace_create_file("trace_marker", 0220, d_tracer,
4307 NULL, &tracing_mark_fops);
4309 trace_create_file("saved_cmdlines", 0444, d_tracer,
4310 NULL, &tracing_saved_cmdlines_fops);
4312 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4313 &trace_clock_fops);
4315 #ifdef CONFIG_DYNAMIC_FTRACE
4316 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4317 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4318 #endif
4319 #ifdef CONFIG_SYSPROF_TRACER
4320 init_tracer_sysprof_debugfs(d_tracer);
4321 #endif
4323 create_trace_options_dir();
4325 for_each_tracing_cpu(cpu)
4326 tracing_init_debugfs_percpu(cpu);
4328 return 0;
4331 static int trace_panic_handler(struct notifier_block *this,
4332 unsigned long event, void *unused)
4334 if (ftrace_dump_on_oops)
4335 ftrace_dump();
4336 return NOTIFY_OK;
4339 static struct notifier_block trace_panic_notifier = {
4340 .notifier_call = trace_panic_handler,
4341 .next = NULL,
4342 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4345 static int trace_die_handler(struct notifier_block *self,
4346 unsigned long val,
4347 void *data)
4349 switch (val) {
4350 case DIE_OOPS:
4351 if (ftrace_dump_on_oops)
4352 ftrace_dump();
4353 break;
4354 default:
4355 break;
4357 return NOTIFY_OK;
4360 static struct notifier_block trace_die_notifier = {
4361 .notifier_call = trace_die_handler,
4362 .priority = 200
4366 * printk is set to max of 1024, we really don't need it that big.
4367 * Nothing should be printing 1000 characters anyway.
4369 #define TRACE_MAX_PRINT 1000
4372 * Define here KERN_TRACE so that we have one place to modify
4373 * it if we decide to change what log level the ftrace dump
4374 * should be at.
4376 #define KERN_TRACE KERN_EMERG
4378 static void
4379 trace_printk_seq(struct trace_seq *s)
4381 /* Probably should print a warning here. */
4382 if (s->len >= 1000)
4383 s->len = 1000;
4385 /* should be zero ended, but we are paranoid. */
4386 s->buffer[s->len] = 0;
4388 printk(KERN_TRACE "%s", s->buffer);
4390 trace_seq_init(s);
4393 static void __ftrace_dump(bool disable_tracing)
4395 static arch_spinlock_t ftrace_dump_lock =
4396 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
4397 /* use static because iter can be a bit big for the stack */
4398 static struct trace_iterator iter;
4399 unsigned int old_userobj;
4400 static int dump_ran;
4401 unsigned long flags;
4402 int cnt = 0, cpu;
4404 /* only one dump */
4405 local_irq_save(flags);
4406 arch_spin_lock(&ftrace_dump_lock);
4407 if (dump_ran)
4408 goto out;
4410 dump_ran = 1;
4412 tracing_off();
4414 if (disable_tracing)
4415 ftrace_kill();
4417 for_each_tracing_cpu(cpu) {
4418 atomic_inc(&global_trace.data[cpu]->disabled);
4421 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4423 /* don't look at user memory in panic mode */
4424 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4426 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4428 /* Simulate the iterator */
4429 iter.tr = &global_trace;
4430 iter.trace = current_trace;
4431 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4434 * We need to stop all tracing on all CPUS to read the
4435 * the next buffer. This is a bit expensive, but is
4436 * not done often. We fill all what we can read,
4437 * and then release the locks again.
4440 while (!trace_empty(&iter)) {
4442 if (!cnt)
4443 printk(KERN_TRACE "---------------------------------\n");
4445 cnt++;
4447 /* reset all but tr, trace, and overruns */
4448 memset(&iter.seq, 0,
4449 sizeof(struct trace_iterator) -
4450 offsetof(struct trace_iterator, seq));
4451 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4452 iter.pos = -1;
4454 if (find_next_entry_inc(&iter) != NULL) {
4455 int ret;
4457 ret = print_trace_line(&iter);
4458 if (ret != TRACE_TYPE_NO_CONSUME)
4459 trace_consume(&iter);
4462 trace_printk_seq(&iter.seq);
4465 if (!cnt)
4466 printk(KERN_TRACE " (ftrace buffer empty)\n");
4467 else
4468 printk(KERN_TRACE "---------------------------------\n");
4470 /* Re-enable tracing if requested */
4471 if (!disable_tracing) {
4472 trace_flags |= old_userobj;
4474 for_each_tracing_cpu(cpu) {
4475 atomic_dec(&global_trace.data[cpu]->disabled);
4477 tracing_on();
4480 out:
4481 arch_spin_unlock(&ftrace_dump_lock);
4482 local_irq_restore(flags);
4485 /* By default: disable tracing after the dump */
4486 void ftrace_dump(void)
4488 __ftrace_dump(true);
4491 __init static int tracer_alloc_buffers(void)
4493 int ring_buf_size;
4494 int i;
4495 int ret = -ENOMEM;
4497 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4498 goto out;
4500 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4501 goto out_free_buffer_mask;
4503 /* To save memory, keep the ring buffer size to its minimum */
4504 if (ring_buffer_expanded)
4505 ring_buf_size = trace_buf_size;
4506 else
4507 ring_buf_size = 1;
4509 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4510 cpumask_copy(tracing_cpumask, cpu_all_mask);
4512 /* TODO: make the number of buffers hot pluggable with CPUS */
4513 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4514 TRACE_BUFFER_FLAGS);
4515 if (!global_trace.buffer) {
4516 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4517 WARN_ON(1);
4518 goto out_free_cpumask;
4520 global_trace.entries = ring_buffer_size(global_trace.buffer);
4523 #ifdef CONFIG_TRACER_MAX_TRACE
4524 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4525 TRACE_BUFFER_FLAGS);
4526 if (!max_tr.buffer) {
4527 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4528 WARN_ON(1);
4529 ring_buffer_free(global_trace.buffer);
4530 goto out_free_cpumask;
4532 max_tr.entries = ring_buffer_size(max_tr.buffer);
4533 WARN_ON(max_tr.entries != global_trace.entries);
4534 #endif
4536 /* Allocate the first page for all buffers */
4537 for_each_tracing_cpu(i) {
4538 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4539 max_tr.data[i] = &per_cpu(max_tr_data, i);
4542 trace_init_cmdlines();
4544 register_tracer(&nop_trace);
4545 current_trace = &nop_trace;
4546 #ifdef CONFIG_BOOT_TRACER
4547 register_tracer(&boot_tracer);
4548 #endif
4549 /* All seems OK, enable tracing */
4550 tracing_disabled = 0;
4552 atomic_notifier_chain_register(&panic_notifier_list,
4553 &trace_panic_notifier);
4555 register_die_notifier(&trace_die_notifier);
4557 return 0;
4559 out_free_cpumask:
4560 free_cpumask_var(tracing_cpumask);
4561 out_free_buffer_mask:
4562 free_cpumask_var(tracing_buffer_mask);
4563 out:
4564 return ret;
4567 __init static int clear_boot_tracer(void)
4570 * The default tracer at boot buffer is an init section.
4571 * This function is called in lateinit. If we did not
4572 * find the boot tracer, then clear it out, to prevent
4573 * later registration from accessing the buffer that is
4574 * about to be freed.
4576 if (!default_bootup_tracer)
4577 return 0;
4579 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4580 default_bootup_tracer);
4581 default_bootup_tracer = NULL;
4583 return 0;
4586 early_initcall(tracer_alloc_buffers);
4587 fs_initcall(tracer_init_debugfs);
4588 late_initcall(clear_boot_tracer);