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