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