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