Merge branch 'x86/x2apic' into x86/core
[linux-2.6/x86.git] / kernel / trace / trace.c
blob8f3fb3db61c39306bdbffd36303cfed7dab02420
1 /*
2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/debugfs.h>
18 #include <linux/pagemap.h>
19 #include <linux/hardirq.h>
20 #include <linux/linkage.h>
21 #include <linux/uaccess.h>
22 #include <linux/ftrace.h>
23 #include <linux/module.h>
24 #include <linux/percpu.h>
25 #include <linux/ctype.h>
26 #include <linux/init.h>
27 #include <linux/poll.h>
28 #include <linux/gfp.h>
29 #include <linux/fs.h>
30 #include <linux/kprobes.h>
31 #include <linux/writeback.h>
33 #include <linux/stacktrace.h>
35 #include "trace.h"
37 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
38 unsigned long __read_mostly tracing_thresh;
40 static unsigned long __read_mostly tracing_nr_buffers;
41 static cpumask_t __read_mostly tracing_buffer_mask;
43 #define for_each_tracing_cpu(cpu) \
44 for_each_cpu_mask(cpu, tracing_buffer_mask)
46 static int trace_alloc_page(void);
47 static int trace_free_page(void);
49 static int tracing_disabled = 1;
51 static unsigned long tracing_pages_allocated;
53 long
54 ns2usecs(cycle_t nsec)
56 nsec += 500;
57 do_div(nsec, 1000);
58 return nsec;
61 cycle_t ftrace_now(int cpu)
63 return cpu_clock(cpu);
67 * The global_trace is the descriptor that holds the tracing
68 * buffers for the live tracing. For each CPU, it contains
69 * a link list of pages that will store trace entries. The
70 * page descriptor of the pages in the memory is used to hold
71 * the link list by linking the lru item in the page descriptor
72 * to each of the pages in the buffer per CPU.
74 * For each active CPU there is a data field that holds the
75 * pages for the buffer for that CPU. Each CPU has the same number
76 * of pages allocated for its buffer.
78 static struct trace_array global_trace;
80 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
83 * The max_tr is used to snapshot the global_trace when a maximum
84 * latency is reached. Some tracers will use this to store a maximum
85 * trace while it continues examining live traces.
87 * The buffers for the max_tr are set up the same as the global_trace.
88 * When a snapshot is taken, the link list of the max_tr is swapped
89 * with the link list of the global_trace and the buffers are reset for
90 * the global_trace so the tracing can continue.
92 static struct trace_array max_tr;
94 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
96 /* tracer_enabled is used to toggle activation of a tracer */
97 static int tracer_enabled = 1;
99 /* function tracing enabled */
100 int ftrace_function_enabled;
103 * trace_nr_entries is the number of entries that is allocated
104 * for a buffer. Note, the number of entries is always rounded
105 * to ENTRIES_PER_PAGE.
107 static unsigned long trace_nr_entries = 65536UL;
109 /* trace_types holds a link list of available tracers. */
110 static struct tracer *trace_types __read_mostly;
112 /* current_trace points to the tracer that is currently active */
113 static struct tracer *current_trace __read_mostly;
116 * max_tracer_type_len is used to simplify the allocating of
117 * buffers to read userspace tracer names. We keep track of
118 * the longest tracer name registered.
120 static int max_tracer_type_len;
123 * trace_types_lock is used to protect the trace_types list.
124 * This lock is also used to keep user access serialized.
125 * Accesses from userspace will grab this lock while userspace
126 * activities happen inside the kernel.
128 static DEFINE_MUTEX(trace_types_lock);
130 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
131 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
133 /* trace_flags holds iter_ctrl options */
134 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
136 static notrace void no_trace_init(struct trace_array *tr)
138 int cpu;
140 ftrace_function_enabled = 0;
141 if(tr->ctrl)
142 for_each_online_cpu(cpu)
143 tracing_reset(tr->data[cpu]);
144 tracer_enabled = 0;
147 /* dummy trace to disable tracing */
148 static struct tracer no_tracer __read_mostly = {
149 .name = "none",
150 .init = no_trace_init
155 * trace_wake_up - wake up tasks waiting for trace input
157 * Simply wakes up any task that is blocked on the trace_wait
158 * queue. These is used with trace_poll for tasks polling the trace.
160 void trace_wake_up(void)
163 * The runqueue_is_locked() can fail, but this is the best we
164 * have for now:
166 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
167 wake_up(&trace_wait);
170 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
172 static int __init set_nr_entries(char *str)
174 unsigned long nr_entries;
175 int ret;
177 if (!str)
178 return 0;
179 ret = strict_strtoul(str, 0, &nr_entries);
180 /* nr_entries can not be zero */
181 if (ret < 0 || nr_entries == 0)
182 return 0;
183 trace_nr_entries = nr_entries;
184 return 1;
186 __setup("trace_entries=", set_nr_entries);
188 unsigned long nsecs_to_usecs(unsigned long nsecs)
190 return nsecs / 1000;
194 * trace_flag_type is an enumeration that holds different
195 * states when a trace occurs. These are:
196 * IRQS_OFF - interrupts were disabled
197 * NEED_RESCED - reschedule is requested
198 * HARDIRQ - inside an interrupt handler
199 * SOFTIRQ - inside a softirq handler
201 enum trace_flag_type {
202 TRACE_FLAG_IRQS_OFF = 0x01,
203 TRACE_FLAG_NEED_RESCHED = 0x02,
204 TRACE_FLAG_HARDIRQ = 0x04,
205 TRACE_FLAG_SOFTIRQ = 0x08,
209 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
210 * control the output of kernel symbols.
212 #define TRACE_ITER_SYM_MASK \
213 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
215 /* These must match the bit postions in trace_iterator_flags */
216 static const char *trace_options[] = {
217 "print-parent",
218 "sym-offset",
219 "sym-addr",
220 "verbose",
221 "raw",
222 "hex",
223 "bin",
224 "block",
225 "stacktrace",
226 "sched-tree",
227 NULL
231 * ftrace_max_lock is used to protect the swapping of buffers
232 * when taking a max snapshot. The buffers themselves are
233 * protected by per_cpu spinlocks. But the action of the swap
234 * needs its own lock.
236 * This is defined as a raw_spinlock_t in order to help
237 * with performance when lockdep debugging is enabled.
239 static raw_spinlock_t ftrace_max_lock =
240 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
243 * Copy the new maximum trace into the separate maximum-trace
244 * structure. (this way the maximum trace is permanently saved,
245 * for later retrieval via /debugfs/tracing/latency_trace)
247 static void
248 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
250 struct trace_array_cpu *data = tr->data[cpu];
252 max_tr.cpu = cpu;
253 max_tr.time_start = data->preempt_timestamp;
255 data = max_tr.data[cpu];
256 data->saved_latency = tracing_max_latency;
258 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
259 data->pid = tsk->pid;
260 data->uid = tsk->uid;
261 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
262 data->policy = tsk->policy;
263 data->rt_priority = tsk->rt_priority;
265 /* record this tasks comm */
266 tracing_record_cmdline(current);
269 #define CHECK_COND(cond) \
270 if (unlikely(cond)) { \
271 tracing_disabled = 1; \
272 WARN_ON(1); \
273 return -1; \
277 * check_pages - integrity check of trace buffers
279 * As a safty measure we check to make sure the data pages have not
280 * been corrupted.
282 int check_pages(struct trace_array_cpu *data)
284 struct page *page, *tmp;
286 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
287 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
289 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
290 CHECK_COND(page->lru.next->prev != &page->lru);
291 CHECK_COND(page->lru.prev->next != &page->lru);
294 return 0;
298 * head_page - page address of the first page in per_cpu buffer.
300 * head_page returns the page address of the first page in
301 * a per_cpu buffer. This also preforms various consistency
302 * checks to make sure the buffer has not been corrupted.
304 void *head_page(struct trace_array_cpu *data)
306 struct page *page;
308 if (list_empty(&data->trace_pages))
309 return NULL;
311 page = list_entry(data->trace_pages.next, struct page, lru);
312 BUG_ON(&page->lru == &data->trace_pages);
314 return page_address(page);
318 * trace_seq_printf - sequence printing of trace information
319 * @s: trace sequence descriptor
320 * @fmt: printf format string
322 * The tracer may use either sequence operations or its own
323 * copy to user routines. To simplify formating of a trace
324 * trace_seq_printf is used to store strings into a special
325 * buffer (@s). Then the output may be either used by
326 * the sequencer or pulled into another buffer.
329 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
331 int len = (PAGE_SIZE - 1) - s->len;
332 va_list ap;
333 int ret;
335 if (!len)
336 return 0;
338 va_start(ap, fmt);
339 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
340 va_end(ap);
342 /* If we can't write it all, don't bother writing anything */
343 if (ret >= len)
344 return 0;
346 s->len += ret;
348 return len;
352 * trace_seq_puts - trace sequence printing of simple string
353 * @s: trace sequence descriptor
354 * @str: simple string to record
356 * The tracer may use either the sequence operations or its own
357 * copy to user routines. This function records a simple string
358 * into a special buffer (@s) for later retrieval by a sequencer
359 * or other mechanism.
361 static int
362 trace_seq_puts(struct trace_seq *s, const char *str)
364 int len = strlen(str);
366 if (len > ((PAGE_SIZE - 1) - s->len))
367 return 0;
369 memcpy(s->buffer + s->len, str, len);
370 s->len += len;
372 return len;
375 static int
376 trace_seq_putc(struct trace_seq *s, unsigned char c)
378 if (s->len >= (PAGE_SIZE - 1))
379 return 0;
381 s->buffer[s->len++] = c;
383 return 1;
386 static int
387 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
389 if (len > ((PAGE_SIZE - 1) - s->len))
390 return 0;
392 memcpy(s->buffer + s->len, mem, len);
393 s->len += len;
395 return len;
398 #define HEX_CHARS 17
399 static const char hex2asc[] = "0123456789abcdef";
401 static int
402 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
404 unsigned char hex[HEX_CHARS];
405 unsigned char *data = mem;
406 unsigned char byte;
407 int i, j;
409 BUG_ON(len >= HEX_CHARS);
411 #ifdef __BIG_ENDIAN
412 for (i = 0, j = 0; i < len; i++) {
413 #else
414 for (i = len-1, j = 0; i >= 0; i--) {
415 #endif
416 byte = data[i];
418 hex[j++] = hex2asc[byte & 0x0f];
419 hex[j++] = hex2asc[byte >> 4];
421 hex[j++] = ' ';
423 return trace_seq_putmem(s, hex, j);
426 static void
427 trace_seq_reset(struct trace_seq *s)
429 s->len = 0;
430 s->readpos = 0;
433 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
435 int len;
436 int ret;
438 if (s->len <= s->readpos)
439 return -EBUSY;
441 len = s->len - s->readpos;
442 if (cnt > len)
443 cnt = len;
444 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
445 if (ret)
446 return -EFAULT;
448 s->readpos += len;
449 return cnt;
452 static void
453 trace_print_seq(struct seq_file *m, struct trace_seq *s)
455 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
457 s->buffer[len] = 0;
458 seq_puts(m, s->buffer);
460 trace_seq_reset(s);
464 * flip the trace buffers between two trace descriptors.
465 * This usually is the buffers between the global_trace and
466 * the max_tr to record a snapshot of a current trace.
468 * The ftrace_max_lock must be held.
470 static void
471 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
473 struct list_head flip_pages;
475 INIT_LIST_HEAD(&flip_pages);
477 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
478 sizeof(struct trace_array_cpu) -
479 offsetof(struct trace_array_cpu, trace_head_idx));
481 check_pages(tr1);
482 check_pages(tr2);
483 list_splice_init(&tr1->trace_pages, &flip_pages);
484 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
485 list_splice_init(&flip_pages, &tr2->trace_pages);
486 BUG_ON(!list_empty(&flip_pages));
487 check_pages(tr1);
488 check_pages(tr2);
492 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
493 * @tr: tracer
494 * @tsk: the task with the latency
495 * @cpu: The cpu that initiated the trace.
497 * Flip the buffers between the @tr and the max_tr and record information
498 * about which task was the cause of this latency.
500 void
501 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
503 struct trace_array_cpu *data;
504 int i;
506 WARN_ON_ONCE(!irqs_disabled());
507 __raw_spin_lock(&ftrace_max_lock);
508 /* clear out all the previous traces */
509 for_each_tracing_cpu(i) {
510 data = tr->data[i];
511 flip_trace(max_tr.data[i], data);
512 tracing_reset(data);
515 __update_max_tr(tr, tsk, cpu);
516 __raw_spin_unlock(&ftrace_max_lock);
520 * update_max_tr_single - only copy one trace over, and reset the rest
521 * @tr - tracer
522 * @tsk - task with the latency
523 * @cpu - the cpu of the buffer to copy.
525 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
527 void
528 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
530 struct trace_array_cpu *data = tr->data[cpu];
531 int i;
533 WARN_ON_ONCE(!irqs_disabled());
534 __raw_spin_lock(&ftrace_max_lock);
535 for_each_tracing_cpu(i)
536 tracing_reset(max_tr.data[i]);
538 flip_trace(max_tr.data[cpu], data);
539 tracing_reset(data);
541 __update_max_tr(tr, tsk, cpu);
542 __raw_spin_unlock(&ftrace_max_lock);
546 * register_tracer - register a tracer with the ftrace system.
547 * @type - the plugin for the tracer
549 * Register a new plugin tracer.
551 int register_tracer(struct tracer *type)
553 struct tracer *t;
554 int len;
555 int ret = 0;
557 if (!type->name) {
558 pr_info("Tracer must have a name\n");
559 return -1;
562 mutex_lock(&trace_types_lock);
563 for (t = trace_types; t; t = t->next) {
564 if (strcmp(type->name, t->name) == 0) {
565 /* already found */
566 pr_info("Trace %s already registered\n",
567 type->name);
568 ret = -1;
569 goto out;
573 #ifdef CONFIG_FTRACE_STARTUP_TEST
574 if (type->selftest) {
575 struct tracer *saved_tracer = current_trace;
576 struct trace_array_cpu *data;
577 struct trace_array *tr = &global_trace;
578 int saved_ctrl = tr->ctrl;
579 int i;
581 * Run a selftest on this tracer.
582 * Here we reset the trace buffer, and set the current
583 * tracer to be this tracer. The tracer can then run some
584 * internal tracing to verify that everything is in order.
585 * If we fail, we do not register this tracer.
587 for_each_tracing_cpu(i) {
588 data = tr->data[i];
589 if (!head_page(data))
590 continue;
591 tracing_reset(data);
593 current_trace = type;
594 tr->ctrl = 0;
595 /* the test is responsible for initializing and enabling */
596 pr_info("Testing tracer %s: ", type->name);
597 ret = type->selftest(type, tr);
598 /* the test is responsible for resetting too */
599 current_trace = saved_tracer;
600 tr->ctrl = saved_ctrl;
601 if (ret) {
602 printk(KERN_CONT "FAILED!\n");
603 goto out;
605 /* Only reset on passing, to avoid touching corrupted buffers */
606 for_each_tracing_cpu(i) {
607 data = tr->data[i];
608 if (!head_page(data))
609 continue;
610 tracing_reset(data);
612 printk(KERN_CONT "PASSED\n");
614 #endif
616 type->next = trace_types;
617 trace_types = type;
618 len = strlen(type->name);
619 if (len > max_tracer_type_len)
620 max_tracer_type_len = len;
622 out:
623 mutex_unlock(&trace_types_lock);
625 return ret;
628 void unregister_tracer(struct tracer *type)
630 struct tracer **t;
631 int len;
633 mutex_lock(&trace_types_lock);
634 for (t = &trace_types; *t; t = &(*t)->next) {
635 if (*t == type)
636 goto found;
638 pr_info("Trace %s not registered\n", type->name);
639 goto out;
641 found:
642 *t = (*t)->next;
643 if (strlen(type->name) != max_tracer_type_len)
644 goto out;
646 max_tracer_type_len = 0;
647 for (t = &trace_types; *t; t = &(*t)->next) {
648 len = strlen((*t)->name);
649 if (len > max_tracer_type_len)
650 max_tracer_type_len = len;
652 out:
653 mutex_unlock(&trace_types_lock);
656 void tracing_reset(struct trace_array_cpu *data)
658 data->trace_idx = 0;
659 data->overrun = 0;
660 data->trace_head = data->trace_tail = head_page(data);
661 data->trace_head_idx = 0;
662 data->trace_tail_idx = 0;
665 #define SAVED_CMDLINES 128
666 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
667 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
668 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
669 static int cmdline_idx;
670 static DEFINE_SPINLOCK(trace_cmdline_lock);
672 /* temporary disable recording */
673 atomic_t trace_record_cmdline_disabled __read_mostly;
675 static void trace_init_cmdlines(void)
677 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
678 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
679 cmdline_idx = 0;
682 void trace_stop_cmdline_recording(void);
684 static void trace_save_cmdline(struct task_struct *tsk)
686 unsigned map;
687 unsigned idx;
689 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
690 return;
693 * It's not the end of the world if we don't get
694 * the lock, but we also don't want to spin
695 * nor do we want to disable interrupts,
696 * so if we miss here, then better luck next time.
698 if (!spin_trylock(&trace_cmdline_lock))
699 return;
701 idx = map_pid_to_cmdline[tsk->pid];
702 if (idx >= SAVED_CMDLINES) {
703 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
705 map = map_cmdline_to_pid[idx];
706 if (map <= PID_MAX_DEFAULT)
707 map_pid_to_cmdline[map] = (unsigned)-1;
709 map_pid_to_cmdline[tsk->pid] = idx;
711 cmdline_idx = idx;
714 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
716 spin_unlock(&trace_cmdline_lock);
719 static char *trace_find_cmdline(int pid)
721 char *cmdline = "<...>";
722 unsigned map;
724 if (!pid)
725 return "<idle>";
727 if (pid > PID_MAX_DEFAULT)
728 goto out;
730 map = map_pid_to_cmdline[pid];
731 if (map >= SAVED_CMDLINES)
732 goto out;
734 cmdline = saved_cmdlines[map];
736 out:
737 return cmdline;
740 void tracing_record_cmdline(struct task_struct *tsk)
742 if (atomic_read(&trace_record_cmdline_disabled))
743 return;
745 trace_save_cmdline(tsk);
748 static inline struct list_head *
749 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
752 * Roundrobin - but skip the head (which is not a real page):
754 next = next->next;
755 if (unlikely(next == &data->trace_pages))
756 next = next->next;
757 BUG_ON(next == &data->trace_pages);
759 return next;
762 static inline void *
763 trace_next_page(struct trace_array_cpu *data, void *addr)
765 struct list_head *next;
766 struct page *page;
768 page = virt_to_page(addr);
770 next = trace_next_list(data, &page->lru);
771 page = list_entry(next, struct page, lru);
773 return page_address(page);
776 static inline struct trace_entry *
777 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
779 unsigned long idx, idx_next;
780 struct trace_entry *entry;
782 data->trace_idx++;
783 idx = data->trace_head_idx;
784 idx_next = idx + 1;
786 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
788 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
790 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
791 data->trace_head = trace_next_page(data, data->trace_head);
792 idx_next = 0;
795 if (data->trace_head == data->trace_tail &&
796 idx_next == data->trace_tail_idx) {
797 /* overrun */
798 data->overrun++;
799 data->trace_tail_idx++;
800 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
801 data->trace_tail =
802 trace_next_page(data, data->trace_tail);
803 data->trace_tail_idx = 0;
807 data->trace_head_idx = idx_next;
809 return entry;
812 static inline void
813 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
815 struct task_struct *tsk = current;
816 unsigned long pc;
818 pc = preempt_count();
820 entry->preempt_count = pc & 0xff;
821 entry->pid = (tsk) ? tsk->pid : 0;
822 entry->t = ftrace_now(raw_smp_processor_id());
823 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
824 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
825 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
826 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
829 void
830 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
831 unsigned long ip, unsigned long parent_ip, unsigned long flags)
833 struct trace_entry *entry;
834 unsigned long irq_flags;
836 raw_local_irq_save(irq_flags);
837 __raw_spin_lock(&data->lock);
838 entry = tracing_get_trace_entry(tr, data);
839 tracing_generic_entry_update(entry, flags);
840 entry->type = TRACE_FN;
841 entry->fn.ip = ip;
842 entry->fn.parent_ip = parent_ip;
843 __raw_spin_unlock(&data->lock);
844 raw_local_irq_restore(irq_flags);
847 void
848 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
849 unsigned long ip, unsigned long parent_ip, unsigned long flags)
851 if (likely(!atomic_read(&data->disabled)))
852 trace_function(tr, data, ip, parent_ip, flags);
855 #ifdef CONFIG_MMIOTRACE
856 void __trace_mmiotrace_rw(struct trace_array *tr, struct trace_array_cpu *data,
857 struct mmiotrace_rw *rw)
859 struct trace_entry *entry;
860 unsigned long irq_flags;
862 raw_local_irq_save(irq_flags);
863 __raw_spin_lock(&data->lock);
865 entry = tracing_get_trace_entry(tr, data);
866 tracing_generic_entry_update(entry, 0);
867 entry->type = TRACE_MMIO_RW;
868 entry->mmiorw = *rw;
870 __raw_spin_unlock(&data->lock);
871 raw_local_irq_restore(irq_flags);
873 trace_wake_up();
876 void __trace_mmiotrace_map(struct trace_array *tr, struct trace_array_cpu *data,
877 struct mmiotrace_map *map)
879 struct trace_entry *entry;
880 unsigned long irq_flags;
882 raw_local_irq_save(irq_flags);
883 __raw_spin_lock(&data->lock);
885 entry = tracing_get_trace_entry(tr, data);
886 tracing_generic_entry_update(entry, 0);
887 entry->type = TRACE_MMIO_MAP;
888 entry->mmiomap = *map;
890 __raw_spin_unlock(&data->lock);
891 raw_local_irq_restore(irq_flags);
893 trace_wake_up();
895 #endif
897 void __trace_stack(struct trace_array *tr,
898 struct trace_array_cpu *data,
899 unsigned long flags,
900 int skip)
902 struct trace_entry *entry;
903 struct stack_trace trace;
905 if (!(trace_flags & TRACE_ITER_STACKTRACE))
906 return;
908 entry = tracing_get_trace_entry(tr, data);
909 tracing_generic_entry_update(entry, flags);
910 entry->type = TRACE_STACK;
912 memset(&entry->stack, 0, sizeof(entry->stack));
914 trace.nr_entries = 0;
915 trace.max_entries = FTRACE_STACK_ENTRIES;
916 trace.skip = skip;
917 trace.entries = entry->stack.caller;
919 save_stack_trace(&trace);
922 void
923 __trace_special(void *__tr, void *__data,
924 unsigned long arg1, unsigned long arg2, unsigned long arg3)
926 struct trace_array_cpu *data = __data;
927 struct trace_array *tr = __tr;
928 struct trace_entry *entry;
929 unsigned long irq_flags;
931 raw_local_irq_save(irq_flags);
932 __raw_spin_lock(&data->lock);
933 entry = tracing_get_trace_entry(tr, data);
934 tracing_generic_entry_update(entry, 0);
935 entry->type = TRACE_SPECIAL;
936 entry->special.arg1 = arg1;
937 entry->special.arg2 = arg2;
938 entry->special.arg3 = arg3;
939 __trace_stack(tr, data, irq_flags, 4);
940 __raw_spin_unlock(&data->lock);
941 raw_local_irq_restore(irq_flags);
943 trace_wake_up();
946 void
947 tracing_sched_switch_trace(struct trace_array *tr,
948 struct trace_array_cpu *data,
949 struct task_struct *prev,
950 struct task_struct *next,
951 unsigned long flags)
953 struct trace_entry *entry;
954 unsigned long irq_flags;
956 raw_local_irq_save(irq_flags);
957 __raw_spin_lock(&data->lock);
958 entry = tracing_get_trace_entry(tr, data);
959 tracing_generic_entry_update(entry, flags);
960 entry->type = TRACE_CTX;
961 entry->ctx.prev_pid = prev->pid;
962 entry->ctx.prev_prio = prev->prio;
963 entry->ctx.prev_state = prev->state;
964 entry->ctx.next_pid = next->pid;
965 entry->ctx.next_prio = next->prio;
966 entry->ctx.next_state = next->state;
967 __trace_stack(tr, data, flags, 5);
968 __raw_spin_unlock(&data->lock);
969 raw_local_irq_restore(irq_flags);
972 void
973 tracing_sched_wakeup_trace(struct trace_array *tr,
974 struct trace_array_cpu *data,
975 struct task_struct *wakee,
976 struct task_struct *curr,
977 unsigned long flags)
979 struct trace_entry *entry;
980 unsigned long irq_flags;
982 raw_local_irq_save(irq_flags);
983 __raw_spin_lock(&data->lock);
984 entry = tracing_get_trace_entry(tr, data);
985 tracing_generic_entry_update(entry, flags);
986 entry->type = TRACE_WAKE;
987 entry->ctx.prev_pid = curr->pid;
988 entry->ctx.prev_prio = curr->prio;
989 entry->ctx.prev_state = curr->state;
990 entry->ctx.next_pid = wakee->pid;
991 entry->ctx.next_prio = wakee->prio;
992 entry->ctx.next_state = wakee->state;
993 __trace_stack(tr, data, flags, 6);
994 __raw_spin_unlock(&data->lock);
995 raw_local_irq_restore(irq_flags);
997 trace_wake_up();
1000 void
1001 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1003 struct trace_array *tr = &global_trace;
1004 struct trace_array_cpu *data;
1005 unsigned long flags;
1006 long disabled;
1007 int cpu;
1009 if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
1010 return;
1012 local_irq_save(flags);
1013 cpu = raw_smp_processor_id();
1014 data = tr->data[cpu];
1015 disabled = atomic_inc_return(&data->disabled);
1017 if (likely(disabled == 1))
1018 __trace_special(tr, data, arg1, arg2, arg3);
1020 atomic_dec(&data->disabled);
1021 local_irq_restore(flags);
1024 #ifdef CONFIG_FTRACE
1025 static void
1026 function_trace_call(unsigned long ip, unsigned long parent_ip)
1028 struct trace_array *tr = &global_trace;
1029 struct trace_array_cpu *data;
1030 unsigned long flags;
1031 long disabled;
1032 int cpu;
1034 if (unlikely(!ftrace_function_enabled))
1035 return;
1037 if (skip_trace(ip))
1038 return;
1040 local_irq_save(flags);
1041 cpu = raw_smp_processor_id();
1042 data = tr->data[cpu];
1043 disabled = atomic_inc_return(&data->disabled);
1045 if (likely(disabled == 1))
1046 trace_function(tr, data, ip, parent_ip, flags);
1048 atomic_dec(&data->disabled);
1049 local_irq_restore(flags);
1052 static struct ftrace_ops trace_ops __read_mostly =
1054 .func = function_trace_call,
1057 void tracing_start_function_trace(void)
1059 ftrace_function_enabled = 0;
1060 register_ftrace_function(&trace_ops);
1061 if (tracer_enabled)
1062 ftrace_function_enabled = 1;
1065 void tracing_stop_function_trace(void)
1067 ftrace_function_enabled = 0;
1068 unregister_ftrace_function(&trace_ops);
1070 #endif
1072 enum trace_file_type {
1073 TRACE_FILE_LAT_FMT = 1,
1076 static struct trace_entry *
1077 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1078 struct trace_iterator *iter, int cpu)
1080 struct page *page;
1081 struct trace_entry *array;
1083 if (iter->next_idx[cpu] >= tr->entries ||
1084 iter->next_idx[cpu] >= data->trace_idx ||
1085 (data->trace_head == data->trace_tail &&
1086 data->trace_head_idx == data->trace_tail_idx))
1087 return NULL;
1089 if (!iter->next_page[cpu]) {
1090 /* Initialize the iterator for this cpu trace buffer */
1091 WARN_ON(!data->trace_tail);
1092 page = virt_to_page(data->trace_tail);
1093 iter->next_page[cpu] = &page->lru;
1094 iter->next_page_idx[cpu] = data->trace_tail_idx;
1097 page = list_entry(iter->next_page[cpu], struct page, lru);
1098 BUG_ON(&data->trace_pages == &page->lru);
1100 array = page_address(page);
1102 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
1103 return &array[iter->next_page_idx[cpu]];
1106 static struct trace_entry *
1107 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1109 struct trace_array *tr = iter->tr;
1110 struct trace_entry *ent, *next = NULL;
1111 int next_cpu = -1;
1112 int cpu;
1114 for_each_tracing_cpu(cpu) {
1115 if (!head_page(tr->data[cpu]))
1116 continue;
1117 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1119 * Pick the entry with the smallest timestamp:
1121 if (ent && (!next || ent->t < next->t)) {
1122 next = ent;
1123 next_cpu = cpu;
1127 if (ent_cpu)
1128 *ent_cpu = next_cpu;
1130 return next;
1133 static void trace_iterator_increment(struct trace_iterator *iter)
1135 iter->idx++;
1136 iter->next_idx[iter->cpu]++;
1137 iter->next_page_idx[iter->cpu]++;
1139 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1140 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1142 iter->next_page_idx[iter->cpu] = 0;
1143 iter->next_page[iter->cpu] =
1144 trace_next_list(data, iter->next_page[iter->cpu]);
1148 static void trace_consume(struct trace_iterator *iter)
1150 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1152 data->trace_tail_idx++;
1153 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1154 data->trace_tail = trace_next_page(data, data->trace_tail);
1155 data->trace_tail_idx = 0;
1158 /* Check if we empty it, then reset the index */
1159 if (data->trace_head == data->trace_tail &&
1160 data->trace_head_idx == data->trace_tail_idx)
1161 data->trace_idx = 0;
1164 static void *find_next_entry_inc(struct trace_iterator *iter)
1166 struct trace_entry *next;
1167 int next_cpu = -1;
1169 next = find_next_entry(iter, &next_cpu);
1171 iter->prev_ent = iter->ent;
1172 iter->prev_cpu = iter->cpu;
1174 iter->ent = next;
1175 iter->cpu = next_cpu;
1177 if (next)
1178 trace_iterator_increment(iter);
1180 return next ? iter : NULL;
1183 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1185 struct trace_iterator *iter = m->private;
1186 int i = (int)*pos;
1187 void *ent;
1189 (*pos)++;
1191 /* can't go backwards */
1192 if (iter->idx > i)
1193 return NULL;
1195 if (iter->idx < 0)
1196 ent = find_next_entry_inc(iter);
1197 else
1198 ent = iter;
1200 while (ent && iter->idx < i)
1201 ent = find_next_entry_inc(iter);
1203 iter->pos = *pos;
1205 return ent;
1208 static void *s_start(struct seq_file *m, loff_t *pos)
1210 struct trace_iterator *iter = m->private;
1211 void *p = NULL;
1212 loff_t l = 0;
1213 int i;
1215 mutex_lock(&trace_types_lock);
1217 if (!current_trace || current_trace != iter->trace) {
1218 mutex_unlock(&trace_types_lock);
1219 return NULL;
1222 atomic_inc(&trace_record_cmdline_disabled);
1224 /* let the tracer grab locks here if needed */
1225 if (current_trace->start)
1226 current_trace->start(iter);
1228 if (*pos != iter->pos) {
1229 iter->ent = NULL;
1230 iter->cpu = 0;
1231 iter->idx = -1;
1232 iter->prev_ent = NULL;
1233 iter->prev_cpu = -1;
1235 for_each_tracing_cpu(i) {
1236 iter->next_idx[i] = 0;
1237 iter->next_page[i] = NULL;
1240 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1243 } else {
1244 l = *pos - 1;
1245 p = s_next(m, p, &l);
1248 return p;
1251 static void s_stop(struct seq_file *m, void *p)
1253 struct trace_iterator *iter = m->private;
1255 atomic_dec(&trace_record_cmdline_disabled);
1257 /* let the tracer release locks here if needed */
1258 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1259 iter->trace->stop(iter);
1261 mutex_unlock(&trace_types_lock);
1264 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1266 #ifdef CONFIG_KRETPROBES
1267 static inline int kretprobed(unsigned long addr)
1269 return addr == (unsigned long)kretprobe_trampoline;
1271 #else
1272 static inline int kretprobed(unsigned long addr)
1274 return 0;
1276 #endif /* CONFIG_KRETPROBES */
1278 static int
1279 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1281 #ifdef CONFIG_KALLSYMS
1282 char str[KSYM_SYMBOL_LEN];
1284 kallsyms_lookup(address, NULL, NULL, NULL, str);
1286 return trace_seq_printf(s, fmt, str);
1287 #endif
1288 return 1;
1291 static int
1292 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1293 unsigned long address)
1295 #ifdef CONFIG_KALLSYMS
1296 char str[KSYM_SYMBOL_LEN];
1298 sprint_symbol(str, address);
1299 return trace_seq_printf(s, fmt, str);
1300 #endif
1301 return 1;
1304 #ifndef CONFIG_64BIT
1305 # define IP_FMT "%08lx"
1306 #else
1307 # define IP_FMT "%016lx"
1308 #endif
1310 static int
1311 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1313 int ret;
1315 if (!ip)
1316 return trace_seq_printf(s, "0");
1318 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1319 ret = seq_print_sym_offset(s, "%s", ip);
1320 else
1321 ret = seq_print_sym_short(s, "%s", ip);
1323 if (!ret)
1324 return 0;
1326 if (sym_flags & TRACE_ITER_SYM_ADDR)
1327 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1328 return ret;
1331 static void print_lat_help_header(struct seq_file *m)
1333 seq_puts(m, "# _------=> CPU# \n");
1334 seq_puts(m, "# / _-----=> irqs-off \n");
1335 seq_puts(m, "# | / _----=> need-resched \n");
1336 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1337 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1338 seq_puts(m, "# |||| / \n");
1339 seq_puts(m, "# ||||| delay \n");
1340 seq_puts(m, "# cmd pid ||||| time | caller \n");
1341 seq_puts(m, "# \\ / ||||| \\ | / \n");
1344 static void print_func_help_header(struct seq_file *m)
1346 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1347 seq_puts(m, "# | | | | |\n");
1351 static void
1352 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1354 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1355 struct trace_array *tr = iter->tr;
1356 struct trace_array_cpu *data = tr->data[tr->cpu];
1357 struct tracer *type = current_trace;
1358 unsigned long total = 0;
1359 unsigned long entries = 0;
1360 int cpu;
1361 const char *name = "preemption";
1363 if (type)
1364 name = type->name;
1366 for_each_tracing_cpu(cpu) {
1367 if (head_page(tr->data[cpu])) {
1368 total += tr->data[cpu]->trace_idx;
1369 if (tr->data[cpu]->trace_idx > tr->entries)
1370 entries += tr->entries;
1371 else
1372 entries += tr->data[cpu]->trace_idx;
1376 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1377 name, UTS_RELEASE);
1378 seq_puts(m, "-----------------------------------"
1379 "---------------------------------\n");
1380 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1381 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1382 nsecs_to_usecs(data->saved_latency),
1383 entries,
1384 total,
1385 tr->cpu,
1386 #if defined(CONFIG_PREEMPT_NONE)
1387 "server",
1388 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1389 "desktop",
1390 #elif defined(CONFIG_PREEMPT)
1391 "preempt",
1392 #else
1393 "unknown",
1394 #endif
1395 /* These are reserved for later use */
1396 0, 0, 0, 0);
1397 #ifdef CONFIG_SMP
1398 seq_printf(m, " #P:%d)\n", num_online_cpus());
1399 #else
1400 seq_puts(m, ")\n");
1401 #endif
1402 seq_puts(m, " -----------------\n");
1403 seq_printf(m, " | task: %.16s-%d "
1404 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1405 data->comm, data->pid, data->uid, data->nice,
1406 data->policy, data->rt_priority);
1407 seq_puts(m, " -----------------\n");
1409 if (data->critical_start) {
1410 seq_puts(m, " => started at: ");
1411 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1412 trace_print_seq(m, &iter->seq);
1413 seq_puts(m, "\n => ended at: ");
1414 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1415 trace_print_seq(m, &iter->seq);
1416 seq_puts(m, "\n");
1419 seq_puts(m, "\n");
1422 static void
1423 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1425 int hardirq, softirq;
1426 char *comm;
1428 comm = trace_find_cmdline(entry->pid);
1430 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1431 trace_seq_printf(s, "%d", cpu);
1432 trace_seq_printf(s, "%c%c",
1433 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1434 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1436 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1437 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1438 if (hardirq && softirq) {
1439 trace_seq_putc(s, 'H');
1440 } else {
1441 if (hardirq) {
1442 trace_seq_putc(s, 'h');
1443 } else {
1444 if (softirq)
1445 trace_seq_putc(s, 's');
1446 else
1447 trace_seq_putc(s, '.');
1451 if (entry->preempt_count)
1452 trace_seq_printf(s, "%x", entry->preempt_count);
1453 else
1454 trace_seq_puts(s, ".");
1457 unsigned long preempt_mark_thresh = 100;
1459 static void
1460 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1461 unsigned long rel_usecs)
1463 trace_seq_printf(s, " %4lldus", abs_usecs);
1464 if (rel_usecs > preempt_mark_thresh)
1465 trace_seq_puts(s, "!: ");
1466 else if (rel_usecs > 1)
1467 trace_seq_puts(s, "+: ");
1468 else
1469 trace_seq_puts(s, " : ");
1472 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1474 static int
1475 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1477 struct trace_seq *s = &iter->seq;
1478 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1479 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1480 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1481 struct trace_entry *entry = iter->ent;
1482 unsigned long abs_usecs;
1483 unsigned long rel_usecs;
1484 char *comm;
1485 int S, T;
1486 int i;
1487 unsigned state;
1489 if (!next_entry)
1490 next_entry = entry;
1491 rel_usecs = ns2usecs(next_entry->t - entry->t);
1492 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1494 if (verbose) {
1495 comm = trace_find_cmdline(entry->pid);
1496 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1497 " %ld.%03ldms (+%ld.%03ldms): ",
1498 comm,
1499 entry->pid, cpu, entry->flags,
1500 entry->preempt_count, trace_idx,
1501 ns2usecs(entry->t),
1502 abs_usecs/1000,
1503 abs_usecs % 1000, rel_usecs/1000,
1504 rel_usecs % 1000);
1505 } else {
1506 lat_print_generic(s, entry, cpu);
1507 lat_print_timestamp(s, abs_usecs, rel_usecs);
1509 switch (entry->type) {
1510 case TRACE_FN:
1511 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1512 trace_seq_puts(s, " (");
1513 if (kretprobed(entry->fn.parent_ip))
1514 trace_seq_puts(s, KRETPROBE_MSG);
1515 else
1516 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1517 trace_seq_puts(s, ")\n");
1518 break;
1519 case TRACE_CTX:
1520 case TRACE_WAKE:
1521 T = entry->ctx.next_state < sizeof(state_to_char) ?
1522 state_to_char[entry->ctx.next_state] : 'X';
1524 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1525 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1526 comm = trace_find_cmdline(entry->ctx.next_pid);
1527 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1528 entry->ctx.prev_pid,
1529 entry->ctx.prev_prio,
1530 S, entry->type == TRACE_CTX ? "==>" : " +",
1531 entry->ctx.next_pid,
1532 entry->ctx.next_prio,
1533 T, comm);
1534 break;
1535 case TRACE_SPECIAL:
1536 trace_seq_printf(s, "# %ld %ld %ld\n",
1537 entry->special.arg1,
1538 entry->special.arg2,
1539 entry->special.arg3);
1540 break;
1541 case TRACE_STACK:
1542 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1543 if (i)
1544 trace_seq_puts(s, " <= ");
1545 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1547 trace_seq_puts(s, "\n");
1548 break;
1549 default:
1550 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1552 return 1;
1555 static int print_trace_fmt(struct trace_iterator *iter)
1557 struct trace_seq *s = &iter->seq;
1558 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1559 struct trace_entry *entry;
1560 unsigned long usec_rem;
1561 unsigned long long t;
1562 unsigned long secs;
1563 char *comm;
1564 int ret;
1565 int S, T;
1566 int i;
1568 entry = iter->ent;
1570 comm = trace_find_cmdline(iter->ent->pid);
1572 t = ns2usecs(entry->t);
1573 usec_rem = do_div(t, 1000000ULL);
1574 secs = (unsigned long)t;
1576 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1577 if (!ret)
1578 return 0;
1579 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1580 if (!ret)
1581 return 0;
1582 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1583 if (!ret)
1584 return 0;
1586 switch (entry->type) {
1587 case TRACE_FN:
1588 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1589 if (!ret)
1590 return 0;
1591 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1592 entry->fn.parent_ip) {
1593 ret = trace_seq_printf(s, " <-");
1594 if (!ret)
1595 return 0;
1596 if (kretprobed(entry->fn.parent_ip))
1597 ret = trace_seq_puts(s, KRETPROBE_MSG);
1598 else
1599 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1600 sym_flags);
1601 if (!ret)
1602 return 0;
1604 ret = trace_seq_printf(s, "\n");
1605 if (!ret)
1606 return 0;
1607 break;
1608 case TRACE_CTX:
1609 case TRACE_WAKE:
1610 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1611 state_to_char[entry->ctx.prev_state] : 'X';
1612 T = entry->ctx.next_state < sizeof(state_to_char) ?
1613 state_to_char[entry->ctx.next_state] : 'X';
1614 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
1615 entry->ctx.prev_pid,
1616 entry->ctx.prev_prio,
1618 entry->type == TRACE_CTX ? "==>" : " +",
1619 entry->ctx.next_pid,
1620 entry->ctx.next_prio,
1622 if (!ret)
1623 return 0;
1624 break;
1625 case TRACE_SPECIAL:
1626 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1627 entry->special.arg1,
1628 entry->special.arg2,
1629 entry->special.arg3);
1630 if (!ret)
1631 return 0;
1632 break;
1633 case TRACE_STACK:
1634 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1635 if (i) {
1636 ret = trace_seq_puts(s, " <= ");
1637 if (!ret)
1638 return 0;
1640 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1641 sym_flags);
1642 if (!ret)
1643 return 0;
1645 ret = trace_seq_puts(s, "\n");
1646 if (!ret)
1647 return 0;
1648 break;
1650 return 1;
1653 static int print_raw_fmt(struct trace_iterator *iter)
1655 struct trace_seq *s = &iter->seq;
1656 struct trace_entry *entry;
1657 int ret;
1658 int S, T;
1660 entry = iter->ent;
1662 ret = trace_seq_printf(s, "%d %d %llu ",
1663 entry->pid, iter->cpu, entry->t);
1664 if (!ret)
1665 return 0;
1667 switch (entry->type) {
1668 case TRACE_FN:
1669 ret = trace_seq_printf(s, "%x %x\n",
1670 entry->fn.ip, entry->fn.parent_ip);
1671 if (!ret)
1672 return 0;
1673 break;
1674 case TRACE_CTX:
1675 case TRACE_WAKE:
1676 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1677 state_to_char[entry->ctx.prev_state] : 'X';
1678 T = entry->ctx.next_state < sizeof(state_to_char) ?
1679 state_to_char[entry->ctx.next_state] : 'X';
1680 if (entry->type == TRACE_WAKE)
1681 S = '+';
1682 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
1683 entry->ctx.prev_pid,
1684 entry->ctx.prev_prio,
1686 entry->ctx.next_pid,
1687 entry->ctx.next_prio,
1689 if (!ret)
1690 return 0;
1691 break;
1692 case TRACE_SPECIAL:
1693 case TRACE_STACK:
1694 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1695 entry->special.arg1,
1696 entry->special.arg2,
1697 entry->special.arg3);
1698 if (!ret)
1699 return 0;
1700 break;
1702 return 1;
1705 #define SEQ_PUT_FIELD_RET(s, x) \
1706 do { \
1707 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1708 return 0; \
1709 } while (0)
1711 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1712 do { \
1713 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1714 return 0; \
1715 } while (0)
1717 static int print_hex_fmt(struct trace_iterator *iter)
1719 struct trace_seq *s = &iter->seq;
1720 unsigned char newline = '\n';
1721 struct trace_entry *entry;
1722 int S, T;
1724 entry = iter->ent;
1726 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1727 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1728 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1730 switch (entry->type) {
1731 case TRACE_FN:
1732 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1733 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1734 break;
1735 case TRACE_CTX:
1736 case TRACE_WAKE:
1737 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1738 state_to_char[entry->ctx.prev_state] : 'X';
1739 T = entry->ctx.next_state < sizeof(state_to_char) ?
1740 state_to_char[entry->ctx.next_state] : 'X';
1741 if (entry->type == TRACE_WAKE)
1742 S = '+';
1743 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1744 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1745 SEQ_PUT_HEX_FIELD_RET(s, S);
1746 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1747 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1748 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1749 SEQ_PUT_HEX_FIELD_RET(s, T);
1750 break;
1751 case TRACE_SPECIAL:
1752 case TRACE_STACK:
1753 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1754 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1755 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1756 break;
1758 SEQ_PUT_FIELD_RET(s, newline);
1760 return 1;
1763 static int print_bin_fmt(struct trace_iterator *iter)
1765 struct trace_seq *s = &iter->seq;
1766 struct trace_entry *entry;
1768 entry = iter->ent;
1770 SEQ_PUT_FIELD_RET(s, entry->pid);
1771 SEQ_PUT_FIELD_RET(s, entry->cpu);
1772 SEQ_PUT_FIELD_RET(s, entry->t);
1774 switch (entry->type) {
1775 case TRACE_FN:
1776 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1777 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1778 break;
1779 case TRACE_CTX:
1780 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1781 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1782 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1783 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1784 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1785 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
1786 break;
1787 case TRACE_SPECIAL:
1788 case TRACE_STACK:
1789 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1790 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1791 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1792 break;
1794 return 1;
1797 static int trace_empty(struct trace_iterator *iter)
1799 struct trace_array_cpu *data;
1800 int cpu;
1802 for_each_tracing_cpu(cpu) {
1803 data = iter->tr->data[cpu];
1805 if (head_page(data) && data->trace_idx &&
1806 (data->trace_tail != data->trace_head ||
1807 data->trace_tail_idx != data->trace_head_idx))
1808 return 0;
1810 return 1;
1813 static int print_trace_line(struct trace_iterator *iter)
1815 if (iter->trace && iter->trace->print_line)
1816 return iter->trace->print_line(iter);
1818 if (trace_flags & TRACE_ITER_BIN)
1819 return print_bin_fmt(iter);
1821 if (trace_flags & TRACE_ITER_HEX)
1822 return print_hex_fmt(iter);
1824 if (trace_flags & TRACE_ITER_RAW)
1825 return print_raw_fmt(iter);
1827 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1828 return print_lat_fmt(iter, iter->idx, iter->cpu);
1830 return print_trace_fmt(iter);
1833 static int s_show(struct seq_file *m, void *v)
1835 struct trace_iterator *iter = v;
1837 if (iter->ent == NULL) {
1838 if (iter->tr) {
1839 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1840 seq_puts(m, "#\n");
1842 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1843 /* print nothing if the buffers are empty */
1844 if (trace_empty(iter))
1845 return 0;
1846 print_trace_header(m, iter);
1847 if (!(trace_flags & TRACE_ITER_VERBOSE))
1848 print_lat_help_header(m);
1849 } else {
1850 if (!(trace_flags & TRACE_ITER_VERBOSE))
1851 print_func_help_header(m);
1853 } else {
1854 print_trace_line(iter);
1855 trace_print_seq(m, &iter->seq);
1858 return 0;
1861 static struct seq_operations tracer_seq_ops = {
1862 .start = s_start,
1863 .next = s_next,
1864 .stop = s_stop,
1865 .show = s_show,
1868 static struct trace_iterator *
1869 __tracing_open(struct inode *inode, struct file *file, int *ret)
1871 struct trace_iterator *iter;
1873 if (tracing_disabled) {
1874 *ret = -ENODEV;
1875 return NULL;
1878 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1879 if (!iter) {
1880 *ret = -ENOMEM;
1881 goto out;
1884 mutex_lock(&trace_types_lock);
1885 if (current_trace && current_trace->print_max)
1886 iter->tr = &max_tr;
1887 else
1888 iter->tr = inode->i_private;
1889 iter->trace = current_trace;
1890 iter->pos = -1;
1892 /* TODO stop tracer */
1893 *ret = seq_open(file, &tracer_seq_ops);
1894 if (!*ret) {
1895 struct seq_file *m = file->private_data;
1896 m->private = iter;
1898 /* stop the trace while dumping */
1899 if (iter->tr->ctrl) {
1900 tracer_enabled = 0;
1901 ftrace_function_enabled = 0;
1904 if (iter->trace && iter->trace->open)
1905 iter->trace->open(iter);
1906 } else {
1907 kfree(iter);
1908 iter = NULL;
1910 mutex_unlock(&trace_types_lock);
1912 out:
1913 return iter;
1916 int tracing_open_generic(struct inode *inode, struct file *filp)
1918 if (tracing_disabled)
1919 return -ENODEV;
1921 filp->private_data = inode->i_private;
1922 return 0;
1925 int tracing_release(struct inode *inode, struct file *file)
1927 struct seq_file *m = (struct seq_file *)file->private_data;
1928 struct trace_iterator *iter = m->private;
1930 mutex_lock(&trace_types_lock);
1931 if (iter->trace && iter->trace->close)
1932 iter->trace->close(iter);
1934 /* reenable tracing if it was previously enabled */
1935 if (iter->tr->ctrl) {
1936 tracer_enabled = 1;
1938 * It is safe to enable function tracing even if it
1939 * isn't used
1941 ftrace_function_enabled = 1;
1943 mutex_unlock(&trace_types_lock);
1945 seq_release(inode, file);
1946 kfree(iter);
1947 return 0;
1950 static int tracing_open(struct inode *inode, struct file *file)
1952 int ret;
1954 __tracing_open(inode, file, &ret);
1956 return ret;
1959 static int tracing_lt_open(struct inode *inode, struct file *file)
1961 struct trace_iterator *iter;
1962 int ret;
1964 iter = __tracing_open(inode, file, &ret);
1966 if (!ret)
1967 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1969 return ret;
1973 static void *
1974 t_next(struct seq_file *m, void *v, loff_t *pos)
1976 struct tracer *t = m->private;
1978 (*pos)++;
1980 if (t)
1981 t = t->next;
1983 m->private = t;
1985 return t;
1988 static void *t_start(struct seq_file *m, loff_t *pos)
1990 struct tracer *t = m->private;
1991 loff_t l = 0;
1993 mutex_lock(&trace_types_lock);
1994 for (; t && l < *pos; t = t_next(m, t, &l))
1997 return t;
2000 static void t_stop(struct seq_file *m, void *p)
2002 mutex_unlock(&trace_types_lock);
2005 static int t_show(struct seq_file *m, void *v)
2007 struct tracer *t = v;
2009 if (!t)
2010 return 0;
2012 seq_printf(m, "%s", t->name);
2013 if (t->next)
2014 seq_putc(m, ' ');
2015 else
2016 seq_putc(m, '\n');
2018 return 0;
2021 static struct seq_operations show_traces_seq_ops = {
2022 .start = t_start,
2023 .next = t_next,
2024 .stop = t_stop,
2025 .show = t_show,
2028 static int show_traces_open(struct inode *inode, struct file *file)
2030 int ret;
2032 if (tracing_disabled)
2033 return -ENODEV;
2035 ret = seq_open(file, &show_traces_seq_ops);
2036 if (!ret) {
2037 struct seq_file *m = file->private_data;
2038 m->private = trace_types;
2041 return ret;
2044 static struct file_operations tracing_fops = {
2045 .open = tracing_open,
2046 .read = seq_read,
2047 .llseek = seq_lseek,
2048 .release = tracing_release,
2051 static struct file_operations tracing_lt_fops = {
2052 .open = tracing_lt_open,
2053 .read = seq_read,
2054 .llseek = seq_lseek,
2055 .release = tracing_release,
2058 static struct file_operations show_traces_fops = {
2059 .open = show_traces_open,
2060 .read = seq_read,
2061 .release = seq_release,
2065 * Only trace on a CPU if the bitmask is set:
2067 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2070 * When tracing/tracing_cpu_mask is modified then this holds
2071 * the new bitmask we are about to install:
2073 static cpumask_t tracing_cpumask_new;
2076 * The tracer itself will not take this lock, but still we want
2077 * to provide a consistent cpumask to user-space:
2079 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2082 * Temporary storage for the character representation of the
2083 * CPU bitmask (and one more byte for the newline):
2085 static char mask_str[NR_CPUS + 1];
2087 static ssize_t
2088 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2089 size_t count, loff_t *ppos)
2091 int len;
2093 mutex_lock(&tracing_cpumask_update_lock);
2095 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2096 if (count - len < 2) {
2097 count = -EINVAL;
2098 goto out_err;
2100 len += sprintf(mask_str + len, "\n");
2101 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2103 out_err:
2104 mutex_unlock(&tracing_cpumask_update_lock);
2106 return count;
2109 static ssize_t
2110 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2111 size_t count, loff_t *ppos)
2113 int err, cpu;
2115 mutex_lock(&tracing_cpumask_update_lock);
2116 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2117 if (err)
2118 goto err_unlock;
2120 raw_local_irq_disable();
2121 __raw_spin_lock(&ftrace_max_lock);
2122 for_each_tracing_cpu(cpu) {
2124 * Increase/decrease the disabled counter if we are
2125 * about to flip a bit in the cpumask:
2127 if (cpu_isset(cpu, tracing_cpumask) &&
2128 !cpu_isset(cpu, tracing_cpumask_new)) {
2129 atomic_inc(&global_trace.data[cpu]->disabled);
2131 if (!cpu_isset(cpu, tracing_cpumask) &&
2132 cpu_isset(cpu, tracing_cpumask_new)) {
2133 atomic_dec(&global_trace.data[cpu]->disabled);
2136 __raw_spin_unlock(&ftrace_max_lock);
2137 raw_local_irq_enable();
2139 tracing_cpumask = tracing_cpumask_new;
2141 mutex_unlock(&tracing_cpumask_update_lock);
2143 return count;
2145 err_unlock:
2146 mutex_unlock(&tracing_cpumask_update_lock);
2148 return err;
2151 static struct file_operations tracing_cpumask_fops = {
2152 .open = tracing_open_generic,
2153 .read = tracing_cpumask_read,
2154 .write = tracing_cpumask_write,
2157 static ssize_t
2158 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2159 size_t cnt, loff_t *ppos)
2161 char *buf;
2162 int r = 0;
2163 int len = 0;
2164 int i;
2166 /* calulate max size */
2167 for (i = 0; trace_options[i]; i++) {
2168 len += strlen(trace_options[i]);
2169 len += 3; /* "no" and space */
2172 /* +2 for \n and \0 */
2173 buf = kmalloc(len + 2, GFP_KERNEL);
2174 if (!buf)
2175 return -ENOMEM;
2177 for (i = 0; trace_options[i]; i++) {
2178 if (trace_flags & (1 << i))
2179 r += sprintf(buf + r, "%s ", trace_options[i]);
2180 else
2181 r += sprintf(buf + r, "no%s ", trace_options[i]);
2184 r += sprintf(buf + r, "\n");
2185 WARN_ON(r >= len + 2);
2187 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2189 kfree(buf);
2191 return r;
2194 static ssize_t
2195 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2196 size_t cnt, loff_t *ppos)
2198 char buf[64];
2199 char *cmp = buf;
2200 int neg = 0;
2201 int i;
2203 if (cnt >= sizeof(buf))
2204 return -EINVAL;
2206 if (copy_from_user(&buf, ubuf, cnt))
2207 return -EFAULT;
2209 buf[cnt] = 0;
2211 if (strncmp(buf, "no", 2) == 0) {
2212 neg = 1;
2213 cmp += 2;
2216 for (i = 0; trace_options[i]; i++) {
2217 int len = strlen(trace_options[i]);
2219 if (strncmp(cmp, trace_options[i], len) == 0) {
2220 if (neg)
2221 trace_flags &= ~(1 << i);
2222 else
2223 trace_flags |= (1 << i);
2224 break;
2228 * If no option could be set, return an error:
2230 if (!trace_options[i])
2231 return -EINVAL;
2233 filp->f_pos += cnt;
2235 return cnt;
2238 static struct file_operations tracing_iter_fops = {
2239 .open = tracing_open_generic,
2240 .read = tracing_iter_ctrl_read,
2241 .write = tracing_iter_ctrl_write,
2244 static const char readme_msg[] =
2245 "tracing mini-HOWTO:\n\n"
2246 "# mkdir /debug\n"
2247 "# mount -t debugfs nodev /debug\n\n"
2248 "# cat /debug/tracing/available_tracers\n"
2249 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2250 "# cat /debug/tracing/current_tracer\n"
2251 "none\n"
2252 "# echo sched_switch > /debug/tracing/current_tracer\n"
2253 "# cat /debug/tracing/current_tracer\n"
2254 "sched_switch\n"
2255 "# cat /debug/tracing/iter_ctrl\n"
2256 "noprint-parent nosym-offset nosym-addr noverbose\n"
2257 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2258 "# echo 1 > /debug/tracing/tracing_enabled\n"
2259 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2260 "echo 0 > /debug/tracing/tracing_enabled\n"
2263 static ssize_t
2264 tracing_readme_read(struct file *filp, char __user *ubuf,
2265 size_t cnt, loff_t *ppos)
2267 return simple_read_from_buffer(ubuf, cnt, ppos,
2268 readme_msg, strlen(readme_msg));
2271 static struct file_operations tracing_readme_fops = {
2272 .open = tracing_open_generic,
2273 .read = tracing_readme_read,
2276 static ssize_t
2277 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2278 size_t cnt, loff_t *ppos)
2280 struct trace_array *tr = filp->private_data;
2281 char buf[64];
2282 int r;
2284 r = sprintf(buf, "%ld\n", tr->ctrl);
2285 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2288 static ssize_t
2289 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2290 size_t cnt, loff_t *ppos)
2292 struct trace_array *tr = filp->private_data;
2293 char buf[64];
2294 long val;
2295 int ret;
2297 if (cnt >= sizeof(buf))
2298 return -EINVAL;
2300 if (copy_from_user(&buf, ubuf, cnt))
2301 return -EFAULT;
2303 buf[cnt] = 0;
2305 ret = strict_strtoul(buf, 10, &val);
2306 if (ret < 0)
2307 return ret;
2309 val = !!val;
2311 mutex_lock(&trace_types_lock);
2312 if (tr->ctrl ^ val) {
2313 if (val)
2314 tracer_enabled = 1;
2315 else
2316 tracer_enabled = 0;
2318 tr->ctrl = val;
2320 if (current_trace && current_trace->ctrl_update)
2321 current_trace->ctrl_update(tr);
2323 mutex_unlock(&trace_types_lock);
2325 filp->f_pos += cnt;
2327 return cnt;
2330 static ssize_t
2331 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2332 size_t cnt, loff_t *ppos)
2334 char buf[max_tracer_type_len+2];
2335 int r;
2337 mutex_lock(&trace_types_lock);
2338 if (current_trace)
2339 r = sprintf(buf, "%s\n", current_trace->name);
2340 else
2341 r = sprintf(buf, "\n");
2342 mutex_unlock(&trace_types_lock);
2344 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2347 static ssize_t
2348 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2349 size_t cnt, loff_t *ppos)
2351 struct trace_array *tr = &global_trace;
2352 struct tracer *t;
2353 char buf[max_tracer_type_len+1];
2354 int i;
2356 if (cnt > max_tracer_type_len)
2357 cnt = max_tracer_type_len;
2359 if (copy_from_user(&buf, ubuf, cnt))
2360 return -EFAULT;
2362 buf[cnt] = 0;
2364 /* strip ending whitespace. */
2365 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2366 buf[i] = 0;
2368 mutex_lock(&trace_types_lock);
2369 for (t = trace_types; t; t = t->next) {
2370 if (strcmp(t->name, buf) == 0)
2371 break;
2373 if (!t || t == current_trace)
2374 goto out;
2376 if (current_trace && current_trace->reset)
2377 current_trace->reset(tr);
2379 current_trace = t;
2380 if (t->init)
2381 t->init(tr);
2383 out:
2384 mutex_unlock(&trace_types_lock);
2386 filp->f_pos += cnt;
2388 return cnt;
2391 static ssize_t
2392 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2393 size_t cnt, loff_t *ppos)
2395 unsigned long *ptr = filp->private_data;
2396 char buf[64];
2397 int r;
2399 r = snprintf(buf, sizeof(buf), "%ld\n",
2400 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2401 if (r > sizeof(buf))
2402 r = sizeof(buf);
2403 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2406 static ssize_t
2407 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2408 size_t cnt, loff_t *ppos)
2410 long *ptr = filp->private_data;
2411 char buf[64];
2412 long val;
2413 int ret;
2415 if (cnt >= sizeof(buf))
2416 return -EINVAL;
2418 if (copy_from_user(&buf, ubuf, cnt))
2419 return -EFAULT;
2421 buf[cnt] = 0;
2423 ret = strict_strtoul(buf, 10, &val);
2424 if (ret < 0)
2425 return ret;
2427 *ptr = val * 1000;
2429 return cnt;
2432 static atomic_t tracing_reader;
2434 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2436 struct trace_iterator *iter;
2438 if (tracing_disabled)
2439 return -ENODEV;
2441 /* We only allow for reader of the pipe */
2442 if (atomic_inc_return(&tracing_reader) != 1) {
2443 atomic_dec(&tracing_reader);
2444 return -EBUSY;
2447 /* create a buffer to store the information to pass to userspace */
2448 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2449 if (!iter)
2450 return -ENOMEM;
2452 mutex_lock(&trace_types_lock);
2453 iter->tr = &global_trace;
2454 iter->trace = current_trace;
2455 filp->private_data = iter;
2457 if (iter->trace->pipe_open)
2458 iter->trace->pipe_open(iter);
2459 mutex_unlock(&trace_types_lock);
2461 return 0;
2464 static int tracing_release_pipe(struct inode *inode, struct file *file)
2466 struct trace_iterator *iter = file->private_data;
2468 kfree(iter);
2469 atomic_dec(&tracing_reader);
2471 return 0;
2474 static unsigned int
2475 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2477 struct trace_iterator *iter = filp->private_data;
2479 if (trace_flags & TRACE_ITER_BLOCK) {
2481 * Always select as readable when in blocking mode
2483 return POLLIN | POLLRDNORM;
2484 } else {
2485 if (!trace_empty(iter))
2486 return POLLIN | POLLRDNORM;
2487 poll_wait(filp, &trace_wait, poll_table);
2488 if (!trace_empty(iter))
2489 return POLLIN | POLLRDNORM;
2491 return 0;
2496 * Consumer reader.
2498 static ssize_t
2499 tracing_read_pipe(struct file *filp, char __user *ubuf,
2500 size_t cnt, loff_t *ppos)
2502 struct trace_iterator *iter = filp->private_data;
2503 struct trace_array_cpu *data;
2504 static cpumask_t mask;
2505 unsigned long flags;
2506 #ifdef CONFIG_FTRACE
2507 int ftrace_save;
2508 #endif
2509 int cpu;
2510 ssize_t sret;
2512 /* return any leftover data */
2513 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2514 if (sret != -EBUSY)
2515 return sret;
2516 sret = 0;
2518 trace_seq_reset(&iter->seq);
2520 mutex_lock(&trace_types_lock);
2521 if (iter->trace->read) {
2522 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2523 if (sret)
2524 goto out;
2527 while (trace_empty(iter)) {
2529 if ((filp->f_flags & O_NONBLOCK)) {
2530 sret = -EAGAIN;
2531 goto out;
2535 * This is a make-shift waitqueue. The reason we don't use
2536 * an actual wait queue is because:
2537 * 1) we only ever have one waiter
2538 * 2) the tracing, traces all functions, we don't want
2539 * the overhead of calling wake_up and friends
2540 * (and tracing them too)
2541 * Anyway, this is really very primitive wakeup.
2543 set_current_state(TASK_INTERRUPTIBLE);
2544 iter->tr->waiter = current;
2546 mutex_unlock(&trace_types_lock);
2548 /* sleep for 100 msecs, and try again. */
2549 schedule_timeout(HZ/10);
2551 mutex_lock(&trace_types_lock);
2553 iter->tr->waiter = NULL;
2555 if (signal_pending(current)) {
2556 sret = -EINTR;
2557 goto out;
2560 if (iter->trace != current_trace)
2561 goto out;
2564 * We block until we read something and tracing is disabled.
2565 * We still block if tracing is disabled, but we have never
2566 * read anything. This allows a user to cat this file, and
2567 * then enable tracing. But after we have read something,
2568 * we give an EOF when tracing is again disabled.
2570 * iter->pos will be 0 if we haven't read anything.
2572 if (!tracer_enabled && iter->pos)
2573 break;
2575 continue;
2578 /* stop when tracing is finished */
2579 if (trace_empty(iter))
2580 goto out;
2582 if (cnt >= PAGE_SIZE)
2583 cnt = PAGE_SIZE - 1;
2585 /* reset all but tr, trace, and overruns */
2586 memset(&iter->seq, 0,
2587 sizeof(struct trace_iterator) -
2588 offsetof(struct trace_iterator, seq));
2589 iter->pos = -1;
2592 * We need to stop all tracing on all CPUS to read the
2593 * the next buffer. This is a bit expensive, but is
2594 * not done often. We fill all what we can read,
2595 * and then release the locks again.
2598 cpus_clear(mask);
2599 local_irq_save(flags);
2600 #ifdef CONFIG_FTRACE
2601 ftrace_save = ftrace_enabled;
2602 ftrace_enabled = 0;
2603 #endif
2604 smp_wmb();
2605 for_each_tracing_cpu(cpu) {
2606 data = iter->tr->data[cpu];
2608 if (!head_page(data) || !data->trace_idx)
2609 continue;
2611 atomic_inc(&data->disabled);
2612 cpu_set(cpu, mask);
2615 for_each_cpu_mask(cpu, mask) {
2616 data = iter->tr->data[cpu];
2617 __raw_spin_lock(&data->lock);
2619 if (data->overrun > iter->last_overrun[cpu])
2620 iter->overrun[cpu] +=
2621 data->overrun - iter->last_overrun[cpu];
2622 iter->last_overrun[cpu] = data->overrun;
2625 while (find_next_entry_inc(iter) != NULL) {
2626 int ret;
2627 int len = iter->seq.len;
2629 ret = print_trace_line(iter);
2630 if (!ret) {
2631 /* don't print partial lines */
2632 iter->seq.len = len;
2633 break;
2636 trace_consume(iter);
2638 if (iter->seq.len >= cnt)
2639 break;
2642 for_each_cpu_mask(cpu, mask) {
2643 data = iter->tr->data[cpu];
2644 __raw_spin_unlock(&data->lock);
2647 for_each_cpu_mask(cpu, mask) {
2648 data = iter->tr->data[cpu];
2649 atomic_dec(&data->disabled);
2651 #ifdef CONFIG_FTRACE
2652 ftrace_enabled = ftrace_save;
2653 #endif
2654 local_irq_restore(flags);
2656 /* Now copy what we have to the user */
2657 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2658 if (iter->seq.readpos >= iter->seq.len)
2659 trace_seq_reset(&iter->seq);
2660 if (sret == -EBUSY)
2661 sret = 0;
2663 out:
2664 mutex_unlock(&trace_types_lock);
2666 return sret;
2669 static ssize_t
2670 tracing_entries_read(struct file *filp, char __user *ubuf,
2671 size_t cnt, loff_t *ppos)
2673 struct trace_array *tr = filp->private_data;
2674 char buf[64];
2675 int r;
2677 r = sprintf(buf, "%lu\n", tr->entries);
2678 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2681 static ssize_t
2682 tracing_entries_write(struct file *filp, const char __user *ubuf,
2683 size_t cnt, loff_t *ppos)
2685 unsigned long val;
2686 char buf[64];
2687 int i, ret;
2689 if (cnt >= sizeof(buf))
2690 return -EINVAL;
2692 if (copy_from_user(&buf, ubuf, cnt))
2693 return -EFAULT;
2695 buf[cnt] = 0;
2697 ret = strict_strtoul(buf, 10, &val);
2698 if (ret < 0)
2699 return ret;
2701 /* must have at least 1 entry */
2702 if (!val)
2703 return -EINVAL;
2705 mutex_lock(&trace_types_lock);
2707 if (current_trace != &no_tracer) {
2708 cnt = -EBUSY;
2709 pr_info("ftrace: set current_tracer to none"
2710 " before modifying buffer size\n");
2711 goto out;
2714 if (val > global_trace.entries) {
2715 long pages_requested;
2716 unsigned long freeable_pages;
2718 /* make sure we have enough memory before mapping */
2719 pages_requested =
2720 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2722 /* account for each buffer (and max_tr) */
2723 pages_requested *= tracing_nr_buffers * 2;
2725 /* Check for overflow */
2726 if (pages_requested < 0) {
2727 cnt = -ENOMEM;
2728 goto out;
2731 freeable_pages = determine_dirtyable_memory();
2733 /* we only allow to request 1/4 of useable memory */
2734 if (pages_requested >
2735 ((freeable_pages + tracing_pages_allocated) / 4)) {
2736 cnt = -ENOMEM;
2737 goto out;
2740 while (global_trace.entries < val) {
2741 if (trace_alloc_page()) {
2742 cnt = -ENOMEM;
2743 goto out;
2745 /* double check that we don't go over the known pages */
2746 if (tracing_pages_allocated > pages_requested)
2747 break;
2750 } else {
2751 /* include the number of entries in val (inc of page entries) */
2752 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2753 trace_free_page();
2756 /* check integrity */
2757 for_each_tracing_cpu(i)
2758 check_pages(global_trace.data[i]);
2760 filp->f_pos += cnt;
2762 /* If check pages failed, return ENOMEM */
2763 if (tracing_disabled)
2764 cnt = -ENOMEM;
2765 out:
2766 max_tr.entries = global_trace.entries;
2767 mutex_unlock(&trace_types_lock);
2769 return cnt;
2772 static struct file_operations tracing_max_lat_fops = {
2773 .open = tracing_open_generic,
2774 .read = tracing_max_lat_read,
2775 .write = tracing_max_lat_write,
2778 static struct file_operations tracing_ctrl_fops = {
2779 .open = tracing_open_generic,
2780 .read = tracing_ctrl_read,
2781 .write = tracing_ctrl_write,
2784 static struct file_operations set_tracer_fops = {
2785 .open = tracing_open_generic,
2786 .read = tracing_set_trace_read,
2787 .write = tracing_set_trace_write,
2790 static struct file_operations tracing_pipe_fops = {
2791 .open = tracing_open_pipe,
2792 .poll = tracing_poll_pipe,
2793 .read = tracing_read_pipe,
2794 .release = tracing_release_pipe,
2797 static struct file_operations tracing_entries_fops = {
2798 .open = tracing_open_generic,
2799 .read = tracing_entries_read,
2800 .write = tracing_entries_write,
2803 #ifdef CONFIG_DYNAMIC_FTRACE
2805 static ssize_t
2806 tracing_read_long(struct file *filp, char __user *ubuf,
2807 size_t cnt, loff_t *ppos)
2809 unsigned long *p = filp->private_data;
2810 char buf[64];
2811 int r;
2813 r = sprintf(buf, "%ld\n", *p);
2815 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2818 static struct file_operations tracing_read_long_fops = {
2819 .open = tracing_open_generic,
2820 .read = tracing_read_long,
2822 #endif
2824 static struct dentry *d_tracer;
2826 struct dentry *tracing_init_dentry(void)
2828 static int once;
2830 if (d_tracer)
2831 return d_tracer;
2833 d_tracer = debugfs_create_dir("tracing", NULL);
2835 if (!d_tracer && !once) {
2836 once = 1;
2837 pr_warning("Could not create debugfs directory 'tracing'\n");
2838 return NULL;
2841 return d_tracer;
2844 #ifdef CONFIG_FTRACE_SELFTEST
2845 /* Let selftest have access to static functions in this file */
2846 #include "trace_selftest.c"
2847 #endif
2849 static __init void tracer_init_debugfs(void)
2851 struct dentry *d_tracer;
2852 struct dentry *entry;
2854 d_tracer = tracing_init_dentry();
2856 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2857 &global_trace, &tracing_ctrl_fops);
2858 if (!entry)
2859 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2861 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2862 NULL, &tracing_iter_fops);
2863 if (!entry)
2864 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2866 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2867 NULL, &tracing_cpumask_fops);
2868 if (!entry)
2869 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2871 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2872 &global_trace, &tracing_lt_fops);
2873 if (!entry)
2874 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2876 entry = debugfs_create_file("trace", 0444, d_tracer,
2877 &global_trace, &tracing_fops);
2878 if (!entry)
2879 pr_warning("Could not create debugfs 'trace' entry\n");
2881 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2882 &global_trace, &show_traces_fops);
2883 if (!entry)
2884 pr_warning("Could not create debugfs 'trace' entry\n");
2886 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2887 &global_trace, &set_tracer_fops);
2888 if (!entry)
2889 pr_warning("Could not create debugfs 'trace' entry\n");
2891 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2892 &tracing_max_latency,
2893 &tracing_max_lat_fops);
2894 if (!entry)
2895 pr_warning("Could not create debugfs "
2896 "'tracing_max_latency' entry\n");
2898 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2899 &tracing_thresh, &tracing_max_lat_fops);
2900 if (!entry)
2901 pr_warning("Could not create debugfs "
2902 "'tracing_threash' entry\n");
2903 entry = debugfs_create_file("README", 0644, d_tracer,
2904 NULL, &tracing_readme_fops);
2905 if (!entry)
2906 pr_warning("Could not create debugfs 'README' entry\n");
2908 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2909 NULL, &tracing_pipe_fops);
2910 if (!entry)
2911 pr_warning("Could not create debugfs "
2912 "'tracing_threash' entry\n");
2914 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2915 &global_trace, &tracing_entries_fops);
2916 if (!entry)
2917 pr_warning("Could not create debugfs "
2918 "'tracing_threash' entry\n");
2920 #ifdef CONFIG_DYNAMIC_FTRACE
2921 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2922 &ftrace_update_tot_cnt,
2923 &tracing_read_long_fops);
2924 if (!entry)
2925 pr_warning("Could not create debugfs "
2926 "'dyn_ftrace_total_info' entry\n");
2927 #endif
2928 #ifdef CONFIG_SYSPROF_TRACER
2929 init_tracer_sysprof_debugfs(d_tracer);
2930 #endif
2933 static int trace_alloc_page(void)
2935 struct trace_array_cpu *data;
2936 struct page *page, *tmp;
2937 LIST_HEAD(pages);
2938 void *array;
2939 unsigned pages_allocated = 0;
2940 int i;
2942 /* first allocate a page for each CPU */
2943 for_each_tracing_cpu(i) {
2944 array = (void *)__get_free_page(GFP_KERNEL);
2945 if (array == NULL) {
2946 printk(KERN_ERR "tracer: failed to allocate page"
2947 "for trace buffer!\n");
2948 goto free_pages;
2951 pages_allocated++;
2952 page = virt_to_page(array);
2953 list_add(&page->lru, &pages);
2955 /* Only allocate if we are actually using the max trace */
2956 #ifdef CONFIG_TRACER_MAX_TRACE
2957 array = (void *)__get_free_page(GFP_KERNEL);
2958 if (array == NULL) {
2959 printk(KERN_ERR "tracer: failed to allocate page"
2960 "for trace buffer!\n");
2961 goto free_pages;
2963 pages_allocated++;
2964 page = virt_to_page(array);
2965 list_add(&page->lru, &pages);
2966 #endif
2969 /* Now that we successfully allocate a page per CPU, add them */
2970 for_each_tracing_cpu(i) {
2971 data = global_trace.data[i];
2972 page = list_entry(pages.next, struct page, lru);
2973 list_del_init(&page->lru);
2974 list_add_tail(&page->lru, &data->trace_pages);
2975 ClearPageLRU(page);
2977 #ifdef CONFIG_TRACER_MAX_TRACE
2978 data = max_tr.data[i];
2979 page = list_entry(pages.next, struct page, lru);
2980 list_del_init(&page->lru);
2981 list_add_tail(&page->lru, &data->trace_pages);
2982 SetPageLRU(page);
2983 #endif
2985 tracing_pages_allocated += pages_allocated;
2986 global_trace.entries += ENTRIES_PER_PAGE;
2988 return 0;
2990 free_pages:
2991 list_for_each_entry_safe(page, tmp, &pages, lru) {
2992 list_del_init(&page->lru);
2993 __free_page(page);
2995 return -ENOMEM;
2998 static int trace_free_page(void)
3000 struct trace_array_cpu *data;
3001 struct page *page;
3002 struct list_head *p;
3003 int i;
3004 int ret = 0;
3006 /* free one page from each buffer */
3007 for_each_tracing_cpu(i) {
3008 data = global_trace.data[i];
3009 p = data->trace_pages.next;
3010 if (p == &data->trace_pages) {
3011 /* should never happen */
3012 WARN_ON(1);
3013 tracing_disabled = 1;
3014 ret = -1;
3015 break;
3017 page = list_entry(p, struct page, lru);
3018 ClearPageLRU(page);
3019 list_del(&page->lru);
3020 tracing_pages_allocated--;
3021 tracing_pages_allocated--;
3022 __free_page(page);
3024 tracing_reset(data);
3026 #ifdef CONFIG_TRACER_MAX_TRACE
3027 data = max_tr.data[i];
3028 p = data->trace_pages.next;
3029 if (p == &data->trace_pages) {
3030 /* should never happen */
3031 WARN_ON(1);
3032 tracing_disabled = 1;
3033 ret = -1;
3034 break;
3036 page = list_entry(p, struct page, lru);
3037 ClearPageLRU(page);
3038 list_del(&page->lru);
3039 __free_page(page);
3041 tracing_reset(data);
3042 #endif
3044 global_trace.entries -= ENTRIES_PER_PAGE;
3046 return ret;
3049 __init static int tracer_alloc_buffers(void)
3051 struct trace_array_cpu *data;
3052 void *array;
3053 struct page *page;
3054 int pages = 0;
3055 int ret = -ENOMEM;
3056 int i;
3058 /* TODO: make the number of buffers hot pluggable with CPUS */
3059 tracing_nr_buffers = num_possible_cpus();
3060 tracing_buffer_mask = cpu_possible_map;
3062 /* Allocate the first page for all buffers */
3063 for_each_tracing_cpu(i) {
3064 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3065 max_tr.data[i] = &per_cpu(max_data, i);
3067 array = (void *)__get_free_page(GFP_KERNEL);
3068 if (array == NULL) {
3069 printk(KERN_ERR "tracer: failed to allocate page"
3070 "for trace buffer!\n");
3071 goto free_buffers;
3074 /* set the array to the list */
3075 INIT_LIST_HEAD(&data->trace_pages);
3076 page = virt_to_page(array);
3077 list_add(&page->lru, &data->trace_pages);
3078 /* use the LRU flag to differentiate the two buffers */
3079 ClearPageLRU(page);
3081 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3082 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3084 /* Only allocate if we are actually using the max trace */
3085 #ifdef CONFIG_TRACER_MAX_TRACE
3086 array = (void *)__get_free_page(GFP_KERNEL);
3087 if (array == NULL) {
3088 printk(KERN_ERR "tracer: failed to allocate page"
3089 "for trace buffer!\n");
3090 goto free_buffers;
3093 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3094 page = virt_to_page(array);
3095 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3096 SetPageLRU(page);
3097 #endif
3101 * Since we allocate by orders of pages, we may be able to
3102 * round up a bit.
3104 global_trace.entries = ENTRIES_PER_PAGE;
3105 pages++;
3107 while (global_trace.entries < trace_nr_entries) {
3108 if (trace_alloc_page())
3109 break;
3110 pages++;
3112 max_tr.entries = global_trace.entries;
3114 pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
3115 pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
3116 pr_info(" actual entries %ld\n", global_trace.entries);
3118 tracer_init_debugfs();
3120 trace_init_cmdlines();
3122 register_tracer(&no_tracer);
3123 current_trace = &no_tracer;
3125 /* All seems OK, enable tracing */
3126 global_trace.ctrl = tracer_enabled;
3127 tracing_disabled = 0;
3129 return 0;
3131 free_buffers:
3132 for (i-- ; i >= 0; i--) {
3133 struct page *page, *tmp;
3134 struct trace_array_cpu *data = global_trace.data[i];
3136 if (data) {
3137 list_for_each_entry_safe(page, tmp,
3138 &data->trace_pages, lru) {
3139 list_del_init(&page->lru);
3140 __free_page(page);
3144 #ifdef CONFIG_TRACER_MAX_TRACE
3145 data = max_tr.data[i];
3146 if (data) {
3147 list_for_each_entry_safe(page, tmp,
3148 &data->trace_pages, lru) {
3149 list_del_init(&page->lru);
3150 __free_page(page);
3153 #endif
3155 return ret;
3157 fs_initcall(tracer_alloc_buffers);