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 #include <linux/writeback.h>
31
32 #include <linux/stacktrace.h>
33
34 #include <asm/asm-offsets.h>
35 #include <asm/unistd.h>
36
37 #include "trace.h"
38
39 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
40 unsigned long __read_mostly tracing_thresh;
41
42 static unsigned long __read_mostly tracing_nr_buffers;
43 static cpumask_t __read_mostly tracing_buffer_mask;
44
45 #define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
46 #define for_each_tracing_cpu(cpu) \
47 for_each_cpu_mask_nr(cpu, tracing_buffer_mask)
48
49 /* dummy trace to disable tracing */
50 static struct tracer no_tracer __read_mostly = {
51 .name = "none",
52 };
53
54 static int trace_alloc_page(void);
55 static int trace_free_page(void);
56
57 static int tracing_disabled = 1;
58
59 static unsigned long tracing_pages_allocated;
60
61 long
62 ns2usecs(cycle_t nsec)
63 {
64 nsec += 500;
65 do_div(nsec, 1000);
66 return nsec;
67 }
68
69 cycle_t ftrace_now(int cpu)
70 {
71 // return cpu_clock(cpu);
72 return sched_clock();
73 }
74
75 /*
76 * The global_trace is the descriptor that holds the tracing
77 * buffers for the live tracing. For each CPU, it contains
78 * a link list of pages that will store trace entries. The
79 * page descriptor of the pages in the memory is used to hold
80 * the link list by linking the lru item in the page descriptor
81 * to each of the pages in the buffer per CPU.
82 *
83 * For each active CPU there is a data field that holds the
84 * pages for the buffer for that CPU. Each CPU has the same number
85 * of pages allocated for its buffer.
86 */
87 static struct trace_array global_trace;
88
89 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
90
91 /*
92 * The max_tr is used to snapshot the global_trace when a maximum
93 * latency is reached. Some tracers will use this to store a maximum
94 * trace while it continues examining live traces.
95 *
96 * The buffers for the max_tr are set up the same as the global_trace.
97 * When a snapshot is taken, the link list of the max_tr is swapped
98 * with the link list of the global_trace and the buffers are reset for
99 * the global_trace so the tracing can continue.
100 */
101 static struct trace_array max_tr;
102
103 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
104
105 /* tracer_enabled is used to toggle activation of a tracer */
106 static int tracer_enabled = 1;
107
108 /*
109 * trace_nr_entries is the number of entries that is allocated
110 * for a buffer. Note, the number of entries is always rounded
111 * to ENTRIES_PER_PAGE.
112 */
113 static unsigned long trace_nr_entries = 65536UL;
114
115 /* trace_types holds a link list of available tracers. */
116 static struct tracer *trace_types __read_mostly;
117
118 /* current_trace points to the tracer that is currently active */
119 static struct tracer *current_trace __read_mostly;
120
121 /*
122 * max_tracer_type_len is used to simplify the allocating of
123 * buffers to read userspace tracer names. We keep track of
124 * the longest tracer name registered.
125 */
126 static int max_tracer_type_len;
127
128 /*
129 * trace_types_lock is used to protect the trace_types list.
130 * This lock is also used to keep user access serialized.
131 * Accesses from userspace will grab this lock while userspace
132 * activities happen inside the kernel.
133 */
134 static DEFINE_MUTEX(trace_types_lock);
135
136 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
137 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
138
139 /* trace_flags holds iter_ctrl options */
140 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
141
142 /**
143 * trace_wake_up - wake up tasks waiting for trace input
144 *
145 * Simply wakes up any task that is blocked on the trace_wait
146 * queue. These is used with trace_poll for tasks polling the trace.
147 */
148 void trace_wake_up(void)
149 {
150 /*
151 * The runqueue_is_locked() can fail, but this is the best we
152 * have for now:
153 */
154 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
155 wake_up(&trace_wait);
156 }
157
158 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
159
160 static int __init set_nr_entries(char *str)
161 {
162 unsigned long nr_entries;
163 int ret;
164
165 if (!str)
166 return 0;
167 ret = strict_strtoul(str, 0, &nr_entries);
168 /* nr_entries can not be zero */
169 if (ret < 0 || nr_entries == 0)
170 return 0;
171 trace_nr_entries = nr_entries;
172 return 1;
173 }
174 __setup("trace_entries=", set_nr_entries);
175
176 unsigned long nsecs_to_usecs(unsigned long nsecs)
177 {
178 return nsecs / 1000;
179 }
180
181 /*
182 * trace_flag_type is an enumeration that holds different
183 * states when a trace occurs. These are:
184 * IRQS_OFF - interrupts were disabled
185 * NEED_RESCED - reschedule is requested
186 * HARDIRQ - inside an interrupt handler
187 * SOFTIRQ - inside a softirq handler
188 */
189 enum trace_flag_type {
190 TRACE_FLAG_IRQS_OFF = 0x01,
191 TRACE_FLAG_NEED_RESCHED = 0x02,
192 TRACE_FLAG_HARDIRQ = 0x04,
193 TRACE_FLAG_SOFTIRQ = 0x08,
194 };
195
196 /*
197 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
198 * control the output of kernel symbols.
199 */
200 #define TRACE_ITER_SYM_MASK \
201 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
202
203 /* These must match the bit postions in trace_iterator_flags */
204 static const char *trace_options[] = {
205 "print-parent",
206 "sym-offset",
207 "sym-addr",
208 "verbose",
209 "raw",
210 "hex",
211 "bin",
212 "block",
213 "stacktrace",
214 "sched-tree",
215 NULL
216 };
217
218 /*
219 * ftrace_max_lock is used to protect the swapping of buffers
220 * when taking a max snapshot. The buffers themselves are
221 * protected by per_cpu spinlocks. But the action of the swap
222 * needs its own lock.
223 *
224 * This is defined as a raw_spinlock_t in order to help
225 * with performance when lockdep debugging is enabled.
226 */
227 static __raw_spinlock_t ftrace_max_lock =
228 (__raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
229
230 /*
231 * Copy the new maximum trace into the separate maximum-trace
232 * structure. (this way the maximum trace is permanently saved,
233 * for later retrieval via /debugfs/tracing/latency_trace)
234 */
235 static void
236 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
237 {
238 struct trace_array_cpu *data = tr->data[cpu];
239
240 max_tr.cpu = cpu;
241 max_tr.time_start = data->preempt_timestamp;
242
243 data = max_tr.data[cpu];
244 data->saved_latency = tracing_max_latency;
245
246 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
247 data->pid = tsk->pid;
248 data->uid = tsk->uid;
249 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
250 data->policy = tsk->policy;
251 data->rt_priority = tsk->rt_priority;
252
253 /* record this tasks comm */
254 tracing_record_cmdline(current);
255 }
256
257 #define CHECK_COND(cond) \
258 if (unlikely(cond)) { \
259 tracing_disabled = 1; \
260 WARN_ON(1); \
261 return -1; \
262 }
263
264 /**
265 * check_pages - integrity check of trace buffers
266 *
267 * As a safty measure we check to make sure the data pages have not
268 * been corrupted.
269 */
270 int check_pages(struct trace_array_cpu *data)
271 {
272 struct page *page, *tmp;
273
274 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
275 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
276
277 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
278 CHECK_COND(page->lru.next->prev != &page->lru);
279 CHECK_COND(page->lru.prev->next != &page->lru);
280 }
281
282 return 0;
283 }
284
285 /**
286 * head_page - page address of the first page in per_cpu buffer.
287 *
288 * head_page returns the page address of the first page in
289 * a per_cpu buffer. This also preforms various consistency
290 * checks to make sure the buffer has not been corrupted.
291 */
292 void *head_page(struct trace_array_cpu *data)
293 {
294 struct page *page;
295
296 if (list_empty(&data->trace_pages))
297 return NULL;
298
299 page = list_entry(data->trace_pages.next, struct page, lru);
300 BUG_ON(&page->lru == &data->trace_pages);
301
302 return page_address(page);
303 }
304
305 /**
306 * trace_seq_printf - sequence printing of trace information
307 * @s: trace sequence descriptor
308 * @fmt: printf format string
309 *
310 * The tracer may use either sequence operations or its own
311 * copy to user routines. To simplify formating of a trace
312 * trace_seq_printf is used to store strings into a special
313 * buffer (@s). Then the output may be either used by
314 * the sequencer or pulled into another buffer.
315 */
316 int
317 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
318 {
319 int len = (PAGE_SIZE - 1) - s->len;
320 va_list ap;
321 int ret;
322
323 if (!len)
324 return 0;
325
326 va_start(ap, fmt);
327 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
328 va_end(ap);
329
330 /* If we can't write it all, don't bother writing anything */
331 if (ret >= len)
332 return 0;
333
334 s->len += ret;
335
336 return len;
337 }
338
339 /**
340 * trace_seq_puts - trace sequence printing of simple string
341 * @s: trace sequence descriptor
342 * @str: simple string to record
343 *
344 * The tracer may use either the sequence operations or its own
345 * copy to user routines. This function records a simple string
346 * into a special buffer (@s) for later retrieval by a sequencer
347 * or other mechanism.
348 */
349 static int
350 trace_seq_puts(struct trace_seq *s, const char *str)
351 {
352 int len = strlen(str);
353
354 if (len > ((PAGE_SIZE - 1) - s->len))
355 return 0;
356
357 memcpy(s->buffer + s->len, str, len);
358 s->len += len;
359
360 return len;
361 }
362
363 static int
364 trace_seq_putc(struct trace_seq *s, unsigned char c)
365 {
366 if (s->len >= (PAGE_SIZE - 1))
367 return 0;
368
369 s->buffer[s->len++] = c;
370
371 return 1;
372 }
373
374 static int
375 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
376 {
377 if (len > ((PAGE_SIZE - 1) - s->len))
378 return 0;
379
380 memcpy(s->buffer + s->len, mem, len);
381 s->len += len;
382
383 return len;
384 }
385
386 #define HEX_CHARS 17
387 static const char hex2asc[] = "0123456789abcdef";
388
389 static int
390 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
391 {
392 unsigned char hex[HEX_CHARS];
393 unsigned char *data = mem;
394 unsigned char byte;
395 int i, j;
396
397 BUG_ON(len >= HEX_CHARS);
398
399 #ifdef __BIG_ENDIAN
400 for (i = 0, j = 0; i < len; i++) {
401 #else
402 for (i = len-1, j = 0; i >= 0; i--) {
403 #endif
404 byte = data[i];
405
406 hex[j++] = hex2asc[byte & 0x0f];
407 hex[j++] = hex2asc[byte >> 4];
408 }
409 hex[j++] = ' ';
410
411 return trace_seq_putmem(s, hex, j);
412 }
413
414 static void
415 trace_seq_reset(struct trace_seq *s)
416 {
417 s->len = 0;
418 s->readpos = 0;
419 }
420
421 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
422 {
423 int len;
424 int ret;
425
426 if (s->len <= s->readpos)
427 return -EBUSY;
428
429 len = s->len - s->readpos;
430 if (cnt > len)
431 cnt = len;
432 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
433 if (ret)
434 return -EFAULT;
435
436 s->readpos += len;
437 return cnt;
438 }
439
440 static void
441 trace_print_seq(struct seq_file *m, struct trace_seq *s)
442 {
443 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
444
445 s->buffer[len] = 0;
446 seq_puts(m, s->buffer);
447
448 trace_seq_reset(s);
449 }
450
451 /*
452 * flip the trace buffers between two trace descriptors.
453 * This usually is the buffers between the global_trace and
454 * the max_tr to record a snapshot of a current trace.
455 *
456 * The ftrace_max_lock must be held.
457 */
458 static void
459 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
460 {
461 struct list_head flip_pages;
462
463 INIT_LIST_HEAD(&flip_pages);
464
465 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
466 sizeof(struct trace_array_cpu) -
467 offsetof(struct trace_array_cpu, trace_head_idx));
468
469 check_pages(tr1);
470 check_pages(tr2);
471 list_splice_init(&tr1->trace_pages, &flip_pages);
472 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
473 list_splice_init(&flip_pages, &tr2->trace_pages);
474 BUG_ON(!list_empty(&flip_pages));
475 check_pages(tr1);
476 check_pages(tr2);
477 }
478
479 /**
480 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
481 * @tr: tracer
482 * @tsk: the task with the latency
483 * @cpu: The cpu that initiated the trace.
484 *
485 * Flip the buffers between the @tr and the max_tr and record information
486 * about which task was the cause of this latency.
487 */
488 void
489 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
490 {
491 struct trace_array_cpu *data;
492 int i;
493
494 WARN_ON_ONCE(!irqs_disabled());
495 __raw_spin_lock(&ftrace_max_lock);
496 /* clear out all the previous traces */
497 for_each_tracing_cpu(i) {
498 data = tr->data[i];
499 flip_trace(max_tr.data[i], data);
500 tracing_reset(data);
501 }
502
503 __update_max_tr(tr, tsk, cpu);
504 __raw_spin_unlock(&ftrace_max_lock);
505 }
506
507 /**
508 * update_max_tr_single - only copy one trace over, and reset the rest
509 * @tr - tracer
510 * @tsk - task with the latency
511 * @cpu - the cpu of the buffer to copy.
512 *
513 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
514 */
515 void
516 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
517 {
518 struct trace_array_cpu *data = tr->data[cpu];
519 int i;
520
521 WARN_ON_ONCE(!irqs_disabled());
522 __raw_spin_lock(&ftrace_max_lock);
523 for_each_tracing_cpu(i)
524 tracing_reset(max_tr.data[i]);
525
526 flip_trace(max_tr.data[cpu], data);
527 tracing_reset(data);
528
529 __update_max_tr(tr, tsk, cpu);
530 __raw_spin_unlock(&ftrace_max_lock);
531 }
532
533 /**
534 * register_tracer - register a tracer with the ftrace system.
535 * @type - the plugin for the tracer
536 *
537 * Register a new plugin tracer.
538 */
539 int register_tracer(struct tracer *type)
540 {
541 struct tracer *t;
542 int len;
543 int ret = 0;
544
545 if (!type->name) {
546 pr_info("Tracer must have a name\n");
547 return -1;
548 }
549
550 mutex_lock(&trace_types_lock);
551 for (t = trace_types; t; t = t->next) {
552 if (strcmp(type->name, t->name) == 0) {
553 /* already found */
554 pr_info("Trace %s already registered\n",
555 type->name);
556 ret = -1;
557 goto out;
558 }
559 }
560
561 #ifdef CONFIG_FTRACE_STARTUP_TEST
562 if (type->selftest) {
563 struct tracer *saved_tracer = current_trace;
564 struct trace_array_cpu *data;
565 struct trace_array *tr = &global_trace;
566 int saved_ctrl = tr->ctrl;
567 int i;
568 /*
569 * Run a selftest on this tracer.
570 * Here we reset the trace buffer, and set the current
571 * tracer to be this tracer. The tracer can then run some
572 * internal tracing to verify that everything is in order.
573 * If we fail, we do not register this tracer.
574 */
575 for_each_tracing_cpu(i) {
576 data = tr->data[i];
577 if (!head_page(data))
578 continue;
579 tracing_reset(data);
580 }
581 current_trace = type;
582 tr->ctrl = 0;
583 /* the test is responsible for initializing and enabling */
584 pr_info("Testing tracer %s: ", type->name);
585 ret = type->selftest(type, tr);
586 /* the test is responsible for resetting too */
587 current_trace = saved_tracer;
588 tr->ctrl = saved_ctrl;
589 if (ret) {
590 printk(KERN_CONT "FAILED!\n");
591 goto out;
592 }
593 /* Only reset on passing, to avoid touching corrupted buffers */
594 for_each_tracing_cpu(i) {
595 data = tr->data[i];
596 if (!head_page(data))
597 continue;
598 tracing_reset(data);
599 }
600 printk(KERN_CONT "PASSED\n");
601 }
602 #endif
603
604 type->next = trace_types;
605 trace_types = type;
606 len = strlen(type->name);
607 if (len > max_tracer_type_len)
608 max_tracer_type_len = len;
609
610 out:
611 mutex_unlock(&trace_types_lock);
612
613 return ret;
614 }
615
616 void unregister_tracer(struct tracer *type)
617 {
618 struct tracer **t;
619 int len;
620
621 mutex_lock(&trace_types_lock);
622 for (t = &trace_types; *t; t = &(*t)->next) {
623 if (*t == type)
624 goto found;
625 }
626 pr_info("Trace %s not registered\n", type->name);
627 goto out;
628
629 found:
630 *t = (*t)->next;
631 if (strlen(type->name) != max_tracer_type_len)
632 goto out;
633
634 max_tracer_type_len = 0;
635 for (t = &trace_types; *t; t = &(*t)->next) {
636 len = strlen((*t)->name);
637 if (len > max_tracer_type_len)
638 max_tracer_type_len = len;
639 }
640 out:
641 mutex_unlock(&trace_types_lock);
642 }
643
644 void tracing_reset(struct trace_array_cpu *data)
645 {
646 data->trace_idx = 0;
647 data->overrun = 0;
648 data->trace_head = data->trace_tail = head_page(data);
649 data->trace_head_idx = 0;
650 data->trace_tail_idx = 0;
651 }
652
653 #define SAVED_CMDLINES 128
654 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
655 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
656 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
657 static int cmdline_idx;
658 static DEFINE_RAW_SPINLOCK(trace_cmdline_lock);
659
660 /* temporary disable recording */
661 atomic_t trace_record_cmdline_disabled __read_mostly;
662
663 static void trace_init_cmdlines(void)
664 {
665 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
666 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
667 cmdline_idx = 0;
668 }
669
670 void trace_stop_cmdline_recording(void);
671
672 static void trace_save_cmdline(struct task_struct *tsk)
673 {
674 unsigned map;
675 unsigned idx;
676
677 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
678 return;
679
680 /*
681 * It's not the end of the world if we don't get
682 * the lock, but we also don't want to spin
683 * nor do we want to disable interrupts,
684 * so if we miss here, then better luck next time.
685 */
686 if (!spin_trylock(&trace_cmdline_lock))
687 return;
688
689 /* from the pid, find the index of the cmdline array */
690 idx = map_pid_to_cmdline[tsk->pid];
691
692 if (idx >= SAVED_CMDLINES) {
693 /* this is new */
694 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
695
696 /* check the reverse map and reset it if needed */
697 map = map_cmdline_to_pid[idx];
698 if (map <= PID_MAX_DEFAULT)
699 map_pid_to_cmdline[map] = (unsigned)-1;
700
701 map_cmdline_to_pid[idx] = tsk->pid;
702 map_pid_to_cmdline[tsk->pid] = idx;
703
704 cmdline_idx = idx;
705 }
706
707 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
708
709 spin_unlock(&trace_cmdline_lock);
710 }
711
712 static char *trace_find_cmdline(int pid)
713 {
714 char *cmdline = "<...>";
715 unsigned map;
716
717 if (!pid)
718 return "<idle>";
719
720 if (pid > PID_MAX_DEFAULT)
721 goto out;
722
723 map = map_pid_to_cmdline[pid];
724 if (map >= SAVED_CMDLINES)
725 goto out;
726
727 if (map_cmdline_to_pid[map] != pid)
728 goto out;
729
730 cmdline = saved_cmdlines[map];
731
732 out:
733 return cmdline;
734 }
735
736 void tracing_record_cmdline(struct task_struct *tsk)
737 {
738 if (atomic_read(&trace_record_cmdline_disabled))
739 return;
740
741 trace_save_cmdline(tsk);
742 }
743
744 static inline struct list_head *
745 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
746 {
747 /*
748 * Roundrobin - but skip the head (which is not a real page):
749 */
750 next = next->next;
751 if (unlikely(next == &data->trace_pages))
752 next = next->next;
753 BUG_ON(next == &data->trace_pages);
754
755 return next;
756 }
757
758 static inline void *
759 trace_next_page(struct trace_array_cpu *data, void *addr)
760 {
761 struct list_head *next;
762 struct page *page;
763
764 page = virt_to_page(addr);
765
766 next = trace_next_list(data, &page->lru);
767 page = list_entry(next, struct page, lru);
768
769 return page_address(page);
770 }
771
772 static inline struct trace_entry *
773 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
774 {
775 unsigned long idx, idx_next;
776 struct trace_entry *entry;
777
778 data->trace_idx++;
779 idx = data->trace_head_idx;
780 idx_next = idx + 1;
781
782 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
783
784 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
785
786 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
787 data->trace_head = trace_next_page(data, data->trace_head);
788 idx_next = 0;
789 }
790
791 if (data->trace_head == data->trace_tail &&
792 idx_next == data->trace_tail_idx) {
793 /* overrun */
794 data->overrun++;
795 data->trace_tail_idx++;
796 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
797 data->trace_tail =
798 trace_next_page(data, data->trace_tail);
799 data->trace_tail_idx = 0;
800 }
801 }
802
803 data->trace_head_idx = idx_next;
804
805 return entry;
806 }
807
808 static inline void
809 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
810 {
811 struct task_struct *tsk = current;
812 unsigned long pc;
813
814 pc = preempt_count();
815
816 entry->preempt_count = pc & 0xff;
817 entry->pid = (tsk) ? tsk->pid : 0;
818 entry->t = ftrace_now(raw_smp_processor_id());
819 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
820 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
821 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
822 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
823 }
824
825 void
826 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
827 unsigned long ip, unsigned long parent_ip, unsigned long flags)
828 {
829 struct trace_entry *entry;
830 unsigned long irq_flags;
831
832 raw_local_irq_save(irq_flags);
833 __raw_spin_lock(&data->lock);
834 entry = tracing_get_trace_entry(tr, data);
835 tracing_generic_entry_update(entry, flags);
836 entry->type = TRACE_FN;
837 entry->fn.ip = ip;
838 entry->fn.parent_ip = parent_ip;
839 __raw_spin_unlock(&data->lock);
840 raw_local_irq_restore(irq_flags);
841 }
842
843 void
844 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
845 unsigned long ip, unsigned long parent_ip, unsigned long flags)
846 {
847 if (likely(!atomic_read(&data->disabled)))
848 trace_function(tr, data, ip, parent_ip, flags);
849 }
850
851 #ifdef CONFIG_MMIOTRACE
852 void __trace_mmiotrace_rw(struct trace_array *tr, struct trace_array_cpu *data,
853 struct mmiotrace_rw *rw)
854 {
855 struct trace_entry *entry;
856 unsigned long irq_flags;
857
858 raw_local_irq_save(irq_flags);
859 __raw_spin_lock(&data->lock);
860
861 entry = tracing_get_trace_entry(tr, data);
862 tracing_generic_entry_update(entry, 0);
863 entry->type = TRACE_MMIO_RW;
864 entry->mmiorw = *rw;
865
866 __raw_spin_unlock(&data->lock);
867 raw_local_irq_restore(irq_flags);
868
869 trace_wake_up();
870 }
871
872 void __trace_mmiotrace_map(struct trace_array *tr, struct trace_array_cpu *data,
873 struct mmiotrace_map *map)
874 {
875 struct trace_entry *entry;
876 unsigned long irq_flags;
877
878 raw_local_irq_save(irq_flags);
879 __raw_spin_lock(&data->lock);
880
881 entry = tracing_get_trace_entry(tr, data);
882 tracing_generic_entry_update(entry, 0);
883 entry->type = TRACE_MMIO_MAP;
884 entry->mmiomap = *map;
885
886 __raw_spin_unlock(&data->lock);
887 raw_local_irq_restore(irq_flags);
888
889 trace_wake_up();
890 }
891 #endif
892
893 void __trace_stack(struct trace_array *tr,
894 struct trace_array_cpu *data,
895 unsigned long flags,
896 int skip)
897 {
898 struct trace_entry *entry;
899 struct stack_trace trace;
900
901 if (!(trace_flags & TRACE_ITER_STACKTRACE))
902 return;
903
904 entry = tracing_get_trace_entry(tr, data);
905 tracing_generic_entry_update(entry, flags);
906 entry->type = TRACE_STACK;
907
908 memset(&entry->stack, 0, sizeof(entry->stack));
909
910 trace.nr_entries = 0;
911 trace.max_entries = FTRACE_STACK_ENTRIES;
912 trace.skip = skip;
913 trace.entries = entry->stack.caller;
914
915 save_stack_trace(&trace);
916 }
917
918 void
919 __trace_special(void *__tr, void *__data,
920 unsigned long arg1, unsigned long arg2, unsigned long arg3)
921 {
922 struct trace_array_cpu *data = __data;
923 struct trace_array *tr = __tr;
924 struct trace_entry *entry;
925 unsigned long irq_flags;
926
927 raw_local_irq_save(irq_flags);
928 __raw_spin_lock(&data->lock);
929 entry = tracing_get_trace_entry(tr, data);
930 tracing_generic_entry_update(entry, 0);
931 entry->type = TRACE_SPECIAL;
932 entry->special.arg1 = arg1;
933 entry->special.arg2 = arg2;
934 entry->special.arg3 = arg3;
935 __trace_stack(tr, data, irq_flags, 4);
936 __raw_spin_unlock(&data->lock);
937 raw_local_irq_restore(irq_flags);
938
939 trace_wake_up();
940 }
941
942 void
943 tracing_sched_switch_trace(struct trace_array *tr,
944 struct trace_array_cpu *data,
945 struct task_struct *prev,
946 struct task_struct *next,
947 unsigned long flags)
948 {
949 struct trace_entry *entry;
950 unsigned long irq_flags;
951
952 raw_local_irq_save(irq_flags);
953 __raw_spin_lock(&data->lock);
954 entry = tracing_get_trace_entry(tr, data);
955 tracing_generic_entry_update(entry, flags);
956 entry->type = TRACE_CTX;
957 entry->ctx.prev_pid = prev->pid;
958 entry->ctx.prev_prio = prev->prio;
959 entry->ctx.prev_state = prev->state;
960 entry->ctx.next_pid = next->pid;
961 entry->ctx.next_prio = next->prio;
962 entry->ctx.next_state = next->state;
963 __trace_stack(tr, data, flags, 5);
964 __raw_spin_unlock(&data->lock);
965 raw_local_irq_restore(irq_flags);
966 }
967
968 void
969 tracing_sched_wakeup_trace(struct trace_array *tr,
970 struct trace_array_cpu *data,
971 struct task_struct *wakee,
972 struct task_struct *curr,
973 unsigned long flags)
974 {
975 struct trace_entry *entry;
976 unsigned long irq_flags;
977
978 raw_local_irq_save(irq_flags);
979 __raw_spin_lock(&data->lock);
980 entry = tracing_get_trace_entry(tr, data);
981 tracing_generic_entry_update(entry, flags);
982 entry->type = TRACE_WAKE;
983 entry->ctx.prev_pid = curr->pid;
984 entry->ctx.prev_prio = curr->prio;
985 entry->ctx.prev_state = curr->state;
986 entry->ctx.next_pid = wakee->pid;
987 entry->ctx.next_prio = wakee->prio;
988 entry->ctx.next_state = wakee->state;
989 __trace_stack(tr, data, flags, 6);
990 __raw_spin_unlock(&data->lock);
991 raw_local_irq_restore(irq_flags);
992
993 trace_wake_up();
994 }
995
996 void
997 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
998 {
999 struct trace_array *tr = &global_trace;
1000 struct trace_array_cpu *data;
1001 unsigned long flags;
1002 long disabled;
1003 int cpu;
1004
1005 if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
1006 return;
1007
1008 local_irq_save(flags);
1009 cpu = raw_smp_processor_id();
1010 data = tr->data[cpu];
1011 disabled = atomic_inc_return(&data->disabled);
1012
1013 if (likely(disabled == 1))
1014 __trace_special(tr, data, arg1, arg2, arg3);
1015
1016 atomic_dec(&data->disabled);
1017 local_irq_restore(flags);
1018 }
1019
1020 void tracing_event_irq(struct trace_array *tr,
1021 struct trace_array_cpu *data,
1022 unsigned long flags,
1023 unsigned long ip,
1024 int irq, int usermode,
1025 unsigned long retip)
1026 {
1027 struct trace_entry *entry;
1028
1029 entry = tracing_get_trace_entry(tr, data);
1030 tracing_generic_entry_update(entry, flags);
1031 entry->type = TRACE_IRQ;
1032 entry->irq.ip = ip;
1033 entry->irq.irq = irq;
1034 entry->irq.ret_ip = retip;
1035 entry->irq.usermode = usermode;
1036 }
1037
1038 void tracing_event_fault(struct trace_array *tr,
1039 struct trace_array_cpu *data,
1040 unsigned long flags,
1041 unsigned long ip,
1042 unsigned long retip,
1043 unsigned long error_code,
1044 unsigned long address)
1045 {
1046 struct trace_entry *entry;
1047
1048 entry = tracing_get_trace_entry(tr, data);
1049 tracing_generic_entry_update(entry, flags);
1050 entry->type = TRACE_FAULT;
1051 entry->fault.ip = ip;
1052 entry->fault.ret_ip = retip;
1053 entry->fault.errorcode = error_code;
1054 entry->fault.address = address;
1055 }
1056
1057 void tracing_event_timer_set(struct trace_array *tr,
1058 struct trace_array_cpu *data,
1059 unsigned long flags,
1060 unsigned long ip,
1061 ktime_t *expires, void *timer)
1062 {
1063 struct trace_entry *entry;
1064
1065 entry = tracing_get_trace_entry(tr, data);
1066 tracing_generic_entry_update(entry, flags);
1067 entry->type = TRACE_TIMER_SET;
1068 entry->timer.ip = ip;
1069 entry->timer.expire = *expires;
1070 entry->timer.timer = timer;
1071 }
1072
1073 void tracing_event_program_event(struct trace_array *tr,
1074 struct trace_array_cpu *data,
1075 unsigned long flags,
1076 unsigned long ip,
1077 ktime_t *expires, int64_t *delta)
1078 {
1079 struct trace_entry *entry;
1080
1081 entry = tracing_get_trace_entry(tr, data);
1082 tracing_generic_entry_update(entry, flags);
1083 entry->type = TRACE_PROGRAM_EVENT;
1084 entry->program.ip = ip;
1085 entry->program.expire = *expires;
1086 entry->program.delta = *delta;
1087 }
1088
1089 void tracing_event_timer_triggered(struct trace_array *tr,
1090 struct trace_array_cpu *data,
1091 unsigned long flags,
1092 unsigned long ip,
1093 ktime_t *expired, void *timer)
1094 {
1095 struct trace_entry *entry;
1096
1097 entry = tracing_get_trace_entry(tr, data);
1098 tracing_generic_entry_update(entry, flags);
1099 entry->type = TRACE_TIMER_TRIG;
1100 entry->timer.ip = ip;
1101 entry->timer.expire = *expired;
1102 entry->timer.timer = timer;
1103 }
1104
1105 void tracing_event_timestamp(struct trace_array *tr,
1106 struct trace_array_cpu *data,
1107 unsigned long flags,
1108 unsigned long ip,
1109 ktime_t *now)
1110 {
1111 struct trace_entry *entry;
1112
1113 entry = tracing_get_trace_entry(tr, data);
1114 tracing_generic_entry_update(entry, flags);
1115 entry->type = TRACE_TIMESTAMP;
1116 entry->timestamp.ip = ip;
1117 entry->timestamp.now = *now;
1118 }
1119
1120 void tracing_event_task_activate(struct trace_array *tr,
1121 struct trace_array_cpu *data,
1122 unsigned long flags,
1123 unsigned long ip,
1124 struct task_struct *p,
1125 int task_cpu)
1126 {
1127 struct trace_entry *entry;
1128
1129 entry = tracing_get_trace_entry(tr, data);
1130 tracing_generic_entry_update(entry, flags);
1131 entry->type = TRACE_TASK_ACT;
1132 entry->task.ip = ip;
1133 entry->task.pid = p->pid;
1134 entry->task.prio = p->prio;
1135 entry->task.cpu = task_cpu;
1136 }
1137
1138 void tracing_event_task_deactivate(struct trace_array *tr,
1139 struct trace_array_cpu *data,
1140 unsigned long flags,
1141 unsigned long ip,
1142 struct task_struct *p,
1143 int task_cpu)
1144 {
1145 struct trace_entry *entry;
1146
1147 entry = tracing_get_trace_entry(tr, data);
1148 tracing_generic_entry_update(entry, flags);
1149 entry->type = TRACE_TASK_DEACT;
1150 entry->task.ip = ip;
1151 entry->task.pid = p->pid;
1152 entry->task.prio = p->prio;
1153 entry->task.cpu = task_cpu;
1154 }
1155
1156 void tracing_event_syscall(struct trace_array *tr,
1157 struct trace_array_cpu *data,
1158 unsigned long flags,
1159 unsigned long ip,
1160 unsigned long nr,
1161 unsigned long p1,
1162 unsigned long p2,
1163 unsigned long p3)
1164 {
1165 struct trace_entry *entry;
1166
1167 entry = tracing_get_trace_entry(tr, data);
1168 tracing_generic_entry_update(entry, flags);
1169 entry->type = TRACE_SYSCALL;
1170 entry->syscall.ip = ip;
1171 entry->syscall.nr = nr;
1172 entry->syscall.p1 = p1;
1173 entry->syscall.p2 = p2;
1174 entry->syscall.p3 = p3;
1175 }
1176
1177 void tracing_event_sysret(struct trace_array *tr,
1178 struct trace_array_cpu *data,
1179 unsigned long flags,
1180 unsigned long ip,
1181 unsigned long ret)
1182 {
1183 struct trace_entry *entry;
1184
1185 entry = tracing_get_trace_entry(tr, data);
1186 tracing_generic_entry_update(entry, flags);
1187 entry->type = TRACE_SYSRET;
1188 entry->sysret.ip = ip;
1189 entry->sysret.ret = ret;
1190 }
1191
1192 #ifdef CONFIG_FTRACE
1193 static void
1194 function_trace_call(unsigned long ip, unsigned long parent_ip)
1195 {
1196 struct trace_array *tr = &global_trace;
1197 struct trace_array_cpu *data;
1198 unsigned long flags;
1199 long disabled;
1200 int cpu, resched;
1201
1202 if (unlikely(!tracer_enabled))
1203 return;
1204
1205 resched = need_resched();
1206 preempt_disable_notrace();
1207 cpu = raw_smp_processor_id();
1208 data = tr->data[cpu];
1209 disabled = atomic_inc_return(&data->disabled);
1210
1211 if (likely(disabled == 1)) {
1212 local_save_flags(flags);
1213 trace_function(tr, data, ip, parent_ip, flags);
1214 }
1215
1216 atomic_dec(&data->disabled);
1217
1218 /*
1219 * To prevent recursion with schedule(), we look at the
1220 * resched flag before disabling preemption. If it is already
1221 * set, then we may be just calling schedule, and we don't
1222 * want to call schedule again. But if it was not set, then
1223 * it is fine to call schedule() if we need to.
1224 */
1225 if (resched)
1226 preempt_enable_no_resched_notrace();
1227 else
1228 preempt_enable_notrace();
1229 }
1230
1231 static struct ftrace_ops trace_ops __read_mostly =
1232 {
1233 .func = function_trace_call,
1234 };
1235
1236 void tracing_start_function_trace(void)
1237 {
1238 register_ftrace_function(&trace_ops);
1239 tracing_record_cmdline(current);
1240 }
1241
1242 void tracing_stop_function_trace(void)
1243 {
1244 tracing_record_cmdline(current);
1245 unregister_ftrace_function(&trace_ops);
1246 }
1247 #endif
1248
1249 enum trace_file_type {
1250 TRACE_FILE_LAT_FMT = 1,
1251 };
1252
1253 static struct trace_entry *
1254 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1255 struct trace_iterator *iter, int cpu)
1256 {
1257 struct page *page;
1258 struct trace_entry *array;
1259
1260 if (iter->next_idx[cpu] >= tr->entries ||
1261 iter->next_idx[cpu] >= data->trace_idx ||
1262 (data->trace_head == data->trace_tail &&
1263 data->trace_head_idx == data->trace_tail_idx))
1264 return NULL;
1265
1266 if (!iter->next_page[cpu]) {
1267 /* Initialize the iterator for this cpu trace buffer */
1268 WARN_ON(!data->trace_tail);
1269 page = virt_to_page(data->trace_tail);
1270 iter->next_page[cpu] = &page->lru;
1271 iter->next_page_idx[cpu] = data->trace_tail_idx;
1272 }
1273
1274 page = list_entry(iter->next_page[cpu], struct page, lru);
1275 BUG_ON(&data->trace_pages == &page->lru);
1276
1277 array = page_address(page);
1278
1279 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
1280 return &array[iter->next_page_idx[cpu]];
1281 }
1282
1283 static struct trace_entry *
1284 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1285 {
1286 struct trace_array *tr = iter->tr;
1287 struct trace_entry *ent, *next = NULL;
1288 int next_cpu = -1;
1289 int cpu;
1290
1291 for_each_tracing_cpu(cpu) {
1292 if (!head_page(tr->data[cpu]))
1293 continue;
1294 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1295 /*
1296 * Pick the entry with the smallest timestamp:
1297 */
1298 if (ent && (!next || ent->t < next->t)) {
1299 next = ent;
1300 next_cpu = cpu;
1301 }
1302 }
1303
1304 if (ent_cpu)
1305 *ent_cpu = next_cpu;
1306
1307 return next;
1308 }
1309
1310 static void trace_iterator_increment(struct trace_iterator *iter)
1311 {
1312 iter->idx++;
1313 iter->next_idx[iter->cpu]++;
1314 iter->next_page_idx[iter->cpu]++;
1315
1316 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1317 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1318
1319 iter->next_page_idx[iter->cpu] = 0;
1320 iter->next_page[iter->cpu] =
1321 trace_next_list(data, iter->next_page[iter->cpu]);
1322 }
1323 }
1324
1325 static void trace_consume(struct trace_iterator *iter)
1326 {
1327 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1328
1329 data->trace_tail_idx++;
1330 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1331 data->trace_tail = trace_next_page(data, data->trace_tail);
1332 data->trace_tail_idx = 0;
1333 }
1334
1335 /* Check if we empty it, then reset the index */
1336 if (data->trace_head == data->trace_tail &&
1337 data->trace_head_idx == data->trace_tail_idx)
1338 data->trace_idx = 0;
1339 }
1340
1341 static void *find_next_entry_inc(struct trace_iterator *iter)
1342 {
1343 struct trace_entry *next;
1344 int next_cpu = -1;
1345
1346 next = find_next_entry(iter, &next_cpu);
1347
1348 iter->prev_ent = iter->ent;
1349 iter->prev_cpu = iter->cpu;
1350
1351 iter->ent = next;
1352 iter->cpu = next_cpu;
1353
1354 if (next)
1355 trace_iterator_increment(iter);
1356
1357 return next ? iter : NULL;
1358 }
1359
1360 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1361 {
1362 struct trace_iterator *iter = m->private;
1363 void *last_ent = iter->ent;
1364 int i = (int)*pos;
1365 void *ent;
1366
1367 (*pos)++;
1368
1369 /* can't go backwards */
1370 if (iter->idx > i)
1371 return NULL;
1372
1373 if (iter->idx < 0)
1374 ent = find_next_entry_inc(iter);
1375 else
1376 ent = iter;
1377
1378 while (ent && iter->idx < i)
1379 ent = find_next_entry_inc(iter);
1380
1381 iter->pos = *pos;
1382
1383 if (last_ent && !ent)
1384 seq_puts(m, "\n\nvim:ft=help\n");
1385
1386 return ent;
1387 }
1388
1389 static void *s_start(struct seq_file *m, loff_t *pos)
1390 {
1391 struct trace_iterator *iter = m->private;
1392 void *p = NULL;
1393 loff_t l = 0;
1394 int i;
1395
1396 mutex_lock(&trace_types_lock);
1397
1398 if (!current_trace || current_trace != iter->trace) {
1399 mutex_unlock(&trace_types_lock);
1400 return NULL;
1401 }
1402
1403 atomic_inc(&trace_record_cmdline_disabled);
1404
1405 /* let the tracer grab locks here if needed */
1406 if (current_trace->start)
1407 current_trace->start(iter);
1408
1409 if (*pos != iter->pos) {
1410 iter->ent = NULL;
1411 iter->cpu = 0;
1412 iter->idx = -1;
1413 iter->prev_ent = NULL;
1414 iter->prev_cpu = -1;
1415
1416 for_each_tracing_cpu(i) {
1417 iter->next_idx[i] = 0;
1418 iter->next_page[i] = NULL;
1419 }
1420
1421 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1422 ;
1423
1424 } else {
1425 l = *pos - 1;
1426 p = s_next(m, p, &l);
1427 }
1428
1429 return p;
1430 }
1431
1432 static void s_stop(struct seq_file *m, void *p)
1433 {
1434 struct trace_iterator *iter = m->private;
1435
1436 atomic_dec(&trace_record_cmdline_disabled);
1437
1438 /* let the tracer release locks here if needed */
1439 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1440 iter->trace->stop(iter);
1441
1442 mutex_unlock(&trace_types_lock);
1443 }
1444
1445 static int
1446 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1447 {
1448 #ifdef CONFIG_KALLSYMS
1449 char str[KSYM_SYMBOL_LEN];
1450
1451 kallsyms_lookup(address, NULL, NULL, NULL, str);
1452
1453 return trace_seq_printf(s, fmt, str);
1454 #endif
1455 return 1;
1456 }
1457
1458 static int
1459 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1460 unsigned long address)
1461 {
1462 #ifdef CONFIG_KALLSYMS
1463 char str[KSYM_SYMBOL_LEN];
1464
1465 sprint_symbol(str, address);
1466 return trace_seq_printf(s, fmt, str);
1467 #endif
1468 return 1;
1469 }
1470
1471 #ifndef CONFIG_64BIT
1472 # define IP_FMT "%08lx"
1473 #else
1474 # define IP_FMT "%016lx"
1475 #endif
1476
1477 static int
1478 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1479 {
1480 int ret;
1481
1482 if (!ip)
1483 return trace_seq_printf(s, "");
1484
1485 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1486 ret = seq_print_sym_offset(s, "%s", ip);
1487 else
1488 ret = seq_print_sym_short(s, "%s", ip);
1489
1490 if (!ret)
1491 return 0;
1492
1493 if (sym_flags & TRACE_ITER_SYM_ADDR)
1494 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1495 return ret;
1496 }
1497
1498 static void print_lat_help_header(struct seq_file *m)
1499 {
1500 seq_puts(m, "# _------=> CPU# \n");
1501 seq_puts(m, "# / _-----=> irqs-off \n");
1502 seq_puts(m, "# | / _----=> need-resched \n");
1503 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1504 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1505 seq_puts(m, "# |||| / \n");
1506 seq_puts(m, "# ||||| delay \n");
1507 seq_puts(m, "# cmd pid ||||| time | caller \n");
1508 seq_puts(m, "# \\ / ||||| \\ | / \n");
1509 }
1510
1511 static void print_func_help_header(struct seq_file *m)
1512 {
1513 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1514 seq_puts(m, "# | | | | |\n");
1515 }
1516
1517
1518 static void
1519 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1520 {
1521 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1522 struct trace_array *tr = iter->tr;
1523 struct trace_array_cpu *data = tr->data[tr->cpu];
1524 struct tracer *type = current_trace;
1525 unsigned long total = 0;
1526 unsigned long entries = 0;
1527 int cpu;
1528 const char *name = "preemption";
1529
1530 if (type)
1531 name = type->name;
1532
1533 for_each_tracing_cpu(cpu) {
1534 if (head_page(tr->data[cpu])) {
1535 total += tr->data[cpu]->trace_idx;
1536 if (tr->data[cpu]->trace_idx > tr->entries)
1537 entries += tr->entries;
1538 else
1539 entries += tr->data[cpu]->trace_idx;
1540 }
1541 }
1542
1543 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1544 name, UTS_RELEASE);
1545 seq_puts(m, "-----------------------------------"
1546 "---------------------------------\n");
1547 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1548 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1549 nsecs_to_usecs(data->saved_latency),
1550 entries,
1551 total,
1552 tr->cpu,
1553 #if defined(CONFIG_PREEMPT_NONE)
1554 "server",
1555 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1556 "desktop",
1557 #elif defined(CONFIG_PREEMPT_DESKTOP)
1558 "preempt",
1559 #else
1560 "unknown",
1561 #endif
1562 /* These are reserved for later use */
1563 0, 0, 0, 0);
1564 #ifdef CONFIG_SMP
1565 seq_printf(m, " #P:%d)\n", num_online_cpus());
1566 #else
1567 seq_puts(m, ")\n");
1568 #endif
1569 seq_puts(m, " -----------------\n");
1570 seq_printf(m, " | task: %.16s-%d "
1571 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1572 data->comm, data->pid, data->uid, data->nice,
1573 data->policy, data->rt_priority);
1574 seq_puts(m, " -----------------\n");
1575
1576 if (data->critical_start) {
1577 seq_puts(m, " => started at: ");
1578 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1579 trace_print_seq(m, &iter->seq);
1580 seq_puts(m, "\n => ended at: ");
1581 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1582 trace_print_seq(m, &iter->seq);
1583 seq_puts(m, "\n");
1584 }
1585
1586 seq_puts(m, "\n");
1587 }
1588
1589 static void
1590 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1591 {
1592 int hardirq, softirq;
1593 char *comm;
1594
1595 comm = trace_find_cmdline(entry->pid);
1596
1597 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1598 trace_seq_printf(s, "%d", cpu);
1599 trace_seq_printf(s, "%c%c",
1600 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1601 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1602
1603 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1604 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1605 if (hardirq && softirq) {
1606 trace_seq_putc(s, 'H');
1607 } else {
1608 if (hardirq) {
1609 trace_seq_putc(s, 'h');
1610 } else {
1611 if (softirq)
1612 trace_seq_putc(s, 's');
1613 else
1614 trace_seq_putc(s, '.');
1615 }
1616 }
1617
1618 if (entry->preempt_count)
1619 trace_seq_printf(s, "%x", entry->preempt_count);
1620 else
1621 trace_seq_puts(s, ".");
1622 }
1623
1624 unsigned long preempt_mark_thresh = 100;
1625
1626 static void
1627 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1628 unsigned long rel_usecs)
1629 {
1630 trace_seq_printf(s, " %4lldus", abs_usecs);
1631 if (rel_usecs > preempt_mark_thresh)
1632 trace_seq_puts(s, "!: ");
1633 else if (rel_usecs > 1)
1634 trace_seq_puts(s, "+: ");
1635 else
1636 trace_seq_puts(s, " : ");
1637 }
1638
1639 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1640
1641 extern unsigned long sys_call_table[NR_syscalls];
1642
1643 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86)
1644 extern unsigned long ia32_sys_call_table[], ia32_syscall_end[];
1645 # define IA32_NR_syscalls (ia32_syscall_end - ia32_sys_call_table)
1646 #endif
1647
1648 static int
1649 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1650 {
1651 struct trace_seq *s = &iter->seq;
1652 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1653 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1654 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1655 struct trace_entry *entry = iter->ent;
1656 unsigned long abs_usecs;
1657 unsigned long rel_usecs;
1658 unsigned long nr;
1659 char *comm;
1660 int S, T;
1661 int i;
1662 unsigned state;
1663
1664 if (!next_entry)
1665 next_entry = entry;
1666 rel_usecs = ns2usecs(next_entry->t - entry->t);
1667 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1668
1669 if (verbose) {
1670 comm = trace_find_cmdline(entry->pid);
1671 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1672 " %ld.%03ldms (+%ld.%03ldms): ",
1673 comm,
1674 entry->pid, cpu, entry->flags,
1675 entry->preempt_count, trace_idx,
1676 ns2usecs(entry->t),
1677 abs_usecs/1000,
1678 abs_usecs % 1000, rel_usecs/1000,
1679 rel_usecs % 1000);
1680 } else {
1681 lat_print_generic(s, entry, cpu);
1682 lat_print_timestamp(s, abs_usecs, rel_usecs);
1683 }
1684 switch (entry->type) {
1685 case TRACE_FN:
1686 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1687 trace_seq_puts(s, " (");
1688 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1689 trace_seq_puts(s, ")\n");
1690 break;
1691 case TRACE_CTX:
1692 case TRACE_WAKE:
1693 T = entry->ctx.next_state < sizeof(state_to_char) ?
1694 state_to_char[entry->ctx.next_state] : 'X';
1695
1696 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1697 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1698 comm = trace_find_cmdline(entry->ctx.next_pid);
1699 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1700 entry->ctx.prev_pid,
1701 entry->ctx.prev_prio,
1702 S, entry->type == TRACE_CTX ? "==>" : " +",
1703 entry->ctx.next_pid,
1704 entry->ctx.next_prio,
1705 T, comm);
1706 break;
1707 case TRACE_SPECIAL:
1708 trace_seq_printf(s, "# %ld %ld %ld\n",
1709 entry->special.arg1,
1710 entry->special.arg2,
1711 entry->special.arg3);
1712 break;
1713 case TRACE_STACK:
1714 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1715 if (i)
1716 trace_seq_puts(s, " <= ");
1717 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1718 }
1719 trace_seq_puts(s, "\n");
1720 break;
1721 case TRACE_IRQ:
1722 seq_print_ip_sym(s, entry->irq.ip, sym_flags);
1723 if (entry->irq.irq >= 0)
1724 trace_seq_printf(s, " %d ", entry->irq.irq);
1725 if (entry->irq.usermode)
1726 trace_seq_puts(s, " (usermode)\n ");
1727 else {
1728 trace_seq_puts(s, " (");
1729 seq_print_ip_sym(s, entry->irq.ret_ip, sym_flags);
1730 trace_seq_puts(s, ")\n");
1731 }
1732 break;
1733 case TRACE_FAULT:
1734 seq_print_ip_sym(s, entry->fault.ip, sym_flags);
1735 trace_seq_printf(s, " %lx ", entry->fault.errorcode);
1736 trace_seq_puts(s, " (");
1737 seq_print_ip_sym(s, entry->fault.ret_ip, sym_flags);
1738 trace_seq_puts(s, ")");
1739 trace_seq_printf(s, " [%lx]\n", entry->fault.address);
1740 break;
1741 case TRACE_TIMER_SET:
1742 seq_print_ip_sym(s, entry->timer.ip, sym_flags);
1743 trace_seq_printf(s, " (%Ld) (%p)\n",
1744 entry->timer.expire, entry->timer.timer);
1745 break;
1746 case TRACE_TIMER_TRIG:
1747 seq_print_ip_sym(s, entry->timer.ip, sym_flags);
1748 trace_seq_printf(s, " (%Ld) (%p)\n",
1749 entry->timer.expire, entry->timer.timer);
1750 break;
1751 case TRACE_TIMESTAMP:
1752 seq_print_ip_sym(s, entry->timestamp.ip, sym_flags);
1753 trace_seq_printf(s, " (%Ld)\n",
1754 entry->timestamp.now.tv64);
1755 break;
1756 case TRACE_PROGRAM_EVENT:
1757 seq_print_ip_sym(s, entry->program.ip, sym_flags);
1758 trace_seq_printf(s, " (%Ld) (%Ld)\n",
1759 entry->program.expire, entry->program.delta);
1760 break;
1761 case TRACE_TASK_ACT:
1762 seq_print_ip_sym(s, entry->task.ip, sym_flags);
1763 comm = trace_find_cmdline(entry->task.pid);
1764 trace_seq_printf(s, " %s %d %d [%d]\n",
1765 comm, entry->task.pid,
1766 entry->task.prio, entry->task.cpu);
1767 break;
1768 case TRACE_TASK_DEACT:
1769 seq_print_ip_sym(s, entry->task.ip, sym_flags);
1770 comm = trace_find_cmdline(entry->task.pid);
1771 trace_seq_printf(s, " %s %d %d [%d]\n",
1772 comm, entry->task.pid,
1773 entry->task.prio, entry->task.cpu);
1774 break;
1775 case TRACE_SYSCALL:
1776 seq_print_ip_sym(s, entry->syscall.ip, sym_flags);
1777 nr = entry->syscall.nr;
1778 trace_seq_putc(s, ' ');
1779 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86)
1780 if (nr & 0x80000000) {
1781 nr &= ~0x80000000;
1782 if (nr < IA32_NR_syscalls)
1783 seq_print_ip_sym(s, ia32_sys_call_table[nr], 0);
1784 else
1785 trace_seq_printf(s, "<badsys(%lu)>", nr);
1786 } else
1787 #endif
1788 if (nr < NR_syscalls)
1789 seq_print_ip_sym(s, sys_call_table[nr], 0);
1790 else
1791 trace_seq_printf(s, "<badsys(%lu)>", nr);
1792
1793 trace_seq_printf(s, " (%lx %lx %lx)\n",
1794 entry->syscall.p1,
1795 entry->syscall.p2,
1796 entry->syscall.p3);
1797 break;
1798 case TRACE_SYSRET:
1799 seq_print_ip_sym(s, entry->sysret.ip, sym_flags);
1800 trace_seq_printf(s, " < (%ld)\n",
1801 entry->sysret.ret);
1802 break;
1803 default:
1804 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1805 }
1806 return 1;
1807 }
1808
1809 static int print_trace_fmt(struct trace_iterator *iter)
1810 {
1811 struct trace_seq *s = &iter->seq;
1812 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1813 struct trace_entry *entry;
1814 unsigned long usec_rem;
1815 unsigned long long t;
1816 unsigned long secs;
1817 long nr;
1818 char *comm;
1819 int ret;
1820 int S, T;
1821 int i;
1822
1823 entry = iter->ent;
1824
1825 comm = trace_find_cmdline(iter->ent->pid);
1826
1827 t = ns2usecs(entry->t);
1828 usec_rem = do_div(t, 1000000ULL);
1829 secs = (unsigned long)t;
1830
1831 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1832 if (!ret)
1833 return 0;
1834 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1835 if (!ret)
1836 return 0;
1837 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1838 if (!ret)
1839 return 0;
1840
1841 switch (entry->type) {
1842 case TRACE_FN:
1843 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1844 if (!ret)
1845 return 0;
1846 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1847 entry->fn.parent_ip) {
1848 ret = trace_seq_printf(s, " <-");
1849 if (!ret)
1850 return 0;
1851 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1852 sym_flags);
1853 if (!ret)
1854 return 0;
1855 }
1856 ret = trace_seq_printf(s, "\n");
1857 if (!ret)
1858 return 0;
1859 break;
1860 case TRACE_CTX:
1861 case TRACE_WAKE:
1862 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1863 state_to_char[entry->ctx.prev_state] : 'X';
1864 T = entry->ctx.next_state < sizeof(state_to_char) ?
1865 state_to_char[entry->ctx.next_state] : 'X';
1866 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
1867 entry->ctx.prev_pid,
1868 entry->ctx.prev_prio,
1869 S,
1870 entry->type == TRACE_CTX ? "==>" : " +",
1871 entry->ctx.next_pid,
1872 entry->ctx.next_prio,
1873 T);
1874 if (!ret)
1875 return 0;
1876 break;
1877 case TRACE_SPECIAL:
1878 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1879 entry->special.arg1,
1880 entry->special.arg2,
1881 entry->special.arg3);
1882 if (!ret)
1883 return 0;
1884 break;
1885 case TRACE_STACK:
1886 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1887 if (i) {
1888 ret = trace_seq_puts(s, " <= ");
1889 if (!ret)
1890 return 0;
1891 }
1892 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1893 sym_flags);
1894 if (!ret)
1895 return 0;
1896 }
1897 ret = trace_seq_puts(s, "\n");
1898 if (!ret)
1899 return 0;
1900 break;
1901 case TRACE_IRQ:
1902 seq_print_ip_sym(s, entry->irq.ip, sym_flags);
1903 if (entry->irq.irq >= 0)
1904 trace_seq_printf(s, " %d ", entry->irq.irq);
1905 if (entry->irq.usermode)
1906 trace_seq_puts(s, " (usermode)\n ");
1907 else {
1908 trace_seq_puts(s, " (");
1909 seq_print_ip_sym(s, entry->irq.ret_ip, sym_flags);
1910 trace_seq_puts(s, ")\n");
1911 }
1912 break;
1913 case TRACE_FAULT:
1914 seq_print_ip_sym(s, entry->fault.ip, sym_flags);
1915 trace_seq_printf(s, " %lx ", entry->fault.errorcode);
1916 trace_seq_puts(s, " (");
1917 seq_print_ip_sym(s, entry->fault.ret_ip, sym_flags);
1918 trace_seq_puts(s, ")");
1919 trace_seq_printf(s, " [%lx]\n", entry->fault.address);
1920 break;
1921 case TRACE_TIMER_SET:
1922 seq_print_ip_sym(s, entry->timer.ip, sym_flags);
1923 trace_seq_printf(s, " (%Ld) (%p)\n",
1924 entry->timer.expire, entry->timer.timer);
1925 break;
1926 case TRACE_TIMER_TRIG:
1927 seq_print_ip_sym(s, entry->timer.ip, sym_flags);
1928 trace_seq_printf(s, " (%Ld) (%p)\n",
1929 entry->timer.expire, entry->timer.timer);
1930 break;
1931 case TRACE_TIMESTAMP:
1932 seq_print_ip_sym(s, entry->timestamp.ip, sym_flags);
1933 trace_seq_printf(s, " (%Ld)\n",
1934 entry->timestamp.now.tv64);
1935 break;
1936 case TRACE_PROGRAM_EVENT:
1937 seq_print_ip_sym(s, entry->program.ip, sym_flags);
1938 trace_seq_printf(s, " (%Ld) (%Ld)\n",
1939 entry->program.expire, entry->program.delta);
1940 break;
1941 case TRACE_TASK_ACT:
1942 seq_print_ip_sym(s, entry->task.ip, sym_flags);
1943 comm = trace_find_cmdline(entry->task.pid);
1944 trace_seq_printf(s, " %s %d %d [%d]\n",
1945 comm, entry->task.pid,
1946 entry->task.prio, entry->task.cpu);
1947 break;
1948 case TRACE_TASK_DEACT:
1949 seq_print_ip_sym(s, entry->task.ip, sym_flags);
1950 comm = trace_find_cmdline(entry->task.pid);
1951 trace_seq_printf(s, " %s %d %d [%d]\n",
1952 comm, entry->task.pid,
1953 entry->task.prio, entry->task.cpu);
1954 break;
1955 case TRACE_SYSCALL:
1956 seq_print_ip_sym(s, entry->syscall.ip, sym_flags);
1957 nr = entry->syscall.nr;
1958 trace_seq_putc(s, ' ');
1959 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86)
1960 if (nr & 0x80000000) {
1961 nr &= ~0x80000000;
1962 if (nr < IA32_NR_syscalls)
1963 seq_print_ip_sym(s, ia32_sys_call_table[nr], 0);
1964 else
1965 trace_seq_printf(s, "<badsys(%lu)>", nr);
1966 } else
1967 #endif
1968 if (nr < NR_syscalls)
1969 seq_print_ip_sym(s, sys_call_table[nr], 0);
1970 else
1971 trace_seq_printf(s, "<badsys(%lu)>", nr);
1972
1973 trace_seq_printf(s, " (%lx %lx %lx)\n",
1974 entry->syscall.p1,
1975 entry->syscall.p2,
1976 entry->syscall.p3);
1977 break;
1978 case TRACE_SYSRET:
1979 seq_print_ip_sym(s, entry->sysret.ip, sym_flags);
1980 trace_seq_printf(s, "< (%ld)\n",
1981 entry->sysret.ret);
1982 break;
1983 default:
1984 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1985 }
1986 return 1;
1987 }
1988
1989 static int print_raw_fmt(struct trace_iterator *iter)
1990 {
1991 struct trace_seq *s = &iter->seq;
1992 struct trace_entry *entry;
1993 int ret;
1994 int S, T;
1995
1996 entry = iter->ent;
1997
1998 ret = trace_seq_printf(s, "%d %d %llu ",
1999 entry->pid, iter->cpu, entry->t);
2000 if (!ret)
2001 return 0;
2002
2003 switch (entry->type) {
2004 case TRACE_FN:
2005 ret = trace_seq_printf(s, "%x %x\n",
2006 entry->fn.ip, entry->fn.parent_ip);
2007 if (!ret)
2008 return 0;
2009 break;
2010 case TRACE_CTX:
2011 case TRACE_WAKE:
2012 S = entry->ctx.prev_state < sizeof(state_to_char) ?
2013 state_to_char[entry->ctx.prev_state] : 'X';
2014 T = entry->ctx.next_state < sizeof(state_to_char) ?
2015 state_to_char[entry->ctx.next_state] : 'X';
2016 if (entry->type == TRACE_WAKE)
2017 S = '+';
2018 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
2019 entry->ctx.prev_pid,
2020 entry->ctx.prev_prio,
2021 S,
2022 entry->ctx.next_pid,
2023 entry->ctx.next_prio,
2024 T);
2025 if (!ret)
2026 return 0;
2027 break;
2028 case TRACE_SPECIAL:
2029 case TRACE_STACK:
2030 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
2031 entry->special.arg1,
2032 entry->special.arg2,
2033 entry->special.arg3);
2034 if (!ret)
2035 return 0;
2036 break;
2037 }
2038 return 1;
2039 }
2040
2041 #define SEQ_PUT_FIELD_RET(s, x) \
2042 do { \
2043 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
2044 return 0; \
2045 } while (0)
2046
2047 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
2048 do { \
2049 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
2050 return 0; \
2051 } while (0)
2052
2053 static int print_hex_fmt(struct trace_iterator *iter)
2054 {
2055 struct trace_seq *s = &iter->seq;
2056 unsigned char newline = '\n';
2057 struct trace_entry *entry;
2058 int S, T;
2059
2060 entry = iter->ent;
2061
2062 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2063 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2064 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
2065
2066 switch (entry->type) {
2067 case TRACE_FN:
2068 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
2069 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
2070 break;
2071 case TRACE_CTX:
2072 case TRACE_WAKE:
2073 S = entry->ctx.prev_state < sizeof(state_to_char) ?
2074 state_to_char[entry->ctx.prev_state] : 'X';
2075 T = entry->ctx.next_state < sizeof(state_to_char) ?
2076 state_to_char[entry->ctx.next_state] : 'X';
2077 if (entry->type == TRACE_WAKE)
2078 S = '+';
2079 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
2080 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
2081 SEQ_PUT_HEX_FIELD_RET(s, S);
2082 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
2083 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
2084 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
2085 SEQ_PUT_HEX_FIELD_RET(s, T);
2086 break;
2087 case TRACE_SPECIAL:
2088 case TRACE_STACK:
2089 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
2090 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
2091 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
2092 break;
2093 }
2094 SEQ_PUT_FIELD_RET(s, newline);
2095
2096 return 1;
2097 }
2098
2099 static int print_bin_fmt(struct trace_iterator *iter)
2100 {
2101 struct trace_seq *s = &iter->seq;
2102 struct trace_entry *entry;
2103
2104 entry = iter->ent;
2105
2106 SEQ_PUT_FIELD_RET(s, entry->pid);
2107 SEQ_PUT_FIELD_RET(s, entry->cpu);
2108 SEQ_PUT_FIELD_RET(s, entry->t);
2109
2110 switch (entry->type) {
2111 case TRACE_FN:
2112 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
2113 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
2114 break;
2115 case TRACE_CTX:
2116 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
2117 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
2118 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
2119 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
2120 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
2121 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
2122 break;
2123 case TRACE_SPECIAL:
2124 case TRACE_STACK:
2125 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
2126 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
2127 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
2128 break;
2129 }
2130 return 1;
2131 }
2132
2133 static int trace_empty(struct trace_iterator *iter)
2134 {
2135 struct trace_array_cpu *data;
2136 int cpu;
2137
2138 for_each_tracing_cpu(cpu) {
2139 data = iter->tr->data[cpu];
2140
2141 if (head_page(data) && data->trace_idx &&
2142 (data->trace_tail != data->trace_head ||
2143 data->trace_tail_idx != data->trace_head_idx))
2144 return 0;
2145 }
2146 return 1;
2147 }
2148
2149 static int print_trace_line(struct trace_iterator *iter)
2150 {
2151 if (iter->trace && iter->trace->print_line)
2152 return iter->trace->print_line(iter);
2153
2154 if (trace_flags & TRACE_ITER_BIN)
2155 return print_bin_fmt(iter);
2156
2157 if (trace_flags & TRACE_ITER_HEX)
2158 return print_hex_fmt(iter);
2159
2160 if (trace_flags & TRACE_ITER_RAW)
2161 return print_raw_fmt(iter);
2162
2163 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2164 return print_lat_fmt(iter, iter->idx, iter->cpu);
2165
2166 return print_trace_fmt(iter);
2167 }
2168
2169 static int s_show(struct seq_file *m, void *v)
2170 {
2171 struct trace_iterator *iter = v;
2172
2173 if (iter->ent == NULL) {
2174 if (iter->tr) {
2175 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2176 seq_puts(m, "#\n");
2177 }
2178 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2179 /* print nothing if the buffers are empty */
2180 if (trace_empty(iter))
2181 return 0;
2182 print_trace_header(m, iter);
2183 if (!(trace_flags & TRACE_ITER_VERBOSE))
2184 print_lat_help_header(m);
2185 } else {
2186 if (!(trace_flags & TRACE_ITER_VERBOSE))
2187 print_func_help_header(m);
2188 }
2189 } else {
2190 print_trace_line(iter);
2191 trace_print_seq(m, &iter->seq);
2192 }
2193
2194 return 0;
2195 }
2196
2197 static struct seq_operations tracer_seq_ops = {
2198 .start = s_start,
2199 .next = s_next,
2200 .stop = s_stop,
2201 .show = s_show,
2202 };
2203
2204 static struct trace_iterator *
2205 __tracing_open(struct inode *inode, struct file *file, int *ret)
2206 {
2207 struct trace_iterator *iter;
2208
2209 if (tracing_disabled) {
2210 *ret = -ENODEV;
2211 return NULL;
2212 }
2213
2214 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2215 if (!iter) {
2216 *ret = -ENOMEM;
2217 goto out;
2218 }
2219
2220 mutex_lock(&trace_types_lock);
2221 if (current_trace && current_trace->print_max)
2222 iter->tr = &max_tr;
2223 else
2224 iter->tr = inode->i_private;
2225 iter->trace = current_trace;
2226 iter->pos = -1;
2227
2228 /* TODO stop tracer */
2229 *ret = seq_open(file, &tracer_seq_ops);
2230 if (!*ret) {
2231 struct seq_file *m = file->private_data;
2232 m->private = iter;
2233
2234 /* stop the trace while dumping */
2235 if (iter->tr->ctrl)
2236 tracer_enabled = 0;
2237
2238 if (iter->trace && iter->trace->open)
2239 iter->trace->open(iter);
2240 } else {
2241 kfree(iter);
2242 iter = NULL;
2243 }
2244 mutex_unlock(&trace_types_lock);
2245
2246 out:
2247 return iter;
2248 }
2249
2250 int tracing_open_generic(struct inode *inode, struct file *filp)
2251 {
2252 if (tracing_disabled)
2253 return -ENODEV;
2254
2255 filp->private_data = inode->i_private;
2256 return 0;
2257 }
2258
2259 int tracing_release(struct inode *inode, struct file *file)
2260 {
2261 struct seq_file *m = (struct seq_file *)file->private_data;
2262 struct trace_iterator *iter = m->private;
2263
2264 mutex_lock(&trace_types_lock);
2265 if (iter->trace && iter->trace->close)
2266 iter->trace->close(iter);
2267
2268 /* reenable tracing if it was previously enabled */
2269 if (iter->tr->ctrl)
2270 tracer_enabled = 1;
2271 mutex_unlock(&trace_types_lock);
2272
2273 seq_release(inode, file);
2274 kfree(iter);
2275 return 0;
2276 }
2277
2278 static int tracing_open(struct inode *inode, struct file *file)
2279 {
2280 int ret;
2281
2282 __tracing_open(inode, file, &ret);
2283
2284 return ret;
2285 }
2286
2287 static int tracing_lt_open(struct inode *inode, struct file *file)
2288 {
2289 struct trace_iterator *iter;
2290 int ret;
2291
2292 iter = __tracing_open(inode, file, &ret);
2293
2294 if (!ret)
2295 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2296
2297 return ret;
2298 }
2299
2300
2301 static void *
2302 t_next(struct seq_file *m, void *v, loff_t *pos)
2303 {
2304 struct tracer *t = m->private;
2305
2306 (*pos)++;
2307
2308 if (t)
2309 t = t->next;
2310
2311 m->private = t;
2312
2313 return t;
2314 }
2315
2316 static void *t_start(struct seq_file *m, loff_t *pos)
2317 {
2318 struct tracer *t = m->private;
2319 loff_t l = 0;
2320
2321 mutex_lock(&trace_types_lock);
2322 for (; t && l < *pos; t = t_next(m, t, &l))
2323 ;
2324
2325 return t;
2326 }
2327
2328 static void t_stop(struct seq_file *m, void *p)
2329 {
2330 mutex_unlock(&trace_types_lock);
2331 }
2332
2333 static int t_show(struct seq_file *m, void *v)
2334 {
2335 struct tracer *t = v;
2336
2337 if (!t)
2338 return 0;
2339
2340 seq_printf(m, "%s", t->name);
2341 if (t->next)
2342 seq_putc(m, ' ');
2343 else
2344 seq_putc(m, '\n');
2345
2346 return 0;
2347 }
2348
2349 static struct seq_operations show_traces_seq_ops = {
2350 .start = t_start,
2351 .next = t_next,
2352 .stop = t_stop,
2353 .show = t_show,
2354 };
2355
2356 static int show_traces_open(struct inode *inode, struct file *file)
2357 {
2358 int ret;
2359
2360 if (tracing_disabled)
2361 return -ENODEV;
2362
2363 ret = seq_open(file, &show_traces_seq_ops);
2364 if (!ret) {
2365 struct seq_file *m = file->private_data;
2366 m->private = trace_types;
2367 }
2368
2369 return ret;
2370 }
2371
2372 static struct file_operations tracing_fops = {
2373 .open = tracing_open,
2374 .read = seq_read,
2375 .llseek = seq_lseek,
2376 .release = tracing_release,
2377 };
2378
2379 static struct file_operations tracing_lt_fops = {
2380 .open = tracing_lt_open,
2381 .read = seq_read,
2382 .llseek = seq_lseek,
2383 .release = tracing_release,
2384 };
2385
2386 static struct file_operations show_traces_fops = {
2387 .open = show_traces_open,
2388 .read = seq_read,
2389 .release = seq_release,
2390 };
2391
2392 /*
2393 * Only trace on a CPU if the bitmask is set:
2394 */
2395 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2396
2397 /*
2398 * When tracing/tracing_cpu_mask is modified then this holds
2399 * the new bitmask we are about to install:
2400 */
2401 static cpumask_t tracing_cpumask_new;
2402
2403 /*
2404 * The tracer itself will not take this lock, but still we want
2405 * to provide a consistent cpumask to user-space:
2406 */
2407 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2408
2409 /*
2410 * Temporary storage for the character representation of the
2411 * CPU bitmask (and one more byte for the newline):
2412 */
2413 static char mask_str[NR_CPUS + 1];
2414
2415 static ssize_t
2416 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2417 size_t count, loff_t *ppos)
2418 {
2419 int len;
2420
2421 mutex_lock(&tracing_cpumask_update_lock);
2422
2423 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2424 if (count - len < 2) {
2425 count = -EINVAL;
2426 goto out_err;
2427 }
2428 len += sprintf(mask_str + len, "\n");
2429 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2430
2431 out_err:
2432 mutex_unlock(&tracing_cpumask_update_lock);
2433
2434 return count;
2435 }
2436
2437 static ssize_t
2438 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2439 size_t count, loff_t *ppos)
2440 {
2441 int err, cpu;
2442
2443 mutex_lock(&tracing_cpumask_update_lock);
2444 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2445 if (err)
2446 goto err_unlock;
2447
2448 raw_local_irq_disable();
2449 __raw_spin_lock(&ftrace_max_lock);
2450 for_each_tracing_cpu(cpu) {
2451 /*
2452 * Increase/decrease the disabled counter if we are
2453 * about to flip a bit in the cpumask:
2454 */
2455 if (cpu_isset(cpu, tracing_cpumask) &&
2456 !cpu_isset(cpu, tracing_cpumask_new)) {
2457 atomic_inc(&global_trace.data[cpu]->disabled);
2458 }
2459 if (!cpu_isset(cpu, tracing_cpumask) &&
2460 cpu_isset(cpu, tracing_cpumask_new)) {
2461 atomic_dec(&global_trace.data[cpu]->disabled);
2462 }
2463 }
2464 __raw_spin_unlock(&ftrace_max_lock);
2465 raw_local_irq_enable();
2466
2467 tracing_cpumask = tracing_cpumask_new;
2468
2469 mutex_unlock(&tracing_cpumask_update_lock);
2470
2471 return count;
2472
2473 err_unlock:
2474 mutex_unlock(&tracing_cpumask_update_lock);
2475
2476 return err;
2477 }
2478
2479 static struct file_operations tracing_cpumask_fops = {
2480 .open = tracing_open_generic,
2481 .read = tracing_cpumask_read,
2482 .write = tracing_cpumask_write,
2483 };
2484
2485 static ssize_t
2486 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2487 size_t cnt, loff_t *ppos)
2488 {
2489 char *buf;
2490 int r = 0;
2491 int len = 0;
2492 int i;
2493
2494 /* calulate max size */
2495 for (i = 0; trace_options[i]; i++) {
2496 len += strlen(trace_options[i]);
2497 len += 3; /* "no" and space */
2498 }
2499
2500 /* +2 for \n and \0 */
2501 buf = kmalloc(len + 2, GFP_KERNEL);
2502 if (!buf)
2503 return -ENOMEM;
2504
2505 for (i = 0; trace_options[i]; i++) {
2506 if (trace_flags & (1 << i))
2507 r += sprintf(buf + r, "%s ", trace_options[i]);
2508 else
2509 r += sprintf(buf + r, "no%s ", trace_options[i]);
2510 }
2511
2512 r += sprintf(buf + r, "\n");
2513 WARN_ON(r >= len + 2);
2514
2515 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2516
2517 kfree(buf);
2518
2519 return r;
2520 }
2521
2522 static ssize_t
2523 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2524 size_t cnt, loff_t *ppos)
2525 {
2526 char buf[64];
2527 char *cmp = buf;
2528 int neg = 0;
2529 int i;
2530
2531 if (cnt >= sizeof(buf))
2532 return -EINVAL;
2533
2534 if (copy_from_user(&buf, ubuf, cnt))
2535 return -EFAULT;
2536
2537 buf[cnt] = 0;
2538
2539 if (strncmp(buf, "no", 2) == 0) {
2540 neg = 1;
2541 cmp += 2;
2542 }
2543
2544 for (i = 0; trace_options[i]; i++) {
2545 int len = strlen(trace_options[i]);
2546
2547 if (strncmp(cmp, trace_options[i], len) == 0) {
2548 if (neg)
2549 trace_flags &= ~(1 << i);
2550 else
2551 trace_flags |= (1 << i);
2552 break;
2553 }
2554 }
2555 /*
2556 * If no option could be set, return an error:
2557 */
2558 if (!trace_options[i])
2559 return -EINVAL;
2560
2561 filp->f_pos += cnt;
2562
2563 return cnt;
2564 }
2565
2566 static struct file_operations tracing_iter_fops = {
2567 .open = tracing_open_generic,
2568 .read = tracing_iter_ctrl_read,
2569 .write = tracing_iter_ctrl_write,
2570 };
2571
2572 static const char readme_msg[] =
2573 "tracing mini-HOWTO:\n\n"
2574 "# mkdir /debug\n"
2575 "# mount -t debugfs nodev /debug\n\n"
2576 "# cat /debug/tracing/available_tracers\n"
2577 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2578 "# cat /debug/tracing/current_tracer\n"
2579 "none\n"
2580 "# echo sched_switch > /debug/tracing/current_tracer\n"
2581 "# cat /debug/tracing/current_tracer\n"
2582 "sched_switch\n"
2583 "# cat /debug/tracing/iter_ctrl\n"
2584 "noprint-parent nosym-offset nosym-addr noverbose\n"
2585 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2586 "# echo 1 > /debug/tracing/tracing_enabled\n"
2587 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2588 "echo 0 > /debug/tracing/tracing_enabled\n"
2589 ;
2590
2591 static ssize_t
2592 tracing_readme_read(struct file *filp, char __user *ubuf,
2593 size_t cnt, loff_t *ppos)
2594 {
2595 return simple_read_from_buffer(ubuf, cnt, ppos,
2596 readme_msg, strlen(readme_msg));
2597 }
2598
2599 static struct file_operations tracing_readme_fops = {
2600 .open = tracing_open_generic,
2601 .read = tracing_readme_read,
2602 };
2603
2604 static ssize_t
2605 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2606 size_t cnt, loff_t *ppos)
2607 {
2608 struct trace_array *tr = filp->private_data;
2609 char buf[64];
2610 int r;
2611
2612 r = sprintf(buf, "%ld\n", tr->ctrl);
2613 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2614 }
2615
2616 static ssize_t
2617 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2618 size_t cnt, loff_t *ppos)
2619 {
2620 struct trace_array *tr = filp->private_data;
2621 char buf[64];
2622 long val;
2623 int ret;
2624
2625 if (cnt >= sizeof(buf))
2626 return -EINVAL;
2627
2628 if (copy_from_user(&buf, ubuf, cnt))
2629 return -EFAULT;
2630
2631 buf[cnt] = 0;
2632
2633 ret = strict_strtoul(buf, 10, &val);
2634 if (ret < 0)
2635 return ret;
2636
2637 val = !!val;
2638
2639 mutex_lock(&trace_types_lock);
2640 if (tr->ctrl ^ val) {
2641 if (val)
2642 tracer_enabled = 1;
2643 else
2644 tracer_enabled = 0;
2645
2646 tr->ctrl = val;
2647
2648 if (current_trace && current_trace->ctrl_update)
2649 current_trace->ctrl_update(tr);
2650 }
2651 mutex_unlock(&trace_types_lock);
2652
2653 filp->f_pos += cnt;
2654
2655 return cnt;
2656 }
2657
2658 static ssize_t
2659 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2660 size_t cnt, loff_t *ppos)
2661 {
2662 char buf[max_tracer_type_len+2];
2663 int r;
2664
2665 mutex_lock(&trace_types_lock);
2666 if (current_trace)
2667 r = sprintf(buf, "%s\n", current_trace->name);
2668 else
2669 r = sprintf(buf, "\n");
2670 mutex_unlock(&trace_types_lock);
2671
2672 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2673 }
2674
2675 static ssize_t
2676 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2677 size_t cnt, loff_t *ppos)
2678 {
2679 struct trace_array *tr = &global_trace;
2680 struct tracer *t;
2681 char buf[max_tracer_type_len+1];
2682 int i;
2683
2684 if (cnt > max_tracer_type_len)
2685 cnt = max_tracer_type_len;
2686
2687 if (copy_from_user(&buf, ubuf, cnt))
2688 return -EFAULT;
2689
2690 buf[cnt] = 0;
2691
2692 /* strip ending whitespace. */
2693 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2694 buf[i] = 0;
2695
2696 mutex_lock(&trace_types_lock);
2697 for (t = trace_types; t; t = t->next) {
2698 if (strcmp(t->name, buf) == 0)
2699 break;
2700 }
2701 if (!t || t == current_trace)
2702 goto out;
2703
2704 if (current_trace && current_trace->reset)
2705 current_trace->reset(tr);
2706
2707 current_trace = t;
2708 if (t->init)
2709 t->init(tr);
2710
2711 out:
2712 mutex_unlock(&trace_types_lock);
2713
2714 filp->f_pos += cnt;
2715
2716 return cnt;
2717 }
2718
2719 static ssize_t
2720 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2721 size_t cnt, loff_t *ppos)
2722 {
2723 unsigned long *ptr = filp->private_data;
2724 char buf[64];
2725 int r;
2726
2727 r = snprintf(buf, sizeof(buf), "%ld\n",
2728 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2729 if (r > sizeof(buf))
2730 r = sizeof(buf);
2731 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2732 }
2733
2734 static ssize_t
2735 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2736 size_t cnt, loff_t *ppos)
2737 {
2738 long *ptr = filp->private_data;
2739 char buf[64];
2740 long val;
2741 int ret;
2742
2743 if (cnt >= sizeof(buf))
2744 return -EINVAL;
2745
2746 if (copy_from_user(&buf, ubuf, cnt))
2747 return -EFAULT;
2748
2749 buf[cnt] = 0;
2750
2751 ret = strict_strtoul(buf, 10, &val);
2752 if (ret < 0)
2753 return ret;
2754
2755 *ptr = val * 1000;
2756
2757 return cnt;
2758 }
2759
2760 static atomic_t tracing_reader;
2761
2762 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2763 {
2764 struct trace_iterator *iter;
2765
2766 if (tracing_disabled)
2767 return -ENODEV;
2768
2769 /* We only allow for reader of the pipe */
2770 if (atomic_inc_return(&tracing_reader) != 1) {
2771 atomic_dec(&tracing_reader);
2772 return -EBUSY;
2773 }
2774
2775 /* create a buffer to store the information to pass to userspace */
2776 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2777 if (!iter)
2778 return -ENOMEM;
2779
2780 mutex_lock(&trace_types_lock);
2781 iter->tr = &global_trace;
2782 iter->trace = current_trace;
2783 filp->private_data = iter;
2784
2785 if (iter->trace->pipe_open)
2786 iter->trace->pipe_open(iter);
2787 mutex_unlock(&trace_types_lock);
2788
2789 return 0;
2790 }
2791
2792 static int tracing_release_pipe(struct inode *inode, struct file *file)
2793 {
2794 struct trace_iterator *iter = file->private_data;
2795
2796 kfree(iter);
2797 atomic_dec(&tracing_reader);
2798
2799 return 0;
2800 }
2801
2802 static unsigned int
2803 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2804 {
2805 struct trace_iterator *iter = filp->private_data;
2806
2807 if (trace_flags & TRACE_ITER_BLOCK) {
2808 /*
2809 * Always select as readable when in blocking mode
2810 */
2811 return POLLIN | POLLRDNORM;
2812 } else {
2813 if (!trace_empty(iter))
2814 return POLLIN | POLLRDNORM;
2815 poll_wait(filp, &trace_wait, poll_table);
2816 if (!trace_empty(iter))
2817 return POLLIN | POLLRDNORM;
2818
2819 return 0;
2820 }
2821 }
2822
2823 /*
2824 * Consumer reader.
2825 */
2826 static ssize_t
2827 tracing_read_pipe(struct file *filp, char __user *ubuf,
2828 size_t cnt, loff_t *ppos)
2829 {
2830 struct trace_iterator *iter = filp->private_data;
2831 struct trace_array_cpu *data;
2832 static cpumask_t mask;
2833 unsigned long flags;
2834 #ifdef CONFIG_FTRACE
2835 int ftrace_save;
2836 #endif
2837 int cpu;
2838 ssize_t sret;
2839
2840 /* return any leftover data */
2841 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2842 if (sret != -EBUSY)
2843 return sret;
2844 sret = 0;
2845
2846 trace_seq_reset(&iter->seq);
2847
2848 mutex_lock(&trace_types_lock);
2849 if (iter->trace->read) {
2850 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2851 if (sret)
2852 goto out;
2853 }
2854
2855 while (trace_empty(iter)) {
2856
2857 if ((filp->f_flags & O_NONBLOCK)) {
2858 sret = -EAGAIN;
2859 goto out;
2860 }
2861
2862 /*
2863 * This is a make-shift waitqueue. The reason we don't use
2864 * an actual wait queue is because:
2865 * 1) we only ever have one waiter
2866 * 2) the tracing, traces all functions, we don't want
2867 * the overhead of calling wake_up and friends
2868 * (and tracing them too)
2869 * Anyway, this is really very primitive wakeup.
2870 */
2871 set_current_state(TASK_INTERRUPTIBLE);
2872 iter->tr->waiter = current;
2873
2874 mutex_unlock(&trace_types_lock);
2875
2876 /* sleep for 100 msecs, and try again. */
2877 schedule_timeout(HZ/10);
2878
2879 mutex_lock(&trace_types_lock);
2880
2881 iter->tr->waiter = NULL;
2882
2883 if (signal_pending(current)) {
2884 sret = -EINTR;
2885 goto out;
2886 }
2887
2888 if (iter->trace != current_trace)
2889 goto out;
2890
2891 /*
2892 * We block until we read something and tracing is disabled.
2893 * We still block if tracing is disabled, but we have never
2894 * read anything. This allows a user to cat this file, and
2895 * then enable tracing. But after we have read something,
2896 * we give an EOF when tracing is again disabled.
2897 *
2898 * iter->pos will be 0 if we haven't read anything.
2899 */
2900 if (!tracer_enabled && iter->pos)
2901 break;
2902
2903 continue;
2904 }
2905
2906 /* stop when tracing is finished */
2907 if (trace_empty(iter))
2908 goto out;
2909
2910 if (cnt >= PAGE_SIZE)
2911 cnt = PAGE_SIZE - 1;
2912
2913 /* reset all but tr, trace, and overruns */
2914 memset(&iter->seq, 0,
2915 sizeof(struct trace_iterator) -
2916 offsetof(struct trace_iterator, seq));
2917 iter->pos = -1;
2918
2919 /*
2920 * We need to stop all tracing on all CPUS to read the
2921 * the next buffer. This is a bit expensive, but is
2922 * not done often. We fill all what we can read,
2923 * and then release the locks again.
2924 */
2925
2926 cpus_clear(mask);
2927 local_irq_save(flags);
2928 #ifdef CONFIG_FTRACE
2929 ftrace_save = ftrace_enabled;
2930 ftrace_enabled = 0;
2931 #endif
2932 smp_wmb();
2933 for_each_tracing_cpu(cpu) {
2934 data = iter->tr->data[cpu];
2935
2936 if (!head_page(data) || !data->trace_idx)
2937 continue;
2938
2939 atomic_inc(&data->disabled);
2940 cpu_set(cpu, mask);
2941 }
2942
2943 for_each_cpu_mask_nr(cpu, mask) {
2944 data = iter->tr->data[cpu];
2945 __raw_spin_lock(&data->lock);
2946
2947 if (data->overrun > iter->last_overrun[cpu])
2948 iter->overrun[cpu] +=
2949 data->overrun - iter->last_overrun[cpu];
2950 iter->last_overrun[cpu] = data->overrun;
2951 }
2952
2953 while (find_next_entry_inc(iter) != NULL) {
2954 int ret;
2955 int len = iter->seq.len;
2956
2957 ret = print_trace_line(iter);
2958 if (!ret) {
2959 /* don't print partial lines */
2960 iter->seq.len = len;
2961 break;
2962 }
2963
2964 trace_consume(iter);
2965
2966 if (iter->seq.len >= cnt)
2967 break;
2968 }
2969
2970 for_each_cpu_mask_nr(cpu, mask) {
2971 data = iter->tr->data[cpu];
2972 __raw_spin_unlock(&data->lock);
2973 }
2974
2975 for_each_cpu_mask_nr(cpu, mask) {
2976 data = iter->tr->data[cpu];
2977 atomic_dec(&data->disabled);
2978 }
2979 #ifdef CONFIG_FTRACE
2980 ftrace_enabled = ftrace_save;
2981 #endif
2982 local_irq_restore(flags);
2983
2984 /* Now copy what we have to the user */
2985 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2986 if (iter->seq.readpos >= iter->seq.len)
2987 trace_seq_reset(&iter->seq);
2988 if (sret == -EBUSY)
2989 sret = 0;
2990
2991 out:
2992 mutex_unlock(&trace_types_lock);
2993
2994 return sret;
2995 }
2996
2997 static ssize_t
2998 tracing_entries_read(struct file *filp, char __user *ubuf,
2999 size_t cnt, loff_t *ppos)
3000 {
3001 struct trace_array *tr = filp->private_data;
3002 char buf[64];
3003 int r;
3004
3005 r = sprintf(buf, "%lu\n", tr->entries);
3006 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3007 }
3008
3009 static ssize_t
3010 tracing_entries_write(struct file *filp, const char __user *ubuf,
3011 size_t cnt, loff_t *ppos)
3012 {
3013 unsigned long val;
3014 char buf[64];
3015 int i, ret;
3016
3017 if (cnt >= sizeof(buf))
3018 return -EINVAL;
3019
3020 if (copy_from_user(&buf, ubuf, cnt))
3021 return -EFAULT;
3022
3023 buf[cnt] = 0;
3024
3025 ret = strict_strtoul(buf, 10, &val);
3026 if (ret < 0)
3027 return ret;
3028
3029 /* must have at least 1 entry */
3030 if (!val)
3031 return -EINVAL;
3032
3033 mutex_lock(&trace_types_lock);
3034
3035 if (current_trace != &no_tracer) {
3036 cnt = -EBUSY;
3037 pr_info("ftrace: set current_tracer to none"
3038 " before modifying buffer size\n");
3039 goto out;
3040 }
3041
3042 if (val > global_trace.entries) {
3043 long pages_requested;
3044 unsigned long freeable_pages;
3045
3046 /* make sure we have enough memory before mapping */
3047 pages_requested =
3048 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
3049
3050 /* account for each buffer (and max_tr) */
3051 pages_requested *= tracing_nr_buffers * 2;
3052
3053 /* Check for overflow */
3054 if (pages_requested < 0) {
3055 cnt = -ENOMEM;
3056 goto out;
3057 }
3058
3059 freeable_pages = determine_dirtyable_memory();
3060
3061 /* we only allow to request 1/4 of useable memory */
3062 if (pages_requested >
3063 ((freeable_pages + tracing_pages_allocated) / 4)) {
3064 cnt = -ENOMEM;
3065 goto out;
3066 }
3067
3068 while (global_trace.entries < val) {
3069 if (trace_alloc_page()) {
3070 cnt = -ENOMEM;
3071 goto out;
3072 }
3073 /* double check that we don't go over the known pages */
3074 if (tracing_pages_allocated > pages_requested)
3075 break;
3076 }
3077
3078 } else {
3079 /* include the number of entries in val (inc of page entries) */
3080 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
3081 trace_free_page();
3082 }
3083
3084 /* check integrity */
3085 for_each_tracing_cpu(i)
3086 check_pages(global_trace.data[i]);
3087
3088 filp->f_pos += cnt;
3089
3090 /* If check pages failed, return ENOMEM */
3091 if (tracing_disabled)
3092 cnt = -ENOMEM;
3093 out:
3094 max_tr.entries = global_trace.entries;
3095 mutex_unlock(&trace_types_lock);
3096
3097 return cnt;
3098 }
3099
3100 static struct file_operations tracing_max_lat_fops = {
3101 .open = tracing_open_generic,
3102 .read = tracing_max_lat_read,
3103 .write = tracing_max_lat_write,
3104 };
3105
3106 static struct file_operations tracing_ctrl_fops = {
3107 .open = tracing_open_generic,
3108 .read = tracing_ctrl_read,
3109 .write = tracing_ctrl_write,
3110 };
3111
3112 static struct file_operations set_tracer_fops = {
3113 .open = tracing_open_generic,
3114 .read = tracing_set_trace_read,
3115 .write = tracing_set_trace_write,
3116 };
3117
3118 static struct file_operations tracing_pipe_fops = {
3119 .open = tracing_open_pipe,
3120 .poll = tracing_poll_pipe,
3121 .read = tracing_read_pipe,
3122 .release = tracing_release_pipe,
3123 };
3124
3125 static struct file_operations tracing_entries_fops = {
3126 .open = tracing_open_generic,
3127 .read = tracing_entries_read,
3128 .write = tracing_entries_write,
3129 };
3130
3131 #ifdef CONFIG_DYNAMIC_FTRACE
3132
3133 static ssize_t
3134 tracing_read_long(struct file *filp, char __user *ubuf,
3135 size_t cnt, loff_t *ppos)
3136 {
3137 unsigned long *p = filp->private_data;
3138 char buf[64];
3139 int r;
3140
3141 r = sprintf(buf, "%ld\n", *p);
3142
3143 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3144 }
3145
3146 static struct file_operations tracing_read_long_fops = {
3147 .open = tracing_open_generic,
3148 .read = tracing_read_long,
3149 };
3150 #endif
3151
3152 static struct dentry *d_tracer;
3153
3154 struct dentry *tracing_init_dentry(void)
3155 {
3156 static int once;
3157
3158 if (d_tracer)
3159 return d_tracer;
3160
3161 d_tracer = debugfs_create_dir("tracing", NULL);
3162
3163 if (!d_tracer && !once) {
3164 once = 1;
3165 pr_warning("Could not create debugfs directory 'tracing'\n");
3166 return NULL;
3167 }
3168
3169 return d_tracer;
3170 }
3171
3172 #ifdef CONFIG_FTRACE_SELFTEST
3173 /* Let selftest have access to static functions in this file */
3174 #include "trace_selftest.c"
3175 #endif
3176
3177 static __init void tracer_init_debugfs(void)
3178 {
3179 struct dentry *d_tracer;
3180 struct dentry *entry;
3181
3182 d_tracer = tracing_init_dentry();
3183
3184 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3185 &global_trace, &tracing_ctrl_fops);
3186 if (!entry)
3187 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3188
3189 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
3190 NULL, &tracing_iter_fops);
3191 if (!entry)
3192 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
3193
3194 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3195 NULL, &tracing_cpumask_fops);
3196 if (!entry)
3197 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3198
3199 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3200 &global_trace, &tracing_lt_fops);
3201 if (!entry)
3202 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3203
3204 entry = debugfs_create_file("trace", 0444, d_tracer,
3205 &global_trace, &tracing_fops);
3206 if (!entry)
3207 pr_warning("Could not create debugfs 'trace' entry\n");
3208
3209 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3210 &global_trace, &show_traces_fops);
3211 if (!entry)
3212 pr_warning("Could not create debugfs 'trace' entry\n");
3213
3214 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3215 &global_trace, &set_tracer_fops);
3216 if (!entry)
3217 pr_warning("Could not create debugfs 'trace' entry\n");
3218
3219 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3220 &tracing_max_latency,
3221 &tracing_max_lat_fops);
3222 if (!entry)
3223 pr_warning("Could not create debugfs "
3224 "'tracing_max_latency' entry\n");
3225
3226 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3227 &tracing_thresh, &tracing_max_lat_fops);
3228 if (!entry)
3229 pr_warning("Could not create debugfs "
3230 "'tracing_threash' entry\n");
3231 entry = debugfs_create_file("README", 0644, d_tracer,
3232 NULL, &tracing_readme_fops);
3233 if (!entry)
3234 pr_warning("Could not create debugfs 'README' entry\n");
3235
3236 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3237 NULL, &tracing_pipe_fops);
3238 if (!entry)
3239 pr_warning("Could not create debugfs "
3240 "'tracing_threash' entry\n");
3241
3242 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
3243 &global_trace, &tracing_entries_fops);
3244 if (!entry)
3245 pr_warning("Could not create debugfs "
3246 "'tracing_threash' entry\n");
3247
3248 #ifdef CONFIG_DYNAMIC_FTRACE
3249 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3250 &ftrace_update_tot_cnt,
3251 &tracing_read_long_fops);
3252 if (!entry)
3253 pr_warning("Could not create debugfs "
3254 "'dyn_ftrace_total_info' entry\n");
3255 #endif
3256 #ifdef CONFIG_SYSPROF_TRACER
3257 init_tracer_sysprof_debugfs(d_tracer);
3258 #endif
3259 }
3260
3261 /**
3262 * ftrace_stop - called when we need to drastically disable the tracer.
3263 */
3264 void ftrace_stop(void)
3265 {
3266 struct tracer *saved_tracer = current_trace;
3267 struct trace_array *tr = &global_trace;
3268 struct trace_array_cpu *data;
3269 int i;
3270
3271 __ftrace_kill();
3272 for_each_tracing_cpu(i) {
3273 data = tr->data[i];
3274 atomic_inc(&data->disabled);
3275 }
3276 tracer_enabled = 0;
3277
3278 /*
3279 * TODO: make a safe method to ctrl_update.
3280 * ctrl_update may schedule, but currently only
3281 * does when ftrace is enabled.
3282 */
3283 if (tr->ctrl) {
3284 tr->ctrl = 0;
3285 if (saved_tracer && saved_tracer->ctrl_update)
3286 saved_tracer->ctrl_update;
3287 }
3288
3289
3290 }
3291
3292 static int trace_alloc_page(void)
3293 {
3294 struct trace_array_cpu *data;
3295 struct page *page, *tmp;
3296 LIST_HEAD(pages);
3297 void *array;
3298 unsigned pages_allocated = 0;
3299 int i;
3300
3301 /* first allocate a page for each CPU */
3302 for_each_tracing_cpu(i) {
3303 array = (void *)__get_free_page(GFP_KERNEL);
3304 if (array == NULL) {
3305 printk(KERN_ERR "tracer: failed to allocate page"
3306 "for trace buffer!\n");
3307 goto free_pages;
3308 }
3309
3310 pages_allocated++;
3311 page = virt_to_page(array);
3312 list_add(&page->lru, &pages);
3313
3314 /* Only allocate if we are actually using the max trace */
3315 #ifdef CONFIG_TRACER_MAX_TRACE
3316 array = (void *)__get_free_page(GFP_KERNEL);
3317 if (array == NULL) {
3318 printk(KERN_ERR "tracer: failed to allocate page"
3319 "for trace buffer!\n");
3320 goto free_pages;
3321 }
3322 pages_allocated++;
3323 page = virt_to_page(array);
3324 list_add(&page->lru, &pages);
3325 #endif
3326 }
3327
3328 /* Now that we successfully allocate a page per CPU, add them */
3329 for_each_tracing_cpu(i) {
3330 data = global_trace.data[i];
3331 page = list_entry(pages.next, struct page, lru);
3332 list_del_init(&page->lru);
3333 list_add_tail(&page->lru, &data->trace_pages);
3334 ClearPageLRU(page);
3335
3336 #ifdef CONFIG_TRACER_MAX_TRACE
3337 data = max_tr.data[i];
3338 page = list_entry(pages.next, struct page, lru);
3339 list_del_init(&page->lru);
3340 list_add_tail(&page->lru, &data->trace_pages);
3341 SetPageLRU(page);
3342 #endif
3343 }
3344 tracing_pages_allocated += pages_allocated;
3345 global_trace.entries += ENTRIES_PER_PAGE;
3346
3347 return 0;
3348
3349 free_pages:
3350 list_for_each_entry_safe(page, tmp, &pages, lru) {
3351 list_del_init(&page->lru);
3352 __free_page(page);
3353 }
3354 return -ENOMEM;
3355 }
3356
3357 static int trace_free_page(void)
3358 {
3359 struct trace_array_cpu *data;
3360 struct page *page;
3361 struct list_head *p;
3362 int i;
3363 int ret = 0;
3364
3365 /* free one page from each buffer */
3366 for_each_tracing_cpu(i) {
3367 data = global_trace.data[i];
3368 p = data->trace_pages.next;
3369 if (p == &data->trace_pages) {
3370 /* should never happen */
3371 WARN_ON(1);
3372 tracing_disabled = 1;
3373 ret = -1;
3374 break;
3375 }
3376 page = list_entry(p, struct page, lru);
3377 ClearPageLRU(page);
3378 list_del(&page->lru);
3379 tracing_pages_allocated--;
3380 tracing_pages_allocated--;
3381 __free_page(page);
3382
3383 tracing_reset(data);
3384
3385 #ifdef CONFIG_TRACER_MAX_TRACE
3386 data = max_tr.data[i];
3387 p = data->trace_pages.next;
3388 if (p == &data->trace_pages) {
3389 /* should never happen */
3390 WARN_ON(1);
3391 tracing_disabled = 1;
3392 ret = -1;
3393 break;
3394 }
3395 page = list_entry(p, struct page, lru);
3396 ClearPageLRU(page);
3397 list_del(&page->lru);
3398 __free_page(page);
3399
3400 tracing_reset(data);
3401 #endif
3402 }
3403 global_trace.entries -= ENTRIES_PER_PAGE;
3404
3405 return ret;
3406 }
3407
3408 __init static int tracer_alloc_buffers(void)
3409 {
3410 struct trace_array_cpu *data;
3411 void *array;
3412 struct page *page;
3413 int pages = 0;
3414 int ret = -ENOMEM;
3415 int i;
3416
3417 /* TODO: make the number of buffers hot pluggable with CPUS */
3418 tracing_nr_buffers = num_possible_cpus();
3419 tracing_buffer_mask = cpu_possible_map;
3420
3421 /* Allocate the first page for all buffers */
3422 for_each_tracing_cpu(i) {
3423 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3424 max_tr.data[i] = &per_cpu(max_data, i);
3425
3426 array = (void *)__get_free_page(GFP_KERNEL);
3427 if (array == NULL) {
3428 printk(KERN_ERR "tracer: failed to allocate page"
3429 "for trace buffer!\n");
3430 goto free_buffers;
3431 }
3432
3433 /* set the array to the list */
3434 INIT_LIST_HEAD(&data->trace_pages);
3435 page = virt_to_page(array);
3436 list_add(&page->lru, &data->trace_pages);
3437 /* use the LRU flag to differentiate the two buffers */
3438 ClearPageLRU(page);
3439
3440 data->lock = (__raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3441 max_tr.data[i]->lock = (__raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3442
3443 /* Only allocate if we are actually using the max trace */
3444 #ifdef CONFIG_TRACER_MAX_TRACE
3445 array = (void *)__get_free_page(GFP_KERNEL);
3446 if (array == NULL) {
3447 printk(KERN_ERR "tracer: failed to allocate page"
3448 "for trace buffer!\n");
3449 goto free_buffers;
3450 }
3451
3452 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3453 page = virt_to_page(array);
3454 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3455 SetPageLRU(page);
3456 #endif
3457 }
3458
3459 /*
3460 * Since we allocate by orders of pages, we may be able to
3461 * round up a bit.
3462 */
3463 global_trace.entries = ENTRIES_PER_PAGE;
3464 pages++;
3465
3466 while (global_trace.entries < trace_nr_entries) {
3467 if (trace_alloc_page())
3468 break;
3469 pages++;
3470 }
3471 max_tr.entries = global_trace.entries;
3472
3473 pr_info("tracer: %d pages allocated for %ld",
3474 pages, trace_nr_entries);
3475 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
3476 pr_info(" actual entries %ld\n", global_trace.entries);
3477
3478 tracer_init_debugfs();
3479
3480 trace_init_cmdlines();
3481
3482 register_tracer(&no_tracer);
3483 current_trace = &no_tracer;
3484
3485 /* All seems OK, enable tracing */
3486 global_trace.ctrl = tracer_enabled;
3487 tracing_disabled = 0;
3488
3489 return 0;
3490
3491 free_buffers:
3492 for (i-- ; i >= 0; i--) {
3493 struct page *page, *tmp;
3494 struct trace_array_cpu *data = global_trace.data[i];
3495
3496 if (data) {
3497 list_for_each_entry_safe(page, tmp,
3498 &data->trace_pages, lru) {
3499 list_del_init(&page->lru);
3500 __free_page(page);
3501 }
3502 }
3503
3504 #ifdef CONFIG_TRACER_MAX_TRACE
3505 data = max_tr.data[i];
3506 if (data) {
3507 list_for_each_entry_safe(page, tmp,
3508 &data->trace_pages, lru) {
3509 list_del_init(&page->lru);
3510 __free_page(page);
3511 }
3512 }
3513 #endif
3514 }
3515 return ret;
3516 }
3517 fs_initcall(tracer_alloc_buffers);
3518
|
This page was automatically generated by the
LXR engine.
|