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/debugfs.h>
18 #include <linux/pagemap.h>
19 #include <linux/hardirq.h>
20 #include <linux/linkage.h>
21 #include <linux/uaccess.h>
22 #include <linux/ftrace.h>
23 #include <linux/module.h>
24 #include <linux/percpu.h>
25 #include <linux/ctype.h>
26 #include <linux/init.h>
27 #include <linux/poll.h>
28 #include <linux/gfp.h>
31 #include <linux/stacktrace.h>
35 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
36 unsigned long __read_mostly tracing_thresh;
38 /* dummy trace to disable tracing */
39 static struct tracer no_tracer __read_mostly =
44 static int trace_alloc_page(void);
45 static int trace_free_page(void);
47 static int tracing_disabled = 1;
50 ns2usecs(cycle_t nsec)
57 cycle_t ftrace_now(int cpu)
59 return cpu_clock(cpu);
62 static struct trace_array global_trace;
64 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
66 static struct trace_array max_tr;
68 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
70 static int tracer_enabled = 1;
71 static unsigned long trace_nr_entries = 65536UL;
73 static struct tracer *trace_types __read_mostly;
74 static struct tracer *current_trace __read_mostly;
75 static int max_tracer_type_len;
77 static DEFINE_MUTEX(trace_types_lock);
78 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
80 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
82 void trace_wake_up(void)
85 * The runqueue_is_locked() can fail, but this is the best we
88 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
92 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
94 static int __init set_nr_entries(char *str)
98 trace_nr_entries = simple_strtoul(str, &str, 0);
101 __setup("trace_entries=", set_nr_entries);
103 unsigned long nsecs_to_usecs(unsigned long nsecs)
108 enum trace_flag_type {
109 TRACE_FLAG_IRQS_OFF = 0x01,
110 TRACE_FLAG_NEED_RESCHED = 0x02,
111 TRACE_FLAG_HARDIRQ = 0x04,
112 TRACE_FLAG_SOFTIRQ = 0x08,
115 #define TRACE_ITER_SYM_MASK \
116 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
118 /* These must match the bit postions above */
119 static const char *trace_options[] = {
133 static raw_spinlock_t ftrace_max_lock =
134 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
137 * Copy the new maximum trace into the separate maximum-trace
138 * structure. (this way the maximum trace is permanently saved,
139 * for later retrieval via /debugfs/tracing/latency_trace)
142 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
144 struct trace_array_cpu *data = tr->data[cpu];
147 max_tr.time_start = data->preempt_timestamp;
149 data = max_tr.data[cpu];
150 data->saved_latency = tracing_max_latency;
152 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
153 data->pid = tsk->pid;
154 data->uid = tsk->uid;
155 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
156 data->policy = tsk->policy;
157 data->rt_priority = tsk->rt_priority;
159 /* record this tasks comm */
160 tracing_record_cmdline(current);
163 void check_pages(struct trace_array_cpu *data)
165 struct page *page, *tmp;
167 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
168 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
170 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
171 BUG_ON(page->lru.next->prev != &page->lru);
172 BUG_ON(page->lru.prev->next != &page->lru);
176 void *head_page(struct trace_array_cpu *data)
181 if (list_empty(&data->trace_pages))
184 page = list_entry(data->trace_pages.next, struct page, lru);
185 BUG_ON(&page->lru == &data->trace_pages);
187 return page_address(page);
191 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
193 int len = (PAGE_SIZE - 1) - s->len;
201 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
204 /* If we can't write it all, don't bother writing anything */
214 trace_seq_puts(struct trace_seq *s, const char *str)
216 int len = strlen(str);
218 if (len > ((PAGE_SIZE - 1) - s->len))
221 memcpy(s->buffer + s->len, str, len);
228 trace_seq_putc(struct trace_seq *s, unsigned char c)
230 if (s->len >= (PAGE_SIZE - 1))
233 s->buffer[s->len++] = c;
239 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
241 if (len > ((PAGE_SIZE - 1) - s->len))
244 memcpy(s->buffer + s->len, mem, len);
253 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
255 unsigned char hex[HEX_CHARS];
260 BUG_ON(len >= HEX_CHARS);
265 for (i = 0, j = 0; i < len; i++) {
267 for (i = len-1, j = 0; i >= 0; i--) {
271 hex[j] = byte & 0x0f;
288 return trace_seq_putmem(s, hex, j);
292 trace_seq_reset(struct trace_seq *s)
298 trace_print_seq(struct seq_file *m, struct trace_seq *s)
300 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
303 seq_puts(m, s->buffer);
309 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
311 struct list_head flip_pages;
313 INIT_LIST_HEAD(&flip_pages);
315 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
316 sizeof(struct trace_array_cpu) -
317 offsetof(struct trace_array_cpu, trace_head_idx));
321 list_splice_init(&tr1->trace_pages, &flip_pages);
322 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
323 list_splice_init(&flip_pages, &tr2->trace_pages);
324 BUG_ON(!list_empty(&flip_pages));
330 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
332 struct trace_array_cpu *data;
335 WARN_ON_ONCE(!irqs_disabled());
336 __raw_spin_lock(&ftrace_max_lock);
337 /* clear out all the previous traces */
338 for_each_possible_cpu(i) {
340 flip_trace(max_tr.data[i], data);
344 __update_max_tr(tr, tsk, cpu);
345 __raw_spin_unlock(&ftrace_max_lock);
349 * update_max_tr_single - only copy one trace over, and reset the rest
351 * @tsk - task with the latency
352 * @cpu - the cpu of the buffer to copy.
355 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
357 struct trace_array_cpu *data = tr->data[cpu];
360 WARN_ON_ONCE(!irqs_disabled());
361 __raw_spin_lock(&ftrace_max_lock);
362 for_each_possible_cpu(i)
363 tracing_reset(max_tr.data[i]);
365 flip_trace(max_tr.data[cpu], data);
368 __update_max_tr(tr, tsk, cpu);
369 __raw_spin_unlock(&ftrace_max_lock);
372 int register_tracer(struct tracer *type)
379 pr_info("Tracer must have a name\n");
383 mutex_lock(&trace_types_lock);
384 for (t = trace_types; t; t = t->next) {
385 if (strcmp(type->name, t->name) == 0) {
387 pr_info("Trace %s already registered\n",
394 #ifdef CONFIG_FTRACE_STARTUP_TEST
395 if (type->selftest) {
396 struct tracer *saved_tracer = current_trace;
397 struct trace_array_cpu *data;
398 struct trace_array *tr = &global_trace;
399 int saved_ctrl = tr->ctrl;
402 * Run a selftest on this tracer.
403 * Here we reset the trace buffer, and set the current
404 * tracer to be this tracer. The tracer can then run some
405 * internal tracing to verify that everything is in order.
406 * If we fail, we do not register this tracer.
408 for_each_possible_cpu(i) {
410 if (!head_page(data))
414 current_trace = type;
416 /* the test is responsible for initializing and enabling */
417 pr_info("Testing tracer %s: ", type->name);
418 ret = type->selftest(type, tr);
419 /* the test is responsible for resetting too */
420 current_trace = saved_tracer;
421 tr->ctrl = saved_ctrl;
423 printk(KERN_CONT "FAILED!\n");
426 /* Only reset on passing, to avoid touching corrupted buffers */
427 for_each_possible_cpu(i) {
429 if (!head_page(data))
433 printk(KERN_CONT "PASSED\n");
437 type->next = trace_types;
439 len = strlen(type->name);
440 if (len > max_tracer_type_len)
441 max_tracer_type_len = len;
444 mutex_unlock(&trace_types_lock);
449 void unregister_tracer(struct tracer *type)
454 mutex_lock(&trace_types_lock);
455 for (t = &trace_types; *t; t = &(*t)->next) {
459 pr_info("Trace %s not registered\n", type->name);
464 if (strlen(type->name) != max_tracer_type_len)
467 max_tracer_type_len = 0;
468 for (t = &trace_types; *t; t = &(*t)->next) {
469 len = strlen((*t)->name);
470 if (len > max_tracer_type_len)
471 max_tracer_type_len = len;
474 mutex_unlock(&trace_types_lock);
477 void tracing_reset(struct trace_array_cpu *data)
480 data->trace_head = data->trace_tail = head_page(data);
481 data->trace_head_idx = 0;
482 data->trace_tail_idx = 0;
485 #define SAVED_CMDLINES 128
486 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
487 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
488 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
489 static int cmdline_idx;
490 static DEFINE_SPINLOCK(trace_cmdline_lock);
491 atomic_t trace_record_cmdline_disabled;
493 static void trace_init_cmdlines(void)
495 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
496 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
500 void trace_stop_cmdline_recording(void);
502 static void trace_save_cmdline(struct task_struct *tsk)
507 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
511 * It's not the end of the world if we don't get
512 * the lock, but we also don't want to spin
513 * nor do we want to disable interrupts,
514 * so if we miss here, then better luck next time.
516 if (!spin_trylock(&trace_cmdline_lock))
519 idx = map_pid_to_cmdline[tsk->pid];
520 if (idx >= SAVED_CMDLINES) {
521 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
523 map = map_cmdline_to_pid[idx];
524 if (map <= PID_MAX_DEFAULT)
525 map_pid_to_cmdline[map] = (unsigned)-1;
527 map_pid_to_cmdline[tsk->pid] = idx;
532 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
534 spin_unlock(&trace_cmdline_lock);
537 static char *trace_find_cmdline(int pid)
539 char *cmdline = "<...>";
545 if (pid > PID_MAX_DEFAULT)
548 map = map_pid_to_cmdline[pid];
549 if (map >= SAVED_CMDLINES)
552 cmdline = saved_cmdlines[map];
558 void tracing_record_cmdline(struct task_struct *tsk)
560 if (atomic_read(&trace_record_cmdline_disabled))
563 trace_save_cmdline(tsk);
566 static inline struct list_head *
567 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
570 * Roundrobin - but skip the head (which is not a real page):
573 if (unlikely(next == &data->trace_pages))
575 BUG_ON(next == &data->trace_pages);
581 trace_next_page(struct trace_array_cpu *data, void *addr)
583 struct list_head *next;
586 page = virt_to_page(addr);
588 next = trace_next_list(data, &page->lru);
589 page = list_entry(next, struct page, lru);
591 return page_address(page);
594 static inline struct trace_entry *
595 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
597 unsigned long idx, idx_next;
598 struct trace_entry *entry;
601 idx = data->trace_head_idx;
604 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
606 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
608 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
609 data->trace_head = trace_next_page(data, data->trace_head);
613 if (data->trace_head == data->trace_tail &&
614 idx_next == data->trace_tail_idx) {
616 data->trace_tail_idx++;
617 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
619 trace_next_page(data, data->trace_tail);
620 data->trace_tail_idx = 0;
624 data->trace_head_idx = idx_next;
630 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
632 struct task_struct *tsk = current;
635 pc = preempt_count();
637 entry->preempt_count = pc & 0xff;
638 entry->pid = (tsk) ? tsk->pid : 0;
639 entry->t = ftrace_now(raw_smp_processor_id());
640 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
641 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
642 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
643 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
647 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
648 unsigned long ip, unsigned long parent_ip, unsigned long flags)
650 struct trace_entry *entry;
651 unsigned long irq_flags;
653 raw_local_irq_save(irq_flags);
654 __raw_spin_lock(&data->lock);
655 entry = tracing_get_trace_entry(tr, data);
656 tracing_generic_entry_update(entry, flags);
657 entry->type = TRACE_FN;
659 entry->fn.parent_ip = parent_ip;
660 __raw_spin_unlock(&data->lock);
661 raw_local_irq_restore(irq_flags);
665 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
666 unsigned long ip, unsigned long parent_ip, unsigned long flags)
668 if (likely(!atomic_read(&data->disabled)))
669 trace_function(tr, data, ip, parent_ip, flags);
673 __trace_special(void *__tr, void *__data,
674 unsigned long arg1, unsigned long arg2, unsigned long arg3)
676 struct trace_array_cpu *data = __data;
677 struct trace_array *tr = __tr;
678 struct trace_entry *entry;
679 unsigned long irq_flags;
681 raw_local_irq_save(irq_flags);
682 __raw_spin_lock(&data->lock);
683 entry = tracing_get_trace_entry(tr, data);
684 tracing_generic_entry_update(entry, 0);
685 entry->type = TRACE_SPECIAL;
686 entry->special.arg1 = arg1;
687 entry->special.arg2 = arg2;
688 entry->special.arg3 = arg3;
689 __raw_spin_unlock(&data->lock);
690 raw_local_irq_restore(irq_flags);
695 void __trace_stack(struct trace_array *tr,
696 struct trace_array_cpu *data,
700 struct trace_entry *entry;
701 struct stack_trace trace;
703 if (!(trace_flags & TRACE_ITER_STACKTRACE))
706 entry = tracing_get_trace_entry(tr, data);
707 tracing_generic_entry_update(entry, flags);
708 entry->type = TRACE_STACK;
710 memset(&entry->stack, 0, sizeof(entry->stack));
712 trace.nr_entries = 0;
713 trace.max_entries = FTRACE_STACK_ENTRIES;
715 trace.entries = entry->stack.caller;
717 save_stack_trace(&trace);
721 tracing_sched_switch_trace(struct trace_array *tr,
722 struct trace_array_cpu *data,
723 struct task_struct *prev,
724 struct task_struct *next,
727 struct trace_entry *entry;
728 unsigned long irq_flags;
730 raw_local_irq_save(irq_flags);
731 __raw_spin_lock(&data->lock);
732 entry = tracing_get_trace_entry(tr, data);
733 tracing_generic_entry_update(entry, flags);
734 entry->type = TRACE_CTX;
735 entry->ctx.prev_pid = prev->pid;
736 entry->ctx.prev_prio = prev->prio;
737 entry->ctx.prev_state = prev->state;
738 entry->ctx.next_pid = next->pid;
739 entry->ctx.next_prio = next->prio;
740 entry->ctx.next_state = next->state;
741 __trace_stack(tr, data, flags, 4);
742 __raw_spin_unlock(&data->lock);
743 raw_local_irq_restore(irq_flags);
747 tracing_sched_wakeup_trace(struct trace_array *tr,
748 struct trace_array_cpu *data,
749 struct task_struct *wakee,
750 struct task_struct *curr,
753 struct trace_entry *entry;
754 unsigned long irq_flags;
756 raw_local_irq_save(irq_flags);
757 __raw_spin_lock(&data->lock);
758 entry = tracing_get_trace_entry(tr, data);
759 tracing_generic_entry_update(entry, flags);
760 entry->type = TRACE_WAKE;
761 entry->ctx.prev_pid = curr->pid;
762 entry->ctx.prev_prio = curr->prio;
763 entry->ctx.prev_state = curr->state;
764 entry->ctx.next_pid = wakee->pid;
765 entry->ctx.next_prio = wakee->prio;
766 entry->ctx.next_state = wakee->state;
767 __trace_stack(tr, data, flags, 5);
768 __raw_spin_unlock(&data->lock);
769 raw_local_irq_restore(irq_flags);
776 function_trace_call(unsigned long ip, unsigned long parent_ip)
778 struct trace_array *tr = &global_trace;
779 struct trace_array_cpu *data;
784 if (unlikely(!tracer_enabled))
787 local_irq_save(flags);
788 cpu = raw_smp_processor_id();
789 data = tr->data[cpu];
790 disabled = atomic_inc_return(&data->disabled);
792 if (likely(disabled == 1))
793 trace_function(tr, data, ip, parent_ip, flags);
795 atomic_dec(&data->disabled);
796 local_irq_restore(flags);
799 static struct ftrace_ops trace_ops __read_mostly =
801 .func = function_trace_call,
804 void tracing_start_function_trace(void)
806 register_ftrace_function(&trace_ops);
809 void tracing_stop_function_trace(void)
811 unregister_ftrace_function(&trace_ops);
815 enum trace_file_type {
816 TRACE_FILE_LAT_FMT = 1,
819 static struct trace_entry *
820 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
821 struct trace_iterator *iter, int cpu)
824 struct trace_entry *array;
826 if (iter->next_idx[cpu] >= tr->entries ||
827 iter->next_idx[cpu] >= data->trace_idx ||
828 (data->trace_head == data->trace_tail &&
829 data->trace_head_idx == data->trace_tail_idx))
832 if (!iter->next_page[cpu]) {
833 /* Initialize the iterator for this cpu trace buffer */
834 WARN_ON(!data->trace_tail);
835 page = virt_to_page(data->trace_tail);
836 iter->next_page[cpu] = &page->lru;
837 iter->next_page_idx[cpu] = data->trace_tail_idx;
840 page = list_entry(iter->next_page[cpu], struct page, lru);
841 BUG_ON(&data->trace_pages == &page->lru);
843 array = page_address(page);
845 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
846 return &array[iter->next_page_idx[cpu]];
849 static struct trace_entry *
850 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
852 struct trace_array *tr = iter->tr;
853 struct trace_entry *ent, *next = NULL;
857 for_each_possible_cpu(cpu) {
858 if (!head_page(tr->data[cpu]))
860 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
862 * Pick the entry with the smallest timestamp:
864 if (ent && (!next || ent->t < next->t)) {
876 static void trace_iterator_increment(struct trace_iterator *iter)
879 iter->next_idx[iter->cpu]++;
880 iter->next_page_idx[iter->cpu]++;
882 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
883 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
885 iter->next_page_idx[iter->cpu] = 0;
886 iter->next_page[iter->cpu] =
887 trace_next_list(data, iter->next_page[iter->cpu]);
891 static void trace_consume(struct trace_iterator *iter)
893 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
895 data->trace_tail_idx++;
896 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
897 data->trace_tail = trace_next_page(data, data->trace_tail);
898 data->trace_tail_idx = 0;
901 /* Check if we empty it, then reset the index */
902 if (data->trace_head == data->trace_tail &&
903 data->trace_head_idx == data->trace_tail_idx)
907 static void *find_next_entry_inc(struct trace_iterator *iter)
909 struct trace_entry *next;
912 next = find_next_entry(iter, &next_cpu);
914 iter->prev_ent = iter->ent;
915 iter->prev_cpu = iter->cpu;
918 iter->cpu = next_cpu;
921 trace_iterator_increment(iter);
923 return next ? iter : NULL;
926 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
928 struct trace_iterator *iter = m->private;
929 void *last_ent = iter->ent;
935 /* can't go backwards */
940 ent = find_next_entry_inc(iter);
944 while (ent && iter->idx < i)
945 ent = find_next_entry_inc(iter);
949 if (last_ent && !ent)
950 seq_puts(m, "\n\nvim:ft=help\n");
955 static void *s_start(struct seq_file *m, loff_t *pos)
957 struct trace_iterator *iter = m->private;
962 mutex_lock(&trace_types_lock);
964 if (!current_trace || current_trace != iter->trace) {
965 mutex_unlock(&trace_types_lock);
969 atomic_inc(&trace_record_cmdline_disabled);
971 /* let the tracer grab locks here if needed */
972 if (current_trace->start)
973 current_trace->start(iter);
975 if (*pos != iter->pos) {
979 iter->prev_ent = NULL;
982 for_each_possible_cpu(i) {
983 iter->next_idx[i] = 0;
984 iter->next_page[i] = NULL;
987 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
992 p = s_next(m, p, &l);
998 static void s_stop(struct seq_file *m, void *p)
1000 struct trace_iterator *iter = m->private;
1002 atomic_dec(&trace_record_cmdline_disabled);
1004 /* let the tracer release locks here if needed */
1005 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1006 iter->trace->stop(iter);
1008 mutex_unlock(&trace_types_lock);
1012 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1014 #ifdef CONFIG_KALLSYMS
1015 char str[KSYM_SYMBOL_LEN];
1017 kallsyms_lookup(address, NULL, NULL, NULL, str);
1019 return trace_seq_printf(s, fmt, str);
1025 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1026 unsigned long address)
1028 #ifdef CONFIG_KALLSYMS
1029 char str[KSYM_SYMBOL_LEN];
1031 sprint_symbol(str, address);
1032 return trace_seq_printf(s, fmt, str);
1037 #ifndef CONFIG_64BIT
1038 # define IP_FMT "%08lx"
1040 # define IP_FMT "%016lx"
1044 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1049 return trace_seq_printf(s, "0");
1051 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1052 ret = seq_print_sym_offset(s, "%s", ip);
1054 ret = seq_print_sym_short(s, "%s", ip);
1059 if (sym_flags & TRACE_ITER_SYM_ADDR)
1060 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1064 static void print_lat_help_header(struct seq_file *m)
1066 seq_puts(m, "# _------=> CPU# \n");
1067 seq_puts(m, "# / _-----=> irqs-off \n");
1068 seq_puts(m, "# | / _----=> need-resched \n");
1069 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1070 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1071 seq_puts(m, "# |||| / \n");
1072 seq_puts(m, "# ||||| delay \n");
1073 seq_puts(m, "# cmd pid ||||| time | caller \n");
1074 seq_puts(m, "# \\ / ||||| \\ | / \n");
1077 static void print_func_help_header(struct seq_file *m)
1079 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1080 seq_puts(m, "# | | | | |\n");
1085 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1087 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1088 struct trace_array *tr = iter->tr;
1089 struct trace_array_cpu *data = tr->data[tr->cpu];
1090 struct tracer *type = current_trace;
1091 unsigned long total = 0;
1092 unsigned long entries = 0;
1094 const char *name = "preemption";
1099 for_each_possible_cpu(cpu) {
1100 if (head_page(tr->data[cpu])) {
1101 total += tr->data[cpu]->trace_idx;
1102 if (tr->data[cpu]->trace_idx > tr->entries)
1103 entries += tr->entries;
1105 entries += tr->data[cpu]->trace_idx;
1109 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1111 seq_puts(m, "-----------------------------------"
1112 "---------------------------------\n");
1113 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1114 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1115 nsecs_to_usecs(data->saved_latency),
1119 #if defined(CONFIG_PREEMPT_NONE)
1121 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1123 #elif defined(CONFIG_PREEMPT_DESKTOP)
1128 /* These are reserved for later use */
1131 seq_printf(m, " #P:%d)\n", num_online_cpus());
1135 seq_puts(m, " -----------------\n");
1136 seq_printf(m, " | task: %.16s-%d "
1137 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1138 data->comm, data->pid, data->uid, data->nice,
1139 data->policy, data->rt_priority);
1140 seq_puts(m, " -----------------\n");
1142 if (data->critical_start) {
1143 seq_puts(m, " => started at: ");
1144 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1145 trace_print_seq(m, &iter->seq);
1146 seq_puts(m, "\n => ended at: ");
1147 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1148 trace_print_seq(m, &iter->seq);
1156 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1158 int hardirq, softirq;
1161 comm = trace_find_cmdline(entry->pid);
1163 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1164 trace_seq_printf(s, "%d", cpu);
1165 trace_seq_printf(s, "%c%c",
1166 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1167 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1169 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1170 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1171 if (hardirq && softirq)
1172 trace_seq_putc(s, 'H');
1175 trace_seq_putc(s, 'h');
1178 trace_seq_putc(s, 's');
1180 trace_seq_putc(s, '.');
1184 if (entry->preempt_count)
1185 trace_seq_printf(s, "%x", entry->preempt_count);
1187 trace_seq_puts(s, ".");
1190 unsigned long preempt_mark_thresh = 100;
1193 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1194 unsigned long rel_usecs)
1196 trace_seq_printf(s, " %4lldus", abs_usecs);
1197 if (rel_usecs > preempt_mark_thresh)
1198 trace_seq_puts(s, "!: ");
1199 else if (rel_usecs > 1)
1200 trace_seq_puts(s, "+: ");
1202 trace_seq_puts(s, " : ");
1205 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1208 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1210 struct trace_seq *s = &iter->seq;
1211 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1212 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1213 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1214 struct trace_entry *entry = iter->ent;
1215 unsigned long abs_usecs;
1216 unsigned long rel_usecs;
1224 rel_usecs = ns2usecs(next_entry->t - entry->t);
1225 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1228 comm = trace_find_cmdline(entry->pid);
1229 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1230 " %ld.%03ldms (+%ld.%03ldms): ",
1232 entry->pid, cpu, entry->flags,
1233 entry->preempt_count, trace_idx,
1236 abs_usecs % 1000, rel_usecs/1000,
1239 lat_print_generic(s, entry, cpu);
1240 lat_print_timestamp(s, abs_usecs, rel_usecs);
1242 switch (entry->type) {
1244 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1245 trace_seq_puts(s, " (");
1246 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1247 trace_seq_puts(s, ")\n");
1251 T = entry->ctx.next_state < sizeof(state_to_char) ?
1252 state_to_char[entry->ctx.next_state] : 'X';
1254 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1255 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1256 comm = trace_find_cmdline(entry->ctx.next_pid);
1257 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1258 entry->ctx.prev_pid,
1259 entry->ctx.prev_prio,
1260 S, entry->type == TRACE_CTX ? "==>" : " +",
1261 entry->ctx.next_pid,
1262 entry->ctx.next_prio,
1266 trace_seq_printf(s, "# %ld %ld %ld\n",
1267 entry->special.arg1,
1268 entry->special.arg2,
1269 entry->special.arg3);
1272 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1274 trace_seq_puts(s, " <= ");
1275 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1277 trace_seq_puts(s, "\n");
1280 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1285 static int print_trace_fmt(struct trace_iterator *iter)
1287 struct trace_seq *s = &iter->seq;
1288 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1289 struct trace_entry *entry;
1290 unsigned long usec_rem;
1291 unsigned long long t;
1300 comm = trace_find_cmdline(iter->ent->pid);
1302 t = ns2usecs(entry->t);
1303 usec_rem = do_div(t, 1000000ULL);
1304 secs = (unsigned long)t;
1306 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1309 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1312 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1316 switch (entry->type) {
1318 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1321 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1322 entry->fn.parent_ip) {
1323 ret = trace_seq_printf(s, " <-");
1326 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1331 ret = trace_seq_printf(s, "\n");
1337 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1338 state_to_char[entry->ctx.prev_state] : 'X';
1339 T = entry->ctx.next_state < sizeof(state_to_char) ?
1340 state_to_char[entry->ctx.next_state] : 'X';
1341 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
1342 entry->ctx.prev_pid,
1343 entry->ctx.prev_prio,
1345 entry->type == TRACE_CTX ? "==>" : " +",
1346 entry->ctx.next_pid,
1347 entry->ctx.next_prio,
1353 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1354 entry->special.arg1,
1355 entry->special.arg2,
1356 entry->special.arg3);
1361 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1363 ret = trace_seq_puts(s, " <= ");
1367 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1372 ret = trace_seq_puts(s, "\n");
1380 static int print_raw_fmt(struct trace_iterator *iter)
1382 struct trace_seq *s = &iter->seq;
1383 struct trace_entry *entry;
1389 ret = trace_seq_printf(s, "%d %d %llu ",
1390 entry->pid, iter->cpu, entry->t);
1394 switch (entry->type) {
1396 ret = trace_seq_printf(s, "%x %x\n",
1397 entry->fn.ip, entry->fn.parent_ip);
1403 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1404 state_to_char[entry->ctx.prev_state] : 'X';
1405 T = entry->ctx.next_state < sizeof(state_to_char) ?
1406 state_to_char[entry->ctx.next_state] : 'X';
1407 if (entry->type == TRACE_WAKE)
1409 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
1410 entry->ctx.prev_pid,
1411 entry->ctx.prev_prio,
1413 entry->ctx.next_pid,
1414 entry->ctx.next_prio,
1421 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1422 entry->special.arg1,
1423 entry->special.arg2,
1424 entry->special.arg3);
1432 #define SEQ_PUT_FIELD_RET(s, x) \
1434 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1438 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1440 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1444 static int print_hex_fmt(struct trace_iterator *iter)
1446 struct trace_seq *s = &iter->seq;
1447 unsigned char newline = '\n';
1448 struct trace_entry *entry;
1453 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1454 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1455 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1457 switch (entry->type) {
1459 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1460 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1464 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1465 state_to_char[entry->ctx.prev_state] : 'X';
1466 T = entry->ctx.next_state < sizeof(state_to_char) ?
1467 state_to_char[entry->ctx.next_state] : 'X';
1468 if (entry->type == TRACE_WAKE)
1470 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1471 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1472 SEQ_PUT_HEX_FIELD_RET(s, S);
1473 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1474 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1475 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1476 SEQ_PUT_HEX_FIELD_RET(s, T);
1480 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1481 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1482 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1485 SEQ_PUT_FIELD_RET(s, newline);
1490 static int print_bin_fmt(struct trace_iterator *iter)
1492 struct trace_seq *s = &iter->seq;
1493 struct trace_entry *entry;
1497 SEQ_PUT_FIELD_RET(s, entry->pid);
1498 SEQ_PUT_FIELD_RET(s, entry->cpu);
1499 SEQ_PUT_FIELD_RET(s, entry->t);
1501 switch (entry->type) {
1503 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1504 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1507 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1508 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1509 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1510 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1511 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1512 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
1516 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1517 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1518 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1524 static int trace_empty(struct trace_iterator *iter)
1526 struct trace_array_cpu *data;
1529 for_each_possible_cpu(cpu) {
1530 data = iter->tr->data[cpu];
1532 if (head_page(data) && data->trace_idx &&
1533 (data->trace_tail != data->trace_head ||
1534 data->trace_tail_idx != data->trace_head_idx))
1540 static int print_trace_line(struct trace_iterator *iter)
1542 if (iter->trace && iter->trace->print_line)
1543 return iter->trace->print_line(iter);
1545 if (trace_flags & TRACE_ITER_BIN)
1546 return print_bin_fmt(iter);
1548 if (trace_flags & TRACE_ITER_HEX)
1549 return print_hex_fmt(iter);
1551 if (trace_flags & TRACE_ITER_RAW)
1552 return print_raw_fmt(iter);
1554 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1555 return print_lat_fmt(iter, iter->idx, iter->cpu);
1557 return print_trace_fmt(iter);
1560 static int s_show(struct seq_file *m, void *v)
1562 struct trace_iterator *iter = v;
1564 if (iter->ent == NULL) {
1566 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1569 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1570 /* print nothing if the buffers are empty */
1571 if (trace_empty(iter))
1573 print_trace_header(m, iter);
1574 if (!(trace_flags & TRACE_ITER_VERBOSE))
1575 print_lat_help_header(m);
1577 if (!(trace_flags & TRACE_ITER_VERBOSE))
1578 print_func_help_header(m);
1581 print_trace_line(iter);
1582 trace_print_seq(m, &iter->seq);
1588 static struct seq_operations tracer_seq_ops = {
1595 static struct trace_iterator *
1596 __tracing_open(struct inode *inode, struct file *file, int *ret)
1598 struct trace_iterator *iter;
1600 if (tracing_disabled) {
1605 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1611 mutex_lock(&trace_types_lock);
1612 if (current_trace && current_trace->print_max)
1615 iter->tr = inode->i_private;
1616 iter->trace = current_trace;
1619 /* TODO stop tracer */
1620 *ret = seq_open(file, &tracer_seq_ops);
1622 struct seq_file *m = file->private_data;
1625 /* stop the trace while dumping */
1629 if (iter->trace && iter->trace->open)
1630 iter->trace->open(iter);
1635 mutex_unlock(&trace_types_lock);
1641 int tracing_open_generic(struct inode *inode, struct file *filp)
1643 if (tracing_disabled)
1646 filp->private_data = inode->i_private;
1650 int tracing_release(struct inode *inode, struct file *file)
1652 struct seq_file *m = (struct seq_file *)file->private_data;
1653 struct trace_iterator *iter = m->private;
1655 mutex_lock(&trace_types_lock);
1656 if (iter->trace && iter->trace->close)
1657 iter->trace->close(iter);
1659 /* reenable tracing if it was previously enabled */
1662 mutex_unlock(&trace_types_lock);
1664 seq_release(inode, file);
1669 static int tracing_open(struct inode *inode, struct file *file)
1673 __tracing_open(inode, file, &ret);
1678 static int tracing_lt_open(struct inode *inode, struct file *file)
1680 struct trace_iterator *iter;
1683 iter = __tracing_open(inode, file, &ret);
1686 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1693 t_next(struct seq_file *m, void *v, loff_t *pos)
1695 struct tracer *t = m->private;
1707 static void *t_start(struct seq_file *m, loff_t *pos)
1709 struct tracer *t = m->private;
1712 mutex_lock(&trace_types_lock);
1713 for (; t && l < *pos; t = t_next(m, t, &l))
1719 static void t_stop(struct seq_file *m, void *p)
1721 mutex_unlock(&trace_types_lock);
1724 static int t_show(struct seq_file *m, void *v)
1726 struct tracer *t = v;
1731 seq_printf(m, "%s", t->name);
1740 static struct seq_operations show_traces_seq_ops = {
1747 static int show_traces_open(struct inode *inode, struct file *file)
1751 if (tracing_disabled)
1754 ret = seq_open(file, &show_traces_seq_ops);
1756 struct seq_file *m = file->private_data;
1757 m->private = trace_types;
1763 static struct file_operations tracing_fops = {
1764 .open = tracing_open,
1766 .llseek = seq_lseek,
1767 .release = tracing_release,
1770 static struct file_operations tracing_lt_fops = {
1771 .open = tracing_lt_open,
1773 .llseek = seq_lseek,
1774 .release = tracing_release,
1777 static struct file_operations show_traces_fops = {
1778 .open = show_traces_open,
1780 .release = seq_release,
1784 * Only trace on a CPU if the bitmask is set:
1786 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1789 * When tracing/tracing_cpu_mask is modified then this holds
1790 * the new bitmask we are about to install:
1792 static cpumask_t tracing_cpumask_new;
1795 * The tracer itself will not take this lock, but still we want
1796 * to provide a consistent cpumask to user-space:
1798 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1801 * Temporary storage for the character representation of the
1802 * CPU bitmask (and one more byte for the newline):
1804 static char mask_str[NR_CPUS + 1];
1807 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1808 size_t count, loff_t *ppos)
1812 mutex_lock(&tracing_cpumask_update_lock);
1814 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1815 if (count - len < 2) {
1819 len += sprintf(mask_str + len, "\n");
1820 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1823 mutex_unlock(&tracing_cpumask_update_lock);
1829 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1830 size_t count, loff_t *ppos)
1834 mutex_lock(&tracing_cpumask_update_lock);
1835 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1839 raw_local_irq_disable();
1840 __raw_spin_lock(&ftrace_max_lock);
1841 for_each_possible_cpu(cpu) {
1843 * Increase/decrease the disabled counter if we are
1844 * about to flip a bit in the cpumask:
1846 if (cpu_isset(cpu, tracing_cpumask) &&
1847 !cpu_isset(cpu, tracing_cpumask_new)) {
1848 atomic_inc(&global_trace.data[cpu]->disabled);
1850 if (!cpu_isset(cpu, tracing_cpumask) &&
1851 cpu_isset(cpu, tracing_cpumask_new)) {
1852 atomic_dec(&global_trace.data[cpu]->disabled);
1855 __raw_spin_unlock(&ftrace_max_lock);
1856 raw_local_irq_enable();
1858 tracing_cpumask = tracing_cpumask_new;
1860 mutex_unlock(&tracing_cpumask_update_lock);
1865 mutex_unlock(&tracing_cpumask_update_lock);
1870 static struct file_operations tracing_cpumask_fops = {
1871 .open = tracing_open_generic,
1872 .read = tracing_cpumask_read,
1873 .write = tracing_cpumask_write,
1877 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1878 size_t cnt, loff_t *ppos)
1885 /* calulate max size */
1886 for (i = 0; trace_options[i]; i++) {
1887 len += strlen(trace_options[i]);
1888 len += 3; /* "no" and space */
1891 /* +2 for \n and \0 */
1892 buf = kmalloc(len + 2, GFP_KERNEL);
1896 for (i = 0; trace_options[i]; i++) {
1897 if (trace_flags & (1 << i))
1898 r += sprintf(buf + r, "%s ", trace_options[i]);
1900 r += sprintf(buf + r, "no%s ", trace_options[i]);
1903 r += sprintf(buf + r, "\n");
1904 WARN_ON(r >= len + 2);
1906 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1914 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1915 size_t cnt, loff_t *ppos)
1925 if (copy_from_user(&buf, ubuf, cnt))
1930 if (strncmp(buf, "no", 2) == 0) {
1935 for (i = 0; trace_options[i]; i++) {
1936 int len = strlen(trace_options[i]);
1938 if (strncmp(cmp, trace_options[i], len) == 0) {
1940 trace_flags &= ~(1 << i);
1942 trace_flags |= (1 << i);
1947 * If no option could be set, return an error:
1949 if (!trace_options[i])
1957 static struct file_operations tracing_iter_fops = {
1958 .open = tracing_open_generic,
1959 .read = tracing_iter_ctrl_read,
1960 .write = tracing_iter_ctrl_write,
1963 static const char readme_msg[] =
1964 "tracing mini-HOWTO:\n\n"
1966 "# mount -t debugfs nodev /debug\n\n"
1967 "# cat /debug/tracing/available_tracers\n"
1968 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1969 "# cat /debug/tracing/current_tracer\n"
1971 "# echo sched_switch > /debug/tracing/current_tracer\n"
1972 "# cat /debug/tracing/current_tracer\n"
1974 "# cat /debug/tracing/iter_ctrl\n"
1975 "noprint-parent nosym-offset nosym-addr noverbose\n"
1976 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1977 "# echo 1 > /debug/tracing/tracing_enabled\n"
1978 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1979 "echo 0 > /debug/tracing/tracing_enabled\n"
1983 tracing_readme_read(struct file *filp, char __user *ubuf,
1984 size_t cnt, loff_t *ppos)
1986 return simple_read_from_buffer(ubuf, cnt, ppos,
1987 readme_msg, strlen(readme_msg));
1990 static struct file_operations tracing_readme_fops = {
1991 .open = tracing_open_generic,
1992 .read = tracing_readme_read,
1996 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1997 size_t cnt, loff_t *ppos)
1999 struct trace_array *tr = filp->private_data;
2003 r = sprintf(buf, "%ld\n", tr->ctrl);
2004 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2008 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2009 size_t cnt, loff_t *ppos)
2011 struct trace_array *tr = filp->private_data;
2018 if (copy_from_user(&buf, ubuf, cnt))
2023 val = simple_strtoul(buf, NULL, 10);
2027 mutex_lock(&trace_types_lock);
2028 if (tr->ctrl ^ val) {
2036 if (current_trace && current_trace->ctrl_update)
2037 current_trace->ctrl_update(tr);
2039 mutex_unlock(&trace_types_lock);
2047 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2048 size_t cnt, loff_t *ppos)
2050 char buf[max_tracer_type_len+2];
2053 mutex_lock(&trace_types_lock);
2055 r = sprintf(buf, "%s\n", current_trace->name);
2057 r = sprintf(buf, "\n");
2058 mutex_unlock(&trace_types_lock);
2060 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2064 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2065 size_t cnt, loff_t *ppos)
2067 struct trace_array *tr = &global_trace;
2069 char buf[max_tracer_type_len+1];
2072 if (cnt > max_tracer_type_len)
2073 cnt = max_tracer_type_len;
2075 if (copy_from_user(&buf, ubuf, cnt))
2080 /* strip ending whitespace. */
2081 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2084 mutex_lock(&trace_types_lock);
2085 for (t = trace_types; t; t = t->next) {
2086 if (strcmp(t->name, buf) == 0)
2089 if (!t || t == current_trace)
2092 if (current_trace && current_trace->reset)
2093 current_trace->reset(tr);
2100 mutex_unlock(&trace_types_lock);
2108 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2109 size_t cnt, loff_t *ppos)
2111 unsigned long *ptr = filp->private_data;
2115 r = snprintf(buf, 64, "%ld\n",
2116 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2119 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2123 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2124 size_t cnt, loff_t *ppos)
2126 long *ptr = filp->private_data;
2133 if (copy_from_user(&buf, ubuf, cnt))
2138 val = simple_strtoul(buf, NULL, 10);
2145 static atomic_t tracing_reader;
2147 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2149 struct trace_iterator *iter;
2151 if (tracing_disabled)
2154 /* We only allow for reader of the pipe */
2155 if (atomic_inc_return(&tracing_reader) != 1) {
2156 atomic_dec(&tracing_reader);
2160 /* create a buffer to store the information to pass to userspace */
2161 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2165 iter->tr = &global_trace;
2166 iter->trace = current_trace;
2168 filp->private_data = iter;
2173 static int tracing_release_pipe(struct inode *inode, struct file *file)
2175 struct trace_iterator *iter = file->private_data;
2178 atomic_dec(&tracing_reader);
2184 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2186 struct trace_iterator *iter = filp->private_data;
2188 if (trace_flags & TRACE_ITER_BLOCK) {
2190 * Always select as readable when in blocking mode
2192 return POLLIN | POLLRDNORM;
2195 if (!trace_empty(iter))
2196 return POLLIN | POLLRDNORM;
2197 poll_wait(filp, &trace_wait, poll_table);
2198 if (!trace_empty(iter))
2199 return POLLIN | POLLRDNORM;
2209 tracing_read_pipe(struct file *filp, char __user *ubuf,
2210 size_t cnt, loff_t *ppos)
2212 struct trace_iterator *iter = filp->private_data;
2213 struct trace_array_cpu *data;
2214 struct trace_array *tr = iter->tr;
2215 struct tracer *tracer = iter->trace;
2216 static cpumask_t mask;
2218 unsigned long flags;
2219 #ifdef CONFIG_FTRACE
2227 /* return any leftover data */
2228 if (iter->seq.len > start) {
2229 len = iter->seq.len - start;
2232 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2241 trace_seq_reset(&iter->seq);
2244 while (trace_empty(iter)) {
2246 if ((filp->f_flags & O_NONBLOCK))
2250 * This is a make-shift waitqueue. The reason we don't use
2251 * an actual wait queue is because:
2252 * 1) we only ever have one waiter
2253 * 2) the tracing, traces all functions, we don't want
2254 * the overhead of calling wake_up and friends
2255 * (and tracing them too)
2256 * Anyway, this is really very primitive wakeup.
2258 set_current_state(TASK_INTERRUPTIBLE);
2259 iter->tr->waiter = current;
2261 /* sleep for one second, and try again. */
2262 schedule_timeout(HZ);
2264 iter->tr->waiter = NULL;
2266 if (signal_pending(current))
2269 if (iter->trace != current_trace)
2273 * We block until we read something and tracing is disabled.
2274 * We still block if tracing is disabled, but we have never
2275 * read anything. This allows a user to cat this file, and
2276 * then enable tracing. But after we have read something,
2277 * we give an EOF when tracing is again disabled.
2279 * iter->pos will be 0 if we haven't read anything.
2281 if (!tracer_enabled && iter->pos)
2287 /* stop when tracing is finished */
2288 if (trace_empty(iter))
2291 if (cnt >= PAGE_SIZE)
2292 cnt = PAGE_SIZE - 1;
2294 memset(iter, 0, sizeof(*iter));
2296 iter->trace = tracer;
2300 * We need to stop all tracing on all CPUS to read the
2301 * the next buffer. This is a bit expensive, but is
2302 * not done often. We fill all what we can read,
2303 * and then release the locks again.
2307 local_irq_save(flags);
2308 #ifdef CONFIG_FTRACE
2309 ftrace_save = ftrace_enabled;
2313 for_each_possible_cpu(cpu) {
2314 data = iter->tr->data[cpu];
2316 if (!head_page(data) || !data->trace_idx)
2319 atomic_inc(&data->disabled);
2323 for_each_cpu_mask(cpu, mask) {
2324 data = iter->tr->data[cpu];
2325 __raw_spin_lock(&data->lock);
2328 while (find_next_entry_inc(iter) != NULL) {
2329 int len = iter->seq.len;
2331 ret = print_trace_line(iter);
2333 /* don't print partial lines */
2334 iter->seq.len = len;
2338 trace_consume(iter);
2340 if (iter->seq.len >= cnt)
2344 for_each_cpu_mask(cpu, mask) {
2345 data = iter->tr->data[cpu];
2346 __raw_spin_unlock(&data->lock);
2349 for_each_cpu_mask(cpu, mask) {
2350 data = iter->tr->data[cpu];
2351 atomic_dec(&data->disabled);
2353 #ifdef CONFIG_FTRACE
2354 ftrace_enabled = ftrace_save;
2356 local_irq_restore(flags);
2358 /* Now copy what we have to the user */
2359 read = iter->seq.len;
2363 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2365 if (read < iter->seq.len)
2368 trace_seq_reset(&iter->seq);
2377 tracing_entries_read(struct file *filp, char __user *ubuf,
2378 size_t cnt, loff_t *ppos)
2380 struct trace_array *tr = filp->private_data;
2384 r = sprintf(buf, "%lu\n", tr->entries);
2385 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2389 tracing_entries_write(struct file *filp, const char __user *ubuf,
2390 size_t cnt, loff_t *ppos)
2398 if (copy_from_user(&buf, ubuf, cnt))
2403 val = simple_strtoul(buf, NULL, 10);
2405 /* must have at least 1 entry */
2409 mutex_lock(&trace_types_lock);
2411 if (current_trace != &no_tracer) {
2413 pr_info("ftrace: set current_tracer to none"
2414 " before modifying buffer size\n");
2418 if (val > global_trace.entries) {
2419 while (global_trace.entries < val) {
2420 if (trace_alloc_page()) {
2426 /* include the number of entries in val (inc of page entries) */
2427 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2434 max_tr.entries = global_trace.entries;
2435 mutex_unlock(&trace_types_lock);
2440 static struct file_operations tracing_max_lat_fops = {
2441 .open = tracing_open_generic,
2442 .read = tracing_max_lat_read,
2443 .write = tracing_max_lat_write,
2446 static struct file_operations tracing_ctrl_fops = {
2447 .open = tracing_open_generic,
2448 .read = tracing_ctrl_read,
2449 .write = tracing_ctrl_write,
2452 static struct file_operations set_tracer_fops = {
2453 .open = tracing_open_generic,
2454 .read = tracing_set_trace_read,
2455 .write = tracing_set_trace_write,
2458 static struct file_operations tracing_pipe_fops = {
2459 .open = tracing_open_pipe,
2460 .poll = tracing_poll_pipe,
2461 .read = tracing_read_pipe,
2462 .release = tracing_release_pipe,
2465 static struct file_operations tracing_entries_fops = {
2466 .open = tracing_open_generic,
2467 .read = tracing_entries_read,
2468 .write = tracing_entries_write,
2471 #ifdef CONFIG_DYNAMIC_FTRACE
2474 tracing_read_long(struct file *filp, char __user *ubuf,
2475 size_t cnt, loff_t *ppos)
2477 unsigned long *p = filp->private_data;
2481 r = sprintf(buf, "%ld\n", *p);
2483 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2486 static struct file_operations tracing_read_long_fops = {
2487 .open = tracing_open_generic,
2488 .read = tracing_read_long,
2492 static struct dentry *d_tracer;
2494 struct dentry *tracing_init_dentry(void)
2501 d_tracer = debugfs_create_dir("tracing", NULL);
2503 if (!d_tracer && !once) {
2505 pr_warning("Could not create debugfs directory 'tracing'\n");
2512 #ifdef CONFIG_FTRACE_SELFTEST
2513 /* Let selftest have access to static functions in this file */
2514 #include "trace_selftest.c"
2517 static __init void tracer_init_debugfs(void)
2519 struct dentry *d_tracer;
2520 struct dentry *entry;
2522 d_tracer = tracing_init_dentry();
2524 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2525 &global_trace, &tracing_ctrl_fops);
2527 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2529 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2530 NULL, &tracing_iter_fops);
2532 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2534 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2535 NULL, &tracing_cpumask_fops);
2537 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2539 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2540 &global_trace, &tracing_lt_fops);
2542 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2544 entry = debugfs_create_file("trace", 0444, d_tracer,
2545 &global_trace, &tracing_fops);
2547 pr_warning("Could not create debugfs 'trace' entry\n");
2549 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2550 &global_trace, &show_traces_fops);
2552 pr_warning("Could not create debugfs 'trace' entry\n");
2554 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2555 &global_trace, &set_tracer_fops);
2557 pr_warning("Could not create debugfs 'trace' entry\n");
2559 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2560 &tracing_max_latency,
2561 &tracing_max_lat_fops);
2563 pr_warning("Could not create debugfs "
2564 "'tracing_max_latency' entry\n");
2566 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2567 &tracing_thresh, &tracing_max_lat_fops);
2569 pr_warning("Could not create debugfs "
2570 "'tracing_threash' entry\n");
2571 entry = debugfs_create_file("README", 0644, d_tracer,
2572 NULL, &tracing_readme_fops);
2574 pr_warning("Could not create debugfs 'README' entry\n");
2576 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2577 NULL, &tracing_pipe_fops);
2579 pr_warning("Could not create debugfs "
2580 "'tracing_threash' entry\n");
2582 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2583 &global_trace, &tracing_entries_fops);
2585 pr_warning("Could not create debugfs "
2586 "'tracing_threash' entry\n");
2588 #ifdef CONFIG_DYNAMIC_FTRACE
2589 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2590 &ftrace_update_tot_cnt,
2591 &tracing_read_long_fops);
2593 pr_warning("Could not create debugfs "
2594 "'dyn_ftrace_total_info' entry\n");
2598 static int trace_alloc_page(void)
2600 struct trace_array_cpu *data;
2601 struct page *page, *tmp;
2606 /* first allocate a page for each CPU */
2607 for_each_possible_cpu(i) {
2608 array = (void *)__get_free_page(GFP_KERNEL);
2609 if (array == NULL) {
2610 printk(KERN_ERR "tracer: failed to allocate page"
2611 "for trace buffer!\n");
2615 page = virt_to_page(array);
2616 list_add(&page->lru, &pages);
2618 /* Only allocate if we are actually using the max trace */
2619 #ifdef CONFIG_TRACER_MAX_TRACE
2620 array = (void *)__get_free_page(GFP_KERNEL);
2621 if (array == NULL) {
2622 printk(KERN_ERR "tracer: failed to allocate page"
2623 "for trace buffer!\n");
2626 page = virt_to_page(array);
2627 list_add(&page->lru, &pages);
2631 /* Now that we successfully allocate a page per CPU, add them */
2632 for_each_possible_cpu(i) {
2633 data = global_trace.data[i];
2634 page = list_entry(pages.next, struct page, lru);
2635 list_del_init(&page->lru);
2636 list_add_tail(&page->lru, &data->trace_pages);
2639 #ifdef CONFIG_TRACER_MAX_TRACE
2640 data = max_tr.data[i];
2641 page = list_entry(pages.next, struct page, lru);
2642 list_del_init(&page->lru);
2643 list_add_tail(&page->lru, &data->trace_pages);
2647 global_trace.entries += ENTRIES_PER_PAGE;
2652 list_for_each_entry_safe(page, tmp, &pages, lru) {
2653 list_del_init(&page->lru);
2659 static int trace_free_page(void)
2661 struct trace_array_cpu *data;
2663 struct list_head *p;
2667 /* free one page from each buffer */
2668 for_each_possible_cpu(i) {
2669 data = global_trace.data[i];
2670 p = data->trace_pages.next;
2671 if (p == &data->trace_pages) {
2672 /* should never happen */
2674 tracing_disabled = 1;
2678 page = list_entry(p, struct page, lru);
2680 list_del(&page->lru);
2683 tracing_reset(data);
2685 #ifdef CONFIG_TRACER_MAX_TRACE
2686 data = max_tr.data[i];
2687 p = data->trace_pages.next;
2688 if (p == &data->trace_pages) {
2689 /* should never happen */
2691 tracing_disabled = 1;
2695 page = list_entry(p, struct page, lru);
2697 list_del(&page->lru);
2700 tracing_reset(data);
2703 global_trace.entries -= ENTRIES_PER_PAGE;
2708 __init static int tracer_alloc_buffers(void)
2710 struct trace_array_cpu *data;
2717 global_trace.ctrl = tracer_enabled;
2719 /* Allocate the first page for all buffers */
2720 for_each_possible_cpu(i) {
2721 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2722 max_tr.data[i] = &per_cpu(max_data, i);
2724 array = (void *)__get_free_page(GFP_KERNEL);
2725 if (array == NULL) {
2726 printk(KERN_ERR "tracer: failed to allocate page"
2727 "for trace buffer!\n");
2731 /* set the array to the list */
2732 INIT_LIST_HEAD(&data->trace_pages);
2733 page = virt_to_page(array);
2734 list_add(&page->lru, &data->trace_pages);
2735 /* use the LRU flag to differentiate the two buffers */
2738 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2739 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2741 /* Only allocate if we are actually using the max trace */
2742 #ifdef CONFIG_TRACER_MAX_TRACE
2743 array = (void *)__get_free_page(GFP_KERNEL);
2744 if (array == NULL) {
2745 printk(KERN_ERR "tracer: failed to allocate page"
2746 "for trace buffer!\n");
2750 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2751 page = virt_to_page(array);
2752 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2758 * Since we allocate by orders of pages, we may be able to
2761 global_trace.entries = ENTRIES_PER_PAGE;
2764 while (global_trace.entries < trace_nr_entries) {
2765 if (trace_alloc_page())
2769 max_tr.entries = global_trace.entries;
2771 pr_info("tracer: %d pages allocated for %ld",
2772 pages, trace_nr_entries);
2773 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2774 pr_info(" actual entries %ld\n", global_trace.entries);
2776 tracer_init_debugfs();
2778 trace_init_cmdlines();
2780 register_tracer(&no_tracer);
2781 current_trace = &no_tracer;
2783 /* All seems OK, enable tracing */
2784 tracing_disabled = 0;
2789 for (i-- ; i >= 0; i--) {
2790 struct page *page, *tmp;
2791 struct trace_array_cpu *data = global_trace.data[i];
2794 list_for_each_entry_safe(page, tmp,
2795 &data->trace_pages, lru) {
2796 list_del_init(&page->lru);
2801 #ifdef CONFIG_TRACER_MAX_TRACE
2802 data = max_tr.data[i];
2804 list_for_each_entry_safe(page, tmp,
2805 &data->trace_pages, lru) {
2806 list_del_init(&page->lru);
2814 fs_initcall(tracer_alloc_buffers);