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>
35 #include <linux/stacktrace.h>
36 #include <linux/ring_buffer.h>
37 #include <linux/irqflags.h>
40 #include "trace_output.h"
42 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
44 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
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;
56 /* For tracers that don't implement custom flags */
57 static struct tracer_opt dummy_tracer_opt[] = {
61 static struct tracer_flags dummy_tracer_flags = {
63 .opts = dummy_tracer_opt
66 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
72 * Kill all tracing for good (never come back).
73 * It is initialized to 1 but will turn to zero if the initialization
74 * of the tracer is successful. But that is the only place that sets
77 int tracing_disabled = 1;
79 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
81 static inline void ftrace_disable_cpu(void)
84 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
87 static inline void ftrace_enable_cpu(void)
89 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
93 static cpumask_var_t __read_mostly tracing_buffer_mask;
95 #define for_each_tracing_cpu(cpu) \
96 for_each_cpu(cpu, tracing_buffer_mask)
99 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
101 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
102 * is set, then ftrace_dump is called. This will output the contents
103 * of the ftrace buffers to the console. This is very useful for
104 * capturing traces that lead to crashes and outputing it to a
107 * It is default off, but you can enable it with either specifying
108 * "ftrace_dump_on_oops" in the kernel command line, or setting
109 * /proc/sys/kernel/ftrace_dump_on_oops to true.
111 int ftrace_dump_on_oops;
113 static int tracing_set_tracer(char *buf);
115 static int __init set_ftrace(char *str)
117 tracing_set_tracer(str);
120 __setup("ftrace", set_ftrace);
122 static int __init set_ftrace_dump_on_oops(char *str)
124 ftrace_dump_on_oops = 1;
127 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
130 ns2usecs(cycle_t nsec)
137 cycle_t ftrace_now(int cpu)
139 u64 ts = ring_buffer_time_stamp(cpu);
140 ring_buffer_normalize_time_stamp(cpu, &ts);
145 * The global_trace is the descriptor that holds the tracing
146 * buffers for the live tracing. For each CPU, it contains
147 * a link list of pages that will store trace entries. The
148 * page descriptor of the pages in the memory is used to hold
149 * the link list by linking the lru item in the page descriptor
150 * to each of the pages in the buffer per CPU.
152 * For each active CPU there is a data field that holds the
153 * pages for the buffer for that CPU. Each CPU has the same number
154 * of pages allocated for its buffer.
156 static struct trace_array global_trace;
158 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
161 * The max_tr is used to snapshot the global_trace when a maximum
162 * latency is reached. Some tracers will use this to store a maximum
163 * trace while it continues examining live traces.
165 * The buffers for the max_tr are set up the same as the global_trace.
166 * When a snapshot is taken, the link list of the max_tr is swapped
167 * with the link list of the global_trace and the buffers are reset for
168 * the global_trace so the tracing can continue.
170 static struct trace_array max_tr;
172 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
174 /* tracer_enabled is used to toggle activation of a tracer */
175 static int tracer_enabled = 1;
178 * tracing_is_enabled - return tracer_enabled status
180 * This function is used by other tracers to know the status
181 * of the tracer_enabled flag. Tracers may use this function
182 * to know if it should enable their features when starting
183 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
185 int tracing_is_enabled(void)
187 return tracer_enabled;
190 /* function tracing enabled */
191 int ftrace_function_enabled;
194 * trace_buf_size is the size in bytes that is allocated
195 * for a buffer. Note, the number of bytes is always rounded
198 * This number is purposely set to a low number of 16384.
199 * If the dump on oops happens, it will be much appreciated
200 * to not have to wait for all that output. Anyway this can be
201 * boot time and run time configurable.
203 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
205 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
207 /* trace_types holds a link list of available tracers. */
208 static struct tracer *trace_types __read_mostly;
210 /* current_trace points to the tracer that is currently active */
211 static struct tracer *current_trace __read_mostly;
214 * max_tracer_type_len is used to simplify the allocating of
215 * buffers to read userspace tracer names. We keep track of
216 * the longest tracer name registered.
218 static int max_tracer_type_len;
221 * trace_types_lock is used to protect the trace_types list.
222 * This lock is also used to keep user access serialized.
223 * Accesses from userspace will grab this lock while userspace
224 * activities happen inside the kernel.
226 static DEFINE_MUTEX(trace_types_lock);
228 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
229 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
231 /* trace_flags holds trace_options default values */
232 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
236 * trace_wake_up - wake up tasks waiting for trace input
238 * Simply wakes up any task that is blocked on the trace_wait
239 * queue. These is used with trace_poll for tasks polling the trace.
241 void trace_wake_up(void)
244 * The runqueue_is_locked() can fail, but this is the best we
247 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
248 wake_up(&trace_wait);
251 static int __init set_buf_size(char *str)
253 unsigned long buf_size;
258 ret = strict_strtoul(str, 0, &buf_size);
259 /* nr_entries can not be zero */
260 if (ret < 0 || buf_size == 0)
262 trace_buf_size = buf_size;
265 __setup("trace_buf_size=", set_buf_size);
267 unsigned long nsecs_to_usecs(unsigned long nsecs)
272 /* These must match the bit postions in trace_iterator_flags */
273 static const char *trace_options[] = {
295 * ftrace_max_lock is used to protect the swapping of buffers
296 * when taking a max snapshot. The buffers themselves are
297 * protected by per_cpu spinlocks. But the action of the swap
298 * needs its own lock.
300 * This is defined as a raw_spinlock_t in order to help
301 * with performance when lockdep debugging is enabled.
303 static raw_spinlock_t ftrace_max_lock =
304 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
307 * Copy the new maximum trace into the separate maximum-trace
308 * structure. (this way the maximum trace is permanently saved,
309 * for later retrieval via /debugfs/tracing/latency_trace)
312 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
314 struct trace_array_cpu *data = tr->data[cpu];
317 max_tr.time_start = data->preempt_timestamp;
319 data = max_tr.data[cpu];
320 data->saved_latency = tracing_max_latency;
322 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
323 data->pid = tsk->pid;
324 data->uid = task_uid(tsk);
325 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
326 data->policy = tsk->policy;
327 data->rt_priority = tsk->rt_priority;
329 /* record this tasks comm */
330 tracing_record_cmdline(current);
334 trace_seq_reset(struct trace_seq *s)
340 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
345 if (s->len <= s->readpos)
348 len = s->len - s->readpos;
351 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
360 trace_print_seq(struct seq_file *m, struct trace_seq *s)
362 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
365 seq_puts(m, s->buffer);
371 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
373 * @tsk: the task with the latency
374 * @cpu: The cpu that initiated the trace.
376 * Flip the buffers between the @tr and the max_tr and record information
377 * about which task was the cause of this latency.
380 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
382 struct ring_buffer *buf = tr->buffer;
384 WARN_ON_ONCE(!irqs_disabled());
385 __raw_spin_lock(&ftrace_max_lock);
387 tr->buffer = max_tr.buffer;
390 ftrace_disable_cpu();
391 ring_buffer_reset(tr->buffer);
394 __update_max_tr(tr, tsk, cpu);
395 __raw_spin_unlock(&ftrace_max_lock);
399 * update_max_tr_single - only copy one trace over, and reset the rest
401 * @tsk - task with the latency
402 * @cpu - the cpu of the buffer to copy.
404 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
407 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
411 WARN_ON_ONCE(!irqs_disabled());
412 __raw_spin_lock(&ftrace_max_lock);
414 ftrace_disable_cpu();
416 ring_buffer_reset(max_tr.buffer);
417 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
423 __update_max_tr(tr, tsk, cpu);
424 __raw_spin_unlock(&ftrace_max_lock);
428 * register_tracer - register a tracer with the ftrace system.
429 * @type - the plugin for the tracer
431 * Register a new plugin tracer.
433 int register_tracer(struct tracer *type)
440 pr_info("Tracer must have a name\n");
445 * When this gets called we hold the BKL which means that
446 * preemption is disabled. Various trace selftests however
447 * need to disable and enable preemption for successful tests.
448 * So we drop the BKL here and grab it after the tests again.
451 mutex_lock(&trace_types_lock);
453 tracing_selftest_running = true;
455 for (t = trace_types; t; t = t->next) {
456 if (strcmp(type->name, t->name) == 0) {
458 pr_info("Trace %s already registered\n",
466 type->set_flag = &dummy_set_flag;
468 type->flags = &dummy_tracer_flags;
470 if (!type->flags->opts)
471 type->flags->opts = dummy_tracer_opt;
473 #ifdef CONFIG_FTRACE_STARTUP_TEST
474 if (type->selftest) {
475 struct tracer *saved_tracer = current_trace;
476 struct trace_array *tr = &global_trace;
480 * Run a selftest on this tracer.
481 * Here we reset the trace buffer, and set the current
482 * tracer to be this tracer. The tracer can then run some
483 * internal tracing to verify that everything is in order.
484 * If we fail, we do not register this tracer.
486 for_each_tracing_cpu(i)
487 tracing_reset(tr, i);
489 current_trace = type;
490 /* the test is responsible for initializing and enabling */
491 pr_info("Testing tracer %s: ", type->name);
492 ret = type->selftest(type, tr);
493 /* the test is responsible for resetting too */
494 current_trace = saved_tracer;
496 printk(KERN_CONT "FAILED!\n");
499 /* Only reset on passing, to avoid touching corrupted buffers */
500 for_each_tracing_cpu(i)
501 tracing_reset(tr, i);
503 printk(KERN_CONT "PASSED\n");
507 type->next = trace_types;
509 len = strlen(type->name);
510 if (len > max_tracer_type_len)
511 max_tracer_type_len = len;
514 tracing_selftest_running = false;
515 mutex_unlock(&trace_types_lock);
521 void unregister_tracer(struct tracer *type)
526 mutex_lock(&trace_types_lock);
527 for (t = &trace_types; *t; t = &(*t)->next) {
531 pr_info("Trace %s not registered\n", type->name);
536 if (strlen(type->name) != max_tracer_type_len)
539 max_tracer_type_len = 0;
540 for (t = &trace_types; *t; t = &(*t)->next) {
541 len = strlen((*t)->name);
542 if (len > max_tracer_type_len)
543 max_tracer_type_len = len;
546 mutex_unlock(&trace_types_lock);
549 void tracing_reset(struct trace_array *tr, int cpu)
551 ftrace_disable_cpu();
552 ring_buffer_reset_cpu(tr->buffer, cpu);
556 void tracing_reset_online_cpus(struct trace_array *tr)
560 tr->time_start = ftrace_now(tr->cpu);
562 for_each_online_cpu(cpu)
563 tracing_reset(tr, cpu);
566 #define SAVED_CMDLINES 128
567 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
568 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
569 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
570 static int cmdline_idx;
571 static DEFINE_SPINLOCK(trace_cmdline_lock);
573 /* temporary disable recording */
574 atomic_t trace_record_cmdline_disabled __read_mostly;
576 static void trace_init_cmdlines(void)
578 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
579 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
583 static int trace_stop_count;
584 static DEFINE_SPINLOCK(tracing_start_lock);
587 * ftrace_off_permanent - disable all ftrace code permanently
589 * This should only be called when a serious anomally has
590 * been detected. This will turn off the function tracing,
591 * ring buffers, and other tracing utilites. It takes no
592 * locks and can be called from any context.
594 void ftrace_off_permanent(void)
596 tracing_disabled = 1;
598 tracing_off_permanent();
602 * tracing_start - quick start of the tracer
604 * If tracing is enabled but was stopped by tracing_stop,
605 * this will start the tracer back up.
607 void tracing_start(void)
609 struct ring_buffer *buffer;
612 if (tracing_disabled)
615 spin_lock_irqsave(&tracing_start_lock, flags);
616 if (--trace_stop_count)
619 if (trace_stop_count < 0) {
620 /* Someone screwed up their debugging */
622 trace_stop_count = 0;
627 buffer = global_trace.buffer;
629 ring_buffer_record_enable(buffer);
631 buffer = max_tr.buffer;
633 ring_buffer_record_enable(buffer);
637 spin_unlock_irqrestore(&tracing_start_lock, flags);
641 * tracing_stop - quick stop of the tracer
643 * Light weight way to stop tracing. Use in conjunction with
646 void tracing_stop(void)
648 struct ring_buffer *buffer;
652 spin_lock_irqsave(&tracing_start_lock, flags);
653 if (trace_stop_count++)
656 buffer = global_trace.buffer;
658 ring_buffer_record_disable(buffer);
660 buffer = max_tr.buffer;
662 ring_buffer_record_disable(buffer);
665 spin_unlock_irqrestore(&tracing_start_lock, flags);
668 void trace_stop_cmdline_recording(void);
670 static void trace_save_cmdline(struct task_struct *tsk)
675 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
679 * It's not the end of the world if we don't get
680 * the lock, but we also don't want to spin
681 * nor do we want to disable interrupts,
682 * so if we miss here, then better luck next time.
684 if (!spin_trylock(&trace_cmdline_lock))
687 idx = map_pid_to_cmdline[tsk->pid];
688 if (idx >= SAVED_CMDLINES) {
689 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
691 map = map_cmdline_to_pid[idx];
692 if (map <= PID_MAX_DEFAULT)
693 map_pid_to_cmdline[map] = (unsigned)-1;
695 map_pid_to_cmdline[tsk->pid] = idx;
700 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
702 spin_unlock(&trace_cmdline_lock);
705 char *trace_find_cmdline(int pid)
707 char *cmdline = "<...>";
713 if (pid > PID_MAX_DEFAULT)
716 map = map_pid_to_cmdline[pid];
717 if (map >= SAVED_CMDLINES)
720 cmdline = saved_cmdlines[map];
726 void tracing_record_cmdline(struct task_struct *tsk)
728 if (atomic_read(&trace_record_cmdline_disabled))
731 trace_save_cmdline(tsk);
735 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
738 struct task_struct *tsk = current;
740 entry->preempt_count = pc & 0xff;
741 entry->pid = (tsk) ? tsk->pid : 0;
742 entry->tgid = (tsk) ? tsk->tgid : 0;
744 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
745 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
747 TRACE_FLAG_IRQS_NOSUPPORT |
749 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
750 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
751 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
755 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
756 unsigned long ip, unsigned long parent_ip, unsigned long flags,
759 struct ring_buffer_event *event;
760 struct ftrace_entry *entry;
761 unsigned long irq_flags;
763 /* If we are reading the ring buffer, don't trace */
764 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
767 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
771 entry = ring_buffer_event_data(event);
772 tracing_generic_entry_update(&entry->ent, flags, pc);
773 entry->ent.type = TRACE_FN;
775 entry->parent_ip = parent_ip;
776 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
779 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
780 static void __trace_graph_entry(struct trace_array *tr,
781 struct trace_array_cpu *data,
782 struct ftrace_graph_ent *trace,
786 struct ring_buffer_event *event;
787 struct ftrace_graph_ent_entry *entry;
788 unsigned long irq_flags;
790 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
793 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
797 entry = ring_buffer_event_data(event);
798 tracing_generic_entry_update(&entry->ent, flags, pc);
799 entry->ent.type = TRACE_GRAPH_ENT;
800 entry->graph_ent = *trace;
801 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
804 static void __trace_graph_return(struct trace_array *tr,
805 struct trace_array_cpu *data,
806 struct ftrace_graph_ret *trace,
810 struct ring_buffer_event *event;
811 struct ftrace_graph_ret_entry *entry;
812 unsigned long irq_flags;
814 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
817 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
821 entry = ring_buffer_event_data(event);
822 tracing_generic_entry_update(&entry->ent, flags, pc);
823 entry->ent.type = TRACE_GRAPH_RET;
825 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
830 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
831 unsigned long ip, unsigned long parent_ip, unsigned long flags,
834 if (likely(!atomic_read(&data->disabled)))
835 trace_function(tr, data, ip, parent_ip, flags, pc);
838 static void ftrace_trace_stack(struct trace_array *tr,
839 struct trace_array_cpu *data,
843 #ifdef CONFIG_STACKTRACE
844 struct ring_buffer_event *event;
845 struct stack_entry *entry;
846 struct stack_trace trace;
847 unsigned long irq_flags;
849 if (!(trace_flags & TRACE_ITER_STACKTRACE))
852 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
856 entry = ring_buffer_event_data(event);
857 tracing_generic_entry_update(&entry->ent, flags, pc);
858 entry->ent.type = TRACE_STACK;
860 memset(&entry->caller, 0, sizeof(entry->caller));
862 trace.nr_entries = 0;
863 trace.max_entries = FTRACE_STACK_ENTRIES;
865 trace.entries = entry->caller;
867 save_stack_trace(&trace);
868 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
872 void __trace_stack(struct trace_array *tr,
873 struct trace_array_cpu *data,
877 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
880 static void ftrace_trace_userstack(struct trace_array *tr,
881 struct trace_array_cpu *data,
882 unsigned long flags, int pc)
884 #ifdef CONFIG_STACKTRACE
885 struct ring_buffer_event *event;
886 struct userstack_entry *entry;
887 struct stack_trace trace;
888 unsigned long irq_flags;
890 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
893 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
897 entry = ring_buffer_event_data(event);
898 tracing_generic_entry_update(&entry->ent, flags, pc);
899 entry->ent.type = TRACE_USER_STACK;
901 memset(&entry->caller, 0, sizeof(entry->caller));
903 trace.nr_entries = 0;
904 trace.max_entries = FTRACE_STACK_ENTRIES;
906 trace.entries = entry->caller;
908 save_stack_trace_user(&trace);
909 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
913 void __trace_userstack(struct trace_array *tr,
914 struct trace_array_cpu *data,
917 ftrace_trace_userstack(tr, data, flags, preempt_count());
921 ftrace_trace_special(void *__tr, void *__data,
922 unsigned long arg1, unsigned long arg2, unsigned long arg3,
925 struct ring_buffer_event *event;
926 struct trace_array_cpu *data = __data;
927 struct trace_array *tr = __tr;
928 struct special_entry *entry;
929 unsigned long irq_flags;
931 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
935 entry = ring_buffer_event_data(event);
936 tracing_generic_entry_update(&entry->ent, 0, pc);
937 entry->ent.type = TRACE_SPECIAL;
941 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
942 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
943 ftrace_trace_userstack(tr, data, irq_flags, pc);
949 __trace_special(void *__tr, void *__data,
950 unsigned long arg1, unsigned long arg2, unsigned long arg3)
952 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
956 tracing_sched_switch_trace(struct trace_array *tr,
957 struct trace_array_cpu *data,
958 struct task_struct *prev,
959 struct task_struct *next,
960 unsigned long flags, int pc)
962 struct ring_buffer_event *event;
963 struct ctx_switch_entry *entry;
964 unsigned long irq_flags;
966 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
970 entry = ring_buffer_event_data(event);
971 tracing_generic_entry_update(&entry->ent, flags, pc);
972 entry->ent.type = TRACE_CTX;
973 entry->prev_pid = prev->pid;
974 entry->prev_prio = prev->prio;
975 entry->prev_state = prev->state;
976 entry->next_pid = next->pid;
977 entry->next_prio = next->prio;
978 entry->next_state = next->state;
979 entry->next_cpu = task_cpu(next);
980 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
981 ftrace_trace_stack(tr, data, flags, 5, pc);
982 ftrace_trace_userstack(tr, data, flags, pc);
986 tracing_sched_wakeup_trace(struct trace_array *tr,
987 struct trace_array_cpu *data,
988 struct task_struct *wakee,
989 struct task_struct *curr,
990 unsigned long flags, int pc)
992 struct ring_buffer_event *event;
993 struct ctx_switch_entry *entry;
994 unsigned long irq_flags;
996 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1000 entry = ring_buffer_event_data(event);
1001 tracing_generic_entry_update(&entry->ent, flags, pc);
1002 entry->ent.type = TRACE_WAKE;
1003 entry->prev_pid = curr->pid;
1004 entry->prev_prio = curr->prio;
1005 entry->prev_state = curr->state;
1006 entry->next_pid = wakee->pid;
1007 entry->next_prio = wakee->prio;
1008 entry->next_state = wakee->state;
1009 entry->next_cpu = task_cpu(wakee);
1010 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1011 ftrace_trace_stack(tr, data, flags, 6, pc);
1012 ftrace_trace_userstack(tr, data, flags, pc);
1018 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1020 struct trace_array *tr = &global_trace;
1021 struct trace_array_cpu *data;
1022 unsigned long flags;
1026 if (tracing_disabled)
1029 pc = preempt_count();
1030 local_irq_save(flags);
1031 cpu = raw_smp_processor_id();
1032 data = tr->data[cpu];
1034 if (likely(atomic_inc_return(&data->disabled) == 1))
1035 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1037 atomic_dec(&data->disabled);
1038 local_irq_restore(flags);
1041 #ifdef CONFIG_FUNCTION_TRACER
1043 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
1045 struct trace_array *tr = &global_trace;
1046 struct trace_array_cpu *data;
1047 unsigned long flags;
1052 if (unlikely(!ftrace_function_enabled))
1055 pc = preempt_count();
1056 resched = ftrace_preempt_disable();
1057 local_save_flags(flags);
1058 cpu = raw_smp_processor_id();
1059 data = tr->data[cpu];
1060 disabled = atomic_inc_return(&data->disabled);
1062 if (likely(disabled == 1))
1063 trace_function(tr, data, ip, parent_ip, flags, pc);
1065 atomic_dec(&data->disabled);
1066 ftrace_preempt_enable(resched);
1070 function_trace_call(unsigned long ip, unsigned long parent_ip)
1072 struct trace_array *tr = &global_trace;
1073 struct trace_array_cpu *data;
1074 unsigned long flags;
1079 if (unlikely(!ftrace_function_enabled))
1083 * Need to use raw, since this must be called before the
1084 * recursive protection is performed.
1086 local_irq_save(flags);
1087 cpu = raw_smp_processor_id();
1088 data = tr->data[cpu];
1089 disabled = atomic_inc_return(&data->disabled);
1091 if (likely(disabled == 1)) {
1092 pc = preempt_count();
1093 trace_function(tr, data, ip, parent_ip, flags, pc);
1096 atomic_dec(&data->disabled);
1097 local_irq_restore(flags);
1100 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1101 int trace_graph_entry(struct ftrace_graph_ent *trace)
1103 struct trace_array *tr = &global_trace;
1104 struct trace_array_cpu *data;
1105 unsigned long flags;
1110 if (!ftrace_trace_task(current))
1113 if (!ftrace_graph_addr(trace->func))
1116 local_irq_save(flags);
1117 cpu = raw_smp_processor_id();
1118 data = tr->data[cpu];
1119 disabled = atomic_inc_return(&data->disabled);
1120 if (likely(disabled == 1)) {
1121 pc = preempt_count();
1122 __trace_graph_entry(tr, data, trace, flags, pc);
1124 /* Only do the atomic if it is not already set */
1125 if (!test_tsk_trace_graph(current))
1126 set_tsk_trace_graph(current);
1127 atomic_dec(&data->disabled);
1128 local_irq_restore(flags);
1133 void trace_graph_return(struct ftrace_graph_ret *trace)
1135 struct trace_array *tr = &global_trace;
1136 struct trace_array_cpu *data;
1137 unsigned long flags;
1142 local_irq_save(flags);
1143 cpu = raw_smp_processor_id();
1144 data = tr->data[cpu];
1145 disabled = atomic_inc_return(&data->disabled);
1146 if (likely(disabled == 1)) {
1147 pc = preempt_count();
1148 __trace_graph_return(tr, data, trace, flags, pc);
1151 clear_tsk_trace_graph(current);
1152 atomic_dec(&data->disabled);
1153 local_irq_restore(flags);
1155 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1157 static struct ftrace_ops trace_ops __read_mostly =
1159 .func = function_trace_call,
1162 void tracing_start_function_trace(void)
1164 ftrace_function_enabled = 0;
1166 if (trace_flags & TRACE_ITER_PREEMPTONLY)
1167 trace_ops.func = function_trace_call_preempt_only;
1169 trace_ops.func = function_trace_call;
1171 register_ftrace_function(&trace_ops);
1172 ftrace_function_enabled = 1;
1175 void tracing_stop_function_trace(void)
1177 ftrace_function_enabled = 0;
1178 unregister_ftrace_function(&trace_ops);
1182 enum trace_file_type {
1183 TRACE_FILE_LAT_FMT = 1,
1184 TRACE_FILE_ANNOTATE = 2,
1187 static void trace_iterator_increment(struct trace_iterator *iter)
1189 /* Don't allow ftrace to trace into the ring buffers */
1190 ftrace_disable_cpu();
1193 if (iter->buffer_iter[iter->cpu])
1194 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1196 ftrace_enable_cpu();
1199 static struct trace_entry *
1200 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1202 struct ring_buffer_event *event;
1203 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1205 /* Don't allow ftrace to trace into the ring buffers */
1206 ftrace_disable_cpu();
1209 event = ring_buffer_iter_peek(buf_iter, ts);
1211 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1213 ftrace_enable_cpu();
1215 return event ? ring_buffer_event_data(event) : NULL;
1218 static struct trace_entry *
1219 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1221 struct ring_buffer *buffer = iter->tr->buffer;
1222 struct trace_entry *ent, *next = NULL;
1223 u64 next_ts = 0, ts;
1227 for_each_tracing_cpu(cpu) {
1229 if (ring_buffer_empty_cpu(buffer, cpu))
1232 ent = peek_next_entry(iter, cpu, &ts);
1235 * Pick the entry with the smallest timestamp:
1237 if (ent && (!next || ts < next_ts)) {
1245 *ent_cpu = next_cpu;
1253 /* Find the next real entry, without updating the iterator itself */
1254 static struct trace_entry *
1255 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1257 return __find_next_entry(iter, ent_cpu, ent_ts);
1260 /* Find the next real entry, and increment the iterator to the next entry */
1261 static void *find_next_entry_inc(struct trace_iterator *iter)
1263 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1266 trace_iterator_increment(iter);
1268 return iter->ent ? iter : NULL;
1271 static void trace_consume(struct trace_iterator *iter)
1273 /* Don't allow ftrace to trace into the ring buffers */
1274 ftrace_disable_cpu();
1275 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1276 ftrace_enable_cpu();
1279 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1281 struct trace_iterator *iter = m->private;
1287 /* can't go backwards */
1292 ent = find_next_entry_inc(iter);
1296 while (ent && iter->idx < i)
1297 ent = find_next_entry_inc(iter);
1304 static void *s_start(struct seq_file *m, loff_t *pos)
1306 struct trace_iterator *iter = m->private;
1311 mutex_lock(&trace_types_lock);
1313 if (!current_trace || current_trace != iter->trace) {
1314 mutex_unlock(&trace_types_lock);
1318 atomic_inc(&trace_record_cmdline_disabled);
1320 if (*pos != iter->pos) {
1325 ftrace_disable_cpu();
1327 for_each_tracing_cpu(cpu) {
1328 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1331 ftrace_enable_cpu();
1333 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1338 p = s_next(m, p, &l);
1344 static void s_stop(struct seq_file *m, void *p)
1346 atomic_dec(&trace_record_cmdline_disabled);
1347 mutex_unlock(&trace_types_lock);
1350 static void print_lat_help_header(struct seq_file *m)
1352 seq_puts(m, "# _------=> CPU# \n");
1353 seq_puts(m, "# / _-----=> irqs-off \n");
1354 seq_puts(m, "# | / _----=> need-resched \n");
1355 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1356 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1357 seq_puts(m, "# |||| / \n");
1358 seq_puts(m, "# ||||| delay \n");
1359 seq_puts(m, "# cmd pid ||||| time | caller \n");
1360 seq_puts(m, "# \\ / ||||| \\ | / \n");
1363 static void print_func_help_header(struct seq_file *m)
1365 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1366 seq_puts(m, "# | | | | |\n");
1371 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1373 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1374 struct trace_array *tr = iter->tr;
1375 struct trace_array_cpu *data = tr->data[tr->cpu];
1376 struct tracer *type = current_trace;
1377 unsigned long total;
1378 unsigned long entries;
1379 const char *name = "preemption";
1384 entries = ring_buffer_entries(iter->tr->buffer);
1386 ring_buffer_overruns(iter->tr->buffer);
1388 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1390 seq_puts(m, "-----------------------------------"
1391 "---------------------------------\n");
1392 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1393 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1394 nsecs_to_usecs(data->saved_latency),
1398 #if defined(CONFIG_PREEMPT_NONE)
1400 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1402 #elif defined(CONFIG_PREEMPT)
1407 /* These are reserved for later use */
1410 seq_printf(m, " #P:%d)\n", num_online_cpus());
1414 seq_puts(m, " -----------------\n");
1415 seq_printf(m, " | task: %.16s-%d "
1416 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1417 data->comm, data->pid, data->uid, data->nice,
1418 data->policy, data->rt_priority);
1419 seq_puts(m, " -----------------\n");
1421 if (data->critical_start) {
1422 seq_puts(m, " => started at: ");
1423 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1424 trace_print_seq(m, &iter->seq);
1425 seq_puts(m, "\n => ended at: ");
1426 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1427 trace_print_seq(m, &iter->seq);
1435 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1437 int hardirq, softirq;
1440 comm = trace_find_cmdline(entry->pid);
1442 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1443 trace_seq_printf(s, "%3d", cpu);
1444 trace_seq_printf(s, "%c%c",
1445 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1446 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1447 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1449 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1450 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1451 if (hardirq && softirq) {
1452 trace_seq_putc(s, 'H');
1455 trace_seq_putc(s, 'h');
1458 trace_seq_putc(s, 's');
1460 trace_seq_putc(s, '.');
1464 if (entry->preempt_count)
1465 trace_seq_printf(s, "%x", entry->preempt_count);
1467 trace_seq_puts(s, ".");
1470 unsigned long preempt_mark_thresh = 100;
1473 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1474 unsigned long rel_usecs)
1476 trace_seq_printf(s, " %4lldus", abs_usecs);
1477 if (rel_usecs > preempt_mark_thresh)
1478 trace_seq_puts(s, "!: ");
1479 else if (rel_usecs > 1)
1480 trace_seq_puts(s, "+: ");
1482 trace_seq_puts(s, " : ");
1485 static void test_cpu_buff_start(struct trace_iterator *iter)
1487 struct trace_seq *s = &iter->seq;
1489 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1492 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1495 if (cpumask_test_cpu(iter->cpu, iter->started))
1498 cpumask_set_cpu(iter->cpu, iter->started);
1499 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1502 static enum print_line_t
1503 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1505 struct trace_seq *s = &iter->seq;
1506 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1507 struct trace_entry *next_entry;
1508 struct trace_event *event;
1509 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1510 struct trace_entry *entry = iter->ent;
1511 unsigned long abs_usecs;
1512 unsigned long rel_usecs;
1517 test_cpu_buff_start(iter);
1519 next_entry = find_next_entry(iter, NULL, &next_ts);
1522 rel_usecs = ns2usecs(next_ts - iter->ts);
1523 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1526 comm = trace_find_cmdline(entry->pid);
1527 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1528 " %ld.%03ldms (+%ld.%03ldms): ",
1530 entry->pid, cpu, entry->flags,
1531 entry->preempt_count, trace_idx,
1534 abs_usecs % 1000, rel_usecs/1000,
1537 lat_print_generic(s, entry, cpu);
1538 lat_print_timestamp(s, abs_usecs, rel_usecs);
1541 event = ftrace_find_event(entry->type);
1542 if (event && event->latency_trace) {
1543 ret = event->latency_trace(s, entry, sym_flags);
1546 return TRACE_TYPE_HANDLED;
1549 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1550 return TRACE_TYPE_HANDLED;
1553 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1555 struct trace_seq *s = &iter->seq;
1556 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1557 struct trace_entry *entry;
1558 struct trace_event *event;
1559 unsigned long usec_rem;
1560 unsigned long long t;
1567 test_cpu_buff_start(iter);
1569 comm = trace_find_cmdline(iter->ent->pid);
1571 t = ns2usecs(iter->ts);
1572 usec_rem = do_div(t, 1000000ULL);
1573 secs = (unsigned long)t;
1575 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1577 return TRACE_TYPE_PARTIAL_LINE;
1578 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1580 return TRACE_TYPE_PARTIAL_LINE;
1581 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1583 return TRACE_TYPE_PARTIAL_LINE;
1585 event = ftrace_find_event(entry->type);
1586 if (event && event->trace) {
1587 ret = event->trace(s, entry, sym_flags);
1590 return TRACE_TYPE_HANDLED;
1592 ret = trace_seq_printf(s, "Unknown type %d\n", entry->type);
1594 return TRACE_TYPE_PARTIAL_LINE;
1596 return TRACE_TYPE_HANDLED;
1599 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1601 struct trace_seq *s = &iter->seq;
1602 struct trace_entry *entry;
1603 struct trace_event *event;
1608 ret = trace_seq_printf(s, "%d %d %llu ",
1609 entry->pid, iter->cpu, iter->ts);
1611 return TRACE_TYPE_PARTIAL_LINE;
1613 event = ftrace_find_event(entry->type);
1614 if (event && event->raw) {
1615 ret = event->raw(s, entry, 0);
1618 return TRACE_TYPE_HANDLED;
1620 ret = trace_seq_printf(s, "%d ?\n", entry->type);
1622 return TRACE_TYPE_PARTIAL_LINE;
1624 return TRACE_TYPE_HANDLED;
1627 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1629 struct trace_seq *s = &iter->seq;
1630 unsigned char newline = '\n';
1631 struct trace_entry *entry;
1632 struct trace_event *event;
1636 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1637 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1638 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1640 event = ftrace_find_event(entry->type);
1641 if (event && event->hex)
1642 event->hex(s, entry, 0);
1644 SEQ_PUT_FIELD_RET(s, newline);
1646 return TRACE_TYPE_HANDLED;
1649 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1651 struct trace_seq *s = &iter->seq;
1652 struct trace_entry *entry = iter->ent;
1653 struct print_entry *field;
1656 trace_assign_type(field, entry);
1658 ret = trace_seq_printf(s, field->buf);
1660 return TRACE_TYPE_PARTIAL_LINE;
1662 return TRACE_TYPE_HANDLED;
1665 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1667 struct trace_seq *s = &iter->seq;
1668 struct trace_entry *entry;
1669 struct trace_event *event;
1673 SEQ_PUT_FIELD_RET(s, entry->pid);
1674 SEQ_PUT_FIELD_RET(s, entry->cpu);
1675 SEQ_PUT_FIELD_RET(s, iter->ts);
1677 event = ftrace_find_event(entry->type);
1678 if (event && event->binary)
1679 event->binary(s, entry, 0);
1681 return TRACE_TYPE_HANDLED;
1684 static int trace_empty(struct trace_iterator *iter)
1688 for_each_tracing_cpu(cpu) {
1689 if (iter->buffer_iter[cpu]) {
1690 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1693 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1701 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1703 enum print_line_t ret;
1705 if (iter->trace && iter->trace->print_line) {
1706 ret = iter->trace->print_line(iter);
1707 if (ret != TRACE_TYPE_UNHANDLED)
1711 if (iter->ent->type == TRACE_PRINT &&
1712 trace_flags & TRACE_ITER_PRINTK &&
1713 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1714 return print_printk_msg_only(iter);
1716 if (trace_flags & TRACE_ITER_BIN)
1717 return print_bin_fmt(iter);
1719 if (trace_flags & TRACE_ITER_HEX)
1720 return print_hex_fmt(iter);
1722 if (trace_flags & TRACE_ITER_RAW)
1723 return print_raw_fmt(iter);
1725 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1726 return print_lat_fmt(iter, iter->idx, iter->cpu);
1728 return print_trace_fmt(iter);
1731 static int s_show(struct seq_file *m, void *v)
1733 struct trace_iterator *iter = v;
1735 if (iter->ent == NULL) {
1737 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1740 if (iter->trace && iter->trace->print_header)
1741 iter->trace->print_header(m);
1742 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1743 /* print nothing if the buffers are empty */
1744 if (trace_empty(iter))
1746 print_trace_header(m, iter);
1747 if (!(trace_flags & TRACE_ITER_VERBOSE))
1748 print_lat_help_header(m);
1750 if (!(trace_flags & TRACE_ITER_VERBOSE))
1751 print_func_help_header(m);
1754 print_trace_line(iter);
1755 trace_print_seq(m, &iter->seq);
1761 static struct seq_operations tracer_seq_ops = {
1768 static struct trace_iterator *
1769 __tracing_open(struct inode *inode, struct file *file, int *ret)
1771 struct trace_iterator *iter;
1775 if (tracing_disabled) {
1780 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1786 mutex_lock(&trace_types_lock);
1787 if (current_trace && current_trace->print_max)
1790 iter->tr = inode->i_private;
1791 iter->trace = current_trace;
1794 /* Notify the tracer early; before we stop tracing. */
1795 if (iter->trace && iter->trace->open)
1796 iter->trace->open(iter);
1798 /* Annotate start of buffers if we had overruns */
1799 if (ring_buffer_overruns(iter->tr->buffer))
1800 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1803 for_each_tracing_cpu(cpu) {
1805 iter->buffer_iter[cpu] =
1806 ring_buffer_read_start(iter->tr->buffer, cpu);
1808 if (!iter->buffer_iter[cpu])
1812 /* TODO stop tracer */
1813 *ret = seq_open(file, &tracer_seq_ops);
1817 m = file->private_data;
1820 /* stop the trace while dumping */
1823 mutex_unlock(&trace_types_lock);
1829 for_each_tracing_cpu(cpu) {
1830 if (iter->buffer_iter[cpu])
1831 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1833 mutex_unlock(&trace_types_lock);
1836 return ERR_PTR(-ENOMEM);
1839 int tracing_open_generic(struct inode *inode, struct file *filp)
1841 if (tracing_disabled)
1844 filp->private_data = inode->i_private;
1848 int tracing_release(struct inode *inode, struct file *file)
1850 struct seq_file *m = (struct seq_file *)file->private_data;
1851 struct trace_iterator *iter = m->private;
1854 mutex_lock(&trace_types_lock);
1855 for_each_tracing_cpu(cpu) {
1856 if (iter->buffer_iter[cpu])
1857 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1860 if (iter->trace && iter->trace->close)
1861 iter->trace->close(iter);
1863 /* reenable tracing if it was previously enabled */
1865 mutex_unlock(&trace_types_lock);
1867 seq_release(inode, file);
1872 static int tracing_open(struct inode *inode, struct file *file)
1876 __tracing_open(inode, file, &ret);
1881 static int tracing_lt_open(struct inode *inode, struct file *file)
1883 struct trace_iterator *iter;
1886 iter = __tracing_open(inode, file, &ret);
1889 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1896 t_next(struct seq_file *m, void *v, loff_t *pos)
1898 struct tracer *t = m->private;
1910 static void *t_start(struct seq_file *m, loff_t *pos)
1912 struct tracer *t = m->private;
1915 mutex_lock(&trace_types_lock);
1916 for (; t && l < *pos; t = t_next(m, t, &l))
1922 static void t_stop(struct seq_file *m, void *p)
1924 mutex_unlock(&trace_types_lock);
1927 static int t_show(struct seq_file *m, void *v)
1929 struct tracer *t = v;
1934 seq_printf(m, "%s", t->name);
1943 static struct seq_operations show_traces_seq_ops = {
1950 static int show_traces_open(struct inode *inode, struct file *file)
1954 if (tracing_disabled)
1957 ret = seq_open(file, &show_traces_seq_ops);
1959 struct seq_file *m = file->private_data;
1960 m->private = trace_types;
1966 static struct file_operations tracing_fops = {
1967 .open = tracing_open,
1969 .llseek = seq_lseek,
1970 .release = tracing_release,
1973 static struct file_operations tracing_lt_fops = {
1974 .open = tracing_lt_open,
1976 .llseek = seq_lseek,
1977 .release = tracing_release,
1980 static struct file_operations show_traces_fops = {
1981 .open = show_traces_open,
1983 .release = seq_release,
1987 * Only trace on a CPU if the bitmask is set:
1989 static cpumask_var_t tracing_cpumask;
1992 * The tracer itself will not take this lock, but still we want
1993 * to provide a consistent cpumask to user-space:
1995 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1998 * Temporary storage for the character representation of the
1999 * CPU bitmask (and one more byte for the newline):
2001 static char mask_str[NR_CPUS + 1];
2004 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2005 size_t count, loff_t *ppos)
2009 mutex_lock(&tracing_cpumask_update_lock);
2011 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2012 if (count - len < 2) {
2016 len += sprintf(mask_str + len, "\n");
2017 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2020 mutex_unlock(&tracing_cpumask_update_lock);
2026 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2027 size_t count, loff_t *ppos)
2030 cpumask_var_t tracing_cpumask_new;
2032 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2035 mutex_lock(&tracing_cpumask_update_lock);
2036 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2040 local_irq_disable();
2041 __raw_spin_lock(&ftrace_max_lock);
2042 for_each_tracing_cpu(cpu) {
2044 * Increase/decrease the disabled counter if we are
2045 * about to flip a bit in the cpumask:
2047 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2048 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2049 atomic_inc(&global_trace.data[cpu]->disabled);
2051 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2052 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2053 atomic_dec(&global_trace.data[cpu]->disabled);
2056 __raw_spin_unlock(&ftrace_max_lock);
2059 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2061 mutex_unlock(&tracing_cpumask_update_lock);
2062 free_cpumask_var(tracing_cpumask_new);
2067 mutex_unlock(&tracing_cpumask_update_lock);
2068 free_cpumask_var(tracing_cpumask);
2073 static struct file_operations tracing_cpumask_fops = {
2074 .open = tracing_open_generic,
2075 .read = tracing_cpumask_read,
2076 .write = tracing_cpumask_write,
2080 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2081 size_t cnt, loff_t *ppos)
2087 u32 tracer_flags = current_trace->flags->val;
2088 struct tracer_opt *trace_opts = current_trace->flags->opts;
2091 /* calulate max size */
2092 for (i = 0; trace_options[i]; i++) {
2093 len += strlen(trace_options[i]);
2094 len += 3; /* "no" and space */
2098 * Increase the size with names of options specific
2099 * of the current tracer.
2101 for (i = 0; trace_opts[i].name; i++) {
2102 len += strlen(trace_opts[i].name);
2103 len += 3; /* "no" and space */
2106 /* +2 for \n and \0 */
2107 buf = kmalloc(len + 2, GFP_KERNEL);
2111 for (i = 0; trace_options[i]; i++) {
2112 if (trace_flags & (1 << i))
2113 r += sprintf(buf + r, "%s ", trace_options[i]);
2115 r += sprintf(buf + r, "no%s ", trace_options[i]);
2118 for (i = 0; trace_opts[i].name; i++) {
2119 if (tracer_flags & trace_opts[i].bit)
2120 r += sprintf(buf + r, "%s ",
2121 trace_opts[i].name);
2123 r += sprintf(buf + r, "no%s ",
2124 trace_opts[i].name);
2127 r += sprintf(buf + r, "\n");
2128 WARN_ON(r >= len + 2);
2130 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2137 /* Try to assign a tracer specific option */
2138 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2140 struct tracer_flags *trace_flags = trace->flags;
2141 struct tracer_opt *opts = NULL;
2145 for (i = 0; trace_flags->opts[i].name; i++) {
2146 opts = &trace_flags->opts[i];
2147 len = strlen(opts->name);
2149 if (strncmp(cmp, opts->name, len) == 0) {
2150 ret = trace->set_flag(trace_flags->val,
2156 if (!trace_flags->opts[i].name)
2159 /* Refused to handle */
2164 trace_flags->val &= ~opts->bit;
2166 trace_flags->val |= opts->bit;
2172 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2173 size_t cnt, loff_t *ppos)
2181 if (cnt >= sizeof(buf))
2184 if (copy_from_user(&buf, ubuf, cnt))
2189 if (strncmp(buf, "no", 2) == 0) {
2194 for (i = 0; trace_options[i]; i++) {
2195 int len = strlen(trace_options[i]);
2197 if (strncmp(cmp, trace_options[i], len) == 0) {
2199 trace_flags &= ~(1 << i);
2201 trace_flags |= (1 << i);
2206 /* If no option could be set, test the specific tracer options */
2207 if (!trace_options[i]) {
2208 ret = set_tracer_option(current_trace, cmp, neg);
2218 static struct file_operations tracing_iter_fops = {
2219 .open = tracing_open_generic,
2220 .read = tracing_trace_options_read,
2221 .write = tracing_trace_options_write,
2224 static const char readme_msg[] =
2225 "tracing mini-HOWTO:\n\n"
2227 "# mount -t debugfs nodev /debug\n\n"
2228 "# cat /debug/tracing/available_tracers\n"
2229 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2230 "# cat /debug/tracing/current_tracer\n"
2232 "# echo sched_switch > /debug/tracing/current_tracer\n"
2233 "# cat /debug/tracing/current_tracer\n"
2235 "# cat /debug/tracing/trace_options\n"
2236 "noprint-parent nosym-offset nosym-addr noverbose\n"
2237 "# echo print-parent > /debug/tracing/trace_options\n"
2238 "# echo 1 > /debug/tracing/tracing_enabled\n"
2239 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2240 "echo 0 > /debug/tracing/tracing_enabled\n"
2244 tracing_readme_read(struct file *filp, char __user *ubuf,
2245 size_t cnt, loff_t *ppos)
2247 return simple_read_from_buffer(ubuf, cnt, ppos,
2248 readme_msg, strlen(readme_msg));
2251 static struct file_operations tracing_readme_fops = {
2252 .open = tracing_open_generic,
2253 .read = tracing_readme_read,
2257 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2258 size_t cnt, loff_t *ppos)
2263 r = sprintf(buf, "%u\n", tracer_enabled);
2264 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2268 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2269 size_t cnt, loff_t *ppos)
2271 struct trace_array *tr = filp->private_data;
2276 if (cnt >= sizeof(buf))
2279 if (copy_from_user(&buf, ubuf, cnt))
2284 ret = strict_strtoul(buf, 10, &val);
2290 mutex_lock(&trace_types_lock);
2291 if (tracer_enabled ^ val) {
2294 if (current_trace->start)
2295 current_trace->start(tr);
2300 if (current_trace->stop)
2301 current_trace->stop(tr);
2304 mutex_unlock(&trace_types_lock);
2312 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2313 size_t cnt, loff_t *ppos)
2315 char buf[max_tracer_type_len+2];
2318 mutex_lock(&trace_types_lock);
2320 r = sprintf(buf, "%s\n", current_trace->name);
2322 r = sprintf(buf, "\n");
2323 mutex_unlock(&trace_types_lock);
2325 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2328 static int tracing_set_tracer(char *buf)
2330 struct trace_array *tr = &global_trace;
2334 mutex_lock(&trace_types_lock);
2335 for (t = trace_types; t; t = t->next) {
2336 if (strcmp(t->name, buf) == 0)
2343 if (t == current_trace)
2346 trace_branch_disable();
2347 if (current_trace && current_trace->reset)
2348 current_trace->reset(tr);
2356 init_tracer_stat(t);
2358 trace_branch_enable(tr);
2360 mutex_unlock(&trace_types_lock);
2366 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2367 size_t cnt, loff_t *ppos)
2369 char buf[max_tracer_type_len+1];
2376 if (cnt > max_tracer_type_len)
2377 cnt = max_tracer_type_len;
2379 if (copy_from_user(&buf, ubuf, cnt))
2384 /* strip ending whitespace. */
2385 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2388 err = tracing_set_tracer(buf);
2398 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2399 size_t cnt, loff_t *ppos)
2401 unsigned long *ptr = filp->private_data;
2405 r = snprintf(buf, sizeof(buf), "%ld\n",
2406 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2407 if (r > sizeof(buf))
2409 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2413 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2414 size_t cnt, loff_t *ppos)
2416 long *ptr = filp->private_data;
2421 if (cnt >= sizeof(buf))
2424 if (copy_from_user(&buf, ubuf, cnt))
2429 ret = strict_strtoul(buf, 10, &val);
2438 static atomic_t tracing_reader;
2440 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2442 struct trace_iterator *iter;
2444 if (tracing_disabled)
2447 /* We only allow for reader of the pipe */
2448 if (atomic_inc_return(&tracing_reader) != 1) {
2449 atomic_dec(&tracing_reader);
2453 /* create a buffer to store the information to pass to userspace */
2454 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2458 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2463 mutex_lock(&trace_types_lock);
2465 /* trace pipe does not show start of buffer */
2466 cpumask_setall(iter->started);
2468 iter->tr = &global_trace;
2469 iter->trace = current_trace;
2470 filp->private_data = iter;
2472 if (iter->trace->pipe_open)
2473 iter->trace->pipe_open(iter);
2474 mutex_unlock(&trace_types_lock);
2479 static int tracing_release_pipe(struct inode *inode, struct file *file)
2481 struct trace_iterator *iter = file->private_data;
2483 free_cpumask_var(iter->started);
2485 atomic_dec(&tracing_reader);
2491 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2493 struct trace_iterator *iter = filp->private_data;
2495 if (trace_flags & TRACE_ITER_BLOCK) {
2497 * Always select as readable when in blocking mode
2499 return POLLIN | POLLRDNORM;
2501 if (!trace_empty(iter))
2502 return POLLIN | POLLRDNORM;
2503 poll_wait(filp, &trace_wait, poll_table);
2504 if (!trace_empty(iter))
2505 return POLLIN | POLLRDNORM;
2515 tracing_read_pipe(struct file *filp, char __user *ubuf,
2516 size_t cnt, loff_t *ppos)
2518 struct trace_iterator *iter = filp->private_data;
2521 /* return any leftover data */
2522 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2526 trace_seq_reset(&iter->seq);
2528 mutex_lock(&trace_types_lock);
2529 if (iter->trace->read) {
2530 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2537 while (trace_empty(iter)) {
2539 if ((filp->f_flags & O_NONBLOCK)) {
2545 * This is a make-shift waitqueue. The reason we don't use
2546 * an actual wait queue is because:
2547 * 1) we only ever have one waiter
2548 * 2) the tracing, traces all functions, we don't want
2549 * the overhead of calling wake_up and friends
2550 * (and tracing them too)
2551 * Anyway, this is really very primitive wakeup.
2553 set_current_state(TASK_INTERRUPTIBLE);
2554 iter->tr->waiter = current;
2556 mutex_unlock(&trace_types_lock);
2558 /* sleep for 100 msecs, and try again. */
2559 schedule_timeout(HZ/10);
2561 mutex_lock(&trace_types_lock);
2563 iter->tr->waiter = NULL;
2565 if (signal_pending(current)) {
2570 if (iter->trace != current_trace)
2574 * We block until we read something and tracing is disabled.
2575 * We still block if tracing is disabled, but we have never
2576 * read anything. This allows a user to cat this file, and
2577 * then enable tracing. But after we have read something,
2578 * we give an EOF when tracing is again disabled.
2580 * iter->pos will be 0 if we haven't read anything.
2582 if (!tracer_enabled && iter->pos)
2588 /* stop when tracing is finished */
2589 if (trace_empty(iter))
2592 if (cnt >= PAGE_SIZE)
2593 cnt = PAGE_SIZE - 1;
2595 /* reset all but tr, trace, and overruns */
2596 memset(&iter->seq, 0,
2597 sizeof(struct trace_iterator) -
2598 offsetof(struct trace_iterator, seq));
2601 while (find_next_entry_inc(iter) != NULL) {
2602 enum print_line_t ret;
2603 int len = iter->seq.len;
2605 ret = print_trace_line(iter);
2606 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2607 /* don't print partial lines */
2608 iter->seq.len = len;
2612 trace_consume(iter);
2614 if (iter->seq.len >= cnt)
2618 /* Now copy what we have to the user */
2619 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2620 if (iter->seq.readpos >= iter->seq.len)
2621 trace_seq_reset(&iter->seq);
2624 * If there was nothing to send to user, inspite of consuming trace
2625 * entries, go back to wait for more entries.
2631 mutex_unlock(&trace_types_lock);
2637 tracing_entries_read(struct file *filp, char __user *ubuf,
2638 size_t cnt, loff_t *ppos)
2640 struct trace_array *tr = filp->private_data;
2644 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2645 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2649 tracing_entries_write(struct file *filp, const char __user *ubuf,
2650 size_t cnt, loff_t *ppos)
2656 if (cnt >= sizeof(buf))
2659 if (copy_from_user(&buf, ubuf, cnt))
2664 ret = strict_strtoul(buf, 10, &val);
2668 /* must have at least 1 entry */
2672 mutex_lock(&trace_types_lock);
2676 /* disable all cpu buffers */
2677 for_each_tracing_cpu(cpu) {
2678 if (global_trace.data[cpu])
2679 atomic_inc(&global_trace.data[cpu]->disabled);
2680 if (max_tr.data[cpu])
2681 atomic_inc(&max_tr.data[cpu]->disabled);
2684 /* value is in KB */
2687 if (val != global_trace.entries) {
2688 ret = ring_buffer_resize(global_trace.buffer, val);
2694 ret = ring_buffer_resize(max_tr.buffer, val);
2698 r = ring_buffer_resize(global_trace.buffer,
2699 global_trace.entries);
2701 /* AARGH! We are left with different
2702 * size max buffer!!!! */
2704 tracing_disabled = 1;
2709 global_trace.entries = val;
2714 /* If check pages failed, return ENOMEM */
2715 if (tracing_disabled)
2718 for_each_tracing_cpu(cpu) {
2719 if (global_trace.data[cpu])
2720 atomic_dec(&global_trace.data[cpu]->disabled);
2721 if (max_tr.data[cpu])
2722 atomic_dec(&max_tr.data[cpu]->disabled);
2726 max_tr.entries = global_trace.entries;
2727 mutex_unlock(&trace_types_lock);
2732 static int mark_printk(const char *fmt, ...)
2736 va_start(args, fmt);
2737 ret = trace_vprintk(0, -1, fmt, args);
2743 tracing_mark_write(struct file *filp, const char __user *ubuf,
2744 size_t cnt, loff_t *fpos)
2749 if (tracing_disabled)
2752 if (cnt > TRACE_BUF_SIZE)
2753 cnt = TRACE_BUF_SIZE;
2755 buf = kmalloc(cnt + 1, GFP_KERNEL);
2759 if (copy_from_user(buf, ubuf, cnt)) {
2764 /* Cut from the first nil or newline. */
2766 end = strchr(buf, '\n');
2770 cnt = mark_printk("%s\n", buf);
2777 static struct file_operations tracing_max_lat_fops = {
2778 .open = tracing_open_generic,
2779 .read = tracing_max_lat_read,
2780 .write = tracing_max_lat_write,
2783 static struct file_operations tracing_ctrl_fops = {
2784 .open = tracing_open_generic,
2785 .read = tracing_ctrl_read,
2786 .write = tracing_ctrl_write,
2789 static struct file_operations set_tracer_fops = {
2790 .open = tracing_open_generic,
2791 .read = tracing_set_trace_read,
2792 .write = tracing_set_trace_write,
2795 static struct file_operations tracing_pipe_fops = {
2796 .open = tracing_open_pipe,
2797 .poll = tracing_poll_pipe,
2798 .read = tracing_read_pipe,
2799 .release = tracing_release_pipe,
2802 static struct file_operations tracing_entries_fops = {
2803 .open = tracing_open_generic,
2804 .read = tracing_entries_read,
2805 .write = tracing_entries_write,
2808 static struct file_operations tracing_mark_fops = {
2809 .open = tracing_open_generic,
2810 .write = tracing_mark_write,
2813 #ifdef CONFIG_DYNAMIC_FTRACE
2815 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
2821 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
2822 size_t cnt, loff_t *ppos)
2824 static char ftrace_dyn_info_buffer[1024];
2825 static DEFINE_MUTEX(dyn_info_mutex);
2826 unsigned long *p = filp->private_data;
2827 char *buf = ftrace_dyn_info_buffer;
2828 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
2831 mutex_lock(&dyn_info_mutex);
2832 r = sprintf(buf, "%ld ", *p);
2834 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
2837 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2839 mutex_unlock(&dyn_info_mutex);
2844 static struct file_operations tracing_dyn_info_fops = {
2845 .open = tracing_open_generic,
2846 .read = tracing_read_dyn_info,
2850 static struct dentry *d_tracer;
2852 struct dentry *tracing_init_dentry(void)
2859 d_tracer = debugfs_create_dir("tracing", NULL);
2861 if (!d_tracer && !once) {
2863 pr_warning("Could not create debugfs directory 'tracing'\n");
2870 #ifdef CONFIG_FTRACE_SELFTEST
2871 /* Let selftest have access to static functions in this file */
2872 #include "trace_selftest.c"
2875 static __init int tracer_init_debugfs(void)
2877 struct dentry *d_tracer;
2878 struct dentry *entry;
2880 d_tracer = tracing_init_dentry();
2882 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2883 &global_trace, &tracing_ctrl_fops);
2885 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2887 entry = debugfs_create_file("trace_options", 0644, d_tracer,
2888 NULL, &tracing_iter_fops);
2890 pr_warning("Could not create debugfs 'trace_options' entry\n");
2892 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2893 NULL, &tracing_cpumask_fops);
2895 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2897 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2898 &global_trace, &tracing_lt_fops);
2900 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2902 entry = debugfs_create_file("trace", 0444, d_tracer,
2903 &global_trace, &tracing_fops);
2905 pr_warning("Could not create debugfs 'trace' entry\n");
2907 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2908 &global_trace, &show_traces_fops);
2910 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2912 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2913 &global_trace, &set_tracer_fops);
2915 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2917 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2918 &tracing_max_latency,
2919 &tracing_max_lat_fops);
2921 pr_warning("Could not create debugfs "
2922 "'tracing_max_latency' entry\n");
2924 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2925 &tracing_thresh, &tracing_max_lat_fops);
2927 pr_warning("Could not create debugfs "
2928 "'tracing_thresh' entry\n");
2929 entry = debugfs_create_file("README", 0644, d_tracer,
2930 NULL, &tracing_readme_fops);
2932 pr_warning("Could not create debugfs 'README' entry\n");
2934 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2935 NULL, &tracing_pipe_fops);
2937 pr_warning("Could not create debugfs "
2938 "'trace_pipe' entry\n");
2940 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
2941 &global_trace, &tracing_entries_fops);
2943 pr_warning("Could not create debugfs "
2944 "'buffer_size_kb' entry\n");
2946 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2947 NULL, &tracing_mark_fops);
2949 pr_warning("Could not create debugfs "
2950 "'trace_marker' entry\n");
2952 #ifdef CONFIG_DYNAMIC_FTRACE
2953 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2954 &ftrace_update_tot_cnt,
2955 &tracing_dyn_info_fops);
2957 pr_warning("Could not create debugfs "
2958 "'dyn_ftrace_total_info' entry\n");
2960 #ifdef CONFIG_SYSPROF_TRACER
2961 init_tracer_sysprof_debugfs(d_tracer);
2966 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
2968 static DEFINE_SPINLOCK(trace_buf_lock);
2969 static char trace_buf[TRACE_BUF_SIZE];
2971 struct ring_buffer_event *event;
2972 struct trace_array *tr = &global_trace;
2973 struct trace_array_cpu *data;
2974 int cpu, len = 0, size, pc;
2975 struct print_entry *entry;
2976 unsigned long irq_flags;
2978 if (tracing_disabled || tracing_selftest_running)
2981 pc = preempt_count();
2982 preempt_disable_notrace();
2983 cpu = raw_smp_processor_id();
2984 data = tr->data[cpu];
2986 if (unlikely(atomic_read(&data->disabled)))
2989 pause_graph_tracing();
2990 spin_lock_irqsave(&trace_buf_lock, irq_flags);
2991 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
2993 len = min(len, TRACE_BUF_SIZE-1);
2996 size = sizeof(*entry) + len + 1;
2997 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3000 entry = ring_buffer_event_data(event);
3001 tracing_generic_entry_update(&entry->ent, irq_flags, pc);
3002 entry->ent.type = TRACE_PRINT;
3004 entry->depth = depth;
3006 memcpy(&entry->buf, trace_buf, len);
3007 entry->buf[len] = 0;
3008 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3011 spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
3012 unpause_graph_tracing();
3014 preempt_enable_notrace();
3018 EXPORT_SYMBOL_GPL(trace_vprintk);
3020 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3025 if (!(trace_flags & TRACE_ITER_PRINTK))
3029 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3033 EXPORT_SYMBOL_GPL(__ftrace_printk);
3035 static int trace_panic_handler(struct notifier_block *this,
3036 unsigned long event, void *unused)
3038 if (ftrace_dump_on_oops)
3043 static struct notifier_block trace_panic_notifier = {
3044 .notifier_call = trace_panic_handler,
3046 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3049 static int trace_die_handler(struct notifier_block *self,
3055 if (ftrace_dump_on_oops)
3064 static struct notifier_block trace_die_notifier = {
3065 .notifier_call = trace_die_handler,
3070 * printk is set to max of 1024, we really don't need it that big.
3071 * Nothing should be printing 1000 characters anyway.
3073 #define TRACE_MAX_PRINT 1000
3076 * Define here KERN_TRACE so that we have one place to modify
3077 * it if we decide to change what log level the ftrace dump
3080 #define KERN_TRACE KERN_INFO
3083 trace_printk_seq(struct trace_seq *s)
3085 /* Probably should print a warning here. */
3089 /* should be zero ended, but we are paranoid. */
3090 s->buffer[s->len] = 0;
3092 printk(KERN_TRACE "%s", s->buffer);
3097 void ftrace_dump(void)
3099 static DEFINE_SPINLOCK(ftrace_dump_lock);
3100 /* use static because iter can be a bit big for the stack */
3101 static struct trace_iterator iter;
3102 static int dump_ran;
3103 unsigned long flags;
3107 spin_lock_irqsave(&ftrace_dump_lock, flags);
3113 /* No turning back! */
3116 for_each_tracing_cpu(cpu) {
3117 atomic_inc(&global_trace.data[cpu]->disabled);
3120 /* don't look at user memory in panic mode */
3121 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3123 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3125 iter.tr = &global_trace;
3126 iter.trace = current_trace;
3129 * We need to stop all tracing on all CPUS to read the
3130 * the next buffer. This is a bit expensive, but is
3131 * not done often. We fill all what we can read,
3132 * and then release the locks again.
3135 while (!trace_empty(&iter)) {
3138 printk(KERN_TRACE "---------------------------------\n");
3142 /* reset all but tr, trace, and overruns */
3143 memset(&iter.seq, 0,
3144 sizeof(struct trace_iterator) -
3145 offsetof(struct trace_iterator, seq));
3146 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3149 if (find_next_entry_inc(&iter) != NULL) {
3150 print_trace_line(&iter);
3151 trace_consume(&iter);
3154 trace_printk_seq(&iter.seq);
3158 printk(KERN_TRACE " (ftrace buffer empty)\n");
3160 printk(KERN_TRACE "---------------------------------\n");
3163 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3166 __init static int tracer_alloc_buffers(void)
3168 struct trace_array_cpu *data;
3172 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3175 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3176 goto out_free_buffer_mask;
3178 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3179 cpumask_copy(tracing_cpumask, cpu_all_mask);
3181 /* TODO: make the number of buffers hot pluggable with CPUS */
3182 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3183 TRACE_BUFFER_FLAGS);
3184 if (!global_trace.buffer) {
3185 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3187 goto out_free_cpumask;
3189 global_trace.entries = ring_buffer_size(global_trace.buffer);
3192 #ifdef CONFIG_TRACER_MAX_TRACE
3193 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3194 TRACE_BUFFER_FLAGS);
3195 if (!max_tr.buffer) {
3196 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3198 ring_buffer_free(global_trace.buffer);
3199 goto out_free_cpumask;
3201 max_tr.entries = ring_buffer_size(max_tr.buffer);
3202 WARN_ON(max_tr.entries != global_trace.entries);
3205 /* Allocate the first page for all buffers */
3206 for_each_tracing_cpu(i) {
3207 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3208 max_tr.data[i] = &per_cpu(max_data, i);
3211 trace_init_cmdlines();
3213 register_tracer(&nop_trace);
3214 #ifdef CONFIG_BOOT_TRACER
3215 register_tracer(&boot_tracer);
3216 current_trace = &boot_tracer;
3217 current_trace->init(&global_trace);
3219 current_trace = &nop_trace;
3221 init_tracer_stat(current_trace);
3222 /* All seems OK, enable tracing */
3223 tracing_disabled = 0;
3225 atomic_notifier_chain_register(&panic_notifier_list,
3226 &trace_panic_notifier);
3228 register_die_notifier(&trace_die_notifier);
3232 free_cpumask_var(tracing_cpumask);
3233 out_free_buffer_mask:
3234 free_cpumask_var(tracing_buffer_mask);
3238 early_initcall(tracer_alloc_buffers);
3239 fs_initcall(tracer_init_debugfs);