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