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)
95 unsigned long nr_entries;
100 ret = strict_strtoul(str, 0, &nr_entries);
101 /* nr_entries can not be zero */
102 if (ret < 0 || nr_entries == 0)
104 trace_nr_entries = nr_entries;
107 __setup("trace_entries=", set_nr_entries);
109 unsigned long nsecs_to_usecs(unsigned long nsecs)
114 enum trace_flag_type {
115 TRACE_FLAG_IRQS_OFF = 0x01,
116 TRACE_FLAG_NEED_RESCHED = 0x02,
117 TRACE_FLAG_HARDIRQ = 0x04,
118 TRACE_FLAG_SOFTIRQ = 0x08,
121 #define TRACE_ITER_SYM_MASK \
122 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
124 /* These must match the bit postions above */
125 static const char *trace_options[] = {
139 static raw_spinlock_t ftrace_max_lock =
140 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
143 * Copy the new maximum trace into the separate maximum-trace
144 * structure. (this way the maximum trace is permanently saved,
145 * for later retrieval via /debugfs/tracing/latency_trace)
148 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
150 struct trace_array_cpu *data = tr->data[cpu];
153 max_tr.time_start = data->preempt_timestamp;
155 data = max_tr.data[cpu];
156 data->saved_latency = tracing_max_latency;
158 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
159 data->pid = tsk->pid;
160 data->uid = tsk->uid;
161 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
162 data->policy = tsk->policy;
163 data->rt_priority = tsk->rt_priority;
165 /* record this tasks comm */
166 tracing_record_cmdline(current);
169 void check_pages(struct trace_array_cpu *data)
171 struct page *page, *tmp;
173 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
174 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
176 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
177 BUG_ON(page->lru.next->prev != &page->lru);
178 BUG_ON(page->lru.prev->next != &page->lru);
182 void *head_page(struct trace_array_cpu *data)
187 if (list_empty(&data->trace_pages))
190 page = list_entry(data->trace_pages.next, struct page, lru);
191 BUG_ON(&page->lru == &data->trace_pages);
193 return page_address(page);
197 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
199 int len = (PAGE_SIZE - 1) - s->len;
207 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
210 /* If we can't write it all, don't bother writing anything */
220 trace_seq_puts(struct trace_seq *s, const char *str)
222 int len = strlen(str);
224 if (len > ((PAGE_SIZE - 1) - s->len))
227 memcpy(s->buffer + s->len, str, len);
234 trace_seq_putc(struct trace_seq *s, unsigned char c)
236 if (s->len >= (PAGE_SIZE - 1))
239 s->buffer[s->len++] = c;
245 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
247 if (len > ((PAGE_SIZE - 1) - s->len))
250 memcpy(s->buffer + s->len, mem, len);
257 static const char hex2asc[] = "0123456789abcdef";
260 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
262 unsigned char hex[HEX_CHARS];
263 unsigned char *data = mem;
267 BUG_ON(len >= HEX_CHARS);
270 for (i = 0, j = 0; i < len; i++) {
272 for (i = len-1, j = 0; i >= 0; i--) {
276 hex[j++] = hex2asc[byte & 0x0f];
277 hex[j++] = hex2asc[byte >> 4];
281 return trace_seq_putmem(s, hex, j);
285 trace_seq_reset(struct trace_seq *s)
291 trace_print_seq(struct seq_file *m, struct trace_seq *s)
293 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
296 seq_puts(m, s->buffer);
302 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
304 struct list_head flip_pages;
306 INIT_LIST_HEAD(&flip_pages);
308 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
309 sizeof(struct trace_array_cpu) -
310 offsetof(struct trace_array_cpu, trace_head_idx));
314 list_splice_init(&tr1->trace_pages, &flip_pages);
315 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
316 list_splice_init(&flip_pages, &tr2->trace_pages);
317 BUG_ON(!list_empty(&flip_pages));
323 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
325 struct trace_array_cpu *data;
328 WARN_ON_ONCE(!irqs_disabled());
329 __raw_spin_lock(&ftrace_max_lock);
330 /* clear out all the previous traces */
331 for_each_possible_cpu(i) {
333 flip_trace(max_tr.data[i], data);
337 __update_max_tr(tr, tsk, cpu);
338 __raw_spin_unlock(&ftrace_max_lock);
342 * update_max_tr_single - only copy one trace over, and reset the rest
344 * @tsk - task with the latency
345 * @cpu - the cpu of the buffer to copy.
348 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
350 struct trace_array_cpu *data = tr->data[cpu];
353 WARN_ON_ONCE(!irqs_disabled());
354 __raw_spin_lock(&ftrace_max_lock);
355 for_each_possible_cpu(i)
356 tracing_reset(max_tr.data[i]);
358 flip_trace(max_tr.data[cpu], data);
361 __update_max_tr(tr, tsk, cpu);
362 __raw_spin_unlock(&ftrace_max_lock);
365 int register_tracer(struct tracer *type)
372 pr_info("Tracer must have a name\n");
376 mutex_lock(&trace_types_lock);
377 for (t = trace_types; t; t = t->next) {
378 if (strcmp(type->name, t->name) == 0) {
380 pr_info("Trace %s already registered\n",
387 #ifdef CONFIG_FTRACE_STARTUP_TEST
388 if (type->selftest) {
389 struct tracer *saved_tracer = current_trace;
390 struct trace_array_cpu *data;
391 struct trace_array *tr = &global_trace;
392 int saved_ctrl = tr->ctrl;
395 * Run a selftest on this tracer.
396 * Here we reset the trace buffer, and set the current
397 * tracer to be this tracer. The tracer can then run some
398 * internal tracing to verify that everything is in order.
399 * If we fail, we do not register this tracer.
401 for_each_possible_cpu(i) {
403 if (!head_page(data))
407 current_trace = type;
409 /* the test is responsible for initializing and enabling */
410 pr_info("Testing tracer %s: ", type->name);
411 ret = type->selftest(type, tr);
412 /* the test is responsible for resetting too */
413 current_trace = saved_tracer;
414 tr->ctrl = saved_ctrl;
416 printk(KERN_CONT "FAILED!\n");
419 /* Only reset on passing, to avoid touching corrupted buffers */
420 for_each_possible_cpu(i) {
422 if (!head_page(data))
426 printk(KERN_CONT "PASSED\n");
430 type->next = trace_types;
432 len = strlen(type->name);
433 if (len > max_tracer_type_len)
434 max_tracer_type_len = len;
437 mutex_unlock(&trace_types_lock);
442 void unregister_tracer(struct tracer *type)
447 mutex_lock(&trace_types_lock);
448 for (t = &trace_types; *t; t = &(*t)->next) {
452 pr_info("Trace %s not registered\n", type->name);
457 if (strlen(type->name) != max_tracer_type_len)
460 max_tracer_type_len = 0;
461 for (t = &trace_types; *t; t = &(*t)->next) {
462 len = strlen((*t)->name);
463 if (len > max_tracer_type_len)
464 max_tracer_type_len = len;
467 mutex_unlock(&trace_types_lock);
470 void tracing_reset(struct trace_array_cpu *data)
473 data->trace_head = data->trace_tail = head_page(data);
474 data->trace_head_idx = 0;
475 data->trace_tail_idx = 0;
478 #define SAVED_CMDLINES 128
479 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
480 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
481 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
482 static int cmdline_idx;
483 static DEFINE_SPINLOCK(trace_cmdline_lock);
484 atomic_t trace_record_cmdline_disabled;
486 static void trace_init_cmdlines(void)
488 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
489 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
493 void trace_stop_cmdline_recording(void);
495 static void trace_save_cmdline(struct task_struct *tsk)
500 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
504 * It's not the end of the world if we don't get
505 * the lock, but we also don't want to spin
506 * nor do we want to disable interrupts,
507 * so if we miss here, then better luck next time.
509 if (!spin_trylock(&trace_cmdline_lock))
512 idx = map_pid_to_cmdline[tsk->pid];
513 if (idx >= SAVED_CMDLINES) {
514 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
516 map = map_cmdline_to_pid[idx];
517 if (map <= PID_MAX_DEFAULT)
518 map_pid_to_cmdline[map] = (unsigned)-1;
520 map_pid_to_cmdline[tsk->pid] = idx;
525 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
527 spin_unlock(&trace_cmdline_lock);
530 static char *trace_find_cmdline(int pid)
532 char *cmdline = "<...>";
538 if (pid > PID_MAX_DEFAULT)
541 map = map_pid_to_cmdline[pid];
542 if (map >= SAVED_CMDLINES)
545 cmdline = saved_cmdlines[map];
551 void tracing_record_cmdline(struct task_struct *tsk)
553 if (atomic_read(&trace_record_cmdline_disabled))
556 trace_save_cmdline(tsk);
559 static inline struct list_head *
560 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
563 * Roundrobin - but skip the head (which is not a real page):
566 if (unlikely(next == &data->trace_pages))
568 BUG_ON(next == &data->trace_pages);
574 trace_next_page(struct trace_array_cpu *data, void *addr)
576 struct list_head *next;
579 page = virt_to_page(addr);
581 next = trace_next_list(data, &page->lru);
582 page = list_entry(next, struct page, lru);
584 return page_address(page);
587 static inline struct trace_entry *
588 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
590 unsigned long idx, idx_next;
591 struct trace_entry *entry;
594 idx = data->trace_head_idx;
597 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
599 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
601 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
602 data->trace_head = trace_next_page(data, data->trace_head);
606 if (data->trace_head == data->trace_tail &&
607 idx_next == data->trace_tail_idx) {
609 data->trace_tail_idx++;
610 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
612 trace_next_page(data, data->trace_tail);
613 data->trace_tail_idx = 0;
617 data->trace_head_idx = idx_next;
623 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
625 struct task_struct *tsk = current;
628 pc = preempt_count();
630 entry->preempt_count = pc & 0xff;
631 entry->pid = (tsk) ? tsk->pid : 0;
632 entry->t = ftrace_now(raw_smp_processor_id());
633 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
634 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
635 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
636 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
640 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
641 unsigned long ip, unsigned long parent_ip, unsigned long flags)
643 struct trace_entry *entry;
644 unsigned long irq_flags;
646 raw_local_irq_save(irq_flags);
647 __raw_spin_lock(&data->lock);
648 entry = tracing_get_trace_entry(tr, data);
649 tracing_generic_entry_update(entry, flags);
650 entry->type = TRACE_FN;
652 entry->fn.parent_ip = parent_ip;
653 __raw_spin_unlock(&data->lock);
654 raw_local_irq_restore(irq_flags);
658 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
659 unsigned long ip, unsigned long parent_ip, unsigned long flags)
661 if (likely(!atomic_read(&data->disabled)))
662 trace_function(tr, data, ip, parent_ip, flags);
666 __trace_special(void *__tr, void *__data,
667 unsigned long arg1, unsigned long arg2, unsigned long arg3)
669 struct trace_array_cpu *data = __data;
670 struct trace_array *tr = __tr;
671 struct trace_entry *entry;
672 unsigned long irq_flags;
674 raw_local_irq_save(irq_flags);
675 __raw_spin_lock(&data->lock);
676 entry = tracing_get_trace_entry(tr, data);
677 tracing_generic_entry_update(entry, 0);
678 entry->type = TRACE_SPECIAL;
679 entry->special.arg1 = arg1;
680 entry->special.arg2 = arg2;
681 entry->special.arg3 = arg3;
682 __raw_spin_unlock(&data->lock);
683 raw_local_irq_restore(irq_flags);
688 void __trace_stack(struct trace_array *tr,
689 struct trace_array_cpu *data,
693 struct trace_entry *entry;
694 struct stack_trace trace;
696 if (!(trace_flags & TRACE_ITER_STACKTRACE))
699 entry = tracing_get_trace_entry(tr, data);
700 tracing_generic_entry_update(entry, flags);
701 entry->type = TRACE_STACK;
703 memset(&entry->stack, 0, sizeof(entry->stack));
705 trace.nr_entries = 0;
706 trace.max_entries = FTRACE_STACK_ENTRIES;
708 trace.entries = entry->stack.caller;
710 save_stack_trace(&trace);
714 tracing_sched_switch_trace(struct trace_array *tr,
715 struct trace_array_cpu *data,
716 struct task_struct *prev,
717 struct task_struct *next,
720 struct trace_entry *entry;
721 unsigned long irq_flags;
723 raw_local_irq_save(irq_flags);
724 __raw_spin_lock(&data->lock);
725 entry = tracing_get_trace_entry(tr, data);
726 tracing_generic_entry_update(entry, flags);
727 entry->type = TRACE_CTX;
728 entry->ctx.prev_pid = prev->pid;
729 entry->ctx.prev_prio = prev->prio;
730 entry->ctx.prev_state = prev->state;
731 entry->ctx.next_pid = next->pid;
732 entry->ctx.next_prio = next->prio;
733 entry->ctx.next_state = next->state;
734 __trace_stack(tr, data, flags, 4);
735 __raw_spin_unlock(&data->lock);
736 raw_local_irq_restore(irq_flags);
740 tracing_sched_wakeup_trace(struct trace_array *tr,
741 struct trace_array_cpu *data,
742 struct task_struct *wakee,
743 struct task_struct *curr,
746 struct trace_entry *entry;
747 unsigned long irq_flags;
749 raw_local_irq_save(irq_flags);
750 __raw_spin_lock(&data->lock);
751 entry = tracing_get_trace_entry(tr, data);
752 tracing_generic_entry_update(entry, flags);
753 entry->type = TRACE_WAKE;
754 entry->ctx.prev_pid = curr->pid;
755 entry->ctx.prev_prio = curr->prio;
756 entry->ctx.prev_state = curr->state;
757 entry->ctx.next_pid = wakee->pid;
758 entry->ctx.next_prio = wakee->prio;
759 entry->ctx.next_state = wakee->state;
760 __trace_stack(tr, data, flags, 5);
761 __raw_spin_unlock(&data->lock);
762 raw_local_irq_restore(irq_flags);
769 function_trace_call(unsigned long ip, unsigned long parent_ip)
771 struct trace_array *tr = &global_trace;
772 struct trace_array_cpu *data;
777 if (unlikely(!tracer_enabled))
780 local_irq_save(flags);
781 cpu = raw_smp_processor_id();
782 data = tr->data[cpu];
783 disabled = atomic_inc_return(&data->disabled);
785 if (likely(disabled == 1))
786 trace_function(tr, data, ip, parent_ip, flags);
788 atomic_dec(&data->disabled);
789 local_irq_restore(flags);
792 static struct ftrace_ops trace_ops __read_mostly =
794 .func = function_trace_call,
797 void tracing_start_function_trace(void)
799 register_ftrace_function(&trace_ops);
802 void tracing_stop_function_trace(void)
804 unregister_ftrace_function(&trace_ops);
808 enum trace_file_type {
809 TRACE_FILE_LAT_FMT = 1,
812 static struct trace_entry *
813 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
814 struct trace_iterator *iter, int cpu)
817 struct trace_entry *array;
819 if (iter->next_idx[cpu] >= tr->entries ||
820 iter->next_idx[cpu] >= data->trace_idx ||
821 (data->trace_head == data->trace_tail &&
822 data->trace_head_idx == data->trace_tail_idx))
825 if (!iter->next_page[cpu]) {
826 /* Initialize the iterator for this cpu trace buffer */
827 WARN_ON(!data->trace_tail);
828 page = virt_to_page(data->trace_tail);
829 iter->next_page[cpu] = &page->lru;
830 iter->next_page_idx[cpu] = data->trace_tail_idx;
833 page = list_entry(iter->next_page[cpu], struct page, lru);
834 BUG_ON(&data->trace_pages == &page->lru);
836 array = page_address(page);
838 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
839 return &array[iter->next_page_idx[cpu]];
842 static struct trace_entry *
843 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
845 struct trace_array *tr = iter->tr;
846 struct trace_entry *ent, *next = NULL;
850 for_each_possible_cpu(cpu) {
851 if (!head_page(tr->data[cpu]))
853 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
855 * Pick the entry with the smallest timestamp:
857 if (ent && (!next || ent->t < next->t)) {
869 static void trace_iterator_increment(struct trace_iterator *iter)
872 iter->next_idx[iter->cpu]++;
873 iter->next_page_idx[iter->cpu]++;
875 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
876 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
878 iter->next_page_idx[iter->cpu] = 0;
879 iter->next_page[iter->cpu] =
880 trace_next_list(data, iter->next_page[iter->cpu]);
884 static void trace_consume(struct trace_iterator *iter)
886 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
888 data->trace_tail_idx++;
889 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
890 data->trace_tail = trace_next_page(data, data->trace_tail);
891 data->trace_tail_idx = 0;
894 /* Check if we empty it, then reset the index */
895 if (data->trace_head == data->trace_tail &&
896 data->trace_head_idx == data->trace_tail_idx)
900 static void *find_next_entry_inc(struct trace_iterator *iter)
902 struct trace_entry *next;
905 next = find_next_entry(iter, &next_cpu);
907 iter->prev_ent = iter->ent;
908 iter->prev_cpu = iter->cpu;
911 iter->cpu = next_cpu;
914 trace_iterator_increment(iter);
916 return next ? iter : NULL;
919 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
921 struct trace_iterator *iter = m->private;
922 void *last_ent = iter->ent;
928 /* can't go backwards */
933 ent = find_next_entry_inc(iter);
937 while (ent && iter->idx < i)
938 ent = find_next_entry_inc(iter);
942 if (last_ent && !ent)
943 seq_puts(m, "\n\nvim:ft=help\n");
948 static void *s_start(struct seq_file *m, loff_t *pos)
950 struct trace_iterator *iter = m->private;
955 mutex_lock(&trace_types_lock);
957 if (!current_trace || current_trace != iter->trace) {
958 mutex_unlock(&trace_types_lock);
962 atomic_inc(&trace_record_cmdline_disabled);
964 /* let the tracer grab locks here if needed */
965 if (current_trace->start)
966 current_trace->start(iter);
968 if (*pos != iter->pos) {
972 iter->prev_ent = NULL;
975 for_each_possible_cpu(i) {
976 iter->next_idx[i] = 0;
977 iter->next_page[i] = NULL;
980 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
985 p = s_next(m, p, &l);
991 static void s_stop(struct seq_file *m, void *p)
993 struct trace_iterator *iter = m->private;
995 atomic_dec(&trace_record_cmdline_disabled);
997 /* let the tracer release locks here if needed */
998 if (current_trace && current_trace == iter->trace && iter->trace->stop)
999 iter->trace->stop(iter);
1001 mutex_unlock(&trace_types_lock);
1005 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1007 #ifdef CONFIG_KALLSYMS
1008 char str[KSYM_SYMBOL_LEN];
1010 kallsyms_lookup(address, NULL, NULL, NULL, str);
1012 return trace_seq_printf(s, fmt, str);
1018 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1019 unsigned long address)
1021 #ifdef CONFIG_KALLSYMS
1022 char str[KSYM_SYMBOL_LEN];
1024 sprint_symbol(str, address);
1025 return trace_seq_printf(s, fmt, str);
1030 #ifndef CONFIG_64BIT
1031 # define IP_FMT "%08lx"
1033 # define IP_FMT "%016lx"
1037 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1042 return trace_seq_printf(s, "0");
1044 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1045 ret = seq_print_sym_offset(s, "%s", ip);
1047 ret = seq_print_sym_short(s, "%s", ip);
1052 if (sym_flags & TRACE_ITER_SYM_ADDR)
1053 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1057 static void print_lat_help_header(struct seq_file *m)
1059 seq_puts(m, "# _------=> CPU# \n");
1060 seq_puts(m, "# / _-----=> irqs-off \n");
1061 seq_puts(m, "# | / _----=> need-resched \n");
1062 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1063 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1064 seq_puts(m, "# |||| / \n");
1065 seq_puts(m, "# ||||| delay \n");
1066 seq_puts(m, "# cmd pid ||||| time | caller \n");
1067 seq_puts(m, "# \\ / ||||| \\ | / \n");
1070 static void print_func_help_header(struct seq_file *m)
1072 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1073 seq_puts(m, "# | | | | |\n");
1078 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1080 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1081 struct trace_array *tr = iter->tr;
1082 struct trace_array_cpu *data = tr->data[tr->cpu];
1083 struct tracer *type = current_trace;
1084 unsigned long total = 0;
1085 unsigned long entries = 0;
1087 const char *name = "preemption";
1092 for_each_possible_cpu(cpu) {
1093 if (head_page(tr->data[cpu])) {
1094 total += tr->data[cpu]->trace_idx;
1095 if (tr->data[cpu]->trace_idx > tr->entries)
1096 entries += tr->entries;
1098 entries += tr->data[cpu]->trace_idx;
1102 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1104 seq_puts(m, "-----------------------------------"
1105 "---------------------------------\n");
1106 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1107 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1108 nsecs_to_usecs(data->saved_latency),
1112 #if defined(CONFIG_PREEMPT_NONE)
1114 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1116 #elif defined(CONFIG_PREEMPT_DESKTOP)
1121 /* These are reserved for later use */
1124 seq_printf(m, " #P:%d)\n", num_online_cpus());
1128 seq_puts(m, " -----------------\n");
1129 seq_printf(m, " | task: %.16s-%d "
1130 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1131 data->comm, data->pid, data->uid, data->nice,
1132 data->policy, data->rt_priority);
1133 seq_puts(m, " -----------------\n");
1135 if (data->critical_start) {
1136 seq_puts(m, " => started at: ");
1137 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1138 trace_print_seq(m, &iter->seq);
1139 seq_puts(m, "\n => ended at: ");
1140 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1141 trace_print_seq(m, &iter->seq);
1149 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1151 int hardirq, softirq;
1154 comm = trace_find_cmdline(entry->pid);
1156 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1157 trace_seq_printf(s, "%d", cpu);
1158 trace_seq_printf(s, "%c%c",
1159 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1160 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1162 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1163 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1164 if (hardirq && softirq) {
1165 trace_seq_putc(s, 'H');
1168 trace_seq_putc(s, 'h');
1171 trace_seq_putc(s, 's');
1173 trace_seq_putc(s, '.');
1177 if (entry->preempt_count)
1178 trace_seq_printf(s, "%x", entry->preempt_count);
1180 trace_seq_puts(s, ".");
1183 unsigned long preempt_mark_thresh = 100;
1186 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1187 unsigned long rel_usecs)
1189 trace_seq_printf(s, " %4lldus", abs_usecs);
1190 if (rel_usecs > preempt_mark_thresh)
1191 trace_seq_puts(s, "!: ");
1192 else if (rel_usecs > 1)
1193 trace_seq_puts(s, "+: ");
1195 trace_seq_puts(s, " : ");
1198 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1201 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1203 struct trace_seq *s = &iter->seq;
1204 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1205 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1206 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1207 struct trace_entry *entry = iter->ent;
1208 unsigned long abs_usecs;
1209 unsigned long rel_usecs;
1217 rel_usecs = ns2usecs(next_entry->t - entry->t);
1218 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1221 comm = trace_find_cmdline(entry->pid);
1222 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1223 " %ld.%03ldms (+%ld.%03ldms): ",
1225 entry->pid, cpu, entry->flags,
1226 entry->preempt_count, trace_idx,
1229 abs_usecs % 1000, rel_usecs/1000,
1232 lat_print_generic(s, entry, cpu);
1233 lat_print_timestamp(s, abs_usecs, rel_usecs);
1235 switch (entry->type) {
1237 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1238 trace_seq_puts(s, " (");
1239 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1240 trace_seq_puts(s, ")\n");
1244 T = entry->ctx.next_state < sizeof(state_to_char) ?
1245 state_to_char[entry->ctx.next_state] : 'X';
1247 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1248 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1249 comm = trace_find_cmdline(entry->ctx.next_pid);
1250 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1251 entry->ctx.prev_pid,
1252 entry->ctx.prev_prio,
1253 S, entry->type == TRACE_CTX ? "==>" : " +",
1254 entry->ctx.next_pid,
1255 entry->ctx.next_prio,
1259 trace_seq_printf(s, "# %ld %ld %ld\n",
1260 entry->special.arg1,
1261 entry->special.arg2,
1262 entry->special.arg3);
1265 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1267 trace_seq_puts(s, " <= ");
1268 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1270 trace_seq_puts(s, "\n");
1273 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1278 static int print_trace_fmt(struct trace_iterator *iter)
1280 struct trace_seq *s = &iter->seq;
1281 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1282 struct trace_entry *entry;
1283 unsigned long usec_rem;
1284 unsigned long long t;
1293 comm = trace_find_cmdline(iter->ent->pid);
1295 t = ns2usecs(entry->t);
1296 usec_rem = do_div(t, 1000000ULL);
1297 secs = (unsigned long)t;
1299 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1302 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1305 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1309 switch (entry->type) {
1311 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1314 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1315 entry->fn.parent_ip) {
1316 ret = trace_seq_printf(s, " <-");
1319 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1324 ret = trace_seq_printf(s, "\n");
1330 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1331 state_to_char[entry->ctx.prev_state] : 'X';
1332 T = entry->ctx.next_state < sizeof(state_to_char) ?
1333 state_to_char[entry->ctx.next_state] : 'X';
1334 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
1335 entry->ctx.prev_pid,
1336 entry->ctx.prev_prio,
1338 entry->type == TRACE_CTX ? "==>" : " +",
1339 entry->ctx.next_pid,
1340 entry->ctx.next_prio,
1346 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1347 entry->special.arg1,
1348 entry->special.arg2,
1349 entry->special.arg3);
1354 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1356 ret = trace_seq_puts(s, " <= ");
1360 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1365 ret = trace_seq_puts(s, "\n");
1373 static int print_raw_fmt(struct trace_iterator *iter)
1375 struct trace_seq *s = &iter->seq;
1376 struct trace_entry *entry;
1382 ret = trace_seq_printf(s, "%d %d %llu ",
1383 entry->pid, iter->cpu, entry->t);
1387 switch (entry->type) {
1389 ret = trace_seq_printf(s, "%x %x\n",
1390 entry->fn.ip, entry->fn.parent_ip);
1396 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1397 state_to_char[entry->ctx.prev_state] : 'X';
1398 T = entry->ctx.next_state < sizeof(state_to_char) ?
1399 state_to_char[entry->ctx.next_state] : 'X';
1400 if (entry->type == TRACE_WAKE)
1402 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
1403 entry->ctx.prev_pid,
1404 entry->ctx.prev_prio,
1406 entry->ctx.next_pid,
1407 entry->ctx.next_prio,
1414 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1415 entry->special.arg1,
1416 entry->special.arg2,
1417 entry->special.arg3);
1425 #define SEQ_PUT_FIELD_RET(s, x) \
1427 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1431 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1433 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1437 static int print_hex_fmt(struct trace_iterator *iter)
1439 struct trace_seq *s = &iter->seq;
1440 unsigned char newline = '\n';
1441 struct trace_entry *entry;
1446 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1447 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1448 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1450 switch (entry->type) {
1452 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1453 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1457 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1458 state_to_char[entry->ctx.prev_state] : 'X';
1459 T = entry->ctx.next_state < sizeof(state_to_char) ?
1460 state_to_char[entry->ctx.next_state] : 'X';
1461 if (entry->type == TRACE_WAKE)
1463 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1464 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1465 SEQ_PUT_HEX_FIELD_RET(s, S);
1466 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1467 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1468 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1469 SEQ_PUT_HEX_FIELD_RET(s, T);
1473 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1474 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1475 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1478 SEQ_PUT_FIELD_RET(s, newline);
1483 static int print_bin_fmt(struct trace_iterator *iter)
1485 struct trace_seq *s = &iter->seq;
1486 struct trace_entry *entry;
1490 SEQ_PUT_FIELD_RET(s, entry->pid);
1491 SEQ_PUT_FIELD_RET(s, entry->cpu);
1492 SEQ_PUT_FIELD_RET(s, entry->t);
1494 switch (entry->type) {
1496 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1497 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1500 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1501 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1502 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1503 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1504 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1505 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
1509 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1510 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1511 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1517 static int trace_empty(struct trace_iterator *iter)
1519 struct trace_array_cpu *data;
1522 for_each_possible_cpu(cpu) {
1523 data = iter->tr->data[cpu];
1525 if (head_page(data) && data->trace_idx &&
1526 (data->trace_tail != data->trace_head ||
1527 data->trace_tail_idx != data->trace_head_idx))
1533 static int print_trace_line(struct trace_iterator *iter)
1535 if (iter->trace && iter->trace->print_line)
1536 return iter->trace->print_line(iter);
1538 if (trace_flags & TRACE_ITER_BIN)
1539 return print_bin_fmt(iter);
1541 if (trace_flags & TRACE_ITER_HEX)
1542 return print_hex_fmt(iter);
1544 if (trace_flags & TRACE_ITER_RAW)
1545 return print_raw_fmt(iter);
1547 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1548 return print_lat_fmt(iter, iter->idx, iter->cpu);
1550 return print_trace_fmt(iter);
1553 static int s_show(struct seq_file *m, void *v)
1555 struct trace_iterator *iter = v;
1557 if (iter->ent == NULL) {
1559 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1562 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1563 /* print nothing if the buffers are empty */
1564 if (trace_empty(iter))
1566 print_trace_header(m, iter);
1567 if (!(trace_flags & TRACE_ITER_VERBOSE))
1568 print_lat_help_header(m);
1570 if (!(trace_flags & TRACE_ITER_VERBOSE))
1571 print_func_help_header(m);
1574 print_trace_line(iter);
1575 trace_print_seq(m, &iter->seq);
1581 static struct seq_operations tracer_seq_ops = {
1588 static struct trace_iterator *
1589 __tracing_open(struct inode *inode, struct file *file, int *ret)
1591 struct trace_iterator *iter;
1593 if (tracing_disabled) {
1598 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1604 mutex_lock(&trace_types_lock);
1605 if (current_trace && current_trace->print_max)
1608 iter->tr = inode->i_private;
1609 iter->trace = current_trace;
1612 /* TODO stop tracer */
1613 *ret = seq_open(file, &tracer_seq_ops);
1615 struct seq_file *m = file->private_data;
1618 /* stop the trace while dumping */
1622 if (iter->trace && iter->trace->open)
1623 iter->trace->open(iter);
1628 mutex_unlock(&trace_types_lock);
1634 int tracing_open_generic(struct inode *inode, struct file *filp)
1636 if (tracing_disabled)
1639 filp->private_data = inode->i_private;
1643 int tracing_release(struct inode *inode, struct file *file)
1645 struct seq_file *m = (struct seq_file *)file->private_data;
1646 struct trace_iterator *iter = m->private;
1648 mutex_lock(&trace_types_lock);
1649 if (iter->trace && iter->trace->close)
1650 iter->trace->close(iter);
1652 /* reenable tracing if it was previously enabled */
1655 mutex_unlock(&trace_types_lock);
1657 seq_release(inode, file);
1662 static int tracing_open(struct inode *inode, struct file *file)
1666 __tracing_open(inode, file, &ret);
1671 static int tracing_lt_open(struct inode *inode, struct file *file)
1673 struct trace_iterator *iter;
1676 iter = __tracing_open(inode, file, &ret);
1679 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1686 t_next(struct seq_file *m, void *v, loff_t *pos)
1688 struct tracer *t = m->private;
1700 static void *t_start(struct seq_file *m, loff_t *pos)
1702 struct tracer *t = m->private;
1705 mutex_lock(&trace_types_lock);
1706 for (; t && l < *pos; t = t_next(m, t, &l))
1712 static void t_stop(struct seq_file *m, void *p)
1714 mutex_unlock(&trace_types_lock);
1717 static int t_show(struct seq_file *m, void *v)
1719 struct tracer *t = v;
1724 seq_printf(m, "%s", t->name);
1733 static struct seq_operations show_traces_seq_ops = {
1740 static int show_traces_open(struct inode *inode, struct file *file)
1744 if (tracing_disabled)
1747 ret = seq_open(file, &show_traces_seq_ops);
1749 struct seq_file *m = file->private_data;
1750 m->private = trace_types;
1756 static struct file_operations tracing_fops = {
1757 .open = tracing_open,
1759 .llseek = seq_lseek,
1760 .release = tracing_release,
1763 static struct file_operations tracing_lt_fops = {
1764 .open = tracing_lt_open,
1766 .llseek = seq_lseek,
1767 .release = tracing_release,
1770 static struct file_operations show_traces_fops = {
1771 .open = show_traces_open,
1773 .release = seq_release,
1777 * Only trace on a CPU if the bitmask is set:
1779 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1782 * When tracing/tracing_cpu_mask is modified then this holds
1783 * the new bitmask we are about to install:
1785 static cpumask_t tracing_cpumask_new;
1788 * The tracer itself will not take this lock, but still we want
1789 * to provide a consistent cpumask to user-space:
1791 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1794 * Temporary storage for the character representation of the
1795 * CPU bitmask (and one more byte for the newline):
1797 static char mask_str[NR_CPUS + 1];
1800 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1801 size_t count, loff_t *ppos)
1805 mutex_lock(&tracing_cpumask_update_lock);
1807 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1808 if (count - len < 2) {
1812 len += sprintf(mask_str + len, "\n");
1813 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1816 mutex_unlock(&tracing_cpumask_update_lock);
1822 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1823 size_t count, loff_t *ppos)
1827 mutex_lock(&tracing_cpumask_update_lock);
1828 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1832 raw_local_irq_disable();
1833 __raw_spin_lock(&ftrace_max_lock);
1834 for_each_possible_cpu(cpu) {
1836 * Increase/decrease the disabled counter if we are
1837 * about to flip a bit in the cpumask:
1839 if (cpu_isset(cpu, tracing_cpumask) &&
1840 !cpu_isset(cpu, tracing_cpumask_new)) {
1841 atomic_inc(&global_trace.data[cpu]->disabled);
1843 if (!cpu_isset(cpu, tracing_cpumask) &&
1844 cpu_isset(cpu, tracing_cpumask_new)) {
1845 atomic_dec(&global_trace.data[cpu]->disabled);
1848 __raw_spin_unlock(&ftrace_max_lock);
1849 raw_local_irq_enable();
1851 tracing_cpumask = tracing_cpumask_new;
1853 mutex_unlock(&tracing_cpumask_update_lock);
1858 mutex_unlock(&tracing_cpumask_update_lock);
1863 static struct file_operations tracing_cpumask_fops = {
1864 .open = tracing_open_generic,
1865 .read = tracing_cpumask_read,
1866 .write = tracing_cpumask_write,
1870 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1871 size_t cnt, loff_t *ppos)
1878 /* calulate max size */
1879 for (i = 0; trace_options[i]; i++) {
1880 len += strlen(trace_options[i]);
1881 len += 3; /* "no" and space */
1884 /* +2 for \n and \0 */
1885 buf = kmalloc(len + 2, GFP_KERNEL);
1889 for (i = 0; trace_options[i]; i++) {
1890 if (trace_flags & (1 << i))
1891 r += sprintf(buf + r, "%s ", trace_options[i]);
1893 r += sprintf(buf + r, "no%s ", trace_options[i]);
1896 r += sprintf(buf + r, "\n");
1897 WARN_ON(r >= len + 2);
1899 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1907 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1908 size_t cnt, loff_t *ppos)
1915 if (cnt >= sizeof(buf))
1918 if (copy_from_user(&buf, ubuf, cnt))
1923 if (strncmp(buf, "no", 2) == 0) {
1928 for (i = 0; trace_options[i]; i++) {
1929 int len = strlen(trace_options[i]);
1931 if (strncmp(cmp, trace_options[i], len) == 0) {
1933 trace_flags &= ~(1 << i);
1935 trace_flags |= (1 << i);
1940 * If no option could be set, return an error:
1942 if (!trace_options[i])
1950 static struct file_operations tracing_iter_fops = {
1951 .open = tracing_open_generic,
1952 .read = tracing_iter_ctrl_read,
1953 .write = tracing_iter_ctrl_write,
1956 static const char readme_msg[] =
1957 "tracing mini-HOWTO:\n\n"
1959 "# mount -t debugfs nodev /debug\n\n"
1960 "# cat /debug/tracing/available_tracers\n"
1961 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1962 "# cat /debug/tracing/current_tracer\n"
1964 "# echo sched_switch > /debug/tracing/current_tracer\n"
1965 "# cat /debug/tracing/current_tracer\n"
1967 "# cat /debug/tracing/iter_ctrl\n"
1968 "noprint-parent nosym-offset nosym-addr noverbose\n"
1969 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1970 "# echo 1 > /debug/tracing/tracing_enabled\n"
1971 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1972 "echo 0 > /debug/tracing/tracing_enabled\n"
1976 tracing_readme_read(struct file *filp, char __user *ubuf,
1977 size_t cnt, loff_t *ppos)
1979 return simple_read_from_buffer(ubuf, cnt, ppos,
1980 readme_msg, strlen(readme_msg));
1983 static struct file_operations tracing_readme_fops = {
1984 .open = tracing_open_generic,
1985 .read = tracing_readme_read,
1989 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1990 size_t cnt, loff_t *ppos)
1992 struct trace_array *tr = filp->private_data;
1996 r = sprintf(buf, "%ld\n", tr->ctrl);
1997 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2001 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2002 size_t cnt, loff_t *ppos)
2004 struct trace_array *tr = filp->private_data;
2009 if (cnt >= sizeof(buf))
2012 if (copy_from_user(&buf, ubuf, cnt))
2017 ret = strict_strtoul(buf, 10, &val);
2023 mutex_lock(&trace_types_lock);
2024 if (tr->ctrl ^ val) {
2032 if (current_trace && current_trace->ctrl_update)
2033 current_trace->ctrl_update(tr);
2035 mutex_unlock(&trace_types_lock);
2043 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2044 size_t cnt, loff_t *ppos)
2046 char buf[max_tracer_type_len+2];
2049 mutex_lock(&trace_types_lock);
2051 r = sprintf(buf, "%s\n", current_trace->name);
2053 r = sprintf(buf, "\n");
2054 mutex_unlock(&trace_types_lock);
2056 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2060 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2061 size_t cnt, loff_t *ppos)
2063 struct trace_array *tr = &global_trace;
2065 char buf[max_tracer_type_len+1];
2068 if (cnt > max_tracer_type_len)
2069 cnt = max_tracer_type_len;
2071 if (copy_from_user(&buf, ubuf, cnt))
2076 /* strip ending whitespace. */
2077 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2080 mutex_lock(&trace_types_lock);
2081 for (t = trace_types; t; t = t->next) {
2082 if (strcmp(t->name, buf) == 0)
2085 if (!t || t == current_trace)
2088 if (current_trace && current_trace->reset)
2089 current_trace->reset(tr);
2096 mutex_unlock(&trace_types_lock);
2104 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2105 size_t cnt, loff_t *ppos)
2107 unsigned long *ptr = filp->private_data;
2111 r = snprintf(buf, sizeof(buf), "%ld\n",
2112 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2113 if (r > sizeof(buf))
2115 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2119 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2120 size_t cnt, loff_t *ppos)
2122 long *ptr = filp->private_data;
2127 if (cnt >= sizeof(buf))
2130 if (copy_from_user(&buf, ubuf, cnt))
2135 ret = strict_strtoul(buf, 10, &val);
2144 static atomic_t tracing_reader;
2146 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2148 struct trace_iterator *iter;
2150 if (tracing_disabled)
2153 /* We only allow for reader of the pipe */
2154 if (atomic_inc_return(&tracing_reader) != 1) {
2155 atomic_dec(&tracing_reader);
2159 /* create a buffer to store the information to pass to userspace */
2160 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2164 iter->tr = &global_trace;
2165 iter->trace = current_trace;
2167 filp->private_data = iter;
2172 static int tracing_release_pipe(struct inode *inode, struct file *file)
2174 struct trace_iterator *iter = file->private_data;
2177 atomic_dec(&tracing_reader);
2183 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2185 struct trace_iterator *iter = filp->private_data;
2187 if (trace_flags & TRACE_ITER_BLOCK) {
2189 * Always select as readable when in blocking mode
2191 return POLLIN | POLLRDNORM;
2193 if (!trace_empty(iter))
2194 return POLLIN | POLLRDNORM;
2195 poll_wait(filp, &trace_wait, poll_table);
2196 if (!trace_empty(iter))
2197 return POLLIN | POLLRDNORM;
2207 tracing_read_pipe(struct file *filp, char __user *ubuf,
2208 size_t cnt, loff_t *ppos)
2210 struct trace_iterator *iter = filp->private_data;
2211 struct trace_array_cpu *data;
2212 struct trace_array *tr = iter->tr;
2213 struct tracer *tracer = iter->trace;
2214 static cpumask_t mask;
2216 unsigned long flags;
2217 #ifdef CONFIG_FTRACE
2225 /* return any leftover data */
2226 if (iter->seq.len > start) {
2227 len = iter->seq.len - start;
2230 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2239 trace_seq_reset(&iter->seq);
2242 while (trace_empty(iter)) {
2244 if ((filp->f_flags & O_NONBLOCK))
2248 * This is a make-shift waitqueue. The reason we don't use
2249 * an actual wait queue is because:
2250 * 1) we only ever have one waiter
2251 * 2) the tracing, traces all functions, we don't want
2252 * the overhead of calling wake_up and friends
2253 * (and tracing them too)
2254 * Anyway, this is really very primitive wakeup.
2256 set_current_state(TASK_INTERRUPTIBLE);
2257 iter->tr->waiter = current;
2259 /* sleep for one second, and try again. */
2260 schedule_timeout(HZ);
2262 iter->tr->waiter = NULL;
2264 if (signal_pending(current))
2267 if (iter->trace != current_trace)
2271 * We block until we read something and tracing is disabled.
2272 * We still block if tracing is disabled, but we have never
2273 * read anything. This allows a user to cat this file, and
2274 * then enable tracing. But after we have read something,
2275 * we give an EOF when tracing is again disabled.
2277 * iter->pos will be 0 if we haven't read anything.
2279 if (!tracer_enabled && iter->pos)
2285 /* stop when tracing is finished */
2286 if (trace_empty(iter))
2289 if (cnt >= PAGE_SIZE)
2290 cnt = PAGE_SIZE - 1;
2292 memset(iter, 0, sizeof(*iter));
2294 iter->trace = tracer;
2298 * We need to stop all tracing on all CPUS to read the
2299 * the next buffer. This is a bit expensive, but is
2300 * not done often. We fill all what we can read,
2301 * and then release the locks again.
2305 local_irq_save(flags);
2306 #ifdef CONFIG_FTRACE
2307 ftrace_save = ftrace_enabled;
2311 for_each_possible_cpu(cpu) {
2312 data = iter->tr->data[cpu];
2314 if (!head_page(data) || !data->trace_idx)
2317 atomic_inc(&data->disabled);
2321 for_each_cpu_mask(cpu, mask) {
2322 data = iter->tr->data[cpu];
2323 __raw_spin_lock(&data->lock);
2326 while (find_next_entry_inc(iter) != NULL) {
2327 int len = iter->seq.len;
2329 ret = print_trace_line(iter);
2331 /* don't print partial lines */
2332 iter->seq.len = len;
2336 trace_consume(iter);
2338 if (iter->seq.len >= cnt)
2342 for_each_cpu_mask(cpu, mask) {
2343 data = iter->tr->data[cpu];
2344 __raw_spin_unlock(&data->lock);
2347 for_each_cpu_mask(cpu, mask) {
2348 data = iter->tr->data[cpu];
2349 atomic_dec(&data->disabled);
2351 #ifdef CONFIG_FTRACE
2352 ftrace_enabled = ftrace_save;
2354 local_irq_restore(flags);
2356 /* Now copy what we have to the user */
2357 read = iter->seq.len;
2361 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2363 if (read < iter->seq.len)
2366 trace_seq_reset(&iter->seq);
2375 tracing_entries_read(struct file *filp, char __user *ubuf,
2376 size_t cnt, loff_t *ppos)
2378 struct trace_array *tr = filp->private_data;
2382 r = sprintf(buf, "%lu\n", tr->entries);
2383 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2387 tracing_entries_write(struct file *filp, const char __user *ubuf,
2388 size_t cnt, loff_t *ppos)
2394 if (cnt >= sizeof(buf))
2397 if (copy_from_user(&buf, ubuf, cnt))
2402 ret = strict_strtoul(buf, 10, &val);
2406 /* must have at least 1 entry */
2410 mutex_lock(&trace_types_lock);
2412 if (current_trace != &no_tracer) {
2414 pr_info("ftrace: set current_tracer to none"
2415 " before modifying buffer size\n");
2419 if (val > global_trace.entries) {
2420 while (global_trace.entries < val) {
2421 if (trace_alloc_page()) {
2427 /* include the number of entries in val (inc of page entries) */
2428 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2435 max_tr.entries = global_trace.entries;
2436 mutex_unlock(&trace_types_lock);
2441 static struct file_operations tracing_max_lat_fops = {
2442 .open = tracing_open_generic,
2443 .read = tracing_max_lat_read,
2444 .write = tracing_max_lat_write,
2447 static struct file_operations tracing_ctrl_fops = {
2448 .open = tracing_open_generic,
2449 .read = tracing_ctrl_read,
2450 .write = tracing_ctrl_write,
2453 static struct file_operations set_tracer_fops = {
2454 .open = tracing_open_generic,
2455 .read = tracing_set_trace_read,
2456 .write = tracing_set_trace_write,
2459 static struct file_operations tracing_pipe_fops = {
2460 .open = tracing_open_pipe,
2461 .poll = tracing_poll_pipe,
2462 .read = tracing_read_pipe,
2463 .release = tracing_release_pipe,
2466 static struct file_operations tracing_entries_fops = {
2467 .open = tracing_open_generic,
2468 .read = tracing_entries_read,
2469 .write = tracing_entries_write,
2472 #ifdef CONFIG_DYNAMIC_FTRACE
2475 tracing_read_long(struct file *filp, char __user *ubuf,
2476 size_t cnt, loff_t *ppos)
2478 unsigned long *p = filp->private_data;
2482 r = sprintf(buf, "%ld\n", *p);
2484 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2487 static struct file_operations tracing_read_long_fops = {
2488 .open = tracing_open_generic,
2489 .read = tracing_read_long,
2493 static struct dentry *d_tracer;
2495 struct dentry *tracing_init_dentry(void)
2502 d_tracer = debugfs_create_dir("tracing", NULL);
2504 if (!d_tracer && !once) {
2506 pr_warning("Could not create debugfs directory 'tracing'\n");
2513 #ifdef CONFIG_FTRACE_SELFTEST
2514 /* Let selftest have access to static functions in this file */
2515 #include "trace_selftest.c"
2518 static __init void tracer_init_debugfs(void)
2520 struct dentry *d_tracer;
2521 struct dentry *entry;
2523 d_tracer = tracing_init_dentry();
2525 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2526 &global_trace, &tracing_ctrl_fops);
2528 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2530 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2531 NULL, &tracing_iter_fops);
2533 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2535 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2536 NULL, &tracing_cpumask_fops);
2538 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2540 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2541 &global_trace, &tracing_lt_fops);
2543 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2545 entry = debugfs_create_file("trace", 0444, d_tracer,
2546 &global_trace, &tracing_fops);
2548 pr_warning("Could not create debugfs 'trace' entry\n");
2550 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2551 &global_trace, &show_traces_fops);
2553 pr_warning("Could not create debugfs 'trace' entry\n");
2555 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2556 &global_trace, &set_tracer_fops);
2558 pr_warning("Could not create debugfs 'trace' entry\n");
2560 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2561 &tracing_max_latency,
2562 &tracing_max_lat_fops);
2564 pr_warning("Could not create debugfs "
2565 "'tracing_max_latency' entry\n");
2567 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2568 &tracing_thresh, &tracing_max_lat_fops);
2570 pr_warning("Could not create debugfs "
2571 "'tracing_threash' entry\n");
2572 entry = debugfs_create_file("README", 0644, d_tracer,
2573 NULL, &tracing_readme_fops);
2575 pr_warning("Could not create debugfs 'README' entry\n");
2577 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2578 NULL, &tracing_pipe_fops);
2580 pr_warning("Could not create debugfs "
2581 "'tracing_threash' entry\n");
2583 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2584 &global_trace, &tracing_entries_fops);
2586 pr_warning("Could not create debugfs "
2587 "'tracing_threash' entry\n");
2589 #ifdef CONFIG_DYNAMIC_FTRACE
2590 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2591 &ftrace_update_tot_cnt,
2592 &tracing_read_long_fops);
2594 pr_warning("Could not create debugfs "
2595 "'dyn_ftrace_total_info' entry\n");
2599 static int trace_alloc_page(void)
2601 struct trace_array_cpu *data;
2602 struct page *page, *tmp;
2607 /* first allocate a page for each CPU */
2608 for_each_possible_cpu(i) {
2609 array = (void *)__get_free_page(GFP_KERNEL);
2610 if (array == NULL) {
2611 printk(KERN_ERR "tracer: failed to allocate page"
2612 "for trace buffer!\n");
2616 page = virt_to_page(array);
2617 list_add(&page->lru, &pages);
2619 /* Only allocate if we are actually using the max trace */
2620 #ifdef CONFIG_TRACER_MAX_TRACE
2621 array = (void *)__get_free_page(GFP_KERNEL);
2622 if (array == NULL) {
2623 printk(KERN_ERR "tracer: failed to allocate page"
2624 "for trace buffer!\n");
2627 page = virt_to_page(array);
2628 list_add(&page->lru, &pages);
2632 /* Now that we successfully allocate a page per CPU, add them */
2633 for_each_possible_cpu(i) {
2634 data = global_trace.data[i];
2635 page = list_entry(pages.next, struct page, lru);
2636 list_del_init(&page->lru);
2637 list_add_tail(&page->lru, &data->trace_pages);
2640 #ifdef CONFIG_TRACER_MAX_TRACE
2641 data = max_tr.data[i];
2642 page = list_entry(pages.next, struct page, lru);
2643 list_del_init(&page->lru);
2644 list_add_tail(&page->lru, &data->trace_pages);
2648 global_trace.entries += ENTRIES_PER_PAGE;
2653 list_for_each_entry_safe(page, tmp, &pages, lru) {
2654 list_del_init(&page->lru);
2660 static int trace_free_page(void)
2662 struct trace_array_cpu *data;
2664 struct list_head *p;
2668 /* free one page from each buffer */
2669 for_each_possible_cpu(i) {
2670 data = global_trace.data[i];
2671 p = data->trace_pages.next;
2672 if (p == &data->trace_pages) {
2673 /* should never happen */
2675 tracing_disabled = 1;
2679 page = list_entry(p, struct page, lru);
2681 list_del(&page->lru);
2684 tracing_reset(data);
2686 #ifdef CONFIG_TRACER_MAX_TRACE
2687 data = max_tr.data[i];
2688 p = data->trace_pages.next;
2689 if (p == &data->trace_pages) {
2690 /* should never happen */
2692 tracing_disabled = 1;
2696 page = list_entry(p, struct page, lru);
2698 list_del(&page->lru);
2701 tracing_reset(data);
2704 global_trace.entries -= ENTRIES_PER_PAGE;
2709 __init static int tracer_alloc_buffers(void)
2711 struct trace_array_cpu *data;
2718 global_trace.ctrl = tracer_enabled;
2720 /* Allocate the first page for all buffers */
2721 for_each_possible_cpu(i) {
2722 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2723 max_tr.data[i] = &per_cpu(max_data, i);
2725 array = (void *)__get_free_page(GFP_KERNEL);
2726 if (array == NULL) {
2727 printk(KERN_ERR "tracer: failed to allocate page"
2728 "for trace buffer!\n");
2732 /* set the array to the list */
2733 INIT_LIST_HEAD(&data->trace_pages);
2734 page = virt_to_page(array);
2735 list_add(&page->lru, &data->trace_pages);
2736 /* use the LRU flag to differentiate the two buffers */
2739 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2740 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2742 /* Only allocate if we are actually using the max trace */
2743 #ifdef CONFIG_TRACER_MAX_TRACE
2744 array = (void *)__get_free_page(GFP_KERNEL);
2745 if (array == NULL) {
2746 printk(KERN_ERR "tracer: failed to allocate page"
2747 "for trace buffer!\n");
2751 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2752 page = virt_to_page(array);
2753 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2759 * Since we allocate by orders of pages, we may be able to
2762 global_trace.entries = ENTRIES_PER_PAGE;
2765 while (global_trace.entries < trace_nr_entries) {
2766 if (trace_alloc_page())
2770 max_tr.entries = global_trace.entries;
2772 pr_info("tracer: %d pages allocated for %ld",
2773 pages, trace_nr_entries);
2774 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2775 pr_info(" actual entries %ld\n", global_trace.entries);
2777 tracer_init_debugfs();
2779 trace_init_cmdlines();
2781 register_tracer(&no_tracer);
2782 current_trace = &no_tracer;
2784 /* All seems OK, enable tracing */
2785 tracing_disabled = 0;
2790 for (i-- ; i >= 0; i--) {
2791 struct page *page, *tmp;
2792 struct trace_array_cpu *data = global_trace.data[i];
2795 list_for_each_entry_safe(page, tmp,
2796 &data->trace_pages, lru) {
2797 list_del_init(&page->lru);
2802 #ifdef CONFIG_TRACER_MAX_TRACE
2803 data = max_tr.data[i];
2805 list_for_each_entry_safe(page, tmp,
2806 &data->trace_pages, lru) {
2807 list_del_init(&page->lru);
2815 fs_initcall(tracer_alloc_buffers);