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;
145 static notrace void no_trace_init(struct trace_array *tr)
149 ftrace_function_enabled = 0;
151 for_each_online_cpu(cpu)
152 tracing_reset(tr->data[cpu]);
156 /* dummy trace to disable tracing */
157 static struct tracer no_tracer __read_mostly = {
159 .init = no_trace_init
164 * trace_wake_up - wake up tasks waiting for trace input
166 * Simply wakes up any task that is blocked on the trace_wait
167 * queue. These is used with trace_poll for tasks polling the trace.
169 void trace_wake_up(void)
172 * The runqueue_is_locked() can fail, but this is the best we
175 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
176 wake_up(&trace_wait);
179 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
181 static int __init set_nr_entries(char *str)
183 unsigned long nr_entries;
188 ret = strict_strtoul(str, 0, &nr_entries);
189 /* nr_entries can not be zero */
190 if (ret < 0 || nr_entries == 0)
192 trace_nr_entries = nr_entries;
195 __setup("trace_entries=", set_nr_entries);
197 unsigned long nsecs_to_usecs(unsigned long nsecs)
203 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
204 * control the output of kernel symbols.
206 #define TRACE_ITER_SYM_MASK \
207 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
209 /* These must match the bit postions in trace_iterator_flags */
210 static const char *trace_options[] = {
226 * ftrace_max_lock is used to protect the swapping of buffers
227 * when taking a max snapshot. The buffers themselves are
228 * protected by per_cpu spinlocks. But the action of the swap
229 * needs its own lock.
231 * This is defined as a raw_spinlock_t in order to help
232 * with performance when lockdep debugging is enabled.
234 static raw_spinlock_t ftrace_max_lock =
235 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
238 * Copy the new maximum trace into the separate maximum-trace
239 * structure. (this way the maximum trace is permanently saved,
240 * for later retrieval via /debugfs/tracing/latency_trace)
243 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
245 struct trace_array_cpu *data = tr->data[cpu];
248 max_tr.time_start = data->preempt_timestamp;
250 data = max_tr.data[cpu];
251 data->saved_latency = tracing_max_latency;
253 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
254 data->pid = tsk->pid;
255 data->uid = tsk->uid;
256 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
257 data->policy = tsk->policy;
258 data->rt_priority = tsk->rt_priority;
260 /* record this tasks comm */
261 tracing_record_cmdline(current);
264 #define CHECK_COND(cond) \
265 if (unlikely(cond)) { \
266 tracing_disabled = 1; \
272 * check_pages - integrity check of trace buffers
274 * As a safty measure we check to make sure the data pages have not
277 int check_pages(struct trace_array_cpu *data)
279 struct page *page, *tmp;
281 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
282 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
284 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
285 CHECK_COND(page->lru.next->prev != &page->lru);
286 CHECK_COND(page->lru.prev->next != &page->lru);
293 * head_page - page address of the first page in per_cpu buffer.
295 * head_page returns the page address of the first page in
296 * a per_cpu buffer. This also preforms various consistency
297 * checks to make sure the buffer has not been corrupted.
299 void *head_page(struct trace_array_cpu *data)
303 if (list_empty(&data->trace_pages))
306 page = list_entry(data->trace_pages.next, struct page, lru);
307 BUG_ON(&page->lru == &data->trace_pages);
309 return page_address(page);
313 * trace_seq_printf - sequence printing of trace information
314 * @s: trace sequence descriptor
315 * @fmt: printf format string
317 * The tracer may use either sequence operations or its own
318 * copy to user routines. To simplify formating of a trace
319 * trace_seq_printf is used to store strings into a special
320 * buffer (@s). Then the output may be either used by
321 * the sequencer or pulled into another buffer.
324 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
326 int len = (PAGE_SIZE - 1) - s->len;
334 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
337 /* If we can't write it all, don't bother writing anything */
347 * trace_seq_puts - trace sequence printing of simple string
348 * @s: trace sequence descriptor
349 * @str: simple string to record
351 * The tracer may use either the sequence operations or its own
352 * copy to user routines. This function records a simple string
353 * into a special buffer (@s) for later retrieval by a sequencer
354 * or other mechanism.
357 trace_seq_puts(struct trace_seq *s, const char *str)
359 int len = strlen(str);
361 if (len > ((PAGE_SIZE - 1) - s->len))
364 memcpy(s->buffer + s->len, str, len);
371 trace_seq_putc(struct trace_seq *s, unsigned char c)
373 if (s->len >= (PAGE_SIZE - 1))
376 s->buffer[s->len++] = c;
382 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
384 if (len > ((PAGE_SIZE - 1) - s->len))
387 memcpy(s->buffer + s->len, mem, len);
394 static const char hex2asc[] = "0123456789abcdef";
397 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
399 unsigned char hex[HEX_CHARS];
400 unsigned char *data = mem;
404 BUG_ON(len >= HEX_CHARS);
407 for (i = 0, j = 0; i < len; i++) {
409 for (i = len-1, j = 0; i >= 0; i--) {
413 hex[j++] = hex2asc[byte & 0x0f];
414 hex[j++] = hex2asc[byte >> 4];
418 return trace_seq_putmem(s, hex, j);
422 trace_seq_reset(struct trace_seq *s)
428 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
433 if (s->len <= s->readpos)
436 len = s->len - s->readpos;
439 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
448 trace_print_seq(struct seq_file *m, struct trace_seq *s)
450 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
453 seq_puts(m, s->buffer);
459 * flip the trace buffers between two trace descriptors.
460 * This usually is the buffers between the global_trace and
461 * the max_tr to record a snapshot of a current trace.
463 * The ftrace_max_lock must be held.
466 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
468 struct list_head flip_pages;
470 INIT_LIST_HEAD(&flip_pages);
472 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
473 sizeof(struct trace_array_cpu) -
474 offsetof(struct trace_array_cpu, trace_head_idx));
478 list_splice_init(&tr1->trace_pages, &flip_pages);
479 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
480 list_splice_init(&flip_pages, &tr2->trace_pages);
481 BUG_ON(!list_empty(&flip_pages));
487 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
489 * @tsk: the task with the latency
490 * @cpu: The cpu that initiated the trace.
492 * Flip the buffers between the @tr and the max_tr and record information
493 * about which task was the cause of this latency.
496 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
498 struct trace_array_cpu *data;
501 WARN_ON_ONCE(!irqs_disabled());
502 __raw_spin_lock(&ftrace_max_lock);
503 /* clear out all the previous traces */
504 for_each_tracing_cpu(i) {
506 flip_trace(max_tr.data[i], data);
510 __update_max_tr(tr, tsk, cpu);
511 __raw_spin_unlock(&ftrace_max_lock);
515 * update_max_tr_single - only copy one trace over, and reset the rest
517 * @tsk - task with the latency
518 * @cpu - the cpu of the buffer to copy.
520 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
523 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
525 struct trace_array_cpu *data = tr->data[cpu];
528 WARN_ON_ONCE(!irqs_disabled());
529 __raw_spin_lock(&ftrace_max_lock);
530 for_each_tracing_cpu(i)
531 tracing_reset(max_tr.data[i]);
533 flip_trace(max_tr.data[cpu], data);
536 __update_max_tr(tr, tsk, cpu);
537 __raw_spin_unlock(&ftrace_max_lock);
541 * register_tracer - register a tracer with the ftrace system.
542 * @type - the plugin for the tracer
544 * Register a new plugin tracer.
546 int register_tracer(struct tracer *type)
553 pr_info("Tracer must have a name\n");
557 mutex_lock(&trace_types_lock);
558 for (t = trace_types; t; t = t->next) {
559 if (strcmp(type->name, t->name) == 0) {
561 pr_info("Trace %s already registered\n",
568 #ifdef CONFIG_FTRACE_STARTUP_TEST
569 if (type->selftest) {
570 struct tracer *saved_tracer = current_trace;
571 struct trace_array_cpu *data;
572 struct trace_array *tr = &global_trace;
573 int saved_ctrl = tr->ctrl;
576 * Run a selftest on this tracer.
577 * Here we reset the trace buffer, and set the current
578 * tracer to be this tracer. The tracer can then run some
579 * internal tracing to verify that everything is in order.
580 * If we fail, we do not register this tracer.
582 for_each_tracing_cpu(i) {
584 if (!head_page(data))
588 current_trace = type;
590 /* the test is responsible for initializing and enabling */
591 pr_info("Testing tracer %s: ", type->name);
592 ret = type->selftest(type, tr);
593 /* the test is responsible for resetting too */
594 current_trace = saved_tracer;
595 tr->ctrl = saved_ctrl;
597 printk(KERN_CONT "FAILED!\n");
600 /* Only reset on passing, to avoid touching corrupted buffers */
601 for_each_tracing_cpu(i) {
603 if (!head_page(data))
607 printk(KERN_CONT "PASSED\n");
611 type->next = trace_types;
613 len = strlen(type->name);
614 if (len > max_tracer_type_len)
615 max_tracer_type_len = len;
618 mutex_unlock(&trace_types_lock);
623 void unregister_tracer(struct tracer *type)
628 mutex_lock(&trace_types_lock);
629 for (t = &trace_types; *t; t = &(*t)->next) {
633 pr_info("Trace %s not registered\n", type->name);
638 if (strlen(type->name) != max_tracer_type_len)
641 max_tracer_type_len = 0;
642 for (t = &trace_types; *t; t = &(*t)->next) {
643 len = strlen((*t)->name);
644 if (len > max_tracer_type_len)
645 max_tracer_type_len = len;
648 mutex_unlock(&trace_types_lock);
651 void tracing_reset(struct trace_array_cpu *data)
655 data->trace_head = data->trace_tail = head_page(data);
656 data->trace_head_idx = 0;
657 data->trace_tail_idx = 0;
660 #define SAVED_CMDLINES 128
661 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
662 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
663 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
664 static int cmdline_idx;
665 static DEFINE_SPINLOCK(trace_cmdline_lock);
667 /* temporary disable recording */
668 atomic_t trace_record_cmdline_disabled __read_mostly;
670 static void trace_init_cmdlines(void)
672 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
673 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
677 void trace_stop_cmdline_recording(void);
679 static void trace_save_cmdline(struct task_struct *tsk)
684 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
688 * It's not the end of the world if we don't get
689 * the lock, but we also don't want to spin
690 * nor do we want to disable interrupts,
691 * so if we miss here, then better luck next time.
693 if (!spin_trylock(&trace_cmdline_lock))
696 idx = map_pid_to_cmdline[tsk->pid];
697 if (idx >= SAVED_CMDLINES) {
698 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
700 map = map_cmdline_to_pid[idx];
701 if (map <= PID_MAX_DEFAULT)
702 map_pid_to_cmdline[map] = (unsigned)-1;
704 map_pid_to_cmdline[tsk->pid] = idx;
709 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
711 spin_unlock(&trace_cmdline_lock);
714 static char *trace_find_cmdline(int pid)
716 char *cmdline = "<...>";
722 if (pid > PID_MAX_DEFAULT)
725 map = map_pid_to_cmdline[pid];
726 if (map >= SAVED_CMDLINES)
729 cmdline = saved_cmdlines[map];
735 void tracing_record_cmdline(struct task_struct *tsk)
737 if (atomic_read(&trace_record_cmdline_disabled))
740 trace_save_cmdline(tsk);
743 static inline struct list_head *
744 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
747 * Roundrobin - but skip the head (which is not a real page):
750 if (unlikely(next == &data->trace_pages))
752 BUG_ON(next == &data->trace_pages);
758 trace_next_page(struct trace_array_cpu *data, void *addr)
760 struct list_head *next;
763 page = virt_to_page(addr);
765 next = trace_next_list(data, &page->lru);
766 page = list_entry(next, struct page, lru);
768 return page_address(page);
772 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
774 unsigned long idx, idx_next;
775 struct trace_entry *entry;
778 idx = data->trace_head_idx;
781 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
783 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
785 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
786 data->trace_head = trace_next_page(data, data->trace_head);
790 if (data->trace_head == data->trace_tail &&
791 idx_next == data->trace_tail_idx) {
794 data->trace_tail_idx++;
795 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
797 trace_next_page(data, data->trace_tail);
798 data->trace_tail_idx = 0;
802 data->trace_head_idx = idx_next;
808 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
810 struct task_struct *tsk = current;
813 pc = preempt_count();
815 entry->field.preempt_count = pc & 0xff;
816 entry->field.pid = (tsk) ? tsk->pid : 0;
817 entry->field.t = ftrace_now(raw_smp_processor_id());
819 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
820 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
821 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
822 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
826 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
827 unsigned long ip, unsigned long parent_ip, unsigned long flags)
829 struct trace_entry *entry;
830 unsigned long irq_flags;
832 raw_local_irq_save(irq_flags);
833 __raw_spin_lock(&data->lock);
834 entry = tracing_get_trace_entry(tr, data);
835 tracing_generic_entry_update(entry, flags);
836 entry->type = TRACE_FN;
837 entry->field.fn.ip = ip;
838 entry->field.fn.parent_ip = parent_ip;
839 __raw_spin_unlock(&data->lock);
840 raw_local_irq_restore(irq_flags);
844 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
845 unsigned long ip, unsigned long parent_ip, unsigned long flags)
847 if (likely(!atomic_read(&data->disabled)))
848 trace_function(tr, data, ip, parent_ip, flags);
851 void __trace_stack(struct trace_array *tr,
852 struct trace_array_cpu *data,
856 struct trace_entry *entry;
857 struct stack_trace trace;
859 if (!(trace_flags & TRACE_ITER_STACKTRACE))
862 entry = tracing_get_trace_entry(tr, data);
863 tracing_generic_entry_update(entry, flags);
864 entry->type = TRACE_STACK;
866 memset(&entry->field.stack, 0, sizeof(entry->field.stack));
868 trace.nr_entries = 0;
869 trace.max_entries = FTRACE_STACK_ENTRIES;
871 trace.entries = entry->field.stack.caller;
873 save_stack_trace(&trace);
877 __trace_special(void *__tr, void *__data,
878 unsigned long arg1, unsigned long arg2, unsigned long arg3)
880 struct trace_array_cpu *data = __data;
881 struct trace_array *tr = __tr;
882 struct trace_entry *entry;
883 unsigned long irq_flags;
885 raw_local_irq_save(irq_flags);
886 __raw_spin_lock(&data->lock);
887 entry = tracing_get_trace_entry(tr, data);
888 tracing_generic_entry_update(entry, 0);
889 entry->type = TRACE_SPECIAL;
890 entry->field.special.arg1 = arg1;
891 entry->field.special.arg2 = arg2;
892 entry->field.special.arg3 = arg3;
893 __trace_stack(tr, data, irq_flags, 4);
894 __raw_spin_unlock(&data->lock);
895 raw_local_irq_restore(irq_flags);
901 tracing_sched_switch_trace(struct trace_array *tr,
902 struct trace_array_cpu *data,
903 struct task_struct *prev,
904 struct task_struct *next,
907 struct trace_entry *entry;
908 unsigned long irq_flags;
910 raw_local_irq_save(irq_flags);
911 __raw_spin_lock(&data->lock);
912 entry = tracing_get_trace_entry(tr, data);
913 tracing_generic_entry_update(entry, flags);
914 entry->type = TRACE_CTX;
915 entry->field.ctx.prev_pid = prev->pid;
916 entry->field.ctx.prev_prio = prev->prio;
917 entry->field.ctx.prev_state = prev->state;
918 entry->field.ctx.next_pid = next->pid;
919 entry->field.ctx.next_prio = next->prio;
920 entry->field.ctx.next_state = next->state;
921 entry->field.ctx.next_cpu = task_cpu(next);
922 __trace_stack(tr, data, flags, 5);
923 __raw_spin_unlock(&data->lock);
924 raw_local_irq_restore(irq_flags);
928 tracing_sched_wakeup_trace(struct trace_array *tr,
929 struct trace_array_cpu *data,
930 struct task_struct *wakee,
931 struct task_struct *curr,
934 struct trace_entry *entry;
935 unsigned long irq_flags;
937 raw_local_irq_save(irq_flags);
938 __raw_spin_lock(&data->lock);
939 entry = tracing_get_trace_entry(tr, data);
940 tracing_generic_entry_update(entry, flags);
941 entry->type = TRACE_WAKE;
942 entry->field.ctx.prev_pid = curr->pid;
943 entry->field.ctx.prev_prio = curr->prio;
944 entry->field.ctx.prev_state = curr->state;
945 entry->field.ctx.next_pid = wakee->pid;
946 entry->field.ctx.next_prio = wakee->prio;
947 entry->field.ctx.next_state = wakee->state;
948 entry->field.ctx.next_cpu = task_cpu(wakee);
949 __trace_stack(tr, data, flags, 6);
950 __raw_spin_unlock(&data->lock);
951 raw_local_irq_restore(irq_flags);
957 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
959 struct trace_array *tr = &global_trace;
960 struct trace_array_cpu *data;
965 if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
968 local_irq_save(flags);
969 cpu = raw_smp_processor_id();
970 data = tr->data[cpu];
971 disabled = atomic_inc_return(&data->disabled);
973 if (likely(disabled == 1))
974 __trace_special(tr, data, arg1, arg2, arg3);
976 atomic_dec(&data->disabled);
977 local_irq_restore(flags);
982 function_trace_call(unsigned long ip, unsigned long parent_ip)
984 struct trace_array *tr = &global_trace;
985 struct trace_array_cpu *data;
990 if (unlikely(!ftrace_function_enabled))
996 local_irq_save(flags);
997 cpu = raw_smp_processor_id();
998 data = tr->data[cpu];
999 disabled = atomic_inc_return(&data->disabled);
1001 if (likely(disabled == 1))
1002 trace_function(tr, data, ip, parent_ip, flags);
1004 atomic_dec(&data->disabled);
1005 local_irq_restore(flags);
1008 static struct ftrace_ops trace_ops __read_mostly =
1010 .func = function_trace_call,
1013 void tracing_start_function_trace(void)
1015 ftrace_function_enabled = 0;
1016 register_ftrace_function(&trace_ops);
1018 ftrace_function_enabled = 1;
1021 void tracing_stop_function_trace(void)
1023 ftrace_function_enabled = 0;
1024 unregister_ftrace_function(&trace_ops);
1028 enum trace_file_type {
1029 TRACE_FILE_LAT_FMT = 1,
1032 /* Return the current entry. */
1033 static struct trace_entry *
1034 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1035 struct trace_iterator *iter, int cpu)
1038 struct trace_entry *array;
1040 if (iter->next_idx[cpu] >= tr->entries ||
1041 iter->next_idx[cpu] >= data->trace_idx ||
1042 (data->trace_head == data->trace_tail &&
1043 data->trace_head_idx == data->trace_tail_idx))
1046 if (!iter->next_page[cpu]) {
1047 /* Initialize the iterator for this cpu trace buffer */
1048 WARN_ON(!data->trace_tail);
1049 page = virt_to_page(data->trace_tail);
1050 iter->next_page[cpu] = &page->lru;
1051 iter->next_page_idx[cpu] = data->trace_tail_idx;
1054 page = list_entry(iter->next_page[cpu], struct page, lru);
1055 BUG_ON(&data->trace_pages == &page->lru);
1057 array = page_address(page);
1059 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
1060 return &array[iter->next_page_idx[cpu]];
1063 /* Increment the index counter of an iterator by one */
1064 static void __trace_iterator_increment(struct trace_iterator *iter, int cpu)
1066 iter->next_idx[cpu]++;
1067 iter->next_page_idx[cpu]++;
1069 if (iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE) {
1070 struct trace_array_cpu *data = iter->tr->data[cpu];
1072 iter->next_page_idx[cpu] = 0;
1073 iter->next_page[cpu] =
1074 trace_next_list(data, iter->next_page[cpu]);
1078 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1081 __trace_iterator_increment(iter, cpu);
1084 static struct trace_entry *
1085 trace_entry_next(struct trace_array *tr, struct trace_array_cpu *data,
1086 struct trace_iterator *iter, int cpu)
1088 struct list_head *next_page;
1089 struct trace_entry *ent;
1090 int idx, next_idx, next_page_idx;
1092 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1094 if (likely(!ent || ent->type != TRACE_CONT))
1097 /* save the iterator details */
1099 next_idx = iter->next_idx[cpu];
1100 next_page_idx = iter->next_page_idx[cpu];
1101 next_page = iter->next_page[cpu];
1103 /* find a real entry */
1105 __trace_iterator_increment(iter, cpu);
1106 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1107 } while (ent && ent->type != TRACE_CONT);
1109 /* reset the iterator */
1111 iter->next_idx[cpu] = next_idx;
1112 iter->next_page_idx[cpu] = next_page_idx;
1113 iter->next_page[cpu] = next_page;
1118 static struct trace_entry *
1119 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, int inc)
1121 struct trace_array *tr = iter->tr;
1122 struct trace_entry *ent, *next = NULL;
1126 for_each_tracing_cpu(cpu) {
1127 if (!head_page(tr->data[cpu]))
1130 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1132 if (ent && ent->type == TRACE_CONT) {
1133 struct trace_array_cpu *data = tr->data[cpu];
1136 ent = trace_entry_next(tr, data, iter, cpu);
1138 while (ent && ent->type == TRACE_CONT) {
1139 __trace_iterator_increment(iter, cpu);
1140 ent = trace_entry_idx(tr, tr->data[cpu],
1147 * Pick the entry with the smallest timestamp:
1149 if (ent && (!next || ent->field.t < next->field.t)) {
1156 *ent_cpu = next_cpu;
1161 /* Find the next real entry, without updating the iterator itself */
1162 static struct trace_entry *
1163 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1165 return __find_next_entry(iter, ent_cpu, 0);
1168 /* Find the next real entry, and increment the iterator to the next entry */
1169 static void *find_next_entry_inc(struct trace_iterator *iter)
1171 struct trace_entry *next;
1174 next = __find_next_entry(iter, &next_cpu, 1);
1176 iter->prev_ent = iter->ent;
1177 iter->prev_cpu = iter->cpu;
1180 iter->cpu = next_cpu;
1183 trace_iterator_increment(iter, iter->cpu);
1185 return next ? iter : NULL;
1188 static void trace_consume(struct trace_iterator *iter)
1190 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1191 struct trace_entry *ent;
1194 data->trace_tail_idx++;
1195 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1196 data->trace_tail = trace_next_page(data, data->trace_tail);
1197 data->trace_tail_idx = 0;
1200 /* Check if we empty it, then reset the index */
1201 if (data->trace_head == data->trace_tail &&
1202 data->trace_head_idx == data->trace_tail_idx)
1203 data->trace_idx = 0;
1205 ent = trace_entry_idx(iter->tr, iter->tr->data[iter->cpu],
1207 if (ent && ent->type == TRACE_CONT)
1211 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1213 struct trace_iterator *iter = m->private;
1219 /* can't go backwards */
1224 ent = find_next_entry_inc(iter);
1228 while (ent && iter->idx < i)
1229 ent = find_next_entry_inc(iter);
1236 static void *s_start(struct seq_file *m, loff_t *pos)
1238 struct trace_iterator *iter = m->private;
1243 mutex_lock(&trace_types_lock);
1245 if (!current_trace || current_trace != iter->trace) {
1246 mutex_unlock(&trace_types_lock);
1250 atomic_inc(&trace_record_cmdline_disabled);
1252 /* let the tracer grab locks here if needed */
1253 if (current_trace->start)
1254 current_trace->start(iter);
1256 if (*pos != iter->pos) {
1260 iter->prev_ent = NULL;
1261 iter->prev_cpu = -1;
1263 for_each_tracing_cpu(i) {
1264 iter->next_idx[i] = 0;
1265 iter->next_page[i] = NULL;
1268 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1273 p = s_next(m, p, &l);
1279 static void s_stop(struct seq_file *m, void *p)
1281 struct trace_iterator *iter = m->private;
1283 atomic_dec(&trace_record_cmdline_disabled);
1285 /* let the tracer release locks here if needed */
1286 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1287 iter->trace->stop(iter);
1289 mutex_unlock(&trace_types_lock);
1292 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1294 #ifdef CONFIG_KRETPROBES
1295 static inline int kretprobed(unsigned long addr)
1297 return addr == (unsigned long)kretprobe_trampoline;
1300 static inline int kretprobed(unsigned long addr)
1304 #endif /* CONFIG_KRETPROBES */
1307 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1309 #ifdef CONFIG_KALLSYMS
1310 char str[KSYM_SYMBOL_LEN];
1312 kallsyms_lookup(address, NULL, NULL, NULL, str);
1314 return trace_seq_printf(s, fmt, str);
1320 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1321 unsigned long address)
1323 #ifdef CONFIG_KALLSYMS
1324 char str[KSYM_SYMBOL_LEN];
1326 sprint_symbol(str, address);
1327 return trace_seq_printf(s, fmt, str);
1332 #ifndef CONFIG_64BIT
1333 # define IP_FMT "%08lx"
1335 # define IP_FMT "%016lx"
1339 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1344 return trace_seq_printf(s, "0");
1346 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1347 ret = seq_print_sym_offset(s, "%s", ip);
1349 ret = seq_print_sym_short(s, "%s", ip);
1354 if (sym_flags & TRACE_ITER_SYM_ADDR)
1355 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1359 static void print_lat_help_header(struct seq_file *m)
1361 seq_puts(m, "# _------=> CPU# \n");
1362 seq_puts(m, "# / _-----=> irqs-off \n");
1363 seq_puts(m, "# | / _----=> need-resched \n");
1364 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1365 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1366 seq_puts(m, "# |||| / \n");
1367 seq_puts(m, "# ||||| delay \n");
1368 seq_puts(m, "# cmd pid ||||| time | caller \n");
1369 seq_puts(m, "# \\ / ||||| \\ | / \n");
1372 static void print_func_help_header(struct seq_file *m)
1374 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1375 seq_puts(m, "# | | | | |\n");
1380 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1382 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1383 struct trace_array *tr = iter->tr;
1384 struct trace_array_cpu *data = tr->data[tr->cpu];
1385 struct tracer *type = current_trace;
1386 unsigned long total = 0;
1387 unsigned long entries = 0;
1389 const char *name = "preemption";
1394 for_each_tracing_cpu(cpu) {
1395 if (head_page(tr->data[cpu])) {
1396 total += tr->data[cpu]->trace_idx;
1397 if (tr->data[cpu]->trace_idx > tr->entries)
1398 entries += tr->entries;
1400 entries += tr->data[cpu]->trace_idx;
1404 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1406 seq_puts(m, "-----------------------------------"
1407 "---------------------------------\n");
1408 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1409 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1410 nsecs_to_usecs(data->saved_latency),
1414 #if defined(CONFIG_PREEMPT_NONE)
1416 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1418 #elif defined(CONFIG_PREEMPT)
1423 /* These are reserved for later use */
1426 seq_printf(m, " #P:%d)\n", num_online_cpus());
1430 seq_puts(m, " -----------------\n");
1431 seq_printf(m, " | task: %.16s-%d "
1432 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1433 data->comm, data->pid, data->uid, data->nice,
1434 data->policy, data->rt_priority);
1435 seq_puts(m, " -----------------\n");
1437 if (data->critical_start) {
1438 seq_puts(m, " => started at: ");
1439 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1440 trace_print_seq(m, &iter->seq);
1441 seq_puts(m, "\n => ended at: ");
1442 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1443 trace_print_seq(m, &iter->seq);
1451 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1453 struct trace_field *field = &entry->field;
1454 int hardirq, softirq;
1457 comm = trace_find_cmdline(field->pid);
1459 trace_seq_printf(s, "%8.8s-%-5d ", comm, field->pid);
1460 trace_seq_printf(s, "%3d", cpu);
1461 trace_seq_printf(s, "%c%c",
1462 (field->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1463 ((field->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1465 hardirq = field->flags & TRACE_FLAG_HARDIRQ;
1466 softirq = field->flags & TRACE_FLAG_SOFTIRQ;
1467 if (hardirq && softirq) {
1468 trace_seq_putc(s, 'H');
1471 trace_seq_putc(s, 'h');
1474 trace_seq_putc(s, 's');
1476 trace_seq_putc(s, '.');
1480 if (field->preempt_count)
1481 trace_seq_printf(s, "%x", field->preempt_count);
1483 trace_seq_puts(s, ".");
1486 unsigned long preempt_mark_thresh = 100;
1489 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1490 unsigned long rel_usecs)
1492 trace_seq_printf(s, " %4lldus", abs_usecs);
1493 if (rel_usecs > preempt_mark_thresh)
1494 trace_seq_puts(s, "!: ");
1495 else if (rel_usecs > 1)
1496 trace_seq_puts(s, "+: ");
1498 trace_seq_puts(s, " : ");
1501 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1504 * The message is supposed to contain an ending newline.
1505 * If the printing stops prematurely, try to add a newline of our own.
1507 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1509 struct trace_array *tr = iter->tr;
1510 struct trace_array_cpu *data = tr->data[iter->cpu];
1511 struct trace_entry *ent;
1514 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1515 if (!ent || ent->type != TRACE_CONT) {
1516 trace_seq_putc(s, '\n');
1522 ok = (trace_seq_printf(s, "%s", ent->cont.buf) > 0);
1523 __trace_iterator_increment(iter, iter->cpu);
1524 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1525 } while (ent && ent->type == TRACE_CONT);
1528 trace_seq_putc(s, '\n');
1532 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1534 struct trace_seq *s = &iter->seq;
1535 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1536 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1537 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1538 struct trace_entry *entry = iter->ent;
1539 struct trace_field *field = &entry->field;
1540 unsigned long abs_usecs;
1541 unsigned long rel_usecs;
1550 if (entry->type == TRACE_CONT)
1553 rel_usecs = ns2usecs(next_entry->field.t - entry->field.t);
1554 abs_usecs = ns2usecs(entry->field.t - iter->tr->time_start);
1557 comm = trace_find_cmdline(field->pid);
1558 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1559 " %ld.%03ldms (+%ld.%03ldms): ",
1561 field->pid, cpu, field->flags,
1562 field->preempt_count, trace_idx,
1565 abs_usecs % 1000, rel_usecs/1000,
1568 lat_print_generic(s, entry, cpu);
1569 lat_print_timestamp(s, abs_usecs, rel_usecs);
1571 switch (entry->type) {
1573 seq_print_ip_sym(s, field->fn.ip, sym_flags);
1574 trace_seq_puts(s, " (");
1575 if (kretprobed(field->fn.parent_ip))
1576 trace_seq_puts(s, KRETPROBE_MSG);
1578 seq_print_ip_sym(s, field->fn.parent_ip, sym_flags);
1579 trace_seq_puts(s, ")\n");
1583 T = field->ctx.next_state < sizeof(state_to_char) ?
1584 state_to_char[field->ctx.next_state] : 'X';
1586 state = field->ctx.prev_state ?
1587 __ffs(field->ctx.prev_state) + 1 : 0;
1588 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1589 comm = trace_find_cmdline(field->ctx.next_pid);
1590 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1591 field->ctx.prev_pid,
1592 field->ctx.prev_prio,
1593 S, entry->type == TRACE_CTX ? "==>" : " +",
1594 field->ctx.next_cpu,
1595 field->ctx.next_pid,
1596 field->ctx.next_prio,
1600 trace_seq_printf(s, "# %ld %ld %ld\n",
1601 field->special.arg1,
1602 field->special.arg2,
1603 field->special.arg3);
1606 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1608 trace_seq_puts(s, " <= ");
1609 seq_print_ip_sym(s, field->stack.caller[i], sym_flags);
1611 trace_seq_puts(s, "\n");
1614 seq_print_ip_sym(s, field->print.ip, sym_flags);
1615 trace_seq_printf(s, ": %s", field->print.buf);
1616 if (field->flags & TRACE_FLAG_CONT)
1617 trace_seq_print_cont(s, iter);
1620 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1625 static int print_trace_fmt(struct trace_iterator *iter)
1627 struct trace_seq *s = &iter->seq;
1628 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1629 struct trace_entry *entry;
1630 struct trace_field *field;
1631 unsigned long usec_rem;
1632 unsigned long long t;
1641 if (entry->type == TRACE_CONT)
1644 field = &entry->field;
1646 comm = trace_find_cmdline(iter->ent->field.pid);
1648 t = ns2usecs(field->t);
1649 usec_rem = do_div(t, 1000000ULL);
1650 secs = (unsigned long)t;
1652 ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid);
1655 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1658 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1662 switch (entry->type) {
1664 ret = seq_print_ip_sym(s, field->fn.ip, sym_flags);
1667 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1668 field->fn.parent_ip) {
1669 ret = trace_seq_printf(s, " <-");
1672 if (kretprobed(field->fn.parent_ip))
1673 ret = trace_seq_puts(s, KRETPROBE_MSG);
1675 ret = seq_print_ip_sym(s,
1676 field->fn.parent_ip,
1681 ret = trace_seq_printf(s, "\n");
1687 S = field->ctx.prev_state < sizeof(state_to_char) ?
1688 state_to_char[field->ctx.prev_state] : 'X';
1689 T = field->ctx.next_state < sizeof(state_to_char) ?
1690 state_to_char[field->ctx.next_state] : 'X';
1691 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1692 field->ctx.prev_pid,
1693 field->ctx.prev_prio,
1695 entry->type == TRACE_CTX ? "==>" : " +",
1696 field->ctx.next_cpu,
1697 field->ctx.next_pid,
1698 field->ctx.next_prio,
1704 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1705 field->special.arg1,
1706 field->special.arg2,
1707 field->special.arg3);
1712 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1714 ret = trace_seq_puts(s, " <= ");
1718 ret = seq_print_ip_sym(s, field->stack.caller[i],
1723 ret = trace_seq_puts(s, "\n");
1728 seq_print_ip_sym(s, field->print.ip, sym_flags);
1729 trace_seq_printf(s, ": %s", field->print.buf);
1730 if (field->flags & TRACE_FLAG_CONT)
1731 trace_seq_print_cont(s, iter);
1737 static int print_raw_fmt(struct trace_iterator *iter)
1739 struct trace_seq *s = &iter->seq;
1740 struct trace_entry *entry;
1741 struct trace_field *field;
1747 if (entry->type == TRACE_CONT)
1750 field = &entry->field;
1752 ret = trace_seq_printf(s, "%d %d %llu ",
1753 field->pid, iter->cpu, field->t);
1757 switch (entry->type) {
1759 ret = trace_seq_printf(s, "%x %x\n",
1761 field->fn.parent_ip);
1767 S = field->ctx.prev_state < sizeof(state_to_char) ?
1768 state_to_char[field->ctx.prev_state] : 'X';
1769 T = field->ctx.next_state < sizeof(state_to_char) ?
1770 state_to_char[field->ctx.next_state] : 'X';
1771 if (entry->type == TRACE_WAKE)
1773 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
1774 field->ctx.prev_pid,
1775 field->ctx.prev_prio,
1777 field->ctx.next_cpu,
1778 field->ctx.next_pid,
1779 field->ctx.next_prio,
1786 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1787 field->special.arg1,
1788 field->special.arg2,
1789 field->special.arg3);
1794 trace_seq_printf(s, "# %lx %s",
1795 field->print.ip, field->print.buf);
1796 if (field->flags & TRACE_FLAG_CONT)
1797 trace_seq_print_cont(s, iter);
1803 #define SEQ_PUT_FIELD_RET(s, x) \
1805 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1809 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1811 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1815 static int print_hex_fmt(struct trace_iterator *iter)
1817 struct trace_seq *s = &iter->seq;
1818 unsigned char newline = '\n';
1819 struct trace_entry *entry;
1820 struct trace_field *field;
1825 if (entry->type == TRACE_CONT)
1828 field = &entry->field;
1830 SEQ_PUT_HEX_FIELD_RET(s, field->pid);
1831 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1832 SEQ_PUT_HEX_FIELD_RET(s, field->t);
1834 switch (entry->type) {
1836 SEQ_PUT_HEX_FIELD_RET(s, field->fn.ip);
1837 SEQ_PUT_HEX_FIELD_RET(s, field->fn.parent_ip);
1841 S = field->ctx.prev_state < sizeof(state_to_char) ?
1842 state_to_char[field->ctx.prev_state] : 'X';
1843 T = field->ctx.next_state < sizeof(state_to_char) ?
1844 state_to_char[field->ctx.next_state] : 'X';
1845 if (entry->type == TRACE_WAKE)
1847 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_pid);
1848 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_prio);
1849 SEQ_PUT_HEX_FIELD_RET(s, S);
1850 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_cpu);
1851 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_pid);
1852 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_prio);
1853 SEQ_PUT_HEX_FIELD_RET(s, T);
1857 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg1);
1858 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg2);
1859 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg3);
1862 SEQ_PUT_FIELD_RET(s, newline);
1867 static int print_bin_fmt(struct trace_iterator *iter)
1869 struct trace_seq *s = &iter->seq;
1870 struct trace_entry *entry;
1871 struct trace_field *field;
1875 if (entry->type == TRACE_CONT)
1878 field = &entry->field;
1880 SEQ_PUT_FIELD_RET(s, field->pid);
1881 SEQ_PUT_FIELD_RET(s, field->cpu);
1882 SEQ_PUT_FIELD_RET(s, field->t);
1884 switch (entry->type) {
1886 SEQ_PUT_FIELD_RET(s, field->fn.ip);
1887 SEQ_PUT_FIELD_RET(s, field->fn.parent_ip);
1890 SEQ_PUT_FIELD_RET(s, field->ctx.prev_pid);
1891 SEQ_PUT_FIELD_RET(s, field->ctx.prev_prio);
1892 SEQ_PUT_FIELD_RET(s, field->ctx.prev_state);
1893 SEQ_PUT_FIELD_RET(s, field->ctx.next_pid);
1894 SEQ_PUT_FIELD_RET(s, field->ctx.next_prio);
1895 SEQ_PUT_FIELD_RET(s, field->ctx.next_state);
1899 SEQ_PUT_FIELD_RET(s, field->special.arg1);
1900 SEQ_PUT_FIELD_RET(s, field->special.arg2);
1901 SEQ_PUT_FIELD_RET(s, field->special.arg3);
1907 static int trace_empty(struct trace_iterator *iter)
1909 struct trace_array_cpu *data;
1912 for_each_tracing_cpu(cpu) {
1913 data = iter->tr->data[cpu];
1915 if (head_page(data) && data->trace_idx &&
1916 (data->trace_tail != data->trace_head ||
1917 data->trace_tail_idx != data->trace_head_idx))
1923 static int print_trace_line(struct trace_iterator *iter)
1925 if (iter->trace && iter->trace->print_line)
1926 return iter->trace->print_line(iter);
1928 if (trace_flags & TRACE_ITER_BIN)
1929 return print_bin_fmt(iter);
1931 if (trace_flags & TRACE_ITER_HEX)
1932 return print_hex_fmt(iter);
1934 if (trace_flags & TRACE_ITER_RAW)
1935 return print_raw_fmt(iter);
1937 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1938 return print_lat_fmt(iter, iter->idx, iter->cpu);
1940 return print_trace_fmt(iter);
1943 static int s_show(struct seq_file *m, void *v)
1945 struct trace_iterator *iter = v;
1947 if (iter->ent == NULL) {
1949 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1952 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1953 /* print nothing if the buffers are empty */
1954 if (trace_empty(iter))
1956 print_trace_header(m, iter);
1957 if (!(trace_flags & TRACE_ITER_VERBOSE))
1958 print_lat_help_header(m);
1960 if (!(trace_flags & TRACE_ITER_VERBOSE))
1961 print_func_help_header(m);
1964 print_trace_line(iter);
1965 trace_print_seq(m, &iter->seq);
1971 static struct seq_operations tracer_seq_ops = {
1978 static struct trace_iterator *
1979 __tracing_open(struct inode *inode, struct file *file, int *ret)
1981 struct trace_iterator *iter;
1983 if (tracing_disabled) {
1988 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1994 mutex_lock(&trace_types_lock);
1995 if (current_trace && current_trace->print_max)
1998 iter->tr = inode->i_private;
1999 iter->trace = current_trace;
2002 /* TODO stop tracer */
2003 *ret = seq_open(file, &tracer_seq_ops);
2005 struct seq_file *m = file->private_data;
2008 /* stop the trace while dumping */
2009 if (iter->tr->ctrl) {
2011 ftrace_function_enabled = 0;
2014 if (iter->trace && iter->trace->open)
2015 iter->trace->open(iter);
2020 mutex_unlock(&trace_types_lock);
2026 int tracing_open_generic(struct inode *inode, struct file *filp)
2028 if (tracing_disabled)
2031 filp->private_data = inode->i_private;
2035 int tracing_release(struct inode *inode, struct file *file)
2037 struct seq_file *m = (struct seq_file *)file->private_data;
2038 struct trace_iterator *iter = m->private;
2040 mutex_lock(&trace_types_lock);
2041 if (iter->trace && iter->trace->close)
2042 iter->trace->close(iter);
2044 /* reenable tracing if it was previously enabled */
2045 if (iter->tr->ctrl) {
2048 * It is safe to enable function tracing even if it
2051 ftrace_function_enabled = 1;
2053 mutex_unlock(&trace_types_lock);
2055 seq_release(inode, file);
2060 static int tracing_open(struct inode *inode, struct file *file)
2064 __tracing_open(inode, file, &ret);
2069 static int tracing_lt_open(struct inode *inode, struct file *file)
2071 struct trace_iterator *iter;
2074 iter = __tracing_open(inode, file, &ret);
2077 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2084 t_next(struct seq_file *m, void *v, loff_t *pos)
2086 struct tracer *t = m->private;
2098 static void *t_start(struct seq_file *m, loff_t *pos)
2100 struct tracer *t = m->private;
2103 mutex_lock(&trace_types_lock);
2104 for (; t && l < *pos; t = t_next(m, t, &l))
2110 static void t_stop(struct seq_file *m, void *p)
2112 mutex_unlock(&trace_types_lock);
2115 static int t_show(struct seq_file *m, void *v)
2117 struct tracer *t = v;
2122 seq_printf(m, "%s", t->name);
2131 static struct seq_operations show_traces_seq_ops = {
2138 static int show_traces_open(struct inode *inode, struct file *file)
2142 if (tracing_disabled)
2145 ret = seq_open(file, &show_traces_seq_ops);
2147 struct seq_file *m = file->private_data;
2148 m->private = trace_types;
2154 static struct file_operations tracing_fops = {
2155 .open = tracing_open,
2157 .llseek = seq_lseek,
2158 .release = tracing_release,
2161 static struct file_operations tracing_lt_fops = {
2162 .open = tracing_lt_open,
2164 .llseek = seq_lseek,
2165 .release = tracing_release,
2168 static struct file_operations show_traces_fops = {
2169 .open = show_traces_open,
2171 .release = seq_release,
2175 * Only trace on a CPU if the bitmask is set:
2177 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2180 * When tracing/tracing_cpu_mask is modified then this holds
2181 * the new bitmask we are about to install:
2183 static cpumask_t tracing_cpumask_new;
2186 * The tracer itself will not take this lock, but still we want
2187 * to provide a consistent cpumask to user-space:
2189 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2192 * Temporary storage for the character representation of the
2193 * CPU bitmask (and one more byte for the newline):
2195 static char mask_str[NR_CPUS + 1];
2198 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2199 size_t count, loff_t *ppos)
2203 mutex_lock(&tracing_cpumask_update_lock);
2205 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2206 if (count - len < 2) {
2210 len += sprintf(mask_str + len, "\n");
2211 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2214 mutex_unlock(&tracing_cpumask_update_lock);
2220 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2221 size_t count, loff_t *ppos)
2225 mutex_lock(&tracing_cpumask_update_lock);
2226 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2230 raw_local_irq_disable();
2231 __raw_spin_lock(&ftrace_max_lock);
2232 for_each_tracing_cpu(cpu) {
2234 * Increase/decrease the disabled counter if we are
2235 * about to flip a bit in the cpumask:
2237 if (cpu_isset(cpu, tracing_cpumask) &&
2238 !cpu_isset(cpu, tracing_cpumask_new)) {
2239 atomic_inc(&global_trace.data[cpu]->disabled);
2241 if (!cpu_isset(cpu, tracing_cpumask) &&
2242 cpu_isset(cpu, tracing_cpumask_new)) {
2243 atomic_dec(&global_trace.data[cpu]->disabled);
2246 __raw_spin_unlock(&ftrace_max_lock);
2247 raw_local_irq_enable();
2249 tracing_cpumask = tracing_cpumask_new;
2251 mutex_unlock(&tracing_cpumask_update_lock);
2256 mutex_unlock(&tracing_cpumask_update_lock);
2261 static struct file_operations tracing_cpumask_fops = {
2262 .open = tracing_open_generic,
2263 .read = tracing_cpumask_read,
2264 .write = tracing_cpumask_write,
2268 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2269 size_t cnt, loff_t *ppos)
2276 /* calulate max size */
2277 for (i = 0; trace_options[i]; i++) {
2278 len += strlen(trace_options[i]);
2279 len += 3; /* "no" and space */
2282 /* +2 for \n and \0 */
2283 buf = kmalloc(len + 2, GFP_KERNEL);
2287 for (i = 0; trace_options[i]; i++) {
2288 if (trace_flags & (1 << i))
2289 r += sprintf(buf + r, "%s ", trace_options[i]);
2291 r += sprintf(buf + r, "no%s ", trace_options[i]);
2294 r += sprintf(buf + r, "\n");
2295 WARN_ON(r >= len + 2);
2297 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2305 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2306 size_t cnt, loff_t *ppos)
2313 if (cnt >= sizeof(buf))
2316 if (copy_from_user(&buf, ubuf, cnt))
2321 if (strncmp(buf, "no", 2) == 0) {
2326 for (i = 0; trace_options[i]; i++) {
2327 int len = strlen(trace_options[i]);
2329 if (strncmp(cmp, trace_options[i], len) == 0) {
2331 trace_flags &= ~(1 << i);
2333 trace_flags |= (1 << i);
2338 * If no option could be set, return an error:
2340 if (!trace_options[i])
2348 static struct file_operations tracing_iter_fops = {
2349 .open = tracing_open_generic,
2350 .read = tracing_iter_ctrl_read,
2351 .write = tracing_iter_ctrl_write,
2354 static const char readme_msg[] =
2355 "tracing mini-HOWTO:\n\n"
2357 "# mount -t debugfs nodev /debug\n\n"
2358 "# cat /debug/tracing/available_tracers\n"
2359 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2360 "# cat /debug/tracing/current_tracer\n"
2362 "# echo sched_switch > /debug/tracing/current_tracer\n"
2363 "# cat /debug/tracing/current_tracer\n"
2365 "# cat /debug/tracing/iter_ctrl\n"
2366 "noprint-parent nosym-offset nosym-addr noverbose\n"
2367 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2368 "# echo 1 > /debug/tracing/tracing_enabled\n"
2369 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2370 "echo 0 > /debug/tracing/tracing_enabled\n"
2374 tracing_readme_read(struct file *filp, char __user *ubuf,
2375 size_t cnt, loff_t *ppos)
2377 return simple_read_from_buffer(ubuf, cnt, ppos,
2378 readme_msg, strlen(readme_msg));
2381 static struct file_operations tracing_readme_fops = {
2382 .open = tracing_open_generic,
2383 .read = tracing_readme_read,
2387 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2388 size_t cnt, loff_t *ppos)
2390 struct trace_array *tr = filp->private_data;
2394 r = sprintf(buf, "%ld\n", tr->ctrl);
2395 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2399 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2400 size_t cnt, loff_t *ppos)
2402 struct trace_array *tr = filp->private_data;
2407 if (cnt >= sizeof(buf))
2410 if (copy_from_user(&buf, ubuf, cnt))
2415 ret = strict_strtoul(buf, 10, &val);
2421 mutex_lock(&trace_types_lock);
2422 if (tr->ctrl ^ val) {
2430 if (current_trace && current_trace->ctrl_update)
2431 current_trace->ctrl_update(tr);
2433 mutex_unlock(&trace_types_lock);
2441 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2442 size_t cnt, loff_t *ppos)
2444 char buf[max_tracer_type_len+2];
2447 mutex_lock(&trace_types_lock);
2449 r = sprintf(buf, "%s\n", current_trace->name);
2451 r = sprintf(buf, "\n");
2452 mutex_unlock(&trace_types_lock);
2454 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2458 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2459 size_t cnt, loff_t *ppos)
2461 struct trace_array *tr = &global_trace;
2463 char buf[max_tracer_type_len+1];
2466 if (cnt > max_tracer_type_len)
2467 cnt = max_tracer_type_len;
2469 if (copy_from_user(&buf, ubuf, cnt))
2474 /* strip ending whitespace. */
2475 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2478 mutex_lock(&trace_types_lock);
2479 for (t = trace_types; t; t = t->next) {
2480 if (strcmp(t->name, buf) == 0)
2483 if (!t || t == current_trace)
2486 if (current_trace && current_trace->reset)
2487 current_trace->reset(tr);
2494 mutex_unlock(&trace_types_lock);
2502 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2503 size_t cnt, loff_t *ppos)
2505 unsigned long *ptr = filp->private_data;
2509 r = snprintf(buf, sizeof(buf), "%ld\n",
2510 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2511 if (r > sizeof(buf))
2513 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2517 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2518 size_t cnt, loff_t *ppos)
2520 long *ptr = filp->private_data;
2525 if (cnt >= sizeof(buf))
2528 if (copy_from_user(&buf, ubuf, cnt))
2533 ret = strict_strtoul(buf, 10, &val);
2542 static atomic_t tracing_reader;
2544 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2546 struct trace_iterator *iter;
2548 if (tracing_disabled)
2551 /* We only allow for reader of the pipe */
2552 if (atomic_inc_return(&tracing_reader) != 1) {
2553 atomic_dec(&tracing_reader);
2557 /* create a buffer to store the information to pass to userspace */
2558 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2562 mutex_lock(&trace_types_lock);
2563 iter->tr = &global_trace;
2564 iter->trace = current_trace;
2565 filp->private_data = iter;
2567 if (iter->trace->pipe_open)
2568 iter->trace->pipe_open(iter);
2569 mutex_unlock(&trace_types_lock);
2574 static int tracing_release_pipe(struct inode *inode, struct file *file)
2576 struct trace_iterator *iter = file->private_data;
2579 atomic_dec(&tracing_reader);
2585 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2587 struct trace_iterator *iter = filp->private_data;
2589 if (trace_flags & TRACE_ITER_BLOCK) {
2591 * Always select as readable when in blocking mode
2593 return POLLIN | POLLRDNORM;
2595 if (!trace_empty(iter))
2596 return POLLIN | POLLRDNORM;
2597 poll_wait(filp, &trace_wait, poll_table);
2598 if (!trace_empty(iter))
2599 return POLLIN | POLLRDNORM;
2609 tracing_read_pipe(struct file *filp, char __user *ubuf,
2610 size_t cnt, loff_t *ppos)
2612 struct trace_iterator *iter = filp->private_data;
2613 struct trace_array_cpu *data;
2614 static cpumask_t mask;
2615 unsigned long flags;
2616 #ifdef CONFIG_FTRACE
2622 /* return any leftover data */
2623 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2628 trace_seq_reset(&iter->seq);
2630 mutex_lock(&trace_types_lock);
2631 if (iter->trace->read) {
2632 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2637 while (trace_empty(iter)) {
2639 if ((filp->f_flags & O_NONBLOCK)) {
2645 * This is a make-shift waitqueue. The reason we don't use
2646 * an actual wait queue is because:
2647 * 1) we only ever have one waiter
2648 * 2) the tracing, traces all functions, we don't want
2649 * the overhead of calling wake_up and friends
2650 * (and tracing them too)
2651 * Anyway, this is really very primitive wakeup.
2653 set_current_state(TASK_INTERRUPTIBLE);
2654 iter->tr->waiter = current;
2656 mutex_unlock(&trace_types_lock);
2658 /* sleep for 100 msecs, and try again. */
2659 schedule_timeout(HZ/10);
2661 mutex_lock(&trace_types_lock);
2663 iter->tr->waiter = NULL;
2665 if (signal_pending(current)) {
2670 if (iter->trace != current_trace)
2674 * We block until we read something and tracing is disabled.
2675 * We still block if tracing is disabled, but we have never
2676 * read anything. This allows a user to cat this file, and
2677 * then enable tracing. But after we have read something,
2678 * we give an EOF when tracing is again disabled.
2680 * iter->pos will be 0 if we haven't read anything.
2682 if (!tracer_enabled && iter->pos)
2688 /* stop when tracing is finished */
2689 if (trace_empty(iter))
2692 if (cnt >= PAGE_SIZE)
2693 cnt = PAGE_SIZE - 1;
2695 /* reset all but tr, trace, and overruns */
2696 memset(&iter->seq, 0,
2697 sizeof(struct trace_iterator) -
2698 offsetof(struct trace_iterator, seq));
2702 * We need to stop all tracing on all CPUS to read the
2703 * the next buffer. This is a bit expensive, but is
2704 * not done often. We fill all what we can read,
2705 * and then release the locks again.
2709 local_irq_save(flags);
2710 #ifdef CONFIG_FTRACE
2711 ftrace_save = ftrace_enabled;
2715 for_each_tracing_cpu(cpu) {
2716 data = iter->tr->data[cpu];
2718 if (!head_page(data) || !data->trace_idx)
2721 atomic_inc(&data->disabled);
2725 for_each_cpu_mask(cpu, mask) {
2726 data = iter->tr->data[cpu];
2727 __raw_spin_lock(&data->lock);
2729 if (data->overrun > iter->last_overrun[cpu])
2730 iter->overrun[cpu] +=
2731 data->overrun - iter->last_overrun[cpu];
2732 iter->last_overrun[cpu] = data->overrun;
2735 while (find_next_entry_inc(iter) != NULL) {
2737 int len = iter->seq.len;
2739 ret = print_trace_line(iter);
2741 /* don't print partial lines */
2742 iter->seq.len = len;
2746 trace_consume(iter);
2748 if (iter->seq.len >= cnt)
2752 for_each_cpu_mask(cpu, mask) {
2753 data = iter->tr->data[cpu];
2754 __raw_spin_unlock(&data->lock);
2757 for_each_cpu_mask(cpu, mask) {
2758 data = iter->tr->data[cpu];
2759 atomic_dec(&data->disabled);
2761 #ifdef CONFIG_FTRACE
2762 ftrace_enabled = ftrace_save;
2764 local_irq_restore(flags);
2766 /* Now copy what we have to the user */
2767 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2768 if (iter->seq.readpos >= iter->seq.len)
2769 trace_seq_reset(&iter->seq);
2774 mutex_unlock(&trace_types_lock);
2780 tracing_entries_read(struct file *filp, char __user *ubuf,
2781 size_t cnt, loff_t *ppos)
2783 struct trace_array *tr = filp->private_data;
2787 r = sprintf(buf, "%lu\n", tr->entries);
2788 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2792 tracing_entries_write(struct file *filp, const char __user *ubuf,
2793 size_t cnt, loff_t *ppos)
2799 if (cnt >= sizeof(buf))
2802 if (copy_from_user(&buf, ubuf, cnt))
2807 ret = strict_strtoul(buf, 10, &val);
2811 /* must have at least 1 entry */
2815 mutex_lock(&trace_types_lock);
2817 if (current_trace != &no_tracer) {
2819 pr_info("ftrace: set current_tracer to none"
2820 " before modifying buffer size\n");
2824 if (val > global_trace.entries) {
2825 long pages_requested;
2826 unsigned long freeable_pages;
2828 /* make sure we have enough memory before mapping */
2830 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2832 /* account for each buffer (and max_tr) */
2833 pages_requested *= tracing_nr_buffers * 2;
2835 /* Check for overflow */
2836 if (pages_requested < 0) {
2841 freeable_pages = determine_dirtyable_memory();
2843 /* we only allow to request 1/4 of useable memory */
2844 if (pages_requested >
2845 ((freeable_pages + tracing_pages_allocated) / 4)) {
2850 while (global_trace.entries < val) {
2851 if (trace_alloc_page()) {
2855 /* double check that we don't go over the known pages */
2856 if (tracing_pages_allocated > pages_requested)
2861 /* include the number of entries in val (inc of page entries) */
2862 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2866 /* check integrity */
2867 for_each_tracing_cpu(i)
2868 check_pages(global_trace.data[i]);
2872 /* If check pages failed, return ENOMEM */
2873 if (tracing_disabled)
2876 max_tr.entries = global_trace.entries;
2877 mutex_unlock(&trace_types_lock);
2882 static struct file_operations tracing_max_lat_fops = {
2883 .open = tracing_open_generic,
2884 .read = tracing_max_lat_read,
2885 .write = tracing_max_lat_write,
2888 static struct file_operations tracing_ctrl_fops = {
2889 .open = tracing_open_generic,
2890 .read = tracing_ctrl_read,
2891 .write = tracing_ctrl_write,
2894 static struct file_operations set_tracer_fops = {
2895 .open = tracing_open_generic,
2896 .read = tracing_set_trace_read,
2897 .write = tracing_set_trace_write,
2900 static struct file_operations tracing_pipe_fops = {
2901 .open = tracing_open_pipe,
2902 .poll = tracing_poll_pipe,
2903 .read = tracing_read_pipe,
2904 .release = tracing_release_pipe,
2907 static struct file_operations tracing_entries_fops = {
2908 .open = tracing_open_generic,
2909 .read = tracing_entries_read,
2910 .write = tracing_entries_write,
2913 #ifdef CONFIG_DYNAMIC_FTRACE
2916 tracing_read_long(struct file *filp, char __user *ubuf,
2917 size_t cnt, loff_t *ppos)
2919 unsigned long *p = filp->private_data;
2923 r = sprintf(buf, "%ld\n", *p);
2925 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2928 static struct file_operations tracing_read_long_fops = {
2929 .open = tracing_open_generic,
2930 .read = tracing_read_long,
2934 static struct dentry *d_tracer;
2936 struct dentry *tracing_init_dentry(void)
2943 d_tracer = debugfs_create_dir("tracing", NULL);
2945 if (!d_tracer && !once) {
2947 pr_warning("Could not create debugfs directory 'tracing'\n");
2954 #ifdef CONFIG_FTRACE_SELFTEST
2955 /* Let selftest have access to static functions in this file */
2956 #include "trace_selftest.c"
2959 static __init void tracer_init_debugfs(void)
2961 struct dentry *d_tracer;
2962 struct dentry *entry;
2964 d_tracer = tracing_init_dentry();
2966 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2967 &global_trace, &tracing_ctrl_fops);
2969 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2971 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2972 NULL, &tracing_iter_fops);
2974 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2976 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2977 NULL, &tracing_cpumask_fops);
2979 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2981 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2982 &global_trace, &tracing_lt_fops);
2984 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2986 entry = debugfs_create_file("trace", 0444, d_tracer,
2987 &global_trace, &tracing_fops);
2989 pr_warning("Could not create debugfs 'trace' entry\n");
2991 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2992 &global_trace, &show_traces_fops);
2994 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2996 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2997 &global_trace, &set_tracer_fops);
2999 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3001 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3002 &tracing_max_latency,
3003 &tracing_max_lat_fops);
3005 pr_warning("Could not create debugfs "
3006 "'tracing_max_latency' entry\n");
3008 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3009 &tracing_thresh, &tracing_max_lat_fops);
3011 pr_warning("Could not create debugfs "
3012 "'tracing_thresh' entry\n");
3013 entry = debugfs_create_file("README", 0644, d_tracer,
3014 NULL, &tracing_readme_fops);
3016 pr_warning("Could not create debugfs 'README' entry\n");
3018 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3019 NULL, &tracing_pipe_fops);
3021 pr_warning("Could not create debugfs "
3022 "'trace_pipe' entry\n");
3024 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
3025 &global_trace, &tracing_entries_fops);
3027 pr_warning("Could not create debugfs "
3028 "'trace_entries' entry\n");
3030 #ifdef CONFIG_DYNAMIC_FTRACE
3031 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3032 &ftrace_update_tot_cnt,
3033 &tracing_read_long_fops);
3035 pr_warning("Could not create debugfs "
3036 "'dyn_ftrace_total_info' entry\n");
3038 #ifdef CONFIG_SYSPROF_TRACER
3039 init_tracer_sysprof_debugfs(d_tracer);
3043 #define TRACE_BUF_SIZE 1024
3044 #define TRACE_PRINT_BUF_SIZE \
3045 (sizeof(struct trace_field) - offsetof(struct trace_field, print.buf))
3046 #define TRACE_CONT_BUF_SIZE sizeof(struct trace_field)
3048 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3050 static DEFINE_SPINLOCK(trace_buf_lock);
3051 static char trace_buf[TRACE_BUF_SIZE];
3053 struct trace_array *tr = &global_trace;
3054 struct trace_array_cpu *data;
3055 struct trace_entry *entry;
3056 unsigned long flags;
3058 int cpu, len = 0, write, written = 0;
3060 if (current_trace == &no_tracer || !tr->ctrl || tracing_disabled)
3063 local_irq_save(flags);
3064 cpu = raw_smp_processor_id();
3065 data = tr->data[cpu];
3066 disabled = atomic_inc_return(&data->disabled);
3068 if (unlikely(disabled != 1))
3071 spin_lock(&trace_buf_lock);
3072 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3074 len = min(len, TRACE_BUF_SIZE-1);
3077 __raw_spin_lock(&data->lock);
3078 entry = tracing_get_trace_entry(tr, data);
3079 tracing_generic_entry_update(entry, flags);
3080 entry->type = TRACE_PRINT;
3081 entry->field.print.ip = ip;
3083 write = min(len, (int)(TRACE_PRINT_BUF_SIZE-1));
3085 memcpy(&entry->field.print.buf, trace_buf, write);
3086 entry->field.print.buf[write] = 0;
3090 entry->field.flags |= TRACE_FLAG_CONT;
3092 while (written != len) {
3093 entry = tracing_get_trace_entry(tr, data);
3095 entry->type = TRACE_CONT;
3096 write = min(len - written, (int)(TRACE_CONT_BUF_SIZE-1));
3097 memcpy(&entry->cont.buf, trace_buf+written, write);
3098 entry->cont.buf[write] = 0;
3101 __raw_spin_unlock(&data->lock);
3103 spin_unlock(&trace_buf_lock);
3106 atomic_dec(&data->disabled);
3107 local_irq_restore(flags);
3111 EXPORT_SYMBOL_GPL(trace_vprintk);
3113 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3118 if (!(trace_flags & TRACE_ITER_PRINTK))
3122 ret = trace_vprintk(ip, fmt, ap);
3126 EXPORT_SYMBOL_GPL(__ftrace_printk);
3128 static int trace_panic_handler(struct notifier_block *this,
3129 unsigned long event, void *unused)
3135 static struct notifier_block trace_panic_notifier = {
3136 .notifier_call = trace_panic_handler,
3138 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3141 static int trace_die_handler(struct notifier_block *self,
3155 static struct notifier_block trace_die_notifier = {
3156 .notifier_call = trace_die_handler,
3161 * printk is set to max of 1024, we really don't need it that big.
3162 * Nothing should be printing 1000 characters anyway.
3164 #define TRACE_MAX_PRINT 1000
3167 * Define here KERN_TRACE so that we have one place to modify
3168 * it if we decide to change what log level the ftrace dump
3171 #define KERN_TRACE KERN_INFO
3174 trace_printk_seq(struct trace_seq *s)
3176 /* Probably should print a warning here. */
3180 /* should be zero ended, but we are paranoid. */
3181 s->buffer[s->len] = 0;
3183 printk(KERN_TRACE "%s", s->buffer);
3189 void ftrace_dump(void)
3191 static DEFINE_SPINLOCK(ftrace_dump_lock);
3192 /* use static because iter can be a bit big for the stack */
3193 static struct trace_iterator iter;
3194 struct trace_array_cpu *data;
3195 static cpumask_t mask;
3196 static int dump_ran;
3197 unsigned long flags;
3202 spin_lock_irqsave(&ftrace_dump_lock, flags);
3208 /* No turning back! */
3209 ftrace_kill_atomic();
3211 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3213 iter.tr = &global_trace;
3214 iter.trace = current_trace;
3217 * We need to stop all tracing on all CPUS to read the
3218 * the next buffer. This is a bit expensive, but is
3219 * not done often. We fill all what we can read,
3220 * and then release the locks again.
3225 for_each_tracing_cpu(cpu) {
3226 data = iter.tr->data[cpu];
3228 if (!head_page(data) || !data->trace_idx)
3231 atomic_inc(&data->disabled);
3235 for_each_cpu_mask(cpu, mask) {
3236 data = iter.tr->data[cpu];
3237 __raw_spin_lock(&data->lock);
3239 if (data->overrun > iter.last_overrun[cpu])
3240 iter.overrun[cpu] +=
3241 data->overrun - iter.last_overrun[cpu];
3242 iter.last_overrun[cpu] = data->overrun;
3245 while (!trace_empty(&iter)) {
3248 printk(KERN_TRACE "---------------------------------\n");
3252 /* reset all but tr, trace, and overruns */
3253 memset(&iter.seq, 0,
3254 sizeof(struct trace_iterator) -
3255 offsetof(struct trace_iterator, seq));
3256 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3259 if (find_next_entry_inc(&iter) != NULL) {
3260 print_trace_line(&iter);
3261 trace_consume(&iter);
3264 trace_printk_seq(&iter.seq);
3268 printk(KERN_TRACE " (ftrace buffer empty)\n");
3270 printk(KERN_TRACE "---------------------------------\n");
3272 for_each_cpu_mask(cpu, mask) {
3273 data = iter.tr->data[cpu];
3274 __raw_spin_unlock(&data->lock);
3277 for_each_cpu_mask(cpu, mask) {
3278 data = iter.tr->data[cpu];
3279 atomic_dec(&data->disabled);
3284 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3287 static int trace_alloc_page(void)
3289 struct trace_array_cpu *data;
3290 struct page *page, *tmp;
3293 unsigned pages_allocated = 0;
3296 /* first allocate a page for each CPU */
3297 for_each_tracing_cpu(i) {
3298 array = (void *)__get_free_page(GFP_KERNEL);
3299 if (array == NULL) {
3300 printk(KERN_ERR "tracer: failed to allocate page"
3301 "for trace buffer!\n");
3306 page = virt_to_page(array);
3307 list_add(&page->lru, &pages);
3309 /* Only allocate if we are actually using the max trace */
3310 #ifdef CONFIG_TRACER_MAX_TRACE
3311 array = (void *)__get_free_page(GFP_KERNEL);
3312 if (array == NULL) {
3313 printk(KERN_ERR "tracer: failed to allocate page"
3314 "for trace buffer!\n");
3318 page = virt_to_page(array);
3319 list_add(&page->lru, &pages);
3323 /* Now that we successfully allocate a page per CPU, add them */
3324 for_each_tracing_cpu(i) {
3325 data = global_trace.data[i];
3326 page = list_entry(pages.next, struct page, lru);
3327 list_del_init(&page->lru);
3328 list_add_tail(&page->lru, &data->trace_pages);
3331 #ifdef CONFIG_TRACER_MAX_TRACE
3332 data = max_tr.data[i];
3333 page = list_entry(pages.next, struct page, lru);
3334 list_del_init(&page->lru);
3335 list_add_tail(&page->lru, &data->trace_pages);
3339 tracing_pages_allocated += pages_allocated;
3340 global_trace.entries += ENTRIES_PER_PAGE;
3345 list_for_each_entry_safe(page, tmp, &pages, lru) {
3346 list_del_init(&page->lru);
3352 static int trace_free_page(void)
3354 struct trace_array_cpu *data;
3356 struct list_head *p;
3360 /* free one page from each buffer */
3361 for_each_tracing_cpu(i) {
3362 data = global_trace.data[i];
3363 p = data->trace_pages.next;
3364 if (p == &data->trace_pages) {
3365 /* should never happen */
3367 tracing_disabled = 1;
3371 page = list_entry(p, struct page, lru);
3373 list_del(&page->lru);
3374 tracing_pages_allocated--;
3375 tracing_pages_allocated--;
3378 tracing_reset(data);
3380 #ifdef CONFIG_TRACER_MAX_TRACE
3381 data = max_tr.data[i];
3382 p = data->trace_pages.next;
3383 if (p == &data->trace_pages) {
3384 /* should never happen */
3386 tracing_disabled = 1;
3390 page = list_entry(p, struct page, lru);
3392 list_del(&page->lru);
3395 tracing_reset(data);
3398 global_trace.entries -= ENTRIES_PER_PAGE;
3403 __init static int tracer_alloc_buffers(void)
3405 struct trace_array_cpu *data;
3412 /* TODO: make the number of buffers hot pluggable with CPUS */
3413 tracing_nr_buffers = num_possible_cpus();
3414 tracing_buffer_mask = cpu_possible_map;
3416 /* Allocate the first page for all buffers */
3417 for_each_tracing_cpu(i) {
3418 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3419 max_tr.data[i] = &per_cpu(max_data, i);
3421 array = (void *)__get_free_page(GFP_KERNEL);
3422 if (array == NULL) {
3423 printk(KERN_ERR "tracer: failed to allocate page"
3424 "for trace buffer!\n");
3428 /* set the array to the list */
3429 INIT_LIST_HEAD(&data->trace_pages);
3430 page = virt_to_page(array);
3431 list_add(&page->lru, &data->trace_pages);
3432 /* use the LRU flag to differentiate the two buffers */
3435 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3436 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3438 /* Only allocate if we are actually using the max trace */
3439 #ifdef CONFIG_TRACER_MAX_TRACE
3440 array = (void *)__get_free_page(GFP_KERNEL);
3441 if (array == NULL) {
3442 printk(KERN_ERR "tracer: failed to allocate page"
3443 "for trace buffer!\n");
3447 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3448 page = virt_to_page(array);
3449 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3455 * Since we allocate by orders of pages, we may be able to
3458 global_trace.entries = ENTRIES_PER_PAGE;
3461 while (global_trace.entries < trace_nr_entries) {
3462 if (trace_alloc_page())
3466 max_tr.entries = global_trace.entries;
3468 pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
3469 pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
3470 pr_info(" actual entries %ld\n", global_trace.entries);
3472 tracer_init_debugfs();
3474 trace_init_cmdlines();
3476 register_tracer(&no_tracer);
3477 current_trace = &no_tracer;
3479 /* All seems OK, enable tracing */
3480 global_trace.ctrl = tracer_enabled;
3481 tracing_disabled = 0;
3483 atomic_notifier_chain_register(&panic_notifier_list,
3484 &trace_panic_notifier);
3486 register_die_notifier(&trace_die_notifier);
3491 for (i-- ; i >= 0; i--) {
3492 struct page *page, *tmp;
3493 struct trace_array_cpu *data = global_trace.data[i];
3496 list_for_each_entry_safe(page, tmp,
3497 &data->trace_pages, lru) {
3498 list_del_init(&page->lru);
3503 #ifdef CONFIG_TRACER_MAX_TRACE
3504 data = max_tr.data[i];
3506 list_for_each_entry_safe(page, tmp,
3507 &data->trace_pages, lru) {
3508 list_del_init(&page->lru);
3516 fs_initcall(tracer_alloc_buffers);