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