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