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;
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[] = {
268 * ftrace_max_lock is used to protect the swapping of buffers
269 * when taking a max snapshot. The buffers themselves are
270 * protected by per_cpu spinlocks. But the action of the swap
271 * needs its own lock.
273 * This is defined as a raw_spinlock_t in order to help
274 * with performance when lockdep debugging is enabled.
276 static raw_spinlock_t ftrace_max_lock =
277 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
280 * Copy the new maximum trace into the separate maximum-trace
281 * structure. (this way the maximum trace is permanently saved,
282 * for later retrieval via /debugfs/tracing/latency_trace)
285 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
287 struct trace_array_cpu *data = tr->data[cpu];
290 max_tr.time_start = data->preempt_timestamp;
292 data = max_tr.data[cpu];
293 data->saved_latency = tracing_max_latency;
295 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
296 data->pid = tsk->pid;
297 data->uid = tsk->uid;
298 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
299 data->policy = tsk->policy;
300 data->rt_priority = tsk->rt_priority;
302 /* record this tasks comm */
303 tracing_record_cmdline(current);
307 * trace_seq_printf - sequence printing of trace information
308 * @s: trace sequence descriptor
309 * @fmt: printf format string
311 * The tracer may use either sequence operations or its own
312 * copy to user routines. To simplify formating of a trace
313 * trace_seq_printf is used to store strings into a special
314 * buffer (@s). Then the output may be either used by
315 * the sequencer or pulled into another buffer.
318 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
320 int len = (PAGE_SIZE - 1) - s->len;
328 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
331 /* If we can't write it all, don't bother writing anything */
341 * trace_seq_puts - trace sequence printing of simple string
342 * @s: trace sequence descriptor
343 * @str: simple string to record
345 * The tracer may use either the sequence operations or its own
346 * copy to user routines. This function records a simple string
347 * into a special buffer (@s) for later retrieval by a sequencer
348 * or other mechanism.
351 trace_seq_puts(struct trace_seq *s, const char *str)
353 int len = strlen(str);
355 if (len > ((PAGE_SIZE - 1) - s->len))
358 memcpy(s->buffer + s->len, str, len);
365 trace_seq_putc(struct trace_seq *s, unsigned char c)
367 if (s->len >= (PAGE_SIZE - 1))
370 s->buffer[s->len++] = c;
376 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
378 if (len > ((PAGE_SIZE - 1) - s->len))
381 memcpy(s->buffer + s->len, mem, len);
387 #define MAX_MEMHEX_BYTES 8
388 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
391 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
393 unsigned char hex[HEX_CHARS];
394 unsigned char *data = mem;
398 for (i = 0, j = 0; i < len; i++) {
400 for (i = len-1, j = 0; i >= 0; i--) {
402 hex[j++] = hex_asc_hi(data[i]);
403 hex[j++] = hex_asc_lo(data[i]);
407 return trace_seq_putmem(s, hex, j);
411 trace_seq_reset(struct trace_seq *s)
417 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
422 if (s->len <= s->readpos)
425 len = s->len - s->readpos;
428 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
437 trace_print_seq(struct seq_file *m, struct trace_seq *s)
439 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
442 seq_puts(m, s->buffer);
448 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
450 * @tsk: the task with the latency
451 * @cpu: The cpu that initiated the trace.
453 * Flip the buffers between the @tr and the max_tr and record information
454 * about which task was the cause of this latency.
457 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
459 struct ring_buffer *buf = tr->buffer;
461 WARN_ON_ONCE(!irqs_disabled());
462 __raw_spin_lock(&ftrace_max_lock);
464 tr->buffer = max_tr.buffer;
467 ftrace_disable_cpu();
468 ring_buffer_reset(tr->buffer);
471 __update_max_tr(tr, tsk, cpu);
472 __raw_spin_unlock(&ftrace_max_lock);
476 * update_max_tr_single - only copy one trace over, and reset the rest
478 * @tsk - task with the latency
479 * @cpu - the cpu of the buffer to copy.
481 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
484 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
488 WARN_ON_ONCE(!irqs_disabled());
489 __raw_spin_lock(&ftrace_max_lock);
491 ftrace_disable_cpu();
493 ring_buffer_reset(max_tr.buffer);
494 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
500 __update_max_tr(tr, tsk, cpu);
501 __raw_spin_unlock(&ftrace_max_lock);
505 * register_tracer - register a tracer with the ftrace system.
506 * @type - the plugin for the tracer
508 * Register a new plugin tracer.
510 int register_tracer(struct tracer *type)
517 pr_info("Tracer must have a name\n");
521 mutex_lock(&trace_types_lock);
522 for (t = trace_types; t; t = t->next) {
523 if (strcmp(type->name, t->name) == 0) {
525 pr_info("Trace %s already registered\n",
532 #ifdef CONFIG_FTRACE_STARTUP_TEST
533 if (type->selftest) {
534 struct tracer *saved_tracer = current_trace;
535 struct trace_array *tr = &global_trace;
538 * Run a selftest on this tracer.
539 * Here we reset the trace buffer, and set the current
540 * tracer to be this tracer. The tracer can then run some
541 * internal tracing to verify that everything is in order.
542 * If we fail, we do not register this tracer.
544 for_each_tracing_cpu(i) {
545 tracing_reset(tr, i);
547 current_trace = type;
548 /* the test is responsible for initializing and enabling */
549 pr_info("Testing tracer %s: ", type->name);
550 ret = type->selftest(type, tr);
551 /* the test is responsible for resetting too */
552 current_trace = saved_tracer;
554 printk(KERN_CONT "FAILED!\n");
557 /* Only reset on passing, to avoid touching corrupted buffers */
558 for_each_tracing_cpu(i) {
559 tracing_reset(tr, i);
561 printk(KERN_CONT "PASSED\n");
565 type->next = trace_types;
567 len = strlen(type->name);
568 if (len > max_tracer_type_len)
569 max_tracer_type_len = len;
572 mutex_unlock(&trace_types_lock);
577 void unregister_tracer(struct tracer *type)
582 mutex_lock(&trace_types_lock);
583 for (t = &trace_types; *t; t = &(*t)->next) {
587 pr_info("Trace %s not registered\n", type->name);
592 if (strlen(type->name) != max_tracer_type_len)
595 max_tracer_type_len = 0;
596 for (t = &trace_types; *t; t = &(*t)->next) {
597 len = strlen((*t)->name);
598 if (len > max_tracer_type_len)
599 max_tracer_type_len = len;
602 mutex_unlock(&trace_types_lock);
605 void tracing_reset(struct trace_array *tr, int cpu)
607 ftrace_disable_cpu();
608 ring_buffer_reset_cpu(tr->buffer, cpu);
612 #define SAVED_CMDLINES 128
613 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
614 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
615 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
616 static int cmdline_idx;
617 static DEFINE_SPINLOCK(trace_cmdline_lock);
619 /* temporary disable recording */
620 atomic_t trace_record_cmdline_disabled __read_mostly;
622 static void trace_init_cmdlines(void)
624 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
625 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
629 static int trace_stop_count;
630 static DEFINE_SPINLOCK(tracing_start_lock);
633 * tracing_start - quick start of the tracer
635 * If tracing is enabled but was stopped by tracing_stop,
636 * this will start the tracer back up.
638 void tracing_start(void)
640 struct ring_buffer *buffer;
643 if (tracing_disabled)
646 spin_lock_irqsave(&tracing_start_lock, flags);
647 if (--trace_stop_count)
650 if (trace_stop_count < 0) {
651 /* Someone screwed up their debugging */
653 trace_stop_count = 0;
658 buffer = global_trace.buffer;
660 ring_buffer_record_enable(buffer);
662 buffer = max_tr.buffer;
664 ring_buffer_record_enable(buffer);
668 spin_unlock_irqrestore(&tracing_start_lock, flags);
672 * tracing_stop - quick stop of the tracer
674 * Light weight way to stop tracing. Use in conjunction with
677 void tracing_stop(void)
679 struct ring_buffer *buffer;
683 spin_lock_irqsave(&tracing_start_lock, flags);
684 if (trace_stop_count++)
687 buffer = global_trace.buffer;
689 ring_buffer_record_disable(buffer);
691 buffer = max_tr.buffer;
693 ring_buffer_record_disable(buffer);
696 spin_unlock_irqrestore(&tracing_start_lock, flags);
699 void trace_stop_cmdline_recording(void);
701 static void trace_save_cmdline(struct task_struct *tsk)
706 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
710 * It's not the end of the world if we don't get
711 * the lock, but we also don't want to spin
712 * nor do we want to disable interrupts,
713 * so if we miss here, then better luck next time.
715 if (!spin_trylock(&trace_cmdline_lock))
718 idx = map_pid_to_cmdline[tsk->pid];
719 if (idx >= SAVED_CMDLINES) {
720 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
722 map = map_cmdline_to_pid[idx];
723 if (map <= PID_MAX_DEFAULT)
724 map_pid_to_cmdline[map] = (unsigned)-1;
726 map_pid_to_cmdline[tsk->pid] = idx;
731 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
733 spin_unlock(&trace_cmdline_lock);
736 static char *trace_find_cmdline(int pid)
738 char *cmdline = "<...>";
744 if (pid > PID_MAX_DEFAULT)
747 map = map_pid_to_cmdline[pid];
748 if (map >= SAVED_CMDLINES)
751 cmdline = saved_cmdlines[map];
757 void tracing_record_cmdline(struct task_struct *tsk)
759 if (atomic_read(&trace_record_cmdline_disabled))
762 trace_save_cmdline(tsk);
766 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
769 struct task_struct *tsk = current;
771 entry->preempt_count = pc & 0xff;
772 entry->pid = (tsk) ? tsk->pid : 0;
774 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
775 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
777 TRACE_FLAG_IRQS_NOSUPPORT |
779 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
780 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
781 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
785 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
786 unsigned long ip, unsigned long parent_ip, unsigned long flags,
789 struct ring_buffer_event *event;
790 struct ftrace_entry *entry;
791 unsigned long irq_flags;
793 /* If we are reading the ring buffer, don't trace */
794 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
797 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
801 entry = ring_buffer_event_data(event);
802 tracing_generic_entry_update(&entry->ent, flags, pc);
803 entry->ent.type = TRACE_FN;
805 entry->parent_ip = parent_ip;
806 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
809 #ifdef CONFIG_FUNCTION_RET_TRACER
810 static void __trace_function_return(struct trace_array *tr,
811 struct trace_array_cpu *data,
812 struct ftrace_retfunc *trace,
816 struct ring_buffer_event *event;
817 struct ftrace_ret_entry *entry;
818 unsigned long irq_flags;
820 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
823 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
827 entry = ring_buffer_event_data(event);
828 tracing_generic_entry_update(&entry->ent, flags, pc);
829 entry->ent.type = TRACE_FN_RET;
830 entry->ip = trace->func;
831 entry->parent_ip = trace->ret;
832 entry->rettime = trace->rettime;
833 entry->calltime = trace->calltime;
834 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
839 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
840 unsigned long ip, unsigned long parent_ip, unsigned long flags,
843 if (likely(!atomic_read(&data->disabled)))
844 trace_function(tr, data, ip, parent_ip, flags, pc);
847 static void ftrace_trace_stack(struct trace_array *tr,
848 struct trace_array_cpu *data,
852 #ifdef CONFIG_STACKTRACE
853 struct ring_buffer_event *event;
854 struct stack_entry *entry;
855 struct stack_trace trace;
856 unsigned long irq_flags;
858 if (!(trace_flags & TRACE_ITER_STACKTRACE))
861 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
865 entry = ring_buffer_event_data(event);
866 tracing_generic_entry_update(&entry->ent, flags, pc);
867 entry->ent.type = TRACE_STACK;
869 memset(&entry->caller, 0, sizeof(entry->caller));
871 trace.nr_entries = 0;
872 trace.max_entries = FTRACE_STACK_ENTRIES;
874 trace.entries = entry->caller;
876 save_stack_trace(&trace);
877 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
881 void __trace_stack(struct trace_array *tr,
882 struct trace_array_cpu *data,
886 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
890 ftrace_trace_special(void *__tr, void *__data,
891 unsigned long arg1, unsigned long arg2, unsigned long arg3,
894 struct ring_buffer_event *event;
895 struct trace_array_cpu *data = __data;
896 struct trace_array *tr = __tr;
897 struct special_entry *entry;
898 unsigned long irq_flags;
900 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
904 entry = ring_buffer_event_data(event);
905 tracing_generic_entry_update(&entry->ent, 0, pc);
906 entry->ent.type = TRACE_SPECIAL;
910 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
911 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
917 __trace_special(void *__tr, void *__data,
918 unsigned long arg1, unsigned long arg2, unsigned long arg3)
920 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
924 tracing_sched_switch_trace(struct trace_array *tr,
925 struct trace_array_cpu *data,
926 struct task_struct *prev,
927 struct task_struct *next,
928 unsigned long flags, int pc)
930 struct ring_buffer_event *event;
931 struct ctx_switch_entry *entry;
932 unsigned long irq_flags;
934 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
938 entry = ring_buffer_event_data(event);
939 tracing_generic_entry_update(&entry->ent, flags, pc);
940 entry->ent.type = TRACE_CTX;
941 entry->prev_pid = prev->pid;
942 entry->prev_prio = prev->prio;
943 entry->prev_state = prev->state;
944 entry->next_pid = next->pid;
945 entry->next_prio = next->prio;
946 entry->next_state = next->state;
947 entry->next_cpu = task_cpu(next);
948 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
949 ftrace_trace_stack(tr, data, flags, 5, pc);
953 tracing_sched_wakeup_trace(struct trace_array *tr,
954 struct trace_array_cpu *data,
955 struct task_struct *wakee,
956 struct task_struct *curr,
957 unsigned long flags, int pc)
959 struct ring_buffer_event *event;
960 struct ctx_switch_entry *entry;
961 unsigned long irq_flags;
963 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
967 entry = ring_buffer_event_data(event);
968 tracing_generic_entry_update(&entry->ent, flags, pc);
969 entry->ent.type = TRACE_WAKE;
970 entry->prev_pid = curr->pid;
971 entry->prev_prio = curr->prio;
972 entry->prev_state = curr->state;
973 entry->next_pid = wakee->pid;
974 entry->next_prio = wakee->prio;
975 entry->next_state = wakee->state;
976 entry->next_cpu = task_cpu(wakee);
977 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
978 ftrace_trace_stack(tr, data, flags, 6, pc);
984 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
986 struct trace_array *tr = &global_trace;
987 struct trace_array_cpu *data;
992 if (tracing_disabled)
995 pc = preempt_count();
996 local_irq_save(flags);
997 cpu = raw_smp_processor_id();
998 data = tr->data[cpu];
1000 if (likely(atomic_inc_return(&data->disabled) == 1))
1001 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1003 atomic_dec(&data->disabled);
1004 local_irq_restore(flags);
1007 #ifdef CONFIG_FUNCTION_TRACER
1009 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
1011 struct trace_array *tr = &global_trace;
1012 struct trace_array_cpu *data;
1013 unsigned long flags;
1018 if (unlikely(!ftrace_function_enabled))
1021 pc = preempt_count();
1022 resched = ftrace_preempt_disable();
1023 local_save_flags(flags);
1024 cpu = raw_smp_processor_id();
1025 data = tr->data[cpu];
1026 disabled = atomic_inc_return(&data->disabled);
1028 if (likely(disabled == 1))
1029 trace_function(tr, data, ip, parent_ip, flags, pc);
1031 atomic_dec(&data->disabled);
1032 ftrace_preempt_enable(resched);
1036 function_trace_call(unsigned long ip, unsigned long parent_ip)
1038 struct trace_array *tr = &global_trace;
1039 struct trace_array_cpu *data;
1040 unsigned long flags;
1045 if (unlikely(!ftrace_function_enabled))
1049 * Need to use raw, since this must be called before the
1050 * recursive protection is performed.
1052 local_irq_save(flags);
1053 cpu = raw_smp_processor_id();
1054 data = tr->data[cpu];
1055 disabled = atomic_inc_return(&data->disabled);
1057 if (likely(disabled == 1)) {
1058 pc = preempt_count();
1059 trace_function(tr, data, ip, parent_ip, flags, pc);
1062 atomic_dec(&data->disabled);
1063 local_irq_restore(flags);
1066 #ifdef CONFIG_FUNCTION_RET_TRACER
1067 void trace_function_return(struct ftrace_retfunc *trace)
1069 struct trace_array *tr = &global_trace;
1070 struct trace_array_cpu *data;
1071 unsigned long flags;
1076 raw_local_irq_save(flags);
1077 cpu = raw_smp_processor_id();
1078 data = tr->data[cpu];
1079 disabled = atomic_inc_return(&data->disabled);
1080 if (likely(disabled == 1)) {
1081 pc = preempt_count();
1082 __trace_function_return(tr, data, trace, flags, pc);
1084 atomic_dec(&data->disabled);
1085 raw_local_irq_restore(flags);
1087 #endif /* CONFIG_FUNCTION_RET_TRACER */
1089 static struct ftrace_ops trace_ops __read_mostly =
1091 .func = function_trace_call,
1094 void tracing_start_function_trace(void)
1096 ftrace_function_enabled = 0;
1098 if (trace_flags & TRACE_ITER_PREEMPTONLY)
1099 trace_ops.func = function_trace_call_preempt_only;
1101 trace_ops.func = function_trace_call;
1103 register_ftrace_function(&trace_ops);
1104 ftrace_function_enabled = 1;
1107 void tracing_stop_function_trace(void)
1109 ftrace_function_enabled = 0;
1110 unregister_ftrace_function(&trace_ops);
1114 enum trace_file_type {
1115 TRACE_FILE_LAT_FMT = 1,
1116 TRACE_FILE_ANNOTATE = 2,
1119 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1121 /* Don't allow ftrace to trace into the ring buffers */
1122 ftrace_disable_cpu();
1125 if (iter->buffer_iter[iter->cpu])
1126 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1128 ftrace_enable_cpu();
1131 static struct trace_entry *
1132 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1134 struct ring_buffer_event *event;
1135 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1137 /* Don't allow ftrace to trace into the ring buffers */
1138 ftrace_disable_cpu();
1141 event = ring_buffer_iter_peek(buf_iter, ts);
1143 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1145 ftrace_enable_cpu();
1147 return event ? ring_buffer_event_data(event) : NULL;
1150 static struct trace_entry *
1151 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1153 struct ring_buffer *buffer = iter->tr->buffer;
1154 struct trace_entry *ent, *next = NULL;
1155 u64 next_ts = 0, ts;
1159 for_each_tracing_cpu(cpu) {
1161 if (ring_buffer_empty_cpu(buffer, cpu))
1164 ent = peek_next_entry(iter, cpu, &ts);
1167 * Pick the entry with the smallest timestamp:
1169 if (ent && (!next || ts < next_ts)) {
1177 *ent_cpu = next_cpu;
1185 /* Find the next real entry, without updating the iterator itself */
1186 static struct trace_entry *
1187 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1189 return __find_next_entry(iter, ent_cpu, ent_ts);
1192 /* Find the next real entry, and increment the iterator to the next entry */
1193 static void *find_next_entry_inc(struct trace_iterator *iter)
1195 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1198 trace_iterator_increment(iter, iter->cpu);
1200 return iter->ent ? iter : NULL;
1203 static void trace_consume(struct trace_iterator *iter)
1205 /* Don't allow ftrace to trace into the ring buffers */
1206 ftrace_disable_cpu();
1207 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1208 ftrace_enable_cpu();
1211 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1213 struct trace_iterator *iter = m->private;
1219 /* can't go backwards */
1224 ent = find_next_entry_inc(iter);
1228 while (ent && iter->idx < i)
1229 ent = find_next_entry_inc(iter);
1236 static void *s_start(struct seq_file *m, loff_t *pos)
1238 struct trace_iterator *iter = m->private;
1243 mutex_lock(&trace_types_lock);
1245 if (!current_trace || current_trace != iter->trace) {
1246 mutex_unlock(&trace_types_lock);
1250 atomic_inc(&trace_record_cmdline_disabled);
1252 if (*pos != iter->pos) {
1257 ftrace_disable_cpu();
1259 for_each_tracing_cpu(cpu) {
1260 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1263 ftrace_enable_cpu();
1265 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1270 p = s_next(m, p, &l);
1276 static void s_stop(struct seq_file *m, void *p)
1278 atomic_dec(&trace_record_cmdline_disabled);
1279 mutex_unlock(&trace_types_lock);
1282 #ifdef CONFIG_KRETPROBES
1283 static inline const char *kretprobed(const char *name)
1285 static const char tramp_name[] = "kretprobe_trampoline";
1286 int size = sizeof(tramp_name);
1288 if (strncmp(tramp_name, name, size) == 0)
1289 return "[unknown/kretprobe'd]";
1293 static inline const char *kretprobed(const char *name)
1297 #endif /* CONFIG_KRETPROBES */
1300 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1302 #ifdef CONFIG_KALLSYMS
1303 char str[KSYM_SYMBOL_LEN];
1306 kallsyms_lookup(address, NULL, NULL, NULL, str);
1308 name = kretprobed(str);
1310 return trace_seq_printf(s, fmt, name);
1316 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1317 unsigned long address)
1319 #ifdef CONFIG_KALLSYMS
1320 char str[KSYM_SYMBOL_LEN];
1323 sprint_symbol(str, address);
1324 name = kretprobed(str);
1326 return trace_seq_printf(s, fmt, name);
1331 #ifndef CONFIG_64BIT
1332 # define IP_FMT "%08lx"
1334 # define IP_FMT "%016lx"
1338 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1343 return trace_seq_printf(s, "0");
1345 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1346 ret = seq_print_sym_offset(s, "%s", ip);
1348 ret = seq_print_sym_short(s, "%s", ip);
1353 if (sym_flags & TRACE_ITER_SYM_ADDR)
1354 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1358 static void print_lat_help_header(struct seq_file *m)
1360 seq_puts(m, "# _------=> CPU# \n");
1361 seq_puts(m, "# / _-----=> irqs-off \n");
1362 seq_puts(m, "# | / _----=> need-resched \n");
1363 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1364 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1365 seq_puts(m, "# |||| / \n");
1366 seq_puts(m, "# ||||| delay \n");
1367 seq_puts(m, "# cmd pid ||||| time | caller \n");
1368 seq_puts(m, "# \\ / ||||| \\ | / \n");
1371 static void print_func_help_header(struct seq_file *m)
1373 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1374 seq_puts(m, "# | | | | |\n");
1379 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1381 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1382 struct trace_array *tr = iter->tr;
1383 struct trace_array_cpu *data = tr->data[tr->cpu];
1384 struct tracer *type = current_trace;
1385 unsigned long total;
1386 unsigned long entries;
1387 const char *name = "preemption";
1392 entries = ring_buffer_entries(iter->tr->buffer);
1394 ring_buffer_overruns(iter->tr->buffer);
1396 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1398 seq_puts(m, "-----------------------------------"
1399 "---------------------------------\n");
1400 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1401 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1402 nsecs_to_usecs(data->saved_latency),
1406 #if defined(CONFIG_PREEMPT_NONE)
1408 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1410 #elif defined(CONFIG_PREEMPT)
1415 /* These are reserved for later use */
1418 seq_printf(m, " #P:%d)\n", num_online_cpus());
1422 seq_puts(m, " -----------------\n");
1423 seq_printf(m, " | task: %.16s-%d "
1424 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1425 data->comm, data->pid, data->uid, data->nice,
1426 data->policy, data->rt_priority);
1427 seq_puts(m, " -----------------\n");
1429 if (data->critical_start) {
1430 seq_puts(m, " => started at: ");
1431 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1432 trace_print_seq(m, &iter->seq);
1433 seq_puts(m, "\n => ended at: ");
1434 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1435 trace_print_seq(m, &iter->seq);
1443 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1445 int hardirq, softirq;
1448 comm = trace_find_cmdline(entry->pid);
1450 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1451 trace_seq_printf(s, "%3d", cpu);
1452 trace_seq_printf(s, "%c%c",
1453 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1454 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1455 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1457 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1458 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1459 if (hardirq && softirq) {
1460 trace_seq_putc(s, 'H');
1463 trace_seq_putc(s, 'h');
1466 trace_seq_putc(s, 's');
1468 trace_seq_putc(s, '.');
1472 if (entry->preempt_count)
1473 trace_seq_printf(s, "%x", entry->preempt_count);
1475 trace_seq_puts(s, ".");
1478 unsigned long preempt_mark_thresh = 100;
1481 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1482 unsigned long rel_usecs)
1484 trace_seq_printf(s, " %4lldus", abs_usecs);
1485 if (rel_usecs > preempt_mark_thresh)
1486 trace_seq_puts(s, "!: ");
1487 else if (rel_usecs > 1)
1488 trace_seq_puts(s, "+: ");
1490 trace_seq_puts(s, " : ");
1493 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1496 * The message is supposed to contain an ending newline.
1497 * If the printing stops prematurely, try to add a newline of our own.
1499 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1501 struct trace_entry *ent;
1502 struct trace_field_cont *cont;
1505 ent = peek_next_entry(iter, iter->cpu, NULL);
1506 if (!ent || ent->type != TRACE_CONT) {
1507 trace_seq_putc(s, '\n');
1512 cont = (struct trace_field_cont *)ent;
1514 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1516 ftrace_disable_cpu();
1518 if (iter->buffer_iter[iter->cpu])
1519 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1521 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1523 ftrace_enable_cpu();
1525 ent = peek_next_entry(iter, iter->cpu, NULL);
1526 } while (ent && ent->type == TRACE_CONT);
1529 trace_seq_putc(s, '\n');
1532 static void test_cpu_buff_start(struct trace_iterator *iter)
1534 struct trace_seq *s = &iter->seq;
1536 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1539 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1542 if (cpu_isset(iter->cpu, iter->started))
1545 cpu_set(iter->cpu, iter->started);
1546 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1549 static enum print_line_t
1550 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1552 struct trace_seq *s = &iter->seq;
1553 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1554 struct trace_entry *next_entry;
1555 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1556 struct trace_entry *entry = iter->ent;
1557 unsigned long abs_usecs;
1558 unsigned long rel_usecs;
1565 if (entry->type == TRACE_CONT)
1566 return TRACE_TYPE_HANDLED;
1568 test_cpu_buff_start(iter);
1570 next_entry = find_next_entry(iter, NULL, &next_ts);
1573 rel_usecs = ns2usecs(next_ts - iter->ts);
1574 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1577 comm = trace_find_cmdline(entry->pid);
1578 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1579 " %ld.%03ldms (+%ld.%03ldms): ",
1581 entry->pid, cpu, entry->flags,
1582 entry->preempt_count, trace_idx,
1585 abs_usecs % 1000, rel_usecs/1000,
1588 lat_print_generic(s, entry, cpu);
1589 lat_print_timestamp(s, abs_usecs, rel_usecs);
1591 switch (entry->type) {
1593 struct ftrace_entry *field;
1595 trace_assign_type(field, entry);
1597 seq_print_ip_sym(s, field->ip, sym_flags);
1598 trace_seq_puts(s, " (");
1599 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1600 trace_seq_puts(s, ")\n");
1605 struct ctx_switch_entry *field;
1607 trace_assign_type(field, entry);
1609 T = field->next_state < sizeof(state_to_char) ?
1610 state_to_char[field->next_state] : 'X';
1612 state = field->prev_state ?
1613 __ffs(field->prev_state) + 1 : 0;
1614 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1615 comm = trace_find_cmdline(field->next_pid);
1616 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1619 S, entry->type == TRACE_CTX ? "==>" : " +",
1626 case TRACE_SPECIAL: {
1627 struct special_entry *field;
1629 trace_assign_type(field, entry);
1631 trace_seq_printf(s, "# %ld %ld %ld\n",
1638 struct stack_entry *field;
1640 trace_assign_type(field, entry);
1642 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1644 trace_seq_puts(s, " <= ");
1645 seq_print_ip_sym(s, field->caller[i], sym_flags);
1647 trace_seq_puts(s, "\n");
1651 struct print_entry *field;
1653 trace_assign_type(field, entry);
1655 seq_print_ip_sym(s, field->ip, sym_flags);
1656 trace_seq_printf(s, ": %s", field->buf);
1657 if (entry->flags & TRACE_FLAG_CONT)
1658 trace_seq_print_cont(s, iter);
1661 case TRACE_BRANCH: {
1662 struct trace_branch *field;
1664 trace_assign_type(field, entry);
1666 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1667 field->correct ? " ok " : " MISS ",
1674 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1676 return TRACE_TYPE_HANDLED;
1679 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1681 struct trace_seq *s = &iter->seq;
1682 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1683 struct trace_entry *entry;
1684 unsigned long usec_rem;
1685 unsigned long long t;
1694 if (entry->type == TRACE_CONT)
1695 return TRACE_TYPE_HANDLED;
1697 test_cpu_buff_start(iter);
1699 comm = trace_find_cmdline(iter->ent->pid);
1701 t = ns2usecs(iter->ts);
1702 usec_rem = do_div(t, 1000000ULL);
1703 secs = (unsigned long)t;
1705 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1707 return TRACE_TYPE_PARTIAL_LINE;
1708 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1710 return TRACE_TYPE_PARTIAL_LINE;
1711 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1713 return TRACE_TYPE_PARTIAL_LINE;
1715 switch (entry->type) {
1717 struct ftrace_entry *field;
1719 trace_assign_type(field, entry);
1721 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1723 return TRACE_TYPE_PARTIAL_LINE;
1724 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1726 ret = trace_seq_printf(s, " <-");
1728 return TRACE_TYPE_PARTIAL_LINE;
1729 ret = seq_print_ip_sym(s,
1733 return TRACE_TYPE_PARTIAL_LINE;
1735 ret = trace_seq_printf(s, "\n");
1737 return TRACE_TYPE_PARTIAL_LINE;
1742 struct ctx_switch_entry *field;
1744 trace_assign_type(field, entry);
1746 S = field->prev_state < sizeof(state_to_char) ?
1747 state_to_char[field->prev_state] : 'X';
1748 T = field->next_state < sizeof(state_to_char) ?
1749 state_to_char[field->next_state] : 'X';
1750 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1754 entry->type == TRACE_CTX ? "==>" : " +",
1760 return TRACE_TYPE_PARTIAL_LINE;
1763 case TRACE_SPECIAL: {
1764 struct special_entry *field;
1766 trace_assign_type(field, entry);
1768 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1773 return TRACE_TYPE_PARTIAL_LINE;
1777 struct stack_entry *field;
1779 trace_assign_type(field, entry);
1781 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1783 ret = trace_seq_puts(s, " <= ");
1785 return TRACE_TYPE_PARTIAL_LINE;
1787 ret = seq_print_ip_sym(s, field->caller[i],
1790 return TRACE_TYPE_PARTIAL_LINE;
1792 ret = trace_seq_puts(s, "\n");
1794 return TRACE_TYPE_PARTIAL_LINE;
1798 struct print_entry *field;
1800 trace_assign_type(field, entry);
1802 seq_print_ip_sym(s, field->ip, sym_flags);
1803 trace_seq_printf(s, ": %s", field->buf);
1804 if (entry->flags & TRACE_FLAG_CONT)
1805 trace_seq_print_cont(s, iter);
1808 case TRACE_FN_RET: {
1809 return print_return_function(iter);
1812 case TRACE_BRANCH: {
1813 struct trace_branch *field;
1815 trace_assign_type(field, entry);
1817 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1818 field->correct ? " ok " : " MISS ",
1825 return TRACE_TYPE_HANDLED;
1828 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1830 struct trace_seq *s = &iter->seq;
1831 struct trace_entry *entry;
1837 if (entry->type == TRACE_CONT)
1838 return TRACE_TYPE_HANDLED;
1840 ret = trace_seq_printf(s, "%d %d %llu ",
1841 entry->pid, iter->cpu, iter->ts);
1843 return TRACE_TYPE_PARTIAL_LINE;
1845 switch (entry->type) {
1847 struct ftrace_entry *field;
1849 trace_assign_type(field, entry);
1851 ret = trace_seq_printf(s, "%x %x\n",
1855 return TRACE_TYPE_PARTIAL_LINE;
1860 struct ctx_switch_entry *field;
1862 trace_assign_type(field, entry);
1864 S = field->prev_state < sizeof(state_to_char) ?
1865 state_to_char[field->prev_state] : 'X';
1866 T = field->next_state < sizeof(state_to_char) ?
1867 state_to_char[field->next_state] : 'X';
1868 if (entry->type == TRACE_WAKE)
1870 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
1879 return TRACE_TYPE_PARTIAL_LINE;
1884 struct special_entry *field;
1886 trace_assign_type(field, entry);
1888 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1893 return TRACE_TYPE_PARTIAL_LINE;
1897 struct print_entry *field;
1899 trace_assign_type(field, entry);
1901 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
1902 if (entry->flags & TRACE_FLAG_CONT)
1903 trace_seq_print_cont(s, iter);
1907 return TRACE_TYPE_HANDLED;
1910 #define SEQ_PUT_FIELD_RET(s, x) \
1912 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1916 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1918 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
1919 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1923 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1925 struct trace_seq *s = &iter->seq;
1926 unsigned char newline = '\n';
1927 struct trace_entry *entry;
1932 if (entry->type == TRACE_CONT)
1933 return TRACE_TYPE_HANDLED;
1935 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1936 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1937 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1939 switch (entry->type) {
1941 struct ftrace_entry *field;
1943 trace_assign_type(field, entry);
1945 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1946 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
1951 struct ctx_switch_entry *field;
1953 trace_assign_type(field, entry);
1955 S = field->prev_state < sizeof(state_to_char) ?
1956 state_to_char[field->prev_state] : 'X';
1957 T = field->next_state < sizeof(state_to_char) ?
1958 state_to_char[field->next_state] : 'X';
1959 if (entry->type == TRACE_WAKE)
1961 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1962 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
1963 SEQ_PUT_HEX_FIELD_RET(s, S);
1964 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1965 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1966 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
1967 SEQ_PUT_HEX_FIELD_RET(s, T);
1972 struct special_entry *field;
1974 trace_assign_type(field, entry);
1976 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
1977 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
1978 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
1982 SEQ_PUT_FIELD_RET(s, newline);
1984 return TRACE_TYPE_HANDLED;
1987 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1989 struct trace_seq *s = &iter->seq;
1990 struct trace_entry *entry;
1994 if (entry->type == TRACE_CONT)
1995 return TRACE_TYPE_HANDLED;
1997 SEQ_PUT_FIELD_RET(s, entry->pid);
1998 SEQ_PUT_FIELD_RET(s, entry->cpu);
1999 SEQ_PUT_FIELD_RET(s, iter->ts);
2001 switch (entry->type) {
2003 struct ftrace_entry *field;
2005 trace_assign_type(field, entry);
2007 SEQ_PUT_FIELD_RET(s, field->ip);
2008 SEQ_PUT_FIELD_RET(s, field->parent_ip);
2012 struct ctx_switch_entry *field;
2014 trace_assign_type(field, entry);
2016 SEQ_PUT_FIELD_RET(s, field->prev_pid);
2017 SEQ_PUT_FIELD_RET(s, field->prev_prio);
2018 SEQ_PUT_FIELD_RET(s, field->prev_state);
2019 SEQ_PUT_FIELD_RET(s, field->next_pid);
2020 SEQ_PUT_FIELD_RET(s, field->next_prio);
2021 SEQ_PUT_FIELD_RET(s, field->next_state);
2026 struct special_entry *field;
2028 trace_assign_type(field, entry);
2030 SEQ_PUT_FIELD_RET(s, field->arg1);
2031 SEQ_PUT_FIELD_RET(s, field->arg2);
2032 SEQ_PUT_FIELD_RET(s, field->arg3);
2039 static int trace_empty(struct trace_iterator *iter)
2043 for_each_tracing_cpu(cpu) {
2044 if (iter->buffer_iter[cpu]) {
2045 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2048 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2056 static enum print_line_t print_trace_line(struct trace_iterator *iter)
2058 enum print_line_t ret;
2060 if (iter->trace && iter->trace->print_line) {
2061 ret = iter->trace->print_line(iter);
2062 if (ret != TRACE_TYPE_UNHANDLED)
2066 if (trace_flags & TRACE_ITER_BIN)
2067 return print_bin_fmt(iter);
2069 if (trace_flags & TRACE_ITER_HEX)
2070 return print_hex_fmt(iter);
2072 if (trace_flags & TRACE_ITER_RAW)
2073 return print_raw_fmt(iter);
2075 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2076 return print_lat_fmt(iter, iter->idx, iter->cpu);
2078 return print_trace_fmt(iter);
2081 static int s_show(struct seq_file *m, void *v)
2083 struct trace_iterator *iter = v;
2085 if (iter->ent == NULL) {
2087 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2090 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2091 /* print nothing if the buffers are empty */
2092 if (trace_empty(iter))
2094 print_trace_header(m, iter);
2095 if (!(trace_flags & TRACE_ITER_VERBOSE))
2096 print_lat_help_header(m);
2098 if (!(trace_flags & TRACE_ITER_VERBOSE))
2099 print_func_help_header(m);
2102 print_trace_line(iter);
2103 trace_print_seq(m, &iter->seq);
2109 static struct seq_operations tracer_seq_ops = {
2116 static struct trace_iterator *
2117 __tracing_open(struct inode *inode, struct file *file, int *ret)
2119 struct trace_iterator *iter;
2123 if (tracing_disabled) {
2128 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2134 mutex_lock(&trace_types_lock);
2135 if (current_trace && current_trace->print_max)
2138 iter->tr = inode->i_private;
2139 iter->trace = current_trace;
2142 /* Annotate start of buffers if we had overruns */
2143 if (ring_buffer_overruns(iter->tr->buffer))
2144 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2147 for_each_tracing_cpu(cpu) {
2149 iter->buffer_iter[cpu] =
2150 ring_buffer_read_start(iter->tr->buffer, cpu);
2152 if (!iter->buffer_iter[cpu])
2156 /* TODO stop tracer */
2157 *ret = seq_open(file, &tracer_seq_ops);
2161 m = file->private_data;
2164 /* stop the trace while dumping */
2167 if (iter->trace && iter->trace->open)
2168 iter->trace->open(iter);
2170 mutex_unlock(&trace_types_lock);
2176 for_each_tracing_cpu(cpu) {
2177 if (iter->buffer_iter[cpu])
2178 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2180 mutex_unlock(&trace_types_lock);
2182 return ERR_PTR(-ENOMEM);
2185 int tracing_open_generic(struct inode *inode, struct file *filp)
2187 if (tracing_disabled)
2190 filp->private_data = inode->i_private;
2194 int tracing_release(struct inode *inode, struct file *file)
2196 struct seq_file *m = (struct seq_file *)file->private_data;
2197 struct trace_iterator *iter = m->private;
2200 mutex_lock(&trace_types_lock);
2201 for_each_tracing_cpu(cpu) {
2202 if (iter->buffer_iter[cpu])
2203 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2206 if (iter->trace && iter->trace->close)
2207 iter->trace->close(iter);
2209 /* reenable tracing if it was previously enabled */
2211 mutex_unlock(&trace_types_lock);
2213 seq_release(inode, file);
2218 static int tracing_open(struct inode *inode, struct file *file)
2222 __tracing_open(inode, file, &ret);
2227 static int tracing_lt_open(struct inode *inode, struct file *file)
2229 struct trace_iterator *iter;
2232 iter = __tracing_open(inode, file, &ret);
2235 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2242 t_next(struct seq_file *m, void *v, loff_t *pos)
2244 struct tracer *t = m->private;
2256 static void *t_start(struct seq_file *m, loff_t *pos)
2258 struct tracer *t = m->private;
2261 mutex_lock(&trace_types_lock);
2262 for (; t && l < *pos; t = t_next(m, t, &l))
2268 static void t_stop(struct seq_file *m, void *p)
2270 mutex_unlock(&trace_types_lock);
2273 static int t_show(struct seq_file *m, void *v)
2275 struct tracer *t = v;
2280 seq_printf(m, "%s", t->name);
2289 static struct seq_operations show_traces_seq_ops = {
2296 static int show_traces_open(struct inode *inode, struct file *file)
2300 if (tracing_disabled)
2303 ret = seq_open(file, &show_traces_seq_ops);
2305 struct seq_file *m = file->private_data;
2306 m->private = trace_types;
2312 static struct file_operations tracing_fops = {
2313 .open = tracing_open,
2315 .llseek = seq_lseek,
2316 .release = tracing_release,
2319 static struct file_operations tracing_lt_fops = {
2320 .open = tracing_lt_open,
2322 .llseek = seq_lseek,
2323 .release = tracing_release,
2326 static struct file_operations show_traces_fops = {
2327 .open = show_traces_open,
2329 .release = seq_release,
2333 * Only trace on a CPU if the bitmask is set:
2335 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2338 * When tracing/tracing_cpu_mask is modified then this holds
2339 * the new bitmask we are about to install:
2341 static cpumask_t tracing_cpumask_new;
2344 * The tracer itself will not take this lock, but still we want
2345 * to provide a consistent cpumask to user-space:
2347 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2350 * Temporary storage for the character representation of the
2351 * CPU bitmask (and one more byte for the newline):
2353 static char mask_str[NR_CPUS + 1];
2356 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2357 size_t count, loff_t *ppos)
2361 mutex_lock(&tracing_cpumask_update_lock);
2363 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2364 if (count - len < 2) {
2368 len += sprintf(mask_str + len, "\n");
2369 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2372 mutex_unlock(&tracing_cpumask_update_lock);
2378 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2379 size_t count, loff_t *ppos)
2383 mutex_lock(&tracing_cpumask_update_lock);
2384 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2388 raw_local_irq_disable();
2389 __raw_spin_lock(&ftrace_max_lock);
2390 for_each_tracing_cpu(cpu) {
2392 * Increase/decrease the disabled counter if we are
2393 * about to flip a bit in the cpumask:
2395 if (cpu_isset(cpu, tracing_cpumask) &&
2396 !cpu_isset(cpu, tracing_cpumask_new)) {
2397 atomic_inc(&global_trace.data[cpu]->disabled);
2399 if (!cpu_isset(cpu, tracing_cpumask) &&
2400 cpu_isset(cpu, tracing_cpumask_new)) {
2401 atomic_dec(&global_trace.data[cpu]->disabled);
2404 __raw_spin_unlock(&ftrace_max_lock);
2405 raw_local_irq_enable();
2407 tracing_cpumask = tracing_cpumask_new;
2409 mutex_unlock(&tracing_cpumask_update_lock);
2414 mutex_unlock(&tracing_cpumask_update_lock);
2419 static struct file_operations tracing_cpumask_fops = {
2420 .open = tracing_open_generic,
2421 .read = tracing_cpumask_read,
2422 .write = tracing_cpumask_write,
2426 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2427 size_t cnt, loff_t *ppos)
2434 /* calulate max size */
2435 for (i = 0; trace_options[i]; i++) {
2436 len += strlen(trace_options[i]);
2437 len += 3; /* "no" and space */
2440 /* +2 for \n and \0 */
2441 buf = kmalloc(len + 2, GFP_KERNEL);
2445 for (i = 0; trace_options[i]; i++) {
2446 if (trace_flags & (1 << i))
2447 r += sprintf(buf + r, "%s ", trace_options[i]);
2449 r += sprintf(buf + r, "no%s ", trace_options[i]);
2452 r += sprintf(buf + r, "\n");
2453 WARN_ON(r >= len + 2);
2455 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2463 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2464 size_t cnt, loff_t *ppos)
2471 if (cnt >= sizeof(buf))
2474 if (copy_from_user(&buf, ubuf, cnt))
2479 if (strncmp(buf, "no", 2) == 0) {
2484 for (i = 0; trace_options[i]; i++) {
2485 int len = strlen(trace_options[i]);
2487 if (strncmp(cmp, trace_options[i], len) == 0) {
2489 trace_flags &= ~(1 << i);
2491 trace_flags |= (1 << i);
2496 * If no option could be set, return an error:
2498 if (!trace_options[i])
2506 static struct file_operations tracing_iter_fops = {
2507 .open = tracing_open_generic,
2508 .read = tracing_trace_options_read,
2509 .write = tracing_trace_options_write,
2512 static const char readme_msg[] =
2513 "tracing mini-HOWTO:\n\n"
2515 "# mount -t debugfs nodev /debug\n\n"
2516 "# cat /debug/tracing/available_tracers\n"
2517 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2518 "# cat /debug/tracing/current_tracer\n"
2520 "# echo sched_switch > /debug/tracing/current_tracer\n"
2521 "# cat /debug/tracing/current_tracer\n"
2523 "# cat /debug/tracing/trace_options\n"
2524 "noprint-parent nosym-offset nosym-addr noverbose\n"
2525 "# echo print-parent > /debug/tracing/trace_options\n"
2526 "# echo 1 > /debug/tracing/tracing_enabled\n"
2527 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2528 "echo 0 > /debug/tracing/tracing_enabled\n"
2532 tracing_readme_read(struct file *filp, char __user *ubuf,
2533 size_t cnt, loff_t *ppos)
2535 return simple_read_from_buffer(ubuf, cnt, ppos,
2536 readme_msg, strlen(readme_msg));
2539 static struct file_operations tracing_readme_fops = {
2540 .open = tracing_open_generic,
2541 .read = tracing_readme_read,
2545 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2546 size_t cnt, loff_t *ppos)
2551 r = sprintf(buf, "%u\n", tracer_enabled);
2552 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2556 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2557 size_t cnt, loff_t *ppos)
2559 struct trace_array *tr = filp->private_data;
2564 if (cnt >= sizeof(buf))
2567 if (copy_from_user(&buf, ubuf, cnt))
2572 ret = strict_strtoul(buf, 10, &val);
2578 mutex_lock(&trace_types_lock);
2579 if (tracer_enabled ^ val) {
2582 if (current_trace->start)
2583 current_trace->start(tr);
2588 if (current_trace->stop)
2589 current_trace->stop(tr);
2592 mutex_unlock(&trace_types_lock);
2600 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2601 size_t cnt, loff_t *ppos)
2603 char buf[max_tracer_type_len+2];
2606 mutex_lock(&trace_types_lock);
2608 r = sprintf(buf, "%s\n", current_trace->name);
2610 r = sprintf(buf, "\n");
2611 mutex_unlock(&trace_types_lock);
2613 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2616 static int tracing_set_tracer(char *buf)
2618 struct trace_array *tr = &global_trace;
2622 mutex_lock(&trace_types_lock);
2623 for (t = trace_types; t; t = t->next) {
2624 if (strcmp(t->name, buf) == 0)
2631 if (t == current_trace)
2634 trace_branch_disable();
2635 if (current_trace && current_trace->reset)
2636 current_trace->reset(tr);
2645 trace_branch_enable(tr);
2647 mutex_unlock(&trace_types_lock);
2653 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2654 size_t cnt, loff_t *ppos)
2656 char buf[max_tracer_type_len+1];
2663 if (cnt > max_tracer_type_len)
2664 cnt = max_tracer_type_len;
2666 if (copy_from_user(&buf, ubuf, cnt))
2671 /* strip ending whitespace. */
2672 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2675 err = tracing_set_tracer(buf);
2685 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2686 size_t cnt, loff_t *ppos)
2688 unsigned long *ptr = filp->private_data;
2692 r = snprintf(buf, sizeof(buf), "%ld\n",
2693 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2694 if (r > sizeof(buf))
2696 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2700 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2701 size_t cnt, loff_t *ppos)
2703 long *ptr = filp->private_data;
2708 if (cnt >= sizeof(buf))
2711 if (copy_from_user(&buf, ubuf, cnt))
2716 ret = strict_strtoul(buf, 10, &val);
2725 static atomic_t tracing_reader;
2727 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2729 struct trace_iterator *iter;
2731 if (tracing_disabled)
2734 /* We only allow for reader of the pipe */
2735 if (atomic_inc_return(&tracing_reader) != 1) {
2736 atomic_dec(&tracing_reader);
2740 /* create a buffer to store the information to pass to userspace */
2741 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2745 mutex_lock(&trace_types_lock);
2747 /* trace pipe does not show start of buffer */
2748 cpus_setall(iter->started);
2750 iter->tr = &global_trace;
2751 iter->trace = current_trace;
2752 filp->private_data = iter;
2754 if (iter->trace->pipe_open)
2755 iter->trace->pipe_open(iter);
2756 mutex_unlock(&trace_types_lock);
2761 static int tracing_release_pipe(struct inode *inode, struct file *file)
2763 struct trace_iterator *iter = file->private_data;
2766 atomic_dec(&tracing_reader);
2772 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2774 struct trace_iterator *iter = filp->private_data;
2776 if (trace_flags & TRACE_ITER_BLOCK) {
2778 * Always select as readable when in blocking mode
2780 return POLLIN | POLLRDNORM;
2782 if (!trace_empty(iter))
2783 return POLLIN | POLLRDNORM;
2784 poll_wait(filp, &trace_wait, poll_table);
2785 if (!trace_empty(iter))
2786 return POLLIN | POLLRDNORM;
2796 tracing_read_pipe(struct file *filp, char __user *ubuf,
2797 size_t cnt, loff_t *ppos)
2799 struct trace_iterator *iter = filp->private_data;
2802 /* return any leftover data */
2803 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2807 trace_seq_reset(&iter->seq);
2809 mutex_lock(&trace_types_lock);
2810 if (iter->trace->read) {
2811 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2818 while (trace_empty(iter)) {
2820 if ((filp->f_flags & O_NONBLOCK)) {
2826 * This is a make-shift waitqueue. The reason we don't use
2827 * an actual wait queue is because:
2828 * 1) we only ever have one waiter
2829 * 2) the tracing, traces all functions, we don't want
2830 * the overhead of calling wake_up and friends
2831 * (and tracing them too)
2832 * Anyway, this is really very primitive wakeup.
2834 set_current_state(TASK_INTERRUPTIBLE);
2835 iter->tr->waiter = current;
2837 mutex_unlock(&trace_types_lock);
2839 /* sleep for 100 msecs, and try again. */
2840 schedule_timeout(HZ/10);
2842 mutex_lock(&trace_types_lock);
2844 iter->tr->waiter = NULL;
2846 if (signal_pending(current)) {
2851 if (iter->trace != current_trace)
2855 * We block until we read something and tracing is disabled.
2856 * We still block if tracing is disabled, but we have never
2857 * read anything. This allows a user to cat this file, and
2858 * then enable tracing. But after we have read something,
2859 * we give an EOF when tracing is again disabled.
2861 * iter->pos will be 0 if we haven't read anything.
2863 if (!tracer_enabled && iter->pos)
2869 /* stop when tracing is finished */
2870 if (trace_empty(iter))
2873 if (cnt >= PAGE_SIZE)
2874 cnt = PAGE_SIZE - 1;
2876 /* reset all but tr, trace, and overruns */
2877 memset(&iter->seq, 0,
2878 sizeof(struct trace_iterator) -
2879 offsetof(struct trace_iterator, seq));
2882 while (find_next_entry_inc(iter) != NULL) {
2883 enum print_line_t ret;
2884 int len = iter->seq.len;
2886 ret = print_trace_line(iter);
2887 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2888 /* don't print partial lines */
2889 iter->seq.len = len;
2893 trace_consume(iter);
2895 if (iter->seq.len >= cnt)
2899 /* Now copy what we have to the user */
2900 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2901 if (iter->seq.readpos >= iter->seq.len)
2902 trace_seq_reset(&iter->seq);
2905 * If there was nothing to send to user, inspite of consuming trace
2906 * entries, go back to wait for more entries.
2912 mutex_unlock(&trace_types_lock);
2918 tracing_entries_read(struct file *filp, char __user *ubuf,
2919 size_t cnt, loff_t *ppos)
2921 struct trace_array *tr = filp->private_data;
2925 r = sprintf(buf, "%lu\n", tr->entries >> 10);
2926 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2930 tracing_entries_write(struct file *filp, const char __user *ubuf,
2931 size_t cnt, loff_t *ppos)
2937 if (cnt >= sizeof(buf))
2940 if (copy_from_user(&buf, ubuf, cnt))
2945 ret = strict_strtoul(buf, 10, &val);
2949 /* must have at least 1 entry */
2953 mutex_lock(&trace_types_lock);
2957 /* disable all cpu buffers */
2958 for_each_tracing_cpu(cpu) {
2959 if (global_trace.data[cpu])
2960 atomic_inc(&global_trace.data[cpu]->disabled);
2961 if (max_tr.data[cpu])
2962 atomic_inc(&max_tr.data[cpu]->disabled);
2965 /* value is in KB */
2968 if (val != global_trace.entries) {
2969 ret = ring_buffer_resize(global_trace.buffer, val);
2975 ret = ring_buffer_resize(max_tr.buffer, val);
2979 r = ring_buffer_resize(global_trace.buffer,
2980 global_trace.entries);
2982 /* AARGH! We are left with different
2983 * size max buffer!!!! */
2985 tracing_disabled = 1;
2990 global_trace.entries = val;
2995 /* If check pages failed, return ENOMEM */
2996 if (tracing_disabled)
2999 for_each_tracing_cpu(cpu) {
3000 if (global_trace.data[cpu])
3001 atomic_dec(&global_trace.data[cpu]->disabled);
3002 if (max_tr.data[cpu])
3003 atomic_dec(&max_tr.data[cpu]->disabled);
3007 max_tr.entries = global_trace.entries;
3008 mutex_unlock(&trace_types_lock);
3013 static int mark_printk(const char *fmt, ...)
3017 va_start(args, fmt);
3018 ret = trace_vprintk(0, fmt, args);
3024 tracing_mark_write(struct file *filp, const char __user *ubuf,
3025 size_t cnt, loff_t *fpos)
3030 if (tracing_disabled)
3033 if (cnt > TRACE_BUF_SIZE)
3034 cnt = TRACE_BUF_SIZE;
3036 buf = kmalloc(cnt + 1, GFP_KERNEL);
3040 if (copy_from_user(buf, ubuf, cnt)) {
3045 /* Cut from the first nil or newline. */
3047 end = strchr(buf, '\n');
3051 cnt = mark_printk("%s\n", buf);
3058 static struct file_operations tracing_max_lat_fops = {
3059 .open = tracing_open_generic,
3060 .read = tracing_max_lat_read,
3061 .write = tracing_max_lat_write,
3064 static struct file_operations tracing_ctrl_fops = {
3065 .open = tracing_open_generic,
3066 .read = tracing_ctrl_read,
3067 .write = tracing_ctrl_write,
3070 static struct file_operations set_tracer_fops = {
3071 .open = tracing_open_generic,
3072 .read = tracing_set_trace_read,
3073 .write = tracing_set_trace_write,
3076 static struct file_operations tracing_pipe_fops = {
3077 .open = tracing_open_pipe,
3078 .poll = tracing_poll_pipe,
3079 .read = tracing_read_pipe,
3080 .release = tracing_release_pipe,
3083 static struct file_operations tracing_entries_fops = {
3084 .open = tracing_open_generic,
3085 .read = tracing_entries_read,
3086 .write = tracing_entries_write,
3089 static struct file_operations tracing_mark_fops = {
3090 .open = tracing_open_generic,
3091 .write = tracing_mark_write,
3094 #ifdef CONFIG_DYNAMIC_FTRACE
3096 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3102 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3103 size_t cnt, loff_t *ppos)
3105 static char ftrace_dyn_info_buffer[1024];
3106 static DEFINE_MUTEX(dyn_info_mutex);
3107 unsigned long *p = filp->private_data;
3108 char *buf = ftrace_dyn_info_buffer;
3109 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3112 mutex_lock(&dyn_info_mutex);
3113 r = sprintf(buf, "%ld ", *p);
3115 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3118 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3120 mutex_unlock(&dyn_info_mutex);
3125 static struct file_operations tracing_dyn_info_fops = {
3126 .open = tracing_open_generic,
3127 .read = tracing_read_dyn_info,
3131 static struct dentry *d_tracer;
3133 struct dentry *tracing_init_dentry(void)
3140 d_tracer = debugfs_create_dir("tracing", NULL);
3142 if (!d_tracer && !once) {
3144 pr_warning("Could not create debugfs directory 'tracing'\n");
3151 #ifdef CONFIG_FTRACE_SELFTEST
3152 /* Let selftest have access to static functions in this file */
3153 #include "trace_selftest.c"
3156 static __init int tracer_init_debugfs(void)
3158 struct dentry *d_tracer;
3159 struct dentry *entry;
3161 d_tracer = tracing_init_dentry();
3163 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3164 &global_trace, &tracing_ctrl_fops);
3166 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3168 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3169 NULL, &tracing_iter_fops);
3171 pr_warning("Could not create debugfs 'trace_options' entry\n");
3173 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3174 NULL, &tracing_cpumask_fops);
3176 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3178 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3179 &global_trace, &tracing_lt_fops);
3181 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3183 entry = debugfs_create_file("trace", 0444, d_tracer,
3184 &global_trace, &tracing_fops);
3186 pr_warning("Could not create debugfs 'trace' entry\n");
3188 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3189 &global_trace, &show_traces_fops);
3191 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3193 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3194 &global_trace, &set_tracer_fops);
3196 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3198 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3199 &tracing_max_latency,
3200 &tracing_max_lat_fops);
3202 pr_warning("Could not create debugfs "
3203 "'tracing_max_latency' entry\n");
3205 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3206 &tracing_thresh, &tracing_max_lat_fops);
3208 pr_warning("Could not create debugfs "
3209 "'tracing_thresh' entry\n");
3210 entry = debugfs_create_file("README", 0644, d_tracer,
3211 NULL, &tracing_readme_fops);
3213 pr_warning("Could not create debugfs 'README' entry\n");
3215 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3216 NULL, &tracing_pipe_fops);
3218 pr_warning("Could not create debugfs "
3219 "'trace_pipe' entry\n");
3221 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3222 &global_trace, &tracing_entries_fops);
3224 pr_warning("Could not create debugfs "
3225 "'buffer_size_kb' entry\n");
3227 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3228 NULL, &tracing_mark_fops);
3230 pr_warning("Could not create debugfs "
3231 "'trace_marker' entry\n");
3233 #ifdef CONFIG_DYNAMIC_FTRACE
3234 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3235 &ftrace_update_tot_cnt,
3236 &tracing_dyn_info_fops);
3238 pr_warning("Could not create debugfs "
3239 "'dyn_ftrace_total_info' entry\n");
3241 #ifdef CONFIG_SYSPROF_TRACER
3242 init_tracer_sysprof_debugfs(d_tracer);
3247 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3249 static DEFINE_SPINLOCK(trace_buf_lock);
3250 static char trace_buf[TRACE_BUF_SIZE];
3252 struct ring_buffer_event *event;
3253 struct trace_array *tr = &global_trace;
3254 struct trace_array_cpu *data;
3255 struct print_entry *entry;
3256 unsigned long flags, irq_flags;
3257 int cpu, len = 0, size, pc;
3259 if (tracing_disabled)
3262 pc = preempt_count();
3263 preempt_disable_notrace();
3264 cpu = raw_smp_processor_id();
3265 data = tr->data[cpu];
3267 if (unlikely(atomic_read(&data->disabled)))
3270 spin_lock_irqsave(&trace_buf_lock, flags);
3271 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3273 len = min(len, TRACE_BUF_SIZE-1);
3276 size = sizeof(*entry) + len + 1;
3277 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3280 entry = ring_buffer_event_data(event);
3281 tracing_generic_entry_update(&entry->ent, flags, pc);
3282 entry->ent.type = TRACE_PRINT;
3285 memcpy(&entry->buf, trace_buf, len);
3286 entry->buf[len] = 0;
3287 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3290 spin_unlock_irqrestore(&trace_buf_lock, flags);
3293 preempt_enable_notrace();
3297 EXPORT_SYMBOL_GPL(trace_vprintk);
3299 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3304 if (!(trace_flags & TRACE_ITER_PRINTK))
3308 ret = trace_vprintk(ip, fmt, ap);
3312 EXPORT_SYMBOL_GPL(__ftrace_printk);
3314 static int trace_panic_handler(struct notifier_block *this,
3315 unsigned long event, void *unused)
3317 if (ftrace_dump_on_oops)
3322 static struct notifier_block trace_panic_notifier = {
3323 .notifier_call = trace_panic_handler,
3325 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3328 static int trace_die_handler(struct notifier_block *self,
3334 if (ftrace_dump_on_oops)
3343 static struct notifier_block trace_die_notifier = {
3344 .notifier_call = trace_die_handler,
3349 * printk is set to max of 1024, we really don't need it that big.
3350 * Nothing should be printing 1000 characters anyway.
3352 #define TRACE_MAX_PRINT 1000
3355 * Define here KERN_TRACE so that we have one place to modify
3356 * it if we decide to change what log level the ftrace dump
3359 #define KERN_TRACE KERN_INFO
3362 trace_printk_seq(struct trace_seq *s)
3364 /* Probably should print a warning here. */
3368 /* should be zero ended, but we are paranoid. */
3369 s->buffer[s->len] = 0;
3371 printk(KERN_TRACE "%s", s->buffer);
3376 void ftrace_dump(void)
3378 static DEFINE_SPINLOCK(ftrace_dump_lock);
3379 /* use static because iter can be a bit big for the stack */
3380 static struct trace_iterator iter;
3381 static cpumask_t mask;
3382 static int dump_ran;
3383 unsigned long flags;
3387 spin_lock_irqsave(&ftrace_dump_lock, flags);
3393 /* No turning back! */
3396 for_each_tracing_cpu(cpu) {
3397 atomic_inc(&global_trace.data[cpu]->disabled);
3400 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3402 iter.tr = &global_trace;
3403 iter.trace = current_trace;
3406 * We need to stop all tracing on all CPUS to read the
3407 * the next buffer. This is a bit expensive, but is
3408 * not done often. We fill all what we can read,
3409 * and then release the locks again.
3414 while (!trace_empty(&iter)) {
3417 printk(KERN_TRACE "---------------------------------\n");
3421 /* reset all but tr, trace, and overruns */
3422 memset(&iter.seq, 0,
3423 sizeof(struct trace_iterator) -
3424 offsetof(struct trace_iterator, seq));
3425 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3428 if (find_next_entry_inc(&iter) != NULL) {
3429 print_trace_line(&iter);
3430 trace_consume(&iter);
3433 trace_printk_seq(&iter.seq);
3437 printk(KERN_TRACE " (ftrace buffer empty)\n");
3439 printk(KERN_TRACE "---------------------------------\n");
3442 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3445 __init static int tracer_alloc_buffers(void)
3447 struct trace_array_cpu *data;
3450 /* TODO: make the number of buffers hot pluggable with CPUS */
3451 tracing_buffer_mask = cpu_possible_map;
3453 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3454 TRACE_BUFFER_FLAGS);
3455 if (!global_trace.buffer) {
3456 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3460 global_trace.entries = ring_buffer_size(global_trace.buffer);
3462 #ifdef CONFIG_TRACER_MAX_TRACE
3463 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3464 TRACE_BUFFER_FLAGS);
3465 if (!max_tr.buffer) {
3466 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3468 ring_buffer_free(global_trace.buffer);
3471 max_tr.entries = ring_buffer_size(max_tr.buffer);
3472 WARN_ON(max_tr.entries != global_trace.entries);
3475 /* Allocate the first page for all buffers */
3476 for_each_tracing_cpu(i) {
3477 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3478 max_tr.data[i] = &per_cpu(max_data, i);
3481 trace_init_cmdlines();
3483 register_tracer(&nop_trace);
3484 #ifdef CONFIG_BOOT_TRACER
3485 register_tracer(&boot_tracer);
3486 current_trace = &boot_tracer;
3487 current_trace->init(&global_trace);
3489 current_trace = &nop_trace;
3492 /* All seems OK, enable tracing */
3493 tracing_disabled = 0;
3495 atomic_notifier_chain_register(&panic_notifier_list,
3496 &trace_panic_notifier);
3498 register_die_notifier(&trace_die_notifier);
3502 early_initcall(tracer_alloc_buffers);
3503 fs_initcall(tracer_init_debugfs);