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