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