2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/ring_buffer.h>
15 #include <linux/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/ctype.h>
34 #include <linux/init.h>
35 #include <linux/poll.h>
36 #include <linux/gfp.h>
40 #include "trace_output.h"
42 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
44 unsigned long __read_mostly tracing_max_latency;
45 unsigned long __read_mostly tracing_thresh;
48 * We need to change this state when a selftest is running.
49 * A selftest will lurk into the ring-buffer to count the
50 * entries inserted during the selftest although some concurrent
51 * insertions into the ring-buffer such as ftrace_printk could occurred
52 * at the same time, giving false positive or negative results.
54 static bool __read_mostly tracing_selftest_running;
57 * If a tracer is running, we do not want to run SELFTEST.
59 static bool __read_mostly tracing_selftest_disabled;
61 /* For tracers that don't implement custom flags */
62 static struct tracer_opt dummy_tracer_opt[] = {
66 static struct tracer_flags dummy_tracer_flags = {
68 .opts = dummy_tracer_opt
71 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
77 * Kill all tracing for good (never come back).
78 * It is initialized to 1 but will turn to zero if the initialization
79 * of the tracer is successful. But that is the only place that sets
82 static int tracing_disabled = 1;
84 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
86 static inline void ftrace_disable_cpu(void)
89 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
92 static inline void ftrace_enable_cpu(void)
94 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
98 static cpumask_var_t __read_mostly tracing_buffer_mask;
100 /* Define which cpu buffers are currently read in trace_pipe */
101 static cpumask_var_t tracing_reader_cpumask;
103 #define for_each_tracing_cpu(cpu) \
104 for_each_cpu(cpu, tracing_buffer_mask)
107 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
109 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
110 * is set, then ftrace_dump is called. This will output the contents
111 * of the ftrace buffers to the console. This is very useful for
112 * capturing traces that lead to crashes and outputing it to a
115 * It is default off, but you can enable it with either specifying
116 * "ftrace_dump_on_oops" in the kernel command line, or setting
117 * /proc/sys/kernel/ftrace_dump_on_oops to true.
119 int ftrace_dump_on_oops;
121 static int tracing_set_tracer(const char *buf);
123 #define BOOTUP_TRACER_SIZE 100
124 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
125 static char *default_bootup_tracer;
127 static int __init set_ftrace(char *str)
129 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
130 default_bootup_tracer = bootup_tracer_buf;
133 __setup("ftrace=", set_ftrace);
135 static int __init set_ftrace_dump_on_oops(char *str)
137 ftrace_dump_on_oops = 1;
140 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
143 ns2usecs(cycle_t nsec)
150 cycle_t ftrace_now(int cpu)
152 u64 ts = ring_buffer_time_stamp(cpu);
153 ring_buffer_normalize_time_stamp(cpu, &ts);
158 * The global_trace is the descriptor that holds the tracing
159 * buffers for the live tracing. For each CPU, it contains
160 * a link list of pages that will store trace entries. The
161 * page descriptor of the pages in the memory is used to hold
162 * the link list by linking the lru item in the page descriptor
163 * to each of the pages in the buffer per CPU.
165 * For each active CPU there is a data field that holds the
166 * pages for the buffer for that CPU. Each CPU has the same number
167 * of pages allocated for its buffer.
169 static struct trace_array global_trace;
171 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
174 * The max_tr is used to snapshot the global_trace when a maximum
175 * latency is reached. Some tracers will use this to store a maximum
176 * trace while it continues examining live traces.
178 * The buffers for the max_tr are set up the same as the global_trace.
179 * When a snapshot is taken, the link list of the max_tr is swapped
180 * with the link list of the global_trace and the buffers are reset for
181 * the global_trace so the tracing can continue.
183 static struct trace_array max_tr;
185 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
187 /* tracer_enabled is used to toggle activation of a tracer */
188 static int tracer_enabled = 1;
191 * tracing_is_enabled - return tracer_enabled status
193 * This function is used by other tracers to know the status
194 * of the tracer_enabled flag. Tracers may use this function
195 * to know if it should enable their features when starting
196 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
198 int tracing_is_enabled(void)
200 return tracer_enabled;
204 * trace_buf_size is the size in bytes that is allocated
205 * for a buffer. Note, the number of bytes is always rounded
208 * This number is purposely set to a low number of 16384.
209 * If the dump on oops happens, it will be much appreciated
210 * to not have to wait for all that output. Anyway this can be
211 * boot time and run time configurable.
213 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
215 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
217 /* trace_types holds a link list of available tracers. */
218 static struct tracer *trace_types __read_mostly;
220 /* current_trace points to the tracer that is currently active */
221 static struct tracer *current_trace __read_mostly;
224 * max_tracer_type_len is used to simplify the allocating of
225 * buffers to read userspace tracer names. We keep track of
226 * the longest tracer name registered.
228 static int max_tracer_type_len;
231 * trace_types_lock is used to protect the trace_types list.
232 * This lock is also used to keep user access serialized.
233 * Accesses from userspace will grab this lock while userspace
234 * activities happen inside the kernel.
236 static DEFINE_MUTEX(trace_types_lock);
238 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
239 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
241 /* trace_flags holds trace_options default values */
242 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
243 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
246 * trace_wake_up - wake up tasks waiting for trace input
248 * Simply wakes up any task that is blocked on the trace_wait
249 * queue. These is used with trace_poll for tasks polling the trace.
251 void trace_wake_up(void)
254 * The runqueue_is_locked() can fail, but this is the best we
257 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
258 wake_up(&trace_wait);
261 static int __init set_buf_size(char *str)
263 unsigned long buf_size;
268 ret = strict_strtoul(str, 0, &buf_size);
269 /* nr_entries can not be zero */
270 if (ret < 0 || buf_size == 0)
272 trace_buf_size = buf_size;
275 __setup("trace_buf_size=", set_buf_size);
277 unsigned long nsecs_to_usecs(unsigned long nsecs)
282 /* These must match the bit postions in trace_iterator_flags */
283 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);
345 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
353 if (s->len <= s->readpos)
356 len = s->len - s->readpos;
359 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
369 ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
374 if (s->len <= s->readpos)
377 len = s->len - s->readpos;
380 ret = memcpy(buf, s->buffer + s->readpos, cnt);
389 trace_print_seq(struct seq_file *m, struct trace_seq *s)
391 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
394 seq_puts(m, s->buffer);
400 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
402 * @tsk: the task with the latency
403 * @cpu: The cpu that initiated the trace.
405 * Flip the buffers between the @tr and the max_tr and record information
406 * about which task was the cause of this latency.
409 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
411 struct ring_buffer *buf = tr->buffer;
413 WARN_ON_ONCE(!irqs_disabled());
414 __raw_spin_lock(&ftrace_max_lock);
416 tr->buffer = max_tr.buffer;
419 ftrace_disable_cpu();
420 ring_buffer_reset(tr->buffer);
423 __update_max_tr(tr, tsk, cpu);
424 __raw_spin_unlock(&ftrace_max_lock);
428 * update_max_tr_single - only copy one trace over, and reset the rest
430 * @tsk - task with the latency
431 * @cpu - the cpu of the buffer to copy.
433 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
436 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
440 WARN_ON_ONCE(!irqs_disabled());
441 __raw_spin_lock(&ftrace_max_lock);
443 ftrace_disable_cpu();
445 ring_buffer_reset(max_tr.buffer);
446 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
450 WARN_ON_ONCE(ret && ret != -EAGAIN);
452 __update_max_tr(tr, tsk, cpu);
453 __raw_spin_unlock(&ftrace_max_lock);
457 * register_tracer - register a tracer with the ftrace system.
458 * @type - the plugin for the tracer
460 * Register a new plugin tracer.
462 int register_tracer(struct tracer *type)
463 __releases(kernel_lock)
464 __acquires(kernel_lock)
471 pr_info("Tracer must have a name\n");
476 * When this gets called we hold the BKL which means that
477 * preemption is disabled. Various trace selftests however
478 * need to disable and enable preemption for successful tests.
479 * So we drop the BKL here and grab it after the tests again.
482 mutex_lock(&trace_types_lock);
484 tracing_selftest_running = true;
486 for (t = trace_types; t; t = t->next) {
487 if (strcmp(type->name, t->name) == 0) {
489 pr_info("Trace %s already registered\n",
497 type->set_flag = &dummy_set_flag;
499 type->flags = &dummy_tracer_flags;
501 if (!type->flags->opts)
502 type->flags->opts = dummy_tracer_opt;
503 if (!type->wait_pipe)
504 type->wait_pipe = default_wait_pipe;
507 #ifdef CONFIG_FTRACE_STARTUP_TEST
508 if (type->selftest && !tracing_selftest_disabled) {
509 struct tracer *saved_tracer = current_trace;
510 struct trace_array *tr = &global_trace;
514 * Run a selftest on this tracer.
515 * Here we reset the trace buffer, and set the current
516 * tracer to be this tracer. The tracer can then run some
517 * internal tracing to verify that everything is in order.
518 * If we fail, we do not register this tracer.
520 for_each_tracing_cpu(i)
521 tracing_reset(tr, i);
523 current_trace = type;
524 /* the test is responsible for initializing and enabling */
525 pr_info("Testing tracer %s: ", type->name);
526 ret = type->selftest(type, tr);
527 /* the test is responsible for resetting too */
528 current_trace = saved_tracer;
530 printk(KERN_CONT "FAILED!\n");
533 /* Only reset on passing, to avoid touching corrupted buffers */
534 for_each_tracing_cpu(i)
535 tracing_reset(tr, i);
537 printk(KERN_CONT "PASSED\n");
541 type->next = trace_types;
543 len = strlen(type->name);
544 if (len > max_tracer_type_len)
545 max_tracer_type_len = len;
548 tracing_selftest_running = false;
549 mutex_unlock(&trace_types_lock);
551 if (ret || !default_bootup_tracer)
554 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
557 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
558 /* Do we want this tracer to start on bootup? */
559 tracing_set_tracer(type->name);
560 default_bootup_tracer = NULL;
561 /* disable other selftests, since this will break it. */
562 tracing_selftest_disabled = 1;
563 #ifdef CONFIG_FTRACE_STARTUP_TEST
564 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
573 void unregister_tracer(struct tracer *type)
578 mutex_lock(&trace_types_lock);
579 for (t = &trace_types; *t; t = &(*t)->next) {
583 pr_info("Trace %s not registered\n", type->name);
589 if (type == current_trace && tracer_enabled) {
592 if (current_trace->stop)
593 current_trace->stop(&global_trace);
594 current_trace = &nop_trace;
597 if (strlen(type->name) != max_tracer_type_len)
600 max_tracer_type_len = 0;
601 for (t = &trace_types; *t; t = &(*t)->next) {
602 len = strlen((*t)->name);
603 if (len > max_tracer_type_len)
604 max_tracer_type_len = len;
607 mutex_unlock(&trace_types_lock);
610 void tracing_reset(struct trace_array *tr, int cpu)
612 ftrace_disable_cpu();
613 ring_buffer_reset_cpu(tr->buffer, cpu);
617 void tracing_reset_online_cpus(struct trace_array *tr)
621 tr->time_start = ftrace_now(tr->cpu);
623 for_each_online_cpu(cpu)
624 tracing_reset(tr, cpu);
627 #define SAVED_CMDLINES 128
628 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
629 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
630 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
631 static int cmdline_idx;
632 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
634 /* temporary disable recording */
635 static atomic_t trace_record_cmdline_disabled __read_mostly;
637 static void trace_init_cmdlines(void)
639 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
640 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
644 static int trace_stop_count;
645 static DEFINE_SPINLOCK(tracing_start_lock);
648 * ftrace_off_permanent - disable all ftrace code permanently
650 * This should only be called when a serious anomally has
651 * been detected. This will turn off the function tracing,
652 * ring buffers, and other tracing utilites. It takes no
653 * locks and can be called from any context.
655 void ftrace_off_permanent(void)
657 tracing_disabled = 1;
659 tracing_off_permanent();
663 * tracing_start - quick start of the tracer
665 * If tracing is enabled but was stopped by tracing_stop,
666 * this will start the tracer back up.
668 void tracing_start(void)
670 struct ring_buffer *buffer;
673 if (tracing_disabled)
676 spin_lock_irqsave(&tracing_start_lock, flags);
677 if (--trace_stop_count) {
678 if (trace_stop_count < 0) {
679 /* Someone screwed up their debugging */
681 trace_stop_count = 0;
687 buffer = global_trace.buffer;
689 ring_buffer_record_enable(buffer);
691 buffer = max_tr.buffer;
693 ring_buffer_record_enable(buffer);
697 spin_unlock_irqrestore(&tracing_start_lock, flags);
701 * tracing_stop - quick stop of the tracer
703 * Light weight way to stop tracing. Use in conjunction with
706 void tracing_stop(void)
708 struct ring_buffer *buffer;
712 spin_lock_irqsave(&tracing_start_lock, flags);
713 if (trace_stop_count++)
716 buffer = global_trace.buffer;
718 ring_buffer_record_disable(buffer);
720 buffer = max_tr.buffer;
722 ring_buffer_record_disable(buffer);
725 spin_unlock_irqrestore(&tracing_start_lock, flags);
728 void trace_stop_cmdline_recording(void);
730 static void trace_save_cmdline(struct task_struct *tsk)
735 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
739 * It's not the end of the world if we don't get
740 * the lock, but we also don't want to spin
741 * nor do we want to disable interrupts,
742 * so if we miss here, then better luck next time.
744 if (!__raw_spin_trylock(&trace_cmdline_lock))
747 idx = map_pid_to_cmdline[tsk->pid];
748 if (idx >= SAVED_CMDLINES) {
749 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
751 map = map_cmdline_to_pid[idx];
752 if (map <= PID_MAX_DEFAULT)
753 map_pid_to_cmdline[map] = (unsigned)-1;
755 map_pid_to_cmdline[tsk->pid] = idx;
760 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
762 __raw_spin_unlock(&trace_cmdline_lock);
765 char *trace_find_cmdline(int pid)
767 char *cmdline = "<...>";
773 if (pid > PID_MAX_DEFAULT)
776 map = map_pid_to_cmdline[pid];
777 if (map >= SAVED_CMDLINES)
780 cmdline = saved_cmdlines[map];
786 void tracing_record_cmdline(struct task_struct *tsk)
788 if (atomic_read(&trace_record_cmdline_disabled))
791 trace_save_cmdline(tsk);
795 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
798 struct task_struct *tsk = current;
800 entry->preempt_count = pc & 0xff;
801 entry->pid = (tsk) ? tsk->pid : 0;
802 entry->tgid = (tsk) ? tsk->tgid : 0;
804 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
805 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
807 TRACE_FLAG_IRQS_NOSUPPORT |
809 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
810 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
811 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
814 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
817 unsigned long flags, int pc)
819 struct ring_buffer_event *event;
821 event = ring_buffer_lock_reserve(tr->buffer, len);
823 struct trace_entry *ent = ring_buffer_event_data(event);
825 tracing_generic_entry_update(ent, flags, pc);
831 static void ftrace_trace_stack(struct trace_array *tr,
832 unsigned long flags, int skip, int pc);
833 static void ftrace_trace_userstack(struct trace_array *tr,
834 unsigned long flags, int pc);
836 void trace_buffer_unlock_commit(struct trace_array *tr,
837 struct ring_buffer_event *event,
838 unsigned long flags, int pc)
840 ring_buffer_unlock_commit(tr->buffer, event);
842 ftrace_trace_stack(tr, flags, 6, pc);
843 ftrace_trace_userstack(tr, flags, pc);
847 struct ring_buffer_event *
848 trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
849 unsigned long flags, int pc)
851 return trace_buffer_lock_reserve(&global_trace,
852 type, len, flags, pc);
855 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
856 unsigned long flags, int pc)
858 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
862 trace_function(struct trace_array *tr,
863 unsigned long ip, unsigned long parent_ip, unsigned long flags,
866 struct ring_buffer_event *event;
867 struct ftrace_entry *entry;
869 /* If we are reading the ring buffer, don't trace */
870 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
873 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
877 entry = ring_buffer_event_data(event);
879 entry->parent_ip = parent_ip;
880 ring_buffer_unlock_commit(tr->buffer, event);
883 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
884 static void __trace_graph_entry(struct trace_array *tr,
885 struct ftrace_graph_ent *trace,
889 struct ring_buffer_event *event;
890 struct ftrace_graph_ent_entry *entry;
892 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
895 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
896 sizeof(*entry), flags, pc);
899 entry = ring_buffer_event_data(event);
900 entry->graph_ent = *trace;
901 ring_buffer_unlock_commit(global_trace.buffer, event);
904 static void __trace_graph_return(struct trace_array *tr,
905 struct ftrace_graph_ret *trace,
909 struct ring_buffer_event *event;
910 struct ftrace_graph_ret_entry *entry;
912 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
915 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
916 sizeof(*entry), flags, pc);
919 entry = ring_buffer_event_data(event);
921 ring_buffer_unlock_commit(global_trace.buffer, event);
926 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
927 unsigned long ip, unsigned long parent_ip, unsigned long flags,
930 if (likely(!atomic_read(&data->disabled)))
931 trace_function(tr, ip, parent_ip, flags, pc);
934 static void __ftrace_trace_stack(struct trace_array *tr,
938 #ifdef CONFIG_STACKTRACE
939 struct ring_buffer_event *event;
940 struct stack_entry *entry;
941 struct stack_trace trace;
943 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
944 sizeof(*entry), flags, pc);
947 entry = ring_buffer_event_data(event);
948 memset(&entry->caller, 0, sizeof(entry->caller));
950 trace.nr_entries = 0;
951 trace.max_entries = FTRACE_STACK_ENTRIES;
953 trace.entries = entry->caller;
955 save_stack_trace(&trace);
956 ring_buffer_unlock_commit(tr->buffer, event);
960 static void ftrace_trace_stack(struct trace_array *tr,
964 if (!(trace_flags & TRACE_ITER_STACKTRACE))
967 __ftrace_trace_stack(tr, flags, skip, pc);
970 void __trace_stack(struct trace_array *tr,
974 __ftrace_trace_stack(tr, flags, skip, pc);
977 static void ftrace_trace_userstack(struct trace_array *tr,
978 unsigned long flags, int pc)
980 #ifdef CONFIG_STACKTRACE
981 struct ring_buffer_event *event;
982 struct userstack_entry *entry;
983 struct stack_trace trace;
985 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
988 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
989 sizeof(*entry), flags, pc);
992 entry = ring_buffer_event_data(event);
994 memset(&entry->caller, 0, sizeof(entry->caller));
996 trace.nr_entries = 0;
997 trace.max_entries = FTRACE_STACK_ENTRIES;
999 trace.entries = entry->caller;
1001 save_stack_trace_user(&trace);
1002 ring_buffer_unlock_commit(tr->buffer, event);
1007 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1009 ftrace_trace_userstack(tr, flags, preempt_count());
1014 ftrace_trace_special(void *__tr,
1015 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1018 struct ring_buffer_event *event;
1019 struct trace_array *tr = __tr;
1020 struct special_entry *entry;
1022 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1023 sizeof(*entry), 0, pc);
1026 entry = ring_buffer_event_data(event);
1030 trace_buffer_unlock_commit(tr, event, 0, pc);
1034 __trace_special(void *__tr, void *__data,
1035 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1037 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1041 tracing_sched_switch_trace(struct trace_array *tr,
1042 struct task_struct *prev,
1043 struct task_struct *next,
1044 unsigned long flags, int pc)
1046 struct ring_buffer_event *event;
1047 struct ctx_switch_entry *entry;
1049 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1050 sizeof(*entry), flags, pc);
1053 entry = ring_buffer_event_data(event);
1054 entry->prev_pid = prev->pid;
1055 entry->prev_prio = prev->prio;
1056 entry->prev_state = prev->state;
1057 entry->next_pid = next->pid;
1058 entry->next_prio = next->prio;
1059 entry->next_state = next->state;
1060 entry->next_cpu = task_cpu(next);
1061 trace_buffer_unlock_commit(tr, event, flags, pc);
1065 tracing_sched_wakeup_trace(struct trace_array *tr,
1066 struct task_struct *wakee,
1067 struct task_struct *curr,
1068 unsigned long flags, int pc)
1070 struct ring_buffer_event *event;
1071 struct ctx_switch_entry *entry;
1073 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1074 sizeof(*entry), flags, pc);
1077 entry = ring_buffer_event_data(event);
1078 entry->prev_pid = curr->pid;
1079 entry->prev_prio = curr->prio;
1080 entry->prev_state = curr->state;
1081 entry->next_pid = wakee->pid;
1082 entry->next_prio = wakee->prio;
1083 entry->next_state = wakee->state;
1084 entry->next_cpu = task_cpu(wakee);
1086 ring_buffer_unlock_commit(tr->buffer, event);
1087 ftrace_trace_stack(tr, flags, 6, pc);
1088 ftrace_trace_userstack(tr, flags, pc);
1092 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1094 struct trace_array *tr = &global_trace;
1095 struct trace_array_cpu *data;
1096 unsigned long flags;
1100 if (tracing_disabled)
1103 pc = preempt_count();
1104 local_irq_save(flags);
1105 cpu = raw_smp_processor_id();
1106 data = tr->data[cpu];
1108 if (likely(atomic_inc_return(&data->disabled) == 1))
1109 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1111 atomic_dec(&data->disabled);
1112 local_irq_restore(flags);
1115 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1116 int trace_graph_entry(struct ftrace_graph_ent *trace)
1118 struct trace_array *tr = &global_trace;
1119 struct trace_array_cpu *data;
1120 unsigned long flags;
1125 if (!ftrace_trace_task(current))
1128 if (!ftrace_graph_addr(trace->func))
1131 local_irq_save(flags);
1132 cpu = raw_smp_processor_id();
1133 data = tr->data[cpu];
1134 disabled = atomic_inc_return(&data->disabled);
1135 if (likely(disabled == 1)) {
1136 pc = preempt_count();
1137 __trace_graph_entry(tr, trace, flags, pc);
1139 /* Only do the atomic if it is not already set */
1140 if (!test_tsk_trace_graph(current))
1141 set_tsk_trace_graph(current);
1142 atomic_dec(&data->disabled);
1143 local_irq_restore(flags);
1148 void trace_graph_return(struct ftrace_graph_ret *trace)
1150 struct trace_array *tr = &global_trace;
1151 struct trace_array_cpu *data;
1152 unsigned long flags;
1157 local_irq_save(flags);
1158 cpu = raw_smp_processor_id();
1159 data = tr->data[cpu];
1160 disabled = atomic_inc_return(&data->disabled);
1161 if (likely(disabled == 1)) {
1162 pc = preempt_count();
1163 __trace_graph_return(tr, trace, flags, pc);
1166 clear_tsk_trace_graph(current);
1167 atomic_dec(&data->disabled);
1168 local_irq_restore(flags);
1170 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1172 enum trace_file_type {
1173 TRACE_FILE_LAT_FMT = 1,
1174 TRACE_FILE_ANNOTATE = 2,
1177 static void trace_iterator_increment(struct trace_iterator *iter)
1179 /* Don't allow ftrace to trace into the ring buffers */
1180 ftrace_disable_cpu();
1183 if (iter->buffer_iter[iter->cpu])
1184 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1186 ftrace_enable_cpu();
1189 static struct trace_entry *
1190 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1192 struct ring_buffer_event *event;
1193 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1195 /* Don't allow ftrace to trace into the ring buffers */
1196 ftrace_disable_cpu();
1199 event = ring_buffer_iter_peek(buf_iter, ts);
1201 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1203 ftrace_enable_cpu();
1205 return event ? ring_buffer_event_data(event) : NULL;
1208 static struct trace_entry *
1209 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1211 struct ring_buffer *buffer = iter->tr->buffer;
1212 struct trace_entry *ent, *next = NULL;
1213 int cpu_file = iter->cpu_file;
1214 u64 next_ts = 0, ts;
1219 * If we are in a per_cpu trace file, don't bother by iterating over
1220 * all cpu and peek directly.
1222 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1223 if (ring_buffer_empty_cpu(buffer, cpu_file))
1225 ent = peek_next_entry(iter, cpu_file, ent_ts);
1227 *ent_cpu = cpu_file;
1232 for_each_tracing_cpu(cpu) {
1234 if (ring_buffer_empty_cpu(buffer, cpu))
1237 ent = peek_next_entry(iter, cpu, &ts);
1240 * Pick the entry with the smallest timestamp:
1242 if (ent && (!next || ts < next_ts)) {
1250 *ent_cpu = next_cpu;
1258 /* Find the next real entry, without updating the iterator itself */
1259 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1260 int *ent_cpu, u64 *ent_ts)
1262 return __find_next_entry(iter, ent_cpu, ent_ts);
1265 /* Find the next real entry, and increment the iterator to the next entry */
1266 static void *find_next_entry_inc(struct trace_iterator *iter)
1268 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1271 trace_iterator_increment(iter);
1273 return iter->ent ? iter : NULL;
1276 static void trace_consume(struct trace_iterator *iter)
1278 /* Don't allow ftrace to trace into the ring buffers */
1279 ftrace_disable_cpu();
1280 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1281 ftrace_enable_cpu();
1284 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1286 struct trace_iterator *iter = m->private;
1292 /* can't go backwards */
1297 ent = find_next_entry_inc(iter);
1301 while (ent && iter->idx < i)
1302 ent = find_next_entry_inc(iter);
1310 * No necessary locking here. The worst thing which can
1311 * happen is loosing events consumed at the same time
1312 * by a trace_pipe reader.
1313 * Other than that, we don't risk to crash the ring buffer
1314 * because it serializes the readers.
1316 * The current tracer is copied to avoid a global locking
1319 static void *s_start(struct seq_file *m, loff_t *pos)
1321 struct trace_iterator *iter = m->private;
1322 static struct tracer *old_tracer;
1323 int cpu_file = iter->cpu_file;
1328 /* copy the tracer to avoid using a global lock all around */
1329 mutex_lock(&trace_types_lock);
1330 if (unlikely(old_tracer != current_trace && current_trace)) {
1331 old_tracer = current_trace;
1332 *iter->trace = *current_trace;
1334 mutex_unlock(&trace_types_lock);
1336 atomic_inc(&trace_record_cmdline_disabled);
1338 if (*pos != iter->pos) {
1343 ftrace_disable_cpu();
1345 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1346 for_each_tracing_cpu(cpu)
1347 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1349 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1352 ftrace_enable_cpu();
1354 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1359 p = s_next(m, p, &l);
1365 static void s_stop(struct seq_file *m, void *p)
1367 atomic_dec(&trace_record_cmdline_disabled);
1370 static void print_lat_help_header(struct seq_file *m)
1372 seq_puts(m, "# _------=> CPU# \n");
1373 seq_puts(m, "# / _-----=> irqs-off \n");
1374 seq_puts(m, "# | / _----=> need-resched \n");
1375 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1376 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1377 seq_puts(m, "# |||| / \n");
1378 seq_puts(m, "# ||||| delay \n");
1379 seq_puts(m, "# cmd pid ||||| time | caller \n");
1380 seq_puts(m, "# \\ / ||||| \\ | / \n");
1383 static void print_func_help_header(struct seq_file *m)
1385 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1386 seq_puts(m, "# | | | | |\n");
1391 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1393 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1394 struct trace_array *tr = iter->tr;
1395 struct trace_array_cpu *data = tr->data[tr->cpu];
1396 struct tracer *type = current_trace;
1397 unsigned long total;
1398 unsigned long entries;
1399 const char *name = "preemption";
1404 entries = ring_buffer_entries(iter->tr->buffer);
1406 ring_buffer_overruns(iter->tr->buffer);
1408 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1410 seq_puts(m, "-----------------------------------"
1411 "---------------------------------\n");
1412 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1413 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1414 nsecs_to_usecs(data->saved_latency),
1418 #if defined(CONFIG_PREEMPT_NONE)
1420 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1422 #elif defined(CONFIG_PREEMPT)
1427 /* These are reserved for later use */
1430 seq_printf(m, " #P:%d)\n", num_online_cpus());
1434 seq_puts(m, " -----------------\n");
1435 seq_printf(m, " | task: %.16s-%d "
1436 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1437 data->comm, data->pid, data->uid, data->nice,
1438 data->policy, data->rt_priority);
1439 seq_puts(m, " -----------------\n");
1441 if (data->critical_start) {
1442 seq_puts(m, " => started at: ");
1443 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1444 trace_print_seq(m, &iter->seq);
1445 seq_puts(m, "\n => ended at: ");
1446 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1447 trace_print_seq(m, &iter->seq);
1454 static void test_cpu_buff_start(struct trace_iterator *iter)
1456 struct trace_seq *s = &iter->seq;
1458 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1461 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1464 if (cpumask_test_cpu(iter->cpu, iter->started))
1467 cpumask_set_cpu(iter->cpu, iter->started);
1468 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1471 static enum print_line_t print_lat_fmt(struct trace_iterator *iter)
1473 struct trace_seq *s = &iter->seq;
1474 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1475 struct trace_event *event;
1476 struct trace_entry *entry = iter->ent;
1478 test_cpu_buff_start(iter);
1480 event = ftrace_find_event(entry->type);
1482 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1483 if (!trace_print_lat_context(iter))
1488 return event->trace(iter, sym_flags);
1490 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1493 return TRACE_TYPE_HANDLED;
1495 return TRACE_TYPE_PARTIAL_LINE;
1498 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1500 struct trace_seq *s = &iter->seq;
1501 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1502 struct trace_entry *entry;
1503 struct trace_event *event;
1507 test_cpu_buff_start(iter);
1509 event = ftrace_find_event(entry->type);
1511 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1512 if (!trace_print_context(iter))
1517 return event->trace(iter, sym_flags);
1519 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1522 return TRACE_TYPE_HANDLED;
1524 return TRACE_TYPE_PARTIAL_LINE;
1527 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1529 struct trace_seq *s = &iter->seq;
1530 struct trace_entry *entry;
1531 struct trace_event *event;
1535 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1536 if (!trace_seq_printf(s, "%d %d %llu ",
1537 entry->pid, iter->cpu, iter->ts))
1541 event = ftrace_find_event(entry->type);
1543 return event->raw(iter, 0);
1545 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1548 return TRACE_TYPE_HANDLED;
1550 return TRACE_TYPE_PARTIAL_LINE;
1553 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1555 struct trace_seq *s = &iter->seq;
1556 unsigned char newline = '\n';
1557 struct trace_entry *entry;
1558 struct trace_event *event;
1562 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1563 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1564 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1565 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1568 event = ftrace_find_event(entry->type);
1570 enum print_line_t ret = event->hex(iter, 0);
1571 if (ret != TRACE_TYPE_HANDLED)
1575 SEQ_PUT_FIELD_RET(s, newline);
1577 return TRACE_TYPE_HANDLED;
1580 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1582 struct trace_seq *s = &iter->seq;
1583 struct trace_entry *entry = iter->ent;
1584 struct print_entry *field;
1587 trace_assign_type(field, entry);
1589 ret = trace_seq_printf(s, "%s", field->buf);
1591 return TRACE_TYPE_PARTIAL_LINE;
1593 return TRACE_TYPE_HANDLED;
1596 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1598 struct trace_seq *s = &iter->seq;
1599 struct trace_entry *entry;
1600 struct trace_event *event;
1604 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1605 SEQ_PUT_FIELD_RET(s, entry->pid);
1606 SEQ_PUT_FIELD_RET(s, iter->cpu);
1607 SEQ_PUT_FIELD_RET(s, iter->ts);
1610 event = ftrace_find_event(entry->type);
1611 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1614 static int trace_empty(struct trace_iterator *iter)
1618 for_each_tracing_cpu(cpu) {
1619 if (iter->buffer_iter[cpu]) {
1620 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1623 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1631 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1633 enum print_line_t ret;
1635 if (iter->trace && iter->trace->print_line) {
1636 ret = iter->trace->print_line(iter);
1637 if (ret != TRACE_TYPE_UNHANDLED)
1641 if (iter->ent->type == TRACE_PRINT &&
1642 trace_flags & TRACE_ITER_PRINTK &&
1643 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1644 return print_printk_msg_only(iter);
1646 if (trace_flags & TRACE_ITER_BIN)
1647 return print_bin_fmt(iter);
1649 if (trace_flags & TRACE_ITER_HEX)
1650 return print_hex_fmt(iter);
1652 if (trace_flags & TRACE_ITER_RAW)
1653 return print_raw_fmt(iter);
1655 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1656 return print_lat_fmt(iter);
1658 return print_trace_fmt(iter);
1661 static int s_show(struct seq_file *m, void *v)
1663 struct trace_iterator *iter = v;
1665 if (iter->ent == NULL) {
1667 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1670 if (iter->trace && iter->trace->print_header)
1671 iter->trace->print_header(m);
1672 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1673 /* print nothing if the buffers are empty */
1674 if (trace_empty(iter))
1676 print_trace_header(m, iter);
1677 if (!(trace_flags & TRACE_ITER_VERBOSE))
1678 print_lat_help_header(m);
1680 if (!(trace_flags & TRACE_ITER_VERBOSE))
1681 print_func_help_header(m);
1684 print_trace_line(iter);
1685 trace_print_seq(m, &iter->seq);
1691 static struct seq_operations tracer_seq_ops = {
1698 static struct trace_iterator *
1699 __tracing_open(struct inode *inode, struct file *file)
1701 long cpu_file = (long) inode->i_private;
1702 void *fail_ret = ERR_PTR(-ENOMEM);
1703 struct trace_iterator *iter;
1707 if (tracing_disabled)
1708 return ERR_PTR(-ENODEV);
1710 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1712 return ERR_PTR(-ENOMEM);
1715 * We make a copy of the current tracer to avoid concurrent
1716 * changes on it while we are reading.
1718 mutex_lock(&trace_types_lock);
1719 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1724 *iter->trace = *current_trace;
1726 if (current_trace && current_trace->print_max)
1729 iter->tr = &global_trace;
1731 mutex_init(&iter->mutex);
1732 iter->cpu_file = cpu_file;
1734 /* Notify the tracer early; before we stop tracing. */
1735 if (iter->trace && iter->trace->open)
1736 iter->trace->open(iter);
1738 /* Annotate start of buffers if we had overruns */
1739 if (ring_buffer_overruns(iter->tr->buffer))
1740 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1742 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1743 for_each_tracing_cpu(cpu) {
1745 iter->buffer_iter[cpu] =
1746 ring_buffer_read_start(iter->tr->buffer, cpu);
1748 if (!iter->buffer_iter[cpu])
1752 cpu = iter->cpu_file;
1753 iter->buffer_iter[cpu] =
1754 ring_buffer_read_start(iter->tr->buffer, cpu);
1756 if (!iter->buffer_iter[cpu])
1760 /* TODO stop tracer */
1761 ret = seq_open(file, &tracer_seq_ops);
1763 fail_ret = ERR_PTR(ret);
1767 m = file->private_data;
1770 /* stop the trace while dumping */
1773 mutex_unlock(&trace_types_lock);
1778 for_each_tracing_cpu(cpu) {
1779 if (iter->buffer_iter[cpu])
1780 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1783 mutex_unlock(&trace_types_lock);
1790 int tracing_open_generic(struct inode *inode, struct file *filp)
1792 if (tracing_disabled)
1795 filp->private_data = inode->i_private;
1799 static int tracing_release(struct inode *inode, struct file *file)
1801 struct seq_file *m = (struct seq_file *)file->private_data;
1802 struct trace_iterator *iter = m->private;
1805 mutex_lock(&trace_types_lock);
1806 for_each_tracing_cpu(cpu) {
1807 if (iter->buffer_iter[cpu])
1808 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1811 if (iter->trace && iter->trace->close)
1812 iter->trace->close(iter);
1814 /* reenable tracing if it was previously enabled */
1816 mutex_unlock(&trace_types_lock);
1818 seq_release(inode, file);
1819 mutex_destroy(&iter->mutex);
1825 static int tracing_open(struct inode *inode, struct file *file)
1827 struct trace_iterator *iter;
1830 iter = __tracing_open(inode, file);
1832 ret = PTR_ERR(iter);
1833 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1834 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1840 t_next(struct seq_file *m, void *v, loff_t *pos)
1842 struct tracer *t = m->private;
1854 static void *t_start(struct seq_file *m, loff_t *pos)
1856 struct tracer *t = m->private;
1859 mutex_lock(&trace_types_lock);
1860 for (; t && l < *pos; t = t_next(m, t, &l))
1866 static void t_stop(struct seq_file *m, void *p)
1868 mutex_unlock(&trace_types_lock);
1871 static int t_show(struct seq_file *m, void *v)
1873 struct tracer *t = v;
1878 seq_printf(m, "%s", t->name);
1887 static struct seq_operations show_traces_seq_ops = {
1894 static int show_traces_open(struct inode *inode, struct file *file)
1898 if (tracing_disabled)
1901 ret = seq_open(file, &show_traces_seq_ops);
1903 struct seq_file *m = file->private_data;
1904 m->private = trace_types;
1910 static struct file_operations tracing_fops = {
1911 .open = tracing_open,
1913 .llseek = seq_lseek,
1914 .release = tracing_release,
1917 static struct file_operations show_traces_fops = {
1918 .open = show_traces_open,
1920 .release = seq_release,
1924 * Only trace on a CPU if the bitmask is set:
1926 static cpumask_var_t tracing_cpumask;
1929 * The tracer itself will not take this lock, but still we want
1930 * to provide a consistent cpumask to user-space:
1932 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1935 * Temporary storage for the character representation of the
1936 * CPU bitmask (and one more byte for the newline):
1938 static char mask_str[NR_CPUS + 1];
1941 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1942 size_t count, loff_t *ppos)
1946 mutex_lock(&tracing_cpumask_update_lock);
1948 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1949 if (count - len < 2) {
1953 len += sprintf(mask_str + len, "\n");
1954 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1957 mutex_unlock(&tracing_cpumask_update_lock);
1963 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1964 size_t count, loff_t *ppos)
1967 cpumask_var_t tracing_cpumask_new;
1969 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1972 mutex_lock(&tracing_cpumask_update_lock);
1973 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1977 local_irq_disable();
1978 __raw_spin_lock(&ftrace_max_lock);
1979 for_each_tracing_cpu(cpu) {
1981 * Increase/decrease the disabled counter if we are
1982 * about to flip a bit in the cpumask:
1984 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
1985 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
1986 atomic_inc(&global_trace.data[cpu]->disabled);
1988 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
1989 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
1990 atomic_dec(&global_trace.data[cpu]->disabled);
1993 __raw_spin_unlock(&ftrace_max_lock);
1996 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
1998 mutex_unlock(&tracing_cpumask_update_lock);
1999 free_cpumask_var(tracing_cpumask_new);
2004 mutex_unlock(&tracing_cpumask_update_lock);
2005 free_cpumask_var(tracing_cpumask);
2010 static struct file_operations tracing_cpumask_fops = {
2011 .open = tracing_open_generic,
2012 .read = tracing_cpumask_read,
2013 .write = tracing_cpumask_write,
2017 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2018 size_t cnt, loff_t *ppos)
2020 struct tracer_opt *trace_opts;
2028 /* calculate max size */
2029 for (i = 0; trace_options[i]; i++) {
2030 len += strlen(trace_options[i]);
2031 len += 3; /* "no" and newline */
2034 mutex_lock(&trace_types_lock);
2035 tracer_flags = current_trace->flags->val;
2036 trace_opts = current_trace->flags->opts;
2039 * Increase the size with names of options specific
2040 * of the current tracer.
2042 for (i = 0; trace_opts[i].name; i++) {
2043 len += strlen(trace_opts[i].name);
2044 len += 3; /* "no" and newline */
2047 /* +2 for \n and \0 */
2048 buf = kmalloc(len + 2, GFP_KERNEL);
2050 mutex_unlock(&trace_types_lock);
2054 for (i = 0; trace_options[i]; i++) {
2055 if (trace_flags & (1 << i))
2056 r += sprintf(buf + r, "%s\n", trace_options[i]);
2058 r += sprintf(buf + r, "no%s\n", trace_options[i]);
2061 for (i = 0; trace_opts[i].name; i++) {
2062 if (tracer_flags & trace_opts[i].bit)
2063 r += sprintf(buf + r, "%s\n",
2064 trace_opts[i].name);
2066 r += sprintf(buf + r, "no%s\n",
2067 trace_opts[i].name);
2069 mutex_unlock(&trace_types_lock);
2071 WARN_ON(r >= len + 2);
2073 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2079 /* Try to assign a tracer specific option */
2080 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2082 struct tracer_flags *trace_flags = trace->flags;
2083 struct tracer_opt *opts = NULL;
2087 for (i = 0; trace_flags->opts[i].name; i++) {
2088 opts = &trace_flags->opts[i];
2089 len = strlen(opts->name);
2091 if (strncmp(cmp, opts->name, len) == 0) {
2092 ret = trace->set_flag(trace_flags->val,
2098 if (!trace_flags->opts[i].name)
2101 /* Refused to handle */
2106 trace_flags->val &= ~opts->bit;
2108 trace_flags->val |= opts->bit;
2114 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2115 size_t cnt, loff_t *ppos)
2123 if (cnt >= sizeof(buf))
2126 if (copy_from_user(&buf, ubuf, cnt))
2131 if (strncmp(buf, "no", 2) == 0) {
2136 for (i = 0; trace_options[i]; i++) {
2137 int len = strlen(trace_options[i]);
2139 if (strncmp(cmp, trace_options[i], len) == 0) {
2141 trace_flags &= ~(1 << i);
2143 trace_flags |= (1 << i);
2148 /* If no option could be set, test the specific tracer options */
2149 if (!trace_options[i]) {
2150 mutex_lock(&trace_types_lock);
2151 ret = set_tracer_option(current_trace, cmp, neg);
2152 mutex_unlock(&trace_types_lock);
2162 static struct file_operations tracing_iter_fops = {
2163 .open = tracing_open_generic,
2164 .read = tracing_trace_options_read,
2165 .write = tracing_trace_options_write,
2168 static const char readme_msg[] =
2169 "tracing mini-HOWTO:\n\n"
2171 "# mount -t debugfs nodev /debug\n\n"
2172 "# cat /debug/tracing/available_tracers\n"
2173 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2174 "# cat /debug/tracing/current_tracer\n"
2176 "# echo sched_switch > /debug/tracing/current_tracer\n"
2177 "# cat /debug/tracing/current_tracer\n"
2179 "# cat /debug/tracing/trace_options\n"
2180 "noprint-parent nosym-offset nosym-addr noverbose\n"
2181 "# echo print-parent > /debug/tracing/trace_options\n"
2182 "# echo 1 > /debug/tracing/tracing_enabled\n"
2183 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2184 "echo 0 > /debug/tracing/tracing_enabled\n"
2188 tracing_readme_read(struct file *filp, char __user *ubuf,
2189 size_t cnt, loff_t *ppos)
2191 return simple_read_from_buffer(ubuf, cnt, ppos,
2192 readme_msg, strlen(readme_msg));
2195 static struct file_operations tracing_readme_fops = {
2196 .open = tracing_open_generic,
2197 .read = tracing_readme_read,
2201 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2202 size_t cnt, loff_t *ppos)
2207 r = sprintf(buf, "%u\n", tracer_enabled);
2208 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2212 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2213 size_t cnt, loff_t *ppos)
2215 struct trace_array *tr = filp->private_data;
2220 if (cnt >= sizeof(buf))
2223 if (copy_from_user(&buf, ubuf, cnt))
2228 ret = strict_strtoul(buf, 10, &val);
2234 mutex_lock(&trace_types_lock);
2235 if (tracer_enabled ^ val) {
2238 if (current_trace->start)
2239 current_trace->start(tr);
2244 if (current_trace->stop)
2245 current_trace->stop(tr);
2248 mutex_unlock(&trace_types_lock);
2256 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2257 size_t cnt, loff_t *ppos)
2259 char buf[max_tracer_type_len+2];
2262 mutex_lock(&trace_types_lock);
2264 r = sprintf(buf, "%s\n", current_trace->name);
2266 r = sprintf(buf, "\n");
2267 mutex_unlock(&trace_types_lock);
2269 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2272 int tracer_init(struct tracer *t, struct trace_array *tr)
2274 tracing_reset_online_cpus(tr);
2278 struct trace_option_dentry;
2280 static struct trace_option_dentry *
2281 create_trace_option_files(struct tracer *tracer);
2284 destroy_trace_option_files(struct trace_option_dentry *topts);
2286 static int tracing_set_tracer(const char *buf)
2288 static struct trace_option_dentry *topts;
2289 struct trace_array *tr = &global_trace;
2293 mutex_lock(&trace_types_lock);
2294 for (t = trace_types; t; t = t->next) {
2295 if (strcmp(t->name, buf) == 0)
2302 if (t == current_trace)
2305 trace_branch_disable();
2306 if (current_trace && current_trace->reset)
2307 current_trace->reset(tr);
2309 destroy_trace_option_files(topts);
2313 topts = create_trace_option_files(current_trace);
2316 ret = tracer_init(t, tr);
2321 trace_branch_enable(tr);
2323 mutex_unlock(&trace_types_lock);
2329 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2330 size_t cnt, loff_t *ppos)
2332 char buf[max_tracer_type_len+1];
2339 if (cnt > max_tracer_type_len)
2340 cnt = max_tracer_type_len;
2342 if (copy_from_user(&buf, ubuf, cnt))
2347 /* strip ending whitespace. */
2348 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2351 err = tracing_set_tracer(buf);
2361 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2362 size_t cnt, loff_t *ppos)
2364 unsigned long *ptr = filp->private_data;
2368 r = snprintf(buf, sizeof(buf), "%ld\n",
2369 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2370 if (r > sizeof(buf))
2372 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2376 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2377 size_t cnt, loff_t *ppos)
2379 unsigned long *ptr = filp->private_data;
2384 if (cnt >= sizeof(buf))
2387 if (copy_from_user(&buf, ubuf, cnt))
2392 ret = strict_strtoul(buf, 10, &val);
2401 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2403 long cpu_file = (long) inode->i_private;
2404 struct trace_iterator *iter;
2407 if (tracing_disabled)
2410 mutex_lock(&trace_types_lock);
2412 /* We only allow one reader per cpu */
2413 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2414 if (!cpumask_empty(tracing_reader_cpumask)) {
2418 cpumask_setall(tracing_reader_cpumask);
2420 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2421 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2428 /* create a buffer to store the information to pass to userspace */
2429 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2436 * We make a copy of the current tracer to avoid concurrent
2437 * changes on it while we are reading.
2439 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2445 *iter->trace = *current_trace;
2447 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2452 /* trace pipe does not show start of buffer */
2453 cpumask_setall(iter->started);
2455 iter->cpu_file = cpu_file;
2456 iter->tr = &global_trace;
2457 mutex_init(&iter->mutex);
2458 filp->private_data = iter;
2460 if (iter->trace->pipe_open)
2461 iter->trace->pipe_open(iter);
2464 mutex_unlock(&trace_types_lock);
2470 mutex_unlock(&trace_types_lock);
2474 static int tracing_release_pipe(struct inode *inode, struct file *file)
2476 struct trace_iterator *iter = file->private_data;
2478 mutex_lock(&trace_types_lock);
2480 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2481 cpumask_clear(tracing_reader_cpumask);
2483 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2485 mutex_unlock(&trace_types_lock);
2487 free_cpumask_var(iter->started);
2488 mutex_destroy(&iter->mutex);
2496 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2498 struct trace_iterator *iter = filp->private_data;
2500 if (trace_flags & TRACE_ITER_BLOCK) {
2502 * Always select as readable when in blocking mode
2504 return POLLIN | POLLRDNORM;
2506 if (!trace_empty(iter))
2507 return POLLIN | POLLRDNORM;
2508 poll_wait(filp, &trace_wait, poll_table);
2509 if (!trace_empty(iter))
2510 return POLLIN | POLLRDNORM;
2517 void default_wait_pipe(struct trace_iterator *iter)
2521 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2523 if (trace_empty(iter))
2526 finish_wait(&trace_wait, &wait);
2530 * This is a make-shift waitqueue.
2531 * A tracer might use this callback on some rare cases:
2533 * 1) the current tracer might hold the runqueue lock when it wakes up
2534 * a reader, hence a deadlock (sched, function, and function graph tracers)
2535 * 2) the function tracers, trace all functions, we don't want
2536 * the overhead of calling wake_up and friends
2537 * (and tracing them too)
2539 * Anyway, this is really very primitive wakeup.
2541 void poll_wait_pipe(struct trace_iterator *iter)
2543 set_current_state(TASK_INTERRUPTIBLE);
2544 /* sleep for 100 msecs, and try again. */
2545 schedule_timeout(HZ / 10);
2548 /* Must be called with trace_types_lock mutex held. */
2549 static int tracing_wait_pipe(struct file *filp)
2551 struct trace_iterator *iter = filp->private_data;
2553 while (trace_empty(iter)) {
2555 if ((filp->f_flags & O_NONBLOCK)) {
2559 mutex_unlock(&iter->mutex);
2561 iter->trace->wait_pipe(iter);
2563 mutex_lock(&iter->mutex);
2565 if (signal_pending(current))
2569 * We block until we read something and tracing is disabled.
2570 * We still block if tracing is disabled, but we have never
2571 * read anything. This allows a user to cat this file, and
2572 * then enable tracing. But after we have read something,
2573 * we give an EOF when tracing is again disabled.
2575 * iter->pos will be 0 if we haven't read anything.
2577 if (!tracer_enabled && iter->pos)
2588 tracing_read_pipe(struct file *filp, char __user *ubuf,
2589 size_t cnt, loff_t *ppos)
2591 struct trace_iterator *iter = filp->private_data;
2592 static struct tracer *old_tracer;
2595 /* return any leftover data */
2596 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2600 trace_seq_init(&iter->seq);
2602 /* copy the tracer to avoid using a global lock all around */
2603 mutex_lock(&trace_types_lock);
2604 if (unlikely(old_tracer != current_trace && current_trace)) {
2605 old_tracer = current_trace;
2606 *iter->trace = *current_trace;
2608 mutex_unlock(&trace_types_lock);
2611 * Avoid more than one consumer on a single file descriptor
2612 * This is just a matter of traces coherency, the ring buffer itself
2615 mutex_lock(&iter->mutex);
2616 if (iter->trace->read) {
2617 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2623 sret = tracing_wait_pipe(filp);
2627 /* stop when tracing is finished */
2628 if (trace_empty(iter)) {
2633 if (cnt >= PAGE_SIZE)
2634 cnt = PAGE_SIZE - 1;
2636 /* reset all but tr, trace, and overruns */
2637 memset(&iter->seq, 0,
2638 sizeof(struct trace_iterator) -
2639 offsetof(struct trace_iterator, seq));
2642 while (find_next_entry_inc(iter) != NULL) {
2643 enum print_line_t ret;
2644 int len = iter->seq.len;
2646 ret = print_trace_line(iter);
2647 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2648 /* don't print partial lines */
2649 iter->seq.len = len;
2652 if (ret != TRACE_TYPE_NO_CONSUME)
2653 trace_consume(iter);
2655 if (iter->seq.len >= cnt)
2659 /* Now copy what we have to the user */
2660 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2661 if (iter->seq.readpos >= iter->seq.len)
2662 trace_seq_init(&iter->seq);
2665 * If there was nothing to send to user, inspite of consuming trace
2666 * entries, go back to wait for more entries.
2672 mutex_unlock(&iter->mutex);
2677 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2678 struct pipe_buffer *buf)
2680 __free_page(buf->page);
2683 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2686 __free_page(spd->pages[idx]);
2689 static struct pipe_buf_operations tracing_pipe_buf_ops = {
2691 .map = generic_pipe_buf_map,
2692 .unmap = generic_pipe_buf_unmap,
2693 .confirm = generic_pipe_buf_confirm,
2694 .release = tracing_pipe_buf_release,
2695 .steal = generic_pipe_buf_steal,
2696 .get = generic_pipe_buf_get,
2700 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
2705 /* Seq buffer is page-sized, exactly what we need. */
2707 count = iter->seq.len;
2708 ret = print_trace_line(iter);
2709 count = iter->seq.len - count;
2712 iter->seq.len -= count;
2715 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2716 iter->seq.len -= count;
2720 trace_consume(iter);
2722 if (!find_next_entry_inc(iter)) {
2732 static ssize_t tracing_splice_read_pipe(struct file *filp,
2734 struct pipe_inode_info *pipe,
2738 struct page *pages[PIPE_BUFFERS];
2739 struct partial_page partial[PIPE_BUFFERS];
2740 struct trace_iterator *iter = filp->private_data;
2741 struct splice_pipe_desc spd = {
2744 .nr_pages = 0, /* This gets updated below. */
2746 .ops = &tracing_pipe_buf_ops,
2747 .spd_release = tracing_spd_release_pipe,
2749 static struct tracer *old_tracer;
2754 /* copy the tracer to avoid using a global lock all around */
2755 mutex_lock(&trace_types_lock);
2756 if (unlikely(old_tracer != current_trace && current_trace)) {
2757 old_tracer = current_trace;
2758 *iter->trace = *current_trace;
2760 mutex_unlock(&trace_types_lock);
2762 mutex_lock(&iter->mutex);
2764 if (iter->trace->splice_read) {
2765 ret = iter->trace->splice_read(iter, filp,
2766 ppos, pipe, len, flags);
2771 ret = tracing_wait_pipe(filp);
2775 if (!iter->ent && !find_next_entry_inc(iter)) {
2780 /* Fill as many pages as possible. */
2781 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2782 pages[i] = alloc_page(GFP_KERNEL);
2786 rem = tracing_fill_pipe_page(rem, iter);
2788 /* Copy the data into the page, so we can start over. */
2789 ret = trace_seq_to_buffer(&iter->seq,
2790 page_address(pages[i]),
2793 __free_page(pages[i]);
2796 partial[i].offset = 0;
2797 partial[i].len = iter->seq.len;
2799 trace_seq_init(&iter->seq);
2802 mutex_unlock(&iter->mutex);
2806 return splice_to_pipe(pipe, &spd);
2809 mutex_unlock(&iter->mutex);
2815 tracing_entries_read(struct file *filp, char __user *ubuf,
2816 size_t cnt, loff_t *ppos)
2818 struct trace_array *tr = filp->private_data;
2822 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2823 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2827 tracing_entries_write(struct file *filp, const char __user *ubuf,
2828 size_t cnt, loff_t *ppos)
2834 if (cnt >= sizeof(buf))
2837 if (copy_from_user(&buf, ubuf, cnt))
2842 ret = strict_strtoul(buf, 10, &val);
2846 /* must have at least 1 entry */
2850 mutex_lock(&trace_types_lock);
2854 /* disable all cpu buffers */
2855 for_each_tracing_cpu(cpu) {
2856 if (global_trace.data[cpu])
2857 atomic_inc(&global_trace.data[cpu]->disabled);
2858 if (max_tr.data[cpu])
2859 atomic_inc(&max_tr.data[cpu]->disabled);
2862 /* value is in KB */
2865 if (val != global_trace.entries) {
2866 ret = ring_buffer_resize(global_trace.buffer, val);
2872 ret = ring_buffer_resize(max_tr.buffer, val);
2876 r = ring_buffer_resize(global_trace.buffer,
2877 global_trace.entries);
2879 /* AARGH! We are left with different
2880 * size max buffer!!!! */
2882 tracing_disabled = 1;
2887 global_trace.entries = val;
2892 /* If check pages failed, return ENOMEM */
2893 if (tracing_disabled)
2896 for_each_tracing_cpu(cpu) {
2897 if (global_trace.data[cpu])
2898 atomic_dec(&global_trace.data[cpu]->disabled);
2899 if (max_tr.data[cpu])
2900 atomic_dec(&max_tr.data[cpu]->disabled);
2904 max_tr.entries = global_trace.entries;
2905 mutex_unlock(&trace_types_lock);
2910 static int mark_printk(const char *fmt, ...)
2914 va_start(args, fmt);
2915 ret = trace_vprintk(0, -1, fmt, args);
2921 tracing_mark_write(struct file *filp, const char __user *ubuf,
2922 size_t cnt, loff_t *fpos)
2927 if (tracing_disabled)
2930 if (cnt > TRACE_BUF_SIZE)
2931 cnt = TRACE_BUF_SIZE;
2933 buf = kmalloc(cnt + 1, GFP_KERNEL);
2937 if (copy_from_user(buf, ubuf, cnt)) {
2942 /* Cut from the first nil or newline. */
2944 end = strchr(buf, '\n');
2948 cnt = mark_printk("%s\n", buf);
2955 static struct file_operations tracing_max_lat_fops = {
2956 .open = tracing_open_generic,
2957 .read = tracing_max_lat_read,
2958 .write = tracing_max_lat_write,
2961 static struct file_operations tracing_ctrl_fops = {
2962 .open = tracing_open_generic,
2963 .read = tracing_ctrl_read,
2964 .write = tracing_ctrl_write,
2967 static struct file_operations set_tracer_fops = {
2968 .open = tracing_open_generic,
2969 .read = tracing_set_trace_read,
2970 .write = tracing_set_trace_write,
2973 static struct file_operations tracing_pipe_fops = {
2974 .open = tracing_open_pipe,
2975 .poll = tracing_poll_pipe,
2976 .read = tracing_read_pipe,
2977 .splice_read = tracing_splice_read_pipe,
2978 .release = tracing_release_pipe,
2981 static struct file_operations tracing_entries_fops = {
2982 .open = tracing_open_generic,
2983 .read = tracing_entries_read,
2984 .write = tracing_entries_write,
2987 static struct file_operations tracing_mark_fops = {
2988 .open = tracing_open_generic,
2989 .write = tracing_mark_write,
2992 struct ftrace_buffer_info {
2993 struct trace_array *tr;
2999 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3001 int cpu = (int)(long)inode->i_private;
3002 struct ftrace_buffer_info *info;
3004 if (tracing_disabled)
3007 info = kzalloc(sizeof(*info), GFP_KERNEL);
3011 info->tr = &global_trace;
3013 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3014 /* Force reading ring buffer for first read */
3015 info->read = (unsigned int)-1;
3019 filp->private_data = info;
3029 tracing_buffers_read(struct file *filp, char __user *ubuf,
3030 size_t count, loff_t *ppos)
3032 struct ftrace_buffer_info *info = filp->private_data;
3040 /* Do we have previous read data to read? */
3041 if (info->read < PAGE_SIZE)
3046 ret = ring_buffer_read_page(info->tr->buffer,
3053 pos = ring_buffer_page_len(info->spare);
3055 if (pos < PAGE_SIZE)
3056 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3059 size = PAGE_SIZE - info->read;
3063 ret = copy_to_user(ubuf, info->spare + info->read, size);
3074 static int tracing_buffers_release(struct inode *inode, struct file *file)
3076 struct ftrace_buffer_info *info = file->private_data;
3078 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3085 struct ring_buffer *buffer;
3090 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3091 struct pipe_buffer *buf)
3093 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3098 ring_buffer_free_read_page(ref->buffer, ref->page);
3103 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3104 struct pipe_buffer *buf)
3109 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3110 struct pipe_buffer *buf)
3112 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3117 /* Pipe buffer operations for a buffer. */
3118 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3120 .map = generic_pipe_buf_map,
3121 .unmap = generic_pipe_buf_unmap,
3122 .confirm = generic_pipe_buf_confirm,
3123 .release = buffer_pipe_buf_release,
3124 .steal = buffer_pipe_buf_steal,
3125 .get = buffer_pipe_buf_get,
3129 * Callback from splice_to_pipe(), if we need to release some pages
3130 * at the end of the spd in case we error'ed out in filling the pipe.
3132 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3134 struct buffer_ref *ref =
3135 (struct buffer_ref *)spd->partial[i].private;
3140 ring_buffer_free_read_page(ref->buffer, ref->page);
3142 spd->partial[i].private = 0;
3146 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3147 struct pipe_inode_info *pipe, size_t len,
3150 struct ftrace_buffer_info *info = file->private_data;
3151 struct partial_page partial[PIPE_BUFFERS];
3152 struct page *pages[PIPE_BUFFERS];
3153 struct splice_pipe_desc spd = {
3157 .ops = &buffer_pipe_buf_ops,
3158 .spd_release = buffer_spd_release,
3160 struct buffer_ref *ref;
3165 * We can't seek on a buffer input
3167 if (unlikely(*ppos))
3171 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3175 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3179 ref->buffer = info->tr->buffer;
3180 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3186 r = ring_buffer_read_page(ref->buffer, &ref->page,
3189 ring_buffer_free_read_page(ref->buffer,
3196 * zero out any left over data, this is going to
3199 size = ring_buffer_page_len(ref->page);
3200 if (size < PAGE_SIZE)
3201 memset(ref->page + size, 0, PAGE_SIZE - size);
3203 page = virt_to_page(ref->page);
3205 spd.pages[i] = page;
3206 spd.partial[i].len = PAGE_SIZE;
3207 spd.partial[i].offset = 0;
3208 spd.partial[i].private = (unsigned long)ref;
3214 /* did we read anything? */
3215 if (!spd.nr_pages) {
3216 if (flags & SPLICE_F_NONBLOCK)
3224 ret = splice_to_pipe(pipe, &spd);
3229 static const struct file_operations tracing_buffers_fops = {
3230 .open = tracing_buffers_open,
3231 .read = tracing_buffers_read,
3232 .release = tracing_buffers_release,
3233 .splice_read = tracing_buffers_splice_read,
3234 .llseek = no_llseek,
3237 #ifdef CONFIG_DYNAMIC_FTRACE
3239 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3245 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3246 size_t cnt, loff_t *ppos)
3248 static char ftrace_dyn_info_buffer[1024];
3249 static DEFINE_MUTEX(dyn_info_mutex);
3250 unsigned long *p = filp->private_data;
3251 char *buf = ftrace_dyn_info_buffer;
3252 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3255 mutex_lock(&dyn_info_mutex);
3256 r = sprintf(buf, "%ld ", *p);
3258 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3261 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3263 mutex_unlock(&dyn_info_mutex);
3268 static struct file_operations tracing_dyn_info_fops = {
3269 .open = tracing_open_generic,
3270 .read = tracing_read_dyn_info,
3274 static struct dentry *d_tracer;
3276 struct dentry *tracing_init_dentry(void)
3283 d_tracer = debugfs_create_dir("tracing", NULL);
3285 if (!d_tracer && !once) {
3287 pr_warning("Could not create debugfs directory 'tracing'\n");
3294 static struct dentry *d_percpu;
3296 struct dentry *tracing_dentry_percpu(void)
3299 struct dentry *d_tracer;
3304 d_tracer = tracing_init_dentry();
3309 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3311 if (!d_percpu && !once) {
3313 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3320 static void tracing_init_debugfs_percpu(long cpu)
3322 struct dentry *d_percpu = tracing_dentry_percpu();
3323 struct dentry *entry, *d_cpu;
3324 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3327 if (cpu > 999 || cpu < 0)
3330 sprintf(cpu_dir, "cpu%ld", cpu);
3331 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3333 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3337 /* per cpu trace_pipe */
3338 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
3339 (void *) cpu, &tracing_pipe_fops);
3341 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
3344 entry = debugfs_create_file("trace", 0444, d_cpu,
3345 (void *) cpu, &tracing_fops);
3347 pr_warning("Could not create debugfs 'trace' entry\n");
3350 #ifdef CONFIG_FTRACE_SELFTEST
3351 /* Let selftest have access to static functions in this file */
3352 #include "trace_selftest.c"
3355 struct trace_option_dentry {
3356 struct tracer_opt *opt;
3357 struct tracer_flags *flags;
3358 struct dentry *entry;
3362 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3365 struct trace_option_dentry *topt = filp->private_data;
3368 if (topt->flags->val & topt->opt->bit)
3373 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3377 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3380 struct trace_option_dentry *topt = filp->private_data;
3385 if (cnt >= sizeof(buf))
3388 if (copy_from_user(&buf, ubuf, cnt))
3393 ret = strict_strtoul(buf, 10, &val);
3400 /* do nothing if already cleared */
3401 if (!(topt->flags->val & topt->opt->bit))
3404 mutex_lock(&trace_types_lock);
3405 if (current_trace->set_flag)
3406 ret = current_trace->set_flag(topt->flags->val,
3408 mutex_unlock(&trace_types_lock);
3411 topt->flags->val &= ~topt->opt->bit;
3414 /* do nothing if already set */
3415 if (topt->flags->val & topt->opt->bit)
3418 mutex_lock(&trace_types_lock);
3419 if (current_trace->set_flag)
3420 ret = current_trace->set_flag(topt->flags->val,
3422 mutex_unlock(&trace_types_lock);
3425 topt->flags->val |= topt->opt->bit;
3438 static const struct file_operations trace_options_fops = {
3439 .open = tracing_open_generic,
3440 .read = trace_options_read,
3441 .write = trace_options_write,
3445 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3448 long index = (long)filp->private_data;
3451 if (trace_flags & (1 << index))
3456 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3460 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3463 long index = (long)filp->private_data;
3468 if (cnt >= sizeof(buf))
3471 if (copy_from_user(&buf, ubuf, cnt))
3476 ret = strict_strtoul(buf, 10, &val);
3482 trace_flags &= ~(1 << index);
3485 trace_flags |= 1 << index;
3497 static const struct file_operations trace_options_core_fops = {
3498 .open = tracing_open_generic,
3499 .read = trace_options_core_read,
3500 .write = trace_options_core_write,
3503 static struct dentry *trace_options_init_dentry(void)
3505 struct dentry *d_tracer;
3506 static struct dentry *t_options;
3511 d_tracer = tracing_init_dentry();
3515 t_options = debugfs_create_dir("options", d_tracer);
3517 pr_warning("Could not create debugfs directory 'options'\n");
3525 create_trace_option_file(struct trace_option_dentry *topt,
3526 struct tracer_flags *flags,
3527 struct tracer_opt *opt)
3529 struct dentry *t_options;
3530 struct dentry *entry;
3532 t_options = trace_options_init_dentry();
3536 topt->flags = flags;
3539 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3540 &trace_options_fops);
3542 topt->entry = entry;
3546 static struct trace_option_dentry *
3547 create_trace_option_files(struct tracer *tracer)
3549 struct trace_option_dentry *topts;
3550 struct tracer_flags *flags;
3551 struct tracer_opt *opts;
3557 flags = tracer->flags;
3559 if (!flags || !flags->opts)
3564 for (cnt = 0; opts[cnt].name; cnt++)
3567 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
3571 for (cnt = 0; opts[cnt].name; cnt++)
3572 create_trace_option_file(&topts[cnt], flags,
3579 destroy_trace_option_files(struct trace_option_dentry *topts)
3586 for (cnt = 0; topts[cnt].opt; cnt++) {
3587 if (topts[cnt].entry)
3588 debugfs_remove(topts[cnt].entry);
3594 static struct dentry *
3595 create_trace_option_core_file(const char *option, long index)
3597 struct dentry *t_options;
3598 struct dentry *entry;
3600 t_options = trace_options_init_dentry();
3604 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3605 &trace_options_core_fops);
3610 static __init void create_trace_options_dir(void)
3612 struct dentry *t_options;
3613 struct dentry *entry;
3616 t_options = trace_options_init_dentry();
3620 for (i = 0; trace_options[i]; i++) {
3621 entry = create_trace_option_core_file(trace_options[i], i);
3623 pr_warning("Could not create debugfs %s entry\n",
3628 static __init int tracer_init_debugfs(void)
3630 struct dentry *d_tracer;
3631 struct dentry *buffers;
3632 struct dentry *entry;
3635 d_tracer = tracing_init_dentry();
3637 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3638 &global_trace, &tracing_ctrl_fops);
3640 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3642 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3643 NULL, &tracing_iter_fops);
3645 pr_warning("Could not create debugfs 'trace_options' entry\n");
3647 create_trace_options_dir();
3649 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3650 NULL, &tracing_cpumask_fops);
3652 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3654 entry = debugfs_create_file("trace", 0444, d_tracer,
3655 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
3657 pr_warning("Could not create debugfs 'trace' entry\n");
3659 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3660 &global_trace, &show_traces_fops);
3662 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3664 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3665 &global_trace, &set_tracer_fops);
3667 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3669 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3670 &tracing_max_latency,
3671 &tracing_max_lat_fops);
3673 pr_warning("Could not create debugfs "
3674 "'tracing_max_latency' entry\n");
3676 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3677 &tracing_thresh, &tracing_max_lat_fops);
3679 pr_warning("Could not create debugfs "
3680 "'tracing_thresh' entry\n");
3681 entry = debugfs_create_file("README", 0644, d_tracer,
3682 NULL, &tracing_readme_fops);
3684 pr_warning("Could not create debugfs 'README' entry\n");
3686 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3687 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
3689 pr_warning("Could not create debugfs "
3690 "'trace_pipe' entry\n");
3692 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3693 &global_trace, &tracing_entries_fops);
3695 pr_warning("Could not create debugfs "
3696 "'buffer_size_kb' entry\n");
3698 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3699 NULL, &tracing_mark_fops);
3701 pr_warning("Could not create debugfs "
3702 "'trace_marker' entry\n");
3704 buffers = debugfs_create_dir("binary_buffers", d_tracer);
3707 pr_warning("Could not create buffers directory\n");
3712 for_each_tracing_cpu(cpu) {
3713 sprintf(buf, "%d", cpu);
3715 entry = debugfs_create_file(buf, 0444, buffers,
3717 &tracing_buffers_fops);
3719 pr_warning("Could not create debugfs buffers "
3720 "'%s' entry\n", buf);
3724 #ifdef CONFIG_DYNAMIC_FTRACE
3725 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3726 &ftrace_update_tot_cnt,
3727 &tracing_dyn_info_fops);
3729 pr_warning("Could not create debugfs "
3730 "'dyn_ftrace_total_info' entry\n");
3732 #ifdef CONFIG_SYSPROF_TRACER
3733 init_tracer_sysprof_debugfs(d_tracer);
3736 for_each_tracing_cpu(cpu)
3737 tracing_init_debugfs_percpu(cpu);
3742 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
3744 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
3745 static char trace_buf[TRACE_BUF_SIZE];
3747 struct ring_buffer_event *event;
3748 struct trace_array *tr = &global_trace;
3749 struct trace_array_cpu *data;
3750 int cpu, len = 0, size, pc;
3751 struct print_entry *entry;
3752 unsigned long irq_flags;
3754 if (tracing_disabled || tracing_selftest_running)
3757 pc = preempt_count();
3758 preempt_disable_notrace();
3759 cpu = raw_smp_processor_id();
3760 data = tr->data[cpu];
3762 if (unlikely(atomic_read(&data->disabled)))
3765 pause_graph_tracing();
3766 raw_local_irq_save(irq_flags);
3767 __raw_spin_lock(&trace_buf_lock);
3768 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3770 len = min(len, TRACE_BUF_SIZE-1);
3773 size = sizeof(*entry) + len + 1;
3774 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
3777 entry = ring_buffer_event_data(event);
3779 entry->depth = depth;
3781 memcpy(&entry->buf, trace_buf, len);
3782 entry->buf[len] = 0;
3783 ring_buffer_unlock_commit(tr->buffer, event);
3786 __raw_spin_unlock(&trace_buf_lock);
3787 raw_local_irq_restore(irq_flags);
3788 unpause_graph_tracing();
3790 preempt_enable_notrace();
3794 EXPORT_SYMBOL_GPL(trace_vprintk);
3796 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3801 if (!(trace_flags & TRACE_ITER_PRINTK))
3805 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3809 EXPORT_SYMBOL_GPL(__ftrace_printk);
3811 int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
3813 if (!(trace_flags & TRACE_ITER_PRINTK))
3816 return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3818 EXPORT_SYMBOL_GPL(__ftrace_vprintk);
3820 static int trace_panic_handler(struct notifier_block *this,
3821 unsigned long event, void *unused)
3823 if (ftrace_dump_on_oops)
3828 static struct notifier_block trace_panic_notifier = {
3829 .notifier_call = trace_panic_handler,
3831 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3834 static int trace_die_handler(struct notifier_block *self,
3840 if (ftrace_dump_on_oops)
3849 static struct notifier_block trace_die_notifier = {
3850 .notifier_call = trace_die_handler,
3855 * printk is set to max of 1024, we really don't need it that big.
3856 * Nothing should be printing 1000 characters anyway.
3858 #define TRACE_MAX_PRINT 1000
3861 * Define here KERN_TRACE so that we have one place to modify
3862 * it if we decide to change what log level the ftrace dump
3865 #define KERN_TRACE KERN_EMERG
3868 trace_printk_seq(struct trace_seq *s)
3870 /* Probably should print a warning here. */
3874 /* should be zero ended, but we are paranoid. */
3875 s->buffer[s->len] = 0;
3877 printk(KERN_TRACE "%s", s->buffer);
3882 void ftrace_dump(void)
3884 static DEFINE_SPINLOCK(ftrace_dump_lock);
3885 /* use static because iter can be a bit big for the stack */
3886 static struct trace_iterator iter;
3887 static int dump_ran;
3888 unsigned long flags;
3892 spin_lock_irqsave(&ftrace_dump_lock, flags);
3898 /* No turning back! */
3902 for_each_tracing_cpu(cpu) {
3903 atomic_inc(&global_trace.data[cpu]->disabled);
3906 /* don't look at user memory in panic mode */
3907 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3909 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3911 /* Simulate the iterator */
3912 iter.tr = &global_trace;
3913 iter.trace = current_trace;
3914 iter.cpu_file = TRACE_PIPE_ALL_CPU;
3917 * We need to stop all tracing on all CPUS to read the
3918 * the next buffer. This is a bit expensive, but is
3919 * not done often. We fill all what we can read,
3920 * and then release the locks again.
3923 while (!trace_empty(&iter)) {
3926 printk(KERN_TRACE "---------------------------------\n");
3930 /* reset all but tr, trace, and overruns */
3931 memset(&iter.seq, 0,
3932 sizeof(struct trace_iterator) -
3933 offsetof(struct trace_iterator, seq));
3934 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3937 if (find_next_entry_inc(&iter) != NULL) {
3938 print_trace_line(&iter);
3939 trace_consume(&iter);
3942 trace_printk_seq(&iter.seq);
3946 printk(KERN_TRACE " (ftrace buffer empty)\n");
3948 printk(KERN_TRACE "---------------------------------\n");
3951 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3954 __init static int tracer_alloc_buffers(void)
3956 struct trace_array_cpu *data;
3960 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3963 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3964 goto out_free_buffer_mask;
3966 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
3967 goto out_free_tracing_cpumask;
3969 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3970 cpumask_copy(tracing_cpumask, cpu_all_mask);
3971 cpumask_clear(tracing_reader_cpumask);
3973 /* TODO: make the number of buffers hot pluggable with CPUS */
3974 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3975 TRACE_BUFFER_FLAGS);
3976 if (!global_trace.buffer) {
3977 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3979 goto out_free_cpumask;
3981 global_trace.entries = ring_buffer_size(global_trace.buffer);
3984 #ifdef CONFIG_TRACER_MAX_TRACE
3985 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3986 TRACE_BUFFER_FLAGS);
3987 if (!max_tr.buffer) {
3988 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3990 ring_buffer_free(global_trace.buffer);
3991 goto out_free_cpumask;
3993 max_tr.entries = ring_buffer_size(max_tr.buffer);
3994 WARN_ON(max_tr.entries != global_trace.entries);
3997 /* Allocate the first page for all buffers */
3998 for_each_tracing_cpu(i) {
3999 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4000 max_tr.data[i] = &per_cpu(max_data, i);
4003 trace_init_cmdlines();
4005 register_tracer(&nop_trace);
4006 current_trace = &nop_trace;
4007 #ifdef CONFIG_BOOT_TRACER
4008 register_tracer(&boot_tracer);
4010 /* All seems OK, enable tracing */
4011 tracing_disabled = 0;
4013 atomic_notifier_chain_register(&panic_notifier_list,
4014 &trace_panic_notifier);
4016 register_die_notifier(&trace_die_notifier);
4020 free_cpumask_var(tracing_reader_cpumask);
4021 out_free_tracing_cpumask:
4022 free_cpumask_var(tracing_cpumask);
4023 out_free_buffer_mask:
4024 free_cpumask_var(tracing_buffer_mask);
4029 __init static int clear_boot_tracer(void)
4032 * The default tracer at boot buffer is an init section.
4033 * This function is called in lateinit. If we did not
4034 * find the boot tracer, then clear it out, to prevent
4035 * later registration from accessing the buffer that is
4036 * about to be freed.
4038 if (!default_bootup_tracer)
4041 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4042 default_bootup_tracer);
4043 default_bootup_tracer = NULL;
4048 early_initcall(tracer_alloc_buffers);
4049 fs_initcall(tracer_init_debugfs);
4050 late_initcall(clear_boot_tracer);