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;
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;
191 * trace_buf_size is the size in bytes that is allocated
192 * for a buffer. Note, the number of bytes is always rounded
195 * This number is purposely set to a low number of 16384.
196 * If the dump on oops happens, it will be much appreciated
197 * to not have to wait for all that output. Anyway this can be
198 * boot time and run time configurable.
200 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
202 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
204 /* trace_types holds a link list of available tracers. */
205 static struct tracer *trace_types __read_mostly;
207 /* current_trace points to the tracer that is currently active */
208 static struct tracer *current_trace __read_mostly;
211 * max_tracer_type_len is used to simplify the allocating of
212 * buffers to read userspace tracer names. We keep track of
213 * the longest tracer name registered.
215 static int max_tracer_type_len;
218 * trace_types_lock is used to protect the trace_types list.
219 * This lock is also used to keep user access serialized.
220 * Accesses from userspace will grab this lock while userspace
221 * activities happen inside the kernel.
223 static DEFINE_MUTEX(trace_types_lock);
225 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
226 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
228 /* trace_flags holds trace_options default values */
229 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
233 * trace_wake_up - wake up tasks waiting for trace input
235 * Simply wakes up any task that is blocked on the trace_wait
236 * queue. These is used with trace_poll for tasks polling the trace.
238 void trace_wake_up(void)
241 * The runqueue_is_locked() can fail, but this is the best we
244 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
245 wake_up(&trace_wait);
248 static int __init set_buf_size(char *str)
250 unsigned long buf_size;
255 ret = strict_strtoul(str, 0, &buf_size);
256 /* nr_entries can not be zero */
257 if (ret < 0 || buf_size == 0)
259 trace_buf_size = buf_size;
262 __setup("trace_buf_size=", set_buf_size);
264 unsigned long nsecs_to_usecs(unsigned long nsecs)
269 /* These must match the bit postions in trace_iterator_flags */
270 static const char *trace_options[] = {
292 * ftrace_max_lock is used to protect the swapping of buffers
293 * when taking a max snapshot. The buffers themselves are
294 * protected by per_cpu spinlocks. But the action of the swap
295 * needs its own lock.
297 * This is defined as a raw_spinlock_t in order to help
298 * with performance when lockdep debugging is enabled.
300 static raw_spinlock_t ftrace_max_lock =
301 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
304 * Copy the new maximum trace into the separate maximum-trace
305 * structure. (this way the maximum trace is permanently saved,
306 * for later retrieval via /debugfs/tracing/latency_trace)
309 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
311 struct trace_array_cpu *data = tr->data[cpu];
314 max_tr.time_start = data->preempt_timestamp;
316 data = max_tr.data[cpu];
317 data->saved_latency = tracing_max_latency;
319 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
320 data->pid = tsk->pid;
321 data->uid = task_uid(tsk);
322 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
323 data->policy = tsk->policy;
324 data->rt_priority = tsk->rt_priority;
326 /* record this tasks comm */
327 tracing_record_cmdline(current);
331 trace_seq_reset(struct trace_seq *s)
337 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
342 if (s->len <= s->readpos)
345 len = s->len - s->readpos;
348 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
357 trace_print_seq(struct seq_file *m, struct trace_seq *s)
359 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
362 seq_puts(m, s->buffer);
368 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
370 * @tsk: the task with the latency
371 * @cpu: The cpu that initiated the trace.
373 * Flip the buffers between the @tr and the max_tr and record information
374 * about which task was the cause of this latency.
377 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
379 struct ring_buffer *buf = tr->buffer;
381 WARN_ON_ONCE(!irqs_disabled());
382 __raw_spin_lock(&ftrace_max_lock);
384 tr->buffer = max_tr.buffer;
387 ftrace_disable_cpu();
388 ring_buffer_reset(tr->buffer);
391 __update_max_tr(tr, tsk, cpu);
392 __raw_spin_unlock(&ftrace_max_lock);
396 * update_max_tr_single - only copy one trace over, and reset the rest
398 * @tsk - task with the latency
399 * @cpu - the cpu of the buffer to copy.
401 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
404 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
408 WARN_ON_ONCE(!irqs_disabled());
409 __raw_spin_lock(&ftrace_max_lock);
411 ftrace_disable_cpu();
413 ring_buffer_reset(max_tr.buffer);
414 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
418 WARN_ON_ONCE(ret && ret != -EAGAIN);
420 __update_max_tr(tr, tsk, cpu);
421 __raw_spin_unlock(&ftrace_max_lock);
425 * register_tracer - register a tracer with the ftrace system.
426 * @type - the plugin for the tracer
428 * Register a new plugin tracer.
430 int register_tracer(struct tracer *type)
437 pr_info("Tracer must have a name\n");
442 * When this gets called we hold the BKL which means that
443 * preemption is disabled. Various trace selftests however
444 * need to disable and enable preemption for successful tests.
445 * So we drop the BKL here and grab it after the tests again.
448 mutex_lock(&trace_types_lock);
450 tracing_selftest_running = true;
452 for (t = trace_types; t; t = t->next) {
453 if (strcmp(type->name, t->name) == 0) {
455 pr_info("Trace %s already registered\n",
463 type->set_flag = &dummy_set_flag;
465 type->flags = &dummy_tracer_flags;
467 if (!type->flags->opts)
468 type->flags->opts = dummy_tracer_opt;
470 #ifdef CONFIG_FTRACE_STARTUP_TEST
471 if (type->selftest) {
472 struct tracer *saved_tracer = current_trace;
473 struct trace_array *tr = &global_trace;
477 * Run a selftest on this tracer.
478 * Here we reset the trace buffer, and set the current
479 * tracer to be this tracer. The tracer can then run some
480 * internal tracing to verify that everything is in order.
481 * If we fail, we do not register this tracer.
483 for_each_tracing_cpu(i)
484 tracing_reset(tr, i);
486 current_trace = type;
487 /* the test is responsible for initializing and enabling */
488 pr_info("Testing tracer %s: ", type->name);
489 ret = type->selftest(type, tr);
490 /* the test is responsible for resetting too */
491 current_trace = saved_tracer;
493 printk(KERN_CONT "FAILED!\n");
496 /* Only reset on passing, to avoid touching corrupted buffers */
497 for_each_tracing_cpu(i)
498 tracing_reset(tr, i);
500 printk(KERN_CONT "PASSED\n");
504 type->next = trace_types;
506 len = strlen(type->name);
507 if (len > max_tracer_type_len)
508 max_tracer_type_len = len;
511 tracing_selftest_running = false;
512 mutex_unlock(&trace_types_lock);
518 void unregister_tracer(struct tracer *type)
523 mutex_lock(&trace_types_lock);
524 for (t = &trace_types; *t; t = &(*t)->next) {
528 pr_info("Trace %s not registered\n", type->name);
533 if (strlen(type->name) != max_tracer_type_len)
536 max_tracer_type_len = 0;
537 for (t = &trace_types; *t; t = &(*t)->next) {
538 len = strlen((*t)->name);
539 if (len > max_tracer_type_len)
540 max_tracer_type_len = len;
543 mutex_unlock(&trace_types_lock);
546 void tracing_reset(struct trace_array *tr, int cpu)
548 ftrace_disable_cpu();
549 ring_buffer_reset_cpu(tr->buffer, cpu);
553 void tracing_reset_online_cpus(struct trace_array *tr)
557 tr->time_start = ftrace_now(tr->cpu);
559 for_each_online_cpu(cpu)
560 tracing_reset(tr, cpu);
563 #define SAVED_CMDLINES 128
564 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
565 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
566 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
567 static int cmdline_idx;
568 static DEFINE_SPINLOCK(trace_cmdline_lock);
570 /* temporary disable recording */
571 atomic_t trace_record_cmdline_disabled __read_mostly;
573 static void trace_init_cmdlines(void)
575 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
576 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
580 static int trace_stop_count;
581 static DEFINE_SPINLOCK(tracing_start_lock);
584 * ftrace_off_permanent - disable all ftrace code permanently
586 * This should only be called when a serious anomally has
587 * been detected. This will turn off the function tracing,
588 * ring buffers, and other tracing utilites. It takes no
589 * locks and can be called from any context.
591 void ftrace_off_permanent(void)
593 tracing_disabled = 1;
595 tracing_off_permanent();
599 * tracing_start - quick start of the tracer
601 * If tracing is enabled but was stopped by tracing_stop,
602 * this will start the tracer back up.
604 void tracing_start(void)
606 struct ring_buffer *buffer;
609 if (tracing_disabled)
612 spin_lock_irqsave(&tracing_start_lock, flags);
613 if (--trace_stop_count) {
614 if (trace_stop_count < 0) {
615 /* Someone screwed up their debugging */
617 trace_stop_count = 0;
623 buffer = global_trace.buffer;
625 ring_buffer_record_enable(buffer);
627 buffer = max_tr.buffer;
629 ring_buffer_record_enable(buffer);
633 spin_unlock_irqrestore(&tracing_start_lock, flags);
637 * tracing_stop - quick stop of the tracer
639 * Light weight way to stop tracing. Use in conjunction with
642 void tracing_stop(void)
644 struct ring_buffer *buffer;
648 spin_lock_irqsave(&tracing_start_lock, flags);
649 if (trace_stop_count++)
652 buffer = global_trace.buffer;
654 ring_buffer_record_disable(buffer);
656 buffer = max_tr.buffer;
658 ring_buffer_record_disable(buffer);
661 spin_unlock_irqrestore(&tracing_start_lock, flags);
664 void trace_stop_cmdline_recording(void);
666 static void trace_save_cmdline(struct task_struct *tsk)
671 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
675 * It's not the end of the world if we don't get
676 * the lock, but we also don't want to spin
677 * nor do we want to disable interrupts,
678 * so if we miss here, then better luck next time.
680 if (!spin_trylock(&trace_cmdline_lock))
683 idx = map_pid_to_cmdline[tsk->pid];
684 if (idx >= SAVED_CMDLINES) {
685 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
687 map = map_cmdline_to_pid[idx];
688 if (map <= PID_MAX_DEFAULT)
689 map_pid_to_cmdline[map] = (unsigned)-1;
691 map_pid_to_cmdline[tsk->pid] = idx;
696 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
698 spin_unlock(&trace_cmdline_lock);
701 char *trace_find_cmdline(int pid)
703 char *cmdline = "<...>";
709 if (pid > PID_MAX_DEFAULT)
712 map = map_pid_to_cmdline[pid];
713 if (map >= SAVED_CMDLINES)
716 cmdline = saved_cmdlines[map];
722 void tracing_record_cmdline(struct task_struct *tsk)
724 if (atomic_read(&trace_record_cmdline_disabled))
727 trace_save_cmdline(tsk);
731 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
734 struct task_struct *tsk = current;
736 entry->preempt_count = pc & 0xff;
737 entry->pid = (tsk) ? tsk->pid : 0;
738 entry->tgid = (tsk) ? tsk->tgid : 0;
740 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
741 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
743 TRACE_FLAG_IRQS_NOSUPPORT |
745 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
746 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
747 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
751 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
752 unsigned long ip, unsigned long parent_ip, unsigned long flags,
755 struct ring_buffer_event *event;
756 struct ftrace_entry *entry;
757 unsigned long irq_flags;
759 /* If we are reading the ring buffer, don't trace */
760 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
763 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
767 entry = ring_buffer_event_data(event);
768 tracing_generic_entry_update(&entry->ent, flags, pc);
769 entry->ent.type = TRACE_FN;
771 entry->parent_ip = parent_ip;
772 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
775 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
776 static void __trace_graph_entry(struct trace_array *tr,
777 struct trace_array_cpu *data,
778 struct ftrace_graph_ent *trace,
782 struct ring_buffer_event *event;
783 struct ftrace_graph_ent_entry *entry;
784 unsigned long irq_flags;
786 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
789 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
793 entry = ring_buffer_event_data(event);
794 tracing_generic_entry_update(&entry->ent, flags, pc);
795 entry->ent.type = TRACE_GRAPH_ENT;
796 entry->graph_ent = *trace;
797 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
800 static void __trace_graph_return(struct trace_array *tr,
801 struct trace_array_cpu *data,
802 struct ftrace_graph_ret *trace,
806 struct ring_buffer_event *event;
807 struct ftrace_graph_ret_entry *entry;
808 unsigned long irq_flags;
810 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
813 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
817 entry = ring_buffer_event_data(event);
818 tracing_generic_entry_update(&entry->ent, flags, pc);
819 entry->ent.type = TRACE_GRAPH_RET;
821 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
826 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
827 unsigned long ip, unsigned long parent_ip, unsigned long flags,
830 if (likely(!atomic_read(&data->disabled)))
831 trace_function(tr, data, ip, parent_ip, flags, pc);
834 static void __ftrace_trace_stack(struct trace_array *tr,
835 struct trace_array_cpu *data,
839 #ifdef CONFIG_STACKTRACE
840 struct ring_buffer_event *event;
841 struct stack_entry *entry;
842 struct stack_trace trace;
843 unsigned long irq_flags;
845 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
849 entry = ring_buffer_event_data(event);
850 tracing_generic_entry_update(&entry->ent, flags, pc);
851 entry->ent.type = TRACE_STACK;
853 memset(&entry->caller, 0, sizeof(entry->caller));
855 trace.nr_entries = 0;
856 trace.max_entries = FTRACE_STACK_ENTRIES;
858 trace.entries = entry->caller;
860 save_stack_trace(&trace);
861 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
865 static void ftrace_trace_stack(struct trace_array *tr,
866 struct trace_array_cpu *data,
870 if (!(trace_flags & TRACE_ITER_STACKTRACE))
873 __ftrace_trace_stack(tr, data, flags, skip, pc);
876 void __trace_stack(struct trace_array *tr,
877 struct trace_array_cpu *data,
881 __ftrace_trace_stack(tr, data, flags, skip, pc);
884 static void ftrace_trace_userstack(struct trace_array *tr,
885 struct trace_array_cpu *data,
886 unsigned long flags, int pc)
888 #ifdef CONFIG_STACKTRACE
889 struct ring_buffer_event *event;
890 struct userstack_entry *entry;
891 struct stack_trace trace;
892 unsigned long irq_flags;
894 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
897 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
901 entry = ring_buffer_event_data(event);
902 tracing_generic_entry_update(&entry->ent, flags, pc);
903 entry->ent.type = TRACE_USER_STACK;
905 memset(&entry->caller, 0, sizeof(entry->caller));
907 trace.nr_entries = 0;
908 trace.max_entries = FTRACE_STACK_ENTRIES;
910 trace.entries = entry->caller;
912 save_stack_trace_user(&trace);
913 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
917 void __trace_userstack(struct trace_array *tr,
918 struct trace_array_cpu *data,
921 ftrace_trace_userstack(tr, data, flags, preempt_count());
925 ftrace_trace_special(void *__tr, void *__data,
926 unsigned long arg1, unsigned long arg2, unsigned long arg3,
929 struct ring_buffer_event *event;
930 struct trace_array_cpu *data = __data;
931 struct trace_array *tr = __tr;
932 struct special_entry *entry;
933 unsigned long irq_flags;
935 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
939 entry = ring_buffer_event_data(event);
940 tracing_generic_entry_update(&entry->ent, 0, pc);
941 entry->ent.type = TRACE_SPECIAL;
945 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
946 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
947 ftrace_trace_userstack(tr, data, irq_flags, pc);
953 __trace_special(void *__tr, void *__data,
954 unsigned long arg1, unsigned long arg2, unsigned long arg3)
956 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
960 tracing_sched_switch_trace(struct trace_array *tr,
961 struct trace_array_cpu *data,
962 struct task_struct *prev,
963 struct task_struct *next,
964 unsigned long flags, int pc)
966 struct ring_buffer_event *event;
967 struct ctx_switch_entry *entry;
968 unsigned long irq_flags;
970 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
974 entry = ring_buffer_event_data(event);
975 tracing_generic_entry_update(&entry->ent, flags, pc);
976 entry->ent.type = TRACE_CTX;
977 entry->prev_pid = prev->pid;
978 entry->prev_prio = prev->prio;
979 entry->prev_state = prev->state;
980 entry->next_pid = next->pid;
981 entry->next_prio = next->prio;
982 entry->next_state = next->state;
983 entry->next_cpu = task_cpu(next);
984 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
985 ftrace_trace_stack(tr, data, flags, 5, pc);
986 ftrace_trace_userstack(tr, data, flags, pc);
990 tracing_sched_wakeup_trace(struct trace_array *tr,
991 struct trace_array_cpu *data,
992 struct task_struct *wakee,
993 struct task_struct *curr,
994 unsigned long flags, int pc)
996 struct ring_buffer_event *event;
997 struct ctx_switch_entry *entry;
998 unsigned long irq_flags;
1000 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1004 entry = ring_buffer_event_data(event);
1005 tracing_generic_entry_update(&entry->ent, flags, pc);
1006 entry->ent.type = TRACE_WAKE;
1007 entry->prev_pid = curr->pid;
1008 entry->prev_prio = curr->prio;
1009 entry->prev_state = curr->state;
1010 entry->next_pid = wakee->pid;
1011 entry->next_prio = wakee->prio;
1012 entry->next_state = wakee->state;
1013 entry->next_cpu = task_cpu(wakee);
1014 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1015 ftrace_trace_stack(tr, data, flags, 6, pc);
1016 ftrace_trace_userstack(tr, data, flags, pc);
1022 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1024 struct trace_array *tr = &global_trace;
1025 struct trace_array_cpu *data;
1026 unsigned long flags;
1030 if (tracing_disabled)
1033 pc = preempt_count();
1034 local_irq_save(flags);
1035 cpu = raw_smp_processor_id();
1036 data = tr->data[cpu];
1038 if (likely(atomic_inc_return(&data->disabled) == 1))
1039 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1041 atomic_dec(&data->disabled);
1042 local_irq_restore(flags);
1045 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1046 int trace_graph_entry(struct ftrace_graph_ent *trace)
1048 struct trace_array *tr = &global_trace;
1049 struct trace_array_cpu *data;
1050 unsigned long flags;
1055 if (!ftrace_trace_task(current))
1058 if (!ftrace_graph_addr(trace->func))
1061 local_irq_save(flags);
1062 cpu = raw_smp_processor_id();
1063 data = tr->data[cpu];
1064 disabled = atomic_inc_return(&data->disabled);
1065 if (likely(disabled == 1)) {
1066 pc = preempt_count();
1067 __trace_graph_entry(tr, data, trace, flags, pc);
1069 /* Only do the atomic if it is not already set */
1070 if (!test_tsk_trace_graph(current))
1071 set_tsk_trace_graph(current);
1072 atomic_dec(&data->disabled);
1073 local_irq_restore(flags);
1078 void trace_graph_return(struct ftrace_graph_ret *trace)
1080 struct trace_array *tr = &global_trace;
1081 struct trace_array_cpu *data;
1082 unsigned long flags;
1087 local_irq_save(flags);
1088 cpu = raw_smp_processor_id();
1089 data = tr->data[cpu];
1090 disabled = atomic_inc_return(&data->disabled);
1091 if (likely(disabled == 1)) {
1092 pc = preempt_count();
1093 __trace_graph_return(tr, data, trace, flags, pc);
1096 clear_tsk_trace_graph(current);
1097 atomic_dec(&data->disabled);
1098 local_irq_restore(flags);
1100 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1102 enum trace_file_type {
1103 TRACE_FILE_LAT_FMT = 1,
1104 TRACE_FILE_ANNOTATE = 2,
1107 static void trace_iterator_increment(struct trace_iterator *iter)
1109 /* Don't allow ftrace to trace into the ring buffers */
1110 ftrace_disable_cpu();
1113 if (iter->buffer_iter[iter->cpu])
1114 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1116 ftrace_enable_cpu();
1119 static struct trace_entry *
1120 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1122 struct ring_buffer_event *event;
1123 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1125 /* Don't allow ftrace to trace into the ring buffers */
1126 ftrace_disable_cpu();
1129 event = ring_buffer_iter_peek(buf_iter, ts);
1131 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1133 ftrace_enable_cpu();
1135 return event ? ring_buffer_event_data(event) : NULL;
1138 static struct trace_entry *
1139 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1141 struct ring_buffer *buffer = iter->tr->buffer;
1142 struct trace_entry *ent, *next = NULL;
1143 u64 next_ts = 0, ts;
1147 for_each_tracing_cpu(cpu) {
1149 if (ring_buffer_empty_cpu(buffer, cpu))
1152 ent = peek_next_entry(iter, cpu, &ts);
1155 * Pick the entry with the smallest timestamp:
1157 if (ent && (!next || ts < next_ts)) {
1165 *ent_cpu = next_cpu;
1173 /* Find the next real entry, without updating the iterator itself */
1174 static struct trace_entry *
1175 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1177 return __find_next_entry(iter, ent_cpu, ent_ts);
1180 /* Find the next real entry, and increment the iterator to the next entry */
1181 static void *find_next_entry_inc(struct trace_iterator *iter)
1183 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1186 trace_iterator_increment(iter);
1188 return iter->ent ? iter : NULL;
1191 static void trace_consume(struct trace_iterator *iter)
1193 /* Don't allow ftrace to trace into the ring buffers */
1194 ftrace_disable_cpu();
1195 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1196 ftrace_enable_cpu();
1199 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1201 struct trace_iterator *iter = m->private;
1207 /* can't go backwards */
1212 ent = find_next_entry_inc(iter);
1216 while (ent && iter->idx < i)
1217 ent = find_next_entry_inc(iter);
1224 static void *s_start(struct seq_file *m, loff_t *pos)
1226 struct trace_iterator *iter = m->private;
1231 mutex_lock(&trace_types_lock);
1233 if (!current_trace || current_trace != iter->trace) {
1234 mutex_unlock(&trace_types_lock);
1238 atomic_inc(&trace_record_cmdline_disabled);
1240 if (*pos != iter->pos) {
1245 ftrace_disable_cpu();
1247 for_each_tracing_cpu(cpu) {
1248 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1251 ftrace_enable_cpu();
1253 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1258 p = s_next(m, p, &l);
1264 static void s_stop(struct seq_file *m, void *p)
1266 atomic_dec(&trace_record_cmdline_disabled);
1267 mutex_unlock(&trace_types_lock);
1270 static void print_lat_help_header(struct seq_file *m)
1272 seq_puts(m, "# _------=> CPU# \n");
1273 seq_puts(m, "# / _-----=> irqs-off \n");
1274 seq_puts(m, "# | / _----=> need-resched \n");
1275 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1276 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1277 seq_puts(m, "# |||| / \n");
1278 seq_puts(m, "# ||||| delay \n");
1279 seq_puts(m, "# cmd pid ||||| time | caller \n");
1280 seq_puts(m, "# \\ / ||||| \\ | / \n");
1283 static void print_func_help_header(struct seq_file *m)
1285 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1286 seq_puts(m, "# | | | | |\n");
1291 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1293 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1294 struct trace_array *tr = iter->tr;
1295 struct trace_array_cpu *data = tr->data[tr->cpu];
1296 struct tracer *type = current_trace;
1297 unsigned long total;
1298 unsigned long entries;
1299 const char *name = "preemption";
1304 entries = ring_buffer_entries(iter->tr->buffer);
1306 ring_buffer_overruns(iter->tr->buffer);
1308 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1310 seq_puts(m, "-----------------------------------"
1311 "---------------------------------\n");
1312 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1313 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1314 nsecs_to_usecs(data->saved_latency),
1318 #if defined(CONFIG_PREEMPT_NONE)
1320 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1322 #elif defined(CONFIG_PREEMPT)
1327 /* These are reserved for later use */
1330 seq_printf(m, " #P:%d)\n", num_online_cpus());
1334 seq_puts(m, " -----------------\n");
1335 seq_printf(m, " | task: %.16s-%d "
1336 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1337 data->comm, data->pid, data->uid, data->nice,
1338 data->policy, data->rt_priority);
1339 seq_puts(m, " -----------------\n");
1341 if (data->critical_start) {
1342 seq_puts(m, " => started at: ");
1343 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1344 trace_print_seq(m, &iter->seq);
1345 seq_puts(m, "\n => ended at: ");
1346 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1347 trace_print_seq(m, &iter->seq);
1355 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1357 int hardirq, softirq;
1360 comm = trace_find_cmdline(entry->pid);
1362 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1363 trace_seq_printf(s, "%3d", cpu);
1364 trace_seq_printf(s, "%c%c",
1365 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1366 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1367 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1369 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1370 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1371 if (hardirq && softirq) {
1372 trace_seq_putc(s, 'H');
1375 trace_seq_putc(s, 'h');
1378 trace_seq_putc(s, 's');
1380 trace_seq_putc(s, '.');
1384 if (entry->preempt_count)
1385 trace_seq_printf(s, "%x", entry->preempt_count);
1387 trace_seq_puts(s, ".");
1390 unsigned long preempt_mark_thresh = 100;
1393 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1394 unsigned long rel_usecs)
1396 trace_seq_printf(s, " %4lldus", abs_usecs);
1397 if (rel_usecs > preempt_mark_thresh)
1398 trace_seq_puts(s, "!: ");
1399 else if (rel_usecs > 1)
1400 trace_seq_puts(s, "+: ");
1402 trace_seq_puts(s, " : ");
1405 static void test_cpu_buff_start(struct trace_iterator *iter)
1407 struct trace_seq *s = &iter->seq;
1409 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1412 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1415 if (cpumask_test_cpu(iter->cpu, iter->started))
1418 cpumask_set_cpu(iter->cpu, iter->started);
1419 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1422 static enum print_line_t
1423 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1425 struct trace_seq *s = &iter->seq;
1426 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1427 struct trace_entry *next_entry;
1428 struct trace_event *event;
1429 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1430 struct trace_entry *entry = iter->ent;
1431 unsigned long abs_usecs;
1432 unsigned long rel_usecs;
1437 test_cpu_buff_start(iter);
1439 next_entry = find_next_entry(iter, NULL, &next_ts);
1442 rel_usecs = ns2usecs(next_ts - iter->ts);
1443 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1446 comm = trace_find_cmdline(entry->pid);
1447 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1448 " %ld.%03ldms (+%ld.%03ldms): ",
1450 entry->pid, cpu, entry->flags,
1451 entry->preempt_count, trace_idx,
1454 abs_usecs % 1000, rel_usecs/1000,
1457 lat_print_generic(s, entry, cpu);
1458 lat_print_timestamp(s, abs_usecs, rel_usecs);
1461 event = ftrace_find_event(entry->type);
1462 if (event && event->latency_trace) {
1463 ret = event->latency_trace(s, entry, sym_flags);
1466 return TRACE_TYPE_HANDLED;
1469 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1470 return TRACE_TYPE_HANDLED;
1473 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1475 struct trace_seq *s = &iter->seq;
1476 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1477 struct trace_entry *entry;
1478 struct trace_event *event;
1479 unsigned long usec_rem;
1480 unsigned long long t;
1487 test_cpu_buff_start(iter);
1489 comm = trace_find_cmdline(iter->ent->pid);
1491 t = ns2usecs(iter->ts);
1492 usec_rem = do_div(t, 1000000ULL);
1493 secs = (unsigned long)t;
1495 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1497 return TRACE_TYPE_PARTIAL_LINE;
1498 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1500 return TRACE_TYPE_PARTIAL_LINE;
1501 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1503 return TRACE_TYPE_PARTIAL_LINE;
1505 event = ftrace_find_event(entry->type);
1506 if (event && event->trace) {
1507 ret = event->trace(s, entry, sym_flags);
1510 return TRACE_TYPE_HANDLED;
1512 ret = trace_seq_printf(s, "Unknown type %d\n", entry->type);
1514 return TRACE_TYPE_PARTIAL_LINE;
1516 return TRACE_TYPE_HANDLED;
1519 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1521 struct trace_seq *s = &iter->seq;
1522 struct trace_entry *entry;
1523 struct trace_event *event;
1528 ret = trace_seq_printf(s, "%d %d %llu ",
1529 entry->pid, iter->cpu, iter->ts);
1531 return TRACE_TYPE_PARTIAL_LINE;
1533 event = ftrace_find_event(entry->type);
1534 if (event && event->raw) {
1535 ret = event->raw(s, entry, 0);
1538 return TRACE_TYPE_HANDLED;
1540 ret = trace_seq_printf(s, "%d ?\n", entry->type);
1542 return TRACE_TYPE_PARTIAL_LINE;
1544 return TRACE_TYPE_HANDLED;
1547 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1549 struct trace_seq *s = &iter->seq;
1550 unsigned char newline = '\n';
1551 struct trace_entry *entry;
1552 struct trace_event *event;
1556 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1557 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1558 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1560 event = ftrace_find_event(entry->type);
1561 if (event && event->hex)
1562 event->hex(s, entry, 0);
1564 SEQ_PUT_FIELD_RET(s, newline);
1566 return TRACE_TYPE_HANDLED;
1569 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1571 struct trace_seq *s = &iter->seq;
1572 struct trace_entry *entry = iter->ent;
1573 struct print_entry *field;
1576 trace_assign_type(field, entry);
1578 ret = trace_seq_printf(s, field->buf);
1580 return TRACE_TYPE_PARTIAL_LINE;
1582 return TRACE_TYPE_HANDLED;
1585 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1587 struct trace_seq *s = &iter->seq;
1588 struct trace_entry *entry;
1589 struct trace_event *event;
1593 SEQ_PUT_FIELD_RET(s, entry->pid);
1594 SEQ_PUT_FIELD_RET(s, entry->cpu);
1595 SEQ_PUT_FIELD_RET(s, iter->ts);
1597 event = ftrace_find_event(entry->type);
1598 if (event && event->binary)
1599 event->binary(s, entry, 0);
1601 return TRACE_TYPE_HANDLED;
1604 static int trace_empty(struct trace_iterator *iter)
1608 for_each_tracing_cpu(cpu) {
1609 if (iter->buffer_iter[cpu]) {
1610 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1613 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1621 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1623 enum print_line_t ret;
1625 if (iter->trace && iter->trace->print_line) {
1626 ret = iter->trace->print_line(iter);
1627 if (ret != TRACE_TYPE_UNHANDLED)
1631 if (iter->ent->type == TRACE_PRINT &&
1632 trace_flags & TRACE_ITER_PRINTK &&
1633 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1634 return print_printk_msg_only(iter);
1636 if (trace_flags & TRACE_ITER_BIN)
1637 return print_bin_fmt(iter);
1639 if (trace_flags & TRACE_ITER_HEX)
1640 return print_hex_fmt(iter);
1642 if (trace_flags & TRACE_ITER_RAW)
1643 return print_raw_fmt(iter);
1645 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1646 return print_lat_fmt(iter, iter->idx, iter->cpu);
1648 return print_trace_fmt(iter);
1651 static int s_show(struct seq_file *m, void *v)
1653 struct trace_iterator *iter = v;
1655 if (iter->ent == NULL) {
1657 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1660 if (iter->trace && iter->trace->print_header)
1661 iter->trace->print_header(m);
1662 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1663 /* print nothing if the buffers are empty */
1664 if (trace_empty(iter))
1666 print_trace_header(m, iter);
1667 if (!(trace_flags & TRACE_ITER_VERBOSE))
1668 print_lat_help_header(m);
1670 if (!(trace_flags & TRACE_ITER_VERBOSE))
1671 print_func_help_header(m);
1674 print_trace_line(iter);
1675 trace_print_seq(m, &iter->seq);
1681 static struct seq_operations tracer_seq_ops = {
1688 static struct trace_iterator *
1689 __tracing_open(struct inode *inode, struct file *file, int *ret)
1691 struct trace_iterator *iter;
1695 if (tracing_disabled) {
1700 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1706 mutex_lock(&trace_types_lock);
1707 if (current_trace && current_trace->print_max)
1710 iter->tr = inode->i_private;
1711 iter->trace = current_trace;
1714 /* Notify the tracer early; before we stop tracing. */
1715 if (iter->trace && iter->trace->open)
1716 iter->trace->open(iter);
1718 /* Annotate start of buffers if we had overruns */
1719 if (ring_buffer_overruns(iter->tr->buffer))
1720 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1723 for_each_tracing_cpu(cpu) {
1725 iter->buffer_iter[cpu] =
1726 ring_buffer_read_start(iter->tr->buffer, cpu);
1728 if (!iter->buffer_iter[cpu])
1732 /* TODO stop tracer */
1733 *ret = seq_open(file, &tracer_seq_ops);
1737 m = file->private_data;
1740 /* stop the trace while dumping */
1743 mutex_unlock(&trace_types_lock);
1749 for_each_tracing_cpu(cpu) {
1750 if (iter->buffer_iter[cpu])
1751 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1753 mutex_unlock(&trace_types_lock);
1756 return ERR_PTR(-ENOMEM);
1759 int tracing_open_generic(struct inode *inode, struct file *filp)
1761 if (tracing_disabled)
1764 filp->private_data = inode->i_private;
1768 int tracing_release(struct inode *inode, struct file *file)
1770 struct seq_file *m = (struct seq_file *)file->private_data;
1771 struct trace_iterator *iter = m->private;
1774 mutex_lock(&trace_types_lock);
1775 for_each_tracing_cpu(cpu) {
1776 if (iter->buffer_iter[cpu])
1777 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1780 if (iter->trace && iter->trace->close)
1781 iter->trace->close(iter);
1783 /* reenable tracing if it was previously enabled */
1785 mutex_unlock(&trace_types_lock);
1787 seq_release(inode, file);
1792 static int tracing_open(struct inode *inode, struct file *file)
1796 __tracing_open(inode, file, &ret);
1801 static int tracing_lt_open(struct inode *inode, struct file *file)
1803 struct trace_iterator *iter;
1806 iter = __tracing_open(inode, file, &ret);
1809 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1816 t_next(struct seq_file *m, void *v, loff_t *pos)
1818 struct tracer *t = m->private;
1830 static void *t_start(struct seq_file *m, loff_t *pos)
1832 struct tracer *t = m->private;
1835 mutex_lock(&trace_types_lock);
1836 for (; t && l < *pos; t = t_next(m, t, &l))
1842 static void t_stop(struct seq_file *m, void *p)
1844 mutex_unlock(&trace_types_lock);
1847 static int t_show(struct seq_file *m, void *v)
1849 struct tracer *t = v;
1854 seq_printf(m, "%s", t->name);
1863 static struct seq_operations show_traces_seq_ops = {
1870 static int show_traces_open(struct inode *inode, struct file *file)
1874 if (tracing_disabled)
1877 ret = seq_open(file, &show_traces_seq_ops);
1879 struct seq_file *m = file->private_data;
1880 m->private = trace_types;
1886 static struct file_operations tracing_fops = {
1887 .open = tracing_open,
1889 .llseek = seq_lseek,
1890 .release = tracing_release,
1893 static struct file_operations tracing_lt_fops = {
1894 .open = tracing_lt_open,
1896 .llseek = seq_lseek,
1897 .release = tracing_release,
1900 static struct file_operations show_traces_fops = {
1901 .open = show_traces_open,
1903 .release = seq_release,
1907 * Only trace on a CPU if the bitmask is set:
1909 static cpumask_var_t tracing_cpumask;
1912 * The tracer itself will not take this lock, but still we want
1913 * to provide a consistent cpumask to user-space:
1915 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1918 * Temporary storage for the character representation of the
1919 * CPU bitmask (and one more byte for the newline):
1921 static char mask_str[NR_CPUS + 1];
1924 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1925 size_t count, loff_t *ppos)
1929 mutex_lock(&tracing_cpumask_update_lock);
1931 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1932 if (count - len < 2) {
1936 len += sprintf(mask_str + len, "\n");
1937 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1940 mutex_unlock(&tracing_cpumask_update_lock);
1946 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1947 size_t count, loff_t *ppos)
1950 cpumask_var_t tracing_cpumask_new;
1952 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1955 mutex_lock(&tracing_cpumask_update_lock);
1956 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1960 local_irq_disable();
1961 __raw_spin_lock(&ftrace_max_lock);
1962 for_each_tracing_cpu(cpu) {
1964 * Increase/decrease the disabled counter if we are
1965 * about to flip a bit in the cpumask:
1967 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
1968 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
1969 atomic_inc(&global_trace.data[cpu]->disabled);
1971 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
1972 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
1973 atomic_dec(&global_trace.data[cpu]->disabled);
1976 __raw_spin_unlock(&ftrace_max_lock);
1979 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
1981 mutex_unlock(&tracing_cpumask_update_lock);
1982 free_cpumask_var(tracing_cpumask_new);
1987 mutex_unlock(&tracing_cpumask_update_lock);
1988 free_cpumask_var(tracing_cpumask);
1993 static struct file_operations tracing_cpumask_fops = {
1994 .open = tracing_open_generic,
1995 .read = tracing_cpumask_read,
1996 .write = tracing_cpumask_write,
2000 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2001 size_t cnt, loff_t *ppos)
2007 u32 tracer_flags = current_trace->flags->val;
2008 struct tracer_opt *trace_opts = current_trace->flags->opts;
2011 /* calulate max size */
2012 for (i = 0; trace_options[i]; i++) {
2013 len += strlen(trace_options[i]);
2014 len += 3; /* "no" and space */
2018 * Increase the size with names of options specific
2019 * of the current tracer.
2021 for (i = 0; trace_opts[i].name; i++) {
2022 len += strlen(trace_opts[i].name);
2023 len += 3; /* "no" and space */
2026 /* +2 for \n and \0 */
2027 buf = kmalloc(len + 2, GFP_KERNEL);
2031 for (i = 0; trace_options[i]; i++) {
2032 if (trace_flags & (1 << i))
2033 r += sprintf(buf + r, "%s ", trace_options[i]);
2035 r += sprintf(buf + r, "no%s ", trace_options[i]);
2038 for (i = 0; trace_opts[i].name; i++) {
2039 if (tracer_flags & trace_opts[i].bit)
2040 r += sprintf(buf + r, "%s ",
2041 trace_opts[i].name);
2043 r += sprintf(buf + r, "no%s ",
2044 trace_opts[i].name);
2047 r += sprintf(buf + r, "\n");
2048 WARN_ON(r >= len + 2);
2050 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2057 /* Try to assign a tracer specific option */
2058 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2060 struct tracer_flags *trace_flags = trace->flags;
2061 struct tracer_opt *opts = NULL;
2065 for (i = 0; trace_flags->opts[i].name; i++) {
2066 opts = &trace_flags->opts[i];
2067 len = strlen(opts->name);
2069 if (strncmp(cmp, opts->name, len) == 0) {
2070 ret = trace->set_flag(trace_flags->val,
2076 if (!trace_flags->opts[i].name)
2079 /* Refused to handle */
2084 trace_flags->val &= ~opts->bit;
2086 trace_flags->val |= opts->bit;
2092 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2093 size_t cnt, loff_t *ppos)
2101 if (cnt >= sizeof(buf))
2104 if (copy_from_user(&buf, ubuf, cnt))
2109 if (strncmp(buf, "no", 2) == 0) {
2114 for (i = 0; trace_options[i]; i++) {
2115 int len = strlen(trace_options[i]);
2117 if (strncmp(cmp, trace_options[i], len) == 0) {
2119 trace_flags &= ~(1 << i);
2121 trace_flags |= (1 << i);
2126 /* If no option could be set, test the specific tracer options */
2127 if (!trace_options[i]) {
2128 ret = set_tracer_option(current_trace, cmp, neg);
2138 static struct file_operations tracing_iter_fops = {
2139 .open = tracing_open_generic,
2140 .read = tracing_trace_options_read,
2141 .write = tracing_trace_options_write,
2144 static const char readme_msg[] =
2145 "tracing mini-HOWTO:\n\n"
2147 "# mount -t debugfs nodev /debug\n\n"
2148 "# cat /debug/tracing/available_tracers\n"
2149 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2150 "# cat /debug/tracing/current_tracer\n"
2152 "# echo sched_switch > /debug/tracing/current_tracer\n"
2153 "# cat /debug/tracing/current_tracer\n"
2155 "# cat /debug/tracing/trace_options\n"
2156 "noprint-parent nosym-offset nosym-addr noverbose\n"
2157 "# echo print-parent > /debug/tracing/trace_options\n"
2158 "# echo 1 > /debug/tracing/tracing_enabled\n"
2159 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2160 "echo 0 > /debug/tracing/tracing_enabled\n"
2164 tracing_readme_read(struct file *filp, char __user *ubuf,
2165 size_t cnt, loff_t *ppos)
2167 return simple_read_from_buffer(ubuf, cnt, ppos,
2168 readme_msg, strlen(readme_msg));
2171 static struct file_operations tracing_readme_fops = {
2172 .open = tracing_open_generic,
2173 .read = tracing_readme_read,
2177 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2178 size_t cnt, loff_t *ppos)
2183 r = sprintf(buf, "%u\n", tracer_enabled);
2184 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2188 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2189 size_t cnt, loff_t *ppos)
2191 struct trace_array *tr = filp->private_data;
2196 if (cnt >= sizeof(buf))
2199 if (copy_from_user(&buf, ubuf, cnt))
2204 ret = strict_strtoul(buf, 10, &val);
2210 mutex_lock(&trace_types_lock);
2211 if (tracer_enabled ^ val) {
2214 if (current_trace->start)
2215 current_trace->start(tr);
2220 if (current_trace->stop)
2221 current_trace->stop(tr);
2224 mutex_unlock(&trace_types_lock);
2232 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2233 size_t cnt, loff_t *ppos)
2235 char buf[max_tracer_type_len+2];
2238 mutex_lock(&trace_types_lock);
2240 r = sprintf(buf, "%s\n", current_trace->name);
2242 r = sprintf(buf, "\n");
2243 mutex_unlock(&trace_types_lock);
2245 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2248 static int tracing_set_tracer(char *buf)
2250 struct trace_array *tr = &global_trace;
2254 mutex_lock(&trace_types_lock);
2255 for (t = trace_types; t; t = t->next) {
2256 if (strcmp(t->name, buf) == 0)
2263 if (t == current_trace)
2266 trace_branch_disable();
2267 if (current_trace && current_trace->reset)
2268 current_trace->reset(tr);
2277 trace_branch_enable(tr);
2279 mutex_unlock(&trace_types_lock);
2285 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2286 size_t cnt, loff_t *ppos)
2288 char buf[max_tracer_type_len+1];
2295 if (cnt > max_tracer_type_len)
2296 cnt = max_tracer_type_len;
2298 if (copy_from_user(&buf, ubuf, cnt))
2303 /* strip ending whitespace. */
2304 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2307 err = tracing_set_tracer(buf);
2317 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2318 size_t cnt, loff_t *ppos)
2320 unsigned long *ptr = filp->private_data;
2324 r = snprintf(buf, sizeof(buf), "%ld\n",
2325 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2326 if (r > sizeof(buf))
2328 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2332 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2333 size_t cnt, loff_t *ppos)
2335 long *ptr = filp->private_data;
2340 if (cnt >= sizeof(buf))
2343 if (copy_from_user(&buf, ubuf, cnt))
2348 ret = strict_strtoul(buf, 10, &val);
2357 static atomic_t tracing_reader;
2359 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2361 struct trace_iterator *iter;
2363 if (tracing_disabled)
2366 /* We only allow for reader of the pipe */
2367 if (atomic_inc_return(&tracing_reader) != 1) {
2368 atomic_dec(&tracing_reader);
2372 /* create a buffer to store the information to pass to userspace */
2373 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2377 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2382 mutex_lock(&trace_types_lock);
2384 /* trace pipe does not show start of buffer */
2385 cpumask_setall(iter->started);
2387 iter->tr = &global_trace;
2388 iter->trace = current_trace;
2389 filp->private_data = iter;
2391 if (iter->trace->pipe_open)
2392 iter->trace->pipe_open(iter);
2393 mutex_unlock(&trace_types_lock);
2398 static int tracing_release_pipe(struct inode *inode, struct file *file)
2400 struct trace_iterator *iter = file->private_data;
2402 free_cpumask_var(iter->started);
2404 atomic_dec(&tracing_reader);
2410 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2412 struct trace_iterator *iter = filp->private_data;
2414 if (trace_flags & TRACE_ITER_BLOCK) {
2416 * Always select as readable when in blocking mode
2418 return POLLIN | POLLRDNORM;
2420 if (!trace_empty(iter))
2421 return POLLIN | POLLRDNORM;
2422 poll_wait(filp, &trace_wait, poll_table);
2423 if (!trace_empty(iter))
2424 return POLLIN | POLLRDNORM;
2434 tracing_read_pipe(struct file *filp, char __user *ubuf,
2435 size_t cnt, loff_t *ppos)
2437 struct trace_iterator *iter = filp->private_data;
2440 /* return any leftover data */
2441 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2445 trace_seq_reset(&iter->seq);
2447 mutex_lock(&trace_types_lock);
2448 if (iter->trace->read) {
2449 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2456 while (trace_empty(iter)) {
2458 if ((filp->f_flags & O_NONBLOCK)) {
2464 * This is a make-shift waitqueue. The reason we don't use
2465 * an actual wait queue is because:
2466 * 1) we only ever have one waiter
2467 * 2) the tracing, traces all functions, we don't want
2468 * the overhead of calling wake_up and friends
2469 * (and tracing them too)
2470 * Anyway, this is really very primitive wakeup.
2472 set_current_state(TASK_INTERRUPTIBLE);
2473 iter->tr->waiter = current;
2475 mutex_unlock(&trace_types_lock);
2477 /* sleep for 100 msecs, and try again. */
2478 schedule_timeout(HZ/10);
2480 mutex_lock(&trace_types_lock);
2482 iter->tr->waiter = NULL;
2484 if (signal_pending(current)) {
2489 if (iter->trace != current_trace)
2493 * We block until we read something and tracing is disabled.
2494 * We still block if tracing is disabled, but we have never
2495 * read anything. This allows a user to cat this file, and
2496 * then enable tracing. But after we have read something,
2497 * we give an EOF when tracing is again disabled.
2499 * iter->pos will be 0 if we haven't read anything.
2501 if (!tracer_enabled && iter->pos)
2507 /* stop when tracing is finished */
2508 if (trace_empty(iter))
2511 if (cnt >= PAGE_SIZE)
2512 cnt = PAGE_SIZE - 1;
2514 /* reset all but tr, trace, and overruns */
2515 memset(&iter->seq, 0,
2516 sizeof(struct trace_iterator) -
2517 offsetof(struct trace_iterator, seq));
2520 while (find_next_entry_inc(iter) != NULL) {
2521 enum print_line_t ret;
2522 int len = iter->seq.len;
2524 ret = print_trace_line(iter);
2525 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2526 /* don't print partial lines */
2527 iter->seq.len = len;
2531 trace_consume(iter);
2533 if (iter->seq.len >= cnt)
2537 /* Now copy what we have to the user */
2538 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2539 if (iter->seq.readpos >= iter->seq.len)
2540 trace_seq_reset(&iter->seq);
2543 * If there was nothing to send to user, inspite of consuming trace
2544 * entries, go back to wait for more entries.
2550 mutex_unlock(&trace_types_lock);
2556 tracing_entries_read(struct file *filp, char __user *ubuf,
2557 size_t cnt, loff_t *ppos)
2559 struct trace_array *tr = filp->private_data;
2563 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2564 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2568 tracing_entries_write(struct file *filp, const char __user *ubuf,
2569 size_t cnt, loff_t *ppos)
2575 if (cnt >= sizeof(buf))
2578 if (copy_from_user(&buf, ubuf, cnt))
2583 ret = strict_strtoul(buf, 10, &val);
2587 /* must have at least 1 entry */
2591 mutex_lock(&trace_types_lock);
2595 /* disable all cpu buffers */
2596 for_each_tracing_cpu(cpu) {
2597 if (global_trace.data[cpu])
2598 atomic_inc(&global_trace.data[cpu]->disabled);
2599 if (max_tr.data[cpu])
2600 atomic_inc(&max_tr.data[cpu]->disabled);
2603 /* value is in KB */
2606 if (val != global_trace.entries) {
2607 ret = ring_buffer_resize(global_trace.buffer, val);
2613 ret = ring_buffer_resize(max_tr.buffer, val);
2617 r = ring_buffer_resize(global_trace.buffer,
2618 global_trace.entries);
2620 /* AARGH! We are left with different
2621 * size max buffer!!!! */
2623 tracing_disabled = 1;
2628 global_trace.entries = val;
2633 /* If check pages failed, return ENOMEM */
2634 if (tracing_disabled)
2637 for_each_tracing_cpu(cpu) {
2638 if (global_trace.data[cpu])
2639 atomic_dec(&global_trace.data[cpu]->disabled);
2640 if (max_tr.data[cpu])
2641 atomic_dec(&max_tr.data[cpu]->disabled);
2645 max_tr.entries = global_trace.entries;
2646 mutex_unlock(&trace_types_lock);
2651 static int mark_printk(const char *fmt, ...)
2655 va_start(args, fmt);
2656 ret = trace_vprintk(0, -1, fmt, args);
2662 tracing_mark_write(struct file *filp, const char __user *ubuf,
2663 size_t cnt, loff_t *fpos)
2668 if (tracing_disabled)
2671 if (cnt > TRACE_BUF_SIZE)
2672 cnt = TRACE_BUF_SIZE;
2674 buf = kmalloc(cnt + 1, GFP_KERNEL);
2678 if (copy_from_user(buf, ubuf, cnt)) {
2683 /* Cut from the first nil or newline. */
2685 end = strchr(buf, '\n');
2689 cnt = mark_printk("%s\n", buf);
2696 static struct file_operations tracing_max_lat_fops = {
2697 .open = tracing_open_generic,
2698 .read = tracing_max_lat_read,
2699 .write = tracing_max_lat_write,
2702 static struct file_operations tracing_ctrl_fops = {
2703 .open = tracing_open_generic,
2704 .read = tracing_ctrl_read,
2705 .write = tracing_ctrl_write,
2708 static struct file_operations set_tracer_fops = {
2709 .open = tracing_open_generic,
2710 .read = tracing_set_trace_read,
2711 .write = tracing_set_trace_write,
2714 static struct file_operations tracing_pipe_fops = {
2715 .open = tracing_open_pipe,
2716 .poll = tracing_poll_pipe,
2717 .read = tracing_read_pipe,
2718 .release = tracing_release_pipe,
2721 static struct file_operations tracing_entries_fops = {
2722 .open = tracing_open_generic,
2723 .read = tracing_entries_read,
2724 .write = tracing_entries_write,
2727 static struct file_operations tracing_mark_fops = {
2728 .open = tracing_open_generic,
2729 .write = tracing_mark_write,
2732 #ifdef CONFIG_DYNAMIC_FTRACE
2734 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
2740 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
2741 size_t cnt, loff_t *ppos)
2743 static char ftrace_dyn_info_buffer[1024];
2744 static DEFINE_MUTEX(dyn_info_mutex);
2745 unsigned long *p = filp->private_data;
2746 char *buf = ftrace_dyn_info_buffer;
2747 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
2750 mutex_lock(&dyn_info_mutex);
2751 r = sprintf(buf, "%ld ", *p);
2753 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
2756 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2758 mutex_unlock(&dyn_info_mutex);
2763 static struct file_operations tracing_dyn_info_fops = {
2764 .open = tracing_open_generic,
2765 .read = tracing_read_dyn_info,
2769 static struct dentry *d_tracer;
2771 struct dentry *tracing_init_dentry(void)
2778 d_tracer = debugfs_create_dir("tracing", NULL);
2780 if (!d_tracer && !once) {
2782 pr_warning("Could not create debugfs directory 'tracing'\n");
2789 #ifdef CONFIG_FTRACE_SELFTEST
2790 /* Let selftest have access to static functions in this file */
2791 #include "trace_selftest.c"
2794 static __init int tracer_init_debugfs(void)
2796 struct dentry *d_tracer;
2797 struct dentry *entry;
2799 d_tracer = tracing_init_dentry();
2801 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2802 &global_trace, &tracing_ctrl_fops);
2804 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2806 entry = debugfs_create_file("trace_options", 0644, d_tracer,
2807 NULL, &tracing_iter_fops);
2809 pr_warning("Could not create debugfs 'trace_options' entry\n");
2811 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2812 NULL, &tracing_cpumask_fops);
2814 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2816 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2817 &global_trace, &tracing_lt_fops);
2819 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2821 entry = debugfs_create_file("trace", 0444, d_tracer,
2822 &global_trace, &tracing_fops);
2824 pr_warning("Could not create debugfs 'trace' entry\n");
2826 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2827 &global_trace, &show_traces_fops);
2829 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2831 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2832 &global_trace, &set_tracer_fops);
2834 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2836 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2837 &tracing_max_latency,
2838 &tracing_max_lat_fops);
2840 pr_warning("Could not create debugfs "
2841 "'tracing_max_latency' entry\n");
2843 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2844 &tracing_thresh, &tracing_max_lat_fops);
2846 pr_warning("Could not create debugfs "
2847 "'tracing_thresh' entry\n");
2848 entry = debugfs_create_file("README", 0644, d_tracer,
2849 NULL, &tracing_readme_fops);
2851 pr_warning("Could not create debugfs 'README' entry\n");
2853 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2854 NULL, &tracing_pipe_fops);
2856 pr_warning("Could not create debugfs "
2857 "'trace_pipe' entry\n");
2859 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
2860 &global_trace, &tracing_entries_fops);
2862 pr_warning("Could not create debugfs "
2863 "'buffer_size_kb' entry\n");
2865 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2866 NULL, &tracing_mark_fops);
2868 pr_warning("Could not create debugfs "
2869 "'trace_marker' entry\n");
2871 #ifdef CONFIG_DYNAMIC_FTRACE
2872 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2873 &ftrace_update_tot_cnt,
2874 &tracing_dyn_info_fops);
2876 pr_warning("Could not create debugfs "
2877 "'dyn_ftrace_total_info' entry\n");
2879 #ifdef CONFIG_SYSPROF_TRACER
2880 init_tracer_sysprof_debugfs(d_tracer);
2885 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
2887 static DEFINE_SPINLOCK(trace_buf_lock);
2888 static char trace_buf[TRACE_BUF_SIZE];
2890 struct ring_buffer_event *event;
2891 struct trace_array *tr = &global_trace;
2892 struct trace_array_cpu *data;
2893 int cpu, len = 0, size, pc;
2894 struct print_entry *entry;
2895 unsigned long irq_flags;
2897 if (tracing_disabled || tracing_selftest_running)
2900 pc = preempt_count();
2901 preempt_disable_notrace();
2902 cpu = raw_smp_processor_id();
2903 data = tr->data[cpu];
2905 if (unlikely(atomic_read(&data->disabled)))
2908 pause_graph_tracing();
2909 spin_lock_irqsave(&trace_buf_lock, irq_flags);
2910 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
2912 len = min(len, TRACE_BUF_SIZE-1);
2915 size = sizeof(*entry) + len + 1;
2916 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
2919 entry = ring_buffer_event_data(event);
2920 tracing_generic_entry_update(&entry->ent, irq_flags, pc);
2921 entry->ent.type = TRACE_PRINT;
2923 entry->depth = depth;
2925 memcpy(&entry->buf, trace_buf, len);
2926 entry->buf[len] = 0;
2927 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
2930 spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
2931 unpause_graph_tracing();
2933 preempt_enable_notrace();
2937 EXPORT_SYMBOL_GPL(trace_vprintk);
2939 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
2944 if (!(trace_flags & TRACE_ITER_PRINTK))
2948 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
2952 EXPORT_SYMBOL_GPL(__ftrace_printk);
2954 static int trace_panic_handler(struct notifier_block *this,
2955 unsigned long event, void *unused)
2957 if (ftrace_dump_on_oops)
2962 static struct notifier_block trace_panic_notifier = {
2963 .notifier_call = trace_panic_handler,
2965 .priority = 150 /* priority: INT_MAX >= x >= 0 */
2968 static int trace_die_handler(struct notifier_block *self,
2974 if (ftrace_dump_on_oops)
2983 static struct notifier_block trace_die_notifier = {
2984 .notifier_call = trace_die_handler,
2989 * printk is set to max of 1024, we really don't need it that big.
2990 * Nothing should be printing 1000 characters anyway.
2992 #define TRACE_MAX_PRINT 1000
2995 * Define here KERN_TRACE so that we have one place to modify
2996 * it if we decide to change what log level the ftrace dump
2999 #define KERN_TRACE KERN_EMERG
3002 trace_printk_seq(struct trace_seq *s)
3004 /* Probably should print a warning here. */
3008 /* should be zero ended, but we are paranoid. */
3009 s->buffer[s->len] = 0;
3011 printk(KERN_TRACE "%s", s->buffer);
3016 void ftrace_dump(void)
3018 static DEFINE_SPINLOCK(ftrace_dump_lock);
3019 /* use static because iter can be a bit big for the stack */
3020 static struct trace_iterator iter;
3021 static int dump_ran;
3022 unsigned long flags;
3026 spin_lock_irqsave(&ftrace_dump_lock, flags);
3032 /* No turning back! */
3036 for_each_tracing_cpu(cpu) {
3037 atomic_inc(&global_trace.data[cpu]->disabled);
3040 /* don't look at user memory in panic mode */
3041 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3043 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3045 iter.tr = &global_trace;
3046 iter.trace = current_trace;
3049 * We need to stop all tracing on all CPUS to read the
3050 * the next buffer. This is a bit expensive, but is
3051 * not done often. We fill all what we can read,
3052 * and then release the locks again.
3055 while (!trace_empty(&iter)) {
3058 printk(KERN_TRACE "---------------------------------\n");
3062 /* reset all but tr, trace, and overruns */
3063 memset(&iter.seq, 0,
3064 sizeof(struct trace_iterator) -
3065 offsetof(struct trace_iterator, seq));
3066 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3069 if (find_next_entry_inc(&iter) != NULL) {
3070 print_trace_line(&iter);
3071 trace_consume(&iter);
3074 trace_printk_seq(&iter.seq);
3078 printk(KERN_TRACE " (ftrace buffer empty)\n");
3080 printk(KERN_TRACE "---------------------------------\n");
3083 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3086 __init static int tracer_alloc_buffers(void)
3088 struct trace_array_cpu *data;
3092 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3095 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3096 goto out_free_buffer_mask;
3098 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3099 cpumask_copy(tracing_cpumask, cpu_all_mask);
3101 /* TODO: make the number of buffers hot pluggable with CPUS */
3102 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3103 TRACE_BUFFER_FLAGS);
3104 if (!global_trace.buffer) {
3105 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3107 goto out_free_cpumask;
3109 global_trace.entries = ring_buffer_size(global_trace.buffer);
3112 #ifdef CONFIG_TRACER_MAX_TRACE
3113 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3114 TRACE_BUFFER_FLAGS);
3115 if (!max_tr.buffer) {
3116 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3118 ring_buffer_free(global_trace.buffer);
3119 goto out_free_cpumask;
3121 max_tr.entries = ring_buffer_size(max_tr.buffer);
3122 WARN_ON(max_tr.entries != global_trace.entries);
3125 /* Allocate the first page for all buffers */
3126 for_each_tracing_cpu(i) {
3127 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3128 max_tr.data[i] = &per_cpu(max_data, i);
3131 trace_init_cmdlines();
3133 register_tracer(&nop_trace);
3134 #ifdef CONFIG_BOOT_TRACER
3135 register_tracer(&boot_tracer);
3136 current_trace = &boot_tracer;
3137 current_trace->init(&global_trace);
3139 current_trace = &nop_trace;
3141 /* All seems OK, enable tracing */
3142 tracing_disabled = 0;
3144 atomic_notifier_chain_register(&panic_notifier_list,
3145 &trace_panic_notifier);
3147 register_die_notifier(&trace_die_notifier);
3151 free_cpumask_var(tracing_cpumask);
3152 out_free_buffer_mask:
3153 free_cpumask_var(tracing_buffer_mask);
3157 early_initcall(tracer_alloc_buffers);
3158 fs_initcall(tracer_init_debugfs);