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/notifier.h>
18 #include <linux/debugfs.h>
19 #include <linux/pagemap.h>
20 #include <linux/hardirq.h>
21 #include <linux/linkage.h>
22 #include <linux/uaccess.h>
23 #include <linux/ftrace.h>
24 #include <linux/module.h>
25 #include <linux/percpu.h>
26 #include <linux/kdebug.h>
27 #include <linux/ctype.h>
28 #include <linux/init.h>
29 #include <linux/poll.h>
30 #include <linux/gfp.h>
32 #include <linux/kprobes.h>
33 #include <linux/writeback.h>
35 #include <linux/stacktrace.h>
39 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
40 unsigned long __read_mostly tracing_thresh;
42 static unsigned long __read_mostly tracing_nr_buffers;
43 static cpumask_t __read_mostly tracing_buffer_mask;
45 #define for_each_tracing_cpu(cpu) \
46 for_each_cpu_mask(cpu, tracing_buffer_mask)
48 static int trace_alloc_page(void);
49 static int trace_free_page(void);
51 static int tracing_disabled = 1;
53 static unsigned long tracing_pages_allocated;
56 ns2usecs(cycle_t nsec)
63 cycle_t ftrace_now(int cpu)
65 return cpu_clock(cpu);
69 * The global_trace is the descriptor that holds the tracing
70 * buffers for the live tracing. For each CPU, it contains
71 * a link list of pages that will store trace entries. The
72 * page descriptor of the pages in the memory is used to hold
73 * the link list by linking the lru item in the page descriptor
74 * to each of the pages in the buffer per CPU.
76 * For each active CPU there is a data field that holds the
77 * pages for the buffer for that CPU. Each CPU has the same number
78 * of pages allocated for its buffer.
80 static struct trace_array global_trace;
82 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
85 * The max_tr is used to snapshot the global_trace when a maximum
86 * latency is reached. Some tracers will use this to store a maximum
87 * trace while it continues examining live traces.
89 * The buffers for the max_tr are set up the same as the global_trace.
90 * When a snapshot is taken, the link list of the max_tr is swapped
91 * with the link list of the global_trace and the buffers are reset for
92 * the global_trace so the tracing can continue.
94 static struct trace_array max_tr;
96 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
98 /* tracer_enabled is used to toggle activation of a tracer */
99 static int tracer_enabled = 1;
101 /* function tracing enabled */
102 int ftrace_function_enabled;
105 * trace_nr_entries is the number of entries that is allocated
106 * for a buffer. Note, the number of entries is always rounded
107 * to ENTRIES_PER_PAGE.
109 * This number is purposely set to a low number of 16384.
110 * If the dump on oops happens, it will be much appreciated
111 * to not have to wait for all that output. Anyway this can be
112 * boot time and run time configurable.
114 #define TRACE_ENTRIES_DEFAULT 16384UL
116 static unsigned long trace_nr_entries = TRACE_ENTRIES_DEFAULT;
118 /* trace_types holds a link list of available tracers. */
119 static struct tracer *trace_types __read_mostly;
121 /* current_trace points to the tracer that is currently active */
122 static struct tracer *current_trace __read_mostly;
125 * max_tracer_type_len is used to simplify the allocating of
126 * buffers to read userspace tracer names. We keep track of
127 * the longest tracer name registered.
129 static int max_tracer_type_len;
132 * trace_types_lock is used to protect the trace_types list.
133 * This lock is also used to keep user access serialized.
134 * Accesses from userspace will grab this lock while userspace
135 * activities happen inside the kernel.
137 static DEFINE_MUTEX(trace_types_lock);
139 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
140 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
142 /* trace_flags holds iter_ctrl options */
143 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
146 * trace_wake_up - wake up tasks waiting for trace input
148 * Simply wakes up any task that is blocked on the trace_wait
149 * queue. These is used with trace_poll for tasks polling the trace.
151 void trace_wake_up(void)
154 * The runqueue_is_locked() can fail, but this is the best we
157 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
158 wake_up(&trace_wait);
161 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
163 static int __init set_nr_entries(char *str)
165 unsigned long nr_entries;
170 ret = strict_strtoul(str, 0, &nr_entries);
171 /* nr_entries can not be zero */
172 if (ret < 0 || nr_entries == 0)
174 trace_nr_entries = nr_entries;
177 __setup("trace_entries=", set_nr_entries);
179 unsigned long nsecs_to_usecs(unsigned long nsecs)
185 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
186 * control the output of kernel symbols.
188 #define TRACE_ITER_SYM_MASK \
189 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
191 /* These must match the bit postions in trace_iterator_flags */
192 static const char *trace_options[] = {
208 * ftrace_max_lock is used to protect the swapping of buffers
209 * when taking a max snapshot. The buffers themselves are
210 * protected by per_cpu spinlocks. But the action of the swap
211 * needs its own lock.
213 * This is defined as a raw_spinlock_t in order to help
214 * with performance when lockdep debugging is enabled.
216 static raw_spinlock_t ftrace_max_lock =
217 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
220 * Copy the new maximum trace into the separate maximum-trace
221 * structure. (this way the maximum trace is permanently saved,
222 * for later retrieval via /debugfs/tracing/latency_trace)
225 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
227 struct trace_array_cpu *data = tr->data[cpu];
230 max_tr.time_start = data->preempt_timestamp;
232 data = max_tr.data[cpu];
233 data->saved_latency = tracing_max_latency;
235 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
236 data->pid = tsk->pid;
237 data->uid = tsk->uid;
238 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
239 data->policy = tsk->policy;
240 data->rt_priority = tsk->rt_priority;
242 /* record this tasks comm */
243 tracing_record_cmdline(current);
246 #define CHECK_COND(cond) \
247 if (unlikely(cond)) { \
248 tracing_disabled = 1; \
254 * check_pages - integrity check of trace buffers
256 * As a safty measure we check to make sure the data pages have not
259 int check_pages(struct trace_array_cpu *data)
261 struct page *page, *tmp;
263 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
264 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
266 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
267 CHECK_COND(page->lru.next->prev != &page->lru);
268 CHECK_COND(page->lru.prev->next != &page->lru);
275 * head_page - page address of the first page in per_cpu buffer.
277 * head_page returns the page address of the first page in
278 * a per_cpu buffer. This also preforms various consistency
279 * checks to make sure the buffer has not been corrupted.
281 void *head_page(struct trace_array_cpu *data)
285 if (list_empty(&data->trace_pages))
288 page = list_entry(data->trace_pages.next, struct page, lru);
289 BUG_ON(&page->lru == &data->trace_pages);
291 return page_address(page);
295 * trace_seq_printf - sequence printing of trace information
296 * @s: trace sequence descriptor
297 * @fmt: printf format string
299 * The tracer may use either sequence operations or its own
300 * copy to user routines. To simplify formating of a trace
301 * trace_seq_printf is used to store strings into a special
302 * buffer (@s). Then the output may be either used by
303 * the sequencer or pulled into another buffer.
306 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
308 int len = (PAGE_SIZE - 1) - s->len;
316 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
319 /* If we can't write it all, don't bother writing anything */
329 * trace_seq_puts - trace sequence printing of simple string
330 * @s: trace sequence descriptor
331 * @str: simple string to record
333 * The tracer may use either the sequence operations or its own
334 * copy to user routines. This function records a simple string
335 * into a special buffer (@s) for later retrieval by a sequencer
336 * or other mechanism.
339 trace_seq_puts(struct trace_seq *s, const char *str)
341 int len = strlen(str);
343 if (len > ((PAGE_SIZE - 1) - s->len))
346 memcpy(s->buffer + s->len, str, len);
353 trace_seq_putc(struct trace_seq *s, unsigned char c)
355 if (s->len >= (PAGE_SIZE - 1))
358 s->buffer[s->len++] = c;
364 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
366 if (len > ((PAGE_SIZE - 1) - s->len))
369 memcpy(s->buffer + s->len, mem, len);
376 static const char hex2asc[] = "0123456789abcdef";
379 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
381 unsigned char hex[HEX_CHARS];
382 unsigned char *data = mem;
386 BUG_ON(len >= HEX_CHARS);
389 for (i = 0, j = 0; i < len; i++) {
391 for (i = len-1, j = 0; i >= 0; i--) {
395 hex[j++] = hex2asc[byte & 0x0f];
396 hex[j++] = hex2asc[byte >> 4];
400 return trace_seq_putmem(s, hex, j);
404 trace_seq_reset(struct trace_seq *s)
410 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
415 if (s->len <= s->readpos)
418 len = s->len - s->readpos;
421 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
430 trace_print_seq(struct seq_file *m, struct trace_seq *s)
432 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
435 seq_puts(m, s->buffer);
441 * flip the trace buffers between two trace descriptors.
442 * This usually is the buffers between the global_trace and
443 * the max_tr to record a snapshot of a current trace.
445 * The ftrace_max_lock must be held.
448 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
450 struct list_head flip_pages;
452 INIT_LIST_HEAD(&flip_pages);
454 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
455 sizeof(struct trace_array_cpu) -
456 offsetof(struct trace_array_cpu, trace_head_idx));
460 list_splice_init(&tr1->trace_pages, &flip_pages);
461 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
462 list_splice_init(&flip_pages, &tr2->trace_pages);
463 BUG_ON(!list_empty(&flip_pages));
469 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
471 * @tsk: the task with the latency
472 * @cpu: The cpu that initiated the trace.
474 * Flip the buffers between the @tr and the max_tr and record information
475 * about which task was the cause of this latency.
478 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
480 struct trace_array_cpu *data;
483 WARN_ON_ONCE(!irqs_disabled());
484 __raw_spin_lock(&ftrace_max_lock);
485 /* clear out all the previous traces */
486 for_each_tracing_cpu(i) {
488 flip_trace(max_tr.data[i], data);
492 __update_max_tr(tr, tsk, cpu);
493 __raw_spin_unlock(&ftrace_max_lock);
497 * update_max_tr_single - only copy one trace over, and reset the rest
499 * @tsk - task with the latency
500 * @cpu - the cpu of the buffer to copy.
502 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
505 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
507 struct trace_array_cpu *data = tr->data[cpu];
510 WARN_ON_ONCE(!irqs_disabled());
511 __raw_spin_lock(&ftrace_max_lock);
512 for_each_tracing_cpu(i)
513 tracing_reset(max_tr.data[i]);
515 flip_trace(max_tr.data[cpu], data);
518 __update_max_tr(tr, tsk, cpu);
519 __raw_spin_unlock(&ftrace_max_lock);
523 * register_tracer - register a tracer with the ftrace system.
524 * @type - the plugin for the tracer
526 * Register a new plugin tracer.
528 int register_tracer(struct tracer *type)
535 pr_info("Tracer must have a name\n");
539 mutex_lock(&trace_types_lock);
540 for (t = trace_types; t; t = t->next) {
541 if (strcmp(type->name, t->name) == 0) {
543 pr_info("Trace %s already registered\n",
550 #ifdef CONFIG_FTRACE_STARTUP_TEST
551 if (type->selftest) {
552 struct tracer *saved_tracer = current_trace;
553 struct trace_array_cpu *data;
554 struct trace_array *tr = &global_trace;
555 int saved_ctrl = tr->ctrl;
558 * Run a selftest on this tracer.
559 * Here we reset the trace buffer, and set the current
560 * tracer to be this tracer. The tracer can then run some
561 * internal tracing to verify that everything is in order.
562 * If we fail, we do not register this tracer.
564 for_each_tracing_cpu(i) {
566 if (!head_page(data))
570 current_trace = type;
572 /* the test is responsible for initializing and enabling */
573 pr_info("Testing tracer %s: ", type->name);
574 ret = type->selftest(type, tr);
575 /* the test is responsible for resetting too */
576 current_trace = saved_tracer;
577 tr->ctrl = saved_ctrl;
579 printk(KERN_CONT "FAILED!\n");
582 /* Only reset on passing, to avoid touching corrupted buffers */
583 for_each_tracing_cpu(i) {
585 if (!head_page(data))
589 printk(KERN_CONT "PASSED\n");
593 type->next = trace_types;
595 len = strlen(type->name);
596 if (len > max_tracer_type_len)
597 max_tracer_type_len = len;
600 mutex_unlock(&trace_types_lock);
605 void unregister_tracer(struct tracer *type)
610 mutex_lock(&trace_types_lock);
611 for (t = &trace_types; *t; t = &(*t)->next) {
615 pr_info("Trace %s not registered\n", type->name);
620 if (strlen(type->name) != max_tracer_type_len)
623 max_tracer_type_len = 0;
624 for (t = &trace_types; *t; t = &(*t)->next) {
625 len = strlen((*t)->name);
626 if (len > max_tracer_type_len)
627 max_tracer_type_len = len;
630 mutex_unlock(&trace_types_lock);
633 void tracing_reset(struct trace_array_cpu *data)
637 data->trace_head = data->trace_tail = head_page(data);
638 data->trace_head_idx = 0;
639 data->trace_tail_idx = 0;
642 #define SAVED_CMDLINES 128
643 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
644 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
645 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
646 static int cmdline_idx;
647 static DEFINE_SPINLOCK(trace_cmdline_lock);
649 /* temporary disable recording */
650 atomic_t trace_record_cmdline_disabled __read_mostly;
652 static void trace_init_cmdlines(void)
654 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
655 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
659 void trace_stop_cmdline_recording(void);
661 static void trace_save_cmdline(struct task_struct *tsk)
666 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
670 * It's not the end of the world if we don't get
671 * the lock, but we also don't want to spin
672 * nor do we want to disable interrupts,
673 * so if we miss here, then better luck next time.
675 if (!spin_trylock(&trace_cmdline_lock))
678 idx = map_pid_to_cmdline[tsk->pid];
679 if (idx >= SAVED_CMDLINES) {
680 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
682 map = map_cmdline_to_pid[idx];
683 if (map <= PID_MAX_DEFAULT)
684 map_pid_to_cmdline[map] = (unsigned)-1;
686 map_pid_to_cmdline[tsk->pid] = idx;
691 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
693 spin_unlock(&trace_cmdline_lock);
696 static char *trace_find_cmdline(int pid)
698 char *cmdline = "<...>";
704 if (pid > PID_MAX_DEFAULT)
707 map = map_pid_to_cmdline[pid];
708 if (map >= SAVED_CMDLINES)
711 cmdline = saved_cmdlines[map];
717 void tracing_record_cmdline(struct task_struct *tsk)
719 if (atomic_read(&trace_record_cmdline_disabled))
722 trace_save_cmdline(tsk);
725 static inline struct list_head *
726 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
729 * Roundrobin - but skip the head (which is not a real page):
732 if (unlikely(next == &data->trace_pages))
734 BUG_ON(next == &data->trace_pages);
740 trace_next_page(struct trace_array_cpu *data, void *addr)
742 struct list_head *next;
745 page = virt_to_page(addr);
747 next = trace_next_list(data, &page->lru);
748 page = list_entry(next, struct page, lru);
750 return page_address(page);
754 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
756 unsigned long idx, idx_next;
757 struct trace_entry *entry;
760 idx = data->trace_head_idx;
763 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
765 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
767 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
768 data->trace_head = trace_next_page(data, data->trace_head);
772 if (data->trace_head == data->trace_tail &&
773 idx_next == data->trace_tail_idx) {
776 data->trace_tail_idx++;
777 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
779 trace_next_page(data, data->trace_tail);
780 data->trace_tail_idx = 0;
784 data->trace_head_idx = idx_next;
790 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
792 struct task_struct *tsk = current;
795 pc = preempt_count();
797 entry->field.preempt_count = pc & 0xff;
798 entry->field.pid = (tsk) ? tsk->pid : 0;
799 entry->field.t = ftrace_now(raw_smp_processor_id());
801 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
802 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
803 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
804 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
808 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
809 unsigned long ip, unsigned long parent_ip, unsigned long flags)
811 struct trace_entry *entry;
812 unsigned long irq_flags;
814 raw_local_irq_save(irq_flags);
815 __raw_spin_lock(&data->lock);
816 entry = tracing_get_trace_entry(tr, data);
817 tracing_generic_entry_update(entry, flags);
818 entry->type = TRACE_FN;
819 entry->field.fn.ip = ip;
820 entry->field.fn.parent_ip = parent_ip;
821 __raw_spin_unlock(&data->lock);
822 raw_local_irq_restore(irq_flags);
826 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
827 unsigned long ip, unsigned long parent_ip, unsigned long flags)
829 if (likely(!atomic_read(&data->disabled)))
830 trace_function(tr, data, ip, parent_ip, flags);
833 void __trace_stack(struct trace_array *tr,
834 struct trace_array_cpu *data,
838 struct trace_entry *entry;
839 struct stack_trace trace;
841 if (!(trace_flags & TRACE_ITER_STACKTRACE))
844 entry = tracing_get_trace_entry(tr, data);
845 tracing_generic_entry_update(entry, flags);
846 entry->type = TRACE_STACK;
848 memset(&entry->field.stack, 0, sizeof(entry->field.stack));
850 trace.nr_entries = 0;
851 trace.max_entries = FTRACE_STACK_ENTRIES;
853 trace.entries = entry->field.stack.caller;
855 save_stack_trace(&trace);
859 __trace_special(void *__tr, void *__data,
860 unsigned long arg1, unsigned long arg2, unsigned long arg3)
862 struct trace_array_cpu *data = __data;
863 struct trace_array *tr = __tr;
864 struct trace_entry *entry;
865 unsigned long irq_flags;
867 raw_local_irq_save(irq_flags);
868 __raw_spin_lock(&data->lock);
869 entry = tracing_get_trace_entry(tr, data);
870 tracing_generic_entry_update(entry, 0);
871 entry->type = TRACE_SPECIAL;
872 entry->field.special.arg1 = arg1;
873 entry->field.special.arg2 = arg2;
874 entry->field.special.arg3 = arg3;
875 __trace_stack(tr, data, irq_flags, 4);
876 __raw_spin_unlock(&data->lock);
877 raw_local_irq_restore(irq_flags);
883 tracing_sched_switch_trace(struct trace_array *tr,
884 struct trace_array_cpu *data,
885 struct task_struct *prev,
886 struct task_struct *next,
889 struct trace_entry *entry;
890 unsigned long irq_flags;
892 raw_local_irq_save(irq_flags);
893 __raw_spin_lock(&data->lock);
894 entry = tracing_get_trace_entry(tr, data);
895 tracing_generic_entry_update(entry, flags);
896 entry->type = TRACE_CTX;
897 entry->field.ctx.prev_pid = prev->pid;
898 entry->field.ctx.prev_prio = prev->prio;
899 entry->field.ctx.prev_state = prev->state;
900 entry->field.ctx.next_pid = next->pid;
901 entry->field.ctx.next_prio = next->prio;
902 entry->field.ctx.next_state = next->state;
903 entry->field.ctx.next_cpu = task_cpu(next);
904 __trace_stack(tr, data, flags, 5);
905 __raw_spin_unlock(&data->lock);
906 raw_local_irq_restore(irq_flags);
910 tracing_sched_wakeup_trace(struct trace_array *tr,
911 struct trace_array_cpu *data,
912 struct task_struct *wakee,
913 struct task_struct *curr,
916 struct trace_entry *entry;
917 unsigned long irq_flags;
919 raw_local_irq_save(irq_flags);
920 __raw_spin_lock(&data->lock);
921 entry = tracing_get_trace_entry(tr, data);
922 tracing_generic_entry_update(entry, flags);
923 entry->type = TRACE_WAKE;
924 entry->field.ctx.prev_pid = curr->pid;
925 entry->field.ctx.prev_prio = curr->prio;
926 entry->field.ctx.prev_state = curr->state;
927 entry->field.ctx.next_pid = wakee->pid;
928 entry->field.ctx.next_prio = wakee->prio;
929 entry->field.ctx.next_state = wakee->state;
930 entry->field.ctx.next_cpu = task_cpu(wakee);
931 __trace_stack(tr, data, flags, 6);
932 __raw_spin_unlock(&data->lock);
933 raw_local_irq_restore(irq_flags);
939 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
941 struct trace_array *tr = &global_trace;
942 struct trace_array_cpu *data;
947 if (tracing_disabled || !tr->ctrl)
950 local_irq_save(flags);
951 cpu = raw_smp_processor_id();
952 data = tr->data[cpu];
953 disabled = atomic_inc_return(&data->disabled);
955 if (likely(disabled == 1))
956 __trace_special(tr, data, arg1, arg2, arg3);
958 atomic_dec(&data->disabled);
959 local_irq_restore(flags);
964 function_trace_call(unsigned long ip, unsigned long parent_ip)
966 struct trace_array *tr = &global_trace;
967 struct trace_array_cpu *data;
972 if (unlikely(!ftrace_function_enabled))
978 local_irq_save(flags);
979 cpu = raw_smp_processor_id();
980 data = tr->data[cpu];
981 disabled = atomic_inc_return(&data->disabled);
983 if (likely(disabled == 1))
984 trace_function(tr, data, ip, parent_ip, flags);
986 atomic_dec(&data->disabled);
987 local_irq_restore(flags);
990 static struct ftrace_ops trace_ops __read_mostly =
992 .func = function_trace_call,
995 void tracing_start_function_trace(void)
997 ftrace_function_enabled = 0;
998 register_ftrace_function(&trace_ops);
1000 ftrace_function_enabled = 1;
1003 void tracing_stop_function_trace(void)
1005 ftrace_function_enabled = 0;
1006 unregister_ftrace_function(&trace_ops);
1010 enum trace_file_type {
1011 TRACE_FILE_LAT_FMT = 1,
1014 /* Return the current entry. */
1015 static struct trace_entry *
1016 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1017 struct trace_iterator *iter, int cpu)
1020 struct trace_entry *array;
1022 if (iter->next_idx[cpu] >= tr->entries ||
1023 iter->next_idx[cpu] >= data->trace_idx ||
1024 (data->trace_head == data->trace_tail &&
1025 data->trace_head_idx == data->trace_tail_idx))
1028 if (!iter->next_page[cpu]) {
1029 /* Initialize the iterator for this cpu trace buffer */
1030 WARN_ON(!data->trace_tail);
1031 page = virt_to_page(data->trace_tail);
1032 iter->next_page[cpu] = &page->lru;
1033 iter->next_page_idx[cpu] = data->trace_tail_idx;
1036 page = list_entry(iter->next_page[cpu], struct page, lru);
1037 BUG_ON(&data->trace_pages == &page->lru);
1039 array = page_address(page);
1041 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
1042 return &array[iter->next_page_idx[cpu]];
1045 /* Increment the index counter of an iterator by one */
1046 static void __trace_iterator_increment(struct trace_iterator *iter, int cpu)
1048 iter->next_idx[cpu]++;
1049 iter->next_page_idx[cpu]++;
1051 if (iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE) {
1052 struct trace_array_cpu *data = iter->tr->data[cpu];
1054 iter->next_page_idx[cpu] = 0;
1055 iter->next_page[cpu] =
1056 trace_next_list(data, iter->next_page[cpu]);
1060 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1063 __trace_iterator_increment(iter, cpu);
1066 static struct trace_entry *
1067 trace_entry_next(struct trace_array *tr, struct trace_array_cpu *data,
1068 struct trace_iterator *iter, int cpu)
1070 struct list_head *next_page;
1071 struct trace_entry *ent;
1072 int idx, next_idx, next_page_idx;
1074 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1076 if (likely(!ent || ent->type != TRACE_CONT))
1079 /* save the iterator details */
1081 next_idx = iter->next_idx[cpu];
1082 next_page_idx = iter->next_page_idx[cpu];
1083 next_page = iter->next_page[cpu];
1085 /* find a real entry */
1087 __trace_iterator_increment(iter, cpu);
1088 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1089 } while (ent && ent->type != TRACE_CONT);
1091 /* reset the iterator */
1093 iter->next_idx[cpu] = next_idx;
1094 iter->next_page_idx[cpu] = next_page_idx;
1095 iter->next_page[cpu] = next_page;
1100 static struct trace_entry *
1101 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, int inc)
1103 struct trace_array *tr = iter->tr;
1104 struct trace_entry *ent, *next = NULL;
1108 for_each_tracing_cpu(cpu) {
1109 if (!head_page(tr->data[cpu]))
1112 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1114 if (ent && ent->type == TRACE_CONT) {
1115 struct trace_array_cpu *data = tr->data[cpu];
1118 ent = trace_entry_next(tr, data, iter, cpu);
1120 while (ent && ent->type == TRACE_CONT) {
1121 __trace_iterator_increment(iter, cpu);
1122 ent = trace_entry_idx(tr, tr->data[cpu],
1129 * Pick the entry with the smallest timestamp:
1131 if (ent && (!next || ent->field.t < next->field.t)) {
1138 *ent_cpu = next_cpu;
1143 /* Find the next real entry, without updating the iterator itself */
1144 static struct trace_entry *
1145 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1147 return __find_next_entry(iter, ent_cpu, 0);
1150 /* Find the next real entry, and increment the iterator to the next entry */
1151 static void *find_next_entry_inc(struct trace_iterator *iter)
1153 struct trace_entry *next;
1156 next = __find_next_entry(iter, &next_cpu, 1);
1158 iter->prev_ent = iter->ent;
1159 iter->prev_cpu = iter->cpu;
1162 iter->cpu = next_cpu;
1165 trace_iterator_increment(iter, iter->cpu);
1167 return next ? iter : NULL;
1170 static void trace_consume(struct trace_iterator *iter)
1172 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1173 struct trace_entry *ent;
1176 data->trace_tail_idx++;
1177 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1178 data->trace_tail = trace_next_page(data, data->trace_tail);
1179 data->trace_tail_idx = 0;
1182 /* Check if we empty it, then reset the index */
1183 if (data->trace_head == data->trace_tail &&
1184 data->trace_head_idx == data->trace_tail_idx)
1185 data->trace_idx = 0;
1187 ent = trace_entry_idx(iter->tr, iter->tr->data[iter->cpu],
1189 if (ent && ent->type == TRACE_CONT)
1193 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1195 struct trace_iterator *iter = m->private;
1201 /* can't go backwards */
1206 ent = find_next_entry_inc(iter);
1210 while (ent && iter->idx < i)
1211 ent = find_next_entry_inc(iter);
1218 static void *s_start(struct seq_file *m, loff_t *pos)
1220 struct trace_iterator *iter = m->private;
1225 mutex_lock(&trace_types_lock);
1227 if (!current_trace || current_trace != iter->trace) {
1228 mutex_unlock(&trace_types_lock);
1232 atomic_inc(&trace_record_cmdline_disabled);
1234 /* let the tracer grab locks here if needed */
1235 if (current_trace->start)
1236 current_trace->start(iter);
1238 if (*pos != iter->pos) {
1242 iter->prev_ent = NULL;
1243 iter->prev_cpu = -1;
1245 for_each_tracing_cpu(i) {
1246 iter->next_idx[i] = 0;
1247 iter->next_page[i] = NULL;
1250 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1255 p = s_next(m, p, &l);
1261 static void s_stop(struct seq_file *m, void *p)
1263 struct trace_iterator *iter = m->private;
1265 atomic_dec(&trace_record_cmdline_disabled);
1267 /* let the tracer release locks here if needed */
1268 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1269 iter->trace->stop(iter);
1271 mutex_unlock(&trace_types_lock);
1274 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1276 #ifdef CONFIG_KRETPROBES
1277 static inline int kretprobed(unsigned long addr)
1279 return addr == (unsigned long)kretprobe_trampoline;
1282 static inline int kretprobed(unsigned long addr)
1286 #endif /* CONFIG_KRETPROBES */
1289 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1291 #ifdef CONFIG_KALLSYMS
1292 char str[KSYM_SYMBOL_LEN];
1294 kallsyms_lookup(address, NULL, NULL, NULL, str);
1296 return trace_seq_printf(s, fmt, str);
1302 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1303 unsigned long address)
1305 #ifdef CONFIG_KALLSYMS
1306 char str[KSYM_SYMBOL_LEN];
1308 sprint_symbol(str, address);
1309 return trace_seq_printf(s, fmt, str);
1314 #ifndef CONFIG_64BIT
1315 # define IP_FMT "%08lx"
1317 # define IP_FMT "%016lx"
1321 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1326 return trace_seq_printf(s, "0");
1328 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1329 ret = seq_print_sym_offset(s, "%s", ip);
1331 ret = seq_print_sym_short(s, "%s", ip);
1336 if (sym_flags & TRACE_ITER_SYM_ADDR)
1337 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1341 static void print_lat_help_header(struct seq_file *m)
1343 seq_puts(m, "# _------=> CPU# \n");
1344 seq_puts(m, "# / _-----=> irqs-off \n");
1345 seq_puts(m, "# | / _----=> need-resched \n");
1346 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1347 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1348 seq_puts(m, "# |||| / \n");
1349 seq_puts(m, "# ||||| delay \n");
1350 seq_puts(m, "# cmd pid ||||| time | caller \n");
1351 seq_puts(m, "# \\ / ||||| \\ | / \n");
1354 static void print_func_help_header(struct seq_file *m)
1356 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1357 seq_puts(m, "# | | | | |\n");
1362 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1364 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1365 struct trace_array *tr = iter->tr;
1366 struct trace_array_cpu *data = tr->data[tr->cpu];
1367 struct tracer *type = current_trace;
1368 unsigned long total = 0;
1369 unsigned long entries = 0;
1371 const char *name = "preemption";
1376 for_each_tracing_cpu(cpu) {
1377 if (head_page(tr->data[cpu])) {
1378 total += tr->data[cpu]->trace_idx;
1379 if (tr->data[cpu]->trace_idx > tr->entries)
1380 entries += tr->entries;
1382 entries += tr->data[cpu]->trace_idx;
1386 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1388 seq_puts(m, "-----------------------------------"
1389 "---------------------------------\n");
1390 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1391 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1392 nsecs_to_usecs(data->saved_latency),
1396 #if defined(CONFIG_PREEMPT_NONE)
1398 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1400 #elif defined(CONFIG_PREEMPT)
1405 /* These are reserved for later use */
1408 seq_printf(m, " #P:%d)\n", num_online_cpus());
1412 seq_puts(m, " -----------------\n");
1413 seq_printf(m, " | task: %.16s-%d "
1414 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1415 data->comm, data->pid, data->uid, data->nice,
1416 data->policy, data->rt_priority);
1417 seq_puts(m, " -----------------\n");
1419 if (data->critical_start) {
1420 seq_puts(m, " => started at: ");
1421 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1422 trace_print_seq(m, &iter->seq);
1423 seq_puts(m, "\n => ended at: ");
1424 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1425 trace_print_seq(m, &iter->seq);
1433 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1435 struct trace_field *field = &entry->field;
1436 int hardirq, softirq;
1439 comm = trace_find_cmdline(field->pid);
1441 trace_seq_printf(s, "%8.8s-%-5d ", comm, field->pid);
1442 trace_seq_printf(s, "%3d", cpu);
1443 trace_seq_printf(s, "%c%c",
1444 (field->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1445 ((field->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1447 hardirq = field->flags & TRACE_FLAG_HARDIRQ;
1448 softirq = field->flags & TRACE_FLAG_SOFTIRQ;
1449 if (hardirq && softirq) {
1450 trace_seq_putc(s, 'H');
1453 trace_seq_putc(s, 'h');
1456 trace_seq_putc(s, 's');
1458 trace_seq_putc(s, '.');
1462 if (field->preempt_count)
1463 trace_seq_printf(s, "%x", field->preempt_count);
1465 trace_seq_puts(s, ".");
1468 unsigned long preempt_mark_thresh = 100;
1471 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1472 unsigned long rel_usecs)
1474 trace_seq_printf(s, " %4lldus", abs_usecs);
1475 if (rel_usecs > preempt_mark_thresh)
1476 trace_seq_puts(s, "!: ");
1477 else if (rel_usecs > 1)
1478 trace_seq_puts(s, "+: ");
1480 trace_seq_puts(s, " : ");
1483 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1486 * The message is supposed to contain an ending newline.
1487 * If the printing stops prematurely, try to add a newline of our own.
1489 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1491 struct trace_array *tr = iter->tr;
1492 struct trace_array_cpu *data = tr->data[iter->cpu];
1493 struct trace_entry *ent;
1496 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1497 if (!ent || ent->type != TRACE_CONT) {
1498 trace_seq_putc(s, '\n');
1504 ok = (trace_seq_printf(s, "%s", ent->cont.buf) > 0);
1505 __trace_iterator_increment(iter, iter->cpu);
1506 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1507 } while (ent && ent->type == TRACE_CONT);
1510 trace_seq_putc(s, '\n');
1514 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1516 struct trace_seq *s = &iter->seq;
1517 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1518 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1519 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1520 struct trace_entry *entry = iter->ent;
1521 struct trace_field *field = &entry->field;
1522 unsigned long abs_usecs;
1523 unsigned long rel_usecs;
1532 if (entry->type == TRACE_CONT)
1535 rel_usecs = ns2usecs(next_entry->field.t - entry->field.t);
1536 abs_usecs = ns2usecs(entry->field.t - iter->tr->time_start);
1539 comm = trace_find_cmdline(field->pid);
1540 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1541 " %ld.%03ldms (+%ld.%03ldms): ",
1543 field->pid, cpu, field->flags,
1544 field->preempt_count, trace_idx,
1547 abs_usecs % 1000, rel_usecs/1000,
1550 lat_print_generic(s, entry, cpu);
1551 lat_print_timestamp(s, abs_usecs, rel_usecs);
1553 switch (entry->type) {
1555 seq_print_ip_sym(s, field->fn.ip, sym_flags);
1556 trace_seq_puts(s, " (");
1557 if (kretprobed(field->fn.parent_ip))
1558 trace_seq_puts(s, KRETPROBE_MSG);
1560 seq_print_ip_sym(s, field->fn.parent_ip, sym_flags);
1561 trace_seq_puts(s, ")\n");
1565 T = field->ctx.next_state < sizeof(state_to_char) ?
1566 state_to_char[field->ctx.next_state] : 'X';
1568 state = field->ctx.prev_state ?
1569 __ffs(field->ctx.prev_state) + 1 : 0;
1570 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1571 comm = trace_find_cmdline(field->ctx.next_pid);
1572 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1573 field->ctx.prev_pid,
1574 field->ctx.prev_prio,
1575 S, entry->type == TRACE_CTX ? "==>" : " +",
1576 field->ctx.next_cpu,
1577 field->ctx.next_pid,
1578 field->ctx.next_prio,
1582 trace_seq_printf(s, "# %ld %ld %ld\n",
1583 field->special.arg1,
1584 field->special.arg2,
1585 field->special.arg3);
1588 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1590 trace_seq_puts(s, " <= ");
1591 seq_print_ip_sym(s, field->stack.caller[i], sym_flags);
1593 trace_seq_puts(s, "\n");
1596 seq_print_ip_sym(s, field->print.ip, sym_flags);
1597 trace_seq_printf(s, ": %s", field->print.buf);
1598 if (field->flags & TRACE_FLAG_CONT)
1599 trace_seq_print_cont(s, iter);
1602 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1607 static int print_trace_fmt(struct trace_iterator *iter)
1609 struct trace_seq *s = &iter->seq;
1610 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1611 struct trace_entry *entry;
1612 struct trace_field *field;
1613 unsigned long usec_rem;
1614 unsigned long long t;
1623 if (entry->type == TRACE_CONT)
1626 field = &entry->field;
1628 comm = trace_find_cmdline(iter->ent->field.pid);
1630 t = ns2usecs(field->t);
1631 usec_rem = do_div(t, 1000000ULL);
1632 secs = (unsigned long)t;
1634 ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid);
1637 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1640 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1644 switch (entry->type) {
1646 ret = seq_print_ip_sym(s, field->fn.ip, sym_flags);
1649 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1650 field->fn.parent_ip) {
1651 ret = trace_seq_printf(s, " <-");
1654 if (kretprobed(field->fn.parent_ip))
1655 ret = trace_seq_puts(s, KRETPROBE_MSG);
1657 ret = seq_print_ip_sym(s,
1658 field->fn.parent_ip,
1663 ret = trace_seq_printf(s, "\n");
1669 S = field->ctx.prev_state < sizeof(state_to_char) ?
1670 state_to_char[field->ctx.prev_state] : 'X';
1671 T = field->ctx.next_state < sizeof(state_to_char) ?
1672 state_to_char[field->ctx.next_state] : 'X';
1673 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1674 field->ctx.prev_pid,
1675 field->ctx.prev_prio,
1677 entry->type == TRACE_CTX ? "==>" : " +",
1678 field->ctx.next_cpu,
1679 field->ctx.next_pid,
1680 field->ctx.next_prio,
1686 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1687 field->special.arg1,
1688 field->special.arg2,
1689 field->special.arg3);
1694 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1696 ret = trace_seq_puts(s, " <= ");
1700 ret = seq_print_ip_sym(s, field->stack.caller[i],
1705 ret = trace_seq_puts(s, "\n");
1710 seq_print_ip_sym(s, field->print.ip, sym_flags);
1711 trace_seq_printf(s, ": %s", field->print.buf);
1712 if (field->flags & TRACE_FLAG_CONT)
1713 trace_seq_print_cont(s, iter);
1719 static int print_raw_fmt(struct trace_iterator *iter)
1721 struct trace_seq *s = &iter->seq;
1722 struct trace_entry *entry;
1723 struct trace_field *field;
1729 if (entry->type == TRACE_CONT)
1732 field = &entry->field;
1734 ret = trace_seq_printf(s, "%d %d %llu ",
1735 field->pid, iter->cpu, field->t);
1739 switch (entry->type) {
1741 ret = trace_seq_printf(s, "%x %x\n",
1743 field->fn.parent_ip);
1749 S = field->ctx.prev_state < sizeof(state_to_char) ?
1750 state_to_char[field->ctx.prev_state] : 'X';
1751 T = field->ctx.next_state < sizeof(state_to_char) ?
1752 state_to_char[field->ctx.next_state] : 'X';
1753 if (entry->type == TRACE_WAKE)
1755 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
1756 field->ctx.prev_pid,
1757 field->ctx.prev_prio,
1759 field->ctx.next_cpu,
1760 field->ctx.next_pid,
1761 field->ctx.next_prio,
1768 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1769 field->special.arg1,
1770 field->special.arg2,
1771 field->special.arg3);
1776 trace_seq_printf(s, "# %lx %s",
1777 field->print.ip, field->print.buf);
1778 if (field->flags & TRACE_FLAG_CONT)
1779 trace_seq_print_cont(s, iter);
1785 #define SEQ_PUT_FIELD_RET(s, x) \
1787 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1791 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1793 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1797 static int print_hex_fmt(struct trace_iterator *iter)
1799 struct trace_seq *s = &iter->seq;
1800 unsigned char newline = '\n';
1801 struct trace_entry *entry;
1802 struct trace_field *field;
1807 if (entry->type == TRACE_CONT)
1810 field = &entry->field;
1812 SEQ_PUT_HEX_FIELD_RET(s, field->pid);
1813 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1814 SEQ_PUT_HEX_FIELD_RET(s, field->t);
1816 switch (entry->type) {
1818 SEQ_PUT_HEX_FIELD_RET(s, field->fn.ip);
1819 SEQ_PUT_HEX_FIELD_RET(s, field->fn.parent_ip);
1823 S = field->ctx.prev_state < sizeof(state_to_char) ?
1824 state_to_char[field->ctx.prev_state] : 'X';
1825 T = field->ctx.next_state < sizeof(state_to_char) ?
1826 state_to_char[field->ctx.next_state] : 'X';
1827 if (entry->type == TRACE_WAKE)
1829 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_pid);
1830 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_prio);
1831 SEQ_PUT_HEX_FIELD_RET(s, S);
1832 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_cpu);
1833 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_pid);
1834 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_prio);
1835 SEQ_PUT_HEX_FIELD_RET(s, T);
1839 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg1);
1840 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg2);
1841 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg3);
1844 SEQ_PUT_FIELD_RET(s, newline);
1849 static int print_bin_fmt(struct trace_iterator *iter)
1851 struct trace_seq *s = &iter->seq;
1852 struct trace_entry *entry;
1853 struct trace_field *field;
1857 if (entry->type == TRACE_CONT)
1860 field = &entry->field;
1862 SEQ_PUT_FIELD_RET(s, field->pid);
1863 SEQ_PUT_FIELD_RET(s, field->cpu);
1864 SEQ_PUT_FIELD_RET(s, field->t);
1866 switch (entry->type) {
1868 SEQ_PUT_FIELD_RET(s, field->fn.ip);
1869 SEQ_PUT_FIELD_RET(s, field->fn.parent_ip);
1872 SEQ_PUT_FIELD_RET(s, field->ctx.prev_pid);
1873 SEQ_PUT_FIELD_RET(s, field->ctx.prev_prio);
1874 SEQ_PUT_FIELD_RET(s, field->ctx.prev_state);
1875 SEQ_PUT_FIELD_RET(s, field->ctx.next_pid);
1876 SEQ_PUT_FIELD_RET(s, field->ctx.next_prio);
1877 SEQ_PUT_FIELD_RET(s, field->ctx.next_state);
1881 SEQ_PUT_FIELD_RET(s, field->special.arg1);
1882 SEQ_PUT_FIELD_RET(s, field->special.arg2);
1883 SEQ_PUT_FIELD_RET(s, field->special.arg3);
1889 static int trace_empty(struct trace_iterator *iter)
1891 struct trace_array_cpu *data;
1894 for_each_tracing_cpu(cpu) {
1895 data = iter->tr->data[cpu];
1897 if (head_page(data) && data->trace_idx &&
1898 (data->trace_tail != data->trace_head ||
1899 data->trace_tail_idx != data->trace_head_idx))
1905 static int print_trace_line(struct trace_iterator *iter)
1907 if (iter->trace && iter->trace->print_line)
1908 return iter->trace->print_line(iter);
1910 if (trace_flags & TRACE_ITER_BIN)
1911 return print_bin_fmt(iter);
1913 if (trace_flags & TRACE_ITER_HEX)
1914 return print_hex_fmt(iter);
1916 if (trace_flags & TRACE_ITER_RAW)
1917 return print_raw_fmt(iter);
1919 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1920 return print_lat_fmt(iter, iter->idx, iter->cpu);
1922 return print_trace_fmt(iter);
1925 static int s_show(struct seq_file *m, void *v)
1927 struct trace_iterator *iter = v;
1929 if (iter->ent == NULL) {
1931 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1934 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1935 /* print nothing if the buffers are empty */
1936 if (trace_empty(iter))
1938 print_trace_header(m, iter);
1939 if (!(trace_flags & TRACE_ITER_VERBOSE))
1940 print_lat_help_header(m);
1942 if (!(trace_flags & TRACE_ITER_VERBOSE))
1943 print_func_help_header(m);
1946 print_trace_line(iter);
1947 trace_print_seq(m, &iter->seq);
1953 static struct seq_operations tracer_seq_ops = {
1960 static struct trace_iterator *
1961 __tracing_open(struct inode *inode, struct file *file, int *ret)
1963 struct trace_iterator *iter;
1965 if (tracing_disabled) {
1970 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1976 mutex_lock(&trace_types_lock);
1977 if (current_trace && current_trace->print_max)
1980 iter->tr = inode->i_private;
1981 iter->trace = current_trace;
1984 /* TODO stop tracer */
1985 *ret = seq_open(file, &tracer_seq_ops);
1987 struct seq_file *m = file->private_data;
1990 /* stop the trace while dumping */
1991 if (iter->tr->ctrl) {
1993 ftrace_function_enabled = 0;
1996 if (iter->trace && iter->trace->open)
1997 iter->trace->open(iter);
2002 mutex_unlock(&trace_types_lock);
2008 int tracing_open_generic(struct inode *inode, struct file *filp)
2010 if (tracing_disabled)
2013 filp->private_data = inode->i_private;
2017 int tracing_release(struct inode *inode, struct file *file)
2019 struct seq_file *m = (struct seq_file *)file->private_data;
2020 struct trace_iterator *iter = m->private;
2022 mutex_lock(&trace_types_lock);
2023 if (iter->trace && iter->trace->close)
2024 iter->trace->close(iter);
2026 /* reenable tracing if it was previously enabled */
2027 if (iter->tr->ctrl) {
2030 * It is safe to enable function tracing even if it
2033 ftrace_function_enabled = 1;
2035 mutex_unlock(&trace_types_lock);
2037 seq_release(inode, file);
2042 static int tracing_open(struct inode *inode, struct file *file)
2046 __tracing_open(inode, file, &ret);
2051 static int tracing_lt_open(struct inode *inode, struct file *file)
2053 struct trace_iterator *iter;
2056 iter = __tracing_open(inode, file, &ret);
2059 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2066 t_next(struct seq_file *m, void *v, loff_t *pos)
2068 struct tracer *t = m->private;
2080 static void *t_start(struct seq_file *m, loff_t *pos)
2082 struct tracer *t = m->private;
2085 mutex_lock(&trace_types_lock);
2086 for (; t && l < *pos; t = t_next(m, t, &l))
2092 static void t_stop(struct seq_file *m, void *p)
2094 mutex_unlock(&trace_types_lock);
2097 static int t_show(struct seq_file *m, void *v)
2099 struct tracer *t = v;
2104 seq_printf(m, "%s", t->name);
2113 static struct seq_operations show_traces_seq_ops = {
2120 static int show_traces_open(struct inode *inode, struct file *file)
2124 if (tracing_disabled)
2127 ret = seq_open(file, &show_traces_seq_ops);
2129 struct seq_file *m = file->private_data;
2130 m->private = trace_types;
2136 static struct file_operations tracing_fops = {
2137 .open = tracing_open,
2139 .llseek = seq_lseek,
2140 .release = tracing_release,
2143 static struct file_operations tracing_lt_fops = {
2144 .open = tracing_lt_open,
2146 .llseek = seq_lseek,
2147 .release = tracing_release,
2150 static struct file_operations show_traces_fops = {
2151 .open = show_traces_open,
2153 .release = seq_release,
2157 * Only trace on a CPU if the bitmask is set:
2159 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2162 * When tracing/tracing_cpu_mask is modified then this holds
2163 * the new bitmask we are about to install:
2165 static cpumask_t tracing_cpumask_new;
2168 * The tracer itself will not take this lock, but still we want
2169 * to provide a consistent cpumask to user-space:
2171 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2174 * Temporary storage for the character representation of the
2175 * CPU bitmask (and one more byte for the newline):
2177 static char mask_str[NR_CPUS + 1];
2180 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2181 size_t count, loff_t *ppos)
2185 mutex_lock(&tracing_cpumask_update_lock);
2187 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2188 if (count - len < 2) {
2192 len += sprintf(mask_str + len, "\n");
2193 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2196 mutex_unlock(&tracing_cpumask_update_lock);
2202 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2203 size_t count, loff_t *ppos)
2207 mutex_lock(&tracing_cpumask_update_lock);
2208 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2212 raw_local_irq_disable();
2213 __raw_spin_lock(&ftrace_max_lock);
2214 for_each_tracing_cpu(cpu) {
2216 * Increase/decrease the disabled counter if we are
2217 * about to flip a bit in the cpumask:
2219 if (cpu_isset(cpu, tracing_cpumask) &&
2220 !cpu_isset(cpu, tracing_cpumask_new)) {
2221 atomic_inc(&global_trace.data[cpu]->disabled);
2223 if (!cpu_isset(cpu, tracing_cpumask) &&
2224 cpu_isset(cpu, tracing_cpumask_new)) {
2225 atomic_dec(&global_trace.data[cpu]->disabled);
2228 __raw_spin_unlock(&ftrace_max_lock);
2229 raw_local_irq_enable();
2231 tracing_cpumask = tracing_cpumask_new;
2233 mutex_unlock(&tracing_cpumask_update_lock);
2238 mutex_unlock(&tracing_cpumask_update_lock);
2243 static struct file_operations tracing_cpumask_fops = {
2244 .open = tracing_open_generic,
2245 .read = tracing_cpumask_read,
2246 .write = tracing_cpumask_write,
2250 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2251 size_t cnt, loff_t *ppos)
2258 /* calulate max size */
2259 for (i = 0; trace_options[i]; i++) {
2260 len += strlen(trace_options[i]);
2261 len += 3; /* "no" and space */
2264 /* +2 for \n and \0 */
2265 buf = kmalloc(len + 2, GFP_KERNEL);
2269 for (i = 0; trace_options[i]; i++) {
2270 if (trace_flags & (1 << i))
2271 r += sprintf(buf + r, "%s ", trace_options[i]);
2273 r += sprintf(buf + r, "no%s ", trace_options[i]);
2276 r += sprintf(buf + r, "\n");
2277 WARN_ON(r >= len + 2);
2279 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2287 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2288 size_t cnt, loff_t *ppos)
2295 if (cnt >= sizeof(buf))
2298 if (copy_from_user(&buf, ubuf, cnt))
2303 if (strncmp(buf, "no", 2) == 0) {
2308 for (i = 0; trace_options[i]; i++) {
2309 int len = strlen(trace_options[i]);
2311 if (strncmp(cmp, trace_options[i], len) == 0) {
2313 trace_flags &= ~(1 << i);
2315 trace_flags |= (1 << i);
2320 * If no option could be set, return an error:
2322 if (!trace_options[i])
2330 static struct file_operations tracing_iter_fops = {
2331 .open = tracing_open_generic,
2332 .read = tracing_iter_ctrl_read,
2333 .write = tracing_iter_ctrl_write,
2336 static const char readme_msg[] =
2337 "tracing mini-HOWTO:\n\n"
2339 "# mount -t debugfs nodev /debug\n\n"
2340 "# cat /debug/tracing/available_tracers\n"
2341 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2342 "# cat /debug/tracing/current_tracer\n"
2344 "# echo sched_switch > /debug/tracing/current_tracer\n"
2345 "# cat /debug/tracing/current_tracer\n"
2347 "# cat /debug/tracing/iter_ctrl\n"
2348 "noprint-parent nosym-offset nosym-addr noverbose\n"
2349 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2350 "# echo 1 > /debug/tracing/tracing_enabled\n"
2351 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2352 "echo 0 > /debug/tracing/tracing_enabled\n"
2356 tracing_readme_read(struct file *filp, char __user *ubuf,
2357 size_t cnt, loff_t *ppos)
2359 return simple_read_from_buffer(ubuf, cnt, ppos,
2360 readme_msg, strlen(readme_msg));
2363 static struct file_operations tracing_readme_fops = {
2364 .open = tracing_open_generic,
2365 .read = tracing_readme_read,
2369 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2370 size_t cnt, loff_t *ppos)
2372 struct trace_array *tr = filp->private_data;
2376 r = sprintf(buf, "%ld\n", tr->ctrl);
2377 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2381 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2382 size_t cnt, loff_t *ppos)
2384 struct trace_array *tr = filp->private_data;
2389 if (cnt >= sizeof(buf))
2392 if (copy_from_user(&buf, ubuf, cnt))
2397 ret = strict_strtoul(buf, 10, &val);
2403 mutex_lock(&trace_types_lock);
2404 if (tr->ctrl ^ val) {
2412 if (current_trace && current_trace->ctrl_update)
2413 current_trace->ctrl_update(tr);
2415 mutex_unlock(&trace_types_lock);
2423 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2424 size_t cnt, loff_t *ppos)
2426 char buf[max_tracer_type_len+2];
2429 mutex_lock(&trace_types_lock);
2431 r = sprintf(buf, "%s\n", current_trace->name);
2433 r = sprintf(buf, "\n");
2434 mutex_unlock(&trace_types_lock);
2436 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2440 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2441 size_t cnt, loff_t *ppos)
2443 struct trace_array *tr = &global_trace;
2445 char buf[max_tracer_type_len+1];
2448 if (cnt > max_tracer_type_len)
2449 cnt = max_tracer_type_len;
2451 if (copy_from_user(&buf, ubuf, cnt))
2456 /* strip ending whitespace. */
2457 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2460 mutex_lock(&trace_types_lock);
2461 for (t = trace_types; t; t = t->next) {
2462 if (strcmp(t->name, buf) == 0)
2465 if (!t || t == current_trace)
2468 if (current_trace && current_trace->reset)
2469 current_trace->reset(tr);
2476 mutex_unlock(&trace_types_lock);
2484 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2485 size_t cnt, loff_t *ppos)
2487 unsigned long *ptr = filp->private_data;
2491 r = snprintf(buf, sizeof(buf), "%ld\n",
2492 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2493 if (r > sizeof(buf))
2495 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2499 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2500 size_t cnt, loff_t *ppos)
2502 long *ptr = filp->private_data;
2507 if (cnt >= sizeof(buf))
2510 if (copy_from_user(&buf, ubuf, cnt))
2515 ret = strict_strtoul(buf, 10, &val);
2524 static atomic_t tracing_reader;
2526 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2528 struct trace_iterator *iter;
2530 if (tracing_disabled)
2533 /* We only allow for reader of the pipe */
2534 if (atomic_inc_return(&tracing_reader) != 1) {
2535 atomic_dec(&tracing_reader);
2539 /* create a buffer to store the information to pass to userspace */
2540 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2544 mutex_lock(&trace_types_lock);
2545 iter->tr = &global_trace;
2546 iter->trace = current_trace;
2547 filp->private_data = iter;
2549 if (iter->trace->pipe_open)
2550 iter->trace->pipe_open(iter);
2551 mutex_unlock(&trace_types_lock);
2556 static int tracing_release_pipe(struct inode *inode, struct file *file)
2558 struct trace_iterator *iter = file->private_data;
2561 atomic_dec(&tracing_reader);
2567 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2569 struct trace_iterator *iter = filp->private_data;
2571 if (trace_flags & TRACE_ITER_BLOCK) {
2573 * Always select as readable when in blocking mode
2575 return POLLIN | POLLRDNORM;
2577 if (!trace_empty(iter))
2578 return POLLIN | POLLRDNORM;
2579 poll_wait(filp, &trace_wait, poll_table);
2580 if (!trace_empty(iter))
2581 return POLLIN | POLLRDNORM;
2591 tracing_read_pipe(struct file *filp, char __user *ubuf,
2592 size_t cnt, loff_t *ppos)
2594 struct trace_iterator *iter = filp->private_data;
2595 struct trace_array_cpu *data;
2596 static cpumask_t mask;
2597 unsigned long flags;
2598 #ifdef CONFIG_FTRACE
2604 /* return any leftover data */
2605 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2610 trace_seq_reset(&iter->seq);
2612 mutex_lock(&trace_types_lock);
2613 if (iter->trace->read) {
2614 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2619 while (trace_empty(iter)) {
2621 if ((filp->f_flags & O_NONBLOCK)) {
2627 * This is a make-shift waitqueue. The reason we don't use
2628 * an actual wait queue is because:
2629 * 1) we only ever have one waiter
2630 * 2) the tracing, traces all functions, we don't want
2631 * the overhead of calling wake_up and friends
2632 * (and tracing them too)
2633 * Anyway, this is really very primitive wakeup.
2635 set_current_state(TASK_INTERRUPTIBLE);
2636 iter->tr->waiter = current;
2638 mutex_unlock(&trace_types_lock);
2640 /* sleep for 100 msecs, and try again. */
2641 schedule_timeout(HZ/10);
2643 mutex_lock(&trace_types_lock);
2645 iter->tr->waiter = NULL;
2647 if (signal_pending(current)) {
2652 if (iter->trace != current_trace)
2656 * We block until we read something and tracing is disabled.
2657 * We still block if tracing is disabled, but we have never
2658 * read anything. This allows a user to cat this file, and
2659 * then enable tracing. But after we have read something,
2660 * we give an EOF when tracing is again disabled.
2662 * iter->pos will be 0 if we haven't read anything.
2664 if (!tracer_enabled && iter->pos)
2670 /* stop when tracing is finished */
2671 if (trace_empty(iter))
2674 if (cnt >= PAGE_SIZE)
2675 cnt = PAGE_SIZE - 1;
2677 /* reset all but tr, trace, and overruns */
2678 memset(&iter->seq, 0,
2679 sizeof(struct trace_iterator) -
2680 offsetof(struct trace_iterator, seq));
2684 * We need to stop all tracing on all CPUS to read the
2685 * the next buffer. This is a bit expensive, but is
2686 * not done often. We fill all what we can read,
2687 * and then release the locks again.
2691 local_irq_save(flags);
2692 #ifdef CONFIG_FTRACE
2693 ftrace_save = ftrace_enabled;
2697 for_each_tracing_cpu(cpu) {
2698 data = iter->tr->data[cpu];
2700 if (!head_page(data) || !data->trace_idx)
2703 atomic_inc(&data->disabled);
2707 for_each_cpu_mask(cpu, mask) {
2708 data = iter->tr->data[cpu];
2709 __raw_spin_lock(&data->lock);
2711 if (data->overrun > iter->last_overrun[cpu])
2712 iter->overrun[cpu] +=
2713 data->overrun - iter->last_overrun[cpu];
2714 iter->last_overrun[cpu] = data->overrun;
2717 while (find_next_entry_inc(iter) != NULL) {
2719 int len = iter->seq.len;
2721 ret = print_trace_line(iter);
2723 /* don't print partial lines */
2724 iter->seq.len = len;
2728 trace_consume(iter);
2730 if (iter->seq.len >= cnt)
2734 for_each_cpu_mask(cpu, mask) {
2735 data = iter->tr->data[cpu];
2736 __raw_spin_unlock(&data->lock);
2739 for_each_cpu_mask(cpu, mask) {
2740 data = iter->tr->data[cpu];
2741 atomic_dec(&data->disabled);
2743 #ifdef CONFIG_FTRACE
2744 ftrace_enabled = ftrace_save;
2746 local_irq_restore(flags);
2748 /* Now copy what we have to the user */
2749 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2750 if (iter->seq.readpos >= iter->seq.len)
2751 trace_seq_reset(&iter->seq);
2756 mutex_unlock(&trace_types_lock);
2762 tracing_entries_read(struct file *filp, char __user *ubuf,
2763 size_t cnt, loff_t *ppos)
2765 struct trace_array *tr = filp->private_data;
2769 r = sprintf(buf, "%lu\n", tr->entries);
2770 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2774 tracing_entries_write(struct file *filp, const char __user *ubuf,
2775 size_t cnt, loff_t *ppos)
2780 struct trace_array *tr = filp->private_data;
2782 if (cnt >= sizeof(buf))
2785 if (copy_from_user(&buf, ubuf, cnt))
2790 ret = strict_strtoul(buf, 10, &val);
2794 /* must have at least 1 entry */
2798 mutex_lock(&trace_types_lock);
2802 pr_info("ftrace: please disable tracing"
2803 " before modifying buffer size\n");
2807 if (val > global_trace.entries) {
2808 long pages_requested;
2809 unsigned long freeable_pages;
2811 /* make sure we have enough memory before mapping */
2813 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2815 /* account for each buffer (and max_tr) */
2816 pages_requested *= tracing_nr_buffers * 2;
2818 /* Check for overflow */
2819 if (pages_requested < 0) {
2824 freeable_pages = determine_dirtyable_memory();
2826 /* we only allow to request 1/4 of useable memory */
2827 if (pages_requested >
2828 ((freeable_pages + tracing_pages_allocated) / 4)) {
2833 while (global_trace.entries < val) {
2834 if (trace_alloc_page()) {
2838 /* double check that we don't go over the known pages */
2839 if (tracing_pages_allocated > pages_requested)
2844 /* include the number of entries in val (inc of page entries) */
2845 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2849 /* check integrity */
2850 for_each_tracing_cpu(i)
2851 check_pages(global_trace.data[i]);
2855 /* If check pages failed, return ENOMEM */
2856 if (tracing_disabled)
2859 max_tr.entries = global_trace.entries;
2860 mutex_unlock(&trace_types_lock);
2865 static int mark_printk(const char *fmt, ...)
2869 va_start(args, fmt);
2870 ret = trace_vprintk(0, fmt, args);
2876 tracing_mark_write(struct file *filp, const char __user *ubuf,
2877 size_t cnt, loff_t *fpos)
2881 struct trace_array *tr = &global_trace;
2883 if (!tr->ctrl || tracing_disabled)
2886 if (cnt > TRACE_BUF_SIZE)
2887 cnt = TRACE_BUF_SIZE;
2889 buf = kmalloc(cnt + 1, GFP_KERNEL);
2893 if (copy_from_user(buf, ubuf, cnt)) {
2898 /* Cut from the first nil or newline. */
2900 end = strchr(buf, '\n');
2904 cnt = mark_printk("%s\n", buf);
2911 static struct file_operations tracing_max_lat_fops = {
2912 .open = tracing_open_generic,
2913 .read = tracing_max_lat_read,
2914 .write = tracing_max_lat_write,
2917 static struct file_operations tracing_ctrl_fops = {
2918 .open = tracing_open_generic,
2919 .read = tracing_ctrl_read,
2920 .write = tracing_ctrl_write,
2923 static struct file_operations set_tracer_fops = {
2924 .open = tracing_open_generic,
2925 .read = tracing_set_trace_read,
2926 .write = tracing_set_trace_write,
2929 static struct file_operations tracing_pipe_fops = {
2930 .open = tracing_open_pipe,
2931 .poll = tracing_poll_pipe,
2932 .read = tracing_read_pipe,
2933 .release = tracing_release_pipe,
2936 static struct file_operations tracing_entries_fops = {
2937 .open = tracing_open_generic,
2938 .read = tracing_entries_read,
2939 .write = tracing_entries_write,
2942 static struct file_operations tracing_mark_fops = {
2943 .open = tracing_open_generic,
2944 .write = tracing_mark_write,
2947 #ifdef CONFIG_DYNAMIC_FTRACE
2950 tracing_read_long(struct file *filp, char __user *ubuf,
2951 size_t cnt, loff_t *ppos)
2953 unsigned long *p = filp->private_data;
2957 r = sprintf(buf, "%ld\n", *p);
2959 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2962 static struct file_operations tracing_read_long_fops = {
2963 .open = tracing_open_generic,
2964 .read = tracing_read_long,
2968 static struct dentry *d_tracer;
2970 struct dentry *tracing_init_dentry(void)
2977 d_tracer = debugfs_create_dir("tracing", NULL);
2979 if (!d_tracer && !once) {
2981 pr_warning("Could not create debugfs directory 'tracing'\n");
2988 #ifdef CONFIG_FTRACE_SELFTEST
2989 /* Let selftest have access to static functions in this file */
2990 #include "trace_selftest.c"
2993 static __init void tracer_init_debugfs(void)
2995 struct dentry *d_tracer;
2996 struct dentry *entry;
2998 d_tracer = tracing_init_dentry();
3000 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3001 &global_trace, &tracing_ctrl_fops);
3003 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3005 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
3006 NULL, &tracing_iter_fops);
3008 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
3010 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3011 NULL, &tracing_cpumask_fops);
3013 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3015 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3016 &global_trace, &tracing_lt_fops);
3018 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3020 entry = debugfs_create_file("trace", 0444, d_tracer,
3021 &global_trace, &tracing_fops);
3023 pr_warning("Could not create debugfs 'trace' entry\n");
3025 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3026 &global_trace, &show_traces_fops);
3028 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3030 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3031 &global_trace, &set_tracer_fops);
3033 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3035 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3036 &tracing_max_latency,
3037 &tracing_max_lat_fops);
3039 pr_warning("Could not create debugfs "
3040 "'tracing_max_latency' entry\n");
3042 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3043 &tracing_thresh, &tracing_max_lat_fops);
3045 pr_warning("Could not create debugfs "
3046 "'tracing_thresh' entry\n");
3047 entry = debugfs_create_file("README", 0644, d_tracer,
3048 NULL, &tracing_readme_fops);
3050 pr_warning("Could not create debugfs 'README' entry\n");
3052 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3053 NULL, &tracing_pipe_fops);
3055 pr_warning("Could not create debugfs "
3056 "'trace_pipe' entry\n");
3058 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
3059 &global_trace, &tracing_entries_fops);
3061 pr_warning("Could not create debugfs "
3062 "'trace_entries' entry\n");
3064 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3065 NULL, &tracing_mark_fops);
3067 pr_warning("Could not create debugfs "
3068 "'trace_marker' entry\n");
3070 #ifdef CONFIG_DYNAMIC_FTRACE
3071 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3072 &ftrace_update_tot_cnt,
3073 &tracing_read_long_fops);
3075 pr_warning("Could not create debugfs "
3076 "'dyn_ftrace_total_info' entry\n");
3078 #ifdef CONFIG_SYSPROF_TRACER
3079 init_tracer_sysprof_debugfs(d_tracer);
3083 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3085 static DEFINE_SPINLOCK(trace_buf_lock);
3086 static char trace_buf[TRACE_BUF_SIZE];
3088 struct trace_array *tr = &global_trace;
3089 struct trace_array_cpu *data;
3090 struct trace_entry *entry;
3091 unsigned long flags;
3093 int cpu, len = 0, write, written = 0;
3095 if (!tr->ctrl || tracing_disabled)
3098 local_irq_save(flags);
3099 cpu = raw_smp_processor_id();
3100 data = tr->data[cpu];
3101 disabled = atomic_inc_return(&data->disabled);
3103 if (unlikely(disabled != 1))
3106 spin_lock(&trace_buf_lock);
3107 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3109 len = min(len, TRACE_BUF_SIZE-1);
3112 __raw_spin_lock(&data->lock);
3113 entry = tracing_get_trace_entry(tr, data);
3114 tracing_generic_entry_update(entry, flags);
3115 entry->type = TRACE_PRINT;
3116 entry->field.print.ip = ip;
3118 write = min(len, (int)(TRACE_PRINT_BUF_SIZE-1));
3120 memcpy(&entry->field.print.buf, trace_buf, write);
3121 entry->field.print.buf[write] = 0;
3125 entry->field.flags |= TRACE_FLAG_CONT;
3127 while (written != len) {
3128 entry = tracing_get_trace_entry(tr, data);
3130 entry->type = TRACE_CONT;
3131 write = min(len - written, (int)(TRACE_CONT_BUF_SIZE-1));
3132 memcpy(&entry->cont.buf, trace_buf+written, write);
3133 entry->cont.buf[write] = 0;
3136 __raw_spin_unlock(&data->lock);
3138 spin_unlock(&trace_buf_lock);
3141 atomic_dec(&data->disabled);
3142 local_irq_restore(flags);
3146 EXPORT_SYMBOL_GPL(trace_vprintk);
3148 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3153 if (!(trace_flags & TRACE_ITER_PRINTK))
3157 ret = trace_vprintk(ip, fmt, ap);
3161 EXPORT_SYMBOL_GPL(__ftrace_printk);
3163 static int trace_panic_handler(struct notifier_block *this,
3164 unsigned long event, void *unused)
3170 static struct notifier_block trace_panic_notifier = {
3171 .notifier_call = trace_panic_handler,
3173 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3176 static int trace_die_handler(struct notifier_block *self,
3190 static struct notifier_block trace_die_notifier = {
3191 .notifier_call = trace_die_handler,
3196 * printk is set to max of 1024, we really don't need it that big.
3197 * Nothing should be printing 1000 characters anyway.
3199 #define TRACE_MAX_PRINT 1000
3202 * Define here KERN_TRACE so that we have one place to modify
3203 * it if we decide to change what log level the ftrace dump
3206 #define KERN_TRACE KERN_INFO
3209 trace_printk_seq(struct trace_seq *s)
3211 /* Probably should print a warning here. */
3215 /* should be zero ended, but we are paranoid. */
3216 s->buffer[s->len] = 0;
3218 printk(KERN_TRACE "%s", s->buffer);
3224 void ftrace_dump(void)
3226 static DEFINE_SPINLOCK(ftrace_dump_lock);
3227 /* use static because iter can be a bit big for the stack */
3228 static struct trace_iterator iter;
3229 struct trace_array_cpu *data;
3230 static cpumask_t mask;
3231 static int dump_ran;
3232 unsigned long flags;
3237 spin_lock_irqsave(&ftrace_dump_lock, flags);
3243 /* No turning back! */
3244 ftrace_kill_atomic();
3246 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3248 iter.tr = &global_trace;
3249 iter.trace = current_trace;
3252 * We need to stop all tracing on all CPUS to read the
3253 * the next buffer. This is a bit expensive, but is
3254 * not done often. We fill all what we can read,
3255 * and then release the locks again.
3260 for_each_tracing_cpu(cpu) {
3261 data = iter.tr->data[cpu];
3263 if (!head_page(data) || !data->trace_idx)
3266 atomic_inc(&data->disabled);
3270 for_each_cpu_mask(cpu, mask) {
3271 data = iter.tr->data[cpu];
3272 __raw_spin_lock(&data->lock);
3274 if (data->overrun > iter.last_overrun[cpu])
3275 iter.overrun[cpu] +=
3276 data->overrun - iter.last_overrun[cpu];
3277 iter.last_overrun[cpu] = data->overrun;
3280 while (!trace_empty(&iter)) {
3283 printk(KERN_TRACE "---------------------------------\n");
3287 /* reset all but tr, trace, and overruns */
3288 memset(&iter.seq, 0,
3289 sizeof(struct trace_iterator) -
3290 offsetof(struct trace_iterator, seq));
3291 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3294 if (find_next_entry_inc(&iter) != NULL) {
3295 print_trace_line(&iter);
3296 trace_consume(&iter);
3299 trace_printk_seq(&iter.seq);
3303 printk(KERN_TRACE " (ftrace buffer empty)\n");
3305 printk(KERN_TRACE "---------------------------------\n");
3307 for_each_cpu_mask(cpu, mask) {
3308 data = iter.tr->data[cpu];
3309 __raw_spin_unlock(&data->lock);
3312 for_each_cpu_mask(cpu, mask) {
3313 data = iter.tr->data[cpu];
3314 atomic_dec(&data->disabled);
3319 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3322 static int trace_alloc_page(void)
3324 struct trace_array_cpu *data;
3325 struct page *page, *tmp;
3328 unsigned pages_allocated = 0;
3331 /* first allocate a page for each CPU */
3332 for_each_tracing_cpu(i) {
3333 array = (void *)__get_free_page(GFP_KERNEL);
3334 if (array == NULL) {
3335 printk(KERN_ERR "tracer: failed to allocate page"
3336 "for trace buffer!\n");
3341 page = virt_to_page(array);
3342 list_add(&page->lru, &pages);
3344 /* Only allocate if we are actually using the max trace */
3345 #ifdef CONFIG_TRACER_MAX_TRACE
3346 array = (void *)__get_free_page(GFP_KERNEL);
3347 if (array == NULL) {
3348 printk(KERN_ERR "tracer: failed to allocate page"
3349 "for trace buffer!\n");
3353 page = virt_to_page(array);
3354 list_add(&page->lru, &pages);
3358 /* Now that we successfully allocate a page per CPU, add them */
3359 for_each_tracing_cpu(i) {
3360 data = global_trace.data[i];
3361 page = list_entry(pages.next, struct page, lru);
3362 list_del_init(&page->lru);
3363 list_add_tail(&page->lru, &data->trace_pages);
3366 #ifdef CONFIG_TRACER_MAX_TRACE
3367 data = max_tr.data[i];
3368 page = list_entry(pages.next, struct page, lru);
3369 list_del_init(&page->lru);
3370 list_add_tail(&page->lru, &data->trace_pages);
3374 tracing_pages_allocated += pages_allocated;
3375 global_trace.entries += ENTRIES_PER_PAGE;
3380 list_for_each_entry_safe(page, tmp, &pages, lru) {
3381 list_del_init(&page->lru);
3387 static int trace_free_page(void)
3389 struct trace_array_cpu *data;
3391 struct list_head *p;
3395 /* free one page from each buffer */
3396 for_each_tracing_cpu(i) {
3397 data = global_trace.data[i];
3398 p = data->trace_pages.next;
3399 if (p == &data->trace_pages) {
3400 /* should never happen */
3402 tracing_disabled = 1;
3406 page = list_entry(p, struct page, lru);
3408 list_del(&page->lru);
3409 tracing_pages_allocated--;
3410 tracing_pages_allocated--;
3413 tracing_reset(data);
3415 #ifdef CONFIG_TRACER_MAX_TRACE
3416 data = max_tr.data[i];
3417 p = data->trace_pages.next;
3418 if (p == &data->trace_pages) {
3419 /* should never happen */
3421 tracing_disabled = 1;
3425 page = list_entry(p, struct page, lru);
3427 list_del(&page->lru);
3430 tracing_reset(data);
3433 global_trace.entries -= ENTRIES_PER_PAGE;
3438 __init static int tracer_alloc_buffers(void)
3440 struct trace_array_cpu *data;
3447 /* TODO: make the number of buffers hot pluggable with CPUS */
3448 tracing_nr_buffers = num_possible_cpus();
3449 tracing_buffer_mask = cpu_possible_map;
3451 /* Allocate the first page for all buffers */
3452 for_each_tracing_cpu(i) {
3453 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3454 max_tr.data[i] = &per_cpu(max_data, i);
3456 array = (void *)__get_free_page(GFP_KERNEL);
3457 if (array == NULL) {
3458 printk(KERN_ERR "tracer: failed to allocate page"
3459 "for trace buffer!\n");
3463 /* set the array to the list */
3464 INIT_LIST_HEAD(&data->trace_pages);
3465 page = virt_to_page(array);
3466 list_add(&page->lru, &data->trace_pages);
3467 /* use the LRU flag to differentiate the two buffers */
3470 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3471 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3473 /* Only allocate if we are actually using the max trace */
3474 #ifdef CONFIG_TRACER_MAX_TRACE
3475 array = (void *)__get_free_page(GFP_KERNEL);
3476 if (array == NULL) {
3477 printk(KERN_ERR "tracer: failed to allocate page"
3478 "for trace buffer!\n");
3482 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3483 page = virt_to_page(array);
3484 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3490 * Since we allocate by orders of pages, we may be able to
3493 global_trace.entries = ENTRIES_PER_PAGE;
3496 while (global_trace.entries < trace_nr_entries) {
3497 if (trace_alloc_page())
3501 max_tr.entries = global_trace.entries;
3503 pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
3504 pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
3505 pr_info(" actual entries %ld\n", global_trace.entries);
3507 tracer_init_debugfs();
3509 trace_init_cmdlines();
3511 register_tracer(&nop_trace);
3512 current_trace = &nop_trace;
3514 /* All seems OK, enable tracing */
3515 global_trace.ctrl = tracer_enabled;
3516 tracing_disabled = 0;
3518 atomic_notifier_chain_register(&panic_notifier_list,
3519 &trace_panic_notifier);
3521 register_die_notifier(&trace_die_notifier);
3526 for (i-- ; i >= 0; i--) {
3527 struct page *page, *tmp;
3528 struct trace_array_cpu *data = global_trace.data[i];
3531 list_for_each_entry_safe(page, tmp,
3532 &data->trace_pages, lru) {
3533 list_del_init(&page->lru);
3538 #ifdef CONFIG_TRACER_MAX_TRACE
3539 data = max_tr.data[i];
3541 list_for_each_entry_safe(page, tmp,
3542 &data->trace_pages, lru) {
3543 list_del_init(&page->lru);
3551 fs_initcall(tracer_alloc_buffers);