]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/trace/trace.c
tracing: Add trace_dump_stack()
[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
03889384
SR
1154/**
1155 * trace_dump_stack - record a stack back trace in the trace buffer
1156 */
1157void trace_dump_stack(void)
1158{
1159 unsigned long flags;
1160
1161 if (tracing_disabled || tracing_selftest_running)
1162 return 0;
1163
1164 local_save_flags(flags);
1165
1166 /* skipping 3 traces, seems to get us at the caller of this function */
1167 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
1168}
1169
e77405ad
SR
1170void
1171ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
02b67518 1172{
e1112b4d 1173 struct ftrace_event_call *call = &event_user_stack;
8d7c6a96 1174 struct ring_buffer_event *event;
02b67518
TE
1175 struct userstack_entry *entry;
1176 struct stack_trace trace;
02b67518
TE
1177
1178 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1179 return;
1180
e77405ad 1181 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
51a763dd 1182 sizeof(*entry), flags, pc);
02b67518
TE
1183 if (!event)
1184 return;
1185 entry = ring_buffer_event_data(event);
02b67518 1186
48659d31 1187 entry->tgid = current->tgid;
02b67518
TE
1188 memset(&entry->caller, 0, sizeof(entry->caller));
1189
1190 trace.nr_entries = 0;
1191 trace.max_entries = FTRACE_STACK_ENTRIES;
1192 trace.skip = 0;
1193 trace.entries = entry->caller;
1194
1195 save_stack_trace_user(&trace);
e77405ad
SR
1196 if (!filter_check_discard(call, entry, buffer, event))
1197 ring_buffer_unlock_commit(buffer, event);
02b67518
TE
1198}
1199
4fd27358
HE
1200#ifdef UNUSED
1201static void __trace_userstack(struct trace_array *tr, unsigned long flags)
02b67518 1202{
7be42151 1203 ftrace_trace_userstack(tr, flags, preempt_count());
02b67518 1204}
4fd27358 1205#endif /* UNUSED */
02b67518 1206
c0a0d0d3
FW
1207#endif /* CONFIG_STACKTRACE */
1208
38697053 1209static void
7be42151 1210ftrace_trace_special(void *__tr,
38697053
SR
1211 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1212 int pc)
a4feb834 1213{
60ba7702 1214 struct ftrace_event_call *call = &event_special;
3928a8a2 1215 struct ring_buffer_event *event;
a4feb834 1216 struct trace_array *tr = __tr;
e77405ad 1217 struct ring_buffer *buffer = tr->buffer;
777e208d 1218 struct special_entry *entry;
a4feb834 1219
e77405ad 1220 event = trace_buffer_lock_reserve(buffer, TRACE_SPECIAL,
51a763dd 1221 sizeof(*entry), 0, pc);
3928a8a2
SR
1222 if (!event)
1223 return;
1224 entry = ring_buffer_event_data(event);
777e208d
SR
1225 entry->arg1 = arg1;
1226 entry->arg2 = arg2;
1227 entry->arg3 = arg3;
60ba7702
SR
1228
1229 if (!filter_check_discard(call, entry, buffer, event))
1230 trace_buffer_unlock_commit(buffer, event, 0, pc);
a4feb834
IM
1231}
1232
38697053
SR
1233void
1234__trace_special(void *__tr, void *__data,
1235 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1236{
7be42151 1237 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
38697053
SR
1238}
1239
4902f884
SR
1240void
1241ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1242{
1243 struct trace_array *tr = &global_trace;
1244 struct trace_array_cpu *data;
5aa1ba6a 1245 unsigned long flags;
4902f884 1246 int cpu;
38697053 1247 int pc;
4902f884 1248
c76f0694 1249 if (tracing_disabled)
4902f884
SR
1250 return;
1251
38697053 1252 pc = preempt_count();
5aa1ba6a 1253 local_irq_save(flags);
4902f884
SR
1254 cpu = raw_smp_processor_id();
1255 data = tr->data[cpu];
4902f884 1256
5aa1ba6a 1257 if (likely(atomic_inc_return(&data->disabled) == 1))
7be42151 1258 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
4902f884 1259
5aa1ba6a
SR
1260 atomic_dec(&data->disabled);
1261 local_irq_restore(flags);
4902f884
SR
1262}
1263
769b0441 1264/**
48ead020 1265 * trace_vbprintk - write binary msg to tracing buffer
769b0441
FW
1266 *
1267 */
40ce74f1 1268int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
769b0441 1269{
80370cb7
SR
1270 static raw_spinlock_t trace_buf_lock =
1271 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
769b0441
FW
1272 static u32 trace_buf[TRACE_BUF_SIZE];
1273
e1112b4d 1274 struct ftrace_event_call *call = &event_bprint;
769b0441 1275 struct ring_buffer_event *event;
e77405ad 1276 struct ring_buffer *buffer;
769b0441
FW
1277 struct trace_array *tr = &global_trace;
1278 struct trace_array_cpu *data;
48ead020 1279 struct bprint_entry *entry;
769b0441 1280 unsigned long flags;
3189cdb3 1281 int disable;
769b0441
FW
1282 int resched;
1283 int cpu, len = 0, size, pc;
1284
1285 if (unlikely(tracing_selftest_running || tracing_disabled))
1286 return 0;
1287
1288 /* Don't pollute graph traces with trace_vprintk internals */
1289 pause_graph_tracing();
1290
1291 pc = preempt_count();
1292 resched = ftrace_preempt_disable();
1293 cpu = raw_smp_processor_id();
1294 data = tr->data[cpu];
1295
3189cdb3
SR
1296 disable = atomic_inc_return(&data->disabled);
1297 if (unlikely(disable != 1))
769b0441
FW
1298 goto out;
1299
80370cb7
SR
1300 /* Lockdep uses trace_printk for lock tracing */
1301 local_irq_save(flags);
1302 __raw_spin_lock(&trace_buf_lock);
769b0441
FW
1303 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1304
1305 if (len > TRACE_BUF_SIZE || len < 0)
1306 goto out_unlock;
1307
1308 size = sizeof(*entry) + sizeof(u32) * len;
e77405ad
SR
1309 buffer = tr->buffer;
1310 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1311 flags, pc);
769b0441
FW
1312 if (!event)
1313 goto out_unlock;
1314 entry = ring_buffer_event_data(event);
1315 entry->ip = ip;
769b0441
FW
1316 entry->fmt = fmt;
1317
1318 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
e77405ad
SR
1319 if (!filter_check_discard(call, entry, buffer, event))
1320 ring_buffer_unlock_commit(buffer, event);
769b0441
FW
1321
1322out_unlock:
80370cb7
SR
1323 __raw_spin_unlock(&trace_buf_lock);
1324 local_irq_restore(flags);
769b0441
FW
1325
1326out:
3189cdb3 1327 atomic_dec_return(&data->disabled);
769b0441
FW
1328 ftrace_preempt_enable(resched);
1329 unpause_graph_tracing();
1330
1331 return len;
1332}
48ead020
FW
1333EXPORT_SYMBOL_GPL(trace_vbprintk);
1334
659372d3
SR
1335int trace_array_printk(struct trace_array *tr,
1336 unsigned long ip, const char *fmt, ...)
1337{
1338 int ret;
1339 va_list ap;
1340
1341 if (!(trace_flags & TRACE_ITER_PRINTK))
1342 return 0;
1343
1344 va_start(ap, fmt);
1345 ret = trace_array_vprintk(tr, ip, fmt, ap);
1346 va_end(ap);
1347 return ret;
1348}
1349
1350int trace_array_vprintk(struct trace_array *tr,
1351 unsigned long ip, const char *fmt, va_list args)
48ead020
FW
1352{
1353 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1354 static char trace_buf[TRACE_BUF_SIZE];
1355
e1112b4d 1356 struct ftrace_event_call *call = &event_print;
48ead020 1357 struct ring_buffer_event *event;
e77405ad 1358 struct ring_buffer *buffer;
48ead020
FW
1359 struct trace_array_cpu *data;
1360 int cpu, len = 0, size, pc;
1361 struct print_entry *entry;
1362 unsigned long irq_flags;
3189cdb3 1363 int disable;
48ead020
FW
1364
1365 if (tracing_disabled || tracing_selftest_running)
1366 return 0;
1367
1368 pc = preempt_count();
1369 preempt_disable_notrace();
1370 cpu = raw_smp_processor_id();
1371 data = tr->data[cpu];
1372
3189cdb3
SR
1373 disable = atomic_inc_return(&data->disabled);
1374 if (unlikely(disable != 1))
48ead020
FW
1375 goto out;
1376
1377 pause_graph_tracing();
1378 raw_local_irq_save(irq_flags);
1379 __raw_spin_lock(&trace_buf_lock);
f2942487 1380 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
48ead020
FW
1381
1382 size = sizeof(*entry) + len + 1;
e77405ad
SR
1383 buffer = tr->buffer;
1384 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1385 irq_flags, pc);
48ead020
FW
1386 if (!event)
1387 goto out_unlock;
1388 entry = ring_buffer_event_data(event);
c13d2f7c 1389 entry->ip = ip;
48ead020
FW
1390
1391 memcpy(&entry->buf, trace_buf, len);
c13d2f7c 1392 entry->buf[len] = '\0';
e77405ad
SR
1393 if (!filter_check_discard(call, entry, buffer, event))
1394 ring_buffer_unlock_commit(buffer, event);
48ead020
FW
1395
1396 out_unlock:
1397 __raw_spin_unlock(&trace_buf_lock);
1398 raw_local_irq_restore(irq_flags);
1399 unpause_graph_tracing();
1400 out:
3189cdb3 1401 atomic_dec_return(&data->disabled);
48ead020
FW
1402 preempt_enable_notrace();
1403
1404 return len;
1405}
659372d3
SR
1406
1407int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1408{
a813a159 1409 return trace_array_vprintk(&global_trace, ip, fmt, args);
659372d3 1410}
769b0441
FW
1411EXPORT_SYMBOL_GPL(trace_vprintk);
1412
bc0c38d1
SR
1413enum trace_file_type {
1414 TRACE_FILE_LAT_FMT = 1,
12ef7d44 1415 TRACE_FILE_ANNOTATE = 2,
bc0c38d1
SR
1416};
1417
e2ac8ef5 1418static void trace_iterator_increment(struct trace_iterator *iter)
5a90f577 1419{
d769041f
SR
1420 /* Don't allow ftrace to trace into the ring buffers */
1421 ftrace_disable_cpu();
1422
5a90f577 1423 iter->idx++;
d769041f
SR
1424 if (iter->buffer_iter[iter->cpu])
1425 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1426
1427 ftrace_enable_cpu();
5a90f577
SR
1428}
1429
e309b41d 1430static struct trace_entry *
3928a8a2 1431peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
dd0e545f 1432{
3928a8a2
SR
1433 struct ring_buffer_event *event;
1434 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
dd0e545f 1435
d769041f
SR
1436 /* Don't allow ftrace to trace into the ring buffers */
1437 ftrace_disable_cpu();
1438
1439 if (buf_iter)
1440 event = ring_buffer_iter_peek(buf_iter, ts);
1441 else
1442 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1443
1444 ftrace_enable_cpu();
1445
3928a8a2 1446 return event ? ring_buffer_event_data(event) : NULL;
dd0e545f 1447}
d769041f 1448
dd0e545f 1449static struct trace_entry *
3928a8a2 1450__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
bc0c38d1 1451{
3928a8a2 1452 struct ring_buffer *buffer = iter->tr->buffer;
bc0c38d1 1453 struct trace_entry *ent, *next = NULL;
b04cc6b1 1454 int cpu_file = iter->cpu_file;
3928a8a2 1455 u64 next_ts = 0, ts;
bc0c38d1
SR
1456 int next_cpu = -1;
1457 int cpu;
1458
b04cc6b1
FW
1459 /*
1460 * If we are in a per_cpu trace file, don't bother by iterating over
1461 * all cpu and peek directly.
1462 */
1463 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1464 if (ring_buffer_empty_cpu(buffer, cpu_file))
1465 return NULL;
1466 ent = peek_next_entry(iter, cpu_file, ent_ts);
1467 if (ent_cpu)
1468 *ent_cpu = cpu_file;
1469
1470 return ent;
1471 }
1472
ab46428c 1473 for_each_tracing_cpu(cpu) {
dd0e545f 1474
3928a8a2
SR
1475 if (ring_buffer_empty_cpu(buffer, cpu))
1476 continue;
dd0e545f 1477
3928a8a2 1478 ent = peek_next_entry(iter, cpu, &ts);
dd0e545f 1479
cdd31cd2
IM
1480 /*
1481 * Pick the entry with the smallest timestamp:
1482 */
3928a8a2 1483 if (ent && (!next || ts < next_ts)) {
bc0c38d1
SR
1484 next = ent;
1485 next_cpu = cpu;
3928a8a2 1486 next_ts = ts;
bc0c38d1
SR
1487 }
1488 }
1489
1490 if (ent_cpu)
1491 *ent_cpu = next_cpu;
1492
3928a8a2
SR
1493 if (ent_ts)
1494 *ent_ts = next_ts;
1495
bc0c38d1
SR
1496 return next;
1497}
1498
dd0e545f 1499/* Find the next real entry, without updating the iterator itself */
c4a8e8be
FW
1500struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1501 int *ent_cpu, u64 *ent_ts)
bc0c38d1 1502{
3928a8a2 1503 return __find_next_entry(iter, ent_cpu, ent_ts);
dd0e545f
SR
1504}
1505
1506/* Find the next real entry, and increment the iterator to the next entry */
1507static void *find_next_entry_inc(struct trace_iterator *iter)
1508{
3928a8a2 1509 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
dd0e545f 1510
3928a8a2 1511 if (iter->ent)
e2ac8ef5 1512 trace_iterator_increment(iter);
dd0e545f 1513
3928a8a2 1514 return iter->ent ? iter : NULL;
b3806b43 1515}
bc0c38d1 1516
e309b41d 1517static void trace_consume(struct trace_iterator *iter)
b3806b43 1518{
d769041f
SR
1519 /* Don't allow ftrace to trace into the ring buffers */
1520 ftrace_disable_cpu();
3928a8a2 1521 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
d769041f 1522 ftrace_enable_cpu();
bc0c38d1
SR
1523}
1524
e309b41d 1525static void *s_next(struct seq_file *m, void *v, loff_t *pos)
bc0c38d1
SR
1526{
1527 struct trace_iterator *iter = m->private;
bc0c38d1 1528 int i = (int)*pos;
4e3c3333 1529 void *ent;
bc0c38d1 1530
a63ce5b3
SR
1531 WARN_ON_ONCE(iter->leftover);
1532
bc0c38d1
SR
1533 (*pos)++;
1534
1535 /* can't go backwards */
1536 if (iter->idx > i)
1537 return NULL;
1538
1539 if (iter->idx < 0)
1540 ent = find_next_entry_inc(iter);
1541 else
1542 ent = iter;
1543
1544 while (ent && iter->idx < i)
1545 ent = find_next_entry_inc(iter);
1546
1547 iter->pos = *pos;
1548
bc0c38d1
SR
1549 return ent;
1550}
1551
2f26ebd5
SR
1552static void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1553{
1554 struct trace_array *tr = iter->tr;
1555 struct ring_buffer_event *event;
1556 struct ring_buffer_iter *buf_iter;
1557 unsigned long entries = 0;
1558 u64 ts;
1559
1560 tr->data[cpu]->skipped_entries = 0;
1561
1562 if (!iter->buffer_iter[cpu])
1563 return;
1564
1565 buf_iter = iter->buffer_iter[cpu];
1566 ring_buffer_iter_reset(buf_iter);
1567
1568 /*
1569 * We could have the case with the max latency tracers
1570 * that a reset never took place on a cpu. This is evident
1571 * by the timestamp being before the start of the buffer.
1572 */
1573 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1574 if (ts >= iter->tr->time_start)
1575 break;
1576 entries++;
1577 ring_buffer_read(buf_iter, NULL);
1578 }
1579
1580 tr->data[cpu]->skipped_entries = entries;
1581}
1582
d7350c3f
FW
1583/*
1584 * No necessary locking here. The worst thing which can
1585 * happen is loosing events consumed at the same time
1586 * by a trace_pipe reader.
1587 * Other than that, we don't risk to crash the ring buffer
1588 * because it serializes the readers.
1589 *
1590 * The current tracer is copied to avoid a global locking
1591 * all around.
1592 */
bc0c38d1
SR
1593static void *s_start(struct seq_file *m, loff_t *pos)
1594{
1595 struct trace_iterator *iter = m->private;
d7350c3f 1596 static struct tracer *old_tracer;
b04cc6b1 1597 int cpu_file = iter->cpu_file;
bc0c38d1
SR
1598 void *p = NULL;
1599 loff_t l = 0;
3928a8a2 1600 int cpu;
bc0c38d1 1601
d7350c3f 1602 /* copy the tracer to avoid using a global lock all around */
bc0c38d1 1603 mutex_lock(&trace_types_lock);
d7350c3f
FW
1604 if (unlikely(old_tracer != current_trace && current_trace)) {
1605 old_tracer = current_trace;
1606 *iter->trace = *current_trace;
d15f57f2 1607 }
d7350c3f 1608 mutex_unlock(&trace_types_lock);
bc0c38d1
SR
1609
1610 atomic_inc(&trace_record_cmdline_disabled);
1611
bc0c38d1
SR
1612 if (*pos != iter->pos) {
1613 iter->ent = NULL;
1614 iter->cpu = 0;
1615 iter->idx = -1;
1616
d769041f
SR
1617 ftrace_disable_cpu();
1618
b04cc6b1
FW
1619 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1620 for_each_tracing_cpu(cpu)
2f26ebd5 1621 tracing_iter_reset(iter, cpu);
b04cc6b1 1622 } else
2f26ebd5 1623 tracing_iter_reset(iter, cpu_file);
bc0c38d1 1624
d769041f
SR
1625 ftrace_enable_cpu();
1626
bc0c38d1
SR
1627 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1628 ;
1629
1630 } else {
a63ce5b3
SR
1631 /*
1632 * If we overflowed the seq_file before, then we want
1633 * to just reuse the trace_seq buffer again.
1634 */
1635 if (iter->leftover)
1636 p = iter;
1637 else {
1638 l = *pos - 1;
1639 p = s_next(m, p, &l);
1640 }
bc0c38d1
SR
1641 }
1642
4f535968 1643 trace_event_read_lock();
bc0c38d1
SR
1644 return p;
1645}
1646
1647static void s_stop(struct seq_file *m, void *p)
1648{
bc0c38d1 1649 atomic_dec(&trace_record_cmdline_disabled);
4f535968 1650 trace_event_read_unlock();
bc0c38d1
SR
1651}
1652
e309b41d 1653static void print_lat_help_header(struct seq_file *m)
bc0c38d1 1654{
a6168353
ME
1655 seq_puts(m, "# _------=> CPU# \n");
1656 seq_puts(m, "# / _-----=> irqs-off \n");
1657 seq_puts(m, "# | / _----=> need-resched \n");
1658 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1659 seq_puts(m, "# ||| / _--=> preempt-depth \n");
637e7e86
SR
1660 seq_puts(m, "# |||| /_--=> lock-depth \n");
1661 seq_puts(m, "# |||||/ delay \n");
1662 seq_puts(m, "# cmd pid |||||| time | caller \n");
1663 seq_puts(m, "# \\ / |||||| \\ | / \n");
bc0c38d1
SR
1664}
1665
e309b41d 1666static void print_func_help_header(struct seq_file *m)
bc0c38d1 1667{
a6168353
ME
1668 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1669 seq_puts(m, "# | | | | |\n");
bc0c38d1
SR
1670}
1671
1672
e309b41d 1673static void
bc0c38d1
SR
1674print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1675{
1676 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1677 struct trace_array *tr = iter->tr;
1678 struct trace_array_cpu *data = tr->data[tr->cpu];
1679 struct tracer *type = current_trace;
2f26ebd5
SR
1680 unsigned long entries = 0;
1681 unsigned long total = 0;
1682 unsigned long count;
bc0c38d1 1683 const char *name = "preemption";
2f26ebd5 1684 int cpu;
bc0c38d1
SR
1685
1686 if (type)
1687 name = type->name;
1688
2f26ebd5
SR
1689
1690 for_each_tracing_cpu(cpu) {
1691 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1692 /*
1693 * If this buffer has skipped entries, then we hold all
1694 * entries for the trace and we need to ignore the
1695 * ones before the time stamp.
1696 */
1697 if (tr->data[cpu]->skipped_entries) {
1698 count -= tr->data[cpu]->skipped_entries;
1699 /* total is the same as the entries */
1700 total += count;
1701 } else
1702 total += count +
1703 ring_buffer_overrun_cpu(tr->buffer, cpu);
1704 entries += count;
1705 }
bc0c38d1 1706
888b55dc 1707 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
bc0c38d1 1708 name, UTS_RELEASE);
888b55dc 1709 seq_puts(m, "# -----------------------------------"
bc0c38d1 1710 "---------------------------------\n");
888b55dc 1711 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
bc0c38d1 1712 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
57f50be1 1713 nsecs_to_usecs(data->saved_latency),
bc0c38d1 1714 entries,
4c11d7ae 1715 total,
bc0c38d1
SR
1716 tr->cpu,
1717#if defined(CONFIG_PREEMPT_NONE)
1718 "server",
1719#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1720 "desktop",
b5c21b45 1721#elif defined(CONFIG_PREEMPT)
bc0c38d1
SR
1722 "preempt",
1723#else
1724 "unknown",
1725#endif
1726 /* These are reserved for later use */
1727 0, 0, 0, 0);
1728#ifdef CONFIG_SMP
1729 seq_printf(m, " #P:%d)\n", num_online_cpus());
1730#else
1731 seq_puts(m, ")\n");
1732#endif
888b55dc
KM
1733 seq_puts(m, "# -----------------\n");
1734 seq_printf(m, "# | task: %.16s-%d "
bc0c38d1
SR
1735 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1736 data->comm, data->pid, data->uid, data->nice,
1737 data->policy, data->rt_priority);
888b55dc 1738 seq_puts(m, "# -----------------\n");
bc0c38d1
SR
1739
1740 if (data->critical_start) {
888b55dc 1741 seq_puts(m, "# => started at: ");
214023c3
SR
1742 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1743 trace_print_seq(m, &iter->seq);
888b55dc 1744 seq_puts(m, "\n# => ended at: ");
214023c3
SR
1745 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1746 trace_print_seq(m, &iter->seq);
8248ac05 1747 seq_puts(m, "\n#\n");
bc0c38d1
SR
1748 }
1749
888b55dc 1750 seq_puts(m, "#\n");
bc0c38d1
SR
1751}
1752
a309720c
SR
1753static void test_cpu_buff_start(struct trace_iterator *iter)
1754{
1755 struct trace_seq *s = &iter->seq;
1756
12ef7d44
SR
1757 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1758 return;
1759
1760 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1761 return;
1762
4462344e 1763 if (cpumask_test_cpu(iter->cpu, iter->started))
a309720c
SR
1764 return;
1765
2f26ebd5
SR
1766 if (iter->tr->data[iter->cpu]->skipped_entries)
1767 return;
1768
4462344e 1769 cpumask_set_cpu(iter->cpu, iter->started);
b0dfa978
FW
1770
1771 /* Don't print started cpu buffer for the first entry of the trace */
1772 if (iter->idx > 1)
1773 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1774 iter->cpu);
a309720c
SR
1775}
1776
2c4f035f 1777static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 1778{
214023c3 1779 struct trace_seq *s = &iter->seq;
bc0c38d1 1780 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 1781 struct trace_entry *entry;
f633cef0 1782 struct trace_event *event;
bc0c38d1 1783
4e3c3333 1784 entry = iter->ent;
dd0e545f 1785
a309720c
SR
1786 test_cpu_buff_start(iter);
1787
c4a8e8be 1788 event = ftrace_find_event(entry->type);
bc0c38d1 1789
c4a8e8be 1790 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
27d48be8
SR
1791 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1792 if (!trace_print_lat_context(iter))
1793 goto partial;
1794 } else {
1795 if (!trace_print_context(iter))
1796 goto partial;
1797 }
c4a8e8be 1798 }
bc0c38d1 1799
268ccda0 1800 if (event)
d9793bd8
ACM
1801 return event->trace(iter, sym_flags);
1802
1803 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1804 goto partial;
02b67518 1805
2c4f035f 1806 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
1807partial:
1808 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1
SR
1809}
1810
2c4f035f 1811static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
f9896bf3
IM
1812{
1813 struct trace_seq *s = &iter->seq;
1814 struct trace_entry *entry;
f633cef0 1815 struct trace_event *event;
f9896bf3
IM
1816
1817 entry = iter->ent;
dd0e545f 1818
c4a8e8be 1819 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
d9793bd8
ACM
1820 if (!trace_seq_printf(s, "%d %d %llu ",
1821 entry->pid, iter->cpu, iter->ts))
1822 goto partial;
c4a8e8be 1823 }
f9896bf3 1824
f633cef0 1825 event = ftrace_find_event(entry->type);
268ccda0 1826 if (event)
d9793bd8
ACM
1827 return event->raw(iter, 0);
1828
1829 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1830 goto partial;
777e208d 1831
2c4f035f 1832 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
1833partial:
1834 return TRACE_TYPE_PARTIAL_LINE;
f9896bf3
IM
1835}
1836
2c4f035f 1837static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
5e3ca0ec
IM
1838{
1839 struct trace_seq *s = &iter->seq;
1840 unsigned char newline = '\n';
1841 struct trace_entry *entry;
f633cef0 1842 struct trace_event *event;
5e3ca0ec
IM
1843
1844 entry = iter->ent;
dd0e545f 1845
c4a8e8be
FW
1846 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1847 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1848 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1849 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1850 }
5e3ca0ec 1851
f633cef0 1852 event = ftrace_find_event(entry->type);
268ccda0 1853 if (event) {
ae7462b4 1854 enum print_line_t ret = event->hex(iter, 0);
d9793bd8
ACM
1855 if (ret != TRACE_TYPE_HANDLED)
1856 return ret;
1857 }
7104f300 1858
5e3ca0ec
IM
1859 SEQ_PUT_FIELD_RET(s, newline);
1860
2c4f035f 1861 return TRACE_TYPE_HANDLED;
5e3ca0ec
IM
1862}
1863
2c4f035f 1864static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
cb0f12aa
IM
1865{
1866 struct trace_seq *s = &iter->seq;
1867 struct trace_entry *entry;
f633cef0 1868 struct trace_event *event;
cb0f12aa
IM
1869
1870 entry = iter->ent;
dd0e545f 1871
c4a8e8be
FW
1872 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1873 SEQ_PUT_FIELD_RET(s, entry->pid);
1830b52d 1874 SEQ_PUT_FIELD_RET(s, iter->cpu);
c4a8e8be
FW
1875 SEQ_PUT_FIELD_RET(s, iter->ts);
1876 }
cb0f12aa 1877
f633cef0 1878 event = ftrace_find_event(entry->type);
268ccda0 1879 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
cb0f12aa
IM
1880}
1881
bc0c38d1
SR
1882static int trace_empty(struct trace_iterator *iter)
1883{
bc0c38d1
SR
1884 int cpu;
1885
9aba60fe
SR
1886 /* If we are looking at one CPU buffer, only check that one */
1887 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1888 cpu = iter->cpu_file;
1889 if (iter->buffer_iter[cpu]) {
1890 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1891 return 0;
1892 } else {
1893 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1894 return 0;
1895 }
1896 return 1;
1897 }
1898
ab46428c 1899 for_each_tracing_cpu(cpu) {
d769041f
SR
1900 if (iter->buffer_iter[cpu]) {
1901 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1902 return 0;
1903 } else {
1904 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1905 return 0;
1906 }
bc0c38d1 1907 }
d769041f 1908
797d3712 1909 return 1;
bc0c38d1
SR
1910}
1911
4f535968 1912/* Called with trace_event_read_lock() held. */
2c4f035f 1913static enum print_line_t print_trace_line(struct trace_iterator *iter)
f9896bf3 1914{
2c4f035f
FW
1915 enum print_line_t ret;
1916
1917 if (iter->trace && iter->trace->print_line) {
1918 ret = iter->trace->print_line(iter);
1919 if (ret != TRACE_TYPE_UNHANDLED)
1920 return ret;
1921 }
72829bc3 1922
48ead020
FW
1923 if (iter->ent->type == TRACE_BPRINT &&
1924 trace_flags & TRACE_ITER_PRINTK &&
1925 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 1926 return trace_print_bprintk_msg_only(iter);
48ead020 1927
66896a85
FW
1928 if (iter->ent->type == TRACE_PRINT &&
1929 trace_flags & TRACE_ITER_PRINTK &&
1930 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 1931 return trace_print_printk_msg_only(iter);
66896a85 1932
cb0f12aa
IM
1933 if (trace_flags & TRACE_ITER_BIN)
1934 return print_bin_fmt(iter);
1935
5e3ca0ec
IM
1936 if (trace_flags & TRACE_ITER_HEX)
1937 return print_hex_fmt(iter);
1938
f9896bf3
IM
1939 if (trace_flags & TRACE_ITER_RAW)
1940 return print_raw_fmt(iter);
1941
f9896bf3
IM
1942 return print_trace_fmt(iter);
1943}
1944
bc0c38d1
SR
1945static int s_show(struct seq_file *m, void *v)
1946{
1947 struct trace_iterator *iter = v;
a63ce5b3 1948 int ret;
bc0c38d1
SR
1949
1950 if (iter->ent == NULL) {
1951 if (iter->tr) {
1952 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1953 seq_puts(m, "#\n");
1954 }
8bba1bf5
MM
1955 if (iter->trace && iter->trace->print_header)
1956 iter->trace->print_header(m);
1957 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
bc0c38d1
SR
1958 /* print nothing if the buffers are empty */
1959 if (trace_empty(iter))
1960 return 0;
1961 print_trace_header(m, iter);
1962 if (!(trace_flags & TRACE_ITER_VERBOSE))
1963 print_lat_help_header(m);
1964 } else {
1965 if (!(trace_flags & TRACE_ITER_VERBOSE))
1966 print_func_help_header(m);
1967 }
a63ce5b3
SR
1968 } else if (iter->leftover) {
1969 /*
1970 * If we filled the seq_file buffer earlier, we
1971 * want to just show it now.
1972 */
1973 ret = trace_print_seq(m, &iter->seq);
1974
1975 /* ret should this time be zero, but you never know */
1976 iter->leftover = ret;
1977
bc0c38d1 1978 } else {
f9896bf3 1979 print_trace_line(iter);
a63ce5b3
SR
1980 ret = trace_print_seq(m, &iter->seq);
1981 /*
1982 * If we overflow the seq_file buffer, then it will
1983 * ask us for this data again at start up.
1984 * Use that instead.
1985 * ret is 0 if seq_file write succeeded.
1986 * -1 otherwise.
1987 */
1988 iter->leftover = ret;
bc0c38d1
SR
1989 }
1990
1991 return 0;
1992}
1993
88e9d34c 1994static const struct seq_operations tracer_seq_ops = {
4bf39a94
IM
1995 .start = s_start,
1996 .next = s_next,
1997 .stop = s_stop,
1998 .show = s_show,
bc0c38d1
SR
1999};
2000
e309b41d 2001static struct trace_iterator *
85a2f9b4 2002__tracing_open(struct inode *inode, struct file *file)
bc0c38d1 2003{
b04cc6b1 2004 long cpu_file = (long) inode->i_private;
85a2f9b4 2005 void *fail_ret = ERR_PTR(-ENOMEM);
bc0c38d1 2006 struct trace_iterator *iter;
3928a8a2 2007 struct seq_file *m;
85a2f9b4 2008 int cpu, ret;
bc0c38d1 2009
85a2f9b4
SR
2010 if (tracing_disabled)
2011 return ERR_PTR(-ENODEV);
60a11774 2012
bc0c38d1 2013 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
85a2f9b4
SR
2014 if (!iter)
2015 return ERR_PTR(-ENOMEM);
bc0c38d1 2016
d7350c3f
FW
2017 /*
2018 * We make a copy of the current tracer to avoid concurrent
2019 * changes on it while we are reading.
2020 */
bc0c38d1 2021 mutex_lock(&trace_types_lock);
d7350c3f 2022 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
85a2f9b4 2023 if (!iter->trace)
d7350c3f 2024 goto fail;
85a2f9b4 2025
d7350c3f
FW
2026 if (current_trace)
2027 *iter->trace = *current_trace;
2028
79f55997 2029 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
b0dfa978
FW
2030 goto fail;
2031
bc0c38d1
SR
2032 if (current_trace && current_trace->print_max)
2033 iter->tr = &max_tr;
2034 else
b04cc6b1 2035 iter->tr = &global_trace;
bc0c38d1 2036 iter->pos = -1;
d7350c3f 2037 mutex_init(&iter->mutex);
b04cc6b1 2038 iter->cpu_file = cpu_file;
bc0c38d1 2039
8bba1bf5
MM
2040 /* Notify the tracer early; before we stop tracing. */
2041 if (iter->trace && iter->trace->open)
a93751ca 2042 iter->trace->open(iter);
8bba1bf5 2043
12ef7d44
SR
2044 /* Annotate start of buffers if we had overruns */
2045 if (ring_buffer_overruns(iter->tr->buffer))
2046 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2047
2f26ebd5
SR
2048 /* stop the trace while dumping */
2049 tracing_stop();
2050
b04cc6b1
FW
2051 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2052 for_each_tracing_cpu(cpu) {
12ef7d44 2053
b04cc6b1
FW
2054 iter->buffer_iter[cpu] =
2055 ring_buffer_read_start(iter->tr->buffer, cpu);
2f26ebd5 2056 tracing_iter_reset(iter, cpu);
b04cc6b1
FW
2057 }
2058 } else {
2059 cpu = iter->cpu_file;
3928a8a2 2060 iter->buffer_iter[cpu] =
b04cc6b1 2061 ring_buffer_read_start(iter->tr->buffer, cpu);
2f26ebd5 2062 tracing_iter_reset(iter, cpu);
3928a8a2
SR
2063 }
2064
85a2f9b4
SR
2065 ret = seq_open(file, &tracer_seq_ops);
2066 if (ret < 0) {
2067 fail_ret = ERR_PTR(ret);
3928a8a2 2068 goto fail_buffer;
85a2f9b4 2069 }
bc0c38d1 2070
3928a8a2
SR
2071 m = file->private_data;
2072 m->private = iter;
bc0c38d1 2073
bc0c38d1
SR
2074 mutex_unlock(&trace_types_lock);
2075
bc0c38d1 2076 return iter;
3928a8a2
SR
2077
2078 fail_buffer:
2079 for_each_tracing_cpu(cpu) {
2080 if (iter->buffer_iter[cpu])
2081 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2082 }
b0dfa978 2083 free_cpumask_var(iter->started);
2f26ebd5 2084 tracing_start();
d7350c3f 2085 fail:
3928a8a2 2086 mutex_unlock(&trace_types_lock);
d7350c3f 2087 kfree(iter->trace);
0bb943c7 2088 kfree(iter);
3928a8a2 2089
85a2f9b4 2090 return fail_ret;
bc0c38d1
SR
2091}
2092
2093int tracing_open_generic(struct inode *inode, struct file *filp)
2094{
60a11774
SR
2095 if (tracing_disabled)
2096 return -ENODEV;
2097
bc0c38d1
SR
2098 filp->private_data = inode->i_private;
2099 return 0;
2100}
2101
4fd27358 2102static int tracing_release(struct inode *inode, struct file *file)
bc0c38d1
SR
2103{
2104 struct seq_file *m = (struct seq_file *)file->private_data;
4acd4d00 2105 struct trace_iterator *iter;
3928a8a2 2106 int cpu;
bc0c38d1 2107
4acd4d00
SR
2108 if (!(file->f_mode & FMODE_READ))
2109 return 0;
2110
2111 iter = m->private;
2112
bc0c38d1 2113 mutex_lock(&trace_types_lock);
3928a8a2
SR
2114 for_each_tracing_cpu(cpu) {
2115 if (iter->buffer_iter[cpu])
2116 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2117 }
2118
bc0c38d1
SR
2119 if (iter->trace && iter->trace->close)
2120 iter->trace->close(iter);
2121
2122 /* reenable tracing if it was previously enabled */
9036990d 2123 tracing_start();
bc0c38d1
SR
2124 mutex_unlock(&trace_types_lock);
2125
2126 seq_release(inode, file);
d7350c3f 2127 mutex_destroy(&iter->mutex);
b0dfa978 2128 free_cpumask_var(iter->started);
d7350c3f 2129 kfree(iter->trace);
bc0c38d1
SR
2130 kfree(iter);
2131 return 0;
2132}
2133
2134static int tracing_open(struct inode *inode, struct file *file)
2135{
85a2f9b4
SR
2136 struct trace_iterator *iter;
2137 int ret = 0;
bc0c38d1 2138
4acd4d00
SR
2139 /* If this file was open for write, then erase contents */
2140 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 2141 (file->f_flags & O_TRUNC)) {
4acd4d00 2142 long cpu = (long) inode->i_private;
bc0c38d1 2143
4acd4d00
SR
2144 if (cpu == TRACE_PIPE_ALL_CPU)
2145 tracing_reset_online_cpus(&global_trace);
2146 else
2147 tracing_reset(&global_trace, cpu);
2148 }
bc0c38d1 2149
4acd4d00
SR
2150 if (file->f_mode & FMODE_READ) {
2151 iter = __tracing_open(inode, file);
2152 if (IS_ERR(iter))
2153 ret = PTR_ERR(iter);
2154 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2155 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2156 }
bc0c38d1
SR
2157 return ret;
2158}
2159
e309b41d 2160static void *
bc0c38d1
SR
2161t_next(struct seq_file *m, void *v, loff_t *pos)
2162{
f129e965 2163 struct tracer *t = v;
bc0c38d1
SR
2164
2165 (*pos)++;
2166
2167 if (t)
2168 t = t->next;
2169
bc0c38d1
SR
2170 return t;
2171}
2172
2173static void *t_start(struct seq_file *m, loff_t *pos)
2174{
f129e965 2175 struct tracer *t;
bc0c38d1
SR
2176 loff_t l = 0;
2177
2178 mutex_lock(&trace_types_lock);
f129e965 2179 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
bc0c38d1
SR
2180 ;
2181
2182 return t;
2183}
2184
2185static void t_stop(struct seq_file *m, void *p)
2186{
2187 mutex_unlock(&trace_types_lock);
2188}
2189
2190static int t_show(struct seq_file *m, void *v)
2191{
2192 struct tracer *t = v;
2193
2194 if (!t)
2195 return 0;
2196
2197 seq_printf(m, "%s", t->name);
2198 if (t->next)
2199 seq_putc(m, ' ');
2200 else
2201 seq_putc(m, '\n');
2202
2203 return 0;
2204}
2205
88e9d34c 2206static const struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
2207 .start = t_start,
2208 .next = t_next,
2209 .stop = t_stop,
2210 .show = t_show,
bc0c38d1
SR
2211};
2212
2213static int show_traces_open(struct inode *inode, struct file *file)
2214{
60a11774
SR
2215 if (tracing_disabled)
2216 return -ENODEV;
2217
f129e965 2218 return seq_open(file, &show_traces_seq_ops);
bc0c38d1
SR
2219}
2220
4acd4d00
SR
2221static ssize_t
2222tracing_write_stub(struct file *filp, const char __user *ubuf,
2223 size_t count, loff_t *ppos)
2224{
2225 return count;
2226}
2227
5e2336a0 2228static const struct file_operations tracing_fops = {
4bf39a94
IM
2229 .open = tracing_open,
2230 .read = seq_read,
4acd4d00 2231 .write = tracing_write_stub,
4bf39a94
IM
2232 .llseek = seq_lseek,
2233 .release = tracing_release,
bc0c38d1
SR
2234};
2235
5e2336a0 2236static const struct file_operations show_traces_fops = {
c7078de1
IM
2237 .open = show_traces_open,
2238 .read = seq_read,
2239 .release = seq_release,
2240};
2241
36dfe925
IM
2242/*
2243 * Only trace on a CPU if the bitmask is set:
2244 */
9e01c1b7 2245static cpumask_var_t tracing_cpumask;
36dfe925
IM
2246
2247/*
2248 * The tracer itself will not take this lock, but still we want
2249 * to provide a consistent cpumask to user-space:
2250 */
2251static DEFINE_MUTEX(tracing_cpumask_update_lock);
2252
2253/*
2254 * Temporary storage for the character representation of the
2255 * CPU bitmask (and one more byte for the newline):
2256 */
2257static char mask_str[NR_CPUS + 1];
2258
c7078de1
IM
2259static ssize_t
2260tracing_cpumask_read(struct file *filp, char __user *ubuf,
2261 size_t count, loff_t *ppos)
2262{
36dfe925 2263 int len;
c7078de1
IM
2264
2265 mutex_lock(&tracing_cpumask_update_lock);
36dfe925 2266
9e01c1b7 2267 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
36dfe925
IM
2268 if (count - len < 2) {
2269 count = -EINVAL;
2270 goto out_err;
2271 }
2272 len += sprintf(mask_str + len, "\n");
2273 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2274
2275out_err:
c7078de1
IM
2276 mutex_unlock(&tracing_cpumask_update_lock);
2277
2278 return count;
2279}
2280
2281static ssize_t
2282tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2283 size_t count, loff_t *ppos)
2284{
36dfe925 2285 int err, cpu;
9e01c1b7
RR
2286 cpumask_var_t tracing_cpumask_new;
2287
2288 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2289 return -ENOMEM;
c7078de1 2290
9e01c1b7 2291 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
c7078de1 2292 if (err)
36dfe925
IM
2293 goto err_unlock;
2294
215368e8
LZ
2295 mutex_lock(&tracing_cpumask_update_lock);
2296
a5e25883 2297 local_irq_disable();
92205c23 2298 __raw_spin_lock(&ftrace_max_lock);
ab46428c 2299 for_each_tracing_cpu(cpu) {
36dfe925
IM
2300 /*
2301 * Increase/decrease the disabled counter if we are
2302 * about to flip a bit in the cpumask:
2303 */
9e01c1b7
RR
2304 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2305 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
36dfe925
IM
2306 atomic_inc(&global_trace.data[cpu]->disabled);
2307 }
9e01c1b7
RR
2308 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2309 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
36dfe925
IM
2310 atomic_dec(&global_trace.data[cpu]->disabled);
2311 }
2312 }
92205c23 2313 __raw_spin_unlock(&ftrace_max_lock);
a5e25883 2314 local_irq_enable();
36dfe925 2315
9e01c1b7 2316 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
36dfe925
IM
2317
2318 mutex_unlock(&tracing_cpumask_update_lock);
9e01c1b7 2319 free_cpumask_var(tracing_cpumask_new);
c7078de1
IM
2320
2321 return count;
36dfe925
IM
2322
2323err_unlock:
215368e8 2324 free_cpumask_var(tracing_cpumask_new);
36dfe925
IM
2325
2326 return err;
c7078de1
IM
2327}
2328
5e2336a0 2329static const struct file_operations tracing_cpumask_fops = {
c7078de1
IM
2330 .open = tracing_open_generic,
2331 .read = tracing_cpumask_read,
2332 .write = tracing_cpumask_write,
bc0c38d1
SR
2333};
2334
2335static ssize_t
ee6bce52 2336tracing_trace_options_read(struct file *filp, char __user *ubuf,
bc0c38d1
SR
2337 size_t cnt, loff_t *ppos)
2338{
d8e83d26
SR
2339 struct tracer_opt *trace_opts;
2340 u32 tracer_flags;
2341 int len = 0;
bc0c38d1
SR
2342 char *buf;
2343 int r = 0;
d8e83d26 2344 int i;
adf9f195 2345
bc0c38d1 2346
c3706f00 2347 /* calculate max size */
bc0c38d1
SR
2348 for (i = 0; trace_options[i]; i++) {
2349 len += strlen(trace_options[i]);
5c6a3ae1 2350 len += 3; /* "no" and newline */
bc0c38d1
SR
2351 }
2352
d8e83d26
SR
2353 mutex_lock(&trace_types_lock);
2354 tracer_flags = current_trace->flags->val;
2355 trace_opts = current_trace->flags->opts;
2356
adf9f195
FW
2357 /*
2358 * Increase the size with names of options specific
2359 * of the current tracer.
2360 */
2361 for (i = 0; trace_opts[i].name; i++) {
2362 len += strlen(trace_opts[i].name);
5c6a3ae1 2363 len += 3; /* "no" and newline */
adf9f195
FW
2364 }
2365
ff4e9da2
XG
2366 /* +1 for \0 */
2367 buf = kmalloc(len + 1, GFP_KERNEL);
d8e83d26
SR
2368 if (!buf) {
2369 mutex_unlock(&trace_types_lock);
bc0c38d1 2370 return -ENOMEM;
d8e83d26 2371 }
bc0c38d1
SR
2372
2373 for (i = 0; trace_options[i]; i++) {
2374 if (trace_flags & (1 << i))
5c6a3ae1 2375 r += sprintf(buf + r, "%s\n", trace_options[i]);
bc0c38d1 2376 else
5c6a3ae1 2377 r += sprintf(buf + r, "no%s\n", trace_options[i]);
bc0c38d1
SR
2378 }
2379
adf9f195
FW
2380 for (i = 0; trace_opts[i].name; i++) {
2381 if (tracer_flags & trace_opts[i].bit)
5c6a3ae1 2382 r += sprintf(buf + r, "%s\n",
adf9f195
FW
2383 trace_opts[i].name);
2384 else
5c6a3ae1 2385 r += sprintf(buf + r, "no%s\n",
adf9f195
FW
2386 trace_opts[i].name);
2387 }
d8e83d26 2388 mutex_unlock(&trace_types_lock);
adf9f195 2389
ff4e9da2 2390 WARN_ON(r >= len + 1);
bc0c38d1 2391
36dfe925 2392 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2393
2394 kfree(buf);
bc0c38d1
SR
2395 return r;
2396}
2397
adf9f195
FW
2398/* Try to assign a tracer specific option */
2399static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2400{
7770841e 2401 struct tracer_flags *tracer_flags = trace->flags;
adf9f195
FW
2402 struct tracer_opt *opts = NULL;
2403 int ret = 0, i = 0;
2404 int len;
2405
7770841e
Z
2406 for (i = 0; tracer_flags->opts[i].name; i++) {
2407 opts = &tracer_flags->opts[i];
adf9f195
FW
2408 len = strlen(opts->name);
2409
2410 if (strncmp(cmp, opts->name, len) == 0) {
7770841e 2411 ret = trace->set_flag(tracer_flags->val,
adf9f195
FW
2412 opts->bit, !neg);
2413 break;
2414 }
2415 }
2416 /* Not found */
7770841e 2417 if (!tracer_flags->opts[i].name)
adf9f195
FW
2418 return -EINVAL;
2419
2420 /* Refused to handle */
2421 if (ret)
2422 return ret;
2423
2424 if (neg)
7770841e 2425 tracer_flags->val &= ~opts->bit;
adf9f195 2426 else
7770841e 2427 tracer_flags->val |= opts->bit;
adf9f195
FW
2428
2429 return 0;
2430}
2431
af4617bd
SR
2432static void set_tracer_flags(unsigned int mask, int enabled)
2433{
2434 /* do nothing if flag is already set */
2435 if (!!(trace_flags & mask) == !!enabled)
2436 return;
2437
2438 if (enabled)
2439 trace_flags |= mask;
2440 else
2441 trace_flags &= ~mask;
af4617bd
SR
2442}
2443
bc0c38d1 2444static ssize_t
ee6bce52 2445tracing_trace_options_write(struct file *filp, const char __user *ubuf,
bc0c38d1
SR
2446 size_t cnt, loff_t *ppos)
2447{
2448 char buf[64];
2449 char *cmp = buf;
2450 int neg = 0;
adf9f195 2451 int ret;
bc0c38d1
SR
2452 int i;
2453
cffae437
SR
2454 if (cnt >= sizeof(buf))
2455 return -EINVAL;
bc0c38d1
SR
2456
2457 if (copy_from_user(&buf, ubuf, cnt))
2458 return -EFAULT;
2459
2460 buf[cnt] = 0;
2461
2462 if (strncmp(buf, "no", 2) == 0) {
2463 neg = 1;
2464 cmp += 2;
2465 }
2466
2467 for (i = 0; trace_options[i]; i++) {
2468 int len = strlen(trace_options[i]);
2469
2470 if (strncmp(cmp, trace_options[i], len) == 0) {
af4617bd 2471 set_tracer_flags(1 << i, !neg);
bc0c38d1
SR
2472 break;
2473 }
2474 }
adf9f195
FW
2475
2476 /* If no option could be set, test the specific tracer options */
2477 if (!trace_options[i]) {
d8e83d26 2478 mutex_lock(&trace_types_lock);
adf9f195 2479 ret = set_tracer_option(current_trace, cmp, neg);
d8e83d26 2480 mutex_unlock(&trace_types_lock);
adf9f195
FW
2481 if (ret)
2482 return ret;
2483 }
bc0c38d1 2484
cf8517cf 2485 *ppos += cnt;
bc0c38d1
SR
2486
2487 return cnt;
2488}
2489
5e2336a0 2490static const struct file_operations tracing_iter_fops = {
c7078de1 2491 .open = tracing_open_generic,
ee6bce52
SR
2492 .read = tracing_trace_options_read,
2493 .write = tracing_trace_options_write,
bc0c38d1
SR
2494};
2495
7bd2f24c
IM
2496static const char readme_msg[] =
2497 "tracing mini-HOWTO:\n\n"
156f5a78
GL
2498 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2499 "# cat /sys/kernel/debug/tracing/available_tracers\n"
bc2b6871 2500 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
156f5a78 2501 "# cat /sys/kernel/debug/tracing/current_tracer\n"
bc2b6871 2502 "nop\n"
156f5a78
GL
2503 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2504 "# cat /sys/kernel/debug/tracing/current_tracer\n"
7bd2f24c 2505 "sched_switch\n"
156f5a78 2506 "# cat /sys/kernel/debug/tracing/trace_options\n"
7bd2f24c 2507 "noprint-parent nosym-offset nosym-addr noverbose\n"
156f5a78
GL
2508 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2509 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2510 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2511 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
7bd2f24c
IM
2512;
2513
2514static ssize_t
2515tracing_readme_read(struct file *filp, char __user *ubuf,
2516 size_t cnt, loff_t *ppos)
2517{
2518 return simple_read_from_buffer(ubuf, cnt, ppos,
2519 readme_msg, strlen(readme_msg));
2520}
2521
5e2336a0 2522static const struct file_operations tracing_readme_fops = {
c7078de1
IM
2523 .open = tracing_open_generic,
2524 .read = tracing_readme_read,
7bd2f24c
IM
2525};
2526
69abe6a5
AP
2527static ssize_t
2528tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2529 size_t cnt, loff_t *ppos)
2530{
2531 char *buf_comm;
2532 char *file_buf;
2533 char *buf;
2534 int len = 0;
2535 int pid;
2536 int i;
2537
2538 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2539 if (!file_buf)
2540 return -ENOMEM;
2541
2542 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2543 if (!buf_comm) {
2544 kfree(file_buf);
2545 return -ENOMEM;
2546 }
2547
2548 buf = file_buf;
2549
2550 for (i = 0; i < SAVED_CMDLINES; i++) {
2551 int r;
2552
2553 pid = map_cmdline_to_pid[i];
2554 if (pid == -1 || pid == NO_CMDLINE_MAP)
2555 continue;
2556
2557 trace_find_cmdline(pid, buf_comm);
2558 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2559 buf += r;
2560 len += r;
2561 }
2562
2563 len = simple_read_from_buffer(ubuf, cnt, ppos,
2564 file_buf, len);
2565
2566 kfree(file_buf);
2567 kfree(buf_comm);
2568
2569 return len;
2570}
2571
2572static const struct file_operations tracing_saved_cmdlines_fops = {
2573 .open = tracing_open_generic,
2574 .read = tracing_saved_cmdlines_read,
2575};
2576
bc0c38d1
SR
2577static ssize_t
2578tracing_ctrl_read(struct file *filp, char __user *ubuf,
2579 size_t cnt, loff_t *ppos)
2580{
bc0c38d1
SR
2581 char buf[64];
2582 int r;
2583
9036990d 2584 r = sprintf(buf, "%u\n", tracer_enabled);
4e3c3333 2585 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2586}
2587
2588static ssize_t
2589tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2590 size_t cnt, loff_t *ppos)
2591{
2592 struct trace_array *tr = filp->private_data;
bc0c38d1 2593 char buf[64];
5e39841c 2594 unsigned long val;
c6caeeb1 2595 int ret;
bc0c38d1 2596
cffae437
SR
2597 if (cnt >= sizeof(buf))
2598 return -EINVAL;
bc0c38d1
SR
2599
2600 if (copy_from_user(&buf, ubuf, cnt))
2601 return -EFAULT;
2602
2603 buf[cnt] = 0;
2604
c6caeeb1
SR
2605 ret = strict_strtoul(buf, 10, &val);
2606 if (ret < 0)
2607 return ret;
bc0c38d1
SR
2608
2609 val = !!val;
2610
2611 mutex_lock(&trace_types_lock);
9036990d
SR
2612 if (tracer_enabled ^ val) {
2613 if (val) {
bc0c38d1 2614 tracer_enabled = 1;
9036990d
SR
2615 if (current_trace->start)
2616 current_trace->start(tr);
2617 tracing_start();
2618 } else {
bc0c38d1 2619 tracer_enabled = 0;
9036990d
SR
2620 tracing_stop();
2621 if (current_trace->stop)
2622 current_trace->stop(tr);
2623 }
bc0c38d1
SR
2624 }
2625 mutex_unlock(&trace_types_lock);
2626
cf8517cf 2627 *ppos += cnt;
bc0c38d1
SR
2628
2629 return cnt;
2630}
2631
2632static ssize_t
2633tracing_set_trace_read(struct file *filp, char __user *ubuf,
2634 size_t cnt, loff_t *ppos)
2635{
ee6c2c1b 2636 char buf[MAX_TRACER_SIZE+2];
bc0c38d1
SR
2637 int r;
2638
2639 mutex_lock(&trace_types_lock);
2640 if (current_trace)
2641 r = sprintf(buf, "%s\n", current_trace->name);
2642 else
2643 r = sprintf(buf, "\n");
2644 mutex_unlock(&trace_types_lock);
2645
4bf39a94 2646 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2647}
2648
b6f11df2
ACM
2649int tracer_init(struct tracer *t, struct trace_array *tr)
2650{
2651 tracing_reset_online_cpus(tr);
2652 return t->init(tr);
2653}
2654
73c5162a
SR
2655static int tracing_resize_ring_buffer(unsigned long size)
2656{
2657 int ret;
2658
2659 /*
2660 * If kernel or user changes the size of the ring buffer
a123c52b
SR
2661 * we use the size that was given, and we can forget about
2662 * expanding it later.
73c5162a
SR
2663 */
2664 ring_buffer_expanded = 1;
2665
2666 ret = ring_buffer_resize(global_trace.buffer, size);
2667 if (ret < 0)
2668 return ret;
2669
2670 ret = ring_buffer_resize(max_tr.buffer, size);
2671 if (ret < 0) {
2672 int r;
2673
2674 r = ring_buffer_resize(global_trace.buffer,
2675 global_trace.entries);
2676 if (r < 0) {
a123c52b
SR
2677 /*
2678 * AARGH! We are left with different
2679 * size max buffer!!!!
2680 * The max buffer is our "snapshot" buffer.
2681 * When a tracer needs a snapshot (one of the
2682 * latency tracers), it swaps the max buffer
2683 * with the saved snap shot. We succeeded to
2684 * update the size of the main buffer, but failed to
2685 * update the size of the max buffer. But when we tried
2686 * to reset the main buffer to the original size, we
2687 * failed there too. This is very unlikely to
2688 * happen, but if it does, warn and kill all
2689 * tracing.
2690 */
73c5162a
SR
2691 WARN_ON(1);
2692 tracing_disabled = 1;
2693 }
2694 return ret;
2695 }
2696
2697 global_trace.entries = size;
2698
2699 return ret;
2700}
2701
1852fcce
SR
2702/**
2703 * tracing_update_buffers - used by tracing facility to expand ring buffers
2704 *
2705 * To save on memory when the tracing is never used on a system with it
2706 * configured in. The ring buffers are set to a minimum size. But once
2707 * a user starts to use the tracing facility, then they need to grow
2708 * to their default size.
2709 *
2710 * This function is to be called when a tracer is about to be used.
2711 */
2712int tracing_update_buffers(void)
2713{
2714 int ret = 0;
2715
1027fcb2 2716 mutex_lock(&trace_types_lock);
1852fcce
SR
2717 if (!ring_buffer_expanded)
2718 ret = tracing_resize_ring_buffer(trace_buf_size);
1027fcb2 2719 mutex_unlock(&trace_types_lock);
1852fcce
SR
2720
2721 return ret;
2722}
2723
577b785f
SR
2724struct trace_option_dentry;
2725
2726static struct trace_option_dentry *
2727create_trace_option_files(struct tracer *tracer);
2728
2729static void
2730destroy_trace_option_files(struct trace_option_dentry *topts);
2731
b2821ae6 2732static int tracing_set_tracer(const char *buf)
bc0c38d1 2733{
577b785f 2734 static struct trace_option_dentry *topts;
bc0c38d1
SR
2735 struct trace_array *tr = &global_trace;
2736 struct tracer *t;
d9e54076 2737 int ret = 0;
bc0c38d1 2738
1027fcb2
SR
2739 mutex_lock(&trace_types_lock);
2740
73c5162a
SR
2741 if (!ring_buffer_expanded) {
2742 ret = tracing_resize_ring_buffer(trace_buf_size);
2743 if (ret < 0)
59f586db 2744 goto out;
73c5162a
SR
2745 ret = 0;
2746 }
2747
bc0c38d1
SR
2748 for (t = trace_types; t; t = t->next) {
2749 if (strcmp(t->name, buf) == 0)
2750 break;
2751 }
c2931e05
FW
2752 if (!t) {
2753 ret = -EINVAL;
2754 goto out;
2755 }
2756 if (t == current_trace)
bc0c38d1
SR
2757 goto out;
2758
9f029e83 2759 trace_branch_disable();
bc0c38d1
SR
2760 if (current_trace && current_trace->reset)
2761 current_trace->reset(tr);
2762
577b785f
SR
2763 destroy_trace_option_files(topts);
2764
bc0c38d1 2765 current_trace = t;
577b785f
SR
2766
2767 topts = create_trace_option_files(current_trace);
2768
1c80025a 2769 if (t->init) {
b6f11df2 2770 ret = tracer_init(t, tr);
1c80025a
FW
2771 if (ret)
2772 goto out;
2773 }
bc0c38d1 2774
9f029e83 2775 trace_branch_enable(tr);
bc0c38d1
SR
2776 out:
2777 mutex_unlock(&trace_types_lock);
2778
d9e54076
PZ
2779 return ret;
2780}
2781
2782static ssize_t
2783tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2784 size_t cnt, loff_t *ppos)
2785{
ee6c2c1b 2786 char buf[MAX_TRACER_SIZE+1];
d9e54076
PZ
2787 int i;
2788 size_t ret;
e6e7a65a
FW
2789 int err;
2790
2791 ret = cnt;
d9e54076 2792
ee6c2c1b
LZ
2793 if (cnt > MAX_TRACER_SIZE)
2794 cnt = MAX_TRACER_SIZE;
d9e54076
PZ
2795
2796 if (copy_from_user(&buf, ubuf, cnt))
2797 return -EFAULT;
2798
2799 buf[cnt] = 0;
2800
2801 /* strip ending whitespace. */
2802 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2803 buf[i] = 0;
2804
e6e7a65a
FW
2805 err = tracing_set_tracer(buf);
2806 if (err)
2807 return err;
d9e54076 2808
cf8517cf 2809 *ppos += ret;
bc0c38d1 2810
c2931e05 2811 return ret;
bc0c38d1
SR
2812}
2813
2814static ssize_t
2815tracing_max_lat_read(struct file *filp, char __user *ubuf,
2816 size_t cnt, loff_t *ppos)
2817{
2818 unsigned long *ptr = filp->private_data;
2819 char buf[64];
2820 int r;
2821
cffae437 2822 r = snprintf(buf, sizeof(buf), "%ld\n",
bc0c38d1 2823 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
cffae437
SR
2824 if (r > sizeof(buf))
2825 r = sizeof(buf);
4bf39a94 2826 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2827}
2828
2829static ssize_t
2830tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2831 size_t cnt, loff_t *ppos)
2832{
5e39841c 2833 unsigned long *ptr = filp->private_data;
bc0c38d1 2834 char buf[64];
5e39841c 2835 unsigned long val;
c6caeeb1 2836 int ret;
bc0c38d1 2837
cffae437
SR
2838 if (cnt >= sizeof(buf))
2839 return -EINVAL;
bc0c38d1
SR
2840
2841 if (copy_from_user(&buf, ubuf, cnt))
2842 return -EFAULT;
2843
2844 buf[cnt] = 0;
2845
c6caeeb1
SR
2846 ret = strict_strtoul(buf, 10, &val);
2847 if (ret < 0)
2848 return ret;
bc0c38d1
SR
2849
2850 *ptr = val * 1000;
2851
2852 return cnt;
2853}
2854
b3806b43
SR
2855static int tracing_open_pipe(struct inode *inode, struct file *filp)
2856{
b04cc6b1 2857 long cpu_file = (long) inode->i_private;
b3806b43 2858 struct trace_iterator *iter;
b04cc6b1 2859 int ret = 0;
b3806b43
SR
2860
2861 if (tracing_disabled)
2862 return -ENODEV;
2863
b04cc6b1
FW
2864 mutex_lock(&trace_types_lock);
2865
2866 /* We only allow one reader per cpu */
2867 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2868 if (!cpumask_empty(tracing_reader_cpumask)) {
2869 ret = -EBUSY;
2870 goto out;
2871 }
2872 cpumask_setall(tracing_reader_cpumask);
2873 } else {
2874 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2875 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2876 else {
2877 ret = -EBUSY;
2878 goto out;
2879 }
b3806b43
SR
2880 }
2881
2882 /* create a buffer to store the information to pass to userspace */
2883 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
b04cc6b1
FW
2884 if (!iter) {
2885 ret = -ENOMEM;
2886 goto out;
2887 }
b3806b43 2888
d7350c3f
FW
2889 /*
2890 * We make a copy of the current tracer to avoid concurrent
2891 * changes on it while we are reading.
2892 */
2893 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2894 if (!iter->trace) {
2895 ret = -ENOMEM;
2896 goto fail;
2897 }
2898 if (current_trace)
2899 *iter->trace = *current_trace;
2900
4462344e 2901 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
b04cc6b1 2902 ret = -ENOMEM;
d7350c3f 2903 goto fail;
4462344e
RR
2904 }
2905
a309720c 2906 /* trace pipe does not show start of buffer */
4462344e 2907 cpumask_setall(iter->started);
a309720c 2908
112f38a7
SR
2909 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2910 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2911
b04cc6b1 2912 iter->cpu_file = cpu_file;
b3806b43 2913 iter->tr = &global_trace;
d7350c3f 2914 mutex_init(&iter->mutex);
b3806b43
SR
2915 filp->private_data = iter;
2916
107bad8b
SR
2917 if (iter->trace->pipe_open)
2918 iter->trace->pipe_open(iter);
107bad8b 2919
b04cc6b1
FW
2920out:
2921 mutex_unlock(&trace_types_lock);
2922 return ret;
d7350c3f
FW
2923
2924fail:
2925 kfree(iter->trace);
2926 kfree(iter);
2927 mutex_unlock(&trace_types_lock);
2928 return ret;
b3806b43
SR
2929}
2930
2931static int tracing_release_pipe(struct inode *inode, struct file *file)
2932{
2933 struct trace_iterator *iter = file->private_data;
2934
b04cc6b1
FW
2935 mutex_lock(&trace_types_lock);
2936
2937 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2938 cpumask_clear(tracing_reader_cpumask);
2939 else
2940 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2941
c521efd1 2942
29bf4a5e 2943 if (iter->trace->pipe_close)
c521efd1
SR
2944 iter->trace->pipe_close(iter);
2945
b04cc6b1
FW
2946 mutex_unlock(&trace_types_lock);
2947
4462344e 2948 free_cpumask_var(iter->started);
d7350c3f
FW
2949 mutex_destroy(&iter->mutex);
2950 kfree(iter->trace);
b3806b43 2951 kfree(iter);
b3806b43
SR
2952
2953 return 0;
2954}
2955
2a2cc8f7
SSP
2956static unsigned int
2957tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2958{
2959 struct trace_iterator *iter = filp->private_data;
2960
2961 if (trace_flags & TRACE_ITER_BLOCK) {
2962 /*
2963 * Always select as readable when in blocking mode
2964 */
2965 return POLLIN | POLLRDNORM;
afc2abc0 2966 } else {
2a2cc8f7
SSP
2967 if (!trace_empty(iter))
2968 return POLLIN | POLLRDNORM;
2969 poll_wait(filp, &trace_wait, poll_table);
2970 if (!trace_empty(iter))
2971 return POLLIN | POLLRDNORM;
2972
2973 return 0;
2974 }
2975}
2976
6eaaa5d5
FW
2977
2978void default_wait_pipe(struct trace_iterator *iter)
2979{
2980 DEFINE_WAIT(wait);
2981
2982 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2983
2984 if (trace_empty(iter))
2985 schedule();
2986
2987 finish_wait(&trace_wait, &wait);
2988}
2989
2990/*
2991 * This is a make-shift waitqueue.
2992 * A tracer might use this callback on some rare cases:
2993 *
2994 * 1) the current tracer might hold the runqueue lock when it wakes up
2995 * a reader, hence a deadlock (sched, function, and function graph tracers)
2996 * 2) the function tracers, trace all functions, we don't want
2997 * the overhead of calling wake_up and friends
2998 * (and tracing them too)
2999 *
3000 * Anyway, this is really very primitive wakeup.
3001 */
3002void poll_wait_pipe(struct trace_iterator *iter)
3003{
3004 set_current_state(TASK_INTERRUPTIBLE);
3005 /* sleep for 100 msecs, and try again. */
3006 schedule_timeout(HZ / 10);
3007}
3008
ff98781b
EGM
3009/* Must be called with trace_types_lock mutex held. */
3010static int tracing_wait_pipe(struct file *filp)
b3806b43
SR
3011{
3012 struct trace_iterator *iter = filp->private_data;
b3806b43 3013
b3806b43 3014 while (trace_empty(iter)) {
2dc8f095 3015
107bad8b 3016 if ((filp->f_flags & O_NONBLOCK)) {
ff98781b 3017 return -EAGAIN;
107bad8b 3018 }
2dc8f095 3019
d7350c3f 3020 mutex_unlock(&iter->mutex);
107bad8b 3021
6eaaa5d5 3022 iter->trace->wait_pipe(iter);
b3806b43 3023
d7350c3f 3024 mutex_lock(&iter->mutex);
107bad8b 3025
6eaaa5d5 3026 if (signal_pending(current))
ff98781b 3027 return -EINTR;
b3806b43
SR
3028
3029 /*
3030 * We block until we read something and tracing is disabled.
3031 * We still block if tracing is disabled, but we have never
3032 * read anything. This allows a user to cat this file, and
3033 * then enable tracing. But after we have read something,
3034 * we give an EOF when tracing is again disabled.
3035 *
3036 * iter->pos will be 0 if we haven't read anything.
3037 */
3038 if (!tracer_enabled && iter->pos)
3039 break;
b3806b43
SR
3040 }
3041
ff98781b
EGM
3042 return 1;
3043}
3044
3045/*
3046 * Consumer reader.
3047 */
3048static ssize_t
3049tracing_read_pipe(struct file *filp, char __user *ubuf,
3050 size_t cnt, loff_t *ppos)
3051{
3052 struct trace_iterator *iter = filp->private_data;
d7350c3f 3053 static struct tracer *old_tracer;
ff98781b
EGM
3054 ssize_t sret;
3055
3056 /* return any leftover data */
3057 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3058 if (sret != -EBUSY)
3059 return sret;
3060
f9520750 3061 trace_seq_init(&iter->seq);
ff98781b 3062
d7350c3f 3063 /* copy the tracer to avoid using a global lock all around */
ff98781b 3064 mutex_lock(&trace_types_lock);
d7350c3f
FW
3065 if (unlikely(old_tracer != current_trace && current_trace)) {
3066 old_tracer = current_trace;
3067 *iter->trace = *current_trace;
3068 }
3069 mutex_unlock(&trace_types_lock);
3070
3071 /*
3072 * Avoid more than one consumer on a single file descriptor
3073 * This is just a matter of traces coherency, the ring buffer itself
3074 * is protected.
3075 */
3076 mutex_lock(&iter->mutex);
ff98781b
EGM
3077 if (iter->trace->read) {
3078 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3079 if (sret)
3080 goto out;
3081 }
3082
3083waitagain:
3084 sret = tracing_wait_pipe(filp);
3085 if (sret <= 0)
3086 goto out;
3087
b3806b43 3088 /* stop when tracing is finished */
ff98781b
EGM
3089 if (trace_empty(iter)) {
3090 sret = 0;
107bad8b 3091 goto out;
ff98781b 3092 }
b3806b43
SR
3093
3094 if (cnt >= PAGE_SIZE)
3095 cnt = PAGE_SIZE - 1;
3096
53d0aa77 3097 /* reset all but tr, trace, and overruns */
53d0aa77
SR
3098 memset(&iter->seq, 0,
3099 sizeof(struct trace_iterator) -
3100 offsetof(struct trace_iterator, seq));
4823ed7e 3101 iter->pos = -1;
b3806b43 3102
4f535968 3103 trace_event_read_lock();
088b1e42 3104 while (find_next_entry_inc(iter) != NULL) {
2c4f035f 3105 enum print_line_t ret;
088b1e42
SR
3106 int len = iter->seq.len;
3107
f9896bf3 3108 ret = print_trace_line(iter);
2c4f035f 3109 if (ret == TRACE_TYPE_PARTIAL_LINE) {
088b1e42
SR
3110 /* don't print partial lines */
3111 iter->seq.len = len;
b3806b43 3112 break;
088b1e42 3113 }
b91facc3
FW
3114 if (ret != TRACE_TYPE_NO_CONSUME)
3115 trace_consume(iter);
b3806b43
SR
3116
3117 if (iter->seq.len >= cnt)
3118 break;
b3806b43 3119 }
4f535968 3120 trace_event_read_unlock();
b3806b43 3121
b3806b43 3122 /* Now copy what we have to the user */
6c6c2796
PP
3123 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3124 if (iter->seq.readpos >= iter->seq.len)
f9520750 3125 trace_seq_init(&iter->seq);
9ff4b974
PP
3126
3127 /*
3128 * If there was nothing to send to user, inspite of consuming trace
3129 * entries, go back to wait for more entries.
3130 */
6c6c2796 3131 if (sret == -EBUSY)
9ff4b974 3132 goto waitagain;
b3806b43 3133
107bad8b 3134out:
d7350c3f 3135 mutex_unlock(&iter->mutex);
107bad8b 3136
6c6c2796 3137 return sret;
b3806b43
SR
3138}
3139
3c56819b
EGM
3140static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3141 struct pipe_buffer *buf)
3142{
3143 __free_page(buf->page);
3144}
3145
3146static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3147 unsigned int idx)
3148{
3149 __free_page(spd->pages[idx]);
3150}
3151
3152static struct pipe_buf_operations tracing_pipe_buf_ops = {
34cd4998
SR
3153 .can_merge = 0,
3154 .map = generic_pipe_buf_map,
3155 .unmap = generic_pipe_buf_unmap,
3156 .confirm = generic_pipe_buf_confirm,
3157 .release = tracing_pipe_buf_release,
3158 .steal = generic_pipe_buf_steal,
3159 .get = generic_pipe_buf_get,
3c56819b
EGM
3160};
3161
34cd4998 3162static size_t
fa7c7f6e 3163tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
34cd4998
SR
3164{
3165 size_t count;
3166 int ret;
3167
3168 /* Seq buffer is page-sized, exactly what we need. */
3169 for (;;) {
3170 count = iter->seq.len;
3171 ret = print_trace_line(iter);
3172 count = iter->seq.len - count;
3173 if (rem < count) {
3174 rem = 0;
3175 iter->seq.len -= count;
3176 break;
3177 }
3178 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3179 iter->seq.len -= count;
3180 break;
3181 }
3182
74e7ff8c
LJ
3183 if (ret != TRACE_TYPE_NO_CONSUME)
3184 trace_consume(iter);
34cd4998
SR
3185 rem -= count;
3186 if (!find_next_entry_inc(iter)) {
3187 rem = 0;
3188 iter->ent = NULL;
3189 break;
3190 }
3191 }
3192
3193 return rem;
3194}
3195
3c56819b
EGM
3196static ssize_t tracing_splice_read_pipe(struct file *filp,
3197 loff_t *ppos,
3198 struct pipe_inode_info *pipe,
3199 size_t len,
3200 unsigned int flags)
3201{
3202 struct page *pages[PIPE_BUFFERS];
3203 struct partial_page partial[PIPE_BUFFERS];
3204 struct trace_iterator *iter = filp->private_data;
3205 struct splice_pipe_desc spd = {
34cd4998
SR
3206 .pages = pages,
3207 .partial = partial,
3208 .nr_pages = 0, /* This gets updated below. */
3209 .flags = flags,
3210 .ops = &tracing_pipe_buf_ops,
3211 .spd_release = tracing_spd_release_pipe,
3c56819b 3212 };
d7350c3f 3213 static struct tracer *old_tracer;
3c56819b 3214 ssize_t ret;
34cd4998 3215 size_t rem;
3c56819b
EGM
3216 unsigned int i;
3217
d7350c3f 3218 /* copy the tracer to avoid using a global lock all around */
3c56819b 3219 mutex_lock(&trace_types_lock);
d7350c3f
FW
3220 if (unlikely(old_tracer != current_trace && current_trace)) {
3221 old_tracer = current_trace;
3222 *iter->trace = *current_trace;
3223 }
3224 mutex_unlock(&trace_types_lock);
3225
3226 mutex_lock(&iter->mutex);
3c56819b
EGM
3227
3228 if (iter->trace->splice_read) {
3229 ret = iter->trace->splice_read(iter, filp,
3230 ppos, pipe, len, flags);
3231 if (ret)
34cd4998 3232 goto out_err;
3c56819b
EGM
3233 }
3234
3235 ret = tracing_wait_pipe(filp);
3236 if (ret <= 0)
34cd4998 3237 goto out_err;
3c56819b
EGM
3238
3239 if (!iter->ent && !find_next_entry_inc(iter)) {
3240 ret = -EFAULT;
34cd4998 3241 goto out_err;
3c56819b
EGM
3242 }
3243
4f535968
LJ
3244 trace_event_read_lock();
3245
3c56819b
EGM
3246 /* Fill as many pages as possible. */
3247 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3248 pages[i] = alloc_page(GFP_KERNEL);
34cd4998
SR
3249 if (!pages[i])
3250 break;
3c56819b 3251
fa7c7f6e 3252 rem = tracing_fill_pipe_page(rem, iter);
3c56819b
EGM
3253
3254 /* Copy the data into the page, so we can start over. */
3255 ret = trace_seq_to_buffer(&iter->seq,
3256 page_address(pages[i]),
3257 iter->seq.len);
3258 if (ret < 0) {
3259 __free_page(pages[i]);
3260 break;
3261 }
3262 partial[i].offset = 0;
3263 partial[i].len = iter->seq.len;
3264
f9520750 3265 trace_seq_init(&iter->seq);
3c56819b
EGM
3266 }
3267
4f535968 3268 trace_event_read_unlock();
d7350c3f 3269 mutex_unlock(&iter->mutex);
3c56819b
EGM
3270
3271 spd.nr_pages = i;
3272
3273 return splice_to_pipe(pipe, &spd);
3274
34cd4998 3275out_err:
d7350c3f 3276 mutex_unlock(&iter->mutex);
3c56819b
EGM
3277
3278 return ret;
3279}
3280
a98a3c3f
SR
3281static ssize_t
3282tracing_entries_read(struct file *filp, char __user *ubuf,
3283 size_t cnt, loff_t *ppos)
3284{
3285 struct trace_array *tr = filp->private_data;
db526ca3 3286 char buf[96];
a98a3c3f
SR
3287 int r;
3288
db526ca3
SR
3289 mutex_lock(&trace_types_lock);
3290 if (!ring_buffer_expanded)
3291 r = sprintf(buf, "%lu (expanded: %lu)\n",
3292 tr->entries >> 10,
3293 trace_buf_size >> 10);
3294 else
3295 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3296 mutex_unlock(&trace_types_lock);
3297
a98a3c3f
SR
3298 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3299}
3300
3301static ssize_t
3302tracing_entries_write(struct file *filp, const char __user *ubuf,
3303 size_t cnt, loff_t *ppos)
3304{
3305 unsigned long val;
3306 char buf[64];
bf5e6519 3307 int ret, cpu;
a98a3c3f 3308
cffae437
SR
3309 if (cnt >= sizeof(buf))
3310 return -EINVAL;
a98a3c3f
SR
3311
3312 if (copy_from_user(&buf, ubuf, cnt))
3313 return -EFAULT;
3314
3315 buf[cnt] = 0;
3316
c6caeeb1
SR
3317 ret = strict_strtoul(buf, 10, &val);
3318 if (ret < 0)
3319 return ret;
a98a3c3f
SR
3320
3321 /* must have at least 1 entry */
3322 if (!val)
3323 return -EINVAL;
3324
3325 mutex_lock(&trace_types_lock);
3326
c76f0694 3327 tracing_stop();
a98a3c3f 3328
bf5e6519
SR
3329 /* disable all cpu buffers */
3330 for_each_tracing_cpu(cpu) {
3331 if (global_trace.data[cpu])
3332 atomic_inc(&global_trace.data[cpu]->disabled);
3333 if (max_tr.data[cpu])
3334 atomic_inc(&max_tr.data[cpu]->disabled);
3335 }
3336
1696b2b0
SR
3337 /* value is in KB */
3338 val <<= 10;
3339
3928a8a2 3340 if (val != global_trace.entries) {
73c5162a 3341 ret = tracing_resize_ring_buffer(val);
3928a8a2
SR
3342 if (ret < 0) {
3343 cnt = ret;
3eefae99
SR
3344 goto out;
3345 }
a98a3c3f
SR
3346 }
3347
cf8517cf 3348 *ppos += cnt;
a98a3c3f 3349
19384c03
SR
3350 /* If check pages failed, return ENOMEM */
3351 if (tracing_disabled)
3352 cnt = -ENOMEM;
a98a3c3f 3353 out:
bf5e6519
SR
3354 for_each_tracing_cpu(cpu) {
3355 if (global_trace.data[cpu])
3356 atomic_dec(&global_trace.data[cpu]->disabled);
3357 if (max_tr.data[cpu])
3358 atomic_dec(&max_tr.data[cpu]->disabled);
3359 }
3360
c76f0694 3361 tracing_start();
a98a3c3f
SR
3362 max_tr.entries = global_trace.entries;
3363 mutex_unlock(&trace_types_lock);
3364
3365 return cnt;
3366}
3367
f2942487
CE
3368static int mark_printk(const char *fmt, ...)
3369{
3370 int ret;
3371 va_list args;
3372 va_start(args, fmt);
3373 ret = trace_vprintk(0, fmt, args);
3374 va_end(args);
3375 return ret;
3376}
3377
5bf9a1ee
PP
3378static ssize_t
3379tracing_mark_write(struct file *filp, const char __user *ubuf,
3380 size_t cnt, loff_t *fpos)
3381{
3382 char *buf;
5bf9a1ee 3383
c76f0694 3384 if (tracing_disabled)
5bf9a1ee
PP
3385 return -EINVAL;
3386
3387 if (cnt > TRACE_BUF_SIZE)
3388 cnt = TRACE_BUF_SIZE;
3389
c13d2f7c 3390 buf = kmalloc(cnt + 2, GFP_KERNEL);
5bf9a1ee
PP
3391 if (buf == NULL)
3392 return -ENOMEM;
3393
3394 if (copy_from_user(buf, ubuf, cnt)) {
3395 kfree(buf);
3396 return -EFAULT;
3397 }
c13d2f7c
CE
3398 if (buf[cnt-1] != '\n') {
3399 buf[cnt] = '\n';
3400 buf[cnt+1] = '\0';
3401 } else
3402 buf[cnt] = '\0';
5bf9a1ee 3403
f2942487 3404 cnt = mark_printk("%s", buf);
5bf9a1ee
PP
3405 kfree(buf);
3406 *fpos += cnt;
3407
3408 return cnt;
3409}
3410
5079f326
Z
3411static ssize_t tracing_clock_read(struct file *filp, char __user *ubuf,
3412 size_t cnt, loff_t *ppos)
3413{
3414 char buf[64];
3415 int bufiter = 0;
3416 int i;
3417
3418 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3419 bufiter += snprintf(buf + bufiter, sizeof(buf) - bufiter,
3420 "%s%s%s%s", i ? " " : "",
3421 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3422 i == trace_clock_id ? "]" : "");
3423 bufiter += snprintf(buf + bufiter, sizeof(buf) - bufiter, "\n");
3424
3425 return simple_read_from_buffer(ubuf, cnt, ppos, buf, bufiter);
3426}
3427
3428static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3429 size_t cnt, loff_t *fpos)
3430{
3431 char buf[64];
3432 const char *clockstr;
3433 int i;
3434
3435 if (cnt >= sizeof(buf))
3436 return -EINVAL;
3437
3438 if (copy_from_user(&buf, ubuf, cnt))
3439 return -EFAULT;
3440
3441 buf[cnt] = 0;
3442
3443 clockstr = strstrip(buf);
3444
3445 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3446 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3447 break;
3448 }
3449 if (i == ARRAY_SIZE(trace_clocks))
3450 return -EINVAL;
3451
3452 trace_clock_id = i;
3453
3454 mutex_lock(&trace_types_lock);
3455
3456 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3457 if (max_tr.buffer)
3458 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3459
3460 mutex_unlock(&trace_types_lock);
3461
3462 *fpos += cnt;
3463
3464 return cnt;
3465}
3466
5e2336a0 3467static const struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
3468 .open = tracing_open_generic,
3469 .read = tracing_max_lat_read,
3470 .write = tracing_max_lat_write,
bc0c38d1
SR
3471};
3472
5e2336a0 3473static const struct file_operations tracing_ctrl_fops = {
4bf39a94
IM
3474 .open = tracing_open_generic,
3475 .read = tracing_ctrl_read,
3476 .write = tracing_ctrl_write,
bc0c38d1
SR
3477};
3478
5e2336a0 3479static const struct file_operations set_tracer_fops = {
4bf39a94
IM
3480 .open = tracing_open_generic,
3481 .read = tracing_set_trace_read,
3482 .write = tracing_set_trace_write,
bc0c38d1
SR
3483};
3484
5e2336a0 3485static const struct file_operations tracing_pipe_fops = {
4bf39a94 3486 .open = tracing_open_pipe,
2a2cc8f7 3487 .poll = tracing_poll_pipe,
4bf39a94 3488 .read = tracing_read_pipe,
3c56819b 3489 .splice_read = tracing_splice_read_pipe,
4bf39a94 3490 .release = tracing_release_pipe,
b3806b43
SR
3491};
3492
5e2336a0 3493static const struct file_operations tracing_entries_fops = {
a98a3c3f
SR
3494 .open = tracing_open_generic,
3495 .read = tracing_entries_read,
3496 .write = tracing_entries_write,
3497};
3498
5e2336a0 3499static const struct file_operations tracing_mark_fops = {
43a15386 3500 .open = tracing_open_generic,
5bf9a1ee
PP
3501 .write = tracing_mark_write,
3502};
3503
5079f326
Z
3504static const struct file_operations trace_clock_fops = {
3505 .open = tracing_open_generic,
3506 .read = tracing_clock_read,
3507 .write = tracing_clock_write,
3508};
3509
2cadf913
SR
3510struct ftrace_buffer_info {
3511 struct trace_array *tr;
3512 void *spare;
3513 int cpu;
3514 unsigned int read;
3515};
3516
3517static int tracing_buffers_open(struct inode *inode, struct file *filp)
3518{
3519 int cpu = (int)(long)inode->i_private;
3520 struct ftrace_buffer_info *info;
3521
3522 if (tracing_disabled)
3523 return -ENODEV;
3524
3525 info = kzalloc(sizeof(*info), GFP_KERNEL);
3526 if (!info)
3527 return -ENOMEM;
3528
3529 info->tr = &global_trace;
3530 info->cpu = cpu;
ddd538f3 3531 info->spare = NULL;
2cadf913
SR
3532 /* Force reading ring buffer for first read */
3533 info->read = (unsigned int)-1;
2cadf913
SR
3534
3535 filp->private_data = info;
3536
d1e7e02f 3537 return nonseekable_open(inode, filp);
2cadf913
SR
3538}
3539
3540static ssize_t
3541tracing_buffers_read(struct file *filp, char __user *ubuf,
3542 size_t count, loff_t *ppos)
3543{
3544 struct ftrace_buffer_info *info = filp->private_data;
3545 unsigned int pos;
3546 ssize_t ret;
3547 size_t size;
3548
2dc5d12b
SR
3549 if (!count)
3550 return 0;
3551
ddd538f3
LJ
3552 if (!info->spare)
3553 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3554 if (!info->spare)
3555 return -ENOMEM;
3556
2cadf913
SR
3557 /* Do we have previous read data to read? */
3558 if (info->read < PAGE_SIZE)
3559 goto read;
3560
3561 info->read = 0;
3562
3563 ret = ring_buffer_read_page(info->tr->buffer,
3564 &info->spare,
3565 count,
3566 info->cpu, 0);
3567 if (ret < 0)
3568 return 0;
3569
3570 pos = ring_buffer_page_len(info->spare);
3571
3572 if (pos < PAGE_SIZE)
3573 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3574
3575read:
3576 size = PAGE_SIZE - info->read;
3577 if (size > count)
3578 size = count;
3579
3580 ret = copy_to_user(ubuf, info->spare + info->read, size);
2dc5d12b 3581 if (ret == size)
2cadf913 3582 return -EFAULT;
2dc5d12b
SR
3583 size -= ret;
3584
2cadf913
SR
3585 *ppos += size;
3586 info->read += size;
3587
3588 return size;
3589}
3590
3591static int tracing_buffers_release(struct inode *inode, struct file *file)
3592{
3593 struct ftrace_buffer_info *info = file->private_data;
3594
ddd538f3
LJ
3595 if (info->spare)
3596 ring_buffer_free_read_page(info->tr->buffer, info->spare);
2cadf913
SR
3597 kfree(info);
3598
3599 return 0;
3600}
3601
3602struct buffer_ref {
3603 struct ring_buffer *buffer;
3604 void *page;
3605 int ref;
3606};
3607
3608static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3609 struct pipe_buffer *buf)
3610{
3611 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3612
3613 if (--ref->ref)
3614 return;
3615
3616 ring_buffer_free_read_page(ref->buffer, ref->page);
3617 kfree(ref);
3618 buf->private = 0;
3619}
3620
3621static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3622 struct pipe_buffer *buf)
3623{
3624 return 1;
3625}
3626
3627static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3628 struct pipe_buffer *buf)
3629{
3630 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3631
3632 ref->ref++;
3633}
3634
3635/* Pipe buffer operations for a buffer. */
3636static struct pipe_buf_operations buffer_pipe_buf_ops = {
3637 .can_merge = 0,
3638 .map = generic_pipe_buf_map,
3639 .unmap = generic_pipe_buf_unmap,
3640 .confirm = generic_pipe_buf_confirm,
3641 .release = buffer_pipe_buf_release,
3642 .steal = buffer_pipe_buf_steal,
3643 .get = buffer_pipe_buf_get,
3644};
3645
3646/*
3647 * Callback from splice_to_pipe(), if we need to release some pages
3648 * at the end of the spd in case we error'ed out in filling the pipe.
3649 */
3650static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3651{
3652 struct buffer_ref *ref =
3653 (struct buffer_ref *)spd->partial[i].private;
3654
3655 if (--ref->ref)
3656 return;
3657
3658 ring_buffer_free_read_page(ref->buffer, ref->page);
3659 kfree(ref);
3660 spd->partial[i].private = 0;
3661}
3662
3663static ssize_t
3664tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3665 struct pipe_inode_info *pipe, size_t len,
3666 unsigned int flags)
3667{
3668 struct ftrace_buffer_info *info = file->private_data;
3669 struct partial_page partial[PIPE_BUFFERS];
3670 struct page *pages[PIPE_BUFFERS];
3671 struct splice_pipe_desc spd = {
3672 .pages = pages,
3673 .partial = partial,
3674 .flags = flags,
3675 .ops = &buffer_pipe_buf_ops,
3676 .spd_release = buffer_spd_release,
3677 };
3678 struct buffer_ref *ref;
93459c6c 3679 int entries, size, i;
2cadf913
SR
3680 size_t ret;
3681
93cfb3c9
LJ
3682 if (*ppos & (PAGE_SIZE - 1)) {
3683 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3684 return -EINVAL;
3685 }
3686
3687 if (len & (PAGE_SIZE - 1)) {
3688 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3689 if (len < PAGE_SIZE)
3690 return -EINVAL;
3691 len &= PAGE_MASK;
3692 }
3693
93459c6c
SR
3694 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3695
3696 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
2cadf913
SR
3697 struct page *page;
3698 int r;
3699
3700 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3701 if (!ref)
3702 break;
3703
7267fa68 3704 ref->ref = 1;
2cadf913
SR
3705 ref->buffer = info->tr->buffer;
3706 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3707 if (!ref->page) {
3708 kfree(ref);
3709 break;
3710 }
3711
3712 r = ring_buffer_read_page(ref->buffer, &ref->page,
f2957f1f 3713 len, info->cpu, 1);
2cadf913
SR
3714 if (r < 0) {
3715 ring_buffer_free_read_page(ref->buffer,
3716 ref->page);
3717 kfree(ref);
3718 break;
3719 }
3720
3721 /*
3722 * zero out any left over data, this is going to
3723 * user land.
3724 */
3725 size = ring_buffer_page_len(ref->page);
3726 if (size < PAGE_SIZE)
3727 memset(ref->page + size, 0, PAGE_SIZE - size);
3728
3729 page = virt_to_page(ref->page);
3730
3731 spd.pages[i] = page;
3732 spd.partial[i].len = PAGE_SIZE;
3733 spd.partial[i].offset = 0;
3734 spd.partial[i].private = (unsigned long)ref;
3735 spd.nr_pages++;
93cfb3c9 3736 *ppos += PAGE_SIZE;
93459c6c
SR
3737
3738 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
2cadf913
SR
3739 }
3740
3741 spd.nr_pages = i;
3742
3743 /* did we read anything? */
3744 if (!spd.nr_pages) {
3745 if (flags & SPLICE_F_NONBLOCK)
3746 ret = -EAGAIN;
3747 else
3748 ret = 0;
3749 /* TODO: block */
3750 return ret;
3751 }
3752
3753 ret = splice_to_pipe(pipe, &spd);
3754
3755 return ret;
3756}
3757
3758static const struct file_operations tracing_buffers_fops = {
3759 .open = tracing_buffers_open,
3760 .read = tracing_buffers_read,
3761 .release = tracing_buffers_release,
3762 .splice_read = tracing_buffers_splice_read,
3763 .llseek = no_llseek,
3764};
3765
c8d77183
SR
3766static ssize_t
3767tracing_stats_read(struct file *filp, char __user *ubuf,
3768 size_t count, loff_t *ppos)
3769{
3770 unsigned long cpu = (unsigned long)filp->private_data;
3771 struct trace_array *tr = &global_trace;
3772 struct trace_seq *s;
3773 unsigned long cnt;
3774
e4f2d10f 3775 s = kmalloc(sizeof(*s), GFP_KERNEL);
c8d77183 3776 if (!s)
a646365c 3777 return -ENOMEM;
c8d77183
SR
3778
3779 trace_seq_init(s);
3780
3781 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3782 trace_seq_printf(s, "entries: %ld\n", cnt);
3783
3784 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3785 trace_seq_printf(s, "overrun: %ld\n", cnt);
3786
3787 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3788 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3789
c8d77183
SR
3790 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3791
3792 kfree(s);
3793
3794 return count;
3795}
3796
3797static const struct file_operations tracing_stats_fops = {
3798 .open = tracing_open_generic,
3799 .read = tracing_stats_read,
3800};
3801
bc0c38d1
SR
3802#ifdef CONFIG_DYNAMIC_FTRACE
3803
b807c3d0
SR
3804int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3805{
3806 return 0;
3807}
3808
bc0c38d1 3809static ssize_t
b807c3d0 3810tracing_read_dyn_info(struct file *filp, char __user *ubuf,
bc0c38d1
SR
3811 size_t cnt, loff_t *ppos)
3812{
a26a2a27
SR
3813 static char ftrace_dyn_info_buffer[1024];
3814 static DEFINE_MUTEX(dyn_info_mutex);
bc0c38d1 3815 unsigned long *p = filp->private_data;
b807c3d0 3816 char *buf = ftrace_dyn_info_buffer;
a26a2a27 3817 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
bc0c38d1
SR
3818 int r;
3819
b807c3d0
SR
3820 mutex_lock(&dyn_info_mutex);
3821 r = sprintf(buf, "%ld ", *p);
4bf39a94 3822
a26a2a27 3823 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
b807c3d0
SR
3824 buf[r++] = '\n';
3825
3826 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3827
3828 mutex_unlock(&dyn_info_mutex);
3829
3830 return r;
bc0c38d1
SR
3831}
3832
5e2336a0 3833static const struct file_operations tracing_dyn_info_fops = {
4bf39a94 3834 .open = tracing_open_generic,
b807c3d0 3835 .read = tracing_read_dyn_info,
bc0c38d1
SR
3836};
3837#endif
3838
3839static struct dentry *d_tracer;
3840
3841struct dentry *tracing_init_dentry(void)
3842{
3843 static int once;
3844
3845 if (d_tracer)
3846 return d_tracer;
3847
3e1f60b8
FW
3848 if (!debugfs_initialized())
3849 return NULL;
3850
bc0c38d1
SR
3851 d_tracer = debugfs_create_dir("tracing", NULL);
3852
3853 if (!d_tracer && !once) {
3854 once = 1;
3855 pr_warning("Could not create debugfs directory 'tracing'\n");
3856 return NULL;
3857 }
3858
3859 return d_tracer;
3860}
3861
b04cc6b1
FW
3862static struct dentry *d_percpu;
3863
3864struct dentry *tracing_dentry_percpu(void)
3865{
3866 static int once;
3867 struct dentry *d_tracer;
3868
3869 if (d_percpu)
3870 return d_percpu;
3871
3872 d_tracer = tracing_init_dentry();
3873
3874 if (!d_tracer)
3875 return NULL;
3876
3877 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3878
3879 if (!d_percpu && !once) {
3880 once = 1;
3881 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3882 return NULL;
3883 }
3884
3885 return d_percpu;
3886}
3887
3888static void tracing_init_debugfs_percpu(long cpu)
3889{
3890 struct dentry *d_percpu = tracing_dentry_percpu();
5452af66 3891 struct dentry *d_cpu;
8656e7a2
FW
3892 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3893 char cpu_dir[7];
b04cc6b1
FW
3894
3895 if (cpu > 999 || cpu < 0)
3896 return;
3897
8656e7a2
FW
3898 sprintf(cpu_dir, "cpu%ld", cpu);
3899 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3900 if (!d_cpu) {
3901 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3902 return;
3903 }
b04cc6b1 3904
8656e7a2 3905 /* per cpu trace_pipe */
5452af66
FW
3906 trace_create_file("trace_pipe", 0444, d_cpu,
3907 (void *) cpu, &tracing_pipe_fops);
b04cc6b1
FW
3908
3909 /* per cpu trace */
5452af66
FW
3910 trace_create_file("trace", 0644, d_cpu,
3911 (void *) cpu, &tracing_fops);
7f96f93f 3912
5452af66
FW
3913 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3914 (void *) cpu, &tracing_buffers_fops);
7f96f93f 3915
c8d77183
SR
3916 trace_create_file("stats", 0444, d_cpu,
3917 (void *) cpu, &tracing_stats_fops);
b04cc6b1
FW
3918}
3919
60a11774
SR
3920#ifdef CONFIG_FTRACE_SELFTEST
3921/* Let selftest have access to static functions in this file */
3922#include "trace_selftest.c"
3923#endif
3924
577b785f
SR
3925struct trace_option_dentry {
3926 struct tracer_opt *opt;
3927 struct tracer_flags *flags;
3928 struct dentry *entry;
3929};
3930
3931static ssize_t
3932trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3933 loff_t *ppos)
3934{
3935 struct trace_option_dentry *topt = filp->private_data;
3936 char *buf;
3937
3938 if (topt->flags->val & topt->opt->bit)
3939 buf = "1\n";
3940 else
3941 buf = "0\n";
3942
3943 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3944}
3945
3946static ssize_t
3947trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3948 loff_t *ppos)
3949{
3950 struct trace_option_dentry *topt = filp->private_data;
3951 unsigned long val;
3952 char buf[64];
3953 int ret;
3954
3955 if (cnt >= sizeof(buf))
3956 return -EINVAL;
3957
3958 if (copy_from_user(&buf, ubuf, cnt))
3959 return -EFAULT;
3960
3961 buf[cnt] = 0;
3962
3963 ret = strict_strtoul(buf, 10, &val);
3964 if (ret < 0)
3965 return ret;
3966
3967 ret = 0;
3968 switch (val) {
3969 case 0:
3970 /* do nothing if already cleared */
3971 if (!(topt->flags->val & topt->opt->bit))
3972 break;
3973
3974 mutex_lock(&trace_types_lock);
3975 if (current_trace->set_flag)
3976 ret = current_trace->set_flag(topt->flags->val,
3977 topt->opt->bit, 0);
3978 mutex_unlock(&trace_types_lock);
3979 if (ret)
3980 return ret;
3981 topt->flags->val &= ~topt->opt->bit;
3982 break;
3983 case 1:
3984 /* do nothing if already set */
3985 if (topt->flags->val & topt->opt->bit)
3986 break;
3987
3988 mutex_lock(&trace_types_lock);
3989 if (current_trace->set_flag)
3990 ret = current_trace->set_flag(topt->flags->val,
3991 topt->opt->bit, 1);
3992 mutex_unlock(&trace_types_lock);
3993 if (ret)
3994 return ret;
3995 topt->flags->val |= topt->opt->bit;
3996 break;
3997
3998 default:
3999 return -EINVAL;
4000 }
4001
4002 *ppos += cnt;
4003
4004 return cnt;
4005}
4006
4007
4008static const struct file_operations trace_options_fops = {
4009 .open = tracing_open_generic,
4010 .read = trace_options_read,
4011 .write = trace_options_write,
4012};
4013
a8259075
SR
4014static ssize_t
4015trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4016 loff_t *ppos)
4017{
4018 long index = (long)filp->private_data;
4019 char *buf;
4020
4021 if (trace_flags & (1 << index))
4022 buf = "1\n";
4023 else
4024 buf = "0\n";
4025
4026 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4027}
4028
4029static ssize_t
4030trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4031 loff_t *ppos)
4032{
4033 long index = (long)filp->private_data;
4034 char buf[64];
4035 unsigned long val;
4036 int ret;
4037
4038 if (cnt >= sizeof(buf))
4039 return -EINVAL;
4040
4041 if (copy_from_user(&buf, ubuf, cnt))
4042 return -EFAULT;
4043
4044 buf[cnt] = 0;
4045
4046 ret = strict_strtoul(buf, 10, &val);
4047 if (ret < 0)
4048 return ret;
4049
f2d84b65 4050 if (val != 0 && val != 1)
a8259075 4051 return -EINVAL;
f2d84b65 4052 set_tracer_flags(1 << index, val);
a8259075
SR
4053
4054 *ppos += cnt;
4055
4056 return cnt;
4057}
4058
a8259075
SR
4059static const struct file_operations trace_options_core_fops = {
4060 .open = tracing_open_generic,
4061 .read = trace_options_core_read,
4062 .write = trace_options_core_write,
4063};
4064
5452af66
FW
4065struct dentry *trace_create_file(const char *name,
4066 mode_t mode,
4067 struct dentry *parent,
4068 void *data,
4069 const struct file_operations *fops)
4070{
4071 struct dentry *ret;
4072
4073 ret = debugfs_create_file(name, mode, parent, data, fops);
4074 if (!ret)
4075 pr_warning("Could not create debugfs '%s' entry\n", name);
4076
4077 return ret;
4078}
4079
4080
a8259075
SR
4081static struct dentry *trace_options_init_dentry(void)
4082{
4083 struct dentry *d_tracer;
4084 static struct dentry *t_options;
4085
4086 if (t_options)
4087 return t_options;
4088
4089 d_tracer = tracing_init_dentry();
4090 if (!d_tracer)
4091 return NULL;
4092
4093 t_options = debugfs_create_dir("options", d_tracer);
4094 if (!t_options) {
4095 pr_warning("Could not create debugfs directory 'options'\n");
4096 return NULL;
4097 }
4098
4099 return t_options;
4100}
4101
577b785f
SR
4102static void
4103create_trace_option_file(struct trace_option_dentry *topt,
4104 struct tracer_flags *flags,
4105 struct tracer_opt *opt)
4106{
4107 struct dentry *t_options;
577b785f
SR
4108
4109 t_options = trace_options_init_dentry();
4110 if (!t_options)
4111 return;
4112
4113 topt->flags = flags;
4114 topt->opt = opt;
4115
5452af66 4116 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
577b785f
SR
4117 &trace_options_fops);
4118
577b785f
SR
4119}
4120
4121static struct trace_option_dentry *
4122create_trace_option_files(struct tracer *tracer)
4123{
4124 struct trace_option_dentry *topts;
4125 struct tracer_flags *flags;
4126 struct tracer_opt *opts;
4127 int cnt;
4128
4129 if (!tracer)
4130 return NULL;
4131
4132 flags = tracer->flags;
4133
4134 if (!flags || !flags->opts)
4135 return NULL;
4136
4137 opts = flags->opts;
4138
4139 for (cnt = 0; opts[cnt].name; cnt++)
4140 ;
4141
0cfe8245 4142 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
577b785f
SR
4143 if (!topts)
4144 return NULL;
4145
4146 for (cnt = 0; opts[cnt].name; cnt++)
4147 create_trace_option_file(&topts[cnt], flags,
4148 &opts[cnt]);
4149
4150 return topts;
4151}
4152
4153static void
4154destroy_trace_option_files(struct trace_option_dentry *topts)
4155{
4156 int cnt;
4157
4158 if (!topts)
4159 return;
4160
4161 for (cnt = 0; topts[cnt].opt; cnt++) {
4162 if (topts[cnt].entry)
4163 debugfs_remove(topts[cnt].entry);
4164 }
4165
4166 kfree(topts);
4167}
4168
a8259075
SR
4169static struct dentry *
4170create_trace_option_core_file(const char *option, long index)
4171{
4172 struct dentry *t_options;
a8259075
SR
4173
4174 t_options = trace_options_init_dentry();
4175 if (!t_options)
4176 return NULL;
4177
5452af66 4178 return trace_create_file(option, 0644, t_options, (void *)index,
a8259075 4179 &trace_options_core_fops);
a8259075
SR
4180}
4181
4182static __init void create_trace_options_dir(void)
4183{
4184 struct dentry *t_options;
a8259075
SR
4185 int i;
4186
4187 t_options = trace_options_init_dentry();
4188 if (!t_options)
4189 return;
4190
5452af66
FW
4191 for (i = 0; trace_options[i]; i++)
4192 create_trace_option_core_file(trace_options[i], i);
a8259075
SR
4193}
4194
b5ad384e 4195static __init int tracer_init_debugfs(void)
bc0c38d1
SR
4196{
4197 struct dentry *d_tracer;
b04cc6b1 4198 int cpu;
bc0c38d1
SR
4199
4200 d_tracer = tracing_init_dentry();
4201
5452af66
FW
4202 trace_create_file("tracing_enabled", 0644, d_tracer,
4203 &global_trace, &tracing_ctrl_fops);
bc0c38d1 4204
5452af66
FW
4205 trace_create_file("trace_options", 0644, d_tracer,
4206 NULL, &tracing_iter_fops);
bc0c38d1 4207
5452af66
FW
4208 trace_create_file("tracing_cpumask", 0644, d_tracer,
4209 NULL, &tracing_cpumask_fops);
4210
4211 trace_create_file("trace", 0644, d_tracer,
4212 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
a8259075 4213
5452af66
FW
4214 trace_create_file("available_tracers", 0444, d_tracer,
4215 &global_trace, &show_traces_fops);
4216
339ae5d3 4217 trace_create_file("current_tracer", 0644, d_tracer,
5452af66
FW
4218 &global_trace, &set_tracer_fops);
4219
5d4a9dba 4220#ifdef CONFIG_TRACER_MAX_TRACE
5452af66
FW
4221 trace_create_file("tracing_max_latency", 0644, d_tracer,
4222 &tracing_max_latency, &tracing_max_lat_fops);
4223
4224 trace_create_file("tracing_thresh", 0644, d_tracer,
4225 &tracing_thresh, &tracing_max_lat_fops);
5d4a9dba 4226#endif
a8259075 4227
339ae5d3 4228 trace_create_file("README", 0444, d_tracer,
5452af66
FW
4229 NULL, &tracing_readme_fops);
4230
4231 trace_create_file("trace_pipe", 0444, d_tracer,
b04cc6b1 4232 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
5452af66
FW
4233
4234 trace_create_file("buffer_size_kb", 0644, d_tracer,
4235 &global_trace, &tracing_entries_fops);
4236
4237 trace_create_file("trace_marker", 0220, d_tracer,
4238 NULL, &tracing_mark_fops);
5bf9a1ee 4239
69abe6a5
AP
4240 trace_create_file("saved_cmdlines", 0444, d_tracer,
4241 NULL, &tracing_saved_cmdlines_fops);
5bf9a1ee 4242
5079f326
Z
4243 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4244 &trace_clock_fops);
4245
bc0c38d1 4246#ifdef CONFIG_DYNAMIC_FTRACE
5452af66
FW
4247 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4248 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
bc0c38d1 4249#endif
d618b3e6
IM
4250#ifdef CONFIG_SYSPROF_TRACER
4251 init_tracer_sysprof_debugfs(d_tracer);
4252#endif
b04cc6b1 4253
5452af66
FW
4254 create_trace_options_dir();
4255
b04cc6b1
FW
4256 for_each_tracing_cpu(cpu)
4257 tracing_init_debugfs_percpu(cpu);
4258
b5ad384e 4259 return 0;
bc0c38d1
SR
4260}
4261
3f5a54e3
SR
4262static int trace_panic_handler(struct notifier_block *this,
4263 unsigned long event, void *unused)
4264{
944ac425
SR
4265 if (ftrace_dump_on_oops)
4266 ftrace_dump();
3f5a54e3
SR
4267 return NOTIFY_OK;
4268}
4269
4270static struct notifier_block trace_panic_notifier = {
4271 .notifier_call = trace_panic_handler,
4272 .next = NULL,
4273 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4274};
4275
4276static int trace_die_handler(struct notifier_block *self,
4277 unsigned long val,
4278 void *data)
4279{
4280 switch (val) {
4281 case DIE_OOPS:
944ac425
SR
4282 if (ftrace_dump_on_oops)
4283 ftrace_dump();
3f5a54e3
SR
4284 break;
4285 default:
4286 break;
4287 }
4288 return NOTIFY_OK;
4289}
4290
4291static struct notifier_block trace_die_notifier = {
4292 .notifier_call = trace_die_handler,
4293 .priority = 200
4294};
4295
4296/*
4297 * printk is set to max of 1024, we really don't need it that big.
4298 * Nothing should be printing 1000 characters anyway.
4299 */
4300#define TRACE_MAX_PRINT 1000
4301
4302/*
4303 * Define here KERN_TRACE so that we have one place to modify
4304 * it if we decide to change what log level the ftrace dump
4305 * should be at.
4306 */
428aee14 4307#define KERN_TRACE KERN_EMERG
3f5a54e3
SR
4308
4309static void
4310trace_printk_seq(struct trace_seq *s)
4311{
4312 /* Probably should print a warning here. */
4313 if (s->len >= 1000)
4314 s->len = 1000;
4315
4316 /* should be zero ended, but we are paranoid. */
4317 s->buffer[s->len] = 0;
4318
4319 printk(KERN_TRACE "%s", s->buffer);
4320
f9520750 4321 trace_seq_init(s);
3f5a54e3
SR
4322}
4323
cf586b61 4324static void __ftrace_dump(bool disable_tracing)
3f5a54e3 4325{
cd891ae0
SR
4326 static raw_spinlock_t ftrace_dump_lock =
4327 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3f5a54e3
SR
4328 /* use static because iter can be a bit big for the stack */
4329 static struct trace_iterator iter;
cf586b61 4330 unsigned int old_userobj;
3f5a54e3 4331 static int dump_ran;
d769041f
SR
4332 unsigned long flags;
4333 int cnt = 0, cpu;
3f5a54e3
SR
4334
4335 /* only one dump */
cd891ae0
SR
4336 local_irq_save(flags);
4337 __raw_spin_lock(&ftrace_dump_lock);
3f5a54e3
SR
4338 if (dump_ran)
4339 goto out;
4340
4341 dump_ran = 1;
4342
0ee6b6cf 4343 tracing_off();
cf586b61
FW
4344
4345 if (disable_tracing)
4346 ftrace_kill();
3f5a54e3 4347
d769041f
SR
4348 for_each_tracing_cpu(cpu) {
4349 atomic_inc(&global_trace.data[cpu]->disabled);
4350 }
4351
cf586b61
FW
4352 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4353
b54d3de9
TE
4354 /* don't look at user memory in panic mode */
4355 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4356
3f5a54e3
SR
4357 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4358
e543ad76 4359 /* Simulate the iterator */
3f5a54e3
SR
4360 iter.tr = &global_trace;
4361 iter.trace = current_trace;
e543ad76 4362 iter.cpu_file = TRACE_PIPE_ALL_CPU;
3f5a54e3
SR
4363
4364 /*
4365 * We need to stop all tracing on all CPUS to read the
4366 * the next buffer. This is a bit expensive, but is
4367 * not done often. We fill all what we can read,
4368 * and then release the locks again.
4369 */
4370
3f5a54e3
SR
4371 while (!trace_empty(&iter)) {
4372
4373 if (!cnt)
4374 printk(KERN_TRACE "---------------------------------\n");
4375
4376 cnt++;
4377
4378 /* reset all but tr, trace, and overruns */
4379 memset(&iter.seq, 0,
4380 sizeof(struct trace_iterator) -
4381 offsetof(struct trace_iterator, seq));
4382 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4383 iter.pos = -1;
4384
4385 if (find_next_entry_inc(&iter) != NULL) {
74e7ff8c
LJ
4386 int ret;
4387
4388 ret = print_trace_line(&iter);
4389 if (ret != TRACE_TYPE_NO_CONSUME)
4390 trace_consume(&iter);
3f5a54e3
SR
4391 }
4392
4393 trace_printk_seq(&iter.seq);
4394 }
4395
4396 if (!cnt)
4397 printk(KERN_TRACE " (ftrace buffer empty)\n");
4398 else
4399 printk(KERN_TRACE "---------------------------------\n");
4400
cf586b61
FW
4401 /* Re-enable tracing if requested */
4402 if (!disable_tracing) {
4403 trace_flags |= old_userobj;
4404
4405 for_each_tracing_cpu(cpu) {
4406 atomic_dec(&global_trace.data[cpu]->disabled);
4407 }
4408 tracing_on();
4409 }
4410
3f5a54e3 4411 out:
cd891ae0
SR
4412 __raw_spin_unlock(&ftrace_dump_lock);
4413 local_irq_restore(flags);
3f5a54e3
SR
4414}
4415
cf586b61
FW
4416/* By default: disable tracing after the dump */
4417void ftrace_dump(void)
4418{
4419 __ftrace_dump(true);
4420}
4421
3928a8a2 4422__init static int tracer_alloc_buffers(void)
bc0c38d1 4423{
73c5162a 4424 int ring_buf_size;
4c11d7ae 4425 int i;
9e01c1b7 4426 int ret = -ENOMEM;
4c11d7ae 4427
9e01c1b7
RR
4428 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4429 goto out;
4430
4431 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4432 goto out_free_buffer_mask;
4c11d7ae 4433
79f55997 4434 if (!zalloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
b04cc6b1
FW
4435 goto out_free_tracing_cpumask;
4436
73c5162a
SR
4437 /* To save memory, keep the ring buffer size to its minimum */
4438 if (ring_buffer_expanded)
4439 ring_buf_size = trace_buf_size;
4440 else
4441 ring_buf_size = 1;
4442
9e01c1b7
RR
4443 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4444 cpumask_copy(tracing_cpumask, cpu_all_mask);
4445
4446 /* TODO: make the number of buffers hot pluggable with CPUS */
73c5162a 4447 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
3928a8a2
SR
4448 TRACE_BUFFER_FLAGS);
4449 if (!global_trace.buffer) {
4450 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4451 WARN_ON(1);
9e01c1b7 4452 goto out_free_cpumask;
4c11d7ae 4453 }
3928a8a2 4454 global_trace.entries = ring_buffer_size(global_trace.buffer);
4c11d7ae 4455
9e01c1b7 4456
4c11d7ae 4457#ifdef CONFIG_TRACER_MAX_TRACE
73c5162a 4458 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
3928a8a2
SR
4459 TRACE_BUFFER_FLAGS);
4460 if (!max_tr.buffer) {
4461 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4462 WARN_ON(1);
4463 ring_buffer_free(global_trace.buffer);
9e01c1b7 4464 goto out_free_cpumask;
4c11d7ae 4465 }
3928a8a2
SR
4466 max_tr.entries = ring_buffer_size(max_tr.buffer);
4467 WARN_ON(max_tr.entries != global_trace.entries);
a98a3c3f 4468#endif
ab46428c 4469
4c11d7ae 4470 /* Allocate the first page for all buffers */
ab46428c 4471 for_each_tracing_cpu(i) {
566b0aaf 4472 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
bc0c38d1 4473 max_tr.data[i] = &per_cpu(max_data, i);
4c11d7ae 4474 }
bc0c38d1 4475
bc0c38d1
SR
4476 trace_init_cmdlines();
4477
43a15386 4478 register_tracer(&nop_trace);
79fb0768 4479 current_trace = &nop_trace;
b5ad384e
FW
4480#ifdef CONFIG_BOOT_TRACER
4481 register_tracer(&boot_tracer);
b5ad384e 4482#endif
60a11774
SR
4483 /* All seems OK, enable tracing */
4484 tracing_disabled = 0;
3928a8a2 4485
3f5a54e3
SR
4486 atomic_notifier_chain_register(&panic_notifier_list,
4487 &trace_panic_notifier);
4488
4489 register_die_notifier(&trace_die_notifier);
2fc1dfbe
FW
4490
4491 return 0;
3f5a54e3 4492
9e01c1b7 4493out_free_cpumask:
b04cc6b1
FW
4494 free_cpumask_var(tracing_reader_cpumask);
4495out_free_tracing_cpumask:
9e01c1b7
RR
4496 free_cpumask_var(tracing_cpumask);
4497out_free_buffer_mask:
4498 free_cpumask_var(tracing_buffer_mask);
4499out:
4500 return ret;
bc0c38d1 4501}
b2821ae6
SR
4502
4503__init static int clear_boot_tracer(void)
4504{
4505 /*
4506 * The default tracer at boot buffer is an init section.
4507 * This function is called in lateinit. If we did not
4508 * find the boot tracer, then clear it out, to prevent
4509 * later registration from accessing the buffer that is
4510 * about to be freed.
4511 */
4512 if (!default_bootup_tracer)
4513 return 0;
4514
4515 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4516 default_bootup_tracer);
4517 default_bootup_tracer = NULL;
4518
4519 return 0;
4520}
4521
b5ad384e
FW
4522early_initcall(tracer_alloc_buffers);
4523fs_initcall(tracer_init_debugfs);
b2821ae6 4524late_initcall(clear_boot_tracer);