]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/trace/trace.c
tracing: Use appropriate perl constructs in recordmcount.pl
[net-next-2.6.git] / kernel / trace / trace.c
CommitLineData
bc0c38d1
SR
1/*
2 * ring buffer based function tracer
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
13 */
2cadf913 14#include <linux/ring_buffer.h>
273b281f 15#include <generated/utsrelease.h>
2cadf913
SR
16#include <linux/stacktrace.h>
17#include <linux/writeback.h>
bc0c38d1
SR
18#include <linux/kallsyms.h>
19#include <linux/seq_file.h>
405f5571 20#include <linux/smp_lock.h>
3f5a54e3 21#include <linux/notifier.h>
2cadf913 22#include <linux/irqflags.h>
bc0c38d1 23#include <linux/debugfs.h>
4c11d7ae 24#include <linux/pagemap.h>
bc0c38d1
SR
25#include <linux/hardirq.h>
26#include <linux/linkage.h>
27#include <linux/uaccess.h>
2cadf913 28#include <linux/kprobes.h>
bc0c38d1
SR
29#include <linux/ftrace.h>
30#include <linux/module.h>
31#include <linux/percpu.h>
2cadf913 32#include <linux/splice.h>
3f5a54e3 33#include <linux/kdebug.h>
5f0c6c03 34#include <linux/string.h>
7e53bd42 35#include <linux/rwsem.h>
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
4ca53085
SR
1031 if (pid > PID_MAX_DEFAULT) {
1032 strcpy(comm, "<...>");
1033 return;
1034 }
bc0c38d1 1035
5b6045a9 1036 preempt_disable();
0199c4e6 1037 arch_spin_lock(&trace_cmdline_lock);
bc0c38d1 1038 map = map_pid_to_cmdline[pid];
50d88758
TG
1039 if (map != NO_CMDLINE_MAP)
1040 strcpy(comm, saved_cmdlines[map]);
1041 else
1042 strcpy(comm, "<...>");
bc0c38d1 1043
0199c4e6 1044 arch_spin_unlock(&trace_cmdline_lock);
5b6045a9 1045 preempt_enable();
bc0c38d1
SR
1046}
1047
e309b41d 1048void tracing_record_cmdline(struct task_struct *tsk)
bc0c38d1 1049{
18aecd36
TG
1050 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1051 !tracing_is_on())
bc0c38d1
SR
1052 return;
1053
1054 trace_save_cmdline(tsk);
1055}
1056
45dcd8b8 1057void
38697053
SR
1058tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1059 int pc)
bc0c38d1
SR
1060{
1061 struct task_struct *tsk = current;
bc0c38d1 1062
777e208d
SR
1063 entry->preempt_count = pc & 0xff;
1064 entry->pid = (tsk) ? tsk->pid : 0;
637e7e86 1065 entry->lock_depth = (tsk) ? tsk->lock_depth : 0;
777e208d 1066 entry->flags =
9244489a 1067#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
2e2ca155 1068 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
9244489a
SR
1069#else
1070 TRACE_FLAG_IRQS_NOSUPPORT |
1071#endif
bc0c38d1
SR
1072 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1073 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1074 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1075}
f413cdb8 1076EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
bc0c38d1 1077
e77405ad
SR
1078struct ring_buffer_event *
1079trace_buffer_lock_reserve(struct ring_buffer *buffer,
1080 int type,
1081 unsigned long len,
1082 unsigned long flags, int pc)
51a763dd
ACM
1083{
1084 struct ring_buffer_event *event;
1085
e77405ad 1086 event = ring_buffer_lock_reserve(buffer, len);
51a763dd
ACM
1087 if (event != NULL) {
1088 struct trace_entry *ent = ring_buffer_event_data(event);
1089
1090 tracing_generic_entry_update(ent, flags, pc);
1091 ent->type = type;
1092 }
1093
1094 return event;
1095}
51a763dd 1096
e77405ad
SR
1097static inline void
1098__trace_buffer_unlock_commit(struct ring_buffer *buffer,
1099 struct ring_buffer_event *event,
1100 unsigned long flags, int pc,
1101 int wake)
51a763dd 1102{
e77405ad 1103 ring_buffer_unlock_commit(buffer, event);
51a763dd 1104
e77405ad
SR
1105 ftrace_trace_stack(buffer, flags, 6, pc);
1106 ftrace_trace_userstack(buffer, flags, pc);
07edf712
FW
1107
1108 if (wake)
1109 trace_wake_up();
1110}
1111
e77405ad
SR
1112void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1113 struct ring_buffer_event *event,
1114 unsigned long flags, int pc)
07edf712 1115{
e77405ad 1116 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
51a763dd
ACM
1117}
1118
ef5580d0 1119struct ring_buffer_event *
e77405ad
SR
1120trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1121 int type, unsigned long len,
ef5580d0
SR
1122 unsigned long flags, int pc)
1123{
e77405ad
SR
1124 *current_rb = global_trace.buffer;
1125 return trace_buffer_lock_reserve(*current_rb,
ef5580d0
SR
1126 type, len, flags, pc);
1127}
94487d6d 1128EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
ef5580d0 1129
e77405ad
SR
1130void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1131 struct ring_buffer_event *event,
ef5580d0
SR
1132 unsigned long flags, int pc)
1133{
e77405ad 1134 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
07edf712 1135}
94487d6d 1136EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
07edf712 1137
e77405ad
SR
1138void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1139 struct ring_buffer_event *event,
1140 unsigned long flags, int pc)
07edf712 1141{
e77405ad 1142 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
77d9f465 1143}
94487d6d 1144EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
77d9f465 1145
e77405ad
SR
1146void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1147 struct ring_buffer_event *event)
77d9f465 1148{
e77405ad 1149 ring_buffer_discard_commit(buffer, event);
ef5580d0 1150}
12acd473 1151EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
ef5580d0 1152
e309b41d 1153void
7be42151 1154trace_function(struct trace_array *tr,
38697053
SR
1155 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1156 int pc)
bc0c38d1 1157{
e1112b4d 1158 struct ftrace_event_call *call = &event_function;
e77405ad 1159 struct ring_buffer *buffer = tr->buffer;
3928a8a2 1160 struct ring_buffer_event *event;
777e208d 1161 struct ftrace_entry *entry;
bc0c38d1 1162
d769041f 1163 /* If we are reading the ring buffer, don't trace */
9288f99a 1164 if (unlikely(__this_cpu_read(per_cpu_var(ftrace_cpu_disabled))))
d769041f
SR
1165 return;
1166
e77405ad 1167 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
51a763dd 1168 flags, pc);
3928a8a2
SR
1169 if (!event)
1170 return;
1171 entry = ring_buffer_event_data(event);
777e208d
SR
1172 entry->ip = ip;
1173 entry->parent_ip = parent_ip;
e1112b4d 1174
e77405ad
SR
1175 if (!filter_check_discard(call, entry, buffer, event))
1176 ring_buffer_unlock_commit(buffer, event);
bc0c38d1
SR
1177}
1178
e309b41d 1179void
2e0f5761 1180ftrace(struct trace_array *tr, struct trace_array_cpu *data,
38697053
SR
1181 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1182 int pc)
2e0f5761
IM
1183{
1184 if (likely(!atomic_read(&data->disabled)))
7be42151 1185 trace_function(tr, ip, parent_ip, flags, pc);
2e0f5761
IM
1186}
1187
c0a0d0d3 1188#ifdef CONFIG_STACKTRACE
e77405ad 1189static void __ftrace_trace_stack(struct ring_buffer *buffer,
53614991
SR
1190 unsigned long flags,
1191 int skip, int pc)
86387f7e 1192{
e1112b4d 1193 struct ftrace_event_call *call = &event_kernel_stack;
3928a8a2 1194 struct ring_buffer_event *event;
777e208d 1195 struct stack_entry *entry;
86387f7e
IM
1196 struct stack_trace trace;
1197
e77405ad 1198 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
51a763dd 1199 sizeof(*entry), flags, pc);
3928a8a2
SR
1200 if (!event)
1201 return;
1202 entry = ring_buffer_event_data(event);
777e208d 1203 memset(&entry->caller, 0, sizeof(entry->caller));
86387f7e
IM
1204
1205 trace.nr_entries = 0;
1206 trace.max_entries = FTRACE_STACK_ENTRIES;
1207 trace.skip = skip;
777e208d 1208 trace.entries = entry->caller;
86387f7e
IM
1209
1210 save_stack_trace(&trace);
e77405ad
SR
1211 if (!filter_check_discard(call, entry, buffer, event))
1212 ring_buffer_unlock_commit(buffer, event);
f0a920d5
IM
1213}
1214
e77405ad
SR
1215void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1216 int skip, int pc)
53614991
SR
1217{
1218 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1219 return;
1220
e77405ad 1221 __ftrace_trace_stack(buffer, flags, skip, pc);
53614991
SR
1222}
1223
c0a0d0d3
FW
1224void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1225 int pc)
38697053 1226{
e77405ad 1227 __ftrace_trace_stack(tr->buffer, flags, skip, pc);
38697053
SR
1228}
1229
03889384
SR
1230/**
1231 * trace_dump_stack - record a stack back trace in the trace buffer
1232 */
1233void trace_dump_stack(void)
1234{
1235 unsigned long flags;
1236
1237 if (tracing_disabled || tracing_selftest_running)
e36c5458 1238 return;
03889384
SR
1239
1240 local_save_flags(flags);
1241
1242 /* skipping 3 traces, seems to get us at the caller of this function */
1243 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
1244}
1245
e77405ad
SR
1246void
1247ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
02b67518 1248{
e1112b4d 1249 struct ftrace_event_call *call = &event_user_stack;
8d7c6a96 1250 struct ring_buffer_event *event;
02b67518
TE
1251 struct userstack_entry *entry;
1252 struct stack_trace trace;
02b67518
TE
1253
1254 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1255 return;
1256
e77405ad 1257 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
51a763dd 1258 sizeof(*entry), flags, pc);
02b67518
TE
1259 if (!event)
1260 return;
1261 entry = ring_buffer_event_data(event);
02b67518 1262
48659d31 1263 entry->tgid = current->tgid;
02b67518
TE
1264 memset(&entry->caller, 0, sizeof(entry->caller));
1265
1266 trace.nr_entries = 0;
1267 trace.max_entries = FTRACE_STACK_ENTRIES;
1268 trace.skip = 0;
1269 trace.entries = entry->caller;
1270
1271 save_stack_trace_user(&trace);
e77405ad
SR
1272 if (!filter_check_discard(call, entry, buffer, event))
1273 ring_buffer_unlock_commit(buffer, event);
02b67518
TE
1274}
1275
4fd27358
HE
1276#ifdef UNUSED
1277static void __trace_userstack(struct trace_array *tr, unsigned long flags)
02b67518 1278{
7be42151 1279 ftrace_trace_userstack(tr, flags, preempt_count());
02b67518 1280}
4fd27358 1281#endif /* UNUSED */
02b67518 1282
c0a0d0d3
FW
1283#endif /* CONFIG_STACKTRACE */
1284
38697053 1285static void
7be42151 1286ftrace_trace_special(void *__tr,
38697053
SR
1287 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1288 int pc)
a4feb834 1289{
60ba7702 1290 struct ftrace_event_call *call = &event_special;
3928a8a2 1291 struct ring_buffer_event *event;
a4feb834 1292 struct trace_array *tr = __tr;
e77405ad 1293 struct ring_buffer *buffer = tr->buffer;
777e208d 1294 struct special_entry *entry;
a4feb834 1295
e77405ad 1296 event = trace_buffer_lock_reserve(buffer, TRACE_SPECIAL,
51a763dd 1297 sizeof(*entry), 0, pc);
3928a8a2
SR
1298 if (!event)
1299 return;
1300 entry = ring_buffer_event_data(event);
777e208d
SR
1301 entry->arg1 = arg1;
1302 entry->arg2 = arg2;
1303 entry->arg3 = arg3;
60ba7702
SR
1304
1305 if (!filter_check_discard(call, entry, buffer, event))
1306 trace_buffer_unlock_commit(buffer, event, 0, pc);
a4feb834
IM
1307}
1308
38697053
SR
1309void
1310__trace_special(void *__tr, void *__data,
1311 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1312{
7be42151 1313 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
38697053
SR
1314}
1315
4902f884
SR
1316void
1317ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1318{
1319 struct trace_array *tr = &global_trace;
1320 struct trace_array_cpu *data;
5aa1ba6a 1321 unsigned long flags;
4902f884 1322 int cpu;
38697053 1323 int pc;
4902f884 1324
c76f0694 1325 if (tracing_disabled)
4902f884
SR
1326 return;
1327
38697053 1328 pc = preempt_count();
5aa1ba6a 1329 local_irq_save(flags);
4902f884
SR
1330 cpu = raw_smp_processor_id();
1331 data = tr->data[cpu];
4902f884 1332
5aa1ba6a 1333 if (likely(atomic_inc_return(&data->disabled) == 1))
7be42151 1334 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
4902f884 1335
5aa1ba6a
SR
1336 atomic_dec(&data->disabled);
1337 local_irq_restore(flags);
4902f884
SR
1338}
1339
769b0441 1340/**
48ead020 1341 * trace_vbprintk - write binary msg to tracing buffer
769b0441
FW
1342 *
1343 */
40ce74f1 1344int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
769b0441 1345{
445c8951 1346 static arch_spinlock_t trace_buf_lock =
edc35bd7 1347 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
769b0441
FW
1348 static u32 trace_buf[TRACE_BUF_SIZE];
1349
e1112b4d 1350 struct ftrace_event_call *call = &event_bprint;
769b0441 1351 struct ring_buffer_event *event;
e77405ad 1352 struct ring_buffer *buffer;
769b0441
FW
1353 struct trace_array *tr = &global_trace;
1354 struct trace_array_cpu *data;
48ead020 1355 struct bprint_entry *entry;
769b0441 1356 unsigned long flags;
3189cdb3 1357 int disable;
769b0441
FW
1358 int resched;
1359 int cpu, len = 0, size, pc;
1360
1361 if (unlikely(tracing_selftest_running || tracing_disabled))
1362 return 0;
1363
1364 /* Don't pollute graph traces with trace_vprintk internals */
1365 pause_graph_tracing();
1366
1367 pc = preempt_count();
1368 resched = ftrace_preempt_disable();
1369 cpu = raw_smp_processor_id();
1370 data = tr->data[cpu];
1371
3189cdb3
SR
1372 disable = atomic_inc_return(&data->disabled);
1373 if (unlikely(disable != 1))
769b0441
FW
1374 goto out;
1375
80370cb7
SR
1376 /* Lockdep uses trace_printk for lock tracing */
1377 local_irq_save(flags);
0199c4e6 1378 arch_spin_lock(&trace_buf_lock);
769b0441
FW
1379 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1380
1381 if (len > TRACE_BUF_SIZE || len < 0)
1382 goto out_unlock;
1383
1384 size = sizeof(*entry) + sizeof(u32) * len;
e77405ad
SR
1385 buffer = tr->buffer;
1386 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1387 flags, pc);
769b0441
FW
1388 if (!event)
1389 goto out_unlock;
1390 entry = ring_buffer_event_data(event);
1391 entry->ip = ip;
769b0441
FW
1392 entry->fmt = fmt;
1393
1394 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
e77405ad
SR
1395 if (!filter_check_discard(call, entry, buffer, event))
1396 ring_buffer_unlock_commit(buffer, event);
769b0441
FW
1397
1398out_unlock:
0199c4e6 1399 arch_spin_unlock(&trace_buf_lock);
80370cb7 1400 local_irq_restore(flags);
769b0441
FW
1401
1402out:
3189cdb3 1403 atomic_dec_return(&data->disabled);
769b0441
FW
1404 ftrace_preempt_enable(resched);
1405 unpause_graph_tracing();
1406
1407 return len;
1408}
48ead020
FW
1409EXPORT_SYMBOL_GPL(trace_vbprintk);
1410
659372d3
SR
1411int trace_array_printk(struct trace_array *tr,
1412 unsigned long ip, const char *fmt, ...)
1413{
1414 int ret;
1415 va_list ap;
1416
1417 if (!(trace_flags & TRACE_ITER_PRINTK))
1418 return 0;
1419
1420 va_start(ap, fmt);
1421 ret = trace_array_vprintk(tr, ip, fmt, ap);
1422 va_end(ap);
1423 return ret;
1424}
1425
1426int trace_array_vprintk(struct trace_array *tr,
1427 unsigned long ip, const char *fmt, va_list args)
48ead020 1428{
edc35bd7 1429 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
48ead020
FW
1430 static char trace_buf[TRACE_BUF_SIZE];
1431
e1112b4d 1432 struct ftrace_event_call *call = &event_print;
48ead020 1433 struct ring_buffer_event *event;
e77405ad 1434 struct ring_buffer *buffer;
48ead020
FW
1435 struct trace_array_cpu *data;
1436 int cpu, len = 0, size, pc;
1437 struct print_entry *entry;
1438 unsigned long irq_flags;
3189cdb3 1439 int disable;
48ead020
FW
1440
1441 if (tracing_disabled || tracing_selftest_running)
1442 return 0;
1443
1444 pc = preempt_count();
1445 preempt_disable_notrace();
1446 cpu = raw_smp_processor_id();
1447 data = tr->data[cpu];
1448
3189cdb3
SR
1449 disable = atomic_inc_return(&data->disabled);
1450 if (unlikely(disable != 1))
48ead020
FW
1451 goto out;
1452
1453 pause_graph_tracing();
1454 raw_local_irq_save(irq_flags);
0199c4e6 1455 arch_spin_lock(&trace_buf_lock);
f2942487 1456 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
48ead020
FW
1457
1458 size = sizeof(*entry) + len + 1;
e77405ad
SR
1459 buffer = tr->buffer;
1460 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1461 irq_flags, pc);
48ead020
FW
1462 if (!event)
1463 goto out_unlock;
1464 entry = ring_buffer_event_data(event);
c13d2f7c 1465 entry->ip = ip;
48ead020
FW
1466
1467 memcpy(&entry->buf, trace_buf, len);
c13d2f7c 1468 entry->buf[len] = '\0';
e77405ad
SR
1469 if (!filter_check_discard(call, entry, buffer, event))
1470 ring_buffer_unlock_commit(buffer, event);
48ead020
FW
1471
1472 out_unlock:
0199c4e6 1473 arch_spin_unlock(&trace_buf_lock);
48ead020
FW
1474 raw_local_irq_restore(irq_flags);
1475 unpause_graph_tracing();
1476 out:
3189cdb3 1477 atomic_dec_return(&data->disabled);
48ead020
FW
1478 preempt_enable_notrace();
1479
1480 return len;
1481}
659372d3
SR
1482
1483int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1484{
a813a159 1485 return trace_array_vprintk(&global_trace, ip, fmt, args);
659372d3 1486}
769b0441
FW
1487EXPORT_SYMBOL_GPL(trace_vprintk);
1488
bc0c38d1
SR
1489enum trace_file_type {
1490 TRACE_FILE_LAT_FMT = 1,
12ef7d44 1491 TRACE_FILE_ANNOTATE = 2,
bc0c38d1
SR
1492};
1493
e2ac8ef5 1494static void trace_iterator_increment(struct trace_iterator *iter)
5a90f577 1495{
d769041f
SR
1496 /* Don't allow ftrace to trace into the ring buffers */
1497 ftrace_disable_cpu();
1498
5a90f577 1499 iter->idx++;
d769041f
SR
1500 if (iter->buffer_iter[iter->cpu])
1501 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1502
1503 ftrace_enable_cpu();
5a90f577
SR
1504}
1505
e309b41d 1506static struct trace_entry *
3928a8a2 1507peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
dd0e545f 1508{
3928a8a2
SR
1509 struct ring_buffer_event *event;
1510 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
dd0e545f 1511
d769041f
SR
1512 /* Don't allow ftrace to trace into the ring buffers */
1513 ftrace_disable_cpu();
1514
1515 if (buf_iter)
1516 event = ring_buffer_iter_peek(buf_iter, ts);
1517 else
1518 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1519
1520 ftrace_enable_cpu();
1521
3928a8a2 1522 return event ? ring_buffer_event_data(event) : NULL;
dd0e545f 1523}
d769041f 1524
dd0e545f 1525static struct trace_entry *
3928a8a2 1526__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
bc0c38d1 1527{
3928a8a2 1528 struct ring_buffer *buffer = iter->tr->buffer;
bc0c38d1 1529 struct trace_entry *ent, *next = NULL;
b04cc6b1 1530 int cpu_file = iter->cpu_file;
3928a8a2 1531 u64 next_ts = 0, ts;
bc0c38d1
SR
1532 int next_cpu = -1;
1533 int cpu;
1534
b04cc6b1
FW
1535 /*
1536 * If we are in a per_cpu trace file, don't bother by iterating over
1537 * all cpu and peek directly.
1538 */
1539 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1540 if (ring_buffer_empty_cpu(buffer, cpu_file))
1541 return NULL;
1542 ent = peek_next_entry(iter, cpu_file, ent_ts);
1543 if (ent_cpu)
1544 *ent_cpu = cpu_file;
1545
1546 return ent;
1547 }
1548
ab46428c 1549 for_each_tracing_cpu(cpu) {
dd0e545f 1550
3928a8a2
SR
1551 if (ring_buffer_empty_cpu(buffer, cpu))
1552 continue;
dd0e545f 1553
3928a8a2 1554 ent = peek_next_entry(iter, cpu, &ts);
dd0e545f 1555
cdd31cd2
IM
1556 /*
1557 * Pick the entry with the smallest timestamp:
1558 */
3928a8a2 1559 if (ent && (!next || ts < next_ts)) {
bc0c38d1
SR
1560 next = ent;
1561 next_cpu = cpu;
3928a8a2 1562 next_ts = ts;
bc0c38d1
SR
1563 }
1564 }
1565
1566 if (ent_cpu)
1567 *ent_cpu = next_cpu;
1568
3928a8a2
SR
1569 if (ent_ts)
1570 *ent_ts = next_ts;
1571
bc0c38d1
SR
1572 return next;
1573}
1574
dd0e545f 1575/* Find the next real entry, without updating the iterator itself */
c4a8e8be
FW
1576struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1577 int *ent_cpu, u64 *ent_ts)
bc0c38d1 1578{
3928a8a2 1579 return __find_next_entry(iter, ent_cpu, ent_ts);
dd0e545f
SR
1580}
1581
1582/* Find the next real entry, and increment the iterator to the next entry */
1583static void *find_next_entry_inc(struct trace_iterator *iter)
1584{
3928a8a2 1585 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
dd0e545f 1586
3928a8a2 1587 if (iter->ent)
e2ac8ef5 1588 trace_iterator_increment(iter);
dd0e545f 1589
3928a8a2 1590 return iter->ent ? iter : NULL;
b3806b43 1591}
bc0c38d1 1592
e309b41d 1593static void trace_consume(struct trace_iterator *iter)
b3806b43 1594{
d769041f
SR
1595 /* Don't allow ftrace to trace into the ring buffers */
1596 ftrace_disable_cpu();
3928a8a2 1597 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
d769041f 1598 ftrace_enable_cpu();
bc0c38d1
SR
1599}
1600
e309b41d 1601static void *s_next(struct seq_file *m, void *v, loff_t *pos)
bc0c38d1
SR
1602{
1603 struct trace_iterator *iter = m->private;
bc0c38d1 1604 int i = (int)*pos;
4e3c3333 1605 void *ent;
bc0c38d1 1606
a63ce5b3
SR
1607 WARN_ON_ONCE(iter->leftover);
1608
bc0c38d1
SR
1609 (*pos)++;
1610
1611 /* can't go backwards */
1612 if (iter->idx > i)
1613 return NULL;
1614
1615 if (iter->idx < 0)
1616 ent = find_next_entry_inc(iter);
1617 else
1618 ent = iter;
1619
1620 while (ent && iter->idx < i)
1621 ent = find_next_entry_inc(iter);
1622
1623 iter->pos = *pos;
1624
bc0c38d1
SR
1625 return ent;
1626}
1627
2f26ebd5
SR
1628static void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1629{
1630 struct trace_array *tr = iter->tr;
1631 struct ring_buffer_event *event;
1632 struct ring_buffer_iter *buf_iter;
1633 unsigned long entries = 0;
1634 u64 ts;
1635
1636 tr->data[cpu]->skipped_entries = 0;
1637
1638 if (!iter->buffer_iter[cpu])
1639 return;
1640
1641 buf_iter = iter->buffer_iter[cpu];
1642 ring_buffer_iter_reset(buf_iter);
1643
1644 /*
1645 * We could have the case with the max latency tracers
1646 * that a reset never took place on a cpu. This is evident
1647 * by the timestamp being before the start of the buffer.
1648 */
1649 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1650 if (ts >= iter->tr->time_start)
1651 break;
1652 entries++;
1653 ring_buffer_read(buf_iter, NULL);
1654 }
1655
1656 tr->data[cpu]->skipped_entries = entries;
1657}
1658
d7350c3f 1659/*
d7350c3f
FW
1660 * The current tracer is copied to avoid a global locking
1661 * all around.
1662 */
bc0c38d1
SR
1663static void *s_start(struct seq_file *m, loff_t *pos)
1664{
1665 struct trace_iterator *iter = m->private;
d7350c3f 1666 static struct tracer *old_tracer;
b04cc6b1 1667 int cpu_file = iter->cpu_file;
bc0c38d1
SR
1668 void *p = NULL;
1669 loff_t l = 0;
3928a8a2 1670 int cpu;
bc0c38d1 1671
d7350c3f 1672 /* copy the tracer to avoid using a global lock all around */
bc0c38d1 1673 mutex_lock(&trace_types_lock);
d7350c3f
FW
1674 if (unlikely(old_tracer != current_trace && current_trace)) {
1675 old_tracer = current_trace;
1676 *iter->trace = *current_trace;
d15f57f2 1677 }
d7350c3f 1678 mutex_unlock(&trace_types_lock);
bc0c38d1
SR
1679
1680 atomic_inc(&trace_record_cmdline_disabled);
1681
bc0c38d1
SR
1682 if (*pos != iter->pos) {
1683 iter->ent = NULL;
1684 iter->cpu = 0;
1685 iter->idx = -1;
1686
d769041f
SR
1687 ftrace_disable_cpu();
1688
b04cc6b1
FW
1689 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1690 for_each_tracing_cpu(cpu)
2f26ebd5 1691 tracing_iter_reset(iter, cpu);
b04cc6b1 1692 } else
2f26ebd5 1693 tracing_iter_reset(iter, cpu_file);
bc0c38d1 1694
d769041f
SR
1695 ftrace_enable_cpu();
1696
bc0c38d1
SR
1697 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1698 ;
1699
1700 } else {
a63ce5b3
SR
1701 /*
1702 * If we overflowed the seq_file before, then we want
1703 * to just reuse the trace_seq buffer again.
1704 */
1705 if (iter->leftover)
1706 p = iter;
1707 else {
1708 l = *pos - 1;
1709 p = s_next(m, p, &l);
1710 }
bc0c38d1
SR
1711 }
1712
4f535968 1713 trace_event_read_lock();
7e53bd42 1714 trace_access_lock(cpu_file);
bc0c38d1
SR
1715 return p;
1716}
1717
1718static void s_stop(struct seq_file *m, void *p)
1719{
7e53bd42
LJ
1720 struct trace_iterator *iter = m->private;
1721
bc0c38d1 1722 atomic_dec(&trace_record_cmdline_disabled);
7e53bd42 1723 trace_access_unlock(iter->cpu_file);
4f535968 1724 trace_event_read_unlock();
bc0c38d1
SR
1725}
1726
e309b41d 1727static void print_lat_help_header(struct seq_file *m)
bc0c38d1 1728{
a6168353
ME
1729 seq_puts(m, "# _------=> CPU# \n");
1730 seq_puts(m, "# / _-----=> irqs-off \n");
1731 seq_puts(m, "# | / _----=> need-resched \n");
1732 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1733 seq_puts(m, "# ||| / _--=> preempt-depth \n");
637e7e86
SR
1734 seq_puts(m, "# |||| /_--=> lock-depth \n");
1735 seq_puts(m, "# |||||/ delay \n");
1736 seq_puts(m, "# cmd pid |||||| time | caller \n");
1737 seq_puts(m, "# \\ / |||||| \\ | / \n");
bc0c38d1
SR
1738}
1739
e309b41d 1740static void print_func_help_header(struct seq_file *m)
bc0c38d1 1741{
a6168353
ME
1742 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1743 seq_puts(m, "# | | | | |\n");
bc0c38d1
SR
1744}
1745
1746
e309b41d 1747static void
bc0c38d1
SR
1748print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1749{
1750 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1751 struct trace_array *tr = iter->tr;
1752 struct trace_array_cpu *data = tr->data[tr->cpu];
1753 struct tracer *type = current_trace;
2f26ebd5
SR
1754 unsigned long entries = 0;
1755 unsigned long total = 0;
1756 unsigned long count;
bc0c38d1 1757 const char *name = "preemption";
2f26ebd5 1758 int cpu;
bc0c38d1
SR
1759
1760 if (type)
1761 name = type->name;
1762
2f26ebd5
SR
1763
1764 for_each_tracing_cpu(cpu) {
1765 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1766 /*
1767 * If this buffer has skipped entries, then we hold all
1768 * entries for the trace and we need to ignore the
1769 * ones before the time stamp.
1770 */
1771 if (tr->data[cpu]->skipped_entries) {
1772 count -= tr->data[cpu]->skipped_entries;
1773 /* total is the same as the entries */
1774 total += count;
1775 } else
1776 total += count +
1777 ring_buffer_overrun_cpu(tr->buffer, cpu);
1778 entries += count;
1779 }
bc0c38d1 1780
888b55dc 1781 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
bc0c38d1 1782 name, UTS_RELEASE);
888b55dc 1783 seq_puts(m, "# -----------------------------------"
bc0c38d1 1784 "---------------------------------\n");
888b55dc 1785 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
bc0c38d1 1786 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
57f50be1 1787 nsecs_to_usecs(data->saved_latency),
bc0c38d1 1788 entries,
4c11d7ae 1789 total,
bc0c38d1
SR
1790 tr->cpu,
1791#if defined(CONFIG_PREEMPT_NONE)
1792 "server",
1793#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1794 "desktop",
b5c21b45 1795#elif defined(CONFIG_PREEMPT)
bc0c38d1
SR
1796 "preempt",
1797#else
1798 "unknown",
1799#endif
1800 /* These are reserved for later use */
1801 0, 0, 0, 0);
1802#ifdef CONFIG_SMP
1803 seq_printf(m, " #P:%d)\n", num_online_cpus());
1804#else
1805 seq_puts(m, ")\n");
1806#endif
888b55dc
KM
1807 seq_puts(m, "# -----------------\n");
1808 seq_printf(m, "# | task: %.16s-%d "
bc0c38d1
SR
1809 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1810 data->comm, data->pid, data->uid, data->nice,
1811 data->policy, data->rt_priority);
888b55dc 1812 seq_puts(m, "# -----------------\n");
bc0c38d1
SR
1813
1814 if (data->critical_start) {
888b55dc 1815 seq_puts(m, "# => started at: ");
214023c3
SR
1816 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1817 trace_print_seq(m, &iter->seq);
888b55dc 1818 seq_puts(m, "\n# => ended at: ");
214023c3
SR
1819 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1820 trace_print_seq(m, &iter->seq);
8248ac05 1821 seq_puts(m, "\n#\n");
bc0c38d1
SR
1822 }
1823
888b55dc 1824 seq_puts(m, "#\n");
bc0c38d1
SR
1825}
1826
a309720c
SR
1827static void test_cpu_buff_start(struct trace_iterator *iter)
1828{
1829 struct trace_seq *s = &iter->seq;
1830
12ef7d44
SR
1831 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1832 return;
1833
1834 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1835 return;
1836
4462344e 1837 if (cpumask_test_cpu(iter->cpu, iter->started))
a309720c
SR
1838 return;
1839
2f26ebd5
SR
1840 if (iter->tr->data[iter->cpu]->skipped_entries)
1841 return;
1842
4462344e 1843 cpumask_set_cpu(iter->cpu, iter->started);
b0dfa978
FW
1844
1845 /* Don't print started cpu buffer for the first entry of the trace */
1846 if (iter->idx > 1)
1847 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1848 iter->cpu);
a309720c
SR
1849}
1850
2c4f035f 1851static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 1852{
214023c3 1853 struct trace_seq *s = &iter->seq;
bc0c38d1 1854 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 1855 struct trace_entry *entry;
f633cef0 1856 struct trace_event *event;
bc0c38d1 1857
4e3c3333 1858 entry = iter->ent;
dd0e545f 1859
a309720c
SR
1860 test_cpu_buff_start(iter);
1861
c4a8e8be 1862 event = ftrace_find_event(entry->type);
bc0c38d1 1863
c4a8e8be 1864 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
27d48be8
SR
1865 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1866 if (!trace_print_lat_context(iter))
1867 goto partial;
1868 } else {
1869 if (!trace_print_context(iter))
1870 goto partial;
1871 }
c4a8e8be 1872 }
bc0c38d1 1873
268ccda0 1874 if (event)
d9793bd8
ACM
1875 return event->trace(iter, sym_flags);
1876
1877 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1878 goto partial;
02b67518 1879
2c4f035f 1880 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
1881partial:
1882 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1
SR
1883}
1884
2c4f035f 1885static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
f9896bf3
IM
1886{
1887 struct trace_seq *s = &iter->seq;
1888 struct trace_entry *entry;
f633cef0 1889 struct trace_event *event;
f9896bf3
IM
1890
1891 entry = iter->ent;
dd0e545f 1892
c4a8e8be 1893 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
d9793bd8
ACM
1894 if (!trace_seq_printf(s, "%d %d %llu ",
1895 entry->pid, iter->cpu, iter->ts))
1896 goto partial;
c4a8e8be 1897 }
f9896bf3 1898
f633cef0 1899 event = ftrace_find_event(entry->type);
268ccda0 1900 if (event)
d9793bd8
ACM
1901 return event->raw(iter, 0);
1902
1903 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1904 goto partial;
777e208d 1905
2c4f035f 1906 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
1907partial:
1908 return TRACE_TYPE_PARTIAL_LINE;
f9896bf3
IM
1909}
1910
2c4f035f 1911static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
5e3ca0ec
IM
1912{
1913 struct trace_seq *s = &iter->seq;
1914 unsigned char newline = '\n';
1915 struct trace_entry *entry;
f633cef0 1916 struct trace_event *event;
5e3ca0ec
IM
1917
1918 entry = iter->ent;
dd0e545f 1919
c4a8e8be
FW
1920 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1921 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1922 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1923 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1924 }
5e3ca0ec 1925
f633cef0 1926 event = ftrace_find_event(entry->type);
268ccda0 1927 if (event) {
ae7462b4 1928 enum print_line_t ret = event->hex(iter, 0);
d9793bd8
ACM
1929 if (ret != TRACE_TYPE_HANDLED)
1930 return ret;
1931 }
7104f300 1932
5e3ca0ec
IM
1933 SEQ_PUT_FIELD_RET(s, newline);
1934
2c4f035f 1935 return TRACE_TYPE_HANDLED;
5e3ca0ec
IM
1936}
1937
2c4f035f 1938static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
cb0f12aa
IM
1939{
1940 struct trace_seq *s = &iter->seq;
1941 struct trace_entry *entry;
f633cef0 1942 struct trace_event *event;
cb0f12aa
IM
1943
1944 entry = iter->ent;
dd0e545f 1945
c4a8e8be
FW
1946 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1947 SEQ_PUT_FIELD_RET(s, entry->pid);
1830b52d 1948 SEQ_PUT_FIELD_RET(s, iter->cpu);
c4a8e8be
FW
1949 SEQ_PUT_FIELD_RET(s, iter->ts);
1950 }
cb0f12aa 1951
f633cef0 1952 event = ftrace_find_event(entry->type);
268ccda0 1953 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
cb0f12aa
IM
1954}
1955
bc0c38d1
SR
1956static int trace_empty(struct trace_iterator *iter)
1957{
bc0c38d1
SR
1958 int cpu;
1959
9aba60fe
SR
1960 /* If we are looking at one CPU buffer, only check that one */
1961 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1962 cpu = iter->cpu_file;
1963 if (iter->buffer_iter[cpu]) {
1964 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1965 return 0;
1966 } else {
1967 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1968 return 0;
1969 }
1970 return 1;
1971 }
1972
ab46428c 1973 for_each_tracing_cpu(cpu) {
d769041f
SR
1974 if (iter->buffer_iter[cpu]) {
1975 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1976 return 0;
1977 } else {
1978 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1979 return 0;
1980 }
bc0c38d1 1981 }
d769041f 1982
797d3712 1983 return 1;
bc0c38d1
SR
1984}
1985
4f535968 1986/* Called with trace_event_read_lock() held. */
2c4f035f 1987static enum print_line_t print_trace_line(struct trace_iterator *iter)
f9896bf3 1988{
2c4f035f
FW
1989 enum print_line_t ret;
1990
1991 if (iter->trace && iter->trace->print_line) {
1992 ret = iter->trace->print_line(iter);
1993 if (ret != TRACE_TYPE_UNHANDLED)
1994 return ret;
1995 }
72829bc3 1996
48ead020
FW
1997 if (iter->ent->type == TRACE_BPRINT &&
1998 trace_flags & TRACE_ITER_PRINTK &&
1999 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 2000 return trace_print_bprintk_msg_only(iter);
48ead020 2001
66896a85
FW
2002 if (iter->ent->type == TRACE_PRINT &&
2003 trace_flags & TRACE_ITER_PRINTK &&
2004 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 2005 return trace_print_printk_msg_only(iter);
66896a85 2006
cb0f12aa
IM
2007 if (trace_flags & TRACE_ITER_BIN)
2008 return print_bin_fmt(iter);
2009
5e3ca0ec
IM
2010 if (trace_flags & TRACE_ITER_HEX)
2011 return print_hex_fmt(iter);
2012
f9896bf3
IM
2013 if (trace_flags & TRACE_ITER_RAW)
2014 return print_raw_fmt(iter);
2015
f9896bf3
IM
2016 return print_trace_fmt(iter);
2017}
2018
bc0c38d1
SR
2019static int s_show(struct seq_file *m, void *v)
2020{
2021 struct trace_iterator *iter = v;
a63ce5b3 2022 int ret;
bc0c38d1
SR
2023
2024 if (iter->ent == NULL) {
2025 if (iter->tr) {
2026 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2027 seq_puts(m, "#\n");
2028 }
8bba1bf5
MM
2029 if (iter->trace && iter->trace->print_header)
2030 iter->trace->print_header(m);
2031 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
bc0c38d1
SR
2032 /* print nothing if the buffers are empty */
2033 if (trace_empty(iter))
2034 return 0;
2035 print_trace_header(m, iter);
2036 if (!(trace_flags & TRACE_ITER_VERBOSE))
2037 print_lat_help_header(m);
2038 } else {
2039 if (!(trace_flags & TRACE_ITER_VERBOSE))
2040 print_func_help_header(m);
2041 }
a63ce5b3
SR
2042 } else if (iter->leftover) {
2043 /*
2044 * If we filled the seq_file buffer earlier, we
2045 * want to just show it now.
2046 */
2047 ret = trace_print_seq(m, &iter->seq);
2048
2049 /* ret should this time be zero, but you never know */
2050 iter->leftover = ret;
2051
bc0c38d1 2052 } else {
f9896bf3 2053 print_trace_line(iter);
a63ce5b3
SR
2054 ret = trace_print_seq(m, &iter->seq);
2055 /*
2056 * If we overflow the seq_file buffer, then it will
2057 * ask us for this data again at start up.
2058 * Use that instead.
2059 * ret is 0 if seq_file write succeeded.
2060 * -1 otherwise.
2061 */
2062 iter->leftover = ret;
bc0c38d1
SR
2063 }
2064
2065 return 0;
2066}
2067
88e9d34c 2068static const struct seq_operations tracer_seq_ops = {
4bf39a94
IM
2069 .start = s_start,
2070 .next = s_next,
2071 .stop = s_stop,
2072 .show = s_show,
bc0c38d1
SR
2073};
2074
e309b41d 2075static struct trace_iterator *
85a2f9b4 2076__tracing_open(struct inode *inode, struct file *file)
bc0c38d1 2077{
b04cc6b1 2078 long cpu_file = (long) inode->i_private;
85a2f9b4 2079 void *fail_ret = ERR_PTR(-ENOMEM);
bc0c38d1 2080 struct trace_iterator *iter;
3928a8a2 2081 struct seq_file *m;
85a2f9b4 2082 int cpu, ret;
bc0c38d1 2083
85a2f9b4
SR
2084 if (tracing_disabled)
2085 return ERR_PTR(-ENODEV);
60a11774 2086
bc0c38d1 2087 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
85a2f9b4
SR
2088 if (!iter)
2089 return ERR_PTR(-ENOMEM);
bc0c38d1 2090
d7350c3f
FW
2091 /*
2092 * We make a copy of the current tracer to avoid concurrent
2093 * changes on it while we are reading.
2094 */
bc0c38d1 2095 mutex_lock(&trace_types_lock);
d7350c3f 2096 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
85a2f9b4 2097 if (!iter->trace)
d7350c3f 2098 goto fail;
85a2f9b4 2099
d7350c3f
FW
2100 if (current_trace)
2101 *iter->trace = *current_trace;
2102
79f55997 2103 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
b0dfa978
FW
2104 goto fail;
2105
bc0c38d1
SR
2106 if (current_trace && current_trace->print_max)
2107 iter->tr = &max_tr;
2108 else
b04cc6b1 2109 iter->tr = &global_trace;
bc0c38d1 2110 iter->pos = -1;
d7350c3f 2111 mutex_init(&iter->mutex);
b04cc6b1 2112 iter->cpu_file = cpu_file;
bc0c38d1 2113
8bba1bf5
MM
2114 /* Notify the tracer early; before we stop tracing. */
2115 if (iter->trace && iter->trace->open)
a93751ca 2116 iter->trace->open(iter);
8bba1bf5 2117
12ef7d44
SR
2118 /* Annotate start of buffers if we had overruns */
2119 if (ring_buffer_overruns(iter->tr->buffer))
2120 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2121
2f26ebd5
SR
2122 /* stop the trace while dumping */
2123 tracing_stop();
2124
b04cc6b1
FW
2125 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2126 for_each_tracing_cpu(cpu) {
12ef7d44 2127
b04cc6b1
FW
2128 iter->buffer_iter[cpu] =
2129 ring_buffer_read_start(iter->tr->buffer, cpu);
2f26ebd5 2130 tracing_iter_reset(iter, cpu);
b04cc6b1
FW
2131 }
2132 } else {
2133 cpu = iter->cpu_file;
3928a8a2 2134 iter->buffer_iter[cpu] =
b04cc6b1 2135 ring_buffer_read_start(iter->tr->buffer, cpu);
2f26ebd5 2136 tracing_iter_reset(iter, cpu);
3928a8a2
SR
2137 }
2138
85a2f9b4
SR
2139 ret = seq_open(file, &tracer_seq_ops);
2140 if (ret < 0) {
2141 fail_ret = ERR_PTR(ret);
3928a8a2 2142 goto fail_buffer;
85a2f9b4 2143 }
bc0c38d1 2144
3928a8a2
SR
2145 m = file->private_data;
2146 m->private = iter;
bc0c38d1 2147
bc0c38d1
SR
2148 mutex_unlock(&trace_types_lock);
2149
bc0c38d1 2150 return iter;
3928a8a2
SR
2151
2152 fail_buffer:
2153 for_each_tracing_cpu(cpu) {
2154 if (iter->buffer_iter[cpu])
2155 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2156 }
b0dfa978 2157 free_cpumask_var(iter->started);
2f26ebd5 2158 tracing_start();
d7350c3f 2159 fail:
3928a8a2 2160 mutex_unlock(&trace_types_lock);
d7350c3f 2161 kfree(iter->trace);
0bb943c7 2162 kfree(iter);
3928a8a2 2163
85a2f9b4 2164 return fail_ret;
bc0c38d1
SR
2165}
2166
2167int tracing_open_generic(struct inode *inode, struct file *filp)
2168{
60a11774
SR
2169 if (tracing_disabled)
2170 return -ENODEV;
2171
bc0c38d1
SR
2172 filp->private_data = inode->i_private;
2173 return 0;
2174}
2175
4fd27358 2176static int tracing_release(struct inode *inode, struct file *file)
bc0c38d1
SR
2177{
2178 struct seq_file *m = (struct seq_file *)file->private_data;
4acd4d00 2179 struct trace_iterator *iter;
3928a8a2 2180 int cpu;
bc0c38d1 2181
4acd4d00
SR
2182 if (!(file->f_mode & FMODE_READ))
2183 return 0;
2184
2185 iter = m->private;
2186
bc0c38d1 2187 mutex_lock(&trace_types_lock);
3928a8a2
SR
2188 for_each_tracing_cpu(cpu) {
2189 if (iter->buffer_iter[cpu])
2190 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2191 }
2192
bc0c38d1
SR
2193 if (iter->trace && iter->trace->close)
2194 iter->trace->close(iter);
2195
2196 /* reenable tracing if it was previously enabled */
9036990d 2197 tracing_start();
bc0c38d1
SR
2198 mutex_unlock(&trace_types_lock);
2199
2200 seq_release(inode, file);
d7350c3f 2201 mutex_destroy(&iter->mutex);
b0dfa978 2202 free_cpumask_var(iter->started);
d7350c3f 2203 kfree(iter->trace);
bc0c38d1
SR
2204 kfree(iter);
2205 return 0;
2206}
2207
2208static int tracing_open(struct inode *inode, struct file *file)
2209{
85a2f9b4
SR
2210 struct trace_iterator *iter;
2211 int ret = 0;
bc0c38d1 2212
4acd4d00
SR
2213 /* If this file was open for write, then erase contents */
2214 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 2215 (file->f_flags & O_TRUNC)) {
4acd4d00 2216 long cpu = (long) inode->i_private;
bc0c38d1 2217
4acd4d00
SR
2218 if (cpu == TRACE_PIPE_ALL_CPU)
2219 tracing_reset_online_cpus(&global_trace);
2220 else
2221 tracing_reset(&global_trace, cpu);
2222 }
bc0c38d1 2223
4acd4d00
SR
2224 if (file->f_mode & FMODE_READ) {
2225 iter = __tracing_open(inode, file);
2226 if (IS_ERR(iter))
2227 ret = PTR_ERR(iter);
2228 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2229 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2230 }
bc0c38d1
SR
2231 return ret;
2232}
2233
e309b41d 2234static void *
bc0c38d1
SR
2235t_next(struct seq_file *m, void *v, loff_t *pos)
2236{
f129e965 2237 struct tracer *t = v;
bc0c38d1
SR
2238
2239 (*pos)++;
2240
2241 if (t)
2242 t = t->next;
2243
bc0c38d1
SR
2244 return t;
2245}
2246
2247static void *t_start(struct seq_file *m, loff_t *pos)
2248{
f129e965 2249 struct tracer *t;
bc0c38d1
SR
2250 loff_t l = 0;
2251
2252 mutex_lock(&trace_types_lock);
f129e965 2253 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
bc0c38d1
SR
2254 ;
2255
2256 return t;
2257}
2258
2259static void t_stop(struct seq_file *m, void *p)
2260{
2261 mutex_unlock(&trace_types_lock);
2262}
2263
2264static int t_show(struct seq_file *m, void *v)
2265{
2266 struct tracer *t = v;
2267
2268 if (!t)
2269 return 0;
2270
2271 seq_printf(m, "%s", t->name);
2272 if (t->next)
2273 seq_putc(m, ' ');
2274 else
2275 seq_putc(m, '\n');
2276
2277 return 0;
2278}
2279
88e9d34c 2280static const struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
2281 .start = t_start,
2282 .next = t_next,
2283 .stop = t_stop,
2284 .show = t_show,
bc0c38d1
SR
2285};
2286
2287static int show_traces_open(struct inode *inode, struct file *file)
2288{
60a11774
SR
2289 if (tracing_disabled)
2290 return -ENODEV;
2291
f129e965 2292 return seq_open(file, &show_traces_seq_ops);
bc0c38d1
SR
2293}
2294
4acd4d00
SR
2295static ssize_t
2296tracing_write_stub(struct file *filp, const char __user *ubuf,
2297 size_t count, loff_t *ppos)
2298{
2299 return count;
2300}
2301
5e2336a0 2302static const struct file_operations tracing_fops = {
4bf39a94
IM
2303 .open = tracing_open,
2304 .read = seq_read,
4acd4d00 2305 .write = tracing_write_stub,
4bf39a94
IM
2306 .llseek = seq_lseek,
2307 .release = tracing_release,
bc0c38d1
SR
2308};
2309
5e2336a0 2310static const struct file_operations show_traces_fops = {
c7078de1
IM
2311 .open = show_traces_open,
2312 .read = seq_read,
2313 .release = seq_release,
2314};
2315
36dfe925
IM
2316/*
2317 * Only trace on a CPU if the bitmask is set:
2318 */
9e01c1b7 2319static cpumask_var_t tracing_cpumask;
36dfe925
IM
2320
2321/*
2322 * The tracer itself will not take this lock, but still we want
2323 * to provide a consistent cpumask to user-space:
2324 */
2325static DEFINE_MUTEX(tracing_cpumask_update_lock);
2326
2327/*
2328 * Temporary storage for the character representation of the
2329 * CPU bitmask (and one more byte for the newline):
2330 */
2331static char mask_str[NR_CPUS + 1];
2332
c7078de1
IM
2333static ssize_t
2334tracing_cpumask_read(struct file *filp, char __user *ubuf,
2335 size_t count, loff_t *ppos)
2336{
36dfe925 2337 int len;
c7078de1
IM
2338
2339 mutex_lock(&tracing_cpumask_update_lock);
36dfe925 2340
9e01c1b7 2341 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
36dfe925
IM
2342 if (count - len < 2) {
2343 count = -EINVAL;
2344 goto out_err;
2345 }
2346 len += sprintf(mask_str + len, "\n");
2347 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2348
2349out_err:
c7078de1
IM
2350 mutex_unlock(&tracing_cpumask_update_lock);
2351
2352 return count;
2353}
2354
2355static ssize_t
2356tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2357 size_t count, loff_t *ppos)
2358{
36dfe925 2359 int err, cpu;
9e01c1b7
RR
2360 cpumask_var_t tracing_cpumask_new;
2361
2362 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2363 return -ENOMEM;
c7078de1 2364
9e01c1b7 2365 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
c7078de1 2366 if (err)
36dfe925
IM
2367 goto err_unlock;
2368
215368e8
LZ
2369 mutex_lock(&tracing_cpumask_update_lock);
2370
a5e25883 2371 local_irq_disable();
0199c4e6 2372 arch_spin_lock(&ftrace_max_lock);
ab46428c 2373 for_each_tracing_cpu(cpu) {
36dfe925
IM
2374 /*
2375 * Increase/decrease the disabled counter if we are
2376 * about to flip a bit in the cpumask:
2377 */
9e01c1b7
RR
2378 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2379 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
36dfe925
IM
2380 atomic_inc(&global_trace.data[cpu]->disabled);
2381 }
9e01c1b7
RR
2382 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2383 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
36dfe925
IM
2384 atomic_dec(&global_trace.data[cpu]->disabled);
2385 }
2386 }
0199c4e6 2387 arch_spin_unlock(&ftrace_max_lock);
a5e25883 2388 local_irq_enable();
36dfe925 2389
9e01c1b7 2390 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
36dfe925
IM
2391
2392 mutex_unlock(&tracing_cpumask_update_lock);
9e01c1b7 2393 free_cpumask_var(tracing_cpumask_new);
c7078de1
IM
2394
2395 return count;
36dfe925
IM
2396
2397err_unlock:
215368e8 2398 free_cpumask_var(tracing_cpumask_new);
36dfe925
IM
2399
2400 return err;
c7078de1
IM
2401}
2402
5e2336a0 2403static const struct file_operations tracing_cpumask_fops = {
c7078de1
IM
2404 .open = tracing_open_generic,
2405 .read = tracing_cpumask_read,
2406 .write = tracing_cpumask_write,
bc0c38d1
SR
2407};
2408
fdb372ed 2409static int tracing_trace_options_show(struct seq_file *m, void *v)
bc0c38d1 2410{
d8e83d26
SR
2411 struct tracer_opt *trace_opts;
2412 u32 tracer_flags;
d8e83d26 2413 int i;
adf9f195 2414
d8e83d26
SR
2415 mutex_lock(&trace_types_lock);
2416 tracer_flags = current_trace->flags->val;
2417 trace_opts = current_trace->flags->opts;
2418
bc0c38d1
SR
2419 for (i = 0; trace_options[i]; i++) {
2420 if (trace_flags & (1 << i))
fdb372ed 2421 seq_printf(m, "%s\n", trace_options[i]);
bc0c38d1 2422 else
fdb372ed 2423 seq_printf(m, "no%s\n", trace_options[i]);
bc0c38d1
SR
2424 }
2425
adf9f195
FW
2426 for (i = 0; trace_opts[i].name; i++) {
2427 if (tracer_flags & trace_opts[i].bit)
fdb372ed 2428 seq_printf(m, "%s\n", trace_opts[i].name);
adf9f195 2429 else
fdb372ed 2430 seq_printf(m, "no%s\n", trace_opts[i].name);
adf9f195 2431 }
d8e83d26 2432 mutex_unlock(&trace_types_lock);
adf9f195 2433
fdb372ed 2434 return 0;
bc0c38d1 2435}
bc0c38d1 2436
8d18eaaf
LZ
2437static int __set_tracer_option(struct tracer *trace,
2438 struct tracer_flags *tracer_flags,
2439 struct tracer_opt *opts, int neg)
2440{
2441 int ret;
bc0c38d1 2442
8d18eaaf
LZ
2443 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2444 if (ret)
2445 return ret;
2446
2447 if (neg)
2448 tracer_flags->val &= ~opts->bit;
2449 else
2450 tracer_flags->val |= opts->bit;
2451 return 0;
bc0c38d1
SR
2452}
2453
adf9f195
FW
2454/* Try to assign a tracer specific option */
2455static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2456{
7770841e 2457 struct tracer_flags *tracer_flags = trace->flags;
adf9f195 2458 struct tracer_opt *opts = NULL;
8d18eaaf 2459 int i;
adf9f195 2460
7770841e
Z
2461 for (i = 0; tracer_flags->opts[i].name; i++) {
2462 opts = &tracer_flags->opts[i];
adf9f195 2463
8d18eaaf
LZ
2464 if (strcmp(cmp, opts->name) == 0)
2465 return __set_tracer_option(trace, trace->flags,
2466 opts, neg);
adf9f195 2467 }
adf9f195 2468
8d18eaaf 2469 return -EINVAL;
adf9f195
FW
2470}
2471
af4617bd
SR
2472static void set_tracer_flags(unsigned int mask, int enabled)
2473{
2474 /* do nothing if flag is already set */
2475 if (!!(trace_flags & mask) == !!enabled)
2476 return;
2477
2478 if (enabled)
2479 trace_flags |= mask;
2480 else
2481 trace_flags &= ~mask;
af4617bd
SR
2482}
2483
bc0c38d1 2484static ssize_t
ee6bce52 2485tracing_trace_options_write(struct file *filp, const char __user *ubuf,
bc0c38d1
SR
2486 size_t cnt, loff_t *ppos)
2487{
2488 char buf[64];
8d18eaaf 2489 char *cmp;
bc0c38d1 2490 int neg = 0;
adf9f195 2491 int ret;
bc0c38d1
SR
2492 int i;
2493
cffae437
SR
2494 if (cnt >= sizeof(buf))
2495 return -EINVAL;
bc0c38d1
SR
2496
2497 if (copy_from_user(&buf, ubuf, cnt))
2498 return -EFAULT;
2499
2500 buf[cnt] = 0;
8d18eaaf 2501 cmp = strstrip(buf);
bc0c38d1 2502
8d18eaaf 2503 if (strncmp(cmp, "no", 2) == 0) {
bc0c38d1
SR
2504 neg = 1;
2505 cmp += 2;
2506 }
2507
2508 for (i = 0; trace_options[i]; i++) {
8d18eaaf 2509 if (strcmp(cmp, trace_options[i]) == 0) {
af4617bd 2510 set_tracer_flags(1 << i, !neg);
bc0c38d1
SR
2511 break;
2512 }
2513 }
adf9f195
FW
2514
2515 /* If no option could be set, test the specific tracer options */
2516 if (!trace_options[i]) {
d8e83d26 2517 mutex_lock(&trace_types_lock);
adf9f195 2518 ret = set_tracer_option(current_trace, cmp, neg);
d8e83d26 2519 mutex_unlock(&trace_types_lock);
adf9f195
FW
2520 if (ret)
2521 return ret;
2522 }
bc0c38d1 2523
cf8517cf 2524 *ppos += cnt;
bc0c38d1
SR
2525
2526 return cnt;
2527}
2528
fdb372ed
LZ
2529static int tracing_trace_options_open(struct inode *inode, struct file *file)
2530{
2531 if (tracing_disabled)
2532 return -ENODEV;
2533 return single_open(file, tracing_trace_options_show, NULL);
2534}
2535
5e2336a0 2536static const struct file_operations tracing_iter_fops = {
fdb372ed
LZ
2537 .open = tracing_trace_options_open,
2538 .read = seq_read,
2539 .llseek = seq_lseek,
2540 .release = single_release,
ee6bce52 2541 .write = tracing_trace_options_write,
bc0c38d1
SR
2542};
2543
7bd2f24c
IM
2544static const char readme_msg[] =
2545 "tracing mini-HOWTO:\n\n"
156f5a78
GL
2546 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2547 "# cat /sys/kernel/debug/tracing/available_tracers\n"
bc2b6871 2548 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
156f5a78 2549 "# cat /sys/kernel/debug/tracing/current_tracer\n"
bc2b6871 2550 "nop\n"
156f5a78
GL
2551 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2552 "# cat /sys/kernel/debug/tracing/current_tracer\n"
7bd2f24c 2553 "sched_switch\n"
156f5a78 2554 "# cat /sys/kernel/debug/tracing/trace_options\n"
7bd2f24c 2555 "noprint-parent nosym-offset nosym-addr noverbose\n"
156f5a78
GL
2556 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2557 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2558 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2559 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
7bd2f24c
IM
2560;
2561
2562static ssize_t
2563tracing_readme_read(struct file *filp, char __user *ubuf,
2564 size_t cnt, loff_t *ppos)
2565{
2566 return simple_read_from_buffer(ubuf, cnt, ppos,
2567 readme_msg, strlen(readme_msg));
2568}
2569
5e2336a0 2570static const struct file_operations tracing_readme_fops = {
c7078de1
IM
2571 .open = tracing_open_generic,
2572 .read = tracing_readme_read,
7bd2f24c
IM
2573};
2574
69abe6a5
AP
2575static ssize_t
2576tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2577 size_t cnt, loff_t *ppos)
2578{
2579 char *buf_comm;
2580 char *file_buf;
2581 char *buf;
2582 int len = 0;
2583 int pid;
2584 int i;
2585
2586 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2587 if (!file_buf)
2588 return -ENOMEM;
2589
2590 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2591 if (!buf_comm) {
2592 kfree(file_buf);
2593 return -ENOMEM;
2594 }
2595
2596 buf = file_buf;
2597
2598 for (i = 0; i < SAVED_CMDLINES; i++) {
2599 int r;
2600
2601 pid = map_cmdline_to_pid[i];
2602 if (pid == -1 || pid == NO_CMDLINE_MAP)
2603 continue;
2604
2605 trace_find_cmdline(pid, buf_comm);
2606 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2607 buf += r;
2608 len += r;
2609 }
2610
2611 len = simple_read_from_buffer(ubuf, cnt, ppos,
2612 file_buf, len);
2613
2614 kfree(file_buf);
2615 kfree(buf_comm);
2616
2617 return len;
2618}
2619
2620static const struct file_operations tracing_saved_cmdlines_fops = {
2621 .open = tracing_open_generic,
2622 .read = tracing_saved_cmdlines_read,
2623};
2624
bc0c38d1
SR
2625static ssize_t
2626tracing_ctrl_read(struct file *filp, char __user *ubuf,
2627 size_t cnt, loff_t *ppos)
2628{
bc0c38d1
SR
2629 char buf[64];
2630 int r;
2631
9036990d 2632 r = sprintf(buf, "%u\n", tracer_enabled);
4e3c3333 2633 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2634}
2635
2636static ssize_t
2637tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2638 size_t cnt, loff_t *ppos)
2639{
2640 struct trace_array *tr = filp->private_data;
bc0c38d1 2641 char buf[64];
5e39841c 2642 unsigned long val;
c6caeeb1 2643 int ret;
bc0c38d1 2644
cffae437
SR
2645 if (cnt >= sizeof(buf))
2646 return -EINVAL;
bc0c38d1
SR
2647
2648 if (copy_from_user(&buf, ubuf, cnt))
2649 return -EFAULT;
2650
2651 buf[cnt] = 0;
2652
c6caeeb1
SR
2653 ret = strict_strtoul(buf, 10, &val);
2654 if (ret < 0)
2655 return ret;
bc0c38d1
SR
2656
2657 val = !!val;
2658
2659 mutex_lock(&trace_types_lock);
9036990d
SR
2660 if (tracer_enabled ^ val) {
2661 if (val) {
bc0c38d1 2662 tracer_enabled = 1;
9036990d
SR
2663 if (current_trace->start)
2664 current_trace->start(tr);
2665 tracing_start();
2666 } else {
bc0c38d1 2667 tracer_enabled = 0;
9036990d
SR
2668 tracing_stop();
2669 if (current_trace->stop)
2670 current_trace->stop(tr);
2671 }
bc0c38d1
SR
2672 }
2673 mutex_unlock(&trace_types_lock);
2674
cf8517cf 2675 *ppos += cnt;
bc0c38d1
SR
2676
2677 return cnt;
2678}
2679
2680static ssize_t
2681tracing_set_trace_read(struct file *filp, char __user *ubuf,
2682 size_t cnt, loff_t *ppos)
2683{
ee6c2c1b 2684 char buf[MAX_TRACER_SIZE+2];
bc0c38d1
SR
2685 int r;
2686
2687 mutex_lock(&trace_types_lock);
2688 if (current_trace)
2689 r = sprintf(buf, "%s\n", current_trace->name);
2690 else
2691 r = sprintf(buf, "\n");
2692 mutex_unlock(&trace_types_lock);
2693
4bf39a94 2694 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2695}
2696
b6f11df2
ACM
2697int tracer_init(struct tracer *t, struct trace_array *tr)
2698{
2699 tracing_reset_online_cpus(tr);
2700 return t->init(tr);
2701}
2702
73c5162a
SR
2703static int tracing_resize_ring_buffer(unsigned long size)
2704{
2705 int ret;
2706
2707 /*
2708 * If kernel or user changes the size of the ring buffer
a123c52b
SR
2709 * we use the size that was given, and we can forget about
2710 * expanding it later.
73c5162a
SR
2711 */
2712 ring_buffer_expanded = 1;
2713
2714 ret = ring_buffer_resize(global_trace.buffer, size);
2715 if (ret < 0)
2716 return ret;
2717
2718 ret = ring_buffer_resize(max_tr.buffer, size);
2719 if (ret < 0) {
2720 int r;
2721
2722 r = ring_buffer_resize(global_trace.buffer,
2723 global_trace.entries);
2724 if (r < 0) {
a123c52b
SR
2725 /*
2726 * AARGH! We are left with different
2727 * size max buffer!!!!
2728 * The max buffer is our "snapshot" buffer.
2729 * When a tracer needs a snapshot (one of the
2730 * latency tracers), it swaps the max buffer
2731 * with the saved snap shot. We succeeded to
2732 * update the size of the main buffer, but failed to
2733 * update the size of the max buffer. But when we tried
2734 * to reset the main buffer to the original size, we
2735 * failed there too. This is very unlikely to
2736 * happen, but if it does, warn and kill all
2737 * tracing.
2738 */
73c5162a
SR
2739 WARN_ON(1);
2740 tracing_disabled = 1;
2741 }
2742 return ret;
2743 }
2744
2745 global_trace.entries = size;
2746
2747 return ret;
2748}
2749
1852fcce
SR
2750/**
2751 * tracing_update_buffers - used by tracing facility to expand ring buffers
2752 *
2753 * To save on memory when the tracing is never used on a system with it
2754 * configured in. The ring buffers are set to a minimum size. But once
2755 * a user starts to use the tracing facility, then they need to grow
2756 * to their default size.
2757 *
2758 * This function is to be called when a tracer is about to be used.
2759 */
2760int tracing_update_buffers(void)
2761{
2762 int ret = 0;
2763
1027fcb2 2764 mutex_lock(&trace_types_lock);
1852fcce
SR
2765 if (!ring_buffer_expanded)
2766 ret = tracing_resize_ring_buffer(trace_buf_size);
1027fcb2 2767 mutex_unlock(&trace_types_lock);
1852fcce
SR
2768
2769 return ret;
2770}
2771
577b785f
SR
2772struct trace_option_dentry;
2773
2774static struct trace_option_dentry *
2775create_trace_option_files(struct tracer *tracer);
2776
2777static void
2778destroy_trace_option_files(struct trace_option_dentry *topts);
2779
b2821ae6 2780static int tracing_set_tracer(const char *buf)
bc0c38d1 2781{
577b785f 2782 static struct trace_option_dentry *topts;
bc0c38d1
SR
2783 struct trace_array *tr = &global_trace;
2784 struct tracer *t;
d9e54076 2785 int ret = 0;
bc0c38d1 2786
1027fcb2
SR
2787 mutex_lock(&trace_types_lock);
2788
73c5162a
SR
2789 if (!ring_buffer_expanded) {
2790 ret = tracing_resize_ring_buffer(trace_buf_size);
2791 if (ret < 0)
59f586db 2792 goto out;
73c5162a
SR
2793 ret = 0;
2794 }
2795
bc0c38d1
SR
2796 for (t = trace_types; t; t = t->next) {
2797 if (strcmp(t->name, buf) == 0)
2798 break;
2799 }
c2931e05
FW
2800 if (!t) {
2801 ret = -EINVAL;
2802 goto out;
2803 }
2804 if (t == current_trace)
bc0c38d1
SR
2805 goto out;
2806
9f029e83 2807 trace_branch_disable();
bc0c38d1
SR
2808 if (current_trace && current_trace->reset)
2809 current_trace->reset(tr);
2810
577b785f
SR
2811 destroy_trace_option_files(topts);
2812
bc0c38d1 2813 current_trace = t;
577b785f
SR
2814
2815 topts = create_trace_option_files(current_trace);
2816
1c80025a 2817 if (t->init) {
b6f11df2 2818 ret = tracer_init(t, tr);
1c80025a
FW
2819 if (ret)
2820 goto out;
2821 }
bc0c38d1 2822
9f029e83 2823 trace_branch_enable(tr);
bc0c38d1
SR
2824 out:
2825 mutex_unlock(&trace_types_lock);
2826
d9e54076
PZ
2827 return ret;
2828}
2829
2830static ssize_t
2831tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2832 size_t cnt, loff_t *ppos)
2833{
ee6c2c1b 2834 char buf[MAX_TRACER_SIZE+1];
d9e54076
PZ
2835 int i;
2836 size_t ret;
e6e7a65a
FW
2837 int err;
2838
2839 ret = cnt;
d9e54076 2840
ee6c2c1b
LZ
2841 if (cnt > MAX_TRACER_SIZE)
2842 cnt = MAX_TRACER_SIZE;
d9e54076
PZ
2843
2844 if (copy_from_user(&buf, ubuf, cnt))
2845 return -EFAULT;
2846
2847 buf[cnt] = 0;
2848
2849 /* strip ending whitespace. */
2850 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2851 buf[i] = 0;
2852
e6e7a65a
FW
2853 err = tracing_set_tracer(buf);
2854 if (err)
2855 return err;
d9e54076 2856
cf8517cf 2857 *ppos += ret;
bc0c38d1 2858
c2931e05 2859 return ret;
bc0c38d1
SR
2860}
2861
2862static ssize_t
2863tracing_max_lat_read(struct file *filp, char __user *ubuf,
2864 size_t cnt, loff_t *ppos)
2865{
2866 unsigned long *ptr = filp->private_data;
2867 char buf[64];
2868 int r;
2869
cffae437 2870 r = snprintf(buf, sizeof(buf), "%ld\n",
bc0c38d1 2871 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
cffae437
SR
2872 if (r > sizeof(buf))
2873 r = sizeof(buf);
4bf39a94 2874 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2875}
2876
2877static ssize_t
2878tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2879 size_t cnt, loff_t *ppos)
2880{
5e39841c 2881 unsigned long *ptr = filp->private_data;
bc0c38d1 2882 char buf[64];
5e39841c 2883 unsigned long val;
c6caeeb1 2884 int ret;
bc0c38d1 2885
cffae437
SR
2886 if (cnt >= sizeof(buf))
2887 return -EINVAL;
bc0c38d1
SR
2888
2889 if (copy_from_user(&buf, ubuf, cnt))
2890 return -EFAULT;
2891
2892 buf[cnt] = 0;
2893
c6caeeb1
SR
2894 ret = strict_strtoul(buf, 10, &val);
2895 if (ret < 0)
2896 return ret;
bc0c38d1
SR
2897
2898 *ptr = val * 1000;
2899
2900 return cnt;
2901}
2902
b3806b43
SR
2903static int tracing_open_pipe(struct inode *inode, struct file *filp)
2904{
b04cc6b1 2905 long cpu_file = (long) inode->i_private;
b3806b43 2906 struct trace_iterator *iter;
b04cc6b1 2907 int ret = 0;
b3806b43
SR
2908
2909 if (tracing_disabled)
2910 return -ENODEV;
2911
b04cc6b1
FW
2912 mutex_lock(&trace_types_lock);
2913
b3806b43
SR
2914 /* create a buffer to store the information to pass to userspace */
2915 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
b04cc6b1
FW
2916 if (!iter) {
2917 ret = -ENOMEM;
2918 goto out;
2919 }
b3806b43 2920
d7350c3f
FW
2921 /*
2922 * We make a copy of the current tracer to avoid concurrent
2923 * changes on it while we are reading.
2924 */
2925 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2926 if (!iter->trace) {
2927 ret = -ENOMEM;
2928 goto fail;
2929 }
2930 if (current_trace)
2931 *iter->trace = *current_trace;
2932
4462344e 2933 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
b04cc6b1 2934 ret = -ENOMEM;
d7350c3f 2935 goto fail;
4462344e
RR
2936 }
2937
a309720c 2938 /* trace pipe does not show start of buffer */
4462344e 2939 cpumask_setall(iter->started);
a309720c 2940
112f38a7
SR
2941 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2942 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2943
b04cc6b1 2944 iter->cpu_file = cpu_file;
b3806b43 2945 iter->tr = &global_trace;
d7350c3f 2946 mutex_init(&iter->mutex);
b3806b43
SR
2947 filp->private_data = iter;
2948
107bad8b
SR
2949 if (iter->trace->pipe_open)
2950 iter->trace->pipe_open(iter);
107bad8b 2951
b04cc6b1
FW
2952out:
2953 mutex_unlock(&trace_types_lock);
2954 return ret;
d7350c3f
FW
2955
2956fail:
2957 kfree(iter->trace);
2958 kfree(iter);
2959 mutex_unlock(&trace_types_lock);
2960 return ret;
b3806b43
SR
2961}
2962
2963static int tracing_release_pipe(struct inode *inode, struct file *file)
2964{
2965 struct trace_iterator *iter = file->private_data;
2966
b04cc6b1
FW
2967 mutex_lock(&trace_types_lock);
2968
29bf4a5e 2969 if (iter->trace->pipe_close)
c521efd1
SR
2970 iter->trace->pipe_close(iter);
2971
b04cc6b1
FW
2972 mutex_unlock(&trace_types_lock);
2973
4462344e 2974 free_cpumask_var(iter->started);
d7350c3f
FW
2975 mutex_destroy(&iter->mutex);
2976 kfree(iter->trace);
b3806b43 2977 kfree(iter);
b3806b43
SR
2978
2979 return 0;
2980}
2981
2a2cc8f7
SSP
2982static unsigned int
2983tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2984{
2985 struct trace_iterator *iter = filp->private_data;
2986
2987 if (trace_flags & TRACE_ITER_BLOCK) {
2988 /*
2989 * Always select as readable when in blocking mode
2990 */
2991 return POLLIN | POLLRDNORM;
afc2abc0 2992 } else {
2a2cc8f7
SSP
2993 if (!trace_empty(iter))
2994 return POLLIN | POLLRDNORM;
2995 poll_wait(filp, &trace_wait, poll_table);
2996 if (!trace_empty(iter))
2997 return POLLIN | POLLRDNORM;
2998
2999 return 0;
3000 }
3001}
3002
6eaaa5d5
FW
3003
3004void default_wait_pipe(struct trace_iterator *iter)
3005{
3006 DEFINE_WAIT(wait);
3007
3008 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3009
3010 if (trace_empty(iter))
3011 schedule();
3012
3013 finish_wait(&trace_wait, &wait);
3014}
3015
3016/*
3017 * This is a make-shift waitqueue.
3018 * A tracer might use this callback on some rare cases:
3019 *
3020 * 1) the current tracer might hold the runqueue lock when it wakes up
3021 * a reader, hence a deadlock (sched, function, and function graph tracers)
3022 * 2) the function tracers, trace all functions, we don't want
3023 * the overhead of calling wake_up and friends
3024 * (and tracing them too)
3025 *
3026 * Anyway, this is really very primitive wakeup.
3027 */
3028void poll_wait_pipe(struct trace_iterator *iter)
3029{
3030 set_current_state(TASK_INTERRUPTIBLE);
3031 /* sleep for 100 msecs, and try again. */
3032 schedule_timeout(HZ / 10);
3033}
3034
ff98781b
EGM
3035/* Must be called with trace_types_lock mutex held. */
3036static int tracing_wait_pipe(struct file *filp)
b3806b43
SR
3037{
3038 struct trace_iterator *iter = filp->private_data;
b3806b43 3039
b3806b43 3040 while (trace_empty(iter)) {
2dc8f095 3041
107bad8b 3042 if ((filp->f_flags & O_NONBLOCK)) {
ff98781b 3043 return -EAGAIN;
107bad8b 3044 }
2dc8f095 3045
d7350c3f 3046 mutex_unlock(&iter->mutex);
107bad8b 3047
6eaaa5d5 3048 iter->trace->wait_pipe(iter);
b3806b43 3049
d7350c3f 3050 mutex_lock(&iter->mutex);
107bad8b 3051
6eaaa5d5 3052 if (signal_pending(current))
ff98781b 3053 return -EINTR;
b3806b43
SR
3054
3055 /*
3056 * We block until we read something and tracing is disabled.
3057 * We still block if tracing is disabled, but we have never
3058 * read anything. This allows a user to cat this file, and
3059 * then enable tracing. But after we have read something,
3060 * we give an EOF when tracing is again disabled.
3061 *
3062 * iter->pos will be 0 if we haven't read anything.
3063 */
3064 if (!tracer_enabled && iter->pos)
3065 break;
b3806b43
SR
3066 }
3067
ff98781b
EGM
3068 return 1;
3069}
3070
3071/*
3072 * Consumer reader.
3073 */
3074static ssize_t
3075tracing_read_pipe(struct file *filp, char __user *ubuf,
3076 size_t cnt, loff_t *ppos)
3077{
3078 struct trace_iterator *iter = filp->private_data;
d7350c3f 3079 static struct tracer *old_tracer;
ff98781b
EGM
3080 ssize_t sret;
3081
3082 /* return any leftover data */
3083 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3084 if (sret != -EBUSY)
3085 return sret;
3086
f9520750 3087 trace_seq_init(&iter->seq);
ff98781b 3088
d7350c3f 3089 /* copy the tracer to avoid using a global lock all around */
ff98781b 3090 mutex_lock(&trace_types_lock);
d7350c3f
FW
3091 if (unlikely(old_tracer != current_trace && current_trace)) {
3092 old_tracer = current_trace;
3093 *iter->trace = *current_trace;
3094 }
3095 mutex_unlock(&trace_types_lock);
3096
3097 /*
3098 * Avoid more than one consumer on a single file descriptor
3099 * This is just a matter of traces coherency, the ring buffer itself
3100 * is protected.
3101 */
3102 mutex_lock(&iter->mutex);
ff98781b
EGM
3103 if (iter->trace->read) {
3104 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3105 if (sret)
3106 goto out;
3107 }
3108
3109waitagain:
3110 sret = tracing_wait_pipe(filp);
3111 if (sret <= 0)
3112 goto out;
3113
b3806b43 3114 /* stop when tracing is finished */
ff98781b
EGM
3115 if (trace_empty(iter)) {
3116 sret = 0;
107bad8b 3117 goto out;
ff98781b 3118 }
b3806b43
SR
3119
3120 if (cnt >= PAGE_SIZE)
3121 cnt = PAGE_SIZE - 1;
3122
53d0aa77 3123 /* reset all but tr, trace, and overruns */
53d0aa77
SR
3124 memset(&iter->seq, 0,
3125 sizeof(struct trace_iterator) -
3126 offsetof(struct trace_iterator, seq));
4823ed7e 3127 iter->pos = -1;
b3806b43 3128
4f535968 3129 trace_event_read_lock();
7e53bd42 3130 trace_access_lock(iter->cpu_file);
088b1e42 3131 while (find_next_entry_inc(iter) != NULL) {
2c4f035f 3132 enum print_line_t ret;
088b1e42
SR
3133 int len = iter->seq.len;
3134
f9896bf3 3135 ret = print_trace_line(iter);
2c4f035f 3136 if (ret == TRACE_TYPE_PARTIAL_LINE) {
088b1e42
SR
3137 /* don't print partial lines */
3138 iter->seq.len = len;
b3806b43 3139 break;
088b1e42 3140 }
b91facc3
FW
3141 if (ret != TRACE_TYPE_NO_CONSUME)
3142 trace_consume(iter);
b3806b43
SR
3143
3144 if (iter->seq.len >= cnt)
3145 break;
b3806b43 3146 }
7e53bd42 3147 trace_access_unlock(iter->cpu_file);
4f535968 3148 trace_event_read_unlock();
b3806b43 3149
b3806b43 3150 /* Now copy what we have to the user */
6c6c2796
PP
3151 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3152 if (iter->seq.readpos >= iter->seq.len)
f9520750 3153 trace_seq_init(&iter->seq);
9ff4b974
PP
3154
3155 /*
3156 * If there was nothing to send to user, inspite of consuming trace
3157 * entries, go back to wait for more entries.
3158 */
6c6c2796 3159 if (sret == -EBUSY)
9ff4b974 3160 goto waitagain;
b3806b43 3161
107bad8b 3162out:
d7350c3f 3163 mutex_unlock(&iter->mutex);
107bad8b 3164
6c6c2796 3165 return sret;
b3806b43
SR
3166}
3167
3c56819b
EGM
3168static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3169 struct pipe_buffer *buf)
3170{
3171 __free_page(buf->page);
3172}
3173
3174static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3175 unsigned int idx)
3176{
3177 __free_page(spd->pages[idx]);
3178}
3179
28dfef8f 3180static const struct pipe_buf_operations tracing_pipe_buf_ops = {
34cd4998
SR
3181 .can_merge = 0,
3182 .map = generic_pipe_buf_map,
3183 .unmap = generic_pipe_buf_unmap,
3184 .confirm = generic_pipe_buf_confirm,
3185 .release = tracing_pipe_buf_release,
3186 .steal = generic_pipe_buf_steal,
3187 .get = generic_pipe_buf_get,
3c56819b
EGM
3188};
3189
34cd4998 3190static size_t
fa7c7f6e 3191tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
34cd4998
SR
3192{
3193 size_t count;
3194 int ret;
3195
3196 /* Seq buffer is page-sized, exactly what we need. */
3197 for (;;) {
3198 count = iter->seq.len;
3199 ret = print_trace_line(iter);
3200 count = iter->seq.len - count;
3201 if (rem < count) {
3202 rem = 0;
3203 iter->seq.len -= count;
3204 break;
3205 }
3206 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3207 iter->seq.len -= count;
3208 break;
3209 }
3210
74e7ff8c
LJ
3211 if (ret != TRACE_TYPE_NO_CONSUME)
3212 trace_consume(iter);
34cd4998
SR
3213 rem -= count;
3214 if (!find_next_entry_inc(iter)) {
3215 rem = 0;
3216 iter->ent = NULL;
3217 break;
3218 }
3219 }
3220
3221 return rem;
3222}
3223
3c56819b
EGM
3224static ssize_t tracing_splice_read_pipe(struct file *filp,
3225 loff_t *ppos,
3226 struct pipe_inode_info *pipe,
3227 size_t len,
3228 unsigned int flags)
3229{
3230 struct page *pages[PIPE_BUFFERS];
3231 struct partial_page partial[PIPE_BUFFERS];
3232 struct trace_iterator *iter = filp->private_data;
3233 struct splice_pipe_desc spd = {
34cd4998
SR
3234 .pages = pages,
3235 .partial = partial,
3236 .nr_pages = 0, /* This gets updated below. */
3237 .flags = flags,
3238 .ops = &tracing_pipe_buf_ops,
3239 .spd_release = tracing_spd_release_pipe,
3c56819b 3240 };
d7350c3f 3241 static struct tracer *old_tracer;
3c56819b 3242 ssize_t ret;
34cd4998 3243 size_t rem;
3c56819b
EGM
3244 unsigned int i;
3245
d7350c3f 3246 /* copy the tracer to avoid using a global lock all around */
3c56819b 3247 mutex_lock(&trace_types_lock);
d7350c3f
FW
3248 if (unlikely(old_tracer != current_trace && current_trace)) {
3249 old_tracer = current_trace;
3250 *iter->trace = *current_trace;
3251 }
3252 mutex_unlock(&trace_types_lock);
3253
3254 mutex_lock(&iter->mutex);
3c56819b
EGM
3255
3256 if (iter->trace->splice_read) {
3257 ret = iter->trace->splice_read(iter, filp,
3258 ppos, pipe, len, flags);
3259 if (ret)
34cd4998 3260 goto out_err;
3c56819b
EGM
3261 }
3262
3263 ret = tracing_wait_pipe(filp);
3264 if (ret <= 0)
34cd4998 3265 goto out_err;
3c56819b
EGM
3266
3267 if (!iter->ent && !find_next_entry_inc(iter)) {
3268 ret = -EFAULT;
34cd4998 3269 goto out_err;
3c56819b
EGM
3270 }
3271
4f535968 3272 trace_event_read_lock();
7e53bd42 3273 trace_access_lock(iter->cpu_file);
4f535968 3274
3c56819b
EGM
3275 /* Fill as many pages as possible. */
3276 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3277 pages[i] = alloc_page(GFP_KERNEL);
34cd4998
SR
3278 if (!pages[i])
3279 break;
3c56819b 3280
fa7c7f6e 3281 rem = tracing_fill_pipe_page(rem, iter);
3c56819b
EGM
3282
3283 /* Copy the data into the page, so we can start over. */
3284 ret = trace_seq_to_buffer(&iter->seq,
3285 page_address(pages[i]),
3286 iter->seq.len);
3287 if (ret < 0) {
3288 __free_page(pages[i]);
3289 break;
3290 }
3291 partial[i].offset = 0;
3292 partial[i].len = iter->seq.len;
3293
f9520750 3294 trace_seq_init(&iter->seq);
3c56819b
EGM
3295 }
3296
7e53bd42 3297 trace_access_unlock(iter->cpu_file);
4f535968 3298 trace_event_read_unlock();
d7350c3f 3299 mutex_unlock(&iter->mutex);
3c56819b
EGM
3300
3301 spd.nr_pages = i;
3302
3303 return splice_to_pipe(pipe, &spd);
3304
34cd4998 3305out_err:
d7350c3f 3306 mutex_unlock(&iter->mutex);
3c56819b
EGM
3307
3308 return ret;
3309}
3310
a98a3c3f
SR
3311static ssize_t
3312tracing_entries_read(struct file *filp, char __user *ubuf,
3313 size_t cnt, loff_t *ppos)
3314{
3315 struct trace_array *tr = filp->private_data;
db526ca3 3316 char buf[96];
a98a3c3f
SR
3317 int r;
3318
db526ca3
SR
3319 mutex_lock(&trace_types_lock);
3320 if (!ring_buffer_expanded)
3321 r = sprintf(buf, "%lu (expanded: %lu)\n",
3322 tr->entries >> 10,
3323 trace_buf_size >> 10);
3324 else
3325 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3326 mutex_unlock(&trace_types_lock);
3327
a98a3c3f
SR
3328 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3329}
3330
3331static ssize_t
3332tracing_entries_write(struct file *filp, const char __user *ubuf,
3333 size_t cnt, loff_t *ppos)
3334{
3335 unsigned long val;
3336 char buf[64];
bf5e6519 3337 int ret, cpu;
a98a3c3f 3338
cffae437
SR
3339 if (cnt >= sizeof(buf))
3340 return -EINVAL;
a98a3c3f
SR
3341
3342 if (copy_from_user(&buf, ubuf, cnt))
3343 return -EFAULT;
3344
3345 buf[cnt] = 0;
3346
c6caeeb1
SR
3347 ret = strict_strtoul(buf, 10, &val);
3348 if (ret < 0)
3349 return ret;
a98a3c3f
SR
3350
3351 /* must have at least 1 entry */
3352 if (!val)
3353 return -EINVAL;
3354
3355 mutex_lock(&trace_types_lock);
3356
c76f0694 3357 tracing_stop();
a98a3c3f 3358
bf5e6519
SR
3359 /* disable all cpu buffers */
3360 for_each_tracing_cpu(cpu) {
3361 if (global_trace.data[cpu])
3362 atomic_inc(&global_trace.data[cpu]->disabled);
3363 if (max_tr.data[cpu])
3364 atomic_inc(&max_tr.data[cpu]->disabled);
3365 }
3366
1696b2b0
SR
3367 /* value is in KB */
3368 val <<= 10;
3369
3928a8a2 3370 if (val != global_trace.entries) {
73c5162a 3371 ret = tracing_resize_ring_buffer(val);
3928a8a2
SR
3372 if (ret < 0) {
3373 cnt = ret;
3eefae99
SR
3374 goto out;
3375 }
a98a3c3f
SR
3376 }
3377
cf8517cf 3378 *ppos += cnt;
a98a3c3f 3379
19384c03
SR
3380 /* If check pages failed, return ENOMEM */
3381 if (tracing_disabled)
3382 cnt = -ENOMEM;
a98a3c3f 3383 out:
bf5e6519
SR
3384 for_each_tracing_cpu(cpu) {
3385 if (global_trace.data[cpu])
3386 atomic_dec(&global_trace.data[cpu]->disabled);
3387 if (max_tr.data[cpu])
3388 atomic_dec(&max_tr.data[cpu]->disabled);
3389 }
3390
c76f0694 3391 tracing_start();
a98a3c3f
SR
3392 max_tr.entries = global_trace.entries;
3393 mutex_unlock(&trace_types_lock);
3394
3395 return cnt;
3396}
3397
f2942487
CE
3398static int mark_printk(const char *fmt, ...)
3399{
3400 int ret;
3401 va_list args;
3402 va_start(args, fmt);
3403 ret = trace_vprintk(0, fmt, args);
3404 va_end(args);
3405 return ret;
3406}
3407
5bf9a1ee
PP
3408static ssize_t
3409tracing_mark_write(struct file *filp, const char __user *ubuf,
3410 size_t cnt, loff_t *fpos)
3411{
3412 char *buf;
5bf9a1ee 3413
c76f0694 3414 if (tracing_disabled)
5bf9a1ee
PP
3415 return -EINVAL;
3416
3417 if (cnt > TRACE_BUF_SIZE)
3418 cnt = TRACE_BUF_SIZE;
3419
c13d2f7c 3420 buf = kmalloc(cnt + 2, GFP_KERNEL);
5bf9a1ee
PP
3421 if (buf == NULL)
3422 return -ENOMEM;
3423
3424 if (copy_from_user(buf, ubuf, cnt)) {
3425 kfree(buf);
3426 return -EFAULT;
3427 }
c13d2f7c
CE
3428 if (buf[cnt-1] != '\n') {
3429 buf[cnt] = '\n';
3430 buf[cnt+1] = '\0';
3431 } else
3432 buf[cnt] = '\0';
5bf9a1ee 3433
f2942487 3434 cnt = mark_printk("%s", buf);
5bf9a1ee
PP
3435 kfree(buf);
3436 *fpos += cnt;
3437
3438 return cnt;
3439}
3440
13f16d20 3441static int tracing_clock_show(struct seq_file *m, void *v)
5079f326 3442{
5079f326
Z
3443 int i;
3444
3445 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
13f16d20 3446 seq_printf(m,
5079f326
Z
3447 "%s%s%s%s", i ? " " : "",
3448 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3449 i == trace_clock_id ? "]" : "");
13f16d20 3450 seq_putc(m, '\n');
5079f326 3451
13f16d20 3452 return 0;
5079f326
Z
3453}
3454
3455static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3456 size_t cnt, loff_t *fpos)
3457{
3458 char buf[64];
3459 const char *clockstr;
3460 int i;
3461
3462 if (cnt >= sizeof(buf))
3463 return -EINVAL;
3464
3465 if (copy_from_user(&buf, ubuf, cnt))
3466 return -EFAULT;
3467
3468 buf[cnt] = 0;
3469
3470 clockstr = strstrip(buf);
3471
3472 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3473 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3474 break;
3475 }
3476 if (i == ARRAY_SIZE(trace_clocks))
3477 return -EINVAL;
3478
3479 trace_clock_id = i;
3480
3481 mutex_lock(&trace_types_lock);
3482
3483 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3484 if (max_tr.buffer)
3485 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3486
3487 mutex_unlock(&trace_types_lock);
3488
3489 *fpos += cnt;
3490
3491 return cnt;
3492}
3493
13f16d20
LZ
3494static int tracing_clock_open(struct inode *inode, struct file *file)
3495{
3496 if (tracing_disabled)
3497 return -ENODEV;
3498 return single_open(file, tracing_clock_show, NULL);
3499}
3500
5e2336a0 3501static const struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
3502 .open = tracing_open_generic,
3503 .read = tracing_max_lat_read,
3504 .write = tracing_max_lat_write,
bc0c38d1
SR
3505};
3506
5e2336a0 3507static const struct file_operations tracing_ctrl_fops = {
4bf39a94
IM
3508 .open = tracing_open_generic,
3509 .read = tracing_ctrl_read,
3510 .write = tracing_ctrl_write,
bc0c38d1
SR
3511};
3512
5e2336a0 3513static const struct file_operations set_tracer_fops = {
4bf39a94
IM
3514 .open = tracing_open_generic,
3515 .read = tracing_set_trace_read,
3516 .write = tracing_set_trace_write,
bc0c38d1
SR
3517};
3518
5e2336a0 3519static const struct file_operations tracing_pipe_fops = {
4bf39a94 3520 .open = tracing_open_pipe,
2a2cc8f7 3521 .poll = tracing_poll_pipe,
4bf39a94 3522 .read = tracing_read_pipe,
3c56819b 3523 .splice_read = tracing_splice_read_pipe,
4bf39a94 3524 .release = tracing_release_pipe,
b3806b43
SR
3525};
3526
5e2336a0 3527static const struct file_operations tracing_entries_fops = {
a98a3c3f
SR
3528 .open = tracing_open_generic,
3529 .read = tracing_entries_read,
3530 .write = tracing_entries_write,
3531};
3532
5e2336a0 3533static const struct file_operations tracing_mark_fops = {
43a15386 3534 .open = tracing_open_generic,
5bf9a1ee
PP
3535 .write = tracing_mark_write,
3536};
3537
5079f326 3538static const struct file_operations trace_clock_fops = {
13f16d20
LZ
3539 .open = tracing_clock_open,
3540 .read = seq_read,
3541 .llseek = seq_lseek,
3542 .release = single_release,
5079f326
Z
3543 .write = tracing_clock_write,
3544};
3545
2cadf913
SR
3546struct ftrace_buffer_info {
3547 struct trace_array *tr;
3548 void *spare;
3549 int cpu;
3550 unsigned int read;
3551};
3552
3553static int tracing_buffers_open(struct inode *inode, struct file *filp)
3554{
3555 int cpu = (int)(long)inode->i_private;
3556 struct ftrace_buffer_info *info;
3557
3558 if (tracing_disabled)
3559 return -ENODEV;
3560
3561 info = kzalloc(sizeof(*info), GFP_KERNEL);
3562 if (!info)
3563 return -ENOMEM;
3564
3565 info->tr = &global_trace;
3566 info->cpu = cpu;
ddd538f3 3567 info->spare = NULL;
2cadf913
SR
3568 /* Force reading ring buffer for first read */
3569 info->read = (unsigned int)-1;
2cadf913
SR
3570
3571 filp->private_data = info;
3572
d1e7e02f 3573 return nonseekable_open(inode, filp);
2cadf913
SR
3574}
3575
3576static ssize_t
3577tracing_buffers_read(struct file *filp, char __user *ubuf,
3578 size_t count, loff_t *ppos)
3579{
3580 struct ftrace_buffer_info *info = filp->private_data;
3581 unsigned int pos;
3582 ssize_t ret;
3583 size_t size;
3584
2dc5d12b
SR
3585 if (!count)
3586 return 0;
3587
ddd538f3
LJ
3588 if (!info->spare)
3589 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3590 if (!info->spare)
3591 return -ENOMEM;
3592
2cadf913
SR
3593 /* Do we have previous read data to read? */
3594 if (info->read < PAGE_SIZE)
3595 goto read;
3596
3597 info->read = 0;
3598
7e53bd42 3599 trace_access_lock(info->cpu);
2cadf913
SR
3600 ret = ring_buffer_read_page(info->tr->buffer,
3601 &info->spare,
3602 count,
3603 info->cpu, 0);
7e53bd42 3604 trace_access_unlock(info->cpu);
2cadf913
SR
3605 if (ret < 0)
3606 return 0;
3607
3608 pos = ring_buffer_page_len(info->spare);
3609
3610 if (pos < PAGE_SIZE)
3611 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3612
3613read:
3614 size = PAGE_SIZE - info->read;
3615 if (size > count)
3616 size = count;
3617
3618 ret = copy_to_user(ubuf, info->spare + info->read, size);
2dc5d12b 3619 if (ret == size)
2cadf913 3620 return -EFAULT;
2dc5d12b
SR
3621 size -= ret;
3622
2cadf913
SR
3623 *ppos += size;
3624 info->read += size;
3625
3626 return size;
3627}
3628
3629static int tracing_buffers_release(struct inode *inode, struct file *file)
3630{
3631 struct ftrace_buffer_info *info = file->private_data;
3632
ddd538f3
LJ
3633 if (info->spare)
3634 ring_buffer_free_read_page(info->tr->buffer, info->spare);
2cadf913
SR
3635 kfree(info);
3636
3637 return 0;
3638}
3639
3640struct buffer_ref {
3641 struct ring_buffer *buffer;
3642 void *page;
3643 int ref;
3644};
3645
3646static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3647 struct pipe_buffer *buf)
3648{
3649 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3650
3651 if (--ref->ref)
3652 return;
3653
3654 ring_buffer_free_read_page(ref->buffer, ref->page);
3655 kfree(ref);
3656 buf->private = 0;
3657}
3658
3659static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3660 struct pipe_buffer *buf)
3661{
3662 return 1;
3663}
3664
3665static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3666 struct pipe_buffer *buf)
3667{
3668 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3669
3670 ref->ref++;
3671}
3672
3673/* Pipe buffer operations for a buffer. */
28dfef8f 3674static const struct pipe_buf_operations buffer_pipe_buf_ops = {
2cadf913
SR
3675 .can_merge = 0,
3676 .map = generic_pipe_buf_map,
3677 .unmap = generic_pipe_buf_unmap,
3678 .confirm = generic_pipe_buf_confirm,
3679 .release = buffer_pipe_buf_release,
3680 .steal = buffer_pipe_buf_steal,
3681 .get = buffer_pipe_buf_get,
3682};
3683
3684/*
3685 * Callback from splice_to_pipe(), if we need to release some pages
3686 * at the end of the spd in case we error'ed out in filling the pipe.
3687 */
3688static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3689{
3690 struct buffer_ref *ref =
3691 (struct buffer_ref *)spd->partial[i].private;
3692
3693 if (--ref->ref)
3694 return;
3695
3696 ring_buffer_free_read_page(ref->buffer, ref->page);
3697 kfree(ref);
3698 spd->partial[i].private = 0;
3699}
3700
3701static ssize_t
3702tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3703 struct pipe_inode_info *pipe, size_t len,
3704 unsigned int flags)
3705{
3706 struct ftrace_buffer_info *info = file->private_data;
3707 struct partial_page partial[PIPE_BUFFERS];
3708 struct page *pages[PIPE_BUFFERS];
3709 struct splice_pipe_desc spd = {
3710 .pages = pages,
3711 .partial = partial,
3712 .flags = flags,
3713 .ops = &buffer_pipe_buf_ops,
3714 .spd_release = buffer_spd_release,
3715 };
3716 struct buffer_ref *ref;
93459c6c 3717 int entries, size, i;
2cadf913
SR
3718 size_t ret;
3719
93cfb3c9
LJ
3720 if (*ppos & (PAGE_SIZE - 1)) {
3721 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3722 return -EINVAL;
3723 }
3724
3725 if (len & (PAGE_SIZE - 1)) {
3726 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3727 if (len < PAGE_SIZE)
3728 return -EINVAL;
3729 len &= PAGE_MASK;
3730 }
3731
7e53bd42 3732 trace_access_lock(info->cpu);
93459c6c
SR
3733 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3734
3735 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
2cadf913
SR
3736 struct page *page;
3737 int r;
3738
3739 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3740 if (!ref)
3741 break;
3742
7267fa68 3743 ref->ref = 1;
2cadf913
SR
3744 ref->buffer = info->tr->buffer;
3745 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3746 if (!ref->page) {
3747 kfree(ref);
3748 break;
3749 }
3750
3751 r = ring_buffer_read_page(ref->buffer, &ref->page,
f2957f1f 3752 len, info->cpu, 1);
2cadf913
SR
3753 if (r < 0) {
3754 ring_buffer_free_read_page(ref->buffer,
3755 ref->page);
3756 kfree(ref);
3757 break;
3758 }
3759
3760 /*
3761 * zero out any left over data, this is going to
3762 * user land.
3763 */
3764 size = ring_buffer_page_len(ref->page);
3765 if (size < PAGE_SIZE)
3766 memset(ref->page + size, 0, PAGE_SIZE - size);
3767
3768 page = virt_to_page(ref->page);
3769
3770 spd.pages[i] = page;
3771 spd.partial[i].len = PAGE_SIZE;
3772 spd.partial[i].offset = 0;
3773 spd.partial[i].private = (unsigned long)ref;
3774 spd.nr_pages++;
93cfb3c9 3775 *ppos += PAGE_SIZE;
93459c6c
SR
3776
3777 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
2cadf913
SR
3778 }
3779
7e53bd42 3780 trace_access_unlock(info->cpu);
2cadf913
SR
3781 spd.nr_pages = i;
3782
3783 /* did we read anything? */
3784 if (!spd.nr_pages) {
3785 if (flags & SPLICE_F_NONBLOCK)
3786 ret = -EAGAIN;
3787 else
3788 ret = 0;
3789 /* TODO: block */
3790 return ret;
3791 }
3792
3793 ret = splice_to_pipe(pipe, &spd);
3794
3795 return ret;
3796}
3797
3798static const struct file_operations tracing_buffers_fops = {
3799 .open = tracing_buffers_open,
3800 .read = tracing_buffers_read,
3801 .release = tracing_buffers_release,
3802 .splice_read = tracing_buffers_splice_read,
3803 .llseek = no_llseek,
3804};
3805
c8d77183
SR
3806static ssize_t
3807tracing_stats_read(struct file *filp, char __user *ubuf,
3808 size_t count, loff_t *ppos)
3809{
3810 unsigned long cpu = (unsigned long)filp->private_data;
3811 struct trace_array *tr = &global_trace;
3812 struct trace_seq *s;
3813 unsigned long cnt;
3814
e4f2d10f 3815 s = kmalloc(sizeof(*s), GFP_KERNEL);
c8d77183 3816 if (!s)
a646365c 3817 return -ENOMEM;
c8d77183
SR
3818
3819 trace_seq_init(s);
3820
3821 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3822 trace_seq_printf(s, "entries: %ld\n", cnt);
3823
3824 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3825 trace_seq_printf(s, "overrun: %ld\n", cnt);
3826
3827 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3828 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3829
c8d77183
SR
3830 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3831
3832 kfree(s);
3833
3834 return count;
3835}
3836
3837static const struct file_operations tracing_stats_fops = {
3838 .open = tracing_open_generic,
3839 .read = tracing_stats_read,
3840};
3841
bc0c38d1
SR
3842#ifdef CONFIG_DYNAMIC_FTRACE
3843
b807c3d0
SR
3844int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3845{
3846 return 0;
3847}
3848
bc0c38d1 3849static ssize_t
b807c3d0 3850tracing_read_dyn_info(struct file *filp, char __user *ubuf,
bc0c38d1
SR
3851 size_t cnt, loff_t *ppos)
3852{
a26a2a27
SR
3853 static char ftrace_dyn_info_buffer[1024];
3854 static DEFINE_MUTEX(dyn_info_mutex);
bc0c38d1 3855 unsigned long *p = filp->private_data;
b807c3d0 3856 char *buf = ftrace_dyn_info_buffer;
a26a2a27 3857 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
bc0c38d1
SR
3858 int r;
3859
b807c3d0
SR
3860 mutex_lock(&dyn_info_mutex);
3861 r = sprintf(buf, "%ld ", *p);
4bf39a94 3862
a26a2a27 3863 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
b807c3d0
SR
3864 buf[r++] = '\n';
3865
3866 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3867
3868 mutex_unlock(&dyn_info_mutex);
3869
3870 return r;
bc0c38d1
SR
3871}
3872
5e2336a0 3873static const struct file_operations tracing_dyn_info_fops = {
4bf39a94 3874 .open = tracing_open_generic,
b807c3d0 3875 .read = tracing_read_dyn_info,
bc0c38d1
SR
3876};
3877#endif
3878
3879static struct dentry *d_tracer;
3880
3881struct dentry *tracing_init_dentry(void)
3882{
3883 static int once;
3884
3885 if (d_tracer)
3886 return d_tracer;
3887
3e1f60b8
FW
3888 if (!debugfs_initialized())
3889 return NULL;
3890
bc0c38d1
SR
3891 d_tracer = debugfs_create_dir("tracing", NULL);
3892
3893 if (!d_tracer && !once) {
3894 once = 1;
3895 pr_warning("Could not create debugfs directory 'tracing'\n");
3896 return NULL;
3897 }
3898
3899 return d_tracer;
3900}
3901
b04cc6b1
FW
3902static struct dentry *d_percpu;
3903
3904struct dentry *tracing_dentry_percpu(void)
3905{
3906 static int once;
3907 struct dentry *d_tracer;
3908
3909 if (d_percpu)
3910 return d_percpu;
3911
3912 d_tracer = tracing_init_dentry();
3913
3914 if (!d_tracer)
3915 return NULL;
3916
3917 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3918
3919 if (!d_percpu && !once) {
3920 once = 1;
3921 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3922 return NULL;
3923 }
3924
3925 return d_percpu;
3926}
3927
3928static void tracing_init_debugfs_percpu(long cpu)
3929{
3930 struct dentry *d_percpu = tracing_dentry_percpu();
5452af66 3931 struct dentry *d_cpu;
8656e7a2
FW
3932 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3933 char cpu_dir[7];
b04cc6b1
FW
3934
3935 if (cpu > 999 || cpu < 0)
3936 return;
3937
8656e7a2
FW
3938 sprintf(cpu_dir, "cpu%ld", cpu);
3939 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3940 if (!d_cpu) {
3941 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3942 return;
3943 }
b04cc6b1 3944
8656e7a2 3945 /* per cpu trace_pipe */
5452af66
FW
3946 trace_create_file("trace_pipe", 0444, d_cpu,
3947 (void *) cpu, &tracing_pipe_fops);
b04cc6b1
FW
3948
3949 /* per cpu trace */
5452af66
FW
3950 trace_create_file("trace", 0644, d_cpu,
3951 (void *) cpu, &tracing_fops);
7f96f93f 3952
5452af66
FW
3953 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3954 (void *) cpu, &tracing_buffers_fops);
7f96f93f 3955
c8d77183
SR
3956 trace_create_file("stats", 0444, d_cpu,
3957 (void *) cpu, &tracing_stats_fops);
b04cc6b1
FW
3958}
3959
60a11774
SR
3960#ifdef CONFIG_FTRACE_SELFTEST
3961/* Let selftest have access to static functions in this file */
3962#include "trace_selftest.c"
3963#endif
3964
577b785f
SR
3965struct trace_option_dentry {
3966 struct tracer_opt *opt;
3967 struct tracer_flags *flags;
3968 struct dentry *entry;
3969};
3970
3971static ssize_t
3972trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3973 loff_t *ppos)
3974{
3975 struct trace_option_dentry *topt = filp->private_data;
3976 char *buf;
3977
3978 if (topt->flags->val & topt->opt->bit)
3979 buf = "1\n";
3980 else
3981 buf = "0\n";
3982
3983 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3984}
3985
3986static ssize_t
3987trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3988 loff_t *ppos)
3989{
3990 struct trace_option_dentry *topt = filp->private_data;
3991 unsigned long val;
3992 char buf[64];
3993 int ret;
3994
3995 if (cnt >= sizeof(buf))
3996 return -EINVAL;
3997
3998 if (copy_from_user(&buf, ubuf, cnt))
3999 return -EFAULT;
4000
4001 buf[cnt] = 0;
4002
4003 ret = strict_strtoul(buf, 10, &val);
4004 if (ret < 0)
4005 return ret;
4006
8d18eaaf
LZ
4007 if (val != 0 && val != 1)
4008 return -EINVAL;
577b785f 4009
8d18eaaf 4010 if (!!(topt->flags->val & topt->opt->bit) != val) {
577b785f 4011 mutex_lock(&trace_types_lock);
8d18eaaf 4012 ret = __set_tracer_option(current_trace, topt->flags,
c757bea9 4013 topt->opt, !val);
577b785f
SR
4014 mutex_unlock(&trace_types_lock);
4015 if (ret)
4016 return ret;
577b785f
SR
4017 }
4018
4019 *ppos += cnt;
4020
4021 return cnt;
4022}
4023
4024
4025static const struct file_operations trace_options_fops = {
4026 .open = tracing_open_generic,
4027 .read = trace_options_read,
4028 .write = trace_options_write,
4029};
4030
a8259075
SR
4031static ssize_t
4032trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4033 loff_t *ppos)
4034{
4035 long index = (long)filp->private_data;
4036 char *buf;
4037
4038 if (trace_flags & (1 << index))
4039 buf = "1\n";
4040 else
4041 buf = "0\n";
4042
4043 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4044}
4045
4046static ssize_t
4047trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4048 loff_t *ppos)
4049{
4050 long index = (long)filp->private_data;
4051 char buf[64];
4052 unsigned long val;
4053 int ret;
4054
4055 if (cnt >= sizeof(buf))
4056 return -EINVAL;
4057
4058 if (copy_from_user(&buf, ubuf, cnt))
4059 return -EFAULT;
4060
4061 buf[cnt] = 0;
4062
4063 ret = strict_strtoul(buf, 10, &val);
4064 if (ret < 0)
4065 return ret;
4066
f2d84b65 4067 if (val != 0 && val != 1)
a8259075 4068 return -EINVAL;
f2d84b65 4069 set_tracer_flags(1 << index, val);
a8259075
SR
4070
4071 *ppos += cnt;
4072
4073 return cnt;
4074}
4075
a8259075
SR
4076static const struct file_operations trace_options_core_fops = {
4077 .open = tracing_open_generic,
4078 .read = trace_options_core_read,
4079 .write = trace_options_core_write,
4080};
4081
5452af66
FW
4082struct dentry *trace_create_file(const char *name,
4083 mode_t mode,
4084 struct dentry *parent,
4085 void *data,
4086 const struct file_operations *fops)
4087{
4088 struct dentry *ret;
4089
4090 ret = debugfs_create_file(name, mode, parent, data, fops);
4091 if (!ret)
4092 pr_warning("Could not create debugfs '%s' entry\n", name);
4093
4094 return ret;
4095}
4096
4097
a8259075
SR
4098static struct dentry *trace_options_init_dentry(void)
4099{
4100 struct dentry *d_tracer;
4101 static struct dentry *t_options;
4102
4103 if (t_options)
4104 return t_options;
4105
4106 d_tracer = tracing_init_dentry();
4107 if (!d_tracer)
4108 return NULL;
4109
4110 t_options = debugfs_create_dir("options", d_tracer);
4111 if (!t_options) {
4112 pr_warning("Could not create debugfs directory 'options'\n");
4113 return NULL;
4114 }
4115
4116 return t_options;
4117}
4118
577b785f
SR
4119static void
4120create_trace_option_file(struct trace_option_dentry *topt,
4121 struct tracer_flags *flags,
4122 struct tracer_opt *opt)
4123{
4124 struct dentry *t_options;
577b785f
SR
4125
4126 t_options = trace_options_init_dentry();
4127 if (!t_options)
4128 return;
4129
4130 topt->flags = flags;
4131 topt->opt = opt;
4132
5452af66 4133 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
577b785f
SR
4134 &trace_options_fops);
4135
577b785f
SR
4136}
4137
4138static struct trace_option_dentry *
4139create_trace_option_files(struct tracer *tracer)
4140{
4141 struct trace_option_dentry *topts;
4142 struct tracer_flags *flags;
4143 struct tracer_opt *opts;
4144 int cnt;
4145
4146 if (!tracer)
4147 return NULL;
4148
4149 flags = tracer->flags;
4150
4151 if (!flags || !flags->opts)
4152 return NULL;
4153
4154 opts = flags->opts;
4155
4156 for (cnt = 0; opts[cnt].name; cnt++)
4157 ;
4158
0cfe8245 4159 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
577b785f
SR
4160 if (!topts)
4161 return NULL;
4162
4163 for (cnt = 0; opts[cnt].name; cnt++)
4164 create_trace_option_file(&topts[cnt], flags,
4165 &opts[cnt]);
4166
4167 return topts;
4168}
4169
4170static void
4171destroy_trace_option_files(struct trace_option_dentry *topts)
4172{
4173 int cnt;
4174
4175 if (!topts)
4176 return;
4177
4178 for (cnt = 0; topts[cnt].opt; cnt++) {
4179 if (topts[cnt].entry)
4180 debugfs_remove(topts[cnt].entry);
4181 }
4182
4183 kfree(topts);
4184}
4185
a8259075
SR
4186static struct dentry *
4187create_trace_option_core_file(const char *option, long index)
4188{
4189 struct dentry *t_options;
a8259075
SR
4190
4191 t_options = trace_options_init_dentry();
4192 if (!t_options)
4193 return NULL;
4194
5452af66 4195 return trace_create_file(option, 0644, t_options, (void *)index,
a8259075 4196 &trace_options_core_fops);
a8259075
SR
4197}
4198
4199static __init void create_trace_options_dir(void)
4200{
4201 struct dentry *t_options;
a8259075
SR
4202 int i;
4203
4204 t_options = trace_options_init_dentry();
4205 if (!t_options)
4206 return;
4207
5452af66
FW
4208 for (i = 0; trace_options[i]; i++)
4209 create_trace_option_core_file(trace_options[i], i);
a8259075
SR
4210}
4211
b5ad384e 4212static __init int tracer_init_debugfs(void)
bc0c38d1
SR
4213{
4214 struct dentry *d_tracer;
b04cc6b1 4215 int cpu;
bc0c38d1 4216
7e53bd42
LJ
4217 trace_access_lock_init();
4218
bc0c38d1
SR
4219 d_tracer = tracing_init_dentry();
4220
5452af66
FW
4221 trace_create_file("tracing_enabled", 0644, d_tracer,
4222 &global_trace, &tracing_ctrl_fops);
bc0c38d1 4223
5452af66
FW
4224 trace_create_file("trace_options", 0644, d_tracer,
4225 NULL, &tracing_iter_fops);
bc0c38d1 4226
5452af66
FW
4227 trace_create_file("tracing_cpumask", 0644, d_tracer,
4228 NULL, &tracing_cpumask_fops);
4229
4230 trace_create_file("trace", 0644, d_tracer,
4231 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
a8259075 4232
5452af66
FW
4233 trace_create_file("available_tracers", 0444, d_tracer,
4234 &global_trace, &show_traces_fops);
4235
339ae5d3 4236 trace_create_file("current_tracer", 0644, d_tracer,
5452af66
FW
4237 &global_trace, &set_tracer_fops);
4238
5d4a9dba 4239#ifdef CONFIG_TRACER_MAX_TRACE
5452af66
FW
4240 trace_create_file("tracing_max_latency", 0644, d_tracer,
4241 &tracing_max_latency, &tracing_max_lat_fops);
4242
4243 trace_create_file("tracing_thresh", 0644, d_tracer,
4244 &tracing_thresh, &tracing_max_lat_fops);
5d4a9dba 4245#endif
a8259075 4246
339ae5d3 4247 trace_create_file("README", 0444, d_tracer,
5452af66
FW
4248 NULL, &tracing_readme_fops);
4249
4250 trace_create_file("trace_pipe", 0444, d_tracer,
b04cc6b1 4251 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
5452af66
FW
4252
4253 trace_create_file("buffer_size_kb", 0644, d_tracer,
4254 &global_trace, &tracing_entries_fops);
4255
4256 trace_create_file("trace_marker", 0220, d_tracer,
4257 NULL, &tracing_mark_fops);
5bf9a1ee 4258
69abe6a5
AP
4259 trace_create_file("saved_cmdlines", 0444, d_tracer,
4260 NULL, &tracing_saved_cmdlines_fops);
5bf9a1ee 4261
5079f326
Z
4262 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4263 &trace_clock_fops);
4264
bc0c38d1 4265#ifdef CONFIG_DYNAMIC_FTRACE
5452af66
FW
4266 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4267 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
bc0c38d1 4268#endif
d618b3e6
IM
4269#ifdef CONFIG_SYSPROF_TRACER
4270 init_tracer_sysprof_debugfs(d_tracer);
4271#endif
b04cc6b1 4272
5452af66
FW
4273 create_trace_options_dir();
4274
b04cc6b1
FW
4275 for_each_tracing_cpu(cpu)
4276 tracing_init_debugfs_percpu(cpu);
4277
b5ad384e 4278 return 0;
bc0c38d1
SR
4279}
4280
3f5a54e3
SR
4281static int trace_panic_handler(struct notifier_block *this,
4282 unsigned long event, void *unused)
4283{
944ac425
SR
4284 if (ftrace_dump_on_oops)
4285 ftrace_dump();
3f5a54e3
SR
4286 return NOTIFY_OK;
4287}
4288
4289static struct notifier_block trace_panic_notifier = {
4290 .notifier_call = trace_panic_handler,
4291 .next = NULL,
4292 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4293};
4294
4295static int trace_die_handler(struct notifier_block *self,
4296 unsigned long val,
4297 void *data)
4298{
4299 switch (val) {
4300 case DIE_OOPS:
944ac425
SR
4301 if (ftrace_dump_on_oops)
4302 ftrace_dump();
3f5a54e3
SR
4303 break;
4304 default:
4305 break;
4306 }
4307 return NOTIFY_OK;
4308}
4309
4310static struct notifier_block trace_die_notifier = {
4311 .notifier_call = trace_die_handler,
4312 .priority = 200
4313};
4314
4315/*
4316 * printk is set to max of 1024, we really don't need it that big.
4317 * Nothing should be printing 1000 characters anyway.
4318 */
4319#define TRACE_MAX_PRINT 1000
4320
4321/*
4322 * Define here KERN_TRACE so that we have one place to modify
4323 * it if we decide to change what log level the ftrace dump
4324 * should be at.
4325 */
428aee14 4326#define KERN_TRACE KERN_EMERG
3f5a54e3
SR
4327
4328static void
4329trace_printk_seq(struct trace_seq *s)
4330{
4331 /* Probably should print a warning here. */
4332 if (s->len >= 1000)
4333 s->len = 1000;
4334
4335 /* should be zero ended, but we are paranoid. */
4336 s->buffer[s->len] = 0;
4337
4338 printk(KERN_TRACE "%s", s->buffer);
4339
f9520750 4340 trace_seq_init(s);
3f5a54e3
SR
4341}
4342
cf586b61 4343static void __ftrace_dump(bool disable_tracing)
3f5a54e3 4344{
445c8951 4345 static arch_spinlock_t ftrace_dump_lock =
edc35bd7 4346 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
3f5a54e3
SR
4347 /* use static because iter can be a bit big for the stack */
4348 static struct trace_iterator iter;
cf586b61 4349 unsigned int old_userobj;
3f5a54e3 4350 static int dump_ran;
d769041f
SR
4351 unsigned long flags;
4352 int cnt = 0, cpu;
3f5a54e3
SR
4353
4354 /* only one dump */
cd891ae0 4355 local_irq_save(flags);
0199c4e6 4356 arch_spin_lock(&ftrace_dump_lock);
3f5a54e3
SR
4357 if (dump_ran)
4358 goto out;
4359
4360 dump_ran = 1;
4361
0ee6b6cf 4362 tracing_off();
cf586b61
FW
4363
4364 if (disable_tracing)
4365 ftrace_kill();
3f5a54e3 4366
d769041f
SR
4367 for_each_tracing_cpu(cpu) {
4368 atomic_inc(&global_trace.data[cpu]->disabled);
4369 }
4370
cf586b61
FW
4371 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4372
b54d3de9
TE
4373 /* don't look at user memory in panic mode */
4374 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4375
3f5a54e3
SR
4376 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4377
e543ad76 4378 /* Simulate the iterator */
3f5a54e3
SR
4379 iter.tr = &global_trace;
4380 iter.trace = current_trace;
e543ad76 4381 iter.cpu_file = TRACE_PIPE_ALL_CPU;
3f5a54e3
SR
4382
4383 /*
4384 * We need to stop all tracing on all CPUS to read the
4385 * the next buffer. This is a bit expensive, but is
4386 * not done often. We fill all what we can read,
4387 * and then release the locks again.
4388 */
4389
3f5a54e3
SR
4390 while (!trace_empty(&iter)) {
4391
4392 if (!cnt)
4393 printk(KERN_TRACE "---------------------------------\n");
4394
4395 cnt++;
4396
4397 /* reset all but tr, trace, and overruns */
4398 memset(&iter.seq, 0,
4399 sizeof(struct trace_iterator) -
4400 offsetof(struct trace_iterator, seq));
4401 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4402 iter.pos = -1;
4403
4404 if (find_next_entry_inc(&iter) != NULL) {
74e7ff8c
LJ
4405 int ret;
4406
4407 ret = print_trace_line(&iter);
4408 if (ret != TRACE_TYPE_NO_CONSUME)
4409 trace_consume(&iter);
3f5a54e3
SR
4410 }
4411
4412 trace_printk_seq(&iter.seq);
4413 }
4414
4415 if (!cnt)
4416 printk(KERN_TRACE " (ftrace buffer empty)\n");
4417 else
4418 printk(KERN_TRACE "---------------------------------\n");
4419
cf586b61
FW
4420 /* Re-enable tracing if requested */
4421 if (!disable_tracing) {
4422 trace_flags |= old_userobj;
4423
4424 for_each_tracing_cpu(cpu) {
4425 atomic_dec(&global_trace.data[cpu]->disabled);
4426 }
4427 tracing_on();
4428 }
4429
3f5a54e3 4430 out:
0199c4e6 4431 arch_spin_unlock(&ftrace_dump_lock);
cd891ae0 4432 local_irq_restore(flags);
3f5a54e3
SR
4433}
4434
cf586b61
FW
4435/* By default: disable tracing after the dump */
4436void ftrace_dump(void)
4437{
4438 __ftrace_dump(true);
4439}
4440
3928a8a2 4441__init static int tracer_alloc_buffers(void)
bc0c38d1 4442{
73c5162a 4443 int ring_buf_size;
4c11d7ae 4444 int i;
9e01c1b7 4445 int ret = -ENOMEM;
4c11d7ae 4446
9e01c1b7
RR
4447 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4448 goto out;
4449
4450 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4451 goto out_free_buffer_mask;
4c11d7ae 4452
73c5162a
SR
4453 /* To save memory, keep the ring buffer size to its minimum */
4454 if (ring_buffer_expanded)
4455 ring_buf_size = trace_buf_size;
4456 else
4457 ring_buf_size = 1;
4458
9e01c1b7
RR
4459 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4460 cpumask_copy(tracing_cpumask, cpu_all_mask);
4461
4462 /* TODO: make the number of buffers hot pluggable with CPUS */
73c5162a 4463 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
3928a8a2
SR
4464 TRACE_BUFFER_FLAGS);
4465 if (!global_trace.buffer) {
4466 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4467 WARN_ON(1);
9e01c1b7 4468 goto out_free_cpumask;
4c11d7ae 4469 }
3928a8a2 4470 global_trace.entries = ring_buffer_size(global_trace.buffer);
4c11d7ae 4471
9e01c1b7 4472
4c11d7ae 4473#ifdef CONFIG_TRACER_MAX_TRACE
73c5162a 4474 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
3928a8a2
SR
4475 TRACE_BUFFER_FLAGS);
4476 if (!max_tr.buffer) {
4477 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4478 WARN_ON(1);
4479 ring_buffer_free(global_trace.buffer);
9e01c1b7 4480 goto out_free_cpumask;
4c11d7ae 4481 }
3928a8a2
SR
4482 max_tr.entries = ring_buffer_size(max_tr.buffer);
4483 WARN_ON(max_tr.entries != global_trace.entries);
a98a3c3f 4484#endif
ab46428c 4485
4c11d7ae 4486 /* Allocate the first page for all buffers */
ab46428c 4487 for_each_tracing_cpu(i) {
566b0aaf 4488 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
9705f69e 4489 max_tr.data[i] = &per_cpu(max_tr_data, i);
4c11d7ae 4490 }
bc0c38d1 4491
bc0c38d1
SR
4492 trace_init_cmdlines();
4493
43a15386 4494 register_tracer(&nop_trace);
79fb0768 4495 current_trace = &nop_trace;
b5ad384e
FW
4496#ifdef CONFIG_BOOT_TRACER
4497 register_tracer(&boot_tracer);
b5ad384e 4498#endif
60a11774
SR
4499 /* All seems OK, enable tracing */
4500 tracing_disabled = 0;
3928a8a2 4501
3f5a54e3
SR
4502 atomic_notifier_chain_register(&panic_notifier_list,
4503 &trace_panic_notifier);
4504
4505 register_die_notifier(&trace_die_notifier);
2fc1dfbe
FW
4506
4507 return 0;
3f5a54e3 4508
9e01c1b7
RR
4509out_free_cpumask:
4510 free_cpumask_var(tracing_cpumask);
4511out_free_buffer_mask:
4512 free_cpumask_var(tracing_buffer_mask);
4513out:
4514 return ret;
bc0c38d1 4515}
b2821ae6
SR
4516
4517__init static int clear_boot_tracer(void)
4518{
4519 /*
4520 * The default tracer at boot buffer is an init section.
4521 * This function is called in lateinit. If we did not
4522 * find the boot tracer, then clear it out, to prevent
4523 * later registration from accessing the buffer that is
4524 * about to be freed.
4525 */
4526 if (!default_bootup_tracer)
4527 return 0;
4528
4529 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4530 default_bootup_tracer);
4531 default_bootup_tracer = NULL;
4532
4533 return 0;
4534}
4535
b5ad384e
FW
4536early_initcall(tracer_alloc_buffers);
4537fs_initcall(tracer_init_debugfs);
b2821ae6 4538late_initcall(clear_boot_tracer);