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 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
853 entry = ring_buffer_event_data(event);
854 tracing_generic_entry_update(&entry->ent, flags, pc);
855 entry->ent.type = TRACE_STACK;
857 memset(&entry->caller, 0, sizeof(entry->caller));
859 trace.nr_entries = 0;
860 trace.max_entries = FTRACE_STACK_ENTRIES;
862 trace.entries = entry->caller;
864 save_stack_trace(&trace);
865 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
869 static void ftrace_trace_stack(struct trace_array *tr,
870 struct trace_array_cpu *data,
874 if (!(trace_flags & TRACE_ITER_STACKTRACE))
877 __ftrace_trace_stack(tr, data, flags, skip, pc);
880 void __trace_stack(struct trace_array *tr,
881 struct trace_array_cpu *data,
885 __ftrace_trace_stack(tr, data, flags, skip, pc);
888 static void ftrace_trace_userstack(struct trace_array *tr,
889 struct trace_array_cpu *data,
890 unsigned long flags, int pc)
892 #ifdef CONFIG_STACKTRACE
893 struct ring_buffer_event *event;
894 struct userstack_entry *entry;
895 struct stack_trace trace;
896 unsigned long irq_flags;
898 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
901 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
905 entry = ring_buffer_event_data(event);
906 tracing_generic_entry_update(&entry->ent, flags, pc);
907 entry->ent.type = TRACE_USER_STACK;
909 memset(&entry->caller, 0, sizeof(entry->caller));
911 trace.nr_entries = 0;
912 trace.max_entries = FTRACE_STACK_ENTRIES;
914 trace.entries = entry->caller;
916 save_stack_trace_user(&trace);
917 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
921 void __trace_userstack(struct trace_array *tr,
922 struct trace_array_cpu *data,
925 ftrace_trace_userstack(tr, data, flags, preempt_count());
929 ftrace_trace_special(void *__tr, void *__data,
930 unsigned long arg1, unsigned long arg2, unsigned long arg3,
933 struct ring_buffer_event *event;
934 struct trace_array_cpu *data = __data;
935 struct trace_array *tr = __tr;
936 struct special_entry *entry;
937 unsigned long irq_flags;
939 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
943 entry = ring_buffer_event_data(event);
944 tracing_generic_entry_update(&entry->ent, 0, pc);
945 entry->ent.type = TRACE_SPECIAL;
949 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
950 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
951 ftrace_trace_userstack(tr, data, irq_flags, pc);
957 __trace_special(void *__tr, void *__data,
958 unsigned long arg1, unsigned long arg2, unsigned long arg3)
960 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
964 tracing_sched_switch_trace(struct trace_array *tr,
965 struct trace_array_cpu *data,
966 struct task_struct *prev,
967 struct task_struct *next,
968 unsigned long flags, int pc)
970 struct ring_buffer_event *event;
971 struct ctx_switch_entry *entry;
972 unsigned long irq_flags;
974 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
978 entry = ring_buffer_event_data(event);
979 tracing_generic_entry_update(&entry->ent, flags, pc);
980 entry->ent.type = TRACE_CTX;
981 entry->prev_pid = prev->pid;
982 entry->prev_prio = prev->prio;
983 entry->prev_state = prev->state;
984 entry->next_pid = next->pid;
985 entry->next_prio = next->prio;
986 entry->next_state = next->state;
987 entry->next_cpu = task_cpu(next);
988 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
989 ftrace_trace_stack(tr, data, flags, 5, pc);
990 ftrace_trace_userstack(tr, data, flags, pc);
994 tracing_sched_wakeup_trace(struct trace_array *tr,
995 struct trace_array_cpu *data,
996 struct task_struct *wakee,
997 struct task_struct *curr,
998 unsigned long flags, int pc)
1000 struct ring_buffer_event *event;
1001 struct ctx_switch_entry *entry;
1002 unsigned long irq_flags;
1004 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1008 entry = ring_buffer_event_data(event);
1009 tracing_generic_entry_update(&entry->ent, flags, pc);
1010 entry->ent.type = TRACE_WAKE;
1011 entry->prev_pid = curr->pid;
1012 entry->prev_prio = curr->prio;
1013 entry->prev_state = curr->state;
1014 entry->next_pid = wakee->pid;
1015 entry->next_prio = wakee->prio;
1016 entry->next_state = wakee->state;
1017 entry->next_cpu = task_cpu(wakee);
1018 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1019 ftrace_trace_stack(tr, data, flags, 6, pc);
1020 ftrace_trace_userstack(tr, data, flags, pc);
1026 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1028 struct trace_array *tr = &global_trace;
1029 struct trace_array_cpu *data;
1030 unsigned long flags;
1034 if (tracing_disabled)
1037 pc = preempt_count();
1038 local_irq_save(flags);
1039 cpu = raw_smp_processor_id();
1040 data = tr->data[cpu];
1042 if (likely(atomic_inc_return(&data->disabled) == 1))
1043 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1045 atomic_dec(&data->disabled);
1046 local_irq_restore(flags);
1049 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1050 int trace_graph_entry(struct ftrace_graph_ent *trace)
1052 struct trace_array *tr = &global_trace;
1053 struct trace_array_cpu *data;
1054 unsigned long flags;
1059 if (!ftrace_trace_task(current))
1062 if (!ftrace_graph_addr(trace->func))
1065 local_irq_save(flags);
1066 cpu = raw_smp_processor_id();
1067 data = tr->data[cpu];
1068 disabled = atomic_inc_return(&data->disabled);
1069 if (likely(disabled == 1)) {
1070 pc = preempt_count();
1071 __trace_graph_entry(tr, data, trace, flags, pc);
1073 /* Only do the atomic if it is not already set */
1074 if (!test_tsk_trace_graph(current))
1075 set_tsk_trace_graph(current);
1076 atomic_dec(&data->disabled);
1077 local_irq_restore(flags);
1082 void trace_graph_return(struct ftrace_graph_ret *trace)
1084 struct trace_array *tr = &global_trace;
1085 struct trace_array_cpu *data;
1086 unsigned long flags;
1091 local_irq_save(flags);
1092 cpu = raw_smp_processor_id();
1093 data = tr->data[cpu];
1094 disabled = atomic_inc_return(&data->disabled);
1095 if (likely(disabled == 1)) {
1096 pc = preempt_count();
1097 __trace_graph_return(tr, data, trace, flags, pc);
1100 clear_tsk_trace_graph(current);
1101 atomic_dec(&data->disabled);
1102 local_irq_restore(flags);
1104 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1106 enum trace_file_type {
1107 TRACE_FILE_LAT_FMT = 1,
1108 TRACE_FILE_ANNOTATE = 2,
1111 static void trace_iterator_increment(struct trace_iterator *iter)
1113 /* Don't allow ftrace to trace into the ring buffers */
1114 ftrace_disable_cpu();
1117 if (iter->buffer_iter[iter->cpu])
1118 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1120 ftrace_enable_cpu();
1123 static struct trace_entry *
1124 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1126 struct ring_buffer_event *event;
1127 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1129 /* Don't allow ftrace to trace into the ring buffers */
1130 ftrace_disable_cpu();
1133 event = ring_buffer_iter_peek(buf_iter, ts);
1135 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1137 ftrace_enable_cpu();
1139 return event ? ring_buffer_event_data(event) : NULL;
1142 static struct trace_entry *
1143 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1145 struct ring_buffer *buffer = iter->tr->buffer;
1146 struct trace_entry *ent, *next = NULL;
1147 u64 next_ts = 0, ts;
1151 for_each_tracing_cpu(cpu) {
1153 if (ring_buffer_empty_cpu(buffer, cpu))
1156 ent = peek_next_entry(iter, cpu, &ts);
1159 * Pick the entry with the smallest timestamp:
1161 if (ent && (!next || ts < next_ts)) {
1169 *ent_cpu = next_cpu;
1177 /* Find the next real entry, without updating the iterator itself */
1178 static struct trace_entry *
1179 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1181 return __find_next_entry(iter, ent_cpu, ent_ts);
1184 /* Find the next real entry, and increment the iterator to the next entry */
1185 static void *find_next_entry_inc(struct trace_iterator *iter)
1187 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1190 trace_iterator_increment(iter);
1192 return iter->ent ? iter : NULL;
1195 static void trace_consume(struct trace_iterator *iter)
1197 /* Don't allow ftrace to trace into the ring buffers */
1198 ftrace_disable_cpu();
1199 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1200 ftrace_enable_cpu();
1203 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1205 struct trace_iterator *iter = m->private;
1211 /* can't go backwards */
1216 ent = find_next_entry_inc(iter);
1220 while (ent && iter->idx < i)
1221 ent = find_next_entry_inc(iter);
1228 static void *s_start(struct seq_file *m, loff_t *pos)
1230 struct trace_iterator *iter = m->private;
1235 mutex_lock(&trace_types_lock);
1237 if (!current_trace || current_trace != iter->trace) {
1238 mutex_unlock(&trace_types_lock);
1242 atomic_inc(&trace_record_cmdline_disabled);
1244 if (*pos != iter->pos) {
1249 ftrace_disable_cpu();
1251 for_each_tracing_cpu(cpu) {
1252 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1255 ftrace_enable_cpu();
1257 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1262 p = s_next(m, p, &l);
1268 static void s_stop(struct seq_file *m, void *p)
1270 atomic_dec(&trace_record_cmdline_disabled);
1271 mutex_unlock(&trace_types_lock);
1274 static void print_lat_help_header(struct seq_file *m)
1276 seq_puts(m, "# _------=> CPU# \n");
1277 seq_puts(m, "# / _-----=> irqs-off \n");
1278 seq_puts(m, "# | / _----=> need-resched \n");
1279 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1280 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1281 seq_puts(m, "# |||| / \n");
1282 seq_puts(m, "# ||||| delay \n");
1283 seq_puts(m, "# cmd pid ||||| time | caller \n");
1284 seq_puts(m, "# \\ / ||||| \\ | / \n");
1287 static void print_func_help_header(struct seq_file *m)
1289 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1290 seq_puts(m, "# | | | | |\n");
1295 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1297 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1298 struct trace_array *tr = iter->tr;
1299 struct trace_array_cpu *data = tr->data[tr->cpu];
1300 struct tracer *type = current_trace;
1301 unsigned long total;
1302 unsigned long entries;
1303 const char *name = "preemption";
1308 entries = ring_buffer_entries(iter->tr->buffer);
1310 ring_buffer_overruns(iter->tr->buffer);
1312 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1314 seq_puts(m, "-----------------------------------"
1315 "---------------------------------\n");
1316 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1317 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1318 nsecs_to_usecs(data->saved_latency),
1322 #if defined(CONFIG_PREEMPT_NONE)
1324 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1326 #elif defined(CONFIG_PREEMPT)
1331 /* These are reserved for later use */
1334 seq_printf(m, " #P:%d)\n", num_online_cpus());
1338 seq_puts(m, " -----------------\n");
1339 seq_printf(m, " | task: %.16s-%d "
1340 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1341 data->comm, data->pid, data->uid, data->nice,
1342 data->policy, data->rt_priority);
1343 seq_puts(m, " -----------------\n");
1345 if (data->critical_start) {
1346 seq_puts(m, " => started at: ");
1347 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1348 trace_print_seq(m, &iter->seq);
1349 seq_puts(m, "\n => ended at: ");
1350 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1351 trace_print_seq(m, &iter->seq);
1359 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1361 int hardirq, softirq;
1364 comm = trace_find_cmdline(entry->pid);
1366 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1367 trace_seq_printf(s, "%3d", cpu);
1368 trace_seq_printf(s, "%c%c",
1369 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1370 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1371 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1373 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1374 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1375 if (hardirq && softirq) {
1376 trace_seq_putc(s, 'H');
1379 trace_seq_putc(s, 'h');
1382 trace_seq_putc(s, 's');
1384 trace_seq_putc(s, '.');
1388 if (entry->preempt_count)
1389 trace_seq_printf(s, "%x", entry->preempt_count);
1391 trace_seq_puts(s, ".");
1394 unsigned long preempt_mark_thresh = 100;
1397 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1398 unsigned long rel_usecs)
1400 trace_seq_printf(s, " %4lldus", abs_usecs);
1401 if (rel_usecs > preempt_mark_thresh)
1402 trace_seq_puts(s, "!: ");
1403 else if (rel_usecs > 1)
1404 trace_seq_puts(s, "+: ");
1406 trace_seq_puts(s, " : ");
1409 static void test_cpu_buff_start(struct trace_iterator *iter)
1411 struct trace_seq *s = &iter->seq;
1413 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1416 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1419 if (cpumask_test_cpu(iter->cpu, iter->started))
1422 cpumask_set_cpu(iter->cpu, iter->started);
1423 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1426 static enum print_line_t
1427 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1429 struct trace_seq *s = &iter->seq;
1430 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1431 struct trace_entry *next_entry;
1432 struct trace_event *event;
1433 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1434 struct trace_entry *entry = iter->ent;
1435 unsigned long abs_usecs;
1436 unsigned long rel_usecs;
1441 test_cpu_buff_start(iter);
1443 next_entry = find_next_entry(iter, NULL, &next_ts);
1446 rel_usecs = ns2usecs(next_ts - iter->ts);
1447 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1450 comm = trace_find_cmdline(entry->pid);
1451 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1452 " %ld.%03ldms (+%ld.%03ldms): ",
1454 entry->pid, cpu, entry->flags,
1455 entry->preempt_count, trace_idx,
1458 abs_usecs % 1000, rel_usecs/1000,
1461 lat_print_generic(s, entry, cpu);
1462 lat_print_timestamp(s, abs_usecs, rel_usecs);
1465 event = ftrace_find_event(entry->type);
1466 if (event && event->latency_trace) {
1467 ret = event->latency_trace(s, entry, sym_flags);
1470 return TRACE_TYPE_HANDLED;
1473 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1474 return TRACE_TYPE_HANDLED;
1477 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1479 struct trace_seq *s = &iter->seq;
1480 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1481 struct trace_entry *entry;
1482 struct trace_event *event;
1483 unsigned long usec_rem;
1484 unsigned long long t;
1491 test_cpu_buff_start(iter);
1493 comm = trace_find_cmdline(iter->ent->pid);
1495 t = ns2usecs(iter->ts);
1496 usec_rem = do_div(t, 1000000ULL);
1497 secs = (unsigned long)t;
1499 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1501 return TRACE_TYPE_PARTIAL_LINE;
1502 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1504 return TRACE_TYPE_PARTIAL_LINE;
1505 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1507 return TRACE_TYPE_PARTIAL_LINE;
1509 event = ftrace_find_event(entry->type);
1510 if (event && event->trace) {
1511 ret = event->trace(s, entry, sym_flags);
1514 return TRACE_TYPE_HANDLED;
1516 ret = trace_seq_printf(s, "Unknown type %d\n", entry->type);
1518 return TRACE_TYPE_PARTIAL_LINE;
1520 return TRACE_TYPE_HANDLED;
1523 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1525 struct trace_seq *s = &iter->seq;
1526 struct trace_entry *entry;
1527 struct trace_event *event;
1532 ret = trace_seq_printf(s, "%d %d %llu ",
1533 entry->pid, iter->cpu, iter->ts);
1535 return TRACE_TYPE_PARTIAL_LINE;
1537 event = ftrace_find_event(entry->type);
1538 if (event && event->raw) {
1539 ret = event->raw(s, entry, 0);
1542 return TRACE_TYPE_HANDLED;
1544 ret = trace_seq_printf(s, "%d ?\n", entry->type);
1546 return TRACE_TYPE_PARTIAL_LINE;
1548 return TRACE_TYPE_HANDLED;
1551 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1553 struct trace_seq *s = &iter->seq;
1554 unsigned char newline = '\n';
1555 struct trace_entry *entry;
1556 struct trace_event *event;
1560 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1561 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1562 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1564 event = ftrace_find_event(entry->type);
1565 if (event && event->hex)
1566 event->hex(s, entry, 0);
1568 SEQ_PUT_FIELD_RET(s, newline);
1570 return TRACE_TYPE_HANDLED;
1573 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1575 struct trace_seq *s = &iter->seq;
1576 struct trace_entry *entry = iter->ent;
1577 struct print_entry *field;
1580 trace_assign_type(field, entry);
1582 ret = trace_seq_printf(s, field->buf);
1584 return TRACE_TYPE_PARTIAL_LINE;
1586 return TRACE_TYPE_HANDLED;
1589 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1591 struct trace_seq *s = &iter->seq;
1592 struct trace_entry *entry;
1593 struct trace_event *event;
1597 SEQ_PUT_FIELD_RET(s, entry->pid);
1598 SEQ_PUT_FIELD_RET(s, entry->cpu);
1599 SEQ_PUT_FIELD_RET(s, iter->ts);
1601 event = ftrace_find_event(entry->type);
1602 if (event && event->binary)
1603 event->binary(s, entry, 0);
1605 return TRACE_TYPE_HANDLED;
1608 static int trace_empty(struct trace_iterator *iter)
1612 for_each_tracing_cpu(cpu) {
1613 if (iter->buffer_iter[cpu]) {
1614 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1617 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1625 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1627 enum print_line_t ret;
1629 if (iter->trace && iter->trace->print_line) {
1630 ret = iter->trace->print_line(iter);
1631 if (ret != TRACE_TYPE_UNHANDLED)
1635 if (iter->ent->type == TRACE_PRINT &&
1636 trace_flags & TRACE_ITER_PRINTK &&
1637 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1638 return print_printk_msg_only(iter);
1640 if (trace_flags & TRACE_ITER_BIN)
1641 return print_bin_fmt(iter);
1643 if (trace_flags & TRACE_ITER_HEX)
1644 return print_hex_fmt(iter);
1646 if (trace_flags & TRACE_ITER_RAW)
1647 return print_raw_fmt(iter);
1649 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1650 return print_lat_fmt(iter, iter->idx, iter->cpu);
1652 return print_trace_fmt(iter);
1655 static int s_show(struct seq_file *m, void *v)
1657 struct trace_iterator *iter = v;
1659 if (iter->ent == NULL) {
1661 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1664 if (iter->trace && iter->trace->print_header)
1665 iter->trace->print_header(m);
1666 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1667 /* print nothing if the buffers are empty */
1668 if (trace_empty(iter))
1670 print_trace_header(m, iter);
1671 if (!(trace_flags & TRACE_ITER_VERBOSE))
1672 print_lat_help_header(m);
1674 if (!(trace_flags & TRACE_ITER_VERBOSE))
1675 print_func_help_header(m);
1678 print_trace_line(iter);
1679 trace_print_seq(m, &iter->seq);
1685 static struct seq_operations tracer_seq_ops = {
1692 static struct trace_iterator *
1693 __tracing_open(struct inode *inode, struct file *file, int *ret)
1695 struct trace_iterator *iter;
1699 if (tracing_disabled) {
1704 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1710 mutex_lock(&trace_types_lock);
1711 if (current_trace && current_trace->print_max)
1714 iter->tr = inode->i_private;
1715 iter->trace = current_trace;
1718 /* Notify the tracer early; before we stop tracing. */
1719 if (iter->trace && iter->trace->open)
1720 iter->trace->open(iter);
1722 /* Annotate start of buffers if we had overruns */
1723 if (ring_buffer_overruns(iter->tr->buffer))
1724 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1727 for_each_tracing_cpu(cpu) {
1729 iter->buffer_iter[cpu] =
1730 ring_buffer_read_start(iter->tr->buffer, cpu);
1732 if (!iter->buffer_iter[cpu])
1736 /* TODO stop tracer */
1737 *ret = seq_open(file, &tracer_seq_ops);
1741 m = file->private_data;
1744 /* stop the trace while dumping */
1747 mutex_unlock(&trace_types_lock);
1753 for_each_tracing_cpu(cpu) {
1754 if (iter->buffer_iter[cpu])
1755 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1757 mutex_unlock(&trace_types_lock);
1760 return ERR_PTR(-ENOMEM);
1763 int tracing_open_generic(struct inode *inode, struct file *filp)
1765 if (tracing_disabled)
1768 filp->private_data = inode->i_private;
1772 int tracing_release(struct inode *inode, struct file *file)
1774 struct seq_file *m = (struct seq_file *)file->private_data;
1775 struct trace_iterator *iter = m->private;
1778 mutex_lock(&trace_types_lock);
1779 for_each_tracing_cpu(cpu) {
1780 if (iter->buffer_iter[cpu])
1781 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1784 if (iter->trace && iter->trace->close)
1785 iter->trace->close(iter);
1787 /* reenable tracing if it was previously enabled */
1789 mutex_unlock(&trace_types_lock);
1791 seq_release(inode, file);
1796 static int tracing_open(struct inode *inode, struct file *file)
1800 __tracing_open(inode, file, &ret);
1805 static int tracing_lt_open(struct inode *inode, struct file *file)
1807 struct trace_iterator *iter;
1810 iter = __tracing_open(inode, file, &ret);
1813 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1820 t_next(struct seq_file *m, void *v, loff_t *pos)
1822 struct tracer *t = m->private;
1834 static void *t_start(struct seq_file *m, loff_t *pos)
1836 struct tracer *t = m->private;
1839 mutex_lock(&trace_types_lock);
1840 for (; t && l < *pos; t = t_next(m, t, &l))
1846 static void t_stop(struct seq_file *m, void *p)
1848 mutex_unlock(&trace_types_lock);
1851 static int t_show(struct seq_file *m, void *v)
1853 struct tracer *t = v;
1858 seq_printf(m, "%s", t->name);
1867 static struct seq_operations show_traces_seq_ops = {
1874 static int show_traces_open(struct inode *inode, struct file *file)
1878 if (tracing_disabled)
1881 ret = seq_open(file, &show_traces_seq_ops);
1883 struct seq_file *m = file->private_data;
1884 m->private = trace_types;
1890 static struct file_operations tracing_fops = {
1891 .open = tracing_open,
1893 .llseek = seq_lseek,
1894 .release = tracing_release,
1897 static struct file_operations tracing_lt_fops = {
1898 .open = tracing_lt_open,
1900 .llseek = seq_lseek,
1901 .release = tracing_release,
1904 static struct file_operations show_traces_fops = {
1905 .open = show_traces_open,
1907 .release = seq_release,
1911 * Only trace on a CPU if the bitmask is set:
1913 static cpumask_var_t tracing_cpumask;
1916 * The tracer itself will not take this lock, but still we want
1917 * to provide a consistent cpumask to user-space:
1919 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1922 * Temporary storage for the character representation of the
1923 * CPU bitmask (and one more byte for the newline):
1925 static char mask_str[NR_CPUS + 1];
1928 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1929 size_t count, loff_t *ppos)
1933 mutex_lock(&tracing_cpumask_update_lock);
1935 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1936 if (count - len < 2) {
1940 len += sprintf(mask_str + len, "\n");
1941 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1944 mutex_unlock(&tracing_cpumask_update_lock);
1950 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1951 size_t count, loff_t *ppos)
1954 cpumask_var_t tracing_cpumask_new;
1956 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1959 mutex_lock(&tracing_cpumask_update_lock);
1960 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1964 local_irq_disable();
1965 __raw_spin_lock(&ftrace_max_lock);
1966 for_each_tracing_cpu(cpu) {
1968 * Increase/decrease the disabled counter if we are
1969 * about to flip a bit in the cpumask:
1971 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
1972 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
1973 atomic_inc(&global_trace.data[cpu]->disabled);
1975 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
1976 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
1977 atomic_dec(&global_trace.data[cpu]->disabled);
1980 __raw_spin_unlock(&ftrace_max_lock);
1983 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
1985 mutex_unlock(&tracing_cpumask_update_lock);
1986 free_cpumask_var(tracing_cpumask_new);
1991 mutex_unlock(&tracing_cpumask_update_lock);
1992 free_cpumask_var(tracing_cpumask);
1997 static struct file_operations tracing_cpumask_fops = {
1998 .open = tracing_open_generic,
1999 .read = tracing_cpumask_read,
2000 .write = tracing_cpumask_write,
2004 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2005 size_t cnt, loff_t *ppos)
2011 u32 tracer_flags = current_trace->flags->val;
2012 struct tracer_opt *trace_opts = current_trace->flags->opts;
2015 /* calulate max size */
2016 for (i = 0; trace_options[i]; i++) {
2017 len += strlen(trace_options[i]);
2018 len += 3; /* "no" and space */
2022 * Increase the size with names of options specific
2023 * of the current tracer.
2025 for (i = 0; trace_opts[i].name; i++) {
2026 len += strlen(trace_opts[i].name);
2027 len += 3; /* "no" and space */
2030 /* +2 for \n and \0 */
2031 buf = kmalloc(len + 2, GFP_KERNEL);
2035 for (i = 0; trace_options[i]; i++) {
2036 if (trace_flags & (1 << i))
2037 r += sprintf(buf + r, "%s ", trace_options[i]);
2039 r += sprintf(buf + r, "no%s ", trace_options[i]);
2042 for (i = 0; trace_opts[i].name; i++) {
2043 if (tracer_flags & trace_opts[i].bit)
2044 r += sprintf(buf + r, "%s ",
2045 trace_opts[i].name);
2047 r += sprintf(buf + r, "no%s ",
2048 trace_opts[i].name);
2051 r += sprintf(buf + r, "\n");
2052 WARN_ON(r >= len + 2);
2054 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2061 /* Try to assign a tracer specific option */
2062 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2064 struct tracer_flags *trace_flags = trace->flags;
2065 struct tracer_opt *opts = NULL;
2069 for (i = 0; trace_flags->opts[i].name; i++) {
2070 opts = &trace_flags->opts[i];
2071 len = strlen(opts->name);
2073 if (strncmp(cmp, opts->name, len) == 0) {
2074 ret = trace->set_flag(trace_flags->val,
2080 if (!trace_flags->opts[i].name)
2083 /* Refused to handle */
2088 trace_flags->val &= ~opts->bit;
2090 trace_flags->val |= opts->bit;
2096 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2097 size_t cnt, loff_t *ppos)
2105 if (cnt >= sizeof(buf))
2108 if (copy_from_user(&buf, ubuf, cnt))
2113 if (strncmp(buf, "no", 2) == 0) {
2118 for (i = 0; trace_options[i]; i++) {
2119 int len = strlen(trace_options[i]);
2121 if (strncmp(cmp, trace_options[i], len) == 0) {
2123 trace_flags &= ~(1 << i);
2125 trace_flags |= (1 << i);
2130 /* If no option could be set, test the specific tracer options */
2131 if (!trace_options[i]) {
2132 ret = set_tracer_option(current_trace, cmp, neg);
2142 static struct file_operations tracing_iter_fops = {
2143 .open = tracing_open_generic,
2144 .read = tracing_trace_options_read,
2145 .write = tracing_trace_options_write,
2148 static const char readme_msg[] =
2149 "tracing mini-HOWTO:\n\n"
2151 "# mount -t debugfs nodev /debug\n\n"
2152 "# cat /debug/tracing/available_tracers\n"
2153 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2154 "# cat /debug/tracing/current_tracer\n"
2156 "# echo sched_switch > /debug/tracing/current_tracer\n"
2157 "# cat /debug/tracing/current_tracer\n"
2159 "# cat /debug/tracing/trace_options\n"
2160 "noprint-parent nosym-offset nosym-addr noverbose\n"
2161 "# echo print-parent > /debug/tracing/trace_options\n"
2162 "# echo 1 > /debug/tracing/tracing_enabled\n"
2163 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2164 "echo 0 > /debug/tracing/tracing_enabled\n"
2168 tracing_readme_read(struct file *filp, char __user *ubuf,
2169 size_t cnt, loff_t *ppos)
2171 return simple_read_from_buffer(ubuf, cnt, ppos,
2172 readme_msg, strlen(readme_msg));
2175 static struct file_operations tracing_readme_fops = {
2176 .open = tracing_open_generic,
2177 .read = tracing_readme_read,
2181 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2182 size_t cnt, loff_t *ppos)
2187 r = sprintf(buf, "%u\n", tracer_enabled);
2188 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2192 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2193 size_t cnt, loff_t *ppos)
2195 struct trace_array *tr = filp->private_data;
2200 if (cnt >= sizeof(buf))
2203 if (copy_from_user(&buf, ubuf, cnt))
2208 ret = strict_strtoul(buf, 10, &val);
2214 mutex_lock(&trace_types_lock);
2215 if (tracer_enabled ^ val) {
2218 if (current_trace->start)
2219 current_trace->start(tr);
2224 if (current_trace->stop)
2225 current_trace->stop(tr);
2228 mutex_unlock(&trace_types_lock);
2236 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2237 size_t cnt, loff_t *ppos)
2239 char buf[max_tracer_type_len+2];
2242 mutex_lock(&trace_types_lock);
2244 r = sprintf(buf, "%s\n", current_trace->name);
2246 r = sprintf(buf, "\n");
2247 mutex_unlock(&trace_types_lock);
2249 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2252 static int tracing_set_tracer(char *buf)
2254 struct trace_array *tr = &global_trace;
2258 mutex_lock(&trace_types_lock);
2259 for (t = trace_types; t; t = t->next) {
2260 if (strcmp(t->name, buf) == 0)
2267 if (t == current_trace)
2270 trace_branch_disable();
2271 if (current_trace && current_trace->reset)
2272 current_trace->reset(tr);
2281 trace_branch_enable(tr);
2283 mutex_unlock(&trace_types_lock);
2289 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2290 size_t cnt, loff_t *ppos)
2292 char buf[max_tracer_type_len+1];
2299 if (cnt > max_tracer_type_len)
2300 cnt = max_tracer_type_len;
2302 if (copy_from_user(&buf, ubuf, cnt))
2307 /* strip ending whitespace. */
2308 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2311 err = tracing_set_tracer(buf);
2321 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2322 size_t cnt, loff_t *ppos)
2324 unsigned long *ptr = filp->private_data;
2328 r = snprintf(buf, sizeof(buf), "%ld\n",
2329 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2330 if (r > sizeof(buf))
2332 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2336 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2337 size_t cnt, loff_t *ppos)
2339 long *ptr = filp->private_data;
2344 if (cnt >= sizeof(buf))
2347 if (copy_from_user(&buf, ubuf, cnt))
2352 ret = strict_strtoul(buf, 10, &val);
2361 static atomic_t tracing_reader;
2363 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2365 struct trace_iterator *iter;
2367 if (tracing_disabled)
2370 /* We only allow for reader of the pipe */
2371 if (atomic_inc_return(&tracing_reader) != 1) {
2372 atomic_dec(&tracing_reader);
2376 /* create a buffer to store the information to pass to userspace */
2377 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2381 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2386 mutex_lock(&trace_types_lock);
2388 /* trace pipe does not show start of buffer */
2389 cpumask_setall(iter->started);
2391 iter->tr = &global_trace;
2392 iter->trace = current_trace;
2393 filp->private_data = iter;
2395 if (iter->trace->pipe_open)
2396 iter->trace->pipe_open(iter);
2397 mutex_unlock(&trace_types_lock);
2402 static int tracing_release_pipe(struct inode *inode, struct file *file)
2404 struct trace_iterator *iter = file->private_data;
2406 free_cpumask_var(iter->started);
2408 atomic_dec(&tracing_reader);
2414 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2416 struct trace_iterator *iter = filp->private_data;
2418 if (trace_flags & TRACE_ITER_BLOCK) {
2420 * Always select as readable when in blocking mode
2422 return POLLIN | POLLRDNORM;
2424 if (!trace_empty(iter))
2425 return POLLIN | POLLRDNORM;
2426 poll_wait(filp, &trace_wait, poll_table);
2427 if (!trace_empty(iter))
2428 return POLLIN | POLLRDNORM;
2438 tracing_read_pipe(struct file *filp, char __user *ubuf,
2439 size_t cnt, loff_t *ppos)
2441 struct trace_iterator *iter = filp->private_data;
2444 /* return any leftover data */
2445 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2449 trace_seq_reset(&iter->seq);
2451 mutex_lock(&trace_types_lock);
2452 if (iter->trace->read) {
2453 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2460 while (trace_empty(iter)) {
2462 if ((filp->f_flags & O_NONBLOCK)) {
2468 * This is a make-shift waitqueue. The reason we don't use
2469 * an actual wait queue is because:
2470 * 1) we only ever have one waiter
2471 * 2) the tracing, traces all functions, we don't want
2472 * the overhead of calling wake_up and friends
2473 * (and tracing them too)
2474 * Anyway, this is really very primitive wakeup.
2476 set_current_state(TASK_INTERRUPTIBLE);
2477 iter->tr->waiter = current;
2479 mutex_unlock(&trace_types_lock);
2481 /* sleep for 100 msecs, and try again. */
2482 schedule_timeout(HZ/10);
2484 mutex_lock(&trace_types_lock);
2486 iter->tr->waiter = NULL;
2488 if (signal_pending(current)) {
2493 if (iter->trace != current_trace)
2497 * We block until we read something and tracing is disabled.
2498 * We still block if tracing is disabled, but we have never
2499 * read anything. This allows a user to cat this file, and
2500 * then enable tracing. But after we have read something,
2501 * we give an EOF when tracing is again disabled.
2503 * iter->pos will be 0 if we haven't read anything.
2505 if (!tracer_enabled && iter->pos)
2511 /* stop when tracing is finished */
2512 if (trace_empty(iter))
2515 if (cnt >= PAGE_SIZE)
2516 cnt = PAGE_SIZE - 1;
2518 /* reset all but tr, trace, and overruns */
2519 memset(&iter->seq, 0,
2520 sizeof(struct trace_iterator) -
2521 offsetof(struct trace_iterator, seq));
2524 while (find_next_entry_inc(iter) != NULL) {
2525 enum print_line_t ret;
2526 int len = iter->seq.len;
2528 ret = print_trace_line(iter);
2529 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2530 /* don't print partial lines */
2531 iter->seq.len = len;
2535 trace_consume(iter);
2537 if (iter->seq.len >= cnt)
2541 /* Now copy what we have to the user */
2542 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2543 if (iter->seq.readpos >= iter->seq.len)
2544 trace_seq_reset(&iter->seq);
2547 * If there was nothing to send to user, inspite of consuming trace
2548 * entries, go back to wait for more entries.
2554 mutex_unlock(&trace_types_lock);
2560 tracing_entries_read(struct file *filp, char __user *ubuf,
2561 size_t cnt, loff_t *ppos)
2563 struct trace_array *tr = filp->private_data;
2567 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2568 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2572 tracing_entries_write(struct file *filp, const char __user *ubuf,
2573 size_t cnt, loff_t *ppos)
2579 if (cnt >= sizeof(buf))
2582 if (copy_from_user(&buf, ubuf, cnt))
2587 ret = strict_strtoul(buf, 10, &val);
2591 /* must have at least 1 entry */
2595 mutex_lock(&trace_types_lock);
2599 /* disable all cpu buffers */
2600 for_each_tracing_cpu(cpu) {
2601 if (global_trace.data[cpu])
2602 atomic_inc(&global_trace.data[cpu]->disabled);
2603 if (max_tr.data[cpu])
2604 atomic_inc(&max_tr.data[cpu]->disabled);
2607 /* value is in KB */
2610 if (val != global_trace.entries) {
2611 ret = ring_buffer_resize(global_trace.buffer, val);
2617 ret = ring_buffer_resize(max_tr.buffer, val);
2621 r = ring_buffer_resize(global_trace.buffer,
2622 global_trace.entries);
2624 /* AARGH! We are left with different
2625 * size max buffer!!!! */
2627 tracing_disabled = 1;
2632 global_trace.entries = val;
2637 /* If check pages failed, return ENOMEM */
2638 if (tracing_disabled)
2641 for_each_tracing_cpu(cpu) {
2642 if (global_trace.data[cpu])
2643 atomic_dec(&global_trace.data[cpu]->disabled);
2644 if (max_tr.data[cpu])
2645 atomic_dec(&max_tr.data[cpu]->disabled);
2649 max_tr.entries = global_trace.entries;
2650 mutex_unlock(&trace_types_lock);
2655 static int mark_printk(const char *fmt, ...)
2659 va_start(args, fmt);
2660 ret = trace_vprintk(0, -1, fmt, args);
2666 tracing_mark_write(struct file *filp, const char __user *ubuf,
2667 size_t cnt, loff_t *fpos)
2672 if (tracing_disabled)
2675 if (cnt > TRACE_BUF_SIZE)
2676 cnt = TRACE_BUF_SIZE;
2678 buf = kmalloc(cnt + 1, GFP_KERNEL);
2682 if (copy_from_user(buf, ubuf, cnt)) {
2687 /* Cut from the first nil or newline. */
2689 end = strchr(buf, '\n');
2693 cnt = mark_printk("%s\n", buf);
2700 static struct file_operations tracing_max_lat_fops = {
2701 .open = tracing_open_generic,
2702 .read = tracing_max_lat_read,
2703 .write = tracing_max_lat_write,
2706 static struct file_operations tracing_ctrl_fops = {
2707 .open = tracing_open_generic,
2708 .read = tracing_ctrl_read,
2709 .write = tracing_ctrl_write,
2712 static struct file_operations set_tracer_fops = {
2713 .open = tracing_open_generic,
2714 .read = tracing_set_trace_read,
2715 .write = tracing_set_trace_write,
2718 static struct file_operations tracing_pipe_fops = {
2719 .open = tracing_open_pipe,
2720 .poll = tracing_poll_pipe,
2721 .read = tracing_read_pipe,
2722 .release = tracing_release_pipe,
2725 static struct file_operations tracing_entries_fops = {
2726 .open = tracing_open_generic,
2727 .read = tracing_entries_read,
2728 .write = tracing_entries_write,
2731 static struct file_operations tracing_mark_fops = {
2732 .open = tracing_open_generic,
2733 .write = tracing_mark_write,
2736 #ifdef CONFIG_DYNAMIC_FTRACE
2738 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
2744 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
2745 size_t cnt, loff_t *ppos)
2747 static char ftrace_dyn_info_buffer[1024];
2748 static DEFINE_MUTEX(dyn_info_mutex);
2749 unsigned long *p = filp->private_data;
2750 char *buf = ftrace_dyn_info_buffer;
2751 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
2754 mutex_lock(&dyn_info_mutex);
2755 r = sprintf(buf, "%ld ", *p);
2757 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
2760 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2762 mutex_unlock(&dyn_info_mutex);
2767 static struct file_operations tracing_dyn_info_fops = {
2768 .open = tracing_open_generic,
2769 .read = tracing_read_dyn_info,
2773 static struct dentry *d_tracer;
2775 struct dentry *tracing_init_dentry(void)
2782 d_tracer = debugfs_create_dir("tracing", NULL);
2784 if (!d_tracer && !once) {
2786 pr_warning("Could not create debugfs directory 'tracing'\n");
2793 #ifdef CONFIG_FTRACE_SELFTEST
2794 /* Let selftest have access to static functions in this file */
2795 #include "trace_selftest.c"
2798 static __init int tracer_init_debugfs(void)
2800 struct dentry *d_tracer;
2801 struct dentry *entry;
2803 d_tracer = tracing_init_dentry();
2805 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2806 &global_trace, &tracing_ctrl_fops);
2808 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2810 entry = debugfs_create_file("trace_options", 0644, d_tracer,
2811 NULL, &tracing_iter_fops);
2813 pr_warning("Could not create debugfs 'trace_options' entry\n");
2815 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2816 NULL, &tracing_cpumask_fops);
2818 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2820 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2821 &global_trace, &tracing_lt_fops);
2823 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2825 entry = debugfs_create_file("trace", 0444, d_tracer,
2826 &global_trace, &tracing_fops);
2828 pr_warning("Could not create debugfs 'trace' entry\n");
2830 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2831 &global_trace, &show_traces_fops);
2833 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2835 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2836 &global_trace, &set_tracer_fops);
2838 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2840 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2841 &tracing_max_latency,
2842 &tracing_max_lat_fops);
2844 pr_warning("Could not create debugfs "
2845 "'tracing_max_latency' entry\n");
2847 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2848 &tracing_thresh, &tracing_max_lat_fops);
2850 pr_warning("Could not create debugfs "
2851 "'tracing_thresh' entry\n");
2852 entry = debugfs_create_file("README", 0644, d_tracer,
2853 NULL, &tracing_readme_fops);
2855 pr_warning("Could not create debugfs 'README' entry\n");
2857 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2858 NULL, &tracing_pipe_fops);
2860 pr_warning("Could not create debugfs "
2861 "'trace_pipe' entry\n");
2863 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
2864 &global_trace, &tracing_entries_fops);
2866 pr_warning("Could not create debugfs "
2867 "'buffer_size_kb' entry\n");
2869 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2870 NULL, &tracing_mark_fops);
2872 pr_warning("Could not create debugfs "
2873 "'trace_marker' entry\n");
2875 #ifdef CONFIG_DYNAMIC_FTRACE
2876 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2877 &ftrace_update_tot_cnt,
2878 &tracing_dyn_info_fops);
2880 pr_warning("Could not create debugfs "
2881 "'dyn_ftrace_total_info' entry\n");
2883 #ifdef CONFIG_SYSPROF_TRACER
2884 init_tracer_sysprof_debugfs(d_tracer);
2889 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
2891 static DEFINE_SPINLOCK(trace_buf_lock);
2892 static char trace_buf[TRACE_BUF_SIZE];
2894 struct ring_buffer_event *event;
2895 struct trace_array *tr = &global_trace;
2896 struct trace_array_cpu *data;
2897 int cpu, len = 0, size, pc;
2898 struct print_entry *entry;
2899 unsigned long irq_flags;
2901 if (tracing_disabled || tracing_selftest_running)
2904 pc = preempt_count();
2905 preempt_disable_notrace();
2906 cpu = raw_smp_processor_id();
2907 data = tr->data[cpu];
2909 if (unlikely(atomic_read(&data->disabled)))
2912 pause_graph_tracing();
2913 spin_lock_irqsave(&trace_buf_lock, irq_flags);
2914 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
2916 len = min(len, TRACE_BUF_SIZE-1);
2919 size = sizeof(*entry) + len + 1;
2920 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
2923 entry = ring_buffer_event_data(event);
2924 tracing_generic_entry_update(&entry->ent, irq_flags, pc);
2925 entry->ent.type = TRACE_PRINT;
2927 entry->depth = depth;
2929 memcpy(&entry->buf, trace_buf, len);
2930 entry->buf[len] = 0;
2931 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
2934 spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
2935 unpause_graph_tracing();
2937 preempt_enable_notrace();
2941 EXPORT_SYMBOL_GPL(trace_vprintk);
2943 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
2948 if (!(trace_flags & TRACE_ITER_PRINTK))
2952 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
2956 EXPORT_SYMBOL_GPL(__ftrace_printk);
2958 static int trace_panic_handler(struct notifier_block *this,
2959 unsigned long event, void *unused)
2961 if (ftrace_dump_on_oops)
2966 static struct notifier_block trace_panic_notifier = {
2967 .notifier_call = trace_panic_handler,
2969 .priority = 150 /* priority: INT_MAX >= x >= 0 */
2972 static int trace_die_handler(struct notifier_block *self,
2978 if (ftrace_dump_on_oops)
2987 static struct notifier_block trace_die_notifier = {
2988 .notifier_call = trace_die_handler,
2993 * printk is set to max of 1024, we really don't need it that big.
2994 * Nothing should be printing 1000 characters anyway.
2996 #define TRACE_MAX_PRINT 1000
2999 * Define here KERN_TRACE so that we have one place to modify
3000 * it if we decide to change what log level the ftrace dump
3003 #define KERN_TRACE KERN_EMERG
3006 trace_printk_seq(struct trace_seq *s)
3008 /* Probably should print a warning here. */
3012 /* should be zero ended, but we are paranoid. */
3013 s->buffer[s->len] = 0;
3015 printk(KERN_TRACE "%s", s->buffer);
3020 void ftrace_dump(void)
3022 static DEFINE_SPINLOCK(ftrace_dump_lock);
3023 /* use static because iter can be a bit big for the stack */
3024 static struct trace_iterator iter;
3025 static int dump_ran;
3026 unsigned long flags;
3030 spin_lock_irqsave(&ftrace_dump_lock, flags);
3036 /* No turning back! */
3040 for_each_tracing_cpu(cpu) {
3041 atomic_inc(&global_trace.data[cpu]->disabled);
3044 /* don't look at user memory in panic mode */
3045 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3047 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3049 iter.tr = &global_trace;
3050 iter.trace = current_trace;
3053 * We need to stop all tracing on all CPUS to read the
3054 * the next buffer. This is a bit expensive, but is
3055 * not done often. We fill all what we can read,
3056 * and then release the locks again.
3059 while (!trace_empty(&iter)) {
3062 printk(KERN_TRACE "---------------------------------\n");
3066 /* reset all but tr, trace, and overruns */
3067 memset(&iter.seq, 0,
3068 sizeof(struct trace_iterator) -
3069 offsetof(struct trace_iterator, seq));
3070 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3073 if (find_next_entry_inc(&iter) != NULL) {
3074 print_trace_line(&iter);
3075 trace_consume(&iter);
3078 trace_printk_seq(&iter.seq);
3082 printk(KERN_TRACE " (ftrace buffer empty)\n");
3084 printk(KERN_TRACE "---------------------------------\n");
3087 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3090 __init static int tracer_alloc_buffers(void)
3092 struct trace_array_cpu *data;
3096 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3099 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3100 goto out_free_buffer_mask;
3102 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3103 cpumask_copy(tracing_cpumask, cpu_all_mask);
3105 /* TODO: make the number of buffers hot pluggable with CPUS */
3106 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3107 TRACE_BUFFER_FLAGS);
3108 if (!global_trace.buffer) {
3109 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3111 goto out_free_cpumask;
3113 global_trace.entries = ring_buffer_size(global_trace.buffer);
3116 #ifdef CONFIG_TRACER_MAX_TRACE
3117 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3118 TRACE_BUFFER_FLAGS);
3119 if (!max_tr.buffer) {
3120 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3122 ring_buffer_free(global_trace.buffer);
3123 goto out_free_cpumask;
3125 max_tr.entries = ring_buffer_size(max_tr.buffer);
3126 WARN_ON(max_tr.entries != global_trace.entries);
3129 /* Allocate the first page for all buffers */
3130 for_each_tracing_cpu(i) {
3131 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3132 max_tr.data[i] = &per_cpu(max_data, i);
3135 trace_init_cmdlines();
3137 register_tracer(&nop_trace);
3138 #ifdef CONFIG_BOOT_TRACER
3139 register_tracer(&boot_tracer);
3140 current_trace = &boot_tracer;
3141 current_trace->init(&global_trace);
3143 current_trace = &nop_trace;
3145 /* All seems OK, enable tracing */
3146 tracing_disabled = 0;
3148 atomic_notifier_chain_register(&panic_notifier_list,
3149 &trace_panic_notifier);
3151 register_die_notifier(&trace_die_notifier);
3155 free_cpumask_var(tracing_cpumask);
3156 out_free_buffer_mask:
3157 free_cpumask_var(tracing_buffer_mask);
3161 early_initcall(tracer_alloc_buffers);
3162 fs_initcall(tracer_init_debugfs);