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