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