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