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