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