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