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