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