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