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