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