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