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