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