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