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