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