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