2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/notifier.h>
18 #include <linux/debugfs.h>
19 #include <linux/pagemap.h>
20 #include <linux/hardirq.h>
21 #include <linux/linkage.h>
22 #include <linux/uaccess.h>
23 #include <linux/ftrace.h>
24 #include <linux/module.h>
25 #include <linux/percpu.h>
26 #include <linux/kdebug.h>
27 #include <linux/ctype.h>
28 #include <linux/init.h>
29 #include <linux/poll.h>
30 #include <linux/gfp.h>
32 #include <linux/kprobes.h>
33 #include <linux/writeback.h>
34 #include <linux/splice.h>
36 #include <linux/stacktrace.h>
37 #include <linux/ring_buffer.h>
38 #include <linux/irqflags.h>
41 #include "trace_output.h"
43 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
45 unsigned long __read_mostly tracing_max_latency;
46 unsigned long __read_mostly tracing_thresh;
49 * We need to change this state when a selftest is running.
50 * A selftest will lurk into the ring-buffer to count the
51 * entries inserted during the selftest although some concurrent
52 * insertions into the ring-buffer such as ftrace_printk could occurred
53 * at the same time, giving false positive or negative results.
55 static bool __read_mostly tracing_selftest_running;
58 * If a tracer is running, we do not want to run SELFTEST.
60 static bool __read_mostly tracing_selftest_disabled;
62 /* For tracers that don't implement custom flags */
63 static struct tracer_opt dummy_tracer_opt[] = {
67 static struct tracer_flags dummy_tracer_flags = {
69 .opts = dummy_tracer_opt
72 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
78 * Kill all tracing for good (never come back).
79 * It is initialized to 1 but will turn to zero if the initialization
80 * of the tracer is successful. But that is the only place that sets
83 static int tracing_disabled = 1;
85 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
87 static inline void ftrace_disable_cpu(void)
90 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
93 static inline void ftrace_enable_cpu(void)
95 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
99 static cpumask_var_t __read_mostly tracing_buffer_mask;
101 /* Define which cpu buffers are currently read in trace_pipe */
102 static cpumask_var_t tracing_reader_cpumask;
104 #define for_each_tracing_cpu(cpu) \
105 for_each_cpu(cpu, tracing_buffer_mask)
108 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
110 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
111 * is set, then ftrace_dump is called. This will output the contents
112 * of the ftrace buffers to the console. This is very useful for
113 * capturing traces that lead to crashes and outputing it to a
116 * It is default off, but you can enable it with either specifying
117 * "ftrace_dump_on_oops" in the kernel command line, or setting
118 * /proc/sys/kernel/ftrace_dump_on_oops to true.
120 int ftrace_dump_on_oops;
122 static int tracing_set_tracer(const char *buf);
124 #define BOOTUP_TRACER_SIZE 100
125 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
126 static char *default_bootup_tracer;
128 static int __init set_ftrace(char *str)
130 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
131 default_bootup_tracer = bootup_tracer_buf;
134 __setup("ftrace=", set_ftrace);
136 static int __init set_ftrace_dump_on_oops(char *str)
138 ftrace_dump_on_oops = 1;
141 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
144 ns2usecs(cycle_t nsec)
151 cycle_t ftrace_now(int cpu)
153 u64 ts = ring_buffer_time_stamp(cpu);
154 ring_buffer_normalize_time_stamp(cpu, &ts);
159 * The global_trace is the descriptor that holds the tracing
160 * buffers for the live tracing. For each CPU, it contains
161 * a link list of pages that will store trace entries. The
162 * page descriptor of the pages in the memory is used to hold
163 * the link list by linking the lru item in the page descriptor
164 * to each of the pages in the buffer per CPU.
166 * For each active CPU there is a data field that holds the
167 * pages for the buffer for that CPU. Each CPU has the same number
168 * of pages allocated for its buffer.
170 static struct trace_array global_trace;
172 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
175 * The max_tr is used to snapshot the global_trace when a maximum
176 * latency is reached. Some tracers will use this to store a maximum
177 * trace while it continues examining live traces.
179 * The buffers for the max_tr are set up the same as the global_trace.
180 * When a snapshot is taken, the link list of the max_tr is swapped
181 * with the link list of the global_trace and the buffers are reset for
182 * the global_trace so the tracing can continue.
184 static struct trace_array max_tr;
186 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
188 /* tracer_enabled is used to toggle activation of a tracer */
189 static int tracer_enabled = 1;
192 * tracing_is_enabled - return tracer_enabled status
194 * This function is used by other tracers to know the status
195 * of the tracer_enabled flag. Tracers may use this function
196 * to know if it should enable their features when starting
197 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
199 int tracing_is_enabled(void)
201 return tracer_enabled;
205 * trace_buf_size is the size in bytes that is allocated
206 * for a buffer. Note, the number of bytes is always rounded
209 * This number is purposely set to a low number of 16384.
210 * If the dump on oops happens, it will be much appreciated
211 * to not have to wait for all that output. Anyway this can be
212 * boot time and run time configurable.
214 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
216 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
218 /* trace_types holds a link list of available tracers. */
219 static struct tracer *trace_types __read_mostly;
221 /* current_trace points to the tracer that is currently active */
222 static struct tracer *current_trace __read_mostly;
225 * max_tracer_type_len is used to simplify the allocating of
226 * buffers to read userspace tracer names. We keep track of
227 * the longest tracer name registered.
229 static int max_tracer_type_len;
232 * trace_types_lock is used to protect the trace_types list.
233 * This lock is also used to keep user access serialized.
234 * Accesses from userspace will grab this lock while userspace
235 * activities happen inside the kernel.
237 static DEFINE_MUTEX(trace_types_lock);
239 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
240 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
242 /* trace_flags holds trace_options default values */
243 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
244 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
247 * trace_wake_up - wake up tasks waiting for trace input
249 * Simply wakes up any task that is blocked on the trace_wait
250 * queue. These is used with trace_poll for tasks polling the trace.
252 void trace_wake_up(void)
255 * The runqueue_is_locked() can fail, but this is the best we
258 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
259 wake_up(&trace_wait);
262 static int __init set_buf_size(char *str)
264 unsigned long buf_size;
269 ret = strict_strtoul(str, 0, &buf_size);
270 /* nr_entries can not be zero */
271 if (ret < 0 || buf_size == 0)
273 trace_buf_size = buf_size;
276 __setup("trace_buf_size=", set_buf_size);
278 unsigned long nsecs_to_usecs(unsigned long nsecs)
283 /* These must match the bit postions in trace_iterator_flags */
284 static const char *trace_options[] = {
307 * ftrace_max_lock is used to protect the swapping of buffers
308 * when taking a max snapshot. The buffers themselves are
309 * protected by per_cpu spinlocks. But the action of the swap
310 * needs its own lock.
312 * This is defined as a raw_spinlock_t in order to help
313 * with performance when lockdep debugging is enabled.
315 static raw_spinlock_t ftrace_max_lock =
316 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
319 * Copy the new maximum trace into the separate maximum-trace
320 * structure. (this way the maximum trace is permanently saved,
321 * for later retrieval via /debugfs/tracing/latency_trace)
324 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
326 struct trace_array_cpu *data = tr->data[cpu];
329 max_tr.time_start = data->preempt_timestamp;
331 data = max_tr.data[cpu];
332 data->saved_latency = tracing_max_latency;
334 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
335 data->pid = tsk->pid;
336 data->uid = task_uid(tsk);
337 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
338 data->policy = tsk->policy;
339 data->rt_priority = tsk->rt_priority;
341 /* record this tasks comm */
342 tracing_record_cmdline(tsk);
346 trace_seq_reset(struct trace_seq *s)
352 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
357 if (s->len <= s->readpos)
360 len = s->len - s->readpos;
363 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
371 ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
376 if (s->len <= s->readpos)
379 len = s->len - s->readpos;
382 ret = memcpy(buf, s->buffer + s->readpos, cnt);
391 trace_print_seq(struct seq_file *m, struct trace_seq *s)
393 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
396 seq_puts(m, s->buffer);
402 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
404 * @tsk: the task with the latency
405 * @cpu: The cpu that initiated the trace.
407 * Flip the buffers between the @tr and the max_tr and record information
408 * about which task was the cause of this latency.
411 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
413 struct ring_buffer *buf = tr->buffer;
415 WARN_ON_ONCE(!irqs_disabled());
416 __raw_spin_lock(&ftrace_max_lock);
418 tr->buffer = max_tr.buffer;
421 ftrace_disable_cpu();
422 ring_buffer_reset(tr->buffer);
425 __update_max_tr(tr, tsk, cpu);
426 __raw_spin_unlock(&ftrace_max_lock);
430 * update_max_tr_single - only copy one trace over, and reset the rest
432 * @tsk - task with the latency
433 * @cpu - the cpu of the buffer to copy.
435 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
438 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
442 WARN_ON_ONCE(!irqs_disabled());
443 __raw_spin_lock(&ftrace_max_lock);
445 ftrace_disable_cpu();
447 ring_buffer_reset(max_tr.buffer);
448 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
452 WARN_ON_ONCE(ret && ret != -EAGAIN);
454 __update_max_tr(tr, tsk, cpu);
455 __raw_spin_unlock(&ftrace_max_lock);
459 * register_tracer - register a tracer with the ftrace system.
460 * @type - the plugin for the tracer
462 * Register a new plugin tracer.
464 int register_tracer(struct tracer *type)
465 __releases(kernel_lock)
466 __acquires(kernel_lock)
473 pr_info("Tracer must have a name\n");
478 * When this gets called we hold the BKL which means that
479 * preemption is disabled. Various trace selftests however
480 * need to disable and enable preemption for successful tests.
481 * So we drop the BKL here and grab it after the tests again.
484 mutex_lock(&trace_types_lock);
486 tracing_selftest_running = true;
488 for (t = trace_types; t; t = t->next) {
489 if (strcmp(type->name, t->name) == 0) {
491 pr_info("Trace %s already registered\n",
499 type->set_flag = &dummy_set_flag;
501 type->flags = &dummy_tracer_flags;
503 if (!type->flags->opts)
504 type->flags->opts = dummy_tracer_opt;
505 if (!type->wait_pipe)
506 type->wait_pipe = default_wait_pipe;
509 #ifdef CONFIG_FTRACE_STARTUP_TEST
510 if (type->selftest && !tracing_selftest_disabled) {
511 struct tracer *saved_tracer = current_trace;
512 struct trace_array *tr = &global_trace;
516 * Run a selftest on this tracer.
517 * Here we reset the trace buffer, and set the current
518 * tracer to be this tracer. The tracer can then run some
519 * internal tracing to verify that everything is in order.
520 * If we fail, we do not register this tracer.
522 for_each_tracing_cpu(i)
523 tracing_reset(tr, i);
525 current_trace = type;
526 /* the test is responsible for initializing and enabling */
527 pr_info("Testing tracer %s: ", type->name);
528 ret = type->selftest(type, tr);
529 /* the test is responsible for resetting too */
530 current_trace = saved_tracer;
532 printk(KERN_CONT "FAILED!\n");
535 /* Only reset on passing, to avoid touching corrupted buffers */
536 for_each_tracing_cpu(i)
537 tracing_reset(tr, i);
539 printk(KERN_CONT "PASSED\n");
543 type->next = trace_types;
545 len = strlen(type->name);
546 if (len > max_tracer_type_len)
547 max_tracer_type_len = len;
550 tracing_selftest_running = false;
551 mutex_unlock(&trace_types_lock);
553 if (ret || !default_bootup_tracer)
556 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
559 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
560 /* Do we want this tracer to start on bootup? */
561 tracing_set_tracer(type->name);
562 default_bootup_tracer = NULL;
563 /* disable other selftests, since this will break it. */
564 tracing_selftest_disabled = 1;
565 #ifdef CONFIG_FTRACE_STARTUP_TEST
566 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
575 void unregister_tracer(struct tracer *type)
580 mutex_lock(&trace_types_lock);
581 for (t = &trace_types; *t; t = &(*t)->next) {
585 pr_info("Trace %s not registered\n", type->name);
591 if (type == current_trace && tracer_enabled) {
594 if (current_trace->stop)
595 current_trace->stop(&global_trace);
596 current_trace = &nop_trace;
599 if (strlen(type->name) != max_tracer_type_len)
602 max_tracer_type_len = 0;
603 for (t = &trace_types; *t; t = &(*t)->next) {
604 len = strlen((*t)->name);
605 if (len > max_tracer_type_len)
606 max_tracer_type_len = len;
609 mutex_unlock(&trace_types_lock);
612 void tracing_reset(struct trace_array *tr, int cpu)
614 ftrace_disable_cpu();
615 ring_buffer_reset_cpu(tr->buffer, cpu);
619 void tracing_reset_online_cpus(struct trace_array *tr)
623 tr->time_start = ftrace_now(tr->cpu);
625 for_each_online_cpu(cpu)
626 tracing_reset(tr, cpu);
629 #define SAVED_CMDLINES 128
630 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
631 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
632 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
633 static int cmdline_idx;
634 static DEFINE_SPINLOCK(trace_cmdline_lock);
636 /* temporary disable recording */
637 static atomic_t trace_record_cmdline_disabled __read_mostly;
639 static void trace_init_cmdlines(void)
641 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
642 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
646 static int trace_stop_count;
647 static DEFINE_SPINLOCK(tracing_start_lock);
650 * ftrace_off_permanent - disable all ftrace code permanently
652 * This should only be called when a serious anomally has
653 * been detected. This will turn off the function tracing,
654 * ring buffers, and other tracing utilites. It takes no
655 * locks and can be called from any context.
657 void ftrace_off_permanent(void)
659 tracing_disabled = 1;
661 tracing_off_permanent();
665 * tracing_start - quick start of the tracer
667 * If tracing is enabled but was stopped by tracing_stop,
668 * this will start the tracer back up.
670 void tracing_start(void)
672 struct ring_buffer *buffer;
675 if (tracing_disabled)
678 spin_lock_irqsave(&tracing_start_lock, flags);
679 if (--trace_stop_count) {
680 if (trace_stop_count < 0) {
681 /* Someone screwed up their debugging */
683 trace_stop_count = 0;
689 buffer = global_trace.buffer;
691 ring_buffer_record_enable(buffer);
693 buffer = max_tr.buffer;
695 ring_buffer_record_enable(buffer);
699 spin_unlock_irqrestore(&tracing_start_lock, flags);
703 * tracing_stop - quick stop of the tracer
705 * Light weight way to stop tracing. Use in conjunction with
708 void tracing_stop(void)
710 struct ring_buffer *buffer;
714 spin_lock_irqsave(&tracing_start_lock, flags);
715 if (trace_stop_count++)
718 buffer = global_trace.buffer;
720 ring_buffer_record_disable(buffer);
722 buffer = max_tr.buffer;
724 ring_buffer_record_disable(buffer);
727 spin_unlock_irqrestore(&tracing_start_lock, flags);
730 void trace_stop_cmdline_recording(void);
732 static void trace_save_cmdline(struct task_struct *tsk)
737 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
741 * It's not the end of the world if we don't get
742 * the lock, but we also don't want to spin
743 * nor do we want to disable interrupts,
744 * so if we miss here, then better luck next time.
746 if (!spin_trylock(&trace_cmdline_lock))
749 idx = map_pid_to_cmdline[tsk->pid];
750 if (idx >= SAVED_CMDLINES) {
751 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
753 map = map_cmdline_to_pid[idx];
754 if (map <= PID_MAX_DEFAULT)
755 map_pid_to_cmdline[map] = (unsigned)-1;
757 map_pid_to_cmdline[tsk->pid] = idx;
762 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
764 spin_unlock(&trace_cmdline_lock);
767 char *trace_find_cmdline(int pid)
769 char *cmdline = "<...>";
775 if (pid > PID_MAX_DEFAULT)
778 map = map_pid_to_cmdline[pid];
779 if (map >= SAVED_CMDLINES)
782 cmdline = saved_cmdlines[map];
788 void tracing_record_cmdline(struct task_struct *tsk)
790 if (atomic_read(&trace_record_cmdline_disabled))
793 trace_save_cmdline(tsk);
797 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
800 struct task_struct *tsk = current;
802 entry->preempt_count = pc & 0xff;
803 entry->pid = (tsk) ? tsk->pid : 0;
804 entry->tgid = (tsk) ? tsk->tgid : 0;
806 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
807 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
809 TRACE_FLAG_IRQS_NOSUPPORT |
811 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
812 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
813 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
816 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
819 unsigned long flags, int pc)
821 struct ring_buffer_event *event;
823 event = ring_buffer_lock_reserve(tr->buffer, len);
825 struct trace_entry *ent = ring_buffer_event_data(event);
827 tracing_generic_entry_update(ent, flags, pc);
833 static void ftrace_trace_stack(struct trace_array *tr,
834 unsigned long flags, int skip, int pc);
835 static void ftrace_trace_userstack(struct trace_array *tr,
836 unsigned long flags, int pc);
838 void trace_buffer_unlock_commit(struct trace_array *tr,
839 struct ring_buffer_event *event,
840 unsigned long flags, int pc)
842 ring_buffer_unlock_commit(tr->buffer, event);
844 ftrace_trace_stack(tr, flags, 6, pc);
845 ftrace_trace_userstack(tr, flags, pc);
849 struct ring_buffer_event *
850 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
851 unsigned long flags, int pc)
853 return trace_buffer_lock_reserve(&global_trace,
854 type, len, flags, pc);
857 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
858 unsigned long flags, int pc)
860 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
864 trace_function(struct trace_array *tr,
865 unsigned long ip, unsigned long parent_ip, unsigned long flags,
868 struct ring_buffer_event *event;
869 struct ftrace_entry *entry;
871 /* If we are reading the ring buffer, don't trace */
872 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
875 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
879 entry = ring_buffer_event_data(event);
881 entry->parent_ip = parent_ip;
882 ring_buffer_unlock_commit(tr->buffer, event);
885 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
886 static void __trace_graph_entry(struct trace_array *tr,
887 struct ftrace_graph_ent *trace,
891 struct ring_buffer_event *event;
892 struct ftrace_graph_ent_entry *entry;
894 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
897 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
898 sizeof(*entry), flags, pc);
901 entry = ring_buffer_event_data(event);
902 entry->graph_ent = *trace;
903 ring_buffer_unlock_commit(global_trace.buffer, event);
906 static void __trace_graph_return(struct trace_array *tr,
907 struct ftrace_graph_ret *trace,
911 struct ring_buffer_event *event;
912 struct ftrace_graph_ret_entry *entry;
914 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
917 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
918 sizeof(*entry), flags, pc);
921 entry = ring_buffer_event_data(event);
923 ring_buffer_unlock_commit(global_trace.buffer, event);
928 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
929 unsigned long ip, unsigned long parent_ip, unsigned long flags,
932 if (likely(!atomic_read(&data->disabled)))
933 trace_function(tr, ip, parent_ip, flags, pc);
936 static void __ftrace_trace_stack(struct trace_array *tr,
940 #ifdef CONFIG_STACKTRACE
941 struct ring_buffer_event *event;
942 struct stack_entry *entry;
943 struct stack_trace trace;
945 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
946 sizeof(*entry), flags, pc);
949 entry = ring_buffer_event_data(event);
950 memset(&entry->caller, 0, sizeof(entry->caller));
952 trace.nr_entries = 0;
953 trace.max_entries = FTRACE_STACK_ENTRIES;
955 trace.entries = entry->caller;
957 save_stack_trace(&trace);
958 ring_buffer_unlock_commit(tr->buffer, event);
962 static void ftrace_trace_stack(struct trace_array *tr,
966 if (!(trace_flags & TRACE_ITER_STACKTRACE))
969 __ftrace_trace_stack(tr, flags, skip, pc);
972 void __trace_stack(struct trace_array *tr,
976 __ftrace_trace_stack(tr, flags, skip, pc);
979 static void ftrace_trace_userstack(struct trace_array *tr,
980 unsigned long flags, int pc)
982 #ifdef CONFIG_STACKTRACE
983 struct ring_buffer_event *event;
984 struct userstack_entry *entry;
985 struct stack_trace trace;
987 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
990 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
991 sizeof(*entry), flags, pc);
994 entry = ring_buffer_event_data(event);
996 memset(&entry->caller, 0, sizeof(entry->caller));
998 trace.nr_entries = 0;
999 trace.max_entries = FTRACE_STACK_ENTRIES;
1001 trace.entries = entry->caller;
1003 save_stack_trace_user(&trace);
1004 ring_buffer_unlock_commit(tr->buffer, event);
1009 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1011 ftrace_trace_userstack(tr, flags, preempt_count());
1016 ftrace_trace_special(void *__tr,
1017 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1020 struct ring_buffer_event *event;
1021 struct trace_array *tr = __tr;
1022 struct special_entry *entry;
1024 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1025 sizeof(*entry), 0, pc);
1028 entry = ring_buffer_event_data(event);
1032 trace_buffer_unlock_commit(tr, event, 0, pc);
1036 __trace_special(void *__tr, void *__data,
1037 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1039 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1043 tracing_sched_switch_trace(struct trace_array *tr,
1044 struct task_struct *prev,
1045 struct task_struct *next,
1046 unsigned long flags, int pc)
1048 struct ring_buffer_event *event;
1049 struct ctx_switch_entry *entry;
1051 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1052 sizeof(*entry), flags, pc);
1055 entry = ring_buffer_event_data(event);
1056 entry->prev_pid = prev->pid;
1057 entry->prev_prio = prev->prio;
1058 entry->prev_state = prev->state;
1059 entry->next_pid = next->pid;
1060 entry->next_prio = next->prio;
1061 entry->next_state = next->state;
1062 entry->next_cpu = task_cpu(next);
1063 trace_buffer_unlock_commit(tr, event, flags, pc);
1067 tracing_sched_wakeup_trace(struct trace_array *tr,
1068 struct task_struct *wakee,
1069 struct task_struct *curr,
1070 unsigned long flags, int pc)
1072 struct ring_buffer_event *event;
1073 struct ctx_switch_entry *entry;
1075 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1076 sizeof(*entry), flags, pc);
1079 entry = ring_buffer_event_data(event);
1080 entry->prev_pid = curr->pid;
1081 entry->prev_prio = curr->prio;
1082 entry->prev_state = curr->state;
1083 entry->next_pid = wakee->pid;
1084 entry->next_prio = wakee->prio;
1085 entry->next_state = wakee->state;
1086 entry->next_cpu = task_cpu(wakee);
1088 ring_buffer_unlock_commit(tr->buffer, event);
1089 ftrace_trace_stack(tr, flags, 6, pc);
1090 ftrace_trace_userstack(tr, flags, pc);
1094 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1096 struct trace_array *tr = &global_trace;
1097 struct trace_array_cpu *data;
1098 unsigned long flags;
1102 if (tracing_disabled)
1105 pc = preempt_count();
1106 local_irq_save(flags);
1107 cpu = raw_smp_processor_id();
1108 data = tr->data[cpu];
1110 if (likely(atomic_inc_return(&data->disabled) == 1))
1111 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1113 atomic_dec(&data->disabled);
1114 local_irq_restore(flags);
1117 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1118 int trace_graph_entry(struct ftrace_graph_ent *trace)
1120 struct trace_array *tr = &global_trace;
1121 struct trace_array_cpu *data;
1122 unsigned long flags;
1127 if (!ftrace_trace_task(current))
1130 if (!ftrace_graph_addr(trace->func))
1133 local_irq_save(flags);
1134 cpu = raw_smp_processor_id();
1135 data = tr->data[cpu];
1136 disabled = atomic_inc_return(&data->disabled);
1137 if (likely(disabled == 1)) {
1138 pc = preempt_count();
1139 __trace_graph_entry(tr, trace, flags, pc);
1141 /* Only do the atomic if it is not already set */
1142 if (!test_tsk_trace_graph(current))
1143 set_tsk_trace_graph(current);
1144 atomic_dec(&data->disabled);
1145 local_irq_restore(flags);
1150 void trace_graph_return(struct ftrace_graph_ret *trace)
1152 struct trace_array *tr = &global_trace;
1153 struct trace_array_cpu *data;
1154 unsigned long flags;
1159 local_irq_save(flags);
1160 cpu = raw_smp_processor_id();
1161 data = tr->data[cpu];
1162 disabled = atomic_inc_return(&data->disabled);
1163 if (likely(disabled == 1)) {
1164 pc = preempt_count();
1165 __trace_graph_return(tr, trace, flags, pc);
1168 clear_tsk_trace_graph(current);
1169 atomic_dec(&data->disabled);
1170 local_irq_restore(flags);
1172 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1174 enum trace_file_type {
1175 TRACE_FILE_LAT_FMT = 1,
1176 TRACE_FILE_ANNOTATE = 2,
1179 static void trace_iterator_increment(struct trace_iterator *iter)
1181 /* Don't allow ftrace to trace into the ring buffers */
1182 ftrace_disable_cpu();
1185 if (iter->buffer_iter[iter->cpu])
1186 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1188 ftrace_enable_cpu();
1191 static struct trace_entry *
1192 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1194 struct ring_buffer_event *event;
1195 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1197 /* Don't allow ftrace to trace into the ring buffers */
1198 ftrace_disable_cpu();
1201 event = ring_buffer_iter_peek(buf_iter, ts);
1203 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1205 ftrace_enable_cpu();
1207 return event ? ring_buffer_event_data(event) : NULL;
1210 static struct trace_entry *
1211 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1213 struct ring_buffer *buffer = iter->tr->buffer;
1214 struct trace_entry *ent, *next = NULL;
1215 int cpu_file = iter->cpu_file;
1216 u64 next_ts = 0, ts;
1221 * If we are in a per_cpu trace file, don't bother by iterating over
1222 * all cpu and peek directly.
1224 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1225 if (ring_buffer_empty_cpu(buffer, cpu_file))
1227 ent = peek_next_entry(iter, cpu_file, ent_ts);
1229 *ent_cpu = cpu_file;
1234 for_each_tracing_cpu(cpu) {
1236 if (ring_buffer_empty_cpu(buffer, cpu))
1239 ent = peek_next_entry(iter, cpu, &ts);
1242 * Pick the entry with the smallest timestamp:
1244 if (ent && (!next || ts < next_ts)) {
1252 *ent_cpu = next_cpu;
1260 /* Find the next real entry, without updating the iterator itself */
1261 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1262 int *ent_cpu, u64 *ent_ts)
1264 return __find_next_entry(iter, ent_cpu, ent_ts);
1267 /* Find the next real entry, and increment the iterator to the next entry */
1268 static void *find_next_entry_inc(struct trace_iterator *iter)
1270 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1273 trace_iterator_increment(iter);
1275 return iter->ent ? iter : NULL;
1278 static void trace_consume(struct trace_iterator *iter)
1280 /* Don't allow ftrace to trace into the ring buffers */
1281 ftrace_disable_cpu();
1282 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1283 ftrace_enable_cpu();
1286 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1288 struct trace_iterator *iter = m->private;
1294 /* can't go backwards */
1299 ent = find_next_entry_inc(iter);
1303 while (ent && iter->idx < i)
1304 ent = find_next_entry_inc(iter);
1312 * No necessary locking here. The worst thing which can
1313 * happen is loosing events consumed at the same time
1314 * by a trace_pipe reader.
1315 * Other than that, we don't risk to crash the ring buffer
1316 * because it serializes the readers.
1318 * The current tracer is copied to avoid a global locking
1321 static void *s_start(struct seq_file *m, loff_t *pos)
1323 struct trace_iterator *iter = m->private;
1324 static struct tracer *old_tracer;
1325 int cpu_file = iter->cpu_file;
1330 /* copy the tracer to avoid using a global lock all around */
1331 mutex_lock(&trace_types_lock);
1332 if (unlikely(old_tracer != current_trace && current_trace)) {
1333 old_tracer = current_trace;
1334 *iter->trace = *current_trace;
1336 mutex_unlock(&trace_types_lock);
1338 atomic_inc(&trace_record_cmdline_disabled);
1340 if (*pos != iter->pos) {
1345 ftrace_disable_cpu();
1347 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1348 for_each_tracing_cpu(cpu)
1349 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1351 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1354 ftrace_enable_cpu();
1356 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1361 p = s_next(m, p, &l);
1367 static void s_stop(struct seq_file *m, void *p)
1369 atomic_dec(&trace_record_cmdline_disabled);
1372 static void print_lat_help_header(struct seq_file *m)
1374 seq_puts(m, "# _------=> CPU# \n");
1375 seq_puts(m, "# / _-----=> irqs-off \n");
1376 seq_puts(m, "# | / _----=> need-resched \n");
1377 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1378 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1379 seq_puts(m, "# |||| / \n");
1380 seq_puts(m, "# ||||| delay \n");
1381 seq_puts(m, "# cmd pid ||||| time | caller \n");
1382 seq_puts(m, "# \\ / ||||| \\ | / \n");
1385 static void print_func_help_header(struct seq_file *m)
1387 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1388 seq_puts(m, "# | | | | |\n");
1393 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1395 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1396 struct trace_array *tr = iter->tr;
1397 struct trace_array_cpu *data = tr->data[tr->cpu];
1398 struct tracer *type = current_trace;
1399 unsigned long total;
1400 unsigned long entries;
1401 const char *name = "preemption";
1406 entries = ring_buffer_entries(iter->tr->buffer);
1408 ring_buffer_overruns(iter->tr->buffer);
1410 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1412 seq_puts(m, "-----------------------------------"
1413 "---------------------------------\n");
1414 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1415 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1416 nsecs_to_usecs(data->saved_latency),
1420 #if defined(CONFIG_PREEMPT_NONE)
1422 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1424 #elif defined(CONFIG_PREEMPT)
1429 /* These are reserved for later use */
1432 seq_printf(m, " #P:%d)\n", num_online_cpus());
1436 seq_puts(m, " -----------------\n");
1437 seq_printf(m, " | task: %.16s-%d "
1438 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1439 data->comm, data->pid, data->uid, data->nice,
1440 data->policy, data->rt_priority);
1441 seq_puts(m, " -----------------\n");
1443 if (data->critical_start) {
1444 seq_puts(m, " => started at: ");
1445 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1446 trace_print_seq(m, &iter->seq);
1447 seq_puts(m, "\n => ended at: ");
1448 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1449 trace_print_seq(m, &iter->seq);
1456 static void test_cpu_buff_start(struct trace_iterator *iter)
1458 struct trace_seq *s = &iter->seq;
1460 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1463 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1466 if (cpumask_test_cpu(iter->cpu, iter->started))
1469 cpumask_set_cpu(iter->cpu, iter->started);
1470 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1473 static enum print_line_t print_lat_fmt(struct trace_iterator *iter)
1475 struct trace_seq *s = &iter->seq;
1476 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1477 struct trace_event *event;
1478 struct trace_entry *entry = iter->ent;
1480 test_cpu_buff_start(iter);
1482 event = ftrace_find_event(entry->type);
1484 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1485 if (!trace_print_lat_context(iter))
1490 return event->latency_trace(iter, sym_flags);
1492 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1495 return TRACE_TYPE_HANDLED;
1497 return TRACE_TYPE_PARTIAL_LINE;
1500 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1502 struct trace_seq *s = &iter->seq;
1503 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1504 struct trace_entry *entry;
1505 struct trace_event *event;
1509 test_cpu_buff_start(iter);
1511 event = ftrace_find_event(entry->type);
1513 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1514 if (!trace_print_context(iter))
1519 return event->trace(iter, sym_flags);
1521 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1524 return TRACE_TYPE_HANDLED;
1526 return TRACE_TYPE_PARTIAL_LINE;
1529 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1531 struct trace_seq *s = &iter->seq;
1532 struct trace_entry *entry;
1533 struct trace_event *event;
1537 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1538 if (!trace_seq_printf(s, "%d %d %llu ",
1539 entry->pid, iter->cpu, iter->ts))
1543 event = ftrace_find_event(entry->type);
1545 return event->raw(iter, 0);
1547 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1550 return TRACE_TYPE_HANDLED;
1552 return TRACE_TYPE_PARTIAL_LINE;
1555 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1557 struct trace_seq *s = &iter->seq;
1558 unsigned char newline = '\n';
1559 struct trace_entry *entry;
1560 struct trace_event *event;
1564 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1565 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1566 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1567 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1570 event = ftrace_find_event(entry->type);
1572 enum print_line_t ret = event->hex(iter, 0);
1573 if (ret != TRACE_TYPE_HANDLED)
1577 SEQ_PUT_FIELD_RET(s, newline);
1579 return TRACE_TYPE_HANDLED;
1582 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1584 struct trace_seq *s = &iter->seq;
1585 struct trace_entry *entry = iter->ent;
1586 struct print_entry *field;
1589 trace_assign_type(field, entry);
1591 ret = trace_seq_printf(s, "%s", field->buf);
1593 return TRACE_TYPE_PARTIAL_LINE;
1595 return TRACE_TYPE_HANDLED;
1598 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1600 struct trace_seq *s = &iter->seq;
1601 struct trace_entry *entry;
1602 struct trace_event *event;
1606 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1607 SEQ_PUT_FIELD_RET(s, entry->pid);
1608 SEQ_PUT_FIELD_RET(s, iter->cpu);
1609 SEQ_PUT_FIELD_RET(s, iter->ts);
1612 event = ftrace_find_event(entry->type);
1613 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1616 static int trace_empty(struct trace_iterator *iter)
1620 for_each_tracing_cpu(cpu) {
1621 if (iter->buffer_iter[cpu]) {
1622 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1625 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1633 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1635 enum print_line_t ret;
1637 if (iter->trace && iter->trace->print_line) {
1638 ret = iter->trace->print_line(iter);
1639 if (ret != TRACE_TYPE_UNHANDLED)
1643 if (iter->ent->type == TRACE_PRINT &&
1644 trace_flags & TRACE_ITER_PRINTK &&
1645 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1646 return print_printk_msg_only(iter);
1648 if (trace_flags & TRACE_ITER_BIN)
1649 return print_bin_fmt(iter);
1651 if (trace_flags & TRACE_ITER_HEX)
1652 return print_hex_fmt(iter);
1654 if (trace_flags & TRACE_ITER_RAW)
1655 return print_raw_fmt(iter);
1657 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1658 return print_lat_fmt(iter);
1660 return print_trace_fmt(iter);
1663 static int s_show(struct seq_file *m, void *v)
1665 struct trace_iterator *iter = v;
1667 if (iter->ent == NULL) {
1669 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1672 if (iter->trace && iter->trace->print_header)
1673 iter->trace->print_header(m);
1674 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1675 /* print nothing if the buffers are empty */
1676 if (trace_empty(iter))
1678 print_trace_header(m, iter);
1679 if (!(trace_flags & TRACE_ITER_VERBOSE))
1680 print_lat_help_header(m);
1682 if (!(trace_flags & TRACE_ITER_VERBOSE))
1683 print_func_help_header(m);
1686 print_trace_line(iter);
1687 trace_print_seq(m, &iter->seq);
1693 static struct seq_operations tracer_seq_ops = {
1700 static struct trace_iterator *
1701 __tracing_open(struct inode *inode, struct file *file)
1703 long cpu_file = (long) inode->i_private;
1704 void *fail_ret = ERR_PTR(-ENOMEM);
1705 struct trace_iterator *iter;
1709 if (tracing_disabled)
1710 return ERR_PTR(-ENODEV);
1712 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1714 return ERR_PTR(-ENOMEM);
1717 * We make a copy of the current tracer to avoid concurrent
1718 * changes on it while we are reading.
1720 mutex_lock(&trace_types_lock);
1721 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1726 *iter->trace = *current_trace;
1728 if (current_trace && current_trace->print_max)
1731 iter->tr = &global_trace;
1733 mutex_init(&iter->mutex);
1734 iter->cpu_file = cpu_file;
1736 /* Notify the tracer early; before we stop tracing. */
1737 if (iter->trace && iter->trace->open)
1738 iter->trace->open(iter);
1740 /* Annotate start of buffers if we had overruns */
1741 if (ring_buffer_overruns(iter->tr->buffer))
1742 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1744 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1745 for_each_tracing_cpu(cpu) {
1747 iter->buffer_iter[cpu] =
1748 ring_buffer_read_start(iter->tr->buffer, cpu);
1750 if (!iter->buffer_iter[cpu])
1754 cpu = iter->cpu_file;
1755 iter->buffer_iter[cpu] =
1756 ring_buffer_read_start(iter->tr->buffer, cpu);
1758 if (!iter->buffer_iter[cpu])
1762 /* TODO stop tracer */
1763 ret = seq_open(file, &tracer_seq_ops);
1765 fail_ret = ERR_PTR(ret);
1769 m = file->private_data;
1772 /* stop the trace while dumping */
1775 mutex_unlock(&trace_types_lock);
1780 for_each_tracing_cpu(cpu) {
1781 if (iter->buffer_iter[cpu])
1782 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1785 mutex_unlock(&trace_types_lock);
1792 int tracing_open_generic(struct inode *inode, struct file *filp)
1794 if (tracing_disabled)
1797 filp->private_data = inode->i_private;
1801 static int tracing_release(struct inode *inode, struct file *file)
1803 struct seq_file *m = (struct seq_file *)file->private_data;
1804 struct trace_iterator *iter = m->private;
1807 mutex_lock(&trace_types_lock);
1808 for_each_tracing_cpu(cpu) {
1809 if (iter->buffer_iter[cpu])
1810 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1813 if (iter->trace && iter->trace->close)
1814 iter->trace->close(iter);
1816 /* reenable tracing if it was previously enabled */
1818 mutex_unlock(&trace_types_lock);
1820 seq_release(inode, file);
1821 mutex_destroy(&iter->mutex);
1827 static int tracing_open(struct inode *inode, struct file *file)
1829 struct trace_iterator *iter;
1832 iter = __tracing_open(inode, file);
1834 ret = PTR_ERR(iter);
1839 static int tracing_lt_open(struct inode *inode, struct file *file)
1841 struct trace_iterator *iter;
1844 iter = __tracing_open(inode, file);
1847 ret = PTR_ERR(iter);
1849 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1856 t_next(struct seq_file *m, void *v, loff_t *pos)
1858 struct tracer *t = m->private;
1870 static void *t_start(struct seq_file *m, loff_t *pos)
1872 struct tracer *t = m->private;
1875 mutex_lock(&trace_types_lock);
1876 for (; t && l < *pos; t = t_next(m, t, &l))
1882 static void t_stop(struct seq_file *m, void *p)
1884 mutex_unlock(&trace_types_lock);
1887 static int t_show(struct seq_file *m, void *v)
1889 struct tracer *t = v;
1894 seq_printf(m, "%s", t->name);
1903 static struct seq_operations show_traces_seq_ops = {
1910 static int show_traces_open(struct inode *inode, struct file *file)
1914 if (tracing_disabled)
1917 ret = seq_open(file, &show_traces_seq_ops);
1919 struct seq_file *m = file->private_data;
1920 m->private = trace_types;
1926 static struct file_operations tracing_fops = {
1927 .open = tracing_open,
1929 .llseek = seq_lseek,
1930 .release = tracing_release,
1933 static struct file_operations tracing_lt_fops = {
1934 .open = tracing_lt_open,
1936 .llseek = seq_lseek,
1937 .release = tracing_release,
1940 static struct file_operations show_traces_fops = {
1941 .open = show_traces_open,
1943 .release = seq_release,
1947 * Only trace on a CPU if the bitmask is set:
1949 static cpumask_var_t tracing_cpumask;
1952 * The tracer itself will not take this lock, but still we want
1953 * to provide a consistent cpumask to user-space:
1955 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1958 * Temporary storage for the character representation of the
1959 * CPU bitmask (and one more byte for the newline):
1961 static char mask_str[NR_CPUS + 1];
1964 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1965 size_t count, loff_t *ppos)
1969 mutex_lock(&tracing_cpumask_update_lock);
1971 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1972 if (count - len < 2) {
1976 len += sprintf(mask_str + len, "\n");
1977 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1980 mutex_unlock(&tracing_cpumask_update_lock);
1986 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1987 size_t count, loff_t *ppos)
1990 cpumask_var_t tracing_cpumask_new;
1992 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1995 mutex_lock(&tracing_cpumask_update_lock);
1996 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2000 local_irq_disable();
2001 __raw_spin_lock(&ftrace_max_lock);
2002 for_each_tracing_cpu(cpu) {
2004 * Increase/decrease the disabled counter if we are
2005 * about to flip a bit in the cpumask:
2007 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2008 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2009 atomic_inc(&global_trace.data[cpu]->disabled);
2011 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2012 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2013 atomic_dec(&global_trace.data[cpu]->disabled);
2016 __raw_spin_unlock(&ftrace_max_lock);
2019 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2021 mutex_unlock(&tracing_cpumask_update_lock);
2022 free_cpumask_var(tracing_cpumask_new);
2027 mutex_unlock(&tracing_cpumask_update_lock);
2028 free_cpumask_var(tracing_cpumask);
2033 static struct file_operations tracing_cpumask_fops = {
2034 .open = tracing_open_generic,
2035 .read = tracing_cpumask_read,
2036 .write = tracing_cpumask_write,
2040 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2041 size_t cnt, loff_t *ppos)
2043 struct tracer_opt *trace_opts;
2051 /* calculate max size */
2052 for (i = 0; trace_options[i]; i++) {
2053 len += strlen(trace_options[i]);
2054 len += 3; /* "no" and newline */
2057 mutex_lock(&trace_types_lock);
2058 tracer_flags = current_trace->flags->val;
2059 trace_opts = current_trace->flags->opts;
2062 * Increase the size with names of options specific
2063 * of the current tracer.
2065 for (i = 0; trace_opts[i].name; i++) {
2066 len += strlen(trace_opts[i].name);
2067 len += 3; /* "no" and newline */
2070 /* +2 for \n and \0 */
2071 buf = kmalloc(len + 2, GFP_KERNEL);
2073 mutex_unlock(&trace_types_lock);
2077 for (i = 0; trace_options[i]; i++) {
2078 if (trace_flags & (1 << i))
2079 r += sprintf(buf + r, "%s\n", trace_options[i]);
2081 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2084 for (i = 0; trace_opts[i].name; i++) {
2085 if (tracer_flags & trace_opts[i].bit)
2086 r += sprintf(buf + r, "%s\n",
2087 trace_opts[i].name);
2089 r += sprintf(buf + r, "no%s\n",
2090 trace_opts[i].name);
2092 mutex_unlock(&trace_types_lock);
2094 WARN_ON(r >= len + 2);
2096 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2102 /* Try to assign a tracer specific option */
2103 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2105 struct tracer_flags *trace_flags = trace->flags;
2106 struct tracer_opt *opts = NULL;
2110 for (i = 0; trace_flags->opts[i].name; i++) {
2111 opts = &trace_flags->opts[i];
2112 len = strlen(opts->name);
2114 if (strncmp(cmp, opts->name, len) == 0) {
2115 ret = trace->set_flag(trace_flags->val,
2121 if (!trace_flags->opts[i].name)
2124 /* Refused to handle */
2129 trace_flags->val &= ~opts->bit;
2131 trace_flags->val |= opts->bit;
2137 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2138 size_t cnt, loff_t *ppos)
2146 if (cnt >= sizeof(buf))
2149 if (copy_from_user(&buf, ubuf, cnt))
2154 if (strncmp(buf, "no", 2) == 0) {
2159 for (i = 0; trace_options[i]; i++) {
2160 int len = strlen(trace_options[i]);
2162 if (strncmp(cmp, trace_options[i], len) == 0) {
2164 trace_flags &= ~(1 << i);
2166 trace_flags |= (1 << i);
2171 /* If no option could be set, test the specific tracer options */
2172 if (!trace_options[i]) {
2173 mutex_lock(&trace_types_lock);
2174 ret = set_tracer_option(current_trace, cmp, neg);
2175 mutex_unlock(&trace_types_lock);
2185 static struct file_operations tracing_iter_fops = {
2186 .open = tracing_open_generic,
2187 .read = tracing_trace_options_read,
2188 .write = tracing_trace_options_write,
2191 static const char readme_msg[] =
2192 "tracing mini-HOWTO:\n\n"
2194 "# mount -t debugfs nodev /debug\n\n"
2195 "# cat /debug/tracing/available_tracers\n"
2196 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2197 "# cat /debug/tracing/current_tracer\n"
2199 "# echo sched_switch > /debug/tracing/current_tracer\n"
2200 "# cat /debug/tracing/current_tracer\n"
2202 "# cat /debug/tracing/trace_options\n"
2203 "noprint-parent nosym-offset nosym-addr noverbose\n"
2204 "# echo print-parent > /debug/tracing/trace_options\n"
2205 "# echo 1 > /debug/tracing/tracing_enabled\n"
2206 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2207 "echo 0 > /debug/tracing/tracing_enabled\n"
2211 tracing_readme_read(struct file *filp, char __user *ubuf,
2212 size_t cnt, loff_t *ppos)
2214 return simple_read_from_buffer(ubuf, cnt, ppos,
2215 readme_msg, strlen(readme_msg));
2218 static struct file_operations tracing_readme_fops = {
2219 .open = tracing_open_generic,
2220 .read = tracing_readme_read,
2224 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2225 size_t cnt, loff_t *ppos)
2230 r = sprintf(buf, "%u\n", tracer_enabled);
2231 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2235 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2236 size_t cnt, loff_t *ppos)
2238 struct trace_array *tr = filp->private_data;
2243 if (cnt >= sizeof(buf))
2246 if (copy_from_user(&buf, ubuf, cnt))
2251 ret = strict_strtoul(buf, 10, &val);
2257 mutex_lock(&trace_types_lock);
2258 if (tracer_enabled ^ val) {
2261 if (current_trace->start)
2262 current_trace->start(tr);
2267 if (current_trace->stop)
2268 current_trace->stop(tr);
2271 mutex_unlock(&trace_types_lock);
2279 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2280 size_t cnt, loff_t *ppos)
2282 char buf[max_tracer_type_len+2];
2285 mutex_lock(&trace_types_lock);
2287 r = sprintf(buf, "%s\n", current_trace->name);
2289 r = sprintf(buf, "\n");
2290 mutex_unlock(&trace_types_lock);
2292 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2295 int tracer_init(struct tracer *t, struct trace_array *tr)
2297 tracing_reset_online_cpus(tr);
2301 struct trace_option_dentry;
2303 static struct trace_option_dentry *
2304 create_trace_option_files(struct tracer *tracer);
2307 destroy_trace_option_files(struct trace_option_dentry *topts);
2309 static int tracing_set_tracer(const char *buf)
2311 static struct trace_option_dentry *topts;
2312 struct trace_array *tr = &global_trace;
2316 mutex_lock(&trace_types_lock);
2317 for (t = trace_types; t; t = t->next) {
2318 if (strcmp(t->name, buf) == 0)
2325 if (t == current_trace)
2328 trace_branch_disable();
2329 if (current_trace && current_trace->reset)
2330 current_trace->reset(tr);
2332 destroy_trace_option_files(topts);
2336 topts = create_trace_option_files(current_trace);
2339 ret = tracer_init(t, tr);
2344 trace_branch_enable(tr);
2346 mutex_unlock(&trace_types_lock);
2352 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2353 size_t cnt, loff_t *ppos)
2355 char buf[max_tracer_type_len+1];
2362 if (cnt > max_tracer_type_len)
2363 cnt = max_tracer_type_len;
2365 if (copy_from_user(&buf, ubuf, cnt))
2370 /* strip ending whitespace. */
2371 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2374 err = tracing_set_tracer(buf);
2384 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2385 size_t cnt, loff_t *ppos)
2387 unsigned long *ptr = filp->private_data;
2391 r = snprintf(buf, sizeof(buf), "%ld\n",
2392 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2393 if (r > sizeof(buf))
2395 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2399 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2400 size_t cnt, loff_t *ppos)
2402 unsigned long *ptr = filp->private_data;
2407 if (cnt >= sizeof(buf))
2410 if (copy_from_user(&buf, ubuf, cnt))
2415 ret = strict_strtoul(buf, 10, &val);
2424 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2426 long cpu_file = (long) inode->i_private;
2427 struct trace_iterator *iter;
2430 if (tracing_disabled)
2433 mutex_lock(&trace_types_lock);
2435 /* We only allow one reader per cpu */
2436 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2437 if (!cpumask_empty(tracing_reader_cpumask)) {
2441 cpumask_setall(tracing_reader_cpumask);
2443 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2444 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2451 /* create a buffer to store the information to pass to userspace */
2452 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2459 * We make a copy of the current tracer to avoid concurrent
2460 * changes on it while we are reading.
2462 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2468 *iter->trace = *current_trace;
2470 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2475 /* trace pipe does not show start of buffer */
2476 cpumask_setall(iter->started);
2478 iter->cpu_file = cpu_file;
2479 iter->tr = &global_trace;
2480 mutex_init(&iter->mutex);
2481 filp->private_data = iter;
2483 if (iter->trace->pipe_open)
2484 iter->trace->pipe_open(iter);
2487 mutex_unlock(&trace_types_lock);
2493 mutex_unlock(&trace_types_lock);
2497 static int tracing_release_pipe(struct inode *inode, struct file *file)
2499 struct trace_iterator *iter = file->private_data;
2501 mutex_lock(&trace_types_lock);
2503 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2504 cpumask_clear(tracing_reader_cpumask);
2506 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2508 mutex_unlock(&trace_types_lock);
2510 free_cpumask_var(iter->started);
2511 mutex_destroy(&iter->mutex);
2519 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2521 struct trace_iterator *iter = filp->private_data;
2523 if (trace_flags & TRACE_ITER_BLOCK) {
2525 * Always select as readable when in blocking mode
2527 return POLLIN | POLLRDNORM;
2529 if (!trace_empty(iter))
2530 return POLLIN | POLLRDNORM;
2531 poll_wait(filp, &trace_wait, poll_table);
2532 if (!trace_empty(iter))
2533 return POLLIN | POLLRDNORM;
2540 void default_wait_pipe(struct trace_iterator *iter)
2544 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2546 if (trace_empty(iter))
2549 finish_wait(&trace_wait, &wait);
2553 * This is a make-shift waitqueue.
2554 * A tracer might use this callback on some rare cases:
2556 * 1) the current tracer might hold the runqueue lock when it wakes up
2557 * a reader, hence a deadlock (sched, function, and function graph tracers)
2558 * 2) the function tracers, trace all functions, we don't want
2559 * the overhead of calling wake_up and friends
2560 * (and tracing them too)
2562 * Anyway, this is really very primitive wakeup.
2564 void poll_wait_pipe(struct trace_iterator *iter)
2566 set_current_state(TASK_INTERRUPTIBLE);
2567 /* sleep for 100 msecs, and try again. */
2568 schedule_timeout(HZ / 10);
2571 /* Must be called with trace_types_lock mutex held. */
2572 static int tracing_wait_pipe(struct file *filp)
2574 struct trace_iterator *iter = filp->private_data;
2576 while (trace_empty(iter)) {
2578 if ((filp->f_flags & O_NONBLOCK)) {
2582 mutex_unlock(&iter->mutex);
2584 iter->trace->wait_pipe(iter);
2586 mutex_lock(&iter->mutex);
2588 if (signal_pending(current))
2592 * We block until we read something and tracing is disabled.
2593 * We still block if tracing is disabled, but we have never
2594 * read anything. This allows a user to cat this file, and
2595 * then enable tracing. But after we have read something,
2596 * we give an EOF when tracing is again disabled.
2598 * iter->pos will be 0 if we haven't read anything.
2600 if (!tracer_enabled && iter->pos)
2611 tracing_read_pipe(struct file *filp, char __user *ubuf,
2612 size_t cnt, loff_t *ppos)
2614 struct trace_iterator *iter = filp->private_data;
2615 static struct tracer *old_tracer;
2618 /* return any leftover data */
2619 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2623 trace_seq_reset(&iter->seq);
2625 /* copy the tracer to avoid using a global lock all around */
2626 mutex_lock(&trace_types_lock);
2627 if (unlikely(old_tracer != current_trace && current_trace)) {
2628 old_tracer = current_trace;
2629 *iter->trace = *current_trace;
2631 mutex_unlock(&trace_types_lock);
2634 * Avoid more than one consumer on a single file descriptor
2635 * This is just a matter of traces coherency, the ring buffer itself
2638 mutex_lock(&iter->mutex);
2639 if (iter->trace->read) {
2640 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2646 sret = tracing_wait_pipe(filp);
2650 /* stop when tracing is finished */
2651 if (trace_empty(iter)) {
2656 if (cnt >= PAGE_SIZE)
2657 cnt = PAGE_SIZE - 1;
2659 /* reset all but tr, trace, and overruns */
2660 memset(&iter->seq, 0,
2661 sizeof(struct trace_iterator) -
2662 offsetof(struct trace_iterator, seq));
2665 while (find_next_entry_inc(iter) != NULL) {
2666 enum print_line_t ret;
2667 int len = iter->seq.len;
2669 ret = print_trace_line(iter);
2670 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2671 /* don't print partial lines */
2672 iter->seq.len = len;
2675 if (ret != TRACE_TYPE_NO_CONSUME)
2676 trace_consume(iter);
2678 if (iter->seq.len >= cnt)
2682 /* Now copy what we have to the user */
2683 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2684 if (iter->seq.readpos >= iter->seq.len)
2685 trace_seq_reset(&iter->seq);
2688 * If there was nothing to send to user, inspite of consuming trace
2689 * entries, go back to wait for more entries.
2695 mutex_unlock(&iter->mutex);
2700 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2701 struct pipe_buffer *buf)
2703 __free_page(buf->page);
2706 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2709 __free_page(spd->pages[idx]);
2712 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2714 .map = generic_pipe_buf_map,
2715 .unmap = generic_pipe_buf_unmap,
2716 .confirm = generic_pipe_buf_confirm,
2717 .release = tracing_pipe_buf_release,
2718 .steal = generic_pipe_buf_steal,
2719 .get = generic_pipe_buf_get,
2723 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2728 /* Seq buffer is page-sized, exactly what we need. */
2730 count = iter->seq.len;
2731 ret = print_trace_line(iter);
2732 count = iter->seq.len - count;
2735 iter->seq.len -= count;
2738 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2739 iter->seq.len -= count;
2743 trace_consume(iter);
2745 if (!find_next_entry_inc(iter)) {
2755 static ssize_t tracing_splice_read_pipe(struct file *filp,
2757 struct pipe_inode_info *pipe,
2761 struct page *pages[PIPE_BUFFERS];
2762 struct partial_page partial[PIPE_BUFFERS];
2763 struct trace_iterator *iter = filp->private_data;
2764 struct splice_pipe_desc spd = {
2767 .nr_pages = 0, /* This gets updated below. */
2769 .ops = &tracing_pipe_buf_ops,
2770 .spd_release = tracing_spd_release_pipe,
2772 static struct tracer *old_tracer;
2777 /* copy the tracer to avoid using a global lock all around */
2778 mutex_lock(&trace_types_lock);
2779 if (unlikely(old_tracer != current_trace && current_trace)) {
2780 old_tracer = current_trace;
2781 *iter->trace = *current_trace;
2783 mutex_unlock(&trace_types_lock);
2785 mutex_lock(&iter->mutex);
2787 if (iter->trace->splice_read) {
2788 ret = iter->trace->splice_read(iter, filp,
2789 ppos, pipe, len, flags);
2794 ret = tracing_wait_pipe(filp);
2798 if (!iter->ent && !find_next_entry_inc(iter)) {
2803 /* Fill as many pages as possible. */
2804 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2805 pages[i] = alloc_page(GFP_KERNEL);
2809 rem = tracing_fill_pipe_page(rem, iter);
2811 /* Copy the data into the page, so we can start over. */
2812 ret = trace_seq_to_buffer(&iter->seq,
2813 page_address(pages[i]),
2816 __free_page(pages[i]);
2819 partial[i].offset = 0;
2820 partial[i].len = iter->seq.len;
2822 trace_seq_reset(&iter->seq);
2825 mutex_unlock(&iter->mutex);
2829 return splice_to_pipe(pipe, &spd);
2832 mutex_unlock(&iter->mutex);
2838 tracing_entries_read(struct file *filp, char __user *ubuf,
2839 size_t cnt, loff_t *ppos)
2841 struct trace_array *tr = filp->private_data;
2845 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2846 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2850 tracing_entries_write(struct file *filp, const char __user *ubuf,
2851 size_t cnt, loff_t *ppos)
2857 if (cnt >= sizeof(buf))
2860 if (copy_from_user(&buf, ubuf, cnt))
2865 ret = strict_strtoul(buf, 10, &val);
2869 /* must have at least 1 entry */
2873 mutex_lock(&trace_types_lock);
2877 /* disable all cpu buffers */
2878 for_each_tracing_cpu(cpu) {
2879 if (global_trace.data[cpu])
2880 atomic_inc(&global_trace.data[cpu]->disabled);
2881 if (max_tr.data[cpu])
2882 atomic_inc(&max_tr.data[cpu]->disabled);
2885 /* value is in KB */
2888 if (val != global_trace.entries) {
2889 ret = ring_buffer_resize(global_trace.buffer, val);
2895 ret = ring_buffer_resize(max_tr.buffer, val);
2899 r = ring_buffer_resize(global_trace.buffer,
2900 global_trace.entries);
2902 /* AARGH! We are left with different
2903 * size max buffer!!!! */
2905 tracing_disabled = 1;
2910 global_trace.entries = val;
2915 /* If check pages failed, return ENOMEM */
2916 if (tracing_disabled)
2919 for_each_tracing_cpu(cpu) {
2920 if (global_trace.data[cpu])
2921 atomic_dec(&global_trace.data[cpu]->disabled);
2922 if (max_tr.data[cpu])
2923 atomic_dec(&max_tr.data[cpu]->disabled);
2927 max_tr.entries = global_trace.entries;
2928 mutex_unlock(&trace_types_lock);
2933 static int mark_printk(const char *fmt, ...)
2937 va_start(args, fmt);
2938 ret = trace_vprintk(0, -1, fmt, args);
2944 tracing_mark_write(struct file *filp, const char __user *ubuf,
2945 size_t cnt, loff_t *fpos)
2950 if (tracing_disabled)
2953 if (cnt > TRACE_BUF_SIZE)
2954 cnt = TRACE_BUF_SIZE;
2956 buf = kmalloc(cnt + 1, GFP_KERNEL);
2960 if (copy_from_user(buf, ubuf, cnt)) {
2965 /* Cut from the first nil or newline. */
2967 end = strchr(buf, '\n');
2971 cnt = mark_printk("%s\n", buf);
2978 static struct file_operations tracing_max_lat_fops = {
2979 .open = tracing_open_generic,
2980 .read = tracing_max_lat_read,
2981 .write = tracing_max_lat_write,
2984 static struct file_operations tracing_ctrl_fops = {
2985 .open = tracing_open_generic,
2986 .read = tracing_ctrl_read,
2987 .write = tracing_ctrl_write,
2990 static struct file_operations set_tracer_fops = {
2991 .open = tracing_open_generic,
2992 .read = tracing_set_trace_read,
2993 .write = tracing_set_trace_write,
2996 static struct file_operations tracing_pipe_fops = {
2997 .open = tracing_open_pipe,
2998 .poll = tracing_poll_pipe,
2999 .read = tracing_read_pipe,
3000 .splice_read = tracing_splice_read_pipe,
3001 .release = tracing_release_pipe,
3004 static struct file_operations tracing_entries_fops = {
3005 .open = tracing_open_generic,
3006 .read = tracing_entries_read,
3007 .write = tracing_entries_write,
3010 static struct file_operations tracing_mark_fops = {
3011 .open = tracing_open_generic,
3012 .write = tracing_mark_write,
3015 #ifdef CONFIG_DYNAMIC_FTRACE
3017 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3023 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3024 size_t cnt, loff_t *ppos)
3026 static char ftrace_dyn_info_buffer[1024];
3027 static DEFINE_MUTEX(dyn_info_mutex);
3028 unsigned long *p = filp->private_data;
3029 char *buf = ftrace_dyn_info_buffer;
3030 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3033 mutex_lock(&dyn_info_mutex);
3034 r = sprintf(buf, "%ld ", *p);
3036 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3039 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3041 mutex_unlock(&dyn_info_mutex);
3046 static struct file_operations tracing_dyn_info_fops = {
3047 .open = tracing_open_generic,
3048 .read = tracing_read_dyn_info,
3052 static struct dentry *d_tracer;
3054 struct dentry *tracing_init_dentry(void)
3061 d_tracer = debugfs_create_dir("tracing", NULL);
3063 if (!d_tracer && !once) {
3065 pr_warning("Could not create debugfs directory 'tracing'\n");
3072 static struct dentry *d_percpu;
3074 struct dentry *tracing_dentry_percpu(void)
3077 struct dentry *d_tracer;
3082 d_tracer = tracing_init_dentry();
3087 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3089 if (!d_percpu && !once) {
3091 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3098 static void tracing_init_debugfs_percpu(long cpu)
3100 struct dentry *d_percpu = tracing_dentry_percpu();
3101 struct dentry *entry, *d_cpu;
3102 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3105 if (cpu > 999 || cpu < 0)
3108 sprintf(cpu_dir, "cpu%ld", cpu);
3109 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3111 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3115 /* per cpu trace_pipe */
3116 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
3117 (void *) cpu, &tracing_pipe_fops);
3119 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3122 entry = debugfs_create_file("trace", 0444, d_cpu,
3123 (void *) cpu, &tracing_fops);
3125 pr_warning("Could not create debugfs 'trace' entry\n");
3128 #ifdef CONFIG_FTRACE_SELFTEST
3129 /* Let selftest have access to static functions in this file */
3130 #include "trace_selftest.c"
3133 struct trace_option_dentry {
3134 struct tracer_opt *opt;
3135 struct tracer_flags *flags;
3136 struct dentry *entry;
3140 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3143 struct trace_option_dentry *topt = filp->private_data;
3146 if (topt->flags->val & topt->opt->bit)
3151 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3155 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3158 struct trace_option_dentry *topt = filp->private_data;
3163 if (cnt >= sizeof(buf))
3166 if (copy_from_user(&buf, ubuf, cnt))
3171 ret = strict_strtoul(buf, 10, &val);
3178 /* do nothing if already cleared */
3179 if (!(topt->flags->val & topt->opt->bit))
3182 mutex_lock(&trace_types_lock);
3183 if (current_trace->set_flag)
3184 ret = current_trace->set_flag(topt->flags->val,
3186 mutex_unlock(&trace_types_lock);
3189 topt->flags->val &= ~topt->opt->bit;
3192 /* do nothing if already set */
3193 if (topt->flags->val & topt->opt->bit)
3196 mutex_lock(&trace_types_lock);
3197 if (current_trace->set_flag)
3198 ret = current_trace->set_flag(topt->flags->val,
3200 mutex_unlock(&trace_types_lock);
3203 topt->flags->val |= topt->opt->bit;
3216 static const struct file_operations trace_options_fops = {
3217 .open = tracing_open_generic,
3218 .read = trace_options_read,
3219 .write = trace_options_write,
3223 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3226 long index = (long)filp->private_data;
3229 if (trace_flags & (1 << index))
3234 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3238 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3241 long index = (long)filp->private_data;
3246 if (cnt >= sizeof(buf))
3249 if (copy_from_user(&buf, ubuf, cnt))
3254 ret = strict_strtoul(buf, 10, &val);
3260 trace_flags &= ~(1 << index);
3263 trace_flags |= 1 << index;
3275 static const struct file_operations trace_options_core_fops = {
3276 .open = tracing_open_generic,
3277 .read = trace_options_core_read,
3278 .write = trace_options_core_write,
3281 static struct dentry *trace_options_init_dentry(void)
3283 struct dentry *d_tracer;
3284 static struct dentry *t_options;
3289 d_tracer = tracing_init_dentry();
3293 t_options = debugfs_create_dir("options", d_tracer);
3295 pr_warning("Could not create debugfs directory 'options'\n");
3303 create_trace_option_file(struct trace_option_dentry *topt,
3304 struct tracer_flags *flags,
3305 struct tracer_opt *opt)
3307 struct dentry *t_options;
3308 struct dentry *entry;
3310 t_options = trace_options_init_dentry();
3314 topt->flags = flags;
3317 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3318 &trace_options_fops);
3320 topt->entry = entry;
3324 static struct trace_option_dentry *
3325 create_trace_option_files(struct tracer *tracer)
3327 struct trace_option_dentry *topts;
3328 struct tracer_flags *flags;
3329 struct tracer_opt *opts;
3335 flags = tracer->flags;
3337 if (!flags || !flags->opts)
3342 for (cnt = 0; opts[cnt].name; cnt++)
3345 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3349 for (cnt = 0; opts[cnt].name; cnt++)
3350 create_trace_option_file(&topts[cnt], flags,
3357 destroy_trace_option_files(struct trace_option_dentry *topts)
3364 for (cnt = 0; topts[cnt].opt; cnt++) {
3365 if (topts[cnt].entry)
3366 debugfs_remove(topts[cnt].entry);
3372 static struct dentry *
3373 create_trace_option_core_file(const char *option, long index)
3375 struct dentry *t_options;
3376 struct dentry *entry;
3378 t_options = trace_options_init_dentry();
3382 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3383 &trace_options_core_fops);
3388 static __init void create_trace_options_dir(void)
3390 struct dentry *t_options;
3391 struct dentry *entry;
3394 t_options = trace_options_init_dentry();
3398 for (i = 0; trace_options[i]; i++) {
3399 entry = create_trace_option_core_file(trace_options[i], i);
3401 pr_warning("Could not create debugfs %s entry\n",
3406 static __init int tracer_init_debugfs(void)
3408 struct dentry *d_tracer;
3409 struct dentry *entry;
3412 d_tracer = tracing_init_dentry();
3414 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3415 &global_trace, &tracing_ctrl_fops);
3417 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3419 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3420 NULL, &tracing_iter_fops);
3422 pr_warning("Could not create debugfs 'trace_options' entry\n");
3424 create_trace_options_dir();
3426 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3427 NULL, &tracing_cpumask_fops);
3429 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3431 entry = debugfs_create_file("trace", 0444, d_tracer,
3432 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3434 pr_warning("Could not create debugfs 'trace' entry\n");
3436 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3437 &global_trace, &show_traces_fops);
3439 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3441 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3442 &global_trace, &set_tracer_fops);
3444 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3446 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3447 &tracing_max_latency,
3448 &tracing_max_lat_fops);
3450 pr_warning("Could not create debugfs "
3451 "'tracing_max_latency' entry\n");
3453 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3454 &tracing_thresh, &tracing_max_lat_fops);
3456 pr_warning("Could not create debugfs "
3457 "'tracing_thresh' entry\n");
3458 entry = debugfs_create_file("README", 0644, d_tracer,
3459 NULL, &tracing_readme_fops);
3461 pr_warning("Could not create debugfs 'README' entry\n");
3463 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3464 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3466 pr_warning("Could not create debugfs "
3467 "'trace_pipe' entry\n");
3469 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3470 &global_trace, &tracing_entries_fops);
3472 pr_warning("Could not create debugfs "
3473 "'buffer_size_kb' entry\n");
3475 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3476 NULL, &tracing_mark_fops);
3478 pr_warning("Could not create debugfs "
3479 "'trace_marker' entry\n");
3481 #ifdef CONFIG_DYNAMIC_FTRACE
3482 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3483 &ftrace_update_tot_cnt,
3484 &tracing_dyn_info_fops);
3486 pr_warning("Could not create debugfs "
3487 "'dyn_ftrace_total_info' entry\n");
3489 #ifdef CONFIG_SYSPROF_TRACER
3490 init_tracer_sysprof_debugfs(d_tracer);
3493 for_each_tracing_cpu(cpu)
3494 tracing_init_debugfs_percpu(cpu);
3499 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
3501 static DEFINE_SPINLOCK(trace_buf_lock);
3502 static char trace_buf[TRACE_BUF_SIZE];
3504 struct ring_buffer_event *event;
3505 struct trace_array *tr = &global_trace;
3506 struct trace_array_cpu *data;
3507 int cpu, len = 0, size, pc;
3508 struct print_entry *entry;
3509 unsigned long irq_flags;
3511 if (tracing_disabled || tracing_selftest_running)
3514 pc = preempt_count();
3515 preempt_disable_notrace();
3516 cpu = raw_smp_processor_id();
3517 data = tr->data[cpu];
3519 if (unlikely(atomic_read(&data->disabled)))
3522 pause_graph_tracing();
3523 spin_lock_irqsave(&trace_buf_lock, irq_flags);
3524 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3526 len = min(len, TRACE_BUF_SIZE-1);
3529 size = sizeof(*entry) + len + 1;
3530 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
3533 entry = ring_buffer_event_data(event);
3535 entry->depth = depth;
3537 memcpy(&entry->buf, trace_buf, len);
3538 entry->buf[len] = 0;
3539 ring_buffer_unlock_commit(tr->buffer, event);
3542 spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
3543 unpause_graph_tracing();
3545 preempt_enable_notrace();
3549 EXPORT_SYMBOL_GPL(trace_vprintk);
3551 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3556 if (!(trace_flags & TRACE_ITER_PRINTK))
3560 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3564 EXPORT_SYMBOL_GPL(__ftrace_printk);
3566 int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
3568 if (!(trace_flags & TRACE_ITER_PRINTK))
3571 return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3573 EXPORT_SYMBOL_GPL(__ftrace_vprintk);
3575 static int trace_panic_handler(struct notifier_block *this,
3576 unsigned long event, void *unused)
3578 if (ftrace_dump_on_oops)
3583 static struct notifier_block trace_panic_notifier = {
3584 .notifier_call = trace_panic_handler,
3586 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3589 static int trace_die_handler(struct notifier_block *self,
3595 if (ftrace_dump_on_oops)
3604 static struct notifier_block trace_die_notifier = {
3605 .notifier_call = trace_die_handler,
3610 * printk is set to max of 1024, we really don't need it that big.
3611 * Nothing should be printing 1000 characters anyway.
3613 #define TRACE_MAX_PRINT 1000
3616 * Define here KERN_TRACE so that we have one place to modify
3617 * it if we decide to change what log level the ftrace dump
3620 #define KERN_TRACE KERN_EMERG
3623 trace_printk_seq(struct trace_seq *s)
3625 /* Probably should print a warning here. */
3629 /* should be zero ended, but we are paranoid. */
3630 s->buffer[s->len] = 0;
3632 printk(KERN_TRACE "%s", s->buffer);
3637 void ftrace_dump(void)
3639 static DEFINE_SPINLOCK(ftrace_dump_lock);
3640 /* use static because iter can be a bit big for the stack */
3641 static struct trace_iterator iter;
3642 static int dump_ran;
3643 unsigned long flags;
3647 spin_lock_irqsave(&ftrace_dump_lock, flags);
3653 /* No turning back! */
3657 for_each_tracing_cpu(cpu) {
3658 atomic_inc(&global_trace.data[cpu]->disabled);
3661 /* don't look at user memory in panic mode */
3662 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3664 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3666 iter.tr = &global_trace;
3667 iter.trace = current_trace;
3670 * We need to stop all tracing on all CPUS to read the
3671 * the next buffer. This is a bit expensive, but is
3672 * not done often. We fill all what we can read,
3673 * and then release the locks again.
3676 while (!trace_empty(&iter)) {
3679 printk(KERN_TRACE "---------------------------------\n");
3683 /* reset all but tr, trace, and overruns */
3684 memset(&iter.seq, 0,
3685 sizeof(struct trace_iterator) -
3686 offsetof(struct trace_iterator, seq));
3687 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3690 if (find_next_entry_inc(&iter) != NULL) {
3691 print_trace_line(&iter);
3692 trace_consume(&iter);
3695 trace_printk_seq(&iter.seq);
3699 printk(KERN_TRACE " (ftrace buffer empty)\n");
3701 printk(KERN_TRACE "---------------------------------\n");
3704 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3707 __init static int tracer_alloc_buffers(void)
3709 struct trace_array_cpu *data;
3713 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3716 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3717 goto out_free_buffer_mask;
3719 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
3720 goto out_free_tracing_cpumask;
3722 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3723 cpumask_copy(tracing_cpumask, cpu_all_mask);
3724 cpumask_clear(tracing_reader_cpumask);
3726 /* TODO: make the number of buffers hot pluggable with CPUS */
3727 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3728 TRACE_BUFFER_FLAGS);
3729 if (!global_trace.buffer) {
3730 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3732 goto out_free_cpumask;
3734 global_trace.entries = ring_buffer_size(global_trace.buffer);
3737 #ifdef CONFIG_TRACER_MAX_TRACE
3738 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3739 TRACE_BUFFER_FLAGS);
3740 if (!max_tr.buffer) {
3741 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3743 ring_buffer_free(global_trace.buffer);
3744 goto out_free_cpumask;
3746 max_tr.entries = ring_buffer_size(max_tr.buffer);
3747 WARN_ON(max_tr.entries != global_trace.entries);
3750 /* Allocate the first page for all buffers */
3751 for_each_tracing_cpu(i) {
3752 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3753 max_tr.data[i] = &per_cpu(max_data, i);
3756 trace_init_cmdlines();
3758 register_tracer(&nop_trace);
3759 current_trace = &nop_trace;
3760 #ifdef CONFIG_BOOT_TRACER
3761 register_tracer(&boot_tracer);
3763 /* All seems OK, enable tracing */
3764 tracing_disabled = 0;
3766 atomic_notifier_chain_register(&panic_notifier_list,
3767 &trace_panic_notifier);
3769 register_die_notifier(&trace_die_notifier);
3773 free_cpumask_var(tracing_reader_cpumask);
3774 out_free_tracing_cpumask:
3775 free_cpumask_var(tracing_cpumask);
3776 out_free_buffer_mask:
3777 free_cpumask_var(tracing_buffer_mask);
3782 __init static int clear_boot_tracer(void)
3785 * The default tracer at boot buffer is an init section.
3786 * This function is called in lateinit. If we did not
3787 * find the boot tracer, then clear it out, to prevent
3788 * later registration from accessing the buffer that is
3789 * about to be freed.
3791 if (!default_bootup_tracer)
3794 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
3795 default_bootup_tracer);
3796 default_bootup_tracer = NULL;
3801 early_initcall(tracer_alloc_buffers);
3802 fs_initcall(tracer_init_debugfs);
3803 late_initcall(clear_boot_tracer);