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