2 * ring buffer based function tracer
7 * Originally taken from the RT patch by:
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>
41 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
43 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
44 unsigned long __read_mostly tracing_thresh;
48 * Kill all tracing for good (never come back).
49 * It is initialized to 1 but will turn to zero if the initialization
50 * of the tracer is successful. But that is the only place that sets
53 int tracing_disabled = 1;
55 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
57 static inline void ftrace_disable_cpu(void)
60 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
63 static inline void ftrace_enable_cpu(void)
65 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
69 static cpumask_t __read_mostly tracing_buffer_mask;
71 #define for_each_tracing_cpu(cpu) \
72 for_each_cpu_mask(cpu, tracing_buffer_mask)
75 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
77 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
78 * is set, then ftrace_dump is called. This will output the contents
79 * of the ftrace buffers to the console. This is very useful for
80 * capturing traces that lead to crashes and outputing it to a
83 * It is default off, but you can enable it with either specifying
84 * "ftrace_dump_on_oops" in the kernel command line, or setting
85 * /proc/sys/kernel/ftrace_dump_on_oops to true.
87 int ftrace_dump_on_oops;
89 static int tracing_set_tracer(char *buf);
91 static int __init set_ftrace(char *str)
93 tracing_set_tracer(str);
96 __setup("ftrace", set_ftrace);
98 static int __init set_ftrace_dump_on_oops(char *str)
100 ftrace_dump_on_oops = 1;
103 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
106 ns2usecs(cycle_t nsec)
113 cycle_t ftrace_now(int cpu)
115 u64 ts = ring_buffer_time_stamp(cpu);
116 ring_buffer_normalize_time_stamp(cpu, &ts);
121 * The global_trace is the descriptor that holds the tracing
122 * buffers for the live tracing. For each CPU, it contains
123 * a link list of pages that will store trace entries. The
124 * page descriptor of the pages in the memory is used to hold
125 * the link list by linking the lru item in the page descriptor
126 * to each of the pages in the buffer per CPU.
128 * For each active CPU there is a data field that holds the
129 * pages for the buffer for that CPU. Each CPU has the same number
130 * of pages allocated for its buffer.
132 static struct trace_array global_trace;
134 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
137 * The max_tr is used to snapshot the global_trace when a maximum
138 * latency is reached. Some tracers will use this to store a maximum
139 * trace while it continues examining live traces.
141 * The buffers for the max_tr are set up the same as the global_trace.
142 * When a snapshot is taken, the link list of the max_tr is swapped
143 * with the link list of the global_trace and the buffers are reset for
144 * the global_trace so the tracing can continue.
146 static struct trace_array max_tr;
148 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
150 /* tracer_enabled is used to toggle activation of a tracer */
151 static int tracer_enabled = 1;
154 * tracing_is_enabled - return tracer_enabled status
156 * This function is used by other tracers to know the status
157 * of the tracer_enabled flag. Tracers may use this function
158 * to know if it should enable their features when starting
159 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
161 int tracing_is_enabled(void)
163 return tracer_enabled;
166 /* function tracing enabled */
167 int ftrace_function_enabled;
170 * trace_buf_size is the size in bytes that is allocated
171 * for a buffer. Note, the number of bytes is always rounded
174 * This number is purposely set to a low number of 16384.
175 * If the dump on oops happens, it will be much appreciated
176 * to not have to wait for all that output. Anyway this can be
177 * boot time and run time configurable.
179 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
181 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
183 /* trace_types holds a link list of available tracers. */
184 static struct tracer *trace_types __read_mostly;
186 /* current_trace points to the tracer that is currently active */
187 static struct tracer *current_trace __read_mostly;
190 * max_tracer_type_len is used to simplify the allocating of
191 * buffers to read userspace tracer names. We keep track of
192 * the longest tracer name registered.
194 static int max_tracer_type_len;
197 * trace_types_lock is used to protect the trace_types list.
198 * This lock is also used to keep user access serialized.
199 * Accesses from userspace will grab this lock while userspace
200 * activities happen inside the kernel.
202 static DEFINE_MUTEX(trace_types_lock);
204 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
205 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
207 /* trace_flags holds trace_options default values */
208 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
212 * trace_wake_up - wake up tasks waiting for trace input
214 * Simply wakes up any task that is blocked on the trace_wait
215 * queue. These is used with trace_poll for tasks polling the trace.
217 void trace_wake_up(void)
220 * The runqueue_is_locked() can fail, but this is the best we
223 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
224 wake_up(&trace_wait);
227 static int __init set_buf_size(char *str)
229 unsigned long buf_size;
234 ret = strict_strtoul(str, 0, &buf_size);
235 /* nr_entries can not be zero */
236 if (ret < 0 || buf_size == 0)
238 trace_buf_size = buf_size;
241 __setup("trace_buf_size=", set_buf_size);
243 unsigned long nsecs_to_usecs(unsigned long nsecs)
248 /* These must match the bit postions in trace_iterator_flags */
249 static const char *trace_options[] = {
262 #ifdef CONFIG_BRANCH_TRACER
270 * ftrace_max_lock is used to protect the swapping of buffers
271 * when taking a max snapshot. The buffers themselves are
272 * protected by per_cpu spinlocks. But the action of the swap
273 * needs its own lock.
275 * This is defined as a raw_spinlock_t in order to help
276 * with performance when lockdep debugging is enabled.
278 static raw_spinlock_t ftrace_max_lock =
279 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
282 * Copy the new maximum trace into the separate maximum-trace
283 * structure. (this way the maximum trace is permanently saved,
284 * for later retrieval via /debugfs/tracing/latency_trace)
287 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
289 struct trace_array_cpu *data = tr->data[cpu];
292 max_tr.time_start = data->preempt_timestamp;
294 data = max_tr.data[cpu];
295 data->saved_latency = tracing_max_latency;
297 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
298 data->pid = tsk->pid;
299 data->uid = tsk->uid;
300 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
301 data->policy = tsk->policy;
302 data->rt_priority = tsk->rt_priority;
304 /* record this tasks comm */
305 tracing_record_cmdline(current);
309 * trace_seq_printf - sequence printing of trace information
310 * @s: trace sequence descriptor
311 * @fmt: printf format string
313 * The tracer may use either sequence operations or its own
314 * copy to user routines. To simplify formating of a trace
315 * trace_seq_printf is used to store strings into a special
316 * buffer (@s). Then the output may be either used by
317 * the sequencer or pulled into another buffer.
320 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
322 int len = (PAGE_SIZE - 1) - s->len;
330 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
333 /* If we can't write it all, don't bother writing anything */
343 * trace_seq_puts - trace sequence printing of simple string
344 * @s: trace sequence descriptor
345 * @str: simple string to record
347 * The tracer may use either the sequence operations or its own
348 * copy to user routines. This function records a simple string
349 * into a special buffer (@s) for later retrieval by a sequencer
350 * or other mechanism.
353 trace_seq_puts(struct trace_seq *s, const char *str)
355 int len = strlen(str);
357 if (len > ((PAGE_SIZE - 1) - s->len))
360 memcpy(s->buffer + s->len, str, len);
367 trace_seq_putc(struct trace_seq *s, unsigned char c)
369 if (s->len >= (PAGE_SIZE - 1))
372 s->buffer[s->len++] = c;
378 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
380 if (len > ((PAGE_SIZE - 1) - s->len))
383 memcpy(s->buffer + s->len, mem, len);
389 #define MAX_MEMHEX_BYTES 8
390 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
393 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
395 unsigned char hex[HEX_CHARS];
396 unsigned char *data = mem;
400 for (i = 0, j = 0; i < len; i++) {
402 for (i = len-1, j = 0; i >= 0; i--) {
404 hex[j++] = hex_asc_hi(data[i]);
405 hex[j++] = hex_asc_lo(data[i]);
409 return trace_seq_putmem(s, hex, j);
413 trace_seq_reset(struct trace_seq *s)
419 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
424 if (s->len <= s->readpos)
427 len = s->len - s->readpos;
430 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
439 trace_print_seq(struct seq_file *m, struct trace_seq *s)
441 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
444 seq_puts(m, s->buffer);
450 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
452 * @tsk: the task with the latency
453 * @cpu: The cpu that initiated the trace.
455 * Flip the buffers between the @tr and the max_tr and record information
456 * about which task was the cause of this latency.
459 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
461 struct ring_buffer *buf = tr->buffer;
463 WARN_ON_ONCE(!irqs_disabled());
464 __raw_spin_lock(&ftrace_max_lock);
466 tr->buffer = max_tr.buffer;
469 ftrace_disable_cpu();
470 ring_buffer_reset(tr->buffer);
473 __update_max_tr(tr, tsk, cpu);
474 __raw_spin_unlock(&ftrace_max_lock);
478 * update_max_tr_single - only copy one trace over, and reset the rest
480 * @tsk - task with the latency
481 * @cpu - the cpu of the buffer to copy.
483 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
486 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
490 WARN_ON_ONCE(!irqs_disabled());
491 __raw_spin_lock(&ftrace_max_lock);
493 ftrace_disable_cpu();
495 ring_buffer_reset(max_tr.buffer);
496 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
502 __update_max_tr(tr, tsk, cpu);
503 __raw_spin_unlock(&ftrace_max_lock);
507 * register_tracer - register a tracer with the ftrace system.
508 * @type - the plugin for the tracer
510 * Register a new plugin tracer.
512 int register_tracer(struct tracer *type)
519 pr_info("Tracer must have a name\n");
523 mutex_lock(&trace_types_lock);
524 for (t = trace_types; t; t = t->next) {
525 if (strcmp(type->name, t->name) == 0) {
527 pr_info("Trace %s already registered\n",
534 #ifdef CONFIG_FTRACE_STARTUP_TEST
536 * When this gets called we hold the BKL which means that preemption
537 * is disabled. Various trace selftests however need to disable
538 * and enable preemption for successful tests. So we drop the BKL here
539 * and grab it after the tests again.
542 if (type->selftest) {
543 struct tracer *saved_tracer = current_trace;
544 struct trace_array *tr = &global_trace;
547 * Run a selftest on this tracer.
548 * Here we reset the trace buffer, and set the current
549 * tracer to be this tracer. The tracer can then run some
550 * internal tracing to verify that everything is in order.
551 * If we fail, we do not register this tracer.
553 for_each_tracing_cpu(i) {
554 tracing_reset(tr, i);
556 current_trace = type;
557 /* the test is responsible for initializing and enabling */
558 pr_info("Testing tracer %s: ", type->name);
559 ret = type->selftest(type, tr);
560 /* the test is responsible for resetting too */
561 current_trace = saved_tracer;
563 printk(KERN_CONT "FAILED!\n");
566 /* Only reset on passing, to avoid touching corrupted buffers */
567 for_each_tracing_cpu(i) {
568 tracing_reset(tr, i);
570 printk(KERN_CONT "PASSED\n");
575 type->next = trace_types;
577 len = strlen(type->name);
578 if (len > max_tracer_type_len)
579 max_tracer_type_len = len;
582 mutex_unlock(&trace_types_lock);
587 void unregister_tracer(struct tracer *type)
592 mutex_lock(&trace_types_lock);
593 for (t = &trace_types; *t; t = &(*t)->next) {
597 pr_info("Trace %s not registered\n", type->name);
602 if (strlen(type->name) != max_tracer_type_len)
605 max_tracer_type_len = 0;
606 for (t = &trace_types; *t; t = &(*t)->next) {
607 len = strlen((*t)->name);
608 if (len > max_tracer_type_len)
609 max_tracer_type_len = len;
612 mutex_unlock(&trace_types_lock);
615 void tracing_reset(struct trace_array *tr, int cpu)
617 ftrace_disable_cpu();
618 ring_buffer_reset_cpu(tr->buffer, cpu);
622 #define SAVED_CMDLINES 128
623 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
624 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
625 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
626 static int cmdline_idx;
627 static DEFINE_SPINLOCK(trace_cmdline_lock);
629 /* temporary disable recording */
630 atomic_t trace_record_cmdline_disabled __read_mostly;
632 static void trace_init_cmdlines(void)
634 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
635 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
639 static int trace_stop_count;
640 static DEFINE_SPINLOCK(tracing_start_lock);
643 * tracing_start - quick start of the tracer
645 * If tracing is enabled but was stopped by tracing_stop,
646 * this will start the tracer back up.
648 void tracing_start(void)
650 struct ring_buffer *buffer;
653 if (tracing_disabled)
656 spin_lock_irqsave(&tracing_start_lock, flags);
657 if (--trace_stop_count)
660 if (trace_stop_count < 0) {
661 /* Someone screwed up their debugging */
663 trace_stop_count = 0;
668 buffer = global_trace.buffer;
670 ring_buffer_record_enable(buffer);
672 buffer = max_tr.buffer;
674 ring_buffer_record_enable(buffer);
678 spin_unlock_irqrestore(&tracing_start_lock, flags);
682 * tracing_stop - quick stop of the tracer
684 * Light weight way to stop tracing. Use in conjunction with
687 void tracing_stop(void)
689 struct ring_buffer *buffer;
693 spin_lock_irqsave(&tracing_start_lock, flags);
694 if (trace_stop_count++)
697 buffer = global_trace.buffer;
699 ring_buffer_record_disable(buffer);
701 buffer = max_tr.buffer;
703 ring_buffer_record_disable(buffer);
706 spin_unlock_irqrestore(&tracing_start_lock, flags);
709 void trace_stop_cmdline_recording(void);
711 static void trace_save_cmdline(struct task_struct *tsk)
716 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
720 * It's not the end of the world if we don't get
721 * the lock, but we also don't want to spin
722 * nor do we want to disable interrupts,
723 * so if we miss here, then better luck next time.
725 if (!spin_trylock(&trace_cmdline_lock))
728 idx = map_pid_to_cmdline[tsk->pid];
729 if (idx >= SAVED_CMDLINES) {
730 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
732 map = map_cmdline_to_pid[idx];
733 if (map <= PID_MAX_DEFAULT)
734 map_pid_to_cmdline[map] = (unsigned)-1;
736 map_pid_to_cmdline[tsk->pid] = idx;
741 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
743 spin_unlock(&trace_cmdline_lock);
746 static char *trace_find_cmdline(int pid)
748 char *cmdline = "<...>";
754 if (pid > PID_MAX_DEFAULT)
757 map = map_pid_to_cmdline[pid];
758 if (map >= SAVED_CMDLINES)
761 cmdline = saved_cmdlines[map];
767 void tracing_record_cmdline(struct task_struct *tsk)
769 if (atomic_read(&trace_record_cmdline_disabled))
772 trace_save_cmdline(tsk);
776 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
779 struct task_struct *tsk = current;
781 entry->preempt_count = pc & 0xff;
782 entry->pid = (tsk) ? tsk->pid : 0;
784 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
785 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
787 TRACE_FLAG_IRQS_NOSUPPORT |
789 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
790 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
791 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
795 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
796 unsigned long ip, unsigned long parent_ip, unsigned long flags,
799 struct ring_buffer_event *event;
800 struct ftrace_entry *entry;
801 unsigned long irq_flags;
803 /* If we are reading the ring buffer, don't trace */
804 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
807 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
811 entry = ring_buffer_event_data(event);
812 tracing_generic_entry_update(&entry->ent, flags, pc);
813 entry->ent.type = TRACE_FN;
815 entry->parent_ip = parent_ip;
816 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
819 #ifdef CONFIG_FUNCTION_RET_TRACER
820 static void __trace_function_return(struct trace_array *tr,
821 struct trace_array_cpu *data,
822 struct ftrace_retfunc *trace,
826 struct ring_buffer_event *event;
827 struct ftrace_ret_entry *entry;
828 unsigned long irq_flags;
830 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
833 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
837 entry = ring_buffer_event_data(event);
838 tracing_generic_entry_update(&entry->ent, flags, pc);
839 entry->ent.type = TRACE_FN_RET;
840 entry->ip = trace->func;
841 entry->parent_ip = trace->ret;
842 entry->rettime = trace->rettime;
843 entry->calltime = trace->calltime;
844 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
849 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
850 unsigned long ip, unsigned long parent_ip, unsigned long flags,
853 if (likely(!atomic_read(&data->disabled)))
854 trace_function(tr, data, ip, parent_ip, flags, pc);
857 static void ftrace_trace_stack(struct trace_array *tr,
858 struct trace_array_cpu *data,
862 #ifdef CONFIG_STACKTRACE
863 struct ring_buffer_event *event;
864 struct stack_entry *entry;
865 struct stack_trace trace;
866 unsigned long irq_flags;
868 if (!(trace_flags & TRACE_ITER_STACKTRACE))
871 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
875 entry = ring_buffer_event_data(event);
876 tracing_generic_entry_update(&entry->ent, flags, pc);
877 entry->ent.type = TRACE_STACK;
879 memset(&entry->caller, 0, sizeof(entry->caller));
881 trace.nr_entries = 0;
882 trace.max_entries = FTRACE_STACK_ENTRIES;
884 trace.entries = entry->caller;
886 save_stack_trace(&trace);
887 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
891 void __trace_stack(struct trace_array *tr,
892 struct trace_array_cpu *data,
896 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
900 ftrace_trace_special(void *__tr, void *__data,
901 unsigned long arg1, unsigned long arg2, unsigned long arg3,
904 struct ring_buffer_event *event;
905 struct trace_array_cpu *data = __data;
906 struct trace_array *tr = __tr;
907 struct special_entry *entry;
908 unsigned long irq_flags;
910 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
914 entry = ring_buffer_event_data(event);
915 tracing_generic_entry_update(&entry->ent, 0, pc);
916 entry->ent.type = TRACE_SPECIAL;
920 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
921 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
927 __trace_special(void *__tr, void *__data,
928 unsigned long arg1, unsigned long arg2, unsigned long arg3)
930 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
934 tracing_sched_switch_trace(struct trace_array *tr,
935 struct trace_array_cpu *data,
936 struct task_struct *prev,
937 struct task_struct *next,
938 unsigned long flags, int pc)
940 struct ring_buffer_event *event;
941 struct ctx_switch_entry *entry;
942 unsigned long irq_flags;
944 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
948 entry = ring_buffer_event_data(event);
949 tracing_generic_entry_update(&entry->ent, flags, pc);
950 entry->ent.type = TRACE_CTX;
951 entry->prev_pid = prev->pid;
952 entry->prev_prio = prev->prio;
953 entry->prev_state = prev->state;
954 entry->next_pid = next->pid;
955 entry->next_prio = next->prio;
956 entry->next_state = next->state;
957 entry->next_cpu = task_cpu(next);
958 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
959 ftrace_trace_stack(tr, data, flags, 5, pc);
963 tracing_sched_wakeup_trace(struct trace_array *tr,
964 struct trace_array_cpu *data,
965 struct task_struct *wakee,
966 struct task_struct *curr,
967 unsigned long flags, int pc)
969 struct ring_buffer_event *event;
970 struct ctx_switch_entry *entry;
971 unsigned long irq_flags;
973 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
977 entry = ring_buffer_event_data(event);
978 tracing_generic_entry_update(&entry->ent, flags, pc);
979 entry->ent.type = TRACE_WAKE;
980 entry->prev_pid = curr->pid;
981 entry->prev_prio = curr->prio;
982 entry->prev_state = curr->state;
983 entry->next_pid = wakee->pid;
984 entry->next_prio = wakee->prio;
985 entry->next_state = wakee->state;
986 entry->next_cpu = task_cpu(wakee);
987 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
988 ftrace_trace_stack(tr, data, flags, 6, pc);
994 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
996 struct trace_array *tr = &global_trace;
997 struct trace_array_cpu *data;
1002 if (tracing_disabled)
1005 pc = preempt_count();
1006 local_irq_save(flags);
1007 cpu = raw_smp_processor_id();
1008 data = tr->data[cpu];
1010 if (likely(atomic_inc_return(&data->disabled) == 1))
1011 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1013 atomic_dec(&data->disabled);
1014 local_irq_restore(flags);
1017 #ifdef CONFIG_FUNCTION_TRACER
1019 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
1021 struct trace_array *tr = &global_trace;
1022 struct trace_array_cpu *data;
1023 unsigned long flags;
1028 if (unlikely(!ftrace_function_enabled))
1031 pc = preempt_count();
1032 resched = ftrace_preempt_disable();
1033 local_save_flags(flags);
1034 cpu = raw_smp_processor_id();
1035 data = tr->data[cpu];
1036 disabled = atomic_inc_return(&data->disabled);
1038 if (likely(disabled == 1))
1039 trace_function(tr, data, ip, parent_ip, flags, pc);
1041 atomic_dec(&data->disabled);
1042 ftrace_preempt_enable(resched);
1046 function_trace_call(unsigned long ip, unsigned long parent_ip)
1048 struct trace_array *tr = &global_trace;
1049 struct trace_array_cpu *data;
1050 unsigned long flags;
1055 if (unlikely(!ftrace_function_enabled))
1059 * Need to use raw, since this must be called before the
1060 * recursive protection is performed.
1062 local_irq_save(flags);
1063 cpu = raw_smp_processor_id();
1064 data = tr->data[cpu];
1065 disabled = atomic_inc_return(&data->disabled);
1067 if (likely(disabled == 1)) {
1068 pc = preempt_count();
1069 trace_function(tr, data, ip, parent_ip, flags, pc);
1072 atomic_dec(&data->disabled);
1073 local_irq_restore(flags);
1076 #ifdef CONFIG_FUNCTION_RET_TRACER
1077 void trace_function_return(struct ftrace_retfunc *trace)
1079 struct trace_array *tr = &global_trace;
1080 struct trace_array_cpu *data;
1081 unsigned long flags;
1086 raw_local_irq_save(flags);
1087 cpu = raw_smp_processor_id();
1088 data = tr->data[cpu];
1089 disabled = atomic_inc_return(&data->disabled);
1090 if (likely(disabled == 1)) {
1091 pc = preempt_count();
1092 __trace_function_return(tr, data, trace, flags, pc);
1094 atomic_dec(&data->disabled);
1095 raw_local_irq_restore(flags);
1097 #endif /* CONFIG_FUNCTION_RET_TRACER */
1099 static struct ftrace_ops trace_ops __read_mostly =
1101 .func = function_trace_call,
1104 void tracing_start_function_trace(void)
1106 ftrace_function_enabled = 0;
1108 if (trace_flags & TRACE_ITER_PREEMPTONLY)
1109 trace_ops.func = function_trace_call_preempt_only;
1111 trace_ops.func = function_trace_call;
1113 register_ftrace_function(&trace_ops);
1114 ftrace_function_enabled = 1;
1117 void tracing_stop_function_trace(void)
1119 ftrace_function_enabled = 0;
1120 unregister_ftrace_function(&trace_ops);
1124 enum trace_file_type {
1125 TRACE_FILE_LAT_FMT = 1,
1126 TRACE_FILE_ANNOTATE = 2,
1129 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1131 /* Don't allow ftrace to trace into the ring buffers */
1132 ftrace_disable_cpu();
1135 if (iter->buffer_iter[iter->cpu])
1136 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1138 ftrace_enable_cpu();
1141 static struct trace_entry *
1142 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1144 struct ring_buffer_event *event;
1145 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1147 /* Don't allow ftrace to trace into the ring buffers */
1148 ftrace_disable_cpu();
1151 event = ring_buffer_iter_peek(buf_iter, ts);
1153 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1155 ftrace_enable_cpu();
1157 return event ? ring_buffer_event_data(event) : NULL;
1160 static struct trace_entry *
1161 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1163 struct ring_buffer *buffer = iter->tr->buffer;
1164 struct trace_entry *ent, *next = NULL;
1165 u64 next_ts = 0, ts;
1169 for_each_tracing_cpu(cpu) {
1171 if (ring_buffer_empty_cpu(buffer, cpu))
1174 ent = peek_next_entry(iter, cpu, &ts);
1177 * Pick the entry with the smallest timestamp:
1179 if (ent && (!next || ts < next_ts)) {
1187 *ent_cpu = next_cpu;
1195 /* Find the next real entry, without updating the iterator itself */
1196 static struct trace_entry *
1197 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1199 return __find_next_entry(iter, ent_cpu, ent_ts);
1202 /* Find the next real entry, and increment the iterator to the next entry */
1203 static void *find_next_entry_inc(struct trace_iterator *iter)
1205 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1208 trace_iterator_increment(iter, iter->cpu);
1210 return iter->ent ? iter : NULL;
1213 static void trace_consume(struct trace_iterator *iter)
1215 /* Don't allow ftrace to trace into the ring buffers */
1216 ftrace_disable_cpu();
1217 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1218 ftrace_enable_cpu();
1221 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1223 struct trace_iterator *iter = m->private;
1229 /* can't go backwards */
1234 ent = find_next_entry_inc(iter);
1238 while (ent && iter->idx < i)
1239 ent = find_next_entry_inc(iter);
1246 static void *s_start(struct seq_file *m, loff_t *pos)
1248 struct trace_iterator *iter = m->private;
1253 mutex_lock(&trace_types_lock);
1255 if (!current_trace || current_trace != iter->trace) {
1256 mutex_unlock(&trace_types_lock);
1260 atomic_inc(&trace_record_cmdline_disabled);
1262 if (*pos != iter->pos) {
1267 ftrace_disable_cpu();
1269 for_each_tracing_cpu(cpu) {
1270 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1273 ftrace_enable_cpu();
1275 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1280 p = s_next(m, p, &l);
1286 static void s_stop(struct seq_file *m, void *p)
1288 atomic_dec(&trace_record_cmdline_disabled);
1289 mutex_unlock(&trace_types_lock);
1292 #ifdef CONFIG_KRETPROBES
1293 static inline const char *kretprobed(const char *name)
1295 static const char tramp_name[] = "kretprobe_trampoline";
1296 int size = sizeof(tramp_name);
1298 if (strncmp(tramp_name, name, size) == 0)
1299 return "[unknown/kretprobe'd]";
1303 static inline const char *kretprobed(const char *name)
1307 #endif /* CONFIG_KRETPROBES */
1310 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1312 #ifdef CONFIG_KALLSYMS
1313 char str[KSYM_SYMBOL_LEN];
1316 kallsyms_lookup(address, NULL, NULL, NULL, str);
1318 name = kretprobed(str);
1320 return trace_seq_printf(s, fmt, name);
1326 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1327 unsigned long address)
1329 #ifdef CONFIG_KALLSYMS
1330 char str[KSYM_SYMBOL_LEN];
1333 sprint_symbol(str, address);
1334 name = kretprobed(str);
1336 return trace_seq_printf(s, fmt, name);
1341 #ifndef CONFIG_64BIT
1342 # define IP_FMT "%08lx"
1344 # define IP_FMT "%016lx"
1348 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1353 return trace_seq_printf(s, "0");
1355 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1356 ret = seq_print_sym_offset(s, "%s", ip);
1358 ret = seq_print_sym_short(s, "%s", ip);
1363 if (sym_flags & TRACE_ITER_SYM_ADDR)
1364 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1368 static void print_lat_help_header(struct seq_file *m)
1370 seq_puts(m, "# _------=> CPU# \n");
1371 seq_puts(m, "# / _-----=> irqs-off \n");
1372 seq_puts(m, "# | / _----=> need-resched \n");
1373 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1374 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1375 seq_puts(m, "# |||| / \n");
1376 seq_puts(m, "# ||||| delay \n");
1377 seq_puts(m, "# cmd pid ||||| time | caller \n");
1378 seq_puts(m, "# \\ / ||||| \\ | / \n");
1381 static void print_func_help_header(struct seq_file *m)
1383 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1384 seq_puts(m, "# | | | | |\n");
1389 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1391 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1392 struct trace_array *tr = iter->tr;
1393 struct trace_array_cpu *data = tr->data[tr->cpu];
1394 struct tracer *type = current_trace;
1395 unsigned long total;
1396 unsigned long entries;
1397 const char *name = "preemption";
1402 entries = ring_buffer_entries(iter->tr->buffer);
1404 ring_buffer_overruns(iter->tr->buffer);
1406 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1408 seq_puts(m, "-----------------------------------"
1409 "---------------------------------\n");
1410 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1411 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1412 nsecs_to_usecs(data->saved_latency),
1416 #if defined(CONFIG_PREEMPT_NONE)
1418 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1420 #elif defined(CONFIG_PREEMPT)
1425 /* These are reserved for later use */
1428 seq_printf(m, " #P:%d)\n", num_online_cpus());
1432 seq_puts(m, " -----------------\n");
1433 seq_printf(m, " | task: %.16s-%d "
1434 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1435 data->comm, data->pid, data->uid, data->nice,
1436 data->policy, data->rt_priority);
1437 seq_puts(m, " -----------------\n");
1439 if (data->critical_start) {
1440 seq_puts(m, " => started at: ");
1441 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1442 trace_print_seq(m, &iter->seq);
1443 seq_puts(m, "\n => ended at: ");
1444 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1445 trace_print_seq(m, &iter->seq);
1453 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1455 int hardirq, softirq;
1458 comm = trace_find_cmdline(entry->pid);
1460 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1461 trace_seq_printf(s, "%3d", cpu);
1462 trace_seq_printf(s, "%c%c",
1463 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1464 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1465 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1467 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1468 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1469 if (hardirq && softirq) {
1470 trace_seq_putc(s, 'H');
1473 trace_seq_putc(s, 'h');
1476 trace_seq_putc(s, 's');
1478 trace_seq_putc(s, '.');
1482 if (entry->preempt_count)
1483 trace_seq_printf(s, "%x", entry->preempt_count);
1485 trace_seq_puts(s, ".");
1488 unsigned long preempt_mark_thresh = 100;
1491 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1492 unsigned long rel_usecs)
1494 trace_seq_printf(s, " %4lldus", abs_usecs);
1495 if (rel_usecs > preempt_mark_thresh)
1496 trace_seq_puts(s, "!: ");
1497 else if (rel_usecs > 1)
1498 trace_seq_puts(s, "+: ");
1500 trace_seq_puts(s, " : ");
1503 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1506 * The message is supposed to contain an ending newline.
1507 * If the printing stops prematurely, try to add a newline of our own.
1509 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1511 struct trace_entry *ent;
1512 struct trace_field_cont *cont;
1515 ent = peek_next_entry(iter, iter->cpu, NULL);
1516 if (!ent || ent->type != TRACE_CONT) {
1517 trace_seq_putc(s, '\n');
1522 cont = (struct trace_field_cont *)ent;
1524 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1526 ftrace_disable_cpu();
1528 if (iter->buffer_iter[iter->cpu])
1529 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1531 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1533 ftrace_enable_cpu();
1535 ent = peek_next_entry(iter, iter->cpu, NULL);
1536 } while (ent && ent->type == TRACE_CONT);
1539 trace_seq_putc(s, '\n');
1542 static void test_cpu_buff_start(struct trace_iterator *iter)
1544 struct trace_seq *s = &iter->seq;
1546 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1549 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1552 if (cpu_isset(iter->cpu, iter->started))
1555 cpu_set(iter->cpu, iter->started);
1556 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1559 static enum print_line_t
1560 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1562 struct trace_seq *s = &iter->seq;
1563 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1564 struct trace_entry *next_entry;
1565 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1566 struct trace_entry *entry = iter->ent;
1567 unsigned long abs_usecs;
1568 unsigned long rel_usecs;
1575 if (entry->type == TRACE_CONT)
1576 return TRACE_TYPE_HANDLED;
1578 test_cpu_buff_start(iter);
1580 next_entry = find_next_entry(iter, NULL, &next_ts);
1583 rel_usecs = ns2usecs(next_ts - iter->ts);
1584 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1587 comm = trace_find_cmdline(entry->pid);
1588 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1589 " %ld.%03ldms (+%ld.%03ldms): ",
1591 entry->pid, cpu, entry->flags,
1592 entry->preempt_count, trace_idx,
1595 abs_usecs % 1000, rel_usecs/1000,
1598 lat_print_generic(s, entry, cpu);
1599 lat_print_timestamp(s, abs_usecs, rel_usecs);
1601 switch (entry->type) {
1603 struct ftrace_entry *field;
1605 trace_assign_type(field, entry);
1607 seq_print_ip_sym(s, field->ip, sym_flags);
1608 trace_seq_puts(s, " (");
1609 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1610 trace_seq_puts(s, ")\n");
1615 struct ctx_switch_entry *field;
1617 trace_assign_type(field, entry);
1619 T = field->next_state < sizeof(state_to_char) ?
1620 state_to_char[field->next_state] : 'X';
1622 state = field->prev_state ?
1623 __ffs(field->prev_state) + 1 : 0;
1624 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1625 comm = trace_find_cmdline(field->next_pid);
1626 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1629 S, entry->type == TRACE_CTX ? "==>" : " +",
1636 case TRACE_SPECIAL: {
1637 struct special_entry *field;
1639 trace_assign_type(field, entry);
1641 trace_seq_printf(s, "# %ld %ld %ld\n",
1648 struct stack_entry *field;
1650 trace_assign_type(field, entry);
1652 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1654 trace_seq_puts(s, " <= ");
1655 seq_print_ip_sym(s, field->caller[i], sym_flags);
1657 trace_seq_puts(s, "\n");
1661 struct print_entry *field;
1663 trace_assign_type(field, entry);
1665 seq_print_ip_sym(s, field->ip, sym_flags);
1666 trace_seq_printf(s, ": %s", field->buf);
1667 if (entry->flags & TRACE_FLAG_CONT)
1668 trace_seq_print_cont(s, iter);
1671 case TRACE_BRANCH: {
1672 struct trace_branch *field;
1674 trace_assign_type(field, entry);
1676 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1677 field->correct ? " ok " : " MISS ",
1684 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1686 return TRACE_TYPE_HANDLED;
1689 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1691 struct trace_seq *s = &iter->seq;
1692 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1693 struct trace_entry *entry;
1694 unsigned long usec_rem;
1695 unsigned long long t;
1704 if (entry->type == TRACE_CONT)
1705 return TRACE_TYPE_HANDLED;
1707 test_cpu_buff_start(iter);
1709 comm = trace_find_cmdline(iter->ent->pid);
1711 t = ns2usecs(iter->ts);
1712 usec_rem = do_div(t, 1000000ULL);
1713 secs = (unsigned long)t;
1715 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1717 return TRACE_TYPE_PARTIAL_LINE;
1718 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1720 return TRACE_TYPE_PARTIAL_LINE;
1721 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1723 return TRACE_TYPE_PARTIAL_LINE;
1725 switch (entry->type) {
1727 struct ftrace_entry *field;
1729 trace_assign_type(field, entry);
1731 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1733 return TRACE_TYPE_PARTIAL_LINE;
1734 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1736 ret = trace_seq_printf(s, " <-");
1738 return TRACE_TYPE_PARTIAL_LINE;
1739 ret = seq_print_ip_sym(s,
1743 return TRACE_TYPE_PARTIAL_LINE;
1745 ret = trace_seq_printf(s, "\n");
1747 return TRACE_TYPE_PARTIAL_LINE;
1752 struct ctx_switch_entry *field;
1754 trace_assign_type(field, entry);
1756 S = field->prev_state < sizeof(state_to_char) ?
1757 state_to_char[field->prev_state] : 'X';
1758 T = field->next_state < sizeof(state_to_char) ?
1759 state_to_char[field->next_state] : 'X';
1760 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1764 entry->type == TRACE_CTX ? "==>" : " +",
1770 return TRACE_TYPE_PARTIAL_LINE;
1773 case TRACE_SPECIAL: {
1774 struct special_entry *field;
1776 trace_assign_type(field, entry);
1778 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1783 return TRACE_TYPE_PARTIAL_LINE;
1787 struct stack_entry *field;
1789 trace_assign_type(field, entry);
1791 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1793 ret = trace_seq_puts(s, " <= ");
1795 return TRACE_TYPE_PARTIAL_LINE;
1797 ret = seq_print_ip_sym(s, field->caller[i],
1800 return TRACE_TYPE_PARTIAL_LINE;
1802 ret = trace_seq_puts(s, "\n");
1804 return TRACE_TYPE_PARTIAL_LINE;
1808 struct print_entry *field;
1810 trace_assign_type(field, entry);
1812 seq_print_ip_sym(s, field->ip, sym_flags);
1813 trace_seq_printf(s, ": %s", field->buf);
1814 if (entry->flags & TRACE_FLAG_CONT)
1815 trace_seq_print_cont(s, iter);
1818 case TRACE_FN_RET: {
1819 return print_return_function(iter);
1822 case TRACE_BRANCH: {
1823 struct trace_branch *field;
1825 trace_assign_type(field, entry);
1827 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1828 field->correct ? " ok " : " MISS ",
1835 return TRACE_TYPE_HANDLED;
1838 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1840 struct trace_seq *s = &iter->seq;
1841 struct trace_entry *entry;
1847 if (entry->type == TRACE_CONT)
1848 return TRACE_TYPE_HANDLED;
1850 ret = trace_seq_printf(s, "%d %d %llu ",
1851 entry->pid, iter->cpu, iter->ts);
1853 return TRACE_TYPE_PARTIAL_LINE;
1855 switch (entry->type) {
1857 struct ftrace_entry *field;
1859 trace_assign_type(field, entry);
1861 ret = trace_seq_printf(s, "%x %x\n",
1865 return TRACE_TYPE_PARTIAL_LINE;
1870 struct ctx_switch_entry *field;
1872 trace_assign_type(field, entry);
1874 S = field->prev_state < sizeof(state_to_char) ?
1875 state_to_char[field->prev_state] : 'X';
1876 T = field->next_state < sizeof(state_to_char) ?
1877 state_to_char[field->next_state] : 'X';
1878 if (entry->type == TRACE_WAKE)
1880 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
1889 return TRACE_TYPE_PARTIAL_LINE;
1894 struct special_entry *field;
1896 trace_assign_type(field, entry);
1898 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1903 return TRACE_TYPE_PARTIAL_LINE;
1907 struct print_entry *field;
1909 trace_assign_type(field, entry);
1911 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
1912 if (entry->flags & TRACE_FLAG_CONT)
1913 trace_seq_print_cont(s, iter);
1917 return TRACE_TYPE_HANDLED;
1920 #define SEQ_PUT_FIELD_RET(s, x) \
1922 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1926 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1928 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
1929 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1933 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1935 struct trace_seq *s = &iter->seq;
1936 unsigned char newline = '\n';
1937 struct trace_entry *entry;
1942 if (entry->type == TRACE_CONT)
1943 return TRACE_TYPE_HANDLED;
1945 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1946 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1947 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1949 switch (entry->type) {
1951 struct ftrace_entry *field;
1953 trace_assign_type(field, entry);
1955 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1956 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
1961 struct ctx_switch_entry *field;
1963 trace_assign_type(field, entry);
1965 S = field->prev_state < sizeof(state_to_char) ?
1966 state_to_char[field->prev_state] : 'X';
1967 T = field->next_state < sizeof(state_to_char) ?
1968 state_to_char[field->next_state] : 'X';
1969 if (entry->type == TRACE_WAKE)
1971 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1972 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
1973 SEQ_PUT_HEX_FIELD_RET(s, S);
1974 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1975 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1976 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
1977 SEQ_PUT_HEX_FIELD_RET(s, T);
1982 struct special_entry *field;
1984 trace_assign_type(field, entry);
1986 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
1987 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
1988 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
1992 SEQ_PUT_FIELD_RET(s, newline);
1994 return TRACE_TYPE_HANDLED;
1997 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1999 struct trace_seq *s = &iter->seq;
2000 struct trace_entry *entry;
2004 if (entry->type == TRACE_CONT)
2005 return TRACE_TYPE_HANDLED;
2007 SEQ_PUT_FIELD_RET(s, entry->pid);
2008 SEQ_PUT_FIELD_RET(s, entry->cpu);
2009 SEQ_PUT_FIELD_RET(s, iter->ts);
2011 switch (entry->type) {
2013 struct ftrace_entry *field;
2015 trace_assign_type(field, entry);
2017 SEQ_PUT_FIELD_RET(s, field->ip);
2018 SEQ_PUT_FIELD_RET(s, field->parent_ip);
2022 struct ctx_switch_entry *field;
2024 trace_assign_type(field, entry);
2026 SEQ_PUT_FIELD_RET(s, field->prev_pid);
2027 SEQ_PUT_FIELD_RET(s, field->prev_prio);
2028 SEQ_PUT_FIELD_RET(s, field->prev_state);
2029 SEQ_PUT_FIELD_RET(s, field->next_pid);
2030 SEQ_PUT_FIELD_RET(s, field->next_prio);
2031 SEQ_PUT_FIELD_RET(s, field->next_state);
2036 struct special_entry *field;
2038 trace_assign_type(field, entry);
2040 SEQ_PUT_FIELD_RET(s, field->arg1);
2041 SEQ_PUT_FIELD_RET(s, field->arg2);
2042 SEQ_PUT_FIELD_RET(s, field->arg3);
2049 static int trace_empty(struct trace_iterator *iter)
2053 for_each_tracing_cpu(cpu) {
2054 if (iter->buffer_iter[cpu]) {
2055 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2058 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2066 static enum print_line_t print_trace_line(struct trace_iterator *iter)
2068 enum print_line_t ret;
2070 if (iter->trace && iter->trace->print_line) {
2071 ret = iter->trace->print_line(iter);
2072 if (ret != TRACE_TYPE_UNHANDLED)
2076 if (trace_flags & TRACE_ITER_BIN)
2077 return print_bin_fmt(iter);
2079 if (trace_flags & TRACE_ITER_HEX)
2080 return print_hex_fmt(iter);
2082 if (trace_flags & TRACE_ITER_RAW)
2083 return print_raw_fmt(iter);
2085 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2086 return print_lat_fmt(iter, iter->idx, iter->cpu);
2088 return print_trace_fmt(iter);
2091 static int s_show(struct seq_file *m, void *v)
2093 struct trace_iterator *iter = v;
2095 if (iter->ent == NULL) {
2097 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2100 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2101 /* print nothing if the buffers are empty */
2102 if (trace_empty(iter))
2104 print_trace_header(m, iter);
2105 if (!(trace_flags & TRACE_ITER_VERBOSE))
2106 print_lat_help_header(m);
2108 if (!(trace_flags & TRACE_ITER_VERBOSE))
2109 print_func_help_header(m);
2112 print_trace_line(iter);
2113 trace_print_seq(m, &iter->seq);
2119 static struct seq_operations tracer_seq_ops = {
2126 static struct trace_iterator *
2127 __tracing_open(struct inode *inode, struct file *file, int *ret)
2129 struct trace_iterator *iter;
2133 if (tracing_disabled) {
2138 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2144 mutex_lock(&trace_types_lock);
2145 if (current_trace && current_trace->print_max)
2148 iter->tr = inode->i_private;
2149 iter->trace = current_trace;
2152 /* Annotate start of buffers if we had overruns */
2153 if (ring_buffer_overruns(iter->tr->buffer))
2154 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2157 for_each_tracing_cpu(cpu) {
2159 iter->buffer_iter[cpu] =
2160 ring_buffer_read_start(iter->tr->buffer, cpu);
2162 if (!iter->buffer_iter[cpu])
2166 /* TODO stop tracer */
2167 *ret = seq_open(file, &tracer_seq_ops);
2171 m = file->private_data;
2174 /* stop the trace while dumping */
2177 if (iter->trace && iter->trace->open)
2178 iter->trace->open(iter);
2180 mutex_unlock(&trace_types_lock);
2186 for_each_tracing_cpu(cpu) {
2187 if (iter->buffer_iter[cpu])
2188 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2190 mutex_unlock(&trace_types_lock);
2192 return ERR_PTR(-ENOMEM);
2195 int tracing_open_generic(struct inode *inode, struct file *filp)
2197 if (tracing_disabled)
2200 filp->private_data = inode->i_private;
2204 int tracing_release(struct inode *inode, struct file *file)
2206 struct seq_file *m = (struct seq_file *)file->private_data;
2207 struct trace_iterator *iter = m->private;
2210 mutex_lock(&trace_types_lock);
2211 for_each_tracing_cpu(cpu) {
2212 if (iter->buffer_iter[cpu])
2213 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2216 if (iter->trace && iter->trace->close)
2217 iter->trace->close(iter);
2219 /* reenable tracing if it was previously enabled */
2221 mutex_unlock(&trace_types_lock);
2223 seq_release(inode, file);
2228 static int tracing_open(struct inode *inode, struct file *file)
2232 __tracing_open(inode, file, &ret);
2237 static int tracing_lt_open(struct inode *inode, struct file *file)
2239 struct trace_iterator *iter;
2242 iter = __tracing_open(inode, file, &ret);
2245 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2252 t_next(struct seq_file *m, void *v, loff_t *pos)
2254 struct tracer *t = m->private;
2266 static void *t_start(struct seq_file *m, loff_t *pos)
2268 struct tracer *t = m->private;
2271 mutex_lock(&trace_types_lock);
2272 for (; t && l < *pos; t = t_next(m, t, &l))
2278 static void t_stop(struct seq_file *m, void *p)
2280 mutex_unlock(&trace_types_lock);
2283 static int t_show(struct seq_file *m, void *v)
2285 struct tracer *t = v;
2290 seq_printf(m, "%s", t->name);
2299 static struct seq_operations show_traces_seq_ops = {
2306 static int show_traces_open(struct inode *inode, struct file *file)
2310 if (tracing_disabled)
2313 ret = seq_open(file, &show_traces_seq_ops);
2315 struct seq_file *m = file->private_data;
2316 m->private = trace_types;
2322 static struct file_operations tracing_fops = {
2323 .open = tracing_open,
2325 .llseek = seq_lseek,
2326 .release = tracing_release,
2329 static struct file_operations tracing_lt_fops = {
2330 .open = tracing_lt_open,
2332 .llseek = seq_lseek,
2333 .release = tracing_release,
2336 static struct file_operations show_traces_fops = {
2337 .open = show_traces_open,
2339 .release = seq_release,
2343 * Only trace on a CPU if the bitmask is set:
2345 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2348 * When tracing/tracing_cpu_mask is modified then this holds
2349 * the new bitmask we are about to install:
2351 static cpumask_t tracing_cpumask_new;
2354 * The tracer itself will not take this lock, but still we want
2355 * to provide a consistent cpumask to user-space:
2357 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2360 * Temporary storage for the character representation of the
2361 * CPU bitmask (and one more byte for the newline):
2363 static char mask_str[NR_CPUS + 1];
2366 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2367 size_t count, loff_t *ppos)
2371 mutex_lock(&tracing_cpumask_update_lock);
2373 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2374 if (count - len < 2) {
2378 len += sprintf(mask_str + len, "\n");
2379 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2382 mutex_unlock(&tracing_cpumask_update_lock);
2388 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2389 size_t count, loff_t *ppos)
2393 mutex_lock(&tracing_cpumask_update_lock);
2394 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2398 raw_local_irq_disable();
2399 __raw_spin_lock(&ftrace_max_lock);
2400 for_each_tracing_cpu(cpu) {
2402 * Increase/decrease the disabled counter if we are
2403 * about to flip a bit in the cpumask:
2405 if (cpu_isset(cpu, tracing_cpumask) &&
2406 !cpu_isset(cpu, tracing_cpumask_new)) {
2407 atomic_inc(&global_trace.data[cpu]->disabled);
2409 if (!cpu_isset(cpu, tracing_cpumask) &&
2410 cpu_isset(cpu, tracing_cpumask_new)) {
2411 atomic_dec(&global_trace.data[cpu]->disabled);
2414 __raw_spin_unlock(&ftrace_max_lock);
2415 raw_local_irq_enable();
2417 tracing_cpumask = tracing_cpumask_new;
2419 mutex_unlock(&tracing_cpumask_update_lock);
2424 mutex_unlock(&tracing_cpumask_update_lock);
2429 static struct file_operations tracing_cpumask_fops = {
2430 .open = tracing_open_generic,
2431 .read = tracing_cpumask_read,
2432 .write = tracing_cpumask_write,
2436 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2437 size_t cnt, loff_t *ppos)
2444 /* calulate max size */
2445 for (i = 0; trace_options[i]; i++) {
2446 len += strlen(trace_options[i]);
2447 len += 3; /* "no" and space */
2450 /* +2 for \n and \0 */
2451 buf = kmalloc(len + 2, GFP_KERNEL);
2455 for (i = 0; trace_options[i]; i++) {
2456 if (trace_flags & (1 << i))
2457 r += sprintf(buf + r, "%s ", trace_options[i]);
2459 r += sprintf(buf + r, "no%s ", trace_options[i]);
2462 r += sprintf(buf + r, "\n");
2463 WARN_ON(r >= len + 2);
2465 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2473 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2474 size_t cnt, loff_t *ppos)
2481 if (cnt >= sizeof(buf))
2484 if (copy_from_user(&buf, ubuf, cnt))
2489 if (strncmp(buf, "no", 2) == 0) {
2494 for (i = 0; trace_options[i]; i++) {
2495 int len = strlen(trace_options[i]);
2497 if (strncmp(cmp, trace_options[i], len) == 0) {
2499 trace_flags &= ~(1 << i);
2501 trace_flags |= (1 << i);
2506 * If no option could be set, return an error:
2508 if (!trace_options[i])
2516 static struct file_operations tracing_iter_fops = {
2517 .open = tracing_open_generic,
2518 .read = tracing_trace_options_read,
2519 .write = tracing_trace_options_write,
2522 static const char readme_msg[] =
2523 "tracing mini-HOWTO:\n\n"
2525 "# mount -t debugfs nodev /debug\n\n"
2526 "# cat /debug/tracing/available_tracers\n"
2527 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2528 "# cat /debug/tracing/current_tracer\n"
2530 "# echo sched_switch > /debug/tracing/current_tracer\n"
2531 "# cat /debug/tracing/current_tracer\n"
2533 "# cat /debug/tracing/trace_options\n"
2534 "noprint-parent nosym-offset nosym-addr noverbose\n"
2535 "# echo print-parent > /debug/tracing/trace_options\n"
2536 "# echo 1 > /debug/tracing/tracing_enabled\n"
2537 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2538 "echo 0 > /debug/tracing/tracing_enabled\n"
2542 tracing_readme_read(struct file *filp, char __user *ubuf,
2543 size_t cnt, loff_t *ppos)
2545 return simple_read_from_buffer(ubuf, cnt, ppos,
2546 readme_msg, strlen(readme_msg));
2549 static struct file_operations tracing_readme_fops = {
2550 .open = tracing_open_generic,
2551 .read = tracing_readme_read,
2555 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2556 size_t cnt, loff_t *ppos)
2561 r = sprintf(buf, "%u\n", tracer_enabled);
2562 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2566 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2567 size_t cnt, loff_t *ppos)
2569 struct trace_array *tr = filp->private_data;
2574 if (cnt >= sizeof(buf))
2577 if (copy_from_user(&buf, ubuf, cnt))
2582 ret = strict_strtoul(buf, 10, &val);
2588 mutex_lock(&trace_types_lock);
2589 if (tracer_enabled ^ val) {
2592 if (current_trace->start)
2593 current_trace->start(tr);
2598 if (current_trace->stop)
2599 current_trace->stop(tr);
2602 mutex_unlock(&trace_types_lock);
2610 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2611 size_t cnt, loff_t *ppos)
2613 char buf[max_tracer_type_len+2];
2616 mutex_lock(&trace_types_lock);
2618 r = sprintf(buf, "%s\n", current_trace->name);
2620 r = sprintf(buf, "\n");
2621 mutex_unlock(&trace_types_lock);
2623 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2626 static int tracing_set_tracer(char *buf)
2628 struct trace_array *tr = &global_trace;
2632 mutex_lock(&trace_types_lock);
2633 for (t = trace_types; t; t = t->next) {
2634 if (strcmp(t->name, buf) == 0)
2641 if (t == current_trace)
2644 trace_branch_disable();
2645 if (current_trace && current_trace->reset)
2646 current_trace->reset(tr);
2655 trace_branch_enable(tr);
2657 mutex_unlock(&trace_types_lock);
2663 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2664 size_t cnt, loff_t *ppos)
2666 char buf[max_tracer_type_len+1];
2673 if (cnt > max_tracer_type_len)
2674 cnt = max_tracer_type_len;
2676 if (copy_from_user(&buf, ubuf, cnt))
2681 /* strip ending whitespace. */
2682 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2685 err = tracing_set_tracer(buf);
2695 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2696 size_t cnt, loff_t *ppos)
2698 unsigned long *ptr = filp->private_data;
2702 r = snprintf(buf, sizeof(buf), "%ld\n",
2703 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2704 if (r > sizeof(buf))
2706 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2710 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2711 size_t cnt, loff_t *ppos)
2713 long *ptr = filp->private_data;
2718 if (cnt >= sizeof(buf))
2721 if (copy_from_user(&buf, ubuf, cnt))
2726 ret = strict_strtoul(buf, 10, &val);
2735 static atomic_t tracing_reader;
2737 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2739 struct trace_iterator *iter;
2741 if (tracing_disabled)
2744 /* We only allow for reader of the pipe */
2745 if (atomic_inc_return(&tracing_reader) != 1) {
2746 atomic_dec(&tracing_reader);
2750 /* create a buffer to store the information to pass to userspace */
2751 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2755 mutex_lock(&trace_types_lock);
2757 /* trace pipe does not show start of buffer */
2758 cpus_setall(iter->started);
2760 iter->tr = &global_trace;
2761 iter->trace = current_trace;
2762 filp->private_data = iter;
2764 if (iter->trace->pipe_open)
2765 iter->trace->pipe_open(iter);
2766 mutex_unlock(&trace_types_lock);
2771 static int tracing_release_pipe(struct inode *inode, struct file *file)
2773 struct trace_iterator *iter = file->private_data;
2776 atomic_dec(&tracing_reader);
2782 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2784 struct trace_iterator *iter = filp->private_data;
2786 if (trace_flags & TRACE_ITER_BLOCK) {
2788 * Always select as readable when in blocking mode
2790 return POLLIN | POLLRDNORM;
2792 if (!trace_empty(iter))
2793 return POLLIN | POLLRDNORM;
2794 poll_wait(filp, &trace_wait, poll_table);
2795 if (!trace_empty(iter))
2796 return POLLIN | POLLRDNORM;
2806 tracing_read_pipe(struct file *filp, char __user *ubuf,
2807 size_t cnt, loff_t *ppos)
2809 struct trace_iterator *iter = filp->private_data;
2812 /* return any leftover data */
2813 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2817 trace_seq_reset(&iter->seq);
2819 mutex_lock(&trace_types_lock);
2820 if (iter->trace->read) {
2821 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2828 while (trace_empty(iter)) {
2830 if ((filp->f_flags & O_NONBLOCK)) {
2836 * This is a make-shift waitqueue. The reason we don't use
2837 * an actual wait queue is because:
2838 * 1) we only ever have one waiter
2839 * 2) the tracing, traces all functions, we don't want
2840 * the overhead of calling wake_up and friends
2841 * (and tracing them too)
2842 * Anyway, this is really very primitive wakeup.
2844 set_current_state(TASK_INTERRUPTIBLE);
2845 iter->tr->waiter = current;
2847 mutex_unlock(&trace_types_lock);
2849 /* sleep for 100 msecs, and try again. */
2850 schedule_timeout(HZ/10);
2852 mutex_lock(&trace_types_lock);
2854 iter->tr->waiter = NULL;
2856 if (signal_pending(current)) {
2861 if (iter->trace != current_trace)
2865 * We block until we read something and tracing is disabled.
2866 * We still block if tracing is disabled, but we have never
2867 * read anything. This allows a user to cat this file, and
2868 * then enable tracing. But after we have read something,
2869 * we give an EOF when tracing is again disabled.
2871 * iter->pos will be 0 if we haven't read anything.
2873 if (!tracer_enabled && iter->pos)
2879 /* stop when tracing is finished */
2880 if (trace_empty(iter))
2883 if (cnt >= PAGE_SIZE)
2884 cnt = PAGE_SIZE - 1;
2886 /* reset all but tr, trace, and overruns */
2887 memset(&iter->seq, 0,
2888 sizeof(struct trace_iterator) -
2889 offsetof(struct trace_iterator, seq));
2892 while (find_next_entry_inc(iter) != NULL) {
2893 enum print_line_t ret;
2894 int len = iter->seq.len;
2896 ret = print_trace_line(iter);
2897 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2898 /* don't print partial lines */
2899 iter->seq.len = len;
2903 trace_consume(iter);
2905 if (iter->seq.len >= cnt)
2909 /* Now copy what we have to the user */
2910 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2911 if (iter->seq.readpos >= iter->seq.len)
2912 trace_seq_reset(&iter->seq);
2915 * If there was nothing to send to user, inspite of consuming trace
2916 * entries, go back to wait for more entries.
2922 mutex_unlock(&trace_types_lock);
2928 tracing_entries_read(struct file *filp, char __user *ubuf,
2929 size_t cnt, loff_t *ppos)
2931 struct trace_array *tr = filp->private_data;
2935 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2936 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2940 tracing_entries_write(struct file *filp, const char __user *ubuf,
2941 size_t cnt, loff_t *ppos)
2947 if (cnt >= sizeof(buf))
2950 if (copy_from_user(&buf, ubuf, cnt))
2955 ret = strict_strtoul(buf, 10, &val);
2959 /* must have at least 1 entry */
2963 mutex_lock(&trace_types_lock);
2967 /* disable all cpu buffers */
2968 for_each_tracing_cpu(cpu) {
2969 if (global_trace.data[cpu])
2970 atomic_inc(&global_trace.data[cpu]->disabled);
2971 if (max_tr.data[cpu])
2972 atomic_inc(&max_tr.data[cpu]->disabled);
2975 /* value is in KB */
2978 if (val != global_trace.entries) {
2979 ret = ring_buffer_resize(global_trace.buffer, val);
2985 ret = ring_buffer_resize(max_tr.buffer, val);
2989 r = ring_buffer_resize(global_trace.buffer,
2990 global_trace.entries);
2992 /* AARGH! We are left with different
2993 * size max buffer!!!! */
2995 tracing_disabled = 1;
3000 global_trace.entries = val;
3005 /* If check pages failed, return ENOMEM */
3006 if (tracing_disabled)
3009 for_each_tracing_cpu(cpu) {
3010 if (global_trace.data[cpu])
3011 atomic_dec(&global_trace.data[cpu]->disabled);
3012 if (max_tr.data[cpu])
3013 atomic_dec(&max_tr.data[cpu]->disabled);
3017 max_tr.entries = global_trace.entries;
3018 mutex_unlock(&trace_types_lock);
3023 static int mark_printk(const char *fmt, ...)
3027 va_start(args, fmt);
3028 ret = trace_vprintk(0, fmt, args);
3034 tracing_mark_write(struct file *filp, const char __user *ubuf,
3035 size_t cnt, loff_t *fpos)
3040 if (tracing_disabled)
3043 if (cnt > TRACE_BUF_SIZE)
3044 cnt = TRACE_BUF_SIZE;
3046 buf = kmalloc(cnt + 1, GFP_KERNEL);
3050 if (copy_from_user(buf, ubuf, cnt)) {
3055 /* Cut from the first nil or newline. */
3057 end = strchr(buf, '\n');
3061 cnt = mark_printk("%s\n", buf);
3068 static struct file_operations tracing_max_lat_fops = {
3069 .open = tracing_open_generic,
3070 .read = tracing_max_lat_read,
3071 .write = tracing_max_lat_write,
3074 static struct file_operations tracing_ctrl_fops = {
3075 .open = tracing_open_generic,
3076 .read = tracing_ctrl_read,
3077 .write = tracing_ctrl_write,
3080 static struct file_operations set_tracer_fops = {
3081 .open = tracing_open_generic,
3082 .read = tracing_set_trace_read,
3083 .write = tracing_set_trace_write,
3086 static struct file_operations tracing_pipe_fops = {
3087 .open = tracing_open_pipe,
3088 .poll = tracing_poll_pipe,
3089 .read = tracing_read_pipe,
3090 .release = tracing_release_pipe,
3093 static struct file_operations tracing_entries_fops = {
3094 .open = tracing_open_generic,
3095 .read = tracing_entries_read,
3096 .write = tracing_entries_write,
3099 static struct file_operations tracing_mark_fops = {
3100 .open = tracing_open_generic,
3101 .write = tracing_mark_write,
3104 #ifdef CONFIG_DYNAMIC_FTRACE
3106 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3112 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3113 size_t cnt, loff_t *ppos)
3115 static char ftrace_dyn_info_buffer[1024];
3116 static DEFINE_MUTEX(dyn_info_mutex);
3117 unsigned long *p = filp->private_data;
3118 char *buf = ftrace_dyn_info_buffer;
3119 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3122 mutex_lock(&dyn_info_mutex);
3123 r = sprintf(buf, "%ld ", *p);
3125 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3128 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3130 mutex_unlock(&dyn_info_mutex);
3135 static struct file_operations tracing_dyn_info_fops = {
3136 .open = tracing_open_generic,
3137 .read = tracing_read_dyn_info,
3141 static struct dentry *d_tracer;
3143 struct dentry *tracing_init_dentry(void)
3150 d_tracer = debugfs_create_dir("tracing", NULL);
3152 if (!d_tracer && !once) {
3154 pr_warning("Could not create debugfs directory 'tracing'\n");
3161 #ifdef CONFIG_FTRACE_SELFTEST
3162 /* Let selftest have access to static functions in this file */
3163 #include "trace_selftest.c"
3166 static __init int tracer_init_debugfs(void)
3168 struct dentry *d_tracer;
3169 struct dentry *entry;
3171 d_tracer = tracing_init_dentry();
3173 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3174 &global_trace, &tracing_ctrl_fops);
3176 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3178 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3179 NULL, &tracing_iter_fops);
3181 pr_warning("Could not create debugfs 'trace_options' entry\n");
3183 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3184 NULL, &tracing_cpumask_fops);
3186 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3188 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3189 &global_trace, &tracing_lt_fops);
3191 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3193 entry = debugfs_create_file("trace", 0444, d_tracer,
3194 &global_trace, &tracing_fops);
3196 pr_warning("Could not create debugfs 'trace' entry\n");
3198 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3199 &global_trace, &show_traces_fops);
3201 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3203 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3204 &global_trace, &set_tracer_fops);
3206 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3208 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3209 &tracing_max_latency,
3210 &tracing_max_lat_fops);
3212 pr_warning("Could not create debugfs "
3213 "'tracing_max_latency' entry\n");
3215 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3216 &tracing_thresh, &tracing_max_lat_fops);
3218 pr_warning("Could not create debugfs "
3219 "'tracing_thresh' entry\n");
3220 entry = debugfs_create_file("README", 0644, d_tracer,
3221 NULL, &tracing_readme_fops);
3223 pr_warning("Could not create debugfs 'README' entry\n");
3225 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3226 NULL, &tracing_pipe_fops);
3228 pr_warning("Could not create debugfs "
3229 "'trace_pipe' entry\n");
3231 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3232 &global_trace, &tracing_entries_fops);
3234 pr_warning("Could not create debugfs "
3235 "'buffer_size_kb' entry\n");
3237 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3238 NULL, &tracing_mark_fops);
3240 pr_warning("Could not create debugfs "
3241 "'trace_marker' entry\n");
3243 #ifdef CONFIG_DYNAMIC_FTRACE
3244 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3245 &ftrace_update_tot_cnt,
3246 &tracing_dyn_info_fops);
3248 pr_warning("Could not create debugfs "
3249 "'dyn_ftrace_total_info' entry\n");
3251 #ifdef CONFIG_SYSPROF_TRACER
3252 init_tracer_sysprof_debugfs(d_tracer);
3257 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3259 static DEFINE_SPINLOCK(trace_buf_lock);
3260 static char trace_buf[TRACE_BUF_SIZE];
3262 struct ring_buffer_event *event;
3263 struct trace_array *tr = &global_trace;
3264 struct trace_array_cpu *data;
3265 struct print_entry *entry;
3266 unsigned long flags, irq_flags;
3267 int cpu, len = 0, size, pc;
3269 if (tracing_disabled)
3272 pc = preempt_count();
3273 preempt_disable_notrace();
3274 cpu = raw_smp_processor_id();
3275 data = tr->data[cpu];
3277 if (unlikely(atomic_read(&data->disabled)))
3280 spin_lock_irqsave(&trace_buf_lock, flags);
3281 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3283 len = min(len, TRACE_BUF_SIZE-1);
3286 size = sizeof(*entry) + len + 1;
3287 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3290 entry = ring_buffer_event_data(event);
3291 tracing_generic_entry_update(&entry->ent, flags, pc);
3292 entry->ent.type = TRACE_PRINT;
3295 memcpy(&entry->buf, trace_buf, len);
3296 entry->buf[len] = 0;
3297 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3300 spin_unlock_irqrestore(&trace_buf_lock, flags);
3303 preempt_enable_notrace();
3307 EXPORT_SYMBOL_GPL(trace_vprintk);
3309 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3314 if (!(trace_flags & TRACE_ITER_PRINTK))
3318 ret = trace_vprintk(ip, fmt, ap);
3322 EXPORT_SYMBOL_GPL(__ftrace_printk);
3324 static int trace_panic_handler(struct notifier_block *this,
3325 unsigned long event, void *unused)
3327 if (ftrace_dump_on_oops)
3332 static struct notifier_block trace_panic_notifier = {
3333 .notifier_call = trace_panic_handler,
3335 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3338 static int trace_die_handler(struct notifier_block *self,
3344 if (ftrace_dump_on_oops)
3353 static struct notifier_block trace_die_notifier = {
3354 .notifier_call = trace_die_handler,
3359 * printk is set to max of 1024, we really don't need it that big.
3360 * Nothing should be printing 1000 characters anyway.
3362 #define TRACE_MAX_PRINT 1000
3365 * Define here KERN_TRACE so that we have one place to modify
3366 * it if we decide to change what log level the ftrace dump
3369 #define KERN_TRACE KERN_INFO
3372 trace_printk_seq(struct trace_seq *s)
3374 /* Probably should print a warning here. */
3378 /* should be zero ended, but we are paranoid. */
3379 s->buffer[s->len] = 0;
3381 printk(KERN_TRACE "%s", s->buffer);
3386 void ftrace_dump(void)
3388 static DEFINE_SPINLOCK(ftrace_dump_lock);
3389 /* use static because iter can be a bit big for the stack */
3390 static struct trace_iterator iter;
3391 static cpumask_t mask;
3392 static int dump_ran;
3393 unsigned long flags;
3397 spin_lock_irqsave(&ftrace_dump_lock, flags);
3403 /* No turning back! */
3406 for_each_tracing_cpu(cpu) {
3407 atomic_inc(&global_trace.data[cpu]->disabled);
3410 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3412 iter.tr = &global_trace;
3413 iter.trace = current_trace;
3416 * We need to stop all tracing on all CPUS to read the
3417 * the next buffer. This is a bit expensive, but is
3418 * not done often. We fill all what we can read,
3419 * and then release the locks again.
3424 while (!trace_empty(&iter)) {
3427 printk(KERN_TRACE "---------------------------------\n");
3431 /* reset all but tr, trace, and overruns */
3432 memset(&iter.seq, 0,
3433 sizeof(struct trace_iterator) -
3434 offsetof(struct trace_iterator, seq));
3435 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3438 if (find_next_entry_inc(&iter) != NULL) {
3439 print_trace_line(&iter);
3440 trace_consume(&iter);
3443 trace_printk_seq(&iter.seq);
3447 printk(KERN_TRACE " (ftrace buffer empty)\n");
3449 printk(KERN_TRACE "---------------------------------\n");
3452 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3455 __init static int tracer_alloc_buffers(void)
3457 struct trace_array_cpu *data;
3460 /* TODO: make the number of buffers hot pluggable with CPUS */
3461 tracing_buffer_mask = cpu_possible_map;
3463 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3464 TRACE_BUFFER_FLAGS);
3465 if (!global_trace.buffer) {
3466 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3470 global_trace.entries = ring_buffer_size(global_trace.buffer);
3472 #ifdef CONFIG_TRACER_MAX_TRACE
3473 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3474 TRACE_BUFFER_FLAGS);
3475 if (!max_tr.buffer) {
3476 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3478 ring_buffer_free(global_trace.buffer);
3481 max_tr.entries = ring_buffer_size(max_tr.buffer);
3482 WARN_ON(max_tr.entries != global_trace.entries);
3485 /* Allocate the first page for all buffers */
3486 for_each_tracing_cpu(i) {
3487 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3488 max_tr.data[i] = &per_cpu(max_data, i);
3491 trace_init_cmdlines();
3493 register_tracer(&nop_trace);
3494 #ifdef CONFIG_BOOT_TRACER
3495 register_tracer(&boot_tracer);
3496 current_trace = &boot_tracer;
3497 current_trace->init(&global_trace);
3499 current_trace = &nop_trace;
3502 /* All seems OK, enable tracing */
3503 tracing_disabled = 0;
3505 atomic_notifier_chain_register(&panic_notifier_list,
3506 &trace_panic_notifier);
3508 register_die_notifier(&trace_die_notifier);
3512 early_initcall(tracer_alloc_buffers);
3513 fs_initcall(tracer_init_debugfs);