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