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>
39 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
40 unsigned long __read_mostly tracing_thresh;
42 static unsigned long __read_mostly tracing_nr_buffers;
43 static cpumask_t __read_mostly tracing_buffer_mask;
45 #define for_each_tracing_cpu(cpu) \
46 for_each_cpu_mask(cpu, tracing_buffer_mask)
48 static int trace_alloc_page(void);
49 static int trace_free_page(void);
51 static int tracing_disabled = 1;
53 static unsigned long tracing_pages_allocated;
56 ns2usecs(cycle_t nsec)
63 cycle_t ftrace_now(int cpu)
65 return cpu_clock(cpu);
69 * The global_trace is the descriptor that holds the tracing
70 * buffers for the live tracing. For each CPU, it contains
71 * a link list of pages that will store trace entries. The
72 * page descriptor of the pages in the memory is used to hold
73 * the link list by linking the lru item in the page descriptor
74 * to each of the pages in the buffer per CPU.
76 * For each active CPU there is a data field that holds the
77 * pages for the buffer for that CPU. Each CPU has the same number
78 * of pages allocated for its buffer.
80 static struct trace_array global_trace;
82 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
85 * The max_tr is used to snapshot the global_trace when a maximum
86 * latency is reached. Some tracers will use this to store a maximum
87 * trace while it continues examining live traces.
89 * The buffers for the max_tr are set up the same as the global_trace.
90 * When a snapshot is taken, the link list of the max_tr is swapped
91 * with the link list of the global_trace and the buffers are reset for
92 * the global_trace so the tracing can continue.
94 static struct trace_array max_tr;
96 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
98 /* tracer_enabled is used to toggle activation of a tracer */
99 static int tracer_enabled = 1;
101 /* function tracing enabled */
102 int ftrace_function_enabled;
105 * trace_nr_entries is the number of entries that is allocated
106 * for a buffer. Note, the number of entries is always rounded
107 * to ENTRIES_PER_PAGE.
109 * This number is purposely set to a low number of 16384.
110 * If the dump on oops happens, it will be much appreciated
111 * to not have to wait for all that output. Anyway this can be
112 * boot time and run time configurable.
114 #define TRACE_ENTRIES_DEFAULT 16384UL
116 static unsigned long trace_nr_entries = TRACE_ENTRIES_DEFAULT;
118 /* trace_types holds a link list of available tracers. */
119 static struct tracer *trace_types __read_mostly;
121 /* current_trace points to the tracer that is currently active */
122 static struct tracer *current_trace __read_mostly;
125 * max_tracer_type_len is used to simplify the allocating of
126 * buffers to read userspace tracer names. We keep track of
127 * the longest tracer name registered.
129 static int max_tracer_type_len;
132 * trace_types_lock is used to protect the trace_types list.
133 * This lock is also used to keep user access serialized.
134 * Accesses from userspace will grab this lock while userspace
135 * activities happen inside the kernel.
137 static DEFINE_MUTEX(trace_types_lock);
139 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
140 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
142 /* trace_flags holds iter_ctrl options */
143 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
145 static notrace void no_trace_init(struct trace_array *tr)
149 ftrace_function_enabled = 0;
151 for_each_online_cpu(cpu)
152 tracing_reset(tr->data[cpu]);
156 /* dummy trace to disable tracing */
157 static struct tracer no_tracer __read_mostly = {
159 .init = no_trace_init
164 * trace_wake_up - wake up tasks waiting for trace input
166 * Simply wakes up any task that is blocked on the trace_wait
167 * queue. These is used with trace_poll for tasks polling the trace.
169 void trace_wake_up(void)
172 * The runqueue_is_locked() can fail, but this is the best we
175 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
176 wake_up(&trace_wait);
179 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
181 static int __init set_nr_entries(char *str)
183 unsigned long nr_entries;
188 ret = strict_strtoul(str, 0, &nr_entries);
189 /* nr_entries can not be zero */
190 if (ret < 0 || nr_entries == 0)
192 trace_nr_entries = nr_entries;
195 __setup("trace_entries=", set_nr_entries);
197 unsigned long nsecs_to_usecs(unsigned long nsecs)
203 * trace_flag_type is an enumeration that holds different
204 * states when a trace occurs. These are:
205 * IRQS_OFF - interrupts were disabled
206 * NEED_RESCED - reschedule is requested
207 * HARDIRQ - inside an interrupt handler
208 * SOFTIRQ - inside a softirq handler
209 * CONT - multiple entries hold the trace item
211 enum trace_flag_type {
212 TRACE_FLAG_IRQS_OFF = 0x01,
213 TRACE_FLAG_NEED_RESCHED = 0x02,
214 TRACE_FLAG_HARDIRQ = 0x04,
215 TRACE_FLAG_SOFTIRQ = 0x08,
216 TRACE_FLAG_CONT = 0x10,
220 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
221 * control the output of kernel symbols.
223 #define TRACE_ITER_SYM_MASK \
224 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
226 /* These must match the bit postions in trace_iterator_flags */
227 static const char *trace_options[] = {
242 * ftrace_max_lock is used to protect the swapping of buffers
243 * when taking a max snapshot. The buffers themselves are
244 * protected by per_cpu spinlocks. But the action of the swap
245 * needs its own lock.
247 * This is defined as a raw_spinlock_t in order to help
248 * with performance when lockdep debugging is enabled.
250 static raw_spinlock_t ftrace_max_lock =
251 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
254 * Copy the new maximum trace into the separate maximum-trace
255 * structure. (this way the maximum trace is permanently saved,
256 * for later retrieval via /debugfs/tracing/latency_trace)
259 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
261 struct trace_array_cpu *data = tr->data[cpu];
264 max_tr.time_start = data->preempt_timestamp;
266 data = max_tr.data[cpu];
267 data->saved_latency = tracing_max_latency;
269 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
270 data->pid = tsk->pid;
271 data->uid = tsk->uid;
272 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
273 data->policy = tsk->policy;
274 data->rt_priority = tsk->rt_priority;
276 /* record this tasks comm */
277 tracing_record_cmdline(current);
280 #define CHECK_COND(cond) \
281 if (unlikely(cond)) { \
282 tracing_disabled = 1; \
288 * check_pages - integrity check of trace buffers
290 * As a safty measure we check to make sure the data pages have not
293 int check_pages(struct trace_array_cpu *data)
295 struct page *page, *tmp;
297 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
298 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
300 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
301 CHECK_COND(page->lru.next->prev != &page->lru);
302 CHECK_COND(page->lru.prev->next != &page->lru);
309 * head_page - page address of the first page in per_cpu buffer.
311 * head_page returns the page address of the first page in
312 * a per_cpu buffer. This also preforms various consistency
313 * checks to make sure the buffer has not been corrupted.
315 void *head_page(struct trace_array_cpu *data)
319 if (list_empty(&data->trace_pages))
322 page = list_entry(data->trace_pages.next, struct page, lru);
323 BUG_ON(&page->lru == &data->trace_pages);
325 return page_address(page);
329 * trace_seq_printf - sequence printing of trace information
330 * @s: trace sequence descriptor
331 * @fmt: printf format string
333 * The tracer may use either sequence operations or its own
334 * copy to user routines. To simplify formating of a trace
335 * trace_seq_printf is used to store strings into a special
336 * buffer (@s). Then the output may be either used by
337 * the sequencer or pulled into another buffer.
340 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
342 int len = (PAGE_SIZE - 1) - s->len;
350 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
353 /* If we can't write it all, don't bother writing anything */
363 * trace_seq_puts - trace sequence printing of simple string
364 * @s: trace sequence descriptor
365 * @str: simple string to record
367 * The tracer may use either the sequence operations or its own
368 * copy to user routines. This function records a simple string
369 * into a special buffer (@s) for later retrieval by a sequencer
370 * or other mechanism.
373 trace_seq_puts(struct trace_seq *s, const char *str)
375 int len = strlen(str);
377 if (len > ((PAGE_SIZE - 1) - s->len))
380 memcpy(s->buffer + s->len, str, len);
387 trace_seq_putc(struct trace_seq *s, unsigned char c)
389 if (s->len >= (PAGE_SIZE - 1))
392 s->buffer[s->len++] = c;
398 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
400 if (len > ((PAGE_SIZE - 1) - s->len))
403 memcpy(s->buffer + s->len, mem, len);
410 static const char hex2asc[] = "0123456789abcdef";
413 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
415 unsigned char hex[HEX_CHARS];
416 unsigned char *data = mem;
420 BUG_ON(len >= HEX_CHARS);
423 for (i = 0, j = 0; i < len; i++) {
425 for (i = len-1, j = 0; i >= 0; i--) {
429 hex[j++] = hex2asc[byte & 0x0f];
430 hex[j++] = hex2asc[byte >> 4];
434 return trace_seq_putmem(s, hex, j);
438 trace_seq_reset(struct trace_seq *s)
444 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
449 if (s->len <= s->readpos)
452 len = s->len - s->readpos;
455 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
464 trace_print_seq(struct seq_file *m, struct trace_seq *s)
466 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
469 seq_puts(m, s->buffer);
475 * flip the trace buffers between two trace descriptors.
476 * This usually is the buffers between the global_trace and
477 * the max_tr to record a snapshot of a current trace.
479 * The ftrace_max_lock must be held.
482 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
484 struct list_head flip_pages;
486 INIT_LIST_HEAD(&flip_pages);
488 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
489 sizeof(struct trace_array_cpu) -
490 offsetof(struct trace_array_cpu, trace_head_idx));
494 list_splice_init(&tr1->trace_pages, &flip_pages);
495 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
496 list_splice_init(&flip_pages, &tr2->trace_pages);
497 BUG_ON(!list_empty(&flip_pages));
503 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
505 * @tsk: the task with the latency
506 * @cpu: The cpu that initiated the trace.
508 * Flip the buffers between the @tr and the max_tr and record information
509 * about which task was the cause of this latency.
512 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
514 struct trace_array_cpu *data;
517 WARN_ON_ONCE(!irqs_disabled());
518 __raw_spin_lock(&ftrace_max_lock);
519 /* clear out all the previous traces */
520 for_each_tracing_cpu(i) {
522 flip_trace(max_tr.data[i], data);
526 __update_max_tr(tr, tsk, cpu);
527 __raw_spin_unlock(&ftrace_max_lock);
531 * update_max_tr_single - only copy one trace over, and reset the rest
533 * @tsk - task with the latency
534 * @cpu - the cpu of the buffer to copy.
536 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
539 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
541 struct trace_array_cpu *data = tr->data[cpu];
544 WARN_ON_ONCE(!irqs_disabled());
545 __raw_spin_lock(&ftrace_max_lock);
546 for_each_tracing_cpu(i)
547 tracing_reset(max_tr.data[i]);
549 flip_trace(max_tr.data[cpu], data);
552 __update_max_tr(tr, tsk, cpu);
553 __raw_spin_unlock(&ftrace_max_lock);
557 * register_tracer - register a tracer with the ftrace system.
558 * @type - the plugin for the tracer
560 * Register a new plugin tracer.
562 int register_tracer(struct tracer *type)
569 pr_info("Tracer must have a name\n");
573 mutex_lock(&trace_types_lock);
574 for (t = trace_types; t; t = t->next) {
575 if (strcmp(type->name, t->name) == 0) {
577 pr_info("Trace %s already registered\n",
584 #ifdef CONFIG_FTRACE_STARTUP_TEST
585 if (type->selftest) {
586 struct tracer *saved_tracer = current_trace;
587 struct trace_array_cpu *data;
588 struct trace_array *tr = &global_trace;
589 int saved_ctrl = tr->ctrl;
592 * Run a selftest on this tracer.
593 * Here we reset the trace buffer, and set the current
594 * tracer to be this tracer. The tracer can then run some
595 * internal tracing to verify that everything is in order.
596 * If we fail, we do not register this tracer.
598 for_each_tracing_cpu(i) {
600 if (!head_page(data))
604 current_trace = type;
606 /* the test is responsible for initializing and enabling */
607 pr_info("Testing tracer %s: ", type->name);
608 ret = type->selftest(type, tr);
609 /* the test is responsible for resetting too */
610 current_trace = saved_tracer;
611 tr->ctrl = saved_ctrl;
613 printk(KERN_CONT "FAILED!\n");
616 /* Only reset on passing, to avoid touching corrupted buffers */
617 for_each_tracing_cpu(i) {
619 if (!head_page(data))
623 printk(KERN_CONT "PASSED\n");
627 type->next = trace_types;
629 len = strlen(type->name);
630 if (len > max_tracer_type_len)
631 max_tracer_type_len = len;
634 mutex_unlock(&trace_types_lock);
639 void unregister_tracer(struct tracer *type)
644 mutex_lock(&trace_types_lock);
645 for (t = &trace_types; *t; t = &(*t)->next) {
649 pr_info("Trace %s not registered\n", type->name);
654 if (strlen(type->name) != max_tracer_type_len)
657 max_tracer_type_len = 0;
658 for (t = &trace_types; *t; t = &(*t)->next) {
659 len = strlen((*t)->name);
660 if (len > max_tracer_type_len)
661 max_tracer_type_len = len;
664 mutex_unlock(&trace_types_lock);
667 void tracing_reset(struct trace_array_cpu *data)
671 data->trace_head = data->trace_tail = head_page(data);
672 data->trace_head_idx = 0;
673 data->trace_tail_idx = 0;
676 #define SAVED_CMDLINES 128
677 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
678 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
679 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
680 static int cmdline_idx;
681 static DEFINE_SPINLOCK(trace_cmdline_lock);
683 /* temporary disable recording */
684 atomic_t trace_record_cmdline_disabled __read_mostly;
686 static void trace_init_cmdlines(void)
688 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
689 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
693 void trace_stop_cmdline_recording(void);
695 static void trace_save_cmdline(struct task_struct *tsk)
700 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
704 * It's not the end of the world if we don't get
705 * the lock, but we also don't want to spin
706 * nor do we want to disable interrupts,
707 * so if we miss here, then better luck next time.
709 if (!spin_trylock(&trace_cmdline_lock))
712 idx = map_pid_to_cmdline[tsk->pid];
713 if (idx >= SAVED_CMDLINES) {
714 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
716 map = map_cmdline_to_pid[idx];
717 if (map <= PID_MAX_DEFAULT)
718 map_pid_to_cmdline[map] = (unsigned)-1;
720 map_pid_to_cmdline[tsk->pid] = idx;
725 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
727 spin_unlock(&trace_cmdline_lock);
730 static char *trace_find_cmdline(int pid)
732 char *cmdline = "<...>";
738 if (pid > PID_MAX_DEFAULT)
741 map = map_pid_to_cmdline[pid];
742 if (map >= SAVED_CMDLINES)
745 cmdline = saved_cmdlines[map];
751 void tracing_record_cmdline(struct task_struct *tsk)
753 if (atomic_read(&trace_record_cmdline_disabled))
756 trace_save_cmdline(tsk);
759 static inline struct list_head *
760 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
763 * Roundrobin - but skip the head (which is not a real page):
766 if (unlikely(next == &data->trace_pages))
768 BUG_ON(next == &data->trace_pages);
774 trace_next_page(struct trace_array_cpu *data, void *addr)
776 struct list_head *next;
779 page = virt_to_page(addr);
781 next = trace_next_list(data, &page->lru);
782 page = list_entry(next, struct page, lru);
784 return page_address(page);
787 static inline struct trace_entry *
788 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
790 unsigned long idx, idx_next;
791 struct trace_entry *entry;
794 idx = data->trace_head_idx;
797 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
799 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
801 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
802 data->trace_head = trace_next_page(data, data->trace_head);
806 if (data->trace_head == data->trace_tail &&
807 idx_next == data->trace_tail_idx) {
810 data->trace_tail_idx++;
811 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
813 trace_next_page(data, data->trace_tail);
814 data->trace_tail_idx = 0;
818 data->trace_head_idx = idx_next;
824 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
826 struct task_struct *tsk = current;
829 pc = preempt_count();
831 entry->field.preempt_count = pc & 0xff;
832 entry->field.pid = (tsk) ? tsk->pid : 0;
833 entry->field.t = ftrace_now(raw_smp_processor_id());
835 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
836 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
837 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
838 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
842 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
843 unsigned long ip, unsigned long parent_ip, unsigned long flags)
845 struct trace_entry *entry;
846 unsigned long irq_flags;
848 raw_local_irq_save(irq_flags);
849 __raw_spin_lock(&data->lock);
850 entry = tracing_get_trace_entry(tr, data);
851 tracing_generic_entry_update(entry, flags);
852 entry->type = TRACE_FN;
853 entry->field.fn.ip = ip;
854 entry->field.fn.parent_ip = parent_ip;
855 __raw_spin_unlock(&data->lock);
856 raw_local_irq_restore(irq_flags);
860 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
861 unsigned long ip, unsigned long parent_ip, unsigned long flags)
863 if (likely(!atomic_read(&data->disabled)))
864 trace_function(tr, data, ip, parent_ip, flags);
867 #ifdef CONFIG_MMIOTRACE
868 void __trace_mmiotrace_rw(struct trace_array *tr, struct trace_array_cpu *data,
869 struct mmiotrace_rw *rw)
871 struct trace_entry *entry;
872 unsigned long irq_flags;
874 raw_local_irq_save(irq_flags);
875 __raw_spin_lock(&data->lock);
877 entry = tracing_get_trace_entry(tr, data);
878 tracing_generic_entry_update(entry, 0);
879 entry->type = TRACE_MMIO_RW;
880 entry->field.mmiorw = *rw;
882 __raw_spin_unlock(&data->lock);
883 raw_local_irq_restore(irq_flags);
888 void __trace_mmiotrace_map(struct trace_array *tr, struct trace_array_cpu *data,
889 struct mmiotrace_map *map)
891 struct trace_entry *entry;
892 unsigned long irq_flags;
894 raw_local_irq_save(irq_flags);
895 __raw_spin_lock(&data->lock);
897 entry = tracing_get_trace_entry(tr, data);
898 tracing_generic_entry_update(entry, 0);
899 entry->type = TRACE_MMIO_MAP;
900 entry->field.mmiomap = *map;
902 __raw_spin_unlock(&data->lock);
903 raw_local_irq_restore(irq_flags);
909 void __trace_stack(struct trace_array *tr,
910 struct trace_array_cpu *data,
914 struct trace_entry *entry;
915 struct stack_trace trace;
917 if (!(trace_flags & TRACE_ITER_STACKTRACE))
920 entry = tracing_get_trace_entry(tr, data);
921 tracing_generic_entry_update(entry, flags);
922 entry->type = TRACE_STACK;
924 memset(&entry->field.stack, 0, sizeof(entry->field.stack));
926 trace.nr_entries = 0;
927 trace.max_entries = FTRACE_STACK_ENTRIES;
929 trace.entries = entry->field.stack.caller;
931 save_stack_trace(&trace);
935 __trace_special(void *__tr, void *__data,
936 unsigned long arg1, unsigned long arg2, unsigned long arg3)
938 struct trace_array_cpu *data = __data;
939 struct trace_array *tr = __tr;
940 struct trace_entry *entry;
941 unsigned long irq_flags;
943 raw_local_irq_save(irq_flags);
944 __raw_spin_lock(&data->lock);
945 entry = tracing_get_trace_entry(tr, data);
946 tracing_generic_entry_update(entry, 0);
947 entry->type = TRACE_SPECIAL;
948 entry->field.special.arg1 = arg1;
949 entry->field.special.arg2 = arg2;
950 entry->field.special.arg3 = arg3;
951 __trace_stack(tr, data, irq_flags, 4);
952 __raw_spin_unlock(&data->lock);
953 raw_local_irq_restore(irq_flags);
959 tracing_sched_switch_trace(struct trace_array *tr,
960 struct trace_array_cpu *data,
961 struct task_struct *prev,
962 struct task_struct *next,
965 struct trace_entry *entry;
966 unsigned long irq_flags;
968 raw_local_irq_save(irq_flags);
969 __raw_spin_lock(&data->lock);
970 entry = tracing_get_trace_entry(tr, data);
971 tracing_generic_entry_update(entry, flags);
972 entry->type = TRACE_CTX;
973 entry->field.ctx.prev_pid = prev->pid;
974 entry->field.ctx.prev_prio = prev->prio;
975 entry->field.ctx.prev_state = prev->state;
976 entry->field.ctx.next_pid = next->pid;
977 entry->field.ctx.next_prio = next->prio;
978 entry->field.ctx.next_state = next->state;
979 __trace_stack(tr, data, flags, 5);
980 __raw_spin_unlock(&data->lock);
981 raw_local_irq_restore(irq_flags);
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,
991 struct trace_entry *entry;
992 unsigned long irq_flags;
994 raw_local_irq_save(irq_flags);
995 __raw_spin_lock(&data->lock);
996 entry = tracing_get_trace_entry(tr, data);
997 tracing_generic_entry_update(entry, flags);
998 entry->type = TRACE_WAKE;
999 entry->field.ctx.prev_pid = curr->pid;
1000 entry->field.ctx.prev_prio = curr->prio;
1001 entry->field.ctx.prev_state = curr->state;
1002 entry->field.ctx.next_pid = wakee->pid;
1003 entry->field.ctx.next_prio = wakee->prio;
1004 entry->field.ctx.next_state = wakee->state;
1005 __trace_stack(tr, data, flags, 6);
1006 __raw_spin_unlock(&data->lock);
1007 raw_local_irq_restore(irq_flags);
1013 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1015 struct trace_array *tr = &global_trace;
1016 struct trace_array_cpu *data;
1017 unsigned long flags;
1021 if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
1024 local_irq_save(flags);
1025 cpu = raw_smp_processor_id();
1026 data = tr->data[cpu];
1027 disabled = atomic_inc_return(&data->disabled);
1029 if (likely(disabled == 1))
1030 __trace_special(tr, data, arg1, arg2, arg3);
1032 atomic_dec(&data->disabled);
1033 local_irq_restore(flags);
1036 #ifdef CONFIG_FTRACE
1038 function_trace_call(unsigned long ip, unsigned long parent_ip)
1040 struct trace_array *tr = &global_trace;
1041 struct trace_array_cpu *data;
1042 unsigned long flags;
1046 if (unlikely(!ftrace_function_enabled))
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 trace_function(tr, data, ip, parent_ip, flags);
1060 atomic_dec(&data->disabled);
1061 local_irq_restore(flags);
1064 static struct ftrace_ops trace_ops __read_mostly =
1066 .func = function_trace_call,
1069 void tracing_start_function_trace(void)
1071 ftrace_function_enabled = 0;
1072 register_ftrace_function(&trace_ops);
1074 ftrace_function_enabled = 1;
1077 void tracing_stop_function_trace(void)
1079 ftrace_function_enabled = 0;
1080 unregister_ftrace_function(&trace_ops);
1084 enum trace_file_type {
1085 TRACE_FILE_LAT_FMT = 1,
1088 /* Return the current entry. */
1089 static struct trace_entry *
1090 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1091 struct trace_iterator *iter, int cpu)
1094 struct trace_entry *array;
1096 if (iter->next_idx[cpu] >= tr->entries ||
1097 iter->next_idx[cpu] >= data->trace_idx ||
1098 (data->trace_head == data->trace_tail &&
1099 data->trace_head_idx == data->trace_tail_idx))
1102 if (!iter->next_page[cpu]) {
1103 /* Initialize the iterator for this cpu trace buffer */
1104 WARN_ON(!data->trace_tail);
1105 page = virt_to_page(data->trace_tail);
1106 iter->next_page[cpu] = &page->lru;
1107 iter->next_page_idx[cpu] = data->trace_tail_idx;
1110 page = list_entry(iter->next_page[cpu], struct page, lru);
1111 BUG_ON(&data->trace_pages == &page->lru);
1113 array = page_address(page);
1115 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
1116 return &array[iter->next_page_idx[cpu]];
1119 /* Increment the index counter of an iterator by one */
1120 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1123 iter->next_idx[cpu]++;
1124 iter->next_page_idx[cpu]++;
1126 if (iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE) {
1127 struct trace_array_cpu *data = iter->tr->data[cpu];
1129 iter->next_page_idx[cpu] = 0;
1130 iter->next_page[cpu] =
1131 trace_next_list(data, iter->next_page[cpu]);
1135 static struct trace_entry *
1136 trace_entry_next(struct trace_array *tr, struct trace_array_cpu *data,
1137 struct trace_iterator *iter, int cpu)
1139 struct list_head *next_page;
1140 struct trace_entry *ent;
1141 int idx, next_idx, next_page_idx;
1143 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1145 if (likely(!ent || ent->type != TRACE_CONT))
1148 /* save the iterator details */
1150 next_idx = iter->next_idx[cpu];
1151 next_page_idx = iter->next_page_idx[cpu];
1152 next_page = iter->next_page[cpu];
1154 /* find a real entry */
1156 trace_iterator_increment(iter, cpu);
1157 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1158 } while (ent && ent->type != TRACE_CONT);
1160 /* reset the iterator */
1162 iter->next_idx[cpu] = next_idx;
1163 iter->next_page_idx[cpu] = next_page_idx;
1164 iter->next_page[cpu] = next_page;
1169 static struct trace_entry *
1170 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, int inc)
1172 struct trace_array *tr = iter->tr;
1173 struct trace_entry *ent, *next = NULL;
1177 for_each_tracing_cpu(cpu) {
1178 if (!head_page(tr->data[cpu]))
1181 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1183 if (ent && ent->type == TRACE_CONT) {
1184 struct trace_array_cpu *data = tr->data[cpu];
1187 ent = trace_entry_next(tr, data, iter, cpu);
1189 while (ent && ent->type == TRACE_CONT) {
1190 trace_iterator_increment(iter, cpu);
1191 ent = trace_entry_idx(tr, tr->data[cpu],
1198 * Pick the entry with the smallest timestamp:
1200 if (ent && (!next || ent->field.t < next->field.t)) {
1207 *ent_cpu = next_cpu;
1212 /* Find the next real entry, without updating the iterator itself */
1213 static struct trace_entry *
1214 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1216 return __find_next_entry(iter, ent_cpu, 0);
1219 /* Find the next real entry, and increment the iterator to the next entry */
1220 static void *find_next_entry_inc(struct trace_iterator *iter)
1222 struct trace_entry *next;
1225 next = __find_next_entry(iter, &next_cpu, 1);
1227 iter->prev_ent = iter->ent;
1228 iter->prev_cpu = iter->cpu;
1231 iter->cpu = next_cpu;
1234 trace_iterator_increment(iter, iter->cpu);
1236 return next ? iter : NULL;
1239 static void trace_consume(struct trace_iterator *iter)
1241 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1242 struct trace_entry *ent;
1245 data->trace_tail_idx++;
1246 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1247 data->trace_tail = trace_next_page(data, data->trace_tail);
1248 data->trace_tail_idx = 0;
1251 /* Check if we empty it, then reset the index */
1252 if (data->trace_head == data->trace_tail &&
1253 data->trace_head_idx == data->trace_tail_idx)
1254 data->trace_idx = 0;
1256 ent = trace_entry_idx(iter->tr, iter->tr->data[iter->cpu],
1258 if (ent && ent->type == TRACE_CONT)
1262 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1264 struct trace_iterator *iter = m->private;
1270 /* can't go backwards */
1275 ent = find_next_entry_inc(iter);
1279 while (ent && iter->idx < i)
1280 ent = find_next_entry_inc(iter);
1287 static void *s_start(struct seq_file *m, loff_t *pos)
1289 struct trace_iterator *iter = m->private;
1294 mutex_lock(&trace_types_lock);
1296 if (!current_trace || current_trace != iter->trace) {
1297 mutex_unlock(&trace_types_lock);
1301 atomic_inc(&trace_record_cmdline_disabled);
1303 /* let the tracer grab locks here if needed */
1304 if (current_trace->start)
1305 current_trace->start(iter);
1307 if (*pos != iter->pos) {
1311 iter->prev_ent = NULL;
1312 iter->prev_cpu = -1;
1314 for_each_tracing_cpu(i) {
1315 iter->next_idx[i] = 0;
1316 iter->next_page[i] = NULL;
1319 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1324 p = s_next(m, p, &l);
1330 static void s_stop(struct seq_file *m, void *p)
1332 struct trace_iterator *iter = m->private;
1334 atomic_dec(&trace_record_cmdline_disabled);
1336 /* let the tracer release locks here if needed */
1337 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1338 iter->trace->stop(iter);
1340 mutex_unlock(&trace_types_lock);
1343 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1345 #ifdef CONFIG_KRETPROBES
1346 static inline int kretprobed(unsigned long addr)
1348 return addr == (unsigned long)kretprobe_trampoline;
1351 static inline int kretprobed(unsigned long addr)
1355 #endif /* CONFIG_KRETPROBES */
1358 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1360 #ifdef CONFIG_KALLSYMS
1361 char str[KSYM_SYMBOL_LEN];
1363 kallsyms_lookup(address, NULL, NULL, NULL, str);
1365 return trace_seq_printf(s, fmt, str);
1371 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1372 unsigned long address)
1374 #ifdef CONFIG_KALLSYMS
1375 char str[KSYM_SYMBOL_LEN];
1377 sprint_symbol(str, address);
1378 return trace_seq_printf(s, fmt, str);
1383 #ifndef CONFIG_64BIT
1384 # define IP_FMT "%08lx"
1386 # define IP_FMT "%016lx"
1390 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1395 return trace_seq_printf(s, "0");
1397 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1398 ret = seq_print_sym_offset(s, "%s", ip);
1400 ret = seq_print_sym_short(s, "%s", ip);
1405 if (sym_flags & TRACE_ITER_SYM_ADDR)
1406 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1410 static void print_lat_help_header(struct seq_file *m)
1412 seq_puts(m, "# _------=> CPU# \n");
1413 seq_puts(m, "# / _-----=> irqs-off \n");
1414 seq_puts(m, "# | / _----=> need-resched \n");
1415 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1416 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1417 seq_puts(m, "# |||| / \n");
1418 seq_puts(m, "# ||||| delay \n");
1419 seq_puts(m, "# cmd pid ||||| time | caller \n");
1420 seq_puts(m, "# \\ / ||||| \\ | / \n");
1423 static void print_func_help_header(struct seq_file *m)
1425 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1426 seq_puts(m, "# | | | | |\n");
1431 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1433 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1434 struct trace_array *tr = iter->tr;
1435 struct trace_array_cpu *data = tr->data[tr->cpu];
1436 struct tracer *type = current_trace;
1437 unsigned long total = 0;
1438 unsigned long entries = 0;
1440 const char *name = "preemption";
1445 for_each_tracing_cpu(cpu) {
1446 if (head_page(tr->data[cpu])) {
1447 total += tr->data[cpu]->trace_idx;
1448 if (tr->data[cpu]->trace_idx > tr->entries)
1449 entries += tr->entries;
1451 entries += tr->data[cpu]->trace_idx;
1455 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1457 seq_puts(m, "-----------------------------------"
1458 "---------------------------------\n");
1459 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1460 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1461 nsecs_to_usecs(data->saved_latency),
1465 #if defined(CONFIG_PREEMPT_NONE)
1467 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1469 #elif defined(CONFIG_PREEMPT)
1474 /* These are reserved for later use */
1477 seq_printf(m, " #P:%d)\n", num_online_cpus());
1481 seq_puts(m, " -----------------\n");
1482 seq_printf(m, " | task: %.16s-%d "
1483 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1484 data->comm, data->pid, data->uid, data->nice,
1485 data->policy, data->rt_priority);
1486 seq_puts(m, " -----------------\n");
1488 if (data->critical_start) {
1489 seq_puts(m, " => started at: ");
1490 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1491 trace_print_seq(m, &iter->seq);
1492 seq_puts(m, "\n => ended at: ");
1493 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1494 trace_print_seq(m, &iter->seq);
1502 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1504 struct trace_field *field = &entry->field;
1505 int hardirq, softirq;
1508 comm = trace_find_cmdline(field->pid);
1510 trace_seq_printf(s, "%8.8s-%-5d ", comm, field->pid);
1511 trace_seq_printf(s, "%3d", cpu);
1512 trace_seq_printf(s, "%c%c",
1513 (field->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1514 ((field->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1516 hardirq = field->flags & TRACE_FLAG_HARDIRQ;
1517 softirq = field->flags & TRACE_FLAG_SOFTIRQ;
1518 if (hardirq && softirq) {
1519 trace_seq_putc(s, 'H');
1522 trace_seq_putc(s, 'h');
1525 trace_seq_putc(s, 's');
1527 trace_seq_putc(s, '.');
1531 if (field->preempt_count)
1532 trace_seq_printf(s, "%x", field->preempt_count);
1534 trace_seq_puts(s, ".");
1537 unsigned long preempt_mark_thresh = 100;
1540 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1541 unsigned long rel_usecs)
1543 trace_seq_printf(s, " %4lldus", abs_usecs);
1544 if (rel_usecs > preempt_mark_thresh)
1545 trace_seq_puts(s, "!: ");
1546 else if (rel_usecs > 1)
1547 trace_seq_puts(s, "+: ");
1549 trace_seq_puts(s, " : ");
1552 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1555 trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1557 struct trace_array *tr = iter->tr;
1558 struct trace_array_cpu *data = tr->data[iter->cpu];
1559 struct trace_entry *ent;
1561 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1562 if (!ent || ent->type != TRACE_CONT) {
1563 trace_seq_putc(s, '\n');
1568 trace_seq_printf(s, "%s", ent->cont.buf);
1569 trace_iterator_increment(iter, iter->cpu);
1570 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1571 } while (ent && ent->type == TRACE_CONT);
1575 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1577 struct trace_seq *s = &iter->seq;
1578 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1579 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1580 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1581 struct trace_entry *entry = iter->ent;
1582 struct trace_field *field = &entry->field;
1583 unsigned long abs_usecs;
1584 unsigned long rel_usecs;
1593 if (entry->type == TRACE_CONT)
1596 rel_usecs = ns2usecs(next_entry->field.t - entry->field.t);
1597 abs_usecs = ns2usecs(entry->field.t - iter->tr->time_start);
1600 comm = trace_find_cmdline(field->pid);
1601 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1602 " %ld.%03ldms (+%ld.%03ldms): ",
1604 field->pid, cpu, field->flags,
1605 field->preempt_count, trace_idx,
1608 abs_usecs % 1000, rel_usecs/1000,
1611 lat_print_generic(s, entry, cpu);
1612 lat_print_timestamp(s, abs_usecs, rel_usecs);
1614 switch (entry->type) {
1616 seq_print_ip_sym(s, field->fn.ip, sym_flags);
1617 trace_seq_puts(s, " (");
1618 if (kretprobed(field->fn.parent_ip))
1619 trace_seq_puts(s, KRETPROBE_MSG);
1621 seq_print_ip_sym(s, field->fn.parent_ip, sym_flags);
1622 trace_seq_puts(s, ")\n");
1626 T = field->ctx.next_state < sizeof(state_to_char) ?
1627 state_to_char[field->ctx.next_state] : 'X';
1629 state = field->ctx.prev_state ?
1630 __ffs(field->ctx.prev_state) + 1 : 0;
1631 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1632 comm = trace_find_cmdline(field->ctx.next_pid);
1633 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1634 field->ctx.prev_pid,
1635 field->ctx.prev_prio,
1636 S, entry->type == TRACE_CTX ? "==>" : " +",
1637 field->ctx.next_pid,
1638 field->ctx.next_prio,
1642 trace_seq_printf(s, "# %ld %ld %ld\n",
1643 field->special.arg1,
1644 field->special.arg2,
1645 field->special.arg3);
1648 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1650 trace_seq_puts(s, " <= ");
1651 seq_print_ip_sym(s, field->stack.caller[i], sym_flags);
1653 trace_seq_puts(s, "\n");
1656 seq_print_ip_sym(s, field->print.ip, sym_flags);
1657 trace_seq_printf(s, ": %s", field->print.buf);
1658 if (field->flags & TRACE_FLAG_CONT)
1659 trace_seq_print_cont(s, iter);
1662 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1667 static int print_trace_fmt(struct trace_iterator *iter)
1669 struct trace_seq *s = &iter->seq;
1670 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1671 struct trace_entry *entry;
1672 struct trace_field *field;
1673 unsigned long usec_rem;
1674 unsigned long long t;
1683 if (entry->type == TRACE_CONT)
1686 field = &entry->field;
1688 comm = trace_find_cmdline(iter->ent->field.pid);
1690 t = ns2usecs(field->t);
1691 usec_rem = do_div(t, 1000000ULL);
1692 secs = (unsigned long)t;
1694 ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid);
1697 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1700 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1704 switch (entry->type) {
1706 ret = seq_print_ip_sym(s, field->fn.ip, sym_flags);
1709 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1710 field->fn.parent_ip) {
1711 ret = trace_seq_printf(s, " <-");
1714 if (kretprobed(field->fn.parent_ip))
1715 ret = trace_seq_puts(s, KRETPROBE_MSG);
1717 ret = seq_print_ip_sym(s,
1718 field->fn.parent_ip,
1723 ret = trace_seq_printf(s, "\n");
1729 S = field->ctx.prev_state < sizeof(state_to_char) ?
1730 state_to_char[field->ctx.prev_state] : 'X';
1731 T = field->ctx.next_state < sizeof(state_to_char) ?
1732 state_to_char[field->ctx.next_state] : 'X';
1733 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
1734 field->ctx.prev_pid,
1735 field->ctx.prev_prio,
1737 entry->type == TRACE_CTX ? "==>" : " +",
1738 field->ctx.next_pid,
1739 field->ctx.next_prio,
1745 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1746 field->special.arg1,
1747 field->special.arg2,
1748 field->special.arg3);
1753 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1755 ret = trace_seq_puts(s, " <= ");
1759 ret = seq_print_ip_sym(s, field->stack.caller[i],
1764 ret = trace_seq_puts(s, "\n");
1769 seq_print_ip_sym(s, field->print.ip, sym_flags);
1770 trace_seq_printf(s, ": %s", field->print.buf);
1771 if (field->flags & TRACE_FLAG_CONT)
1772 trace_seq_print_cont(s, iter);
1778 static int print_raw_fmt(struct trace_iterator *iter)
1780 struct trace_seq *s = &iter->seq;
1781 struct trace_entry *entry;
1782 struct trace_field *field;
1788 if (entry->type == TRACE_CONT)
1791 field = &entry->field;
1793 ret = trace_seq_printf(s, "%d %d %llu ",
1794 field->pid, iter->cpu, field->t);
1798 switch (entry->type) {
1800 ret = trace_seq_printf(s, "%x %x\n",
1802 field->fn.parent_ip);
1808 S = field->ctx.prev_state < sizeof(state_to_char) ?
1809 state_to_char[field->ctx.prev_state] : 'X';
1810 T = field->ctx.next_state < sizeof(state_to_char) ?
1811 state_to_char[field->ctx.next_state] : 'X';
1812 if (entry->type == TRACE_WAKE)
1814 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
1815 field->ctx.prev_pid,
1816 field->ctx.prev_prio,
1818 field->ctx.next_pid,
1819 field->ctx.next_prio,
1826 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1827 field->special.arg1,
1828 field->special.arg2,
1829 field->special.arg3);
1834 trace_seq_printf(s, "# %lx %s",
1835 field->print.ip, field->print.buf);
1836 if (field->flags & TRACE_FLAG_CONT)
1837 trace_seq_print_cont(s, iter);
1843 #define SEQ_PUT_FIELD_RET(s, x) \
1845 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1849 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1851 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1855 static int print_hex_fmt(struct trace_iterator *iter)
1857 struct trace_seq *s = &iter->seq;
1858 unsigned char newline = '\n';
1859 struct trace_entry *entry;
1860 struct trace_field *field;
1865 if (entry->type == TRACE_CONT)
1868 field = &entry->field;
1870 SEQ_PUT_HEX_FIELD_RET(s, field->pid);
1871 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1872 SEQ_PUT_HEX_FIELD_RET(s, field->t);
1874 switch (entry->type) {
1876 SEQ_PUT_HEX_FIELD_RET(s, field->fn.ip);
1877 SEQ_PUT_HEX_FIELD_RET(s, field->fn.parent_ip);
1881 S = field->ctx.prev_state < sizeof(state_to_char) ?
1882 state_to_char[field->ctx.prev_state] : 'X';
1883 T = field->ctx.next_state < sizeof(state_to_char) ?
1884 state_to_char[field->ctx.next_state] : 'X';
1885 if (entry->type == TRACE_WAKE)
1887 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_pid);
1888 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_prio);
1889 SEQ_PUT_HEX_FIELD_RET(s, S);
1890 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_pid);
1891 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_prio);
1892 SEQ_PUT_HEX_FIELD_RET(s, T);
1896 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg1);
1897 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg2);
1898 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg3);
1901 SEQ_PUT_FIELD_RET(s, newline);
1906 static int print_bin_fmt(struct trace_iterator *iter)
1908 struct trace_seq *s = &iter->seq;
1909 struct trace_entry *entry;
1910 struct trace_field *field;
1914 if (entry->type == TRACE_CONT)
1917 field = &entry->field;
1919 SEQ_PUT_FIELD_RET(s, field->pid);
1920 SEQ_PUT_FIELD_RET(s, field->cpu);
1921 SEQ_PUT_FIELD_RET(s, field->t);
1923 switch (entry->type) {
1925 SEQ_PUT_FIELD_RET(s, field->fn.ip);
1926 SEQ_PUT_FIELD_RET(s, field->fn.parent_ip);
1929 SEQ_PUT_FIELD_RET(s, field->ctx.prev_pid);
1930 SEQ_PUT_FIELD_RET(s, field->ctx.prev_prio);
1931 SEQ_PUT_FIELD_RET(s, field->ctx.prev_state);
1932 SEQ_PUT_FIELD_RET(s, field->ctx.next_pid);
1933 SEQ_PUT_FIELD_RET(s, field->ctx.next_prio);
1934 SEQ_PUT_FIELD_RET(s, field->ctx.next_state);
1938 SEQ_PUT_FIELD_RET(s, field->special.arg1);
1939 SEQ_PUT_FIELD_RET(s, field->special.arg2);
1940 SEQ_PUT_FIELD_RET(s, field->special.arg3);
1946 static int trace_empty(struct trace_iterator *iter)
1948 struct trace_array_cpu *data;
1951 for_each_tracing_cpu(cpu) {
1952 data = iter->tr->data[cpu];
1954 if (head_page(data) && data->trace_idx &&
1955 (data->trace_tail != data->trace_head ||
1956 data->trace_tail_idx != data->trace_head_idx))
1962 static int print_trace_line(struct trace_iterator *iter)
1964 if (iter->trace && iter->trace->print_line)
1965 return iter->trace->print_line(iter);
1967 if (trace_flags & TRACE_ITER_BIN)
1968 return print_bin_fmt(iter);
1970 if (trace_flags & TRACE_ITER_HEX)
1971 return print_hex_fmt(iter);
1973 if (trace_flags & TRACE_ITER_RAW)
1974 return print_raw_fmt(iter);
1976 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1977 return print_lat_fmt(iter, iter->idx, iter->cpu);
1979 return print_trace_fmt(iter);
1982 static int s_show(struct seq_file *m, void *v)
1984 struct trace_iterator *iter = v;
1986 if (iter->ent == NULL) {
1988 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1991 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1992 /* print nothing if the buffers are empty */
1993 if (trace_empty(iter))
1995 print_trace_header(m, iter);
1996 if (!(trace_flags & TRACE_ITER_VERBOSE))
1997 print_lat_help_header(m);
1999 if (!(trace_flags & TRACE_ITER_VERBOSE))
2000 print_func_help_header(m);
2003 print_trace_line(iter);
2004 trace_print_seq(m, &iter->seq);
2010 static struct seq_operations tracer_seq_ops = {
2017 static struct trace_iterator *
2018 __tracing_open(struct inode *inode, struct file *file, int *ret)
2020 struct trace_iterator *iter;
2022 if (tracing_disabled) {
2027 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2033 mutex_lock(&trace_types_lock);
2034 if (current_trace && current_trace->print_max)
2037 iter->tr = inode->i_private;
2038 iter->trace = current_trace;
2041 /* TODO stop tracer */
2042 *ret = seq_open(file, &tracer_seq_ops);
2044 struct seq_file *m = file->private_data;
2047 /* stop the trace while dumping */
2048 if (iter->tr->ctrl) {
2050 ftrace_function_enabled = 0;
2053 if (iter->trace && iter->trace->open)
2054 iter->trace->open(iter);
2059 mutex_unlock(&trace_types_lock);
2065 int tracing_open_generic(struct inode *inode, struct file *filp)
2067 if (tracing_disabled)
2070 filp->private_data = inode->i_private;
2074 int tracing_release(struct inode *inode, struct file *file)
2076 struct seq_file *m = (struct seq_file *)file->private_data;
2077 struct trace_iterator *iter = m->private;
2079 mutex_lock(&trace_types_lock);
2080 if (iter->trace && iter->trace->close)
2081 iter->trace->close(iter);
2083 /* reenable tracing if it was previously enabled */
2084 if (iter->tr->ctrl) {
2087 * It is safe to enable function tracing even if it
2090 ftrace_function_enabled = 1;
2092 mutex_unlock(&trace_types_lock);
2094 seq_release(inode, file);
2099 static int tracing_open(struct inode *inode, struct file *file)
2103 __tracing_open(inode, file, &ret);
2108 static int tracing_lt_open(struct inode *inode, struct file *file)
2110 struct trace_iterator *iter;
2113 iter = __tracing_open(inode, file, &ret);
2116 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2123 t_next(struct seq_file *m, void *v, loff_t *pos)
2125 struct tracer *t = m->private;
2137 static void *t_start(struct seq_file *m, loff_t *pos)
2139 struct tracer *t = m->private;
2142 mutex_lock(&trace_types_lock);
2143 for (; t && l < *pos; t = t_next(m, t, &l))
2149 static void t_stop(struct seq_file *m, void *p)
2151 mutex_unlock(&trace_types_lock);
2154 static int t_show(struct seq_file *m, void *v)
2156 struct tracer *t = v;
2161 seq_printf(m, "%s", t->name);
2170 static struct seq_operations show_traces_seq_ops = {
2177 static int show_traces_open(struct inode *inode, struct file *file)
2181 if (tracing_disabled)
2184 ret = seq_open(file, &show_traces_seq_ops);
2186 struct seq_file *m = file->private_data;
2187 m->private = trace_types;
2193 static struct file_operations tracing_fops = {
2194 .open = tracing_open,
2196 .llseek = seq_lseek,
2197 .release = tracing_release,
2200 static struct file_operations tracing_lt_fops = {
2201 .open = tracing_lt_open,
2203 .llseek = seq_lseek,
2204 .release = tracing_release,
2207 static struct file_operations show_traces_fops = {
2208 .open = show_traces_open,
2210 .release = seq_release,
2214 * Only trace on a CPU if the bitmask is set:
2216 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2219 * When tracing/tracing_cpu_mask is modified then this holds
2220 * the new bitmask we are about to install:
2222 static cpumask_t tracing_cpumask_new;
2225 * The tracer itself will not take this lock, but still we want
2226 * to provide a consistent cpumask to user-space:
2228 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2231 * Temporary storage for the character representation of the
2232 * CPU bitmask (and one more byte for the newline):
2234 static char mask_str[NR_CPUS + 1];
2237 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2238 size_t count, loff_t *ppos)
2242 mutex_lock(&tracing_cpumask_update_lock);
2244 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2245 if (count - len < 2) {
2249 len += sprintf(mask_str + len, "\n");
2250 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2253 mutex_unlock(&tracing_cpumask_update_lock);
2259 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2260 size_t count, loff_t *ppos)
2264 mutex_lock(&tracing_cpumask_update_lock);
2265 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2269 raw_local_irq_disable();
2270 __raw_spin_lock(&ftrace_max_lock);
2271 for_each_tracing_cpu(cpu) {
2273 * Increase/decrease the disabled counter if we are
2274 * about to flip a bit in the cpumask:
2276 if (cpu_isset(cpu, tracing_cpumask) &&
2277 !cpu_isset(cpu, tracing_cpumask_new)) {
2278 atomic_inc(&global_trace.data[cpu]->disabled);
2280 if (!cpu_isset(cpu, tracing_cpumask) &&
2281 cpu_isset(cpu, tracing_cpumask_new)) {
2282 atomic_dec(&global_trace.data[cpu]->disabled);
2285 __raw_spin_unlock(&ftrace_max_lock);
2286 raw_local_irq_enable();
2288 tracing_cpumask = tracing_cpumask_new;
2290 mutex_unlock(&tracing_cpumask_update_lock);
2295 mutex_unlock(&tracing_cpumask_update_lock);
2300 static struct file_operations tracing_cpumask_fops = {
2301 .open = tracing_open_generic,
2302 .read = tracing_cpumask_read,
2303 .write = tracing_cpumask_write,
2307 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2308 size_t cnt, loff_t *ppos)
2315 /* calulate max size */
2316 for (i = 0; trace_options[i]; i++) {
2317 len += strlen(trace_options[i]);
2318 len += 3; /* "no" and space */
2321 /* +2 for \n and \0 */
2322 buf = kmalloc(len + 2, GFP_KERNEL);
2326 for (i = 0; trace_options[i]; i++) {
2327 if (trace_flags & (1 << i))
2328 r += sprintf(buf + r, "%s ", trace_options[i]);
2330 r += sprintf(buf + r, "no%s ", trace_options[i]);
2333 r += sprintf(buf + r, "\n");
2334 WARN_ON(r >= len + 2);
2336 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2344 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2345 size_t cnt, loff_t *ppos)
2352 if (cnt >= sizeof(buf))
2355 if (copy_from_user(&buf, ubuf, cnt))
2360 if (strncmp(buf, "no", 2) == 0) {
2365 for (i = 0; trace_options[i]; i++) {
2366 int len = strlen(trace_options[i]);
2368 if (strncmp(cmp, trace_options[i], len) == 0) {
2370 trace_flags &= ~(1 << i);
2372 trace_flags |= (1 << i);
2377 * If no option could be set, return an error:
2379 if (!trace_options[i])
2387 static struct file_operations tracing_iter_fops = {
2388 .open = tracing_open_generic,
2389 .read = tracing_iter_ctrl_read,
2390 .write = tracing_iter_ctrl_write,
2393 static const char readme_msg[] =
2394 "tracing mini-HOWTO:\n\n"
2396 "# mount -t debugfs nodev /debug\n\n"
2397 "# cat /debug/tracing/available_tracers\n"
2398 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2399 "# cat /debug/tracing/current_tracer\n"
2401 "# echo sched_switch > /debug/tracing/current_tracer\n"
2402 "# cat /debug/tracing/current_tracer\n"
2404 "# cat /debug/tracing/iter_ctrl\n"
2405 "noprint-parent nosym-offset nosym-addr noverbose\n"
2406 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2407 "# echo 1 > /debug/tracing/tracing_enabled\n"
2408 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2409 "echo 0 > /debug/tracing/tracing_enabled\n"
2413 tracing_readme_read(struct file *filp, char __user *ubuf,
2414 size_t cnt, loff_t *ppos)
2416 return simple_read_from_buffer(ubuf, cnt, ppos,
2417 readme_msg, strlen(readme_msg));
2420 static struct file_operations tracing_readme_fops = {
2421 .open = tracing_open_generic,
2422 .read = tracing_readme_read,
2426 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2427 size_t cnt, loff_t *ppos)
2429 struct trace_array *tr = filp->private_data;
2433 r = sprintf(buf, "%ld\n", tr->ctrl);
2434 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2438 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2439 size_t cnt, loff_t *ppos)
2441 struct trace_array *tr = filp->private_data;
2446 if (cnt >= sizeof(buf))
2449 if (copy_from_user(&buf, ubuf, cnt))
2454 ret = strict_strtoul(buf, 10, &val);
2460 mutex_lock(&trace_types_lock);
2461 if (tr->ctrl ^ val) {
2469 if (current_trace && current_trace->ctrl_update)
2470 current_trace->ctrl_update(tr);
2472 mutex_unlock(&trace_types_lock);
2480 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2481 size_t cnt, loff_t *ppos)
2483 char buf[max_tracer_type_len+2];
2486 mutex_lock(&trace_types_lock);
2488 r = sprintf(buf, "%s\n", current_trace->name);
2490 r = sprintf(buf, "\n");
2491 mutex_unlock(&trace_types_lock);
2493 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2497 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2498 size_t cnt, loff_t *ppos)
2500 struct trace_array *tr = &global_trace;
2502 char buf[max_tracer_type_len+1];
2505 if (cnt > max_tracer_type_len)
2506 cnt = max_tracer_type_len;
2508 if (copy_from_user(&buf, ubuf, cnt))
2513 /* strip ending whitespace. */
2514 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2517 mutex_lock(&trace_types_lock);
2518 for (t = trace_types; t; t = t->next) {
2519 if (strcmp(t->name, buf) == 0)
2522 if (!t || t == current_trace)
2525 if (current_trace && current_trace->reset)
2526 current_trace->reset(tr);
2533 mutex_unlock(&trace_types_lock);
2541 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2542 size_t cnt, loff_t *ppos)
2544 unsigned long *ptr = filp->private_data;
2548 r = snprintf(buf, sizeof(buf), "%ld\n",
2549 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2550 if (r > sizeof(buf))
2552 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2556 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2557 size_t cnt, loff_t *ppos)
2559 long *ptr = filp->private_data;
2564 if (cnt >= sizeof(buf))
2567 if (copy_from_user(&buf, ubuf, cnt))
2572 ret = strict_strtoul(buf, 10, &val);
2581 static atomic_t tracing_reader;
2583 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2585 struct trace_iterator *iter;
2587 if (tracing_disabled)
2590 /* We only allow for reader of the pipe */
2591 if (atomic_inc_return(&tracing_reader) != 1) {
2592 atomic_dec(&tracing_reader);
2596 /* create a buffer to store the information to pass to userspace */
2597 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2601 mutex_lock(&trace_types_lock);
2602 iter->tr = &global_trace;
2603 iter->trace = current_trace;
2604 filp->private_data = iter;
2606 if (iter->trace->pipe_open)
2607 iter->trace->pipe_open(iter);
2608 mutex_unlock(&trace_types_lock);
2613 static int tracing_release_pipe(struct inode *inode, struct file *file)
2615 struct trace_iterator *iter = file->private_data;
2618 atomic_dec(&tracing_reader);
2624 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2626 struct trace_iterator *iter = filp->private_data;
2628 if (trace_flags & TRACE_ITER_BLOCK) {
2630 * Always select as readable when in blocking mode
2632 return POLLIN | POLLRDNORM;
2634 if (!trace_empty(iter))
2635 return POLLIN | POLLRDNORM;
2636 poll_wait(filp, &trace_wait, poll_table);
2637 if (!trace_empty(iter))
2638 return POLLIN | POLLRDNORM;
2648 tracing_read_pipe(struct file *filp, char __user *ubuf,
2649 size_t cnt, loff_t *ppos)
2651 struct trace_iterator *iter = filp->private_data;
2652 struct trace_array_cpu *data;
2653 static cpumask_t mask;
2654 unsigned long flags;
2655 #ifdef CONFIG_FTRACE
2661 /* return any leftover data */
2662 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2667 trace_seq_reset(&iter->seq);
2669 mutex_lock(&trace_types_lock);
2670 if (iter->trace->read) {
2671 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2676 while (trace_empty(iter)) {
2678 if ((filp->f_flags & O_NONBLOCK)) {
2684 * This is a make-shift waitqueue. The reason we don't use
2685 * an actual wait queue is because:
2686 * 1) we only ever have one waiter
2687 * 2) the tracing, traces all functions, we don't want
2688 * the overhead of calling wake_up and friends
2689 * (and tracing them too)
2690 * Anyway, this is really very primitive wakeup.
2692 set_current_state(TASK_INTERRUPTIBLE);
2693 iter->tr->waiter = current;
2695 mutex_unlock(&trace_types_lock);
2697 /* sleep for 100 msecs, and try again. */
2698 schedule_timeout(HZ/10);
2700 mutex_lock(&trace_types_lock);
2702 iter->tr->waiter = NULL;
2704 if (signal_pending(current)) {
2709 if (iter->trace != current_trace)
2713 * We block until we read something and tracing is disabled.
2714 * We still block if tracing is disabled, but we have never
2715 * read anything. This allows a user to cat this file, and
2716 * then enable tracing. But after we have read something,
2717 * we give an EOF when tracing is again disabled.
2719 * iter->pos will be 0 if we haven't read anything.
2721 if (!tracer_enabled && iter->pos)
2727 /* stop when tracing is finished */
2728 if (trace_empty(iter))
2731 if (cnt >= PAGE_SIZE)
2732 cnt = PAGE_SIZE - 1;
2734 /* reset all but tr, trace, and overruns */
2735 memset(&iter->seq, 0,
2736 sizeof(struct trace_iterator) -
2737 offsetof(struct trace_iterator, seq));
2741 * We need to stop all tracing on all CPUS to read the
2742 * the next buffer. This is a bit expensive, but is
2743 * not done often. We fill all what we can read,
2744 * and then release the locks again.
2748 local_irq_save(flags);
2749 #ifdef CONFIG_FTRACE
2750 ftrace_save = ftrace_enabled;
2754 for_each_tracing_cpu(cpu) {
2755 data = iter->tr->data[cpu];
2757 if (!head_page(data) || !data->trace_idx)
2760 atomic_inc(&data->disabled);
2764 for_each_cpu_mask(cpu, mask) {
2765 data = iter->tr->data[cpu];
2766 __raw_spin_lock(&data->lock);
2768 if (data->overrun > iter->last_overrun[cpu])
2769 iter->overrun[cpu] +=
2770 data->overrun - iter->last_overrun[cpu];
2771 iter->last_overrun[cpu] = data->overrun;
2774 while (find_next_entry_inc(iter) != NULL) {
2776 int len = iter->seq.len;
2778 ret = print_trace_line(iter);
2780 /* don't print partial lines */
2781 iter->seq.len = len;
2785 trace_consume(iter);
2787 if (iter->seq.len >= cnt)
2791 for_each_cpu_mask(cpu, mask) {
2792 data = iter->tr->data[cpu];
2793 __raw_spin_unlock(&data->lock);
2796 for_each_cpu_mask(cpu, mask) {
2797 data = iter->tr->data[cpu];
2798 atomic_dec(&data->disabled);
2800 #ifdef CONFIG_FTRACE
2801 ftrace_enabled = ftrace_save;
2803 local_irq_restore(flags);
2805 /* Now copy what we have to the user */
2806 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2807 if (iter->seq.readpos >= iter->seq.len)
2808 trace_seq_reset(&iter->seq);
2813 mutex_unlock(&trace_types_lock);
2819 tracing_entries_read(struct file *filp, char __user *ubuf,
2820 size_t cnt, loff_t *ppos)
2822 struct trace_array *tr = filp->private_data;
2826 r = sprintf(buf, "%lu\n", tr->entries);
2827 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2831 tracing_entries_write(struct file *filp, const char __user *ubuf,
2832 size_t cnt, loff_t *ppos)
2838 if (cnt >= sizeof(buf))
2841 if (copy_from_user(&buf, ubuf, cnt))
2846 ret = strict_strtoul(buf, 10, &val);
2850 /* must have at least 1 entry */
2854 mutex_lock(&trace_types_lock);
2856 if (current_trace != &no_tracer) {
2858 pr_info("ftrace: set current_tracer to none"
2859 " before modifying buffer size\n");
2863 if (val > global_trace.entries) {
2864 long pages_requested;
2865 unsigned long freeable_pages;
2867 /* make sure we have enough memory before mapping */
2869 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2871 /* account for each buffer (and max_tr) */
2872 pages_requested *= tracing_nr_buffers * 2;
2874 /* Check for overflow */
2875 if (pages_requested < 0) {
2880 freeable_pages = determine_dirtyable_memory();
2882 /* we only allow to request 1/4 of useable memory */
2883 if (pages_requested >
2884 ((freeable_pages + tracing_pages_allocated) / 4)) {
2889 while (global_trace.entries < val) {
2890 if (trace_alloc_page()) {
2894 /* double check that we don't go over the known pages */
2895 if (tracing_pages_allocated > pages_requested)
2900 /* include the number of entries in val (inc of page entries) */
2901 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2905 /* check integrity */
2906 for_each_tracing_cpu(i)
2907 check_pages(global_trace.data[i]);
2911 /* If check pages failed, return ENOMEM */
2912 if (tracing_disabled)
2915 max_tr.entries = global_trace.entries;
2916 mutex_unlock(&trace_types_lock);
2921 static struct file_operations tracing_max_lat_fops = {
2922 .open = tracing_open_generic,
2923 .read = tracing_max_lat_read,
2924 .write = tracing_max_lat_write,
2927 static struct file_operations tracing_ctrl_fops = {
2928 .open = tracing_open_generic,
2929 .read = tracing_ctrl_read,
2930 .write = tracing_ctrl_write,
2933 static struct file_operations set_tracer_fops = {
2934 .open = tracing_open_generic,
2935 .read = tracing_set_trace_read,
2936 .write = tracing_set_trace_write,
2939 static struct file_operations tracing_pipe_fops = {
2940 .open = tracing_open_pipe,
2941 .poll = tracing_poll_pipe,
2942 .read = tracing_read_pipe,
2943 .release = tracing_release_pipe,
2946 static struct file_operations tracing_entries_fops = {
2947 .open = tracing_open_generic,
2948 .read = tracing_entries_read,
2949 .write = tracing_entries_write,
2952 #ifdef CONFIG_DYNAMIC_FTRACE
2955 tracing_read_long(struct file *filp, char __user *ubuf,
2956 size_t cnt, loff_t *ppos)
2958 unsigned long *p = filp->private_data;
2962 r = sprintf(buf, "%ld\n", *p);
2964 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2967 static struct file_operations tracing_read_long_fops = {
2968 .open = tracing_open_generic,
2969 .read = tracing_read_long,
2973 static struct dentry *d_tracer;
2975 struct dentry *tracing_init_dentry(void)
2982 d_tracer = debugfs_create_dir("tracing", NULL);
2984 if (!d_tracer && !once) {
2986 pr_warning("Could not create debugfs directory 'tracing'\n");
2993 #ifdef CONFIG_FTRACE_SELFTEST
2994 /* Let selftest have access to static functions in this file */
2995 #include "trace_selftest.c"
2998 static __init void tracer_init_debugfs(void)
3000 struct dentry *d_tracer;
3001 struct dentry *entry;
3003 d_tracer = tracing_init_dentry();
3005 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3006 &global_trace, &tracing_ctrl_fops);
3008 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3010 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
3011 NULL, &tracing_iter_fops);
3013 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
3015 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3016 NULL, &tracing_cpumask_fops);
3018 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3020 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3021 &global_trace, &tracing_lt_fops);
3023 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3025 entry = debugfs_create_file("trace", 0444, d_tracer,
3026 &global_trace, &tracing_fops);
3028 pr_warning("Could not create debugfs 'trace' entry\n");
3030 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3031 &global_trace, &show_traces_fops);
3033 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3035 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3036 &global_trace, &set_tracer_fops);
3038 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3040 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3041 &tracing_max_latency,
3042 &tracing_max_lat_fops);
3044 pr_warning("Could not create debugfs "
3045 "'tracing_max_latency' entry\n");
3047 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3048 &tracing_thresh, &tracing_max_lat_fops);
3050 pr_warning("Could not create debugfs "
3051 "'tracing_thresh' entry\n");
3052 entry = debugfs_create_file("README", 0644, d_tracer,
3053 NULL, &tracing_readme_fops);
3055 pr_warning("Could not create debugfs 'README' entry\n");
3057 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3058 NULL, &tracing_pipe_fops);
3060 pr_warning("Could not create debugfs "
3061 "'trace_pipe' entry\n");
3063 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
3064 &global_trace, &tracing_entries_fops);
3066 pr_warning("Could not create debugfs "
3067 "'trace_entries' entry\n");
3069 #ifdef CONFIG_DYNAMIC_FTRACE
3070 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3071 &ftrace_update_tot_cnt,
3072 &tracing_read_long_fops);
3074 pr_warning("Could not create debugfs "
3075 "'dyn_ftrace_total_info' entry\n");
3077 #ifdef CONFIG_SYSPROF_TRACER
3078 init_tracer_sysprof_debugfs(d_tracer);
3082 #define TRACE_BUF_SIZE 1024
3083 #define TRACE_PRINT_BUF_SIZE \
3084 (sizeof(struct trace_field) - offsetof(struct trace_field, print.buf))
3085 #define TRACE_CONT_BUF_SIZE sizeof(struct trace_field)
3087 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3089 struct trace_array *tr = &global_trace;
3090 static DEFINE_SPINLOCK(trace_buf_lock);
3091 static char trace_buf[TRACE_BUF_SIZE];
3092 struct trace_array_cpu *data;
3093 struct trace_entry *entry;
3094 unsigned long flags;
3097 int cpu, len = 0, write, written = 0;
3099 if (likely(!ftrace_function_enabled))
3102 local_irq_save(flags);
3103 cpu = raw_smp_processor_id();
3104 data = tr->data[cpu];
3105 disabled = atomic_inc_return(&data->disabled);
3107 if (unlikely(disabled != 1 || !ftrace_function_enabled))
3110 spin_lock(&trace_buf_lock);
3112 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, ap);
3115 len = min(len, TRACE_BUF_SIZE-1);
3118 __raw_spin_lock(&data->lock);
3119 entry = tracing_get_trace_entry(tr, data);
3120 tracing_generic_entry_update(entry, flags);
3121 entry->type = TRACE_PRINT;
3122 entry->field.print.ip = ip;
3124 write = min(len, (int)(TRACE_PRINT_BUF_SIZE-1));
3126 memcpy(&entry->field.print.buf, trace_buf, write);
3127 entry->field.print.buf[write] = 0;
3131 entry->field.flags |= TRACE_FLAG_CONT;
3133 while (written != len) {
3134 entry = tracing_get_trace_entry(tr, data);
3136 entry->type = TRACE_CONT;
3137 write = min(len - written, (int)(TRACE_CONT_BUF_SIZE-1));
3138 memcpy(&entry->cont.buf, trace_buf+written, write);
3139 entry->cont.buf[write] = 0;
3142 __raw_spin_unlock(&data->lock);
3144 spin_unlock(&trace_buf_lock);
3147 atomic_dec(&data->disabled);
3148 local_irq_restore(flags);
3152 EXPORT_SYMBOL_GPL(__ftrace_printk);
3154 static int trace_panic_handler(struct notifier_block *this,
3155 unsigned long event, void *unused)
3161 static struct notifier_block trace_panic_notifier = {
3162 .notifier_call = trace_panic_handler,
3164 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3167 static int trace_die_handler(struct notifier_block *self,
3181 static struct notifier_block trace_die_notifier = {
3182 .notifier_call = trace_die_handler,
3187 * printk is set to max of 1024, we really don't need it that big.
3188 * Nothing should be printing 1000 characters anyway.
3190 #define TRACE_MAX_PRINT 1000
3193 * Define here KERN_TRACE so that we have one place to modify
3194 * it if we decide to change what log level the ftrace dump
3197 #define KERN_TRACE KERN_INFO
3200 trace_printk_seq(struct trace_seq *s)
3202 /* Probably should print a warning here. */
3206 /* should be zero ended, but we are paranoid. */
3207 s->buffer[s->len] = 0;
3209 printk(KERN_TRACE "%s", s->buffer);
3215 void ftrace_dump(void)
3217 static DEFINE_SPINLOCK(ftrace_dump_lock);
3218 /* use static because iter can be a bit big for the stack */
3219 static struct trace_iterator iter;
3220 struct trace_array_cpu *data;
3221 static cpumask_t mask;
3222 static int dump_ran;
3223 unsigned long flags;
3228 spin_lock_irqsave(&ftrace_dump_lock, flags);
3234 /* No turning back! */
3235 ftrace_kill_atomic();
3237 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3239 iter.tr = &global_trace;
3240 iter.trace = current_trace;
3243 * We need to stop all tracing on all CPUS to read the
3244 * the next buffer. This is a bit expensive, but is
3245 * not done often. We fill all what we can read,
3246 * and then release the locks again.
3251 for_each_tracing_cpu(cpu) {
3252 data = iter.tr->data[cpu];
3254 if (!head_page(data) || !data->trace_idx)
3257 atomic_inc(&data->disabled);
3261 for_each_cpu_mask(cpu, mask) {
3262 data = iter.tr->data[cpu];
3263 __raw_spin_lock(&data->lock);
3265 if (data->overrun > iter.last_overrun[cpu])
3266 iter.overrun[cpu] +=
3267 data->overrun - iter.last_overrun[cpu];
3268 iter.last_overrun[cpu] = data->overrun;
3271 while (!trace_empty(&iter)) {
3274 printk(KERN_TRACE "---------------------------------\n");
3278 /* reset all but tr, trace, and overruns */
3279 memset(&iter.seq, 0,
3280 sizeof(struct trace_iterator) -
3281 offsetof(struct trace_iterator, seq));
3282 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3285 if (find_next_entry_inc(&iter) != NULL) {
3286 print_trace_line(&iter);
3287 trace_consume(&iter);
3290 trace_printk_seq(&iter.seq);
3294 printk(KERN_TRACE " (ftrace buffer empty)\n");
3296 printk(KERN_TRACE "---------------------------------\n");
3298 for_each_cpu_mask(cpu, mask) {
3299 data = iter.tr->data[cpu];
3300 __raw_spin_unlock(&data->lock);
3303 for_each_cpu_mask(cpu, mask) {
3304 data = iter.tr->data[cpu];
3305 atomic_dec(&data->disabled);
3310 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3313 static int trace_alloc_page(void)
3315 struct trace_array_cpu *data;
3316 struct page *page, *tmp;
3319 unsigned pages_allocated = 0;
3322 /* first allocate a page for each CPU */
3323 for_each_tracing_cpu(i) {
3324 array = (void *)__get_free_page(GFP_KERNEL);
3325 if (array == NULL) {
3326 printk(KERN_ERR "tracer: failed to allocate page"
3327 "for trace buffer!\n");
3332 page = virt_to_page(array);
3333 list_add(&page->lru, &pages);
3335 /* Only allocate if we are actually using the max trace */
3336 #ifdef CONFIG_TRACER_MAX_TRACE
3337 array = (void *)__get_free_page(GFP_KERNEL);
3338 if (array == NULL) {
3339 printk(KERN_ERR "tracer: failed to allocate page"
3340 "for trace buffer!\n");
3344 page = virt_to_page(array);
3345 list_add(&page->lru, &pages);
3349 /* Now that we successfully allocate a page per CPU, add them */
3350 for_each_tracing_cpu(i) {
3351 data = global_trace.data[i];
3352 page = list_entry(pages.next, struct page, lru);
3353 list_del_init(&page->lru);
3354 list_add_tail(&page->lru, &data->trace_pages);
3357 #ifdef CONFIG_TRACER_MAX_TRACE
3358 data = max_tr.data[i];
3359 page = list_entry(pages.next, struct page, lru);
3360 list_del_init(&page->lru);
3361 list_add_tail(&page->lru, &data->trace_pages);
3365 tracing_pages_allocated += pages_allocated;
3366 global_trace.entries += ENTRIES_PER_PAGE;
3371 list_for_each_entry_safe(page, tmp, &pages, lru) {
3372 list_del_init(&page->lru);
3378 static int trace_free_page(void)
3380 struct trace_array_cpu *data;
3382 struct list_head *p;
3386 /* free one page from each buffer */
3387 for_each_tracing_cpu(i) {
3388 data = global_trace.data[i];
3389 p = data->trace_pages.next;
3390 if (p == &data->trace_pages) {
3391 /* should never happen */
3393 tracing_disabled = 1;
3397 page = list_entry(p, struct page, lru);
3399 list_del(&page->lru);
3400 tracing_pages_allocated--;
3401 tracing_pages_allocated--;
3404 tracing_reset(data);
3406 #ifdef CONFIG_TRACER_MAX_TRACE
3407 data = max_tr.data[i];
3408 p = data->trace_pages.next;
3409 if (p == &data->trace_pages) {
3410 /* should never happen */
3412 tracing_disabled = 1;
3416 page = list_entry(p, struct page, lru);
3418 list_del(&page->lru);
3421 tracing_reset(data);
3424 global_trace.entries -= ENTRIES_PER_PAGE;
3429 __init static int tracer_alloc_buffers(void)
3431 struct trace_array_cpu *data;
3438 /* TODO: make the number of buffers hot pluggable with CPUS */
3439 tracing_nr_buffers = num_possible_cpus();
3440 tracing_buffer_mask = cpu_possible_map;
3442 /* Allocate the first page for all buffers */
3443 for_each_tracing_cpu(i) {
3444 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3445 max_tr.data[i] = &per_cpu(max_data, i);
3447 array = (void *)__get_free_page(GFP_KERNEL);
3448 if (array == NULL) {
3449 printk(KERN_ERR "tracer: failed to allocate page"
3450 "for trace buffer!\n");
3454 /* set the array to the list */
3455 INIT_LIST_HEAD(&data->trace_pages);
3456 page = virt_to_page(array);
3457 list_add(&page->lru, &data->trace_pages);
3458 /* use the LRU flag to differentiate the two buffers */
3461 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3462 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3464 /* Only allocate if we are actually using the max trace */
3465 #ifdef CONFIG_TRACER_MAX_TRACE
3466 array = (void *)__get_free_page(GFP_KERNEL);
3467 if (array == NULL) {
3468 printk(KERN_ERR "tracer: failed to allocate page"
3469 "for trace buffer!\n");
3473 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3474 page = virt_to_page(array);
3475 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3481 * Since we allocate by orders of pages, we may be able to
3484 global_trace.entries = ENTRIES_PER_PAGE;
3487 while (global_trace.entries < trace_nr_entries) {
3488 if (trace_alloc_page())
3492 max_tr.entries = global_trace.entries;
3494 pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
3495 pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
3496 pr_info(" actual entries %ld\n", global_trace.entries);
3498 tracer_init_debugfs();
3500 trace_init_cmdlines();
3502 register_tracer(&no_tracer);
3503 current_trace = &no_tracer;
3505 /* All seems OK, enable tracing */
3506 global_trace.ctrl = tracer_enabled;
3507 tracing_disabled = 0;
3509 atomic_notifier_chain_register(&panic_notifier_list,
3510 &trace_panic_notifier);
3512 register_die_notifier(&trace_die_notifier);
3517 for (i-- ; i >= 0; i--) {
3518 struct page *page, *tmp;
3519 struct trace_array_cpu *data = global_trace.data[i];
3522 list_for_each_entry_safe(page, tmp,
3523 &data->trace_pages, lru) {
3524 list_del_init(&page->lru);
3529 #ifdef CONFIG_TRACER_MAX_TRACE
3530 data = max_tr.data[i];
3532 list_for_each_entry_safe(page, tmp,
3533 &data->trace_pages, lru) {
3534 list_del_init(&page->lru);
3542 fs_initcall(tracer_alloc_buffers);