tracing, function_graph: Add context-info support for function_graph tracer
[GitHub/mt8127/android_kernel_alcatel_ttab.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
f56e7f8e
JO
2056 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2057 return;
2058
62b915f1
JO
2059 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2060 /* print nothing if the buffers are empty */
2061 if (trace_empty(iter))
2062 return;
2063 print_trace_header(m, iter);
2064 if (!(trace_flags & TRACE_ITER_VERBOSE))
2065 print_lat_help_header(m);
2066 } else {
2067 if (!(trace_flags & TRACE_ITER_VERBOSE))
2068 print_func_help_header(m);
2069 }
2070}
2071
bc0c38d1
SR
2072static int s_show(struct seq_file *m, void *v)
2073{
2074 struct trace_iterator *iter = v;
a63ce5b3 2075 int ret;
bc0c38d1
SR
2076
2077 if (iter->ent == NULL) {
2078 if (iter->tr) {
2079 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2080 seq_puts(m, "#\n");
2081 }
8bba1bf5
MM
2082 if (iter->trace && iter->trace->print_header)
2083 iter->trace->print_header(m);
62b915f1
JO
2084 else
2085 trace_default_header(m);
2086
a63ce5b3
SR
2087 } else if (iter->leftover) {
2088 /*
2089 * If we filled the seq_file buffer earlier, we
2090 * want to just show it now.
2091 */
2092 ret = trace_print_seq(m, &iter->seq);
2093
2094 /* ret should this time be zero, but you never know */
2095 iter->leftover = ret;
2096
bc0c38d1 2097 } else {
f9896bf3 2098 print_trace_line(iter);
a63ce5b3
SR
2099 ret = trace_print_seq(m, &iter->seq);
2100 /*
2101 * If we overflow the seq_file buffer, then it will
2102 * ask us for this data again at start up.
2103 * Use that instead.
2104 * ret is 0 if seq_file write succeeded.
2105 * -1 otherwise.
2106 */
2107 iter->leftover = ret;
bc0c38d1
SR
2108 }
2109
2110 return 0;
2111}
2112
88e9d34c 2113static const struct seq_operations tracer_seq_ops = {
4bf39a94
IM
2114 .start = s_start,
2115 .next = s_next,
2116 .stop = s_stop,
2117 .show = s_show,
bc0c38d1
SR
2118};
2119
e309b41d 2120static struct trace_iterator *
85a2f9b4 2121__tracing_open(struct inode *inode, struct file *file)
bc0c38d1 2122{
b04cc6b1 2123 long cpu_file = (long) inode->i_private;
85a2f9b4 2124 void *fail_ret = ERR_PTR(-ENOMEM);
bc0c38d1 2125 struct trace_iterator *iter;
3928a8a2 2126 struct seq_file *m;
85a2f9b4 2127 int cpu, ret;
bc0c38d1 2128
85a2f9b4
SR
2129 if (tracing_disabled)
2130 return ERR_PTR(-ENODEV);
60a11774 2131
bc0c38d1 2132 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
85a2f9b4
SR
2133 if (!iter)
2134 return ERR_PTR(-ENOMEM);
bc0c38d1 2135
d7350c3f
FW
2136 /*
2137 * We make a copy of the current tracer to avoid concurrent
2138 * changes on it while we are reading.
2139 */
bc0c38d1 2140 mutex_lock(&trace_types_lock);
d7350c3f 2141 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
85a2f9b4 2142 if (!iter->trace)
d7350c3f 2143 goto fail;
85a2f9b4 2144
d7350c3f
FW
2145 if (current_trace)
2146 *iter->trace = *current_trace;
2147
79f55997 2148 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
b0dfa978
FW
2149 goto fail;
2150
bc0c38d1
SR
2151 if (current_trace && current_trace->print_max)
2152 iter->tr = &max_tr;
2153 else
b04cc6b1 2154 iter->tr = &global_trace;
bc0c38d1 2155 iter->pos = -1;
d7350c3f 2156 mutex_init(&iter->mutex);
b04cc6b1 2157 iter->cpu_file = cpu_file;
bc0c38d1 2158
8bba1bf5
MM
2159 /* Notify the tracer early; before we stop tracing. */
2160 if (iter->trace && iter->trace->open)
a93751ca 2161 iter->trace->open(iter);
8bba1bf5 2162
12ef7d44
SR
2163 /* Annotate start of buffers if we had overruns */
2164 if (ring_buffer_overruns(iter->tr->buffer))
2165 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2166
2f26ebd5
SR
2167 /* stop the trace while dumping */
2168 tracing_stop();
2169
b04cc6b1
FW
2170 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2171 for_each_tracing_cpu(cpu) {
b04cc6b1 2172 iter->buffer_iter[cpu] =
72c9ddfd
DM
2173 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2174 }
2175 ring_buffer_read_prepare_sync();
2176 for_each_tracing_cpu(cpu) {
2177 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 2178 tracing_iter_reset(iter, cpu);
b04cc6b1
FW
2179 }
2180 } else {
2181 cpu = iter->cpu_file;
3928a8a2 2182 iter->buffer_iter[cpu] =
72c9ddfd
DM
2183 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2184 ring_buffer_read_prepare_sync();
2185 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 2186 tracing_iter_reset(iter, cpu);
3928a8a2
SR
2187 }
2188
85a2f9b4
SR
2189 ret = seq_open(file, &tracer_seq_ops);
2190 if (ret < 0) {
2191 fail_ret = ERR_PTR(ret);
3928a8a2 2192 goto fail_buffer;
85a2f9b4 2193 }
bc0c38d1 2194
3928a8a2
SR
2195 m = file->private_data;
2196 m->private = iter;
bc0c38d1 2197
bc0c38d1
SR
2198 mutex_unlock(&trace_types_lock);
2199
bc0c38d1 2200 return iter;
3928a8a2
SR
2201
2202 fail_buffer:
2203 for_each_tracing_cpu(cpu) {
2204 if (iter->buffer_iter[cpu])
2205 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2206 }
b0dfa978 2207 free_cpumask_var(iter->started);
2f26ebd5 2208 tracing_start();
d7350c3f 2209 fail:
3928a8a2 2210 mutex_unlock(&trace_types_lock);
d7350c3f 2211 kfree(iter->trace);
0bb943c7 2212 kfree(iter);
3928a8a2 2213
85a2f9b4 2214 return fail_ret;
bc0c38d1
SR
2215}
2216
2217int tracing_open_generic(struct inode *inode, struct file *filp)
2218{
60a11774
SR
2219 if (tracing_disabled)
2220 return -ENODEV;
2221
bc0c38d1
SR
2222 filp->private_data = inode->i_private;
2223 return 0;
2224}
2225
4fd27358 2226static int tracing_release(struct inode *inode, struct file *file)
bc0c38d1 2227{
907f2784 2228 struct seq_file *m = file->private_data;
4acd4d00 2229 struct trace_iterator *iter;
3928a8a2 2230 int cpu;
bc0c38d1 2231
4acd4d00
SR
2232 if (!(file->f_mode & FMODE_READ))
2233 return 0;
2234
2235 iter = m->private;
2236
bc0c38d1 2237 mutex_lock(&trace_types_lock);
3928a8a2
SR
2238 for_each_tracing_cpu(cpu) {
2239 if (iter->buffer_iter[cpu])
2240 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2241 }
2242
bc0c38d1
SR
2243 if (iter->trace && iter->trace->close)
2244 iter->trace->close(iter);
2245
2246 /* reenable tracing if it was previously enabled */
9036990d 2247 tracing_start();
bc0c38d1
SR
2248 mutex_unlock(&trace_types_lock);
2249
2250 seq_release(inode, file);
d7350c3f 2251 mutex_destroy(&iter->mutex);
b0dfa978 2252 free_cpumask_var(iter->started);
d7350c3f 2253 kfree(iter->trace);
bc0c38d1
SR
2254 kfree(iter);
2255 return 0;
2256}
2257
2258static int tracing_open(struct inode *inode, struct file *file)
2259{
85a2f9b4
SR
2260 struct trace_iterator *iter;
2261 int ret = 0;
bc0c38d1 2262
4acd4d00
SR
2263 /* If this file was open for write, then erase contents */
2264 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 2265 (file->f_flags & O_TRUNC)) {
4acd4d00 2266 long cpu = (long) inode->i_private;
bc0c38d1 2267
4acd4d00
SR
2268 if (cpu == TRACE_PIPE_ALL_CPU)
2269 tracing_reset_online_cpus(&global_trace);
2270 else
2271 tracing_reset(&global_trace, cpu);
2272 }
bc0c38d1 2273
4acd4d00
SR
2274 if (file->f_mode & FMODE_READ) {
2275 iter = __tracing_open(inode, file);
2276 if (IS_ERR(iter))
2277 ret = PTR_ERR(iter);
2278 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2279 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2280 }
bc0c38d1
SR
2281 return ret;
2282}
2283
e309b41d 2284static void *
bc0c38d1
SR
2285t_next(struct seq_file *m, void *v, loff_t *pos)
2286{
f129e965 2287 struct tracer *t = v;
bc0c38d1
SR
2288
2289 (*pos)++;
2290
2291 if (t)
2292 t = t->next;
2293
bc0c38d1
SR
2294 return t;
2295}
2296
2297static void *t_start(struct seq_file *m, loff_t *pos)
2298{
f129e965 2299 struct tracer *t;
bc0c38d1
SR
2300 loff_t l = 0;
2301
2302 mutex_lock(&trace_types_lock);
f129e965 2303 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
bc0c38d1
SR
2304 ;
2305
2306 return t;
2307}
2308
2309static void t_stop(struct seq_file *m, void *p)
2310{
2311 mutex_unlock(&trace_types_lock);
2312}
2313
2314static int t_show(struct seq_file *m, void *v)
2315{
2316 struct tracer *t = v;
2317
2318 if (!t)
2319 return 0;
2320
2321 seq_printf(m, "%s", t->name);
2322 if (t->next)
2323 seq_putc(m, ' ');
2324 else
2325 seq_putc(m, '\n');
2326
2327 return 0;
2328}
2329
88e9d34c 2330static const struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
2331 .start = t_start,
2332 .next = t_next,
2333 .stop = t_stop,
2334 .show = t_show,
bc0c38d1
SR
2335};
2336
2337static int show_traces_open(struct inode *inode, struct file *file)
2338{
60a11774
SR
2339 if (tracing_disabled)
2340 return -ENODEV;
2341
f129e965 2342 return seq_open(file, &show_traces_seq_ops);
bc0c38d1
SR
2343}
2344
4acd4d00
SR
2345static ssize_t
2346tracing_write_stub(struct file *filp, const char __user *ubuf,
2347 size_t count, loff_t *ppos)
2348{
2349 return count;
2350}
2351
364829b1
SP
2352static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2353{
2354 if (file->f_mode & FMODE_READ)
2355 return seq_lseek(file, offset, origin);
2356 else
2357 return 0;
2358}
2359
5e2336a0 2360static const struct file_operations tracing_fops = {
4bf39a94
IM
2361 .open = tracing_open,
2362 .read = seq_read,
4acd4d00 2363 .write = tracing_write_stub,
364829b1 2364 .llseek = tracing_seek,
4bf39a94 2365 .release = tracing_release,
bc0c38d1
SR
2366};
2367
5e2336a0 2368static const struct file_operations show_traces_fops = {
c7078de1
IM
2369 .open = show_traces_open,
2370 .read = seq_read,
2371 .release = seq_release,
b444786f 2372 .llseek = seq_lseek,
c7078de1
IM
2373};
2374
36dfe925
IM
2375/*
2376 * Only trace on a CPU if the bitmask is set:
2377 */
9e01c1b7 2378static cpumask_var_t tracing_cpumask;
36dfe925
IM
2379
2380/*
2381 * The tracer itself will not take this lock, but still we want
2382 * to provide a consistent cpumask to user-space:
2383 */
2384static DEFINE_MUTEX(tracing_cpumask_update_lock);
2385
2386/*
2387 * Temporary storage for the character representation of the
2388 * CPU bitmask (and one more byte for the newline):
2389 */
2390static char mask_str[NR_CPUS + 1];
2391
c7078de1
IM
2392static ssize_t
2393tracing_cpumask_read(struct file *filp, char __user *ubuf,
2394 size_t count, loff_t *ppos)
2395{
36dfe925 2396 int len;
c7078de1
IM
2397
2398 mutex_lock(&tracing_cpumask_update_lock);
36dfe925 2399
9e01c1b7 2400 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
36dfe925
IM
2401 if (count - len < 2) {
2402 count = -EINVAL;
2403 goto out_err;
2404 }
2405 len += sprintf(mask_str + len, "\n");
2406 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2407
2408out_err:
c7078de1
IM
2409 mutex_unlock(&tracing_cpumask_update_lock);
2410
2411 return count;
2412}
2413
2414static ssize_t
2415tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2416 size_t count, loff_t *ppos)
2417{
36dfe925 2418 int err, cpu;
9e01c1b7
RR
2419 cpumask_var_t tracing_cpumask_new;
2420
2421 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2422 return -ENOMEM;
c7078de1 2423
9e01c1b7 2424 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
c7078de1 2425 if (err)
36dfe925
IM
2426 goto err_unlock;
2427
215368e8
LZ
2428 mutex_lock(&tracing_cpumask_update_lock);
2429
a5e25883 2430 local_irq_disable();
0199c4e6 2431 arch_spin_lock(&ftrace_max_lock);
ab46428c 2432 for_each_tracing_cpu(cpu) {
36dfe925
IM
2433 /*
2434 * Increase/decrease the disabled counter if we are
2435 * about to flip a bit in the cpumask:
2436 */
9e01c1b7
RR
2437 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2438 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
36dfe925
IM
2439 atomic_inc(&global_trace.data[cpu]->disabled);
2440 }
9e01c1b7
RR
2441 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2442 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
36dfe925
IM
2443 atomic_dec(&global_trace.data[cpu]->disabled);
2444 }
2445 }
0199c4e6 2446 arch_spin_unlock(&ftrace_max_lock);
a5e25883 2447 local_irq_enable();
36dfe925 2448
9e01c1b7 2449 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
36dfe925
IM
2450
2451 mutex_unlock(&tracing_cpumask_update_lock);
9e01c1b7 2452 free_cpumask_var(tracing_cpumask_new);
c7078de1
IM
2453
2454 return count;
36dfe925
IM
2455
2456err_unlock:
215368e8 2457 free_cpumask_var(tracing_cpumask_new);
36dfe925
IM
2458
2459 return err;
c7078de1
IM
2460}
2461
5e2336a0 2462static const struct file_operations tracing_cpumask_fops = {
c7078de1
IM
2463 .open = tracing_open_generic,
2464 .read = tracing_cpumask_read,
2465 .write = tracing_cpumask_write,
b444786f 2466 .llseek = generic_file_llseek,
bc0c38d1
SR
2467};
2468
fdb372ed 2469static int tracing_trace_options_show(struct seq_file *m, void *v)
bc0c38d1 2470{
d8e83d26
SR
2471 struct tracer_opt *trace_opts;
2472 u32 tracer_flags;
d8e83d26 2473 int i;
adf9f195 2474
d8e83d26
SR
2475 mutex_lock(&trace_types_lock);
2476 tracer_flags = current_trace->flags->val;
2477 trace_opts = current_trace->flags->opts;
2478
bc0c38d1
SR
2479 for (i = 0; trace_options[i]; i++) {
2480 if (trace_flags & (1 << i))
fdb372ed 2481 seq_printf(m, "%s\n", trace_options[i]);
bc0c38d1 2482 else
fdb372ed 2483 seq_printf(m, "no%s\n", trace_options[i]);
bc0c38d1
SR
2484 }
2485
adf9f195
FW
2486 for (i = 0; trace_opts[i].name; i++) {
2487 if (tracer_flags & trace_opts[i].bit)
fdb372ed 2488 seq_printf(m, "%s\n", trace_opts[i].name);
adf9f195 2489 else
fdb372ed 2490 seq_printf(m, "no%s\n", trace_opts[i].name);
adf9f195 2491 }
d8e83d26 2492 mutex_unlock(&trace_types_lock);
adf9f195 2493
fdb372ed 2494 return 0;
bc0c38d1 2495}
bc0c38d1 2496
8d18eaaf
LZ
2497static int __set_tracer_option(struct tracer *trace,
2498 struct tracer_flags *tracer_flags,
2499 struct tracer_opt *opts, int neg)
2500{
2501 int ret;
bc0c38d1 2502
8d18eaaf
LZ
2503 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2504 if (ret)
2505 return ret;
2506
2507 if (neg)
2508 tracer_flags->val &= ~opts->bit;
2509 else
2510 tracer_flags->val |= opts->bit;
2511 return 0;
bc0c38d1
SR
2512}
2513
adf9f195
FW
2514/* Try to assign a tracer specific option */
2515static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2516{
7770841e 2517 struct tracer_flags *tracer_flags = trace->flags;
adf9f195 2518 struct tracer_opt *opts = NULL;
8d18eaaf 2519 int i;
adf9f195 2520
7770841e
Z
2521 for (i = 0; tracer_flags->opts[i].name; i++) {
2522 opts = &tracer_flags->opts[i];
adf9f195 2523
8d18eaaf
LZ
2524 if (strcmp(cmp, opts->name) == 0)
2525 return __set_tracer_option(trace, trace->flags,
2526 opts, neg);
adf9f195 2527 }
adf9f195 2528
8d18eaaf 2529 return -EINVAL;
adf9f195
FW
2530}
2531
af4617bd
SR
2532static void set_tracer_flags(unsigned int mask, int enabled)
2533{
2534 /* do nothing if flag is already set */
2535 if (!!(trace_flags & mask) == !!enabled)
2536 return;
2537
2538 if (enabled)
2539 trace_flags |= mask;
2540 else
2541 trace_flags &= ~mask;
e870e9a1
LZ
2542
2543 if (mask == TRACE_ITER_RECORD_CMD)
2544 trace_event_enable_cmd_record(enabled);
750912fa
DS
2545
2546 if (mask == TRACE_ITER_OVERWRITE)
2547 ring_buffer_change_overwrite(global_trace.buffer, enabled);
af4617bd
SR
2548}
2549
bc0c38d1 2550static ssize_t
ee6bce52 2551tracing_trace_options_write(struct file *filp, const char __user *ubuf,
bc0c38d1
SR
2552 size_t cnt, loff_t *ppos)
2553{
2554 char buf[64];
8d18eaaf 2555 char *cmp;
bc0c38d1 2556 int neg = 0;
adf9f195 2557 int ret;
bc0c38d1
SR
2558 int i;
2559
cffae437
SR
2560 if (cnt >= sizeof(buf))
2561 return -EINVAL;
bc0c38d1
SR
2562
2563 if (copy_from_user(&buf, ubuf, cnt))
2564 return -EFAULT;
2565
2566 buf[cnt] = 0;
8d18eaaf 2567 cmp = strstrip(buf);
bc0c38d1 2568
8d18eaaf 2569 if (strncmp(cmp, "no", 2) == 0) {
bc0c38d1
SR
2570 neg = 1;
2571 cmp += 2;
2572 }
2573
2574 for (i = 0; trace_options[i]; i++) {
8d18eaaf 2575 if (strcmp(cmp, trace_options[i]) == 0) {
af4617bd 2576 set_tracer_flags(1 << i, !neg);
bc0c38d1
SR
2577 break;
2578 }
2579 }
adf9f195
FW
2580
2581 /* If no option could be set, test the specific tracer options */
2582 if (!trace_options[i]) {
d8e83d26 2583 mutex_lock(&trace_types_lock);
adf9f195 2584 ret = set_tracer_option(current_trace, cmp, neg);
d8e83d26 2585 mutex_unlock(&trace_types_lock);
adf9f195
FW
2586 if (ret)
2587 return ret;
2588 }
bc0c38d1 2589
cf8517cf 2590 *ppos += cnt;
bc0c38d1
SR
2591
2592 return cnt;
2593}
2594
fdb372ed
LZ
2595static int tracing_trace_options_open(struct inode *inode, struct file *file)
2596{
2597 if (tracing_disabled)
2598 return -ENODEV;
2599 return single_open(file, tracing_trace_options_show, NULL);
2600}
2601
5e2336a0 2602static const struct file_operations tracing_iter_fops = {
fdb372ed
LZ
2603 .open = tracing_trace_options_open,
2604 .read = seq_read,
2605 .llseek = seq_lseek,
2606 .release = single_release,
ee6bce52 2607 .write = tracing_trace_options_write,
bc0c38d1
SR
2608};
2609
7bd2f24c
IM
2610static const char readme_msg[] =
2611 "tracing mini-HOWTO:\n\n"
156f5a78
GL
2612 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2613 "# cat /sys/kernel/debug/tracing/available_tracers\n"
bc2b6871 2614 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
156f5a78 2615 "# cat /sys/kernel/debug/tracing/current_tracer\n"
bc2b6871 2616 "nop\n"
156f5a78
GL
2617 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2618 "# cat /sys/kernel/debug/tracing/current_tracer\n"
7bd2f24c 2619 "sched_switch\n"
156f5a78 2620 "# cat /sys/kernel/debug/tracing/trace_options\n"
7bd2f24c 2621 "noprint-parent nosym-offset nosym-addr noverbose\n"
156f5a78
GL
2622 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2623 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2624 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2625 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
7bd2f24c
IM
2626;
2627
2628static ssize_t
2629tracing_readme_read(struct file *filp, char __user *ubuf,
2630 size_t cnt, loff_t *ppos)
2631{
2632 return simple_read_from_buffer(ubuf, cnt, ppos,
2633 readme_msg, strlen(readme_msg));
2634}
2635
5e2336a0 2636static const struct file_operations tracing_readme_fops = {
c7078de1
IM
2637 .open = tracing_open_generic,
2638 .read = tracing_readme_read,
b444786f 2639 .llseek = generic_file_llseek,
7bd2f24c
IM
2640};
2641
69abe6a5
AP
2642static ssize_t
2643tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2644 size_t cnt, loff_t *ppos)
2645{
2646 char *buf_comm;
2647 char *file_buf;
2648 char *buf;
2649 int len = 0;
2650 int pid;
2651 int i;
2652
2653 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2654 if (!file_buf)
2655 return -ENOMEM;
2656
2657 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2658 if (!buf_comm) {
2659 kfree(file_buf);
2660 return -ENOMEM;
2661 }
2662
2663 buf = file_buf;
2664
2665 for (i = 0; i < SAVED_CMDLINES; i++) {
2666 int r;
2667
2668 pid = map_cmdline_to_pid[i];
2669 if (pid == -1 || pid == NO_CMDLINE_MAP)
2670 continue;
2671
2672 trace_find_cmdline(pid, buf_comm);
2673 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2674 buf += r;
2675 len += r;
2676 }
2677
2678 len = simple_read_from_buffer(ubuf, cnt, ppos,
2679 file_buf, len);
2680
2681 kfree(file_buf);
2682 kfree(buf_comm);
2683
2684 return len;
2685}
2686
2687static const struct file_operations tracing_saved_cmdlines_fops = {
2688 .open = tracing_open_generic,
2689 .read = tracing_saved_cmdlines_read,
b444786f 2690 .llseek = generic_file_llseek,
69abe6a5
AP
2691};
2692
bc0c38d1
SR
2693static ssize_t
2694tracing_ctrl_read(struct file *filp, char __user *ubuf,
2695 size_t cnt, loff_t *ppos)
2696{
bc0c38d1
SR
2697 char buf[64];
2698 int r;
2699
9036990d 2700 r = sprintf(buf, "%u\n", tracer_enabled);
4e3c3333 2701 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2702}
2703
2704static ssize_t
2705tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2706 size_t cnt, loff_t *ppos)
2707{
2708 struct trace_array *tr = filp->private_data;
bc0c38d1 2709 char buf[64];
5e39841c 2710 unsigned long val;
c6caeeb1 2711 int ret;
bc0c38d1 2712
cffae437
SR
2713 if (cnt >= sizeof(buf))
2714 return -EINVAL;
bc0c38d1
SR
2715
2716 if (copy_from_user(&buf, ubuf, cnt))
2717 return -EFAULT;
2718
2719 buf[cnt] = 0;
2720
c6caeeb1
SR
2721 ret = strict_strtoul(buf, 10, &val);
2722 if (ret < 0)
2723 return ret;
bc0c38d1
SR
2724
2725 val = !!val;
2726
2727 mutex_lock(&trace_types_lock);
9036990d 2728 if (tracer_enabled ^ val) {
6752ab4a
SR
2729
2730 /* Only need to warn if this is used to change the state */
2731 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
2732
9036990d 2733 if (val) {
bc0c38d1 2734 tracer_enabled = 1;
9036990d
SR
2735 if (current_trace->start)
2736 current_trace->start(tr);
2737 tracing_start();
2738 } else {
bc0c38d1 2739 tracer_enabled = 0;
9036990d
SR
2740 tracing_stop();
2741 if (current_trace->stop)
2742 current_trace->stop(tr);
2743 }
bc0c38d1
SR
2744 }
2745 mutex_unlock(&trace_types_lock);
2746
cf8517cf 2747 *ppos += cnt;
bc0c38d1
SR
2748
2749 return cnt;
2750}
2751
2752static ssize_t
2753tracing_set_trace_read(struct file *filp, char __user *ubuf,
2754 size_t cnt, loff_t *ppos)
2755{
ee6c2c1b 2756 char buf[MAX_TRACER_SIZE+2];
bc0c38d1
SR
2757 int r;
2758
2759 mutex_lock(&trace_types_lock);
2760 if (current_trace)
2761 r = sprintf(buf, "%s\n", current_trace->name);
2762 else
2763 r = sprintf(buf, "\n");
2764 mutex_unlock(&trace_types_lock);
2765
4bf39a94 2766 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2767}
2768
b6f11df2
ACM
2769int tracer_init(struct tracer *t, struct trace_array *tr)
2770{
2771 tracing_reset_online_cpus(tr);
2772 return t->init(tr);
2773}
2774
4f271a2a 2775static int __tracing_resize_ring_buffer(unsigned long size)
73c5162a
SR
2776{
2777 int ret;
2778
2779 /*
2780 * If kernel or user changes the size of the ring buffer
a123c52b
SR
2781 * we use the size that was given, and we can forget about
2782 * expanding it later.
73c5162a
SR
2783 */
2784 ring_buffer_expanded = 1;
2785
2786 ret = ring_buffer_resize(global_trace.buffer, size);
2787 if (ret < 0)
2788 return ret;
2789
ef710e10
KM
2790 if (!current_trace->use_max_tr)
2791 goto out;
2792
73c5162a
SR
2793 ret = ring_buffer_resize(max_tr.buffer, size);
2794 if (ret < 0) {
2795 int r;
2796
2797 r = ring_buffer_resize(global_trace.buffer,
2798 global_trace.entries);
2799 if (r < 0) {
a123c52b
SR
2800 /*
2801 * AARGH! We are left with different
2802 * size max buffer!!!!
2803 * The max buffer is our "snapshot" buffer.
2804 * When a tracer needs a snapshot (one of the
2805 * latency tracers), it swaps the max buffer
2806 * with the saved snap shot. We succeeded to
2807 * update the size of the main buffer, but failed to
2808 * update the size of the max buffer. But when we tried
2809 * to reset the main buffer to the original size, we
2810 * failed there too. This is very unlikely to
2811 * happen, but if it does, warn and kill all
2812 * tracing.
2813 */
73c5162a
SR
2814 WARN_ON(1);
2815 tracing_disabled = 1;
2816 }
2817 return ret;
2818 }
2819
ef710e10
KM
2820 max_tr.entries = size;
2821 out:
73c5162a
SR
2822 global_trace.entries = size;
2823
2824 return ret;
2825}
2826
4f271a2a
VN
2827static ssize_t tracing_resize_ring_buffer(unsigned long size)
2828{
2829 int cpu, ret = size;
2830
2831 mutex_lock(&trace_types_lock);
2832
2833 tracing_stop();
2834
2835 /* disable all cpu buffers */
2836 for_each_tracing_cpu(cpu) {
2837 if (global_trace.data[cpu])
2838 atomic_inc(&global_trace.data[cpu]->disabled);
2839 if (max_tr.data[cpu])
2840 atomic_inc(&max_tr.data[cpu]->disabled);
2841 }
2842
2843 if (size != global_trace.entries)
2844 ret = __tracing_resize_ring_buffer(size);
2845
2846 if (ret < 0)
2847 ret = -ENOMEM;
2848
2849 for_each_tracing_cpu(cpu) {
2850 if (global_trace.data[cpu])
2851 atomic_dec(&global_trace.data[cpu]->disabled);
2852 if (max_tr.data[cpu])
2853 atomic_dec(&max_tr.data[cpu]->disabled);
2854 }
2855
2856 tracing_start();
2857 mutex_unlock(&trace_types_lock);
2858
2859 return ret;
2860}
2861
ef710e10 2862
1852fcce
SR
2863/**
2864 * tracing_update_buffers - used by tracing facility to expand ring buffers
2865 *
2866 * To save on memory when the tracing is never used on a system with it
2867 * configured in. The ring buffers are set to a minimum size. But once
2868 * a user starts to use the tracing facility, then they need to grow
2869 * to their default size.
2870 *
2871 * This function is to be called when a tracer is about to be used.
2872 */
2873int tracing_update_buffers(void)
2874{
2875 int ret = 0;
2876
1027fcb2 2877 mutex_lock(&trace_types_lock);
1852fcce 2878 if (!ring_buffer_expanded)
4f271a2a 2879 ret = __tracing_resize_ring_buffer(trace_buf_size);
1027fcb2 2880 mutex_unlock(&trace_types_lock);
1852fcce
SR
2881
2882 return ret;
2883}
2884
577b785f
SR
2885struct trace_option_dentry;
2886
2887static struct trace_option_dentry *
2888create_trace_option_files(struct tracer *tracer);
2889
2890static void
2891destroy_trace_option_files(struct trace_option_dentry *topts);
2892
b2821ae6 2893static int tracing_set_tracer(const char *buf)
bc0c38d1 2894{
577b785f 2895 static struct trace_option_dentry *topts;
bc0c38d1
SR
2896 struct trace_array *tr = &global_trace;
2897 struct tracer *t;
d9e54076 2898 int ret = 0;
bc0c38d1 2899
1027fcb2
SR
2900 mutex_lock(&trace_types_lock);
2901
73c5162a 2902 if (!ring_buffer_expanded) {
4f271a2a 2903 ret = __tracing_resize_ring_buffer(trace_buf_size);
73c5162a 2904 if (ret < 0)
59f586db 2905 goto out;
73c5162a
SR
2906 ret = 0;
2907 }
2908
bc0c38d1
SR
2909 for (t = trace_types; t; t = t->next) {
2910 if (strcmp(t->name, buf) == 0)
2911 break;
2912 }
c2931e05
FW
2913 if (!t) {
2914 ret = -EINVAL;
2915 goto out;
2916 }
2917 if (t == current_trace)
bc0c38d1
SR
2918 goto out;
2919
9f029e83 2920 trace_branch_disable();
bc0c38d1
SR
2921 if (current_trace && current_trace->reset)
2922 current_trace->reset(tr);
ef710e10
KM
2923 if (current_trace && current_trace->use_max_tr) {
2924 /*
2925 * We don't free the ring buffer. instead, resize it because
2926 * The max_tr ring buffer has some state (e.g. ring->clock) and
2927 * we want preserve it.
2928 */
2929 ring_buffer_resize(max_tr.buffer, 1);
2930 max_tr.entries = 1;
2931 }
577b785f
SR
2932 destroy_trace_option_files(topts);
2933
bc0c38d1 2934 current_trace = t;
577b785f
SR
2935
2936 topts = create_trace_option_files(current_trace);
ef710e10
KM
2937 if (current_trace->use_max_tr) {
2938 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
2939 if (ret < 0)
2940 goto out;
2941 max_tr.entries = global_trace.entries;
2942 }
577b785f 2943
1c80025a 2944 if (t->init) {
b6f11df2 2945 ret = tracer_init(t, tr);
1c80025a
FW
2946 if (ret)
2947 goto out;
2948 }
bc0c38d1 2949
9f029e83 2950 trace_branch_enable(tr);
bc0c38d1
SR
2951 out:
2952 mutex_unlock(&trace_types_lock);
2953
d9e54076
PZ
2954 return ret;
2955}
2956
2957static ssize_t
2958tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2959 size_t cnt, loff_t *ppos)
2960{
ee6c2c1b 2961 char buf[MAX_TRACER_SIZE+1];
d9e54076
PZ
2962 int i;
2963 size_t ret;
e6e7a65a
FW
2964 int err;
2965
2966 ret = cnt;
d9e54076 2967
ee6c2c1b
LZ
2968 if (cnt > MAX_TRACER_SIZE)
2969 cnt = MAX_TRACER_SIZE;
d9e54076
PZ
2970
2971 if (copy_from_user(&buf, ubuf, cnt))
2972 return -EFAULT;
2973
2974 buf[cnt] = 0;
2975
2976 /* strip ending whitespace. */
2977 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2978 buf[i] = 0;
2979
e6e7a65a
FW
2980 err = tracing_set_tracer(buf);
2981 if (err)
2982 return err;
d9e54076 2983
cf8517cf 2984 *ppos += ret;
bc0c38d1 2985
c2931e05 2986 return ret;
bc0c38d1
SR
2987}
2988
2989static ssize_t
2990tracing_max_lat_read(struct file *filp, char __user *ubuf,
2991 size_t cnt, loff_t *ppos)
2992{
2993 unsigned long *ptr = filp->private_data;
2994 char buf[64];
2995 int r;
2996
cffae437 2997 r = snprintf(buf, sizeof(buf), "%ld\n",
bc0c38d1 2998 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
cffae437
SR
2999 if (r > sizeof(buf))
3000 r = sizeof(buf);
4bf39a94 3001 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
3002}
3003
3004static ssize_t
3005tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3006 size_t cnt, loff_t *ppos)
3007{
5e39841c 3008 unsigned long *ptr = filp->private_data;
bc0c38d1 3009 char buf[64];
5e39841c 3010 unsigned long val;
c6caeeb1 3011 int ret;
bc0c38d1 3012
cffae437
SR
3013 if (cnt >= sizeof(buf))
3014 return -EINVAL;
bc0c38d1
SR
3015
3016 if (copy_from_user(&buf, ubuf, cnt))
3017 return -EFAULT;
3018
3019 buf[cnt] = 0;
3020
c6caeeb1
SR
3021 ret = strict_strtoul(buf, 10, &val);
3022 if (ret < 0)
3023 return ret;
bc0c38d1
SR
3024
3025 *ptr = val * 1000;
3026
3027 return cnt;
3028}
3029
b3806b43
SR
3030static int tracing_open_pipe(struct inode *inode, struct file *filp)
3031{
b04cc6b1 3032 long cpu_file = (long) inode->i_private;
b3806b43 3033 struct trace_iterator *iter;
b04cc6b1 3034 int ret = 0;
b3806b43
SR
3035
3036 if (tracing_disabled)
3037 return -ENODEV;
3038
b04cc6b1
FW
3039 mutex_lock(&trace_types_lock);
3040
b3806b43
SR
3041 /* create a buffer to store the information to pass to userspace */
3042 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
b04cc6b1
FW
3043 if (!iter) {
3044 ret = -ENOMEM;
3045 goto out;
3046 }
b3806b43 3047
d7350c3f
FW
3048 /*
3049 * We make a copy of the current tracer to avoid concurrent
3050 * changes on it while we are reading.
3051 */
3052 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3053 if (!iter->trace) {
3054 ret = -ENOMEM;
3055 goto fail;
3056 }
3057 if (current_trace)
3058 *iter->trace = *current_trace;
3059
4462344e 3060 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
b04cc6b1 3061 ret = -ENOMEM;
d7350c3f 3062 goto fail;
4462344e
RR
3063 }
3064
a309720c 3065 /* trace pipe does not show start of buffer */
4462344e 3066 cpumask_setall(iter->started);
a309720c 3067
112f38a7
SR
3068 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3069 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3070
b04cc6b1 3071 iter->cpu_file = cpu_file;
b3806b43 3072 iter->tr = &global_trace;
d7350c3f 3073 mutex_init(&iter->mutex);
b3806b43
SR
3074 filp->private_data = iter;
3075
107bad8b
SR
3076 if (iter->trace->pipe_open)
3077 iter->trace->pipe_open(iter);
107bad8b 3078
b444786f 3079 nonseekable_open(inode, filp);
b04cc6b1
FW
3080out:
3081 mutex_unlock(&trace_types_lock);
3082 return ret;
d7350c3f
FW
3083
3084fail:
3085 kfree(iter->trace);
3086 kfree(iter);
3087 mutex_unlock(&trace_types_lock);
3088 return ret;
b3806b43
SR
3089}
3090
3091static int tracing_release_pipe(struct inode *inode, struct file *file)
3092{
3093 struct trace_iterator *iter = file->private_data;
3094
b04cc6b1
FW
3095 mutex_lock(&trace_types_lock);
3096
29bf4a5e 3097 if (iter->trace->pipe_close)
c521efd1
SR
3098 iter->trace->pipe_close(iter);
3099
b04cc6b1
FW
3100 mutex_unlock(&trace_types_lock);
3101
4462344e 3102 free_cpumask_var(iter->started);
d7350c3f
FW
3103 mutex_destroy(&iter->mutex);
3104 kfree(iter->trace);
b3806b43 3105 kfree(iter);
b3806b43
SR
3106
3107 return 0;
3108}
3109
2a2cc8f7
SSP
3110static unsigned int
3111tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3112{
3113 struct trace_iterator *iter = filp->private_data;
3114
3115 if (trace_flags & TRACE_ITER_BLOCK) {
3116 /*
3117 * Always select as readable when in blocking mode
3118 */
3119 return POLLIN | POLLRDNORM;
afc2abc0 3120 } else {
2a2cc8f7
SSP
3121 if (!trace_empty(iter))
3122 return POLLIN | POLLRDNORM;
3123 poll_wait(filp, &trace_wait, poll_table);
3124 if (!trace_empty(iter))
3125 return POLLIN | POLLRDNORM;
3126
3127 return 0;
3128 }
3129}
3130
6eaaa5d5
FW
3131
3132void default_wait_pipe(struct trace_iterator *iter)
3133{
3134 DEFINE_WAIT(wait);
3135
3136 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3137
3138 if (trace_empty(iter))
3139 schedule();
3140
3141 finish_wait(&trace_wait, &wait);
3142}
3143
3144/*
3145 * This is a make-shift waitqueue.
3146 * A tracer might use this callback on some rare cases:
3147 *
3148 * 1) the current tracer might hold the runqueue lock when it wakes up
3149 * a reader, hence a deadlock (sched, function, and function graph tracers)
3150 * 2) the function tracers, trace all functions, we don't want
3151 * the overhead of calling wake_up and friends
3152 * (and tracing them too)
3153 *
3154 * Anyway, this is really very primitive wakeup.
3155 */
3156void poll_wait_pipe(struct trace_iterator *iter)
3157{
3158 set_current_state(TASK_INTERRUPTIBLE);
3159 /* sleep for 100 msecs, and try again. */
3160 schedule_timeout(HZ / 10);
3161}
3162
ff98781b
EGM
3163/* Must be called with trace_types_lock mutex held. */
3164static int tracing_wait_pipe(struct file *filp)
b3806b43
SR
3165{
3166 struct trace_iterator *iter = filp->private_data;
b3806b43 3167
b3806b43 3168 while (trace_empty(iter)) {
2dc8f095 3169
107bad8b 3170 if ((filp->f_flags & O_NONBLOCK)) {
ff98781b 3171 return -EAGAIN;
107bad8b 3172 }
2dc8f095 3173
d7350c3f 3174 mutex_unlock(&iter->mutex);
107bad8b 3175
6eaaa5d5 3176 iter->trace->wait_pipe(iter);
b3806b43 3177
d7350c3f 3178 mutex_lock(&iter->mutex);
107bad8b 3179
6eaaa5d5 3180 if (signal_pending(current))
ff98781b 3181 return -EINTR;
b3806b43
SR
3182
3183 /*
3184 * We block until we read something and tracing is disabled.
3185 * We still block if tracing is disabled, but we have never
3186 * read anything. This allows a user to cat this file, and
3187 * then enable tracing. But after we have read something,
3188 * we give an EOF when tracing is again disabled.
3189 *
3190 * iter->pos will be 0 if we haven't read anything.
3191 */
3192 if (!tracer_enabled && iter->pos)
3193 break;
b3806b43
SR
3194 }
3195
ff98781b
EGM
3196 return 1;
3197}
3198
3199/*
3200 * Consumer reader.
3201 */
3202static ssize_t
3203tracing_read_pipe(struct file *filp, char __user *ubuf,
3204 size_t cnt, loff_t *ppos)
3205{
3206 struct trace_iterator *iter = filp->private_data;
d7350c3f 3207 static struct tracer *old_tracer;
ff98781b
EGM
3208 ssize_t sret;
3209
3210 /* return any leftover data */
3211 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3212 if (sret != -EBUSY)
3213 return sret;
3214
f9520750 3215 trace_seq_init(&iter->seq);
ff98781b 3216
d7350c3f 3217 /* copy the tracer to avoid using a global lock all around */
ff98781b 3218 mutex_lock(&trace_types_lock);
d7350c3f
FW
3219 if (unlikely(old_tracer != current_trace && current_trace)) {
3220 old_tracer = current_trace;
3221 *iter->trace = *current_trace;
3222 }
3223 mutex_unlock(&trace_types_lock);
3224
3225 /*
3226 * Avoid more than one consumer on a single file descriptor
3227 * This is just a matter of traces coherency, the ring buffer itself
3228 * is protected.
3229 */
3230 mutex_lock(&iter->mutex);
ff98781b
EGM
3231 if (iter->trace->read) {
3232 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3233 if (sret)
3234 goto out;
3235 }
3236
3237waitagain:
3238 sret = tracing_wait_pipe(filp);
3239 if (sret <= 0)
3240 goto out;
3241
b3806b43 3242 /* stop when tracing is finished */
ff98781b
EGM
3243 if (trace_empty(iter)) {
3244 sret = 0;
107bad8b 3245 goto out;
ff98781b 3246 }
b3806b43
SR
3247
3248 if (cnt >= PAGE_SIZE)
3249 cnt = PAGE_SIZE - 1;
3250
53d0aa77 3251 /* reset all but tr, trace, and overruns */
53d0aa77
SR
3252 memset(&iter->seq, 0,
3253 sizeof(struct trace_iterator) -
3254 offsetof(struct trace_iterator, seq));
4823ed7e 3255 iter->pos = -1;
b3806b43 3256
4f535968 3257 trace_event_read_lock();
7e53bd42 3258 trace_access_lock(iter->cpu_file);
955b61e5 3259 while (trace_find_next_entry_inc(iter) != NULL) {
2c4f035f 3260 enum print_line_t ret;
088b1e42
SR
3261 int len = iter->seq.len;
3262
f9896bf3 3263 ret = print_trace_line(iter);
2c4f035f 3264 if (ret == TRACE_TYPE_PARTIAL_LINE) {
088b1e42
SR
3265 /* don't print partial lines */
3266 iter->seq.len = len;
b3806b43 3267 break;
088b1e42 3268 }
b91facc3
FW
3269 if (ret != TRACE_TYPE_NO_CONSUME)
3270 trace_consume(iter);
b3806b43
SR
3271
3272 if (iter->seq.len >= cnt)
3273 break;
ee5e51f5
JO
3274
3275 /*
3276 * Setting the full flag means we reached the trace_seq buffer
3277 * size and we should leave by partial output condition above.
3278 * One of the trace_seq_* functions is not used properly.
3279 */
3280 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3281 iter->ent->type);
b3806b43 3282 }
7e53bd42 3283 trace_access_unlock(iter->cpu_file);
4f535968 3284 trace_event_read_unlock();
b3806b43 3285
b3806b43 3286 /* Now copy what we have to the user */
6c6c2796
PP
3287 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3288 if (iter->seq.readpos >= iter->seq.len)
f9520750 3289 trace_seq_init(&iter->seq);
9ff4b974
PP
3290
3291 /*
25985edc 3292 * If there was nothing to send to user, in spite of consuming trace
9ff4b974
PP
3293 * entries, go back to wait for more entries.
3294 */
6c6c2796 3295 if (sret == -EBUSY)
9ff4b974 3296 goto waitagain;
b3806b43 3297
107bad8b 3298out:
d7350c3f 3299 mutex_unlock(&iter->mutex);
107bad8b 3300
6c6c2796 3301 return sret;
b3806b43
SR
3302}
3303
3c56819b
EGM
3304static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3305 struct pipe_buffer *buf)
3306{
3307 __free_page(buf->page);
3308}
3309
3310static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3311 unsigned int idx)
3312{
3313 __free_page(spd->pages[idx]);
3314}
3315
28dfef8f 3316static const struct pipe_buf_operations tracing_pipe_buf_ops = {
34cd4998
SR
3317 .can_merge = 0,
3318 .map = generic_pipe_buf_map,
3319 .unmap = generic_pipe_buf_unmap,
3320 .confirm = generic_pipe_buf_confirm,
3321 .release = tracing_pipe_buf_release,
3322 .steal = generic_pipe_buf_steal,
3323 .get = generic_pipe_buf_get,
3c56819b
EGM
3324};
3325
34cd4998 3326static size_t
fa7c7f6e 3327tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
34cd4998
SR
3328{
3329 size_t count;
3330 int ret;
3331
3332 /* Seq buffer is page-sized, exactly what we need. */
3333 for (;;) {
3334 count = iter->seq.len;
3335 ret = print_trace_line(iter);
3336 count = iter->seq.len - count;
3337 if (rem < count) {
3338 rem = 0;
3339 iter->seq.len -= count;
3340 break;
3341 }
3342 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3343 iter->seq.len -= count;
3344 break;
3345 }
3346
74e7ff8c
LJ
3347 if (ret != TRACE_TYPE_NO_CONSUME)
3348 trace_consume(iter);
34cd4998 3349 rem -= count;
955b61e5 3350 if (!trace_find_next_entry_inc(iter)) {
34cd4998
SR
3351 rem = 0;
3352 iter->ent = NULL;
3353 break;
3354 }
3355 }
3356
3357 return rem;
3358}
3359
3c56819b
EGM
3360static ssize_t tracing_splice_read_pipe(struct file *filp,
3361 loff_t *ppos,
3362 struct pipe_inode_info *pipe,
3363 size_t len,
3364 unsigned int flags)
3365{
35f3d14d
JA
3366 struct page *pages_def[PIPE_DEF_BUFFERS];
3367 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3c56819b
EGM
3368 struct trace_iterator *iter = filp->private_data;
3369 struct splice_pipe_desc spd = {
35f3d14d
JA
3370 .pages = pages_def,
3371 .partial = partial_def,
34cd4998
SR
3372 .nr_pages = 0, /* This gets updated below. */
3373 .flags = flags,
3374 .ops = &tracing_pipe_buf_ops,
3375 .spd_release = tracing_spd_release_pipe,
3c56819b 3376 };
d7350c3f 3377 static struct tracer *old_tracer;
3c56819b 3378 ssize_t ret;
34cd4998 3379 size_t rem;
3c56819b
EGM
3380 unsigned int i;
3381
35f3d14d
JA
3382 if (splice_grow_spd(pipe, &spd))
3383 return -ENOMEM;
3384
d7350c3f 3385 /* copy the tracer to avoid using a global lock all around */
3c56819b 3386 mutex_lock(&trace_types_lock);
d7350c3f
FW
3387 if (unlikely(old_tracer != current_trace && current_trace)) {
3388 old_tracer = current_trace;
3389 *iter->trace = *current_trace;
3390 }
3391 mutex_unlock(&trace_types_lock);
3392
3393 mutex_lock(&iter->mutex);
3c56819b
EGM
3394
3395 if (iter->trace->splice_read) {
3396 ret = iter->trace->splice_read(iter, filp,
3397 ppos, pipe, len, flags);
3398 if (ret)
34cd4998 3399 goto out_err;
3c56819b
EGM
3400 }
3401
3402 ret = tracing_wait_pipe(filp);
3403 if (ret <= 0)
34cd4998 3404 goto out_err;
3c56819b 3405
955b61e5 3406 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3c56819b 3407 ret = -EFAULT;
34cd4998 3408 goto out_err;
3c56819b
EGM
3409 }
3410
4f535968 3411 trace_event_read_lock();
7e53bd42 3412 trace_access_lock(iter->cpu_file);
4f535968 3413
3c56819b 3414 /* Fill as many pages as possible. */
35f3d14d
JA
3415 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3416 spd.pages[i] = alloc_page(GFP_KERNEL);
3417 if (!spd.pages[i])
34cd4998 3418 break;
3c56819b 3419
fa7c7f6e 3420 rem = tracing_fill_pipe_page(rem, iter);
3c56819b
EGM
3421
3422 /* Copy the data into the page, so we can start over. */
3423 ret = trace_seq_to_buffer(&iter->seq,
35f3d14d 3424 page_address(spd.pages[i]),
3c56819b
EGM
3425 iter->seq.len);
3426 if (ret < 0) {
35f3d14d 3427 __free_page(spd.pages[i]);
3c56819b
EGM
3428 break;
3429 }
35f3d14d
JA
3430 spd.partial[i].offset = 0;
3431 spd.partial[i].len = iter->seq.len;
3c56819b 3432
f9520750 3433 trace_seq_init(&iter->seq);
3c56819b
EGM
3434 }
3435
7e53bd42 3436 trace_access_unlock(iter->cpu_file);
4f535968 3437 trace_event_read_unlock();
d7350c3f 3438 mutex_unlock(&iter->mutex);
3c56819b
EGM
3439
3440 spd.nr_pages = i;
3441
35f3d14d
JA
3442 ret = splice_to_pipe(pipe, &spd);
3443out:
3444 splice_shrink_spd(pipe, &spd);
3445 return ret;
3c56819b 3446
34cd4998 3447out_err:
d7350c3f 3448 mutex_unlock(&iter->mutex);
35f3d14d 3449 goto out;
3c56819b
EGM
3450}
3451
a98a3c3f
SR
3452static ssize_t
3453tracing_entries_read(struct file *filp, char __user *ubuf,
3454 size_t cnt, loff_t *ppos)
3455{
3456 struct trace_array *tr = filp->private_data;
db526ca3 3457 char buf[96];
a98a3c3f
SR
3458 int r;
3459
db526ca3
SR
3460 mutex_lock(&trace_types_lock);
3461 if (!ring_buffer_expanded)
3462 r = sprintf(buf, "%lu (expanded: %lu)\n",
3463 tr->entries >> 10,
3464 trace_buf_size >> 10);
3465 else
3466 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3467 mutex_unlock(&trace_types_lock);
3468
a98a3c3f
SR
3469 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3470}
3471
3472static ssize_t
3473tracing_entries_write(struct file *filp, const char __user *ubuf,
3474 size_t cnt, loff_t *ppos)
3475{
3476 unsigned long val;
3477 char buf[64];
4f271a2a 3478 int ret;
a98a3c3f 3479
cffae437
SR
3480 if (cnt >= sizeof(buf))
3481 return -EINVAL;
a98a3c3f
SR
3482
3483 if (copy_from_user(&buf, ubuf, cnt))
3484 return -EFAULT;
3485
3486 buf[cnt] = 0;
3487
c6caeeb1
SR
3488 ret = strict_strtoul(buf, 10, &val);
3489 if (ret < 0)
3490 return ret;
a98a3c3f
SR
3491
3492 /* must have at least 1 entry */
3493 if (!val)
3494 return -EINVAL;
3495
1696b2b0
SR
3496 /* value is in KB */
3497 val <<= 10;
3498
4f271a2a
VN
3499 ret = tracing_resize_ring_buffer(val);
3500 if (ret < 0)
3501 return ret;
a98a3c3f 3502
cf8517cf 3503 *ppos += cnt;
a98a3c3f 3504
4f271a2a
VN
3505 return cnt;
3506}
bf5e6519 3507
4f271a2a
VN
3508static ssize_t
3509tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3510 size_t cnt, loff_t *ppos)
3511{
3512 /*
3513 * There is no need to read what the user has written, this function
3514 * is just to make sure that there is no error when "echo" is used
3515 */
3516
3517 *ppos += cnt;
a98a3c3f
SR
3518
3519 return cnt;
3520}
3521
4f271a2a
VN
3522static int
3523tracing_free_buffer_release(struct inode *inode, struct file *filp)
3524{
cf30cf67
SR
3525 /* disable tracing ? */
3526 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3527 tracing_off();
4f271a2a
VN
3528 /* resize the ring buffer to 0 */
3529 tracing_resize_ring_buffer(0);
3530
3531 return 0;
3532}
3533
f2942487
CE
3534static int mark_printk(const char *fmt, ...)
3535{
3536 int ret;
3537 va_list args;
3538 va_start(args, fmt);
3539 ret = trace_vprintk(0, fmt, args);
3540 va_end(args);
3541 return ret;
3542}
3543
5bf9a1ee
PP
3544static ssize_t
3545tracing_mark_write(struct file *filp, const char __user *ubuf,
3546 size_t cnt, loff_t *fpos)
3547{
3548 char *buf;
1aa54bca 3549 size_t written;
5bf9a1ee 3550
c76f0694 3551 if (tracing_disabled)
5bf9a1ee
PP
3552 return -EINVAL;
3553
3554 if (cnt > TRACE_BUF_SIZE)
3555 cnt = TRACE_BUF_SIZE;
3556
c13d2f7c 3557 buf = kmalloc(cnt + 2, GFP_KERNEL);
5bf9a1ee
PP
3558 if (buf == NULL)
3559 return -ENOMEM;
3560
3561 if (copy_from_user(buf, ubuf, cnt)) {
3562 kfree(buf);
3563 return -EFAULT;
3564 }
c13d2f7c
CE
3565 if (buf[cnt-1] != '\n') {
3566 buf[cnt] = '\n';
3567 buf[cnt+1] = '\0';
3568 } else
3569 buf[cnt] = '\0';
5bf9a1ee 3570
1aa54bca 3571 written = mark_printk("%s", buf);
5bf9a1ee 3572 kfree(buf);
1aa54bca 3573 *fpos += written;
5bf9a1ee 3574
1aa54bca
MS
3575 /* don't tell userspace we wrote more - it might confuse them */
3576 if (written > cnt)
3577 written = cnt;
3578
3579 return written;
5bf9a1ee
PP
3580}
3581
13f16d20 3582static int tracing_clock_show(struct seq_file *m, void *v)
5079f326 3583{
5079f326
Z
3584 int i;
3585
3586 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
13f16d20 3587 seq_printf(m,
5079f326
Z
3588 "%s%s%s%s", i ? " " : "",
3589 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3590 i == trace_clock_id ? "]" : "");
13f16d20 3591 seq_putc(m, '\n');
5079f326 3592
13f16d20 3593 return 0;
5079f326
Z
3594}
3595
3596static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3597 size_t cnt, loff_t *fpos)
3598{
3599 char buf[64];
3600 const char *clockstr;
3601 int i;
3602
3603 if (cnt >= sizeof(buf))
3604 return -EINVAL;
3605
3606 if (copy_from_user(&buf, ubuf, cnt))
3607 return -EFAULT;
3608
3609 buf[cnt] = 0;
3610
3611 clockstr = strstrip(buf);
3612
3613 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3614 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3615 break;
3616 }
3617 if (i == ARRAY_SIZE(trace_clocks))
3618 return -EINVAL;
3619
3620 trace_clock_id = i;
3621
3622 mutex_lock(&trace_types_lock);
3623
3624 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3625 if (max_tr.buffer)
3626 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3627
3628 mutex_unlock(&trace_types_lock);
3629
3630 *fpos += cnt;
3631
3632 return cnt;
3633}
3634
13f16d20
LZ
3635static int tracing_clock_open(struct inode *inode, struct file *file)
3636{
3637 if (tracing_disabled)
3638 return -ENODEV;
3639 return single_open(file, tracing_clock_show, NULL);
3640}
3641
5e2336a0 3642static const struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
3643 .open = tracing_open_generic,
3644 .read = tracing_max_lat_read,
3645 .write = tracing_max_lat_write,
b444786f 3646 .llseek = generic_file_llseek,
bc0c38d1
SR
3647};
3648
5e2336a0 3649static const struct file_operations tracing_ctrl_fops = {
4bf39a94
IM
3650 .open = tracing_open_generic,
3651 .read = tracing_ctrl_read,
3652 .write = tracing_ctrl_write,
b444786f 3653 .llseek = generic_file_llseek,
bc0c38d1
SR
3654};
3655
5e2336a0 3656static const struct file_operations set_tracer_fops = {
4bf39a94
IM
3657 .open = tracing_open_generic,
3658 .read = tracing_set_trace_read,
3659 .write = tracing_set_trace_write,
b444786f 3660 .llseek = generic_file_llseek,
bc0c38d1
SR
3661};
3662
5e2336a0 3663static const struct file_operations tracing_pipe_fops = {
4bf39a94 3664 .open = tracing_open_pipe,
2a2cc8f7 3665 .poll = tracing_poll_pipe,
4bf39a94 3666 .read = tracing_read_pipe,
3c56819b 3667 .splice_read = tracing_splice_read_pipe,
4bf39a94 3668 .release = tracing_release_pipe,
b444786f 3669 .llseek = no_llseek,
b3806b43
SR
3670};
3671
5e2336a0 3672static const struct file_operations tracing_entries_fops = {
a98a3c3f
SR
3673 .open = tracing_open_generic,
3674 .read = tracing_entries_read,
3675 .write = tracing_entries_write,
b444786f 3676 .llseek = generic_file_llseek,
a98a3c3f
SR
3677};
3678
4f271a2a
VN
3679static const struct file_operations tracing_free_buffer_fops = {
3680 .write = tracing_free_buffer_write,
3681 .release = tracing_free_buffer_release,
3682};
3683
5e2336a0 3684static const struct file_operations tracing_mark_fops = {
43a15386 3685 .open = tracing_open_generic,
5bf9a1ee 3686 .write = tracing_mark_write,
b444786f 3687 .llseek = generic_file_llseek,
5bf9a1ee
PP
3688};
3689
5079f326 3690static const struct file_operations trace_clock_fops = {
13f16d20
LZ
3691 .open = tracing_clock_open,
3692 .read = seq_read,
3693 .llseek = seq_lseek,
3694 .release = single_release,
5079f326
Z
3695 .write = tracing_clock_write,
3696};
3697
2cadf913
SR
3698struct ftrace_buffer_info {
3699 struct trace_array *tr;
3700 void *spare;
3701 int cpu;
3702 unsigned int read;
3703};
3704
3705static int tracing_buffers_open(struct inode *inode, struct file *filp)
3706{
3707 int cpu = (int)(long)inode->i_private;
3708 struct ftrace_buffer_info *info;
3709
3710 if (tracing_disabled)
3711 return -ENODEV;
3712
3713 info = kzalloc(sizeof(*info), GFP_KERNEL);
3714 if (!info)
3715 return -ENOMEM;
3716
3717 info->tr = &global_trace;
3718 info->cpu = cpu;
ddd538f3 3719 info->spare = NULL;
2cadf913
SR
3720 /* Force reading ring buffer for first read */
3721 info->read = (unsigned int)-1;
2cadf913
SR
3722
3723 filp->private_data = info;
3724
d1e7e02f 3725 return nonseekable_open(inode, filp);
2cadf913
SR
3726}
3727
3728static ssize_t
3729tracing_buffers_read(struct file *filp, char __user *ubuf,
3730 size_t count, loff_t *ppos)
3731{
3732 struct ftrace_buffer_info *info = filp->private_data;
2cadf913
SR
3733 ssize_t ret;
3734 size_t size;
3735
2dc5d12b
SR
3736 if (!count)
3737 return 0;
3738
ddd538f3 3739 if (!info->spare)
7ea59064 3740 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
ddd538f3
LJ
3741 if (!info->spare)
3742 return -ENOMEM;
3743
2cadf913
SR
3744 /* Do we have previous read data to read? */
3745 if (info->read < PAGE_SIZE)
3746 goto read;
3747
3748 info->read = 0;
3749
7e53bd42 3750 trace_access_lock(info->cpu);
2cadf913
SR
3751 ret = ring_buffer_read_page(info->tr->buffer,
3752 &info->spare,
3753 count,
3754 info->cpu, 0);
7e53bd42 3755 trace_access_unlock(info->cpu);
2cadf913
SR
3756 if (ret < 0)
3757 return 0;
3758
2cadf913
SR
3759read:
3760 size = PAGE_SIZE - info->read;
3761 if (size > count)
3762 size = count;
3763
3764 ret = copy_to_user(ubuf, info->spare + info->read, size);
2dc5d12b 3765 if (ret == size)
2cadf913 3766 return -EFAULT;
2dc5d12b
SR
3767 size -= ret;
3768
2cadf913
SR
3769 *ppos += size;
3770 info->read += size;
3771
3772 return size;
3773}
3774
3775static int tracing_buffers_release(struct inode *inode, struct file *file)
3776{
3777 struct ftrace_buffer_info *info = file->private_data;
3778
ddd538f3
LJ
3779 if (info->spare)
3780 ring_buffer_free_read_page(info->tr->buffer, info->spare);
2cadf913
SR
3781 kfree(info);
3782
3783 return 0;
3784}
3785
3786struct buffer_ref {
3787 struct ring_buffer *buffer;
3788 void *page;
3789 int ref;
3790};
3791
3792static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3793 struct pipe_buffer *buf)
3794{
3795 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3796
3797 if (--ref->ref)
3798 return;
3799
3800 ring_buffer_free_read_page(ref->buffer, ref->page);
3801 kfree(ref);
3802 buf->private = 0;
3803}
3804
3805static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3806 struct pipe_buffer *buf)
3807{
3808 return 1;
3809}
3810
3811static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3812 struct pipe_buffer *buf)
3813{
3814 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3815
3816 ref->ref++;
3817}
3818
3819/* Pipe buffer operations for a buffer. */
28dfef8f 3820static const struct pipe_buf_operations buffer_pipe_buf_ops = {
2cadf913
SR
3821 .can_merge = 0,
3822 .map = generic_pipe_buf_map,
3823 .unmap = generic_pipe_buf_unmap,
3824 .confirm = generic_pipe_buf_confirm,
3825 .release = buffer_pipe_buf_release,
3826 .steal = buffer_pipe_buf_steal,
3827 .get = buffer_pipe_buf_get,
3828};
3829
3830/*
3831 * Callback from splice_to_pipe(), if we need to release some pages
3832 * at the end of the spd in case we error'ed out in filling the pipe.
3833 */
3834static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3835{
3836 struct buffer_ref *ref =
3837 (struct buffer_ref *)spd->partial[i].private;
3838
3839 if (--ref->ref)
3840 return;
3841
3842 ring_buffer_free_read_page(ref->buffer, ref->page);
3843 kfree(ref);
3844 spd->partial[i].private = 0;
3845}
3846
3847static ssize_t
3848tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3849 struct pipe_inode_info *pipe, size_t len,
3850 unsigned int flags)
3851{
3852 struct ftrace_buffer_info *info = file->private_data;
35f3d14d
JA
3853 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3854 struct page *pages_def[PIPE_DEF_BUFFERS];
2cadf913 3855 struct splice_pipe_desc spd = {
35f3d14d
JA
3856 .pages = pages_def,
3857 .partial = partial_def,
2cadf913
SR
3858 .flags = flags,
3859 .ops = &buffer_pipe_buf_ops,
3860 .spd_release = buffer_spd_release,
3861 };
3862 struct buffer_ref *ref;
93459c6c 3863 int entries, size, i;
2cadf913
SR
3864 size_t ret;
3865
35f3d14d
JA
3866 if (splice_grow_spd(pipe, &spd))
3867 return -ENOMEM;
3868
93cfb3c9
LJ
3869 if (*ppos & (PAGE_SIZE - 1)) {
3870 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
35f3d14d
JA
3871 ret = -EINVAL;
3872 goto out;
93cfb3c9
LJ
3873 }
3874
3875 if (len & (PAGE_SIZE - 1)) {
3876 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
35f3d14d
JA
3877 if (len < PAGE_SIZE) {
3878 ret = -EINVAL;
3879 goto out;
3880 }
93cfb3c9
LJ
3881 len &= PAGE_MASK;
3882 }
3883
7e53bd42 3884 trace_access_lock(info->cpu);
93459c6c
SR
3885 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3886
35f3d14d 3887 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
2cadf913
SR
3888 struct page *page;
3889 int r;
3890
3891 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3892 if (!ref)
3893 break;
3894
7267fa68 3895 ref->ref = 1;
2cadf913 3896 ref->buffer = info->tr->buffer;
7ea59064 3897 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
2cadf913
SR
3898 if (!ref->page) {
3899 kfree(ref);
3900 break;
3901 }
3902
3903 r = ring_buffer_read_page(ref->buffer, &ref->page,
f2957f1f 3904 len, info->cpu, 1);
2cadf913 3905 if (r < 0) {
7ea59064 3906 ring_buffer_free_read_page(ref->buffer, ref->page);
2cadf913
SR
3907 kfree(ref);
3908 break;
3909 }
3910
3911 /*
3912 * zero out any left over data, this is going to
3913 * user land.
3914 */
3915 size = ring_buffer_page_len(ref->page);
3916 if (size < PAGE_SIZE)
3917 memset(ref->page + size, 0, PAGE_SIZE - size);
3918
3919 page = virt_to_page(ref->page);
3920
3921 spd.pages[i] = page;
3922 spd.partial[i].len = PAGE_SIZE;
3923 spd.partial[i].offset = 0;
3924 spd.partial[i].private = (unsigned long)ref;
3925 spd.nr_pages++;
93cfb3c9 3926 *ppos += PAGE_SIZE;
93459c6c
SR
3927
3928 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
2cadf913
SR
3929 }
3930
7e53bd42 3931 trace_access_unlock(info->cpu);
2cadf913
SR
3932 spd.nr_pages = i;
3933
3934 /* did we read anything? */
3935 if (!spd.nr_pages) {
3936 if (flags & SPLICE_F_NONBLOCK)
3937 ret = -EAGAIN;
3938 else
3939 ret = 0;
3940 /* TODO: block */
35f3d14d 3941 goto out;
2cadf913
SR
3942 }
3943
3944 ret = splice_to_pipe(pipe, &spd);
35f3d14d
JA
3945 splice_shrink_spd(pipe, &spd);
3946out:
2cadf913
SR
3947 return ret;
3948}
3949
3950static const struct file_operations tracing_buffers_fops = {
3951 .open = tracing_buffers_open,
3952 .read = tracing_buffers_read,
3953 .release = tracing_buffers_release,
3954 .splice_read = tracing_buffers_splice_read,
3955 .llseek = no_llseek,
3956};
3957
c8d77183
SR
3958static ssize_t
3959tracing_stats_read(struct file *filp, char __user *ubuf,
3960 size_t count, loff_t *ppos)
3961{
3962 unsigned long cpu = (unsigned long)filp->private_data;
3963 struct trace_array *tr = &global_trace;
3964 struct trace_seq *s;
3965 unsigned long cnt;
3966
e4f2d10f 3967 s = kmalloc(sizeof(*s), GFP_KERNEL);
c8d77183 3968 if (!s)
a646365c 3969 return -ENOMEM;
c8d77183
SR
3970
3971 trace_seq_init(s);
3972
3973 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3974 trace_seq_printf(s, "entries: %ld\n", cnt);
3975
3976 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3977 trace_seq_printf(s, "overrun: %ld\n", cnt);
3978
3979 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3980 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3981
c8d77183
SR
3982 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3983
3984 kfree(s);
3985
3986 return count;
3987}
3988
3989static const struct file_operations tracing_stats_fops = {
3990 .open = tracing_open_generic,
3991 .read = tracing_stats_read,
b444786f 3992 .llseek = generic_file_llseek,
c8d77183
SR
3993};
3994
bc0c38d1
SR
3995#ifdef CONFIG_DYNAMIC_FTRACE
3996
b807c3d0
SR
3997int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3998{
3999 return 0;
4000}
4001
bc0c38d1 4002static ssize_t
b807c3d0 4003tracing_read_dyn_info(struct file *filp, char __user *ubuf,
bc0c38d1
SR
4004 size_t cnt, loff_t *ppos)
4005{
a26a2a27
SR
4006 static char ftrace_dyn_info_buffer[1024];
4007 static DEFINE_MUTEX(dyn_info_mutex);
bc0c38d1 4008 unsigned long *p = filp->private_data;
b807c3d0 4009 char *buf = ftrace_dyn_info_buffer;
a26a2a27 4010 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
bc0c38d1
SR
4011 int r;
4012
b807c3d0
SR
4013 mutex_lock(&dyn_info_mutex);
4014 r = sprintf(buf, "%ld ", *p);
4bf39a94 4015
a26a2a27 4016 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
b807c3d0
SR
4017 buf[r++] = '\n';
4018
4019 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4020
4021 mutex_unlock(&dyn_info_mutex);
4022
4023 return r;
bc0c38d1
SR
4024}
4025
5e2336a0 4026static const struct file_operations tracing_dyn_info_fops = {
4bf39a94 4027 .open = tracing_open_generic,
b807c3d0 4028 .read = tracing_read_dyn_info,
b444786f 4029 .llseek = generic_file_llseek,
bc0c38d1
SR
4030};
4031#endif
4032
4033static struct dentry *d_tracer;
4034
4035struct dentry *tracing_init_dentry(void)
4036{
4037 static int once;
4038
4039 if (d_tracer)
4040 return d_tracer;
4041
3e1f60b8
FW
4042 if (!debugfs_initialized())
4043 return NULL;
4044
bc0c38d1
SR
4045 d_tracer = debugfs_create_dir("tracing", NULL);
4046
4047 if (!d_tracer && !once) {
4048 once = 1;
4049 pr_warning("Could not create debugfs directory 'tracing'\n");
4050 return NULL;
4051 }
4052
4053 return d_tracer;
4054}
4055
b04cc6b1
FW
4056static struct dentry *d_percpu;
4057
4058struct dentry *tracing_dentry_percpu(void)
4059{
4060 static int once;
4061 struct dentry *d_tracer;
4062
4063 if (d_percpu)
4064 return d_percpu;
4065
4066 d_tracer = tracing_init_dentry();
4067
4068 if (!d_tracer)
4069 return NULL;
4070
4071 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4072
4073 if (!d_percpu && !once) {
4074 once = 1;
4075 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4076 return NULL;
4077 }
4078
4079 return d_percpu;
4080}
4081
4082static void tracing_init_debugfs_percpu(long cpu)
4083{
4084 struct dentry *d_percpu = tracing_dentry_percpu();
5452af66 4085 struct dentry *d_cpu;
dd49a38c 4086 char cpu_dir[30]; /* 30 characters should be more than enough */
b04cc6b1 4087
dd49a38c 4088 snprintf(cpu_dir, 30, "cpu%ld", cpu);
8656e7a2
FW
4089 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4090 if (!d_cpu) {
4091 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4092 return;
4093 }
b04cc6b1 4094
8656e7a2 4095 /* per cpu trace_pipe */
5452af66
FW
4096 trace_create_file("trace_pipe", 0444, d_cpu,
4097 (void *) cpu, &tracing_pipe_fops);
b04cc6b1
FW
4098
4099 /* per cpu trace */
5452af66
FW
4100 trace_create_file("trace", 0644, d_cpu,
4101 (void *) cpu, &tracing_fops);
7f96f93f 4102
5452af66
FW
4103 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4104 (void *) cpu, &tracing_buffers_fops);
7f96f93f 4105
c8d77183
SR
4106 trace_create_file("stats", 0444, d_cpu,
4107 (void *) cpu, &tracing_stats_fops);
b04cc6b1
FW
4108}
4109
60a11774
SR
4110#ifdef CONFIG_FTRACE_SELFTEST
4111/* Let selftest have access to static functions in this file */
4112#include "trace_selftest.c"
4113#endif
4114
577b785f
SR
4115struct trace_option_dentry {
4116 struct tracer_opt *opt;
4117 struct tracer_flags *flags;
4118 struct dentry *entry;
4119};
4120
4121static ssize_t
4122trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4123 loff_t *ppos)
4124{
4125 struct trace_option_dentry *topt = filp->private_data;
4126 char *buf;
4127
4128 if (topt->flags->val & topt->opt->bit)
4129 buf = "1\n";
4130 else
4131 buf = "0\n";
4132
4133 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4134}
4135
4136static ssize_t
4137trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4138 loff_t *ppos)
4139{
4140 struct trace_option_dentry *topt = filp->private_data;
4141 unsigned long val;
4142 char buf[64];
4143 int ret;
4144
4145 if (cnt >= sizeof(buf))
4146 return -EINVAL;
4147
4148 if (copy_from_user(&buf, ubuf, cnt))
4149 return -EFAULT;
4150
4151 buf[cnt] = 0;
4152
4153 ret = strict_strtoul(buf, 10, &val);
4154 if (ret < 0)
4155 return ret;
4156
8d18eaaf
LZ
4157 if (val != 0 && val != 1)
4158 return -EINVAL;
577b785f 4159
8d18eaaf 4160 if (!!(topt->flags->val & topt->opt->bit) != val) {
577b785f 4161 mutex_lock(&trace_types_lock);
8d18eaaf 4162 ret = __set_tracer_option(current_trace, topt->flags,
c757bea9 4163 topt->opt, !val);
577b785f
SR
4164 mutex_unlock(&trace_types_lock);
4165 if (ret)
4166 return ret;
577b785f
SR
4167 }
4168
4169 *ppos += cnt;
4170
4171 return cnt;
4172}
4173
4174
4175static const struct file_operations trace_options_fops = {
4176 .open = tracing_open_generic,
4177 .read = trace_options_read,
4178 .write = trace_options_write,
b444786f 4179 .llseek = generic_file_llseek,
577b785f
SR
4180};
4181
a8259075
SR
4182static ssize_t
4183trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4184 loff_t *ppos)
4185{
4186 long index = (long)filp->private_data;
4187 char *buf;
4188
4189 if (trace_flags & (1 << index))
4190 buf = "1\n";
4191 else
4192 buf = "0\n";
4193
4194 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4195}
4196
4197static ssize_t
4198trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4199 loff_t *ppos)
4200{
4201 long index = (long)filp->private_data;
4202 char buf[64];
4203 unsigned long val;
4204 int ret;
4205
4206 if (cnt >= sizeof(buf))
4207 return -EINVAL;
4208
4209 if (copy_from_user(&buf, ubuf, cnt))
4210 return -EFAULT;
4211
4212 buf[cnt] = 0;
4213
4214 ret = strict_strtoul(buf, 10, &val);
4215 if (ret < 0)
4216 return ret;
4217
f2d84b65 4218 if (val != 0 && val != 1)
a8259075 4219 return -EINVAL;
f2d84b65 4220 set_tracer_flags(1 << index, val);
a8259075
SR
4221
4222 *ppos += cnt;
4223
4224 return cnt;
4225}
4226
a8259075
SR
4227static const struct file_operations trace_options_core_fops = {
4228 .open = tracing_open_generic,
4229 .read = trace_options_core_read,
4230 .write = trace_options_core_write,
b444786f 4231 .llseek = generic_file_llseek,
a8259075
SR
4232};
4233
5452af66
FW
4234struct dentry *trace_create_file(const char *name,
4235 mode_t mode,
4236 struct dentry *parent,
4237 void *data,
4238 const struct file_operations *fops)
4239{
4240 struct dentry *ret;
4241
4242 ret = debugfs_create_file(name, mode, parent, data, fops);
4243 if (!ret)
4244 pr_warning("Could not create debugfs '%s' entry\n", name);
4245
4246 return ret;
4247}
4248
4249
a8259075
SR
4250static struct dentry *trace_options_init_dentry(void)
4251{
4252 struct dentry *d_tracer;
4253 static struct dentry *t_options;
4254
4255 if (t_options)
4256 return t_options;
4257
4258 d_tracer = tracing_init_dentry();
4259 if (!d_tracer)
4260 return NULL;
4261
4262 t_options = debugfs_create_dir("options", d_tracer);
4263 if (!t_options) {
4264 pr_warning("Could not create debugfs directory 'options'\n");
4265 return NULL;
4266 }
4267
4268 return t_options;
4269}
4270
577b785f
SR
4271static void
4272create_trace_option_file(struct trace_option_dentry *topt,
4273 struct tracer_flags *flags,
4274 struct tracer_opt *opt)
4275{
4276 struct dentry *t_options;
577b785f
SR
4277
4278 t_options = trace_options_init_dentry();
4279 if (!t_options)
4280 return;
4281
4282 topt->flags = flags;
4283 topt->opt = opt;
4284
5452af66 4285 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
577b785f
SR
4286 &trace_options_fops);
4287
577b785f
SR
4288}
4289
4290static struct trace_option_dentry *
4291create_trace_option_files(struct tracer *tracer)
4292{
4293 struct trace_option_dentry *topts;
4294 struct tracer_flags *flags;
4295 struct tracer_opt *opts;
4296 int cnt;
4297
4298 if (!tracer)
4299 return NULL;
4300
4301 flags = tracer->flags;
4302
4303 if (!flags || !flags->opts)
4304 return NULL;
4305
4306 opts = flags->opts;
4307
4308 for (cnt = 0; opts[cnt].name; cnt++)
4309 ;
4310
0cfe8245 4311 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
577b785f
SR
4312 if (!topts)
4313 return NULL;
4314
4315 for (cnt = 0; opts[cnt].name; cnt++)
4316 create_trace_option_file(&topts[cnt], flags,
4317 &opts[cnt]);
4318
4319 return topts;
4320}
4321
4322static void
4323destroy_trace_option_files(struct trace_option_dentry *topts)
4324{
4325 int cnt;
4326
4327 if (!topts)
4328 return;
4329
4330 for (cnt = 0; topts[cnt].opt; cnt++) {
4331 if (topts[cnt].entry)
4332 debugfs_remove(topts[cnt].entry);
4333 }
4334
4335 kfree(topts);
4336}
4337
a8259075
SR
4338static struct dentry *
4339create_trace_option_core_file(const char *option, long index)
4340{
4341 struct dentry *t_options;
a8259075
SR
4342
4343 t_options = trace_options_init_dentry();
4344 if (!t_options)
4345 return NULL;
4346
5452af66 4347 return trace_create_file(option, 0644, t_options, (void *)index,
a8259075 4348 &trace_options_core_fops);
a8259075
SR
4349}
4350
4351static __init void create_trace_options_dir(void)
4352{
4353 struct dentry *t_options;
a8259075
SR
4354 int i;
4355
4356 t_options = trace_options_init_dentry();
4357 if (!t_options)
4358 return;
4359
5452af66
FW
4360 for (i = 0; trace_options[i]; i++)
4361 create_trace_option_core_file(trace_options[i], i);
a8259075
SR
4362}
4363
b5ad384e 4364static __init int tracer_init_debugfs(void)
bc0c38d1
SR
4365{
4366 struct dentry *d_tracer;
b04cc6b1 4367 int cpu;
bc0c38d1 4368
7e53bd42
LJ
4369 trace_access_lock_init();
4370
bc0c38d1
SR
4371 d_tracer = tracing_init_dentry();
4372
5452af66
FW
4373 trace_create_file("tracing_enabled", 0644, d_tracer,
4374 &global_trace, &tracing_ctrl_fops);
bc0c38d1 4375
5452af66
FW
4376 trace_create_file("trace_options", 0644, d_tracer,
4377 NULL, &tracing_iter_fops);
bc0c38d1 4378
5452af66
FW
4379 trace_create_file("tracing_cpumask", 0644, d_tracer,
4380 NULL, &tracing_cpumask_fops);
4381
4382 trace_create_file("trace", 0644, d_tracer,
4383 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
a8259075 4384
5452af66
FW
4385 trace_create_file("available_tracers", 0444, d_tracer,
4386 &global_trace, &show_traces_fops);
4387
339ae5d3 4388 trace_create_file("current_tracer", 0644, d_tracer,
5452af66
FW
4389 &global_trace, &set_tracer_fops);
4390
5d4a9dba 4391#ifdef CONFIG_TRACER_MAX_TRACE
5452af66
FW
4392 trace_create_file("tracing_max_latency", 0644, d_tracer,
4393 &tracing_max_latency, &tracing_max_lat_fops);
0e950173 4394#endif
5452af66
FW
4395
4396 trace_create_file("tracing_thresh", 0644, d_tracer,
4397 &tracing_thresh, &tracing_max_lat_fops);
a8259075 4398
339ae5d3 4399 trace_create_file("README", 0444, d_tracer,
5452af66
FW
4400 NULL, &tracing_readme_fops);
4401
4402 trace_create_file("trace_pipe", 0444, d_tracer,
b04cc6b1 4403 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
5452af66
FW
4404
4405 trace_create_file("buffer_size_kb", 0644, d_tracer,
4406 &global_trace, &tracing_entries_fops);
4407
4f271a2a
VN
4408 trace_create_file("free_buffer", 0644, d_tracer,
4409 &global_trace, &tracing_free_buffer_fops);
4410
5452af66
FW
4411 trace_create_file("trace_marker", 0220, d_tracer,
4412 NULL, &tracing_mark_fops);
5bf9a1ee 4413
69abe6a5
AP
4414 trace_create_file("saved_cmdlines", 0444, d_tracer,
4415 NULL, &tracing_saved_cmdlines_fops);
5bf9a1ee 4416
5079f326
Z
4417 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4418 &trace_clock_fops);
4419
bc0c38d1 4420#ifdef CONFIG_DYNAMIC_FTRACE
5452af66
FW
4421 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4422 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
bc0c38d1 4423#endif
b04cc6b1 4424
5452af66
FW
4425 create_trace_options_dir();
4426
b04cc6b1
FW
4427 for_each_tracing_cpu(cpu)
4428 tracing_init_debugfs_percpu(cpu);
4429
b5ad384e 4430 return 0;
bc0c38d1
SR
4431}
4432
3f5a54e3
SR
4433static int trace_panic_handler(struct notifier_block *this,
4434 unsigned long event, void *unused)
4435{
944ac425 4436 if (ftrace_dump_on_oops)
cecbca96 4437 ftrace_dump(ftrace_dump_on_oops);
3f5a54e3
SR
4438 return NOTIFY_OK;
4439}
4440
4441static struct notifier_block trace_panic_notifier = {
4442 .notifier_call = trace_panic_handler,
4443 .next = NULL,
4444 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4445};
4446
4447static int trace_die_handler(struct notifier_block *self,
4448 unsigned long val,
4449 void *data)
4450{
4451 switch (val) {
4452 case DIE_OOPS:
944ac425 4453 if (ftrace_dump_on_oops)
cecbca96 4454 ftrace_dump(ftrace_dump_on_oops);
3f5a54e3
SR
4455 break;
4456 default:
4457 break;
4458 }
4459 return NOTIFY_OK;
4460}
4461
4462static struct notifier_block trace_die_notifier = {
4463 .notifier_call = trace_die_handler,
4464 .priority = 200
4465};
4466
4467/*
4468 * printk is set to max of 1024, we really don't need it that big.
4469 * Nothing should be printing 1000 characters anyway.
4470 */
4471#define TRACE_MAX_PRINT 1000
4472
4473/*
4474 * Define here KERN_TRACE so that we have one place to modify
4475 * it if we decide to change what log level the ftrace dump
4476 * should be at.
4477 */
428aee14 4478#define KERN_TRACE KERN_EMERG
3f5a54e3 4479
955b61e5 4480void
3f5a54e3
SR
4481trace_printk_seq(struct trace_seq *s)
4482{
4483 /* Probably should print a warning here. */
4484 if (s->len >= 1000)
4485 s->len = 1000;
4486
4487 /* should be zero ended, but we are paranoid. */
4488 s->buffer[s->len] = 0;
4489
4490 printk(KERN_TRACE "%s", s->buffer);
4491
f9520750 4492 trace_seq_init(s);
3f5a54e3
SR
4493}
4494
955b61e5
JW
4495void trace_init_global_iter(struct trace_iterator *iter)
4496{
4497 iter->tr = &global_trace;
4498 iter->trace = current_trace;
4499 iter->cpu_file = TRACE_PIPE_ALL_CPU;
4500}
4501
cecbca96
FW
4502static void
4503__ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
3f5a54e3 4504{
445c8951 4505 static arch_spinlock_t ftrace_dump_lock =
edc35bd7 4506 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
3f5a54e3
SR
4507 /* use static because iter can be a bit big for the stack */
4508 static struct trace_iterator iter;
cf586b61 4509 unsigned int old_userobj;
3f5a54e3 4510 static int dump_ran;
d769041f
SR
4511 unsigned long flags;
4512 int cnt = 0, cpu;
3f5a54e3
SR
4513
4514 /* only one dump */
cd891ae0 4515 local_irq_save(flags);
0199c4e6 4516 arch_spin_lock(&ftrace_dump_lock);
3f5a54e3
SR
4517 if (dump_ran)
4518 goto out;
4519
4520 dump_ran = 1;
4521
0ee6b6cf 4522 tracing_off();
cf586b61
FW
4523
4524 if (disable_tracing)
4525 ftrace_kill();
3f5a54e3 4526
955b61e5
JW
4527 trace_init_global_iter(&iter);
4528
d769041f 4529 for_each_tracing_cpu(cpu) {
955b61e5 4530 atomic_inc(&iter.tr->data[cpu]->disabled);
d769041f
SR
4531 }
4532
cf586b61
FW
4533 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4534
b54d3de9
TE
4535 /* don't look at user memory in panic mode */
4536 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4537
e543ad76 4538 /* Simulate the iterator */
3f5a54e3
SR
4539 iter.tr = &global_trace;
4540 iter.trace = current_trace;
cecbca96
FW
4541
4542 switch (oops_dump_mode) {
4543 case DUMP_ALL:
4544 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4545 break;
4546 case DUMP_ORIG:
4547 iter.cpu_file = raw_smp_processor_id();
4548 break;
4549 case DUMP_NONE:
4550 goto out_enable;
4551 default:
4552 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4553 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4554 }
4555
4556 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3f5a54e3
SR
4557
4558 /*
4559 * We need to stop all tracing on all CPUS to read the
4560 * the next buffer. This is a bit expensive, but is
4561 * not done often. We fill all what we can read,
4562 * and then release the locks again.
4563 */
4564
3f5a54e3
SR
4565 while (!trace_empty(&iter)) {
4566
4567 if (!cnt)
4568 printk(KERN_TRACE "---------------------------------\n");
4569
4570 cnt++;
4571
4572 /* reset all but tr, trace, and overruns */
4573 memset(&iter.seq, 0,
4574 sizeof(struct trace_iterator) -
4575 offsetof(struct trace_iterator, seq));
4576 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4577 iter.pos = -1;
4578
955b61e5 4579 if (trace_find_next_entry_inc(&iter) != NULL) {
74e7ff8c
LJ
4580 int ret;
4581
4582 ret = print_trace_line(&iter);
4583 if (ret != TRACE_TYPE_NO_CONSUME)
4584 trace_consume(&iter);
3f5a54e3
SR
4585 }
4586
4587 trace_printk_seq(&iter.seq);
4588 }
4589
4590 if (!cnt)
4591 printk(KERN_TRACE " (ftrace buffer empty)\n");
4592 else
4593 printk(KERN_TRACE "---------------------------------\n");
4594
cecbca96 4595 out_enable:
cf586b61
FW
4596 /* Re-enable tracing if requested */
4597 if (!disable_tracing) {
4598 trace_flags |= old_userobj;
4599
4600 for_each_tracing_cpu(cpu) {
955b61e5 4601 atomic_dec(&iter.tr->data[cpu]->disabled);
cf586b61
FW
4602 }
4603 tracing_on();
4604 }
4605
3f5a54e3 4606 out:
0199c4e6 4607 arch_spin_unlock(&ftrace_dump_lock);
cd891ae0 4608 local_irq_restore(flags);
3f5a54e3
SR
4609}
4610
cf586b61 4611/* By default: disable tracing after the dump */
cecbca96 4612void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
cf586b61 4613{
cecbca96 4614 __ftrace_dump(true, oops_dump_mode);
cf586b61
FW
4615}
4616
3928a8a2 4617__init static int tracer_alloc_buffers(void)
bc0c38d1 4618{
73c5162a 4619 int ring_buf_size;
750912fa 4620 enum ring_buffer_flags rb_flags;
4c11d7ae 4621 int i;
9e01c1b7 4622 int ret = -ENOMEM;
4c11d7ae 4623
750912fa 4624
9e01c1b7
RR
4625 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4626 goto out;
4627
4628 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4629 goto out_free_buffer_mask;
4c11d7ae 4630
73c5162a
SR
4631 /* To save memory, keep the ring buffer size to its minimum */
4632 if (ring_buffer_expanded)
4633 ring_buf_size = trace_buf_size;
4634 else
4635 ring_buf_size = 1;
4636
750912fa
DS
4637 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
4638
9e01c1b7
RR
4639 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4640 cpumask_copy(tracing_cpumask, cpu_all_mask);
4641
4642 /* TODO: make the number of buffers hot pluggable with CPUS */
750912fa 4643 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
3928a8a2
SR
4644 if (!global_trace.buffer) {
4645 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4646 WARN_ON(1);
9e01c1b7 4647 goto out_free_cpumask;
4c11d7ae 4648 }
3928a8a2 4649 global_trace.entries = ring_buffer_size(global_trace.buffer);
4c11d7ae 4650
9e01c1b7 4651
4c11d7ae 4652#ifdef CONFIG_TRACER_MAX_TRACE
750912fa 4653 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
3928a8a2
SR
4654 if (!max_tr.buffer) {
4655 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4656 WARN_ON(1);
4657 ring_buffer_free(global_trace.buffer);
9e01c1b7 4658 goto out_free_cpumask;
4c11d7ae 4659 }
ef710e10 4660 max_tr.entries = 1;
a98a3c3f 4661#endif
ab46428c 4662
4c11d7ae 4663 /* Allocate the first page for all buffers */
ab46428c 4664 for_each_tracing_cpu(i) {
566b0aaf 4665 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
9705f69e 4666 max_tr.data[i] = &per_cpu(max_tr_data, i);
4c11d7ae 4667 }
bc0c38d1 4668
bc0c38d1
SR
4669 trace_init_cmdlines();
4670
43a15386 4671 register_tracer(&nop_trace);
79fb0768 4672 current_trace = &nop_trace;
60a11774
SR
4673 /* All seems OK, enable tracing */
4674 tracing_disabled = 0;
3928a8a2 4675
3f5a54e3
SR
4676 atomic_notifier_chain_register(&panic_notifier_list,
4677 &trace_panic_notifier);
4678
4679 register_die_notifier(&trace_die_notifier);
2fc1dfbe
FW
4680
4681 return 0;
3f5a54e3 4682
9e01c1b7
RR
4683out_free_cpumask:
4684 free_cpumask_var(tracing_cpumask);
4685out_free_buffer_mask:
4686 free_cpumask_var(tracing_buffer_mask);
4687out:
4688 return ret;
bc0c38d1 4689}
b2821ae6
SR
4690
4691__init static int clear_boot_tracer(void)
4692{
4693 /*
4694 * The default tracer at boot buffer is an init section.
4695 * This function is called in lateinit. If we did not
4696 * find the boot tracer, then clear it out, to prevent
4697 * later registration from accessing the buffer that is
4698 * about to be freed.
4699 */
4700 if (!default_bootup_tracer)
4701 return 0;
4702
4703 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4704 default_bootup_tracer);
4705 default_bootup_tracer = NULL;
4706
4707 return 0;
4708}
4709
b5ad384e
FW
4710early_initcall(tracer_alloc_buffers);
4711fs_initcall(tracer_init_debugfs);
b2821ae6 4712late_initcall(clear_boot_tracer);