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