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