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