]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/trace/trace.c
tracing/ftrace: fix the check on nopped sites
[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/**
48ead020 1182 * trace_vbprintk - write binary msg to tracing buffer
769b0441
FW
1183 *
1184 */
48ead020 1185int trace_vbprintk(unsigned long ip, int depth, const char *fmt, va_list args)
769b0441 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;
48ead020 1194 struct bprint_entry *entry;
769b0441
FW
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;
48ead020 1222 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
769b0441
FW
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}
48ead020
FW
1243EXPORT_SYMBOL_GPL(trace_vbprintk);
1244
1245int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1246{
1247 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1248 static char trace_buf[TRACE_BUF_SIZE];
1249
1250 struct ring_buffer_event *event;
1251 struct trace_array *tr = &global_trace;
1252 struct trace_array_cpu *data;
1253 int cpu, len = 0, size, pc;
1254 struct print_entry *entry;
1255 unsigned long irq_flags;
1256
1257 if (tracing_disabled || tracing_selftest_running)
1258 return 0;
1259
1260 pc = preempt_count();
1261 preempt_disable_notrace();
1262 cpu = raw_smp_processor_id();
1263 data = tr->data[cpu];
1264
1265 if (unlikely(atomic_read(&data->disabled)))
1266 goto out;
1267
1268 pause_graph_tracing();
1269 raw_local_irq_save(irq_flags);
1270 __raw_spin_lock(&trace_buf_lock);
1271 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1272
1273 len = min(len, TRACE_BUF_SIZE-1);
1274 trace_buf[len] = 0;
1275
1276 size = sizeof(*entry) + len + 1;
1277 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1278 if (!event)
1279 goto out_unlock;
1280 entry = ring_buffer_event_data(event);
1281 entry->ip = ip;
1282 entry->depth = depth;
1283
1284 memcpy(&entry->buf, trace_buf, len);
1285 entry->buf[len] = 0;
1286 ring_buffer_unlock_commit(tr->buffer, event);
1287
1288 out_unlock:
1289 __raw_spin_unlock(&trace_buf_lock);
1290 raw_local_irq_restore(irq_flags);
1291 unpause_graph_tracing();
1292 out:
1293 preempt_enable_notrace();
1294
1295 return len;
1296}
769b0441
FW
1297EXPORT_SYMBOL_GPL(trace_vprintk);
1298
bc0c38d1
SR
1299enum trace_file_type {
1300 TRACE_FILE_LAT_FMT = 1,
12ef7d44 1301 TRACE_FILE_ANNOTATE = 2,
bc0c38d1
SR
1302};
1303
e2ac8ef5 1304static void trace_iterator_increment(struct trace_iterator *iter)
5a90f577 1305{
d769041f
SR
1306 /* Don't allow ftrace to trace into the ring buffers */
1307 ftrace_disable_cpu();
1308
5a90f577 1309 iter->idx++;
d769041f
SR
1310 if (iter->buffer_iter[iter->cpu])
1311 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1312
1313 ftrace_enable_cpu();
5a90f577
SR
1314}
1315
e309b41d 1316static struct trace_entry *
3928a8a2 1317peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
dd0e545f 1318{
3928a8a2
SR
1319 struct ring_buffer_event *event;
1320 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
dd0e545f 1321
d769041f
SR
1322 /* Don't allow ftrace to trace into the ring buffers */
1323 ftrace_disable_cpu();
1324
1325 if (buf_iter)
1326 event = ring_buffer_iter_peek(buf_iter, ts);
1327 else
1328 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1329
1330 ftrace_enable_cpu();
1331
3928a8a2 1332 return event ? ring_buffer_event_data(event) : NULL;
dd0e545f 1333}
d769041f 1334
dd0e545f 1335static struct trace_entry *
3928a8a2 1336__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
bc0c38d1 1337{
3928a8a2 1338 struct ring_buffer *buffer = iter->tr->buffer;
bc0c38d1 1339 struct trace_entry *ent, *next = NULL;
b04cc6b1 1340 int cpu_file = iter->cpu_file;
3928a8a2 1341 u64 next_ts = 0, ts;
bc0c38d1
SR
1342 int next_cpu = -1;
1343 int cpu;
1344
b04cc6b1
FW
1345 /*
1346 * If we are in a per_cpu trace file, don't bother by iterating over
1347 * all cpu and peek directly.
1348 */
1349 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1350 if (ring_buffer_empty_cpu(buffer, cpu_file))
1351 return NULL;
1352 ent = peek_next_entry(iter, cpu_file, ent_ts);
1353 if (ent_cpu)
1354 *ent_cpu = cpu_file;
1355
1356 return ent;
1357 }
1358
ab46428c 1359 for_each_tracing_cpu(cpu) {
dd0e545f 1360
3928a8a2
SR
1361 if (ring_buffer_empty_cpu(buffer, cpu))
1362 continue;
dd0e545f 1363
3928a8a2 1364 ent = peek_next_entry(iter, cpu, &ts);
dd0e545f 1365
cdd31cd2
IM
1366 /*
1367 * Pick the entry with the smallest timestamp:
1368 */
3928a8a2 1369 if (ent && (!next || ts < next_ts)) {
bc0c38d1
SR
1370 next = ent;
1371 next_cpu = cpu;
3928a8a2 1372 next_ts = ts;
bc0c38d1
SR
1373 }
1374 }
1375
1376 if (ent_cpu)
1377 *ent_cpu = next_cpu;
1378
3928a8a2
SR
1379 if (ent_ts)
1380 *ent_ts = next_ts;
1381
bc0c38d1
SR
1382 return next;
1383}
1384
dd0e545f 1385/* Find the next real entry, without updating the iterator itself */
c4a8e8be
FW
1386struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1387 int *ent_cpu, u64 *ent_ts)
bc0c38d1 1388{
3928a8a2 1389 return __find_next_entry(iter, ent_cpu, ent_ts);
dd0e545f
SR
1390}
1391
1392/* Find the next real entry, and increment the iterator to the next entry */
1393static void *find_next_entry_inc(struct trace_iterator *iter)
1394{
3928a8a2 1395 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
dd0e545f 1396
3928a8a2 1397 if (iter->ent)
e2ac8ef5 1398 trace_iterator_increment(iter);
dd0e545f 1399
3928a8a2 1400 return iter->ent ? iter : NULL;
b3806b43 1401}
bc0c38d1 1402
e309b41d 1403static void trace_consume(struct trace_iterator *iter)
b3806b43 1404{
d769041f
SR
1405 /* Don't allow ftrace to trace into the ring buffers */
1406 ftrace_disable_cpu();
3928a8a2 1407 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
d769041f 1408 ftrace_enable_cpu();
bc0c38d1
SR
1409}
1410
e309b41d 1411static void *s_next(struct seq_file *m, void *v, loff_t *pos)
bc0c38d1
SR
1412{
1413 struct trace_iterator *iter = m->private;
bc0c38d1 1414 int i = (int)*pos;
4e3c3333 1415 void *ent;
bc0c38d1
SR
1416
1417 (*pos)++;
1418
1419 /* can't go backwards */
1420 if (iter->idx > i)
1421 return NULL;
1422
1423 if (iter->idx < 0)
1424 ent = find_next_entry_inc(iter);
1425 else
1426 ent = iter;
1427
1428 while (ent && iter->idx < i)
1429 ent = find_next_entry_inc(iter);
1430
1431 iter->pos = *pos;
1432
bc0c38d1
SR
1433 return ent;
1434}
1435
d7350c3f
FW
1436/*
1437 * No necessary locking here. The worst thing which can
1438 * happen is loosing events consumed at the same time
1439 * by a trace_pipe reader.
1440 * Other than that, we don't risk to crash the ring buffer
1441 * because it serializes the readers.
1442 *
1443 * The current tracer is copied to avoid a global locking
1444 * all around.
1445 */
bc0c38d1
SR
1446static void *s_start(struct seq_file *m, loff_t *pos)
1447{
1448 struct trace_iterator *iter = m->private;
d7350c3f 1449 static struct tracer *old_tracer;
b04cc6b1 1450 int cpu_file = iter->cpu_file;
bc0c38d1
SR
1451 void *p = NULL;
1452 loff_t l = 0;
3928a8a2 1453 int cpu;
bc0c38d1 1454
d7350c3f 1455 /* copy the tracer to avoid using a global lock all around */
bc0c38d1 1456 mutex_lock(&trace_types_lock);
d7350c3f
FW
1457 if (unlikely(old_tracer != current_trace && current_trace)) {
1458 old_tracer = current_trace;
1459 *iter->trace = *current_trace;
d15f57f2 1460 }
d7350c3f 1461 mutex_unlock(&trace_types_lock);
bc0c38d1
SR
1462
1463 atomic_inc(&trace_record_cmdline_disabled);
1464
bc0c38d1
SR
1465 if (*pos != iter->pos) {
1466 iter->ent = NULL;
1467 iter->cpu = 0;
1468 iter->idx = -1;
1469
d769041f
SR
1470 ftrace_disable_cpu();
1471
b04cc6b1
FW
1472 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1473 for_each_tracing_cpu(cpu)
1474 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1475 } else
1476 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1477
bc0c38d1 1478
d769041f
SR
1479 ftrace_enable_cpu();
1480
bc0c38d1
SR
1481 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1482 ;
1483
1484 } else {
4c11d7ae 1485 l = *pos - 1;
bc0c38d1
SR
1486 p = s_next(m, p, &l);
1487 }
1488
1489 return p;
1490}
1491
1492static void s_stop(struct seq_file *m, void *p)
1493{
bc0c38d1 1494 atomic_dec(&trace_record_cmdline_disabled);
bc0c38d1
SR
1495}
1496
e309b41d 1497static void print_lat_help_header(struct seq_file *m)
bc0c38d1 1498{
a6168353
ME
1499 seq_puts(m, "# _------=> CPU# \n");
1500 seq_puts(m, "# / _-----=> irqs-off \n");
1501 seq_puts(m, "# | / _----=> need-resched \n");
1502 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1503 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1504 seq_puts(m, "# |||| / \n");
1505 seq_puts(m, "# ||||| delay \n");
1506 seq_puts(m, "# cmd pid ||||| time | caller \n");
1507 seq_puts(m, "# \\ / ||||| \\ | / \n");
bc0c38d1
SR
1508}
1509
e309b41d 1510static void print_func_help_header(struct seq_file *m)
bc0c38d1 1511{
a6168353
ME
1512 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1513 seq_puts(m, "# | | | | |\n");
bc0c38d1
SR
1514}
1515
1516
e309b41d 1517static void
bc0c38d1
SR
1518print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1519{
1520 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1521 struct trace_array *tr = iter->tr;
1522 struct trace_array_cpu *data = tr->data[tr->cpu];
1523 struct tracer *type = current_trace;
3928a8a2
SR
1524 unsigned long total;
1525 unsigned long entries;
bc0c38d1
SR
1526 const char *name = "preemption";
1527
1528 if (type)
1529 name = type->name;
1530
3928a8a2
SR
1531 entries = ring_buffer_entries(iter->tr->buffer);
1532 total = entries +
1533 ring_buffer_overruns(iter->tr->buffer);
bc0c38d1 1534
888b55dc 1535 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
bc0c38d1 1536 name, UTS_RELEASE);
888b55dc 1537 seq_puts(m, "# -----------------------------------"
bc0c38d1 1538 "---------------------------------\n");
888b55dc 1539 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
bc0c38d1 1540 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
57f50be1 1541 nsecs_to_usecs(data->saved_latency),
bc0c38d1 1542 entries,
4c11d7ae 1543 total,
bc0c38d1
SR
1544 tr->cpu,
1545#if defined(CONFIG_PREEMPT_NONE)
1546 "server",
1547#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1548 "desktop",
b5c21b45 1549#elif defined(CONFIG_PREEMPT)
bc0c38d1
SR
1550 "preempt",
1551#else
1552 "unknown",
1553#endif
1554 /* These are reserved for later use */
1555 0, 0, 0, 0);
1556#ifdef CONFIG_SMP
1557 seq_printf(m, " #P:%d)\n", num_online_cpus());
1558#else
1559 seq_puts(m, ")\n");
1560#endif
888b55dc
KM
1561 seq_puts(m, "# -----------------\n");
1562 seq_printf(m, "# | task: %.16s-%d "
bc0c38d1
SR
1563 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1564 data->comm, data->pid, data->uid, data->nice,
1565 data->policy, data->rt_priority);
888b55dc 1566 seq_puts(m, "# -----------------\n");
bc0c38d1
SR
1567
1568 if (data->critical_start) {
888b55dc 1569 seq_puts(m, "# => started at: ");
214023c3
SR
1570 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1571 trace_print_seq(m, &iter->seq);
888b55dc 1572 seq_puts(m, "\n# => ended at: ");
214023c3
SR
1573 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1574 trace_print_seq(m, &iter->seq);
888b55dc 1575 seq_puts(m, "#\n");
bc0c38d1
SR
1576 }
1577
888b55dc 1578 seq_puts(m, "#\n");
bc0c38d1
SR
1579}
1580
a309720c
SR
1581static void test_cpu_buff_start(struct trace_iterator *iter)
1582{
1583 struct trace_seq *s = &iter->seq;
1584
12ef7d44
SR
1585 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1586 return;
1587
1588 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1589 return;
1590
4462344e 1591 if (cpumask_test_cpu(iter->cpu, iter->started))
a309720c
SR
1592 return;
1593
4462344e 1594 cpumask_set_cpu(iter->cpu, iter->started);
a309720c
SR
1595 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1596}
1597
2c4f035f 1598static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 1599{
214023c3 1600 struct trace_seq *s = &iter->seq;
bc0c38d1 1601 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 1602 struct trace_entry *entry;
f633cef0 1603 struct trace_event *event;
bc0c38d1 1604
4e3c3333 1605 entry = iter->ent;
dd0e545f 1606
a309720c
SR
1607 test_cpu_buff_start(iter);
1608
c4a8e8be 1609 event = ftrace_find_event(entry->type);
bc0c38d1 1610
c4a8e8be 1611 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
27d48be8
SR
1612 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1613 if (!trace_print_lat_context(iter))
1614 goto partial;
1615 } else {
1616 if (!trace_print_context(iter))
1617 goto partial;
1618 }
c4a8e8be 1619 }
bc0c38d1 1620
268ccda0 1621 if (event)
d9793bd8
ACM
1622 return event->trace(iter, sym_flags);
1623
1624 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1625 goto partial;
02b67518 1626
2c4f035f 1627 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
1628partial:
1629 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1
SR
1630}
1631
2c4f035f 1632static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
f9896bf3
IM
1633{
1634 struct trace_seq *s = &iter->seq;
1635 struct trace_entry *entry;
f633cef0 1636 struct trace_event *event;
f9896bf3
IM
1637
1638 entry = iter->ent;
dd0e545f 1639
c4a8e8be 1640 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
d9793bd8
ACM
1641 if (!trace_seq_printf(s, "%d %d %llu ",
1642 entry->pid, iter->cpu, iter->ts))
1643 goto partial;
c4a8e8be 1644 }
f9896bf3 1645
f633cef0 1646 event = ftrace_find_event(entry->type);
268ccda0 1647 if (event)
d9793bd8
ACM
1648 return event->raw(iter, 0);
1649
1650 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1651 goto partial;
777e208d 1652
2c4f035f 1653 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
1654partial:
1655 return TRACE_TYPE_PARTIAL_LINE;
f9896bf3
IM
1656}
1657
2c4f035f 1658static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
5e3ca0ec
IM
1659{
1660 struct trace_seq *s = &iter->seq;
1661 unsigned char newline = '\n';
1662 struct trace_entry *entry;
f633cef0 1663 struct trace_event *event;
5e3ca0ec
IM
1664
1665 entry = iter->ent;
dd0e545f 1666
c4a8e8be
FW
1667 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1668 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1669 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1670 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1671 }
5e3ca0ec 1672
f633cef0 1673 event = ftrace_find_event(entry->type);
268ccda0 1674 if (event) {
ae7462b4 1675 enum print_line_t ret = event->hex(iter, 0);
d9793bd8
ACM
1676 if (ret != TRACE_TYPE_HANDLED)
1677 return ret;
1678 }
7104f300 1679
5e3ca0ec
IM
1680 SEQ_PUT_FIELD_RET(s, newline);
1681
2c4f035f 1682 return TRACE_TYPE_HANDLED;
5e3ca0ec
IM
1683}
1684
48ead020
FW
1685static enum print_line_t print_bprintk_msg_only(struct trace_iterator *iter)
1686{
1687 struct trace_seq *s = &iter->seq;
1688 struct trace_entry *entry = iter->ent;
1689 struct bprint_entry *field;
1690 int ret;
1691
1692 trace_assign_type(field, entry);
1693
1694 ret = trace_seq_bprintf(s, field->fmt, field->buf);
1695 if (!ret)
1696 return TRACE_TYPE_PARTIAL_LINE;
1697
1698 return TRACE_TYPE_HANDLED;
1699}
1700
66896a85
FW
1701static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1702{
1703 struct trace_seq *s = &iter->seq;
1704 struct trace_entry *entry = iter->ent;
1705 struct print_entry *field;
1706 int ret;
1707
1708 trace_assign_type(field, entry);
1709
48ead020 1710 ret = trace_seq_printf(s, "%s", field->buf);
66896a85
FW
1711 if (!ret)
1712 return TRACE_TYPE_PARTIAL_LINE;
1713
66896a85
FW
1714 return TRACE_TYPE_HANDLED;
1715}
1716
2c4f035f 1717static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
cb0f12aa
IM
1718{
1719 struct trace_seq *s = &iter->seq;
1720 struct trace_entry *entry;
f633cef0 1721 struct trace_event *event;
cb0f12aa
IM
1722
1723 entry = iter->ent;
dd0e545f 1724
c4a8e8be
FW
1725 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1726 SEQ_PUT_FIELD_RET(s, entry->pid);
1830b52d 1727 SEQ_PUT_FIELD_RET(s, iter->cpu);
c4a8e8be
FW
1728 SEQ_PUT_FIELD_RET(s, iter->ts);
1729 }
cb0f12aa 1730
f633cef0 1731 event = ftrace_find_event(entry->type);
268ccda0 1732 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
cb0f12aa
IM
1733}
1734
bc0c38d1
SR
1735static int trace_empty(struct trace_iterator *iter)
1736{
bc0c38d1
SR
1737 int cpu;
1738
9aba60fe
SR
1739 /* If we are looking at one CPU buffer, only check that one */
1740 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1741 cpu = iter->cpu_file;
1742 if (iter->buffer_iter[cpu]) {
1743 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1744 return 0;
1745 } else {
1746 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1747 return 0;
1748 }
1749 return 1;
1750 }
1751
ab46428c 1752 for_each_tracing_cpu(cpu) {
d769041f
SR
1753 if (iter->buffer_iter[cpu]) {
1754 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1755 return 0;
1756 } else {
1757 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1758 return 0;
1759 }
bc0c38d1 1760 }
d769041f 1761
797d3712 1762 return 1;
bc0c38d1
SR
1763}
1764
2c4f035f 1765static enum print_line_t print_trace_line(struct trace_iterator *iter)
f9896bf3 1766{
2c4f035f
FW
1767 enum print_line_t ret;
1768
1769 if (iter->trace && iter->trace->print_line) {
1770 ret = iter->trace->print_line(iter);
1771 if (ret != TRACE_TYPE_UNHANDLED)
1772 return ret;
1773 }
72829bc3 1774
48ead020
FW
1775 if (iter->ent->type == TRACE_BPRINT &&
1776 trace_flags & TRACE_ITER_PRINTK &&
1777 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1778 return print_bprintk_msg_only(iter);
1779
66896a85
FW
1780 if (iter->ent->type == TRACE_PRINT &&
1781 trace_flags & TRACE_ITER_PRINTK &&
1782 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1783 return print_printk_msg_only(iter);
1784
cb0f12aa
IM
1785 if (trace_flags & TRACE_ITER_BIN)
1786 return print_bin_fmt(iter);
1787
5e3ca0ec
IM
1788 if (trace_flags & TRACE_ITER_HEX)
1789 return print_hex_fmt(iter);
1790
f9896bf3
IM
1791 if (trace_flags & TRACE_ITER_RAW)
1792 return print_raw_fmt(iter);
1793
f9896bf3
IM
1794 return print_trace_fmt(iter);
1795}
1796
bc0c38d1
SR
1797static int s_show(struct seq_file *m, void *v)
1798{
1799 struct trace_iterator *iter = v;
1800
1801 if (iter->ent == NULL) {
1802 if (iter->tr) {
1803 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1804 seq_puts(m, "#\n");
1805 }
8bba1bf5
MM
1806 if (iter->trace && iter->trace->print_header)
1807 iter->trace->print_header(m);
1808 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
bc0c38d1
SR
1809 /* print nothing if the buffers are empty */
1810 if (trace_empty(iter))
1811 return 0;
1812 print_trace_header(m, iter);
1813 if (!(trace_flags & TRACE_ITER_VERBOSE))
1814 print_lat_help_header(m);
1815 } else {
1816 if (!(trace_flags & TRACE_ITER_VERBOSE))
1817 print_func_help_header(m);
1818 }
1819 } else {
f9896bf3 1820 print_trace_line(iter);
214023c3 1821 trace_print_seq(m, &iter->seq);
bc0c38d1
SR
1822 }
1823
1824 return 0;
1825}
1826
1827static struct seq_operations tracer_seq_ops = {
4bf39a94
IM
1828 .start = s_start,
1829 .next = s_next,
1830 .stop = s_stop,
1831 .show = s_show,
bc0c38d1
SR
1832};
1833
e309b41d 1834static struct trace_iterator *
85a2f9b4 1835__tracing_open(struct inode *inode, struct file *file)
bc0c38d1 1836{
b04cc6b1 1837 long cpu_file = (long) inode->i_private;
85a2f9b4 1838 void *fail_ret = ERR_PTR(-ENOMEM);
bc0c38d1 1839 struct trace_iterator *iter;
3928a8a2 1840 struct seq_file *m;
85a2f9b4 1841 int cpu, ret;
bc0c38d1 1842
85a2f9b4
SR
1843 if (tracing_disabled)
1844 return ERR_PTR(-ENODEV);
60a11774 1845
bc0c38d1 1846 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
85a2f9b4
SR
1847 if (!iter)
1848 return ERR_PTR(-ENOMEM);
bc0c38d1 1849
d7350c3f
FW
1850 /*
1851 * We make a copy of the current tracer to avoid concurrent
1852 * changes on it while we are reading.
1853 */
bc0c38d1 1854 mutex_lock(&trace_types_lock);
d7350c3f 1855 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
85a2f9b4 1856 if (!iter->trace)
d7350c3f 1857 goto fail;
85a2f9b4 1858
d7350c3f
FW
1859 if (current_trace)
1860 *iter->trace = *current_trace;
1861
bc0c38d1
SR
1862 if (current_trace && current_trace->print_max)
1863 iter->tr = &max_tr;
1864 else
b04cc6b1 1865 iter->tr = &global_trace;
bc0c38d1 1866 iter->pos = -1;
d7350c3f 1867 mutex_init(&iter->mutex);
b04cc6b1 1868 iter->cpu_file = cpu_file;
bc0c38d1 1869
8bba1bf5
MM
1870 /* Notify the tracer early; before we stop tracing. */
1871 if (iter->trace && iter->trace->open)
a93751ca 1872 iter->trace->open(iter);
8bba1bf5 1873
12ef7d44
SR
1874 /* Annotate start of buffers if we had overruns */
1875 if (ring_buffer_overruns(iter->tr->buffer))
1876 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1877
b04cc6b1
FW
1878 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1879 for_each_tracing_cpu(cpu) {
12ef7d44 1880
b04cc6b1
FW
1881 iter->buffer_iter[cpu] =
1882 ring_buffer_read_start(iter->tr->buffer, cpu);
b04cc6b1
FW
1883 }
1884 } else {
1885 cpu = iter->cpu_file;
3928a8a2 1886 iter->buffer_iter[cpu] =
b04cc6b1 1887 ring_buffer_read_start(iter->tr->buffer, cpu);
3928a8a2
SR
1888 }
1889
bc0c38d1 1890 /* TODO stop tracer */
85a2f9b4
SR
1891 ret = seq_open(file, &tracer_seq_ops);
1892 if (ret < 0) {
1893 fail_ret = ERR_PTR(ret);
3928a8a2 1894 goto fail_buffer;
85a2f9b4 1895 }
bc0c38d1 1896
3928a8a2
SR
1897 m = file->private_data;
1898 m->private = iter;
bc0c38d1 1899
3928a8a2 1900 /* stop the trace while dumping */
9036990d 1901 tracing_stop();
3928a8a2 1902
bc0c38d1
SR
1903 mutex_unlock(&trace_types_lock);
1904
bc0c38d1 1905 return iter;
3928a8a2
SR
1906
1907 fail_buffer:
1908 for_each_tracing_cpu(cpu) {
1909 if (iter->buffer_iter[cpu])
1910 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1911 }
d7350c3f 1912 fail:
3928a8a2 1913 mutex_unlock(&trace_types_lock);
d7350c3f 1914 kfree(iter->trace);
0bb943c7 1915 kfree(iter);
3928a8a2 1916
85a2f9b4 1917 return fail_ret;
bc0c38d1
SR
1918}
1919
1920int tracing_open_generic(struct inode *inode, struct file *filp)
1921{
60a11774
SR
1922 if (tracing_disabled)
1923 return -ENODEV;
1924
bc0c38d1
SR
1925 filp->private_data = inode->i_private;
1926 return 0;
1927}
1928
4fd27358 1929static int tracing_release(struct inode *inode, struct file *file)
bc0c38d1
SR
1930{
1931 struct seq_file *m = (struct seq_file *)file->private_data;
1932 struct trace_iterator *iter = m->private;
3928a8a2 1933 int cpu;
bc0c38d1
SR
1934
1935 mutex_lock(&trace_types_lock);
3928a8a2
SR
1936 for_each_tracing_cpu(cpu) {
1937 if (iter->buffer_iter[cpu])
1938 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1939 }
1940
bc0c38d1
SR
1941 if (iter->trace && iter->trace->close)
1942 iter->trace->close(iter);
1943
1944 /* reenable tracing if it was previously enabled */
9036990d 1945 tracing_start();
bc0c38d1
SR
1946 mutex_unlock(&trace_types_lock);
1947
1948 seq_release(inode, file);
d7350c3f
FW
1949 mutex_destroy(&iter->mutex);
1950 kfree(iter->trace);
bc0c38d1
SR
1951 kfree(iter);
1952 return 0;
1953}
1954
1955static int tracing_open(struct inode *inode, struct file *file)
1956{
85a2f9b4
SR
1957 struct trace_iterator *iter;
1958 int ret = 0;
bc0c38d1 1959
85a2f9b4
SR
1960 iter = __tracing_open(inode, file);
1961 if (IS_ERR(iter))
1962 ret = PTR_ERR(iter);
c032ef64 1963 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
bc0c38d1
SR
1964 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1965
1966 return ret;
1967}
1968
e309b41d 1969static void *
bc0c38d1
SR
1970t_next(struct seq_file *m, void *v, loff_t *pos)
1971{
1972 struct tracer *t = m->private;
1973
1974 (*pos)++;
1975
1976 if (t)
1977 t = t->next;
1978
1979 m->private = t;
1980
1981 return t;
1982}
1983
1984static void *t_start(struct seq_file *m, loff_t *pos)
1985{
1986 struct tracer *t = m->private;
1987 loff_t l = 0;
1988
1989 mutex_lock(&trace_types_lock);
1990 for (; t && l < *pos; t = t_next(m, t, &l))
1991 ;
1992
1993 return t;
1994}
1995
1996static void t_stop(struct seq_file *m, void *p)
1997{
1998 mutex_unlock(&trace_types_lock);
1999}
2000
2001static int t_show(struct seq_file *m, void *v)
2002{
2003 struct tracer *t = v;
2004
2005 if (!t)
2006 return 0;
2007
2008 seq_printf(m, "%s", t->name);
2009 if (t->next)
2010 seq_putc(m, ' ');
2011 else
2012 seq_putc(m, '\n');
2013
2014 return 0;
2015}
2016
2017static struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
2018 .start = t_start,
2019 .next = t_next,
2020 .stop = t_stop,
2021 .show = t_show,
bc0c38d1
SR
2022};
2023
2024static int show_traces_open(struct inode *inode, struct file *file)
2025{
2026 int ret;
2027
60a11774
SR
2028 if (tracing_disabled)
2029 return -ENODEV;
2030
bc0c38d1
SR
2031 ret = seq_open(file, &show_traces_seq_ops);
2032 if (!ret) {
2033 struct seq_file *m = file->private_data;
2034 m->private = trace_types;
2035 }
2036
2037 return ret;
2038}
2039
5e2336a0 2040static const struct file_operations tracing_fops = {
4bf39a94
IM
2041 .open = tracing_open,
2042 .read = seq_read,
2043 .llseek = seq_lseek,
2044 .release = tracing_release,
bc0c38d1
SR
2045};
2046
5e2336a0 2047static const struct file_operations show_traces_fops = {
c7078de1
IM
2048 .open = show_traces_open,
2049 .read = seq_read,
2050 .release = seq_release,
2051};
2052
36dfe925
IM
2053/*
2054 * Only trace on a CPU if the bitmask is set:
2055 */
9e01c1b7 2056static cpumask_var_t tracing_cpumask;
36dfe925
IM
2057
2058/*
2059 * The tracer itself will not take this lock, but still we want
2060 * to provide a consistent cpumask to user-space:
2061 */
2062static DEFINE_MUTEX(tracing_cpumask_update_lock);
2063
2064/*
2065 * Temporary storage for the character representation of the
2066 * CPU bitmask (and one more byte for the newline):
2067 */
2068static char mask_str[NR_CPUS + 1];
2069
c7078de1
IM
2070static ssize_t
2071tracing_cpumask_read(struct file *filp, char __user *ubuf,
2072 size_t count, loff_t *ppos)
2073{
36dfe925 2074 int len;
c7078de1
IM
2075
2076 mutex_lock(&tracing_cpumask_update_lock);
36dfe925 2077
9e01c1b7 2078 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
36dfe925
IM
2079 if (count - len < 2) {
2080 count = -EINVAL;
2081 goto out_err;
2082 }
2083 len += sprintf(mask_str + len, "\n");
2084 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2085
2086out_err:
c7078de1
IM
2087 mutex_unlock(&tracing_cpumask_update_lock);
2088
2089 return count;
2090}
2091
2092static ssize_t
2093tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2094 size_t count, loff_t *ppos)
2095{
36dfe925 2096 int err, cpu;
9e01c1b7
RR
2097 cpumask_var_t tracing_cpumask_new;
2098
2099 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2100 return -ENOMEM;
c7078de1
IM
2101
2102 mutex_lock(&tracing_cpumask_update_lock);
9e01c1b7 2103 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
c7078de1 2104 if (err)
36dfe925
IM
2105 goto err_unlock;
2106
a5e25883 2107 local_irq_disable();
92205c23 2108 __raw_spin_lock(&ftrace_max_lock);
ab46428c 2109 for_each_tracing_cpu(cpu) {
36dfe925
IM
2110 /*
2111 * Increase/decrease the disabled counter if we are
2112 * about to flip a bit in the cpumask:
2113 */
9e01c1b7
RR
2114 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2115 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
36dfe925
IM
2116 atomic_inc(&global_trace.data[cpu]->disabled);
2117 }
9e01c1b7
RR
2118 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2119 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
36dfe925
IM
2120 atomic_dec(&global_trace.data[cpu]->disabled);
2121 }
2122 }
92205c23 2123 __raw_spin_unlock(&ftrace_max_lock);
a5e25883 2124 local_irq_enable();
36dfe925 2125
9e01c1b7 2126 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
36dfe925
IM
2127
2128 mutex_unlock(&tracing_cpumask_update_lock);
9e01c1b7 2129 free_cpumask_var(tracing_cpumask_new);
c7078de1
IM
2130
2131 return count;
36dfe925
IM
2132
2133err_unlock:
2134 mutex_unlock(&tracing_cpumask_update_lock);
9e01c1b7 2135 free_cpumask_var(tracing_cpumask);
36dfe925
IM
2136
2137 return err;
c7078de1
IM
2138}
2139
5e2336a0 2140static const struct file_operations tracing_cpumask_fops = {
c7078de1
IM
2141 .open = tracing_open_generic,
2142 .read = tracing_cpumask_read,
2143 .write = tracing_cpumask_write,
bc0c38d1
SR
2144};
2145
2146static ssize_t
ee6bce52 2147tracing_trace_options_read(struct file *filp, char __user *ubuf,
bc0c38d1
SR
2148 size_t cnt, loff_t *ppos)
2149{
d8e83d26
SR
2150 struct tracer_opt *trace_opts;
2151 u32 tracer_flags;
2152 int len = 0;
bc0c38d1
SR
2153 char *buf;
2154 int r = 0;
d8e83d26 2155 int i;
adf9f195 2156
bc0c38d1 2157
c3706f00 2158 /* calculate max size */
bc0c38d1
SR
2159 for (i = 0; trace_options[i]; i++) {
2160 len += strlen(trace_options[i]);
5c6a3ae1 2161 len += 3; /* "no" and newline */
bc0c38d1
SR
2162 }
2163
d8e83d26
SR
2164 mutex_lock(&trace_types_lock);
2165 tracer_flags = current_trace->flags->val;
2166 trace_opts = current_trace->flags->opts;
2167
adf9f195
FW
2168 /*
2169 * Increase the size with names of options specific
2170 * of the current tracer.
2171 */
2172 for (i = 0; trace_opts[i].name; i++) {
2173 len += strlen(trace_opts[i].name);
5c6a3ae1 2174 len += 3; /* "no" and newline */
adf9f195
FW
2175 }
2176
bc0c38d1
SR
2177 /* +2 for \n and \0 */
2178 buf = kmalloc(len + 2, GFP_KERNEL);
d8e83d26
SR
2179 if (!buf) {
2180 mutex_unlock(&trace_types_lock);
bc0c38d1 2181 return -ENOMEM;
d8e83d26 2182 }
bc0c38d1
SR
2183
2184 for (i = 0; trace_options[i]; i++) {
2185 if (trace_flags & (1 << i))
5c6a3ae1 2186 r += sprintf(buf + r, "%s\n", trace_options[i]);
bc0c38d1 2187 else
5c6a3ae1 2188 r += sprintf(buf + r, "no%s\n", trace_options[i]);
bc0c38d1
SR
2189 }
2190
adf9f195
FW
2191 for (i = 0; trace_opts[i].name; i++) {
2192 if (tracer_flags & trace_opts[i].bit)
5c6a3ae1 2193 r += sprintf(buf + r, "%s\n",
adf9f195
FW
2194 trace_opts[i].name);
2195 else
5c6a3ae1 2196 r += sprintf(buf + r, "no%s\n",
adf9f195
FW
2197 trace_opts[i].name);
2198 }
d8e83d26 2199 mutex_unlock(&trace_types_lock);
adf9f195 2200
bc0c38d1
SR
2201 WARN_ON(r >= len + 2);
2202
36dfe925 2203 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2204
2205 kfree(buf);
bc0c38d1
SR
2206 return r;
2207}
2208
adf9f195
FW
2209/* Try to assign a tracer specific option */
2210static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2211{
2212 struct tracer_flags *trace_flags = trace->flags;
2213 struct tracer_opt *opts = NULL;
2214 int ret = 0, i = 0;
2215 int len;
2216
2217 for (i = 0; trace_flags->opts[i].name; i++) {
2218 opts = &trace_flags->opts[i];
2219 len = strlen(opts->name);
2220
2221 if (strncmp(cmp, opts->name, len) == 0) {
2222 ret = trace->set_flag(trace_flags->val,
2223 opts->bit, !neg);
2224 break;
2225 }
2226 }
2227 /* Not found */
2228 if (!trace_flags->opts[i].name)
2229 return -EINVAL;
2230
2231 /* Refused to handle */
2232 if (ret)
2233 return ret;
2234
2235 if (neg)
2236 trace_flags->val &= ~opts->bit;
2237 else
2238 trace_flags->val |= opts->bit;
2239
2240 return 0;
2241}
2242
bc0c38d1 2243static ssize_t
ee6bce52 2244tracing_trace_options_write(struct file *filp, const char __user *ubuf,
bc0c38d1
SR
2245 size_t cnt, loff_t *ppos)
2246{
2247 char buf[64];
2248 char *cmp = buf;
2249 int neg = 0;
adf9f195 2250 int ret;
bc0c38d1
SR
2251 int i;
2252
cffae437
SR
2253 if (cnt >= sizeof(buf))
2254 return -EINVAL;
bc0c38d1
SR
2255
2256 if (copy_from_user(&buf, ubuf, cnt))
2257 return -EFAULT;
2258
2259 buf[cnt] = 0;
2260
2261 if (strncmp(buf, "no", 2) == 0) {
2262 neg = 1;
2263 cmp += 2;
2264 }
2265
2266 for (i = 0; trace_options[i]; i++) {
2267 int len = strlen(trace_options[i]);
2268
2269 if (strncmp(cmp, trace_options[i], len) == 0) {
2270 if (neg)
2271 trace_flags &= ~(1 << i);
2272 else
2273 trace_flags |= (1 << i);
2274 break;
2275 }
2276 }
adf9f195
FW
2277
2278 /* If no option could be set, test the specific tracer options */
2279 if (!trace_options[i]) {
d8e83d26 2280 mutex_lock(&trace_types_lock);
adf9f195 2281 ret = set_tracer_option(current_trace, cmp, neg);
d8e83d26 2282 mutex_unlock(&trace_types_lock);
adf9f195
FW
2283 if (ret)
2284 return ret;
2285 }
bc0c38d1
SR
2286
2287 filp->f_pos += cnt;
2288
2289 return cnt;
2290}
2291
5e2336a0 2292static const struct file_operations tracing_iter_fops = {
c7078de1 2293 .open = tracing_open_generic,
ee6bce52
SR
2294 .read = tracing_trace_options_read,
2295 .write = tracing_trace_options_write,
bc0c38d1
SR
2296};
2297
7bd2f24c
IM
2298static const char readme_msg[] =
2299 "tracing mini-HOWTO:\n\n"
2300 "# mkdir /debug\n"
2301 "# mount -t debugfs nodev /debug\n\n"
2302 "# cat /debug/tracing/available_tracers\n"
2303 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2304 "# cat /debug/tracing/current_tracer\n"
2305 "none\n"
2306 "# echo sched_switch > /debug/tracing/current_tracer\n"
2307 "# cat /debug/tracing/current_tracer\n"
2308 "sched_switch\n"
ee6bce52 2309 "# cat /debug/tracing/trace_options\n"
7bd2f24c 2310 "noprint-parent nosym-offset nosym-addr noverbose\n"
ee6bce52 2311 "# echo print-parent > /debug/tracing/trace_options\n"
7bd2f24c
IM
2312 "# echo 1 > /debug/tracing/tracing_enabled\n"
2313 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2314 "echo 0 > /debug/tracing/tracing_enabled\n"
2315;
2316
2317static ssize_t
2318tracing_readme_read(struct file *filp, char __user *ubuf,
2319 size_t cnt, loff_t *ppos)
2320{
2321 return simple_read_from_buffer(ubuf, cnt, ppos,
2322 readme_msg, strlen(readme_msg));
2323}
2324
5e2336a0 2325static const struct file_operations tracing_readme_fops = {
c7078de1
IM
2326 .open = tracing_open_generic,
2327 .read = tracing_readme_read,
7bd2f24c
IM
2328};
2329
bc0c38d1
SR
2330static ssize_t
2331tracing_ctrl_read(struct file *filp, char __user *ubuf,
2332 size_t cnt, loff_t *ppos)
2333{
bc0c38d1
SR
2334 char buf[64];
2335 int r;
2336
9036990d 2337 r = sprintf(buf, "%u\n", tracer_enabled);
4e3c3333 2338 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2339}
2340
2341static ssize_t
2342tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2343 size_t cnt, loff_t *ppos)
2344{
2345 struct trace_array *tr = filp->private_data;
bc0c38d1 2346 char buf[64];
5e39841c 2347 unsigned long val;
c6caeeb1 2348 int ret;
bc0c38d1 2349
cffae437
SR
2350 if (cnt >= sizeof(buf))
2351 return -EINVAL;
bc0c38d1
SR
2352
2353 if (copy_from_user(&buf, ubuf, cnt))
2354 return -EFAULT;
2355
2356 buf[cnt] = 0;
2357
c6caeeb1
SR
2358 ret = strict_strtoul(buf, 10, &val);
2359 if (ret < 0)
2360 return ret;
bc0c38d1
SR
2361
2362 val = !!val;
2363
2364 mutex_lock(&trace_types_lock);
9036990d
SR
2365 if (tracer_enabled ^ val) {
2366 if (val) {
bc0c38d1 2367 tracer_enabled = 1;
9036990d
SR
2368 if (current_trace->start)
2369 current_trace->start(tr);
2370 tracing_start();
2371 } else {
bc0c38d1 2372 tracer_enabled = 0;
9036990d
SR
2373 tracing_stop();
2374 if (current_trace->stop)
2375 current_trace->stop(tr);
2376 }
bc0c38d1
SR
2377 }
2378 mutex_unlock(&trace_types_lock);
2379
2380 filp->f_pos += cnt;
2381
2382 return cnt;
2383}
2384
2385static ssize_t
2386tracing_set_trace_read(struct file *filp, char __user *ubuf,
2387 size_t cnt, loff_t *ppos)
2388{
2389 char buf[max_tracer_type_len+2];
2390 int r;
2391
2392 mutex_lock(&trace_types_lock);
2393 if (current_trace)
2394 r = sprintf(buf, "%s\n", current_trace->name);
2395 else
2396 r = sprintf(buf, "\n");
2397 mutex_unlock(&trace_types_lock);
2398
4bf39a94 2399 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2400}
2401
b6f11df2
ACM
2402int tracer_init(struct tracer *t, struct trace_array *tr)
2403{
2404 tracing_reset_online_cpus(tr);
2405 return t->init(tr);
2406}
2407
73c5162a
SR
2408static int tracing_resize_ring_buffer(unsigned long size)
2409{
2410 int ret;
2411
2412 /*
2413 * If kernel or user changes the size of the ring buffer
a123c52b
SR
2414 * we use the size that was given, and we can forget about
2415 * expanding it later.
73c5162a
SR
2416 */
2417 ring_buffer_expanded = 1;
2418
2419 ret = ring_buffer_resize(global_trace.buffer, size);
2420 if (ret < 0)
2421 return ret;
2422
2423 ret = ring_buffer_resize(max_tr.buffer, size);
2424 if (ret < 0) {
2425 int r;
2426
2427 r = ring_buffer_resize(global_trace.buffer,
2428 global_trace.entries);
2429 if (r < 0) {
a123c52b
SR
2430 /*
2431 * AARGH! We are left with different
2432 * size max buffer!!!!
2433 * The max buffer is our "snapshot" buffer.
2434 * When a tracer needs a snapshot (one of the
2435 * latency tracers), it swaps the max buffer
2436 * with the saved snap shot. We succeeded to
2437 * update the size of the main buffer, but failed to
2438 * update the size of the max buffer. But when we tried
2439 * to reset the main buffer to the original size, we
2440 * failed there too. This is very unlikely to
2441 * happen, but if it does, warn and kill all
2442 * tracing.
2443 */
73c5162a
SR
2444 WARN_ON(1);
2445 tracing_disabled = 1;
2446 }
2447 return ret;
2448 }
2449
2450 global_trace.entries = size;
2451
2452 return ret;
2453}
2454
1852fcce
SR
2455/**
2456 * tracing_update_buffers - used by tracing facility to expand ring buffers
2457 *
2458 * To save on memory when the tracing is never used on a system with it
2459 * configured in. The ring buffers are set to a minimum size. But once
2460 * a user starts to use the tracing facility, then they need to grow
2461 * to their default size.
2462 *
2463 * This function is to be called when a tracer is about to be used.
2464 */
2465int tracing_update_buffers(void)
2466{
2467 int ret = 0;
2468
1027fcb2 2469 mutex_lock(&trace_types_lock);
1852fcce
SR
2470 if (!ring_buffer_expanded)
2471 ret = tracing_resize_ring_buffer(trace_buf_size);
1027fcb2 2472 mutex_unlock(&trace_types_lock);
1852fcce
SR
2473
2474 return ret;
2475}
2476
577b785f
SR
2477struct trace_option_dentry;
2478
2479static struct trace_option_dentry *
2480create_trace_option_files(struct tracer *tracer);
2481
2482static void
2483destroy_trace_option_files(struct trace_option_dentry *topts);
2484
b2821ae6 2485static int tracing_set_tracer(const char *buf)
bc0c38d1 2486{
577b785f 2487 static struct trace_option_dentry *topts;
bc0c38d1
SR
2488 struct trace_array *tr = &global_trace;
2489 struct tracer *t;
d9e54076 2490 int ret = 0;
bc0c38d1 2491
1027fcb2
SR
2492 mutex_lock(&trace_types_lock);
2493
73c5162a
SR
2494 if (!ring_buffer_expanded) {
2495 ret = tracing_resize_ring_buffer(trace_buf_size);
2496 if (ret < 0)
2497 return ret;
2498 ret = 0;
2499 }
2500
bc0c38d1
SR
2501 for (t = trace_types; t; t = t->next) {
2502 if (strcmp(t->name, buf) == 0)
2503 break;
2504 }
c2931e05
FW
2505 if (!t) {
2506 ret = -EINVAL;
2507 goto out;
2508 }
2509 if (t == current_trace)
bc0c38d1
SR
2510 goto out;
2511
9f029e83 2512 trace_branch_disable();
bc0c38d1
SR
2513 if (current_trace && current_trace->reset)
2514 current_trace->reset(tr);
2515
577b785f
SR
2516 destroy_trace_option_files(topts);
2517
bc0c38d1 2518 current_trace = t;
577b785f
SR
2519
2520 topts = create_trace_option_files(current_trace);
2521
1c80025a 2522 if (t->init) {
b6f11df2 2523 ret = tracer_init(t, tr);
1c80025a
FW
2524 if (ret)
2525 goto out;
2526 }
bc0c38d1 2527
9f029e83 2528 trace_branch_enable(tr);
bc0c38d1
SR
2529 out:
2530 mutex_unlock(&trace_types_lock);
2531
d9e54076
PZ
2532 return ret;
2533}
2534
2535static ssize_t
2536tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2537 size_t cnt, loff_t *ppos)
2538{
2539 char buf[max_tracer_type_len+1];
2540 int i;
2541 size_t ret;
e6e7a65a
FW
2542 int err;
2543
2544 ret = cnt;
d9e54076
PZ
2545
2546 if (cnt > max_tracer_type_len)
2547 cnt = max_tracer_type_len;
2548
2549 if (copy_from_user(&buf, ubuf, cnt))
2550 return -EFAULT;
2551
2552 buf[cnt] = 0;
2553
2554 /* strip ending whitespace. */
2555 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2556 buf[i] = 0;
2557
e6e7a65a
FW
2558 err = tracing_set_tracer(buf);
2559 if (err)
2560 return err;
d9e54076 2561
e6e7a65a 2562 filp->f_pos += ret;
bc0c38d1 2563
c2931e05 2564 return ret;
bc0c38d1
SR
2565}
2566
2567static ssize_t
2568tracing_max_lat_read(struct file *filp, char __user *ubuf,
2569 size_t cnt, loff_t *ppos)
2570{
2571 unsigned long *ptr = filp->private_data;
2572 char buf[64];
2573 int r;
2574
cffae437 2575 r = snprintf(buf, sizeof(buf), "%ld\n",
bc0c38d1 2576 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
cffae437
SR
2577 if (r > sizeof(buf))
2578 r = sizeof(buf);
4bf39a94 2579 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2580}
2581
2582static ssize_t
2583tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2584 size_t cnt, loff_t *ppos)
2585{
5e39841c 2586 unsigned long *ptr = filp->private_data;
bc0c38d1 2587 char buf[64];
5e39841c 2588 unsigned long val;
c6caeeb1 2589 int ret;
bc0c38d1 2590
cffae437
SR
2591 if (cnt >= sizeof(buf))
2592 return -EINVAL;
bc0c38d1
SR
2593
2594 if (copy_from_user(&buf, ubuf, cnt))
2595 return -EFAULT;
2596
2597 buf[cnt] = 0;
2598
c6caeeb1
SR
2599 ret = strict_strtoul(buf, 10, &val);
2600 if (ret < 0)
2601 return ret;
bc0c38d1
SR
2602
2603 *ptr = val * 1000;
2604
2605 return cnt;
2606}
2607
b3806b43
SR
2608static int tracing_open_pipe(struct inode *inode, struct file *filp)
2609{
b04cc6b1 2610 long cpu_file = (long) inode->i_private;
b3806b43 2611 struct trace_iterator *iter;
b04cc6b1 2612 int ret = 0;
b3806b43
SR
2613
2614 if (tracing_disabled)
2615 return -ENODEV;
2616
b04cc6b1
FW
2617 mutex_lock(&trace_types_lock);
2618
2619 /* We only allow one reader per cpu */
2620 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2621 if (!cpumask_empty(tracing_reader_cpumask)) {
2622 ret = -EBUSY;
2623 goto out;
2624 }
2625 cpumask_setall(tracing_reader_cpumask);
2626 } else {
2627 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2628 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2629 else {
2630 ret = -EBUSY;
2631 goto out;
2632 }
b3806b43
SR
2633 }
2634
2635 /* create a buffer to store the information to pass to userspace */
2636 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
b04cc6b1
FW
2637 if (!iter) {
2638 ret = -ENOMEM;
2639 goto out;
2640 }
b3806b43 2641
d7350c3f
FW
2642 /*
2643 * We make a copy of the current tracer to avoid concurrent
2644 * changes on it while we are reading.
2645 */
2646 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2647 if (!iter->trace) {
2648 ret = -ENOMEM;
2649 goto fail;
2650 }
2651 if (current_trace)
2652 *iter->trace = *current_trace;
2653
4462344e 2654 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
b04cc6b1 2655 ret = -ENOMEM;
d7350c3f 2656 goto fail;
4462344e
RR
2657 }
2658
a309720c 2659 /* trace pipe does not show start of buffer */
4462344e 2660 cpumask_setall(iter->started);
a309720c 2661
b04cc6b1 2662 iter->cpu_file = cpu_file;
b3806b43 2663 iter->tr = &global_trace;
d7350c3f 2664 mutex_init(&iter->mutex);
b3806b43
SR
2665 filp->private_data = iter;
2666
107bad8b
SR
2667 if (iter->trace->pipe_open)
2668 iter->trace->pipe_open(iter);
107bad8b 2669
b04cc6b1
FW
2670out:
2671 mutex_unlock(&trace_types_lock);
2672 return ret;
d7350c3f
FW
2673
2674fail:
2675 kfree(iter->trace);
2676 kfree(iter);
2677 mutex_unlock(&trace_types_lock);
2678 return ret;
b3806b43
SR
2679}
2680
2681static int tracing_release_pipe(struct inode *inode, struct file *file)
2682{
2683 struct trace_iterator *iter = file->private_data;
2684
b04cc6b1
FW
2685 mutex_lock(&trace_types_lock);
2686
2687 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2688 cpumask_clear(tracing_reader_cpumask);
2689 else
2690 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2691
2692 mutex_unlock(&trace_types_lock);
2693
4462344e 2694 free_cpumask_var(iter->started);
d7350c3f
FW
2695 mutex_destroy(&iter->mutex);
2696 kfree(iter->trace);
b3806b43 2697 kfree(iter);
b3806b43
SR
2698
2699 return 0;
2700}
2701
2a2cc8f7
SSP
2702static unsigned int
2703tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2704{
2705 struct trace_iterator *iter = filp->private_data;
2706
2707 if (trace_flags & TRACE_ITER_BLOCK) {
2708 /*
2709 * Always select as readable when in blocking mode
2710 */
2711 return POLLIN | POLLRDNORM;
afc2abc0 2712 } else {
2a2cc8f7
SSP
2713 if (!trace_empty(iter))
2714 return POLLIN | POLLRDNORM;
2715 poll_wait(filp, &trace_wait, poll_table);
2716 if (!trace_empty(iter))
2717 return POLLIN | POLLRDNORM;
2718
2719 return 0;
2720 }
2721}
2722
6eaaa5d5
FW
2723
2724void default_wait_pipe(struct trace_iterator *iter)
2725{
2726 DEFINE_WAIT(wait);
2727
2728 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2729
2730 if (trace_empty(iter))
2731 schedule();
2732
2733 finish_wait(&trace_wait, &wait);
2734}
2735
2736/*
2737 * This is a make-shift waitqueue.
2738 * A tracer might use this callback on some rare cases:
2739 *
2740 * 1) the current tracer might hold the runqueue lock when it wakes up
2741 * a reader, hence a deadlock (sched, function, and function graph tracers)
2742 * 2) the function tracers, trace all functions, we don't want
2743 * the overhead of calling wake_up and friends
2744 * (and tracing them too)
2745 *
2746 * Anyway, this is really very primitive wakeup.
2747 */
2748void poll_wait_pipe(struct trace_iterator *iter)
2749{
2750 set_current_state(TASK_INTERRUPTIBLE);
2751 /* sleep for 100 msecs, and try again. */
2752 schedule_timeout(HZ / 10);
2753}
2754
ff98781b
EGM
2755/* Must be called with trace_types_lock mutex held. */
2756static int tracing_wait_pipe(struct file *filp)
b3806b43
SR
2757{
2758 struct trace_iterator *iter = filp->private_data;
b3806b43 2759
b3806b43 2760 while (trace_empty(iter)) {
2dc8f095 2761
107bad8b 2762 if ((filp->f_flags & O_NONBLOCK)) {
ff98781b 2763 return -EAGAIN;
107bad8b 2764 }
2dc8f095 2765
d7350c3f 2766 mutex_unlock(&iter->mutex);
107bad8b 2767
6eaaa5d5 2768 iter->trace->wait_pipe(iter);
b3806b43 2769
d7350c3f 2770 mutex_lock(&iter->mutex);
107bad8b 2771
6eaaa5d5 2772 if (signal_pending(current))
ff98781b 2773 return -EINTR;
b3806b43
SR
2774
2775 /*
2776 * We block until we read something and tracing is disabled.
2777 * We still block if tracing is disabled, but we have never
2778 * read anything. This allows a user to cat this file, and
2779 * then enable tracing. But after we have read something,
2780 * we give an EOF when tracing is again disabled.
2781 *
2782 * iter->pos will be 0 if we haven't read anything.
2783 */
2784 if (!tracer_enabled && iter->pos)
2785 break;
b3806b43
SR
2786 }
2787
ff98781b
EGM
2788 return 1;
2789}
2790
2791/*
2792 * Consumer reader.
2793 */
2794static ssize_t
2795tracing_read_pipe(struct file *filp, char __user *ubuf,
2796 size_t cnt, loff_t *ppos)
2797{
2798 struct trace_iterator *iter = filp->private_data;
d7350c3f 2799 static struct tracer *old_tracer;
ff98781b
EGM
2800 ssize_t sret;
2801
2802 /* return any leftover data */
2803 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2804 if (sret != -EBUSY)
2805 return sret;
2806
f9520750 2807 trace_seq_init(&iter->seq);
ff98781b 2808
d7350c3f 2809 /* copy the tracer to avoid using a global lock all around */
ff98781b 2810 mutex_lock(&trace_types_lock);
d7350c3f
FW
2811 if (unlikely(old_tracer != current_trace && current_trace)) {
2812 old_tracer = current_trace;
2813 *iter->trace = *current_trace;
2814 }
2815 mutex_unlock(&trace_types_lock);
2816
2817 /*
2818 * Avoid more than one consumer on a single file descriptor
2819 * This is just a matter of traces coherency, the ring buffer itself
2820 * is protected.
2821 */
2822 mutex_lock(&iter->mutex);
ff98781b
EGM
2823 if (iter->trace->read) {
2824 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2825 if (sret)
2826 goto out;
2827 }
2828
2829waitagain:
2830 sret = tracing_wait_pipe(filp);
2831 if (sret <= 0)
2832 goto out;
2833
b3806b43 2834 /* stop when tracing is finished */
ff98781b
EGM
2835 if (trace_empty(iter)) {
2836 sret = 0;
107bad8b 2837 goto out;
ff98781b 2838 }
b3806b43
SR
2839
2840 if (cnt >= PAGE_SIZE)
2841 cnt = PAGE_SIZE - 1;
2842
53d0aa77 2843 /* reset all but tr, trace, and overruns */
53d0aa77
SR
2844 memset(&iter->seq, 0,
2845 sizeof(struct trace_iterator) -
2846 offsetof(struct trace_iterator, seq));
4823ed7e 2847 iter->pos = -1;
b3806b43 2848
088b1e42 2849 while (find_next_entry_inc(iter) != NULL) {
2c4f035f 2850 enum print_line_t ret;
088b1e42
SR
2851 int len = iter->seq.len;
2852
f9896bf3 2853 ret = print_trace_line(iter);
2c4f035f 2854 if (ret == TRACE_TYPE_PARTIAL_LINE) {
088b1e42
SR
2855 /* don't print partial lines */
2856 iter->seq.len = len;
b3806b43 2857 break;
088b1e42 2858 }
b91facc3
FW
2859 if (ret != TRACE_TYPE_NO_CONSUME)
2860 trace_consume(iter);
b3806b43
SR
2861
2862 if (iter->seq.len >= cnt)
2863 break;
b3806b43
SR
2864 }
2865
b3806b43 2866 /* Now copy what we have to the user */
6c6c2796
PP
2867 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2868 if (iter->seq.readpos >= iter->seq.len)
f9520750 2869 trace_seq_init(&iter->seq);
9ff4b974
PP
2870
2871 /*
2872 * If there was nothing to send to user, inspite of consuming trace
2873 * entries, go back to wait for more entries.
2874 */
6c6c2796 2875 if (sret == -EBUSY)
9ff4b974 2876 goto waitagain;
b3806b43 2877
107bad8b 2878out:
d7350c3f 2879 mutex_unlock(&iter->mutex);
107bad8b 2880
6c6c2796 2881 return sret;
b3806b43
SR
2882}
2883
3c56819b
EGM
2884static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2885 struct pipe_buffer *buf)
2886{
2887 __free_page(buf->page);
2888}
2889
2890static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2891 unsigned int idx)
2892{
2893 __free_page(spd->pages[idx]);
2894}
2895
2896static struct pipe_buf_operations tracing_pipe_buf_ops = {
34cd4998
SR
2897 .can_merge = 0,
2898 .map = generic_pipe_buf_map,
2899 .unmap = generic_pipe_buf_unmap,
2900 .confirm = generic_pipe_buf_confirm,
2901 .release = tracing_pipe_buf_release,
2902 .steal = generic_pipe_buf_steal,
2903 .get = generic_pipe_buf_get,
3c56819b
EGM
2904};
2905
34cd4998 2906static size_t
fa7c7f6e 2907tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
34cd4998
SR
2908{
2909 size_t count;
2910 int ret;
2911
2912 /* Seq buffer is page-sized, exactly what we need. */
2913 for (;;) {
2914 count = iter->seq.len;
2915 ret = print_trace_line(iter);
2916 count = iter->seq.len - count;
2917 if (rem < count) {
2918 rem = 0;
2919 iter->seq.len -= count;
2920 break;
2921 }
2922 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2923 iter->seq.len -= count;
2924 break;
2925 }
2926
2927 trace_consume(iter);
2928 rem -= count;
2929 if (!find_next_entry_inc(iter)) {
2930 rem = 0;
2931 iter->ent = NULL;
2932 break;
2933 }
2934 }
2935
2936 return rem;
2937}
2938
3c56819b
EGM
2939static ssize_t tracing_splice_read_pipe(struct file *filp,
2940 loff_t *ppos,
2941 struct pipe_inode_info *pipe,
2942 size_t len,
2943 unsigned int flags)
2944{
2945 struct page *pages[PIPE_BUFFERS];
2946 struct partial_page partial[PIPE_BUFFERS];
2947 struct trace_iterator *iter = filp->private_data;
2948 struct splice_pipe_desc spd = {
34cd4998
SR
2949 .pages = pages,
2950 .partial = partial,
2951 .nr_pages = 0, /* This gets updated below. */
2952 .flags = flags,
2953 .ops = &tracing_pipe_buf_ops,
2954 .spd_release = tracing_spd_release_pipe,
3c56819b 2955 };
d7350c3f 2956 static struct tracer *old_tracer;
3c56819b 2957 ssize_t ret;
34cd4998 2958 size_t rem;
3c56819b
EGM
2959 unsigned int i;
2960
d7350c3f 2961 /* copy the tracer to avoid using a global lock all around */
3c56819b 2962 mutex_lock(&trace_types_lock);
d7350c3f
FW
2963 if (unlikely(old_tracer != current_trace && current_trace)) {
2964 old_tracer = current_trace;
2965 *iter->trace = *current_trace;
2966 }
2967 mutex_unlock(&trace_types_lock);
2968
2969 mutex_lock(&iter->mutex);
3c56819b
EGM
2970
2971 if (iter->trace->splice_read) {
2972 ret = iter->trace->splice_read(iter, filp,
2973 ppos, pipe, len, flags);
2974 if (ret)
34cd4998 2975 goto out_err;
3c56819b
EGM
2976 }
2977
2978 ret = tracing_wait_pipe(filp);
2979 if (ret <= 0)
34cd4998 2980 goto out_err;
3c56819b
EGM
2981
2982 if (!iter->ent && !find_next_entry_inc(iter)) {
2983 ret = -EFAULT;
34cd4998 2984 goto out_err;
3c56819b
EGM
2985 }
2986
2987 /* Fill as many pages as possible. */
2988 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2989 pages[i] = alloc_page(GFP_KERNEL);
34cd4998
SR
2990 if (!pages[i])
2991 break;
3c56819b 2992
fa7c7f6e 2993 rem = tracing_fill_pipe_page(rem, iter);
3c56819b
EGM
2994
2995 /* Copy the data into the page, so we can start over. */
2996 ret = trace_seq_to_buffer(&iter->seq,
2997 page_address(pages[i]),
2998 iter->seq.len);
2999 if (ret < 0) {
3000 __free_page(pages[i]);
3001 break;
3002 }
3003 partial[i].offset = 0;
3004 partial[i].len = iter->seq.len;
3005
f9520750 3006 trace_seq_init(&iter->seq);
3c56819b
EGM
3007 }
3008
d7350c3f 3009 mutex_unlock(&iter->mutex);
3c56819b
EGM
3010
3011 spd.nr_pages = i;
3012
3013 return splice_to_pipe(pipe, &spd);
3014
34cd4998 3015out_err:
d7350c3f 3016 mutex_unlock(&iter->mutex);
3c56819b
EGM
3017
3018 return ret;
3019}
3020
a98a3c3f
SR
3021static ssize_t
3022tracing_entries_read(struct file *filp, char __user *ubuf,
3023 size_t cnt, loff_t *ppos)
3024{
3025 struct trace_array *tr = filp->private_data;
db526ca3 3026 char buf[96];
a98a3c3f
SR
3027 int r;
3028
db526ca3
SR
3029 mutex_lock(&trace_types_lock);
3030 if (!ring_buffer_expanded)
3031 r = sprintf(buf, "%lu (expanded: %lu)\n",
3032 tr->entries >> 10,
3033 trace_buf_size >> 10);
3034 else
3035 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3036 mutex_unlock(&trace_types_lock);
3037
a98a3c3f
SR
3038 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3039}
3040
3041static ssize_t
3042tracing_entries_write(struct file *filp, const char __user *ubuf,
3043 size_t cnt, loff_t *ppos)
3044{
3045 unsigned long val;
3046 char buf[64];
bf5e6519 3047 int ret, cpu;
a98a3c3f 3048
cffae437
SR
3049 if (cnt >= sizeof(buf))
3050 return -EINVAL;
a98a3c3f
SR
3051
3052 if (copy_from_user(&buf, ubuf, cnt))
3053 return -EFAULT;
3054
3055 buf[cnt] = 0;
3056
c6caeeb1
SR
3057 ret = strict_strtoul(buf, 10, &val);
3058 if (ret < 0)
3059 return ret;
a98a3c3f
SR
3060
3061 /* must have at least 1 entry */
3062 if (!val)
3063 return -EINVAL;
3064
3065 mutex_lock(&trace_types_lock);
3066
c76f0694 3067 tracing_stop();
a98a3c3f 3068
bf5e6519
SR
3069 /* disable all cpu buffers */
3070 for_each_tracing_cpu(cpu) {
3071 if (global_trace.data[cpu])
3072 atomic_inc(&global_trace.data[cpu]->disabled);
3073 if (max_tr.data[cpu])
3074 atomic_inc(&max_tr.data[cpu]->disabled);
3075 }
3076
1696b2b0
SR
3077 /* value is in KB */
3078 val <<= 10;
3079
3928a8a2 3080 if (val != global_trace.entries) {
73c5162a 3081 ret = tracing_resize_ring_buffer(val);
3928a8a2
SR
3082 if (ret < 0) {
3083 cnt = ret;
3eefae99
SR
3084 goto out;
3085 }
a98a3c3f
SR
3086 }
3087
3088 filp->f_pos += cnt;
3089
19384c03
SR
3090 /* If check pages failed, return ENOMEM */
3091 if (tracing_disabled)
3092 cnt = -ENOMEM;
a98a3c3f 3093 out:
bf5e6519
SR
3094 for_each_tracing_cpu(cpu) {
3095 if (global_trace.data[cpu])
3096 atomic_dec(&global_trace.data[cpu]->disabled);
3097 if (max_tr.data[cpu])
3098 atomic_dec(&max_tr.data[cpu]->disabled);
3099 }
3100
c76f0694 3101 tracing_start();
a98a3c3f
SR
3102 max_tr.entries = global_trace.entries;
3103 mutex_unlock(&trace_types_lock);
3104
3105 return cnt;
3106}
3107
5bf9a1ee
PP
3108static int mark_printk(const char *fmt, ...)
3109{
3110 int ret;
3111 va_list args;
3112 va_start(args, fmt);
1fd8f2a3 3113 ret = trace_vprintk(0, -1, fmt, args);
5bf9a1ee
PP
3114 va_end(args);
3115 return ret;
3116}
3117
3118static ssize_t
3119tracing_mark_write(struct file *filp, const char __user *ubuf,
3120 size_t cnt, loff_t *fpos)
3121{
3122 char *buf;
3123 char *end;
5bf9a1ee 3124
c76f0694 3125 if (tracing_disabled)
5bf9a1ee
PP
3126 return -EINVAL;
3127
3128 if (cnt > TRACE_BUF_SIZE)
3129 cnt = TRACE_BUF_SIZE;
3130
3131 buf = kmalloc(cnt + 1, GFP_KERNEL);
3132 if (buf == NULL)
3133 return -ENOMEM;
3134
3135 if (copy_from_user(buf, ubuf, cnt)) {
3136 kfree(buf);
3137 return -EFAULT;
3138 }
3139
3140 /* Cut from the first nil or newline. */
3141 buf[cnt] = '\0';
3142 end = strchr(buf, '\n');
3143 if (end)
3144 *end = '\0';
3145
3146 cnt = mark_printk("%s\n", buf);
3147 kfree(buf);
3148 *fpos += cnt;
3149
3150 return cnt;
3151}
3152
5e2336a0 3153static const struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
3154 .open = tracing_open_generic,
3155 .read = tracing_max_lat_read,
3156 .write = tracing_max_lat_write,
bc0c38d1
SR
3157};
3158
5e2336a0 3159static const struct file_operations tracing_ctrl_fops = {
4bf39a94
IM
3160 .open = tracing_open_generic,
3161 .read = tracing_ctrl_read,
3162 .write = tracing_ctrl_write,
bc0c38d1
SR
3163};
3164
5e2336a0 3165static const struct file_operations set_tracer_fops = {
4bf39a94
IM
3166 .open = tracing_open_generic,
3167 .read = tracing_set_trace_read,
3168 .write = tracing_set_trace_write,
bc0c38d1
SR
3169};
3170
5e2336a0 3171static const struct file_operations tracing_pipe_fops = {
4bf39a94 3172 .open = tracing_open_pipe,
2a2cc8f7 3173 .poll = tracing_poll_pipe,
4bf39a94 3174 .read = tracing_read_pipe,
3c56819b 3175 .splice_read = tracing_splice_read_pipe,
4bf39a94 3176 .release = tracing_release_pipe,
b3806b43
SR
3177};
3178
5e2336a0 3179static const struct file_operations tracing_entries_fops = {
a98a3c3f
SR
3180 .open = tracing_open_generic,
3181 .read = tracing_entries_read,
3182 .write = tracing_entries_write,
3183};
3184
5e2336a0 3185static const struct file_operations tracing_mark_fops = {
43a15386 3186 .open = tracing_open_generic,
5bf9a1ee
PP
3187 .write = tracing_mark_write,
3188};
3189
2cadf913
SR
3190struct ftrace_buffer_info {
3191 struct trace_array *tr;
3192 void *spare;
3193 int cpu;
3194 unsigned int read;
3195};
3196
3197static int tracing_buffers_open(struct inode *inode, struct file *filp)
3198{
3199 int cpu = (int)(long)inode->i_private;
3200 struct ftrace_buffer_info *info;
3201
3202 if (tracing_disabled)
3203 return -ENODEV;
3204
3205 info = kzalloc(sizeof(*info), GFP_KERNEL);
3206 if (!info)
3207 return -ENOMEM;
3208
3209 info->tr = &global_trace;
3210 info->cpu = cpu;
3211 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3212 /* Force reading ring buffer for first read */
3213 info->read = (unsigned int)-1;
3214 if (!info->spare)
3215 goto out;
3216
3217 filp->private_data = info;
3218
3219 return 0;
3220
3221 out:
3222 kfree(info);
3223 return -ENOMEM;
3224}
3225
3226static ssize_t
3227tracing_buffers_read(struct file *filp, char __user *ubuf,
3228 size_t count, loff_t *ppos)
3229{
3230 struct ftrace_buffer_info *info = filp->private_data;
3231 unsigned int pos;
3232 ssize_t ret;
3233 size_t size;
3234
2dc5d12b
SR
3235 if (!count)
3236 return 0;
3237
2cadf913
SR
3238 /* Do we have previous read data to read? */
3239 if (info->read < PAGE_SIZE)
3240 goto read;
3241
3242 info->read = 0;
3243
3244 ret = ring_buffer_read_page(info->tr->buffer,
3245 &info->spare,
3246 count,
3247 info->cpu, 0);
3248 if (ret < 0)
3249 return 0;
3250
3251 pos = ring_buffer_page_len(info->spare);
3252
3253 if (pos < PAGE_SIZE)
3254 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3255
3256read:
3257 size = PAGE_SIZE - info->read;
3258 if (size > count)
3259 size = count;
3260
3261 ret = copy_to_user(ubuf, info->spare + info->read, size);
2dc5d12b 3262 if (ret == size)
2cadf913 3263 return -EFAULT;
2dc5d12b
SR
3264 size -= ret;
3265
2cadf913
SR
3266 *ppos += size;
3267 info->read += size;
3268
3269 return size;
3270}
3271
3272static int tracing_buffers_release(struct inode *inode, struct file *file)
3273{
3274 struct ftrace_buffer_info *info = file->private_data;
3275
3276 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3277 kfree(info);
3278
3279 return 0;
3280}
3281
3282struct buffer_ref {
3283 struct ring_buffer *buffer;
3284 void *page;
3285 int ref;
3286};
3287
3288static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3289 struct pipe_buffer *buf)
3290{
3291 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3292
3293 if (--ref->ref)
3294 return;
3295
3296 ring_buffer_free_read_page(ref->buffer, ref->page);
3297 kfree(ref);
3298 buf->private = 0;
3299}
3300
3301static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3302 struct pipe_buffer *buf)
3303{
3304 return 1;
3305}
3306
3307static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3308 struct pipe_buffer *buf)
3309{
3310 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3311
3312 ref->ref++;
3313}
3314
3315/* Pipe buffer operations for a buffer. */
3316static struct pipe_buf_operations buffer_pipe_buf_ops = {
3317 .can_merge = 0,
3318 .map = generic_pipe_buf_map,
3319 .unmap = generic_pipe_buf_unmap,
3320 .confirm = generic_pipe_buf_confirm,
3321 .release = buffer_pipe_buf_release,
3322 .steal = buffer_pipe_buf_steal,
3323 .get = buffer_pipe_buf_get,
3324};
3325
3326/*
3327 * Callback from splice_to_pipe(), if we need to release some pages
3328 * at the end of the spd in case we error'ed out in filling the pipe.
3329 */
3330static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3331{
3332 struct buffer_ref *ref =
3333 (struct buffer_ref *)spd->partial[i].private;
3334
3335 if (--ref->ref)
3336 return;
3337
3338 ring_buffer_free_read_page(ref->buffer, ref->page);
3339 kfree(ref);
3340 spd->partial[i].private = 0;
3341}
3342
3343static ssize_t
3344tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3345 struct pipe_inode_info *pipe, size_t len,
3346 unsigned int flags)
3347{
3348 struct ftrace_buffer_info *info = file->private_data;
3349 struct partial_page partial[PIPE_BUFFERS];
3350 struct page *pages[PIPE_BUFFERS];
3351 struct splice_pipe_desc spd = {
3352 .pages = pages,
3353 .partial = partial,
3354 .flags = flags,
3355 .ops = &buffer_pipe_buf_ops,
3356 .spd_release = buffer_spd_release,
3357 };
3358 struct buffer_ref *ref;
3359 int size, i;
3360 size_t ret;
3361
3362 /*
3363 * We can't seek on a buffer input
3364 */
3365 if (unlikely(*ppos))
3366 return -ESPIPE;
3367
3368
3369 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3370 struct page *page;
3371 int r;
3372
3373 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3374 if (!ref)
3375 break;
3376
3377 ref->buffer = info->tr->buffer;
3378 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3379 if (!ref->page) {
3380 kfree(ref);
3381 break;
3382 }
3383
3384 r = ring_buffer_read_page(ref->buffer, &ref->page,
3385 len, info->cpu, 0);
3386 if (r < 0) {
3387 ring_buffer_free_read_page(ref->buffer,
3388 ref->page);
3389 kfree(ref);
3390 break;
3391 }
3392
3393 /*
3394 * zero out any left over data, this is going to
3395 * user land.
3396 */
3397 size = ring_buffer_page_len(ref->page);
3398 if (size < PAGE_SIZE)
3399 memset(ref->page + size, 0, PAGE_SIZE - size);
3400
3401 page = virt_to_page(ref->page);
3402
3403 spd.pages[i] = page;
3404 spd.partial[i].len = PAGE_SIZE;
3405 spd.partial[i].offset = 0;
3406 spd.partial[i].private = (unsigned long)ref;
3407 spd.nr_pages++;
3408 }
3409
3410 spd.nr_pages = i;
3411
3412 /* did we read anything? */
3413 if (!spd.nr_pages) {
3414 if (flags & SPLICE_F_NONBLOCK)
3415 ret = -EAGAIN;
3416 else
3417 ret = 0;
3418 /* TODO: block */
3419 return ret;
3420 }
3421
3422 ret = splice_to_pipe(pipe, &spd);
3423
3424 return ret;
3425}
3426
3427static const struct file_operations tracing_buffers_fops = {
3428 .open = tracing_buffers_open,
3429 .read = tracing_buffers_read,
3430 .release = tracing_buffers_release,
3431 .splice_read = tracing_buffers_splice_read,
3432 .llseek = no_llseek,
3433};
3434
bc0c38d1
SR
3435#ifdef CONFIG_DYNAMIC_FTRACE
3436
b807c3d0
SR
3437int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3438{
3439 return 0;
3440}
3441
bc0c38d1 3442static ssize_t
b807c3d0 3443tracing_read_dyn_info(struct file *filp, char __user *ubuf,
bc0c38d1
SR
3444 size_t cnt, loff_t *ppos)
3445{
a26a2a27
SR
3446 static char ftrace_dyn_info_buffer[1024];
3447 static DEFINE_MUTEX(dyn_info_mutex);
bc0c38d1 3448 unsigned long *p = filp->private_data;
b807c3d0 3449 char *buf = ftrace_dyn_info_buffer;
a26a2a27 3450 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
bc0c38d1
SR
3451 int r;
3452
b807c3d0
SR
3453 mutex_lock(&dyn_info_mutex);
3454 r = sprintf(buf, "%ld ", *p);
4bf39a94 3455
a26a2a27 3456 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
b807c3d0
SR
3457 buf[r++] = '\n';
3458
3459 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3460
3461 mutex_unlock(&dyn_info_mutex);
3462
3463 return r;
bc0c38d1
SR
3464}
3465
5e2336a0 3466static const struct file_operations tracing_dyn_info_fops = {
4bf39a94 3467 .open = tracing_open_generic,
b807c3d0 3468 .read = tracing_read_dyn_info,
bc0c38d1
SR
3469};
3470#endif
3471
3472static struct dentry *d_tracer;
3473
3474struct dentry *tracing_init_dentry(void)
3475{
3476 static int once;
3477
3478 if (d_tracer)
3479 return d_tracer;
3480
3481 d_tracer = debugfs_create_dir("tracing", NULL);
3482
3483 if (!d_tracer && !once) {
3484 once = 1;
3485 pr_warning("Could not create debugfs directory 'tracing'\n");
3486 return NULL;
3487 }
3488
3489 return d_tracer;
3490}
3491
b04cc6b1
FW
3492static struct dentry *d_percpu;
3493
3494struct dentry *tracing_dentry_percpu(void)
3495{
3496 static int once;
3497 struct dentry *d_tracer;
3498
3499 if (d_percpu)
3500 return d_percpu;
3501
3502 d_tracer = tracing_init_dentry();
3503
3504 if (!d_tracer)
3505 return NULL;
3506
3507 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3508
3509 if (!d_percpu && !once) {
3510 once = 1;
3511 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3512 return NULL;
3513 }
3514
3515 return d_percpu;
3516}
3517
3518static void tracing_init_debugfs_percpu(long cpu)
3519{
3520 struct dentry *d_percpu = tracing_dentry_percpu();
8656e7a2
FW
3521 struct dentry *entry, *d_cpu;
3522 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3523 char cpu_dir[7];
b04cc6b1
FW
3524
3525 if (cpu > 999 || cpu < 0)
3526 return;
3527
8656e7a2
FW
3528 sprintf(cpu_dir, "cpu%ld", cpu);
3529 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3530 if (!d_cpu) {
3531 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3532 return;
3533 }
b04cc6b1 3534
8656e7a2
FW
3535 /* per cpu trace_pipe */
3536 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
b04cc6b1
FW
3537 (void *) cpu, &tracing_pipe_fops);
3538 if (!entry)
8656e7a2 3539 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
b04cc6b1
FW
3540
3541 /* per cpu trace */
8656e7a2 3542 entry = debugfs_create_file("trace", 0444, d_cpu,
b04cc6b1
FW
3543 (void *) cpu, &tracing_fops);
3544 if (!entry)
8656e7a2 3545 pr_warning("Could not create debugfs 'trace' entry\n");
7f96f93f
SR
3546
3547 entry = debugfs_create_file("trace_pipe_raw", 0444, d_cpu,
3548 (void *) cpu, &tracing_buffers_fops);
3549 if (!entry)
3550 pr_warning("Could not create debugfs 'trace_pipe_raw' entry\n");
b04cc6b1
FW
3551}
3552
60a11774
SR
3553#ifdef CONFIG_FTRACE_SELFTEST
3554/* Let selftest have access to static functions in this file */
3555#include "trace_selftest.c"
3556#endif
3557
577b785f
SR
3558struct trace_option_dentry {
3559 struct tracer_opt *opt;
3560 struct tracer_flags *flags;
3561 struct dentry *entry;
3562};
3563
3564static ssize_t
3565trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3566 loff_t *ppos)
3567{
3568 struct trace_option_dentry *topt = filp->private_data;
3569 char *buf;
3570
3571 if (topt->flags->val & topt->opt->bit)
3572 buf = "1\n";
3573 else
3574 buf = "0\n";
3575
3576 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3577}
3578
3579static ssize_t
3580trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3581 loff_t *ppos)
3582{
3583 struct trace_option_dentry *topt = filp->private_data;
3584 unsigned long val;
3585 char buf[64];
3586 int ret;
3587
3588 if (cnt >= sizeof(buf))
3589 return -EINVAL;
3590
3591 if (copy_from_user(&buf, ubuf, cnt))
3592 return -EFAULT;
3593
3594 buf[cnt] = 0;
3595
3596 ret = strict_strtoul(buf, 10, &val);
3597 if (ret < 0)
3598 return ret;
3599
3600 ret = 0;
3601 switch (val) {
3602 case 0:
3603 /* do nothing if already cleared */
3604 if (!(topt->flags->val & topt->opt->bit))
3605 break;
3606
3607 mutex_lock(&trace_types_lock);
3608 if (current_trace->set_flag)
3609 ret = current_trace->set_flag(topt->flags->val,
3610 topt->opt->bit, 0);
3611 mutex_unlock(&trace_types_lock);
3612 if (ret)
3613 return ret;
3614 topt->flags->val &= ~topt->opt->bit;
3615 break;
3616 case 1:
3617 /* do nothing if already set */
3618 if (topt->flags->val & topt->opt->bit)
3619 break;
3620
3621 mutex_lock(&trace_types_lock);
3622 if (current_trace->set_flag)
3623 ret = current_trace->set_flag(topt->flags->val,
3624 topt->opt->bit, 1);
3625 mutex_unlock(&trace_types_lock);
3626 if (ret)
3627 return ret;
3628 topt->flags->val |= topt->opt->bit;
3629 break;
3630
3631 default:
3632 return -EINVAL;
3633 }
3634
3635 *ppos += cnt;
3636
3637 return cnt;
3638}
3639
3640
3641static const struct file_operations trace_options_fops = {
3642 .open = tracing_open_generic,
3643 .read = trace_options_read,
3644 .write = trace_options_write,
3645};
3646
a8259075
SR
3647static ssize_t
3648trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3649 loff_t *ppos)
3650{
3651 long index = (long)filp->private_data;
3652 char *buf;
3653
3654 if (trace_flags & (1 << index))
3655 buf = "1\n";
3656 else
3657 buf = "0\n";
3658
3659 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3660}
3661
3662static ssize_t
3663trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3664 loff_t *ppos)
3665{
3666 long index = (long)filp->private_data;
3667 char buf[64];
3668 unsigned long val;
3669 int ret;
3670
3671 if (cnt >= sizeof(buf))
3672 return -EINVAL;
3673
3674 if (copy_from_user(&buf, ubuf, cnt))
3675 return -EFAULT;
3676
3677 buf[cnt] = 0;
3678
3679 ret = strict_strtoul(buf, 10, &val);
3680 if (ret < 0)
3681 return ret;
3682
3683 switch (val) {
3684 case 0:
3685 trace_flags &= ~(1 << index);
3686 break;
3687 case 1:
3688 trace_flags |= 1 << index;
3689 break;
3690
3691 default:
3692 return -EINVAL;
3693 }
3694
3695 *ppos += cnt;
3696
3697 return cnt;
3698}
3699
a8259075
SR
3700static const struct file_operations trace_options_core_fops = {
3701 .open = tracing_open_generic,
3702 .read = trace_options_core_read,
3703 .write = trace_options_core_write,
3704};
3705
3706static struct dentry *trace_options_init_dentry(void)
3707{
3708 struct dentry *d_tracer;
3709 static struct dentry *t_options;
3710
3711 if (t_options)
3712 return t_options;
3713
3714 d_tracer = tracing_init_dentry();
3715 if (!d_tracer)
3716 return NULL;
3717
3718 t_options = debugfs_create_dir("options", d_tracer);
3719 if (!t_options) {
3720 pr_warning("Could not create debugfs directory 'options'\n");
3721 return NULL;
3722 }
3723
3724 return t_options;
3725}
3726
577b785f
SR
3727static void
3728create_trace_option_file(struct trace_option_dentry *topt,
3729 struct tracer_flags *flags,
3730 struct tracer_opt *opt)
3731{
3732 struct dentry *t_options;
3733 struct dentry *entry;
3734
3735 t_options = trace_options_init_dentry();
3736 if (!t_options)
3737 return;
3738
3739 topt->flags = flags;
3740 topt->opt = opt;
3741
3742 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3743 &trace_options_fops);
3744
3745 topt->entry = entry;
3746
3747}
3748
3749static struct trace_option_dentry *
3750create_trace_option_files(struct tracer *tracer)
3751{
3752 struct trace_option_dentry *topts;
3753 struct tracer_flags *flags;
3754 struct tracer_opt *opts;
3755 int cnt;
3756
3757 if (!tracer)
3758 return NULL;
3759
3760 flags = tracer->flags;
3761
3762 if (!flags || !flags->opts)
3763 return NULL;
3764
3765 opts = flags->opts;
3766
3767 for (cnt = 0; opts[cnt].name; cnt++)
3768 ;
3769
0cfe8245 3770 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
577b785f
SR
3771 if (!topts)
3772 return NULL;
3773
3774 for (cnt = 0; opts[cnt].name; cnt++)
3775 create_trace_option_file(&topts[cnt], flags,
3776 &opts[cnt]);
3777
3778 return topts;
3779}
3780
3781static void
3782destroy_trace_option_files(struct trace_option_dentry *topts)
3783{
3784 int cnt;
3785
3786 if (!topts)
3787 return;
3788
3789 for (cnt = 0; topts[cnt].opt; cnt++) {
3790 if (topts[cnt].entry)
3791 debugfs_remove(topts[cnt].entry);
3792 }
3793
3794 kfree(topts);
3795}
3796
a8259075
SR
3797static struct dentry *
3798create_trace_option_core_file(const char *option, long index)
3799{
3800 struct dentry *t_options;
3801 struct dentry *entry;
3802
3803 t_options = trace_options_init_dentry();
3804 if (!t_options)
3805 return NULL;
3806
3807 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3808 &trace_options_core_fops);
3809
3810 return entry;
3811}
3812
3813static __init void create_trace_options_dir(void)
3814{
3815 struct dentry *t_options;
3816 struct dentry *entry;
3817 int i;
3818
3819 t_options = trace_options_init_dentry();
3820 if (!t_options)
3821 return;
3822
3823 for (i = 0; trace_options[i]; i++) {
3824 entry = create_trace_option_core_file(trace_options[i], i);
3825 if (!entry)
3826 pr_warning("Could not create debugfs %s entry\n",
3827 trace_options[i]);
3828 }
3829}
3830
b5ad384e 3831static __init int tracer_init_debugfs(void)
bc0c38d1
SR
3832{
3833 struct dentry *d_tracer;
3834 struct dentry *entry;
b04cc6b1 3835 int cpu;
bc0c38d1
SR
3836
3837 d_tracer = tracing_init_dentry();
3838
3839 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3840 &global_trace, &tracing_ctrl_fops);
3841 if (!entry)
3842 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3843
ee6bce52 3844 entry = debugfs_create_file("trace_options", 0644, d_tracer,
bc0c38d1
SR
3845 NULL, &tracing_iter_fops);
3846 if (!entry)
ee6bce52 3847 pr_warning("Could not create debugfs 'trace_options' entry\n");
bc0c38d1 3848
a8259075
SR
3849 create_trace_options_dir();
3850
c7078de1
IM
3851 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3852 NULL, &tracing_cpumask_fops);
3853 if (!entry)
3854 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3855
bc0c38d1 3856 entry = debugfs_create_file("trace", 0444, d_tracer,
b04cc6b1 3857 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
bc0c38d1
SR
3858 if (!entry)
3859 pr_warning("Could not create debugfs 'trace' entry\n");
3860
3861 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3862 &global_trace, &show_traces_fops);
3863 if (!entry)
98a983aa 3864 pr_warning("Could not create debugfs 'available_tracers' entry\n");
bc0c38d1
SR
3865
3866 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3867 &global_trace, &set_tracer_fops);
3868 if (!entry)
98a983aa 3869 pr_warning("Could not create debugfs 'current_tracer' entry\n");
bc0c38d1
SR
3870
3871 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3872 &tracing_max_latency,
3873 &tracing_max_lat_fops);
3874 if (!entry)
3875 pr_warning("Could not create debugfs "
3876 "'tracing_max_latency' entry\n");
3877
3878 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3879 &tracing_thresh, &tracing_max_lat_fops);
3880 if (!entry)
3881 pr_warning("Could not create debugfs "
98a983aa 3882 "'tracing_thresh' entry\n");
7bd2f24c
IM
3883 entry = debugfs_create_file("README", 0644, d_tracer,
3884 NULL, &tracing_readme_fops);
3885 if (!entry)
3886 pr_warning("Could not create debugfs 'README' entry\n");
3887
b04cc6b1
FW
3888 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3889 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
b3806b43
SR
3890 if (!entry)
3891 pr_warning("Could not create debugfs "
98a983aa 3892 "'trace_pipe' entry\n");
bc0c38d1 3893
a94c80e7 3894 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
a98a3c3f
SR
3895 &global_trace, &tracing_entries_fops);
3896 if (!entry)
3897 pr_warning("Could not create debugfs "
a94c80e7 3898 "'buffer_size_kb' entry\n");
a98a3c3f 3899
5bf9a1ee
PP
3900 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3901 NULL, &tracing_mark_fops);
3902 if (!entry)
3903 pr_warning("Could not create debugfs "
3904 "'trace_marker' entry\n");
3905
bc0c38d1
SR
3906#ifdef CONFIG_DYNAMIC_FTRACE
3907 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3908 &ftrace_update_tot_cnt,
b807c3d0 3909 &tracing_dyn_info_fops);
bc0c38d1
SR
3910 if (!entry)
3911 pr_warning("Could not create debugfs "
3912 "'dyn_ftrace_total_info' entry\n");
3913#endif
d618b3e6
IM
3914#ifdef CONFIG_SYSPROF_TRACER
3915 init_tracer_sysprof_debugfs(d_tracer);
3916#endif
b04cc6b1
FW
3917
3918 for_each_tracing_cpu(cpu)
3919 tracing_init_debugfs_percpu(cpu);
3920
b5ad384e 3921 return 0;
bc0c38d1
SR
3922}
3923
3f5a54e3
SR
3924static int trace_panic_handler(struct notifier_block *this,
3925 unsigned long event, void *unused)
3926{
944ac425
SR
3927 if (ftrace_dump_on_oops)
3928 ftrace_dump();
3f5a54e3
SR
3929 return NOTIFY_OK;
3930}
3931
3932static struct notifier_block trace_panic_notifier = {
3933 .notifier_call = trace_panic_handler,
3934 .next = NULL,
3935 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3936};
3937
3938static int trace_die_handler(struct notifier_block *self,
3939 unsigned long val,
3940 void *data)
3941{
3942 switch (val) {
3943 case DIE_OOPS:
944ac425
SR
3944 if (ftrace_dump_on_oops)
3945 ftrace_dump();
3f5a54e3
SR
3946 break;
3947 default:
3948 break;
3949 }
3950 return NOTIFY_OK;
3951}
3952
3953static struct notifier_block trace_die_notifier = {
3954 .notifier_call = trace_die_handler,
3955 .priority = 200
3956};
3957
3958/*
3959 * printk is set to max of 1024, we really don't need it that big.
3960 * Nothing should be printing 1000 characters anyway.
3961 */
3962#define TRACE_MAX_PRINT 1000
3963
3964/*
3965 * Define here KERN_TRACE so that we have one place to modify
3966 * it if we decide to change what log level the ftrace dump
3967 * should be at.
3968 */
428aee14 3969#define KERN_TRACE KERN_EMERG
3f5a54e3
SR
3970
3971static void
3972trace_printk_seq(struct trace_seq *s)
3973{
3974 /* Probably should print a warning here. */
3975 if (s->len >= 1000)
3976 s->len = 1000;
3977
3978 /* should be zero ended, but we are paranoid. */
3979 s->buffer[s->len] = 0;
3980
3981 printk(KERN_TRACE "%s", s->buffer);
3982
f9520750 3983 trace_seq_init(s);
3f5a54e3
SR
3984}
3985
3f5a54e3
SR
3986void ftrace_dump(void)
3987{
3988 static DEFINE_SPINLOCK(ftrace_dump_lock);
3989 /* use static because iter can be a bit big for the stack */
3990 static struct trace_iterator iter;
3f5a54e3 3991 static int dump_ran;
d769041f
SR
3992 unsigned long flags;
3993 int cnt = 0, cpu;
3f5a54e3
SR
3994
3995 /* only one dump */
3996 spin_lock_irqsave(&ftrace_dump_lock, flags);
3997 if (dump_ran)
3998 goto out;
3999
4000 dump_ran = 1;
4001
4002 /* No turning back! */
0ee6b6cf 4003 tracing_off();
81adbdc0 4004 ftrace_kill();
3f5a54e3 4005
d769041f
SR
4006 for_each_tracing_cpu(cpu) {
4007 atomic_inc(&global_trace.data[cpu]->disabled);
4008 }
4009
b54d3de9
TE
4010 /* don't look at user memory in panic mode */
4011 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4012
3f5a54e3
SR
4013 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4014
e543ad76 4015 /* Simulate the iterator */
3f5a54e3
SR
4016 iter.tr = &global_trace;
4017 iter.trace = current_trace;
e543ad76 4018 iter.cpu_file = TRACE_PIPE_ALL_CPU;
3f5a54e3
SR
4019
4020 /*
4021 * We need to stop all tracing on all CPUS to read the
4022 * the next buffer. This is a bit expensive, but is
4023 * not done often. We fill all what we can read,
4024 * and then release the locks again.
4025 */
4026
3f5a54e3
SR
4027 while (!trace_empty(&iter)) {
4028
4029 if (!cnt)
4030 printk(KERN_TRACE "---------------------------------\n");
4031
4032 cnt++;
4033
4034 /* reset all but tr, trace, and overruns */
4035 memset(&iter.seq, 0,
4036 sizeof(struct trace_iterator) -
4037 offsetof(struct trace_iterator, seq));
4038 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4039 iter.pos = -1;
4040
4041 if (find_next_entry_inc(&iter) != NULL) {
4042 print_trace_line(&iter);
4043 trace_consume(&iter);
4044 }
4045
4046 trace_printk_seq(&iter.seq);
4047 }
4048
4049 if (!cnt)
4050 printk(KERN_TRACE " (ftrace buffer empty)\n");
4051 else
4052 printk(KERN_TRACE "---------------------------------\n");
4053
3f5a54e3
SR
4054 out:
4055 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
4056}
4057
3928a8a2 4058__init static int tracer_alloc_buffers(void)
bc0c38d1 4059{
4c11d7ae 4060 struct trace_array_cpu *data;
73c5162a 4061 int ring_buf_size;
4c11d7ae 4062 int i;
9e01c1b7 4063 int ret = -ENOMEM;
4c11d7ae 4064
9e01c1b7
RR
4065 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4066 goto out;
4067
4068 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4069 goto out_free_buffer_mask;
4c11d7ae 4070
b04cc6b1
FW
4071 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4072 goto out_free_tracing_cpumask;
4073
73c5162a
SR
4074 /* To save memory, keep the ring buffer size to its minimum */
4075 if (ring_buffer_expanded)
4076 ring_buf_size = trace_buf_size;
4077 else
4078 ring_buf_size = 1;
4079
9e01c1b7
RR
4080 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4081 cpumask_copy(tracing_cpumask, cpu_all_mask);
b04cc6b1 4082 cpumask_clear(tracing_reader_cpumask);
9e01c1b7
RR
4083
4084 /* TODO: make the number of buffers hot pluggable with CPUS */
73c5162a 4085 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
3928a8a2
SR
4086 TRACE_BUFFER_FLAGS);
4087 if (!global_trace.buffer) {
4088 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4089 WARN_ON(1);
9e01c1b7 4090 goto out_free_cpumask;
4c11d7ae 4091 }
3928a8a2 4092 global_trace.entries = ring_buffer_size(global_trace.buffer);
4c11d7ae 4093
9e01c1b7 4094
4c11d7ae 4095#ifdef CONFIG_TRACER_MAX_TRACE
73c5162a 4096 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
3928a8a2
SR
4097 TRACE_BUFFER_FLAGS);
4098 if (!max_tr.buffer) {
4099 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4100 WARN_ON(1);
4101 ring_buffer_free(global_trace.buffer);
9e01c1b7 4102 goto out_free_cpumask;
4c11d7ae 4103 }
3928a8a2
SR
4104 max_tr.entries = ring_buffer_size(max_tr.buffer);
4105 WARN_ON(max_tr.entries != global_trace.entries);
a98a3c3f 4106#endif
ab46428c 4107
4c11d7ae 4108 /* Allocate the first page for all buffers */
ab46428c 4109 for_each_tracing_cpu(i) {
4c11d7ae 4110 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
bc0c38d1 4111 max_tr.data[i] = &per_cpu(max_data, i);
4c11d7ae 4112 }
bc0c38d1 4113
bc0c38d1
SR
4114 trace_init_cmdlines();
4115
43a15386 4116 register_tracer(&nop_trace);
79fb0768 4117 current_trace = &nop_trace;
b5ad384e
FW
4118#ifdef CONFIG_BOOT_TRACER
4119 register_tracer(&boot_tracer);
b5ad384e 4120#endif
60a11774
SR
4121 /* All seems OK, enable tracing */
4122 tracing_disabled = 0;
3928a8a2 4123
3f5a54e3
SR
4124 atomic_notifier_chain_register(&panic_notifier_list,
4125 &trace_panic_notifier);
4126
4127 register_die_notifier(&trace_die_notifier);
9e01c1b7 4128 ret = 0;
3f5a54e3 4129
9e01c1b7 4130out_free_cpumask:
b04cc6b1
FW
4131 free_cpumask_var(tracing_reader_cpumask);
4132out_free_tracing_cpumask:
9e01c1b7
RR
4133 free_cpumask_var(tracing_cpumask);
4134out_free_buffer_mask:
4135 free_cpumask_var(tracing_buffer_mask);
4136out:
4137 return ret;
bc0c38d1 4138}
b2821ae6
SR
4139
4140__init static int clear_boot_tracer(void)
4141{
4142 /*
4143 * The default tracer at boot buffer is an init section.
4144 * This function is called in lateinit. If we did not
4145 * find the boot tracer, then clear it out, to prevent
4146 * later registration from accessing the buffer that is
4147 * about to be freed.
4148 */
4149 if (!default_bootup_tracer)
4150 return 0;
4151
4152 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4153 default_bootup_tracer);
4154 default_bootup_tracer = NULL;
4155
4156 return 0;
4157}
4158
b5ad384e
FW
4159early_initcall(tracer_alloc_buffers);
4160fs_initcall(tracer_init_debugfs);
b2821ae6 4161late_initcall(clear_boot_tracer);