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