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