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