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