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