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 static int tracing_disabled = 1;
41 ns2usecs(cycle_t nsec)
48 cycle_t ftrace_now(int cpu)
50 return cpu_clock(cpu);
53 static struct trace_array global_trace;
55 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
57 static struct trace_array max_tr;
59 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
61 static int tracer_enabled = 1;
62 static unsigned long trace_nr_entries = 65536UL;
64 static struct tracer *trace_types __read_mostly;
65 static struct tracer *current_trace __read_mostly;
66 static int max_tracer_type_len;
68 static DEFINE_MUTEX(trace_types_lock);
69 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
71 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
73 void trace_wake_up(void)
76 * The runqueue_is_locked() can fail, but this is the best we
79 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
83 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
85 static int __init set_nr_entries(char *str)
89 trace_nr_entries = simple_strtoul(str, &str, 0);
92 __setup("trace_entries=", set_nr_entries);
94 unsigned long nsecs_to_usecs(unsigned long nsecs)
100 __TRACE_FIRST_TYPE = 0,
111 enum trace_flag_type {
112 TRACE_FLAG_IRQS_OFF = 0x01,
113 TRACE_FLAG_NEED_RESCHED = 0x02,
114 TRACE_FLAG_HARDIRQ = 0x04,
115 TRACE_FLAG_SOFTIRQ = 0x08,
118 #define TRACE_ITER_SYM_MASK \
119 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
121 /* These must match the bit postions above */
122 static const char *trace_options[] = {
136 static DEFINE_SPINLOCK(ftrace_max_lock);
139 * Copy the new maximum trace into the separate maximum-trace
140 * structure. (this way the maximum trace is permanently saved,
141 * for later retrieval via /debugfs/tracing/latency_trace)
144 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
146 struct trace_array_cpu *data = tr->data[cpu];
149 max_tr.time_start = data->preempt_timestamp;
151 data = max_tr.data[cpu];
152 data->saved_latency = tracing_max_latency;
154 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
155 data->pid = tsk->pid;
156 data->uid = tsk->uid;
157 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
158 data->policy = tsk->policy;
159 data->rt_priority = tsk->rt_priority;
161 /* record this tasks comm */
162 tracing_record_cmdline(current);
165 void check_pages(struct trace_array_cpu *data)
167 struct page *page, *tmp;
169 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
170 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
172 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
173 BUG_ON(page->lru.next->prev != &page->lru);
174 BUG_ON(page->lru.prev->next != &page->lru);
178 void *head_page(struct trace_array_cpu *data)
183 if (list_empty(&data->trace_pages))
186 page = list_entry(data->trace_pages.next, struct page, lru);
187 BUG_ON(&page->lru == &data->trace_pages);
189 return page_address(page);
193 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
195 int len = (PAGE_SIZE - 1) - s->len;
203 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
206 /* If we can't write it all, don't bother writing anything */
216 trace_seq_puts(struct trace_seq *s, const char *str)
218 int len = strlen(str);
220 if (len > ((PAGE_SIZE - 1) - s->len))
223 memcpy(s->buffer + s->len, str, len);
230 trace_seq_putc(struct trace_seq *s, unsigned char c)
232 if (s->len >= (PAGE_SIZE - 1))
235 s->buffer[s->len++] = c;
241 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
243 if (len > ((PAGE_SIZE - 1) - s->len))
246 memcpy(s->buffer + s->len, mem, len);
255 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
257 unsigned char hex[HEX_CHARS];
262 BUG_ON(len >= HEX_CHARS);
267 for (i = 0, j = 0; i < len; i++) {
269 for (i = len-1, j = 0; i >= 0; i--) {
273 hex[j] = byte & 0x0f;
290 return trace_seq_putmem(s, hex, j);
294 trace_seq_reset(struct trace_seq *s)
300 trace_print_seq(struct seq_file *m, struct trace_seq *s)
302 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
305 seq_puts(m, s->buffer);
311 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
313 struct list_head flip_pages;
315 INIT_LIST_HEAD(&flip_pages);
317 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
318 sizeof(struct trace_array_cpu) -
319 offsetof(struct trace_array_cpu, trace_head_idx));
323 list_splice_init(&tr1->trace_pages, &flip_pages);
324 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
325 list_splice_init(&flip_pages, &tr2->trace_pages);
326 BUG_ON(!list_empty(&flip_pages));
332 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
334 struct trace_array_cpu *data;
337 WARN_ON_ONCE(!irqs_disabled());
338 spin_lock(&ftrace_max_lock);
339 /* clear out all the previous traces */
340 for_each_possible_cpu(i) {
342 flip_trace(max_tr.data[i], data);
346 __update_max_tr(tr, tsk, cpu);
347 spin_unlock(&ftrace_max_lock);
351 * update_max_tr_single - only copy one trace over, and reset the rest
353 * @tsk - task with the latency
354 * @cpu - the cpu of the buffer to copy.
357 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
359 struct trace_array_cpu *data = tr->data[cpu];
362 WARN_ON_ONCE(!irqs_disabled());
363 spin_lock(&ftrace_max_lock);
364 for_each_possible_cpu(i)
365 tracing_reset(max_tr.data[i]);
367 flip_trace(max_tr.data[cpu], data);
370 __update_max_tr(tr, tsk, cpu);
371 spin_unlock(&ftrace_max_lock);
374 int register_tracer(struct tracer *type)
381 pr_info("Tracer must have a name\n");
385 mutex_lock(&trace_types_lock);
386 for (t = trace_types; t; t = t->next) {
387 if (strcmp(type->name, t->name) == 0) {
389 pr_info("Trace %s already registered\n",
396 #ifdef CONFIG_FTRACE_STARTUP_TEST
397 if (type->selftest) {
398 struct tracer *saved_tracer = current_trace;
399 struct trace_array_cpu *data;
400 struct trace_array *tr = &global_trace;
401 int saved_ctrl = tr->ctrl;
404 * Run a selftest on this tracer.
405 * Here we reset the trace buffer, and set the current
406 * tracer to be this tracer. The tracer can then run some
407 * internal tracing to verify that everything is in order.
408 * If we fail, we do not register this tracer.
410 for_each_possible_cpu(i) {
412 if (!head_page(data))
416 current_trace = type;
418 /* the test is responsible for initializing and enabling */
419 pr_info("Testing tracer %s: ", type->name);
420 ret = type->selftest(type, tr);
421 /* the test is responsible for resetting too */
422 current_trace = saved_tracer;
423 tr->ctrl = saved_ctrl;
425 printk(KERN_CONT "FAILED!\n");
428 /* Only reset on passing, to avoid touching corrupted buffers */
429 for_each_possible_cpu(i) {
431 if (!head_page(data))
435 printk(KERN_CONT "PASSED\n");
439 type->next = trace_types;
441 len = strlen(type->name);
442 if (len > max_tracer_type_len)
443 max_tracer_type_len = len;
446 mutex_unlock(&trace_types_lock);
451 void unregister_tracer(struct tracer *type)
456 mutex_lock(&trace_types_lock);
457 for (t = &trace_types; *t; t = &(*t)->next) {
461 pr_info("Trace %s not registered\n", type->name);
466 if (strlen(type->name) != max_tracer_type_len)
469 max_tracer_type_len = 0;
470 for (t = &trace_types; *t; t = &(*t)->next) {
471 len = strlen((*t)->name);
472 if (len > max_tracer_type_len)
473 max_tracer_type_len = len;
476 mutex_unlock(&trace_types_lock);
479 void tracing_reset(struct trace_array_cpu *data)
482 data->trace_head = data->trace_tail = head_page(data);
483 data->trace_head_idx = 0;
484 data->trace_tail_idx = 0;
487 #define SAVED_CMDLINES 128
488 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
489 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
490 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
491 static int cmdline_idx;
492 static DEFINE_SPINLOCK(trace_cmdline_lock);
493 atomic_t trace_record_cmdline_disabled;
495 static void trace_init_cmdlines(void)
497 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
498 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
502 void trace_stop_cmdline_recording(void);
504 static void trace_save_cmdline(struct task_struct *tsk)
509 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
513 * It's not the end of the world if we don't get
514 * the lock, but we also don't want to spin
515 * nor do we want to disable interrupts,
516 * so if we miss here, then better luck next time.
518 if (!spin_trylock(&trace_cmdline_lock))
521 idx = map_pid_to_cmdline[tsk->pid];
522 if (idx >= SAVED_CMDLINES) {
523 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
525 map = map_cmdline_to_pid[idx];
526 if (map <= PID_MAX_DEFAULT)
527 map_pid_to_cmdline[map] = (unsigned)-1;
529 map_pid_to_cmdline[tsk->pid] = idx;
534 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
536 spin_unlock(&trace_cmdline_lock);
539 static char *trace_find_cmdline(int pid)
541 char *cmdline = "<...>";
547 if (pid > PID_MAX_DEFAULT)
550 map = map_pid_to_cmdline[pid];
551 if (map >= SAVED_CMDLINES)
554 cmdline = saved_cmdlines[map];
560 void tracing_record_cmdline(struct task_struct *tsk)
562 if (atomic_read(&trace_record_cmdline_disabled))
565 trace_save_cmdline(tsk);
568 static inline struct list_head *
569 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
572 * Roundrobin - but skip the head (which is not a real page):
575 if (unlikely(next == &data->trace_pages))
577 BUG_ON(next == &data->trace_pages);
583 trace_next_page(struct trace_array_cpu *data, void *addr)
585 struct list_head *next;
588 page = virt_to_page(addr);
590 next = trace_next_list(data, &page->lru);
591 page = list_entry(next, struct page, lru);
593 return page_address(page);
596 static inline struct trace_entry *
597 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
599 unsigned long idx, idx_next;
600 struct trace_entry *entry;
603 idx = data->trace_head_idx;
606 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
608 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
610 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
611 data->trace_head = trace_next_page(data, data->trace_head);
615 if (data->trace_head == data->trace_tail &&
616 idx_next == data->trace_tail_idx) {
618 data->trace_tail_idx++;
619 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
621 trace_next_page(data, data->trace_tail);
622 data->trace_tail_idx = 0;
626 data->trace_head_idx = idx_next;
632 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
634 struct task_struct *tsk = current;
637 pc = preempt_count();
639 entry->preempt_count = pc & 0xff;
640 entry->pid = tsk->pid;
641 entry->t = ftrace_now(raw_smp_processor_id());
642 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
643 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
644 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
645 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
649 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
650 unsigned long ip, unsigned long parent_ip, unsigned long flags)
652 struct trace_entry *entry;
653 unsigned long irq_flags;
655 spin_lock_irqsave(&data->lock, irq_flags);
656 entry = tracing_get_trace_entry(tr, data);
657 tracing_generic_entry_update(entry, flags);
658 entry->type = TRACE_FN;
660 entry->fn.parent_ip = parent_ip;
661 spin_unlock_irqrestore(&data->lock, irq_flags);
667 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
668 unsigned long ip, unsigned long parent_ip, unsigned long flags)
670 if (likely(!atomic_read(&data->disabled)))
671 trace_function(tr, data, ip, parent_ip, flags);
675 __trace_special(void *__tr, void *__data,
676 unsigned long arg1, unsigned long arg2, unsigned long arg3)
678 struct trace_array_cpu *data = __data;
679 struct trace_array *tr = __tr;
680 struct trace_entry *entry;
681 unsigned long irq_flags;
683 spin_lock_irqsave(&data->lock, irq_flags);
684 entry = tracing_get_trace_entry(tr, data);
685 tracing_generic_entry_update(entry, 0);
686 entry->type = TRACE_SPECIAL;
687 entry->special.arg1 = arg1;
688 entry->special.arg2 = arg2;
689 entry->special.arg3 = arg3;
690 spin_unlock_irqrestore(&data->lock, irq_flags);
695 void __trace_stack(struct trace_array *tr,
696 struct trace_array_cpu *data,
700 struct trace_entry *entry;
701 struct stack_trace trace;
703 if (!(trace_flags & TRACE_ITER_STACKTRACE))
706 entry = tracing_get_trace_entry(tr, data);
707 tracing_generic_entry_update(entry, flags);
708 entry->type = TRACE_STACK;
710 memset(&entry->stack, 0, sizeof(entry->stack));
712 trace.nr_entries = 0;
713 trace.max_entries = FTRACE_STACK_ENTRIES;
715 trace.entries = entry->stack.caller;
717 save_stack_trace(&trace);
721 tracing_sched_switch_trace(struct trace_array *tr,
722 struct trace_array_cpu *data,
723 struct task_struct *prev,
724 struct task_struct *next,
727 struct trace_entry *entry;
728 unsigned long irq_flags;
730 spin_lock_irqsave(&data->lock, irq_flags);
731 entry = tracing_get_trace_entry(tr, data);
732 tracing_generic_entry_update(entry, flags);
733 entry->type = TRACE_CTX;
734 entry->ctx.prev_pid = prev->pid;
735 entry->ctx.prev_prio = prev->prio;
736 entry->ctx.prev_state = prev->state;
737 entry->ctx.next_pid = next->pid;
738 entry->ctx.next_prio = next->prio;
739 entry->ctx.next_state = next->state;
740 __trace_stack(tr, data, flags, 4);
741 spin_unlock_irqrestore(&data->lock, irq_flags);
745 tracing_sched_wakeup_trace(struct trace_array *tr,
746 struct trace_array_cpu *data,
747 struct task_struct *wakee,
748 struct task_struct *curr,
751 struct trace_entry *entry;
752 unsigned long irq_flags;
754 spin_lock_irqsave(&data->lock, irq_flags);
755 entry = tracing_get_trace_entry(tr, data);
756 tracing_generic_entry_update(entry, flags);
757 entry->type = TRACE_WAKE;
758 entry->ctx.prev_pid = curr->pid;
759 entry->ctx.prev_prio = curr->prio;
760 entry->ctx.prev_state = curr->state;
761 entry->ctx.next_pid = wakee->pid;
762 entry->ctx.next_prio = wakee->prio;
763 entry->ctx.next_state = wakee->state;
764 __trace_stack(tr, data, flags, 5);
765 spin_unlock_irqrestore(&data->lock, irq_flags);
772 function_trace_call(unsigned long ip, unsigned long parent_ip)
774 struct trace_array *tr = &global_trace;
775 struct trace_array_cpu *data;
780 if (unlikely(!tracer_enabled))
783 local_irq_save(flags);
784 cpu = raw_smp_processor_id();
785 data = tr->data[cpu];
786 disabled = atomic_inc_return(&data->disabled);
788 if (likely(disabled == 1))
789 trace_function(tr, data, ip, parent_ip, flags);
791 atomic_dec(&data->disabled);
792 local_irq_restore(flags);
795 static struct ftrace_ops trace_ops __read_mostly =
797 .func = function_trace_call,
800 void tracing_start_function_trace(void)
802 register_ftrace_function(&trace_ops);
805 void tracing_stop_function_trace(void)
807 unregister_ftrace_function(&trace_ops);
811 enum trace_file_type {
812 TRACE_FILE_LAT_FMT = 1,
815 static struct trace_entry *
816 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
817 struct trace_iterator *iter, int cpu)
820 struct trace_entry *array;
822 if (iter->next_idx[cpu] >= tr->entries ||
823 iter->next_idx[cpu] >= data->trace_idx ||
824 (data->trace_head == data->trace_tail &&
825 data->trace_head_idx == data->trace_tail_idx))
828 if (!iter->next_page[cpu]) {
829 /* Initialize the iterator for this cpu trace buffer */
830 WARN_ON(!data->trace_tail);
831 page = virt_to_page(data->trace_tail);
832 iter->next_page[cpu] = &page->lru;
833 iter->next_page_idx[cpu] = data->trace_tail_idx;
836 page = list_entry(iter->next_page[cpu], struct page, lru);
837 BUG_ON(&data->trace_pages == &page->lru);
839 array = page_address(page);
841 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
842 return &array[iter->next_page_idx[cpu]];
845 static struct trace_entry *
846 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
848 struct trace_array *tr = iter->tr;
849 struct trace_entry *ent, *next = NULL;
853 for_each_possible_cpu(cpu) {
854 if (!head_page(tr->data[cpu]))
856 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
858 * Pick the entry with the smallest timestamp:
860 if (ent && (!next || ent->t < next->t)) {
872 static void trace_iterator_increment(struct trace_iterator *iter)
875 iter->next_idx[iter->cpu]++;
876 iter->next_page_idx[iter->cpu]++;
878 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
879 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
881 iter->next_page_idx[iter->cpu] = 0;
882 iter->next_page[iter->cpu] =
883 trace_next_list(data, iter->next_page[iter->cpu]);
887 static void trace_consume(struct trace_iterator *iter)
889 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
891 data->trace_tail_idx++;
892 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
893 data->trace_tail = trace_next_page(data, data->trace_tail);
894 data->trace_tail_idx = 0;
897 /* Check if we empty it, then reset the index */
898 if (data->trace_head == data->trace_tail &&
899 data->trace_head_idx == data->trace_tail_idx)
903 static void *find_next_entry_inc(struct trace_iterator *iter)
905 struct trace_entry *next;
908 next = find_next_entry(iter, &next_cpu);
910 iter->prev_ent = iter->ent;
911 iter->prev_cpu = iter->cpu;
914 iter->cpu = next_cpu;
917 trace_iterator_increment(iter);
919 return next ? iter : NULL;
922 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
924 struct trace_iterator *iter = m->private;
925 void *last_ent = iter->ent;
931 /* can't go backwards */
936 ent = find_next_entry_inc(iter);
940 while (ent && iter->idx < i)
941 ent = find_next_entry_inc(iter);
945 if (last_ent && !ent)
946 seq_puts(m, "\n\nvim:ft=help\n");
951 static void *s_start(struct seq_file *m, loff_t *pos)
953 struct trace_iterator *iter = m->private;
958 mutex_lock(&trace_types_lock);
960 if (!current_trace || current_trace != iter->trace)
963 atomic_inc(&trace_record_cmdline_disabled);
965 /* let the tracer grab locks here if needed */
966 if (current_trace->start)
967 current_trace->start(iter);
969 if (*pos != iter->pos) {
973 iter->prev_ent = NULL;
976 for_each_possible_cpu(i) {
977 iter->next_idx[i] = 0;
978 iter->next_page[i] = NULL;
981 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
986 p = s_next(m, p, &l);
992 static void s_stop(struct seq_file *m, void *p)
994 struct trace_iterator *iter = m->private;
996 atomic_dec(&trace_record_cmdline_disabled);
998 /* let the tracer release locks here if needed */
999 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1000 iter->trace->stop(iter);
1002 mutex_unlock(&trace_types_lock);
1006 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1008 #ifdef CONFIG_KALLSYMS
1009 char str[KSYM_SYMBOL_LEN];
1011 kallsyms_lookup(address, NULL, NULL, NULL, str);
1013 return trace_seq_printf(s, fmt, str);
1019 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1020 unsigned long address)
1022 #ifdef CONFIG_KALLSYMS
1023 char str[KSYM_SYMBOL_LEN];
1025 sprint_symbol(str, address);
1026 return trace_seq_printf(s, fmt, str);
1031 #ifndef CONFIG_64BIT
1032 # define IP_FMT "%08lx"
1034 # define IP_FMT "%016lx"
1038 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1043 return trace_seq_printf(s, "0");
1045 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1046 ret = seq_print_sym_offset(s, "%s", ip);
1048 ret = seq_print_sym_short(s, "%s", ip);
1053 if (sym_flags & TRACE_ITER_SYM_ADDR)
1054 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1058 static void print_lat_help_header(struct seq_file *m)
1060 seq_puts(m, "# _------=> CPU# \n");
1061 seq_puts(m, "# / _-----=> irqs-off \n");
1062 seq_puts(m, "# | / _----=> need-resched \n");
1063 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1064 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1065 seq_puts(m, "# |||| / \n");
1066 seq_puts(m, "# ||||| delay \n");
1067 seq_puts(m, "# cmd pid ||||| time | caller \n");
1068 seq_puts(m, "# \\ / ||||| \\ | / \n");
1071 static void print_func_help_header(struct seq_file *m)
1073 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1074 seq_puts(m, "# | | | | |\n");
1079 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1081 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1082 struct trace_array *tr = iter->tr;
1083 struct trace_array_cpu *data = tr->data[tr->cpu];
1084 struct tracer *type = current_trace;
1085 unsigned long total = 0;
1086 unsigned long entries = 0;
1088 const char *name = "preemption";
1093 for_each_possible_cpu(cpu) {
1094 if (head_page(tr->data[cpu])) {
1095 total += tr->data[cpu]->trace_idx;
1096 if (tr->data[cpu]->trace_idx > tr->entries)
1097 entries += tr->entries;
1099 entries += tr->data[cpu]->trace_idx;
1103 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1105 seq_puts(m, "-----------------------------------"
1106 "---------------------------------\n");
1107 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1108 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1109 nsecs_to_usecs(data->saved_latency),
1113 #if defined(CONFIG_PREEMPT_NONE)
1115 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1117 #elif defined(CONFIG_PREEMPT_DESKTOP)
1122 /* These are reserved for later use */
1125 seq_printf(m, " #P:%d)\n", num_online_cpus());
1129 seq_puts(m, " -----------------\n");
1130 seq_printf(m, " | task: %.16s-%d "
1131 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1132 data->comm, data->pid, data->uid, data->nice,
1133 data->policy, data->rt_priority);
1134 seq_puts(m, " -----------------\n");
1136 if (data->critical_start) {
1137 seq_puts(m, " => started at: ");
1138 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1139 trace_print_seq(m, &iter->seq);
1140 seq_puts(m, "\n => ended at: ");
1141 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1142 trace_print_seq(m, &iter->seq);
1150 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1152 int hardirq, softirq;
1155 comm = trace_find_cmdline(entry->pid);
1157 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1158 trace_seq_printf(s, "%d", cpu);
1159 trace_seq_printf(s, "%c%c",
1160 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1161 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1163 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1164 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1165 if (hardirq && softirq)
1166 trace_seq_putc(s, 'H');
1169 trace_seq_putc(s, 'h');
1172 trace_seq_putc(s, 's');
1174 trace_seq_putc(s, '.');
1178 if (entry->preempt_count)
1179 trace_seq_printf(s, "%x", entry->preempt_count);
1181 trace_seq_puts(s, ".");
1184 unsigned long preempt_mark_thresh = 100;
1187 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1188 unsigned long rel_usecs)
1190 trace_seq_printf(s, " %4lldus", abs_usecs);
1191 if (rel_usecs > preempt_mark_thresh)
1192 trace_seq_puts(s, "!: ");
1193 else if (rel_usecs > 1)
1194 trace_seq_puts(s, "+: ");
1196 trace_seq_puts(s, " : ");
1199 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1202 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1204 struct trace_seq *s = &iter->seq;
1205 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1206 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1207 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1208 struct trace_entry *entry = iter->ent;
1209 unsigned long abs_usecs;
1210 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 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1245 state_to_char[entry->ctx.prev_state] : 'X';
1246 T = entry->ctx.next_state < sizeof(state_to_char) ?
1247 state_to_char[entry->ctx.next_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 (trace_flags & TRACE_ITER_BIN)
1536 return print_bin_fmt(iter);
1538 if (trace_flags & TRACE_ITER_HEX)
1539 return print_hex_fmt(iter);
1541 if (trace_flags & TRACE_ITER_RAW)
1542 return print_raw_fmt(iter);
1544 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1545 return print_lat_fmt(iter, iter->idx, iter->cpu);
1547 return print_trace_fmt(iter);
1550 static int s_show(struct seq_file *m, void *v)
1552 struct trace_iterator *iter = v;
1554 if (iter->ent == NULL) {
1556 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1559 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1560 /* print nothing if the buffers are empty */
1561 if (trace_empty(iter))
1563 print_trace_header(m, iter);
1564 if (!(trace_flags & TRACE_ITER_VERBOSE))
1565 print_lat_help_header(m);
1567 if (!(trace_flags & TRACE_ITER_VERBOSE))
1568 print_func_help_header(m);
1571 print_trace_line(iter);
1572 trace_print_seq(m, &iter->seq);
1578 static struct seq_operations tracer_seq_ops = {
1585 static struct trace_iterator *
1586 __tracing_open(struct inode *inode, struct file *file, int *ret)
1588 struct trace_iterator *iter;
1590 if (tracing_disabled) {
1595 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1601 mutex_lock(&trace_types_lock);
1602 if (current_trace && current_trace->print_max)
1605 iter->tr = inode->i_private;
1606 iter->trace = current_trace;
1609 /* TODO stop tracer */
1610 *ret = seq_open(file, &tracer_seq_ops);
1612 struct seq_file *m = file->private_data;
1615 /* stop the trace while dumping */
1619 if (iter->trace && iter->trace->open)
1620 iter->trace->open(iter);
1625 mutex_unlock(&trace_types_lock);
1631 int tracing_open_generic(struct inode *inode, struct file *filp)
1633 if (tracing_disabled)
1636 filp->private_data = inode->i_private;
1640 int tracing_release(struct inode *inode, struct file *file)
1642 struct seq_file *m = (struct seq_file *)file->private_data;
1643 struct trace_iterator *iter = m->private;
1645 mutex_lock(&trace_types_lock);
1646 if (iter->trace && iter->trace->close)
1647 iter->trace->close(iter);
1649 /* reenable tracing if it was previously enabled */
1652 mutex_unlock(&trace_types_lock);
1654 seq_release(inode, file);
1659 static int tracing_open(struct inode *inode, struct file *file)
1663 __tracing_open(inode, file, &ret);
1668 static int tracing_lt_open(struct inode *inode, struct file *file)
1670 struct trace_iterator *iter;
1673 iter = __tracing_open(inode, file, &ret);
1676 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1683 t_next(struct seq_file *m, void *v, loff_t *pos)
1685 struct tracer *t = m->private;
1697 static void *t_start(struct seq_file *m, loff_t *pos)
1699 struct tracer *t = m->private;
1702 mutex_lock(&trace_types_lock);
1703 for (; t && l < *pos; t = t_next(m, t, &l))
1709 static void t_stop(struct seq_file *m, void *p)
1711 mutex_unlock(&trace_types_lock);
1714 static int t_show(struct seq_file *m, void *v)
1716 struct tracer *t = v;
1721 seq_printf(m, "%s", t->name);
1730 static struct seq_operations show_traces_seq_ops = {
1737 static int show_traces_open(struct inode *inode, struct file *file)
1741 if (tracing_disabled)
1744 ret = seq_open(file, &show_traces_seq_ops);
1746 struct seq_file *m = file->private_data;
1747 m->private = trace_types;
1753 static struct file_operations tracing_fops = {
1754 .open = tracing_open,
1756 .llseek = seq_lseek,
1757 .release = tracing_release,
1760 static struct file_operations tracing_lt_fops = {
1761 .open = tracing_lt_open,
1763 .llseek = seq_lseek,
1764 .release = tracing_release,
1767 static struct file_operations show_traces_fops = {
1768 .open = show_traces_open,
1770 .release = seq_release,
1774 * Only trace on a CPU if the bitmask is set:
1776 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1779 * When tracing/tracing_cpu_mask is modified then this holds
1780 * the new bitmask we are about to install:
1782 static cpumask_t tracing_cpumask_new;
1785 * The tracer itself will not take this lock, but still we want
1786 * to provide a consistent cpumask to user-space:
1788 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1791 * Temporary storage for the character representation of the
1792 * CPU bitmask (and one more byte for the newline):
1794 static char mask_str[NR_CPUS + 1];
1797 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1798 size_t count, loff_t *ppos)
1802 mutex_lock(&tracing_cpumask_update_lock);
1804 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1805 if (count - len < 2) {
1809 len += sprintf(mask_str + len, "\n");
1810 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1813 mutex_unlock(&tracing_cpumask_update_lock);
1819 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1820 size_t count, loff_t *ppos)
1824 mutex_lock(&tracing_cpumask_update_lock);
1825 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1829 spin_lock_irq(&ftrace_max_lock);
1830 for_each_possible_cpu(cpu) {
1832 * Increase/decrease the disabled counter if we are
1833 * about to flip a bit in the cpumask:
1835 if (cpu_isset(cpu, tracing_cpumask) &&
1836 !cpu_isset(cpu, tracing_cpumask_new)) {
1837 atomic_inc(&global_trace.data[cpu]->disabled);
1839 if (!cpu_isset(cpu, tracing_cpumask) &&
1840 cpu_isset(cpu, tracing_cpumask_new)) {
1841 atomic_dec(&global_trace.data[cpu]->disabled);
1844 spin_unlock_irq(&ftrace_max_lock);
1846 tracing_cpumask = tracing_cpumask_new;
1848 mutex_unlock(&tracing_cpumask_update_lock);
1853 mutex_unlock(&tracing_cpumask_update_lock);
1858 static struct file_operations tracing_cpumask_fops = {
1859 .open = tracing_open_generic,
1860 .read = tracing_cpumask_read,
1861 .write = tracing_cpumask_write,
1865 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1866 size_t cnt, loff_t *ppos)
1873 /* calulate max size */
1874 for (i = 0; trace_options[i]; i++) {
1875 len += strlen(trace_options[i]);
1876 len += 3; /* "no" and space */
1879 /* +2 for \n and \0 */
1880 buf = kmalloc(len + 2, GFP_KERNEL);
1884 for (i = 0; trace_options[i]; i++) {
1885 if (trace_flags & (1 << i))
1886 r += sprintf(buf + r, "%s ", trace_options[i]);
1888 r += sprintf(buf + r, "no%s ", trace_options[i]);
1891 r += sprintf(buf + r, "\n");
1892 WARN_ON(r >= len + 2);
1894 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1902 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1903 size_t cnt, loff_t *ppos)
1913 if (copy_from_user(&buf, ubuf, cnt))
1918 if (strncmp(buf, "no", 2) == 0) {
1923 for (i = 0; trace_options[i]; i++) {
1924 int len = strlen(trace_options[i]);
1926 if (strncmp(cmp, trace_options[i], len) == 0) {
1928 trace_flags &= ~(1 << i);
1930 trace_flags |= (1 << i);
1935 * If no option could be set, return an error:
1937 if (!trace_options[i])
1945 static struct file_operations tracing_iter_fops = {
1946 .open = tracing_open_generic,
1947 .read = tracing_iter_ctrl_read,
1948 .write = tracing_iter_ctrl_write,
1951 static const char readme_msg[] =
1952 "tracing mini-HOWTO:\n\n"
1954 "# mount -t debugfs nodev /debug\n\n"
1955 "# cat /debug/tracing/available_tracers\n"
1956 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1957 "# cat /debug/tracing/current_tracer\n"
1959 "# echo sched_switch > /debug/tracing/current_tracer\n"
1960 "# cat /debug/tracing/current_tracer\n"
1962 "# cat /debug/tracing/iter_ctrl\n"
1963 "noprint-parent nosym-offset nosym-addr noverbose\n"
1964 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1965 "# echo 1 > /debug/tracing/tracing_enabled\n"
1966 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1967 "echo 0 > /debug/tracing/tracing_enabled\n"
1971 tracing_readme_read(struct file *filp, char __user *ubuf,
1972 size_t cnt, loff_t *ppos)
1974 return simple_read_from_buffer(ubuf, cnt, ppos,
1975 readme_msg, strlen(readme_msg));
1978 static struct file_operations tracing_readme_fops = {
1979 .open = tracing_open_generic,
1980 .read = tracing_readme_read,
1984 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1985 size_t cnt, loff_t *ppos)
1987 struct trace_array *tr = filp->private_data;
1991 r = sprintf(buf, "%ld\n", tr->ctrl);
1992 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1996 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1997 size_t cnt, loff_t *ppos)
1999 struct trace_array *tr = filp->private_data;
2006 if (copy_from_user(&buf, ubuf, cnt))
2011 val = simple_strtoul(buf, NULL, 10);
2015 mutex_lock(&trace_types_lock);
2016 if (tr->ctrl ^ val) {
2024 if (current_trace && current_trace->ctrl_update)
2025 current_trace->ctrl_update(tr);
2027 mutex_unlock(&trace_types_lock);
2035 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2036 size_t cnt, loff_t *ppos)
2038 char buf[max_tracer_type_len+2];
2041 mutex_lock(&trace_types_lock);
2043 r = sprintf(buf, "%s\n", current_trace->name);
2045 r = sprintf(buf, "\n");
2046 mutex_unlock(&trace_types_lock);
2048 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2052 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2053 size_t cnt, loff_t *ppos)
2055 struct trace_array *tr = &global_trace;
2057 char buf[max_tracer_type_len+1];
2060 if (cnt > max_tracer_type_len)
2061 cnt = max_tracer_type_len;
2063 if (copy_from_user(&buf, ubuf, cnt))
2068 /* strip ending whitespace. */
2069 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2072 mutex_lock(&trace_types_lock);
2073 for (t = trace_types; t; t = t->next) {
2074 if (strcmp(t->name, buf) == 0)
2077 if (!t || t == current_trace)
2080 if (current_trace && current_trace->reset)
2081 current_trace->reset(tr);
2088 mutex_unlock(&trace_types_lock);
2096 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2097 size_t cnt, loff_t *ppos)
2099 unsigned long *ptr = filp->private_data;
2103 r = snprintf(buf, 64, "%ld\n",
2104 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2107 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2111 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2112 size_t cnt, loff_t *ppos)
2114 long *ptr = filp->private_data;
2121 if (copy_from_user(&buf, ubuf, cnt))
2126 val = simple_strtoul(buf, NULL, 10);
2133 static atomic_t tracing_reader;
2135 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2137 struct trace_iterator *iter;
2139 if (tracing_disabled)
2142 /* We only allow for reader of the pipe */
2143 if (atomic_inc_return(&tracing_reader) != 1) {
2144 atomic_dec(&tracing_reader);
2148 /* create a buffer to store the information to pass to userspace */
2149 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2153 iter->tr = &global_trace;
2155 filp->private_data = iter;
2160 static int tracing_release_pipe(struct inode *inode, struct file *file)
2162 struct trace_iterator *iter = file->private_data;
2165 atomic_dec(&tracing_reader);
2171 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2173 struct trace_iterator *iter = filp->private_data;
2175 if (trace_flags & TRACE_ITER_BLOCK) {
2177 * Always select as readable when in blocking mode
2179 return POLLIN | POLLRDNORM;
2182 if (!trace_empty(iter))
2183 return POLLIN | POLLRDNORM;
2184 poll_wait(filp, &trace_wait, poll_table);
2185 if (!trace_empty(iter))
2186 return POLLIN | POLLRDNORM;
2196 tracing_read_pipe(struct file *filp, char __user *ubuf,
2197 size_t cnt, loff_t *ppos)
2199 struct trace_iterator *iter = filp->private_data;
2200 struct trace_array_cpu *data;
2201 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)) {
2230 if (!(trace_flags & TRACE_ITER_BLOCK))
2231 return -EWOULDBLOCK;
2233 * This is a make-shift waitqueue. The reason we don't use
2234 * an actual wait queue is because:
2235 * 1) we only ever have one waiter
2236 * 2) the tracing, traces all functions, we don't want
2237 * the overhead of calling wake_up and friends
2238 * (and tracing them too)
2239 * Anyway, this is really very primitive wakeup.
2241 set_current_state(TASK_INTERRUPTIBLE);
2242 iter->tr->waiter = current;
2244 /* sleep for one second, and try again. */
2245 schedule_timeout(HZ);
2247 iter->tr->waiter = NULL;
2249 if (signal_pending(current))
2253 * We block until we read something and tracing is disabled.
2254 * We still block if tracing is disabled, but we have never
2255 * read anything. This allows a user to cat this file, and
2256 * then enable tracing. But after we have read something,
2257 * we give an EOF when tracing is again disabled.
2259 * iter->pos will be 0 if we haven't read anything.
2261 if (!tracer_enabled && iter->pos)
2267 /* stop when tracing is finished */
2268 if (trace_empty(iter))
2271 if (cnt >= PAGE_SIZE)
2272 cnt = PAGE_SIZE - 1;
2274 memset(iter, 0, sizeof(*iter));
2275 iter->tr = &global_trace;
2279 * We need to stop all tracing on all CPUS to read the
2280 * the next buffer. This is a bit expensive, but is
2281 * not done often. We fill all what we can read,
2282 * and then release the locks again.
2286 local_irq_save(flags);
2287 #ifdef CONFIG_FTRACE
2288 ftrace_save = ftrace_enabled;
2292 for_each_possible_cpu(cpu) {
2293 data = iter->tr->data[cpu];
2295 if (!head_page(data) || !data->trace_idx)
2298 atomic_inc(&data->disabled);
2302 for_each_cpu_mask(cpu, mask) {
2303 data = iter->tr->data[cpu];
2304 spin_lock(&data->lock);
2307 while (find_next_entry_inc(iter) != NULL) {
2308 int len = iter->seq.len;
2310 ret = print_trace_line(iter);
2312 /* don't print partial lines */
2313 iter->seq.len = len;
2317 trace_consume(iter);
2319 if (iter->seq.len >= cnt)
2323 for_each_cpu_mask(cpu, mask) {
2324 data = iter->tr->data[cpu];
2325 spin_unlock(&data->lock);
2328 for_each_cpu_mask(cpu, mask) {
2329 data = iter->tr->data[cpu];
2330 atomic_dec(&data->disabled);
2332 #ifdef CONFIG_FTRACE
2333 ftrace_enabled = ftrace_save;
2335 local_irq_restore(flags);
2337 /* Now copy what we have to the user */
2338 read = iter->seq.len;
2342 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2344 if (read < iter->seq.len)
2347 trace_seq_reset(&iter->seq);
2355 static struct file_operations tracing_max_lat_fops = {
2356 .open = tracing_open_generic,
2357 .read = tracing_max_lat_read,
2358 .write = tracing_max_lat_write,
2361 static struct file_operations tracing_ctrl_fops = {
2362 .open = tracing_open_generic,
2363 .read = tracing_ctrl_read,
2364 .write = tracing_ctrl_write,
2367 static struct file_operations set_tracer_fops = {
2368 .open = tracing_open_generic,
2369 .read = tracing_set_trace_read,
2370 .write = tracing_set_trace_write,
2373 static struct file_operations tracing_pipe_fops = {
2374 .open = tracing_open_pipe,
2375 .poll = tracing_poll_pipe,
2376 .read = tracing_read_pipe,
2377 .release = tracing_release_pipe,
2380 #ifdef CONFIG_DYNAMIC_FTRACE
2383 tracing_read_long(struct file *filp, char __user *ubuf,
2384 size_t cnt, loff_t *ppos)
2386 unsigned long *p = filp->private_data;
2390 r = sprintf(buf, "%ld\n", *p);
2392 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2395 static struct file_operations tracing_read_long_fops = {
2396 .open = tracing_open_generic,
2397 .read = tracing_read_long,
2401 static struct dentry *d_tracer;
2403 struct dentry *tracing_init_dentry(void)
2410 d_tracer = debugfs_create_dir("tracing", NULL);
2412 if (!d_tracer && !once) {
2414 pr_warning("Could not create debugfs directory 'tracing'\n");
2421 #ifdef CONFIG_FTRACE_SELFTEST
2422 /* Let selftest have access to static functions in this file */
2423 #include "trace_selftest.c"
2426 static __init void tracer_init_debugfs(void)
2428 struct dentry *d_tracer;
2429 struct dentry *entry;
2431 d_tracer = tracing_init_dentry();
2433 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2434 &global_trace, &tracing_ctrl_fops);
2436 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2438 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2439 NULL, &tracing_iter_fops);
2441 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2443 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2444 NULL, &tracing_cpumask_fops);
2446 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2448 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2449 &global_trace, &tracing_lt_fops);
2451 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2453 entry = debugfs_create_file("trace", 0444, d_tracer,
2454 &global_trace, &tracing_fops);
2456 pr_warning("Could not create debugfs 'trace' entry\n");
2458 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2459 &global_trace, &show_traces_fops);
2461 pr_warning("Could not create debugfs 'trace' entry\n");
2463 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2464 &global_trace, &set_tracer_fops);
2466 pr_warning("Could not create debugfs 'trace' entry\n");
2468 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2469 &tracing_max_latency,
2470 &tracing_max_lat_fops);
2472 pr_warning("Could not create debugfs "
2473 "'tracing_max_latency' entry\n");
2475 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2476 &tracing_thresh, &tracing_max_lat_fops);
2478 pr_warning("Could not create debugfs "
2479 "'tracing_threash' entry\n");
2480 entry = debugfs_create_file("README", 0644, d_tracer,
2481 NULL, &tracing_readme_fops);
2483 pr_warning("Could not create debugfs 'README' entry\n");
2485 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2486 NULL, &tracing_pipe_fops);
2488 pr_warning("Could not create debugfs "
2489 "'tracing_threash' entry\n");
2491 #ifdef CONFIG_DYNAMIC_FTRACE
2492 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2493 &ftrace_update_tot_cnt,
2494 &tracing_read_long_fops);
2496 pr_warning("Could not create debugfs "
2497 "'dyn_ftrace_total_info' entry\n");
2501 /* dummy trace to disable tracing */
2502 static struct tracer no_tracer __read_mostly =
2507 static int trace_alloc_page(void)
2509 struct trace_array_cpu *data;
2510 struct page *page, *tmp;
2515 /* first allocate a page for each CPU */
2516 for_each_possible_cpu(i) {
2517 array = (void *)__get_free_page(GFP_KERNEL);
2518 if (array == NULL) {
2519 printk(KERN_ERR "tracer: failed to allocate page"
2520 "for trace buffer!\n");
2524 page = virt_to_page(array);
2525 list_add(&page->lru, &pages);
2527 /* Only allocate if we are actually using the max trace */
2528 #ifdef CONFIG_TRACER_MAX_TRACE
2529 array = (void *)__get_free_page(GFP_KERNEL);
2530 if (array == NULL) {
2531 printk(KERN_ERR "tracer: failed to allocate page"
2532 "for trace buffer!\n");
2535 page = virt_to_page(array);
2536 list_add(&page->lru, &pages);
2540 /* Now that we successfully allocate a page per CPU, add them */
2541 for_each_possible_cpu(i) {
2542 data = global_trace.data[i];
2543 spin_lock_init(&data->lock);
2544 lockdep_set_class(&data->lock, &data->lock_key);
2545 page = list_entry(pages.next, struct page, lru);
2546 list_del_init(&page->lru);
2547 list_add_tail(&page->lru, &data->trace_pages);
2550 #ifdef CONFIG_TRACER_MAX_TRACE
2551 data = max_tr.data[i];
2552 spin_lock_init(&data->lock);
2553 lockdep_set_class(&data->lock, &data->lock_key);
2554 page = list_entry(pages.next, struct page, lru);
2555 list_del_init(&page->lru);
2556 list_add_tail(&page->lru, &data->trace_pages);
2560 global_trace.entries += ENTRIES_PER_PAGE;
2565 list_for_each_entry_safe(page, tmp, &pages, lru) {
2566 list_del_init(&page->lru);
2572 __init static int tracer_alloc_buffers(void)
2574 struct trace_array_cpu *data;
2581 global_trace.ctrl = tracer_enabled;
2583 /* Allocate the first page for all buffers */
2584 for_each_possible_cpu(i) {
2585 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2586 max_tr.data[i] = &per_cpu(max_data, i);
2588 array = (void *)__get_free_page(GFP_KERNEL);
2589 if (array == NULL) {
2590 printk(KERN_ERR "tracer: failed to allocate page"
2591 "for trace buffer!\n");
2595 /* set the array to the list */
2596 INIT_LIST_HEAD(&data->trace_pages);
2597 page = virt_to_page(array);
2598 list_add(&page->lru, &data->trace_pages);
2599 /* use the LRU flag to differentiate the two buffers */
2602 /* Only allocate if we are actually using the max trace */
2603 #ifdef CONFIG_TRACER_MAX_TRACE
2604 array = (void *)__get_free_page(GFP_KERNEL);
2605 if (array == NULL) {
2606 printk(KERN_ERR "tracer: failed to allocate page"
2607 "for trace buffer!\n");
2611 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2612 page = virt_to_page(array);
2613 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2619 * Since we allocate by orders of pages, we may be able to
2622 global_trace.entries = ENTRIES_PER_PAGE;
2625 while (global_trace.entries < trace_nr_entries) {
2626 if (trace_alloc_page())
2630 max_tr.entries = global_trace.entries;
2632 pr_info("tracer: %d pages allocated for %ld",
2633 pages, trace_nr_entries);
2634 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2635 pr_info(" actual entries %ld\n", global_trace.entries);
2637 tracer_init_debugfs();
2639 trace_init_cmdlines();
2641 register_tracer(&no_tracer);
2642 current_trace = &no_tracer;
2644 /* All seems OK, enable tracing */
2645 tracing_disabled = 0;
2650 for (i-- ; i >= 0; i--) {
2651 struct page *page, *tmp;
2652 struct trace_array_cpu *data = global_trace.data[i];
2655 list_for_each_entry_safe(page, tmp,
2656 &data->trace_pages, lru) {
2657 list_del_init(&page->lru);
2662 #ifdef CONFIG_TRACER_MAX_TRACE
2663 data = max_tr.data[i];
2665 list_for_each_entry_safe(page, tmp,
2666 &data->trace_pages, lru) {
2667 list_del_init(&page->lru);
2675 fs_initcall(tracer_alloc_buffers);