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