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