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