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