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