1 /*
2 * ring buffer based function tracer
3 *
4 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 Nadia Yvette Chambers
13 */
14 #include <linux/ring_buffer.h>
15 #include <generated/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/rwsem.h>
35 #include <linux/slab.h>
36 #include <linux/ctype.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
39 #include <linux/nmi.h>
40 #include <linux/fs.h>
41 #include <linux/sched/rt.h>
42
43 #include "trace.h"
44 #include "trace_output.h"
45
46 /*
47 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
49 */
50 bool ring_buffer_expanded;
51
52 /*
53 * We need to change this state when a selftest is running.
54 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
56 * insertions into the ring-buffer such as trace_printk could occurred
57 * at the same time, giving false positive or negative results.
58 */
59 static bool __read_mostly tracing_selftest_running;
60
61 /*
62 * If a tracer is running, we do not want to run SELFTEST.
63 */
64 bool __read_mostly tracing_selftest_disabled;
65
66 /* For tracers that don't implement custom flags */
67 static struct tracer_opt dummy_tracer_opt[] = {
68 { }
69 };
70
71 static struct tracer_flags dummy_tracer_flags = {
72 .val = 0,
73 .opts = dummy_tracer_opt
74 };
75
dummy_set_flag(u32 old_flags,u32 bit,int set)76 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
77 {
78 return 0;
79 }
80
81 /*
82 * To prevent the comm cache from being overwritten when no
83 * tracing is active, only save the comm when a trace event
84 * occurred.
85 */
86 static DEFINE_PER_CPU(bool, trace_cmdline_save);
87
88 /*
89 * Kill all tracing for good (never come back).
90 * It is initialized to 1 but will turn to zero if the initialization
91 * of the tracer is successful. But that is the only place that sets
92 * this back to zero.
93 */
94 static int tracing_disabled = 1;
95
96 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
97
98 cpumask_var_t __read_mostly tracing_buffer_mask;
99
100 /*
101 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
102 *
103 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
104 * is set, then ftrace_dump is called. This will output the contents
105 * of the ftrace buffers to the console. This is very useful for
106 * capturing traces that lead to crashes and outputing it to a
107 * serial console.
108 *
109 * It is default off, but you can enable it with either specifying
110 * "ftrace_dump_on_oops" in the kernel command line, or setting
111 * /proc/sys/kernel/ftrace_dump_on_oops
112 * Set 1 if you want to dump buffers of all CPUs
113 * Set 2 if you want to dump the buffer of the CPU that triggered oops
114 */
115
116 enum ftrace_dump_mode ftrace_dump_on_oops;
117
118 static int tracing_set_tracer(const char *buf);
119
120 #define MAX_TRACER_SIZE 100
121 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
122 static char *default_bootup_tracer;
123
124 static bool allocate_snapshot;
125
set_cmdline_ftrace(char * str)126 static int __init set_cmdline_ftrace(char *str)
127 {
128 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
129 default_bootup_tracer = bootup_tracer_buf;
130 /* We are using ftrace early, expand it */
131 ring_buffer_expanded = true;
132 return 1;
133 }
134 __setup("ftrace=", set_cmdline_ftrace);
135
set_ftrace_dump_on_oops(char * str)136 static int __init set_ftrace_dump_on_oops(char *str)
137 {
138 if (*str++ != '=' || !*str) {
139 ftrace_dump_on_oops = DUMP_ALL;
140 return 1;
141 }
142
143 if (!strcmp("orig_cpu", str)) {
144 ftrace_dump_on_oops = DUMP_ORIG;
145 return 1;
146 }
147
148 return 0;
149 }
150 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
151
boot_alloc_snapshot(char * str)152 static int __init boot_alloc_snapshot(char *str)
153 {
154 allocate_snapshot = true;
155 /* We also need the main ring buffer expanded */
156 ring_buffer_expanded = true;
157 return 1;
158 }
159 __setup("alloc_snapshot", boot_alloc_snapshot);
160
161
162 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
163 static char *trace_boot_options __initdata;
164
set_trace_boot_options(char * str)165 static int __init set_trace_boot_options(char *str)
166 {
167 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
168 trace_boot_options = trace_boot_options_buf;
169 return 0;
170 }
171 __setup("trace_options=", set_trace_boot_options);
172
ns2usecs(cycle_t nsec)173 unsigned long long ns2usecs(cycle_t nsec)
174 {
175 nsec += 500;
176 do_div(nsec, 1000);
177 return nsec;
178 }
179
180 /*
181 * The global_trace is the descriptor that holds the tracing
182 * buffers for the live tracing. For each CPU, it contains
183 * a link list of pages that will store trace entries. The
184 * page descriptor of the pages in the memory is used to hold
185 * the link list by linking the lru item in the page descriptor
186 * to each of the pages in the buffer per CPU.
187 *
188 * For each active CPU there is a data field that holds the
189 * pages for the buffer for that CPU. Each CPU has the same number
190 * of pages allocated for its buffer.
191 */
192 static struct trace_array global_trace;
193
194 LIST_HEAD(ftrace_trace_arrays);
195
filter_current_check_discard(struct ring_buffer * buffer,struct ftrace_event_call * call,void * rec,struct ring_buffer_event * event)196 int filter_current_check_discard(struct ring_buffer *buffer,
197 struct ftrace_event_call *call, void *rec,
198 struct ring_buffer_event *event)
199 {
200 return filter_check_discard(call, rec, buffer, event);
201 }
202 EXPORT_SYMBOL_GPL(filter_current_check_discard);
203
ftrace_now(int cpu)204 cycle_t ftrace_now(int cpu)
205 {
206 u64 ts;
207
208 /* Early boot up does not have a buffer yet */
209 if (!global_trace.trace_buffer.buffer)
210 return trace_clock_local();
211
212 ts = ring_buffer_time_stamp(global_trace.trace_buffer.buffer, cpu);
213 ring_buffer_normalize_time_stamp(global_trace.trace_buffer.buffer, cpu, &ts);
214
215 return ts;
216 }
217
tracing_is_enabled(void)218 int tracing_is_enabled(void)
219 {
220 return tracing_is_on();
221 }
222
223 /*
224 * trace_buf_size is the size in bytes that is allocated
225 * for a buffer. Note, the number of bytes is always rounded
226 * to page size.
227 *
228 * This number is purposely set to a low number of 16384.
229 * If the dump on oops happens, it will be much appreciated
230 * to not have to wait for all that output. Anyway this can be
231 * boot time and run time configurable.
232 */
233 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
234
235 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
236
237 /* trace_types holds a link list of available tracers. */
238 static struct tracer *trace_types __read_mostly;
239
240 /*
241 * trace_types_lock is used to protect the trace_types list.
242 */
243 static DEFINE_MUTEX(trace_types_lock);
244
245 /*
246 * serialize the access of the ring buffer
247 *
248 * ring buffer serializes readers, but it is low level protection.
249 * The validity of the events (which returns by ring_buffer_peek() ..etc)
250 * are not protected by ring buffer.
251 *
252 * The content of events may become garbage if we allow other process consumes
253 * these events concurrently:
254 * A) the page of the consumed events may become a normal page
255 * (not reader page) in ring buffer, and this page will be rewrited
256 * by events producer.
257 * B) The page of the consumed events may become a page for splice_read,
258 * and this page will be returned to system.
259 *
260 * These primitives allow multi process access to different cpu ring buffer
261 * concurrently.
262 *
263 * These primitives don't distinguish read-only and read-consume access.
264 * Multi read-only access are also serialized.
265 */
266
267 #ifdef CONFIG_SMP
268 static DECLARE_RWSEM(all_cpu_access_lock);
269 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
270
trace_access_lock(int cpu)271 static inline void trace_access_lock(int cpu)
272 {
273 if (cpu == RING_BUFFER_ALL_CPUS) {
274 /* gain it for accessing the whole ring buffer. */
275 down_write(&all_cpu_access_lock);
276 } else {
277 /* gain it for accessing a cpu ring buffer. */
278
279 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
280 down_read(&all_cpu_access_lock);
281
282 /* Secondly block other access to this @cpu ring buffer. */
283 mutex_lock(&per_cpu(cpu_access_lock, cpu));
284 }
285 }
286
trace_access_unlock(int cpu)287 static inline void trace_access_unlock(int cpu)
288 {
289 if (cpu == RING_BUFFER_ALL_CPUS) {
290 up_write(&all_cpu_access_lock);
291 } else {
292 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
293 up_read(&all_cpu_access_lock);
294 }
295 }
296
trace_access_lock_init(void)297 static inline void trace_access_lock_init(void)
298 {
299 int cpu;
300
301 for_each_possible_cpu(cpu)
302 mutex_init(&per_cpu(cpu_access_lock, cpu));
303 }
304
305 #else
306
307 static DEFINE_MUTEX(access_lock);
308
trace_access_lock(int cpu)309 static inline void trace_access_lock(int cpu)
310 {
311 (void)cpu;
312 mutex_lock(&access_lock);
313 }
314
trace_access_unlock(int cpu)315 static inline void trace_access_unlock(int cpu)
316 {
317 (void)cpu;
318 mutex_unlock(&access_lock);
319 }
320
trace_access_lock_init(void)321 static inline void trace_access_lock_init(void)
322 {
323 }
324
325 #endif
326
327 /* trace_flags holds trace_options default values */
328 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
329 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
330 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
331 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
332
333 /**
334 * tracing_on - enable tracing buffers
335 *
336 * This function enables tracing buffers that may have been
337 * disabled with tracing_off.
338 */
tracing_on(void)339 void tracing_on(void)
340 {
341 if (global_trace.trace_buffer.buffer)
342 ring_buffer_record_on(global_trace.trace_buffer.buffer);
343 /*
344 * This flag is only looked at when buffers haven't been
345 * allocated yet. We don't really care about the race
346 * between setting this flag and actually turning
347 * on the buffer.
348 */
349 global_trace.buffer_disabled = 0;
350 }
351 EXPORT_SYMBOL_GPL(tracing_on);
352
353 /**
354 * __trace_puts - write a constant string into the trace buffer.
355 * @ip: The address of the caller
356 * @str: The constant string to write
357 * @size: The size of the string.
358 */
__trace_puts(unsigned long ip,const char * str,int size)359 int __trace_puts(unsigned long ip, const char *str, int size)
360 {
361 struct ring_buffer_event *event;
362 struct ring_buffer *buffer;
363 struct print_entry *entry;
364 unsigned long irq_flags;
365 int alloc;
366
367 alloc = sizeof(*entry) + size + 2; /* possible \n added */
368
369 local_save_flags(irq_flags);
370 buffer = global_trace.trace_buffer.buffer;
371 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
372 irq_flags, preempt_count());
373 if (!event)
374 return 0;
375
376 entry = ring_buffer_event_data(event);
377 entry->ip = ip;
378
379 memcpy(&entry->buf, str, size);
380
381 /* Add a newline if necessary */
382 if (entry->buf[size - 1] != '\n') {
383 entry->buf[size] = '\n';
384 entry->buf[size + 1] = '\0';
385 } else
386 entry->buf[size] = '\0';
387
388 __buffer_unlock_commit(buffer, event);
389
390 return size;
391 }
392 EXPORT_SYMBOL_GPL(__trace_puts);
393
394 /**
395 * __trace_bputs - write the pointer to a constant string into trace buffer
396 * @ip: The address of the caller
397 * @str: The constant string to write to the buffer to
398 */
__trace_bputs(unsigned long ip,const char * str)399 int __trace_bputs(unsigned long ip, const char *str)
400 {
401 struct ring_buffer_event *event;
402 struct ring_buffer *buffer;
403 struct bputs_entry *entry;
404 unsigned long irq_flags;
405 int size = sizeof(struct bputs_entry);
406
407 local_save_flags(irq_flags);
408 buffer = global_trace.trace_buffer.buffer;
409 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
410 irq_flags, preempt_count());
411 if (!event)
412 return 0;
413
414 entry = ring_buffer_event_data(event);
415 entry->ip = ip;
416 entry->str = str;
417
418 __buffer_unlock_commit(buffer, event);
419
420 return 1;
421 }
422 EXPORT_SYMBOL_GPL(__trace_bputs);
423
424 #ifdef CONFIG_TRACER_SNAPSHOT
425 /**
426 * trace_snapshot - take a snapshot of the current buffer.
427 *
428 * This causes a swap between the snapshot buffer and the current live
429 * tracing buffer. You can use this to take snapshots of the live
430 * trace when some condition is triggered, but continue to trace.
431 *
432 * Note, make sure to allocate the snapshot with either
433 * a tracing_snapshot_alloc(), or by doing it manually
434 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
435 *
436 * If the snapshot buffer is not allocated, it will stop tracing.
437 * Basically making a permanent snapshot.
438 */
tracing_snapshot(void)439 void tracing_snapshot(void)
440 {
441 struct trace_array *tr = &global_trace;
442 struct tracer *tracer = tr->current_trace;
443 unsigned long flags;
444
445 if (in_nmi()) {
446 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
447 internal_trace_puts("*** snapshot is being ignored ***\n");
448 return;
449 }
450
451 if (!tr->allocated_snapshot) {
452 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
453 internal_trace_puts("*** stopping trace here! ***\n");
454 tracing_off();
455 return;
456 }
457
458 /* Note, snapshot can not be used when the tracer uses it */
459 if (tracer->use_max_tr) {
460 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
461 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
462 return;
463 }
464
465 local_irq_save(flags);
466 update_max_tr(tr, current, smp_processor_id());
467 local_irq_restore(flags);
468 }
469 EXPORT_SYMBOL_GPL(tracing_snapshot);
470
471 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
472 struct trace_buffer *size_buf, int cpu_id);
473 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
474
alloc_snapshot(struct trace_array * tr)475 static int alloc_snapshot(struct trace_array *tr)
476 {
477 int ret;
478
479 if (!tr->allocated_snapshot) {
480
481 /* allocate spare buffer */
482 ret = resize_buffer_duplicate_size(&tr->max_buffer,
483 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
484 if (ret < 0)
485 return ret;
486
487 tr->allocated_snapshot = true;
488 }
489
490 return 0;
491 }
492
free_snapshot(struct trace_array * tr)493 void free_snapshot(struct trace_array *tr)
494 {
495 /*
496 * We don't free the ring buffer. instead, resize it because
497 * The max_tr ring buffer has some state (e.g. ring->clock) and
498 * we want preserve it.
499 */
500 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
501 set_buffer_entries(&tr->max_buffer, 1);
502 tracing_reset_online_cpus(&tr->max_buffer);
503 tr->allocated_snapshot = false;
504 }
505
506 /**
507 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
508 *
509 * This is similar to trace_snapshot(), but it will allocate the
510 * snapshot buffer if it isn't already allocated. Use this only
511 * where it is safe to sleep, as the allocation may sleep.
512 *
513 * This causes a swap between the snapshot buffer and the current live
514 * tracing buffer. You can use this to take snapshots of the live
515 * trace when some condition is triggered, but continue to trace.
516 */
tracing_snapshot_alloc(void)517 void tracing_snapshot_alloc(void)
518 {
519 struct trace_array *tr = &global_trace;
520 int ret;
521
522 ret = alloc_snapshot(tr);
523 if (WARN_ON(ret < 0))
524 return;
525
526 tracing_snapshot();
527 }
528 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
529 #else
tracing_snapshot(void)530 void tracing_snapshot(void)
531 {
532 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
533 }
534 EXPORT_SYMBOL_GPL(tracing_snapshot);
tracing_snapshot_alloc(void)535 void tracing_snapshot_alloc(void)
536 {
537 /* Give warning */
538 tracing_snapshot();
539 }
540 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
541 #endif /* CONFIG_TRACER_SNAPSHOT */
542
543 /**
544 * tracing_off - turn off tracing buffers
545 *
546 * This function stops the tracing buffers from recording data.
547 * It does not disable any overhead the tracers themselves may
548 * be causing. This function simply causes all recording to
549 * the ring buffers to fail.
550 */
tracing_off(void)551 void tracing_off(void)
552 {
553 if (global_trace.trace_buffer.buffer)
554 ring_buffer_record_off(global_trace.trace_buffer.buffer);
555 /*
556 * This flag is only looked at when buffers haven't been
557 * allocated yet. We don't really care about the race
558 * between setting this flag and actually turning
559 * on the buffer.
560 */
561 global_trace.buffer_disabled = 1;
562 }
563 EXPORT_SYMBOL_GPL(tracing_off);
564
565 /**
566 * tracing_is_on - show state of ring buffers enabled
567 */
tracing_is_on(void)568 int tracing_is_on(void)
569 {
570 if (global_trace.trace_buffer.buffer)
571 return ring_buffer_record_is_on(global_trace.trace_buffer.buffer);
572 return !global_trace.buffer_disabled;
573 }
574 EXPORT_SYMBOL_GPL(tracing_is_on);
575
set_buf_size(char * str)576 static int __init set_buf_size(char *str)
577 {
578 unsigned long buf_size;
579
580 if (!str)
581 return 0;
582 buf_size = memparse(str, &str);
583 /* nr_entries can not be zero */
584 if (buf_size == 0)
585 return 0;
586 trace_buf_size = buf_size;
587 return 1;
588 }
589 __setup("trace_buf_size=", set_buf_size);
590
set_tracing_thresh(char * str)591 static int __init set_tracing_thresh(char *str)
592 {
593 unsigned long threshold;
594 int ret;
595
596 if (!str)
597 return 0;
598 ret = kstrtoul(str, 0, &threshold);
599 if (ret < 0)
600 return 0;
601 tracing_thresh = threshold * 1000;
602 return 1;
603 }
604 __setup("tracing_thresh=", set_tracing_thresh);
605
nsecs_to_usecs(unsigned long nsecs)606 unsigned long nsecs_to_usecs(unsigned long nsecs)
607 {
608 return nsecs / 1000;
609 }
610
611 /* These must match the bit postions in trace_iterator_flags */
612 static const char *trace_options[] = {
613 "print-parent",
614 "sym-offset",
615 "sym-addr",
616 "verbose",
617 "raw",
618 "hex",
619 "bin",
620 "block",
621 "stacktrace",
622 "trace_printk",
623 "ftrace_preempt",
624 "branch",
625 "annotate",
626 "userstacktrace",
627 "sym-userobj",
628 "printk-msg-only",
629 "context-info",
630 "latency-format",
631 "sleep-time",
632 "graph-time",
633 "record-cmd",
634 "overwrite",
635 "disable_on_free",
636 "irq-info",
637 "markers",
638 "function-trace",
639 "print-tgid",
640 NULL
641 };
642
643 static struct {
644 u64 (*func)(void);
645 const char *name;
646 int in_ns; /* is this clock in nanoseconds? */
647 } trace_clocks[] = {
648 { trace_clock_local, "local", 1 },
649 { trace_clock_global, "global", 1 },
650 { trace_clock_counter, "counter", 0 },
651 { trace_clock_jiffies, "uptime", 1 },
652 { trace_clock, "perf", 1 },
653 ARCH_TRACE_CLOCKS
654 };
655
656 /*
657 * trace_parser_get_init - gets the buffer for trace parser
658 */
trace_parser_get_init(struct trace_parser * parser,int size)659 int trace_parser_get_init(struct trace_parser *parser, int size)
660 {
661 memset(parser, 0, sizeof(*parser));
662
663 parser->buffer = kmalloc(size, GFP_KERNEL);
664 if (!parser->buffer)
665 return 1;
666
667 parser->size = size;
668 return 0;
669 }
670
671 /*
672 * trace_parser_put - frees the buffer for trace parser
673 */
trace_parser_put(struct trace_parser * parser)674 void trace_parser_put(struct trace_parser *parser)
675 {
676 kfree(parser->buffer);
677 }
678
679 /*
680 * trace_get_user - reads the user input string separated by space
681 * (matched by isspace(ch))
682 *
683 * For each string found the 'struct trace_parser' is updated,
684 * and the function returns.
685 *
686 * Returns number of bytes read.
687 *
688 * See kernel/trace/trace.h for 'struct trace_parser' details.
689 */
trace_get_user(struct trace_parser * parser,const char __user * ubuf,size_t cnt,loff_t * ppos)690 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
691 size_t cnt, loff_t *ppos)
692 {
693 char ch;
694 size_t read = 0;
695 ssize_t ret;
696
697 if (!*ppos)
698 trace_parser_clear(parser);
699
700 ret = get_user(ch, ubuf++);
701 if (ret)
702 goto out;
703
704 read++;
705 cnt--;
706
707 /*
708 * The parser is not finished with the last write,
709 * continue reading the user input without skipping spaces.
710 */
711 if (!parser->cont) {
712 /* skip white space */
713 while (cnt && isspace(ch)) {
714 ret = get_user(ch, ubuf++);
715 if (ret)
716 goto out;
717 read++;
718 cnt--;
719 }
720
721 /* only spaces were written */
722 if (isspace(ch)) {
723 *ppos += read;
724 ret = read;
725 goto out;
726 }
727
728 parser->idx = 0;
729 }
730
731 /* read the non-space input */
732 while (cnt && !isspace(ch)) {
733 if (parser->idx < parser->size - 1)
734 parser->buffer[parser->idx++] = ch;
735 else {
736 ret = -EINVAL;
737 goto out;
738 }
739 ret = get_user(ch, ubuf++);
740 if (ret)
741 goto out;
742 read++;
743 cnt--;
744 }
745
746 /* We either got finished input or we have to wait for another call. */
747 if (isspace(ch)) {
748 parser->buffer[parser->idx] = 0;
749 parser->cont = false;
750 } else {
751 parser->cont = true;
752 parser->buffer[parser->idx++] = ch;
753 }
754
755 *ppos += read;
756 ret = read;
757
758 out:
759 return ret;
760 }
761
trace_seq_to_user(struct trace_seq * s,char __user * ubuf,size_t cnt)762 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
763 {
764 int len;
765 int ret;
766
767 if (!cnt)
768 return 0;
769
770 if (s->len <= s->readpos)
771 return -EBUSY;
772
773 len = s->len - s->readpos;
774 if (cnt > len)
775 cnt = len;
776 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
777 if (ret == cnt)
778 return -EFAULT;
779
780 cnt -= ret;
781
782 s->readpos += cnt;
783 return cnt;
784 }
785
trace_seq_to_buffer(struct trace_seq * s,void * buf,size_t cnt)786 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
787 {
788 int len;
789
790 if (s->len <= s->readpos)
791 return -EBUSY;
792
793 len = s->len - s->readpos;
794 if (cnt > len)
795 cnt = len;
796 memcpy(buf, s->buffer + s->readpos, cnt);
797
798 s->readpos += cnt;
799 return cnt;
800 }
801
802 /*
803 * ftrace_max_lock is used to protect the swapping of buffers
804 * when taking a max snapshot. The buffers themselves are
805 * protected by per_cpu spinlocks. But the action of the swap
806 * needs its own lock.
807 *
808 * This is defined as a arch_spinlock_t in order to help
809 * with performance when lockdep debugging is enabled.
810 *
811 * It is also used in other places outside the update_max_tr
812 * so it needs to be defined outside of the
813 * CONFIG_TRACER_MAX_TRACE.
814 */
815 static arch_spinlock_t ftrace_max_lock =
816 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
817
818 unsigned long __read_mostly tracing_thresh;
819
820 #ifdef CONFIG_TRACER_MAX_TRACE
821 unsigned long __read_mostly tracing_max_latency;
822
823 /*
824 * Copy the new maximum trace into the separate maximum-trace
825 * structure. (this way the maximum trace is permanently saved,
826 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
827 */
828 static void
__update_max_tr(struct trace_array * tr,struct task_struct * tsk,int cpu)829 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
830 {
831 struct trace_buffer *trace_buf = &tr->trace_buffer;
832 struct trace_buffer *max_buf = &tr->max_buffer;
833 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
834 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
835
836 max_buf->cpu = cpu;
837 max_buf->time_start = data->preempt_timestamp;
838
839 max_data->saved_latency = tracing_max_latency;
840 max_data->critical_start = data->critical_start;
841 max_data->critical_end = data->critical_end;
842
843 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
844 max_data->pid = tsk->pid;
845 /*
846 * If tsk == current, then use current_uid(), as that does not use
847 * RCU. The irq tracer can be called out of RCU scope.
848 */
849 if (tsk == current)
850 max_data->uid = current_uid();
851 else
852 max_data->uid = task_uid(tsk);
853
854 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
855 max_data->policy = tsk->policy;
856 max_data->rt_priority = tsk->rt_priority;
857
858 /* record this tasks comm */
859 tracing_record_cmdline(tsk);
860 }
861
862 /**
863 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
864 * @tr: tracer
865 * @tsk: the task with the latency
866 * @cpu: The cpu that initiated the trace.
867 *
868 * Flip the buffers between the @tr and the max_tr and record information
869 * about which task was the cause of this latency.
870 */
871 void
update_max_tr(struct trace_array * tr,struct task_struct * tsk,int cpu)872 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
873 {
874 struct ring_buffer *buf;
875
876 if (tr->stop_count)
877 return;
878
879 WARN_ON_ONCE(!irqs_disabled());
880
881 if (!tr->allocated_snapshot) {
882 /* Only the nop tracer should hit this when disabling */
883 WARN_ON_ONCE(tr->current_trace != &nop_trace);
884 return;
885 }
886
887 arch_spin_lock(&ftrace_max_lock);
888
889 buf = tr->trace_buffer.buffer;
890 tr->trace_buffer.buffer = tr->max_buffer.buffer;
891 tr->max_buffer.buffer = buf;
892
893 __update_max_tr(tr, tsk, cpu);
894 arch_spin_unlock(&ftrace_max_lock);
895 }
896
897 /**
898 * update_max_tr_single - only copy one trace over, and reset the rest
899 * @tr - tracer
900 * @tsk - task with the latency
901 * @cpu - the cpu of the buffer to copy.
902 *
903 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
904 */
905 void
update_max_tr_single(struct trace_array * tr,struct task_struct * tsk,int cpu)906 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
907 {
908 int ret;
909
910 if (tr->stop_count)
911 return;
912
913 WARN_ON_ONCE(!irqs_disabled());
914 if (!tr->allocated_snapshot) {
915 /* Only the nop tracer should hit this when disabling */
916 WARN_ON_ONCE(tr->current_trace != &nop_trace);
917 return;
918 }
919
920 arch_spin_lock(&ftrace_max_lock);
921
922 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
923
924 if (ret == -EBUSY) {
925 /*
926 * We failed to swap the buffer due to a commit taking
927 * place on this CPU. We fail to record, but we reset
928 * the max trace buffer (no one writes directly to it)
929 * and flag that it failed.
930 */
931 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
932 "Failed to swap buffers due to commit in progress\n");
933 }
934
935 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
936
937 __update_max_tr(tr, tsk, cpu);
938 arch_spin_unlock(&ftrace_max_lock);
939 }
940 #endif /* CONFIG_TRACER_MAX_TRACE */
941
default_wait_pipe(struct trace_iterator * iter)942 static void default_wait_pipe(struct trace_iterator *iter)
943 {
944 /* Iterators are static, they should be filled or empty */
945 if (trace_buffer_iter(iter, iter->cpu_file))
946 return;
947
948 ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
949 }
950
951 #ifdef CONFIG_FTRACE_STARTUP_TEST
run_tracer_selftest(struct tracer * type)952 static int run_tracer_selftest(struct tracer *type)
953 {
954 struct trace_array *tr = &global_trace;
955 struct tracer *saved_tracer = tr->current_trace;
956 int ret;
957
958 if (!type->selftest || tracing_selftest_disabled)
959 return 0;
960
961 /*
962 * Run a selftest on this tracer.
963 * Here we reset the trace buffer, and set the current
964 * tracer to be this tracer. The tracer can then run some
965 * internal tracing to verify that everything is in order.
966 * If we fail, we do not register this tracer.
967 */
968 tracing_reset_online_cpus(&tr->trace_buffer);
969
970 tr->current_trace = type;
971
972 #ifdef CONFIG_TRACER_MAX_TRACE
973 if (type->use_max_tr) {
974 /* If we expanded the buffers, make sure the max is expanded too */
975 if (ring_buffer_expanded)
976 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
977 RING_BUFFER_ALL_CPUS);
978 tr->allocated_snapshot = true;
979 }
980 #endif
981
982 /* the test is responsible for initializing and enabling */
983 pr_info("Testing tracer %s: ", type->name);
984 ret = type->selftest(type, tr);
985 /* the test is responsible for resetting too */
986 tr->current_trace = saved_tracer;
987 if (ret) {
988 printk(KERN_CONT "FAILED!\n");
989 /* Add the warning after printing 'FAILED' */
990 WARN_ON(1);
991 return -1;
992 }
993 /* Only reset on passing, to avoid touching corrupted buffers */
994 tracing_reset_online_cpus(&tr->trace_buffer);
995
996 #ifdef CONFIG_TRACER_MAX_TRACE
997 if (type->use_max_tr) {
998 tr->allocated_snapshot = false;
999
1000 /* Shrink the max buffer again */
1001 if (ring_buffer_expanded)
1002 ring_buffer_resize(tr->max_buffer.buffer, 1,
1003 RING_BUFFER_ALL_CPUS);
1004 }
1005 #endif
1006
1007 printk(KERN_CONT "PASSED\n");
1008 return 0;
1009 }
1010 #else
run_tracer_selftest(struct tracer * type)1011 static inline int run_tracer_selftest(struct tracer *type)
1012 {
1013 return 0;
1014 }
1015 #endif /* CONFIG_FTRACE_STARTUP_TEST */
1016
1017 /**
1018 * register_tracer - register a tracer with the ftrace system.
1019 * @type - the plugin for the tracer
1020 *
1021 * Register a new plugin tracer.
1022 */
register_tracer(struct tracer * type)1023 int register_tracer(struct tracer *type)
1024 {
1025 struct tracer *t;
1026 int ret = 0;
1027
1028 if (!type->name) {
1029 pr_info("Tracer must have a name\n");
1030 return -1;
1031 }
1032
1033 if (strlen(type->name) >= MAX_TRACER_SIZE) {
1034 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1035 return -1;
1036 }
1037
1038 mutex_lock(&trace_types_lock);
1039
1040 tracing_selftest_running = true;
1041
1042 for (t = trace_types; t; t = t->next) {
1043 if (strcmp(type->name, t->name) == 0) {
1044 /* already found */
1045 pr_info("Tracer %s already registered\n",
1046 type->name);
1047 ret = -1;
1048 goto out;
1049 }
1050 }
1051
1052 if (!type->set_flag)
1053 type->set_flag = &dummy_set_flag;
1054 if (!type->flags)
1055 type->flags = &dummy_tracer_flags;
1056 else
1057 if (!type->flags->opts)
1058 type->flags->opts = dummy_tracer_opt;
1059 if (!type->wait_pipe)
1060 type->wait_pipe = default_wait_pipe;
1061
1062 ret = run_tracer_selftest(type);
1063 if (ret < 0)
1064 goto out;
1065
1066 type->next = trace_types;
1067 trace_types = type;
1068
1069 out:
1070 tracing_selftest_running = false;
1071 mutex_unlock(&trace_types_lock);
1072
1073 if (ret || !default_bootup_tracer)
1074 goto out_unlock;
1075
1076 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
1077 goto out_unlock;
1078
1079 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1080 /* Do we want this tracer to start on bootup? */
1081 tracing_set_tracer(type->name);
1082 default_bootup_tracer = NULL;
1083 /* disable other selftests, since this will break it. */
1084 tracing_selftest_disabled = true;
1085 #ifdef CONFIG_FTRACE_STARTUP_TEST
1086 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1087 type->name);
1088 #endif
1089
1090 out_unlock:
1091 return ret;
1092 }
1093
tracing_reset(struct trace_buffer * buf,int cpu)1094 void tracing_reset(struct trace_buffer *buf, int cpu)
1095 {
1096 struct ring_buffer *buffer = buf->buffer;
1097
1098 if (!buffer)
1099 return;
1100
1101 ring_buffer_record_disable(buffer);
1102
1103 /* Make sure all commits have finished */
1104 synchronize_sched();
1105 ring_buffer_reset_cpu(buffer, cpu);
1106
1107 ring_buffer_record_enable(buffer);
1108 }
1109
tracing_reset_online_cpus(struct trace_buffer * buf)1110 void tracing_reset_online_cpus(struct trace_buffer *buf)
1111 {
1112 struct ring_buffer *buffer = buf->buffer;
1113 int cpu;
1114
1115 if (!buffer)
1116 return;
1117
1118 ring_buffer_record_disable(buffer);
1119
1120 /* Make sure all commits have finished */
1121 synchronize_sched();
1122
1123 buf->time_start = ftrace_now(buf->cpu);
1124
1125 for_each_online_cpu(cpu)
1126 ring_buffer_reset_cpu(buffer, cpu);
1127
1128 ring_buffer_record_enable(buffer);
1129 }
1130
tracing_reset_current(int cpu)1131 void tracing_reset_current(int cpu)
1132 {
1133 tracing_reset(&global_trace.trace_buffer, cpu);
1134 }
1135
tracing_reset_all_online_cpus(void)1136 void tracing_reset_all_online_cpus(void)
1137 {
1138 struct trace_array *tr;
1139
1140 mutex_lock(&trace_types_lock);
1141 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1142 tracing_reset_online_cpus(&tr->trace_buffer);
1143 #ifdef CONFIG_TRACER_MAX_TRACE
1144 tracing_reset_online_cpus(&tr->max_buffer);
1145 #endif
1146 }
1147 mutex_unlock(&trace_types_lock);
1148 }
1149
1150 #define SAVED_CMDLINES 128
1151 #define NO_CMDLINE_MAP UINT_MAX
1152 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1153 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1154 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1155 static unsigned saved_tgids[SAVED_CMDLINES];
1156 static int cmdline_idx;
1157 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1158
1159 /* temporary disable recording */
1160 static atomic_t trace_record_cmdline_disabled __read_mostly;
1161
trace_init_cmdlines(void)1162 static void trace_init_cmdlines(void)
1163 {
1164 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1165 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
1166 cmdline_idx = 0;
1167 }
1168
is_tracing_stopped(void)1169 int is_tracing_stopped(void)
1170 {
1171 return global_trace.stop_count;
1172 }
1173
1174 /**
1175 * ftrace_off_permanent - disable all ftrace code permanently
1176 *
1177 * This should only be called when a serious anomally has
1178 * been detected. This will turn off the function tracing,
1179 * ring buffers, and other tracing utilites. It takes no
1180 * locks and can be called from any context.
1181 */
ftrace_off_permanent(void)1182 void ftrace_off_permanent(void)
1183 {
1184 tracing_disabled = 1;
1185 ftrace_stop();
1186 tracing_off_permanent();
1187 }
1188
1189 /**
1190 * tracing_start - quick start of the tracer
1191 *
1192 * If tracing is enabled but was stopped by tracing_stop,
1193 * this will start the tracer back up.
1194 */
tracing_start(void)1195 void tracing_start(void)
1196 {
1197 struct ring_buffer *buffer;
1198 unsigned long flags;
1199
1200 if (tracing_disabled)
1201 return;
1202
1203 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1204 if (--global_trace.stop_count) {
1205 if (global_trace.stop_count < 0) {
1206 /* Someone screwed up their debugging */
1207 WARN_ON_ONCE(1);
1208 global_trace.stop_count = 0;
1209 }
1210 goto out;
1211 }
1212
1213 /* Prevent the buffers from switching */
1214 arch_spin_lock(&ftrace_max_lock);
1215
1216 buffer = global_trace.trace_buffer.buffer;
1217 if (buffer)
1218 ring_buffer_record_enable(buffer);
1219
1220 #ifdef CONFIG_TRACER_MAX_TRACE
1221 buffer = global_trace.max_buffer.buffer;
1222 if (buffer)
1223 ring_buffer_record_enable(buffer);
1224 #endif
1225
1226 arch_spin_unlock(&ftrace_max_lock);
1227
1228 ftrace_start();
1229 out:
1230 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1231 }
1232
tracing_start_tr(struct trace_array * tr)1233 static void tracing_start_tr(struct trace_array *tr)
1234 {
1235 struct ring_buffer *buffer;
1236 unsigned long flags;
1237
1238 if (tracing_disabled)
1239 return;
1240
1241 /* If global, we need to also start the max tracer */
1242 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1243 return tracing_start();
1244
1245 raw_spin_lock_irqsave(&tr->start_lock, flags);
1246
1247 if (--tr->stop_count) {
1248 if (tr->stop_count < 0) {
1249 /* Someone screwed up their debugging */
1250 WARN_ON_ONCE(1);
1251 tr->stop_count = 0;
1252 }
1253 goto out;
1254 }
1255
1256 buffer = tr->trace_buffer.buffer;
1257 if (buffer)
1258 ring_buffer_record_enable(buffer);
1259
1260 out:
1261 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1262 }
1263
1264 /**
1265 * tracing_stop - quick stop of the tracer
1266 *
1267 * Light weight way to stop tracing. Use in conjunction with
1268 * tracing_start.
1269 */
tracing_stop(void)1270 void tracing_stop(void)
1271 {
1272 struct ring_buffer *buffer;
1273 unsigned long flags;
1274
1275 ftrace_stop();
1276 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1277 if (global_trace.stop_count++)
1278 goto out;
1279
1280 /* Prevent the buffers from switching */
1281 arch_spin_lock(&ftrace_max_lock);
1282
1283 buffer = global_trace.trace_buffer.buffer;
1284 if (buffer)
1285 ring_buffer_record_disable(buffer);
1286
1287 #ifdef CONFIG_TRACER_MAX_TRACE
1288 buffer = global_trace.max_buffer.buffer;
1289 if (buffer)
1290 ring_buffer_record_disable(buffer);
1291 #endif
1292
1293 arch_spin_unlock(&ftrace_max_lock);
1294
1295 out:
1296 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1297 }
1298
tracing_stop_tr(struct trace_array * tr)1299 static void tracing_stop_tr(struct trace_array *tr)
1300 {
1301 struct ring_buffer *buffer;
1302 unsigned long flags;
1303
1304 /* If global, we need to also stop the max tracer */
1305 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1306 return tracing_stop();
1307
1308 raw_spin_lock_irqsave(&tr->start_lock, flags);
1309 if (tr->stop_count++)
1310 goto out;
1311
1312 buffer = tr->trace_buffer.buffer;
1313 if (buffer)
1314 ring_buffer_record_disable(buffer);
1315
1316 out:
1317 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1318 }
1319
1320 void trace_stop_cmdline_recording(void);
1321
trace_save_cmdline(struct task_struct * tsk)1322 static void trace_save_cmdline(struct task_struct *tsk)
1323 {
1324 unsigned pid, idx;
1325
1326 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1327 return;
1328
1329 /*
1330 * It's not the end of the world if we don't get
1331 * the lock, but we also don't want to spin
1332 * nor do we want to disable interrupts,
1333 * so if we miss here, then better luck next time.
1334 */
1335 if (!arch_spin_trylock(&trace_cmdline_lock))
1336 return;
1337
1338 idx = map_pid_to_cmdline[tsk->pid];
1339 if (idx == NO_CMDLINE_MAP) {
1340 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1341
1342 /*
1343 * Check whether the cmdline buffer at idx has a pid
1344 * mapped. We are going to overwrite that entry so we
1345 * need to clear the map_pid_to_cmdline. Otherwise we
1346 * would read the new comm for the old pid.
1347 */
1348 pid = map_cmdline_to_pid[idx];
1349 if (pid != NO_CMDLINE_MAP)
1350 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1351
1352 map_cmdline_to_pid[idx] = tsk->pid;
1353 map_pid_to_cmdline[tsk->pid] = idx;
1354
1355 cmdline_idx = idx;
1356 }
1357
1358 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1359 saved_tgids[idx] = tsk->tgid;
1360
1361 arch_spin_unlock(&trace_cmdline_lock);
1362 }
1363
trace_find_cmdline(int pid,char comm[])1364 void trace_find_cmdline(int pid, char comm[])
1365 {
1366 unsigned map;
1367
1368 if (!pid) {
1369 strcpy(comm, "<idle>");
1370 return;
1371 }
1372
1373 if (WARN_ON_ONCE(pid < 0)) {
1374 strcpy(comm, "<XXX>");
1375 return;
1376 }
1377
1378 if (pid > PID_MAX_DEFAULT) {
1379 strcpy(comm, "<...>");
1380 return;
1381 }
1382
1383 preempt_disable();
1384 arch_spin_lock(&trace_cmdline_lock);
1385 map = map_pid_to_cmdline[pid];
1386 if (map != NO_CMDLINE_MAP)
1387 strcpy(comm, saved_cmdlines[map]);
1388 else
1389 strcpy(comm, "<...>");
1390
1391 arch_spin_unlock(&trace_cmdline_lock);
1392 preempt_enable();
1393 }
1394
trace_find_tgid(int pid)1395 int trace_find_tgid(int pid)
1396 {
1397 unsigned map;
1398 int tgid;
1399
1400 preempt_disable();
1401 arch_spin_lock(&trace_cmdline_lock);
1402 map = map_pid_to_cmdline[pid];
1403 if (map != NO_CMDLINE_MAP)
1404 tgid = saved_tgids[map];
1405 else
1406 tgid = -1;
1407
1408 arch_spin_unlock(&trace_cmdline_lock);
1409 preempt_enable();
1410
1411 return tgid;
1412 }
1413
tracing_record_cmdline(struct task_struct * tsk)1414 void tracing_record_cmdline(struct task_struct *tsk)
1415 {
1416 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1417 return;
1418
1419 if (!__this_cpu_read(trace_cmdline_save))
1420 return;
1421
1422 __this_cpu_write(trace_cmdline_save, false);
1423
1424 trace_save_cmdline(tsk);
1425 }
1426
1427 void
tracing_generic_entry_update(struct trace_entry * entry,unsigned long flags,int pc)1428 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1429 int pc)
1430 {
1431 struct task_struct *tsk = current;
1432
1433 entry->preempt_count = pc & 0xff;
1434 entry->pid = (tsk) ? tsk->pid : 0;
1435 entry->flags =
1436 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1437 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1438 #else
1439 TRACE_FLAG_IRQS_NOSUPPORT |
1440 #endif
1441 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1442 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1443 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1444 }
1445 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1446
1447 struct ring_buffer_event *
trace_buffer_lock_reserve(struct ring_buffer * buffer,int type,unsigned long len,unsigned long flags,int pc)1448 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1449 int type,
1450 unsigned long len,
1451 unsigned long flags, int pc)
1452 {
1453 struct ring_buffer_event *event;
1454
1455 event = ring_buffer_lock_reserve(buffer, len);
1456 if (event != NULL) {
1457 struct trace_entry *ent = ring_buffer_event_data(event);
1458
1459 tracing_generic_entry_update(ent, flags, pc);
1460 ent->type = type;
1461 }
1462
1463 return event;
1464 }
1465
1466 void
__buffer_unlock_commit(struct ring_buffer * buffer,struct ring_buffer_event * event)1467 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1468 {
1469 __this_cpu_write(trace_cmdline_save, true);
1470 ring_buffer_unlock_commit(buffer, event);
1471 }
1472
1473 static inline void
__trace_buffer_unlock_commit(struct ring_buffer * buffer,struct ring_buffer_event * event,unsigned long flags,int pc)1474 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1475 struct ring_buffer_event *event,
1476 unsigned long flags, int pc)
1477 {
1478 __buffer_unlock_commit(buffer, event);
1479
1480 ftrace_trace_stack(buffer, flags, 6, pc);
1481 ftrace_trace_userstack(buffer, flags, pc);
1482 }
1483
trace_buffer_unlock_commit(struct ring_buffer * buffer,struct ring_buffer_event * event,unsigned long flags,int pc)1484 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1485 struct ring_buffer_event *event,
1486 unsigned long flags, int pc)
1487 {
1488 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1489 }
1490 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1491
1492 struct ring_buffer_event *
trace_event_buffer_lock_reserve(struct ring_buffer ** current_rb,struct ftrace_event_file * ftrace_file,int type,unsigned long len,unsigned long flags,int pc)1493 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1494 struct ftrace_event_file *ftrace_file,
1495 int type, unsigned long len,
1496 unsigned long flags, int pc)
1497 {
1498 *current_rb = ftrace_file->tr->trace_buffer.buffer;
1499 return trace_buffer_lock_reserve(*current_rb,
1500 type, len, flags, pc);
1501 }
1502 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1503
1504 struct ring_buffer_event *
trace_current_buffer_lock_reserve(struct ring_buffer ** current_rb,int type,unsigned long len,unsigned long flags,int pc)1505 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1506 int type, unsigned long len,
1507 unsigned long flags, int pc)
1508 {
1509 *current_rb = global_trace.trace_buffer.buffer;
1510 return trace_buffer_lock_reserve(*current_rb,
1511 type, len, flags, pc);
1512 }
1513 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1514
trace_current_buffer_unlock_commit(struct ring_buffer * buffer,struct ring_buffer_event * event,unsigned long flags,int pc)1515 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1516 struct ring_buffer_event *event,
1517 unsigned long flags, int pc)
1518 {
1519 __trace_buffer_unlock_commit(buffer, event, flags, pc);
1520 }
1521 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1522
trace_buffer_unlock_commit_regs(struct ring_buffer * buffer,struct ring_buffer_event * event,unsigned long flags,int pc,struct pt_regs * regs)1523 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1524 struct ring_buffer_event *event,
1525 unsigned long flags, int pc,
1526 struct pt_regs *regs)
1527 {
1528 __buffer_unlock_commit(buffer, event);
1529
1530 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1531 ftrace_trace_userstack(buffer, flags, pc);
1532 }
1533 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1534
trace_current_buffer_discard_commit(struct ring_buffer * buffer,struct ring_buffer_event * event)1535 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1536 struct ring_buffer_event *event)
1537 {
1538 ring_buffer_discard_commit(buffer, event);
1539 }
1540 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1541
1542 void
trace_function(struct trace_array * tr,unsigned long ip,unsigned long parent_ip,unsigned long flags,int pc)1543 trace_function(struct trace_array *tr,
1544 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1545 int pc)
1546 {
1547 struct ftrace_event_call *call = &event_function;
1548 struct ring_buffer *buffer = tr->trace_buffer.buffer;
1549 struct ring_buffer_event *event;
1550 struct ftrace_entry *entry;
1551
1552 /* If we are reading the ring buffer, don't trace */
1553 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1554 return;
1555
1556 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1557 flags, pc);
1558 if (!event)
1559 return;
1560 entry = ring_buffer_event_data(event);
1561 entry->ip = ip;
1562 entry->parent_ip = parent_ip;
1563
1564 if (!filter_check_discard(call, entry, buffer, event))
1565 __buffer_unlock_commit(buffer, event);
1566 }
1567
1568 void
ftrace(struct trace_array * tr,struct trace_array_cpu * data,unsigned long ip,unsigned long parent_ip,unsigned long flags,int pc)1569 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1570 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1571 int pc)
1572 {
1573 if (likely(!atomic_read(&data->disabled)))
1574 trace_function(tr, ip, parent_ip, flags, pc);
1575 }
1576
1577 #ifdef CONFIG_STACKTRACE
1578
1579 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1580 struct ftrace_stack {
1581 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1582 };
1583
1584 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1585 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1586
__ftrace_trace_stack(struct ring_buffer * buffer,unsigned long flags,int skip,int pc,struct pt_regs * regs)1587 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1588 unsigned long flags,
1589 int skip, int pc, struct pt_regs *regs)
1590 {
1591 struct ftrace_event_call *call = &event_kernel_stack;
1592 struct ring_buffer_event *event;
1593 struct stack_entry *entry;
1594 struct stack_trace trace;
1595 int use_stack;
1596 int size = FTRACE_STACK_ENTRIES;
1597
1598 trace.nr_entries = 0;
1599 trace.skip = skip;
1600
1601 /*
1602 * Since events can happen in NMIs there's no safe way to
1603 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1604 * or NMI comes in, it will just have to use the default
1605 * FTRACE_STACK_SIZE.
1606 */
1607 preempt_disable_notrace();
1608
1609 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1610 /*
1611 * We don't need any atomic variables, just a barrier.
1612 * If an interrupt comes in, we don't care, because it would
1613 * have exited and put the counter back to what we want.
1614 * We just need a barrier to keep gcc from moving things
1615 * around.
1616 */
1617 barrier();
1618 if (use_stack == 1) {
1619 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1620 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1621
1622 if (regs)
1623 save_stack_trace_regs(regs, &trace);
1624 else
1625 save_stack_trace(&trace);
1626
1627 if (trace.nr_entries > size)
1628 size = trace.nr_entries;
1629 } else
1630 /* From now on, use_stack is a boolean */
1631 use_stack = 0;
1632
1633 size *= sizeof(unsigned long);
1634
1635 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1636 sizeof(*entry) + size, flags, pc);
1637 if (!event)
1638 goto out;
1639 entry = ring_buffer_event_data(event);
1640
1641 memset(&entry->caller, 0, size);
1642
1643 if (use_stack)
1644 memcpy(&entry->caller, trace.entries,
1645 trace.nr_entries * sizeof(unsigned long));
1646 else {
1647 trace.max_entries = FTRACE_STACK_ENTRIES;
1648 trace.entries = entry->caller;
1649 if (regs)
1650 save_stack_trace_regs(regs, &trace);
1651 else
1652 save_stack_trace(&trace);
1653 }
1654
1655 entry->size = trace.nr_entries;
1656
1657 if (!filter_check_discard(call, entry, buffer, event))
1658 __buffer_unlock_commit(buffer, event);
1659
1660 out:
1661 /* Again, don't let gcc optimize things here */
1662 barrier();
1663 __this_cpu_dec(ftrace_stack_reserve);
1664 preempt_enable_notrace();
1665
1666 }
1667
ftrace_trace_stack_regs(struct ring_buffer * buffer,unsigned long flags,int skip,int pc,struct pt_regs * regs)1668 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1669 int skip, int pc, struct pt_regs *regs)
1670 {
1671 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1672 return;
1673
1674 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1675 }
1676
ftrace_trace_stack(struct ring_buffer * buffer,unsigned long flags,int skip,int pc)1677 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1678 int skip, int pc)
1679 {
1680 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1681 return;
1682
1683 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1684 }
1685
__trace_stack(struct trace_array * tr,unsigned long flags,int skip,int pc)1686 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1687 int pc)
1688 {
1689 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
1690 }
1691
1692 /**
1693 * trace_dump_stack - record a stack back trace in the trace buffer
1694 * @skip: Number of functions to skip (helper handlers)
1695 */
trace_dump_stack(int skip)1696 void trace_dump_stack(int skip)
1697 {
1698 unsigned long flags;
1699
1700 if (tracing_disabled || tracing_selftest_running)
1701 return;
1702
1703 local_save_flags(flags);
1704
1705 /*
1706 * Skip 3 more, seems to get us at the caller of
1707 * this function.
1708 */
1709 skip += 3;
1710 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1711 flags, skip, preempt_count(), NULL);
1712 }
1713
1714 static DEFINE_PER_CPU(int, user_stack_count);
1715
1716 void
ftrace_trace_userstack(struct ring_buffer * buffer,unsigned long flags,int pc)1717 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1718 {
1719 struct ftrace_event_call *call = &event_user_stack;
1720 struct ring_buffer_event *event;
1721 struct userstack_entry *entry;
1722 struct stack_trace trace;
1723
1724 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1725 return;
1726
1727 /*
1728 * NMIs can not handle page faults, even with fix ups.
1729 * The save user stack can (and often does) fault.
1730 */
1731 if (unlikely(in_nmi()))
1732 return;
1733
1734 /*
1735 * prevent recursion, since the user stack tracing may
1736 * trigger other kernel events.
1737 */
1738 preempt_disable();
1739 if (__this_cpu_read(user_stack_count))
1740 goto out;
1741
1742 __this_cpu_inc(user_stack_count);
1743
1744 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1745 sizeof(*entry), flags, pc);
1746 if (!event)
1747 goto out_drop_count;
1748 entry = ring_buffer_event_data(event);
1749
1750 entry->tgid = current->tgid;
1751 memset(&entry->caller, 0, sizeof(entry->caller));
1752
1753 trace.nr_entries = 0;
1754 trace.max_entries = FTRACE_STACK_ENTRIES;
1755 trace.skip = 0;
1756 trace.entries = entry->caller;
1757
1758 save_stack_trace_user(&trace);
1759 if (!filter_check_discard(call, entry, buffer, event))
1760 __buffer_unlock_commit(buffer, event);
1761
1762 out_drop_count:
1763 __this_cpu_dec(user_stack_count);
1764 out:
1765 preempt_enable();
1766 }
1767
1768 #ifdef UNUSED
__trace_userstack(struct trace_array * tr,unsigned long flags)1769 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1770 {
1771 ftrace_trace_userstack(tr, flags, preempt_count());
1772 }
1773 #endif /* UNUSED */
1774
1775 #endif /* CONFIG_STACKTRACE */
1776
1777 /* created for use with alloc_percpu */
1778 struct trace_buffer_struct {
1779 char buffer[TRACE_BUF_SIZE];
1780 };
1781
1782 static struct trace_buffer_struct *trace_percpu_buffer;
1783 static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1784 static struct trace_buffer_struct *trace_percpu_irq_buffer;
1785 static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1786
1787 /*
1788 * The buffer used is dependent on the context. There is a per cpu
1789 * buffer for normal context, softirq contex, hard irq context and
1790 * for NMI context. Thise allows for lockless recording.
1791 *
1792 * Note, if the buffers failed to be allocated, then this returns NULL
1793 */
get_trace_buf(void)1794 static char *get_trace_buf(void)
1795 {
1796 struct trace_buffer_struct *percpu_buffer;
1797
1798 /*
1799 * If we have allocated per cpu buffers, then we do not
1800 * need to do any locking.
1801 */
1802 if (in_nmi())
1803 percpu_buffer = trace_percpu_nmi_buffer;
1804 else if (in_irq())
1805 percpu_buffer = trace_percpu_irq_buffer;
1806 else if (in_softirq())
1807 percpu_buffer = trace_percpu_sirq_buffer;
1808 else
1809 percpu_buffer = trace_percpu_buffer;
1810
1811 if (!percpu_buffer)
1812 return NULL;
1813
1814 return this_cpu_ptr(&percpu_buffer->buffer[0]);
1815 }
1816
alloc_percpu_trace_buffer(void)1817 static int alloc_percpu_trace_buffer(void)
1818 {
1819 struct trace_buffer_struct *buffers;
1820 struct trace_buffer_struct *sirq_buffers;
1821 struct trace_buffer_struct *irq_buffers;
1822 struct trace_buffer_struct *nmi_buffers;
1823
1824 buffers = alloc_percpu(struct trace_buffer_struct);
1825 if (!buffers)
1826 goto err_warn;
1827
1828 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1829 if (!sirq_buffers)
1830 goto err_sirq;
1831
1832 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1833 if (!irq_buffers)
1834 goto err_irq;
1835
1836 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1837 if (!nmi_buffers)
1838 goto err_nmi;
1839
1840 trace_percpu_buffer = buffers;
1841 trace_percpu_sirq_buffer = sirq_buffers;
1842 trace_percpu_irq_buffer = irq_buffers;
1843 trace_percpu_nmi_buffer = nmi_buffers;
1844
1845 return 0;
1846
1847 err_nmi:
1848 free_percpu(irq_buffers);
1849 err_irq:
1850 free_percpu(sirq_buffers);
1851 err_sirq:
1852 free_percpu(buffers);
1853 err_warn:
1854 WARN(1, "Could not allocate percpu trace_printk buffer");
1855 return -ENOMEM;
1856 }
1857
1858 static int buffers_allocated;
1859
trace_printk_init_buffers(void)1860 void trace_printk_init_buffers(void)
1861 {
1862 if (buffers_allocated)
1863 return;
1864
1865 if (alloc_percpu_trace_buffer())
1866 return;
1867
1868 pr_info("ftrace: Allocated trace_printk buffers\n");
1869
1870 /* Expand the buffers to set size */
1871 tracing_update_buffers();
1872
1873 buffers_allocated = 1;
1874
1875 /*
1876 * trace_printk_init_buffers() can be called by modules.
1877 * If that happens, then we need to start cmdline recording
1878 * directly here. If the global_trace.buffer is already
1879 * allocated here, then this was called by module code.
1880 */
1881 if (global_trace.trace_buffer.buffer)
1882 tracing_start_cmdline_record();
1883 }
1884
trace_printk_start_comm(void)1885 void trace_printk_start_comm(void)
1886 {
1887 /* Start tracing comms if trace printk is set */
1888 if (!buffers_allocated)
1889 return;
1890 tracing_start_cmdline_record();
1891 }
1892
trace_printk_start_stop_comm(int enabled)1893 static void trace_printk_start_stop_comm(int enabled)
1894 {
1895 if (!buffers_allocated)
1896 return;
1897
1898 if (enabled)
1899 tracing_start_cmdline_record();
1900 else
1901 tracing_stop_cmdline_record();
1902 }
1903
1904 /**
1905 * trace_vbprintk - write binary msg to tracing buffer
1906 *
1907 */
trace_vbprintk(unsigned long ip,const char * fmt,va_list args)1908 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1909 {
1910 struct ftrace_event_call *call = &event_bprint;
1911 struct ring_buffer_event *event;
1912 struct ring_buffer *buffer;
1913 struct trace_array *tr = &global_trace;
1914 struct bprint_entry *entry;
1915 unsigned long flags;
1916 char *tbuffer;
1917 int len = 0, size, pc;
1918
1919 if (unlikely(tracing_selftest_running || tracing_disabled))
1920 return 0;
1921
1922 /* Don't pollute graph traces with trace_vprintk internals */
1923 pause_graph_tracing();
1924
1925 pc = preempt_count();
1926 preempt_disable_notrace();
1927
1928 tbuffer = get_trace_buf();
1929 if (!tbuffer) {
1930 len = 0;
1931 goto out;
1932 }
1933
1934 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1935
1936 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1937 goto out;
1938
1939 local_save_flags(flags);
1940 size = sizeof(*entry) + sizeof(u32) * len;
1941 buffer = tr->trace_buffer.buffer;
1942 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1943 flags, pc);
1944 if (!event)
1945 goto out;
1946 entry = ring_buffer_event_data(event);
1947 entry->ip = ip;
1948 entry->fmt = fmt;
1949
1950 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
1951 if (!filter_check_discard(call, entry, buffer, event)) {
1952 __buffer_unlock_commit(buffer, event);
1953 ftrace_trace_stack(buffer, flags, 6, pc);
1954 }
1955
1956 out:
1957 preempt_enable_notrace();
1958 unpause_graph_tracing();
1959
1960 return len;
1961 }
1962 EXPORT_SYMBOL_GPL(trace_vbprintk);
1963
1964 static int
__trace_array_vprintk(struct ring_buffer * buffer,unsigned long ip,const char * fmt,va_list args)1965 __trace_array_vprintk(struct ring_buffer *buffer,
1966 unsigned long ip, const char *fmt, va_list args)
1967 {
1968 struct ftrace_event_call *call = &event_print;
1969 struct ring_buffer_event *event;
1970 int len = 0, size, pc;
1971 struct print_entry *entry;
1972 unsigned long flags;
1973 char *tbuffer;
1974
1975 if (tracing_disabled || tracing_selftest_running)
1976 return 0;
1977
1978 /* Don't pollute graph traces with trace_vprintk internals */
1979 pause_graph_tracing();
1980
1981 pc = preempt_count();
1982 preempt_disable_notrace();
1983
1984
1985 tbuffer = get_trace_buf();
1986 if (!tbuffer) {
1987 len = 0;
1988 goto out;
1989 }
1990
1991 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1992 if (len > TRACE_BUF_SIZE)
1993 goto out;
1994
1995 local_save_flags(flags);
1996 size = sizeof(*entry) + len + 1;
1997 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1998 flags, pc);
1999 if (!event)
2000 goto out;
2001 entry = ring_buffer_event_data(event);
2002 entry->ip = ip;
2003
2004 memcpy(&entry->buf, tbuffer, len);
2005 entry->buf[len] = '\0';
2006 if (!filter_check_discard(call, entry, buffer, event)) {
2007 __buffer_unlock_commit(buffer, event);
2008 ftrace_trace_stack(buffer, flags, 6, pc);
2009 }
2010 out:
2011 preempt_enable_notrace();
2012 unpause_graph_tracing();
2013
2014 return len;
2015 }
2016
trace_array_vprintk(struct trace_array * tr,unsigned long ip,const char * fmt,va_list args)2017 int trace_array_vprintk(struct trace_array *tr,
2018 unsigned long ip, const char *fmt, va_list args)
2019 {
2020 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2021 }
2022
trace_array_printk(struct trace_array * tr,unsigned long ip,const char * fmt,...)2023 int trace_array_printk(struct trace_array *tr,
2024 unsigned long ip, const char *fmt, ...)
2025 {
2026 int ret;
2027 va_list ap;
2028
2029 if (!(trace_flags & TRACE_ITER_PRINTK))
2030 return 0;
2031
2032 va_start(ap, fmt);
2033 ret = trace_array_vprintk(tr, ip, fmt, ap);
2034 va_end(ap);
2035 return ret;
2036 }
2037
trace_array_printk_buf(struct ring_buffer * buffer,unsigned long ip,const char * fmt,...)2038 int trace_array_printk_buf(struct ring_buffer *buffer,
2039 unsigned long ip, const char *fmt, ...)
2040 {
2041 int ret;
2042 va_list ap;
2043
2044 if (!(trace_flags & TRACE_ITER_PRINTK))
2045 return 0;
2046
2047 va_start(ap, fmt);
2048 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2049 va_end(ap);
2050 return ret;
2051 }
2052
trace_vprintk(unsigned long ip,const char * fmt,va_list args)2053 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2054 {
2055 return trace_array_vprintk(&global_trace, ip, fmt, args);
2056 }
2057 EXPORT_SYMBOL_GPL(trace_vprintk);
2058
trace_iterator_increment(struct trace_iterator * iter)2059 static void trace_iterator_increment(struct trace_iterator *iter)
2060 {
2061 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2062
2063 iter->idx++;
2064 if (buf_iter)
2065 ring_buffer_read(buf_iter, NULL);
2066 }
2067
2068 static struct trace_entry *
peek_next_entry(struct trace_iterator * iter,int cpu,u64 * ts,unsigned long * lost_events)2069 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2070 unsigned long *lost_events)
2071 {
2072 struct ring_buffer_event *event;
2073 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
2074
2075 if (buf_iter)
2076 event = ring_buffer_iter_peek(buf_iter, ts);
2077 else
2078 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
2079 lost_events);
2080
2081 if (event) {
2082 iter->ent_size = ring_buffer_event_length(event);
2083 return ring_buffer_event_data(event);
2084 }
2085 iter->ent_size = 0;
2086 return NULL;
2087 }
2088
2089 static struct trace_entry *
__find_next_entry(struct trace_iterator * iter,int * ent_cpu,unsigned long * missing_events,u64 * ent_ts)2090 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2091 unsigned long *missing_events, u64 *ent_ts)
2092 {
2093 struct ring_buffer *buffer = iter->trace_buffer->buffer;
2094 struct trace_entry *ent, *next = NULL;
2095 unsigned long lost_events = 0, next_lost = 0;
2096 int cpu_file = iter->cpu_file;
2097 u64 next_ts = 0, ts;
2098 int next_cpu = -1;
2099 int next_size = 0;
2100 int cpu;
2101
2102 /*
2103 * If we are in a per_cpu trace file, don't bother by iterating over
2104 * all cpu and peek directly.
2105 */
2106 if (cpu_file > RING_BUFFER_ALL_CPUS) {
2107 if (ring_buffer_empty_cpu(buffer, cpu_file))
2108 return NULL;
2109 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
2110 if (ent_cpu)
2111 *ent_cpu = cpu_file;
2112
2113 return ent;
2114 }
2115
2116 for_each_tracing_cpu(cpu) {
2117
2118 if (ring_buffer_empty_cpu(buffer, cpu))
2119 continue;
2120
2121 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
2122
2123 /*
2124 * Pick the entry with the smallest timestamp:
2125 */
2126 if (ent && (!next || ts < next_ts)) {
2127 next = ent;
2128 next_cpu = cpu;
2129 next_ts = ts;
2130 next_lost = lost_events;
2131 next_size = iter->ent_size;
2132 }
2133 }
2134
2135 iter->ent_size = next_size;
2136
2137 if (ent_cpu)
2138 *ent_cpu = next_cpu;
2139
2140 if (ent_ts)
2141 *ent_ts = next_ts;
2142
2143 if (missing_events)
2144 *missing_events = next_lost;
2145
2146 return next;
2147 }
2148
2149 /* Find the next real entry, without updating the iterator itself */
trace_find_next_entry(struct trace_iterator * iter,int * ent_cpu,u64 * ent_ts)2150 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2151 int *ent_cpu, u64 *ent_ts)
2152 {
2153 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
2154 }
2155
2156 /* Find the next real entry, and increment the iterator to the next entry */
trace_find_next_entry_inc(struct trace_iterator * iter)2157 void *trace_find_next_entry_inc(struct trace_iterator *iter)
2158 {
2159 iter->ent = __find_next_entry(iter, &iter->cpu,
2160 &iter->lost_events, &iter->ts);
2161
2162 if (iter->ent)
2163 trace_iterator_increment(iter);
2164
2165 return iter->ent ? iter : NULL;
2166 }
2167
trace_consume(struct trace_iterator * iter)2168 static void trace_consume(struct trace_iterator *iter)
2169 {
2170 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
2171 &iter->lost_events);
2172 }
2173
s_next(struct seq_file * m,void * v,loff_t * pos)2174 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
2175 {
2176 struct trace_iterator *iter = m->private;
2177 int i = (int)*pos;
2178 void *ent;
2179
2180 WARN_ON_ONCE(iter->leftover);
2181
2182 (*pos)++;
2183
2184 /* can't go backwards */
2185 if (iter->idx > i)
2186 return NULL;
2187
2188 if (iter->idx < 0)
2189 ent = trace_find_next_entry_inc(iter);
2190 else
2191 ent = iter;
2192
2193 while (ent && iter->idx < i)
2194 ent = trace_find_next_entry_inc(iter);
2195
2196 iter->pos = *pos;
2197
2198 return ent;
2199 }
2200
tracing_iter_reset(struct trace_iterator * iter,int cpu)2201 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2202 {
2203 struct ring_buffer_event *event;
2204 struct ring_buffer_iter *buf_iter;
2205 unsigned long entries = 0;
2206 u64 ts;
2207
2208 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2209
2210 buf_iter = trace_buffer_iter(iter, cpu);
2211 if (!buf_iter)
2212 return;
2213
2214 ring_buffer_iter_reset(buf_iter);
2215
2216 /*
2217 * We could have the case with the max latency tracers
2218 * that a reset never took place on a cpu. This is evident
2219 * by the timestamp being before the start of the buffer.
2220 */
2221 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
2222 if (ts >= iter->trace_buffer->time_start)
2223 break;
2224 entries++;
2225 ring_buffer_read(buf_iter, NULL);
2226 }
2227
2228 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2229 }
2230
2231 /*
2232 * The current tracer is copied to avoid a global locking
2233 * all around.
2234 */
s_start(struct seq_file * m,loff_t * pos)2235 static void *s_start(struct seq_file *m, loff_t *pos)
2236 {
2237 struct trace_iterator *iter = m->private;
2238 struct trace_array *tr = iter->tr;
2239 int cpu_file = iter->cpu_file;
2240 void *p = NULL;
2241 loff_t l = 0;
2242 int cpu;
2243
2244 /*
2245 * copy the tracer to avoid using a global lock all around.
2246 * iter->trace is a copy of current_trace, the pointer to the
2247 * name may be used instead of a strcmp(), as iter->trace->name
2248 * will point to the same string as current_trace->name.
2249 */
2250 mutex_lock(&trace_types_lock);
2251 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2252 *iter->trace = *tr->current_trace;
2253 mutex_unlock(&trace_types_lock);
2254
2255 #ifdef CONFIG_TRACER_MAX_TRACE
2256 if (iter->snapshot && iter->trace->use_max_tr)
2257 return ERR_PTR(-EBUSY);
2258 #endif
2259
2260 if (!iter->snapshot)
2261 atomic_inc(&trace_record_cmdline_disabled);
2262
2263 if (*pos != iter->pos) {
2264 iter->ent = NULL;
2265 iter->cpu = 0;
2266 iter->idx = -1;
2267
2268 if (cpu_file == RING_BUFFER_ALL_CPUS) {
2269 for_each_tracing_cpu(cpu)
2270 tracing_iter_reset(iter, cpu);
2271 } else
2272 tracing_iter_reset(iter, cpu_file);
2273
2274 iter->leftover = 0;
2275 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2276 ;
2277
2278 } else {
2279 /*
2280 * If we overflowed the seq_file before, then we want
2281 * to just reuse the trace_seq buffer again.
2282 */
2283 if (iter->leftover)
2284 p = iter;
2285 else {
2286 l = *pos - 1;
2287 p = s_next(m, p, &l);
2288 }
2289 }
2290
2291 trace_event_read_lock();
2292 trace_access_lock(cpu_file);
2293 return p;
2294 }
2295
s_stop(struct seq_file * m,void * p)2296 static void s_stop(struct seq_file *m, void *p)
2297 {
2298 struct trace_iterator *iter = m->private;
2299
2300 #ifdef CONFIG_TRACER_MAX_TRACE
2301 if (iter->snapshot && iter->trace->use_max_tr)
2302 return;
2303 #endif
2304
2305 if (!iter->snapshot)
2306 atomic_dec(&trace_record_cmdline_disabled);
2307
2308 trace_access_unlock(iter->cpu_file);
2309 trace_event_read_unlock();
2310 }
2311
2312 static void
get_total_entries(struct trace_buffer * buf,unsigned long * total,unsigned long * entries)2313 get_total_entries(struct trace_buffer *buf,
2314 unsigned long *total, unsigned long *entries)
2315 {
2316 unsigned long count;
2317 int cpu;
2318
2319 *total = 0;
2320 *entries = 0;
2321
2322 for_each_tracing_cpu(cpu) {
2323 count = ring_buffer_entries_cpu(buf->buffer, cpu);
2324 /*
2325 * If this buffer has skipped entries, then we hold all
2326 * entries for the trace and we need to ignore the
2327 * ones before the time stamp.
2328 */
2329 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2330 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
2331 /* total is the same as the entries */
2332 *total += count;
2333 } else
2334 *total += count +
2335 ring_buffer_overrun_cpu(buf->buffer, cpu);
2336 *entries += count;
2337 }
2338 }
2339
print_lat_help_header(struct seq_file * m)2340 static void print_lat_help_header(struct seq_file *m)
2341 {
2342 seq_puts(m, "# _------=> CPU# \n");
2343 seq_puts(m, "# / _-----=> irqs-off \n");
2344 seq_puts(m, "# | / _----=> need-resched \n");
2345 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2346 seq_puts(m, "# ||| / _--=> preempt-depth \n");
2347 seq_puts(m, "# |||| / delay \n");
2348 seq_puts(m, "# cmd pid ||||| time | caller \n");
2349 seq_puts(m, "# \\ / ||||| \\ | / \n");
2350 }
2351
print_event_info(struct trace_buffer * buf,struct seq_file * m)2352 static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
2353 {
2354 unsigned long total;
2355 unsigned long entries;
2356
2357 get_total_entries(buf, &total, &entries);
2358 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2359 entries, total, num_online_cpus());
2360 seq_puts(m, "#\n");
2361 }
2362
print_func_help_header(struct trace_buffer * buf,struct seq_file * m)2363 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
2364 {
2365 print_event_info(buf, m);
2366 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2367 seq_puts(m, "# | | | | |\n");
2368 }
2369
print_func_help_header_tgid(struct trace_buffer * buf,struct seq_file * m)2370 static void print_func_help_header_tgid(struct trace_buffer *buf, struct seq_file *m)
2371 {
2372 print_event_info(buf, m);
2373 seq_puts(m, "# TASK-PID TGID CPU# TIMESTAMP FUNCTION\n");
2374 seq_puts(m, "# | | | | | |\n");
2375 }
2376
print_func_help_header_irq(struct trace_buffer * buf,struct seq_file * m)2377 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
2378 {
2379 print_event_info(buf, m);
2380 seq_puts(m, "# _-----=> irqs-off\n");
2381 seq_puts(m, "# / _----=> need-resched\n");
2382 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2383 seq_puts(m, "# || / _--=> preempt-depth\n");
2384 seq_puts(m, "# ||| / delay\n");
2385 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2386 seq_puts(m, "# | | | |||| | |\n");
2387 }
2388
print_func_help_header_irq_tgid(struct trace_buffer * buf,struct seq_file * m)2389 static void print_func_help_header_irq_tgid(struct trace_buffer *buf, struct seq_file *m)
2390 {
2391 print_event_info(buf, m);
2392 seq_puts(m, "# _-----=> irqs-off\n");
2393 seq_puts(m, "# / _----=> need-resched\n");
2394 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2395 seq_puts(m, "# || / _--=> preempt-depth\n");
2396 seq_puts(m, "# ||| / delay\n");
2397 seq_puts(m, "# TASK-PID TGID CPU# |||| TIMESTAMP FUNCTION\n");
2398 seq_puts(m, "# | | | | |||| | |\n");
2399 }
2400
2401 void
print_trace_header(struct seq_file * m,struct trace_iterator * iter)2402 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2403 {
2404 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2405 struct trace_buffer *buf = iter->trace_buffer;
2406 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2407 struct tracer *type = iter->trace;
2408 unsigned long entries;
2409 unsigned long total;
2410 const char *name = "preemption";
2411
2412 name = type->name;
2413
2414 get_total_entries(buf, &total, &entries);
2415
2416 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2417 name, UTS_RELEASE);
2418 seq_puts(m, "# -----------------------------------"
2419 "---------------------------------\n");
2420 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2421 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2422 nsecs_to_usecs(data->saved_latency),
2423 entries,
2424 total,
2425 buf->cpu,
2426 #if defined(CONFIG_PREEMPT_NONE)
2427 "server",
2428 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2429 "desktop",
2430 #elif defined(CONFIG_PREEMPT)
2431 "preempt",
2432 #else
2433 "unknown",
2434 #endif
2435 /* These are reserved for later use */
2436 0, 0, 0, 0);
2437 #ifdef CONFIG_SMP
2438 seq_printf(m, " #P:%d)\n", num_online_cpus());
2439 #else
2440 seq_puts(m, ")\n");
2441 #endif
2442 seq_puts(m, "# -----------------\n");
2443 seq_printf(m, "# | task: %.16s-%d "
2444 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2445 data->comm, data->pid,
2446 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2447 data->policy, data->rt_priority);
2448 seq_puts(m, "# -----------------\n");
2449
2450 if (data->critical_start) {
2451 seq_puts(m, "# => started at: ");
2452 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2453 trace_print_seq(m, &iter->seq);
2454 seq_puts(m, "\n# => ended at: ");
2455 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2456 trace_print_seq(m, &iter->seq);
2457 seq_puts(m, "\n#\n");
2458 }
2459
2460 seq_puts(m, "#\n");
2461 }
2462
test_cpu_buff_start(struct trace_iterator * iter)2463 static void test_cpu_buff_start(struct trace_iterator *iter)
2464 {
2465 struct trace_seq *s = &iter->seq;
2466
2467 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2468 return;
2469
2470 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2471 return;
2472
2473 if (cpumask_test_cpu(iter->cpu, iter->started))
2474 return;
2475
2476 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2477 return;
2478
2479 cpumask_set_cpu(iter->cpu, iter->started);
2480
2481 /* Don't print started cpu buffer for the first entry of the trace */
2482 if (iter->idx > 1)
2483 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2484 iter->cpu);
2485 }
2486
print_trace_fmt(struct trace_iterator * iter)2487 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2488 {
2489 struct trace_seq *s = &iter->seq;
2490 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2491 struct trace_entry *entry;
2492 struct trace_event *event;
2493
2494 entry = iter->ent;
2495
2496 test_cpu_buff_start(iter);
2497
2498 event = ftrace_find_event(entry->type);
2499
2500 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2501 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2502 if (!trace_print_lat_context(iter))
2503 goto partial;
2504 } else {
2505 if (!trace_print_context(iter))
2506 goto partial;
2507 }
2508 }
2509
2510 if (event)
2511 return event->funcs->trace(iter, sym_flags, event);
2512
2513 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2514 goto partial;
2515
2516 return TRACE_TYPE_HANDLED;
2517 partial:
2518 return TRACE_TYPE_PARTIAL_LINE;
2519 }
2520
print_raw_fmt(struct trace_iterator * iter)2521 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2522 {
2523 struct trace_seq *s = &iter->seq;
2524 struct trace_entry *entry;
2525 struct trace_event *event;
2526
2527 entry = iter->ent;
2528
2529 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2530 if (!trace_seq_printf(s, "%d %d %llu ",
2531 entry->pid, iter->cpu, iter->ts))
2532 goto partial;
2533 }
2534
2535 event = ftrace_find_event(entry->type);
2536 if (event)
2537 return event->funcs->raw(iter, 0, event);
2538
2539 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2540 goto partial;
2541
2542 return TRACE_TYPE_HANDLED;
2543 partial:
2544 return TRACE_TYPE_PARTIAL_LINE;
2545 }
2546
print_hex_fmt(struct trace_iterator * iter)2547 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2548 {
2549 struct trace_seq *s = &iter->seq;
2550 unsigned char newline = '\n';
2551 struct trace_entry *entry;
2552 struct trace_event *event;
2553
2554 entry = iter->ent;
2555
2556 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2557 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2558 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2559 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2560 }
2561
2562 event = ftrace_find_event(entry->type);
2563 if (event) {
2564 enum print_line_t ret = event->funcs->hex(iter, 0, event);
2565 if (ret != TRACE_TYPE_HANDLED)
2566 return ret;
2567 }
2568
2569 SEQ_PUT_FIELD_RET(s, newline);
2570
2571 return TRACE_TYPE_HANDLED;
2572 }
2573
print_bin_fmt(struct trace_iterator * iter)2574 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2575 {
2576 struct trace_seq *s = &iter->seq;
2577 struct trace_entry *entry;
2578 struct trace_event *event;
2579
2580 entry = iter->ent;
2581
2582 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2583 SEQ_PUT_FIELD_RET(s, entry->pid);
2584 SEQ_PUT_FIELD_RET(s, iter->cpu);
2585 SEQ_PUT_FIELD_RET(s, iter->ts);
2586 }
2587
2588 event = ftrace_find_event(entry->type);
2589 return event ? event->funcs->binary(iter, 0, event) :
2590 TRACE_TYPE_HANDLED;
2591 }
2592
trace_empty(struct trace_iterator * iter)2593 int trace_empty(struct trace_iterator *iter)
2594 {
2595 struct ring_buffer_iter *buf_iter;
2596 int cpu;
2597
2598 /* If we are looking at one CPU buffer, only check that one */
2599 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2600 cpu = iter->cpu_file;
2601 buf_iter = trace_buffer_iter(iter, cpu);
2602 if (buf_iter) {
2603 if (!ring_buffer_iter_empty(buf_iter))
2604 return 0;
2605 } else {
2606 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2607 return 0;
2608 }
2609 return 1;
2610 }
2611
2612 for_each_tracing_cpu(cpu) {
2613 buf_iter = trace_buffer_iter(iter, cpu);
2614 if (buf_iter) {
2615 if (!ring_buffer_iter_empty(buf_iter))
2616 return 0;
2617 } else {
2618 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2619 return 0;
2620 }
2621 }
2622
2623 return 1;
2624 }
2625
2626 /* Called with trace_event_read_lock() held. */
print_trace_line(struct trace_iterator * iter)2627 enum print_line_t print_trace_line(struct trace_iterator *iter)
2628 {
2629 enum print_line_t ret;
2630
2631 if (iter->lost_events &&
2632 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2633 iter->cpu, iter->lost_events))
2634 return TRACE_TYPE_PARTIAL_LINE;
2635
2636 if (iter->trace && iter->trace->print_line) {
2637 ret = iter->trace->print_line(iter);
2638 if (ret != TRACE_TYPE_UNHANDLED)
2639 return ret;
2640 }
2641
2642 if (iter->ent->type == TRACE_BPUTS &&
2643 trace_flags & TRACE_ITER_PRINTK &&
2644 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2645 return trace_print_bputs_msg_only(iter);
2646
2647 if (iter->ent->type == TRACE_BPRINT &&
2648 trace_flags & TRACE_ITER_PRINTK &&
2649 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2650 return trace_print_bprintk_msg_only(iter);
2651
2652 if (iter->ent->type == TRACE_PRINT &&
2653 trace_flags & TRACE_ITER_PRINTK &&
2654 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2655 return trace_print_printk_msg_only(iter);
2656
2657 if (trace_flags & TRACE_ITER_BIN)
2658 return print_bin_fmt(iter);
2659
2660 if (trace_flags & TRACE_ITER_HEX)
2661 return print_hex_fmt(iter);
2662
2663 if (trace_flags & TRACE_ITER_RAW)
2664 return print_raw_fmt(iter);
2665
2666 return print_trace_fmt(iter);
2667 }
2668
trace_latency_header(struct seq_file * m)2669 void trace_latency_header(struct seq_file *m)
2670 {
2671 struct trace_iterator *iter = m->private;
2672
2673 /* print nothing if the buffers are empty */
2674 if (trace_empty(iter))
2675 return;
2676
2677 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2678 print_trace_header(m, iter);
2679
2680 if (!(trace_flags & TRACE_ITER_VERBOSE))
2681 print_lat_help_header(m);
2682 }
2683
trace_default_header(struct seq_file * m)2684 void trace_default_header(struct seq_file *m)
2685 {
2686 struct trace_iterator *iter = m->private;
2687
2688 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2689 return;
2690
2691 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2692 /* print nothing if the buffers are empty */
2693 if (trace_empty(iter))
2694 return;
2695 print_trace_header(m, iter);
2696 if (!(trace_flags & TRACE_ITER_VERBOSE))
2697 print_lat_help_header(m);
2698 } else {
2699 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2700 if (trace_flags & TRACE_ITER_IRQ_INFO)
2701 if (trace_flags & TRACE_ITER_TGID)
2702 print_func_help_header_irq_tgid(iter->trace_buffer, m);
2703 else
2704 print_func_help_header_irq(iter->trace_buffer, m);
2705 else
2706 if (trace_flags & TRACE_ITER_TGID)
2707 print_func_help_header_tgid(iter->trace_buffer, m);
2708 else
2709 print_func_help_header(iter->trace_buffer, m);
2710 }
2711 }
2712 }
2713
test_ftrace_alive(struct seq_file * m)2714 static void test_ftrace_alive(struct seq_file *m)
2715 {
2716 if (!ftrace_is_dead())
2717 return;
2718 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2719 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2720 }
2721
2722 #ifdef CONFIG_TRACER_MAX_TRACE
show_snapshot_main_help(struct seq_file * m)2723 static void show_snapshot_main_help(struct seq_file *m)
2724 {
2725 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2726 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2727 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2728 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2729 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2730 seq_printf(m, "# is not a '0' or '1')\n");
2731 }
2732
show_snapshot_percpu_help(struct seq_file * m)2733 static void show_snapshot_percpu_help(struct seq_file *m)
2734 {
2735 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2736 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2737 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2738 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2739 #else
2740 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2741 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2742 #endif
2743 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2744 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2745 seq_printf(m, "# is not a '0' or '1')\n");
2746 }
2747
print_snapshot_help(struct seq_file * m,struct trace_iterator * iter)2748 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2749 {
2750 if (iter->tr->allocated_snapshot)
2751 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2752 else
2753 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2754
2755 seq_printf(m, "# Snapshot commands:\n");
2756 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2757 show_snapshot_main_help(m);
2758 else
2759 show_snapshot_percpu_help(m);
2760 }
2761 #else
2762 /* Should never be called */
print_snapshot_help(struct seq_file * m,struct trace_iterator * iter)2763 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2764 #endif
2765
s_show(struct seq_file * m,void * v)2766 static int s_show(struct seq_file *m, void *v)
2767 {
2768 struct trace_iterator *iter = v;
2769 int ret;
2770
2771 if (iter->ent == NULL) {
2772 if (iter->tr) {
2773 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2774 seq_puts(m, "#\n");
2775 test_ftrace_alive(m);
2776 }
2777 if (iter->snapshot && trace_empty(iter))
2778 print_snapshot_help(m, iter);
2779 else if (iter->trace && iter->trace->print_header)
2780 iter->trace->print_header(m);
2781 else
2782 trace_default_header(m);
2783
2784 } else if (iter->leftover) {
2785 /*
2786 * If we filled the seq_file buffer earlier, we
2787 * want to just show it now.
2788 */
2789 ret = trace_print_seq(m, &iter->seq);
2790
2791 /* ret should this time be zero, but you never know */
2792 iter->leftover = ret;
2793
2794 } else {
2795 print_trace_line(iter);
2796 ret = trace_print_seq(m, &iter->seq);
2797 /*
2798 * If we overflow the seq_file buffer, then it will
2799 * ask us for this data again at start up.
2800 * Use that instead.
2801 * ret is 0 if seq_file write succeeded.
2802 * -1 otherwise.
2803 */
2804 iter->leftover = ret;
2805 }
2806
2807 return 0;
2808 }
2809
2810 static const struct seq_operations tracer_seq_ops = {
2811 .start = s_start,
2812 .next = s_next,
2813 .stop = s_stop,
2814 .show = s_show,
2815 };
2816
2817 static struct trace_iterator *
__tracing_open(struct inode * inode,struct file * file,bool snapshot)2818 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2819 {
2820 struct trace_cpu *tc = inode->i_private;
2821 struct trace_array *tr = tc->tr;
2822 struct trace_iterator *iter;
2823 int cpu;
2824
2825 if (tracing_disabled)
2826 return ERR_PTR(-ENODEV);
2827
2828 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2829 if (!iter)
2830 return ERR_PTR(-ENOMEM);
2831
2832 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2833 GFP_KERNEL);
2834 if (!iter->buffer_iter)
2835 goto release;
2836
2837 /*
2838 * We make a copy of the current tracer to avoid concurrent
2839 * changes on it while we are reading.
2840 */
2841 mutex_lock(&trace_types_lock);
2842 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2843 if (!iter->trace)
2844 goto fail;
2845
2846 *iter->trace = *tr->current_trace;
2847
2848 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2849 goto fail;
2850
2851 iter->tr = tr;
2852
2853 #ifdef CONFIG_TRACER_MAX_TRACE
2854 /* Currently only the top directory has a snapshot */
2855 if (tr->current_trace->print_max || snapshot)
2856 iter->trace_buffer = &tr->max_buffer;
2857 else
2858 #endif
2859 iter->trace_buffer = &tr->trace_buffer;
2860 iter->snapshot = snapshot;
2861 iter->pos = -1;
2862 mutex_init(&iter->mutex);
2863 iter->cpu_file = tc->cpu;
2864
2865 /* Notify the tracer early; before we stop tracing. */
2866 if (iter->trace && iter->trace->open)
2867 iter->trace->open(iter);
2868
2869 /* Annotate start of buffers if we had overruns */
2870 if (ring_buffer_overruns(iter->trace_buffer->buffer))
2871 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2872
2873 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2874 if (trace_clocks[tr->clock_id].in_ns)
2875 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2876
2877 /* stop the trace while dumping if we are not opening "snapshot" */
2878 if (!iter->snapshot)
2879 tracing_stop_tr(tr);
2880
2881 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
2882 for_each_tracing_cpu(cpu) {
2883 iter->buffer_iter[cpu] =
2884 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2885 }
2886 ring_buffer_read_prepare_sync();
2887 for_each_tracing_cpu(cpu) {
2888 ring_buffer_read_start(iter->buffer_iter[cpu]);
2889 tracing_iter_reset(iter, cpu);
2890 }
2891 } else {
2892 cpu = iter->cpu_file;
2893 iter->buffer_iter[cpu] =
2894 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
2895 ring_buffer_read_prepare_sync();
2896 ring_buffer_read_start(iter->buffer_iter[cpu]);
2897 tracing_iter_reset(iter, cpu);
2898 }
2899
2900 tr->ref++;
2901
2902 mutex_unlock(&trace_types_lock);
2903
2904 return iter;
2905
2906 fail:
2907 mutex_unlock(&trace_types_lock);
2908 kfree(iter->trace);
2909 kfree(iter->buffer_iter);
2910 release:
2911 seq_release_private(inode, file);
2912 return ERR_PTR(-ENOMEM);
2913 }
2914
tracing_open_generic(struct inode * inode,struct file * filp)2915 int tracing_open_generic(struct inode *inode, struct file *filp)
2916 {
2917 if (tracing_disabled)
2918 return -ENODEV;
2919
2920 filp->private_data = inode->i_private;
2921 return 0;
2922 }
2923
tracing_release(struct inode * inode,struct file * file)2924 static int tracing_release(struct inode *inode, struct file *file)
2925 {
2926 struct seq_file *m = file->private_data;
2927 struct trace_iterator *iter;
2928 struct trace_array *tr;
2929 int cpu;
2930
2931 if (!(file->f_mode & FMODE_READ))
2932 return 0;
2933
2934 iter = m->private;
2935 tr = iter->tr;
2936
2937 mutex_lock(&trace_types_lock);
2938
2939 WARN_ON(!tr->ref);
2940 tr->ref--;
2941
2942 for_each_tracing_cpu(cpu) {
2943 if (iter->buffer_iter[cpu])
2944 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2945 }
2946
2947 if (iter->trace && iter->trace->close)
2948 iter->trace->close(iter);
2949
2950 if (!iter->snapshot)
2951 /* reenable tracing if it was previously enabled */
2952 tracing_start_tr(tr);
2953 mutex_unlock(&trace_types_lock);
2954
2955 mutex_destroy(&iter->mutex);
2956 free_cpumask_var(iter->started);
2957 kfree(iter->trace);
2958 kfree(iter->buffer_iter);
2959 seq_release_private(inode, file);
2960 return 0;
2961 }
2962
tracing_open(struct inode * inode,struct file * file)2963 static int tracing_open(struct inode *inode, struct file *file)
2964 {
2965 struct trace_iterator *iter;
2966 int ret = 0;
2967
2968 /* If this file was open for write, then erase contents */
2969 if ((file->f_mode & FMODE_WRITE) &&
2970 (file->f_flags & O_TRUNC)) {
2971 struct trace_cpu *tc = inode->i_private;
2972 struct trace_array *tr = tc->tr;
2973
2974 if (tc->cpu == RING_BUFFER_ALL_CPUS)
2975 tracing_reset_online_cpus(&tr->trace_buffer);
2976 else
2977 tracing_reset(&tr->trace_buffer, tc->cpu);
2978 }
2979
2980 if (file->f_mode & FMODE_READ) {
2981 iter = __tracing_open(inode, file, false);
2982 if (IS_ERR(iter))
2983 ret = PTR_ERR(iter);
2984 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2985 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2986 }
2987 return ret;
2988 }
2989
2990 static void *
t_next(struct seq_file * m,void * v,loff_t * pos)2991 t_next(struct seq_file *m, void *v, loff_t *pos)
2992 {
2993 struct tracer *t = v;
2994
2995 (*pos)++;
2996
2997 if (t)
2998 t = t->next;
2999
3000 return t;
3001 }
3002
t_start(struct seq_file * m,loff_t * pos)3003 static void *t_start(struct seq_file *m, loff_t *pos)
3004 {
3005 struct tracer *t;
3006 loff_t l = 0;
3007
3008 mutex_lock(&trace_types_lock);
3009 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
3010 ;
3011
3012 return t;
3013 }
3014
t_stop(struct seq_file * m,void * p)3015 static void t_stop(struct seq_file *m, void *p)
3016 {
3017 mutex_unlock(&trace_types_lock);
3018 }
3019
t_show(struct seq_file * m,void * v)3020 static int t_show(struct seq_file *m, void *v)
3021 {
3022 struct tracer *t = v;
3023
3024 if (!t)
3025 return 0;
3026
3027 seq_printf(m, "%s", t->name);
3028 if (t->next)
3029 seq_putc(m, ' ');
3030 else
3031 seq_putc(m, '\n');
3032
3033 return 0;
3034 }
3035
3036 static const struct seq_operations show_traces_seq_ops = {
3037 .start = t_start,
3038 .next = t_next,
3039 .stop = t_stop,
3040 .show = t_show,
3041 };
3042
show_traces_open(struct inode * inode,struct file * file)3043 static int show_traces_open(struct inode *inode, struct file *file)
3044 {
3045 if (tracing_disabled)
3046 return -ENODEV;
3047
3048 return seq_open(file, &show_traces_seq_ops);
3049 }
3050
3051 static ssize_t
tracing_write_stub(struct file * filp,const char __user * ubuf,size_t count,loff_t * ppos)3052 tracing_write_stub(struct file *filp, const char __user *ubuf,
3053 size_t count, loff_t *ppos)
3054 {
3055 return count;
3056 }
3057
tracing_seek(struct file * file,loff_t offset,int origin)3058 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
3059 {
3060 if (file->f_mode & FMODE_READ)
3061 return seq_lseek(file, offset, origin);
3062 else
3063 return 0;
3064 }
3065
3066 static const struct file_operations tracing_fops = {
3067 .open = tracing_open,
3068 .read = seq_read,
3069 .write = tracing_write_stub,
3070 .llseek = tracing_seek,
3071 .release = tracing_release,
3072 };
3073
3074 static const struct file_operations show_traces_fops = {
3075 .open = show_traces_open,
3076 .read = seq_read,
3077 .release = seq_release,
3078 .llseek = seq_lseek,
3079 };
3080
3081 /*
3082 * Only trace on a CPU if the bitmask is set:
3083 */
3084 static cpumask_var_t tracing_cpumask;
3085
3086 /*
3087 * The tracer itself will not take this lock, but still we want
3088 * to provide a consistent cpumask to user-space:
3089 */
3090 static DEFINE_MUTEX(tracing_cpumask_update_lock);
3091
3092 /*
3093 * Temporary storage for the character representation of the
3094 * CPU bitmask (and one more byte for the newline):
3095 */
3096 static char mask_str[NR_CPUS + 1];
3097
3098 static ssize_t
tracing_cpumask_read(struct file * filp,char __user * ubuf,size_t count,loff_t * ppos)3099 tracing_cpumask_read(struct file *filp, char __user *ubuf,
3100 size_t count, loff_t *ppos)
3101 {
3102 int len;
3103
3104 mutex_lock(&tracing_cpumask_update_lock);
3105
3106 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
3107 if (count - len < 2) {
3108 count = -EINVAL;
3109 goto out_err;
3110 }
3111 len += sprintf(mask_str + len, "\n");
3112 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3113
3114 out_err:
3115 mutex_unlock(&tracing_cpumask_update_lock);
3116
3117 return count;
3118 }
3119
3120 static ssize_t
tracing_cpumask_write(struct file * filp,const char __user * ubuf,size_t count,loff_t * ppos)3121 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3122 size_t count, loff_t *ppos)
3123 {
3124 struct trace_array *tr = filp->private_data;
3125 cpumask_var_t tracing_cpumask_new;
3126 int err, cpu;
3127
3128 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3129 return -ENOMEM;
3130
3131 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
3132 if (err)
3133 goto err_unlock;
3134
3135 mutex_lock(&tracing_cpumask_update_lock);
3136
3137 local_irq_disable();
3138 arch_spin_lock(&ftrace_max_lock);
3139 for_each_tracing_cpu(cpu) {
3140 /*
3141 * Increase/decrease the disabled counter if we are
3142 * about to flip a bit in the cpumask:
3143 */
3144 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
3145 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3146 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3147 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
3148 }
3149 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
3150 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3151 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3152 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
3153 }
3154 }
3155 arch_spin_unlock(&ftrace_max_lock);
3156 local_irq_enable();
3157
3158 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
3159
3160 mutex_unlock(&tracing_cpumask_update_lock);
3161 free_cpumask_var(tracing_cpumask_new);
3162
3163 return count;
3164
3165 err_unlock:
3166 free_cpumask_var(tracing_cpumask_new);
3167
3168 return err;
3169 }
3170
3171 static const struct file_operations tracing_cpumask_fops = {
3172 .open = tracing_open_generic,
3173 .read = tracing_cpumask_read,
3174 .write = tracing_cpumask_write,
3175 .llseek = generic_file_llseek,
3176 };
3177
tracing_trace_options_show(struct seq_file * m,void * v)3178 static int tracing_trace_options_show(struct seq_file *m, void *v)
3179 {
3180 struct tracer_opt *trace_opts;
3181 struct trace_array *tr = m->private;
3182 u32 tracer_flags;
3183 int i;
3184
3185 mutex_lock(&trace_types_lock);
3186 tracer_flags = tr->current_trace->flags->val;
3187 trace_opts = tr->current_trace->flags->opts;
3188
3189 for (i = 0; trace_options[i]; i++) {
3190 if (trace_flags & (1 << i))
3191 seq_printf(m, "%s\n", trace_options[i]);
3192 else
3193 seq_printf(m, "no%s\n", trace_options[i]);
3194 }
3195
3196 for (i = 0; trace_opts[i].name; i++) {
3197 if (tracer_flags & trace_opts[i].bit)
3198 seq_printf(m, "%s\n", trace_opts[i].name);
3199 else
3200 seq_printf(m, "no%s\n", trace_opts[i].name);
3201 }
3202 mutex_unlock(&trace_types_lock);
3203
3204 return 0;
3205 }
3206
__set_tracer_option(struct tracer * trace,struct tracer_flags * tracer_flags,struct tracer_opt * opts,int neg)3207 static int __set_tracer_option(struct tracer *trace,
3208 struct tracer_flags *tracer_flags,
3209 struct tracer_opt *opts, int neg)
3210 {
3211 int ret;
3212
3213 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
3214 if (ret)
3215 return ret;
3216
3217 if (neg)
3218 tracer_flags->val &= ~opts->bit;
3219 else
3220 tracer_flags->val |= opts->bit;
3221 return 0;
3222 }
3223
3224 /* Try to assign a tracer specific option */
set_tracer_option(struct tracer * trace,char * cmp,int neg)3225 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3226 {
3227 struct tracer_flags *tracer_flags = trace->flags;
3228 struct tracer_opt *opts = NULL;
3229 int i;
3230
3231 for (i = 0; tracer_flags->opts[i].name; i++) {
3232 opts = &tracer_flags->opts[i];
3233
3234 if (strcmp(cmp, opts->name) == 0)
3235 return __set_tracer_option(trace, trace->flags,
3236 opts, neg);
3237 }
3238
3239 return -EINVAL;
3240 }
3241
3242 /* Some tracers require overwrite to stay enabled */
trace_keep_overwrite(struct tracer * tracer,u32 mask,int set)3243 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3244 {
3245 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3246 return -1;
3247
3248 return 0;
3249 }
3250
set_tracer_flag(struct trace_array * tr,unsigned int mask,int enabled)3251 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
3252 {
3253 /* do nothing if flag is already set */
3254 if (!!(trace_flags & mask) == !!enabled)
3255 return 0;
3256
3257 /* Give the tracer a chance to approve the change */
3258 if (tr->current_trace->flag_changed)
3259 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
3260 return -EINVAL;
3261
3262 if (enabled)
3263 trace_flags |= mask;
3264 else
3265 trace_flags &= ~mask;
3266
3267 if (mask == TRACE_ITER_RECORD_CMD)
3268 trace_event_enable_cmd_record(enabled);
3269
3270 if (mask == TRACE_ITER_OVERWRITE) {
3271 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
3272 #ifdef CONFIG_TRACER_MAX_TRACE
3273 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
3274 #endif
3275 }
3276
3277 if (mask == TRACE_ITER_PRINTK)
3278 trace_printk_start_stop_comm(enabled);
3279
3280 return 0;
3281 }
3282
trace_set_options(struct trace_array * tr,char * option)3283 static int trace_set_options(struct trace_array *tr, char *option)
3284 {
3285 char *cmp;
3286 int neg = 0;
3287 int ret = -ENODEV;
3288 int i;
3289
3290 cmp = strstrip(option);
3291
3292 if (strncmp(cmp, "no", 2) == 0) {
3293 neg = 1;
3294 cmp += 2;
3295 }
3296
3297 mutex_lock(&trace_types_lock);
3298
3299 for (i = 0; trace_options[i]; i++) {
3300 if (strcmp(cmp, trace_options[i]) == 0) {
3301 ret = set_tracer_flag(tr, 1 << i, !neg);
3302 break;
3303 }
3304 }
3305
3306 /* If no option could be set, test the specific tracer options */
3307 if (!trace_options[i])
3308 ret = set_tracer_option(tr->current_trace, cmp, neg);
3309
3310 mutex_unlock(&trace_types_lock);
3311
3312 return ret;
3313 }
3314
3315 static ssize_t
tracing_trace_options_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)3316 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3317 size_t cnt, loff_t *ppos)
3318 {
3319 struct seq_file *m = filp->private_data;
3320 struct trace_array *tr = m->private;
3321 char buf[64];
3322 int ret;
3323
3324 if (cnt >= sizeof(buf))
3325 return -EINVAL;
3326
3327 if (copy_from_user(&buf, ubuf, cnt))
3328 return -EFAULT;
3329
3330 buf[cnt] = 0;
3331
3332 ret = trace_set_options(tr, buf);
3333 if (ret < 0)
3334 return ret;
3335
3336 *ppos += cnt;
3337
3338 return cnt;
3339 }
3340
tracing_trace_options_open(struct inode * inode,struct file * file)3341 static int tracing_trace_options_open(struct inode *inode, struct file *file)
3342 {
3343 if (tracing_disabled)
3344 return -ENODEV;
3345
3346 return single_open(file, tracing_trace_options_show, inode->i_private);
3347 }
3348
3349 static const struct file_operations tracing_iter_fops = {
3350 .open = tracing_trace_options_open,
3351 .read = seq_read,
3352 .llseek = seq_lseek,
3353 .release = single_release,
3354 .write = tracing_trace_options_write,
3355 };
3356
3357 static const char readme_msg[] =
3358 "tracing mini-HOWTO:\n\n"
3359 "# echo 0 > tracing_on : quick way to disable tracing\n"
3360 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3361 " Important files:\n"
3362 " trace\t\t\t- The static contents of the buffer\n"
3363 "\t\t\t To clear the buffer write into this file: echo > trace\n"
3364 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3365 " current_tracer\t- function and latency tracers\n"
3366 " available_tracers\t- list of configured tracers for current_tracer\n"
3367 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3368 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3369 " trace_clock\t\t-change the clock used to order events\n"
3370 " local: Per cpu clock but may not be synced across CPUs\n"
3371 " global: Synced across CPUs but slows tracing down.\n"
3372 " counter: Not a clock, but just an increment\n"
3373 " uptime: Jiffy counter from time of boot\n"
3374 " perf: Same clock that perf events use\n"
3375 #ifdef CONFIG_X86_64
3376 " x86-tsc: TSC cycle counter\n"
3377 #endif
3378 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3379 " tracing_cpumask\t- Limit which CPUs to trace\n"
3380 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3381 "\t\t\t Remove sub-buffer with rmdir\n"
3382 " trace_options\t\t- Set format or modify how tracing happens\n"
3383 "\t\t\t Disable an option by adding a suffix 'no' to the option name\n"
3384 #ifdef CONFIG_DYNAMIC_FTRACE
3385 "\n available_filter_functions - list of functions that can be filtered on\n"
3386 " set_ftrace_filter\t- echo function name in here to only trace these functions\n"
3387 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3388 " modules: Can select a group via module\n"
3389 " Format: :mod:<module-name>\n"
3390 " example: echo :mod:ext3 > set_ftrace_filter\n"
3391 " triggers: a command to perform when function is hit\n"
3392 " Format: <function>:<trigger>[:count]\n"
3393 " trigger: traceon, traceoff\n"
3394 " enable_event:<system>:<event>\n"
3395 " disable_event:<system>:<event>\n"
3396 #ifdef CONFIG_STACKTRACE
3397 " stacktrace\n"
3398 #endif
3399 #ifdef CONFIG_TRACER_SNAPSHOT
3400 " snapshot\n"
3401 #endif
3402 " example: echo do_fault:traceoff > set_ftrace_filter\n"
3403 " echo do_trap:traceoff:3 > set_ftrace_filter\n"
3404 " The first one will disable tracing every time do_fault is hit\n"
3405 " The second will disable tracing at most 3 times when do_trap is hit\n"
3406 " The first time do trap is hit and it disables tracing, the counter\n"
3407 " will decrement to 2. If tracing is already disabled, the counter\n"
3408 " will not decrement. It only decrements when the trigger did work\n"
3409 " To remove trigger without count:\n"
3410 " echo '!<function>:<trigger> > set_ftrace_filter\n"
3411 " To remove trigger with a count:\n"
3412 " echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
3413 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
3414 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3415 " modules: Can select a group via module command :mod:\n"
3416 " Does not accept triggers\n"
3417 #endif /* CONFIG_DYNAMIC_FTRACE */
3418 #ifdef CONFIG_FUNCTION_TRACER
3419 " set_ftrace_pid\t- Write pid(s) to only function trace those pids (function)\n"
3420 #endif
3421 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3422 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3423 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3424 #endif
3425 #ifdef CONFIG_TRACER_SNAPSHOT
3426 "\n snapshot\t\t- Like 'trace' but shows the content of the static snapshot buffer\n"
3427 "\t\t\t Read the contents for more information\n"
3428 #endif
3429 #ifdef CONFIG_STACKTRACE
3430 " stack_trace\t\t- Shows the max stack trace when active\n"
3431 " stack_max_size\t- Shows current max stack size that was traced\n"
3432 "\t\t\t Write into this file to reset the max size (trigger a new trace)\n"
3433 #ifdef CONFIG_DYNAMIC_FTRACE
3434 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace traces\n"
3435 #endif
3436 #endif /* CONFIG_STACKTRACE */
3437 ;
3438
3439 static ssize_t
tracing_readme_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)3440 tracing_readme_read(struct file *filp, char __user *ubuf,
3441 size_t cnt, loff_t *ppos)
3442 {
3443 return simple_read_from_buffer(ubuf, cnt, ppos,
3444 readme_msg, strlen(readme_msg));
3445 }
3446
3447 static const struct file_operations tracing_readme_fops = {
3448 .open = tracing_open_generic,
3449 .read = tracing_readme_read,
3450 .llseek = generic_file_llseek,
3451 };
3452
3453 static ssize_t
tracing_saved_cmdlines_read(struct file * file,char __user * ubuf,size_t cnt,loff_t * ppos)3454 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3455 size_t cnt, loff_t *ppos)
3456 {
3457 char *buf_comm;
3458 char *file_buf;
3459 char *buf;
3460 int len = 0;
3461 int pid;
3462 int i;
3463
3464 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3465 if (!file_buf)
3466 return -ENOMEM;
3467
3468 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3469 if (!buf_comm) {
3470 kfree(file_buf);
3471 return -ENOMEM;
3472 }
3473
3474 buf = file_buf;
3475
3476 for (i = 0; i < SAVED_CMDLINES; i++) {
3477 int r;
3478
3479 pid = map_cmdline_to_pid[i];
3480 if (pid == -1 || pid == NO_CMDLINE_MAP)
3481 continue;
3482
3483 trace_find_cmdline(pid, buf_comm);
3484 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3485 buf += r;
3486 len += r;
3487 }
3488
3489 len = simple_read_from_buffer(ubuf, cnt, ppos,
3490 file_buf, len);
3491
3492 kfree(file_buf);
3493 kfree(buf_comm);
3494
3495 return len;
3496 }
3497
3498 static const struct file_operations tracing_saved_cmdlines_fops = {
3499 .open = tracing_open_generic,
3500 .read = tracing_saved_cmdlines_read,
3501 .llseek = generic_file_llseek,
3502 };
3503
3504 static ssize_t
tracing_saved_tgids_read(struct file * file,char __user * ubuf,size_t cnt,loff_t * ppos)3505 tracing_saved_tgids_read(struct file *file, char __user *ubuf,
3506 size_t cnt, loff_t *ppos)
3507 {
3508 char *file_buf;
3509 char *buf;
3510 int len = 0;
3511 int pid;
3512 int i;
3513
3514 file_buf = kmalloc(SAVED_CMDLINES*(16+1+16), GFP_KERNEL);
3515 if (!file_buf)
3516 return -ENOMEM;
3517
3518 buf = file_buf;
3519
3520 for (i = 0; i < SAVED_CMDLINES; i++) {
3521 int tgid;
3522 int r;
3523
3524 pid = map_cmdline_to_pid[i];
3525 if (pid == -1 || pid == NO_CMDLINE_MAP)
3526 continue;
3527
3528 tgid = trace_find_tgid(pid);
3529 r = sprintf(buf, "%d %d\n", pid, tgid);
3530 buf += r;
3531 len += r;
3532 }
3533
3534 len = simple_read_from_buffer(ubuf, cnt, ppos,
3535 file_buf, len);
3536
3537 kfree(file_buf);
3538
3539 return len;
3540 }
3541
3542 static const struct file_operations tracing_saved_tgids_fops = {
3543 .open = tracing_open_generic,
3544 .read = tracing_saved_tgids_read,
3545 .llseek = generic_file_llseek,
3546 };
3547
3548 static ssize_t
tracing_set_trace_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)3549 tracing_set_trace_read(struct file *filp, char __user *ubuf,
3550 size_t cnt, loff_t *ppos)
3551 {
3552 struct trace_array *tr = filp->private_data;
3553 char buf[MAX_TRACER_SIZE+2];
3554 int r;
3555
3556 mutex_lock(&trace_types_lock);
3557 r = sprintf(buf, "%s\n", tr->current_trace->name);
3558 mutex_unlock(&trace_types_lock);
3559
3560 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3561 }
3562
tracer_init(struct tracer * t,struct trace_array * tr)3563 int tracer_init(struct tracer *t, struct trace_array *tr)
3564 {
3565 tracing_reset_online_cpus(&tr->trace_buffer);
3566 return t->init(tr);
3567 }
3568
set_buffer_entries(struct trace_buffer * buf,unsigned long val)3569 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
3570 {
3571 int cpu;
3572
3573 for_each_tracing_cpu(cpu)
3574 per_cpu_ptr(buf->data, cpu)->entries = val;
3575 }
3576
3577 #ifdef CONFIG_TRACER_MAX_TRACE
3578 /* resize @tr's buffer to the size of @size_tr's entries */
resize_buffer_duplicate_size(struct trace_buffer * trace_buf,struct trace_buffer * size_buf,int cpu_id)3579 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3580 struct trace_buffer *size_buf, int cpu_id)
3581 {
3582 int cpu, ret = 0;
3583
3584 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3585 for_each_tracing_cpu(cpu) {
3586 ret = ring_buffer_resize(trace_buf->buffer,
3587 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
3588 if (ret < 0)
3589 break;
3590 per_cpu_ptr(trace_buf->data, cpu)->entries =
3591 per_cpu_ptr(size_buf->data, cpu)->entries;
3592 }
3593 } else {
3594 ret = ring_buffer_resize(trace_buf->buffer,
3595 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
3596 if (ret == 0)
3597 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3598 per_cpu_ptr(size_buf->data, cpu_id)->entries;
3599 }
3600
3601 return ret;
3602 }
3603 #endif /* CONFIG_TRACER_MAX_TRACE */
3604
__tracing_resize_ring_buffer(struct trace_array * tr,unsigned long size,int cpu)3605 static int __tracing_resize_ring_buffer(struct trace_array *tr,
3606 unsigned long size, int cpu)
3607 {
3608 int ret;
3609
3610 /*
3611 * If kernel or user changes the size of the ring buffer
3612 * we use the size that was given, and we can forget about
3613 * expanding it later.
3614 */
3615 ring_buffer_expanded = true;
3616
3617 /* May be called before buffers are initialized */
3618 if (!tr->trace_buffer.buffer)
3619 return 0;
3620
3621 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
3622 if (ret < 0)
3623 return ret;
3624
3625 #ifdef CONFIG_TRACER_MAX_TRACE
3626 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3627 !tr->current_trace->use_max_tr)
3628 goto out;
3629
3630 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
3631 if (ret < 0) {
3632 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3633 &tr->trace_buffer, cpu);
3634 if (r < 0) {
3635 /*
3636 * AARGH! We are left with different
3637 * size max buffer!!!!
3638 * The max buffer is our "snapshot" buffer.
3639 * When a tracer needs a snapshot (one of the
3640 * latency tracers), it swaps the max buffer
3641 * with the saved snap shot. We succeeded to
3642 * update the size of the main buffer, but failed to
3643 * update the size of the max buffer. But when we tried
3644 * to reset the main buffer to the original size, we
3645 * failed there too. This is very unlikely to
3646 * happen, but if it does, warn and kill all
3647 * tracing.
3648 */
3649 WARN_ON(1);
3650 tracing_disabled = 1;
3651 }
3652 return ret;
3653 }
3654
3655 if (cpu == RING_BUFFER_ALL_CPUS)
3656 set_buffer_entries(&tr->max_buffer, size);
3657 else
3658 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
3659
3660 out:
3661 #endif /* CONFIG_TRACER_MAX_TRACE */
3662
3663 if (cpu == RING_BUFFER_ALL_CPUS)
3664 set_buffer_entries(&tr->trace_buffer, size);
3665 else
3666 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
3667
3668 return ret;
3669 }
3670
tracing_resize_ring_buffer(struct trace_array * tr,unsigned long size,int cpu_id)3671 static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3672 unsigned long size, int cpu_id)
3673 {
3674 int ret = size;
3675
3676 mutex_lock(&trace_types_lock);
3677
3678 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3679 /* make sure, this cpu is enabled in the mask */
3680 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3681 ret = -EINVAL;
3682 goto out;
3683 }
3684 }
3685
3686 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
3687 if (ret < 0)
3688 ret = -ENOMEM;
3689
3690 out:
3691 mutex_unlock(&trace_types_lock);
3692
3693 return ret;
3694 }
3695
3696
3697 /**
3698 * tracing_update_buffers - used by tracing facility to expand ring buffers
3699 *
3700 * To save on memory when the tracing is never used on a system with it
3701 * configured in. The ring buffers are set to a minimum size. But once
3702 * a user starts to use the tracing facility, then they need to grow
3703 * to their default size.
3704 *
3705 * This function is to be called when a tracer is about to be used.
3706 */
tracing_update_buffers(void)3707 int tracing_update_buffers(void)
3708 {
3709 int ret = 0;
3710
3711 mutex_lock(&trace_types_lock);
3712 if (!ring_buffer_expanded)
3713 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
3714 RING_BUFFER_ALL_CPUS);
3715 mutex_unlock(&trace_types_lock);
3716
3717 return ret;
3718 }
3719
3720 struct trace_option_dentry;
3721
3722 static struct trace_option_dentry *
3723 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
3724
3725 static void
3726 destroy_trace_option_files(struct trace_option_dentry *topts);
3727
tracing_set_tracer(const char * buf)3728 static int tracing_set_tracer(const char *buf)
3729 {
3730 static struct trace_option_dentry *topts;
3731 struct trace_array *tr = &global_trace;
3732 struct tracer *t;
3733 #ifdef CONFIG_TRACER_MAX_TRACE
3734 bool had_max_tr;
3735 #endif
3736 int ret = 0;
3737
3738 mutex_lock(&trace_types_lock);
3739
3740 if (!ring_buffer_expanded) {
3741 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
3742 RING_BUFFER_ALL_CPUS);
3743 if (ret < 0)
3744 goto out;
3745 ret = 0;
3746 }
3747
3748 for (t = trace_types; t; t = t->next) {
3749 if (strcmp(t->name, buf) == 0)
3750 break;
3751 }
3752 if (!t) {
3753 ret = -EINVAL;
3754 goto out;
3755 }
3756 if (t == tr->current_trace)
3757 goto out;
3758
3759 trace_branch_disable();
3760
3761 tr->current_trace->enabled = false;
3762
3763 if (tr->current_trace->reset)
3764 tr->current_trace->reset(tr);
3765
3766 /* Current trace needs to be nop_trace before synchronize_sched */
3767 tr->current_trace = &nop_trace;
3768
3769 #ifdef CONFIG_TRACER_MAX_TRACE
3770 had_max_tr = tr->allocated_snapshot;
3771
3772 if (had_max_tr && !t->use_max_tr) {
3773 /*
3774 * We need to make sure that the update_max_tr sees that
3775 * current_trace changed to nop_trace to keep it from
3776 * swapping the buffers after we resize it.
3777 * The update_max_tr is called from interrupts disabled
3778 * so a synchronized_sched() is sufficient.
3779 */
3780 synchronize_sched();
3781 free_snapshot(tr);
3782 }
3783 #endif
3784 destroy_trace_option_files(topts);
3785
3786 topts = create_trace_option_files(tr, t);
3787
3788 #ifdef CONFIG_TRACER_MAX_TRACE
3789 if (t->use_max_tr && !had_max_tr) {
3790 ret = alloc_snapshot(tr);
3791 if (ret < 0)
3792 goto out;
3793 }
3794 #endif
3795
3796 if (t->init) {
3797 ret = tracer_init(t, tr);
3798 if (ret)
3799 goto out;
3800 }
3801
3802 tr->current_trace = t;
3803 tr->current_trace->enabled = true;
3804 trace_branch_enable(tr);
3805 out:
3806 mutex_unlock(&trace_types_lock);
3807
3808 return ret;
3809 }
3810
3811 static ssize_t
tracing_set_trace_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)3812 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3813 size_t cnt, loff_t *ppos)
3814 {
3815 char buf[MAX_TRACER_SIZE+1];
3816 int i;
3817 size_t ret;
3818 int err;
3819
3820 ret = cnt;
3821
3822 if (cnt > MAX_TRACER_SIZE)
3823 cnt = MAX_TRACER_SIZE;
3824
3825 if (copy_from_user(&buf, ubuf, cnt))
3826 return -EFAULT;
3827
3828 buf[cnt] = 0;
3829
3830 /* strip ending whitespace. */
3831 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3832 buf[i] = 0;
3833
3834 err = tracing_set_tracer(buf);
3835 if (err)
3836 return err;
3837
3838 *ppos += ret;
3839
3840 return ret;
3841 }
3842
3843 static ssize_t
tracing_max_lat_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)3844 tracing_max_lat_read(struct file *filp, char __user *ubuf,
3845 size_t cnt, loff_t *ppos)
3846 {
3847 unsigned long *ptr = filp->private_data;
3848 char buf[64];
3849 int r;
3850
3851 r = snprintf(buf, sizeof(buf), "%ld\n",
3852 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
3853 if (r > sizeof(buf))
3854 r = sizeof(buf);
3855 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3856 }
3857
3858 static ssize_t
tracing_max_lat_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)3859 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3860 size_t cnt, loff_t *ppos)
3861 {
3862 unsigned long *ptr = filp->private_data;
3863 unsigned long val;
3864 int ret;
3865
3866 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3867 if (ret)
3868 return ret;
3869
3870 *ptr = val * 1000;
3871
3872 return cnt;
3873 }
3874
tracing_open_pipe(struct inode * inode,struct file * filp)3875 static int tracing_open_pipe(struct inode *inode, struct file *filp)
3876 {
3877 struct trace_cpu *tc = inode->i_private;
3878 struct trace_array *tr = tc->tr;
3879 struct trace_iterator *iter;
3880 int ret = 0;
3881
3882 if (tracing_disabled)
3883 return -ENODEV;
3884
3885 mutex_lock(&trace_types_lock);
3886
3887 /* create a buffer to store the information to pass to userspace */
3888 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3889 if (!iter) {
3890 ret = -ENOMEM;
3891 goto out;
3892 }
3893
3894 /*
3895 * We make a copy of the current tracer to avoid concurrent
3896 * changes on it while we are reading.
3897 */
3898 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3899 if (!iter->trace) {
3900 ret = -ENOMEM;
3901 goto fail;
3902 }
3903 *iter->trace = *tr->current_trace;
3904
3905 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3906 ret = -ENOMEM;
3907 goto fail;
3908 }
3909
3910 /* trace pipe does not show start of buffer */
3911 cpumask_setall(iter->started);
3912
3913 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3914 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3915
3916 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3917 if (trace_clocks[tr->clock_id].in_ns)
3918 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3919
3920 iter->cpu_file = tc->cpu;
3921 iter->tr = tc->tr;
3922 iter->trace_buffer = &tc->tr->trace_buffer;
3923 mutex_init(&iter->mutex);
3924 filp->private_data = iter;
3925
3926 if (iter->trace->pipe_open)
3927 iter->trace->pipe_open(iter);
3928
3929 nonseekable_open(inode, filp);
3930 out:
3931 mutex_unlock(&trace_types_lock);
3932 return ret;
3933
3934 fail:
3935 kfree(iter->trace);
3936 kfree(iter);
3937 mutex_unlock(&trace_types_lock);
3938 return ret;
3939 }
3940
tracing_release_pipe(struct inode * inode,struct file * file)3941 static int tracing_release_pipe(struct inode *inode, struct file *file)
3942 {
3943 struct trace_iterator *iter = file->private_data;
3944
3945 mutex_lock(&trace_types_lock);
3946
3947 if (iter->trace->pipe_close)
3948 iter->trace->pipe_close(iter);
3949
3950 mutex_unlock(&trace_types_lock);
3951
3952 free_cpumask_var(iter->started);
3953 mutex_destroy(&iter->mutex);
3954 kfree(iter->trace);
3955 kfree(iter);
3956
3957 return 0;
3958 }
3959
3960 static unsigned int
trace_poll(struct trace_iterator * iter,struct file * filp,poll_table * poll_table)3961 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
3962 {
3963 /* Iterators are static, they should be filled or empty */
3964 if (trace_buffer_iter(iter, iter->cpu_file))
3965 return POLLIN | POLLRDNORM;
3966
3967 if (trace_flags & TRACE_ITER_BLOCK)
3968 /*
3969 * Always select as readable when in blocking mode
3970 */
3971 return POLLIN | POLLRDNORM;
3972 else
3973 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
3974 filp, poll_table);
3975 }
3976
3977 static unsigned int
tracing_poll_pipe(struct file * filp,poll_table * poll_table)3978 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3979 {
3980 struct trace_iterator *iter = filp->private_data;
3981
3982 return trace_poll(iter, filp, poll_table);
3983 }
3984
3985 /*
3986 * This is a make-shift waitqueue.
3987 * A tracer might use this callback on some rare cases:
3988 *
3989 * 1) the current tracer might hold the runqueue lock when it wakes up
3990 * a reader, hence a deadlock (sched, function, and function graph tracers)
3991 * 2) the function tracers, trace all functions, we don't want
3992 * the overhead of calling wake_up and friends
3993 * (and tracing them too)
3994 *
3995 * Anyway, this is really very primitive wakeup.
3996 */
poll_wait_pipe(struct trace_iterator * iter)3997 void poll_wait_pipe(struct trace_iterator *iter)
3998 {
3999 set_current_state(TASK_INTERRUPTIBLE);
4000 /* sleep for 100 msecs, and try again. */
4001 schedule_timeout(HZ / 10);
4002 }
4003
4004 /* Must be called with trace_types_lock mutex held. */
tracing_wait_pipe(struct file * filp)4005 static int tracing_wait_pipe(struct file *filp)
4006 {
4007 struct trace_iterator *iter = filp->private_data;
4008
4009 while (trace_empty(iter)) {
4010
4011 if ((filp->f_flags & O_NONBLOCK)) {
4012 return -EAGAIN;
4013 }
4014
4015 mutex_unlock(&iter->mutex);
4016
4017 iter->trace->wait_pipe(iter);
4018
4019 mutex_lock(&iter->mutex);
4020
4021 if (signal_pending(current))
4022 return -EINTR;
4023
4024 /*
4025 * We block until we read something and tracing is disabled.
4026 * We still block if tracing is disabled, but we have never
4027 * read anything. This allows a user to cat this file, and
4028 * then enable tracing. But after we have read something,
4029 * we give an EOF when tracing is again disabled.
4030 *
4031 * iter->pos will be 0 if we haven't read anything.
4032 */
4033 if (!tracing_is_enabled() && iter->pos)
4034 break;
4035 }
4036
4037 return 1;
4038 }
4039
4040 /*
4041 * Consumer reader.
4042 */
4043 static ssize_t
tracing_read_pipe(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)4044 tracing_read_pipe(struct file *filp, char __user *ubuf,
4045 size_t cnt, loff_t *ppos)
4046 {
4047 struct trace_iterator *iter = filp->private_data;
4048 struct trace_array *tr = iter->tr;
4049 ssize_t sret;
4050
4051 /* return any leftover data */
4052 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4053 if (sret != -EBUSY)
4054 return sret;
4055
4056 trace_seq_init(&iter->seq);
4057
4058 /* copy the tracer to avoid using a global lock all around */
4059 mutex_lock(&trace_types_lock);
4060 if (unlikely(iter->trace->name != tr->current_trace->name))
4061 *iter->trace = *tr->current_trace;
4062 mutex_unlock(&trace_types_lock);
4063
4064 /*
4065 * Avoid more than one consumer on a single file descriptor
4066 * This is just a matter of traces coherency, the ring buffer itself
4067 * is protected.
4068 */
4069 mutex_lock(&iter->mutex);
4070 if (iter->trace->read) {
4071 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
4072 if (sret)
4073 goto out;
4074 }
4075
4076 waitagain:
4077 sret = tracing_wait_pipe(filp);
4078 if (sret <= 0)
4079 goto out;
4080
4081 /* stop when tracing is finished */
4082 if (trace_empty(iter)) {
4083 sret = 0;
4084 goto out;
4085 }
4086
4087 if (cnt >= PAGE_SIZE)
4088 cnt = PAGE_SIZE - 1;
4089
4090 /* reset all but tr, trace, and overruns */
4091 memset(&iter->seq, 0,
4092 sizeof(struct trace_iterator) -
4093 offsetof(struct trace_iterator, seq));
4094 iter->pos = -1;
4095
4096 trace_event_read_lock();
4097 trace_access_lock(iter->cpu_file);
4098 while (trace_find_next_entry_inc(iter) != NULL) {
4099 enum print_line_t ret;
4100 int len = iter->seq.len;
4101
4102 ret = print_trace_line(iter);
4103 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4104 /* don't print partial lines */
4105 iter->seq.len = len;
4106 break;
4107 }
4108 if (ret != TRACE_TYPE_NO_CONSUME)
4109 trace_consume(iter);
4110
4111 if (iter->seq.len >= cnt)
4112 break;
4113
4114 /*
4115 * Setting the full flag means we reached the trace_seq buffer
4116 * size and we should leave by partial output condition above.
4117 * One of the trace_seq_* functions is not used properly.
4118 */
4119 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
4120 iter->ent->type);
4121 }
4122 trace_access_unlock(iter->cpu_file);
4123 trace_event_read_unlock();
4124
4125 /* Now copy what we have to the user */
4126 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4127 if (iter->seq.readpos >= iter->seq.len)
4128 trace_seq_init(&iter->seq);
4129
4130 /*
4131 * If there was nothing to send to user, in spite of consuming trace
4132 * entries, go back to wait for more entries.
4133 */
4134 if (sret == -EBUSY)
4135 goto waitagain;
4136
4137 out:
4138 mutex_unlock(&iter->mutex);
4139
4140 return sret;
4141 }
4142
tracing_pipe_buf_release(struct pipe_inode_info * pipe,struct pipe_buffer * buf)4143 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
4144 struct pipe_buffer *buf)
4145 {
4146 __free_page(buf->page);
4147 }
4148
tracing_spd_release_pipe(struct splice_pipe_desc * spd,unsigned int idx)4149 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
4150 unsigned int idx)
4151 {
4152 __free_page(spd->pages[idx]);
4153 }
4154
4155 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
4156 .can_merge = 0,
4157 .map = generic_pipe_buf_map,
4158 .unmap = generic_pipe_buf_unmap,
4159 .confirm = generic_pipe_buf_confirm,
4160 .release = tracing_pipe_buf_release,
4161 .steal = generic_pipe_buf_steal,
4162 .get = generic_pipe_buf_get,
4163 };
4164
4165 static size_t
tracing_fill_pipe_page(size_t rem,struct trace_iterator * iter)4166 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
4167 {
4168 size_t count;
4169 int ret;
4170
4171 /* Seq buffer is page-sized, exactly what we need. */
4172 for (;;) {
4173 count = iter->seq.len;
4174 ret = print_trace_line(iter);
4175 count = iter->seq.len - count;
4176 if (rem < count) {
4177 rem = 0;
4178 iter->seq.len -= count;
4179 break;
4180 }
4181 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4182 iter->seq.len -= count;
4183 break;
4184 }
4185
4186 if (ret != TRACE_TYPE_NO_CONSUME)
4187 trace_consume(iter);
4188 rem -= count;
4189 if (!trace_find_next_entry_inc(iter)) {
4190 rem = 0;
4191 iter->ent = NULL;
4192 break;
4193 }
4194 }
4195
4196 return rem;
4197 }
4198
tracing_splice_read_pipe(struct file * filp,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)4199 static ssize_t tracing_splice_read_pipe(struct file *filp,
4200 loff_t *ppos,
4201 struct pipe_inode_info *pipe,
4202 size_t len,
4203 unsigned int flags)
4204 {
4205 struct page *pages_def[PIPE_DEF_BUFFERS];
4206 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4207 struct trace_iterator *iter = filp->private_data;
4208 struct splice_pipe_desc spd = {
4209 .pages = pages_def,
4210 .partial = partial_def,
4211 .nr_pages = 0, /* This gets updated below. */
4212 .nr_pages_max = PIPE_DEF_BUFFERS,
4213 .flags = flags,
4214 .ops = &tracing_pipe_buf_ops,
4215 .spd_release = tracing_spd_release_pipe,
4216 };
4217 struct trace_array *tr = iter->tr;
4218 ssize_t ret;
4219 size_t rem;
4220 unsigned int i;
4221
4222 if (splice_grow_spd(pipe, &spd))
4223 return -ENOMEM;
4224
4225 /* copy the tracer to avoid using a global lock all around */
4226 mutex_lock(&trace_types_lock);
4227 if (unlikely(iter->trace->name != tr->current_trace->name))
4228 *iter->trace = *tr->current_trace;
4229 mutex_unlock(&trace_types_lock);
4230
4231 mutex_lock(&iter->mutex);
4232
4233 if (iter->trace->splice_read) {
4234 ret = iter->trace->splice_read(iter, filp,
4235 ppos, pipe, len, flags);
4236 if (ret)
4237 goto out_err;
4238 }
4239
4240 ret = tracing_wait_pipe(filp);
4241 if (ret <= 0)
4242 goto out_err;
4243
4244 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
4245 ret = -EFAULT;
4246 goto out_err;
4247 }
4248
4249 trace_event_read_lock();
4250 trace_access_lock(iter->cpu_file);
4251
4252 /* Fill as many pages as possible. */
4253 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
4254 spd.pages[i] = alloc_page(GFP_KERNEL);
4255 if (!spd.pages[i])
4256 break;
4257
4258 rem = tracing_fill_pipe_page(rem, iter);
4259
4260 /* Copy the data into the page, so we can start over. */
4261 ret = trace_seq_to_buffer(&iter->seq,
4262 page_address(spd.pages[i]),
4263 iter->seq.len);
4264 if (ret < 0) {
4265 __free_page(spd.pages[i]);
4266 break;
4267 }
4268 spd.partial[i].offset = 0;
4269 spd.partial[i].len = iter->seq.len;
4270
4271 trace_seq_init(&iter->seq);
4272 }
4273
4274 trace_access_unlock(iter->cpu_file);
4275 trace_event_read_unlock();
4276 mutex_unlock(&iter->mutex);
4277
4278 spd.nr_pages = i;
4279
4280 ret = splice_to_pipe(pipe, &spd);
4281 out:
4282 splice_shrink_spd(&spd);
4283 return ret;
4284
4285 out_err:
4286 mutex_unlock(&iter->mutex);
4287 goto out;
4288 }
4289
4290 static ssize_t
tracing_entries_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)4291 tracing_entries_read(struct file *filp, char __user *ubuf,
4292 size_t cnt, loff_t *ppos)
4293 {
4294 struct trace_cpu *tc = filp->private_data;
4295 struct trace_array *tr = tc->tr;
4296 char buf[64];
4297 int r = 0;
4298 ssize_t ret;
4299
4300 mutex_lock(&trace_types_lock);
4301
4302 if (tc->cpu == RING_BUFFER_ALL_CPUS) {
4303 int cpu, buf_size_same;
4304 unsigned long size;
4305
4306 size = 0;
4307 buf_size_same = 1;
4308 /* check if all cpu sizes are same */
4309 for_each_tracing_cpu(cpu) {
4310 /* fill in the size from first enabled cpu */
4311 if (size == 0)
4312 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4313 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
4314 buf_size_same = 0;
4315 break;
4316 }
4317 }
4318
4319 if (buf_size_same) {
4320 if (!ring_buffer_expanded)
4321 r = sprintf(buf, "%lu (expanded: %lu)\n",
4322 size >> 10,
4323 trace_buf_size >> 10);
4324 else
4325 r = sprintf(buf, "%lu\n", size >> 10);
4326 } else
4327 r = sprintf(buf, "X\n");
4328 } else
4329 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, tc->cpu)->entries >> 10);
4330
4331 mutex_unlock(&trace_types_lock);
4332
4333 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4334 return ret;
4335 }
4336
4337 static ssize_t
tracing_entries_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)4338 tracing_entries_write(struct file *filp, const char __user *ubuf,
4339 size_t cnt, loff_t *ppos)
4340 {
4341 struct trace_cpu *tc = filp->private_data;
4342 unsigned long val;
4343 int ret;
4344
4345 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4346 if (ret)
4347 return ret;
4348
4349 /* must have at least 1 entry */
4350 if (!val)
4351 return -EINVAL;
4352
4353 /* value is in KB */
4354 val <<= 10;
4355
4356 ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
4357 if (ret < 0)
4358 return ret;
4359
4360 *ppos += cnt;
4361
4362 return cnt;
4363 }
4364
4365 static ssize_t
tracing_total_entries_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)4366 tracing_total_entries_read(struct file *filp, char __user *ubuf,
4367 size_t cnt, loff_t *ppos)
4368 {
4369 struct trace_array *tr = filp->private_data;
4370 char buf[64];
4371 int r, cpu;
4372 unsigned long size = 0, expanded_size = 0;
4373
4374 mutex_lock(&trace_types_lock);
4375 for_each_tracing_cpu(cpu) {
4376 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
4377 if (!ring_buffer_expanded)
4378 expanded_size += trace_buf_size >> 10;
4379 }
4380 if (ring_buffer_expanded)
4381 r = sprintf(buf, "%lu\n", size);
4382 else
4383 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4384 mutex_unlock(&trace_types_lock);
4385
4386 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4387 }
4388
4389 static ssize_t
tracing_free_buffer_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)4390 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4391 size_t cnt, loff_t *ppos)
4392 {
4393 /*
4394 * There is no need to read what the user has written, this function
4395 * is just to make sure that there is no error when "echo" is used
4396 */
4397
4398 *ppos += cnt;
4399
4400 return cnt;
4401 }
4402
4403 static int
tracing_free_buffer_release(struct inode * inode,struct file * filp)4404 tracing_free_buffer_release(struct inode *inode, struct file *filp)
4405 {
4406 struct trace_array *tr = inode->i_private;
4407
4408 /* disable tracing ? */
4409 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4410 tracing_off();
4411 /* resize the ring buffer to 0 */
4412 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4413
4414 return 0;
4415 }
4416
4417 static ssize_t
tracing_mark_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * fpos)4418 tracing_mark_write(struct file *filp, const char __user *ubuf,
4419 size_t cnt, loff_t *fpos)
4420 {
4421 unsigned long addr = (unsigned long)ubuf;
4422 struct ring_buffer_event *event;
4423 struct ring_buffer *buffer;
4424 struct print_entry *entry;
4425 unsigned long irq_flags;
4426 struct page *pages[2];
4427 void *map_page[2];
4428 int nr_pages = 1;
4429 ssize_t written;
4430 int offset;
4431 int size;
4432 int len;
4433 int ret;
4434 int i;
4435
4436 if (tracing_disabled)
4437 return -EINVAL;
4438
4439 if (!(trace_flags & TRACE_ITER_MARKERS))
4440 return -EINVAL;
4441
4442 if (cnt > TRACE_BUF_SIZE)
4443 cnt = TRACE_BUF_SIZE;
4444
4445 /*
4446 * Userspace is injecting traces into the kernel trace buffer.
4447 * We want to be as non intrusive as possible.
4448 * To do so, we do not want to allocate any special buffers
4449 * or take any locks, but instead write the userspace data
4450 * straight into the ring buffer.
4451 *
4452 * First we need to pin the userspace buffer into memory,
4453 * which, most likely it is, because it just referenced it.
4454 * But there's no guarantee that it is. By using get_user_pages_fast()
4455 * and kmap_atomic/kunmap_atomic() we can get access to the
4456 * pages directly. We then write the data directly into the
4457 * ring buffer.
4458 */
4459 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4460
4461 /* check if we cross pages */
4462 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4463 nr_pages = 2;
4464
4465 offset = addr & (PAGE_SIZE - 1);
4466 addr &= PAGE_MASK;
4467
4468 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4469 if (ret < nr_pages) {
4470 while (--ret >= 0)
4471 put_page(pages[ret]);
4472 written = -EFAULT;
4473 goto out;
4474 }
4475
4476 for (i = 0; i < nr_pages; i++)
4477 map_page[i] = kmap_atomic(pages[i]);
4478
4479 local_save_flags(irq_flags);
4480 size = sizeof(*entry) + cnt + 2; /* possible \n added */
4481 buffer = global_trace.trace_buffer.buffer;
4482 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4483 irq_flags, preempt_count());
4484 if (!event) {
4485 /* Ring buffer disabled, return as if not open for write */
4486 written = -EBADF;
4487 goto out_unlock;
4488 }
4489
4490 entry = ring_buffer_event_data(event);
4491 entry->ip = _THIS_IP_;
4492
4493 if (nr_pages == 2) {
4494 len = PAGE_SIZE - offset;
4495 memcpy(&entry->buf, map_page[0] + offset, len);
4496 memcpy(&entry->buf[len], map_page[1], cnt - len);
4497 } else
4498 memcpy(&entry->buf, map_page[0] + offset, cnt);
4499
4500 if (entry->buf[cnt - 1] != '\n') {
4501 entry->buf[cnt] = '\n';
4502 entry->buf[cnt + 1] = '\0';
4503 } else
4504 entry->buf[cnt] = '\0';
4505
4506 __buffer_unlock_commit(buffer, event);
4507
4508 written = cnt;
4509
4510 *fpos += written;
4511
4512 out_unlock:
4513 for (i = 0; i < nr_pages; i++){
4514 kunmap_atomic(map_page[i]);
4515 put_page(pages[i]);
4516 }
4517 out:
4518 return written;
4519 }
4520
tracing_clock_show(struct seq_file * m,void * v)4521 static int tracing_clock_show(struct seq_file *m, void *v)
4522 {
4523 struct trace_array *tr = m->private;
4524 int i;
4525
4526 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4527 seq_printf(m,
4528 "%s%s%s%s", i ? " " : "",
4529 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4530 i == tr->clock_id ? "]" : "");
4531 seq_putc(m, '\n');
4532
4533 return 0;
4534 }
4535
tracing_clock_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * fpos)4536 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4537 size_t cnt, loff_t *fpos)
4538 {
4539 struct seq_file *m = filp->private_data;
4540 struct trace_array *tr = m->private;
4541 char buf[64];
4542 const char *clockstr;
4543 int i;
4544
4545 if (cnt >= sizeof(buf))
4546 return -EINVAL;
4547
4548 if (copy_from_user(&buf, ubuf, cnt))
4549 return -EFAULT;
4550
4551 buf[cnt] = 0;
4552
4553 clockstr = strstrip(buf);
4554
4555 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4556 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4557 break;
4558 }
4559 if (i == ARRAY_SIZE(trace_clocks))
4560 return -EINVAL;
4561
4562 mutex_lock(&trace_types_lock);
4563
4564 tr->clock_id = i;
4565
4566 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
4567
4568 /*
4569 * New clock may not be consistent with the previous clock.
4570 * Reset the buffer so that it doesn't have incomparable timestamps.
4571 */
4572 tracing_reset_online_cpus(&global_trace.trace_buffer);
4573
4574 #ifdef CONFIG_TRACER_MAX_TRACE
4575 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4576 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4577 tracing_reset_online_cpus(&global_trace.max_buffer);
4578 #endif
4579
4580 mutex_unlock(&trace_types_lock);
4581
4582 *fpos += cnt;
4583
4584 return cnt;
4585 }
4586
tracing_clock_open(struct inode * inode,struct file * file)4587 static int tracing_clock_open(struct inode *inode, struct file *file)
4588 {
4589 if (tracing_disabled)
4590 return -ENODEV;
4591
4592 return single_open(file, tracing_clock_show, inode->i_private);
4593 }
4594
4595 struct ftrace_buffer_info {
4596 struct trace_iterator iter;
4597 void *spare;
4598 unsigned int read;
4599 };
4600
4601 #ifdef CONFIG_TRACER_SNAPSHOT
tracing_snapshot_open(struct inode * inode,struct file * file)4602 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4603 {
4604 struct trace_cpu *tc = inode->i_private;
4605 struct trace_iterator *iter;
4606 struct seq_file *m;
4607 int ret = 0;
4608
4609 if (file->f_mode & FMODE_READ) {
4610 iter = __tracing_open(inode, file, true);
4611 if (IS_ERR(iter))
4612 ret = PTR_ERR(iter);
4613 } else {
4614 /* Writes still need the seq_file to hold the private data */
4615 m = kzalloc(sizeof(*m), GFP_KERNEL);
4616 if (!m)
4617 return -ENOMEM;
4618 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4619 if (!iter) {
4620 kfree(m);
4621 return -ENOMEM;
4622 }
4623 iter->tr = tc->tr;
4624 iter->trace_buffer = &tc->tr->max_buffer;
4625 iter->cpu_file = tc->cpu;
4626 m->private = iter;
4627 file->private_data = m;
4628 }
4629
4630 return ret;
4631 }
4632
4633 static ssize_t
tracing_snapshot_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)4634 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4635 loff_t *ppos)
4636 {
4637 struct seq_file *m = filp->private_data;
4638 struct trace_iterator *iter = m->private;
4639 struct trace_array *tr = iter->tr;
4640 unsigned long val;
4641 int ret;
4642
4643 ret = tracing_update_buffers();
4644 if (ret < 0)
4645 return ret;
4646
4647 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4648 if (ret)
4649 return ret;
4650
4651 mutex_lock(&trace_types_lock);
4652
4653 if (tr->current_trace->use_max_tr) {
4654 ret = -EBUSY;
4655 goto out;
4656 }
4657
4658 switch (val) {
4659 case 0:
4660 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4661 ret = -EINVAL;
4662 break;
4663 }
4664 if (tr->allocated_snapshot)
4665 free_snapshot(tr);
4666 break;
4667 case 1:
4668 /* Only allow per-cpu swap if the ring buffer supports it */
4669 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4670 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4671 ret = -EINVAL;
4672 break;
4673 }
4674 #endif
4675 if (!tr->allocated_snapshot) {
4676 ret = alloc_snapshot(tr);
4677 if (ret < 0)
4678 break;
4679 }
4680 local_irq_disable();
4681 /* Now, we're going to swap */
4682 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4683 update_max_tr(tr, current, smp_processor_id());
4684 else
4685 update_max_tr_single(tr, current, iter->cpu_file);
4686 local_irq_enable();
4687 break;
4688 default:
4689 if (tr->allocated_snapshot) {
4690 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4691 tracing_reset_online_cpus(&tr->max_buffer);
4692 else
4693 tracing_reset(&tr->max_buffer, iter->cpu_file);
4694 }
4695 break;
4696 }
4697
4698 if (ret >= 0) {
4699 *ppos += cnt;
4700 ret = cnt;
4701 }
4702 out:
4703 mutex_unlock(&trace_types_lock);
4704 return ret;
4705 }
4706
tracing_snapshot_release(struct inode * inode,struct file * file)4707 static int tracing_snapshot_release(struct inode *inode, struct file *file)
4708 {
4709 struct seq_file *m = file->private_data;
4710
4711 if (file->f_mode & FMODE_READ)
4712 return tracing_release(inode, file);
4713
4714 /* If write only, the seq_file is just a stub */
4715 if (m)
4716 kfree(m->private);
4717 kfree(m);
4718
4719 return 0;
4720 }
4721
4722 static int tracing_buffers_open(struct inode *inode, struct file *filp);
4723 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
4724 size_t count, loff_t *ppos);
4725 static int tracing_buffers_release(struct inode *inode, struct file *file);
4726 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4727 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
4728
snapshot_raw_open(struct inode * inode,struct file * filp)4729 static int snapshot_raw_open(struct inode *inode, struct file *filp)
4730 {
4731 struct ftrace_buffer_info *info;
4732 int ret;
4733
4734 ret = tracing_buffers_open(inode, filp);
4735 if (ret < 0)
4736 return ret;
4737
4738 info = filp->private_data;
4739
4740 if (info->iter.trace->use_max_tr) {
4741 tracing_buffers_release(inode, filp);
4742 return -EBUSY;
4743 }
4744
4745 info->iter.snapshot = true;
4746 info->iter.trace_buffer = &info->iter.tr->max_buffer;
4747
4748 return ret;
4749 }
4750
4751 #endif /* CONFIG_TRACER_SNAPSHOT */
4752
4753
4754 static const struct file_operations tracing_max_lat_fops = {
4755 .open = tracing_open_generic,
4756 .read = tracing_max_lat_read,
4757 .write = tracing_max_lat_write,
4758 .llseek = generic_file_llseek,
4759 };
4760
4761 static const struct file_operations set_tracer_fops = {
4762 .open = tracing_open_generic,
4763 .read = tracing_set_trace_read,
4764 .write = tracing_set_trace_write,
4765 .llseek = generic_file_llseek,
4766 };
4767
4768 static const struct file_operations tracing_pipe_fops = {
4769 .open = tracing_open_pipe,
4770 .poll = tracing_poll_pipe,
4771 .read = tracing_read_pipe,
4772 .splice_read = tracing_splice_read_pipe,
4773 .release = tracing_release_pipe,
4774 .llseek = no_llseek,
4775 };
4776
4777 static const struct file_operations tracing_entries_fops = {
4778 .open = tracing_open_generic,
4779 .read = tracing_entries_read,
4780 .write = tracing_entries_write,
4781 .llseek = generic_file_llseek,
4782 };
4783
4784 static const struct file_operations tracing_total_entries_fops = {
4785 .open = tracing_open_generic,
4786 .read = tracing_total_entries_read,
4787 .llseek = generic_file_llseek,
4788 };
4789
4790 static const struct file_operations tracing_free_buffer_fops = {
4791 .write = tracing_free_buffer_write,
4792 .release = tracing_free_buffer_release,
4793 };
4794
4795 static const struct file_operations tracing_mark_fops = {
4796 .open = tracing_open_generic,
4797 .write = tracing_mark_write,
4798 .llseek = generic_file_llseek,
4799 };
4800
4801 static const struct file_operations trace_clock_fops = {
4802 .open = tracing_clock_open,
4803 .read = seq_read,
4804 .llseek = seq_lseek,
4805 .release = single_release,
4806 .write = tracing_clock_write,
4807 };
4808
4809 #ifdef CONFIG_TRACER_SNAPSHOT
4810 static const struct file_operations snapshot_fops = {
4811 .open = tracing_snapshot_open,
4812 .read = seq_read,
4813 .write = tracing_snapshot_write,
4814 .llseek = tracing_seek,
4815 .release = tracing_snapshot_release,
4816 };
4817
4818 static const struct file_operations snapshot_raw_fops = {
4819 .open = snapshot_raw_open,
4820 .read = tracing_buffers_read,
4821 .release = tracing_buffers_release,
4822 .splice_read = tracing_buffers_splice_read,
4823 .llseek = no_llseek,
4824 };
4825
4826 #endif /* CONFIG_TRACER_SNAPSHOT */
4827
tracing_buffers_open(struct inode * inode,struct file * filp)4828 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4829 {
4830 struct trace_cpu *tc = inode->i_private;
4831 struct trace_array *tr = tc->tr;
4832 struct ftrace_buffer_info *info;
4833
4834 if (tracing_disabled)
4835 return -ENODEV;
4836
4837 info = kzalloc(sizeof(*info), GFP_KERNEL);
4838 if (!info)
4839 return -ENOMEM;
4840
4841 mutex_lock(&trace_types_lock);
4842
4843 tr->ref++;
4844
4845 info->iter.tr = tr;
4846 info->iter.cpu_file = tc->cpu;
4847 info->iter.trace = tr->current_trace;
4848 info->iter.trace_buffer = &tr->trace_buffer;
4849 info->spare = NULL;
4850 /* Force reading ring buffer for first read */
4851 info->read = (unsigned int)-1;
4852
4853 filp->private_data = info;
4854
4855 mutex_unlock(&trace_types_lock);
4856
4857 return nonseekable_open(inode, filp);
4858 }
4859
4860 static unsigned int
tracing_buffers_poll(struct file * filp,poll_table * poll_table)4861 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
4862 {
4863 struct ftrace_buffer_info *info = filp->private_data;
4864 struct trace_iterator *iter = &info->iter;
4865
4866 return trace_poll(iter, filp, poll_table);
4867 }
4868
4869 static ssize_t
tracing_buffers_read(struct file * filp,char __user * ubuf,size_t count,loff_t * ppos)4870 tracing_buffers_read(struct file *filp, char __user *ubuf,
4871 size_t count, loff_t *ppos)
4872 {
4873 struct ftrace_buffer_info *info = filp->private_data;
4874 struct trace_iterator *iter = &info->iter;
4875 ssize_t ret;
4876 ssize_t size;
4877
4878 if (!count)
4879 return 0;
4880
4881 mutex_lock(&trace_types_lock);
4882
4883 #ifdef CONFIG_TRACER_MAX_TRACE
4884 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4885 size = -EBUSY;
4886 goto out_unlock;
4887 }
4888 #endif
4889
4890 if (!info->spare)
4891 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
4892 iter->cpu_file);
4893 size = -ENOMEM;
4894 if (!info->spare)
4895 goto out_unlock;
4896
4897 /* Do we have previous read data to read? */
4898 if (info->read < PAGE_SIZE)
4899 goto read;
4900
4901 again:
4902 trace_access_lock(iter->cpu_file);
4903 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
4904 &info->spare,
4905 count,
4906 iter->cpu_file, 0);
4907 trace_access_unlock(iter->cpu_file);
4908
4909 if (ret < 0) {
4910 if (trace_empty(iter)) {
4911 if ((filp->f_flags & O_NONBLOCK)) {
4912 size = -EAGAIN;
4913 goto out_unlock;
4914 }
4915 mutex_unlock(&trace_types_lock);
4916 iter->trace->wait_pipe(iter);
4917 mutex_lock(&trace_types_lock);
4918 if (signal_pending(current)) {
4919 size = -EINTR;
4920 goto out_unlock;
4921 }
4922 goto again;
4923 }
4924 size = 0;
4925 goto out_unlock;
4926 }
4927
4928 info->read = 0;
4929 read:
4930 size = PAGE_SIZE - info->read;
4931 if (size > count)
4932 size = count;
4933
4934 ret = copy_to_user(ubuf, info->spare + info->read, size);
4935 if (ret == size) {
4936 size = -EFAULT;
4937 goto out_unlock;
4938 }
4939 size -= ret;
4940
4941 *ppos += size;
4942 info->read += size;
4943
4944 out_unlock:
4945 mutex_unlock(&trace_types_lock);
4946
4947 return size;
4948 }
4949
tracing_buffers_release(struct inode * inode,struct file * file)4950 static int tracing_buffers_release(struct inode *inode, struct file *file)
4951 {
4952 struct ftrace_buffer_info *info = file->private_data;
4953 struct trace_iterator *iter = &info->iter;
4954
4955 mutex_lock(&trace_types_lock);
4956
4957 WARN_ON(!iter->tr->ref);
4958 iter->tr->ref--;
4959
4960 if (info->spare)
4961 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
4962 kfree(info);
4963
4964 mutex_unlock(&trace_types_lock);
4965
4966 return 0;
4967 }
4968
4969 struct buffer_ref {
4970 struct ring_buffer *buffer;
4971 void *page;
4972 int ref;
4973 };
4974
buffer_pipe_buf_release(struct pipe_inode_info * pipe,struct pipe_buffer * buf)4975 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4976 struct pipe_buffer *buf)
4977 {
4978 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4979
4980 if (--ref->ref)
4981 return;
4982
4983 ring_buffer_free_read_page(ref->buffer, ref->page);
4984 kfree(ref);
4985 buf->private = 0;
4986 }
4987
buffer_pipe_buf_get(struct pipe_inode_info * pipe,struct pipe_buffer * buf)4988 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4989 struct pipe_buffer *buf)
4990 {
4991 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4992
4993 ref->ref++;
4994 }
4995
4996 /* Pipe buffer operations for a buffer. */
4997 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4998 .can_merge = 0,
4999 .map = generic_pipe_buf_map,
5000 .unmap = generic_pipe_buf_unmap,
5001 .confirm = generic_pipe_buf_confirm,
5002 .release = buffer_pipe_buf_release,
5003 .steal = generic_pipe_buf_steal,
5004 .get = buffer_pipe_buf_get,
5005 };
5006
5007 /*
5008 * Callback from splice_to_pipe(), if we need to release some pages
5009 * at the end of the spd in case we error'ed out in filling the pipe.
5010 */
buffer_spd_release(struct splice_pipe_desc * spd,unsigned int i)5011 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
5012 {
5013 struct buffer_ref *ref =
5014 (struct buffer_ref *)spd->partial[i].private;
5015
5016 if (--ref->ref)
5017 return;
5018
5019 ring_buffer_free_read_page(ref->buffer, ref->page);
5020 kfree(ref);
5021 spd->partial[i].private = 0;
5022 }
5023
5024 static ssize_t
tracing_buffers_splice_read(struct file * file,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)5025 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5026 struct pipe_inode_info *pipe, size_t len,
5027 unsigned int flags)
5028 {
5029 struct ftrace_buffer_info *info = file->private_data;
5030 struct trace_iterator *iter = &info->iter;
5031 struct partial_page partial_def[PIPE_DEF_BUFFERS];
5032 struct page *pages_def[PIPE_DEF_BUFFERS];
5033 struct splice_pipe_desc spd = {
5034 .pages = pages_def,
5035 .partial = partial_def,
5036 .nr_pages_max = PIPE_DEF_BUFFERS,
5037 .flags = flags,
5038 .ops = &buffer_pipe_buf_ops,
5039 .spd_release = buffer_spd_release,
5040 };
5041 struct buffer_ref *ref;
5042 int entries, size, i;
5043 ssize_t ret;
5044
5045 mutex_lock(&trace_types_lock);
5046
5047 #ifdef CONFIG_TRACER_MAX_TRACE
5048 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5049 ret = -EBUSY;
5050 goto out;
5051 }
5052 #endif
5053
5054 if (splice_grow_spd(pipe, &spd)) {
5055 ret = -ENOMEM;
5056 goto out;
5057 }
5058
5059 if (*ppos & (PAGE_SIZE - 1)) {
5060 ret = -EINVAL;
5061 goto out;
5062 }
5063
5064 if (len & (PAGE_SIZE - 1)) {
5065 if (len < PAGE_SIZE) {
5066 ret = -EINVAL;
5067 goto out;
5068 }
5069 len &= PAGE_MASK;
5070 }
5071
5072 again:
5073 trace_access_lock(iter->cpu_file);
5074 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5075
5076 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
5077 struct page *page;
5078 int r;
5079
5080 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
5081 if (!ref)
5082 break;
5083
5084 ref->ref = 1;
5085 ref->buffer = iter->trace_buffer->buffer;
5086 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
5087 if (!ref->page) {
5088 kfree(ref);
5089 break;
5090 }
5091
5092 r = ring_buffer_read_page(ref->buffer, &ref->page,
5093 len, iter->cpu_file, 1);
5094 if (r < 0) {
5095 ring_buffer_free_read_page(ref->buffer, ref->page);
5096 kfree(ref);
5097 break;
5098 }
5099
5100 /*
5101 * zero out any left over data, this is going to
5102 * user land.
5103 */
5104 size = ring_buffer_page_len(ref->page);
5105 if (size < PAGE_SIZE)
5106 memset(ref->page + size, 0, PAGE_SIZE - size);
5107
5108 page = virt_to_page(ref->page);
5109
5110 spd.pages[i] = page;
5111 spd.partial[i].len = PAGE_SIZE;
5112 spd.partial[i].offset = 0;
5113 spd.partial[i].private = (unsigned long)ref;
5114 spd.nr_pages++;
5115 *ppos += PAGE_SIZE;
5116
5117 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5118 }
5119
5120 trace_access_unlock(iter->cpu_file);
5121 spd.nr_pages = i;
5122
5123 /* did we read anything? */
5124 if (!spd.nr_pages) {
5125 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
5126 ret = -EAGAIN;
5127 goto out;
5128 }
5129 mutex_unlock(&trace_types_lock);
5130 iter->trace->wait_pipe(iter);
5131 mutex_lock(&trace_types_lock);
5132 if (signal_pending(current)) {
5133 ret = -EINTR;
5134 goto out;
5135 }
5136 goto again;
5137 }
5138
5139 ret = splice_to_pipe(pipe, &spd);
5140 splice_shrink_spd(&spd);
5141 out:
5142 mutex_unlock(&trace_types_lock);
5143
5144 return ret;
5145 }
5146
5147 static const struct file_operations tracing_buffers_fops = {
5148 .open = tracing_buffers_open,
5149 .read = tracing_buffers_read,
5150 .poll = tracing_buffers_poll,
5151 .release = tracing_buffers_release,
5152 .splice_read = tracing_buffers_splice_read,
5153 .llseek = no_llseek,
5154 };
5155
5156 static ssize_t
tracing_stats_read(struct file * filp,char __user * ubuf,size_t count,loff_t * ppos)5157 tracing_stats_read(struct file *filp, char __user *ubuf,
5158 size_t count, loff_t *ppos)
5159 {
5160 struct trace_cpu *tc = filp->private_data;
5161 struct trace_array *tr = tc->tr;
5162 struct trace_buffer *trace_buf = &tr->trace_buffer;
5163 struct trace_seq *s;
5164 unsigned long cnt;
5165 unsigned long long t;
5166 unsigned long usec_rem;
5167 int cpu = tc->cpu;
5168
5169 s = kmalloc(sizeof(*s), GFP_KERNEL);
5170 if (!s)
5171 return -ENOMEM;
5172
5173 trace_seq_init(s);
5174
5175 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
5176 trace_seq_printf(s, "entries: %ld\n", cnt);
5177
5178 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
5179 trace_seq_printf(s, "overrun: %ld\n", cnt);
5180
5181 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
5182 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5183
5184 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
5185 trace_seq_printf(s, "bytes: %ld\n", cnt);
5186
5187 if (trace_clocks[tr->clock_id].in_ns) {
5188 /* local or global for trace_clock */
5189 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5190 usec_rem = do_div(t, USEC_PER_SEC);
5191 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5192 t, usec_rem);
5193
5194 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
5195 usec_rem = do_div(t, USEC_PER_SEC);
5196 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5197 } else {
5198 /* counter or tsc mode for trace_clock */
5199 trace_seq_printf(s, "oldest event ts: %llu\n",
5200 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5201
5202 trace_seq_printf(s, "now ts: %llu\n",
5203 ring_buffer_time_stamp(trace_buf->buffer, cpu));
5204 }
5205
5206 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
5207 trace_seq_printf(s, "dropped events: %ld\n", cnt);
5208
5209 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
5210 trace_seq_printf(s, "read events: %ld\n", cnt);
5211
5212 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
5213
5214 kfree(s);
5215
5216 return count;
5217 }
5218
5219 static const struct file_operations tracing_stats_fops = {
5220 .open = tracing_open_generic,
5221 .read = tracing_stats_read,
5222 .llseek = generic_file_llseek,
5223 };
5224
5225 #ifdef CONFIG_DYNAMIC_FTRACE
5226
ftrace_arch_read_dyn_info(char * buf,int size)5227 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
5228 {
5229 return 0;
5230 }
5231
5232 static ssize_t
tracing_read_dyn_info(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)5233 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
5234 size_t cnt, loff_t *ppos)
5235 {
5236 static char ftrace_dyn_info_buffer[1024];
5237 static DEFINE_MUTEX(dyn_info_mutex);
5238 unsigned long *p = filp->private_data;
5239 char *buf = ftrace_dyn_info_buffer;
5240 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
5241 int r;
5242
5243 mutex_lock(&dyn_info_mutex);
5244 r = sprintf(buf, "%ld ", *p);
5245
5246 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
5247 buf[r++] = '\n';
5248
5249 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5250
5251 mutex_unlock(&dyn_info_mutex);
5252
5253 return r;
5254 }
5255
5256 static const struct file_operations tracing_dyn_info_fops = {
5257 .open = tracing_open_generic,
5258 .read = tracing_read_dyn_info,
5259 .llseek = generic_file_llseek,
5260 };
5261 #endif /* CONFIG_DYNAMIC_FTRACE */
5262
5263 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5264 static void
ftrace_snapshot(unsigned long ip,unsigned long parent_ip,void ** data)5265 ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5266 {
5267 tracing_snapshot();
5268 }
5269
5270 static void
ftrace_count_snapshot(unsigned long ip,unsigned long parent_ip,void ** data)5271 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5272 {
5273 unsigned long *count = (long *)data;
5274
5275 if (!*count)
5276 return;
5277
5278 if (*count != -1)
5279 (*count)--;
5280
5281 tracing_snapshot();
5282 }
5283
5284 static int
ftrace_snapshot_print(struct seq_file * m,unsigned long ip,struct ftrace_probe_ops * ops,void * data)5285 ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5286 struct ftrace_probe_ops *ops, void *data)
5287 {
5288 long count = (long)data;
5289
5290 seq_printf(m, "%ps:", (void *)ip);
5291
5292 seq_printf(m, "snapshot");
5293
5294 if (count == -1)
5295 seq_printf(m, ":unlimited\n");
5296 else
5297 seq_printf(m, ":count=%ld\n", count);
5298
5299 return 0;
5300 }
5301
5302 static struct ftrace_probe_ops snapshot_probe_ops = {
5303 .func = ftrace_snapshot,
5304 .print = ftrace_snapshot_print,
5305 };
5306
5307 static struct ftrace_probe_ops snapshot_count_probe_ops = {
5308 .func = ftrace_count_snapshot,
5309 .print = ftrace_snapshot_print,
5310 };
5311
5312 static int
ftrace_trace_snapshot_callback(struct ftrace_hash * hash,char * glob,char * cmd,char * param,int enable)5313 ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5314 char *glob, char *cmd, char *param, int enable)
5315 {
5316 struct ftrace_probe_ops *ops;
5317 void *count = (void *)-1;
5318 char *number;
5319 int ret;
5320
5321 /* hash funcs only work with set_ftrace_filter */
5322 if (!enable)
5323 return -EINVAL;
5324
5325 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
5326
5327 if (glob[0] == '!') {
5328 unregister_ftrace_function_probe_func(glob+1, ops);
5329 return 0;
5330 }
5331
5332 if (!param)
5333 goto out_reg;
5334
5335 number = strsep(¶m, ":");
5336
5337 if (!strlen(number))
5338 goto out_reg;
5339
5340 /*
5341 * We use the callback data field (which is a pointer)
5342 * as our counter.
5343 */
5344 ret = kstrtoul(number, 0, (unsigned long *)&count);
5345 if (ret)
5346 return ret;
5347
5348 out_reg:
5349 ret = register_ftrace_function_probe(glob, ops, count);
5350
5351 if (ret >= 0)
5352 alloc_snapshot(&global_trace);
5353
5354 return ret < 0 ? ret : 0;
5355 }
5356
5357 static struct ftrace_func_command ftrace_snapshot_cmd = {
5358 .name = "snapshot",
5359 .func = ftrace_trace_snapshot_callback,
5360 };
5361
register_snapshot_cmd(void)5362 static int register_snapshot_cmd(void)
5363 {
5364 return register_ftrace_command(&ftrace_snapshot_cmd);
5365 }
5366 #else
register_snapshot_cmd(void)5367 static inline int register_snapshot_cmd(void) { return 0; }
5368 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
5369
tracing_init_dentry_tr(struct trace_array * tr)5370 struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
5371 {
5372 if (tr->dir)
5373 return tr->dir;
5374
5375 if (!debugfs_initialized())
5376 return NULL;
5377
5378 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5379 tr->dir = debugfs_create_dir("tracing", NULL);
5380
5381 if (!tr->dir)
5382 pr_warn_once("Could not create debugfs directory 'tracing'\n");
5383
5384 return tr->dir;
5385 }
5386
tracing_init_dentry(void)5387 struct dentry *tracing_init_dentry(void)
5388 {
5389 return tracing_init_dentry_tr(&global_trace);
5390 }
5391
tracing_dentry_percpu(struct trace_array * tr,int cpu)5392 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
5393 {
5394 struct dentry *d_tracer;
5395
5396 if (tr->percpu_dir)
5397 return tr->percpu_dir;
5398
5399 d_tracer = tracing_init_dentry_tr(tr);
5400 if (!d_tracer)
5401 return NULL;
5402
5403 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
5404
5405 WARN_ONCE(!tr->percpu_dir,
5406 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
5407
5408 return tr->percpu_dir;
5409 }
5410
5411 static void
tracing_init_debugfs_percpu(struct trace_array * tr,long cpu)5412 tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
5413 {
5414 struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
5415 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5416 struct dentry *d_cpu;
5417 char cpu_dir[30]; /* 30 characters should be more than enough */
5418
5419 if (!d_percpu)
5420 return;
5421
5422 snprintf(cpu_dir, 30, "cpu%ld", cpu);
5423 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5424 if (!d_cpu) {
5425 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5426 return;
5427 }
5428
5429 /* per cpu trace_pipe */
5430 trace_create_file("trace_pipe", 0444, d_cpu,
5431 (void *)&data->trace_cpu, &tracing_pipe_fops);
5432
5433 /* per cpu trace */
5434 trace_create_file("trace", 0644, d_cpu,
5435 (void *)&data->trace_cpu, &tracing_fops);
5436
5437 trace_create_file("trace_pipe_raw", 0444, d_cpu,
5438 (void *)&data->trace_cpu, &tracing_buffers_fops);
5439
5440 trace_create_file("stats", 0444, d_cpu,
5441 (void *)&data->trace_cpu, &tracing_stats_fops);
5442
5443 trace_create_file("buffer_size_kb", 0444, d_cpu,
5444 (void *)&data->trace_cpu, &tracing_entries_fops);
5445
5446 #ifdef CONFIG_TRACER_SNAPSHOT
5447 trace_create_file("snapshot", 0644, d_cpu,
5448 (void *)&data->trace_cpu, &snapshot_fops);
5449
5450 trace_create_file("snapshot_raw", 0444, d_cpu,
5451 (void *)&data->trace_cpu, &snapshot_raw_fops);
5452 #endif
5453 }
5454
5455 #ifdef CONFIG_FTRACE_SELFTEST
5456 /* Let selftest have access to static functions in this file */
5457 #include "trace_selftest.c"
5458 #endif
5459
5460 struct trace_option_dentry {
5461 struct tracer_opt *opt;
5462 struct tracer_flags *flags;
5463 struct trace_array *tr;
5464 struct dentry *entry;
5465 };
5466
5467 static ssize_t
trace_options_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)5468 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5469 loff_t *ppos)
5470 {
5471 struct trace_option_dentry *topt = filp->private_data;
5472 char *buf;
5473
5474 if (topt->flags->val & topt->opt->bit)
5475 buf = "1\n";
5476 else
5477 buf = "0\n";
5478
5479 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5480 }
5481
5482 static ssize_t
trace_options_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)5483 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5484 loff_t *ppos)
5485 {
5486 struct trace_option_dentry *topt = filp->private_data;
5487 unsigned long val;
5488 int ret;
5489
5490 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5491 if (ret)
5492 return ret;
5493
5494 if (val != 0 && val != 1)
5495 return -EINVAL;
5496
5497 if (!!(topt->flags->val & topt->opt->bit) != val) {
5498 mutex_lock(&trace_types_lock);
5499 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
5500 topt->opt, !val);
5501 mutex_unlock(&trace_types_lock);
5502 if (ret)
5503 return ret;
5504 }
5505
5506 *ppos += cnt;
5507
5508 return cnt;
5509 }
5510
5511
5512 static const struct file_operations trace_options_fops = {
5513 .open = tracing_open_generic,
5514 .read = trace_options_read,
5515 .write = trace_options_write,
5516 .llseek = generic_file_llseek,
5517 };
5518
5519 static ssize_t
trace_options_core_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)5520 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5521 loff_t *ppos)
5522 {
5523 long index = (long)filp->private_data;
5524 char *buf;
5525
5526 if (trace_flags & (1 << index))
5527 buf = "1\n";
5528 else
5529 buf = "0\n";
5530
5531 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5532 }
5533
5534 static ssize_t
trace_options_core_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)5535 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5536 loff_t *ppos)
5537 {
5538 struct trace_array *tr = &global_trace;
5539 long index = (long)filp->private_data;
5540 unsigned long val;
5541 int ret;
5542
5543 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5544 if (ret)
5545 return ret;
5546
5547 if (val != 0 && val != 1)
5548 return -EINVAL;
5549
5550 mutex_lock(&trace_types_lock);
5551 ret = set_tracer_flag(tr, 1 << index, val);
5552 mutex_unlock(&trace_types_lock);
5553
5554 if (ret < 0)
5555 return ret;
5556
5557 *ppos += cnt;
5558
5559 return cnt;
5560 }
5561
5562 static const struct file_operations trace_options_core_fops = {
5563 .open = tracing_open_generic,
5564 .read = trace_options_core_read,
5565 .write = trace_options_core_write,
5566 .llseek = generic_file_llseek,
5567 };
5568
trace_create_file(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops)5569 struct dentry *trace_create_file(const char *name,
5570 umode_t mode,
5571 struct dentry *parent,
5572 void *data,
5573 const struct file_operations *fops)
5574 {
5575 struct dentry *ret;
5576
5577 ret = debugfs_create_file(name, mode, parent, data, fops);
5578 if (!ret)
5579 pr_warning("Could not create debugfs '%s' entry\n", name);
5580
5581 return ret;
5582 }
5583
5584
trace_options_init_dentry(struct trace_array * tr)5585 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
5586 {
5587 struct dentry *d_tracer;
5588
5589 if (tr->options)
5590 return tr->options;
5591
5592 d_tracer = tracing_init_dentry_tr(tr);
5593 if (!d_tracer)
5594 return NULL;
5595
5596 tr->options = debugfs_create_dir("options", d_tracer);
5597 if (!tr->options) {
5598 pr_warning("Could not create debugfs directory 'options'\n");
5599 return NULL;
5600 }
5601
5602 return tr->options;
5603 }
5604
5605 static void
create_trace_option_file(struct trace_array * tr,struct trace_option_dentry * topt,struct tracer_flags * flags,struct tracer_opt * opt)5606 create_trace_option_file(struct trace_array *tr,
5607 struct trace_option_dentry *topt,
5608 struct tracer_flags *flags,
5609 struct tracer_opt *opt)
5610 {
5611 struct dentry *t_options;
5612
5613 t_options = trace_options_init_dentry(tr);
5614 if (!t_options)
5615 return;
5616
5617 topt->flags = flags;
5618 topt->opt = opt;
5619 topt->tr = tr;
5620
5621 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
5622 &trace_options_fops);
5623
5624 }
5625
5626 static struct trace_option_dentry *
create_trace_option_files(struct trace_array * tr,struct tracer * tracer)5627 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
5628 {
5629 struct trace_option_dentry *topts;
5630 struct tracer_flags *flags;
5631 struct tracer_opt *opts;
5632 int cnt;
5633
5634 if (!tracer)
5635 return NULL;
5636
5637 flags = tracer->flags;
5638
5639 if (!flags || !flags->opts)
5640 return NULL;
5641
5642 opts = flags->opts;
5643
5644 for (cnt = 0; opts[cnt].name; cnt++)
5645 ;
5646
5647 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
5648 if (!topts)
5649 return NULL;
5650
5651 for (cnt = 0; opts[cnt].name; cnt++)
5652 create_trace_option_file(tr, &topts[cnt], flags,
5653 &opts[cnt]);
5654
5655 return topts;
5656 }
5657
5658 static void
destroy_trace_option_files(struct trace_option_dentry * topts)5659 destroy_trace_option_files(struct trace_option_dentry *topts)
5660 {
5661 int cnt;
5662
5663 if (!topts)
5664 return;
5665
5666 for (cnt = 0; topts[cnt].opt; cnt++) {
5667 if (topts[cnt].entry)
5668 debugfs_remove(topts[cnt].entry);
5669 }
5670
5671 kfree(topts);
5672 }
5673
5674 static struct dentry *
create_trace_option_core_file(struct trace_array * tr,const char * option,long index)5675 create_trace_option_core_file(struct trace_array *tr,
5676 const char *option, long index)
5677 {
5678 struct dentry *t_options;
5679
5680 t_options = trace_options_init_dentry(tr);
5681 if (!t_options)
5682 return NULL;
5683
5684 return trace_create_file(option, 0644, t_options, (void *)index,
5685 &trace_options_core_fops);
5686 }
5687
create_trace_options_dir(struct trace_array * tr)5688 static __init void create_trace_options_dir(struct trace_array *tr)
5689 {
5690 struct dentry *t_options;
5691 int i;
5692
5693 t_options = trace_options_init_dentry(tr);
5694 if (!t_options)
5695 return;
5696
5697 for (i = 0; trace_options[i]; i++)
5698 create_trace_option_core_file(tr, trace_options[i], i);
5699 }
5700
5701 static ssize_t
rb_simple_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)5702 rb_simple_read(struct file *filp, char __user *ubuf,
5703 size_t cnt, loff_t *ppos)
5704 {
5705 struct trace_array *tr = filp->private_data;
5706 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5707 char buf[64];
5708 int r;
5709
5710 if (buffer)
5711 r = ring_buffer_record_is_on(buffer);
5712 else
5713 r = 0;
5714
5715 r = sprintf(buf, "%d\n", r);
5716
5717 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5718 }
5719
5720 static ssize_t
rb_simple_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)5721 rb_simple_write(struct file *filp, const char __user *ubuf,
5722 size_t cnt, loff_t *ppos)
5723 {
5724 struct trace_array *tr = filp->private_data;
5725 struct ring_buffer *buffer = tr->trace_buffer.buffer;
5726 unsigned long val;
5727 int ret;
5728
5729 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5730 if (ret)
5731 return ret;
5732
5733 if (buffer) {
5734 mutex_lock(&trace_types_lock);
5735 if (val) {
5736 ring_buffer_record_on(buffer);
5737 if (tr->current_trace->start)
5738 tr->current_trace->start(tr);
5739 } else {
5740 ring_buffer_record_off(buffer);
5741 if (tr->current_trace->stop)
5742 tr->current_trace->stop(tr);
5743 }
5744 mutex_unlock(&trace_types_lock);
5745 }
5746
5747 (*ppos)++;
5748
5749 return cnt;
5750 }
5751
5752 static const struct file_operations rb_simple_fops = {
5753 .open = tracing_open_generic,
5754 .read = rb_simple_read,
5755 .write = rb_simple_write,
5756 .llseek = default_llseek,
5757 };
5758
5759 struct dentry *trace_instance_dir;
5760
5761 static void
5762 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5763
init_trace_buffers(struct trace_array * tr,struct trace_buffer * buf)5764 static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
5765 {
5766 int cpu;
5767
5768 for_each_tracing_cpu(cpu) {
5769 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
5770 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
5771 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
5772 }
5773 }
5774
5775 static int
allocate_trace_buffer(struct trace_array * tr,struct trace_buffer * buf,int size)5776 allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
5777 {
5778 enum ring_buffer_flags rb_flags;
5779
5780 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5781
5782 buf->buffer = ring_buffer_alloc(size, rb_flags);
5783 if (!buf->buffer)
5784 return -ENOMEM;
5785
5786 buf->data = alloc_percpu(struct trace_array_cpu);
5787 if (!buf->data) {
5788 ring_buffer_free(buf->buffer);
5789 return -ENOMEM;
5790 }
5791
5792 init_trace_buffers(tr, buf);
5793
5794 /* Allocate the first page for all buffers */
5795 set_buffer_entries(&tr->trace_buffer,
5796 ring_buffer_size(tr->trace_buffer.buffer, 0));
5797
5798 return 0;
5799 }
5800
allocate_trace_buffers(struct trace_array * tr,int size)5801 static int allocate_trace_buffers(struct trace_array *tr, int size)
5802 {
5803 int ret;
5804
5805 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
5806 if (ret)
5807 return ret;
5808
5809 #ifdef CONFIG_TRACER_MAX_TRACE
5810 ret = allocate_trace_buffer(tr, &tr->max_buffer,
5811 allocate_snapshot ? size : 1);
5812 if (WARN_ON(ret)) {
5813 ring_buffer_free(tr->trace_buffer.buffer);
5814 free_percpu(tr->trace_buffer.data);
5815 return -ENOMEM;
5816 }
5817 tr->allocated_snapshot = allocate_snapshot;
5818
5819 /*
5820 * Only the top level trace array gets its snapshot allocated
5821 * from the kernel command line.
5822 */
5823 allocate_snapshot = false;
5824 #endif
5825 return 0;
5826 }
5827
new_instance_create(const char * name)5828 static int new_instance_create(const char *name)
5829 {
5830 struct trace_array *tr;
5831 int ret;
5832
5833 mutex_lock(&trace_types_lock);
5834
5835 ret = -EEXIST;
5836 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5837 if (tr->name && strcmp(tr->name, name) == 0)
5838 goto out_unlock;
5839 }
5840
5841 ret = -ENOMEM;
5842 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
5843 if (!tr)
5844 goto out_unlock;
5845
5846 tr->name = kstrdup(name, GFP_KERNEL);
5847 if (!tr->name)
5848 goto out_free_tr;
5849
5850 raw_spin_lock_init(&tr->start_lock);
5851
5852 tr->current_trace = &nop_trace;
5853
5854 INIT_LIST_HEAD(&tr->systems);
5855 INIT_LIST_HEAD(&tr->events);
5856
5857 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
5858 goto out_free_tr;
5859
5860 /* Holder for file callbacks */
5861 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5862 tr->trace_cpu.tr = tr;
5863
5864 tr->dir = debugfs_create_dir(name, trace_instance_dir);
5865 if (!tr->dir)
5866 goto out_free_tr;
5867
5868 ret = event_trace_add_tracer(tr->dir, tr);
5869 if (ret)
5870 goto out_free_tr;
5871
5872 init_tracer_debugfs(tr, tr->dir);
5873
5874 list_add(&tr->list, &ftrace_trace_arrays);
5875
5876 mutex_unlock(&trace_types_lock);
5877
5878 return 0;
5879
5880 out_free_tr:
5881 if (tr->trace_buffer.buffer)
5882 ring_buffer_free(tr->trace_buffer.buffer);
5883 kfree(tr->name);
5884 kfree(tr);
5885
5886 out_unlock:
5887 mutex_unlock(&trace_types_lock);
5888
5889 return ret;
5890
5891 }
5892
instance_delete(const char * name)5893 static int instance_delete(const char *name)
5894 {
5895 struct trace_array *tr;
5896 int found = 0;
5897 int ret;
5898
5899 mutex_lock(&trace_types_lock);
5900
5901 ret = -ENODEV;
5902 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5903 if (tr->name && strcmp(tr->name, name) == 0) {
5904 found = 1;
5905 break;
5906 }
5907 }
5908 if (!found)
5909 goto out_unlock;
5910
5911 ret = -EBUSY;
5912 if (tr->ref)
5913 goto out_unlock;
5914
5915 list_del(&tr->list);
5916
5917 event_trace_del_tracer(tr);
5918 debugfs_remove_recursive(tr->dir);
5919 free_percpu(tr->trace_buffer.data);
5920 ring_buffer_free(tr->trace_buffer.buffer);
5921
5922 kfree(tr->name);
5923 kfree(tr);
5924
5925 ret = 0;
5926
5927 out_unlock:
5928 mutex_unlock(&trace_types_lock);
5929
5930 return ret;
5931 }
5932
instance_mkdir(struct inode * inode,struct dentry * dentry,umode_t mode)5933 static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
5934 {
5935 struct dentry *parent;
5936 int ret;
5937
5938 /* Paranoid: Make sure the parent is the "instances" directory */
5939 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5940 if (WARN_ON_ONCE(parent != trace_instance_dir))
5941 return -ENOENT;
5942
5943 /*
5944 * The inode mutex is locked, but debugfs_create_dir() will also
5945 * take the mutex. As the instances directory can not be destroyed
5946 * or changed in any other way, it is safe to unlock it, and
5947 * let the dentry try. If two users try to make the same dir at
5948 * the same time, then the new_instance_create() will determine the
5949 * winner.
5950 */
5951 mutex_unlock(&inode->i_mutex);
5952
5953 ret = new_instance_create(dentry->d_iname);
5954
5955 mutex_lock(&inode->i_mutex);
5956
5957 return ret;
5958 }
5959
instance_rmdir(struct inode * inode,struct dentry * dentry)5960 static int instance_rmdir(struct inode *inode, struct dentry *dentry)
5961 {
5962 struct dentry *parent;
5963 int ret;
5964
5965 /* Paranoid: Make sure the parent is the "instances" directory */
5966 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5967 if (WARN_ON_ONCE(parent != trace_instance_dir))
5968 return -ENOENT;
5969
5970 /* The caller did a dget() on dentry */
5971 mutex_unlock(&dentry->d_inode->i_mutex);
5972
5973 /*
5974 * The inode mutex is locked, but debugfs_create_dir() will also
5975 * take the mutex. As the instances directory can not be destroyed
5976 * or changed in any other way, it is safe to unlock it, and
5977 * let the dentry try. If two users try to make the same dir at
5978 * the same time, then the instance_delete() will determine the
5979 * winner.
5980 */
5981 mutex_unlock(&inode->i_mutex);
5982
5983 ret = instance_delete(dentry->d_iname);
5984
5985 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
5986 mutex_lock(&dentry->d_inode->i_mutex);
5987
5988 return ret;
5989 }
5990
5991 static const struct inode_operations instance_dir_inode_operations = {
5992 .lookup = simple_lookup,
5993 .mkdir = instance_mkdir,
5994 .rmdir = instance_rmdir,
5995 };
5996
create_trace_instances(struct dentry * d_tracer)5997 static __init void create_trace_instances(struct dentry *d_tracer)
5998 {
5999 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
6000 if (WARN_ON(!trace_instance_dir))
6001 return;
6002
6003 /* Hijack the dir inode operations, to allow mkdir */
6004 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
6005 }
6006
6007 static void
init_tracer_debugfs(struct trace_array * tr,struct dentry * d_tracer)6008 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
6009 {
6010 int cpu;
6011
6012 trace_create_file("trace_options", 0644, d_tracer,
6013 tr, &tracing_iter_fops);
6014
6015 trace_create_file("trace", 0644, d_tracer,
6016 (void *)&tr->trace_cpu, &tracing_fops);
6017
6018 trace_create_file("trace_pipe", 0444, d_tracer,
6019 (void *)&tr->trace_cpu, &tracing_pipe_fops);
6020
6021 trace_create_file("buffer_size_kb", 0644, d_tracer,
6022 (void *)&tr->trace_cpu, &tracing_entries_fops);
6023
6024 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
6025 tr, &tracing_total_entries_fops);
6026
6027 trace_create_file("free_buffer", 0644, d_tracer,
6028 tr, &tracing_free_buffer_fops);
6029
6030 trace_create_file("trace_marker", 0220, d_tracer,
6031 tr, &tracing_mark_fops);
6032
6033 trace_create_file("saved_tgids", 0444, d_tracer,
6034 tr, &tracing_saved_tgids_fops);
6035
6036 trace_create_file("trace_clock", 0644, d_tracer, tr,
6037 &trace_clock_fops);
6038
6039 trace_create_file("tracing_on", 0644, d_tracer,
6040 tr, &rb_simple_fops);
6041
6042 #ifdef CONFIG_TRACER_SNAPSHOT
6043 trace_create_file("snapshot", 0644, d_tracer,
6044 (void *)&tr->trace_cpu, &snapshot_fops);
6045 #endif
6046
6047 for_each_tracing_cpu(cpu)
6048 tracing_init_debugfs_percpu(tr, cpu);
6049
6050 }
6051
tracer_init_debugfs(void)6052 static __init int tracer_init_debugfs(void)
6053 {
6054 struct dentry *d_tracer;
6055
6056 trace_access_lock_init();
6057
6058 d_tracer = tracing_init_dentry();
6059 if (!d_tracer)
6060 return 0;
6061
6062 init_tracer_debugfs(&global_trace, d_tracer);
6063
6064 trace_create_file("tracing_cpumask", 0644, d_tracer,
6065 &global_trace, &tracing_cpumask_fops);
6066
6067 trace_create_file("available_tracers", 0444, d_tracer,
6068 &global_trace, &show_traces_fops);
6069
6070 trace_create_file("current_tracer", 0644, d_tracer,
6071 &global_trace, &set_tracer_fops);
6072
6073 #ifdef CONFIG_TRACER_MAX_TRACE
6074 trace_create_file("tracing_max_latency", 0644, d_tracer,
6075 &tracing_max_latency, &tracing_max_lat_fops);
6076 #endif
6077
6078 trace_create_file("tracing_thresh", 0644, d_tracer,
6079 &tracing_thresh, &tracing_max_lat_fops);
6080
6081 trace_create_file("README", 0444, d_tracer,
6082 NULL, &tracing_readme_fops);
6083
6084 trace_create_file("saved_cmdlines", 0444, d_tracer,
6085 NULL, &tracing_saved_cmdlines_fops);
6086
6087 #ifdef CONFIG_DYNAMIC_FTRACE
6088 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
6089 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
6090 #endif
6091
6092 create_trace_instances(d_tracer);
6093
6094 create_trace_options_dir(&global_trace);
6095
6096 return 0;
6097 }
6098
trace_panic_handler(struct notifier_block * this,unsigned long event,void * unused)6099 static int trace_panic_handler(struct notifier_block *this,
6100 unsigned long event, void *unused)
6101 {
6102 if (ftrace_dump_on_oops)
6103 ftrace_dump(ftrace_dump_on_oops);
6104 return NOTIFY_OK;
6105 }
6106
6107 static struct notifier_block trace_panic_notifier = {
6108 .notifier_call = trace_panic_handler,
6109 .next = NULL,
6110 .priority = 150 /* priority: INT_MAX >= x >= 0 */
6111 };
6112
trace_die_handler(struct notifier_block * self,unsigned long val,void * data)6113 static int trace_die_handler(struct notifier_block *self,
6114 unsigned long val,
6115 void *data)
6116 {
6117 switch (val) {
6118 case DIE_OOPS:
6119 if (ftrace_dump_on_oops)
6120 ftrace_dump(ftrace_dump_on_oops);
6121 break;
6122 default:
6123 break;
6124 }
6125 return NOTIFY_OK;
6126 }
6127
6128 static struct notifier_block trace_die_notifier = {
6129 .notifier_call = trace_die_handler,
6130 .priority = 200
6131 };
6132
6133 /*
6134 * printk is set to max of 1024, we really don't need it that big.
6135 * Nothing should be printing 1000 characters anyway.
6136 */
6137 #define TRACE_MAX_PRINT 1000
6138
6139 /*
6140 * Define here KERN_TRACE so that we have one place to modify
6141 * it if we decide to change what log level the ftrace dump
6142 * should be at.
6143 */
6144 #define KERN_TRACE KERN_EMERG
6145
6146 void
trace_printk_seq(struct trace_seq * s)6147 trace_printk_seq(struct trace_seq *s)
6148 {
6149 /* Probably should print a warning here. */
6150 if (s->len >= TRACE_MAX_PRINT)
6151 s->len = TRACE_MAX_PRINT;
6152
6153 /* should be zero ended, but we are paranoid. */
6154 s->buffer[s->len] = 0;
6155
6156 printk(KERN_TRACE "%s", s->buffer);
6157
6158 trace_seq_init(s);
6159 }
6160
trace_init_global_iter(struct trace_iterator * iter)6161 void trace_init_global_iter(struct trace_iterator *iter)
6162 {
6163 iter->tr = &global_trace;
6164 iter->trace = iter->tr->current_trace;
6165 iter->cpu_file = RING_BUFFER_ALL_CPUS;
6166 iter->trace_buffer = &global_trace.trace_buffer;
6167 }
6168
ftrace_dump(enum ftrace_dump_mode oops_dump_mode)6169 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
6170 {
6171 /* use static because iter can be a bit big for the stack */
6172 static struct trace_iterator iter;
6173 static atomic_t dump_running;
6174 unsigned int old_userobj;
6175 unsigned long flags;
6176 int cnt = 0, cpu;
6177
6178 /* Only allow one dump user at a time. */
6179 if (atomic_inc_return(&dump_running) != 1) {
6180 atomic_dec(&dump_running);
6181 return;
6182 }
6183
6184 /*
6185 * Always turn off tracing when we dump.
6186 * We don't need to show trace output of what happens
6187 * between multiple crashes.
6188 *
6189 * If the user does a sysrq-z, then they can re-enable
6190 * tracing with echo 1 > tracing_on.
6191 */
6192 tracing_off();
6193
6194 local_irq_save(flags);
6195
6196 /* Simulate the iterator */
6197 trace_init_global_iter(&iter);
6198
6199 for_each_tracing_cpu(cpu) {
6200 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
6201 }
6202
6203 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
6204
6205 /* don't look at user memory in panic mode */
6206 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
6207
6208 switch (oops_dump_mode) {
6209 case DUMP_ALL:
6210 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6211 break;
6212 case DUMP_ORIG:
6213 iter.cpu_file = raw_smp_processor_id();
6214 break;
6215 case DUMP_NONE:
6216 goto out_enable;
6217 default:
6218 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
6219 iter.cpu_file = RING_BUFFER_ALL_CPUS;
6220 }
6221
6222 printk(KERN_TRACE "Dumping ftrace buffer:\n");
6223
6224 /* Did function tracer already get disabled? */
6225 if (ftrace_is_dead()) {
6226 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
6227 printk("# MAY BE MISSING FUNCTION EVENTS\n");
6228 }
6229
6230 /*
6231 * We need to stop all tracing on all CPUS to read the
6232 * the next buffer. This is a bit expensive, but is
6233 * not done often. We fill all what we can read,
6234 * and then release the locks again.
6235 */
6236
6237 while (!trace_empty(&iter)) {
6238
6239 if (!cnt)
6240 printk(KERN_TRACE "---------------------------------\n");
6241
6242 cnt++;
6243
6244 /* reset all but tr, trace, and overruns */
6245 memset(&iter.seq, 0,
6246 sizeof(struct trace_iterator) -
6247 offsetof(struct trace_iterator, seq));
6248 iter.iter_flags |= TRACE_FILE_LAT_FMT;
6249 iter.pos = -1;
6250
6251 if (trace_find_next_entry_inc(&iter) != NULL) {
6252 int ret;
6253
6254 ret = print_trace_line(&iter);
6255 if (ret != TRACE_TYPE_NO_CONSUME)
6256 trace_consume(&iter);
6257 }
6258 touch_nmi_watchdog();
6259
6260 trace_printk_seq(&iter.seq);
6261 }
6262
6263 if (!cnt)
6264 printk(KERN_TRACE " (ftrace buffer empty)\n");
6265 else
6266 printk(KERN_TRACE "---------------------------------\n");
6267
6268 out_enable:
6269 trace_flags |= old_userobj;
6270
6271 for_each_tracing_cpu(cpu) {
6272 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
6273 }
6274 atomic_dec(&dump_running);
6275 local_irq_restore(flags);
6276 }
6277 EXPORT_SYMBOL_GPL(ftrace_dump);
6278
tracer_alloc_buffers(void)6279 __init static int tracer_alloc_buffers(void)
6280 {
6281 int ring_buf_size;
6282 int ret = -ENOMEM;
6283
6284
6285 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
6286 goto out;
6287
6288 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
6289 goto out_free_buffer_mask;
6290
6291 /* Only allocate trace_printk buffers if a trace_printk exists */
6292 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
6293 /* Must be called before global_trace.buffer is allocated */
6294 trace_printk_init_buffers();
6295
6296 /* To save memory, keep the ring buffer size to its minimum */
6297 if (ring_buffer_expanded)
6298 ring_buf_size = trace_buf_size;
6299 else
6300 ring_buf_size = 1;
6301
6302 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
6303 cpumask_copy(tracing_cpumask, cpu_all_mask);
6304
6305 raw_spin_lock_init(&global_trace.start_lock);
6306
6307 /* TODO: make the number of buffers hot pluggable with CPUS */
6308 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
6309 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
6310 WARN_ON(1);
6311 goto out_free_cpumask;
6312 }
6313
6314 if (global_trace.buffer_disabled)
6315 tracing_off();
6316
6317 trace_init_cmdlines();
6318
6319 /*
6320 * register_tracer() might reference current_trace, so it
6321 * needs to be set before we register anything. This is
6322 * just a bootstrap of current_trace anyway.
6323 */
6324 global_trace.current_trace = &nop_trace;
6325
6326 register_tracer(&nop_trace);
6327
6328 /* All seems OK, enable tracing */
6329 tracing_disabled = 0;
6330
6331 atomic_notifier_chain_register(&panic_notifier_list,
6332 &trace_panic_notifier);
6333
6334 register_die_notifier(&trace_die_notifier);
6335
6336 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
6337
6338 /* Holder for file callbacks */
6339 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
6340 global_trace.trace_cpu.tr = &global_trace;
6341
6342 INIT_LIST_HEAD(&global_trace.systems);
6343 INIT_LIST_HEAD(&global_trace.events);
6344 list_add(&global_trace.list, &ftrace_trace_arrays);
6345
6346 while (trace_boot_options) {
6347 char *option;
6348
6349 option = strsep(&trace_boot_options, ",");
6350 trace_set_options(&global_trace, option);
6351 }
6352
6353 register_snapshot_cmd();
6354
6355 return 0;
6356
6357 out_free_cpumask:
6358 free_percpu(global_trace.trace_buffer.data);
6359 #ifdef CONFIG_TRACER_MAX_TRACE
6360 free_percpu(global_trace.max_buffer.data);
6361 #endif
6362 free_cpumask_var(tracing_cpumask);
6363 out_free_buffer_mask:
6364 free_cpumask_var(tracing_buffer_mask);
6365 out:
6366 return ret;
6367 }
6368
clear_boot_tracer(void)6369 __init static int clear_boot_tracer(void)
6370 {
6371 /*
6372 * The default tracer at boot buffer is an init section.
6373 * This function is called in lateinit. If we did not
6374 * find the boot tracer, then clear it out, to prevent
6375 * later registration from accessing the buffer that is
6376 * about to be freed.
6377 */
6378 if (!default_bootup_tracer)
6379 return 0;
6380
6381 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6382 default_bootup_tracer);
6383 default_bootup_tracer = NULL;
6384
6385 return 0;
6386 }
6387
6388 early_initcall(tracer_alloc_buffers);
6389 fs_initcall(tracer_init_debugfs);
6390 late_initcall(clear_boot_tracer);
6391