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