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