ftrace: add raw output
[deliverable/linux.git] / kernel / trace / trace.c
1 /*
2 * ring buffer based function tracer
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
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
13 */
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>
29
30 #include "trace.h"
31
32 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
33 unsigned long __read_mostly tracing_thresh;
34
35 static int tracing_disabled = 1;
36
37 static long notrace
38 ns2usecs(cycle_t nsec)
39 {
40 nsec += 500;
41 do_div(nsec, 1000);
42 return nsec;
43 }
44
45 static const int time_sync_freq_max = 128;
46 static const cycle_t time_sync_thresh = 100000;
47
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);
52
53 /*
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:
58 */
59 static DEFINE_SPINLOCK(time_sync_lock);
60 static cycle_t prev_global_time;
61
62 static notrace cycle_t __ftrace_now_sync(cycles_t time, int cpu)
63 {
64 unsigned long flags;
65
66 spin_lock_irqsave(&time_sync_lock, flags);
67
68 /*
69 * Update the synchronization frequency:
70 */
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);
74
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;
80 }
81
82 spin_unlock_irqrestore(&time_sync_lock, flags);
83
84 return time;
85 }
86
87 notrace cycle_t ftrace_now(int cpu)
88 {
89 cycle_t prev_cpu_time, time, delta_time;
90
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;
94
95 if (unlikely(delta_time > time_sync_thresh ||
96 --per_cpu(time_sync_count, cpu) <= 0))
97 time = __ftrace_now_sync(time, cpu);
98
99 return time;
100 }
101
102 static struct trace_array global_trace;
103
104 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
105
106 static struct trace_array max_tr;
107
108 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
109
110 static int tracer_enabled;
111 static unsigned long trace_nr_entries = 16384UL;
112
113 static struct tracer *trace_types __read_mostly;
114 static struct tracer *current_trace __read_mostly;
115 static int max_tracer_type_len;
116
117 static DEFINE_MUTEX(trace_types_lock);
118
119 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
120
121 static int __init set_nr_entries(char *str)
122 {
123 if (!str)
124 return 0;
125 trace_nr_entries = simple_strtoul(str, &str, 0);
126 return 1;
127 }
128 __setup("trace_entries=", set_nr_entries);
129
130 unsigned long nsecs_to_usecs(unsigned long nsecs)
131 {
132 return nsecs / 1000;
133 }
134
135 enum trace_type {
136 __TRACE_FIRST_TYPE = 0,
137
138 TRACE_FN,
139 TRACE_CTX,
140
141 __TRACE_LAST_TYPE
142 };
143
144 enum trace_flag_type {
145 TRACE_FLAG_IRQS_OFF = 0x01,
146 TRACE_FLAG_NEED_RESCHED = 0x02,
147 TRACE_FLAG_HARDIRQ = 0x04,
148 TRACE_FLAG_SOFTIRQ = 0x08,
149 };
150
151 enum trace_iterator_flags {
152 TRACE_ITER_PRINT_PARENT = 0x01,
153 TRACE_ITER_SYM_OFFSET = 0x02,
154 TRACE_ITER_SYM_ADDR = 0x04,
155 TRACE_ITER_VERBOSE = 0x08,
156 TRACE_ITER_RAW = 0x10,
157 };
158
159 #define TRACE_ITER_SYM_MASK \
160 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
161
162 /* These must match the bit postions above */
163 static const char *trace_options[] = {
164 "print-parent",
165 "sym-offset",
166 "sym-addr",
167 "verbose",
168 "raw",
169 NULL
170 };
171
172 static unsigned trace_flags;
173
174 static DEFINE_SPINLOCK(ftrace_max_lock);
175
176 /*
177 * Copy the new maximum trace into the separate maximum-trace
178 * structure. (this way the maximum trace is permanently saved,
179 * for later retrieval via /debugfs/tracing/latency_trace)
180 */
181 static notrace void
182 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
183 {
184 struct trace_array_cpu *data = tr->data[cpu];
185
186 max_tr.cpu = cpu;
187 max_tr.time_start = data->preempt_timestamp;
188
189 data = max_tr.data[cpu];
190 data->saved_latency = tracing_max_latency;
191
192 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
193 data->pid = tsk->pid;
194 data->uid = tsk->uid;
195 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
196 data->policy = tsk->policy;
197 data->rt_priority = tsk->rt_priority;
198
199 /* record this tasks comm */
200 tracing_record_cmdline(current);
201 }
202
203 void check_pages(struct trace_array_cpu *data)
204 {
205 struct page *page, *tmp;
206
207 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
208 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
209
210 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
211 BUG_ON(page->lru.next->prev != &page->lru);
212 BUG_ON(page->lru.prev->next != &page->lru);
213 }
214 }
215
216 void *head_page(struct trace_array_cpu *data)
217 {
218 struct page *page;
219
220 check_pages(data);
221 if (list_empty(&data->trace_pages))
222 return NULL;
223
224 page = list_entry(data->trace_pages.next, struct page, lru);
225 BUG_ON(&page->lru == &data->trace_pages);
226
227 return page_address(page);
228 }
229
230 static notrace int
231 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
232 {
233 int len = (PAGE_SIZE - 1) - s->len;
234 va_list ap;
235 int ret;
236
237 if (!len)
238 return 0;
239
240 va_start(ap, fmt);
241 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
242 va_end(ap);
243
244 /* If we can't write it all, don't bother writing anything */
245 if (ret > len)
246 return 0;
247
248 s->len += ret;
249
250 return len;
251 }
252
253 static notrace int
254 trace_seq_puts(struct trace_seq *s, const char *str)
255 {
256 int len = strlen(str);
257
258 if (len > ((PAGE_SIZE - 1) - s->len))
259 return 0;
260
261 memcpy(s->buffer + s->len, str, len);
262 s->len += len;
263
264 return len;
265 }
266
267 static notrace int
268 trace_seq_putc(struct trace_seq *s, unsigned char c)
269 {
270 if (s->len >= (PAGE_SIZE - 1))
271 return 0;
272
273 s->buffer[s->len++] = c;
274
275 return 1;
276 }
277
278 static notrace void
279 trace_seq_reset(struct trace_seq *s)
280 {
281 s->len = 0;
282 }
283
284 static notrace void
285 trace_print_seq(struct seq_file *m, struct trace_seq *s)
286 {
287 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
288
289 s->buffer[len] = 0;
290 seq_puts(m, s->buffer);
291
292 trace_seq_reset(s);
293 }
294
295 notrace static void
296 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
297 {
298 struct list_head flip_pages;
299
300 INIT_LIST_HEAD(&flip_pages);
301
302 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
303 sizeof(struct trace_array_cpu) -
304 offsetof(struct trace_array_cpu, trace_head_idx));
305
306 check_pages(tr1);
307 check_pages(tr2);
308 list_splice_init(&tr1->trace_pages, &flip_pages);
309 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
310 list_splice_init(&flip_pages, &tr2->trace_pages);
311 BUG_ON(!list_empty(&flip_pages));
312 check_pages(tr1);
313 check_pages(tr2);
314 }
315
316 notrace void
317 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
318 {
319 struct trace_array_cpu *data;
320 int i;
321
322 WARN_ON_ONCE(!irqs_disabled());
323 spin_lock(&ftrace_max_lock);
324 /* clear out all the previous traces */
325 for_each_possible_cpu(i) {
326 data = tr->data[i];
327 flip_trace(max_tr.data[i], data);
328 tracing_reset(data);
329 }
330
331 __update_max_tr(tr, tsk, cpu);
332 spin_unlock(&ftrace_max_lock);
333 }
334
335 /**
336 * update_max_tr_single - only copy one trace over, and reset the rest
337 * @tr - tracer
338 * @tsk - task with the latency
339 * @cpu - the cpu of the buffer to copy.
340 */
341 notrace void
342 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
343 {
344 struct trace_array_cpu *data = tr->data[cpu];
345 int i;
346
347 WARN_ON_ONCE(!irqs_disabled());
348 spin_lock(&ftrace_max_lock);
349 for_each_possible_cpu(i)
350 tracing_reset(max_tr.data[i]);
351
352 flip_trace(max_tr.data[cpu], data);
353 tracing_reset(data);
354
355 __update_max_tr(tr, tsk, cpu);
356 spin_unlock(&ftrace_max_lock);
357 }
358
359 int register_tracer(struct tracer *type)
360 {
361 struct tracer *t;
362 int len;
363 int ret = 0;
364
365 if (!type->name) {
366 pr_info("Tracer must have a name\n");
367 return -1;
368 }
369
370 mutex_lock(&trace_types_lock);
371 for (t = trace_types; t; t = t->next) {
372 if (strcmp(type->name, t->name) == 0) {
373 /* already found */
374 pr_info("Trace %s already registered\n",
375 type->name);
376 ret = -1;
377 goto out;
378 }
379 }
380
381 #ifdef CONFIG_FTRACE_STARTUP_TEST
382 if (type->selftest) {
383 struct tracer *saved_tracer = current_trace;
384 struct trace_array_cpu *data;
385 struct trace_array *tr = &global_trace;
386 int saved_ctrl = tr->ctrl;
387 int i;
388 /*
389 * Run a selftest on this tracer.
390 * Here we reset the trace buffer, and set the current
391 * tracer to be this tracer. The tracer can then run some
392 * internal tracing to verify that everything is in order.
393 * If we fail, we do not register this tracer.
394 */
395 for_each_possible_cpu(i) {
396 data = tr->data[i];
397 if (!head_page(data))
398 continue;
399 tracing_reset(data);
400 }
401 current_trace = type;
402 tr->ctrl = 0;
403 /* the test is responsible for initializing and enabling */
404 pr_info("Testing tracer %s: ", type->name);
405 ret = type->selftest(type, tr);
406 /* the test is responsible for resetting too */
407 current_trace = saved_tracer;
408 tr->ctrl = saved_ctrl;
409 if (ret) {
410 printk(KERN_CONT "FAILED!\n");
411 goto out;
412 }
413 /* Only reset on passing, to avoid touching corrupted buffers */
414 for_each_possible_cpu(i) {
415 data = tr->data[i];
416 if (!head_page(data))
417 continue;
418 tracing_reset(data);
419 }
420 printk(KERN_CONT "PASSED\n");
421 }
422 #endif
423
424 type->next = trace_types;
425 trace_types = type;
426 len = strlen(type->name);
427 if (len > max_tracer_type_len)
428 max_tracer_type_len = len;
429
430 out:
431 mutex_unlock(&trace_types_lock);
432
433 return ret;
434 }
435
436 void unregister_tracer(struct tracer *type)
437 {
438 struct tracer **t;
439 int len;
440
441 mutex_lock(&trace_types_lock);
442 for (t = &trace_types; *t; t = &(*t)->next) {
443 if (*t == type)
444 goto found;
445 }
446 pr_info("Trace %s not registered\n", type->name);
447 goto out;
448
449 found:
450 *t = (*t)->next;
451 if (strlen(type->name) != max_tracer_type_len)
452 goto out;
453
454 max_tracer_type_len = 0;
455 for (t = &trace_types; *t; t = &(*t)->next) {
456 len = strlen((*t)->name);
457 if (len > max_tracer_type_len)
458 max_tracer_type_len = len;
459 }
460 out:
461 mutex_unlock(&trace_types_lock);
462 }
463
464 notrace void tracing_reset(struct trace_array_cpu *data)
465 {
466 data->trace_idx = 0;
467 data->trace_head = data->trace_tail = head_page(data);
468 data->trace_head_idx = 0;
469 data->trace_tail_idx = 0;
470 }
471
472 #ifdef CONFIG_FTRACE
473 static notrace void
474 function_trace_call(unsigned long ip, unsigned long parent_ip)
475 {
476 struct trace_array *tr = &global_trace;
477 struct trace_array_cpu *data;
478 unsigned long flags;
479 long disabled;
480 int cpu;
481
482 if (unlikely(!tracer_enabled))
483 return;
484
485 local_irq_save(flags);
486 cpu = raw_smp_processor_id();
487 data = tr->data[cpu];
488 disabled = atomic_inc_return(&data->disabled);
489
490 if (likely(disabled == 1))
491 ftrace(tr, data, ip, parent_ip, flags);
492
493 atomic_dec(&data->disabled);
494 local_irq_restore(flags);
495 }
496
497 static struct ftrace_ops trace_ops __read_mostly =
498 {
499 .func = function_trace_call,
500 };
501 #endif
502
503 notrace void tracing_start_function_trace(void)
504 {
505 register_ftrace_function(&trace_ops);
506 }
507
508 notrace void tracing_stop_function_trace(void)
509 {
510 unregister_ftrace_function(&trace_ops);
511 }
512
513 #define SAVED_CMDLINES 128
514 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
515 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
516 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
517 static int cmdline_idx;
518 static DEFINE_SPINLOCK(trace_cmdline_lock);
519 atomic_t trace_record_cmdline_disabled;
520
521 static void trace_init_cmdlines(void)
522 {
523 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
524 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
525 cmdline_idx = 0;
526 }
527
528 notrace void trace_stop_cmdline_recording(void);
529
530 static notrace void trace_save_cmdline(struct task_struct *tsk)
531 {
532 unsigned map;
533 unsigned idx;
534
535 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
536 return;
537
538 /*
539 * It's not the end of the world if we don't get
540 * the lock, but we also don't want to spin
541 * nor do we want to disable interrupts,
542 * so if we miss here, then better luck next time.
543 */
544 if (!spin_trylock(&trace_cmdline_lock))
545 return;
546
547 idx = map_pid_to_cmdline[tsk->pid];
548 if (idx >= SAVED_CMDLINES) {
549 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
550
551 map = map_cmdline_to_pid[idx];
552 if (map <= PID_MAX_DEFAULT)
553 map_pid_to_cmdline[map] = (unsigned)-1;
554
555 map_pid_to_cmdline[tsk->pid] = idx;
556
557 cmdline_idx = idx;
558 }
559
560 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
561
562 spin_unlock(&trace_cmdline_lock);
563 }
564
565 static notrace char *trace_find_cmdline(int pid)
566 {
567 char *cmdline = "<...>";
568 unsigned map;
569
570 if (!pid)
571 return "<idle>";
572
573 if (pid > PID_MAX_DEFAULT)
574 goto out;
575
576 map = map_pid_to_cmdline[pid];
577 if (map >= SAVED_CMDLINES)
578 goto out;
579
580 cmdline = saved_cmdlines[map];
581
582 out:
583 return cmdline;
584 }
585
586 notrace void tracing_record_cmdline(struct task_struct *tsk)
587 {
588 if (atomic_read(&trace_record_cmdline_disabled))
589 return;
590
591 trace_save_cmdline(tsk);
592 }
593
594 static inline notrace struct list_head *
595 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
596 {
597 /*
598 * Roundrobin - but skip the head (which is not a real page):
599 */
600 next = next->next;
601 if (unlikely(next == &data->trace_pages))
602 next = next->next;
603 BUG_ON(next == &data->trace_pages);
604
605 return next;
606 }
607
608 static inline notrace void *
609 trace_next_page(struct trace_array_cpu *data, void *addr)
610 {
611 struct list_head *next;
612 struct page *page;
613
614 page = virt_to_page(addr);
615
616 next = trace_next_list(data, &page->lru);
617 page = list_entry(next, struct page, lru);
618
619 return page_address(page);
620 }
621
622 static inline notrace struct trace_entry *
623 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
624 {
625 unsigned long idx, idx_next;
626 struct trace_entry *entry;
627
628 data->trace_idx++;
629 idx = data->trace_head_idx;
630 idx_next = idx + 1;
631
632 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
633
634 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
635
636 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
637 data->trace_head = trace_next_page(data, data->trace_head);
638 idx_next = 0;
639 }
640
641 if (data->trace_head == data->trace_tail &&
642 idx_next == data->trace_tail_idx) {
643 /* overrun */
644 data->trace_tail_idx++;
645 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
646 data->trace_tail =
647 trace_next_page(data, data->trace_tail);
648 data->trace_tail_idx = 0;
649 }
650 }
651
652 data->trace_head_idx = idx_next;
653
654 return entry;
655 }
656
657 static inline notrace void
658 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
659 {
660 struct task_struct *tsk = current;
661 unsigned long pc;
662
663 pc = preempt_count();
664
665 entry->preempt_count = pc & 0xff;
666 entry->pid = tsk->pid;
667 entry->t = ftrace_now(raw_smp_processor_id());
668 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
669 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
670 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
671 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
672 }
673
674 notrace void
675 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
676 unsigned long ip, unsigned long parent_ip, unsigned long flags)
677 {
678 struct trace_entry *entry;
679
680 spin_lock(&data->lock);
681 entry = tracing_get_trace_entry(tr, data);
682 tracing_generic_entry_update(entry, flags);
683 entry->type = TRACE_FN;
684 entry->fn.ip = ip;
685 entry->fn.parent_ip = parent_ip;
686 spin_unlock(&data->lock);
687 }
688
689 notrace void
690 tracing_sched_switch_trace(struct trace_array *tr,
691 struct trace_array_cpu *data,
692 struct task_struct *prev, struct task_struct *next,
693 unsigned long flags)
694 {
695 struct trace_entry *entry;
696
697 spin_lock(&data->lock);
698 entry = tracing_get_trace_entry(tr, data);
699 tracing_generic_entry_update(entry, flags);
700 entry->type = TRACE_CTX;
701 entry->ctx.prev_pid = prev->pid;
702 entry->ctx.prev_prio = prev->prio;
703 entry->ctx.prev_state = prev->state;
704 entry->ctx.next_pid = next->pid;
705 entry->ctx.next_prio = next->prio;
706 spin_unlock(&data->lock);
707 }
708
709 enum trace_file_type {
710 TRACE_FILE_LAT_FMT = 1,
711 };
712
713 static struct trace_entry *
714 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
715 struct trace_iterator *iter, int cpu)
716 {
717 struct page *page;
718 struct trace_entry *array;
719
720 if (iter->next_idx[cpu] >= tr->entries ||
721 iter->next_idx[cpu] >= data->trace_idx ||
722 (data->trace_head == data->trace_tail &&
723 data->trace_head_idx == data->trace_tail_idx))
724 return NULL;
725
726 if (!iter->next_page[cpu]) {
727 /* Initialize the iterator for this cpu trace buffer */
728 WARN_ON(!data->trace_tail);
729 page = virt_to_page(data->trace_tail);
730 iter->next_page[cpu] = &page->lru;
731 iter->next_page_idx[cpu] = data->trace_tail_idx;
732 }
733
734 page = list_entry(iter->next_page[cpu], struct page, lru);
735 BUG_ON(&data->trace_pages == &page->lru);
736
737 array = page_address(page);
738
739 /* Still possible to catch up to the tail */
740 if (iter->next_idx[cpu] && array == data->trace_tail &&
741 iter->next_page_idx[cpu] == data->trace_tail_idx)
742 return NULL;
743
744 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
745 return &array[iter->next_page_idx[cpu]];
746 }
747
748 static struct notrace trace_entry *
749 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
750 {
751 struct trace_array *tr = iter->tr;
752 struct trace_entry *ent, *next = NULL;
753 int next_cpu = -1;
754 int cpu;
755
756 for_each_possible_cpu(cpu) {
757 if (!head_page(tr->data[cpu]))
758 continue;
759 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
760 /*
761 * Pick the entry with the smallest timestamp:
762 */
763 if (ent && (!next || ent->t < next->t)) {
764 next = ent;
765 next_cpu = cpu;
766 }
767 }
768
769 if (ent_cpu)
770 *ent_cpu = next_cpu;
771
772 return next;
773 }
774
775 static notrace void trace_iterator_increment(struct trace_iterator *iter)
776 {
777 iter->idx++;
778 iter->next_idx[iter->cpu]++;
779 iter->next_page_idx[iter->cpu]++;
780
781 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
782 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
783
784 iter->next_page_idx[iter->cpu] = 0;
785 iter->next_page[iter->cpu] =
786 trace_next_list(data, iter->next_page[iter->cpu]);
787 }
788 }
789
790 static notrace void trace_consume(struct trace_iterator *iter)
791 {
792 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
793
794 data->trace_tail_idx++;
795 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
796 data->trace_tail = trace_next_page(data, data->trace_tail);
797 data->trace_tail_idx = 0;
798 }
799
800 /* Check if we empty it, then reset the index */
801 if (data->trace_head == data->trace_tail &&
802 data->trace_head_idx == data->trace_tail_idx)
803 data->trace_idx = 0;
804 }
805
806 static notrace void *find_next_entry_inc(struct trace_iterator *iter)
807 {
808 struct trace_entry *next;
809 int next_cpu = -1;
810
811 next = find_next_entry(iter, &next_cpu);
812
813 iter->prev_ent = iter->ent;
814 iter->prev_cpu = iter->cpu;
815
816 iter->ent = next;
817 iter->cpu = next_cpu;
818
819 if (next)
820 trace_iterator_increment(iter);
821
822 return next ? iter : NULL;
823 }
824
825 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
826 {
827 struct trace_iterator *iter = m->private;
828 void *last_ent = iter->ent;
829 int i = (int)*pos;
830 void *ent;
831
832 (*pos)++;
833
834 /* can't go backwards */
835 if (iter->idx > i)
836 return NULL;
837
838 if (iter->idx < 0)
839 ent = find_next_entry_inc(iter);
840 else
841 ent = iter;
842
843 while (ent && iter->idx < i)
844 ent = find_next_entry_inc(iter);
845
846 iter->pos = *pos;
847
848 if (last_ent && !ent)
849 seq_puts(m, "\n\nvim:ft=help\n");
850
851 return ent;
852 }
853
854 static void *s_start(struct seq_file *m, loff_t *pos)
855 {
856 struct trace_iterator *iter = m->private;
857 void *p = NULL;
858 loff_t l = 0;
859 int i;
860
861 mutex_lock(&trace_types_lock);
862
863 if (!current_trace || current_trace != iter->trace)
864 return NULL;
865
866 atomic_inc(&trace_record_cmdline_disabled);
867
868 /* let the tracer grab locks here if needed */
869 if (current_trace->start)
870 current_trace->start(iter);
871
872 if (*pos != iter->pos) {
873 iter->ent = NULL;
874 iter->cpu = 0;
875 iter->idx = -1;
876 iter->prev_ent = NULL;
877 iter->prev_cpu = -1;
878
879 for_each_possible_cpu(i) {
880 iter->next_idx[i] = 0;
881 iter->next_page[i] = NULL;
882 }
883
884 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
885 ;
886
887 } else {
888 l = *pos - 1;
889 p = s_next(m, p, &l);
890 }
891
892 return p;
893 }
894
895 static void s_stop(struct seq_file *m, void *p)
896 {
897 struct trace_iterator *iter = m->private;
898
899 atomic_dec(&trace_record_cmdline_disabled);
900
901 /* let the tracer release locks here if needed */
902 if (current_trace && current_trace == iter->trace && iter->trace->stop)
903 iter->trace->stop(iter);
904
905 mutex_unlock(&trace_types_lock);
906 }
907
908 static int
909 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
910 {
911 #ifdef CONFIG_KALLSYMS
912 char str[KSYM_SYMBOL_LEN];
913
914 kallsyms_lookup(address, NULL, NULL, NULL, str);
915
916 return trace_seq_printf(s, fmt, str);
917 #endif
918 return 1;
919 }
920
921 static int
922 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
923 unsigned long address)
924 {
925 #ifdef CONFIG_KALLSYMS
926 char str[KSYM_SYMBOL_LEN];
927
928 sprint_symbol(str, address);
929 return trace_seq_printf(s, fmt, str);
930 #endif
931 return 1;
932 }
933
934 #ifndef CONFIG_64BIT
935 # define IP_FMT "%08lx"
936 #else
937 # define IP_FMT "%016lx"
938 #endif
939
940 static notrace int
941 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
942 {
943 int ret;
944
945 if (!ip)
946 return trace_seq_printf(s, "0");
947
948 if (sym_flags & TRACE_ITER_SYM_OFFSET)
949 ret = seq_print_sym_offset(s, "%s", ip);
950 else
951 ret = seq_print_sym_short(s, "%s", ip);
952
953 if (!ret)
954 return 0;
955
956 if (sym_flags & TRACE_ITER_SYM_ADDR)
957 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
958 return ret;
959 }
960
961 static notrace void print_lat_help_header(struct seq_file *m)
962 {
963 seq_puts(m, "# _------=> CPU# \n");
964 seq_puts(m, "# / _-----=> irqs-off \n");
965 seq_puts(m, "# | / _----=> need-resched \n");
966 seq_puts(m, "# || / _---=> hardirq/softirq \n");
967 seq_puts(m, "# ||| / _--=> preempt-depth \n");
968 seq_puts(m, "# |||| / \n");
969 seq_puts(m, "# ||||| delay \n");
970 seq_puts(m, "# cmd pid ||||| time | caller \n");
971 seq_puts(m, "# \\ / ||||| \\ | / \n");
972 }
973
974 static notrace void print_func_help_header(struct seq_file *m)
975 {
976 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
977 seq_puts(m, "# | | | | |\n");
978 }
979
980
981 static notrace void
982 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
983 {
984 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
985 struct trace_array *tr = iter->tr;
986 struct trace_array_cpu *data = tr->data[tr->cpu];
987 struct tracer *type = current_trace;
988 unsigned long total = 0;
989 unsigned long entries = 0;
990 int cpu;
991 const char *name = "preemption";
992
993 if (type)
994 name = type->name;
995
996 for_each_possible_cpu(cpu) {
997 if (head_page(tr->data[cpu])) {
998 total += tr->data[cpu]->trace_idx;
999 if (tr->data[cpu]->trace_idx > tr->entries)
1000 entries += tr->entries;
1001 else
1002 entries += tr->data[cpu]->trace_idx;
1003 }
1004 }
1005
1006 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1007 name, UTS_RELEASE);
1008 seq_puts(m, "-----------------------------------"
1009 "---------------------------------\n");
1010 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1011 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1012 nsecs_to_usecs(data->saved_latency),
1013 entries,
1014 total,
1015 tr->cpu,
1016 #if defined(CONFIG_PREEMPT_NONE)
1017 "server",
1018 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1019 "desktop",
1020 #elif defined(CONFIG_PREEMPT_DESKTOP)
1021 "preempt",
1022 #else
1023 "unknown",
1024 #endif
1025 /* These are reserved for later use */
1026 0, 0, 0, 0);
1027 #ifdef CONFIG_SMP
1028 seq_printf(m, " #P:%d)\n", num_online_cpus());
1029 #else
1030 seq_puts(m, ")\n");
1031 #endif
1032 seq_puts(m, " -----------------\n");
1033 seq_printf(m, " | task: %.16s-%d "
1034 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1035 data->comm, data->pid, data->uid, data->nice,
1036 data->policy, data->rt_priority);
1037 seq_puts(m, " -----------------\n");
1038
1039 if (data->critical_start) {
1040 seq_puts(m, " => started at: ");
1041 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1042 trace_print_seq(m, &iter->seq);
1043 seq_puts(m, "\n => ended at: ");
1044 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1045 trace_print_seq(m, &iter->seq);
1046 seq_puts(m, "\n");
1047 }
1048
1049 seq_puts(m, "\n");
1050 }
1051
1052 static notrace void
1053 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1054 {
1055 int hardirq, softirq;
1056 char *comm;
1057
1058 comm = trace_find_cmdline(entry->pid);
1059
1060 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1061 trace_seq_printf(s, "%d", cpu);
1062 trace_seq_printf(s, "%c%c",
1063 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1064 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1065
1066 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1067 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1068 if (hardirq && softirq)
1069 trace_seq_putc(s, 'H');
1070 else {
1071 if (hardirq)
1072 trace_seq_putc(s, 'h');
1073 else {
1074 if (softirq)
1075 trace_seq_putc(s, 's');
1076 else
1077 trace_seq_putc(s, '.');
1078 }
1079 }
1080
1081 if (entry->preempt_count)
1082 trace_seq_printf(s, "%x", entry->preempt_count);
1083 else
1084 trace_seq_puts(s, ".");
1085 }
1086
1087 unsigned long preempt_mark_thresh = 100;
1088
1089 static notrace void
1090 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1091 unsigned long rel_usecs)
1092 {
1093 trace_seq_printf(s, " %4lldus", abs_usecs);
1094 if (rel_usecs > preempt_mark_thresh)
1095 trace_seq_puts(s, "!: ");
1096 else if (rel_usecs > 1)
1097 trace_seq_puts(s, "+: ");
1098 else
1099 trace_seq_puts(s, " : ");
1100 }
1101
1102 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1103
1104 static notrace int
1105 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1106 {
1107 struct trace_seq *s = &iter->seq;
1108 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1109 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1110 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1111 struct trace_entry *entry = iter->ent;
1112 unsigned long abs_usecs;
1113 unsigned long rel_usecs;
1114 char *comm;
1115 int S;
1116
1117 if (!next_entry)
1118 next_entry = entry;
1119 rel_usecs = ns2usecs(next_entry->t - entry->t);
1120 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1121
1122 if (verbose) {
1123 comm = trace_find_cmdline(entry->pid);
1124 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1125 " %ld.%03ldms (+%ld.%03ldms): ",
1126 comm,
1127 entry->pid, cpu, entry->flags,
1128 entry->preempt_count, trace_idx,
1129 ns2usecs(entry->t),
1130 abs_usecs/1000,
1131 abs_usecs % 1000, rel_usecs/1000,
1132 rel_usecs % 1000);
1133 } else {
1134 lat_print_generic(s, entry, cpu);
1135 lat_print_timestamp(s, abs_usecs, rel_usecs);
1136 }
1137 switch (entry->type) {
1138 case TRACE_FN:
1139 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1140 trace_seq_puts(s, " (");
1141 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1142 trace_seq_puts(s, ")\n");
1143 break;
1144 case TRACE_CTX:
1145 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1146 state_to_char[entry->ctx.prev_state] : 'X';
1147 comm = trace_find_cmdline(entry->ctx.next_pid);
1148 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1149 entry->ctx.prev_pid,
1150 entry->ctx.prev_prio,
1151 S,
1152 entry->ctx.next_pid,
1153 entry->ctx.next_prio,
1154 comm);
1155 break;
1156 default:
1157 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1158 }
1159 return 1;
1160 }
1161
1162 static notrace int print_trace_fmt(struct trace_iterator *iter)
1163 {
1164 struct trace_seq *s = &iter->seq;
1165 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1166 struct trace_entry *entry;
1167 unsigned long usec_rem;
1168 unsigned long long t;
1169 unsigned long secs;
1170 char *comm;
1171 int S;
1172 int ret;
1173
1174 entry = iter->ent;
1175
1176 comm = trace_find_cmdline(iter->ent->pid);
1177
1178 t = ns2usecs(entry->t);
1179 usec_rem = do_div(t, 1000000ULL);
1180 secs = (unsigned long)t;
1181
1182 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1183 if (!ret)
1184 return 0;
1185 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1186 if (!ret)
1187 return 0;
1188 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1189 if (!ret)
1190 return 0;
1191
1192 switch (entry->type) {
1193 case TRACE_FN:
1194 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1195 if (!ret)
1196 return 0;
1197 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1198 entry->fn.parent_ip) {
1199 ret = trace_seq_printf(s, " <-");
1200 if (!ret)
1201 return 0;
1202 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1203 sym_flags);
1204 if (!ret)
1205 return 0;
1206 }
1207 ret = trace_seq_printf(s, "\n");
1208 if (!ret)
1209 return 0;
1210 break;
1211 case TRACE_CTX:
1212 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1213 state_to_char[entry->ctx.prev_state] : 'X';
1214 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1215 entry->ctx.prev_pid,
1216 entry->ctx.prev_prio,
1217 S,
1218 entry->ctx.next_pid,
1219 entry->ctx.next_prio);
1220 if (!ret)
1221 return 0;
1222 break;
1223 }
1224 return 1;
1225 }
1226
1227 static notrace int print_raw_fmt(struct trace_iterator *iter)
1228 {
1229 struct trace_seq *s = &iter->seq;
1230 struct trace_entry *entry;
1231 int ret;
1232 int S;
1233
1234 entry = iter->ent;
1235
1236 ret = trace_seq_printf(s, "%d %d %llu ",
1237 entry->pid, iter->cpu, entry->t);
1238 if (!ret)
1239 return 0;
1240
1241 switch (entry->type) {
1242 case TRACE_FN:
1243 ret = trace_seq_printf(s, "%x %x\n",
1244 entry->fn.ip, entry->fn.parent_ip);
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,
1254 S,
1255 entry->ctx.next_pid,
1256 entry->ctx.next_prio);
1257 if (!ret)
1258 return 0;
1259 break;
1260 }
1261 return 1;
1262 }
1263
1264 static int trace_empty(struct trace_iterator *iter)
1265 {
1266 struct trace_array_cpu *data;
1267 int cpu;
1268
1269 for_each_possible_cpu(cpu) {
1270 data = iter->tr->data[cpu];
1271
1272 if (head_page(data) && data->trace_idx &&
1273 (data->trace_tail != data->trace_head ||
1274 data->trace_tail_idx != data->trace_head_idx))
1275 return 0;
1276 }
1277 return 1;
1278 }
1279
1280 static int print_trace_line(struct trace_iterator *iter)
1281 {
1282 if (trace_flags & TRACE_ITER_RAW)
1283 return print_raw_fmt(iter);
1284
1285 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1286 return print_lat_fmt(iter, iter->idx, iter->cpu);
1287
1288 return print_trace_fmt(iter);
1289 }
1290
1291 static int s_show(struct seq_file *m, void *v)
1292 {
1293 struct trace_iterator *iter = v;
1294
1295 if (iter->ent == NULL) {
1296 if (iter->tr) {
1297 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1298 seq_puts(m, "#\n");
1299 }
1300 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1301 /* print nothing if the buffers are empty */
1302 if (trace_empty(iter))
1303 return 0;
1304 print_trace_header(m, iter);
1305 if (!(trace_flags & TRACE_ITER_VERBOSE))
1306 print_lat_help_header(m);
1307 } else {
1308 if (!(trace_flags & TRACE_ITER_VERBOSE))
1309 print_func_help_header(m);
1310 }
1311 } else {
1312 print_trace_line(iter);
1313 trace_print_seq(m, &iter->seq);
1314 }
1315
1316 return 0;
1317 }
1318
1319 static struct seq_operations tracer_seq_ops = {
1320 .start = s_start,
1321 .next = s_next,
1322 .stop = s_stop,
1323 .show = s_show,
1324 };
1325
1326 static struct trace_iterator notrace *
1327 __tracing_open(struct inode *inode, struct file *file, int *ret)
1328 {
1329 struct trace_iterator *iter;
1330
1331 if (tracing_disabled) {
1332 *ret = -ENODEV;
1333 return NULL;
1334 }
1335
1336 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1337 if (!iter) {
1338 *ret = -ENOMEM;
1339 goto out;
1340 }
1341
1342 mutex_lock(&trace_types_lock);
1343 if (current_trace && current_trace->print_max)
1344 iter->tr = &max_tr;
1345 else
1346 iter->tr = inode->i_private;
1347 iter->trace = current_trace;
1348 iter->pos = -1;
1349
1350 /* TODO stop tracer */
1351 *ret = seq_open(file, &tracer_seq_ops);
1352 if (!*ret) {
1353 struct seq_file *m = file->private_data;
1354 m->private = iter;
1355
1356 /* stop the trace while dumping */
1357 if (iter->tr->ctrl)
1358 tracer_enabled = 0;
1359
1360 if (iter->trace && iter->trace->open)
1361 iter->trace->open(iter);
1362 } else {
1363 kfree(iter);
1364 iter = NULL;
1365 }
1366 mutex_unlock(&trace_types_lock);
1367
1368 out:
1369 return iter;
1370 }
1371
1372 int tracing_open_generic(struct inode *inode, struct file *filp)
1373 {
1374 if (tracing_disabled)
1375 return -ENODEV;
1376
1377 filp->private_data = inode->i_private;
1378 return 0;
1379 }
1380
1381 int tracing_release(struct inode *inode, struct file *file)
1382 {
1383 struct seq_file *m = (struct seq_file *)file->private_data;
1384 struct trace_iterator *iter = m->private;
1385
1386 mutex_lock(&trace_types_lock);
1387 if (iter->trace && iter->trace->close)
1388 iter->trace->close(iter);
1389
1390 /* reenable tracing if it was previously enabled */
1391 if (iter->tr->ctrl)
1392 tracer_enabled = 1;
1393 mutex_unlock(&trace_types_lock);
1394
1395 seq_release(inode, file);
1396 kfree(iter);
1397 return 0;
1398 }
1399
1400 static int tracing_open(struct inode *inode, struct file *file)
1401 {
1402 int ret;
1403
1404 __tracing_open(inode, file, &ret);
1405
1406 return ret;
1407 }
1408
1409 static int tracing_lt_open(struct inode *inode, struct file *file)
1410 {
1411 struct trace_iterator *iter;
1412 int ret;
1413
1414 iter = __tracing_open(inode, file, &ret);
1415
1416 if (!ret)
1417 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1418
1419 return ret;
1420 }
1421
1422
1423 static notrace void *
1424 t_next(struct seq_file *m, void *v, loff_t *pos)
1425 {
1426 struct tracer *t = m->private;
1427
1428 (*pos)++;
1429
1430 if (t)
1431 t = t->next;
1432
1433 m->private = t;
1434
1435 return t;
1436 }
1437
1438 static void *t_start(struct seq_file *m, loff_t *pos)
1439 {
1440 struct tracer *t = m->private;
1441 loff_t l = 0;
1442
1443 mutex_lock(&trace_types_lock);
1444 for (; t && l < *pos; t = t_next(m, t, &l))
1445 ;
1446
1447 return t;
1448 }
1449
1450 static void t_stop(struct seq_file *m, void *p)
1451 {
1452 mutex_unlock(&trace_types_lock);
1453 }
1454
1455 static int t_show(struct seq_file *m, void *v)
1456 {
1457 struct tracer *t = v;
1458
1459 if (!t)
1460 return 0;
1461
1462 seq_printf(m, "%s", t->name);
1463 if (t->next)
1464 seq_putc(m, ' ');
1465 else
1466 seq_putc(m, '\n');
1467
1468 return 0;
1469 }
1470
1471 static struct seq_operations show_traces_seq_ops = {
1472 .start = t_start,
1473 .next = t_next,
1474 .stop = t_stop,
1475 .show = t_show,
1476 };
1477
1478 static int show_traces_open(struct inode *inode, struct file *file)
1479 {
1480 int ret;
1481
1482 if (tracing_disabled)
1483 return -ENODEV;
1484
1485 ret = seq_open(file, &show_traces_seq_ops);
1486 if (!ret) {
1487 struct seq_file *m = file->private_data;
1488 m->private = trace_types;
1489 }
1490
1491 return ret;
1492 }
1493
1494 static struct file_operations tracing_fops = {
1495 .open = tracing_open,
1496 .read = seq_read,
1497 .llseek = seq_lseek,
1498 .release = tracing_release,
1499 };
1500
1501 static struct file_operations tracing_lt_fops = {
1502 .open = tracing_lt_open,
1503 .read = seq_read,
1504 .llseek = seq_lseek,
1505 .release = tracing_release,
1506 };
1507
1508 static struct file_operations show_traces_fops = {
1509 .open = show_traces_open,
1510 .read = seq_read,
1511 .release = seq_release,
1512 };
1513
1514 static ssize_t
1515 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1516 size_t cnt, loff_t *ppos)
1517 {
1518 char *buf;
1519 int r = 0;
1520 int len = 0;
1521 int i;
1522
1523 /* calulate max size */
1524 for (i = 0; trace_options[i]; i++) {
1525 len += strlen(trace_options[i]);
1526 len += 3; /* "no" and space */
1527 }
1528
1529 /* +2 for \n and \0 */
1530 buf = kmalloc(len + 2, GFP_KERNEL);
1531 if (!buf)
1532 return -ENOMEM;
1533
1534 for (i = 0; trace_options[i]; i++) {
1535 if (trace_flags & (1 << i))
1536 r += sprintf(buf + r, "%s ", trace_options[i]);
1537 else
1538 r += sprintf(buf + r, "no%s ", trace_options[i]);
1539 }
1540
1541 r += sprintf(buf + r, "\n");
1542 WARN_ON(r >= len + 2);
1543
1544 r = simple_read_from_buffer(ubuf, cnt, ppos,
1545 buf, r);
1546
1547 kfree(buf);
1548
1549 return r;
1550 }
1551
1552 static ssize_t
1553 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1554 size_t cnt, loff_t *ppos)
1555 {
1556 char buf[64];
1557 char *cmp = buf;
1558 int neg = 0;
1559 int i;
1560
1561 if (cnt > 63)
1562 cnt = 63;
1563
1564 if (copy_from_user(&buf, ubuf, cnt))
1565 return -EFAULT;
1566
1567 buf[cnt] = 0;
1568
1569 if (strncmp(buf, "no", 2) == 0) {
1570 neg = 1;
1571 cmp += 2;
1572 }
1573
1574 for (i = 0; trace_options[i]; i++) {
1575 int len = strlen(trace_options[i]);
1576
1577 if (strncmp(cmp, trace_options[i], len) == 0) {
1578 if (neg)
1579 trace_flags &= ~(1 << i);
1580 else
1581 trace_flags |= (1 << i);
1582 break;
1583 }
1584 }
1585
1586 filp->f_pos += cnt;
1587
1588 return cnt;
1589 }
1590
1591 static struct file_operations tracing_iter_fops = {
1592 .open = tracing_open_generic,
1593 .read = tracing_iter_ctrl_read,
1594 .write = tracing_iter_ctrl_write,
1595 };
1596
1597 static const char readme_msg[] =
1598 "tracing mini-HOWTO:\n\n"
1599 "# mkdir /debug\n"
1600 "# mount -t debugfs nodev /debug\n\n"
1601 "# cat /debug/tracing/available_tracers\n"
1602 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1603 "# cat /debug/tracing/current_tracer\n"
1604 "none\n"
1605 "# echo sched_switch > /debug/tracing/current_tracer\n"
1606 "# cat /debug/tracing/current_tracer\n"
1607 "sched_switch\n"
1608 "# cat /debug/tracing/iter_ctrl\n"
1609 "noprint-parent nosym-offset nosym-addr noverbose\n"
1610 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1611 "# echo 1 > /debug/tracing/tracing_enabled\n"
1612 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1613 "echo 0 > /debug/tracing/tracing_enabled\n"
1614 ;
1615
1616 static ssize_t
1617 tracing_readme_read(struct file *filp, char __user *ubuf,
1618 size_t cnt, loff_t *ppos)
1619 {
1620 return simple_read_from_buffer(ubuf, cnt, ppos,
1621 readme_msg, strlen(readme_msg));
1622 }
1623
1624 static struct file_operations tracing_readme_fops = {
1625 .open = tracing_open_generic,
1626 .read = tracing_readme_read,
1627 };
1628
1629
1630 static ssize_t
1631 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1632 size_t cnt, loff_t *ppos)
1633 {
1634 struct trace_array *tr = filp->private_data;
1635 char buf[64];
1636 int r;
1637
1638 r = sprintf(buf, "%ld\n", tr->ctrl);
1639 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1640 }
1641
1642 static ssize_t
1643 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1644 size_t cnt, loff_t *ppos)
1645 {
1646 struct trace_array *tr = filp->private_data;
1647 long val;
1648 char buf[64];
1649
1650 if (cnt > 63)
1651 cnt = 63;
1652
1653 if (copy_from_user(&buf, ubuf, cnt))
1654 return -EFAULT;
1655
1656 buf[cnt] = 0;
1657
1658 val = simple_strtoul(buf, NULL, 10);
1659
1660 val = !!val;
1661
1662 mutex_lock(&trace_types_lock);
1663 if (tr->ctrl ^ val) {
1664 if (val)
1665 tracer_enabled = 1;
1666 else
1667 tracer_enabled = 0;
1668
1669 tr->ctrl = val;
1670
1671 if (current_trace && current_trace->ctrl_update)
1672 current_trace->ctrl_update(tr);
1673 }
1674 mutex_unlock(&trace_types_lock);
1675
1676 filp->f_pos += cnt;
1677
1678 return cnt;
1679 }
1680
1681 static ssize_t
1682 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1683 size_t cnt, loff_t *ppos)
1684 {
1685 char buf[max_tracer_type_len+2];
1686 int r;
1687
1688 mutex_lock(&trace_types_lock);
1689 if (current_trace)
1690 r = sprintf(buf, "%s\n", current_trace->name);
1691 else
1692 r = sprintf(buf, "\n");
1693 mutex_unlock(&trace_types_lock);
1694
1695 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1696 }
1697
1698 static ssize_t
1699 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1700 size_t cnt, loff_t *ppos)
1701 {
1702 struct trace_array *tr = &global_trace;
1703 struct tracer *t;
1704 char buf[max_tracer_type_len+1];
1705 int i;
1706
1707 if (cnt > max_tracer_type_len)
1708 cnt = max_tracer_type_len;
1709
1710 if (copy_from_user(&buf, ubuf, cnt))
1711 return -EFAULT;
1712
1713 buf[cnt] = 0;
1714
1715 /* strip ending whitespace. */
1716 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1717 buf[i] = 0;
1718
1719 mutex_lock(&trace_types_lock);
1720 for (t = trace_types; t; t = t->next) {
1721 if (strcmp(t->name, buf) == 0)
1722 break;
1723 }
1724 if (!t || t == current_trace)
1725 goto out;
1726
1727 if (current_trace && current_trace->reset)
1728 current_trace->reset(tr);
1729
1730 current_trace = t;
1731 if (t->init)
1732 t->init(tr);
1733
1734 out:
1735 mutex_unlock(&trace_types_lock);
1736
1737 filp->f_pos += cnt;
1738
1739 return cnt;
1740 }
1741
1742 static ssize_t
1743 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1744 size_t cnt, loff_t *ppos)
1745 {
1746 unsigned long *ptr = filp->private_data;
1747 char buf[64];
1748 int r;
1749
1750 r = snprintf(buf, 64, "%ld\n",
1751 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1752 if (r > 64)
1753 r = 64;
1754 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1755 }
1756
1757 static ssize_t
1758 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1759 size_t cnt, loff_t *ppos)
1760 {
1761 long *ptr = filp->private_data;
1762 long val;
1763 char buf[64];
1764
1765 if (cnt > 63)
1766 cnt = 63;
1767
1768 if (copy_from_user(&buf, ubuf, cnt))
1769 return -EFAULT;
1770
1771 buf[cnt] = 0;
1772
1773 val = simple_strtoul(buf, NULL, 10);
1774
1775 *ptr = val * 1000;
1776
1777 return cnt;
1778 }
1779
1780 static atomic_t tracing_reader;
1781
1782 static int tracing_open_pipe(struct inode *inode, struct file *filp)
1783 {
1784 struct trace_iterator *iter;
1785
1786 if (tracing_disabled)
1787 return -ENODEV;
1788
1789 /* We only allow for reader of the pipe */
1790 if (atomic_inc_return(&tracing_reader) != 1) {
1791 atomic_dec(&tracing_reader);
1792 return -EBUSY;
1793 }
1794
1795 /* create a buffer to store the information to pass to userspace */
1796 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1797 if (!iter)
1798 return -ENOMEM;
1799
1800 iter->tr = &global_trace;
1801
1802 filp->private_data = iter;
1803
1804 return 0;
1805 }
1806
1807 static int tracing_release_pipe(struct inode *inode, struct file *file)
1808 {
1809 struct trace_iterator *iter = file->private_data;
1810
1811 kfree(iter);
1812 atomic_dec(&tracing_reader);
1813
1814 return 0;
1815 }
1816
1817 /*
1818 * Consumer reader.
1819 */
1820 static ssize_t
1821 tracing_read_pipe(struct file *filp, char __user *ubuf,
1822 size_t cnt, loff_t *ppos)
1823 {
1824 struct trace_iterator *iter = filp->private_data;
1825 struct trace_array_cpu *data;
1826 static cpumask_t mask;
1827 struct trace_entry *entry;
1828 static int start;
1829 unsigned long flags;
1830 int read = 0;
1831 int cpu;
1832 int len;
1833 int ret;
1834
1835 /* return any leftover data */
1836 if (iter->seq.len > start) {
1837 len = iter->seq.len - start;
1838 if (cnt > len)
1839 cnt = len;
1840 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1841 if (ret)
1842 cnt = -EFAULT;
1843
1844 start += len;
1845
1846 return cnt;
1847 }
1848
1849 trace_seq_reset(&iter->seq);
1850 start = 0;
1851
1852 while (trace_empty(iter)) {
1853 /*
1854 * This is a make-shift waitqueue. The reason we don't use
1855 * an actual wait queue is because:
1856 * 1) we only ever have one waiter
1857 * 2) the tracing, traces all functions, we don't want
1858 * the overhead of calling wake_up and friends
1859 * (and tracing them too)
1860 * Anyway, this is really very primitive wakeup.
1861 */
1862 set_current_state(TASK_INTERRUPTIBLE);
1863 iter->tr->waiter = current;
1864
1865 /* sleep for one second, and try again. */
1866 schedule_timeout(HZ);
1867
1868 iter->tr->waiter = NULL;
1869
1870 if (signal_pending(current))
1871 return -EINTR;
1872
1873 /*
1874 * We block until we read something and tracing is disabled.
1875 * We still block if tracing is disabled, but we have never
1876 * read anything. This allows a user to cat this file, and
1877 * then enable tracing. But after we have read something,
1878 * we give an EOF when tracing is again disabled.
1879 *
1880 * iter->pos will be 0 if we haven't read anything.
1881 */
1882 if (!tracer_enabled && iter->pos)
1883 break;
1884
1885 continue;
1886 }
1887
1888 /* stop when tracing is finished */
1889 if (trace_empty(iter))
1890 return 0;
1891
1892 if (cnt >= PAGE_SIZE)
1893 cnt = PAGE_SIZE - 1;
1894
1895 memset(iter, 0, sizeof(*iter));
1896 iter->tr = &global_trace;
1897 iter->pos = -1;
1898
1899 /*
1900 * We need to stop all tracing on all CPUS to read the
1901 * the next buffer. This is a bit expensive, but is
1902 * not done often. We fill all what we can read,
1903 * and then release the locks again.
1904 */
1905
1906 cpus_clear(mask);
1907 local_irq_save(flags);
1908 for_each_possible_cpu(cpu) {
1909 data = iter->tr->data[cpu];
1910
1911 if (!head_page(data) || !data->trace_idx)
1912 continue;
1913
1914 atomic_inc(&data->disabled);
1915 spin_lock(&data->lock);
1916 cpu_set(cpu, mask);
1917 }
1918
1919 while ((entry = find_next_entry_inc(iter)) != NULL) {
1920 ret = print_trace_line(iter);
1921 if (!ret)
1922 break;
1923
1924 trace_consume(iter);
1925
1926 if (iter->seq.len >= cnt)
1927 break;
1928 }
1929
1930 for_each_cpu_mask(cpu, mask) {
1931 data = iter->tr->data[cpu];
1932 spin_unlock(&data->lock);
1933 atomic_dec(&data->disabled);
1934 }
1935 local_irq_restore(flags);
1936
1937 /* Now copy what we have to the user */
1938 read = iter->seq.len;
1939 if (read > cnt)
1940 read = cnt;
1941
1942 ret = copy_to_user(ubuf, iter->seq.buffer, read);
1943
1944 if (read < iter->seq.len)
1945 start = read;
1946 else
1947 trace_seq_reset(&iter->seq);
1948
1949 if (ret)
1950 read = -EFAULT;
1951
1952 return read;
1953 }
1954
1955 static struct file_operations tracing_max_lat_fops = {
1956 .open = tracing_open_generic,
1957 .read = tracing_max_lat_read,
1958 .write = tracing_max_lat_write,
1959 };
1960
1961 static struct file_operations tracing_ctrl_fops = {
1962 .open = tracing_open_generic,
1963 .read = tracing_ctrl_read,
1964 .write = tracing_ctrl_write,
1965 };
1966
1967 static struct file_operations set_tracer_fops = {
1968 .open = tracing_open_generic,
1969 .read = tracing_set_trace_read,
1970 .write = tracing_set_trace_write,
1971 };
1972
1973 static struct file_operations tracing_pipe_fops = {
1974 .open = tracing_open_pipe,
1975 .read = tracing_read_pipe,
1976 .release = tracing_release_pipe,
1977 };
1978
1979 #ifdef CONFIG_DYNAMIC_FTRACE
1980
1981 static ssize_t
1982 tracing_read_long(struct file *filp, char __user *ubuf,
1983 size_t cnt, loff_t *ppos)
1984 {
1985 unsigned long *p = filp->private_data;
1986 char buf[64];
1987 int r;
1988
1989 r = sprintf(buf, "%ld\n", *p);
1990
1991 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1992 }
1993
1994 static struct file_operations tracing_read_long_fops = {
1995 .open = tracing_open_generic,
1996 .read = tracing_read_long,
1997 };
1998 #endif
1999
2000 static struct dentry *d_tracer;
2001
2002 struct dentry *tracing_init_dentry(void)
2003 {
2004 static int once;
2005
2006 if (d_tracer)
2007 return d_tracer;
2008
2009 d_tracer = debugfs_create_dir("tracing", NULL);
2010
2011 if (!d_tracer && !once) {
2012 once = 1;
2013 pr_warning("Could not create debugfs directory 'tracing'\n");
2014 return NULL;
2015 }
2016
2017 return d_tracer;
2018 }
2019
2020 #ifdef CONFIG_FTRACE_SELFTEST
2021 /* Let selftest have access to static functions in this file */
2022 #include "trace_selftest.c"
2023 #endif
2024
2025 static __init void tracer_init_debugfs(void)
2026 {
2027 struct dentry *d_tracer;
2028 struct dentry *entry;
2029
2030 d_tracer = tracing_init_dentry();
2031
2032 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2033 &global_trace, &tracing_ctrl_fops);
2034 if (!entry)
2035 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2036
2037 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2038 NULL, &tracing_iter_fops);
2039 if (!entry)
2040 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2041
2042 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2043 &global_trace, &tracing_lt_fops);
2044 if (!entry)
2045 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2046
2047 entry = debugfs_create_file("trace", 0444, d_tracer,
2048 &global_trace, &tracing_fops);
2049 if (!entry)
2050 pr_warning("Could not create debugfs 'trace' entry\n");
2051
2052 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2053 &global_trace, &show_traces_fops);
2054 if (!entry)
2055 pr_warning("Could not create debugfs 'trace' entry\n");
2056
2057 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2058 &global_trace, &set_tracer_fops);
2059 if (!entry)
2060 pr_warning("Could not create debugfs 'trace' entry\n");
2061
2062 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2063 &tracing_max_latency,
2064 &tracing_max_lat_fops);
2065 if (!entry)
2066 pr_warning("Could not create debugfs "
2067 "'tracing_max_latency' entry\n");
2068
2069 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2070 &tracing_thresh, &tracing_max_lat_fops);
2071 if (!entry)
2072 pr_warning("Could not create debugfs "
2073 "'tracing_threash' entry\n");
2074 entry = debugfs_create_file("README", 0644, d_tracer,
2075 NULL, &tracing_readme_fops);
2076 if (!entry)
2077 pr_warning("Could not create debugfs 'README' entry\n");
2078
2079 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2080 NULL, &tracing_pipe_fops);
2081 if (!entry)
2082 pr_warning("Could not create debugfs "
2083 "'tracing_threash' entry\n");
2084
2085 #ifdef CONFIG_DYNAMIC_FTRACE
2086 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2087 &ftrace_update_tot_cnt,
2088 &tracing_read_long_fops);
2089 if (!entry)
2090 pr_warning("Could not create debugfs "
2091 "'dyn_ftrace_total_info' entry\n");
2092 #endif
2093 }
2094
2095 /* dummy trace to disable tracing */
2096 static struct tracer no_tracer __read_mostly =
2097 {
2098 .name = "none",
2099 };
2100
2101 static int trace_alloc_page(void)
2102 {
2103 struct trace_array_cpu *data;
2104 struct page *page, *tmp;
2105 LIST_HEAD(pages);
2106 void *array;
2107 int i;
2108
2109 /* first allocate a page for each CPU */
2110 for_each_possible_cpu(i) {
2111 array = (void *)__get_free_page(GFP_KERNEL);
2112 if (array == NULL) {
2113 printk(KERN_ERR "tracer: failed to allocate page"
2114 "for trace buffer!\n");
2115 goto free_pages;
2116 }
2117
2118 page = virt_to_page(array);
2119 list_add(&page->lru, &pages);
2120
2121 /* Only allocate if we are actually using the max trace */
2122 #ifdef CONFIG_TRACER_MAX_TRACE
2123 array = (void *)__get_free_page(GFP_KERNEL);
2124 if (array == NULL) {
2125 printk(KERN_ERR "tracer: failed to allocate page"
2126 "for trace buffer!\n");
2127 goto free_pages;
2128 }
2129 page = virt_to_page(array);
2130 list_add(&page->lru, &pages);
2131 #endif
2132 }
2133
2134 /* Now that we successfully allocate a page per CPU, add them */
2135 for_each_possible_cpu(i) {
2136 data = global_trace.data[i];
2137 spin_lock_init(&data->lock);
2138 lockdep_set_class(&data->lock, &data->lock_key);
2139 page = list_entry(pages.next, struct page, lru);
2140 list_del_init(&page->lru);
2141 list_add_tail(&page->lru, &data->trace_pages);
2142 ClearPageLRU(page);
2143
2144 #ifdef CONFIG_TRACER_MAX_TRACE
2145 data = max_tr.data[i];
2146 spin_lock_init(&data->lock);
2147 lockdep_set_class(&data->lock, &data->lock_key);
2148 page = list_entry(pages.next, struct page, lru);
2149 list_del_init(&page->lru);
2150 list_add_tail(&page->lru, &data->trace_pages);
2151 SetPageLRU(page);
2152 #endif
2153 }
2154 global_trace.entries += ENTRIES_PER_PAGE;
2155
2156 return 0;
2157
2158 free_pages:
2159 list_for_each_entry_safe(page, tmp, &pages, lru) {
2160 list_del_init(&page->lru);
2161 __free_page(page);
2162 }
2163 return -ENOMEM;
2164 }
2165
2166 __init static int tracer_alloc_buffers(void)
2167 {
2168 struct trace_array_cpu *data;
2169 void *array;
2170 struct page *page;
2171 int pages = 0;
2172 int ret = -ENOMEM;
2173 int i;
2174
2175 /* Allocate the first page for all buffers */
2176 for_each_possible_cpu(i) {
2177 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2178 max_tr.data[i] = &per_cpu(max_data, i);
2179
2180 array = (void *)__get_free_page(GFP_KERNEL);
2181 if (array == NULL) {
2182 printk(KERN_ERR "tracer: failed to allocate page"
2183 "for trace buffer!\n");
2184 goto free_buffers;
2185 }
2186
2187 /* set the array to the list */
2188 INIT_LIST_HEAD(&data->trace_pages);
2189 page = virt_to_page(array);
2190 list_add(&page->lru, &data->trace_pages);
2191 /* use the LRU flag to differentiate the two buffers */
2192 ClearPageLRU(page);
2193
2194 /* Only allocate if we are actually using the max trace */
2195 #ifdef CONFIG_TRACER_MAX_TRACE
2196 array = (void *)__get_free_page(GFP_KERNEL);
2197 if (array == NULL) {
2198 printk(KERN_ERR "tracer: failed to allocate page"
2199 "for trace buffer!\n");
2200 goto free_buffers;
2201 }
2202
2203 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2204 page = virt_to_page(array);
2205 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2206 SetPageLRU(page);
2207 #endif
2208 }
2209
2210 /*
2211 * Since we allocate by orders of pages, we may be able to
2212 * round up a bit.
2213 */
2214 global_trace.entries = ENTRIES_PER_PAGE;
2215 pages++;
2216
2217 while (global_trace.entries < trace_nr_entries) {
2218 if (trace_alloc_page())
2219 break;
2220 pages++;
2221 }
2222 max_tr.entries = global_trace.entries;
2223
2224 pr_info("tracer: %d pages allocated for %ld",
2225 pages, trace_nr_entries);
2226 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2227 pr_info(" actual entries %ld\n", global_trace.entries);
2228
2229 tracer_init_debugfs();
2230
2231 trace_init_cmdlines();
2232
2233 register_tracer(&no_tracer);
2234 current_trace = &no_tracer;
2235
2236 /* All seems OK, enable tracing */
2237 tracing_disabled = 0;
2238
2239 return 0;
2240
2241 free_buffers:
2242 for (i-- ; i >= 0; i--) {
2243 struct page *page, *tmp;
2244 struct trace_array_cpu *data = global_trace.data[i];
2245
2246 if (data) {
2247 list_for_each_entry_safe(page, tmp,
2248 &data->trace_pages, lru) {
2249 list_del_init(&page->lru);
2250 __free_page(page);
2251 }
2252 }
2253
2254 #ifdef CONFIG_TRACER_MAX_TRACE
2255 data = max_tr.data[i];
2256 if (data) {
2257 list_for_each_entry_safe(page, tmp,
2258 &data->trace_pages, lru) {
2259 list_del_init(&page->lru);
2260 __free_page(page);
2261 }
2262 }
2263 #endif
2264 }
2265 return ret;
2266 }
2267 fs_initcall(tracer_alloc_buffers);
This page took 0.077272 seconds and 5 git commands to generate.