tracing: Encapsulate global_trace and remove dependencies on global vars
[deliverable/linux.git] / kernel / trace / trace.c
CommitLineData
bc0c38d1
SR
1/*
2 * ring buffer based function tracer
3 *
2b6080f2 4 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
bc0c38d1
SR
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
6d49e352 12 * Copyright (C) 2004 Nadia Yvette Chambers
bc0c38d1 13 */
2cadf913 14#include <linux/ring_buffer.h>
273b281f 15#include <generated/utsrelease.h>
2cadf913
SR
16#include <linux/stacktrace.h>
17#include <linux/writeback.h>
bc0c38d1
SR
18#include <linux/kallsyms.h>
19#include <linux/seq_file.h>
3f5a54e3 20#include <linux/notifier.h>
2cadf913 21#include <linux/irqflags.h>
0d5c6e1c 22#include <linux/irq_work.h>
bc0c38d1 23#include <linux/debugfs.h>
4c11d7ae 24#include <linux/pagemap.h>
bc0c38d1
SR
25#include <linux/hardirq.h>
26#include <linux/linkage.h>
27#include <linux/uaccess.h>
2cadf913 28#include <linux/kprobes.h>
bc0c38d1
SR
29#include <linux/ftrace.h>
30#include <linux/module.h>
31#include <linux/percpu.h>
2cadf913 32#include <linux/splice.h>
3f5a54e3 33#include <linux/kdebug.h>
5f0c6c03 34#include <linux/string.h>
7e53bd42 35#include <linux/rwsem.h>
5a0e3ad6 36#include <linux/slab.h>
bc0c38d1
SR
37#include <linux/ctype.h>
38#include <linux/init.h>
2a2cc8f7 39#include <linux/poll.h>
b892e5c8 40#include <linux/nmi.h>
bc0c38d1 41#include <linux/fs.h>
8bd75c77 42#include <linux/sched/rt.h>
86387f7e 43
bc0c38d1 44#include "trace.h"
f0868d1e 45#include "trace_output.h"
bc0c38d1 46
73c5162a
SR
47/*
48 * On boot up, the ring buffer is set to the minimum size, so that
49 * we do not waste memory on systems that are not using tracing.
50 */
020e5f85 51int ring_buffer_expanded;
73c5162a 52
8e1b82e0
FW
53/*
54 * We need to change this state when a selftest is running.
ff32504f
FW
55 * A selftest will lurk into the ring-buffer to count the
56 * entries inserted during the selftest although some concurrent
5e1607a0 57 * insertions into the ring-buffer such as trace_printk could occurred
ff32504f
FW
58 * at the same time, giving false positive or negative results.
59 */
8e1b82e0 60static bool __read_mostly tracing_selftest_running;
ff32504f 61
b2821ae6
SR
62/*
63 * If a tracer is running, we do not want to run SELFTEST.
64 */
020e5f85 65bool __read_mostly tracing_selftest_disabled;
b2821ae6 66
adf9f195
FW
67/* For tracers that don't implement custom flags */
68static struct tracer_opt dummy_tracer_opt[] = {
69 { }
70};
71
72static struct tracer_flags dummy_tracer_flags = {
73 .val = 0,
74 .opts = dummy_tracer_opt
75};
76
77static int dummy_set_flag(u32 old_flags, u32 bit, int set)
78{
79 return 0;
80}
0f048701 81
7ffbd48d
SR
82/*
83 * To prevent the comm cache from being overwritten when no
84 * tracing is active, only save the comm when a trace event
85 * occurred.
86 */
87static DEFINE_PER_CPU(bool, trace_cmdline_save);
88
0d5c6e1c
SR
89/*
90 * When a reader is waiting for data, then this variable is
91 * set to true.
92 */
93static bool trace_wakeup_needed;
94
95static struct irq_work trace_work_wakeup;
96
0f048701
SR
97/*
98 * Kill all tracing for good (never come back).
99 * It is initialized to 1 but will turn to zero if the initialization
100 * of the tracer is successful. But that is the only place that sets
101 * this back to zero.
102 */
4fd27358 103static int tracing_disabled = 1;
0f048701 104
9288f99a 105DEFINE_PER_CPU(int, ftrace_cpu_disabled);
d769041f 106
955b61e5 107cpumask_var_t __read_mostly tracing_buffer_mask;
ab46428c 108
944ac425
SR
109/*
110 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
111 *
112 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
113 * is set, then ftrace_dump is called. This will output the contents
114 * of the ftrace buffers to the console. This is very useful for
115 * capturing traces that lead to crashes and outputing it to a
116 * serial console.
117 *
118 * It is default off, but you can enable it with either specifying
119 * "ftrace_dump_on_oops" in the kernel command line, or setting
cecbca96
FW
120 * /proc/sys/kernel/ftrace_dump_on_oops
121 * Set 1 if you want to dump buffers of all CPUs
122 * Set 2 if you want to dump the buffer of the CPU that triggered oops
944ac425 123 */
cecbca96
FW
124
125enum ftrace_dump_mode ftrace_dump_on_oops;
944ac425 126
b2821ae6
SR
127static int tracing_set_tracer(const char *buf);
128
ee6c2c1b
LZ
129#define MAX_TRACER_SIZE 100
130static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
b2821ae6 131static char *default_bootup_tracer;
d9e54076 132
1beee96b 133static int __init set_cmdline_ftrace(char *str)
d9e54076 134{
ee6c2c1b 135 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
b2821ae6 136 default_bootup_tracer = bootup_tracer_buf;
73c5162a
SR
137 /* We are using ftrace early, expand it */
138 ring_buffer_expanded = 1;
d9e54076
PZ
139 return 1;
140}
1beee96b 141__setup("ftrace=", set_cmdline_ftrace);
d9e54076 142
944ac425
SR
143static int __init set_ftrace_dump_on_oops(char *str)
144{
cecbca96
FW
145 if (*str++ != '=' || !*str) {
146 ftrace_dump_on_oops = DUMP_ALL;
147 return 1;
148 }
149
150 if (!strcmp("orig_cpu", str)) {
151 ftrace_dump_on_oops = DUMP_ORIG;
152 return 1;
153 }
154
155 return 0;
944ac425
SR
156}
157__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
60a11774 158
7bcfaf54
SR
159
160static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
161static char *trace_boot_options __initdata;
162
163static int __init set_trace_boot_options(char *str)
164{
165 strncpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
166 trace_boot_options = trace_boot_options_buf;
167 return 0;
168}
169__setup("trace_options=", set_trace_boot_options);
170
cf8e3474 171unsigned long long ns2usecs(cycle_t nsec)
bc0c38d1
SR
172{
173 nsec += 500;
174 do_div(nsec, 1000);
175 return nsec;
176}
177
4fcdae83
SR
178/*
179 * The global_trace is the descriptor that holds the tracing
180 * buffers for the live tracing. For each CPU, it contains
181 * a link list of pages that will store trace entries. The
182 * page descriptor of the pages in the memory is used to hold
183 * the link list by linking the lru item in the page descriptor
184 * to each of the pages in the buffer per CPU.
185 *
186 * For each active CPU there is a data field that holds the
187 * pages for the buffer for that CPU. Each CPU has the same number
188 * of pages allocated for its buffer.
189 */
bc0c38d1
SR
190static struct trace_array global_trace;
191
ae63b31e
SR
192LIST_HEAD(ftrace_trace_arrays);
193
bc0c38d1
SR
194static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
195
e77405ad
SR
196int filter_current_check_discard(struct ring_buffer *buffer,
197 struct ftrace_event_call *call, void *rec,
eb02ce01
TZ
198 struct ring_buffer_event *event)
199{
e77405ad 200 return filter_check_discard(call, rec, buffer, event);
eb02ce01 201}
17c873ec 202EXPORT_SYMBOL_GPL(filter_current_check_discard);
eb02ce01 203
37886f6a
SR
204cycle_t ftrace_now(int cpu)
205{
206 u64 ts;
207
208 /* Early boot up does not have a buffer yet */
209 if (!global_trace.buffer)
210 return trace_clock_local();
211
212 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
213 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
214
215 return ts;
216}
bc0c38d1 217
4fcdae83
SR
218/*
219 * The max_tr is used to snapshot the global_trace when a maximum
220 * latency is reached. Some tracers will use this to store a maximum
221 * trace while it continues examining live traces.
222 *
223 * The buffers for the max_tr are set up the same as the global_trace.
224 * When a snapshot is taken, the link list of the max_tr is swapped
225 * with the link list of the global_trace and the buffers are reset for
226 * the global_trace so the tracing can continue.
227 */
bc0c38d1
SR
228static struct trace_array max_tr;
229
9705f69e 230static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
bc0c38d1 231
9036990d
SR
232int tracing_is_enabled(void)
233{
0fb9656d 234 return tracing_is_on();
9036990d
SR
235}
236
4fcdae83 237/*
3928a8a2
SR
238 * trace_buf_size is the size in bytes that is allocated
239 * for a buffer. Note, the number of bytes is always rounded
240 * to page size.
3f5a54e3
SR
241 *
242 * This number is purposely set to a low number of 16384.
243 * If the dump on oops happens, it will be much appreciated
244 * to not have to wait for all that output. Anyway this can be
245 * boot time and run time configurable.
4fcdae83 246 */
3928a8a2 247#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
3f5a54e3 248
3928a8a2 249static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
bc0c38d1 250
4fcdae83 251/* trace_types holds a link list of available tracers. */
bc0c38d1 252static struct tracer *trace_types __read_mostly;
4fcdae83 253
4fcdae83
SR
254/*
255 * trace_types_lock is used to protect the trace_types list.
4fcdae83 256 */
bc0c38d1 257static DEFINE_MUTEX(trace_types_lock);
4fcdae83 258
7e53bd42
LJ
259/*
260 * serialize the access of the ring buffer
261 *
262 * ring buffer serializes readers, but it is low level protection.
263 * The validity of the events (which returns by ring_buffer_peek() ..etc)
264 * are not protected by ring buffer.
265 *
266 * The content of events may become garbage if we allow other process consumes
267 * these events concurrently:
268 * A) the page of the consumed events may become a normal page
269 * (not reader page) in ring buffer, and this page will be rewrited
270 * by events producer.
271 * B) The page of the consumed events may become a page for splice_read,
272 * and this page will be returned to system.
273 *
274 * These primitives allow multi process access to different cpu ring buffer
275 * concurrently.
276 *
277 * These primitives don't distinguish read-only and read-consume access.
278 * Multi read-only access are also serialized.
279 */
280
281#ifdef CONFIG_SMP
282static DECLARE_RWSEM(all_cpu_access_lock);
283static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
284
285static inline void trace_access_lock(int cpu)
286{
ae3b5093 287 if (cpu == RING_BUFFER_ALL_CPUS) {
7e53bd42
LJ
288 /* gain it for accessing the whole ring buffer. */
289 down_write(&all_cpu_access_lock);
290 } else {
291 /* gain it for accessing a cpu ring buffer. */
292
ae3b5093 293 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
7e53bd42
LJ
294 down_read(&all_cpu_access_lock);
295
296 /* Secondly block other access to this @cpu ring buffer. */
297 mutex_lock(&per_cpu(cpu_access_lock, cpu));
298 }
299}
300
301static inline void trace_access_unlock(int cpu)
302{
ae3b5093 303 if (cpu == RING_BUFFER_ALL_CPUS) {
7e53bd42
LJ
304 up_write(&all_cpu_access_lock);
305 } else {
306 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
307 up_read(&all_cpu_access_lock);
308 }
309}
310
311static inline void trace_access_lock_init(void)
312{
313 int cpu;
314
315 for_each_possible_cpu(cpu)
316 mutex_init(&per_cpu(cpu_access_lock, cpu));
317}
318
319#else
320
321static DEFINE_MUTEX(access_lock);
322
323static inline void trace_access_lock(int cpu)
324{
325 (void)cpu;
326 mutex_lock(&access_lock);
327}
328
329static inline void trace_access_unlock(int cpu)
330{
331 (void)cpu;
332 mutex_unlock(&access_lock);
333}
334
335static inline void trace_access_lock_init(void)
336{
337}
338
339#endif
340
4fcdae83 341/* trace_wait is a waitqueue for tasks blocked on trace_poll */
4e655519
IM
342static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
343
ee6bce52 344/* trace_flags holds trace_options default values */
12ef7d44 345unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
a2a16d6a 346 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
77271ce4 347 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
5224c3a3 348 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS;
4e655519 349
0d5c6e1c
SR
350/**
351 * trace_wake_up - wake up tasks waiting for trace input
352 *
353 * Schedules a delayed work to wake up any task that is blocked on the
354 * trace_wait queue. These is used with trace_poll for tasks polling the
355 * trace.
356 */
357static void trace_wake_up(struct irq_work *work)
e7e2ee89 358{
0d5c6e1c 359 wake_up_all(&trace_wait);
e7e2ee89 360
0d5c6e1c 361}
e7e2ee89 362
499e5470
SR
363/**
364 * tracing_on - enable tracing buffers
365 *
366 * This function enables tracing buffers that may have been
367 * disabled with tracing_off.
368 */
369void tracing_on(void)
370{
371 if (global_trace.buffer)
372 ring_buffer_record_on(global_trace.buffer);
373 /*
374 * This flag is only looked at when buffers haven't been
375 * allocated yet. We don't really care about the race
376 * between setting this flag and actually turning
377 * on the buffer.
378 */
379 global_trace.buffer_disabled = 0;
380}
381EXPORT_SYMBOL_GPL(tracing_on);
382
383/**
384 * tracing_off - turn off tracing buffers
385 *
386 * This function stops the tracing buffers from recording data.
387 * It does not disable any overhead the tracers themselves may
388 * be causing. This function simply causes all recording to
389 * the ring buffers to fail.
390 */
391void tracing_off(void)
392{
393 if (global_trace.buffer)
f2bf1f6f 394 ring_buffer_record_off(global_trace.buffer);
499e5470
SR
395 /*
396 * This flag is only looked at when buffers haven't been
397 * allocated yet. We don't really care about the race
398 * between setting this flag and actually turning
399 * on the buffer.
400 */
401 global_trace.buffer_disabled = 1;
402}
403EXPORT_SYMBOL_GPL(tracing_off);
404
405/**
406 * tracing_is_on - show state of ring buffers enabled
407 */
408int tracing_is_on(void)
409{
410 if (global_trace.buffer)
411 return ring_buffer_record_is_on(global_trace.buffer);
412 return !global_trace.buffer_disabled;
413}
414EXPORT_SYMBOL_GPL(tracing_is_on);
415
3928a8a2 416static int __init set_buf_size(char *str)
bc0c38d1 417{
3928a8a2 418 unsigned long buf_size;
c6caeeb1 419
bc0c38d1
SR
420 if (!str)
421 return 0;
9d612bef 422 buf_size = memparse(str, &str);
c6caeeb1 423 /* nr_entries can not be zero */
9d612bef 424 if (buf_size == 0)
c6caeeb1 425 return 0;
3928a8a2 426 trace_buf_size = buf_size;
bc0c38d1
SR
427 return 1;
428}
3928a8a2 429__setup("trace_buf_size=", set_buf_size);
bc0c38d1 430
0e950173
TB
431static int __init set_tracing_thresh(char *str)
432{
87abb3b1 433 unsigned long threshold;
0e950173
TB
434 int ret;
435
436 if (!str)
437 return 0;
bcd83ea6 438 ret = kstrtoul(str, 0, &threshold);
0e950173
TB
439 if (ret < 0)
440 return 0;
87abb3b1 441 tracing_thresh = threshold * 1000;
0e950173
TB
442 return 1;
443}
444__setup("tracing_thresh=", set_tracing_thresh);
445
57f50be1
SR
446unsigned long nsecs_to_usecs(unsigned long nsecs)
447{
448 return nsecs / 1000;
449}
450
4fcdae83 451/* These must match the bit postions in trace_iterator_flags */
bc0c38d1
SR
452static const char *trace_options[] = {
453 "print-parent",
454 "sym-offset",
455 "sym-addr",
456 "verbose",
f9896bf3 457 "raw",
5e3ca0ec 458 "hex",
cb0f12aa 459 "bin",
2a2cc8f7 460 "block",
86387f7e 461 "stacktrace",
5e1607a0 462 "trace_printk",
b2a866f9 463 "ftrace_preempt",
9f029e83 464 "branch",
12ef7d44 465 "annotate",
02b67518 466 "userstacktrace",
b54d3de9 467 "sym-userobj",
66896a85 468 "printk-msg-only",
c4a8e8be 469 "context-info",
c032ef64 470 "latency-format",
be6f164a 471 "sleep-time",
a2a16d6a 472 "graph-time",
e870e9a1 473 "record-cmd",
750912fa 474 "overwrite",
cf30cf67 475 "disable_on_free",
77271ce4 476 "irq-info",
5224c3a3 477 "markers",
bc0c38d1
SR
478 NULL
479};
480
5079f326
Z
481static struct {
482 u64 (*func)(void);
483 const char *name;
8be0709f 484 int in_ns; /* is this clock in nanoseconds? */
5079f326 485} trace_clocks[] = {
8be0709f
DS
486 { trace_clock_local, "local", 1 },
487 { trace_clock_global, "global", 1 },
488 { trace_clock_counter, "counter", 0 },
8cbd9cc6 489 ARCH_TRACE_CLOCKS
5079f326
Z
490};
491
492int trace_clock_id;
493
b63f39ea 494/*
495 * trace_parser_get_init - gets the buffer for trace parser
496 */
497int trace_parser_get_init(struct trace_parser *parser, int size)
498{
499 memset(parser, 0, sizeof(*parser));
500
501 parser->buffer = kmalloc(size, GFP_KERNEL);
502 if (!parser->buffer)
503 return 1;
504
505 parser->size = size;
506 return 0;
507}
508
509/*
510 * trace_parser_put - frees the buffer for trace parser
511 */
512void trace_parser_put(struct trace_parser *parser)
513{
514 kfree(parser->buffer);
515}
516
517/*
518 * trace_get_user - reads the user input string separated by space
519 * (matched by isspace(ch))
520 *
521 * For each string found the 'struct trace_parser' is updated,
522 * and the function returns.
523 *
524 * Returns number of bytes read.
525 *
526 * See kernel/trace/trace.h for 'struct trace_parser' details.
527 */
528int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
529 size_t cnt, loff_t *ppos)
530{
531 char ch;
532 size_t read = 0;
533 ssize_t ret;
534
535 if (!*ppos)
536 trace_parser_clear(parser);
537
538 ret = get_user(ch, ubuf++);
539 if (ret)
540 goto out;
541
542 read++;
543 cnt--;
544
545 /*
546 * The parser is not finished with the last write,
547 * continue reading the user input without skipping spaces.
548 */
549 if (!parser->cont) {
550 /* skip white space */
551 while (cnt && isspace(ch)) {
552 ret = get_user(ch, ubuf++);
553 if (ret)
554 goto out;
555 read++;
556 cnt--;
557 }
558
559 /* only spaces were written */
560 if (isspace(ch)) {
561 *ppos += read;
562 ret = read;
563 goto out;
564 }
565
566 parser->idx = 0;
567 }
568
569 /* read the non-space input */
570 while (cnt && !isspace(ch)) {
3c235a33 571 if (parser->idx < parser->size - 1)
b63f39ea 572 parser->buffer[parser->idx++] = ch;
573 else {
574 ret = -EINVAL;
575 goto out;
576 }
577 ret = get_user(ch, ubuf++);
578 if (ret)
579 goto out;
580 read++;
581 cnt--;
582 }
583
584 /* We either got finished input or we have to wait for another call. */
585 if (isspace(ch)) {
586 parser->buffer[parser->idx] = 0;
587 parser->cont = false;
588 } else {
589 parser->cont = true;
590 parser->buffer[parser->idx++] = ch;
591 }
592
593 *ppos += read;
594 ret = read;
595
596out:
597 return ret;
598}
599
6c6c2796
PP
600ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
601{
602 int len;
603 int ret;
604
2dc5d12b
SR
605 if (!cnt)
606 return 0;
607
6c6c2796
PP
608 if (s->len <= s->readpos)
609 return -EBUSY;
610
611 len = s->len - s->readpos;
612 if (cnt > len)
613 cnt = len;
614 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
2dc5d12b 615 if (ret == cnt)
6c6c2796
PP
616 return -EFAULT;
617
2dc5d12b
SR
618 cnt -= ret;
619
e74da523 620 s->readpos += cnt;
6c6c2796 621 return cnt;
214023c3
SR
622}
623
b8b94265 624static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
3c56819b
EGM
625{
626 int len;
3c56819b
EGM
627
628 if (s->len <= s->readpos)
629 return -EBUSY;
630
631 len = s->len - s->readpos;
632 if (cnt > len)
633 cnt = len;
5a26c8f0 634 memcpy(buf, s->buffer + s->readpos, cnt);
3c56819b 635
e74da523 636 s->readpos += cnt;
3c56819b
EGM
637 return cnt;
638}
639
5d4a9dba
SR
640/*
641 * ftrace_max_lock is used to protect the swapping of buffers
642 * when taking a max snapshot. The buffers themselves are
643 * protected by per_cpu spinlocks. But the action of the swap
644 * needs its own lock.
645 *
445c8951 646 * This is defined as a arch_spinlock_t in order to help
5d4a9dba
SR
647 * with performance when lockdep debugging is enabled.
648 *
649 * It is also used in other places outside the update_max_tr
650 * so it needs to be defined outside of the
651 * CONFIG_TRACER_MAX_TRACE.
652 */
445c8951 653static arch_spinlock_t ftrace_max_lock =
edc35bd7 654 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
5d4a9dba 655
0e950173
TB
656unsigned long __read_mostly tracing_thresh;
657
5d4a9dba
SR
658#ifdef CONFIG_TRACER_MAX_TRACE
659unsigned long __read_mostly tracing_max_latency;
5d4a9dba
SR
660
661/*
662 * Copy the new maximum trace into the separate maximum-trace
663 * structure. (this way the maximum trace is permanently saved,
664 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
665 */
666static void
667__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
668{
669 struct trace_array_cpu *data = tr->data[cpu];
1acaa1b2 670 struct trace_array_cpu *max_data;
5d4a9dba
SR
671
672 max_tr.cpu = cpu;
673 max_tr.time_start = data->preempt_timestamp;
674
8248ac05
SR
675 max_data = max_tr.data[cpu];
676 max_data->saved_latency = tracing_max_latency;
677 max_data->critical_start = data->critical_start;
678 max_data->critical_end = data->critical_end;
5d4a9dba 679
1acaa1b2 680 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
8248ac05
SR
681 max_data->pid = tsk->pid;
682 max_data->uid = task_uid(tsk);
683 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
684 max_data->policy = tsk->policy;
685 max_data->rt_priority = tsk->rt_priority;
5d4a9dba
SR
686
687 /* record this tasks comm */
688 tracing_record_cmdline(tsk);
689}
690
4fcdae83
SR
691/**
692 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
693 * @tr: tracer
694 * @tsk: the task with the latency
695 * @cpu: The cpu that initiated the trace.
696 *
697 * Flip the buffers between the @tr and the max_tr and record information
698 * about which task was the cause of this latency.
699 */
e309b41d 700void
bc0c38d1
SR
701update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
702{
2721e72d 703 struct ring_buffer *buf;
bc0c38d1 704
2b6080f2 705 if (tr->stop_count)
b8de7bd1
SR
706 return;
707
4c11d7ae 708 WARN_ON_ONCE(!irqs_disabled());
34600f0e 709
2b6080f2 710 if (!tr->current_trace->allocated_snapshot) {
debdd57f 711 /* Only the nop tracer should hit this when disabling */
2b6080f2 712 WARN_ON_ONCE(tr->current_trace != &nop_trace);
34600f0e 713 return;
debdd57f 714 }
34600f0e 715
0199c4e6 716 arch_spin_lock(&ftrace_max_lock);
3928a8a2 717
2721e72d 718 buf = tr->buffer;
3928a8a2
SR
719 tr->buffer = max_tr.buffer;
720 max_tr.buffer = buf;
721
bc0c38d1 722 __update_max_tr(tr, tsk, cpu);
0199c4e6 723 arch_spin_unlock(&ftrace_max_lock);
bc0c38d1
SR
724}
725
726/**
727 * update_max_tr_single - only copy one trace over, and reset the rest
728 * @tr - tracer
729 * @tsk - task with the latency
730 * @cpu - the cpu of the buffer to copy.
4fcdae83
SR
731 *
732 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
bc0c38d1 733 */
e309b41d 734void
bc0c38d1
SR
735update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
736{
3928a8a2 737 int ret;
bc0c38d1 738
2b6080f2 739 if (tr->stop_count)
b8de7bd1
SR
740 return;
741
4c11d7ae 742 WARN_ON_ONCE(!irqs_disabled());
2b6080f2 743 if (WARN_ON_ONCE(!tr->current_trace->allocated_snapshot))
ef710e10 744 return;
ef710e10 745
0199c4e6 746 arch_spin_lock(&ftrace_max_lock);
bc0c38d1 747
3928a8a2
SR
748 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
749
e8165dbb
SR
750 if (ret == -EBUSY) {
751 /*
752 * We failed to swap the buffer due to a commit taking
753 * place on this CPU. We fail to record, but we reset
754 * the max trace buffer (no one writes directly to it)
755 * and flag that it failed.
756 */
757 trace_array_printk(&max_tr, _THIS_IP_,
758 "Failed to swap buffers due to commit in progress\n");
759 }
760
e8165dbb 761 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
bc0c38d1
SR
762
763 __update_max_tr(tr, tsk, cpu);
0199c4e6 764 arch_spin_unlock(&ftrace_max_lock);
bc0c38d1 765}
5d4a9dba 766#endif /* CONFIG_TRACER_MAX_TRACE */
bc0c38d1 767
0d5c6e1c
SR
768static void default_wait_pipe(struct trace_iterator *iter)
769{
770 DEFINE_WAIT(wait);
771
772 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
773
774 /*
775 * The events can happen in critical sections where
776 * checking a work queue can cause deadlocks.
777 * After adding a task to the queue, this flag is set
778 * only to notify events to try to wake up the queue
779 * using irq_work.
780 *
781 * We don't clear it even if the buffer is no longer
782 * empty. The flag only causes the next event to run
783 * irq_work to do the work queue wake up. The worse
784 * that can happen if we race with !trace_empty() is that
785 * an event will cause an irq_work to try to wake up
786 * an empty queue.
787 *
788 * There's no reason to protect this flag either, as
789 * the work queue and irq_work logic will do the necessary
790 * synchronization for the wake ups. The only thing
791 * that is necessary is that the wake up happens after
792 * a task has been queued. It's OK for spurious wake ups.
793 */
794 trace_wakeup_needed = true;
795
796 if (trace_empty(iter))
797 schedule();
798
799 finish_wait(&trace_wait, &wait);
800}
801
4fcdae83
SR
802/**
803 * register_tracer - register a tracer with the ftrace system.
804 * @type - the plugin for the tracer
805 *
806 * Register a new plugin tracer.
807 */
bc0c38d1
SR
808int register_tracer(struct tracer *type)
809{
810 struct tracer *t;
bc0c38d1
SR
811 int ret = 0;
812
813 if (!type->name) {
814 pr_info("Tracer must have a name\n");
815 return -1;
816 }
817
24a461d5 818 if (strlen(type->name) >= MAX_TRACER_SIZE) {
ee6c2c1b
LZ
819 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
820 return -1;
821 }
822
bc0c38d1 823 mutex_lock(&trace_types_lock);
86fa2f60 824
8e1b82e0
FW
825 tracing_selftest_running = true;
826
bc0c38d1
SR
827 for (t = trace_types; t; t = t->next) {
828 if (strcmp(type->name, t->name) == 0) {
829 /* already found */
ee6c2c1b 830 pr_info("Tracer %s already registered\n",
bc0c38d1
SR
831 type->name);
832 ret = -1;
833 goto out;
834 }
835 }
836
adf9f195
FW
837 if (!type->set_flag)
838 type->set_flag = &dummy_set_flag;
839 if (!type->flags)
840 type->flags = &dummy_tracer_flags;
841 else
842 if (!type->flags->opts)
843 type->flags->opts = dummy_tracer_opt;
6eaaa5d5
FW
844 if (!type->wait_pipe)
845 type->wait_pipe = default_wait_pipe;
846
adf9f195 847
60a11774 848#ifdef CONFIG_FTRACE_STARTUP_TEST
b2821ae6 849 if (type->selftest && !tracing_selftest_disabled) {
60a11774 850 struct trace_array *tr = &global_trace;
2b6080f2 851 struct tracer *saved_tracer = tr->current_trace;
ff32504f 852
60a11774
SR
853 /*
854 * Run a selftest on this tracer.
855 * Here we reset the trace buffer, and set the current
856 * tracer to be this tracer. The tracer can then run some
857 * internal tracing to verify that everything is in order.
858 * If we fail, we do not register this tracer.
859 */
76f0d073 860 tracing_reset_online_cpus(tr);
86fa2f60 861
2b6080f2 862 tr->current_trace = type;
4a0b1665 863
debdd57f
HT
864 if (type->use_max_tr) {
865 /* If we expanded the buffers, make sure the max is expanded too */
866 if (ring_buffer_expanded)
867 ring_buffer_resize(max_tr.buffer, trace_buf_size,
868 RING_BUFFER_ALL_CPUS);
869 type->allocated_snapshot = true;
870 }
4a0b1665 871
60a11774
SR
872 /* the test is responsible for initializing and enabling */
873 pr_info("Testing tracer %s: ", type->name);
874 ret = type->selftest(type, tr);
875 /* the test is responsible for resetting too */
2b6080f2 876 tr->current_trace = saved_tracer;
60a11774
SR
877 if (ret) {
878 printk(KERN_CONT "FAILED!\n");
0be61ebc
SR
879 /* Add the warning after printing 'FAILED' */
880 WARN_ON(1);
60a11774
SR
881 goto out;
882 }
1d4db00a 883 /* Only reset on passing, to avoid touching corrupted buffers */
76f0d073 884 tracing_reset_online_cpus(tr);
86fa2f60 885
debdd57f
HT
886 if (type->use_max_tr) {
887 type->allocated_snapshot = false;
888
889 /* Shrink the max buffer again */
890 if (ring_buffer_expanded)
891 ring_buffer_resize(max_tr.buffer, 1,
892 RING_BUFFER_ALL_CPUS);
893 }
4a0b1665 894
60a11774
SR
895 printk(KERN_CONT "PASSED\n");
896 }
897#endif
898
bc0c38d1
SR
899 type->next = trace_types;
900 trace_types = type;
60a11774 901
bc0c38d1 902 out:
8e1b82e0 903 tracing_selftest_running = false;
bc0c38d1
SR
904 mutex_unlock(&trace_types_lock);
905
dac74940
SR
906 if (ret || !default_bootup_tracer)
907 goto out_unlock;
908
ee6c2c1b 909 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
dac74940
SR
910 goto out_unlock;
911
912 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
913 /* Do we want this tracer to start on bootup? */
914 tracing_set_tracer(type->name);
915 default_bootup_tracer = NULL;
916 /* disable other selftests, since this will break it. */
917 tracing_selftest_disabled = 1;
b2821ae6 918#ifdef CONFIG_FTRACE_STARTUP_TEST
dac74940
SR
919 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
920 type->name);
b2821ae6 921#endif
b2821ae6 922
dac74940 923 out_unlock:
bc0c38d1
SR
924 return ret;
925}
926
f633903a
SR
927void tracing_reset(struct trace_array *tr, int cpu)
928{
929 struct ring_buffer *buffer = tr->buffer;
930
a5416411
HT
931 if (!buffer)
932 return;
933
f633903a
SR
934 ring_buffer_record_disable(buffer);
935
936 /* Make sure all commits have finished */
937 synchronize_sched();
68179686 938 ring_buffer_reset_cpu(buffer, cpu);
f633903a
SR
939
940 ring_buffer_record_enable(buffer);
941}
942
213cc060
PE
943void tracing_reset_online_cpus(struct trace_array *tr)
944{
621968cd 945 struct ring_buffer *buffer = tr->buffer;
213cc060
PE
946 int cpu;
947
a5416411
HT
948 if (!buffer)
949 return;
950
621968cd
SR
951 ring_buffer_record_disable(buffer);
952
953 /* Make sure all commits have finished */
954 synchronize_sched();
955
213cc060
PE
956 tr->time_start = ftrace_now(tr->cpu);
957
958 for_each_online_cpu(cpu)
68179686 959 ring_buffer_reset_cpu(buffer, cpu);
621968cd
SR
960
961 ring_buffer_record_enable(buffer);
213cc060
PE
962}
963
9456f0fa
SR
964void tracing_reset_current(int cpu)
965{
966 tracing_reset(&global_trace, cpu);
967}
968
969void tracing_reset_current_online_cpus(void)
970{
971 tracing_reset_online_cpus(&global_trace);
972}
973
bc0c38d1 974#define SAVED_CMDLINES 128
2c7eea4c 975#define NO_CMDLINE_MAP UINT_MAX
bc0c38d1
SR
976static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
977static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
978static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
979static int cmdline_idx;
edc35bd7 980static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
25b0b44a 981
25b0b44a 982/* temporary disable recording */
4fd27358 983static atomic_t trace_record_cmdline_disabled __read_mostly;
bc0c38d1
SR
984
985static void trace_init_cmdlines(void)
986{
2c7eea4c
TG
987 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
988 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
bc0c38d1
SR
989 cmdline_idx = 0;
990}
991
b5130b1e
CE
992int is_tracing_stopped(void)
993{
2b6080f2 994 return global_trace.stop_count;
b5130b1e
CE
995}
996
69bb54ec
SR
997/**
998 * ftrace_off_permanent - disable all ftrace code permanently
999 *
1000 * This should only be called when a serious anomally has
1001 * been detected. This will turn off the function tracing,
1002 * ring buffers, and other tracing utilites. It takes no
1003 * locks and can be called from any context.
1004 */
1005void ftrace_off_permanent(void)
1006{
1007 tracing_disabled = 1;
1008 ftrace_stop();
1009 tracing_off_permanent();
1010}
1011
0f048701
SR
1012/**
1013 * tracing_start - quick start of the tracer
1014 *
1015 * If tracing is enabled but was stopped by tracing_stop,
1016 * this will start the tracer back up.
1017 */
1018void tracing_start(void)
1019{
1020 struct ring_buffer *buffer;
1021 unsigned long flags;
1022
1023 if (tracing_disabled)
1024 return;
1025
2b6080f2
SR
1026 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1027 if (--global_trace.stop_count) {
1028 if (global_trace.stop_count < 0) {
b06a8301
SR
1029 /* Someone screwed up their debugging */
1030 WARN_ON_ONCE(1);
2b6080f2 1031 global_trace.stop_count = 0;
b06a8301 1032 }
0f048701
SR
1033 goto out;
1034 }
1035
a2f80714
SR
1036 /* Prevent the buffers from switching */
1037 arch_spin_lock(&ftrace_max_lock);
0f048701
SR
1038
1039 buffer = global_trace.buffer;
1040 if (buffer)
1041 ring_buffer_record_enable(buffer);
1042
1043 buffer = max_tr.buffer;
1044 if (buffer)
1045 ring_buffer_record_enable(buffer);
1046
a2f80714
SR
1047 arch_spin_unlock(&ftrace_max_lock);
1048
0f048701
SR
1049 ftrace_start();
1050 out:
2b6080f2
SR
1051 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1052}
1053
1054static void tracing_start_tr(struct trace_array *tr)
1055{
1056 struct ring_buffer *buffer;
1057 unsigned long flags;
1058
1059 if (tracing_disabled)
1060 return;
1061
1062 /* If global, we need to also start the max tracer */
1063 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1064 return tracing_start();
1065
1066 raw_spin_lock_irqsave(&tr->start_lock, flags);
1067
1068 if (--tr->stop_count) {
1069 if (tr->stop_count < 0) {
1070 /* Someone screwed up their debugging */
1071 WARN_ON_ONCE(1);
1072 tr->stop_count = 0;
1073 }
1074 goto out;
1075 }
1076
1077 buffer = tr->buffer;
1078 if (buffer)
1079 ring_buffer_record_enable(buffer);
1080
1081 out:
1082 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
0f048701
SR
1083}
1084
1085/**
1086 * tracing_stop - quick stop of the tracer
1087 *
1088 * Light weight way to stop tracing. Use in conjunction with
1089 * tracing_start.
1090 */
1091void tracing_stop(void)
1092{
1093 struct ring_buffer *buffer;
1094 unsigned long flags;
1095
1096 ftrace_stop();
2b6080f2
SR
1097 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1098 if (global_trace.stop_count++)
0f048701
SR
1099 goto out;
1100
a2f80714
SR
1101 /* Prevent the buffers from switching */
1102 arch_spin_lock(&ftrace_max_lock);
1103
0f048701
SR
1104 buffer = global_trace.buffer;
1105 if (buffer)
1106 ring_buffer_record_disable(buffer);
1107
1108 buffer = max_tr.buffer;
1109 if (buffer)
1110 ring_buffer_record_disable(buffer);
1111
a2f80714
SR
1112 arch_spin_unlock(&ftrace_max_lock);
1113
0f048701 1114 out:
2b6080f2
SR
1115 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1116}
1117
1118static void tracing_stop_tr(struct trace_array *tr)
1119{
1120 struct ring_buffer *buffer;
1121 unsigned long flags;
1122
1123 /* If global, we need to also stop the max tracer */
1124 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1125 return tracing_stop();
1126
1127 raw_spin_lock_irqsave(&tr->start_lock, flags);
1128 if (tr->stop_count++)
1129 goto out;
1130
1131 buffer = tr->buffer;
1132 if (buffer)
1133 ring_buffer_record_disable(buffer);
1134
1135 out:
1136 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
0f048701
SR
1137}
1138
e309b41d 1139void trace_stop_cmdline_recording(void);
bc0c38d1 1140
e309b41d 1141static void trace_save_cmdline(struct task_struct *tsk)
bc0c38d1 1142{
a635cf04 1143 unsigned pid, idx;
bc0c38d1
SR
1144
1145 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1146 return;
1147
1148 /*
1149 * It's not the end of the world if we don't get
1150 * the lock, but we also don't want to spin
1151 * nor do we want to disable interrupts,
1152 * so if we miss here, then better luck next time.
1153 */
0199c4e6 1154 if (!arch_spin_trylock(&trace_cmdline_lock))
bc0c38d1
SR
1155 return;
1156
1157 idx = map_pid_to_cmdline[tsk->pid];
2c7eea4c 1158 if (idx == NO_CMDLINE_MAP) {
bc0c38d1
SR
1159 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1160
a635cf04
CE
1161 /*
1162 * Check whether the cmdline buffer at idx has a pid
1163 * mapped. We are going to overwrite that entry so we
1164 * need to clear the map_pid_to_cmdline. Otherwise we
1165 * would read the new comm for the old pid.
1166 */
1167 pid = map_cmdline_to_pid[idx];
1168 if (pid != NO_CMDLINE_MAP)
1169 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
bc0c38d1 1170
a635cf04 1171 map_cmdline_to_pid[idx] = tsk->pid;
bc0c38d1
SR
1172 map_pid_to_cmdline[tsk->pid] = idx;
1173
1174 cmdline_idx = idx;
1175 }
1176
1177 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1178
0199c4e6 1179 arch_spin_unlock(&trace_cmdline_lock);
bc0c38d1
SR
1180}
1181
4ca53085 1182void trace_find_cmdline(int pid, char comm[])
bc0c38d1 1183{
bc0c38d1
SR
1184 unsigned map;
1185
4ca53085
SR
1186 if (!pid) {
1187 strcpy(comm, "<idle>");
1188 return;
1189 }
bc0c38d1 1190
74bf4076
SR
1191 if (WARN_ON_ONCE(pid < 0)) {
1192 strcpy(comm, "<XXX>");
1193 return;
1194 }
1195
4ca53085
SR
1196 if (pid > PID_MAX_DEFAULT) {
1197 strcpy(comm, "<...>");
1198 return;
1199 }
bc0c38d1 1200
5b6045a9 1201 preempt_disable();
0199c4e6 1202 arch_spin_lock(&trace_cmdline_lock);
bc0c38d1 1203 map = map_pid_to_cmdline[pid];
50d88758
TG
1204 if (map != NO_CMDLINE_MAP)
1205 strcpy(comm, saved_cmdlines[map]);
1206 else
1207 strcpy(comm, "<...>");
bc0c38d1 1208
0199c4e6 1209 arch_spin_unlock(&trace_cmdline_lock);
5b6045a9 1210 preempt_enable();
bc0c38d1
SR
1211}
1212
e309b41d 1213void tracing_record_cmdline(struct task_struct *tsk)
bc0c38d1 1214{
0fb9656d 1215 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
bc0c38d1
SR
1216 return;
1217
7ffbd48d
SR
1218 if (!__this_cpu_read(trace_cmdline_save))
1219 return;
1220
1221 __this_cpu_write(trace_cmdline_save, false);
1222
bc0c38d1
SR
1223 trace_save_cmdline(tsk);
1224}
1225
45dcd8b8 1226void
38697053
SR
1227tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1228 int pc)
bc0c38d1
SR
1229{
1230 struct task_struct *tsk = current;
bc0c38d1 1231
777e208d
SR
1232 entry->preempt_count = pc & 0xff;
1233 entry->pid = (tsk) ? tsk->pid : 0;
1234 entry->flags =
9244489a 1235#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
2e2ca155 1236 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
9244489a
SR
1237#else
1238 TRACE_FLAG_IRQS_NOSUPPORT |
1239#endif
bc0c38d1
SR
1240 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1241 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1242 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1243}
f413cdb8 1244EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
bc0c38d1 1245
e77405ad
SR
1246struct ring_buffer_event *
1247trace_buffer_lock_reserve(struct ring_buffer *buffer,
1248 int type,
1249 unsigned long len,
1250 unsigned long flags, int pc)
51a763dd
ACM
1251{
1252 struct ring_buffer_event *event;
1253
e77405ad 1254 event = ring_buffer_lock_reserve(buffer, len);
51a763dd
ACM
1255 if (event != NULL) {
1256 struct trace_entry *ent = ring_buffer_event_data(event);
1257
1258 tracing_generic_entry_update(ent, flags, pc);
1259 ent->type = type;
1260 }
1261
1262 return event;
1263}
51a763dd 1264
7ffbd48d
SR
1265void
1266__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1267{
1268 __this_cpu_write(trace_cmdline_save, true);
0d5c6e1c
SR
1269 if (trace_wakeup_needed) {
1270 trace_wakeup_needed = false;
1271 /* irq_work_queue() supplies it's own memory barriers */
1272 irq_work_queue(&trace_work_wakeup);
1273 }
7ffbd48d
SR
1274 ring_buffer_unlock_commit(buffer, event);
1275}
1276
e77405ad
SR
1277static inline void
1278__trace_buffer_unlock_commit(struct ring_buffer *buffer,
1279 struct ring_buffer_event *event,
0d5c6e1c 1280 unsigned long flags, int pc)
51a763dd 1281{
7ffbd48d 1282 __buffer_unlock_commit(buffer, event);
51a763dd 1283
e77405ad
SR
1284 ftrace_trace_stack(buffer, flags, 6, pc);
1285 ftrace_trace_userstack(buffer, flags, pc);
07edf712
FW
1286}
1287
e77405ad
SR
1288void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1289 struct ring_buffer_event *event,
1290 unsigned long flags, int pc)
07edf712 1291{
0d5c6e1c 1292 __trace_buffer_unlock_commit(buffer, event, flags, pc);
51a763dd 1293}
0d5c6e1c 1294EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
51a763dd 1295
ef5580d0 1296struct ring_buffer_event *
e77405ad
SR
1297trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1298 int type, unsigned long len,
ef5580d0
SR
1299 unsigned long flags, int pc)
1300{
e77405ad
SR
1301 *current_rb = global_trace.buffer;
1302 return trace_buffer_lock_reserve(*current_rb,
ef5580d0
SR
1303 type, len, flags, pc);
1304}
94487d6d 1305EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
ef5580d0 1306
e77405ad
SR
1307void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1308 struct ring_buffer_event *event,
ef5580d0
SR
1309 unsigned long flags, int pc)
1310{
0d5c6e1c 1311 __trace_buffer_unlock_commit(buffer, event, flags, pc);
07edf712 1312}
94487d6d 1313EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
07edf712 1314
0d5c6e1c
SR
1315void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1316 struct ring_buffer_event *event,
1317 unsigned long flags, int pc,
1318 struct pt_regs *regs)
1fd8df2c 1319{
7ffbd48d 1320 __buffer_unlock_commit(buffer, event);
1fd8df2c
MH
1321
1322 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1323 ftrace_trace_userstack(buffer, flags, pc);
1324}
0d5c6e1c 1325EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1fd8df2c 1326
e77405ad
SR
1327void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1328 struct ring_buffer_event *event)
77d9f465 1329{
e77405ad 1330 ring_buffer_discard_commit(buffer, event);
ef5580d0 1331}
12acd473 1332EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
ef5580d0 1333
e309b41d 1334void
7be42151 1335trace_function(struct trace_array *tr,
38697053
SR
1336 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1337 int pc)
bc0c38d1 1338{
e1112b4d 1339 struct ftrace_event_call *call = &event_function;
e77405ad 1340 struct ring_buffer *buffer = tr->buffer;
3928a8a2 1341 struct ring_buffer_event *event;
777e208d 1342 struct ftrace_entry *entry;
bc0c38d1 1343
d769041f 1344 /* If we are reading the ring buffer, don't trace */
dd17c8f7 1345 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
d769041f
SR
1346 return;
1347
e77405ad 1348 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
51a763dd 1349 flags, pc);
3928a8a2
SR
1350 if (!event)
1351 return;
1352 entry = ring_buffer_event_data(event);
777e208d
SR
1353 entry->ip = ip;
1354 entry->parent_ip = parent_ip;
e1112b4d 1355
e77405ad 1356 if (!filter_check_discard(call, entry, buffer, event))
7ffbd48d 1357 __buffer_unlock_commit(buffer, event);
bc0c38d1
SR
1358}
1359
e309b41d 1360void
2e0f5761 1361ftrace(struct trace_array *tr, struct trace_array_cpu *data,
38697053
SR
1362 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1363 int pc)
2e0f5761
IM
1364{
1365 if (likely(!atomic_read(&data->disabled)))
7be42151 1366 trace_function(tr, ip, parent_ip, flags, pc);
2e0f5761
IM
1367}
1368
c0a0d0d3 1369#ifdef CONFIG_STACKTRACE
4a9bd3f1
SR
1370
1371#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1372struct ftrace_stack {
1373 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1374};
1375
1376static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1377static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1378
e77405ad 1379static void __ftrace_trace_stack(struct ring_buffer *buffer,
53614991 1380 unsigned long flags,
1fd8df2c 1381 int skip, int pc, struct pt_regs *regs)
86387f7e 1382{
e1112b4d 1383 struct ftrace_event_call *call = &event_kernel_stack;
3928a8a2 1384 struct ring_buffer_event *event;
777e208d 1385 struct stack_entry *entry;
86387f7e 1386 struct stack_trace trace;
4a9bd3f1
SR
1387 int use_stack;
1388 int size = FTRACE_STACK_ENTRIES;
1389
1390 trace.nr_entries = 0;
1391 trace.skip = skip;
1392
1393 /*
1394 * Since events can happen in NMIs there's no safe way to
1395 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1396 * or NMI comes in, it will just have to use the default
1397 * FTRACE_STACK_SIZE.
1398 */
1399 preempt_disable_notrace();
1400
82146529 1401 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
4a9bd3f1
SR
1402 /*
1403 * We don't need any atomic variables, just a barrier.
1404 * If an interrupt comes in, we don't care, because it would
1405 * have exited and put the counter back to what we want.
1406 * We just need a barrier to keep gcc from moving things
1407 * around.
1408 */
1409 barrier();
1410 if (use_stack == 1) {
1411 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1412 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1413
1414 if (regs)
1415 save_stack_trace_regs(regs, &trace);
1416 else
1417 save_stack_trace(&trace);
1418
1419 if (trace.nr_entries > size)
1420 size = trace.nr_entries;
1421 } else
1422 /* From now on, use_stack is a boolean */
1423 use_stack = 0;
1424
1425 size *= sizeof(unsigned long);
86387f7e 1426
e77405ad 1427 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
4a9bd3f1 1428 sizeof(*entry) + size, flags, pc);
3928a8a2 1429 if (!event)
4a9bd3f1
SR
1430 goto out;
1431 entry = ring_buffer_event_data(event);
86387f7e 1432
4a9bd3f1
SR
1433 memset(&entry->caller, 0, size);
1434
1435 if (use_stack)
1436 memcpy(&entry->caller, trace.entries,
1437 trace.nr_entries * sizeof(unsigned long));
1438 else {
1439 trace.max_entries = FTRACE_STACK_ENTRIES;
1440 trace.entries = entry->caller;
1441 if (regs)
1442 save_stack_trace_regs(regs, &trace);
1443 else
1444 save_stack_trace(&trace);
1445 }
1446
1447 entry->size = trace.nr_entries;
86387f7e 1448
e77405ad 1449 if (!filter_check_discard(call, entry, buffer, event))
7ffbd48d 1450 __buffer_unlock_commit(buffer, event);
4a9bd3f1
SR
1451
1452 out:
1453 /* Again, don't let gcc optimize things here */
1454 barrier();
82146529 1455 __this_cpu_dec(ftrace_stack_reserve);
4a9bd3f1
SR
1456 preempt_enable_notrace();
1457
f0a920d5
IM
1458}
1459
1fd8df2c
MH
1460void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1461 int skip, int pc, struct pt_regs *regs)
1462{
1463 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1464 return;
1465
1466 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1467}
1468
e77405ad
SR
1469void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1470 int skip, int pc)
53614991
SR
1471{
1472 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1473 return;
1474
1fd8df2c 1475 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
53614991
SR
1476}
1477
c0a0d0d3
FW
1478void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1479 int pc)
38697053 1480{
1fd8df2c 1481 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
38697053
SR
1482}
1483
03889384
SR
1484/**
1485 * trace_dump_stack - record a stack back trace in the trace buffer
1486 */
1487void trace_dump_stack(void)
1488{
1489 unsigned long flags;
1490
1491 if (tracing_disabled || tracing_selftest_running)
e36c5458 1492 return;
03889384
SR
1493
1494 local_save_flags(flags);
1495
1496 /* skipping 3 traces, seems to get us at the caller of this function */
1fd8df2c 1497 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
03889384
SR
1498}
1499
91e86e56
SR
1500static DEFINE_PER_CPU(int, user_stack_count);
1501
e77405ad
SR
1502void
1503ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
02b67518 1504{
e1112b4d 1505 struct ftrace_event_call *call = &event_user_stack;
8d7c6a96 1506 struct ring_buffer_event *event;
02b67518
TE
1507 struct userstack_entry *entry;
1508 struct stack_trace trace;
02b67518
TE
1509
1510 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1511 return;
1512
b6345879
SR
1513 /*
1514 * NMIs can not handle page faults, even with fix ups.
1515 * The save user stack can (and often does) fault.
1516 */
1517 if (unlikely(in_nmi()))
1518 return;
02b67518 1519
91e86e56
SR
1520 /*
1521 * prevent recursion, since the user stack tracing may
1522 * trigger other kernel events.
1523 */
1524 preempt_disable();
1525 if (__this_cpu_read(user_stack_count))
1526 goto out;
1527
1528 __this_cpu_inc(user_stack_count);
1529
e77405ad 1530 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
51a763dd 1531 sizeof(*entry), flags, pc);
02b67518 1532 if (!event)
1dbd1951 1533 goto out_drop_count;
02b67518 1534 entry = ring_buffer_event_data(event);
02b67518 1535
48659d31 1536 entry->tgid = current->tgid;
02b67518
TE
1537 memset(&entry->caller, 0, sizeof(entry->caller));
1538
1539 trace.nr_entries = 0;
1540 trace.max_entries = FTRACE_STACK_ENTRIES;
1541 trace.skip = 0;
1542 trace.entries = entry->caller;
1543
1544 save_stack_trace_user(&trace);
e77405ad 1545 if (!filter_check_discard(call, entry, buffer, event))
7ffbd48d 1546 __buffer_unlock_commit(buffer, event);
91e86e56 1547
1dbd1951 1548 out_drop_count:
91e86e56 1549 __this_cpu_dec(user_stack_count);
91e86e56
SR
1550 out:
1551 preempt_enable();
02b67518
TE
1552}
1553
4fd27358
HE
1554#ifdef UNUSED
1555static void __trace_userstack(struct trace_array *tr, unsigned long flags)
02b67518 1556{
7be42151 1557 ftrace_trace_userstack(tr, flags, preempt_count());
02b67518 1558}
4fd27358 1559#endif /* UNUSED */
02b67518 1560
c0a0d0d3
FW
1561#endif /* CONFIG_STACKTRACE */
1562
07d777fe
SR
1563/* created for use with alloc_percpu */
1564struct trace_buffer_struct {
1565 char buffer[TRACE_BUF_SIZE];
1566};
1567
1568static struct trace_buffer_struct *trace_percpu_buffer;
1569static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1570static struct trace_buffer_struct *trace_percpu_irq_buffer;
1571static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1572
1573/*
1574 * The buffer used is dependent on the context. There is a per cpu
1575 * buffer for normal context, softirq contex, hard irq context and
1576 * for NMI context. Thise allows for lockless recording.
1577 *
1578 * Note, if the buffers failed to be allocated, then this returns NULL
1579 */
1580static char *get_trace_buf(void)
1581{
1582 struct trace_buffer_struct *percpu_buffer;
07d777fe
SR
1583
1584 /*
1585 * If we have allocated per cpu buffers, then we do not
1586 * need to do any locking.
1587 */
1588 if (in_nmi())
1589 percpu_buffer = trace_percpu_nmi_buffer;
1590 else if (in_irq())
1591 percpu_buffer = trace_percpu_irq_buffer;
1592 else if (in_softirq())
1593 percpu_buffer = trace_percpu_sirq_buffer;
1594 else
1595 percpu_buffer = trace_percpu_buffer;
1596
1597 if (!percpu_buffer)
1598 return NULL;
1599
d8a0349c 1600 return this_cpu_ptr(&percpu_buffer->buffer[0]);
07d777fe
SR
1601}
1602
1603static int alloc_percpu_trace_buffer(void)
1604{
1605 struct trace_buffer_struct *buffers;
1606 struct trace_buffer_struct *sirq_buffers;
1607 struct trace_buffer_struct *irq_buffers;
1608 struct trace_buffer_struct *nmi_buffers;
1609
1610 buffers = alloc_percpu(struct trace_buffer_struct);
1611 if (!buffers)
1612 goto err_warn;
1613
1614 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1615 if (!sirq_buffers)
1616 goto err_sirq;
1617
1618 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1619 if (!irq_buffers)
1620 goto err_irq;
1621
1622 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1623 if (!nmi_buffers)
1624 goto err_nmi;
1625
1626 trace_percpu_buffer = buffers;
1627 trace_percpu_sirq_buffer = sirq_buffers;
1628 trace_percpu_irq_buffer = irq_buffers;
1629 trace_percpu_nmi_buffer = nmi_buffers;
1630
1631 return 0;
1632
1633 err_nmi:
1634 free_percpu(irq_buffers);
1635 err_irq:
1636 free_percpu(sirq_buffers);
1637 err_sirq:
1638 free_percpu(buffers);
1639 err_warn:
1640 WARN(1, "Could not allocate percpu trace_printk buffer");
1641 return -ENOMEM;
1642}
1643
81698831
SR
1644static int buffers_allocated;
1645
07d777fe
SR
1646void trace_printk_init_buffers(void)
1647{
07d777fe
SR
1648 if (buffers_allocated)
1649 return;
1650
1651 if (alloc_percpu_trace_buffer())
1652 return;
1653
1654 pr_info("ftrace: Allocated trace_printk buffers\n");
1655
b382ede6
SR
1656 /* Expand the buffers to set size */
1657 tracing_update_buffers();
1658
07d777fe 1659 buffers_allocated = 1;
81698831
SR
1660
1661 /*
1662 * trace_printk_init_buffers() can be called by modules.
1663 * If that happens, then we need to start cmdline recording
1664 * directly here. If the global_trace.buffer is already
1665 * allocated here, then this was called by module code.
1666 */
1667 if (global_trace.buffer)
1668 tracing_start_cmdline_record();
1669}
1670
1671void trace_printk_start_comm(void)
1672{
1673 /* Start tracing comms if trace printk is set */
1674 if (!buffers_allocated)
1675 return;
1676 tracing_start_cmdline_record();
1677}
1678
1679static void trace_printk_start_stop_comm(int enabled)
1680{
1681 if (!buffers_allocated)
1682 return;
1683
1684 if (enabled)
1685 tracing_start_cmdline_record();
1686 else
1687 tracing_stop_cmdline_record();
07d777fe
SR
1688}
1689
769b0441 1690/**
48ead020 1691 * trace_vbprintk - write binary msg to tracing buffer
769b0441
FW
1692 *
1693 */
40ce74f1 1694int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
769b0441 1695{
e1112b4d 1696 struct ftrace_event_call *call = &event_bprint;
769b0441 1697 struct ring_buffer_event *event;
e77405ad 1698 struct ring_buffer *buffer;
769b0441 1699 struct trace_array *tr = &global_trace;
48ead020 1700 struct bprint_entry *entry;
769b0441 1701 unsigned long flags;
07d777fe
SR
1702 char *tbuffer;
1703 int len = 0, size, pc;
769b0441
FW
1704
1705 if (unlikely(tracing_selftest_running || tracing_disabled))
1706 return 0;
1707
1708 /* Don't pollute graph traces with trace_vprintk internals */
1709 pause_graph_tracing();
1710
1711 pc = preempt_count();
5168ae50 1712 preempt_disable_notrace();
769b0441 1713
07d777fe
SR
1714 tbuffer = get_trace_buf();
1715 if (!tbuffer) {
1716 len = 0;
769b0441 1717 goto out;
07d777fe 1718 }
769b0441 1719
07d777fe 1720 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
769b0441 1721
07d777fe
SR
1722 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1723 goto out;
769b0441 1724
07d777fe 1725 local_save_flags(flags);
769b0441 1726 size = sizeof(*entry) + sizeof(u32) * len;
e77405ad
SR
1727 buffer = tr->buffer;
1728 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1729 flags, pc);
769b0441 1730 if (!event)
07d777fe 1731 goto out;
769b0441
FW
1732 entry = ring_buffer_event_data(event);
1733 entry->ip = ip;
769b0441
FW
1734 entry->fmt = fmt;
1735
07d777fe 1736 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
d931369b 1737 if (!filter_check_discard(call, entry, buffer, event)) {
7ffbd48d 1738 __buffer_unlock_commit(buffer, event);
d931369b
SR
1739 ftrace_trace_stack(buffer, flags, 6, pc);
1740 }
769b0441 1741
769b0441 1742out:
5168ae50 1743 preempt_enable_notrace();
769b0441
FW
1744 unpause_graph_tracing();
1745
1746 return len;
1747}
48ead020
FW
1748EXPORT_SYMBOL_GPL(trace_vbprintk);
1749
659372d3
SR
1750int trace_array_printk(struct trace_array *tr,
1751 unsigned long ip, const char *fmt, ...)
1752{
1753 int ret;
1754 va_list ap;
1755
1756 if (!(trace_flags & TRACE_ITER_PRINTK))
1757 return 0;
1758
1759 va_start(ap, fmt);
1760 ret = trace_array_vprintk(tr, ip, fmt, ap);
1761 va_end(ap);
1762 return ret;
1763}
1764
1765int trace_array_vprintk(struct trace_array *tr,
1766 unsigned long ip, const char *fmt, va_list args)
48ead020 1767{
e1112b4d 1768 struct ftrace_event_call *call = &event_print;
48ead020 1769 struct ring_buffer_event *event;
e77405ad 1770 struct ring_buffer *buffer;
07d777fe 1771 int len = 0, size, pc;
48ead020 1772 struct print_entry *entry;
07d777fe
SR
1773 unsigned long flags;
1774 char *tbuffer;
48ead020
FW
1775
1776 if (tracing_disabled || tracing_selftest_running)
1777 return 0;
1778
07d777fe
SR
1779 /* Don't pollute graph traces with trace_vprintk internals */
1780 pause_graph_tracing();
1781
48ead020
FW
1782 pc = preempt_count();
1783 preempt_disable_notrace();
48ead020 1784
07d777fe
SR
1785
1786 tbuffer = get_trace_buf();
1787 if (!tbuffer) {
1788 len = 0;
48ead020 1789 goto out;
07d777fe 1790 }
48ead020 1791
07d777fe
SR
1792 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1793 if (len > TRACE_BUF_SIZE)
1794 goto out;
48ead020 1795
07d777fe 1796 local_save_flags(flags);
48ead020 1797 size = sizeof(*entry) + len + 1;
e77405ad
SR
1798 buffer = tr->buffer;
1799 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
07d777fe 1800 flags, pc);
48ead020 1801 if (!event)
07d777fe 1802 goto out;
48ead020 1803 entry = ring_buffer_event_data(event);
c13d2f7c 1804 entry->ip = ip;
48ead020 1805
07d777fe 1806 memcpy(&entry->buf, tbuffer, len);
c13d2f7c 1807 entry->buf[len] = '\0';
d931369b 1808 if (!filter_check_discard(call, entry, buffer, event)) {
7ffbd48d 1809 __buffer_unlock_commit(buffer, event);
07d777fe 1810 ftrace_trace_stack(buffer, flags, 6, pc);
d931369b 1811 }
48ead020
FW
1812 out:
1813 preempt_enable_notrace();
07d777fe 1814 unpause_graph_tracing();
48ead020
FW
1815
1816 return len;
1817}
659372d3
SR
1818
1819int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1820{
a813a159 1821 return trace_array_vprintk(&global_trace, ip, fmt, args);
659372d3 1822}
769b0441
FW
1823EXPORT_SYMBOL_GPL(trace_vprintk);
1824
e2ac8ef5 1825static void trace_iterator_increment(struct trace_iterator *iter)
5a90f577 1826{
6d158a81
SR
1827 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
1828
5a90f577 1829 iter->idx++;
6d158a81
SR
1830 if (buf_iter)
1831 ring_buffer_read(buf_iter, NULL);
5a90f577
SR
1832}
1833
e309b41d 1834static struct trace_entry *
bc21b478
SR
1835peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1836 unsigned long *lost_events)
dd0e545f 1837{
3928a8a2 1838 struct ring_buffer_event *event;
6d158a81 1839 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
dd0e545f 1840
d769041f
SR
1841 if (buf_iter)
1842 event = ring_buffer_iter_peek(buf_iter, ts);
1843 else
bc21b478
SR
1844 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1845 lost_events);
d769041f 1846
4a9bd3f1
SR
1847 if (event) {
1848 iter->ent_size = ring_buffer_event_length(event);
1849 return ring_buffer_event_data(event);
1850 }
1851 iter->ent_size = 0;
1852 return NULL;
dd0e545f 1853}
d769041f 1854
dd0e545f 1855static struct trace_entry *
bc21b478
SR
1856__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1857 unsigned long *missing_events, u64 *ent_ts)
bc0c38d1 1858{
3928a8a2 1859 struct ring_buffer *buffer = iter->tr->buffer;
bc0c38d1 1860 struct trace_entry *ent, *next = NULL;
aa27497c 1861 unsigned long lost_events = 0, next_lost = 0;
b04cc6b1 1862 int cpu_file = iter->cpu_file;
3928a8a2 1863 u64 next_ts = 0, ts;
bc0c38d1 1864 int next_cpu = -1;
12b5da34 1865 int next_size = 0;
bc0c38d1
SR
1866 int cpu;
1867
b04cc6b1
FW
1868 /*
1869 * If we are in a per_cpu trace file, don't bother by iterating over
1870 * all cpu and peek directly.
1871 */
ae3b5093 1872 if (cpu_file > RING_BUFFER_ALL_CPUS) {
b04cc6b1
FW
1873 if (ring_buffer_empty_cpu(buffer, cpu_file))
1874 return NULL;
bc21b478 1875 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
b04cc6b1
FW
1876 if (ent_cpu)
1877 *ent_cpu = cpu_file;
1878
1879 return ent;
1880 }
1881
ab46428c 1882 for_each_tracing_cpu(cpu) {
dd0e545f 1883
3928a8a2
SR
1884 if (ring_buffer_empty_cpu(buffer, cpu))
1885 continue;
dd0e545f 1886
bc21b478 1887 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
dd0e545f 1888
cdd31cd2
IM
1889 /*
1890 * Pick the entry with the smallest timestamp:
1891 */
3928a8a2 1892 if (ent && (!next || ts < next_ts)) {
bc0c38d1
SR
1893 next = ent;
1894 next_cpu = cpu;
3928a8a2 1895 next_ts = ts;
bc21b478 1896 next_lost = lost_events;
12b5da34 1897 next_size = iter->ent_size;
bc0c38d1
SR
1898 }
1899 }
1900
12b5da34
SR
1901 iter->ent_size = next_size;
1902
bc0c38d1
SR
1903 if (ent_cpu)
1904 *ent_cpu = next_cpu;
1905
3928a8a2
SR
1906 if (ent_ts)
1907 *ent_ts = next_ts;
1908
bc21b478
SR
1909 if (missing_events)
1910 *missing_events = next_lost;
1911
bc0c38d1
SR
1912 return next;
1913}
1914
dd0e545f 1915/* Find the next real entry, without updating the iterator itself */
c4a8e8be
FW
1916struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1917 int *ent_cpu, u64 *ent_ts)
bc0c38d1 1918{
bc21b478 1919 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
dd0e545f
SR
1920}
1921
1922/* Find the next real entry, and increment the iterator to the next entry */
955b61e5 1923void *trace_find_next_entry_inc(struct trace_iterator *iter)
dd0e545f 1924{
bc21b478
SR
1925 iter->ent = __find_next_entry(iter, &iter->cpu,
1926 &iter->lost_events, &iter->ts);
dd0e545f 1927
3928a8a2 1928 if (iter->ent)
e2ac8ef5 1929 trace_iterator_increment(iter);
dd0e545f 1930
3928a8a2 1931 return iter->ent ? iter : NULL;
b3806b43 1932}
bc0c38d1 1933
e309b41d 1934static void trace_consume(struct trace_iterator *iter)
b3806b43 1935{
bc21b478
SR
1936 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1937 &iter->lost_events);
bc0c38d1
SR
1938}
1939
e309b41d 1940static void *s_next(struct seq_file *m, void *v, loff_t *pos)
bc0c38d1
SR
1941{
1942 struct trace_iterator *iter = m->private;
bc0c38d1 1943 int i = (int)*pos;
4e3c3333 1944 void *ent;
bc0c38d1 1945
a63ce5b3
SR
1946 WARN_ON_ONCE(iter->leftover);
1947
bc0c38d1
SR
1948 (*pos)++;
1949
1950 /* can't go backwards */
1951 if (iter->idx > i)
1952 return NULL;
1953
1954 if (iter->idx < 0)
955b61e5 1955 ent = trace_find_next_entry_inc(iter);
bc0c38d1
SR
1956 else
1957 ent = iter;
1958
1959 while (ent && iter->idx < i)
955b61e5 1960 ent = trace_find_next_entry_inc(iter);
bc0c38d1
SR
1961
1962 iter->pos = *pos;
1963
bc0c38d1
SR
1964 return ent;
1965}
1966
955b61e5 1967void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2f26ebd5
SR
1968{
1969 struct trace_array *tr = iter->tr;
1970 struct ring_buffer_event *event;
1971 struct ring_buffer_iter *buf_iter;
1972 unsigned long entries = 0;
1973 u64 ts;
1974
1975 tr->data[cpu]->skipped_entries = 0;
1976
6d158a81
SR
1977 buf_iter = trace_buffer_iter(iter, cpu);
1978 if (!buf_iter)
2f26ebd5
SR
1979 return;
1980
2f26ebd5
SR
1981 ring_buffer_iter_reset(buf_iter);
1982
1983 /*
1984 * We could have the case with the max latency tracers
1985 * that a reset never took place on a cpu. This is evident
1986 * by the timestamp being before the start of the buffer.
1987 */
1988 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1989 if (ts >= iter->tr->time_start)
1990 break;
1991 entries++;
1992 ring_buffer_read(buf_iter, NULL);
1993 }
1994
1995 tr->data[cpu]->skipped_entries = entries;
1996}
1997
d7350c3f 1998/*
d7350c3f
FW
1999 * The current tracer is copied to avoid a global locking
2000 * all around.
2001 */
bc0c38d1
SR
2002static void *s_start(struct seq_file *m, loff_t *pos)
2003{
2004 struct trace_iterator *iter = m->private;
2b6080f2 2005 struct trace_array *tr = iter->tr;
b04cc6b1 2006 int cpu_file = iter->cpu_file;
bc0c38d1
SR
2007 void *p = NULL;
2008 loff_t l = 0;
3928a8a2 2009 int cpu;
bc0c38d1 2010
2fd196ec
HT
2011 /*
2012 * copy the tracer to avoid using a global lock all around.
2013 * iter->trace is a copy of current_trace, the pointer to the
2014 * name may be used instead of a strcmp(), as iter->trace->name
2015 * will point to the same string as current_trace->name.
2016 */
bc0c38d1 2017 mutex_lock(&trace_types_lock);
2b6080f2
SR
2018 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2019 *iter->trace = *tr->current_trace;
d7350c3f 2020 mutex_unlock(&trace_types_lock);
bc0c38d1 2021
debdd57f
HT
2022 if (iter->snapshot && iter->trace->use_max_tr)
2023 return ERR_PTR(-EBUSY);
2024
2025 if (!iter->snapshot)
2026 atomic_inc(&trace_record_cmdline_disabled);
bc0c38d1 2027
bc0c38d1
SR
2028 if (*pos != iter->pos) {
2029 iter->ent = NULL;
2030 iter->cpu = 0;
2031 iter->idx = -1;
2032
ae3b5093 2033 if (cpu_file == RING_BUFFER_ALL_CPUS) {
b04cc6b1 2034 for_each_tracing_cpu(cpu)
2f26ebd5 2035 tracing_iter_reset(iter, cpu);
b04cc6b1 2036 } else
2f26ebd5 2037 tracing_iter_reset(iter, cpu_file);
bc0c38d1 2038
ac91d854 2039 iter->leftover = 0;
bc0c38d1
SR
2040 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2041 ;
2042
2043 } else {
a63ce5b3
SR
2044 /*
2045 * If we overflowed the seq_file before, then we want
2046 * to just reuse the trace_seq buffer again.
2047 */
2048 if (iter->leftover)
2049 p = iter;
2050 else {
2051 l = *pos - 1;
2052 p = s_next(m, p, &l);
2053 }
bc0c38d1
SR
2054 }
2055
4f535968 2056 trace_event_read_lock();
7e53bd42 2057 trace_access_lock(cpu_file);
bc0c38d1
SR
2058 return p;
2059}
2060
2061static void s_stop(struct seq_file *m, void *p)
2062{
7e53bd42
LJ
2063 struct trace_iterator *iter = m->private;
2064
debdd57f
HT
2065 if (iter->snapshot && iter->trace->use_max_tr)
2066 return;
2067
2068 if (!iter->snapshot)
2069 atomic_dec(&trace_record_cmdline_disabled);
7e53bd42 2070 trace_access_unlock(iter->cpu_file);
4f535968 2071 trace_event_read_unlock();
bc0c38d1
SR
2072}
2073
39eaf7ef
SR
2074static void
2075get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *entries)
2076{
2077 unsigned long count;
2078 int cpu;
2079
2080 *total = 0;
2081 *entries = 0;
2082
2083 for_each_tracing_cpu(cpu) {
2084 count = ring_buffer_entries_cpu(tr->buffer, cpu);
2085 /*
2086 * If this buffer has skipped entries, then we hold all
2087 * entries for the trace and we need to ignore the
2088 * ones before the time stamp.
2089 */
2090 if (tr->data[cpu]->skipped_entries) {
2091 count -= tr->data[cpu]->skipped_entries;
2092 /* total is the same as the entries */
2093 *total += count;
2094 } else
2095 *total += count +
2096 ring_buffer_overrun_cpu(tr->buffer, cpu);
2097 *entries += count;
2098 }
2099}
2100
e309b41d 2101static void print_lat_help_header(struct seq_file *m)
bc0c38d1 2102{
a6168353
ME
2103 seq_puts(m, "# _------=> CPU# \n");
2104 seq_puts(m, "# / _-----=> irqs-off \n");
2105 seq_puts(m, "# | / _----=> need-resched \n");
2106 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2107 seq_puts(m, "# ||| / _--=> preempt-depth \n");
e6e1e259
SR
2108 seq_puts(m, "# |||| / delay \n");
2109 seq_puts(m, "# cmd pid ||||| time | caller \n");
2110 seq_puts(m, "# \\ / ||||| \\ | / \n");
bc0c38d1
SR
2111}
2112
39eaf7ef 2113static void print_event_info(struct trace_array *tr, struct seq_file *m)
bc0c38d1 2114{
39eaf7ef
SR
2115 unsigned long total;
2116 unsigned long entries;
2117
2118 get_total_entries(tr, &total, &entries);
2119 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2120 entries, total, num_online_cpus());
2121 seq_puts(m, "#\n");
2122}
2123
2124static void print_func_help_header(struct trace_array *tr, struct seq_file *m)
2125{
2126 print_event_info(tr, m);
77271ce4 2127 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
a6168353 2128 seq_puts(m, "# | | | | |\n");
bc0c38d1
SR
2129}
2130
39eaf7ef 2131static void print_func_help_header_irq(struct trace_array *tr, struct seq_file *m)
77271ce4 2132{
39eaf7ef 2133 print_event_info(tr, m);
77271ce4
SR
2134 seq_puts(m, "# _-----=> irqs-off\n");
2135 seq_puts(m, "# / _----=> need-resched\n");
2136 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2137 seq_puts(m, "# || / _--=> preempt-depth\n");
2138 seq_puts(m, "# ||| / delay\n");
2139 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2140 seq_puts(m, "# | | | |||| | |\n");
2141}
bc0c38d1 2142
62b915f1 2143void
bc0c38d1
SR
2144print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2145{
2146 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2147 struct trace_array *tr = iter->tr;
2148 struct trace_array_cpu *data = tr->data[tr->cpu];
2b6080f2 2149 struct tracer *type = iter->trace;
39eaf7ef
SR
2150 unsigned long entries;
2151 unsigned long total;
bc0c38d1
SR
2152 const char *name = "preemption";
2153
d840f718 2154 name = type->name;
bc0c38d1 2155
39eaf7ef 2156 get_total_entries(tr, &total, &entries);
bc0c38d1 2157
888b55dc 2158 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
bc0c38d1 2159 name, UTS_RELEASE);
888b55dc 2160 seq_puts(m, "# -----------------------------------"
bc0c38d1 2161 "---------------------------------\n");
888b55dc 2162 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
bc0c38d1 2163 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
57f50be1 2164 nsecs_to_usecs(data->saved_latency),
bc0c38d1 2165 entries,
4c11d7ae 2166 total,
bc0c38d1
SR
2167 tr->cpu,
2168#if defined(CONFIG_PREEMPT_NONE)
2169 "server",
2170#elif defined(CONFIG_PREEMPT_VOLUNTARY)
2171 "desktop",
b5c21b45 2172#elif defined(CONFIG_PREEMPT)
bc0c38d1
SR
2173 "preempt",
2174#else
2175 "unknown",
2176#endif
2177 /* These are reserved for later use */
2178 0, 0, 0, 0);
2179#ifdef CONFIG_SMP
2180 seq_printf(m, " #P:%d)\n", num_online_cpus());
2181#else
2182 seq_puts(m, ")\n");
2183#endif
888b55dc
KM
2184 seq_puts(m, "# -----------------\n");
2185 seq_printf(m, "# | task: %.16s-%d "
bc0c38d1 2186 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
d20b92ab
EB
2187 data->comm, data->pid,
2188 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
bc0c38d1 2189 data->policy, data->rt_priority);
888b55dc 2190 seq_puts(m, "# -----------------\n");
bc0c38d1
SR
2191
2192 if (data->critical_start) {
888b55dc 2193 seq_puts(m, "# => started at: ");
214023c3
SR
2194 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2195 trace_print_seq(m, &iter->seq);
888b55dc 2196 seq_puts(m, "\n# => ended at: ");
214023c3
SR
2197 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2198 trace_print_seq(m, &iter->seq);
8248ac05 2199 seq_puts(m, "\n#\n");
bc0c38d1
SR
2200 }
2201
888b55dc 2202 seq_puts(m, "#\n");
bc0c38d1
SR
2203}
2204
a309720c
SR
2205static void test_cpu_buff_start(struct trace_iterator *iter)
2206{
2207 struct trace_seq *s = &iter->seq;
2208
12ef7d44
SR
2209 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2210 return;
2211
2212 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2213 return;
2214
4462344e 2215 if (cpumask_test_cpu(iter->cpu, iter->started))
a309720c
SR
2216 return;
2217
2f26ebd5
SR
2218 if (iter->tr->data[iter->cpu]->skipped_entries)
2219 return;
2220
4462344e 2221 cpumask_set_cpu(iter->cpu, iter->started);
b0dfa978
FW
2222
2223 /* Don't print started cpu buffer for the first entry of the trace */
2224 if (iter->idx > 1)
2225 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2226 iter->cpu);
a309720c
SR
2227}
2228
2c4f035f 2229static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 2230{
214023c3 2231 struct trace_seq *s = &iter->seq;
bc0c38d1 2232 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 2233 struct trace_entry *entry;
f633cef0 2234 struct trace_event *event;
bc0c38d1 2235
4e3c3333 2236 entry = iter->ent;
dd0e545f 2237
a309720c
SR
2238 test_cpu_buff_start(iter);
2239
c4a8e8be 2240 event = ftrace_find_event(entry->type);
bc0c38d1 2241
c4a8e8be 2242 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
27d48be8
SR
2243 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2244 if (!trace_print_lat_context(iter))
2245 goto partial;
2246 } else {
2247 if (!trace_print_context(iter))
2248 goto partial;
2249 }
c4a8e8be 2250 }
bc0c38d1 2251
268ccda0 2252 if (event)
a9a57763 2253 return event->funcs->trace(iter, sym_flags, event);
d9793bd8
ACM
2254
2255 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2256 goto partial;
02b67518 2257
2c4f035f 2258 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
2259partial:
2260 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1
SR
2261}
2262
2c4f035f 2263static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
f9896bf3
IM
2264{
2265 struct trace_seq *s = &iter->seq;
2266 struct trace_entry *entry;
f633cef0 2267 struct trace_event *event;
f9896bf3
IM
2268
2269 entry = iter->ent;
dd0e545f 2270
c4a8e8be 2271 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
d9793bd8
ACM
2272 if (!trace_seq_printf(s, "%d %d %llu ",
2273 entry->pid, iter->cpu, iter->ts))
2274 goto partial;
c4a8e8be 2275 }
f9896bf3 2276
f633cef0 2277 event = ftrace_find_event(entry->type);
268ccda0 2278 if (event)
a9a57763 2279 return event->funcs->raw(iter, 0, event);
d9793bd8
ACM
2280
2281 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2282 goto partial;
777e208d 2283
2c4f035f 2284 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
2285partial:
2286 return TRACE_TYPE_PARTIAL_LINE;
f9896bf3
IM
2287}
2288
2c4f035f 2289static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
5e3ca0ec
IM
2290{
2291 struct trace_seq *s = &iter->seq;
2292 unsigned char newline = '\n';
2293 struct trace_entry *entry;
f633cef0 2294 struct trace_event *event;
5e3ca0ec
IM
2295
2296 entry = iter->ent;
dd0e545f 2297
c4a8e8be
FW
2298 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2299 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2300 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2301 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2302 }
5e3ca0ec 2303
f633cef0 2304 event = ftrace_find_event(entry->type);
268ccda0 2305 if (event) {
a9a57763 2306 enum print_line_t ret = event->funcs->hex(iter, 0, event);
d9793bd8
ACM
2307 if (ret != TRACE_TYPE_HANDLED)
2308 return ret;
2309 }
7104f300 2310
5e3ca0ec
IM
2311 SEQ_PUT_FIELD_RET(s, newline);
2312
2c4f035f 2313 return TRACE_TYPE_HANDLED;
5e3ca0ec
IM
2314}
2315
2c4f035f 2316static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
cb0f12aa
IM
2317{
2318 struct trace_seq *s = &iter->seq;
2319 struct trace_entry *entry;
f633cef0 2320 struct trace_event *event;
cb0f12aa
IM
2321
2322 entry = iter->ent;
dd0e545f 2323
c4a8e8be
FW
2324 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2325 SEQ_PUT_FIELD_RET(s, entry->pid);
1830b52d 2326 SEQ_PUT_FIELD_RET(s, iter->cpu);
c4a8e8be
FW
2327 SEQ_PUT_FIELD_RET(s, iter->ts);
2328 }
cb0f12aa 2329
f633cef0 2330 event = ftrace_find_event(entry->type);
a9a57763
SR
2331 return event ? event->funcs->binary(iter, 0, event) :
2332 TRACE_TYPE_HANDLED;
cb0f12aa
IM
2333}
2334
62b915f1 2335int trace_empty(struct trace_iterator *iter)
bc0c38d1 2336{
6d158a81 2337 struct ring_buffer_iter *buf_iter;
bc0c38d1
SR
2338 int cpu;
2339
9aba60fe 2340 /* If we are looking at one CPU buffer, only check that one */
ae3b5093 2341 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
9aba60fe 2342 cpu = iter->cpu_file;
6d158a81
SR
2343 buf_iter = trace_buffer_iter(iter, cpu);
2344 if (buf_iter) {
2345 if (!ring_buffer_iter_empty(buf_iter))
9aba60fe
SR
2346 return 0;
2347 } else {
2348 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2349 return 0;
2350 }
2351 return 1;
2352 }
2353
ab46428c 2354 for_each_tracing_cpu(cpu) {
6d158a81
SR
2355 buf_iter = trace_buffer_iter(iter, cpu);
2356 if (buf_iter) {
2357 if (!ring_buffer_iter_empty(buf_iter))
d769041f
SR
2358 return 0;
2359 } else {
2360 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2361 return 0;
2362 }
bc0c38d1 2363 }
d769041f 2364
797d3712 2365 return 1;
bc0c38d1
SR
2366}
2367
4f535968 2368/* Called with trace_event_read_lock() held. */
955b61e5 2369enum print_line_t print_trace_line(struct trace_iterator *iter)
f9896bf3 2370{
2c4f035f
FW
2371 enum print_line_t ret;
2372
ee5e51f5
JO
2373 if (iter->lost_events &&
2374 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2375 iter->cpu, iter->lost_events))
2376 return TRACE_TYPE_PARTIAL_LINE;
bc21b478 2377
2c4f035f
FW
2378 if (iter->trace && iter->trace->print_line) {
2379 ret = iter->trace->print_line(iter);
2380 if (ret != TRACE_TYPE_UNHANDLED)
2381 return ret;
2382 }
72829bc3 2383
48ead020
FW
2384 if (iter->ent->type == TRACE_BPRINT &&
2385 trace_flags & TRACE_ITER_PRINTK &&
2386 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 2387 return trace_print_bprintk_msg_only(iter);
48ead020 2388
66896a85
FW
2389 if (iter->ent->type == TRACE_PRINT &&
2390 trace_flags & TRACE_ITER_PRINTK &&
2391 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 2392 return trace_print_printk_msg_only(iter);
66896a85 2393
cb0f12aa
IM
2394 if (trace_flags & TRACE_ITER_BIN)
2395 return print_bin_fmt(iter);
2396
5e3ca0ec
IM
2397 if (trace_flags & TRACE_ITER_HEX)
2398 return print_hex_fmt(iter);
2399
f9896bf3
IM
2400 if (trace_flags & TRACE_ITER_RAW)
2401 return print_raw_fmt(iter);
2402
f9896bf3
IM
2403 return print_trace_fmt(iter);
2404}
2405
7e9a49ef
JO
2406void trace_latency_header(struct seq_file *m)
2407{
2408 struct trace_iterator *iter = m->private;
2409
2410 /* print nothing if the buffers are empty */
2411 if (trace_empty(iter))
2412 return;
2413
2414 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2415 print_trace_header(m, iter);
2416
2417 if (!(trace_flags & TRACE_ITER_VERBOSE))
2418 print_lat_help_header(m);
2419}
2420
62b915f1
JO
2421void trace_default_header(struct seq_file *m)
2422{
2423 struct trace_iterator *iter = m->private;
2424
f56e7f8e
JO
2425 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2426 return;
2427
62b915f1
JO
2428 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2429 /* print nothing if the buffers are empty */
2430 if (trace_empty(iter))
2431 return;
2432 print_trace_header(m, iter);
2433 if (!(trace_flags & TRACE_ITER_VERBOSE))
2434 print_lat_help_header(m);
2435 } else {
77271ce4
SR
2436 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2437 if (trace_flags & TRACE_ITER_IRQ_INFO)
39eaf7ef 2438 print_func_help_header_irq(iter->tr, m);
77271ce4 2439 else
39eaf7ef 2440 print_func_help_header(iter->tr, m);
77271ce4 2441 }
62b915f1
JO
2442 }
2443}
2444
e0a413f6
SR
2445static void test_ftrace_alive(struct seq_file *m)
2446{
2447 if (!ftrace_is_dead())
2448 return;
2449 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2450 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2451}
2452
d8741e2e
SRRH
2453#ifdef CONFIG_TRACER_MAX_TRACE
2454static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2455{
2456 if (iter->trace->allocated_snapshot)
2457 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2458 else
2459 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2460
2461 seq_printf(m, "# Snapshot commands:\n");
2462 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2463 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2464 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2465 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2466 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2467 seq_printf(m, "# is not a '0' or '1')\n");
2468}
2469#else
2470/* Should never be called */
2471static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2472#endif
2473
bc0c38d1
SR
2474static int s_show(struct seq_file *m, void *v)
2475{
2476 struct trace_iterator *iter = v;
a63ce5b3 2477 int ret;
bc0c38d1
SR
2478
2479 if (iter->ent == NULL) {
2480 if (iter->tr) {
2481 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2482 seq_puts(m, "#\n");
e0a413f6 2483 test_ftrace_alive(m);
bc0c38d1 2484 }
d8741e2e
SRRH
2485 if (iter->snapshot && trace_empty(iter))
2486 print_snapshot_help(m, iter);
2487 else if (iter->trace && iter->trace->print_header)
8bba1bf5 2488 iter->trace->print_header(m);
62b915f1
JO
2489 else
2490 trace_default_header(m);
2491
a63ce5b3
SR
2492 } else if (iter->leftover) {
2493 /*
2494 * If we filled the seq_file buffer earlier, we
2495 * want to just show it now.
2496 */
2497 ret = trace_print_seq(m, &iter->seq);
2498
2499 /* ret should this time be zero, but you never know */
2500 iter->leftover = ret;
2501
bc0c38d1 2502 } else {
f9896bf3 2503 print_trace_line(iter);
a63ce5b3
SR
2504 ret = trace_print_seq(m, &iter->seq);
2505 /*
2506 * If we overflow the seq_file buffer, then it will
2507 * ask us for this data again at start up.
2508 * Use that instead.
2509 * ret is 0 if seq_file write succeeded.
2510 * -1 otherwise.
2511 */
2512 iter->leftover = ret;
bc0c38d1
SR
2513 }
2514
2515 return 0;
2516}
2517
88e9d34c 2518static const struct seq_operations tracer_seq_ops = {
4bf39a94
IM
2519 .start = s_start,
2520 .next = s_next,
2521 .stop = s_stop,
2522 .show = s_show,
bc0c38d1
SR
2523};
2524
e309b41d 2525static struct trace_iterator *
debdd57f 2526__tracing_open(struct inode *inode, struct file *file, bool snapshot)
bc0c38d1 2527{
2b6080f2
SR
2528 struct trace_cpu *tc = inode->i_private;
2529 struct trace_array *tr = tc->tr;
bc0c38d1 2530 struct trace_iterator *iter;
50e18b94 2531 int cpu;
bc0c38d1 2532
85a2f9b4
SR
2533 if (tracing_disabled)
2534 return ERR_PTR(-ENODEV);
60a11774 2535
50e18b94 2536 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
85a2f9b4
SR
2537 if (!iter)
2538 return ERR_PTR(-ENOMEM);
bc0c38d1 2539
6d158a81
SR
2540 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2541 GFP_KERNEL);
93574fcc
DC
2542 if (!iter->buffer_iter)
2543 goto release;
2544
d7350c3f
FW
2545 /*
2546 * We make a copy of the current tracer to avoid concurrent
2547 * changes on it while we are reading.
2548 */
bc0c38d1 2549 mutex_lock(&trace_types_lock);
d7350c3f 2550 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
85a2f9b4 2551 if (!iter->trace)
d7350c3f 2552 goto fail;
85a2f9b4 2553
2b6080f2 2554 *iter->trace = *tr->current_trace;
d7350c3f 2555
79f55997 2556 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
b0dfa978
FW
2557 goto fail;
2558
2b6080f2
SR
2559 /* Currently only the top directory has a snapshot */
2560 if (tr->current_trace->print_max || snapshot)
bc0c38d1
SR
2561 iter->tr = &max_tr;
2562 else
2b6080f2 2563 iter->tr = tr;
debdd57f 2564 iter->snapshot = snapshot;
bc0c38d1 2565 iter->pos = -1;
d7350c3f 2566 mutex_init(&iter->mutex);
2b6080f2 2567 iter->cpu_file = tc->cpu;
bc0c38d1 2568
8bba1bf5
MM
2569 /* Notify the tracer early; before we stop tracing. */
2570 if (iter->trace && iter->trace->open)
a93751ca 2571 iter->trace->open(iter);
8bba1bf5 2572
12ef7d44
SR
2573 /* Annotate start of buffers if we had overruns */
2574 if (ring_buffer_overruns(iter->tr->buffer))
2575 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2576
8be0709f
DS
2577 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2578 if (trace_clocks[trace_clock_id].in_ns)
2579 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2580
debdd57f
HT
2581 /* stop the trace while dumping if we are not opening "snapshot" */
2582 if (!iter->snapshot)
2b6080f2 2583 tracing_stop_tr(tr);
2f26ebd5 2584
ae3b5093 2585 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
b04cc6b1 2586 for_each_tracing_cpu(cpu) {
b04cc6b1 2587 iter->buffer_iter[cpu] =
72c9ddfd
DM
2588 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2589 }
2590 ring_buffer_read_prepare_sync();
2591 for_each_tracing_cpu(cpu) {
2592 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 2593 tracing_iter_reset(iter, cpu);
b04cc6b1
FW
2594 }
2595 } else {
2596 cpu = iter->cpu_file;
3928a8a2 2597 iter->buffer_iter[cpu] =
72c9ddfd
DM
2598 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2599 ring_buffer_read_prepare_sync();
2600 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 2601 tracing_iter_reset(iter, cpu);
3928a8a2
SR
2602 }
2603
bc0c38d1
SR
2604 mutex_unlock(&trace_types_lock);
2605
bc0c38d1 2606 return iter;
3928a8a2 2607
d7350c3f 2608 fail:
3928a8a2 2609 mutex_unlock(&trace_types_lock);
d7350c3f 2610 kfree(iter->trace);
6d158a81 2611 kfree(iter->buffer_iter);
93574fcc 2612release:
50e18b94
JO
2613 seq_release_private(inode, file);
2614 return ERR_PTR(-ENOMEM);
bc0c38d1
SR
2615}
2616
2617int tracing_open_generic(struct inode *inode, struct file *filp)
2618{
60a11774
SR
2619 if (tracing_disabled)
2620 return -ENODEV;
2621
bc0c38d1
SR
2622 filp->private_data = inode->i_private;
2623 return 0;
2624}
2625
4fd27358 2626static int tracing_release(struct inode *inode, struct file *file)
bc0c38d1 2627{
907f2784 2628 struct seq_file *m = file->private_data;
4acd4d00 2629 struct trace_iterator *iter;
2b6080f2 2630 struct trace_array *tr;
3928a8a2 2631 int cpu;
bc0c38d1 2632
4acd4d00
SR
2633 if (!(file->f_mode & FMODE_READ))
2634 return 0;
2635
2636 iter = m->private;
2637
2b6080f2
SR
2638 /* Only the global tracer has a matching max_tr */
2639 if (iter->tr == &max_tr)
2640 tr = &global_trace;
2641 else
2642 tr = iter->tr;
2643
bc0c38d1 2644 mutex_lock(&trace_types_lock);
3928a8a2
SR
2645 for_each_tracing_cpu(cpu) {
2646 if (iter->buffer_iter[cpu])
2647 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2648 }
2649
bc0c38d1
SR
2650 if (iter->trace && iter->trace->close)
2651 iter->trace->close(iter);
2652
debdd57f
HT
2653 if (!iter->snapshot)
2654 /* reenable tracing if it was previously enabled */
2b6080f2 2655 tracing_start_tr(tr);
bc0c38d1
SR
2656 mutex_unlock(&trace_types_lock);
2657
d7350c3f 2658 mutex_destroy(&iter->mutex);
b0dfa978 2659 free_cpumask_var(iter->started);
d7350c3f 2660 kfree(iter->trace);
6d158a81 2661 kfree(iter->buffer_iter);
50e18b94 2662 seq_release_private(inode, file);
bc0c38d1
SR
2663 return 0;
2664}
2665
2666static int tracing_open(struct inode *inode, struct file *file)
2667{
85a2f9b4
SR
2668 struct trace_iterator *iter;
2669 int ret = 0;
bc0c38d1 2670
4acd4d00
SR
2671 /* If this file was open for write, then erase contents */
2672 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 2673 (file->f_flags & O_TRUNC)) {
2b6080f2
SR
2674 struct trace_cpu *tc = inode->i_private;
2675 struct trace_array *tr = tc->tr;
bc0c38d1 2676
2b6080f2
SR
2677 if (tc->cpu == RING_BUFFER_ALL_CPUS)
2678 tracing_reset_online_cpus(tr);
4acd4d00 2679 else
2b6080f2 2680 tracing_reset(tr, tc->cpu);
4acd4d00 2681 }
bc0c38d1 2682
4acd4d00 2683 if (file->f_mode & FMODE_READ) {
debdd57f 2684 iter = __tracing_open(inode, file, false);
4acd4d00
SR
2685 if (IS_ERR(iter))
2686 ret = PTR_ERR(iter);
2687 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2688 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2689 }
bc0c38d1
SR
2690 return ret;
2691}
2692
e309b41d 2693static void *
bc0c38d1
SR
2694t_next(struct seq_file *m, void *v, loff_t *pos)
2695{
f129e965 2696 struct tracer *t = v;
bc0c38d1
SR
2697
2698 (*pos)++;
2699
2700 if (t)
2701 t = t->next;
2702
bc0c38d1
SR
2703 return t;
2704}
2705
2706static void *t_start(struct seq_file *m, loff_t *pos)
2707{
f129e965 2708 struct tracer *t;
bc0c38d1
SR
2709 loff_t l = 0;
2710
2711 mutex_lock(&trace_types_lock);
f129e965 2712 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
bc0c38d1
SR
2713 ;
2714
2715 return t;
2716}
2717
2718static void t_stop(struct seq_file *m, void *p)
2719{
2720 mutex_unlock(&trace_types_lock);
2721}
2722
2723static int t_show(struct seq_file *m, void *v)
2724{
2725 struct tracer *t = v;
2726
2727 if (!t)
2728 return 0;
2729
2730 seq_printf(m, "%s", t->name);
2731 if (t->next)
2732 seq_putc(m, ' ');
2733 else
2734 seq_putc(m, '\n');
2735
2736 return 0;
2737}
2738
88e9d34c 2739static const struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
2740 .start = t_start,
2741 .next = t_next,
2742 .stop = t_stop,
2743 .show = t_show,
bc0c38d1
SR
2744};
2745
2746static int show_traces_open(struct inode *inode, struct file *file)
2747{
60a11774
SR
2748 if (tracing_disabled)
2749 return -ENODEV;
2750
f129e965 2751 return seq_open(file, &show_traces_seq_ops);
bc0c38d1
SR
2752}
2753
4acd4d00
SR
2754static ssize_t
2755tracing_write_stub(struct file *filp, const char __user *ubuf,
2756 size_t count, loff_t *ppos)
2757{
2758 return count;
2759}
2760
364829b1
SP
2761static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2762{
2763 if (file->f_mode & FMODE_READ)
2764 return seq_lseek(file, offset, origin);
2765 else
2766 return 0;
2767}
2768
5e2336a0 2769static const struct file_operations tracing_fops = {
4bf39a94
IM
2770 .open = tracing_open,
2771 .read = seq_read,
4acd4d00 2772 .write = tracing_write_stub,
364829b1 2773 .llseek = tracing_seek,
4bf39a94 2774 .release = tracing_release,
bc0c38d1
SR
2775};
2776
5e2336a0 2777static const struct file_operations show_traces_fops = {
c7078de1
IM
2778 .open = show_traces_open,
2779 .read = seq_read,
2780 .release = seq_release,
b444786f 2781 .llseek = seq_lseek,
c7078de1
IM
2782};
2783
36dfe925
IM
2784/*
2785 * Only trace on a CPU if the bitmask is set:
2786 */
9e01c1b7 2787static cpumask_var_t tracing_cpumask;
36dfe925
IM
2788
2789/*
2790 * The tracer itself will not take this lock, but still we want
2791 * to provide a consistent cpumask to user-space:
2792 */
2793static DEFINE_MUTEX(tracing_cpumask_update_lock);
2794
2795/*
2796 * Temporary storage for the character representation of the
2797 * CPU bitmask (and one more byte for the newline):
2798 */
2799static char mask_str[NR_CPUS + 1];
2800
c7078de1
IM
2801static ssize_t
2802tracing_cpumask_read(struct file *filp, char __user *ubuf,
2803 size_t count, loff_t *ppos)
2804{
36dfe925 2805 int len;
c7078de1
IM
2806
2807 mutex_lock(&tracing_cpumask_update_lock);
36dfe925 2808
9e01c1b7 2809 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
36dfe925
IM
2810 if (count - len < 2) {
2811 count = -EINVAL;
2812 goto out_err;
2813 }
2814 len += sprintf(mask_str + len, "\n");
2815 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2816
2817out_err:
c7078de1
IM
2818 mutex_unlock(&tracing_cpumask_update_lock);
2819
2820 return count;
2821}
2822
2823static ssize_t
2824tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2825 size_t count, loff_t *ppos)
2826{
2b6080f2 2827 struct trace_array *tr = filp->private_data;
9e01c1b7 2828 cpumask_var_t tracing_cpumask_new;
2b6080f2 2829 int err, cpu;
9e01c1b7
RR
2830
2831 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2832 return -ENOMEM;
c7078de1 2833
9e01c1b7 2834 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
c7078de1 2835 if (err)
36dfe925
IM
2836 goto err_unlock;
2837
215368e8
LZ
2838 mutex_lock(&tracing_cpumask_update_lock);
2839
a5e25883 2840 local_irq_disable();
0199c4e6 2841 arch_spin_lock(&ftrace_max_lock);
ab46428c 2842 for_each_tracing_cpu(cpu) {
36dfe925
IM
2843 /*
2844 * Increase/decrease the disabled counter if we are
2845 * about to flip a bit in the cpumask:
2846 */
9e01c1b7
RR
2847 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2848 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2b6080f2
SR
2849 atomic_inc(&tr->data[cpu]->disabled);
2850 ring_buffer_record_disable_cpu(tr->buffer, cpu);
36dfe925 2851 }
9e01c1b7
RR
2852 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2853 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2b6080f2
SR
2854 atomic_dec(&tr->data[cpu]->disabled);
2855 ring_buffer_record_enable_cpu(tr->buffer, cpu);
36dfe925
IM
2856 }
2857 }
0199c4e6 2858 arch_spin_unlock(&ftrace_max_lock);
a5e25883 2859 local_irq_enable();
36dfe925 2860
9e01c1b7 2861 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
36dfe925
IM
2862
2863 mutex_unlock(&tracing_cpumask_update_lock);
9e01c1b7 2864 free_cpumask_var(tracing_cpumask_new);
c7078de1
IM
2865
2866 return count;
36dfe925
IM
2867
2868err_unlock:
215368e8 2869 free_cpumask_var(tracing_cpumask_new);
36dfe925
IM
2870
2871 return err;
c7078de1
IM
2872}
2873
5e2336a0 2874static const struct file_operations tracing_cpumask_fops = {
c7078de1
IM
2875 .open = tracing_open_generic,
2876 .read = tracing_cpumask_read,
2877 .write = tracing_cpumask_write,
b444786f 2878 .llseek = generic_file_llseek,
bc0c38d1
SR
2879};
2880
fdb372ed 2881static int tracing_trace_options_show(struct seq_file *m, void *v)
bc0c38d1 2882{
d8e83d26 2883 struct tracer_opt *trace_opts;
2b6080f2 2884 struct trace_array *tr = m->private;
d8e83d26 2885 u32 tracer_flags;
d8e83d26 2886 int i;
adf9f195 2887
d8e83d26 2888 mutex_lock(&trace_types_lock);
2b6080f2
SR
2889 tracer_flags = tr->current_trace->flags->val;
2890 trace_opts = tr->current_trace->flags->opts;
d8e83d26 2891
bc0c38d1
SR
2892 for (i = 0; trace_options[i]; i++) {
2893 if (trace_flags & (1 << i))
fdb372ed 2894 seq_printf(m, "%s\n", trace_options[i]);
bc0c38d1 2895 else
fdb372ed 2896 seq_printf(m, "no%s\n", trace_options[i]);
bc0c38d1
SR
2897 }
2898
adf9f195
FW
2899 for (i = 0; trace_opts[i].name; i++) {
2900 if (tracer_flags & trace_opts[i].bit)
fdb372ed 2901 seq_printf(m, "%s\n", trace_opts[i].name);
adf9f195 2902 else
fdb372ed 2903 seq_printf(m, "no%s\n", trace_opts[i].name);
adf9f195 2904 }
d8e83d26 2905 mutex_unlock(&trace_types_lock);
adf9f195 2906
fdb372ed 2907 return 0;
bc0c38d1 2908}
bc0c38d1 2909
8d18eaaf
LZ
2910static int __set_tracer_option(struct tracer *trace,
2911 struct tracer_flags *tracer_flags,
2912 struct tracer_opt *opts, int neg)
2913{
2914 int ret;
bc0c38d1 2915
8d18eaaf
LZ
2916 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2917 if (ret)
2918 return ret;
2919
2920 if (neg)
2921 tracer_flags->val &= ~opts->bit;
2922 else
2923 tracer_flags->val |= opts->bit;
2924 return 0;
bc0c38d1
SR
2925}
2926
adf9f195
FW
2927/* Try to assign a tracer specific option */
2928static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2929{
7770841e 2930 struct tracer_flags *tracer_flags = trace->flags;
adf9f195 2931 struct tracer_opt *opts = NULL;
8d18eaaf 2932 int i;
adf9f195 2933
7770841e
Z
2934 for (i = 0; tracer_flags->opts[i].name; i++) {
2935 opts = &tracer_flags->opts[i];
adf9f195 2936
8d18eaaf
LZ
2937 if (strcmp(cmp, opts->name) == 0)
2938 return __set_tracer_option(trace, trace->flags,
2939 opts, neg);
adf9f195 2940 }
adf9f195 2941
8d18eaaf 2942 return -EINVAL;
adf9f195
FW
2943}
2944
613f04a0
SRRH
2945/* Some tracers require overwrite to stay enabled */
2946int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
2947{
2948 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
2949 return -1;
2950
2951 return 0;
2952}
2953
2b6080f2 2954int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
af4617bd
SR
2955{
2956 /* do nothing if flag is already set */
2957 if (!!(trace_flags & mask) == !!enabled)
613f04a0
SRRH
2958 return 0;
2959
2960 /* Give the tracer a chance to approve the change */
2b6080f2
SR
2961 if (tr->current_trace->flag_changed)
2962 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
613f04a0 2963 return -EINVAL;
af4617bd
SR
2964
2965 if (enabled)
2966 trace_flags |= mask;
2967 else
2968 trace_flags &= ~mask;
e870e9a1
LZ
2969
2970 if (mask == TRACE_ITER_RECORD_CMD)
2971 trace_event_enable_cmd_record(enabled);
750912fa 2972
80902822 2973 if (mask == TRACE_ITER_OVERWRITE) {
750912fa 2974 ring_buffer_change_overwrite(global_trace.buffer, enabled);
80902822
SRRH
2975#ifdef CONFIG_TRACER_MAX_TRACE
2976 ring_buffer_change_overwrite(max_tr.buffer, enabled);
2977#endif
2978 }
81698831
SR
2979
2980 if (mask == TRACE_ITER_PRINTK)
2981 trace_printk_start_stop_comm(enabled);
613f04a0
SRRH
2982
2983 return 0;
af4617bd
SR
2984}
2985
2b6080f2 2986static int trace_set_options(struct trace_array *tr, char *option)
bc0c38d1 2987{
8d18eaaf 2988 char *cmp;
bc0c38d1 2989 int neg = 0;
613f04a0 2990 int ret = -ENODEV;
bc0c38d1
SR
2991 int i;
2992
7bcfaf54 2993 cmp = strstrip(option);
bc0c38d1 2994
8d18eaaf 2995 if (strncmp(cmp, "no", 2) == 0) {
bc0c38d1
SR
2996 neg = 1;
2997 cmp += 2;
2998 }
2999
69d34da2
SRRH
3000 mutex_lock(&trace_types_lock);
3001
bc0c38d1 3002 for (i = 0; trace_options[i]; i++) {
8d18eaaf 3003 if (strcmp(cmp, trace_options[i]) == 0) {
2b6080f2 3004 ret = set_tracer_flag(tr, 1 << i, !neg);
bc0c38d1
SR
3005 break;
3006 }
3007 }
adf9f195
FW
3008
3009 /* If no option could be set, test the specific tracer options */
69d34da2 3010 if (!trace_options[i])
2b6080f2 3011 ret = set_tracer_option(tr->current_trace, cmp, neg);
69d34da2
SRRH
3012
3013 mutex_unlock(&trace_types_lock);
bc0c38d1 3014
7bcfaf54
SR
3015 return ret;
3016}
3017
3018static ssize_t
3019tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3020 size_t cnt, loff_t *ppos)
3021{
2b6080f2
SR
3022 struct seq_file *m = filp->private_data;
3023 struct trace_array *tr = m->private;
7bcfaf54 3024 char buf[64];
613f04a0 3025 int ret;
7bcfaf54
SR
3026
3027 if (cnt >= sizeof(buf))
3028 return -EINVAL;
3029
3030 if (copy_from_user(&buf, ubuf, cnt))
3031 return -EFAULT;
3032
a8dd2176
SR
3033 buf[cnt] = 0;
3034
2b6080f2 3035 ret = trace_set_options(tr, buf);
613f04a0
SRRH
3036 if (ret < 0)
3037 return ret;
7bcfaf54 3038
cf8517cf 3039 *ppos += cnt;
bc0c38d1
SR
3040
3041 return cnt;
3042}
3043
fdb372ed
LZ
3044static int tracing_trace_options_open(struct inode *inode, struct file *file)
3045{
3046 if (tracing_disabled)
3047 return -ENODEV;
2b6080f2
SR
3048
3049 return single_open(file, tracing_trace_options_show, inode->i_private);
fdb372ed
LZ
3050}
3051
5e2336a0 3052static const struct file_operations tracing_iter_fops = {
fdb372ed
LZ
3053 .open = tracing_trace_options_open,
3054 .read = seq_read,
3055 .llseek = seq_lseek,
3056 .release = single_release,
ee6bce52 3057 .write = tracing_trace_options_write,
bc0c38d1
SR
3058};
3059
7bd2f24c
IM
3060static const char readme_msg[] =
3061 "tracing mini-HOWTO:\n\n"
156f5a78
GL
3062 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
3063 "# cat /sys/kernel/debug/tracing/available_tracers\n"
1e42e83f 3064 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
156f5a78 3065 "# cat /sys/kernel/debug/tracing/current_tracer\n"
bc2b6871 3066 "nop\n"
1e42e83f 3067 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
156f5a78 3068 "# cat /sys/kernel/debug/tracing/current_tracer\n"
1e42e83f 3069 "wakeup\n"
156f5a78 3070 "# cat /sys/kernel/debug/tracing/trace_options\n"
7bd2f24c 3071 "noprint-parent nosym-offset nosym-addr noverbose\n"
156f5a78 3072 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
9b5f8b31 3073 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
156f5a78 3074 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
9b5f8b31 3075 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
7bd2f24c
IM
3076;
3077
3078static ssize_t
3079tracing_readme_read(struct file *filp, char __user *ubuf,
3080 size_t cnt, loff_t *ppos)
3081{
3082 return simple_read_from_buffer(ubuf, cnt, ppos,
3083 readme_msg, strlen(readme_msg));
3084}
3085
5e2336a0 3086static const struct file_operations tracing_readme_fops = {
c7078de1
IM
3087 .open = tracing_open_generic,
3088 .read = tracing_readme_read,
b444786f 3089 .llseek = generic_file_llseek,
7bd2f24c
IM
3090};
3091
69abe6a5
AP
3092static ssize_t
3093tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3094 size_t cnt, loff_t *ppos)
3095{
3096 char *buf_comm;
3097 char *file_buf;
3098 char *buf;
3099 int len = 0;
3100 int pid;
3101 int i;
3102
3103 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3104 if (!file_buf)
3105 return -ENOMEM;
3106
3107 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3108 if (!buf_comm) {
3109 kfree(file_buf);
3110 return -ENOMEM;
3111 }
3112
3113 buf = file_buf;
3114
3115 for (i = 0; i < SAVED_CMDLINES; i++) {
3116 int r;
3117
3118 pid = map_cmdline_to_pid[i];
3119 if (pid == -1 || pid == NO_CMDLINE_MAP)
3120 continue;
3121
3122 trace_find_cmdline(pid, buf_comm);
3123 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3124 buf += r;
3125 len += r;
3126 }
3127
3128 len = simple_read_from_buffer(ubuf, cnt, ppos,
3129 file_buf, len);
3130
3131 kfree(file_buf);
3132 kfree(buf_comm);
3133
3134 return len;
3135}
3136
3137static const struct file_operations tracing_saved_cmdlines_fops = {
3138 .open = tracing_open_generic,
3139 .read = tracing_saved_cmdlines_read,
b444786f 3140 .llseek = generic_file_llseek,
69abe6a5
AP
3141};
3142
bc0c38d1
SR
3143static ssize_t
3144tracing_set_trace_read(struct file *filp, char __user *ubuf,
3145 size_t cnt, loff_t *ppos)
3146{
2b6080f2 3147 struct trace_array *tr = filp->private_data;
ee6c2c1b 3148 char buf[MAX_TRACER_SIZE+2];
bc0c38d1
SR
3149 int r;
3150
3151 mutex_lock(&trace_types_lock);
2b6080f2 3152 r = sprintf(buf, "%s\n", tr->current_trace->name);
bc0c38d1
SR
3153 mutex_unlock(&trace_types_lock);
3154
4bf39a94 3155 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
3156}
3157
b6f11df2
ACM
3158int tracer_init(struct tracer *t, struct trace_array *tr)
3159{
3160 tracing_reset_online_cpus(tr);
3161 return t->init(tr);
3162}
3163
438ced17
VN
3164static void set_buffer_entries(struct trace_array *tr, unsigned long val)
3165{
3166 int cpu;
3167 for_each_tracing_cpu(cpu)
3168 tr->data[cpu]->entries = val;
3169}
3170
d60da506
HT
3171/* resize @tr's buffer to the size of @size_tr's entries */
3172static int resize_buffer_duplicate_size(struct trace_array *tr,
3173 struct trace_array *size_tr, int cpu_id)
3174{
3175 int cpu, ret = 0;
3176
3177 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3178 for_each_tracing_cpu(cpu) {
3179 ret = ring_buffer_resize(tr->buffer,
3180 size_tr->data[cpu]->entries, cpu);
3181 if (ret < 0)
3182 break;
3183 tr->data[cpu]->entries = size_tr->data[cpu]->entries;
3184 }
3185 } else {
3186 ret = ring_buffer_resize(tr->buffer,
3187 size_tr->data[cpu_id]->entries, cpu_id);
3188 if (ret == 0)
3189 tr->data[cpu_id]->entries =
3190 size_tr->data[cpu_id]->entries;
3191 }
3192
3193 return ret;
3194}
3195
2b6080f2
SR
3196static int __tracing_resize_ring_buffer(struct trace_array *tr,
3197 unsigned long size, int cpu)
73c5162a
SR
3198{
3199 int ret;
3200
3201 /*
3202 * If kernel or user changes the size of the ring buffer
a123c52b
SR
3203 * we use the size that was given, and we can forget about
3204 * expanding it later.
73c5162a
SR
3205 */
3206 ring_buffer_expanded = 1;
3207
b382ede6 3208 /* May be called before buffers are initialized */
2b6080f2 3209 if (!tr->buffer)
b382ede6
SR
3210 return 0;
3211
2b6080f2 3212 ret = ring_buffer_resize(tr->buffer, size, cpu);
73c5162a
SR
3213 if (ret < 0)
3214 return ret;
3215
2b6080f2
SR
3216 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3217 !tr->current_trace->use_max_tr)
ef710e10
KM
3218 goto out;
3219
438ced17 3220 ret = ring_buffer_resize(max_tr.buffer, size, cpu);
73c5162a 3221 if (ret < 0) {
2b6080f2 3222 int r = resize_buffer_duplicate_size(tr, tr, cpu);
73c5162a 3223 if (r < 0) {
a123c52b
SR
3224 /*
3225 * AARGH! We are left with different
3226 * size max buffer!!!!
3227 * The max buffer is our "snapshot" buffer.
3228 * When a tracer needs a snapshot (one of the
3229 * latency tracers), it swaps the max buffer
3230 * with the saved snap shot. We succeeded to
3231 * update the size of the main buffer, but failed to
3232 * update the size of the max buffer. But when we tried
3233 * to reset the main buffer to the original size, we
3234 * failed there too. This is very unlikely to
3235 * happen, but if it does, warn and kill all
3236 * tracing.
3237 */
73c5162a
SR
3238 WARN_ON(1);
3239 tracing_disabled = 1;
3240 }
3241 return ret;
3242 }
3243
438ced17
VN
3244 if (cpu == RING_BUFFER_ALL_CPUS)
3245 set_buffer_entries(&max_tr, size);
3246 else
3247 max_tr.data[cpu]->entries = size;
3248
ef710e10 3249 out:
438ced17 3250 if (cpu == RING_BUFFER_ALL_CPUS)
2b6080f2 3251 set_buffer_entries(tr, size);
438ced17 3252 else
2b6080f2 3253 tr->data[cpu]->entries = size;
73c5162a
SR
3254
3255 return ret;
3256}
3257
2b6080f2
SR
3258static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3259 unsigned long size, int cpu_id)
4f271a2a 3260{
83f40318 3261 int ret = size;
4f271a2a
VN
3262
3263 mutex_lock(&trace_types_lock);
3264
438ced17
VN
3265 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3266 /* make sure, this cpu is enabled in the mask */
3267 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3268 ret = -EINVAL;
3269 goto out;
3270 }
3271 }
4f271a2a 3272
2b6080f2 3273 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
4f271a2a
VN
3274 if (ret < 0)
3275 ret = -ENOMEM;
3276
438ced17 3277out:
4f271a2a
VN
3278 mutex_unlock(&trace_types_lock);
3279
3280 return ret;
3281}
3282
ef710e10 3283
1852fcce
SR
3284/**
3285 * tracing_update_buffers - used by tracing facility to expand ring buffers
3286 *
3287 * To save on memory when the tracing is never used on a system with it
3288 * configured in. The ring buffers are set to a minimum size. But once
3289 * a user starts to use the tracing facility, then they need to grow
3290 * to their default size.
3291 *
3292 * This function is to be called when a tracer is about to be used.
3293 */
3294int tracing_update_buffers(void)
3295{
3296 int ret = 0;
3297
1027fcb2 3298 mutex_lock(&trace_types_lock);
1852fcce 3299 if (!ring_buffer_expanded)
2b6080f2 3300 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
438ced17 3301 RING_BUFFER_ALL_CPUS);
1027fcb2 3302 mutex_unlock(&trace_types_lock);
1852fcce
SR
3303
3304 return ret;
3305}
3306
577b785f
SR
3307struct trace_option_dentry;
3308
3309static struct trace_option_dentry *
2b6080f2 3310create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
577b785f
SR
3311
3312static void
3313destroy_trace_option_files(struct trace_option_dentry *topts);
3314
b2821ae6 3315static int tracing_set_tracer(const char *buf)
bc0c38d1 3316{
577b785f 3317 static struct trace_option_dentry *topts;
bc0c38d1
SR
3318 struct trace_array *tr = &global_trace;
3319 struct tracer *t;
34600f0e 3320 bool had_max_tr;
d9e54076 3321 int ret = 0;
bc0c38d1 3322
1027fcb2
SR
3323 mutex_lock(&trace_types_lock);
3324
73c5162a 3325 if (!ring_buffer_expanded) {
2b6080f2 3326 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
438ced17 3327 RING_BUFFER_ALL_CPUS);
73c5162a 3328 if (ret < 0)
59f586db 3329 goto out;
73c5162a
SR
3330 ret = 0;
3331 }
3332
bc0c38d1
SR
3333 for (t = trace_types; t; t = t->next) {
3334 if (strcmp(t->name, buf) == 0)
3335 break;
3336 }
c2931e05
FW
3337 if (!t) {
3338 ret = -EINVAL;
3339 goto out;
3340 }
2b6080f2 3341 if (t == tr->current_trace)
bc0c38d1
SR
3342 goto out;
3343
9f029e83 3344 trace_branch_disable();
613f04a0 3345
2b6080f2 3346 tr->current_trace->enabled = false;
613f04a0 3347
2b6080f2
SR
3348 if (tr->current_trace->reset)
3349 tr->current_trace->reset(tr);
34600f0e 3350
2b6080f2
SR
3351 had_max_tr = tr->current_trace->allocated_snapshot;
3352 tr->current_trace = &nop_trace;
34600f0e
SR
3353
3354 if (had_max_tr && !t->use_max_tr) {
3355 /*
3356 * We need to make sure that the update_max_tr sees that
3357 * current_trace changed to nop_trace to keep it from
3358 * swapping the buffers after we resize it.
3359 * The update_max_tr is called from interrupts disabled
3360 * so a synchronized_sched() is sufficient.
3361 */
3362 synchronize_sched();
ef710e10
KM
3363 /*
3364 * We don't free the ring buffer. instead, resize it because
3365 * The max_tr ring buffer has some state (e.g. ring->clock) and
3366 * we want preserve it.
3367 */
438ced17
VN
3368 ring_buffer_resize(max_tr.buffer, 1, RING_BUFFER_ALL_CPUS);
3369 set_buffer_entries(&max_tr, 1);
debdd57f 3370 tracing_reset_online_cpus(&max_tr);
2b6080f2 3371 tr->current_trace->allocated_snapshot = false;
ef710e10 3372 }
577b785f
SR
3373 destroy_trace_option_files(topts);
3374
2b6080f2 3375 topts = create_trace_option_files(tr, t);
34600f0e 3376 if (t->use_max_tr && !had_max_tr) {
438ced17 3377 /* we need to make per cpu buffer sizes equivalent */
d60da506
HT
3378 ret = resize_buffer_duplicate_size(&max_tr, &global_trace,
3379 RING_BUFFER_ALL_CPUS);
3380 if (ret < 0)
3381 goto out;
debdd57f 3382 t->allocated_snapshot = true;
ef710e10 3383 }
577b785f 3384
1c80025a 3385 if (t->init) {
b6f11df2 3386 ret = tracer_init(t, tr);
1c80025a
FW
3387 if (ret)
3388 goto out;
3389 }
bc0c38d1 3390
2b6080f2
SR
3391 tr->current_trace = t;
3392 tr->current_trace->enabled = true;
9f029e83 3393 trace_branch_enable(tr);
bc0c38d1
SR
3394 out:
3395 mutex_unlock(&trace_types_lock);
3396
d9e54076
PZ
3397 return ret;
3398}
3399
3400static ssize_t
3401tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3402 size_t cnt, loff_t *ppos)
3403{
ee6c2c1b 3404 char buf[MAX_TRACER_SIZE+1];
d9e54076
PZ
3405 int i;
3406 size_t ret;
e6e7a65a
FW
3407 int err;
3408
3409 ret = cnt;
d9e54076 3410
ee6c2c1b
LZ
3411 if (cnt > MAX_TRACER_SIZE)
3412 cnt = MAX_TRACER_SIZE;
d9e54076
PZ
3413
3414 if (copy_from_user(&buf, ubuf, cnt))
3415 return -EFAULT;
3416
3417 buf[cnt] = 0;
3418
3419 /* strip ending whitespace. */
3420 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3421 buf[i] = 0;
3422
e6e7a65a
FW
3423 err = tracing_set_tracer(buf);
3424 if (err)
3425 return err;
d9e54076 3426
cf8517cf 3427 *ppos += ret;
bc0c38d1 3428
c2931e05 3429 return ret;
bc0c38d1
SR
3430}
3431
3432static ssize_t
3433tracing_max_lat_read(struct file *filp, char __user *ubuf,
3434 size_t cnt, loff_t *ppos)
3435{
3436 unsigned long *ptr = filp->private_data;
3437 char buf[64];
3438 int r;
3439
cffae437 3440 r = snprintf(buf, sizeof(buf), "%ld\n",
bc0c38d1 3441 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
cffae437
SR
3442 if (r > sizeof(buf))
3443 r = sizeof(buf);
4bf39a94 3444 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
3445}
3446
3447static ssize_t
3448tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3449 size_t cnt, loff_t *ppos)
3450{
5e39841c 3451 unsigned long *ptr = filp->private_data;
5e39841c 3452 unsigned long val;
c6caeeb1 3453 int ret;
bc0c38d1 3454
22fe9b54
PH
3455 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3456 if (ret)
c6caeeb1 3457 return ret;
bc0c38d1
SR
3458
3459 *ptr = val * 1000;
3460
3461 return cnt;
3462}
3463
b3806b43
SR
3464static int tracing_open_pipe(struct inode *inode, struct file *filp)
3465{
2b6080f2
SR
3466 struct trace_cpu *tc = inode->i_private;
3467 struct trace_array *tr = tc->tr;
b3806b43 3468 struct trace_iterator *iter;
b04cc6b1 3469 int ret = 0;
b3806b43
SR
3470
3471 if (tracing_disabled)
3472 return -ENODEV;
3473
b04cc6b1
FW
3474 mutex_lock(&trace_types_lock);
3475
b3806b43
SR
3476 /* create a buffer to store the information to pass to userspace */
3477 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
b04cc6b1
FW
3478 if (!iter) {
3479 ret = -ENOMEM;
3480 goto out;
3481 }
b3806b43 3482
d7350c3f
FW
3483 /*
3484 * We make a copy of the current tracer to avoid concurrent
3485 * changes on it while we are reading.
3486 */
3487 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3488 if (!iter->trace) {
3489 ret = -ENOMEM;
3490 goto fail;
3491 }
2b6080f2 3492 *iter->trace = *tr->current_trace;
d7350c3f 3493
4462344e 3494 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
b04cc6b1 3495 ret = -ENOMEM;
d7350c3f 3496 goto fail;
4462344e
RR
3497 }
3498
a309720c 3499 /* trace pipe does not show start of buffer */
4462344e 3500 cpumask_setall(iter->started);
a309720c 3501
112f38a7
SR
3502 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3503 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3504
8be0709f
DS
3505 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3506 if (trace_clocks[trace_clock_id].in_ns)
3507 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3508
2b6080f2
SR
3509 iter->cpu_file = tc->cpu;
3510 iter->tr = tc->tr;
d7350c3f 3511 mutex_init(&iter->mutex);
b3806b43
SR
3512 filp->private_data = iter;
3513
107bad8b
SR
3514 if (iter->trace->pipe_open)
3515 iter->trace->pipe_open(iter);
107bad8b 3516
b444786f 3517 nonseekable_open(inode, filp);
b04cc6b1
FW
3518out:
3519 mutex_unlock(&trace_types_lock);
3520 return ret;
d7350c3f
FW
3521
3522fail:
3523 kfree(iter->trace);
3524 kfree(iter);
3525 mutex_unlock(&trace_types_lock);
3526 return ret;
b3806b43
SR
3527}
3528
3529static int tracing_release_pipe(struct inode *inode, struct file *file)
3530{
3531 struct trace_iterator *iter = file->private_data;
3532
b04cc6b1
FW
3533 mutex_lock(&trace_types_lock);
3534
29bf4a5e 3535 if (iter->trace->pipe_close)
c521efd1
SR
3536 iter->trace->pipe_close(iter);
3537
b04cc6b1
FW
3538 mutex_unlock(&trace_types_lock);
3539
4462344e 3540 free_cpumask_var(iter->started);
d7350c3f
FW
3541 mutex_destroy(&iter->mutex);
3542 kfree(iter->trace);
b3806b43 3543 kfree(iter);
b3806b43
SR
3544
3545 return 0;
3546}
3547
2a2cc8f7
SSP
3548static unsigned int
3549tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3550{
3551 struct trace_iterator *iter = filp->private_data;
3552
3553 if (trace_flags & TRACE_ITER_BLOCK) {
3554 /*
3555 * Always select as readable when in blocking mode
3556 */
3557 return POLLIN | POLLRDNORM;
afc2abc0 3558 } else {
2a2cc8f7
SSP
3559 if (!trace_empty(iter))
3560 return POLLIN | POLLRDNORM;
3561 poll_wait(filp, &trace_wait, poll_table);
3562 if (!trace_empty(iter))
3563 return POLLIN | POLLRDNORM;
3564
3565 return 0;
3566 }
3567}
3568
6eaaa5d5
FW
3569/*
3570 * This is a make-shift waitqueue.
3571 * A tracer might use this callback on some rare cases:
3572 *
3573 * 1) the current tracer might hold the runqueue lock when it wakes up
3574 * a reader, hence a deadlock (sched, function, and function graph tracers)
3575 * 2) the function tracers, trace all functions, we don't want
3576 * the overhead of calling wake_up and friends
3577 * (and tracing them too)
3578 *
3579 * Anyway, this is really very primitive wakeup.
3580 */
3581void poll_wait_pipe(struct trace_iterator *iter)
3582{
3583 set_current_state(TASK_INTERRUPTIBLE);
3584 /* sleep for 100 msecs, and try again. */
3585 schedule_timeout(HZ / 10);
3586}
3587
ff98781b
EGM
3588/* Must be called with trace_types_lock mutex held. */
3589static int tracing_wait_pipe(struct file *filp)
b3806b43
SR
3590{
3591 struct trace_iterator *iter = filp->private_data;
b3806b43 3592
b3806b43 3593 while (trace_empty(iter)) {
2dc8f095 3594
107bad8b 3595 if ((filp->f_flags & O_NONBLOCK)) {
ff98781b 3596 return -EAGAIN;
107bad8b 3597 }
2dc8f095 3598
d7350c3f 3599 mutex_unlock(&iter->mutex);
107bad8b 3600
6eaaa5d5 3601 iter->trace->wait_pipe(iter);
b3806b43 3602
d7350c3f 3603 mutex_lock(&iter->mutex);
107bad8b 3604
6eaaa5d5 3605 if (signal_pending(current))
ff98781b 3606 return -EINTR;
b3806b43
SR
3607
3608 /*
250bfd3d 3609 * We block until we read something and tracing is disabled.
b3806b43
SR
3610 * We still block if tracing is disabled, but we have never
3611 * read anything. This allows a user to cat this file, and
3612 * then enable tracing. But after we have read something,
3613 * we give an EOF when tracing is again disabled.
3614 *
3615 * iter->pos will be 0 if we haven't read anything.
3616 */
250bfd3d 3617 if (!tracing_is_enabled() && iter->pos)
b3806b43 3618 break;
b3806b43
SR
3619 }
3620
ff98781b
EGM
3621 return 1;
3622}
3623
3624/*
3625 * Consumer reader.
3626 */
3627static ssize_t
3628tracing_read_pipe(struct file *filp, char __user *ubuf,
3629 size_t cnt, loff_t *ppos)
3630{
3631 struct trace_iterator *iter = filp->private_data;
2b6080f2 3632 struct trace_array *tr = iter->tr;
ff98781b
EGM
3633 ssize_t sret;
3634
3635 /* return any leftover data */
3636 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3637 if (sret != -EBUSY)
3638 return sret;
3639
f9520750 3640 trace_seq_init(&iter->seq);
ff98781b 3641
d7350c3f 3642 /* copy the tracer to avoid using a global lock all around */
ff98781b 3643 mutex_lock(&trace_types_lock);
2b6080f2
SR
3644 if (unlikely(iter->trace->name != tr->current_trace->name))
3645 *iter->trace = *tr->current_trace;
d7350c3f
FW
3646 mutex_unlock(&trace_types_lock);
3647
3648 /*
3649 * Avoid more than one consumer on a single file descriptor
3650 * This is just a matter of traces coherency, the ring buffer itself
3651 * is protected.
3652 */
3653 mutex_lock(&iter->mutex);
ff98781b
EGM
3654 if (iter->trace->read) {
3655 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3656 if (sret)
3657 goto out;
3658 }
3659
3660waitagain:
3661 sret = tracing_wait_pipe(filp);
3662 if (sret <= 0)
3663 goto out;
3664
b3806b43 3665 /* stop when tracing is finished */
ff98781b
EGM
3666 if (trace_empty(iter)) {
3667 sret = 0;
107bad8b 3668 goto out;
ff98781b 3669 }
b3806b43
SR
3670
3671 if (cnt >= PAGE_SIZE)
3672 cnt = PAGE_SIZE - 1;
3673
53d0aa77 3674 /* reset all but tr, trace, and overruns */
53d0aa77
SR
3675 memset(&iter->seq, 0,
3676 sizeof(struct trace_iterator) -
3677 offsetof(struct trace_iterator, seq));
4823ed7e 3678 iter->pos = -1;
b3806b43 3679
4f535968 3680 trace_event_read_lock();
7e53bd42 3681 trace_access_lock(iter->cpu_file);
955b61e5 3682 while (trace_find_next_entry_inc(iter) != NULL) {
2c4f035f 3683 enum print_line_t ret;
088b1e42
SR
3684 int len = iter->seq.len;
3685
f9896bf3 3686 ret = print_trace_line(iter);
2c4f035f 3687 if (ret == TRACE_TYPE_PARTIAL_LINE) {
088b1e42
SR
3688 /* don't print partial lines */
3689 iter->seq.len = len;
b3806b43 3690 break;
088b1e42 3691 }
b91facc3
FW
3692 if (ret != TRACE_TYPE_NO_CONSUME)
3693 trace_consume(iter);
b3806b43
SR
3694
3695 if (iter->seq.len >= cnt)
3696 break;
ee5e51f5
JO
3697
3698 /*
3699 * Setting the full flag means we reached the trace_seq buffer
3700 * size and we should leave by partial output condition above.
3701 * One of the trace_seq_* functions is not used properly.
3702 */
3703 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3704 iter->ent->type);
b3806b43 3705 }
7e53bd42 3706 trace_access_unlock(iter->cpu_file);
4f535968 3707 trace_event_read_unlock();
b3806b43 3708
b3806b43 3709 /* Now copy what we have to the user */
6c6c2796
PP
3710 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3711 if (iter->seq.readpos >= iter->seq.len)
f9520750 3712 trace_seq_init(&iter->seq);
9ff4b974
PP
3713
3714 /*
25985edc 3715 * If there was nothing to send to user, in spite of consuming trace
9ff4b974
PP
3716 * entries, go back to wait for more entries.
3717 */
6c6c2796 3718 if (sret == -EBUSY)
9ff4b974 3719 goto waitagain;
b3806b43 3720
107bad8b 3721out:
d7350c3f 3722 mutex_unlock(&iter->mutex);
107bad8b 3723
6c6c2796 3724 return sret;
b3806b43
SR
3725}
3726
3c56819b
EGM
3727static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3728 struct pipe_buffer *buf)
3729{
3730 __free_page(buf->page);
3731}
3732
3733static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3734 unsigned int idx)
3735{
3736 __free_page(spd->pages[idx]);
3737}
3738
28dfef8f 3739static const struct pipe_buf_operations tracing_pipe_buf_ops = {
34cd4998
SR
3740 .can_merge = 0,
3741 .map = generic_pipe_buf_map,
3742 .unmap = generic_pipe_buf_unmap,
3743 .confirm = generic_pipe_buf_confirm,
3744 .release = tracing_pipe_buf_release,
3745 .steal = generic_pipe_buf_steal,
3746 .get = generic_pipe_buf_get,
3c56819b
EGM
3747};
3748
34cd4998 3749static size_t
fa7c7f6e 3750tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
34cd4998
SR
3751{
3752 size_t count;
3753 int ret;
3754
3755 /* Seq buffer is page-sized, exactly what we need. */
3756 for (;;) {
3757 count = iter->seq.len;
3758 ret = print_trace_line(iter);
3759 count = iter->seq.len - count;
3760 if (rem < count) {
3761 rem = 0;
3762 iter->seq.len -= count;
3763 break;
3764 }
3765 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3766 iter->seq.len -= count;
3767 break;
3768 }
3769
74e7ff8c
LJ
3770 if (ret != TRACE_TYPE_NO_CONSUME)
3771 trace_consume(iter);
34cd4998 3772 rem -= count;
955b61e5 3773 if (!trace_find_next_entry_inc(iter)) {
34cd4998
SR
3774 rem = 0;
3775 iter->ent = NULL;
3776 break;
3777 }
3778 }
3779
3780 return rem;
3781}
3782
3c56819b
EGM
3783static ssize_t tracing_splice_read_pipe(struct file *filp,
3784 loff_t *ppos,
3785 struct pipe_inode_info *pipe,
3786 size_t len,
3787 unsigned int flags)
3788{
35f3d14d
JA
3789 struct page *pages_def[PIPE_DEF_BUFFERS];
3790 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3c56819b
EGM
3791 struct trace_iterator *iter = filp->private_data;
3792 struct splice_pipe_desc spd = {
35f3d14d
JA
3793 .pages = pages_def,
3794 .partial = partial_def,
34cd4998 3795 .nr_pages = 0, /* This gets updated below. */
047fe360 3796 .nr_pages_max = PIPE_DEF_BUFFERS,
34cd4998
SR
3797 .flags = flags,
3798 .ops = &tracing_pipe_buf_ops,
3799 .spd_release = tracing_spd_release_pipe,
3c56819b 3800 };
2b6080f2 3801 struct trace_array *tr = iter->tr;
3c56819b 3802 ssize_t ret;
34cd4998 3803 size_t rem;
3c56819b
EGM
3804 unsigned int i;
3805
35f3d14d
JA
3806 if (splice_grow_spd(pipe, &spd))
3807 return -ENOMEM;
3808
d7350c3f 3809 /* copy the tracer to avoid using a global lock all around */
3c56819b 3810 mutex_lock(&trace_types_lock);
2b6080f2
SR
3811 if (unlikely(iter->trace->name != tr->current_trace->name))
3812 *iter->trace = *tr->current_trace;
d7350c3f
FW
3813 mutex_unlock(&trace_types_lock);
3814
3815 mutex_lock(&iter->mutex);
3c56819b
EGM
3816
3817 if (iter->trace->splice_read) {
3818 ret = iter->trace->splice_read(iter, filp,
3819 ppos, pipe, len, flags);
3820 if (ret)
34cd4998 3821 goto out_err;
3c56819b
EGM
3822 }
3823
3824 ret = tracing_wait_pipe(filp);
3825 if (ret <= 0)
34cd4998 3826 goto out_err;
3c56819b 3827
955b61e5 3828 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3c56819b 3829 ret = -EFAULT;
34cd4998 3830 goto out_err;
3c56819b
EGM
3831 }
3832
4f535968 3833 trace_event_read_lock();
7e53bd42 3834 trace_access_lock(iter->cpu_file);
4f535968 3835
3c56819b 3836 /* Fill as many pages as possible. */
35f3d14d
JA
3837 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3838 spd.pages[i] = alloc_page(GFP_KERNEL);
3839 if (!spd.pages[i])
34cd4998 3840 break;
3c56819b 3841
fa7c7f6e 3842 rem = tracing_fill_pipe_page(rem, iter);
3c56819b
EGM
3843
3844 /* Copy the data into the page, so we can start over. */
3845 ret = trace_seq_to_buffer(&iter->seq,
35f3d14d 3846 page_address(spd.pages[i]),
3c56819b
EGM
3847 iter->seq.len);
3848 if (ret < 0) {
35f3d14d 3849 __free_page(spd.pages[i]);
3c56819b
EGM
3850 break;
3851 }
35f3d14d
JA
3852 spd.partial[i].offset = 0;
3853 spd.partial[i].len = iter->seq.len;
3c56819b 3854
f9520750 3855 trace_seq_init(&iter->seq);
3c56819b
EGM
3856 }
3857
7e53bd42 3858 trace_access_unlock(iter->cpu_file);
4f535968 3859 trace_event_read_unlock();
d7350c3f 3860 mutex_unlock(&iter->mutex);
3c56819b
EGM
3861
3862 spd.nr_pages = i;
3863
35f3d14d
JA
3864 ret = splice_to_pipe(pipe, &spd);
3865out:
047fe360 3866 splice_shrink_spd(&spd);
35f3d14d 3867 return ret;
3c56819b 3868
34cd4998 3869out_err:
d7350c3f 3870 mutex_unlock(&iter->mutex);
35f3d14d 3871 goto out;
3c56819b
EGM
3872}
3873
a98a3c3f
SR
3874static ssize_t
3875tracing_entries_read(struct file *filp, char __user *ubuf,
3876 size_t cnt, loff_t *ppos)
3877{
2b6080f2
SR
3878 struct trace_cpu *tc = filp->private_data;
3879 struct trace_array *tr = tc->tr;
438ced17
VN
3880 char buf[64];
3881 int r = 0;
3882 ssize_t ret;
a98a3c3f 3883
db526ca3 3884 mutex_lock(&trace_types_lock);
438ced17 3885
2b6080f2 3886 if (tc->cpu == RING_BUFFER_ALL_CPUS) {
438ced17
VN
3887 int cpu, buf_size_same;
3888 unsigned long size;
3889
3890 size = 0;
3891 buf_size_same = 1;
3892 /* check if all cpu sizes are same */
3893 for_each_tracing_cpu(cpu) {
3894 /* fill in the size from first enabled cpu */
3895 if (size == 0)
3896 size = tr->data[cpu]->entries;
3897 if (size != tr->data[cpu]->entries) {
3898 buf_size_same = 0;
3899 break;
3900 }
3901 }
3902
3903 if (buf_size_same) {
3904 if (!ring_buffer_expanded)
3905 r = sprintf(buf, "%lu (expanded: %lu)\n",
3906 size >> 10,
3907 trace_buf_size >> 10);
3908 else
3909 r = sprintf(buf, "%lu\n", size >> 10);
3910 } else
3911 r = sprintf(buf, "X\n");
3912 } else
2b6080f2 3913 r = sprintf(buf, "%lu\n", tr->data[tc->cpu]->entries >> 10);
438ced17 3914
db526ca3
SR
3915 mutex_unlock(&trace_types_lock);
3916
438ced17
VN
3917 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3918 return ret;
a98a3c3f
SR
3919}
3920
3921static ssize_t
3922tracing_entries_write(struct file *filp, const char __user *ubuf,
3923 size_t cnt, loff_t *ppos)
3924{
2b6080f2 3925 struct trace_cpu *tc = filp->private_data;
a98a3c3f 3926 unsigned long val;
4f271a2a 3927 int ret;
a98a3c3f 3928
22fe9b54
PH
3929 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3930 if (ret)
c6caeeb1 3931 return ret;
a98a3c3f
SR
3932
3933 /* must have at least 1 entry */
3934 if (!val)
3935 return -EINVAL;
3936
1696b2b0
SR
3937 /* value is in KB */
3938 val <<= 10;
3939
2b6080f2 3940 ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
4f271a2a
VN
3941 if (ret < 0)
3942 return ret;
a98a3c3f 3943
cf8517cf 3944 *ppos += cnt;
a98a3c3f 3945
4f271a2a
VN
3946 return cnt;
3947}
bf5e6519 3948
f81ab074
VN
3949static ssize_t
3950tracing_total_entries_read(struct file *filp, char __user *ubuf,
3951 size_t cnt, loff_t *ppos)
3952{
3953 struct trace_array *tr = filp->private_data;
3954 char buf[64];
3955 int r, cpu;
3956 unsigned long size = 0, expanded_size = 0;
3957
3958 mutex_lock(&trace_types_lock);
3959 for_each_tracing_cpu(cpu) {
438ced17 3960 size += tr->data[cpu]->entries >> 10;
f81ab074
VN
3961 if (!ring_buffer_expanded)
3962 expanded_size += trace_buf_size >> 10;
3963 }
3964 if (ring_buffer_expanded)
3965 r = sprintf(buf, "%lu\n", size);
3966 else
3967 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3968 mutex_unlock(&trace_types_lock);
3969
3970 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3971}
3972
4f271a2a
VN
3973static ssize_t
3974tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3975 size_t cnt, loff_t *ppos)
3976{
3977 /*
3978 * There is no need to read what the user has written, this function
3979 * is just to make sure that there is no error when "echo" is used
3980 */
3981
3982 *ppos += cnt;
a98a3c3f
SR
3983
3984 return cnt;
3985}
3986
4f271a2a
VN
3987static int
3988tracing_free_buffer_release(struct inode *inode, struct file *filp)
3989{
2b6080f2
SR
3990 struct trace_array *tr = inode->i_private;
3991
cf30cf67
SR
3992 /* disable tracing ? */
3993 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3994 tracing_off();
4f271a2a 3995 /* resize the ring buffer to 0 */
2b6080f2 3996 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4f271a2a
VN
3997
3998 return 0;
3999}
4000
5bf9a1ee
PP
4001static ssize_t
4002tracing_mark_write(struct file *filp, const char __user *ubuf,
4003 size_t cnt, loff_t *fpos)
4004{
d696b58c
SR
4005 unsigned long addr = (unsigned long)ubuf;
4006 struct ring_buffer_event *event;
4007 struct ring_buffer *buffer;
4008 struct print_entry *entry;
4009 unsigned long irq_flags;
4010 struct page *pages[2];
6edb2a8a 4011 void *map_page[2];
d696b58c
SR
4012 int nr_pages = 1;
4013 ssize_t written;
d696b58c
SR
4014 int offset;
4015 int size;
4016 int len;
4017 int ret;
6edb2a8a 4018 int i;
5bf9a1ee 4019
c76f0694 4020 if (tracing_disabled)
5bf9a1ee
PP
4021 return -EINVAL;
4022
5224c3a3
MSB
4023 if (!(trace_flags & TRACE_ITER_MARKERS))
4024 return -EINVAL;
4025
5bf9a1ee
PP
4026 if (cnt > TRACE_BUF_SIZE)
4027 cnt = TRACE_BUF_SIZE;
4028
d696b58c
SR
4029 /*
4030 * Userspace is injecting traces into the kernel trace buffer.
4031 * We want to be as non intrusive as possible.
4032 * To do so, we do not want to allocate any special buffers
4033 * or take any locks, but instead write the userspace data
4034 * straight into the ring buffer.
4035 *
4036 * First we need to pin the userspace buffer into memory,
4037 * which, most likely it is, because it just referenced it.
4038 * But there's no guarantee that it is. By using get_user_pages_fast()
4039 * and kmap_atomic/kunmap_atomic() we can get access to the
4040 * pages directly. We then write the data directly into the
4041 * ring buffer.
4042 */
4043 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
5bf9a1ee 4044
d696b58c
SR
4045 /* check if we cross pages */
4046 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4047 nr_pages = 2;
4048
4049 offset = addr & (PAGE_SIZE - 1);
4050 addr &= PAGE_MASK;
4051
4052 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4053 if (ret < nr_pages) {
4054 while (--ret >= 0)
4055 put_page(pages[ret]);
4056 written = -EFAULT;
4057 goto out;
5bf9a1ee 4058 }
d696b58c 4059
6edb2a8a
SR
4060 for (i = 0; i < nr_pages; i++)
4061 map_page[i] = kmap_atomic(pages[i]);
d696b58c
SR
4062
4063 local_save_flags(irq_flags);
4064 size = sizeof(*entry) + cnt + 2; /* possible \n added */
4065 buffer = global_trace.buffer;
4066 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4067 irq_flags, preempt_count());
4068 if (!event) {
4069 /* Ring buffer disabled, return as if not open for write */
4070 written = -EBADF;
4071 goto out_unlock;
5bf9a1ee 4072 }
d696b58c
SR
4073
4074 entry = ring_buffer_event_data(event);
4075 entry->ip = _THIS_IP_;
4076
4077 if (nr_pages == 2) {
4078 len = PAGE_SIZE - offset;
6edb2a8a
SR
4079 memcpy(&entry->buf, map_page[0] + offset, len);
4080 memcpy(&entry->buf[len], map_page[1], cnt - len);
c13d2f7c 4081 } else
6edb2a8a 4082 memcpy(&entry->buf, map_page[0] + offset, cnt);
5bf9a1ee 4083
d696b58c
SR
4084 if (entry->buf[cnt - 1] != '\n') {
4085 entry->buf[cnt] = '\n';
4086 entry->buf[cnt + 1] = '\0';
4087 } else
4088 entry->buf[cnt] = '\0';
4089
7ffbd48d 4090 __buffer_unlock_commit(buffer, event);
5bf9a1ee 4091
d696b58c 4092 written = cnt;
5bf9a1ee 4093
d696b58c 4094 *fpos += written;
1aa54bca 4095
d696b58c 4096 out_unlock:
6edb2a8a
SR
4097 for (i = 0; i < nr_pages; i++){
4098 kunmap_atomic(map_page[i]);
4099 put_page(pages[i]);
4100 }
d696b58c 4101 out:
1aa54bca 4102 return written;
5bf9a1ee
PP
4103}
4104
13f16d20 4105static int tracing_clock_show(struct seq_file *m, void *v)
5079f326 4106{
2b6080f2 4107 struct trace_array *tr = m->private;
5079f326
Z
4108 int i;
4109
4110 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
13f16d20 4111 seq_printf(m,
5079f326 4112 "%s%s%s%s", i ? " " : "",
2b6080f2
SR
4113 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4114 i == tr->clock_id ? "]" : "");
13f16d20 4115 seq_putc(m, '\n');
5079f326 4116
13f16d20 4117 return 0;
5079f326
Z
4118}
4119
4120static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4121 size_t cnt, loff_t *fpos)
4122{
2b6080f2
SR
4123 struct seq_file *m = filp->private_data;
4124 struct trace_array *tr = m->private;
5079f326
Z
4125 char buf[64];
4126 const char *clockstr;
4127 int i;
4128
4129 if (cnt >= sizeof(buf))
4130 return -EINVAL;
4131
4132 if (copy_from_user(&buf, ubuf, cnt))
4133 return -EFAULT;
4134
4135 buf[cnt] = 0;
4136
4137 clockstr = strstrip(buf);
4138
4139 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4140 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4141 break;
4142 }
4143 if (i == ARRAY_SIZE(trace_clocks))
4144 return -EINVAL;
4145
5079f326
Z
4146 mutex_lock(&trace_types_lock);
4147
2b6080f2
SR
4148 tr->clock_id = i;
4149
4150 ring_buffer_set_clock(tr->buffer, trace_clocks[i].func);
4151 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && max_tr.buffer)
5079f326
Z
4152 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
4153
60303ed3
DS
4154 /*
4155 * New clock may not be consistent with the previous clock.
4156 * Reset the buffer so that it doesn't have incomparable timestamps.
4157 */
4158 tracing_reset_online_cpus(&global_trace);
84c6cf0d 4159 tracing_reset_online_cpus(&max_tr);
60303ed3 4160
5079f326
Z
4161 mutex_unlock(&trace_types_lock);
4162
4163 *fpos += cnt;
4164
4165 return cnt;
4166}
4167
13f16d20
LZ
4168static int tracing_clock_open(struct inode *inode, struct file *file)
4169{
4170 if (tracing_disabled)
4171 return -ENODEV;
2b6080f2
SR
4172
4173 return single_open(file, tracing_clock_show, inode->i_private);
13f16d20
LZ
4174}
4175
debdd57f
HT
4176#ifdef CONFIG_TRACER_SNAPSHOT
4177static int tracing_snapshot_open(struct inode *inode, struct file *file)
4178{
2b6080f2 4179 struct trace_cpu *tc = inode->i_private;
debdd57f 4180 struct trace_iterator *iter;
2b6080f2 4181 struct seq_file *m;
debdd57f
HT
4182 int ret = 0;
4183
4184 if (file->f_mode & FMODE_READ) {
4185 iter = __tracing_open(inode, file, true);
4186 if (IS_ERR(iter))
4187 ret = PTR_ERR(iter);
2b6080f2
SR
4188 } else {
4189 /* Writes still need the seq_file to hold the private data */
4190 m = kzalloc(sizeof(*m), GFP_KERNEL);
4191 if (!m)
4192 return -ENOMEM;
4193 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4194 if (!iter) {
4195 kfree(m);
4196 return -ENOMEM;
4197 }
4198 iter->tr = tc->tr;
4199 m->private = iter;
4200 file->private_data = m;
debdd57f 4201 }
2b6080f2 4202
debdd57f
HT
4203 return ret;
4204}
4205
4206static ssize_t
4207tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4208 loff_t *ppos)
4209{
2b6080f2
SR
4210 struct seq_file *m = filp->private_data;
4211 struct trace_iterator *iter = m->private;
4212 struct trace_array *tr = iter->tr;
debdd57f
HT
4213 unsigned long val;
4214 int ret;
4215
4216 ret = tracing_update_buffers();
4217 if (ret < 0)
4218 return ret;
4219
4220 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4221 if (ret)
4222 return ret;
4223
4224 mutex_lock(&trace_types_lock);
4225
2b6080f2 4226 if (tr->current_trace->use_max_tr) {
debdd57f
HT
4227 ret = -EBUSY;
4228 goto out;
4229 }
4230
4231 switch (val) {
4232 case 0:
2b6080f2 4233 if (tr->current_trace->allocated_snapshot) {
debdd57f
HT
4234 /* free spare buffer */
4235 ring_buffer_resize(max_tr.buffer, 1,
4236 RING_BUFFER_ALL_CPUS);
4237 set_buffer_entries(&max_tr, 1);
4238 tracing_reset_online_cpus(&max_tr);
2b6080f2 4239 tr->current_trace->allocated_snapshot = false;
debdd57f
HT
4240 }
4241 break;
4242 case 1:
2b6080f2 4243 if (!tr->current_trace->allocated_snapshot) {
debdd57f
HT
4244 /* allocate spare buffer */
4245 ret = resize_buffer_duplicate_size(&max_tr,
4246 &global_trace, RING_BUFFER_ALL_CPUS);
4247 if (ret < 0)
4248 break;
2b6080f2 4249 tr->current_trace->allocated_snapshot = true;
debdd57f
HT
4250 }
4251
4252 local_irq_disable();
4253 /* Now, we're going to swap */
4254 update_max_tr(&global_trace, current, smp_processor_id());
4255 local_irq_enable();
4256 break;
4257 default:
2b6080f2 4258 if (tr->current_trace->allocated_snapshot)
debdd57f 4259 tracing_reset_online_cpus(&max_tr);
debdd57f
HT
4260 break;
4261 }
4262
4263 if (ret >= 0) {
4264 *ppos += cnt;
4265 ret = cnt;
4266 }
4267out:
4268 mutex_unlock(&trace_types_lock);
4269 return ret;
4270}
2b6080f2
SR
4271
4272static int tracing_snapshot_release(struct inode *inode, struct file *file)
4273{
4274 struct seq_file *m = file->private_data;
4275
4276 if (file->f_mode & FMODE_READ)
4277 return tracing_release(inode, file);
4278
4279 /* If write only, the seq_file is just a stub */
4280 if (m)
4281 kfree(m->private);
4282 kfree(m);
4283
4284 return 0;
4285}
4286
debdd57f
HT
4287#endif /* CONFIG_TRACER_SNAPSHOT */
4288
4289
5e2336a0 4290static const struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
4291 .open = tracing_open_generic,
4292 .read = tracing_max_lat_read,
4293 .write = tracing_max_lat_write,
b444786f 4294 .llseek = generic_file_llseek,
bc0c38d1
SR
4295};
4296
5e2336a0 4297static const struct file_operations set_tracer_fops = {
4bf39a94
IM
4298 .open = tracing_open_generic,
4299 .read = tracing_set_trace_read,
4300 .write = tracing_set_trace_write,
b444786f 4301 .llseek = generic_file_llseek,
bc0c38d1
SR
4302};
4303
5e2336a0 4304static const struct file_operations tracing_pipe_fops = {
4bf39a94 4305 .open = tracing_open_pipe,
2a2cc8f7 4306 .poll = tracing_poll_pipe,
4bf39a94 4307 .read = tracing_read_pipe,
3c56819b 4308 .splice_read = tracing_splice_read_pipe,
4bf39a94 4309 .release = tracing_release_pipe,
b444786f 4310 .llseek = no_llseek,
b3806b43
SR
4311};
4312
5e2336a0 4313static const struct file_operations tracing_entries_fops = {
2b6080f2 4314 .open = tracing_open_generic,
a98a3c3f
SR
4315 .read = tracing_entries_read,
4316 .write = tracing_entries_write,
b444786f 4317 .llseek = generic_file_llseek,
a98a3c3f
SR
4318};
4319
f81ab074
VN
4320static const struct file_operations tracing_total_entries_fops = {
4321 .open = tracing_open_generic,
4322 .read = tracing_total_entries_read,
4323 .llseek = generic_file_llseek,
4324};
4325
4f271a2a
VN
4326static const struct file_operations tracing_free_buffer_fops = {
4327 .write = tracing_free_buffer_write,
4328 .release = tracing_free_buffer_release,
4329};
4330
5e2336a0 4331static const struct file_operations tracing_mark_fops = {
43a15386 4332 .open = tracing_open_generic,
5bf9a1ee 4333 .write = tracing_mark_write,
b444786f 4334 .llseek = generic_file_llseek,
5bf9a1ee
PP
4335};
4336
5079f326 4337static const struct file_operations trace_clock_fops = {
13f16d20
LZ
4338 .open = tracing_clock_open,
4339 .read = seq_read,
4340 .llseek = seq_lseek,
4341 .release = single_release,
5079f326
Z
4342 .write = tracing_clock_write,
4343};
4344
debdd57f
HT
4345#ifdef CONFIG_TRACER_SNAPSHOT
4346static const struct file_operations snapshot_fops = {
4347 .open = tracing_snapshot_open,
4348 .read = seq_read,
4349 .write = tracing_snapshot_write,
4350 .llseek = tracing_seek,
2b6080f2 4351 .release = tracing_snapshot_release,
debdd57f
HT
4352};
4353#endif /* CONFIG_TRACER_SNAPSHOT */
4354
2cadf913
SR
4355struct ftrace_buffer_info {
4356 struct trace_array *tr;
4357 void *spare;
4358 int cpu;
4359 unsigned int read;
4360};
4361
4362static int tracing_buffers_open(struct inode *inode, struct file *filp)
4363{
2b6080f2
SR
4364 struct trace_cpu *tc = inode->i_private;
4365 struct trace_array *tr = tc->tr;
2cadf913
SR
4366 struct ftrace_buffer_info *info;
4367
4368 if (tracing_disabled)
4369 return -ENODEV;
4370
4371 info = kzalloc(sizeof(*info), GFP_KERNEL);
4372 if (!info)
4373 return -ENOMEM;
4374
2b6080f2
SR
4375 info->tr = tr;
4376 info->cpu = tc->cpu;
ddd538f3 4377 info->spare = NULL;
2cadf913
SR
4378 /* Force reading ring buffer for first read */
4379 info->read = (unsigned int)-1;
2cadf913
SR
4380
4381 filp->private_data = info;
4382
d1e7e02f 4383 return nonseekable_open(inode, filp);
2cadf913
SR
4384}
4385
4386static ssize_t
4387tracing_buffers_read(struct file *filp, char __user *ubuf,
4388 size_t count, loff_t *ppos)
4389{
4390 struct ftrace_buffer_info *info = filp->private_data;
2cadf913
SR
4391 ssize_t ret;
4392 size_t size;
4393
2dc5d12b
SR
4394 if (!count)
4395 return 0;
4396
ddd538f3 4397 if (!info->spare)
7ea59064 4398 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
ddd538f3
LJ
4399 if (!info->spare)
4400 return -ENOMEM;
4401
2cadf913
SR
4402 /* Do we have previous read data to read? */
4403 if (info->read < PAGE_SIZE)
4404 goto read;
4405
7e53bd42 4406 trace_access_lock(info->cpu);
2cadf913
SR
4407 ret = ring_buffer_read_page(info->tr->buffer,
4408 &info->spare,
4409 count,
4410 info->cpu, 0);
7e53bd42 4411 trace_access_unlock(info->cpu);
2cadf913
SR
4412 if (ret < 0)
4413 return 0;
4414
436fc280
SR
4415 info->read = 0;
4416
2cadf913
SR
4417read:
4418 size = PAGE_SIZE - info->read;
4419 if (size > count)
4420 size = count;
4421
4422 ret = copy_to_user(ubuf, info->spare + info->read, size);
2dc5d12b 4423 if (ret == size)
2cadf913 4424 return -EFAULT;
2dc5d12b
SR
4425 size -= ret;
4426
2cadf913
SR
4427 *ppos += size;
4428 info->read += size;
4429
4430 return size;
4431}
4432
4433static int tracing_buffers_release(struct inode *inode, struct file *file)
4434{
4435 struct ftrace_buffer_info *info = file->private_data;
4436
ddd538f3
LJ
4437 if (info->spare)
4438 ring_buffer_free_read_page(info->tr->buffer, info->spare);
2cadf913
SR
4439 kfree(info);
4440
4441 return 0;
4442}
4443
4444struct buffer_ref {
4445 struct ring_buffer *buffer;
4446 void *page;
4447 int ref;
4448};
4449
4450static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4451 struct pipe_buffer *buf)
4452{
4453 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4454
4455 if (--ref->ref)
4456 return;
4457
4458 ring_buffer_free_read_page(ref->buffer, ref->page);
4459 kfree(ref);
4460 buf->private = 0;
4461}
4462
2cadf913
SR
4463static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4464 struct pipe_buffer *buf)
4465{
4466 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4467
4468 ref->ref++;
4469}
4470
4471/* Pipe buffer operations for a buffer. */
28dfef8f 4472static const struct pipe_buf_operations buffer_pipe_buf_ops = {
2cadf913
SR
4473 .can_merge = 0,
4474 .map = generic_pipe_buf_map,
4475 .unmap = generic_pipe_buf_unmap,
4476 .confirm = generic_pipe_buf_confirm,
4477 .release = buffer_pipe_buf_release,
d55cb6cf 4478 .steal = generic_pipe_buf_steal,
2cadf913
SR
4479 .get = buffer_pipe_buf_get,
4480};
4481
4482/*
4483 * Callback from splice_to_pipe(), if we need to release some pages
4484 * at the end of the spd in case we error'ed out in filling the pipe.
4485 */
4486static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4487{
4488 struct buffer_ref *ref =
4489 (struct buffer_ref *)spd->partial[i].private;
4490
4491 if (--ref->ref)
4492 return;
4493
4494 ring_buffer_free_read_page(ref->buffer, ref->page);
4495 kfree(ref);
4496 spd->partial[i].private = 0;
4497}
4498
4499static ssize_t
4500tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4501 struct pipe_inode_info *pipe, size_t len,
4502 unsigned int flags)
4503{
4504 struct ftrace_buffer_info *info = file->private_data;
35f3d14d
JA
4505 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4506 struct page *pages_def[PIPE_DEF_BUFFERS];
2cadf913 4507 struct splice_pipe_desc spd = {
35f3d14d
JA
4508 .pages = pages_def,
4509 .partial = partial_def,
047fe360 4510 .nr_pages_max = PIPE_DEF_BUFFERS,
2cadf913
SR
4511 .flags = flags,
4512 .ops = &buffer_pipe_buf_ops,
4513 .spd_release = buffer_spd_release,
4514 };
4515 struct buffer_ref *ref;
93459c6c 4516 int entries, size, i;
2cadf913
SR
4517 size_t ret;
4518
35f3d14d
JA
4519 if (splice_grow_spd(pipe, &spd))
4520 return -ENOMEM;
4521
93cfb3c9 4522 if (*ppos & (PAGE_SIZE - 1)) {
35f3d14d
JA
4523 ret = -EINVAL;
4524 goto out;
93cfb3c9
LJ
4525 }
4526
4527 if (len & (PAGE_SIZE - 1)) {
35f3d14d
JA
4528 if (len < PAGE_SIZE) {
4529 ret = -EINVAL;
4530 goto out;
4531 }
93cfb3c9
LJ
4532 len &= PAGE_MASK;
4533 }
4534
7e53bd42 4535 trace_access_lock(info->cpu);
93459c6c
SR
4536 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4537
35f3d14d 4538 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
2cadf913
SR
4539 struct page *page;
4540 int r;
4541
4542 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4543 if (!ref)
4544 break;
4545
7267fa68 4546 ref->ref = 1;
2cadf913 4547 ref->buffer = info->tr->buffer;
7ea59064 4548 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
2cadf913
SR
4549 if (!ref->page) {
4550 kfree(ref);
4551 break;
4552 }
4553
4554 r = ring_buffer_read_page(ref->buffer, &ref->page,
f2957f1f 4555 len, info->cpu, 1);
2cadf913 4556 if (r < 0) {
7ea59064 4557 ring_buffer_free_read_page(ref->buffer, ref->page);
2cadf913
SR
4558 kfree(ref);
4559 break;
4560 }
4561
4562 /*
4563 * zero out any left over data, this is going to
4564 * user land.
4565 */
4566 size = ring_buffer_page_len(ref->page);
4567 if (size < PAGE_SIZE)
4568 memset(ref->page + size, 0, PAGE_SIZE - size);
4569
4570 page = virt_to_page(ref->page);
4571
4572 spd.pages[i] = page;
4573 spd.partial[i].len = PAGE_SIZE;
4574 spd.partial[i].offset = 0;
4575 spd.partial[i].private = (unsigned long)ref;
4576 spd.nr_pages++;
93cfb3c9 4577 *ppos += PAGE_SIZE;
93459c6c
SR
4578
4579 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
2cadf913
SR
4580 }
4581
7e53bd42 4582 trace_access_unlock(info->cpu);
2cadf913
SR
4583 spd.nr_pages = i;
4584
4585 /* did we read anything? */
4586 if (!spd.nr_pages) {
4587 if (flags & SPLICE_F_NONBLOCK)
4588 ret = -EAGAIN;
4589 else
4590 ret = 0;
4591 /* TODO: block */
35f3d14d 4592 goto out;
2cadf913
SR
4593 }
4594
4595 ret = splice_to_pipe(pipe, &spd);
047fe360 4596 splice_shrink_spd(&spd);
35f3d14d 4597out:
2cadf913
SR
4598 return ret;
4599}
4600
4601static const struct file_operations tracing_buffers_fops = {
4602 .open = tracing_buffers_open,
4603 .read = tracing_buffers_read,
4604 .release = tracing_buffers_release,
4605 .splice_read = tracing_buffers_splice_read,
4606 .llseek = no_llseek,
4607};
4608
c8d77183
SR
4609static ssize_t
4610tracing_stats_read(struct file *filp, char __user *ubuf,
4611 size_t count, loff_t *ppos)
4612{
2b6080f2
SR
4613 struct trace_cpu *tc = filp->private_data;
4614 struct trace_array *tr = tc->tr;
c8d77183
SR
4615 struct trace_seq *s;
4616 unsigned long cnt;
c64e148a
VN
4617 unsigned long long t;
4618 unsigned long usec_rem;
2b6080f2 4619 int cpu = tc->cpu;
c8d77183 4620
e4f2d10f 4621 s = kmalloc(sizeof(*s), GFP_KERNEL);
c8d77183 4622 if (!s)
a646365c 4623 return -ENOMEM;
c8d77183
SR
4624
4625 trace_seq_init(s);
4626
4627 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4628 trace_seq_printf(s, "entries: %ld\n", cnt);
4629
4630 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4631 trace_seq_printf(s, "overrun: %ld\n", cnt);
4632
4633 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4634 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4635
c64e148a
VN
4636 cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4637 trace_seq_printf(s, "bytes: %ld\n", cnt);
4638
11043d8b
YY
4639 if (trace_clocks[trace_clock_id].in_ns) {
4640 /* local or global for trace_clock */
4641 t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4642 usec_rem = do_div(t, USEC_PER_SEC);
4643 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
4644 t, usec_rem);
4645
4646 t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4647 usec_rem = do_div(t, USEC_PER_SEC);
4648 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4649 } else {
4650 /* counter or tsc mode for trace_clock */
4651 trace_seq_printf(s, "oldest event ts: %llu\n",
4652 ring_buffer_oldest_event_ts(tr->buffer, cpu));
c64e148a 4653
11043d8b
YY
4654 trace_seq_printf(s, "now ts: %llu\n",
4655 ring_buffer_time_stamp(tr->buffer, cpu));
4656 }
c64e148a 4657
884bfe89
SP
4658 cnt = ring_buffer_dropped_events_cpu(tr->buffer, cpu);
4659 trace_seq_printf(s, "dropped events: %ld\n", cnt);
4660
ad964704
SRRH
4661 cnt = ring_buffer_read_events_cpu(tr->buffer, cpu);
4662 trace_seq_printf(s, "read events: %ld\n", cnt);
4663
c8d77183
SR
4664 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4665
4666 kfree(s);
4667
4668 return count;
4669}
4670
4671static const struct file_operations tracing_stats_fops = {
4672 .open = tracing_open_generic,
4673 .read = tracing_stats_read,
b444786f 4674 .llseek = generic_file_llseek,
c8d77183
SR
4675};
4676
bc0c38d1
SR
4677#ifdef CONFIG_DYNAMIC_FTRACE
4678
b807c3d0
SR
4679int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4680{
4681 return 0;
4682}
4683
bc0c38d1 4684static ssize_t
b807c3d0 4685tracing_read_dyn_info(struct file *filp, char __user *ubuf,
bc0c38d1
SR
4686 size_t cnt, loff_t *ppos)
4687{
a26a2a27
SR
4688 static char ftrace_dyn_info_buffer[1024];
4689 static DEFINE_MUTEX(dyn_info_mutex);
bc0c38d1 4690 unsigned long *p = filp->private_data;
b807c3d0 4691 char *buf = ftrace_dyn_info_buffer;
a26a2a27 4692 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
bc0c38d1
SR
4693 int r;
4694
b807c3d0
SR
4695 mutex_lock(&dyn_info_mutex);
4696 r = sprintf(buf, "%ld ", *p);
4bf39a94 4697
a26a2a27 4698 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
b807c3d0
SR
4699 buf[r++] = '\n';
4700
4701 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4702
4703 mutex_unlock(&dyn_info_mutex);
4704
4705 return r;
bc0c38d1
SR
4706}
4707
5e2336a0 4708static const struct file_operations tracing_dyn_info_fops = {
4bf39a94 4709 .open = tracing_open_generic,
b807c3d0 4710 .read = tracing_read_dyn_info,
b444786f 4711 .llseek = generic_file_llseek,
bc0c38d1
SR
4712};
4713#endif
4714
2b6080f2 4715struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
bc0c38d1
SR
4716{
4717 static int once;
4718
2b6080f2
SR
4719 if (tr->dir)
4720 return tr->dir;
bc0c38d1 4721
3e1f60b8
FW
4722 if (!debugfs_initialized())
4723 return NULL;
4724
2b6080f2
SR
4725 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
4726 tr->dir = debugfs_create_dir("tracing", NULL);
bc0c38d1 4727
2b6080f2 4728 if (!tr->dir && !once) {
bc0c38d1
SR
4729 once = 1;
4730 pr_warning("Could not create debugfs directory 'tracing'\n");
4731 return NULL;
4732 }
4733
2b6080f2 4734 return tr->dir;
bc0c38d1
SR
4735}
4736
2b6080f2
SR
4737struct dentry *tracing_init_dentry(void)
4738{
4739 return tracing_init_dentry_tr(&global_trace);
4740}
b04cc6b1 4741
2b6080f2 4742static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
b04cc6b1 4743{
b04cc6b1
FW
4744 struct dentry *d_tracer;
4745
2b6080f2
SR
4746 if (tr->percpu_dir)
4747 return tr->percpu_dir;
b04cc6b1 4748
2b6080f2 4749 d_tracer = tracing_init_dentry_tr(tr);
b04cc6b1
FW
4750 if (!d_tracer)
4751 return NULL;
4752
2b6080f2 4753 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
b04cc6b1 4754
2b6080f2
SR
4755 WARN_ONCE(!tr->percpu_dir,
4756 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
b04cc6b1 4757
2b6080f2 4758 return tr->percpu_dir;
b04cc6b1
FW
4759}
4760
2b6080f2
SR
4761static void
4762tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
b04cc6b1 4763{
2b6080f2
SR
4764 struct trace_array_cpu *data = tr->data[cpu];
4765 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5452af66 4766 struct dentry *d_cpu;
dd49a38c 4767 char cpu_dir[30]; /* 30 characters should be more than enough */
b04cc6b1 4768
0a3d7ce7
NK
4769 if (!d_percpu)
4770 return;
4771
dd49a38c 4772 snprintf(cpu_dir, 30, "cpu%ld", cpu);
8656e7a2
FW
4773 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4774 if (!d_cpu) {
4775 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4776 return;
4777 }
b04cc6b1 4778
8656e7a2 4779 /* per cpu trace_pipe */
5452af66 4780 trace_create_file("trace_pipe", 0444, d_cpu,
2b6080f2 4781 (void *)&data->trace_cpu, &tracing_pipe_fops);
b04cc6b1
FW
4782
4783 /* per cpu trace */
5452af66 4784 trace_create_file("trace", 0644, d_cpu,
2b6080f2 4785 (void *)&data->trace_cpu, &tracing_fops);
7f96f93f 4786
5452af66 4787 trace_create_file("trace_pipe_raw", 0444, d_cpu,
2b6080f2 4788 (void *)&data->trace_cpu, &tracing_buffers_fops);
7f96f93f 4789
c8d77183 4790 trace_create_file("stats", 0444, d_cpu,
2b6080f2 4791 (void *)&data->trace_cpu, &tracing_stats_fops);
438ced17
VN
4792
4793 trace_create_file("buffer_size_kb", 0444, d_cpu,
2b6080f2 4794 (void *)&data->trace_cpu, &tracing_entries_fops);
b04cc6b1
FW
4795}
4796
60a11774
SR
4797#ifdef CONFIG_FTRACE_SELFTEST
4798/* Let selftest have access to static functions in this file */
4799#include "trace_selftest.c"
4800#endif
4801
577b785f
SR
4802struct trace_option_dentry {
4803 struct tracer_opt *opt;
4804 struct tracer_flags *flags;
2b6080f2 4805 struct trace_array *tr;
577b785f
SR
4806 struct dentry *entry;
4807};
4808
4809static ssize_t
4810trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4811 loff_t *ppos)
4812{
4813 struct trace_option_dentry *topt = filp->private_data;
4814 char *buf;
4815
4816 if (topt->flags->val & topt->opt->bit)
4817 buf = "1\n";
4818 else
4819 buf = "0\n";
4820
4821 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4822}
4823
4824static ssize_t
4825trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4826 loff_t *ppos)
4827{
4828 struct trace_option_dentry *topt = filp->private_data;
4829 unsigned long val;
577b785f
SR
4830 int ret;
4831
22fe9b54
PH
4832 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4833 if (ret)
577b785f
SR
4834 return ret;
4835
8d18eaaf
LZ
4836 if (val != 0 && val != 1)
4837 return -EINVAL;
577b785f 4838
8d18eaaf 4839 if (!!(topt->flags->val & topt->opt->bit) != val) {
577b785f 4840 mutex_lock(&trace_types_lock);
2b6080f2 4841 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
c757bea9 4842 topt->opt, !val);
577b785f
SR
4843 mutex_unlock(&trace_types_lock);
4844 if (ret)
4845 return ret;
577b785f
SR
4846 }
4847
4848 *ppos += cnt;
4849
4850 return cnt;
4851}
4852
4853
4854static const struct file_operations trace_options_fops = {
4855 .open = tracing_open_generic,
4856 .read = trace_options_read,
4857 .write = trace_options_write,
b444786f 4858 .llseek = generic_file_llseek,
577b785f
SR
4859};
4860
a8259075
SR
4861static ssize_t
4862trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4863 loff_t *ppos)
4864{
4865 long index = (long)filp->private_data;
4866 char *buf;
4867
4868 if (trace_flags & (1 << index))
4869 buf = "1\n";
4870 else
4871 buf = "0\n";
4872
4873 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4874}
4875
4876static ssize_t
4877trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4878 loff_t *ppos)
4879{
2b6080f2 4880 struct trace_array *tr = &global_trace;
a8259075 4881 long index = (long)filp->private_data;
a8259075
SR
4882 unsigned long val;
4883 int ret;
4884
22fe9b54
PH
4885 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4886 if (ret)
a8259075
SR
4887 return ret;
4888
f2d84b65 4889 if (val != 0 && val != 1)
a8259075 4890 return -EINVAL;
69d34da2
SRRH
4891
4892 mutex_lock(&trace_types_lock);
2b6080f2 4893 ret = set_tracer_flag(tr, 1 << index, val);
69d34da2 4894 mutex_unlock(&trace_types_lock);
a8259075 4895
613f04a0
SRRH
4896 if (ret < 0)
4897 return ret;
4898
a8259075
SR
4899 *ppos += cnt;
4900
4901 return cnt;
4902}
4903
a8259075
SR
4904static const struct file_operations trace_options_core_fops = {
4905 .open = tracing_open_generic,
4906 .read = trace_options_core_read,
4907 .write = trace_options_core_write,
b444786f 4908 .llseek = generic_file_llseek,
a8259075
SR
4909};
4910
5452af66 4911struct dentry *trace_create_file(const char *name,
f4ae40a6 4912 umode_t mode,
5452af66
FW
4913 struct dentry *parent,
4914 void *data,
4915 const struct file_operations *fops)
4916{
4917 struct dentry *ret;
4918
4919 ret = debugfs_create_file(name, mode, parent, data, fops);
4920 if (!ret)
4921 pr_warning("Could not create debugfs '%s' entry\n", name);
4922
4923 return ret;
4924}
4925
4926
2b6080f2 4927static struct dentry *trace_options_init_dentry(struct trace_array *tr)
a8259075
SR
4928{
4929 struct dentry *d_tracer;
a8259075 4930
2b6080f2
SR
4931 if (tr->options)
4932 return tr->options;
a8259075 4933
2b6080f2 4934 d_tracer = tracing_init_dentry_tr(tr);
a8259075
SR
4935 if (!d_tracer)
4936 return NULL;
4937
2b6080f2
SR
4938 tr->options = debugfs_create_dir("options", d_tracer);
4939 if (!tr->options) {
a8259075
SR
4940 pr_warning("Could not create debugfs directory 'options'\n");
4941 return NULL;
4942 }
4943
2b6080f2 4944 return tr->options;
a8259075
SR
4945}
4946
577b785f 4947static void
2b6080f2
SR
4948create_trace_option_file(struct trace_array *tr,
4949 struct trace_option_dentry *topt,
577b785f
SR
4950 struct tracer_flags *flags,
4951 struct tracer_opt *opt)
4952{
4953 struct dentry *t_options;
577b785f 4954
2b6080f2 4955 t_options = trace_options_init_dentry(tr);
577b785f
SR
4956 if (!t_options)
4957 return;
4958
4959 topt->flags = flags;
4960 topt->opt = opt;
2b6080f2 4961 topt->tr = tr;
577b785f 4962
5452af66 4963 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
577b785f
SR
4964 &trace_options_fops);
4965
577b785f
SR
4966}
4967
4968static struct trace_option_dentry *
2b6080f2 4969create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
577b785f
SR
4970{
4971 struct trace_option_dentry *topts;
4972 struct tracer_flags *flags;
4973 struct tracer_opt *opts;
4974 int cnt;
4975
4976 if (!tracer)
4977 return NULL;
4978
4979 flags = tracer->flags;
4980
4981 if (!flags || !flags->opts)
4982 return NULL;
4983
4984 opts = flags->opts;
4985
4986 for (cnt = 0; opts[cnt].name; cnt++)
4987 ;
4988
0cfe8245 4989 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
577b785f
SR
4990 if (!topts)
4991 return NULL;
4992
4993 for (cnt = 0; opts[cnt].name; cnt++)
2b6080f2 4994 create_trace_option_file(tr, &topts[cnt], flags,
577b785f
SR
4995 &opts[cnt]);
4996
4997 return topts;
4998}
4999
5000static void
5001destroy_trace_option_files(struct trace_option_dentry *topts)
5002{
5003 int cnt;
5004
5005 if (!topts)
5006 return;
5007
5008 for (cnt = 0; topts[cnt].opt; cnt++) {
5009 if (topts[cnt].entry)
5010 debugfs_remove(topts[cnt].entry);
5011 }
5012
5013 kfree(topts);
5014}
5015
a8259075 5016static struct dentry *
2b6080f2
SR
5017create_trace_option_core_file(struct trace_array *tr,
5018 const char *option, long index)
a8259075
SR
5019{
5020 struct dentry *t_options;
a8259075 5021
2b6080f2 5022 t_options = trace_options_init_dentry(tr);
a8259075
SR
5023 if (!t_options)
5024 return NULL;
5025
5452af66 5026 return trace_create_file(option, 0644, t_options, (void *)index,
a8259075 5027 &trace_options_core_fops);
a8259075
SR
5028}
5029
2b6080f2 5030static __init void create_trace_options_dir(struct trace_array *tr)
a8259075
SR
5031{
5032 struct dentry *t_options;
a8259075
SR
5033 int i;
5034
2b6080f2 5035 t_options = trace_options_init_dentry(tr);
a8259075
SR
5036 if (!t_options)
5037 return;
5038
5452af66 5039 for (i = 0; trace_options[i]; i++)
2b6080f2 5040 create_trace_option_core_file(tr, trace_options[i], i);
a8259075
SR
5041}
5042
499e5470
SR
5043static ssize_t
5044rb_simple_read(struct file *filp, char __user *ubuf,
5045 size_t cnt, loff_t *ppos)
5046{
348f0fc2
SR
5047 struct trace_array *tr = filp->private_data;
5048 struct ring_buffer *buffer = tr->buffer;
499e5470
SR
5049 char buf[64];
5050 int r;
5051
5052 if (buffer)
5053 r = ring_buffer_record_is_on(buffer);
5054 else
5055 r = 0;
5056
5057 r = sprintf(buf, "%d\n", r);
5058
5059 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5060}
5061
5062static ssize_t
5063rb_simple_write(struct file *filp, const char __user *ubuf,
5064 size_t cnt, loff_t *ppos)
5065{
348f0fc2
SR
5066 struct trace_array *tr = filp->private_data;
5067 struct ring_buffer *buffer = tr->buffer;
499e5470
SR
5068 unsigned long val;
5069 int ret;
5070
5071 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5072 if (ret)
5073 return ret;
5074
5075 if (buffer) {
2df8f8a6
SR
5076 mutex_lock(&trace_types_lock);
5077 if (val) {
499e5470 5078 ring_buffer_record_on(buffer);
2b6080f2
SR
5079 if (tr->current_trace->start)
5080 tr->current_trace->start(tr);
2df8f8a6 5081 } else {
499e5470 5082 ring_buffer_record_off(buffer);
2b6080f2
SR
5083 if (tr->current_trace->stop)
5084 tr->current_trace->stop(tr);
2df8f8a6
SR
5085 }
5086 mutex_unlock(&trace_types_lock);
499e5470
SR
5087 }
5088
5089 (*ppos)++;
5090
5091 return cnt;
5092}
5093
5094static const struct file_operations rb_simple_fops = {
5095 .open = tracing_open_generic,
5096 .read = rb_simple_read,
5097 .write = rb_simple_write,
5098 .llseek = default_llseek,
5099};
5100
2b6080f2
SR
5101static void
5102init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
5103{
5104
5105 trace_create_file("trace_options", 0644, d_tracer,
5106 tr, &tracing_iter_fops);
5107
5108 trace_create_file("trace", 0644, d_tracer,
5109 (void *)&tr->trace_cpu, &tracing_fops);
5110
5111 trace_create_file("trace_pipe", 0444, d_tracer,
5112 (void *)&tr->trace_cpu, &tracing_pipe_fops);
5113
5114 trace_create_file("buffer_size_kb", 0644, d_tracer,
5115 (void *)&tr->trace_cpu, &tracing_entries_fops);
5116
5117 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
5118 tr, &tracing_total_entries_fops);
5119
5120 trace_create_file("free_buffer", 0644, d_tracer,
5121 tr, &tracing_free_buffer_fops);
5122
5123 trace_create_file("trace_marker", 0220, d_tracer,
5124 tr, &tracing_mark_fops);
5125
5126 trace_create_file("trace_clock", 0644, d_tracer, tr,
5127 &trace_clock_fops);
5128
5129 trace_create_file("tracing_on", 0644, d_tracer,
5130 tr, &rb_simple_fops);
5131}
5132
b5ad384e 5133static __init int tracer_init_debugfs(void)
bc0c38d1
SR
5134{
5135 struct dentry *d_tracer;
b04cc6b1 5136 int cpu;
bc0c38d1 5137
7e53bd42
LJ
5138 trace_access_lock_init();
5139
bc0c38d1
SR
5140 d_tracer = tracing_init_dentry();
5141
2b6080f2 5142 init_tracer_debugfs(&global_trace, d_tracer);
bc0c38d1 5143
5452af66 5144 trace_create_file("tracing_cpumask", 0644, d_tracer,
2b6080f2 5145 &global_trace, &tracing_cpumask_fops);
a8259075 5146
5452af66
FW
5147 trace_create_file("available_tracers", 0444, d_tracer,
5148 &global_trace, &show_traces_fops);
5149
339ae5d3 5150 trace_create_file("current_tracer", 0644, d_tracer,
5452af66
FW
5151 &global_trace, &set_tracer_fops);
5152
5d4a9dba 5153#ifdef CONFIG_TRACER_MAX_TRACE
5452af66
FW
5154 trace_create_file("tracing_max_latency", 0644, d_tracer,
5155 &tracing_max_latency, &tracing_max_lat_fops);
0e950173 5156#endif
5452af66
FW
5157
5158 trace_create_file("tracing_thresh", 0644, d_tracer,
5159 &tracing_thresh, &tracing_max_lat_fops);
a8259075 5160
339ae5d3 5161 trace_create_file("README", 0444, d_tracer,
5452af66
FW
5162 NULL, &tracing_readme_fops);
5163
69abe6a5
AP
5164 trace_create_file("saved_cmdlines", 0444, d_tracer,
5165 NULL, &tracing_saved_cmdlines_fops);
5bf9a1ee 5166
bc0c38d1 5167#ifdef CONFIG_DYNAMIC_FTRACE
5452af66
FW
5168 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
5169 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
bc0c38d1 5170#endif
b04cc6b1 5171
debdd57f
HT
5172#ifdef CONFIG_TRACER_SNAPSHOT
5173 trace_create_file("snapshot", 0644, d_tracer,
2b6080f2 5174 (void *)&global_trace.trace_cpu, &snapshot_fops);
debdd57f
HT
5175#endif
5176
2b6080f2 5177 create_trace_options_dir(&global_trace);
5452af66 5178
b04cc6b1 5179 for_each_tracing_cpu(cpu)
2b6080f2 5180 tracing_init_debugfs_percpu(&global_trace, cpu);
b04cc6b1 5181
b5ad384e 5182 return 0;
bc0c38d1
SR
5183}
5184
3f5a54e3
SR
5185static int trace_panic_handler(struct notifier_block *this,
5186 unsigned long event, void *unused)
5187{
944ac425 5188 if (ftrace_dump_on_oops)
cecbca96 5189 ftrace_dump(ftrace_dump_on_oops);
3f5a54e3
SR
5190 return NOTIFY_OK;
5191}
5192
5193static struct notifier_block trace_panic_notifier = {
5194 .notifier_call = trace_panic_handler,
5195 .next = NULL,
5196 .priority = 150 /* priority: INT_MAX >= x >= 0 */
5197};
5198
5199static int trace_die_handler(struct notifier_block *self,
5200 unsigned long val,
5201 void *data)
5202{
5203 switch (val) {
5204 case DIE_OOPS:
944ac425 5205 if (ftrace_dump_on_oops)
cecbca96 5206 ftrace_dump(ftrace_dump_on_oops);
3f5a54e3
SR
5207 break;
5208 default:
5209 break;
5210 }
5211 return NOTIFY_OK;
5212}
5213
5214static struct notifier_block trace_die_notifier = {
5215 .notifier_call = trace_die_handler,
5216 .priority = 200
5217};
5218
5219/*
5220 * printk is set to max of 1024, we really don't need it that big.
5221 * Nothing should be printing 1000 characters anyway.
5222 */
5223#define TRACE_MAX_PRINT 1000
5224
5225/*
5226 * Define here KERN_TRACE so that we have one place to modify
5227 * it if we decide to change what log level the ftrace dump
5228 * should be at.
5229 */
428aee14 5230#define KERN_TRACE KERN_EMERG
3f5a54e3 5231
955b61e5 5232void
3f5a54e3
SR
5233trace_printk_seq(struct trace_seq *s)
5234{
5235 /* Probably should print a warning here. */
5236 if (s->len >= 1000)
5237 s->len = 1000;
5238
5239 /* should be zero ended, but we are paranoid. */
5240 s->buffer[s->len] = 0;
5241
5242 printk(KERN_TRACE "%s", s->buffer);
5243
f9520750 5244 trace_seq_init(s);
3f5a54e3
SR
5245}
5246
955b61e5
JW
5247void trace_init_global_iter(struct trace_iterator *iter)
5248{
5249 iter->tr = &global_trace;
2b6080f2 5250 iter->trace = iter->tr->current_trace;
ae3b5093 5251 iter->cpu_file = RING_BUFFER_ALL_CPUS;
955b61e5
JW
5252}
5253
cecbca96
FW
5254static void
5255__ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
3f5a54e3 5256{
445c8951 5257 static arch_spinlock_t ftrace_dump_lock =
edc35bd7 5258 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
3f5a54e3
SR
5259 /* use static because iter can be a bit big for the stack */
5260 static struct trace_iterator iter;
cf586b61 5261 unsigned int old_userobj;
3f5a54e3 5262 static int dump_ran;
d769041f
SR
5263 unsigned long flags;
5264 int cnt = 0, cpu;
3f5a54e3
SR
5265
5266 /* only one dump */
cd891ae0 5267 local_irq_save(flags);
0199c4e6 5268 arch_spin_lock(&ftrace_dump_lock);
3f5a54e3
SR
5269 if (dump_ran)
5270 goto out;
5271
5272 dump_ran = 1;
5273
0ee6b6cf 5274 tracing_off();
cf586b61 5275
e0a413f6
SR
5276 /* Did function tracer already get disabled? */
5277 if (ftrace_is_dead()) {
5278 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
5279 printk("# MAY BE MISSING FUNCTION EVENTS\n");
5280 }
5281
cf586b61
FW
5282 if (disable_tracing)
5283 ftrace_kill();
3f5a54e3 5284
38dbe0b1 5285 /* Simulate the iterator */
955b61e5
JW
5286 trace_init_global_iter(&iter);
5287
d769041f 5288 for_each_tracing_cpu(cpu) {
955b61e5 5289 atomic_inc(&iter.tr->data[cpu]->disabled);
d769041f
SR
5290 }
5291
cf586b61
FW
5292 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
5293
b54d3de9
TE
5294 /* don't look at user memory in panic mode */
5295 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
5296
cecbca96
FW
5297 switch (oops_dump_mode) {
5298 case DUMP_ALL:
ae3b5093 5299 iter.cpu_file = RING_BUFFER_ALL_CPUS;
cecbca96
FW
5300 break;
5301 case DUMP_ORIG:
5302 iter.cpu_file = raw_smp_processor_id();
5303 break;
5304 case DUMP_NONE:
5305 goto out_enable;
5306 default:
5307 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
ae3b5093 5308 iter.cpu_file = RING_BUFFER_ALL_CPUS;
cecbca96
FW
5309 }
5310
5311 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3f5a54e3
SR
5312
5313 /*
5314 * We need to stop all tracing on all CPUS to read the
5315 * the next buffer. This is a bit expensive, but is
5316 * not done often. We fill all what we can read,
5317 * and then release the locks again.
5318 */
5319
3f5a54e3
SR
5320 while (!trace_empty(&iter)) {
5321
5322 if (!cnt)
5323 printk(KERN_TRACE "---------------------------------\n");
5324
5325 cnt++;
5326
5327 /* reset all but tr, trace, and overruns */
5328 memset(&iter.seq, 0,
5329 sizeof(struct trace_iterator) -
5330 offsetof(struct trace_iterator, seq));
5331 iter.iter_flags |= TRACE_FILE_LAT_FMT;
5332 iter.pos = -1;
5333
955b61e5 5334 if (trace_find_next_entry_inc(&iter) != NULL) {
74e7ff8c
LJ
5335 int ret;
5336
5337 ret = print_trace_line(&iter);
5338 if (ret != TRACE_TYPE_NO_CONSUME)
5339 trace_consume(&iter);
3f5a54e3 5340 }
b892e5c8 5341 touch_nmi_watchdog();
3f5a54e3
SR
5342
5343 trace_printk_seq(&iter.seq);
5344 }
5345
5346 if (!cnt)
5347 printk(KERN_TRACE " (ftrace buffer empty)\n");
5348 else
5349 printk(KERN_TRACE "---------------------------------\n");
5350
cecbca96 5351 out_enable:
cf586b61
FW
5352 /* Re-enable tracing if requested */
5353 if (!disable_tracing) {
5354 trace_flags |= old_userobj;
5355
5356 for_each_tracing_cpu(cpu) {
955b61e5 5357 atomic_dec(&iter.tr->data[cpu]->disabled);
cf586b61
FW
5358 }
5359 tracing_on();
5360 }
5361
3f5a54e3 5362 out:
0199c4e6 5363 arch_spin_unlock(&ftrace_dump_lock);
cd891ae0 5364 local_irq_restore(flags);
3f5a54e3
SR
5365}
5366
cf586b61 5367/* By default: disable tracing after the dump */
cecbca96 5368void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
cf586b61 5369{
cecbca96 5370 __ftrace_dump(true, oops_dump_mode);
cf586b61 5371}
a8eecf22 5372EXPORT_SYMBOL_GPL(ftrace_dump);
cf586b61 5373
3928a8a2 5374__init static int tracer_alloc_buffers(void)
bc0c38d1 5375{
73c5162a 5376 int ring_buf_size;
750912fa 5377 enum ring_buffer_flags rb_flags;
4c11d7ae 5378 int i;
9e01c1b7 5379 int ret = -ENOMEM;
4c11d7ae 5380
750912fa 5381
9e01c1b7
RR
5382 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
5383 goto out;
5384
5385 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
5386 goto out_free_buffer_mask;
4c11d7ae 5387
07d777fe
SR
5388 /* Only allocate trace_printk buffers if a trace_printk exists */
5389 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
81698831 5390 /* Must be called before global_trace.buffer is allocated */
07d777fe
SR
5391 trace_printk_init_buffers();
5392
73c5162a
SR
5393 /* To save memory, keep the ring buffer size to its minimum */
5394 if (ring_buffer_expanded)
5395 ring_buf_size = trace_buf_size;
5396 else
5397 ring_buf_size = 1;
5398
750912fa
DS
5399 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5400
9e01c1b7
RR
5401 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
5402 cpumask_copy(tracing_cpumask, cpu_all_mask);
5403
2b6080f2
SR
5404 raw_spin_lock_init(&global_trace.start_lock);
5405
9e01c1b7 5406 /* TODO: make the number of buffers hot pluggable with CPUS */
750912fa 5407 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
3928a8a2
SR
5408 if (!global_trace.buffer) {
5409 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
5410 WARN_ON(1);
9e01c1b7 5411 goto out_free_cpumask;
4c11d7ae 5412 }
499e5470
SR
5413 if (global_trace.buffer_disabled)
5414 tracing_off();
4c11d7ae 5415
9e01c1b7 5416
4c11d7ae 5417#ifdef CONFIG_TRACER_MAX_TRACE
750912fa 5418 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
2b6080f2 5419 raw_spin_lock_init(&max_tr.start_lock);
3928a8a2
SR
5420 if (!max_tr.buffer) {
5421 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
5422 WARN_ON(1);
5423 ring_buffer_free(global_trace.buffer);
9e01c1b7 5424 goto out_free_cpumask;
4c11d7ae 5425 }
a98a3c3f 5426#endif
ab46428c 5427
4c11d7ae 5428 /* Allocate the first page for all buffers */
ab46428c 5429 for_each_tracing_cpu(i) {
566b0aaf 5430 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2b6080f2
SR
5431 global_trace.data[i]->trace_cpu.cpu = i;
5432 global_trace.data[i]->trace_cpu.tr = &global_trace;
9705f69e 5433 max_tr.data[i] = &per_cpu(max_tr_data, i);
2b6080f2
SR
5434 max_tr.data[i]->trace_cpu.cpu = i;
5435 max_tr.data[i]->trace_cpu.tr = &max_tr;
4c11d7ae 5436 }
438ced17 5437
a591c73f
VN
5438 set_buffer_entries(&global_trace,
5439 ring_buffer_size(global_trace.buffer, 0));
438ced17
VN
5440#ifdef CONFIG_TRACER_MAX_TRACE
5441 set_buffer_entries(&max_tr, 1);
5442#endif
bc0c38d1 5443
bc0c38d1 5444 trace_init_cmdlines();
0d5c6e1c 5445 init_irq_work(&trace_work_wakeup, trace_wake_up);
bc0c38d1 5446
43a15386 5447 register_tracer(&nop_trace);
d840f718 5448
2b6080f2
SR
5449 global_trace.current_trace = &nop_trace;
5450
60a11774
SR
5451 /* All seems OK, enable tracing */
5452 tracing_disabled = 0;
3928a8a2 5453
3f5a54e3
SR
5454 atomic_notifier_chain_register(&panic_notifier_list,
5455 &trace_panic_notifier);
5456
5457 register_die_notifier(&trace_die_notifier);
2fc1dfbe 5458
ae63b31e
SR
5459 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
5460
2b6080f2
SR
5461 /* Holder for file callbacks */
5462 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5463 global_trace.trace_cpu.tr = &global_trace;
5464
ae63b31e
SR
5465 INIT_LIST_HEAD(&global_trace.systems);
5466 INIT_LIST_HEAD(&global_trace.events);
5467 list_add(&global_trace.list, &ftrace_trace_arrays);
5468
7bcfaf54
SR
5469 while (trace_boot_options) {
5470 char *option;
5471
5472 option = strsep(&trace_boot_options, ",");
2b6080f2 5473 trace_set_options(&global_trace, option);
7bcfaf54
SR
5474 }
5475
2fc1dfbe 5476 return 0;
3f5a54e3 5477
9e01c1b7
RR
5478out_free_cpumask:
5479 free_cpumask_var(tracing_cpumask);
5480out_free_buffer_mask:
5481 free_cpumask_var(tracing_buffer_mask);
5482out:
5483 return ret;
bc0c38d1 5484}
b2821ae6
SR
5485
5486__init static int clear_boot_tracer(void)
5487{
5488 /*
5489 * The default tracer at boot buffer is an init section.
5490 * This function is called in lateinit. If we did not
5491 * find the boot tracer, then clear it out, to prevent
5492 * later registration from accessing the buffer that is
5493 * about to be freed.
5494 */
5495 if (!default_bootup_tracer)
5496 return 0;
5497
5498 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
5499 default_bootup_tracer);
5500 default_bootup_tracer = NULL;
5501
5502 return 0;
5503}
5504
b5ad384e
FW
5505early_initcall(tracer_alloc_buffers);
5506fs_initcall(tracer_init_debugfs);
b2821ae6 5507late_initcall(clear_boot_tracer);
This page took 0.878682 seconds and 5 git commands to generate.