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 /* For tracers that don't implement custom flags */
47 static struct tracer_opt dummy_tracer_opt[] = {
51 static struct tracer_flags dummy_tracer_flags = {
53 .opts = dummy_tracer_opt
56 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
62 * Kill all tracing for good (never come back).
63 * It is initialized to 1 but will turn to zero if the initialization
64 * of the tracer is successful. But that is the only place that sets
67 int tracing_disabled = 1;
69 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
71 static inline void ftrace_disable_cpu(void)
74 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
77 static inline void ftrace_enable_cpu(void)
79 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
83 static cpumask_t __read_mostly tracing_buffer_mask;
85 #define for_each_tracing_cpu(cpu) \
86 for_each_cpu_mask(cpu, tracing_buffer_mask)
89 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
91 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
92 * is set, then ftrace_dump is called. This will output the contents
93 * of the ftrace buffers to the console. This is very useful for
94 * capturing traces that lead to crashes and outputing it to a
97 * It is default off, but you can enable it with either specifying
98 * "ftrace_dump_on_oops" in the kernel command line, or setting
99 * /proc/sys/kernel/ftrace_dump_on_oops to true.
101 int ftrace_dump_on_oops;
103 static int tracing_set_tracer(char *buf);
105 static int __init set_ftrace(char *str)
107 tracing_set_tracer(str);
110 __setup("ftrace", set_ftrace);
112 static int __init set_ftrace_dump_on_oops(char *str)
114 ftrace_dump_on_oops = 1;
117 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
120 ns2usecs(cycle_t nsec)
127 cycle_t ftrace_now(int cpu)
129 u64 ts = ring_buffer_time_stamp(cpu);
130 ring_buffer_normalize_time_stamp(cpu, &ts);
135 * The global_trace is the descriptor that holds the tracing
136 * buffers for the live tracing. For each CPU, it contains
137 * a link list of pages that will store trace entries. The
138 * page descriptor of the pages in the memory is used to hold
139 * the link list by linking the lru item in the page descriptor
140 * to each of the pages in the buffer per CPU.
142 * For each active CPU there is a data field that holds the
143 * pages for the buffer for that CPU. Each CPU has the same number
144 * of pages allocated for its buffer.
146 static struct trace_array global_trace;
148 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
151 * The max_tr is used to snapshot the global_trace when a maximum
152 * latency is reached. Some tracers will use this to store a maximum
153 * trace while it continues examining live traces.
155 * The buffers for the max_tr are set up the same as the global_trace.
156 * When a snapshot is taken, the link list of the max_tr is swapped
157 * with the link list of the global_trace and the buffers are reset for
158 * the global_trace so the tracing can continue.
160 static struct trace_array max_tr;
162 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
164 /* tracer_enabled is used to toggle activation of a tracer */
165 static int tracer_enabled = 1;
168 * tracing_is_enabled - return tracer_enabled status
170 * This function is used by other tracers to know the status
171 * of the tracer_enabled flag. Tracers may use this function
172 * to know if it should enable their features when starting
173 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
175 int tracing_is_enabled(void)
177 return tracer_enabled;
180 /* function tracing enabled */
181 int ftrace_function_enabled;
184 * trace_buf_size is the size in bytes that is allocated
185 * for a buffer. Note, the number of bytes is always rounded
188 * This number is purposely set to a low number of 16384.
189 * If the dump on oops happens, it will be much appreciated
190 * to not have to wait for all that output. Anyway this can be
191 * boot time and run time configurable.
193 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
195 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
197 /* trace_types holds a link list of available tracers. */
198 static struct tracer *trace_types __read_mostly;
200 /* current_trace points to the tracer that is currently active */
201 static struct tracer *current_trace __read_mostly;
204 * max_tracer_type_len is used to simplify the allocating of
205 * buffers to read userspace tracer names. We keep track of
206 * the longest tracer name registered.
208 static int max_tracer_type_len;
211 * trace_types_lock is used to protect the trace_types list.
212 * This lock is also used to keep user access serialized.
213 * Accesses from userspace will grab this lock while userspace
214 * activities happen inside the kernel.
216 static DEFINE_MUTEX(trace_types_lock);
218 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
219 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
221 /* trace_flags holds trace_options default values */
222 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
226 * trace_wake_up - wake up tasks waiting for trace input
228 * Simply wakes up any task that is blocked on the trace_wait
229 * queue. These is used with trace_poll for tasks polling the trace.
231 void trace_wake_up(void)
234 * The runqueue_is_locked() can fail, but this is the best we
237 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
238 wake_up(&trace_wait);
241 static int __init set_buf_size(char *str)
243 unsigned long buf_size;
248 ret = strict_strtoul(str, 0, &buf_size);
249 /* nr_entries can not be zero */
250 if (ret < 0 || buf_size == 0)
252 trace_buf_size = buf_size;
255 __setup("trace_buf_size=", set_buf_size);
257 unsigned long nsecs_to_usecs(unsigned long nsecs)
262 /* These must match the bit postions in trace_iterator_flags */
263 static const char *trace_options[] = {
282 * ftrace_max_lock is used to protect the swapping of buffers
283 * when taking a max snapshot. The buffers themselves are
284 * protected by per_cpu spinlocks. But the action of the swap
285 * needs its own lock.
287 * This is defined as a raw_spinlock_t in order to help
288 * with performance when lockdep debugging is enabled.
290 static raw_spinlock_t ftrace_max_lock =
291 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
294 * Copy the new maximum trace into the separate maximum-trace
295 * structure. (this way the maximum trace is permanently saved,
296 * for later retrieval via /debugfs/tracing/latency_trace)
299 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
301 struct trace_array_cpu *data = tr->data[cpu];
304 max_tr.time_start = data->preempt_timestamp;
306 data = max_tr.data[cpu];
307 data->saved_latency = tracing_max_latency;
309 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
310 data->pid = tsk->pid;
311 data->uid = tsk->uid;
312 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
313 data->policy = tsk->policy;
314 data->rt_priority = tsk->rt_priority;
316 /* record this tasks comm */
317 tracing_record_cmdline(current);
321 * trace_seq_printf - sequence printing of trace information
322 * @s: trace sequence descriptor
323 * @fmt: printf format string
325 * The tracer may use either sequence operations or its own
326 * copy to user routines. To simplify formating of a trace
327 * trace_seq_printf is used to store strings into a special
328 * buffer (@s). Then the output may be either used by
329 * the sequencer or pulled into another buffer.
332 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
334 int len = (PAGE_SIZE - 1) - s->len;
342 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
345 /* If we can't write it all, don't bother writing anything */
355 * trace_seq_puts - trace sequence printing of simple string
356 * @s: trace sequence descriptor
357 * @str: simple string to record
359 * The tracer may use either the sequence operations or its own
360 * copy to user routines. This function records a simple string
361 * into a special buffer (@s) for later retrieval by a sequencer
362 * or other mechanism.
365 trace_seq_puts(struct trace_seq *s, const char *str)
367 int len = strlen(str);
369 if (len > ((PAGE_SIZE - 1) - s->len))
372 memcpy(s->buffer + s->len, str, len);
379 trace_seq_putc(struct trace_seq *s, unsigned char c)
381 if (s->len >= (PAGE_SIZE - 1))
384 s->buffer[s->len++] = c;
390 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
392 if (len > ((PAGE_SIZE - 1) - s->len))
395 memcpy(s->buffer + s->len, mem, len);
401 #define MAX_MEMHEX_BYTES 8
402 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
405 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
407 unsigned char hex[HEX_CHARS];
408 unsigned char *data = mem;
412 for (i = 0, j = 0; i < len; i++) {
414 for (i = len-1, j = 0; i >= 0; i--) {
416 hex[j++] = hex_asc_hi(data[i]);
417 hex[j++] = hex_asc_lo(data[i]);
421 return trace_seq_putmem(s, hex, j);
425 trace_seq_reset(struct trace_seq *s)
431 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
436 if (s->len <= s->readpos)
439 len = s->len - s->readpos;
442 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
451 trace_print_seq(struct seq_file *m, struct trace_seq *s)
453 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
456 seq_puts(m, s->buffer);
462 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
464 * @tsk: the task with the latency
465 * @cpu: The cpu that initiated the trace.
467 * Flip the buffers between the @tr and the max_tr and record information
468 * about which task was the cause of this latency.
471 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
473 struct ring_buffer *buf = tr->buffer;
475 WARN_ON_ONCE(!irqs_disabled());
476 __raw_spin_lock(&ftrace_max_lock);
478 tr->buffer = max_tr.buffer;
481 ftrace_disable_cpu();
482 ring_buffer_reset(tr->buffer);
485 __update_max_tr(tr, tsk, cpu);
486 __raw_spin_unlock(&ftrace_max_lock);
490 * update_max_tr_single - only copy one trace over, and reset the rest
492 * @tsk - task with the latency
493 * @cpu - the cpu of the buffer to copy.
495 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
498 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
502 WARN_ON_ONCE(!irqs_disabled());
503 __raw_spin_lock(&ftrace_max_lock);
505 ftrace_disable_cpu();
507 ring_buffer_reset(max_tr.buffer);
508 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
514 __update_max_tr(tr, tsk, cpu);
515 __raw_spin_unlock(&ftrace_max_lock);
519 * register_tracer - register a tracer with the ftrace system.
520 * @type - the plugin for the tracer
522 * Register a new plugin tracer.
524 int register_tracer(struct tracer *type)
531 pr_info("Tracer must have a name\n");
536 * When this gets called we hold the BKL which means that
537 * preemption is disabled. Various trace selftests however
538 * need to disable and enable preemption for successful tests.
539 * So we drop the BKL here and grab it after the tests again.
542 mutex_lock(&trace_types_lock);
544 for (t = trace_types; t; t = t->next) {
545 if (strcmp(type->name, t->name) == 0) {
547 pr_info("Trace %s already registered\n",
555 type->set_flag = &dummy_set_flag;
557 type->flags = &dummy_tracer_flags;
559 if (!type->flags->opts)
560 type->flags->opts = dummy_tracer_opt;
562 #ifdef CONFIG_FTRACE_STARTUP_TEST
563 if (type->selftest) {
564 struct tracer *saved_tracer = current_trace;
565 struct trace_array *tr = &global_trace;
568 * Run a selftest on this tracer.
569 * Here we reset the trace buffer, and set the current
570 * tracer to be this tracer. The tracer can then run some
571 * internal tracing to verify that everything is in order.
572 * If we fail, we do not register this tracer.
574 for_each_tracing_cpu(i)
575 tracing_reset(tr, i);
577 current_trace = type;
578 /* the test is responsible for initializing and enabling */
579 pr_info("Testing tracer %s: ", type->name);
580 ret = type->selftest(type, tr);
581 /* the test is responsible for resetting too */
582 current_trace = saved_tracer;
584 printk(KERN_CONT "FAILED!\n");
587 /* Only reset on passing, to avoid touching corrupted buffers */
588 for_each_tracing_cpu(i)
589 tracing_reset(tr, i);
591 printk(KERN_CONT "PASSED\n");
595 type->next = trace_types;
597 len = strlen(type->name);
598 if (len > max_tracer_type_len)
599 max_tracer_type_len = len;
602 mutex_unlock(&trace_types_lock);
608 void unregister_tracer(struct tracer *type)
613 mutex_lock(&trace_types_lock);
614 for (t = &trace_types; *t; t = &(*t)->next) {
618 pr_info("Trace %s not registered\n", type->name);
623 if (strlen(type->name) != max_tracer_type_len)
626 max_tracer_type_len = 0;
627 for (t = &trace_types; *t; t = &(*t)->next) {
628 len = strlen((*t)->name);
629 if (len > max_tracer_type_len)
630 max_tracer_type_len = len;
633 mutex_unlock(&trace_types_lock);
636 void tracing_reset(struct trace_array *tr, int cpu)
638 ftrace_disable_cpu();
639 ring_buffer_reset_cpu(tr->buffer, cpu);
643 #define SAVED_CMDLINES 128
644 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
645 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
646 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
647 static int cmdline_idx;
648 static DEFINE_SPINLOCK(trace_cmdline_lock);
650 /* temporary disable recording */
651 atomic_t trace_record_cmdline_disabled __read_mostly;
653 static void trace_init_cmdlines(void)
655 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
656 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
660 static int trace_stop_count;
661 static DEFINE_SPINLOCK(tracing_start_lock);
664 * tracing_start - quick start of the tracer
666 * If tracing is enabled but was stopped by tracing_stop,
667 * this will start the tracer back up.
669 void tracing_start(void)
671 struct ring_buffer *buffer;
674 if (tracing_disabled)
677 spin_lock_irqsave(&tracing_start_lock, flags);
678 if (--trace_stop_count)
681 if (trace_stop_count < 0) {
682 /* Someone screwed up their debugging */
684 trace_stop_count = 0;
689 buffer = global_trace.buffer;
691 ring_buffer_record_enable(buffer);
693 buffer = max_tr.buffer;
695 ring_buffer_record_enable(buffer);
699 spin_unlock_irqrestore(&tracing_start_lock, flags);
703 * tracing_stop - quick stop of the tracer
705 * Light weight way to stop tracing. Use in conjunction with
708 void tracing_stop(void)
710 struct ring_buffer *buffer;
714 spin_lock_irqsave(&tracing_start_lock, flags);
715 if (trace_stop_count++)
718 buffer = global_trace.buffer;
720 ring_buffer_record_disable(buffer);
722 buffer = max_tr.buffer;
724 ring_buffer_record_disable(buffer);
727 spin_unlock_irqrestore(&tracing_start_lock, flags);
730 void trace_stop_cmdline_recording(void);
732 static void trace_save_cmdline(struct task_struct *tsk)
737 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
741 * It's not the end of the world if we don't get
742 * the lock, but we also don't want to spin
743 * nor do we want to disable interrupts,
744 * so if we miss here, then better luck next time.
746 if (!spin_trylock(&trace_cmdline_lock))
749 idx = map_pid_to_cmdline[tsk->pid];
750 if (idx >= SAVED_CMDLINES) {
751 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
753 map = map_cmdline_to_pid[idx];
754 if (map <= PID_MAX_DEFAULT)
755 map_pid_to_cmdline[map] = (unsigned)-1;
757 map_pid_to_cmdline[tsk->pid] = idx;
762 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
764 spin_unlock(&trace_cmdline_lock);
767 static char *trace_find_cmdline(int pid)
769 char *cmdline = "<...>";
775 if (pid > PID_MAX_DEFAULT)
778 map = map_pid_to_cmdline[pid];
779 if (map >= SAVED_CMDLINES)
782 cmdline = saved_cmdlines[map];
788 void tracing_record_cmdline(struct task_struct *tsk)
790 if (atomic_read(&trace_record_cmdline_disabled))
793 trace_save_cmdline(tsk);
797 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
800 struct task_struct *tsk = current;
802 entry->preempt_count = pc & 0xff;
803 entry->pid = (tsk) ? tsk->pid : 0;
805 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
806 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
808 TRACE_FLAG_IRQS_NOSUPPORT |
810 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
811 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
812 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
816 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
817 unsigned long ip, unsigned long parent_ip, unsigned long flags,
820 struct ring_buffer_event *event;
821 struct ftrace_entry *entry;
822 unsigned long irq_flags;
824 /* If we are reading the ring buffer, don't trace */
825 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
828 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
832 entry = ring_buffer_event_data(event);
833 tracing_generic_entry_update(&entry->ent, flags, pc);
834 entry->ent.type = TRACE_FN;
836 entry->parent_ip = parent_ip;
837 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
840 #ifdef CONFIG_FUNCTION_RET_TRACER
841 static void __trace_function_return(struct trace_array *tr,
842 struct trace_array_cpu *data,
843 struct ftrace_retfunc *trace,
847 struct ring_buffer_event *event;
848 struct ftrace_ret_entry *entry;
849 unsigned long irq_flags;
851 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
854 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
858 entry = ring_buffer_event_data(event);
859 tracing_generic_entry_update(&entry->ent, flags, pc);
860 entry->ent.type = TRACE_FN_RET;
861 entry->ip = trace->func;
862 entry->parent_ip = trace->ret;
863 entry->rettime = trace->rettime;
864 entry->calltime = trace->calltime;
865 entry->overrun = trace->overrun;
866 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
871 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
872 unsigned long ip, unsigned long parent_ip, unsigned long flags,
875 if (likely(!atomic_read(&data->disabled)))
876 trace_function(tr, data, ip, parent_ip, flags, pc);
879 static void ftrace_trace_stack(struct trace_array *tr,
880 struct trace_array_cpu *data,
884 #ifdef CONFIG_STACKTRACE
885 struct ring_buffer_event *event;
886 struct stack_entry *entry;
887 struct stack_trace trace;
888 unsigned long irq_flags;
890 if (!(trace_flags & TRACE_ITER_STACKTRACE))
893 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
897 entry = ring_buffer_event_data(event);
898 tracing_generic_entry_update(&entry->ent, flags, pc);
899 entry->ent.type = TRACE_STACK;
901 memset(&entry->caller, 0, sizeof(entry->caller));
903 trace.nr_entries = 0;
904 trace.max_entries = FTRACE_STACK_ENTRIES;
906 trace.entries = entry->caller;
908 save_stack_trace(&trace);
909 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
913 void __trace_stack(struct trace_array *tr,
914 struct trace_array_cpu *data,
918 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
922 ftrace_trace_special(void *__tr, void *__data,
923 unsigned long arg1, unsigned long arg2, unsigned long arg3,
926 struct ring_buffer_event *event;
927 struct trace_array_cpu *data = __data;
928 struct trace_array *tr = __tr;
929 struct special_entry *entry;
930 unsigned long irq_flags;
932 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
936 entry = ring_buffer_event_data(event);
937 tracing_generic_entry_update(&entry->ent, 0, pc);
938 entry->ent.type = TRACE_SPECIAL;
942 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
943 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
949 __trace_special(void *__tr, void *__data,
950 unsigned long arg1, unsigned long arg2, unsigned long arg3)
952 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
956 tracing_sched_switch_trace(struct trace_array *tr,
957 struct trace_array_cpu *data,
958 struct task_struct *prev,
959 struct task_struct *next,
960 unsigned long flags, int pc)
962 struct ring_buffer_event *event;
963 struct ctx_switch_entry *entry;
964 unsigned long irq_flags;
966 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
970 entry = ring_buffer_event_data(event);
971 tracing_generic_entry_update(&entry->ent, flags, pc);
972 entry->ent.type = TRACE_CTX;
973 entry->prev_pid = prev->pid;
974 entry->prev_prio = prev->prio;
975 entry->prev_state = prev->state;
976 entry->next_pid = next->pid;
977 entry->next_prio = next->prio;
978 entry->next_state = next->state;
979 entry->next_cpu = task_cpu(next);
980 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
981 ftrace_trace_stack(tr, data, flags, 5, pc);
985 tracing_sched_wakeup_trace(struct trace_array *tr,
986 struct trace_array_cpu *data,
987 struct task_struct *wakee,
988 struct task_struct *curr,
989 unsigned long flags, int pc)
991 struct ring_buffer_event *event;
992 struct ctx_switch_entry *entry;
993 unsigned long irq_flags;
995 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
999 entry = ring_buffer_event_data(event);
1000 tracing_generic_entry_update(&entry->ent, flags, pc);
1001 entry->ent.type = TRACE_WAKE;
1002 entry->prev_pid = curr->pid;
1003 entry->prev_prio = curr->prio;
1004 entry->prev_state = curr->state;
1005 entry->next_pid = wakee->pid;
1006 entry->next_prio = wakee->prio;
1007 entry->next_state = wakee->state;
1008 entry->next_cpu = task_cpu(wakee);
1009 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1010 ftrace_trace_stack(tr, data, flags, 6, pc);
1016 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1018 struct trace_array *tr = &global_trace;
1019 struct trace_array_cpu *data;
1020 unsigned long flags;
1024 if (tracing_disabled)
1027 pc = preempt_count();
1028 local_irq_save(flags);
1029 cpu = raw_smp_processor_id();
1030 data = tr->data[cpu];
1032 if (likely(atomic_inc_return(&data->disabled) == 1))
1033 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1035 atomic_dec(&data->disabled);
1036 local_irq_restore(flags);
1039 #ifdef CONFIG_FUNCTION_TRACER
1041 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
1043 struct trace_array *tr = &global_trace;
1044 struct trace_array_cpu *data;
1045 unsigned long flags;
1050 if (unlikely(!ftrace_function_enabled))
1053 pc = preempt_count();
1054 resched = ftrace_preempt_disable();
1055 local_save_flags(flags);
1056 cpu = raw_smp_processor_id();
1057 data = tr->data[cpu];
1058 disabled = atomic_inc_return(&data->disabled);
1060 if (likely(disabled == 1))
1061 trace_function(tr, data, ip, parent_ip, flags, pc);
1063 atomic_dec(&data->disabled);
1064 ftrace_preempt_enable(resched);
1068 function_trace_call(unsigned long ip, unsigned long parent_ip)
1070 struct trace_array *tr = &global_trace;
1071 struct trace_array_cpu *data;
1072 unsigned long flags;
1077 if (unlikely(!ftrace_function_enabled))
1081 * Need to use raw, since this must be called before the
1082 * recursive protection is performed.
1084 local_irq_save(flags);
1085 cpu = raw_smp_processor_id();
1086 data = tr->data[cpu];
1087 disabled = atomic_inc_return(&data->disabled);
1089 if (likely(disabled == 1)) {
1090 pc = preempt_count();
1091 trace_function(tr, data, ip, parent_ip, flags, pc);
1094 atomic_dec(&data->disabled);
1095 local_irq_restore(flags);
1098 #ifdef CONFIG_FUNCTION_RET_TRACER
1099 void trace_function_return(struct ftrace_retfunc *trace)
1101 struct trace_array *tr = &global_trace;
1102 struct trace_array_cpu *data;
1103 unsigned long flags;
1108 raw_local_irq_save(flags);
1109 cpu = raw_smp_processor_id();
1110 data = tr->data[cpu];
1111 disabled = atomic_inc_return(&data->disabled);
1112 if (likely(disabled == 1)) {
1113 pc = preempt_count();
1114 __trace_function_return(tr, data, trace, flags, pc);
1116 atomic_dec(&data->disabled);
1117 raw_local_irq_restore(flags);
1119 #endif /* CONFIG_FUNCTION_RET_TRACER */
1121 static struct ftrace_ops trace_ops __read_mostly =
1123 .func = function_trace_call,
1126 void tracing_start_function_trace(void)
1128 ftrace_function_enabled = 0;
1130 if (trace_flags & TRACE_ITER_PREEMPTONLY)
1131 trace_ops.func = function_trace_call_preempt_only;
1133 trace_ops.func = function_trace_call;
1135 register_ftrace_function(&trace_ops);
1136 ftrace_function_enabled = 1;
1139 void tracing_stop_function_trace(void)
1141 ftrace_function_enabled = 0;
1142 unregister_ftrace_function(&trace_ops);
1146 enum trace_file_type {
1147 TRACE_FILE_LAT_FMT = 1,
1148 TRACE_FILE_ANNOTATE = 2,
1151 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1153 /* Don't allow ftrace to trace into the ring buffers */
1154 ftrace_disable_cpu();
1157 if (iter->buffer_iter[iter->cpu])
1158 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1160 ftrace_enable_cpu();
1163 static struct trace_entry *
1164 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1166 struct ring_buffer_event *event;
1167 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1169 /* Don't allow ftrace to trace into the ring buffers */
1170 ftrace_disable_cpu();
1173 event = ring_buffer_iter_peek(buf_iter, ts);
1175 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1177 ftrace_enable_cpu();
1179 return event ? ring_buffer_event_data(event) : NULL;
1182 static struct trace_entry *
1183 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1185 struct ring_buffer *buffer = iter->tr->buffer;
1186 struct trace_entry *ent, *next = NULL;
1187 u64 next_ts = 0, ts;
1191 for_each_tracing_cpu(cpu) {
1193 if (ring_buffer_empty_cpu(buffer, cpu))
1196 ent = peek_next_entry(iter, cpu, &ts);
1199 * Pick the entry with the smallest timestamp:
1201 if (ent && (!next || ts < next_ts)) {
1209 *ent_cpu = next_cpu;
1217 /* Find the next real entry, without updating the iterator itself */
1218 static struct trace_entry *
1219 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1221 return __find_next_entry(iter, ent_cpu, ent_ts);
1224 /* Find the next real entry, and increment the iterator to the next entry */
1225 static void *find_next_entry_inc(struct trace_iterator *iter)
1227 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1230 trace_iterator_increment(iter, iter->cpu);
1232 return iter->ent ? iter : NULL;
1235 static void trace_consume(struct trace_iterator *iter)
1237 /* Don't allow ftrace to trace into the ring buffers */
1238 ftrace_disable_cpu();
1239 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1240 ftrace_enable_cpu();
1243 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1245 struct trace_iterator *iter = m->private;
1251 /* can't go backwards */
1256 ent = find_next_entry_inc(iter);
1260 while (ent && iter->idx < i)
1261 ent = find_next_entry_inc(iter);
1268 static void *s_start(struct seq_file *m, loff_t *pos)
1270 struct trace_iterator *iter = m->private;
1275 mutex_lock(&trace_types_lock);
1277 if (!current_trace || current_trace != iter->trace) {
1278 mutex_unlock(&trace_types_lock);
1282 atomic_inc(&trace_record_cmdline_disabled);
1284 if (*pos != iter->pos) {
1289 ftrace_disable_cpu();
1291 for_each_tracing_cpu(cpu) {
1292 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1295 ftrace_enable_cpu();
1297 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1302 p = s_next(m, p, &l);
1308 static void s_stop(struct seq_file *m, void *p)
1310 atomic_dec(&trace_record_cmdline_disabled);
1311 mutex_unlock(&trace_types_lock);
1314 #ifdef CONFIG_KRETPROBES
1315 static inline const char *kretprobed(const char *name)
1317 static const char tramp_name[] = "kretprobe_trampoline";
1318 int size = sizeof(tramp_name);
1320 if (strncmp(tramp_name, name, size) == 0)
1321 return "[unknown/kretprobe'd]";
1325 static inline const char *kretprobed(const char *name)
1329 #endif /* CONFIG_KRETPROBES */
1332 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1334 #ifdef CONFIG_KALLSYMS
1335 char str[KSYM_SYMBOL_LEN];
1338 kallsyms_lookup(address, NULL, NULL, NULL, str);
1340 name = kretprobed(str);
1342 return trace_seq_printf(s, fmt, name);
1348 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1349 unsigned long address)
1351 #ifdef CONFIG_KALLSYMS
1352 char str[KSYM_SYMBOL_LEN];
1355 sprint_symbol(str, address);
1356 name = kretprobed(str);
1358 return trace_seq_printf(s, fmt, name);
1363 #ifndef CONFIG_64BIT
1364 # define IP_FMT "%08lx"
1366 # define IP_FMT "%016lx"
1370 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1375 return trace_seq_printf(s, "0");
1377 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1378 ret = seq_print_sym_offset(s, "%s", ip);
1380 ret = seq_print_sym_short(s, "%s", ip);
1385 if (sym_flags & TRACE_ITER_SYM_ADDR)
1386 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1390 static void print_lat_help_header(struct seq_file *m)
1392 seq_puts(m, "# _------=> CPU# \n");
1393 seq_puts(m, "# / _-----=> irqs-off \n");
1394 seq_puts(m, "# | / _----=> need-resched \n");
1395 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1396 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1397 seq_puts(m, "# |||| / \n");
1398 seq_puts(m, "# ||||| delay \n");
1399 seq_puts(m, "# cmd pid ||||| time | caller \n");
1400 seq_puts(m, "# \\ / ||||| \\ | / \n");
1403 static void print_func_help_header(struct seq_file *m)
1405 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1406 seq_puts(m, "# | | | | |\n");
1411 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1413 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1414 struct trace_array *tr = iter->tr;
1415 struct trace_array_cpu *data = tr->data[tr->cpu];
1416 struct tracer *type = current_trace;
1417 unsigned long total;
1418 unsigned long entries;
1419 const char *name = "preemption";
1424 entries = ring_buffer_entries(iter->tr->buffer);
1426 ring_buffer_overruns(iter->tr->buffer);
1428 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1430 seq_puts(m, "-----------------------------------"
1431 "---------------------------------\n");
1432 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1433 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1434 nsecs_to_usecs(data->saved_latency),
1438 #if defined(CONFIG_PREEMPT_NONE)
1440 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1442 #elif defined(CONFIG_PREEMPT)
1447 /* These are reserved for later use */
1450 seq_printf(m, " #P:%d)\n", num_online_cpus());
1454 seq_puts(m, " -----------------\n");
1455 seq_printf(m, " | task: %.16s-%d "
1456 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1457 data->comm, data->pid, data->uid, data->nice,
1458 data->policy, data->rt_priority);
1459 seq_puts(m, " -----------------\n");
1461 if (data->critical_start) {
1462 seq_puts(m, " => started at: ");
1463 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1464 trace_print_seq(m, &iter->seq);
1465 seq_puts(m, "\n => ended at: ");
1466 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1467 trace_print_seq(m, &iter->seq);
1475 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1477 int hardirq, softirq;
1480 comm = trace_find_cmdline(entry->pid);
1482 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1483 trace_seq_printf(s, "%3d", cpu);
1484 trace_seq_printf(s, "%c%c",
1485 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1486 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1487 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1489 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1490 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1491 if (hardirq && softirq) {
1492 trace_seq_putc(s, 'H');
1495 trace_seq_putc(s, 'h');
1498 trace_seq_putc(s, 's');
1500 trace_seq_putc(s, '.');
1504 if (entry->preempt_count)
1505 trace_seq_printf(s, "%x", entry->preempt_count);
1507 trace_seq_puts(s, ".");
1510 unsigned long preempt_mark_thresh = 100;
1513 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1514 unsigned long rel_usecs)
1516 trace_seq_printf(s, " %4lldus", abs_usecs);
1517 if (rel_usecs > preempt_mark_thresh)
1518 trace_seq_puts(s, "!: ");
1519 else if (rel_usecs > 1)
1520 trace_seq_puts(s, "+: ");
1522 trace_seq_puts(s, " : ");
1525 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1528 * The message is supposed to contain an ending newline.
1529 * If the printing stops prematurely, try to add a newline of our own.
1531 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1533 struct trace_entry *ent;
1534 struct trace_field_cont *cont;
1537 ent = peek_next_entry(iter, iter->cpu, NULL);
1538 if (!ent || ent->type != TRACE_CONT) {
1539 trace_seq_putc(s, '\n');
1544 cont = (struct trace_field_cont *)ent;
1546 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
1548 ftrace_disable_cpu();
1550 if (iter->buffer_iter[iter->cpu])
1551 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1553 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1555 ftrace_enable_cpu();
1557 ent = peek_next_entry(iter, iter->cpu, NULL);
1558 } while (ent && ent->type == TRACE_CONT);
1561 trace_seq_putc(s, '\n');
1564 static void test_cpu_buff_start(struct trace_iterator *iter)
1566 struct trace_seq *s = &iter->seq;
1568 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1571 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1574 if (cpu_isset(iter->cpu, iter->started))
1577 cpu_set(iter->cpu, iter->started);
1578 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1581 static enum print_line_t
1582 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1584 struct trace_seq *s = &iter->seq;
1585 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1586 struct trace_entry *next_entry;
1587 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1588 struct trace_entry *entry = iter->ent;
1589 unsigned long abs_usecs;
1590 unsigned long rel_usecs;
1597 if (entry->type == TRACE_CONT)
1598 return TRACE_TYPE_HANDLED;
1600 test_cpu_buff_start(iter);
1602 next_entry = find_next_entry(iter, NULL, &next_ts);
1605 rel_usecs = ns2usecs(next_ts - iter->ts);
1606 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1609 comm = trace_find_cmdline(entry->pid);
1610 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1611 " %ld.%03ldms (+%ld.%03ldms): ",
1613 entry->pid, cpu, entry->flags,
1614 entry->preempt_count, trace_idx,
1617 abs_usecs % 1000, rel_usecs/1000,
1620 lat_print_generic(s, entry, cpu);
1621 lat_print_timestamp(s, abs_usecs, rel_usecs);
1623 switch (entry->type) {
1625 struct ftrace_entry *field;
1627 trace_assign_type(field, entry);
1629 seq_print_ip_sym(s, field->ip, sym_flags);
1630 trace_seq_puts(s, " (");
1631 seq_print_ip_sym(s, field->parent_ip, sym_flags);
1632 trace_seq_puts(s, ")\n");
1637 struct ctx_switch_entry *field;
1639 trace_assign_type(field, entry);
1641 T = field->next_state < sizeof(state_to_char) ?
1642 state_to_char[field->next_state] : 'X';
1644 state = field->prev_state ?
1645 __ffs(field->prev_state) + 1 : 0;
1646 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1647 comm = trace_find_cmdline(field->next_pid);
1648 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1651 S, entry->type == TRACE_CTX ? "==>" : " +",
1658 case TRACE_SPECIAL: {
1659 struct special_entry *field;
1661 trace_assign_type(field, entry);
1663 trace_seq_printf(s, "# %ld %ld %ld\n",
1670 struct stack_entry *field;
1672 trace_assign_type(field, entry);
1674 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1676 trace_seq_puts(s, " <= ");
1677 seq_print_ip_sym(s, field->caller[i], sym_flags);
1679 trace_seq_puts(s, "\n");
1683 struct print_entry *field;
1685 trace_assign_type(field, entry);
1687 seq_print_ip_sym(s, field->ip, sym_flags);
1688 trace_seq_printf(s, ": %s", field->buf);
1689 if (entry->flags & TRACE_FLAG_CONT)
1690 trace_seq_print_cont(s, iter);
1693 case TRACE_BRANCH: {
1694 struct trace_branch *field;
1696 trace_assign_type(field, entry);
1698 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1699 field->correct ? " ok " : " MISS ",
1706 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1708 return TRACE_TYPE_HANDLED;
1711 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1713 struct trace_seq *s = &iter->seq;
1714 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1715 struct trace_entry *entry;
1716 unsigned long usec_rem;
1717 unsigned long long t;
1726 if (entry->type == TRACE_CONT)
1727 return TRACE_TYPE_HANDLED;
1729 test_cpu_buff_start(iter);
1731 comm = trace_find_cmdline(iter->ent->pid);
1733 t = ns2usecs(iter->ts);
1734 usec_rem = do_div(t, 1000000ULL);
1735 secs = (unsigned long)t;
1737 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1739 return TRACE_TYPE_PARTIAL_LINE;
1740 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1742 return TRACE_TYPE_PARTIAL_LINE;
1743 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1745 return TRACE_TYPE_PARTIAL_LINE;
1747 switch (entry->type) {
1749 struct ftrace_entry *field;
1751 trace_assign_type(field, entry);
1753 ret = seq_print_ip_sym(s, field->ip, sym_flags);
1755 return TRACE_TYPE_PARTIAL_LINE;
1756 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1758 ret = trace_seq_printf(s, " <-");
1760 return TRACE_TYPE_PARTIAL_LINE;
1761 ret = seq_print_ip_sym(s,
1765 return TRACE_TYPE_PARTIAL_LINE;
1767 ret = trace_seq_printf(s, "\n");
1769 return TRACE_TYPE_PARTIAL_LINE;
1774 struct ctx_switch_entry *field;
1776 trace_assign_type(field, entry);
1778 S = field->prev_state < sizeof(state_to_char) ?
1779 state_to_char[field->prev_state] : 'X';
1780 T = field->next_state < sizeof(state_to_char) ?
1781 state_to_char[field->next_state] : 'X';
1782 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1786 entry->type == TRACE_CTX ? "==>" : " +",
1792 return TRACE_TYPE_PARTIAL_LINE;
1795 case TRACE_SPECIAL: {
1796 struct special_entry *field;
1798 trace_assign_type(field, entry);
1800 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1805 return TRACE_TYPE_PARTIAL_LINE;
1809 struct stack_entry *field;
1811 trace_assign_type(field, entry);
1813 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1815 ret = trace_seq_puts(s, " <= ");
1817 return TRACE_TYPE_PARTIAL_LINE;
1819 ret = seq_print_ip_sym(s, field->caller[i],
1822 return TRACE_TYPE_PARTIAL_LINE;
1824 ret = trace_seq_puts(s, "\n");
1826 return TRACE_TYPE_PARTIAL_LINE;
1830 struct print_entry *field;
1832 trace_assign_type(field, entry);
1834 seq_print_ip_sym(s, field->ip, sym_flags);
1835 trace_seq_printf(s, ": %s", field->buf);
1836 if (entry->flags & TRACE_FLAG_CONT)
1837 trace_seq_print_cont(s, iter);
1840 case TRACE_FN_RET: {
1841 return print_return_function(iter);
1844 case TRACE_BRANCH: {
1845 struct trace_branch *field;
1847 trace_assign_type(field, entry);
1849 trace_seq_printf(s, "[%s] %s:%s:%d\n",
1850 field->correct ? " ok " : " MISS ",
1857 return TRACE_TYPE_HANDLED;
1860 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1862 struct trace_seq *s = &iter->seq;
1863 struct trace_entry *entry;
1869 if (entry->type == TRACE_CONT)
1870 return TRACE_TYPE_HANDLED;
1872 ret = trace_seq_printf(s, "%d %d %llu ",
1873 entry->pid, iter->cpu, iter->ts);
1875 return TRACE_TYPE_PARTIAL_LINE;
1877 switch (entry->type) {
1879 struct ftrace_entry *field;
1881 trace_assign_type(field, entry);
1883 ret = trace_seq_printf(s, "%x %x\n",
1887 return TRACE_TYPE_PARTIAL_LINE;
1892 struct ctx_switch_entry *field;
1894 trace_assign_type(field, entry);
1896 S = field->prev_state < sizeof(state_to_char) ?
1897 state_to_char[field->prev_state] : 'X';
1898 T = field->next_state < sizeof(state_to_char) ?
1899 state_to_char[field->next_state] : 'X';
1900 if (entry->type == TRACE_WAKE)
1902 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
1911 return TRACE_TYPE_PARTIAL_LINE;
1916 struct special_entry *field;
1918 trace_assign_type(field, entry);
1920 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1925 return TRACE_TYPE_PARTIAL_LINE;
1929 struct print_entry *field;
1931 trace_assign_type(field, entry);
1933 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
1934 if (entry->flags & TRACE_FLAG_CONT)
1935 trace_seq_print_cont(s, iter);
1939 return TRACE_TYPE_HANDLED;
1942 #define SEQ_PUT_FIELD_RET(s, x) \
1944 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1948 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1950 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
1951 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1955 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1957 struct trace_seq *s = &iter->seq;
1958 unsigned char newline = '\n';
1959 struct trace_entry *entry;
1964 if (entry->type == TRACE_CONT)
1965 return TRACE_TYPE_HANDLED;
1967 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1968 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1969 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1971 switch (entry->type) {
1973 struct ftrace_entry *field;
1975 trace_assign_type(field, entry);
1977 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1978 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
1983 struct ctx_switch_entry *field;
1985 trace_assign_type(field, entry);
1987 S = field->prev_state < sizeof(state_to_char) ?
1988 state_to_char[field->prev_state] : 'X';
1989 T = field->next_state < sizeof(state_to_char) ?
1990 state_to_char[field->next_state] : 'X';
1991 if (entry->type == TRACE_WAKE)
1993 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1994 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
1995 SEQ_PUT_HEX_FIELD_RET(s, S);
1996 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1997 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1998 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
1999 SEQ_PUT_HEX_FIELD_RET(s, T);
2004 struct special_entry *field;
2006 trace_assign_type(field, entry);
2008 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
2009 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
2010 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
2014 SEQ_PUT_FIELD_RET(s, newline);
2016 return TRACE_TYPE_HANDLED;
2019 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2021 struct trace_seq *s = &iter->seq;
2022 struct trace_entry *entry;
2026 if (entry->type == TRACE_CONT)
2027 return TRACE_TYPE_HANDLED;
2029 SEQ_PUT_FIELD_RET(s, entry->pid);
2030 SEQ_PUT_FIELD_RET(s, entry->cpu);
2031 SEQ_PUT_FIELD_RET(s, iter->ts);
2033 switch (entry->type) {
2035 struct ftrace_entry *field;
2037 trace_assign_type(field, entry);
2039 SEQ_PUT_FIELD_RET(s, field->ip);
2040 SEQ_PUT_FIELD_RET(s, field->parent_ip);
2044 struct ctx_switch_entry *field;
2046 trace_assign_type(field, entry);
2048 SEQ_PUT_FIELD_RET(s, field->prev_pid);
2049 SEQ_PUT_FIELD_RET(s, field->prev_prio);
2050 SEQ_PUT_FIELD_RET(s, field->prev_state);
2051 SEQ_PUT_FIELD_RET(s, field->next_pid);
2052 SEQ_PUT_FIELD_RET(s, field->next_prio);
2053 SEQ_PUT_FIELD_RET(s, field->next_state);
2058 struct special_entry *field;
2060 trace_assign_type(field, entry);
2062 SEQ_PUT_FIELD_RET(s, field->arg1);
2063 SEQ_PUT_FIELD_RET(s, field->arg2);
2064 SEQ_PUT_FIELD_RET(s, field->arg3);
2071 static int trace_empty(struct trace_iterator *iter)
2075 for_each_tracing_cpu(cpu) {
2076 if (iter->buffer_iter[cpu]) {
2077 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2080 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2088 static enum print_line_t print_trace_line(struct trace_iterator *iter)
2090 enum print_line_t ret;
2092 if (iter->trace && iter->trace->print_line) {
2093 ret = iter->trace->print_line(iter);
2094 if (ret != TRACE_TYPE_UNHANDLED)
2098 if (trace_flags & TRACE_ITER_BIN)
2099 return print_bin_fmt(iter);
2101 if (trace_flags & TRACE_ITER_HEX)
2102 return print_hex_fmt(iter);
2104 if (trace_flags & TRACE_ITER_RAW)
2105 return print_raw_fmt(iter);
2107 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2108 return print_lat_fmt(iter, iter->idx, iter->cpu);
2110 return print_trace_fmt(iter);
2113 static int s_show(struct seq_file *m, void *v)
2115 struct trace_iterator *iter = v;
2117 if (iter->ent == NULL) {
2119 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2122 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2123 /* print nothing if the buffers are empty */
2124 if (trace_empty(iter))
2126 print_trace_header(m, iter);
2127 if (!(trace_flags & TRACE_ITER_VERBOSE))
2128 print_lat_help_header(m);
2130 if (!(trace_flags & TRACE_ITER_VERBOSE))
2131 print_func_help_header(m);
2134 print_trace_line(iter);
2135 trace_print_seq(m, &iter->seq);
2141 static struct seq_operations tracer_seq_ops = {
2148 static struct trace_iterator *
2149 __tracing_open(struct inode *inode, struct file *file, int *ret)
2151 struct trace_iterator *iter;
2155 if (tracing_disabled) {
2160 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2166 mutex_lock(&trace_types_lock);
2167 if (current_trace && current_trace->print_max)
2170 iter->tr = inode->i_private;
2171 iter->trace = current_trace;
2174 /* Annotate start of buffers if we had overruns */
2175 if (ring_buffer_overruns(iter->tr->buffer))
2176 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2179 for_each_tracing_cpu(cpu) {
2181 iter->buffer_iter[cpu] =
2182 ring_buffer_read_start(iter->tr->buffer, cpu);
2184 if (!iter->buffer_iter[cpu])
2188 /* TODO stop tracer */
2189 *ret = seq_open(file, &tracer_seq_ops);
2193 m = file->private_data;
2196 /* stop the trace while dumping */
2199 if (iter->trace && iter->trace->open)
2200 iter->trace->open(iter);
2202 mutex_unlock(&trace_types_lock);
2208 for_each_tracing_cpu(cpu) {
2209 if (iter->buffer_iter[cpu])
2210 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2212 mutex_unlock(&trace_types_lock);
2215 return ERR_PTR(-ENOMEM);
2218 int tracing_open_generic(struct inode *inode, struct file *filp)
2220 if (tracing_disabled)
2223 filp->private_data = inode->i_private;
2227 int tracing_release(struct inode *inode, struct file *file)
2229 struct seq_file *m = (struct seq_file *)file->private_data;
2230 struct trace_iterator *iter = m->private;
2233 mutex_lock(&trace_types_lock);
2234 for_each_tracing_cpu(cpu) {
2235 if (iter->buffer_iter[cpu])
2236 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2239 if (iter->trace && iter->trace->close)
2240 iter->trace->close(iter);
2242 /* reenable tracing if it was previously enabled */
2244 mutex_unlock(&trace_types_lock);
2246 seq_release(inode, file);
2251 static int tracing_open(struct inode *inode, struct file *file)
2255 __tracing_open(inode, file, &ret);
2260 static int tracing_lt_open(struct inode *inode, struct file *file)
2262 struct trace_iterator *iter;
2265 iter = __tracing_open(inode, file, &ret);
2268 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2275 t_next(struct seq_file *m, void *v, loff_t *pos)
2277 struct tracer *t = m->private;
2289 static void *t_start(struct seq_file *m, loff_t *pos)
2291 struct tracer *t = m->private;
2294 mutex_lock(&trace_types_lock);
2295 for (; t && l < *pos; t = t_next(m, t, &l))
2301 static void t_stop(struct seq_file *m, void *p)
2303 mutex_unlock(&trace_types_lock);
2306 static int t_show(struct seq_file *m, void *v)
2308 struct tracer *t = v;
2313 seq_printf(m, "%s", t->name);
2322 static struct seq_operations show_traces_seq_ops = {
2329 static int show_traces_open(struct inode *inode, struct file *file)
2333 if (tracing_disabled)
2336 ret = seq_open(file, &show_traces_seq_ops);
2338 struct seq_file *m = file->private_data;
2339 m->private = trace_types;
2345 static struct file_operations tracing_fops = {
2346 .open = tracing_open,
2348 .llseek = seq_lseek,
2349 .release = tracing_release,
2352 static struct file_operations tracing_lt_fops = {
2353 .open = tracing_lt_open,
2355 .llseek = seq_lseek,
2356 .release = tracing_release,
2359 static struct file_operations show_traces_fops = {
2360 .open = show_traces_open,
2362 .release = seq_release,
2366 * Only trace on a CPU if the bitmask is set:
2368 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2371 * When tracing/tracing_cpu_mask is modified then this holds
2372 * the new bitmask we are about to install:
2374 static cpumask_t tracing_cpumask_new;
2377 * The tracer itself will not take this lock, but still we want
2378 * to provide a consistent cpumask to user-space:
2380 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2383 * Temporary storage for the character representation of the
2384 * CPU bitmask (and one more byte for the newline):
2386 static char mask_str[NR_CPUS + 1];
2389 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2390 size_t count, loff_t *ppos)
2394 mutex_lock(&tracing_cpumask_update_lock);
2396 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2397 if (count - len < 2) {
2401 len += sprintf(mask_str + len, "\n");
2402 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2405 mutex_unlock(&tracing_cpumask_update_lock);
2411 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2412 size_t count, loff_t *ppos)
2416 mutex_lock(&tracing_cpumask_update_lock);
2417 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2421 raw_local_irq_disable();
2422 __raw_spin_lock(&ftrace_max_lock);
2423 for_each_tracing_cpu(cpu) {
2425 * Increase/decrease the disabled counter if we are
2426 * about to flip a bit in the cpumask:
2428 if (cpu_isset(cpu, tracing_cpumask) &&
2429 !cpu_isset(cpu, tracing_cpumask_new)) {
2430 atomic_inc(&global_trace.data[cpu]->disabled);
2432 if (!cpu_isset(cpu, tracing_cpumask) &&
2433 cpu_isset(cpu, tracing_cpumask_new)) {
2434 atomic_dec(&global_trace.data[cpu]->disabled);
2437 __raw_spin_unlock(&ftrace_max_lock);
2438 raw_local_irq_enable();
2440 tracing_cpumask = tracing_cpumask_new;
2442 mutex_unlock(&tracing_cpumask_update_lock);
2447 mutex_unlock(&tracing_cpumask_update_lock);
2452 static struct file_operations tracing_cpumask_fops = {
2453 .open = tracing_open_generic,
2454 .read = tracing_cpumask_read,
2455 .write = tracing_cpumask_write,
2459 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2460 size_t cnt, loff_t *ppos)
2466 u32 tracer_flags = current_trace->flags->val;
2467 struct tracer_opt *trace_opts = current_trace->flags->opts;
2470 /* calulate max size */
2471 for (i = 0; trace_options[i]; i++) {
2472 len += strlen(trace_options[i]);
2473 len += 3; /* "no" and space */
2477 * Increase the size with names of options specific
2478 * of the current tracer.
2480 for (i = 0; trace_opts[i].name; i++) {
2481 len += strlen(trace_opts[i].name);
2482 len += 3; /* "no" and space */
2485 /* +2 for \n and \0 */
2486 buf = kmalloc(len + 2, GFP_KERNEL);
2490 for (i = 0; trace_options[i]; i++) {
2491 if (trace_flags & (1 << i))
2492 r += sprintf(buf + r, "%s ", trace_options[i]);
2494 r += sprintf(buf + r, "no%s ", trace_options[i]);
2497 for (i = 0; trace_opts[i].name; i++) {
2498 if (tracer_flags & trace_opts[i].bit)
2499 r += sprintf(buf + r, "%s ",
2500 trace_opts[i].name);
2502 r += sprintf(buf + r, "no%s ",
2503 trace_opts[i].name);
2506 r += sprintf(buf + r, "\n");
2507 WARN_ON(r >= len + 2);
2509 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2516 /* Try to assign a tracer specific option */
2517 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2519 struct tracer_flags *trace_flags = trace->flags;
2520 struct tracer_opt *opts = NULL;
2524 for (i = 0; trace_flags->opts[i].name; i++) {
2525 opts = &trace_flags->opts[i];
2526 len = strlen(opts->name);
2528 if (strncmp(cmp, opts->name, len) == 0) {
2529 ret = trace->set_flag(trace_flags->val,
2535 if (!trace_flags->opts[i].name)
2538 /* Refused to handle */
2543 trace_flags->val &= ~opts->bit;
2545 trace_flags->val |= opts->bit;
2551 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2552 size_t cnt, loff_t *ppos)
2560 if (cnt >= sizeof(buf))
2563 if (copy_from_user(&buf, ubuf, cnt))
2568 if (strncmp(buf, "no", 2) == 0) {
2573 for (i = 0; trace_options[i]; i++) {
2574 int len = strlen(trace_options[i]);
2576 if (strncmp(cmp, trace_options[i], len) == 0) {
2578 trace_flags &= ~(1 << i);
2580 trace_flags |= (1 << i);
2585 /* If no option could be set, test the specific tracer options */
2586 if (!trace_options[i]) {
2587 ret = set_tracer_option(current_trace, cmp, neg);
2597 static struct file_operations tracing_iter_fops = {
2598 .open = tracing_open_generic,
2599 .read = tracing_trace_options_read,
2600 .write = tracing_trace_options_write,
2603 static const char readme_msg[] =
2604 "tracing mini-HOWTO:\n\n"
2606 "# mount -t debugfs nodev /debug\n\n"
2607 "# cat /debug/tracing/available_tracers\n"
2608 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2609 "# cat /debug/tracing/current_tracer\n"
2611 "# echo sched_switch > /debug/tracing/current_tracer\n"
2612 "# cat /debug/tracing/current_tracer\n"
2614 "# cat /debug/tracing/trace_options\n"
2615 "noprint-parent nosym-offset nosym-addr noverbose\n"
2616 "# echo print-parent > /debug/tracing/trace_options\n"
2617 "# echo 1 > /debug/tracing/tracing_enabled\n"
2618 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2619 "echo 0 > /debug/tracing/tracing_enabled\n"
2623 tracing_readme_read(struct file *filp, char __user *ubuf,
2624 size_t cnt, loff_t *ppos)
2626 return simple_read_from_buffer(ubuf, cnt, ppos,
2627 readme_msg, strlen(readme_msg));
2630 static struct file_operations tracing_readme_fops = {
2631 .open = tracing_open_generic,
2632 .read = tracing_readme_read,
2636 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2637 size_t cnt, loff_t *ppos)
2642 r = sprintf(buf, "%u\n", tracer_enabled);
2643 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2647 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2648 size_t cnt, loff_t *ppos)
2650 struct trace_array *tr = filp->private_data;
2655 if (cnt >= sizeof(buf))
2658 if (copy_from_user(&buf, ubuf, cnt))
2663 ret = strict_strtoul(buf, 10, &val);
2669 mutex_lock(&trace_types_lock);
2670 if (tracer_enabled ^ val) {
2673 if (current_trace->start)
2674 current_trace->start(tr);
2679 if (current_trace->stop)
2680 current_trace->stop(tr);
2683 mutex_unlock(&trace_types_lock);
2691 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2692 size_t cnt, loff_t *ppos)
2694 char buf[max_tracer_type_len+2];
2697 mutex_lock(&trace_types_lock);
2699 r = sprintf(buf, "%s\n", current_trace->name);
2701 r = sprintf(buf, "\n");
2702 mutex_unlock(&trace_types_lock);
2704 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2707 static int tracing_set_tracer(char *buf)
2709 struct trace_array *tr = &global_trace;
2713 mutex_lock(&trace_types_lock);
2714 for (t = trace_types; t; t = t->next) {
2715 if (strcmp(t->name, buf) == 0)
2722 if (t == current_trace)
2725 trace_branch_disable();
2726 if (current_trace && current_trace->reset)
2727 current_trace->reset(tr);
2736 trace_branch_enable(tr);
2738 mutex_unlock(&trace_types_lock);
2744 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2745 size_t cnt, loff_t *ppos)
2747 char buf[max_tracer_type_len+1];
2754 if (cnt > max_tracer_type_len)
2755 cnt = max_tracer_type_len;
2757 if (copy_from_user(&buf, ubuf, cnt))
2762 /* strip ending whitespace. */
2763 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2766 err = tracing_set_tracer(buf);
2776 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2777 size_t cnt, loff_t *ppos)
2779 unsigned long *ptr = filp->private_data;
2783 r = snprintf(buf, sizeof(buf), "%ld\n",
2784 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2785 if (r > sizeof(buf))
2787 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2791 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2792 size_t cnt, loff_t *ppos)
2794 long *ptr = filp->private_data;
2799 if (cnt >= sizeof(buf))
2802 if (copy_from_user(&buf, ubuf, cnt))
2807 ret = strict_strtoul(buf, 10, &val);
2816 static atomic_t tracing_reader;
2818 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2820 struct trace_iterator *iter;
2822 if (tracing_disabled)
2825 /* We only allow for reader of the pipe */
2826 if (atomic_inc_return(&tracing_reader) != 1) {
2827 atomic_dec(&tracing_reader);
2831 /* create a buffer to store the information to pass to userspace */
2832 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2836 mutex_lock(&trace_types_lock);
2838 /* trace pipe does not show start of buffer */
2839 cpus_setall(iter->started);
2841 iter->tr = &global_trace;
2842 iter->trace = current_trace;
2843 filp->private_data = iter;
2845 if (iter->trace->pipe_open)
2846 iter->trace->pipe_open(iter);
2847 mutex_unlock(&trace_types_lock);
2852 static int tracing_release_pipe(struct inode *inode, struct file *file)
2854 struct trace_iterator *iter = file->private_data;
2857 atomic_dec(&tracing_reader);
2863 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2865 struct trace_iterator *iter = filp->private_data;
2867 if (trace_flags & TRACE_ITER_BLOCK) {
2869 * Always select as readable when in blocking mode
2871 return POLLIN | POLLRDNORM;
2873 if (!trace_empty(iter))
2874 return POLLIN | POLLRDNORM;
2875 poll_wait(filp, &trace_wait, poll_table);
2876 if (!trace_empty(iter))
2877 return POLLIN | POLLRDNORM;
2887 tracing_read_pipe(struct file *filp, char __user *ubuf,
2888 size_t cnt, loff_t *ppos)
2890 struct trace_iterator *iter = filp->private_data;
2893 /* return any leftover data */
2894 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2898 trace_seq_reset(&iter->seq);
2900 mutex_lock(&trace_types_lock);
2901 if (iter->trace->read) {
2902 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2909 while (trace_empty(iter)) {
2911 if ((filp->f_flags & O_NONBLOCK)) {
2917 * This is a make-shift waitqueue. The reason we don't use
2918 * an actual wait queue is because:
2919 * 1) we only ever have one waiter
2920 * 2) the tracing, traces all functions, we don't want
2921 * the overhead of calling wake_up and friends
2922 * (and tracing them too)
2923 * Anyway, this is really very primitive wakeup.
2925 set_current_state(TASK_INTERRUPTIBLE);
2926 iter->tr->waiter = current;
2928 mutex_unlock(&trace_types_lock);
2930 /* sleep for 100 msecs, and try again. */
2931 schedule_timeout(HZ/10);
2933 mutex_lock(&trace_types_lock);
2935 iter->tr->waiter = NULL;
2937 if (signal_pending(current)) {
2942 if (iter->trace != current_trace)
2946 * We block until we read something and tracing is disabled.
2947 * We still block if tracing is disabled, but we have never
2948 * read anything. This allows a user to cat this file, and
2949 * then enable tracing. But after we have read something,
2950 * we give an EOF when tracing is again disabled.
2952 * iter->pos will be 0 if we haven't read anything.
2954 if (!tracer_enabled && iter->pos)
2960 /* stop when tracing is finished */
2961 if (trace_empty(iter))
2964 if (cnt >= PAGE_SIZE)
2965 cnt = PAGE_SIZE - 1;
2967 /* reset all but tr, trace, and overruns */
2968 memset(&iter->seq, 0,
2969 sizeof(struct trace_iterator) -
2970 offsetof(struct trace_iterator, seq));
2973 while (find_next_entry_inc(iter) != NULL) {
2974 enum print_line_t ret;
2975 int len = iter->seq.len;
2977 ret = print_trace_line(iter);
2978 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2979 /* don't print partial lines */
2980 iter->seq.len = len;
2984 trace_consume(iter);
2986 if (iter->seq.len >= cnt)
2990 /* Now copy what we have to the user */
2991 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2992 if (iter->seq.readpos >= iter->seq.len)
2993 trace_seq_reset(&iter->seq);
2996 * If there was nothing to send to user, inspite of consuming trace
2997 * entries, go back to wait for more entries.
3003 mutex_unlock(&trace_types_lock);
3009 tracing_entries_read(struct file *filp, char __user *ubuf,
3010 size_t cnt, loff_t *ppos)
3012 struct trace_array *tr = filp->private_data;
3016 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3017 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3021 tracing_entries_write(struct file *filp, const char __user *ubuf,
3022 size_t cnt, loff_t *ppos)
3028 if (cnt >= sizeof(buf))
3031 if (copy_from_user(&buf, ubuf, cnt))
3036 ret = strict_strtoul(buf, 10, &val);
3040 /* must have at least 1 entry */
3044 mutex_lock(&trace_types_lock);
3048 /* disable all cpu buffers */
3049 for_each_tracing_cpu(cpu) {
3050 if (global_trace.data[cpu])
3051 atomic_inc(&global_trace.data[cpu]->disabled);
3052 if (max_tr.data[cpu])
3053 atomic_inc(&max_tr.data[cpu]->disabled);
3056 /* value is in KB */
3059 if (val != global_trace.entries) {
3060 ret = ring_buffer_resize(global_trace.buffer, val);
3066 ret = ring_buffer_resize(max_tr.buffer, val);
3070 r = ring_buffer_resize(global_trace.buffer,
3071 global_trace.entries);
3073 /* AARGH! We are left with different
3074 * size max buffer!!!! */
3076 tracing_disabled = 1;
3081 global_trace.entries = val;
3086 /* If check pages failed, return ENOMEM */
3087 if (tracing_disabled)
3090 for_each_tracing_cpu(cpu) {
3091 if (global_trace.data[cpu])
3092 atomic_dec(&global_trace.data[cpu]->disabled);
3093 if (max_tr.data[cpu])
3094 atomic_dec(&max_tr.data[cpu]->disabled);
3098 max_tr.entries = global_trace.entries;
3099 mutex_unlock(&trace_types_lock);
3104 static int mark_printk(const char *fmt, ...)
3108 va_start(args, fmt);
3109 ret = trace_vprintk(0, fmt, args);
3115 tracing_mark_write(struct file *filp, const char __user *ubuf,
3116 size_t cnt, loff_t *fpos)
3121 if (tracing_disabled)
3124 if (cnt > TRACE_BUF_SIZE)
3125 cnt = TRACE_BUF_SIZE;
3127 buf = kmalloc(cnt + 1, GFP_KERNEL);
3131 if (copy_from_user(buf, ubuf, cnt)) {
3136 /* Cut from the first nil or newline. */
3138 end = strchr(buf, '\n');
3142 cnt = mark_printk("%s\n", buf);
3149 static struct file_operations tracing_max_lat_fops = {
3150 .open = tracing_open_generic,
3151 .read = tracing_max_lat_read,
3152 .write = tracing_max_lat_write,
3155 static struct file_operations tracing_ctrl_fops = {
3156 .open = tracing_open_generic,
3157 .read = tracing_ctrl_read,
3158 .write = tracing_ctrl_write,
3161 static struct file_operations set_tracer_fops = {
3162 .open = tracing_open_generic,
3163 .read = tracing_set_trace_read,
3164 .write = tracing_set_trace_write,
3167 static struct file_operations tracing_pipe_fops = {
3168 .open = tracing_open_pipe,
3169 .poll = tracing_poll_pipe,
3170 .read = tracing_read_pipe,
3171 .release = tracing_release_pipe,
3174 static struct file_operations tracing_entries_fops = {
3175 .open = tracing_open_generic,
3176 .read = tracing_entries_read,
3177 .write = tracing_entries_write,
3180 static struct file_operations tracing_mark_fops = {
3181 .open = tracing_open_generic,
3182 .write = tracing_mark_write,
3185 #ifdef CONFIG_DYNAMIC_FTRACE
3187 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3193 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3194 size_t cnt, loff_t *ppos)
3196 static char ftrace_dyn_info_buffer[1024];
3197 static DEFINE_MUTEX(dyn_info_mutex);
3198 unsigned long *p = filp->private_data;
3199 char *buf = ftrace_dyn_info_buffer;
3200 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3203 mutex_lock(&dyn_info_mutex);
3204 r = sprintf(buf, "%ld ", *p);
3206 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3209 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3211 mutex_unlock(&dyn_info_mutex);
3216 static struct file_operations tracing_dyn_info_fops = {
3217 .open = tracing_open_generic,
3218 .read = tracing_read_dyn_info,
3222 static struct dentry *d_tracer;
3224 struct dentry *tracing_init_dentry(void)
3231 d_tracer = debugfs_create_dir("tracing", NULL);
3233 if (!d_tracer && !once) {
3235 pr_warning("Could not create debugfs directory 'tracing'\n");
3242 #ifdef CONFIG_FTRACE_SELFTEST
3243 /* Let selftest have access to static functions in this file */
3244 #include "trace_selftest.c"
3247 static __init int tracer_init_debugfs(void)
3249 struct dentry *d_tracer;
3250 struct dentry *entry;
3252 d_tracer = tracing_init_dentry();
3254 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3255 &global_trace, &tracing_ctrl_fops);
3257 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3259 entry = debugfs_create_file("trace_options", 0644, d_tracer,
3260 NULL, &tracing_iter_fops);
3262 pr_warning("Could not create debugfs 'trace_options' entry\n");
3264 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3265 NULL, &tracing_cpumask_fops);
3267 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3269 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3270 &global_trace, &tracing_lt_fops);
3272 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3274 entry = debugfs_create_file("trace", 0444, d_tracer,
3275 &global_trace, &tracing_fops);
3277 pr_warning("Could not create debugfs 'trace' entry\n");
3279 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3280 &global_trace, &show_traces_fops);
3282 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3284 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3285 &global_trace, &set_tracer_fops);
3287 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3289 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3290 &tracing_max_latency,
3291 &tracing_max_lat_fops);
3293 pr_warning("Could not create debugfs "
3294 "'tracing_max_latency' entry\n");
3296 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3297 &tracing_thresh, &tracing_max_lat_fops);
3299 pr_warning("Could not create debugfs "
3300 "'tracing_thresh' entry\n");
3301 entry = debugfs_create_file("README", 0644, d_tracer,
3302 NULL, &tracing_readme_fops);
3304 pr_warning("Could not create debugfs 'README' entry\n");
3306 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3307 NULL, &tracing_pipe_fops);
3309 pr_warning("Could not create debugfs "
3310 "'trace_pipe' entry\n");
3312 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
3313 &global_trace, &tracing_entries_fops);
3315 pr_warning("Could not create debugfs "
3316 "'buffer_size_kb' entry\n");
3318 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3319 NULL, &tracing_mark_fops);
3321 pr_warning("Could not create debugfs "
3322 "'trace_marker' entry\n");
3324 #ifdef CONFIG_DYNAMIC_FTRACE
3325 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3326 &ftrace_update_tot_cnt,
3327 &tracing_dyn_info_fops);
3329 pr_warning("Could not create debugfs "
3330 "'dyn_ftrace_total_info' entry\n");
3332 #ifdef CONFIG_SYSPROF_TRACER
3333 init_tracer_sysprof_debugfs(d_tracer);
3338 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3340 static DEFINE_SPINLOCK(trace_buf_lock);
3341 static char trace_buf[TRACE_BUF_SIZE];
3343 struct ring_buffer_event *event;
3344 struct trace_array *tr = &global_trace;
3345 struct trace_array_cpu *data;
3346 struct print_entry *entry;
3347 unsigned long flags, irq_flags;
3348 int cpu, len = 0, size, pc;
3350 if (tracing_disabled)
3353 pc = preempt_count();
3354 preempt_disable_notrace();
3355 cpu = raw_smp_processor_id();
3356 data = tr->data[cpu];
3358 if (unlikely(atomic_read(&data->disabled)))
3361 spin_lock_irqsave(&trace_buf_lock, flags);
3362 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3364 len = min(len, TRACE_BUF_SIZE-1);
3367 size = sizeof(*entry) + len + 1;
3368 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3371 entry = ring_buffer_event_data(event);
3372 tracing_generic_entry_update(&entry->ent, flags, pc);
3373 entry->ent.type = TRACE_PRINT;
3376 memcpy(&entry->buf, trace_buf, len);
3377 entry->buf[len] = 0;
3378 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3381 spin_unlock_irqrestore(&trace_buf_lock, flags);
3384 preempt_enable_notrace();
3388 EXPORT_SYMBOL_GPL(trace_vprintk);
3390 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3395 if (!(trace_flags & TRACE_ITER_PRINTK))
3399 ret = trace_vprintk(ip, fmt, ap);
3403 EXPORT_SYMBOL_GPL(__ftrace_printk);
3405 static int trace_panic_handler(struct notifier_block *this,
3406 unsigned long event, void *unused)
3408 if (ftrace_dump_on_oops)
3413 static struct notifier_block trace_panic_notifier = {
3414 .notifier_call = trace_panic_handler,
3416 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3419 static int trace_die_handler(struct notifier_block *self,
3425 if (ftrace_dump_on_oops)
3434 static struct notifier_block trace_die_notifier = {
3435 .notifier_call = trace_die_handler,
3440 * printk is set to max of 1024, we really don't need it that big.
3441 * Nothing should be printing 1000 characters anyway.
3443 #define TRACE_MAX_PRINT 1000
3446 * Define here KERN_TRACE so that we have one place to modify
3447 * it if we decide to change what log level the ftrace dump
3450 #define KERN_TRACE KERN_INFO
3453 trace_printk_seq(struct trace_seq *s)
3455 /* Probably should print a warning here. */
3459 /* should be zero ended, but we are paranoid. */
3460 s->buffer[s->len] = 0;
3462 printk(KERN_TRACE "%s", s->buffer);
3467 void ftrace_dump(void)
3469 static DEFINE_SPINLOCK(ftrace_dump_lock);
3470 /* use static because iter can be a bit big for the stack */
3471 static struct trace_iterator iter;
3472 static cpumask_t mask;
3473 static int dump_ran;
3474 unsigned long flags;
3478 spin_lock_irqsave(&ftrace_dump_lock, flags);
3484 /* No turning back! */
3487 for_each_tracing_cpu(cpu) {
3488 atomic_inc(&global_trace.data[cpu]->disabled);
3491 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3493 iter.tr = &global_trace;
3494 iter.trace = current_trace;
3497 * We need to stop all tracing on all CPUS to read the
3498 * the next buffer. This is a bit expensive, but is
3499 * not done often. We fill all what we can read,
3500 * and then release the locks again.
3505 while (!trace_empty(&iter)) {
3508 printk(KERN_TRACE "---------------------------------\n");
3512 /* reset all but tr, trace, and overruns */
3513 memset(&iter.seq, 0,
3514 sizeof(struct trace_iterator) -
3515 offsetof(struct trace_iterator, seq));
3516 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3519 if (find_next_entry_inc(&iter) != NULL) {
3520 print_trace_line(&iter);
3521 trace_consume(&iter);
3524 trace_printk_seq(&iter.seq);
3528 printk(KERN_TRACE " (ftrace buffer empty)\n");
3530 printk(KERN_TRACE "---------------------------------\n");
3533 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3536 __init static int tracer_alloc_buffers(void)
3538 struct trace_array_cpu *data;
3541 /* TODO: make the number of buffers hot pluggable with CPUS */
3542 tracing_buffer_mask = cpu_possible_map;
3544 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3545 TRACE_BUFFER_FLAGS);
3546 if (!global_trace.buffer) {
3547 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3551 global_trace.entries = ring_buffer_size(global_trace.buffer);
3553 #ifdef CONFIG_TRACER_MAX_TRACE
3554 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3555 TRACE_BUFFER_FLAGS);
3556 if (!max_tr.buffer) {
3557 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3559 ring_buffer_free(global_trace.buffer);
3562 max_tr.entries = ring_buffer_size(max_tr.buffer);
3563 WARN_ON(max_tr.entries != global_trace.entries);
3566 /* Allocate the first page for all buffers */
3567 for_each_tracing_cpu(i) {
3568 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3569 max_tr.data[i] = &per_cpu(max_data, i);
3572 trace_init_cmdlines();
3574 register_tracer(&nop_trace);
3575 #ifdef CONFIG_BOOT_TRACER
3576 register_tracer(&boot_tracer);
3577 current_trace = &boot_tracer;
3578 current_trace->init(&global_trace);
3580 current_trace = &nop_trace;
3583 /* All seems OK, enable tracing */
3584 tracing_disabled = 0;
3586 atomic_notifier_chain_register(&panic_notifier_list,
3587 &trace_panic_notifier);
3589 register_die_notifier(&trace_die_notifier);
3593 early_initcall(tracer_alloc_buffers);
3594 fs_initcall(tracer_init_debugfs);