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/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/notifier.h>
18 #include <linux/debugfs.h>
19 #include <linux/pagemap.h>
20 #include <linux/hardirq.h>
21 #include <linux/linkage.h>
22 #include <linux/uaccess.h>
23 #include <linux/ftrace.h>
24 #include <linux/module.h>
25 #include <linux/percpu.h>
26 #include <linux/kdebug.h>
27 #include <linux/ctype.h>
28 #include <linux/init.h>
29 #include <linux/poll.h>
30 #include <linux/gfp.h>
32 #include <linux/kprobes.h>
33 #include <linux/writeback.h>
34 #include <linux/splice.h>
36 #include <linux/stacktrace.h>
37 #include <linux/ring_buffer.h>
38 #include <linux/irqflags.h>
41 #include "trace_output.h"
43 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
45 unsigned long __read_mostly tracing_max_latency;
46 unsigned long __read_mostly tracing_thresh;
49 * We need to change this state when a selftest is running.
50 * A selftest will lurk into the ring-buffer to count the
51 * entries inserted during the selftest although some concurrent
52 * insertions into the ring-buffer such as ftrace_printk could occurred
53 * at the same time, giving false positive or negative results.
55 static bool __read_mostly tracing_selftest_running;
58 * If a tracer is running, we do not want to run SELFTEST.
60 static bool __read_mostly tracing_selftest_disabled;
62 /* For tracers that don't implement custom flags */
63 static struct tracer_opt dummy_tracer_opt[] = {
67 static struct tracer_flags dummy_tracer_flags = {
69 .opts = dummy_tracer_opt
72 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
78 * Kill all tracing for good (never come back).
79 * It is initialized to 1 but will turn to zero if the initialization
80 * of the tracer is successful. But that is the only place that sets
83 static int tracing_disabled = 1;
85 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
87 static inline void ftrace_disable_cpu(void)
90 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
93 static inline void ftrace_enable_cpu(void)
95 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
99 static cpumask_var_t __read_mostly tracing_buffer_mask;
101 /* Define which cpu buffers are currently read in trace_pipe */
102 static cpumask_var_t tracing_reader_cpumask;
104 #define for_each_tracing_cpu(cpu) \
105 for_each_cpu(cpu, tracing_buffer_mask)
108 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
110 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
111 * is set, then ftrace_dump is called. This will output the contents
112 * of the ftrace buffers to the console. This is very useful for
113 * capturing traces that lead to crashes and outputing it to a
116 * It is default off, but you can enable it with either specifying
117 * "ftrace_dump_on_oops" in the kernel command line, or setting
118 * /proc/sys/kernel/ftrace_dump_on_oops to true.
120 int ftrace_dump_on_oops;
122 static int tracing_set_tracer(const char *buf);
124 #define BOOTUP_TRACER_SIZE 100
125 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
126 static char *default_bootup_tracer;
128 static int __init set_ftrace(char *str)
130 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
131 default_bootup_tracer = bootup_tracer_buf;
134 __setup("ftrace=", set_ftrace);
136 static int __init set_ftrace_dump_on_oops(char *str)
138 ftrace_dump_on_oops = 1;
141 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
144 ns2usecs(cycle_t nsec)
151 cycle_t ftrace_now(int cpu)
153 u64 ts = ring_buffer_time_stamp(cpu);
154 ring_buffer_normalize_time_stamp(cpu, &ts);
159 * The global_trace is the descriptor that holds the tracing
160 * buffers for the live tracing. For each CPU, it contains
161 * a link list of pages that will store trace entries. The
162 * page descriptor of the pages in the memory is used to hold
163 * the link list by linking the lru item in the page descriptor
164 * to each of the pages in the buffer per CPU.
166 * For each active CPU there is a data field that holds the
167 * pages for the buffer for that CPU. Each CPU has the same number
168 * of pages allocated for its buffer.
170 static struct trace_array global_trace;
172 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
175 * The max_tr is used to snapshot the global_trace when a maximum
176 * latency is reached. Some tracers will use this to store a maximum
177 * trace while it continues examining live traces.
179 * The buffers for the max_tr are set up the same as the global_trace.
180 * When a snapshot is taken, the link list of the max_tr is swapped
181 * with the link list of the global_trace and the buffers are reset for
182 * the global_trace so the tracing can continue.
184 static struct trace_array max_tr;
186 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
188 /* tracer_enabled is used to toggle activation of a tracer */
189 static int tracer_enabled = 1;
192 * tracing_is_enabled - return tracer_enabled status
194 * This function is used by other tracers to know the status
195 * of the tracer_enabled flag. Tracers may use this function
196 * to know if it should enable their features when starting
197 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
199 int tracing_is_enabled(void)
201 return tracer_enabled;
205 * trace_buf_size is the size in bytes that is allocated
206 * for a buffer. Note, the number of bytes is always rounded
209 * This number is purposely set to a low number of 16384.
210 * If the dump on oops happens, it will be much appreciated
211 * to not have to wait for all that output. Anyway this can be
212 * boot time and run time configurable.
214 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
216 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
218 /* trace_types holds a link list of available tracers. */
219 static struct tracer *trace_types __read_mostly;
221 /* current_trace points to the tracer that is currently active */
222 static struct tracer *current_trace __read_mostly;
225 * max_tracer_type_len is used to simplify the allocating of
226 * buffers to read userspace tracer names. We keep track of
227 * the longest tracer name registered.
229 static int max_tracer_type_len;
232 * trace_types_lock is used to protect the trace_types list.
233 * This lock is also used to keep user access serialized.
234 * Accesses from userspace will grab this lock while userspace
235 * activities happen inside the kernel.
237 static DEFINE_MUTEX(trace_types_lock);
239 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
240 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
242 /* trace_flags holds trace_options default values */
243 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
244 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
247 * trace_wake_up - wake up tasks waiting for trace input
249 * Simply wakes up any task that is blocked on the trace_wait
250 * queue. These is used with trace_poll for tasks polling the trace.
252 void trace_wake_up(void)
255 * The runqueue_is_locked() can fail, but this is the best we
258 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
259 wake_up(&trace_wait);
262 static int __init set_buf_size(char *str)
264 unsigned long buf_size;
269 ret = strict_strtoul(str, 0, &buf_size);
270 /* nr_entries can not be zero */
271 if (ret < 0 || buf_size == 0)
273 trace_buf_size = buf_size;
276 __setup("trace_buf_size=", set_buf_size);
278 unsigned long nsecs_to_usecs(unsigned long nsecs)
283 /* These must match the bit postions in trace_iterator_flags */
284 static const char *trace_options[] = {
307 * ftrace_max_lock is used to protect the swapping of buffers
308 * when taking a max snapshot. The buffers themselves are
309 * protected by per_cpu spinlocks. But the action of the swap
310 * needs its own lock.
312 * This is defined as a raw_spinlock_t in order to help
313 * with performance when lockdep debugging is enabled.
315 static raw_spinlock_t ftrace_max_lock =
316 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
319 * Copy the new maximum trace into the separate maximum-trace
320 * structure. (this way the maximum trace is permanently saved,
321 * for later retrieval via /debugfs/tracing/latency_trace)
324 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
326 struct trace_array_cpu *data = tr->data[cpu];
329 max_tr.time_start = data->preempt_timestamp;
331 data = max_tr.data[cpu];
332 data->saved_latency = tracing_max_latency;
334 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
335 data->pid = tsk->pid;
336 data->uid = task_uid(tsk);
337 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
338 data->policy = tsk->policy;
339 data->rt_priority = tsk->rt_priority;
341 /* record this tasks comm */
342 tracing_record_cmdline(tsk);
346 trace_seq_reset(struct trace_seq *s)
352 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
357 if (s->len <= s->readpos)
360 len = s->len - s->readpos;
363 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
371 ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
376 if (s->len <= s->readpos)
379 len = s->len - s->readpos;
382 ret = memcpy(buf, s->buffer + s->readpos, cnt);
391 trace_print_seq(struct seq_file *m, struct trace_seq *s)
393 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
396 seq_puts(m, s->buffer);
402 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
404 * @tsk: the task with the latency
405 * @cpu: The cpu that initiated the trace.
407 * Flip the buffers between the @tr and the max_tr and record information
408 * about which task was the cause of this latency.
411 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
413 struct ring_buffer *buf = tr->buffer;
415 WARN_ON_ONCE(!irqs_disabled());
416 __raw_spin_lock(&ftrace_max_lock);
418 tr->buffer = max_tr.buffer;
421 ftrace_disable_cpu();
422 ring_buffer_reset(tr->buffer);
425 __update_max_tr(tr, tsk, cpu);
426 __raw_spin_unlock(&ftrace_max_lock);
430 * update_max_tr_single - only copy one trace over, and reset the rest
432 * @tsk - task with the latency
433 * @cpu - the cpu of the buffer to copy.
435 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
438 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
442 WARN_ON_ONCE(!irqs_disabled());
443 __raw_spin_lock(&ftrace_max_lock);
445 ftrace_disable_cpu();
447 ring_buffer_reset(max_tr.buffer);
448 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
452 WARN_ON_ONCE(ret && ret != -EAGAIN);
454 __update_max_tr(tr, tsk, cpu);
455 __raw_spin_unlock(&ftrace_max_lock);
459 * register_tracer - register a tracer with the ftrace system.
460 * @type - the plugin for the tracer
462 * Register a new plugin tracer.
464 int register_tracer(struct tracer *type)
465 __releases(kernel_lock)
466 __acquires(kernel_lock)
473 pr_info("Tracer must have a name\n");
478 * When this gets called we hold the BKL which means that
479 * preemption is disabled. Various trace selftests however
480 * need to disable and enable preemption for successful tests.
481 * So we drop the BKL here and grab it after the tests again.
484 mutex_lock(&trace_types_lock);
486 tracing_selftest_running = true;
488 for (t = trace_types; t; t = t->next) {
489 if (strcmp(type->name, t->name) == 0) {
491 pr_info("Trace %s already registered\n",
499 type->set_flag = &dummy_set_flag;
501 type->flags = &dummy_tracer_flags;
503 if (!type->flags->opts)
504 type->flags->opts = dummy_tracer_opt;
505 if (!type->wait_pipe)
506 type->wait_pipe = default_wait_pipe;
509 #ifdef CONFIG_FTRACE_STARTUP_TEST
510 if (type->selftest && !tracing_selftest_disabled) {
511 struct tracer *saved_tracer = current_trace;
512 struct trace_array *tr = &global_trace;
516 * Run a selftest on this tracer.
517 * Here we reset the trace buffer, and set the current
518 * tracer to be this tracer. The tracer can then run some
519 * internal tracing to verify that everything is in order.
520 * If we fail, we do not register this tracer.
522 for_each_tracing_cpu(i)
523 tracing_reset(tr, i);
525 current_trace = type;
526 /* the test is responsible for initializing and enabling */
527 pr_info("Testing tracer %s: ", type->name);
528 ret = type->selftest(type, tr);
529 /* the test is responsible for resetting too */
530 current_trace = saved_tracer;
532 printk(KERN_CONT "FAILED!\n");
535 /* Only reset on passing, to avoid touching corrupted buffers */
536 for_each_tracing_cpu(i)
537 tracing_reset(tr, i);
539 printk(KERN_CONT "PASSED\n");
543 type->next = trace_types;
545 len = strlen(type->name);
546 if (len > max_tracer_type_len)
547 max_tracer_type_len = len;
550 tracing_selftest_running = false;
551 mutex_unlock(&trace_types_lock);
553 if (ret || !default_bootup_tracer)
556 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
559 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
560 /* Do we want this tracer to start on bootup? */
561 tracing_set_tracer(type->name);
562 default_bootup_tracer = NULL;
563 /* disable other selftests, since this will break it. */
564 tracing_selftest_disabled = 1;
565 #ifdef CONFIG_FTRACE_STARTUP_TEST
566 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
575 void unregister_tracer(struct tracer *type)
580 mutex_lock(&trace_types_lock);
581 for (t = &trace_types; *t; t = &(*t)->next) {
585 pr_info("Trace %s not registered\n", type->name);
591 if (type == current_trace && tracer_enabled) {
594 if (current_trace->stop)
595 current_trace->stop(&global_trace);
596 current_trace = &nop_trace;
599 if (strlen(type->name) != max_tracer_type_len)
602 max_tracer_type_len = 0;
603 for (t = &trace_types; *t; t = &(*t)->next) {
604 len = strlen((*t)->name);
605 if (len > max_tracer_type_len)
606 max_tracer_type_len = len;
609 mutex_unlock(&trace_types_lock);
612 void tracing_reset(struct trace_array *tr, int cpu)
614 ftrace_disable_cpu();
615 ring_buffer_reset_cpu(tr->buffer, cpu);
619 void tracing_reset_online_cpus(struct trace_array *tr)
623 tr->time_start = ftrace_now(tr->cpu);
625 for_each_online_cpu(cpu)
626 tracing_reset(tr, cpu);
629 #define SAVED_CMDLINES 128
630 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
631 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
632 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
633 static int cmdline_idx;
634 static DEFINE_SPINLOCK(trace_cmdline_lock);
636 /* temporary disable recording */
637 static atomic_t trace_record_cmdline_disabled __read_mostly;
639 static void trace_init_cmdlines(void)
641 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
642 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
646 static int trace_stop_count;
647 static DEFINE_SPINLOCK(tracing_start_lock);
650 * ftrace_off_permanent - disable all ftrace code permanently
652 * This should only be called when a serious anomally has
653 * been detected. This will turn off the function tracing,
654 * ring buffers, and other tracing utilites. It takes no
655 * locks and can be called from any context.
657 void ftrace_off_permanent(void)
659 tracing_disabled = 1;
661 tracing_off_permanent();
665 * tracing_start - quick start of the tracer
667 * If tracing is enabled but was stopped by tracing_stop,
668 * this will start the tracer back up.
670 void tracing_start(void)
672 struct ring_buffer *buffer;
675 if (tracing_disabled)
678 spin_lock_irqsave(&tracing_start_lock, flags);
679 if (--trace_stop_count) {
680 if (trace_stop_count < 0) {
681 /* Someone screwed up their debugging */
683 trace_stop_count = 0;
689 buffer = global_trace.buffer;
691 ring_buffer_record_enable(buffer);
693 buffer = max_tr.buffer;
695 ring_buffer_record_enable(buffer);
699 spin_unlock_irqrestore(&tracing_start_lock, flags);
703 * tracing_stop - quick stop of the tracer
705 * Light weight way to stop tracing. Use in conjunction with
708 void tracing_stop(void)
710 struct ring_buffer *buffer;
714 spin_lock_irqsave(&tracing_start_lock, flags);
715 if (trace_stop_count++)
718 buffer = global_trace.buffer;
720 ring_buffer_record_disable(buffer);
722 buffer = max_tr.buffer;
724 ring_buffer_record_disable(buffer);
727 spin_unlock_irqrestore(&tracing_start_lock, flags);
730 void trace_stop_cmdline_recording(void);
732 static void trace_save_cmdline(struct task_struct *tsk)
737 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
741 * It's not the end of the world if we don't get
742 * the lock, but we also don't want to spin
743 * nor do we want to disable interrupts,
744 * so if we miss here, then better luck next time.
746 if (!spin_trylock(&trace_cmdline_lock))
749 idx = map_pid_to_cmdline[tsk->pid];
750 if (idx >= SAVED_CMDLINES) {
751 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
753 map = map_cmdline_to_pid[idx];
754 if (map <= PID_MAX_DEFAULT)
755 map_pid_to_cmdline[map] = (unsigned)-1;
757 map_pid_to_cmdline[tsk->pid] = idx;
762 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
764 spin_unlock(&trace_cmdline_lock);
767 char *trace_find_cmdline(int pid)
769 char *cmdline = "<...>";
775 if (pid > PID_MAX_DEFAULT)
778 map = map_pid_to_cmdline[pid];
779 if (map >= SAVED_CMDLINES)
782 cmdline = saved_cmdlines[map];
788 void tracing_record_cmdline(struct task_struct *tsk)
790 if (atomic_read(&trace_record_cmdline_disabled))
793 trace_save_cmdline(tsk);
797 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
800 struct task_struct *tsk = current;
802 entry->preempt_count = pc & 0xff;
803 entry->pid = (tsk) ? tsk->pid : 0;
804 entry->tgid = (tsk) ? tsk->tgid : 0;
806 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
807 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
809 TRACE_FLAG_IRQS_NOSUPPORT |
811 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
812 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
813 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
816 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
819 unsigned long flags, int pc)
821 struct ring_buffer_event *event;
823 event = ring_buffer_lock_reserve(tr->buffer, len);
825 struct trace_entry *ent = ring_buffer_event_data(event);
827 tracing_generic_entry_update(ent, flags, pc);
833 static void ftrace_trace_stack(struct trace_array *tr,
834 unsigned long flags, int skip, int pc);
835 static void ftrace_trace_userstack(struct trace_array *tr,
836 unsigned long flags, int pc);
838 void trace_buffer_unlock_commit(struct trace_array *tr,
839 struct ring_buffer_event *event,
840 unsigned long flags, int pc)
842 ring_buffer_unlock_commit(tr->buffer, event);
844 ftrace_trace_stack(tr, flags, 6, pc);
845 ftrace_trace_userstack(tr, flags, pc);
850 trace_function(struct trace_array *tr,
851 unsigned long ip, unsigned long parent_ip, unsigned long flags,
854 struct ring_buffer_event *event;
855 struct ftrace_entry *entry;
857 /* If we are reading the ring buffer, don't trace */
858 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
861 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
865 entry = ring_buffer_event_data(event);
867 entry->parent_ip = parent_ip;
868 ring_buffer_unlock_commit(tr->buffer, event);
871 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
872 static void __trace_graph_entry(struct trace_array *tr,
873 struct ftrace_graph_ent *trace,
877 struct ring_buffer_event *event;
878 struct ftrace_graph_ent_entry *entry;
880 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
883 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
884 sizeof(*entry), flags, pc);
887 entry = ring_buffer_event_data(event);
888 entry->graph_ent = *trace;
889 ring_buffer_unlock_commit(global_trace.buffer, event);
892 static void __trace_graph_return(struct trace_array *tr,
893 struct ftrace_graph_ret *trace,
897 struct ring_buffer_event *event;
898 struct ftrace_graph_ret_entry *entry;
900 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
903 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
904 sizeof(*entry), flags, pc);
907 entry = ring_buffer_event_data(event);
909 ring_buffer_unlock_commit(global_trace.buffer, event);
914 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
915 unsigned long ip, unsigned long parent_ip, unsigned long flags,
918 if (likely(!atomic_read(&data->disabled)))
919 trace_function(tr, ip, parent_ip, flags, pc);
922 static void __ftrace_trace_stack(struct trace_array *tr,
926 #ifdef CONFIG_STACKTRACE
927 struct ring_buffer_event *event;
928 struct stack_entry *entry;
929 struct stack_trace trace;
931 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
932 sizeof(*entry), flags, pc);
935 entry = ring_buffer_event_data(event);
936 memset(&entry->caller, 0, sizeof(entry->caller));
938 trace.nr_entries = 0;
939 trace.max_entries = FTRACE_STACK_ENTRIES;
941 trace.entries = entry->caller;
943 save_stack_trace(&trace);
944 ring_buffer_unlock_commit(tr->buffer, event);
948 static void ftrace_trace_stack(struct trace_array *tr,
952 if (!(trace_flags & TRACE_ITER_STACKTRACE))
955 __ftrace_trace_stack(tr, flags, skip, pc);
958 void __trace_stack(struct trace_array *tr,
962 __ftrace_trace_stack(tr, flags, skip, pc);
965 static void ftrace_trace_userstack(struct trace_array *tr,
966 unsigned long flags, int pc)
968 #ifdef CONFIG_STACKTRACE
969 struct ring_buffer_event *event;
970 struct userstack_entry *entry;
971 struct stack_trace trace;
973 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
976 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
977 sizeof(*entry), flags, pc);
980 entry = ring_buffer_event_data(event);
982 memset(&entry->caller, 0, sizeof(entry->caller));
984 trace.nr_entries = 0;
985 trace.max_entries = FTRACE_STACK_ENTRIES;
987 trace.entries = entry->caller;
989 save_stack_trace_user(&trace);
990 ring_buffer_unlock_commit(tr->buffer, event);
995 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
997 ftrace_trace_userstack(tr, flags, preempt_count());
1002 ftrace_trace_special(void *__tr,
1003 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1006 struct ring_buffer_event *event;
1007 struct trace_array *tr = __tr;
1008 struct special_entry *entry;
1010 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1011 sizeof(*entry), 0, pc);
1014 entry = ring_buffer_event_data(event);
1018 trace_buffer_unlock_commit(tr, event, 0, pc);
1022 __trace_special(void *__tr, void *__data,
1023 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1025 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1029 tracing_sched_switch_trace(struct trace_array *tr,
1030 struct task_struct *prev,
1031 struct task_struct *next,
1032 unsigned long flags, int pc)
1034 struct ring_buffer_event *event;
1035 struct ctx_switch_entry *entry;
1037 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1038 sizeof(*entry), flags, pc);
1041 entry = ring_buffer_event_data(event);
1042 entry->prev_pid = prev->pid;
1043 entry->prev_prio = prev->prio;
1044 entry->prev_state = prev->state;
1045 entry->next_pid = next->pid;
1046 entry->next_prio = next->prio;
1047 entry->next_state = next->state;
1048 entry->next_cpu = task_cpu(next);
1049 trace_buffer_unlock_commit(tr, event, flags, pc);
1053 tracing_sched_wakeup_trace(struct trace_array *tr,
1054 struct task_struct *wakee,
1055 struct task_struct *curr,
1056 unsigned long flags, int pc)
1058 struct ring_buffer_event *event;
1059 struct ctx_switch_entry *entry;
1061 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1062 sizeof(*entry), flags, pc);
1065 entry = ring_buffer_event_data(event);
1066 entry->prev_pid = curr->pid;
1067 entry->prev_prio = curr->prio;
1068 entry->prev_state = curr->state;
1069 entry->next_pid = wakee->pid;
1070 entry->next_prio = wakee->prio;
1071 entry->next_state = wakee->state;
1072 entry->next_cpu = task_cpu(wakee);
1074 ring_buffer_unlock_commit(tr->buffer, event);
1075 ftrace_trace_stack(tr, flags, 6, pc);
1076 ftrace_trace_userstack(tr, flags, pc);
1080 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1082 struct trace_array *tr = &global_trace;
1083 struct trace_array_cpu *data;
1084 unsigned long flags;
1088 if (tracing_disabled)
1091 pc = preempt_count();
1092 local_irq_save(flags);
1093 cpu = raw_smp_processor_id();
1094 data = tr->data[cpu];
1096 if (likely(atomic_inc_return(&data->disabled) == 1))
1097 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1099 atomic_dec(&data->disabled);
1100 local_irq_restore(flags);
1103 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1104 int trace_graph_entry(struct ftrace_graph_ent *trace)
1106 struct trace_array *tr = &global_trace;
1107 struct trace_array_cpu *data;
1108 unsigned long flags;
1113 if (!ftrace_trace_task(current))
1116 if (!ftrace_graph_addr(trace->func))
1119 local_irq_save(flags);
1120 cpu = raw_smp_processor_id();
1121 data = tr->data[cpu];
1122 disabled = atomic_inc_return(&data->disabled);
1123 if (likely(disabled == 1)) {
1124 pc = preempt_count();
1125 __trace_graph_entry(tr, trace, flags, pc);
1127 /* Only do the atomic if it is not already set */
1128 if (!test_tsk_trace_graph(current))
1129 set_tsk_trace_graph(current);
1130 atomic_dec(&data->disabled);
1131 local_irq_restore(flags);
1136 void trace_graph_return(struct ftrace_graph_ret *trace)
1138 struct trace_array *tr = &global_trace;
1139 struct trace_array_cpu *data;
1140 unsigned long flags;
1145 local_irq_save(flags);
1146 cpu = raw_smp_processor_id();
1147 data = tr->data[cpu];
1148 disabled = atomic_inc_return(&data->disabled);
1149 if (likely(disabled == 1)) {
1150 pc = preempt_count();
1151 __trace_graph_return(tr, trace, flags, pc);
1154 clear_tsk_trace_graph(current);
1155 atomic_dec(&data->disabled);
1156 local_irq_restore(flags);
1158 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1160 enum trace_file_type {
1161 TRACE_FILE_LAT_FMT = 1,
1162 TRACE_FILE_ANNOTATE = 2,
1165 static void trace_iterator_increment(struct trace_iterator *iter)
1167 /* Don't allow ftrace to trace into the ring buffers */
1168 ftrace_disable_cpu();
1171 if (iter->buffer_iter[iter->cpu])
1172 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1174 ftrace_enable_cpu();
1177 static struct trace_entry *
1178 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1180 struct ring_buffer_event *event;
1181 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1183 /* Don't allow ftrace to trace into the ring buffers */
1184 ftrace_disable_cpu();
1187 event = ring_buffer_iter_peek(buf_iter, ts);
1189 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1191 ftrace_enable_cpu();
1193 return event ? ring_buffer_event_data(event) : NULL;
1196 static struct trace_entry *
1197 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1199 struct ring_buffer *buffer = iter->tr->buffer;
1200 struct trace_entry *ent, *next = NULL;
1201 int cpu_file = iter->cpu_file;
1202 u64 next_ts = 0, ts;
1207 * If we are in a per_cpu trace file, don't bother by iterating over
1208 * all cpu and peek directly.
1210 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1211 if (ring_buffer_empty_cpu(buffer, cpu_file))
1213 ent = peek_next_entry(iter, cpu_file, ent_ts);
1215 *ent_cpu = cpu_file;
1220 for_each_tracing_cpu(cpu) {
1222 if (ring_buffer_empty_cpu(buffer, cpu))
1225 ent = peek_next_entry(iter, cpu, &ts);
1228 * Pick the entry with the smallest timestamp:
1230 if (ent && (!next || ts < next_ts)) {
1238 *ent_cpu = next_cpu;
1246 /* Find the next real entry, without updating the iterator itself */
1247 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1248 int *ent_cpu, u64 *ent_ts)
1250 return __find_next_entry(iter, ent_cpu, ent_ts);
1253 /* Find the next real entry, and increment the iterator to the next entry */
1254 static void *find_next_entry_inc(struct trace_iterator *iter)
1256 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1259 trace_iterator_increment(iter);
1261 return iter->ent ? iter : NULL;
1264 static void trace_consume(struct trace_iterator *iter)
1266 /* Don't allow ftrace to trace into the ring buffers */
1267 ftrace_disable_cpu();
1268 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1269 ftrace_enable_cpu();
1272 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1274 struct trace_iterator *iter = m->private;
1280 /* can't go backwards */
1285 ent = find_next_entry_inc(iter);
1289 while (ent && iter->idx < i)
1290 ent = find_next_entry_inc(iter);
1297 static void *s_start(struct seq_file *m, loff_t *pos)
1299 struct trace_iterator *iter = m->private;
1300 int cpu_file = iter->cpu_file;
1305 mutex_lock(&trace_types_lock);
1307 if (!current_trace || current_trace != iter->trace) {
1308 mutex_unlock(&trace_types_lock);
1312 atomic_inc(&trace_record_cmdline_disabled);
1314 if (*pos != iter->pos) {
1319 ftrace_disable_cpu();
1321 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1322 for_each_tracing_cpu(cpu)
1323 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1325 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1328 ftrace_enable_cpu();
1330 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1335 p = s_next(m, p, &l);
1341 static void s_stop(struct seq_file *m, void *p)
1343 atomic_dec(&trace_record_cmdline_disabled);
1344 mutex_unlock(&trace_types_lock);
1347 static void print_lat_help_header(struct seq_file *m)
1349 seq_puts(m, "# _------=> CPU# \n");
1350 seq_puts(m, "# / _-----=> irqs-off \n");
1351 seq_puts(m, "# | / _----=> need-resched \n");
1352 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1353 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1354 seq_puts(m, "# |||| / \n");
1355 seq_puts(m, "# ||||| delay \n");
1356 seq_puts(m, "# cmd pid ||||| time | caller \n");
1357 seq_puts(m, "# \\ / ||||| \\ | / \n");
1360 static void print_func_help_header(struct seq_file *m)
1362 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1363 seq_puts(m, "# | | | | |\n");
1368 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1370 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1371 struct trace_array *tr = iter->tr;
1372 struct trace_array_cpu *data = tr->data[tr->cpu];
1373 struct tracer *type = current_trace;
1374 unsigned long total;
1375 unsigned long entries;
1376 const char *name = "preemption";
1381 entries = ring_buffer_entries(iter->tr->buffer);
1383 ring_buffer_overruns(iter->tr->buffer);
1385 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1387 seq_puts(m, "-----------------------------------"
1388 "---------------------------------\n");
1389 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1390 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1391 nsecs_to_usecs(data->saved_latency),
1395 #if defined(CONFIG_PREEMPT_NONE)
1397 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1399 #elif defined(CONFIG_PREEMPT)
1404 /* These are reserved for later use */
1407 seq_printf(m, " #P:%d)\n", num_online_cpus());
1411 seq_puts(m, " -----------------\n");
1412 seq_printf(m, " | task: %.16s-%d "
1413 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1414 data->comm, data->pid, data->uid, data->nice,
1415 data->policy, data->rt_priority);
1416 seq_puts(m, " -----------------\n");
1418 if (data->critical_start) {
1419 seq_puts(m, " => started at: ");
1420 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1421 trace_print_seq(m, &iter->seq);
1422 seq_puts(m, "\n => ended at: ");
1423 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1424 trace_print_seq(m, &iter->seq);
1431 static void test_cpu_buff_start(struct trace_iterator *iter)
1433 struct trace_seq *s = &iter->seq;
1435 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1438 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1441 if (cpumask_test_cpu(iter->cpu, iter->started))
1444 cpumask_set_cpu(iter->cpu, iter->started);
1445 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1448 static enum print_line_t print_lat_fmt(struct trace_iterator *iter)
1450 struct trace_seq *s = &iter->seq;
1451 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1452 struct trace_event *event;
1453 struct trace_entry *entry = iter->ent;
1455 test_cpu_buff_start(iter);
1457 event = ftrace_find_event(entry->type);
1459 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1460 if (!trace_print_lat_context(iter))
1465 return event->latency_trace(iter, sym_flags);
1467 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1470 return TRACE_TYPE_HANDLED;
1472 return TRACE_TYPE_PARTIAL_LINE;
1475 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1477 struct trace_seq *s = &iter->seq;
1478 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1479 struct trace_entry *entry;
1480 struct trace_event *event;
1484 test_cpu_buff_start(iter);
1486 event = ftrace_find_event(entry->type);
1488 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1489 if (!trace_print_context(iter))
1494 return event->trace(iter, sym_flags);
1496 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1499 return TRACE_TYPE_HANDLED;
1501 return TRACE_TYPE_PARTIAL_LINE;
1504 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1506 struct trace_seq *s = &iter->seq;
1507 struct trace_entry *entry;
1508 struct trace_event *event;
1512 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1513 if (!trace_seq_printf(s, "%d %d %llu ",
1514 entry->pid, iter->cpu, iter->ts))
1518 event = ftrace_find_event(entry->type);
1520 return event->raw(iter, 0);
1522 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1525 return TRACE_TYPE_HANDLED;
1527 return TRACE_TYPE_PARTIAL_LINE;
1530 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1532 struct trace_seq *s = &iter->seq;
1533 unsigned char newline = '\n';
1534 struct trace_entry *entry;
1535 struct trace_event *event;
1539 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1540 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1541 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1542 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1545 event = ftrace_find_event(entry->type);
1547 enum print_line_t ret = event->hex(iter, 0);
1548 if (ret != TRACE_TYPE_HANDLED)
1552 SEQ_PUT_FIELD_RET(s, newline);
1554 return TRACE_TYPE_HANDLED;
1557 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1559 struct trace_seq *s = &iter->seq;
1560 struct trace_entry *entry = iter->ent;
1561 struct print_entry *field;
1564 trace_assign_type(field, entry);
1566 ret = trace_seq_printf(s, "%s", field->buf);
1568 return TRACE_TYPE_PARTIAL_LINE;
1570 return TRACE_TYPE_HANDLED;
1573 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1575 struct trace_seq *s = &iter->seq;
1576 struct trace_entry *entry;
1577 struct trace_event *event;
1581 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1582 SEQ_PUT_FIELD_RET(s, entry->pid);
1583 SEQ_PUT_FIELD_RET(s, iter->cpu);
1584 SEQ_PUT_FIELD_RET(s, iter->ts);
1587 event = ftrace_find_event(entry->type);
1588 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1591 static int trace_empty(struct trace_iterator *iter)
1595 for_each_tracing_cpu(cpu) {
1596 if (iter->buffer_iter[cpu]) {
1597 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1600 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1608 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1610 enum print_line_t ret;
1612 if (iter->trace && iter->trace->print_line) {
1613 ret = iter->trace->print_line(iter);
1614 if (ret != TRACE_TYPE_UNHANDLED)
1618 if (iter->ent->type == TRACE_PRINT &&
1619 trace_flags & TRACE_ITER_PRINTK &&
1620 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1621 return print_printk_msg_only(iter);
1623 if (trace_flags & TRACE_ITER_BIN)
1624 return print_bin_fmt(iter);
1626 if (trace_flags & TRACE_ITER_HEX)
1627 return print_hex_fmt(iter);
1629 if (trace_flags & TRACE_ITER_RAW)
1630 return print_raw_fmt(iter);
1632 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1633 return print_lat_fmt(iter);
1635 return print_trace_fmt(iter);
1638 static int s_show(struct seq_file *m, void *v)
1640 struct trace_iterator *iter = v;
1642 if (iter->ent == NULL) {
1644 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1647 if (iter->trace && iter->trace->print_header)
1648 iter->trace->print_header(m);
1649 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1650 /* print nothing if the buffers are empty */
1651 if (trace_empty(iter))
1653 print_trace_header(m, iter);
1654 if (!(trace_flags & TRACE_ITER_VERBOSE))
1655 print_lat_help_header(m);
1657 if (!(trace_flags & TRACE_ITER_VERBOSE))
1658 print_func_help_header(m);
1661 print_trace_line(iter);
1662 trace_print_seq(m, &iter->seq);
1668 static struct seq_operations tracer_seq_ops = {
1675 static struct trace_iterator *
1676 __tracing_open(struct inode *inode, struct file *file, int *ret)
1678 long cpu_file = (long) inode->i_private;
1679 struct trace_iterator *iter;
1683 if (tracing_disabled) {
1688 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1694 mutex_lock(&trace_types_lock);
1695 if (current_trace && current_trace->print_max)
1698 iter->tr = &global_trace;
1699 iter->trace = current_trace;
1701 iter->cpu_file = cpu_file;
1703 /* Notify the tracer early; before we stop tracing. */
1704 if (iter->trace && iter->trace->open)
1705 iter->trace->open(iter);
1707 /* Annotate start of buffers if we had overruns */
1708 if (ring_buffer_overruns(iter->tr->buffer))
1709 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1711 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1712 for_each_tracing_cpu(cpu) {
1714 iter->buffer_iter[cpu] =
1715 ring_buffer_read_start(iter->tr->buffer, cpu);
1717 if (!iter->buffer_iter[cpu])
1721 cpu = iter->cpu_file;
1722 iter->buffer_iter[cpu] =
1723 ring_buffer_read_start(iter->tr->buffer, cpu);
1725 if (!iter->buffer_iter[cpu])
1729 /* TODO stop tracer */
1730 *ret = seq_open(file, &tracer_seq_ops);
1734 m = file->private_data;
1737 /* stop the trace while dumping */
1740 mutex_unlock(&trace_types_lock);
1746 for_each_tracing_cpu(cpu) {
1747 if (iter->buffer_iter[cpu])
1748 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1751 mutex_unlock(&trace_types_lock);
1754 return ERR_PTR(-ENOMEM);
1757 int tracing_open_generic(struct inode *inode, struct file *filp)
1759 if (tracing_disabled)
1762 filp->private_data = inode->i_private;
1766 static int tracing_release(struct inode *inode, struct file *file)
1768 struct seq_file *m = (struct seq_file *)file->private_data;
1769 struct trace_iterator *iter = m->private;
1772 mutex_lock(&trace_types_lock);
1773 for_each_tracing_cpu(cpu) {
1774 if (iter->buffer_iter[cpu])
1775 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1778 if (iter->trace && iter->trace->close)
1779 iter->trace->close(iter);
1781 /* reenable tracing if it was previously enabled */
1783 mutex_unlock(&trace_types_lock);
1785 seq_release(inode, file);
1790 static int tracing_open(struct inode *inode, struct file *file)
1794 __tracing_open(inode, file, &ret);
1799 static int tracing_lt_open(struct inode *inode, struct file *file)
1801 struct trace_iterator *iter;
1804 iter = __tracing_open(inode, file, &ret);
1807 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1814 t_next(struct seq_file *m, void *v, loff_t *pos)
1816 struct tracer *t = m->private;
1828 static void *t_start(struct seq_file *m, loff_t *pos)
1830 struct tracer *t = m->private;
1833 mutex_lock(&trace_types_lock);
1834 for (; t && l < *pos; t = t_next(m, t, &l))
1840 static void t_stop(struct seq_file *m, void *p)
1842 mutex_unlock(&trace_types_lock);
1845 static int t_show(struct seq_file *m, void *v)
1847 struct tracer *t = v;
1852 seq_printf(m, "%s", t->name);
1861 static struct seq_operations show_traces_seq_ops = {
1868 static int show_traces_open(struct inode *inode, struct file *file)
1872 if (tracing_disabled)
1875 ret = seq_open(file, &show_traces_seq_ops);
1877 struct seq_file *m = file->private_data;
1878 m->private = trace_types;
1884 static struct file_operations tracing_fops = {
1885 .open = tracing_open,
1887 .llseek = seq_lseek,
1888 .release = tracing_release,
1891 static struct file_operations tracing_lt_fops = {
1892 .open = tracing_lt_open,
1894 .llseek = seq_lseek,
1895 .release = tracing_release,
1898 static struct file_operations show_traces_fops = {
1899 .open = show_traces_open,
1901 .release = seq_release,
1905 * Only trace on a CPU if the bitmask is set:
1907 static cpumask_var_t tracing_cpumask;
1910 * The tracer itself will not take this lock, but still we want
1911 * to provide a consistent cpumask to user-space:
1913 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1916 * Temporary storage for the character representation of the
1917 * CPU bitmask (and one more byte for the newline):
1919 static char mask_str[NR_CPUS + 1];
1922 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1923 size_t count, loff_t *ppos)
1927 mutex_lock(&tracing_cpumask_update_lock);
1929 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1930 if (count - len < 2) {
1934 len += sprintf(mask_str + len, "\n");
1935 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1938 mutex_unlock(&tracing_cpumask_update_lock);
1944 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1945 size_t count, loff_t *ppos)
1948 cpumask_var_t tracing_cpumask_new;
1950 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1953 mutex_lock(&tracing_cpumask_update_lock);
1954 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1958 local_irq_disable();
1959 __raw_spin_lock(&ftrace_max_lock);
1960 for_each_tracing_cpu(cpu) {
1962 * Increase/decrease the disabled counter if we are
1963 * about to flip a bit in the cpumask:
1965 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
1966 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
1967 atomic_inc(&global_trace.data[cpu]->disabled);
1969 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
1970 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
1971 atomic_dec(&global_trace.data[cpu]->disabled);
1974 __raw_spin_unlock(&ftrace_max_lock);
1977 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
1979 mutex_unlock(&tracing_cpumask_update_lock);
1980 free_cpumask_var(tracing_cpumask_new);
1985 mutex_unlock(&tracing_cpumask_update_lock);
1986 free_cpumask_var(tracing_cpumask);
1991 static struct file_operations tracing_cpumask_fops = {
1992 .open = tracing_open_generic,
1993 .read = tracing_cpumask_read,
1994 .write = tracing_cpumask_write,
1998 tracing_trace_options_read(struct file *filp, char __user *ubuf,
1999 size_t cnt, loff_t *ppos)
2005 u32 tracer_flags = current_trace->flags->val;
2006 struct tracer_opt *trace_opts = current_trace->flags->opts;
2009 /* calculate max size */
2010 for (i = 0; trace_options[i]; i++) {
2011 len += strlen(trace_options[i]);
2012 len += 3; /* "no" and space */
2016 * Increase the size with names of options specific
2017 * of the current tracer.
2019 for (i = 0; trace_opts[i].name; i++) {
2020 len += strlen(trace_opts[i].name);
2021 len += 3; /* "no" and space */
2024 /* +2 for \n and \0 */
2025 buf = kmalloc(len + 2, GFP_KERNEL);
2029 for (i = 0; trace_options[i]; i++) {
2030 if (trace_flags & (1 << i))
2031 r += sprintf(buf + r, "%s ", trace_options[i]);
2033 r += sprintf(buf + r, "no%s ", trace_options[i]);
2036 for (i = 0; trace_opts[i].name; i++) {
2037 if (tracer_flags & trace_opts[i].bit)
2038 r += sprintf(buf + r, "%s ",
2039 trace_opts[i].name);
2041 r += sprintf(buf + r, "no%s ",
2042 trace_opts[i].name);
2045 r += sprintf(buf + r, "\n");
2046 WARN_ON(r >= len + 2);
2048 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2055 /* Try to assign a tracer specific option */
2056 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2058 struct tracer_flags *trace_flags = trace->flags;
2059 struct tracer_opt *opts = NULL;
2063 for (i = 0; trace_flags->opts[i].name; i++) {
2064 opts = &trace_flags->opts[i];
2065 len = strlen(opts->name);
2067 if (strncmp(cmp, opts->name, len) == 0) {
2068 ret = trace->set_flag(trace_flags->val,
2074 if (!trace_flags->opts[i].name)
2077 /* Refused to handle */
2082 trace_flags->val &= ~opts->bit;
2084 trace_flags->val |= opts->bit;
2090 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2091 size_t cnt, loff_t *ppos)
2099 if (cnt >= sizeof(buf))
2102 if (copy_from_user(&buf, ubuf, cnt))
2107 if (strncmp(buf, "no", 2) == 0) {
2112 for (i = 0; trace_options[i]; i++) {
2113 int len = strlen(trace_options[i]);
2115 if (strncmp(cmp, trace_options[i], len) == 0) {
2117 trace_flags &= ~(1 << i);
2119 trace_flags |= (1 << i);
2124 /* If no option could be set, test the specific tracer options */
2125 if (!trace_options[i]) {
2126 ret = set_tracer_option(current_trace, cmp, neg);
2136 static struct file_operations tracing_iter_fops = {
2137 .open = tracing_open_generic,
2138 .read = tracing_trace_options_read,
2139 .write = tracing_trace_options_write,
2142 static const char readme_msg[] =
2143 "tracing mini-HOWTO:\n\n"
2145 "# mount -t debugfs nodev /debug\n\n"
2146 "# cat /debug/tracing/available_tracers\n"
2147 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2148 "# cat /debug/tracing/current_tracer\n"
2150 "# echo sched_switch > /debug/tracing/current_tracer\n"
2151 "# cat /debug/tracing/current_tracer\n"
2153 "# cat /debug/tracing/trace_options\n"
2154 "noprint-parent nosym-offset nosym-addr noverbose\n"
2155 "# echo print-parent > /debug/tracing/trace_options\n"
2156 "# echo 1 > /debug/tracing/tracing_enabled\n"
2157 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2158 "echo 0 > /debug/tracing/tracing_enabled\n"
2162 tracing_readme_read(struct file *filp, char __user *ubuf,
2163 size_t cnt, loff_t *ppos)
2165 return simple_read_from_buffer(ubuf, cnt, ppos,
2166 readme_msg, strlen(readme_msg));
2169 static struct file_operations tracing_readme_fops = {
2170 .open = tracing_open_generic,
2171 .read = tracing_readme_read,
2175 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2176 size_t cnt, loff_t *ppos)
2181 r = sprintf(buf, "%u\n", tracer_enabled);
2182 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2186 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2187 size_t cnt, loff_t *ppos)
2189 struct trace_array *tr = filp->private_data;
2194 if (cnt >= sizeof(buf))
2197 if (copy_from_user(&buf, ubuf, cnt))
2202 ret = strict_strtoul(buf, 10, &val);
2208 mutex_lock(&trace_types_lock);
2209 if (tracer_enabled ^ val) {
2212 if (current_trace->start)
2213 current_trace->start(tr);
2218 if (current_trace->stop)
2219 current_trace->stop(tr);
2222 mutex_unlock(&trace_types_lock);
2230 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2231 size_t cnt, loff_t *ppos)
2233 char buf[max_tracer_type_len+2];
2236 mutex_lock(&trace_types_lock);
2238 r = sprintf(buf, "%s\n", current_trace->name);
2240 r = sprintf(buf, "\n");
2241 mutex_unlock(&trace_types_lock);
2243 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2246 int tracer_init(struct tracer *t, struct trace_array *tr)
2248 tracing_reset_online_cpus(tr);
2252 static int tracing_set_tracer(const char *buf)
2254 struct trace_array *tr = &global_trace;
2258 mutex_lock(&trace_types_lock);
2259 for (t = trace_types; t; t = t->next) {
2260 if (strcmp(t->name, buf) == 0)
2267 if (t == current_trace)
2270 trace_branch_disable();
2271 if (current_trace && current_trace->reset)
2272 current_trace->reset(tr);
2276 ret = tracer_init(t, tr);
2281 trace_branch_enable(tr);
2283 mutex_unlock(&trace_types_lock);
2289 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2290 size_t cnt, loff_t *ppos)
2292 char buf[max_tracer_type_len+1];
2299 if (cnt > max_tracer_type_len)
2300 cnt = max_tracer_type_len;
2302 if (copy_from_user(&buf, ubuf, cnt))
2307 /* strip ending whitespace. */
2308 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2311 err = tracing_set_tracer(buf);
2321 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2322 size_t cnt, loff_t *ppos)
2324 unsigned long *ptr = filp->private_data;
2328 r = snprintf(buf, sizeof(buf), "%ld\n",
2329 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2330 if (r > sizeof(buf))
2332 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2336 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2337 size_t cnt, loff_t *ppos)
2339 unsigned long *ptr = filp->private_data;
2344 if (cnt >= sizeof(buf))
2347 if (copy_from_user(&buf, ubuf, cnt))
2352 ret = strict_strtoul(buf, 10, &val);
2361 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2363 long cpu_file = (long) inode->i_private;
2364 struct trace_iterator *iter;
2367 if (tracing_disabled)
2370 mutex_lock(&trace_types_lock);
2372 /* We only allow one reader per cpu */
2373 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2374 if (!cpumask_empty(tracing_reader_cpumask)) {
2378 cpumask_setall(tracing_reader_cpumask);
2380 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2381 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2388 /* create a buffer to store the information to pass to userspace */
2389 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2395 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2401 /* trace pipe does not show start of buffer */
2402 cpumask_setall(iter->started);
2404 iter->cpu_file = cpu_file;
2405 iter->tr = &global_trace;
2406 iter->trace = current_trace;
2407 filp->private_data = iter;
2409 if (iter->trace->pipe_open)
2410 iter->trace->pipe_open(iter);
2413 mutex_unlock(&trace_types_lock);
2417 static int tracing_release_pipe(struct inode *inode, struct file *file)
2419 struct trace_iterator *iter = file->private_data;
2421 mutex_lock(&trace_types_lock);
2423 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2424 cpumask_clear(tracing_reader_cpumask);
2426 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2428 mutex_unlock(&trace_types_lock);
2430 free_cpumask_var(iter->started);
2437 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2439 struct trace_iterator *iter = filp->private_data;
2441 if (trace_flags & TRACE_ITER_BLOCK) {
2443 * Always select as readable when in blocking mode
2445 return POLLIN | POLLRDNORM;
2447 if (!trace_empty(iter))
2448 return POLLIN | POLLRDNORM;
2449 poll_wait(filp, &trace_wait, poll_table);
2450 if (!trace_empty(iter))
2451 return POLLIN | POLLRDNORM;
2458 void default_wait_pipe(struct trace_iterator *iter)
2462 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2464 if (trace_empty(iter))
2467 finish_wait(&trace_wait, &wait);
2471 * This is a make-shift waitqueue.
2472 * A tracer might use this callback on some rare cases:
2474 * 1) the current tracer might hold the runqueue lock when it wakes up
2475 * a reader, hence a deadlock (sched, function, and function graph tracers)
2476 * 2) the function tracers, trace all functions, we don't want
2477 * the overhead of calling wake_up and friends
2478 * (and tracing them too)
2480 * Anyway, this is really very primitive wakeup.
2482 void poll_wait_pipe(struct trace_iterator *iter)
2484 set_current_state(TASK_INTERRUPTIBLE);
2485 /* sleep for 100 msecs, and try again. */
2486 schedule_timeout(HZ / 10);
2489 /* Must be called with trace_types_lock mutex held. */
2490 static int tracing_wait_pipe(struct file *filp)
2492 struct trace_iterator *iter = filp->private_data;
2494 while (trace_empty(iter)) {
2496 if ((filp->f_flags & O_NONBLOCK)) {
2500 mutex_unlock(&trace_types_lock);
2502 iter->trace->wait_pipe(iter);
2504 mutex_lock(&trace_types_lock);
2506 if (signal_pending(current))
2509 if (iter->trace != current_trace)
2513 * We block until we read something and tracing is disabled.
2514 * We still block if tracing is disabled, but we have never
2515 * read anything. This allows a user to cat this file, and
2516 * then enable tracing. But after we have read something,
2517 * we give an EOF when tracing is again disabled.
2519 * iter->pos will be 0 if we haven't read anything.
2521 if (!tracer_enabled && iter->pos)
2532 tracing_read_pipe(struct file *filp, char __user *ubuf,
2533 size_t cnt, loff_t *ppos)
2535 struct trace_iterator *iter = filp->private_data;
2538 /* return any leftover data */
2539 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2543 trace_seq_reset(&iter->seq);
2545 mutex_lock(&trace_types_lock);
2546 if (iter->trace->read) {
2547 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2553 sret = tracing_wait_pipe(filp);
2557 /* stop when tracing is finished */
2558 if (trace_empty(iter)) {
2563 if (cnt >= PAGE_SIZE)
2564 cnt = PAGE_SIZE - 1;
2566 /* reset all but tr, trace, and overruns */
2567 memset(&iter->seq, 0,
2568 sizeof(struct trace_iterator) -
2569 offsetof(struct trace_iterator, seq));
2572 while (find_next_entry_inc(iter) != NULL) {
2573 enum print_line_t ret;
2574 int len = iter->seq.len;
2576 ret = print_trace_line(iter);
2577 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2578 /* don't print partial lines */
2579 iter->seq.len = len;
2582 if (ret != TRACE_TYPE_NO_CONSUME)
2583 trace_consume(iter);
2585 if (iter->seq.len >= cnt)
2589 /* Now copy what we have to the user */
2590 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2591 if (iter->seq.readpos >= iter->seq.len)
2592 trace_seq_reset(&iter->seq);
2595 * If there was nothing to send to user, inspite of consuming trace
2596 * entries, go back to wait for more entries.
2602 mutex_unlock(&trace_types_lock);
2607 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2608 struct pipe_buffer *buf)
2610 __free_page(buf->page);
2613 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2616 __free_page(spd->pages[idx]);
2619 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2621 .map = generic_pipe_buf_map,
2622 .unmap = generic_pipe_buf_unmap,
2623 .confirm = generic_pipe_buf_confirm,
2624 .release = tracing_pipe_buf_release,
2625 .steal = generic_pipe_buf_steal,
2626 .get = generic_pipe_buf_get,
2630 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2635 /* Seq buffer is page-sized, exactly what we need. */
2637 count = iter->seq.len;
2638 ret = print_trace_line(iter);
2639 count = iter->seq.len - count;
2642 iter->seq.len -= count;
2645 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2646 iter->seq.len -= count;
2650 trace_consume(iter);
2652 if (!find_next_entry_inc(iter)) {
2662 static ssize_t tracing_splice_read_pipe(struct file *filp,
2664 struct pipe_inode_info *pipe,
2668 struct page *pages[PIPE_BUFFERS];
2669 struct partial_page partial[PIPE_BUFFERS];
2670 struct trace_iterator *iter = filp->private_data;
2671 struct splice_pipe_desc spd = {
2674 .nr_pages = 0, /* This gets updated below. */
2676 .ops = &tracing_pipe_buf_ops,
2677 .spd_release = tracing_spd_release_pipe,
2683 mutex_lock(&trace_types_lock);
2685 if (iter->trace->splice_read) {
2686 ret = iter->trace->splice_read(iter, filp,
2687 ppos, pipe, len, flags);
2692 ret = tracing_wait_pipe(filp);
2696 if (!iter->ent && !find_next_entry_inc(iter)) {
2701 /* Fill as many pages as possible. */
2702 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2703 pages[i] = alloc_page(GFP_KERNEL);
2707 rem = tracing_fill_pipe_page(rem, iter);
2709 /* Copy the data into the page, so we can start over. */
2710 ret = trace_seq_to_buffer(&iter->seq,
2711 page_address(pages[i]),
2714 __free_page(pages[i]);
2717 partial[i].offset = 0;
2718 partial[i].len = iter->seq.len;
2720 trace_seq_reset(&iter->seq);
2723 mutex_unlock(&trace_types_lock);
2727 return splice_to_pipe(pipe, &spd);
2730 mutex_unlock(&trace_types_lock);
2736 tracing_entries_read(struct file *filp, char __user *ubuf,
2737 size_t cnt, loff_t *ppos)
2739 struct trace_array *tr = filp->private_data;
2743 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2744 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2748 tracing_entries_write(struct file *filp, const char __user *ubuf,
2749 size_t cnt, loff_t *ppos)
2755 if (cnt >= sizeof(buf))
2758 if (copy_from_user(&buf, ubuf, cnt))
2763 ret = strict_strtoul(buf, 10, &val);
2767 /* must have at least 1 entry */
2771 mutex_lock(&trace_types_lock);
2775 /* disable all cpu buffers */
2776 for_each_tracing_cpu(cpu) {
2777 if (global_trace.data[cpu])
2778 atomic_inc(&global_trace.data[cpu]->disabled);
2779 if (max_tr.data[cpu])
2780 atomic_inc(&max_tr.data[cpu]->disabled);
2783 /* value is in KB */
2786 if (val != global_trace.entries) {
2787 ret = ring_buffer_resize(global_trace.buffer, val);
2793 ret = ring_buffer_resize(max_tr.buffer, val);
2797 r = ring_buffer_resize(global_trace.buffer,
2798 global_trace.entries);
2800 /* AARGH! We are left with different
2801 * size max buffer!!!! */
2803 tracing_disabled = 1;
2808 global_trace.entries = val;
2813 /* If check pages failed, return ENOMEM */
2814 if (tracing_disabled)
2817 for_each_tracing_cpu(cpu) {
2818 if (global_trace.data[cpu])
2819 atomic_dec(&global_trace.data[cpu]->disabled);
2820 if (max_tr.data[cpu])
2821 atomic_dec(&max_tr.data[cpu]->disabled);
2825 max_tr.entries = global_trace.entries;
2826 mutex_unlock(&trace_types_lock);
2831 static int mark_printk(const char *fmt, ...)
2835 va_start(args, fmt);
2836 ret = trace_vprintk(0, -1, fmt, args);
2842 tracing_mark_write(struct file *filp, const char __user *ubuf,
2843 size_t cnt, loff_t *fpos)
2848 if (tracing_disabled)
2851 if (cnt > TRACE_BUF_SIZE)
2852 cnt = TRACE_BUF_SIZE;
2854 buf = kmalloc(cnt + 1, GFP_KERNEL);
2858 if (copy_from_user(buf, ubuf, cnt)) {
2863 /* Cut from the first nil or newline. */
2865 end = strchr(buf, '\n');
2869 cnt = mark_printk("%s\n", buf);
2876 static struct file_operations tracing_max_lat_fops = {
2877 .open = tracing_open_generic,
2878 .read = tracing_max_lat_read,
2879 .write = tracing_max_lat_write,
2882 static struct file_operations tracing_ctrl_fops = {
2883 .open = tracing_open_generic,
2884 .read = tracing_ctrl_read,
2885 .write = tracing_ctrl_write,
2888 static struct file_operations set_tracer_fops = {
2889 .open = tracing_open_generic,
2890 .read = tracing_set_trace_read,
2891 .write = tracing_set_trace_write,
2894 static struct file_operations tracing_pipe_fops = {
2895 .open = tracing_open_pipe,
2896 .poll = tracing_poll_pipe,
2897 .read = tracing_read_pipe,
2898 .splice_read = tracing_splice_read_pipe,
2899 .release = tracing_release_pipe,
2902 static struct file_operations tracing_entries_fops = {
2903 .open = tracing_open_generic,
2904 .read = tracing_entries_read,
2905 .write = tracing_entries_write,
2908 static struct file_operations tracing_mark_fops = {
2909 .open = tracing_open_generic,
2910 .write = tracing_mark_write,
2913 #ifdef CONFIG_DYNAMIC_FTRACE
2915 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
2921 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
2922 size_t cnt, loff_t *ppos)
2924 static char ftrace_dyn_info_buffer[1024];
2925 static DEFINE_MUTEX(dyn_info_mutex);
2926 unsigned long *p = filp->private_data;
2927 char *buf = ftrace_dyn_info_buffer;
2928 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
2931 mutex_lock(&dyn_info_mutex);
2932 r = sprintf(buf, "%ld ", *p);
2934 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
2937 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2939 mutex_unlock(&dyn_info_mutex);
2944 static struct file_operations tracing_dyn_info_fops = {
2945 .open = tracing_open_generic,
2946 .read = tracing_read_dyn_info,
2950 static struct dentry *d_tracer;
2952 struct dentry *tracing_init_dentry(void)
2959 d_tracer = debugfs_create_dir("tracing", NULL);
2961 if (!d_tracer && !once) {
2963 pr_warning("Could not create debugfs directory 'tracing'\n");
2970 static struct dentry *d_percpu;
2972 struct dentry *tracing_dentry_percpu(void)
2975 struct dentry *d_tracer;
2980 d_tracer = tracing_init_dentry();
2985 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
2987 if (!d_percpu && !once) {
2989 pr_warning("Could not create debugfs directory 'per_cpu'\n");
2996 static void tracing_init_debugfs_percpu(long cpu)
2998 struct dentry *d_percpu = tracing_dentry_percpu();
2999 struct dentry *entry;
3000 /* strlen(trace_pipe) + MAX(log10(cpu)) + '\0' */
3003 if (cpu > 999 || cpu < 0)
3006 /* per cpu trace_pipe */
3007 sprintf(filename, "trace_pipe%ld", cpu);
3009 entry = debugfs_create_file(filename, 0444, d_percpu,
3010 (void *) cpu, &tracing_pipe_fops);
3012 pr_warning("Could not create debugfs '%s' entry\n", filename);
3015 sprintf(filename, "trace%ld", cpu);
3017 entry = debugfs_create_file(filename, 0444, d_percpu,
3018 (void *) cpu, &tracing_fops);
3020 pr_warning("Could not create debugfs '%s' entry\n", filename);
3023 #ifdef CONFIG_FTRACE_SELFTEST
3024 /* Let selftest have access to static functions in this file */
3025 #include "trace_selftest.c"
3028 static __init int tracer_init_debugfs(void)
3030 struct dentry *d_tracer;
3031 struct dentry *entry;
3034 d_tracer = tracing_init_dentry();
3036 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3037 &global_trace, &tracing_ctrl_fops);
3039 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3041 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3042 NULL, &tracing_iter_fops);
3044 pr_warning("Could not create debugfs 'trace_options' entry\n");
3046 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3047 NULL, &tracing_cpumask_fops);
3049 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3051 entry = debugfs_create_file("trace", 0444, d_tracer,
3052 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3054 pr_warning("Could not create debugfs 'trace' entry\n");
3056 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3057 &global_trace, &show_traces_fops);
3059 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3061 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3062 &global_trace, &set_tracer_fops);
3064 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3066 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3067 &tracing_max_latency,
3068 &tracing_max_lat_fops);
3070 pr_warning("Could not create debugfs "
3071 "'tracing_max_latency' entry\n");
3073 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3074 &tracing_thresh, &tracing_max_lat_fops);
3076 pr_warning("Could not create debugfs "
3077 "'tracing_thresh' entry\n");
3078 entry = debugfs_create_file("README", 0644, d_tracer,
3079 NULL, &tracing_readme_fops);
3081 pr_warning("Could not create debugfs 'README' entry\n");
3083 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3084 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3086 pr_warning("Could not create debugfs "
3087 "'trace_pipe' entry\n");
3089 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3090 &global_trace, &tracing_entries_fops);
3092 pr_warning("Could not create debugfs "
3093 "'buffer_size_kb' entry\n");
3095 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3096 NULL, &tracing_mark_fops);
3098 pr_warning("Could not create debugfs "
3099 "'trace_marker' entry\n");
3101 #ifdef CONFIG_DYNAMIC_FTRACE
3102 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3103 &ftrace_update_tot_cnt,
3104 &tracing_dyn_info_fops);
3106 pr_warning("Could not create debugfs "
3107 "'dyn_ftrace_total_info' entry\n");
3109 #ifdef CONFIG_SYSPROF_TRACER
3110 init_tracer_sysprof_debugfs(d_tracer);
3113 for_each_tracing_cpu(cpu)
3114 tracing_init_debugfs_percpu(cpu);
3119 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
3121 static DEFINE_SPINLOCK(trace_buf_lock);
3122 static char trace_buf[TRACE_BUF_SIZE];
3124 struct ring_buffer_event *event;
3125 struct trace_array *tr = &global_trace;
3126 struct trace_array_cpu *data;
3127 int cpu, len = 0, size, pc;
3128 struct print_entry *entry;
3129 unsigned long irq_flags;
3131 if (tracing_disabled || tracing_selftest_running)
3134 pc = preempt_count();
3135 preempt_disable_notrace();
3136 cpu = raw_smp_processor_id();
3137 data = tr->data[cpu];
3139 if (unlikely(atomic_read(&data->disabled)))
3142 pause_graph_tracing();
3143 spin_lock_irqsave(&trace_buf_lock, irq_flags);
3144 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3146 len = min(len, TRACE_BUF_SIZE-1);
3149 size = sizeof(*entry) + len + 1;
3150 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
3153 entry = ring_buffer_event_data(event);
3155 entry->depth = depth;
3157 memcpy(&entry->buf, trace_buf, len);
3158 entry->buf[len] = 0;
3159 ring_buffer_unlock_commit(tr->buffer, event);
3162 spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
3163 unpause_graph_tracing();
3165 preempt_enable_notrace();
3169 EXPORT_SYMBOL_GPL(trace_vprintk);
3171 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3176 if (!(trace_flags & TRACE_ITER_PRINTK))
3180 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3184 EXPORT_SYMBOL_GPL(__ftrace_printk);
3186 int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
3188 if (!(trace_flags & TRACE_ITER_PRINTK))
3191 return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3193 EXPORT_SYMBOL_GPL(__ftrace_vprintk);
3195 static int trace_panic_handler(struct notifier_block *this,
3196 unsigned long event, void *unused)
3198 if (ftrace_dump_on_oops)
3203 static struct notifier_block trace_panic_notifier = {
3204 .notifier_call = trace_panic_handler,
3206 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3209 static int trace_die_handler(struct notifier_block *self,
3215 if (ftrace_dump_on_oops)
3224 static struct notifier_block trace_die_notifier = {
3225 .notifier_call = trace_die_handler,
3230 * printk is set to max of 1024, we really don't need it that big.
3231 * Nothing should be printing 1000 characters anyway.
3233 #define TRACE_MAX_PRINT 1000
3236 * Define here KERN_TRACE so that we have one place to modify
3237 * it if we decide to change what log level the ftrace dump
3240 #define KERN_TRACE KERN_EMERG
3243 trace_printk_seq(struct trace_seq *s)
3245 /* Probably should print a warning here. */
3249 /* should be zero ended, but we are paranoid. */
3250 s->buffer[s->len] = 0;
3252 printk(KERN_TRACE "%s", s->buffer);
3257 void ftrace_dump(void)
3259 static DEFINE_SPINLOCK(ftrace_dump_lock);
3260 /* use static because iter can be a bit big for the stack */
3261 static struct trace_iterator iter;
3262 static int dump_ran;
3263 unsigned long flags;
3267 spin_lock_irqsave(&ftrace_dump_lock, flags);
3273 /* No turning back! */
3277 for_each_tracing_cpu(cpu) {
3278 atomic_inc(&global_trace.data[cpu]->disabled);
3281 /* don't look at user memory in panic mode */
3282 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3284 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3286 iter.tr = &global_trace;
3287 iter.trace = current_trace;
3290 * We need to stop all tracing on all CPUS to read the
3291 * the next buffer. This is a bit expensive, but is
3292 * not done often. We fill all what we can read,
3293 * and then release the locks again.
3296 while (!trace_empty(&iter)) {
3299 printk(KERN_TRACE "---------------------------------\n");
3303 /* reset all but tr, trace, and overruns */
3304 memset(&iter.seq, 0,
3305 sizeof(struct trace_iterator) -
3306 offsetof(struct trace_iterator, seq));
3307 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3310 if (find_next_entry_inc(&iter) != NULL) {
3311 print_trace_line(&iter);
3312 trace_consume(&iter);
3315 trace_printk_seq(&iter.seq);
3319 printk(KERN_TRACE " (ftrace buffer empty)\n");
3321 printk(KERN_TRACE "---------------------------------\n");
3324 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3327 __init static int tracer_alloc_buffers(void)
3329 struct trace_array_cpu *data;
3333 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3336 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3337 goto out_free_buffer_mask;
3339 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
3340 goto out_free_tracing_cpumask;
3342 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3343 cpumask_copy(tracing_cpumask, cpu_all_mask);
3344 cpumask_clear(tracing_reader_cpumask);
3346 /* TODO: make the number of buffers hot pluggable with CPUS */
3347 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3348 TRACE_BUFFER_FLAGS);
3349 if (!global_trace.buffer) {
3350 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3352 goto out_free_cpumask;
3354 global_trace.entries = ring_buffer_size(global_trace.buffer);
3357 #ifdef CONFIG_TRACER_MAX_TRACE
3358 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3359 TRACE_BUFFER_FLAGS);
3360 if (!max_tr.buffer) {
3361 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3363 ring_buffer_free(global_trace.buffer);
3364 goto out_free_cpumask;
3366 max_tr.entries = ring_buffer_size(max_tr.buffer);
3367 WARN_ON(max_tr.entries != global_trace.entries);
3370 /* Allocate the first page for all buffers */
3371 for_each_tracing_cpu(i) {
3372 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3373 max_tr.data[i] = &per_cpu(max_data, i);
3376 trace_init_cmdlines();
3378 register_tracer(&nop_trace);
3379 current_trace = &nop_trace;
3380 #ifdef CONFIG_BOOT_TRACER
3381 register_tracer(&boot_tracer);
3383 /* All seems OK, enable tracing */
3384 tracing_disabled = 0;
3386 atomic_notifier_chain_register(&panic_notifier_list,
3387 &trace_panic_notifier);
3389 register_die_notifier(&trace_die_notifier);
3393 free_cpumask_var(tracing_reader_cpumask);
3394 out_free_tracing_cpumask:
3395 free_cpumask_var(tracing_cpumask);
3396 out_free_buffer_mask:
3397 free_cpumask_var(tracing_buffer_mask);
3402 __init static int clear_boot_tracer(void)
3405 * The default tracer at boot buffer is an init section.
3406 * This function is called in lateinit. If we did not
3407 * find the boot tracer, then clear it out, to prevent
3408 * later registration from accessing the buffer that is
3409 * about to be freed.
3411 if (!default_bootup_tracer)
3414 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
3415 default_bootup_tracer);
3416 default_bootup_tracer = NULL;
3421 early_initcall(tracer_alloc_buffers);
3422 fs_initcall(tracer_init_debugfs);
3423 late_initcall(clear_boot_tracer);