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