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