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>
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;
46 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
48 static inline void ftrace_disable_cpu(void)
51 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
54 static inline void ftrace_enable_cpu(void)
56 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
60 static cpumask_t __read_mostly tracing_buffer_mask;
62 #define for_each_tracing_cpu(cpu) \
63 for_each_cpu_mask(cpu, tracing_buffer_mask)
65 static int tracing_disabled = 1;
68 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
70 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
71 * is set, then ftrace_dump is called. This will output the contents
72 * of the ftrace buffers to the console. This is very useful for
73 * capturing traces that lead to crashes and outputing it to a
76 * It is default off, but you can enable it with either specifying
77 * "ftrace_dump_on_oops" in the kernel command line, or setting
78 * /proc/sys/kernel/ftrace_dump_on_oops to true.
80 int ftrace_dump_on_oops;
82 static int tracing_set_tracer(char *buf);
84 static int __init set_ftrace(char *str)
86 tracing_set_tracer(str);
89 __setup("ftrace", set_ftrace);
91 static int __init set_ftrace_dump_on_oops(char *str)
93 ftrace_dump_on_oops = 1;
96 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
99 ns2usecs(cycle_t nsec)
106 cycle_t ftrace_now(int cpu)
108 u64 ts = ring_buffer_time_stamp(cpu);
109 ring_buffer_normalize_time_stamp(cpu, &ts);
114 * The global_trace is the descriptor that holds the tracing
115 * buffers for the live tracing. For each CPU, it contains
116 * a link list of pages that will store trace entries. The
117 * page descriptor of the pages in the memory is used to hold
118 * the link list by linking the lru item in the page descriptor
119 * to each of the pages in the buffer per CPU.
121 * For each active CPU there is a data field that holds the
122 * pages for the buffer for that CPU. Each CPU has the same number
123 * of pages allocated for its buffer.
125 static struct trace_array global_trace;
127 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
130 * The max_tr is used to snapshot the global_trace when a maximum
131 * latency is reached. Some tracers will use this to store a maximum
132 * trace while it continues examining live traces.
134 * The buffers for the max_tr are set up the same as the global_trace.
135 * When a snapshot is taken, the link list of the max_tr is swapped
136 * with the link list of the global_trace and the buffers are reset for
137 * the global_trace so the tracing can continue.
139 static struct trace_array max_tr;
141 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
143 /* tracer_enabled is used to toggle activation of a tracer */
144 static int tracer_enabled = 1;
146 /* function tracing enabled */
147 int ftrace_function_enabled;
150 * trace_buf_size is the size in bytes that is allocated
151 * for a buffer. Note, the number of bytes is always rounded
154 * This number is purposely set to a low number of 16384.
155 * If the dump on oops happens, it will be much appreciated
156 * to not have to wait for all that output. Anyway this can be
157 * boot time and run time configurable.
159 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
161 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
163 /* trace_types holds a link list of available tracers. */
164 static struct tracer *trace_types __read_mostly;
166 /* current_trace points to the tracer that is currently active */
167 static struct tracer *current_trace __read_mostly;
170 * max_tracer_type_len is used to simplify the allocating of
171 * buffers to read userspace tracer names. We keep track of
172 * the longest tracer name registered.
174 static int max_tracer_type_len;
177 * trace_types_lock is used to protect the trace_types list.
178 * This lock is also used to keep user access serialized.
179 * Accesses from userspace will grab this lock while userspace
180 * activities happen inside the kernel.
182 static DEFINE_MUTEX(trace_types_lock);
184 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
185 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
187 /* trace_flags holds iter_ctrl options */
188 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
191 * trace_wake_up - wake up tasks waiting for trace input
193 * Simply wakes up any task that is blocked on the trace_wait
194 * queue. These is used with trace_poll for tasks polling the trace.
196 void trace_wake_up(void)
199 * The runqueue_is_locked() can fail, but this is the best we
202 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
203 wake_up(&trace_wait);
206 static int __init set_buf_size(char *str)
208 unsigned long buf_size;
213 ret = strict_strtoul(str, 0, &buf_size);
214 /* nr_entries can not be zero */
215 if (ret < 0 || buf_size == 0)
217 trace_buf_size = buf_size;
220 __setup("trace_buf_size=", set_buf_size);
222 unsigned long nsecs_to_usecs(unsigned long nsecs)
228 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
229 * control the output of kernel symbols.
231 #define TRACE_ITER_SYM_MASK \
232 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
234 /* These must match the bit postions in trace_iterator_flags */
235 static const char *trace_options[] = {
251 * ftrace_max_lock is used to protect the swapping of buffers
252 * when taking a max snapshot. The buffers themselves are
253 * protected by per_cpu spinlocks. But the action of the swap
254 * needs its own lock.
256 * This is defined as a raw_spinlock_t in order to help
257 * with performance when lockdep debugging is enabled.
259 static raw_spinlock_t ftrace_max_lock =
260 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
263 * Copy the new maximum trace into the separate maximum-trace
264 * structure. (this way the maximum trace is permanently saved,
265 * for later retrieval via /debugfs/tracing/latency_trace)
268 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
270 struct trace_array_cpu *data = tr->data[cpu];
273 max_tr.time_start = data->preempt_timestamp;
275 data = max_tr.data[cpu];
276 data->saved_latency = tracing_max_latency;
278 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
279 data->pid = tsk->pid;
280 data->uid = tsk->uid;
281 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
282 data->policy = tsk->policy;
283 data->rt_priority = tsk->rt_priority;
285 /* record this tasks comm */
286 tracing_record_cmdline(current);
290 * trace_seq_printf - sequence printing of trace information
291 * @s: trace sequence descriptor
292 * @fmt: printf format string
294 * The tracer may use either sequence operations or its own
295 * copy to user routines. To simplify formating of a trace
296 * trace_seq_printf is used to store strings into a special
297 * buffer (@s). Then the output may be either used by
298 * the sequencer or pulled into another buffer.
301 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
303 int len = (PAGE_SIZE - 1) - s->len;
311 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
314 /* If we can't write it all, don't bother writing anything */
324 * trace_seq_puts - trace sequence printing of simple string
325 * @s: trace sequence descriptor
326 * @str: simple string to record
328 * The tracer may use either the sequence operations or its own
329 * copy to user routines. This function records a simple string
330 * into a special buffer (@s) for later retrieval by a sequencer
331 * or other mechanism.
334 trace_seq_puts(struct trace_seq *s, const char *str)
336 int len = strlen(str);
338 if (len > ((PAGE_SIZE - 1) - s->len))
341 memcpy(s->buffer + s->len, str, len);
348 trace_seq_putc(struct trace_seq *s, unsigned char c)
350 if (s->len >= (PAGE_SIZE - 1))
353 s->buffer[s->len++] = c;
359 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
361 if (len > ((PAGE_SIZE - 1) - s->len))
364 memcpy(s->buffer + s->len, mem, len);
370 #define MAX_MEMHEX_BYTES 8
371 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
374 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
376 unsigned char hex[HEX_CHARS];
377 unsigned char *data = mem;
381 for (i = 0, j = 0; i < len; i++) {
383 for (i = len-1, j = 0; i >= 0; i--) {
385 hex[j++] = hex_asc_hi(data[i]);
386 hex[j++] = hex_asc_lo(data[i]);
390 return trace_seq_putmem(s, hex, j);
394 trace_seq_reset(struct trace_seq *s)
400 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
405 if (s->len <= s->readpos)
408 len = s->len - s->readpos;
411 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
420 trace_print_seq(struct seq_file *m, struct trace_seq *s)
422 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
425 seq_puts(m, s->buffer);
431 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
433 * @tsk: the task with the latency
434 * @cpu: The cpu that initiated the trace.
436 * Flip the buffers between the @tr and the max_tr and record information
437 * about which task was the cause of this latency.
440 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
442 struct ring_buffer *buf = tr->buffer;
444 WARN_ON_ONCE(!irqs_disabled());
445 __raw_spin_lock(&ftrace_max_lock);
447 tr->buffer = max_tr.buffer;
450 ftrace_disable_cpu();
451 ring_buffer_reset(tr->buffer);
454 __update_max_tr(tr, tsk, cpu);
455 __raw_spin_unlock(&ftrace_max_lock);
459 * update_max_tr_single - only copy one trace over, and reset the rest
461 * @tsk - task with the latency
462 * @cpu - the cpu of the buffer to copy.
464 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
467 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
471 WARN_ON_ONCE(!irqs_disabled());
472 __raw_spin_lock(&ftrace_max_lock);
474 ftrace_disable_cpu();
476 ring_buffer_reset(max_tr.buffer);
477 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
483 __update_max_tr(tr, tsk, cpu);
484 __raw_spin_unlock(&ftrace_max_lock);
488 * register_tracer - register a tracer with the ftrace system.
489 * @type - the plugin for the tracer
491 * Register a new plugin tracer.
493 int register_tracer(struct tracer *type)
500 pr_info("Tracer must have a name\n");
504 mutex_lock(&trace_types_lock);
505 for (t = trace_types; t; t = t->next) {
506 if (strcmp(type->name, t->name) == 0) {
508 pr_info("Trace %s already registered\n",
515 #ifdef CONFIG_FTRACE_STARTUP_TEST
516 if (type->selftest) {
517 struct tracer *saved_tracer = current_trace;
518 struct trace_array *tr = &global_trace;
519 int saved_ctrl = tr->ctrl;
522 * Run a selftest on this tracer.
523 * Here we reset the trace buffer, and set the current
524 * tracer to be this tracer. The tracer can then run some
525 * internal tracing to verify that everything is in order.
526 * If we fail, we do not register this tracer.
528 for_each_tracing_cpu(i) {
529 tracing_reset(tr, i);
531 current_trace = type;
533 /* the test is responsible for initializing and enabling */
534 pr_info("Testing tracer %s: ", type->name);
535 ret = type->selftest(type, tr);
536 /* the test is responsible for resetting too */
537 current_trace = saved_tracer;
538 tr->ctrl = saved_ctrl;
540 printk(KERN_CONT "FAILED!\n");
543 /* Only reset on passing, to avoid touching corrupted buffers */
544 for_each_tracing_cpu(i) {
545 tracing_reset(tr, i);
547 printk(KERN_CONT "PASSED\n");
551 type->next = trace_types;
553 len = strlen(type->name);
554 if (len > max_tracer_type_len)
555 max_tracer_type_len = len;
558 mutex_unlock(&trace_types_lock);
563 void unregister_tracer(struct tracer *type)
568 mutex_lock(&trace_types_lock);
569 for (t = &trace_types; *t; t = &(*t)->next) {
573 pr_info("Trace %s not registered\n", type->name);
578 if (strlen(type->name) != max_tracer_type_len)
581 max_tracer_type_len = 0;
582 for (t = &trace_types; *t; t = &(*t)->next) {
583 len = strlen((*t)->name);
584 if (len > max_tracer_type_len)
585 max_tracer_type_len = len;
588 mutex_unlock(&trace_types_lock);
591 void tracing_reset(struct trace_array *tr, int cpu)
593 ftrace_disable_cpu();
594 ring_buffer_reset_cpu(tr->buffer, cpu);
598 #define SAVED_CMDLINES 128
599 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
600 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
601 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
602 static int cmdline_idx;
603 static DEFINE_SPINLOCK(trace_cmdline_lock);
605 /* temporary disable recording */
606 atomic_t trace_record_cmdline_disabled __read_mostly;
608 static void trace_init_cmdlines(void)
610 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
611 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
615 void trace_stop_cmdline_recording(void);
617 static void trace_save_cmdline(struct task_struct *tsk)
622 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
626 * It's not the end of the world if we don't get
627 * the lock, but we also don't want to spin
628 * nor do we want to disable interrupts,
629 * so if we miss here, then better luck next time.
631 if (!spin_trylock(&trace_cmdline_lock))
634 idx = map_pid_to_cmdline[tsk->pid];
635 if (idx >= SAVED_CMDLINES) {
636 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
638 map = map_cmdline_to_pid[idx];
639 if (map <= PID_MAX_DEFAULT)
640 map_pid_to_cmdline[map] = (unsigned)-1;
642 map_pid_to_cmdline[tsk->pid] = idx;
647 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
649 spin_unlock(&trace_cmdline_lock);
652 static char *trace_find_cmdline(int pid)
654 char *cmdline = "<...>";
660 if (pid > PID_MAX_DEFAULT)
663 map = map_pid_to_cmdline[pid];
664 if (map >= SAVED_CMDLINES)
667 cmdline = saved_cmdlines[map];
673 void tracing_record_cmdline(struct task_struct *tsk)
675 if (atomic_read(&trace_record_cmdline_disabled))
678 trace_save_cmdline(tsk);
682 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
685 struct task_struct *tsk = current;
687 entry->preempt_count = pc & 0xff;
688 entry->pid = (tsk) ? tsk->pid : 0;
690 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
691 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
693 TRACE_FLAG_IRQS_NOSUPPORT |
695 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
696 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
697 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
701 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
702 unsigned long ip, unsigned long parent_ip, unsigned long flags,
705 struct ring_buffer_event *event;
706 struct ftrace_entry *entry;
707 unsigned long irq_flags;
709 /* If we are reading the ring buffer, don't trace */
710 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
713 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
717 entry = ring_buffer_event_data(event);
718 tracing_generic_entry_update(&entry->ent, flags, pc);
719 entry->ent.type = TRACE_FN;
721 entry->parent_ip = parent_ip;
722 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
726 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
727 unsigned long ip, unsigned long parent_ip, unsigned long flags,
730 if (likely(!atomic_read(&data->disabled)))
731 trace_function(tr, data, ip, parent_ip, flags, pc);
734 static void ftrace_trace_stack(struct trace_array *tr,
735 struct trace_array_cpu *data,
739 struct ring_buffer_event *event;
740 struct stack_entry *entry;
741 struct stack_trace trace;
742 unsigned long irq_flags;
744 if (!(trace_flags & TRACE_ITER_STACKTRACE))
747 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
751 entry = ring_buffer_event_data(event);
752 tracing_generic_entry_update(&entry->ent, flags, pc);
753 entry->ent.type = TRACE_STACK;
755 memset(&entry->caller, 0, sizeof(entry->caller));
757 trace.nr_entries = 0;
758 trace.max_entries = FTRACE_STACK_ENTRIES;
760 trace.entries = entry->caller;
762 save_stack_trace(&trace);
763 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
766 void __trace_stack(struct trace_array *tr,
767 struct trace_array_cpu *data,
771 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
775 ftrace_trace_special(void *__tr, void *__data,
776 unsigned long arg1, unsigned long arg2, unsigned long arg3,
779 struct ring_buffer_event *event;
780 struct trace_array_cpu *data = __data;
781 struct trace_array *tr = __tr;
782 struct special_entry *entry;
783 unsigned long irq_flags;
785 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
789 entry = ring_buffer_event_data(event);
790 tracing_generic_entry_update(&entry->ent, 0, pc);
791 entry->ent.type = TRACE_SPECIAL;
795 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
796 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
802 __trace_special(void *__tr, void *__data,
803 unsigned long arg1, unsigned long arg2, unsigned long arg3)
805 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
809 tracing_sched_switch_trace(struct trace_array *tr,
810 struct trace_array_cpu *data,
811 struct task_struct *prev,
812 struct task_struct *next,
813 unsigned long flags, int pc)
815 struct ring_buffer_event *event;
816 struct ctx_switch_entry *entry;
817 unsigned long irq_flags;
819 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
823 entry = ring_buffer_event_data(event);
824 tracing_generic_entry_update(&entry->ent, flags, pc);
825 entry->ent.type = TRACE_CTX;
826 entry->prev_pid = prev->pid;
827 entry->prev_prio = prev->prio;
828 entry->prev_state = prev->state;
829 entry->next_pid = next->pid;
830 entry->next_prio = next->prio;
831 entry->next_state = next->state;
832 entry->next_cpu = task_cpu(next);
833 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
834 ftrace_trace_stack(tr, data, flags, 5, pc);
838 tracing_sched_wakeup_trace(struct trace_array *tr,
839 struct trace_array_cpu *data,
840 struct task_struct *wakee,
841 struct task_struct *curr,
842 unsigned long flags, int pc)
844 struct ring_buffer_event *event;
845 struct ctx_switch_entry *entry;
846 unsigned long irq_flags;
848 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
852 entry = ring_buffer_event_data(event);
853 tracing_generic_entry_update(&entry->ent, flags, pc);
854 entry->ent.type = TRACE_WAKE;
855 entry->prev_pid = curr->pid;
856 entry->prev_prio = curr->prio;
857 entry->prev_state = curr->state;
858 entry->next_pid = wakee->pid;
859 entry->next_prio = wakee->prio;
860 entry->next_state = wakee->state;
861 entry->next_cpu = task_cpu(wakee);
862 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
863 ftrace_trace_stack(tr, data, flags, 6, pc);
869 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
871 struct trace_array *tr = &global_trace;
872 struct trace_array_cpu *data;
876 if (tracing_disabled || !tr->ctrl)
879 pc = preempt_count();
880 preempt_disable_notrace();
881 cpu = raw_smp_processor_id();
882 data = tr->data[cpu];
884 if (likely(!atomic_read(&data->disabled)))
885 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
887 preempt_enable_notrace();
890 #ifdef CONFIG_FUNCTION_TRACER
892 function_trace_call(unsigned long ip, unsigned long parent_ip)
894 struct trace_array *tr = &global_trace;
895 struct trace_array_cpu *data;
901 if (unlikely(!ftrace_function_enabled))
904 pc = preempt_count();
905 resched = need_resched();
906 preempt_disable_notrace();
907 local_save_flags(flags);
908 cpu = raw_smp_processor_id();
909 data = tr->data[cpu];
910 disabled = atomic_inc_return(&data->disabled);
912 if (likely(disabled == 1))
913 trace_function(tr, data, ip, parent_ip, flags, pc);
915 atomic_dec(&data->disabled);
917 preempt_enable_no_resched_notrace();
919 preempt_enable_notrace();
922 static struct ftrace_ops trace_ops __read_mostly =
924 .func = function_trace_call,
927 void tracing_start_function_trace(void)
929 ftrace_function_enabled = 0;
930 register_ftrace_function(&trace_ops);
932 ftrace_function_enabled = 1;
935 void tracing_stop_function_trace(void)
937 ftrace_function_enabled = 0;
938 unregister_ftrace_function(&trace_ops);
942 enum trace_file_type {
943 TRACE_FILE_LAT_FMT = 1,
946 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
948 /* Don't allow ftrace to trace into the ring buffers */
949 ftrace_disable_cpu();
952 if (iter->buffer_iter[iter->cpu])
953 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
958 static struct trace_entry *
959 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
961 struct ring_buffer_event *event;
962 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
964 /* Don't allow ftrace to trace into the ring buffers */
965 ftrace_disable_cpu();
968 event = ring_buffer_iter_peek(buf_iter, ts);
970 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
974 return event ? ring_buffer_event_data(event) : NULL;
977 static struct trace_entry *
978 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
980 struct ring_buffer *buffer = iter->tr->buffer;
981 struct trace_entry *ent, *next = NULL;
986 for_each_tracing_cpu(cpu) {
988 if (ring_buffer_empty_cpu(buffer, cpu))
991 ent = peek_next_entry(iter, cpu, &ts);
994 * Pick the entry with the smallest timestamp:
996 if (ent && (!next || ts < next_ts)) {
1004 *ent_cpu = next_cpu;
1012 /* Find the next real entry, without updating the iterator itself */
1013 static struct trace_entry *
1014 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1016 return __find_next_entry(iter, ent_cpu, ent_ts);
1019 /* Find the next real entry, and increment the iterator to the next entry */
1020 static void *find_next_entry_inc(struct trace_iterator *iter)
1022 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1025 trace_iterator_increment(iter, iter->cpu);
1027 return iter->ent ? iter : NULL;
1030 static void trace_consume(struct trace_iterator *iter)
1032 /* Don't allow ftrace to trace into the ring buffers */
1033 ftrace_disable_cpu();
1034 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1035 ftrace_enable_cpu();
1038 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1040 struct trace_iterator *iter = m->private;
1046 /* can't go backwards */
1051 ent = find_next_entry_inc(iter);
1055 while (ent && iter->idx < i)
1056 ent = find_next_entry_inc(iter);
1063 static void *s_start(struct seq_file *m, loff_t *pos)
1065 struct trace_iterator *iter = m->private;
1070 mutex_lock(&trace_types_lock);
1072 if (!current_trace || current_trace != iter->trace) {
1073 mutex_unlock(&trace_types_lock);
1077 atomic_inc(&trace_record_cmdline_disabled);
1079 /* let the tracer grab locks here if needed */
1080 if (current_trace->start)
1081 current_trace->start(iter);
1083 if (*pos != iter->pos) {
1088 ftrace_disable_cpu();
1090 for_each_tracing_cpu(cpu) {
1091 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1094 ftrace_enable_cpu();
1096 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1101 p = s_next(m, p, &l);
1107 static void s_stop(struct seq_file *m, void *p)
1109 struct trace_iterator *iter = m->private;
1111 atomic_dec(&trace_record_cmdline_disabled);
1113 /* let the tracer release locks here if needed */
1114 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1115 iter->trace->stop(iter);
1117 mutex_unlock(&trace_types_lock);
1120 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1122 #ifdef CONFIG_KRETPROBES
1123 static inline int kretprobed(unsigned long addr)
1125 return addr == (unsigned long)kretprobe_trampoline;
1128 static inline int kretprobed(unsigned long addr)
1132 #endif /* CONFIG_KRETPROBES */
1135 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1137 #ifdef CONFIG_KALLSYMS
1138 char str[KSYM_SYMBOL_LEN];
1140 kallsyms_lookup(address, NULL, NULL, NULL, str);
1142 return trace_seq_printf(s, fmt, str);
1148 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1149 unsigned long address)
1151 #ifdef CONFIG_KALLSYMS
1152 char str[KSYM_SYMBOL_LEN];
1154 sprint_symbol(str, address);
1155 return trace_seq_printf(s, fmt, str);
1160 #ifndef CONFIG_64BIT
1161 # define IP_FMT "%08lx"
1163 # define IP_FMT "%016lx"
1167 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1172 return trace_seq_printf(s, "0");
1174 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1175 ret = seq_print_sym_offset(s, "%s", ip);
1177 ret = seq_print_sym_short(s, "%s", ip);
1182 if (sym_flags & TRACE_ITER_SYM_ADDR)
1183 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1187 static void print_lat_help_header(struct seq_file *m)
1189 seq_puts(m, "# _------=> CPU# \n");
1190 seq_puts(m, "# / _-----=> irqs-off \n");
1191 seq_puts(m, "# | / _----=> need-resched \n");
1192 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1193 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1194 seq_puts(m, "# |||| / \n");
1195 seq_puts(m, "# ||||| delay \n");
1196 seq_puts(m, "# cmd pid ||||| time | caller \n");
1197 seq_puts(m, "# \\ / ||||| \\ | / \n");
1200 static void print_func_help_header(struct seq_file *m)
1202 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1203 seq_puts(m, "# | | | | |\n");
1208 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1210 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1211 struct trace_array *tr = iter->tr;
1212 struct trace_array_cpu *data = tr->data[tr->cpu];
1213 struct tracer *type = current_trace;
1214 unsigned long total;
1215 unsigned long entries;
1216 const char *name = "preemption";
1221 entries = ring_buffer_entries(iter->tr->buffer);
1223 ring_buffer_overruns(iter->tr->buffer);
1225 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1227 seq_puts(m, "-----------------------------------"
1228 "---------------------------------\n");
1229 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1230 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1231 nsecs_to_usecs(data->saved_latency),
1235 #if defined(CONFIG_PREEMPT_NONE)
1237 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1239 #elif defined(CONFIG_PREEMPT)
1244 /* These are reserved for later use */
1247 seq_printf(m, " #P:%d)\n", num_online_cpus());
1251 seq_puts(m, " -----------------\n");
1252 seq_printf(m, " | task: %.16s-%d "
1253 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1254 data->comm, data->pid, data->uid, data->nice,
1255 data->policy, data->rt_priority);
1256 seq_puts(m, " -----------------\n");
1258 if (data->critical_start) {
1259 seq_puts(m, " => started at: ");
1260 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1261 trace_print_seq(m, &iter->seq);
1262 seq_puts(m, "\n => ended at: ");
1263 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1264 trace_print_seq(m, &iter->seq);
1272 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1274 int hardirq, softirq;
1277 comm = trace_find_cmdline(entry->pid);
1279 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1280 trace_seq_printf(s, "%3d", cpu);
1281 trace_seq_printf(s, "%c%c",
1282 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1283 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1284 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1286 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1287 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1288 if (hardirq && softirq) {
1289 trace_seq_putc(s, 'H');
1292 trace_seq_putc(s, 'h');
1295 trace_seq_putc(s, 's');
1297 trace_seq_putc(s, '.');
1301 if (entry->preempt_count)
1302 trace_seq_printf(s, "%x", entry->preempt_count);
1304 trace_seq_puts(s, ".");
1307 unsigned long preempt_mark_thresh = 100;
1310 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1311 unsigned long rel_usecs)
1313 trace_seq_printf(s, " %4lldus", abs_usecs);
1314 if (rel_usecs > preempt_mark_thresh)
1315 trace_seq_puts(s, "!: ");
1316 else if (rel_usecs > 1)
1317 trace_seq_puts(s, "+: ");
1319 trace_seq_puts(s, " : ");
1322 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1325 * The message is supposed to contain an ending newline.
1326 * If the printing stops prematurely, try to add a newline of our own.
1328 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1330 struct trace_entry *ent;
1331 struct trace_field_cont *cont;
1334 ent = peek_next_entry(iter, iter->cpu, NULL);
1335 if (!ent || ent->type != TRACE_CONT) {
1336 trace_seq_putc(s, '\n');
1341 cont = (struct trace_field_cont *)ent;
1343 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1345 ftrace_disable_cpu();
1347 if (iter->buffer_iter[iter->cpu])
1348 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1350 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1352 ftrace_enable_cpu();
1354 ent = peek_next_entry(iter, iter->cpu, NULL);
1355 } while (ent && ent->type == TRACE_CONT);
1358 trace_seq_putc(s, '\n');
1361 static enum print_line_t
1362 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1364 struct trace_seq *s = &iter->seq;
1365 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1366 struct trace_entry *next_entry;
1367 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1368 struct trace_entry *entry = iter->ent;
1369 unsigned long abs_usecs;
1370 unsigned long rel_usecs;
1377 if (entry->type == TRACE_CONT)
1378 return TRACE_TYPE_HANDLED;
1380 next_entry = find_next_entry(iter, NULL, &next_ts);
1383 rel_usecs = ns2usecs(next_ts - iter->ts);
1384 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1387 comm = trace_find_cmdline(entry->pid);
1388 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1389 " %ld.%03ldms (+%ld.%03ldms): ",
1391 entry->pid, cpu, entry->flags,
1392 entry->preempt_count, trace_idx,
1395 abs_usecs % 1000, rel_usecs/1000,
1398 lat_print_generic(s, entry, cpu);
1399 lat_print_timestamp(s, abs_usecs, rel_usecs);
1401 switch (entry->type) {
1403 struct ftrace_entry *field;
1405 trace_assign_type(field, entry);
1407 seq_print_ip_sym(s, field->ip, sym_flags);
1408 trace_seq_puts(s, " (");
1409 if (kretprobed(field->parent_ip))
1410 trace_seq_puts(s, KRETPROBE_MSG);
1412 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1413 trace_seq_puts(s, ")\n");
1418 struct ctx_switch_entry *field;
1420 trace_assign_type(field, entry);
1422 T = field->next_state < sizeof(state_to_char) ?
1423 state_to_char[field->next_state] : 'X';
1425 state = field->prev_state ?
1426 __ffs(field->prev_state) + 1 : 0;
1427 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1428 comm = trace_find_cmdline(field->next_pid);
1429 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1432 S, entry->type == TRACE_CTX ? "==>" : " +",
1439 case TRACE_SPECIAL: {
1440 struct special_entry *field;
1442 trace_assign_type(field, entry);
1444 trace_seq_printf(s, "# %ld %ld %ld\n",
1451 struct stack_entry *field;
1453 trace_assign_type(field, entry);
1455 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1457 trace_seq_puts(s, " <= ");
1458 seq_print_ip_sym(s, field->caller[i], sym_flags);
1460 trace_seq_puts(s, "\n");
1464 struct print_entry *field;
1466 trace_assign_type(field, entry);
1468 seq_print_ip_sym(s, field->ip, sym_flags);
1469 trace_seq_printf(s, ": %s", field->buf);
1470 if (entry->flags & TRACE_FLAG_CONT)
1471 trace_seq_print_cont(s, iter);
1475 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1477 return TRACE_TYPE_HANDLED;
1480 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1482 struct trace_seq *s = &iter->seq;
1483 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1484 struct trace_entry *entry;
1485 unsigned long usec_rem;
1486 unsigned long long t;
1495 if (entry->type == TRACE_CONT)
1496 return TRACE_TYPE_HANDLED;
1498 comm = trace_find_cmdline(iter->ent->pid);
1500 t = ns2usecs(iter->ts);
1501 usec_rem = do_div(t, 1000000ULL);
1502 secs = (unsigned long)t;
1504 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1506 return TRACE_TYPE_PARTIAL_LINE;
1507 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1509 return TRACE_TYPE_PARTIAL_LINE;
1510 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1512 return TRACE_TYPE_PARTIAL_LINE;
1514 switch (entry->type) {
1516 struct ftrace_entry *field;
1518 trace_assign_type(field, entry);
1520 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1522 return TRACE_TYPE_PARTIAL_LINE;
1523 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1525 ret = trace_seq_printf(s, " <-");
1527 return TRACE_TYPE_PARTIAL_LINE;
1528 if (kretprobed(field->parent_ip))
1529 ret = trace_seq_puts(s, KRETPROBE_MSG);
1531 ret = seq_print_ip_sym(s,
1535 return TRACE_TYPE_PARTIAL_LINE;
1537 ret = trace_seq_printf(s, "\n");
1539 return TRACE_TYPE_PARTIAL_LINE;
1544 struct ctx_switch_entry *field;
1546 trace_assign_type(field, entry);
1548 S = field->prev_state < sizeof(state_to_char) ?
1549 state_to_char[field->prev_state] : 'X';
1550 T = field->next_state < sizeof(state_to_char) ?
1551 state_to_char[field->next_state] : 'X';
1552 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1556 entry->type == TRACE_CTX ? "==>" : " +",
1562 return TRACE_TYPE_PARTIAL_LINE;
1565 case TRACE_SPECIAL: {
1566 struct special_entry *field;
1568 trace_assign_type(field, entry);
1570 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1575 return TRACE_TYPE_PARTIAL_LINE;
1579 struct stack_entry *field;
1581 trace_assign_type(field, entry);
1583 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1585 ret = trace_seq_puts(s, " <= ");
1587 return TRACE_TYPE_PARTIAL_LINE;
1589 ret = seq_print_ip_sym(s, field->caller[i],
1592 return TRACE_TYPE_PARTIAL_LINE;
1594 ret = trace_seq_puts(s, "\n");
1596 return TRACE_TYPE_PARTIAL_LINE;
1600 struct print_entry *field;
1602 trace_assign_type(field, entry);
1604 seq_print_ip_sym(s, field->ip, sym_flags);
1605 trace_seq_printf(s, ": %s", field->buf);
1606 if (entry->flags & TRACE_FLAG_CONT)
1607 trace_seq_print_cont(s, iter);
1611 return TRACE_TYPE_HANDLED;
1614 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1616 struct trace_seq *s = &iter->seq;
1617 struct trace_entry *entry;
1623 if (entry->type == TRACE_CONT)
1624 return TRACE_TYPE_HANDLED;
1626 ret = trace_seq_printf(s, "%d %d %llu ",
1627 entry->pid, iter->cpu, iter->ts);
1629 return TRACE_TYPE_PARTIAL_LINE;
1631 switch (entry->type) {
1633 struct ftrace_entry *field;
1635 trace_assign_type(field, entry);
1637 ret = trace_seq_printf(s, "%x %x\n",
1641 return TRACE_TYPE_PARTIAL_LINE;
1646 struct ctx_switch_entry *field;
1648 trace_assign_type(field, entry);
1650 S = field->prev_state < sizeof(state_to_char) ?
1651 state_to_char[field->prev_state] : 'X';
1652 T = field->next_state < sizeof(state_to_char) ?
1653 state_to_char[field->next_state] : 'X';
1654 if (entry->type == TRACE_WAKE)
1656 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
1665 return TRACE_TYPE_PARTIAL_LINE;
1670 struct special_entry *field;
1672 trace_assign_type(field, entry);
1674 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1679 return TRACE_TYPE_PARTIAL_LINE;
1683 struct print_entry *field;
1685 trace_assign_type(field, entry);
1687 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
1688 if (entry->flags & TRACE_FLAG_CONT)
1689 trace_seq_print_cont(s, iter);
1693 return TRACE_TYPE_HANDLED;
1696 #define SEQ_PUT_FIELD_RET(s, x) \
1698 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1702 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1704 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
1705 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1709 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1711 struct trace_seq *s = &iter->seq;
1712 unsigned char newline = '\n';
1713 struct trace_entry *entry;
1718 if (entry->type == TRACE_CONT)
1719 return TRACE_TYPE_HANDLED;
1721 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1722 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1723 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1725 switch (entry->type) {
1727 struct ftrace_entry *field;
1729 trace_assign_type(field, entry);
1731 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1732 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
1737 struct ctx_switch_entry *field;
1739 trace_assign_type(field, entry);
1741 S = field->prev_state < sizeof(state_to_char) ?
1742 state_to_char[field->prev_state] : 'X';
1743 T = field->next_state < sizeof(state_to_char) ?
1744 state_to_char[field->next_state] : 'X';
1745 if (entry->type == TRACE_WAKE)
1747 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1748 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
1749 SEQ_PUT_HEX_FIELD_RET(s, S);
1750 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1751 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1752 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
1753 SEQ_PUT_HEX_FIELD_RET(s, T);
1758 struct special_entry *field;
1760 trace_assign_type(field, entry);
1762 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
1763 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
1764 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
1768 SEQ_PUT_FIELD_RET(s, newline);
1770 return TRACE_TYPE_HANDLED;
1773 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1775 struct trace_seq *s = &iter->seq;
1776 struct trace_entry *entry;
1780 if (entry->type == TRACE_CONT)
1781 return TRACE_TYPE_HANDLED;
1783 SEQ_PUT_FIELD_RET(s, entry->pid);
1784 SEQ_PUT_FIELD_RET(s, iter->cpu);
1785 SEQ_PUT_FIELD_RET(s, iter->ts);
1787 switch (entry->type) {
1789 struct ftrace_entry *field;
1791 trace_assign_type(field, entry);
1793 SEQ_PUT_FIELD_RET(s, field->ip);
1794 SEQ_PUT_FIELD_RET(s, field->parent_ip);
1798 struct ctx_switch_entry *field;
1800 trace_assign_type(field, entry);
1802 SEQ_PUT_FIELD_RET(s, field->prev_pid);
1803 SEQ_PUT_FIELD_RET(s, field->prev_prio);
1804 SEQ_PUT_FIELD_RET(s, field->prev_state);
1805 SEQ_PUT_FIELD_RET(s, field->next_pid);
1806 SEQ_PUT_FIELD_RET(s, field->next_prio);
1807 SEQ_PUT_FIELD_RET(s, field->next_state);
1812 struct special_entry *field;
1814 trace_assign_type(field, entry);
1816 SEQ_PUT_FIELD_RET(s, field->arg1);
1817 SEQ_PUT_FIELD_RET(s, field->arg2);
1818 SEQ_PUT_FIELD_RET(s, field->arg3);
1825 static int trace_empty(struct trace_iterator *iter)
1829 for_each_tracing_cpu(cpu) {
1830 if (iter->buffer_iter[cpu]) {
1831 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1834 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1842 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1844 enum print_line_t ret;
1846 if (iter->trace && iter->trace->print_line) {
1847 ret = iter->trace->print_line(iter);
1848 if (ret != TRACE_TYPE_UNHANDLED)
1852 if (trace_flags & TRACE_ITER_BIN)
1853 return print_bin_fmt(iter);
1855 if (trace_flags & TRACE_ITER_HEX)
1856 return print_hex_fmt(iter);
1858 if (trace_flags & TRACE_ITER_RAW)
1859 return print_raw_fmt(iter);
1861 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1862 return print_lat_fmt(iter, iter->idx, iter->cpu);
1864 return print_trace_fmt(iter);
1867 static int s_show(struct seq_file *m, void *v)
1869 struct trace_iterator *iter = v;
1871 if (iter->ent == NULL) {
1873 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1876 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1877 /* print nothing if the buffers are empty */
1878 if (trace_empty(iter))
1880 print_trace_header(m, iter);
1881 if (!(trace_flags & TRACE_ITER_VERBOSE))
1882 print_lat_help_header(m);
1884 if (!(trace_flags & TRACE_ITER_VERBOSE))
1885 print_func_help_header(m);
1888 print_trace_line(iter);
1889 trace_print_seq(m, &iter->seq);
1895 static struct seq_operations tracer_seq_ops = {
1902 static struct trace_iterator *
1903 __tracing_open(struct inode *inode, struct file *file, int *ret)
1905 struct trace_iterator *iter;
1909 if (tracing_disabled) {
1914 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1920 mutex_lock(&trace_types_lock);
1921 if (current_trace && current_trace->print_max)
1924 iter->tr = inode->i_private;
1925 iter->trace = current_trace;
1928 for_each_tracing_cpu(cpu) {
1930 iter->buffer_iter[cpu] =
1931 ring_buffer_read_start(iter->tr->buffer, cpu);
1933 if (!iter->buffer_iter[cpu])
1937 /* TODO stop tracer */
1938 *ret = seq_open(file, &tracer_seq_ops);
1942 m = file->private_data;
1945 /* stop the trace while dumping */
1946 if (iter->tr->ctrl) {
1948 ftrace_function_enabled = 0;
1951 if (iter->trace && iter->trace->open)
1952 iter->trace->open(iter);
1954 mutex_unlock(&trace_types_lock);
1960 for_each_tracing_cpu(cpu) {
1961 if (iter->buffer_iter[cpu])
1962 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1964 mutex_unlock(&trace_types_lock);
1966 return ERR_PTR(-ENOMEM);
1969 int tracing_open_generic(struct inode *inode, struct file *filp)
1971 if (tracing_disabled)
1974 filp->private_data = inode->i_private;
1978 int tracing_release(struct inode *inode, struct file *file)
1980 struct seq_file *m = (struct seq_file *)file->private_data;
1981 struct trace_iterator *iter = m->private;
1984 mutex_lock(&trace_types_lock);
1985 for_each_tracing_cpu(cpu) {
1986 if (iter->buffer_iter[cpu])
1987 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1990 if (iter->trace && iter->trace->close)
1991 iter->trace->close(iter);
1993 /* reenable tracing if it was previously enabled */
1994 if (iter->tr->ctrl) {
1997 * It is safe to enable function tracing even if it
2000 ftrace_function_enabled = 1;
2002 mutex_unlock(&trace_types_lock);
2004 seq_release(inode, file);
2009 static int tracing_open(struct inode *inode, struct file *file)
2013 __tracing_open(inode, file, &ret);
2018 static int tracing_lt_open(struct inode *inode, struct file *file)
2020 struct trace_iterator *iter;
2023 iter = __tracing_open(inode, file, &ret);
2026 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2033 t_next(struct seq_file *m, void *v, loff_t *pos)
2035 struct tracer *t = m->private;
2047 static void *t_start(struct seq_file *m, loff_t *pos)
2049 struct tracer *t = m->private;
2052 mutex_lock(&trace_types_lock);
2053 for (; t && l < *pos; t = t_next(m, t, &l))
2059 static void t_stop(struct seq_file *m, void *p)
2061 mutex_unlock(&trace_types_lock);
2064 static int t_show(struct seq_file *m, void *v)
2066 struct tracer *t = v;
2071 seq_printf(m, "%s", t->name);
2080 static struct seq_operations show_traces_seq_ops = {
2087 static int show_traces_open(struct inode *inode, struct file *file)
2091 if (tracing_disabled)
2094 ret = seq_open(file, &show_traces_seq_ops);
2096 struct seq_file *m = file->private_data;
2097 m->private = trace_types;
2103 static struct file_operations tracing_fops = {
2104 .open = tracing_open,
2106 .llseek = seq_lseek,
2107 .release = tracing_release,
2110 static struct file_operations tracing_lt_fops = {
2111 .open = tracing_lt_open,
2113 .llseek = seq_lseek,
2114 .release = tracing_release,
2117 static struct file_operations show_traces_fops = {
2118 .open = show_traces_open,
2120 .release = seq_release,
2124 * Only trace on a CPU if the bitmask is set:
2126 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2129 * When tracing/tracing_cpu_mask is modified then this holds
2130 * the new bitmask we are about to install:
2132 static cpumask_t tracing_cpumask_new;
2135 * The tracer itself will not take this lock, but still we want
2136 * to provide a consistent cpumask to user-space:
2138 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2141 * Temporary storage for the character representation of the
2142 * CPU bitmask (and one more byte for the newline):
2144 static char mask_str[NR_CPUS + 1];
2147 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2148 size_t count, loff_t *ppos)
2152 mutex_lock(&tracing_cpumask_update_lock);
2154 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2155 if (count - len < 2) {
2159 len += sprintf(mask_str + len, "\n");
2160 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2163 mutex_unlock(&tracing_cpumask_update_lock);
2169 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2170 size_t count, loff_t *ppos)
2174 mutex_lock(&tracing_cpumask_update_lock);
2175 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2179 raw_local_irq_disable();
2180 __raw_spin_lock(&ftrace_max_lock);
2181 for_each_tracing_cpu(cpu) {
2183 * Increase/decrease the disabled counter if we are
2184 * about to flip a bit in the cpumask:
2186 if (cpu_isset(cpu, tracing_cpumask) &&
2187 !cpu_isset(cpu, tracing_cpumask_new)) {
2188 atomic_inc(&global_trace.data[cpu]->disabled);
2190 if (!cpu_isset(cpu, tracing_cpumask) &&
2191 cpu_isset(cpu, tracing_cpumask_new)) {
2192 atomic_dec(&global_trace.data[cpu]->disabled);
2195 __raw_spin_unlock(&ftrace_max_lock);
2196 raw_local_irq_enable();
2198 tracing_cpumask = tracing_cpumask_new;
2200 mutex_unlock(&tracing_cpumask_update_lock);
2205 mutex_unlock(&tracing_cpumask_update_lock);
2210 static struct file_operations tracing_cpumask_fops = {
2211 .open = tracing_open_generic,
2212 .read = tracing_cpumask_read,
2213 .write = tracing_cpumask_write,
2217 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2218 size_t cnt, loff_t *ppos)
2225 /* calulate max size */
2226 for (i = 0; trace_options[i]; i++) {
2227 len += strlen(trace_options[i]);
2228 len += 3; /* "no" and space */
2231 /* +2 for \n and \0 */
2232 buf = kmalloc(len + 2, GFP_KERNEL);
2236 for (i = 0; trace_options[i]; i++) {
2237 if (trace_flags & (1 << i))
2238 r += sprintf(buf + r, "%s ", trace_options[i]);
2240 r += sprintf(buf + r, "no%s ", trace_options[i]);
2243 r += sprintf(buf + r, "\n");
2244 WARN_ON(r >= len + 2);
2246 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2254 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2255 size_t cnt, loff_t *ppos)
2262 if (cnt >= sizeof(buf))
2265 if (copy_from_user(&buf, ubuf, cnt))
2270 if (strncmp(buf, "no", 2) == 0) {
2275 for (i = 0; trace_options[i]; i++) {
2276 int len = strlen(trace_options[i]);
2278 if (strncmp(cmp, trace_options[i], len) == 0) {
2280 trace_flags &= ~(1 << i);
2282 trace_flags |= (1 << i);
2287 * If no option could be set, return an error:
2289 if (!trace_options[i])
2297 static struct file_operations tracing_iter_fops = {
2298 .open = tracing_open_generic,
2299 .read = tracing_iter_ctrl_read,
2300 .write = tracing_iter_ctrl_write,
2303 static const char readme_msg[] =
2304 "tracing mini-HOWTO:\n\n"
2306 "# mount -t debugfs nodev /debug\n\n"
2307 "# cat /debug/tracing/available_tracers\n"
2308 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2309 "# cat /debug/tracing/current_tracer\n"
2311 "# echo sched_switch > /debug/tracing/current_tracer\n"
2312 "# cat /debug/tracing/current_tracer\n"
2314 "# cat /debug/tracing/iter_ctrl\n"
2315 "noprint-parent nosym-offset nosym-addr noverbose\n"
2316 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2317 "# echo 1 > /debug/tracing/tracing_enabled\n"
2318 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2319 "echo 0 > /debug/tracing/tracing_enabled\n"
2323 tracing_readme_read(struct file *filp, char __user *ubuf,
2324 size_t cnt, loff_t *ppos)
2326 return simple_read_from_buffer(ubuf, cnt, ppos,
2327 readme_msg, strlen(readme_msg));
2330 static struct file_operations tracing_readme_fops = {
2331 .open = tracing_open_generic,
2332 .read = tracing_readme_read,
2336 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2337 size_t cnt, loff_t *ppos)
2339 struct trace_array *tr = filp->private_data;
2343 r = sprintf(buf, "%ld\n", tr->ctrl);
2344 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2348 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2349 size_t cnt, loff_t *ppos)
2351 struct trace_array *tr = filp->private_data;
2356 if (cnt >= sizeof(buf))
2359 if (copy_from_user(&buf, ubuf, cnt))
2364 ret = strict_strtoul(buf, 10, &val);
2370 mutex_lock(&trace_types_lock);
2371 if (tr->ctrl ^ val) {
2379 if (current_trace && current_trace->ctrl_update)
2380 current_trace->ctrl_update(tr);
2382 mutex_unlock(&trace_types_lock);
2390 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2391 size_t cnt, loff_t *ppos)
2393 char buf[max_tracer_type_len+2];
2396 mutex_lock(&trace_types_lock);
2398 r = sprintf(buf, "%s\n", current_trace->name);
2400 r = sprintf(buf, "\n");
2401 mutex_unlock(&trace_types_lock);
2403 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2406 static int tracing_set_tracer(char *buf)
2408 struct trace_array *tr = &global_trace;
2412 mutex_lock(&trace_types_lock);
2413 for (t = trace_types; t; t = t->next) {
2414 if (strcmp(t->name, buf) == 0)
2421 if (t == current_trace)
2424 if (current_trace && current_trace->reset)
2425 current_trace->reset(tr);
2432 mutex_unlock(&trace_types_lock);
2438 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2439 size_t cnt, loff_t *ppos)
2441 char buf[max_tracer_type_len+1];
2445 if (cnt > max_tracer_type_len)
2446 cnt = max_tracer_type_len;
2448 if (copy_from_user(&buf, ubuf, cnt))
2453 /* strip ending whitespace. */
2454 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2457 ret = tracing_set_tracer(buf);
2468 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2469 size_t cnt, loff_t *ppos)
2471 unsigned long *ptr = filp->private_data;
2475 r = snprintf(buf, sizeof(buf), "%ld\n",
2476 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2477 if (r > sizeof(buf))
2479 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2483 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2484 size_t cnt, loff_t *ppos)
2486 long *ptr = filp->private_data;
2491 if (cnt >= sizeof(buf))
2494 if (copy_from_user(&buf, ubuf, cnt))
2499 ret = strict_strtoul(buf, 10, &val);
2508 static atomic_t tracing_reader;
2510 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2512 struct trace_iterator *iter;
2514 if (tracing_disabled)
2517 /* We only allow for reader of the pipe */
2518 if (atomic_inc_return(&tracing_reader) != 1) {
2519 atomic_dec(&tracing_reader);
2523 /* create a buffer to store the information to pass to userspace */
2524 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2528 mutex_lock(&trace_types_lock);
2529 iter->tr = &global_trace;
2530 iter->trace = current_trace;
2531 filp->private_data = iter;
2533 if (iter->trace->pipe_open)
2534 iter->trace->pipe_open(iter);
2535 mutex_unlock(&trace_types_lock);
2540 static int tracing_release_pipe(struct inode *inode, struct file *file)
2542 struct trace_iterator *iter = file->private_data;
2545 atomic_dec(&tracing_reader);
2551 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2553 struct trace_iterator *iter = filp->private_data;
2555 if (trace_flags & TRACE_ITER_BLOCK) {
2557 * Always select as readable when in blocking mode
2559 return POLLIN | POLLRDNORM;
2561 if (!trace_empty(iter))
2562 return POLLIN | POLLRDNORM;
2563 poll_wait(filp, &trace_wait, poll_table);
2564 if (!trace_empty(iter))
2565 return POLLIN | POLLRDNORM;
2575 tracing_read_pipe(struct file *filp, char __user *ubuf,
2576 size_t cnt, loff_t *ppos)
2578 struct trace_iterator *iter = filp->private_data;
2581 /* return any leftover data */
2582 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2586 trace_seq_reset(&iter->seq);
2588 mutex_lock(&trace_types_lock);
2589 if (iter->trace->read) {
2590 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2597 while (trace_empty(iter)) {
2599 if ((filp->f_flags & O_NONBLOCK)) {
2605 * This is a make-shift waitqueue. The reason we don't use
2606 * an actual wait queue is because:
2607 * 1) we only ever have one waiter
2608 * 2) the tracing, traces all functions, we don't want
2609 * the overhead of calling wake_up and friends
2610 * (and tracing them too)
2611 * Anyway, this is really very primitive wakeup.
2613 set_current_state(TASK_INTERRUPTIBLE);
2614 iter->tr->waiter = current;
2616 mutex_unlock(&trace_types_lock);
2618 /* sleep for 100 msecs, and try again. */
2619 schedule_timeout(HZ/10);
2621 mutex_lock(&trace_types_lock);
2623 iter->tr->waiter = NULL;
2625 if (signal_pending(current)) {
2630 if (iter->trace != current_trace)
2634 * We block until we read something and tracing is disabled.
2635 * We still block if tracing is disabled, but we have never
2636 * read anything. This allows a user to cat this file, and
2637 * then enable tracing. But after we have read something,
2638 * we give an EOF when tracing is again disabled.
2640 * iter->pos will be 0 if we haven't read anything.
2642 if (!tracer_enabled && iter->pos)
2648 /* stop when tracing is finished */
2649 if (trace_empty(iter))
2652 if (cnt >= PAGE_SIZE)
2653 cnt = PAGE_SIZE - 1;
2655 /* reset all but tr, trace, and overruns */
2656 memset(&iter->seq, 0,
2657 sizeof(struct trace_iterator) -
2658 offsetof(struct trace_iterator, seq));
2661 while (find_next_entry_inc(iter) != NULL) {
2662 enum print_line_t ret;
2663 int len = iter->seq.len;
2665 ret = print_trace_line(iter);
2666 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2667 /* don't print partial lines */
2668 iter->seq.len = len;
2672 trace_consume(iter);
2674 if (iter->seq.len >= cnt)
2678 /* Now copy what we have to the user */
2679 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2680 if (iter->seq.readpos >= iter->seq.len)
2681 trace_seq_reset(&iter->seq);
2684 * If there was nothing to send to user, inspite of consuming trace
2685 * entries, go back to wait for more entries.
2691 mutex_unlock(&trace_types_lock);
2697 tracing_entries_read(struct file *filp, char __user *ubuf,
2698 size_t cnt, loff_t *ppos)
2700 struct trace_array *tr = filp->private_data;
2704 r = sprintf(buf, "%lu\n", tr->entries);
2705 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2709 tracing_entries_write(struct file *filp, const char __user *ubuf,
2710 size_t cnt, loff_t *ppos)
2715 struct trace_array *tr = filp->private_data;
2717 if (cnt >= sizeof(buf))
2720 if (copy_from_user(&buf, ubuf, cnt))
2725 ret = strict_strtoul(buf, 10, &val);
2729 /* must have at least 1 entry */
2733 mutex_lock(&trace_types_lock);
2737 pr_info("ftrace: please disable tracing"
2738 " before modifying buffer size\n");
2742 if (val != global_trace.entries) {
2743 ret = ring_buffer_resize(global_trace.buffer, val);
2749 ret = ring_buffer_resize(max_tr.buffer, val);
2753 r = ring_buffer_resize(global_trace.buffer,
2754 global_trace.entries);
2756 /* AARGH! We are left with different
2757 * size max buffer!!!! */
2759 tracing_disabled = 1;
2764 global_trace.entries = val;
2769 /* If check pages failed, return ENOMEM */
2770 if (tracing_disabled)
2773 max_tr.entries = global_trace.entries;
2774 mutex_unlock(&trace_types_lock);
2779 static int mark_printk(const char *fmt, ...)
2783 va_start(args, fmt);
2784 ret = trace_vprintk(0, fmt, args);
2790 tracing_mark_write(struct file *filp, const char __user *ubuf,
2791 size_t cnt, loff_t *fpos)
2795 struct trace_array *tr = &global_trace;
2797 if (!tr->ctrl || tracing_disabled)
2800 if (cnt > TRACE_BUF_SIZE)
2801 cnt = TRACE_BUF_SIZE;
2803 buf = kmalloc(cnt + 1, GFP_KERNEL);
2807 if (copy_from_user(buf, ubuf, cnt)) {
2812 /* Cut from the first nil or newline. */
2814 end = strchr(buf, '\n');
2818 cnt = mark_printk("%s\n", buf);
2825 static struct file_operations tracing_max_lat_fops = {
2826 .open = tracing_open_generic,
2827 .read = tracing_max_lat_read,
2828 .write = tracing_max_lat_write,
2831 static struct file_operations tracing_ctrl_fops = {
2832 .open = tracing_open_generic,
2833 .read = tracing_ctrl_read,
2834 .write = tracing_ctrl_write,
2837 static struct file_operations set_tracer_fops = {
2838 .open = tracing_open_generic,
2839 .read = tracing_set_trace_read,
2840 .write = tracing_set_trace_write,
2843 static struct file_operations tracing_pipe_fops = {
2844 .open = tracing_open_pipe,
2845 .poll = tracing_poll_pipe,
2846 .read = tracing_read_pipe,
2847 .release = tracing_release_pipe,
2850 static struct file_operations tracing_entries_fops = {
2851 .open = tracing_open_generic,
2852 .read = tracing_entries_read,
2853 .write = tracing_entries_write,
2856 static struct file_operations tracing_mark_fops = {
2857 .open = tracing_open_generic,
2858 .write = tracing_mark_write,
2861 #ifdef CONFIG_DYNAMIC_FTRACE
2864 tracing_read_long(struct file *filp, char __user *ubuf,
2865 size_t cnt, loff_t *ppos)
2867 unsigned long *p = filp->private_data;
2871 r = sprintf(buf, "%ld\n", *p);
2873 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2876 static struct file_operations tracing_read_long_fops = {
2877 .open = tracing_open_generic,
2878 .read = tracing_read_long,
2882 static struct dentry *d_tracer;
2884 struct dentry *tracing_init_dentry(void)
2891 d_tracer = debugfs_create_dir("tracing", NULL);
2893 if (!d_tracer && !once) {
2895 pr_warning("Could not create debugfs directory 'tracing'\n");
2902 #ifdef CONFIG_FTRACE_SELFTEST
2903 /* Let selftest have access to static functions in this file */
2904 #include "trace_selftest.c"
2907 static __init int tracer_init_debugfs(void)
2909 struct dentry *d_tracer;
2910 struct dentry *entry;
2912 d_tracer = tracing_init_dentry();
2914 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2915 &global_trace, &tracing_ctrl_fops);
2917 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2919 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2920 NULL, &tracing_iter_fops);
2922 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2924 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2925 NULL, &tracing_cpumask_fops);
2927 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2929 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2930 &global_trace, &tracing_lt_fops);
2932 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2934 entry = debugfs_create_file("trace", 0444, d_tracer,
2935 &global_trace, &tracing_fops);
2937 pr_warning("Could not create debugfs 'trace' entry\n");
2939 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2940 &global_trace, &show_traces_fops);
2942 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2944 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2945 &global_trace, &set_tracer_fops);
2947 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2949 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2950 &tracing_max_latency,
2951 &tracing_max_lat_fops);
2953 pr_warning("Could not create debugfs "
2954 "'tracing_max_latency' entry\n");
2956 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2957 &tracing_thresh, &tracing_max_lat_fops);
2959 pr_warning("Could not create debugfs "
2960 "'tracing_thresh' entry\n");
2961 entry = debugfs_create_file("README", 0644, d_tracer,
2962 NULL, &tracing_readme_fops);
2964 pr_warning("Could not create debugfs 'README' entry\n");
2966 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2967 NULL, &tracing_pipe_fops);
2969 pr_warning("Could not create debugfs "
2970 "'trace_pipe' entry\n");
2972 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2973 &global_trace, &tracing_entries_fops);
2975 pr_warning("Could not create debugfs "
2976 "'trace_entries' entry\n");
2978 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2979 NULL, &tracing_mark_fops);
2981 pr_warning("Could not create debugfs "
2982 "'trace_marker' entry\n");
2984 #ifdef CONFIG_DYNAMIC_FTRACE
2985 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2986 &ftrace_update_tot_cnt,
2987 &tracing_read_long_fops);
2989 pr_warning("Could not create debugfs "
2990 "'dyn_ftrace_total_info' entry\n");
2992 #ifdef CONFIG_SYSPROF_TRACER
2993 init_tracer_sysprof_debugfs(d_tracer);
2998 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3000 static DEFINE_SPINLOCK(trace_buf_lock);
3001 static char trace_buf[TRACE_BUF_SIZE];
3003 struct ring_buffer_event *event;
3004 struct trace_array *tr = &global_trace;
3005 struct trace_array_cpu *data;
3006 struct print_entry *entry;
3007 unsigned long flags, irq_flags;
3008 int cpu, len = 0, size, pc;
3010 if (!tr->ctrl || tracing_disabled)
3013 pc = preempt_count();
3014 preempt_disable_notrace();
3015 cpu = raw_smp_processor_id();
3016 data = tr->data[cpu];
3018 if (unlikely(atomic_read(&data->disabled)))
3021 spin_lock_irqsave(&trace_buf_lock, flags);
3022 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3024 len = min(len, TRACE_BUF_SIZE-1);
3027 size = sizeof(*entry) + len + 1;
3028 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3031 entry = ring_buffer_event_data(event);
3032 tracing_generic_entry_update(&entry->ent, flags, pc);
3033 entry->ent.type = TRACE_PRINT;
3036 memcpy(&entry->buf, trace_buf, len);
3037 entry->buf[len] = 0;
3038 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3041 spin_unlock_irqrestore(&trace_buf_lock, flags);
3044 preempt_enable_notrace();
3048 EXPORT_SYMBOL_GPL(trace_vprintk);
3050 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3055 if (!(trace_flags & TRACE_ITER_PRINTK))
3059 ret = trace_vprintk(ip, fmt, ap);
3063 EXPORT_SYMBOL_GPL(__ftrace_printk);
3065 static int trace_panic_handler(struct notifier_block *this,
3066 unsigned long event, void *unused)
3068 if (ftrace_dump_on_oops)
3073 static struct notifier_block trace_panic_notifier = {
3074 .notifier_call = trace_panic_handler,
3076 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3079 static int trace_die_handler(struct notifier_block *self,
3085 if (ftrace_dump_on_oops)
3094 static struct notifier_block trace_die_notifier = {
3095 .notifier_call = trace_die_handler,
3100 * printk is set to max of 1024, we really don't need it that big.
3101 * Nothing should be printing 1000 characters anyway.
3103 #define TRACE_MAX_PRINT 1000
3106 * Define here KERN_TRACE so that we have one place to modify
3107 * it if we decide to change what log level the ftrace dump
3110 #define KERN_TRACE KERN_INFO
3113 trace_printk_seq(struct trace_seq *s)
3115 /* Probably should print a warning here. */
3119 /* should be zero ended, but we are paranoid. */
3120 s->buffer[s->len] = 0;
3122 printk(KERN_TRACE "%s", s->buffer);
3127 void ftrace_dump(void)
3129 static DEFINE_SPINLOCK(ftrace_dump_lock);
3130 /* use static because iter can be a bit big for the stack */
3131 static struct trace_iterator iter;
3132 static cpumask_t mask;
3133 static int dump_ran;
3134 unsigned long flags;
3138 spin_lock_irqsave(&ftrace_dump_lock, flags);
3144 /* No turning back! */
3147 for_each_tracing_cpu(cpu) {
3148 atomic_inc(&global_trace.data[cpu]->disabled);
3151 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3153 iter.tr = &global_trace;
3154 iter.trace = current_trace;
3157 * We need to stop all tracing on all CPUS to read the
3158 * the next buffer. This is a bit expensive, but is
3159 * not done often. We fill all what we can read,
3160 * and then release the locks again.
3165 while (!trace_empty(&iter)) {
3168 printk(KERN_TRACE "---------------------------------\n");
3172 /* reset all but tr, trace, and overruns */
3173 memset(&iter.seq, 0,
3174 sizeof(struct trace_iterator) -
3175 offsetof(struct trace_iterator, seq));
3176 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3179 if (find_next_entry_inc(&iter) != NULL) {
3180 print_trace_line(&iter);
3181 trace_consume(&iter);
3184 trace_printk_seq(&iter.seq);
3188 printk(KERN_TRACE " (ftrace buffer empty)\n");
3190 printk(KERN_TRACE "---------------------------------\n");
3193 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3196 __init static int tracer_alloc_buffers(void)
3198 struct trace_array_cpu *data;
3201 /* TODO: make the number of buffers hot pluggable with CPUS */
3202 tracing_buffer_mask = cpu_possible_map;
3204 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3205 TRACE_BUFFER_FLAGS);
3206 if (!global_trace.buffer) {
3207 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3211 global_trace.entries = ring_buffer_size(global_trace.buffer);
3213 #ifdef CONFIG_TRACER_MAX_TRACE
3214 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3215 TRACE_BUFFER_FLAGS);
3216 if (!max_tr.buffer) {
3217 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3219 ring_buffer_free(global_trace.buffer);
3222 max_tr.entries = ring_buffer_size(max_tr.buffer);
3223 WARN_ON(max_tr.entries != global_trace.entries);
3226 /* Allocate the first page for all buffers */
3227 for_each_tracing_cpu(i) {
3228 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3229 max_tr.data[i] = &per_cpu(max_data, i);
3232 trace_init_cmdlines();
3234 register_tracer(&nop_trace);
3235 #ifdef CONFIG_BOOT_TRACER
3236 register_tracer(&boot_tracer);
3237 current_trace = &boot_tracer;
3238 current_trace->init(&global_trace);
3240 current_trace = &nop_trace;
3243 /* All seems OK, enable tracing */
3244 global_trace.ctrl = tracer_enabled;
3245 tracing_disabled = 0;
3247 atomic_notifier_chain_register(&panic_notifier_list,
3248 &trace_panic_notifier);
3250 register_die_notifier(&trace_die_notifier);
3254 early_initcall(tracer_alloc_buffers);
3255 fs_initcall(tracer_init_debugfs);