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