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