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