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 = {
43 static int trace_alloc_page(void);
44 static int trace_free_page(void);
46 static int tracing_disabled = 1;
49 ns2usecs(cycle_t nsec)
56 cycle_t ftrace_now(int cpu)
58 return cpu_clock(cpu);
61 static struct trace_array global_trace;
63 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
65 static struct trace_array max_tr;
67 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
69 static int tracer_enabled = 1;
70 static unsigned long trace_nr_entries = 65536UL;
72 static struct tracer *trace_types __read_mostly;
73 static struct tracer *current_trace __read_mostly;
74 static int max_tracer_type_len;
76 static DEFINE_MUTEX(trace_types_lock);
77 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
79 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
81 void trace_wake_up(void)
84 * The runqueue_is_locked() can fail, but this is the best we
87 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
91 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
93 static int __init set_nr_entries(char *str)
97 trace_nr_entries = simple_strtoul(str, &str, 0);
100 __setup("trace_entries=", set_nr_entries);
102 unsigned long nsecs_to_usecs(unsigned long nsecs)
107 enum trace_flag_type {
108 TRACE_FLAG_IRQS_OFF = 0x01,
109 TRACE_FLAG_NEED_RESCHED = 0x02,
110 TRACE_FLAG_HARDIRQ = 0x04,
111 TRACE_FLAG_SOFTIRQ = 0x08,
114 #define TRACE_ITER_SYM_MASK \
115 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
117 /* These must match the bit postions above */
118 static const char *trace_options[] = {
132 static raw_spinlock_t ftrace_max_lock =
133 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
136 * Copy the new maximum trace into the separate maximum-trace
137 * structure. (this way the maximum trace is permanently saved,
138 * for later retrieval via /debugfs/tracing/latency_trace)
141 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
143 struct trace_array_cpu *data = tr->data[cpu];
146 max_tr.time_start = data->preempt_timestamp;
148 data = max_tr.data[cpu];
149 data->saved_latency = tracing_max_latency;
151 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
152 data->pid = tsk->pid;
153 data->uid = tsk->uid;
154 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
155 data->policy = tsk->policy;
156 data->rt_priority = tsk->rt_priority;
158 /* record this tasks comm */
159 tracing_record_cmdline(current);
162 void check_pages(struct trace_array_cpu *data)
164 struct page *page, *tmp;
166 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
167 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
169 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
170 BUG_ON(page->lru.next->prev != &page->lru);
171 BUG_ON(page->lru.prev->next != &page->lru);
175 void *head_page(struct trace_array_cpu *data)
180 if (list_empty(&data->trace_pages))
183 page = list_entry(data->trace_pages.next, struct page, lru);
184 BUG_ON(&page->lru == &data->trace_pages);
186 return page_address(page);
190 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
192 int len = (PAGE_SIZE - 1) - s->len;
200 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
203 /* If we can't write it all, don't bother writing anything */
213 trace_seq_puts(struct trace_seq *s, const char *str)
215 int len = strlen(str);
217 if (len > ((PAGE_SIZE - 1) - s->len))
220 memcpy(s->buffer + s->len, str, len);
227 trace_seq_putc(struct trace_seq *s, unsigned char c)
229 if (s->len >= (PAGE_SIZE - 1))
232 s->buffer[s->len++] = c;
238 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
240 if (len > ((PAGE_SIZE - 1) - s->len))
243 memcpy(s->buffer + s->len, mem, len);
250 static const char hex2asc[] = "0123456789abcdef";
253 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
255 unsigned char hex[HEX_CHARS];
256 unsigned char *data = mem;
260 BUG_ON(len >= HEX_CHARS);
263 for (i = 0, j = 0; i < len; i++) {
265 for (i = len-1, j = 0; i >= 0; i--) {
269 hex[j++] = hex2asc[byte & 0x0f];
270 hex[j++] = hex2asc[byte >> 4];
274 return trace_seq_putmem(s, hex, j);
278 trace_seq_reset(struct trace_seq *s)
284 trace_print_seq(struct seq_file *m, struct trace_seq *s)
286 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
289 seq_puts(m, s->buffer);
295 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
297 struct list_head flip_pages;
299 INIT_LIST_HEAD(&flip_pages);
301 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
302 sizeof(struct trace_array_cpu) -
303 offsetof(struct trace_array_cpu, trace_head_idx));
307 list_splice_init(&tr1->trace_pages, &flip_pages);
308 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
309 list_splice_init(&flip_pages, &tr2->trace_pages);
310 BUG_ON(!list_empty(&flip_pages));
316 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
318 struct trace_array_cpu *data;
321 WARN_ON_ONCE(!irqs_disabled());
322 __raw_spin_lock(&ftrace_max_lock);
323 /* clear out all the previous traces */
324 for_each_possible_cpu(i) {
326 flip_trace(max_tr.data[i], data);
330 __update_max_tr(tr, tsk, cpu);
331 __raw_spin_unlock(&ftrace_max_lock);
335 * update_max_tr_single - only copy one trace over, and reset the rest
337 * @tsk - task with the latency
338 * @cpu - the cpu of the buffer to copy.
341 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
343 struct trace_array_cpu *data = tr->data[cpu];
346 WARN_ON_ONCE(!irqs_disabled());
347 __raw_spin_lock(&ftrace_max_lock);
348 for_each_possible_cpu(i)
349 tracing_reset(max_tr.data[i]);
351 flip_trace(max_tr.data[cpu], data);
354 __update_max_tr(tr, tsk, cpu);
355 __raw_spin_unlock(&ftrace_max_lock);
358 int register_tracer(struct tracer *type)
365 pr_info("Tracer must have a name\n");
369 mutex_lock(&trace_types_lock);
370 for (t = trace_types; t; t = t->next) {
371 if (strcmp(type->name, t->name) == 0) {
373 pr_info("Trace %s already registered\n",
380 #ifdef CONFIG_FTRACE_STARTUP_TEST
381 if (type->selftest) {
382 struct tracer *saved_tracer = current_trace;
383 struct trace_array_cpu *data;
384 struct trace_array *tr = &global_trace;
385 int saved_ctrl = tr->ctrl;
388 * Run a selftest on this tracer.
389 * Here we reset the trace buffer, and set the current
390 * tracer to be this tracer. The tracer can then run some
391 * internal tracing to verify that everything is in order.
392 * If we fail, we do not register this tracer.
394 for_each_possible_cpu(i) {
396 if (!head_page(data))
400 current_trace = type;
402 /* the test is responsible for initializing and enabling */
403 pr_info("Testing tracer %s: ", type->name);
404 ret = type->selftest(type, tr);
405 /* the test is responsible for resetting too */
406 current_trace = saved_tracer;
407 tr->ctrl = saved_ctrl;
409 printk(KERN_CONT "FAILED!\n");
412 /* Only reset on passing, to avoid touching corrupted buffers */
413 for_each_possible_cpu(i) {
415 if (!head_page(data))
419 printk(KERN_CONT "PASSED\n");
423 type->next = trace_types;
425 len = strlen(type->name);
426 if (len > max_tracer_type_len)
427 max_tracer_type_len = len;
430 mutex_unlock(&trace_types_lock);
435 void unregister_tracer(struct tracer *type)
440 mutex_lock(&trace_types_lock);
441 for (t = &trace_types; *t; t = &(*t)->next) {
445 pr_info("Trace %s not registered\n", type->name);
450 if (strlen(type->name) != max_tracer_type_len)
453 max_tracer_type_len = 0;
454 for (t = &trace_types; *t; t = &(*t)->next) {
455 len = strlen((*t)->name);
456 if (len > max_tracer_type_len)
457 max_tracer_type_len = len;
460 mutex_unlock(&trace_types_lock);
463 void tracing_reset(struct trace_array_cpu *data)
466 data->trace_head = data->trace_tail = head_page(data);
467 data->trace_head_idx = 0;
468 data->trace_tail_idx = 0;
471 #define SAVED_CMDLINES 128
472 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
473 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
474 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
475 static int cmdline_idx;
476 static DEFINE_SPINLOCK(trace_cmdline_lock);
477 atomic_t trace_record_cmdline_disabled;
479 static void trace_init_cmdlines(void)
481 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
482 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
486 void trace_stop_cmdline_recording(void);
488 static void trace_save_cmdline(struct task_struct *tsk)
493 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
497 * It's not the end of the world if we don't get
498 * the lock, but we also don't want to spin
499 * nor do we want to disable interrupts,
500 * so if we miss here, then better luck next time.
502 if (!spin_trylock(&trace_cmdline_lock))
505 idx = map_pid_to_cmdline[tsk->pid];
506 if (idx >= SAVED_CMDLINES) {
507 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
509 map = map_cmdline_to_pid[idx];
510 if (map <= PID_MAX_DEFAULT)
511 map_pid_to_cmdline[map] = (unsigned)-1;
513 map_pid_to_cmdline[tsk->pid] = idx;
518 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
520 spin_unlock(&trace_cmdline_lock);
523 static char *trace_find_cmdline(int pid)
525 char *cmdline = "<...>";
531 if (pid > PID_MAX_DEFAULT)
534 map = map_pid_to_cmdline[pid];
535 if (map >= SAVED_CMDLINES)
538 cmdline = saved_cmdlines[map];
544 void tracing_record_cmdline(struct task_struct *tsk)
546 if (atomic_read(&trace_record_cmdline_disabled))
549 trace_save_cmdline(tsk);
552 static inline struct list_head *
553 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
556 * Roundrobin - but skip the head (which is not a real page):
559 if (unlikely(next == &data->trace_pages))
561 BUG_ON(next == &data->trace_pages);
567 trace_next_page(struct trace_array_cpu *data, void *addr)
569 struct list_head *next;
572 page = virt_to_page(addr);
574 next = trace_next_list(data, &page->lru);
575 page = list_entry(next, struct page, lru);
577 return page_address(page);
580 static inline struct trace_entry *
581 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
583 unsigned long idx, idx_next;
584 struct trace_entry *entry;
587 idx = data->trace_head_idx;
590 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
592 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
594 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
595 data->trace_head = trace_next_page(data, data->trace_head);
599 if (data->trace_head == data->trace_tail &&
600 idx_next == data->trace_tail_idx) {
602 data->trace_tail_idx++;
603 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
605 trace_next_page(data, data->trace_tail);
606 data->trace_tail_idx = 0;
610 data->trace_head_idx = idx_next;
616 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
618 struct task_struct *tsk = current;
621 pc = preempt_count();
623 entry->preempt_count = pc & 0xff;
624 entry->pid = (tsk) ? tsk->pid : 0;
625 entry->t = ftrace_now(raw_smp_processor_id());
626 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
627 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
628 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
629 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
633 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
634 unsigned long ip, unsigned long parent_ip, unsigned long flags)
636 struct trace_entry *entry;
637 unsigned long irq_flags;
639 raw_local_irq_save(irq_flags);
640 __raw_spin_lock(&data->lock);
641 entry = tracing_get_trace_entry(tr, data);
642 tracing_generic_entry_update(entry, flags);
643 entry->type = TRACE_FN;
645 entry->fn.parent_ip = parent_ip;
646 __raw_spin_unlock(&data->lock);
647 raw_local_irq_restore(irq_flags);
651 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
652 unsigned long ip, unsigned long parent_ip, unsigned long flags)
654 if (likely(!atomic_read(&data->disabled)))
655 trace_function(tr, data, ip, parent_ip, flags);
659 __trace_special(void *__tr, void *__data,
660 unsigned long arg1, unsigned long arg2, unsigned long arg3)
662 struct trace_array_cpu *data = __data;
663 struct trace_array *tr = __tr;
664 struct trace_entry *entry;
665 unsigned long irq_flags;
667 raw_local_irq_save(irq_flags);
668 __raw_spin_lock(&data->lock);
669 entry = tracing_get_trace_entry(tr, data);
670 tracing_generic_entry_update(entry, 0);
671 entry->type = TRACE_SPECIAL;
672 entry->special.arg1 = arg1;
673 entry->special.arg2 = arg2;
674 entry->special.arg3 = arg3;
675 __raw_spin_unlock(&data->lock);
676 raw_local_irq_restore(irq_flags);
681 void __trace_stack(struct trace_array *tr,
682 struct trace_array_cpu *data,
686 struct trace_entry *entry;
687 struct stack_trace trace;
689 if (!(trace_flags & TRACE_ITER_STACKTRACE))
692 entry = tracing_get_trace_entry(tr, data);
693 tracing_generic_entry_update(entry, flags);
694 entry->type = TRACE_STACK;
696 memset(&entry->stack, 0, sizeof(entry->stack));
698 trace.nr_entries = 0;
699 trace.max_entries = FTRACE_STACK_ENTRIES;
701 trace.entries = entry->stack.caller;
703 save_stack_trace(&trace);
707 tracing_sched_switch_trace(struct trace_array *tr,
708 struct trace_array_cpu *data,
709 struct task_struct *prev,
710 struct task_struct *next,
713 struct trace_entry *entry;
714 unsigned long irq_flags;
716 raw_local_irq_save(irq_flags);
717 __raw_spin_lock(&data->lock);
718 entry = tracing_get_trace_entry(tr, data);
719 tracing_generic_entry_update(entry, flags);
720 entry->type = TRACE_CTX;
721 entry->ctx.prev_pid = prev->pid;
722 entry->ctx.prev_prio = prev->prio;
723 entry->ctx.prev_state = prev->state;
724 entry->ctx.next_pid = next->pid;
725 entry->ctx.next_prio = next->prio;
726 entry->ctx.next_state = next->state;
727 __trace_stack(tr, data, flags, 4);
728 __raw_spin_unlock(&data->lock);
729 raw_local_irq_restore(irq_flags);
733 tracing_sched_wakeup_trace(struct trace_array *tr,
734 struct trace_array_cpu *data,
735 struct task_struct *wakee,
736 struct task_struct *curr,
739 struct trace_entry *entry;
740 unsigned long irq_flags;
742 raw_local_irq_save(irq_flags);
743 __raw_spin_lock(&data->lock);
744 entry = tracing_get_trace_entry(tr, data);
745 tracing_generic_entry_update(entry, flags);
746 entry->type = TRACE_WAKE;
747 entry->ctx.prev_pid = curr->pid;
748 entry->ctx.prev_prio = curr->prio;
749 entry->ctx.prev_state = curr->state;
750 entry->ctx.next_pid = wakee->pid;
751 entry->ctx.next_prio = wakee->prio;
752 entry->ctx.next_state = wakee->state;
753 __trace_stack(tr, data, flags, 5);
754 __raw_spin_unlock(&data->lock);
755 raw_local_irq_restore(irq_flags);
762 function_trace_call(unsigned long ip, unsigned long parent_ip)
764 struct trace_array *tr = &global_trace;
765 struct trace_array_cpu *data;
770 if (unlikely(!tracer_enabled))
773 local_irq_save(flags);
774 cpu = raw_smp_processor_id();
775 data = tr->data[cpu];
776 disabled = atomic_inc_return(&data->disabled);
778 if (likely(disabled == 1))
779 trace_function(tr, data, ip, parent_ip, flags);
781 atomic_dec(&data->disabled);
782 local_irq_restore(flags);
785 static struct ftrace_ops trace_ops __read_mostly =
787 .func = function_trace_call,
790 void tracing_start_function_trace(void)
792 register_ftrace_function(&trace_ops);
795 void tracing_stop_function_trace(void)
797 unregister_ftrace_function(&trace_ops);
801 enum trace_file_type {
802 TRACE_FILE_LAT_FMT = 1,
805 static struct trace_entry *
806 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
807 struct trace_iterator *iter, int cpu)
810 struct trace_entry *array;
812 if (iter->next_idx[cpu] >= tr->entries ||
813 iter->next_idx[cpu] >= data->trace_idx ||
814 (data->trace_head == data->trace_tail &&
815 data->trace_head_idx == data->trace_tail_idx))
818 if (!iter->next_page[cpu]) {
819 /* Initialize the iterator for this cpu trace buffer */
820 WARN_ON(!data->trace_tail);
821 page = virt_to_page(data->trace_tail);
822 iter->next_page[cpu] = &page->lru;
823 iter->next_page_idx[cpu] = data->trace_tail_idx;
826 page = list_entry(iter->next_page[cpu], struct page, lru);
827 BUG_ON(&data->trace_pages == &page->lru);
829 array = page_address(page);
831 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
832 return &array[iter->next_page_idx[cpu]];
835 static struct trace_entry *
836 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
838 struct trace_array *tr = iter->tr;
839 struct trace_entry *ent, *next = NULL;
843 for_each_possible_cpu(cpu) {
844 if (!head_page(tr->data[cpu]))
846 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
848 * Pick the entry with the smallest timestamp:
850 if (ent && (!next || ent->t < next->t)) {
862 static void trace_iterator_increment(struct trace_iterator *iter)
865 iter->next_idx[iter->cpu]++;
866 iter->next_page_idx[iter->cpu]++;
868 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
869 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
871 iter->next_page_idx[iter->cpu] = 0;
872 iter->next_page[iter->cpu] =
873 trace_next_list(data, iter->next_page[iter->cpu]);
877 static void trace_consume(struct trace_iterator *iter)
879 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
881 data->trace_tail_idx++;
882 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
883 data->trace_tail = trace_next_page(data, data->trace_tail);
884 data->trace_tail_idx = 0;
887 /* Check if we empty it, then reset the index */
888 if (data->trace_head == data->trace_tail &&
889 data->trace_head_idx == data->trace_tail_idx)
893 static void *find_next_entry_inc(struct trace_iterator *iter)
895 struct trace_entry *next;
898 next = find_next_entry(iter, &next_cpu);
900 iter->prev_ent = iter->ent;
901 iter->prev_cpu = iter->cpu;
904 iter->cpu = next_cpu;
907 trace_iterator_increment(iter);
909 return next ? iter : NULL;
912 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
914 struct trace_iterator *iter = m->private;
915 void *last_ent = iter->ent;
921 /* can't go backwards */
926 ent = find_next_entry_inc(iter);
930 while (ent && iter->idx < i)
931 ent = find_next_entry_inc(iter);
935 if (last_ent && !ent)
936 seq_puts(m, "\n\nvim:ft=help\n");
941 static void *s_start(struct seq_file *m, loff_t *pos)
943 struct trace_iterator *iter = m->private;
948 mutex_lock(&trace_types_lock);
950 if (!current_trace || current_trace != iter->trace) {
951 mutex_unlock(&trace_types_lock);
955 atomic_inc(&trace_record_cmdline_disabled);
957 /* let the tracer grab locks here if needed */
958 if (current_trace->start)
959 current_trace->start(iter);
961 if (*pos != iter->pos) {
965 iter->prev_ent = NULL;
968 for_each_possible_cpu(i) {
969 iter->next_idx[i] = 0;
970 iter->next_page[i] = NULL;
973 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
978 p = s_next(m, p, &l);
984 static void s_stop(struct seq_file *m, void *p)
986 struct trace_iterator *iter = m->private;
988 atomic_dec(&trace_record_cmdline_disabled);
990 /* let the tracer release locks here if needed */
991 if (current_trace && current_trace == iter->trace && iter->trace->stop)
992 iter->trace->stop(iter);
994 mutex_unlock(&trace_types_lock);
998 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1000 #ifdef CONFIG_KALLSYMS
1001 char str[KSYM_SYMBOL_LEN];
1003 kallsyms_lookup(address, NULL, NULL, NULL, str);
1005 return trace_seq_printf(s, fmt, str);
1011 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1012 unsigned long address)
1014 #ifdef CONFIG_KALLSYMS
1015 char str[KSYM_SYMBOL_LEN];
1017 sprint_symbol(str, address);
1018 return trace_seq_printf(s, fmt, str);
1023 #ifndef CONFIG_64BIT
1024 # define IP_FMT "%08lx"
1026 # define IP_FMT "%016lx"
1030 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1035 return trace_seq_printf(s, "0");
1037 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1038 ret = seq_print_sym_offset(s, "%s", ip);
1040 ret = seq_print_sym_short(s, "%s", ip);
1045 if (sym_flags & TRACE_ITER_SYM_ADDR)
1046 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1050 static void print_lat_help_header(struct seq_file *m)
1052 seq_puts(m, "# _------=> CPU# \n");
1053 seq_puts(m, "# / _-----=> irqs-off \n");
1054 seq_puts(m, "# | / _----=> need-resched \n");
1055 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1056 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1057 seq_puts(m, "# |||| / \n");
1058 seq_puts(m, "# ||||| delay \n");
1059 seq_puts(m, "# cmd pid ||||| time | caller \n");
1060 seq_puts(m, "# \\ / ||||| \\ | / \n");
1063 static void print_func_help_header(struct seq_file *m)
1065 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1066 seq_puts(m, "# | | | | |\n");
1071 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1073 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1074 struct trace_array *tr = iter->tr;
1075 struct trace_array_cpu *data = tr->data[tr->cpu];
1076 struct tracer *type = current_trace;
1077 unsigned long total = 0;
1078 unsigned long entries = 0;
1080 const char *name = "preemption";
1085 for_each_possible_cpu(cpu) {
1086 if (head_page(tr->data[cpu])) {
1087 total += tr->data[cpu]->trace_idx;
1088 if (tr->data[cpu]->trace_idx > tr->entries)
1089 entries += tr->entries;
1091 entries += tr->data[cpu]->trace_idx;
1095 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1097 seq_puts(m, "-----------------------------------"
1098 "---------------------------------\n");
1099 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1100 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1101 nsecs_to_usecs(data->saved_latency),
1105 #if defined(CONFIG_PREEMPT_NONE)
1107 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1109 #elif defined(CONFIG_PREEMPT_DESKTOP)
1114 /* These are reserved for later use */
1117 seq_printf(m, " #P:%d)\n", num_online_cpus());
1121 seq_puts(m, " -----------------\n");
1122 seq_printf(m, " | task: %.16s-%d "
1123 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1124 data->comm, data->pid, data->uid, data->nice,
1125 data->policy, data->rt_priority);
1126 seq_puts(m, " -----------------\n");
1128 if (data->critical_start) {
1129 seq_puts(m, " => started at: ");
1130 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1131 trace_print_seq(m, &iter->seq);
1132 seq_puts(m, "\n => ended at: ");
1133 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1134 trace_print_seq(m, &iter->seq);
1142 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1144 int hardirq, softirq;
1147 comm = trace_find_cmdline(entry->pid);
1149 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1150 trace_seq_printf(s, "%d", cpu);
1151 trace_seq_printf(s, "%c%c",
1152 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1153 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1155 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1156 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1157 if (hardirq && softirq) {
1158 trace_seq_putc(s, 'H');
1161 trace_seq_putc(s, 'h');
1164 trace_seq_putc(s, 's');
1166 trace_seq_putc(s, '.');
1170 if (entry->preempt_count)
1171 trace_seq_printf(s, "%x", entry->preempt_count);
1173 trace_seq_puts(s, ".");
1176 unsigned long preempt_mark_thresh = 100;
1179 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1180 unsigned long rel_usecs)
1182 trace_seq_printf(s, " %4lldus", abs_usecs);
1183 if (rel_usecs > preempt_mark_thresh)
1184 trace_seq_puts(s, "!: ");
1185 else if (rel_usecs > 1)
1186 trace_seq_puts(s, "+: ");
1188 trace_seq_puts(s, " : ");
1191 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1194 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1196 struct trace_seq *s = &iter->seq;
1197 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1198 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1199 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1200 struct trace_entry *entry = iter->ent;
1201 unsigned long abs_usecs;
1202 unsigned long rel_usecs;
1210 rel_usecs = ns2usecs(next_entry->t - entry->t);
1211 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1214 comm = trace_find_cmdline(entry->pid);
1215 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1216 " %ld.%03ldms (+%ld.%03ldms): ",
1218 entry->pid, cpu, entry->flags,
1219 entry->preempt_count, trace_idx,
1222 abs_usecs % 1000, rel_usecs/1000,
1225 lat_print_generic(s, entry, cpu);
1226 lat_print_timestamp(s, abs_usecs, rel_usecs);
1228 switch (entry->type) {
1230 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1231 trace_seq_puts(s, " (");
1232 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1233 trace_seq_puts(s, ")\n");
1237 T = entry->ctx.next_state < sizeof(state_to_char) ?
1238 state_to_char[entry->ctx.next_state] : 'X';
1240 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1241 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1242 comm = trace_find_cmdline(entry->ctx.next_pid);
1243 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1244 entry->ctx.prev_pid,
1245 entry->ctx.prev_prio,
1246 S, entry->type == TRACE_CTX ? "==>" : " +",
1247 entry->ctx.next_pid,
1248 entry->ctx.next_prio,
1252 trace_seq_printf(s, "# %ld %ld %ld\n",
1253 entry->special.arg1,
1254 entry->special.arg2,
1255 entry->special.arg3);
1258 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1260 trace_seq_puts(s, " <= ");
1261 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1263 trace_seq_puts(s, "\n");
1266 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1271 static int print_trace_fmt(struct trace_iterator *iter)
1273 struct trace_seq *s = &iter->seq;
1274 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1275 struct trace_entry *entry;
1276 unsigned long usec_rem;
1277 unsigned long long t;
1286 comm = trace_find_cmdline(iter->ent->pid);
1288 t = ns2usecs(entry->t);
1289 usec_rem = do_div(t, 1000000ULL);
1290 secs = (unsigned long)t;
1292 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1295 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1298 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1302 switch (entry->type) {
1304 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1307 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1308 entry->fn.parent_ip) {
1309 ret = trace_seq_printf(s, " <-");
1312 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1317 ret = trace_seq_printf(s, "\n");
1323 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1324 state_to_char[entry->ctx.prev_state] : 'X';
1325 T = entry->ctx.next_state < sizeof(state_to_char) ?
1326 state_to_char[entry->ctx.next_state] : 'X';
1327 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
1328 entry->ctx.prev_pid,
1329 entry->ctx.prev_prio,
1331 entry->type == TRACE_CTX ? "==>" : " +",
1332 entry->ctx.next_pid,
1333 entry->ctx.next_prio,
1339 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1340 entry->special.arg1,
1341 entry->special.arg2,
1342 entry->special.arg3);
1347 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1349 ret = trace_seq_puts(s, " <= ");
1353 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1358 ret = trace_seq_puts(s, "\n");
1366 static int print_raw_fmt(struct trace_iterator *iter)
1368 struct trace_seq *s = &iter->seq;
1369 struct trace_entry *entry;
1375 ret = trace_seq_printf(s, "%d %d %llu ",
1376 entry->pid, iter->cpu, entry->t);
1380 switch (entry->type) {
1382 ret = trace_seq_printf(s, "%x %x\n",
1383 entry->fn.ip, entry->fn.parent_ip);
1389 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1390 state_to_char[entry->ctx.prev_state] : 'X';
1391 T = entry->ctx.next_state < sizeof(state_to_char) ?
1392 state_to_char[entry->ctx.next_state] : 'X';
1393 if (entry->type == TRACE_WAKE)
1395 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
1396 entry->ctx.prev_pid,
1397 entry->ctx.prev_prio,
1399 entry->ctx.next_pid,
1400 entry->ctx.next_prio,
1407 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1408 entry->special.arg1,
1409 entry->special.arg2,
1410 entry->special.arg3);
1418 #define SEQ_PUT_FIELD_RET(s, x) \
1420 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1424 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1426 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1430 static int print_hex_fmt(struct trace_iterator *iter)
1432 struct trace_seq *s = &iter->seq;
1433 unsigned char newline = '\n';
1434 struct trace_entry *entry;
1439 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1440 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1441 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1443 switch (entry->type) {
1445 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1446 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1450 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1451 state_to_char[entry->ctx.prev_state] : 'X';
1452 T = entry->ctx.next_state < sizeof(state_to_char) ?
1453 state_to_char[entry->ctx.next_state] : 'X';
1454 if (entry->type == TRACE_WAKE)
1456 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1457 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1458 SEQ_PUT_HEX_FIELD_RET(s, S);
1459 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1460 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1461 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1462 SEQ_PUT_HEX_FIELD_RET(s, T);
1466 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1467 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1468 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1471 SEQ_PUT_FIELD_RET(s, newline);
1476 static int print_bin_fmt(struct trace_iterator *iter)
1478 struct trace_seq *s = &iter->seq;
1479 struct trace_entry *entry;
1483 SEQ_PUT_FIELD_RET(s, entry->pid);
1484 SEQ_PUT_FIELD_RET(s, entry->cpu);
1485 SEQ_PUT_FIELD_RET(s, entry->t);
1487 switch (entry->type) {
1489 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1490 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1493 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1494 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1495 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1496 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1497 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1498 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
1502 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1503 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1504 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1510 static int trace_empty(struct trace_iterator *iter)
1512 struct trace_array_cpu *data;
1515 for_each_possible_cpu(cpu) {
1516 data = iter->tr->data[cpu];
1518 if (head_page(data) && data->trace_idx &&
1519 (data->trace_tail != data->trace_head ||
1520 data->trace_tail_idx != data->trace_head_idx))
1526 static int print_trace_line(struct trace_iterator *iter)
1528 if (iter->trace && iter->trace->print_line)
1529 return iter->trace->print_line(iter);
1531 if (trace_flags & TRACE_ITER_BIN)
1532 return print_bin_fmt(iter);
1534 if (trace_flags & TRACE_ITER_HEX)
1535 return print_hex_fmt(iter);
1537 if (trace_flags & TRACE_ITER_RAW)
1538 return print_raw_fmt(iter);
1540 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1541 return print_lat_fmt(iter, iter->idx, iter->cpu);
1543 return print_trace_fmt(iter);
1546 static int s_show(struct seq_file *m, void *v)
1548 struct trace_iterator *iter = v;
1550 if (iter->ent == NULL) {
1552 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1555 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1556 /* print nothing if the buffers are empty */
1557 if (trace_empty(iter))
1559 print_trace_header(m, iter);
1560 if (!(trace_flags & TRACE_ITER_VERBOSE))
1561 print_lat_help_header(m);
1563 if (!(trace_flags & TRACE_ITER_VERBOSE))
1564 print_func_help_header(m);
1567 print_trace_line(iter);
1568 trace_print_seq(m, &iter->seq);
1574 static struct seq_operations tracer_seq_ops = {
1581 static struct trace_iterator *
1582 __tracing_open(struct inode *inode, struct file *file, int *ret)
1584 struct trace_iterator *iter;
1586 if (tracing_disabled) {
1591 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1597 mutex_lock(&trace_types_lock);
1598 if (current_trace && current_trace->print_max)
1601 iter->tr = inode->i_private;
1602 iter->trace = current_trace;
1605 /* TODO stop tracer */
1606 *ret = seq_open(file, &tracer_seq_ops);
1608 struct seq_file *m = file->private_data;
1611 /* stop the trace while dumping */
1615 if (iter->trace && iter->trace->open)
1616 iter->trace->open(iter);
1621 mutex_unlock(&trace_types_lock);
1627 int tracing_open_generic(struct inode *inode, struct file *filp)
1629 if (tracing_disabled)
1632 filp->private_data = inode->i_private;
1636 int tracing_release(struct inode *inode, struct file *file)
1638 struct seq_file *m = (struct seq_file *)file->private_data;
1639 struct trace_iterator *iter = m->private;
1641 mutex_lock(&trace_types_lock);
1642 if (iter->trace && iter->trace->close)
1643 iter->trace->close(iter);
1645 /* reenable tracing if it was previously enabled */
1648 mutex_unlock(&trace_types_lock);
1650 seq_release(inode, file);
1655 static int tracing_open(struct inode *inode, struct file *file)
1659 __tracing_open(inode, file, &ret);
1664 static int tracing_lt_open(struct inode *inode, struct file *file)
1666 struct trace_iterator *iter;
1669 iter = __tracing_open(inode, file, &ret);
1672 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1679 t_next(struct seq_file *m, void *v, loff_t *pos)
1681 struct tracer *t = m->private;
1693 static void *t_start(struct seq_file *m, loff_t *pos)
1695 struct tracer *t = m->private;
1698 mutex_lock(&trace_types_lock);
1699 for (; t && l < *pos; t = t_next(m, t, &l))
1705 static void t_stop(struct seq_file *m, void *p)
1707 mutex_unlock(&trace_types_lock);
1710 static int t_show(struct seq_file *m, void *v)
1712 struct tracer *t = v;
1717 seq_printf(m, "%s", t->name);
1726 static struct seq_operations show_traces_seq_ops = {
1733 static int show_traces_open(struct inode *inode, struct file *file)
1737 if (tracing_disabled)
1740 ret = seq_open(file, &show_traces_seq_ops);
1742 struct seq_file *m = file->private_data;
1743 m->private = trace_types;
1749 static struct file_operations tracing_fops = {
1750 .open = tracing_open,
1752 .llseek = seq_lseek,
1753 .release = tracing_release,
1756 static struct file_operations tracing_lt_fops = {
1757 .open = tracing_lt_open,
1759 .llseek = seq_lseek,
1760 .release = tracing_release,
1763 static struct file_operations show_traces_fops = {
1764 .open = show_traces_open,
1766 .release = seq_release,
1770 * Only trace on a CPU if the bitmask is set:
1772 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1775 * When tracing/tracing_cpu_mask is modified then this holds
1776 * the new bitmask we are about to install:
1778 static cpumask_t tracing_cpumask_new;
1781 * The tracer itself will not take this lock, but still we want
1782 * to provide a consistent cpumask to user-space:
1784 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1787 * Temporary storage for the character representation of the
1788 * CPU bitmask (and one more byte for the newline):
1790 static char mask_str[NR_CPUS + 1];
1793 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1794 size_t count, loff_t *ppos)
1798 mutex_lock(&tracing_cpumask_update_lock);
1800 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1801 if (count - len < 2) {
1805 len += sprintf(mask_str + len, "\n");
1806 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1809 mutex_unlock(&tracing_cpumask_update_lock);
1815 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1816 size_t count, loff_t *ppos)
1820 mutex_lock(&tracing_cpumask_update_lock);
1821 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1825 raw_local_irq_disable();
1826 __raw_spin_lock(&ftrace_max_lock);
1827 for_each_possible_cpu(cpu) {
1829 * Increase/decrease the disabled counter if we are
1830 * about to flip a bit in the cpumask:
1832 if (cpu_isset(cpu, tracing_cpumask) &&
1833 !cpu_isset(cpu, tracing_cpumask_new)) {
1834 atomic_inc(&global_trace.data[cpu]->disabled);
1836 if (!cpu_isset(cpu, tracing_cpumask) &&
1837 cpu_isset(cpu, tracing_cpumask_new)) {
1838 atomic_dec(&global_trace.data[cpu]->disabled);
1841 __raw_spin_unlock(&ftrace_max_lock);
1842 raw_local_irq_enable();
1844 tracing_cpumask = tracing_cpumask_new;
1846 mutex_unlock(&tracing_cpumask_update_lock);
1851 mutex_unlock(&tracing_cpumask_update_lock);
1856 static struct file_operations tracing_cpumask_fops = {
1857 .open = tracing_open_generic,
1858 .read = tracing_cpumask_read,
1859 .write = tracing_cpumask_write,
1863 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1864 size_t cnt, loff_t *ppos)
1871 /* calulate max size */
1872 for (i = 0; trace_options[i]; i++) {
1873 len += strlen(trace_options[i]);
1874 len += 3; /* "no" and space */
1877 /* +2 for \n and \0 */
1878 buf = kmalloc(len + 2, GFP_KERNEL);
1882 for (i = 0; trace_options[i]; i++) {
1883 if (trace_flags & (1 << i))
1884 r += sprintf(buf + r, "%s ", trace_options[i]);
1886 r += sprintf(buf + r, "no%s ", trace_options[i]);
1889 r += sprintf(buf + r, "\n");
1890 WARN_ON(r >= len + 2);
1892 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1900 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1901 size_t cnt, loff_t *ppos)
1908 if (cnt >= sizeof(buf))
1911 if (copy_from_user(&buf, ubuf, cnt))
1916 if (strncmp(buf, "no", 2) == 0) {
1921 for (i = 0; trace_options[i]; i++) {
1922 int len = strlen(trace_options[i]);
1924 if (strncmp(cmp, trace_options[i], len) == 0) {
1926 trace_flags &= ~(1 << i);
1928 trace_flags |= (1 << i);
1933 * If no option could be set, return an error:
1935 if (!trace_options[i])
1943 static struct file_operations tracing_iter_fops = {
1944 .open = tracing_open_generic,
1945 .read = tracing_iter_ctrl_read,
1946 .write = tracing_iter_ctrl_write,
1949 static const char readme_msg[] =
1950 "tracing mini-HOWTO:\n\n"
1952 "# mount -t debugfs nodev /debug\n\n"
1953 "# cat /debug/tracing/available_tracers\n"
1954 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1955 "# cat /debug/tracing/current_tracer\n"
1957 "# echo sched_switch > /debug/tracing/current_tracer\n"
1958 "# cat /debug/tracing/current_tracer\n"
1960 "# cat /debug/tracing/iter_ctrl\n"
1961 "noprint-parent nosym-offset nosym-addr noverbose\n"
1962 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1963 "# echo 1 > /debug/tracing/tracing_enabled\n"
1964 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1965 "echo 0 > /debug/tracing/tracing_enabled\n"
1969 tracing_readme_read(struct file *filp, char __user *ubuf,
1970 size_t cnt, loff_t *ppos)
1972 return simple_read_from_buffer(ubuf, cnt, ppos,
1973 readme_msg, strlen(readme_msg));
1976 static struct file_operations tracing_readme_fops = {
1977 .open = tracing_open_generic,
1978 .read = tracing_readme_read,
1982 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1983 size_t cnt, loff_t *ppos)
1985 struct trace_array *tr = filp->private_data;
1989 r = sprintf(buf, "%ld\n", tr->ctrl);
1990 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1994 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1995 size_t cnt, loff_t *ppos)
1997 struct trace_array *tr = filp->private_data;
2001 if (cnt >= sizeof(buf))
2004 if (copy_from_user(&buf, ubuf, cnt))
2009 val = simple_strtoul(buf, NULL, 10);
2013 mutex_lock(&trace_types_lock);
2014 if (tr->ctrl ^ val) {
2022 if (current_trace && current_trace->ctrl_update)
2023 current_trace->ctrl_update(tr);
2025 mutex_unlock(&trace_types_lock);
2033 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2034 size_t cnt, loff_t *ppos)
2036 char buf[max_tracer_type_len+2];
2039 mutex_lock(&trace_types_lock);
2041 r = sprintf(buf, "%s\n", current_trace->name);
2043 r = sprintf(buf, "\n");
2044 mutex_unlock(&trace_types_lock);
2046 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2050 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2051 size_t cnt, loff_t *ppos)
2053 struct trace_array *tr = &global_trace;
2055 char buf[max_tracer_type_len+1];
2058 if (cnt > max_tracer_type_len)
2059 cnt = max_tracer_type_len;
2061 if (copy_from_user(&buf, ubuf, cnt))
2066 /* strip ending whitespace. */
2067 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2070 mutex_lock(&trace_types_lock);
2071 for (t = trace_types; t; t = t->next) {
2072 if (strcmp(t->name, buf) == 0)
2075 if (!t || t == current_trace)
2078 if (current_trace && current_trace->reset)
2079 current_trace->reset(tr);
2086 mutex_unlock(&trace_types_lock);
2094 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2095 size_t cnt, loff_t *ppos)
2097 unsigned long *ptr = filp->private_data;
2101 r = snprintf(buf, sizeof(buf), "%ld\n",
2102 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2103 if (r > sizeof(buf))
2105 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2109 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2110 size_t cnt, loff_t *ppos)
2112 long *ptr = filp->private_data;
2116 if (cnt >= sizeof(buf))
2119 if (copy_from_user(&buf, ubuf, cnt))
2124 val = simple_strtoul(buf, NULL, 10);
2131 static atomic_t tracing_reader;
2133 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2135 struct trace_iterator *iter;
2137 if (tracing_disabled)
2140 /* We only allow for reader of the pipe */
2141 if (atomic_inc_return(&tracing_reader) != 1) {
2142 atomic_dec(&tracing_reader);
2146 /* create a buffer to store the information to pass to userspace */
2147 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2151 iter->tr = &global_trace;
2152 iter->trace = current_trace;
2154 filp->private_data = iter;
2159 static int tracing_release_pipe(struct inode *inode, struct file *file)
2161 struct trace_iterator *iter = file->private_data;
2164 atomic_dec(&tracing_reader);
2170 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2172 struct trace_iterator *iter = filp->private_data;
2174 if (trace_flags & TRACE_ITER_BLOCK) {
2176 * Always select as readable when in blocking mode
2178 return POLLIN | POLLRDNORM;
2180 if (!trace_empty(iter))
2181 return POLLIN | POLLRDNORM;
2182 poll_wait(filp, &trace_wait, poll_table);
2183 if (!trace_empty(iter))
2184 return POLLIN | POLLRDNORM;
2194 tracing_read_pipe(struct file *filp, char __user *ubuf,
2195 size_t cnt, loff_t *ppos)
2197 struct trace_iterator *iter = filp->private_data;
2198 struct trace_array_cpu *data;
2199 struct trace_array *tr = iter->tr;
2200 struct tracer *tracer = iter->trace;
2201 static cpumask_t mask;
2203 unsigned long flags;
2204 #ifdef CONFIG_FTRACE
2212 /* return any leftover data */
2213 if (iter->seq.len > start) {
2214 len = iter->seq.len - start;
2217 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2226 trace_seq_reset(&iter->seq);
2229 while (trace_empty(iter)) {
2231 if ((filp->f_flags & O_NONBLOCK))
2235 * This is a make-shift waitqueue. The reason we don't use
2236 * an actual wait queue is because:
2237 * 1) we only ever have one waiter
2238 * 2) the tracing, traces all functions, we don't want
2239 * the overhead of calling wake_up and friends
2240 * (and tracing them too)
2241 * Anyway, this is really very primitive wakeup.
2243 set_current_state(TASK_INTERRUPTIBLE);
2244 iter->tr->waiter = current;
2246 /* sleep for one second, and try again. */
2247 schedule_timeout(HZ);
2249 iter->tr->waiter = NULL;
2251 if (signal_pending(current))
2254 if (iter->trace != current_trace)
2258 * We block until we read something and tracing is disabled.
2259 * We still block if tracing is disabled, but we have never
2260 * read anything. This allows a user to cat this file, and
2261 * then enable tracing. But after we have read something,
2262 * we give an EOF when tracing is again disabled.
2264 * iter->pos will be 0 if we haven't read anything.
2266 if (!tracer_enabled && iter->pos)
2272 /* stop when tracing is finished */
2273 if (trace_empty(iter))
2276 if (cnt >= PAGE_SIZE)
2277 cnt = PAGE_SIZE - 1;
2279 memset(iter, 0, sizeof(*iter));
2281 iter->trace = tracer;
2285 * We need to stop all tracing on all CPUS to read the
2286 * the next buffer. This is a bit expensive, but is
2287 * not done often. We fill all what we can read,
2288 * and then release the locks again.
2292 local_irq_save(flags);
2293 #ifdef CONFIG_FTRACE
2294 ftrace_save = ftrace_enabled;
2298 for_each_possible_cpu(cpu) {
2299 data = iter->tr->data[cpu];
2301 if (!head_page(data) || !data->trace_idx)
2304 atomic_inc(&data->disabled);
2308 for_each_cpu_mask(cpu, mask) {
2309 data = iter->tr->data[cpu];
2310 __raw_spin_lock(&data->lock);
2313 while (find_next_entry_inc(iter) != NULL) {
2314 int len = iter->seq.len;
2316 ret = print_trace_line(iter);
2318 /* don't print partial lines */
2319 iter->seq.len = len;
2323 trace_consume(iter);
2325 if (iter->seq.len >= cnt)
2329 for_each_cpu_mask(cpu, mask) {
2330 data = iter->tr->data[cpu];
2331 __raw_spin_unlock(&data->lock);
2334 for_each_cpu_mask(cpu, mask) {
2335 data = iter->tr->data[cpu];
2336 atomic_dec(&data->disabled);
2338 #ifdef CONFIG_FTRACE
2339 ftrace_enabled = ftrace_save;
2341 local_irq_restore(flags);
2343 /* Now copy what we have to the user */
2344 read = iter->seq.len;
2348 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2350 if (read < iter->seq.len)
2353 trace_seq_reset(&iter->seq);
2362 tracing_entries_read(struct file *filp, char __user *ubuf,
2363 size_t cnt, loff_t *ppos)
2365 struct trace_array *tr = filp->private_data;
2369 r = sprintf(buf, "%lu\n", tr->entries);
2370 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2374 tracing_entries_write(struct file *filp, const char __user *ubuf,
2375 size_t cnt, loff_t *ppos)
2380 if (cnt >= sizeof(buf))
2383 if (copy_from_user(&buf, ubuf, cnt))
2388 val = simple_strtoul(buf, NULL, 10);
2390 /* must have at least 1 entry */
2394 mutex_lock(&trace_types_lock);
2396 if (current_trace != &no_tracer) {
2398 pr_info("ftrace: set current_tracer to none"
2399 " before modifying buffer size\n");
2403 if (val > global_trace.entries) {
2404 while (global_trace.entries < val) {
2405 if (trace_alloc_page()) {
2411 /* include the number of entries in val (inc of page entries) */
2412 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2419 max_tr.entries = global_trace.entries;
2420 mutex_unlock(&trace_types_lock);
2425 static struct file_operations tracing_max_lat_fops = {
2426 .open = tracing_open_generic,
2427 .read = tracing_max_lat_read,
2428 .write = tracing_max_lat_write,
2431 static struct file_operations tracing_ctrl_fops = {
2432 .open = tracing_open_generic,
2433 .read = tracing_ctrl_read,
2434 .write = tracing_ctrl_write,
2437 static struct file_operations set_tracer_fops = {
2438 .open = tracing_open_generic,
2439 .read = tracing_set_trace_read,
2440 .write = tracing_set_trace_write,
2443 static struct file_operations tracing_pipe_fops = {
2444 .open = tracing_open_pipe,
2445 .poll = tracing_poll_pipe,
2446 .read = tracing_read_pipe,
2447 .release = tracing_release_pipe,
2450 static struct file_operations tracing_entries_fops = {
2451 .open = tracing_open_generic,
2452 .read = tracing_entries_read,
2453 .write = tracing_entries_write,
2456 #ifdef CONFIG_DYNAMIC_FTRACE
2459 tracing_read_long(struct file *filp, char __user *ubuf,
2460 size_t cnt, loff_t *ppos)
2462 unsigned long *p = filp->private_data;
2466 r = sprintf(buf, "%ld\n", *p);
2468 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2471 static struct file_operations tracing_read_long_fops = {
2472 .open = tracing_open_generic,
2473 .read = tracing_read_long,
2477 static struct dentry *d_tracer;
2479 struct dentry *tracing_init_dentry(void)
2486 d_tracer = debugfs_create_dir("tracing", NULL);
2488 if (!d_tracer && !once) {
2490 pr_warning("Could not create debugfs directory 'tracing'\n");
2497 #ifdef CONFIG_FTRACE_SELFTEST
2498 /* Let selftest have access to static functions in this file */
2499 #include "trace_selftest.c"
2502 static __init void tracer_init_debugfs(void)
2504 struct dentry *d_tracer;
2505 struct dentry *entry;
2507 d_tracer = tracing_init_dentry();
2509 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2510 &global_trace, &tracing_ctrl_fops);
2512 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2514 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2515 NULL, &tracing_iter_fops);
2517 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2519 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2520 NULL, &tracing_cpumask_fops);
2522 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2524 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2525 &global_trace, &tracing_lt_fops);
2527 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2529 entry = debugfs_create_file("trace", 0444, d_tracer,
2530 &global_trace, &tracing_fops);
2532 pr_warning("Could not create debugfs 'trace' entry\n");
2534 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2535 &global_trace, &show_traces_fops);
2537 pr_warning("Could not create debugfs 'trace' entry\n");
2539 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2540 &global_trace, &set_tracer_fops);
2542 pr_warning("Could not create debugfs 'trace' entry\n");
2544 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2545 &tracing_max_latency,
2546 &tracing_max_lat_fops);
2548 pr_warning("Could not create debugfs "
2549 "'tracing_max_latency' entry\n");
2551 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2552 &tracing_thresh, &tracing_max_lat_fops);
2554 pr_warning("Could not create debugfs "
2555 "'tracing_threash' entry\n");
2556 entry = debugfs_create_file("README", 0644, d_tracer,
2557 NULL, &tracing_readme_fops);
2559 pr_warning("Could not create debugfs 'README' entry\n");
2561 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2562 NULL, &tracing_pipe_fops);
2564 pr_warning("Could not create debugfs "
2565 "'tracing_threash' entry\n");
2567 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2568 &global_trace, &tracing_entries_fops);
2570 pr_warning("Could not create debugfs "
2571 "'tracing_threash' entry\n");
2573 #ifdef CONFIG_DYNAMIC_FTRACE
2574 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2575 &ftrace_update_tot_cnt,
2576 &tracing_read_long_fops);
2578 pr_warning("Could not create debugfs "
2579 "'dyn_ftrace_total_info' entry\n");
2583 static int trace_alloc_page(void)
2585 struct trace_array_cpu *data;
2586 struct page *page, *tmp;
2591 /* first allocate a page for each CPU */
2592 for_each_possible_cpu(i) {
2593 array = (void *)__get_free_page(GFP_KERNEL);
2594 if (array == NULL) {
2595 printk(KERN_ERR "tracer: failed to allocate page"
2596 "for trace buffer!\n");
2600 page = virt_to_page(array);
2601 list_add(&page->lru, &pages);
2603 /* Only allocate if we are actually using the max trace */
2604 #ifdef CONFIG_TRACER_MAX_TRACE
2605 array = (void *)__get_free_page(GFP_KERNEL);
2606 if (array == NULL) {
2607 printk(KERN_ERR "tracer: failed to allocate page"
2608 "for trace buffer!\n");
2611 page = virt_to_page(array);
2612 list_add(&page->lru, &pages);
2616 /* Now that we successfully allocate a page per CPU, add them */
2617 for_each_possible_cpu(i) {
2618 data = global_trace.data[i];
2619 page = list_entry(pages.next, struct page, lru);
2620 list_del_init(&page->lru);
2621 list_add_tail(&page->lru, &data->trace_pages);
2624 #ifdef CONFIG_TRACER_MAX_TRACE
2625 data = max_tr.data[i];
2626 page = list_entry(pages.next, struct page, lru);
2627 list_del_init(&page->lru);
2628 list_add_tail(&page->lru, &data->trace_pages);
2632 global_trace.entries += ENTRIES_PER_PAGE;
2637 list_for_each_entry_safe(page, tmp, &pages, lru) {
2638 list_del_init(&page->lru);
2644 static int trace_free_page(void)
2646 struct trace_array_cpu *data;
2648 struct list_head *p;
2652 /* free one page from each buffer */
2653 for_each_possible_cpu(i) {
2654 data = global_trace.data[i];
2655 p = data->trace_pages.next;
2656 if (p == &data->trace_pages) {
2657 /* should never happen */
2659 tracing_disabled = 1;
2663 page = list_entry(p, struct page, lru);
2665 list_del(&page->lru);
2668 tracing_reset(data);
2670 #ifdef CONFIG_TRACER_MAX_TRACE
2671 data = max_tr.data[i];
2672 p = data->trace_pages.next;
2673 if (p == &data->trace_pages) {
2674 /* should never happen */
2676 tracing_disabled = 1;
2680 page = list_entry(p, struct page, lru);
2682 list_del(&page->lru);
2685 tracing_reset(data);
2688 global_trace.entries -= ENTRIES_PER_PAGE;
2693 __init static int tracer_alloc_buffers(void)
2695 struct trace_array_cpu *data;
2702 global_trace.ctrl = tracer_enabled;
2704 /* Allocate the first page for all buffers */
2705 for_each_possible_cpu(i) {
2706 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2707 max_tr.data[i] = &per_cpu(max_data, i);
2709 array = (void *)__get_free_page(GFP_KERNEL);
2710 if (array == NULL) {
2711 printk(KERN_ERR "tracer: failed to allocate page"
2712 "for trace buffer!\n");
2716 /* set the array to the list */
2717 INIT_LIST_HEAD(&data->trace_pages);
2718 page = virt_to_page(array);
2719 list_add(&page->lru, &data->trace_pages);
2720 /* use the LRU flag to differentiate the two buffers */
2723 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2724 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2726 /* Only allocate if we are actually using the max trace */
2727 #ifdef CONFIG_TRACER_MAX_TRACE
2728 array = (void *)__get_free_page(GFP_KERNEL);
2729 if (array == NULL) {
2730 printk(KERN_ERR "tracer: failed to allocate page"
2731 "for trace buffer!\n");
2735 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2736 page = virt_to_page(array);
2737 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2743 * Since we allocate by orders of pages, we may be able to
2746 global_trace.entries = ENTRIES_PER_PAGE;
2749 while (global_trace.entries < trace_nr_entries) {
2750 if (trace_alloc_page())
2754 max_tr.entries = global_trace.entries;
2756 pr_info("tracer: %d pages allocated for %ld",
2757 pages, trace_nr_entries);
2758 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2759 pr_info(" actual entries %ld\n", global_trace.entries);
2761 tracer_init_debugfs();
2763 trace_init_cmdlines();
2765 register_tracer(&no_tracer);
2766 current_trace = &no_tracer;
2768 /* All seems OK, enable tracing */
2769 tracing_disabled = 0;
2774 for (i-- ; i >= 0; i--) {
2775 struct page *page, *tmp;
2776 struct trace_array_cpu *data = global_trace.data[i];
2779 list_for_each_entry_safe(page, tmp,
2780 &data->trace_pages, lru) {
2781 list_del_init(&page->lru);
2786 #ifdef CONFIG_TRACER_MAX_TRACE
2787 data = max_tr.data[i];
2789 list_for_each_entry_safe(page, tmp,
2790 &data->trace_pages, lru) {
2791 list_del_init(&page->lru);
2799 fs_initcall(tracer_alloc_buffers);