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