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 for_each_tracing_cpu(cpu) \
102 for_each_cpu(cpu, tracing_buffer_mask)
105 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
107 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
108 * is set, then ftrace_dump is called. This will output the contents
109 * of the ftrace buffers to the console. This is very useful for
110 * capturing traces that lead to crashes and outputing it to a
113 * It is default off, but you can enable it with either specifying
114 * "ftrace_dump_on_oops" in the kernel command line, or setting
115 * /proc/sys/kernel/ftrace_dump_on_oops to true.
117 int ftrace_dump_on_oops;
119 static int tracing_set_tracer(const char *buf);
121 #define BOOTUP_TRACER_SIZE 100
122 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
123 static char *default_bootup_tracer;
125 static int __init set_ftrace(char *str)
127 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
128 default_bootup_tracer = bootup_tracer_buf;
131 __setup("ftrace=", set_ftrace);
133 static int __init set_ftrace_dump_on_oops(char *str)
135 ftrace_dump_on_oops = 1;
138 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
141 ns2usecs(cycle_t nsec)
148 cycle_t ftrace_now(int cpu)
150 u64 ts = ring_buffer_time_stamp(cpu);
151 ring_buffer_normalize_time_stamp(cpu, &ts);
156 * The global_trace is the descriptor that holds the tracing
157 * buffers for the live tracing. For each CPU, it contains
158 * a link list of pages that will store trace entries. The
159 * page descriptor of the pages in the memory is used to hold
160 * the link list by linking the lru item in the page descriptor
161 * to each of the pages in the buffer per CPU.
163 * For each active CPU there is a data field that holds the
164 * pages for the buffer for that CPU. Each CPU has the same number
165 * of pages allocated for its buffer.
167 static struct trace_array global_trace;
169 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
172 * The max_tr is used to snapshot the global_trace when a maximum
173 * latency is reached. Some tracers will use this to store a maximum
174 * trace while it continues examining live traces.
176 * The buffers for the max_tr are set up the same as the global_trace.
177 * When a snapshot is taken, the link list of the max_tr is swapped
178 * with the link list of the global_trace and the buffers are reset for
179 * the global_trace so the tracing can continue.
181 static struct trace_array max_tr;
183 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
185 /* tracer_enabled is used to toggle activation of a tracer */
186 static int tracer_enabled = 1;
189 * tracing_is_enabled - return tracer_enabled status
191 * This function is used by other tracers to know the status
192 * of the tracer_enabled flag. Tracers may use this function
193 * to know if it should enable their features when starting
194 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
196 int tracing_is_enabled(void)
198 return tracer_enabled;
202 * trace_buf_size is the size in bytes that is allocated
203 * for a buffer. Note, the number of bytes is always rounded
206 * This number is purposely set to a low number of 16384.
207 * If the dump on oops happens, it will be much appreciated
208 * to not have to wait for all that output. Anyway this can be
209 * boot time and run time configurable.
211 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
213 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
215 /* trace_types holds a link list of available tracers. */
216 static struct tracer *trace_types __read_mostly;
218 /* current_trace points to the tracer that is currently active */
219 static struct tracer *current_trace __read_mostly;
222 * max_tracer_type_len is used to simplify the allocating of
223 * buffers to read userspace tracer names. We keep track of
224 * the longest tracer name registered.
226 static int max_tracer_type_len;
229 * trace_types_lock is used to protect the trace_types list.
230 * This lock is also used to keep user access serialized.
231 * Accesses from userspace will grab this lock while userspace
232 * activities happen inside the kernel.
234 static DEFINE_MUTEX(trace_types_lock);
236 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
237 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
239 /* trace_flags holds trace_options default values */
240 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
241 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
244 * trace_wake_up - wake up tasks waiting for trace input
246 * Simply wakes up any task that is blocked on the trace_wait
247 * queue. These is used with trace_poll for tasks polling the trace.
249 void trace_wake_up(void)
252 * The runqueue_is_locked() can fail, but this is the best we
255 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
256 wake_up(&trace_wait);
259 static int __init set_buf_size(char *str)
261 unsigned long buf_size;
266 ret = strict_strtoul(str, 0, &buf_size);
267 /* nr_entries can not be zero */
268 if (ret < 0 || buf_size == 0)
270 trace_buf_size = buf_size;
273 __setup("trace_buf_size=", set_buf_size);
275 unsigned long nsecs_to_usecs(unsigned long nsecs)
280 /* These must match the bit postions in trace_iterator_flags */
281 static const char *trace_options[] = {
304 * ftrace_max_lock is used to protect the swapping of buffers
305 * when taking a max snapshot. The buffers themselves are
306 * protected by per_cpu spinlocks. But the action of the swap
307 * needs its own lock.
309 * This is defined as a raw_spinlock_t in order to help
310 * with performance when lockdep debugging is enabled.
312 static raw_spinlock_t ftrace_max_lock =
313 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
316 * Copy the new maximum trace into the separate maximum-trace
317 * structure. (this way the maximum trace is permanently saved,
318 * for later retrieval via /debugfs/tracing/latency_trace)
321 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
323 struct trace_array_cpu *data = tr->data[cpu];
326 max_tr.time_start = data->preempt_timestamp;
328 data = max_tr.data[cpu];
329 data->saved_latency = tracing_max_latency;
331 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
332 data->pid = tsk->pid;
333 data->uid = task_uid(tsk);
334 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
335 data->policy = tsk->policy;
336 data->rt_priority = tsk->rt_priority;
338 /* record this tasks comm */
339 tracing_record_cmdline(tsk);
343 trace_seq_reset(struct trace_seq *s)
349 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
354 if (s->len <= s->readpos)
357 len = s->len - s->readpos;
360 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
368 ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
373 if (s->len <= s->readpos)
376 len = s->len - s->readpos;
379 ret = memcpy(buf, s->buffer + s->readpos, cnt);
388 trace_print_seq(struct seq_file *m, struct trace_seq *s)
390 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
393 seq_puts(m, s->buffer);
399 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
401 * @tsk: the task with the latency
402 * @cpu: The cpu that initiated the trace.
404 * Flip the buffers between the @tr and the max_tr and record information
405 * about which task was the cause of this latency.
408 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
410 struct ring_buffer *buf = tr->buffer;
412 WARN_ON_ONCE(!irqs_disabled());
413 __raw_spin_lock(&ftrace_max_lock);
415 tr->buffer = max_tr.buffer;
418 ftrace_disable_cpu();
419 ring_buffer_reset(tr->buffer);
422 __update_max_tr(tr, tsk, cpu);
423 __raw_spin_unlock(&ftrace_max_lock);
427 * update_max_tr_single - only copy one trace over, and reset the rest
429 * @tsk - task with the latency
430 * @cpu - the cpu of the buffer to copy.
432 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
435 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
439 WARN_ON_ONCE(!irqs_disabled());
440 __raw_spin_lock(&ftrace_max_lock);
442 ftrace_disable_cpu();
444 ring_buffer_reset(max_tr.buffer);
445 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
449 WARN_ON_ONCE(ret && ret != -EAGAIN);
451 __update_max_tr(tr, tsk, cpu);
452 __raw_spin_unlock(&ftrace_max_lock);
456 * register_tracer - register a tracer with the ftrace system.
457 * @type - the plugin for the tracer
459 * Register a new plugin tracer.
461 int register_tracer(struct tracer *type)
462 __releases(kernel_lock)
463 __acquires(kernel_lock)
470 pr_info("Tracer must have a name\n");
475 * When this gets called we hold the BKL which means that
476 * preemption is disabled. Various trace selftests however
477 * need to disable and enable preemption for successful tests.
478 * So we drop the BKL here and grab it after the tests again.
481 mutex_lock(&trace_types_lock);
483 tracing_selftest_running = true;
485 for (t = trace_types; t; t = t->next) {
486 if (strcmp(type->name, t->name) == 0) {
488 pr_info("Trace %s already registered\n",
496 type->set_flag = &dummy_set_flag;
498 type->flags = &dummy_tracer_flags;
500 if (!type->flags->opts)
501 type->flags->opts = dummy_tracer_opt;
502 if (!type->wait_pipe)
503 type->wait_pipe = default_wait_pipe;
506 #ifdef CONFIG_FTRACE_STARTUP_TEST
507 if (type->selftest && !tracing_selftest_disabled) {
508 struct tracer *saved_tracer = current_trace;
509 struct trace_array *tr = &global_trace;
513 * Run a selftest on this tracer.
514 * Here we reset the trace buffer, and set the current
515 * tracer to be this tracer. The tracer can then run some
516 * internal tracing to verify that everything is in order.
517 * If we fail, we do not register this tracer.
519 for_each_tracing_cpu(i)
520 tracing_reset(tr, i);
522 current_trace = type;
523 /* the test is responsible for initializing and enabling */
524 pr_info("Testing tracer %s: ", type->name);
525 ret = type->selftest(type, tr);
526 /* the test is responsible for resetting too */
527 current_trace = saved_tracer;
529 printk(KERN_CONT "FAILED!\n");
532 /* Only reset on passing, to avoid touching corrupted buffers */
533 for_each_tracing_cpu(i)
534 tracing_reset(tr, i);
536 printk(KERN_CONT "PASSED\n");
540 type->next = trace_types;
542 len = strlen(type->name);
543 if (len > max_tracer_type_len)
544 max_tracer_type_len = len;
547 tracing_selftest_running = false;
548 mutex_unlock(&trace_types_lock);
550 if (ret || !default_bootup_tracer)
553 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
556 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
557 /* Do we want this tracer to start on bootup? */
558 tracing_set_tracer(type->name);
559 default_bootup_tracer = NULL;
560 /* disable other selftests, since this will break it. */
561 tracing_selftest_disabled = 1;
562 #ifdef CONFIG_FTRACE_STARTUP_TEST
563 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
572 void unregister_tracer(struct tracer *type)
577 mutex_lock(&trace_types_lock);
578 for (t = &trace_types; *t; t = &(*t)->next) {
582 pr_info("Trace %s not registered\n", type->name);
588 if (type == current_trace && tracer_enabled) {
591 if (current_trace->stop)
592 current_trace->stop(&global_trace);
593 current_trace = &nop_trace;
596 if (strlen(type->name) != max_tracer_type_len)
599 max_tracer_type_len = 0;
600 for (t = &trace_types; *t; t = &(*t)->next) {
601 len = strlen((*t)->name);
602 if (len > max_tracer_type_len)
603 max_tracer_type_len = len;
606 mutex_unlock(&trace_types_lock);
609 void tracing_reset(struct trace_array *tr, int cpu)
611 ftrace_disable_cpu();
612 ring_buffer_reset_cpu(tr->buffer, cpu);
616 void tracing_reset_online_cpus(struct trace_array *tr)
620 tr->time_start = ftrace_now(tr->cpu);
622 for_each_online_cpu(cpu)
623 tracing_reset(tr, cpu);
626 #define SAVED_CMDLINES 128
627 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
628 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
629 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
630 static int cmdline_idx;
631 static DEFINE_SPINLOCK(trace_cmdline_lock);
633 /* temporary disable recording */
634 static atomic_t trace_record_cmdline_disabled __read_mostly;
636 static void trace_init_cmdlines(void)
638 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
639 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
643 static int trace_stop_count;
644 static DEFINE_SPINLOCK(tracing_start_lock);
647 * ftrace_off_permanent - disable all ftrace code permanently
649 * This should only be called when a serious anomally has
650 * been detected. This will turn off the function tracing,
651 * ring buffers, and other tracing utilites. It takes no
652 * locks and can be called from any context.
654 void ftrace_off_permanent(void)
656 tracing_disabled = 1;
658 tracing_off_permanent();
662 * tracing_start - quick start of the tracer
664 * If tracing is enabled but was stopped by tracing_stop,
665 * this will start the tracer back up.
667 void tracing_start(void)
669 struct ring_buffer *buffer;
672 if (tracing_disabled)
675 spin_lock_irqsave(&tracing_start_lock, flags);
676 if (--trace_stop_count) {
677 if (trace_stop_count < 0) {
678 /* Someone screwed up their debugging */
680 trace_stop_count = 0;
686 buffer = global_trace.buffer;
688 ring_buffer_record_enable(buffer);
690 buffer = max_tr.buffer;
692 ring_buffer_record_enable(buffer);
696 spin_unlock_irqrestore(&tracing_start_lock, flags);
700 * tracing_stop - quick stop of the tracer
702 * Light weight way to stop tracing. Use in conjunction with
705 void tracing_stop(void)
707 struct ring_buffer *buffer;
711 spin_lock_irqsave(&tracing_start_lock, flags);
712 if (trace_stop_count++)
715 buffer = global_trace.buffer;
717 ring_buffer_record_disable(buffer);
719 buffer = max_tr.buffer;
721 ring_buffer_record_disable(buffer);
724 spin_unlock_irqrestore(&tracing_start_lock, flags);
727 void trace_stop_cmdline_recording(void);
729 static void trace_save_cmdline(struct task_struct *tsk)
734 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
738 * It's not the end of the world if we don't get
739 * the lock, but we also don't want to spin
740 * nor do we want to disable interrupts,
741 * so if we miss here, then better luck next time.
743 if (!spin_trylock(&trace_cmdline_lock))
746 idx = map_pid_to_cmdline[tsk->pid];
747 if (idx >= SAVED_CMDLINES) {
748 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
750 map = map_cmdline_to_pid[idx];
751 if (map <= PID_MAX_DEFAULT)
752 map_pid_to_cmdline[map] = (unsigned)-1;
754 map_pid_to_cmdline[tsk->pid] = idx;
759 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
761 spin_unlock(&trace_cmdline_lock);
764 char *trace_find_cmdline(int pid)
766 char *cmdline = "<...>";
772 if (pid > PID_MAX_DEFAULT)
775 map = map_pid_to_cmdline[pid];
776 if (map >= SAVED_CMDLINES)
779 cmdline = saved_cmdlines[map];
785 void tracing_record_cmdline(struct task_struct *tsk)
787 if (atomic_read(&trace_record_cmdline_disabled))
790 trace_save_cmdline(tsk);
794 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
797 struct task_struct *tsk = current;
799 entry->preempt_count = pc & 0xff;
800 entry->pid = (tsk) ? tsk->pid : 0;
801 entry->tgid = (tsk) ? tsk->tgid : 0;
803 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
804 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
806 TRACE_FLAG_IRQS_NOSUPPORT |
808 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
809 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
810 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
813 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
816 unsigned long flags, int pc)
818 struct ring_buffer_event *event;
820 event = ring_buffer_lock_reserve(tr->buffer, len);
822 struct trace_entry *ent = ring_buffer_event_data(event);
824 tracing_generic_entry_update(ent, flags, pc);
830 static void ftrace_trace_stack(struct trace_array *tr,
831 unsigned long flags, int skip, int pc);
832 static void ftrace_trace_userstack(struct trace_array *tr,
833 unsigned long flags, int pc);
835 void trace_buffer_unlock_commit(struct trace_array *tr,
836 struct ring_buffer_event *event,
837 unsigned long flags, int pc)
839 ring_buffer_unlock_commit(tr->buffer, event);
841 ftrace_trace_stack(tr, flags, 6, pc);
842 ftrace_trace_userstack(tr, flags, pc);
847 trace_function(struct trace_array *tr,
848 unsigned long ip, unsigned long parent_ip, unsigned long flags,
851 struct ring_buffer_event *event;
852 struct ftrace_entry *entry;
854 /* If we are reading the ring buffer, don't trace */
855 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
858 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
862 entry = ring_buffer_event_data(event);
864 entry->parent_ip = parent_ip;
865 ring_buffer_unlock_commit(tr->buffer, event);
868 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
869 static void __trace_graph_entry(struct trace_array *tr,
870 struct ftrace_graph_ent *trace,
874 struct ring_buffer_event *event;
875 struct ftrace_graph_ent_entry *entry;
877 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
880 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
881 sizeof(*entry), flags, pc);
884 entry = ring_buffer_event_data(event);
885 entry->graph_ent = *trace;
886 ring_buffer_unlock_commit(global_trace.buffer, event);
889 static void __trace_graph_return(struct trace_array *tr,
890 struct ftrace_graph_ret *trace,
894 struct ring_buffer_event *event;
895 struct ftrace_graph_ret_entry *entry;
897 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
900 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
901 sizeof(*entry), flags, pc);
904 entry = ring_buffer_event_data(event);
906 ring_buffer_unlock_commit(global_trace.buffer, event);
911 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
912 unsigned long ip, unsigned long parent_ip, unsigned long flags,
915 if (likely(!atomic_read(&data->disabled)))
916 trace_function(tr, ip, parent_ip, flags, pc);
919 static void __ftrace_trace_stack(struct trace_array *tr,
923 #ifdef CONFIG_STACKTRACE
924 struct ring_buffer_event *event;
925 struct stack_entry *entry;
926 struct stack_trace trace;
928 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
929 sizeof(*entry), flags, pc);
932 entry = ring_buffer_event_data(event);
933 memset(&entry->caller, 0, sizeof(entry->caller));
935 trace.nr_entries = 0;
936 trace.max_entries = FTRACE_STACK_ENTRIES;
938 trace.entries = entry->caller;
940 save_stack_trace(&trace);
941 ring_buffer_unlock_commit(tr->buffer, event);
945 static void ftrace_trace_stack(struct trace_array *tr,
949 if (!(trace_flags & TRACE_ITER_STACKTRACE))
952 __ftrace_trace_stack(tr, flags, skip, pc);
955 void __trace_stack(struct trace_array *tr,
959 __ftrace_trace_stack(tr, flags, skip, pc);
962 static void ftrace_trace_userstack(struct trace_array *tr,
963 unsigned long flags, int pc)
965 #ifdef CONFIG_STACKTRACE
966 struct ring_buffer_event *event;
967 struct userstack_entry *entry;
968 struct stack_trace trace;
970 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
973 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
974 sizeof(*entry), flags, pc);
977 entry = ring_buffer_event_data(event);
979 memset(&entry->caller, 0, sizeof(entry->caller));
981 trace.nr_entries = 0;
982 trace.max_entries = FTRACE_STACK_ENTRIES;
984 trace.entries = entry->caller;
986 save_stack_trace_user(&trace);
987 ring_buffer_unlock_commit(tr->buffer, event);
992 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
994 ftrace_trace_userstack(tr, flags, preempt_count());
999 ftrace_trace_special(void *__tr,
1000 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1003 struct ring_buffer_event *event;
1004 struct trace_array *tr = __tr;
1005 struct special_entry *entry;
1007 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1008 sizeof(*entry), 0, pc);
1011 entry = ring_buffer_event_data(event);
1015 trace_buffer_unlock_commit(tr, event, 0, pc);
1019 __trace_special(void *__tr, void *__data,
1020 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1022 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1026 tracing_sched_switch_trace(struct trace_array *tr,
1027 struct task_struct *prev,
1028 struct task_struct *next,
1029 unsigned long flags, int pc)
1031 struct ring_buffer_event *event;
1032 struct ctx_switch_entry *entry;
1034 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1035 sizeof(*entry), flags, pc);
1038 entry = ring_buffer_event_data(event);
1039 entry->prev_pid = prev->pid;
1040 entry->prev_prio = prev->prio;
1041 entry->prev_state = prev->state;
1042 entry->next_pid = next->pid;
1043 entry->next_prio = next->prio;
1044 entry->next_state = next->state;
1045 entry->next_cpu = task_cpu(next);
1046 trace_buffer_unlock_commit(tr, event, flags, pc);
1050 tracing_sched_wakeup_trace(struct trace_array *tr,
1051 struct task_struct *wakee,
1052 struct task_struct *curr,
1053 unsigned long flags, int pc)
1055 struct ring_buffer_event *event;
1056 struct ctx_switch_entry *entry;
1058 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1059 sizeof(*entry), flags, pc);
1062 entry = ring_buffer_event_data(event);
1063 entry->prev_pid = curr->pid;
1064 entry->prev_prio = curr->prio;
1065 entry->prev_state = curr->state;
1066 entry->next_pid = wakee->pid;
1067 entry->next_prio = wakee->prio;
1068 entry->next_state = wakee->state;
1069 entry->next_cpu = task_cpu(wakee);
1071 ring_buffer_unlock_commit(tr->buffer, event);
1072 ftrace_trace_stack(tr, flags, 6, pc);
1073 ftrace_trace_userstack(tr, flags, pc);
1077 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1079 struct trace_array *tr = &global_trace;
1080 struct trace_array_cpu *data;
1081 unsigned long flags;
1085 if (tracing_disabled)
1088 pc = preempt_count();
1089 local_irq_save(flags);
1090 cpu = raw_smp_processor_id();
1091 data = tr->data[cpu];
1093 if (likely(atomic_inc_return(&data->disabled) == 1))
1094 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1096 atomic_dec(&data->disabled);
1097 local_irq_restore(flags);
1100 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1101 int trace_graph_entry(struct ftrace_graph_ent *trace)
1103 struct trace_array *tr = &global_trace;
1104 struct trace_array_cpu *data;
1105 unsigned long flags;
1110 if (!ftrace_trace_task(current))
1113 if (!ftrace_graph_addr(trace->func))
1116 local_irq_save(flags);
1117 cpu = raw_smp_processor_id();
1118 data = tr->data[cpu];
1119 disabled = atomic_inc_return(&data->disabled);
1120 if (likely(disabled == 1)) {
1121 pc = preempt_count();
1122 __trace_graph_entry(tr, trace, flags, pc);
1124 /* Only do the atomic if it is not already set */
1125 if (!test_tsk_trace_graph(current))
1126 set_tsk_trace_graph(current);
1127 atomic_dec(&data->disabled);
1128 local_irq_restore(flags);
1133 void trace_graph_return(struct ftrace_graph_ret *trace)
1135 struct trace_array *tr = &global_trace;
1136 struct trace_array_cpu *data;
1137 unsigned long flags;
1142 local_irq_save(flags);
1143 cpu = raw_smp_processor_id();
1144 data = tr->data[cpu];
1145 disabled = atomic_inc_return(&data->disabled);
1146 if (likely(disabled == 1)) {
1147 pc = preempt_count();
1148 __trace_graph_return(tr, trace, flags, pc);
1151 clear_tsk_trace_graph(current);
1152 atomic_dec(&data->disabled);
1153 local_irq_restore(flags);
1155 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1157 enum trace_file_type {
1158 TRACE_FILE_LAT_FMT = 1,
1159 TRACE_FILE_ANNOTATE = 2,
1162 static void trace_iterator_increment(struct trace_iterator *iter)
1164 /* Don't allow ftrace to trace into the ring buffers */
1165 ftrace_disable_cpu();
1168 if (iter->buffer_iter[iter->cpu])
1169 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1171 ftrace_enable_cpu();
1174 static struct trace_entry *
1175 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1177 struct ring_buffer_event *event;
1178 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1180 /* Don't allow ftrace to trace into the ring buffers */
1181 ftrace_disable_cpu();
1184 event = ring_buffer_iter_peek(buf_iter, ts);
1186 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1188 ftrace_enable_cpu();
1190 return event ? ring_buffer_event_data(event) : NULL;
1193 static struct trace_entry *
1194 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1196 struct ring_buffer *buffer = iter->tr->buffer;
1197 struct trace_entry *ent, *next = NULL;
1198 u64 next_ts = 0, ts;
1202 for_each_tracing_cpu(cpu) {
1204 if (ring_buffer_empty_cpu(buffer, cpu))
1207 ent = peek_next_entry(iter, cpu, &ts);
1210 * Pick the entry with the smallest timestamp:
1212 if (ent && (!next || ts < next_ts)) {
1220 *ent_cpu = next_cpu;
1228 /* Find the next real entry, without updating the iterator itself */
1229 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1230 int *ent_cpu, u64 *ent_ts)
1232 return __find_next_entry(iter, ent_cpu, ent_ts);
1235 /* Find the next real entry, and increment the iterator to the next entry */
1236 static void *find_next_entry_inc(struct trace_iterator *iter)
1238 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1241 trace_iterator_increment(iter);
1243 return iter->ent ? iter : NULL;
1246 static void trace_consume(struct trace_iterator *iter)
1248 /* Don't allow ftrace to trace into the ring buffers */
1249 ftrace_disable_cpu();
1250 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1251 ftrace_enable_cpu();
1254 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1256 struct trace_iterator *iter = m->private;
1262 /* can't go backwards */
1267 ent = find_next_entry_inc(iter);
1271 while (ent && iter->idx < i)
1272 ent = find_next_entry_inc(iter);
1279 static void *s_start(struct seq_file *m, loff_t *pos)
1281 struct trace_iterator *iter = m->private;
1286 mutex_lock(&trace_types_lock);
1288 if (!current_trace || current_trace != iter->trace) {
1289 mutex_unlock(&trace_types_lock);
1293 atomic_inc(&trace_record_cmdline_disabled);
1295 if (*pos != iter->pos) {
1300 ftrace_disable_cpu();
1302 for_each_tracing_cpu(cpu) {
1303 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1306 ftrace_enable_cpu();
1308 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1313 p = s_next(m, p, &l);
1319 static void s_stop(struct seq_file *m, void *p)
1321 atomic_dec(&trace_record_cmdline_disabled);
1322 mutex_unlock(&trace_types_lock);
1325 static void print_lat_help_header(struct seq_file *m)
1327 seq_puts(m, "# _------=> CPU# \n");
1328 seq_puts(m, "# / _-----=> irqs-off \n");
1329 seq_puts(m, "# | / _----=> need-resched \n");
1330 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1331 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1332 seq_puts(m, "# |||| / \n");
1333 seq_puts(m, "# ||||| delay \n");
1334 seq_puts(m, "# cmd pid ||||| time | caller \n");
1335 seq_puts(m, "# \\ / ||||| \\ | / \n");
1338 static void print_func_help_header(struct seq_file *m)
1340 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1341 seq_puts(m, "# | | | | |\n");
1346 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1348 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1349 struct trace_array *tr = iter->tr;
1350 struct trace_array_cpu *data = tr->data[tr->cpu];
1351 struct tracer *type = current_trace;
1352 unsigned long total;
1353 unsigned long entries;
1354 const char *name = "preemption";
1359 entries = ring_buffer_entries(iter->tr->buffer);
1361 ring_buffer_overruns(iter->tr->buffer);
1363 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1365 seq_puts(m, "-----------------------------------"
1366 "---------------------------------\n");
1367 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1368 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1369 nsecs_to_usecs(data->saved_latency),
1373 #if defined(CONFIG_PREEMPT_NONE)
1375 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1377 #elif defined(CONFIG_PREEMPT)
1382 /* These are reserved for later use */
1385 seq_printf(m, " #P:%d)\n", num_online_cpus());
1389 seq_puts(m, " -----------------\n");
1390 seq_printf(m, " | task: %.16s-%d "
1391 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1392 data->comm, data->pid, data->uid, data->nice,
1393 data->policy, data->rt_priority);
1394 seq_puts(m, " -----------------\n");
1396 if (data->critical_start) {
1397 seq_puts(m, " => started at: ");
1398 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1399 trace_print_seq(m, &iter->seq);
1400 seq_puts(m, "\n => ended at: ");
1401 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1402 trace_print_seq(m, &iter->seq);
1409 static void test_cpu_buff_start(struct trace_iterator *iter)
1411 struct trace_seq *s = &iter->seq;
1413 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1416 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1419 if (cpumask_test_cpu(iter->cpu, iter->started))
1422 cpumask_set_cpu(iter->cpu, iter->started);
1423 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1426 static enum print_line_t print_lat_fmt(struct trace_iterator *iter)
1428 struct trace_seq *s = &iter->seq;
1429 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1430 struct trace_event *event;
1431 struct trace_entry *entry = iter->ent;
1433 test_cpu_buff_start(iter);
1435 event = ftrace_find_event(entry->type);
1437 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1438 if (!trace_print_lat_context(iter))
1443 return event->latency_trace(iter, sym_flags);
1445 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1448 return TRACE_TYPE_HANDLED;
1450 return TRACE_TYPE_PARTIAL_LINE;
1453 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1455 struct trace_seq *s = &iter->seq;
1456 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1457 struct trace_entry *entry;
1458 struct trace_event *event;
1462 test_cpu_buff_start(iter);
1464 event = ftrace_find_event(entry->type);
1466 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1467 if (!trace_print_context(iter))
1472 return event->trace(iter, sym_flags);
1474 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1477 return TRACE_TYPE_HANDLED;
1479 return TRACE_TYPE_PARTIAL_LINE;
1482 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1484 struct trace_seq *s = &iter->seq;
1485 struct trace_entry *entry;
1486 struct trace_event *event;
1490 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1491 if (!trace_seq_printf(s, "%d %d %llu ",
1492 entry->pid, iter->cpu, iter->ts))
1496 event = ftrace_find_event(entry->type);
1498 return event->raw(iter, 0);
1500 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1503 return TRACE_TYPE_HANDLED;
1505 return TRACE_TYPE_PARTIAL_LINE;
1508 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1510 struct trace_seq *s = &iter->seq;
1511 unsigned char newline = '\n';
1512 struct trace_entry *entry;
1513 struct trace_event *event;
1517 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1518 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1519 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1520 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1523 event = ftrace_find_event(entry->type);
1525 enum print_line_t ret = event->hex(iter, 0);
1526 if (ret != TRACE_TYPE_HANDLED)
1530 SEQ_PUT_FIELD_RET(s, newline);
1532 return TRACE_TYPE_HANDLED;
1535 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1537 struct trace_seq *s = &iter->seq;
1538 struct trace_entry *entry = iter->ent;
1539 struct print_entry *field;
1542 trace_assign_type(field, entry);
1544 ret = trace_seq_printf(s, "%s", field->buf);
1546 return TRACE_TYPE_PARTIAL_LINE;
1548 return TRACE_TYPE_HANDLED;
1551 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1553 struct trace_seq *s = &iter->seq;
1554 struct trace_entry *entry;
1555 struct trace_event *event;
1559 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1560 SEQ_PUT_FIELD_RET(s, entry->pid);
1561 SEQ_PUT_FIELD_RET(s, iter->cpu);
1562 SEQ_PUT_FIELD_RET(s, iter->ts);
1565 event = ftrace_find_event(entry->type);
1566 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1569 static int trace_empty(struct trace_iterator *iter)
1573 for_each_tracing_cpu(cpu) {
1574 if (iter->buffer_iter[cpu]) {
1575 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1578 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1586 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1588 enum print_line_t ret;
1590 if (iter->trace && iter->trace->print_line) {
1591 ret = iter->trace->print_line(iter);
1592 if (ret != TRACE_TYPE_UNHANDLED)
1596 if (iter->ent->type == TRACE_PRINT &&
1597 trace_flags & TRACE_ITER_PRINTK &&
1598 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1599 return print_printk_msg_only(iter);
1601 if (trace_flags & TRACE_ITER_BIN)
1602 return print_bin_fmt(iter);
1604 if (trace_flags & TRACE_ITER_HEX)
1605 return print_hex_fmt(iter);
1607 if (trace_flags & TRACE_ITER_RAW)
1608 return print_raw_fmt(iter);
1610 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1611 return print_lat_fmt(iter);
1613 return print_trace_fmt(iter);
1616 static int s_show(struct seq_file *m, void *v)
1618 struct trace_iterator *iter = v;
1620 if (iter->ent == NULL) {
1622 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1625 if (iter->trace && iter->trace->print_header)
1626 iter->trace->print_header(m);
1627 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1628 /* print nothing if the buffers are empty */
1629 if (trace_empty(iter))
1631 print_trace_header(m, iter);
1632 if (!(trace_flags & TRACE_ITER_VERBOSE))
1633 print_lat_help_header(m);
1635 if (!(trace_flags & TRACE_ITER_VERBOSE))
1636 print_func_help_header(m);
1639 print_trace_line(iter);
1640 trace_print_seq(m, &iter->seq);
1646 static struct seq_operations tracer_seq_ops = {
1653 static struct trace_iterator *
1654 __tracing_open(struct inode *inode, struct file *file, int *ret)
1656 struct trace_iterator *iter;
1660 if (tracing_disabled) {
1665 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1671 mutex_lock(&trace_types_lock);
1672 if (current_trace && current_trace->print_max)
1675 iter->tr = inode->i_private;
1676 iter->trace = current_trace;
1679 /* Notify the tracer early; before we stop tracing. */
1680 if (iter->trace && iter->trace->open)
1681 iter->trace->open(iter);
1683 /* Annotate start of buffers if we had overruns */
1684 if (ring_buffer_overruns(iter->tr->buffer))
1685 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1688 for_each_tracing_cpu(cpu) {
1690 iter->buffer_iter[cpu] =
1691 ring_buffer_read_start(iter->tr->buffer, cpu);
1693 if (!iter->buffer_iter[cpu])
1697 /* TODO stop tracer */
1698 *ret = seq_open(file, &tracer_seq_ops);
1702 m = file->private_data;
1705 /* stop the trace while dumping */
1708 mutex_unlock(&trace_types_lock);
1714 for_each_tracing_cpu(cpu) {
1715 if (iter->buffer_iter[cpu])
1716 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1718 mutex_unlock(&trace_types_lock);
1721 return ERR_PTR(-ENOMEM);
1724 int tracing_open_generic(struct inode *inode, struct file *filp)
1726 if (tracing_disabled)
1729 filp->private_data = inode->i_private;
1733 static int tracing_release(struct inode *inode, struct file *file)
1735 struct seq_file *m = (struct seq_file *)file->private_data;
1736 struct trace_iterator *iter = m->private;
1739 mutex_lock(&trace_types_lock);
1740 for_each_tracing_cpu(cpu) {
1741 if (iter->buffer_iter[cpu])
1742 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1745 if (iter->trace && iter->trace->close)
1746 iter->trace->close(iter);
1748 /* reenable tracing if it was previously enabled */
1750 mutex_unlock(&trace_types_lock);
1752 seq_release(inode, file);
1757 static int tracing_open(struct inode *inode, struct file *file)
1761 __tracing_open(inode, file, &ret);
1766 static int tracing_lt_open(struct inode *inode, struct file *file)
1768 struct trace_iterator *iter;
1771 iter = __tracing_open(inode, file, &ret);
1774 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1781 t_next(struct seq_file *m, void *v, loff_t *pos)
1783 struct tracer *t = m->private;
1795 static void *t_start(struct seq_file *m, loff_t *pos)
1797 struct tracer *t = m->private;
1800 mutex_lock(&trace_types_lock);
1801 for (; t && l < *pos; t = t_next(m, t, &l))
1807 static void t_stop(struct seq_file *m, void *p)
1809 mutex_unlock(&trace_types_lock);
1812 static int t_show(struct seq_file *m, void *v)
1814 struct tracer *t = v;
1819 seq_printf(m, "%s", t->name);
1828 static struct seq_operations show_traces_seq_ops = {
1835 static int show_traces_open(struct inode *inode, struct file *file)
1839 if (tracing_disabled)
1842 ret = seq_open(file, &show_traces_seq_ops);
1844 struct seq_file *m = file->private_data;
1845 m->private = trace_types;
1851 static struct file_operations tracing_fops = {
1852 .open = tracing_open,
1854 .llseek = seq_lseek,
1855 .release = tracing_release,
1858 static struct file_operations tracing_lt_fops = {
1859 .open = tracing_lt_open,
1861 .llseek = seq_lseek,
1862 .release = tracing_release,
1865 static struct file_operations show_traces_fops = {
1866 .open = show_traces_open,
1868 .release = seq_release,
1872 * Only trace on a CPU if the bitmask is set:
1874 static cpumask_var_t tracing_cpumask;
1877 * The tracer itself will not take this lock, but still we want
1878 * to provide a consistent cpumask to user-space:
1880 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1883 * Temporary storage for the character representation of the
1884 * CPU bitmask (and one more byte for the newline):
1886 static char mask_str[NR_CPUS + 1];
1889 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1890 size_t count, loff_t *ppos)
1894 mutex_lock(&tracing_cpumask_update_lock);
1896 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1897 if (count - len < 2) {
1901 len += sprintf(mask_str + len, "\n");
1902 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1905 mutex_unlock(&tracing_cpumask_update_lock);
1911 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1912 size_t count, loff_t *ppos)
1915 cpumask_var_t tracing_cpumask_new;
1917 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1920 mutex_lock(&tracing_cpumask_update_lock);
1921 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1925 local_irq_disable();
1926 __raw_spin_lock(&ftrace_max_lock);
1927 for_each_tracing_cpu(cpu) {
1929 * Increase/decrease the disabled counter if we are
1930 * about to flip a bit in the cpumask:
1932 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
1933 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
1934 atomic_inc(&global_trace.data[cpu]->disabled);
1936 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
1937 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
1938 atomic_dec(&global_trace.data[cpu]->disabled);
1941 __raw_spin_unlock(&ftrace_max_lock);
1944 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
1946 mutex_unlock(&tracing_cpumask_update_lock);
1947 free_cpumask_var(tracing_cpumask_new);
1952 mutex_unlock(&tracing_cpumask_update_lock);
1953 free_cpumask_var(tracing_cpumask);
1958 static struct file_operations tracing_cpumask_fops = {
1959 .open = tracing_open_generic,
1960 .read = tracing_cpumask_read,
1961 .write = tracing_cpumask_write,
1965 tracing_trace_options_read(struct file *filp, char __user *ubuf,
1966 size_t cnt, loff_t *ppos)
1972 u32 tracer_flags = current_trace->flags->val;
1973 struct tracer_opt *trace_opts = current_trace->flags->opts;
1976 /* calculate max size */
1977 for (i = 0; trace_options[i]; i++) {
1978 len += strlen(trace_options[i]);
1979 len += 3; /* "no" and space */
1983 * Increase the size with names of options specific
1984 * of the current tracer.
1986 for (i = 0; trace_opts[i].name; i++) {
1987 len += strlen(trace_opts[i].name);
1988 len += 3; /* "no" and space */
1991 /* +2 for \n and \0 */
1992 buf = kmalloc(len + 2, GFP_KERNEL);
1996 for (i = 0; trace_options[i]; i++) {
1997 if (trace_flags & (1 << i))
1998 r += sprintf(buf + r, "%s ", trace_options[i]);
2000 r += sprintf(buf + r, "no%s ", trace_options[i]);
2003 for (i = 0; trace_opts[i].name; i++) {
2004 if (tracer_flags & trace_opts[i].bit)
2005 r += sprintf(buf + r, "%s ",
2006 trace_opts[i].name);
2008 r += sprintf(buf + r, "no%s ",
2009 trace_opts[i].name);
2012 r += sprintf(buf + r, "\n");
2013 WARN_ON(r >= len + 2);
2015 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2022 /* Try to assign a tracer specific option */
2023 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2025 struct tracer_flags *trace_flags = trace->flags;
2026 struct tracer_opt *opts = NULL;
2030 for (i = 0; trace_flags->opts[i].name; i++) {
2031 opts = &trace_flags->opts[i];
2032 len = strlen(opts->name);
2034 if (strncmp(cmp, opts->name, len) == 0) {
2035 ret = trace->set_flag(trace_flags->val,
2041 if (!trace_flags->opts[i].name)
2044 /* Refused to handle */
2049 trace_flags->val &= ~opts->bit;
2051 trace_flags->val |= opts->bit;
2057 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2058 size_t cnt, loff_t *ppos)
2066 if (cnt >= sizeof(buf))
2069 if (copy_from_user(&buf, ubuf, cnt))
2074 if (strncmp(buf, "no", 2) == 0) {
2079 for (i = 0; trace_options[i]; i++) {
2080 int len = strlen(trace_options[i]);
2082 if (strncmp(cmp, trace_options[i], len) == 0) {
2084 trace_flags &= ~(1 << i);
2086 trace_flags |= (1 << i);
2091 /* If no option could be set, test the specific tracer options */
2092 if (!trace_options[i]) {
2093 ret = set_tracer_option(current_trace, cmp, neg);
2103 static struct file_operations tracing_iter_fops = {
2104 .open = tracing_open_generic,
2105 .read = tracing_trace_options_read,
2106 .write = tracing_trace_options_write,
2109 static const char readme_msg[] =
2110 "tracing mini-HOWTO:\n\n"
2112 "# mount -t debugfs nodev /debug\n\n"
2113 "# cat /debug/tracing/available_tracers\n"
2114 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2115 "# cat /debug/tracing/current_tracer\n"
2117 "# echo sched_switch > /debug/tracing/current_tracer\n"
2118 "# cat /debug/tracing/current_tracer\n"
2120 "# cat /debug/tracing/trace_options\n"
2121 "noprint-parent nosym-offset nosym-addr noverbose\n"
2122 "# echo print-parent > /debug/tracing/trace_options\n"
2123 "# echo 1 > /debug/tracing/tracing_enabled\n"
2124 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2125 "echo 0 > /debug/tracing/tracing_enabled\n"
2129 tracing_readme_read(struct file *filp, char __user *ubuf,
2130 size_t cnt, loff_t *ppos)
2132 return simple_read_from_buffer(ubuf, cnt, ppos,
2133 readme_msg, strlen(readme_msg));
2136 static struct file_operations tracing_readme_fops = {
2137 .open = tracing_open_generic,
2138 .read = tracing_readme_read,
2142 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2143 size_t cnt, loff_t *ppos)
2148 r = sprintf(buf, "%u\n", tracer_enabled);
2149 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2153 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2154 size_t cnt, loff_t *ppos)
2156 struct trace_array *tr = filp->private_data;
2161 if (cnt >= sizeof(buf))
2164 if (copy_from_user(&buf, ubuf, cnt))
2169 ret = strict_strtoul(buf, 10, &val);
2175 mutex_lock(&trace_types_lock);
2176 if (tracer_enabled ^ val) {
2179 if (current_trace->start)
2180 current_trace->start(tr);
2185 if (current_trace->stop)
2186 current_trace->stop(tr);
2189 mutex_unlock(&trace_types_lock);
2197 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2198 size_t cnt, loff_t *ppos)
2200 char buf[max_tracer_type_len+2];
2203 mutex_lock(&trace_types_lock);
2205 r = sprintf(buf, "%s\n", current_trace->name);
2207 r = sprintf(buf, "\n");
2208 mutex_unlock(&trace_types_lock);
2210 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2213 int tracer_init(struct tracer *t, struct trace_array *tr)
2215 tracing_reset_online_cpus(tr);
2219 static int tracing_set_tracer(const char *buf)
2221 struct trace_array *tr = &global_trace;
2225 mutex_lock(&trace_types_lock);
2226 for (t = trace_types; t; t = t->next) {
2227 if (strcmp(t->name, buf) == 0)
2234 if (t == current_trace)
2237 trace_branch_disable();
2238 if (current_trace && current_trace->reset)
2239 current_trace->reset(tr);
2243 ret = tracer_init(t, tr);
2248 trace_branch_enable(tr);
2250 mutex_unlock(&trace_types_lock);
2256 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2257 size_t cnt, loff_t *ppos)
2259 char buf[max_tracer_type_len+1];
2266 if (cnt > max_tracer_type_len)
2267 cnt = max_tracer_type_len;
2269 if (copy_from_user(&buf, ubuf, cnt))
2274 /* strip ending whitespace. */
2275 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2278 err = tracing_set_tracer(buf);
2288 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2289 size_t cnt, loff_t *ppos)
2291 unsigned long *ptr = filp->private_data;
2295 r = snprintf(buf, sizeof(buf), "%ld\n",
2296 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2297 if (r > sizeof(buf))
2299 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2303 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2304 size_t cnt, loff_t *ppos)
2306 unsigned long *ptr = filp->private_data;
2311 if (cnt >= sizeof(buf))
2314 if (copy_from_user(&buf, ubuf, cnt))
2319 ret = strict_strtoul(buf, 10, &val);
2328 static atomic_t tracing_reader;
2330 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2332 struct trace_iterator *iter;
2334 if (tracing_disabled)
2337 /* We only allow for reader of the pipe */
2338 if (atomic_inc_return(&tracing_reader) != 1) {
2339 atomic_dec(&tracing_reader);
2343 /* create a buffer to store the information to pass to userspace */
2344 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2348 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2353 mutex_lock(&trace_types_lock);
2355 /* trace pipe does not show start of buffer */
2356 cpumask_setall(iter->started);
2358 iter->tr = &global_trace;
2359 iter->trace = current_trace;
2360 filp->private_data = iter;
2362 if (iter->trace->pipe_open)
2363 iter->trace->pipe_open(iter);
2364 mutex_unlock(&trace_types_lock);
2369 static int tracing_release_pipe(struct inode *inode, struct file *file)
2371 struct trace_iterator *iter = file->private_data;
2373 free_cpumask_var(iter->started);
2375 atomic_dec(&tracing_reader);
2381 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2383 struct trace_iterator *iter = filp->private_data;
2385 if (trace_flags & TRACE_ITER_BLOCK) {
2387 * Always select as readable when in blocking mode
2389 return POLLIN | POLLRDNORM;
2391 if (!trace_empty(iter))
2392 return POLLIN | POLLRDNORM;
2393 poll_wait(filp, &trace_wait, poll_table);
2394 if (!trace_empty(iter))
2395 return POLLIN | POLLRDNORM;
2402 void default_wait_pipe(struct trace_iterator *iter)
2406 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2408 if (trace_empty(iter))
2411 finish_wait(&trace_wait, &wait);
2415 * This is a make-shift waitqueue.
2416 * A tracer might use this callback on some rare cases:
2418 * 1) the current tracer might hold the runqueue lock when it wakes up
2419 * a reader, hence a deadlock (sched, function, and function graph tracers)
2420 * 2) the function tracers, trace all functions, we don't want
2421 * the overhead of calling wake_up and friends
2422 * (and tracing them too)
2424 * Anyway, this is really very primitive wakeup.
2426 void poll_wait_pipe(struct trace_iterator *iter)
2428 set_current_state(TASK_INTERRUPTIBLE);
2429 /* sleep for 100 msecs, and try again. */
2430 schedule_timeout(HZ / 10);
2433 /* Must be called with trace_types_lock mutex held. */
2434 static int tracing_wait_pipe(struct file *filp)
2436 struct trace_iterator *iter = filp->private_data;
2438 while (trace_empty(iter)) {
2440 if ((filp->f_flags & O_NONBLOCK)) {
2444 mutex_unlock(&trace_types_lock);
2446 iter->trace->wait_pipe(iter);
2448 mutex_lock(&trace_types_lock);
2450 if (signal_pending(current))
2453 if (iter->trace != current_trace)
2457 * We block until we read something and tracing is disabled.
2458 * We still block if tracing is disabled, but we have never
2459 * read anything. This allows a user to cat this file, and
2460 * then enable tracing. But after we have read something,
2461 * we give an EOF when tracing is again disabled.
2463 * iter->pos will be 0 if we haven't read anything.
2465 if (!tracer_enabled && iter->pos)
2476 tracing_read_pipe(struct file *filp, char __user *ubuf,
2477 size_t cnt, loff_t *ppos)
2479 struct trace_iterator *iter = filp->private_data;
2482 /* return any leftover data */
2483 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2487 trace_seq_reset(&iter->seq);
2489 mutex_lock(&trace_types_lock);
2490 if (iter->trace->read) {
2491 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2497 sret = tracing_wait_pipe(filp);
2501 /* stop when tracing is finished */
2502 if (trace_empty(iter)) {
2507 if (cnt >= PAGE_SIZE)
2508 cnt = PAGE_SIZE - 1;
2510 /* reset all but tr, trace, and overruns */
2511 memset(&iter->seq, 0,
2512 sizeof(struct trace_iterator) -
2513 offsetof(struct trace_iterator, seq));
2516 while (find_next_entry_inc(iter) != NULL) {
2517 enum print_line_t ret;
2518 int len = iter->seq.len;
2520 ret = print_trace_line(iter);
2521 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2522 /* don't print partial lines */
2523 iter->seq.len = len;
2526 if (ret != TRACE_TYPE_NO_CONSUME)
2527 trace_consume(iter);
2529 if (iter->seq.len >= cnt)
2533 /* Now copy what we have to the user */
2534 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2535 if (iter->seq.readpos >= iter->seq.len)
2536 trace_seq_reset(&iter->seq);
2539 * If there was nothing to send to user, inspite of consuming trace
2540 * entries, go back to wait for more entries.
2546 mutex_unlock(&trace_types_lock);
2551 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2552 struct pipe_buffer *buf)
2554 __free_page(buf->page);
2557 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2560 __free_page(spd->pages[idx]);
2563 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2565 .map = generic_pipe_buf_map,
2566 .unmap = generic_pipe_buf_unmap,
2567 .confirm = generic_pipe_buf_confirm,
2568 .release = tracing_pipe_buf_release,
2569 .steal = generic_pipe_buf_steal,
2570 .get = generic_pipe_buf_get,
2574 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2579 /* Seq buffer is page-sized, exactly what we need. */
2581 count = iter->seq.len;
2582 ret = print_trace_line(iter);
2583 count = iter->seq.len - count;
2586 iter->seq.len -= count;
2589 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2590 iter->seq.len -= count;
2594 trace_consume(iter);
2596 if (!find_next_entry_inc(iter)) {
2606 static ssize_t tracing_splice_read_pipe(struct file *filp,
2608 struct pipe_inode_info *pipe,
2612 struct page *pages[PIPE_BUFFERS];
2613 struct partial_page partial[PIPE_BUFFERS];
2614 struct trace_iterator *iter = filp->private_data;
2615 struct splice_pipe_desc spd = {
2618 .nr_pages = 0, /* This gets updated below. */
2620 .ops = &tracing_pipe_buf_ops,
2621 .spd_release = tracing_spd_release_pipe,
2627 mutex_lock(&trace_types_lock);
2629 if (iter->trace->splice_read) {
2630 ret = iter->trace->splice_read(iter, filp,
2631 ppos, pipe, len, flags);
2636 ret = tracing_wait_pipe(filp);
2640 if (!iter->ent && !find_next_entry_inc(iter)) {
2645 /* Fill as many pages as possible. */
2646 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2647 pages[i] = alloc_page(GFP_KERNEL);
2651 rem = tracing_fill_pipe_page(rem, iter);
2653 /* Copy the data into the page, so we can start over. */
2654 ret = trace_seq_to_buffer(&iter->seq,
2655 page_address(pages[i]),
2658 __free_page(pages[i]);
2661 partial[i].offset = 0;
2662 partial[i].len = iter->seq.len;
2664 trace_seq_reset(&iter->seq);
2667 mutex_unlock(&trace_types_lock);
2671 return splice_to_pipe(pipe, &spd);
2674 mutex_unlock(&trace_types_lock);
2680 tracing_entries_read(struct file *filp, char __user *ubuf,
2681 size_t cnt, loff_t *ppos)
2683 struct trace_array *tr = filp->private_data;
2687 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2688 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2692 tracing_entries_write(struct file *filp, const char __user *ubuf,
2693 size_t cnt, loff_t *ppos)
2699 if (cnt >= sizeof(buf))
2702 if (copy_from_user(&buf, ubuf, cnt))
2707 ret = strict_strtoul(buf, 10, &val);
2711 /* must have at least 1 entry */
2715 mutex_lock(&trace_types_lock);
2719 /* disable all cpu buffers */
2720 for_each_tracing_cpu(cpu) {
2721 if (global_trace.data[cpu])
2722 atomic_inc(&global_trace.data[cpu]->disabled);
2723 if (max_tr.data[cpu])
2724 atomic_inc(&max_tr.data[cpu]->disabled);
2727 /* value is in KB */
2730 if (val != global_trace.entries) {
2731 ret = ring_buffer_resize(global_trace.buffer, val);
2737 ret = ring_buffer_resize(max_tr.buffer, val);
2741 r = ring_buffer_resize(global_trace.buffer,
2742 global_trace.entries);
2744 /* AARGH! We are left with different
2745 * size max buffer!!!! */
2747 tracing_disabled = 1;
2752 global_trace.entries = val;
2757 /* If check pages failed, return ENOMEM */
2758 if (tracing_disabled)
2761 for_each_tracing_cpu(cpu) {
2762 if (global_trace.data[cpu])
2763 atomic_dec(&global_trace.data[cpu]->disabled);
2764 if (max_tr.data[cpu])
2765 atomic_dec(&max_tr.data[cpu]->disabled);
2769 max_tr.entries = global_trace.entries;
2770 mutex_unlock(&trace_types_lock);
2775 static int mark_printk(const char *fmt, ...)
2779 va_start(args, fmt);
2780 ret = trace_vprintk(0, -1, fmt, args);
2786 tracing_mark_write(struct file *filp, const char __user *ubuf,
2787 size_t cnt, loff_t *fpos)
2792 if (tracing_disabled)
2795 if (cnt > TRACE_BUF_SIZE)
2796 cnt = TRACE_BUF_SIZE;
2798 buf = kmalloc(cnt + 1, GFP_KERNEL);
2802 if (copy_from_user(buf, ubuf, cnt)) {
2807 /* Cut from the first nil or newline. */
2809 end = strchr(buf, '\n');
2813 cnt = mark_printk("%s\n", buf);
2820 static struct file_operations tracing_max_lat_fops = {
2821 .open = tracing_open_generic,
2822 .read = tracing_max_lat_read,
2823 .write = tracing_max_lat_write,
2826 static struct file_operations tracing_ctrl_fops = {
2827 .open = tracing_open_generic,
2828 .read = tracing_ctrl_read,
2829 .write = tracing_ctrl_write,
2832 static struct file_operations set_tracer_fops = {
2833 .open = tracing_open_generic,
2834 .read = tracing_set_trace_read,
2835 .write = tracing_set_trace_write,
2838 static struct file_operations tracing_pipe_fops = {
2839 .open = tracing_open_pipe,
2840 .poll = tracing_poll_pipe,
2841 .read = tracing_read_pipe,
2842 .splice_read = tracing_splice_read_pipe,
2843 .release = tracing_release_pipe,
2846 static struct file_operations tracing_entries_fops = {
2847 .open = tracing_open_generic,
2848 .read = tracing_entries_read,
2849 .write = tracing_entries_write,
2852 static struct file_operations tracing_mark_fops = {
2853 .open = tracing_open_generic,
2854 .write = tracing_mark_write,
2857 #ifdef CONFIG_DYNAMIC_FTRACE
2859 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
2865 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
2866 size_t cnt, loff_t *ppos)
2868 static char ftrace_dyn_info_buffer[1024];
2869 static DEFINE_MUTEX(dyn_info_mutex);
2870 unsigned long *p = filp->private_data;
2871 char *buf = ftrace_dyn_info_buffer;
2872 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
2875 mutex_lock(&dyn_info_mutex);
2876 r = sprintf(buf, "%ld ", *p);
2878 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
2881 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2883 mutex_unlock(&dyn_info_mutex);
2888 static struct file_operations tracing_dyn_info_fops = {
2889 .open = tracing_open_generic,
2890 .read = tracing_read_dyn_info,
2894 static struct dentry *d_tracer;
2896 struct dentry *tracing_init_dentry(void)
2903 d_tracer = debugfs_create_dir("tracing", NULL);
2905 if (!d_tracer && !once) {
2907 pr_warning("Could not create debugfs directory 'tracing'\n");
2914 #ifdef CONFIG_FTRACE_SELFTEST
2915 /* Let selftest have access to static functions in this file */
2916 #include "trace_selftest.c"
2919 static __init int tracer_init_debugfs(void)
2921 struct dentry *d_tracer;
2922 struct dentry *entry;
2924 d_tracer = tracing_init_dentry();
2926 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2927 &global_trace, &tracing_ctrl_fops);
2929 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2931 entry = debugfs_create_file("trace_options", 0644, d_tracer,
2932 NULL, &tracing_iter_fops);
2934 pr_warning("Could not create debugfs 'trace_options' entry\n");
2936 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2937 NULL, &tracing_cpumask_fops);
2939 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2941 entry = debugfs_create_file("trace", 0444, d_tracer,
2942 &global_trace, &tracing_fops);
2944 pr_warning("Could not create debugfs 'trace' entry\n");
2946 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2947 &global_trace, &show_traces_fops);
2949 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2951 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2952 &global_trace, &set_tracer_fops);
2954 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2956 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2957 &tracing_max_latency,
2958 &tracing_max_lat_fops);
2960 pr_warning("Could not create debugfs "
2961 "'tracing_max_latency' entry\n");
2963 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2964 &tracing_thresh, &tracing_max_lat_fops);
2966 pr_warning("Could not create debugfs "
2967 "'tracing_thresh' entry\n");
2968 entry = debugfs_create_file("README", 0644, d_tracer,
2969 NULL, &tracing_readme_fops);
2971 pr_warning("Could not create debugfs 'README' entry\n");
2973 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2974 NULL, &tracing_pipe_fops);
2976 pr_warning("Could not create debugfs "
2977 "'trace_pipe' entry\n");
2979 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
2980 &global_trace, &tracing_entries_fops);
2982 pr_warning("Could not create debugfs "
2983 "'buffer_size_kb' entry\n");
2985 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2986 NULL, &tracing_mark_fops);
2988 pr_warning("Could not create debugfs "
2989 "'trace_marker' entry\n");
2991 #ifdef CONFIG_DYNAMIC_FTRACE
2992 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2993 &ftrace_update_tot_cnt,
2994 &tracing_dyn_info_fops);
2996 pr_warning("Could not create debugfs "
2997 "'dyn_ftrace_total_info' entry\n");
2999 #ifdef CONFIG_SYSPROF_TRACER
3000 init_tracer_sysprof_debugfs(d_tracer);
3005 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
3007 static DEFINE_SPINLOCK(trace_buf_lock);
3008 static char trace_buf[TRACE_BUF_SIZE];
3010 struct ring_buffer_event *event;
3011 struct trace_array *tr = &global_trace;
3012 struct trace_array_cpu *data;
3013 int cpu, len = 0, size, pc;
3014 struct print_entry *entry;
3015 unsigned long irq_flags;
3017 if (tracing_disabled || tracing_selftest_running)
3020 pc = preempt_count();
3021 preempt_disable_notrace();
3022 cpu = raw_smp_processor_id();
3023 data = tr->data[cpu];
3025 if (unlikely(atomic_read(&data->disabled)))
3028 pause_graph_tracing();
3029 spin_lock_irqsave(&trace_buf_lock, irq_flags);
3030 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3032 len = min(len, TRACE_BUF_SIZE-1);
3035 size = sizeof(*entry) + len + 1;
3036 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
3039 entry = ring_buffer_event_data(event);
3041 entry->depth = depth;
3043 memcpy(&entry->buf, trace_buf, len);
3044 entry->buf[len] = 0;
3045 ring_buffer_unlock_commit(tr->buffer, event);
3048 spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
3049 unpause_graph_tracing();
3051 preempt_enable_notrace();
3055 EXPORT_SYMBOL_GPL(trace_vprintk);
3057 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3062 if (!(trace_flags & TRACE_ITER_PRINTK))
3066 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3070 EXPORT_SYMBOL_GPL(__ftrace_printk);
3072 int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
3074 if (!(trace_flags & TRACE_ITER_PRINTK))
3077 return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3079 EXPORT_SYMBOL_GPL(__ftrace_vprintk);
3081 static int trace_panic_handler(struct notifier_block *this,
3082 unsigned long event, void *unused)
3084 if (ftrace_dump_on_oops)
3089 static struct notifier_block trace_panic_notifier = {
3090 .notifier_call = trace_panic_handler,
3092 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3095 static int trace_die_handler(struct notifier_block *self,
3101 if (ftrace_dump_on_oops)
3110 static struct notifier_block trace_die_notifier = {
3111 .notifier_call = trace_die_handler,
3116 * printk is set to max of 1024, we really don't need it that big.
3117 * Nothing should be printing 1000 characters anyway.
3119 #define TRACE_MAX_PRINT 1000
3122 * Define here KERN_TRACE so that we have one place to modify
3123 * it if we decide to change what log level the ftrace dump
3126 #define KERN_TRACE KERN_EMERG
3129 trace_printk_seq(struct trace_seq *s)
3131 /* Probably should print a warning here. */
3135 /* should be zero ended, but we are paranoid. */
3136 s->buffer[s->len] = 0;
3138 printk(KERN_TRACE "%s", s->buffer);
3143 void ftrace_dump(void)
3145 static DEFINE_SPINLOCK(ftrace_dump_lock);
3146 /* use static because iter can be a bit big for the stack */
3147 static struct trace_iterator iter;
3148 static int dump_ran;
3149 unsigned long flags;
3153 spin_lock_irqsave(&ftrace_dump_lock, flags);
3159 /* No turning back! */
3163 for_each_tracing_cpu(cpu) {
3164 atomic_inc(&global_trace.data[cpu]->disabled);
3167 /* don't look at user memory in panic mode */
3168 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3170 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3172 iter.tr = &global_trace;
3173 iter.trace = current_trace;
3176 * We need to stop all tracing on all CPUS to read the
3177 * the next buffer. This is a bit expensive, but is
3178 * not done often. We fill all what we can read,
3179 * and then release the locks again.
3182 while (!trace_empty(&iter)) {
3185 printk(KERN_TRACE "---------------------------------\n");
3189 /* reset all but tr, trace, and overruns */
3190 memset(&iter.seq, 0,
3191 sizeof(struct trace_iterator) -
3192 offsetof(struct trace_iterator, seq));
3193 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3196 if (find_next_entry_inc(&iter) != NULL) {
3197 print_trace_line(&iter);
3198 trace_consume(&iter);
3201 trace_printk_seq(&iter.seq);
3205 printk(KERN_TRACE " (ftrace buffer empty)\n");
3207 printk(KERN_TRACE "---------------------------------\n");
3210 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3213 __init static int tracer_alloc_buffers(void)
3215 struct trace_array_cpu *data;
3219 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3222 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3223 goto out_free_buffer_mask;
3225 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3226 cpumask_copy(tracing_cpumask, cpu_all_mask);
3228 /* TODO: make the number of buffers hot pluggable with CPUS */
3229 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3230 TRACE_BUFFER_FLAGS);
3231 if (!global_trace.buffer) {
3232 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3234 goto out_free_cpumask;
3236 global_trace.entries = ring_buffer_size(global_trace.buffer);
3239 #ifdef CONFIG_TRACER_MAX_TRACE
3240 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3241 TRACE_BUFFER_FLAGS);
3242 if (!max_tr.buffer) {
3243 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3245 ring_buffer_free(global_trace.buffer);
3246 goto out_free_cpumask;
3248 max_tr.entries = ring_buffer_size(max_tr.buffer);
3249 WARN_ON(max_tr.entries != global_trace.entries);
3252 /* Allocate the first page for all buffers */
3253 for_each_tracing_cpu(i) {
3254 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3255 max_tr.data[i] = &per_cpu(max_data, i);
3258 trace_init_cmdlines();
3260 register_tracer(&nop_trace);
3261 current_trace = &nop_trace;
3262 #ifdef CONFIG_BOOT_TRACER
3263 register_tracer(&boot_tracer);
3265 /* All seems OK, enable tracing */
3266 tracing_disabled = 0;
3268 atomic_notifier_chain_register(&panic_notifier_list,
3269 &trace_panic_notifier);
3271 register_die_notifier(&trace_die_notifier);
3275 free_cpumask_var(tracing_cpumask);
3276 out_free_buffer_mask:
3277 free_cpumask_var(tracing_buffer_mask);
3282 __init static int clear_boot_tracer(void)
3285 * The default tracer at boot buffer is an init section.
3286 * This function is called in lateinit. If we did not
3287 * find the boot tracer, then clear it out, to prevent
3288 * later registration from accessing the buffer that is
3289 * about to be freed.
3291 if (!default_bootup_tracer)
3294 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
3295 default_bootup_tracer);
3296 default_bootup_tracer = NULL;
3301 early_initcall(tracer_alloc_buffers);
3302 fs_initcall(tracer_init_debugfs);
3303 late_initcall(clear_boot_tracer);