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