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_flag_type is an enumeration that holds different
204 * states when a trace occurs. These are:
205 * IRQS_OFF - interrupts were disabled
206 * NEED_RESCED - reschedule is requested
207 * HARDIRQ - inside an interrupt handler
208 * SOFTIRQ - inside a softirq handler
209 * CONT - multiple entries hold the trace item
211 enum trace_flag_type {
212 TRACE_FLAG_IRQS_OFF = 0x01,
213 TRACE_FLAG_NEED_RESCHED = 0x02,
214 TRACE_FLAG_HARDIRQ = 0x04,
215 TRACE_FLAG_SOFTIRQ = 0x08,
216 TRACE_FLAG_CONT = 0x10,
220 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
221 * control the output of kernel symbols.
223 #define TRACE_ITER_SYM_MASK \
224 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
226 /* These must match the bit postions in trace_iterator_flags */
227 static const char *trace_options[] = {
243 * ftrace_max_lock is used to protect the swapping of buffers
244 * when taking a max snapshot. The buffers themselves are
245 * protected by per_cpu spinlocks. But the action of the swap
246 * needs its own lock.
248 * This is defined as a raw_spinlock_t in order to help
249 * with performance when lockdep debugging is enabled.
251 static raw_spinlock_t ftrace_max_lock =
252 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
255 * Copy the new maximum trace into the separate maximum-trace
256 * structure. (this way the maximum trace is permanently saved,
257 * for later retrieval via /debugfs/tracing/latency_trace)
260 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
262 struct trace_array_cpu *data = tr->data[cpu];
265 max_tr.time_start = data->preempt_timestamp;
267 data = max_tr.data[cpu];
268 data->saved_latency = tracing_max_latency;
270 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
271 data->pid = tsk->pid;
272 data->uid = tsk->uid;
273 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
274 data->policy = tsk->policy;
275 data->rt_priority = tsk->rt_priority;
277 /* record this tasks comm */
278 tracing_record_cmdline(current);
281 #define CHECK_COND(cond) \
282 if (unlikely(cond)) { \
283 tracing_disabled = 1; \
289 * check_pages - integrity check of trace buffers
291 * As a safty measure we check to make sure the data pages have not
294 int check_pages(struct trace_array_cpu *data)
296 struct page *page, *tmp;
298 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
299 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
301 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
302 CHECK_COND(page->lru.next->prev != &page->lru);
303 CHECK_COND(page->lru.prev->next != &page->lru);
310 * head_page - page address of the first page in per_cpu buffer.
312 * head_page returns the page address of the first page in
313 * a per_cpu buffer. This also preforms various consistency
314 * checks to make sure the buffer has not been corrupted.
316 void *head_page(struct trace_array_cpu *data)
320 if (list_empty(&data->trace_pages))
323 page = list_entry(data->trace_pages.next, struct page, lru);
324 BUG_ON(&page->lru == &data->trace_pages);
326 return page_address(page);
330 * trace_seq_printf - sequence printing of trace information
331 * @s: trace sequence descriptor
332 * @fmt: printf format string
334 * The tracer may use either sequence operations or its own
335 * copy to user routines. To simplify formating of a trace
336 * trace_seq_printf is used to store strings into a special
337 * buffer (@s). Then the output may be either used by
338 * the sequencer or pulled into another buffer.
341 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
343 int len = (PAGE_SIZE - 1) - s->len;
351 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
354 /* If we can't write it all, don't bother writing anything */
364 * trace_seq_puts - trace sequence printing of simple string
365 * @s: trace sequence descriptor
366 * @str: simple string to record
368 * The tracer may use either the sequence operations or its own
369 * copy to user routines. This function records a simple string
370 * into a special buffer (@s) for later retrieval by a sequencer
371 * or other mechanism.
374 trace_seq_puts(struct trace_seq *s, const char *str)
376 int len = strlen(str);
378 if (len > ((PAGE_SIZE - 1) - s->len))
381 memcpy(s->buffer + s->len, str, len);
388 trace_seq_putc(struct trace_seq *s, unsigned char c)
390 if (s->len >= (PAGE_SIZE - 1))
393 s->buffer[s->len++] = c;
399 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
401 if (len > ((PAGE_SIZE - 1) - s->len))
404 memcpy(s->buffer + s->len, mem, len);
411 static const char hex2asc[] = "0123456789abcdef";
414 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
416 unsigned char hex[HEX_CHARS];
417 unsigned char *data = mem;
421 BUG_ON(len >= HEX_CHARS);
424 for (i = 0, j = 0; i < len; i++) {
426 for (i = len-1, j = 0; i >= 0; i--) {
430 hex[j++] = hex2asc[byte & 0x0f];
431 hex[j++] = hex2asc[byte >> 4];
435 return trace_seq_putmem(s, hex, j);
439 trace_seq_reset(struct trace_seq *s)
445 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
450 if (s->len <= s->readpos)
453 len = s->len - s->readpos;
456 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
465 trace_print_seq(struct seq_file *m, struct trace_seq *s)
467 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
470 seq_puts(m, s->buffer);
476 * flip the trace buffers between two trace descriptors.
477 * This usually is the buffers between the global_trace and
478 * the max_tr to record a snapshot of a current trace.
480 * The ftrace_max_lock must be held.
483 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
485 struct list_head flip_pages;
487 INIT_LIST_HEAD(&flip_pages);
489 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
490 sizeof(struct trace_array_cpu) -
491 offsetof(struct trace_array_cpu, trace_head_idx));
495 list_splice_init(&tr1->trace_pages, &flip_pages);
496 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
497 list_splice_init(&flip_pages, &tr2->trace_pages);
498 BUG_ON(!list_empty(&flip_pages));
504 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
506 * @tsk: the task with the latency
507 * @cpu: The cpu that initiated the trace.
509 * Flip the buffers between the @tr and the max_tr and record information
510 * about which task was the cause of this latency.
513 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
515 struct trace_array_cpu *data;
518 WARN_ON_ONCE(!irqs_disabled());
519 __raw_spin_lock(&ftrace_max_lock);
520 /* clear out all the previous traces */
521 for_each_tracing_cpu(i) {
523 flip_trace(max_tr.data[i], data);
527 __update_max_tr(tr, tsk, cpu);
528 __raw_spin_unlock(&ftrace_max_lock);
532 * update_max_tr_single - only copy one trace over, and reset the rest
534 * @tsk - task with the latency
535 * @cpu - the cpu of the buffer to copy.
537 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
540 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
542 struct trace_array_cpu *data = tr->data[cpu];
545 WARN_ON_ONCE(!irqs_disabled());
546 __raw_spin_lock(&ftrace_max_lock);
547 for_each_tracing_cpu(i)
548 tracing_reset(max_tr.data[i]);
550 flip_trace(max_tr.data[cpu], data);
553 __update_max_tr(tr, tsk, cpu);
554 __raw_spin_unlock(&ftrace_max_lock);
558 * register_tracer - register a tracer with the ftrace system.
559 * @type - the plugin for the tracer
561 * Register a new plugin tracer.
563 int register_tracer(struct tracer *type)
570 pr_info("Tracer must have a name\n");
574 mutex_lock(&trace_types_lock);
575 for (t = trace_types; t; t = t->next) {
576 if (strcmp(type->name, t->name) == 0) {
578 pr_info("Trace %s already registered\n",
585 #ifdef CONFIG_FTRACE_STARTUP_TEST
586 if (type->selftest) {
587 struct tracer *saved_tracer = current_trace;
588 struct trace_array_cpu *data;
589 struct trace_array *tr = &global_trace;
590 int saved_ctrl = tr->ctrl;
593 * Run a selftest on this tracer.
594 * Here we reset the trace buffer, and set the current
595 * tracer to be this tracer. The tracer can then run some
596 * internal tracing to verify that everything is in order.
597 * If we fail, we do not register this tracer.
599 for_each_tracing_cpu(i) {
601 if (!head_page(data))
605 current_trace = type;
607 /* the test is responsible for initializing and enabling */
608 pr_info("Testing tracer %s: ", type->name);
609 ret = type->selftest(type, tr);
610 /* the test is responsible for resetting too */
611 current_trace = saved_tracer;
612 tr->ctrl = saved_ctrl;
614 printk(KERN_CONT "FAILED!\n");
617 /* Only reset on passing, to avoid touching corrupted buffers */
618 for_each_tracing_cpu(i) {
620 if (!head_page(data))
624 printk(KERN_CONT "PASSED\n");
628 type->next = trace_types;
630 len = strlen(type->name);
631 if (len > max_tracer_type_len)
632 max_tracer_type_len = len;
635 mutex_unlock(&trace_types_lock);
640 void unregister_tracer(struct tracer *type)
645 mutex_lock(&trace_types_lock);
646 for (t = &trace_types; *t; t = &(*t)->next) {
650 pr_info("Trace %s not registered\n", type->name);
655 if (strlen(type->name) != max_tracer_type_len)
658 max_tracer_type_len = 0;
659 for (t = &trace_types; *t; t = &(*t)->next) {
660 len = strlen((*t)->name);
661 if (len > max_tracer_type_len)
662 max_tracer_type_len = len;
665 mutex_unlock(&trace_types_lock);
668 void tracing_reset(struct trace_array_cpu *data)
672 data->trace_head = data->trace_tail = head_page(data);
673 data->trace_head_idx = 0;
674 data->trace_tail_idx = 0;
677 #define SAVED_CMDLINES 128
678 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
679 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
680 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
681 static int cmdline_idx;
682 static DEFINE_SPINLOCK(trace_cmdline_lock);
684 /* temporary disable recording */
685 atomic_t trace_record_cmdline_disabled __read_mostly;
687 static void trace_init_cmdlines(void)
689 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
690 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
694 void trace_stop_cmdline_recording(void);
696 static void trace_save_cmdline(struct task_struct *tsk)
701 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
705 * It's not the end of the world if we don't get
706 * the lock, but we also don't want to spin
707 * nor do we want to disable interrupts,
708 * so if we miss here, then better luck next time.
710 if (!spin_trylock(&trace_cmdline_lock))
713 idx = map_pid_to_cmdline[tsk->pid];
714 if (idx >= SAVED_CMDLINES) {
715 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
717 map = map_cmdline_to_pid[idx];
718 if (map <= PID_MAX_DEFAULT)
719 map_pid_to_cmdline[map] = (unsigned)-1;
721 map_pid_to_cmdline[tsk->pid] = idx;
726 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
728 spin_unlock(&trace_cmdline_lock);
731 static char *trace_find_cmdline(int pid)
733 char *cmdline = "<...>";
739 if (pid > PID_MAX_DEFAULT)
742 map = map_pid_to_cmdline[pid];
743 if (map >= SAVED_CMDLINES)
746 cmdline = saved_cmdlines[map];
752 void tracing_record_cmdline(struct task_struct *tsk)
754 if (atomic_read(&trace_record_cmdline_disabled))
757 trace_save_cmdline(tsk);
760 static inline struct list_head *
761 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
764 * Roundrobin - but skip the head (which is not a real page):
767 if (unlikely(next == &data->trace_pages))
769 BUG_ON(next == &data->trace_pages);
775 trace_next_page(struct trace_array_cpu *data, void *addr)
777 struct list_head *next;
780 page = virt_to_page(addr);
782 next = trace_next_list(data, &page->lru);
783 page = list_entry(next, struct page, lru);
785 return page_address(page);
788 static inline struct trace_entry *
789 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
791 unsigned long idx, idx_next;
792 struct trace_entry *entry;
795 idx = data->trace_head_idx;
798 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
800 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
802 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
803 data->trace_head = trace_next_page(data, data->trace_head);
807 if (data->trace_head == data->trace_tail &&
808 idx_next == data->trace_tail_idx) {
811 data->trace_tail_idx++;
812 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
814 trace_next_page(data, data->trace_tail);
815 data->trace_tail_idx = 0;
819 data->trace_head_idx = idx_next;
825 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
827 struct task_struct *tsk = current;
830 pc = preempt_count();
832 entry->field.preempt_count = pc & 0xff;
833 entry->field.pid = (tsk) ? tsk->pid : 0;
834 entry->field.t = ftrace_now(raw_smp_processor_id());
836 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
837 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
838 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
839 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
843 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
844 unsigned long ip, unsigned long parent_ip, unsigned long flags)
846 struct trace_entry *entry;
847 unsigned long irq_flags;
849 raw_local_irq_save(irq_flags);
850 __raw_spin_lock(&data->lock);
851 entry = tracing_get_trace_entry(tr, data);
852 tracing_generic_entry_update(entry, flags);
853 entry->type = TRACE_FN;
854 entry->field.fn.ip = ip;
855 entry->field.fn.parent_ip = parent_ip;
856 __raw_spin_unlock(&data->lock);
857 raw_local_irq_restore(irq_flags);
861 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
862 unsigned long ip, unsigned long parent_ip, unsigned long flags)
864 if (likely(!atomic_read(&data->disabled)))
865 trace_function(tr, data, ip, parent_ip, flags);
868 #ifdef CONFIG_MMIOTRACE
869 void __trace_mmiotrace_rw(struct trace_array *tr, struct trace_array_cpu *data,
870 struct mmiotrace_rw *rw)
872 struct trace_entry *entry;
873 unsigned long irq_flags;
875 raw_local_irq_save(irq_flags);
876 __raw_spin_lock(&data->lock);
878 entry = tracing_get_trace_entry(tr, data);
879 tracing_generic_entry_update(entry, 0);
880 entry->type = TRACE_MMIO_RW;
881 entry->field.mmiorw = *rw;
883 __raw_spin_unlock(&data->lock);
884 raw_local_irq_restore(irq_flags);
889 void __trace_mmiotrace_map(struct trace_array *tr, struct trace_array_cpu *data,
890 struct mmiotrace_map *map)
892 struct trace_entry *entry;
893 unsigned long irq_flags;
895 raw_local_irq_save(irq_flags);
896 __raw_spin_lock(&data->lock);
898 entry = tracing_get_trace_entry(tr, data);
899 tracing_generic_entry_update(entry, 0);
900 entry->type = TRACE_MMIO_MAP;
901 entry->field.mmiomap = *map;
903 __raw_spin_unlock(&data->lock);
904 raw_local_irq_restore(irq_flags);
910 void __trace_stack(struct trace_array *tr,
911 struct trace_array_cpu *data,
915 struct trace_entry *entry;
916 struct stack_trace trace;
918 if (!(trace_flags & TRACE_ITER_STACKTRACE))
921 entry = tracing_get_trace_entry(tr, data);
922 tracing_generic_entry_update(entry, flags);
923 entry->type = TRACE_STACK;
925 memset(&entry->field.stack, 0, sizeof(entry->field.stack));
927 trace.nr_entries = 0;
928 trace.max_entries = FTRACE_STACK_ENTRIES;
930 trace.entries = entry->field.stack.caller;
932 save_stack_trace(&trace);
936 __trace_special(void *__tr, void *__data,
937 unsigned long arg1, unsigned long arg2, unsigned long arg3)
939 struct trace_array_cpu *data = __data;
940 struct trace_array *tr = __tr;
941 struct trace_entry *entry;
942 unsigned long irq_flags;
944 raw_local_irq_save(irq_flags);
945 __raw_spin_lock(&data->lock);
946 entry = tracing_get_trace_entry(tr, data);
947 tracing_generic_entry_update(entry, 0);
948 entry->type = TRACE_SPECIAL;
949 entry->field.special.arg1 = arg1;
950 entry->field.special.arg2 = arg2;
951 entry->field.special.arg3 = arg3;
952 __trace_stack(tr, data, irq_flags, 4);
953 __raw_spin_unlock(&data->lock);
954 raw_local_irq_restore(irq_flags);
960 tracing_sched_switch_trace(struct trace_array *tr,
961 struct trace_array_cpu *data,
962 struct task_struct *prev,
963 struct task_struct *next,
966 struct trace_entry *entry;
967 unsigned long irq_flags;
969 raw_local_irq_save(irq_flags);
970 __raw_spin_lock(&data->lock);
971 entry = tracing_get_trace_entry(tr, data);
972 tracing_generic_entry_update(entry, flags);
973 entry->type = TRACE_CTX;
974 entry->field.ctx.prev_pid = prev->pid;
975 entry->field.ctx.prev_prio = prev->prio;
976 entry->field.ctx.prev_state = prev->state;
977 entry->field.ctx.next_pid = next->pid;
978 entry->field.ctx.next_prio = next->prio;
979 entry->field.ctx.next_state = next->state;
980 __trace_stack(tr, data, flags, 5);
981 __raw_spin_unlock(&data->lock);
982 raw_local_irq_restore(irq_flags);
986 tracing_sched_wakeup_trace(struct trace_array *tr,
987 struct trace_array_cpu *data,
988 struct task_struct *wakee,
989 struct task_struct *curr,
992 struct trace_entry *entry;
993 unsigned long irq_flags;
995 raw_local_irq_save(irq_flags);
996 __raw_spin_lock(&data->lock);
997 entry = tracing_get_trace_entry(tr, data);
998 tracing_generic_entry_update(entry, flags);
999 entry->type = TRACE_WAKE;
1000 entry->field.ctx.prev_pid = curr->pid;
1001 entry->field.ctx.prev_prio = curr->prio;
1002 entry->field.ctx.prev_state = curr->state;
1003 entry->field.ctx.next_pid = wakee->pid;
1004 entry->field.ctx.next_prio = wakee->prio;
1005 entry->field.ctx.next_state = wakee->state;
1006 __trace_stack(tr, data, flags, 6);
1007 __raw_spin_unlock(&data->lock);
1008 raw_local_irq_restore(irq_flags);
1014 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1016 struct trace_array *tr = &global_trace;
1017 struct trace_array_cpu *data;
1018 unsigned long flags;
1022 if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
1025 local_irq_save(flags);
1026 cpu = raw_smp_processor_id();
1027 data = tr->data[cpu];
1028 disabled = atomic_inc_return(&data->disabled);
1030 if (likely(disabled == 1))
1031 __trace_special(tr, data, arg1, arg2, arg3);
1033 atomic_dec(&data->disabled);
1034 local_irq_restore(flags);
1037 #ifdef CONFIG_FTRACE
1039 function_trace_call(unsigned long ip, unsigned long parent_ip)
1041 struct trace_array *tr = &global_trace;
1042 struct trace_array_cpu *data;
1043 unsigned long flags;
1047 if (unlikely(!ftrace_function_enabled))
1053 local_irq_save(flags);
1054 cpu = raw_smp_processor_id();
1055 data = tr->data[cpu];
1056 disabled = atomic_inc_return(&data->disabled);
1058 if (likely(disabled == 1))
1059 trace_function(tr, data, ip, parent_ip, flags);
1061 atomic_dec(&data->disabled);
1062 local_irq_restore(flags);
1065 static struct ftrace_ops trace_ops __read_mostly =
1067 .func = function_trace_call,
1070 void tracing_start_function_trace(void)
1072 ftrace_function_enabled = 0;
1073 register_ftrace_function(&trace_ops);
1075 ftrace_function_enabled = 1;
1078 void tracing_stop_function_trace(void)
1080 ftrace_function_enabled = 0;
1081 unregister_ftrace_function(&trace_ops);
1085 enum trace_file_type {
1086 TRACE_FILE_LAT_FMT = 1,
1089 /* Return the current entry. */
1090 static struct trace_entry *
1091 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1092 struct trace_iterator *iter, int cpu)
1095 struct trace_entry *array;
1097 if (iter->next_idx[cpu] >= tr->entries ||
1098 iter->next_idx[cpu] >= data->trace_idx ||
1099 (data->trace_head == data->trace_tail &&
1100 data->trace_head_idx == data->trace_tail_idx))
1103 if (!iter->next_page[cpu]) {
1104 /* Initialize the iterator for this cpu trace buffer */
1105 WARN_ON(!data->trace_tail);
1106 page = virt_to_page(data->trace_tail);
1107 iter->next_page[cpu] = &page->lru;
1108 iter->next_page_idx[cpu] = data->trace_tail_idx;
1111 page = list_entry(iter->next_page[cpu], struct page, lru);
1112 BUG_ON(&data->trace_pages == &page->lru);
1114 array = page_address(page);
1116 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
1117 return &array[iter->next_page_idx[cpu]];
1120 /* Increment the index counter of an iterator by one */
1121 static void __trace_iterator_increment(struct trace_iterator *iter, int cpu)
1123 iter->next_idx[cpu]++;
1124 iter->next_page_idx[cpu]++;
1126 if (iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE) {
1127 struct trace_array_cpu *data = iter->tr->data[cpu];
1129 iter->next_page_idx[cpu] = 0;
1130 iter->next_page[cpu] =
1131 trace_next_list(data, iter->next_page[cpu]);
1135 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1138 __trace_iterator_increment(iter, cpu);
1141 static struct trace_entry *
1142 trace_entry_next(struct trace_array *tr, struct trace_array_cpu *data,
1143 struct trace_iterator *iter, int cpu)
1145 struct list_head *next_page;
1146 struct trace_entry *ent;
1147 int idx, next_idx, next_page_idx;
1149 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1151 if (likely(!ent || ent->type != TRACE_CONT))
1154 /* save the iterator details */
1156 next_idx = iter->next_idx[cpu];
1157 next_page_idx = iter->next_page_idx[cpu];
1158 next_page = iter->next_page[cpu];
1160 /* find a real entry */
1162 __trace_iterator_increment(iter, cpu);
1163 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1164 } while (ent && ent->type != TRACE_CONT);
1166 /* reset the iterator */
1168 iter->next_idx[cpu] = next_idx;
1169 iter->next_page_idx[cpu] = next_page_idx;
1170 iter->next_page[cpu] = next_page;
1175 static struct trace_entry *
1176 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, int inc)
1178 struct trace_array *tr = iter->tr;
1179 struct trace_entry *ent, *next = NULL;
1183 for_each_tracing_cpu(cpu) {
1184 if (!head_page(tr->data[cpu]))
1187 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1189 if (ent && ent->type == TRACE_CONT) {
1190 struct trace_array_cpu *data = tr->data[cpu];
1193 ent = trace_entry_next(tr, data, iter, cpu);
1195 while (ent && ent->type == TRACE_CONT) {
1196 __trace_iterator_increment(iter, cpu);
1197 ent = trace_entry_idx(tr, tr->data[cpu],
1204 * Pick the entry with the smallest timestamp:
1206 if (ent && (!next || ent->field.t < next->field.t)) {
1213 *ent_cpu = next_cpu;
1218 /* Find the next real entry, without updating the iterator itself */
1219 static struct trace_entry *
1220 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1222 return __find_next_entry(iter, ent_cpu, 0);
1225 /* Find the next real entry, and increment the iterator to the next entry */
1226 static void *find_next_entry_inc(struct trace_iterator *iter)
1228 struct trace_entry *next;
1231 next = __find_next_entry(iter, &next_cpu, 1);
1233 iter->prev_ent = iter->ent;
1234 iter->prev_cpu = iter->cpu;
1237 iter->cpu = next_cpu;
1240 trace_iterator_increment(iter, iter->cpu);
1242 return next ? iter : NULL;
1245 static void trace_consume(struct trace_iterator *iter)
1247 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1248 struct trace_entry *ent;
1251 data->trace_tail_idx++;
1252 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1253 data->trace_tail = trace_next_page(data, data->trace_tail);
1254 data->trace_tail_idx = 0;
1257 /* Check if we empty it, then reset the index */
1258 if (data->trace_head == data->trace_tail &&
1259 data->trace_head_idx == data->trace_tail_idx)
1260 data->trace_idx = 0;
1262 ent = trace_entry_idx(iter->tr, iter->tr->data[iter->cpu],
1264 if (ent && ent->type == TRACE_CONT)
1268 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1270 struct trace_iterator *iter = m->private;
1276 /* can't go backwards */
1281 ent = find_next_entry_inc(iter);
1285 while (ent && iter->idx < i)
1286 ent = find_next_entry_inc(iter);
1293 static void *s_start(struct seq_file *m, loff_t *pos)
1295 struct trace_iterator *iter = m->private;
1300 mutex_lock(&trace_types_lock);
1302 if (!current_trace || current_trace != iter->trace) {
1303 mutex_unlock(&trace_types_lock);
1307 atomic_inc(&trace_record_cmdline_disabled);
1309 /* let the tracer grab locks here if needed */
1310 if (current_trace->start)
1311 current_trace->start(iter);
1313 if (*pos != iter->pos) {
1317 iter->prev_ent = NULL;
1318 iter->prev_cpu = -1;
1320 for_each_tracing_cpu(i) {
1321 iter->next_idx[i] = 0;
1322 iter->next_page[i] = NULL;
1325 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1330 p = s_next(m, p, &l);
1336 static void s_stop(struct seq_file *m, void *p)
1338 struct trace_iterator *iter = m->private;
1340 atomic_dec(&trace_record_cmdline_disabled);
1342 /* let the tracer release locks here if needed */
1343 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1344 iter->trace->stop(iter);
1346 mutex_unlock(&trace_types_lock);
1349 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1351 #ifdef CONFIG_KRETPROBES
1352 static inline int kretprobed(unsigned long addr)
1354 return addr == (unsigned long)kretprobe_trampoline;
1357 static inline int kretprobed(unsigned long addr)
1361 #endif /* CONFIG_KRETPROBES */
1364 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1366 #ifdef CONFIG_KALLSYMS
1367 char str[KSYM_SYMBOL_LEN];
1369 kallsyms_lookup(address, NULL, NULL, NULL, str);
1371 return trace_seq_printf(s, fmt, str);
1377 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1378 unsigned long address)
1380 #ifdef CONFIG_KALLSYMS
1381 char str[KSYM_SYMBOL_LEN];
1383 sprint_symbol(str, address);
1384 return trace_seq_printf(s, fmt, str);
1389 #ifndef CONFIG_64BIT
1390 # define IP_FMT "%08lx"
1392 # define IP_FMT "%016lx"
1396 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1401 return trace_seq_printf(s, "0");
1403 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1404 ret = seq_print_sym_offset(s, "%s", ip);
1406 ret = seq_print_sym_short(s, "%s", ip);
1411 if (sym_flags & TRACE_ITER_SYM_ADDR)
1412 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1416 static void print_lat_help_header(struct seq_file *m)
1418 seq_puts(m, "# _------=> CPU# \n");
1419 seq_puts(m, "# / _-----=> irqs-off \n");
1420 seq_puts(m, "# | / _----=> need-resched \n");
1421 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1422 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1423 seq_puts(m, "# |||| / \n");
1424 seq_puts(m, "# ||||| delay \n");
1425 seq_puts(m, "# cmd pid ||||| time | caller \n");
1426 seq_puts(m, "# \\ / ||||| \\ | / \n");
1429 static void print_func_help_header(struct seq_file *m)
1431 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1432 seq_puts(m, "# | | | | |\n");
1437 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1439 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1440 struct trace_array *tr = iter->tr;
1441 struct trace_array_cpu *data = tr->data[tr->cpu];
1442 struct tracer *type = current_trace;
1443 unsigned long total = 0;
1444 unsigned long entries = 0;
1446 const char *name = "preemption";
1451 for_each_tracing_cpu(cpu) {
1452 if (head_page(tr->data[cpu])) {
1453 total += tr->data[cpu]->trace_idx;
1454 if (tr->data[cpu]->trace_idx > tr->entries)
1455 entries += tr->entries;
1457 entries += tr->data[cpu]->trace_idx;
1461 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1463 seq_puts(m, "-----------------------------------"
1464 "---------------------------------\n");
1465 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1466 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1467 nsecs_to_usecs(data->saved_latency),
1471 #if defined(CONFIG_PREEMPT_NONE)
1473 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1475 #elif defined(CONFIG_PREEMPT)
1480 /* These are reserved for later use */
1483 seq_printf(m, " #P:%d)\n", num_online_cpus());
1487 seq_puts(m, " -----------------\n");
1488 seq_printf(m, " | task: %.16s-%d "
1489 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1490 data->comm, data->pid, data->uid, data->nice,
1491 data->policy, data->rt_priority);
1492 seq_puts(m, " -----------------\n");
1494 if (data->critical_start) {
1495 seq_puts(m, " => started at: ");
1496 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1497 trace_print_seq(m, &iter->seq);
1498 seq_puts(m, "\n => ended at: ");
1499 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1500 trace_print_seq(m, &iter->seq);
1508 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1510 struct trace_field *field = &entry->field;
1511 int hardirq, softirq;
1514 comm = trace_find_cmdline(field->pid);
1516 trace_seq_printf(s, "%8.8s-%-5d ", comm, field->pid);
1517 trace_seq_printf(s, "%3d", cpu);
1518 trace_seq_printf(s, "%c%c",
1519 (field->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1520 ((field->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1522 hardirq = field->flags & TRACE_FLAG_HARDIRQ;
1523 softirq = field->flags & TRACE_FLAG_SOFTIRQ;
1524 if (hardirq && softirq) {
1525 trace_seq_putc(s, 'H');
1528 trace_seq_putc(s, 'h');
1531 trace_seq_putc(s, 's');
1533 trace_seq_putc(s, '.');
1537 if (field->preempt_count)
1538 trace_seq_printf(s, "%x", field->preempt_count);
1540 trace_seq_puts(s, ".");
1543 unsigned long preempt_mark_thresh = 100;
1546 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1547 unsigned long rel_usecs)
1549 trace_seq_printf(s, " %4lldus", abs_usecs);
1550 if (rel_usecs > preempt_mark_thresh)
1551 trace_seq_puts(s, "!: ");
1552 else if (rel_usecs > 1)
1553 trace_seq_puts(s, "+: ");
1555 trace_seq_puts(s, " : ");
1558 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1561 trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1563 struct trace_array *tr = iter->tr;
1564 struct trace_array_cpu *data = tr->data[iter->cpu];
1565 struct trace_entry *ent;
1567 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1568 if (!ent || ent->type != TRACE_CONT) {
1569 trace_seq_putc(s, '\n');
1574 trace_seq_printf(s, "%s", ent->cont.buf);
1575 __trace_iterator_increment(iter, iter->cpu);
1576 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1577 } while (ent && ent->type == TRACE_CONT);
1581 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1583 struct trace_seq *s = &iter->seq;
1584 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1585 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1586 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1587 struct trace_entry *entry = iter->ent;
1588 struct trace_field *field = &entry->field;
1589 unsigned long abs_usecs;
1590 unsigned long rel_usecs;
1599 if (entry->type == TRACE_CONT)
1602 rel_usecs = ns2usecs(next_entry->field.t - entry->field.t);
1603 abs_usecs = ns2usecs(entry->field.t - iter->tr->time_start);
1606 comm = trace_find_cmdline(field->pid);
1607 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1608 " %ld.%03ldms (+%ld.%03ldms): ",
1610 field->pid, cpu, field->flags,
1611 field->preempt_count, trace_idx,
1614 abs_usecs % 1000, rel_usecs/1000,
1617 lat_print_generic(s, entry, cpu);
1618 lat_print_timestamp(s, abs_usecs, rel_usecs);
1620 switch (entry->type) {
1622 seq_print_ip_sym(s, field->fn.ip, sym_flags);
1623 trace_seq_puts(s, " (");
1624 if (kretprobed(field->fn.parent_ip))
1625 trace_seq_puts(s, KRETPROBE_MSG);
1627 seq_print_ip_sym(s, field->fn.parent_ip, sym_flags);
1628 trace_seq_puts(s, ")\n");
1632 T = field->ctx.next_state < sizeof(state_to_char) ?
1633 state_to_char[field->ctx.next_state] : 'X';
1635 state = field->ctx.prev_state ?
1636 __ffs(field->ctx.prev_state) + 1 : 0;
1637 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1638 comm = trace_find_cmdline(field->ctx.next_pid);
1639 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1640 field->ctx.prev_pid,
1641 field->ctx.prev_prio,
1642 S, entry->type == TRACE_CTX ? "==>" : " +",
1643 field->ctx.next_pid,
1644 field->ctx.next_prio,
1648 trace_seq_printf(s, "# %ld %ld %ld\n",
1649 field->special.arg1,
1650 field->special.arg2,
1651 field->special.arg3);
1654 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1656 trace_seq_puts(s, " <= ");
1657 seq_print_ip_sym(s, field->stack.caller[i], sym_flags);
1659 trace_seq_puts(s, "\n");
1662 seq_print_ip_sym(s, field->print.ip, sym_flags);
1663 trace_seq_printf(s, ": %s", field->print.buf);
1664 if (field->flags & TRACE_FLAG_CONT)
1665 trace_seq_print_cont(s, iter);
1668 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1673 static int print_trace_fmt(struct trace_iterator *iter)
1675 struct trace_seq *s = &iter->seq;
1676 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1677 struct trace_entry *entry;
1678 struct trace_field *field;
1679 unsigned long usec_rem;
1680 unsigned long long t;
1689 if (entry->type == TRACE_CONT)
1692 field = &entry->field;
1694 comm = trace_find_cmdline(iter->ent->field.pid);
1696 t = ns2usecs(field->t);
1697 usec_rem = do_div(t, 1000000ULL);
1698 secs = (unsigned long)t;
1700 ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid);
1703 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1706 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1710 switch (entry->type) {
1712 ret = seq_print_ip_sym(s, field->fn.ip, sym_flags);
1715 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1716 field->fn.parent_ip) {
1717 ret = trace_seq_printf(s, " <-");
1720 if (kretprobed(field->fn.parent_ip))
1721 ret = trace_seq_puts(s, KRETPROBE_MSG);
1723 ret = seq_print_ip_sym(s,
1724 field->fn.parent_ip,
1729 ret = trace_seq_printf(s, "\n");
1735 S = field->ctx.prev_state < sizeof(state_to_char) ?
1736 state_to_char[field->ctx.prev_state] : 'X';
1737 T = field->ctx.next_state < sizeof(state_to_char) ?
1738 state_to_char[field->ctx.next_state] : 'X';
1739 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
1740 field->ctx.prev_pid,
1741 field->ctx.prev_prio,
1743 entry->type == TRACE_CTX ? "==>" : " +",
1744 field->ctx.next_pid,
1745 field->ctx.next_prio,
1751 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1752 field->special.arg1,
1753 field->special.arg2,
1754 field->special.arg3);
1759 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1761 ret = trace_seq_puts(s, " <= ");
1765 ret = seq_print_ip_sym(s, field->stack.caller[i],
1770 ret = trace_seq_puts(s, "\n");
1775 seq_print_ip_sym(s, field->print.ip, sym_flags);
1776 trace_seq_printf(s, ": %s", field->print.buf);
1777 if (field->flags & TRACE_FLAG_CONT)
1778 trace_seq_print_cont(s, iter);
1784 static int print_raw_fmt(struct trace_iterator *iter)
1786 struct trace_seq *s = &iter->seq;
1787 struct trace_entry *entry;
1788 struct trace_field *field;
1794 if (entry->type == TRACE_CONT)
1797 field = &entry->field;
1799 ret = trace_seq_printf(s, "%d %d %llu ",
1800 field->pid, iter->cpu, field->t);
1804 switch (entry->type) {
1806 ret = trace_seq_printf(s, "%x %x\n",
1808 field->fn.parent_ip);
1814 S = field->ctx.prev_state < sizeof(state_to_char) ?
1815 state_to_char[field->ctx.prev_state] : 'X';
1816 T = field->ctx.next_state < sizeof(state_to_char) ?
1817 state_to_char[field->ctx.next_state] : 'X';
1818 if (entry->type == TRACE_WAKE)
1820 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
1821 field->ctx.prev_pid,
1822 field->ctx.prev_prio,
1824 field->ctx.next_pid,
1825 field->ctx.next_prio,
1832 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1833 field->special.arg1,
1834 field->special.arg2,
1835 field->special.arg3);
1840 trace_seq_printf(s, "# %lx %s",
1841 field->print.ip, field->print.buf);
1842 if (field->flags & TRACE_FLAG_CONT)
1843 trace_seq_print_cont(s, iter);
1849 #define SEQ_PUT_FIELD_RET(s, x) \
1851 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1855 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1857 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1861 static int print_hex_fmt(struct trace_iterator *iter)
1863 struct trace_seq *s = &iter->seq;
1864 unsigned char newline = '\n';
1865 struct trace_entry *entry;
1866 struct trace_field *field;
1871 if (entry->type == TRACE_CONT)
1874 field = &entry->field;
1876 SEQ_PUT_HEX_FIELD_RET(s, field->pid);
1877 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1878 SEQ_PUT_HEX_FIELD_RET(s, field->t);
1880 switch (entry->type) {
1882 SEQ_PUT_HEX_FIELD_RET(s, field->fn.ip);
1883 SEQ_PUT_HEX_FIELD_RET(s, field->fn.parent_ip);
1887 S = field->ctx.prev_state < sizeof(state_to_char) ?
1888 state_to_char[field->ctx.prev_state] : 'X';
1889 T = field->ctx.next_state < sizeof(state_to_char) ?
1890 state_to_char[field->ctx.next_state] : 'X';
1891 if (entry->type == TRACE_WAKE)
1893 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_pid);
1894 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_prio);
1895 SEQ_PUT_HEX_FIELD_RET(s, S);
1896 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_pid);
1897 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_prio);
1898 SEQ_PUT_HEX_FIELD_RET(s, T);
1902 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg1);
1903 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg2);
1904 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg3);
1907 SEQ_PUT_FIELD_RET(s, newline);
1912 static int print_bin_fmt(struct trace_iterator *iter)
1914 struct trace_seq *s = &iter->seq;
1915 struct trace_entry *entry;
1916 struct trace_field *field;
1920 if (entry->type == TRACE_CONT)
1923 field = &entry->field;
1925 SEQ_PUT_FIELD_RET(s, field->pid);
1926 SEQ_PUT_FIELD_RET(s, field->cpu);
1927 SEQ_PUT_FIELD_RET(s, field->t);
1929 switch (entry->type) {
1931 SEQ_PUT_FIELD_RET(s, field->fn.ip);
1932 SEQ_PUT_FIELD_RET(s, field->fn.parent_ip);
1935 SEQ_PUT_FIELD_RET(s, field->ctx.prev_pid);
1936 SEQ_PUT_FIELD_RET(s, field->ctx.prev_prio);
1937 SEQ_PUT_FIELD_RET(s, field->ctx.prev_state);
1938 SEQ_PUT_FIELD_RET(s, field->ctx.next_pid);
1939 SEQ_PUT_FIELD_RET(s, field->ctx.next_prio);
1940 SEQ_PUT_FIELD_RET(s, field->ctx.next_state);
1944 SEQ_PUT_FIELD_RET(s, field->special.arg1);
1945 SEQ_PUT_FIELD_RET(s, field->special.arg2);
1946 SEQ_PUT_FIELD_RET(s, field->special.arg3);
1952 static int trace_empty(struct trace_iterator *iter)
1954 struct trace_array_cpu *data;
1957 for_each_tracing_cpu(cpu) {
1958 data = iter->tr->data[cpu];
1960 if (head_page(data) && data->trace_idx &&
1961 (data->trace_tail != data->trace_head ||
1962 data->trace_tail_idx != data->trace_head_idx))
1968 static int print_trace_line(struct trace_iterator *iter)
1970 if (iter->trace && iter->trace->print_line)
1971 return iter->trace->print_line(iter);
1973 if (trace_flags & TRACE_ITER_BIN)
1974 return print_bin_fmt(iter);
1976 if (trace_flags & TRACE_ITER_HEX)
1977 return print_hex_fmt(iter);
1979 if (trace_flags & TRACE_ITER_RAW)
1980 return print_raw_fmt(iter);
1982 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1983 return print_lat_fmt(iter, iter->idx, iter->cpu);
1985 return print_trace_fmt(iter);
1988 static int s_show(struct seq_file *m, void *v)
1990 struct trace_iterator *iter = v;
1992 if (iter->ent == NULL) {
1994 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1997 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1998 /* print nothing if the buffers are empty */
1999 if (trace_empty(iter))
2001 print_trace_header(m, iter);
2002 if (!(trace_flags & TRACE_ITER_VERBOSE))
2003 print_lat_help_header(m);
2005 if (!(trace_flags & TRACE_ITER_VERBOSE))
2006 print_func_help_header(m);
2009 print_trace_line(iter);
2010 trace_print_seq(m, &iter->seq);
2016 static struct seq_operations tracer_seq_ops = {
2023 static struct trace_iterator *
2024 __tracing_open(struct inode *inode, struct file *file, int *ret)
2026 struct trace_iterator *iter;
2028 if (tracing_disabled) {
2033 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2039 mutex_lock(&trace_types_lock);
2040 if (current_trace && current_trace->print_max)
2043 iter->tr = inode->i_private;
2044 iter->trace = current_trace;
2047 /* TODO stop tracer */
2048 *ret = seq_open(file, &tracer_seq_ops);
2050 struct seq_file *m = file->private_data;
2053 /* stop the trace while dumping */
2054 if (iter->tr->ctrl) {
2056 ftrace_function_enabled = 0;
2059 if (iter->trace && iter->trace->open)
2060 iter->trace->open(iter);
2065 mutex_unlock(&trace_types_lock);
2071 int tracing_open_generic(struct inode *inode, struct file *filp)
2073 if (tracing_disabled)
2076 filp->private_data = inode->i_private;
2080 int tracing_release(struct inode *inode, struct file *file)
2082 struct seq_file *m = (struct seq_file *)file->private_data;
2083 struct trace_iterator *iter = m->private;
2085 mutex_lock(&trace_types_lock);
2086 if (iter->trace && iter->trace->close)
2087 iter->trace->close(iter);
2089 /* reenable tracing if it was previously enabled */
2090 if (iter->tr->ctrl) {
2093 * It is safe to enable function tracing even if it
2096 ftrace_function_enabled = 1;
2098 mutex_unlock(&trace_types_lock);
2100 seq_release(inode, file);
2105 static int tracing_open(struct inode *inode, struct file *file)
2109 __tracing_open(inode, file, &ret);
2114 static int tracing_lt_open(struct inode *inode, struct file *file)
2116 struct trace_iterator *iter;
2119 iter = __tracing_open(inode, file, &ret);
2122 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2129 t_next(struct seq_file *m, void *v, loff_t *pos)
2131 struct tracer *t = m->private;
2143 static void *t_start(struct seq_file *m, loff_t *pos)
2145 struct tracer *t = m->private;
2148 mutex_lock(&trace_types_lock);
2149 for (; t && l < *pos; t = t_next(m, t, &l))
2155 static void t_stop(struct seq_file *m, void *p)
2157 mutex_unlock(&trace_types_lock);
2160 static int t_show(struct seq_file *m, void *v)
2162 struct tracer *t = v;
2167 seq_printf(m, "%s", t->name);
2176 static struct seq_operations show_traces_seq_ops = {
2183 static int show_traces_open(struct inode *inode, struct file *file)
2187 if (tracing_disabled)
2190 ret = seq_open(file, &show_traces_seq_ops);
2192 struct seq_file *m = file->private_data;
2193 m->private = trace_types;
2199 static struct file_operations tracing_fops = {
2200 .open = tracing_open,
2202 .llseek = seq_lseek,
2203 .release = tracing_release,
2206 static struct file_operations tracing_lt_fops = {
2207 .open = tracing_lt_open,
2209 .llseek = seq_lseek,
2210 .release = tracing_release,
2213 static struct file_operations show_traces_fops = {
2214 .open = show_traces_open,
2216 .release = seq_release,
2220 * Only trace on a CPU if the bitmask is set:
2222 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2225 * When tracing/tracing_cpu_mask is modified then this holds
2226 * the new bitmask we are about to install:
2228 static cpumask_t tracing_cpumask_new;
2231 * The tracer itself will not take this lock, but still we want
2232 * to provide a consistent cpumask to user-space:
2234 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2237 * Temporary storage for the character representation of the
2238 * CPU bitmask (and one more byte for the newline):
2240 static char mask_str[NR_CPUS + 1];
2243 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2244 size_t count, loff_t *ppos)
2248 mutex_lock(&tracing_cpumask_update_lock);
2250 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2251 if (count - len < 2) {
2255 len += sprintf(mask_str + len, "\n");
2256 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2259 mutex_unlock(&tracing_cpumask_update_lock);
2265 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2266 size_t count, loff_t *ppos)
2270 mutex_lock(&tracing_cpumask_update_lock);
2271 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2275 raw_local_irq_disable();
2276 __raw_spin_lock(&ftrace_max_lock);
2277 for_each_tracing_cpu(cpu) {
2279 * Increase/decrease the disabled counter if we are
2280 * about to flip a bit in the cpumask:
2282 if (cpu_isset(cpu, tracing_cpumask) &&
2283 !cpu_isset(cpu, tracing_cpumask_new)) {
2284 atomic_inc(&global_trace.data[cpu]->disabled);
2286 if (!cpu_isset(cpu, tracing_cpumask) &&
2287 cpu_isset(cpu, tracing_cpumask_new)) {
2288 atomic_dec(&global_trace.data[cpu]->disabled);
2291 __raw_spin_unlock(&ftrace_max_lock);
2292 raw_local_irq_enable();
2294 tracing_cpumask = tracing_cpumask_new;
2296 mutex_unlock(&tracing_cpumask_update_lock);
2301 mutex_unlock(&tracing_cpumask_update_lock);
2306 static struct file_operations tracing_cpumask_fops = {
2307 .open = tracing_open_generic,
2308 .read = tracing_cpumask_read,
2309 .write = tracing_cpumask_write,
2313 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2314 size_t cnt, loff_t *ppos)
2321 /* calulate max size */
2322 for (i = 0; trace_options[i]; i++) {
2323 len += strlen(trace_options[i]);
2324 len += 3; /* "no" and space */
2327 /* +2 for \n and \0 */
2328 buf = kmalloc(len + 2, GFP_KERNEL);
2332 for (i = 0; trace_options[i]; i++) {
2333 if (trace_flags & (1 << i))
2334 r += sprintf(buf + r, "%s ", trace_options[i]);
2336 r += sprintf(buf + r, "no%s ", trace_options[i]);
2339 r += sprintf(buf + r, "\n");
2340 WARN_ON(r >= len + 2);
2342 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2350 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2351 size_t cnt, loff_t *ppos)
2358 if (cnt >= sizeof(buf))
2361 if (copy_from_user(&buf, ubuf, cnt))
2366 if (strncmp(buf, "no", 2) == 0) {
2371 for (i = 0; trace_options[i]; i++) {
2372 int len = strlen(trace_options[i]);
2374 if (strncmp(cmp, trace_options[i], len) == 0) {
2376 trace_flags &= ~(1 << i);
2378 trace_flags |= (1 << i);
2383 * If no option could be set, return an error:
2385 if (!trace_options[i])
2393 static struct file_operations tracing_iter_fops = {
2394 .open = tracing_open_generic,
2395 .read = tracing_iter_ctrl_read,
2396 .write = tracing_iter_ctrl_write,
2399 static const char readme_msg[] =
2400 "tracing mini-HOWTO:\n\n"
2402 "# mount -t debugfs nodev /debug\n\n"
2403 "# cat /debug/tracing/available_tracers\n"
2404 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2405 "# cat /debug/tracing/current_tracer\n"
2407 "# echo sched_switch > /debug/tracing/current_tracer\n"
2408 "# cat /debug/tracing/current_tracer\n"
2410 "# cat /debug/tracing/iter_ctrl\n"
2411 "noprint-parent nosym-offset nosym-addr noverbose\n"
2412 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2413 "# echo 1 > /debug/tracing/tracing_enabled\n"
2414 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2415 "echo 0 > /debug/tracing/tracing_enabled\n"
2419 tracing_readme_read(struct file *filp, char __user *ubuf,
2420 size_t cnt, loff_t *ppos)
2422 return simple_read_from_buffer(ubuf, cnt, ppos,
2423 readme_msg, strlen(readme_msg));
2426 static struct file_operations tracing_readme_fops = {
2427 .open = tracing_open_generic,
2428 .read = tracing_readme_read,
2432 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2433 size_t cnt, loff_t *ppos)
2435 struct trace_array *tr = filp->private_data;
2439 r = sprintf(buf, "%ld\n", tr->ctrl);
2440 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2444 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2445 size_t cnt, loff_t *ppos)
2447 struct trace_array *tr = filp->private_data;
2452 if (cnt >= sizeof(buf))
2455 if (copy_from_user(&buf, ubuf, cnt))
2460 ret = strict_strtoul(buf, 10, &val);
2466 mutex_lock(&trace_types_lock);
2467 if (tr->ctrl ^ val) {
2475 if (current_trace && current_trace->ctrl_update)
2476 current_trace->ctrl_update(tr);
2478 mutex_unlock(&trace_types_lock);
2486 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2487 size_t cnt, loff_t *ppos)
2489 char buf[max_tracer_type_len+2];
2492 mutex_lock(&trace_types_lock);
2494 r = sprintf(buf, "%s\n", current_trace->name);
2496 r = sprintf(buf, "\n");
2497 mutex_unlock(&trace_types_lock);
2499 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2503 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2504 size_t cnt, loff_t *ppos)
2506 struct trace_array *tr = &global_trace;
2508 char buf[max_tracer_type_len+1];
2511 if (cnt > max_tracer_type_len)
2512 cnt = max_tracer_type_len;
2514 if (copy_from_user(&buf, ubuf, cnt))
2519 /* strip ending whitespace. */
2520 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2523 mutex_lock(&trace_types_lock);
2524 for (t = trace_types; t; t = t->next) {
2525 if (strcmp(t->name, buf) == 0)
2528 if (!t || t == current_trace)
2531 if (current_trace && current_trace->reset)
2532 current_trace->reset(tr);
2539 mutex_unlock(&trace_types_lock);
2547 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2548 size_t cnt, loff_t *ppos)
2550 unsigned long *ptr = filp->private_data;
2554 r = snprintf(buf, sizeof(buf), "%ld\n",
2555 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2556 if (r > sizeof(buf))
2558 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2562 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2563 size_t cnt, loff_t *ppos)
2565 long *ptr = filp->private_data;
2570 if (cnt >= sizeof(buf))
2573 if (copy_from_user(&buf, ubuf, cnt))
2578 ret = strict_strtoul(buf, 10, &val);
2587 static atomic_t tracing_reader;
2589 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2591 struct trace_iterator *iter;
2593 if (tracing_disabled)
2596 /* We only allow for reader of the pipe */
2597 if (atomic_inc_return(&tracing_reader) != 1) {
2598 atomic_dec(&tracing_reader);
2602 /* create a buffer to store the information to pass to userspace */
2603 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2607 mutex_lock(&trace_types_lock);
2608 iter->tr = &global_trace;
2609 iter->trace = current_trace;
2610 filp->private_data = iter;
2612 if (iter->trace->pipe_open)
2613 iter->trace->pipe_open(iter);
2614 mutex_unlock(&trace_types_lock);
2619 static int tracing_release_pipe(struct inode *inode, struct file *file)
2621 struct trace_iterator *iter = file->private_data;
2624 atomic_dec(&tracing_reader);
2630 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2632 struct trace_iterator *iter = filp->private_data;
2634 if (trace_flags & TRACE_ITER_BLOCK) {
2636 * Always select as readable when in blocking mode
2638 return POLLIN | POLLRDNORM;
2640 if (!trace_empty(iter))
2641 return POLLIN | POLLRDNORM;
2642 poll_wait(filp, &trace_wait, poll_table);
2643 if (!trace_empty(iter))
2644 return POLLIN | POLLRDNORM;
2654 tracing_read_pipe(struct file *filp, char __user *ubuf,
2655 size_t cnt, loff_t *ppos)
2657 struct trace_iterator *iter = filp->private_data;
2658 struct trace_array_cpu *data;
2659 static cpumask_t mask;
2660 unsigned long flags;
2661 #ifdef CONFIG_FTRACE
2667 /* return any leftover data */
2668 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2673 trace_seq_reset(&iter->seq);
2675 mutex_lock(&trace_types_lock);
2676 if (iter->trace->read) {
2677 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2682 while (trace_empty(iter)) {
2684 if ((filp->f_flags & O_NONBLOCK)) {
2690 * This is a make-shift waitqueue. The reason we don't use
2691 * an actual wait queue is because:
2692 * 1) we only ever have one waiter
2693 * 2) the tracing, traces all functions, we don't want
2694 * the overhead of calling wake_up and friends
2695 * (and tracing them too)
2696 * Anyway, this is really very primitive wakeup.
2698 set_current_state(TASK_INTERRUPTIBLE);
2699 iter->tr->waiter = current;
2701 mutex_unlock(&trace_types_lock);
2703 /* sleep for 100 msecs, and try again. */
2704 schedule_timeout(HZ/10);
2706 mutex_lock(&trace_types_lock);
2708 iter->tr->waiter = NULL;
2710 if (signal_pending(current)) {
2715 if (iter->trace != current_trace)
2719 * We block until we read something and tracing is disabled.
2720 * We still block if tracing is disabled, but we have never
2721 * read anything. This allows a user to cat this file, and
2722 * then enable tracing. But after we have read something,
2723 * we give an EOF when tracing is again disabled.
2725 * iter->pos will be 0 if we haven't read anything.
2727 if (!tracer_enabled && iter->pos)
2733 /* stop when tracing is finished */
2734 if (trace_empty(iter))
2737 if (cnt >= PAGE_SIZE)
2738 cnt = PAGE_SIZE - 1;
2740 /* reset all but tr, trace, and overruns */
2741 memset(&iter->seq, 0,
2742 sizeof(struct trace_iterator) -
2743 offsetof(struct trace_iterator, seq));
2747 * We need to stop all tracing on all CPUS to read the
2748 * the next buffer. This is a bit expensive, but is
2749 * not done often. We fill all what we can read,
2750 * and then release the locks again.
2754 local_irq_save(flags);
2755 #ifdef CONFIG_FTRACE
2756 ftrace_save = ftrace_enabled;
2760 for_each_tracing_cpu(cpu) {
2761 data = iter->tr->data[cpu];
2763 if (!head_page(data) || !data->trace_idx)
2766 atomic_inc(&data->disabled);
2770 for_each_cpu_mask(cpu, mask) {
2771 data = iter->tr->data[cpu];
2772 __raw_spin_lock(&data->lock);
2774 if (data->overrun > iter->last_overrun[cpu])
2775 iter->overrun[cpu] +=
2776 data->overrun - iter->last_overrun[cpu];
2777 iter->last_overrun[cpu] = data->overrun;
2780 while (find_next_entry_inc(iter) != NULL) {
2782 int len = iter->seq.len;
2784 ret = print_trace_line(iter);
2786 /* don't print partial lines */
2787 iter->seq.len = len;
2791 trace_consume(iter);
2793 if (iter->seq.len >= cnt)
2797 for_each_cpu_mask(cpu, mask) {
2798 data = iter->tr->data[cpu];
2799 __raw_spin_unlock(&data->lock);
2802 for_each_cpu_mask(cpu, mask) {
2803 data = iter->tr->data[cpu];
2804 atomic_dec(&data->disabled);
2806 #ifdef CONFIG_FTRACE
2807 ftrace_enabled = ftrace_save;
2809 local_irq_restore(flags);
2811 /* Now copy what we have to the user */
2812 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2813 if (iter->seq.readpos >= iter->seq.len)
2814 trace_seq_reset(&iter->seq);
2819 mutex_unlock(&trace_types_lock);
2825 tracing_entries_read(struct file *filp, char __user *ubuf,
2826 size_t cnt, loff_t *ppos)
2828 struct trace_array *tr = filp->private_data;
2832 r = sprintf(buf, "%lu\n", tr->entries);
2833 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2837 tracing_entries_write(struct file *filp, const char __user *ubuf,
2838 size_t cnt, loff_t *ppos)
2844 if (cnt >= sizeof(buf))
2847 if (copy_from_user(&buf, ubuf, cnt))
2852 ret = strict_strtoul(buf, 10, &val);
2856 /* must have at least 1 entry */
2860 mutex_lock(&trace_types_lock);
2862 if (current_trace != &no_tracer) {
2864 pr_info("ftrace: set current_tracer to none"
2865 " before modifying buffer size\n");
2869 if (val > global_trace.entries) {
2870 long pages_requested;
2871 unsigned long freeable_pages;
2873 /* make sure we have enough memory before mapping */
2875 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2877 /* account for each buffer (and max_tr) */
2878 pages_requested *= tracing_nr_buffers * 2;
2880 /* Check for overflow */
2881 if (pages_requested < 0) {
2886 freeable_pages = determine_dirtyable_memory();
2888 /* we only allow to request 1/4 of useable memory */
2889 if (pages_requested >
2890 ((freeable_pages + tracing_pages_allocated) / 4)) {
2895 while (global_trace.entries < val) {
2896 if (trace_alloc_page()) {
2900 /* double check that we don't go over the known pages */
2901 if (tracing_pages_allocated > pages_requested)
2906 /* include the number of entries in val (inc of page entries) */
2907 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2911 /* check integrity */
2912 for_each_tracing_cpu(i)
2913 check_pages(global_trace.data[i]);
2917 /* If check pages failed, return ENOMEM */
2918 if (tracing_disabled)
2921 max_tr.entries = global_trace.entries;
2922 mutex_unlock(&trace_types_lock);
2927 static struct file_operations tracing_max_lat_fops = {
2928 .open = tracing_open_generic,
2929 .read = tracing_max_lat_read,
2930 .write = tracing_max_lat_write,
2933 static struct file_operations tracing_ctrl_fops = {
2934 .open = tracing_open_generic,
2935 .read = tracing_ctrl_read,
2936 .write = tracing_ctrl_write,
2939 static struct file_operations set_tracer_fops = {
2940 .open = tracing_open_generic,
2941 .read = tracing_set_trace_read,
2942 .write = tracing_set_trace_write,
2945 static struct file_operations tracing_pipe_fops = {
2946 .open = tracing_open_pipe,
2947 .poll = tracing_poll_pipe,
2948 .read = tracing_read_pipe,
2949 .release = tracing_release_pipe,
2952 static struct file_operations tracing_entries_fops = {
2953 .open = tracing_open_generic,
2954 .read = tracing_entries_read,
2955 .write = tracing_entries_write,
2958 #ifdef CONFIG_DYNAMIC_FTRACE
2961 tracing_read_long(struct file *filp, char __user *ubuf,
2962 size_t cnt, loff_t *ppos)
2964 unsigned long *p = filp->private_data;
2968 r = sprintf(buf, "%ld\n", *p);
2970 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2973 static struct file_operations tracing_read_long_fops = {
2974 .open = tracing_open_generic,
2975 .read = tracing_read_long,
2979 static struct dentry *d_tracer;
2981 struct dentry *tracing_init_dentry(void)
2988 d_tracer = debugfs_create_dir("tracing", NULL);
2990 if (!d_tracer && !once) {
2992 pr_warning("Could not create debugfs directory 'tracing'\n");
2999 #ifdef CONFIG_FTRACE_SELFTEST
3000 /* Let selftest have access to static functions in this file */
3001 #include "trace_selftest.c"
3004 static __init void tracer_init_debugfs(void)
3006 struct dentry *d_tracer;
3007 struct dentry *entry;
3009 d_tracer = tracing_init_dentry();
3011 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3012 &global_trace, &tracing_ctrl_fops);
3014 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3016 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
3017 NULL, &tracing_iter_fops);
3019 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
3021 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3022 NULL, &tracing_cpumask_fops);
3024 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3026 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3027 &global_trace, &tracing_lt_fops);
3029 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3031 entry = debugfs_create_file("trace", 0444, d_tracer,
3032 &global_trace, &tracing_fops);
3034 pr_warning("Could not create debugfs 'trace' entry\n");
3036 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3037 &global_trace, &show_traces_fops);
3039 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3041 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3042 &global_trace, &set_tracer_fops);
3044 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3046 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3047 &tracing_max_latency,
3048 &tracing_max_lat_fops);
3050 pr_warning("Could not create debugfs "
3051 "'tracing_max_latency' entry\n");
3053 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3054 &tracing_thresh, &tracing_max_lat_fops);
3056 pr_warning("Could not create debugfs "
3057 "'tracing_thresh' entry\n");
3058 entry = debugfs_create_file("README", 0644, d_tracer,
3059 NULL, &tracing_readme_fops);
3061 pr_warning("Could not create debugfs 'README' entry\n");
3063 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3064 NULL, &tracing_pipe_fops);
3066 pr_warning("Could not create debugfs "
3067 "'trace_pipe' entry\n");
3069 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
3070 &global_trace, &tracing_entries_fops);
3072 pr_warning("Could not create debugfs "
3073 "'trace_entries' entry\n");
3075 #ifdef CONFIG_DYNAMIC_FTRACE
3076 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3077 &ftrace_update_tot_cnt,
3078 &tracing_read_long_fops);
3080 pr_warning("Could not create debugfs "
3081 "'dyn_ftrace_total_info' entry\n");
3083 #ifdef CONFIG_SYSPROF_TRACER
3084 init_tracer_sysprof_debugfs(d_tracer);
3088 #define TRACE_BUF_SIZE 1024
3089 #define TRACE_PRINT_BUF_SIZE \
3090 (sizeof(struct trace_field) - offsetof(struct trace_field, print.buf))
3091 #define TRACE_CONT_BUF_SIZE sizeof(struct trace_field)
3093 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3095 static DEFINE_SPINLOCK(trace_buf_lock);
3096 static char trace_buf[TRACE_BUF_SIZE];
3098 struct trace_array *tr = &global_trace;
3099 struct trace_array_cpu *data;
3100 struct trace_entry *entry;
3101 unsigned long flags;
3104 int cpu, len = 0, write, written = 0;
3106 if (!(trace_flags & TRACE_ITER_PRINTK) || !tr->ctrl || tracing_disabled)
3109 local_irq_save(flags);
3110 cpu = raw_smp_processor_id();
3111 data = tr->data[cpu];
3112 disabled = atomic_inc_return(&data->disabled);
3114 if (unlikely(disabled != 1))
3117 spin_lock(&trace_buf_lock);
3119 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, ap);
3122 len = min(len, TRACE_BUF_SIZE-1);
3125 __raw_spin_lock(&data->lock);
3126 entry = tracing_get_trace_entry(tr, data);
3127 tracing_generic_entry_update(entry, flags);
3128 entry->type = TRACE_PRINT;
3129 entry->field.print.ip = ip;
3131 write = min(len, (int)(TRACE_PRINT_BUF_SIZE-1));
3133 memcpy(&entry->field.print.buf, trace_buf, write);
3134 entry->field.print.buf[write] = 0;
3138 entry->field.flags |= TRACE_FLAG_CONT;
3140 while (written != len) {
3141 entry = tracing_get_trace_entry(tr, data);
3143 entry->type = TRACE_CONT;
3144 write = min(len - written, (int)(TRACE_CONT_BUF_SIZE-1));
3145 memcpy(&entry->cont.buf, trace_buf+written, write);
3146 entry->cont.buf[write] = 0;
3149 __raw_spin_unlock(&data->lock);
3151 spin_unlock(&trace_buf_lock);
3154 atomic_dec(&data->disabled);
3155 local_irq_restore(flags);
3159 EXPORT_SYMBOL_GPL(__ftrace_printk);
3161 static int trace_panic_handler(struct notifier_block *this,
3162 unsigned long event, void *unused)
3168 static struct notifier_block trace_panic_notifier = {
3169 .notifier_call = trace_panic_handler,
3171 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3174 static int trace_die_handler(struct notifier_block *self,
3188 static struct notifier_block trace_die_notifier = {
3189 .notifier_call = trace_die_handler,
3194 * printk is set to max of 1024, we really don't need it that big.
3195 * Nothing should be printing 1000 characters anyway.
3197 #define TRACE_MAX_PRINT 1000
3200 * Define here KERN_TRACE so that we have one place to modify
3201 * it if we decide to change what log level the ftrace dump
3204 #define KERN_TRACE KERN_INFO
3207 trace_printk_seq(struct trace_seq *s)
3209 /* Probably should print a warning here. */
3213 /* should be zero ended, but we are paranoid. */
3214 s->buffer[s->len] = 0;
3216 printk(KERN_TRACE "%s", s->buffer);
3222 void ftrace_dump(void)
3224 static DEFINE_SPINLOCK(ftrace_dump_lock);
3225 /* use static because iter can be a bit big for the stack */
3226 static struct trace_iterator iter;
3227 struct trace_array_cpu *data;
3228 static cpumask_t mask;
3229 static int dump_ran;
3230 unsigned long flags;
3235 spin_lock_irqsave(&ftrace_dump_lock, flags);
3241 /* No turning back! */
3242 ftrace_kill_atomic();
3244 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3246 iter.tr = &global_trace;
3247 iter.trace = current_trace;
3250 * We need to stop all tracing on all CPUS to read the
3251 * the next buffer. This is a bit expensive, but is
3252 * not done often. We fill all what we can read,
3253 * and then release the locks again.
3258 for_each_tracing_cpu(cpu) {
3259 data = iter.tr->data[cpu];
3261 if (!head_page(data) || !data->trace_idx)
3264 atomic_inc(&data->disabled);
3268 for_each_cpu_mask(cpu, mask) {
3269 data = iter.tr->data[cpu];
3270 __raw_spin_lock(&data->lock);
3272 if (data->overrun > iter.last_overrun[cpu])
3273 iter.overrun[cpu] +=
3274 data->overrun - iter.last_overrun[cpu];
3275 iter.last_overrun[cpu] = data->overrun;
3278 while (!trace_empty(&iter)) {
3281 printk(KERN_TRACE "---------------------------------\n");
3285 /* reset all but tr, trace, and overruns */
3286 memset(&iter.seq, 0,
3287 sizeof(struct trace_iterator) -
3288 offsetof(struct trace_iterator, seq));
3289 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3292 if (find_next_entry_inc(&iter) != NULL) {
3293 print_trace_line(&iter);
3294 trace_consume(&iter);
3297 trace_printk_seq(&iter.seq);
3301 printk(KERN_TRACE " (ftrace buffer empty)\n");
3303 printk(KERN_TRACE "---------------------------------\n");
3305 for_each_cpu_mask(cpu, mask) {
3306 data = iter.tr->data[cpu];
3307 __raw_spin_unlock(&data->lock);
3310 for_each_cpu_mask(cpu, mask) {
3311 data = iter.tr->data[cpu];
3312 atomic_dec(&data->disabled);
3317 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3320 static int trace_alloc_page(void)
3322 struct trace_array_cpu *data;
3323 struct page *page, *tmp;
3326 unsigned pages_allocated = 0;
3329 /* first allocate a page for each CPU */
3330 for_each_tracing_cpu(i) {
3331 array = (void *)__get_free_page(GFP_KERNEL);
3332 if (array == NULL) {
3333 printk(KERN_ERR "tracer: failed to allocate page"
3334 "for trace buffer!\n");
3339 page = virt_to_page(array);
3340 list_add(&page->lru, &pages);
3342 /* Only allocate if we are actually using the max trace */
3343 #ifdef CONFIG_TRACER_MAX_TRACE
3344 array = (void *)__get_free_page(GFP_KERNEL);
3345 if (array == NULL) {
3346 printk(KERN_ERR "tracer: failed to allocate page"
3347 "for trace buffer!\n");
3351 page = virt_to_page(array);
3352 list_add(&page->lru, &pages);
3356 /* Now that we successfully allocate a page per CPU, add them */
3357 for_each_tracing_cpu(i) {
3358 data = global_trace.data[i];
3359 page = list_entry(pages.next, struct page, lru);
3360 list_del_init(&page->lru);
3361 list_add_tail(&page->lru, &data->trace_pages);
3364 #ifdef CONFIG_TRACER_MAX_TRACE
3365 data = max_tr.data[i];
3366 page = list_entry(pages.next, struct page, lru);
3367 list_del_init(&page->lru);
3368 list_add_tail(&page->lru, &data->trace_pages);
3372 tracing_pages_allocated += pages_allocated;
3373 global_trace.entries += ENTRIES_PER_PAGE;
3378 list_for_each_entry_safe(page, tmp, &pages, lru) {
3379 list_del_init(&page->lru);
3385 static int trace_free_page(void)
3387 struct trace_array_cpu *data;
3389 struct list_head *p;
3393 /* free one page from each buffer */
3394 for_each_tracing_cpu(i) {
3395 data = global_trace.data[i];
3396 p = data->trace_pages.next;
3397 if (p == &data->trace_pages) {
3398 /* should never happen */
3400 tracing_disabled = 1;
3404 page = list_entry(p, struct page, lru);
3406 list_del(&page->lru);
3407 tracing_pages_allocated--;
3408 tracing_pages_allocated--;
3411 tracing_reset(data);
3413 #ifdef CONFIG_TRACER_MAX_TRACE
3414 data = max_tr.data[i];
3415 p = data->trace_pages.next;
3416 if (p == &data->trace_pages) {
3417 /* should never happen */
3419 tracing_disabled = 1;
3423 page = list_entry(p, struct page, lru);
3425 list_del(&page->lru);
3428 tracing_reset(data);
3431 global_trace.entries -= ENTRIES_PER_PAGE;
3436 __init static int tracer_alloc_buffers(void)
3438 struct trace_array_cpu *data;
3445 /* TODO: make the number of buffers hot pluggable with CPUS */
3446 tracing_nr_buffers = num_possible_cpus();
3447 tracing_buffer_mask = cpu_possible_map;
3449 /* Allocate the first page for all buffers */
3450 for_each_tracing_cpu(i) {
3451 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3452 max_tr.data[i] = &per_cpu(max_data, i);
3454 array = (void *)__get_free_page(GFP_KERNEL);
3455 if (array == NULL) {
3456 printk(KERN_ERR "tracer: failed to allocate page"
3457 "for trace buffer!\n");
3461 /* set the array to the list */
3462 INIT_LIST_HEAD(&data->trace_pages);
3463 page = virt_to_page(array);
3464 list_add(&page->lru, &data->trace_pages);
3465 /* use the LRU flag to differentiate the two buffers */
3468 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3469 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3471 /* Only allocate if we are actually using the max trace */
3472 #ifdef CONFIG_TRACER_MAX_TRACE
3473 array = (void *)__get_free_page(GFP_KERNEL);
3474 if (array == NULL) {
3475 printk(KERN_ERR "tracer: failed to allocate page"
3476 "for trace buffer!\n");
3480 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3481 page = virt_to_page(array);
3482 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3488 * Since we allocate by orders of pages, we may be able to
3491 global_trace.entries = ENTRIES_PER_PAGE;
3494 while (global_trace.entries < trace_nr_entries) {
3495 if (trace_alloc_page())
3499 max_tr.entries = global_trace.entries;
3501 pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
3502 pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
3503 pr_info(" actual entries %ld\n", global_trace.entries);
3505 tracer_init_debugfs();
3507 trace_init_cmdlines();
3509 register_tracer(&no_tracer);
3510 current_trace = &no_tracer;
3512 /* All seems OK, enable tracing */
3513 global_trace.ctrl = tracer_enabled;
3514 tracing_disabled = 0;
3516 atomic_notifier_chain_register(&panic_notifier_list,
3517 &trace_panic_notifier);
3519 register_die_notifier(&trace_die_notifier);
3524 for (i-- ; i >= 0; i--) {
3525 struct page *page, *tmp;
3526 struct trace_array_cpu *data = global_trace.data[i];
3529 list_for_each_entry_safe(page, tmp,
3530 &data->trace_pages, lru) {
3531 list_del_init(&page->lru);
3536 #ifdef CONFIG_TRACER_MAX_TRACE
3537 data = max_tr.data[i];
3539 list_for_each_entry_safe(page, tmp,
3540 &data->trace_pages, lru) {
3541 list_del_init(&page->lru);
3549 fs_initcall(tracer_alloc_buffers);