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