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