]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/trace/trace.c
tracing: Fix graph tracer with stack tracer on other archs
[mirror_ubuntu-jammy-kernel.git] / kernel / trace / trace.c
CommitLineData
bc0c38d1
SR
1/*
2 * ring buffer based function tracer
3 *
2b6080f2 4 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
bc0c38d1
SR
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
6d49e352 12 * Copyright (C) 2004 Nadia Yvette Chambers
bc0c38d1 13 */
2cadf913 14#include <linux/ring_buffer.h>
273b281f 15#include <generated/utsrelease.h>
2cadf913
SR
16#include <linux/stacktrace.h>
17#include <linux/writeback.h>
bc0c38d1
SR
18#include <linux/kallsyms.h>
19#include <linux/seq_file.h>
3f5a54e3 20#include <linux/notifier.h>
2cadf913 21#include <linux/irqflags.h>
bc0c38d1 22#include <linux/debugfs.h>
4c11d7ae 23#include <linux/pagemap.h>
bc0c38d1
SR
24#include <linux/hardirq.h>
25#include <linux/linkage.h>
26#include <linux/uaccess.h>
2cadf913 27#include <linux/kprobes.h>
bc0c38d1
SR
28#include <linux/ftrace.h>
29#include <linux/module.h>
30#include <linux/percpu.h>
2cadf913 31#include <linux/splice.h>
3f5a54e3 32#include <linux/kdebug.h>
5f0c6c03 33#include <linux/string.h>
7e53bd42 34#include <linux/rwsem.h>
5a0e3ad6 35#include <linux/slab.h>
bc0c38d1
SR
36#include <linux/ctype.h>
37#include <linux/init.h>
2a2cc8f7 38#include <linux/poll.h>
b892e5c8 39#include <linux/nmi.h>
bc0c38d1 40#include <linux/fs.h>
8bd75c77 41#include <linux/sched/rt.h>
86387f7e 42
bc0c38d1 43#include "trace.h"
f0868d1e 44#include "trace_output.h"
bc0c38d1 45
73c5162a
SR
46/*
47 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
49 */
55034cd6 50bool ring_buffer_expanded;
73c5162a 51
8e1b82e0
FW
52/*
53 * We need to change this state when a selftest is running.
ff32504f
FW
54 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
5e1607a0 56 * insertions into the ring-buffer such as trace_printk could occurred
ff32504f
FW
57 * at the same time, giving false positive or negative results.
58 */
8e1b82e0 59static bool __read_mostly tracing_selftest_running;
ff32504f 60
b2821ae6
SR
61/*
62 * If a tracer is running, we do not want to run SELFTEST.
63 */
020e5f85 64bool __read_mostly tracing_selftest_disabled;
b2821ae6 65
adf9f195
FW
66/* For tracers that don't implement custom flags */
67static struct tracer_opt dummy_tracer_opt[] = {
68 { }
69};
70
71static struct tracer_flags dummy_tracer_flags = {
72 .val = 0,
73 .opts = dummy_tracer_opt
74};
75
8c1a49ae
SRRH
76static int
77dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
adf9f195
FW
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
0f048701
SR
89/*
90 * Kill all tracing for good (never come back).
91 * It is initialized to 1 but will turn to zero if the initialization
92 * of the tracer is successful. But that is the only place that sets
93 * this back to zero.
94 */
4fd27358 95static int tracing_disabled = 1;
0f048701 96
9288f99a 97DEFINE_PER_CPU(int, ftrace_cpu_disabled);
d769041f 98
955b61e5 99cpumask_var_t __read_mostly tracing_buffer_mask;
ab46428c 100
944ac425
SR
101/*
102 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
103 *
104 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
105 * is set, then ftrace_dump is called. This will output the contents
106 * of the ftrace buffers to the console. This is very useful for
107 * capturing traces that lead to crashes and outputing it to a
108 * serial console.
109 *
110 * It is default off, but you can enable it with either specifying
111 * "ftrace_dump_on_oops" in the kernel command line, or setting
cecbca96
FW
112 * /proc/sys/kernel/ftrace_dump_on_oops
113 * Set 1 if you want to dump buffers of all CPUs
114 * Set 2 if you want to dump the buffer of the CPU that triggered oops
944ac425 115 */
cecbca96
FW
116
117enum ftrace_dump_mode ftrace_dump_on_oops;
944ac425 118
de7edd31
SRRH
119/* When set, tracing will stop when a WARN*() is hit */
120int __disable_trace_on_warning;
121
607e2ea1 122static int tracing_set_tracer(struct trace_array *tr, const char *buf);
b2821ae6 123
ee6c2c1b
LZ
124#define MAX_TRACER_SIZE 100
125static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
b2821ae6 126static char *default_bootup_tracer;
d9e54076 127
55034cd6
SRRH
128static bool allocate_snapshot;
129
1beee96b 130static int __init set_cmdline_ftrace(char *str)
d9e54076 131{
67012ab1 132 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
b2821ae6 133 default_bootup_tracer = bootup_tracer_buf;
73c5162a 134 /* We are using ftrace early, expand it */
55034cd6 135 ring_buffer_expanded = true;
d9e54076
PZ
136 return 1;
137}
1beee96b 138__setup("ftrace=", set_cmdline_ftrace);
d9e54076 139
944ac425
SR
140static int __init set_ftrace_dump_on_oops(char *str)
141{
cecbca96
FW
142 if (*str++ != '=' || !*str) {
143 ftrace_dump_on_oops = DUMP_ALL;
144 return 1;
145 }
146
147 if (!strcmp("orig_cpu", str)) {
148 ftrace_dump_on_oops = DUMP_ORIG;
149 return 1;
150 }
151
152 return 0;
944ac425
SR
153}
154__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
60a11774 155
de7edd31
SRRH
156static int __init stop_trace_on_warning(char *str)
157{
158 __disable_trace_on_warning = 1;
159 return 1;
160}
161__setup("traceoff_on_warning=", stop_trace_on_warning);
162
3209cff4 163static int __init boot_alloc_snapshot(char *str)
55034cd6
SRRH
164{
165 allocate_snapshot = true;
166 /* We also need the main ring buffer expanded */
167 ring_buffer_expanded = true;
168 return 1;
169}
3209cff4 170__setup("alloc_snapshot", boot_alloc_snapshot);
55034cd6 171
7bcfaf54
SR
172
173static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
174static char *trace_boot_options __initdata;
175
176static int __init set_trace_boot_options(char *str)
177{
67012ab1 178 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
7bcfaf54
SR
179 trace_boot_options = trace_boot_options_buf;
180 return 0;
181}
182__setup("trace_options=", set_trace_boot_options);
183
e1e232ca
SR
184static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
185static char *trace_boot_clock __initdata;
186
187static int __init set_trace_boot_clock(char *str)
188{
189 strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
190 trace_boot_clock = trace_boot_clock_buf;
191 return 0;
192}
193__setup("trace_clock=", set_trace_boot_clock);
194
de7edd31 195
cf8e3474 196unsigned long long ns2usecs(cycle_t nsec)
bc0c38d1
SR
197{
198 nsec += 500;
199 do_div(nsec, 1000);
200 return nsec;
201}
202
4fcdae83
SR
203/*
204 * The global_trace is the descriptor that holds the tracing
205 * buffers for the live tracing. For each CPU, it contains
206 * a link list of pages that will store trace entries. The
207 * page descriptor of the pages in the memory is used to hold
208 * the link list by linking the lru item in the page descriptor
209 * to each of the pages in the buffer per CPU.
210 *
211 * For each active CPU there is a data field that holds the
212 * pages for the buffer for that CPU. Each CPU has the same number
213 * of pages allocated for its buffer.
214 */
bc0c38d1
SR
215static struct trace_array global_trace;
216
ae63b31e 217LIST_HEAD(ftrace_trace_arrays);
bc0c38d1 218
ff451961
SRRH
219int trace_array_get(struct trace_array *this_tr)
220{
221 struct trace_array *tr;
222 int ret = -ENODEV;
223
224 mutex_lock(&trace_types_lock);
225 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
226 if (tr == this_tr) {
227 tr->ref++;
228 ret = 0;
229 break;
230 }
231 }
232 mutex_unlock(&trace_types_lock);
233
234 return ret;
235}
236
237static void __trace_array_put(struct trace_array *this_tr)
238{
239 WARN_ON(!this_tr->ref);
240 this_tr->ref--;
241}
242
243void trace_array_put(struct trace_array *this_tr)
244{
245 mutex_lock(&trace_types_lock);
246 __trace_array_put(this_tr);
247 mutex_unlock(&trace_types_lock);
248}
249
f306cc82
TZ
250int filter_check_discard(struct ftrace_event_file *file, void *rec,
251 struct ring_buffer *buffer,
252 struct ring_buffer_event *event)
eb02ce01 253{
f306cc82
TZ
254 if (unlikely(file->flags & FTRACE_EVENT_FL_FILTERED) &&
255 !filter_match_preds(file->filter, rec)) {
256 ring_buffer_discard_commit(buffer, event);
257 return 1;
258 }
259
260 return 0;
261}
262EXPORT_SYMBOL_GPL(filter_check_discard);
263
264int call_filter_check_discard(struct ftrace_event_call *call, void *rec,
265 struct ring_buffer *buffer,
266 struct ring_buffer_event *event)
267{
268 if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
269 !filter_match_preds(call->filter, rec)) {
270 ring_buffer_discard_commit(buffer, event);
271 return 1;
272 }
273
274 return 0;
eb02ce01 275}
f306cc82 276EXPORT_SYMBOL_GPL(call_filter_check_discard);
eb02ce01 277
ad1438a0 278static cycle_t buffer_ftrace_now(struct trace_buffer *buf, int cpu)
37886f6a
SR
279{
280 u64 ts;
281
282 /* Early boot up does not have a buffer yet */
9457158b 283 if (!buf->buffer)
37886f6a
SR
284 return trace_clock_local();
285
9457158b
AL
286 ts = ring_buffer_time_stamp(buf->buffer, cpu);
287 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
37886f6a
SR
288
289 return ts;
290}
bc0c38d1 291
9457158b
AL
292cycle_t ftrace_now(int cpu)
293{
294 return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
295}
296
10246fa3
SRRH
297/**
298 * tracing_is_enabled - Show if global_trace has been disabled
299 *
300 * Shows if the global trace has been enabled or not. It uses the
301 * mirror flag "buffer_disabled" to be used in fast paths such as for
302 * the irqsoff tracer. But it may be inaccurate due to races. If you
303 * need to know the accurate state, use tracing_is_on() which is a little
304 * slower, but accurate.
305 */
9036990d
SR
306int tracing_is_enabled(void)
307{
10246fa3
SRRH
308 /*
309 * For quick access (irqsoff uses this in fast path), just
310 * return the mirror variable of the state of the ring buffer.
311 * It's a little racy, but we don't really care.
312 */
313 smp_rmb();
314 return !global_trace.buffer_disabled;
9036990d
SR
315}
316
4fcdae83 317/*
3928a8a2
SR
318 * trace_buf_size is the size in bytes that is allocated
319 * for a buffer. Note, the number of bytes is always rounded
320 * to page size.
3f5a54e3
SR
321 *
322 * This number is purposely set to a low number of 16384.
323 * If the dump on oops happens, it will be much appreciated
324 * to not have to wait for all that output. Anyway this can be
325 * boot time and run time configurable.
4fcdae83 326 */
3928a8a2 327#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
3f5a54e3 328
3928a8a2 329static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
bc0c38d1 330
4fcdae83 331/* trace_types holds a link list of available tracers. */
bc0c38d1 332static struct tracer *trace_types __read_mostly;
4fcdae83 333
4fcdae83
SR
334/*
335 * trace_types_lock is used to protect the trace_types list.
4fcdae83 336 */
a8227415 337DEFINE_MUTEX(trace_types_lock);
4fcdae83 338
7e53bd42
LJ
339/*
340 * serialize the access of the ring buffer
341 *
342 * ring buffer serializes readers, but it is low level protection.
343 * The validity of the events (which returns by ring_buffer_peek() ..etc)
344 * are not protected by ring buffer.
345 *
346 * The content of events may become garbage if we allow other process consumes
347 * these events concurrently:
348 * A) the page of the consumed events may become a normal page
349 * (not reader page) in ring buffer, and this page will be rewrited
350 * by events producer.
351 * B) The page of the consumed events may become a page for splice_read,
352 * and this page will be returned to system.
353 *
354 * These primitives allow multi process access to different cpu ring buffer
355 * concurrently.
356 *
357 * These primitives don't distinguish read-only and read-consume access.
358 * Multi read-only access are also serialized.
359 */
360
361#ifdef CONFIG_SMP
362static DECLARE_RWSEM(all_cpu_access_lock);
363static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
364
365static inline void trace_access_lock(int cpu)
366{
ae3b5093 367 if (cpu == RING_BUFFER_ALL_CPUS) {
7e53bd42
LJ
368 /* gain it for accessing the whole ring buffer. */
369 down_write(&all_cpu_access_lock);
370 } else {
371 /* gain it for accessing a cpu ring buffer. */
372
ae3b5093 373 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
7e53bd42
LJ
374 down_read(&all_cpu_access_lock);
375
376 /* Secondly block other access to this @cpu ring buffer. */
377 mutex_lock(&per_cpu(cpu_access_lock, cpu));
378 }
379}
380
381static inline void trace_access_unlock(int cpu)
382{
ae3b5093 383 if (cpu == RING_BUFFER_ALL_CPUS) {
7e53bd42
LJ
384 up_write(&all_cpu_access_lock);
385 } else {
386 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
387 up_read(&all_cpu_access_lock);
388 }
389}
390
391static inline void trace_access_lock_init(void)
392{
393 int cpu;
394
395 for_each_possible_cpu(cpu)
396 mutex_init(&per_cpu(cpu_access_lock, cpu));
397}
398
399#else
400
401static DEFINE_MUTEX(access_lock);
402
403static inline void trace_access_lock(int cpu)
404{
405 (void)cpu;
406 mutex_lock(&access_lock);
407}
408
409static inline void trace_access_unlock(int cpu)
410{
411 (void)cpu;
412 mutex_unlock(&access_lock);
413}
414
415static inline void trace_access_lock_init(void)
416{
417}
418
419#endif
420
ee6bce52 421/* trace_flags holds trace_options default values */
12ef7d44 422unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
a2a16d6a 423 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
77271ce4 424 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
328df475 425 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
e7e2ee89 426
5280bcef 427static void tracer_tracing_on(struct trace_array *tr)
10246fa3
SRRH
428{
429 if (tr->trace_buffer.buffer)
430 ring_buffer_record_on(tr->trace_buffer.buffer);
431 /*
432 * This flag is looked at when buffers haven't been allocated
433 * yet, or by some tracers (like irqsoff), that just want to
434 * know if the ring buffer has been disabled, but it can handle
435 * races of where it gets disabled but we still do a record.
436 * As the check is in the fast path of the tracers, it is more
437 * important to be fast than accurate.
438 */
439 tr->buffer_disabled = 0;
440 /* Make the flag seen by readers */
441 smp_wmb();
442}
443
499e5470
SR
444/**
445 * tracing_on - enable tracing buffers
446 *
447 * This function enables tracing buffers that may have been
448 * disabled with tracing_off.
449 */
450void tracing_on(void)
451{
10246fa3 452 tracer_tracing_on(&global_trace);
499e5470
SR
453}
454EXPORT_SYMBOL_GPL(tracing_on);
455
09ae7234
SRRH
456/**
457 * __trace_puts - write a constant string into the trace buffer.
458 * @ip: The address of the caller
459 * @str: The constant string to write
460 * @size: The size of the string.
461 */
462int __trace_puts(unsigned long ip, const char *str, int size)
463{
464 struct ring_buffer_event *event;
465 struct ring_buffer *buffer;
466 struct print_entry *entry;
467 unsigned long irq_flags;
468 int alloc;
8abfb872
J
469 int pc;
470
471 pc = preempt_count();
09ae7234 472
3132e107
SRRH
473 if (unlikely(tracing_selftest_running || tracing_disabled))
474 return 0;
475
09ae7234
SRRH
476 alloc = sizeof(*entry) + size + 2; /* possible \n added */
477
478 local_save_flags(irq_flags);
479 buffer = global_trace.trace_buffer.buffer;
480 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
8abfb872 481 irq_flags, pc);
09ae7234
SRRH
482 if (!event)
483 return 0;
484
485 entry = ring_buffer_event_data(event);
486 entry->ip = ip;
487
488 memcpy(&entry->buf, str, size);
489
490 /* Add a newline if necessary */
491 if (entry->buf[size - 1] != '\n') {
492 entry->buf[size] = '\n';
493 entry->buf[size + 1] = '\0';
494 } else
495 entry->buf[size] = '\0';
496
497 __buffer_unlock_commit(buffer, event);
8abfb872 498 ftrace_trace_stack(buffer, irq_flags, 4, pc);
09ae7234
SRRH
499
500 return size;
501}
502EXPORT_SYMBOL_GPL(__trace_puts);
503
504/**
505 * __trace_bputs - write the pointer to a constant string into trace buffer
506 * @ip: The address of the caller
507 * @str: The constant string to write to the buffer to
508 */
509int __trace_bputs(unsigned long ip, const char *str)
510{
511 struct ring_buffer_event *event;
512 struct ring_buffer *buffer;
513 struct bputs_entry *entry;
514 unsigned long irq_flags;
515 int size = sizeof(struct bputs_entry);
8abfb872
J
516 int pc;
517
518 pc = preempt_count();
09ae7234 519
3132e107
SRRH
520 if (unlikely(tracing_selftest_running || tracing_disabled))
521 return 0;
522
09ae7234
SRRH
523 local_save_flags(irq_flags);
524 buffer = global_trace.trace_buffer.buffer;
525 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
8abfb872 526 irq_flags, pc);
09ae7234
SRRH
527 if (!event)
528 return 0;
529
530 entry = ring_buffer_event_data(event);
531 entry->ip = ip;
532 entry->str = str;
533
534 __buffer_unlock_commit(buffer, event);
8abfb872 535 ftrace_trace_stack(buffer, irq_flags, 4, pc);
09ae7234
SRRH
536
537 return 1;
538}
539EXPORT_SYMBOL_GPL(__trace_bputs);
540
ad909e21
SRRH
541#ifdef CONFIG_TRACER_SNAPSHOT
542/**
543 * trace_snapshot - take a snapshot of the current buffer.
544 *
545 * This causes a swap between the snapshot buffer and the current live
546 * tracing buffer. You can use this to take snapshots of the live
547 * trace when some condition is triggered, but continue to trace.
548 *
549 * Note, make sure to allocate the snapshot with either
550 * a tracing_snapshot_alloc(), or by doing it manually
551 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
552 *
553 * If the snapshot buffer is not allocated, it will stop tracing.
554 * Basically making a permanent snapshot.
555 */
556void tracing_snapshot(void)
557{
558 struct trace_array *tr = &global_trace;
559 struct tracer *tracer = tr->current_trace;
560 unsigned long flags;
561
1b22e382
SRRH
562 if (in_nmi()) {
563 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
564 internal_trace_puts("*** snapshot is being ignored ***\n");
565 return;
566 }
567
ad909e21 568 if (!tr->allocated_snapshot) {
ca268da6
SRRH
569 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
570 internal_trace_puts("*** stopping trace here! ***\n");
ad909e21
SRRH
571 tracing_off();
572 return;
573 }
574
575 /* Note, snapshot can not be used when the tracer uses it */
576 if (tracer->use_max_tr) {
ca268da6
SRRH
577 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
578 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
ad909e21
SRRH
579 return;
580 }
581
582 local_irq_save(flags);
583 update_max_tr(tr, current, smp_processor_id());
584 local_irq_restore(flags);
585}
1b22e382 586EXPORT_SYMBOL_GPL(tracing_snapshot);
ad909e21
SRRH
587
588static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
589 struct trace_buffer *size_buf, int cpu_id);
3209cff4
SRRH
590static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
591
592static int alloc_snapshot(struct trace_array *tr)
593{
594 int ret;
595
596 if (!tr->allocated_snapshot) {
597
598 /* allocate spare buffer */
599 ret = resize_buffer_duplicate_size(&tr->max_buffer,
600 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
601 if (ret < 0)
602 return ret;
603
604 tr->allocated_snapshot = true;
605 }
606
607 return 0;
608}
609
ad1438a0 610static void free_snapshot(struct trace_array *tr)
3209cff4
SRRH
611{
612 /*
613 * We don't free the ring buffer. instead, resize it because
614 * The max_tr ring buffer has some state (e.g. ring->clock) and
615 * we want preserve it.
616 */
617 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
618 set_buffer_entries(&tr->max_buffer, 1);
619 tracing_reset_online_cpus(&tr->max_buffer);
620 tr->allocated_snapshot = false;
621}
ad909e21 622
93e31ffb
TZ
623/**
624 * tracing_alloc_snapshot - allocate snapshot buffer.
625 *
626 * This only allocates the snapshot buffer if it isn't already
627 * allocated - it doesn't also take a snapshot.
628 *
629 * This is meant to be used in cases where the snapshot buffer needs
630 * to be set up for events that can't sleep but need to be able to
631 * trigger a snapshot.
632 */
633int tracing_alloc_snapshot(void)
634{
635 struct trace_array *tr = &global_trace;
636 int ret;
637
638 ret = alloc_snapshot(tr);
639 WARN_ON(ret < 0);
640
641 return ret;
642}
643EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
644
ad909e21
SRRH
645/**
646 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
647 *
648 * This is similar to trace_snapshot(), but it will allocate the
649 * snapshot buffer if it isn't already allocated. Use this only
650 * where it is safe to sleep, as the allocation may sleep.
651 *
652 * This causes a swap between the snapshot buffer and the current live
653 * tracing buffer. You can use this to take snapshots of the live
654 * trace when some condition is triggered, but continue to trace.
655 */
656void tracing_snapshot_alloc(void)
657{
ad909e21
SRRH
658 int ret;
659
93e31ffb
TZ
660 ret = tracing_alloc_snapshot();
661 if (ret < 0)
3209cff4 662 return;
ad909e21
SRRH
663
664 tracing_snapshot();
665}
1b22e382 666EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
ad909e21
SRRH
667#else
668void tracing_snapshot(void)
669{
670 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
671}
1b22e382 672EXPORT_SYMBOL_GPL(tracing_snapshot);
93e31ffb
TZ
673int tracing_alloc_snapshot(void)
674{
675 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
676 return -ENODEV;
677}
678EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
ad909e21
SRRH
679void tracing_snapshot_alloc(void)
680{
681 /* Give warning */
682 tracing_snapshot();
683}
1b22e382 684EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
ad909e21
SRRH
685#endif /* CONFIG_TRACER_SNAPSHOT */
686
5280bcef 687static void tracer_tracing_off(struct trace_array *tr)
10246fa3
SRRH
688{
689 if (tr->trace_buffer.buffer)
690 ring_buffer_record_off(tr->trace_buffer.buffer);
691 /*
692 * This flag is looked at when buffers haven't been allocated
693 * yet, or by some tracers (like irqsoff), that just want to
694 * know if the ring buffer has been disabled, but it can handle
695 * races of where it gets disabled but we still do a record.
696 * As the check is in the fast path of the tracers, it is more
697 * important to be fast than accurate.
698 */
699 tr->buffer_disabled = 1;
700 /* Make the flag seen by readers */
701 smp_wmb();
702}
703
499e5470
SR
704/**
705 * tracing_off - turn off tracing buffers
706 *
707 * This function stops the tracing buffers from recording data.
708 * It does not disable any overhead the tracers themselves may
709 * be causing. This function simply causes all recording to
710 * the ring buffers to fail.
711 */
712void tracing_off(void)
713{
10246fa3 714 tracer_tracing_off(&global_trace);
499e5470
SR
715}
716EXPORT_SYMBOL_GPL(tracing_off);
717
de7edd31
SRRH
718void disable_trace_on_warning(void)
719{
720 if (__disable_trace_on_warning)
721 tracing_off();
722}
723
10246fa3
SRRH
724/**
725 * tracer_tracing_is_on - show real state of ring buffer enabled
726 * @tr : the trace array to know if ring buffer is enabled
727 *
728 * Shows real state of the ring buffer if it is enabled or not.
729 */
5280bcef 730static int tracer_tracing_is_on(struct trace_array *tr)
10246fa3
SRRH
731{
732 if (tr->trace_buffer.buffer)
733 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
734 return !tr->buffer_disabled;
735}
736
499e5470
SR
737/**
738 * tracing_is_on - show state of ring buffers enabled
739 */
740int tracing_is_on(void)
741{
10246fa3 742 return tracer_tracing_is_on(&global_trace);
499e5470
SR
743}
744EXPORT_SYMBOL_GPL(tracing_is_on);
745
3928a8a2 746static int __init set_buf_size(char *str)
bc0c38d1 747{
3928a8a2 748 unsigned long buf_size;
c6caeeb1 749
bc0c38d1
SR
750 if (!str)
751 return 0;
9d612bef 752 buf_size = memparse(str, &str);
c6caeeb1 753 /* nr_entries can not be zero */
9d612bef 754 if (buf_size == 0)
c6caeeb1 755 return 0;
3928a8a2 756 trace_buf_size = buf_size;
bc0c38d1
SR
757 return 1;
758}
3928a8a2 759__setup("trace_buf_size=", set_buf_size);
bc0c38d1 760
0e950173
TB
761static int __init set_tracing_thresh(char *str)
762{
87abb3b1 763 unsigned long threshold;
0e950173
TB
764 int ret;
765
766 if (!str)
767 return 0;
bcd83ea6 768 ret = kstrtoul(str, 0, &threshold);
0e950173
TB
769 if (ret < 0)
770 return 0;
87abb3b1 771 tracing_thresh = threshold * 1000;
0e950173
TB
772 return 1;
773}
774__setup("tracing_thresh=", set_tracing_thresh);
775
57f50be1
SR
776unsigned long nsecs_to_usecs(unsigned long nsecs)
777{
778 return nsecs / 1000;
779}
780
4fcdae83 781/* These must match the bit postions in trace_iterator_flags */
bc0c38d1
SR
782static const char *trace_options[] = {
783 "print-parent",
784 "sym-offset",
785 "sym-addr",
786 "verbose",
f9896bf3 787 "raw",
5e3ca0ec 788 "hex",
cb0f12aa 789 "bin",
2a2cc8f7 790 "block",
86387f7e 791 "stacktrace",
5e1607a0 792 "trace_printk",
b2a866f9 793 "ftrace_preempt",
9f029e83 794 "branch",
12ef7d44 795 "annotate",
02b67518 796 "userstacktrace",
b54d3de9 797 "sym-userobj",
66896a85 798 "printk-msg-only",
c4a8e8be 799 "context-info",
c032ef64 800 "latency-format",
be6f164a 801 "sleep-time",
a2a16d6a 802 "graph-time",
e870e9a1 803 "record-cmd",
750912fa 804 "overwrite",
cf30cf67 805 "disable_on_free",
77271ce4 806 "irq-info",
5224c3a3 807 "markers",
328df475 808 "function-trace",
bc0c38d1
SR
809 NULL
810};
811
5079f326
Z
812static struct {
813 u64 (*func)(void);
814 const char *name;
8be0709f 815 int in_ns; /* is this clock in nanoseconds? */
5079f326 816} trace_clocks[] = {
8be0709f
DS
817 { trace_clock_local, "local", 1 },
818 { trace_clock_global, "global", 1 },
819 { trace_clock_counter, "counter", 0 },
8aacf017 820 { trace_clock_jiffies, "uptime", 1 },
76f11917 821 { trace_clock, "perf", 1 },
8cbd9cc6 822 ARCH_TRACE_CLOCKS
5079f326
Z
823};
824
b63f39ea 825/*
826 * trace_parser_get_init - gets the buffer for trace parser
827 */
828int trace_parser_get_init(struct trace_parser *parser, int size)
829{
830 memset(parser, 0, sizeof(*parser));
831
832 parser->buffer = kmalloc(size, GFP_KERNEL);
833 if (!parser->buffer)
834 return 1;
835
836 parser->size = size;
837 return 0;
838}
839
840/*
841 * trace_parser_put - frees the buffer for trace parser
842 */
843void trace_parser_put(struct trace_parser *parser)
844{
845 kfree(parser->buffer);
846}
847
848/*
849 * trace_get_user - reads the user input string separated by space
850 * (matched by isspace(ch))
851 *
852 * For each string found the 'struct trace_parser' is updated,
853 * and the function returns.
854 *
855 * Returns number of bytes read.
856 *
857 * See kernel/trace/trace.h for 'struct trace_parser' details.
858 */
859int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
860 size_t cnt, loff_t *ppos)
861{
862 char ch;
863 size_t read = 0;
864 ssize_t ret;
865
866 if (!*ppos)
867 trace_parser_clear(parser);
868
869 ret = get_user(ch, ubuf++);
870 if (ret)
871 goto out;
872
873 read++;
874 cnt--;
875
876 /*
877 * The parser is not finished with the last write,
878 * continue reading the user input without skipping spaces.
879 */
880 if (!parser->cont) {
881 /* skip white space */
882 while (cnt && isspace(ch)) {
883 ret = get_user(ch, ubuf++);
884 if (ret)
885 goto out;
886 read++;
887 cnt--;
888 }
889
890 /* only spaces were written */
891 if (isspace(ch)) {
892 *ppos += read;
893 ret = read;
894 goto out;
895 }
896
897 parser->idx = 0;
898 }
899
900 /* read the non-space input */
901 while (cnt && !isspace(ch)) {
3c235a33 902 if (parser->idx < parser->size - 1)
b63f39ea 903 parser->buffer[parser->idx++] = ch;
904 else {
905 ret = -EINVAL;
906 goto out;
907 }
908 ret = get_user(ch, ubuf++);
909 if (ret)
910 goto out;
911 read++;
912 cnt--;
913 }
914
915 /* We either got finished input or we have to wait for another call. */
916 if (isspace(ch)) {
917 parser->buffer[parser->idx] = 0;
918 parser->cont = false;
057db848 919 } else if (parser->idx < parser->size - 1) {
b63f39ea 920 parser->cont = true;
921 parser->buffer[parser->idx++] = ch;
057db848
SR
922 } else {
923 ret = -EINVAL;
924 goto out;
b63f39ea 925 }
926
927 *ppos += read;
928 ret = read;
929
930out:
931 return ret;
932}
933
6c6c2796
PP
934ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
935{
936 int len;
937 int ret;
938
2dc5d12b
SR
939 if (!cnt)
940 return 0;
941
6c6c2796
PP
942 if (s->len <= s->readpos)
943 return -EBUSY;
944
945 len = s->len - s->readpos;
946 if (cnt > len)
947 cnt = len;
948 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
2dc5d12b 949 if (ret == cnt)
6c6c2796
PP
950 return -EFAULT;
951
2dc5d12b
SR
952 cnt -= ret;
953
e74da523 954 s->readpos += cnt;
6c6c2796 955 return cnt;
214023c3
SR
956}
957
b8b94265 958static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
3c56819b
EGM
959{
960 int len;
3c56819b
EGM
961
962 if (s->len <= s->readpos)
963 return -EBUSY;
964
965 len = s->len - s->readpos;
966 if (cnt > len)
967 cnt = len;
5a26c8f0 968 memcpy(buf, s->buffer + s->readpos, cnt);
3c56819b 969
e74da523 970 s->readpos += cnt;
3c56819b
EGM
971 return cnt;
972}
973
0e950173
TB
974unsigned long __read_mostly tracing_thresh;
975
5d4a9dba 976#ifdef CONFIG_TRACER_MAX_TRACE
5d4a9dba
SR
977/*
978 * Copy the new maximum trace into the separate maximum-trace
979 * structure. (this way the maximum trace is permanently saved,
980 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
981 */
982static void
983__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
984{
12883efb
SRRH
985 struct trace_buffer *trace_buf = &tr->trace_buffer;
986 struct trace_buffer *max_buf = &tr->max_buffer;
987 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
988 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
5d4a9dba 989
12883efb
SRRH
990 max_buf->cpu = cpu;
991 max_buf->time_start = data->preempt_timestamp;
5d4a9dba 992
6d9b3fa5 993 max_data->saved_latency = tr->max_latency;
8248ac05
SR
994 max_data->critical_start = data->critical_start;
995 max_data->critical_end = data->critical_end;
5d4a9dba 996
1acaa1b2 997 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
8248ac05 998 max_data->pid = tsk->pid;
f17a5194
SRRH
999 /*
1000 * If tsk == current, then use current_uid(), as that does not use
1001 * RCU. The irq tracer can be called out of RCU scope.
1002 */
1003 if (tsk == current)
1004 max_data->uid = current_uid();
1005 else
1006 max_data->uid = task_uid(tsk);
1007
8248ac05
SR
1008 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1009 max_data->policy = tsk->policy;
1010 max_data->rt_priority = tsk->rt_priority;
5d4a9dba
SR
1011
1012 /* record this tasks comm */
1013 tracing_record_cmdline(tsk);
1014}
1015
4fcdae83
SR
1016/**
1017 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1018 * @tr: tracer
1019 * @tsk: the task with the latency
1020 * @cpu: The cpu that initiated the trace.
1021 *
1022 * Flip the buffers between the @tr and the max_tr and record information
1023 * about which task was the cause of this latency.
1024 */
e309b41d 1025void
bc0c38d1
SR
1026update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1027{
2721e72d 1028 struct ring_buffer *buf;
bc0c38d1 1029
2b6080f2 1030 if (tr->stop_count)
b8de7bd1
SR
1031 return;
1032
4c11d7ae 1033 WARN_ON_ONCE(!irqs_disabled());
34600f0e 1034
45ad21ca 1035 if (!tr->allocated_snapshot) {
debdd57f 1036 /* Only the nop tracer should hit this when disabling */
2b6080f2 1037 WARN_ON_ONCE(tr->current_trace != &nop_trace);
34600f0e 1038 return;
debdd57f 1039 }
34600f0e 1040
0b9b12c1 1041 arch_spin_lock(&tr->max_lock);
3928a8a2 1042
12883efb
SRRH
1043 buf = tr->trace_buffer.buffer;
1044 tr->trace_buffer.buffer = tr->max_buffer.buffer;
1045 tr->max_buffer.buffer = buf;
3928a8a2 1046
bc0c38d1 1047 __update_max_tr(tr, tsk, cpu);
0b9b12c1 1048 arch_spin_unlock(&tr->max_lock);
bc0c38d1
SR
1049}
1050
1051/**
1052 * update_max_tr_single - only copy one trace over, and reset the rest
1053 * @tr - tracer
1054 * @tsk - task with the latency
1055 * @cpu - the cpu of the buffer to copy.
4fcdae83
SR
1056 *
1057 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
bc0c38d1 1058 */
e309b41d 1059void
bc0c38d1
SR
1060update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1061{
3928a8a2 1062 int ret;
bc0c38d1 1063
2b6080f2 1064 if (tr->stop_count)
b8de7bd1
SR
1065 return;
1066
4c11d7ae 1067 WARN_ON_ONCE(!irqs_disabled());
6c24499f 1068 if (!tr->allocated_snapshot) {
2930e04d 1069 /* Only the nop tracer should hit this when disabling */
9e8529af 1070 WARN_ON_ONCE(tr->current_trace != &nop_trace);
ef710e10 1071 return;
2930e04d 1072 }
ef710e10 1073
0b9b12c1 1074 arch_spin_lock(&tr->max_lock);
bc0c38d1 1075
12883efb 1076 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
3928a8a2 1077
e8165dbb
SR
1078 if (ret == -EBUSY) {
1079 /*
1080 * We failed to swap the buffer due to a commit taking
1081 * place on this CPU. We fail to record, but we reset
1082 * the max trace buffer (no one writes directly to it)
1083 * and flag that it failed.
1084 */
12883efb 1085 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
e8165dbb
SR
1086 "Failed to swap buffers due to commit in progress\n");
1087 }
1088
e8165dbb 1089 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
bc0c38d1
SR
1090
1091 __update_max_tr(tr, tsk, cpu);
0b9b12c1 1092 arch_spin_unlock(&tr->max_lock);
bc0c38d1 1093}
5d4a9dba 1094#endif /* CONFIG_TRACER_MAX_TRACE */
bc0c38d1 1095
8b8b3683 1096static int wait_on_pipe(struct trace_iterator *iter)
0d5c6e1c 1097{
15693458
SRRH
1098 /* Iterators are static, they should be filled or empty */
1099 if (trace_buffer_iter(iter, iter->cpu_file))
8b8b3683 1100 return 0;
0d5c6e1c 1101
8b8b3683 1102 return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
0d5c6e1c
SR
1103}
1104
f4e781c0
SRRH
1105#ifdef CONFIG_FTRACE_STARTUP_TEST
1106static int run_tracer_selftest(struct tracer *type)
1107{
1108 struct trace_array *tr = &global_trace;
1109 struct tracer *saved_tracer = tr->current_trace;
1110 int ret;
0d5c6e1c 1111
f4e781c0
SRRH
1112 if (!type->selftest || tracing_selftest_disabled)
1113 return 0;
0d5c6e1c
SR
1114
1115 /*
f4e781c0
SRRH
1116 * Run a selftest on this tracer.
1117 * Here we reset the trace buffer, and set the current
1118 * tracer to be this tracer. The tracer can then run some
1119 * internal tracing to verify that everything is in order.
1120 * If we fail, we do not register this tracer.
0d5c6e1c 1121 */
f4e781c0 1122 tracing_reset_online_cpus(&tr->trace_buffer);
0d5c6e1c 1123
f4e781c0
SRRH
1124 tr->current_trace = type;
1125
1126#ifdef CONFIG_TRACER_MAX_TRACE
1127 if (type->use_max_tr) {
1128 /* If we expanded the buffers, make sure the max is expanded too */
1129 if (ring_buffer_expanded)
1130 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1131 RING_BUFFER_ALL_CPUS);
1132 tr->allocated_snapshot = true;
1133 }
1134#endif
1135
1136 /* the test is responsible for initializing and enabling */
1137 pr_info("Testing tracer %s: ", type->name);
1138 ret = type->selftest(type, tr);
1139 /* the test is responsible for resetting too */
1140 tr->current_trace = saved_tracer;
1141 if (ret) {
1142 printk(KERN_CONT "FAILED!\n");
1143 /* Add the warning after printing 'FAILED' */
1144 WARN_ON(1);
1145 return -1;
1146 }
1147 /* Only reset on passing, to avoid touching corrupted buffers */
1148 tracing_reset_online_cpus(&tr->trace_buffer);
1149
1150#ifdef CONFIG_TRACER_MAX_TRACE
1151 if (type->use_max_tr) {
1152 tr->allocated_snapshot = false;
0d5c6e1c 1153
f4e781c0
SRRH
1154 /* Shrink the max buffer again */
1155 if (ring_buffer_expanded)
1156 ring_buffer_resize(tr->max_buffer.buffer, 1,
1157 RING_BUFFER_ALL_CPUS);
1158 }
1159#endif
1160
1161 printk(KERN_CONT "PASSED\n");
1162 return 0;
1163}
1164#else
1165static inline int run_tracer_selftest(struct tracer *type)
1166{
1167 return 0;
0d5c6e1c 1168}
f4e781c0 1169#endif /* CONFIG_FTRACE_STARTUP_TEST */
0d5c6e1c 1170
4fcdae83
SR
1171/**
1172 * register_tracer - register a tracer with the ftrace system.
1173 * @type - the plugin for the tracer
1174 *
1175 * Register a new plugin tracer.
1176 */
bc0c38d1
SR
1177int register_tracer(struct tracer *type)
1178{
1179 struct tracer *t;
bc0c38d1
SR
1180 int ret = 0;
1181
1182 if (!type->name) {
1183 pr_info("Tracer must have a name\n");
1184 return -1;
1185 }
1186
24a461d5 1187 if (strlen(type->name) >= MAX_TRACER_SIZE) {
ee6c2c1b
LZ
1188 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1189 return -1;
1190 }
1191
bc0c38d1 1192 mutex_lock(&trace_types_lock);
86fa2f60 1193
8e1b82e0
FW
1194 tracing_selftest_running = true;
1195
bc0c38d1
SR
1196 for (t = trace_types; t; t = t->next) {
1197 if (strcmp(type->name, t->name) == 0) {
1198 /* already found */
ee6c2c1b 1199 pr_info("Tracer %s already registered\n",
bc0c38d1
SR
1200 type->name);
1201 ret = -1;
1202 goto out;
1203 }
1204 }
1205
adf9f195
FW
1206 if (!type->set_flag)
1207 type->set_flag = &dummy_set_flag;
1208 if (!type->flags)
1209 type->flags = &dummy_tracer_flags;
1210 else
1211 if (!type->flags->opts)
1212 type->flags->opts = dummy_tracer_opt;
6eaaa5d5 1213
f4e781c0
SRRH
1214 ret = run_tracer_selftest(type);
1215 if (ret < 0)
1216 goto out;
60a11774 1217
bc0c38d1
SR
1218 type->next = trace_types;
1219 trace_types = type;
60a11774 1220
bc0c38d1 1221 out:
8e1b82e0 1222 tracing_selftest_running = false;
bc0c38d1
SR
1223 mutex_unlock(&trace_types_lock);
1224
dac74940
SR
1225 if (ret || !default_bootup_tracer)
1226 goto out_unlock;
1227
ee6c2c1b 1228 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
dac74940
SR
1229 goto out_unlock;
1230
1231 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1232 /* Do we want this tracer to start on bootup? */
607e2ea1 1233 tracing_set_tracer(&global_trace, type->name);
dac74940
SR
1234 default_bootup_tracer = NULL;
1235 /* disable other selftests, since this will break it. */
55034cd6 1236 tracing_selftest_disabled = true;
b2821ae6 1237#ifdef CONFIG_FTRACE_STARTUP_TEST
dac74940
SR
1238 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1239 type->name);
b2821ae6 1240#endif
b2821ae6 1241
dac74940 1242 out_unlock:
bc0c38d1
SR
1243 return ret;
1244}
1245
12883efb 1246void tracing_reset(struct trace_buffer *buf, int cpu)
f633903a 1247{
12883efb 1248 struct ring_buffer *buffer = buf->buffer;
f633903a 1249
a5416411
HT
1250 if (!buffer)
1251 return;
1252
f633903a
SR
1253 ring_buffer_record_disable(buffer);
1254
1255 /* Make sure all commits have finished */
1256 synchronize_sched();
68179686 1257 ring_buffer_reset_cpu(buffer, cpu);
f633903a
SR
1258
1259 ring_buffer_record_enable(buffer);
1260}
1261
12883efb 1262void tracing_reset_online_cpus(struct trace_buffer *buf)
213cc060 1263{
12883efb 1264 struct ring_buffer *buffer = buf->buffer;
213cc060
PE
1265 int cpu;
1266
a5416411
HT
1267 if (!buffer)
1268 return;
1269
621968cd
SR
1270 ring_buffer_record_disable(buffer);
1271
1272 /* Make sure all commits have finished */
1273 synchronize_sched();
1274
9457158b 1275 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
213cc060
PE
1276
1277 for_each_online_cpu(cpu)
68179686 1278 ring_buffer_reset_cpu(buffer, cpu);
621968cd
SR
1279
1280 ring_buffer_record_enable(buffer);
213cc060
PE
1281}
1282
09d8091c 1283/* Must have trace_types_lock held */
873c642f 1284void tracing_reset_all_online_cpus(void)
9456f0fa 1285{
873c642f
SRRH
1286 struct trace_array *tr;
1287
873c642f 1288 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
12883efb
SRRH
1289 tracing_reset_online_cpus(&tr->trace_buffer);
1290#ifdef CONFIG_TRACER_MAX_TRACE
1291 tracing_reset_online_cpus(&tr->max_buffer);
1292#endif
873c642f 1293 }
9456f0fa
SR
1294}
1295
939c7a4f 1296#define SAVED_CMDLINES_DEFAULT 128
2c7eea4c 1297#define NO_CMDLINE_MAP UINT_MAX
edc35bd7 1298static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
939c7a4f
YY
1299struct saved_cmdlines_buffer {
1300 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1301 unsigned *map_cmdline_to_pid;
1302 unsigned cmdline_num;
1303 int cmdline_idx;
1304 char *saved_cmdlines;
1305};
1306static struct saved_cmdlines_buffer *savedcmd;
25b0b44a 1307
25b0b44a 1308/* temporary disable recording */
4fd27358 1309static atomic_t trace_record_cmdline_disabled __read_mostly;
bc0c38d1 1310
939c7a4f
YY
1311static inline char *get_saved_cmdlines(int idx)
1312{
1313 return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
1314}
1315
1316static inline void set_cmdline(int idx, const char *cmdline)
bc0c38d1 1317{
939c7a4f
YY
1318 memcpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
1319}
1320
1321static int allocate_cmdlines_buffer(unsigned int val,
1322 struct saved_cmdlines_buffer *s)
1323{
1324 s->map_cmdline_to_pid = kmalloc(val * sizeof(*s->map_cmdline_to_pid),
1325 GFP_KERNEL);
1326 if (!s->map_cmdline_to_pid)
1327 return -ENOMEM;
1328
1329 s->saved_cmdlines = kmalloc(val * TASK_COMM_LEN, GFP_KERNEL);
1330 if (!s->saved_cmdlines) {
1331 kfree(s->map_cmdline_to_pid);
1332 return -ENOMEM;
1333 }
1334
1335 s->cmdline_idx = 0;
1336 s->cmdline_num = val;
1337 memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
1338 sizeof(s->map_pid_to_cmdline));
1339 memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
1340 val * sizeof(*s->map_cmdline_to_pid));
1341
1342 return 0;
1343}
1344
1345static int trace_create_savedcmd(void)
1346{
1347 int ret;
1348
a6af8fbf 1349 savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
939c7a4f
YY
1350 if (!savedcmd)
1351 return -ENOMEM;
1352
1353 ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
1354 if (ret < 0) {
1355 kfree(savedcmd);
1356 savedcmd = NULL;
1357 return -ENOMEM;
1358 }
1359
1360 return 0;
bc0c38d1
SR
1361}
1362
b5130b1e
CE
1363int is_tracing_stopped(void)
1364{
2b6080f2 1365 return global_trace.stop_count;
b5130b1e
CE
1366}
1367
0f048701
SR
1368/**
1369 * tracing_start - quick start of the tracer
1370 *
1371 * If tracing is enabled but was stopped by tracing_stop,
1372 * this will start the tracer back up.
1373 */
1374void tracing_start(void)
1375{
1376 struct ring_buffer *buffer;
1377 unsigned long flags;
1378
1379 if (tracing_disabled)
1380 return;
1381
2b6080f2
SR
1382 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1383 if (--global_trace.stop_count) {
1384 if (global_trace.stop_count < 0) {
b06a8301
SR
1385 /* Someone screwed up their debugging */
1386 WARN_ON_ONCE(1);
2b6080f2 1387 global_trace.stop_count = 0;
b06a8301 1388 }
0f048701
SR
1389 goto out;
1390 }
1391
a2f80714 1392 /* Prevent the buffers from switching */
0b9b12c1 1393 arch_spin_lock(&global_trace.max_lock);
0f048701 1394
12883efb 1395 buffer = global_trace.trace_buffer.buffer;
0f048701
SR
1396 if (buffer)
1397 ring_buffer_record_enable(buffer);
1398
12883efb
SRRH
1399#ifdef CONFIG_TRACER_MAX_TRACE
1400 buffer = global_trace.max_buffer.buffer;
0f048701
SR
1401 if (buffer)
1402 ring_buffer_record_enable(buffer);
12883efb 1403#endif
0f048701 1404
0b9b12c1 1405 arch_spin_unlock(&global_trace.max_lock);
a2f80714 1406
0f048701 1407 out:
2b6080f2
SR
1408 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1409}
1410
1411static void tracing_start_tr(struct trace_array *tr)
1412{
1413 struct ring_buffer *buffer;
1414 unsigned long flags;
1415
1416 if (tracing_disabled)
1417 return;
1418
1419 /* If global, we need to also start the max tracer */
1420 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1421 return tracing_start();
1422
1423 raw_spin_lock_irqsave(&tr->start_lock, flags);
1424
1425 if (--tr->stop_count) {
1426 if (tr->stop_count < 0) {
1427 /* Someone screwed up their debugging */
1428 WARN_ON_ONCE(1);
1429 tr->stop_count = 0;
1430 }
1431 goto out;
1432 }
1433
12883efb 1434 buffer = tr->trace_buffer.buffer;
2b6080f2
SR
1435 if (buffer)
1436 ring_buffer_record_enable(buffer);
1437
1438 out:
1439 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
0f048701
SR
1440}
1441
1442/**
1443 * tracing_stop - quick stop of the tracer
1444 *
1445 * Light weight way to stop tracing. Use in conjunction with
1446 * tracing_start.
1447 */
1448void tracing_stop(void)
1449{
1450 struct ring_buffer *buffer;
1451 unsigned long flags;
1452
2b6080f2
SR
1453 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1454 if (global_trace.stop_count++)
0f048701
SR
1455 goto out;
1456
a2f80714 1457 /* Prevent the buffers from switching */
0b9b12c1 1458 arch_spin_lock(&global_trace.max_lock);
a2f80714 1459
12883efb 1460 buffer = global_trace.trace_buffer.buffer;
0f048701
SR
1461 if (buffer)
1462 ring_buffer_record_disable(buffer);
1463
12883efb
SRRH
1464#ifdef CONFIG_TRACER_MAX_TRACE
1465 buffer = global_trace.max_buffer.buffer;
0f048701
SR
1466 if (buffer)
1467 ring_buffer_record_disable(buffer);
12883efb 1468#endif
0f048701 1469
0b9b12c1 1470 arch_spin_unlock(&global_trace.max_lock);
a2f80714 1471
0f048701 1472 out:
2b6080f2
SR
1473 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1474}
1475
1476static void tracing_stop_tr(struct trace_array *tr)
1477{
1478 struct ring_buffer *buffer;
1479 unsigned long flags;
1480
1481 /* If global, we need to also stop the max tracer */
1482 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1483 return tracing_stop();
1484
1485 raw_spin_lock_irqsave(&tr->start_lock, flags);
1486 if (tr->stop_count++)
1487 goto out;
1488
12883efb 1489 buffer = tr->trace_buffer.buffer;
2b6080f2
SR
1490 if (buffer)
1491 ring_buffer_record_disable(buffer);
1492
1493 out:
1494 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
0f048701
SR
1495}
1496
e309b41d 1497void trace_stop_cmdline_recording(void);
bc0c38d1 1498
379cfdac 1499static int trace_save_cmdline(struct task_struct *tsk)
bc0c38d1 1500{
a635cf04 1501 unsigned pid, idx;
bc0c38d1
SR
1502
1503 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
379cfdac 1504 return 0;
bc0c38d1
SR
1505
1506 /*
1507 * It's not the end of the world if we don't get
1508 * the lock, but we also don't want to spin
1509 * nor do we want to disable interrupts,
1510 * so if we miss here, then better luck next time.
1511 */
0199c4e6 1512 if (!arch_spin_trylock(&trace_cmdline_lock))
379cfdac 1513 return 0;
bc0c38d1 1514
939c7a4f 1515 idx = savedcmd->map_pid_to_cmdline[tsk->pid];
2c7eea4c 1516 if (idx == NO_CMDLINE_MAP) {
939c7a4f 1517 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
bc0c38d1 1518
a635cf04
CE
1519 /*
1520 * Check whether the cmdline buffer at idx has a pid
1521 * mapped. We are going to overwrite that entry so we
1522 * need to clear the map_pid_to_cmdline. Otherwise we
1523 * would read the new comm for the old pid.
1524 */
939c7a4f 1525 pid = savedcmd->map_cmdline_to_pid[idx];
a635cf04 1526 if (pid != NO_CMDLINE_MAP)
939c7a4f 1527 savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
bc0c38d1 1528
939c7a4f
YY
1529 savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
1530 savedcmd->map_pid_to_cmdline[tsk->pid] = idx;
bc0c38d1 1531
939c7a4f 1532 savedcmd->cmdline_idx = idx;
bc0c38d1
SR
1533 }
1534
939c7a4f 1535 set_cmdline(idx, tsk->comm);
bc0c38d1 1536
0199c4e6 1537 arch_spin_unlock(&trace_cmdline_lock);
379cfdac
SRRH
1538
1539 return 1;
bc0c38d1
SR
1540}
1541
4c27e756 1542static void __trace_find_cmdline(int pid, char comm[])
bc0c38d1 1543{
bc0c38d1
SR
1544 unsigned map;
1545
4ca53085
SR
1546 if (!pid) {
1547 strcpy(comm, "<idle>");
1548 return;
1549 }
bc0c38d1 1550
74bf4076
SR
1551 if (WARN_ON_ONCE(pid < 0)) {
1552 strcpy(comm, "<XXX>");
1553 return;
1554 }
1555
4ca53085
SR
1556 if (pid > PID_MAX_DEFAULT) {
1557 strcpy(comm, "<...>");
1558 return;
1559 }
bc0c38d1 1560
939c7a4f 1561 map = savedcmd->map_pid_to_cmdline[pid];
50d88758 1562 if (map != NO_CMDLINE_MAP)
939c7a4f 1563 strcpy(comm, get_saved_cmdlines(map));
50d88758
TG
1564 else
1565 strcpy(comm, "<...>");
4c27e756
SRRH
1566}
1567
1568void trace_find_cmdline(int pid, char comm[])
1569{
1570 preempt_disable();
1571 arch_spin_lock(&trace_cmdline_lock);
1572
1573 __trace_find_cmdline(pid, comm);
bc0c38d1 1574
0199c4e6 1575 arch_spin_unlock(&trace_cmdline_lock);
5b6045a9 1576 preempt_enable();
bc0c38d1
SR
1577}
1578
e309b41d 1579void tracing_record_cmdline(struct task_struct *tsk)
bc0c38d1 1580{
0fb9656d 1581 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
bc0c38d1
SR
1582 return;
1583
7ffbd48d
SR
1584 if (!__this_cpu_read(trace_cmdline_save))
1585 return;
1586
379cfdac
SRRH
1587 if (trace_save_cmdline(tsk))
1588 __this_cpu_write(trace_cmdline_save, false);
bc0c38d1
SR
1589}
1590
45dcd8b8 1591void
38697053
SR
1592tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1593 int pc)
bc0c38d1
SR
1594{
1595 struct task_struct *tsk = current;
bc0c38d1 1596
777e208d
SR
1597 entry->preempt_count = pc & 0xff;
1598 entry->pid = (tsk) ? tsk->pid : 0;
1599 entry->flags =
9244489a 1600#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
2e2ca155 1601 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
9244489a
SR
1602#else
1603 TRACE_FLAG_IRQS_NOSUPPORT |
1604#endif
bc0c38d1
SR
1605 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1606 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
e5137b50
PZ
1607 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
1608 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
bc0c38d1 1609}
f413cdb8 1610EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
bc0c38d1 1611
e77405ad
SR
1612struct ring_buffer_event *
1613trace_buffer_lock_reserve(struct ring_buffer *buffer,
1614 int type,
1615 unsigned long len,
1616 unsigned long flags, int pc)
51a763dd
ACM
1617{
1618 struct ring_buffer_event *event;
1619
e77405ad 1620 event = ring_buffer_lock_reserve(buffer, len);
51a763dd
ACM
1621 if (event != NULL) {
1622 struct trace_entry *ent = ring_buffer_event_data(event);
1623
1624 tracing_generic_entry_update(ent, flags, pc);
1625 ent->type = type;
1626 }
1627
1628 return event;
1629}
51a763dd 1630
7ffbd48d
SR
1631void
1632__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1633{
1634 __this_cpu_write(trace_cmdline_save, true);
1635 ring_buffer_unlock_commit(buffer, event);
1636}
1637
e77405ad
SR
1638static inline void
1639__trace_buffer_unlock_commit(struct ring_buffer *buffer,
1640 struct ring_buffer_event *event,
0d5c6e1c 1641 unsigned long flags, int pc)
51a763dd 1642{
7ffbd48d 1643 __buffer_unlock_commit(buffer, event);
51a763dd 1644
e77405ad
SR
1645 ftrace_trace_stack(buffer, flags, 6, pc);
1646 ftrace_trace_userstack(buffer, flags, pc);
07edf712
FW
1647}
1648
e77405ad
SR
1649void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1650 struct ring_buffer_event *event,
1651 unsigned long flags, int pc)
07edf712 1652{
0d5c6e1c 1653 __trace_buffer_unlock_commit(buffer, event, flags, pc);
51a763dd 1654}
0d5c6e1c 1655EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
51a763dd 1656
2c4a33ab
SRRH
1657static struct ring_buffer *temp_buffer;
1658
ccb469a1
SR
1659struct ring_buffer_event *
1660trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1661 struct ftrace_event_file *ftrace_file,
1662 int type, unsigned long len,
1663 unsigned long flags, int pc)
1664{
2c4a33ab
SRRH
1665 struct ring_buffer_event *entry;
1666
12883efb 1667 *current_rb = ftrace_file->tr->trace_buffer.buffer;
2c4a33ab 1668 entry = trace_buffer_lock_reserve(*current_rb,
ccb469a1 1669 type, len, flags, pc);
2c4a33ab
SRRH
1670 /*
1671 * If tracing is off, but we have triggers enabled
1672 * we still need to look at the event data. Use the temp_buffer
1673 * to store the trace event for the tigger to use. It's recusive
1674 * safe and will not be recorded anywhere.
1675 */
1676 if (!entry && ftrace_file->flags & FTRACE_EVENT_FL_TRIGGER_COND) {
1677 *current_rb = temp_buffer;
1678 entry = trace_buffer_lock_reserve(*current_rb,
1679 type, len, flags, pc);
1680 }
1681 return entry;
ccb469a1
SR
1682}
1683EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1684
ef5580d0 1685struct ring_buffer_event *
e77405ad
SR
1686trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1687 int type, unsigned long len,
ef5580d0
SR
1688 unsigned long flags, int pc)
1689{
12883efb 1690 *current_rb = global_trace.trace_buffer.buffer;
e77405ad 1691 return trace_buffer_lock_reserve(*current_rb,
ef5580d0
SR
1692 type, len, flags, pc);
1693}
94487d6d 1694EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
ef5580d0 1695
e77405ad
SR
1696void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1697 struct ring_buffer_event *event,
ef5580d0
SR
1698 unsigned long flags, int pc)
1699{
0d5c6e1c 1700 __trace_buffer_unlock_commit(buffer, event, flags, pc);
07edf712 1701}
94487d6d 1702EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
07edf712 1703
0d5c6e1c
SR
1704void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1705 struct ring_buffer_event *event,
1706 unsigned long flags, int pc,
1707 struct pt_regs *regs)
1fd8df2c 1708{
7ffbd48d 1709 __buffer_unlock_commit(buffer, event);
1fd8df2c
MH
1710
1711 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1712 ftrace_trace_userstack(buffer, flags, pc);
1713}
0d5c6e1c 1714EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1fd8df2c 1715
e77405ad
SR
1716void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1717 struct ring_buffer_event *event)
77d9f465 1718{
e77405ad 1719 ring_buffer_discard_commit(buffer, event);
ef5580d0 1720}
12acd473 1721EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
ef5580d0 1722
e309b41d 1723void
7be42151 1724trace_function(struct trace_array *tr,
38697053
SR
1725 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1726 int pc)
bc0c38d1 1727{
e1112b4d 1728 struct ftrace_event_call *call = &event_function;
12883efb 1729 struct ring_buffer *buffer = tr->trace_buffer.buffer;
3928a8a2 1730 struct ring_buffer_event *event;
777e208d 1731 struct ftrace_entry *entry;
bc0c38d1 1732
d769041f 1733 /* If we are reading the ring buffer, don't trace */
dd17c8f7 1734 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
d769041f
SR
1735 return;
1736
e77405ad 1737 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
51a763dd 1738 flags, pc);
3928a8a2
SR
1739 if (!event)
1740 return;
1741 entry = ring_buffer_event_data(event);
777e208d
SR
1742 entry->ip = ip;
1743 entry->parent_ip = parent_ip;
e1112b4d 1744
f306cc82 1745 if (!call_filter_check_discard(call, entry, buffer, event))
7ffbd48d 1746 __buffer_unlock_commit(buffer, event);
bc0c38d1
SR
1747}
1748
c0a0d0d3 1749#ifdef CONFIG_STACKTRACE
4a9bd3f1
SR
1750
1751#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1752struct ftrace_stack {
1753 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1754};
1755
1756static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1757static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1758
e77405ad 1759static void __ftrace_trace_stack(struct ring_buffer *buffer,
53614991 1760 unsigned long flags,
1fd8df2c 1761 int skip, int pc, struct pt_regs *regs)
86387f7e 1762{
e1112b4d 1763 struct ftrace_event_call *call = &event_kernel_stack;
3928a8a2 1764 struct ring_buffer_event *event;
777e208d 1765 struct stack_entry *entry;
86387f7e 1766 struct stack_trace trace;
4a9bd3f1
SR
1767 int use_stack;
1768 int size = FTRACE_STACK_ENTRIES;
1769
1770 trace.nr_entries = 0;
1771 trace.skip = skip;
1772
1773 /*
1774 * Since events can happen in NMIs there's no safe way to
1775 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1776 * or NMI comes in, it will just have to use the default
1777 * FTRACE_STACK_SIZE.
1778 */
1779 preempt_disable_notrace();
1780
82146529 1781 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
4a9bd3f1
SR
1782 /*
1783 * We don't need any atomic variables, just a barrier.
1784 * If an interrupt comes in, we don't care, because it would
1785 * have exited and put the counter back to what we want.
1786 * We just need a barrier to keep gcc from moving things
1787 * around.
1788 */
1789 barrier();
1790 if (use_stack == 1) {
bdffd893 1791 trace.entries = this_cpu_ptr(ftrace_stack.calls);
4a9bd3f1
SR
1792 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1793
1794 if (regs)
1795 save_stack_trace_regs(regs, &trace);
1796 else
1797 save_stack_trace(&trace);
1798
1799 if (trace.nr_entries > size)
1800 size = trace.nr_entries;
1801 } else
1802 /* From now on, use_stack is a boolean */
1803 use_stack = 0;
1804
1805 size *= sizeof(unsigned long);
86387f7e 1806
e77405ad 1807 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
4a9bd3f1 1808 sizeof(*entry) + size, flags, pc);
3928a8a2 1809 if (!event)
4a9bd3f1
SR
1810 goto out;
1811 entry = ring_buffer_event_data(event);
86387f7e 1812
4a9bd3f1
SR
1813 memset(&entry->caller, 0, size);
1814
1815 if (use_stack)
1816 memcpy(&entry->caller, trace.entries,
1817 trace.nr_entries * sizeof(unsigned long));
1818 else {
1819 trace.max_entries = FTRACE_STACK_ENTRIES;
1820 trace.entries = entry->caller;
1821 if (regs)
1822 save_stack_trace_regs(regs, &trace);
1823 else
1824 save_stack_trace(&trace);
1825 }
1826
1827 entry->size = trace.nr_entries;
86387f7e 1828
f306cc82 1829 if (!call_filter_check_discard(call, entry, buffer, event))
7ffbd48d 1830 __buffer_unlock_commit(buffer, event);
4a9bd3f1
SR
1831
1832 out:
1833 /* Again, don't let gcc optimize things here */
1834 barrier();
82146529 1835 __this_cpu_dec(ftrace_stack_reserve);
4a9bd3f1
SR
1836 preempt_enable_notrace();
1837
f0a920d5
IM
1838}
1839
1fd8df2c
MH
1840void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1841 int skip, int pc, struct pt_regs *regs)
1842{
1843 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1844 return;
1845
1846 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1847}
1848
e77405ad
SR
1849void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1850 int skip, int pc)
53614991
SR
1851{
1852 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1853 return;
1854
1fd8df2c 1855 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
53614991
SR
1856}
1857
c0a0d0d3
FW
1858void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1859 int pc)
38697053 1860{
12883efb 1861 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
38697053
SR
1862}
1863
03889384
SR
1864/**
1865 * trace_dump_stack - record a stack back trace in the trace buffer
c142be8e 1866 * @skip: Number of functions to skip (helper handlers)
03889384 1867 */
c142be8e 1868void trace_dump_stack(int skip)
03889384
SR
1869{
1870 unsigned long flags;
1871
1872 if (tracing_disabled || tracing_selftest_running)
e36c5458 1873 return;
03889384
SR
1874
1875 local_save_flags(flags);
1876
c142be8e
SRRH
1877 /*
1878 * Skip 3 more, seems to get us at the caller of
1879 * this function.
1880 */
1881 skip += 3;
1882 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1883 flags, skip, preempt_count(), NULL);
03889384
SR
1884}
1885
91e86e56
SR
1886static DEFINE_PER_CPU(int, user_stack_count);
1887
e77405ad
SR
1888void
1889ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
02b67518 1890{
e1112b4d 1891 struct ftrace_event_call *call = &event_user_stack;
8d7c6a96 1892 struct ring_buffer_event *event;
02b67518
TE
1893 struct userstack_entry *entry;
1894 struct stack_trace trace;
02b67518
TE
1895
1896 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1897 return;
1898
b6345879
SR
1899 /*
1900 * NMIs can not handle page faults, even with fix ups.
1901 * The save user stack can (and often does) fault.
1902 */
1903 if (unlikely(in_nmi()))
1904 return;
02b67518 1905
91e86e56
SR
1906 /*
1907 * prevent recursion, since the user stack tracing may
1908 * trigger other kernel events.
1909 */
1910 preempt_disable();
1911 if (__this_cpu_read(user_stack_count))
1912 goto out;
1913
1914 __this_cpu_inc(user_stack_count);
1915
e77405ad 1916 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
51a763dd 1917 sizeof(*entry), flags, pc);
02b67518 1918 if (!event)
1dbd1951 1919 goto out_drop_count;
02b67518 1920 entry = ring_buffer_event_data(event);
02b67518 1921
48659d31 1922 entry->tgid = current->tgid;
02b67518
TE
1923 memset(&entry->caller, 0, sizeof(entry->caller));
1924
1925 trace.nr_entries = 0;
1926 trace.max_entries = FTRACE_STACK_ENTRIES;
1927 trace.skip = 0;
1928 trace.entries = entry->caller;
1929
1930 save_stack_trace_user(&trace);
f306cc82 1931 if (!call_filter_check_discard(call, entry, buffer, event))
7ffbd48d 1932 __buffer_unlock_commit(buffer, event);
91e86e56 1933
1dbd1951 1934 out_drop_count:
91e86e56 1935 __this_cpu_dec(user_stack_count);
91e86e56
SR
1936 out:
1937 preempt_enable();
02b67518
TE
1938}
1939
4fd27358
HE
1940#ifdef UNUSED
1941static void __trace_userstack(struct trace_array *tr, unsigned long flags)
02b67518 1942{
7be42151 1943 ftrace_trace_userstack(tr, flags, preempt_count());
02b67518 1944}
4fd27358 1945#endif /* UNUSED */
02b67518 1946
c0a0d0d3
FW
1947#endif /* CONFIG_STACKTRACE */
1948
07d777fe
SR
1949/* created for use with alloc_percpu */
1950struct trace_buffer_struct {
1951 char buffer[TRACE_BUF_SIZE];
1952};
1953
1954static struct trace_buffer_struct *trace_percpu_buffer;
1955static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1956static struct trace_buffer_struct *trace_percpu_irq_buffer;
1957static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1958
1959/*
1960 * The buffer used is dependent on the context. There is a per cpu
1961 * buffer for normal context, softirq contex, hard irq context and
1962 * for NMI context. Thise allows for lockless recording.
1963 *
1964 * Note, if the buffers failed to be allocated, then this returns NULL
1965 */
1966static char *get_trace_buf(void)
1967{
1968 struct trace_buffer_struct *percpu_buffer;
07d777fe
SR
1969
1970 /*
1971 * If we have allocated per cpu buffers, then we do not
1972 * need to do any locking.
1973 */
1974 if (in_nmi())
1975 percpu_buffer = trace_percpu_nmi_buffer;
1976 else if (in_irq())
1977 percpu_buffer = trace_percpu_irq_buffer;
1978 else if (in_softirq())
1979 percpu_buffer = trace_percpu_sirq_buffer;
1980 else
1981 percpu_buffer = trace_percpu_buffer;
1982
1983 if (!percpu_buffer)
1984 return NULL;
1985
d8a0349c 1986 return this_cpu_ptr(&percpu_buffer->buffer[0]);
07d777fe
SR
1987}
1988
1989static int alloc_percpu_trace_buffer(void)
1990{
1991 struct trace_buffer_struct *buffers;
1992 struct trace_buffer_struct *sirq_buffers;
1993 struct trace_buffer_struct *irq_buffers;
1994 struct trace_buffer_struct *nmi_buffers;
1995
1996 buffers = alloc_percpu(struct trace_buffer_struct);
1997 if (!buffers)
1998 goto err_warn;
1999
2000 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
2001 if (!sirq_buffers)
2002 goto err_sirq;
2003
2004 irq_buffers = alloc_percpu(struct trace_buffer_struct);
2005 if (!irq_buffers)
2006 goto err_irq;
2007
2008 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
2009 if (!nmi_buffers)
2010 goto err_nmi;
2011
2012 trace_percpu_buffer = buffers;
2013 trace_percpu_sirq_buffer = sirq_buffers;
2014 trace_percpu_irq_buffer = irq_buffers;
2015 trace_percpu_nmi_buffer = nmi_buffers;
2016
2017 return 0;
2018
2019 err_nmi:
2020 free_percpu(irq_buffers);
2021 err_irq:
2022 free_percpu(sirq_buffers);
2023 err_sirq:
2024 free_percpu(buffers);
2025 err_warn:
2026 WARN(1, "Could not allocate percpu trace_printk buffer");
2027 return -ENOMEM;
2028}
2029
81698831
SR
2030static int buffers_allocated;
2031
07d777fe
SR
2032void trace_printk_init_buffers(void)
2033{
07d777fe
SR
2034 if (buffers_allocated)
2035 return;
2036
2037 if (alloc_percpu_trace_buffer())
2038 return;
2039
2184db46
SR
2040 /* trace_printk() is for debug use only. Don't use it in production. */
2041
2042 pr_warning("\n**********************************************************\n");
2043 pr_warning("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2044 pr_warning("** **\n");
2045 pr_warning("** trace_printk() being used. Allocating extra memory. **\n");
2046 pr_warning("** **\n");
2047 pr_warning("** This means that this is a DEBUG kernel and it is **\n");
2048 pr_warning("** unsafe for produciton use. **\n");
2049 pr_warning("** **\n");
2050 pr_warning("** If you see this message and you are not debugging **\n");
2051 pr_warning("** the kernel, report this immediately to your vendor! **\n");
2052 pr_warning("** **\n");
2053 pr_warning("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2054 pr_warning("**********************************************************\n");
07d777fe 2055
b382ede6
SR
2056 /* Expand the buffers to set size */
2057 tracing_update_buffers();
2058
07d777fe 2059 buffers_allocated = 1;
81698831
SR
2060
2061 /*
2062 * trace_printk_init_buffers() can be called by modules.
2063 * If that happens, then we need to start cmdline recording
2064 * directly here. If the global_trace.buffer is already
2065 * allocated here, then this was called by module code.
2066 */
12883efb 2067 if (global_trace.trace_buffer.buffer)
81698831
SR
2068 tracing_start_cmdline_record();
2069}
2070
2071void trace_printk_start_comm(void)
2072{
2073 /* Start tracing comms if trace printk is set */
2074 if (!buffers_allocated)
2075 return;
2076 tracing_start_cmdline_record();
2077}
2078
2079static void trace_printk_start_stop_comm(int enabled)
2080{
2081 if (!buffers_allocated)
2082 return;
2083
2084 if (enabled)
2085 tracing_start_cmdline_record();
2086 else
2087 tracing_stop_cmdline_record();
07d777fe
SR
2088}
2089
769b0441 2090/**
48ead020 2091 * trace_vbprintk - write binary msg to tracing buffer
769b0441
FW
2092 *
2093 */
40ce74f1 2094int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
769b0441 2095{
e1112b4d 2096 struct ftrace_event_call *call = &event_bprint;
769b0441 2097 struct ring_buffer_event *event;
e77405ad 2098 struct ring_buffer *buffer;
769b0441 2099 struct trace_array *tr = &global_trace;
48ead020 2100 struct bprint_entry *entry;
769b0441 2101 unsigned long flags;
07d777fe
SR
2102 char *tbuffer;
2103 int len = 0, size, pc;
769b0441
FW
2104
2105 if (unlikely(tracing_selftest_running || tracing_disabled))
2106 return 0;
2107
2108 /* Don't pollute graph traces with trace_vprintk internals */
2109 pause_graph_tracing();
2110
2111 pc = preempt_count();
5168ae50 2112 preempt_disable_notrace();
769b0441 2113
07d777fe
SR
2114 tbuffer = get_trace_buf();
2115 if (!tbuffer) {
2116 len = 0;
769b0441 2117 goto out;
07d777fe 2118 }
769b0441 2119
07d777fe 2120 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
769b0441 2121
07d777fe
SR
2122 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
2123 goto out;
769b0441 2124
07d777fe 2125 local_save_flags(flags);
769b0441 2126 size = sizeof(*entry) + sizeof(u32) * len;
12883efb 2127 buffer = tr->trace_buffer.buffer;
e77405ad
SR
2128 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
2129 flags, pc);
769b0441 2130 if (!event)
07d777fe 2131 goto out;
769b0441
FW
2132 entry = ring_buffer_event_data(event);
2133 entry->ip = ip;
769b0441
FW
2134 entry->fmt = fmt;
2135
07d777fe 2136 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
f306cc82 2137 if (!call_filter_check_discard(call, entry, buffer, event)) {
7ffbd48d 2138 __buffer_unlock_commit(buffer, event);
d931369b
SR
2139 ftrace_trace_stack(buffer, flags, 6, pc);
2140 }
769b0441 2141
769b0441 2142out:
5168ae50 2143 preempt_enable_notrace();
769b0441
FW
2144 unpause_graph_tracing();
2145
2146 return len;
2147}
48ead020
FW
2148EXPORT_SYMBOL_GPL(trace_vbprintk);
2149
12883efb
SRRH
2150static int
2151__trace_array_vprintk(struct ring_buffer *buffer,
2152 unsigned long ip, const char *fmt, va_list args)
48ead020 2153{
e1112b4d 2154 struct ftrace_event_call *call = &event_print;
48ead020 2155 struct ring_buffer_event *event;
07d777fe 2156 int len = 0, size, pc;
48ead020 2157 struct print_entry *entry;
07d777fe
SR
2158 unsigned long flags;
2159 char *tbuffer;
48ead020
FW
2160
2161 if (tracing_disabled || tracing_selftest_running)
2162 return 0;
2163
07d777fe
SR
2164 /* Don't pollute graph traces with trace_vprintk internals */
2165 pause_graph_tracing();
2166
48ead020
FW
2167 pc = preempt_count();
2168 preempt_disable_notrace();
48ead020 2169
07d777fe
SR
2170
2171 tbuffer = get_trace_buf();
2172 if (!tbuffer) {
2173 len = 0;
48ead020 2174 goto out;
07d777fe 2175 }
48ead020 2176
07d777fe
SR
2177 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
2178 if (len > TRACE_BUF_SIZE)
2179 goto out;
48ead020 2180
07d777fe 2181 local_save_flags(flags);
48ead020 2182 size = sizeof(*entry) + len + 1;
e77405ad 2183 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
07d777fe 2184 flags, pc);
48ead020 2185 if (!event)
07d777fe 2186 goto out;
48ead020 2187 entry = ring_buffer_event_data(event);
c13d2f7c 2188 entry->ip = ip;
48ead020 2189
07d777fe 2190 memcpy(&entry->buf, tbuffer, len);
c13d2f7c 2191 entry->buf[len] = '\0';
f306cc82 2192 if (!call_filter_check_discard(call, entry, buffer, event)) {
7ffbd48d 2193 __buffer_unlock_commit(buffer, event);
07d777fe 2194 ftrace_trace_stack(buffer, flags, 6, pc);
d931369b 2195 }
48ead020
FW
2196 out:
2197 preempt_enable_notrace();
07d777fe 2198 unpause_graph_tracing();
48ead020
FW
2199
2200 return len;
2201}
659372d3 2202
12883efb
SRRH
2203int trace_array_vprintk(struct trace_array *tr,
2204 unsigned long ip, const char *fmt, va_list args)
2205{
2206 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2207}
2208
2209int trace_array_printk(struct trace_array *tr,
2210 unsigned long ip, const char *fmt, ...)
2211{
2212 int ret;
2213 va_list ap;
2214
2215 if (!(trace_flags & TRACE_ITER_PRINTK))
2216 return 0;
2217
2218 va_start(ap, fmt);
2219 ret = trace_array_vprintk(tr, ip, fmt, ap);
2220 va_end(ap);
2221 return ret;
2222}
2223
2224int trace_array_printk_buf(struct ring_buffer *buffer,
2225 unsigned long ip, const char *fmt, ...)
2226{
2227 int ret;
2228 va_list ap;
2229
2230 if (!(trace_flags & TRACE_ITER_PRINTK))
2231 return 0;
2232
2233 va_start(ap, fmt);
2234 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2235 va_end(ap);
2236 return ret;
2237}
2238
659372d3
SR
2239int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2240{
a813a159 2241 return trace_array_vprintk(&global_trace, ip, fmt, args);
659372d3 2242}
769b0441
FW
2243EXPORT_SYMBOL_GPL(trace_vprintk);
2244
e2ac8ef5 2245static void trace_iterator_increment(struct trace_iterator *iter)
5a90f577 2246{
6d158a81
SR
2247 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2248
5a90f577 2249 iter->idx++;
6d158a81
SR
2250 if (buf_iter)
2251 ring_buffer_read(buf_iter, NULL);
5a90f577
SR
2252}
2253
e309b41d 2254static struct trace_entry *
bc21b478
SR
2255peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2256 unsigned long *lost_events)
dd0e545f 2257{
3928a8a2 2258 struct ring_buffer_event *event;
6d158a81 2259 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
dd0e545f 2260
d769041f
SR
2261 if (buf_iter)
2262 event = ring_buffer_iter_peek(buf_iter, ts);
2263 else
12883efb 2264 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
bc21b478 2265 lost_events);
d769041f 2266
4a9bd3f1
SR
2267 if (event) {
2268 iter->ent_size = ring_buffer_event_length(event);
2269 return ring_buffer_event_data(event);
2270 }
2271 iter->ent_size = 0;
2272 return NULL;
dd0e545f 2273}
d769041f 2274
dd0e545f 2275static struct trace_entry *
bc21b478
SR
2276__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2277 unsigned long *missing_events, u64 *ent_ts)
bc0c38d1 2278{
12883efb 2279 struct ring_buffer *buffer = iter->trace_buffer->buffer;
bc0c38d1 2280 struct trace_entry *ent, *next = NULL;
aa27497c 2281 unsigned long lost_events = 0, next_lost = 0;
b04cc6b1 2282 int cpu_file = iter->cpu_file;
3928a8a2 2283 u64 next_ts = 0, ts;
bc0c38d1 2284 int next_cpu = -1;
12b5da34 2285 int next_size = 0;
bc0c38d1
SR
2286 int cpu;
2287
b04cc6b1
FW
2288 /*
2289 * If we are in a per_cpu trace file, don't bother by iterating over
2290 * all cpu and peek directly.
2291 */
ae3b5093 2292 if (cpu_file > RING_BUFFER_ALL_CPUS) {
b04cc6b1
FW
2293 if (ring_buffer_empty_cpu(buffer, cpu_file))
2294 return NULL;
bc21b478 2295 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
b04cc6b1
FW
2296 if (ent_cpu)
2297 *ent_cpu = cpu_file;
2298
2299 return ent;
2300 }
2301
ab46428c 2302 for_each_tracing_cpu(cpu) {
dd0e545f 2303
3928a8a2
SR
2304 if (ring_buffer_empty_cpu(buffer, cpu))
2305 continue;
dd0e545f 2306
bc21b478 2307 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
dd0e545f 2308
cdd31cd2
IM
2309 /*
2310 * Pick the entry with the smallest timestamp:
2311 */
3928a8a2 2312 if (ent && (!next || ts < next_ts)) {
bc0c38d1
SR
2313 next = ent;
2314 next_cpu = cpu;
3928a8a2 2315 next_ts = ts;
bc21b478 2316 next_lost = lost_events;
12b5da34 2317 next_size = iter->ent_size;
bc0c38d1
SR
2318 }
2319 }
2320
12b5da34
SR
2321 iter->ent_size = next_size;
2322
bc0c38d1
SR
2323 if (ent_cpu)
2324 *ent_cpu = next_cpu;
2325
3928a8a2
SR
2326 if (ent_ts)
2327 *ent_ts = next_ts;
2328
bc21b478
SR
2329 if (missing_events)
2330 *missing_events = next_lost;
2331
bc0c38d1
SR
2332 return next;
2333}
2334
dd0e545f 2335/* Find the next real entry, without updating the iterator itself */
c4a8e8be
FW
2336struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2337 int *ent_cpu, u64 *ent_ts)
bc0c38d1 2338{
bc21b478 2339 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
dd0e545f
SR
2340}
2341
2342/* Find the next real entry, and increment the iterator to the next entry */
955b61e5 2343void *trace_find_next_entry_inc(struct trace_iterator *iter)
dd0e545f 2344{
bc21b478
SR
2345 iter->ent = __find_next_entry(iter, &iter->cpu,
2346 &iter->lost_events, &iter->ts);
dd0e545f 2347
3928a8a2 2348 if (iter->ent)
e2ac8ef5 2349 trace_iterator_increment(iter);
dd0e545f 2350
3928a8a2 2351 return iter->ent ? iter : NULL;
b3806b43 2352}
bc0c38d1 2353
e309b41d 2354static void trace_consume(struct trace_iterator *iter)
b3806b43 2355{
12883efb 2356 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
bc21b478 2357 &iter->lost_events);
bc0c38d1
SR
2358}
2359
e309b41d 2360static void *s_next(struct seq_file *m, void *v, loff_t *pos)
bc0c38d1
SR
2361{
2362 struct trace_iterator *iter = m->private;
bc0c38d1 2363 int i = (int)*pos;
4e3c3333 2364 void *ent;
bc0c38d1 2365
a63ce5b3
SR
2366 WARN_ON_ONCE(iter->leftover);
2367
bc0c38d1
SR
2368 (*pos)++;
2369
2370 /* can't go backwards */
2371 if (iter->idx > i)
2372 return NULL;
2373
2374 if (iter->idx < 0)
955b61e5 2375 ent = trace_find_next_entry_inc(iter);
bc0c38d1
SR
2376 else
2377 ent = iter;
2378
2379 while (ent && iter->idx < i)
955b61e5 2380 ent = trace_find_next_entry_inc(iter);
bc0c38d1
SR
2381
2382 iter->pos = *pos;
2383
bc0c38d1
SR
2384 return ent;
2385}
2386
955b61e5 2387void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2f26ebd5 2388{
2f26ebd5
SR
2389 struct ring_buffer_event *event;
2390 struct ring_buffer_iter *buf_iter;
2391 unsigned long entries = 0;
2392 u64 ts;
2393
12883efb 2394 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2f26ebd5 2395
6d158a81
SR
2396 buf_iter = trace_buffer_iter(iter, cpu);
2397 if (!buf_iter)
2f26ebd5
SR
2398 return;
2399
2f26ebd5
SR
2400 ring_buffer_iter_reset(buf_iter);
2401
2402 /*
2403 * We could have the case with the max latency tracers
2404 * that a reset never took place on a cpu. This is evident
2405 * by the timestamp being before the start of the buffer.
2406 */
2407 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
12883efb 2408 if (ts >= iter->trace_buffer->time_start)
2f26ebd5
SR
2409 break;
2410 entries++;
2411 ring_buffer_read(buf_iter, NULL);
2412 }
2413
12883efb 2414 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2f26ebd5
SR
2415}
2416
d7350c3f 2417/*
d7350c3f
FW
2418 * The current tracer is copied to avoid a global locking
2419 * all around.
2420 */
bc0c38d1
SR
2421static void *s_start(struct seq_file *m, loff_t *pos)
2422{
2423 struct trace_iterator *iter = m->private;
2b6080f2 2424 struct trace_array *tr = iter->tr;
b04cc6b1 2425 int cpu_file = iter->cpu_file;
bc0c38d1
SR
2426 void *p = NULL;
2427 loff_t l = 0;
3928a8a2 2428 int cpu;
bc0c38d1 2429
2fd196ec
HT
2430 /*
2431 * copy the tracer to avoid using a global lock all around.
2432 * iter->trace is a copy of current_trace, the pointer to the
2433 * name may be used instead of a strcmp(), as iter->trace->name
2434 * will point to the same string as current_trace->name.
2435 */
bc0c38d1 2436 mutex_lock(&trace_types_lock);
2b6080f2
SR
2437 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2438 *iter->trace = *tr->current_trace;
d7350c3f 2439 mutex_unlock(&trace_types_lock);
bc0c38d1 2440
12883efb 2441#ifdef CONFIG_TRACER_MAX_TRACE
debdd57f
HT
2442 if (iter->snapshot && iter->trace->use_max_tr)
2443 return ERR_PTR(-EBUSY);
12883efb 2444#endif
debdd57f
HT
2445
2446 if (!iter->snapshot)
2447 atomic_inc(&trace_record_cmdline_disabled);
bc0c38d1 2448
bc0c38d1
SR
2449 if (*pos != iter->pos) {
2450 iter->ent = NULL;
2451 iter->cpu = 0;
2452 iter->idx = -1;
2453
ae3b5093 2454 if (cpu_file == RING_BUFFER_ALL_CPUS) {
b04cc6b1 2455 for_each_tracing_cpu(cpu)
2f26ebd5 2456 tracing_iter_reset(iter, cpu);
b04cc6b1 2457 } else
2f26ebd5 2458 tracing_iter_reset(iter, cpu_file);
bc0c38d1 2459
ac91d854 2460 iter->leftover = 0;
bc0c38d1
SR
2461 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2462 ;
2463
2464 } else {
a63ce5b3
SR
2465 /*
2466 * If we overflowed the seq_file before, then we want
2467 * to just reuse the trace_seq buffer again.
2468 */
2469 if (iter->leftover)
2470 p = iter;
2471 else {
2472 l = *pos - 1;
2473 p = s_next(m, p, &l);
2474 }
bc0c38d1
SR
2475 }
2476
4f535968 2477 trace_event_read_lock();
7e53bd42 2478 trace_access_lock(cpu_file);
bc0c38d1
SR
2479 return p;
2480}
2481
2482static void s_stop(struct seq_file *m, void *p)
2483{
7e53bd42
LJ
2484 struct trace_iterator *iter = m->private;
2485
12883efb 2486#ifdef CONFIG_TRACER_MAX_TRACE
debdd57f
HT
2487 if (iter->snapshot && iter->trace->use_max_tr)
2488 return;
12883efb 2489#endif
debdd57f
HT
2490
2491 if (!iter->snapshot)
2492 atomic_dec(&trace_record_cmdline_disabled);
12883efb 2493
7e53bd42 2494 trace_access_unlock(iter->cpu_file);
4f535968 2495 trace_event_read_unlock();
bc0c38d1
SR
2496}
2497
39eaf7ef 2498static void
12883efb
SRRH
2499get_total_entries(struct trace_buffer *buf,
2500 unsigned long *total, unsigned long *entries)
39eaf7ef
SR
2501{
2502 unsigned long count;
2503 int cpu;
2504
2505 *total = 0;
2506 *entries = 0;
2507
2508 for_each_tracing_cpu(cpu) {
12883efb 2509 count = ring_buffer_entries_cpu(buf->buffer, cpu);
39eaf7ef
SR
2510 /*
2511 * If this buffer has skipped entries, then we hold all
2512 * entries for the trace and we need to ignore the
2513 * ones before the time stamp.
2514 */
12883efb
SRRH
2515 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2516 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
39eaf7ef
SR
2517 /* total is the same as the entries */
2518 *total += count;
2519 } else
2520 *total += count +
12883efb 2521 ring_buffer_overrun_cpu(buf->buffer, cpu);
39eaf7ef
SR
2522 *entries += count;
2523 }
2524}
2525
e309b41d 2526static void print_lat_help_header(struct seq_file *m)
bc0c38d1 2527{
a6168353
ME
2528 seq_puts(m, "# _------=> CPU# \n");
2529 seq_puts(m, "# / _-----=> irqs-off \n");
2530 seq_puts(m, "# | / _----=> need-resched \n");
2531 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2532 seq_puts(m, "# ||| / _--=> preempt-depth \n");
e6e1e259
SR
2533 seq_puts(m, "# |||| / delay \n");
2534 seq_puts(m, "# cmd pid ||||| time | caller \n");
2535 seq_puts(m, "# \\ / ||||| \\ | / \n");
bc0c38d1
SR
2536}
2537
12883efb 2538static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
bc0c38d1 2539{
39eaf7ef
SR
2540 unsigned long total;
2541 unsigned long entries;
2542
12883efb 2543 get_total_entries(buf, &total, &entries);
39eaf7ef
SR
2544 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2545 entries, total, num_online_cpus());
2546 seq_puts(m, "#\n");
2547}
2548
12883efb 2549static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
39eaf7ef 2550{
12883efb 2551 print_event_info(buf, m);
77271ce4 2552 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
a6168353 2553 seq_puts(m, "# | | | | |\n");
bc0c38d1
SR
2554}
2555
12883efb 2556static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
77271ce4 2557{
12883efb 2558 print_event_info(buf, m);
77271ce4
SR
2559 seq_puts(m, "# _-----=> irqs-off\n");
2560 seq_puts(m, "# / _----=> need-resched\n");
2561 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2562 seq_puts(m, "# || / _--=> preempt-depth\n");
2563 seq_puts(m, "# ||| / delay\n");
2564 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2565 seq_puts(m, "# | | | |||| | |\n");
2566}
bc0c38d1 2567
62b915f1 2568void
bc0c38d1
SR
2569print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2570{
2571 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
12883efb
SRRH
2572 struct trace_buffer *buf = iter->trace_buffer;
2573 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2b6080f2 2574 struct tracer *type = iter->trace;
39eaf7ef
SR
2575 unsigned long entries;
2576 unsigned long total;
bc0c38d1
SR
2577 const char *name = "preemption";
2578
d840f718 2579 name = type->name;
bc0c38d1 2580
12883efb 2581 get_total_entries(buf, &total, &entries);
bc0c38d1 2582
888b55dc 2583 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
bc0c38d1 2584 name, UTS_RELEASE);
888b55dc 2585 seq_puts(m, "# -----------------------------------"
bc0c38d1 2586 "---------------------------------\n");
888b55dc 2587 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
bc0c38d1 2588 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
57f50be1 2589 nsecs_to_usecs(data->saved_latency),
bc0c38d1 2590 entries,
4c11d7ae 2591 total,
12883efb 2592 buf->cpu,
bc0c38d1
SR
2593#if defined(CONFIG_PREEMPT_NONE)
2594 "server",
2595#elif defined(CONFIG_PREEMPT_VOLUNTARY)
2596 "desktop",
b5c21b45 2597#elif defined(CONFIG_PREEMPT)
bc0c38d1
SR
2598 "preempt",
2599#else
2600 "unknown",
2601#endif
2602 /* These are reserved for later use */
2603 0, 0, 0, 0);
2604#ifdef CONFIG_SMP
2605 seq_printf(m, " #P:%d)\n", num_online_cpus());
2606#else
2607 seq_puts(m, ")\n");
2608#endif
888b55dc
KM
2609 seq_puts(m, "# -----------------\n");
2610 seq_printf(m, "# | task: %.16s-%d "
bc0c38d1 2611 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
d20b92ab
EB
2612 data->comm, data->pid,
2613 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
bc0c38d1 2614 data->policy, data->rt_priority);
888b55dc 2615 seq_puts(m, "# -----------------\n");
bc0c38d1
SR
2616
2617 if (data->critical_start) {
888b55dc 2618 seq_puts(m, "# => started at: ");
214023c3
SR
2619 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2620 trace_print_seq(m, &iter->seq);
888b55dc 2621 seq_puts(m, "\n# => ended at: ");
214023c3
SR
2622 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2623 trace_print_seq(m, &iter->seq);
8248ac05 2624 seq_puts(m, "\n#\n");
bc0c38d1
SR
2625 }
2626
888b55dc 2627 seq_puts(m, "#\n");
bc0c38d1
SR
2628}
2629
a309720c
SR
2630static void test_cpu_buff_start(struct trace_iterator *iter)
2631{
2632 struct trace_seq *s = &iter->seq;
2633
12ef7d44
SR
2634 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2635 return;
2636
2637 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2638 return;
2639
4462344e 2640 if (cpumask_test_cpu(iter->cpu, iter->started))
a309720c
SR
2641 return;
2642
12883efb 2643 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2f26ebd5
SR
2644 return;
2645
4462344e 2646 cpumask_set_cpu(iter->cpu, iter->started);
b0dfa978
FW
2647
2648 /* Don't print started cpu buffer for the first entry of the trace */
2649 if (iter->idx > 1)
2650 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2651 iter->cpu);
a309720c
SR
2652}
2653
2c4f035f 2654static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 2655{
214023c3 2656 struct trace_seq *s = &iter->seq;
bc0c38d1 2657 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 2658 struct trace_entry *entry;
f633cef0 2659 struct trace_event *event;
bc0c38d1 2660
4e3c3333 2661 entry = iter->ent;
dd0e545f 2662
a309720c
SR
2663 test_cpu_buff_start(iter);
2664
c4a8e8be 2665 event = ftrace_find_event(entry->type);
bc0c38d1 2666
c4a8e8be 2667 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
27d48be8
SR
2668 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2669 if (!trace_print_lat_context(iter))
2670 goto partial;
2671 } else {
2672 if (!trace_print_context(iter))
2673 goto partial;
2674 }
c4a8e8be 2675 }
bc0c38d1 2676
268ccda0 2677 if (event)
a9a57763 2678 return event->funcs->trace(iter, sym_flags, event);
d9793bd8
ACM
2679
2680 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2681 goto partial;
02b67518 2682
2c4f035f 2683 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
2684partial:
2685 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1
SR
2686}
2687
2c4f035f 2688static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
f9896bf3
IM
2689{
2690 struct trace_seq *s = &iter->seq;
2691 struct trace_entry *entry;
f633cef0 2692 struct trace_event *event;
f9896bf3
IM
2693
2694 entry = iter->ent;
dd0e545f 2695
c4a8e8be 2696 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
d9793bd8
ACM
2697 if (!trace_seq_printf(s, "%d %d %llu ",
2698 entry->pid, iter->cpu, iter->ts))
2699 goto partial;
c4a8e8be 2700 }
f9896bf3 2701
f633cef0 2702 event = ftrace_find_event(entry->type);
268ccda0 2703 if (event)
a9a57763 2704 return event->funcs->raw(iter, 0, event);
d9793bd8
ACM
2705
2706 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2707 goto partial;
777e208d 2708
2c4f035f 2709 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
2710partial:
2711 return TRACE_TYPE_PARTIAL_LINE;
f9896bf3
IM
2712}
2713
2c4f035f 2714static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
5e3ca0ec
IM
2715{
2716 struct trace_seq *s = &iter->seq;
2717 unsigned char newline = '\n';
2718 struct trace_entry *entry;
f633cef0 2719 struct trace_event *event;
5e3ca0ec
IM
2720
2721 entry = iter->ent;
dd0e545f 2722
c4a8e8be
FW
2723 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2724 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2725 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2726 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2727 }
5e3ca0ec 2728
f633cef0 2729 event = ftrace_find_event(entry->type);
268ccda0 2730 if (event) {
a9a57763 2731 enum print_line_t ret = event->funcs->hex(iter, 0, event);
d9793bd8
ACM
2732 if (ret != TRACE_TYPE_HANDLED)
2733 return ret;
2734 }
7104f300 2735
5e3ca0ec
IM
2736 SEQ_PUT_FIELD_RET(s, newline);
2737
2c4f035f 2738 return TRACE_TYPE_HANDLED;
5e3ca0ec
IM
2739}
2740
2c4f035f 2741static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
cb0f12aa
IM
2742{
2743 struct trace_seq *s = &iter->seq;
2744 struct trace_entry *entry;
f633cef0 2745 struct trace_event *event;
cb0f12aa
IM
2746
2747 entry = iter->ent;
dd0e545f 2748
c4a8e8be
FW
2749 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2750 SEQ_PUT_FIELD_RET(s, entry->pid);
1830b52d 2751 SEQ_PUT_FIELD_RET(s, iter->cpu);
c4a8e8be
FW
2752 SEQ_PUT_FIELD_RET(s, iter->ts);
2753 }
cb0f12aa 2754
f633cef0 2755 event = ftrace_find_event(entry->type);
a9a57763
SR
2756 return event ? event->funcs->binary(iter, 0, event) :
2757 TRACE_TYPE_HANDLED;
cb0f12aa
IM
2758}
2759
62b915f1 2760int trace_empty(struct trace_iterator *iter)
bc0c38d1 2761{
6d158a81 2762 struct ring_buffer_iter *buf_iter;
bc0c38d1
SR
2763 int cpu;
2764
9aba60fe 2765 /* If we are looking at one CPU buffer, only check that one */
ae3b5093 2766 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
9aba60fe 2767 cpu = iter->cpu_file;
6d158a81
SR
2768 buf_iter = trace_buffer_iter(iter, cpu);
2769 if (buf_iter) {
2770 if (!ring_buffer_iter_empty(buf_iter))
9aba60fe
SR
2771 return 0;
2772 } else {
12883efb 2773 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
9aba60fe
SR
2774 return 0;
2775 }
2776 return 1;
2777 }
2778
ab46428c 2779 for_each_tracing_cpu(cpu) {
6d158a81
SR
2780 buf_iter = trace_buffer_iter(iter, cpu);
2781 if (buf_iter) {
2782 if (!ring_buffer_iter_empty(buf_iter))
d769041f
SR
2783 return 0;
2784 } else {
12883efb 2785 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
d769041f
SR
2786 return 0;
2787 }
bc0c38d1 2788 }
d769041f 2789
797d3712 2790 return 1;
bc0c38d1
SR
2791}
2792
4f535968 2793/* Called with trace_event_read_lock() held. */
955b61e5 2794enum print_line_t print_trace_line(struct trace_iterator *iter)
f9896bf3 2795{
2c4f035f
FW
2796 enum print_line_t ret;
2797
ee5e51f5
JO
2798 if (iter->lost_events &&
2799 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2800 iter->cpu, iter->lost_events))
2801 return TRACE_TYPE_PARTIAL_LINE;
bc21b478 2802
2c4f035f
FW
2803 if (iter->trace && iter->trace->print_line) {
2804 ret = iter->trace->print_line(iter);
2805 if (ret != TRACE_TYPE_UNHANDLED)
2806 return ret;
2807 }
72829bc3 2808
09ae7234
SRRH
2809 if (iter->ent->type == TRACE_BPUTS &&
2810 trace_flags & TRACE_ITER_PRINTK &&
2811 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2812 return trace_print_bputs_msg_only(iter);
2813
48ead020
FW
2814 if (iter->ent->type == TRACE_BPRINT &&
2815 trace_flags & TRACE_ITER_PRINTK &&
2816 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 2817 return trace_print_bprintk_msg_only(iter);
48ead020 2818
66896a85
FW
2819 if (iter->ent->type == TRACE_PRINT &&
2820 trace_flags & TRACE_ITER_PRINTK &&
2821 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 2822 return trace_print_printk_msg_only(iter);
66896a85 2823
cb0f12aa
IM
2824 if (trace_flags & TRACE_ITER_BIN)
2825 return print_bin_fmt(iter);
2826
5e3ca0ec
IM
2827 if (trace_flags & TRACE_ITER_HEX)
2828 return print_hex_fmt(iter);
2829
f9896bf3
IM
2830 if (trace_flags & TRACE_ITER_RAW)
2831 return print_raw_fmt(iter);
2832
f9896bf3
IM
2833 return print_trace_fmt(iter);
2834}
2835
7e9a49ef
JO
2836void trace_latency_header(struct seq_file *m)
2837{
2838 struct trace_iterator *iter = m->private;
2839
2840 /* print nothing if the buffers are empty */
2841 if (trace_empty(iter))
2842 return;
2843
2844 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2845 print_trace_header(m, iter);
2846
2847 if (!(trace_flags & TRACE_ITER_VERBOSE))
2848 print_lat_help_header(m);
2849}
2850
62b915f1
JO
2851void trace_default_header(struct seq_file *m)
2852{
2853 struct trace_iterator *iter = m->private;
2854
f56e7f8e
JO
2855 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2856 return;
2857
62b915f1
JO
2858 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2859 /* print nothing if the buffers are empty */
2860 if (trace_empty(iter))
2861 return;
2862 print_trace_header(m, iter);
2863 if (!(trace_flags & TRACE_ITER_VERBOSE))
2864 print_lat_help_header(m);
2865 } else {
77271ce4
SR
2866 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2867 if (trace_flags & TRACE_ITER_IRQ_INFO)
12883efb 2868 print_func_help_header_irq(iter->trace_buffer, m);
77271ce4 2869 else
12883efb 2870 print_func_help_header(iter->trace_buffer, m);
77271ce4 2871 }
62b915f1
JO
2872 }
2873}
2874
e0a413f6
SR
2875static void test_ftrace_alive(struct seq_file *m)
2876{
2877 if (!ftrace_is_dead())
2878 return;
2879 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2880 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2881}
2882
d8741e2e 2883#ifdef CONFIG_TRACER_MAX_TRACE
f1affcaa 2884static void show_snapshot_main_help(struct seq_file *m)
d8741e2e 2885{
d8741e2e
SRRH
2886 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2887 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2888 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
b9be6d02 2889 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n");
d8741e2e
SRRH
2890 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2891 seq_printf(m, "# is not a '0' or '1')\n");
2892}
f1affcaa
SRRH
2893
2894static void show_snapshot_percpu_help(struct seq_file *m)
2895{
2896 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2897#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2898 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2899 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2900#else
2901 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2902 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2903#endif
2904 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2905 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2906 seq_printf(m, "# is not a '0' or '1')\n");
2907}
2908
d8741e2e
SRRH
2909static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2910{
45ad21ca 2911 if (iter->tr->allocated_snapshot)
d8741e2e
SRRH
2912 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2913 else
2914 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2915
2916 seq_printf(m, "# Snapshot commands:\n");
f1affcaa
SRRH
2917 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2918 show_snapshot_main_help(m);
2919 else
2920 show_snapshot_percpu_help(m);
d8741e2e
SRRH
2921}
2922#else
2923/* Should never be called */
2924static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2925#endif
2926
bc0c38d1
SR
2927static int s_show(struct seq_file *m, void *v)
2928{
2929 struct trace_iterator *iter = v;
a63ce5b3 2930 int ret;
bc0c38d1
SR
2931
2932 if (iter->ent == NULL) {
2933 if (iter->tr) {
2934 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2935 seq_puts(m, "#\n");
e0a413f6 2936 test_ftrace_alive(m);
bc0c38d1 2937 }
d8741e2e
SRRH
2938 if (iter->snapshot && trace_empty(iter))
2939 print_snapshot_help(m, iter);
2940 else if (iter->trace && iter->trace->print_header)
8bba1bf5 2941 iter->trace->print_header(m);
62b915f1
JO
2942 else
2943 trace_default_header(m);
2944
a63ce5b3
SR
2945 } else if (iter->leftover) {
2946 /*
2947 * If we filled the seq_file buffer earlier, we
2948 * want to just show it now.
2949 */
2950 ret = trace_print_seq(m, &iter->seq);
2951
2952 /* ret should this time be zero, but you never know */
2953 iter->leftover = ret;
2954
bc0c38d1 2955 } else {
f9896bf3 2956 print_trace_line(iter);
a63ce5b3
SR
2957 ret = trace_print_seq(m, &iter->seq);
2958 /*
2959 * If we overflow the seq_file buffer, then it will
2960 * ask us for this data again at start up.
2961 * Use that instead.
2962 * ret is 0 if seq_file write succeeded.
2963 * -1 otherwise.
2964 */
2965 iter->leftover = ret;
bc0c38d1
SR
2966 }
2967
2968 return 0;
2969}
2970
649e9c70
ON
2971/*
2972 * Should be used after trace_array_get(), trace_types_lock
2973 * ensures that i_cdev was already initialized.
2974 */
2975static inline int tracing_get_cpu(struct inode *inode)
2976{
2977 if (inode->i_cdev) /* See trace_create_cpu_file() */
2978 return (long)inode->i_cdev - 1;
2979 return RING_BUFFER_ALL_CPUS;
2980}
2981
88e9d34c 2982static const struct seq_operations tracer_seq_ops = {
4bf39a94
IM
2983 .start = s_start,
2984 .next = s_next,
2985 .stop = s_stop,
2986 .show = s_show,
bc0c38d1
SR
2987};
2988
e309b41d 2989static struct trace_iterator *
6484c71c 2990__tracing_open(struct inode *inode, struct file *file, bool snapshot)
bc0c38d1 2991{
6484c71c 2992 struct trace_array *tr = inode->i_private;
bc0c38d1 2993 struct trace_iterator *iter;
50e18b94 2994 int cpu;
bc0c38d1 2995
85a2f9b4
SR
2996 if (tracing_disabled)
2997 return ERR_PTR(-ENODEV);
60a11774 2998
50e18b94 2999 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
85a2f9b4
SR
3000 if (!iter)
3001 return ERR_PTR(-ENOMEM);
bc0c38d1 3002
6d158a81
SR
3003 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
3004 GFP_KERNEL);
93574fcc
DC
3005 if (!iter->buffer_iter)
3006 goto release;
3007
d7350c3f
FW
3008 /*
3009 * We make a copy of the current tracer to avoid concurrent
3010 * changes on it while we are reading.
3011 */
bc0c38d1 3012 mutex_lock(&trace_types_lock);
d7350c3f 3013 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
85a2f9b4 3014 if (!iter->trace)
d7350c3f 3015 goto fail;
85a2f9b4 3016
2b6080f2 3017 *iter->trace = *tr->current_trace;
d7350c3f 3018
79f55997 3019 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
b0dfa978
FW
3020 goto fail;
3021
12883efb
SRRH
3022 iter->tr = tr;
3023
3024#ifdef CONFIG_TRACER_MAX_TRACE
2b6080f2
SR
3025 /* Currently only the top directory has a snapshot */
3026 if (tr->current_trace->print_max || snapshot)
12883efb 3027 iter->trace_buffer = &tr->max_buffer;
bc0c38d1 3028 else
12883efb
SRRH
3029#endif
3030 iter->trace_buffer = &tr->trace_buffer;
debdd57f 3031 iter->snapshot = snapshot;
bc0c38d1 3032 iter->pos = -1;
6484c71c 3033 iter->cpu_file = tracing_get_cpu(inode);
d7350c3f 3034 mutex_init(&iter->mutex);
bc0c38d1 3035
8bba1bf5
MM
3036 /* Notify the tracer early; before we stop tracing. */
3037 if (iter->trace && iter->trace->open)
a93751ca 3038 iter->trace->open(iter);
8bba1bf5 3039
12ef7d44 3040 /* Annotate start of buffers if we had overruns */
12883efb 3041 if (ring_buffer_overruns(iter->trace_buffer->buffer))
12ef7d44
SR
3042 iter->iter_flags |= TRACE_FILE_ANNOTATE;
3043
8be0709f 3044 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
58e8eedf 3045 if (trace_clocks[tr->clock_id].in_ns)
8be0709f
DS
3046 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3047
debdd57f
HT
3048 /* stop the trace while dumping if we are not opening "snapshot" */
3049 if (!iter->snapshot)
2b6080f2 3050 tracing_stop_tr(tr);
2f26ebd5 3051
ae3b5093 3052 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
b04cc6b1 3053 for_each_tracing_cpu(cpu) {
b04cc6b1 3054 iter->buffer_iter[cpu] =
12883efb 3055 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
72c9ddfd
DM
3056 }
3057 ring_buffer_read_prepare_sync();
3058 for_each_tracing_cpu(cpu) {
3059 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 3060 tracing_iter_reset(iter, cpu);
b04cc6b1
FW
3061 }
3062 } else {
3063 cpu = iter->cpu_file;
3928a8a2 3064 iter->buffer_iter[cpu] =
12883efb 3065 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
72c9ddfd
DM
3066 ring_buffer_read_prepare_sync();
3067 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 3068 tracing_iter_reset(iter, cpu);
3928a8a2
SR
3069 }
3070
bc0c38d1
SR
3071 mutex_unlock(&trace_types_lock);
3072
bc0c38d1 3073 return iter;
3928a8a2 3074
d7350c3f 3075 fail:
3928a8a2 3076 mutex_unlock(&trace_types_lock);
d7350c3f 3077 kfree(iter->trace);
6d158a81 3078 kfree(iter->buffer_iter);
93574fcc 3079release:
50e18b94
JO
3080 seq_release_private(inode, file);
3081 return ERR_PTR(-ENOMEM);
bc0c38d1
SR
3082}
3083
3084int tracing_open_generic(struct inode *inode, struct file *filp)
3085{
60a11774
SR
3086 if (tracing_disabled)
3087 return -ENODEV;
3088
bc0c38d1
SR
3089 filp->private_data = inode->i_private;
3090 return 0;
3091}
3092
2e86421d
GB
3093bool tracing_is_disabled(void)
3094{
3095 return (tracing_disabled) ? true: false;
3096}
3097
7b85af63
SRRH
3098/*
3099 * Open and update trace_array ref count.
3100 * Must have the current trace_array passed to it.
3101 */
dcc30223 3102static int tracing_open_generic_tr(struct inode *inode, struct file *filp)
7b85af63
SRRH
3103{
3104 struct trace_array *tr = inode->i_private;
3105
3106 if (tracing_disabled)
3107 return -ENODEV;
3108
3109 if (trace_array_get(tr) < 0)
3110 return -ENODEV;
3111
3112 filp->private_data = inode->i_private;
3113
3114 return 0;
7b85af63
SRRH
3115}
3116
4fd27358 3117static int tracing_release(struct inode *inode, struct file *file)
bc0c38d1 3118{
6484c71c 3119 struct trace_array *tr = inode->i_private;
907f2784 3120 struct seq_file *m = file->private_data;
4acd4d00 3121 struct trace_iterator *iter;
3928a8a2 3122 int cpu;
bc0c38d1 3123
ff451961 3124 if (!(file->f_mode & FMODE_READ)) {
6484c71c 3125 trace_array_put(tr);
4acd4d00 3126 return 0;
ff451961 3127 }
4acd4d00 3128
6484c71c 3129 /* Writes do not use seq_file */
4acd4d00 3130 iter = m->private;
bc0c38d1 3131 mutex_lock(&trace_types_lock);
a695cb58 3132
3928a8a2
SR
3133 for_each_tracing_cpu(cpu) {
3134 if (iter->buffer_iter[cpu])
3135 ring_buffer_read_finish(iter->buffer_iter[cpu]);
3136 }
3137
bc0c38d1
SR
3138 if (iter->trace && iter->trace->close)
3139 iter->trace->close(iter);
3140
debdd57f
HT
3141 if (!iter->snapshot)
3142 /* reenable tracing if it was previously enabled */
2b6080f2 3143 tracing_start_tr(tr);
f77d09a3
AL
3144
3145 __trace_array_put(tr);
3146
bc0c38d1
SR
3147 mutex_unlock(&trace_types_lock);
3148
d7350c3f 3149 mutex_destroy(&iter->mutex);
b0dfa978 3150 free_cpumask_var(iter->started);
d7350c3f 3151 kfree(iter->trace);
6d158a81 3152 kfree(iter->buffer_iter);
50e18b94 3153 seq_release_private(inode, file);
ff451961 3154
bc0c38d1
SR
3155 return 0;
3156}
3157
7b85af63
SRRH
3158static int tracing_release_generic_tr(struct inode *inode, struct file *file)
3159{
3160 struct trace_array *tr = inode->i_private;
3161
3162 trace_array_put(tr);
bc0c38d1
SR
3163 return 0;
3164}
3165
7b85af63
SRRH
3166static int tracing_single_release_tr(struct inode *inode, struct file *file)
3167{
3168 struct trace_array *tr = inode->i_private;
3169
3170 trace_array_put(tr);
3171
3172 return single_release(inode, file);
3173}
3174
bc0c38d1
SR
3175static int tracing_open(struct inode *inode, struct file *file)
3176{
6484c71c 3177 struct trace_array *tr = inode->i_private;
85a2f9b4
SR
3178 struct trace_iterator *iter;
3179 int ret = 0;
bc0c38d1 3180
ff451961
SRRH
3181 if (trace_array_get(tr) < 0)
3182 return -ENODEV;
3183
4acd4d00 3184 /* If this file was open for write, then erase contents */
6484c71c
ON
3185 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
3186 int cpu = tracing_get_cpu(inode);
3187
3188 if (cpu == RING_BUFFER_ALL_CPUS)
12883efb 3189 tracing_reset_online_cpus(&tr->trace_buffer);
4acd4d00 3190 else
6484c71c 3191 tracing_reset(&tr->trace_buffer, cpu);
4acd4d00 3192 }
bc0c38d1 3193
4acd4d00 3194 if (file->f_mode & FMODE_READ) {
6484c71c 3195 iter = __tracing_open(inode, file, false);
4acd4d00
SR
3196 if (IS_ERR(iter))
3197 ret = PTR_ERR(iter);
3198 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
3199 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3200 }
ff451961
SRRH
3201
3202 if (ret < 0)
3203 trace_array_put(tr);
3204
bc0c38d1
SR
3205 return ret;
3206}
3207
607e2ea1
SRRH
3208/*
3209 * Some tracers are not suitable for instance buffers.
3210 * A tracer is always available for the global array (toplevel)
3211 * or if it explicitly states that it is.
3212 */
3213static bool
3214trace_ok_for_array(struct tracer *t, struct trace_array *tr)
3215{
3216 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
3217}
3218
3219/* Find the next tracer that this trace array may use */
3220static struct tracer *
3221get_tracer_for_array(struct trace_array *tr, struct tracer *t)
3222{
3223 while (t && !trace_ok_for_array(t, tr))
3224 t = t->next;
3225
3226 return t;
3227}
3228
e309b41d 3229static void *
bc0c38d1
SR
3230t_next(struct seq_file *m, void *v, loff_t *pos)
3231{
607e2ea1 3232 struct trace_array *tr = m->private;
f129e965 3233 struct tracer *t = v;
bc0c38d1
SR
3234
3235 (*pos)++;
3236
3237 if (t)
607e2ea1 3238 t = get_tracer_for_array(tr, t->next);
bc0c38d1 3239
bc0c38d1
SR
3240 return t;
3241}
3242
3243static void *t_start(struct seq_file *m, loff_t *pos)
3244{
607e2ea1 3245 struct trace_array *tr = m->private;
f129e965 3246 struct tracer *t;
bc0c38d1
SR
3247 loff_t l = 0;
3248
3249 mutex_lock(&trace_types_lock);
607e2ea1
SRRH
3250
3251 t = get_tracer_for_array(tr, trace_types);
3252 for (; t && l < *pos; t = t_next(m, t, &l))
3253 ;
bc0c38d1
SR
3254
3255 return t;
3256}
3257
3258static void t_stop(struct seq_file *m, void *p)
3259{
3260 mutex_unlock(&trace_types_lock);
3261}
3262
3263static int t_show(struct seq_file *m, void *v)
3264{
3265 struct tracer *t = v;
3266
3267 if (!t)
3268 return 0;
3269
3270 seq_printf(m, "%s", t->name);
3271 if (t->next)
3272 seq_putc(m, ' ');
3273 else
3274 seq_putc(m, '\n');
3275
3276 return 0;
3277}
3278
88e9d34c 3279static const struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
3280 .start = t_start,
3281 .next = t_next,
3282 .stop = t_stop,
3283 .show = t_show,
bc0c38d1
SR
3284};
3285
3286static int show_traces_open(struct inode *inode, struct file *file)
3287{
607e2ea1
SRRH
3288 struct trace_array *tr = inode->i_private;
3289 struct seq_file *m;
3290 int ret;
3291
60a11774
SR
3292 if (tracing_disabled)
3293 return -ENODEV;
3294
607e2ea1
SRRH
3295 ret = seq_open(file, &show_traces_seq_ops);
3296 if (ret)
3297 return ret;
3298
3299 m = file->private_data;
3300 m->private = tr;
3301
3302 return 0;
bc0c38d1
SR
3303}
3304
4acd4d00
SR
3305static ssize_t
3306tracing_write_stub(struct file *filp, const char __user *ubuf,
3307 size_t count, loff_t *ppos)
3308{
3309 return count;
3310}
3311
098c879e 3312loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
364829b1 3313{
098c879e
SRRH
3314 int ret;
3315
364829b1 3316 if (file->f_mode & FMODE_READ)
098c879e 3317 ret = seq_lseek(file, offset, whence);
364829b1 3318 else
098c879e
SRRH
3319 file->f_pos = ret = 0;
3320
3321 return ret;
364829b1
SP
3322}
3323
5e2336a0 3324static const struct file_operations tracing_fops = {
4bf39a94
IM
3325 .open = tracing_open,
3326 .read = seq_read,
4acd4d00 3327 .write = tracing_write_stub,
098c879e 3328 .llseek = tracing_lseek,
4bf39a94 3329 .release = tracing_release,
bc0c38d1
SR
3330};
3331
5e2336a0 3332static const struct file_operations show_traces_fops = {
c7078de1
IM
3333 .open = show_traces_open,
3334 .read = seq_read,
3335 .release = seq_release,
b444786f 3336 .llseek = seq_lseek,
c7078de1
IM
3337};
3338
36dfe925
IM
3339/*
3340 * The tracer itself will not take this lock, but still we want
3341 * to provide a consistent cpumask to user-space:
3342 */
3343static DEFINE_MUTEX(tracing_cpumask_update_lock);
3344
3345/*
3346 * Temporary storage for the character representation of the
3347 * CPU bitmask (and one more byte for the newline):
3348 */
3349static char mask_str[NR_CPUS + 1];
3350
c7078de1
IM
3351static ssize_t
3352tracing_cpumask_read(struct file *filp, char __user *ubuf,
3353 size_t count, loff_t *ppos)
3354{
ccfe9e42 3355 struct trace_array *tr = file_inode(filp)->i_private;
36dfe925 3356 int len;
c7078de1
IM
3357
3358 mutex_lock(&tracing_cpumask_update_lock);
36dfe925 3359
ccfe9e42 3360 len = cpumask_scnprintf(mask_str, count, tr->tracing_cpumask);
36dfe925
IM
3361 if (count - len < 2) {
3362 count = -EINVAL;
3363 goto out_err;
3364 }
3365 len += sprintf(mask_str + len, "\n");
3366 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3367
3368out_err:
c7078de1
IM
3369 mutex_unlock(&tracing_cpumask_update_lock);
3370
3371 return count;
3372}
3373
3374static ssize_t
3375tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3376 size_t count, loff_t *ppos)
3377{
ccfe9e42 3378 struct trace_array *tr = file_inode(filp)->i_private;
9e01c1b7 3379 cpumask_var_t tracing_cpumask_new;
2b6080f2 3380 int err, cpu;
9e01c1b7
RR
3381
3382 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3383 return -ENOMEM;
c7078de1 3384
9e01c1b7 3385 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
c7078de1 3386 if (err)
36dfe925
IM
3387 goto err_unlock;
3388
215368e8
LZ
3389 mutex_lock(&tracing_cpumask_update_lock);
3390
a5e25883 3391 local_irq_disable();
0b9b12c1 3392 arch_spin_lock(&tr->max_lock);
ab46428c 3393 for_each_tracing_cpu(cpu) {
36dfe925
IM
3394 /*
3395 * Increase/decrease the disabled counter if we are
3396 * about to flip a bit in the cpumask:
3397 */
ccfe9e42 3398 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
9e01c1b7 3399 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
12883efb
SRRH
3400 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3401 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
36dfe925 3402 }
ccfe9e42 3403 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
9e01c1b7 3404 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
12883efb
SRRH
3405 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3406 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
36dfe925
IM
3407 }
3408 }
0b9b12c1 3409 arch_spin_unlock(&tr->max_lock);
a5e25883 3410 local_irq_enable();
36dfe925 3411
ccfe9e42 3412 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
36dfe925
IM
3413
3414 mutex_unlock(&tracing_cpumask_update_lock);
9e01c1b7 3415 free_cpumask_var(tracing_cpumask_new);
c7078de1
IM
3416
3417 return count;
36dfe925
IM
3418
3419err_unlock:
215368e8 3420 free_cpumask_var(tracing_cpumask_new);
36dfe925
IM
3421
3422 return err;
c7078de1
IM
3423}
3424
5e2336a0 3425static const struct file_operations tracing_cpumask_fops = {
ccfe9e42 3426 .open = tracing_open_generic_tr,
c7078de1
IM
3427 .read = tracing_cpumask_read,
3428 .write = tracing_cpumask_write,
ccfe9e42 3429 .release = tracing_release_generic_tr,
b444786f 3430 .llseek = generic_file_llseek,
bc0c38d1
SR
3431};
3432
fdb372ed 3433static int tracing_trace_options_show(struct seq_file *m, void *v)
bc0c38d1 3434{
d8e83d26 3435 struct tracer_opt *trace_opts;
2b6080f2 3436 struct trace_array *tr = m->private;
d8e83d26 3437 u32 tracer_flags;
d8e83d26 3438 int i;
adf9f195 3439
d8e83d26 3440 mutex_lock(&trace_types_lock);
2b6080f2
SR
3441 tracer_flags = tr->current_trace->flags->val;
3442 trace_opts = tr->current_trace->flags->opts;
d8e83d26 3443
bc0c38d1
SR
3444 for (i = 0; trace_options[i]; i++) {
3445 if (trace_flags & (1 << i))
fdb372ed 3446 seq_printf(m, "%s\n", trace_options[i]);
bc0c38d1 3447 else
fdb372ed 3448 seq_printf(m, "no%s\n", trace_options[i]);
bc0c38d1
SR
3449 }
3450
adf9f195
FW
3451 for (i = 0; trace_opts[i].name; i++) {
3452 if (tracer_flags & trace_opts[i].bit)
fdb372ed 3453 seq_printf(m, "%s\n", trace_opts[i].name);
adf9f195 3454 else
fdb372ed 3455 seq_printf(m, "no%s\n", trace_opts[i].name);
adf9f195 3456 }
d8e83d26 3457 mutex_unlock(&trace_types_lock);
adf9f195 3458
fdb372ed 3459 return 0;
bc0c38d1 3460}
bc0c38d1 3461
8c1a49ae 3462static int __set_tracer_option(struct trace_array *tr,
8d18eaaf
LZ
3463 struct tracer_flags *tracer_flags,
3464 struct tracer_opt *opts, int neg)
3465{
8c1a49ae 3466 struct tracer *trace = tr->current_trace;
8d18eaaf 3467 int ret;
bc0c38d1 3468
8c1a49ae 3469 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
8d18eaaf
LZ
3470 if (ret)
3471 return ret;
3472
3473 if (neg)
3474 tracer_flags->val &= ~opts->bit;
3475 else
3476 tracer_flags->val |= opts->bit;
3477 return 0;
bc0c38d1
SR
3478}
3479
adf9f195 3480/* Try to assign a tracer specific option */
8c1a49ae 3481static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
adf9f195 3482{
8c1a49ae 3483 struct tracer *trace = tr->current_trace;
7770841e 3484 struct tracer_flags *tracer_flags = trace->flags;
adf9f195 3485 struct tracer_opt *opts = NULL;
8d18eaaf 3486 int i;
adf9f195 3487
7770841e
Z
3488 for (i = 0; tracer_flags->opts[i].name; i++) {
3489 opts = &tracer_flags->opts[i];
adf9f195 3490
8d18eaaf 3491 if (strcmp(cmp, opts->name) == 0)
8c1a49ae 3492 return __set_tracer_option(tr, trace->flags, opts, neg);
adf9f195 3493 }
adf9f195 3494
8d18eaaf 3495 return -EINVAL;
adf9f195
FW
3496}
3497
613f04a0
SRRH
3498/* Some tracers require overwrite to stay enabled */
3499int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3500{
3501 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3502 return -1;
3503
3504 return 0;
3505}
3506
2b6080f2 3507int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
af4617bd
SR
3508{
3509 /* do nothing if flag is already set */
3510 if (!!(trace_flags & mask) == !!enabled)
613f04a0
SRRH
3511 return 0;
3512
3513 /* Give the tracer a chance to approve the change */
2b6080f2 3514 if (tr->current_trace->flag_changed)
bf6065b5 3515 if (tr->current_trace->flag_changed(tr, mask, !!enabled))
613f04a0 3516 return -EINVAL;
af4617bd
SR
3517
3518 if (enabled)
3519 trace_flags |= mask;
3520 else
3521 trace_flags &= ~mask;
e870e9a1
LZ
3522
3523 if (mask == TRACE_ITER_RECORD_CMD)
3524 trace_event_enable_cmd_record(enabled);
750912fa 3525
80902822 3526 if (mask == TRACE_ITER_OVERWRITE) {
12883efb 3527 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
80902822 3528#ifdef CONFIG_TRACER_MAX_TRACE
12883efb 3529 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
80902822
SRRH
3530#endif
3531 }
81698831
SR
3532
3533 if (mask == TRACE_ITER_PRINTK)
3534 trace_printk_start_stop_comm(enabled);
613f04a0
SRRH
3535
3536 return 0;
af4617bd
SR
3537}
3538
2b6080f2 3539static int trace_set_options(struct trace_array *tr, char *option)
bc0c38d1 3540{
8d18eaaf 3541 char *cmp;
bc0c38d1 3542 int neg = 0;
613f04a0 3543 int ret = -ENODEV;
bc0c38d1
SR
3544 int i;
3545
7bcfaf54 3546 cmp = strstrip(option);
bc0c38d1 3547
8d18eaaf 3548 if (strncmp(cmp, "no", 2) == 0) {
bc0c38d1
SR
3549 neg = 1;
3550 cmp += 2;
3551 }
3552
69d34da2
SRRH
3553 mutex_lock(&trace_types_lock);
3554
bc0c38d1 3555 for (i = 0; trace_options[i]; i++) {
8d18eaaf 3556 if (strcmp(cmp, trace_options[i]) == 0) {
2b6080f2 3557 ret = set_tracer_flag(tr, 1 << i, !neg);
bc0c38d1
SR
3558 break;
3559 }
3560 }
adf9f195
FW
3561
3562 /* If no option could be set, test the specific tracer options */
69d34da2 3563 if (!trace_options[i])
8c1a49ae 3564 ret = set_tracer_option(tr, cmp, neg);
69d34da2
SRRH
3565
3566 mutex_unlock(&trace_types_lock);
bc0c38d1 3567
7bcfaf54
SR
3568 return ret;
3569}
3570
3571static ssize_t
3572tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3573 size_t cnt, loff_t *ppos)
3574{
2b6080f2
SR
3575 struct seq_file *m = filp->private_data;
3576 struct trace_array *tr = m->private;
7bcfaf54 3577 char buf[64];
613f04a0 3578 int ret;
7bcfaf54
SR
3579
3580 if (cnt >= sizeof(buf))
3581 return -EINVAL;
3582
3583 if (copy_from_user(&buf, ubuf, cnt))
3584 return -EFAULT;
3585
a8dd2176
SR
3586 buf[cnt] = 0;
3587
2b6080f2 3588 ret = trace_set_options(tr, buf);
613f04a0
SRRH
3589 if (ret < 0)
3590 return ret;
7bcfaf54 3591
cf8517cf 3592 *ppos += cnt;
bc0c38d1
SR
3593
3594 return cnt;
3595}
3596
fdb372ed
LZ
3597static int tracing_trace_options_open(struct inode *inode, struct file *file)
3598{
7b85af63 3599 struct trace_array *tr = inode->i_private;
f77d09a3 3600 int ret;
7b85af63 3601
fdb372ed
LZ
3602 if (tracing_disabled)
3603 return -ENODEV;
2b6080f2 3604
7b85af63
SRRH
3605 if (trace_array_get(tr) < 0)
3606 return -ENODEV;
3607
f77d09a3
AL
3608 ret = single_open(file, tracing_trace_options_show, inode->i_private);
3609 if (ret < 0)
3610 trace_array_put(tr);
3611
3612 return ret;
fdb372ed
LZ
3613}
3614
5e2336a0 3615static const struct file_operations tracing_iter_fops = {
fdb372ed
LZ
3616 .open = tracing_trace_options_open,
3617 .read = seq_read,
3618 .llseek = seq_lseek,
7b85af63 3619 .release = tracing_single_release_tr,
ee6bce52 3620 .write = tracing_trace_options_write,
bc0c38d1
SR
3621};
3622
7bd2f24c
IM
3623static const char readme_msg[] =
3624 "tracing mini-HOWTO:\n\n"
22f45649
SRRH
3625 "# echo 0 > tracing_on : quick way to disable tracing\n"
3626 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3627 " Important files:\n"
3628 " trace\t\t\t- The static contents of the buffer\n"
3629 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3630 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3631 " current_tracer\t- function and latency tracers\n"
3632 " available_tracers\t- list of configured tracers for current_tracer\n"
3633 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3634 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3635 " trace_clock\t\t-change the clock used to order events\n"
3636 " local: Per cpu clock but may not be synced across CPUs\n"
3637 " global: Synced across CPUs but slows tracing down.\n"
3638 " counter: Not a clock, but just an increment\n"
3639 " uptime: Jiffy counter from time of boot\n"
3640 " perf: Same clock that perf events use\n"
3641#ifdef CONFIG_X86_64
3642 " x86-tsc: TSC cycle counter\n"
3643#endif
3644 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3645 " tracing_cpumask\t- Limit which CPUs to trace\n"
3646 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3647 "\t\t\t Remove sub-buffer with rmdir\n"
3648 " trace_options\t\t- Set format or modify how tracing happens\n"
71485c45
SRRH
3649 "\t\t\t Disable an option by adding a suffix 'no' to the\n"
3650 "\t\t\t option name\n"
939c7a4f 3651 " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
22f45649
SRRH
3652#ifdef CONFIG_DYNAMIC_FTRACE
3653 "\n available_filter_functions - list of functions that can be filtered on\n"
71485c45
SRRH
3654 " set_ftrace_filter\t- echo function name in here to only trace these\n"
3655 "\t\t\t functions\n"
3656 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3657 "\t modules: Can select a group via module\n"
3658 "\t Format: :mod:<module-name>\n"
3659 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
3660 "\t triggers: a command to perform when function is hit\n"
3661 "\t Format: <function>:<trigger>[:count]\n"
3662 "\t trigger: traceon, traceoff\n"
3663 "\t\t enable_event:<system>:<event>\n"
3664 "\t\t disable_event:<system>:<event>\n"
22f45649 3665#ifdef CONFIG_STACKTRACE
71485c45 3666 "\t\t stacktrace\n"
22f45649
SRRH
3667#endif
3668#ifdef CONFIG_TRACER_SNAPSHOT
71485c45 3669 "\t\t snapshot\n"
22f45649 3670#endif
17a280ea
SRRH
3671 "\t\t dump\n"
3672 "\t\t cpudump\n"
71485c45
SRRH
3673 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
3674 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
3675 "\t The first one will disable tracing every time do_fault is hit\n"
3676 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
3677 "\t The first time do trap is hit and it disables tracing, the\n"
3678 "\t counter will decrement to 2. If tracing is already disabled,\n"
3679 "\t the counter will not decrement. It only decrements when the\n"
3680 "\t trigger did work\n"
3681 "\t To remove trigger without count:\n"
3682 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
3683 "\t To remove trigger with a count:\n"
3684 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
22f45649 3685 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
71485c45
SRRH
3686 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3687 "\t modules: Can select a group via module command :mod:\n"
3688 "\t Does not accept triggers\n"
22f45649
SRRH
3689#endif /* CONFIG_DYNAMIC_FTRACE */
3690#ifdef CONFIG_FUNCTION_TRACER
71485c45
SRRH
3691 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
3692 "\t\t (function)\n"
22f45649
SRRH
3693#endif
3694#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3695 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3696 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3697#endif
3698#ifdef CONFIG_TRACER_SNAPSHOT
71485c45
SRRH
3699 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
3700 "\t\t\t snapshot buffer. Read the contents for more\n"
3701 "\t\t\t information\n"
22f45649 3702#endif
991821c8 3703#ifdef CONFIG_STACK_TRACER
22f45649
SRRH
3704 " stack_trace\t\t- Shows the max stack trace when active\n"
3705 " stack_max_size\t- Shows current max stack size that was traced\n"
71485c45
SRRH
3706 "\t\t\t Write into this file to reset the max size (trigger a\n"
3707 "\t\t\t new trace)\n"
22f45649 3708#ifdef CONFIG_DYNAMIC_FTRACE
71485c45
SRRH
3709 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
3710 "\t\t\t traces\n"
22f45649 3711#endif
991821c8 3712#endif /* CONFIG_STACK_TRACER */
26f25564
TZ
3713 " events/\t\t- Directory containing all trace event subsystems:\n"
3714 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
3715 " events/<system>/\t- Directory containing all trace events for <system>:\n"
71485c45
SRRH
3716 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
3717 "\t\t\t events\n"
26f25564 3718 " filter\t\t- If set, only events passing filter are traced\n"
71485c45
SRRH
3719 " events/<system>/<event>/\t- Directory containing control files for\n"
3720 "\t\t\t <event>:\n"
26f25564
TZ
3721 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
3722 " filter\t\t- If set, only events passing filter are traced\n"
3723 " trigger\t\t- If set, a command to perform when event is hit\n"
71485c45
SRRH
3724 "\t Format: <trigger>[:count][if <filter>]\n"
3725 "\t trigger: traceon, traceoff\n"
3726 "\t enable_event:<system>:<event>\n"
3727 "\t disable_event:<system>:<event>\n"
26f25564 3728#ifdef CONFIG_STACKTRACE
71485c45 3729 "\t\t stacktrace\n"
26f25564
TZ
3730#endif
3731#ifdef CONFIG_TRACER_SNAPSHOT
71485c45 3732 "\t\t snapshot\n"
26f25564 3733#endif
71485c45
SRRH
3734 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
3735 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
3736 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
3737 "\t events/block/block_unplug/trigger\n"
3738 "\t The first disables tracing every time block_unplug is hit.\n"
3739 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
3740 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
3741 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
3742 "\t Like function triggers, the counter is only decremented if it\n"
3743 "\t enabled or disabled tracing.\n"
3744 "\t To remove a trigger without a count:\n"
3745 "\t echo '!<trigger> > <system>/<event>/trigger\n"
3746 "\t To remove a trigger with a count:\n"
3747 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
3748 "\t Filters can be ignored when removing a trigger.\n"
7bd2f24c
IM
3749;
3750
3751static ssize_t
3752tracing_readme_read(struct file *filp, char __user *ubuf,
3753 size_t cnt, loff_t *ppos)
3754{
3755 return simple_read_from_buffer(ubuf, cnt, ppos,
3756 readme_msg, strlen(readme_msg));
3757}
3758
5e2336a0 3759static const struct file_operations tracing_readme_fops = {
c7078de1
IM
3760 .open = tracing_open_generic,
3761 .read = tracing_readme_read,
b444786f 3762 .llseek = generic_file_llseek,
7bd2f24c
IM
3763};
3764
42584c81
YY
3765static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos)
3766{
3767 unsigned int *ptr = v;
69abe6a5 3768
42584c81
YY
3769 if (*pos || m->count)
3770 ptr++;
69abe6a5 3771
42584c81 3772 (*pos)++;
69abe6a5 3773
939c7a4f
YY
3774 for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num];
3775 ptr++) {
42584c81
YY
3776 if (*ptr == -1 || *ptr == NO_CMDLINE_MAP)
3777 continue;
69abe6a5 3778
42584c81
YY
3779 return ptr;
3780 }
69abe6a5 3781
42584c81
YY
3782 return NULL;
3783}
3784
3785static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos)
3786{
3787 void *v;
3788 loff_t l = 0;
69abe6a5 3789
4c27e756
SRRH
3790 preempt_disable();
3791 arch_spin_lock(&trace_cmdline_lock);
3792
939c7a4f 3793 v = &savedcmd->map_cmdline_to_pid[0];
42584c81
YY
3794 while (l <= *pos) {
3795 v = saved_cmdlines_next(m, v, &l);
3796 if (!v)
3797 return NULL;
69abe6a5
AP
3798 }
3799
42584c81
YY
3800 return v;
3801}
3802
3803static void saved_cmdlines_stop(struct seq_file *m, void *v)
3804{
4c27e756
SRRH
3805 arch_spin_unlock(&trace_cmdline_lock);
3806 preempt_enable();
42584c81 3807}
69abe6a5 3808
42584c81
YY
3809static int saved_cmdlines_show(struct seq_file *m, void *v)
3810{
3811 char buf[TASK_COMM_LEN];
3812 unsigned int *pid = v;
69abe6a5 3813
4c27e756 3814 __trace_find_cmdline(*pid, buf);
42584c81
YY
3815 seq_printf(m, "%d %s\n", *pid, buf);
3816 return 0;
3817}
3818
3819static const struct seq_operations tracing_saved_cmdlines_seq_ops = {
3820 .start = saved_cmdlines_start,
3821 .next = saved_cmdlines_next,
3822 .stop = saved_cmdlines_stop,
3823 .show = saved_cmdlines_show,
3824};
3825
3826static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
3827{
3828 if (tracing_disabled)
3829 return -ENODEV;
3830
3831 return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
69abe6a5
AP
3832}
3833
3834static const struct file_operations tracing_saved_cmdlines_fops = {
42584c81
YY
3835 .open = tracing_saved_cmdlines_open,
3836 .read = seq_read,
3837 .llseek = seq_lseek,
3838 .release = seq_release,
69abe6a5
AP
3839};
3840
939c7a4f
YY
3841static ssize_t
3842tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf,
3843 size_t cnt, loff_t *ppos)
3844{
3845 char buf[64];
3846 int r;
3847
3848 arch_spin_lock(&trace_cmdline_lock);
a6af8fbf 3849 r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
939c7a4f
YY
3850 arch_spin_unlock(&trace_cmdline_lock);
3851
3852 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3853}
3854
3855static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
3856{
3857 kfree(s->saved_cmdlines);
3858 kfree(s->map_cmdline_to_pid);
3859 kfree(s);
3860}
3861
3862static int tracing_resize_saved_cmdlines(unsigned int val)
3863{
3864 struct saved_cmdlines_buffer *s, *savedcmd_temp;
3865
a6af8fbf 3866 s = kmalloc(sizeof(*s), GFP_KERNEL);
939c7a4f
YY
3867 if (!s)
3868 return -ENOMEM;
3869
3870 if (allocate_cmdlines_buffer(val, s) < 0) {
3871 kfree(s);
3872 return -ENOMEM;
3873 }
3874
3875 arch_spin_lock(&trace_cmdline_lock);
3876 savedcmd_temp = savedcmd;
3877 savedcmd = s;
3878 arch_spin_unlock(&trace_cmdline_lock);
3879 free_saved_cmdlines_buffer(savedcmd_temp);
3880
3881 return 0;
3882}
3883
3884static ssize_t
3885tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf,
3886 size_t cnt, loff_t *ppos)
3887{
3888 unsigned long val;
3889 int ret;
3890
3891 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3892 if (ret)
3893 return ret;
3894
3895 /* must have at least 1 entry or less than PID_MAX_DEFAULT */
3896 if (!val || val > PID_MAX_DEFAULT)
3897 return -EINVAL;
3898
3899 ret = tracing_resize_saved_cmdlines((unsigned int)val);
3900 if (ret < 0)
3901 return ret;
3902
3903 *ppos += cnt;
3904
3905 return cnt;
3906}
3907
3908static const struct file_operations tracing_saved_cmdlines_size_fops = {
3909 .open = tracing_open_generic,
3910 .read = tracing_saved_cmdlines_size_read,
3911 .write = tracing_saved_cmdlines_size_write,
3912};
3913
bc0c38d1
SR
3914static ssize_t
3915tracing_set_trace_read(struct file *filp, char __user *ubuf,
3916 size_t cnt, loff_t *ppos)
3917{
2b6080f2 3918 struct trace_array *tr = filp->private_data;
ee6c2c1b 3919 char buf[MAX_TRACER_SIZE+2];
bc0c38d1
SR
3920 int r;
3921
3922 mutex_lock(&trace_types_lock);
2b6080f2 3923 r = sprintf(buf, "%s\n", tr->current_trace->name);
bc0c38d1
SR
3924 mutex_unlock(&trace_types_lock);
3925
4bf39a94 3926 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
3927}
3928
b6f11df2
ACM
3929int tracer_init(struct tracer *t, struct trace_array *tr)
3930{
12883efb 3931 tracing_reset_online_cpus(&tr->trace_buffer);
b6f11df2
ACM
3932 return t->init(tr);
3933}
3934
12883efb 3935static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
438ced17
VN
3936{
3937 int cpu;
737223fb 3938
438ced17 3939 for_each_tracing_cpu(cpu)
12883efb 3940 per_cpu_ptr(buf->data, cpu)->entries = val;
438ced17
VN
3941}
3942
12883efb 3943#ifdef CONFIG_TRACER_MAX_TRACE
d60da506 3944/* resize @tr's buffer to the size of @size_tr's entries */
12883efb
SRRH
3945static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3946 struct trace_buffer *size_buf, int cpu_id)
d60da506
HT
3947{
3948 int cpu, ret = 0;
3949
3950 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3951 for_each_tracing_cpu(cpu) {
12883efb
SRRH
3952 ret = ring_buffer_resize(trace_buf->buffer,
3953 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
d60da506
HT
3954 if (ret < 0)
3955 break;
12883efb
SRRH
3956 per_cpu_ptr(trace_buf->data, cpu)->entries =
3957 per_cpu_ptr(size_buf->data, cpu)->entries;
d60da506
HT
3958 }
3959 } else {
12883efb
SRRH
3960 ret = ring_buffer_resize(trace_buf->buffer,
3961 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
d60da506 3962 if (ret == 0)
12883efb
SRRH
3963 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3964 per_cpu_ptr(size_buf->data, cpu_id)->entries;
d60da506
HT
3965 }
3966
3967 return ret;
3968}
12883efb 3969#endif /* CONFIG_TRACER_MAX_TRACE */
d60da506 3970
2b6080f2
SR
3971static int __tracing_resize_ring_buffer(struct trace_array *tr,
3972 unsigned long size, int cpu)
73c5162a
SR
3973{
3974 int ret;
3975
3976 /*
3977 * If kernel or user changes the size of the ring buffer
a123c52b
SR
3978 * we use the size that was given, and we can forget about
3979 * expanding it later.
73c5162a 3980 */
55034cd6 3981 ring_buffer_expanded = true;
73c5162a 3982
b382ede6 3983 /* May be called before buffers are initialized */
12883efb 3984 if (!tr->trace_buffer.buffer)
b382ede6
SR
3985 return 0;
3986
12883efb 3987 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
73c5162a
SR
3988 if (ret < 0)
3989 return ret;
3990
12883efb 3991#ifdef CONFIG_TRACER_MAX_TRACE
2b6080f2
SR
3992 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3993 !tr->current_trace->use_max_tr)
ef710e10
KM
3994 goto out;
3995
12883efb 3996 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
73c5162a 3997 if (ret < 0) {
12883efb
SRRH
3998 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3999 &tr->trace_buffer, cpu);
73c5162a 4000 if (r < 0) {
a123c52b
SR
4001 /*
4002 * AARGH! We are left with different
4003 * size max buffer!!!!
4004 * The max buffer is our "snapshot" buffer.
4005 * When a tracer needs a snapshot (one of the
4006 * latency tracers), it swaps the max buffer
4007 * with the saved snap shot. We succeeded to
4008 * update the size of the main buffer, but failed to
4009 * update the size of the max buffer. But when we tried
4010 * to reset the main buffer to the original size, we
4011 * failed there too. This is very unlikely to
4012 * happen, but if it does, warn and kill all
4013 * tracing.
4014 */
73c5162a
SR
4015 WARN_ON(1);
4016 tracing_disabled = 1;
4017 }
4018 return ret;
4019 }
4020
438ced17 4021 if (cpu == RING_BUFFER_ALL_CPUS)
12883efb 4022 set_buffer_entries(&tr->max_buffer, size);
438ced17 4023 else
12883efb 4024 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
438ced17 4025
ef710e10 4026 out:
12883efb
SRRH
4027#endif /* CONFIG_TRACER_MAX_TRACE */
4028
438ced17 4029 if (cpu == RING_BUFFER_ALL_CPUS)
12883efb 4030 set_buffer_entries(&tr->trace_buffer, size);
438ced17 4031 else
12883efb 4032 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
73c5162a
SR
4033
4034 return ret;
4035}
4036
2b6080f2
SR
4037static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
4038 unsigned long size, int cpu_id)
4f271a2a 4039{
83f40318 4040 int ret = size;
4f271a2a
VN
4041
4042 mutex_lock(&trace_types_lock);
4043
438ced17
VN
4044 if (cpu_id != RING_BUFFER_ALL_CPUS) {
4045 /* make sure, this cpu is enabled in the mask */
4046 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
4047 ret = -EINVAL;
4048 goto out;
4049 }
4050 }
4f271a2a 4051
2b6080f2 4052 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
4f271a2a
VN
4053 if (ret < 0)
4054 ret = -ENOMEM;
4055
438ced17 4056out:
4f271a2a
VN
4057 mutex_unlock(&trace_types_lock);
4058
4059 return ret;
4060}
4061
ef710e10 4062
1852fcce
SR
4063/**
4064 * tracing_update_buffers - used by tracing facility to expand ring buffers
4065 *
4066 * To save on memory when the tracing is never used on a system with it
4067 * configured in. The ring buffers are set to a minimum size. But once
4068 * a user starts to use the tracing facility, then they need to grow
4069 * to their default size.
4070 *
4071 * This function is to be called when a tracer is about to be used.
4072 */
4073int tracing_update_buffers(void)
4074{
4075 int ret = 0;
4076
1027fcb2 4077 mutex_lock(&trace_types_lock);
1852fcce 4078 if (!ring_buffer_expanded)
2b6080f2 4079 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
438ced17 4080 RING_BUFFER_ALL_CPUS);
1027fcb2 4081 mutex_unlock(&trace_types_lock);
1852fcce
SR
4082
4083 return ret;
4084}
4085
577b785f
SR
4086struct trace_option_dentry;
4087
4088static struct trace_option_dentry *
2b6080f2 4089create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
577b785f
SR
4090
4091static void
4092destroy_trace_option_files(struct trace_option_dentry *topts);
4093
6b450d25
SRRH
4094/*
4095 * Used to clear out the tracer before deletion of an instance.
4096 * Must have trace_types_lock held.
4097 */
4098static void tracing_set_nop(struct trace_array *tr)
4099{
4100 if (tr->current_trace == &nop_trace)
4101 return;
4102
50512ab5 4103 tr->current_trace->enabled--;
6b450d25
SRRH
4104
4105 if (tr->current_trace->reset)
4106 tr->current_trace->reset(tr);
4107
4108 tr->current_trace = &nop_trace;
4109}
4110
607e2ea1 4111static int tracing_set_tracer(struct trace_array *tr, const char *buf)
bc0c38d1 4112{
577b785f 4113 static struct trace_option_dentry *topts;
bc0c38d1 4114 struct tracer *t;
12883efb 4115#ifdef CONFIG_TRACER_MAX_TRACE
34600f0e 4116 bool had_max_tr;
12883efb 4117#endif
d9e54076 4118 int ret = 0;
bc0c38d1 4119
1027fcb2
SR
4120 mutex_lock(&trace_types_lock);
4121
73c5162a 4122 if (!ring_buffer_expanded) {
2b6080f2 4123 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
438ced17 4124 RING_BUFFER_ALL_CPUS);
73c5162a 4125 if (ret < 0)
59f586db 4126 goto out;
73c5162a
SR
4127 ret = 0;
4128 }
4129
bc0c38d1
SR
4130 for (t = trace_types; t; t = t->next) {
4131 if (strcmp(t->name, buf) == 0)
4132 break;
4133 }
c2931e05
FW
4134 if (!t) {
4135 ret = -EINVAL;
4136 goto out;
4137 }
2b6080f2 4138 if (t == tr->current_trace)
bc0c38d1
SR
4139 goto out;
4140
607e2ea1
SRRH
4141 /* Some tracers are only allowed for the top level buffer */
4142 if (!trace_ok_for_array(t, tr)) {
4143 ret = -EINVAL;
4144 goto out;
4145 }
4146
9f029e83 4147 trace_branch_disable();
613f04a0 4148
50512ab5 4149 tr->current_trace->enabled--;
613f04a0 4150
2b6080f2
SR
4151 if (tr->current_trace->reset)
4152 tr->current_trace->reset(tr);
34600f0e 4153
12883efb 4154 /* Current trace needs to be nop_trace before synchronize_sched */
2b6080f2 4155 tr->current_trace = &nop_trace;
34600f0e 4156
45ad21ca
SRRH
4157#ifdef CONFIG_TRACER_MAX_TRACE
4158 had_max_tr = tr->allocated_snapshot;
34600f0e
SR
4159
4160 if (had_max_tr && !t->use_max_tr) {
4161 /*
4162 * We need to make sure that the update_max_tr sees that
4163 * current_trace changed to nop_trace to keep it from
4164 * swapping the buffers after we resize it.
4165 * The update_max_tr is called from interrupts disabled
4166 * so a synchronized_sched() is sufficient.
4167 */
4168 synchronize_sched();
3209cff4 4169 free_snapshot(tr);
ef710e10 4170 }
12883efb 4171#endif
f1b21c9a
SRRH
4172 /* Currently, only the top instance has options */
4173 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4174 destroy_trace_option_files(topts);
4175 topts = create_trace_option_files(tr, t);
4176 }
12883efb
SRRH
4177
4178#ifdef CONFIG_TRACER_MAX_TRACE
34600f0e 4179 if (t->use_max_tr && !had_max_tr) {
3209cff4 4180 ret = alloc_snapshot(tr);
d60da506
HT
4181 if (ret < 0)
4182 goto out;
ef710e10 4183 }
12883efb 4184#endif
577b785f 4185
1c80025a 4186 if (t->init) {
b6f11df2 4187 ret = tracer_init(t, tr);
1c80025a
FW
4188 if (ret)
4189 goto out;
4190 }
bc0c38d1 4191
2b6080f2 4192 tr->current_trace = t;
50512ab5 4193 tr->current_trace->enabled++;
9f029e83 4194 trace_branch_enable(tr);
bc0c38d1
SR
4195 out:
4196 mutex_unlock(&trace_types_lock);
4197
d9e54076
PZ
4198 return ret;
4199}
4200
4201static ssize_t
4202tracing_set_trace_write(struct file *filp, const char __user *ubuf,
4203 size_t cnt, loff_t *ppos)
4204{
607e2ea1 4205 struct trace_array *tr = filp->private_data;
ee6c2c1b 4206 char buf[MAX_TRACER_SIZE+1];
d9e54076
PZ
4207 int i;
4208 size_t ret;
e6e7a65a
FW
4209 int err;
4210
4211 ret = cnt;
d9e54076 4212
ee6c2c1b
LZ
4213 if (cnt > MAX_TRACER_SIZE)
4214 cnt = MAX_TRACER_SIZE;
d9e54076
PZ
4215
4216 if (copy_from_user(&buf, ubuf, cnt))
4217 return -EFAULT;
4218
4219 buf[cnt] = 0;
4220
4221 /* strip ending whitespace. */
4222 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
4223 buf[i] = 0;
4224
607e2ea1 4225 err = tracing_set_tracer(tr, buf);
e6e7a65a
FW
4226 if (err)
4227 return err;
d9e54076 4228
cf8517cf 4229 *ppos += ret;
bc0c38d1 4230
c2931e05 4231 return ret;
bc0c38d1
SR
4232}
4233
4234static ssize_t
4235tracing_max_lat_read(struct file *filp, char __user *ubuf,
4236 size_t cnt, loff_t *ppos)
4237{
4238 unsigned long *ptr = filp->private_data;
4239 char buf[64];
4240 int r;
4241
cffae437 4242 r = snprintf(buf, sizeof(buf), "%ld\n",
bc0c38d1 4243 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
cffae437
SR
4244 if (r > sizeof(buf))
4245 r = sizeof(buf);
4bf39a94 4246 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
4247}
4248
4249static ssize_t
4250tracing_max_lat_write(struct file *filp, const char __user *ubuf,
4251 size_t cnt, loff_t *ppos)
4252{
5e39841c 4253 unsigned long *ptr = filp->private_data;
5e39841c 4254 unsigned long val;
c6caeeb1 4255 int ret;
bc0c38d1 4256
22fe9b54
PH
4257 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4258 if (ret)
c6caeeb1 4259 return ret;
bc0c38d1
SR
4260
4261 *ptr = val * 1000;
4262
4263 return cnt;
4264}
4265
b3806b43
SR
4266static int tracing_open_pipe(struct inode *inode, struct file *filp)
4267{
15544209 4268 struct trace_array *tr = inode->i_private;
b3806b43 4269 struct trace_iterator *iter;
b04cc6b1 4270 int ret = 0;
b3806b43
SR
4271
4272 if (tracing_disabled)
4273 return -ENODEV;
4274
7b85af63
SRRH
4275 if (trace_array_get(tr) < 0)
4276 return -ENODEV;
4277
b04cc6b1
FW
4278 mutex_lock(&trace_types_lock);
4279
b3806b43
SR
4280 /* create a buffer to store the information to pass to userspace */
4281 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
b04cc6b1
FW
4282 if (!iter) {
4283 ret = -ENOMEM;
f77d09a3 4284 __trace_array_put(tr);
b04cc6b1
FW
4285 goto out;
4286 }
b3806b43 4287
d7350c3f
FW
4288 /*
4289 * We make a copy of the current tracer to avoid concurrent
4290 * changes on it while we are reading.
4291 */
4292 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
4293 if (!iter->trace) {
4294 ret = -ENOMEM;
4295 goto fail;
4296 }
2b6080f2 4297 *iter->trace = *tr->current_trace;
d7350c3f 4298
4462344e 4299 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
b04cc6b1 4300 ret = -ENOMEM;
d7350c3f 4301 goto fail;
4462344e
RR
4302 }
4303
a309720c 4304 /* trace pipe does not show start of buffer */
4462344e 4305 cpumask_setall(iter->started);
a309720c 4306
112f38a7
SR
4307 if (trace_flags & TRACE_ITER_LATENCY_FMT)
4308 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4309
8be0709f 4310 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
58e8eedf 4311 if (trace_clocks[tr->clock_id].in_ns)
8be0709f
DS
4312 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4313
15544209
ON
4314 iter->tr = tr;
4315 iter->trace_buffer = &tr->trace_buffer;
4316 iter->cpu_file = tracing_get_cpu(inode);
d7350c3f 4317 mutex_init(&iter->mutex);
b3806b43
SR
4318 filp->private_data = iter;
4319
107bad8b
SR
4320 if (iter->trace->pipe_open)
4321 iter->trace->pipe_open(iter);
107bad8b 4322
b444786f 4323 nonseekable_open(inode, filp);
b04cc6b1
FW
4324out:
4325 mutex_unlock(&trace_types_lock);
4326 return ret;
d7350c3f
FW
4327
4328fail:
4329 kfree(iter->trace);
4330 kfree(iter);
7b85af63 4331 __trace_array_put(tr);
d7350c3f
FW
4332 mutex_unlock(&trace_types_lock);
4333 return ret;
b3806b43
SR
4334}
4335
4336static int tracing_release_pipe(struct inode *inode, struct file *file)
4337{
4338 struct trace_iterator *iter = file->private_data;
15544209 4339 struct trace_array *tr = inode->i_private;
b3806b43 4340
b04cc6b1
FW
4341 mutex_lock(&trace_types_lock);
4342
29bf4a5e 4343 if (iter->trace->pipe_close)
c521efd1
SR
4344 iter->trace->pipe_close(iter);
4345
b04cc6b1
FW
4346 mutex_unlock(&trace_types_lock);
4347
4462344e 4348 free_cpumask_var(iter->started);
d7350c3f
FW
4349 mutex_destroy(&iter->mutex);
4350 kfree(iter->trace);
b3806b43 4351 kfree(iter);
b3806b43 4352
7b85af63
SRRH
4353 trace_array_put(tr);
4354
b3806b43
SR
4355 return 0;
4356}
4357
2a2cc8f7 4358static unsigned int
cc60cdc9 4359trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
2a2cc8f7 4360{
15693458
SRRH
4361 /* Iterators are static, they should be filled or empty */
4362 if (trace_buffer_iter(iter, iter->cpu_file))
4363 return POLLIN | POLLRDNORM;
2a2cc8f7 4364
15693458 4365 if (trace_flags & TRACE_ITER_BLOCK)
2a2cc8f7
SSP
4366 /*
4367 * Always select as readable when in blocking mode
4368 */
4369 return POLLIN | POLLRDNORM;
15693458 4370 else
12883efb 4371 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
15693458 4372 filp, poll_table);
2a2cc8f7 4373}
2a2cc8f7 4374
cc60cdc9
SR
4375static unsigned int
4376tracing_poll_pipe(struct file *filp, poll_table *poll_table)
4377{
4378 struct trace_iterator *iter = filp->private_data;
4379
4380 return trace_poll(iter, filp, poll_table);
2a2cc8f7
SSP
4381}
4382
ff98781b
EGM
4383/* Must be called with trace_types_lock mutex held. */
4384static int tracing_wait_pipe(struct file *filp)
b3806b43
SR
4385{
4386 struct trace_iterator *iter = filp->private_data;
8b8b3683 4387 int ret;
b3806b43 4388
b3806b43 4389 while (trace_empty(iter)) {
2dc8f095 4390
107bad8b 4391 if ((filp->f_flags & O_NONBLOCK)) {
ff98781b 4392 return -EAGAIN;
107bad8b 4393 }
2dc8f095 4394
b3806b43 4395 /*
250bfd3d 4396 * We block until we read something and tracing is disabled.
b3806b43
SR
4397 * We still block if tracing is disabled, but we have never
4398 * read anything. This allows a user to cat this file, and
4399 * then enable tracing. But after we have read something,
4400 * we give an EOF when tracing is again disabled.
4401 *
4402 * iter->pos will be 0 if we haven't read anything.
4403 */
10246fa3 4404 if (!tracing_is_on() && iter->pos)
b3806b43 4405 break;
f4874261
SRRH
4406
4407 mutex_unlock(&iter->mutex);
4408
8b8b3683 4409 ret = wait_on_pipe(iter);
f4874261
SRRH
4410
4411 mutex_lock(&iter->mutex);
4412
8b8b3683
SRRH
4413 if (ret)
4414 return ret;
4415
f4874261
SRRH
4416 if (signal_pending(current))
4417 return -EINTR;
b3806b43
SR
4418 }
4419
ff98781b
EGM
4420 return 1;
4421}
4422
4423/*
4424 * Consumer reader.
4425 */
4426static ssize_t
4427tracing_read_pipe(struct file *filp, char __user *ubuf,
4428 size_t cnt, loff_t *ppos)
4429{
4430 struct trace_iterator *iter = filp->private_data;
2b6080f2 4431 struct trace_array *tr = iter->tr;
ff98781b
EGM
4432 ssize_t sret;
4433
4434 /* return any leftover data */
4435 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4436 if (sret != -EBUSY)
4437 return sret;
4438
f9520750 4439 trace_seq_init(&iter->seq);
ff98781b 4440
d7350c3f 4441 /* copy the tracer to avoid using a global lock all around */
ff98781b 4442 mutex_lock(&trace_types_lock);
2b6080f2
SR
4443 if (unlikely(iter->trace->name != tr->current_trace->name))
4444 *iter->trace = *tr->current_trace;
d7350c3f
FW
4445 mutex_unlock(&trace_types_lock);
4446
4447 /*
4448 * Avoid more than one consumer on a single file descriptor
4449 * This is just a matter of traces coherency, the ring buffer itself
4450 * is protected.
4451 */
4452 mutex_lock(&iter->mutex);
ff98781b
EGM
4453 if (iter->trace->read) {
4454 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
4455 if (sret)
4456 goto out;
4457 }
4458
4459waitagain:
4460 sret = tracing_wait_pipe(filp);
4461 if (sret <= 0)
4462 goto out;
4463
b3806b43 4464 /* stop when tracing is finished */
ff98781b
EGM
4465 if (trace_empty(iter)) {
4466 sret = 0;
107bad8b 4467 goto out;
ff98781b 4468 }
b3806b43
SR
4469
4470 if (cnt >= PAGE_SIZE)
4471 cnt = PAGE_SIZE - 1;
4472
53d0aa77 4473 /* reset all but tr, trace, and overruns */
53d0aa77
SR
4474 memset(&iter->seq, 0,
4475 sizeof(struct trace_iterator) -
4476 offsetof(struct trace_iterator, seq));
ed5467da 4477 cpumask_clear(iter->started);
4823ed7e 4478 iter->pos = -1;
b3806b43 4479
4f535968 4480 trace_event_read_lock();
7e53bd42 4481 trace_access_lock(iter->cpu_file);
955b61e5 4482 while (trace_find_next_entry_inc(iter) != NULL) {
2c4f035f 4483 enum print_line_t ret;
088b1e42
SR
4484 int len = iter->seq.len;
4485
f9896bf3 4486 ret = print_trace_line(iter);
2c4f035f 4487 if (ret == TRACE_TYPE_PARTIAL_LINE) {
088b1e42
SR
4488 /* don't print partial lines */
4489 iter->seq.len = len;
b3806b43 4490 break;
088b1e42 4491 }
b91facc3
FW
4492 if (ret != TRACE_TYPE_NO_CONSUME)
4493 trace_consume(iter);
b3806b43
SR
4494
4495 if (iter->seq.len >= cnt)
4496 break;
ee5e51f5
JO
4497
4498 /*
4499 * Setting the full flag means we reached the trace_seq buffer
4500 * size and we should leave by partial output condition above.
4501 * One of the trace_seq_* functions is not used properly.
4502 */
4503 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
4504 iter->ent->type);
b3806b43 4505 }
7e53bd42 4506 trace_access_unlock(iter->cpu_file);
4f535968 4507 trace_event_read_unlock();
b3806b43 4508
b3806b43 4509 /* Now copy what we have to the user */
6c6c2796
PP
4510 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4511 if (iter->seq.readpos >= iter->seq.len)
f9520750 4512 trace_seq_init(&iter->seq);
9ff4b974
PP
4513
4514 /*
25985edc 4515 * If there was nothing to send to user, in spite of consuming trace
9ff4b974
PP
4516 * entries, go back to wait for more entries.
4517 */
6c6c2796 4518 if (sret == -EBUSY)
9ff4b974 4519 goto waitagain;
b3806b43 4520
107bad8b 4521out:
d7350c3f 4522 mutex_unlock(&iter->mutex);
107bad8b 4523
6c6c2796 4524 return sret;
b3806b43
SR
4525}
4526
3c56819b
EGM
4527static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
4528 unsigned int idx)
4529{
4530 __free_page(spd->pages[idx]);
4531}
4532
28dfef8f 4533static const struct pipe_buf_operations tracing_pipe_buf_ops = {
34cd4998 4534 .can_merge = 0,
34cd4998 4535 .confirm = generic_pipe_buf_confirm,
92fdd98c 4536 .release = generic_pipe_buf_release,
34cd4998
SR
4537 .steal = generic_pipe_buf_steal,
4538 .get = generic_pipe_buf_get,
3c56819b
EGM
4539};
4540
34cd4998 4541static size_t
fa7c7f6e 4542tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
34cd4998
SR
4543{
4544 size_t count;
4545 int ret;
4546
4547 /* Seq buffer is page-sized, exactly what we need. */
4548 for (;;) {
4549 count = iter->seq.len;
4550 ret = print_trace_line(iter);
4551 count = iter->seq.len - count;
4552 if (rem < count) {
4553 rem = 0;
4554 iter->seq.len -= count;
4555 break;
4556 }
4557 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4558 iter->seq.len -= count;
4559 break;
4560 }
4561
74e7ff8c
LJ
4562 if (ret != TRACE_TYPE_NO_CONSUME)
4563 trace_consume(iter);
34cd4998 4564 rem -= count;
955b61e5 4565 if (!trace_find_next_entry_inc(iter)) {
34cd4998
SR
4566 rem = 0;
4567 iter->ent = NULL;
4568 break;
4569 }
4570 }
4571
4572 return rem;
4573}
4574
3c56819b
EGM
4575static ssize_t tracing_splice_read_pipe(struct file *filp,
4576 loff_t *ppos,
4577 struct pipe_inode_info *pipe,
4578 size_t len,
4579 unsigned int flags)
4580{
35f3d14d
JA
4581 struct page *pages_def[PIPE_DEF_BUFFERS];
4582 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3c56819b
EGM
4583 struct trace_iterator *iter = filp->private_data;
4584 struct splice_pipe_desc spd = {
35f3d14d
JA
4585 .pages = pages_def,
4586 .partial = partial_def,
34cd4998 4587 .nr_pages = 0, /* This gets updated below. */
047fe360 4588 .nr_pages_max = PIPE_DEF_BUFFERS,
34cd4998
SR
4589 .flags = flags,
4590 .ops = &tracing_pipe_buf_ops,
4591 .spd_release = tracing_spd_release_pipe,
3c56819b 4592 };
2b6080f2 4593 struct trace_array *tr = iter->tr;
3c56819b 4594 ssize_t ret;
34cd4998 4595 size_t rem;
3c56819b
EGM
4596 unsigned int i;
4597
35f3d14d
JA
4598 if (splice_grow_spd(pipe, &spd))
4599 return -ENOMEM;
4600
d7350c3f 4601 /* copy the tracer to avoid using a global lock all around */
3c56819b 4602 mutex_lock(&trace_types_lock);
2b6080f2
SR
4603 if (unlikely(iter->trace->name != tr->current_trace->name))
4604 *iter->trace = *tr->current_trace;
d7350c3f
FW
4605 mutex_unlock(&trace_types_lock);
4606
4607 mutex_lock(&iter->mutex);
3c56819b
EGM
4608
4609 if (iter->trace->splice_read) {
4610 ret = iter->trace->splice_read(iter, filp,
4611 ppos, pipe, len, flags);
4612 if (ret)
34cd4998 4613 goto out_err;
3c56819b
EGM
4614 }
4615
4616 ret = tracing_wait_pipe(filp);
4617 if (ret <= 0)
34cd4998 4618 goto out_err;
3c56819b 4619
955b61e5 4620 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3c56819b 4621 ret = -EFAULT;
34cd4998 4622 goto out_err;
3c56819b
EGM
4623 }
4624
4f535968 4625 trace_event_read_lock();
7e53bd42 4626 trace_access_lock(iter->cpu_file);
4f535968 4627
3c56819b 4628 /* Fill as many pages as possible. */
a786c06d 4629 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
35f3d14d
JA
4630 spd.pages[i] = alloc_page(GFP_KERNEL);
4631 if (!spd.pages[i])
34cd4998 4632 break;
3c56819b 4633
fa7c7f6e 4634 rem = tracing_fill_pipe_page(rem, iter);
3c56819b
EGM
4635
4636 /* Copy the data into the page, so we can start over. */
4637 ret = trace_seq_to_buffer(&iter->seq,
35f3d14d 4638 page_address(spd.pages[i]),
3c56819b
EGM
4639 iter->seq.len);
4640 if (ret < 0) {
35f3d14d 4641 __free_page(spd.pages[i]);
3c56819b
EGM
4642 break;
4643 }
35f3d14d
JA
4644 spd.partial[i].offset = 0;
4645 spd.partial[i].len = iter->seq.len;
3c56819b 4646
f9520750 4647 trace_seq_init(&iter->seq);
3c56819b
EGM
4648 }
4649
7e53bd42 4650 trace_access_unlock(iter->cpu_file);
4f535968 4651 trace_event_read_unlock();
d7350c3f 4652 mutex_unlock(&iter->mutex);
3c56819b
EGM
4653
4654 spd.nr_pages = i;
4655
35f3d14d
JA
4656 ret = splice_to_pipe(pipe, &spd);
4657out:
047fe360 4658 splice_shrink_spd(&spd);
35f3d14d 4659 return ret;
3c56819b 4660
34cd4998 4661out_err:
d7350c3f 4662 mutex_unlock(&iter->mutex);
35f3d14d 4663 goto out;
3c56819b
EGM
4664}
4665
a98a3c3f
SR
4666static ssize_t
4667tracing_entries_read(struct file *filp, char __user *ubuf,
4668 size_t cnt, loff_t *ppos)
4669{
0bc392ee
ON
4670 struct inode *inode = file_inode(filp);
4671 struct trace_array *tr = inode->i_private;
4672 int cpu = tracing_get_cpu(inode);
438ced17
VN
4673 char buf[64];
4674 int r = 0;
4675 ssize_t ret;
a98a3c3f 4676
db526ca3 4677 mutex_lock(&trace_types_lock);
438ced17 4678
0bc392ee 4679 if (cpu == RING_BUFFER_ALL_CPUS) {
438ced17
VN
4680 int cpu, buf_size_same;
4681 unsigned long size;
4682
4683 size = 0;
4684 buf_size_same = 1;
4685 /* check if all cpu sizes are same */
4686 for_each_tracing_cpu(cpu) {
4687 /* fill in the size from first enabled cpu */
4688 if (size == 0)
12883efb
SRRH
4689 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4690 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
438ced17
VN
4691 buf_size_same = 0;
4692 break;
4693 }
4694 }
4695
4696 if (buf_size_same) {
4697 if (!ring_buffer_expanded)
4698 r = sprintf(buf, "%lu (expanded: %lu)\n",
4699 size >> 10,
4700 trace_buf_size >> 10);
4701 else
4702 r = sprintf(buf, "%lu\n", size >> 10);
4703 } else
4704 r = sprintf(buf, "X\n");
4705 } else
0bc392ee 4706 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
438ced17 4707
db526ca3
SR
4708 mutex_unlock(&trace_types_lock);
4709
438ced17
VN
4710 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4711 return ret;
a98a3c3f
SR
4712}
4713
4714static ssize_t
4715tracing_entries_write(struct file *filp, const char __user *ubuf,
4716 size_t cnt, loff_t *ppos)
4717{
0bc392ee
ON
4718 struct inode *inode = file_inode(filp);
4719 struct trace_array *tr = inode->i_private;
a98a3c3f 4720 unsigned long val;
4f271a2a 4721 int ret;
a98a3c3f 4722
22fe9b54
PH
4723 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4724 if (ret)
c6caeeb1 4725 return ret;
a98a3c3f
SR
4726
4727 /* must have at least 1 entry */
4728 if (!val)
4729 return -EINVAL;
4730
1696b2b0
SR
4731 /* value is in KB */
4732 val <<= 10;
0bc392ee 4733 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
4f271a2a
VN
4734 if (ret < 0)
4735 return ret;
a98a3c3f 4736
cf8517cf 4737 *ppos += cnt;
a98a3c3f 4738
4f271a2a
VN
4739 return cnt;
4740}
bf5e6519 4741
f81ab074
VN
4742static ssize_t
4743tracing_total_entries_read(struct file *filp, char __user *ubuf,
4744 size_t cnt, loff_t *ppos)
4745{
4746 struct trace_array *tr = filp->private_data;
4747 char buf[64];
4748 int r, cpu;
4749 unsigned long size = 0, expanded_size = 0;
4750
4751 mutex_lock(&trace_types_lock);
4752 for_each_tracing_cpu(cpu) {
12883efb 4753 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
f81ab074
VN
4754 if (!ring_buffer_expanded)
4755 expanded_size += trace_buf_size >> 10;
4756 }
4757 if (ring_buffer_expanded)
4758 r = sprintf(buf, "%lu\n", size);
4759 else
4760 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4761 mutex_unlock(&trace_types_lock);
4762
4763 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4764}
4765
4f271a2a
VN
4766static ssize_t
4767tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4768 size_t cnt, loff_t *ppos)
4769{
4770 /*
4771 * There is no need to read what the user has written, this function
4772 * is just to make sure that there is no error when "echo" is used
4773 */
4774
4775 *ppos += cnt;
a98a3c3f
SR
4776
4777 return cnt;
4778}
4779
4f271a2a
VN
4780static int
4781tracing_free_buffer_release(struct inode *inode, struct file *filp)
4782{
2b6080f2
SR
4783 struct trace_array *tr = inode->i_private;
4784
cf30cf67
SR
4785 /* disable tracing ? */
4786 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
711e1243 4787 tracer_tracing_off(tr);
4f271a2a 4788 /* resize the ring buffer to 0 */
2b6080f2 4789 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4f271a2a 4790
7b85af63
SRRH
4791 trace_array_put(tr);
4792
4f271a2a
VN
4793 return 0;
4794}
4795
5bf9a1ee
PP
4796static ssize_t
4797tracing_mark_write(struct file *filp, const char __user *ubuf,
4798 size_t cnt, loff_t *fpos)
4799{
d696b58c 4800 unsigned long addr = (unsigned long)ubuf;
2d71619c 4801 struct trace_array *tr = filp->private_data;
d696b58c
SR
4802 struct ring_buffer_event *event;
4803 struct ring_buffer *buffer;
4804 struct print_entry *entry;
4805 unsigned long irq_flags;
4806 struct page *pages[2];
6edb2a8a 4807 void *map_page[2];
d696b58c
SR
4808 int nr_pages = 1;
4809 ssize_t written;
d696b58c
SR
4810 int offset;
4811 int size;
4812 int len;
4813 int ret;
6edb2a8a 4814 int i;
5bf9a1ee 4815
c76f0694 4816 if (tracing_disabled)
5bf9a1ee
PP
4817 return -EINVAL;
4818
5224c3a3
MSB
4819 if (!(trace_flags & TRACE_ITER_MARKERS))
4820 return -EINVAL;
4821
5bf9a1ee
PP
4822 if (cnt > TRACE_BUF_SIZE)
4823 cnt = TRACE_BUF_SIZE;
4824
d696b58c
SR
4825 /*
4826 * Userspace is injecting traces into the kernel trace buffer.
4827 * We want to be as non intrusive as possible.
4828 * To do so, we do not want to allocate any special buffers
4829 * or take any locks, but instead write the userspace data
4830 * straight into the ring buffer.
4831 *
4832 * First we need to pin the userspace buffer into memory,
4833 * which, most likely it is, because it just referenced it.
4834 * But there's no guarantee that it is. By using get_user_pages_fast()
4835 * and kmap_atomic/kunmap_atomic() we can get access to the
4836 * pages directly. We then write the data directly into the
4837 * ring buffer.
4838 */
4839 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
5bf9a1ee 4840
d696b58c
SR
4841 /* check if we cross pages */
4842 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4843 nr_pages = 2;
4844
4845 offset = addr & (PAGE_SIZE - 1);
4846 addr &= PAGE_MASK;
4847
4848 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4849 if (ret < nr_pages) {
4850 while (--ret >= 0)
4851 put_page(pages[ret]);
4852 written = -EFAULT;
4853 goto out;
5bf9a1ee 4854 }
d696b58c 4855
6edb2a8a
SR
4856 for (i = 0; i < nr_pages; i++)
4857 map_page[i] = kmap_atomic(pages[i]);
d696b58c
SR
4858
4859 local_save_flags(irq_flags);
4860 size = sizeof(*entry) + cnt + 2; /* possible \n added */
2d71619c 4861 buffer = tr->trace_buffer.buffer;
d696b58c
SR
4862 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4863 irq_flags, preempt_count());
4864 if (!event) {
4865 /* Ring buffer disabled, return as if not open for write */
4866 written = -EBADF;
4867 goto out_unlock;
5bf9a1ee 4868 }
d696b58c
SR
4869
4870 entry = ring_buffer_event_data(event);
4871 entry->ip = _THIS_IP_;
4872
4873 if (nr_pages == 2) {
4874 len = PAGE_SIZE - offset;
6edb2a8a
SR
4875 memcpy(&entry->buf, map_page[0] + offset, len);
4876 memcpy(&entry->buf[len], map_page[1], cnt - len);
c13d2f7c 4877 } else
6edb2a8a 4878 memcpy(&entry->buf, map_page[0] + offset, cnt);
5bf9a1ee 4879
d696b58c
SR
4880 if (entry->buf[cnt - 1] != '\n') {
4881 entry->buf[cnt] = '\n';
4882 entry->buf[cnt + 1] = '\0';
4883 } else
4884 entry->buf[cnt] = '\0';
4885
7ffbd48d 4886 __buffer_unlock_commit(buffer, event);
5bf9a1ee 4887
d696b58c 4888 written = cnt;
5bf9a1ee 4889
d696b58c 4890 *fpos += written;
1aa54bca 4891
d696b58c 4892 out_unlock:
6edb2a8a
SR
4893 for (i = 0; i < nr_pages; i++){
4894 kunmap_atomic(map_page[i]);
4895 put_page(pages[i]);
4896 }
d696b58c 4897 out:
1aa54bca 4898 return written;
5bf9a1ee
PP
4899}
4900
13f16d20 4901static int tracing_clock_show(struct seq_file *m, void *v)
5079f326 4902{
2b6080f2 4903 struct trace_array *tr = m->private;
5079f326
Z
4904 int i;
4905
4906 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
13f16d20 4907 seq_printf(m,
5079f326 4908 "%s%s%s%s", i ? " " : "",
2b6080f2
SR
4909 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4910 i == tr->clock_id ? "]" : "");
13f16d20 4911 seq_putc(m, '\n');
5079f326 4912
13f16d20 4913 return 0;
5079f326
Z
4914}
4915
e1e232ca 4916static int tracing_set_clock(struct trace_array *tr, const char *clockstr)
5079f326 4917{
5079f326
Z
4918 int i;
4919
5079f326
Z
4920 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4921 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4922 break;
4923 }
4924 if (i == ARRAY_SIZE(trace_clocks))
4925 return -EINVAL;
4926
5079f326
Z
4927 mutex_lock(&trace_types_lock);
4928
2b6080f2
SR
4929 tr->clock_id = i;
4930
12883efb 4931 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
5079f326 4932
60303ed3
DS
4933 /*
4934 * New clock may not be consistent with the previous clock.
4935 * Reset the buffer so that it doesn't have incomparable timestamps.
4936 */
9457158b 4937 tracing_reset_online_cpus(&tr->trace_buffer);
12883efb
SRRH
4938
4939#ifdef CONFIG_TRACER_MAX_TRACE
4940 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4941 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
9457158b 4942 tracing_reset_online_cpus(&tr->max_buffer);
12883efb 4943#endif
60303ed3 4944
5079f326
Z
4945 mutex_unlock(&trace_types_lock);
4946
e1e232ca
SR
4947 return 0;
4948}
4949
4950static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4951 size_t cnt, loff_t *fpos)
4952{
4953 struct seq_file *m = filp->private_data;
4954 struct trace_array *tr = m->private;
4955 char buf[64];
4956 const char *clockstr;
4957 int ret;
4958
4959 if (cnt >= sizeof(buf))
4960 return -EINVAL;
4961
4962 if (copy_from_user(&buf, ubuf, cnt))
4963 return -EFAULT;
4964
4965 buf[cnt] = 0;
4966
4967 clockstr = strstrip(buf);
4968
4969 ret = tracing_set_clock(tr, clockstr);
4970 if (ret)
4971 return ret;
4972
5079f326
Z
4973 *fpos += cnt;
4974
4975 return cnt;
4976}
4977
13f16d20
LZ
4978static int tracing_clock_open(struct inode *inode, struct file *file)
4979{
7b85af63
SRRH
4980 struct trace_array *tr = inode->i_private;
4981 int ret;
4982
13f16d20
LZ
4983 if (tracing_disabled)
4984 return -ENODEV;
2b6080f2 4985
7b85af63
SRRH
4986 if (trace_array_get(tr))
4987 return -ENODEV;
4988
4989 ret = single_open(file, tracing_clock_show, inode->i_private);
4990 if (ret < 0)
4991 trace_array_put(tr);
4992
4993 return ret;
13f16d20
LZ
4994}
4995
6de58e62
SRRH
4996struct ftrace_buffer_info {
4997 struct trace_iterator iter;
4998 void *spare;
4999 unsigned int read;
5000};
5001
debdd57f
HT
5002#ifdef CONFIG_TRACER_SNAPSHOT
5003static int tracing_snapshot_open(struct inode *inode, struct file *file)
5004{
6484c71c 5005 struct trace_array *tr = inode->i_private;
debdd57f 5006 struct trace_iterator *iter;
2b6080f2 5007 struct seq_file *m;
debdd57f
HT
5008 int ret = 0;
5009
ff451961
SRRH
5010 if (trace_array_get(tr) < 0)
5011 return -ENODEV;
5012
debdd57f 5013 if (file->f_mode & FMODE_READ) {
6484c71c 5014 iter = __tracing_open(inode, file, true);
debdd57f
HT
5015 if (IS_ERR(iter))
5016 ret = PTR_ERR(iter);
2b6080f2
SR
5017 } else {
5018 /* Writes still need the seq_file to hold the private data */
f77d09a3 5019 ret = -ENOMEM;
2b6080f2
SR
5020 m = kzalloc(sizeof(*m), GFP_KERNEL);
5021 if (!m)
f77d09a3 5022 goto out;
2b6080f2
SR
5023 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
5024 if (!iter) {
5025 kfree(m);
f77d09a3 5026 goto out;
2b6080f2 5027 }
f77d09a3
AL
5028 ret = 0;
5029
ff451961 5030 iter->tr = tr;
6484c71c
ON
5031 iter->trace_buffer = &tr->max_buffer;
5032 iter->cpu_file = tracing_get_cpu(inode);
2b6080f2
SR
5033 m->private = iter;
5034 file->private_data = m;
debdd57f 5035 }
f77d09a3 5036out:
ff451961
SRRH
5037 if (ret < 0)
5038 trace_array_put(tr);
5039
debdd57f
HT
5040 return ret;
5041}
5042
5043static ssize_t
5044tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
5045 loff_t *ppos)
5046{
2b6080f2
SR
5047 struct seq_file *m = filp->private_data;
5048 struct trace_iterator *iter = m->private;
5049 struct trace_array *tr = iter->tr;
debdd57f
HT
5050 unsigned long val;
5051 int ret;
5052
5053 ret = tracing_update_buffers();
5054 if (ret < 0)
5055 return ret;
5056
5057 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5058 if (ret)
5059 return ret;
5060
5061 mutex_lock(&trace_types_lock);
5062
2b6080f2 5063 if (tr->current_trace->use_max_tr) {
debdd57f
HT
5064 ret = -EBUSY;
5065 goto out;
5066 }
5067
5068 switch (val) {
5069 case 0:
f1affcaa
SRRH
5070 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
5071 ret = -EINVAL;
5072 break;
debdd57f 5073 }
3209cff4
SRRH
5074 if (tr->allocated_snapshot)
5075 free_snapshot(tr);
debdd57f
HT
5076 break;
5077 case 1:
f1affcaa
SRRH
5078/* Only allow per-cpu swap if the ring buffer supports it */
5079#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
5080 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
5081 ret = -EINVAL;
5082 break;
5083 }
5084#endif
45ad21ca 5085 if (!tr->allocated_snapshot) {
3209cff4 5086 ret = alloc_snapshot(tr);
debdd57f
HT
5087 if (ret < 0)
5088 break;
debdd57f 5089 }
debdd57f
HT
5090 local_irq_disable();
5091 /* Now, we're going to swap */
f1affcaa 5092 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
ce9bae55 5093 update_max_tr(tr, current, smp_processor_id());
f1affcaa 5094 else
ce9bae55 5095 update_max_tr_single(tr, current, iter->cpu_file);
debdd57f
HT
5096 local_irq_enable();
5097 break;
5098 default:
45ad21ca 5099 if (tr->allocated_snapshot) {
f1affcaa
SRRH
5100 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
5101 tracing_reset_online_cpus(&tr->max_buffer);
5102 else
5103 tracing_reset(&tr->max_buffer, iter->cpu_file);
5104 }
debdd57f
HT
5105 break;
5106 }
5107
5108 if (ret >= 0) {
5109 *ppos += cnt;
5110 ret = cnt;
5111 }
5112out:
5113 mutex_unlock(&trace_types_lock);
5114 return ret;
5115}
2b6080f2
SR
5116
5117static int tracing_snapshot_release(struct inode *inode, struct file *file)
5118{
5119 struct seq_file *m = file->private_data;
ff451961
SRRH
5120 int ret;
5121
5122 ret = tracing_release(inode, file);
2b6080f2
SR
5123
5124 if (file->f_mode & FMODE_READ)
ff451961 5125 return ret;
2b6080f2
SR
5126
5127 /* If write only, the seq_file is just a stub */
5128 if (m)
5129 kfree(m->private);
5130 kfree(m);
5131
5132 return 0;
5133}
5134
6de58e62
SRRH
5135static int tracing_buffers_open(struct inode *inode, struct file *filp);
5136static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
5137 size_t count, loff_t *ppos);
5138static int tracing_buffers_release(struct inode *inode, struct file *file);
5139static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5140 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
5141
5142static int snapshot_raw_open(struct inode *inode, struct file *filp)
5143{
5144 struct ftrace_buffer_info *info;
5145 int ret;
5146
5147 ret = tracing_buffers_open(inode, filp);
5148 if (ret < 0)
5149 return ret;
5150
5151 info = filp->private_data;
5152
5153 if (info->iter.trace->use_max_tr) {
5154 tracing_buffers_release(inode, filp);
5155 return -EBUSY;
5156 }
5157
5158 info->iter.snapshot = true;
5159 info->iter.trace_buffer = &info->iter.tr->max_buffer;
5160
5161 return ret;
5162}
5163
debdd57f
HT
5164#endif /* CONFIG_TRACER_SNAPSHOT */
5165
5166
5e2336a0 5167static const struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
5168 .open = tracing_open_generic,
5169 .read = tracing_max_lat_read,
5170 .write = tracing_max_lat_write,
b444786f 5171 .llseek = generic_file_llseek,
bc0c38d1
SR
5172};
5173
5e2336a0 5174static const struct file_operations set_tracer_fops = {
4bf39a94
IM
5175 .open = tracing_open_generic,
5176 .read = tracing_set_trace_read,
5177 .write = tracing_set_trace_write,
b444786f 5178 .llseek = generic_file_llseek,
bc0c38d1
SR
5179};
5180
5e2336a0 5181static const struct file_operations tracing_pipe_fops = {
4bf39a94 5182 .open = tracing_open_pipe,
2a2cc8f7 5183 .poll = tracing_poll_pipe,
4bf39a94 5184 .read = tracing_read_pipe,
3c56819b 5185 .splice_read = tracing_splice_read_pipe,
4bf39a94 5186 .release = tracing_release_pipe,
b444786f 5187 .llseek = no_llseek,
b3806b43
SR
5188};
5189
5e2336a0 5190static const struct file_operations tracing_entries_fops = {
0bc392ee 5191 .open = tracing_open_generic_tr,
a98a3c3f
SR
5192 .read = tracing_entries_read,
5193 .write = tracing_entries_write,
b444786f 5194 .llseek = generic_file_llseek,
0bc392ee 5195 .release = tracing_release_generic_tr,
a98a3c3f
SR
5196};
5197
f81ab074 5198static const struct file_operations tracing_total_entries_fops = {
7b85af63 5199 .open = tracing_open_generic_tr,
f81ab074
VN
5200 .read = tracing_total_entries_read,
5201 .llseek = generic_file_llseek,
7b85af63 5202 .release = tracing_release_generic_tr,
f81ab074
VN
5203};
5204
4f271a2a 5205static const struct file_operations tracing_free_buffer_fops = {
7b85af63 5206 .open = tracing_open_generic_tr,
4f271a2a
VN
5207 .write = tracing_free_buffer_write,
5208 .release = tracing_free_buffer_release,
5209};
5210
5e2336a0 5211static const struct file_operations tracing_mark_fops = {
7b85af63 5212 .open = tracing_open_generic_tr,
5bf9a1ee 5213 .write = tracing_mark_write,
b444786f 5214 .llseek = generic_file_llseek,
7b85af63 5215 .release = tracing_release_generic_tr,
5bf9a1ee
PP
5216};
5217
5079f326 5218static const struct file_operations trace_clock_fops = {
13f16d20
LZ
5219 .open = tracing_clock_open,
5220 .read = seq_read,
5221 .llseek = seq_lseek,
7b85af63 5222 .release = tracing_single_release_tr,
5079f326
Z
5223 .write = tracing_clock_write,
5224};
5225
debdd57f
HT
5226#ifdef CONFIG_TRACER_SNAPSHOT
5227static const struct file_operations snapshot_fops = {
5228 .open = tracing_snapshot_open,
5229 .read = seq_read,
5230 .write = tracing_snapshot_write,
098c879e 5231 .llseek = tracing_lseek,
2b6080f2 5232 .release = tracing_snapshot_release,
debdd57f 5233};
debdd57f 5234
6de58e62
SRRH
5235static const struct file_operations snapshot_raw_fops = {
5236 .open = snapshot_raw_open,
5237 .read = tracing_buffers_read,
5238 .release = tracing_buffers_release,
5239 .splice_read = tracing_buffers_splice_read,
5240 .llseek = no_llseek,
2cadf913
SR
5241};
5242
6de58e62
SRRH
5243#endif /* CONFIG_TRACER_SNAPSHOT */
5244
2cadf913
SR
5245static int tracing_buffers_open(struct inode *inode, struct file *filp)
5246{
46ef2be0 5247 struct trace_array *tr = inode->i_private;
2cadf913 5248 struct ftrace_buffer_info *info;
7b85af63 5249 int ret;
2cadf913
SR
5250
5251 if (tracing_disabled)
5252 return -ENODEV;
5253
7b85af63
SRRH
5254 if (trace_array_get(tr) < 0)
5255 return -ENODEV;
5256
2cadf913 5257 info = kzalloc(sizeof(*info), GFP_KERNEL);
7b85af63
SRRH
5258 if (!info) {
5259 trace_array_put(tr);
2cadf913 5260 return -ENOMEM;
7b85af63 5261 }
2cadf913 5262
a695cb58
SRRH
5263 mutex_lock(&trace_types_lock);
5264
cc60cdc9 5265 info->iter.tr = tr;
46ef2be0 5266 info->iter.cpu_file = tracing_get_cpu(inode);
b627344f 5267 info->iter.trace = tr->current_trace;
12883efb 5268 info->iter.trace_buffer = &tr->trace_buffer;
cc60cdc9 5269 info->spare = NULL;
2cadf913 5270 /* Force reading ring buffer for first read */
cc60cdc9 5271 info->read = (unsigned int)-1;
2cadf913
SR
5272
5273 filp->private_data = info;
5274
a695cb58
SRRH
5275 mutex_unlock(&trace_types_lock);
5276
7b85af63
SRRH
5277 ret = nonseekable_open(inode, filp);
5278 if (ret < 0)
5279 trace_array_put(tr);
5280
5281 return ret;
2cadf913
SR
5282}
5283
cc60cdc9
SR
5284static unsigned int
5285tracing_buffers_poll(struct file *filp, poll_table *poll_table)
5286{
5287 struct ftrace_buffer_info *info = filp->private_data;
5288 struct trace_iterator *iter = &info->iter;
5289
5290 return trace_poll(iter, filp, poll_table);
5291}
5292
2cadf913
SR
5293static ssize_t
5294tracing_buffers_read(struct file *filp, char __user *ubuf,
5295 size_t count, loff_t *ppos)
5296{
5297 struct ftrace_buffer_info *info = filp->private_data;
cc60cdc9 5298 struct trace_iterator *iter = &info->iter;
2cadf913 5299 ssize_t ret;
6de58e62 5300 ssize_t size;
2cadf913 5301
2dc5d12b
SR
5302 if (!count)
5303 return 0;
5304
6de58e62
SRRH
5305 mutex_lock(&trace_types_lock);
5306
5307#ifdef CONFIG_TRACER_MAX_TRACE
5308 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5309 size = -EBUSY;
5310 goto out_unlock;
5311 }
5312#endif
5313
ddd538f3 5314 if (!info->spare)
12883efb
SRRH
5315 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
5316 iter->cpu_file);
6de58e62 5317 size = -ENOMEM;
ddd538f3 5318 if (!info->spare)
6de58e62 5319 goto out_unlock;
ddd538f3 5320
2cadf913
SR
5321 /* Do we have previous read data to read? */
5322 if (info->read < PAGE_SIZE)
5323 goto read;
5324
b627344f 5325 again:
cc60cdc9 5326 trace_access_lock(iter->cpu_file);
12883efb 5327 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
2cadf913
SR
5328 &info->spare,
5329 count,
cc60cdc9
SR
5330 iter->cpu_file, 0);
5331 trace_access_unlock(iter->cpu_file);
2cadf913 5332
b627344f
SR
5333 if (ret < 0) {
5334 if (trace_empty(iter)) {
6de58e62
SRRH
5335 if ((filp->f_flags & O_NONBLOCK)) {
5336 size = -EAGAIN;
5337 goto out_unlock;
5338 }
5339 mutex_unlock(&trace_types_lock);
8b8b3683 5340 ret = wait_on_pipe(iter);
6de58e62 5341 mutex_lock(&trace_types_lock);
8b8b3683
SRRH
5342 if (ret) {
5343 size = ret;
5344 goto out_unlock;
5345 }
6de58e62
SRRH
5346 if (signal_pending(current)) {
5347 size = -EINTR;
5348 goto out_unlock;
5349 }
b627344f
SR
5350 goto again;
5351 }
6de58e62
SRRH
5352 size = 0;
5353 goto out_unlock;
b627344f 5354 }
436fc280 5355
436fc280 5356 info->read = 0;
b627344f 5357 read:
2cadf913
SR
5358 size = PAGE_SIZE - info->read;
5359 if (size > count)
5360 size = count;
5361
5362 ret = copy_to_user(ubuf, info->spare + info->read, size);
6de58e62
SRRH
5363 if (ret == size) {
5364 size = -EFAULT;
5365 goto out_unlock;
5366 }
2dc5d12b
SR
5367 size -= ret;
5368
2cadf913
SR
5369 *ppos += size;
5370 info->read += size;
5371
6de58e62
SRRH
5372 out_unlock:
5373 mutex_unlock(&trace_types_lock);
5374
2cadf913
SR
5375 return size;
5376}
5377
5378static int tracing_buffers_release(struct inode *inode, struct file *file)
5379{
5380 struct ftrace_buffer_info *info = file->private_data;
cc60cdc9 5381 struct trace_iterator *iter = &info->iter;
2cadf913 5382
a695cb58
SRRH
5383 mutex_lock(&trace_types_lock);
5384
ff451961 5385 __trace_array_put(iter->tr);
2cadf913 5386
ddd538f3 5387 if (info->spare)
12883efb 5388 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
2cadf913
SR
5389 kfree(info);
5390
a695cb58
SRRH
5391 mutex_unlock(&trace_types_lock);
5392
2cadf913
SR
5393 return 0;
5394}
5395
5396struct buffer_ref {
5397 struct ring_buffer *buffer;
5398 void *page;
5399 int ref;
5400};
5401
5402static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
5403 struct pipe_buffer *buf)
5404{
5405 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5406
5407 if (--ref->ref)
5408 return;
5409
5410 ring_buffer_free_read_page(ref->buffer, ref->page);
5411 kfree(ref);
5412 buf->private = 0;
5413}
5414
2cadf913
SR
5415static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
5416 struct pipe_buffer *buf)
5417{
5418 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5419
5420 ref->ref++;
5421}
5422
5423/* Pipe buffer operations for a buffer. */
28dfef8f 5424static const struct pipe_buf_operations buffer_pipe_buf_ops = {
2cadf913 5425 .can_merge = 0,
2cadf913
SR
5426 .confirm = generic_pipe_buf_confirm,
5427 .release = buffer_pipe_buf_release,
d55cb6cf 5428 .steal = generic_pipe_buf_steal,
2cadf913
SR
5429 .get = buffer_pipe_buf_get,
5430};
5431
5432/*
5433 * Callback from splice_to_pipe(), if we need to release some pages
5434 * at the end of the spd in case we error'ed out in filling the pipe.
5435 */
5436static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
5437{
5438 struct buffer_ref *ref =
5439 (struct buffer_ref *)spd->partial[i].private;
5440
5441 if (--ref->ref)
5442 return;
5443
5444 ring_buffer_free_read_page(ref->buffer, ref->page);
5445 kfree(ref);
5446 spd->partial[i].private = 0;
5447}
5448
5449static ssize_t
5450tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5451 struct pipe_inode_info *pipe, size_t len,
5452 unsigned int flags)
5453{
5454 struct ftrace_buffer_info *info = file->private_data;
cc60cdc9 5455 struct trace_iterator *iter = &info->iter;
35f3d14d
JA
5456 struct partial_page partial_def[PIPE_DEF_BUFFERS];
5457 struct page *pages_def[PIPE_DEF_BUFFERS];
2cadf913 5458 struct splice_pipe_desc spd = {
35f3d14d
JA
5459 .pages = pages_def,
5460 .partial = partial_def,
047fe360 5461 .nr_pages_max = PIPE_DEF_BUFFERS,
2cadf913
SR
5462 .flags = flags,
5463 .ops = &buffer_pipe_buf_ops,
5464 .spd_release = buffer_spd_release,
5465 };
5466 struct buffer_ref *ref;
93459c6c 5467 int entries, size, i;
6de58e62 5468 ssize_t ret;
2cadf913 5469
6de58e62
SRRH
5470 mutex_lock(&trace_types_lock);
5471
5472#ifdef CONFIG_TRACER_MAX_TRACE
5473 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5474 ret = -EBUSY;
5475 goto out;
5476 }
5477#endif
5478
5479 if (splice_grow_spd(pipe, &spd)) {
5480 ret = -ENOMEM;
5481 goto out;
5482 }
35f3d14d 5483
93cfb3c9 5484 if (*ppos & (PAGE_SIZE - 1)) {
35f3d14d
JA
5485 ret = -EINVAL;
5486 goto out;
93cfb3c9
LJ
5487 }
5488
5489 if (len & (PAGE_SIZE - 1)) {
35f3d14d
JA
5490 if (len < PAGE_SIZE) {
5491 ret = -EINVAL;
5492 goto out;
5493 }
93cfb3c9
LJ
5494 len &= PAGE_MASK;
5495 }
5496
cc60cdc9
SR
5497 again:
5498 trace_access_lock(iter->cpu_file);
12883efb 5499 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
93459c6c 5500
a786c06d 5501 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
2cadf913
SR
5502 struct page *page;
5503 int r;
5504
5505 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
5506 if (!ref)
5507 break;
5508
7267fa68 5509 ref->ref = 1;
12883efb 5510 ref->buffer = iter->trace_buffer->buffer;
cc60cdc9 5511 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
2cadf913
SR
5512 if (!ref->page) {
5513 kfree(ref);
5514 break;
5515 }
5516
5517 r = ring_buffer_read_page(ref->buffer, &ref->page,
cc60cdc9 5518 len, iter->cpu_file, 1);
2cadf913 5519 if (r < 0) {
7ea59064 5520 ring_buffer_free_read_page(ref->buffer, ref->page);
2cadf913
SR
5521 kfree(ref);
5522 break;
5523 }
5524
5525 /*
5526 * zero out any left over data, this is going to
5527 * user land.
5528 */
5529 size = ring_buffer_page_len(ref->page);
5530 if (size < PAGE_SIZE)
5531 memset(ref->page + size, 0, PAGE_SIZE - size);
5532
5533 page = virt_to_page(ref->page);
5534
5535 spd.pages[i] = page;
5536 spd.partial[i].len = PAGE_SIZE;
5537 spd.partial[i].offset = 0;
5538 spd.partial[i].private = (unsigned long)ref;
5539 spd.nr_pages++;
93cfb3c9 5540 *ppos += PAGE_SIZE;
93459c6c 5541
12883efb 5542 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
2cadf913
SR
5543 }
5544
cc60cdc9 5545 trace_access_unlock(iter->cpu_file);
2cadf913
SR
5546 spd.nr_pages = i;
5547
5548 /* did we read anything? */
5549 if (!spd.nr_pages) {
cc60cdc9 5550 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
2cadf913 5551 ret = -EAGAIN;
cc60cdc9
SR
5552 goto out;
5553 }
6de58e62 5554 mutex_unlock(&trace_types_lock);
8b8b3683 5555 ret = wait_on_pipe(iter);
6de58e62 5556 mutex_lock(&trace_types_lock);
8b8b3683
SRRH
5557 if (ret)
5558 goto out;
cc60cdc9
SR
5559 if (signal_pending(current)) {
5560 ret = -EINTR;
5561 goto out;
5562 }
5563 goto again;
2cadf913
SR
5564 }
5565
5566 ret = splice_to_pipe(pipe, &spd);
047fe360 5567 splice_shrink_spd(&spd);
35f3d14d 5568out:
6de58e62
SRRH
5569 mutex_unlock(&trace_types_lock);
5570
2cadf913
SR
5571 return ret;
5572}
5573
5574static const struct file_operations tracing_buffers_fops = {
5575 .open = tracing_buffers_open,
5576 .read = tracing_buffers_read,
cc60cdc9 5577 .poll = tracing_buffers_poll,
2cadf913
SR
5578 .release = tracing_buffers_release,
5579 .splice_read = tracing_buffers_splice_read,
5580 .llseek = no_llseek,
5581};
5582
c8d77183
SR
5583static ssize_t
5584tracing_stats_read(struct file *filp, char __user *ubuf,
5585 size_t count, loff_t *ppos)
5586{
4d3435b8
ON
5587 struct inode *inode = file_inode(filp);
5588 struct trace_array *tr = inode->i_private;
12883efb 5589 struct trace_buffer *trace_buf = &tr->trace_buffer;
4d3435b8 5590 int cpu = tracing_get_cpu(inode);
c8d77183
SR
5591 struct trace_seq *s;
5592 unsigned long cnt;
c64e148a
VN
5593 unsigned long long t;
5594 unsigned long usec_rem;
c8d77183 5595
e4f2d10f 5596 s = kmalloc(sizeof(*s), GFP_KERNEL);
c8d77183 5597 if (!s)
a646365c 5598 return -ENOMEM;
c8d77183
SR
5599
5600 trace_seq_init(s);
5601
12883efb 5602 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
c8d77183
SR
5603 trace_seq_printf(s, "entries: %ld\n", cnt);
5604
12883efb 5605 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
c8d77183
SR
5606 trace_seq_printf(s, "overrun: %ld\n", cnt);
5607
12883efb 5608 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
c8d77183
SR
5609 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5610
12883efb 5611 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
c64e148a
VN
5612 trace_seq_printf(s, "bytes: %ld\n", cnt);
5613
58e8eedf 5614 if (trace_clocks[tr->clock_id].in_ns) {
11043d8b 5615 /* local or global for trace_clock */
12883efb 5616 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
11043d8b
YY
5617 usec_rem = do_div(t, USEC_PER_SEC);
5618 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5619 t, usec_rem);
5620
12883efb 5621 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
11043d8b
YY
5622 usec_rem = do_div(t, USEC_PER_SEC);
5623 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5624 } else {
5625 /* counter or tsc mode for trace_clock */
5626 trace_seq_printf(s, "oldest event ts: %llu\n",
12883efb 5627 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
c64e148a 5628
11043d8b 5629 trace_seq_printf(s, "now ts: %llu\n",
12883efb 5630 ring_buffer_time_stamp(trace_buf->buffer, cpu));
11043d8b 5631 }
c64e148a 5632
12883efb 5633 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
884bfe89
SP
5634 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5635
12883efb 5636 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
ad964704
SRRH
5637 trace_seq_printf(s, "read events: %ld\n", cnt);
5638
c8d77183
SR
5639 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
5640
5641 kfree(s);
5642
5643 return count;
5644}
5645
5646static const struct file_operations tracing_stats_fops = {
4d3435b8 5647 .open = tracing_open_generic_tr,
c8d77183 5648 .read = tracing_stats_read,
b444786f 5649 .llseek = generic_file_llseek,
4d3435b8 5650 .release = tracing_release_generic_tr,
c8d77183
SR
5651};
5652
bc0c38d1
SR
5653#ifdef CONFIG_DYNAMIC_FTRACE
5654
b807c3d0
SR
5655int __weak ftrace_arch_read_dyn_info(char *buf, int size)
5656{
5657 return 0;
5658}
5659
bc0c38d1 5660static ssize_t
b807c3d0 5661tracing_read_dyn_info(struct file *filp, char __user *ubuf,
bc0c38d1
SR
5662 size_t cnt, loff_t *ppos)
5663{
a26a2a27
SR
5664 static char ftrace_dyn_info_buffer[1024];
5665 static DEFINE_MUTEX(dyn_info_mutex);
bc0c38d1 5666 unsigned long *p = filp->private_data;
b807c3d0 5667 char *buf = ftrace_dyn_info_buffer;
a26a2a27 5668 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
bc0c38d1
SR
5669 int r;
5670
b807c3d0
SR
5671 mutex_lock(&dyn_info_mutex);
5672 r = sprintf(buf, "%ld ", *p);
4bf39a94 5673
a26a2a27 5674 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
b807c3d0
SR
5675 buf[r++] = '\n';
5676
5677 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5678
5679 mutex_unlock(&dyn_info_mutex);
5680
5681 return r;
bc0c38d1
SR
5682}
5683
5e2336a0 5684static const struct file_operations tracing_dyn_info_fops = {
4bf39a94 5685 .open = tracing_open_generic,
b807c3d0 5686 .read = tracing_read_dyn_info,
b444786f 5687 .llseek = generic_file_llseek,
bc0c38d1 5688};
77fd5c15 5689#endif /* CONFIG_DYNAMIC_FTRACE */
bc0c38d1 5690
77fd5c15
SRRH
5691#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5692static void
5693ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5694{
5695 tracing_snapshot();
5696}
bc0c38d1 5697
77fd5c15
SRRH
5698static void
5699ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
bc0c38d1 5700{
77fd5c15
SRRH
5701 unsigned long *count = (long *)data;
5702
5703 if (!*count)
5704 return;
bc0c38d1 5705
77fd5c15
SRRH
5706 if (*count != -1)
5707 (*count)--;
5708
5709 tracing_snapshot();
5710}
5711
5712static int
5713ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5714 struct ftrace_probe_ops *ops, void *data)
5715{
5716 long count = (long)data;
5717
5718 seq_printf(m, "%ps:", (void *)ip);
5719
5720 seq_printf(m, "snapshot");
5721
5722 if (count == -1)
5723 seq_printf(m, ":unlimited\n");
5724 else
5725 seq_printf(m, ":count=%ld\n", count);
5726
5727 return 0;
5728}
5729
5730static struct ftrace_probe_ops snapshot_probe_ops = {
5731 .func = ftrace_snapshot,
5732 .print = ftrace_snapshot_print,
5733};
5734
5735static struct ftrace_probe_ops snapshot_count_probe_ops = {
5736 .func = ftrace_count_snapshot,
5737 .print = ftrace_snapshot_print,
5738};
5739
5740static int
5741ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5742 char *glob, char *cmd, char *param, int enable)
5743{
5744 struct ftrace_probe_ops *ops;
5745 void *count = (void *)-1;
5746 char *number;
5747 int ret;
5748
5749 /* hash funcs only work with set_ftrace_filter */
5750 if (!enable)
5751 return -EINVAL;
5752
5753 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
5754
5755 if (glob[0] == '!') {
5756 unregister_ftrace_function_probe_func(glob+1, ops);
5757 return 0;
5758 }
5759
5760 if (!param)
5761 goto out_reg;
5762
5763 number = strsep(&param, ":");
5764
5765 if (!strlen(number))
5766 goto out_reg;
5767
5768 /*
5769 * We use the callback data field (which is a pointer)
5770 * as our counter.
5771 */
5772 ret = kstrtoul(number, 0, (unsigned long *)&count);
5773 if (ret)
5774 return ret;
5775
5776 out_reg:
5777 ret = register_ftrace_function_probe(glob, ops, count);
5778
5779 if (ret >= 0)
5780 alloc_snapshot(&global_trace);
5781
5782 return ret < 0 ? ret : 0;
5783}
5784
5785static struct ftrace_func_command ftrace_snapshot_cmd = {
5786 .name = "snapshot",
5787 .func = ftrace_trace_snapshot_callback,
5788};
5789
38de93ab 5790static __init int register_snapshot_cmd(void)
77fd5c15
SRRH
5791{
5792 return register_ftrace_command(&ftrace_snapshot_cmd);
5793}
5794#else
38de93ab 5795static inline __init int register_snapshot_cmd(void) { return 0; }
77fd5c15 5796#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
bc0c38d1 5797
2b6080f2 5798struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
bc0c38d1 5799{
2b6080f2
SR
5800 if (tr->dir)
5801 return tr->dir;
bc0c38d1 5802
3e1f60b8
FW
5803 if (!debugfs_initialized())
5804 return NULL;
5805
2b6080f2
SR
5806 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5807 tr->dir = debugfs_create_dir("tracing", NULL);
bc0c38d1 5808
687c878a
J
5809 if (!tr->dir)
5810 pr_warn_once("Could not create debugfs directory 'tracing'\n");
bc0c38d1 5811
2b6080f2 5812 return tr->dir;
bc0c38d1
SR
5813}
5814
2b6080f2
SR
5815struct dentry *tracing_init_dentry(void)
5816{
5817 return tracing_init_dentry_tr(&global_trace);
5818}
b04cc6b1 5819
2b6080f2 5820static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
b04cc6b1 5821{
b04cc6b1
FW
5822 struct dentry *d_tracer;
5823
2b6080f2
SR
5824 if (tr->percpu_dir)
5825 return tr->percpu_dir;
b04cc6b1 5826
2b6080f2 5827 d_tracer = tracing_init_dentry_tr(tr);
b04cc6b1
FW
5828 if (!d_tracer)
5829 return NULL;
5830
2b6080f2 5831 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
b04cc6b1 5832
2b6080f2
SR
5833 WARN_ONCE(!tr->percpu_dir,
5834 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
b04cc6b1 5835
2b6080f2 5836 return tr->percpu_dir;
b04cc6b1
FW
5837}
5838
649e9c70
ON
5839static struct dentry *
5840trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
5841 void *data, long cpu, const struct file_operations *fops)
5842{
5843 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
5844
5845 if (ret) /* See tracing_get_cpu() */
5846 ret->d_inode->i_cdev = (void *)(cpu + 1);
5847 return ret;
5848}
5849
2b6080f2
SR
5850static void
5851tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
b04cc6b1 5852{
2b6080f2 5853 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5452af66 5854 struct dentry *d_cpu;
dd49a38c 5855 char cpu_dir[30]; /* 30 characters should be more than enough */
b04cc6b1 5856
0a3d7ce7
NK
5857 if (!d_percpu)
5858 return;
5859
dd49a38c 5860 snprintf(cpu_dir, 30, "cpu%ld", cpu);
8656e7a2
FW
5861 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5862 if (!d_cpu) {
5863 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5864 return;
5865 }
b04cc6b1 5866
8656e7a2 5867 /* per cpu trace_pipe */
649e9c70 5868 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
15544209 5869 tr, cpu, &tracing_pipe_fops);
b04cc6b1
FW
5870
5871 /* per cpu trace */
649e9c70 5872 trace_create_cpu_file("trace", 0644, d_cpu,
6484c71c 5873 tr, cpu, &tracing_fops);
7f96f93f 5874
649e9c70 5875 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
46ef2be0 5876 tr, cpu, &tracing_buffers_fops);
7f96f93f 5877
649e9c70 5878 trace_create_cpu_file("stats", 0444, d_cpu,
4d3435b8 5879 tr, cpu, &tracing_stats_fops);
438ced17 5880
649e9c70 5881 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
0bc392ee 5882 tr, cpu, &tracing_entries_fops);
f1affcaa
SRRH
5883
5884#ifdef CONFIG_TRACER_SNAPSHOT
649e9c70 5885 trace_create_cpu_file("snapshot", 0644, d_cpu,
6484c71c 5886 tr, cpu, &snapshot_fops);
6de58e62 5887
649e9c70 5888 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
46ef2be0 5889 tr, cpu, &snapshot_raw_fops);
f1affcaa 5890#endif
b04cc6b1
FW
5891}
5892
60a11774
SR
5893#ifdef CONFIG_FTRACE_SELFTEST
5894/* Let selftest have access to static functions in this file */
5895#include "trace_selftest.c"
5896#endif
5897
577b785f
SR
5898struct trace_option_dentry {
5899 struct tracer_opt *opt;
5900 struct tracer_flags *flags;
2b6080f2 5901 struct trace_array *tr;
577b785f
SR
5902 struct dentry *entry;
5903};
5904
5905static ssize_t
5906trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5907 loff_t *ppos)
5908{
5909 struct trace_option_dentry *topt = filp->private_data;
5910 char *buf;
5911
5912 if (topt->flags->val & topt->opt->bit)
5913 buf = "1\n";
5914 else
5915 buf = "0\n";
5916
5917 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5918}
5919
5920static ssize_t
5921trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5922 loff_t *ppos)
5923{
5924 struct trace_option_dentry *topt = filp->private_data;
5925 unsigned long val;
577b785f
SR
5926 int ret;
5927
22fe9b54
PH
5928 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5929 if (ret)
577b785f
SR
5930 return ret;
5931
8d18eaaf
LZ
5932 if (val != 0 && val != 1)
5933 return -EINVAL;
577b785f 5934
8d18eaaf 5935 if (!!(topt->flags->val & topt->opt->bit) != val) {
577b785f 5936 mutex_lock(&trace_types_lock);
8c1a49ae 5937 ret = __set_tracer_option(topt->tr, topt->flags,
c757bea9 5938 topt->opt, !val);
577b785f
SR
5939 mutex_unlock(&trace_types_lock);
5940 if (ret)
5941 return ret;
577b785f
SR
5942 }
5943
5944 *ppos += cnt;
5945
5946 return cnt;
5947}
5948
5949
5950static const struct file_operations trace_options_fops = {
5951 .open = tracing_open_generic,
5952 .read = trace_options_read,
5953 .write = trace_options_write,
b444786f 5954 .llseek = generic_file_llseek,
577b785f
SR
5955};
5956
a8259075
SR
5957static ssize_t
5958trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5959 loff_t *ppos)
5960{
5961 long index = (long)filp->private_data;
5962 char *buf;
5963
5964 if (trace_flags & (1 << index))
5965 buf = "1\n";
5966 else
5967 buf = "0\n";
5968
5969 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5970}
5971
5972static ssize_t
5973trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5974 loff_t *ppos)
5975{
2b6080f2 5976 struct trace_array *tr = &global_trace;
a8259075 5977 long index = (long)filp->private_data;
a8259075
SR
5978 unsigned long val;
5979 int ret;
5980
22fe9b54
PH
5981 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5982 if (ret)
a8259075
SR
5983 return ret;
5984
f2d84b65 5985 if (val != 0 && val != 1)
a8259075 5986 return -EINVAL;
69d34da2
SRRH
5987
5988 mutex_lock(&trace_types_lock);
2b6080f2 5989 ret = set_tracer_flag(tr, 1 << index, val);
69d34da2 5990 mutex_unlock(&trace_types_lock);
a8259075 5991
613f04a0
SRRH
5992 if (ret < 0)
5993 return ret;
5994
a8259075
SR
5995 *ppos += cnt;
5996
5997 return cnt;
5998}
5999
a8259075
SR
6000static const struct file_operations trace_options_core_fops = {
6001 .open = tracing_open_generic,
6002 .read = trace_options_core_read,
6003 .write = trace_options_core_write,
b444786f 6004 .llseek = generic_file_llseek,
a8259075
SR
6005};
6006
5452af66 6007struct dentry *trace_create_file(const char *name,
f4ae40a6 6008 umode_t mode,
5452af66
FW
6009 struct dentry *parent,
6010 void *data,
6011 const struct file_operations *fops)
6012{
6013 struct dentry *ret;
6014
6015 ret = debugfs_create_file(name, mode, parent, data, fops);
6016 if (!ret)
6017 pr_warning("Could not create debugfs '%s' entry\n", name);
6018
6019 return ret;
6020}
6021
6022
2b6080f2 6023static struct dentry *trace_options_init_dentry(struct trace_array *tr)
a8259075
SR
6024{
6025 struct dentry *d_tracer;
a8259075 6026
2b6080f2
SR
6027 if (tr->options)
6028 return tr->options;
a8259075 6029
2b6080f2 6030 d_tracer = tracing_init_dentry_tr(tr);
a8259075
SR
6031 if (!d_tracer)
6032 return NULL;
6033
2b6080f2
SR
6034 tr->options = debugfs_create_dir("options", d_tracer);
6035 if (!tr->options) {
a8259075
SR
6036 pr_warning("Could not create debugfs directory 'options'\n");
6037 return NULL;
6038 }
6039
2b6080f2 6040 return tr->options;
a8259075
SR
6041}
6042
577b785f 6043static void
2b6080f2
SR
6044create_trace_option_file(struct trace_array *tr,
6045 struct trace_option_dentry *topt,
577b785f
SR
6046 struct tracer_flags *flags,
6047 struct tracer_opt *opt)
6048{
6049 struct dentry *t_options;
577b785f 6050
2b6080f2 6051 t_options = trace_options_init_dentry(tr);
577b785f
SR
6052 if (!t_options)
6053 return;
6054
6055 topt->flags = flags;
6056 topt->opt = opt;
2b6080f2 6057 topt->tr = tr;
577b785f 6058
5452af66 6059 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
577b785f
SR
6060 &trace_options_fops);
6061
577b785f
SR
6062}
6063
6064static struct trace_option_dentry *
2b6080f2 6065create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
577b785f
SR
6066{
6067 struct trace_option_dentry *topts;
6068 struct tracer_flags *flags;
6069 struct tracer_opt *opts;
6070 int cnt;
6071
6072 if (!tracer)
6073 return NULL;
6074
6075 flags = tracer->flags;
6076
6077 if (!flags || !flags->opts)
6078 return NULL;
6079
6080 opts = flags->opts;
6081
6082 for (cnt = 0; opts[cnt].name; cnt++)
6083 ;
6084
0cfe8245 6085 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
577b785f
SR
6086 if (!topts)
6087 return NULL;
6088
6089 for (cnt = 0; opts[cnt].name; cnt++)
2b6080f2 6090 create_trace_option_file(tr, &topts[cnt], flags,
577b785f
SR
6091 &opts[cnt]);
6092
6093 return topts;
6094}
6095
6096static void
6097destroy_trace_option_files(struct trace_option_dentry *topts)
6098{
6099 int cnt;
6100
6101 if (!topts)
6102 return;
6103
6104 for (cnt = 0; topts[cnt].opt; cnt++) {
6105 if (topts[cnt].entry)
6106 debugfs_remove(topts[cnt].entry);
6107 }
6108
6109 kfree(topts);
6110}
6111
a8259075 6112static struct dentry *
2b6080f2
SR
6113create_trace_option_core_file(struct trace_array *tr,
6114 const char *option, long index)
a8259075
SR
6115{
6116 struct dentry *t_options;
a8259075 6117
2b6080f2 6118 t_options = trace_options_init_dentry(tr);
a8259075
SR
6119 if (!t_options)
6120 return NULL;
6121
5452af66 6122 return trace_create_file(option, 0644, t_options, (void *)index,
a8259075 6123 &trace_options_core_fops);
a8259075
SR
6124}
6125
2b6080f2 6126static __init void create_trace_options_dir(struct trace_array *tr)
a8259075
SR
6127{
6128 struct dentry *t_options;
a8259075
SR
6129 int i;
6130
2b6080f2 6131 t_options = trace_options_init_dentry(tr);
a8259075
SR
6132 if (!t_options)
6133 return;
6134
5452af66 6135 for (i = 0; trace_options[i]; i++)
2b6080f2 6136 create_trace_option_core_file(tr, trace_options[i], i);
a8259075
SR
6137}
6138
499e5470
SR
6139static ssize_t
6140rb_simple_read(struct file *filp, char __user *ubuf,
6141 size_t cnt, loff_t *ppos)
6142{
348f0fc2 6143 struct trace_array *tr = filp->private_data;
499e5470
SR
6144 char buf[64];
6145 int r;
6146
10246fa3 6147 r = tracer_tracing_is_on(tr);
499e5470
SR
6148 r = sprintf(buf, "%d\n", r);
6149
6150 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6151}
6152
6153static ssize_t
6154rb_simple_write(struct file *filp, const char __user *ubuf,
6155 size_t cnt, loff_t *ppos)
6156{
348f0fc2 6157 struct trace_array *tr = filp->private_data;
12883efb 6158 struct ring_buffer *buffer = tr->trace_buffer.buffer;
499e5470
SR
6159 unsigned long val;
6160 int ret;
6161
6162 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6163 if (ret)
6164 return ret;
6165
6166 if (buffer) {
2df8f8a6
SR
6167 mutex_lock(&trace_types_lock);
6168 if (val) {
10246fa3 6169 tracer_tracing_on(tr);
2b6080f2
SR
6170 if (tr->current_trace->start)
6171 tr->current_trace->start(tr);
2df8f8a6 6172 } else {
10246fa3 6173 tracer_tracing_off(tr);
2b6080f2
SR
6174 if (tr->current_trace->stop)
6175 tr->current_trace->stop(tr);
2df8f8a6
SR
6176 }
6177 mutex_unlock(&trace_types_lock);
499e5470
SR
6178 }
6179
6180 (*ppos)++;
6181
6182 return cnt;
6183}
6184
6185static const struct file_operations rb_simple_fops = {
7b85af63 6186 .open = tracing_open_generic_tr,
499e5470
SR
6187 .read = rb_simple_read,
6188 .write = rb_simple_write,
7b85af63 6189 .release = tracing_release_generic_tr,
499e5470
SR
6190 .llseek = default_llseek,
6191};
6192
277ba044
SR
6193struct dentry *trace_instance_dir;
6194
6195static void
6196init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
6197
55034cd6
SRRH
6198static int
6199allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
277ba044
SR
6200{
6201 enum ring_buffer_flags rb_flags;
737223fb
SRRH
6202
6203 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
6204
dced341b
SRRH
6205 buf->tr = tr;
6206
55034cd6
SRRH
6207 buf->buffer = ring_buffer_alloc(size, rb_flags);
6208 if (!buf->buffer)
6209 return -ENOMEM;
737223fb 6210
55034cd6
SRRH
6211 buf->data = alloc_percpu(struct trace_array_cpu);
6212 if (!buf->data) {
6213 ring_buffer_free(buf->buffer);
6214 return -ENOMEM;
6215 }
737223fb 6216
737223fb
SRRH
6217 /* Allocate the first page for all buffers */
6218 set_buffer_entries(&tr->trace_buffer,
6219 ring_buffer_size(tr->trace_buffer.buffer, 0));
6220
55034cd6
SRRH
6221 return 0;
6222}
737223fb 6223
55034cd6
SRRH
6224static int allocate_trace_buffers(struct trace_array *tr, int size)
6225{
6226 int ret;
737223fb 6227
55034cd6
SRRH
6228 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
6229 if (ret)
6230 return ret;
737223fb 6231
55034cd6
SRRH
6232#ifdef CONFIG_TRACER_MAX_TRACE
6233 ret = allocate_trace_buffer(tr, &tr->max_buffer,
6234 allocate_snapshot ? size : 1);
6235 if (WARN_ON(ret)) {
737223fb 6236 ring_buffer_free(tr->trace_buffer.buffer);
55034cd6
SRRH
6237 free_percpu(tr->trace_buffer.data);
6238 return -ENOMEM;
6239 }
6240 tr->allocated_snapshot = allocate_snapshot;
737223fb 6241
55034cd6
SRRH
6242 /*
6243 * Only the top level trace array gets its snapshot allocated
6244 * from the kernel command line.
6245 */
6246 allocate_snapshot = false;
737223fb 6247#endif
55034cd6 6248 return 0;
737223fb
SRRH
6249}
6250
f0b70cc4
SRRH
6251static void free_trace_buffer(struct trace_buffer *buf)
6252{
6253 if (buf->buffer) {
6254 ring_buffer_free(buf->buffer);
6255 buf->buffer = NULL;
6256 free_percpu(buf->data);
6257 buf->data = NULL;
6258 }
6259}
6260
23aaa3c1
SRRH
6261static void free_trace_buffers(struct trace_array *tr)
6262{
6263 if (!tr)
6264 return;
6265
f0b70cc4 6266 free_trace_buffer(&tr->trace_buffer);
23aaa3c1
SRRH
6267
6268#ifdef CONFIG_TRACER_MAX_TRACE
f0b70cc4 6269 free_trace_buffer(&tr->max_buffer);
23aaa3c1
SRRH
6270#endif
6271}
6272
737223fb
SRRH
6273static int new_instance_create(const char *name)
6274{
277ba044
SR
6275 struct trace_array *tr;
6276 int ret;
277ba044
SR
6277
6278 mutex_lock(&trace_types_lock);
6279
6280 ret = -EEXIST;
6281 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6282 if (tr->name && strcmp(tr->name, name) == 0)
6283 goto out_unlock;
6284 }
6285
6286 ret = -ENOMEM;
6287 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
6288 if (!tr)
6289 goto out_unlock;
6290
6291 tr->name = kstrdup(name, GFP_KERNEL);
6292 if (!tr->name)
6293 goto out_free_tr;
6294
ccfe9e42
AL
6295 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
6296 goto out_free_tr;
6297
6298 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
6299
277ba044
SR
6300 raw_spin_lock_init(&tr->start_lock);
6301
0b9b12c1
SRRH
6302 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
6303
277ba044
SR
6304 tr->current_trace = &nop_trace;
6305
6306 INIT_LIST_HEAD(&tr->systems);
6307 INIT_LIST_HEAD(&tr->events);
6308
737223fb 6309 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
277ba044
SR
6310 goto out_free_tr;
6311
277ba044
SR
6312 tr->dir = debugfs_create_dir(name, trace_instance_dir);
6313 if (!tr->dir)
6314 goto out_free_tr;
6315
6316 ret = event_trace_add_tracer(tr->dir, tr);
609e85a7
AL
6317 if (ret) {
6318 debugfs_remove_recursive(tr->dir);
277ba044 6319 goto out_free_tr;
609e85a7 6320 }
277ba044
SR
6321
6322 init_tracer_debugfs(tr, tr->dir);
6323
6324 list_add(&tr->list, &ftrace_trace_arrays);
6325
6326 mutex_unlock(&trace_types_lock);
6327
6328 return 0;
6329
6330 out_free_tr:
23aaa3c1 6331 free_trace_buffers(tr);
ccfe9e42 6332 free_cpumask_var(tr->tracing_cpumask);
277ba044
SR
6333 kfree(tr->name);
6334 kfree(tr);
6335
6336 out_unlock:
6337 mutex_unlock(&trace_types_lock);
6338
6339 return ret;
6340
6341}
6342
0c8916c3
SR
6343static int instance_delete(const char *name)
6344{
6345 struct trace_array *tr;
6346 int found = 0;
6347 int ret;
6348
6349 mutex_lock(&trace_types_lock);
6350
6351 ret = -ENODEV;
6352 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6353 if (tr->name && strcmp(tr->name, name) == 0) {
6354 found = 1;
6355 break;
6356 }
6357 }
6358 if (!found)
6359 goto out_unlock;
6360
a695cb58
SRRH
6361 ret = -EBUSY;
6362 if (tr->ref)
6363 goto out_unlock;
6364
0c8916c3
SR
6365 list_del(&tr->list);
6366
6b450d25 6367 tracing_set_nop(tr);
0c8916c3 6368 event_trace_del_tracer(tr);
591dffda 6369 ftrace_destroy_function_files(tr);
0c8916c3 6370 debugfs_remove_recursive(tr->dir);
a9fcaaac 6371 free_trace_buffers(tr);
0c8916c3
SR
6372
6373 kfree(tr->name);
6374 kfree(tr);
6375
6376 ret = 0;
6377
6378 out_unlock:
6379 mutex_unlock(&trace_types_lock);
6380
6381 return ret;
6382}
6383
277ba044
SR
6384static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
6385{
6386 struct dentry *parent;
6387 int ret;
6388
6389 /* Paranoid: Make sure the parent is the "instances" directory */
6390 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
6391 if (WARN_ON_ONCE(parent != trace_instance_dir))
6392 return -ENOENT;
6393
6394 /*
6395 * The inode mutex is locked, but debugfs_create_dir() will also
6396 * take the mutex. As the instances directory can not be destroyed
6397 * or changed in any other way, it is safe to unlock it, and
6398 * let the dentry try. If two users try to make the same dir at
6399 * the same time, then the new_instance_create() will determine the
6400 * winner.
6401 */
6402 mutex_unlock(&inode->i_mutex);
6403
6404 ret = new_instance_create(dentry->d_iname);
6405
6406 mutex_lock(&inode->i_mutex);
6407
6408 return ret;
6409}
6410
0c8916c3
SR
6411static int instance_rmdir(struct inode *inode, struct dentry *dentry)
6412{
6413 struct dentry *parent;
6414 int ret;
6415
6416 /* Paranoid: Make sure the parent is the "instances" directory */
6417 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
6418 if (WARN_ON_ONCE(parent != trace_instance_dir))
6419 return -ENOENT;
6420
6421 /* The caller did a dget() on dentry */
6422 mutex_unlock(&dentry->d_inode->i_mutex);
6423
6424 /*
6425 * The inode mutex is locked, but debugfs_create_dir() will also
6426 * take the mutex. As the instances directory can not be destroyed
6427 * or changed in any other way, it is safe to unlock it, and
6428 * let the dentry try. If two users try to make the same dir at
6429 * the same time, then the instance_delete() will determine the
6430 * winner.
6431 */
6432 mutex_unlock(&inode->i_mutex);
6433
6434 ret = instance_delete(dentry->d_iname);
6435
6436 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
6437 mutex_lock(&dentry->d_inode->i_mutex);
6438
6439 return ret;
6440}
6441
277ba044
SR
6442static const struct inode_operations instance_dir_inode_operations = {
6443 .lookup = simple_lookup,
6444 .mkdir = instance_mkdir,
0c8916c3 6445 .rmdir = instance_rmdir,
277ba044
SR
6446};
6447
6448static __init void create_trace_instances(struct dentry *d_tracer)
6449{
6450 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
6451 if (WARN_ON(!trace_instance_dir))
6452 return;
6453
6454 /* Hijack the dir inode operations, to allow mkdir */
6455 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
6456}
6457
2b6080f2
SR
6458static void
6459init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
6460{
121aaee7 6461 int cpu;
2b6080f2 6462
607e2ea1
SRRH
6463 trace_create_file("available_tracers", 0444, d_tracer,
6464 tr, &show_traces_fops);
6465
6466 trace_create_file("current_tracer", 0644, d_tracer,
6467 tr, &set_tracer_fops);
6468
ccfe9e42
AL
6469 trace_create_file("tracing_cpumask", 0644, d_tracer,
6470 tr, &tracing_cpumask_fops);
6471
2b6080f2
SR
6472 trace_create_file("trace_options", 0644, d_tracer,
6473 tr, &tracing_iter_fops);
6474
6475 trace_create_file("trace", 0644, d_tracer,
6484c71c 6476 tr, &tracing_fops);
2b6080f2
SR
6477
6478 trace_create_file("trace_pipe", 0444, d_tracer,
15544209 6479 tr, &tracing_pipe_fops);
2b6080f2
SR
6480
6481 trace_create_file("buffer_size_kb", 0644, d_tracer,
0bc392ee 6482 tr, &tracing_entries_fops);
2b6080f2
SR
6483
6484 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
6485 tr, &tracing_total_entries_fops);
6486
238ae93d 6487 trace_create_file("free_buffer", 0200, d_tracer,
2b6080f2
SR
6488 tr, &tracing_free_buffer_fops);
6489
6490 trace_create_file("trace_marker", 0220, d_tracer,
6491 tr, &tracing_mark_fops);
6492
6493 trace_create_file("trace_clock", 0644, d_tracer, tr,
6494 &trace_clock_fops);
6495
6496 trace_create_file("tracing_on", 0644, d_tracer,
6484c71c 6497 tr, &rb_simple_fops);
ce9bae55 6498
6d9b3fa5
SRRH
6499#ifdef CONFIG_TRACER_MAX_TRACE
6500 trace_create_file("tracing_max_latency", 0644, d_tracer,
6501 &tr->max_latency, &tracing_max_lat_fops);
6502#endif
6503
591dffda
SRRH
6504 if (ftrace_create_function_files(tr, d_tracer))
6505 WARN(1, "Could not allocate function filter files");
6506
ce9bae55
SRRH
6507#ifdef CONFIG_TRACER_SNAPSHOT
6508 trace_create_file("snapshot", 0644, d_tracer,
6484c71c 6509 tr, &snapshot_fops);
ce9bae55 6510#endif
121aaee7
SRRH
6511
6512 for_each_tracing_cpu(cpu)
6513 tracing_init_debugfs_percpu(tr, cpu);
6514
2b6080f2
SR
6515}
6516
b5ad384e 6517static __init int tracer_init_debugfs(void)
bc0c38d1
SR
6518{
6519 struct dentry *d_tracer;
bc0c38d1 6520
7e53bd42
LJ
6521 trace_access_lock_init();
6522
bc0c38d1 6523 d_tracer = tracing_init_dentry();
ed6f1c99
NK
6524 if (!d_tracer)
6525 return 0;
bc0c38d1 6526
2b6080f2 6527 init_tracer_debugfs(&global_trace, d_tracer);
bc0c38d1 6528
5452af66
FW
6529 trace_create_file("tracing_thresh", 0644, d_tracer,
6530 &tracing_thresh, &tracing_max_lat_fops);
a8259075 6531
339ae5d3 6532 trace_create_file("README", 0444, d_tracer,
5452af66
FW
6533 NULL, &tracing_readme_fops);
6534
69abe6a5
AP
6535 trace_create_file("saved_cmdlines", 0444, d_tracer,
6536 NULL, &tracing_saved_cmdlines_fops);
5bf9a1ee 6537
939c7a4f
YY
6538 trace_create_file("saved_cmdlines_size", 0644, d_tracer,
6539 NULL, &tracing_saved_cmdlines_size_fops);
6540
bc0c38d1 6541#ifdef CONFIG_DYNAMIC_FTRACE
5452af66
FW
6542 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
6543 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
bc0c38d1 6544#endif
b04cc6b1 6545
277ba044 6546 create_trace_instances(d_tracer);
5452af66 6547
2b6080f2 6548 create_trace_options_dir(&global_trace);
b04cc6b1 6549
b5ad384e 6550 return 0;
bc0c38d1
SR
6551}
6552
3f5a54e3
SR
6553static int trace_panic_handler(struct notifier_block *this,
6554 unsigned long event, void *unused)
6555{
944ac425 6556 if (ftrace_dump_on_oops)
cecbca96 6557 ftrace_dump(ftrace_dump_on_oops);
3f5a54e3
SR
6558 return NOTIFY_OK;
6559}
6560
6561static struct notifier_block trace_panic_notifier = {
6562 .notifier_call = trace_panic_handler,
6563 .next = NULL,
6564 .priority = 150 /* priority: INT_MAX >= x >= 0 */
6565};
6566
6567static int trace_die_handler(struct notifier_block *self,
6568 unsigned long val,
6569 void *data)
6570{
6571 switch (val) {
6572 case DIE_OOPS:
944ac425 6573 if (ftrace_dump_on_oops)
cecbca96 6574 ftrace_dump(ftrace_dump_on_oops);
3f5a54e3
SR
6575 break;
6576 default:
6577 break;
6578 }
6579 return NOTIFY_OK;
6580}
6581
6582static struct notifier_block trace_die_notifier = {
6583 .notifier_call = trace_die_handler,
6584 .priority = 200
6585};
6586
6587/*
6588 * printk is set to max of 1024, we really don't need it that big.
6589 * Nothing should be printing 1000 characters anyway.
6590 */
6591#define TRACE_MAX_PRINT 1000
6592
6593/*
6594 * Define here KERN_TRACE so that we have one place to modify
6595 * it if we decide to change what log level the ftrace dump
6596 * should be at.
6597 */
428aee14 6598#define KERN_TRACE KERN_EMERG
3f5a54e3 6599
955b61e5 6600void
3f5a54e3
SR
6601trace_printk_seq(struct trace_seq *s)
6602{
6603 /* Probably should print a warning here. */
bd6df187
J
6604 if (s->len >= TRACE_MAX_PRINT)
6605 s->len = TRACE_MAX_PRINT;
3f5a54e3
SR
6606
6607 /* should be zero ended, but we are paranoid. */
6608 s->buffer[s->len] = 0;
6609
6610 printk(KERN_TRACE "%s", s->buffer);
6611
f9520750 6612 trace_seq_init(s);
3f5a54e3
SR
6613}
6614
955b61e5
JW
6615void trace_init_global_iter(struct trace_iterator *iter)
6616{
6617 iter->tr = &global_trace;
2b6080f2 6618 iter->trace = iter->tr->current_trace;
ae3b5093 6619 iter->cpu_file = RING_BUFFER_ALL_CPUS;
12883efb 6620 iter->trace_buffer = &global_trace.trace_buffer;
b2f974d6
CS
6621
6622 if (iter->trace && iter->trace->open)
6623 iter->trace->open(iter);
6624
6625 /* Annotate start of buffers if we had overruns */
6626 if (ring_buffer_overruns(iter->trace_buffer->buffer))
6627 iter->iter_flags |= TRACE_FILE_ANNOTATE;
6628
6629 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
6630 if (trace_clocks[iter->tr->clock_id].in_ns)
6631 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
955b61e5
JW
6632}
6633
7fe70b57 6634void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
3f5a54e3 6635{
3f5a54e3
SR
6636 /* use static because iter can be a bit big for the stack */
6637 static struct trace_iterator iter;
7fe70b57 6638 static atomic_t dump_running;
cf586b61 6639 unsigned int old_userobj;
d769041f
SR
6640 unsigned long flags;
6641 int cnt = 0, cpu;
3f5a54e3 6642
7fe70b57
SRRH
6643 /* Only allow one dump user at a time. */
6644 if (atomic_inc_return(&dump_running) != 1) {
6645 atomic_dec(&dump_running);
6646 return;
6647 }
3f5a54e3 6648
7fe70b57
SRRH
6649 /*
6650 * Always turn off tracing when we dump.
6651 * We don't need to show trace output of what happens
6652 * between multiple crashes.
6653 *
6654 * If the user does a sysrq-z, then they can re-enable
6655 * tracing with echo 1 > tracing_on.
6656 */
0ee6b6cf 6657 tracing_off();
cf586b61 6658
7fe70b57 6659 local_irq_save(flags);
3f5a54e3 6660
38dbe0b1 6661 /* Simulate the iterator */
955b61e5
JW
6662 trace_init_global_iter(&iter);
6663
d769041f 6664 for_each_tracing_cpu(cpu) {
12883efb 6665 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
d769041f
SR
6666 }
6667
cf586b61
FW
6668 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
6669
b54d3de9
TE
6670 /* don't look at user memory in panic mode */
6671 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
6672
cecbca96
FW
6673 switch (oops_dump_mode) {
6674 case DUMP_ALL:
ae3b5093 6675 iter.cpu_file = RING_BUFFER_ALL_CPUS;
cecbca96
FW
6676 break;
6677 case DUMP_ORIG:
6678 iter.cpu_file = raw_smp_processor_id();
6679 break;
6680 case DUMP_NONE:
6681 goto out_enable;
6682 default:
6683 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
ae3b5093 6684 iter.cpu_file = RING_BUFFER_ALL_CPUS;
cecbca96
FW
6685 }
6686
6687 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3f5a54e3 6688
7fe70b57
SRRH
6689 /* Did function tracer already get disabled? */
6690 if (ftrace_is_dead()) {
6691 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
6692 printk("# MAY BE MISSING FUNCTION EVENTS\n");
6693 }
6694
3f5a54e3
SR
6695 /*
6696 * We need to stop all tracing on all CPUS to read the
6697 * the next buffer. This is a bit expensive, but is
6698 * not done often. We fill all what we can read,
6699 * and then release the locks again.
6700 */
6701
3f5a54e3
SR
6702 while (!trace_empty(&iter)) {
6703
6704 if (!cnt)
6705 printk(KERN_TRACE "---------------------------------\n");
6706
6707 cnt++;
6708
6709 /* reset all but tr, trace, and overruns */
6710 memset(&iter.seq, 0,
6711 sizeof(struct trace_iterator) -
6712 offsetof(struct trace_iterator, seq));
6713 iter.iter_flags |= TRACE_FILE_LAT_FMT;
6714 iter.pos = -1;
6715
955b61e5 6716 if (trace_find_next_entry_inc(&iter) != NULL) {
74e7ff8c
LJ
6717 int ret;
6718
6719 ret = print_trace_line(&iter);
6720 if (ret != TRACE_TYPE_NO_CONSUME)
6721 trace_consume(&iter);
3f5a54e3 6722 }
b892e5c8 6723 touch_nmi_watchdog();
3f5a54e3
SR
6724
6725 trace_printk_seq(&iter.seq);
6726 }
6727
6728 if (!cnt)
6729 printk(KERN_TRACE " (ftrace buffer empty)\n");
6730 else
6731 printk(KERN_TRACE "---------------------------------\n");
6732
cecbca96 6733 out_enable:
7fe70b57 6734 trace_flags |= old_userobj;
cf586b61 6735
7fe70b57
SRRH
6736 for_each_tracing_cpu(cpu) {
6737 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
cf586b61 6738 }
7fe70b57 6739 atomic_dec(&dump_running);
cd891ae0 6740 local_irq_restore(flags);
3f5a54e3 6741}
a8eecf22 6742EXPORT_SYMBOL_GPL(ftrace_dump);
cf586b61 6743
3928a8a2 6744__init static int tracer_alloc_buffers(void)
bc0c38d1 6745{
73c5162a 6746 int ring_buf_size;
9e01c1b7 6747 int ret = -ENOMEM;
4c11d7ae 6748
750912fa 6749
9e01c1b7
RR
6750 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
6751 goto out;
6752
ccfe9e42 6753 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
9e01c1b7 6754 goto out_free_buffer_mask;
4c11d7ae 6755
07d777fe
SR
6756 /* Only allocate trace_printk buffers if a trace_printk exists */
6757 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
81698831 6758 /* Must be called before global_trace.buffer is allocated */
07d777fe
SR
6759 trace_printk_init_buffers();
6760
73c5162a
SR
6761 /* To save memory, keep the ring buffer size to its minimum */
6762 if (ring_buffer_expanded)
6763 ring_buf_size = trace_buf_size;
6764 else
6765 ring_buf_size = 1;
6766
9e01c1b7 6767 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
ccfe9e42 6768 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
9e01c1b7 6769
2b6080f2
SR
6770 raw_spin_lock_init(&global_trace.start_lock);
6771
2c4a33ab
SRRH
6772 /* Used for event triggers */
6773 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
6774 if (!temp_buffer)
6775 goto out_free_cpumask;
6776
939c7a4f
YY
6777 if (trace_create_savedcmd() < 0)
6778 goto out_free_temp_buffer;
6779
9e01c1b7 6780 /* TODO: make the number of buffers hot pluggable with CPUS */
737223fb 6781 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
3928a8a2
SR
6782 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
6783 WARN_ON(1);
939c7a4f 6784 goto out_free_savedcmd;
4c11d7ae 6785 }
a7603ff4 6786
499e5470
SR
6787 if (global_trace.buffer_disabled)
6788 tracing_off();
4c11d7ae 6789
e1e232ca
SR
6790 if (trace_boot_clock) {
6791 ret = tracing_set_clock(&global_trace, trace_boot_clock);
6792 if (ret < 0)
6793 pr_warning("Trace clock %s not defined, going back to default\n",
6794 trace_boot_clock);
6795 }
6796
ca164318
SRRH
6797 /*
6798 * register_tracer() might reference current_trace, so it
6799 * needs to be set before we register anything. This is
6800 * just a bootstrap of current_trace anyway.
6801 */
2b6080f2
SR
6802 global_trace.current_trace = &nop_trace;
6803
0b9b12c1
SRRH
6804 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
6805
4104d326
SRRH
6806 ftrace_init_global_array_ops(&global_trace);
6807
ca164318
SRRH
6808 register_tracer(&nop_trace);
6809
60a11774
SR
6810 /* All seems OK, enable tracing */
6811 tracing_disabled = 0;
3928a8a2 6812
3f5a54e3
SR
6813 atomic_notifier_chain_register(&panic_notifier_list,
6814 &trace_panic_notifier);
6815
6816 register_die_notifier(&trace_die_notifier);
2fc1dfbe 6817
ae63b31e
SR
6818 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
6819
6820 INIT_LIST_HEAD(&global_trace.systems);
6821 INIT_LIST_HEAD(&global_trace.events);
6822 list_add(&global_trace.list, &ftrace_trace_arrays);
6823
7bcfaf54
SR
6824 while (trace_boot_options) {
6825 char *option;
6826
6827 option = strsep(&trace_boot_options, ",");
2b6080f2 6828 trace_set_options(&global_trace, option);
7bcfaf54
SR
6829 }
6830
77fd5c15
SRRH
6831 register_snapshot_cmd();
6832
2fc1dfbe 6833 return 0;
3f5a54e3 6834
939c7a4f
YY
6835out_free_savedcmd:
6836 free_saved_cmdlines_buffer(savedcmd);
2c4a33ab
SRRH
6837out_free_temp_buffer:
6838 ring_buffer_free(temp_buffer);
9e01c1b7 6839out_free_cpumask:
ccfe9e42 6840 free_cpumask_var(global_trace.tracing_cpumask);
9e01c1b7
RR
6841out_free_buffer_mask:
6842 free_cpumask_var(tracing_buffer_mask);
6843out:
6844 return ret;
bc0c38d1 6845}
b2821ae6
SR
6846
6847__init static int clear_boot_tracer(void)
6848{
6849 /*
6850 * The default tracer at boot buffer is an init section.
6851 * This function is called in lateinit. If we did not
6852 * find the boot tracer, then clear it out, to prevent
6853 * later registration from accessing the buffer that is
6854 * about to be freed.
6855 */
6856 if (!default_bootup_tracer)
6857 return 0;
6858
6859 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6860 default_bootup_tracer);
6861 default_bootup_tracer = NULL;
6862
6863 return 0;
6864}
6865
b5ad384e
FW
6866early_initcall(tracer_alloc_buffers);
6867fs_initcall(tracer_init_debugfs);
b2821ae6 6868late_initcall(clear_boot_tracer);