ftrace: add trace_special()
[linux-2.6/mini2440.git] / kernel / trace / trace.c
blobfa13059eb4627d17fe80ee6ecca8e3a25d8fac5b
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/gfp.h>
28 #include <linux/fs.h>
30 #include "trace.h"
32 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
33 unsigned long __read_mostly tracing_thresh;
35 static int tracing_disabled = 1;
37 static long notrace
38 ns2usecs(cycle_t nsec)
40 nsec += 500;
41 do_div(nsec, 1000);
42 return nsec;
45 static const int time_sync_freq_max = 128;
46 static const cycle_t time_sync_thresh = 100000;
48 static DEFINE_PER_CPU(cycle_t, time_offset);
49 static DEFINE_PER_CPU(cycle_t, prev_cpu_time);
50 static DEFINE_PER_CPU(int, time_sync_count);
51 static DEFINE_PER_CPU(int, time_sync_freq);
54 * Global lock which we take every now and then to synchronize
55 * the CPUs time. This method is not warp-safe, but it's good
56 * enough to synchronize slowly diverging time sources and thus
57 * it's good enough for tracing:
59 static DEFINE_SPINLOCK(time_sync_lock);
60 static cycle_t prev_global_time;
62 static notrace cycle_t __ftrace_now_sync(cycles_t time, int cpu)
64 unsigned long flags;
66 spin_lock_irqsave(&time_sync_lock, flags);
69 * Update the synchronization frequency:
71 if (per_cpu(time_sync_freq, cpu) < time_sync_freq_max)
72 per_cpu(time_sync_freq, cpu) *= 2;
73 per_cpu(time_sync_count, cpu) = per_cpu(time_sync_freq, cpu);
75 if (time < prev_global_time) {
76 per_cpu(time_offset, cpu) += prev_global_time - time;
77 time = prev_global_time;
78 } else {
79 prev_global_time = time;
82 spin_unlock_irqrestore(&time_sync_lock, flags);
84 return time;
87 notrace cycle_t ftrace_now(int cpu)
89 cycle_t prev_cpu_time, time, delta_time;
91 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
92 time = sched_clock() + per_cpu(time_offset, cpu);
93 delta_time = time-prev_cpu_time;
95 if (unlikely(delta_time > time_sync_thresh ||
96 --per_cpu(time_sync_count, cpu) <= 0))
97 time = __ftrace_now_sync(time, cpu);
99 return time;
102 static struct trace_array global_trace;
104 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
106 static struct trace_array max_tr;
108 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
110 static int tracer_enabled;
111 static unsigned long trace_nr_entries = 16384UL;
113 static struct tracer *trace_types __read_mostly;
114 static struct tracer *current_trace __read_mostly;
115 static int max_tracer_type_len;
117 static DEFINE_MUTEX(trace_types_lock);
119 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
121 static int __init set_nr_entries(char *str)
123 if (!str)
124 return 0;
125 trace_nr_entries = simple_strtoul(str, &str, 0);
126 return 1;
128 __setup("trace_entries=", set_nr_entries);
130 unsigned long nsecs_to_usecs(unsigned long nsecs)
132 return nsecs / 1000;
135 enum trace_type {
136 __TRACE_FIRST_TYPE = 0,
138 TRACE_FN,
139 TRACE_CTX,
140 TRACE_SPECIAL,
142 __TRACE_LAST_TYPE
145 enum trace_flag_type {
146 TRACE_FLAG_IRQS_OFF = 0x01,
147 TRACE_FLAG_NEED_RESCHED = 0x02,
148 TRACE_FLAG_HARDIRQ = 0x04,
149 TRACE_FLAG_SOFTIRQ = 0x08,
152 enum trace_iterator_flags {
153 TRACE_ITER_PRINT_PARENT = 0x01,
154 TRACE_ITER_SYM_OFFSET = 0x02,
155 TRACE_ITER_SYM_ADDR = 0x04,
156 TRACE_ITER_VERBOSE = 0x08,
157 TRACE_ITER_RAW = 0x10,
158 TRACE_ITER_BIN = 0x20,
161 #define TRACE_ITER_SYM_MASK \
162 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
164 /* These must match the bit postions above */
165 static const char *trace_options[] = {
166 "print-parent",
167 "sym-offset",
168 "sym-addr",
169 "verbose",
170 "raw",
171 "bin",
172 NULL
175 static unsigned trace_flags;
177 static DEFINE_SPINLOCK(ftrace_max_lock);
180 * Copy the new maximum trace into the separate maximum-trace
181 * structure. (this way the maximum trace is permanently saved,
182 * for later retrieval via /debugfs/tracing/latency_trace)
184 static notrace void
185 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
187 struct trace_array_cpu *data = tr->data[cpu];
189 max_tr.cpu = cpu;
190 max_tr.time_start = data->preempt_timestamp;
192 data = max_tr.data[cpu];
193 data->saved_latency = tracing_max_latency;
195 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
196 data->pid = tsk->pid;
197 data->uid = tsk->uid;
198 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
199 data->policy = tsk->policy;
200 data->rt_priority = tsk->rt_priority;
202 /* record this tasks comm */
203 tracing_record_cmdline(current);
206 void check_pages(struct trace_array_cpu *data)
208 struct page *page, *tmp;
210 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
211 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
213 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
214 BUG_ON(page->lru.next->prev != &page->lru);
215 BUG_ON(page->lru.prev->next != &page->lru);
219 void *head_page(struct trace_array_cpu *data)
221 struct page *page;
223 check_pages(data);
224 if (list_empty(&data->trace_pages))
225 return NULL;
227 page = list_entry(data->trace_pages.next, struct page, lru);
228 BUG_ON(&page->lru == &data->trace_pages);
230 return page_address(page);
233 static notrace int
234 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
236 int len = (PAGE_SIZE - 1) - s->len;
237 va_list ap;
238 int ret;
240 if (!len)
241 return 0;
243 va_start(ap, fmt);
244 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
245 va_end(ap);
247 /* If we can't write it all, don't bother writing anything */
248 if (ret > len)
249 return 0;
251 s->len += ret;
253 return len;
256 static notrace int
257 trace_seq_puts(struct trace_seq *s, const char *str)
259 int len = strlen(str);
261 if (len > ((PAGE_SIZE - 1) - s->len))
262 return 0;
264 memcpy(s->buffer + s->len, str, len);
265 s->len += len;
267 return len;
270 static notrace int
271 trace_seq_putc(struct trace_seq *s, unsigned char c)
273 if (s->len >= (PAGE_SIZE - 1))
274 return 0;
276 s->buffer[s->len++] = c;
278 return 1;
281 static notrace int
282 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
284 if (len > ((PAGE_SIZE - 1) - s->len))
285 return 0;
287 memcpy(s->buffer + s->len, mem, len);
288 s->len += len;
290 return len;
293 static notrace void
294 trace_seq_reset(struct trace_seq *s)
296 s->len = 0;
299 static notrace void
300 trace_print_seq(struct seq_file *m, struct trace_seq *s)
302 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
304 s->buffer[len] = 0;
305 seq_puts(m, s->buffer);
307 trace_seq_reset(s);
310 notrace static void
311 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
313 struct list_head flip_pages;
315 INIT_LIST_HEAD(&flip_pages);
317 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
318 sizeof(struct trace_array_cpu) -
319 offsetof(struct trace_array_cpu, trace_head_idx));
321 check_pages(tr1);
322 check_pages(tr2);
323 list_splice_init(&tr1->trace_pages, &flip_pages);
324 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
325 list_splice_init(&flip_pages, &tr2->trace_pages);
326 BUG_ON(!list_empty(&flip_pages));
327 check_pages(tr1);
328 check_pages(tr2);
331 notrace void
332 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
334 struct trace_array_cpu *data;
335 int i;
337 WARN_ON_ONCE(!irqs_disabled());
338 spin_lock(&ftrace_max_lock);
339 /* clear out all the previous traces */
340 for_each_possible_cpu(i) {
341 data = tr->data[i];
342 flip_trace(max_tr.data[i], data);
343 tracing_reset(data);
346 __update_max_tr(tr, tsk, cpu);
347 spin_unlock(&ftrace_max_lock);
351 * update_max_tr_single - only copy one trace over, and reset the rest
352 * @tr - tracer
353 * @tsk - task with the latency
354 * @cpu - the cpu of the buffer to copy.
356 notrace void
357 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
359 struct trace_array_cpu *data = tr->data[cpu];
360 int i;
362 WARN_ON_ONCE(!irqs_disabled());
363 spin_lock(&ftrace_max_lock);
364 for_each_possible_cpu(i)
365 tracing_reset(max_tr.data[i]);
367 flip_trace(max_tr.data[cpu], data);
368 tracing_reset(data);
370 __update_max_tr(tr, tsk, cpu);
371 spin_unlock(&ftrace_max_lock);
374 int register_tracer(struct tracer *type)
376 struct tracer *t;
377 int len;
378 int ret = 0;
380 if (!type->name) {
381 pr_info("Tracer must have a name\n");
382 return -1;
385 mutex_lock(&trace_types_lock);
386 for (t = trace_types; t; t = t->next) {
387 if (strcmp(type->name, t->name) == 0) {
388 /* already found */
389 pr_info("Trace %s already registered\n",
390 type->name);
391 ret = -1;
392 goto out;
396 #ifdef CONFIG_FTRACE_STARTUP_TEST
397 if (type->selftest) {
398 struct tracer *saved_tracer = current_trace;
399 struct trace_array_cpu *data;
400 struct trace_array *tr = &global_trace;
401 int saved_ctrl = tr->ctrl;
402 int i;
404 * Run a selftest on this tracer.
405 * Here we reset the trace buffer, and set the current
406 * tracer to be this tracer. The tracer can then run some
407 * internal tracing to verify that everything is in order.
408 * If we fail, we do not register this tracer.
410 for_each_possible_cpu(i) {
411 data = tr->data[i];
412 if (!head_page(data))
413 continue;
414 tracing_reset(data);
416 current_trace = type;
417 tr->ctrl = 0;
418 /* the test is responsible for initializing and enabling */
419 pr_info("Testing tracer %s: ", type->name);
420 ret = type->selftest(type, tr);
421 /* the test is responsible for resetting too */
422 current_trace = saved_tracer;
423 tr->ctrl = saved_ctrl;
424 if (ret) {
425 printk(KERN_CONT "FAILED!\n");
426 goto out;
428 /* Only reset on passing, to avoid touching corrupted buffers */
429 for_each_possible_cpu(i) {
430 data = tr->data[i];
431 if (!head_page(data))
432 continue;
433 tracing_reset(data);
435 printk(KERN_CONT "PASSED\n");
437 #endif
439 type->next = trace_types;
440 trace_types = type;
441 len = strlen(type->name);
442 if (len > max_tracer_type_len)
443 max_tracer_type_len = len;
445 out:
446 mutex_unlock(&trace_types_lock);
448 return ret;
451 void unregister_tracer(struct tracer *type)
453 struct tracer **t;
454 int len;
456 mutex_lock(&trace_types_lock);
457 for (t = &trace_types; *t; t = &(*t)->next) {
458 if (*t == type)
459 goto found;
461 pr_info("Trace %s not registered\n", type->name);
462 goto out;
464 found:
465 *t = (*t)->next;
466 if (strlen(type->name) != max_tracer_type_len)
467 goto out;
469 max_tracer_type_len = 0;
470 for (t = &trace_types; *t; t = &(*t)->next) {
471 len = strlen((*t)->name);
472 if (len > max_tracer_type_len)
473 max_tracer_type_len = len;
475 out:
476 mutex_unlock(&trace_types_lock);
479 notrace void tracing_reset(struct trace_array_cpu *data)
481 data->trace_idx = 0;
482 data->trace_head = data->trace_tail = head_page(data);
483 data->trace_head_idx = 0;
484 data->trace_tail_idx = 0;
487 #ifdef CONFIG_FTRACE
488 static notrace void
489 function_trace_call(unsigned long ip, unsigned long parent_ip)
491 struct trace_array *tr = &global_trace;
492 struct trace_array_cpu *data;
493 unsigned long flags;
494 long disabled;
495 int cpu;
497 if (unlikely(!tracer_enabled))
498 return;
500 local_irq_save(flags);
501 cpu = raw_smp_processor_id();
502 data = tr->data[cpu];
503 disabled = atomic_inc_return(&data->disabled);
505 if (likely(disabled == 1))
506 ftrace(tr, data, ip, parent_ip, flags);
508 atomic_dec(&data->disabled);
509 local_irq_restore(flags);
512 static struct ftrace_ops trace_ops __read_mostly =
514 .func = function_trace_call,
516 #endif
518 notrace void tracing_start_function_trace(void)
520 register_ftrace_function(&trace_ops);
523 notrace void tracing_stop_function_trace(void)
525 unregister_ftrace_function(&trace_ops);
528 #define SAVED_CMDLINES 128
529 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
530 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
531 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
532 static int cmdline_idx;
533 static DEFINE_SPINLOCK(trace_cmdline_lock);
534 atomic_t trace_record_cmdline_disabled;
536 static void trace_init_cmdlines(void)
538 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
539 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
540 cmdline_idx = 0;
543 notrace void trace_stop_cmdline_recording(void);
545 static notrace void trace_save_cmdline(struct task_struct *tsk)
547 unsigned map;
548 unsigned idx;
550 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
551 return;
554 * It's not the end of the world if we don't get
555 * the lock, but we also don't want to spin
556 * nor do we want to disable interrupts,
557 * so if we miss here, then better luck next time.
559 if (!spin_trylock(&trace_cmdline_lock))
560 return;
562 idx = map_pid_to_cmdline[tsk->pid];
563 if (idx >= SAVED_CMDLINES) {
564 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
566 map = map_cmdline_to_pid[idx];
567 if (map <= PID_MAX_DEFAULT)
568 map_pid_to_cmdline[map] = (unsigned)-1;
570 map_pid_to_cmdline[tsk->pid] = idx;
572 cmdline_idx = idx;
575 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
577 spin_unlock(&trace_cmdline_lock);
580 static notrace char *trace_find_cmdline(int pid)
582 char *cmdline = "<...>";
583 unsigned map;
585 if (!pid)
586 return "<idle>";
588 if (pid > PID_MAX_DEFAULT)
589 goto out;
591 map = map_pid_to_cmdline[pid];
592 if (map >= SAVED_CMDLINES)
593 goto out;
595 cmdline = saved_cmdlines[map];
597 out:
598 return cmdline;
601 notrace void tracing_record_cmdline(struct task_struct *tsk)
603 if (atomic_read(&trace_record_cmdline_disabled))
604 return;
606 trace_save_cmdline(tsk);
609 static inline notrace struct list_head *
610 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
613 * Roundrobin - but skip the head (which is not a real page):
615 next = next->next;
616 if (unlikely(next == &data->trace_pages))
617 next = next->next;
618 BUG_ON(next == &data->trace_pages);
620 return next;
623 static inline notrace void *
624 trace_next_page(struct trace_array_cpu *data, void *addr)
626 struct list_head *next;
627 struct page *page;
629 page = virt_to_page(addr);
631 next = trace_next_list(data, &page->lru);
632 page = list_entry(next, struct page, lru);
634 return page_address(page);
637 static inline notrace struct trace_entry *
638 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
640 unsigned long idx, idx_next;
641 struct trace_entry *entry;
643 data->trace_idx++;
644 idx = data->trace_head_idx;
645 idx_next = idx + 1;
647 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
649 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
651 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
652 data->trace_head = trace_next_page(data, data->trace_head);
653 idx_next = 0;
656 if (data->trace_head == data->trace_tail &&
657 idx_next == data->trace_tail_idx) {
658 /* overrun */
659 data->trace_tail_idx++;
660 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
661 data->trace_tail =
662 trace_next_page(data, data->trace_tail);
663 data->trace_tail_idx = 0;
667 data->trace_head_idx = idx_next;
669 return entry;
672 static inline notrace void
673 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
675 struct task_struct *tsk = current;
676 unsigned long pc;
678 pc = preempt_count();
680 entry->preempt_count = pc & 0xff;
681 entry->pid = tsk->pid;
682 entry->t = ftrace_now(raw_smp_processor_id());
683 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
684 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
685 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
686 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
689 notrace void
690 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
691 unsigned long ip, unsigned long parent_ip, unsigned long flags)
693 struct trace_entry *entry;
695 spin_lock(&data->lock);
696 entry = tracing_get_trace_entry(tr, data);
697 tracing_generic_entry_update(entry, flags);
698 entry->type = TRACE_FN;
699 entry->fn.ip = ip;
700 entry->fn.parent_ip = parent_ip;
701 spin_unlock(&data->lock);
704 notrace void
705 trace_special(struct trace_array *tr, struct trace_array_cpu *data,
706 unsigned long arg1, unsigned long arg2, unsigned long arg3)
708 struct trace_entry *entry;
710 spin_lock(&data->lock);
711 entry = tracing_get_trace_entry(tr, data);
712 tracing_generic_entry_update(entry, 0);
713 entry->type = TRACE_SPECIAL;
714 entry->special.arg1 = arg1;
715 entry->special.arg2 = arg2;
716 entry->special.arg3 = arg3;
717 spin_unlock(&data->lock);
720 notrace void
721 tracing_sched_switch_trace(struct trace_array *tr,
722 struct trace_array_cpu *data,
723 struct task_struct *prev, struct task_struct *next,
724 unsigned long flags)
726 struct trace_entry *entry;
728 spin_lock(&data->lock);
729 entry = tracing_get_trace_entry(tr, data);
730 tracing_generic_entry_update(entry, flags);
731 entry->type = TRACE_CTX;
732 entry->ctx.prev_pid = prev->pid;
733 entry->ctx.prev_prio = prev->prio;
734 entry->ctx.prev_state = prev->state;
735 entry->ctx.next_pid = next->pid;
736 entry->ctx.next_prio = next->prio;
737 spin_unlock(&data->lock);
740 enum trace_file_type {
741 TRACE_FILE_LAT_FMT = 1,
744 static struct trace_entry *
745 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
746 struct trace_iterator *iter, int cpu)
748 struct page *page;
749 struct trace_entry *array;
751 if (iter->next_idx[cpu] >= tr->entries ||
752 iter->next_idx[cpu] >= data->trace_idx ||
753 (data->trace_head == data->trace_tail &&
754 data->trace_head_idx == data->trace_tail_idx))
755 return NULL;
757 if (!iter->next_page[cpu]) {
758 /* Initialize the iterator for this cpu trace buffer */
759 WARN_ON(!data->trace_tail);
760 page = virt_to_page(data->trace_tail);
761 iter->next_page[cpu] = &page->lru;
762 iter->next_page_idx[cpu] = data->trace_tail_idx;
765 page = list_entry(iter->next_page[cpu], struct page, lru);
766 BUG_ON(&data->trace_pages == &page->lru);
768 array = page_address(page);
770 /* Still possible to catch up to the tail */
771 if (iter->next_idx[cpu] && array == data->trace_tail &&
772 iter->next_page_idx[cpu] == data->trace_tail_idx)
773 return NULL;
775 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
776 return &array[iter->next_page_idx[cpu]];
779 static struct notrace trace_entry *
780 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
782 struct trace_array *tr = iter->tr;
783 struct trace_entry *ent, *next = NULL;
784 int next_cpu = -1;
785 int cpu;
787 for_each_possible_cpu(cpu) {
788 if (!head_page(tr->data[cpu]))
789 continue;
790 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
792 * Pick the entry with the smallest timestamp:
794 if (ent && (!next || ent->t < next->t)) {
795 next = ent;
796 next_cpu = cpu;
800 if (ent_cpu)
801 *ent_cpu = next_cpu;
803 return next;
806 static notrace void trace_iterator_increment(struct trace_iterator *iter)
808 iter->idx++;
809 iter->next_idx[iter->cpu]++;
810 iter->next_page_idx[iter->cpu]++;
812 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
813 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
815 iter->next_page_idx[iter->cpu] = 0;
816 iter->next_page[iter->cpu] =
817 trace_next_list(data, iter->next_page[iter->cpu]);
821 static notrace void trace_consume(struct trace_iterator *iter)
823 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
825 data->trace_tail_idx++;
826 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
827 data->trace_tail = trace_next_page(data, data->trace_tail);
828 data->trace_tail_idx = 0;
831 /* Check if we empty it, then reset the index */
832 if (data->trace_head == data->trace_tail &&
833 data->trace_head_idx == data->trace_tail_idx)
834 data->trace_idx = 0;
837 static notrace void *find_next_entry_inc(struct trace_iterator *iter)
839 struct trace_entry *next;
840 int next_cpu = -1;
842 next = find_next_entry(iter, &next_cpu);
844 iter->prev_ent = iter->ent;
845 iter->prev_cpu = iter->cpu;
847 iter->ent = next;
848 iter->cpu = next_cpu;
850 if (next)
851 trace_iterator_increment(iter);
853 return next ? iter : NULL;
856 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
858 struct trace_iterator *iter = m->private;
859 void *last_ent = iter->ent;
860 int i = (int)*pos;
861 void *ent;
863 (*pos)++;
865 /* can't go backwards */
866 if (iter->idx > i)
867 return NULL;
869 if (iter->idx < 0)
870 ent = find_next_entry_inc(iter);
871 else
872 ent = iter;
874 while (ent && iter->idx < i)
875 ent = find_next_entry_inc(iter);
877 iter->pos = *pos;
879 if (last_ent && !ent)
880 seq_puts(m, "\n\nvim:ft=help\n");
882 return ent;
885 static void *s_start(struct seq_file *m, loff_t *pos)
887 struct trace_iterator *iter = m->private;
888 void *p = NULL;
889 loff_t l = 0;
890 int i;
892 mutex_lock(&trace_types_lock);
894 if (!current_trace || current_trace != iter->trace)
895 return NULL;
897 atomic_inc(&trace_record_cmdline_disabled);
899 /* let the tracer grab locks here if needed */
900 if (current_trace->start)
901 current_trace->start(iter);
903 if (*pos != iter->pos) {
904 iter->ent = NULL;
905 iter->cpu = 0;
906 iter->idx = -1;
907 iter->prev_ent = NULL;
908 iter->prev_cpu = -1;
910 for_each_possible_cpu(i) {
911 iter->next_idx[i] = 0;
912 iter->next_page[i] = NULL;
915 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
918 } else {
919 l = *pos - 1;
920 p = s_next(m, p, &l);
923 return p;
926 static void s_stop(struct seq_file *m, void *p)
928 struct trace_iterator *iter = m->private;
930 atomic_dec(&trace_record_cmdline_disabled);
932 /* let the tracer release locks here if needed */
933 if (current_trace && current_trace == iter->trace && iter->trace->stop)
934 iter->trace->stop(iter);
936 mutex_unlock(&trace_types_lock);
939 static int
940 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
942 #ifdef CONFIG_KALLSYMS
943 char str[KSYM_SYMBOL_LEN];
945 kallsyms_lookup(address, NULL, NULL, NULL, str);
947 return trace_seq_printf(s, fmt, str);
948 #endif
949 return 1;
952 static int
953 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
954 unsigned long address)
956 #ifdef CONFIG_KALLSYMS
957 char str[KSYM_SYMBOL_LEN];
959 sprint_symbol(str, address);
960 return trace_seq_printf(s, fmt, str);
961 #endif
962 return 1;
965 #ifndef CONFIG_64BIT
966 # define IP_FMT "%08lx"
967 #else
968 # define IP_FMT "%016lx"
969 #endif
971 static notrace int
972 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
974 int ret;
976 if (!ip)
977 return trace_seq_printf(s, "0");
979 if (sym_flags & TRACE_ITER_SYM_OFFSET)
980 ret = seq_print_sym_offset(s, "%s", ip);
981 else
982 ret = seq_print_sym_short(s, "%s", ip);
984 if (!ret)
985 return 0;
987 if (sym_flags & TRACE_ITER_SYM_ADDR)
988 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
989 return ret;
992 static notrace void print_lat_help_header(struct seq_file *m)
994 seq_puts(m, "# _------=> CPU# \n");
995 seq_puts(m, "# / _-----=> irqs-off \n");
996 seq_puts(m, "# | / _----=> need-resched \n");
997 seq_puts(m, "# || / _---=> hardirq/softirq \n");
998 seq_puts(m, "# ||| / _--=> preempt-depth \n");
999 seq_puts(m, "# |||| / \n");
1000 seq_puts(m, "# ||||| delay \n");
1001 seq_puts(m, "# cmd pid ||||| time | caller \n");
1002 seq_puts(m, "# \\ / ||||| \\ | / \n");
1005 static notrace void print_func_help_header(struct seq_file *m)
1007 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1008 seq_puts(m, "# | | | | |\n");
1012 static notrace void
1013 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1015 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1016 struct trace_array *tr = iter->tr;
1017 struct trace_array_cpu *data = tr->data[tr->cpu];
1018 struct tracer *type = current_trace;
1019 unsigned long total = 0;
1020 unsigned long entries = 0;
1021 int cpu;
1022 const char *name = "preemption";
1024 if (type)
1025 name = type->name;
1027 for_each_possible_cpu(cpu) {
1028 if (head_page(tr->data[cpu])) {
1029 total += tr->data[cpu]->trace_idx;
1030 if (tr->data[cpu]->trace_idx > tr->entries)
1031 entries += tr->entries;
1032 else
1033 entries += tr->data[cpu]->trace_idx;
1037 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1038 name, UTS_RELEASE);
1039 seq_puts(m, "-----------------------------------"
1040 "---------------------------------\n");
1041 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1042 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1043 nsecs_to_usecs(data->saved_latency),
1044 entries,
1045 total,
1046 tr->cpu,
1047 #if defined(CONFIG_PREEMPT_NONE)
1048 "server",
1049 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1050 "desktop",
1051 #elif defined(CONFIG_PREEMPT_DESKTOP)
1052 "preempt",
1053 #else
1054 "unknown",
1055 #endif
1056 /* These are reserved for later use */
1057 0, 0, 0, 0);
1058 #ifdef CONFIG_SMP
1059 seq_printf(m, " #P:%d)\n", num_online_cpus());
1060 #else
1061 seq_puts(m, ")\n");
1062 #endif
1063 seq_puts(m, " -----------------\n");
1064 seq_printf(m, " | task: %.16s-%d "
1065 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1066 data->comm, data->pid, data->uid, data->nice,
1067 data->policy, data->rt_priority);
1068 seq_puts(m, " -----------------\n");
1070 if (data->critical_start) {
1071 seq_puts(m, " => started at: ");
1072 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1073 trace_print_seq(m, &iter->seq);
1074 seq_puts(m, "\n => ended at: ");
1075 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1076 trace_print_seq(m, &iter->seq);
1077 seq_puts(m, "\n");
1080 seq_puts(m, "\n");
1083 static notrace void
1084 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1086 int hardirq, softirq;
1087 char *comm;
1089 comm = trace_find_cmdline(entry->pid);
1091 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1092 trace_seq_printf(s, "%d", cpu);
1093 trace_seq_printf(s, "%c%c",
1094 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1095 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1097 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1098 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1099 if (hardirq && softirq)
1100 trace_seq_putc(s, 'H');
1101 else {
1102 if (hardirq)
1103 trace_seq_putc(s, 'h');
1104 else {
1105 if (softirq)
1106 trace_seq_putc(s, 's');
1107 else
1108 trace_seq_putc(s, '.');
1112 if (entry->preempt_count)
1113 trace_seq_printf(s, "%x", entry->preempt_count);
1114 else
1115 trace_seq_puts(s, ".");
1118 unsigned long preempt_mark_thresh = 100;
1120 static notrace void
1121 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1122 unsigned long rel_usecs)
1124 trace_seq_printf(s, " %4lldus", abs_usecs);
1125 if (rel_usecs > preempt_mark_thresh)
1126 trace_seq_puts(s, "!: ");
1127 else if (rel_usecs > 1)
1128 trace_seq_puts(s, "+: ");
1129 else
1130 trace_seq_puts(s, " : ");
1133 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1135 static notrace int
1136 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1138 struct trace_seq *s = &iter->seq;
1139 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1140 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1141 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1142 struct trace_entry *entry = iter->ent;
1143 unsigned long abs_usecs;
1144 unsigned long rel_usecs;
1145 char *comm;
1146 int S;
1148 if (!next_entry)
1149 next_entry = entry;
1150 rel_usecs = ns2usecs(next_entry->t - entry->t);
1151 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1153 if (verbose) {
1154 comm = trace_find_cmdline(entry->pid);
1155 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1156 " %ld.%03ldms (+%ld.%03ldms): ",
1157 comm,
1158 entry->pid, cpu, entry->flags,
1159 entry->preempt_count, trace_idx,
1160 ns2usecs(entry->t),
1161 abs_usecs/1000,
1162 abs_usecs % 1000, rel_usecs/1000,
1163 rel_usecs % 1000);
1164 } else {
1165 lat_print_generic(s, entry, cpu);
1166 lat_print_timestamp(s, abs_usecs, rel_usecs);
1168 switch (entry->type) {
1169 case TRACE_FN:
1170 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1171 trace_seq_puts(s, " (");
1172 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1173 trace_seq_puts(s, ")\n");
1174 break;
1175 case TRACE_CTX:
1176 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1177 state_to_char[entry->ctx.prev_state] : 'X';
1178 comm = trace_find_cmdline(entry->ctx.next_pid);
1179 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1180 entry->ctx.prev_pid,
1181 entry->ctx.prev_prio,
1183 entry->ctx.next_pid,
1184 entry->ctx.next_prio,
1185 comm);
1186 break;
1187 case TRACE_SPECIAL:
1188 trace_seq_printf(s, " %lx %lx %lx\n",
1189 entry->special.arg1,
1190 entry->special.arg2,
1191 entry->special.arg3);
1192 break;
1193 default:
1194 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1196 return 1;
1199 static notrace int print_trace_fmt(struct trace_iterator *iter)
1201 struct trace_seq *s = &iter->seq;
1202 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1203 struct trace_entry *entry;
1204 unsigned long usec_rem;
1205 unsigned long long t;
1206 unsigned long secs;
1207 char *comm;
1208 int S;
1209 int ret;
1211 entry = iter->ent;
1213 comm = trace_find_cmdline(iter->ent->pid);
1215 t = ns2usecs(entry->t);
1216 usec_rem = do_div(t, 1000000ULL);
1217 secs = (unsigned long)t;
1219 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1220 if (!ret)
1221 return 0;
1222 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1223 if (!ret)
1224 return 0;
1225 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1226 if (!ret)
1227 return 0;
1229 switch (entry->type) {
1230 case TRACE_FN:
1231 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1232 if (!ret)
1233 return 0;
1234 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1235 entry->fn.parent_ip) {
1236 ret = trace_seq_printf(s, " <-");
1237 if (!ret)
1238 return 0;
1239 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1240 sym_flags);
1241 if (!ret)
1242 return 0;
1244 ret = trace_seq_printf(s, "\n");
1245 if (!ret)
1246 return 0;
1247 break;
1248 case TRACE_CTX:
1249 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1250 state_to_char[entry->ctx.prev_state] : 'X';
1251 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1252 entry->ctx.prev_pid,
1253 entry->ctx.prev_prio,
1255 entry->ctx.next_pid,
1256 entry->ctx.next_prio);
1257 if (!ret)
1258 return 0;
1259 break;
1260 case TRACE_SPECIAL:
1261 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1262 entry->special.arg1,
1263 entry->special.arg2,
1264 entry->special.arg3);
1265 if (!ret)
1266 return 0;
1267 break;
1269 return 1;
1272 static notrace int print_raw_fmt(struct trace_iterator *iter)
1274 struct trace_seq *s = &iter->seq;
1275 struct trace_entry *entry;
1276 int ret;
1277 int S;
1279 entry = iter->ent;
1281 ret = trace_seq_printf(s, "%d %d %llu ",
1282 entry->pid, iter->cpu, entry->t);
1283 if (!ret)
1284 return 0;
1286 switch (entry->type) {
1287 case TRACE_FN:
1288 ret = trace_seq_printf(s, "%x %x\n",
1289 entry->fn.ip, entry->fn.parent_ip);
1290 if (!ret)
1291 return 0;
1292 break;
1293 case TRACE_CTX:
1294 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1295 state_to_char[entry->ctx.prev_state] : 'X';
1296 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1297 entry->ctx.prev_pid,
1298 entry->ctx.prev_prio,
1300 entry->ctx.next_pid,
1301 entry->ctx.next_prio);
1302 if (!ret)
1303 return 0;
1304 break;
1305 case TRACE_SPECIAL:
1306 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1307 entry->special.arg1,
1308 entry->special.arg2,
1309 entry->special.arg3);
1310 if (!ret)
1311 return 0;
1312 break;
1314 return 1;
1317 #define SEQ_PUT_FIELD_RET(s, x) \
1318 do { \
1319 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1320 return 0; \
1321 } while (0)
1323 static notrace int print_bin_fmt(struct trace_iterator *iter)
1325 struct trace_seq *s = &iter->seq;
1326 struct trace_entry *entry;
1328 entry = iter->ent;
1330 SEQ_PUT_FIELD_RET(s, entry->pid);
1331 SEQ_PUT_FIELD_RET(s, entry->cpu);
1332 SEQ_PUT_FIELD_RET(s, entry->t);
1334 switch (entry->type) {
1335 case TRACE_FN:
1336 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1337 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1338 break;
1339 case TRACE_CTX:
1340 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1341 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1342 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1343 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1344 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1345 break;
1346 case TRACE_SPECIAL:
1347 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1348 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1349 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1350 break;
1352 return 1;
1355 static int trace_empty(struct trace_iterator *iter)
1357 struct trace_array_cpu *data;
1358 int cpu;
1360 for_each_possible_cpu(cpu) {
1361 data = iter->tr->data[cpu];
1363 if (head_page(data) && data->trace_idx &&
1364 (data->trace_tail != data->trace_head ||
1365 data->trace_tail_idx != data->trace_head_idx))
1366 return 0;
1368 return 1;
1371 static int print_trace_line(struct trace_iterator *iter)
1373 if (trace_flags & TRACE_ITER_BIN)
1374 return print_bin_fmt(iter);
1376 if (trace_flags & TRACE_ITER_RAW)
1377 return print_raw_fmt(iter);
1379 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1380 return print_lat_fmt(iter, iter->idx, iter->cpu);
1382 return print_trace_fmt(iter);
1385 static int s_show(struct seq_file *m, void *v)
1387 struct trace_iterator *iter = v;
1389 if (iter->ent == NULL) {
1390 if (iter->tr) {
1391 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1392 seq_puts(m, "#\n");
1394 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1395 /* print nothing if the buffers are empty */
1396 if (trace_empty(iter))
1397 return 0;
1398 print_trace_header(m, iter);
1399 if (!(trace_flags & TRACE_ITER_VERBOSE))
1400 print_lat_help_header(m);
1401 } else {
1402 if (!(trace_flags & TRACE_ITER_VERBOSE))
1403 print_func_help_header(m);
1405 } else {
1406 print_trace_line(iter);
1407 trace_print_seq(m, &iter->seq);
1410 return 0;
1413 static struct seq_operations tracer_seq_ops = {
1414 .start = s_start,
1415 .next = s_next,
1416 .stop = s_stop,
1417 .show = s_show,
1420 static struct trace_iterator notrace *
1421 __tracing_open(struct inode *inode, struct file *file, int *ret)
1423 struct trace_iterator *iter;
1425 if (tracing_disabled) {
1426 *ret = -ENODEV;
1427 return NULL;
1430 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1431 if (!iter) {
1432 *ret = -ENOMEM;
1433 goto out;
1436 mutex_lock(&trace_types_lock);
1437 if (current_trace && current_trace->print_max)
1438 iter->tr = &max_tr;
1439 else
1440 iter->tr = inode->i_private;
1441 iter->trace = current_trace;
1442 iter->pos = -1;
1444 /* TODO stop tracer */
1445 *ret = seq_open(file, &tracer_seq_ops);
1446 if (!*ret) {
1447 struct seq_file *m = file->private_data;
1448 m->private = iter;
1450 /* stop the trace while dumping */
1451 if (iter->tr->ctrl)
1452 tracer_enabled = 0;
1454 if (iter->trace && iter->trace->open)
1455 iter->trace->open(iter);
1456 } else {
1457 kfree(iter);
1458 iter = NULL;
1460 mutex_unlock(&trace_types_lock);
1462 out:
1463 return iter;
1466 int tracing_open_generic(struct inode *inode, struct file *filp)
1468 if (tracing_disabled)
1469 return -ENODEV;
1471 filp->private_data = inode->i_private;
1472 return 0;
1475 int tracing_release(struct inode *inode, struct file *file)
1477 struct seq_file *m = (struct seq_file *)file->private_data;
1478 struct trace_iterator *iter = m->private;
1480 mutex_lock(&trace_types_lock);
1481 if (iter->trace && iter->trace->close)
1482 iter->trace->close(iter);
1484 /* reenable tracing if it was previously enabled */
1485 if (iter->tr->ctrl)
1486 tracer_enabled = 1;
1487 mutex_unlock(&trace_types_lock);
1489 seq_release(inode, file);
1490 kfree(iter);
1491 return 0;
1494 static int tracing_open(struct inode *inode, struct file *file)
1496 int ret;
1498 __tracing_open(inode, file, &ret);
1500 return ret;
1503 static int tracing_lt_open(struct inode *inode, struct file *file)
1505 struct trace_iterator *iter;
1506 int ret;
1508 iter = __tracing_open(inode, file, &ret);
1510 if (!ret)
1511 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1513 return ret;
1517 static notrace void *
1518 t_next(struct seq_file *m, void *v, loff_t *pos)
1520 struct tracer *t = m->private;
1522 (*pos)++;
1524 if (t)
1525 t = t->next;
1527 m->private = t;
1529 return t;
1532 static void *t_start(struct seq_file *m, loff_t *pos)
1534 struct tracer *t = m->private;
1535 loff_t l = 0;
1537 mutex_lock(&trace_types_lock);
1538 for (; t && l < *pos; t = t_next(m, t, &l))
1541 return t;
1544 static void t_stop(struct seq_file *m, void *p)
1546 mutex_unlock(&trace_types_lock);
1549 static int t_show(struct seq_file *m, void *v)
1551 struct tracer *t = v;
1553 if (!t)
1554 return 0;
1556 seq_printf(m, "%s", t->name);
1557 if (t->next)
1558 seq_putc(m, ' ');
1559 else
1560 seq_putc(m, '\n');
1562 return 0;
1565 static struct seq_operations show_traces_seq_ops = {
1566 .start = t_start,
1567 .next = t_next,
1568 .stop = t_stop,
1569 .show = t_show,
1572 static int show_traces_open(struct inode *inode, struct file *file)
1574 int ret;
1576 if (tracing_disabled)
1577 return -ENODEV;
1579 ret = seq_open(file, &show_traces_seq_ops);
1580 if (!ret) {
1581 struct seq_file *m = file->private_data;
1582 m->private = trace_types;
1585 return ret;
1588 static struct file_operations tracing_fops = {
1589 .open = tracing_open,
1590 .read = seq_read,
1591 .llseek = seq_lseek,
1592 .release = tracing_release,
1595 static struct file_operations tracing_lt_fops = {
1596 .open = tracing_lt_open,
1597 .read = seq_read,
1598 .llseek = seq_lseek,
1599 .release = tracing_release,
1602 static struct file_operations show_traces_fops = {
1603 .open = show_traces_open,
1604 .read = seq_read,
1605 .release = seq_release,
1608 static ssize_t
1609 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1610 size_t cnt, loff_t *ppos)
1612 char *buf;
1613 int r = 0;
1614 int len = 0;
1615 int i;
1617 /* calulate max size */
1618 for (i = 0; trace_options[i]; i++) {
1619 len += strlen(trace_options[i]);
1620 len += 3; /* "no" and space */
1623 /* +2 for \n and \0 */
1624 buf = kmalloc(len + 2, GFP_KERNEL);
1625 if (!buf)
1626 return -ENOMEM;
1628 for (i = 0; trace_options[i]; i++) {
1629 if (trace_flags & (1 << i))
1630 r += sprintf(buf + r, "%s ", trace_options[i]);
1631 else
1632 r += sprintf(buf + r, "no%s ", trace_options[i]);
1635 r += sprintf(buf + r, "\n");
1636 WARN_ON(r >= len + 2);
1638 r = simple_read_from_buffer(ubuf, cnt, ppos,
1639 buf, r);
1641 kfree(buf);
1643 return r;
1646 static ssize_t
1647 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1648 size_t cnt, loff_t *ppos)
1650 char buf[64];
1651 char *cmp = buf;
1652 int neg = 0;
1653 int i;
1655 if (cnt > 63)
1656 cnt = 63;
1658 if (copy_from_user(&buf, ubuf, cnt))
1659 return -EFAULT;
1661 buf[cnt] = 0;
1663 if (strncmp(buf, "no", 2) == 0) {
1664 neg = 1;
1665 cmp += 2;
1668 for (i = 0; trace_options[i]; i++) {
1669 int len = strlen(trace_options[i]);
1671 if (strncmp(cmp, trace_options[i], len) == 0) {
1672 if (neg)
1673 trace_flags &= ~(1 << i);
1674 else
1675 trace_flags |= (1 << i);
1676 break;
1680 filp->f_pos += cnt;
1682 return cnt;
1685 static struct file_operations tracing_iter_fops = {
1686 .open = tracing_open_generic,
1687 .read = tracing_iter_ctrl_read,
1688 .write = tracing_iter_ctrl_write,
1691 static const char readme_msg[] =
1692 "tracing mini-HOWTO:\n\n"
1693 "# mkdir /debug\n"
1694 "# mount -t debugfs nodev /debug\n\n"
1695 "# cat /debug/tracing/available_tracers\n"
1696 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1697 "# cat /debug/tracing/current_tracer\n"
1698 "none\n"
1699 "# echo sched_switch > /debug/tracing/current_tracer\n"
1700 "# cat /debug/tracing/current_tracer\n"
1701 "sched_switch\n"
1702 "# cat /debug/tracing/iter_ctrl\n"
1703 "noprint-parent nosym-offset nosym-addr noverbose\n"
1704 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1705 "# echo 1 > /debug/tracing/tracing_enabled\n"
1706 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1707 "echo 0 > /debug/tracing/tracing_enabled\n"
1710 static ssize_t
1711 tracing_readme_read(struct file *filp, char __user *ubuf,
1712 size_t cnt, loff_t *ppos)
1714 return simple_read_from_buffer(ubuf, cnt, ppos,
1715 readme_msg, strlen(readme_msg));
1718 static struct file_operations tracing_readme_fops = {
1719 .open = tracing_open_generic,
1720 .read = tracing_readme_read,
1724 static ssize_t
1725 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1726 size_t cnt, loff_t *ppos)
1728 struct trace_array *tr = filp->private_data;
1729 char buf[64];
1730 int r;
1732 r = sprintf(buf, "%ld\n", tr->ctrl);
1733 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1736 static ssize_t
1737 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1738 size_t cnt, loff_t *ppos)
1740 struct trace_array *tr = filp->private_data;
1741 long val;
1742 char buf[64];
1744 if (cnt > 63)
1745 cnt = 63;
1747 if (copy_from_user(&buf, ubuf, cnt))
1748 return -EFAULT;
1750 buf[cnt] = 0;
1752 val = simple_strtoul(buf, NULL, 10);
1754 val = !!val;
1756 mutex_lock(&trace_types_lock);
1757 if (tr->ctrl ^ val) {
1758 if (val)
1759 tracer_enabled = 1;
1760 else
1761 tracer_enabled = 0;
1763 tr->ctrl = val;
1765 if (current_trace && current_trace->ctrl_update)
1766 current_trace->ctrl_update(tr);
1768 mutex_unlock(&trace_types_lock);
1770 filp->f_pos += cnt;
1772 return cnt;
1775 static ssize_t
1776 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1777 size_t cnt, loff_t *ppos)
1779 char buf[max_tracer_type_len+2];
1780 int r;
1782 mutex_lock(&trace_types_lock);
1783 if (current_trace)
1784 r = sprintf(buf, "%s\n", current_trace->name);
1785 else
1786 r = sprintf(buf, "\n");
1787 mutex_unlock(&trace_types_lock);
1789 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1792 static ssize_t
1793 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1794 size_t cnt, loff_t *ppos)
1796 struct trace_array *tr = &global_trace;
1797 struct tracer *t;
1798 char buf[max_tracer_type_len+1];
1799 int i;
1801 if (cnt > max_tracer_type_len)
1802 cnt = max_tracer_type_len;
1804 if (copy_from_user(&buf, ubuf, cnt))
1805 return -EFAULT;
1807 buf[cnt] = 0;
1809 /* strip ending whitespace. */
1810 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1811 buf[i] = 0;
1813 mutex_lock(&trace_types_lock);
1814 for (t = trace_types; t; t = t->next) {
1815 if (strcmp(t->name, buf) == 0)
1816 break;
1818 if (!t || t == current_trace)
1819 goto out;
1821 if (current_trace && current_trace->reset)
1822 current_trace->reset(tr);
1824 current_trace = t;
1825 if (t->init)
1826 t->init(tr);
1828 out:
1829 mutex_unlock(&trace_types_lock);
1831 filp->f_pos += cnt;
1833 return cnt;
1836 static ssize_t
1837 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1838 size_t cnt, loff_t *ppos)
1840 unsigned long *ptr = filp->private_data;
1841 char buf[64];
1842 int r;
1844 r = snprintf(buf, 64, "%ld\n",
1845 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1846 if (r > 64)
1847 r = 64;
1848 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1851 static ssize_t
1852 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1853 size_t cnt, loff_t *ppos)
1855 long *ptr = filp->private_data;
1856 long val;
1857 char buf[64];
1859 if (cnt > 63)
1860 cnt = 63;
1862 if (copy_from_user(&buf, ubuf, cnt))
1863 return -EFAULT;
1865 buf[cnt] = 0;
1867 val = simple_strtoul(buf, NULL, 10);
1869 *ptr = val * 1000;
1871 return cnt;
1874 static atomic_t tracing_reader;
1876 static int tracing_open_pipe(struct inode *inode, struct file *filp)
1878 struct trace_iterator *iter;
1880 if (tracing_disabled)
1881 return -ENODEV;
1883 /* We only allow for reader of the pipe */
1884 if (atomic_inc_return(&tracing_reader) != 1) {
1885 atomic_dec(&tracing_reader);
1886 return -EBUSY;
1889 /* create a buffer to store the information to pass to userspace */
1890 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1891 if (!iter)
1892 return -ENOMEM;
1894 iter->tr = &global_trace;
1896 filp->private_data = iter;
1898 return 0;
1901 static int tracing_release_pipe(struct inode *inode, struct file *file)
1903 struct trace_iterator *iter = file->private_data;
1905 kfree(iter);
1906 atomic_dec(&tracing_reader);
1908 return 0;
1912 * Consumer reader.
1914 static ssize_t
1915 tracing_read_pipe(struct file *filp, char __user *ubuf,
1916 size_t cnt, loff_t *ppos)
1918 struct trace_iterator *iter = filp->private_data;
1919 struct trace_array_cpu *data;
1920 static cpumask_t mask;
1921 struct trace_entry *entry;
1922 static int start;
1923 unsigned long flags;
1924 int read = 0;
1925 int cpu;
1926 int len;
1927 int ret;
1929 /* return any leftover data */
1930 if (iter->seq.len > start) {
1931 len = iter->seq.len - start;
1932 if (cnt > len)
1933 cnt = len;
1934 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1935 if (ret)
1936 cnt = -EFAULT;
1938 start += len;
1940 return cnt;
1943 trace_seq_reset(&iter->seq);
1944 start = 0;
1946 while (trace_empty(iter)) {
1948 * This is a make-shift waitqueue. The reason we don't use
1949 * an actual wait queue is because:
1950 * 1) we only ever have one waiter
1951 * 2) the tracing, traces all functions, we don't want
1952 * the overhead of calling wake_up and friends
1953 * (and tracing them too)
1954 * Anyway, this is really very primitive wakeup.
1956 set_current_state(TASK_INTERRUPTIBLE);
1957 iter->tr->waiter = current;
1959 /* sleep for one second, and try again. */
1960 schedule_timeout(HZ);
1962 iter->tr->waiter = NULL;
1964 if (signal_pending(current))
1965 return -EINTR;
1968 * We block until we read something and tracing is disabled.
1969 * We still block if tracing is disabled, but we have never
1970 * read anything. This allows a user to cat this file, and
1971 * then enable tracing. But after we have read something,
1972 * we give an EOF when tracing is again disabled.
1974 * iter->pos will be 0 if we haven't read anything.
1976 if (!tracer_enabled && iter->pos)
1977 break;
1979 continue;
1982 /* stop when tracing is finished */
1983 if (trace_empty(iter))
1984 return 0;
1986 if (cnt >= PAGE_SIZE)
1987 cnt = PAGE_SIZE - 1;
1989 memset(iter, 0, sizeof(*iter));
1990 iter->tr = &global_trace;
1991 iter->pos = -1;
1994 * We need to stop all tracing on all CPUS to read the
1995 * the next buffer. This is a bit expensive, but is
1996 * not done often. We fill all what we can read,
1997 * and then release the locks again.
2000 cpus_clear(mask);
2001 local_irq_save(flags);
2002 for_each_possible_cpu(cpu) {
2003 data = iter->tr->data[cpu];
2005 if (!head_page(data) || !data->trace_idx)
2006 continue;
2008 atomic_inc(&data->disabled);
2009 spin_lock(&data->lock);
2010 cpu_set(cpu, mask);
2013 while ((entry = find_next_entry_inc(iter)) != NULL) {
2014 ret = print_trace_line(iter);
2015 if (!ret)
2016 break;
2018 trace_consume(iter);
2020 if (iter->seq.len >= cnt)
2021 break;
2024 for_each_cpu_mask(cpu, mask) {
2025 data = iter->tr->data[cpu];
2026 spin_unlock(&data->lock);
2027 atomic_dec(&data->disabled);
2029 local_irq_restore(flags);
2031 /* Now copy what we have to the user */
2032 read = iter->seq.len;
2033 if (read > cnt)
2034 read = cnt;
2036 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2038 if (read < iter->seq.len)
2039 start = read;
2040 else
2041 trace_seq_reset(&iter->seq);
2043 if (ret)
2044 read = -EFAULT;
2046 return read;
2049 static struct file_operations tracing_max_lat_fops = {
2050 .open = tracing_open_generic,
2051 .read = tracing_max_lat_read,
2052 .write = tracing_max_lat_write,
2055 static struct file_operations tracing_ctrl_fops = {
2056 .open = tracing_open_generic,
2057 .read = tracing_ctrl_read,
2058 .write = tracing_ctrl_write,
2061 static struct file_operations set_tracer_fops = {
2062 .open = tracing_open_generic,
2063 .read = tracing_set_trace_read,
2064 .write = tracing_set_trace_write,
2067 static struct file_operations tracing_pipe_fops = {
2068 .open = tracing_open_pipe,
2069 .read = tracing_read_pipe,
2070 .release = tracing_release_pipe,
2073 #ifdef CONFIG_DYNAMIC_FTRACE
2075 static ssize_t
2076 tracing_read_long(struct file *filp, char __user *ubuf,
2077 size_t cnt, loff_t *ppos)
2079 unsigned long *p = filp->private_data;
2080 char buf[64];
2081 int r;
2083 r = sprintf(buf, "%ld\n", *p);
2085 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2088 static struct file_operations tracing_read_long_fops = {
2089 .open = tracing_open_generic,
2090 .read = tracing_read_long,
2092 #endif
2094 static struct dentry *d_tracer;
2096 struct dentry *tracing_init_dentry(void)
2098 static int once;
2100 if (d_tracer)
2101 return d_tracer;
2103 d_tracer = debugfs_create_dir("tracing", NULL);
2105 if (!d_tracer && !once) {
2106 once = 1;
2107 pr_warning("Could not create debugfs directory 'tracing'\n");
2108 return NULL;
2111 return d_tracer;
2114 #ifdef CONFIG_FTRACE_SELFTEST
2115 /* Let selftest have access to static functions in this file */
2116 #include "trace_selftest.c"
2117 #endif
2119 static __init void tracer_init_debugfs(void)
2121 struct dentry *d_tracer;
2122 struct dentry *entry;
2124 d_tracer = tracing_init_dentry();
2126 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2127 &global_trace, &tracing_ctrl_fops);
2128 if (!entry)
2129 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2131 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2132 NULL, &tracing_iter_fops);
2133 if (!entry)
2134 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2136 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2137 &global_trace, &tracing_lt_fops);
2138 if (!entry)
2139 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2141 entry = debugfs_create_file("trace", 0444, d_tracer,
2142 &global_trace, &tracing_fops);
2143 if (!entry)
2144 pr_warning("Could not create debugfs 'trace' entry\n");
2146 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2147 &global_trace, &show_traces_fops);
2148 if (!entry)
2149 pr_warning("Could not create debugfs 'trace' entry\n");
2151 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2152 &global_trace, &set_tracer_fops);
2153 if (!entry)
2154 pr_warning("Could not create debugfs 'trace' entry\n");
2156 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2157 &tracing_max_latency,
2158 &tracing_max_lat_fops);
2159 if (!entry)
2160 pr_warning("Could not create debugfs "
2161 "'tracing_max_latency' entry\n");
2163 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2164 &tracing_thresh, &tracing_max_lat_fops);
2165 if (!entry)
2166 pr_warning("Could not create debugfs "
2167 "'tracing_threash' entry\n");
2168 entry = debugfs_create_file("README", 0644, d_tracer,
2169 NULL, &tracing_readme_fops);
2170 if (!entry)
2171 pr_warning("Could not create debugfs 'README' entry\n");
2173 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2174 NULL, &tracing_pipe_fops);
2175 if (!entry)
2176 pr_warning("Could not create debugfs "
2177 "'tracing_threash' entry\n");
2179 #ifdef CONFIG_DYNAMIC_FTRACE
2180 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2181 &ftrace_update_tot_cnt,
2182 &tracing_read_long_fops);
2183 if (!entry)
2184 pr_warning("Could not create debugfs "
2185 "'dyn_ftrace_total_info' entry\n");
2186 #endif
2189 /* dummy trace to disable tracing */
2190 static struct tracer no_tracer __read_mostly =
2192 .name = "none",
2195 static int trace_alloc_page(void)
2197 struct trace_array_cpu *data;
2198 struct page *page, *tmp;
2199 LIST_HEAD(pages);
2200 void *array;
2201 int i;
2203 /* first allocate a page for each CPU */
2204 for_each_possible_cpu(i) {
2205 array = (void *)__get_free_page(GFP_KERNEL);
2206 if (array == NULL) {
2207 printk(KERN_ERR "tracer: failed to allocate page"
2208 "for trace buffer!\n");
2209 goto free_pages;
2212 page = virt_to_page(array);
2213 list_add(&page->lru, &pages);
2215 /* Only allocate if we are actually using the max trace */
2216 #ifdef CONFIG_TRACER_MAX_TRACE
2217 array = (void *)__get_free_page(GFP_KERNEL);
2218 if (array == NULL) {
2219 printk(KERN_ERR "tracer: failed to allocate page"
2220 "for trace buffer!\n");
2221 goto free_pages;
2223 page = virt_to_page(array);
2224 list_add(&page->lru, &pages);
2225 #endif
2228 /* Now that we successfully allocate a page per CPU, add them */
2229 for_each_possible_cpu(i) {
2230 data = global_trace.data[i];
2231 spin_lock_init(&data->lock);
2232 lockdep_set_class(&data->lock, &data->lock_key);
2233 page = list_entry(pages.next, struct page, lru);
2234 list_del_init(&page->lru);
2235 list_add_tail(&page->lru, &data->trace_pages);
2236 ClearPageLRU(page);
2238 #ifdef CONFIG_TRACER_MAX_TRACE
2239 data = max_tr.data[i];
2240 spin_lock_init(&data->lock);
2241 lockdep_set_class(&data->lock, &data->lock_key);
2242 page = list_entry(pages.next, struct page, lru);
2243 list_del_init(&page->lru);
2244 list_add_tail(&page->lru, &data->trace_pages);
2245 SetPageLRU(page);
2246 #endif
2248 global_trace.entries += ENTRIES_PER_PAGE;
2250 return 0;
2252 free_pages:
2253 list_for_each_entry_safe(page, tmp, &pages, lru) {
2254 list_del_init(&page->lru);
2255 __free_page(page);
2257 return -ENOMEM;
2260 __init static int tracer_alloc_buffers(void)
2262 struct trace_array_cpu *data;
2263 void *array;
2264 struct page *page;
2265 int pages = 0;
2266 int ret = -ENOMEM;
2267 int i;
2269 /* Allocate the first page for all buffers */
2270 for_each_possible_cpu(i) {
2271 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2272 max_tr.data[i] = &per_cpu(max_data, i);
2274 array = (void *)__get_free_page(GFP_KERNEL);
2275 if (array == NULL) {
2276 printk(KERN_ERR "tracer: failed to allocate page"
2277 "for trace buffer!\n");
2278 goto free_buffers;
2281 /* set the array to the list */
2282 INIT_LIST_HEAD(&data->trace_pages);
2283 page = virt_to_page(array);
2284 list_add(&page->lru, &data->trace_pages);
2285 /* use the LRU flag to differentiate the two buffers */
2286 ClearPageLRU(page);
2288 /* Only allocate if we are actually using the max trace */
2289 #ifdef CONFIG_TRACER_MAX_TRACE
2290 array = (void *)__get_free_page(GFP_KERNEL);
2291 if (array == NULL) {
2292 printk(KERN_ERR "tracer: failed to allocate page"
2293 "for trace buffer!\n");
2294 goto free_buffers;
2297 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2298 page = virt_to_page(array);
2299 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2300 SetPageLRU(page);
2301 #endif
2305 * Since we allocate by orders of pages, we may be able to
2306 * round up a bit.
2308 global_trace.entries = ENTRIES_PER_PAGE;
2309 pages++;
2311 while (global_trace.entries < trace_nr_entries) {
2312 if (trace_alloc_page())
2313 break;
2314 pages++;
2316 max_tr.entries = global_trace.entries;
2318 pr_info("tracer: %d pages allocated for %ld",
2319 pages, trace_nr_entries);
2320 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2321 pr_info(" actual entries %ld\n", global_trace.entries);
2323 tracer_init_debugfs();
2325 trace_init_cmdlines();
2327 register_tracer(&no_tracer);
2328 current_trace = &no_tracer;
2330 /* All seems OK, enable tracing */
2331 tracing_disabled = 0;
2333 return 0;
2335 free_buffers:
2336 for (i-- ; i >= 0; i--) {
2337 struct page *page, *tmp;
2338 struct trace_array_cpu *data = global_trace.data[i];
2340 if (data) {
2341 list_for_each_entry_safe(page, tmp,
2342 &data->trace_pages, lru) {
2343 list_del_init(&page->lru);
2344 __free_page(page);
2348 #ifdef CONFIG_TRACER_MAX_TRACE
2349 data = max_tr.data[i];
2350 if (data) {
2351 list_for_each_entry_safe(page, tmp,
2352 &data->trace_pages, lru) {
2353 list_del_init(&page->lru);
2354 __free_page(page);
2357 #endif
2359 return ret;
2361 fs_initcall(tracer_alloc_buffers);