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