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);
251 static const char hex2asc[] = "0123456789abcdef";
254 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
256 unsigned char hex[HEX_CHARS];
257 unsigned char *data = mem;
261 BUG_ON(len >= HEX_CHARS);
264 for (i = 0, j = 0; i < len; i++) {
266 for (i = len-1, j = 0; i >= 0; i--) {
270 hex[j++] = hex2asc[byte & 0x0f];
271 hex[j++] = hex2asc[byte >> 4];
275 return trace_seq_putmem(s, hex, j);
279 trace_seq_reset(struct trace_seq *s)
285 trace_print_seq(struct seq_file *m, struct trace_seq *s)
287 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
290 seq_puts(m, s->buffer);
296 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
298 struct list_head flip_pages;
300 INIT_LIST_HEAD(&flip_pages);
302 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
303 sizeof(struct trace_array_cpu) -
304 offsetof(struct trace_array_cpu, trace_head_idx));
308 list_splice_init(&tr1->trace_pages, &flip_pages);
309 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
310 list_splice_init(&flip_pages, &tr2->trace_pages);
311 BUG_ON(!list_empty(&flip_pages));
317 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
319 struct trace_array_cpu *data;
322 WARN_ON_ONCE(!irqs_disabled());
323 __raw_spin_lock(&ftrace_max_lock);
324 /* clear out all the previous traces */
325 for_each_possible_cpu(i) {
327 flip_trace(max_tr.data[i], data);
331 __update_max_tr(tr, tsk, cpu);
332 __raw_spin_unlock(&ftrace_max_lock);
336 * update_max_tr_single - only copy one trace over, and reset the rest
338 * @tsk - task with the latency
339 * @cpu - the cpu of the buffer to copy.
342 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
344 struct trace_array_cpu *data = tr->data[cpu];
347 WARN_ON_ONCE(!irqs_disabled());
348 __raw_spin_lock(&ftrace_max_lock);
349 for_each_possible_cpu(i)
350 tracing_reset(max_tr.data[i]);
352 flip_trace(max_tr.data[cpu], data);
355 __update_max_tr(tr, tsk, cpu);
356 __raw_spin_unlock(&ftrace_max_lock);
359 int register_tracer(struct tracer *type)
366 pr_info("Tracer must have a name\n");
370 mutex_lock(&trace_types_lock);
371 for (t = trace_types; t; t = t->next) {
372 if (strcmp(type->name, t->name) == 0) {
374 pr_info("Trace %s already registered\n",
381 #ifdef CONFIG_FTRACE_STARTUP_TEST
382 if (type->selftest) {
383 struct tracer *saved_tracer = current_trace;
384 struct trace_array_cpu *data;
385 struct trace_array *tr = &global_trace;
386 int saved_ctrl = tr->ctrl;
389 * Run a selftest on this tracer.
390 * Here we reset the trace buffer, and set the current
391 * tracer to be this tracer. The tracer can then run some
392 * internal tracing to verify that everything is in order.
393 * If we fail, we do not register this tracer.
395 for_each_possible_cpu(i) {
397 if (!head_page(data))
401 current_trace = type;
403 /* the test is responsible for initializing and enabling */
404 pr_info("Testing tracer %s: ", type->name);
405 ret = type->selftest(type, tr);
406 /* the test is responsible for resetting too */
407 current_trace = saved_tracer;
408 tr->ctrl = saved_ctrl;
410 printk(KERN_CONT "FAILED!\n");
413 /* Only reset on passing, to avoid touching corrupted buffers */
414 for_each_possible_cpu(i) {
416 if (!head_page(data))
420 printk(KERN_CONT "PASSED\n");
424 type->next = trace_types;
426 len = strlen(type->name);
427 if (len > max_tracer_type_len)
428 max_tracer_type_len = len;
431 mutex_unlock(&trace_types_lock);
436 void unregister_tracer(struct tracer *type)
441 mutex_lock(&trace_types_lock);
442 for (t = &trace_types; *t; t = &(*t)->next) {
446 pr_info("Trace %s not registered\n", type->name);
451 if (strlen(type->name) != max_tracer_type_len)
454 max_tracer_type_len = 0;
455 for (t = &trace_types; *t; t = &(*t)->next) {
456 len = strlen((*t)->name);
457 if (len > max_tracer_type_len)
458 max_tracer_type_len = len;
461 mutex_unlock(&trace_types_lock);
464 void tracing_reset(struct trace_array_cpu *data)
467 data->trace_head = data->trace_tail = head_page(data);
468 data->trace_head_idx = 0;
469 data->trace_tail_idx = 0;
472 #define SAVED_CMDLINES 128
473 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
474 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
475 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
476 static int cmdline_idx;
477 static DEFINE_SPINLOCK(trace_cmdline_lock);
478 atomic_t trace_record_cmdline_disabled;
480 static void trace_init_cmdlines(void)
482 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
483 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
487 void trace_stop_cmdline_recording(void);
489 static void trace_save_cmdline(struct task_struct *tsk)
494 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
498 * It's not the end of the world if we don't get
499 * the lock, but we also don't want to spin
500 * nor do we want to disable interrupts,
501 * so if we miss here, then better luck next time.
503 if (!spin_trylock(&trace_cmdline_lock))
506 idx = map_pid_to_cmdline[tsk->pid];
507 if (idx >= SAVED_CMDLINES) {
508 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
510 map = map_cmdline_to_pid[idx];
511 if (map <= PID_MAX_DEFAULT)
512 map_pid_to_cmdline[map] = (unsigned)-1;
514 map_pid_to_cmdline[tsk->pid] = idx;
519 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
521 spin_unlock(&trace_cmdline_lock);
524 static char *trace_find_cmdline(int pid)
526 char *cmdline = "<...>";
532 if (pid > PID_MAX_DEFAULT)
535 map = map_pid_to_cmdline[pid];
536 if (map >= SAVED_CMDLINES)
539 cmdline = saved_cmdlines[map];
545 void tracing_record_cmdline(struct task_struct *tsk)
547 if (atomic_read(&trace_record_cmdline_disabled))
550 trace_save_cmdline(tsk);
553 static inline struct list_head *
554 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
557 * Roundrobin - but skip the head (which is not a real page):
560 if (unlikely(next == &data->trace_pages))
562 BUG_ON(next == &data->trace_pages);
568 trace_next_page(struct trace_array_cpu *data, void *addr)
570 struct list_head *next;
573 page = virt_to_page(addr);
575 next = trace_next_list(data, &page->lru);
576 page = list_entry(next, struct page, lru);
578 return page_address(page);
581 static inline struct trace_entry *
582 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
584 unsigned long idx, idx_next;
585 struct trace_entry *entry;
588 idx = data->trace_head_idx;
591 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
593 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
595 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
596 data->trace_head = trace_next_page(data, data->trace_head);
600 if (data->trace_head == data->trace_tail &&
601 idx_next == data->trace_tail_idx) {
603 data->trace_tail_idx++;
604 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
606 trace_next_page(data, data->trace_tail);
607 data->trace_tail_idx = 0;
611 data->trace_head_idx = idx_next;
617 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
619 struct task_struct *tsk = current;
622 pc = preempt_count();
624 entry->preempt_count = pc & 0xff;
625 entry->pid = (tsk) ? tsk->pid : 0;
626 entry->t = ftrace_now(raw_smp_processor_id());
627 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
628 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
629 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
630 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
634 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
635 unsigned long ip, unsigned long parent_ip, unsigned long flags)
637 struct trace_entry *entry;
638 unsigned long irq_flags;
640 raw_local_irq_save(irq_flags);
641 __raw_spin_lock(&data->lock);
642 entry = tracing_get_trace_entry(tr, data);
643 tracing_generic_entry_update(entry, flags);
644 entry->type = TRACE_FN;
646 entry->fn.parent_ip = parent_ip;
647 __raw_spin_unlock(&data->lock);
648 raw_local_irq_restore(irq_flags);
652 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
653 unsigned long ip, unsigned long parent_ip, unsigned long flags)
655 if (likely(!atomic_read(&data->disabled)))
656 trace_function(tr, data, ip, parent_ip, flags);
660 __trace_special(void *__tr, void *__data,
661 unsigned long arg1, unsigned long arg2, unsigned long arg3)
663 struct trace_array_cpu *data = __data;
664 struct trace_array *tr = __tr;
665 struct trace_entry *entry;
666 unsigned long irq_flags;
668 raw_local_irq_save(irq_flags);
669 __raw_spin_lock(&data->lock);
670 entry = tracing_get_trace_entry(tr, data);
671 tracing_generic_entry_update(entry, 0);
672 entry->type = TRACE_SPECIAL;
673 entry->special.arg1 = arg1;
674 entry->special.arg2 = arg2;
675 entry->special.arg3 = arg3;
676 __raw_spin_unlock(&data->lock);
677 raw_local_irq_restore(irq_flags);
682 void __trace_stack(struct trace_array *tr,
683 struct trace_array_cpu *data,
687 struct trace_entry *entry;
688 struct stack_trace trace;
690 if (!(trace_flags & TRACE_ITER_STACKTRACE))
693 entry = tracing_get_trace_entry(tr, data);
694 tracing_generic_entry_update(entry, flags);
695 entry->type = TRACE_STACK;
697 memset(&entry->stack, 0, sizeof(entry->stack));
699 trace.nr_entries = 0;
700 trace.max_entries = FTRACE_STACK_ENTRIES;
702 trace.entries = entry->stack.caller;
704 save_stack_trace(&trace);
708 tracing_sched_switch_trace(struct trace_array *tr,
709 struct trace_array_cpu *data,
710 struct task_struct *prev,
711 struct task_struct *next,
714 struct trace_entry *entry;
715 unsigned long irq_flags;
717 raw_local_irq_save(irq_flags);
718 __raw_spin_lock(&data->lock);
719 entry = tracing_get_trace_entry(tr, data);
720 tracing_generic_entry_update(entry, flags);
721 entry->type = TRACE_CTX;
722 entry->ctx.prev_pid = prev->pid;
723 entry->ctx.prev_prio = prev->prio;
724 entry->ctx.prev_state = prev->state;
725 entry->ctx.next_pid = next->pid;
726 entry->ctx.next_prio = next->prio;
727 entry->ctx.next_state = next->state;
728 __trace_stack(tr, data, flags, 4);
729 __raw_spin_unlock(&data->lock);
730 raw_local_irq_restore(irq_flags);
734 tracing_sched_wakeup_trace(struct trace_array *tr,
735 struct trace_array_cpu *data,
736 struct task_struct *wakee,
737 struct task_struct *curr,
740 struct trace_entry *entry;
741 unsigned long irq_flags;
743 raw_local_irq_save(irq_flags);
744 __raw_spin_lock(&data->lock);
745 entry = tracing_get_trace_entry(tr, data);
746 tracing_generic_entry_update(entry, flags);
747 entry->type = TRACE_WAKE;
748 entry->ctx.prev_pid = curr->pid;
749 entry->ctx.prev_prio = curr->prio;
750 entry->ctx.prev_state = curr->state;
751 entry->ctx.next_pid = wakee->pid;
752 entry->ctx.next_prio = wakee->prio;
753 entry->ctx.next_state = wakee->state;
754 __trace_stack(tr, data, flags, 5);
755 __raw_spin_unlock(&data->lock);
756 raw_local_irq_restore(irq_flags);
763 function_trace_call(unsigned long ip, unsigned long parent_ip)
765 struct trace_array *tr = &global_trace;
766 struct trace_array_cpu *data;
771 if (unlikely(!tracer_enabled))
774 local_irq_save(flags);
775 cpu = raw_smp_processor_id();
776 data = tr->data[cpu];
777 disabled = atomic_inc_return(&data->disabled);
779 if (likely(disabled == 1))
780 trace_function(tr, data, ip, parent_ip, flags);
782 atomic_dec(&data->disabled);
783 local_irq_restore(flags);
786 static struct ftrace_ops trace_ops __read_mostly =
788 .func = function_trace_call,
791 void tracing_start_function_trace(void)
793 register_ftrace_function(&trace_ops);
796 void tracing_stop_function_trace(void)
798 unregister_ftrace_function(&trace_ops);
802 enum trace_file_type {
803 TRACE_FILE_LAT_FMT = 1,
806 static struct trace_entry *
807 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
808 struct trace_iterator *iter, int cpu)
811 struct trace_entry *array;
813 if (iter->next_idx[cpu] >= tr->entries ||
814 iter->next_idx[cpu] >= data->trace_idx ||
815 (data->trace_head == data->trace_tail &&
816 data->trace_head_idx == data->trace_tail_idx))
819 if (!iter->next_page[cpu]) {
820 /* Initialize the iterator for this cpu trace buffer */
821 WARN_ON(!data->trace_tail);
822 page = virt_to_page(data->trace_tail);
823 iter->next_page[cpu] = &page->lru;
824 iter->next_page_idx[cpu] = data->trace_tail_idx;
827 page = list_entry(iter->next_page[cpu], struct page, lru);
828 BUG_ON(&data->trace_pages == &page->lru);
830 array = page_address(page);
832 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
833 return &array[iter->next_page_idx[cpu]];
836 static struct trace_entry *
837 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
839 struct trace_array *tr = iter->tr;
840 struct trace_entry *ent, *next = NULL;
844 for_each_possible_cpu(cpu) {
845 if (!head_page(tr->data[cpu]))
847 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
849 * Pick the entry with the smallest timestamp:
851 if (ent && (!next || ent->t < next->t)) {
863 static void trace_iterator_increment(struct trace_iterator *iter)
866 iter->next_idx[iter->cpu]++;
867 iter->next_page_idx[iter->cpu]++;
869 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
870 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
872 iter->next_page_idx[iter->cpu] = 0;
873 iter->next_page[iter->cpu] =
874 trace_next_list(data, iter->next_page[iter->cpu]);
878 static void trace_consume(struct trace_iterator *iter)
880 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
882 data->trace_tail_idx++;
883 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
884 data->trace_tail = trace_next_page(data, data->trace_tail);
885 data->trace_tail_idx = 0;
888 /* Check if we empty it, then reset the index */
889 if (data->trace_head == data->trace_tail &&
890 data->trace_head_idx == data->trace_tail_idx)
894 static void *find_next_entry_inc(struct trace_iterator *iter)
896 struct trace_entry *next;
899 next = find_next_entry(iter, &next_cpu);
901 iter->prev_ent = iter->ent;
902 iter->prev_cpu = iter->cpu;
905 iter->cpu = next_cpu;
908 trace_iterator_increment(iter);
910 return next ? iter : NULL;
913 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
915 struct trace_iterator *iter = m->private;
916 void *last_ent = iter->ent;
922 /* can't go backwards */
927 ent = find_next_entry_inc(iter);
931 while (ent && iter->idx < i)
932 ent = find_next_entry_inc(iter);
936 if (last_ent && !ent)
937 seq_puts(m, "\n\nvim:ft=help\n");
942 static void *s_start(struct seq_file *m, loff_t *pos)
944 struct trace_iterator *iter = m->private;
949 mutex_lock(&trace_types_lock);
951 if (!current_trace || current_trace != iter->trace) {
952 mutex_unlock(&trace_types_lock);
956 atomic_inc(&trace_record_cmdline_disabled);
958 /* let the tracer grab locks here if needed */
959 if (current_trace->start)
960 current_trace->start(iter);
962 if (*pos != iter->pos) {
966 iter->prev_ent = NULL;
969 for_each_possible_cpu(i) {
970 iter->next_idx[i] = 0;
971 iter->next_page[i] = NULL;
974 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
979 p = s_next(m, p, &l);
985 static void s_stop(struct seq_file *m, void *p)
987 struct trace_iterator *iter = m->private;
989 atomic_dec(&trace_record_cmdline_disabled);
991 /* let the tracer release locks here if needed */
992 if (current_trace && current_trace == iter->trace && iter->trace->stop)
993 iter->trace->stop(iter);
995 mutex_unlock(&trace_types_lock);
999 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1001 #ifdef CONFIG_KALLSYMS
1002 char str[KSYM_SYMBOL_LEN];
1004 kallsyms_lookup(address, NULL, NULL, NULL, str);
1006 return trace_seq_printf(s, fmt, str);
1012 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1013 unsigned long address)
1015 #ifdef CONFIG_KALLSYMS
1016 char str[KSYM_SYMBOL_LEN];
1018 sprint_symbol(str, address);
1019 return trace_seq_printf(s, fmt, str);
1024 #ifndef CONFIG_64BIT
1025 # define IP_FMT "%08lx"
1027 # define IP_FMT "%016lx"
1031 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1036 return trace_seq_printf(s, "0");
1038 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1039 ret = seq_print_sym_offset(s, "%s", ip);
1041 ret = seq_print_sym_short(s, "%s", ip);
1046 if (sym_flags & TRACE_ITER_SYM_ADDR)
1047 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1051 static void print_lat_help_header(struct seq_file *m)
1053 seq_puts(m, "# _------=> CPU# \n");
1054 seq_puts(m, "# / _-----=> irqs-off \n");
1055 seq_puts(m, "# | / _----=> need-resched \n");
1056 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1057 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1058 seq_puts(m, "# |||| / \n");
1059 seq_puts(m, "# ||||| delay \n");
1060 seq_puts(m, "# cmd pid ||||| time | caller \n");
1061 seq_puts(m, "# \\ / ||||| \\ | / \n");
1064 static void print_func_help_header(struct seq_file *m)
1066 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1067 seq_puts(m, "# | | | | |\n");
1072 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1074 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1075 struct trace_array *tr = iter->tr;
1076 struct trace_array_cpu *data = tr->data[tr->cpu];
1077 struct tracer *type = current_trace;
1078 unsigned long total = 0;
1079 unsigned long entries = 0;
1081 const char *name = "preemption";
1086 for_each_possible_cpu(cpu) {
1087 if (head_page(tr->data[cpu])) {
1088 total += tr->data[cpu]->trace_idx;
1089 if (tr->data[cpu]->trace_idx > tr->entries)
1090 entries += tr->entries;
1092 entries += tr->data[cpu]->trace_idx;
1096 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1098 seq_puts(m, "-----------------------------------"
1099 "---------------------------------\n");
1100 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1101 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1102 nsecs_to_usecs(data->saved_latency),
1106 #if defined(CONFIG_PREEMPT_NONE)
1108 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1110 #elif defined(CONFIG_PREEMPT_DESKTOP)
1115 /* These are reserved for later use */
1118 seq_printf(m, " #P:%d)\n", num_online_cpus());
1122 seq_puts(m, " -----------------\n");
1123 seq_printf(m, " | task: %.16s-%d "
1124 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1125 data->comm, data->pid, data->uid, data->nice,
1126 data->policy, data->rt_priority);
1127 seq_puts(m, " -----------------\n");
1129 if (data->critical_start) {
1130 seq_puts(m, " => started at: ");
1131 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1132 trace_print_seq(m, &iter->seq);
1133 seq_puts(m, "\n => ended at: ");
1134 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1135 trace_print_seq(m, &iter->seq);
1143 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1145 int hardirq, softirq;
1148 comm = trace_find_cmdline(entry->pid);
1150 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1151 trace_seq_printf(s, "%d", cpu);
1152 trace_seq_printf(s, "%c%c",
1153 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1154 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1156 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1157 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1158 if (hardirq && softirq)
1159 trace_seq_putc(s, 'H');
1162 trace_seq_putc(s, 'h');
1165 trace_seq_putc(s, 's');
1167 trace_seq_putc(s, '.');
1171 if (entry->preempt_count)
1172 trace_seq_printf(s, "%x", entry->preempt_count);
1174 trace_seq_puts(s, ".");
1177 unsigned long preempt_mark_thresh = 100;
1180 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1181 unsigned long rel_usecs)
1183 trace_seq_printf(s, " %4lldus", abs_usecs);
1184 if (rel_usecs > preempt_mark_thresh)
1185 trace_seq_puts(s, "!: ");
1186 else if (rel_usecs > 1)
1187 trace_seq_puts(s, "+: ");
1189 trace_seq_puts(s, " : ");
1192 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1195 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1197 struct trace_seq *s = &iter->seq;
1198 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1199 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1200 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1201 struct trace_entry *entry = iter->ent;
1202 unsigned long abs_usecs;
1203 unsigned long rel_usecs;
1211 rel_usecs = ns2usecs(next_entry->t - entry->t);
1212 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1215 comm = trace_find_cmdline(entry->pid);
1216 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1217 " %ld.%03ldms (+%ld.%03ldms): ",
1219 entry->pid, cpu, entry->flags,
1220 entry->preempt_count, trace_idx,
1223 abs_usecs % 1000, rel_usecs/1000,
1226 lat_print_generic(s, entry, cpu);
1227 lat_print_timestamp(s, abs_usecs, rel_usecs);
1229 switch (entry->type) {
1231 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1232 trace_seq_puts(s, " (");
1233 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1234 trace_seq_puts(s, ")\n");
1238 T = entry->ctx.next_state < sizeof(state_to_char) ?
1239 state_to_char[entry->ctx.next_state] : 'X';
1241 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1242 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1243 comm = trace_find_cmdline(entry->ctx.next_pid);
1244 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1245 entry->ctx.prev_pid,
1246 entry->ctx.prev_prio,
1247 S, entry->type == TRACE_CTX ? "==>" : " +",
1248 entry->ctx.next_pid,
1249 entry->ctx.next_prio,
1253 trace_seq_printf(s, "# %ld %ld %ld\n",
1254 entry->special.arg1,
1255 entry->special.arg2,
1256 entry->special.arg3);
1259 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1261 trace_seq_puts(s, " <= ");
1262 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1264 trace_seq_puts(s, "\n");
1267 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1272 static int print_trace_fmt(struct trace_iterator *iter)
1274 struct trace_seq *s = &iter->seq;
1275 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1276 struct trace_entry *entry;
1277 unsigned long usec_rem;
1278 unsigned long long t;
1287 comm = trace_find_cmdline(iter->ent->pid);
1289 t = ns2usecs(entry->t);
1290 usec_rem = do_div(t, 1000000ULL);
1291 secs = (unsigned long)t;
1293 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1296 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1299 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1303 switch (entry->type) {
1305 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1308 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1309 entry->fn.parent_ip) {
1310 ret = trace_seq_printf(s, " <-");
1313 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1318 ret = trace_seq_printf(s, "\n");
1324 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1325 state_to_char[entry->ctx.prev_state] : 'X';
1326 T = entry->ctx.next_state < sizeof(state_to_char) ?
1327 state_to_char[entry->ctx.next_state] : 'X';
1328 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
1329 entry->ctx.prev_pid,
1330 entry->ctx.prev_prio,
1332 entry->type == TRACE_CTX ? "==>" : " +",
1333 entry->ctx.next_pid,
1334 entry->ctx.next_prio,
1340 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1341 entry->special.arg1,
1342 entry->special.arg2,
1343 entry->special.arg3);
1348 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1350 ret = trace_seq_puts(s, " <= ");
1354 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1359 ret = trace_seq_puts(s, "\n");
1367 static int print_raw_fmt(struct trace_iterator *iter)
1369 struct trace_seq *s = &iter->seq;
1370 struct trace_entry *entry;
1376 ret = trace_seq_printf(s, "%d %d %llu ",
1377 entry->pid, iter->cpu, entry->t);
1381 switch (entry->type) {
1383 ret = trace_seq_printf(s, "%x %x\n",
1384 entry->fn.ip, entry->fn.parent_ip);
1390 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1391 state_to_char[entry->ctx.prev_state] : 'X';
1392 T = entry->ctx.next_state < sizeof(state_to_char) ?
1393 state_to_char[entry->ctx.next_state] : 'X';
1394 if (entry->type == TRACE_WAKE)
1396 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
1397 entry->ctx.prev_pid,
1398 entry->ctx.prev_prio,
1400 entry->ctx.next_pid,
1401 entry->ctx.next_prio,
1408 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1409 entry->special.arg1,
1410 entry->special.arg2,
1411 entry->special.arg3);
1419 #define SEQ_PUT_FIELD_RET(s, x) \
1421 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1425 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1427 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1431 static int print_hex_fmt(struct trace_iterator *iter)
1433 struct trace_seq *s = &iter->seq;
1434 unsigned char newline = '\n';
1435 struct trace_entry *entry;
1440 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1441 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1442 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1444 switch (entry->type) {
1446 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1447 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1451 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1452 state_to_char[entry->ctx.prev_state] : 'X';
1453 T = entry->ctx.next_state < sizeof(state_to_char) ?
1454 state_to_char[entry->ctx.next_state] : 'X';
1455 if (entry->type == TRACE_WAKE)
1457 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1458 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1459 SEQ_PUT_HEX_FIELD_RET(s, S);
1460 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1461 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1462 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1463 SEQ_PUT_HEX_FIELD_RET(s, T);
1467 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1468 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1469 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1472 SEQ_PUT_FIELD_RET(s, newline);
1477 static int print_bin_fmt(struct trace_iterator *iter)
1479 struct trace_seq *s = &iter->seq;
1480 struct trace_entry *entry;
1484 SEQ_PUT_FIELD_RET(s, entry->pid);
1485 SEQ_PUT_FIELD_RET(s, entry->cpu);
1486 SEQ_PUT_FIELD_RET(s, entry->t);
1488 switch (entry->type) {
1490 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1491 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1494 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1495 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1496 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1497 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1498 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1499 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
1503 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1504 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1505 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1511 static int trace_empty(struct trace_iterator *iter)
1513 struct trace_array_cpu *data;
1516 for_each_possible_cpu(cpu) {
1517 data = iter->tr->data[cpu];
1519 if (head_page(data) && data->trace_idx &&
1520 (data->trace_tail != data->trace_head ||
1521 data->trace_tail_idx != data->trace_head_idx))
1527 static int print_trace_line(struct trace_iterator *iter)
1529 if (iter->trace && iter->trace->print_line)
1530 return iter->trace->print_line(iter);
1532 if (trace_flags & TRACE_ITER_BIN)
1533 return print_bin_fmt(iter);
1535 if (trace_flags & TRACE_ITER_HEX)
1536 return print_hex_fmt(iter);
1538 if (trace_flags & TRACE_ITER_RAW)
1539 return print_raw_fmt(iter);
1541 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1542 return print_lat_fmt(iter, iter->idx, iter->cpu);
1544 return print_trace_fmt(iter);
1547 static int s_show(struct seq_file *m, void *v)
1549 struct trace_iterator *iter = v;
1551 if (iter->ent == NULL) {
1553 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1556 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1557 /* print nothing if the buffers are empty */
1558 if (trace_empty(iter))
1560 print_trace_header(m, iter);
1561 if (!(trace_flags & TRACE_ITER_VERBOSE))
1562 print_lat_help_header(m);
1564 if (!(trace_flags & TRACE_ITER_VERBOSE))
1565 print_func_help_header(m);
1568 print_trace_line(iter);
1569 trace_print_seq(m, &iter->seq);
1575 static struct seq_operations tracer_seq_ops = {
1582 static struct trace_iterator *
1583 __tracing_open(struct inode *inode, struct file *file, int *ret)
1585 struct trace_iterator *iter;
1587 if (tracing_disabled) {
1592 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1598 mutex_lock(&trace_types_lock);
1599 if (current_trace && current_trace->print_max)
1602 iter->tr = inode->i_private;
1603 iter->trace = current_trace;
1606 /* TODO stop tracer */
1607 *ret = seq_open(file, &tracer_seq_ops);
1609 struct seq_file *m = file->private_data;
1612 /* stop the trace while dumping */
1616 if (iter->trace && iter->trace->open)
1617 iter->trace->open(iter);
1622 mutex_unlock(&trace_types_lock);
1628 int tracing_open_generic(struct inode *inode, struct file *filp)
1630 if (tracing_disabled)
1633 filp->private_data = inode->i_private;
1637 int tracing_release(struct inode *inode, struct file *file)
1639 struct seq_file *m = (struct seq_file *)file->private_data;
1640 struct trace_iterator *iter = m->private;
1642 mutex_lock(&trace_types_lock);
1643 if (iter->trace && iter->trace->close)
1644 iter->trace->close(iter);
1646 /* reenable tracing if it was previously enabled */
1649 mutex_unlock(&trace_types_lock);
1651 seq_release(inode, file);
1656 static int tracing_open(struct inode *inode, struct file *file)
1660 __tracing_open(inode, file, &ret);
1665 static int tracing_lt_open(struct inode *inode, struct file *file)
1667 struct trace_iterator *iter;
1670 iter = __tracing_open(inode, file, &ret);
1673 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1680 t_next(struct seq_file *m, void *v, loff_t *pos)
1682 struct tracer *t = m->private;
1694 static void *t_start(struct seq_file *m, loff_t *pos)
1696 struct tracer *t = m->private;
1699 mutex_lock(&trace_types_lock);
1700 for (; t && l < *pos; t = t_next(m, t, &l))
1706 static void t_stop(struct seq_file *m, void *p)
1708 mutex_unlock(&trace_types_lock);
1711 static int t_show(struct seq_file *m, void *v)
1713 struct tracer *t = v;
1718 seq_printf(m, "%s", t->name);
1727 static struct seq_operations show_traces_seq_ops = {
1734 static int show_traces_open(struct inode *inode, struct file *file)
1738 if (tracing_disabled)
1741 ret = seq_open(file, &show_traces_seq_ops);
1743 struct seq_file *m = file->private_data;
1744 m->private = trace_types;
1750 static struct file_operations tracing_fops = {
1751 .open = tracing_open,
1753 .llseek = seq_lseek,
1754 .release = tracing_release,
1757 static struct file_operations tracing_lt_fops = {
1758 .open = tracing_lt_open,
1760 .llseek = seq_lseek,
1761 .release = tracing_release,
1764 static struct file_operations show_traces_fops = {
1765 .open = show_traces_open,
1767 .release = seq_release,
1771 * Only trace on a CPU if the bitmask is set:
1773 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1776 * When tracing/tracing_cpu_mask is modified then this holds
1777 * the new bitmask we are about to install:
1779 static cpumask_t tracing_cpumask_new;
1782 * The tracer itself will not take this lock, but still we want
1783 * to provide a consistent cpumask to user-space:
1785 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1788 * Temporary storage for the character representation of the
1789 * CPU bitmask (and one more byte for the newline):
1791 static char mask_str[NR_CPUS + 1];
1794 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1795 size_t count, loff_t *ppos)
1799 mutex_lock(&tracing_cpumask_update_lock);
1801 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1802 if (count - len < 2) {
1806 len += sprintf(mask_str + len, "\n");
1807 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1810 mutex_unlock(&tracing_cpumask_update_lock);
1816 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1817 size_t count, loff_t *ppos)
1821 mutex_lock(&tracing_cpumask_update_lock);
1822 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1826 raw_local_irq_disable();
1827 __raw_spin_lock(&ftrace_max_lock);
1828 for_each_possible_cpu(cpu) {
1830 * Increase/decrease the disabled counter if we are
1831 * about to flip a bit in the cpumask:
1833 if (cpu_isset(cpu, tracing_cpumask) &&
1834 !cpu_isset(cpu, tracing_cpumask_new)) {
1835 atomic_inc(&global_trace.data[cpu]->disabled);
1837 if (!cpu_isset(cpu, tracing_cpumask) &&
1838 cpu_isset(cpu, tracing_cpumask_new)) {
1839 atomic_dec(&global_trace.data[cpu]->disabled);
1842 __raw_spin_unlock(&ftrace_max_lock);
1843 raw_local_irq_enable();
1845 tracing_cpumask = tracing_cpumask_new;
1847 mutex_unlock(&tracing_cpumask_update_lock);
1852 mutex_unlock(&tracing_cpumask_update_lock);
1857 static struct file_operations tracing_cpumask_fops = {
1858 .open = tracing_open_generic,
1859 .read = tracing_cpumask_read,
1860 .write = tracing_cpumask_write,
1864 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1865 size_t cnt, loff_t *ppos)
1872 /* calulate max size */
1873 for (i = 0; trace_options[i]; i++) {
1874 len += strlen(trace_options[i]);
1875 len += 3; /* "no" and space */
1878 /* +2 for \n and \0 */
1879 buf = kmalloc(len + 2, GFP_KERNEL);
1883 for (i = 0; trace_options[i]; i++) {
1884 if (trace_flags & (1 << i))
1885 r += sprintf(buf + r, "%s ", trace_options[i]);
1887 r += sprintf(buf + r, "no%s ", trace_options[i]);
1890 r += sprintf(buf + r, "\n");
1891 WARN_ON(r >= len + 2);
1893 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1901 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1902 size_t cnt, loff_t *ppos)
1912 if (copy_from_user(&buf, ubuf, cnt))
1917 if (strncmp(buf, "no", 2) == 0) {
1922 for (i = 0; trace_options[i]; i++) {
1923 int len = strlen(trace_options[i]);
1925 if (strncmp(cmp, trace_options[i], len) == 0) {
1927 trace_flags &= ~(1 << i);
1929 trace_flags |= (1 << i);
1934 * If no option could be set, return an error:
1936 if (!trace_options[i])
1944 static struct file_operations tracing_iter_fops = {
1945 .open = tracing_open_generic,
1946 .read = tracing_iter_ctrl_read,
1947 .write = tracing_iter_ctrl_write,
1950 static const char readme_msg[] =
1951 "tracing mini-HOWTO:\n\n"
1953 "# mount -t debugfs nodev /debug\n\n"
1954 "# cat /debug/tracing/available_tracers\n"
1955 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1956 "# cat /debug/tracing/current_tracer\n"
1958 "# echo sched_switch > /debug/tracing/current_tracer\n"
1959 "# cat /debug/tracing/current_tracer\n"
1961 "# cat /debug/tracing/iter_ctrl\n"
1962 "noprint-parent nosym-offset nosym-addr noverbose\n"
1963 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1964 "# echo 1 > /debug/tracing/tracing_enabled\n"
1965 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1966 "echo 0 > /debug/tracing/tracing_enabled\n"
1970 tracing_readme_read(struct file *filp, char __user *ubuf,
1971 size_t cnt, loff_t *ppos)
1973 return simple_read_from_buffer(ubuf, cnt, ppos,
1974 readme_msg, strlen(readme_msg));
1977 static struct file_operations tracing_readme_fops = {
1978 .open = tracing_open_generic,
1979 .read = tracing_readme_read,
1983 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1984 size_t cnt, loff_t *ppos)
1986 struct trace_array *tr = filp->private_data;
1990 r = sprintf(buf, "%ld\n", tr->ctrl);
1991 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1995 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1996 size_t cnt, loff_t *ppos)
1998 struct trace_array *tr = filp->private_data;
2005 if (copy_from_user(&buf, ubuf, cnt))
2010 val = simple_strtoul(buf, NULL, 10);
2014 mutex_lock(&trace_types_lock);
2015 if (tr->ctrl ^ val) {
2023 if (current_trace && current_trace->ctrl_update)
2024 current_trace->ctrl_update(tr);
2026 mutex_unlock(&trace_types_lock);
2034 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2035 size_t cnt, loff_t *ppos)
2037 char buf[max_tracer_type_len+2];
2040 mutex_lock(&trace_types_lock);
2042 r = sprintf(buf, "%s\n", current_trace->name);
2044 r = sprintf(buf, "\n");
2045 mutex_unlock(&trace_types_lock);
2047 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2051 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2052 size_t cnt, loff_t *ppos)
2054 struct trace_array *tr = &global_trace;
2056 char buf[max_tracer_type_len+1];
2059 if (cnt > max_tracer_type_len)
2060 cnt = max_tracer_type_len;
2062 if (copy_from_user(&buf, ubuf, cnt))
2067 /* strip ending whitespace. */
2068 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2071 mutex_lock(&trace_types_lock);
2072 for (t = trace_types; t; t = t->next) {
2073 if (strcmp(t->name, buf) == 0)
2076 if (!t || t == current_trace)
2079 if (current_trace && current_trace->reset)
2080 current_trace->reset(tr);
2087 mutex_unlock(&trace_types_lock);
2095 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2096 size_t cnt, loff_t *ppos)
2098 unsigned long *ptr = filp->private_data;
2102 r = snprintf(buf, 64, "%ld\n",
2103 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2106 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2110 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2111 size_t cnt, loff_t *ppos)
2113 long *ptr = filp->private_data;
2120 if (copy_from_user(&buf, ubuf, cnt))
2125 val = simple_strtoul(buf, NULL, 10);
2132 static atomic_t tracing_reader;
2134 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2136 struct trace_iterator *iter;
2138 if (tracing_disabled)
2141 /* We only allow for reader of the pipe */
2142 if (atomic_inc_return(&tracing_reader) != 1) {
2143 atomic_dec(&tracing_reader);
2147 /* create a buffer to store the information to pass to userspace */
2148 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2152 iter->tr = &global_trace;
2153 iter->trace = current_trace;
2155 filp->private_data = iter;
2160 static int tracing_release_pipe(struct inode *inode, struct file *file)
2162 struct trace_iterator *iter = file->private_data;
2165 atomic_dec(&tracing_reader);
2171 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2173 struct trace_iterator *iter = filp->private_data;
2175 if (trace_flags & TRACE_ITER_BLOCK) {
2177 * Always select as readable when in blocking mode
2179 return POLLIN | POLLRDNORM;
2182 if (!trace_empty(iter))
2183 return POLLIN | POLLRDNORM;
2184 poll_wait(filp, &trace_wait, poll_table);
2185 if (!trace_empty(iter))
2186 return POLLIN | POLLRDNORM;
2196 tracing_read_pipe(struct file *filp, char __user *ubuf,
2197 size_t cnt, loff_t *ppos)
2199 struct trace_iterator *iter = filp->private_data;
2200 struct trace_array_cpu *data;
2201 struct trace_array *tr = iter->tr;
2202 struct tracer *tracer = iter->trace;
2203 static cpumask_t mask;
2205 unsigned long flags;
2206 #ifdef CONFIG_FTRACE
2214 /* return any leftover data */
2215 if (iter->seq.len > start) {
2216 len = iter->seq.len - start;
2219 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2228 trace_seq_reset(&iter->seq);
2231 while (trace_empty(iter)) {
2233 if ((filp->f_flags & O_NONBLOCK))
2237 * This is a make-shift waitqueue. The reason we don't use
2238 * an actual wait queue is because:
2239 * 1) we only ever have one waiter
2240 * 2) the tracing, traces all functions, we don't want
2241 * the overhead of calling wake_up and friends
2242 * (and tracing them too)
2243 * Anyway, this is really very primitive wakeup.
2245 set_current_state(TASK_INTERRUPTIBLE);
2246 iter->tr->waiter = current;
2248 /* sleep for one second, and try again. */
2249 schedule_timeout(HZ);
2251 iter->tr->waiter = NULL;
2253 if (signal_pending(current))
2256 if (iter->trace != current_trace)
2260 * We block until we read something and tracing is disabled.
2261 * We still block if tracing is disabled, but we have never
2262 * read anything. This allows a user to cat this file, and
2263 * then enable tracing. But after we have read something,
2264 * we give an EOF when tracing is again disabled.
2266 * iter->pos will be 0 if we haven't read anything.
2268 if (!tracer_enabled && iter->pos)
2274 /* stop when tracing is finished */
2275 if (trace_empty(iter))
2278 if (cnt >= PAGE_SIZE)
2279 cnt = PAGE_SIZE - 1;
2281 memset(iter, 0, sizeof(*iter));
2283 iter->trace = tracer;
2287 * We need to stop all tracing on all CPUS to read the
2288 * the next buffer. This is a bit expensive, but is
2289 * not done often. We fill all what we can read,
2290 * and then release the locks again.
2294 local_irq_save(flags);
2295 #ifdef CONFIG_FTRACE
2296 ftrace_save = ftrace_enabled;
2300 for_each_possible_cpu(cpu) {
2301 data = iter->tr->data[cpu];
2303 if (!head_page(data) || !data->trace_idx)
2306 atomic_inc(&data->disabled);
2310 for_each_cpu_mask(cpu, mask) {
2311 data = iter->tr->data[cpu];
2312 __raw_spin_lock(&data->lock);
2315 while (find_next_entry_inc(iter) != NULL) {
2316 int len = iter->seq.len;
2318 ret = print_trace_line(iter);
2320 /* don't print partial lines */
2321 iter->seq.len = len;
2325 trace_consume(iter);
2327 if (iter->seq.len >= cnt)
2331 for_each_cpu_mask(cpu, mask) {
2332 data = iter->tr->data[cpu];
2333 __raw_spin_unlock(&data->lock);
2336 for_each_cpu_mask(cpu, mask) {
2337 data = iter->tr->data[cpu];
2338 atomic_dec(&data->disabled);
2340 #ifdef CONFIG_FTRACE
2341 ftrace_enabled = ftrace_save;
2343 local_irq_restore(flags);
2345 /* Now copy what we have to the user */
2346 read = iter->seq.len;
2350 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2352 if (read < iter->seq.len)
2355 trace_seq_reset(&iter->seq);
2364 tracing_entries_read(struct file *filp, char __user *ubuf,
2365 size_t cnt, loff_t *ppos)
2367 struct trace_array *tr = filp->private_data;
2371 r = sprintf(buf, "%lu\n", tr->entries);
2372 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2376 tracing_entries_write(struct file *filp, const char __user *ubuf,
2377 size_t cnt, loff_t *ppos)
2385 if (copy_from_user(&buf, ubuf, cnt))
2390 val = simple_strtoul(buf, NULL, 10);
2392 /* must have at least 1 entry */
2396 mutex_lock(&trace_types_lock);
2398 if (current_trace != &no_tracer) {
2400 pr_info("ftrace: set current_tracer to none"
2401 " before modifying buffer size\n");
2405 if (val > global_trace.entries) {
2406 while (global_trace.entries < val) {
2407 if (trace_alloc_page()) {
2413 /* include the number of entries in val (inc of page entries) */
2414 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2421 max_tr.entries = global_trace.entries;
2422 mutex_unlock(&trace_types_lock);
2427 static struct file_operations tracing_max_lat_fops = {
2428 .open = tracing_open_generic,
2429 .read = tracing_max_lat_read,
2430 .write = tracing_max_lat_write,
2433 static struct file_operations tracing_ctrl_fops = {
2434 .open = tracing_open_generic,
2435 .read = tracing_ctrl_read,
2436 .write = tracing_ctrl_write,
2439 static struct file_operations set_tracer_fops = {
2440 .open = tracing_open_generic,
2441 .read = tracing_set_trace_read,
2442 .write = tracing_set_trace_write,
2445 static struct file_operations tracing_pipe_fops = {
2446 .open = tracing_open_pipe,
2447 .poll = tracing_poll_pipe,
2448 .read = tracing_read_pipe,
2449 .release = tracing_release_pipe,
2452 static struct file_operations tracing_entries_fops = {
2453 .open = tracing_open_generic,
2454 .read = tracing_entries_read,
2455 .write = tracing_entries_write,
2458 #ifdef CONFIG_DYNAMIC_FTRACE
2461 tracing_read_long(struct file *filp, char __user *ubuf,
2462 size_t cnt, loff_t *ppos)
2464 unsigned long *p = filp->private_data;
2468 r = sprintf(buf, "%ld\n", *p);
2470 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2473 static struct file_operations tracing_read_long_fops = {
2474 .open = tracing_open_generic,
2475 .read = tracing_read_long,
2479 static struct dentry *d_tracer;
2481 struct dentry *tracing_init_dentry(void)
2488 d_tracer = debugfs_create_dir("tracing", NULL);
2490 if (!d_tracer && !once) {
2492 pr_warning("Could not create debugfs directory 'tracing'\n");
2499 #ifdef CONFIG_FTRACE_SELFTEST
2500 /* Let selftest have access to static functions in this file */
2501 #include "trace_selftest.c"
2504 static __init void tracer_init_debugfs(void)
2506 struct dentry *d_tracer;
2507 struct dentry *entry;
2509 d_tracer = tracing_init_dentry();
2511 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2512 &global_trace, &tracing_ctrl_fops);
2514 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2516 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2517 NULL, &tracing_iter_fops);
2519 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2521 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2522 NULL, &tracing_cpumask_fops);
2524 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2526 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2527 &global_trace, &tracing_lt_fops);
2529 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2531 entry = debugfs_create_file("trace", 0444, d_tracer,
2532 &global_trace, &tracing_fops);
2534 pr_warning("Could not create debugfs 'trace' entry\n");
2536 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2537 &global_trace, &show_traces_fops);
2539 pr_warning("Could not create debugfs 'trace' entry\n");
2541 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2542 &global_trace, &set_tracer_fops);
2544 pr_warning("Could not create debugfs 'trace' entry\n");
2546 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2547 &tracing_max_latency,
2548 &tracing_max_lat_fops);
2550 pr_warning("Could not create debugfs "
2551 "'tracing_max_latency' entry\n");
2553 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2554 &tracing_thresh, &tracing_max_lat_fops);
2556 pr_warning("Could not create debugfs "
2557 "'tracing_threash' entry\n");
2558 entry = debugfs_create_file("README", 0644, d_tracer,
2559 NULL, &tracing_readme_fops);
2561 pr_warning("Could not create debugfs 'README' entry\n");
2563 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2564 NULL, &tracing_pipe_fops);
2566 pr_warning("Could not create debugfs "
2567 "'tracing_threash' entry\n");
2569 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2570 &global_trace, &tracing_entries_fops);
2572 pr_warning("Could not create debugfs "
2573 "'tracing_threash' entry\n");
2575 #ifdef CONFIG_DYNAMIC_FTRACE
2576 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2577 &ftrace_update_tot_cnt,
2578 &tracing_read_long_fops);
2580 pr_warning("Could not create debugfs "
2581 "'dyn_ftrace_total_info' entry\n");
2585 static int trace_alloc_page(void)
2587 struct trace_array_cpu *data;
2588 struct page *page, *tmp;
2593 /* first allocate a page for each CPU */
2594 for_each_possible_cpu(i) {
2595 array = (void *)__get_free_page(GFP_KERNEL);
2596 if (array == NULL) {
2597 printk(KERN_ERR "tracer: failed to allocate page"
2598 "for trace buffer!\n");
2602 page = virt_to_page(array);
2603 list_add(&page->lru, &pages);
2605 /* Only allocate if we are actually using the max trace */
2606 #ifdef CONFIG_TRACER_MAX_TRACE
2607 array = (void *)__get_free_page(GFP_KERNEL);
2608 if (array == NULL) {
2609 printk(KERN_ERR "tracer: failed to allocate page"
2610 "for trace buffer!\n");
2613 page = virt_to_page(array);
2614 list_add(&page->lru, &pages);
2618 /* Now that we successfully allocate a page per CPU, add them */
2619 for_each_possible_cpu(i) {
2620 data = global_trace.data[i];
2621 page = list_entry(pages.next, struct page, lru);
2622 list_del_init(&page->lru);
2623 list_add_tail(&page->lru, &data->trace_pages);
2626 #ifdef CONFIG_TRACER_MAX_TRACE
2627 data = max_tr.data[i];
2628 page = list_entry(pages.next, struct page, lru);
2629 list_del_init(&page->lru);
2630 list_add_tail(&page->lru, &data->trace_pages);
2634 global_trace.entries += ENTRIES_PER_PAGE;
2639 list_for_each_entry_safe(page, tmp, &pages, lru) {
2640 list_del_init(&page->lru);
2646 static int trace_free_page(void)
2648 struct trace_array_cpu *data;
2650 struct list_head *p;
2654 /* free one page from each buffer */
2655 for_each_possible_cpu(i) {
2656 data = global_trace.data[i];
2657 p = data->trace_pages.next;
2658 if (p == &data->trace_pages) {
2659 /* should never happen */
2661 tracing_disabled = 1;
2665 page = list_entry(p, struct page, lru);
2667 list_del(&page->lru);
2670 tracing_reset(data);
2672 #ifdef CONFIG_TRACER_MAX_TRACE
2673 data = max_tr.data[i];
2674 p = data->trace_pages.next;
2675 if (p == &data->trace_pages) {
2676 /* should never happen */
2678 tracing_disabled = 1;
2682 page = list_entry(p, struct page, lru);
2684 list_del(&page->lru);
2687 tracing_reset(data);
2690 global_trace.entries -= ENTRIES_PER_PAGE;
2695 __init static int tracer_alloc_buffers(void)
2697 struct trace_array_cpu *data;
2704 global_trace.ctrl = tracer_enabled;
2706 /* Allocate the first page for all buffers */
2707 for_each_possible_cpu(i) {
2708 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2709 max_tr.data[i] = &per_cpu(max_data, i);
2711 array = (void *)__get_free_page(GFP_KERNEL);
2712 if (array == NULL) {
2713 printk(KERN_ERR "tracer: failed to allocate page"
2714 "for trace buffer!\n");
2718 /* set the array to the list */
2719 INIT_LIST_HEAD(&data->trace_pages);
2720 page = virt_to_page(array);
2721 list_add(&page->lru, &data->trace_pages);
2722 /* use the LRU flag to differentiate the two buffers */
2725 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2726 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2728 /* Only allocate if we are actually using the max trace */
2729 #ifdef CONFIG_TRACER_MAX_TRACE
2730 array = (void *)__get_free_page(GFP_KERNEL);
2731 if (array == NULL) {
2732 printk(KERN_ERR "tracer: failed to allocate page"
2733 "for trace buffer!\n");
2737 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2738 page = virt_to_page(array);
2739 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2745 * Since we allocate by orders of pages, we may be able to
2748 global_trace.entries = ENTRIES_PER_PAGE;
2751 while (global_trace.entries < trace_nr_entries) {
2752 if (trace_alloc_page())
2756 max_tr.entries = global_trace.entries;
2758 pr_info("tracer: %d pages allocated for %ld",
2759 pages, trace_nr_entries);
2760 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2761 pr_info(" actual entries %ld\n", global_trace.entries);
2763 tracer_init_debugfs();
2765 trace_init_cmdlines();
2767 register_tracer(&no_tracer);
2768 current_trace = &no_tracer;
2770 /* All seems OK, enable tracing */
2771 tracing_disabled = 0;
2776 for (i-- ; i >= 0; i--) {
2777 struct page *page, *tmp;
2778 struct trace_array_cpu *data = global_trace.data[i];
2781 list_for_each_entry_safe(page, tmp,
2782 &data->trace_pages, lru) {
2783 list_del_init(&page->lru);
2788 #ifdef CONFIG_TRACER_MAX_TRACE
2789 data = max_tr.data[i];
2791 list_for_each_entry_safe(page, tmp,
2792 &data->trace_pages, lru) {
2793 list_del_init(&page->lru);
2801 fs_initcall(tracer_alloc_buffers);