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(current);
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;
503 #ifdef CONFIG_FTRACE_STARTUP_TEST
504 if (type->selftest && !tracing_selftest_disabled) {
505 struct tracer *saved_tracer = current_trace;
506 struct trace_array *tr = &global_trace;
510 * Run a selftest on this tracer.
511 * Here we reset the trace buffer, and set the current
512 * tracer to be this tracer. The tracer can then run some
513 * internal tracing to verify that everything is in order.
514 * If we fail, we do not register this tracer.
516 for_each_tracing_cpu(i)
517 tracing_reset(tr, i);
519 current_trace = type;
520 /* the test is responsible for initializing and enabling */
521 pr_info("Testing tracer %s: ", type->name);
522 ret = type->selftest(type, tr);
523 /* the test is responsible for resetting too */
524 current_trace = saved_tracer;
526 printk(KERN_CONT "FAILED!\n");
529 /* Only reset on passing, to avoid touching corrupted buffers */
530 for_each_tracing_cpu(i)
531 tracing_reset(tr, i);
533 printk(KERN_CONT "PASSED\n");
537 type->next = trace_types;
539 len = strlen(type->name);
540 if (len > max_tracer_type_len)
541 max_tracer_type_len = len;
544 tracing_selftest_running = false;
545 mutex_unlock(&trace_types_lock);
547 if (ret || !default_bootup_tracer)
550 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
553 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
554 /* Do we want this tracer to start on bootup? */
555 tracing_set_tracer(type->name);
556 default_bootup_tracer = NULL;
557 /* disable other selftests, since this will break it. */
558 tracing_selftest_disabled = 1;
559 #ifdef CONFIG_FTRACE_STARTUP_TEST
560 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
569 void unregister_tracer(struct tracer *type)
574 mutex_lock(&trace_types_lock);
575 for (t = &trace_types; *t; t = &(*t)->next) {
579 pr_info("Trace %s not registered\n", type->name);
585 if (type == current_trace && tracer_enabled) {
588 if (current_trace->stop)
589 current_trace->stop(&global_trace);
590 current_trace = &nop_trace;
593 if (strlen(type->name) != max_tracer_type_len)
596 max_tracer_type_len = 0;
597 for (t = &trace_types; *t; t = &(*t)->next) {
598 len = strlen((*t)->name);
599 if (len > max_tracer_type_len)
600 max_tracer_type_len = len;
603 mutex_unlock(&trace_types_lock);
606 void tracing_reset(struct trace_array *tr, int cpu)
608 ftrace_disable_cpu();
609 ring_buffer_reset_cpu(tr->buffer, cpu);
613 void tracing_reset_online_cpus(struct trace_array *tr)
617 tr->time_start = ftrace_now(tr->cpu);
619 for_each_online_cpu(cpu)
620 tracing_reset(tr, cpu);
623 #define SAVED_CMDLINES 128
624 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
625 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
626 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
627 static int cmdline_idx;
628 static DEFINE_SPINLOCK(trace_cmdline_lock);
630 /* temporary disable recording */
631 static atomic_t trace_record_cmdline_disabled __read_mostly;
633 static void trace_init_cmdlines(void)
635 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
636 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
640 static int trace_stop_count;
641 static DEFINE_SPINLOCK(tracing_start_lock);
644 * ftrace_off_permanent - disable all ftrace code permanently
646 * This should only be called when a serious anomally has
647 * been detected. This will turn off the function tracing,
648 * ring buffers, and other tracing utilites. It takes no
649 * locks and can be called from any context.
651 void ftrace_off_permanent(void)
653 tracing_disabled = 1;
655 tracing_off_permanent();
659 * tracing_start - quick start of the tracer
661 * If tracing is enabled but was stopped by tracing_stop,
662 * this will start the tracer back up.
664 void tracing_start(void)
666 struct ring_buffer *buffer;
669 if (tracing_disabled)
672 spin_lock_irqsave(&tracing_start_lock, flags);
673 if (--trace_stop_count) {
674 if (trace_stop_count < 0) {
675 /* Someone screwed up their debugging */
677 trace_stop_count = 0;
683 buffer = global_trace.buffer;
685 ring_buffer_record_enable(buffer);
687 buffer = max_tr.buffer;
689 ring_buffer_record_enable(buffer);
693 spin_unlock_irqrestore(&tracing_start_lock, flags);
697 * tracing_stop - quick stop of the tracer
699 * Light weight way to stop tracing. Use in conjunction with
702 void tracing_stop(void)
704 struct ring_buffer *buffer;
708 spin_lock_irqsave(&tracing_start_lock, flags);
709 if (trace_stop_count++)
712 buffer = global_trace.buffer;
714 ring_buffer_record_disable(buffer);
716 buffer = max_tr.buffer;
718 ring_buffer_record_disable(buffer);
721 spin_unlock_irqrestore(&tracing_start_lock, flags);
724 void trace_stop_cmdline_recording(void);
726 static void trace_save_cmdline(struct task_struct *tsk)
731 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
735 * It's not the end of the world if we don't get
736 * the lock, but we also don't want to spin
737 * nor do we want to disable interrupts,
738 * so if we miss here, then better luck next time.
740 if (!spin_trylock(&trace_cmdline_lock))
743 idx = map_pid_to_cmdline[tsk->pid];
744 if (idx >= SAVED_CMDLINES) {
745 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
747 map = map_cmdline_to_pid[idx];
748 if (map <= PID_MAX_DEFAULT)
749 map_pid_to_cmdline[map] = (unsigned)-1;
751 map_pid_to_cmdline[tsk->pid] = idx;
756 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
758 spin_unlock(&trace_cmdline_lock);
761 char *trace_find_cmdline(int pid)
763 char *cmdline = "<...>";
769 if (pid > PID_MAX_DEFAULT)
772 map = map_pid_to_cmdline[pid];
773 if (map >= SAVED_CMDLINES)
776 cmdline = saved_cmdlines[map];
782 void tracing_record_cmdline(struct task_struct *tsk)
784 if (atomic_read(&trace_record_cmdline_disabled))
787 trace_save_cmdline(tsk);
791 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
794 struct task_struct *tsk = current;
796 entry->preempt_count = pc & 0xff;
797 entry->pid = (tsk) ? tsk->pid : 0;
798 entry->tgid = (tsk) ? tsk->tgid : 0;
800 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
801 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
803 TRACE_FLAG_IRQS_NOSUPPORT |
805 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
806 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
807 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
810 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
813 unsigned long flags, int pc)
815 struct ring_buffer_event *event;
817 event = ring_buffer_lock_reserve(tr->buffer, len);
819 struct trace_entry *ent = ring_buffer_event_data(event);
821 tracing_generic_entry_update(ent, flags, pc);
827 static void ftrace_trace_stack(struct trace_array *tr,
828 unsigned long flags, int skip, int pc);
829 static void ftrace_trace_userstack(struct trace_array *tr,
830 unsigned long flags, int pc);
832 void trace_buffer_unlock_commit(struct trace_array *tr,
833 struct ring_buffer_event *event,
834 unsigned long flags, int pc)
836 ring_buffer_unlock_commit(tr->buffer, event);
838 ftrace_trace_stack(tr, flags, 6, pc);
839 ftrace_trace_userstack(tr, flags, pc);
844 trace_function(struct trace_array *tr,
845 unsigned long ip, unsigned long parent_ip, unsigned long flags,
848 struct ring_buffer_event *event;
849 struct ftrace_entry *entry;
851 /* If we are reading the ring buffer, don't trace */
852 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
855 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
859 entry = ring_buffer_event_data(event);
861 entry->parent_ip = parent_ip;
862 ring_buffer_unlock_commit(tr->buffer, event);
865 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
866 static void __trace_graph_entry(struct trace_array *tr,
867 struct ftrace_graph_ent *trace,
871 struct ring_buffer_event *event;
872 struct ftrace_graph_ent_entry *entry;
874 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
877 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
878 sizeof(*entry), flags, pc);
881 entry = ring_buffer_event_data(event);
882 entry->graph_ent = *trace;
883 ring_buffer_unlock_commit(global_trace.buffer, event);
886 static void __trace_graph_return(struct trace_array *tr,
887 struct ftrace_graph_ret *trace,
891 struct ring_buffer_event *event;
892 struct ftrace_graph_ret_entry *entry;
894 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
897 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
898 sizeof(*entry), flags, pc);
901 entry = ring_buffer_event_data(event);
903 ring_buffer_unlock_commit(global_trace.buffer, event);
908 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
909 unsigned long ip, unsigned long parent_ip, unsigned long flags,
912 if (likely(!atomic_read(&data->disabled)))
913 trace_function(tr, ip, parent_ip, flags, pc);
916 static void __ftrace_trace_stack(struct trace_array *tr,
920 #ifdef CONFIG_STACKTRACE
921 struct ring_buffer_event *event;
922 struct stack_entry *entry;
923 struct stack_trace trace;
925 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
926 sizeof(*entry), flags, pc);
929 entry = ring_buffer_event_data(event);
930 memset(&entry->caller, 0, sizeof(entry->caller));
932 trace.nr_entries = 0;
933 trace.max_entries = FTRACE_STACK_ENTRIES;
935 trace.entries = entry->caller;
937 save_stack_trace(&trace);
938 ring_buffer_unlock_commit(tr->buffer, event);
942 static void ftrace_trace_stack(struct trace_array *tr,
946 if (!(trace_flags & TRACE_ITER_STACKTRACE))
949 __ftrace_trace_stack(tr, flags, skip, pc);
952 void __trace_stack(struct trace_array *tr,
956 __ftrace_trace_stack(tr, flags, skip, pc);
959 static void ftrace_trace_userstack(struct trace_array *tr,
960 unsigned long flags, int pc)
962 #ifdef CONFIG_STACKTRACE
963 struct ring_buffer_event *event;
964 struct userstack_entry *entry;
965 struct stack_trace trace;
967 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
970 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
971 sizeof(*entry), flags, pc);
974 entry = ring_buffer_event_data(event);
976 memset(&entry->caller, 0, sizeof(entry->caller));
978 trace.nr_entries = 0;
979 trace.max_entries = FTRACE_STACK_ENTRIES;
981 trace.entries = entry->caller;
983 save_stack_trace_user(&trace);
984 ring_buffer_unlock_commit(tr->buffer, event);
989 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
991 ftrace_trace_userstack(tr, flags, preempt_count());
996 ftrace_trace_special(void *__tr,
997 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1000 struct ring_buffer_event *event;
1001 struct trace_array *tr = __tr;
1002 struct special_entry *entry;
1004 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1005 sizeof(*entry), 0, pc);
1008 entry = ring_buffer_event_data(event);
1012 trace_buffer_unlock_commit(tr, event, 0, pc);
1016 __trace_special(void *__tr, void *__data,
1017 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1019 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1023 tracing_sched_switch_trace(struct trace_array *tr,
1024 struct task_struct *prev,
1025 struct task_struct *next,
1026 unsigned long flags, int pc)
1028 struct ring_buffer_event *event;
1029 struct ctx_switch_entry *entry;
1031 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1032 sizeof(*entry), flags, pc);
1035 entry = ring_buffer_event_data(event);
1036 entry->prev_pid = prev->pid;
1037 entry->prev_prio = prev->prio;
1038 entry->prev_state = prev->state;
1039 entry->next_pid = next->pid;
1040 entry->next_prio = next->prio;
1041 entry->next_state = next->state;
1042 entry->next_cpu = task_cpu(next);
1043 trace_buffer_unlock_commit(tr, event, flags, pc);
1047 tracing_sched_wakeup_trace(struct trace_array *tr,
1048 struct task_struct *wakee,
1049 struct task_struct *curr,
1050 unsigned long flags, int pc)
1052 struct ring_buffer_event *event;
1053 struct ctx_switch_entry *entry;
1055 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1056 sizeof(*entry), flags, pc);
1059 entry = ring_buffer_event_data(event);
1060 entry->prev_pid = curr->pid;
1061 entry->prev_prio = curr->prio;
1062 entry->prev_state = curr->state;
1063 entry->next_pid = wakee->pid;
1064 entry->next_prio = wakee->prio;
1065 entry->next_state = wakee->state;
1066 entry->next_cpu = task_cpu(wakee);
1067 trace_buffer_unlock_commit(tr, event, flags, pc);
1071 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1073 struct trace_array *tr = &global_trace;
1074 struct trace_array_cpu *data;
1075 unsigned long flags;
1079 if (tracing_disabled)
1082 pc = preempt_count();
1083 local_irq_save(flags);
1084 cpu = raw_smp_processor_id();
1085 data = tr->data[cpu];
1087 if (likely(atomic_inc_return(&data->disabled) == 1))
1088 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1090 atomic_dec(&data->disabled);
1091 local_irq_restore(flags);
1094 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1095 int trace_graph_entry(struct ftrace_graph_ent *trace)
1097 struct trace_array *tr = &global_trace;
1098 struct trace_array_cpu *data;
1099 unsigned long flags;
1104 if (!ftrace_trace_task(current))
1107 if (!ftrace_graph_addr(trace->func))
1110 local_irq_save(flags);
1111 cpu = raw_smp_processor_id();
1112 data = tr->data[cpu];
1113 disabled = atomic_inc_return(&data->disabled);
1114 if (likely(disabled == 1)) {
1115 pc = preempt_count();
1116 __trace_graph_entry(tr, trace, flags, pc);
1118 /* Only do the atomic if it is not already set */
1119 if (!test_tsk_trace_graph(current))
1120 set_tsk_trace_graph(current);
1121 atomic_dec(&data->disabled);
1122 local_irq_restore(flags);
1127 void trace_graph_return(struct ftrace_graph_ret *trace)
1129 struct trace_array *tr = &global_trace;
1130 struct trace_array_cpu *data;
1131 unsigned long flags;
1136 local_irq_save(flags);
1137 cpu = raw_smp_processor_id();
1138 data = tr->data[cpu];
1139 disabled = atomic_inc_return(&data->disabled);
1140 if (likely(disabled == 1)) {
1141 pc = preempt_count();
1142 __trace_graph_return(tr, trace, flags, pc);
1145 clear_tsk_trace_graph(current);
1146 atomic_dec(&data->disabled);
1147 local_irq_restore(flags);
1149 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1151 enum trace_file_type {
1152 TRACE_FILE_LAT_FMT = 1,
1153 TRACE_FILE_ANNOTATE = 2,
1156 static void trace_iterator_increment(struct trace_iterator *iter)
1158 /* Don't allow ftrace to trace into the ring buffers */
1159 ftrace_disable_cpu();
1162 if (iter->buffer_iter[iter->cpu])
1163 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1165 ftrace_enable_cpu();
1168 static struct trace_entry *
1169 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1171 struct ring_buffer_event *event;
1172 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1174 /* Don't allow ftrace to trace into the ring buffers */
1175 ftrace_disable_cpu();
1178 event = ring_buffer_iter_peek(buf_iter, ts);
1180 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1182 ftrace_enable_cpu();
1184 return event ? ring_buffer_event_data(event) : NULL;
1187 static struct trace_entry *
1188 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1190 struct ring_buffer *buffer = iter->tr->buffer;
1191 struct trace_entry *ent, *next = NULL;
1192 u64 next_ts = 0, ts;
1196 for_each_tracing_cpu(cpu) {
1198 if (ring_buffer_empty_cpu(buffer, cpu))
1201 ent = peek_next_entry(iter, cpu, &ts);
1204 * Pick the entry with the smallest timestamp:
1206 if (ent && (!next || ts < next_ts)) {
1214 *ent_cpu = next_cpu;
1222 /* Find the next real entry, without updating the iterator itself */
1223 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1224 int *ent_cpu, u64 *ent_ts)
1226 return __find_next_entry(iter, ent_cpu, ent_ts);
1229 /* Find the next real entry, and increment the iterator to the next entry */
1230 static void *find_next_entry_inc(struct trace_iterator *iter)
1232 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1235 trace_iterator_increment(iter);
1237 return iter->ent ? iter : NULL;
1240 static void trace_consume(struct trace_iterator *iter)
1242 /* Don't allow ftrace to trace into the ring buffers */
1243 ftrace_disable_cpu();
1244 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1245 ftrace_enable_cpu();
1248 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1250 struct trace_iterator *iter = m->private;
1256 /* can't go backwards */
1261 ent = find_next_entry_inc(iter);
1265 while (ent && iter->idx < i)
1266 ent = find_next_entry_inc(iter);
1273 static void *s_start(struct seq_file *m, loff_t *pos)
1275 struct trace_iterator *iter = m->private;
1280 mutex_lock(&trace_types_lock);
1282 if (!current_trace || current_trace != iter->trace) {
1283 mutex_unlock(&trace_types_lock);
1287 atomic_inc(&trace_record_cmdline_disabled);
1289 if (*pos != iter->pos) {
1294 ftrace_disable_cpu();
1296 for_each_tracing_cpu(cpu) {
1297 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1300 ftrace_enable_cpu();
1302 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1307 p = s_next(m, p, &l);
1313 static void s_stop(struct seq_file *m, void *p)
1315 atomic_dec(&trace_record_cmdline_disabled);
1316 mutex_unlock(&trace_types_lock);
1319 static void print_lat_help_header(struct seq_file *m)
1321 seq_puts(m, "# _------=> CPU# \n");
1322 seq_puts(m, "# / _-----=> irqs-off \n");
1323 seq_puts(m, "# | / _----=> need-resched \n");
1324 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1325 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1326 seq_puts(m, "# |||| / \n");
1327 seq_puts(m, "# ||||| delay \n");
1328 seq_puts(m, "# cmd pid ||||| time | caller \n");
1329 seq_puts(m, "# \\ / ||||| \\ | / \n");
1332 static void print_func_help_header(struct seq_file *m)
1334 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1335 seq_puts(m, "# | | | | |\n");
1340 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1342 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1343 struct trace_array *tr = iter->tr;
1344 struct trace_array_cpu *data = tr->data[tr->cpu];
1345 struct tracer *type = current_trace;
1346 unsigned long total;
1347 unsigned long entries;
1348 const char *name = "preemption";
1353 entries = ring_buffer_entries(iter->tr->buffer);
1355 ring_buffer_overruns(iter->tr->buffer);
1357 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1359 seq_puts(m, "-----------------------------------"
1360 "---------------------------------\n");
1361 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1362 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1363 nsecs_to_usecs(data->saved_latency),
1367 #if defined(CONFIG_PREEMPT_NONE)
1369 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1371 #elif defined(CONFIG_PREEMPT)
1376 /* These are reserved for later use */
1379 seq_printf(m, " #P:%d)\n", num_online_cpus());
1383 seq_puts(m, " -----------------\n");
1384 seq_printf(m, " | task: %.16s-%d "
1385 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1386 data->comm, data->pid, data->uid, data->nice,
1387 data->policy, data->rt_priority);
1388 seq_puts(m, " -----------------\n");
1390 if (data->critical_start) {
1391 seq_puts(m, " => started at: ");
1392 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1393 trace_print_seq(m, &iter->seq);
1394 seq_puts(m, "\n => ended at: ");
1395 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1396 trace_print_seq(m, &iter->seq);
1403 static void test_cpu_buff_start(struct trace_iterator *iter)
1405 struct trace_seq *s = &iter->seq;
1407 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1410 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1413 if (cpumask_test_cpu(iter->cpu, iter->started))
1416 cpumask_set_cpu(iter->cpu, iter->started);
1417 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1420 static enum print_line_t print_lat_fmt(struct trace_iterator *iter)
1422 struct trace_seq *s = &iter->seq;
1423 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1424 struct trace_event *event;
1425 struct trace_entry *entry = iter->ent;
1427 test_cpu_buff_start(iter);
1429 event = ftrace_find_event(entry->type);
1431 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1432 if (!trace_print_lat_context(iter))
1437 return event->latency_trace(iter, sym_flags);
1439 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1442 return TRACE_TYPE_HANDLED;
1444 return TRACE_TYPE_PARTIAL_LINE;
1447 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1449 struct trace_seq *s = &iter->seq;
1450 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1451 struct trace_entry *entry;
1452 struct trace_event *event;
1456 test_cpu_buff_start(iter);
1458 event = ftrace_find_event(entry->type);
1460 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1461 if (!trace_print_context(iter))
1466 return event->trace(iter, sym_flags);
1468 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1471 return TRACE_TYPE_HANDLED;
1473 return TRACE_TYPE_PARTIAL_LINE;
1476 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1478 struct trace_seq *s = &iter->seq;
1479 struct trace_entry *entry;
1480 struct trace_event *event;
1484 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1485 if (!trace_seq_printf(s, "%d %d %llu ",
1486 entry->pid, iter->cpu, iter->ts))
1490 event = ftrace_find_event(entry->type);
1492 return event->raw(iter, 0);
1494 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1497 return TRACE_TYPE_HANDLED;
1499 return TRACE_TYPE_PARTIAL_LINE;
1502 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1504 struct trace_seq *s = &iter->seq;
1505 unsigned char newline = '\n';
1506 struct trace_entry *entry;
1507 struct trace_event *event;
1511 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1512 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1513 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1514 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1517 event = ftrace_find_event(entry->type);
1519 enum print_line_t ret = event->hex(iter, 0);
1520 if (ret != TRACE_TYPE_HANDLED)
1524 SEQ_PUT_FIELD_RET(s, newline);
1526 return TRACE_TYPE_HANDLED;
1529 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1531 struct trace_seq *s = &iter->seq;
1532 struct trace_entry *entry = iter->ent;
1533 struct print_entry *field;
1536 trace_assign_type(field, entry);
1538 ret = trace_seq_printf(s, "%s", field->buf);
1540 return TRACE_TYPE_PARTIAL_LINE;
1542 return TRACE_TYPE_HANDLED;
1545 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1547 struct trace_seq *s = &iter->seq;
1548 struct trace_entry *entry;
1549 struct trace_event *event;
1553 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1554 SEQ_PUT_FIELD_RET(s, entry->pid);
1555 SEQ_PUT_FIELD_RET(s, iter->cpu);
1556 SEQ_PUT_FIELD_RET(s, iter->ts);
1559 event = ftrace_find_event(entry->type);
1560 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1563 static int trace_empty(struct trace_iterator *iter)
1567 for_each_tracing_cpu(cpu) {
1568 if (iter->buffer_iter[cpu]) {
1569 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1572 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1580 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1582 enum print_line_t ret;
1584 if (iter->trace && iter->trace->print_line) {
1585 ret = iter->trace->print_line(iter);
1586 if (ret != TRACE_TYPE_UNHANDLED)
1590 if (iter->ent->type == TRACE_PRINT &&
1591 trace_flags & TRACE_ITER_PRINTK &&
1592 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1593 return print_printk_msg_only(iter);
1595 if (trace_flags & TRACE_ITER_BIN)
1596 return print_bin_fmt(iter);
1598 if (trace_flags & TRACE_ITER_HEX)
1599 return print_hex_fmt(iter);
1601 if (trace_flags & TRACE_ITER_RAW)
1602 return print_raw_fmt(iter);
1604 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1605 return print_lat_fmt(iter);
1607 return print_trace_fmt(iter);
1610 static int s_show(struct seq_file *m, void *v)
1612 struct trace_iterator *iter = v;
1614 if (iter->ent == NULL) {
1616 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1619 if (iter->trace && iter->trace->print_header)
1620 iter->trace->print_header(m);
1621 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1622 /* print nothing if the buffers are empty */
1623 if (trace_empty(iter))
1625 print_trace_header(m, iter);
1626 if (!(trace_flags & TRACE_ITER_VERBOSE))
1627 print_lat_help_header(m);
1629 if (!(trace_flags & TRACE_ITER_VERBOSE))
1630 print_func_help_header(m);
1633 print_trace_line(iter);
1634 trace_print_seq(m, &iter->seq);
1640 static struct seq_operations tracer_seq_ops = {
1647 static struct trace_iterator *
1648 __tracing_open(struct inode *inode, struct file *file, int *ret)
1650 struct trace_iterator *iter;
1654 if (tracing_disabled) {
1659 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1665 mutex_lock(&trace_types_lock);
1666 if (current_trace && current_trace->print_max)
1669 iter->tr = inode->i_private;
1670 iter->trace = current_trace;
1673 /* Notify the tracer early; before we stop tracing. */
1674 if (iter->trace && iter->trace->open)
1675 iter->trace->open(iter);
1677 /* Annotate start of buffers if we had overruns */
1678 if (ring_buffer_overruns(iter->tr->buffer))
1679 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1682 for_each_tracing_cpu(cpu) {
1684 iter->buffer_iter[cpu] =
1685 ring_buffer_read_start(iter->tr->buffer, cpu);
1687 if (!iter->buffer_iter[cpu])
1691 /* TODO stop tracer */
1692 *ret = seq_open(file, &tracer_seq_ops);
1696 m = file->private_data;
1699 /* stop the trace while dumping */
1702 mutex_unlock(&trace_types_lock);
1708 for_each_tracing_cpu(cpu) {
1709 if (iter->buffer_iter[cpu])
1710 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1712 mutex_unlock(&trace_types_lock);
1715 return ERR_PTR(-ENOMEM);
1718 int tracing_open_generic(struct inode *inode, struct file *filp)
1720 if (tracing_disabled)
1723 filp->private_data = inode->i_private;
1727 static int tracing_release(struct inode *inode, struct file *file)
1729 struct seq_file *m = (struct seq_file *)file->private_data;
1730 struct trace_iterator *iter = m->private;
1733 mutex_lock(&trace_types_lock);
1734 for_each_tracing_cpu(cpu) {
1735 if (iter->buffer_iter[cpu])
1736 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1739 if (iter->trace && iter->trace->close)
1740 iter->trace->close(iter);
1742 /* reenable tracing if it was previously enabled */
1744 mutex_unlock(&trace_types_lock);
1746 seq_release(inode, file);
1751 static int tracing_open(struct inode *inode, struct file *file)
1755 __tracing_open(inode, file, &ret);
1760 static int tracing_lt_open(struct inode *inode, struct file *file)
1762 struct trace_iterator *iter;
1765 iter = __tracing_open(inode, file, &ret);
1768 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1775 t_next(struct seq_file *m, void *v, loff_t *pos)
1777 struct tracer *t = m->private;
1789 static void *t_start(struct seq_file *m, loff_t *pos)
1791 struct tracer *t = m->private;
1794 mutex_lock(&trace_types_lock);
1795 for (; t && l < *pos; t = t_next(m, t, &l))
1801 static void t_stop(struct seq_file *m, void *p)
1803 mutex_unlock(&trace_types_lock);
1806 static int t_show(struct seq_file *m, void *v)
1808 struct tracer *t = v;
1813 seq_printf(m, "%s", t->name);
1822 static struct seq_operations show_traces_seq_ops = {
1829 static int show_traces_open(struct inode *inode, struct file *file)
1833 if (tracing_disabled)
1836 ret = seq_open(file, &show_traces_seq_ops);
1838 struct seq_file *m = file->private_data;
1839 m->private = trace_types;
1845 static struct file_operations tracing_fops = {
1846 .open = tracing_open,
1848 .llseek = seq_lseek,
1849 .release = tracing_release,
1852 static struct file_operations tracing_lt_fops = {
1853 .open = tracing_lt_open,
1855 .llseek = seq_lseek,
1856 .release = tracing_release,
1859 static struct file_operations show_traces_fops = {
1860 .open = show_traces_open,
1862 .release = seq_release,
1866 * Only trace on a CPU if the bitmask is set:
1868 static cpumask_var_t tracing_cpumask;
1871 * The tracer itself will not take this lock, but still we want
1872 * to provide a consistent cpumask to user-space:
1874 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1877 * Temporary storage for the character representation of the
1878 * CPU bitmask (and one more byte for the newline):
1880 static char mask_str[NR_CPUS + 1];
1883 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1884 size_t count, loff_t *ppos)
1888 mutex_lock(&tracing_cpumask_update_lock);
1890 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1891 if (count - len < 2) {
1895 len += sprintf(mask_str + len, "\n");
1896 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1899 mutex_unlock(&tracing_cpumask_update_lock);
1905 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1906 size_t count, loff_t *ppos)
1909 cpumask_var_t tracing_cpumask_new;
1911 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1914 mutex_lock(&tracing_cpumask_update_lock);
1915 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1919 local_irq_disable();
1920 __raw_spin_lock(&ftrace_max_lock);
1921 for_each_tracing_cpu(cpu) {
1923 * Increase/decrease the disabled counter if we are
1924 * about to flip a bit in the cpumask:
1926 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
1927 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
1928 atomic_inc(&global_trace.data[cpu]->disabled);
1930 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
1931 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
1932 atomic_dec(&global_trace.data[cpu]->disabled);
1935 __raw_spin_unlock(&ftrace_max_lock);
1938 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
1940 mutex_unlock(&tracing_cpumask_update_lock);
1941 free_cpumask_var(tracing_cpumask_new);
1946 mutex_unlock(&tracing_cpumask_update_lock);
1947 free_cpumask_var(tracing_cpumask);
1952 static struct file_operations tracing_cpumask_fops = {
1953 .open = tracing_open_generic,
1954 .read = tracing_cpumask_read,
1955 .write = tracing_cpumask_write,
1959 tracing_trace_options_read(struct file *filp, char __user *ubuf,
1960 size_t cnt, loff_t *ppos)
1966 u32 tracer_flags = current_trace->flags->val;
1967 struct tracer_opt *trace_opts = current_trace->flags->opts;
1970 /* calculate max size */
1971 for (i = 0; trace_options[i]; i++) {
1972 len += strlen(trace_options[i]);
1973 len += 3; /* "no" and space */
1977 * Increase the size with names of options specific
1978 * of the current tracer.
1980 for (i = 0; trace_opts[i].name; i++) {
1981 len += strlen(trace_opts[i].name);
1982 len += 3; /* "no" and space */
1985 /* +2 for \n and \0 */
1986 buf = kmalloc(len + 2, GFP_KERNEL);
1990 for (i = 0; trace_options[i]; i++) {
1991 if (trace_flags & (1 << i))
1992 r += sprintf(buf + r, "%s ", trace_options[i]);
1994 r += sprintf(buf + r, "no%s ", trace_options[i]);
1997 for (i = 0; trace_opts[i].name; i++) {
1998 if (tracer_flags & trace_opts[i].bit)
1999 r += sprintf(buf + r, "%s ",
2000 trace_opts[i].name);
2002 r += sprintf(buf + r, "no%s ",
2003 trace_opts[i].name);
2006 r += sprintf(buf + r, "\n");
2007 WARN_ON(r >= len + 2);
2009 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2016 /* Try to assign a tracer specific option */
2017 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2019 struct tracer_flags *trace_flags = trace->flags;
2020 struct tracer_opt *opts = NULL;
2024 for (i = 0; trace_flags->opts[i].name; i++) {
2025 opts = &trace_flags->opts[i];
2026 len = strlen(opts->name);
2028 if (strncmp(cmp, opts->name, len) == 0) {
2029 ret = trace->set_flag(trace_flags->val,
2035 if (!trace_flags->opts[i].name)
2038 /* Refused to handle */
2043 trace_flags->val &= ~opts->bit;
2045 trace_flags->val |= opts->bit;
2051 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2052 size_t cnt, loff_t *ppos)
2060 if (cnt >= sizeof(buf))
2063 if (copy_from_user(&buf, ubuf, cnt))
2068 if (strncmp(buf, "no", 2) == 0) {
2073 for (i = 0; trace_options[i]; i++) {
2074 int len = strlen(trace_options[i]);
2076 if (strncmp(cmp, trace_options[i], len) == 0) {
2078 trace_flags &= ~(1 << i);
2080 trace_flags |= (1 << i);
2085 /* If no option could be set, test the specific tracer options */
2086 if (!trace_options[i]) {
2087 ret = set_tracer_option(current_trace, cmp, neg);
2097 static struct file_operations tracing_iter_fops = {
2098 .open = tracing_open_generic,
2099 .read = tracing_trace_options_read,
2100 .write = tracing_trace_options_write,
2103 static const char readme_msg[] =
2104 "tracing mini-HOWTO:\n\n"
2106 "# mount -t debugfs nodev /debug\n\n"
2107 "# cat /debug/tracing/available_tracers\n"
2108 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2109 "# cat /debug/tracing/current_tracer\n"
2111 "# echo sched_switch > /debug/tracing/current_tracer\n"
2112 "# cat /debug/tracing/current_tracer\n"
2114 "# cat /debug/tracing/trace_options\n"
2115 "noprint-parent nosym-offset nosym-addr noverbose\n"
2116 "# echo print-parent > /debug/tracing/trace_options\n"
2117 "# echo 1 > /debug/tracing/tracing_enabled\n"
2118 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2119 "echo 0 > /debug/tracing/tracing_enabled\n"
2123 tracing_readme_read(struct file *filp, char __user *ubuf,
2124 size_t cnt, loff_t *ppos)
2126 return simple_read_from_buffer(ubuf, cnt, ppos,
2127 readme_msg, strlen(readme_msg));
2130 static struct file_operations tracing_readme_fops = {
2131 .open = tracing_open_generic,
2132 .read = tracing_readme_read,
2136 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2137 size_t cnt, loff_t *ppos)
2142 r = sprintf(buf, "%u\n", tracer_enabled);
2143 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2147 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2148 size_t cnt, loff_t *ppos)
2150 struct trace_array *tr = filp->private_data;
2155 if (cnt >= sizeof(buf))
2158 if (copy_from_user(&buf, ubuf, cnt))
2163 ret = strict_strtoul(buf, 10, &val);
2169 mutex_lock(&trace_types_lock);
2170 if (tracer_enabled ^ val) {
2173 if (current_trace->start)
2174 current_trace->start(tr);
2179 if (current_trace->stop)
2180 current_trace->stop(tr);
2183 mutex_unlock(&trace_types_lock);
2191 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2192 size_t cnt, loff_t *ppos)
2194 char buf[max_tracer_type_len+2];
2197 mutex_lock(&trace_types_lock);
2199 r = sprintf(buf, "%s\n", current_trace->name);
2201 r = sprintf(buf, "\n");
2202 mutex_unlock(&trace_types_lock);
2204 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2207 int tracer_init(struct tracer *t, struct trace_array *tr)
2209 tracing_reset_online_cpus(tr);
2213 static int tracing_set_tracer(const char *buf)
2215 struct trace_array *tr = &global_trace;
2219 mutex_lock(&trace_types_lock);
2220 for (t = trace_types; t; t = t->next) {
2221 if (strcmp(t->name, buf) == 0)
2228 if (t == current_trace)
2231 trace_branch_disable();
2232 if (current_trace && current_trace->reset)
2233 current_trace->reset(tr);
2237 ret = tracer_init(t, tr);
2242 trace_branch_enable(tr);
2244 mutex_unlock(&trace_types_lock);
2250 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2251 size_t cnt, loff_t *ppos)
2253 char buf[max_tracer_type_len+1];
2260 if (cnt > max_tracer_type_len)
2261 cnt = max_tracer_type_len;
2263 if (copy_from_user(&buf, ubuf, cnt))
2268 /* strip ending whitespace. */
2269 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2272 err = tracing_set_tracer(buf);
2282 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2283 size_t cnt, loff_t *ppos)
2285 unsigned long *ptr = filp->private_data;
2289 r = snprintf(buf, sizeof(buf), "%ld\n",
2290 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2291 if (r > sizeof(buf))
2293 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2297 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2298 size_t cnt, loff_t *ppos)
2300 unsigned long *ptr = filp->private_data;
2305 if (cnt >= sizeof(buf))
2308 if (copy_from_user(&buf, ubuf, cnt))
2313 ret = strict_strtoul(buf, 10, &val);
2322 static atomic_t tracing_reader;
2324 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2326 struct trace_iterator *iter;
2328 if (tracing_disabled)
2331 /* We only allow for reader of the pipe */
2332 if (atomic_inc_return(&tracing_reader) != 1) {
2333 atomic_dec(&tracing_reader);
2337 /* create a buffer to store the information to pass to userspace */
2338 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2342 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2347 mutex_lock(&trace_types_lock);
2349 /* trace pipe does not show start of buffer */
2350 cpumask_setall(iter->started);
2352 iter->tr = &global_trace;
2353 iter->trace = current_trace;
2354 filp->private_data = iter;
2356 if (iter->trace->pipe_open)
2357 iter->trace->pipe_open(iter);
2358 mutex_unlock(&trace_types_lock);
2363 static int tracing_release_pipe(struct inode *inode, struct file *file)
2365 struct trace_iterator *iter = file->private_data;
2367 free_cpumask_var(iter->started);
2369 atomic_dec(&tracing_reader);
2375 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2377 struct trace_iterator *iter = filp->private_data;
2379 if (trace_flags & TRACE_ITER_BLOCK) {
2381 * Always select as readable when in blocking mode
2383 return POLLIN | POLLRDNORM;
2385 if (!trace_empty(iter))
2386 return POLLIN | POLLRDNORM;
2387 poll_wait(filp, &trace_wait, poll_table);
2388 if (!trace_empty(iter))
2389 return POLLIN | POLLRDNORM;
2395 /* Must be called with trace_types_lock mutex held. */
2396 static int tracing_wait_pipe(struct file *filp)
2398 struct trace_iterator *iter = filp->private_data;
2400 while (trace_empty(iter)) {
2402 if ((filp->f_flags & O_NONBLOCK)) {
2407 * This is a make-shift waitqueue. The reason we don't use
2408 * an actual wait queue is because:
2409 * 1) we only ever have one waiter
2410 * 2) the tracing, traces all functions, we don't want
2411 * the overhead of calling wake_up and friends
2412 * (and tracing them too)
2413 * Anyway, this is really very primitive wakeup.
2415 set_current_state(TASK_INTERRUPTIBLE);
2416 iter->tr->waiter = current;
2418 mutex_unlock(&trace_types_lock);
2420 /* sleep for 100 msecs, and try again. */
2421 schedule_timeout(HZ/10);
2423 mutex_lock(&trace_types_lock);
2425 iter->tr->waiter = NULL;
2427 if (signal_pending(current)) {
2431 if (iter->trace != current_trace)
2435 * We block until we read something and tracing is disabled.
2436 * We still block if tracing is disabled, but we have never
2437 * read anything. This allows a user to cat this file, and
2438 * then enable tracing. But after we have read something,
2439 * we give an EOF when tracing is again disabled.
2441 * iter->pos will be 0 if we haven't read anything.
2443 if (!tracer_enabled && iter->pos)
2456 tracing_read_pipe(struct file *filp, char __user *ubuf,
2457 size_t cnt, loff_t *ppos)
2459 struct trace_iterator *iter = filp->private_data;
2462 /* return any leftover data */
2463 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2467 trace_seq_reset(&iter->seq);
2469 mutex_lock(&trace_types_lock);
2470 if (iter->trace->read) {
2471 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2477 sret = tracing_wait_pipe(filp);
2481 /* stop when tracing is finished */
2482 if (trace_empty(iter)) {
2487 if (cnt >= PAGE_SIZE)
2488 cnt = PAGE_SIZE - 1;
2490 /* reset all but tr, trace, and overruns */
2491 memset(&iter->seq, 0,
2492 sizeof(struct trace_iterator) -
2493 offsetof(struct trace_iterator, seq));
2496 while (find_next_entry_inc(iter) != NULL) {
2497 enum print_line_t ret;
2498 int len = iter->seq.len;
2500 ret = print_trace_line(iter);
2501 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2502 /* don't print partial lines */
2503 iter->seq.len = len;
2506 if (ret != TRACE_TYPE_NO_CONSUME)
2507 trace_consume(iter);
2509 if (iter->seq.len >= cnt)
2513 /* Now copy what we have to the user */
2514 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2515 if (iter->seq.readpos >= iter->seq.len)
2516 trace_seq_reset(&iter->seq);
2519 * If there was nothing to send to user, inspite of consuming trace
2520 * entries, go back to wait for more entries.
2526 mutex_unlock(&trace_types_lock);
2531 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2532 struct pipe_buffer *buf)
2534 __free_page(buf->page);
2537 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2540 __free_page(spd->pages[idx]);
2543 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2545 .map = generic_pipe_buf_map,
2546 .unmap = generic_pipe_buf_unmap,
2547 .confirm = generic_pipe_buf_confirm,
2548 .release = tracing_pipe_buf_release,
2549 .steal = generic_pipe_buf_steal,
2550 .get = generic_pipe_buf_get,
2554 tracing_fill_pipe_page(struct page *pages, size_t rem,
2555 struct trace_iterator *iter)
2560 /* Seq buffer is page-sized, exactly what we need. */
2562 count = iter->seq.len;
2563 ret = print_trace_line(iter);
2564 count = iter->seq.len - count;
2567 iter->seq.len -= count;
2570 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2571 iter->seq.len -= count;
2575 trace_consume(iter);
2577 if (!find_next_entry_inc(iter)) {
2587 static ssize_t tracing_splice_read_pipe(struct file *filp,
2589 struct pipe_inode_info *pipe,
2593 struct page *pages[PIPE_BUFFERS];
2594 struct partial_page partial[PIPE_BUFFERS];
2595 struct trace_iterator *iter = filp->private_data;
2596 struct splice_pipe_desc spd = {
2599 .nr_pages = 0, /* This gets updated below. */
2601 .ops = &tracing_pipe_buf_ops,
2602 .spd_release = tracing_spd_release_pipe,
2608 mutex_lock(&trace_types_lock);
2610 if (iter->trace->splice_read) {
2611 ret = iter->trace->splice_read(iter, filp,
2612 ppos, pipe, len, flags);
2617 ret = tracing_wait_pipe(filp);
2621 if (!iter->ent && !find_next_entry_inc(iter)) {
2626 /* Fill as many pages as possible. */
2627 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2628 pages[i] = alloc_page(GFP_KERNEL);
2632 rem = tracing_fill_pipe_page(pages[i], rem, iter);
2634 /* Copy the data into the page, so we can start over. */
2635 ret = trace_seq_to_buffer(&iter->seq,
2636 page_address(pages[i]),
2639 __free_page(pages[i]);
2642 partial[i].offset = 0;
2643 partial[i].len = iter->seq.len;
2645 trace_seq_reset(&iter->seq);
2648 mutex_unlock(&trace_types_lock);
2652 return splice_to_pipe(pipe, &spd);
2655 mutex_unlock(&trace_types_lock);
2661 tracing_entries_read(struct file *filp, char __user *ubuf,
2662 size_t cnt, loff_t *ppos)
2664 struct trace_array *tr = filp->private_data;
2668 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2669 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2673 tracing_entries_write(struct file *filp, const char __user *ubuf,
2674 size_t cnt, loff_t *ppos)
2680 if (cnt >= sizeof(buf))
2683 if (copy_from_user(&buf, ubuf, cnt))
2688 ret = strict_strtoul(buf, 10, &val);
2692 /* must have at least 1 entry */
2696 mutex_lock(&trace_types_lock);
2700 /* disable all cpu buffers */
2701 for_each_tracing_cpu(cpu) {
2702 if (global_trace.data[cpu])
2703 atomic_inc(&global_trace.data[cpu]->disabled);
2704 if (max_tr.data[cpu])
2705 atomic_inc(&max_tr.data[cpu]->disabled);
2708 /* value is in KB */
2711 if (val != global_trace.entries) {
2712 ret = ring_buffer_resize(global_trace.buffer, val);
2718 ret = ring_buffer_resize(max_tr.buffer, val);
2722 r = ring_buffer_resize(global_trace.buffer,
2723 global_trace.entries);
2725 /* AARGH! We are left with different
2726 * size max buffer!!!! */
2728 tracing_disabled = 1;
2733 global_trace.entries = val;
2738 /* If check pages failed, return ENOMEM */
2739 if (tracing_disabled)
2742 for_each_tracing_cpu(cpu) {
2743 if (global_trace.data[cpu])
2744 atomic_dec(&global_trace.data[cpu]->disabled);
2745 if (max_tr.data[cpu])
2746 atomic_dec(&max_tr.data[cpu]->disabled);
2750 max_tr.entries = global_trace.entries;
2751 mutex_unlock(&trace_types_lock);
2756 static int mark_printk(const char *fmt, ...)
2760 va_start(args, fmt);
2761 ret = trace_vprintk(0, -1, fmt, args);
2767 tracing_mark_write(struct file *filp, const char __user *ubuf,
2768 size_t cnt, loff_t *fpos)
2773 if (tracing_disabled)
2776 if (cnt > TRACE_BUF_SIZE)
2777 cnt = TRACE_BUF_SIZE;
2779 buf = kmalloc(cnt + 1, GFP_KERNEL);
2783 if (copy_from_user(buf, ubuf, cnt)) {
2788 /* Cut from the first nil or newline. */
2790 end = strchr(buf, '\n');
2794 cnt = mark_printk("%s\n", buf);
2801 static struct file_operations tracing_max_lat_fops = {
2802 .open = tracing_open_generic,
2803 .read = tracing_max_lat_read,
2804 .write = tracing_max_lat_write,
2807 static struct file_operations tracing_ctrl_fops = {
2808 .open = tracing_open_generic,
2809 .read = tracing_ctrl_read,
2810 .write = tracing_ctrl_write,
2813 static struct file_operations set_tracer_fops = {
2814 .open = tracing_open_generic,
2815 .read = tracing_set_trace_read,
2816 .write = tracing_set_trace_write,
2819 static struct file_operations tracing_pipe_fops = {
2820 .open = tracing_open_pipe,
2821 .poll = tracing_poll_pipe,
2822 .read = tracing_read_pipe,
2823 .splice_read = tracing_splice_read_pipe,
2824 .release = tracing_release_pipe,
2827 static struct file_operations tracing_entries_fops = {
2828 .open = tracing_open_generic,
2829 .read = tracing_entries_read,
2830 .write = tracing_entries_write,
2833 static struct file_operations tracing_mark_fops = {
2834 .open = tracing_open_generic,
2835 .write = tracing_mark_write,
2838 #ifdef CONFIG_DYNAMIC_FTRACE
2840 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
2846 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
2847 size_t cnt, loff_t *ppos)
2849 static char ftrace_dyn_info_buffer[1024];
2850 static DEFINE_MUTEX(dyn_info_mutex);
2851 unsigned long *p = filp->private_data;
2852 char *buf = ftrace_dyn_info_buffer;
2853 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
2856 mutex_lock(&dyn_info_mutex);
2857 r = sprintf(buf, "%ld ", *p);
2859 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
2862 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2864 mutex_unlock(&dyn_info_mutex);
2869 static struct file_operations tracing_dyn_info_fops = {
2870 .open = tracing_open_generic,
2871 .read = tracing_read_dyn_info,
2875 static struct dentry *d_tracer;
2877 struct dentry *tracing_init_dentry(void)
2884 d_tracer = debugfs_create_dir("tracing", NULL);
2886 if (!d_tracer && !once) {
2888 pr_warning("Could not create debugfs directory 'tracing'\n");
2895 #ifdef CONFIG_FTRACE_SELFTEST
2896 /* Let selftest have access to static functions in this file */
2897 #include "trace_selftest.c"
2900 static __init int tracer_init_debugfs(void)
2902 struct dentry *d_tracer;
2903 struct dentry *entry;
2905 d_tracer = tracing_init_dentry();
2907 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2908 &global_trace, &tracing_ctrl_fops);
2910 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2912 entry = debugfs_create_file("trace_options", 0644, d_tracer,
2913 NULL, &tracing_iter_fops);
2915 pr_warning("Could not create debugfs 'trace_options' entry\n");
2917 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2918 NULL, &tracing_cpumask_fops);
2920 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2922 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2923 &global_trace, &tracing_lt_fops);
2925 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2927 entry = debugfs_create_file("trace", 0444, d_tracer,
2928 &global_trace, &tracing_fops);
2930 pr_warning("Could not create debugfs 'trace' entry\n");
2932 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2933 &global_trace, &show_traces_fops);
2935 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2937 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2938 &global_trace, &set_tracer_fops);
2940 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2942 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2943 &tracing_max_latency,
2944 &tracing_max_lat_fops);
2946 pr_warning("Could not create debugfs "
2947 "'tracing_max_latency' entry\n");
2949 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2950 &tracing_thresh, &tracing_max_lat_fops);
2952 pr_warning("Could not create debugfs "
2953 "'tracing_thresh' entry\n");
2954 entry = debugfs_create_file("README", 0644, d_tracer,
2955 NULL, &tracing_readme_fops);
2957 pr_warning("Could not create debugfs 'README' entry\n");
2959 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2960 NULL, &tracing_pipe_fops);
2962 pr_warning("Could not create debugfs "
2963 "'trace_pipe' entry\n");
2965 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
2966 &global_trace, &tracing_entries_fops);
2968 pr_warning("Could not create debugfs "
2969 "'buffer_size_kb' entry\n");
2971 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2972 NULL, &tracing_mark_fops);
2974 pr_warning("Could not create debugfs "
2975 "'trace_marker' entry\n");
2977 #ifdef CONFIG_DYNAMIC_FTRACE
2978 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2979 &ftrace_update_tot_cnt,
2980 &tracing_dyn_info_fops);
2982 pr_warning("Could not create debugfs "
2983 "'dyn_ftrace_total_info' entry\n");
2985 #ifdef CONFIG_SYSPROF_TRACER
2986 init_tracer_sysprof_debugfs(d_tracer);
2991 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
2993 static DEFINE_SPINLOCK(trace_buf_lock);
2994 static char trace_buf[TRACE_BUF_SIZE];
2996 struct ring_buffer_event *event;
2997 struct trace_array *tr = &global_trace;
2998 struct trace_array_cpu *data;
2999 int cpu, len = 0, size, pc;
3000 struct print_entry *entry;
3001 unsigned long irq_flags;
3003 if (tracing_disabled || tracing_selftest_running)
3006 pc = preempt_count();
3007 preempt_disable_notrace();
3008 cpu = raw_smp_processor_id();
3009 data = tr->data[cpu];
3011 if (unlikely(atomic_read(&data->disabled)))
3014 pause_graph_tracing();
3015 spin_lock_irqsave(&trace_buf_lock, irq_flags);
3016 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3018 len = min(len, TRACE_BUF_SIZE-1);
3021 size = sizeof(*entry) + len + 1;
3022 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
3025 entry = ring_buffer_event_data(event);
3027 entry->depth = depth;
3029 memcpy(&entry->buf, trace_buf, len);
3030 entry->buf[len] = 0;
3031 ring_buffer_unlock_commit(tr->buffer, event);
3034 spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
3035 unpause_graph_tracing();
3037 preempt_enable_notrace();
3041 EXPORT_SYMBOL_GPL(trace_vprintk);
3043 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3048 if (!(trace_flags & TRACE_ITER_PRINTK))
3052 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3056 EXPORT_SYMBOL_GPL(__ftrace_printk);
3058 int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
3060 if (!(trace_flags & TRACE_ITER_PRINTK))
3063 return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3065 EXPORT_SYMBOL_GPL(__ftrace_vprintk);
3067 static int trace_panic_handler(struct notifier_block *this,
3068 unsigned long event, void *unused)
3070 if (ftrace_dump_on_oops)
3075 static struct notifier_block trace_panic_notifier = {
3076 .notifier_call = trace_panic_handler,
3078 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3081 static int trace_die_handler(struct notifier_block *self,
3087 if (ftrace_dump_on_oops)
3096 static struct notifier_block trace_die_notifier = {
3097 .notifier_call = trace_die_handler,
3102 * printk is set to max of 1024, we really don't need it that big.
3103 * Nothing should be printing 1000 characters anyway.
3105 #define TRACE_MAX_PRINT 1000
3108 * Define here KERN_TRACE so that we have one place to modify
3109 * it if we decide to change what log level the ftrace dump
3112 #define KERN_TRACE KERN_EMERG
3115 trace_printk_seq(struct trace_seq *s)
3117 /* Probably should print a warning here. */
3121 /* should be zero ended, but we are paranoid. */
3122 s->buffer[s->len] = 0;
3124 printk(KERN_TRACE "%s", s->buffer);
3129 void ftrace_dump(void)
3131 static DEFINE_SPINLOCK(ftrace_dump_lock);
3132 /* use static because iter can be a bit big for the stack */
3133 static struct trace_iterator iter;
3134 static int dump_ran;
3135 unsigned long flags;
3139 spin_lock_irqsave(&ftrace_dump_lock, flags);
3145 /* No turning back! */
3149 for_each_tracing_cpu(cpu) {
3150 atomic_inc(&global_trace.data[cpu]->disabled);
3153 /* don't look at user memory in panic mode */
3154 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3156 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3158 iter.tr = &global_trace;
3159 iter.trace = current_trace;
3162 * We need to stop all tracing on all CPUS to read the
3163 * the next buffer. This is a bit expensive, but is
3164 * not done often. We fill all what we can read,
3165 * and then release the locks again.
3168 while (!trace_empty(&iter)) {
3171 printk(KERN_TRACE "---------------------------------\n");
3175 /* reset all but tr, trace, and overruns */
3176 memset(&iter.seq, 0,
3177 sizeof(struct trace_iterator) -
3178 offsetof(struct trace_iterator, seq));
3179 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3182 if (find_next_entry_inc(&iter) != NULL) {
3183 print_trace_line(&iter);
3184 trace_consume(&iter);
3187 trace_printk_seq(&iter.seq);
3191 printk(KERN_TRACE " (ftrace buffer empty)\n");
3193 printk(KERN_TRACE "---------------------------------\n");
3196 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3199 __init static int tracer_alloc_buffers(void)
3201 struct trace_array_cpu *data;
3205 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3208 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3209 goto out_free_buffer_mask;
3211 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3212 cpumask_copy(tracing_cpumask, cpu_all_mask);
3214 /* TODO: make the number of buffers hot pluggable with CPUS */
3215 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3216 TRACE_BUFFER_FLAGS);
3217 if (!global_trace.buffer) {
3218 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3220 goto out_free_cpumask;
3222 global_trace.entries = ring_buffer_size(global_trace.buffer);
3225 #ifdef CONFIG_TRACER_MAX_TRACE
3226 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3227 TRACE_BUFFER_FLAGS);
3228 if (!max_tr.buffer) {
3229 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3231 ring_buffer_free(global_trace.buffer);
3232 goto out_free_cpumask;
3234 max_tr.entries = ring_buffer_size(max_tr.buffer);
3235 WARN_ON(max_tr.entries != global_trace.entries);
3238 /* Allocate the first page for all buffers */
3239 for_each_tracing_cpu(i) {
3240 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3241 max_tr.data[i] = &per_cpu(max_data, i);
3244 trace_init_cmdlines();
3246 register_tracer(&nop_trace);
3247 current_trace = &nop_trace;
3248 #ifdef CONFIG_BOOT_TRACER
3249 register_tracer(&boot_tracer);
3251 /* All seems OK, enable tracing */
3252 tracing_disabled = 0;
3254 atomic_notifier_chain_register(&panic_notifier_list,
3255 &trace_panic_notifier);
3257 register_die_notifier(&trace_die_notifier);
3261 free_cpumask_var(tracing_cpumask);
3262 out_free_buffer_mask:
3263 free_cpumask_var(tracing_buffer_mask);
3268 __init static int clear_boot_tracer(void)
3271 * The default tracer at boot buffer is an init section.
3272 * This function is called in lateinit. If we did not
3273 * find the boot tracer, then clear it out, to prevent
3274 * later registration from accessing the buffer that is
3275 * about to be freed.
3277 if (!default_bootup_tracer)
3280 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
3281 default_bootup_tracer);
3282 default_bootup_tracer = NULL;
3287 early_initcall(tracer_alloc_buffers);
3288 fs_initcall(tracer_init_debugfs);
3289 late_initcall(clear_boot_tracer);